1 /* 2 * Copyright (c) 2008 AnyWi Technologies 3 * Author: Andrea Guzzo <aguzzo@anywi.com> 4 * * based on uark.c 1.1 2006/08/14 08:30:22 jsg * 5 * * parts from ubsa.c 183348 2008-09-25 12:00:56Z phk * 6 * 7 * Permission to use, copy, modify, and distribute this software for any 8 * purpose with or without fee is hereby granted, provided that the above 9 * copyright notice and this permission notice appear in all copies. 10 * 11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 18 * 19 * $FreeBSD$ 20 */ 21 22 /* 23 * NOTE: 24 * 25 * - The detour through the tty layer is ridiculously expensive wrt 26 * buffering due to the high speeds. 27 * 28 * We should consider adding a simple r/w device which allows 29 * attaching of PPP in a more efficient way. 30 * 31 */ 32 33 #include "usbdevs.h" 34 #include <dev/usb/usb.h> 35 #include <dev/usb/usb_mfunc.h> 36 #include <dev/usb/usb_error.h> 37 #include <dev/usb/usb_defs.h> 38 39 #define USB_DEBUG_VAR u3g_debug 40 41 #include <dev/usb/usb_core.h> 42 #include <dev/usb/usb_debug.h> 43 #include <dev/usb/usb_process.h> 44 #include <dev/usb/usb_request.h> 45 #include <dev/usb/usb_lookup.h> 46 #include <dev/usb/usb_util.h> 47 #include <dev/usb/usb_busdma.h> 48 #include <dev/usb/usb_msctest.h> 49 #include <dev/usb/usb_dynamic.h> 50 #include <dev/usb/usb_device.h> 51 52 #include <dev/usb/serial/usb_serial.h> 53 54 #if USB_DEBUG 55 static int u3g_debug = 0; 56 57 SYSCTL_NODE(_hw_usb2, OID_AUTO, u3g, CTLFLAG_RW, 0, "USB 3g"); 58 SYSCTL_INT(_hw_usb2_u3g, OID_AUTO, debug, CTLFLAG_RW, 59 &u3g_debug, 0, "Debug level"); 60 #endif 61 62 #define U3G_MAXPORTS 4 63 #define U3G_CONFIG_INDEX 0 64 #define U3G_BSIZE 2048 65 66 #define U3GSP_GPRS 0 67 #define U3GSP_EDGE 1 68 #define U3GSP_CDMA 2 69 #define U3GSP_UMTS 3 70 #define U3GSP_HSDPA 4 71 #define U3GSP_HSUPA 5 72 #define U3GSP_HSPA 6 73 #define U3GSP_MAX 7 74 75 #define U3GFL_NONE 0x0000 /* No flags */ 76 #define U3GFL_HUAWEI_INIT 0x0001 /* Init command required */ 77 #define U3GFL_SCSI_EJECT 0x0002 /* SCSI eject command required */ 78 #define U3GFL_SIERRA_INIT 0x0004 /* Init command required */ 79 #define U3GFL_SAEL_M460_INIT 0x0008 /* Init device */ 80 81 enum { 82 U3G_BULK_WR, 83 U3G_BULK_RD, 84 U3G_N_TRANSFER, 85 }; 86 87 struct u3g_softc { 88 struct usb2_com_super_softc sc_super_ucom; 89 struct usb2_com_softc sc_ucom[U3G_MAXPORTS]; 90 91 struct usb2_xfer *sc_xfer[U3G_MAXPORTS][U3G_N_TRANSFER]; 92 struct usb2_device *sc_udev; 93 struct mtx sc_mtx; 94 95 uint8_t sc_lsr; /* local status register */ 96 uint8_t sc_msr; /* U3G status register */ 97 uint8_t sc_numports; 98 }; 99 100 static device_probe_t u3g_probe; 101 static device_attach_t u3g_attach; 102 static device_detach_t u3g_detach; 103 104 static usb2_callback_t u3g_write_callback; 105 static usb2_callback_t u3g_read_callback; 106 107 static void u3g_start_read(struct usb2_com_softc *ucom); 108 static void u3g_stop_read(struct usb2_com_softc *ucom); 109 static void u3g_start_write(struct usb2_com_softc *ucom); 110 static void u3g_stop_write(struct usb2_com_softc *ucom); 111 112 static int u3g_driver_loaded(struct module *mod, int what, void *arg); 113 114 static const struct usb2_config u3g_config[U3G_N_TRANSFER] = { 115 116 [U3G_BULK_WR] = { 117 .type = UE_BULK, 118 .endpoint = UE_ADDR_ANY, 119 .direction = UE_DIR_OUT, 120 .mh.bufsize = U3G_BSIZE,/* bytes */ 121 .mh.flags = {.pipe_bof = 1,.force_short_xfer = 1,}, 122 .mh.callback = &u3g_write_callback, 123 }, 124 125 [U3G_BULK_RD] = { 126 .type = UE_BULK, 127 .endpoint = UE_ADDR_ANY, 128 .direction = UE_DIR_IN, 129 .mh.bufsize = U3G_BSIZE,/* bytes */ 130 .mh.flags = {.pipe_bof = 1,.short_xfer_ok = 1,}, 131 .mh.callback = &u3g_read_callback, 132 }, 133 }; 134 135 static const struct usb2_com_callback u3g_callback = { 136 .usb2_com_start_read = &u3g_start_read, 137 .usb2_com_stop_read = &u3g_stop_read, 138 .usb2_com_start_write = &u3g_start_write, 139 .usb2_com_stop_write = &u3g_stop_write, 140 }; 141 142 static device_method_t u3g_methods[] = { 143 DEVMETHOD(device_probe, u3g_probe), 144 DEVMETHOD(device_attach, u3g_attach), 145 DEVMETHOD(device_detach, u3g_detach), 146 {0, 0} 147 }; 148 149 static devclass_t u3g_devclass; 150 151 static driver_t u3g_driver = { 152 .name = "u3g", 153 .methods = u3g_methods, 154 .size = sizeof(struct u3g_softc), 155 }; 156 157 DRIVER_MODULE(u3g, uhub, u3g_driver, u3g_devclass, u3g_driver_loaded, 0); 158 MODULE_DEPEND(u3g, ucom, 1, 1, 1); 159 MODULE_DEPEND(u3g, usb, 1, 1, 1); 160 161 static const struct usb2_device_id u3g_devs[] = { 162 /* OEM: Option */ 163 {USB_VPI(USB_VENDOR_OPTION, USB_PRODUCT_OPTION_GT3G, U3GFL_NONE)}, 164 {USB_VPI(USB_VENDOR_OPTION, USB_PRODUCT_OPTION_GT3GQUAD, U3GFL_NONE)}, 165 {USB_VPI(USB_VENDOR_OPTION, USB_PRODUCT_OPTION_GT3GPLUS, U3GFL_NONE)}, 166 {USB_VPI(USB_VENDOR_OPTION, USB_PRODUCT_OPTION_GTMAX36, U3GFL_NONE)}, 167 {USB_VPI(USB_VENDOR_OPTION, USB_PRODUCT_OPTION_GTMAXHSUPA, U3GFL_NONE)}, 168 {USB_VPI(USB_VENDOR_OPTION, USB_PRODUCT_OPTION_VODAFONEMC3G, U3GFL_NONE)}, 169 /* OEM: Qualcomm, Inc. */ 170 {USB_VPI(USB_VENDOR_QUALCOMMINC, USB_PRODUCT_QUALCOMMINC_ZTE_STOR, U3GFL_SCSI_EJECT)}, 171 {USB_VPI(USB_VENDOR_QUALCOMMINC, USB_PRODUCT_QUALCOMMINC_CDMA_MSM, U3GFL_SCSI_EJECT)}, 172 /* OEM: Huawei */ 173 {USB_VPI(USB_VENDOR_HUAWEI, USB_PRODUCT_HUAWEI_MOBILE, U3GFL_HUAWEI_INIT)}, 174 {USB_VPI(USB_VENDOR_HUAWEI, USB_PRODUCT_HUAWEI_E220, U3GFL_HUAWEI_INIT)}, 175 /* OEM: Novatel */ 176 {USB_VPI(USB_VENDOR_NOVATEL, USB_PRODUCT_NOVATEL_CDMA_MODEM, U3GFL_SCSI_EJECT)}, 177 {USB_VPI(USB_VENDOR_NOVATEL, USB_PRODUCT_NOVATEL_ES620, U3GFL_SCSI_EJECT)}, 178 {USB_VPI(USB_VENDOR_NOVATEL, USB_PRODUCT_NOVATEL_MC950D, U3GFL_SCSI_EJECT)}, 179 {USB_VPI(USB_VENDOR_NOVATEL, USB_PRODUCT_NOVATEL_U720, U3GFL_SCSI_EJECT)}, 180 {USB_VPI(USB_VENDOR_NOVATEL, USB_PRODUCT_NOVATEL_U727, U3GFL_SCSI_EJECT)}, 181 {USB_VPI(USB_VENDOR_NOVATEL, USB_PRODUCT_NOVATEL_U740, U3GFL_SCSI_EJECT)}, 182 {USB_VPI(USB_VENDOR_NOVATEL, USB_PRODUCT_NOVATEL_U740_2, U3GFL_SCSI_EJECT)}, 183 {USB_VPI(USB_VENDOR_NOVATEL, USB_PRODUCT_NOVATEL_U870, U3GFL_SCSI_EJECT)}, 184 {USB_VPI(USB_VENDOR_NOVATEL, USB_PRODUCT_NOVATEL_V620, U3GFL_SCSI_EJECT)}, 185 {USB_VPI(USB_VENDOR_NOVATEL, USB_PRODUCT_NOVATEL_V640, U3GFL_SCSI_EJECT)}, 186 {USB_VPI(USB_VENDOR_NOVATEL, USB_PRODUCT_NOVATEL_V720, U3GFL_SCSI_EJECT)}, 187 {USB_VPI(USB_VENDOR_NOVATEL, USB_PRODUCT_NOVATEL_V740, U3GFL_SCSI_EJECT)}, 188 {USB_VPI(USB_VENDOR_NOVATEL, USB_PRODUCT_NOVATEL_X950D, U3GFL_SCSI_EJECT)}, 189 {USB_VPI(USB_VENDOR_NOVATEL, USB_PRODUCT_NOVATEL_XU870, U3GFL_SCSI_EJECT)}, 190 {USB_VPI(USB_VENDOR_NOVATEL, USB_PRODUCT_NOVATEL_ZEROCD, U3GFL_SCSI_EJECT)}, 191 {USB_VPI(USB_VENDOR_DELL, USB_PRODUCT_DELL_U740, U3GFL_SCSI_EJECT)}, 192 /* OEM: Merlin */ 193 {USB_VPI(USB_VENDOR_MERLIN, USB_PRODUCT_MERLIN_V620, U3GFL_NONE)}, 194 /* OEM: Sierra Wireless: */ 195 {USB_VPI(USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AIRCARD580, U3GFL_NONE)}, 196 {USB_VPI(USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AIRCARD595, U3GFL_NONE)}, 197 {USB_VPI(USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AC595U, U3GFL_NONE)}, 198 {USB_VPI(USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AC597E, U3GFL_NONE)}, 199 {USB_VPI(USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_C597, U3GFL_NONE)}, 200 {USB_VPI(USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AC880, U3GFL_NONE)}, 201 {USB_VPI(USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AC880E, U3GFL_NONE)}, 202 {USB_VPI(USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AC880U, U3GFL_NONE)}, 203 {USB_VPI(USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AC881, U3GFL_NONE)}, 204 {USB_VPI(USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AC881E, U3GFL_NONE)}, 205 {USB_VPI(USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AC881U, U3GFL_NONE)}, 206 {USB_VPI(USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_EM5625, U3GFL_NONE)}, 207 {USB_VPI(USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC5720, U3GFL_NONE)}, 208 {USB_VPI(USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC5720_2, U3GFL_NONE)}, 209 {USB_VPI(USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC5725, U3GFL_NONE)}, 210 {USB_VPI(USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MINI5725, U3GFL_NONE)}, 211 {USB_VPI(USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AIRCARD875, U3GFL_NONE)}, 212 {USB_VPI(USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC8755, U3GFL_NONE)}, 213 {USB_VPI(USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC8755_2, U3GFL_NONE)}, 214 {USB_VPI(USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC8755_3, U3GFL_NONE)}, 215 {USB_VPI(USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC8765, U3GFL_NONE)}, 216 {USB_VPI(USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AC875U, U3GFL_NONE)}, 217 {USB_VPI(USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC8775_2, U3GFL_NONE)}, 218 {USB_VPI(USB_VENDOR_HP, USB_PRODUCT_HP_HS2300, U3GFL_NONE)}, 219 {USB_VPI(USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC8780, U3GFL_NONE)}, 220 {USB_VPI(USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC8781, U3GFL_NONE)}, 221 {USB_VPI(USB_VENDOR_HP, USB_PRODUCT_HP_HS2300, U3GFL_NONE)}, 222 /* Sierra TruInstaller device ID */ 223 {USB_VPI(USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_TRUINSTALL, U3GFL_SIERRA_INIT)}, 224 /* PRUEBA SILABS */ 225 {USB_VPI(USB_VENDOR_SILABS, USB_PRODUCT_SILABS_SAEL, U3GFL_SAEL_M460_INIT)}, 226 }; 227 228 static void 229 u3g_sierra_init(struct usb2_device *udev) 230 { 231 struct usb2_device_request req; 232 233 DPRINTFN(0, "\n"); 234 235 req.bmRequestType = UT_VENDOR; 236 req.bRequest = UR_SET_INTERFACE; 237 USETW(req.wValue, UF_DEVICE_REMOTE_WAKEUP); 238 USETW(req.wIndex, UHF_PORT_CONNECTION); 239 USETW(req.wLength, 0); 240 241 if (usb2_do_request_flags(udev, NULL, &req, 242 NULL, 0, NULL, USB_MS_HZ)) { 243 /* ignore any errors */ 244 } 245 return; 246 } 247 248 static void 249 u3g_huawei_init(struct usb2_device *udev) 250 { 251 struct usb2_device_request req; 252 253 DPRINTFN(0, "\n"); 254 255 req.bmRequestType = UT_WRITE_DEVICE; 256 req.bRequest = UR_SET_FEATURE; 257 USETW(req.wValue, UF_DEVICE_REMOTE_WAKEUP); 258 USETW(req.wIndex, UHF_PORT_SUSPEND); 259 USETW(req.wLength, 0); 260 261 if (usb2_do_request_flags(udev, NULL, &req, 262 NULL, 0, NULL, USB_MS_HZ)) { 263 /* ignore any errors */ 264 } 265 return; 266 } 267 268 static void 269 u3g_sael_m460_init(struct usb2_device *udev) 270 { 271 static const uint8_t setup[][24] = { 272 { 0x41, 0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, 273 { 0x41, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00 }, 274 { 0x41, 0x13, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 275 0x01, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 276 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, 277 { 0xc1, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x40, 0x02 }, 278 { 0xc1, 0x08, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00 }, 279 { 0x41, 0x07, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00 }, 280 { 0xc1, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02 }, 281 { 0x41, 0x01, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00 }, 282 { 0x41, 0x07, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00 }, 283 { 0x41, 0x03, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00 }, 284 { 0x41, 0x19, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 285 0x00, 0x00, 0x00, 0x00, 0x11, 0x13 }, 286 { 0x41, 0x13, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 287 0x09, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 288 0x0a, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00 }, 289 { 0x41, 0x12, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00 }, 290 { 0x41, 0x01, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00 }, 291 { 0x41, 0x07, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00 }, 292 { 0x41, 0x03, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00 }, 293 { 0x41, 0x19, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 294 0x00, 0x00, 0x00, 0x00, 0x11, 0x13 }, 295 { 0x41, 0x13, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 296 0x09, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 297 0x0a, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00 }, 298 { 0x41, 0x07, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00 }, 299 }; 300 301 struct usb2_device_request req; 302 uint16_t len; 303 uint8_t buf[0x300]; 304 uint8_t n; 305 306 DPRINTFN(1, "\n"); 307 308 if (usb2_req_set_alt_interface_no(udev, NULL, 0, 0)) { 309 DPRINTFN(0, "Alt setting 0 failed\n"); 310 return; 311 } 312 313 for (n = 0; n != (sizeof(setup)/sizeof(setup[0])); n++) { 314 315 memcpy(&req, setup[n], sizeof(req)); 316 317 len = UGETW(req.wLength); 318 if (req.bmRequestType & UE_DIR_IN) { 319 if (len > sizeof(buf)) { 320 DPRINTFN(0, "too small buffer\n"); 321 continue; 322 } 323 if (usb2_do_request(udev, NULL, &req, buf)) { 324 DPRINTFN(0, "request %u failed\n", 325 (unsigned int)n); 326 break; 327 } 328 } else { 329 if (len > (sizeof(setup[0]) - 8)) { 330 DPRINTFN(0, "too small buffer\n"); 331 continue; 332 } 333 if (usb2_do_request(udev, NULL, &req, 334 __DECONST(uint8_t *, &setup[n][8]))) { 335 DPRINTFN(0, "request %u failed\n", 336 (unsigned int)n); 337 break; 338 } 339 } 340 } 341 return; 342 } 343 344 static int 345 u3g_lookup_huawei(struct usb2_attach_arg *uaa) 346 { 347 /* Calling the lookup function will also set the driver info! */ 348 return (usb2_lookup_id_by_uaa(u3g_devs, sizeof(u3g_devs), uaa)); 349 } 350 351 /* 352 * The following function handles 3G modem devices (E220, Mobile, 353 * etc.) with auto-install flash disks for Windows/MacOSX on the first 354 * interface. After some command or some delay they change appearance 355 * to a modem. 356 */ 357 static usb2_error_t 358 u3g_test_huawei_autoinst(struct usb2_device *udev, 359 struct usb2_attach_arg *uaa) 360 { 361 struct usb2_interface *iface; 362 struct usb2_interface_descriptor *id; 363 uint32_t flags; 364 365 if (udev == NULL) { 366 return (USB_ERR_INVAL); 367 } 368 iface = usb2_get_iface(udev, 0); 369 if (iface == NULL) { 370 return (USB_ERR_INVAL); 371 } 372 id = iface->idesc; 373 if (id == NULL) { 374 return (USB_ERR_INVAL); 375 } 376 if (id->bInterfaceClass != UICLASS_MASS) { 377 return (USB_ERR_INVAL); 378 } 379 if (u3g_lookup_huawei(uaa)) { 380 /* no device match */ 381 return (USB_ERR_INVAL); 382 } 383 flags = USB_GET_DRIVER_INFO(uaa); 384 385 if (flags & U3GFL_HUAWEI_INIT) { 386 u3g_huawei_init(udev); 387 } else if (flags & U3GFL_SCSI_EJECT) { 388 return (usb2_test_autoinstall(udev, 0, 1)); 389 } else if (flags & U3GFL_SIERRA_INIT) { 390 u3g_sierra_init(udev); 391 } else { 392 /* no quirks */ 393 return (USB_ERR_INVAL); 394 } 395 return (0); /* success */ 396 } 397 398 static int 399 u3g_driver_loaded(struct module *mod, int what, void *arg) 400 { 401 switch (what) { 402 case MOD_LOAD: 403 /* register our autoinstall handler */ 404 usb2_test_huawei_autoinst_p = &u3g_test_huawei_autoinst; 405 break; 406 case MOD_UNLOAD: 407 usb2_test_huawei_unload(NULL); 408 break; 409 default: 410 return (EOPNOTSUPP); 411 } 412 return (0); 413 } 414 415 static int 416 u3g_probe(device_t self) 417 { 418 struct usb2_attach_arg *uaa = device_get_ivars(self); 419 420 if (uaa->usb2_mode != USB_MODE_HOST) { 421 return (ENXIO); 422 } 423 if (uaa->info.bConfigIndex != U3G_CONFIG_INDEX) { 424 return (ENXIO); 425 } 426 if (uaa->info.bInterfaceClass != UICLASS_VENDOR) { 427 return (ENXIO); 428 } 429 return (u3g_lookup_huawei(uaa)); 430 } 431 432 static int 433 u3g_attach(device_t dev) 434 { 435 struct usb2_config u3g_config_tmp[U3G_N_TRANSFER]; 436 struct usb2_attach_arg *uaa = device_get_ivars(dev); 437 struct u3g_softc *sc = device_get_softc(dev); 438 struct usb2_interface *iface; 439 struct usb2_interface_descriptor *id; 440 uint32_t flags; 441 uint8_t m; 442 uint8_t n; 443 uint8_t i; 444 uint8_t x; 445 int error; 446 447 DPRINTF("sc=%p\n", sc); 448 449 flags = USB_GET_DRIVER_INFO(uaa); 450 451 if (flags & U3GFL_SAEL_M460_INIT) 452 u3g_sael_m460_init(uaa->device); 453 454 /* copy in USB config */ 455 for (n = 0; n != U3G_N_TRANSFER; n++) 456 u3g_config_tmp[n] = u3g_config[n]; 457 458 device_set_usb2_desc(dev); 459 mtx_init(&sc->sc_mtx, "u3g", NULL, MTX_DEF); 460 461 sc->sc_udev = uaa->device; 462 463 x = 0; /* interface index */ 464 i = 0; /* endpoint index */ 465 m = 0; /* number of ports */ 466 467 while (m != U3G_MAXPORTS) { 468 469 /* update BULK endpoint index */ 470 for (n = 0; n != U3G_N_TRANSFER; n++) 471 u3g_config_tmp[n].ep_index = i; 472 473 iface = usb2_get_iface(uaa->device, x); 474 if (iface == NULL) { 475 if (m != 0) 476 break; /* end of interfaces */ 477 DPRINTF("did not find any modem endpoints\n"); 478 goto detach; 479 } 480 481 id = usb2_get_interface_descriptor(iface); 482 if ((id == NULL) || 483 (id->bInterfaceClass != UICLASS_VENDOR)) { 484 /* next interface */ 485 x++; 486 i = 0; 487 continue; 488 } 489 490 /* try to allocate a set of BULK endpoints */ 491 error = usb2_transfer_setup(uaa->device, &x, 492 sc->sc_xfer[m], u3g_config_tmp, U3G_N_TRANSFER, 493 &sc->sc_ucom[m], &sc->sc_mtx); 494 if (error) { 495 /* next interface */ 496 x++; 497 i = 0; 498 continue; 499 } 500 501 /* grab other interface, if any */ 502 if (x != uaa->info.bIfaceIndex) 503 usb2_set_parent_iface(uaa->device, x, 504 uaa->info.bIfaceIndex); 505 506 /* set stall by default */ 507 mtx_lock(&sc->sc_mtx); 508 usb2_transfer_set_stall(sc->sc_xfer[m][U3G_BULK_WR]); 509 usb2_transfer_set_stall(sc->sc_xfer[m][U3G_BULK_RD]); 510 mtx_unlock(&sc->sc_mtx); 511 512 m++; /* found one port */ 513 i++; /* next endpoint index */ 514 } 515 516 sc->sc_numports = m; 517 518 error = usb2_com_attach(&sc->sc_super_ucom, sc->sc_ucom, 519 sc->sc_numports, sc, &u3g_callback, &sc->sc_mtx); 520 if (error) { 521 DPRINTF("usb2_com_attach failed\n"); 522 goto detach; 523 } 524 if (sc->sc_numports != 1) { 525 /* be verbose */ 526 device_printf(dev, "Found %u ports.\n", 527 (unsigned int)sc->sc_numports); 528 } 529 return (0); 530 531 detach: 532 u3g_detach(dev); 533 return (ENXIO); 534 } 535 536 static int 537 u3g_detach(device_t dev) 538 { 539 struct u3g_softc *sc = device_get_softc(dev); 540 uint8_t m; 541 542 DPRINTF("sc=%p\n", sc); 543 544 /* NOTE: It is not dangerous to detach more ports than attached! */ 545 usb2_com_detach(&sc->sc_super_ucom, sc->sc_ucom, U3G_MAXPORTS); 546 547 for (m = 0; m != U3G_MAXPORTS; m++) 548 usb2_transfer_unsetup(sc->sc_xfer[m], U3G_N_TRANSFER); 549 mtx_destroy(&sc->sc_mtx); 550 551 return (0); 552 } 553 554 static void 555 u3g_start_read(struct usb2_com_softc *ucom) 556 { 557 struct u3g_softc *sc = ucom->sc_parent; 558 559 /* start read endpoint */ 560 usb2_transfer_start(sc->sc_xfer[ucom->sc_local_unit][U3G_BULK_RD]); 561 return; 562 } 563 564 static void 565 u3g_stop_read(struct usb2_com_softc *ucom) 566 { 567 struct u3g_softc *sc = ucom->sc_parent; 568 569 /* stop read endpoint */ 570 usb2_transfer_stop(sc->sc_xfer[ucom->sc_local_unit][U3G_BULK_RD]); 571 return; 572 } 573 574 static void 575 u3g_start_write(struct usb2_com_softc *ucom) 576 { 577 struct u3g_softc *sc = ucom->sc_parent; 578 579 usb2_transfer_start(sc->sc_xfer[ucom->sc_local_unit][U3G_BULK_WR]); 580 return; 581 } 582 583 static void 584 u3g_stop_write(struct usb2_com_softc *ucom) 585 { 586 struct u3g_softc *sc = ucom->sc_parent; 587 588 usb2_transfer_stop(sc->sc_xfer[ucom->sc_local_unit][U3G_BULK_WR]); 589 return; 590 } 591 592 static void 593 u3g_write_callback(struct usb2_xfer *xfer) 594 { 595 struct usb2_com_softc *ucom = xfer->priv_sc; 596 uint32_t actlen; 597 598 switch (USB_GET_STATE(xfer)) { 599 case USB_ST_TRANSFERRED: 600 case USB_ST_SETUP: 601 tr_setup: 602 if (usb2_com_get_data(ucom, xfer->frbuffers, 0, 603 U3G_BSIZE, &actlen)) { 604 xfer->frlengths[0] = actlen; 605 usb2_start_hardware(xfer); 606 } 607 break; 608 609 default: /* Error */ 610 if (xfer->error != USB_ERR_CANCELLED) { 611 /* do a builtin clear-stall */ 612 xfer->flags.stall_pipe = 1; 613 goto tr_setup; 614 } 615 break; 616 } 617 return; 618 } 619 620 static void 621 u3g_read_callback(struct usb2_xfer *xfer) 622 { 623 struct usb2_com_softc *ucom = xfer->priv_sc; 624 625 switch (USB_GET_STATE(xfer)) { 626 case USB_ST_TRANSFERRED: 627 usb2_com_put_data(ucom, xfer->frbuffers, 0, xfer->actlen); 628 629 case USB_ST_SETUP: 630 tr_setup: 631 xfer->frlengths[0] = xfer->max_data_length; 632 usb2_start_hardware(xfer); 633 break; 634 635 default: /* Error */ 636 if (xfer->error != USB_ERR_CANCELLED) { 637 /* do a builtin clear-stall */ 638 xfer->flags.stall_pipe = 1; 639 goto tr_setup; 640 } 641 break; 642 } 643 return; 644 } 645