1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2011 Anybots Inc 5 * written by Akinori Furukoshi <moonlightakkiy@yahoo.ca> 6 * - ucom part is based on u3g.c 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 30 #include <sys/cdefs.h> 31 __FBSDID("$FreeBSD$"); 32 33 #include <sys/param.h> 34 #include <sys/eventhandler.h> 35 #include <sys/systm.h> 36 #include <sys/queue.h> 37 #include <sys/systm.h> 38 #include <sys/socket.h> 39 #include <sys/kernel.h> 40 #include <sys/bus.h> 41 #include <sys/module.h> 42 #include <sys/sockio.h> 43 #include <sys/socket.h> 44 #include <sys/lock.h> 45 #include <sys/mutex.h> 46 #include <sys/condvar.h> 47 #include <sys/sysctl.h> 48 #include <sys/malloc.h> 49 #include <sys/taskqueue.h> 50 51 #include <net/if.h> 52 #include <net/if_var.h> 53 54 #include <machine/bus.h> 55 56 #include <net/if.h> 57 #include <net/if_types.h> 58 #include <net/netisr.h> 59 #include <net/bpf.h> 60 #include <net/ethernet.h> 61 62 #include <netinet/in.h> 63 #include <netinet/ip.h> 64 #include <netinet/ip6.h> 65 #include <netinet/udp.h> 66 67 #include <net80211/ieee80211_ioctl.h> 68 69 #include <dev/usb/usb.h> 70 #include <dev/usb/usbdi.h> 71 #include <dev/usb/usbdi_util.h> 72 #include <dev/usb/usb_cdc.h> 73 #include "usbdevs.h" 74 75 #define USB_DEBUG_VAR usie_debug 76 #include <dev/usb/usb_debug.h> 77 #include <dev/usb/usb_process.h> 78 #include <dev/usb/usb_msctest.h> 79 80 #include <dev/usb/serial/usb_serial.h> 81 82 #include <dev/usb/net/if_usievar.h> 83 84 #ifdef USB_DEBUG 85 static int usie_debug = 0; 86 87 static SYSCTL_NODE(_hw_usb, OID_AUTO, usie, CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 88 "sierra USB modem"); 89 SYSCTL_INT(_hw_usb_usie, OID_AUTO, debug, CTLFLAG_RWTUN, &usie_debug, 0, 90 "usie debug level"); 91 #endif 92 93 /* Sierra Wireless Direct IP modems */ 94 static const STRUCT_USB_HOST_ID usie_devs[] = { 95 #define USIE_DEV(v, d) { \ 96 USB_VP(USB_VENDOR_##v, USB_PRODUCT_##v##_##d) } 97 USIE_DEV(SIERRA, MC8700), 98 USIE_DEV(SIERRA, TRUINSTALL), 99 USIE_DEV(AIRPRIME, USB308), 100 #undef USIE_DEV 101 }; 102 103 static device_probe_t usie_probe; 104 static device_attach_t usie_attach; 105 static device_detach_t usie_detach; 106 static void usie_free_softc(struct usie_softc *); 107 108 static void usie_free(struct ucom_softc *); 109 static void usie_uc_update_line_state(struct ucom_softc *, uint8_t); 110 static void usie_uc_cfg_get_status(struct ucom_softc *, uint8_t *, uint8_t *); 111 static void usie_uc_cfg_set_dtr(struct ucom_softc *, uint8_t); 112 static void usie_uc_cfg_set_rts(struct ucom_softc *, uint8_t); 113 static void usie_uc_cfg_open(struct ucom_softc *); 114 static void usie_uc_cfg_close(struct ucom_softc *); 115 static void usie_uc_start_read(struct ucom_softc *); 116 static void usie_uc_stop_read(struct ucom_softc *); 117 static void usie_uc_start_write(struct ucom_softc *); 118 static void usie_uc_stop_write(struct ucom_softc *); 119 120 static usb_callback_t usie_uc_tx_callback; 121 static usb_callback_t usie_uc_rx_callback; 122 static usb_callback_t usie_uc_status_callback; 123 static usb_callback_t usie_if_tx_callback; 124 static usb_callback_t usie_if_rx_callback; 125 static usb_callback_t usie_if_status_callback; 126 127 static void usie_if_sync_to(void *); 128 static void usie_if_sync_cb(void *, int); 129 static void usie_if_status_cb(void *, int); 130 131 static void usie_if_start(if_t); 132 static int usie_if_output(if_t, struct mbuf *, 133 const struct sockaddr *, struct route *); 134 static void usie_if_init(void *); 135 static void usie_if_stop(struct usie_softc *); 136 static int usie_if_ioctl(if_t, u_long, caddr_t); 137 138 static int usie_do_request(struct usie_softc *, struct usb_device_request *, void *); 139 static int usie_if_cmd(struct usie_softc *, uint8_t); 140 static void usie_cns_req(struct usie_softc *, uint32_t, uint16_t); 141 static void usie_cns_rsp(struct usie_softc *, struct usie_cns *); 142 static void usie_hip_rsp(struct usie_softc *, uint8_t *, uint32_t); 143 static int usie_driver_loaded(struct module *, int, void *); 144 145 static const struct usb_config usie_uc_config[USIE_UC_N_XFER] = { 146 [USIE_UC_STATUS] = { 147 .type = UE_INTERRUPT, 148 .endpoint = UE_ADDR_ANY, 149 .direction = UE_DIR_IN, 150 .bufsize = 0, /* use wMaxPacketSize */ 151 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,}, 152 .callback = &usie_uc_status_callback, 153 }, 154 [USIE_UC_RX] = { 155 .type = UE_BULK, 156 .endpoint = UE_ADDR_ANY, 157 .direction = UE_DIR_IN, 158 .bufsize = USIE_BUFSIZE, 159 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,.proxy_buffer = 1,}, 160 .callback = &usie_uc_rx_callback, 161 }, 162 [USIE_UC_TX] = { 163 .type = UE_BULK, 164 .endpoint = UE_ADDR_ANY, 165 .direction = UE_DIR_OUT, 166 .bufsize = USIE_BUFSIZE, 167 .flags = {.pipe_bof = 1,.force_short_xfer = 1,}, 168 .callback = &usie_uc_tx_callback, 169 } 170 }; 171 172 static const struct usb_config usie_if_config[USIE_IF_N_XFER] = { 173 [USIE_IF_STATUS] = { 174 .type = UE_INTERRUPT, 175 .endpoint = UE_ADDR_ANY, 176 .direction = UE_DIR_IN, 177 .bufsize = 0, /* use wMaxPacketSize */ 178 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,}, 179 .callback = &usie_if_status_callback, 180 }, 181 [USIE_IF_RX] = { 182 .type = UE_BULK, 183 .endpoint = UE_ADDR_ANY, 184 .direction = UE_DIR_IN, 185 .bufsize = USIE_BUFSIZE, 186 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,}, 187 .callback = &usie_if_rx_callback, 188 }, 189 [USIE_IF_TX] = { 190 .type = UE_BULK, 191 .endpoint = UE_ADDR_ANY, 192 .direction = UE_DIR_OUT, 193 .bufsize = MAX(USIE_BUFSIZE, MCLBYTES), 194 .flags = {.pipe_bof = 1,.force_short_xfer = 1,}, 195 .callback = &usie_if_tx_callback, 196 } 197 }; 198 199 static device_method_t usie_methods[] = { 200 DEVMETHOD(device_probe, usie_probe), 201 DEVMETHOD(device_attach, usie_attach), 202 DEVMETHOD(device_detach, usie_detach), 203 DEVMETHOD_END 204 }; 205 206 static driver_t usie_driver = { 207 .name = "usie", 208 .methods = usie_methods, 209 .size = sizeof(struct usie_softc), 210 }; 211 212 static eventhandler_tag usie_etag; 213 214 DRIVER_MODULE(usie, uhub, usie_driver, usie_driver_loaded, NULL); 215 MODULE_DEPEND(usie, ucom, 1, 1, 1); 216 MODULE_DEPEND(usie, usb, 1, 1, 1); 217 MODULE_VERSION(usie, 1); 218 USB_PNP_HOST_INFO(usie_devs); 219 220 static const struct ucom_callback usie_uc_callback = { 221 .ucom_cfg_get_status = &usie_uc_cfg_get_status, 222 .ucom_cfg_set_dtr = &usie_uc_cfg_set_dtr, 223 .ucom_cfg_set_rts = &usie_uc_cfg_set_rts, 224 .ucom_cfg_open = &usie_uc_cfg_open, 225 .ucom_cfg_close = &usie_uc_cfg_close, 226 .ucom_start_read = &usie_uc_start_read, 227 .ucom_stop_read = &usie_uc_stop_read, 228 .ucom_start_write = &usie_uc_start_write, 229 .ucom_stop_write = &usie_uc_stop_write, 230 .ucom_free = &usie_free, 231 }; 232 233 static void 234 usie_autoinst(void *arg, struct usb_device *udev, 235 struct usb_attach_arg *uaa) 236 { 237 struct usb_interface *iface; 238 struct usb_interface_descriptor *id; 239 struct usb_device_request req; 240 int err; 241 242 if (uaa->dev_state != UAA_DEV_READY) 243 return; 244 245 iface = usbd_get_iface(udev, 0); 246 if (iface == NULL) 247 return; 248 249 id = iface->idesc; 250 if (id == NULL || id->bInterfaceClass != UICLASS_MASS) 251 return; 252 253 if (usbd_lookup_id_by_uaa(usie_devs, sizeof(usie_devs), uaa) != 0) 254 return; /* no device match */ 255 256 if (bootverbose) { 257 DPRINTF("Ejecting %s %s\n", 258 usb_get_manufacturer(udev), 259 usb_get_product(udev)); 260 } 261 req.bmRequestType = UT_VENDOR; 262 req.bRequest = UR_SET_INTERFACE; 263 USETW(req.wValue, UF_DEVICE_REMOTE_WAKEUP); 264 USETW(req.wIndex, UHF_PORT_CONNECTION); 265 USETW(req.wLength, 0); 266 267 /* at this moment there is no mutex */ 268 err = usbd_do_request_flags(udev, NULL, &req, 269 NULL, 0, NULL, 250 /* ms */ ); 270 271 /* success, mark the udev as disappearing */ 272 if (err == 0) 273 uaa->dev_state = UAA_DEV_EJECTING; 274 } 275 276 static int 277 usie_probe(device_t self) 278 { 279 struct usb_attach_arg *uaa = device_get_ivars(self); 280 281 if (uaa->usb_mode != USB_MODE_HOST) 282 return (ENXIO); 283 if (uaa->info.bConfigIndex != USIE_CNFG_INDEX) 284 return (ENXIO); 285 if (uaa->info.bIfaceIndex != USIE_IFACE_INDEX) 286 return (ENXIO); 287 if (uaa->info.bInterfaceClass != UICLASS_VENDOR) 288 return (ENXIO); 289 290 return (usbd_lookup_id_by_uaa(usie_devs, sizeof(usie_devs), uaa)); 291 } 292 293 static int 294 usie_attach(device_t self) 295 { 296 struct usie_softc *sc = device_get_softc(self); 297 struct usb_attach_arg *uaa = device_get_ivars(self); 298 if_t ifp; 299 struct usb_interface *iface; 300 struct usb_interface_descriptor *id; 301 struct usb_device_request req; 302 int err; 303 uint16_t fwattr; 304 uint8_t iface_index; 305 uint8_t ifidx; 306 uint8_t start; 307 308 device_set_usb_desc(self); 309 sc->sc_udev = uaa->device; 310 sc->sc_dev = self; 311 312 mtx_init(&sc->sc_mtx, "usie", MTX_NETWORK_LOCK, MTX_DEF); 313 ucom_ref(&sc->sc_super_ucom); 314 315 TASK_INIT(&sc->sc_if_status_task, 0, usie_if_status_cb, sc); 316 TASK_INIT(&sc->sc_if_sync_task, 0, usie_if_sync_cb, sc); 317 318 usb_callout_init_mtx(&sc->sc_if_sync_ch, &sc->sc_mtx, 0); 319 320 mtx_lock(&sc->sc_mtx); 321 322 /* set power mode to D0 */ 323 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 324 req.bRequest = USIE_POWER; 325 USETW(req.wValue, 0); 326 USETW(req.wIndex, 0); 327 USETW(req.wLength, 0); 328 if (usie_do_request(sc, &req, NULL)) { 329 mtx_unlock(&sc->sc_mtx); 330 goto detach; 331 } 332 /* read fw attr */ 333 fwattr = 0; 334 req.bmRequestType = UT_READ_VENDOR_DEVICE; 335 req.bRequest = USIE_FW_ATTR; 336 USETW(req.wValue, 0); 337 USETW(req.wIndex, 0); 338 USETW(req.wLength, sizeof(fwattr)); 339 if (usie_do_request(sc, &req, &fwattr)) { 340 mtx_unlock(&sc->sc_mtx); 341 goto detach; 342 } 343 mtx_unlock(&sc->sc_mtx); 344 345 /* check DHCP supports */ 346 DPRINTF("fwattr=%x\n", fwattr); 347 if (!(fwattr & USIE_FW_DHCP)) { 348 device_printf(self, "DHCP is not supported. A firmware upgrade might be needed.\n"); 349 } 350 351 /* find available interfaces */ 352 sc->sc_nucom = 0; 353 for (ifidx = 0; ifidx < USIE_IFACE_MAX; ifidx++) { 354 iface = usbd_get_iface(uaa->device, ifidx); 355 if (iface == NULL) 356 break; 357 358 id = usbd_get_interface_descriptor(iface); 359 if ((id == NULL) || (id->bInterfaceClass != UICLASS_VENDOR)) 360 continue; 361 362 /* setup Direct IP transfer */ 363 if (id->bInterfaceNumber >= 7 && id->bNumEndpoints == 3) { 364 sc->sc_if_ifnum = id->bInterfaceNumber; 365 iface_index = ifidx; 366 367 DPRINTF("ifnum=%d, ifidx=%d\n", 368 sc->sc_if_ifnum, ifidx); 369 370 err = usbd_transfer_setup(uaa->device, 371 &iface_index, sc->sc_if_xfer, usie_if_config, 372 USIE_IF_N_XFER, sc, &sc->sc_mtx); 373 374 if (err == 0) 375 continue; 376 377 device_printf(self, 378 "could not allocate USB transfers on " 379 "iface_index=%d, err=%s\n", 380 iface_index, usbd_errstr(err)); 381 goto detach; 382 } 383 384 /* setup ucom */ 385 if (sc->sc_nucom >= USIE_UCOM_MAX) 386 continue; 387 388 usbd_set_parent_iface(uaa->device, ifidx, 389 uaa->info.bIfaceIndex); 390 391 DPRINTF("NumEndpoints=%d bInterfaceNumber=%d\n", 392 id->bNumEndpoints, id->bInterfaceNumber); 393 394 if (id->bNumEndpoints == 2) { 395 sc->sc_uc_xfer[sc->sc_nucom][0] = NULL; 396 start = 1; 397 } else 398 start = 0; 399 400 err = usbd_transfer_setup(uaa->device, &ifidx, 401 sc->sc_uc_xfer[sc->sc_nucom] + start, 402 usie_uc_config + start, USIE_UC_N_XFER - start, 403 &sc->sc_ucom[sc->sc_nucom], &sc->sc_mtx); 404 405 if (err != 0) { 406 DPRINTF("usbd_transfer_setup error=%s\n", usbd_errstr(err)); 407 continue; 408 } 409 410 mtx_lock(&sc->sc_mtx); 411 for (; start < USIE_UC_N_XFER; start++) 412 usbd_xfer_set_stall(sc->sc_uc_xfer[sc->sc_nucom][start]); 413 mtx_unlock(&sc->sc_mtx); 414 415 sc->sc_uc_ifnum[sc->sc_nucom] = id->bInterfaceNumber; 416 417 sc->sc_nucom++; /* found a port */ 418 } 419 420 if (sc->sc_nucom == 0) { 421 device_printf(self, "no comports found\n"); 422 goto detach; 423 } 424 425 err = ucom_attach(&sc->sc_super_ucom, sc->sc_ucom, 426 sc->sc_nucom, sc, &usie_uc_callback, &sc->sc_mtx); 427 428 if (err != 0) { 429 DPRINTF("ucom_attach failed\n"); 430 goto detach; 431 } 432 DPRINTF("Found %d interfaces.\n", sc->sc_nucom); 433 434 /* setup ifnet (Direct IP) */ 435 sc->sc_ifp = ifp = if_alloc(IFT_OTHER); 436 437 if (ifp == NULL) { 438 device_printf(self, "Could not allocate a network interface\n"); 439 goto detach; 440 } 441 if_initname(ifp, "usie", device_get_unit(self)); 442 443 if_setsoftc(ifp, sc); 444 if_setmtu(ifp, USIE_MTU_MAX); 445 if_setflagbits(ifp, IFF_NOARP, 0); 446 if_setinitfn(ifp, usie_if_init); 447 if_setioctlfn(ifp, usie_if_ioctl); 448 if_setstartfn(ifp, usie_if_start); 449 if_setoutputfn(ifp, usie_if_output); 450 if_setsendqlen(ifp, ifqmaxlen); 451 if_setsendqready(ifp); 452 453 if_attach(ifp); 454 bpfattach(ifp, DLT_RAW, 0); 455 456 if (fwattr & USIE_PM_AUTO) { 457 usbd_set_power_mode(uaa->device, USB_POWER_MODE_SAVE); 458 DPRINTF("enabling automatic suspend and resume\n"); 459 } else { 460 usbd_set_power_mode(uaa->device, USB_POWER_MODE_ON); 461 DPRINTF("USB power is always ON\n"); 462 } 463 464 DPRINTF("device attached\n"); 465 return (0); 466 467 detach: 468 usie_detach(self); 469 return (ENOMEM); 470 } 471 472 static int 473 usie_detach(device_t self) 474 { 475 struct usie_softc *sc = device_get_softc(self); 476 uint8_t x; 477 478 /* detach ifnet */ 479 if (sc->sc_ifp != NULL) { 480 usie_if_stop(sc); 481 usbd_transfer_unsetup(sc->sc_if_xfer, USIE_IF_N_XFER); 482 bpfdetach(sc->sc_ifp); 483 if_detach(sc->sc_ifp); 484 if_free(sc->sc_ifp); 485 sc->sc_ifp = NULL; 486 } 487 /* detach ucom */ 488 if (sc->sc_nucom > 0) 489 ucom_detach(&sc->sc_super_ucom, sc->sc_ucom); 490 491 /* stop all USB transfers */ 492 usbd_transfer_unsetup(sc->sc_if_xfer, USIE_IF_N_XFER); 493 494 for (x = 0; x != USIE_UCOM_MAX; x++) 495 usbd_transfer_unsetup(sc->sc_uc_xfer[x], USIE_UC_N_XFER); 496 497 device_claim_softc(self); 498 499 usie_free_softc(sc); 500 501 return (0); 502 } 503 504 UCOM_UNLOAD_DRAIN(usie); 505 506 static void 507 usie_free_softc(struct usie_softc *sc) 508 { 509 if (ucom_unref(&sc->sc_super_ucom)) { 510 mtx_destroy(&sc->sc_mtx); 511 device_free_softc(sc); 512 } 513 } 514 515 static void 516 usie_free(struct ucom_softc *ucom) 517 { 518 usie_free_softc(ucom->sc_parent); 519 } 520 521 static void 522 usie_uc_update_line_state(struct ucom_softc *ucom, uint8_t ls) 523 { 524 struct usie_softc *sc = ucom->sc_parent; 525 struct usb_device_request req; 526 527 if (sc->sc_uc_xfer[ucom->sc_subunit][USIE_UC_STATUS] == NULL) 528 return; 529 530 req.bmRequestType = UT_WRITE_CLASS_INTERFACE; 531 req.bRequest = USIE_LINK_STATE; 532 USETW(req.wValue, ls); 533 USETW(req.wIndex, sc->sc_uc_ifnum[ucom->sc_subunit]); 534 USETW(req.wLength, 0); 535 536 DPRINTF("sc_uc_ifnum=%d\n", sc->sc_uc_ifnum[ucom->sc_subunit]); 537 538 usie_do_request(sc, &req, NULL); 539 } 540 541 static void 542 usie_uc_cfg_get_status(struct ucom_softc *ucom, uint8_t *lsr, uint8_t *msr) 543 { 544 struct usie_softc *sc = ucom->sc_parent; 545 546 *msr = sc->sc_msr; 547 *lsr = sc->sc_lsr; 548 } 549 550 static void 551 usie_uc_cfg_set_dtr(struct ucom_softc *ucom, uint8_t flag) 552 { 553 uint8_t dtr; 554 555 dtr = flag ? USIE_LS_DTR : 0; 556 usie_uc_update_line_state(ucom, dtr); 557 } 558 559 static void 560 usie_uc_cfg_set_rts(struct ucom_softc *ucom, uint8_t flag) 561 { 562 uint8_t rts; 563 564 rts = flag ? USIE_LS_RTS : 0; 565 usie_uc_update_line_state(ucom, rts); 566 } 567 568 static void 569 usie_uc_cfg_open(struct ucom_softc *ucom) 570 { 571 struct usie_softc *sc = ucom->sc_parent; 572 573 /* usbd_transfer_start() is NULL safe */ 574 575 usbd_transfer_start(sc->sc_uc_xfer[ucom->sc_subunit][USIE_UC_STATUS]); 576 } 577 578 static void 579 usie_uc_cfg_close(struct ucom_softc *ucom) 580 { 581 struct usie_softc *sc = ucom->sc_parent; 582 583 usbd_transfer_stop(sc->sc_uc_xfer[ucom->sc_subunit][USIE_UC_STATUS]); 584 } 585 586 static void 587 usie_uc_start_read(struct ucom_softc *ucom) 588 { 589 struct usie_softc *sc = ucom->sc_parent; 590 591 usbd_transfer_start(sc->sc_uc_xfer[ucom->sc_subunit][USIE_UC_RX]); 592 } 593 594 static void 595 usie_uc_stop_read(struct ucom_softc *ucom) 596 { 597 struct usie_softc *sc = ucom->sc_parent; 598 599 usbd_transfer_stop(sc->sc_uc_xfer[ucom->sc_subunit][USIE_UC_RX]); 600 } 601 602 static void 603 usie_uc_start_write(struct ucom_softc *ucom) 604 { 605 struct usie_softc *sc = ucom->sc_parent; 606 607 usbd_transfer_start(sc->sc_uc_xfer[ucom->sc_subunit][USIE_UC_TX]); 608 } 609 610 static void 611 usie_uc_stop_write(struct ucom_softc *ucom) 612 { 613 struct usie_softc *sc = ucom->sc_parent; 614 615 usbd_transfer_stop(sc->sc_uc_xfer[ucom->sc_subunit][USIE_UC_TX]); 616 } 617 618 static void 619 usie_uc_rx_callback(struct usb_xfer *xfer, usb_error_t error) 620 { 621 struct ucom_softc *ucom = usbd_xfer_softc(xfer); 622 struct usie_softc *sc = ucom->sc_parent; 623 struct usb_page_cache *pc; 624 uint32_t actlen; 625 626 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL); 627 628 switch (USB_GET_STATE(xfer)) { 629 case USB_ST_TRANSFERRED: 630 pc = usbd_xfer_get_frame(xfer, 0); 631 632 /* handle CnS response */ 633 if (ucom == sc->sc_ucom && actlen >= USIE_HIPCNS_MIN) { 634 DPRINTF("transferred=%u\n", actlen); 635 636 /* check if it is really CnS reply */ 637 usbd_copy_out(pc, 0, sc->sc_resp_temp, 1); 638 639 if (sc->sc_resp_temp[0] == USIE_HIP_FRM_CHR) { 640 /* verify actlen */ 641 if (actlen > USIE_BUFSIZE) 642 actlen = USIE_BUFSIZE; 643 644 /* get complete message */ 645 usbd_copy_out(pc, 0, sc->sc_resp_temp, actlen); 646 usie_hip_rsp(sc, sc->sc_resp_temp, actlen); 647 648 /* need to fall though */ 649 goto tr_setup; 650 } 651 /* else call ucom_put_data() */ 652 } 653 /* standard ucom transfer */ 654 ucom_put_data(ucom, pc, 0, actlen); 655 656 /* fall though */ 657 case USB_ST_SETUP: 658 tr_setup: 659 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer)); 660 usbd_transfer_submit(xfer); 661 break; 662 663 default: /* Error */ 664 if (error != USB_ERR_CANCELLED) { 665 usbd_xfer_set_stall(xfer); 666 goto tr_setup; 667 } 668 break; 669 } 670 } 671 672 static void 673 usie_uc_tx_callback(struct usb_xfer *xfer, usb_error_t error) 674 { 675 struct ucom_softc *ucom = usbd_xfer_softc(xfer); 676 struct usb_page_cache *pc; 677 uint32_t actlen; 678 679 switch (USB_GET_STATE(xfer)) { 680 case USB_ST_TRANSFERRED: 681 case USB_ST_SETUP: 682 tr_setup: 683 pc = usbd_xfer_get_frame(xfer, 0); 684 685 /* handle CnS request */ 686 struct mbuf *m = usbd_xfer_get_priv(xfer); 687 688 if (m != NULL) { 689 usbd_m_copy_in(pc, 0, m, 0, m->m_pkthdr.len); 690 usbd_xfer_set_frame_len(xfer, 0, m->m_pkthdr.len); 691 usbd_xfer_set_priv(xfer, NULL); 692 usbd_transfer_submit(xfer); 693 m_freem(m); 694 break; 695 } 696 /* standard ucom transfer */ 697 if (ucom_get_data(ucom, pc, 0, USIE_BUFSIZE, &actlen)) { 698 usbd_xfer_set_frame_len(xfer, 0, actlen); 699 usbd_transfer_submit(xfer); 700 } 701 break; 702 703 default: /* Error */ 704 if (error != USB_ERR_CANCELLED) { 705 usbd_xfer_set_stall(xfer); 706 goto tr_setup; 707 } 708 break; 709 } 710 } 711 712 static void 713 usie_uc_status_callback(struct usb_xfer *xfer, usb_error_t error) 714 { 715 struct usb_page_cache *pc; 716 struct { 717 struct usb_device_request req; 718 uint16_t param; 719 } st; 720 uint32_t actlen; 721 uint16_t param; 722 723 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL); 724 725 switch (USB_GET_STATE(xfer)) { 726 case USB_ST_TRANSFERRED: 727 DPRINTFN(4, "info received, actlen=%u\n", actlen); 728 729 if (actlen < sizeof(st)) { 730 DPRINTF("data too short actlen=%u\n", actlen); 731 goto tr_setup; 732 } 733 pc = usbd_xfer_get_frame(xfer, 0); 734 usbd_copy_out(pc, 0, &st, sizeof(st)); 735 736 if (st.req.bmRequestType == 0xa1 && st.req.bRequest == 0x20) { 737 struct ucom_softc *ucom = usbd_xfer_softc(xfer); 738 struct usie_softc *sc = ucom->sc_parent; 739 740 param = le16toh(st.param); 741 DPRINTF("param=%x\n", param); 742 sc->sc_msr = sc->sc_lsr = 0; 743 sc->sc_msr |= (param & USIE_DCD) ? SER_DCD : 0; 744 sc->sc_msr |= (param & USIE_DSR) ? SER_DSR : 0; 745 sc->sc_msr |= (param & USIE_RI) ? SER_RI : 0; 746 sc->sc_msr |= (param & USIE_CTS) ? 0 : SER_CTS; 747 sc->sc_msr |= (param & USIE_RTS) ? SER_RTS : 0; 748 sc->sc_msr |= (param & USIE_DTR) ? SER_DTR : 0; 749 } 750 /* fall though */ 751 case USB_ST_SETUP: 752 tr_setup: 753 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer)); 754 usbd_transfer_submit(xfer); 755 break; 756 757 default: /* Error */ 758 DPRINTF("USB transfer error, %s\n", 759 usbd_errstr(error)); 760 761 if (error != USB_ERR_CANCELLED) { 762 usbd_xfer_set_stall(xfer); 763 goto tr_setup; 764 } 765 break; 766 } 767 } 768 769 static void 770 usie_if_rx_callback(struct usb_xfer *xfer, usb_error_t error) 771 { 772 struct epoch_tracker et; 773 struct usie_softc *sc = usbd_xfer_softc(xfer); 774 if_t ifp = sc->sc_ifp; 775 struct mbuf *m0; 776 struct mbuf *m = NULL; 777 struct usie_desc *rxd; 778 uint32_t actlen; 779 uint16_t err; 780 uint16_t pkt; 781 uint16_t ipl; 782 uint16_t len; 783 uint16_t diff; 784 uint8_t pad; 785 uint8_t ipv; 786 787 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL); 788 789 switch (USB_GET_STATE(xfer)) { 790 case USB_ST_TRANSFERRED: 791 DPRINTFN(15, "rx done, actlen=%u\n", actlen); 792 793 if (actlen < sizeof(struct usie_hip)) { 794 DPRINTF("data too short %u\n", actlen); 795 goto tr_setup; 796 } 797 m = sc->sc_rxm; 798 sc->sc_rxm = NULL; 799 800 /* fall though */ 801 case USB_ST_SETUP: 802 tr_setup: 803 804 if (sc->sc_rxm == NULL) { 805 sc->sc_rxm = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, 806 MJUMPAGESIZE /* could be bigger than MCLBYTES */ ); 807 } 808 if (sc->sc_rxm == NULL) { 809 DPRINTF("could not allocate Rx mbuf\n"); 810 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1); 811 usbd_xfer_set_stall(xfer); 812 usbd_xfer_set_frames(xfer, 0); 813 } else { 814 /* 815 * Directly loading a mbuf cluster into DMA to 816 * save some data copying. This works because 817 * there is only one cluster. 818 */ 819 usbd_xfer_set_frame_data(xfer, 0, 820 mtod(sc->sc_rxm, caddr_t), MIN(MJUMPAGESIZE, USIE_RXSZ_MAX)); 821 usbd_xfer_set_frames(xfer, 1); 822 } 823 usbd_transfer_submit(xfer); 824 break; 825 826 default: /* Error */ 827 DPRINTF("USB transfer error, %s\n", usbd_errstr(error)); 828 829 if (error != USB_ERR_CANCELLED) { 830 /* try to clear stall first */ 831 usbd_xfer_set_stall(xfer); 832 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1); 833 goto tr_setup; 834 } 835 if (sc->sc_rxm != NULL) { 836 m_freem(sc->sc_rxm); 837 sc->sc_rxm = NULL; 838 } 839 break; 840 } 841 842 if (m == NULL) 843 return; 844 845 mtx_unlock(&sc->sc_mtx); 846 847 m->m_pkthdr.len = m->m_len = actlen; 848 849 err = pkt = 0; 850 851 /* HW can aggregate multiple frames in a single USB xfer */ 852 NET_EPOCH_ENTER(et); 853 for (;;) { 854 rxd = mtod(m, struct usie_desc *); 855 856 len = be16toh(rxd->hip.len) & USIE_HIP_IP_LEN_MASK; 857 pad = (rxd->hip.id & USIE_HIP_PAD) ? 1 : 0; 858 ipl = (len - pad - ETHER_HDR_LEN); 859 if (ipl >= len) { 860 DPRINTF("Corrupt frame\n"); 861 m_freem(m); 862 break; 863 } 864 diff = sizeof(struct usie_desc) + ipl + pad; 865 866 if (((rxd->hip.id & USIE_HIP_MASK) != USIE_HIP_IP) || 867 (be16toh(rxd->desc_type) & USIE_TYPE_MASK) != USIE_IP_RX) { 868 DPRINTF("received wrong type of packet\n"); 869 m->m_data += diff; 870 m->m_pkthdr.len = (m->m_len -= diff); 871 err++; 872 if (m->m_pkthdr.len > 0) 873 continue; 874 m_freem(m); 875 break; 876 } 877 switch (be16toh(rxd->ethhdr.ether_type)) { 878 case ETHERTYPE_IP: 879 ipv = NETISR_IP; 880 break; 881 #ifdef INET6 882 case ETHERTYPE_IPV6: 883 ipv = NETISR_IPV6; 884 break; 885 #endif 886 default: 887 DPRINTF("unsupported ether type\n"); 888 err++; 889 break; 890 } 891 892 /* the last packet */ 893 if (m->m_pkthdr.len <= diff) { 894 m->m_data += (sizeof(struct usie_desc) + pad); 895 m->m_pkthdr.len = m->m_len = ipl; 896 m->m_pkthdr.rcvif = ifp; 897 BPF_MTAP(sc->sc_ifp, m); 898 netisr_dispatch(ipv, m); 899 break; 900 } 901 /* copy aggregated frames to another mbuf */ 902 m0 = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR); 903 if (__predict_false(m0 == NULL)) { 904 DPRINTF("could not allocate mbuf\n"); 905 err++; 906 m_freem(m); 907 break; 908 } 909 m_copydata(m, sizeof(struct usie_desc) + pad, ipl, mtod(m0, caddr_t)); 910 m0->m_pkthdr.rcvif = ifp; 911 m0->m_pkthdr.len = m0->m_len = ipl; 912 913 BPF_MTAP(sc->sc_ifp, m0); 914 netisr_dispatch(ipv, m0); 915 916 m->m_data += diff; 917 m->m_pkthdr.len = (m->m_len -= diff); 918 } 919 NET_EPOCH_EXIT(et); 920 921 mtx_lock(&sc->sc_mtx); 922 923 if_inc_counter(ifp, IFCOUNTER_IERRORS, err); 924 if_inc_counter(ifp, IFCOUNTER_IPACKETS, pkt); 925 } 926 927 static void 928 usie_if_tx_callback(struct usb_xfer *xfer, usb_error_t error) 929 { 930 struct usie_softc *sc = usbd_xfer_softc(xfer); 931 struct usb_page_cache *pc; 932 if_t ifp = sc->sc_ifp; 933 struct mbuf *m; 934 uint16_t size; 935 936 switch (USB_GET_STATE(xfer)) { 937 case USB_ST_TRANSFERRED: 938 DPRINTFN(11, "transfer complete\n"); 939 if_setdrvflagbits(ifp, 0, IFF_DRV_OACTIVE); 940 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1); 941 942 /* fall though */ 943 case USB_ST_SETUP: 944 tr_setup: 945 946 if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) == 0) 947 break; 948 949 m = if_dequeue(ifp); 950 if (m == NULL) 951 break; 952 953 if (m->m_pkthdr.len > (int)(MCLBYTES - ETHER_HDR_LEN + 954 ETHER_CRC_LEN - sizeof(sc->sc_txd))) { 955 DPRINTF("packet len is too big: %d\n", 956 m->m_pkthdr.len); 957 break; 958 } 959 pc = usbd_xfer_get_frame(xfer, 0); 960 961 sc->sc_txd.hip.len = htobe16(m->m_pkthdr.len + 962 ETHER_HDR_LEN + ETHER_CRC_LEN); 963 size = sizeof(sc->sc_txd); 964 965 usbd_copy_in(pc, 0, &sc->sc_txd, size); 966 usbd_m_copy_in(pc, size, m, 0, m->m_pkthdr.len); 967 usbd_xfer_set_frame_len(xfer, 0, m->m_pkthdr.len + 968 size + ETHER_CRC_LEN); 969 970 BPF_MTAP(ifp, m); 971 972 m_freem(m); 973 974 usbd_transfer_submit(xfer); 975 break; 976 977 default: /* Error */ 978 DPRINTF("USB transfer error, %s\n", 979 usbd_errstr(error)); 980 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 981 982 if (error != USB_ERR_CANCELLED) { 983 usbd_xfer_set_stall(xfer); 984 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1); 985 goto tr_setup; 986 } 987 break; 988 } 989 } 990 991 static void 992 usie_if_status_callback(struct usb_xfer *xfer, usb_error_t error) 993 { 994 struct usie_softc *sc = usbd_xfer_softc(xfer); 995 struct usb_page_cache *pc; 996 struct usb_cdc_notification cdc; 997 uint32_t actlen; 998 999 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL); 1000 1001 switch (USB_GET_STATE(xfer)) { 1002 case USB_ST_TRANSFERRED: 1003 DPRINTFN(4, "info received, actlen=%d\n", actlen); 1004 1005 /* usb_cdc_notification - .data[16] */ 1006 if (actlen < (sizeof(cdc) - 16)) { 1007 DPRINTF("data too short %d\n", actlen); 1008 goto tr_setup; 1009 } 1010 pc = usbd_xfer_get_frame(xfer, 0); 1011 usbd_copy_out(pc, 0, &cdc, (sizeof(cdc) - 16)); 1012 1013 DPRINTFN(4, "bNotification=%x\n", cdc.bNotification); 1014 1015 if (cdc.bNotification & UCDC_N_RESPONSE_AVAILABLE) { 1016 taskqueue_enqueue(taskqueue_thread, 1017 &sc->sc_if_status_task); 1018 } 1019 /* fall though */ 1020 case USB_ST_SETUP: 1021 tr_setup: 1022 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer)); 1023 usbd_transfer_submit(xfer); 1024 break; 1025 1026 default: /* Error */ 1027 DPRINTF("USB transfer error, %s\n", 1028 usbd_errstr(error)); 1029 1030 if (error != USB_ERR_CANCELLED) { 1031 usbd_xfer_set_stall(xfer); 1032 goto tr_setup; 1033 } 1034 break; 1035 } 1036 } 1037 1038 static void 1039 usie_if_sync_to(void *arg) 1040 { 1041 struct usie_softc *sc = arg; 1042 1043 taskqueue_enqueue(taskqueue_thread, &sc->sc_if_sync_task); 1044 } 1045 1046 static void 1047 usie_if_sync_cb(void *arg, int pending) 1048 { 1049 struct usie_softc *sc = arg; 1050 1051 mtx_lock(&sc->sc_mtx); 1052 1053 /* call twice */ 1054 usie_if_cmd(sc, USIE_HIP_SYNC2M); 1055 usie_if_cmd(sc, USIE_HIP_SYNC2M); 1056 1057 usb_callout_reset(&sc->sc_if_sync_ch, 2 * hz, usie_if_sync_to, sc); 1058 1059 mtx_unlock(&sc->sc_mtx); 1060 } 1061 1062 static void 1063 usie_if_status_cb(void *arg, int pending) 1064 { 1065 struct usie_softc *sc = arg; 1066 if_t ifp = sc->sc_ifp; 1067 struct usb_device_request req; 1068 struct usie_hip *hip; 1069 struct usie_lsi *lsi; 1070 uint16_t actlen; 1071 uint8_t ntries; 1072 uint8_t pad; 1073 1074 mtx_lock(&sc->sc_mtx); 1075 1076 req.bmRequestType = UT_READ_CLASS_INTERFACE; 1077 req.bRequest = UCDC_GET_ENCAPSULATED_RESPONSE; 1078 USETW(req.wValue, 0); 1079 USETW(req.wIndex, sc->sc_if_ifnum); 1080 USETW(req.wLength, sizeof(sc->sc_status_temp)); 1081 1082 for (ntries = 0; ntries != 10; ntries++) { 1083 int err; 1084 1085 err = usbd_do_request_flags(sc->sc_udev, 1086 &sc->sc_mtx, &req, sc->sc_status_temp, USB_SHORT_XFER_OK, 1087 &actlen, USB_DEFAULT_TIMEOUT); 1088 1089 if (err == 0) 1090 break; 1091 1092 DPRINTF("Control request failed: %s %d/10\n", 1093 usbd_errstr(err), ntries); 1094 1095 usb_pause_mtx(&sc->sc_mtx, USB_MS_TO_TICKS(10)); 1096 } 1097 1098 if (ntries == 10) { 1099 mtx_unlock(&sc->sc_mtx); 1100 DPRINTF("Timeout\n"); 1101 return; 1102 } 1103 1104 hip = (struct usie_hip *)sc->sc_status_temp; 1105 1106 pad = (hip->id & USIE_HIP_PAD) ? 1 : 0; 1107 1108 DPRINTF("hip.id=%x hip.len=%d actlen=%u pad=%d\n", 1109 hip->id, be16toh(hip->len), actlen, pad); 1110 1111 switch (hip->id & USIE_HIP_MASK) { 1112 case USIE_HIP_SYNC2H: 1113 usie_if_cmd(sc, USIE_HIP_SYNC2M); 1114 break; 1115 case USIE_HIP_RESTR: 1116 usb_callout_stop(&sc->sc_if_sync_ch); 1117 break; 1118 case USIE_HIP_UMTS: 1119 lsi = (struct usie_lsi *)( 1120 sc->sc_status_temp + sizeof(struct usie_hip) + pad); 1121 1122 DPRINTF("lsi.proto=%x lsi.len=%d\n", lsi->proto, 1123 be16toh(lsi->len)); 1124 1125 if (lsi->proto != USIE_LSI_UMTS) 1126 break; 1127 1128 if (lsi->area == USIE_LSI_AREA_NO || 1129 lsi->area == USIE_LSI_AREA_NODATA) { 1130 device_printf(sc->sc_dev, "no service available\n"); 1131 break; 1132 } 1133 if (lsi->state == USIE_LSI_STATE_IDLE) { 1134 DPRINTF("lsi.state=%x\n", lsi->state); 1135 break; 1136 } 1137 DPRINTF("ctx=%x\n", hip->param); 1138 sc->sc_txd.hip.param = hip->param; 1139 1140 sc->sc_net.addr_len = lsi->pdp_addr_len; 1141 memcpy(&sc->sc_net.dns1_addr, &lsi->dns1_addr, 16); 1142 memcpy(&sc->sc_net.dns2_addr, &lsi->dns2_addr, 16); 1143 memcpy(sc->sc_net.pdp_addr, lsi->pdp_addr, 16); 1144 memcpy(sc->sc_net.gw_addr, lsi->gw_addr, 16); 1145 if_setflagbits(ifp, IFF_UP, 0); 1146 if_setdrvflagbits(ifp, IFF_DRV_RUNNING, 0); 1147 1148 device_printf(sc->sc_dev, "IP Addr=%d.%d.%d.%d\n", 1149 *lsi->pdp_addr, *(lsi->pdp_addr + 1), 1150 *(lsi->pdp_addr + 2), *(lsi->pdp_addr + 3)); 1151 device_printf(sc->sc_dev, "Gateway Addr=%d.%d.%d.%d\n", 1152 *lsi->gw_addr, *(lsi->gw_addr + 1), 1153 *(lsi->gw_addr + 2), *(lsi->gw_addr + 3)); 1154 device_printf(sc->sc_dev, "Prim NS Addr=%d.%d.%d.%d\n", 1155 *lsi->dns1_addr, *(lsi->dns1_addr + 1), 1156 *(lsi->dns1_addr + 2), *(lsi->dns1_addr + 3)); 1157 device_printf(sc->sc_dev, "Scnd NS Addr=%d.%d.%d.%d\n", 1158 *lsi->dns2_addr, *(lsi->dns2_addr + 1), 1159 *(lsi->dns2_addr + 2), *(lsi->dns2_addr + 3)); 1160 1161 usie_cns_req(sc, USIE_CNS_ID_RSSI, USIE_CNS_OB_RSSI); 1162 break; 1163 1164 case USIE_HIP_RCGI: 1165 /* ignore, workaround for sloppy windows */ 1166 break; 1167 default: 1168 DPRINTF("undefined msgid: %x\n", hip->id); 1169 break; 1170 } 1171 1172 mtx_unlock(&sc->sc_mtx); 1173 } 1174 1175 static void 1176 usie_if_start(if_t ifp) 1177 { 1178 struct usie_softc *sc = if_getsoftc(ifp); 1179 1180 if (!(if_getdrvflags(ifp) & IFF_DRV_RUNNING)) { 1181 DPRINTF("Not running\n"); 1182 return; 1183 } 1184 mtx_lock(&sc->sc_mtx); 1185 usbd_transfer_start(sc->sc_if_xfer[USIE_IF_TX]); 1186 mtx_unlock(&sc->sc_mtx); 1187 1188 DPRINTFN(3, "interface started\n"); 1189 } 1190 1191 static int 1192 usie_if_output(if_t ifp, struct mbuf *m, const struct sockaddr *dst, 1193 struct route *ro) 1194 { 1195 int err; 1196 1197 DPRINTF("proto=%x\n", dst->sa_family); 1198 1199 switch (dst->sa_family) { 1200 #ifdef INET6 1201 case AF_INET6; 1202 /* fall though */ 1203 #endif 1204 case AF_INET: 1205 break; 1206 1207 /* silently drop dhclient packets */ 1208 case AF_UNSPEC: 1209 m_freem(m); 1210 return (0); 1211 1212 /* drop other packet types */ 1213 default: 1214 m_freem(m); 1215 return (EAFNOSUPPORT); 1216 } 1217 1218 err = if_transmit(ifp, m); 1219 if (err) { 1220 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 1221 return (ENOBUFS); 1222 } 1223 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1); 1224 1225 return (0); 1226 } 1227 1228 static void 1229 usie_if_init(void *arg) 1230 { 1231 struct usie_softc *sc = arg; 1232 if_t ifp = sc->sc_ifp; 1233 uint8_t i; 1234 1235 mtx_lock(&sc->sc_mtx); 1236 1237 /* write tx descriptor */ 1238 sc->sc_txd.hip.id = USIE_HIP_CTX; 1239 sc->sc_txd.hip.param = 0; /* init value */ 1240 sc->sc_txd.desc_type = htobe16(USIE_IP_TX); 1241 1242 for (i = 0; i != USIE_IF_N_XFER; i++) 1243 usbd_xfer_set_stall(sc->sc_if_xfer[i]); 1244 1245 usbd_transfer_start(sc->sc_uc_xfer[USIE_HIP_IF][USIE_UC_RX]); 1246 usbd_transfer_start(sc->sc_if_xfer[USIE_IF_STATUS]); 1247 usbd_transfer_start(sc->sc_if_xfer[USIE_IF_RX]); 1248 1249 /* if not running, initiate the modem */ 1250 if (!(if_getdrvflags(ifp) & IFF_DRV_RUNNING)) 1251 usie_cns_req(sc, USIE_CNS_ID_INIT, USIE_CNS_OB_LINK_UPDATE); 1252 1253 mtx_unlock(&sc->sc_mtx); 1254 1255 DPRINTF("ifnet initialized\n"); 1256 } 1257 1258 static void 1259 usie_if_stop(struct usie_softc *sc) 1260 { 1261 usb_callout_drain(&sc->sc_if_sync_ch); 1262 1263 mtx_lock(&sc->sc_mtx); 1264 1265 /* usie_cns_req() clears IFF_* flags */ 1266 usie_cns_req(sc, USIE_CNS_ID_STOP, USIE_CNS_OB_LINK_UPDATE); 1267 1268 usbd_transfer_stop(sc->sc_if_xfer[USIE_IF_TX]); 1269 usbd_transfer_stop(sc->sc_if_xfer[USIE_IF_RX]); 1270 usbd_transfer_stop(sc->sc_if_xfer[USIE_IF_STATUS]); 1271 1272 /* shutdown device */ 1273 usie_if_cmd(sc, USIE_HIP_DOWN); 1274 1275 mtx_unlock(&sc->sc_mtx); 1276 } 1277 1278 static int 1279 usie_if_ioctl(if_t ifp, u_long cmd, caddr_t data) 1280 { 1281 struct usie_softc *sc = if_getsoftc(ifp); 1282 struct ieee80211req *ireq; 1283 struct ieee80211req_sta_info si; 1284 struct ifmediareq *ifmr; 1285 1286 switch (cmd) { 1287 case SIOCSIFFLAGS: 1288 if (if_getflags(ifp) & IFF_UP) { 1289 if (!(if_getdrvflags(ifp) & IFF_DRV_RUNNING)) 1290 usie_if_init(sc); 1291 } else { 1292 if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) 1293 usie_if_stop(sc); 1294 } 1295 break; 1296 1297 case SIOCSIFCAP: 1298 if (!(if_getdrvflags(ifp) & IFF_DRV_RUNNING)) { 1299 device_printf(sc->sc_dev, 1300 "Connect to the network first.\n"); 1301 break; 1302 } 1303 mtx_lock(&sc->sc_mtx); 1304 usie_cns_req(sc, USIE_CNS_ID_RSSI, USIE_CNS_OB_RSSI); 1305 mtx_unlock(&sc->sc_mtx); 1306 break; 1307 1308 case SIOCG80211: 1309 ireq = (struct ieee80211req *)data; 1310 1311 if (ireq->i_type != IEEE80211_IOC_STA_INFO) 1312 break; 1313 1314 memset(&si, 0, sizeof(si)); 1315 si.isi_len = sizeof(si); 1316 /* 1317 * ifconfig expects RSSI in 0.5dBm units 1318 * relative to the noise floor. 1319 */ 1320 si.isi_rssi = 2 * sc->sc_rssi; 1321 if (copyout(&si, (uint8_t *)ireq->i_data + 8, 1322 sizeof(struct ieee80211req_sta_info))) 1323 DPRINTF("copyout failed\n"); 1324 DPRINTF("80211\n"); 1325 break; 1326 1327 case SIOCGIFMEDIA: /* to fool ifconfig */ 1328 ifmr = (struct ifmediareq *)data; 1329 ifmr->ifm_count = 1; 1330 DPRINTF("media\n"); 1331 break; 1332 1333 case SIOCSIFADDR: 1334 break; 1335 1336 default: 1337 return (EINVAL); 1338 } 1339 return (0); 1340 } 1341 1342 static int 1343 usie_do_request(struct usie_softc *sc, struct usb_device_request *req, 1344 void *data) 1345 { 1346 int err = 0; 1347 int ntries; 1348 1349 mtx_assert(&sc->sc_mtx, MA_OWNED); 1350 1351 for (ntries = 0; ntries != 10; ntries++) { 1352 err = usbd_do_request(sc->sc_udev, 1353 &sc->sc_mtx, req, data); 1354 if (err == 0) 1355 break; 1356 1357 DPRINTF("Control request failed: %s %d/10\n", 1358 usbd_errstr(err), ntries); 1359 1360 usb_pause_mtx(&sc->sc_mtx, USB_MS_TO_TICKS(10)); 1361 } 1362 return (err); 1363 } 1364 1365 static int 1366 usie_if_cmd(struct usie_softc *sc, uint8_t cmd) 1367 { 1368 struct usb_device_request req; 1369 struct usie_hip msg; 1370 1371 msg.len = 0; 1372 msg.id = cmd; 1373 msg.param = 0; 1374 1375 req.bmRequestType = UT_WRITE_CLASS_INTERFACE; 1376 req.bRequest = UCDC_SEND_ENCAPSULATED_COMMAND; 1377 USETW(req.wValue, 0); 1378 USETW(req.wIndex, sc->sc_if_ifnum); 1379 USETW(req.wLength, sizeof(msg)); 1380 1381 DPRINTF("cmd=%x\n", cmd); 1382 1383 return (usie_do_request(sc, &req, &msg)); 1384 } 1385 1386 static void 1387 usie_cns_req(struct usie_softc *sc, uint32_t id, uint16_t obj) 1388 { 1389 if_t ifp = sc->sc_ifp; 1390 struct mbuf *m; 1391 struct usb_xfer *xfer; 1392 struct usie_hip *hip; 1393 struct usie_cns *cns; 1394 uint8_t *param; 1395 uint8_t *tmp; 1396 uint8_t cns_len; 1397 1398 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR); 1399 if (__predict_false(m == NULL)) { 1400 DPRINTF("could not allocate mbuf\n"); 1401 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1); 1402 return; 1403 } 1404 /* to align usie_hip{} on 32 bit */ 1405 m->m_data += 3; 1406 param = mtod(m, uint8_t *); 1407 *param++ = USIE_HIP_FRM_CHR; 1408 hip = (struct usie_hip *)param; 1409 cns = (struct usie_cns *)(hip + 1); 1410 1411 tmp = param + USIE_HIPCNS_MIN - 2; 1412 1413 switch (obj) { 1414 case USIE_CNS_OB_LINK_UPDATE: 1415 cns_len = 2; 1416 cns->op = USIE_CNS_OP_SET; 1417 *tmp++ = 1; /* profile ID, always use 1 for now */ 1418 *tmp++ = id == USIE_CNS_ID_INIT ? 1 : 0; 1419 break; 1420 1421 case USIE_CNS_OB_PROF_WRITE: 1422 cns_len = 245; 1423 cns->op = USIE_CNS_OP_SET; 1424 *tmp++ = 1; /* profile ID, always use 1 for now */ 1425 *tmp++ = 2; 1426 memcpy(tmp, &sc->sc_net, 34); 1427 memset(tmp + 35, 0, 245 - 36); 1428 tmp += 243; 1429 break; 1430 1431 case USIE_CNS_OB_RSSI: 1432 cns_len = 0; 1433 cns->op = USIE_CNS_OP_REQ; 1434 break; 1435 1436 default: 1437 DPRINTF("unsupported CnS object type\n"); 1438 return; 1439 } 1440 *tmp = USIE_HIP_FRM_CHR; 1441 1442 hip->len = htobe16(sizeof(struct usie_cns) + cns_len); 1443 hip->id = USIE_HIP_CNS2M; 1444 hip->param = 0; /* none for CnS */ 1445 1446 cns->obj = htobe16(obj); 1447 cns->id = htobe32(id); 1448 cns->len = cns_len; 1449 cns->rsv0 = cns->rsv1 = 0; /* always '0' */ 1450 1451 param = (uint8_t *)(cns + 1); 1452 1453 DPRINTF("param: %16D\n", param, ":"); 1454 1455 m->m_pkthdr.len = m->m_len = USIE_HIPCNS_MIN + cns_len + 2; 1456 1457 xfer = sc->sc_uc_xfer[USIE_HIP_IF][USIE_UC_TX]; 1458 1459 if (usbd_xfer_get_priv(xfer) == NULL) { 1460 usbd_xfer_set_priv(xfer, m); 1461 usbd_transfer_start(xfer); 1462 } else { 1463 DPRINTF("Dropped CNS event\n"); 1464 m_freem(m); 1465 } 1466 } 1467 1468 static void 1469 usie_cns_rsp(struct usie_softc *sc, struct usie_cns *cns) 1470 { 1471 if_t ifp = sc->sc_ifp; 1472 1473 DPRINTF("received CnS\n"); 1474 1475 switch (be16toh(cns->obj)) { 1476 case USIE_CNS_OB_LINK_UPDATE: 1477 if (be32toh(cns->id) & USIE_CNS_ID_INIT) 1478 usie_if_sync_to(sc); 1479 else if (be32toh(cns->id) & USIE_CNS_ID_STOP) { 1480 if_setflagbits(ifp, 0, IFF_UP); 1481 if_setdrvflagbits(ifp, 0, 1482 IFF_DRV_RUNNING | IFF_DRV_OACTIVE); 1483 } else 1484 DPRINTF("undefined link update\n"); 1485 break; 1486 1487 case USIE_CNS_OB_RSSI: 1488 sc->sc_rssi = be16toh(*(int16_t *)(cns + 1)); 1489 if (sc->sc_rssi <= 0) 1490 device_printf(sc->sc_dev, "No signal\n"); 1491 else { 1492 device_printf(sc->sc_dev, "RSSI=%ddBm\n", 1493 sc->sc_rssi - 110); 1494 } 1495 break; 1496 1497 case USIE_CNS_OB_PROF_WRITE: 1498 break; 1499 1500 case USIE_CNS_OB_PDP_READ: 1501 break; 1502 1503 default: 1504 DPRINTF("undefined CnS\n"); 1505 break; 1506 } 1507 } 1508 1509 static void 1510 usie_hip_rsp(struct usie_softc *sc, uint8_t *rsp, uint32_t len) 1511 { 1512 struct usie_hip *hip; 1513 struct usie_cns *cns; 1514 uint32_t i; 1515 uint32_t j; 1516 uint32_t off; 1517 uint8_t tmp[USIE_HIPCNS_MAX] __aligned(4); 1518 1519 for (off = 0; (off + USIE_HIPCNS_MIN) <= len; off++) { 1520 uint8_t pad; 1521 1522 while ((off < len) && (rsp[off] == USIE_HIP_FRM_CHR)) 1523 off++; 1524 1525 /* Unstuff the bytes */ 1526 for (i = j = 0; ((i + off) < len) && 1527 (j < USIE_HIPCNS_MAX); i++) { 1528 if (rsp[i + off] == USIE_HIP_FRM_CHR) 1529 break; 1530 1531 if (rsp[i + off] == USIE_HIP_ESC_CHR) { 1532 if ((i + off + 1) >= len) 1533 break; 1534 tmp[j++] = rsp[i++ + off + 1] ^ 0x20; 1535 } else { 1536 tmp[j++] = rsp[i + off]; 1537 } 1538 } 1539 1540 off += i; 1541 1542 DPRINTF("frame len=%d\n", j); 1543 1544 if (j < sizeof(struct usie_hip)) { 1545 DPRINTF("too little data\n"); 1546 break; 1547 } 1548 /* 1549 * Make sure we are not reading the stack if something 1550 * is wrong. 1551 */ 1552 memset(tmp + j, 0, sizeof(tmp) - j); 1553 1554 hip = (struct usie_hip *)tmp; 1555 1556 DPRINTF("hip: len=%d msgID=%02x, param=%02x\n", 1557 be16toh(hip->len), hip->id, hip->param); 1558 1559 pad = (hip->id & USIE_HIP_PAD) ? 1 : 0; 1560 1561 if ((hip->id & USIE_HIP_MASK) == USIE_HIP_CNS2H) { 1562 cns = (struct usie_cns *)(((uint8_t *)(hip + 1)) + pad); 1563 1564 if (j < (sizeof(struct usie_cns) + 1565 sizeof(struct usie_hip) + pad)) { 1566 DPRINTF("too little data\n"); 1567 break; 1568 } 1569 DPRINTF("cns: obj=%04x, op=%02x, rsv0=%02x, " 1570 "app=%08x, rsv1=%02x, len=%d\n", 1571 be16toh(cns->obj), cns->op, cns->rsv0, 1572 be32toh(cns->id), cns->rsv1, cns->len); 1573 1574 if (cns->op & USIE_CNS_OP_ERR) 1575 DPRINTF("CnS error response\n"); 1576 else 1577 usie_cns_rsp(sc, cns); 1578 1579 i = sizeof(struct usie_hip) + pad + sizeof(struct usie_cns); 1580 j = cns->len; 1581 } else { 1582 i = sizeof(struct usie_hip) + pad; 1583 j = be16toh(hip->len); 1584 } 1585 #ifdef USB_DEBUG 1586 if (usie_debug == 0) 1587 continue; 1588 1589 while (i < USIE_HIPCNS_MAX && j > 0) { 1590 DPRINTF("param[0x%02x] = 0x%02x\n", i, tmp[i]); 1591 i++; 1592 j--; 1593 } 1594 #endif 1595 } 1596 } 1597 1598 static int 1599 usie_driver_loaded(struct module *mod, int what, void *arg) 1600 { 1601 switch (what) { 1602 case MOD_LOAD: 1603 /* register autoinstall handler */ 1604 usie_etag = EVENTHANDLER_REGISTER(usb_dev_configured, 1605 usie_autoinst, NULL, EVENTHANDLER_PRI_ANY); 1606 break; 1607 case MOD_UNLOAD: 1608 EVENTHANDLER_DEREGISTER(usb_dev_configured, usie_etag); 1609 break; 1610 default: 1611 return (EOPNOTSUPP); 1612 } 1613 return (0); 1614 } 1615