4 /* $NetBSD: ulpt.c,v 1.60 2003/10/04 21:19:50 augustss Exp $ */
7 * SPDX-License-Identifier: BSD-2-Clause-NetBSD
9 * Copyright (c) 1998, 2003 The NetBSD Foundation, Inc.
10 * All rights reserved.
12 * This code is derived from software contributed to The NetBSD Foundation
13 * by Lennart Augustsson (lennart@augustsson.net) at
14 * Carlstedt Research & Technology.
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
19 * 1. Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
21 * 2. Redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution.
25 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
26 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
29 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
39 * Printer Class spec: http://www.usb.org/developers/data/devclass/usbprint109.PDF
40 * Printer Class spec: http://www.usb.org/developers/devclass_docs/usbprint11.pdf
43 #include <sys/stdint.h>
44 #include <sys/stddef.h>
45 #include <sys/param.h>
46 #include <sys/queue.h>
47 #include <sys/types.h>
48 #include <sys/systm.h>
49 #include <sys/kernel.h>
51 #include <sys/module.h>
53 #include <sys/mutex.h>
54 #include <sys/condvar.h>
55 #include <sys/sysctl.h>
57 #include <sys/unistd.h>
58 #include <sys/callout.h>
59 #include <sys/malloc.h>
61 #include <sys/syslog.h>
62 #include <sys/selinfo.h>
64 #include <sys/fcntl.h>
66 #include <dev/usb/usb.h>
67 #include <dev/usb/usbdi.h>
68 #include <dev/usb/usbdi_util.h>
69 #include <dev/usb/usbhid.h>
72 #define USB_DEBUG_VAR ulpt_debug
73 #include <dev/usb/usb_debug.h>
74 #include <dev/usb/usb_process.h>
77 static int ulpt_debug = 0;
79 static SYSCTL_NODE(_hw_usb, OID_AUTO, ulpt, CTLFLAG_RW, 0, "USB ulpt");
80 SYSCTL_INT(_hw_usb_ulpt, OID_AUTO, debug, CTLFLAG_RWTUN,
81 &ulpt_debug, 0, "Debug level");
84 #define ULPT_BSIZE (1<<15) /* bytes */
85 #define ULPT_IFQ_MAXLEN 2 /* units */
87 #define UR_GET_DEVICE_ID 0x00
88 #define UR_GET_PORT_STATUS 0x01
89 #define UR_SOFT_RESET 0x02
91 #define LPS_NERR 0x08 /* printer no error */
92 #define LPS_SELECT 0x10 /* printer selected */
93 #define LPS_NOPAPER 0x20 /* printer out of paper */
94 #define LPS_INVERT (LPS_SELECT|LPS_NERR)
95 #define LPS_MASK (LPS_SELECT|LPS_NERR|LPS_NOPAPER)
105 struct usb_fifo_sc sc_fifo;
106 struct usb_fifo_sc sc_fifo_noreset;
108 struct usb_callout sc_watchdog;
111 struct usb_device *sc_udev;
112 struct usb_fifo *sc_fifo_open[2];
113 struct usb_xfer *sc_xfer[ULPT_N_TRANSFER];
115 int sc_fflags; /* current open flags, FREAD and
118 uint8_t sc_last_status;
119 uint8_t sc_zlps; /* number of consequtive zero length
120 * packets received */
125 static device_probe_t ulpt_probe;
126 static device_attach_t ulpt_attach;
127 static device_detach_t ulpt_detach;
129 static usb_callback_t ulpt_write_callback;
130 static usb_callback_t ulpt_read_callback;
131 static usb_callback_t ulpt_status_callback;
133 static void ulpt_reset(struct ulpt_softc *);
134 static void ulpt_watchdog(void *);
136 static usb_fifo_close_t ulpt_close;
137 static usb_fifo_cmd_t ulpt_start_read;
138 static usb_fifo_cmd_t ulpt_start_write;
139 static usb_fifo_cmd_t ulpt_stop_read;
140 static usb_fifo_cmd_t ulpt_stop_write;
141 static usb_fifo_ioctl_t ulpt_ioctl;
142 static usb_fifo_open_t ulpt_open;
143 static usb_fifo_open_t unlpt_open;
145 static struct usb_fifo_methods ulpt_fifo_methods = {
146 .f_close = &ulpt_close,
147 .f_ioctl = &ulpt_ioctl,
148 .f_open = &ulpt_open,
149 .f_start_read = &ulpt_start_read,
150 .f_start_write = &ulpt_start_write,
151 .f_stop_read = &ulpt_stop_read,
152 .f_stop_write = &ulpt_stop_write,
153 .basename[0] = "ulpt",
156 static struct usb_fifo_methods unlpt_fifo_methods = {
157 .f_close = &ulpt_close,
158 .f_ioctl = &ulpt_ioctl,
159 .f_open = &unlpt_open,
160 .f_start_read = &ulpt_start_read,
161 .f_start_write = &ulpt_start_write,
162 .f_stop_read = &ulpt_stop_read,
163 .f_stop_write = &ulpt_stop_write,
164 .basename[0] = "unlpt",
168 ulpt_reset(struct ulpt_softc *sc)
170 struct usb_device_request req;
174 req.bRequest = UR_SOFT_RESET;
175 USETW(req.wValue, 0);
176 USETW(req.wIndex, sc->sc_iface_no);
177 USETW(req.wLength, 0);
180 * There was a mistake in the USB printer 1.0 spec that gave the
181 * request type as UT_WRITE_CLASS_OTHER; it should have been
182 * UT_WRITE_CLASS_INTERFACE. Many printers use the old one,
186 mtx_lock(&sc->sc_mtx);
187 req.bmRequestType = UT_WRITE_CLASS_OTHER;
188 if (usbd_do_request_flags(sc->sc_udev, &sc->sc_mtx,
189 &req, NULL, 0, NULL, 2 * USB_MS_HZ)) { /* 1.0 */
190 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
191 if (usbd_do_request_flags(sc->sc_udev, &sc->sc_mtx,
192 &req, NULL, 0, NULL, 2 * USB_MS_HZ)) { /* 1.1 */
196 mtx_unlock(&sc->sc_mtx);
200 ulpt_write_callback(struct usb_xfer *xfer, usb_error_t error)
202 struct ulpt_softc *sc = usbd_xfer_softc(xfer);
203 struct usb_fifo *f = sc->sc_fifo_open[USB_FIFO_TX];
204 struct usb_page_cache *pc;
207 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
210 /* should not happen */
211 DPRINTF("no FIFO\n");
214 DPRINTF("state=0x%x actlen=%d\n", USB_GET_STATE(xfer), actlen);
216 switch (USB_GET_STATE(xfer)) {
217 case USB_ST_TRANSFERRED:
220 pc = usbd_xfer_get_frame(xfer, 0);
221 max = usbd_xfer_max_len(xfer);
222 if (usb_fifo_get_data(f, pc, 0, max, &actlen, 0)) {
223 usbd_xfer_set_frame_len(xfer, 0, actlen);
224 usbd_transfer_submit(xfer);
229 if (error != USB_ERR_CANCELLED) {
230 /* try to clear stall first */
231 usbd_xfer_set_stall(xfer);
239 ulpt_read_callback(struct usb_xfer *xfer, usb_error_t error)
241 struct ulpt_softc *sc = usbd_xfer_softc(xfer);
242 struct usb_fifo *f = sc->sc_fifo_open[USB_FIFO_RX];
243 struct usb_page_cache *pc;
246 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
249 /* should not happen */
250 DPRINTF("no FIFO\n");
253 DPRINTF("state=0x%x\n", USB_GET_STATE(xfer));
255 switch (USB_GET_STATE(xfer)) {
256 case USB_ST_TRANSFERRED:
260 if (sc->sc_zlps == 4) {
261 /* enable BULK throttle */
262 usbd_xfer_set_interval(xfer, 500); /* ms */
267 /* disable BULK throttle */
269 usbd_xfer_set_interval(xfer, 0);
273 pc = usbd_xfer_get_frame(xfer, 0);
274 usb_fifo_put_data(f, pc, 0, actlen, 1);
278 if (usb_fifo_put_bytes_max(f) != 0) {
279 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
280 usbd_transfer_submit(xfer);
285 /* disable BULK throttle */
286 usbd_xfer_set_interval(xfer, 0);
289 if (error != USB_ERR_CANCELLED) {
290 /* try to clear stall first */
291 usbd_xfer_set_stall(xfer);
299 ulpt_status_callback(struct usb_xfer *xfer, usb_error_t error)
301 struct ulpt_softc *sc = usbd_xfer_softc(xfer);
302 struct usb_device_request req;
303 struct usb_page_cache *pc;
307 switch (USB_GET_STATE(xfer)) {
308 case USB_ST_TRANSFERRED:
310 pc = usbd_xfer_get_frame(xfer, 1);
311 usbd_copy_out(pc, 0, &cur_status, 1);
313 cur_status = (cur_status ^ LPS_INVERT) & LPS_MASK;
314 new_status = cur_status & ~sc->sc_last_status;
315 sc->sc_last_status = cur_status;
317 if (new_status & LPS_SELECT)
318 log(LOG_NOTICE, "%s: offline\n",
319 device_get_nameunit(sc->sc_dev));
320 else if (new_status & LPS_NOPAPER)
321 log(LOG_NOTICE, "%s: out of paper\n",
322 device_get_nameunit(sc->sc_dev));
323 else if (new_status & LPS_NERR)
324 log(LOG_NOTICE, "%s: output error\n",
325 device_get_nameunit(sc->sc_dev));
329 req.bmRequestType = UT_READ_CLASS_INTERFACE;
330 req.bRequest = UR_GET_PORT_STATUS;
331 USETW(req.wValue, 0);
332 req.wIndex[0] = sc->sc_iface_no;
334 USETW(req.wLength, 1);
336 pc = usbd_xfer_get_frame(xfer, 0);
337 usbd_copy_in(pc, 0, &req, sizeof(req));
339 usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
340 usbd_xfer_set_frame_len(xfer, 1, 1);
341 usbd_xfer_set_frames(xfer, 2);
342 usbd_transfer_submit(xfer);
346 DPRINTF("error=%s\n", usbd_errstr(error));
347 if (error != USB_ERR_CANCELLED) {
348 /* wait for next watchdog timeout */
354 static const struct usb_config ulpt_config[ULPT_N_TRANSFER] = {
355 [ULPT_BULK_DT_WR] = {
357 .endpoint = UE_ADDR_ANY,
358 .direction = UE_DIR_OUT,
359 .bufsize = ULPT_BSIZE,
360 .flags = {.pipe_bof = 1,.proxy_buffer = 1},
361 .callback = &ulpt_write_callback,
364 [ULPT_BULK_DT_RD] = {
366 .endpoint = UE_ADDR_ANY,
367 .direction = UE_DIR_IN,
368 .bufsize = ULPT_BSIZE,
369 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,.proxy_buffer = 1},
370 .callback = &ulpt_read_callback,
373 [ULPT_INTR_DT_RD] = {
375 .endpoint = 0x00, /* Control pipe */
376 .direction = UE_DIR_ANY,
377 .bufsize = sizeof(struct usb_device_request) + 1,
378 .callback = &ulpt_status_callback,
379 .timeout = 1000, /* 1 second */
384 ulpt_start_read(struct usb_fifo *fifo)
386 struct ulpt_softc *sc = usb_fifo_softc(fifo);
388 usbd_transfer_start(sc->sc_xfer[ULPT_BULK_DT_RD]);
392 ulpt_stop_read(struct usb_fifo *fifo)
394 struct ulpt_softc *sc = usb_fifo_softc(fifo);
396 usbd_transfer_stop(sc->sc_xfer[ULPT_BULK_DT_RD]);
400 ulpt_start_write(struct usb_fifo *fifo)
402 struct ulpt_softc *sc = usb_fifo_softc(fifo);
404 usbd_transfer_start(sc->sc_xfer[ULPT_BULK_DT_WR]);
408 ulpt_stop_write(struct usb_fifo *fifo)
410 struct ulpt_softc *sc = usb_fifo_softc(fifo);
412 usbd_transfer_stop(sc->sc_xfer[ULPT_BULK_DT_WR]);
416 ulpt_open(struct usb_fifo *fifo, int fflags)
418 struct ulpt_softc *sc = usb_fifo_softc(fifo);
420 /* we assume that open is a serial process */
422 if (sc->sc_fflags == 0) {
424 /* reset USB parallel port */
428 return (unlpt_open(fifo, fflags));
432 unlpt_open(struct usb_fifo *fifo, int fflags)
434 struct ulpt_softc *sc = usb_fifo_softc(fifo);
436 if (sc->sc_fflags & fflags) {
439 if (fflags & FREAD) {
440 /* clear stall first */
441 mtx_lock(&sc->sc_mtx);
442 usbd_xfer_set_stall(sc->sc_xfer[ULPT_BULK_DT_RD]);
443 mtx_unlock(&sc->sc_mtx);
444 if (usb_fifo_alloc_buffer(fifo,
445 usbd_xfer_max_len(sc->sc_xfer[ULPT_BULK_DT_RD]),
449 /* set which FIFO is opened */
450 sc->sc_fifo_open[USB_FIFO_RX] = fifo;
452 if (fflags & FWRITE) {
453 /* clear stall first */
454 mtx_lock(&sc->sc_mtx);
455 usbd_xfer_set_stall(sc->sc_xfer[ULPT_BULK_DT_WR]);
456 mtx_unlock(&sc->sc_mtx);
457 if (usb_fifo_alloc_buffer(fifo,
458 usbd_xfer_max_len(sc->sc_xfer[ULPT_BULK_DT_WR]),
462 /* set which FIFO is opened */
463 sc->sc_fifo_open[USB_FIFO_TX] = fifo;
465 sc->sc_fflags |= fflags & (FREAD | FWRITE);
470 ulpt_close(struct usb_fifo *fifo, int fflags)
472 struct ulpt_softc *sc = usb_fifo_softc(fifo);
474 sc->sc_fflags &= ~(fflags & (FREAD | FWRITE));
476 if (fflags & (FREAD | FWRITE)) {
477 usb_fifo_free_buffer(fifo);
482 ulpt_ioctl(struct usb_fifo *fifo, u_long cmd, void *data,
488 static const STRUCT_USB_HOST_ID ulpt_devs[] = {
489 /* Uni-directional USB printer */
490 {USB_IFACE_CLASS(UICLASS_PRINTER),
491 USB_IFACE_SUBCLASS(UISUBCLASS_PRINTER),
492 USB_IFACE_PROTOCOL(UIPROTO_PRINTER_UNI)},
494 /* Bi-directional USB printer */
495 {USB_IFACE_CLASS(UICLASS_PRINTER),
496 USB_IFACE_SUBCLASS(UISUBCLASS_PRINTER),
497 USB_IFACE_PROTOCOL(UIPROTO_PRINTER_BI)},
499 /* 1284 USB printer */
500 {USB_IFACE_CLASS(UICLASS_PRINTER),
501 USB_IFACE_SUBCLASS(UISUBCLASS_PRINTER),
502 USB_IFACE_PROTOCOL(UIPROTO_PRINTER_1284)},
506 ulpt_probe(device_t dev)
508 struct usb_attach_arg *uaa = device_get_ivars(dev);
513 if (uaa->usb_mode != USB_MODE_HOST)
516 error = usbd_lookup_id_by_uaa(ulpt_devs, sizeof(ulpt_devs), uaa);
520 return (BUS_PROBE_GENERIC);
524 ulpt_attach(device_t dev)
526 struct usb_attach_arg *uaa = device_get_ivars(dev);
527 struct ulpt_softc *sc = device_get_softc(dev);
528 struct usb_interface_descriptor *id;
529 int unit = device_get_unit(dev);
531 uint8_t iface_index = uaa->info.bIfaceIndex;
534 DPRINTFN(11, "sc=%p\n", sc);
537 sc->sc_udev = uaa->device;
539 device_set_usb_desc(dev);
541 mtx_init(&sc->sc_mtx, "ulpt lock", NULL, MTX_DEF | MTX_RECURSE);
543 usb_callout_init_mtx(&sc->sc_watchdog, &sc->sc_mtx, 0);
545 /* search through all the descriptors looking for bidir mode */
547 id = usbd_get_interface_descriptor(uaa->iface);
553 if ((id->bDescriptorType == UDESC_INTERFACE) &&
554 (id->bLength >= sizeof(*id))) {
555 if (id->bInterfaceNumber != uaa->info.bIfaceNum) {
559 if ((id->bInterfaceClass == UICLASS_PRINTER) &&
560 (id->bInterfaceSubClass == UISUBCLASS_PRINTER) &&
561 (id->bInterfaceProtocol == UIPROTO_PRINTER_BI)) {
566 id = (void *)usb_desc_foreach(
567 usbd_get_config_descriptor(uaa->device), (void *)id);
573 DPRINTF("setting alternate "
574 "config number: %d\n", alt_index);
578 error = usbd_set_alt_interface_index
579 (uaa->device, iface_index, alt_index);
582 DPRINTF("could not set alternate "
583 "config, error=%s\n", usbd_errstr(error));
587 sc->sc_iface_no = id->bInterfaceNumber;
589 error = usbd_transfer_setup(uaa->device, &iface_index,
590 sc->sc_xfer, ulpt_config, ULPT_N_TRANSFER,
593 DPRINTF("error=%s\n", usbd_errstr(error));
596 device_printf(sc->sc_dev, "using bi-directional mode\n");
600 * This code is disabled because for some mysterious reason it causes
601 * printing not to work. But only sometimes, and mostly with
602 * UHCI and less often with OHCI. *sigh*
605 struct usb_config_descriptor *cd = usbd_get_config_descriptor(dev);
606 struct usb_device_request req;
609 req.bmRequestType = UT_READ_CLASS_INTERFACE;
610 req.bRequest = UR_GET_DEVICE_ID;
611 USETW(req.wValue, cd->bConfigurationValue);
612 USETW2(req.wIndex, id->bInterfaceNumber, id->bAlternateSetting);
613 USETW(req.wLength, sizeof devinfo - 1);
614 error = usbd_do_request_flags(dev, &req, devinfo, USB_SHORT_XFER_OK,
615 &alen, USB_DEFAULT_TIMEOUT);
617 device_printf(sc->sc_dev, "cannot get device id\n");
618 } else if (alen <= 2) {
619 device_printf(sc->sc_dev, "empty device id, no "
620 "printer connected?\n");
622 /* devinfo now contains an IEEE-1284 device ID */
623 len = ((devinfo[0] & 0xff) << 8) | (devinfo[1] & 0xff);
624 if (len > sizeof devinfo - 3)
625 len = sizeof devinfo - 3;
627 printf("%s: device id <", device_get_nameunit(sc->sc_dev));
628 ieee1284_print_id(devinfo + 2);
634 error = usb_fifo_attach(uaa->device, sc, &sc->sc_mtx,
635 &ulpt_fifo_methods, &sc->sc_fifo,
636 unit, -1, uaa->info.bIfaceIndex,
637 UID_ROOT, GID_OPERATOR, 0644);
641 error = usb_fifo_attach(uaa->device, sc, &sc->sc_mtx,
642 &unlpt_fifo_methods, &sc->sc_fifo_noreset,
643 unit, -1, uaa->info.bIfaceIndex,
644 UID_ROOT, GID_OPERATOR, 0644);
648 /* start reading of status */
650 mtx_lock(&sc->sc_mtx);
652 mtx_unlock(&sc->sc_mtx);
661 ulpt_detach(device_t dev)
663 struct ulpt_softc *sc = device_get_softc(dev);
665 DPRINTF("sc=%p\n", sc);
667 usb_fifo_detach(&sc->sc_fifo);
668 usb_fifo_detach(&sc->sc_fifo_noreset);
670 mtx_lock(&sc->sc_mtx);
671 usb_callout_stop(&sc->sc_watchdog);
672 mtx_unlock(&sc->sc_mtx);
674 usbd_transfer_unsetup(sc->sc_xfer, ULPT_N_TRANSFER);
675 usb_callout_drain(&sc->sc_watchdog);
676 mtx_destroy(&sc->sc_mtx);
682 /* XXX This does not belong here. */
685 * Compare two strings until the second ends.
689 ieee1284_compare(const char *a, const char *b)
706 * Print select parts of an IEEE 1284 device ID.
709 ieee1284_print_id(char *str)
713 for (p = str - 1; p; p = strchr(p, ';')) {
715 if (ieee1284_compare(p, "MFG:") == 0 ||
716 ieee1284_compare(p, "MANUFACTURER:") == 0 ||
717 ieee1284_compare(p, "MDL:") == 0 ||
718 ieee1284_compare(p, "MODEL:") == 0) {
721 printf("%.*s", (int)(q - p + 1), p);
729 ulpt_watchdog(void *arg)
731 struct ulpt_softc *sc = arg;
733 mtx_assert(&sc->sc_mtx, MA_OWNED);
736 * Only read status while the device is not opened, due to
737 * possible hardware or firmware bug in some printers.
739 if (sc->sc_fflags == 0)
740 usbd_transfer_start(sc->sc_xfer[ULPT_INTR_DT_RD]);
742 usb_callout_reset(&sc->sc_watchdog,
743 hz, &ulpt_watchdog, sc);
746 static devclass_t ulpt_devclass;
748 static device_method_t ulpt_methods[] = {
749 DEVMETHOD(device_probe, ulpt_probe),
750 DEVMETHOD(device_attach, ulpt_attach),
751 DEVMETHOD(device_detach, ulpt_detach),
755 static driver_t ulpt_driver = {
757 .methods = ulpt_methods,
758 .size = sizeof(struct ulpt_softc),
761 DRIVER_MODULE(ulpt, uhub, ulpt_driver, ulpt_devclass, NULL, 0);
762 MODULE_DEPEND(ulpt, usb, 1, 1, 1);
763 MODULE_VERSION(ulpt, 1);
764 USB_PNP_HOST_INFO(ulpt_devs);