1 /* $OpenBSD: uscom.c,v 1.5 2016/09/02 09:14:59 mpi 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 }; 57 58 int uscom_match(struct device *, void *, void *); 59 void uscom_attach(struct device *, struct device *, void *); 60 int uscom_detach(struct device *, int); 61 62 struct cfdriver uscom_cd = { 63 NULL, "uscom", DV_DULL 64 }; 65 66 const struct cfattach uscom_ca = { 67 sizeof(struct uscom_softc), uscom_match, uscom_attach, uscom_detach 68 }; 69 70 int 71 uscom_match(struct device *parent, void *match, void *aux) 72 { 73 struct usb_attach_arg *uaa = aux; 74 75 if (uaa->iface == NULL) 76 return UMATCH_NONE; 77 78 return (usb_lookup(uscom_devs, uaa->vendor, uaa->product) != NULL) ? 79 UMATCH_VENDOR_PRODUCT : UMATCH_NONE; 80 } 81 82 void 83 uscom_attach(struct device *parent, struct device *self, void *aux) 84 { 85 struct uscom_softc *sc = (struct uscom_softc *)self; 86 struct usb_attach_arg *uaa = aux; 87 struct ucom_attach_args uca; 88 usb_interface_descriptor_t *id; 89 usb_endpoint_descriptor_t *ed; 90 usbd_status error; 91 int i; 92 93 bzero(&uca, sizeof(uca)); 94 sc->sc_udev = uaa->device; 95 96 /* get the first interface handle */ 97 error = usbd_device2interface_handle(sc->sc_udev, USCOM_IFACE_NO, 98 &sc->sc_iface); 99 if (error != 0) { 100 printf("%s: could not get interface handle\n", 101 sc->sc_dev.dv_xname); 102 usbd_deactivate(sc->sc_udev); 103 return; 104 } 105 106 id = usbd_get_interface_descriptor(sc->sc_iface); 107 108 uca.bulkin = uca.bulkout = -1; 109 for (i = 0; i < id->bNumEndpoints; i++) { 110 ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i); 111 if (ed == NULL) { 112 printf("%s: no endpoint descriptor found for %d\n", 113 sc->sc_dev.dv_xname, i); 114 usbd_deactivate(sc->sc_udev); 115 return; 116 } 117 118 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN && 119 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) 120 uca.bulkin = ed->bEndpointAddress; 121 else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT && 122 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) 123 uca.bulkout = ed->bEndpointAddress; 124 } 125 126 if (uca.bulkin == -1 || uca.bulkout == -1) { 127 printf("%s: missing endpoint\n", sc->sc_dev.dv_xname); 128 usbd_deactivate(sc->sc_udev); 129 return; 130 } 131 132 uca.ibufsize = USCOMBUFSZ; 133 uca.obufsize = USCOMBUFSZ; 134 uca.ibufsizepad = USCOMBUFSZ; 135 uca.opkthdrlen = 0; 136 uca.device = sc->sc_udev; 137 uca.iface = sc->sc_iface; 138 uca.methods = &uscom_methods; 139 uca.arg = sc; 140 uca.info = NULL; 141 142 sc->sc_subdev = config_found_sm(self, &uca, ucomprint, ucomsubmatch); 143 } 144 145 int 146 uscom_detach(struct device *self, int flags) 147 { 148 struct uscom_softc *sc = (struct uscom_softc *)self; 149 int rv = 0; 150 151 if (sc->sc_subdev != NULL) { 152 rv = config_detach(sc->sc_subdev, flags); 153 sc->sc_subdev = NULL; 154 } 155 156 return (rv); 157 } 158