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