1 /* $NetBSD: u3g.c,v 1.31 2014/09/24 00:17:13 christos Exp $ */ 2 3 /*- 4 * Copyright (c) 2009 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 19 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 20 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 21 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 22 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28 * POSSIBILITY OF SUCH DAMAGE. 29 */ 30 31 /* 32 * Copyright (c) 2008 AnyWi Technologies 33 * Author: Andrea Guzzo <aguzzo@anywi.com> 34 * * based on uark.c 1.1 2006/08/14 08:30:22 jsg * 35 * * parts from ubsa.c 183348 2008-09-25 12:00:56Z phk * 36 * 37 * Permission to use, copy, modify, and distribute this software for any 38 * purpose with or without fee is hereby granted, provided that the above 39 * copyright notice and this permission notice appear in all copies. 40 * 41 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 42 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 43 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 44 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 45 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 46 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 47 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 48 * 49 * $FreeBSD$ 50 */ 51 52 #include <sys/cdefs.h> 53 __KERNEL_RCSID(0, "$NetBSD: u3g.c,v 1.31 2014/09/24 00:17:13 christos Exp $"); 54 55 #include <sys/param.h> 56 #include <sys/systm.h> 57 #include <sys/kernel.h> 58 #include <sys/malloc.h> 59 #include <sys/bus.h> 60 #include <sys/conf.h> 61 #include <sys/tty.h> 62 63 #include <dev/usb/usb.h> 64 #include <dev/usb/usbdi.h> 65 #include <dev/usb/usbdivar.h> 66 #include <dev/usb/usbdi_util.h> 67 68 #include <dev/usb/ucomvar.h> 69 70 #include "usbdevs.h" 71 72 /* 73 * We read/write data from/to the device in 4KB chunks to maximise 74 * performance. 75 */ 76 #define U3G_BUFF_SIZE 4096 77 78 /* 79 * Some 3G devices (the Huawei E160/E220 springs to mind here) buffer up 80 * data internally even when the USB pipes are closed. So on first open, 81 * we can receive a large chunk of stale data. 82 * 83 * This causes a real problem because the default TTYDEF_LFLAG (applied 84 * on first open) has the ECHO flag set, resulting in all the stale data 85 * being echoed straight back to the device by the tty(4) layer. Some 86 * devices (again, the Huawei E160/E220 for example) react to this spew 87 * by going catatonic. 88 * 89 * All this happens before the application gets a chance to disable ECHO. 90 * 91 * We work around this by ignoring all data received from the device for 92 * a period of two seconds, or until the application starts sending data - 93 * whichever comes first. 94 */ 95 #define U3G_PURGE_SECS 2 96 97 /* 98 * Define bits for the virtual modem control pins. 99 * The input pin states are reported via the interrupt pipe on some devices. 100 */ 101 #define U3G_OUTPIN_DTR (1u << 0) 102 #define U3G_OUTPIN_RTS (1u << 1) 103 #define U3G_INPIN_DCD (1u << 0) 104 #define U3G_INPIN_DSR (1u << 1) 105 #define U3G_INPIN_RI (1u << 3) 106 107 /* 108 * USB request to set the output pin status 109 */ 110 #define U3G_SET_PIN 0x22 111 112 struct u3g_softc { 113 device_t sc_dev; 114 usbd_device_handle sc_udev; 115 bool sc_dying; /* We're going away */ 116 int sc_ifaceno; /* Device interface number */ 117 118 struct u3g_com { 119 device_t c_dev; /* Child ucom(4) handle */ 120 121 bool c_open; /* Device is in use */ 122 bool c_purging; /* Purging stale data */ 123 struct timeval c_purge_start; /* Control duration of purge */ 124 125 u_char c_msr; /* Emulated 'msr' */ 126 uint16_t c_outpins; /* Output pin state */ 127 } sc_com[10]; 128 size_t sc_ncom; 129 130 usbd_pipe_handle sc_intr_pipe; /* Interrupt pipe */ 131 u_char *sc_intr_buff; /* Interrupt buffer */ 132 }; 133 134 /* 135 * The device driver has two personalities. The first uses the 'usbdevif' 136 * interface attribute so that a match will claim the entire USB device 137 * for itself. This is used for when a device needs to be mode-switched 138 * and ensures any other interfaces present cannot be claimed by other 139 * drivers while the mode-switch is in progress. 140 * 141 * The second personality uses the 'usbifif' interface attribute so that 142 * it can claim the 3G modem interfaces for itself, leaving others (such 143 * as the mass storage interfaces on some devices) for other drivers. 144 */ 145 static int u3ginit_match(device_t, cfdata_t, void *); 146 static void u3ginit_attach(device_t, device_t, void *); 147 static int u3ginit_detach(device_t, int); 148 149 CFATTACH_DECL2_NEW(u3ginit, 0, u3ginit_match, 150 u3ginit_attach, u3ginit_detach, NULL, NULL, NULL); 151 152 153 static int u3g_match(device_t, cfdata_t, void *); 154 static void u3g_attach(device_t, device_t, void *); 155 static int u3g_detach(device_t, int); 156 static int u3g_activate(device_t, enum devact); 157 static void u3g_childdet(device_t, device_t); 158 159 CFATTACH_DECL2_NEW(u3g, sizeof(struct u3g_softc), u3g_match, 160 u3g_attach, u3g_detach, u3g_activate, NULL, u3g_childdet); 161 162 163 static void u3g_intr(usbd_xfer_handle, usbd_private_handle, usbd_status); 164 static void u3g_get_status(void *, int, u_char *, u_char *); 165 static void u3g_set(void *, int, int, int); 166 static int u3g_open(void *, int); 167 static void u3g_close(void *, int); 168 static void u3g_read(void *, int, u_char **, uint32_t *); 169 static void u3g_write(void *, int, u_char *, u_char *, u_int32_t *); 170 171 struct ucom_methods u3g_methods = { 172 u3g_get_status, 173 u3g_set, 174 NULL, 175 NULL, 176 u3g_open, 177 u3g_close, 178 u3g_read, 179 u3g_write, 180 }; 181 182 /* 183 * Allegedly supported devices 184 */ 185 static const struct usb_devno u3g_devs[] = { 186 { USB_VENDOR_DELL, USB_PRODUCT_DELL_W5500 }, 187 /* OEM: Huawei */ 188 { USB_VENDOR_HUAWEI, USB_PRODUCT_HUAWEI_E1750 }, 189 { USB_VENDOR_HUAWEI, USB_PRODUCT_HUAWEI_E1820 }, 190 { USB_VENDOR_HUAWEI, USB_PRODUCT_HUAWEI_E220 }, 191 { USB_VENDOR_HUAWEI, USB_PRODUCT_HUAWEI_EM770W }, 192 { USB_VENDOR_HUAWEI, USB_PRODUCT_HUAWEI_K3765 }, 193 { USB_VENDOR_HUAWEI, USB_PRODUCT_HUAWEI_MOBILE }, 194 { USB_VENDOR_HUAWEI, USB_PRODUCT_HUAWEI_E171 }, 195 { USB_VENDOR_HUAWEI, USB_PRODUCT_HUAWEI_E353 }, 196 /* OEM: Merlin */ 197 { USB_VENDOR_MERLIN, USB_PRODUCT_MERLIN_V620 }, 198 /* OEM: Novatel */ 199 { USB_VENDOR_NOVATEL2, USB_PRODUCT_NOVATEL2_ES620 }, 200 { USB_VENDOR_NOVATEL2, USB_PRODUCT_NOVATEL2_EU8X0D }, 201 { USB_VENDOR_NOVATEL2, USB_PRODUCT_NOVATEL2_MC950D }, 202 #if 0 203 /* These are matched in u3ginit_match() */ 204 { USB_VENDOR_NOVATEL2, USB_PRODUCT_NOVATEL2_MC950D_DRIVER }, 205 { USB_VENDOR_NOVATEL2, USB_PRODUCT_NOVATEL2_U760_DRIVER }, 206 #endif 207 { USB_VENDOR_NOVATEL2, USB_PRODUCT_NOVATEL2_MERLINU740 }, 208 { USB_VENDOR_NOVATEL2, USB_PRODUCT_NOVATEL2_MERLINV620 }, 209 { USB_VENDOR_NOVATEL2, USB_PRODUCT_NOVATEL2_S720 }, 210 { USB_VENDOR_NOVATEL2, USB_PRODUCT_NOVATEL2_U720 }, 211 { USB_VENDOR_NOVATEL2, USB_PRODUCT_NOVATEL2_U727 }, 212 { USB_VENDOR_NOVATEL2, USB_PRODUCT_NOVATEL2_U740_2 }, 213 { USB_VENDOR_NOVATEL2, USB_PRODUCT_NOVATEL2_U760 }, 214 { USB_VENDOR_NOVATEL2, USB_PRODUCT_NOVATEL2_U870 }, 215 { USB_VENDOR_NOVATEL2, USB_PRODUCT_NOVATEL2_V740 }, 216 { USB_VENDOR_NOVATEL2, USB_PRODUCT_NOVATEL2_X950D }, 217 { USB_VENDOR_NOVATEL2, USB_PRODUCT_NOVATEL2_XU870 }, 218 /* OEM: Option N.V. */ 219 { USB_VENDOR_OPTIONNV, USB_PRODUCT_OPTIONNV_QUADPLUSUMTS }, 220 { USB_VENDOR_OPTIONNV, USB_PRODUCT_OPTIONNV_HSDPA }, 221 { USB_VENDOR_OPTIONNV, USB_PRODUCT_OPTIONNV_GTMAXHSUPA }, 222 /* OEM: Qualcomm, Inc. */ 223 { USB_VENDOR_QUALCOMM, USB_PRODUCT_QUALCOMM_NTT_DOCOMO_L02C_MODEM }, 224 225 /* OEM: Sierra Wireless: */ 226 { USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AC595U }, 227 { USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AC597E }, 228 { USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AC875U }, 229 { USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AC880 }, 230 { USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AC880E }, 231 { USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AC880U }, 232 { USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AC881 }, 233 { USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AC881E }, 234 { USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AC881U }, 235 { USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AIRCARD580 }, 236 { USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AIRCARD595 }, 237 { USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AIRCARD875 }, 238 { USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_C597 }, 239 { USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_EM5625 }, 240 { USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC5720 }, 241 { USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC5720_2 }, 242 { USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC5725 }, 243 { USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC8755 }, 244 { USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC8755_2 }, 245 { USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC8755_3 }, 246 { USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC8765 }, 247 { USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC8775_2 }, 248 { USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC8780 }, 249 { USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC8781 }, 250 { USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MINI5725 }, 251 { USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_USB305 }, 252 { USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_250U }, 253 /* Toshiba */ 254 { USB_VENDOR_TOSHIBA, USB_PRODUCT_TOSHIBA_HSDPA_MODEM_EU870DT1 }, 255 256 /* ZTE */ 257 { USB_VENDOR_ZTE, USB_PRODUCT_ZTE_MF622 }, 258 { USB_VENDOR_ZTE, USB_PRODUCT_ZTE_MF626 }, 259 { USB_VENDOR_ZTE, USB_PRODUCT_ZTE_MF628 }, 260 { USB_VENDOR_ZTE, USB_PRODUCT_ZTE_MF820D }, 261 262 /* 4G Systems */ 263 { USB_VENDOR_4GSYSTEMS, USB_PRODUCT_4GSYSTEMS_XSSTICK_P14 }, 264 { USB_VENDOR_4GSYSTEMS, USB_PRODUCT_4GSYSTEMS_XSSTICK_W14 }, 265 }; 266 267 static int 268 send_bulkmsg(usbd_device_handle dev, void *cmd, size_t cmdlen) 269 { 270 usbd_interface_handle iface; 271 usb_interface_descriptor_t *id; 272 usb_endpoint_descriptor_t *ed; 273 usbd_pipe_handle pipe; 274 usbd_xfer_handle xfer; 275 int err, i; 276 277 /* Move the device into the configured state. */ 278 err = usbd_set_config_index(dev, 0, 0); 279 if (err) { 280 aprint_error("u3ginit: failed to set config index\n"); 281 return UMATCH_NONE; 282 } 283 284 err = usbd_device2interface_handle(dev, 0, &iface); 285 if (err != 0) { 286 aprint_error("u3ginit: failed to get interface\n"); 287 return UMATCH_NONE; 288 } 289 290 id = usbd_get_interface_descriptor(iface); 291 ed = NULL; 292 for (i = 0 ; i < id->bNumEndpoints ; i++) { 293 ed = usbd_interface2endpoint_descriptor(iface, i); 294 if (ed == NULL) 295 continue; 296 if (UE_GET_DIR(ed->bEndpointAddress) != UE_DIR_OUT) 297 continue; 298 if ((ed->bmAttributes & UE_XFERTYPE) == UE_BULK) 299 break; 300 } 301 302 if (i == id->bNumEndpoints) 303 return UMATCH_NONE; 304 305 err = usbd_open_pipe(iface, ed->bEndpointAddress, 306 USBD_EXCLUSIVE_USE, &pipe); 307 if (err != 0) { 308 aprint_error("u3ginit: failed to open bulk transfer pipe %d\n", 309 ed->bEndpointAddress); 310 return UMATCH_NONE; 311 } 312 313 xfer = usbd_alloc_xfer(dev); 314 if (xfer != NULL) { 315 usbd_setup_xfer(xfer, pipe, NULL, cmd, cmdlen, 316 USBD_SYNCHRONOUS, USBD_DEFAULT_TIMEOUT, NULL); 317 318 err = usbd_transfer(xfer); 319 320 #if 0 /* XXXpooka: at least my huawei "fails" this always, but still detaches */ 321 if (err) 322 aprint_error("u3ginit: transfer failed\n"); 323 #else 324 err = 0; 325 #endif 326 usbd_free_xfer(xfer); 327 } else { 328 aprint_error("u3ginit: failed to allocate xfer\n"); 329 err = USBD_NOMEM; 330 } 331 332 usbd_abort_pipe(pipe); 333 usbd_close_pipe(pipe); 334 335 return (err == USBD_NORMAL_COMPLETION ? UMATCH_HIGHEST : UMATCH_NONE); 336 } 337 338 /* Byte 0..3: Command Block Wrapper (CBW) signature */ 339 static void 340 set_cbw(unsigned char *cmd) 341 { 342 cmd[0] = 0x55; 343 cmd[1] = 0x53; 344 cmd[2] = 0x42; 345 cmd[3] = 0x43; 346 } 347 348 static int 349 u3g_bulk_scsi_eject(usbd_device_handle dev) 350 { 351 unsigned char cmd[31]; 352 353 memset(cmd, 0, sizeof(cmd)); 354 /* Byte 0..3: Command Block Wrapper (CBW) signature */ 355 set_cbw(cmd); 356 /* 4..7: CBW Tag, has to unique, but only a single transfer used. */ 357 cmd[4] = 0x01; 358 /* 8..11: CBW Transfer Length, no data here */ 359 /* 12: CBW Flag: output, so 0 */ 360 /* 13: CBW Lun: 0 */ 361 /* 14: CBW Length */ 362 cmd[14] = 0x06; 363 364 /* Rest is the SCSI payload */ 365 366 /* 0: SCSI START/STOP opcode */ 367 cmd[15] = 0x1b; 368 /* 1..3 unused */ 369 /* 4 Load/Eject command */ 370 cmd[19] = 0x02; 371 /* 5: unused */ 372 373 return send_bulkmsg(dev, cmd, sizeof(cmd)); 374 } 375 376 static int 377 u3g_bulk_ata_eject(usbd_device_handle dev) 378 { 379 unsigned char cmd[31]; 380 381 memset(cmd, 0, sizeof(cmd)); 382 /* Byte 0..3: Command Block Wrapper (CBW) signature */ 383 set_cbw(cmd); 384 /* 4..7: CBW Tag, has to unique, but only a single transfer used. */ 385 cmd[4] = 0x01; 386 /* 8..11: CBW Transfer Length, no data here */ 387 /* 12: CBW Flag: output, so 0 */ 388 /* 13: CBW Lun: 0 */ 389 /* 14: CBW Length */ 390 cmd[14] = 0x06; 391 392 /* Rest is the SCSI payload */ 393 394 /* 0: ATA pass-through */ 395 cmd[15] = 0x85; 396 /* 1..3 unused */ 397 /* 4 XXX What is this command? */ 398 cmd[19] = 0x24; 399 /* 5: unused */ 400 401 return send_bulkmsg(dev, cmd, sizeof(cmd)); 402 } 403 404 static int 405 u3g_huawei_reinit(usbd_device_handle dev) 406 { 407 /* 408 * The Huawei device presents itself as a umass device with Windows 409 * drivers on it. After installation of the driver, it reinits into a 410 * 3G serial device. 411 */ 412 usb_device_request_t req; 413 usb_config_descriptor_t *cdesc; 414 415 /* Get the config descriptor */ 416 cdesc = usbd_get_config_descriptor(dev); 417 if (cdesc == NULL) { 418 usb_device_descriptor_t dd; 419 420 if (usbd_get_device_desc(dev, &dd) != 0) 421 return (UMATCH_NONE); 422 423 if (dd.bNumConfigurations != 1) 424 return (UMATCH_NONE); 425 426 if (usbd_set_config_index(dev, 0, 1) != 0) 427 return (UMATCH_NONE); 428 429 cdesc = usbd_get_config_descriptor(dev); 430 431 if (cdesc == NULL) 432 return (UMATCH_NONE); 433 } 434 435 /* 436 * One iface means umass mode, more than 1 (4 usually) means 3G mode. 437 * 438 * XXX: We should check the first interface's device class just to be 439 * sure. If it's a mass storage device, then we can be fairly certain 440 * it needs a mode-switch. 441 */ 442 if (cdesc->bNumInterface > 1) 443 return (UMATCH_NONE); 444 445 req.bmRequestType = UT_WRITE_DEVICE; 446 req.bRequest = UR_SET_FEATURE; 447 USETW(req.wValue, UF_DEVICE_REMOTE_WAKEUP); 448 USETW(req.wIndex, UHF_PORT_SUSPEND); 449 USETW(req.wLength, 0); 450 451 (void) usbd_do_request(dev, &req, 0); 452 453 return (UMATCH_HIGHEST); /* Prevent umass from attaching */ 454 } 455 456 static int 457 u3g_huawei_k3765_reinit(usbd_device_handle dev) 458 { 459 unsigned char cmd[31]; 460 461 /* magic string adapted from some webpage */ 462 memset(cmd, 0, sizeof(cmd)); 463 /* Byte 0..3: Command Block Wrapper (CBW) signature */ 464 set_cbw(cmd); 465 466 cmd[15]= 0x11; 467 cmd[16]= 0x06; 468 469 return send_bulkmsg(dev, cmd, sizeof(cmd)); 470 } 471 static int 472 u3g_huawei_e171_reinit(usbd_device_handle dev) 473 { 474 unsigned char cmd[31]; 475 476 /* magic string adapted from some webpage */ 477 memset(cmd, 0, sizeof(cmd)); 478 /* Byte 0..3: Command Block Wrapper (CBW) signature */ 479 set_cbw(cmd); 480 481 cmd[15]= 0x11; 482 cmd[16]= 0x06; 483 cmd[17]= 0x20; 484 cmd[20]= 0x01; 485 486 return send_bulkmsg(dev, cmd, sizeof(cmd)); 487 } 488 489 static int 490 u3g_huawei_e353_reinit(usbd_device_handle dev) 491 { 492 unsigned char cmd[31]; 493 494 /* magic string adapted from some webpage */ 495 memset(cmd, 0, sizeof(cmd)); 496 /* Byte 0..3: Command Block Wrapper (CBW) signature */ 497 set_cbw(cmd); 498 499 cmd[4] = 0x7f; 500 cmd[9] = 0x02; 501 cmd[12] = 0x80; 502 cmd[14] = 0x0a; 503 cmd[15] = 0x11; 504 cmd[16] = 0x06; 505 cmd[17] = 0x20; 506 cmd[23] = 0x01; 507 508 return send_bulkmsg(dev, cmd, sizeof(cmd)); 509 } 510 511 static int 512 u3g_sierra_reinit(usbd_device_handle dev) 513 { 514 /* Some Sierra devices presents themselves as a umass device with 515 * Windows drivers on it. After installation of the driver, it 516 * reinits into a * 3G serial device. 517 */ 518 usb_device_request_t req; 519 520 req.bmRequestType = UT_VENDOR; 521 req.bRequest = UR_SET_INTERFACE; 522 USETW(req.wValue, UF_DEVICE_REMOTE_WAKEUP); 523 USETW(req.wIndex, UHF_PORT_CONNECTION); 524 USETW(req.wLength, 0); 525 526 (void) usbd_do_request(dev, &req, 0); 527 528 return (UMATCH_HIGHEST); /* Match to prevent umass from attaching */ 529 } 530 531 static int 532 u3g_4gsystems_reinit(usbd_device_handle dev) 533 { 534 /* magic string adapted from usb_modeswitch database */ 535 unsigned char cmd[31]; 536 537 memset(cmd, 0, sizeof(cmd)); 538 /* Byte 0..3: Command Block Wrapper (CBW) signature */ 539 set_cbw(cmd); 540 541 cmd[4] = 0x12; 542 cmd[5] = 0x34; 543 cmd[6] = 0x56; 544 cmd[7] = 0x78; 545 cmd[8] = 0x80; 546 cmd[12] = 0x80; 547 cmd[14] = 0x06; 548 cmd[15] = 0x06; 549 cmd[16] = 0xf5; 550 cmd[17] = 0x04; 551 cmd[18] = 0x02; 552 cmd[19] = 0x52; 553 cmd[20] = 0x70; 554 555 return send_bulkmsg(dev, cmd, sizeof(cmd)); 556 } 557 558 /* 559 * First personality: 560 * 561 * Claim the entire device if a mode-switch is required. 562 */ 563 564 static int 565 u3ginit_match(device_t parent, cfdata_t match, void *aux) 566 { 567 struct usb_attach_arg *uaa = aux; 568 569 /* 570 * Huawei changes product when it is configured as a modem. 571 */ 572 switch (uaa->vendor) { 573 case USB_VENDOR_HUAWEI: 574 if (uaa->product == USB_PRODUCT_HUAWEI_K3765) 575 return UMATCH_NONE; 576 577 switch (uaa->product) { 578 case USB_PRODUCT_HUAWEI_E1750INIT: 579 case USB_PRODUCT_HUAWEI_K3765INIT: 580 return u3g_huawei_k3765_reinit(uaa->device); 581 break; 582 case USB_PRODUCT_HUAWEI_E171INIT: 583 return u3g_huawei_e171_reinit(uaa->device); 584 break; 585 case USB_PRODUCT_HUAWEI_E353INIT: 586 return u3g_huawei_e353_reinit(uaa->device); 587 break; 588 default: 589 return u3g_huawei_reinit(uaa->device); 590 break; 591 } 592 break; 593 594 case USB_VENDOR_NOVATEL2: 595 switch (uaa->product){ 596 case USB_PRODUCT_NOVATEL2_MC950D_DRIVER: 597 case USB_PRODUCT_NOVATEL2_U760_DRIVER: 598 return u3g_bulk_scsi_eject(uaa->device); 599 break; 600 default: 601 break; 602 } 603 break; 604 605 case USB_VENDOR_SIERRA: 606 if (uaa->product == USB_PRODUCT_SIERRA_INSTALLER) 607 return u3g_sierra_reinit(uaa->device); 608 break; 609 610 case USB_VENDOR_QUALCOMM: 611 if (uaa->product == USB_PRODUCT_QUALCOMM_NTT_DOCOMO_L02C_STORAGE) 612 return u3g_bulk_scsi_eject(uaa->device); 613 break; 614 615 case USB_VENDOR_ZTE: 616 switch (uaa->product){ 617 case USB_PRODUCT_ZTE_INSTALLER: 618 case USB_PRODUCT_ZTE_MF820D_INSTALLER: 619 (void)u3g_bulk_ata_eject(uaa->device); 620 (void)u3g_bulk_scsi_eject(uaa->device); 621 return UMATCH_HIGHEST; 622 default: 623 break; 624 } 625 break; 626 627 case USB_VENDOR_4GSYSTEMS: 628 if (uaa->product == USB_PRODUCT_4GSYSTEMS_XSSTICK_P14_INSTALLER) 629 return u3g_4gsystems_reinit(uaa->device); 630 break; 631 632 default: 633 break; 634 } 635 636 return UMATCH_NONE; 637 } 638 639 static void 640 u3ginit_attach(device_t parent, device_t self, void *aux) 641 { 642 struct usb_attach_arg *uaa = aux; 643 644 aprint_naive("\n"); 645 aprint_normal(": Switching to 3G mode\n"); 646 647 if (uaa->vendor == USB_VENDOR_NOVATEL2) { 648 switch (uaa->product) { 649 case USB_PRODUCT_NOVATEL2_MC950D_DRIVER: 650 case USB_PRODUCT_NOVATEL2_U760_DRIVER: 651 /* About to disappear... */ 652 return; 653 break; 654 default: 655 break; 656 } 657 } 658 659 /* Move the device into the configured state. */ 660 (void) usbd_set_config_index(uaa->device, 0, 1); 661 } 662 663 static int 664 u3ginit_detach(device_t self, int flags) 665 { 666 667 return (0); 668 } 669 670 671 /* 672 * Second personality: 673 * 674 * Claim only those interfaces required for 3G modem operation. 675 */ 676 677 static int 678 u3g_match(device_t parent, cfdata_t match, void *aux) 679 { 680 struct usbif_attach_arg *uaa = aux; 681 usbd_interface_handle iface; 682 usb_interface_descriptor_t *id; 683 usbd_status error; 684 685 if (!usb_lookup(u3g_devs, uaa->vendor, uaa->product)) 686 return (UMATCH_NONE); 687 688 error = usbd_device2interface_handle(uaa->device, uaa->ifaceno, &iface); 689 if (error) { 690 printf("u3g_match: failed to get interface, err=%s\n", 691 usbd_errstr(error)); 692 return (UMATCH_NONE); 693 } 694 695 id = usbd_get_interface_descriptor(iface); 696 if (id == NULL) { 697 printf("u3g_match: failed to get interface descriptor\n"); 698 return (UMATCH_NONE); 699 } 700 701 /* 702 * Huawei modems use the vendor-specific class for all interfaces, 703 * both tty and CDC NCM, which we should avoid attaching to. 704 */ 705 if (uaa->vendor == USB_VENDOR_HUAWEI && id->bInterfaceSubClass == 2 && 706 (id->bInterfaceProtocol & 0xf) == 6) /* 0x16, 0x46, 0x76 */ 707 return (UMATCH_NONE); 708 709 /* 710 * 3G modems generally report vendor-specific class 711 * 712 * XXX: this may be too generalised. 713 */ 714 return ((id->bInterfaceClass == UICLASS_VENDOR) ? 715 UMATCH_VENDOR_PRODUCT : UMATCH_NONE); 716 } 717 718 static void 719 u3g_attach(device_t parent, device_t self, void *aux) 720 { 721 struct u3g_softc *sc = device_private(self); 722 struct usbif_attach_arg *uaa = aux; 723 usbd_device_handle dev = uaa->device; 724 usbd_interface_handle iface; 725 usb_interface_descriptor_t *id; 726 usb_endpoint_descriptor_t *ed; 727 struct ucom_attach_args uca; 728 usbd_status error; 729 int n, intr_address, intr_size; 730 731 aprint_naive("\n"); 732 aprint_normal("\n"); 733 734 sc->sc_dev = self; 735 sc->sc_dying = false; 736 sc->sc_udev = dev; 737 738 error = usbd_device2interface_handle(dev, uaa->ifaceno, &iface); 739 if (error) { 740 aprint_error_dev(self, "failed to get interface, err=%s\n", 741 usbd_errstr(error)); 742 return; 743 } 744 745 id = usbd_get_interface_descriptor(iface); 746 747 uca.info = "3G Modem"; 748 uca.ibufsize = U3G_BUFF_SIZE; 749 uca.obufsize = U3G_BUFF_SIZE; 750 uca.ibufsizepad = U3G_BUFF_SIZE; 751 uca.opkthdrlen = 0; 752 uca.device = dev; 753 uca.iface = iface; 754 uca.methods = &u3g_methods; 755 uca.arg = sc; 756 uca.portno = -1; 757 uca.bulkin = uca.bulkout = -1; 758 759 760 sc->sc_ifaceno = uaa->ifaceno; 761 intr_address = -1; 762 intr_size = 0; 763 764 for (n = 0; n < id->bNumEndpoints; n++) { 765 ed = usbd_interface2endpoint_descriptor(iface, n); 766 if (ed == NULL) { 767 aprint_error_dev(self, "no endpoint descriptor " 768 "for %d (interface: %d)\n", n, sc->sc_ifaceno); 769 sc->sc_dying = true; 770 return; 771 } 772 773 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN && 774 UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) { 775 intr_address = ed->bEndpointAddress; 776 intr_size = UGETW(ed->wMaxPacketSize); 777 } else 778 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN && 779 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) { 780 uca.bulkin = ed->bEndpointAddress; 781 } else 782 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT && 783 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) { 784 uca.bulkout = ed->bEndpointAddress; 785 } 786 if (uca.bulkin != -1 && uca.bulkout != -1) { 787 struct u3g_com *com; 788 if (sc->sc_ncom == __arraycount(sc->sc_com)) { 789 aprint_error_dev(self, "Need to configure " 790 "more than %zu ttys", sc->sc_ncom); 791 continue; 792 } 793 uca.portno = sc->sc_ncom++; 794 com = &sc->sc_com[uca.portno]; 795 com->c_outpins = 0; 796 com->c_msr = UMSR_DSR | UMSR_CTS | UMSR_DCD; 797 com->c_open = false; 798 com->c_purging = false; 799 com->c_dev = config_found_sm_loc(self, "ucombus", 800 NULL, &uca, ucomprint, ucomsubmatch); 801 uca.bulkin = -1; 802 uca.bulkout = -1; 803 } 804 } 805 806 if (sc->sc_ncom == 0) { 807 aprint_error_dev(self, "Missing bulk in/out for interface %d\n", 808 sc->sc_ifaceno); 809 sc->sc_dying = true; 810 return; 811 } 812 813 /* 814 * If the interface has an interrupt pipe, open it immediately so 815 * that we can track input pin state changes regardless of whether 816 * the tty(4) device is open or not. 817 */ 818 if (intr_address != -1) { 819 sc->sc_intr_buff = malloc(intr_size, M_USBDEV, M_WAITOK); 820 error = usbd_open_pipe_intr(iface, intr_address, 821 USBD_SHORT_XFER_OK, &sc->sc_intr_pipe, sc, sc->sc_intr_buff, 822 intr_size, u3g_intr, 100); 823 if (error) { 824 aprint_error_dev(self, "cannot open interrupt pipe " 825 "(addr %d)\n", intr_address); 826 return; 827 } 828 } else { 829 sc->sc_intr_pipe = NULL; 830 sc->sc_intr_buff = NULL; 831 } 832 833 if (!pmf_device_register(self, NULL, NULL)) 834 aprint_error_dev(self, "couldn't establish power handler\n"); 835 } 836 837 static int 838 u3g_detach(device_t self, int flags) 839 { 840 struct u3g_softc *sc = device_private(self); 841 int rv; 842 843 if (sc->sc_dying) 844 return 0; 845 846 pmf_device_deregister(self); 847 848 for (size_t i = 0; i < sc->sc_ncom; i++) 849 if (sc->sc_com[i].c_dev != NULL) { 850 rv = config_detach(sc->sc_com[i].c_dev, flags); 851 if (rv != 0) { 852 aprint_verbose_dev(self, "Can't deallocate " 853 "port (%d)", rv); 854 } 855 } 856 857 if (sc->sc_intr_pipe != NULL) { 858 (void) usbd_abort_pipe(sc->sc_intr_pipe); 859 (void) usbd_close_pipe(sc->sc_intr_pipe); 860 sc->sc_intr_pipe = NULL; 861 } 862 if (sc->sc_intr_buff != NULL) { 863 free(sc->sc_intr_buff, M_USBDEV); 864 sc->sc_intr_buff = NULL; 865 } 866 867 return (0); 868 } 869 870 static void 871 u3g_childdet(device_t self, device_t child) 872 { 873 struct u3g_softc *sc = device_private(self); 874 875 for (size_t i = 0; i < sc->sc_ncom; i++) 876 if (sc->sc_com[i].c_dev == child) 877 sc->sc_com[i].c_dev = NULL; 878 } 879 880 static int 881 u3g_activate(device_t self, enum devact act) 882 { 883 struct u3g_softc *sc = device_private(self); 884 int rv = 0; 885 886 switch (act) { 887 case DVACT_DEACTIVATE: 888 for (size_t i = 0; i < sc->sc_ncom; i++) 889 if (sc->sc_com[i].c_dev != NULL && 890 config_deactivate(sc->sc_com[i].c_dev) && rv == 0) 891 rv = -1; 892 else 893 rv = 0; 894 break; 895 896 default: 897 break; 898 } 899 900 return rv; 901 } 902 903 static void 904 u3g_intr(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status) 905 { 906 struct u3g_softc *sc = (struct u3g_softc *)priv; 907 u_char *buf; 908 int portno = 0; /* XXX */ 909 struct u3g_com *com = &sc->sc_com[portno]; 910 911 if (sc->sc_dying) 912 return; 913 914 if (status != USBD_NORMAL_COMPLETION) { 915 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) 916 return; 917 usbd_clear_endpoint_stall_async(sc->sc_intr_pipe); 918 return; 919 } 920 921 buf = sc->sc_intr_buff; 922 if (buf[0] == 0xa1 && buf[1] == 0x20) { 923 u_char msr; 924 925 msr = com->c_msr & ~(UMSR_DCD | UMSR_DSR | UMSR_RI); 926 927 if (buf[8] & U3G_INPIN_DCD) 928 msr |= UMSR_DCD; 929 930 if (buf[8] & U3G_INPIN_DSR) 931 msr |= UMSR_DSR; 932 933 if (buf[8] & U3G_INPIN_RI) 934 msr |= UMSR_RI; 935 936 if (msr != com->c_msr) { 937 com->c_msr = msr; 938 if (com->c_open) 939 ucom_status_change(device_private(com->c_dev)); 940 } 941 } 942 } 943 944 /*ARGSUSED*/ 945 static void 946 u3g_get_status(void *arg, int portno, u_char *lsr, u_char *msr) 947 { 948 struct u3g_softc *sc = arg; 949 950 if (lsr != NULL) 951 *lsr = 0; /* LSR isn't supported */ 952 if (msr != NULL) 953 *msr = sc->sc_com[portno].c_msr; 954 } 955 956 /*ARGSUSED*/ 957 static void 958 u3g_set(void *arg, int portno, int reg, int onoff) 959 { 960 struct u3g_softc *sc = arg; 961 usb_device_request_t req; 962 uint16_t mask, new_state; 963 usbd_status err; 964 struct u3g_com *com = &sc->sc_com[portno]; 965 966 if (sc->sc_dying) 967 return; 968 969 switch (reg) { 970 case UCOM_SET_DTR: 971 mask = U3G_OUTPIN_DTR; 972 break; 973 case UCOM_SET_RTS: 974 mask = U3G_OUTPIN_RTS; 975 break; 976 default: 977 return; 978 } 979 980 new_state = com->c_outpins & ~mask; 981 if (onoff) 982 new_state |= mask; 983 984 if (new_state == com->c_outpins) 985 return; 986 987 com->c_outpins = new_state; 988 989 req.bmRequestType = UT_WRITE_CLASS_INTERFACE; 990 req.bRequest = U3G_SET_PIN; 991 USETW(req.wValue, new_state); 992 USETW(req.wIndex, sc->sc_ifaceno); 993 USETW(req.wLength, 0); 994 995 err = usbd_do_request(sc->sc_udev, &req, 0); 996 if (err == USBD_STALLED) 997 usbd_clear_endpoint_stall(sc->sc_udev->default_pipe); 998 } 999 1000 /*ARGSUSED*/ 1001 static int 1002 u3g_open(void *arg, int portno) 1003 { 1004 struct u3g_softc *sc = arg; 1005 usb_device_request_t req; 1006 usb_endpoint_descriptor_t *ed; 1007 usb_interface_descriptor_t *id; 1008 usbd_interface_handle ih; 1009 usbd_status err; 1010 struct u3g_com *com = &sc->sc_com[portno]; 1011 int i, nin; 1012 1013 if (sc->sc_dying) 1014 return (0); 1015 1016 err = usbd_device2interface_handle(sc->sc_udev, sc->sc_ifaceno, &ih); 1017 if (err) 1018 return (EIO); 1019 1020 id = usbd_get_interface_descriptor(ih); 1021 1022 for (nin = i = 0; i < id->bNumEndpoints; i++) { 1023 ed = usbd_interface2endpoint_descriptor(ih, i); 1024 if (ed == NULL) 1025 return (EIO); 1026 1027 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN && 1028 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK && 1029 nin++ == portno) { 1030 /* Issue ENDPOINT_HALT request */ 1031 req.bmRequestType = UT_WRITE_ENDPOINT; 1032 req.bRequest = UR_CLEAR_FEATURE; 1033 USETW(req.wValue, UF_ENDPOINT_HALT); 1034 USETW(req.wIndex, ed->bEndpointAddress); 1035 USETW(req.wLength, 0); 1036 err = usbd_do_request(sc->sc_udev, &req, 0); 1037 if (err) 1038 return (EIO); 1039 } 1040 } 1041 1042 com->c_open = true; 1043 com->c_purging = true; 1044 getmicrotime(&com->c_purge_start); 1045 1046 return (0); 1047 } 1048 1049 /*ARGSUSED*/ 1050 static void 1051 u3g_close(void *arg, int portno) 1052 { 1053 struct u3g_softc *sc = arg; 1054 struct u3g_com *com = &sc->sc_com[portno]; 1055 1056 com->c_open = false; 1057 } 1058 1059 /*ARGSUSED*/ 1060 static void 1061 u3g_read(void *arg, int portno, u_char **cpp, uint32_t *ccp) 1062 { 1063 struct u3g_softc *sc = arg; 1064 struct timeval curr_tv, diff_tv; 1065 struct u3g_com *com = &sc->sc_com[portno]; 1066 1067 /* 1068 * If we're not purging input data following first open, do nothing. 1069 */ 1070 if (com->c_purging == false) 1071 return; 1072 1073 /* 1074 * Otherwise check if the purge timeout has expired 1075 */ 1076 getmicrotime(&curr_tv); 1077 timersub(&curr_tv, &com->c_purge_start, &diff_tv); 1078 1079 if (diff_tv.tv_sec >= U3G_PURGE_SECS) { 1080 /* Timeout expired. */ 1081 com->c_purging = false; 1082 } else { 1083 /* Still purging. Adjust the caller's byte count. */ 1084 *ccp = 0; 1085 } 1086 } 1087 1088 /*ARGSUSED*/ 1089 static void 1090 u3g_write(void *arg, int portno, u_char *to, u_char *from, u_int32_t *count) 1091 { 1092 struct u3g_softc *sc = arg; 1093 struct u3g_com *com = &sc->sc_com[portno]; 1094 1095 /* 1096 * Stop purging as soon as the first data is written to the device. 1097 */ 1098 com->c_purging = false; 1099 memcpy(to, from, *count); 1100 } 1101