1 1.43 thorpej /* $NetBSD: ubsa.c,v 1.43 2021/08/07 16:19:17 thorpej Exp $ */ 2 1.38 mrg 3 1.1 augustss /*- 4 1.1 augustss * Copyright (c) 2002, Alexander Kabaev <kan.FreeBSD.org>. 5 1.1 augustss * All rights reserved. 6 1.1 augustss * 7 1.1 augustss * Redistribution and use in source and binary forms, with or without 8 1.1 augustss * modification, are permitted provided that the following conditions 9 1.1 augustss * are met: 10 1.1 augustss * 1. Redistributions of source code must retain the above copyright 11 1.1 augustss * notice, this list of conditions and the following disclaimer. 12 1.1 augustss * 2. Redistributions in binary form must reproduce the above copyright 13 1.1 augustss * notice, this list of conditions and the following disclaimer in the 14 1.1 augustss * documentation and/or other materials provided with the distribution. 15 1.1 augustss * 16 1.1 augustss * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 1.1 augustss * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 1.1 augustss * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 1.1 augustss * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 1.1 augustss * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 1.1 augustss * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 1.1 augustss * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 1.1 augustss * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 1.1 augustss * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 1.1 augustss * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 1.1 augustss * SUCH DAMAGE. 27 1.1 augustss */ 28 1.1 augustss /* 29 1.1 augustss * Copyright (c) 2001 The NetBSD Foundation, Inc. 30 1.1 augustss * All rights reserved. 31 1.1 augustss * 32 1.1 augustss * This code is derived from software contributed to The NetBSD Foundation 33 1.1 augustss * by Ichiro FUKUHARA (ichiro (at) ichiro.org). 34 1.1 augustss * 35 1.1 augustss * Redistribution and use in source and binary forms, with or without 36 1.1 augustss * modification, are permitted provided that the following conditions 37 1.1 augustss * are met: 38 1.1 augustss * 1. Redistributions of source code must retain the above copyright 39 1.1 augustss * notice, this list of conditions and the following disclaimer. 40 1.1 augustss * 2. Redistributions in binary form must reproduce the above copyright 41 1.1 augustss * notice, this list of conditions and the following disclaimer in the 42 1.1 augustss * documentation and/or other materials provided with the distribution. 43 1.1 augustss * 44 1.1 augustss * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 45 1.1 augustss * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 46 1.1 augustss * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 47 1.1 augustss * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 48 1.1 augustss * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 49 1.1 augustss * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 50 1.1 augustss * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 51 1.1 augustss * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 52 1.1 augustss * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 53 1.1 augustss * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 54 1.1 augustss * POSSIBILITY OF SUCH DAMAGE. 55 1.1 augustss */ 56 1.1 augustss 57 1.1 augustss #include <sys/cdefs.h> 58 1.43 thorpej __KERNEL_RCSID(0, "$NetBSD: ubsa.c,v 1.43 2021/08/07 16:19:17 thorpej Exp $"); 59 1.33 skrll 60 1.33 skrll #ifdef _KERNEL_OPT 61 1.33 skrll #include "opt_usb.h" 62 1.33 skrll #endif 63 1.1 augustss 64 1.1 augustss #include <sys/param.h> 65 1.1 augustss #include <sys/systm.h> 66 1.1 augustss #include <sys/kernel.h> 67 1.31 skrll #include <sys/kmem.h> 68 1.1 augustss #include <sys/ioccom.h> 69 1.1 augustss #include <sys/fcntl.h> 70 1.1 augustss #include <sys/conf.h> 71 1.1 augustss #include <sys/tty.h> 72 1.1 augustss #include <sys/file.h> 73 1.1 augustss #include <sys/select.h> 74 1.1 augustss #include <sys/proc.h> 75 1.4 fvdl #include <sys/device.h> 76 1.1 augustss #include <sys/poll.h> 77 1.1 augustss #include <sys/sysctl.h> 78 1.1 augustss 79 1.1 augustss #include <dev/usb/usb.h> 80 1.1 augustss #include <dev/usb/usbcdc.h> 81 1.1 augustss 82 1.1 augustss #include <dev/usb/usbdi.h> 83 1.1 augustss #include <dev/usb/usbdi_util.h> 84 1.1 augustss #include <dev/usb/usbdevs.h> 85 1.1 augustss #include <dev/usb/usb_quirks.h> 86 1.1 augustss 87 1.1 augustss #include <dev/usb/ucomvar.h> 88 1.20 ichiro #include <dev/usb/ubsavar.h> 89 1.1 augustss 90 1.1 augustss #ifdef UBSA_DEBUG 91 1.27 rmind int ubsadebug = 0; 92 1.1 augustss 93 1.1 augustss #define DPRINTFN(n, x) do { \ 94 1.1 augustss if (ubsadebug > (n)) \ 95 1.26 dyoung printf x; \ 96 1.1 augustss } while (0) 97 1.1 augustss #else 98 1.1 augustss #define DPRINTFN(n, x) 99 1.1 augustss #endif 100 1.1 augustss #define DPRINTF(x) DPRINTFN(0, x) 101 1.1 augustss 102 1.39 maxv static const struct ucom_methods ubsa_methods = { 103 1.31 skrll .ucom_get_status = ubsa_get_status, 104 1.31 skrll .ucom_set = ubsa_set, 105 1.31 skrll .ucom_param = ubsa_param, 106 1.31 skrll .ucom_open = ubsa_open, 107 1.31 skrll .ucom_close = ubsa_close, 108 1.1 augustss }; 109 1.1 augustss 110 1.42 mlelstv Static const struct ubsa_type { 111 1.42 mlelstv struct usb_devno ubsa_dev; 112 1.42 mlelstv int ubsa_quadumts; 113 1.42 mlelstv } ubsa_devs[] = { 114 1.1 augustss /* BELKIN F5U103 */ 115 1.42 mlelstv { { USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5U103 }, 0 }, 116 1.1 augustss /* BELKIN F5U120 */ 117 1.42 mlelstv { { USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5U120 }, 0 }, 118 1.1 augustss /* GoHubs GO-COM232 */ 119 1.42 mlelstv { { USB_VENDOR_ETEK, USB_PRODUCT_ETEK_1COM }, 0 }, 120 1.1 augustss /* GoHubs GO-COM232 */ 121 1.42 mlelstv { { USB_VENDOR_GOHUBS, USB_PRODUCT_GOHUBS_GOCOM232 }, 0 }, 122 1.1 augustss /* Peracom */ 123 1.42 mlelstv { { USB_VENDOR_PERACOM, USB_PRODUCT_PERACOM_SERIAL1 }, 0 }, 124 1.17 mlelstv /* Option N.V. */ 125 1.42 mlelstv { { USB_VENDOR_OPTIONNV, USB_PRODUCT_OPTIONNV_MC3G }, 0 }, 126 1.42 mlelstv { { USB_VENDOR_OPTIONNV, USB_PRODUCT_OPTIONNV_QUADUMTS2 }, 1 }, 127 1.42 mlelstv { { USB_VENDOR_OPTIONNV, USB_PRODUCT_OPTIONNV_QUADUMTS }, 1 }, 128 1.42 mlelstv { { USB_VENDOR_OPTIONNV, USB_PRODUCT_OPTIONNV_QUADPLUSUMTS }, 1 }, 129 1.42 mlelstv { { USB_VENDOR_OPTIONNV, USB_PRODUCT_OPTIONNV_HSDPA }, 1 }, 130 1.1 augustss }; 131 1.42 mlelstv #define ubsa_lookup(v, p) ((const struct ubsa_type *)usb_lookup(ubsa_devs, v, p)) 132 1.1 augustss 133 1.23 cube int ubsa_match(device_t, cfdata_t, void *); 134 1.21 dyoung void ubsa_attach(device_t, device_t, void *); 135 1.21 dyoung void ubsa_childdet(device_t, device_t); 136 1.21 dyoung int ubsa_detach(device_t, int); 137 1.21 dyoung 138 1.23 cube CFATTACH_DECL2_NEW(ubsa, sizeof(struct ubsa_softc), 139 1.38 mrg ubsa_match, ubsa_attach, ubsa_detach, NULL, NULL, ubsa_childdet); 140 1.1 augustss 141 1.31 skrll int 142 1.26 dyoung ubsa_match(device_t parent, cfdata_t match, void *aux) 143 1.1 augustss { 144 1.26 dyoung struct usb_attach_arg *uaa = aux; 145 1.1 augustss 146 1.31 skrll return (ubsa_lookup(uaa->uaa_vendor, uaa->uaa_product) != NULL ? 147 1.1 augustss UMATCH_VENDOR_PRODUCT : UMATCH_NONE); 148 1.1 augustss } 149 1.1 augustss 150 1.31 skrll void 151 1.26 dyoung ubsa_attach(device_t parent, device_t self, void *aux) 152 1.1 augustss { 153 1.26 dyoung struct ubsa_softc *sc = device_private(self); 154 1.26 dyoung struct usb_attach_arg *uaa = aux; 155 1.31 skrll struct usbd_device *dev = uaa->uaa_device; 156 1.1 augustss usb_config_descriptor_t *cdesc; 157 1.1 augustss usb_interface_descriptor_t *id; 158 1.1 augustss usb_endpoint_descriptor_t *ed; 159 1.12 augustss char *devinfop; 160 1.1 augustss usbd_status err; 161 1.31 skrll struct ucom_attach_args ucaa; 162 1.1 augustss int i; 163 1.1 augustss 164 1.23 cube sc->sc_dev = self; 165 1.38 mrg sc->sc_dying = false; 166 1.24 plunky 167 1.24 plunky aprint_naive("\n"); 168 1.24 plunky aprint_normal("\n"); 169 1.24 plunky 170 1.12 augustss devinfop = usbd_devinfo_alloc(dev, 0); 171 1.23 cube aprint_normal_dev(self, "%s\n", devinfop); 172 1.12 augustss usbd_devinfo_free(devinfop); 173 1.1 augustss 174 1.1 augustss sc->sc_udev = dev; 175 1.20 ichiro sc->sc_config_index = UBSA_DEFAULT_CONFIG_INDEX; 176 1.20 ichiro sc->sc_numif = 1; /* default device has one interface */ 177 1.1 augustss 178 1.1 augustss /* 179 1.1 augustss * initialize rts, dtr variables to something 180 1.3 augustss * different from boolean 0, 1 181 1.1 augustss */ 182 1.1 augustss sc->sc_dtr = -1; 183 1.1 augustss sc->sc_rts = -1; 184 1.1 augustss 185 1.17 mlelstv /* 186 1.17 mlelstv * Quad UMTS cards use different requests to 187 1.17 mlelstv * control com settings and only some. 188 1.17 mlelstv */ 189 1.42 mlelstv sc->sc_quadumts = ubsa_lookup(uaa->uaa_vendor, uaa->uaa_product)->ubsa_quadumts; 190 1.17 mlelstv 191 1.1 augustss DPRINTF(("ubsa attach: sc = %p\n", sc)); 192 1.1 augustss 193 1.1 augustss /* Move the device into the configured state. */ 194 1.20 ichiro err = usbd_set_config_index(dev, sc->sc_config_index, 1); 195 1.1 augustss if (err) { 196 1.23 cube aprint_error_dev(self, 197 1.23 cube "failed to set configuration: %s\n", 198 1.23 cube usbd_errstr(err)); 199 1.1 augustss goto error; 200 1.1 augustss } 201 1.1 augustss 202 1.1 augustss /* get the config descriptor */ 203 1.1 augustss cdesc = usbd_get_config_descriptor(sc->sc_udev); 204 1.1 augustss 205 1.1 augustss if (cdesc == NULL) { 206 1.23 cube aprint_error_dev(self, 207 1.23 cube "failed to get configuration descriptor\n"); 208 1.1 augustss goto error; 209 1.1 augustss } 210 1.1 augustss 211 1.20 ichiro sc->sc_intr_number = -1; 212 1.20 ichiro sc->sc_intr_pipe = NULL; 213 1.20 ichiro 214 1.20 ichiro /* get the interfaces */ 215 1.20 ichiro err = usbd_device2interface_handle(dev, UBSA_IFACE_INDEX_OFFSET, 216 1.20 ichiro &sc->sc_iface[0]); 217 1.1 augustss if (err) { 218 1.20 ichiro /* can not get main interface */ 219 1.1 augustss goto error; 220 1.1 augustss } 221 1.1 augustss 222 1.1 augustss /* Find the endpoints */ 223 1.20 ichiro id = usbd_get_interface_descriptor(sc->sc_iface[0]); 224 1.20 ichiro sc->sc_iface_number[0] = id->bInterfaceNumber; 225 1.1 augustss 226 1.20 ichiro /* initialize endpoints */ 227 1.31 skrll ucaa.ucaa_bulkin = ucaa.ucaa_bulkout = -1; 228 1.1 augustss 229 1.1 augustss for (i = 0; i < id->bNumEndpoints; i++) { 230 1.20 ichiro ed = usbd_interface2endpoint_descriptor(sc->sc_iface[0], i); 231 1.1 augustss if (ed == NULL) { 232 1.23 cube aprint_error_dev(self, 233 1.23 cube "no endpoint descriptor for %d\n", i); 234 1.20 ichiro break; 235 1.1 augustss } 236 1.1 augustss 237 1.1 augustss if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN && 238 1.1 augustss UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) { 239 1.1 augustss sc->sc_intr_number = ed->bEndpointAddress; 240 1.1 augustss sc->sc_isize = UGETW(ed->wMaxPacketSize); 241 1.1 augustss } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN && 242 1.1 augustss UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) { 243 1.31 skrll ucaa.ucaa_bulkin = ed->bEndpointAddress; 244 1.31 skrll ucaa.ucaa_ibufsize = UGETW(ed->wMaxPacketSize); 245 1.1 augustss } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT && 246 1.1 augustss UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) { 247 1.31 skrll ucaa.ucaa_bulkout = ed->bEndpointAddress; 248 1.31 skrll ucaa.ucaa_obufsize = UGETW(ed->wMaxPacketSize); 249 1.1 augustss } 250 1.20 ichiro } /* end of Endpoint loop */ 251 1.1 augustss 252 1.1 augustss if (sc->sc_intr_number == -1) { 253 1.23 cube aprint_error_dev(self, "Could not find interrupt in\n"); 254 1.1 augustss goto error; 255 1.1 augustss } 256 1.1 augustss 257 1.31 skrll if (ucaa.ucaa_bulkin == -1) { 258 1.23 cube aprint_error_dev(self, "Could not find data bulk in\n"); 259 1.1 augustss goto error; 260 1.1 augustss } 261 1.1 augustss 262 1.31 skrll if (ucaa.ucaa_bulkout == -1) { 263 1.23 cube aprint_error_dev(self, "Could not find data bulk out\n"); 264 1.1 augustss goto error; 265 1.1 augustss } 266 1.1 augustss 267 1.31 skrll ucaa.ucaa_portno = 0; 268 1.1 augustss /* bulkin, bulkout set above */ 269 1.31 skrll ucaa.ucaa_ibufsizepad = ucaa.ucaa_ibufsize; 270 1.31 skrll ucaa.ucaa_opkthdrlen = 0; 271 1.31 skrll ucaa.ucaa_device = dev; 272 1.31 skrll ucaa.ucaa_iface = sc->sc_iface[0]; 273 1.31 skrll ucaa.ucaa_methods = &ubsa_methods; 274 1.31 skrll ucaa.ucaa_arg = sc; 275 1.31 skrll ucaa.ucaa_info = NULL; 276 1.40 christos DPRINTF(("ubsa: int#=%d, in = %#x, out = %#x, intr = %#x\n", 277 1.34 skrll i, ucaa.ucaa_bulkin, ucaa.ucaa_bulkout, sc->sc_intr_number)); 278 1.41 thorpej sc->sc_subdevs[0] = config_found(self, &ucaa, ucomprint, 279 1.43 thorpej CFARGS(.submatch = ucomsubmatch)); 280 1.1 augustss 281 1.32 msaitoh usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, sc->sc_dev); 282 1.1 augustss 283 1.26 dyoung return; 284 1.1 augustss 285 1.1 augustss error: 286 1.38 mrg sc->sc_dying = true; 287 1.26 dyoung return; 288 1.1 augustss } 289 1.1 augustss 290 1.21 dyoung 291 1.21 dyoung void 292 1.21 dyoung ubsa_childdet(device_t self, device_t child) 293 1.21 dyoung { 294 1.21 dyoung int i; 295 1.21 dyoung struct ubsa_softc *sc = device_private(self); 296 1.21 dyoung 297 1.21 dyoung for (i = 0; i < sc->sc_numif; i++) { 298 1.21 dyoung if (sc->sc_subdevs[i] == child) 299 1.21 dyoung break; 300 1.21 dyoung } 301 1.21 dyoung KASSERT(i < sc->sc_numif); 302 1.21 dyoung sc->sc_subdevs[i] = NULL; 303 1.21 dyoung } 304 1.21 dyoung 305 1.31 skrll int 306 1.26 dyoung ubsa_detach(device_t self, int flags) 307 1.1 augustss { 308 1.26 dyoung struct ubsa_softc *sc = device_private(self); 309 1.20 ichiro int i; 310 1.5 fvdl int rv = 0; 311 1.1 augustss 312 1.38 mrg DPRINTF(("ubsa_detach: sc = %p\n", sc)); 313 1.1 augustss 314 1.38 mrg sc->sc_dying = true; 315 1.1 augustss 316 1.38 mrg ubsa_close_pipe(sc); 317 1.1 augustss 318 1.20 ichiro for (i = 0; i < sc->sc_numif; i++) { 319 1.38 mrg if (sc->sc_subdevs[i] != NULL) { 320 1.20 ichiro rv |= config_detach(sc->sc_subdevs[i], flags); 321 1.38 mrg sc->sc_subdevs[i] = NULL; 322 1.38 mrg } 323 1.1 augustss } 324 1.1 augustss 325 1.32 msaitoh usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, sc->sc_dev); 326 1.1 augustss 327 1.35 skrll return rv; 328 1.1 augustss } 329