1 /* $NetBSD: u3g.c,v 1.9 2010/01/07 00:15:20 martin 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.9 2010/01/07 00:15:20 martin 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 117 device_t sc_ucom; /* Child ucom(4) handle */ 118 int sc_ifaceno; /* Device interface number */ 119 120 bool sc_open; /* Device is in use */ 121 bool sc_purging; /* Purging stale data */ 122 struct timeval sc_purge_start; /* Control duration of purge */ 123 124 u_char sc_msr; /* Emulated 'msr' */ 125 uint16_t sc_outpins; /* Output pin state */ 126 127 usbd_pipe_handle sc_intr_pipe; /* Interrupt pipe */ 128 u_char *sc_intr_buff; /* Interrupt buffer */ 129 }; 130 131 /* 132 * The device driver has two personalities. The first uses the 'usbdevif' 133 * interface attribute so that a match will claim the entire USB device 134 * for itself. This is used for when a device needs to be mode-switched 135 * and ensures any other interfaces present cannot be claimed by other 136 * drivers while the mode-switch is in progress. 137 * 138 * The second personality uses the 'usbifif' interface attribute so that 139 * it can claim the 3G modem interfaces for itself, leaving others (such 140 * as the mass storage interfaces on some devices) for other drivers. 141 */ 142 static int u3ginit_match(device_t, cfdata_t, void *); 143 static void u3ginit_attach(device_t, device_t, void *); 144 static int u3ginit_detach(device_t, int); 145 146 CFATTACH_DECL2_NEW(u3ginit, 0, u3ginit_match, 147 u3ginit_attach, u3ginit_detach, NULL, NULL, NULL); 148 149 150 static int u3g_match(device_t, cfdata_t, void *); 151 static void u3g_attach(device_t, device_t, void *); 152 static int u3g_detach(device_t, int); 153 static int u3g_activate(device_t, enum devact); 154 static void u3g_childdet(device_t, device_t); 155 156 CFATTACH_DECL2_NEW(u3g, sizeof(struct u3g_softc), u3g_match, 157 u3g_attach, u3g_detach, u3g_activate, NULL, u3g_childdet); 158 159 160 static void u3g_intr(usbd_xfer_handle, usbd_private_handle, usbd_status); 161 static void u3g_get_status(void *, int, u_char *, u_char *); 162 static void u3g_set(void *, int, int, int); 163 static int u3g_open(void *, int); 164 static void u3g_close(void *, int); 165 static void u3g_read(void *, int, u_char **, uint32_t *); 166 static void u3g_write(void *, int, u_char *, u_char *, u_int32_t *); 167 168 struct ucom_methods u3g_methods = { 169 u3g_get_status, 170 u3g_set, 171 NULL, 172 NULL, 173 u3g_open, 174 u3g_close, 175 u3g_read, 176 u3g_write, 177 }; 178 179 /* 180 * Allegedly supported devices 181 */ 182 static const struct usb_devno u3g_devs[] = { 183 /* OEM: Option N.V. */ 184 { USB_VENDOR_OPTIONNV, USB_PRODUCT_OPTIONNV_QUADPLUSUMTS }, 185 { USB_VENDOR_OPTIONNV, USB_PRODUCT_OPTIONNV_HSDPA }, 186 { USB_VENDOR_OPTIONNV, USB_PRODUCT_OPTIONNV_GTMAXHSUPA }, 187 /* OEM: Qualcomm, Inc. */ 188 { USB_VENDOR_QUALCOMMINC, USB_PRODUCT_QUALCOMMINC_CDMA_MSM }, 189 /* OEM: Huawei */ 190 { USB_VENDOR_HUAWEI, USB_PRODUCT_HUAWEI_MOBILE }, 191 { USB_VENDOR_HUAWEI, USB_PRODUCT_HUAWEI_E220 }, 192 /* OEM: Novatel */ 193 { USB_VENDOR_NOVATEL2, USB_PRODUCT_NOVATEL2_MERLINV620 }, 194 { USB_VENDOR_NOVATEL2, USB_PRODUCT_NOVATEL2_ES620 }, 195 { USB_VENDOR_NOVATEL2, USB_PRODUCT_NOVATEL2_MC950D }, 196 { USB_VENDOR_NOVATEL2, USB_PRODUCT_NOVATEL2_U720 }, 197 { USB_VENDOR_NOVATEL2, USB_PRODUCT_NOVATEL2_U727 }, 198 { USB_VENDOR_NOVATEL2, USB_PRODUCT_NOVATEL2_MERLINU740 }, 199 { USB_VENDOR_NOVATEL2, USB_PRODUCT_NOVATEL2_U740_2 }, 200 { USB_VENDOR_NOVATEL2, USB_PRODUCT_NOVATEL2_U870 }, 201 { USB_VENDOR_NOVATEL2, USB_PRODUCT_NOVATEL2_MERLINV620 }, 202 { USB_VENDOR_NOVATEL2, USB_PRODUCT_NOVATEL2_S720 }, 203 { USB_VENDOR_NOVATEL2, USB_PRODUCT_NOVATEL2_V740 }, 204 { USB_VENDOR_NOVATEL2, USB_PRODUCT_NOVATEL2_X950D }, 205 { USB_VENDOR_NOVATEL2, USB_PRODUCT_NOVATEL2_XU870 }, 206 { USB_VENDOR_DELL, USB_PRODUCT_DELL_W5500 }, 207 #if 0 208 { USB_VENDOR_NOVATEL2, USB_PRODUCT_NOVATEL2_MC950D_DRIVER }, 209 #endif 210 /* OEM: Merlin */ 211 { USB_VENDOR_MERLIN, USB_PRODUCT_MERLIN_V620 }, 212 213 /* OEM: Sierra Wireless: */ 214 { USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AIRCARD580 }, 215 { USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AIRCARD595 }, 216 { USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AC595U }, 217 { USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AC597E }, 218 { USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_C597 }, 219 { USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AC880 }, 220 { USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AC880E }, 221 { USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AC880U }, 222 { USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AC881 }, 223 { USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AC881E }, 224 { USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AC881U }, 225 { USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_EM5625 }, 226 { USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC5720 }, 227 { USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC5720_2 }, 228 { USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC5725 }, 229 { USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MINI5725 }, 230 { USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AIRCARD875 }, 231 { USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC8755 }, 232 { USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC8755_2 }, 233 { USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC8755_3 }, 234 { USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC8765 }, 235 { USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AC875U }, 236 { USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC8775_2 }, 237 { USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC8780 }, 238 { USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC8781 }, 239 }; 240 241 static int 242 u3g_novatel_reinit(usbd_device_handle dev) 243 { 244 unsigned char cmd[31]; 245 usbd_interface_handle iface; 246 usb_interface_descriptor_t *id; 247 usb_endpoint_descriptor_t *ed; 248 usbd_pipe_handle pipe; 249 usbd_xfer_handle xfer; 250 int err, i; 251 252 memset(cmd, 0, sizeof(cmd)); 253 /* Byte 0..3: Command Block Wrapper (CBW) signature */ 254 cmd[0] = 0x55; 255 cmd[1] = 0x53; 256 cmd[2] = 0x42; 257 cmd[3] = 0x43; 258 /* 4..7: CBW Tag, has to unique, but only a single transfer used. */ 259 cmd[4] = 0x01; 260 /* 8..11: CBW Transfer Length, no data here */ 261 /* 12: CBW Flag: output, so 0 */ 262 /* 13: CBW Lun: 0 */ 263 /* 14: CBW Length */ 264 cmd[14] = 0x06; 265 /* Rest is the SCSI payload */ 266 /* 0: SCSI START/STOP opcode */ 267 cmd[15] = 0x1b; 268 /* 1..3 unused */ 269 /* 4 Load/Eject command */ 270 cmd[19] = 0x02; 271 /* 5: unused */ 272 273 274 /* Move the device into the configured state. */ 275 err = usbd_set_config_index(dev, 0, 0); 276 if (err) { 277 aprint_error("u3g: failed to set configuration index\n"); 278 return UMATCH_NONE; 279 } 280 281 err = usbd_device2interface_handle(dev, 0, &iface); 282 if (err != 0) { 283 aprint_error("u3g: failed to get interface\n"); 284 return UMATCH_NONE; 285 } 286 287 id = usbd_get_interface_descriptor(iface); 288 ed = NULL; 289 for (i = 0 ; i < id->bNumEndpoints ; i++) { 290 ed = usbd_interface2endpoint_descriptor(iface, i); 291 if (ed == NULL) 292 continue; 293 if (UE_GET_DIR(ed->bEndpointAddress) != UE_DIR_OUT) 294 continue; 295 if ((ed->bmAttributes & UE_XFERTYPE) == UE_BULK) 296 break; 297 } 298 299 if (i == id->bNumEndpoints) 300 return UMATCH_NONE; 301 302 err = usbd_open_pipe(iface, ed->bEndpointAddress, USBD_EXCLUSIVE_USE, 303 &pipe); 304 if (err != 0) { 305 aprint_error("u3g: failed to open bulk transfer pipe %d\n", 306 ed->bEndpointAddress); 307 return UMATCH_NONE; 308 } 309 310 xfer = usbd_alloc_xfer(dev); 311 if (xfer != NULL) { 312 usbd_setup_xfer(xfer, pipe, NULL, cmd, sizeof(cmd), 313 USBD_SYNCHRONOUS, USBD_DEFAULT_TIMEOUT, NULL); 314 315 err = usbd_transfer(xfer); 316 if (err) 317 aprint_error("u3g: transfer failed\n"); 318 usbd_free_xfer(xfer); 319 } else { 320 aprint_error("u3g: failed to allocate xfer\n"); 321 err = USBD_NOMEM; 322 } 323 324 usbd_abort_pipe(pipe); 325 usbd_close_pipe(pipe); 326 327 return (err == USBD_NORMAL_COMPLETION ? UMATCH_HIGHEST : UMATCH_NONE); 328 } 329 330 static int 331 u3g_huawei_reinit(usbd_device_handle dev) 332 { 333 /* 334 * The Huawei device presents itself as a umass device with Windows 335 * drivers on it. After installation of the driver, it reinits into a 336 * 3G serial device. 337 */ 338 usb_device_request_t req; 339 usb_config_descriptor_t *cdesc; 340 341 /* Get the config descriptor */ 342 cdesc = usbd_get_config_descriptor(dev); 343 if (cdesc == NULL) { 344 usb_device_descriptor_t dd; 345 346 if (usbd_get_device_desc(dev, &dd) != 0) 347 return (UMATCH_NONE); 348 349 if (dd.bNumConfigurations != 1) 350 return (UMATCH_NONE); 351 352 if (usbd_set_config_index(dev, 0, 1) != 0) 353 return (UMATCH_NONE); 354 355 cdesc = usbd_get_config_descriptor(dev); 356 357 if (cdesc == NULL) 358 return (UMATCH_NONE); 359 } 360 361 /* 362 * One iface means umass mode, more than 1 (4 usually) means 3G mode. 363 * 364 * XXX: We should check the first interface's device class just to be 365 * sure. If it's a mass storage device, then we can be fairly certain 366 * it needs a mode-switch. 367 */ 368 if (cdesc->bNumInterface > 1) 369 return (UMATCH_NONE); 370 371 req.bmRequestType = UT_WRITE_DEVICE; 372 req.bRequest = UR_SET_FEATURE; 373 USETW(req.wValue, UF_DEVICE_REMOTE_WAKEUP); 374 USETW(req.wIndex, UHF_PORT_SUSPEND); 375 USETW(req.wLength, 0); 376 377 (void) usbd_do_request(dev, &req, 0); 378 379 return (UMATCH_HIGHEST); /* Match to prevent umass from attaching */ 380 } 381 382 static int 383 u3g_sierra_reinit(usbd_device_handle dev) 384 { 385 /* Some Sierra devices presents themselves as a umass device with 386 * Windows drivers on it. After installation of the driver, it 387 * reinits into a * 3G serial device. 388 */ 389 usb_device_request_t req; 390 391 req.bmRequestType = UT_VENDOR; 392 req.bRequest = UR_SET_INTERFACE; 393 USETW(req.wValue, UF_DEVICE_REMOTE_WAKEUP); 394 USETW(req.wIndex, UHF_PORT_CONNECTION); 395 USETW(req.wLength, 0); 396 397 (void) usbd_do_request(dev, &req, 0); 398 399 return (UMATCH_HIGHEST); /* Match to prevent umass from attaching */ 400 } 401 402 403 /* 404 * First personality: 405 * 406 * Claim the entire device if a mode-switch is required. 407 */ 408 409 static int 410 u3ginit_match(device_t parent, cfdata_t match, void *aux) 411 { 412 struct usb_attach_arg *uaa = aux; 413 414 if (uaa->vendor == USB_VENDOR_HUAWEI) 415 return u3g_huawei_reinit(uaa->device); 416 417 if (uaa->vendor == USB_VENDOR_NOVATEL2 && 418 uaa->product == USB_PRODUCT_NOVATEL2_MC950D_DRIVER) 419 return u3g_novatel_reinit(uaa->device); 420 421 if (uaa->vendor == USB_VENDOR_SIERRA && 422 uaa->product == USB_PRODUCT_SIERRA_INSTALLER) 423 return u3g_sierra_reinit(uaa->device); 424 425 return UMATCH_NONE; 426 } 427 428 static void 429 u3ginit_attach(device_t parent, device_t self, void *aux) 430 { 431 struct usb_attach_arg *uaa = aux; 432 433 aprint_naive("\n"); 434 aprint_normal(": Switching to 3G mode\n"); 435 436 if (uaa->vendor == USB_VENDOR_NOVATEL2 && 437 uaa->product == USB_PRODUCT_NOVATEL2_MC950D_DRIVER) { 438 /* About to disappear... */ 439 return; 440 } 441 442 /* Move the device into the configured state. */ 443 (void) usbd_set_config_index(uaa->device, 0, 1); 444 } 445 446 static int 447 u3ginit_detach(device_t self, int flags) 448 { 449 450 return (0); 451 } 452 453 454 /* 455 * Second personality: 456 * 457 * Claim only those interfaces required for 3G modem operation. 458 */ 459 460 static int 461 u3g_match(device_t parent, cfdata_t match, void *aux) 462 { 463 struct usbif_attach_arg *uaa = aux; 464 usbd_interface_handle iface; 465 usb_interface_descriptor_t *id; 466 usbd_status error; 467 468 if (!usb_lookup(u3g_devs, uaa->vendor, uaa->product)) 469 return (UMATCH_NONE); 470 471 error = usbd_device2interface_handle(uaa->device, uaa->ifaceno, &iface); 472 if (error) { 473 printf("u3g_match: failed to get interface, err=%s\n", 474 usbd_errstr(error)); 475 return (UMATCH_NONE); 476 } 477 478 id = usbd_get_interface_descriptor(iface); 479 if (id == NULL) { 480 printf("u3g_match: failed to get interface descriptor\n"); 481 return (UMATCH_NONE); 482 } 483 484 /* 485 * 3G modems generally report vendor-specific class 486 * 487 * XXX: this may be too generalised. 488 */ 489 return ((id->bInterfaceClass == UICLASS_VENDOR) ? 490 UMATCH_VENDOR_PRODUCT : UMATCH_NONE); 491 } 492 493 static void 494 u3g_attach(device_t parent, device_t self, void *aux) 495 { 496 struct u3g_softc *sc = device_private(self); 497 struct usbif_attach_arg *uaa = aux; 498 usbd_device_handle dev = uaa->device; 499 usbd_interface_handle iface; 500 usb_interface_descriptor_t *id; 501 usb_endpoint_descriptor_t *ed; 502 struct ucom_attach_args uca; 503 usbd_status error; 504 int n, intr_address, intr_size; 505 506 aprint_naive("\n"); 507 aprint_normal("\n"); 508 509 sc->sc_dev = self; 510 sc->sc_dying = false; 511 sc->sc_udev = dev; 512 513 error = usbd_device2interface_handle(dev, uaa->ifaceno, &iface); 514 if (error) { 515 aprint_error_dev(self, "failed to get interface, err=%s\n", 516 usbd_errstr(error)); 517 return; 518 } 519 520 id = usbd_get_interface_descriptor(iface); 521 522 uca.info = "3G Modem"; 523 uca.ibufsize = U3G_BUFF_SIZE; 524 uca.obufsize = U3G_BUFF_SIZE; 525 uca.ibufsizepad = U3G_BUFF_SIZE; 526 uca.portno = uaa->ifaceno; 527 uca.opkthdrlen = 0; 528 uca.device = dev; 529 uca.iface = iface; 530 uca.methods = &u3g_methods; 531 uca.arg = sc; 532 uca.bulkin = uca.bulkout = -1; 533 534 sc->sc_outpins = 0; 535 sc->sc_msr = UMSR_DSR | UMSR_CTS | UMSR_DCD; 536 sc->sc_ifaceno = uaa->ifaceno; 537 sc->sc_open = false; 538 sc->sc_purging = false; 539 540 intr_address = -1; 541 intr_size = 0; 542 543 for (n = 0; n < id->bNumEndpoints; n++) { 544 ed = usbd_interface2endpoint_descriptor(iface, n); 545 if (ed == NULL) { 546 aprint_error_dev(self, "no endpoint descriptor " 547 "for %d (interface: %d)\n", n, sc->sc_ifaceno); 548 sc->sc_dying = true; 549 return; 550 } 551 552 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN && 553 UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) { 554 intr_address = ed->bEndpointAddress; 555 intr_size = UGETW(ed->wMaxPacketSize); 556 } else 557 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN && 558 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) { 559 uca.bulkin = ed->bEndpointAddress; 560 } else 561 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT && 562 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) { 563 uca.bulkout = ed->bEndpointAddress; 564 } 565 } 566 567 if (uca.bulkin == -1) { 568 aprint_error_dev(self, "Missing bulk in for interface %d\n", 569 sc->sc_ifaceno); 570 sc->sc_dying = true; 571 return; 572 } 573 574 if (uca.bulkout == -1) { 575 aprint_error_dev(self, "Missing bulk out for interface %d\n", 576 sc->sc_ifaceno); 577 sc->sc_dying = true; 578 return; 579 } 580 581 sc->sc_ucom = config_found_sm_loc(self, "ucombus", 582 NULL, &uca, ucomprint, ucomsubmatch); 583 584 /* 585 * If the interface has an interrupt pipe, open it immediately so 586 * that we can track input pin state changes regardless of whether 587 * the tty(4) device is open or not. 588 */ 589 if (intr_address != -1) { 590 sc->sc_intr_buff = malloc(intr_size, M_USBDEV, M_WAITOK); 591 error = usbd_open_pipe_intr(iface, intr_address, 592 USBD_SHORT_XFER_OK, &sc->sc_intr_pipe, sc, sc->sc_intr_buff, 593 intr_size, u3g_intr, 100); 594 if (error) { 595 aprint_error_dev(self, "cannot open interrupt pipe " 596 "(addr %d)\n", intr_address); 597 return; 598 } 599 } else { 600 sc->sc_intr_pipe = NULL; 601 sc->sc_intr_buff = NULL; 602 } 603 604 if (!pmf_device_register(self, NULL, NULL)) 605 aprint_error_dev(self, "couldn't establish power handler\n"); 606 } 607 608 static int 609 u3g_detach(device_t self, int flags) 610 { 611 struct u3g_softc *sc = device_private(self); 612 int rv; 613 614 if (sc->sc_dying) 615 return 0; 616 617 pmf_device_deregister(self); 618 619 if (sc->sc_ucom != NULL) { 620 rv = config_detach(sc->sc_ucom, flags); 621 if (rv != 0) { 622 aprint_verbose_dev(self, "Can't deallocate " 623 "port (%d)", rv); 624 } 625 } 626 627 if (sc->sc_intr_pipe != NULL) { 628 (void) usbd_abort_pipe(sc->sc_intr_pipe); 629 (void) usbd_close_pipe(sc->sc_intr_pipe); 630 sc->sc_intr_pipe = NULL; 631 } 632 if (sc->sc_intr_buff != NULL) { 633 free(sc->sc_intr_buff, M_USBDEV); 634 sc->sc_intr_buff = NULL; 635 } 636 637 return (0); 638 } 639 640 static void 641 u3g_childdet(device_t self, device_t child) 642 { 643 struct u3g_softc *sc = device_private(self); 644 645 if (sc->sc_ucom == child) 646 sc->sc_ucom = NULL; 647 } 648 649 static int 650 u3g_activate(device_t self, enum devact act) 651 { 652 struct u3g_softc *sc = device_private(self); 653 int rv; 654 655 switch (act) { 656 case DVACT_DEACTIVATE: 657 if (sc->sc_ucom != NULL && config_deactivate(sc->sc_ucom)) 658 rv = -1; 659 else 660 rv = 0; 661 break; 662 663 default: 664 rv = 0; 665 break; 666 } 667 668 return (rv); 669 } 670 671 static void 672 u3g_intr(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status) 673 { 674 struct u3g_softc *sc = (struct u3g_softc *)priv; 675 u_char *buf; 676 677 if (sc->sc_dying) 678 return; 679 680 if (status != USBD_NORMAL_COMPLETION) { 681 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) 682 return; 683 usbd_clear_endpoint_stall_async(sc->sc_intr_pipe); 684 return; 685 } 686 687 buf = sc->sc_intr_buff; 688 if (buf[0] == 0xa1 && buf[1] == 0x20) { 689 u_char msr; 690 691 msr = sc->sc_msr & ~(UMSR_DCD | UMSR_DSR | UMSR_RI); 692 693 if (buf[8] & U3G_INPIN_DCD) 694 msr |= UMSR_DCD; 695 696 if (buf[8] & U3G_INPIN_DSR) 697 msr |= UMSR_DSR; 698 699 if (buf[8] & U3G_INPIN_RI) 700 msr |= UMSR_RI; 701 702 if (msr != sc->sc_msr) { 703 sc->sc_msr = msr; 704 if (sc->sc_open) 705 ucom_status_change(device_private(sc->sc_ucom)); 706 } 707 } 708 } 709 710 /*ARGSUSED*/ 711 static void 712 u3g_get_status(void *arg, int portno, u_char *lsr, u_char *msr) 713 { 714 struct u3g_softc *sc = arg; 715 716 if (lsr != NULL) 717 *lsr = 0; /* LSR isn't supported */ 718 if (msr != NULL) 719 *msr = sc->sc_msr; 720 } 721 722 /*ARGSUSED*/ 723 static void 724 u3g_set(void *arg, int portno, int reg, int onoff) 725 { 726 struct u3g_softc *sc = arg; 727 usb_device_request_t req; 728 uint16_t mask, new_state; 729 usbd_status err; 730 731 if (sc->sc_dying) 732 return; 733 734 switch (reg) { 735 case UCOM_SET_DTR: 736 mask = U3G_OUTPIN_DTR; 737 break; 738 case UCOM_SET_RTS: 739 mask = U3G_OUTPIN_RTS; 740 break; 741 default: 742 return; 743 } 744 745 new_state = sc->sc_outpins & ~mask; 746 if (onoff) 747 new_state |= mask; 748 749 if (new_state == sc->sc_outpins) 750 return; 751 752 sc->sc_outpins = new_state; 753 754 req.bmRequestType = UT_WRITE_CLASS_INTERFACE; 755 req.bRequest = U3G_SET_PIN; 756 USETW(req.wValue, new_state); 757 USETW(req.wIndex, sc->sc_ifaceno); 758 USETW(req.wLength, 0); 759 760 err = usbd_do_request(sc->sc_udev, &req, 0); 761 if (err == USBD_STALLED) 762 usbd_clear_endpoint_stall(sc->sc_udev->default_pipe); 763 } 764 765 /*ARGSUSED*/ 766 static int 767 u3g_open(void *arg, int portno) 768 { 769 struct u3g_softc *sc = arg; 770 usb_device_request_t req; 771 usb_endpoint_descriptor_t *ed; 772 usb_interface_descriptor_t *id; 773 usbd_interface_handle ih; 774 usbd_status err; 775 int i; 776 777 if (sc->sc_dying) 778 return (0); 779 780 err = usbd_device2interface_handle(sc->sc_udev, portno, &ih); 781 if (err) 782 return (EIO); 783 784 id = usbd_get_interface_descriptor(ih); 785 786 for (i = 0; i < id->bNumEndpoints; i++) { 787 ed = usbd_interface2endpoint_descriptor(ih, i); 788 if (ed == NULL) 789 return (EIO); 790 791 if (UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) { 792 /* Issue ENDPOINT_HALT request */ 793 req.bmRequestType = UT_WRITE_ENDPOINT; 794 req.bRequest = UR_CLEAR_FEATURE; 795 USETW(req.wValue, UF_ENDPOINT_HALT); 796 USETW(req.wIndex, ed->bEndpointAddress); 797 USETW(req.wLength, 0); 798 err = usbd_do_request(sc->sc_udev, &req, 0); 799 if (err) 800 return (EIO); 801 } 802 } 803 804 sc->sc_open = true; 805 sc->sc_purging = true; 806 getmicrotime(&sc->sc_purge_start); 807 808 return (0); 809 } 810 811 /*ARGSUSED*/ 812 static void 813 u3g_close(void *arg, int portno) 814 { 815 struct u3g_softc *sc = arg; 816 817 sc->sc_open = false; 818 } 819 820 /*ARGSUSED*/ 821 static void 822 u3g_read(void *arg, int portno, u_char **cpp, uint32_t *ccp) 823 { 824 struct u3g_softc *sc = arg; 825 struct timeval curr_tv, diff_tv; 826 827 /* 828 * If we're not purging input data following first open, do nothing. 829 */ 830 if (sc->sc_purging == false) 831 return; 832 833 /* 834 * Otherwise check if the purge timeout has expired 835 */ 836 getmicrotime(&curr_tv); 837 timersub(&curr_tv, &sc->sc_purge_start, &diff_tv); 838 839 if (diff_tv.tv_sec >= U3G_PURGE_SECS) { 840 /* Timeout expired. */ 841 sc->sc_purging = false; 842 } else { 843 /* Still purging. Adjust the caller's byte count. */ 844 *ccp = 0; 845 } 846 } 847 848 /*ARGSUSED*/ 849 static void 850 u3g_write(void *arg, int portno, u_char *to, u_char *from, u_int32_t *count) 851 { 852 struct u3g_softc *sc = arg; 853 854 /* 855 * Stop purging as soon as the first data is written to the device. 856 */ 857 sc->sc_purging = false; 858 memcpy(to, from, *count); 859 } 860