Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sys/dev/usb Use the bi-directional protocol if the printer s...



details:   https://anonhg.NetBSD.org/src/rev/ebe7b89efcef
branches:  trunk
changeset: 508631:ebe7b89efcef
user:      augustss <augustss%NetBSD.org@localhost>
date:      Mon Apr 16 00:18:06 2001 +0000

description:
Use the bi-directional protocol if the printer supports it.
This is the way that e.g. HP recommends (but then some of their printers
have a bug that makes the input pipe useless anyway).
Also try reset both the 1.0 and 1.1 ways.

diffstat:

 sys/dev/usb/ulpt.c |  203 ++++++++++++++++++++++++++++++++++++++++++----------
 1 files changed, 164 insertions(+), 39 deletions(-)

diffs (truncated from 314 to 300 lines):

diff -r c120138fdc11 -r ebe7b89efcef sys/dev/usb/ulpt.c
--- a/sys/dev/usb/ulpt.c        Sun Apr 15 23:26:05 2001 +0000
+++ b/sys/dev/usb/ulpt.c        Mon Apr 16 00:18:06 2001 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: ulpt.c,v 1.41 2001/01/07 14:26:19 augustss Exp $       */
+/*     $NetBSD: ulpt.c,v 1.42 2001/04/16 00:18:06 augustss Exp $       */
 /*     $FreeBSD: src/sys/dev/usb/ulpt.c,v 1.24 1999/11/17 22:33:44 n_hibma Exp $       */
 
 /*
@@ -95,8 +95,15 @@
        usbd_device_handle sc_udev;     /* device */
        usbd_interface_handle sc_iface; /* interface */
        int sc_ifaceno;
-       usbd_pipe_handle sc_bulkpipe;   /* bulk pipe */
-       int sc_bulk;
+
+       int sc_out;
+       usbd_pipe_handle sc_out_pipe;   /* bulk out pipe */
+
+       int sc_in;
+       usbd_pipe_handle sc_in_pipe;    /* bulk in pipe */
+       usbd_xfer_handle sc_in_xfer1;
+       usbd_xfer_handle sc_in_xfer2;
+       u_char sc_junk[64];     /* somewhere to dump input */
 
        u_char sc_state;
 #define        ULPT_OPEN       0x01    /* device is open */
@@ -150,7 +157,9 @@
 void ulpt_reset(struct ulpt_softc *);
 int ulpt_statusmsg(u_char, struct ulpt_softc *);
 
+#if 0
 void ieee1284_print_id(char *);
+#endif
 
 #define        ULPTUNIT(s)     (minor(s) & 0x1f)
 #define        ULPTFLAGS(s)    (minor(s) & 0xe0)
@@ -181,41 +190,103 @@
        USB_ATTACH_START(ulpt, sc, uaa);
        usbd_device_handle dev = uaa->device;
        usbd_interface_handle iface = uaa->iface;
-       usb_interface_descriptor_t *id = usbd_get_interface_descriptor(iface);
+       usb_interface_descriptor_t *ifcd = usbd_get_interface_descriptor(iface);
+       usb_interface_descriptor_t *id, *iend;
+       usb_config_descriptor_t *cdesc;
+       usbd_status err;
        char devinfo[1024];
        usb_endpoint_descriptor_t *ed;
-       usbd_status err;
+       u_int8_t epcount;
+       int i, altno;
        
        DPRINTFN(10,("ulpt_attach: sc=%p\n", sc));
        usbd_devinfo(dev, 0, devinfo);
        USB_ATTACH_SETUP;
        printf("%s: %s, iclass %d/%d\n", USBDEVNAME(sc->sc_dev),
-              devinfo, id->bInterfaceClass, id->bInterfaceSubClass);
+              devinfo, ifcd->bInterfaceClass, ifcd->bInterfaceSubClass);
 
