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