1 | /* $NetBSD: ulpt.c,v 1.97 2016/07/07 06:55:42 msaitoh Exp $ */ |
2 | |
3 | /* |
4 | * Copyright (c) 1998, 2003 The NetBSD Foundation, Inc. |
5 | * All rights reserved. |
6 | * |
7 | * This code is derived from software contributed to The NetBSD Foundation |
8 | * by Lennart Augustsson (lennart@augustsson.net) at |
9 | * Carlstedt Research & Technology. |
10 | * |
11 | * Redistribution and use in source and binary forms, with or without |
12 | * modification, are permitted provided that the following conditions |
13 | * are met: |
14 | * 1. Redistributions of source code must retain the above copyright |
15 | * notice, this list of conditions and the following disclaimer. |
16 | * 2. Redistributions in binary form must reproduce the above copyright |
17 | * notice, this list of conditions and the following disclaimer in the |
18 | * documentation and/or other materials provided with the distribution. |
19 | * |
20 | * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS |
21 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
22 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
23 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS |
24 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
25 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
26 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
27 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
28 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
29 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
30 | * POSSIBILITY OF SUCH DAMAGE. |
31 | */ |
32 | |
33 | /* |
34 | * Printer Class spec: http://www.usb.org/developers/data/devclass/usbprint109.PDF |
35 | */ |
36 | |
37 | #include <sys/cdefs.h> |
38 | __KERNEL_RCSID(0, "$NetBSD: ulpt.c,v 1.97 2016/07/07 06:55:42 msaitoh Exp $" ); |
39 | |
40 | #include <sys/param.h> |
41 | #include <sys/systm.h> |
42 | #include <sys/proc.h> |
43 | #include <sys/kernel.h> |
44 | #include <sys/fcntl.h> |
45 | #include <sys/device.h> |
46 | #include <sys/ioctl.h> |
47 | #include <sys/uio.h> |
48 | #include <sys/conf.h> |
49 | #include <sys/vnode.h> |
50 | #include <sys/syslog.h> |
51 | |
52 | #include <machine/vmparam.h> /* PAGE_SIZE */ |
53 | |
54 | #include <dev/usb/usb.h> |
55 | #include <dev/usb/usbdi.h> |
56 | #include <dev/usb/usbdi_util.h> |
57 | #include <dev/usb/usbdevs.h> |
58 | #include <dev/usb/usb_quirks.h> |
59 | |
60 | #define TIMEOUT hz*16 /* wait up to 16 seconds for a ready */ |
61 | #define STEP hz/4 |
62 | |
63 | #define LPTPRI (PZERO+8) |
64 | #define ULPT_BSIZE PAGE_SIZE |
65 | |
66 | #define ULPT_READS_PER_SEC 5 |
67 | /* XXX Why is 10 us a reasonable value? */ |
68 | #define ULPT_READ_TIMO 10 |
69 | |
70 | #ifdef ULPT_DEBUG |
71 | #define DPRINTFN(n,x) if (ulptdebug>=(n)) printf x |
72 | int ulptdebug = 0; |
73 | /* |
74 | * The strategy for debug levels is: |
75 | * 1: attach-time operations |
76 | * 2: open/close/status/reset |
77 | * 3: read/write basic |
78 | * 4: read/write details |
79 | * 10: left over from previous debug code |
80 | */ |
81 | #else |
82 | #define DPRINTFN(n,x) |
83 | #endif |
84 | |
85 | #define UR_GET_DEVICE_ID 0 |
86 | #define UR_GET_PORT_STATUS 1 |
87 | #define UR_SOFT_RESET 2 |
88 | |
89 | #define LPS_NERR 0x08 /* printer no error */ |
90 | #define LPS_SELECT 0x10 /* printer selected */ |
91 | #define LPS_NOPAPER 0x20 /* printer out of paper */ |
92 | #define LPS_INVERT (LPS_SELECT|LPS_NERR) |
93 | #define LPS_MASK (LPS_SELECT|LPS_NERR|LPS_NOPAPER) |
94 | |
95 | struct ulpt_softc { |
96 | device_t sc_dev; |
97 | struct usbd_device *sc_udev; /* device */ |
98 | struct usbd_interface *sc_iface; /* interface */ |
99 | int sc_ifaceno; |
100 | |
101 | int sc_out; |
102 | struct usbd_pipe *sc_out_pipe; /* bulk out pipe */ |
103 | struct usbd_xfer *sc_out_xfer; |
104 | void *sc_out_buf; |
105 | |
106 | int sc_in; |
107 | struct usbd_pipe *sc_in_pipe; /* bulk in pipe */ |
108 | struct usbd_xfer *sc_in_xfer; |
109 | void *sc_in_buf; |
110 | |
111 | struct callout sc_read_callout; /* to drain input on write-only opens */ |
112 | int sc_has_callout; |
113 | |
114 | u_char sc_state; |
115 | #define ULPT_OPEN 0x01 /* device is open */ |
116 | #define ULPT_OBUSY 0x02 /* printer is busy doing output */ |
117 | #define ULPT_INIT 0x04 /* waiting to initialize for open */ |
118 | u_char sc_flags; |
119 | #define ULPT_NOPRIME 0x40 /* don't prime on open */ |
120 | u_char sc_laststatus; |
121 | |
122 | int sc_refcnt; |
123 | u_char sc_dying; |
124 | }; |
125 | |
126 | dev_type_open(ulptopen); |
127 | dev_type_close(ulptclose); |
128 | dev_type_write(ulptwrite); |
129 | dev_type_read(ulptread); |
130 | dev_type_ioctl(ulptioctl); |
131 | |
132 | const struct cdevsw ulpt_cdevsw = { |
133 | .d_open = ulptopen, |
134 | .d_close = ulptclose, |
135 | .d_read = ulptread, |
136 | .d_write = ulptwrite, |
137 | .d_ioctl = ulptioctl, |
138 | .d_stop = nostop, |
139 | .d_tty = notty, |
140 | .d_poll = nopoll, |
141 | .d_mmap = nommap, |
142 | .d_kqfilter = nokqfilter, |
143 | .d_discard = nodiscard, |
144 | .d_flag = D_OTHER |
145 | }; |
146 | |
147 | void ulpt_disco(void *); |
148 | |
149 | int ulpt_do_write(struct ulpt_softc *, struct uio *, int); |
150 | int ulpt_do_read(struct ulpt_softc *, struct uio *, int); |
151 | int ulpt_status(struct ulpt_softc *); |
152 | void ulpt_reset(struct ulpt_softc *); |
153 | int ulpt_statusmsg(u_char, struct ulpt_softc *); |
154 | void ulpt_read_cb(struct usbd_xfer *, void *, usbd_status); |
155 | void ulpt_tick(void *xsc); |
156 | |
157 | #if 0 |
158 | void ieee1284_print_id(char *); |
159 | #endif |
160 | |
161 | #define ULPTUNIT(s) (minor(s) & 0x1f) |
162 | #define ULPTFLAGS(s) (minor(s) & 0xe0) |
163 | |
164 | |
165 | int ulpt_match(device_t, cfdata_t, void *); |
166 | void ulpt_attach(device_t, device_t, void *); |
167 | int ulpt_detach(device_t, int); |
168 | int ulpt_activate(device_t, enum devact); |
169 | |
170 | extern struct cfdriver ulpt_cd; |
171 | |
172 | CFATTACH_DECL_NEW(ulpt, sizeof(struct ulpt_softc), ulpt_match, ulpt_attach, |
173 | ulpt_detach, ulpt_activate); |
174 | |
175 | int |
176 | ulpt_match(device_t parent, cfdata_t match, void *aux) |
177 | { |
178 | struct usbif_attach_arg *uiaa = aux; |
179 | /* XXX Print something useful, or don't. */ |
180 | DPRINTFN(10,("ulpt_match\n" )); |
181 | |
182 | if (uiaa->uiaa_class == UICLASS_PRINTER && |
183 | uiaa->uiaa_subclass == UISUBCLASS_PRINTER && |
184 | (uiaa->uiaa_proto == UIPROTO_PRINTER_UNI || |
185 | uiaa->uiaa_proto == UIPROTO_PRINTER_BI || |
186 | uiaa->uiaa_proto == UIPROTO_PRINTER_1284)) |
187 | return UMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO; |
188 | return UMATCH_NONE; |
189 | } |
190 | |
191 | void |
192 | ulpt_attach(device_t parent, device_t self, void *aux) |
193 | { |
194 | struct ulpt_softc *sc = device_private(self); |
195 | struct usbif_attach_arg *uiaa = aux; |
196 | struct usbd_device *dev = uiaa->uiaa_device; |
197 | struct usbd_interface *iface = uiaa->uiaa_iface; |
198 | usb_interface_descriptor_t *ifcd = usbd_get_interface_descriptor(iface); |
199 | const usb_interface_descriptor_t *id; |
200 | usbd_status err; |
201 | char *devinfop; |
202 | usb_endpoint_descriptor_t *ed; |
203 | uint8_t epcount; |
204 | int i, altno; |
205 | usbd_desc_iter_t iter; |
206 | |
207 | sc->sc_dev = self; |
208 | |
209 | aprint_naive("\n" ); |
210 | aprint_normal("\n" ); |
211 | |
212 | devinfop = usbd_devinfo_alloc(dev, 0); |
213 | aprint_normal_dev(self, "%s, iclass %d/%d\n" , |
214 | devinfop, ifcd->bInterfaceClass, ifcd->bInterfaceSubClass); |
215 | usbd_devinfo_free(devinfop); |
216 | |
217 | /* Loop through descriptors looking for a bidir mode. */ |
218 | usb_desc_iter_init(dev, &iter); |
219 | for (altno = 0;;) { |
220 | id = (const usb_interface_descriptor_t *)usb_desc_iter_next(&iter); |
221 | if (!id) |
222 | break; |
223 | if (id->bDescriptorType == UDESC_INTERFACE && |
224 | id->bInterfaceNumber == ifcd->bInterfaceNumber) { |
225 | if (id->bInterfaceClass == UICLASS_PRINTER && |
226 | id->bInterfaceSubClass == UISUBCLASS_PRINTER && |
227 | (id->bInterfaceProtocol == UIPROTO_PRINTER_BI /*|| |
228 | id->bInterfaceProtocol == UIPROTO_PRINTER_1284*/)) |
229 | goto found; |
230 | altno++; |
231 | } |
232 | } |
233 | id = ifcd; /* not found, use original */ |
234 | found: |
235 | if (id != ifcd) { |
236 | /* Found a new bidir setting */ |
237 | DPRINTFN(1, ("ulpt_attach: set altno = %d\n" , altno)); |
238 | err = usbd_set_interface(iface, altno); |
239 | if (err) { |
240 | aprint_error_dev(self, |
241 | "setting alternate interface failed\n" ); |
242 | sc->sc_dying = 1; |
243 | return; |
244 | } |
245 | } |
246 | |
247 | epcount = 0; |
248 | (void)usbd_endpoint_count(iface, &epcount); |
249 | |
250 | sc->sc_in = -1; |
251 | sc->sc_out = -1; |
252 | for (i = 0; i < epcount; i++) { |
253 | ed = usbd_interface2endpoint_descriptor(iface, i); |
254 | if (ed == NULL) { |
255 | aprint_error_dev(self, "couldn't get ep %d\n" , i); |
256 | return; |
257 | } |
258 | if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN && |
259 | UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) { |
260 | sc->sc_in = ed->bEndpointAddress; |
261 | } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT && |
262 | UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) { |
263 | sc->sc_out = ed->bEndpointAddress; |
264 | } |
265 | } |
266 | if (sc->sc_out == -1) { |
267 | aprint_error_dev(self, "could not find bulk out endpoint\n" ); |
268 | sc->sc_dying = 1; |
269 | return; |
270 | } |
271 | |
272 | if (usbd_get_quirks(dev)->uq_flags & UQ_BROKEN_BIDIR) { |
273 | /* This device doesn't handle reading properly. */ |
274 | sc->sc_in = -1; |
275 | } |
276 | |
277 | aprint_normal_dev(self, "using %s-directional mode\n" , |
278 | sc->sc_in >= 0 ? "bi" : "uni" ); |
279 | |
280 | sc->sc_iface = iface; |
281 | sc->sc_ifaceno = id->bInterfaceNumber; |
282 | sc->sc_udev = dev; |
283 | |
284 | #if 0 |
285 | /* |
286 | * This code is disabled because for some mysterious reason it causes |
287 | * printing not to work. But only sometimes, and mostly with |
288 | * UHCI and less often with OHCI. *sigh* |
289 | */ |
290 | { |
291 | usb_config_descriptor_t *cd = usbd_get_config_descriptor(dev); |
292 | usb_device_request_t req; |
293 | int len, alen; |
294 | |
295 | req.bmRequestType = UT_READ_CLASS_INTERFACE; |
296 | req.bRequest = UR_GET_DEVICE_ID; |
297 | USETW(req.wValue, cd->bConfigurationValue); |
298 | USETW2(req.wIndex, id->bInterfaceNumber, id->bAlternateSetting); |
299 | USETW(req.wLength, DEVINFOSIZE - 1); |
300 | err = usbd_do_request_flags(dev, &req, devinfop, USBD_SHORT_XFER_OK, |
301 | &alen, USBD_DEFAULT_TIMEOUT); |
302 | if (err) { |
303 | printf("%s: cannot get device id\n" , device_xname(sc->sc_dev)); |
304 | } else if (alen <= 2) { |
305 | printf("%s: empty device id, no printer connected?\n" , |
306 | device_xname(sc->sc_dev)); |
307 | } else { |
308 | /* devinfop now contains an IEEE-1284 device ID */ |
309 | len = ((devinfop[0] & 0xff) << 8) | (devinfop[1] & 0xff); |
310 | if (len > DEVINFOSIZE - 3) |
311 | len = DEVINFOSIZE - 3; |
312 | devinfop[len] = 0; |
313 | printf("%s: device id <" , device_xname(sc->sc_dev)); |
314 | ieee1284_print_id(devinfop+2); |
315 | printf(">\n" ); |
316 | } |
317 | } |
318 | #endif |
319 | |
320 | usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, sc->sc_dev); |
321 | |
322 | DPRINTFN(1, ("ulpt_attach: sc=%p in=%d out=%d\n" , |
323 | sc, sc->sc_out, sc->sc_in)); |
324 | |
325 | return; |
326 | } |
327 | |
328 | int |
329 | ulpt_activate(device_t self, enum devact act) |
330 | { |
331 | struct ulpt_softc *sc = device_private(self); |
332 | |
333 | switch (act) { |
334 | case DVACT_DEACTIVATE: |
335 | sc->sc_dying = 1; |
336 | return 0; |
337 | default: |
338 | return EOPNOTSUPP; |
339 | } |
340 | } |
341 | |
342 | int |
343 | ulpt_detach(device_t self, int flags) |
344 | { |
345 | struct ulpt_softc *sc = device_private(self); |
346 | int s; |
347 | int maj, mn; |
348 | |
349 | DPRINTFN(1, ("ulpt_detach: sc=%p\n" , sc)); |
350 | |
351 | sc->sc_dying = 1; |
352 | if (sc->sc_out_pipe != NULL) |
353 | usbd_abort_pipe(sc->sc_out_pipe); |
354 | if (sc->sc_in_pipe != NULL) |
355 | usbd_abort_pipe(sc->sc_in_pipe); |
356 | |
357 | s = splusb(); |
358 | if (--sc->sc_refcnt >= 0) { |
359 | /* There is noone to wake, aborting the pipe is enough */ |
360 | /* Wait for processes to go away. */ |
361 | usb_detach_waitold(sc->sc_dev); |
362 | } |
363 | splx(s); |
364 | |
365 | /* locate the major number */ |
366 | maj = cdevsw_lookup_major(&ulpt_cdevsw); |
367 | |
368 | /* Nuke the vnodes for any open instances (calls close). */ |
369 | mn = device_unit(self); |
370 | vdevgone(maj, mn, mn, VCHR); |
371 | vdevgone(maj, mn | ULPT_NOPRIME , mn | ULPT_NOPRIME, VCHR); |
372 | |
373 | usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, sc->sc_dev); |
374 | |
375 | return 0; |
376 | } |
377 | |
378 | int |
379 | ulpt_status(struct ulpt_softc *sc) |
380 | { |
381 | usb_device_request_t req; |
382 | usbd_status err; |
383 | u_char status; |
384 | |
385 | req.bmRequestType = UT_READ_CLASS_INTERFACE; |
386 | req.bRequest = UR_GET_PORT_STATUS; |
387 | USETW(req.wValue, 0); |
388 | USETW(req.wIndex, sc->sc_ifaceno); |
389 | USETW(req.wLength, 1); |
390 | err = usbd_do_request(sc->sc_udev, &req, &status); |
391 | DPRINTFN(2, ("ulpt_status: status=0x%02x err=%d\n" , status, err)); |
392 | if (!err) |
393 | return status; |
394 | else |
395 | return 0; |
396 | } |
397 | |
398 | void |
399 | ulpt_reset(struct ulpt_softc *sc) |
400 | { |
401 | usb_device_request_t req; |
402 | |
403 | DPRINTFN(2, ("ulpt_reset\n" )); |
404 | req.bRequest = UR_SOFT_RESET; |
405 | USETW(req.wValue, 0); |
406 | USETW(req.wIndex, sc->sc_ifaceno); |
407 | USETW(req.wLength, 0); |
408 | |
409 | /* |
410 | * There was a mistake in the USB printer 1.0 spec that gave the |
411 | * request type as UT_WRITE_CLASS_OTHER; it should have been |
412 | * UT_WRITE_CLASS_INTERFACE. Many printers use the old one, |
413 | * so we try both. |
414 | */ |
415 | req.bmRequestType = UT_WRITE_CLASS_OTHER; |
416 | if (usbd_do_request(sc->sc_udev, &req, 0)) { /* 1.0 */ |
417 | req.bmRequestType = UT_WRITE_CLASS_INTERFACE; |
418 | (void)usbd_do_request(sc->sc_udev, &req, 0); /* 1.1 */ |
419 | } |
420 | } |
421 | |
422 | int ulptusein = 1; |
423 | |
424 | /* |
425 | * Reset the printer, then wait until it's selected and not busy. |
426 | */ |
427 | int |
428 | ulptopen(dev_t dev, int flag, int mode, struct lwp *l) |
429 | { |
430 | u_char flags = ULPTFLAGS(dev); |
431 | struct ulpt_softc *sc; |
432 | usbd_status err; |
433 | int spin, error; |
434 | |
435 | sc = device_lookup_private(&ulpt_cd, ULPTUNIT(dev)); |
436 | if (sc == NULL) |
437 | return ENXIO; |
438 | |
439 | if (sc == NULL || sc->sc_iface == NULL || sc->sc_dying) |
440 | return ENXIO; |
441 | |
442 | if (sc->sc_state) |
443 | return EBUSY; |
444 | |
445 | sc->sc_state = ULPT_INIT; |
446 | sc->sc_flags = flags; |
447 | DPRINTFN(2, ("ulptopen: flags=0x%x\n" , (unsigned)flags)); |
448 | |
449 | error = 0; |
450 | sc->sc_refcnt++; |
451 | |
452 | if ((flags & ULPT_NOPRIME) == 0) |
453 | ulpt_reset(sc); |
454 | |
455 | for (spin = 0; (ulpt_status(sc) & LPS_SELECT) == 0; spin += STEP) { |
456 | DPRINTFN(2, ("ulpt_open: waiting a while\n" )); |
457 | if (spin >= TIMEOUT) { |
458 | error = EBUSY; |
459 | sc->sc_state = 0; |
460 | goto done; |
461 | } |
462 | |
463 | /* wait 1/4 second, give up if we get a signal */ |
464 | error = tsleep((void *)sc, LPTPRI | PCATCH, "ulptop" , STEP); |
465 | if (error != EWOULDBLOCK) { |
466 | sc->sc_state = 0; |
467 | goto done; |
468 | } |
469 | |
470 | if (sc->sc_dying) { |
471 | error = ENXIO; |
472 | sc->sc_state = 0; |
473 | goto done; |
474 | } |
475 | } |
476 | |
477 | err = usbd_open_pipe(sc->sc_iface, sc->sc_out, 0, &sc->sc_out_pipe); |
478 | if (err) { |
479 | error = EIO; |
480 | goto err0; |
481 | } |
482 | error = usbd_create_xfer(sc->sc_out_pipe, ULPT_BSIZE, 0, 0, |
483 | &sc->sc_out_xfer); |
484 | if (error) |
485 | goto err2; |
486 | sc->sc_out_buf = usbd_get_buffer(sc->sc_out_xfer); |
487 | |
488 | if (ulptusein && sc->sc_in != -1) { |
489 | DPRINTFN(2, ("ulpt_open: opening input pipe %d\n" , sc->sc_in)); |
490 | err = usbd_open_pipe(sc->sc_iface, sc->sc_in,0,&sc->sc_in_pipe); |
491 | if (err) { |
492 | error = EIO; |
493 | goto err2; |
494 | } |
495 | error = usbd_create_xfer(sc->sc_in_pipe, ULPT_BSIZE, |
496 | USBD_SHORT_XFER_OK, 0, &sc->sc_in_xfer); |
497 | if (error) |
498 | goto err3; |
499 | sc->sc_in_buf = usbd_get_buffer(sc->sc_in_xfer); |
500 | /* If it's not opened for read then set up a reader. */ |
501 | if (!(flag & FREAD)) { |
502 | DPRINTFN(2, ("ulpt_open: start read callout\n" )); |
503 | callout_init(&sc->sc_read_callout, 0); |
504 | callout_reset(&sc->sc_read_callout, hz/5, ulpt_tick, sc); |
505 | sc->sc_has_callout = 1; |
506 | } |
507 | } |
508 | |
509 | sc->sc_state = ULPT_OPEN; |
510 | goto done; |
511 | |
512 | err3: |
513 | usbd_close_pipe(sc->sc_in_pipe); |
514 | sc->sc_in_pipe = NULL; |
515 | err2: |
516 | usbd_destroy_xfer(sc->sc_out_xfer); |
517 | sc->sc_out_xfer = NULL; |
518 | |
519 | usbd_close_pipe(sc->sc_out_pipe); |
520 | sc->sc_out_pipe = NULL; |
521 | err0: |
522 | sc->sc_state = 0; |
523 | |
524 | done: |
525 | if (--sc->sc_refcnt < 0) |
526 | usb_detach_wakeupold(sc->sc_dev); |
527 | |
528 | DPRINTFN(2, ("ulptopen: done, error=%d\n" , error)); |
529 | return error; |
530 | } |
531 | |
532 | /* |
533 | * XXX Document return value semantics. |
534 | */ |
535 | int |
536 | ulpt_statusmsg(u_char status, struct ulpt_softc *sc) |
537 | { |
538 | u_char new; |
539 | |
540 | status = (status ^ LPS_INVERT) & LPS_MASK; |
541 | new = status & ~sc->sc_laststatus; |
542 | sc->sc_laststatus = status; |
543 | |
544 | if (new & LPS_SELECT) |
545 | log(LOG_NOTICE, "%s: offline\n" , device_xname(sc->sc_dev)); |
546 | if (new & LPS_NOPAPER) |
547 | log(LOG_NOTICE, "%s: out of paper\n" , device_xname(sc->sc_dev)); |
548 | if (new & LPS_NERR) |
549 | log(LOG_NOTICE, "%s: output error\n" , device_xname(sc->sc_dev)); |
550 | |
551 | return status; |
552 | } |
553 | |
554 | int |
555 | ulptclose(dev_t dev, int flag, int mode, |
556 | struct lwp *l) |
557 | { |
558 | struct ulpt_softc *sc; |
559 | |
560 | sc = device_lookup_private(&ulpt_cd, ULPTUNIT(dev)); |
561 | |
562 | if (sc->sc_state != ULPT_OPEN) |
563 | /* We are being forced to close before the open completed. */ |
564 | return 0; |
565 | |
566 | if (sc->sc_has_callout) { |
567 | DPRINTFN(2, ("ulptclose: stopping read callout\n" )); |
568 | callout_halt(&sc->sc_read_callout, NULL); |
569 | callout_destroy(&sc->sc_read_callout); |
570 | sc->sc_has_callout = 0; |
571 | } |
572 | |
573 | if (sc->sc_out_pipe != NULL) { |
574 | usbd_abort_pipe(sc->sc_out_pipe); |
575 | } |
576 | if (sc->sc_out_xfer != NULL) { |
577 | usbd_destroy_xfer(sc->sc_out_xfer); |
578 | sc->sc_out_xfer = NULL; |
579 | } |
580 | if (sc->sc_out_pipe != NULL) { |
581 | usbd_close_pipe(sc->sc_out_pipe); |
582 | sc->sc_out_pipe = NULL; |
583 | } |
584 | if (sc->sc_in_pipe != NULL) { |
585 | usbd_abort_pipe(sc->sc_in_pipe); |
586 | } |
587 | if (sc->sc_in_xfer != NULL) { |
588 | usbd_destroy_xfer(sc->sc_in_xfer); |
589 | sc->sc_in_xfer = NULL; |
590 | } |
591 | if (sc->sc_in_pipe != NULL) { |
592 | usbd_close_pipe(sc->sc_in_pipe); |
593 | sc->sc_in_pipe = NULL; |
594 | } |
595 | |
596 | sc->sc_state = 0; |
597 | |
598 | DPRINTFN(2, ("ulptclose: closed\n" )); |
599 | return 0; |
600 | } |
601 | |
602 | int |
603 | ulpt_do_write(struct ulpt_softc *sc, struct uio *uio, int flags) |
604 | { |
605 | uint32_t n; |
606 | int error = 0; |
607 | void *bufp; |
608 | struct usbd_xfer *xfer; |
609 | usbd_status err; |
610 | |
611 | DPRINTFN(3, ("ulptwrite\n" )); |
612 | xfer = sc->sc_out_xfer; |
613 | bufp = sc->sc_out_buf; |
614 | while ((n = min(ULPT_BSIZE, uio->uio_resid)) != 0) { |
615 | ulpt_statusmsg(ulpt_status(sc), sc); |
616 | error = uiomove(bufp, n, uio); |
617 | if (error) |
618 | break; |
619 | DPRINTFN(4, ("ulptwrite: transfer %d bytes\n" , n)); |
620 | err = usbd_bulk_transfer(xfer, sc->sc_out_pipe, 0, |
621 | USBD_NO_TIMEOUT, bufp, &n); |
622 | if (err) { |
623 | DPRINTFN(3, ("ulptwrite: error=%d\n" , err)); |
624 | error = EIO; |
625 | break; |
626 | } |
627 | } |
628 | |
629 | return error; |
630 | } |
631 | |
632 | int |
633 | ulptwrite(dev_t dev, struct uio *uio, int flags) |
634 | { |
635 | struct ulpt_softc *sc; |
636 | int error; |
637 | |
638 | sc = device_lookup_private(&ulpt_cd, ULPTUNIT(dev)); |
639 | |
640 | if (sc->sc_dying) |
641 | return EIO; |
642 | |
643 | sc->sc_refcnt++; |
644 | error = ulpt_do_write(sc, uio, flags); |
645 | if (--sc->sc_refcnt < 0) |
646 | usb_detach_wakeupold(sc->sc_dev); |
647 | return error; |
648 | } |
649 | |
650 | /* |
651 | * Perform a read operation according to the given uio. |
652 | * This should respect nonblocking I/O status. |
653 | * |
654 | * XXX Doing a short read when more data is available seems to be |
655 | * problematic. See |
656 | * http://www.freebsd.org/cgi/query-pr.cgi?pr=91538&cat= for a fix. |
657 | * However, this will be unnecessary given a proper fix for the next |
658 | * problem, and most actual callers read a lot. |
659 | * |
660 | * XXX This code should interact properly with select/poll, and that |
661 | * requires the USB transactions to be queued and function before the |
662 | * user does a read. Read will then consume data from a buffer, and |
663 | * not interact with the device. See ucom.c for an example of how to |
664 | * do this. |
665 | */ |
666 | int |
667 | ulpt_do_read(struct ulpt_softc *sc, struct uio *uio, int flags) |
668 | { |
669 | uint32_t n, nread, nreq; |
670 | int error = 0, nonblocking, timeout; |
671 | void *bufp; |
672 | struct usbd_xfer *xfer; |
673 | usbd_status err = USBD_NORMAL_COMPLETION; |
674 | |
675 | /* XXX Resolve with background reader process. KASSERT? */ |
676 | if (sc->sc_in_pipe == NULL) |
677 | return EIO; |
678 | |
679 | if (flags & IO_NDELAY) |
680 | nonblocking = 1; |
681 | else |
682 | nonblocking = 0; |
683 | |
684 | if (nonblocking) |
685 | timeout = USBD_DEFAULT_TIMEOUT; /* 5 ms */ |
686 | else |
687 | timeout = USBD_NO_TIMEOUT; |
688 | |
689 | DPRINTFN(3, ("ulptread nonblocking=%d uio_reside=%ld timeout=%d\n" , |
690 | nonblocking, (u_long)uio->uio_resid, timeout)); |
691 | |
692 | xfer = sc->sc_in_xfer; |
693 | bufp = sc->sc_in_buf; |
694 | nread = 0; |
695 | while ((nreq = min(ULPT_BSIZE, uio->uio_resid)) != 0) { |
696 | KASSERT(error == 0); |
697 | if (error != 0) { |
698 | printf("ulptread: pre-switch error %d != 0" , error); |
699 | goto done; |
700 | } |
701 | |
702 | /* |
703 | * XXX Even with the short timeout, this will tsleep, |
704 | * but it should be adequately prompt in practice. |
705 | */ |
706 | n = nreq; |
707 | DPRINTFN(4, ("ulptread: transfer %d bytes, nonblocking=%d timeout=%d\n" , |
708 | n, nonblocking, timeout)); |
709 | err = usbd_bulk_transfer(xfer, sc->sc_in_pipe, |
710 | USBD_SHORT_XFER_OK, timeout, bufp, &n); |
711 | |
712 | DPRINTFN(4, ("ulptread: transfer complete nreq %d n %d nread %d err %d\n" , |
713 | nreq, n, nread, err)); |
714 | /* |
715 | * Process "err" return, jumping to done if we set "error". |
716 | */ |
717 | switch (err) { |
718 | case USBD_NORMAL_COMPLETION: |
719 | if (n == 0) { |
720 | DPRINTFN(3, ("ulptread: NORMAL n==0\n" )); |
721 | } |
722 | break; |
723 | |
724 | case USBD_SHORT_XFER: |
725 | /* We said SHORT_XFER_OK, so shouldn't happen. */ |
726 | DPRINTFN(3, ("ulptread: SHORT n=%d\n" , n)); |
727 | break; |
728 | |
729 | case USBD_TIMEOUT: |
730 | if (nonblocking == 0) { |
731 | /* XXX Cannot happen; perhaps KASSERT. */ |
732 | printf("ulptread: timeout in blocking mode\n" ); |
733 | error = EIO; |
734 | goto done; |
735 | } |
736 | |
737 | DPRINTFN(3, ("ulptread: TIMEOUT n %d nread %d error %d\n" , |
738 | n, nread, error)); |
739 | /* |
740 | * Don't set error until we understand why |
741 | * this happens. |
742 | */ |
743 | break; |
744 | |
745 | case USBD_INTERRUPTED: |
746 | /* |
747 | * The tsleep in usbd_bulk_transfer was |
748 | * interrupted. Reflect it to the caller so |
749 | * that reading can be interrupted. |
750 | */ |
751 | error = EINTR; |
752 | DPRINTFN(3, ("ulptread: EINTR error %d\n" , error)); |
753 | goto done; |
754 | break; |
755 | |
756 | default: |
757 | /* Assume all other return codes are really errors. */ |
758 | error = EIO; |
759 | DPRINTFN(3, ("ulptread: n %d err %d error %d\n" , |
760 | n, err, error)); |
761 | goto done; |
762 | break; |
763 | } |
764 | /* XXX KASSERT */ |
765 | if (error != 0) { |
766 | printf("ulptread: post-switch error %d != 0" , error); |
767 | goto done; |
768 | } |
769 | |
770 | if (n > 0) { |
771 | /* |
772 | * Record progress to enable later choosing |
773 | * between short reads and EWOULDBLOCK. |
774 | */ |
775 | nread += n; |
776 | |
777 | /* Copy to userspace, giving up on any error. */ |
778 | error = uiomove(bufp, n, uio); |
779 | if (error != 0) |
780 | break; |
781 | } else { |
782 | /* |
783 | * We read 0 bytes, and therefore are done, |
784 | * even if we aren't in nonblocking mode. |
785 | */ |
786 | if (error == 0 && nread == 0) |
787 | error = EWOULDBLOCK; |
788 | DPRINTFN(3, ("ulptread: read 0=>done error %d\n" , |
789 | error)); |
790 | goto done; |
791 | } |
792 | |
793 | /* |
794 | * A short transfer indicates no more data will be |
795 | * forthcoming. Terminate this read regardless of |
796 | * whether we are in nonblocking mode. XXX Reconsider |
797 | * for blocking mode; maybe we should continue to |
798 | * block, but maybe it just doesn't make senes to do |
799 | * blocking reads from devices like this. |
800 | */ |
801 | if (err == USBD_SHORT_XFER) { |
802 | DPRINTFN(3, ("ulptread: SHORT=>done n %d nread %d err %d error %d\n" , |
803 | n, nread, err, error)); |
804 | break; |
805 | } |
806 | } |
807 | |
808 | done: |
809 | DPRINTFN(3, ("ulptread: finished n %d nread %d err %d error %d\n" , |
810 | n, nread, err, error)); |
811 | return error; |
812 | } |
813 | |
814 | int |
815 | ulptread(dev_t dev, struct uio *uio, int flags) |
816 | { |
817 | struct ulpt_softc *sc; |
818 | int error; |
819 | |
820 | sc = device_lookup_private(&ulpt_cd, ULPTUNIT(dev)); |
821 | |
822 | if (sc->sc_dying) |
823 | return EIO; |
824 | |
825 | sc->sc_refcnt++; |
826 | error = ulpt_do_read(sc, uio, flags); |
827 | if (--sc->sc_refcnt < 0) |
828 | usb_detach_wakeupold(sc->sc_dev); |
829 | return error; |
830 | } |
831 | |
832 | void |
833 | ulpt_read_cb(struct usbd_xfer *xfer, void *priv, |
834 | usbd_status status) |
835 | { |
836 | usbd_status err; |
837 | uint32_t n; |
838 | void *xsc; |
839 | struct ulpt_softc *sc; |
840 | |
841 | usbd_get_xfer_status(xfer, &xsc, NULL, &n, &err); |
842 | sc = xsc; |
843 | |
844 | DPRINTFN(4, ("ulpt_read_cb: start sc=%p, err=%d n=%d\n" , sc, err, n)); |
845 | |
846 | #ifdef ULPT_DEBUG |
847 | if (!err && n > 0) |
848 | DPRINTFN(3, ("ulpt_tick: discarding %d bytes\n" , n)); |
849 | #endif |
850 | if (!err || err == USBD_TIMEOUT) |
851 | callout_reset(&sc->sc_read_callout, hz / ULPT_READS_PER_SEC, |
852 | ulpt_tick, sc); |
853 | } |
854 | |
855 | /* |
856 | * For devices which are not opened for reading, this function is |
857 | * called continuously to start read bulk transfers to avoid the |
858 | * printer overflowing its output buffer. |
859 | * |
860 | * XXX This should be adapted for continuous reads to allow select to |
861 | * work; see do_ulpt_read(). |
862 | */ |
863 | void |
864 | ulpt_tick(void *xsc) |
865 | { |
866 | struct ulpt_softc *sc = xsc; |
867 | usbd_status err __unused; |
868 | |
869 | if (sc == NULL || sc->sc_dying) |
870 | return; |
871 | |
872 | usbd_setup_xfer(sc->sc_in_xfer, sc, sc->sc_in_buf, ULPT_BSIZE, |
873 | USBD_SHORT_XFER_OK, ULPT_READ_TIMO, ulpt_read_cb); |
874 | err = usbd_transfer(sc->sc_in_xfer); |
875 | DPRINTFN(3, ("ulpt_tick: sc=%p err=%d\n" , sc, err)); |
876 | } |
877 | |
878 | int |
879 | ulptioctl(dev_t dev, u_long cmd, void *data, |
880 | int flag, struct lwp *l) |
881 | { |
882 | #if 0 |
883 | struct ulpt_softc *sc; |
884 | |
885 | sc = device_lookup_private(&ulpt_cd, ULPTUNIT(dev)); |
886 | #endif |
887 | |
888 | switch (cmd) { |
889 | case FIONBIO: |
890 | return 0; |
891 | } |
892 | |
893 | return ENODEV; |
894 | } |
895 | |
896 | #if 0 |
897 | /* XXX This does not belong here. */ |
898 | /* |
899 | * Print select parts of a IEEE 1284 device ID. |
900 | */ |
901 | void |
902 | ieee1284_print_id(char *str) |
903 | { |
904 | char *p, *q; |
905 | |
906 | for (p = str-1; p; p = strchr(p, ';')) { |
907 | p++; /* skip ';' */ |
908 | if (strncmp(p, "MFG:" , 4) == 0 || |
909 | strncmp(p, "MANUFACTURER:" , 14) == 0 || |
910 | strncmp(p, "MDL:" , 4) == 0 || |
911 | strncmp(p, "MODEL:" , 6) == 0) { |
912 | q = strchr(p, ';'); |
913 | if (q) |
914 | printf("%.*s" , (int)(q - p + 1), p); |
915 | } |
916 | } |
917 | } |
918 | #endif |
919 | |