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.
22 * 3. All advertising materials mentioning features or use of this software
23 * must display the following acknowledgement:
24 * This product includes software developed by the NetBSD
25 * Foundation, Inc. and its contributors.
26 * 4. Neither the name of The NetBSD Foundation nor the names of its
27 * contributors may be used to endorse or promote products derived
28 * from this software without specific prior written permission.
30 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
31 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
32 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
33 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
34 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
35 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
36 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
37 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
38 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
39 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
40 * POSSIBILITY OF SUCH DAMAGE.
44 * Printer Class spec: http://www.usb.org/developers/data/devclass/usbprint109.PDF
45 * Printer Class spec: http://www.usb.org/developers/devclass_docs/usbprint11.pdf
48 #include <sys/stdint.h>
49 #include <sys/stddef.h>
50 #include <sys/param.h>
51 #include <sys/queue.h>
52 #include <sys/types.h>
53 #include <sys/systm.h>
54 #include <sys/kernel.h>
56 #include <sys/linker_set.h>
57 #include <sys/module.h>
59 #include <sys/mutex.h>
60 #include <sys/condvar.h>
61 #include <sys/sysctl.h>
63 #include <sys/unistd.h>
64 #include <sys/callout.h>
65 #include <sys/malloc.h>
67 #include <sys/syslog.h>
68 #include <sys/selinfo.h>
70 #include <sys/fcntl.h>
72 #include <dev/usb/usb.h>
73 #include <dev/usb/usbdi.h>
74 #include <dev/usb/usbdi_util.h>
75 #include <dev/usb/usbhid.h>
78 #define USB_DEBUG_VAR ulpt_debug
79 #include <dev/usb/usb_debug.h>
80 #include <dev/usb/usb_process.h>
83 static int ulpt_debug = 0;
85 SYSCTL_NODE(_hw_usb, OID_AUTO, ulpt, CTLFLAG_RW, 0, "USB ulpt");
86 SYSCTL_INT(_hw_usb_ulpt, OID_AUTO, debug, CTLFLAG_RW,
87 &ulpt_debug, 0, "Debug level");
90 #define ULPT_BSIZE (1<<15) /* bytes */
91 #define ULPT_IFQ_MAXLEN 2 /* units */
93 #define UR_GET_DEVICE_ID 0x00
94 #define UR_GET_PORT_STATUS 0x01
95 #define UR_SOFT_RESET 0x02
97 #define LPS_NERR 0x08 /* printer no error */
98 #define LPS_SELECT 0x10 /* printer selected */
99 #define LPS_NOPAPER 0x20 /* printer out of paper */
100 #define LPS_INVERT (LPS_SELECT|LPS_NERR)
101 #define LPS_MASK (LPS_SELECT|LPS_NERR|LPS_NOPAPER)
111 struct usb_fifo_sc sc_fifo;
112 struct usb_fifo_sc sc_fifo_noreset;
114 struct usb_callout sc_watchdog;
117 struct usb_device *sc_udev;
118 struct usb_fifo *sc_fifo_open[2];
119 struct usb_xfer *sc_xfer[ULPT_N_TRANSFER];
121 int sc_fflags; /* current open flags, FREAD and
124 uint8_t sc_last_status;
125 uint8_t sc_zlps; /* number of consequtive zero length
126 * packets received */
131 static device_probe_t ulpt_probe;
132 static device_attach_t ulpt_attach;
133 static device_detach_t ulpt_detach;
135 static usb_callback_t ulpt_write_callback;
136 static usb_callback_t ulpt_read_callback;
137 static usb_callback_t ulpt_status_callback;
139 static void ulpt_reset(struct ulpt_softc *);
140 static void ulpt_watchdog(void *);
142 static usb_fifo_close_t ulpt_close;
143 static usb_fifo_cmd_t ulpt_start_read;
144 static usb_fifo_cmd_t ulpt_start_write;
145 static usb_fifo_cmd_t ulpt_stop_read;
146 static usb_fifo_cmd_t ulpt_stop_write;
147 static usb_fifo_ioctl_t ulpt_ioctl;
148 static usb_fifo_open_t ulpt_open;
149 static usb_fifo_open_t unlpt_open;
151 static struct usb_fifo_methods ulpt_fifo_methods = {
152 .f_close = &ulpt_close,
153 .f_ioctl = &ulpt_ioctl,
154 .f_open = &ulpt_open,
155 .f_start_read = &ulpt_start_read,
156 .f_start_write = &ulpt_start_write,
157 .f_stop_read = &ulpt_stop_read,
158 .f_stop_write = &ulpt_stop_write,
159 .basename[0] = "ulpt",
162 static struct usb_fifo_methods unlpt_fifo_methods = {
163 .f_close = &ulpt_close,
164 .f_ioctl = &ulpt_ioctl,
165 .f_open = &unlpt_open,
166 .f_start_read = &ulpt_start_read,
167 .f_start_write = &ulpt_start_write,
168 .f_stop_read = &ulpt_stop_read,
169 .f_stop_write = &ulpt_stop_write,
170 .basename[0] = "unlpt",
174 ulpt_reset(struct ulpt_softc *sc)
176 struct usb_device_request req;
180 req.bRequest = UR_SOFT_RESET;
181 USETW(req.wValue, 0);
182 USETW(req.wIndex, sc->sc_iface_no);
183 USETW(req.wLength, 0);
186 * There was a mistake in the USB printer 1.0 spec that gave the
187 * request type as UT_WRITE_CLASS_OTHER; it should have been
188 * UT_WRITE_CLASS_INTERFACE. Many printers use the old one,
192 mtx_lock(&sc->sc_mtx);
193 req.bmRequestType = UT_WRITE_CLASS_OTHER;
194 if (usbd_do_request_flags(sc->sc_udev, &sc->sc_mtx,
195 &req, NULL, 0, NULL, 2 * USB_MS_HZ)) { /* 1.0 */
196 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
197 if (usbd_do_request_flags(sc->sc_udev, &sc->sc_mtx,
198 &req, NULL, 0, NULL, 2 * USB_MS_HZ)) { /* 1.1 */
202 mtx_unlock(&sc->sc_mtx);
206 ulpt_write_callback(struct usb_xfer *xfer, usb_error_t error)
208 struct ulpt_softc *sc = usbd_xfer_softc(xfer);
209 struct usb_fifo *f = sc->sc_fifo_open[USB_FIFO_TX];
210 struct usb_page_cache *pc;
213 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
216 /* should not happen */
217 DPRINTF("no FIFO\n");
220 DPRINTF("state=0x%x actlen=%d\n", USB_GET_STATE(xfer), actlen);
222 switch (USB_GET_STATE(xfer)) {
223 case USB_ST_TRANSFERRED:
226 pc = usbd_xfer_get_frame(xfer, 0);
227 max = usbd_xfer_max_len(xfer);
228 if (usb_fifo_get_data(f, pc, 0, max, &actlen, 0)) {
229 usbd_xfer_set_frame_len(xfer, 0, actlen);
230 usbd_transfer_submit(xfer);
235 if (error != USB_ERR_CANCELLED) {
236 /* try to clear stall first */
237 usbd_xfer_set_stall(xfer);
245 ulpt_read_callback(struct usb_xfer *xfer, usb_error_t error)
247 struct ulpt_softc *sc = usbd_xfer_softc(xfer);
248 struct usb_fifo *f = sc->sc_fifo_open[USB_FIFO_RX];
249 struct usb_page_cache *pc;
252 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
255 /* should not happen */
256 DPRINTF("no FIFO\n");
259 DPRINTF("state=0x%x\n", USB_GET_STATE(xfer));
261 switch (USB_GET_STATE(xfer)) {
262 case USB_ST_TRANSFERRED:
266 if (sc->sc_zlps == 4) {
267 /* enable BULK throttle */
268 usbd_xfer_set_interval(xfer, 500); /* ms */
273 /* disable BULK throttle */
275 usbd_xfer_set_interval(xfer, 0);
279 pc = usbd_xfer_get_frame(xfer, 0);
280 usb_fifo_put_data(f, pc, 0, actlen, 1);
284 if (usb_fifo_put_bytes_max(f) != 0) {
285 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
286 usbd_transfer_submit(xfer);
291 /* disable BULK throttle */
292 usbd_xfer_set_interval(xfer, 0);
295 if (error != USB_ERR_CANCELLED) {
296 /* try to clear stall first */
297 usbd_xfer_set_stall(xfer);
305 ulpt_status_callback(struct usb_xfer *xfer, usb_error_t error)
307 struct ulpt_softc *sc = usbd_xfer_softc(xfer);
308 struct usb_device_request req;
309 struct usb_page_cache *pc;
313 switch (USB_GET_STATE(xfer)) {
314 case USB_ST_TRANSFERRED:
316 pc = usbd_xfer_get_frame(xfer, 1);
317 usbd_copy_out(pc, 0, &cur_status, 1);
319 cur_status = (cur_status ^ LPS_INVERT) & LPS_MASK;
320 new_status = cur_status & ~sc->sc_last_status;
321 sc->sc_last_status = cur_status;
323 if (new_status & LPS_SELECT)
324 log(LOG_NOTICE, "%s: offline\n",
325 device_get_nameunit(sc->sc_dev));
326 else if (new_status & LPS_NOPAPER)
327 log(LOG_NOTICE, "%s: out of paper\n",
328 device_get_nameunit(sc->sc_dev));
329 else if (new_status & LPS_NERR)
330 log(LOG_NOTICE, "%s: output error\n",
331 device_get_nameunit(sc->sc_dev));
335 req.bmRequestType = UT_READ_CLASS_INTERFACE;
336 req.bRequest = UR_GET_PORT_STATUS;
337 USETW(req.wValue, 0);
338 req.wIndex[0] = sc->sc_iface_no;
340 USETW(req.wLength, 1);
342 pc = usbd_xfer_get_frame(xfer, 0);
343 usbd_copy_in(pc, 0, &req, sizeof(req));
345 usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
346 usbd_xfer_set_frame_len(xfer, 1, 1);
347 usbd_xfer_set_frames(xfer, 2);
348 usbd_transfer_submit(xfer);
352 DPRINTF("error=%s\n", usbd_errstr(error));
353 if (error != USB_ERR_CANCELLED) {
354 /* wait for next watchdog timeout */
360 static const struct usb_config ulpt_config[ULPT_N_TRANSFER] = {
361 [ULPT_BULK_DT_WR] = {
363 .endpoint = UE_ADDR_ANY,
364 .direction = UE_DIR_OUT,
365 .bufsize = ULPT_BSIZE,
366 .flags = {.pipe_bof = 1,.proxy_buffer = 1},
367 .callback = &ulpt_write_callback,
370 [ULPT_BULK_DT_RD] = {
372 .endpoint = UE_ADDR_ANY,
373 .direction = UE_DIR_IN,
374 .bufsize = ULPT_BSIZE,
375 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,.proxy_buffer = 1},
376 .callback = &ulpt_read_callback,
379 [ULPT_INTR_DT_RD] = {
381 .endpoint = 0x00, /* Control pipe */
382 .direction = UE_DIR_ANY,
383 .bufsize = sizeof(struct usb_device_request) + 1,
384 .callback = &ulpt_status_callback,
385 .timeout = 1000, /* 1 second */
390 ulpt_start_read(struct usb_fifo *fifo)
392 struct ulpt_softc *sc = usb_fifo_softc(fifo);
394 usbd_transfer_start(sc->sc_xfer[ULPT_BULK_DT_RD]);
398 ulpt_stop_read(struct usb_fifo *fifo)
400 struct ulpt_softc *sc = usb_fifo_softc(fifo);
402 usbd_transfer_stop(sc->sc_xfer[ULPT_BULK_DT_RD]);
406 ulpt_start_write(struct usb_fifo *fifo)
408 struct ulpt_softc *sc = usb_fifo_softc(fifo);
410 usbd_transfer_start(sc->sc_xfer[ULPT_BULK_DT_WR]);
414 ulpt_stop_write(struct usb_fifo *fifo)
416 struct ulpt_softc *sc = usb_fifo_softc(fifo);
418 usbd_transfer_stop(sc->sc_xfer[ULPT_BULK_DT_WR]);
422 ulpt_open(struct usb_fifo *fifo, int fflags)
424 struct ulpt_softc *sc = usb_fifo_softc(fifo);
426 /* we assume that open is a serial process */
428 if (sc->sc_fflags == 0) {
430 /* reset USB paralell port */
434 return (unlpt_open(fifo, fflags));
438 unlpt_open(struct usb_fifo *fifo, int fflags)
440 struct ulpt_softc *sc = usb_fifo_softc(fifo);
442 if (sc->sc_fflags & fflags) {
445 if (fflags & FREAD) {
446 /* clear stall first */
447 mtx_lock(&sc->sc_mtx);
448 usbd_xfer_set_stall(sc->sc_xfer[ULPT_BULK_DT_RD]);
449 mtx_unlock(&sc->sc_mtx);
450 if (usb_fifo_alloc_buffer(fifo,
451 usbd_xfer_max_len(sc->sc_xfer[ULPT_BULK_DT_RD]),
455 /* set which FIFO is opened */
456 sc->sc_fifo_open[USB_FIFO_RX] = fifo;
458 if (fflags & FWRITE) {
459 /* clear stall first */
460 mtx_lock(&sc->sc_mtx);
461 usbd_xfer_set_stall(sc->sc_xfer[ULPT_BULK_DT_WR]);
462 mtx_unlock(&sc->sc_mtx);
463 if (usb_fifo_alloc_buffer(fifo,
464 usbd_xfer_max_len(sc->sc_xfer[ULPT_BULK_DT_WR]),
468 /* set which FIFO is opened */
469 sc->sc_fifo_open[USB_FIFO_TX] = fifo;
471 sc->sc_fflags |= fflags & (FREAD | FWRITE);
476 ulpt_close(struct usb_fifo *fifo, int fflags)
478 struct ulpt_softc *sc = usb_fifo_softc(fifo);
480 sc->sc_fflags &= ~(fflags & (FREAD | FWRITE));
482 if (fflags & (FREAD | FWRITE)) {
483 usb_fifo_free_buffer(fifo);
488 ulpt_ioctl(struct usb_fifo *fifo, u_long cmd, void *data,
495 ulpt_probe(device_t dev)
497 struct usb_attach_arg *uaa = device_get_ivars(dev);
501 if (uaa->usb_mode != USB_MODE_HOST) {
504 if ((uaa->info.bInterfaceClass == UICLASS_PRINTER) &&
505 (uaa->info.bInterfaceSubClass == UISUBCLASS_PRINTER) &&
506 ((uaa->info.bInterfaceProtocol == UIPROTO_PRINTER_UNI) ||
507 (uaa->info.bInterfaceProtocol == UIPROTO_PRINTER_BI) ||
508 (uaa->info.bInterfaceProtocol == UIPROTO_PRINTER_1284))) {
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 mtx_init(&sc->sc_mtx, "ulpt lock", NULL, MTX_DEF | MTX_RECURSE);
534 usb_callout_init_mtx(&sc->sc_watchdog, &sc->sc_mtx, 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_mtx,
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_mtx,
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 mtx_lock(&sc->sc_mtx);
643 mtx_unlock(&sc->sc_mtx);
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 mtx_lock(&sc->sc_mtx);
662 usb_callout_stop(&sc->sc_watchdog);
663 mtx_unlock(&sc->sc_mtx);
665 usbd_transfer_unsetup(sc->sc_xfer, ULPT_N_TRANSFER);
666 usb_callout_drain(&sc->sc_watchdog);
667 mtx_destroy(&sc->sc_mtx);
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 mtx_assert(&sc->sc_mtx, MA_OWNED);
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_DEPEND(ulpt, ucom, 1, 1, 1);