1 /* $NetBSD: ulpt.c,v 1.51 2002/08/15 09:32:50 augustss Exp $ */
5 * Copyright (c) 1998 The NetBSD Foundation, Inc.
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Lennart Augustsson (lennart@augustsson.net) at
10 * Carlstedt Research & Technology.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the NetBSD
23 * Foundation, Inc. and its contributors.
24 * 4. Neither the name of The NetBSD Foundation nor the names of its
25 * contributors may be used to endorse or promote products derived
26 * from this software without specific prior written permission.
28 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
29 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
30 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
31 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
32 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38 * POSSIBILITY OF SUCH DAMAGE.
42 * Printer Class spec: http://www.usb.org/developers/data/devclass/usbprint109.PDF
45 #include <sys/param.h>
46 #include <sys/systm.h>
48 #include <sys/kernel.h>
49 #if defined(__NetBSD__) || defined(__OpenBSD__)
50 #include <sys/device.h>
51 #include <sys/ioctl.h>
52 #elif defined(__FreeBSD__)
53 #include <sys/ioccom.h>
54 #include <sys/module.h>
59 #include <sys/vnode.h>
60 #include <sys/syslog.h>
61 #include <sys/sysctl.h>
63 #include <dev/usb/usb.h>
64 #include <dev/usb/usbdi.h>
65 #include <dev/usb/usbdi_util.h>
66 #include <dev/usb/usbdevs.h>
67 #include <dev/usb/usb_quirks.h>
69 #define TIMEOUT hz*16 /* wait up to 16 seconds for a ready */
72 #define LPTPRI (PZERO+8)
73 #define ULPT_BSIZE 16384
76 #define DPRINTF(x) if (ulptdebug) logprintf x
77 #define DPRINTFN(n,x) if (ulptdebug>(n)) logprintf x
79 SYSCTL_NODE(_hw_usb, OID_AUTO, ulpt, CTLFLAG_RW, 0, "USB ulpt");
80 SYSCTL_INT(_hw_usb_ulpt, OID_AUTO, debug, CTLFLAG_RW,
81 &ulptdebug, 0, "ulpt debug level");
87 #define UR_GET_DEVICE_ID 0
88 #define UR_GET_PORT_STATUS 1
89 #define UR_SOFT_RESET 2
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)
99 usbd_device_handle sc_udev; /* device */
100 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_junk[64]; /* somewhere to dump input */
113 #define ULPT_OPEN 0x01 /* device is open */
114 #define ULPT_OBUSY 0x02 /* printer is busy doing output */
115 #define ULPT_INIT 0x04 /* waiting to initialize for open */
117 #define ULPT_NOPRIME 0x40 /* don't prime on open */
118 u_char sc_laststatus;
123 #if defined(__FreeBSD__)
129 #if defined(__NetBSD__) || defined(__OpenBSD__)
131 #elif defined(__FreeBSD__)
132 Static d_open_t ulptopen;
133 Static d_close_t ulptclose;
134 Static d_write_t ulptwrite;
135 Static d_ioctl_t ulptioctl;
137 #define ULPT_CDEV_MAJOR 113
139 Static struct cdevsw ulpt_cdevsw = {
141 /* close */ ulptclose,
143 /* write */ ulptwrite,
144 /* ioctl */ ulptioctl,
147 /* strategy */ nostrategy,
149 /* maj */ ULPT_CDEV_MAJOR,
153 #if __FreeBSD_version < 500014
159 void ulpt_disco(void *);
161 int ulpt_do_write(struct ulpt_softc *, struct uio *uio, int);
162 int ulpt_status(struct ulpt_softc *);
163 void ulpt_reset(struct ulpt_softc *);
164 int ulpt_statusmsg(u_char, struct ulpt_softc *);
167 void ieee1284_print_id(char *);
170 #define ULPTUNIT(s) (minor(s) & 0x1f)
171 #define ULPTFLAGS(s) (minor(s) & 0xe0)
174 USB_DECLARE_DRIVER(ulpt);
178 USB_MATCH_START(ulpt, uaa);
179 usb_interface_descriptor_t *id;
181 DPRINTFN(10,("ulpt_match\n"));
182 if (uaa->iface == NULL)
183 return (UMATCH_NONE);
184 id = usbd_get_interface_descriptor(uaa->iface);
186 id->bInterfaceClass == UICLASS_PRINTER &&
187 id->bInterfaceSubClass == UISUBCLASS_PRINTER &&
188 (id->bInterfaceProtocol == UIPROTO_PRINTER_UNI ||
189 id->bInterfaceProtocol == UIPROTO_PRINTER_BI ||
190 id->bInterfaceProtocol == UIPROTO_PRINTER_1284))
191 return (UMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO);
192 return (UMATCH_NONE);
197 USB_ATTACH_START(ulpt, sc, uaa);
198 usbd_device_handle dev = uaa->device;
199 usbd_interface_handle iface = uaa->iface;
200 usb_interface_descriptor_t *ifcd = usbd_get_interface_descriptor(iface);
201 usb_interface_descriptor_t *id, *iend;
202 usb_config_descriptor_t *cdesc;
205 usb_endpoint_descriptor_t *ed;
209 DPRINTFN(10,("ulpt_attach: sc=%p\n", sc));
210 usbd_devinfo(dev, 0, devinfo);
212 printf("%s: %s, iclass %d/%d\n", USBDEVNAME(sc->sc_dev),
213 devinfo, ifcd->bInterfaceClass, ifcd->bInterfaceSubClass);
216 * Stepping through the alternate settings needs to be abstracted out.
218 cdesc = usbd_get_config_descriptor(dev);
220 printf("%s: failed to get configuration descriptor\n",
221 USBDEVNAME(sc->sc_dev));
222 USB_ATTACH_ERROR_RETURN;
224 iend = (usb_interface_descriptor_t *)
225 ((char *)cdesc + UGETW(cdesc->wTotalLength));
227 if (ifcd < (usb_interface_descriptor_t *)cdesc ||
229 panic("ulpt: iface desc out of range\n");
231 /* Step through all the descriptors looking for bidir mode */
232 for (id = ifcd, altno = 0;
234 id = (void *)((char *)id + id->bLength)) {
235 if (id->bDescriptorType == UDESC_INTERFACE &&
236 id->bInterfaceNumber == ifcd->bInterfaceNumber) {
237 if (id->bInterfaceClass == UICLASS_PRINTER &&
238 id->bInterfaceSubClass == UISUBCLASS_PRINTER &&
239 (id->bInterfaceProtocol == UIPROTO_PRINTER_BI ||
240 id->bInterfaceProtocol == UIPROTO_PRINTER_1284))
245 id = ifcd; /* not found, use original */
248 /* Found a new bidir setting */
249 DPRINTF(("ulpt_attach: set altno = %d\n", altno));
250 err = usbd_set_interface(iface, altno);
252 printf("%s: setting alternate interface failed\n",
253 USBDEVNAME(sc->sc_dev));
255 USB_ATTACH_ERROR_RETURN;
260 (void)usbd_endpoint_count(iface, &epcount);
264 for (i = 0; i < epcount; i++) {
265 ed = usbd_interface2endpoint_descriptor(iface, i);
267 printf("%s: couldn't get ep %d\n",
268 USBDEVNAME(sc->sc_dev), i);
269 USB_ATTACH_ERROR_RETURN;
271 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
272 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
273 sc->sc_in = ed->bEndpointAddress;
274 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
275 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
276 sc->sc_out = ed->bEndpointAddress;
279 if (sc->sc_out == -1) {
280 printf("%s: could not find bulk out endpoint\n",
281 USBDEVNAME(sc->sc_dev));
283 USB_ATTACH_ERROR_RETURN;
286 if (usbd_get_quirks(dev)->uq_flags & UQ_BROKEN_BIDIR) {
287 /* This device doesn't handle reading properly. */
291 printf("%s: using %s-directional mode\n", USBDEVNAME(sc->sc_dev),
292 sc->sc_in >= 0 ? "bi" : "uni");
294 DPRINTFN(10, ("ulpt_attach: bulk=%d\n", sc->sc_out));
296 sc->sc_iface = iface;
297 sc->sc_ifaceno = id->bInterfaceNumber;
302 * This code is disabled because for some mysterious reason it causes
303 * printing not to work. But only sometimes, and mostly with
304 * UHCI and less often with OHCI. *sigh*
307 usb_config_descriptor_t *cd = usbd_get_config_descriptor(dev);
308 usb_device_request_t req;
311 req.bmRequestType = UT_READ_CLASS_INTERFACE;
312 req.bRequest = UR_GET_DEVICE_ID;
313 USETW(req.wValue, cd->bConfigurationValue);
314 USETW2(req.wIndex, id->bInterfaceNumber, id->bAlternateSetting);
315 USETW(req.wLength, sizeof devinfo - 1);
316 err = usbd_do_request_flags(dev, &req, devinfo, USBD_SHORT_XFER_OK,
317 &alen, USBD_DEFAULT_TIMEOUT);
319 printf("%s: cannot get device id\n", USBDEVNAME(sc->sc_dev));
320 } else if (alen <= 2) {
321 printf("%s: empty device id, no printer connected?\n",
322 USBDEVNAME(sc->sc_dev));
324 /* devinfo now contains an IEEE-1284 device ID */
325 len = ((devinfo[0] & 0xff) << 8) | (devinfo[1] & 0xff);
326 if (len > sizeof devinfo - 3)
327 len = sizeof devinfo - 3;
329 printf("%s: device id <", USBDEVNAME(sc->sc_dev));
330 ieee1284_print_id(devinfo+2);
336 #if defined(__FreeBSD__)
337 sc->dev = make_dev(&ulpt_cdevsw, device_get_unit(self),
338 UID_ROOT, GID_OPERATOR, 0644, "ulpt%d", device_get_unit(self));
339 sc->dev_noprime = make_dev(&ulpt_cdevsw,
340 device_get_unit(self)|ULPT_NOPRIME,
341 UID_ROOT, GID_OPERATOR, 0644, "unlpt%d", device_get_unit(self));
344 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
347 USB_ATTACH_SUCCESS_RETURN;
350 #if defined(__NetBSD__) || defined(__OpenBSD__)
352 ulpt_activate(device_ptr_t self, enum devact act)
354 struct ulpt_softc *sc = (struct ulpt_softc *)self;
361 case DVACT_DEACTIVATE:
371 USB_DETACH_START(ulpt, sc);
373 #if defined(__NetBSD__) || defined(__OpenBSD__)
375 #elif defined(__FreeBSD__)
379 #if defined(__NetBSD__) || defined(__OpenBSD__)
380 DPRINTF(("ulpt_detach: sc=%p flags=%d\n", sc, flags));
381 #elif defined(__FreeBSD__)
382 DPRINTF(("ulpt_detach: sc=%p\n", sc));
386 if (sc->sc_out_pipe != NULL)
387 usbd_abort_pipe(sc->sc_out_pipe);
388 if (sc->sc_in_pipe != NULL)
389 usbd_abort_pipe(sc->sc_in_pipe);
392 if (--sc->sc_refcnt >= 0) {
393 /* There is noone to wake, aborting the pipe is enough */
394 /* Wait for processes to go away. */
395 usb_detach_wait(USBDEV(sc->sc_dev));
399 #if defined(__NetBSD__) || defined(__OpenBSD__)
400 /* locate the major number */
401 for (maj = 0; maj < nchrdev; maj++)
402 if (cdevsw[maj].d_open == ulptopen)
405 /* Nuke the vnodes for any open instances (calls close). */
407 vdevgone(maj, mn, mn, VCHR);
408 #elif defined(__FreeBSD__)
409 vp = SLIST_FIRST(&sc->dev->si_hlist);
411 VOP_REVOKE(vp, REVOKEALL);
412 vp = SLIST_FIRST(&sc->dev_noprime->si_hlist);
414 VOP_REVOKE(vp, REVOKEALL);
416 destroy_dev(sc->dev);
417 destroy_dev(sc->dev_noprime);
420 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
427 ulpt_status(struct ulpt_softc *sc)
429 usb_device_request_t req;
433 req.bmRequestType = UT_READ_CLASS_INTERFACE;
434 req.bRequest = UR_GET_PORT_STATUS;
435 USETW(req.wValue, 0);
436 USETW(req.wIndex, sc->sc_ifaceno);
437 USETW(req.wLength, 1);
438 err = usbd_do_request(sc->sc_udev, &req, &status);
439 DPRINTFN(1, ("ulpt_status: status=0x%02x err=%d\n", status, err));
447 ulpt_reset(struct ulpt_softc *sc)
449 usb_device_request_t req;
451 DPRINTFN(1, ("ulpt_reset\n"));
452 req.bRequest = UR_SOFT_RESET;
453 USETW(req.wValue, 0);
454 USETW(req.wIndex, sc->sc_ifaceno);
455 USETW(req.wLength, 0);
458 * There was a mistake in the USB printer 1.0 spec that gave the
459 * request type as UT_WRITE_CLASS_OTHER; it should have been
460 * UT_WRITE_CLASS_INTERFACE. Many printers use the old one,
463 req.bmRequestType = UT_WRITE_CLASS_OTHER;
464 if (usbd_do_request(sc->sc_udev, &req, 0)) { /* 1.0 */
465 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
466 (void)usbd_do_request(sc->sc_udev, &req, 0); /* 1.1 */
471 ulpt_input(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
473 struct ulpt_softc *sc = priv;
475 DPRINTFN(2,("ulpt_input: got some data\n"));
477 if (xfer == sc->sc_in_xfer1)
478 usbd_transfer(sc->sc_in_xfer2);
480 usbd_transfer(sc->sc_in_xfer1);
486 * Reset the printer, then wait until it's selected and not busy.
489 ulptopen(dev_t dev, int flag, int mode, usb_proc_ptr p)
491 u_char flags = ULPTFLAGS(dev);
492 struct ulpt_softc *sc;
496 USB_GET_SC_OPEN(ulpt, ULPTUNIT(dev), sc);
498 if (sc == NULL || sc->sc_iface == NULL || sc->sc_dying)
504 sc->sc_state = ULPT_INIT;
505 sc->sc_flags = flags;
506 DPRINTF(("ulptopen: flags=0x%x\n", (unsigned)flags));
508 #if defined(USB_DEBUG) && defined(__FreeBSD__)
509 /* Ignoring these flags might not be a good idea */
510 if ((flags & ~ULPT_NOPRIME) != 0)
511 printf("ulptopen: flags ignored: %b\n", flags,
512 "\20\3POS_INIT\4POS_ACK\6PRIME_OPEN\7AUTOLF\10BYPASS");
519 if ((flags & ULPT_NOPRIME) == 0)
522 for (spin = 0; (ulpt_status(sc) & LPS_SELECT) == 0; spin += STEP) {
523 DPRINTF(("ulpt_open: waiting a while\n"));
524 if (spin >= TIMEOUT) {
530 /* wait 1/4 second, give up if we get a signal */
531 error = tsleep((caddr_t)sc, LPTPRI | PCATCH, "ulptop", STEP);
532 if (error != EWOULDBLOCK) {
544 err = usbd_open_pipe(sc->sc_iface, sc->sc_out, 0, &sc->sc_out_pipe);
550 if (ulptusein && sc->sc_in != -1) {
551 DPRINTF(("ulpt_open: open input pipe\n"));
552 err = usbd_open_pipe(sc->sc_iface, sc->sc_in,0,&sc->sc_in_pipe);
555 usbd_close_pipe(sc->sc_out_pipe);
556 sc->sc_out_pipe = NULL;
560 sc->sc_in_xfer1 = usbd_alloc_xfer(sc->sc_udev);
561 sc->sc_in_xfer2 = usbd_alloc_xfer(sc->sc_udev);
562 if (sc->sc_in_xfer1 == NULL || sc->sc_in_xfer2 == NULL) {
564 if (sc->sc_in_xfer1 != NULL) {
565 usbd_free_xfer(sc->sc_in_xfer1);
566 sc->sc_in_xfer1 = NULL;
568 if (sc->sc_in_xfer2 != NULL) {
569 usbd_free_xfer(sc->sc_in_xfer2);
570 sc->sc_in_xfer2 = NULL;
572 usbd_close_pipe(sc->sc_out_pipe);
573 sc->sc_out_pipe = NULL;
574 usbd_close_pipe(sc->sc_in_pipe);
575 sc->sc_in_pipe = NULL;
579 usbd_setup_xfer(sc->sc_in_xfer1, sc->sc_in_pipe, sc,
580 sc->sc_junk, sizeof sc->sc_junk, USBD_SHORT_XFER_OK,
581 USBD_NO_TIMEOUT, ulpt_input);
582 usbd_setup_xfer(sc->sc_in_xfer2, sc->sc_in_pipe, sc,
583 sc->sc_junk, sizeof sc->sc_junk, USBD_SHORT_XFER_OK,
584 USBD_NO_TIMEOUT, ulpt_input);
585 usbd_transfer(sc->sc_in_xfer1); /* ignore failed start */
588 sc->sc_state = ULPT_OPEN;
591 if (--sc->sc_refcnt < 0)
592 usb_detach_wakeup(USBDEV(sc->sc_dev));
594 DPRINTF(("ulptopen: done, error=%d\n", error));
599 ulpt_statusmsg(u_char status, struct ulpt_softc *sc)
603 status = (status ^ LPS_INVERT) & LPS_MASK;
604 new = status & ~sc->sc_laststatus;
605 sc->sc_laststatus = status;
607 if (new & LPS_SELECT)
608 log(LOG_NOTICE, "%s: offline\n", USBDEVNAME(sc->sc_dev));
609 else if (new & LPS_NOPAPER)
610 log(LOG_NOTICE, "%s: out of paper\n", USBDEVNAME(sc->sc_dev));
611 else if (new & LPS_NERR)
612 log(LOG_NOTICE, "%s: output error\n", USBDEVNAME(sc->sc_dev));
618 ulptclose(dev_t dev, int flag, int mode, usb_proc_ptr p)
620 struct ulpt_softc *sc;
622 USB_GET_SC(ulpt, ULPTUNIT(dev), sc);
624 if (sc->sc_state != ULPT_OPEN)
625 /* We are being forced to close before the open completed. */
628 if (sc->sc_out_pipe != NULL) {
629 usbd_close_pipe(sc->sc_out_pipe);
630 sc->sc_out_pipe = NULL;
632 if (sc->sc_in_pipe != NULL) {
633 usbd_abort_pipe(sc->sc_in_pipe);
634 usbd_close_pipe(sc->sc_in_pipe);
635 sc->sc_in_pipe = NULL;
636 if (sc->sc_in_xfer1 != NULL) {
637 usbd_free_xfer(sc->sc_in_xfer1);
638 sc->sc_in_xfer1 = NULL;
640 if (sc->sc_in_xfer2 != NULL) {
641 usbd_free_xfer(sc->sc_in_xfer2);
642 sc->sc_in_xfer2 = NULL;
648 DPRINTF(("ulptclose: closed\n"));
653 ulpt_do_write(struct ulpt_softc *sc, struct uio *uio, int flags)
658 usbd_xfer_handle xfer;
661 DPRINTF(("ulptwrite\n"));
662 xfer = usbd_alloc_xfer(sc->sc_udev);
665 bufp = usbd_alloc_buffer(xfer, ULPT_BSIZE);
667 usbd_free_xfer(xfer);
670 while ((n = min(ULPT_BSIZE, uio->uio_resid)) != 0) {
671 ulpt_statusmsg(ulpt_status(sc), sc);
672 error = uiomove(bufp, n, uio);
675 DPRINTFN(1, ("ulptwrite: transfer %d bytes\n", n));
676 err = usbd_bulk_transfer(xfer, sc->sc_out_pipe, USBD_NO_COPY,
677 USBD_NO_TIMEOUT, bufp, &n, "ulptwr");
679 DPRINTF(("ulptwrite: error=%d\n", err));
684 usbd_free_xfer(xfer);
690 ulptwrite(dev_t dev, struct uio *uio, int flags)
692 struct ulpt_softc *sc;
695 USB_GET_SC(ulpt, ULPTUNIT(dev), sc);
701 error = ulpt_do_write(sc, uio, flags);
702 if (--sc->sc_refcnt < 0)
703 usb_detach_wakeup(USBDEV(sc->sc_dev));
708 ulptioctl(dev_t dev, u_long cmd, caddr_t data, int flag, usb_proc_ptr p)
721 /* XXX This does not belong here. */
723 * Print select parts of a IEEE 1284 device ID.
726 ieee1284_print_id(char *str)
730 for (p = str-1; p; p = strchr(p, ';')) {
732 if (strncmp(p, "MFG:", 4) == 0 ||
733 strncmp(p, "MANUFACTURER:", 14) == 0 ||
734 strncmp(p, "MDL:", 4) == 0 ||
735 strncmp(p, "MODEL:", 6) == 0) {
738 printf("%.*s", (int)(q - p + 1), p);
744 #if defined(__FreeBSD__)
745 DRIVER_MODULE(ulpt, uhub, ulpt_driver, ulpt_devclass, usbd_driver_load, 0);