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/event.h>
56 #include <sys/device.h>
57 #include <sys/syslog.h>
58 #include <sys/sysctl.h>
59 #include <sys/devfs.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
71 #define ULPT_JUNK_SIZE 512
74 #define DPRINTF(x) if (ulptdebug) kprintf x
75 #define DPRINTFN(n,x) if (ulptdebug>=(n)) kprintf 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 */
104 usbd_pipe_handle sc_out_pipe; /* bulk out pipe */
107 usbd_pipe_handle sc_in_pipe; /* bulk in pipe */
108 usbd_xfer_handle sc_in_xfer1;
109 usbd_xfer_handle sc_in_xfer2;
110 u_char *sc_junk1; /* somewhere to dump input */
111 u_char *sc_junk2; /* somewhere to dump input */
114 #define ULPT_OPEN 0x01 /* device is open */
115 #define ULPT_OBUSY 0x02 /* printer is busy doing output */
116 #define ULPT_INIT 0x04 /* waiting to initialize for open */
118 #define ULPT_NOPRIME 0x40 /* don't prime on open */
119 u_char sc_laststatus;
123 struct kqinfo sc_wkq;
128 static d_open_t ulptopen;
129 static d_close_t ulptclose;
130 static d_write_t ulptwrite;
131 static d_ioctl_t ulptioctl;
132 static d_kqfilter_t ulptkqfilter;
134 static void ulpt_filt_detach(struct knote *);
135 static int ulpt_filt(struct knote *, long);
137 static struct dev_ops ulpt_ops = {
140 .d_close = ulptclose,
141 .d_write = ulptwrite,
142 .d_ioctl = ulptioctl,
143 .d_kqfilter = ulptkqfilter
146 void ulpt_disco(void *);
148 int ulpt_do_write(struct ulpt_softc *, struct uio *uio, int);
149 int ulpt_status(struct ulpt_softc *);
150 void ulpt_reset(struct ulpt_softc *);
151 int ulpt_statusmsg(u_char, struct ulpt_softc *);
154 void ieee1284_print_id(char *);
157 #define ULPTUNIT(s) (minor(s) & 0x1f)
158 #define ULPTFLAGS(s) (minor(s) & 0xe0)
161 static device_probe_t ulpt_match;
162 static device_attach_t ulpt_attach;
163 static device_detach_t ulpt_detach;
165 static devclass_t ulpt_devclass;
167 static kobj_method_t ulpt_methods[] = {
168 DEVMETHOD(device_probe, ulpt_match),
169 DEVMETHOD(device_attach, ulpt_attach),
170 DEVMETHOD(device_detach, ulpt_detach),
174 static driver_t ulpt_driver = {
177 sizeof(struct ulpt_softc)
180 MODULE_DEPEND(ulpt, usb, 1, 1, 1);
183 ulpt_match(device_t self)
185 struct usb_attach_arg *uaa = device_get_ivars(self);
186 usb_interface_descriptor_t *id;
188 DPRINTFN(10,("ulpt_match\n"));
189 if (uaa->iface == NULL)
190 return (UMATCH_NONE);
191 id = usbd_get_interface_descriptor(uaa->iface);
193 id->bInterfaceClass == UICLASS_PRINTER &&
194 id->bInterfaceSubClass == UISUBCLASS_PRINTER &&
195 (id->bInterfaceProtocol == UIPROTO_PRINTER_UNI ||
196 id->bInterfaceProtocol == UIPROTO_PRINTER_BI ||
197 id->bInterfaceProtocol == UIPROTO_PRINTER_1284))
198 return (UMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO);
199 return (UMATCH_NONE);
203 ulpt_attach(device_t self)
205 struct ulpt_softc *sc = device_get_softc(self);
206 struct usb_attach_arg *uaa = device_get_ivars(self);
207 usbd_device_handle dev = uaa->device;
208 usbd_interface_handle iface = uaa->iface;
209 usb_interface_descriptor_t *ifcd = usbd_get_interface_descriptor(iface);
210 usb_interface_descriptor_t *id, *iend;
211 usb_config_descriptor_t *cdesc;
213 usb_endpoint_descriptor_t *ed;
217 DPRINTFN(10,("ulpt_attach: sc=%p\n", sc));
221 * Stepping through the alternate settings needs to be abstracted out.
223 cdesc = usbd_get_config_descriptor(dev);
225 kprintf("%s: failed to get configuration descriptor\n",
226 device_get_nameunit(sc->sc_dev));
229 iend = (usb_interface_descriptor_t *)
230 ((char *)cdesc + UGETW(cdesc->wTotalLength));
232 if (ifcd < (usb_interface_descriptor_t *)cdesc ||
234 panic("ulpt: iface desc out of range");
236 /* Step through all the descriptors looking for bidir mode */
237 for (id = ifcd, altno = 0;
239 id = (void *)((char *)id + id->bLength)) {
240 if (id->bDescriptorType == UDESC_INTERFACE &&
241 id->bInterfaceNumber == ifcd->bInterfaceNumber) {
242 if (id->bInterfaceClass == UICLASS_PRINTER &&
243 id->bInterfaceSubClass == UISUBCLASS_PRINTER &&
244 (id->bInterfaceProtocol == UIPROTO_PRINTER_BI /* ||
245 id->bInterfaceProtocol == UIPROTO_PRINTER_1284 */))
250 id = ifcd; /* not found, use original */
253 /* Found a new bidir setting */
254 DPRINTF(("ulpt_attach: set altno = %d\n", altno));
255 err = usbd_set_interface(iface, altno);
257 kprintf("%s: setting alternate interface failed\n",
258 device_get_nameunit(sc->sc_dev));
265 (void)usbd_endpoint_count(iface, &epcount);
269 sc->vendor = uaa->vendor;
270 sc->product = uaa->product;
271 for (i = 0; i < epcount; i++) {
272 ed = usbd_interface2endpoint_descriptor(iface, i);
274 kprintf("%s: couldn't get ep %d\n",
275 device_get_nameunit(sc->sc_dev), i);
278 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
279 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
280 sc->sc_in = ed->bEndpointAddress;
281 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
282 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
283 sc->sc_out = ed->bEndpointAddress;
286 if (sc->sc_out == -1) {
287 kprintf("%s: could not find bulk out endpoint\n",
288 device_get_nameunit(sc->sc_dev));
293 if (usbd_get_quirks(dev)->uq_flags & UQ_BROKEN_BIDIR) {
294 /* This device doesn't handle reading properly. */
298 kprintf("%s: using %s-directional mode\n", device_get_nameunit(sc->sc_dev),
299 sc->sc_in >= 0 ? "bi" : "uni");
301 DPRINTFN(10, ("ulpt_attach: bulk=%d\n", sc->sc_out));
303 sc->sc_iface = iface;
304 sc->sc_ifaceno = id->bInterfaceNumber;
309 sc->sc_cdev1 = make_dev(&ulpt_ops, device_get_unit(self),
310 UID_ROOT, GID_OPERATOR, 0644,
311 "ulpt%d", device_get_unit(self));
312 sc->sc_cdev2 = make_dev(&ulpt_ops, device_get_unit(self)|ULPT_NOPRIME,
313 UID_ROOT, GID_OPERATOR, 0644,
314 "unlpt%d", device_get_unit(self));
316 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
323 ulpt_detach(device_t self)
325 struct ulpt_softc *sc = device_get_softc(self);
327 DPRINTF(("ulpt_detach: sc=%p\n", sc));
330 if (sc->sc_out_pipe != NULL)
331 usbd_abort_pipe(sc->sc_out_pipe);
332 if (sc->sc_in_pipe != NULL)
333 usbd_abort_pipe(sc->sc_in_pipe);
336 * Wait for any ongoing operations to complete before we actually
342 if (sc->sc_refcnt >= 0) {
343 kprintf("%s: waiting for idle\n", device_get_nameunit(sc->sc_dev));
344 while (sc->sc_refcnt >= 0)
345 usb_detach_wait(sc->sc_dev);
346 kprintf("%s: idle wait done\n", device_get_nameunit(sc->sc_dev));
350 dev_ops_remove_minor(&ulpt_ops, /*-1, */device_get_unit(self));
351 devfs_assume_knotes(sc->sc_cdev1, &sc->sc_wkq);
352 devfs_assume_knotes(sc->sc_cdev2, &sc->sc_wkq);
354 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, sc->sc_dev);
360 ulpt_status(struct ulpt_softc *sc)
362 usb_device_request_t req;
366 req.bmRequestType = UT_READ_CLASS_INTERFACE;
367 req.bRequest = UR_GET_PORT_STATUS;
368 USETW(req.wValue, 0);
369 USETW(req.wIndex, sc->sc_ifaceno);
370 USETW(req.wLength, 1);
371 err = usbd_do_request(sc->sc_udev, &req, &status);
372 DPRINTFN(1, ("ulpt_status: status=0x%02x err=%d\n", status, err));
380 ulpt_reset(struct ulpt_softc *sc)
382 usb_device_request_t req;
385 /* The Brother HL1240 doesn't handle UT_WRITE_CLASS_OTHER */
386 other_fails = (sc->vendor == 0x04f9 && sc->product == 0x0006);
388 DPRINTFN(1, ("ulpt_reset\n"));
389 req.bRequest = UR_SOFT_RESET;
390 USETW(req.wValue, 0);
391 USETW(req.wIndex, sc->sc_ifaceno);
392 USETW(req.wLength, 0);
395 * There was a mistake in the USB printer 1.0 spec that gave the
396 * request type as UT_WRITE_CLASS_OTHER; it should have been
397 * UT_WRITE_CLASS_INTERFACE. Many printers use the old one,
400 req.bmRequestType = UT_WRITE_CLASS_OTHER;
401 /* Some printers don't handle UT_WRITE_CLASS_OTHER */
402 if (other_fails || usbd_do_request(sc->sc_udev, &req, 0)) {/* 1.0 */
403 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
404 (void)usbd_do_request(sc->sc_udev, &req, 0); /* 1.1 */
409 ulpt_input(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
411 struct ulpt_softc *sc = priv;
414 /* Don't loop on errors or 0-length input. */
415 usbd_get_xfer_status(xfer, NULL, NULL, &count, NULL);
416 if (status != USBD_NORMAL_COMPLETION || count == 0)
419 DPRINTFN(2,("ulpt_input: got some data\n"));
421 if (xfer == sc->sc_in_xfer1)
422 usbd_transfer(sc->sc_in_xfer2);
424 usbd_transfer(sc->sc_in_xfer1);
430 * Reset the printer, then wait until it's selected and not busy.
433 ulptopen(struct dev_open_args *ap)
435 cdev_t dev = ap->a_head.a_dev;
436 u_char flags = ULPTFLAGS(dev);
437 struct ulpt_softc *sc;
441 sc = devclass_get_softc(ulpt_devclass, ULPTUNIT(dev));
445 if (sc == NULL || sc->sc_iface == NULL || sc->sc_dying)
451 sc->sc_state = ULPT_INIT;
452 sc->sc_flags = flags;
453 DPRINTF(("ulptopen: flags=0x%x\n", (unsigned)flags));
455 #if defined(USB_DEBUG)
456 /* Ignoring these flags might not be a good idea */
457 if ((flags & ~ULPT_NOPRIME) != 0)
458 kprintf("ulptopen: flags ignored: %b\n", flags,
459 "\20\3POS_INIT\4POS_ACK\6PRIME_OPEN\7AUTOLF\10BYPASS");
465 if ((flags & ULPT_NOPRIME) == 0) {
474 for (spin = 0; (ulpt_status(sc) & LPS_SELECT) == 0; spin += STEP) {
475 DPRINTF(("ulpt_open: waiting a while\n"));
476 if (spin >= TIMEOUT) {
482 /* wait 1/4 second, give up if we get a signal */
483 error = tsleep((caddr_t)sc, PCATCH, "ulptop", STEP);
484 if (error != EWOULDBLOCK) {
496 err = usbd_open_pipe(sc->sc_iface, sc->sc_out, 0, &sc->sc_out_pipe);
503 if (ulptusein && sc->sc_in != -1) {
504 DPRINTF(("ulpt_open: open input pipe\n"));
505 err = usbd_open_pipe(sc->sc_iface, sc->sc_in,0,&sc->sc_in_pipe);
508 usbd_close_pipe(sc->sc_out_pipe);
509 sc->sc_out_pipe = NULL;
513 sc->sc_in_xfer1 = usbd_alloc_xfer(sc->sc_udev);
514 sc->sc_in_xfer2 = usbd_alloc_xfer(sc->sc_udev);
515 if (sc->sc_in_xfer1 == NULL || sc->sc_in_xfer2 == NULL) {
517 if (sc->sc_in_xfer1 != NULL) {
518 usbd_free_xfer(sc->sc_in_xfer1);
519 sc->sc_in_xfer1 = NULL;
521 if (sc->sc_in_xfer2 != NULL) {
522 usbd_free_xfer(sc->sc_in_xfer2);
523 sc->sc_in_xfer2 = NULL;
525 usbd_close_pipe(sc->sc_out_pipe);
526 sc->sc_out_pipe = NULL;
527 usbd_close_pipe(sc->sc_in_pipe);
528 sc->sc_in_pipe = NULL;
532 sc->sc_junk1 = usbd_alloc_buffer(sc->sc_in_xfer1,
534 sc->sc_junk2 = usbd_alloc_buffer(sc->sc_in_xfer2,
536 usbd_setup_xfer(sc->sc_in_xfer1, sc->sc_in_pipe, sc,
537 sc->sc_junk1, ULPT_JUNK_SIZE, USBD_SHORT_XFER_OK,
538 USBD_NO_TIMEOUT, ulpt_input);
539 usbd_setup_xfer(sc->sc_in_xfer2, sc->sc_in_pipe, sc,
540 sc->sc_junk2, ULPT_JUNK_SIZE, USBD_SHORT_XFER_OK,
541 USBD_NO_TIMEOUT, ulpt_input);
542 usbd_transfer(sc->sc_in_xfer1); /* ignore failed start */
545 sc->sc_state = ULPT_OPEN;
548 if (--sc->sc_refcnt < 0)
549 usb_detach_wakeup(sc->sc_dev);
551 DPRINTF(("ulptopen: done, error=%d\n", error));
556 ulpt_statusmsg(u_char status, struct ulpt_softc *sc)
560 status = (status ^ LPS_INVERT) & LPS_MASK;
561 new = status & ~sc->sc_laststatus;
562 sc->sc_laststatus = status;
564 if (new & LPS_SELECT)
565 log(LOG_NOTICE, "%s: offline\n", device_get_nameunit(sc->sc_dev));
566 else if (new & LPS_NOPAPER)
567 log(LOG_NOTICE, "%s: out of paper\n", device_get_nameunit(sc->sc_dev));
568 else if (new & LPS_NERR)
569 log(LOG_NOTICE, "%s: output error\n", device_get_nameunit(sc->sc_dev));
575 ulptclose(struct dev_close_args *ap)
577 cdev_t dev = ap->a_head.a_dev;
578 struct ulpt_softc *sc;
580 sc = devclass_get_softc(ulpt_devclass, ULPTUNIT(dev));
582 if (sc->sc_state != ULPT_OPEN)
583 /* We are being forced to close before the open completed. */
586 if (sc->sc_out_pipe != NULL) {
587 usbd_close_pipe(sc->sc_out_pipe);
588 sc->sc_out_pipe = NULL;
590 if (sc->sc_in_pipe != NULL) {
591 usbd_abort_pipe(sc->sc_in_pipe);
592 usbd_close_pipe(sc->sc_in_pipe);
593 sc->sc_in_pipe = NULL;
594 if (sc->sc_in_xfer1 != NULL) {
595 usbd_free_xfer(sc->sc_in_xfer1);
596 sc->sc_in_xfer1 = NULL;
598 if (sc->sc_in_xfer2 != NULL) {
599 usbd_free_xfer(sc->sc_in_xfer2);
600 sc->sc_in_xfer2 = NULL;
606 DPRINTF(("ulptclose: closed\n"));
611 ulpt_do_write(struct ulpt_softc *sc, struct uio *uio, int flags)
616 usbd_xfer_handle xfer;
619 DPRINTF(("ulptwrite\n"));
620 xfer = usbd_alloc_xfer(sc->sc_udev);
623 bufp = usbd_alloc_buffer(xfer, ULPT_BSIZE);
625 usbd_free_xfer(xfer);
628 while ((n = szmin(ULPT_BSIZE, uio->uio_resid)) != 0) {
629 ulpt_statusmsg(ulpt_status(sc), sc);
630 error = uiomove(bufp, n, uio);
632 DPRINTF(("ulpt_do_write: uiomove error = %d\n", error));
635 DPRINTFN(1, ("ulptwrite: transfer %d bytes\n", n));
636 err = usbd_bulk_transfer(xfer, sc->sc_out_pipe, USBD_NO_COPY,
637 USBD_NO_TIMEOUT, bufp, &n, "ulptwr");
639 DPRINTF(("ulptwrite: error=%d\n", err));
644 usbd_free_xfer(xfer);
650 ulptwrite(struct dev_write_args *ap)
652 cdev_t dev = ap->a_head.a_dev;
653 struct ulpt_softc *sc;
656 sc = devclass_get_softc(ulpt_devclass, ULPTUNIT(dev));
658 if (sc == NULL || sc->sc_dying)
662 error = ulpt_do_write(sc, ap->a_uio, ap->a_ioflag);
663 DPRINTF(("ulpt_write: error = %d\n", error));
664 if (--sc->sc_refcnt < 0)
665 usb_detach_wakeup(sc->sc_dev);
670 ulptioctl(struct dev_ioctl_args *ap)
682 static struct filterops ulpt_filtops =
683 { FILTEROP_ISFD, NULL, ulpt_filt_detach, ulpt_filt };
686 ulptkqfilter(struct dev_kqfilter_args *ap)
688 cdev_t dev = ap->a_head.a_dev;
689 struct knote *kn = ap->a_kn;
690 struct ulpt_softc *sc;
695 switch(kn->kn_filter) {
697 sc = devclass_get_softc(ulpt_devclass, ULPTUNIT(dev));
698 kn->kn_fop = &ulpt_filtops;
699 kn->kn_hook = (caddr_t)sc;
702 ap->a_result = EOPNOTSUPP;
706 klist = &sc->sc_wkq.ki_note;
707 knote_insert(klist, kn);
713 ulpt_filt_detach(struct knote *kn)
715 struct ulpt_softc *sc = (struct ulpt_softc *)kn->kn_hook;
718 klist = &sc->sc_wkq.ki_note;
719 knote_remove(klist, kn);
723 ulpt_filt(struct knote *kn, long hint)
725 /* struct ulpt_softc *sc = (struct ulpt_softc *)kn->kn_hook;*/
731 /* XXX This does not belong here. */
733 * Print select parts of an IEEE 1284 device ID.
736 ieee1284_print_id(char *str)
740 for (p = str-1; p; p = strchr(p, ';')) {
742 if (strncmp(p, "MFG:", 4) == 0 ||
743 strncmp(p, "MANUFACTURER:", 14) == 0 ||
744 strncmp(p, "MDL:", 4) == 0 ||
745 strncmp(p, "MODEL:", 6) == 0) {
748 kprintf("%.*s", (int)(q - p + 1), p);
754 DRIVER_MODULE(ulpt, uhub, ulpt_driver, ulpt_devclass, usbd_driver_load, NULL);