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.25 2008/02/11 16:56:53 dillon 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/usb_quirks.h>
67 #define TIMEOUT hz*16 /* wait up to 16 seconds for a ready */
70 #define ULPT_BSIZE 16384
73 #define DPRINTF(x) if (ulptdebug) kprintf x
74 #define DPRINTFN(n,x) if (ulptdebug>(n)) kprintf x
76 SYSCTL_NODE(_hw_usb, OID_AUTO, ulpt, CTLFLAG_RW, 0, "USB ulpt");
77 SYSCTL_INT(_hw_usb_ulpt, OID_AUTO, debug, CTLFLAG_RW,
78 &ulptdebug, 0, "ulpt debug level");
84 #define UR_GET_DEVICE_ID 0
85 #define UR_GET_PORT_STATUS 1
86 #define UR_SOFT_RESET 2
88 #define LPS_NERR 0x08 /* printer no error */
89 #define LPS_SELECT 0x10 /* printer selected */
90 #define LPS_NOPAPER 0x20 /* printer out of paper */
91 #define LPS_INVERT (LPS_SELECT|LPS_NERR)
92 #define LPS_MASK (LPS_SELECT|LPS_NERR|LPS_NOPAPER)
96 usbd_device_handle sc_udev; /* device */
97 usbd_interface_handle sc_iface; /* interface */
101 usbd_pipe_handle sc_out_pipe; /* bulk out pipe */
104 usbd_pipe_handle sc_in_pipe; /* bulk in pipe */
105 usbd_xfer_handle sc_in_xfer1;
106 usbd_xfer_handle sc_in_xfer2;
107 u_char sc_junk[64]; /* somewhere to dump input */
110 #define ULPT_OPEN 0x01 /* device is open */
111 #define ULPT_OBUSY 0x02 /* printer is busy doing output */
112 #define ULPT_INIT 0x04 /* waiting to initialize for open */
114 #define ULPT_NOPRIME 0x40 /* don't prime on open */
115 u_char sc_laststatus;
123 static d_open_t ulptopen;
124 static d_close_t ulptclose;
125 static d_write_t ulptwrite;
126 static d_ioctl_t ulptioctl;
128 #define ULPT_CDEV_MAJOR 113
130 static struct dev_ops ulpt_ops = {
131 { "ulpt", ULPT_CDEV_MAJOR, 0 },
133 .d_close = ulptclose,
134 .d_write = ulptwrite,
135 .d_ioctl = ulptioctl,
138 void ulpt_disco(void *);
140 int ulpt_do_write(struct ulpt_softc *, struct uio *uio, int);
141 int ulpt_status(struct ulpt_softc *);
142 void ulpt_reset(struct ulpt_softc *);
143 int ulpt_statusmsg(u_char, struct ulpt_softc *);
146 void ieee1284_print_id(char *);
149 #define ULPTUNIT(s) (minor(s) & 0x1f)
150 #define ULPTFLAGS(s) (minor(s) & 0xe0)
153 static device_probe_t ulpt_match;
154 static device_attach_t ulpt_attach;
155 static device_detach_t ulpt_detach;
157 static devclass_t ulpt_devclass;
159 static kobj_method_t ulpt_methods[] = {
160 DEVMETHOD(device_probe, ulpt_match),
161 DEVMETHOD(device_attach, ulpt_attach),
162 DEVMETHOD(device_detach, ulpt_detach),
166 static driver_t ulpt_driver = {
169 sizeof(struct ulpt_softc)
172 MODULE_DEPEND(ulpt, usb, 1, 1, 1);
175 ulpt_match(device_t self)
177 struct usb_attach_arg *uaa = device_get_ivars(self);
178 usb_interface_descriptor_t *id;
180 DPRINTFN(10,("ulpt_match\n"));
181 if (uaa->iface == NULL)
182 return (UMATCH_NONE);
183 id = usbd_get_interface_descriptor(uaa->iface);
185 id->bInterfaceClass == UICLASS_PRINTER &&
186 id->bInterfaceSubClass == UISUBCLASS_PRINTER &&
187 (id->bInterfaceProtocol == UIPROTO_PRINTER_UNI ||
188 id->bInterfaceProtocol == UIPROTO_PRINTER_BI ||
189 id->bInterfaceProtocol == UIPROTO_PRINTER_1284))
190 return (UMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO);
191 return (UMATCH_NONE);
195 ulpt_attach(device_t self)
197 struct ulpt_softc *sc = device_get_softc(self);
198 struct usb_attach_arg *uaa = device_get_ivars(self);
199 usbd_device_handle dev = uaa->device;
200 usbd_interface_handle iface = uaa->iface;
201 usb_interface_descriptor_t *ifcd = usbd_get_interface_descriptor(iface);
202 usb_interface_descriptor_t *id, *iend;
203 usb_config_descriptor_t *cdesc;
205 usb_endpoint_descriptor_t *ed;
209 DPRINTFN(10,("ulpt_attach: sc=%p\n", sc));
213 * Stepping through the alternate settings needs to be abstracted out.
215 cdesc = usbd_get_config_descriptor(dev);
217 kprintf("%s: failed to get configuration descriptor\n",
218 device_get_nameunit(sc->sc_dev));
221 iend = (usb_interface_descriptor_t *)
222 ((char *)cdesc + UGETW(cdesc->wTotalLength));
224 if (ifcd < (usb_interface_descriptor_t *)cdesc ||
226 panic("ulpt: iface desc out of range");
228 /* Step through all the descriptors looking for bidir mode */
229 for (id = ifcd, altno = 0;
231 id = (void *)((char *)id + id->bLength)) {
232 if (id->bDescriptorType == UDESC_INTERFACE &&
233 id->bInterfaceNumber == ifcd->bInterfaceNumber) {
234 if (id->bInterfaceClass == UICLASS_PRINTER &&
235 id->bInterfaceSubClass == UISUBCLASS_PRINTER &&
236 (id->bInterfaceProtocol == UIPROTO_PRINTER_BI /* ||
237 id->bInterfaceProtocol == UIPROTO_PRINTER_1284 */))
242 id = ifcd; /* not found, use original */
245 /* Found a new bidir setting */
246 DPRINTF(("ulpt_attach: set altno = %d\n", altno));
247 err = usbd_set_interface(iface, altno);
249 kprintf("%s: setting alternate interface failed\n",
250 device_get_nameunit(sc->sc_dev));
257 (void)usbd_endpoint_count(iface, &epcount);
261 sc->vendor = uaa->vendor;
262 sc->product = uaa->product;
263 for (i = 0; i < epcount; i++) {
264 ed = usbd_interface2endpoint_descriptor(iface, i);
266 kprintf("%s: couldn't get ep %d\n",
267 device_get_nameunit(sc->sc_dev), i);
270 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
271 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
272 sc->sc_in = ed->bEndpointAddress;
273 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
274 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
275 sc->sc_out = ed->bEndpointAddress;
278 if (sc->sc_out == -1) {
279 kprintf("%s: could not find bulk out endpoint\n",
280 device_get_nameunit(sc->sc_dev));
285 if (usbd_get_quirks(dev)->uq_flags & UQ_BROKEN_BIDIR) {
286 /* This device doesn't handle reading properly. */
290 kprintf("%s: using %s-directional mode\n", device_get_nameunit(sc->sc_dev),
291 sc->sc_in >= 0 ? "bi" : "uni");
293 DPRINTFN(10, ("ulpt_attach: bulk=%d\n", sc->sc_out));
295 sc->sc_iface = iface;
296 sc->sc_ifaceno = id->bInterfaceNumber;
299 dev_ops_add(&ulpt_ops, -1, device_get_unit(self));
300 make_dev(&ulpt_ops, device_get_unit(self),
301 UID_ROOT, GID_OPERATOR, 0644, "ulpt%d", device_get_unit(self));
303 device_get_unit(self)|ULPT_NOPRIME,
304 UID_ROOT, GID_OPERATOR, 0644, "unlpt%d", device_get_unit(self));
306 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
313 ulpt_detach(device_t self)
315 struct ulpt_softc *sc = device_get_softc(self);
317 DPRINTF(("ulpt_detach: sc=%p\n", sc));
320 if (sc->sc_out_pipe != NULL)
321 usbd_abort_pipe(sc->sc_out_pipe);
322 if (sc->sc_in_pipe != NULL)
323 usbd_abort_pipe(sc->sc_in_pipe);
326 * Wait for any ongoing operations to complete before we actually
332 if (sc->sc_refcnt >= 0) {
333 kprintf("%s: waiting for idle\n", device_get_nameunit(sc->sc_dev));
334 while (sc->sc_refcnt >= 0)
335 usb_detach_wait(sc->sc_dev);
336 kprintf("%s: idle wait done\n", device_get_nameunit(sc->sc_dev));
340 dev_ops_remove(&ulpt_ops, -1, device_get_unit(self));
342 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
349 ulpt_status(struct ulpt_softc *sc)
351 usb_device_request_t req;
355 req.bmRequestType = UT_READ_CLASS_INTERFACE;
356 req.bRequest = UR_GET_PORT_STATUS;
357 USETW(req.wValue, 0);
358 USETW(req.wIndex, sc->sc_ifaceno);
359 USETW(req.wLength, 1);
360 err = usbd_do_request(sc->sc_udev, &req, &status);
361 DPRINTFN(1, ("ulpt_status: status=0x%02x err=%d\n", status, err));
369 ulpt_reset(struct ulpt_softc *sc)
371 usb_device_request_t req;
374 /* The Brother HL1240 doesn't handle UT_WRITE_CLASS_OTHER */
375 other_fails = (sc->vendor == 0x04f9 && sc->product == 0x0006);
377 DPRINTFN(1, ("ulpt_reset\n"));
378 req.bRequest = UR_SOFT_RESET;
379 USETW(req.wValue, 0);
380 USETW(req.wIndex, sc->sc_ifaceno);
381 USETW(req.wLength, 0);
384 * There was a mistake in the USB printer 1.0 spec that gave the
385 * request type as UT_WRITE_CLASS_OTHER; it should have been
386 * UT_WRITE_CLASS_INTERFACE. Many printers use the old one,
389 req.bmRequestType = UT_WRITE_CLASS_OTHER;
390 /* Some printers don't handle UT_WRITE_CLASS_OTHER */
391 if (other_fails || usbd_do_request(sc->sc_udev, &req, 0)) {/* 1.0 */
392 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
393 (void)usbd_do_request(sc->sc_udev, &req, 0); /* 1.1 */
398 ulpt_input(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
400 struct ulpt_softc *sc = priv;
403 /* Don't loop on errors or 0-length input. */
404 usbd_get_xfer_status(xfer, NULL, NULL, &count, NULL);
405 if (status != USBD_NORMAL_COMPLETION || count == 0)
408 DPRINTFN(2,("ulpt_input: got some data\n"));
410 if (xfer == sc->sc_in_xfer1)
411 usbd_transfer(sc->sc_in_xfer2);
413 usbd_transfer(sc->sc_in_xfer1);
419 * Reset the printer, then wait until it's selected and not busy.
422 ulptopen(struct dev_open_args *ap)
424 cdev_t dev = ap->a_head.a_dev;
425 u_char flags = ULPTFLAGS(dev);
426 struct ulpt_softc *sc;
430 sc = devclass_get_softc(ulpt_devclass, ULPTUNIT(dev));
434 if (sc == NULL || sc->sc_iface == NULL || sc->sc_dying)
440 sc->sc_state = ULPT_INIT;
441 sc->sc_flags = flags;
442 DPRINTF(("ulptopen: flags=0x%x\n", (unsigned)flags));
444 #if defined(USB_DEBUG)
445 /* Ignoring these flags might not be a good idea */
446 if ((flags & ~ULPT_NOPRIME) != 0)
447 kprintf("ulptopen: flags ignored: %b\n", flags,
448 "\20\3POS_INIT\4POS_ACK\6PRIME_OPEN\7AUTOLF\10BYPASS");
454 if ((flags & ULPT_NOPRIME) == 0) {
463 for (spin = 0; (ulpt_status(sc) & LPS_SELECT) == 0; spin += STEP) {
464 DPRINTF(("ulpt_open: waiting a while\n"));
465 if (spin >= TIMEOUT) {
471 /* wait 1/4 second, give up if we get a signal */
472 error = tsleep((caddr_t)sc, PCATCH, "ulptop", STEP);
473 if (error != EWOULDBLOCK) {
485 err = usbd_open_pipe(sc->sc_iface, sc->sc_out, 0, &sc->sc_out_pipe);
492 if (ulptusein && sc->sc_in != -1) {
493 DPRINTF(("ulpt_open: open input pipe\n"));
494 err = usbd_open_pipe(sc->sc_iface, sc->sc_in,0,&sc->sc_in_pipe);
497 usbd_close_pipe(sc->sc_out_pipe);
498 sc->sc_out_pipe = NULL;
502 sc->sc_in_xfer1 = usbd_alloc_xfer(sc->sc_udev);
503 sc->sc_in_xfer2 = usbd_alloc_xfer(sc->sc_udev);
504 if (sc->sc_in_xfer1 == NULL || sc->sc_in_xfer2 == NULL) {
506 if (sc->sc_in_xfer1 != NULL) {
507 usbd_free_xfer(sc->sc_in_xfer1);
508 sc->sc_in_xfer1 = NULL;
510 if (sc->sc_in_xfer2 != NULL) {
511 usbd_free_xfer(sc->sc_in_xfer2);
512 sc->sc_in_xfer2 = NULL;
514 usbd_close_pipe(sc->sc_out_pipe);
515 sc->sc_out_pipe = NULL;
516 usbd_close_pipe(sc->sc_in_pipe);
517 sc->sc_in_pipe = NULL;
521 usbd_setup_xfer(sc->sc_in_xfer1, sc->sc_in_pipe, sc,
522 sc->sc_junk, sizeof sc->sc_junk, USBD_SHORT_XFER_OK,
523 USBD_NO_TIMEOUT, ulpt_input);
524 usbd_setup_xfer(sc->sc_in_xfer2, sc->sc_in_pipe, sc,
525 sc->sc_junk, sizeof sc->sc_junk, USBD_SHORT_XFER_OK,
526 USBD_NO_TIMEOUT, ulpt_input);
527 usbd_transfer(sc->sc_in_xfer1); /* ignore failed start */
530 sc->sc_state = ULPT_OPEN;
533 if (--sc->sc_refcnt < 0)
534 usb_detach_wakeup(sc->sc_dev);
536 DPRINTF(("ulptopen: done, error=%d\n", error));
541 ulpt_statusmsg(u_char status, struct ulpt_softc *sc)
545 status = (status ^ LPS_INVERT) & LPS_MASK;
546 new = status & ~sc->sc_laststatus;
547 sc->sc_laststatus = status;
549 if (new & LPS_SELECT)
550 log(LOG_NOTICE, "%s: offline\n", device_get_nameunit(sc->sc_dev));
551 else if (new & LPS_NOPAPER)
552 log(LOG_NOTICE, "%s: out of paper\n", device_get_nameunit(sc->sc_dev));
553 else if (new & LPS_NERR)
554 log(LOG_NOTICE, "%s: output error\n", device_get_nameunit(sc->sc_dev));
560 ulptclose(struct dev_close_args *ap)
562 cdev_t dev = ap->a_head.a_dev;
563 struct ulpt_softc *sc;
565 sc = devclass_get_softc(ulpt_devclass, ULPTUNIT(dev));
567 if (sc->sc_state != ULPT_OPEN)
568 /* We are being forced to close before the open completed. */
571 if (sc->sc_out_pipe != NULL) {
572 usbd_close_pipe(sc->sc_out_pipe);
573 sc->sc_out_pipe = NULL;
575 if (sc->sc_in_pipe != NULL) {
576 usbd_abort_pipe(sc->sc_in_pipe);
577 usbd_close_pipe(sc->sc_in_pipe);
578 sc->sc_in_pipe = NULL;
579 if (sc->sc_in_xfer1 != NULL) {
580 usbd_free_xfer(sc->sc_in_xfer1);
581 sc->sc_in_xfer1 = NULL;
583 if (sc->sc_in_xfer2 != NULL) {
584 usbd_free_xfer(sc->sc_in_xfer2);
585 sc->sc_in_xfer2 = NULL;
591 DPRINTF(("ulptclose: closed\n"));
596 ulpt_do_write(struct ulpt_softc *sc, struct uio *uio, int flags)
601 usbd_xfer_handle xfer;
604 DPRINTF(("ulptwrite\n"));
605 xfer = usbd_alloc_xfer(sc->sc_udev);
608 bufp = usbd_alloc_buffer(xfer, ULPT_BSIZE);
610 usbd_free_xfer(xfer);
613 while ((n = min(ULPT_BSIZE, uio->uio_resid)) != 0) {
614 ulpt_statusmsg(ulpt_status(sc), sc);
615 error = uiomove(bufp, n, uio);
618 DPRINTFN(1, ("ulptwrite: transfer %d bytes\n", n));
619 err = usbd_bulk_transfer(xfer, sc->sc_out_pipe, USBD_NO_COPY,
620 USBD_NO_TIMEOUT, bufp, &n, "ulptwr");
622 DPRINTF(("ulptwrite: error=%d\n", err));
627 usbd_free_xfer(xfer);
633 ulptwrite(struct dev_write_args *ap)
635 cdev_t dev = ap->a_head.a_dev;
636 struct ulpt_softc *sc;
639 sc = devclass_get_softc(ulpt_devclass, ULPTUNIT(dev));
645 error = ulpt_do_write(sc, ap->a_uio, ap->a_ioflag);
646 if (--sc->sc_refcnt < 0)
647 usb_detach_wakeup(sc->sc_dev);
652 ulptioctl(struct dev_ioctl_args *ap)
665 /* XXX This does not belong here. */
667 * Print select parts of an IEEE 1284 device ID.
670 ieee1284_print_id(char *str)
674 for (p = str-1; p; p = strchr(p, ';')) {
676 if (strncmp(p, "MFG:", 4) == 0 ||
677 strncmp(p, "MANUFACTURER:", 14) == 0 ||
678 strncmp(p, "MDL:", 4) == 0 ||
679 strncmp(p, "MODEL:", 6) == 0) {
682 kprintf("%.*s", (int)(q - p + 1), p);
688 DRIVER_MODULE(ulpt, uhub, ulpt_driver, ulpt_devclass, usbd_driver_load, 0);