1 /* $OpenBSD: uscom.c,v 1.7 2019/02/24 17:36:28 patrick Exp $ */ 2 3 /* 4 * Copyright (c) 2006 Jonathan Gray <jsg@openbsd.org> 5 * 6 * Permission to use, copy, modify, and distribute this software for any 7 * purpose with or without fee is hereby granted, provided that the above 8 * copyright notice and this permission notice appear in all copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 */ 18 19 #include <sys/param.h> 20 #include <sys/systm.h> 21 #include <sys/kernel.h> 22 #include <sys/conf.h> 23 #include <sys/tty.h> 24 #include <sys/device.h> 25 26 #include <dev/usb/usb.h> 27 #include <dev/usb/usbdi.h> 28 #include <dev/usb/usbdi_util.h> 29 #include <dev/usb/usbdevs.h> 30 31 #include <dev/usb/ucomvar.h> 32 33 #define USCOMBUFSZ 256 34 #define USCOM_IFACE_NO 0 35 36 struct uscom_softc { 37 struct device sc_dev; 38 struct usbd_device *sc_udev; 39 struct usbd_interface *sc_iface; 40 struct device *sc_subdev; 41 }; 42 43 struct ucom_methods uscom_methods = { 44 NULL, 45 NULL, 46 NULL, 47 NULL, 48 NULL, 49 NULL, 50 NULL, 51 NULL, 52 }; 53 54 static const struct usb_devno uscom_devs[] = { 55 { USB_VENDOR_HP, USB_PRODUCT_HP_HPX9GP }, 56 { USB_VENDOR_DYNASTREAM, USB_PRODUCT_DYNASTREAM_ANTUSB2 }, 57 { USB_VENDOR_DYNASTREAM, USB_PRODUCT_DYNASTREAM_ANTUSBM } 58 }; 59 60 int uscom_match(struct device *, void *, void *); 61 void uscom_attach(struct device *, struct device *, void *); 62 int uscom_detach(struct device *, int); 63 64 struct cfdriver uscom_cd = { 65 NULL, "uscom", DV_DULL 66 }; 67 68 const struct cfattach uscom_ca = { 69 sizeof(struct uscom_softc), uscom_match, uscom_attach, uscom_detach 70 }; 71 72 int 73 uscom_match(struct device *parent, void *match, void *aux) 74 { 75 struct usb_attach_arg *uaa = aux; 76 77 if (uaa->iface == NULL) 78 return UMATCH_NONE; 79 80 return (usb_lookup(uscom_devs, uaa->vendor, uaa->product) != NULL) ? 81 UMATCH_VENDOR_PRODUCT : UMATCH_NONE; 82 } 83 84 void 85 uscom_attach(struct device *parent, struct device *self, void *aux) 86 { 87 struct uscom_softc *sc = (struct uscom_softc *)self; 88 struct usb_attach_arg *uaa = aux; 89 struct ucom_attach_args uca; 90 usb_interface_descriptor_t *id; 91 usb_endpoint_descriptor_t *ed; 92 usbd_status error; 93 int i; 94 95 bzero(&uca, sizeof(uca)); 96 sc->sc_udev = uaa->device; 97 98 /* get the first interface handle */ 99 error = usbd_device2interface_handle(sc->sc_udev, USCOM_IFACE_NO, 100 &sc->sc_iface); 101 if (error != 0) { 102 printf("%s: could not get interface handle\n", 103 sc->sc_dev.dv_xname); 104 usbd_deactivate(sc->sc_udev); 105 return; 106 } 107 108 id = usbd_get_interface_descriptor(sc->sc_iface); 109 110 uca.bulkin = uca.bulkout = -1; 111 for (i = 0; i < id->bNumEndpoints; i++) { 112 ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i); 113 if (ed == NULL) { 114 printf("%s: no endpoint descriptor found for %d\n", 115 sc->sc_dev.dv_xname, i); 116 usbd_deactivate(sc->sc_udev); 117 return; 118 } 119 120 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN && 121 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) 122 uca.bulkin = ed->bEndpointAddress; 123 else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT && 124 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) 125 uca.bulkout = ed->bEndpointAddress; 126 } 127 128 if (uca.bulkin == -1 || uca.bulkout == -1) { 129 printf("%s: missing endpoint\n", sc->sc_dev.dv_xname); 130 usbd_deactivate(sc->sc_udev); 131 return; 132 } 133 134 uca.ibufsize = USCOMBUFSZ; 135 uca.obufsize = USCOMBUFSZ; 136 uca.ibufsizepad = USCOMBUFSZ; 137 uca.opkthdrlen = 0; 138 uca.device = sc->sc_udev; 139 uca.iface = sc->sc_iface; 140 uca.methods = &uscom_methods; 141 uca.arg = sc; 142 uca.info = NULL; 143 144 sc->sc_subdev = config_found_sm(self, &uca, ucomprint, ucomsubmatch); 145 } 146 147 int 148 uscom_detach(struct device *self, int flags) 149 { 150 struct uscom_softc *sc = (struct uscom_softc *)self; 151 int rv = 0; 152 153 if (sc->sc_subdev != NULL) { 154 rv = config_detach(sc->sc_subdev, flags); 155 sc->sc_subdev = NULL; 156 } 157 158 return (rv); 159 } 160