1 /* $OpenBSD: uscom.c,v 1.9 2024/05/23 03:21:09 jsg 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/tty.h> 22 #include <sys/device.h> 23 24 #include <dev/usb/usb.h> 25 #include <dev/usb/usbdi.h> 26 #include <dev/usb/usbdevs.h> 27 28 #include <dev/usb/ucomvar.h> 29 30 #define USCOMBUFSZ 256 31 #define USCOM_IFACE_NO 0 32 33 struct uscom_softc { 34 struct device sc_dev; 35 struct usbd_device *sc_udev; 36 struct usbd_interface *sc_iface; 37 struct device *sc_subdev; 38 }; 39 40 const struct ucom_methods uscom_methods = { 41 NULL, 42 NULL, 43 NULL, 44 NULL, 45 NULL, 46 NULL, 47 NULL, 48 NULL, 49 }; 50 51 static const struct usb_devno uscom_devs[] = { 52 { USB_VENDOR_HP, USB_PRODUCT_HP_HPX9GP }, 53 { USB_VENDOR_DYNASTREAM, USB_PRODUCT_DYNASTREAM_ANTUSB2 }, 54 { USB_VENDOR_DYNASTREAM, USB_PRODUCT_DYNASTREAM_ANTUSBM } 55 }; 56 57 int uscom_match(struct device *, void *, void *); 58 void uscom_attach(struct device *, struct device *, void *); 59 int uscom_detach(struct device *, int); 60 61 struct cfdriver uscom_cd = { 62 NULL, "uscom", DV_DULL 63 }; 64 65 const struct cfattach uscom_ca = { 66 sizeof(struct uscom_softc), uscom_match, uscom_attach, uscom_detach 67 }; 68 69 int 70 uscom_match(struct device *parent, void *match, void *aux) 71 { 72 struct usb_attach_arg *uaa = aux; 73 74 if (uaa->iface == NULL) 75 return UMATCH_NONE; 76 77 return (usb_lookup(uscom_devs, uaa->vendor, uaa->product) != NULL) ? 78 UMATCH_VENDOR_PRODUCT : UMATCH_NONE; 79 } 80 81 void 82 uscom_attach(struct device *parent, struct device *self, void *aux) 83 { 84 struct uscom_softc *sc = (struct uscom_softc *)self; 85 struct usb_attach_arg *uaa = aux; 86 struct ucom_attach_args uca; 87 usb_interface_descriptor_t *id; 88 usb_endpoint_descriptor_t *ed; 89 usbd_status error; 90 int i; 91 92 bzero(&uca, sizeof(uca)); 93 sc->sc_udev = uaa->device; 94 95 /* get the first interface handle */ 96 error = usbd_device2interface_handle(sc->sc_udev, USCOM_IFACE_NO, 97 &sc->sc_iface); 98 if (error != 0) { 99 printf("%s: could not get interface handle\n", 100 sc->sc_dev.dv_xname); 101 usbd_deactivate(sc->sc_udev); 102 return; 103 } 104 105 id = usbd_get_interface_descriptor(sc->sc_iface); 106 107 uca.bulkin = uca.bulkout = -1; 108 for (i = 0; i < id->bNumEndpoints; i++) { 109 ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i); 110 if (ed == NULL) { 111 printf("%s: no endpoint descriptor found for %d\n", 112 sc->sc_dev.dv_xname, i); 113 usbd_deactivate(sc->sc_udev); 114 return; 115 } 116 117 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN && 118 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) 119 uca.bulkin = ed->bEndpointAddress; 120 else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT && 121 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) 122 uca.bulkout = ed->bEndpointAddress; 123 } 124 125 if (uca.bulkin == -1 || uca.bulkout == -1) { 126 printf("%s: missing endpoint\n", sc->sc_dev.dv_xname); 127 usbd_deactivate(sc->sc_udev); 128 return; 129 } 130 131 uca.ibufsize = USCOMBUFSZ; 132 uca.obufsize = USCOMBUFSZ; 133 uca.ibufsizepad = USCOMBUFSZ; 134 uca.opkthdrlen = 0; 135 uca.device = sc->sc_udev; 136 uca.iface = sc->sc_iface; 137 uca.methods = &uscom_methods; 138 uca.arg = sc; 139 uca.info = NULL; 140 141 sc->sc_subdev = config_found_sm(self, &uca, ucomprint, ucomsubmatch); 142 } 143 144 int 145 uscom_detach(struct device *self, int flags) 146 { 147 struct uscom_softc *sc = (struct uscom_softc *)self; 148 int rv = 0; 149 150 if (sc->sc_subdev != NULL) { 151 rv = config_detach(sc->sc_subdev, flags); 152 sc->sc_subdev = NULL; 153 } 154 155 return (rv); 156 } 157