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 iCON EDGE */ 273 UHSO_DEV(OPTION, ICONEDGE, UHSO_STATIC_IFACE), 274 /* Option Module HSxPA */ 275 UHSO_DEV(OPTION, MODHSXPA, UHSO_STATIC_IFACE), 276 /* Option iCON 321 */ 277 UHSO_DEV(OPTION, ICON321, UHSO_STATIC_IFACE), 278 /* Option iCON 322 */ 279 UHSO_DEV(OPTION, GTICON322, UHSO_STATIC_IFACE), 280 /* Option iCON 505 */ 281 UHSO_DEV(OPTION, ICON505, UHSO_AUTO_IFACE), 282 /* Option iCON 452 */ 283 UHSO_DEV(OPTION, ICON505, UHSO_AUTO_IFACE), 284 #undef UHSO_DEV 285 }; 286 287 static SYSCTL_NODE(_hw_usb, OID_AUTO, uhso, CTLFLAG_RW, 0, "USB uhso"); 288 static int uhso_autoswitch = 1; 289 SYSCTL_INT(_hw_usb_uhso, OID_AUTO, auto_switch, CTLFLAG_RW, 290 &uhso_autoswitch, 0, "Automatically switch to modem mode"); 291 292 #ifdef USB_DEBUG 293 #ifdef UHSO_DEBUG 294 static int uhso_debug = UHSO_DEBUG; 295 #else 296 static int uhso_debug = -1; 297 #endif 298 299 SYSCTL_INT(_hw_usb_uhso, OID_AUTO, debug, CTLFLAG_RW, 300 &uhso_debug, 0, "Debug level"); 301 302 #define UHSO_DPRINTF(n, x, ...) {\ 303 if (uhso_debug >= n) {\ 304 printf("%s: " x, __func__, ##__VA_ARGS__);\ 305 }\ 306 } 307 #else 308 #define UHSO_DPRINTF(n, x, ...) 309 #endif 310 311 #ifdef UHSO_DEBUG_HEXDUMP 312 # define UHSO_HEXDUMP(_buf, _len) do { \ 313 { \ 314 size_t __tmp; \ 315 const char *__buf = (const char *)_buf; \ 316 for (__tmp = 0; __tmp < _len; __tmp++) \ 317 printf("%02hhx ", *__buf++); \ 318 printf("\n"); \ 319 } \ 320 } while(0) 321 #else 322 # define UHSO_HEXDUMP(_buf, _len) 323 #endif 324 325 enum { 326 UHSO_MUX_ENDPT_INTR = 0, 327 UHSO_MUX_ENDPT_MAX 328 }; 329 330 enum { 331 UHSO_CTRL_READ = 0, 332 UHSO_CTRL_WRITE, 333 UHSO_CTRL_MAX 334 }; 335 336 enum { 337 UHSO_IFNET_READ = 0, 338 UHSO_IFNET_WRITE, 339 UHSO_IFNET_MAX 340 }; 341 342 enum { 343 UHSO_BULK_ENDPT_READ = 0, 344 UHSO_BULK_ENDPT_WRITE, 345 UHSO_BULK_ENDPT_INTR, 346 UHSO_BULK_ENDPT_MAX 347 }; 348 349 static usb_callback_t uhso_mux_intr_callback; 350 static usb_callback_t uhso_mux_read_callback; 351 static usb_callback_t uhso_mux_write_callback; 352 static usb_callback_t uhso_bs_read_callback; 353 static usb_callback_t uhso_bs_write_callback; 354 static usb_callback_t uhso_bs_intr_callback; 355 static usb_callback_t uhso_ifnet_read_callback; 356 static usb_callback_t uhso_ifnet_write_callback; 357 358 /* Config used for the default control pipes */ 359 static const struct usb_config uhso_ctrl_config[UHSO_CTRL_MAX] = { 360 [UHSO_CTRL_READ] = { 361 .type = UE_CONTROL, 362 .endpoint = 0x00, 363 .direction = UE_DIR_ANY, 364 .flags = { .pipe_bof = 1, .short_xfer_ok = 1 }, 365 .bufsize = sizeof(struct usb_device_request) + 1024, 366 .callback = &uhso_mux_read_callback 367 }, 368 369 [UHSO_CTRL_WRITE] = { 370 .type = UE_CONTROL, 371 .endpoint = 0x00, 372 .direction = UE_DIR_ANY, 373 .flags = { .pipe_bof = 1, .force_short_xfer = 1 }, 374 .bufsize = sizeof(struct usb_device_request) + 1024, 375 .timeout = 1000, 376 .callback = &uhso_mux_write_callback 377 } 378 }; 379 380 /* Config for the multiplexed serial ports */ 381 static const struct usb_config uhso_mux_config[UHSO_MUX_ENDPT_MAX] = { 382 [UHSO_MUX_ENDPT_INTR] = { 383 .type = UE_INTERRUPT, 384 .endpoint = UE_ADDR_ANY, 385 .direction = UE_DIR_IN, 386 .flags = { .short_xfer_ok = 1 }, 387 .bufsize = 0, 388 .callback = &uhso_mux_intr_callback, 389 } 390 }; 391 392 /* Config for the raw IP-packet interface */ 393 static const struct usb_config uhso_ifnet_config[UHSO_IFNET_MAX] = { 394 [UHSO_IFNET_READ] = { 395 .type = UE_BULK, 396 .endpoint = UE_ADDR_ANY, 397 .direction = UE_DIR_IN, 398 .flags = { .pipe_bof = 1, .short_xfer_ok = 1 }, 399 .bufsize = MCLBYTES, 400 .callback = &uhso_ifnet_read_callback 401 }, 402 [UHSO_IFNET_WRITE] = { 403 .type = UE_BULK, 404 .endpoint = UE_ADDR_ANY, 405 .direction = UE_DIR_OUT, 406 .flags = { .pipe_bof = 1, .force_short_xfer = 1 }, 407 .bufsize = MCLBYTES, 408 .timeout = 5 * USB_MS_HZ, 409 .callback = &uhso_ifnet_write_callback 410 } 411 }; 412 413 /* Config for interfaces with normal bulk serial ports */ 414 static const struct usb_config uhso_bs_config[UHSO_BULK_ENDPT_MAX] = { 415 [UHSO_BULK_ENDPT_READ] = { 416 .type = UE_BULK, 417 .endpoint = UE_ADDR_ANY, 418 .direction = UE_DIR_IN, 419 .flags = { .pipe_bof = 1, .short_xfer_ok = 1 }, 420 .bufsize = 4096, 421 .callback = &uhso_bs_read_callback 422 }, 423 424 [UHSO_BULK_ENDPT_WRITE] = { 425 .type = UE_BULK, 426 .endpoint = UE_ADDR_ANY, 427 .direction = UE_DIR_OUT, 428 .flags = { .pipe_bof = 1, .force_short_xfer = 1 }, 429 .bufsize = 8192, 430 .callback = &uhso_bs_write_callback 431 }, 432 433 [UHSO_BULK_ENDPT_INTR] = { 434 .type = UE_INTERRUPT, 435 .endpoint = UE_ADDR_ANY, 436 .direction = UE_DIR_IN, 437 .flags = { .short_xfer_ok = 1 }, 438 .bufsize = 0, 439 .callback = &uhso_bs_intr_callback, 440 } 441 }; 442 443 static int uhso_probe_iface(struct uhso_softc *, int, 444 int (*probe)(struct usb_device *, int)); 445 static int uhso_probe_iface_auto(struct usb_device *, int); 446 static int uhso_probe_iface_static(struct usb_device *, int); 447 static int uhso_attach_muxserial(struct uhso_softc *, struct usb_interface *, 448 int type); 449 static int uhso_attach_bulkserial(struct uhso_softc *, struct usb_interface *, 450 int type); 451 static int uhso_attach_ifnet(struct uhso_softc *, struct usb_interface *, 452 int type); 453 static void uhso_test_autoinst(void *, struct usb_device *, 454 struct usb_attach_arg *); 455 static int uhso_driver_loaded(struct module *, int, void *); 456 static int uhso_radio_sysctl(SYSCTL_HANDLER_ARGS); 457 static int uhso_radio_ctrl(struct uhso_softc *, int); 458 459 static void uhso_free(struct ucom_softc *); 460 static void uhso_ucom_start_read(struct ucom_softc *); 461 static void uhso_ucom_stop_read(struct ucom_softc *); 462 static void uhso_ucom_start_write(struct ucom_softc *); 463 static void uhso_ucom_stop_write(struct ucom_softc *); 464 static void uhso_ucom_cfg_get_status(struct ucom_softc *, uint8_t *, uint8_t *); 465 static void uhso_ucom_cfg_set_dtr(struct ucom_softc *, uint8_t); 466 static void uhso_ucom_cfg_set_rts(struct ucom_softc *, uint8_t); 467 static void uhso_if_init(void *); 468 static void uhso_if_start(struct ifnet *); 469 static void uhso_if_stop(struct uhso_softc *); 470 static int uhso_if_ioctl(struct ifnet *, u_long, caddr_t); 471 static int uhso_if_output(struct ifnet *, struct mbuf *, 472 const struct sockaddr *, struct route *); 473 static void uhso_if_rxflush(void *); 474 475 static device_probe_t uhso_probe; 476 static device_attach_t uhso_attach; 477 static device_detach_t uhso_detach; 478 static void uhso_free_softc(struct uhso_softc *); 479 480 static device_method_t uhso_methods[] = { 481 DEVMETHOD(device_probe, uhso_probe), 482 DEVMETHOD(device_attach, uhso_attach), 483 DEVMETHOD(device_detach, uhso_detach), 484 { 0, 0 } 485 }; 486 487 static driver_t uhso_driver = { 488 .name = "uhso", 489 .methods = uhso_methods, 490 .size = sizeof(struct uhso_softc) 491 }; 492 493 static devclass_t uhso_devclass; 494 DRIVER_MODULE(uhso, uhub, uhso_driver, uhso_devclass, uhso_driver_loaded, 0); 495 MODULE_DEPEND(uhso, ucom, 1, 1, 1); 496 MODULE_DEPEND(uhso, usb, 1, 1, 1); 497 MODULE_VERSION(uhso, 1); 498 499 static struct ucom_callback uhso_ucom_callback = { 500 .ucom_cfg_get_status = &uhso_ucom_cfg_get_status, 501 .ucom_cfg_set_dtr = &uhso_ucom_cfg_set_dtr, 502 .ucom_cfg_set_rts = &uhso_ucom_cfg_set_rts, 503 .ucom_start_read = uhso_ucom_start_read, 504 .ucom_stop_read = uhso_ucom_stop_read, 505 .ucom_start_write = uhso_ucom_start_write, 506 .ucom_stop_write = uhso_ucom_stop_write, 507 .ucom_free = &uhso_free, 508 }; 509 510 static int 511 uhso_probe(device_t self) 512 { 513 struct usb_attach_arg *uaa = device_get_ivars(self); 514 int error; 515 516 if (uaa->usb_mode != USB_MODE_HOST) 517 return (ENXIO); 518 if (uaa->info.bConfigIndex != 0) 519 return (ENXIO); 520 if (uaa->info.bDeviceClass != 0xff) 521 return (ENXIO); 522 523 error = usbd_lookup_id_by_uaa(uhso_devs, sizeof(uhso_devs), uaa); 524 if (error != 0) 525 return (error); 526 527 /* 528 * Probe device to see if we are able to attach 529 * to this interface or not. 530 */ 531 if (USB_GET_DRIVER_INFO(uaa) == UHSO_AUTO_IFACE) { 532 if (uhso_probe_iface_auto(uaa->device, 533 uaa->info.bIfaceNum) == 0) 534 return (ENXIO); 535 } 536 return (error); 537 } 538 539 static int 540 uhso_attach(device_t self) 541 { 542 struct uhso_softc *sc = device_get_softc(self); 543 struct usb_attach_arg *uaa = device_get_ivars(self); 544 struct usb_interface_descriptor *id; 545 struct sysctl_ctx_list *sctx; 546 struct sysctl_oid *soid; 547 struct sysctl_oid *tree = NULL, *tty_node; 548 struct ucom_softc *ucom; 549 struct uhso_tty *ht; 550 int i, error, port; 551 void *probe_f; 552 usb_error_t uerr; 553 char *desc; 554 555 sc->sc_dev = self; 556 sc->sc_udev = uaa->device; 557 mtx_init(&sc->sc_mtx, "uhso", NULL, MTX_DEF); 558 ucom_ref(&sc->sc_super_ucom); 559 560 sc->sc_ucom = NULL; 561 sc->sc_ttys = 0; 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 free(sc->sc_tty, M_USBDEV); 683 free(sc->sc_ucom, M_USBDEV); 684 } 685 686 if (sc->sc_ifp != NULL) { 687 callout_drain(&sc->sc_c); 688 free_unr(uhso_ifnet_unit, sc->sc_ifp->if_dunit); 689 mtx_lock(&sc->sc_mtx); 690 uhso_if_stop(sc); 691 bpfdetach(sc->sc_ifp); 692 if_detach(sc->sc_ifp); 693 if_free(sc->sc_ifp); 694 mtx_unlock(&sc->sc_mtx); 695 usbd_transfer_unsetup(sc->sc_if_xfer, UHSO_IFNET_MAX); 696 } 697 698 device_claim_softc(self); 699 700 uhso_free_softc(sc); 701 702 return (0); 703 } 704 705 UCOM_UNLOAD_DRAIN(uhso); 706 707 static void 708 uhso_free_softc(struct uhso_softc *sc) 709 { 710 if (ucom_unref(&sc->sc_super_ucom)) { 711 mtx_destroy(&sc->sc_mtx); 712 device_free_softc(sc); 713 } 714 } 715 716 static void 717 uhso_free(struct ucom_softc *ucom) 718 { 719 uhso_free_softc(ucom->sc_parent); 720 } 721 722 static void 723 uhso_test_autoinst(void *arg, struct usb_device *udev, 724 struct usb_attach_arg *uaa) 725 { 726 struct usb_interface *iface; 727 struct usb_interface_descriptor *id; 728 729 if (uaa->dev_state != UAA_DEV_READY || !uhso_autoswitch) 730 return; 731 732 iface = usbd_get_iface(udev, 0); 733 if (iface == NULL) 734 return; 735 id = iface->idesc; 736 if (id == NULL || id->bInterfaceClass != UICLASS_MASS) 737 return; 738 if (usbd_lookup_id_by_uaa(uhso_devs, sizeof(uhso_devs), uaa)) 739 return; /* no device match */ 740 741 if (usb_msc_eject(udev, 0, MSC_EJECT_REZERO) == 0) { 742 /* success, mark the udev as disappearing */ 743 uaa->dev_state = UAA_DEV_EJECTING; 744 } 745 } 746 747 static int 748 uhso_driver_loaded(struct module *mod, int what, void *arg) 749 { 750 switch (what) { 751 case MOD_LOAD: 752 /* register our autoinstall handler */ 753 uhso_etag = EVENTHANDLER_REGISTER(usb_dev_configured, 754 uhso_test_autoinst, NULL, EVENTHANDLER_PRI_ANY); 755 /* create our unit allocator for inet devs */ 756 uhso_ifnet_unit = new_unrhdr(0, INT_MAX, NULL); 757 break; 758 case MOD_UNLOAD: 759 EVENTHANDLER_DEREGISTER(usb_dev_configured, uhso_etag); 760 delete_unrhdr(uhso_ifnet_unit); 761 break; 762 default: 763 return (EOPNOTSUPP); 764 } 765 return (0); 766 } 767 768 /* 769 * Probe the interface type by querying the device. The elements 770 * of an array indicates the capabilities of a particular interface. 771 * Returns a bit mask with the interface capabilities. 772 */ 773 static int 774 uhso_probe_iface_auto(struct usb_device *udev, int index) 775 { 776 struct usb_device_request req; 777 usb_error_t uerr; 778 uint16_t actlen = 0; 779 char port; 780 char buf[17] = {0}; 781 782 req.bmRequestType = UT_READ_VENDOR_DEVICE; 783 req.bRequest = 0x86; 784 USETW(req.wValue, 0); 785 USETW(req.wIndex, 0); 786 USETW(req.wLength, 17); 787 788 uerr = usbd_do_request_flags(udev, NULL, &req, buf, 789 0, &actlen, USB_MS_HZ); 790 if (uerr != 0) { 791 printf("%s: usbd_do_request_flags failed, %s\n", 792 __func__, usbd_errstr(uerr)); 793 return (0); 794 } 795 796 UHSO_DPRINTF(1, "actlen=%d\n", actlen); 797 UHSO_HEXDUMP(buf, 17); 798 799 if (index < 0 || index > 16) { 800 UHSO_DPRINTF(0, "Index %d out of range\n", index); 801 return (0); 802 } 803 804 UHSO_DPRINTF(1, "index=%d, type=%x[%s]\n", index, buf[index], 805 uhso_port_type[(int)uhso_port_map[(int)buf[index]]]); 806 807 if (buf[index] >= uhso_port_map_max) 808 port = 0; 809 else 810 port = uhso_port_map[(int)buf[index]]; 811 812 switch (port) { 813 case UHSO_PORT_TYPE_NETWORK: 814 return (UHSO_IFACE_SPEC(UHSO_IF_NET | UHSO_IF_MUX, 815 UHSO_PORT_SERIAL | UHSO_PORT_NETWORK, port)); 816 case UHSO_PORT_TYPE_DIAG: 817 case UHSO_PORT_TYPE_DIAG2: 818 case UHSO_PORT_TYPE_CTL: 819 case UHSO_PORT_TYPE_APP: 820 case UHSO_PORT_TYPE_APP2: 821 case UHSO_PORT_TYPE_MODEM: 822 return (UHSO_IFACE_SPEC(UHSO_IF_BULK, 823 UHSO_PORT_SERIAL, port)); 824 case UHSO_PORT_TYPE_MSD: 825 return (0); 826 case UHSO_PORT_TYPE_UNKNOWN: 827 default: 828 return (0); 829 } 830 831 return (0); 832 } 833 834 /* 835 * Returns the capabilities of interfaces for devices that don't 836 * support the automatic query. 837 * Returns a bit mask with the interface capabilities. 838 */ 839 static int 840 uhso_probe_iface_static(struct usb_device *udev, int index) 841 { 842 struct usb_config_descriptor *cd; 843 844 cd = usbd_get_config_descriptor(udev); 845 if (cd->bNumInterface <= 3) { 846 /* Cards with 3 or less interfaces */ 847 switch (index) { 848 case 0: 849 return UHSO_IFACE_SPEC(UHSO_IF_NET | UHSO_IF_MUX, 850 UHSO_PORT_SERIAL | UHSO_PORT_NETWORK, 851 UHSO_PORT_TYPE_NETWORK); 852 case 1: 853 return UHSO_IFACE_SPEC(UHSO_IF_BULK, 854 UHSO_PORT_SERIAL, UHSO_PORT_TYPE_DIAG); 855 case 2: 856 return UHSO_IFACE_SPEC(UHSO_IF_BULK, 857 UHSO_PORT_SERIAL, UHSO_PORT_TYPE_MODEM); 858 } 859 } else { 860 /* Cards with 4 interfaces */ 861 switch (index) { 862 case 0: 863 return UHSO_IFACE_SPEC(UHSO_IF_NET | UHSO_IF_MUX, 864 UHSO_PORT_SERIAL | UHSO_PORT_NETWORK, 865 UHSO_PORT_TYPE_NETWORK); 866 case 1: 867 return UHSO_IFACE_SPEC(UHSO_IF_BULK, 868 UHSO_PORT_SERIAL, UHSO_PORT_TYPE_DIAG2); 869 case 2: 870 return UHSO_IFACE_SPEC(UHSO_IF_BULK, 871 UHSO_PORT_SERIAL, UHSO_PORT_TYPE_MODEM); 872 case 3: 873 return UHSO_IFACE_SPEC(UHSO_IF_BULK, 874 UHSO_PORT_SERIAL, UHSO_PORT_TYPE_DIAG); 875 } 876 } 877 return (0); 878 } 879 880 /* 881 * Probes an interface for its particular capabilities and attaches if 882 * it's a supported interface. 883 */ 884 static int 885 uhso_probe_iface(struct uhso_softc *sc, int index, 886 int (*probe)(struct usb_device *, int)) 887 { 888 struct usb_interface *iface; 889 int type, error; 890 891 UHSO_DPRINTF(1, "Probing for interface %d, probe_func=%p\n", index, probe); 892 893 type = probe(sc->sc_udev, index); 894 UHSO_DPRINTF(1, "Probe result %x\n", type); 895 if (type <= 0) 896 return (ENXIO); 897 898 sc->sc_type = type; 899 iface = usbd_get_iface(sc->sc_udev, index); 900 901 if (UHSO_IFACE_PORT_TYPE(type) == UHSO_PORT_TYPE_NETWORK) { 902 error = uhso_attach_ifnet(sc, iface, type); 903 if (error) { 904 UHSO_DPRINTF(1, "uhso_attach_ifnet failed"); 905 return (ENXIO); 906 } 907 908 /* 909 * If there is an additional interrupt endpoint on this 910 * interface then we most likely have a multiplexed serial port 911 * available. 912 */ 913 if (iface->idesc->bNumEndpoints < 3) { 914 sc->sc_type = UHSO_IFACE_SPEC( 915 UHSO_IFACE_USB_TYPE(type) & ~UHSO_IF_MUX, 916 UHSO_IFACE_PORT(type) & ~UHSO_PORT_SERIAL, 917 UHSO_IFACE_PORT_TYPE(type)); 918 return (0); 919 } 920 921 UHSO_DPRINTF(1, "Trying to attach mux. serial\n"); 922 error = uhso_attach_muxserial(sc, iface, type); 923 if (error == 0 && sc->sc_ttys > 0) { 924 error = ucom_attach(&sc->sc_super_ucom, sc->sc_ucom, 925 sc->sc_ttys, sc, &uhso_ucom_callback, &sc->sc_mtx); 926 if (error) { 927 device_printf(sc->sc_dev, "ucom_attach failed\n"); 928 return (ENXIO); 929 } 930 ucom_set_pnpinfo_usb(&sc->sc_super_ucom, sc->sc_dev); 931 932 mtx_lock(&sc->sc_mtx); 933 usbd_transfer_start(sc->sc_xfer[UHSO_MUX_ENDPT_INTR]); 934 mtx_unlock(&sc->sc_mtx); 935 } 936 } else if ((UHSO_IFACE_USB_TYPE(type) & UHSO_IF_BULK) && 937 UHSO_IFACE_PORT(type) & UHSO_PORT_SERIAL) { 938 939 error = uhso_attach_bulkserial(sc, iface, type); 940 if (error) 941 return (ENXIO); 942 943 error = ucom_attach(&sc->sc_super_ucom, sc->sc_ucom, 944 sc->sc_ttys, sc, &uhso_ucom_callback, &sc->sc_mtx); 945 if (error) { 946 device_printf(sc->sc_dev, "ucom_attach failed\n"); 947 return (ENXIO); 948 } 949 ucom_set_pnpinfo_usb(&sc->sc_super_ucom, sc->sc_dev); 950 } 951 else { 952 UHSO_DPRINTF(0, "Unknown type %x\n", type); 953 return (ENXIO); 954 } 955 956 return (0); 957 } 958 959 static int 960 uhso_radio_ctrl(struct uhso_softc *sc, int onoff) 961 { 962 struct usb_device_request req; 963 usb_error_t uerr; 964 965 req.bmRequestType = UT_VENDOR; 966 req.bRequest = onoff ? 0x82 : 0x81; 967 USETW(req.wValue, 0); 968 USETW(req.wIndex, 0); 969 USETW(req.wLength, 0); 970 971 uerr = usbd_do_request(sc->sc_udev, NULL, &req, NULL); 972 if (uerr != 0) { 973 device_printf(sc->sc_dev, "usbd_do_request_flags failed: %s\n", 974 usbd_errstr(uerr)); 975 return (-1); 976 } 977 return (onoff); 978 } 979 980 static int 981 uhso_radio_sysctl(SYSCTL_HANDLER_ARGS) 982 { 983 struct uhso_softc *sc = arg1; 984 int error, radio; 985 986 radio = sc->sc_radio; 987 error = sysctl_handle_int(oidp, &radio, 0, req); 988 if (error) 989 return (error); 990 if (radio != sc->sc_radio) { 991 radio = radio != 0 ? 1 : 0; 992 error = uhso_radio_ctrl(sc, radio); 993 if (error != -1) 994 sc->sc_radio = radio; 995 996 } 997 return (0); 998 } 999 1000 /* 1001 * Expands allocated memory to fit an additional TTY. 1002 * Two arrays are kept with matching indexes, one for ucom and one 1003 * for our private data. 1004 */ 1005 static int 1006 uhso_alloc_tty(struct uhso_softc *sc) 1007 { 1008 1009 sc->sc_ttys++; 1010 sc->sc_tty = reallocf(sc->sc_tty, sizeof(struct uhso_tty) * sc->sc_ttys, 1011 M_USBDEV, M_WAITOK | M_ZERO); 1012 if (sc->sc_tty == NULL) 1013 return (-1); 1014 1015 sc->sc_ucom = reallocf(sc->sc_ucom, 1016 sizeof(struct ucom_softc) * sc->sc_ttys, M_USBDEV, M_WAITOK | M_ZERO); 1017 if (sc->sc_ucom == NULL) 1018 return (-1); 1019 1020 sc->sc_tty[sc->sc_ttys - 1].ht_sc = sc; 1021 1022 UHSO_DPRINTF(1, "Allocated TTY %d\n", sc->sc_ttys - 1); 1023 return (sc->sc_ttys - 1); 1024 } 1025 1026 /* 1027 * Attach a multiplexed serial port 1028 * Data is read/written with requests on the default control pipe. An interrupt 1029 * endpoint returns when there is new data to be read. 1030 */ 1031 static int 1032 uhso_attach_muxserial(struct uhso_softc *sc, struct usb_interface *iface, 1033 int type) 1034 { 1035 struct usb_descriptor *desc; 1036 int i, port, tty; 1037 usb_error_t uerr; 1038 1039 /* 1040 * The class specific interface (type 0x24) descriptor subtype field 1041 * contains a bitmask that specifies which (and how many) ports that 1042 * are available through this multiplexed serial port. 1043 */ 1044 desc = usbd_find_descriptor(sc->sc_udev, NULL, 1045 iface->idesc->bInterfaceNumber, UDESC_CS_INTERFACE, 0xff, 0, 0); 1046 if (desc == NULL) { 1047 UHSO_DPRINTF(0, "Failed to find UDESC_CS_INTERFACE\n"); 1048 return (ENXIO); 1049 } 1050 1051 UHSO_DPRINTF(1, "Mux port mask %x\n", desc->bDescriptorSubtype); 1052 if (desc->bDescriptorSubtype == 0) 1053 return (ENXIO); 1054 1055 /* 1056 * The bitmask is one octet, loop through the number of 1057 * bits that are set and create a TTY for each. 1058 */ 1059 for (i = 0; i < 8; i++) { 1060 port = (1 << i); 1061 if ((port & desc->bDescriptorSubtype) == port) { 1062 UHSO_DPRINTF(2, "Found mux port %x (%d)\n", port, i); 1063 tty = uhso_alloc_tty(sc); 1064 if (tty < 0) 1065 return (ENOMEM); 1066 sc->sc_tty[tty].ht_muxport = i; 1067 uerr = usbd_transfer_setup(sc->sc_udev, 1068 &sc->sc_iface_index, sc->sc_tty[tty].ht_xfer, 1069 uhso_ctrl_config, UHSO_CTRL_MAX, sc, &sc->sc_mtx); 1070 if (uerr) { 1071 device_printf(sc->sc_dev, 1072 "Failed to setup control pipe: %s\n", 1073 usbd_errstr(uerr)); 1074 return (ENXIO); 1075 } 1076 } 1077 } 1078 1079 /* Setup the intr. endpoint */ 1080 uerr = usbd_transfer_setup(sc->sc_udev, 1081 &iface->idesc->bInterfaceNumber, sc->sc_xfer, 1082 uhso_mux_config, 1, sc, &sc->sc_mtx); 1083 if (uerr) 1084 return (ENXIO); 1085 1086 return (0); 1087 } 1088 1089 /* 1090 * Interrupt callback for the multiplexed serial port. Indicates 1091 * which serial port has data waiting. 1092 */ 1093 static void 1094 uhso_mux_intr_callback(struct usb_xfer *xfer, usb_error_t error) 1095 { 1096 struct usb_page_cache *pc; 1097 struct usb_page_search res; 1098 struct uhso_softc *sc = usbd_xfer_softc(xfer); 1099 unsigned int i, mux; 1100 1101 UHSO_DPRINTF(3, "status %d\n", USB_GET_STATE(xfer)); 1102 1103 switch (USB_GET_STATE(xfer)) { 1104 case USB_ST_TRANSFERRED: 1105 /* 1106 * The multiplexed port number can be found at the first byte. 1107 * It contains a bit mask, we transform this in to an integer. 1108 */ 1109 pc = usbd_xfer_get_frame(xfer, 0); 1110 usbd_get_page(pc, 0, &res); 1111 1112 i = *((unsigned char *)res.buffer); 1113 mux = 0; 1114 while (i >>= 1) { 1115 mux++; 1116 } 1117 1118 UHSO_DPRINTF(3, "mux port %d (%d)\n", mux, i); 1119 if (mux > UHSO_MPORT_TYPE_NOMAX) 1120 break; 1121 1122 /* Issue a read for this serial port */ 1123 usbd_xfer_set_priv( 1124 sc->sc_tty[mux].ht_xfer[UHSO_CTRL_READ], 1125 &sc->sc_tty[mux]); 1126 usbd_transfer_start(sc->sc_tty[mux].ht_xfer[UHSO_CTRL_READ]); 1127 1128 break; 1129 case USB_ST_SETUP: 1130 tr_setup: 1131 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer)); 1132 usbd_transfer_submit(xfer); 1133 break; 1134 default: 1135 UHSO_DPRINTF(0, "error: %s\n", usbd_errstr(error)); 1136 if (error == USB_ERR_CANCELLED) 1137 break; 1138 1139 usbd_xfer_set_stall(xfer); 1140 goto tr_setup; 1141 } 1142 } 1143 1144 static void 1145 uhso_mux_read_callback(struct usb_xfer *xfer, usb_error_t error) 1146 { 1147 struct uhso_softc *sc = usbd_xfer_softc(xfer); 1148 struct usb_page_cache *pc; 1149 struct usb_device_request req; 1150 struct uhso_tty *ht; 1151 int actlen, len; 1152 1153 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL); 1154 1155 UHSO_DPRINTF(3, "status %d\n", USB_GET_STATE(xfer)); 1156 1157 ht = usbd_xfer_get_priv(xfer); 1158 UHSO_DPRINTF(3, "ht=%p open=%d\n", ht, ht->ht_open); 1159 1160 switch (USB_GET_STATE(xfer)) { 1161 case USB_ST_TRANSFERRED: 1162 /* Got data, send to ucom */ 1163 pc = usbd_xfer_get_frame(xfer, 1); 1164 len = usbd_xfer_frame_len(xfer, 1); 1165 1166 UHSO_DPRINTF(3, "got %d bytes on mux port %d\n", len, 1167 ht->ht_muxport); 1168 if (len <= 0) { 1169 usbd_transfer_start(sc->sc_xfer[UHSO_MUX_ENDPT_INTR]); 1170 break; 1171 } 1172 1173 /* Deliver data if the TTY is open, discard otherwise */ 1174 if (ht->ht_open) 1175 ucom_put_data(&sc->sc_ucom[ht->ht_muxport], pc, 0, len); 1176 /* FALLTHROUGH */ 1177 case USB_ST_SETUP: 1178 tr_setup: 1179 memset(&req, 0, sizeof(struct usb_device_request)); 1180 req.bmRequestType = UT_READ_CLASS_INTERFACE; 1181 req.bRequest = UCDC_GET_ENCAPSULATED_RESPONSE; 1182 USETW(req.wValue, 0); 1183 USETW(req.wIndex, ht->ht_muxport); 1184 USETW(req.wLength, 1024); 1185 1186 pc = usbd_xfer_get_frame(xfer, 0); 1187 usbd_copy_in(pc, 0, &req, sizeof(req)); 1188 1189 usbd_xfer_set_frame_len(xfer, 0, sizeof(req)); 1190 usbd_xfer_set_frame_len(xfer, 1, 1024); 1191 usbd_xfer_set_frames(xfer, 2); 1192 usbd_transfer_submit(xfer); 1193 break; 1194 default: 1195 UHSO_DPRINTF(0, "error: %s\n", usbd_errstr(error)); 1196 if (error == USB_ERR_CANCELLED) 1197 break; 1198 usbd_xfer_set_stall(xfer); 1199 goto tr_setup; 1200 } 1201 } 1202 1203 static void 1204 uhso_mux_write_callback(struct usb_xfer *xfer, usb_error_t error) 1205 { 1206 struct uhso_softc *sc = usbd_xfer_softc(xfer); 1207 struct uhso_tty *ht; 1208 struct usb_page_cache *pc; 1209 struct usb_device_request req; 1210 int actlen; 1211 struct usb_page_search res; 1212 1213 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL); 1214 1215 ht = usbd_xfer_get_priv(xfer); 1216 UHSO_DPRINTF(3, "status=%d, using mux port %d\n", 1217 USB_GET_STATE(xfer), ht->ht_muxport); 1218 1219 switch (USB_GET_STATE(xfer)) { 1220 case USB_ST_TRANSFERRED: 1221 UHSO_DPRINTF(3, "wrote %zd data bytes to muxport %d\n", 1222 actlen - sizeof(struct usb_device_request) , 1223 ht->ht_muxport); 1224 /* FALLTHROUGH */ 1225 case USB_ST_SETUP: 1226 pc = usbd_xfer_get_frame(xfer, 1); 1227 if (ucom_get_data(&sc->sc_ucom[ht->ht_muxport], pc, 1228 0, 32, &actlen)) { 1229 1230 usbd_get_page(pc, 0, &res); 1231 1232 memset(&req, 0, sizeof(struct usb_device_request)); 1233 req.bmRequestType = UT_WRITE_CLASS_INTERFACE; 1234 req.bRequest = UCDC_SEND_ENCAPSULATED_COMMAND; 1235 USETW(req.wValue, 0); 1236 USETW(req.wIndex, ht->ht_muxport); 1237 USETW(req.wLength, actlen); 1238 1239 pc = usbd_xfer_get_frame(xfer, 0); 1240 usbd_copy_in(pc, 0, &req, sizeof(req)); 1241 1242 usbd_xfer_set_frame_len(xfer, 0, sizeof(req)); 1243 usbd_xfer_set_frame_len(xfer, 1, actlen); 1244 usbd_xfer_set_frames(xfer, 2); 1245 1246 UHSO_DPRINTF(3, "Prepared %d bytes for transmit " 1247 "on muxport %d\n", actlen, ht->ht_muxport); 1248 1249 usbd_transfer_submit(xfer); 1250 } 1251 break; 1252 default: 1253 UHSO_DPRINTF(0, "error: %s\n", usbd_errstr(error)); 1254 if (error == USB_ERR_CANCELLED) 1255 break; 1256 break; 1257 } 1258 } 1259 1260 static int 1261 uhso_attach_bulkserial(struct uhso_softc *sc, struct usb_interface *iface, 1262 int type) 1263 { 1264 usb_error_t uerr; 1265 int tty; 1266 1267 /* Try attaching RD/WR/INTR first */ 1268 uerr = usbd_transfer_setup(sc->sc_udev, 1269 &iface->idesc->bInterfaceNumber, sc->sc_xfer, 1270 uhso_bs_config, UHSO_BULK_ENDPT_MAX, sc, &sc->sc_mtx); 1271 if (uerr) { 1272 /* Try only RD/WR */ 1273 uerr = usbd_transfer_setup(sc->sc_udev, 1274 &iface->idesc->bInterfaceNumber, sc->sc_xfer, 1275 uhso_bs_config, UHSO_BULK_ENDPT_MAX - 1, sc, &sc->sc_mtx); 1276 } 1277 if (uerr) { 1278 UHSO_DPRINTF(0, "usbd_transfer_setup failed"); 1279 return (-1); 1280 } 1281 1282 tty = uhso_alloc_tty(sc); 1283 if (tty < 0) { 1284 usbd_transfer_unsetup(sc->sc_xfer, UHSO_BULK_ENDPT_MAX); 1285 return (ENOMEM); 1286 } 1287 1288 sc->sc_tty[tty].ht_muxport = -1; 1289 return (0); 1290 } 1291 1292 static void 1293 uhso_bs_read_callback(struct usb_xfer *xfer, usb_error_t error) 1294 { 1295 struct uhso_softc *sc = usbd_xfer_softc(xfer); 1296 struct usb_page_cache *pc; 1297 int actlen; 1298 1299 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL); 1300 1301 UHSO_DPRINTF(3, "status %d, actlen=%d\n", USB_GET_STATE(xfer), actlen); 1302 1303 switch (USB_GET_STATE(xfer)) { 1304 case USB_ST_TRANSFERRED: 1305 pc = usbd_xfer_get_frame(xfer, 0); 1306 ucom_put_data(&sc->sc_ucom[0], pc, 0, actlen); 1307 /* FALLTHROUGH */ 1308 case USB_ST_SETUP: 1309 tr_setup: 1310 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer)); 1311 usbd_transfer_submit(xfer); 1312 break; 1313 default: 1314 UHSO_DPRINTF(0, "error: %s\n", usbd_errstr(error)); 1315 if (error == USB_ERR_CANCELLED) 1316 break; 1317 usbd_xfer_set_stall(xfer); 1318 goto tr_setup; 1319 } 1320 } 1321 1322 static void 1323 uhso_bs_write_callback(struct usb_xfer *xfer, usb_error_t error) 1324 { 1325 struct uhso_softc *sc = usbd_xfer_softc(xfer); 1326 struct usb_page_cache *pc; 1327 int actlen; 1328 1329 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL); 1330 1331 UHSO_DPRINTF(3, "status %d, actlen=%d\n", USB_GET_STATE(xfer), actlen); 1332 1333 switch (USB_GET_STATE(xfer)) { 1334 case USB_ST_TRANSFERRED: 1335 case USB_ST_SETUP: 1336 tr_setup: 1337 pc = usbd_xfer_get_frame(xfer, 0); 1338 if (ucom_get_data(&sc->sc_ucom[0], pc, 0, 8192, &actlen)) { 1339 usbd_xfer_set_frame_len(xfer, 0, actlen); 1340 usbd_transfer_submit(xfer); 1341 } 1342 break; 1343 break; 1344 default: 1345 UHSO_DPRINTF(0, "error: %s\n", usbd_errstr(error)); 1346 if (error == USB_ERR_CANCELLED) 1347 break; 1348 usbd_xfer_set_stall(xfer); 1349 goto tr_setup; 1350 } 1351 } 1352 1353 static void 1354 uhso_bs_cfg(struct uhso_softc *sc) 1355 { 1356 struct usb_device_request req; 1357 usb_error_t uerr; 1358 1359 if (!(UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_BULK)) 1360 return; 1361 1362 req.bmRequestType = UT_WRITE_CLASS_INTERFACE; 1363 req.bRequest = UCDC_SET_CONTROL_LINE_STATE; 1364 USETW(req.wValue, sc->sc_line); 1365 USETW(req.wIndex, sc->sc_iface_no); 1366 USETW(req.wLength, 0); 1367 1368 uerr = ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom[0], &req, NULL, 0, 1000); 1369 if (uerr != 0) { 1370 device_printf(sc->sc_dev, "failed to set ctrl line state to " 1371 "0x%02x: %s\n", sc->sc_line, usbd_errstr(uerr)); 1372 } 1373 } 1374 1375 static void 1376 uhso_bs_intr_callback(struct usb_xfer *xfer, usb_error_t error) 1377 { 1378 struct uhso_softc *sc = usbd_xfer_softc(xfer); 1379 struct usb_page_cache *pc; 1380 int actlen; 1381 struct usb_cdc_notification cdc; 1382 1383 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL); 1384 UHSO_DPRINTF(3, "status %d, actlen=%d\n", USB_GET_STATE(xfer), actlen); 1385 1386 switch (USB_GET_STATE(xfer)) { 1387 case USB_ST_TRANSFERRED: 1388 if (actlen < UCDC_NOTIFICATION_LENGTH) { 1389 UHSO_DPRINTF(0, "UCDC notification too short: %d\n", actlen); 1390 goto tr_setup; 1391 } 1392 else if (actlen > (int)sizeof(struct usb_cdc_notification)) { 1393 UHSO_DPRINTF(0, "UCDC notification too large: %d\n", actlen); 1394 actlen = sizeof(struct usb_cdc_notification); 1395 } 1396 1397 pc = usbd_xfer_get_frame(xfer, 0); 1398 usbd_copy_out(pc, 0, &cdc, actlen); 1399 1400 if (UGETW(cdc.wIndex) != sc->sc_iface_no) { 1401 UHSO_DPRINTF(0, "Interface mismatch, got %d expected %d\n", 1402 UGETW(cdc.wIndex), sc->sc_iface_no); 1403 goto tr_setup; 1404 } 1405 1406 if (cdc.bmRequestType == UCDC_NOTIFICATION && 1407 cdc.bNotification == UCDC_N_SERIAL_STATE) { 1408 UHSO_DPRINTF(2, "notify = 0x%02x\n", cdc.data[0]); 1409 1410 sc->sc_msr = 0; 1411 sc->sc_lsr = 0; 1412 if (cdc.data[0] & UCDC_N_SERIAL_RI) 1413 sc->sc_msr |= SER_RI; 1414 if (cdc.data[0] & UCDC_N_SERIAL_DSR) 1415 sc->sc_msr |= SER_DSR; 1416 if (cdc.data[0] & UCDC_N_SERIAL_DCD) 1417 sc->sc_msr |= SER_DCD; 1418 1419 ucom_status_change(&sc->sc_ucom[0]); 1420 } 1421 case USB_ST_SETUP: 1422 tr_setup: 1423 default: 1424 if (error == USB_ERR_CANCELLED) 1425 break; 1426 usbd_xfer_set_stall(xfer); 1427 goto tr_setup; 1428 } 1429 } 1430 1431 static void 1432 uhso_ucom_cfg_get_status(struct ucom_softc *ucom, uint8_t *lsr, uint8_t *msr) 1433 { 1434 struct uhso_softc *sc = ucom->sc_parent; 1435 1436 *lsr = sc->sc_lsr; 1437 *msr = sc->sc_msr; 1438 } 1439 1440 static void 1441 uhso_ucom_cfg_set_dtr(struct ucom_softc *ucom, uint8_t onoff) 1442 { 1443 struct uhso_softc *sc = ucom->sc_parent; 1444 1445 if (!(UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_BULK)) 1446 return; 1447 1448 if (onoff) 1449 sc->sc_line |= UCDC_LINE_DTR; 1450 else 1451 sc->sc_line &= ~UCDC_LINE_DTR; 1452 1453 uhso_bs_cfg(sc); 1454 } 1455 1456 static void 1457 uhso_ucom_cfg_set_rts(struct ucom_softc *ucom, uint8_t onoff) 1458 { 1459 struct uhso_softc *sc = ucom->sc_parent; 1460 1461 if (!(UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_BULK)) 1462 return; 1463 1464 if (onoff) 1465 sc->sc_line |= UCDC_LINE_RTS; 1466 else 1467 sc->sc_line &= ~UCDC_LINE_RTS; 1468 1469 uhso_bs_cfg(sc); 1470 } 1471 1472 static void 1473 uhso_ucom_start_read(struct ucom_softc *ucom) 1474 { 1475 struct uhso_softc *sc = ucom->sc_parent; 1476 1477 UHSO_DPRINTF(3, "unit=%d, subunit=%d\n", 1478 ucom->sc_super->sc_unit, ucom->sc_subunit); 1479 1480 if (UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_MUX) { 1481 sc->sc_tty[ucom->sc_subunit].ht_open = 1; 1482 usbd_transfer_start(sc->sc_xfer[UHSO_MUX_ENDPT_INTR]); 1483 } 1484 else if (UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_BULK) { 1485 sc->sc_tty[0].ht_open = 1; 1486 usbd_transfer_start(sc->sc_xfer[UHSO_BULK_ENDPT_READ]); 1487 if (sc->sc_xfer[UHSO_BULK_ENDPT_INTR] != NULL) 1488 usbd_transfer_start(sc->sc_xfer[UHSO_BULK_ENDPT_INTR]); 1489 } 1490 } 1491 1492 static void 1493 uhso_ucom_stop_read(struct ucom_softc *ucom) 1494 { 1495 1496 struct uhso_softc *sc = ucom->sc_parent; 1497 1498 if (UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_MUX) { 1499 sc->sc_tty[ucom->sc_subunit].ht_open = 0; 1500 usbd_transfer_stop( 1501 sc->sc_tty[ucom->sc_subunit].ht_xfer[UHSO_CTRL_READ]); 1502 } 1503 else if (UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_BULK) { 1504 sc->sc_tty[0].ht_open = 0; 1505 usbd_transfer_start(sc->sc_xfer[UHSO_BULK_ENDPT_READ]); 1506 if (sc->sc_xfer[UHSO_BULK_ENDPT_INTR] != NULL) 1507 usbd_transfer_stop(sc->sc_xfer[UHSO_BULK_ENDPT_INTR]); 1508 } 1509 } 1510 1511 static void 1512 uhso_ucom_start_write(struct ucom_softc *ucom) 1513 { 1514 struct uhso_softc *sc = ucom->sc_parent; 1515 1516 if (UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_MUX) { 1517 UHSO_DPRINTF(3, "local unit %d\n", ucom->sc_subunit); 1518 1519 usbd_transfer_start(sc->sc_xfer[UHSO_MUX_ENDPT_INTR]); 1520 1521 usbd_xfer_set_priv( 1522 sc->sc_tty[ucom->sc_subunit].ht_xfer[UHSO_CTRL_WRITE], 1523 &sc->sc_tty[ucom->sc_subunit]); 1524 usbd_transfer_start( 1525 sc->sc_tty[ucom->sc_subunit].ht_xfer[UHSO_CTRL_WRITE]); 1526 1527 } 1528 else if (UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_BULK) { 1529 usbd_transfer_start(sc->sc_xfer[UHSO_BULK_ENDPT_WRITE]); 1530 } 1531 } 1532 1533 static void 1534 uhso_ucom_stop_write(struct ucom_softc *ucom) 1535 { 1536 struct uhso_softc *sc = ucom->sc_parent; 1537 1538 if (UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_MUX) { 1539 usbd_transfer_stop( 1540 sc->sc_tty[ucom->sc_subunit].ht_xfer[UHSO_CTRL_WRITE]); 1541 } 1542 else if (UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_BULK) { 1543 usbd_transfer_stop(sc->sc_xfer[UHSO_BULK_ENDPT_WRITE]); 1544 } 1545 } 1546 1547 static int 1548 uhso_attach_ifnet(struct uhso_softc *sc, struct usb_interface *iface, int type) 1549 { 1550 struct ifnet *ifp; 1551 usb_error_t uerr; 1552 struct sysctl_ctx_list *sctx; 1553 struct sysctl_oid *soid; 1554 unsigned int devunit; 1555 1556 uerr = usbd_transfer_setup(sc->sc_udev, 1557 &iface->idesc->bInterfaceNumber, sc->sc_if_xfer, 1558 uhso_ifnet_config, UHSO_IFNET_MAX, sc, &sc->sc_mtx); 1559 if (uerr) { 1560 UHSO_DPRINTF(0, "usbd_transfer_setup failed: %s\n", 1561 usbd_errstr(uerr)); 1562 return (-1); 1563 } 1564 1565 sc->sc_ifp = ifp = if_alloc(IFT_OTHER); 1566 if (sc->sc_ifp == NULL) { 1567 device_printf(sc->sc_dev, "if_alloc() failed\n"); 1568 return (-1); 1569 } 1570 1571 callout_init_mtx(&sc->sc_c, &sc->sc_mtx, 0); 1572 mtx_lock(&sc->sc_mtx); 1573 callout_reset(&sc->sc_c, 1, uhso_if_rxflush, sc); 1574 mtx_unlock(&sc->sc_mtx); 1575 1576 /* 1577 * We create our own unit numbers for ifnet devices because the 1578 * USB interface unit numbers can be at arbitrary positions yielding 1579 * odd looking device names. 1580 */ 1581 devunit = alloc_unr(uhso_ifnet_unit); 1582 1583 if_initname(ifp, device_get_name(sc->sc_dev), devunit); 1584 ifp->if_mtu = UHSO_MAX_MTU; 1585 ifp->if_ioctl = uhso_if_ioctl; 1586 ifp->if_init = uhso_if_init; 1587 ifp->if_start = uhso_if_start; 1588 ifp->if_output = uhso_if_output; 1589 ifp->if_flags = IFF_BROADCAST | IFF_MULTICAST | IFF_NOARP; 1590 ifp->if_softc = sc; 1591 IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen); 1592 ifp->if_snd.ifq_drv_maxlen = ifqmaxlen; 1593 IFQ_SET_READY(&ifp->if_snd); 1594 1595 if_attach(ifp); 1596 bpfattach(ifp, DLT_RAW, 0); 1597 1598 sctx = device_get_sysctl_ctx(sc->sc_dev); 1599 soid = device_get_sysctl_tree(sc->sc_dev); 1600 /* Unlocked read... */ 1601 SYSCTL_ADD_STRING(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "netif", 1602 CTLFLAG_RD, ifp->if_xname, 0, "Attached network interface"); 1603 1604 return (0); 1605 } 1606 1607 static void 1608 uhso_ifnet_read_callback(struct usb_xfer *xfer, usb_error_t error) 1609 { 1610 struct uhso_softc *sc = usbd_xfer_softc(xfer); 1611 struct mbuf *m; 1612 struct usb_page_cache *pc; 1613 int actlen; 1614 1615 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL); 1616 1617 UHSO_DPRINTF(3, "status=%d, actlen=%d\n", USB_GET_STATE(xfer), actlen); 1618 1619 switch (USB_GET_STATE(xfer)) { 1620 case USB_ST_TRANSFERRED: 1621 if (actlen > 0 && (sc->sc_ifp->if_drv_flags & IFF_DRV_RUNNING)) { 1622 pc = usbd_xfer_get_frame(xfer, 0); 1623 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR); 1624 usbd_copy_out(pc, 0, mtod(m, uint8_t *), actlen); 1625 m->m_pkthdr.len = m->m_len = actlen; 1626 /* Enqueue frame for further processing */ 1627 _IF_ENQUEUE(&sc->sc_rxq, m); 1628 if (!callout_pending(&sc->sc_c) || 1629 !callout_active(&sc->sc_c)) { 1630 callout_schedule(&sc->sc_c, 1); 1631 } 1632 } 1633 /* FALLTHROUGH */ 1634 case USB_ST_SETUP: 1635 tr_setup: 1636 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer)); 1637 usbd_transfer_submit(xfer); 1638 break; 1639 default: 1640 UHSO_DPRINTF(0, "error: %s\n", usbd_errstr(error)); 1641 if (error == USB_ERR_CANCELLED) 1642 break; 1643 usbd_xfer_set_stall(xfer); 1644 goto tr_setup; 1645 } 1646 } 1647 1648 /* 1649 * Deferred RX processing, called with mutex locked. 1650 * 1651 * Each frame we receive might contain several small ip-packets as well 1652 * as partial ip-packets. We need to separate/assemble them into individual 1653 * packets before sending them to the ip-layer. 1654 */ 1655 static void 1656 uhso_if_rxflush(void *arg) 1657 { 1658 struct uhso_softc *sc = arg; 1659 struct ifnet *ifp = sc->sc_ifp; 1660 uint8_t *cp; 1661 struct mbuf *m, *m0, *mwait; 1662 struct ip *ip; 1663 #ifdef INET6 1664 struct ip6_hdr *ip6; 1665 #endif 1666 uint16_t iplen; 1667 int len, isr; 1668 1669 m = NULL; 1670 mwait = sc->sc_mwait; 1671 for (;;) { 1672 if (m == NULL) { 1673 _IF_DEQUEUE(&sc->sc_rxq, m); 1674 if (m == NULL) 1675 break; 1676 UHSO_DPRINTF(3, "dequeue m=%p, len=%d\n", m, m->m_len); 1677 } 1678 mtx_unlock(&sc->sc_mtx); 1679 1680 /* Do we have a partial packet waiting? */ 1681 if (mwait != NULL) { 1682 m0 = mwait; 1683 mwait = NULL; 1684 1685 UHSO_DPRINTF(3, "partial m0=%p(%d), concat w/ m=%p(%d)\n", 1686 m0, m0->m_len, m, m->m_len); 1687 len = m->m_len + m0->m_len; 1688 1689 /* Concat mbufs and fix headers */ 1690 m_cat(m0, m); 1691 m0->m_pkthdr.len = len; 1692 m->m_flags &= ~M_PKTHDR; 1693 1694 m = m_pullup(m0, sizeof(struct ip)); 1695 if (m == NULL) { 1696 ifp->if_ierrors++; 1697 UHSO_DPRINTF(0, "m_pullup failed\n"); 1698 mtx_lock(&sc->sc_mtx); 1699 continue; 1700 } 1701 UHSO_DPRINTF(3, "Constructed mbuf=%p, len=%d\n", 1702 m, m->m_pkthdr.len); 1703 } 1704 1705 cp = mtod(m, uint8_t *); 1706 ip = (struct ip *)cp; 1707 #ifdef INET6 1708 ip6 = (struct ip6_hdr *)cp; 1709 #endif 1710 1711 /* Check for IPv4 */ 1712 if (ip->ip_v == IPVERSION) { 1713 iplen = htons(ip->ip_len); 1714 isr = NETISR_IP; 1715 } 1716 #ifdef INET6 1717 /* Check for IPv6 */ 1718 else if ((ip6->ip6_vfc & IPV6_VERSION_MASK) == IPV6_VERSION) { 1719 iplen = htons(ip6->ip6_plen); 1720 isr = NETISR_IPV6; 1721 } 1722 #endif 1723 else { 1724 UHSO_DPRINTF(0, "got unexpected ip version %d, " 1725 "m=%p, len=%d\n", (*cp & 0xf0) >> 4, m, m->m_len); 1726 ifp->if_ierrors++; 1727 UHSO_HEXDUMP(cp, 4); 1728 m_freem(m); 1729 m = NULL; 1730 mtx_lock(&sc->sc_mtx); 1731 continue; 1732 } 1733 1734 if (iplen == 0) { 1735 UHSO_DPRINTF(0, "Zero IP length\n"); 1736 ifp->if_ierrors++; 1737 m_freem(m); 1738 m = NULL; 1739 mtx_lock(&sc->sc_mtx); 1740 continue; 1741 } 1742 1743 UHSO_DPRINTF(3, "m=%p, len=%d, cp=%p, iplen=%d\n", 1744 m, m->m_pkthdr.len, cp, iplen); 1745 1746 m0 = NULL; 1747 1748 /* More IP packets in this mbuf */ 1749 if (iplen < m->m_pkthdr.len) { 1750 m0 = m; 1751 1752 /* 1753 * Allocate a new mbuf for this IP packet and 1754 * copy the IP-packet into it. 1755 */ 1756 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR); 1757 memcpy(mtod(m, uint8_t *), mtod(m0, uint8_t *), iplen); 1758 m->m_pkthdr.len = m->m_len = iplen; 1759 1760 /* Adjust the size of the original mbuf */ 1761 m_adj(m0, iplen); 1762 m0 = m_defrag(m0, M_WAITOK); 1763 1764 UHSO_DPRINTF(3, "New mbuf=%p, len=%d/%d, m0=%p, " 1765 "m0_len=%d/%d\n", m, m->m_pkthdr.len, m->m_len, 1766 m0, m0->m_pkthdr.len, m0->m_len); 1767 } 1768 else if (iplen > m->m_pkthdr.len) { 1769 UHSO_DPRINTF(3, "Deferred mbuf=%p, len=%d\n", 1770 m, m->m_pkthdr.len); 1771 mwait = m; 1772 m = NULL; 1773 mtx_lock(&sc->sc_mtx); 1774 continue; 1775 } 1776 1777 ifp->if_ipackets++; 1778 m->m_pkthdr.rcvif = ifp; 1779 1780 /* Dispatch to IP layer */ 1781 BPF_MTAP(sc->sc_ifp, m); 1782 M_SETFIB(m, ifp->if_fib); 1783 netisr_dispatch(isr, m); 1784 m = m0 != NULL ? m0 : NULL; 1785 mtx_lock(&sc->sc_mtx); 1786 } 1787 sc->sc_mwait = mwait; 1788 } 1789 1790 static void 1791 uhso_ifnet_write_callback(struct usb_xfer *xfer, usb_error_t error) 1792 { 1793 struct uhso_softc *sc = usbd_xfer_softc(xfer); 1794 struct ifnet *ifp = sc->sc_ifp; 1795 struct usb_page_cache *pc; 1796 struct mbuf *m; 1797 int actlen; 1798 1799 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL); 1800 1801 UHSO_DPRINTF(3, "status %d, actlen=%d\n", USB_GET_STATE(xfer), actlen); 1802 1803 switch (USB_GET_STATE(xfer)) { 1804 case USB_ST_TRANSFERRED: 1805 ifp->if_opackets++; 1806 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 1807 case USB_ST_SETUP: 1808 tr_setup: 1809 IFQ_DRV_DEQUEUE(&ifp->if_snd, m); 1810 if (m == NULL) 1811 break; 1812 1813 ifp->if_drv_flags |= IFF_DRV_OACTIVE; 1814 1815 if (m->m_pkthdr.len > MCLBYTES) 1816 m->m_pkthdr.len = MCLBYTES; 1817 1818 usbd_xfer_set_frame_len(xfer, 0, m->m_pkthdr.len); 1819 pc = usbd_xfer_get_frame(xfer, 0); 1820 usbd_m_copy_in(pc, 0, m, 0, m->m_pkthdr.len); 1821 usbd_transfer_submit(xfer); 1822 1823 BPF_MTAP(ifp, m); 1824 m_freem(m); 1825 break; 1826 default: 1827 UHSO_DPRINTF(0, "error: %s\n", usbd_errstr(error)); 1828 if (error == USB_ERR_CANCELLED) 1829 break; 1830 usbd_xfer_set_stall(xfer); 1831 goto tr_setup; 1832 } 1833 } 1834 1835 static int 1836 uhso_if_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 1837 { 1838 struct uhso_softc *sc; 1839 1840 sc = ifp->if_softc; 1841 1842 switch (cmd) { 1843 case SIOCSIFFLAGS: 1844 if (ifp->if_flags & IFF_UP) { 1845 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) { 1846 uhso_if_init(sc); 1847 } 1848 } 1849 else { 1850 if (ifp->if_drv_flags & IFF_DRV_RUNNING) { 1851 mtx_lock(&sc->sc_mtx); 1852 uhso_if_stop(sc); 1853 mtx_unlock(&sc->sc_mtx); 1854 } 1855 } 1856 break; 1857 case SIOCSIFADDR: 1858 case SIOCADDMULTI: 1859 case SIOCDELMULTI: 1860 break; 1861 default: 1862 return (EINVAL); 1863 } 1864 return (0); 1865 } 1866 1867 static void 1868 uhso_if_init(void *priv) 1869 { 1870 struct uhso_softc *sc = priv; 1871 struct ifnet *ifp = sc->sc_ifp; 1872 1873 mtx_lock(&sc->sc_mtx); 1874 uhso_if_stop(sc); 1875 ifp = sc->sc_ifp; 1876 ifp->if_flags |= IFF_UP; 1877 ifp->if_drv_flags |= IFF_DRV_RUNNING; 1878 mtx_unlock(&sc->sc_mtx); 1879 1880 UHSO_DPRINTF(2, "ifnet initialized\n"); 1881 } 1882 1883 static int 1884 uhso_if_output(struct ifnet *ifp, struct mbuf *m0, const struct sockaddr *dst, 1885 struct route *ro) 1886 { 1887 int error; 1888 1889 /* Only IPv4/6 support */ 1890 if (dst->sa_family != AF_INET 1891 #ifdef INET6 1892 && dst->sa_family != AF_INET6 1893 #endif 1894 ) { 1895 return (EAFNOSUPPORT); 1896 } 1897 1898 error = (ifp->if_transmit)(ifp, m0); 1899 if (error) { 1900 ifp->if_oerrors++; 1901 return (ENOBUFS); 1902 } 1903 ifp->if_opackets++; 1904 return (0); 1905 } 1906 1907 static void 1908 uhso_if_start(struct ifnet *ifp) 1909 { 1910 struct uhso_softc *sc = ifp->if_softc; 1911 1912 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) { 1913 UHSO_DPRINTF(1, "Not running\n"); 1914 return; 1915 } 1916 1917 mtx_lock(&sc->sc_mtx); 1918 usbd_transfer_start(sc->sc_if_xfer[UHSO_IFNET_READ]); 1919 usbd_transfer_start(sc->sc_if_xfer[UHSO_IFNET_WRITE]); 1920 mtx_unlock(&sc->sc_mtx); 1921 UHSO_DPRINTF(3, "interface started\n"); 1922 } 1923 1924 static void 1925 uhso_if_stop(struct uhso_softc *sc) 1926 { 1927 1928 usbd_transfer_stop(sc->sc_if_xfer[UHSO_IFNET_READ]); 1929 usbd_transfer_stop(sc->sc_if_xfer[UHSO_IFNET_WRITE]); 1930 sc->sc_ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE); 1931 } 1932