-       /* Figure out which endpoint is the bulk out endpoint. */
-       ed = usbd_interface2endpoint_descriptor(iface, 0);
-       if (ed == NULL)
-               goto nobulk;
-       if (UE_GET_DIR(ed->bEndpointAddress) != UE_DIR_OUT ||
-           (ed->bmAttributes & UE_XFERTYPE) != UE_BULK) {
-               /* In case we are using a bidir protocol... */
-               ed = usbd_interface2endpoint_descriptor(iface, 1);
-               if (ed == NULL)
-                       goto nobulk;
-               if (UE_GET_DIR(ed->bEndpointAddress) != UE_DIR_OUT ||
-                   (ed->bmAttributes & UE_XFERTYPE) != UE_BULK)
-                       goto nobulk;
+       /* XXX 
+        * Stepping through the alternate settings needs to be abstracted out.
+        */
+       cdesc = usbd_get_config_descriptor(dev);
+       if (cdesc == NULL) {
+               printf("%s: failed to get configuration descriptor\n",
+                      USBDEVNAME(sc->sc_dev));
+               USB_ATTACH_ERROR_RETURN;
+       }
+       iend = (usb_interface_descriptor_t *)
+                  ((char *)cdesc + UGETW(cdesc->wTotalLength));
+#ifdef DIAGNOSTIC
+       if (ifcd < (usb_interface_descriptor_t *)cdesc ||
+           ifcd >= iend)
+               panic("ulpt: iface desc out of range\n");
+#endif
+       /* Step through all the descriptors looking for bidir mode */
+       for (id = ifcd, altno = 0;
+            id < iend;
+            id = (void *)((char *)id + id->bLength)) {
+               if (id->bDescriptorType == UDESC_INTERFACE &&
+                   id->bInterfaceNumber == ifcd->bInterfaceNumber) {
+                       if (id->bInterfaceClass == UICLASS_PRINTER &&
+                           id->bInterfaceSubClass == UISUBCLASS_PRINTER &&
+                           id->bInterfaceProtocol == UIPROTO_PRINTER_BI)
+                               goto found;
+                       altno++;
+               }
        }
-       sc->sc_bulk = ed->bEndpointAddress;
-       DPRINTFN(10, ("ulpt_attach: bulk=%d\n", sc->sc_bulk));
+       id = ifcd;              /* not found, use original */
+ found:
+       if (id != ifcd) {
+               /* Found a new bidir setting */
+               DPRINTF(("ulpt_attach: set altno = %d\n", altno));
+               err = usbd_set_interface(iface, altno);
+               if (err) {
+                       printf("%s: setting alternate interface failed\n",
+                              USBDEVNAME(sc->sc_dev));
+                       sc->sc_dying = 1;
+                       USB_ATTACH_ERROR_RETURN;
+               }
+       }
+
+       epcount = 0;
+       (void)usbd_endpoint_count(iface, &epcount);
 
-       sc->sc_iface = iface;
-       err = usbd_interface2device_handle(iface, &sc->sc_udev);
-       if (err) {
+       sc->sc_in = -1;
+       sc->sc_out = -1;
+       for (i = 0; i < epcount; i++) {
+               ed = usbd_interface2endpoint_descriptor(iface, i);
+               if (ed == NULL) {
+                       printf("%s: couldn't get ep %d\n",
+                           USBDEVNAME(sc->sc_dev), i);
+                       USB_ATTACH_ERROR_RETURN;
+               }
+               if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
+                   UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
+                       sc->sc_in = ed->bEndpointAddress;
+               } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
+                          UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
+                       sc->sc_out = ed->bEndpointAddress;
+               }
+       }
+       if (sc->sc_out == -1) {
+               printf("%s: could not find bulk endpoint\n",
+                   USBDEVNAME(sc->sc_dev));
                sc->sc_dying = 1;
                USB_ATTACH_ERROR_RETURN;
        }
+       printf("%s: using %s-directional mode\n", USBDEVNAME(sc->sc_dev),
+              sc->sc_in >= 0 ? "bi" : "uni");
+
+       if (usbd_get_quirks(dev)->uq_flags & UQ_BROKEN_BIDIR) {
+               /* This device doesn't handle reading properly. */
+               sc->sc_in = -1;
+       }
+
+       DPRINTFN(10, ("ulpt_attach: bulk=%d\n", sc->sc_out));
+
+       sc->sc_iface = iface;
        sc->sc_ifaceno = id->bInterfaceNumber;
+       sc->sc_udev = dev;
 
 #if 0
 /*
@@ -265,11 +336,6 @@
                           USBDEV(sc->sc_dev));
 
        USB_ATTACH_SUCCESS_RETURN;
-
- nobulk:
-       printf("%s: could not find bulk endpoint\n", USBDEVNAME(sc->sc_dev));
-       sc->sc_dying = 1;
-       USB_ATTACH_ERROR_RETURN;
 }
 
 #if defined(__NetBSD__) || defined(__OpenBSD__)
@@ -304,8 +370,10 @@
 #endif
 
        sc->sc_dying = 1;
-       if (sc->sc_bulkpipe != NULL)
-               usbd_abort_pipe(sc->sc_bulkpipe);
+       if (sc->sc_out_pipe != NULL)
+               usbd_abort_pipe(sc->sc_out_pipe);
+       if (sc->sc_in_pipe != NULL)
+               usbd_abort_pipe(sc->sc_in_pipe);
 
        s = splusb();
        if (--sc->sc_refcnt >= 0) {
@@ -363,7 +431,6 @@
        usb_device_request_t req;
 
        DPRINTFN(1, ("ulpt_reset\n"));
-       req.bmRequestType = UT_WRITE_CLASS_OTHER;
        req.bRequest = UR_SOFT_RESET;
        USETW(req.wValue, 0);
        USETW(req.wIndex, sc->sc_ifaceno);
@@ -371,16 +438,31 @@
 
        /*
         * There was a mistake in the USB printer 1.0 spec that gave the
-        * request type as UT_WRITE_CLASS_OTHER, it should have been
+        * request type as UT_WRITE_CLASS_OTHER; it should have been
         * UT_WRITE_CLASS_INTERFACE.  Many printers use the old one,
         * so we try both.
         */
