1 /* $OpenBSD: uscom.c,v 1.2 2014/07/12 21:24:33 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/usbdevs.h> 32 #include <dev/usb/ucomvar.h> 33 34 #define USCOMBUFSZ 256 35 #define USCOM_CONFIG_NO 0 36 #define USCOM_IFACE_NO 0 37 38 struct uscom_softc { 39 struct device sc_dev; 40 struct usbd_device *sc_udev; 41 struct usbd_interface *sc_iface; 42 struct device *sc_subdev; 43 }; 44 45 struct ucom_methods uscom_methods = { 46 NULL, 47 NULL, 48 NULL, 49 NULL, 50 NULL, 51 NULL, 52 NULL, 53 NULL, 54 }; 55 56 static const struct usb_devno uscom_devs[] = { 57 { USB_VENDOR_HP, USB_PRODUCT_HP_HPX9GP } 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 if (usbd_set_config_index(sc->sc_udev, USCOM_CONFIG_NO, 1) != 0) { 99 printf("%s: could not set configuration no\n", 100 sc->sc_dev.dv_xname); 101 usbd_deactivate(sc->sc_udev); 102 return; 103 } 104 105 /* get the first interface handle */ 106 error = usbd_device2interface_handle(sc->sc_udev, USCOM_IFACE_NO, 107 &sc->sc_iface); 108 if (error != 0) { 109 printf("%s: could not get interface handle\n", 110 sc->sc_dev.dv_xname); 111 usbd_deactivate(sc->sc_udev); 112 return; 113 } 114 115 id = usbd_get_interface_descriptor(sc->sc_iface); 116 117 uca.bulkin = uca.bulkout = -1; 118 for (i = 0; i < id->bNumEndpoints; i++) { 119 ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i); 120 if (ed == NULL) { 121 printf("%s: no endpoint descriptor found for %d\n", 122 sc->sc_dev.dv_xname, i); 123 usbd_deactivate(sc->sc_udev); 124 return; 125 } 126 127 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN && 128 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) 129 uca.bulkin = ed->bEndpointAddress; 130 else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT && 131 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) 132 uca.bulkout = ed->bEndpointAddress; 133 } 134 135 if (uca.bulkin == -1 || uca.bulkout == -1) { 136 printf("%s: missing endpoint\n", sc->sc_dev.dv_xname); 137 usbd_deactivate(sc->sc_udev); 138 return; 139 } 140 141 uca.ibufsize = USCOMBUFSZ; 142 uca.obufsize = USCOMBUFSZ; 143 uca.ibufsizepad = USCOMBUFSZ; 144 uca.opkthdrlen = 0; 145 uca.device = sc->sc_udev; 146 uca.iface = sc->sc_iface; 147 uca.methods = &uscom_methods; 148 uca.arg = sc; 149 uca.info = NULL; 150 151 sc->sc_subdev = config_found_sm(self, &uca, ucomprint, ucomsubmatch); 152 } 153 154 int 155 uscom_detach(struct device *self, int flags) 156 { 157 struct uscom_softc *sc = (struct uscom_softc *)self; 158 int rv = 0; 159 160 if (sc->sc_subdev != NULL) { 161 rv = config_detach(sc->sc_subdev, flags); 162 sc->sc_subdev = NULL; 163 } 164 165 return (rv); 166 } 167