1 /* $NetBSD: ulpt.c,v 1.60 2003/10/04 21:19:50 augustss Exp $ */
4 * Copyright (c) 1998, 2003 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Lennart Augustsson (lennart@augustsson.net) at
9 * Carlstedt Research & Technology.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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.
34 * Printer Class spec: http://www.usb.org/developers/data/devclass/usbprint109.PDF
35 * Printer Class spec: http://www.usb.org/developers/devclass_docs/usbprint11.pdf
38 #include <sys/stdint.h>
39 #include <sys/param.h>
40 #include <sys/queue.h>
41 #include <sys/types.h>
42 #include <sys/systm.h>
43 #include <sys/kernel.h>
45 #include <sys/module.h>
47 #include <sys/condvar.h>
48 #include <sys/sysctl.h>
49 #include <sys/unistd.h>
50 #include <sys/callout.h>
51 #include <sys/malloc.h>
53 #include <sys/syslog.h>
55 #include <sys/fcntl.h>
57 #include <bus/u4b/usb.h>
58 #include <bus/u4b/usbdi.h>
59 #include <bus/u4b/usbdi_util.h>
60 #include <bus/u4b/usbhid.h>
61 #include <bus/u4b/usbdevs.h>
63 #define USB_DEBUG_VAR ulpt_debug
64 #include <bus/u4b/usb_debug.h>
65 #include <bus/u4b/usb_process.h>
68 static int ulpt_debug = 0;
70 static SYSCTL_NODE(_hw_usb, OID_AUTO, ulpt, CTLFLAG_RW, 0, "USB ulpt");
71 SYSCTL_INT(_hw_usb_ulpt, OID_AUTO, debug, CTLFLAG_RW,
72 &ulpt_debug, 0, "Debug level");
75 #define ULPT_BSIZE (1<<15) /* bytes */
76 #define ULPT_IFQ_MAXLEN 2 /* units */
78 #define UR_GET_DEVICE_ID 0x00
79 #define UR_GET_PORT_STATUS 0x01
80 #define UR_SOFT_RESET 0x02
82 #define LPS_NERR 0x08 /* printer no error */
83 #define LPS_SELECT 0x10 /* printer selected */
84 #define LPS_NOPAPER 0x20 /* printer out of paper */
85 #define LPS_INVERT (LPS_SELECT|LPS_NERR)
86 #define LPS_MASK (LPS_SELECT|LPS_NERR|LPS_NOPAPER)
96 struct usb_fifo_sc sc_fifo;
97 struct usb_fifo_sc sc_fifo_noreset;
99 struct usb_callout sc_watchdog;
102 struct usb_device *sc_udev;
103 struct usb_fifo *sc_fifo_open[2];
104 struct usb_xfer *sc_xfer[ULPT_N_TRANSFER];
106 int sc_fflags; /* current open flags, FREAD and
109 uint8_t sc_last_status;
110 uint8_t sc_zlps; /* number of consequtive zero length
111 * packets received */
116 static device_probe_t ulpt_probe;
117 static device_attach_t ulpt_attach;
118 static device_detach_t ulpt_detach;
120 static usb_callback_t ulpt_write_callback;
121 static usb_callback_t ulpt_read_callback;
122 static usb_callback_t ulpt_status_callback;
124 static void ulpt_reset(struct ulpt_softc *);
125 static void ulpt_watchdog(void *);
127 static usb_fifo_close_t ulpt_close;
128 static usb_fifo_cmd_t ulpt_start_read;
129 static usb_fifo_cmd_t ulpt_start_write;
130 static usb_fifo_cmd_t ulpt_stop_read;
131 static usb_fifo_cmd_t ulpt_stop_write;
132 static usb_fifo_ioctl_t ulpt_ioctl;
133 static usb_fifo_open_t ulpt_open;
134 static usb_fifo_open_t unlpt_open;
136 static struct usb_fifo_methods ulpt_fifo_methods = {
137 .f_close = &ulpt_close,
138 .f_ioctl = &ulpt_ioctl,
139 .f_open = &ulpt_open,
140 .f_start_read = &ulpt_start_read,
141 .f_start_write = &ulpt_start_write,
142 .f_stop_read = &ulpt_stop_read,
143 .f_stop_write = &ulpt_stop_write,
144 .basename[0] = "ulpt",
147 static struct usb_fifo_methods unlpt_fifo_methods = {
148 .f_close = &ulpt_close,
149 .f_ioctl = &ulpt_ioctl,
150 .f_open = &unlpt_open,
151 .f_start_read = &ulpt_start_read,
152 .f_start_write = &ulpt_start_write,
153 .f_stop_read = &ulpt_stop_read,
154 .f_stop_write = &ulpt_stop_write,
155 .basename[0] = "unlpt",
159 ulpt_reset(struct ulpt_softc *sc)
161 struct usb_device_request req;
165 req.bRequest = UR_SOFT_RESET;
166 USETW(req.wValue, 0);
167 USETW(req.wIndex, sc->sc_iface_no);
168 USETW(req.wLength, 0);
171 * There was a mistake in the USB printer 1.0 spec that gave the
172 * request type as UT_WRITE_CLASS_OTHER; it should have been
173 * UT_WRITE_CLASS_INTERFACE. Many printers use the old one,
177 lockmgr(&sc->sc_lock, LK_EXCLUSIVE);
178 req.bmRequestType = UT_WRITE_CLASS_OTHER;
179 if (usbd_do_request_flags(sc->sc_udev, &sc->sc_lock,
180 &req, NULL, 0, NULL, 2 * USB_MS_HZ)) { /* 1.0 */
181 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
182 if (usbd_do_request_flags(sc->sc_udev, &sc->sc_lock,
183 &req, NULL, 0, NULL, 2 * USB_MS_HZ)) { /* 1.1 */
187 lockmgr(&sc->sc_lock, LK_RELEASE);
191 ulpt_write_callback(struct usb_xfer *xfer, usb_error_t error)
193 struct ulpt_softc *sc = usbd_xfer_softc(xfer);
194 struct usb_fifo *f = sc->sc_fifo_open[USB_FIFO_TX];
195 struct usb_page_cache *pc;
198 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
201 /* should not happen */
202 DPRINTF("no FIFO\n");
205 DPRINTF("state=0x%x actlen=%d\n", USB_GET_STATE(xfer), actlen);
207 switch (USB_GET_STATE(xfer)) {
208 case USB_ST_TRANSFERRED:
211 pc = usbd_xfer_get_frame(xfer, 0);
212 max = usbd_xfer_max_len(xfer);
213 if (usb_fifo_get_data(f, pc, 0, max, &actlen, 0)) {
214 usbd_xfer_set_frame_len(xfer, 0, actlen);
215 usbd_transfer_submit(xfer);
220 if (error != USB_ERR_CANCELLED) {
221 /* try to clear stall first */
222 usbd_xfer_set_stall(xfer);
230 ulpt_read_callback(struct usb_xfer *xfer, usb_error_t error)
232 struct ulpt_softc *sc = usbd_xfer_softc(xfer);
233 struct usb_fifo *f = sc->sc_fifo_open[USB_FIFO_RX];
234 struct usb_page_cache *pc;
237 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
240 /* should not happen */
241 DPRINTF("no FIFO\n");
244 DPRINTF("state=0x%x\n", USB_GET_STATE(xfer));
246 switch (USB_GET_STATE(xfer)) {
247 case USB_ST_TRANSFERRED:
251 if (sc->sc_zlps == 4) {
252 /* enable BULK throttle */
253 usbd_xfer_set_interval(xfer, 500); /* ms */
258 /* disable BULK throttle */
260 usbd_xfer_set_interval(xfer, 0);
264 pc = usbd_xfer_get_frame(xfer, 0);
265 usb_fifo_put_data(f, pc, 0, actlen, 1);
269 if (usb_fifo_put_bytes_max(f) != 0) {
270 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
271 usbd_transfer_submit(xfer);
276 /* disable BULK throttle */
277 usbd_xfer_set_interval(xfer, 0);
280 if (error != USB_ERR_CANCELLED) {
281 /* try to clear stall first */
282 usbd_xfer_set_stall(xfer);
290 ulpt_status_callback(struct usb_xfer *xfer, usb_error_t error)
292 struct ulpt_softc *sc = usbd_xfer_softc(xfer);
293 struct usb_device_request req;
294 struct usb_page_cache *pc;
298 switch (USB_GET_STATE(xfer)) {
299 case USB_ST_TRANSFERRED:
301 pc = usbd_xfer_get_frame(xfer, 1);
302 usbd_copy_out(pc, 0, &cur_status, 1);
304 cur_status = (cur_status ^ LPS_INVERT) & LPS_MASK;
305 new_status = cur_status & ~sc->sc_last_status;
306 sc->sc_last_status = cur_status;
308 if (new_status & LPS_SELECT)
309 log(LOG_NOTICE, "%s: offline\n",
310 device_get_nameunit(sc->sc_dev));
311 else if (new_status & LPS_NOPAPER)
312 log(LOG_NOTICE, "%s: out of paper\n",
313 device_get_nameunit(sc->sc_dev));
314 else if (new_status & LPS_NERR)
315 log(LOG_NOTICE, "%s: output error\n",
316 device_get_nameunit(sc->sc_dev));
320 req.bmRequestType = UT_READ_CLASS_INTERFACE;
321 req.bRequest = UR_GET_PORT_STATUS;
322 USETW(req.wValue, 0);
323 req.wIndex[0] = sc->sc_iface_no;
325 USETW(req.wLength, 1);
327 pc = usbd_xfer_get_frame(xfer, 0);
328 usbd_copy_in(pc, 0, &req, sizeof(req));
330 usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
331 usbd_xfer_set_frame_len(xfer, 1, 1);
332 usbd_xfer_set_frames(xfer, 2);
333 usbd_transfer_submit(xfer);
337 DPRINTF("error=%s\n", usbd_errstr(error));
338 if (error != USB_ERR_CANCELLED) {
339 /* wait for next watchdog timeout */
345 static const struct usb_config ulpt_config[ULPT_N_TRANSFER] = {
346 [ULPT_BULK_DT_WR] = {
348 .endpoint = UE_ADDR_ANY,
349 .direction = UE_DIR_OUT,
350 .bufsize = ULPT_BSIZE,
351 .flags = {.pipe_bof = 1,.proxy_buffer = 1},
352 .callback = &ulpt_write_callback,
355 [ULPT_BULK_DT_RD] = {
357 .endpoint = UE_ADDR_ANY,
358 .direction = UE_DIR_IN,
359 .bufsize = ULPT_BSIZE,
360 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,.proxy_buffer = 1},
361 .callback = &ulpt_read_callback,
364 [ULPT_INTR_DT_RD] = {
366 .endpoint = 0x00, /* Control pipe */
367 .direction = UE_DIR_ANY,
368 .bufsize = sizeof(struct usb_device_request) + 1,
369 .callback = &ulpt_status_callback,
370 .timeout = 1000, /* 1 second */
375 ulpt_start_read(struct usb_fifo *fifo)
377 struct ulpt_softc *sc = usb_fifo_softc(fifo);
379 usbd_transfer_start(sc->sc_xfer[ULPT_BULK_DT_RD]);
383 ulpt_stop_read(struct usb_fifo *fifo)
385 struct ulpt_softc *sc = usb_fifo_softc(fifo);
387 usbd_transfer_stop(sc->sc_xfer[ULPT_BULK_DT_RD]);
391 ulpt_start_write(struct usb_fifo *fifo)
393 struct ulpt_softc *sc = usb_fifo_softc(fifo);
395 usbd_transfer_start(sc->sc_xfer[ULPT_BULK_DT_WR]);
399 ulpt_stop_write(struct usb_fifo *fifo)
401 struct ulpt_softc *sc = usb_fifo_softc(fifo);
403 usbd_transfer_stop(sc->sc_xfer[ULPT_BULK_DT_WR]);
407 ulpt_open(struct usb_fifo *fifo, int fflags)
409 struct ulpt_softc *sc = usb_fifo_softc(fifo);
411 /* we assume that open is a serial process */
413 if (sc->sc_fflags == 0) {
415 /* reset USB paralell port */
419 return (unlpt_open(fifo, fflags));
423 unlpt_open(struct usb_fifo *fifo, int fflags)
425 struct ulpt_softc *sc = usb_fifo_softc(fifo);
427 if (sc->sc_fflags & fflags) {
430 if (fflags & FREAD) {
431 /* clear stall first */
432 lockmgr(&sc->sc_lock, LK_EXCLUSIVE);
433 usbd_xfer_set_stall(sc->sc_xfer[ULPT_BULK_DT_RD]);
434 lockmgr(&sc->sc_lock, LK_RELEASE);
435 if (usb_fifo_alloc_buffer(fifo,
436 usbd_xfer_max_len(sc->sc_xfer[ULPT_BULK_DT_RD]),
440 /* set which FIFO is opened */
441 sc->sc_fifo_open[USB_FIFO_RX] = fifo;
443 if (fflags & FWRITE) {
444 /* clear stall first */
445 lockmgr(&sc->sc_lock, LK_EXCLUSIVE);
446 usbd_xfer_set_stall(sc->sc_xfer[ULPT_BULK_DT_WR]);
447 lockmgr(&sc->sc_lock, LK_RELEASE);
448 if (usb_fifo_alloc_buffer(fifo,
449 usbd_xfer_max_len(sc->sc_xfer[ULPT_BULK_DT_WR]),
453 /* set which FIFO is opened */
454 sc->sc_fifo_open[USB_FIFO_TX] = fifo;
456 sc->sc_fflags |= fflags & (FREAD | FWRITE);
461 ulpt_close(struct usb_fifo *fifo, int fflags)
463 struct ulpt_softc *sc = usb_fifo_softc(fifo);
465 sc->sc_fflags &= ~(fflags & (FREAD | FWRITE));
467 if (fflags & (FREAD | FWRITE)) {
468 usb_fifo_free_buffer(fifo);
473 ulpt_ioctl(struct usb_fifo *fifo, u_long cmd, void *data,
479 static const STRUCT_USB_HOST_ID ulpt_devs[] = {
480 /* Uni-directional USB printer */
481 {USB_IFACE_CLASS(UICLASS_PRINTER),
482 USB_IFACE_SUBCLASS(UISUBCLASS_PRINTER),
483 USB_IFACE_PROTOCOL(UIPROTO_PRINTER_UNI)},
485 /* Bi-directional USB printer */
486 {USB_IFACE_CLASS(UICLASS_PRINTER),
487 USB_IFACE_SUBCLASS(UISUBCLASS_PRINTER),
488 USB_IFACE_PROTOCOL(UIPROTO_PRINTER_BI)},
490 /* 1284 USB printer */
491 {USB_IFACE_CLASS(UICLASS_PRINTER),
492 USB_IFACE_SUBCLASS(UISUBCLASS_PRINTER),
493 USB_IFACE_PROTOCOL(UIPROTO_PRINTER_1284)},
497 ulpt_probe(device_t dev)
499 struct usb_attach_arg *uaa = device_get_ivars(dev);
504 if (uaa->usb_mode != USB_MODE_HOST)
507 error = usbd_lookup_id_by_uaa(ulpt_devs, sizeof(ulpt_devs), uaa);
511 return (BUS_PROBE_GENERIC);
515 ulpt_attach(device_t dev)
517 struct usb_attach_arg *uaa = device_get_ivars(dev);
518 struct ulpt_softc *sc = device_get_softc(dev);
519 struct usb_interface_descriptor *id;
520 int unit = device_get_unit(dev);
522 uint8_t iface_index = uaa->info.bIfaceIndex;
525 DPRINTFN(11, "sc=%p\n", sc);
528 sc->sc_udev = uaa->device;
530 device_set_usb_desc(dev);
532 lockinit(&sc->sc_lock, "ulpt lock", 0, LK_CANRECURSE);
534 usb_callout_init_mtx(&sc->sc_watchdog, &sc->sc_lock, 0);
536 /* search through all the descriptors looking for bidir mode */
538 id = usbd_get_interface_descriptor(uaa->iface);
544 if ((id->bDescriptorType == UDESC_INTERFACE) &&
545 (id->bLength >= sizeof(*id))) {
546 if (id->bInterfaceNumber != uaa->info.bIfaceNum) {
550 if ((id->bInterfaceClass == UICLASS_PRINTER) &&
551 (id->bInterfaceSubClass == UISUBCLASS_PRINTER) &&
552 (id->bInterfaceProtocol == UIPROTO_PRINTER_BI)) {
557 id = (void *)usb_desc_foreach(
558 usbd_get_config_descriptor(uaa->device), (void *)id);
564 DPRINTF("setting alternate "
565 "config number: %d\n", alt_index);
569 error = usbd_set_alt_interface_index
570 (uaa->device, iface_index, alt_index);
573 DPRINTF("could not set alternate "
574 "config, error=%s\n", usbd_errstr(error));
578 sc->sc_iface_no = id->bInterfaceNumber;
580 error = usbd_transfer_setup(uaa->device, &iface_index,
581 sc->sc_xfer, ulpt_config, ULPT_N_TRANSFER,
584 DPRINTF("error=%s\n", usbd_errstr(error));
587 device_printf(sc->sc_dev, "using bi-directional mode\n");
591 * This code is disabled because for some mysterious reason it causes
592 * printing not to work. But only sometimes, and mostly with
593 * UHCI and less often with OHCI. *sigh*
596 struct usb_config_descriptor *cd = usbd_get_config_descriptor(dev);
597 struct usb_device_request req;
600 req.bmRequestType = UT_READ_CLASS_INTERFACE;
601 req.bRequest = UR_GET_DEVICE_ID;
602 USETW(req.wValue, cd->bConfigurationValue);
603 USETW2(req.wIndex, id->bInterfaceNumber, id->bAlternateSetting);
604 USETW(req.wLength, sizeof devinfo - 1);
605 error = usbd_do_request_flags(dev, &req, devinfo, USB_SHORT_XFER_OK,
606 &alen, USB_DEFAULT_TIMEOUT);
608 device_printf(sc->sc_dev, "cannot get device id\n");
609 } else if (alen <= 2) {
610 device_printf(sc->sc_dev, "empty device id, no "
611 "printer connected?\n");
613 /* devinfo now contains an IEEE-1284 device ID */
614 len = ((devinfo[0] & 0xff) << 8) | (devinfo[1] & 0xff);
615 if (len > sizeof devinfo - 3)
616 len = sizeof devinfo - 3;
618 printf("%s: device id <", device_get_nameunit(sc->sc_dev));
619 ieee1284_print_id(devinfo + 2);
625 error = usb_fifo_attach(uaa->device, sc, &sc->sc_lock,
626 &ulpt_fifo_methods, &sc->sc_fifo,
627 unit, 0 - 1, uaa->info.bIfaceIndex,
628 UID_ROOT, GID_OPERATOR, 0644);
632 error = usb_fifo_attach(uaa->device, sc, &sc->sc_lock,
633 &unlpt_fifo_methods, &sc->sc_fifo_noreset,
634 unit, 0 - 1, uaa->info.bIfaceIndex,
635 UID_ROOT, GID_OPERATOR, 0644);
639 /* start reading of status */
641 lockmgr(&sc->sc_lock, LK_EXCLUSIVE);
643 lockmgr(&sc->sc_lock, LK_RELEASE);
652 ulpt_detach(device_t dev)
654 struct ulpt_softc *sc = device_get_softc(dev);
656 DPRINTF("sc=%p\n", sc);
658 usb_fifo_detach(&sc->sc_fifo);
659 usb_fifo_detach(&sc->sc_fifo_noreset);
661 lockmgr(&sc->sc_lock, LK_EXCLUSIVE);
662 usb_callout_stop(&sc->sc_watchdog);
663 lockmgr(&sc->sc_lock, LK_RELEASE);
665 usbd_transfer_unsetup(sc->sc_xfer, ULPT_N_TRANSFER);
666 usb_callout_drain(&sc->sc_watchdog);
667 lockuninit(&sc->sc_lock);
673 /* XXX This does not belong here. */
676 * Compare two strings until the second ends.
680 ieee1284_compare(const char *a, const char *b)
697 * Print select parts of an IEEE 1284 device ID.
700 ieee1284_print_id(char *str)
704 for (p = str - 1; p; p = strchr(p, ';')) {
706 if (ieee1284_compare(p, "MFG:") == 0 ||
707 ieee1284_compare(p, "MANUFACTURER:") == 0 ||
708 ieee1284_compare(p, "MDL:") == 0 ||
709 ieee1284_compare(p, "MODEL:") == 0) {
712 printf("%.*s", (int)(q - p + 1), p);
720 ulpt_watchdog(void *arg)
722 struct ulpt_softc *sc = arg;
724 KKASSERT(lockowned(&sc->sc_lock));
727 * Only read status while the device is not opened, due to
728 * possible hardware or firmware bug in some printers.
730 if (sc->sc_fflags == 0)
731 usbd_transfer_start(sc->sc_xfer[ULPT_INTR_DT_RD]);
733 usb_callout_reset(&sc->sc_watchdog,
734 hz, &ulpt_watchdog, sc);
737 static devclass_t ulpt_devclass;
739 static device_method_t ulpt_methods[] = {
740 DEVMETHOD(device_probe, ulpt_probe),
741 DEVMETHOD(device_attach, ulpt_attach),
742 DEVMETHOD(device_detach, ulpt_detach),
746 static driver_t ulpt_driver = {
748 .methods = ulpt_methods,
749 .size = sizeof(struct ulpt_softc),
752 DRIVER_MODULE(ulpt, uhub, ulpt_driver, ulpt_devclass, NULL, 0);
753 MODULE_DEPEND(ulpt, usb, 1, 1, 1);
754 MODULE_VERSION(ulpt, 1);