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 $
7 * Copyright (c) 1998 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
47 #include <sys/param.h>
48 #include <sys/systm.h>
50 #include <sys/kernel.h>
51 #include <sys/module.h>
55 #include <sys/device.h>
56 #include <sys/syslog.h>
57 #include <sys/sysctl.h>
58 #include <sys/thread2.h>
60 #include <bus/usb/usb.h>
61 #include <bus/usb/usbdi.h>
62 #include <bus/usb/usbdi_util.h>
63 #include <bus/usb/usb_quirks.h>
65 #define TIMEOUT hz*16 /* wait up to 16 seconds for a ready */
68 #define ULPT_BSIZE 16384
71 #define DPRINTF(x) if (ulptdebug) kprintf x
72 #define DPRINTFN(n,x) if (ulptdebug>(n)) kprintf x
74 SYSCTL_NODE(_hw_usb, OID_AUTO, ulpt, CTLFLAG_RW, 0, "USB ulpt");
75 SYSCTL_INT(_hw_usb_ulpt, OID_AUTO, debug, CTLFLAG_RW,
76 &ulptdebug, 0, "ulpt debug level");
82 #define UR_GET_DEVICE_ID 0
83 #define UR_GET_PORT_STATUS 1
84 #define UR_SOFT_RESET 2
86 #define LPS_NERR 0x08 /* printer no error */
87 #define LPS_SELECT 0x10 /* printer selected */
88 #define LPS_NOPAPER 0x20 /* printer out of paper */
89 #define LPS_INVERT (LPS_SELECT|LPS_NERR)
90 #define LPS_MASK (LPS_SELECT|LPS_NERR|LPS_NOPAPER)
94 usbd_device_handle sc_udev; /* device */
95 usbd_interface_handle sc_iface; /* interface */
99 usbd_pipe_handle sc_out_pipe; /* bulk out pipe */
102 usbd_pipe_handle sc_in_pipe; /* bulk in pipe */
103 usbd_xfer_handle sc_in_xfer1;
104 usbd_xfer_handle sc_in_xfer2;
105 u_char sc_junk[64]; /* somewhere to dump input */
108 #define ULPT_OPEN 0x01 /* device is open */
109 #define ULPT_OBUSY 0x02 /* printer is busy doing output */
110 #define ULPT_INIT 0x04 /* waiting to initialize for open */
112 #define ULPT_NOPRIME 0x40 /* don't prime on open */
113 u_char sc_laststatus;
121 static d_open_t ulptopen;
122 static d_close_t ulptclose;
123 static d_write_t ulptwrite;
124 static d_ioctl_t ulptioctl;
126 static struct dev_ops ulpt_ops = {
129 .d_close = ulptclose,
130 .d_write = ulptwrite,
131 .d_ioctl = ulptioctl,
134 void ulpt_disco(void *);
136 int ulpt_do_write(struct ulpt_softc *, struct uio *uio, int);
137 int ulpt_status(struct ulpt_softc *);
138 void ulpt_reset(struct ulpt_softc *);
139 int ulpt_statusmsg(u_char, struct ulpt_softc *);
142 void ieee1284_print_id(char *);
145 #define ULPTUNIT(s) (minor(s) & 0x1f)
146 #define ULPTFLAGS(s) (minor(s) & 0xe0)
149 static device_probe_t ulpt_match;
150 static device_attach_t ulpt_attach;
151 static device_detach_t ulpt_detach;
153 static devclass_t ulpt_devclass;
155 static kobj_method_t ulpt_methods[] = {
156 DEVMETHOD(device_probe, ulpt_match),
157 DEVMETHOD(device_attach, ulpt_attach),
158 DEVMETHOD(device_detach, ulpt_detach),
162 static driver_t ulpt_driver = {
165 sizeof(struct ulpt_softc)
168 MODULE_DEPEND(ulpt, usb, 1, 1, 1);
171 ulpt_match(device_t self)
173 struct usb_attach_arg *uaa = device_get_ivars(self);
174 usb_interface_descriptor_t *id;
176 DPRINTFN(10,("ulpt_match\n"));
177 if (uaa->iface == NULL)
178 return (UMATCH_NONE);
179 id = usbd_get_interface_descriptor(uaa->iface);
181 id->bInterfaceClass == UICLASS_PRINTER &&
182 id->bInterfaceSubClass == UISUBCLASS_PRINTER &&
183 (id->bInterfaceProtocol == UIPROTO_PRINTER_UNI ||
184 id->bInterfaceProtocol == UIPROTO_PRINTER_BI ||
185 id->bInterfaceProtocol == UIPROTO_PRINTER_1284))
186 return (UMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO);
187 return (UMATCH_NONE);
191 ulpt_attach(device_t self)
193 struct ulpt_softc *sc = device_get_softc(self);
194 struct usb_attach_arg *uaa = device_get_ivars(self);
195 usbd_device_handle dev = uaa->device;
196 usbd_interface_handle iface = uaa->iface;
197 usb_interface_descriptor_t *ifcd = usbd_get_interface_descriptor(iface);
198 usb_interface_descriptor_t *id, *iend;
199 usb_config_descriptor_t *cdesc;
201 usb_endpoint_descriptor_t *ed;
205 DPRINTFN(10,("ulpt_attach: sc=%p\n", sc));
209 * Stepping through the alternate settings needs to be abstracted out.
211 cdesc = usbd_get_config_descriptor(dev);
213 kprintf("%s: failed to get configuration descriptor\n",
214 device_get_nameunit(sc->sc_dev));
217 iend = (usb_interface_descriptor_t *)
218 ((char *)cdesc + UGETW(cdesc->wTotalLength));
220 if (ifcd < (usb_interface_descriptor_t *)cdesc ||
222 panic("ulpt: iface desc out of range");
224 /* Step through all the descriptors looking for bidir mode */
225 for (id = ifcd, altno = 0;
227 id = (void *)((char *)id + id->bLength)) {
228 if (id->bDescriptorType == UDESC_INTERFACE &&
229 id->bInterfaceNumber == ifcd->bInterfaceNumber) {
230 if (id->bInterfaceClass == UICLASS_PRINTER &&
231 id->bInterfaceSubClass == UISUBCLASS_PRINTER &&
232 (id->bInterfaceProtocol == UIPROTO_PRINTER_BI /* ||
233 id->bInterfaceProtocol == UIPROTO_PRINTER_1284 */))
238 id = ifcd; /* not found, use original */
241 /* Found a new bidir setting */
242 DPRINTF(("ulpt_attach: set altno = %d\n", altno));
243 err = usbd_set_interface(iface, altno);
245 kprintf("%s: setting alternate interface failed\n",
246 device_get_nameunit(sc->sc_dev));
253 (void)usbd_endpoint_count(iface, &epcount);
257 sc->vendor = uaa->vendor;
258 sc->product = uaa->product;
259 for (i = 0; i < epcount; i++) {
260 ed = usbd_interface2endpoint_descriptor(iface, i);
262 kprintf("%s: couldn't get ep %d\n",
263 device_get_nameunit(sc->sc_dev), i);
266 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
267 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
268 sc->sc_in = ed->bEndpointAddress;
269 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
270 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
271 sc->sc_out = ed->bEndpointAddress;
274 if (sc->sc_out == -1) {
275 kprintf("%s: could not find bulk out endpoint\n",
276 device_get_nameunit(sc->sc_dev));
281 if (usbd_get_quirks(dev)->uq_flags & UQ_BROKEN_BIDIR) {
282 /* This device doesn't handle reading properly. */
286 kprintf("%s: using %s-directional mode\n", device_get_nameunit(sc->sc_dev),
287 sc->sc_in >= 0 ? "bi" : "uni");
289 DPRINTFN(10, ("ulpt_attach: bulk=%d\n", sc->sc_out));
291 sc->sc_iface = iface;
292 sc->sc_ifaceno = id->bInterfaceNumber;
295 make_dev(&ulpt_ops, device_get_unit(self),
296 UID_ROOT, GID_OPERATOR, 0644,
297 "ulpt%d", device_get_unit(self));
299 device_get_unit(self)|ULPT_NOPRIME,
300 UID_ROOT, GID_OPERATOR, 0644,
301 "unlpt%d", device_get_unit(self));
303 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
310 ulpt_detach(device_t self)
312 struct ulpt_softc *sc = device_get_softc(self);
314 DPRINTF(("ulpt_detach: sc=%p\n", sc));
317 if (sc->sc_out_pipe != NULL)
318 usbd_abort_pipe(sc->sc_out_pipe);
319 if (sc->sc_in_pipe != NULL)
320 usbd_abort_pipe(sc->sc_in_pipe);
323 * Wait for any ongoing operations to complete before we actually
329 if (sc->sc_refcnt >= 0) {
330 kprintf("%s: waiting for idle\n", device_get_nameunit(sc->sc_dev));
331 while (sc->sc_refcnt >= 0)
332 usb_detach_wait(sc->sc_dev);
333 kprintf("%s: idle wait done\n", device_get_nameunit(sc->sc_dev));
337 dev_ops_remove_minor(&ulpt_ops, /*-1, */device_get_unit(self));
339 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
346 ulpt_status(struct ulpt_softc *sc)
348 usb_device_request_t req;
352 req.bmRequestType = UT_READ_CLASS_INTERFACE;
353 req.bRequest = UR_GET_PORT_STATUS;
354 USETW(req.wValue, 0);
355 USETW(req.wIndex, sc->sc_ifaceno);
356 USETW(req.wLength, 1);
357 err = usbd_do_request(sc->sc_udev, &req, &status);
358 DPRINTFN(1, ("ulpt_status: status=0x%02x err=%d\n", status, err));
366 ulpt_reset(struct ulpt_softc *sc)
368 usb_device_request_t req;
371 /* The Brother HL1240 doesn't handle UT_WRITE_CLASS_OTHER */
372 other_fails = (sc->vendor == 0x04f9 && sc->product == 0x0006);
374 DPRINTFN(1, ("ulpt_reset\n"));
375 req.bRequest = UR_SOFT_RESET;
376 USETW(req.wValue, 0);
377 USETW(req.wIndex, sc->sc_ifaceno);
378 USETW(req.wLength, 0);
381 * There was a mistake in the USB printer 1.0 spec that gave the
382 * request type as UT_WRITE_CLASS_OTHER; it should have been
383 * UT_WRITE_CLASS_INTERFACE. Many printers use the old one,
386 req.bmRequestType = UT_WRITE_CLASS_OTHER;
387 /* Some printers don't handle UT_WRITE_CLASS_OTHER */
388 if (other_fails || usbd_do_request(sc->sc_udev, &req, 0)) {/* 1.0 */
389 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
390 (void)usbd_do_request(sc->sc_udev, &req, 0); /* 1.1 */
395 ulpt_input(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
397 struct ulpt_softc *sc = priv;
400 /* Don't loop on errors or 0-length input. */
401 usbd_get_xfer_status(xfer, NULL, NULL, &count, NULL);
402 if (status != USBD_NORMAL_COMPLETION || count == 0)
405 DPRINTFN(2,("ulpt_input: got some data\n"));
407 if (xfer == sc->sc_in_xfer1)
408 usbd_transfer(sc->sc_in_xfer2);
410 usbd_transfer(sc->sc_in_xfer1);
416 * Reset the printer, then wait until it's selected and not busy.
419 ulptopen(struct dev_open_args *ap)
421 cdev_t dev = ap->a_head.a_dev;
422 u_char flags = ULPTFLAGS(dev);
423 struct ulpt_softc *sc;
427 sc = devclass_get_softc(ulpt_devclass, ULPTUNIT(dev));
431 if (sc == NULL || sc->sc_iface == NULL || sc->sc_dying)
437 sc->sc_state = ULPT_INIT;
438 sc->sc_flags = flags;
439 DPRINTF(("ulptopen: flags=0x%x\n", (unsigned)flags));
441 #if defined(USB_DEBUG)
442 /* Ignoring these flags might not be a good idea */
443 if ((flags & ~ULPT_NOPRIME) != 0)
444 kprintf("ulptopen: flags ignored: %b\n", flags,
445 "\20\3POS_INIT\4POS_ACK\6PRIME_OPEN\7AUTOLF\10BYPASS");
451 if ((flags & ULPT_NOPRIME) == 0) {
460 for (spin = 0; (ulpt_status(sc) & LPS_SELECT) == 0; spin += STEP) {
461 DPRINTF(("ulpt_open: waiting a while\n"));
462 if (spin >= TIMEOUT) {
468 /* wait 1/4 second, give up if we get a signal */
469 error = tsleep((caddr_t)sc, PCATCH, "ulptop", STEP);
470 if (error != EWOULDBLOCK) {
482 err = usbd_open_pipe(sc->sc_iface, sc->sc_out, 0, &sc->sc_out_pipe);
489 if (ulptusein && sc->sc_in != -1) {
490 DPRINTF(("ulpt_open: open input pipe\n"));
491 err = usbd_open_pipe(sc->sc_iface, sc->sc_in,0,&sc->sc_in_pipe);
494 usbd_close_pipe(sc->sc_out_pipe);
495 sc->sc_out_pipe = NULL;
499 sc->sc_in_xfer1 = usbd_alloc_xfer(sc->sc_udev);
500 sc->sc_in_xfer2 = usbd_alloc_xfer(sc->sc_udev);
501 if (sc->sc_in_xfer1 == NULL || sc->sc_in_xfer2 == NULL) {
503 if (sc->sc_in_xfer1 != NULL) {
504 usbd_free_xfer(sc->sc_in_xfer1);
505 sc->sc_in_xfer1 = NULL;
507 if (sc->sc_in_xfer2 != NULL) {
508 usbd_free_xfer(sc->sc_in_xfer2);
509 sc->sc_in_xfer2 = NULL;
511 usbd_close_pipe(sc->sc_out_pipe);
512 sc->sc_out_pipe = NULL;
513 usbd_close_pipe(sc->sc_in_pipe);
514 sc->sc_in_pipe = NULL;
518 usbd_setup_xfer(sc->sc_in_xfer1, sc->sc_in_pipe, sc,
519 sc->sc_junk, sizeof sc->sc_junk, USBD_SHORT_XFER_OK,
520 USBD_NO_TIMEOUT, ulpt_input);
521 usbd_setup_xfer(sc->sc_in_xfer2, 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_transfer(sc->sc_in_xfer1); /* ignore failed start */
527 sc->sc_state = ULPT_OPEN;
530 if (--sc->sc_refcnt < 0)
531 usb_detach_wakeup(sc->sc_dev);
533 DPRINTF(("ulptopen: done, error=%d\n", error));
538 ulpt_statusmsg(u_char status, struct ulpt_softc *sc)
542 status = (status ^ LPS_INVERT) & LPS_MASK;
543 new = status & ~sc->sc_laststatus;
544 sc->sc_laststatus = status;
546 if (new & LPS_SELECT)
547 log(LOG_NOTICE, "%s: offline\n", device_get_nameunit(sc->sc_dev));
548 else if (new & LPS_NOPAPER)
549 log(LOG_NOTICE, "%s: out of paper\n", device_get_nameunit(sc->sc_dev));
550 else if (new & LPS_NERR)
551 log(LOG_NOTICE, "%s: output error\n", device_get_nameunit(sc->sc_dev));
557 ulptclose(struct dev_close_args *ap)
559 cdev_t dev = ap->a_head.a_dev;
560 struct ulpt_softc *sc;
562 sc = devclass_get_softc(ulpt_devclass, ULPTUNIT(dev));
564 if (sc->sc_state != ULPT_OPEN)
565 /* We are being forced to close before the open completed. */
568 if (sc->sc_out_pipe != NULL) {
569 usbd_close_pipe(sc->sc_out_pipe);
570 sc->sc_out_pipe = NULL;
572 if (sc->sc_in_pipe != NULL) {
573 usbd_abort_pipe(sc->sc_in_pipe);
574 usbd_close_pipe(sc->sc_in_pipe);
575 sc->sc_in_pipe = NULL;
576 if (sc->sc_in_xfer1 != NULL) {
577 usbd_free_xfer(sc->sc_in_xfer1);
578 sc->sc_in_xfer1 = NULL;
580 if (sc->sc_in_xfer2 != NULL) {
581 usbd_free_xfer(sc->sc_in_xfer2);
582 sc->sc_in_xfer2 = NULL;
588 DPRINTF(("ulptclose: closed\n"));
593 ulpt_do_write(struct ulpt_softc *sc, struct uio *uio, int flags)
598 usbd_xfer_handle xfer;
601 DPRINTF(("ulptwrite\n"));
602 xfer = usbd_alloc_xfer(sc->sc_udev);
605 bufp = usbd_alloc_buffer(xfer, ULPT_BSIZE);
607 usbd_free_xfer(xfer);
610 while ((n = szmin(ULPT_BSIZE, uio->uio_resid)) != 0) {
611 ulpt_statusmsg(ulpt_status(sc), sc);
612 error = uiomove(bufp, n, uio);
615 DPRINTFN(1, ("ulptwrite: transfer %d bytes\n", n));
616 err = usbd_bulk_transfer(xfer, sc->sc_out_pipe, USBD_NO_COPY,
617 USBD_NO_TIMEOUT, bufp, &n, "ulptwr");
619 DPRINTF(("ulptwrite: error=%d\n", err));
624 usbd_free_xfer(xfer);
630 ulptwrite(struct dev_write_args *ap)
632 cdev_t dev = ap->a_head.a_dev;
633 struct ulpt_softc *sc;
636 sc = devclass_get_softc(ulpt_devclass, ULPTUNIT(dev));
642 error = ulpt_do_write(sc, ap->a_uio, ap->a_ioflag);
643 if (--sc->sc_refcnt < 0)
644 usb_detach_wakeup(sc->sc_dev);
649 ulptioctl(struct dev_ioctl_args *ap)
662 /* XXX This does not belong here. */
664 * Print select parts of an IEEE 1284 device ID.
667 ieee1284_print_id(char *str)
671 for (p = str-1; p; p = strchr(p, ';')) {
673 if (strncmp(p, "MFG:", 4) == 0 ||
674 strncmp(p, "MANUFACTURER:", 14) == 0 ||
675 strncmp(p, "MDL:", 4) == 0 ||
676 strncmp(p, "MODEL:", 6) == 0) {
679 kprintf("%.*s", (int)(q - p + 1), p);
685 DRIVER_MODULE(ulpt, uhub, ulpt_driver, ulpt_devclass, usbd_driver_load, 0);