-       if (usbd_do_request(sc->sc_udev, &req, 0)) {
+       if (usbd_do_request(sc->sc_udev, &req, 0)) {    /* 1.0 */
                req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
-               (void)usbd_do_request(sc->sc_udev, &req, 0);
+               (void)usbd_do_request(sc->sc_udev, &req, 0); /* 1.1 */
        }
 }
 
+static void
+ulpt_input(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
+{
+       struct ulpt_softc *sc = priv;
+
+       DPRINTFN(2,("ulpt_input: got some data\n"));
+       /* Do it again. */
+       if (xfer == sc->sc_in_xfer1)
+               usbd_transfer(sc->sc_in_xfer2);
+       else
+               usbd_transfer(sc->sc_in_xfer1);
+}
+
+int ulptusein = 1;
+
 /*
  * Reset the printer, then wait until it's selected and not busy.
  */
@@ -419,6 +501,7 @@
                ulpt_reset(sc);
 
        for (spin = 0; (ulpt_status(sc) & LPS_SELECT) == 0; spin += STEP) {
+               DPRINTF(("ulpt_open: waiting a while\n"));
                if (spin >= TIMEOUT) {
                        error = EBUSY;
                        sc->sc_state = 0;
@@ -439,12 +522,39 @@
                }
        }
 
-       err = usbd_open_pipe(sc->sc_iface, sc->sc_bulk, 0, &sc->sc_bulkpipe);
+       err = usbd_open_pipe(sc->sc_iface, sc->sc_out, 0, &sc->sc_out_pipe);
        if (err) {
                sc->sc_state = 0;
                error = EIO;
                goto done;
        }
+       if (ulptusein && sc->sc_in != -1) {
+               DPRINTF(("ulpt_open: open input pipe\n"));
+               err = usbd_open_pipe(sc->sc_iface, sc->sc_in,0,&sc->sc_in_pipe);
+               if (err) {
+                       error = EIO;
+                       usbd_close_pipe(sc->sc_out_pipe);
+                       sc->sc_out_pipe = NULL;
+                       sc->sc_state = 0;
+                       goto done;
+               }
+               sc->sc_in_xfer1 = usbd_alloc_xfer(sc->sc_udev);
+               sc->sc_in_xfer2 = usbd_alloc_xfer(sc->sc_udev);
+               if (sc->sc_in_xfer1 == NULL || sc->sc_in_xfer2 == NULL) {
+                       error = ENOMEM;
+                       usbd_close_pipe(sc->sc_out_pipe);
+                       sc->sc_out_pipe = NULL;
+                       sc->sc_state = 0;
+                       goto done;
+               }
+               usbd_setup_xfer(sc->sc_in_xfer1, sc->sc_in_pipe, sc,
+                   sc->sc_junk, sizeof sc->sc_junk, USBD_SHORT_XFER_OK,
+                   USBD_NO_TIMEOUT, ulpt_input);
+               usbd_setup_xfer(sc->sc_in_xfer2, sc->sc_in_pipe, sc,
+                   sc->sc_junk, sizeof sc->sc_junk, USBD_SHORT_XFER_OK,
+                   USBD_NO_TIMEOUT, ulpt_input);
+               usbd_transfer(sc->sc_in_xfer1); /* ignore failed start */
+       }
 
        sc->sc_state = ULPT_OPEN;
 
@@ -486,8 +596,23 @@
                /* We are being forced to close before the open completed. */
                return (0);
 
-       usbd_close_pipe(sc->sc_bulkpipe);
-       sc->sc_bulkpipe = 0;
+       if (sc->sc_out_pipe != NULL) {
+               usbd_close_pipe(sc->sc_out_pipe);
+               sc->sc_out_pipe = NULL;
+       }
+       if (sc->sc_in_pipe != NULL) {
+               usbd_abort_pipe(sc->sc_in_pipe);
+               usbd_close_pipe(sc->sc_in_pipe);
+               sc->sc_in_pipe = NULL;
+               if (sc->sc_in_xfer1 != NULL) {
+                       usbd_free_xfer(sc->sc_in_xfer1);
+                       sc->sc_in_xfer1 = NULL;
+               }
+               if (sc->sc_in_xfer2 != NULL) {
+                       usbd_free_xfer(sc->sc_in_xfer2);
+                       sc->sc_in_xfer2 = NULL;



Home | Main Index | Thread Index | Old Index