summaryrefslogtreecommitdiff
path: root/sys/dev/usb/ulpt.c
diff options
context:
space:
mode:
authorAaron Campbell <aaron@cvs.openbsd.org>2001-05-03 02:20:36 +0000
committerAaron Campbell <aaron@cvs.openbsd.org>2001-05-03 02:20:36 +0000
commit5b92d7a741e1c34c9d68a85e4b9a47c24ea37b56 (patch)
treeac62c98e8633ce27c0186aaa2e0856e4093ade97 /sys/dev/usb/ulpt.c
parentc31dae3edb4a874106f38c4f1418182e49eda727 (diff)
Sync with NetBSD. Tested with a USB keyboard, USB mouse, and three different
kue(4) Ethernet devices.
Diffstat (limited to 'sys/dev/usb/ulpt.c')
-rw-r--r--sys/dev/usb/ulpt.c207
1 files changed, 166 insertions, 41 deletions
diff --git a/sys/dev/usb/ulpt.c b/sys/dev/usb/ulpt.c
index ede62f3d4d9..3769a2a2e29 100644
--- a/sys/dev/usb/ulpt.c
+++ b/sys/dev/usb/ulpt.c
@@ -1,5 +1,5 @@
-/* $OpenBSD: ulpt.c,v 1.6 2001/01/28 09:43:42 aaron Exp $ */
-/* $NetBSD: ulpt.c,v 1.38 2000/06/01 14:29:00 augustss Exp $ */
+/* $OpenBSD: ulpt.c,v 1.7 2001/05/03 02:20:34 aaron 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 $ */
/*
@@ -96,8 +96,15 @@ struct ulpt_softc {
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 */
@@ -151,7 +158,9 @@ int ulpt_status(struct ulpt_softc *);
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)
@@ -182,41 +191,103 @@ USB_ATTACH(ulpt)
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);
-
- /* 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;
+ devinfo, ifcd->bInterfaceClass, ifcd->bInterfaceSubClass);
+
+ /* 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++;
+ }
+ }
+ 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;
+ }
}
- sc->sc_bulk = ed->bEndpointAddress;
- DPRINTFN(10, ("ulpt_attach: bulk=%d\n", sc->sc_bulk));
- sc->sc_iface = iface;
- err = usbd_interface2device_handle(iface, &sc->sc_udev);
- if (err) {
+ epcount = 0;
+ (void)usbd_endpoint_count(iface, &epcount);
+
+ 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
/*
@@ -266,11 +337,6 @@ USB_ATTACH(ulpt)
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__)
@@ -305,8 +371,10 @@ USB_DETACH(ulpt)
#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) {
@@ -345,7 +413,6 @@ ulpt_status(struct ulpt_softc *sc)
usbd_status err;
u_char status;
- req.bmRequestType = UT_READ_CLASS_INTERFACE;
req.bRequest = UR_GET_PORT_STATUS;
USETW(req.wValue, 0);
USETW(req.wIndex, sc->sc_ifaceno);
@@ -372,16 +439,31 @@ ulpt_reset(struct ulpt_softc *sc)
/*
* 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.
*/
@@ -420,6 +502,7 @@ ulptopen(dev_t dev, int flag, int mode, struct proc *p)
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;
@@ -440,12 +523,39 @@ ulptopen(dev_t dev, int flag, int mode, struct proc *p)
}
}
- 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;
@@ -487,8 +597,23 @@ ulptclose(dev_t dev, int flag, int mode, struct proc *p)
/* 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;
+ }
+ }
sc->sc_state = 0;
@@ -520,7 +645,7 @@ ulpt_do_write(struct ulpt_softc *sc, struct uio *uio, int flags)
if (error)
break;
DPRINTFN(1, ("ulptwrite: transfer %d bytes\n", n));
- err = usbd_bulk_transfer(xfer, sc->sc_bulkpipe, USBD_NO_COPY,
+ err = usbd_bulk_transfer(xfer, sc->sc_out_pipe, USBD_NO_COPY,
USBD_NO_TIMEOUT, bufp, &n, "ulptwr");
if (err) {
DPRINTF(("ulptwrite: error=%d\n", err));