1 /*- 2 * Copyright (c) 2010 Fredrik Lindberg <fli@shapeshifter.se> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 * 25 */ 26 #include <sys/cdefs.h> 27 __FBSDID("$FreeBSD$"); 28 29 #include <sys/param.h> 30 #include <sys/types.h> 31 #include <sys/sockio.h> 32 #include <sys/mbuf.h> 33 #include <sys/malloc.h> 34 #include <sys/kernel.h> 35 #include <sys/module.h> 36 #include <sys/socket.h> 37 #include <sys/tty.h> 38 #include <sys/sysctl.h> 39 #include <sys/condvar.h> 40 #include <sys/sx.h> 41 #include <sys/proc.h> 42 #include <sys/conf.h> 43 #include <sys/bus.h> 44 #include <sys/systm.h> 45 #include <sys/limits.h> 46 47 #include <machine/bus.h> 48 49 #include <net/if.h> 50 #include <net/if_var.h> 51 #include <net/if_types.h> 52 #include <net/netisr.h> 53 #include <net/bpf.h> 54 #include <netinet/in.h> 55 #include <netinet/ip.h> 56 #include <netinet/ip6.h> 57 58 #include <dev/usb/usb.h> 59 #include <dev/usb/usbdi.h> 60 #include <dev/usb/usbdi_util.h> 61 #include <dev/usb/usb_cdc.h> 62 #include "usbdevs.h" 63 #define USB_DEBUG_VAR uhso_debug 64 #include <dev/usb/usb_debug.h> 65 #include <dev/usb/usb_process.h> 66 #include <dev/usb/usb_busdma.h> 67 #include <dev/usb/usb_msctest.h> 68 69 #include <dev/usb/serial/usb_serial.h> 70 71 struct uhso_tty { 72 struct uhso_softc *ht_sc; 73 struct usb_xfer *ht_xfer[3]; 74 int ht_muxport; /* Mux. port no */ 75 int ht_open; 76 char ht_name[32]; 77 }; 78 79 struct uhso_softc { 80 device_t sc_dev; 81 struct usb_device *sc_udev; 82 struct mtx sc_mtx; 83 uint32_t sc_type; /* Interface definition */ 84 int sc_radio; 85 86 struct usb_xfer *sc_xfer[3]; 87 uint8_t sc_iface_no; 88 uint8_t sc_iface_index; 89 90 /* Control pipe */ 91 struct usb_xfer * sc_ctrl_xfer[2]; 92 uint8_t sc_ctrl_iface_no; 93 94 /* Network */ 95 struct usb_xfer *sc_if_xfer[2]; 96 struct ifnet *sc_ifp; 97 struct mbuf *sc_mwait; /* Partial packet */ 98 size_t sc_waitlen; /* No. of outstanding bytes */ 99 struct ifqueue sc_rxq; 100 struct callout sc_c; 101 102 /* TTY related structures */ 103 struct ucom_super_softc sc_super_ucom; 104 int sc_ttys; 105 struct uhso_tty *sc_tty; 106 struct ucom_softc *sc_ucom; 107 int sc_msr; 108 int sc_lsr; 109 int sc_line; 110 }; 111 112 #define UHSO_MAX_MTU 2048 113 114 /* 115 * There are mainly two type of cards floating around. 116 * The first one has 2,3 or 4 interfaces with a multiplexed serial port 117 * and packet interface on the first interface and bulk serial ports 118 * on the others. 119 * The second type of card has several other interfaces, their purpose 120 * can be detected during run-time. 121 */ 122 #define UHSO_IFACE_SPEC(usb_type, port, port_type) \ 123 (((usb_type) << 24) | ((port) << 16) | (port_type)) 124 125 #define UHSO_IFACE_USB_TYPE(x) ((x >> 24) & 0xff) 126 #define UHSO_IFACE_PORT(x) ((x >> 16) & 0xff) 127 #define UHSO_IFACE_PORT_TYPE(x) (x & 0xff) 128 129 /* 130 * USB interface types 131 */ 132 #define UHSO_IF_NET 0x01 /* Network packet interface */ 133 #define UHSO_IF_MUX 0x02 /* Multiplexed serial port */ 134 #define UHSO_IF_BULK 0x04 /* Bulk interface */ 135 136 /* 137 * Port types 138 */ 139 #define UHSO_PORT_UNKNOWN 0x00 140 #define UHSO_PORT_SERIAL 0x01 /* Serial port */ 141 #define UHSO_PORT_NETWORK 0x02 /* Network packet interface */ 142 143 /* 144 * Multiplexed serial port destination sub-port names 145 */ 146 #define UHSO_MPORT_TYPE_CTL 0x00 /* Control port */ 147 #define UHSO_MPORT_TYPE_APP 0x01 /* Application */ 148 #define UHSO_MPORT_TYPE_PCSC 0x02 149 #define UHSO_MPORT_TYPE_GPS 0x03 150 #define UHSO_MPORT_TYPE_APP2 0x04 /* Secondary application */ 151 #define UHSO_MPORT_TYPE_MAX UHSO_MPORT_TYPE_APP2 152 #define UHSO_MPORT_TYPE_NOMAX 8 /* Max number of mux ports */ 153 154 /* 155 * Port definitions 156 * Note that these definitions are arbitrary and do not match the values 157 * returned by the auto config descriptor. 158 */ 159 #define UHSO_PORT_TYPE_UNKNOWN 0x00 160 #define UHSO_PORT_TYPE_CTL 0x01 161 #define UHSO_PORT_TYPE_APP 0x02 162 #define UHSO_PORT_TYPE_APP2 0x03 163 #define UHSO_PORT_TYPE_MODEM 0x04 164 #define UHSO_PORT_TYPE_NETWORK 0x05 165 #define UHSO_PORT_TYPE_DIAG 0x06 166 #define UHSO_PORT_TYPE_DIAG2 0x07 167 #define UHSO_PORT_TYPE_GPS 0x08 168 #define UHSO_PORT_TYPE_GPSCTL 0x09 169 #define UHSO_PORT_TYPE_PCSC 0x0a 170 #define UHSO_PORT_TYPE_MSD 0x0b 171 #define UHSO_PORT_TYPE_VOICE 0x0c 172 #define UHSO_PORT_TYPE_MAX 0x0c 173 174 static eventhandler_tag uhso_etag; 175 176 /* Overall port type */ 177 static char *uhso_port[] = { 178 "Unknown", 179 "Serial", 180 "Network", 181 "Network/Serial" 182 }; 183 184 /* 185 * Map between interface port type read from device and description type. 186 * The position in this array is a direct map to the auto config 187 * descriptor values. 188 */ 189 static unsigned char uhso_port_map[] = { 190 UHSO_PORT_TYPE_UNKNOWN, 191 UHSO_PORT_TYPE_DIAG, 192 UHSO_PORT_TYPE_GPS, 193 UHSO_PORT_TYPE_GPSCTL, 194 UHSO_PORT_TYPE_APP, 195 UHSO_PORT_TYPE_APP2, 196 UHSO_PORT_TYPE_CTL, 197 UHSO_PORT_TYPE_NETWORK, 198 UHSO_PORT_TYPE_MODEM, 199 UHSO_PORT_TYPE_MSD, 200 UHSO_PORT_TYPE_PCSC, 201 UHSO_PORT_TYPE_VOICE 202 }; 203 static char uhso_port_map_max = sizeof(uhso_port_map) / sizeof(char); 204 205 static unsigned char uhso_mux_port_map[] = { 206 UHSO_PORT_TYPE_CTL, 207 UHSO_PORT_TYPE_APP, 208 UHSO_PORT_TYPE_PCSC, 209 UHSO_PORT_TYPE_GPS, 210 UHSO_PORT_TYPE_APP2 211 }; 212 213 static char *uhso_port_type[] = { 214 "Unknown", /* Not a valid port */ 215 "Control", 216 "Application", 217 "Application (Secondary)", 218 "Modem", 219 "Network", 220 "Diagnostic", 221 "Diagnostic (Secondary)", 222 "GPS", 223 "GPS Control", 224 "PC Smartcard", 225 "MSD", 226 "Voice", 227 }; 228 229 static char *uhso_port_type_sysctl[] = { 230 "unknown", 231 "control", 232 "application", 233 "application", 234 "modem", 235 "network", 236 "diagnostic", 237 "diagnostic", 238 "gps", 239 "gps_control", 240 "pcsc", 241 "msd", 242 "voice", 243 }; 244 245 #define UHSO_STATIC_IFACE 0x01 246 #define UHSO_AUTO_IFACE 0x02 247 248 /* ifnet device unit allocations */ 249 static struct unrhdr *uhso_ifnet_unit = NULL; 250 251 static const STRUCT_USB_HOST_ID uhso_devs[] = { 252 #define UHSO_DEV(v,p,i) { USB_VPI(USB_VENDOR_##v, USB_PRODUCT_##v##_##p, i) } 253 /* Option GlobeTrotter MAX 7.2 with upgraded firmware */ 254 UHSO_DEV(OPTION, GTMAX72, UHSO_STATIC_IFACE), 255 /* Option GlobeSurfer iCON 7.2 */ 256 UHSO_DEV(OPTION, GSICON72, UHSO_STATIC_IFACE), 257 /* Option iCON 225 */ 258 UHSO_DEV(OPTION, GTHSDPA, UHSO_STATIC_IFACE), 259 /* Option GlobeSurfer iCON HSUPA */ 260 UHSO_DEV(OPTION, GSICONHSUPA, UHSO_STATIC_IFACE), 261 /* Option GlobeTrotter HSUPA */ 262 UHSO_DEV(OPTION, GTHSUPA, UHSO_STATIC_IFACE), 263 /* GE40x */ 264 UHSO_DEV(OPTION, GE40X, UHSO_AUTO_IFACE), 265 UHSO_DEV(OPTION, GE40X_1, UHSO_AUTO_IFACE), 266 UHSO_DEV(OPTION, GE40X_2, UHSO_AUTO_IFACE), 267 UHSO_DEV(OPTION, GE40X_3, UHSO_AUTO_IFACE), 268 /* Option GlobeSurfer iCON 401 */ 269 UHSO_DEV(OPTION, ICON401, UHSO_AUTO_IFACE), 270 /* Option GlobeTrotter Module 382 */ 271 UHSO_DEV(OPTION, GMT382, UHSO_AUTO_IFACE), 272 /* Option GTM661W */ 273 UHSO_DEV(OPTION, GTM661W, UHSO_AUTO_IFACE), 274 /* Option iCON EDGE */ 275 UHSO_DEV(OPTION, ICONEDGE, UHSO_STATIC_IFACE), 276 /* Option Module HSxPA */ 277 UHSO_DEV(OPTION, MODHSXPA, UHSO_STATIC_IFACE), 278 /* Option iCON 321 */ 279 UHSO_DEV(OPTION, ICON321, UHSO_STATIC_IFACE), 280 /* Option iCON 322 */ 281 UHSO_DEV(OPTION, GTICON322, UHSO_STATIC_IFACE), 282 /* Option iCON 505 */ 283 UHSO_DEV(OPTION, ICON505, UHSO_AUTO_IFACE), 284 /* Option iCON 452 */ 285 UHSO_DEV(OPTION, ICON505, UHSO_AUTO_IFACE), 286 #undef UHSO_DEV 287 }; 288 289 static SYSCTL_NODE(_hw_usb, OID_AUTO, uhso, CTLFLAG_RW, 0, "USB uhso"); 290 static int uhso_autoswitch = 1; 291 SYSCTL_INT(_hw_usb_uhso, OID_AUTO, auto_switch, CTLFLAG_RW, 292 &uhso_autoswitch, 0, "Automatically switch to modem mode"); 293 294 #ifdef USB_DEBUG 295 #ifdef UHSO_DEBUG 296 static int uhso_debug = UHSO_DEBUG; 297 #else 298 static int uhso_debug = -1; 299 #endif 300 301 SYSCTL_INT(_hw_usb_uhso, OID_AUTO, debug, CTLFLAG_RW, 302 &uhso_debug, 0, "Debug level"); 303 304 #define UHSO_DPRINTF(n, x, ...) {\ 305 if (uhso_debug >= n) {\ 306 printf("%s: " x, __func__, ##__VA_ARGS__);\ 307 }\ 308 } 309 #else 310 #define UHSO_DPRINTF(n, x, ...) 311 #endif 312 313 #ifdef UHSO_DEBUG_HEXDUMP 314 # define UHSO_HEXDUMP(_buf, _len) do { \ 315 { \ 316 size_t __tmp; \ 317 const char *__buf = (const char *)_buf; \ 318 for (__tmp = 0; __tmp < _len; __tmp++) \ 319 printf("%02hhx ", *__buf++); \ 320 printf("\n"); \ 321 } \ 322 } while(0) 323 #else 324 # define UHSO_HEXDUMP(_buf, _len) 325 #endif 326 327 enum { 328 UHSO_MUX_ENDPT_INTR = 0, 329 UHSO_MUX_ENDPT_MAX 330 }; 331 332 enum { 333 UHSO_CTRL_READ = 0, 334 UHSO_CTRL_WRITE, 335 UHSO_CTRL_MAX 336 }; 337 338 enum { 339 UHSO_IFNET_READ = 0, 340 UHSO_IFNET_WRITE, 341 UHSO_IFNET_MAX 342 }; 343 344 enum { 345 UHSO_BULK_ENDPT_READ = 0, 346 UHSO_BULK_ENDPT_WRITE, 347 UHSO_BULK_ENDPT_INTR, 348 UHSO_BULK_ENDPT_MAX 349 }; 350 351 static usb_callback_t uhso_mux_intr_callback; 352 static usb_callback_t uhso_mux_read_callback; 353 static usb_callback_t uhso_mux_write_callback; 354 static usb_callback_t uhso_bs_read_callback; 355 static usb_callback_t uhso_bs_write_callback; 356 static usb_callback_t uhso_bs_intr_callback; 357 static usb_callback_t uhso_ifnet_read_callback; 358 static usb_callback_t uhso_ifnet_write_callback; 359 360 /* Config used for the default control pipes */ 361 static const struct usb_config uhso_ctrl_config[UHSO_CTRL_MAX] = { 362 [UHSO_CTRL_READ] = { 363 .type = UE_CONTROL, 364 .endpoint = 0x00, 365 .direction = UE_DIR_ANY, 366 .flags = { .pipe_bof = 1, .short_xfer_ok = 1 }, 367 .bufsize = sizeof(struct usb_device_request) + 1024, 368 .callback = &uhso_mux_read_callback 369 }, 370 371 [UHSO_CTRL_WRITE] = { 372 .type = UE_CONTROL, 373 .endpoint = 0x00, 374 .direction = UE_DIR_ANY, 375 .flags = { .pipe_bof = 1, .force_short_xfer = 1 }, 376 .bufsize = sizeof(struct usb_device_request) + 1024, 377 .timeout = 1000, 378 .callback = &uhso_mux_write_callback 379 } 380 }; 381 382 /* Config for the multiplexed serial ports */ 383 static const struct usb_config uhso_mux_config[UHSO_MUX_ENDPT_MAX] = { 384 [UHSO_MUX_ENDPT_INTR] = { 385 .type = UE_INTERRUPT, 386 .endpoint = UE_ADDR_ANY, 387 .direction = UE_DIR_IN, 388 .flags = { .short_xfer_ok = 1 }, 389 .bufsize = 0, 390 .callback = &uhso_mux_intr_callback, 391 } 392 }; 393 394 /* Config for the raw IP-packet interface */ 395 static const struct usb_config uhso_ifnet_config[UHSO_IFNET_MAX] = { 396 [UHSO_IFNET_READ] = { 397 .type = UE_BULK, 398 .endpoint = UE_ADDR_ANY, 399 .direction = UE_DIR_IN, 400 .flags = { .pipe_bof = 1, .short_xfer_ok = 1 }, 401 .bufsize = MCLBYTES, 402 .callback = &uhso_ifnet_read_callback 403 }, 404 [UHSO_IFNET_WRITE] = { 405 .type = UE_BULK, 406 .endpoint = UE_ADDR_ANY, 407 .direction = UE_DIR_OUT, 408 .flags = { .pipe_bof = 1, .force_short_xfer = 1 }, 409 .bufsize = MCLBYTES, 410 .timeout = 5 * USB_MS_HZ, 411 .callback = &uhso_ifnet_write_callback 412 } 413 }; 414 415 /* Config for interfaces with normal bulk serial ports */ 416 static const struct usb_config uhso_bs_config[UHSO_BULK_ENDPT_MAX] = { 417 [UHSO_BULK_ENDPT_READ] = { 418 .type = UE_BULK, 419 .endpoint = UE_ADDR_ANY, 420 .direction = UE_DIR_IN, 421 .flags = { .pipe_bof = 1, .short_xfer_ok = 1 }, 422 .bufsize = 4096, 423 .callback = &uhso_bs_read_callback 424 }, 425 426 [UHSO_BULK_ENDPT_WRITE] = { 427 .type = UE_BULK, 428 .endpoint = UE_ADDR_ANY, 429 .direction = UE_DIR_OUT, 430 .flags = { .pipe_bof = 1, .force_short_xfer = 1 }, 431 .bufsize = 8192, 432 .callback = &uhso_bs_write_callback 433 }, 434 435 [UHSO_BULK_ENDPT_INTR] = { 436 .type = UE_INTERRUPT, 437 .endpoint = UE_ADDR_ANY, 438 .direction = UE_DIR_IN, 439 .flags = { .short_xfer_ok = 1 }, 440 .bufsize = 0, 441 .callback = &uhso_bs_intr_callback, 442 } 443 }; 444 445 static int uhso_probe_iface(struct uhso_softc *, int, 446 int (*probe)(struct usb_device *, int)); 447 static int uhso_probe_iface_auto(struct usb_device *, int); 448 static int uhso_probe_iface_static(struct usb_device *, int); 449 static int uhso_attach_muxserial(struct uhso_softc *, struct usb_interface *, 450 int type); 451 static int uhso_attach_bulkserial(struct uhso_softc *, struct usb_interface *, 452 int type); 453 static int uhso_attach_ifnet(struct uhso_softc *, struct usb_interface *, 454 int type); 455 static void uhso_test_autoinst(void *, struct usb_device *, 456 struct usb_attach_arg *); 457 static int uhso_driver_loaded(struct module *, int, void *); 458 static int uhso_radio_sysctl(SYSCTL_HANDLER_ARGS); 459 static int uhso_radio_ctrl(struct uhso_softc *, int); 460 461 static void uhso_free(struct ucom_softc *); 462 static void uhso_ucom_start_read(struct ucom_softc *); 463 static void uhso_ucom_stop_read(struct ucom_softc *); 464 static void uhso_ucom_start_write(struct ucom_softc *); 465 static void uhso_ucom_stop_write(struct ucom_softc *); 466 static void uhso_ucom_cfg_get_status(struct ucom_softc *, uint8_t *, uint8_t *); 467 static void uhso_ucom_cfg_set_dtr(struct ucom_softc *, uint8_t); 468 static void uhso_ucom_cfg_set_rts(struct ucom_softc *, uint8_t); 469 static void uhso_if_init(void *); 470 static void uhso_if_start(struct ifnet *); 471 static void uhso_if_stop(struct uhso_softc *); 472 static int uhso_if_ioctl(struct ifnet *, u_long, caddr_t); 473 static int uhso_if_output(struct ifnet *, struct mbuf *, 474 const struct sockaddr *, struct route *); 475 static void uhso_if_rxflush(void *); 476 477 static device_probe_t uhso_probe; 478 static device_attach_t uhso_attach; 479 static device_detach_t uhso_detach; 480 static void uhso_free_softc(struct uhso_softc *); 481 482 static device_method_t uhso_methods[] = { 483 DEVMETHOD(device_probe, uhso_probe), 484 DEVMETHOD(device_attach, uhso_attach), 485 DEVMETHOD(device_detach, uhso_detach), 486 { 0, 0 } 487 }; 488 489 static driver_t uhso_driver = { 490 .name = "uhso", 491 .methods = uhso_methods, 492 .size = sizeof(struct uhso_softc) 493 }; 494 495 static devclass_t uhso_devclass; 496 DRIVER_MODULE(uhso, uhub, uhso_driver, uhso_devclass, uhso_driver_loaded, 0); 497 MODULE_DEPEND(uhso, ucom, 1, 1, 1); 498 MODULE_DEPEND(uhso, usb, 1, 1, 1); 499 MODULE_VERSION(uhso, 1); 500 501 static struct ucom_callback uhso_ucom_callback = { 502 .ucom_cfg_get_status = &uhso_ucom_cfg_get_status, 503 .ucom_cfg_set_dtr = &uhso_ucom_cfg_set_dtr, 504 .ucom_cfg_set_rts = &uhso_ucom_cfg_set_rts, 505 .ucom_start_read = uhso_ucom_start_read, 506 .ucom_stop_read = uhso_ucom_stop_read, 507 .ucom_start_write = uhso_ucom_start_write, 508 .ucom_stop_write = uhso_ucom_stop_write, 509 .ucom_free = &uhso_free, 510 }; 511 512 static int 513 uhso_probe(device_t self) 514 { 515 struct usb_attach_arg *uaa = device_get_ivars(self); 516 int error; 517 518 if (uaa->usb_mode != USB_MODE_HOST) 519 return (ENXIO); 520 if (uaa->info.bConfigIndex != 0) 521 return (ENXIO); 522 if (uaa->info.bDeviceClass != 0xff) 523 return (ENXIO); 524 525 error = usbd_lookup_id_by_uaa(uhso_devs, sizeof(uhso_devs), uaa); 526 if (error != 0) 527 return (error); 528 529 /* 530 * Probe device to see if we are able to attach 531 * to this interface or not. 532 */ 533 if (USB_GET_DRIVER_INFO(uaa) == UHSO_AUTO_IFACE) { 534 if (uhso_probe_iface_auto(uaa->device, 535 uaa->info.bIfaceNum) == 0) 536 return (ENXIO); 537 } 538 return (error); 539 } 540 541 static int 542 uhso_attach(device_t self) 543 { 544 struct uhso_softc *sc = device_get_softc(self); 545 struct usb_attach_arg *uaa = device_get_ivars(self); 546 struct usb_interface_descriptor *id; 547 struct sysctl_ctx_list *sctx; 548 struct sysctl_oid *soid; 549 struct sysctl_oid *tree = NULL, *tty_node; 550 struct ucom_softc *ucom; 551 struct uhso_tty *ht; 552 int i, error, port; 553 void *probe_f; 554 usb_error_t uerr; 555 char *desc; 556 557 sc->sc_dev = self; 558 sc->sc_udev = uaa->device; 559 mtx_init(&sc->sc_mtx, "uhso", NULL, MTX_DEF); 560 ucom_ref(&sc->sc_super_ucom); 561 562 sc->sc_radio = 1; 563 564 id = usbd_get_interface_descriptor(uaa->iface); 565 sc->sc_ctrl_iface_no = id->bInterfaceNumber; 566 567 sc->sc_iface_no = uaa->info.bIfaceNum; 568 sc->sc_iface_index = uaa->info.bIfaceIndex; 569 570 /* Setup control pipe */ 571 uerr = usbd_transfer_setup(uaa->device, 572 &sc->sc_iface_index, sc->sc_ctrl_xfer, 573 uhso_ctrl_config, UHSO_CTRL_MAX, sc, &sc->sc_mtx); 574 if (uerr) { 575 device_printf(self, "Failed to setup control pipe: %s\n", 576 usbd_errstr(uerr)); 577 goto out; 578 } 579 580 if (USB_GET_DRIVER_INFO(uaa) == UHSO_STATIC_IFACE) 581 probe_f = uhso_probe_iface_static; 582 else if (USB_GET_DRIVER_INFO(uaa) == UHSO_AUTO_IFACE) 583 probe_f = uhso_probe_iface_auto; 584 else 585 goto out; 586 587 error = uhso_probe_iface(sc, uaa->info.bIfaceNum, probe_f); 588 if (error != 0) 589 goto out; 590 591 sctx = device_get_sysctl_ctx(sc->sc_dev); 592 soid = device_get_sysctl_tree(sc->sc_dev); 593 594 SYSCTL_ADD_STRING(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "type", 595 CTLFLAG_RD, uhso_port[UHSO_IFACE_PORT(sc->sc_type)], 0, 596 "Port available at this interface"); 597 SYSCTL_ADD_PROC(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "radio", 598 CTLTYPE_INT | CTLFLAG_RW, sc, 0, uhso_radio_sysctl, "I", "Enable radio"); 599 600 /* 601 * The default interface description on most Option devices isn't 602 * very helpful. So we skip device_set_usb_desc and set the 603 * device description manually. 604 */ 605 device_set_desc_copy(self, uhso_port_type[UHSO_IFACE_PORT_TYPE(sc->sc_type)]); 606 /* Announce device */ 607 device_printf(self, "<%s port> at <%s %s> on %s\n", 608 uhso_port_type[UHSO_IFACE_PORT_TYPE(sc->sc_type)], 609 usb_get_manufacturer(uaa->device), 610 usb_get_product(uaa->device), 611 device_get_nameunit(device_get_parent(self))); 612 613 if (sc->sc_ttys > 0) { 614 SYSCTL_ADD_INT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "ports", 615 CTLFLAG_RD, &sc->sc_ttys, 0, "Number of attached serial ports"); 616 617 tree = SYSCTL_ADD_NODE(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, 618 "port", CTLFLAG_RD, NULL, "Serial ports"); 619 } 620 621 /* 622 * Loop through the number of found TTYs and create sysctl 623 * nodes for them. 624 */ 625 for (i = 0; i < sc->sc_ttys; i++) { 626 ht = &sc->sc_tty[i]; 627 ucom = &sc->sc_ucom[i]; 628 629 if (UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_MUX) 630 port = uhso_mux_port_map[ht->ht_muxport]; 631 else 632 port = UHSO_IFACE_PORT_TYPE(sc->sc_type); 633 634 desc = uhso_port_type_sysctl[port]; 635 636 tty_node = SYSCTL_ADD_NODE(sctx, SYSCTL_CHILDREN(tree), OID_AUTO, 637 desc, CTLFLAG_RD, NULL, ""); 638 639 ht->ht_name[0] = 0; 640 if (sc->sc_ttys == 1) 641 snprintf(ht->ht_name, 32, "cuaU%d", ucom->sc_super->sc_unit); 642 else { 643 snprintf(ht->ht_name, 32, "cuaU%d.%d", 644 ucom->sc_super->sc_unit, ucom->sc_subunit); 645 } 646 647 desc = uhso_port_type[port]; 648 SYSCTL_ADD_STRING(sctx, SYSCTL_CHILDREN(tty_node), OID_AUTO, 649 "tty", CTLFLAG_RD, ht->ht_name, 0, ""); 650 SYSCTL_ADD_STRING(sctx, SYSCTL_CHILDREN(tty_node), OID_AUTO, 651 "desc", CTLFLAG_RD, desc, 0, ""); 652 653 if (bootverbose) 654 device_printf(sc->sc_dev, 655 "\"%s\" port at %s\n", desc, ht->ht_name); 656 } 657 658 return (0); 659 out: 660 uhso_detach(sc->sc_dev); 661 return (ENXIO); 662 } 663 664 static int 665 uhso_detach(device_t self) 666 { 667 struct uhso_softc *sc = device_get_softc(self); 668 int i; 669 670 usbd_transfer_unsetup(sc->sc_xfer, 3); 671 usbd_transfer_unsetup(sc->sc_ctrl_xfer, UHSO_CTRL_MAX); 672 if (sc->sc_ttys > 0) { 673 ucom_detach(&sc->sc_super_ucom, sc->sc_ucom); 674 675 for (i = 0; i < sc->sc_ttys; i++) { 676 if (sc->sc_tty[i].ht_muxport != -1) { 677 usbd_transfer_unsetup(sc->sc_tty[i].ht_xfer, 678 UHSO_CTRL_MAX); 679 } 680 } 681 } 682 683 if (sc->sc_ifp != NULL) { 684 callout_drain(&sc->sc_c); 685 free_unr(uhso_ifnet_unit, sc->sc_ifp->if_dunit); 686 mtx_lock(&sc->sc_mtx); 687 uhso_if_stop(sc); 688 bpfdetach(sc->sc_ifp); 689 if_detach(sc->sc_ifp); 690 if_free(sc->sc_ifp); 691 mtx_unlock(&sc->sc_mtx); 692 usbd_transfer_unsetup(sc->sc_if_xfer, UHSO_IFNET_MAX); 693 } 694 695 device_claim_softc(self); 696 697 uhso_free_softc(sc); 698 699 return (0); 700 } 701 702 UCOM_UNLOAD_DRAIN(uhso); 703 704 static void 705 uhso_free_softc(struct uhso_softc *sc) 706 { 707 if (ucom_unref(&sc->sc_super_ucom)) { 708 free(sc->sc_tty, M_USBDEV); 709 free(sc->sc_ucom, M_USBDEV); 710 mtx_destroy(&sc->sc_mtx); 711 device_free_softc(sc); 712 } 713 } 714 715 static void 716 uhso_free(struct ucom_softc *ucom) 717 { 718 uhso_free_softc(ucom->sc_parent); 719 } 720 721 static void 722 uhso_test_autoinst(void *arg, struct usb_device *udev, 723 struct usb_attach_arg *uaa) 724 { 725 struct usb_interface *iface; 726 struct usb_interface_descriptor *id; 727 728 if (uaa->dev_state != UAA_DEV_READY || !uhso_autoswitch) 729 return; 730 731 iface = usbd_get_iface(udev, 0); 732 if (iface == NULL) 733 return; 734 id = iface->idesc; 735 if (id == NULL || id->bInterfaceClass != UICLASS_MASS) 736 return; 737 if (usbd_lookup_id_by_uaa(uhso_devs, sizeof(uhso_devs), uaa)) 738 return; /* no device match */ 739 740 if (usb_msc_eject(udev, 0, MSC_EJECT_REZERO) == 0) { 741 /* success, mark the udev as disappearing */ 742 uaa->dev_state = UAA_DEV_EJECTING; 743 } 744 } 745 746 static int 747 uhso_driver_loaded(struct module *mod, int what, void *arg) 748 { 749 switch (what) { 750 case MOD_LOAD: 751 /* register our autoinstall handler */ 752 uhso_etag = EVENTHANDLER_REGISTER(usb_dev_configured, 753 uhso_test_autoinst, NULL, EVENTHANDLER_PRI_ANY); 754 /* create our unit allocator for inet devs */ 755 uhso_ifnet_unit = new_unrhdr(0, INT_MAX, NULL); 756 break; 757 case MOD_UNLOAD: 758 EVENTHANDLER_DEREGISTER(usb_dev_configured, uhso_etag); 759 delete_unrhdr(uhso_ifnet_unit); 760 break; 761 default: 762 return (EOPNOTSUPP); 763 } 764 return (0); 765 } 766 767 /* 768 * Probe the interface type by querying the device. The elements 769 * of an array indicates the capabilities of a particular interface. 770 * Returns a bit mask with the interface capabilities. 771 */ 772 static int 773 uhso_probe_iface_auto(struct usb_device *udev, int index) 774 { 775 struct usb_device_request req; 776 usb_error_t uerr; 777 uint16_t actlen = 0; 778 char port; 779 char buf[17] = {0}; 780 781 req.bmRequestType = UT_READ_VENDOR_DEVICE; 782 req.bRequest = 0x86; 783 USETW(req.wValue, 0); 784 USETW(req.wIndex, 0); 785 USETW(req.wLength, 17); 786 787 uerr = usbd_do_request_flags(udev, NULL, &req, buf, 788 0, &actlen, USB_MS_HZ); 789 if (uerr != 0) { 790 printf("%s: usbd_do_request_flags failed, %s\n", 791 __func__, usbd_errstr(uerr)); 792 return (0); 793 } 794 795 UHSO_DPRINTF(1, "actlen=%d\n", actlen); 796 UHSO_HEXDUMP(buf, 17); 797 798 if (index < 0 || index > 16) { 799 UHSO_DPRINTF(0, "Index %d out of range\n", index); 800 return (0); 801 } 802 803 UHSO_DPRINTF(1, "index=%d, type=%x[%s]\n", index, buf[index], 804 uhso_port_type[(int)uhso_port_map[(int)buf[index]]]); 805 806 if (buf[index] >= uhso_port_map_max) 807 port = 0; 808 else 809 port = uhso_port_map[(int)buf[index]]; 810 811 switch (port) { 812 case UHSO_PORT_TYPE_NETWORK: 813 return (UHSO_IFACE_SPEC(UHSO_IF_NET | UHSO_IF_MUX, 814 UHSO_PORT_SERIAL | UHSO_PORT_NETWORK, port)); 815 case UHSO_PORT_TYPE_DIAG: 816 case UHSO_PORT_TYPE_DIAG2: 817 case UHSO_PORT_TYPE_GPS: 818 case UHSO_PORT_TYPE_GPSCTL: 819 case UHSO_PORT_TYPE_CTL: 820 case UHSO_PORT_TYPE_APP: 821 case UHSO_PORT_TYPE_APP2: 822 case UHSO_PORT_TYPE_MODEM: 823 return (UHSO_IFACE_SPEC(UHSO_IF_BULK, 824 UHSO_PORT_SERIAL, port)); 825 case UHSO_PORT_TYPE_MSD: 826 return (0); 827 case UHSO_PORT_TYPE_UNKNOWN: 828 default: 829 return (0); 830 } 831 832 return (0); 833 } 834 835 /* 836 * Returns the capabilities of interfaces for devices that don't 837 * support the automatic query. 838 * Returns a bit mask with the interface capabilities. 839 */ 840 static int 841 uhso_probe_iface_static(struct usb_device *udev, int index) 842 { 843 struct usb_config_descriptor *cd; 844 845 cd = usbd_get_config_descriptor(udev); 846 if (cd->bNumInterface <= 3) { 847 /* Cards with 3 or less interfaces */ 848 switch (index) { 849 case 0: 850 return UHSO_IFACE_SPEC(UHSO_IF_NET | UHSO_IF_MUX, 851 UHSO_PORT_SERIAL | UHSO_PORT_NETWORK, 852 UHSO_PORT_TYPE_NETWORK); 853 case 1: 854 return UHSO_IFACE_SPEC(UHSO_IF_BULK, 855 UHSO_PORT_SERIAL, UHSO_PORT_TYPE_DIAG); 856 case 2: 857 return UHSO_IFACE_SPEC(UHSO_IF_BULK, 858 UHSO_PORT_SERIAL, UHSO_PORT_TYPE_MODEM); 859 } 860 } else { 861 /* Cards with 4 interfaces */ 862 switch (index) { 863 case 0: 864 return UHSO_IFACE_SPEC(UHSO_IF_NET | UHSO_IF_MUX, 865 UHSO_PORT_SERIAL | UHSO_PORT_NETWORK, 866 UHSO_PORT_TYPE_NETWORK); 867 case 1: 868 return UHSO_IFACE_SPEC(UHSO_IF_BULK, 869 UHSO_PORT_SERIAL, UHSO_PORT_TYPE_DIAG2); 870 case 2: 871 return UHSO_IFACE_SPEC(UHSO_IF_BULK, 872 UHSO_PORT_SERIAL, UHSO_PORT_TYPE_MODEM); 873 case 3: 874 return UHSO_IFACE_SPEC(UHSO_IF_BULK, 875 UHSO_PORT_SERIAL, UHSO_PORT_TYPE_DIAG); 876 } 877 } 878 return (0); 879 } 880 881 /* 882 * Probes an interface for its particular capabilities and attaches if 883 * it's a supported interface. 884 */ 885 static int 886 uhso_probe_iface(struct uhso_softc *sc, int index, 887 int (*probe)(struct usb_device *, int)) 888 { 889 struct usb_interface *iface; 890 int type, error; 891 892 UHSO_DPRINTF(1, "Probing for interface %d, probe_func=%p\n", index, probe); 893 894 type = probe(sc->sc_udev, index); 895 UHSO_DPRINTF(1, "Probe result %x\n", type); 896 if (type <= 0) 897 return (ENXIO); 898 899 sc->sc_type = type; 900 iface = usbd_get_iface(sc->sc_udev, index); 901 902 if (UHSO_IFACE_PORT_TYPE(type) == UHSO_PORT_TYPE_NETWORK) { 903 error = uhso_attach_ifnet(sc, iface, type); 904 if (error) { 905 UHSO_DPRINTF(1, "uhso_attach_ifnet failed"); 906 return (ENXIO); 907 } 908 909 /* 910 * If there is an additional interrupt endpoint on this 911 * interface then we most likely have a multiplexed serial port 912 * available. 913 */ 914 if (iface->idesc->bNumEndpoints < 3) { 915 sc->sc_type = UHSO_IFACE_SPEC( 916 UHSO_IFACE_USB_TYPE(type) & ~UHSO_IF_MUX, 917 UHSO_IFACE_PORT(type) & ~UHSO_PORT_SERIAL, 918 UHSO_IFACE_PORT_TYPE(type)); 919 return (0); 920 } 921 922 UHSO_DPRINTF(1, "Trying to attach mux. serial\n"); 923 error = uhso_attach_muxserial(sc, iface, type); 924 if (error == 0 && sc->sc_ttys > 0) { 925 error = ucom_attach(&sc->sc_super_ucom, sc->sc_ucom, 926 sc->sc_ttys, sc, &uhso_ucom_callback, &sc->sc_mtx); 927 if (error) { 928 device_printf(sc->sc_dev, "ucom_attach failed\n"); 929 return (ENXIO); 930 } 931 ucom_set_pnpinfo_usb(&sc->sc_super_ucom, sc->sc_dev); 932 933 mtx_lock(&sc->sc_mtx); 934 usbd_transfer_start(sc->sc_xfer[UHSO_MUX_ENDPT_INTR]); 935 mtx_unlock(&sc->sc_mtx); 936 } 937 } else if ((UHSO_IFACE_USB_TYPE(type) & UHSO_IF_BULK) && 938 UHSO_IFACE_PORT(type) & UHSO_PORT_SERIAL) { 939 940 error = uhso_attach_bulkserial(sc, iface, type); 941 if (error) 942 return (ENXIO); 943 944 error = ucom_attach(&sc->sc_super_ucom, sc->sc_ucom, 945 sc->sc_ttys, sc, &uhso_ucom_callback, &sc->sc_mtx); 946 if (error) { 947 device_printf(sc->sc_dev, "ucom_attach failed\n"); 948 return (ENXIO); 949 } 950 ucom_set_pnpinfo_usb(&sc->sc_super_ucom, sc->sc_dev); 951 } 952 else { 953 UHSO_DPRINTF(0, "Unknown type %x\n", type); 954 return (ENXIO); 955 } 956 957 return (0); 958 } 959 960 static int 961 uhso_radio_ctrl(struct uhso_softc *sc, int onoff) 962 { 963 struct usb_device_request req; 964 usb_error_t uerr; 965 966 req.bmRequestType = UT_VENDOR; 967 req.bRequest = onoff ? 0x82 : 0x81; 968 USETW(req.wValue, 0); 969 USETW(req.wIndex, 0); 970 USETW(req.wLength, 0); 971 972 uerr = usbd_do_request(sc->sc_udev, NULL, &req, NULL); 973 if (uerr != 0) { 974 device_printf(sc->sc_dev, "usbd_do_request_flags failed: %s\n", 975 usbd_errstr(uerr)); 976 return (-1); 977 } 978 return (onoff); 979 } 980 981 static int 982 uhso_radio_sysctl(SYSCTL_HANDLER_ARGS) 983 { 984 struct uhso_softc *sc = arg1; 985 int error, radio; 986 987 radio = sc->sc_radio; 988 error = sysctl_handle_int(oidp, &radio, 0, req); 989 if (error) 990 return (error); 991 if (radio != sc->sc_radio) { 992 radio = radio != 0 ? 1 : 0; 993 error = uhso_radio_ctrl(sc, radio); 994 if (error != -1) 995 sc->sc_radio = radio; 996 997 } 998 return (0); 999 } 1000 1001 /* 1002 * Expands allocated memory to fit an additional TTY. 1003 * Two arrays are kept with matching indexes, one for ucom and one 1004 * for our private data. 1005 */ 1006 static int 1007 uhso_alloc_tty(struct uhso_softc *sc) 1008 { 1009 1010 sc->sc_ttys++; 1011 sc->sc_tty = reallocf(sc->sc_tty, sizeof(struct uhso_tty) * sc->sc_ttys, 1012 M_USBDEV, M_WAITOK | M_ZERO); 1013 if (sc->sc_tty == NULL) 1014 return (-1); 1015 1016 sc->sc_ucom = reallocf(sc->sc_ucom, 1017 sizeof(struct ucom_softc) * sc->sc_ttys, M_USBDEV, M_WAITOK | M_ZERO); 1018 if (sc->sc_ucom == NULL) 1019 return (-1); 1020 1021 sc->sc_tty[sc->sc_ttys - 1].ht_sc = sc; 1022 1023 UHSO_DPRINTF(1, "Allocated TTY %d\n", sc->sc_ttys - 1); 1024 return (sc->sc_ttys - 1); 1025 } 1026 1027 /* 1028 * Attach a multiplexed serial port 1029 * Data is read/written with requests on the default control pipe. An interrupt 1030 * endpoint returns when there is new data to be read. 1031 */ 1032 static int 1033 uhso_attach_muxserial(struct uhso_softc *sc, struct usb_interface *iface, 1034 int type) 1035 { 1036 struct usb_descriptor *desc; 1037 int i, port, tty; 1038 usb_error_t uerr; 1039 1040 /* 1041 * The class specific interface (type 0x24) descriptor subtype field 1042 * contains a bitmask that specifies which (and how many) ports that 1043 * are available through this multiplexed serial port. 1044 */ 1045 desc = usbd_find_descriptor(sc->sc_udev, NULL, 1046 iface->idesc->bInterfaceNumber, UDESC_CS_INTERFACE, 0xff, 0, 0); 1047 if (desc == NULL) { 1048 UHSO_DPRINTF(0, "Failed to find UDESC_CS_INTERFACE\n"); 1049 return (ENXIO); 1050 } 1051 1052 UHSO_DPRINTF(1, "Mux port mask %x\n", desc->bDescriptorSubtype); 1053 if (desc->bDescriptorSubtype == 0) 1054 return (ENXIO); 1055 1056 /* 1057 * The bitmask is one octet, loop through the number of 1058 * bits that are set and create a TTY for each. 1059 */ 1060 for (i = 0; i < 8; i++) { 1061 port = (1 << i); 1062 if ((port & desc->bDescriptorSubtype) == port) { 1063 UHSO_DPRINTF(2, "Found mux port %x (%d)\n", port, i); 1064 tty = uhso_alloc_tty(sc); 1065 if (tty < 0) 1066 return (ENOMEM); 1067 sc->sc_tty[tty].ht_muxport = i; 1068 uerr = usbd_transfer_setup(sc->sc_udev, 1069 &sc->sc_iface_index, sc->sc_tty[tty].ht_xfer, 1070 uhso_ctrl_config, UHSO_CTRL_MAX, sc, &sc->sc_mtx); 1071 if (uerr) { 1072 device_printf(sc->sc_dev, 1073 "Failed to setup control pipe: %s\n", 1074 usbd_errstr(uerr)); 1075 return (ENXIO); 1076 } 1077 } 1078 } 1079 1080 /* Setup the intr. endpoint */ 1081 uerr = usbd_transfer_setup(sc->sc_udev, 1082 &iface->idesc->bInterfaceNumber, sc->sc_xfer, 1083 uhso_mux_config, 1, sc, &sc->sc_mtx); 1084 if (uerr) 1085 return (ENXIO); 1086 1087 return (0); 1088 } 1089 1090 /* 1091 * Interrupt callback for the multiplexed serial port. Indicates 1092 * which serial port has data waiting. 1093 */ 1094 static void 1095 uhso_mux_intr_callback(struct usb_xfer *xfer, usb_error_t error) 1096 { 1097 struct usb_page_cache *pc; 1098 struct usb_page_search res; 1099 struct uhso_softc *sc = usbd_xfer_softc(xfer); 1100 unsigned int i, mux; 1101 1102 UHSO_DPRINTF(3, "status %d\n", USB_GET_STATE(xfer)); 1103 1104 switch (USB_GET_STATE(xfer)) { 1105 case USB_ST_TRANSFERRED: 1106 /* 1107 * The multiplexed port number can be found at the first byte. 1108 * It contains a bit mask, we transform this in to an integer. 1109 */ 1110 pc = usbd_xfer_get_frame(xfer, 0); 1111 usbd_get_page(pc, 0, &res); 1112 1113 i = *((unsigned char *)res.buffer); 1114 mux = 0; 1115 while (i >>= 1) { 1116 mux++; 1117 } 1118 1119 UHSO_DPRINTF(3, "mux port %d (%d)\n", mux, i); 1120 if (mux > UHSO_MPORT_TYPE_NOMAX) 1121 break; 1122 1123 /* Issue a read for this serial port */ 1124 usbd_xfer_set_priv( 1125 sc->sc_tty[mux].ht_xfer[UHSO_CTRL_READ], 1126 &sc->sc_tty[mux]); 1127 usbd_transfer_start(sc->sc_tty[mux].ht_xfer[UHSO_CTRL_READ]); 1128 1129 break; 1130 case USB_ST_SETUP: 1131 tr_setup: 1132 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer)); 1133 usbd_transfer_submit(xfer); 1134 break; 1135 default: 1136 UHSO_DPRINTF(0, "error: %s\n", usbd_errstr(error)); 1137 if (error == USB_ERR_CANCELLED) 1138 break; 1139 1140 usbd_xfer_set_stall(xfer); 1141 goto tr_setup; 1142 } 1143 } 1144 1145 static void 1146 uhso_mux_read_callback(struct usb_xfer *xfer, usb_error_t error) 1147 { 1148 struct uhso_softc *sc = usbd_xfer_softc(xfer); 1149 struct usb_page_cache *pc; 1150 struct usb_device_request req; 1151 struct uhso_tty *ht; 1152 int actlen, len; 1153 1154 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL); 1155 1156 UHSO_DPRINTF(3, "status %d\n", USB_GET_STATE(xfer)); 1157 1158 ht = usbd_xfer_get_priv(xfer); 1159 UHSO_DPRINTF(3, "ht=%p open=%d\n", ht, ht->ht_open); 1160 1161 switch (USB_GET_STATE(xfer)) { 1162 case USB_ST_TRANSFERRED: 1163 /* Got data, send to ucom */ 1164 pc = usbd_xfer_get_frame(xfer, 1); 1165 len = usbd_xfer_frame_len(xfer, 1); 1166 1167 UHSO_DPRINTF(3, "got %d bytes on mux port %d\n", len, 1168 ht->ht_muxport); 1169 if (len <= 0) { 1170 usbd_transfer_start(sc->sc_xfer[UHSO_MUX_ENDPT_INTR]); 1171 break; 1172 } 1173 1174 /* Deliver data if the TTY is open, discard otherwise */ 1175 if (ht->ht_open) 1176 ucom_put_data(&sc->sc_ucom[ht->ht_muxport], pc, 0, len); 1177 /* FALLTHROUGH */ 1178 case USB_ST_SETUP: 1179 tr_setup: 1180 memset(&req, 0, sizeof(struct usb_device_request)); 1181 req.bmRequestType = UT_READ_CLASS_INTERFACE; 1182 req.bRequest = UCDC_GET_ENCAPSULATED_RESPONSE; 1183 USETW(req.wValue, 0); 1184 USETW(req.wIndex, ht->ht_muxport); 1185 USETW(req.wLength, 1024); 1186 1187 pc = usbd_xfer_get_frame(xfer, 0); 1188 usbd_copy_in(pc, 0, &req, sizeof(req)); 1189 1190 usbd_xfer_set_frame_len(xfer, 0, sizeof(req)); 1191 usbd_xfer_set_frame_len(xfer, 1, 1024); 1192 usbd_xfer_set_frames(xfer, 2); 1193 usbd_transfer_submit(xfer); 1194 break; 1195 default: 1196 UHSO_DPRINTF(0, "error: %s\n", usbd_errstr(error)); 1197 if (error == USB_ERR_CANCELLED) 1198 break; 1199 usbd_xfer_set_stall(xfer); 1200 goto tr_setup; 1201 } 1202 } 1203 1204 static void 1205 uhso_mux_write_callback(struct usb_xfer *xfer, usb_error_t error) 1206 { 1207 struct uhso_softc *sc = usbd_xfer_softc(xfer); 1208 struct uhso_tty *ht; 1209 struct usb_page_cache *pc; 1210 struct usb_device_request req; 1211 int actlen; 1212 struct usb_page_search res; 1213 1214 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL); 1215 1216 ht = usbd_xfer_get_priv(xfer); 1217 UHSO_DPRINTF(3, "status=%d, using mux port %d\n", 1218 USB_GET_STATE(xfer), ht->ht_muxport); 1219 1220 switch (USB_GET_STATE(xfer)) { 1221 case USB_ST_TRANSFERRED: 1222 UHSO_DPRINTF(3, "wrote %zd data bytes to muxport %d\n", 1223 actlen - sizeof(struct usb_device_request) , 1224 ht->ht_muxport); 1225 /* FALLTHROUGH */ 1226 case USB_ST_SETUP: 1227 pc = usbd_xfer_get_frame(xfer, 1); 1228 if (ucom_get_data(&sc->sc_ucom[ht->ht_muxport], pc, 1229 0, 32, &actlen)) { 1230 1231 usbd_get_page(pc, 0, &res); 1232 1233 memset(&req, 0, sizeof(struct usb_device_request)); 1234 req.bmRequestType = UT_WRITE_CLASS_INTERFACE; 1235 req.bRequest = UCDC_SEND_ENCAPSULATED_COMMAND; 1236 USETW(req.wValue, 0); 1237 USETW(req.wIndex, ht->ht_muxport); 1238 USETW(req.wLength, actlen); 1239 1240 pc = usbd_xfer_get_frame(xfer, 0); 1241 usbd_copy_in(pc, 0, &req, sizeof(req)); 1242 1243 usbd_xfer_set_frame_len(xfer, 0, sizeof(req)); 1244 usbd_xfer_set_frame_len(xfer, 1, actlen); 1245 usbd_xfer_set_frames(xfer, 2); 1246 1247 UHSO_DPRINTF(3, "Prepared %d bytes for transmit " 1248 "on muxport %d\n", actlen, ht->ht_muxport); 1249 1250 usbd_transfer_submit(xfer); 1251 } 1252 break; 1253 default: 1254 UHSO_DPRINTF(0, "error: %s\n", usbd_errstr(error)); 1255 if (error == USB_ERR_CANCELLED) 1256 break; 1257 break; 1258 } 1259 } 1260 1261 static int 1262 uhso_attach_bulkserial(struct uhso_softc *sc, struct usb_interface *iface, 1263 int type) 1264 { 1265 usb_error_t uerr; 1266 int tty; 1267 1268 /* Try attaching RD/WR/INTR first */ 1269 uerr = usbd_transfer_setup(sc->sc_udev, 1270 &iface->idesc->bInterfaceNumber, sc->sc_xfer, 1271 uhso_bs_config, UHSO_BULK_ENDPT_MAX, sc, &sc->sc_mtx); 1272 if (uerr) { 1273 /* Try only RD/WR */ 1274 uerr = usbd_transfer_setup(sc->sc_udev, 1275 &iface->idesc->bInterfaceNumber, sc->sc_xfer, 1276 uhso_bs_config, UHSO_BULK_ENDPT_MAX - 1, sc, &sc->sc_mtx); 1277 } 1278 if (uerr) { 1279 UHSO_DPRINTF(0, "usbd_transfer_setup failed"); 1280 return (-1); 1281 } 1282 1283 tty = uhso_alloc_tty(sc); 1284 if (tty < 0) { 1285 usbd_transfer_unsetup(sc->sc_xfer, UHSO_BULK_ENDPT_MAX); 1286 return (ENOMEM); 1287 } 1288 1289 sc->sc_tty[tty].ht_muxport = -1; 1290 return (0); 1291 } 1292 1293 static void 1294 uhso_bs_read_callback(struct usb_xfer *xfer, usb_error_t error) 1295 { 1296 struct uhso_softc *sc = usbd_xfer_softc(xfer); 1297 struct usb_page_cache *pc; 1298 int actlen; 1299 1300 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL); 1301 1302 UHSO_DPRINTF(3, "status %d, actlen=%d\n", USB_GET_STATE(xfer), actlen); 1303 1304 switch (USB_GET_STATE(xfer)) { 1305 case USB_ST_TRANSFERRED: 1306 pc = usbd_xfer_get_frame(xfer, 0); 1307 ucom_put_data(&sc->sc_ucom[0], pc, 0, actlen); 1308 /* FALLTHROUGH */ 1309 case USB_ST_SETUP: 1310 tr_setup: 1311 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer)); 1312 usbd_transfer_submit(xfer); 1313 break; 1314 default: 1315 UHSO_DPRINTF(0, "error: %s\n", usbd_errstr(error)); 1316 if (error == USB_ERR_CANCELLED) 1317 break; 1318 usbd_xfer_set_stall(xfer); 1319 goto tr_setup; 1320 } 1321 } 1322 1323 static void 1324 uhso_bs_write_callback(struct usb_xfer *xfer, usb_error_t error) 1325 { 1326 struct uhso_softc *sc = usbd_xfer_softc(xfer); 1327 struct usb_page_cache *pc; 1328 int actlen; 1329 1330 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL); 1331 1332 UHSO_DPRINTF(3, "status %d, actlen=%d\n", USB_GET_STATE(xfer), actlen); 1333 1334 switch (USB_GET_STATE(xfer)) { 1335 case USB_ST_TRANSFERRED: 1336 case USB_ST_SETUP: 1337 tr_setup: 1338 pc = usbd_xfer_get_frame(xfer, 0); 1339 if (ucom_get_data(&sc->sc_ucom[0], pc, 0, 8192, &actlen)) { 1340 usbd_xfer_set_frame_len(xfer, 0, actlen); 1341 usbd_transfer_submit(xfer); 1342 } 1343 break; 1344 break; 1345 default: 1346 UHSO_DPRINTF(0, "error: %s\n", usbd_errstr(error)); 1347 if (error == USB_ERR_CANCELLED) 1348 break; 1349 usbd_xfer_set_stall(xfer); 1350 goto tr_setup; 1351 } 1352 } 1353 1354 static void 1355 uhso_bs_cfg(struct uhso_softc *sc) 1356 { 1357 struct usb_device_request req; 1358 usb_error_t uerr; 1359 1360 if (!(UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_BULK)) 1361 return; 1362 1363 req.bmRequestType = UT_WRITE_CLASS_INTERFACE; 1364 req.bRequest = UCDC_SET_CONTROL_LINE_STATE; 1365 USETW(req.wValue, sc->sc_line); 1366 USETW(req.wIndex, sc->sc_iface_no); 1367 USETW(req.wLength, 0); 1368 1369 uerr = ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom[0], &req, NULL, 0, 1000); 1370 if (uerr != 0) { 1371 device_printf(sc->sc_dev, "failed to set ctrl line state to " 1372 "0x%02x: %s\n", sc->sc_line, usbd_errstr(uerr)); 1373 } 1374 } 1375 1376 static void 1377 uhso_bs_intr_callback(struct usb_xfer *xfer, usb_error_t error) 1378 { 1379 struct uhso_softc *sc = usbd_xfer_softc(xfer); 1380 struct usb_page_cache *pc; 1381 int actlen; 1382 struct usb_cdc_notification cdc; 1383 1384 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL); 1385 UHSO_DPRINTF(3, "status %d, actlen=%d\n", USB_GET_STATE(xfer), actlen); 1386 1387 switch (USB_GET_STATE(xfer)) { 1388 case USB_ST_TRANSFERRED: 1389 if (actlen < UCDC_NOTIFICATION_LENGTH) { 1390 UHSO_DPRINTF(0, "UCDC notification too short: %d\n", actlen); 1391 goto tr_setup; 1392 } 1393 else if (actlen > (int)sizeof(struct usb_cdc_notification)) { 1394 UHSO_DPRINTF(0, "UCDC notification too large: %d\n", actlen); 1395 actlen = sizeof(struct usb_cdc_notification); 1396 } 1397 1398 pc = usbd_xfer_get_frame(xfer, 0); 1399 usbd_copy_out(pc, 0, &cdc, actlen); 1400 1401 if (UGETW(cdc.wIndex) != sc->sc_iface_no) { 1402 UHSO_DPRINTF(0, "Interface mismatch, got %d expected %d\n", 1403 UGETW(cdc.wIndex), sc->sc_iface_no); 1404 goto tr_setup; 1405 } 1406 1407 if (cdc.bmRequestType == UCDC_NOTIFICATION && 1408 cdc.bNotification == UCDC_N_SERIAL_STATE) { 1409 UHSO_DPRINTF(2, "notify = 0x%02x\n", cdc.data[0]); 1410 1411 sc->sc_msr = 0; 1412 sc->sc_lsr = 0; 1413 if (cdc.data[0] & UCDC_N_SERIAL_RI) 1414 sc->sc_msr |= SER_RI; 1415 if (cdc.data[0] & UCDC_N_SERIAL_DSR) 1416 sc->sc_msr |= SER_DSR; 1417 if (cdc.data[0] & UCDC_N_SERIAL_DCD) 1418 sc->sc_msr |= SER_DCD; 1419 1420 ucom_status_change(&sc->sc_ucom[0]); 1421 } 1422 case USB_ST_SETUP: 1423 tr_setup: 1424 default: 1425 if (error == USB_ERR_CANCELLED) 1426 break; 1427 usbd_xfer_set_stall(xfer); 1428 goto tr_setup; 1429 } 1430 } 1431 1432 static void 1433 uhso_ucom_cfg_get_status(struct ucom_softc *ucom, uint8_t *lsr, uint8_t *msr) 1434 { 1435 struct uhso_softc *sc = ucom->sc_parent; 1436 1437 *lsr = sc->sc_lsr; 1438 *msr = sc->sc_msr; 1439 } 1440 1441 static void 1442 uhso_ucom_cfg_set_dtr(struct ucom_softc *ucom, uint8_t onoff) 1443 { 1444 struct uhso_softc *sc = ucom->sc_parent; 1445 1446 if (!(UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_BULK)) 1447 return; 1448 1449 if (onoff) 1450 sc->sc_line |= UCDC_LINE_DTR; 1451 else 1452 sc->sc_line &= ~UCDC_LINE_DTR; 1453 1454 uhso_bs_cfg(sc); 1455 } 1456 1457 static void 1458 uhso_ucom_cfg_set_rts(struct ucom_softc *ucom, uint8_t onoff) 1459 { 1460 struct uhso_softc *sc = ucom->sc_parent; 1461 1462 if (!(UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_BULK)) 1463 return; 1464 1465 if (onoff) 1466 sc->sc_line |= UCDC_LINE_RTS; 1467 else 1468 sc->sc_line &= ~UCDC_LINE_RTS; 1469 1470 uhso_bs_cfg(sc); 1471 } 1472 1473 static void 1474 uhso_ucom_start_read(struct ucom_softc *ucom) 1475 { 1476 struct uhso_softc *sc = ucom->sc_parent; 1477 1478 UHSO_DPRINTF(3, "unit=%d, subunit=%d\n", 1479 ucom->sc_super->sc_unit, ucom->sc_subunit); 1480 1481 if (UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_MUX) { 1482 sc->sc_tty[ucom->sc_subunit].ht_open = 1; 1483 usbd_transfer_start(sc->sc_xfer[UHSO_MUX_ENDPT_INTR]); 1484 } 1485 else if (UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_BULK) { 1486 sc->sc_tty[0].ht_open = 1; 1487 usbd_transfer_start(sc->sc_xfer[UHSO_BULK_ENDPT_READ]); 1488 if (sc->sc_xfer[UHSO_BULK_ENDPT_INTR] != NULL) 1489 usbd_transfer_start(sc->sc_xfer[UHSO_BULK_ENDPT_INTR]); 1490 } 1491 } 1492 1493 static void 1494 uhso_ucom_stop_read(struct ucom_softc *ucom) 1495 { 1496 1497 struct uhso_softc *sc = ucom->sc_parent; 1498 1499 if (UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_MUX) { 1500 sc->sc_tty[ucom->sc_subunit].ht_open = 0; 1501 usbd_transfer_stop( 1502 sc->sc_tty[ucom->sc_subunit].ht_xfer[UHSO_CTRL_READ]); 1503 } 1504 else if (UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_BULK) { 1505 sc->sc_tty[0].ht_open = 0; 1506 usbd_transfer_start(sc->sc_xfer[UHSO_BULK_ENDPT_READ]); 1507 if (sc->sc_xfer[UHSO_BULK_ENDPT_INTR] != NULL) 1508 usbd_transfer_stop(sc->sc_xfer[UHSO_BULK_ENDPT_INTR]); 1509 } 1510 } 1511 1512 static void 1513 uhso_ucom_start_write(struct ucom_softc *ucom) 1514 { 1515 struct uhso_softc *sc = ucom->sc_parent; 1516 1517 if (UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_MUX) { 1518 UHSO_DPRINTF(3, "local unit %d\n", ucom->sc_subunit); 1519 1520 usbd_transfer_start(sc->sc_xfer[UHSO_MUX_ENDPT_INTR]); 1521 1522 usbd_xfer_set_priv( 1523 sc->sc_tty[ucom->sc_subunit].ht_xfer[UHSO_CTRL_WRITE], 1524 &sc->sc_tty[ucom->sc_subunit]); 1525 usbd_transfer_start( 1526 sc->sc_tty[ucom->sc_subunit].ht_xfer[UHSO_CTRL_WRITE]); 1527 1528 } 1529 else if (UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_BULK) { 1530 usbd_transfer_start(sc->sc_xfer[UHSO_BULK_ENDPT_WRITE]); 1531 } 1532 } 1533 1534 static void 1535 uhso_ucom_stop_write(struct ucom_softc *ucom) 1536 { 1537 struct uhso_softc *sc = ucom->sc_parent; 1538 1539 if (UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_MUX) { 1540 usbd_transfer_stop( 1541 sc->sc_tty[ucom->sc_subunit].ht_xfer[UHSO_CTRL_WRITE]); 1542 } 1543 else if (UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_BULK) { 1544 usbd_transfer_stop(sc->sc_xfer[UHSO_BULK_ENDPT_WRITE]); 1545 } 1546 } 1547 1548 static int 1549 uhso_attach_ifnet(struct uhso_softc *sc, struct usb_interface *iface, int type) 1550 { 1551 struct ifnet *ifp; 1552 usb_error_t uerr; 1553 struct sysctl_ctx_list *sctx; 1554 struct sysctl_oid *soid; 1555 unsigned int devunit; 1556 1557 uerr = usbd_transfer_setup(sc->sc_udev, 1558 &iface->idesc->bInterfaceNumber, sc->sc_if_xfer, 1559 uhso_ifnet_config, UHSO_IFNET_MAX, sc, &sc->sc_mtx); 1560 if (uerr) { 1561 UHSO_DPRINTF(0, "usbd_transfer_setup failed: %s\n", 1562 usbd_errstr(uerr)); 1563 return (-1); 1564 } 1565 1566 sc->sc_ifp = ifp = if_alloc(IFT_OTHER); 1567 if (sc->sc_ifp == NULL) { 1568 device_printf(sc->sc_dev, "if_alloc() failed\n"); 1569 return (-1); 1570 } 1571 1572 callout_init_mtx(&sc->sc_c, &sc->sc_mtx, 0); 1573 mtx_lock(&sc->sc_mtx); 1574 callout_reset(&sc->sc_c, 1, uhso_if_rxflush, sc); 1575 mtx_unlock(&sc->sc_mtx); 1576 1577 /* 1578 * We create our own unit numbers for ifnet devices because the 1579 * USB interface unit numbers can be at arbitrary positions yielding 1580 * odd looking device names. 1581 */ 1582 devunit = alloc_unr(uhso_ifnet_unit); 1583 1584 if_initname(ifp, device_get_name(sc->sc_dev), devunit); 1585 ifp->if_mtu = UHSO_MAX_MTU; 1586 ifp->if_ioctl = uhso_if_ioctl; 1587 ifp->if_init = uhso_if_init; 1588 ifp->if_start = uhso_if_start; 1589 ifp->if_output = uhso_if_output; 1590 ifp->if_flags = IFF_BROADCAST | IFF_MULTICAST | IFF_NOARP; 1591 ifp->if_softc = sc; 1592 IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen); 1593 ifp->if_snd.ifq_drv_maxlen = ifqmaxlen; 1594 IFQ_SET_READY(&ifp->if_snd); 1595 1596 if_attach(ifp); 1597 bpfattach(ifp, DLT_RAW, 0); 1598 1599 sctx = device_get_sysctl_ctx(sc->sc_dev); 1600 soid = device_get_sysctl_tree(sc->sc_dev); 1601 /* Unlocked read... */ 1602 SYSCTL_ADD_STRING(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "netif", 1603 CTLFLAG_RD, ifp->if_xname, 0, "Attached network interface"); 1604 1605 return (0); 1606 } 1607 1608 static void 1609 uhso_ifnet_read_callback(struct usb_xfer *xfer, usb_error_t error) 1610 { 1611 struct uhso_softc *sc = usbd_xfer_softc(xfer); 1612 struct mbuf *m; 1613 struct usb_page_cache *pc; 1614 int actlen; 1615 1616 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL); 1617 1618 UHSO_DPRINTF(3, "status=%d, actlen=%d\n", USB_GET_STATE(xfer), actlen); 1619 1620 switch (USB_GET_STATE(xfer)) { 1621 case USB_ST_TRANSFERRED: 1622 if (actlen > 0 && (sc->sc_ifp->if_drv_flags & IFF_DRV_RUNNING)) { 1623 pc = usbd_xfer_get_frame(xfer, 0); 1624 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR); 1625 usbd_copy_out(pc, 0, mtod(m, uint8_t *), actlen); 1626 m->m_pkthdr.len = m->m_len = actlen; 1627 /* Enqueue frame for further processing */ 1628 _IF_ENQUEUE(&sc->sc_rxq, m); 1629 if (!callout_pending(&sc->sc_c) || 1630 !callout_active(&sc->sc_c)) { 1631 callout_schedule(&sc->sc_c, 1); 1632 } 1633 } 1634 /* FALLTHROUGH */ 1635 case USB_ST_SETUP: 1636 tr_setup: 1637 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer)); 1638 usbd_transfer_submit(xfer); 1639 break; 1640 default: 1641 UHSO_DPRINTF(0, "error: %s\n", usbd_errstr(error)); 1642 if (error == USB_ERR_CANCELLED) 1643 break; 1644 usbd_xfer_set_stall(xfer); 1645 goto tr_setup; 1646 } 1647 } 1648 1649 /* 1650 * Deferred RX processing, called with mutex locked. 1651 * 1652 * Each frame we receive might contain several small ip-packets as well 1653 * as partial ip-packets. We need to separate/assemble them into individual 1654 * packets before sending them to the ip-layer. 1655 */ 1656 static void 1657 uhso_if_rxflush(void *arg) 1658 { 1659 struct uhso_softc *sc = arg; 1660 struct ifnet *ifp = sc->sc_ifp; 1661 uint8_t *cp; 1662 struct mbuf *m, *m0, *mwait; 1663 struct ip *ip; 1664 #ifdef INET6 1665 struct ip6_hdr *ip6; 1666 #endif 1667 uint16_t iplen; 1668 int len, isr; 1669 1670 m = NULL; 1671 mwait = sc->sc_mwait; 1672 for (;;) { 1673 if (m == NULL) { 1674 _IF_DEQUEUE(&sc->sc_rxq, m); 1675 if (m == NULL) 1676 break; 1677 UHSO_DPRINTF(3, "dequeue m=%p, len=%d\n", m, m->m_len); 1678 } 1679 mtx_unlock(&sc->sc_mtx); 1680 1681 /* Do we have a partial packet waiting? */ 1682 if (mwait != NULL) { 1683 m0 = mwait; 1684 mwait = NULL; 1685 1686 UHSO_DPRINTF(3, "partial m0=%p(%d), concat w/ m=%p(%d)\n", 1687 m0, m0->m_len, m, m->m_len); 1688 len = m->m_len + m0->m_len; 1689 1690 /* Concat mbufs and fix headers */ 1691 m_cat(m0, m); 1692 m0->m_pkthdr.len = len; 1693 m->m_flags &= ~M_PKTHDR; 1694 1695 m = m_pullup(m0, sizeof(struct ip)); 1696 if (m == NULL) { 1697 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1); 1698 UHSO_DPRINTF(0, "m_pullup failed\n"); 1699 mtx_lock(&sc->sc_mtx); 1700 continue; 1701 } 1702 UHSO_DPRINTF(3, "Constructed mbuf=%p, len=%d\n", 1703 m, m->m_pkthdr.len); 1704 } 1705 1706 cp = mtod(m, uint8_t *); 1707 ip = (struct ip *)cp; 1708 #ifdef INET6 1709 ip6 = (struct ip6_hdr *)cp; 1710 #endif 1711 1712 /* Check for IPv4 */ 1713 if (ip->ip_v == IPVERSION) { 1714 iplen = htons(ip->ip_len); 1715 isr = NETISR_IP; 1716 } 1717 #ifdef INET6 1718 /* Check for IPv6 */ 1719 else if ((ip6->ip6_vfc & IPV6_VERSION_MASK) == IPV6_VERSION) { 1720 iplen = htons(ip6->ip6_plen); 1721 isr = NETISR_IPV6; 1722 } 1723 #endif 1724 else { 1725 UHSO_DPRINTF(0, "got unexpected ip version %d, " 1726 "m=%p, len=%d\n", (*cp & 0xf0) >> 4, m, m->m_len); 1727 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1); 1728 UHSO_HEXDUMP(cp, 4); 1729 m_freem(m); 1730 m = NULL; 1731 mtx_lock(&sc->sc_mtx); 1732 continue; 1733 } 1734 1735 if (iplen == 0) { 1736 UHSO_DPRINTF(0, "Zero IP length\n"); 1737 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1); 1738 m_freem(m); 1739 m = NULL; 1740 mtx_lock(&sc->sc_mtx); 1741 continue; 1742 } 1743 1744 UHSO_DPRINTF(3, "m=%p, len=%d, cp=%p, iplen=%d\n", 1745 m, m->m_pkthdr.len, cp, iplen); 1746 1747 m0 = NULL; 1748 1749 /* More IP packets in this mbuf */ 1750 if (iplen < m->m_pkthdr.len) { 1751 m0 = m; 1752 1753 /* 1754 * Allocate a new mbuf for this IP packet and 1755 * copy the IP-packet into it. 1756 */ 1757 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR); 1758 memcpy(mtod(m, uint8_t *), mtod(m0, uint8_t *), iplen); 1759 m->m_pkthdr.len = m->m_len = iplen; 1760 1761 /* Adjust the size of the original mbuf */ 1762 m_adj(m0, iplen); 1763 m0 = m_defrag(m0, M_WAITOK); 1764 1765 UHSO_DPRINTF(3, "New mbuf=%p, len=%d/%d, m0=%p, " 1766 "m0_len=%d/%d\n", m, m->m_pkthdr.len, m->m_len, 1767 m0, m0->m_pkthdr.len, m0->m_len); 1768 } 1769 else if (iplen > m->m_pkthdr.len) { 1770 UHSO_DPRINTF(3, "Deferred mbuf=%p, len=%d\n", 1771 m, m->m_pkthdr.len); 1772 mwait = m; 1773 m = NULL; 1774 mtx_lock(&sc->sc_mtx); 1775 continue; 1776 } 1777 1778 if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1); 1779 m->m_pkthdr.rcvif = ifp; 1780 1781 /* Dispatch to IP layer */ 1782 BPF_MTAP(sc->sc_ifp, m); 1783 M_SETFIB(m, ifp->if_fib); 1784 netisr_dispatch(isr, m); 1785 m = m0 != NULL ? m0 : NULL; 1786 mtx_lock(&sc->sc_mtx); 1787 } 1788 sc->sc_mwait = mwait; 1789 } 1790 1791 static void 1792 uhso_ifnet_write_callback(struct usb_xfer *xfer, usb_error_t error) 1793 { 1794 struct uhso_softc *sc = usbd_xfer_softc(xfer); 1795 struct ifnet *ifp = sc->sc_ifp; 1796 struct usb_page_cache *pc; 1797 struct mbuf *m; 1798 int actlen; 1799 1800 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL); 1801 1802 UHSO_DPRINTF(3, "status %d, actlen=%d\n", USB_GET_STATE(xfer), actlen); 1803 1804 switch (USB_GET_STATE(xfer)) { 1805 case USB_ST_TRANSFERRED: 1806 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1); 1807 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 1808 case USB_ST_SETUP: 1809 tr_setup: 1810 IFQ_DRV_DEQUEUE(&ifp->if_snd, m); 1811 if (m == NULL) 1812 break; 1813 1814 ifp->if_drv_flags |= IFF_DRV_OACTIVE; 1815 1816 if (m->m_pkthdr.len > MCLBYTES) 1817 m->m_pkthdr.len = MCLBYTES; 1818 1819 usbd_xfer_set_frame_len(xfer, 0, m->m_pkthdr.len); 1820 pc = usbd_xfer_get_frame(xfer, 0); 1821 usbd_m_copy_in(pc, 0, m, 0, m->m_pkthdr.len); 1822 usbd_transfer_submit(xfer); 1823 1824 BPF_MTAP(ifp, m); 1825 m_freem(m); 1826 break; 1827 default: 1828 UHSO_DPRINTF(0, "error: %s\n", usbd_errstr(error)); 1829 if (error == USB_ERR_CANCELLED) 1830 break; 1831 usbd_xfer_set_stall(xfer); 1832 goto tr_setup; 1833 } 1834 } 1835 1836 static int 1837 uhso_if_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 1838 { 1839 struct uhso_softc *sc; 1840 1841 sc = ifp->if_softc; 1842 1843 switch (cmd) { 1844 case SIOCSIFFLAGS: 1845 if (ifp->if_flags & IFF_UP) { 1846 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) { 1847 uhso_if_init(sc); 1848 } 1849 } 1850 else { 1851 if (ifp->if_drv_flags & IFF_DRV_RUNNING) { 1852 mtx_lock(&sc->sc_mtx); 1853 uhso_if_stop(sc); 1854 mtx_unlock(&sc->sc_mtx); 1855 } 1856 } 1857 break; 1858 case SIOCSIFADDR: 1859 case SIOCADDMULTI: 1860 case SIOCDELMULTI: 1861 break; 1862 default: 1863 return (EINVAL); 1864 } 1865 return (0); 1866 } 1867 1868 static void 1869 uhso_if_init(void *priv) 1870 { 1871 struct uhso_softc *sc = priv; 1872 struct ifnet *ifp = sc->sc_ifp; 1873 1874 mtx_lock(&sc->sc_mtx); 1875 uhso_if_stop(sc); 1876 ifp = sc->sc_ifp; 1877 ifp->if_flags |= IFF_UP; 1878 ifp->if_drv_flags |= IFF_DRV_RUNNING; 1879 mtx_unlock(&sc->sc_mtx); 1880 1881 UHSO_DPRINTF(2, "ifnet initialized\n"); 1882 } 1883 1884 static int 1885 uhso_if_output(struct ifnet *ifp, struct mbuf *m0, const struct sockaddr *dst, 1886 struct route *ro) 1887 { 1888 int error; 1889 1890 /* Only IPv4/6 support */ 1891 if (dst->sa_family != AF_INET 1892 #ifdef INET6 1893 && dst->sa_family != AF_INET6 1894 #endif 1895 ) { 1896 return (EAFNOSUPPORT); 1897 } 1898 1899 error = (ifp->if_transmit)(ifp, m0); 1900 if (error) { 1901 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 1902 return (ENOBUFS); 1903 } 1904 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1); 1905 return (0); 1906 } 1907 1908 static void 1909 uhso_if_start(struct ifnet *ifp) 1910 { 1911 struct uhso_softc *sc = ifp->if_softc; 1912 1913 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) { 1914 UHSO_DPRINTF(1, "Not running\n"); 1915 return; 1916 } 1917 1918 mtx_lock(&sc->sc_mtx); 1919 usbd_transfer_start(sc->sc_if_xfer[UHSO_IFNET_READ]); 1920 usbd_transfer_start(sc->sc_if_xfer[UHSO_IFNET_WRITE]); 1921 mtx_unlock(&sc->sc_mtx); 1922 UHSO_DPRINTF(3, "interface started\n"); 1923 } 1924 1925 static void 1926 uhso_if_stop(struct uhso_softc *sc) 1927 { 1928 1929 usbd_transfer_stop(sc->sc_if_xfer[UHSO_IFNET_READ]); 1930 usbd_transfer_stop(sc->sc_if_xfer[UHSO_IFNET_WRITE]); 1931 sc->sc_ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE); 1932 } 1933