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