4 /* $NetBSD: ulpt.c,v 1.60 2003/10/04 21:19:50 augustss Exp $ */
7 * Copyright (c) 1998, 2003 The NetBSD Foundation, Inc.
10 * This code is derived from software contributed to The NetBSD Foundation
11 * by Lennart Augustsson (lennart@augustsson.net) at
12 * Carlstedt Research & Technology.
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
23 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
24 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
25 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
27 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGE.
37 * Printer Class spec: http://www.usb.org/developers/data/devclass/usbprint109.PDF
38 * Printer Class spec: http://www.usb.org/developers/devclass_docs/usbprint11.pdf
41 #include <sys/stdint.h>
42 #include <sys/stddef.h>
43 #include <sys/param.h>
44 #include <sys/queue.h>
45 #include <sys/types.h>
46 #include <sys/systm.h>
47 #include <sys/kernel.h>
49 #include <sys/module.h>
51 #include <sys/mutex.h>
52 #include <sys/condvar.h>
53 #include <sys/sysctl.h>
55 #include <sys/unistd.h>
56 #include <sys/callout.h>
57 #include <sys/malloc.h>
59 #include <sys/syslog.h>
60 #include <sys/selinfo.h>
62 #include <sys/fcntl.h>
64 #include <dev/usb/usb.h>
65 #include <dev/usb/usbdi.h>
66 #include <dev/usb/usbdi_util.h>
67 #include <dev/usb/usbhid.h>
70 #define USB_DEBUG_VAR ulpt_debug
71 #include <dev/usb/usb_debug.h>
72 #include <dev/usb/usb_process.h>
75 static int ulpt_debug = 0;
77 static SYSCTL_NODE(_hw_usb, OID_AUTO, ulpt, CTLFLAG_RW, 0, "USB ulpt");
78 SYSCTL_INT(_hw_usb_ulpt, OID_AUTO, debug, CTLFLAG_RW,
79 &ulpt_debug, 0, "Debug level");
82 #define ULPT_BSIZE (1<<15) /* bytes */
83 #define ULPT_IFQ_MAXLEN 2 /* units */
85 #define UR_GET_DEVICE_ID 0x00
86 #define UR_GET_PORT_STATUS 0x01
87 #define UR_SOFT_RESET 0x02
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)
103 struct usb_fifo_sc sc_fifo;
104 struct usb_fifo_sc sc_fifo_noreset;
106 struct usb_callout sc_watchdog;
109 struct usb_device *sc_udev;
110 struct usb_fifo *sc_fifo_open[2];
111 struct usb_xfer *sc_xfer[ULPT_N_TRANSFER];
113 int sc_fflags; /* current open flags, FREAD and
116 uint8_t sc_last_status;
117 uint8_t sc_zlps; /* number of consequtive zero length
118 * packets received */
123 static device_probe_t ulpt_probe;
124 static device_attach_t ulpt_attach;
125 static device_detach_t ulpt_detach;
127 static usb_callback_t ulpt_write_callback;
128 static usb_callback_t ulpt_read_callback;
129 static usb_callback_t ulpt_status_callback;
131 static void ulpt_reset(struct ulpt_softc *);
132 static void ulpt_watchdog(void *);
134 static usb_fifo_close_t ulpt_close;
135 static usb_fifo_cmd_t ulpt_start_read;
136 static usb_fifo_cmd_t ulpt_start_write;
137 static usb_fifo_cmd_t ulpt_stop_read;
138 static usb_fifo_cmd_t ulpt_stop_write;
139 static usb_fifo_ioctl_t ulpt_ioctl;
140 static usb_fifo_open_t ulpt_open;
141 static usb_fifo_open_t unlpt_open;
143 static struct usb_fifo_methods ulpt_fifo_methods = {
144 .f_close = &ulpt_close,
145 .f_ioctl = &ulpt_ioctl,
146 .f_open = &ulpt_open,
147 .f_start_read = &ulpt_start_read,
148 .f_start_write = &ulpt_start_write,
149 .f_stop_read = &ulpt_stop_read,
150 .f_stop_write = &ulpt_stop_write,
151 .basename[0] = "ulpt",
154 static struct usb_fifo_methods unlpt_fifo_methods = {
155 .f_close = &ulpt_close,
156 .f_ioctl = &ulpt_ioctl,
157 .f_open = &unlpt_open,
158 .f_start_read = &ulpt_start_read,
159 .f_start_write = &ulpt_start_write,
160 .f_stop_read = &ulpt_stop_read,
161 .f_stop_write = &ulpt_stop_write,
162 .basename[0] = "unlpt",
166 ulpt_reset(struct ulpt_softc *sc)
168 struct usb_device_request req;
172 req.bRequest = UR_SOFT_RESET;
173 USETW(req.wValue, 0);
174 USETW(req.wIndex, sc->sc_iface_no);
175 USETW(req.wLength, 0);
178 * There was a mistake in the USB printer 1.0 spec that gave the
179 * request type as UT_WRITE_CLASS_OTHER; it should have been
180 * UT_WRITE_CLASS_INTERFACE. Many printers use the old one,
184 mtx_lock(&sc->sc_mtx);
185 req.bmRequestType = UT_WRITE_CLASS_OTHER;
186 if (usbd_do_request_flags(sc->sc_udev, &sc->sc_mtx,
187 &req, NULL, 0, NULL, 2 * USB_MS_HZ)) { /* 1.0 */
188 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
189 if (usbd_do_request_flags(sc->sc_udev, &sc->sc_mtx,
190 &req, NULL, 0, NULL, 2 * USB_MS_HZ)) { /* 1.1 */
194 mtx_unlock(&sc->sc_mtx);
198 ulpt_write_callback(struct usb_xfer *xfer, usb_error_t error)
200 struct ulpt_softc *sc = usbd_xfer_softc(xfer);
201 struct usb_fifo *f = sc->sc_fifo_open[USB_FIFO_TX];
202 struct usb_page_cache *pc;
205 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
208 /* should not happen */
209 DPRINTF("no FIFO\n");
212 DPRINTF("state=0x%x actlen=%d\n", USB_GET_STATE(xfer), actlen);
214 switch (USB_GET_STATE(xfer)) {
215 case USB_ST_TRANSFERRED:
218 pc = usbd_xfer_get_frame(xfer, 0);
219 max = usbd_xfer_max_len(xfer);
220 if (usb_fifo_get_data(f, pc, 0, max, &actlen, 0)) {
221 usbd_xfer_set_frame_len(xfer, 0, actlen);
222 usbd_transfer_submit(xfer);
227 if (error != USB_ERR_CANCELLED) {
228 /* try to clear stall first */
229 usbd_xfer_set_stall(xfer);
237 ulpt_read_callback(struct usb_xfer *xfer, usb_error_t error)
239 struct ulpt_softc *sc = usbd_xfer_softc(xfer);
240 struct usb_fifo *f = sc->sc_fifo_open[USB_FIFO_RX];
241 struct usb_page_cache *pc;
244 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
247 /* should not happen */
248 DPRINTF("no FIFO\n");
251 DPRINTF("state=0x%x\n", USB_GET_STATE(xfer));
253 switch (USB_GET_STATE(xfer)) {
254 case USB_ST_TRANSFERRED:
258 if (sc->sc_zlps == 4) {
259 /* enable BULK throttle */
260 usbd_xfer_set_interval(xfer, 500); /* ms */
265 /* disable BULK throttle */
267 usbd_xfer_set_interval(xfer, 0);
271 pc = usbd_xfer_get_frame(xfer, 0);
272 usb_fifo_put_data(f, pc, 0, actlen, 1);
276 if (usb_fifo_put_bytes_max(f) != 0) {
277 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
278 usbd_transfer_submit(xfer);
283 /* disable BULK throttle */
284 usbd_xfer_set_interval(xfer, 0);
287 if (error != USB_ERR_CANCELLED) {
288 /* try to clear stall first */
289 usbd_xfer_set_stall(xfer);
297 ulpt_status_callback(struct usb_xfer *xfer, usb_error_t error)
299 struct ulpt_softc *sc = usbd_xfer_softc(xfer);
300 struct usb_device_request req;
301 struct usb_page_cache *pc;
305 switch (USB_GET_STATE(xfer)) {
306 case USB_ST_TRANSFERRED:
308 pc = usbd_xfer_get_frame(xfer, 1);
309 usbd_copy_out(pc, 0, &cur_status, 1);
311 cur_status = (cur_status ^ LPS_INVERT) & LPS_MASK;
312 new_status = cur_status & ~sc->sc_last_status;
313 sc->sc_last_status = cur_status;
315 if (new_status & LPS_SELECT)
316 log(LOG_NOTICE, "%s: offline\n",
317 device_get_nameunit(sc->sc_dev));
318 else if (new_status & LPS_NOPAPER)
319 log(LOG_NOTICE, "%s: out of paper\n",
320 device_get_nameunit(sc->sc_dev));
321 else if (new_status & LPS_NERR)
322 log(LOG_NOTICE, "%s: output error\n",
323 device_get_nameunit(sc->sc_dev));
327 req.bmRequestType = UT_READ_CLASS_INTERFACE;
328 req.bRequest = UR_GET_PORT_STATUS;
329 USETW(req.wValue, 0);
330 req.wIndex[0] = sc->sc_iface_no;
332 USETW(req.wLength, 1);
334 pc = usbd_xfer_get_frame(xfer, 0);
335 usbd_copy_in(pc, 0, &req, sizeof(req));
337 usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
338 usbd_xfer_set_frame_len(xfer, 1, 1);
339 usbd_xfer_set_frames(xfer, 2);
340 usbd_transfer_submit(xfer);
344 DPRINTF("error=%s\n", usbd_errstr(error));
345 if (error != USB_ERR_CANCELLED) {
346 /* wait for next watchdog timeout */
352 static const struct usb_config ulpt_config[ULPT_N_TRANSFER] = {
353 [ULPT_BULK_DT_WR] = {
355 .endpoint = UE_ADDR_ANY,
356 .direction = UE_DIR_OUT,
357 .bufsize = ULPT_BSIZE,
358 .flags = {.pipe_bof = 1,.proxy_buffer = 1},
359 .callback = &ulpt_write_callback,
362 [ULPT_BULK_DT_RD] = {
364 .endpoint = UE_ADDR_ANY,
365 .direction = UE_DIR_IN,
366 .bufsize = ULPT_BSIZE,
367 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,.proxy_buffer = 1},
368 .callback = &ulpt_read_callback,
371 [ULPT_INTR_DT_RD] = {
373 .endpoint = 0x00, /* Control pipe */
374 .direction = UE_DIR_ANY,
375 .bufsize = sizeof(struct usb_device_request) + 1,
376 .callback = &ulpt_status_callback,
377 .timeout = 1000, /* 1 second */
382 ulpt_start_read(struct usb_fifo *fifo)
384 struct ulpt_softc *sc = usb_fifo_softc(fifo);
386 usbd_transfer_start(sc->sc_xfer[ULPT_BULK_DT_RD]);
390 ulpt_stop_read(struct usb_fifo *fifo)
392 struct ulpt_softc *sc = usb_fifo_softc(fifo);
394 usbd_transfer_stop(sc->sc_xfer[ULPT_BULK_DT_RD]);
398 ulpt_start_write(struct usb_fifo *fifo)
400 struct ulpt_softc *sc = usb_fifo_softc(fifo);
402 usbd_transfer_start(sc->sc_xfer[ULPT_BULK_DT_WR]);
406 ulpt_stop_write(struct usb_fifo *fifo)
408 struct ulpt_softc *sc = usb_fifo_softc(fifo);
410 usbd_transfer_stop(sc->sc_xfer[ULPT_BULK_DT_WR]);
414 ulpt_open(struct usb_fifo *fifo, int fflags)
416 struct ulpt_softc *sc = usb_fifo_softc(fifo);
418 /* we assume that open is a serial process */
420 if (sc->sc_fflags == 0) {
422 /* reset USB paralell port */
426 return (unlpt_open(fifo, fflags));
430 unlpt_open(struct usb_fifo *fifo, int fflags)
432 struct ulpt_softc *sc = usb_fifo_softc(fifo);
434 if (sc->sc_fflags & fflags) {
437 if (fflags & FREAD) {
438 /* clear stall first */
439 mtx_lock(&sc->sc_mtx);
440 usbd_xfer_set_stall(sc->sc_xfer[ULPT_BULK_DT_RD]);
441 mtx_unlock(&sc->sc_mtx);
442 if (usb_fifo_alloc_buffer(fifo,
443 usbd_xfer_max_len(sc->sc_xfer[ULPT_BULK_DT_RD]),
447 /* set which FIFO is opened */
448 sc->sc_fifo_open[USB_FIFO_RX] = fifo;
450 if (fflags & FWRITE) {
451 /* clear stall first */
452 mtx_lock(&sc->sc_mtx);
453 usbd_xfer_set_stall(sc->sc_xfer[ULPT_BULK_DT_WR]);
454 mtx_unlock(&sc->sc_mtx);
455 if (usb_fifo_alloc_buffer(fifo,
456 usbd_xfer_max_len(sc->sc_xfer[ULPT_BULK_DT_WR]),
460 /* set which FIFO is opened */
461 sc->sc_fifo_open[USB_FIFO_TX] = fifo;
463 sc->sc_fflags |= fflags & (FREAD | FWRITE);
468 ulpt_close(struct usb_fifo *fifo, int fflags)
470 struct ulpt_softc *sc = usb_fifo_softc(fifo);
472 sc->sc_fflags &= ~(fflags & (FREAD | FWRITE));
474 if (fflags & (FREAD | FWRITE)) {
475 usb_fifo_free_buffer(fifo);
480 ulpt_ioctl(struct usb_fifo *fifo, u_long cmd, void *data,
486 static const STRUCT_USB_HOST_ID ulpt_devs[] = {
487 /* Uni-directional USB printer */
488 {USB_IFACE_CLASS(UICLASS_PRINTER),
489 USB_IFACE_SUBCLASS(UISUBCLASS_PRINTER),
490 USB_IFACE_PROTOCOL(UIPROTO_PRINTER_UNI)},
492 /* Bi-directional USB printer */
493 {USB_IFACE_CLASS(UICLASS_PRINTER),
494 USB_IFACE_SUBCLASS(UISUBCLASS_PRINTER),
495 USB_IFACE_PROTOCOL(UIPROTO_PRINTER_BI)},
497 /* 1284 USB printer */
498 {USB_IFACE_CLASS(UICLASS_PRINTER),
499 USB_IFACE_SUBCLASS(UISUBCLASS_PRINTER),
500 USB_IFACE_PROTOCOL(UIPROTO_PRINTER_1284)},
504 ulpt_probe(device_t dev)
506 struct usb_attach_arg *uaa = device_get_ivars(dev);
511 if (uaa->usb_mode != USB_MODE_HOST)
514 error = usbd_lookup_id_by_uaa(ulpt_devs, sizeof(ulpt_devs), uaa);
518 return (BUS_PROBE_GENERIC);
522 ulpt_attach(device_t dev)
524 struct usb_attach_arg *uaa = device_get_ivars(dev);
525 struct ulpt_softc *sc = device_get_softc(dev);
526 struct usb_interface_descriptor *id;
527 int unit = device_get_unit(dev);
529 uint8_t iface_index = uaa->info.bIfaceIndex;
532 DPRINTFN(11, "sc=%p\n", sc);
535 sc->sc_udev = uaa->device;
537 device_set_usb_desc(dev);
539 mtx_init(&sc->sc_mtx, "ulpt lock", NULL, MTX_DEF | MTX_RECURSE);
541 usb_callout_init_mtx(&sc->sc_watchdog, &sc->sc_mtx, 0);
543 /* search through all the descriptors looking for bidir mode */
545 id = usbd_get_interface_descriptor(uaa->iface);
551 if ((id->bDescriptorType == UDESC_INTERFACE) &&
552 (id->bLength >= sizeof(*id))) {
553 if (id->bInterfaceNumber != uaa->info.bIfaceNum) {
557 if ((id->bInterfaceClass == UICLASS_PRINTER) &&
558 (id->bInterfaceSubClass == UISUBCLASS_PRINTER) &&
559 (id->bInterfaceProtocol == UIPROTO_PRINTER_BI)) {
564 id = (void *)usb_desc_foreach(
565 usbd_get_config_descriptor(uaa->device), (void *)id);
571 DPRINTF("setting alternate "
572 "config number: %d\n", alt_index);
576 error = usbd_set_alt_interface_index
577 (uaa->device, iface_index, alt_index);
580 DPRINTF("could not set alternate "
581 "config, error=%s\n", usbd_errstr(error));
585 sc->sc_iface_no = id->bInterfaceNumber;
587 error = usbd_transfer_setup(uaa->device, &iface_index,
588 sc->sc_xfer, ulpt_config, ULPT_N_TRANSFER,
591 DPRINTF("error=%s\n", usbd_errstr(error));
594 device_printf(sc->sc_dev, "using bi-directional mode\n");
598 * This code is disabled because for some mysterious reason it causes
599 * printing not to work. But only sometimes, and mostly with
600 * UHCI and less often with OHCI. *sigh*
603 struct usb_config_descriptor *cd = usbd_get_config_descriptor(dev);
604 struct usb_device_request req;
607 req.bmRequestType = UT_READ_CLASS_INTERFACE;
608 req.bRequest = UR_GET_DEVICE_ID;
609 USETW(req.wValue, cd->bConfigurationValue);
610 USETW2(req.wIndex, id->bInterfaceNumber, id->bAlternateSetting);
611 USETW(req.wLength, sizeof devinfo - 1);
612 error = usbd_do_request_flags(dev, &req, devinfo, USB_SHORT_XFER_OK,
613 &alen, USB_DEFAULT_TIMEOUT);
615 device_printf(sc->sc_dev, "cannot get device id\n");
616 } else if (alen <= 2) {
617 device_printf(sc->sc_dev, "empty device id, no "
618 "printer connected?\n");
620 /* devinfo now contains an IEEE-1284 device ID */
621 len = ((devinfo[0] & 0xff) << 8) | (devinfo[1] & 0xff);
622 if (len > sizeof devinfo - 3)
623 len = sizeof devinfo - 3;
625 printf("%s: device id <", device_get_nameunit(sc->sc_dev));
626 ieee1284_print_id(devinfo + 2);
632 error = usb_fifo_attach(uaa->device, sc, &sc->sc_mtx,
633 &ulpt_fifo_methods, &sc->sc_fifo,
634 unit, 0 - 1, uaa->info.bIfaceIndex,
635 UID_ROOT, GID_OPERATOR, 0644);
639 error = usb_fifo_attach(uaa->device, sc, &sc->sc_mtx,
640 &unlpt_fifo_methods, &sc->sc_fifo_noreset,
641 unit, 0 - 1, uaa->info.bIfaceIndex,
642 UID_ROOT, GID_OPERATOR, 0644);
646 /* start reading of status */
648 mtx_lock(&sc->sc_mtx);
650 mtx_unlock(&sc->sc_mtx);
659 ulpt_detach(device_t dev)
661 struct ulpt_softc *sc = device_get_softc(dev);
663 DPRINTF("sc=%p\n", sc);
665 usb_fifo_detach(&sc->sc_fifo);
666 usb_fifo_detach(&sc->sc_fifo_noreset);
668 mtx_lock(&sc->sc_mtx);
669 usb_callout_stop(&sc->sc_watchdog);
670 mtx_unlock(&sc->sc_mtx);
672 usbd_transfer_unsetup(sc->sc_xfer, ULPT_N_TRANSFER);
673 usb_callout_drain(&sc->sc_watchdog);
674 mtx_destroy(&sc->sc_mtx);
680 /* XXX This does not belong here. */
683 * Compare two strings until the second ends.
687 ieee1284_compare(const char *a, const char *b)
704 * Print select parts of an IEEE 1284 device ID.
707 ieee1284_print_id(char *str)
711 for (p = str - 1; p; p = strchr(p, ';')) {
713 if (ieee1284_compare(p, "MFG:") == 0 ||
714 ieee1284_compare(p, "MANUFACTURER:") == 0 ||
715 ieee1284_compare(p, "MDL:") == 0 ||
716 ieee1284_compare(p, "MODEL:") == 0) {
719 printf("%.*s", (int)(q - p + 1), p);
727 ulpt_watchdog(void *arg)
729 struct ulpt_softc *sc = arg;
731 mtx_assert(&sc->sc_mtx, MA_OWNED);
734 * Only read status while the device is not opened, due to
735 * possible hardware or firmware bug in some printers.
737 if (sc->sc_fflags == 0)
738 usbd_transfer_start(sc->sc_xfer[ULPT_INTR_DT_RD]);
740 usb_callout_reset(&sc->sc_watchdog,
741 hz, &ulpt_watchdog, sc);
744 static devclass_t ulpt_devclass;
746 static device_method_t ulpt_methods[] = {
747 DEVMETHOD(device_probe, ulpt_probe),
748 DEVMETHOD(device_attach, ulpt_attach),
749 DEVMETHOD(device_detach, ulpt_detach),
753 static driver_t ulpt_driver = {
755 .methods = ulpt_methods,
756 .size = sizeof(struct ulpt_softc),
759 DRIVER_MODULE(ulpt, uhub, ulpt_driver, ulpt_devclass, NULL, 0);
760 MODULE_DEPEND(ulpt, usb, 1, 1, 1);
761 MODULE_VERSION(ulpt, 1);