2 * $NetBSD: ulpt.c,v 1.55 2002/10/23 09:14:01 jdolecek Exp $
3 * $FreeBSD: src/sys/dev/usb/ulpt.c,v 1.59 2003/09/28 20:48:13 phk Exp $
4 * $DragonFly: src/sys/dev/usbmisc/ulpt/ulpt.c,v 1.17 2007/06/27 12:28:00 hasso Exp $
8 * Copyright (c) 1998 The NetBSD Foundation, Inc.
11 * This code is derived from software contributed to The NetBSD Foundation
12 * by Lennart Augustsson (lennart@augustsson.net) at
13 * Carlstedt Research & Technology.
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
23 * 3. All advertising materials mentioning features or use of this software
24 * must display the following acknowledgement:
25 * This product includes software developed by the NetBSD
26 * Foundation, Inc. and its contributors.
27 * 4. Neither the name of The NetBSD Foundation nor the names of its
28 * contributors may be used to endorse or promote products derived
29 * from this software without specific prior written permission.
31 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
32 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
33 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
34 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
35 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
36 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
37 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
38 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
39 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41 * POSSIBILITY OF SUCH DAMAGE.
45 * Printer Class spec: http://www.usb.org/developers/data/devclass/usbprint109.PDF
48 #include <sys/param.h>
49 #include <sys/systm.h>
51 #include <sys/kernel.h>
52 #include <sys/ioccom.h>
53 #include <sys/module.h>
57 #include <sys/device.h>
58 #include <sys/syslog.h>
59 #include <sys/sysctl.h>
60 #include <sys/thread2.h>
62 #include <bus/usb/usb.h>
63 #include <bus/usb/usbdi.h>
64 #include <bus/usb/usbdi_util.h>
65 #include <bus/usb/usbdevs.h>
66 #include <bus/usb/usb_quirks.h>
68 #define TIMEOUT hz*16 /* wait up to 16 seconds for a ready */
71 #define ULPT_BSIZE 16384
74 #define DPRINTF(x) if (ulptdebug) logprintf x
75 #define DPRINTFN(n,x) if (ulptdebug>(n)) logprintf x
77 SYSCTL_NODE(_hw_usb, OID_AUTO, ulpt, CTLFLAG_RW, 0, "USB ulpt");
78 SYSCTL_INT(_hw_usb_ulpt, OID_AUTO, debug, CTLFLAG_RW,
79 &ulptdebug, 0, "ulpt debug level");
85 #define UR_GET_DEVICE_ID 0
86 #define UR_GET_PORT_STATUS 1
87 #define UR_SOFT_RESET 2
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)
97 usbd_device_handle sc_udev; /* device */
98 usbd_interface_handle sc_iface; /* interface */
102 usbd_pipe_handle sc_out_pipe; /* bulk out pipe */
105 usbd_pipe_handle sc_in_pipe; /* bulk in pipe */
106 usbd_xfer_handle sc_in_xfer1;
107 usbd_xfer_handle sc_in_xfer2;
108 u_char sc_junk[64]; /* somewhere to dump input */
111 #define ULPT_OPEN 0x01 /* device is open */
112 #define ULPT_OBUSY 0x02 /* printer is busy doing output */
113 #define ULPT_INIT 0x04 /* waiting to initialize for open */
115 #define ULPT_NOPRIME 0x40 /* don't prime on open */
116 u_char sc_laststatus;
122 Static d_open_t ulptopen;
123 Static d_close_t ulptclose;
124 Static d_write_t ulptwrite;
125 Static d_ioctl_t ulptioctl;
127 #define ULPT_CDEV_MAJOR 113
129 Static struct dev_ops ulpt_ops = {
130 { "ulpt", ULPT_CDEV_MAJOR, 0 },
132 .d_close = ulptclose,
133 .d_write = ulptwrite,
134 .d_ioctl = ulptioctl,
137 void ulpt_disco(void *);
139 int ulpt_do_write(struct ulpt_softc *, struct uio *uio, int);
140 int ulpt_status(struct ulpt_softc *);
141 void ulpt_reset(struct ulpt_softc *);
142 int ulpt_statusmsg(u_char, struct ulpt_softc *);
145 void ieee1284_print_id(char *);
148 #define ULPTUNIT(s) (minor(s) & 0x1f)
149 #define ULPTFLAGS(s) (minor(s) & 0xe0)
152 USB_DECLARE_DRIVER(ulpt);
156 USB_MATCH_START(ulpt, uaa);
157 usb_interface_descriptor_t *id;
159 DPRINTFN(10,("ulpt_match\n"));
160 if (uaa->iface == NULL)
161 return (UMATCH_NONE);
162 id = usbd_get_interface_descriptor(uaa->iface);
164 id->bInterfaceClass == UICLASS_PRINTER &&
165 id->bInterfaceSubClass == UISUBCLASS_PRINTER &&
166 (id->bInterfaceProtocol == UIPROTO_PRINTER_UNI ||
167 id->bInterfaceProtocol == UIPROTO_PRINTER_BI ||
168 id->bInterfaceProtocol == UIPROTO_PRINTER_1284))
169 return (UMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO);
170 return (UMATCH_NONE);
175 USB_ATTACH_START(ulpt, sc, uaa);
176 usbd_device_handle dev = uaa->device;
177 usbd_interface_handle iface = uaa->iface;
178 usb_interface_descriptor_t *ifcd = usbd_get_interface_descriptor(iface);
179 usb_interface_descriptor_t *id, *iend;
180 usb_config_descriptor_t *cdesc;
183 usb_endpoint_descriptor_t *ed;
187 DPRINTFN(10,("ulpt_attach: sc=%p\n", sc));
188 usbd_devinfo(dev, 0, devinfo);
190 kprintf("%s: %s, iclass %d/%d\n", USBDEVNAME(sc->sc_dev),
191 devinfo, ifcd->bInterfaceClass, ifcd->bInterfaceSubClass);
194 * Stepping through the alternate settings needs to be abstracted out.
196 cdesc = usbd_get_config_descriptor(dev);
198 kprintf("%s: failed to get configuration descriptor\n",
199 USBDEVNAME(sc->sc_dev));
200 USB_ATTACH_ERROR_RETURN;
202 iend = (usb_interface_descriptor_t *)
203 ((char *)cdesc + UGETW(cdesc->wTotalLength));
205 if (ifcd < (usb_interface_descriptor_t *)cdesc ||
207 panic("ulpt: iface desc out of range");
209 /* Step through all the descriptors looking for bidir mode */
210 for (id = ifcd, altno = 0;
212 id = (void *)((char *)id + id->bLength)) {
213 if (id->bDescriptorType == UDESC_INTERFACE &&
214 id->bInterfaceNumber == ifcd->bInterfaceNumber) {
215 if (id->bInterfaceClass == UICLASS_PRINTER &&
216 id->bInterfaceSubClass == UISUBCLASS_PRINTER &&
217 (id->bInterfaceProtocol == UIPROTO_PRINTER_BI /* ||
218 id->bInterfaceProtocol == UIPROTO_PRINTER_1284 */))
223 id = ifcd; /* not found, use original */
226 /* Found a new bidir setting */
227 DPRINTF(("ulpt_attach: set altno = %d\n", altno));
228 err = usbd_set_interface(iface, altno);
230 kprintf("%s: setting alternate interface failed\n",
231 USBDEVNAME(sc->sc_dev));
233 USB_ATTACH_ERROR_RETURN;
238 (void)usbd_endpoint_count(iface, &epcount);
242 for (i = 0; i < epcount; i++) {
243 ed = usbd_interface2endpoint_descriptor(iface, i);
245 kprintf("%s: couldn't get ep %d\n",
246 USBDEVNAME(sc->sc_dev), i);
247 USB_ATTACH_ERROR_RETURN;
249 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
250 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
251 sc->sc_in = ed->bEndpointAddress;
252 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
253 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
254 sc->sc_out = ed->bEndpointAddress;
257 if (sc->sc_out == -1) {
258 kprintf("%s: could not find bulk out endpoint\n",
259 USBDEVNAME(sc->sc_dev));
261 USB_ATTACH_ERROR_RETURN;
264 if (usbd_get_quirks(dev)->uq_flags & UQ_BROKEN_BIDIR) {
265 /* This device doesn't handle reading properly. */
269 kprintf("%s: using %s-directional mode\n", USBDEVNAME(sc->sc_dev),
270 sc->sc_in >= 0 ? "bi" : "uni");
272 DPRINTFN(10, ("ulpt_attach: bulk=%d\n", sc->sc_out));
274 sc->sc_iface = iface;
275 sc->sc_ifaceno = id->bInterfaceNumber;
280 * This code is disabled because for some mysterious reason it causes
281 * printing not to work. But only sometimes, and mostly with
282 * UHCI and less often with OHCI. *sigh*
285 usb_config_descriptor_t *cd = usbd_get_config_descriptor(dev);
286 usb_device_request_t req;
289 req.bmRequestType = UT_READ_CLASS_INTERFACE;
290 req.bRequest = UR_GET_DEVICE_ID;
291 USETW(req.wValue, cd->bConfigurationValue);
292 USETW2(req.wIndex, id->bInterfaceNumber, id->bAlternateSetting);
293 USETW(req.wLength, sizeof devinfo - 1);
294 err = usbd_do_request_flags(dev, &req, devinfo, USBD_SHORT_XFER_OK,
295 &alen, USBD_DEFAULT_TIMEOUT);
297 kprintf("%s: cannot get device id\n", USBDEVNAME(sc->sc_dev));
298 } else if (alen <= 2) {
299 kprintf("%s: empty device id, no printer connected?\n",
300 USBDEVNAME(sc->sc_dev));
302 /* devinfo now contains an IEEE-1284 device ID */
303 len = ((devinfo[0] & 0xff) << 8) | (devinfo[1] & 0xff);
304 if (len > sizeof devinfo - 3)
305 len = sizeof devinfo - 3;
307 kprintf("%s: device id <", USBDEVNAME(sc->sc_dev));
308 ieee1284_print_id(devinfo+2);
314 dev_ops_add(&ulpt_ops, -1, device_get_unit(self));
315 make_dev(&ulpt_ops, device_get_unit(self),
316 UID_ROOT, GID_OPERATOR, 0644, "ulpt%d", device_get_unit(self));
318 device_get_unit(self)|ULPT_NOPRIME,
319 UID_ROOT, GID_OPERATOR, 0644, "unlpt%d", device_get_unit(self));
321 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
324 USB_ATTACH_SUCCESS_RETURN;
329 USB_DETACH_START(ulpt, sc);
331 DPRINTF(("ulpt_detach: sc=%p\n", sc));
334 if (sc->sc_out_pipe != NULL)
335 usbd_abort_pipe(sc->sc_out_pipe);
336 if (sc->sc_in_pipe != NULL)
337 usbd_abort_pipe(sc->sc_in_pipe);
340 * Wait for any ongoing operations to complete before we actually
346 if (sc->sc_refcnt >= 0) {
347 kprintf("%s: waiting for idle\n", USBDEVNAME(sc->sc_dev));
348 while (sc->sc_refcnt >= 0)
349 usb_detach_wait(sc->sc_dev);
350 kprintf("%s: idle wait done\n", USBDEVNAME(sc->sc_dev));
354 dev_ops_remove(&ulpt_ops, -1, device_get_unit(self));
356 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
363 ulpt_status(struct ulpt_softc *sc)
365 usb_device_request_t req;
369 req.bmRequestType = UT_READ_CLASS_INTERFACE;
370 req.bRequest = UR_GET_PORT_STATUS;
371 USETW(req.wValue, 0);
372 USETW(req.wIndex, sc->sc_ifaceno);
373 USETW(req.wLength, 1);
374 err = usbd_do_request(sc->sc_udev, &req, &status);
375 DPRINTFN(1, ("ulpt_status: status=0x%02x err=%d\n", status, err));
383 ulpt_reset(struct ulpt_softc *sc)
385 usb_device_request_t req;
387 DPRINTFN(1, ("ulpt_reset\n"));
388 req.bRequest = UR_SOFT_RESET;
389 USETW(req.wValue, 0);
390 USETW(req.wIndex, sc->sc_ifaceno);
391 USETW(req.wLength, 0);
394 * There was a mistake in the USB printer 1.0 spec that gave the
395 * request type as UT_WRITE_CLASS_OTHER; it should have been
396 * UT_WRITE_CLASS_INTERFACE. Many printers use the old one,
399 req.bmRequestType = UT_WRITE_CLASS_OTHER;
400 if (usbd_do_request(sc->sc_udev, &req, 0)) { /* 1.0 */
401 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
402 (void)usbd_do_request(sc->sc_udev, &req, 0); /* 1.1 */
407 ulpt_input(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
409 struct ulpt_softc *sc = priv;
412 /* Don't loop on errors or 0-length input. */
413 usbd_get_xfer_status(xfer, NULL, NULL, &count, NULL);
414 if (status != USBD_NORMAL_COMPLETION || count == 0)
417 DPRINTFN(2,("ulpt_input: got some data\n"));
419 if (xfer == sc->sc_in_xfer1)
420 usbd_transfer(sc->sc_in_xfer2);
422 usbd_transfer(sc->sc_in_xfer1);
428 * Reset the printer, then wait until it's selected and not busy.
431 ulptopen(struct dev_open_args *ap)
433 cdev_t dev = ap->a_head.a_dev;
434 u_char flags = ULPTFLAGS(dev);
435 struct ulpt_softc *sc;
439 USB_GET_SC_OPEN(ulpt, ULPTUNIT(dev), sc);
441 if (sc == NULL || sc->sc_iface == NULL || sc->sc_dying)
447 sc->sc_state = ULPT_INIT;
448 sc->sc_flags = flags;
449 DPRINTF(("ulptopen: flags=0x%x\n", (unsigned)flags));
451 #if defined(USB_DEBUG)
452 /* Ignoring these flags might not be a good idea */
453 if ((flags & ~ULPT_NOPRIME) != 0)
454 kprintf("ulptopen: flags ignored: %b\n", flags,
455 "\20\3POS_INIT\4POS_ACK\6PRIME_OPEN\7AUTOLF\10BYPASS");
461 if ((flags & ULPT_NOPRIME) == 0) {
470 for (spin = 0; (ulpt_status(sc) & LPS_SELECT) == 0; spin += STEP) {
471 DPRINTF(("ulpt_open: waiting a while\n"));
472 if (spin >= TIMEOUT) {
478 /* wait 1/4 second, give up if we get a signal */
479 error = tsleep((caddr_t)sc, PCATCH, "ulptop", STEP);
480 if (error != EWOULDBLOCK) {
492 err = usbd_open_pipe(sc->sc_iface, sc->sc_out, 0, &sc->sc_out_pipe);
499 if (ulptusein && sc->sc_in != -1) {
500 DPRINTF(("ulpt_open: open input pipe\n"));
501 err = usbd_open_pipe(sc->sc_iface, sc->sc_in,0,&sc->sc_in_pipe);
504 usbd_close_pipe(sc->sc_out_pipe);
505 sc->sc_out_pipe = NULL;
509 sc->sc_in_xfer1 = usbd_alloc_xfer(sc->sc_udev);
510 sc->sc_in_xfer2 = usbd_alloc_xfer(sc->sc_udev);
511 if (sc->sc_in_xfer1 == NULL || sc->sc_in_xfer2 == NULL) {
513 if (sc->sc_in_xfer1 != NULL) {
514 usbd_free_xfer(sc->sc_in_xfer1);
515 sc->sc_in_xfer1 = NULL;
517 if (sc->sc_in_xfer2 != NULL) {
518 usbd_free_xfer(sc->sc_in_xfer2);
519 sc->sc_in_xfer2 = NULL;
521 usbd_close_pipe(sc->sc_out_pipe);
522 sc->sc_out_pipe = NULL;
523 usbd_close_pipe(sc->sc_in_pipe);
524 sc->sc_in_pipe = NULL;
528 usbd_setup_xfer(sc->sc_in_xfer1, sc->sc_in_pipe, sc,
529 sc->sc_junk, sizeof sc->sc_junk, USBD_SHORT_XFER_OK,
530 USBD_NO_TIMEOUT, ulpt_input);
531 usbd_setup_xfer(sc->sc_in_xfer2, sc->sc_in_pipe, sc,
532 sc->sc_junk, sizeof sc->sc_junk, USBD_SHORT_XFER_OK,
533 USBD_NO_TIMEOUT, ulpt_input);
534 usbd_transfer(sc->sc_in_xfer1); /* ignore failed start */
537 sc->sc_state = ULPT_OPEN;
540 if (--sc->sc_refcnt < 0)
541 usb_detach_wakeup(sc->sc_dev);
543 DPRINTF(("ulptopen: done, error=%d\n", error));
548 ulpt_statusmsg(u_char status, struct ulpt_softc *sc)
552 status = (status ^ LPS_INVERT) & LPS_MASK;
553 new = status & ~sc->sc_laststatus;
554 sc->sc_laststatus = status;
556 if (new & LPS_SELECT)
557 log(LOG_NOTICE, "%s: offline\n", USBDEVNAME(sc->sc_dev));
558 else if (new & LPS_NOPAPER)
559 log(LOG_NOTICE, "%s: out of paper\n", USBDEVNAME(sc->sc_dev));
560 else if (new & LPS_NERR)
561 log(LOG_NOTICE, "%s: output error\n", USBDEVNAME(sc->sc_dev));
567 ulptclose(struct dev_close_args *ap)
569 cdev_t dev = ap->a_head.a_dev;
570 struct ulpt_softc *sc;
572 USB_GET_SC(ulpt, ULPTUNIT(dev), sc);
574 if (sc->sc_state != ULPT_OPEN)
575 /* We are being forced to close before the open completed. */
578 if (sc->sc_out_pipe != NULL) {
579 usbd_close_pipe(sc->sc_out_pipe);
580 sc->sc_out_pipe = NULL;
582 if (sc->sc_in_pipe != NULL) {
583 usbd_abort_pipe(sc->sc_in_pipe);
584 usbd_close_pipe(sc->sc_in_pipe);
585 sc->sc_in_pipe = NULL;
586 if (sc->sc_in_xfer1 != NULL) {
587 usbd_free_xfer(sc->sc_in_xfer1);
588 sc->sc_in_xfer1 = NULL;
590 if (sc->sc_in_xfer2 != NULL) {
591 usbd_free_xfer(sc->sc_in_xfer2);
592 sc->sc_in_xfer2 = NULL;
598 DPRINTF(("ulptclose: closed\n"));
603 ulpt_do_write(struct ulpt_softc *sc, struct uio *uio, int flags)
608 usbd_xfer_handle xfer;
611 DPRINTF(("ulptwrite\n"));
612 xfer = usbd_alloc_xfer(sc->sc_udev);
615 bufp = usbd_alloc_buffer(xfer, ULPT_BSIZE);
617 usbd_free_xfer(xfer);
620 while ((n = min(ULPT_BSIZE, uio->uio_resid)) != 0) {
621 ulpt_statusmsg(ulpt_status(sc), sc);
622 error = uiomove(bufp, n, uio);
625 DPRINTFN(1, ("ulptwrite: transfer %d bytes\n", n));
626 err = usbd_bulk_transfer(xfer, sc->sc_out_pipe, USBD_NO_COPY,
627 USBD_NO_TIMEOUT, bufp, &n, "ulptwr");
629 DPRINTF(("ulptwrite: error=%d\n", err));
634 usbd_free_xfer(xfer);
640 ulptwrite(struct dev_write_args *ap)
642 cdev_t dev = ap->a_head.a_dev;
643 struct ulpt_softc *sc;
646 USB_GET_SC(ulpt, ULPTUNIT(dev), sc);
652 error = ulpt_do_write(sc, ap->a_uio, ap->a_ioflag);
653 if (--sc->sc_refcnt < 0)
654 usb_detach_wakeup(sc->sc_dev);
659 ulptioctl(struct dev_ioctl_args *ap)
672 /* XXX This does not belong here. */
674 * Print select parts of an IEEE 1284 device ID.
677 ieee1284_print_id(char *str)
681 for (p = str-1; p; p = strchr(p, ';')) {
683 if (strncmp(p, "MFG:", 4) == 0 ||
684 strncmp(p, "MANUFACTURER:", 14) == 0 ||
685 strncmp(p, "MDL:", 4) == 0 ||
686 strncmp(p, "MODEL:", 6) == 0) {
689 kprintf("%.*s", (int)(q - p + 1), p);
695 DRIVER_MODULE(ulpt, uhub, ulpt_driver, ulpt_devclass, usbd_driver_load, 0);