uhso.c revision 207554
1202181Sthompsa/*- 2202243Sthompsa * Copyright (c) 2009 Fredrik Lindberg <fli@shapeshifter.se> 3202181Sthompsa * All rights reserved. 4202181Sthompsa * 5202181Sthompsa * Redistribution and use in source and binary forms, with or without 6202181Sthompsa * modification, are permitted provided that the following conditions 7202181Sthompsa * are met: 8202181Sthompsa * 1. Redistributions of source code must retain the above copyright 9202181Sthompsa * notice, this list of conditions and the following disclaimer. 10202181Sthompsa * 2. Redistributions in binary form must reproduce the above copyright 11202181Sthompsa * notice, this list of conditions and the following disclaimer in the 12202181Sthompsa * documentation and/or other materials provided with the distribution. 13202181Sthompsa * 14202181Sthompsa * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 15202181Sthompsa * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 16202181Sthompsa * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 17202181Sthompsa * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 18202181Sthompsa * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 19202181Sthompsa * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 20202181Sthompsa * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 21202181Sthompsa * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22202181Sthompsa * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 23202181Sthompsa * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24202181Sthompsa * 25202181Sthompsa */ 26202181Sthompsa#include <sys/cdefs.h> 27202181Sthompsa__FBSDID("$FreeBSD: head/sys/dev/usb/net/uhso.c 207554 2010-05-03 07:32:50Z sobomax $"); 28202181Sthompsa 29202181Sthompsa#include <sys/param.h> 30202181Sthompsa#include <sys/types.h> 31202181Sthompsa#include <sys/sockio.h> 32202181Sthompsa#include <sys/mbuf.h> 33202181Sthompsa#include <sys/malloc.h> 34202181Sthompsa#include <sys/kernel.h> 35202181Sthompsa#include <sys/module.h> 36202181Sthompsa#include <sys/socket.h> 37202181Sthompsa#include <sys/tty.h> 38202181Sthompsa#include <sys/sysctl.h> 39202181Sthompsa#include <sys/condvar.h> 40202181Sthompsa#include <sys/sx.h> 41202181Sthompsa#include <sys/proc.h> 42202181Sthompsa#include <sys/conf.h> 43202181Sthompsa#include <sys/bus.h> 44202181Sthompsa#include <sys/systm.h> 45202181Sthompsa 46202181Sthompsa#include <machine/bus.h> 47202181Sthompsa 48202181Sthompsa#include <net/if.h> 49202181Sthompsa#include <net/if_types.h> 50202181Sthompsa#include <net/netisr.h> 51202181Sthompsa#include <net/bpf.h> 52202181Sthompsa#include <netinet/in.h> 53202181Sthompsa#include <netinet/ip.h> 54202181Sthompsa#include <netinet/ip6.h> 55202181Sthompsa 56202181Sthompsa#include <dev/usb/usb.h> 57202181Sthompsa#include <dev/usb/usbdi.h> 58202181Sthompsa#include <dev/usb/usbdi_util.h> 59202181Sthompsa#include <dev/usb/usb_cdc.h> 60202181Sthompsa#include "usbdevs.h" 61202181Sthompsa#define USB_DEBUG_VAR uhso_debug 62202181Sthompsa#include <dev/usb/usb_debug.h> 63202181Sthompsa#include <dev/usb/usb_process.h> 64202181Sthompsa#include <dev/usb/usb_device.h> 65202181Sthompsa#include <dev/usb/usb_busdma.h> 66202243Sthompsa#include <dev/usb/usb_controller.h> 67202243Sthompsa#include <dev/usb/usb_bus.h> 68202181Sthompsa#include <dev/usb/serial/usb_serial.h> 69202181Sthompsa#include <dev/usb/usb_msctest.h> 70202181Sthompsa 71202181Sthompsastruct uhso_tty { 72202181Sthompsa struct uhso_softc *ht_sc; 73202181Sthompsa struct usb_xfer *ht_xfer[3]; 74202243Sthompsa int ht_muxport; /* Mux. port no */ 75202181Sthompsa int ht_open; 76202181Sthompsa char ht_name[32]; 77202181Sthompsa}; 78202181Sthompsa 79202181Sthompsastruct uhso_softc { 80202181Sthompsa device_t sc_dev; 81202181Sthompsa struct usb_device *sc_udev; 82202181Sthompsa struct mtx sc_mtx; 83202243Sthompsa uint32_t sc_type; /* Interface definition */ 84202181Sthompsa 85202181Sthompsa struct usb_xfer *sc_xfer[3]; 86202181Sthompsa uint8_t sc_iface_no; 87202181Sthompsa uint8_t sc_iface_index; 88202181Sthompsa 89202181Sthompsa /* Control pipe */ 90202181Sthompsa struct usb_xfer * sc_ctrl_xfer[2]; 91202181Sthompsa uint8_t sc_ctrl_iface_no; 92202181Sthompsa 93202181Sthompsa /* Network */ 94202181Sthompsa struct usb_xfer *sc_if_xfer[2]; 95202181Sthompsa struct ifnet *sc_ifp; 96202243Sthompsa struct mbuf *sc_mwait; /* Partial packet */ 97202243Sthompsa size_t sc_waitlen; /* No. of outstanding bytes */ 98202181Sthompsa struct ifqueue sc_rxq; 99202181Sthompsa struct callout sc_c; 100202181Sthompsa 101202181Sthompsa /* TTY related structures */ 102202181Sthompsa struct ucom_super_softc sc_super_ucom; 103202243Sthompsa int sc_ttys; 104202181Sthompsa struct uhso_tty *sc_tty; 105202181Sthompsa struct ucom_softc *sc_ucom; 106202181Sthompsa int sc_msr; 107202181Sthompsa int sc_lsr; 108202181Sthompsa int sc_line; 109202181Sthompsa}; 110202181Sthompsa 111202181Sthompsa#define UHSO_MAX_MTU 2048 112202181Sthompsa 113202181Sthompsa/* 114202181Sthompsa * There are mainly two type of cards floating around. 115202181Sthompsa * The first one has 2,3 or 4 interfaces with a multiplexed serial port 116202181Sthompsa * and packet interface on the first interface and bulk serial ports 117202181Sthompsa * on the others. 118202181Sthompsa * The second type of card has several other interfaces, their purpose 119202181Sthompsa * can be detected during run-time. 120202181Sthompsa */ 121202181Sthompsa#define UHSO_IFACE_SPEC(usb_type, port, port_type) \ 122202181Sthompsa (((usb_type) << 24) | ((port) << 16) | (port_type)) 123202181Sthompsa 124202181Sthompsa#define UHSO_IFACE_USB_TYPE(x) ((x >> 24) & 0xff) 125202181Sthompsa#define UHSO_IFACE_PORT(x) ((x >> 16) & 0xff) 126202181Sthompsa#define UHSO_IFACE_PORT_TYPE(x) (x & 0xff) 127202181Sthompsa 128202181Sthompsa/* 129202181Sthompsa * USB interface types 130202181Sthompsa */ 131202181Sthompsa#define UHSO_IF_NET 0x01 /* Network packet interface */ 132202181Sthompsa#define UHSO_IF_MUX 0x02 /* Multiplexed serial port */ 133202181Sthompsa#define UHSO_IF_BULK 0x04 /* Bulk interface */ 134202181Sthompsa 135202181Sthompsa/* 136202181Sthompsa * Port types 137202181Sthompsa */ 138202181Sthompsa#define UHSO_PORT_UNKNOWN 0x00 139202243Sthompsa#define UHSO_PORT_SERIAL 0x01 /* Serial port */ 140202181Sthompsa#define UHSO_PORT_NETWORK 0x02 /* Network packet interface */ 141202181Sthompsa 142202181Sthompsa/* 143202181Sthompsa * Multiplexed serial port destination sub-port names 144202181Sthompsa */ 145202181Sthompsa#define UHSO_MPORT_TYPE_CTL 0x00 /* Control port */ 146202181Sthompsa#define UHSO_MPORT_TYPE_APP 0x01 /* Application */ 147202181Sthompsa#define UHSO_MPORT_TYPE_PCSC 0x02 148202181Sthompsa#define UHSO_MPORT_TYPE_GPS 0x03 149202243Sthompsa#define UHSO_MPORT_TYPE_APP2 0x04 /* Secondary application */ 150202181Sthompsa#define UHSO_MPORT_TYPE_MAX UHSO_MPORT_TYPE_APP2 151202181Sthompsa#define UHSO_MPORT_TYPE_NOMAX 8 /* Max number of mux ports */ 152202181Sthompsa 153202181Sthompsa/* 154202181Sthompsa * Port definitions 155202270Sthompsa * Note that these definitions are arbitrary and do not match the values 156202270Sthompsa * returned by the auto config descriptor. 157202181Sthompsa */ 158202181Sthompsa#define UHSO_PORT_TYPE_CTL 0x01 159202181Sthompsa#define UHSO_PORT_TYPE_APP 0x02 160202181Sthompsa#define UHSO_PORT_TYPE_APP2 0x03 161202181Sthompsa#define UHSO_PORT_TYPE_MODEM 0x04 162202181Sthompsa#define UHSO_PORT_TYPE_NETWORK 0x05 163202181Sthompsa#define UHSO_PORT_TYPE_DIAG 0x06 164202181Sthompsa#define UHSO_PORT_TYPE_DIAG2 0x07 165202181Sthompsa#define UHSO_PORT_TYPE_GPS 0x08 166202181Sthompsa#define UHSO_PORT_TYPE_GPSCTL 0x09 167202181Sthompsa#define UHSO_PORT_TYPE_PCSC 0x0a 168202181Sthompsa#define UHSO_PORT_TYPE_MSD 0x0b 169202181Sthompsa#define UHSO_PORT_TYPE_VOICE 0x0c 170202181Sthompsa#define UHSO_PORT_TYPE_MAX 0x0c 171202181Sthompsa 172202181Sthompsastatic eventhandler_tag uhso_etag; 173202181Sthompsa 174202181Sthompsa/* Overall port type */ 175202181Sthompsastatic char *uhso_port[] = { 176202181Sthompsa "Unknown", 177202181Sthompsa "Serial", 178202181Sthompsa "Network", 179202181Sthompsa "Network/Serial" 180202181Sthompsa}; 181202181Sthompsa 182202243Sthompsa/* 183202243Sthompsa * Map between interface port type read from device and description type. 184202243Sthompsa * The position in this array is a direct map to the auto config 185202243Sthompsa * descriptor values. 186202243Sthompsa */ 187202243Sthompsastatic unsigned char uhso_port_map[] = { 188202181Sthompsa 0, 189202181Sthompsa UHSO_PORT_TYPE_DIAG, 190202181Sthompsa UHSO_PORT_TYPE_GPS, 191202181Sthompsa UHSO_PORT_TYPE_GPSCTL, 192202181Sthompsa UHSO_PORT_TYPE_APP, 193202181Sthompsa UHSO_PORT_TYPE_APP2, 194202181Sthompsa UHSO_PORT_TYPE_CTL, 195202181Sthompsa UHSO_PORT_TYPE_NETWORK, 196202181Sthompsa UHSO_PORT_TYPE_MODEM, 197202181Sthompsa UHSO_PORT_TYPE_MSD, 198202181Sthompsa UHSO_PORT_TYPE_PCSC, 199202181Sthompsa UHSO_PORT_TYPE_VOICE 200202181Sthompsa}; 201202181Sthompsastatic char uhso_port_map_max = sizeof(uhso_port_map) / sizeof(char); 202202181Sthompsa 203202243Sthompsastatic unsigned char uhso_mux_port_map[] = { 204202181Sthompsa UHSO_PORT_TYPE_CTL, 205202181Sthompsa UHSO_PORT_TYPE_APP, 206202181Sthompsa UHSO_PORT_TYPE_PCSC, 207202181Sthompsa UHSO_PORT_TYPE_GPS, 208202181Sthompsa UHSO_PORT_TYPE_APP2 209202181Sthompsa}; 210202181Sthompsa 211202181Sthompsastatic char *uhso_port_type[] = { 212202243Sthompsa "Unknown", /* Not a valid port */ 213202181Sthompsa "Control", 214202181Sthompsa "Application", 215202181Sthompsa "Application (Secondary)", 216202181Sthompsa "Modem", 217202181Sthompsa "Network", 218202181Sthompsa "Diagnostic", 219202181Sthompsa "Diagnostic (Secondary)", 220202181Sthompsa "GPS", 221202181Sthompsa "GPS Control", 222202181Sthompsa "PC Smartcard", 223202181Sthompsa "MSD", 224202181Sthompsa "Voice", 225202181Sthompsa}; 226202181Sthompsa 227202181Sthompsastatic char *uhso_port_type_sysctl[] = { 228202181Sthompsa "unknown", 229202181Sthompsa "control", 230202181Sthompsa "application", 231202181Sthompsa "application", 232202181Sthompsa "modem", 233202181Sthompsa "network", 234202181Sthompsa "diagnostic", 235202181Sthompsa "diagnostic", 236202181Sthompsa "gps", 237202181Sthompsa "gps_control", 238202181Sthompsa "pcsc", 239202181Sthompsa "msd", 240202181Sthompsa "voice", 241202181Sthompsa}; 242202181Sthompsa 243202181Sthompsa#define UHSO_STATIC_IFACE 0x01 244202181Sthompsa#define UHSO_AUTO_IFACE 0x02 245202181Sthompsa 246202181Sthompsastatic const struct usb_device_id uhso_devs[] = { 247202181Sthompsa#define UHSO_DEV(v,p,i) { USB_VPI(USB_VENDOR_##v, USB_PRODUCT_##v##_##p, i) } 248202181Sthompsa /* Option GlobeSurfer iCON 7.2 */ 249202181Sthompsa UHSO_DEV(OPTION, GSICON72, UHSO_STATIC_IFACE), 250202181Sthompsa /* Option iCON 225 */ 251202181Sthompsa UHSO_DEV(OPTION, GTHSDPA, UHSO_STATIC_IFACE), 252202181Sthompsa /* Option GlobeSurfer iCON HSUPA */ 253202181Sthompsa UHSO_DEV(OPTION, GSICONHSUPA, UHSO_STATIC_IFACE), 254202181Sthompsa /* Option GlobeTrotter HSUPA */ 255202181Sthompsa UHSO_DEV(OPTION, GTHSUPA, UHSO_STATIC_IFACE), 256202181Sthompsa /* GE40x */ 257202181Sthompsa UHSO_DEV(OPTION, GE40X, UHSO_AUTO_IFACE), 258202181Sthompsa UHSO_DEV(OPTION, GE40X_1, UHSO_AUTO_IFACE), 259202181Sthompsa UHSO_DEV(OPTION, GE40X_2, UHSO_AUTO_IFACE), 260202181Sthompsa UHSO_DEV(OPTION, GE40X_3, UHSO_AUTO_IFACE), 261202181Sthompsa /* Option GlobeSurfer iCON 401 */ 262202181Sthompsa UHSO_DEV(OPTION, ICON401, UHSO_AUTO_IFACE), 263202181Sthompsa /* Option GlobeTrotter Module 382 */ 264202181Sthompsa UHSO_DEV(OPTION, GMT382, UHSO_AUTO_IFACE), 265202181Sthompsa /* Option iCON EDGE */ 266202181Sthompsa UHSO_DEV(OPTION, ICONEDGE, UHSO_STATIC_IFACE), 267202181Sthompsa /* Option Module HSxPA */ 268202181Sthompsa UHSO_DEV(OPTION, MODHSXPA, UHSO_STATIC_IFACE), 269202181Sthompsa /* Option iCON 321 */ 270202181Sthompsa UHSO_DEV(OPTION, ICON321, UHSO_STATIC_IFACE), 271202181Sthompsa /* Option iCON 322 */ 272202243Sthompsa UHSO_DEV(OPTION, GTICON322, UHSO_STATIC_IFACE), 273202243Sthompsa /* Option iCON 505 */ 274202243Sthompsa UHSO_DEV(OPTION, ICON505, UHSO_AUTO_IFACE), 275202181Sthompsa#undef UHSO_DEV 276202181Sthompsa}; 277202181Sthompsa 278202181SthompsaSYSCTL_NODE(_hw_usb, OID_AUTO, uhso, CTLFLAG_RW, 0, "USB uhso"); 279202243Sthompsastatic int uhso_autoswitch = 1; 280202243SthompsaSYSCTL_INT(_hw_usb_uhso, OID_AUTO, auto_switch, CTLFLAG_RW, 281202243Sthompsa &uhso_autoswitch, 0, "Automatically switch to modem mode"); 282202181Sthompsa 283202181Sthompsa#ifdef USB_DEBUG 284202181Sthompsa#ifdef UHSO_DEBUG 285202181Sthompsastatic int uhso_debug = UHSO_DEBUG; 286202181Sthompsa#else 287202181Sthompsastatic int uhso_debug = -1; 288202181Sthompsa#endif 289202181Sthompsa 290202181SthompsaSYSCTL_INT(_hw_usb_uhso, OID_AUTO, debug, CTLFLAG_RW, 291202181Sthompsa &uhso_debug, 0, "Debug level"); 292202181Sthompsa 293202181Sthompsa#define UHSO_DPRINTF(n, x, ...) {\ 294202181Sthompsa if (uhso_debug >= n) {\ 295202181Sthompsa printf("%s: " x, __func__, ##__VA_ARGS__);\ 296202181Sthompsa }\ 297202181Sthompsa} 298202181Sthompsa#else 299202181Sthompsa#define UHSO_DPRINTF(n, x, ...) 300202181Sthompsa#endif 301202181Sthompsa 302202181Sthompsa#ifdef UHSO_DEBUG_HEXDUMP 303202181Sthompsa# define UHSO_HEXDUMP(_buf, _len) do { \ 304202181Sthompsa { \ 305202181Sthompsa size_t __tmp; \ 306202181Sthompsa const char *__buf = (const char *)_buf; \ 307202181Sthompsa for (__tmp = 0; __tmp < _len; __tmp++) \ 308202181Sthompsa printf("%02hhx ", *__buf++); \ 309202181Sthompsa printf("\n"); \ 310202181Sthompsa } \ 311202181Sthompsa} while(0) 312202181Sthompsa#else 313202181Sthompsa# define UHSO_HEXDUMP(_buf, _len) 314202181Sthompsa#endif 315202181Sthompsa 316202181Sthompsaenum { 317202181Sthompsa UHSO_MUX_ENDPT_INTR = 0, 318202181Sthompsa UHSO_MUX_ENDPT_MAX 319202181Sthompsa}; 320202181Sthompsa 321202181Sthompsaenum { 322202181Sthompsa UHSO_CTRL_READ = 0, 323202181Sthompsa UHSO_CTRL_WRITE, 324202181Sthompsa UHSO_CTRL_MAX 325202181Sthompsa}; 326202181Sthompsa 327202181Sthompsaenum { 328202181Sthompsa UHSO_IFNET_READ = 0, 329202181Sthompsa UHSO_IFNET_WRITE, 330202181Sthompsa UHSO_IFNET_MAX 331202181Sthompsa}; 332202181Sthompsa 333202181Sthompsaenum { 334202181Sthompsa UHSO_BULK_ENDPT_READ = 0, 335202181Sthompsa UHSO_BULK_ENDPT_WRITE, 336202181Sthompsa UHSO_BULK_ENDPT_INTR, 337202181Sthompsa UHSO_BULK_ENDPT_MAX 338202181Sthompsa}; 339202181Sthompsa 340202181Sthompsastatic usb_callback_t uhso_mux_intr_callback; 341202181Sthompsastatic usb_callback_t uhso_mux_read_callback; 342202181Sthompsastatic usb_callback_t uhso_mux_write_callback; 343202181Sthompsastatic usb_callback_t uhso_bs_read_callback; 344202181Sthompsastatic usb_callback_t uhso_bs_write_callback; 345202181Sthompsastatic usb_callback_t uhso_bs_intr_callback; 346202181Sthompsastatic usb_callback_t uhso_ifnet_read_callback; 347202181Sthompsastatic usb_callback_t uhso_ifnet_write_callback; 348202181Sthompsa 349202243Sthompsa/* Config used for the default control pipes */ 350202181Sthompsastatic const struct usb_config uhso_ctrl_config[UHSO_CTRL_MAX] = { 351202181Sthompsa [UHSO_CTRL_READ] = { 352202181Sthompsa .type = UE_CONTROL, 353202181Sthompsa .endpoint = 0x00, 354202181Sthompsa .direction = UE_DIR_ANY, 355202181Sthompsa .flags = { .pipe_bof = 1, .short_xfer_ok = 1 }, 356202181Sthompsa .bufsize = sizeof(struct usb_device_request) + 1024, 357202181Sthompsa .callback = &uhso_mux_read_callback 358202181Sthompsa }, 359202181Sthompsa 360202181Sthompsa [UHSO_CTRL_WRITE] = { 361202181Sthompsa .type = UE_CONTROL, 362202181Sthompsa .endpoint = 0x00, 363202181Sthompsa .direction = UE_DIR_ANY, 364202181Sthompsa .flags = { .pipe_bof = 1, .force_short_xfer = 1 }, 365202181Sthompsa .bufsize = sizeof(struct usb_device_request) + 1024, 366202181Sthompsa .timeout = 1000, 367202181Sthompsa .callback = &uhso_mux_write_callback 368202181Sthompsa } 369202181Sthompsa}; 370202181Sthompsa 371202243Sthompsa/* Config for the multiplexed serial ports */ 372202181Sthompsastatic const struct usb_config uhso_mux_config[UHSO_MUX_ENDPT_MAX] = { 373202181Sthompsa [UHSO_MUX_ENDPT_INTR] = { 374202181Sthompsa .type = UE_INTERRUPT, 375202181Sthompsa .endpoint = UE_ADDR_ANY, 376202181Sthompsa .direction = UE_DIR_IN, 377202181Sthompsa .flags = { .short_xfer_ok = 1 }, 378202181Sthompsa .bufsize = 0, 379202181Sthompsa .callback = &uhso_mux_intr_callback, 380202181Sthompsa } 381202181Sthompsa}; 382202181Sthompsa 383202243Sthompsa/* Config for the raw IP-packet interface */ 384202181Sthompsastatic const struct usb_config uhso_ifnet_config[UHSO_IFNET_MAX] = { 385202181Sthompsa [UHSO_IFNET_READ] = { 386202181Sthompsa .type = UE_BULK, 387202181Sthompsa .endpoint = UE_ADDR_ANY, 388202181Sthompsa .direction = UE_DIR_IN, 389202181Sthompsa .flags = { .pipe_bof = 1, .short_xfer_ok = 1 }, 390202181Sthompsa .bufsize = MCLBYTES, 391202181Sthompsa .callback = &uhso_ifnet_read_callback 392202181Sthompsa }, 393202181Sthompsa [UHSO_IFNET_WRITE] = { 394202181Sthompsa .type = UE_BULK, 395202181Sthompsa .endpoint = UE_ADDR_ANY, 396202181Sthompsa .direction = UE_DIR_OUT, 397202181Sthompsa .flags = { .pipe_bof = 1, .force_short_xfer = 1 }, 398202181Sthompsa .bufsize = MCLBYTES, 399202181Sthompsa .timeout = 5 * USB_MS_HZ, 400202181Sthompsa .callback = &uhso_ifnet_write_callback 401202181Sthompsa } 402202181Sthompsa}; 403202181Sthompsa 404202243Sthompsa/* Config for interfaces with normal bulk serial ports */ 405202181Sthompsastatic const struct usb_config uhso_bs_config[UHSO_BULK_ENDPT_MAX] = { 406202181Sthompsa [UHSO_BULK_ENDPT_READ] = { 407202181Sthompsa .type = UE_BULK, 408202181Sthompsa .endpoint = UE_ADDR_ANY, 409202181Sthompsa .direction = UE_DIR_IN, 410202181Sthompsa .flags = { .pipe_bof = 1, .short_xfer_ok = 1 }, 411202181Sthompsa .bufsize = 4096, 412202181Sthompsa .callback = &uhso_bs_read_callback 413202181Sthompsa }, 414202181Sthompsa 415202181Sthompsa [UHSO_BULK_ENDPT_WRITE] = { 416202181Sthompsa .type = UE_BULK, 417202181Sthompsa .endpoint = UE_ADDR_ANY, 418202181Sthompsa .direction = UE_DIR_OUT, 419202181Sthompsa .flags = { .pipe_bof = 1, .force_short_xfer = 1 }, 420202181Sthompsa .bufsize = 8192, 421202181Sthompsa .callback = &uhso_bs_write_callback 422202181Sthompsa }, 423202181Sthompsa 424202181Sthompsa [UHSO_BULK_ENDPT_INTR] = { 425202181Sthompsa .type = UE_INTERRUPT, 426202181Sthompsa .endpoint = UE_ADDR_ANY, 427202181Sthompsa .direction = UE_DIR_IN, 428202181Sthompsa .flags = { .short_xfer_ok = 1 }, 429202181Sthompsa .bufsize = 0, 430202181Sthompsa .callback = &uhso_bs_intr_callback, 431202181Sthompsa } 432202181Sthompsa}; 433202181Sthompsa 434202243Sthompsastatic int uhso_probe_iface(struct uhso_softc *, int, 435202181Sthompsa int (*probe)(struct uhso_softc *, int)); 436202243Sthompsastatic int uhso_probe_iface_auto(struct uhso_softc *, int); 437202243Sthompsastatic int uhso_probe_iface_static(struct uhso_softc *, int); 438202243Sthompsastatic int uhso_attach_muxserial(struct uhso_softc *, struct usb_interface *, 439202181Sthompsa int type); 440202243Sthompsastatic int uhso_attach_bulkserial(struct uhso_softc *, struct usb_interface *, 441202181Sthompsa int type); 442202243Sthompsastatic int uhso_attach_ifnet(struct uhso_softc *, struct usb_interface *, 443202181Sthompsa int type); 444202181Sthompsastatic void uhso_test_autoinst(void *, struct usb_device *, 445202181Sthompsa struct usb_attach_arg *); 446202243Sthompsastatic int uhso_driver_loaded(struct module *, int, void *); 447202181Sthompsa 448202181Sthompsastatic void uhso_ucom_start_read(struct ucom_softc *); 449202181Sthompsastatic void uhso_ucom_stop_read(struct ucom_softc *); 450202181Sthompsastatic void uhso_ucom_start_write(struct ucom_softc *); 451202181Sthompsastatic void uhso_ucom_stop_write(struct ucom_softc *); 452202181Sthompsastatic void uhso_ucom_cfg_get_status(struct ucom_softc *, uint8_t *, uint8_t *); 453202181Sthompsastatic void uhso_ucom_cfg_set_dtr(struct ucom_softc *, uint8_t); 454202181Sthompsastatic void uhso_ucom_cfg_set_rts(struct ucom_softc *, uint8_t); 455202181Sthompsastatic void uhso_if_init(void *); 456202181Sthompsastatic void uhso_if_start(struct ifnet *); 457202181Sthompsastatic void uhso_if_stop(struct uhso_softc *); 458202243Sthompsastatic int uhso_if_ioctl(struct ifnet *, u_long, caddr_t); 459202243Sthompsastatic int uhso_if_output(struct ifnet *, struct mbuf *, struct sockaddr *, 460202181Sthompsa struct route *); 461202181Sthompsastatic void uhso_if_rxflush(void *); 462202181Sthompsa 463202181Sthompsastatic device_probe_t uhso_probe; 464202181Sthompsastatic device_attach_t uhso_attach; 465202181Sthompsastatic device_detach_t uhso_detach; 466202181Sthompsa 467202181Sthompsastatic device_method_t uhso_methods[] = { 468202181Sthompsa DEVMETHOD(device_probe, uhso_probe), 469202181Sthompsa DEVMETHOD(device_attach, uhso_attach), 470202181Sthompsa DEVMETHOD(device_detach, uhso_detach), 471202181Sthompsa { 0, 0 } 472202181Sthompsa}; 473202181Sthompsa 474202181Sthompsastatic driver_t uhso_driver = { 475202181Sthompsa "uhso", 476202181Sthompsa uhso_methods, 477202181Sthompsa sizeof(struct uhso_softc) 478202181Sthompsa}; 479202181Sthompsa 480202181Sthompsastatic devclass_t uhso_devclass; 481202181SthompsaDRIVER_MODULE(uhso, uhub, uhso_driver, uhso_devclass, uhso_driver_loaded, 0); 482202181SthompsaMODULE_DEPEND(uhso, ucom, 1, 1, 1); 483202181SthompsaMODULE_DEPEND(uhso, usb, 1, 1, 1); 484202181SthompsaMODULE_VERSION(uhso, 1); 485202181Sthompsa 486202181Sthompsastatic struct ucom_callback uhso_ucom_callback = { 487202181Sthompsa .ucom_cfg_get_status = &uhso_ucom_cfg_get_status, 488202181Sthompsa .ucom_cfg_set_dtr = &uhso_ucom_cfg_set_dtr, 489202181Sthompsa .ucom_cfg_set_rts = &uhso_ucom_cfg_set_rts, 490202181Sthompsa .ucom_start_read = uhso_ucom_start_read, 491202181Sthompsa .ucom_stop_read = uhso_ucom_stop_read, 492202181Sthompsa .ucom_start_write = uhso_ucom_start_write, 493202181Sthompsa .ucom_stop_write = uhso_ucom_stop_write 494202181Sthompsa}; 495202181Sthompsa 496202181Sthompsastatic int 497202181Sthompsauhso_probe(device_t self) 498202181Sthompsa{ 499202181Sthompsa struct usb_attach_arg *uaa = device_get_ivars(self); 500202181Sthompsa 501202181Sthompsa if (uaa->usb_mode != USB_MODE_HOST) 502202181Sthompsa return (ENXIO); 503202181Sthompsa if (uaa->info.bConfigIndex != 0) 504202181Sthompsa return (ENXIO); 505202181Sthompsa if (uaa->device->ddesc.bDeviceClass != 0xff) 506202181Sthompsa return (ENXIO); 507202181Sthompsa 508202181Sthompsa return (usbd_lookup_id_by_uaa(uhso_devs, sizeof(uhso_devs), uaa)); 509202181Sthompsa} 510202181Sthompsa 511202181Sthompsastatic int 512202181Sthompsauhso_attach(device_t self) 513202181Sthompsa{ 514202181Sthompsa struct uhso_softc *sc = device_get_softc(self); 515202181Sthompsa struct usb_attach_arg *uaa = device_get_ivars(self); 516202181Sthompsa struct usb_config_descriptor *cd; 517202181Sthompsa struct usb_interface_descriptor *id; 518202181Sthompsa struct sysctl_ctx_list *sctx; 519202181Sthompsa struct sysctl_oid *soid; 520202181Sthompsa struct sysctl_oid *tree, *tty_node; 521202181Sthompsa struct ucom_softc *ucom; 522202181Sthompsa struct uhso_tty *ht; 523202181Sthompsa int i, error, port; 524202181Sthompsa void *probe_f; 525202181Sthompsa usb_error_t uerr; 526202181Sthompsa char *desc; 527202181Sthompsa 528202181Sthompsa sc->sc_dev = self; 529202181Sthompsa sc->sc_udev = uaa->device; 530202181Sthompsa mtx_init(&sc->sc_mtx, "uhso", NULL, MTX_DEF); 531202181Sthompsa 532202181Sthompsa sc->sc_ucom = NULL; 533202181Sthompsa sc->sc_ttys = 0; 534202181Sthompsa 535202181Sthompsa cd = usbd_get_config_descriptor(uaa->device); 536202181Sthompsa id = usbd_get_interface_descriptor(uaa->iface); 537202181Sthompsa sc->sc_ctrl_iface_no = id->bInterfaceNumber; 538202181Sthompsa 539202181Sthompsa sc->sc_iface_no = uaa->info.bIfaceNum; 540202181Sthompsa sc->sc_iface_index = uaa->info.bIfaceIndex; 541202181Sthompsa 542202181Sthompsa /* Setup control pipe */ 543202181Sthompsa uerr = usbd_transfer_setup(uaa->device, 544202181Sthompsa &sc->sc_iface_index, sc->sc_ctrl_xfer, 545202181Sthompsa uhso_ctrl_config, UHSO_CTRL_MAX, sc, &sc->sc_mtx); 546202181Sthompsa if (uerr) { 547202181Sthompsa device_printf(self, "Failed to setup control pipe: %s\n", 548202181Sthompsa usbd_errstr(uerr)); 549202181Sthompsa goto out; 550202181Sthompsa } 551202181Sthompsa 552202181Sthompsa if (USB_GET_DRIVER_INFO(uaa) == UHSO_STATIC_IFACE) 553202181Sthompsa probe_f = uhso_probe_iface_static; 554202181Sthompsa else if (USB_GET_DRIVER_INFO(uaa) == UHSO_AUTO_IFACE) 555202181Sthompsa probe_f = uhso_probe_iface_auto; 556202181Sthompsa else 557202181Sthompsa goto out; 558202181Sthompsa 559202181Sthompsa error = uhso_probe_iface(sc, uaa->info.bIfaceNum, probe_f); 560202181Sthompsa if (error != 0) 561202181Sthompsa goto out; 562202181Sthompsa 563202181Sthompsa sctx = device_get_sysctl_ctx(sc->sc_dev); 564202181Sthompsa soid = device_get_sysctl_tree(sc->sc_dev); 565202181Sthompsa 566202181Sthompsa SYSCTL_ADD_STRING(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "type", 567202181Sthompsa CTLFLAG_RD, uhso_port[UHSO_IFACE_PORT(sc->sc_type)], 0, 568202181Sthompsa "Port available at this interface"); 569202181Sthompsa 570202243Sthompsa /* 571202270Sthompsa * The default interface description on most Option devices isn't 572202243Sthompsa * very helpful. So we skip device_set_usb_desc and set the 573202243Sthompsa * device description manually. 574202243Sthompsa */ 575202243Sthompsa device_set_desc_copy(self, uhso_port_type[UHSO_IFACE_PORT_TYPE(sc->sc_type)]); 576202243Sthompsa /* Announce device */ 577202243Sthompsa device_printf(self, "<%s port> at <%s %s> on %s\n", 578202243Sthompsa uhso_port_type[UHSO_IFACE_PORT_TYPE(sc->sc_type)], 579202243Sthompsa uaa->device->manufacturer, uaa->device->product, 580202243Sthompsa device_get_nameunit(uaa->device->bus->bdev)); 581202243Sthompsa 582202181Sthompsa if (sc->sc_ttys > 0) { 583202181Sthompsa SYSCTL_ADD_INT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "ports", 584202181Sthompsa CTLFLAG_RD, &sc->sc_ttys, 0, "Number of attached serial ports"); 585202181Sthompsa 586202181Sthompsa tree = SYSCTL_ADD_NODE(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, 587202181Sthompsa "port", CTLFLAG_RD, NULL, "Serial ports"); 588202181Sthompsa } 589202181Sthompsa 590202243Sthompsa /* 591202243Sthompsa * Loop through the number of found TTYs and create sysctl 592202243Sthompsa * nodes for them. 593202243Sthompsa */ 594202181Sthompsa for (i = 0; i < sc->sc_ttys; i++) { 595202181Sthompsa ht = &sc->sc_tty[i]; 596202181Sthompsa ucom = &sc->sc_ucom[i]; 597202181Sthompsa 598202181Sthompsa if (UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_MUX) 599202181Sthompsa port = uhso_mux_port_map[ht->ht_muxport]; 600202181Sthompsa else 601202181Sthompsa port = UHSO_IFACE_PORT_TYPE(sc->sc_type); 602202181Sthompsa 603202181Sthompsa desc = uhso_port_type_sysctl[port]; 604202181Sthompsa 605202181Sthompsa tty_node = SYSCTL_ADD_NODE(sctx, SYSCTL_CHILDREN(tree), OID_AUTO, 606202181Sthompsa desc, CTLFLAG_RD, NULL, ""); 607202181Sthompsa 608202181Sthompsa ht->ht_name[0] = 0; 609202181Sthompsa if (sc->sc_ttys == 1) 610202181Sthompsa snprintf(ht->ht_name, 32, "cuaU%d", ucom->sc_unit); 611202181Sthompsa else { 612202181Sthompsa snprintf(ht->ht_name, 32, "cuaU%d.%d", 613202181Sthompsa ucom->sc_unit - ucom->sc_local_unit, 614202181Sthompsa ucom->sc_local_unit); 615202181Sthompsa } 616202181Sthompsa 617202181Sthompsa desc = uhso_port_type[port]; 618202181Sthompsa SYSCTL_ADD_STRING(sctx, SYSCTL_CHILDREN(tty_node), OID_AUTO, 619202181Sthompsa "tty", CTLFLAG_RD, ht->ht_name, 0, ""); 620202181Sthompsa SYSCTL_ADD_STRING(sctx, SYSCTL_CHILDREN(tty_node), OID_AUTO, 621202181Sthompsa "desc", CTLFLAG_RD, desc, 0, ""); 622202181Sthompsa 623202181Sthompsa if (bootverbose) 624202181Sthompsa device_printf(sc->sc_dev, 625202181Sthompsa "\"%s\" port at %s\n", desc, ht->ht_name); 626202181Sthompsa } 627202181Sthompsa 628202181Sthompsa return (0); 629202181Sthompsaout: 630202181Sthompsa uhso_detach(sc->sc_dev); 631202181Sthompsa return (ENXIO); 632202181Sthompsa} 633202181Sthompsa 634202181Sthompsastatic int 635202181Sthompsauhso_detach(device_t self) 636202181Sthompsa{ 637202181Sthompsa struct uhso_softc *sc = device_get_softc(self); 638202181Sthompsa int i; 639202181Sthompsa 640202181Sthompsa usbd_transfer_unsetup(sc->sc_xfer, 3); 641202181Sthompsa usbd_transfer_unsetup(sc->sc_ctrl_xfer, UHSO_CTRL_MAX); 642202181Sthompsa if (sc->sc_ttys > 0) { 643202181Sthompsa ucom_detach(&sc->sc_super_ucom, sc->sc_ucom, sc->sc_ttys); 644202181Sthompsa 645202181Sthompsa for (i = 0; i < sc->sc_ttys; i++) { 646202181Sthompsa if (sc->sc_tty[i].ht_muxport != -1) { 647202181Sthompsa usbd_transfer_unsetup(sc->sc_tty[i].ht_xfer, 648202181Sthompsa UHSO_CTRL_MAX); 649202181Sthompsa } 650202181Sthompsa } 651202181Sthompsa 652202181Sthompsa free(sc->sc_tty, M_USBDEV); 653202181Sthompsa free(sc->sc_ucom, M_USBDEV); 654202181Sthompsa } 655202181Sthompsa 656202181Sthompsa if (sc->sc_ifp != NULL) { 657202181Sthompsa callout_drain(&sc->sc_c); 658202181Sthompsa mtx_lock(&sc->sc_mtx); 659202181Sthompsa uhso_if_stop(sc); 660202181Sthompsa bpfdetach(sc->sc_ifp); 661202181Sthompsa if_detach(sc->sc_ifp); 662202181Sthompsa if_free(sc->sc_ifp); 663202181Sthompsa mtx_unlock(&sc->sc_mtx); 664202181Sthompsa usbd_transfer_unsetup(sc->sc_if_xfer, UHSO_IFNET_MAX); 665202181Sthompsa } 666202181Sthompsa 667202181Sthompsa mtx_destroy(&sc->sc_mtx); 668202181Sthompsa return (0); 669202181Sthompsa} 670202181Sthompsa 671202181Sthompsastatic void 672202181Sthompsauhso_test_autoinst(void *arg, struct usb_device *udev, 673202181Sthompsa struct usb_attach_arg *uaa) 674202181Sthompsa{ 675202181Sthompsa struct usb_interface *iface; 676202181Sthompsa struct usb_interface_descriptor *id; 677202181Sthompsa 678202243Sthompsa if (uaa->dev_state != UAA_DEV_READY || !uhso_autoswitch) 679202181Sthompsa return; 680202181Sthompsa 681202181Sthompsa iface = usbd_get_iface(udev, 0); 682202181Sthompsa if (iface == NULL) 683202181Sthompsa return; 684202181Sthompsa id = iface->idesc; 685202181Sthompsa if (id == NULL || id->bInterfaceClass != UICLASS_MASS) 686202181Sthompsa return; 687202181Sthompsa if (usbd_lookup_id_by_uaa(uhso_devs, sizeof(uhso_devs), uaa)) 688202181Sthompsa return; /* no device match */ 689202181Sthompsa 690202181Sthompsa if (usb_msc_eject(udev, 0, MSC_EJECT_REZERO) == 0) { 691202181Sthompsa /* success, mark the udev as disappearing */ 692202181Sthompsa uaa->dev_state = UAA_DEV_EJECTING; 693202181Sthompsa } 694202181Sthompsa} 695202181Sthompsa 696202181Sthompsastatic int 697202181Sthompsauhso_driver_loaded(struct module *mod, int what, void *arg) 698202181Sthompsa{ 699202181Sthompsa switch (what) { 700202181Sthompsa case MOD_LOAD: 701202181Sthompsa /* register our autoinstall handler */ 702202181Sthompsa uhso_etag = EVENTHANDLER_REGISTER(usb_dev_configured, 703202181Sthompsa uhso_test_autoinst, NULL, EVENTHANDLER_PRI_ANY); 704202181Sthompsa break; 705202181Sthompsa case MOD_UNLOAD: 706202181Sthompsa EVENTHANDLER_DEREGISTER(usb_dev_configured, uhso_etag); 707202181Sthompsa break; 708202181Sthompsa default: 709202181Sthompsa return (EOPNOTSUPP); 710202181Sthompsa } 711202181Sthompsa return (0); 712202181Sthompsa} 713202181Sthompsa 714202243Sthompsa/* 715202243Sthompsa * Probe the interface type by querying the device. The elements 716202243Sthompsa * of an array indicates the capabilities of a particular interface. 717202243Sthompsa * Returns a bit mask with the interface capabilities. 718202243Sthompsa */ 719202243Sthompsastatic int 720202243Sthompsauhso_probe_iface_auto(struct uhso_softc *sc, int index) 721202181Sthompsa{ 722202181Sthompsa struct usb_device_request req; 723202181Sthompsa usb_error_t uerr; 724202181Sthompsa uint16_t actlen = 0; 725202181Sthompsa char port; 726202181Sthompsa char buf[17] = {0}; 727202181Sthompsa 728202181Sthompsa req.bmRequestType = UT_READ_VENDOR_DEVICE; 729202181Sthompsa req.bRequest = 0x86; 730202181Sthompsa USETW(req.wValue, 0); 731202181Sthompsa USETW(req.wIndex, 0); 732202181Sthompsa USETW(req.wLength, 17); 733202181Sthompsa 734202181Sthompsa uerr = usbd_do_request_flags(sc->sc_udev, NULL, &req, buf, 735202181Sthompsa 0, &actlen, USB_MS_HZ); 736202181Sthompsa if (uerr != 0) { 737202181Sthompsa device_printf(sc->sc_dev, "usbd_do_request_flags failed: %s\n", 738202181Sthompsa usbd_errstr(uerr)); 739202181Sthompsa return (0); 740202181Sthompsa } 741202181Sthompsa 742202243Sthompsa UHSO_DPRINTF(1, "actlen=%d\n", actlen); 743202181Sthompsa UHSO_HEXDUMP(buf, 17); 744202181Sthompsa 745202181Sthompsa if (index < 0 || index > 16) { 746202181Sthompsa UHSO_DPRINTF(0, "Index %d out of range\n", index); 747202181Sthompsa return (0); 748202181Sthompsa } 749202181Sthompsa 750202243Sthompsa UHSO_DPRINTF(1, "index=%d, type=%x[%s]\n", index, buf[index], 751202243Sthompsa uhso_port_type[(int)uhso_port_map[(int)buf[index]]]); 752202181Sthompsa 753202181Sthompsa if (buf[index] >= uhso_port_map_max) 754202181Sthompsa port = 0; 755202181Sthompsa else 756202181Sthompsa port = uhso_port_map[(int)buf[index]]; 757202181Sthompsa 758202243Sthompsa switch (port) { 759202243Sthompsa case UHSO_PORT_TYPE_NETWORK: 760202243Sthompsa return (UHSO_IFACE_SPEC(UHSO_IF_NET | UHSO_IF_MUX, 761202243Sthompsa UHSO_PORT_SERIAL | UHSO_PORT_NETWORK, port)); 762202243Sthompsa case UHSO_PORT_TYPE_VOICE: 763202243Sthompsa /* Don't claim 'voice' ports */ 764202181Sthompsa return (0); 765202243Sthompsa default: 766202181Sthompsa return (UHSO_IFACE_SPEC(UHSO_IF_BULK, 767202181Sthompsa UHSO_PORT_SERIAL, port)); 768202243Sthompsa } 769202181Sthompsa 770202181Sthompsa return (0); 771202181Sthompsa} 772202181Sthompsa 773202181Sthompsastatic int 774202181Sthompsauhso_probe_iface_static(struct uhso_softc *sc, int index) 775202181Sthompsa{ 776202181Sthompsa struct usb_config_descriptor *cd; 777202181Sthompsa 778202181Sthompsa cd = usbd_get_config_descriptor(sc->sc_udev); 779202181Sthompsa if (cd->bNumInterface <= 3) { 780202243Sthompsa /* Cards with 3 or less interfaces */ 781202181Sthompsa switch (index) { 782202181Sthompsa case 0: 783202181Sthompsa return UHSO_IFACE_SPEC(UHSO_IF_NET | UHSO_IF_MUX, 784202243Sthompsa UHSO_PORT_SERIAL | UHSO_PORT_NETWORK, 785202243Sthompsa UHSO_PORT_TYPE_NETWORK); 786202181Sthompsa case 1: 787202181Sthompsa return UHSO_IFACE_SPEC(UHSO_IF_BULK, 788202181Sthompsa UHSO_PORT_SERIAL, UHSO_PORT_TYPE_DIAG); 789202181Sthompsa case 2: 790202181Sthompsa return UHSO_IFACE_SPEC(UHSO_IF_BULK, 791202181Sthompsa UHSO_PORT_SERIAL, UHSO_PORT_TYPE_MODEM); 792202181Sthompsa } 793202243Sthompsa } else { 794202243Sthompsa /* Cards with 4 interfaces */ 795202181Sthompsa switch (index) { 796202181Sthompsa case 0: 797202181Sthompsa return UHSO_IFACE_SPEC(UHSO_IF_NET | UHSO_IF_MUX, 798202243Sthompsa UHSO_PORT_SERIAL | UHSO_PORT_NETWORK, 799202243Sthompsa UHSO_PORT_TYPE_NETWORK); 800202181Sthompsa case 1: 801202181Sthompsa return UHSO_IFACE_SPEC(UHSO_IF_BULK, 802202181Sthompsa UHSO_PORT_SERIAL, UHSO_PORT_TYPE_DIAG2); 803202181Sthompsa case 2: 804202181Sthompsa return UHSO_IFACE_SPEC(UHSO_IF_BULK, 805202181Sthompsa UHSO_PORT_SERIAL, UHSO_PORT_TYPE_MODEM); 806202181Sthompsa case 3: 807202181Sthompsa return UHSO_IFACE_SPEC(UHSO_IF_BULK, 808202181Sthompsa UHSO_PORT_SERIAL, UHSO_PORT_TYPE_DIAG); 809202181Sthompsa } 810202181Sthompsa } 811202181Sthompsa return (0); 812202181Sthompsa} 813202181Sthompsa 814202243Sthompsa/* 815202243Sthompsa * Probes an interface for its particular capabilities and attaches if 816202243Sthompsa * it's a supported interface. 817202243Sthompsa */ 818202181Sthompsastatic int 819202181Sthompsauhso_probe_iface(struct uhso_softc *sc, int index, 820202181Sthompsa int (*probe)(struct uhso_softc *, int)) 821202181Sthompsa{ 822202181Sthompsa struct usb_interface *iface; 823202243Sthompsa int type, error; 824202181Sthompsa 825202243Sthompsa UHSO_DPRINTF(1, "Probing for interface %d, probe_func=%p\n", index, probe); 826202181Sthompsa 827202181Sthompsa type = probe(sc, index); 828202181Sthompsa UHSO_DPRINTF(1, "Probe result %x\n", type); 829202181Sthompsa if (type <= 0) 830202181Sthompsa return (ENXIO); 831202181Sthompsa 832202181Sthompsa sc->sc_type = type; 833202181Sthompsa iface = usbd_get_iface(sc->sc_udev, index); 834202181Sthompsa 835202243Sthompsa if (UHSO_IFACE_PORT_TYPE(type) == UHSO_PORT_TYPE_NETWORK) { 836202181Sthompsa error = uhso_attach_ifnet(sc, iface, type); 837202243Sthompsa if (error) { 838202243Sthompsa UHSO_DPRINTF(1, "uhso_attach_ifnet failed"); 839202181Sthompsa return (ENXIO); 840202243Sthompsa } 841202181Sthompsa 842202243Sthompsa /* 843202243Sthompsa * If there is an additional interrupt endpoint on this 844202270Sthompsa * interface then we most likely have a multiplexed serial port 845202243Sthompsa * available. 846202243Sthompsa */ 847202243Sthompsa if (iface->idesc->bNumEndpoints < 3) { 848202243Sthompsa sc->sc_type = UHSO_IFACE_SPEC( 849202243Sthompsa UHSO_IFACE_USB_TYPE(type) & ~UHSO_IF_MUX, 850202243Sthompsa UHSO_IFACE_PORT(type) & ~UHSO_PORT_SERIAL, 851202243Sthompsa UHSO_IFACE_PORT_TYPE(type)); 852202243Sthompsa return (0); 853202243Sthompsa } 854202243Sthompsa 855202243Sthompsa UHSO_DPRINTF(1, "Trying to attach mux. serial\n"); 856202243Sthompsa error = uhso_attach_muxserial(sc, iface, type); 857202243Sthompsa if (error == 0 && sc->sc_ttys > 0) { 858202181Sthompsa error = ucom_attach(&sc->sc_super_ucom, sc->sc_ucom, 859202181Sthompsa sc->sc_ttys, sc, &uhso_ucom_callback, &sc->sc_mtx); 860202181Sthompsa if (error) { 861202181Sthompsa device_printf(sc->sc_dev, "ucom_attach failed\n"); 862202181Sthompsa return (ENXIO); 863202181Sthompsa } 864202243Sthompsa 865202243Sthompsa mtx_lock(&sc->sc_mtx); 866202243Sthompsa usbd_transfer_start(sc->sc_xfer[UHSO_MUX_ENDPT_INTR]); 867202243Sthompsa mtx_unlock(&sc->sc_mtx); 868202181Sthompsa } 869202243Sthompsa } else if ((UHSO_IFACE_USB_TYPE(type) & UHSO_IF_BULK) && 870202181Sthompsa UHSO_IFACE_PORT(type) & UHSO_PORT_SERIAL) { 871202181Sthompsa 872202181Sthompsa error = uhso_attach_bulkserial(sc, iface, type); 873202181Sthompsa if (error) 874202181Sthompsa return (ENXIO); 875202181Sthompsa 876202181Sthompsa error = ucom_attach(&sc->sc_super_ucom, sc->sc_ucom, 877202181Sthompsa sc->sc_ttys, sc, &uhso_ucom_callback, &sc->sc_mtx); 878202181Sthompsa if (error) { 879202181Sthompsa device_printf(sc->sc_dev, "ucom_attach failed\n"); 880202181Sthompsa return (ENXIO); 881202181Sthompsa } 882202181Sthompsa } 883202181Sthompsa else { 884202243Sthompsa UHSO_DPRINTF(0, "Unknown type %x\n", type); 885202181Sthompsa return (ENXIO); 886202181Sthompsa } 887202181Sthompsa 888202181Sthompsa return (0); 889202181Sthompsa} 890202181Sthompsa 891202243Sthompsa/* 892202243Sthompsa * Expands allocated memory to fit an additional TTY. 893202243Sthompsa * Two arrays are kept with matching indexes, one for ucom and one 894202243Sthompsa * for our private data. 895202243Sthompsa */ 896202181Sthompsastatic int 897202181Sthompsauhso_alloc_tty(struct uhso_softc *sc) 898202181Sthompsa{ 899202181Sthompsa 900202181Sthompsa sc->sc_ttys++; 901202181Sthompsa sc->sc_tty = reallocf(sc->sc_tty, sizeof(struct uhso_tty) * sc->sc_ttys, 902202181Sthompsa M_USBDEV, M_WAITOK | M_ZERO); 903202181Sthompsa if (sc->sc_tty == NULL) 904202181Sthompsa return (-1); 905202181Sthompsa 906202181Sthompsa sc->sc_ucom = reallocf(sc->sc_ucom, 907202181Sthompsa sizeof(struct ucom_softc) * sc->sc_ttys, M_USBDEV, M_WAITOK | M_ZERO); 908202181Sthompsa if (sc->sc_ucom == NULL) 909202181Sthompsa return (-1); 910202181Sthompsa 911202181Sthompsa sc->sc_tty[sc->sc_ttys - 1].ht_sc = sc; 912202181Sthompsa 913202243Sthompsa UHSO_DPRINTF(1, "Allocated TTY %d\n", sc->sc_ttys - 1); 914202181Sthompsa return (sc->sc_ttys - 1); 915202181Sthompsa} 916202181Sthompsa 917202243Sthompsa/* 918202243Sthompsa * Attach a multiplexed serial port 919202243Sthompsa * Data is read/written with requests on the default control pipe. An interrupt 920202243Sthompsa * endpoint returns when there is new data to be read. 921202243Sthompsa */ 922202181Sthompsastatic int 923202181Sthompsauhso_attach_muxserial(struct uhso_softc *sc, struct usb_interface *iface, 924202181Sthompsa int type) 925202181Sthompsa{ 926202181Sthompsa struct usb_descriptor *desc; 927202181Sthompsa int i, port, tty; 928202181Sthompsa usb_error_t uerr; 929202181Sthompsa 930202181Sthompsa /* 931202181Sthompsa * The class specific interface (type 0x24) descriptor subtype field 932202181Sthompsa * contains a bitmask that specifies which (and how many) ports that 933202181Sthompsa * are available through this multiplexed serial port. 934202181Sthompsa */ 935202181Sthompsa desc = usbd_find_descriptor(sc->sc_udev, NULL, 936202181Sthompsa iface->idesc->bInterfaceNumber, UDESC_CS_INTERFACE, 0xff, 0, 0); 937202181Sthompsa if (desc == NULL) { 938202181Sthompsa UHSO_DPRINTF(0, "Failed to find UDESC_CS_INTERFACE\n"); 939202181Sthompsa return (ENXIO); 940202181Sthompsa } 941202181Sthompsa 942202181Sthompsa UHSO_DPRINTF(1, "Mux port mask %x\n", desc->bDescriptorSubtype); 943202181Sthompsa if (desc->bDescriptorSubtype == 0) 944202181Sthompsa return (ENXIO); 945202181Sthompsa 946202243Sthompsa /* 947202243Sthompsa * The bitmask is one octet, loop through the number of 948202243Sthompsa * bits that are set and create a TTY for each. 949202243Sthompsa */ 950202181Sthompsa for (i = 0; i < 8; i++) { 951202181Sthompsa port = (1 << i); 952202181Sthompsa if ((port & desc->bDescriptorSubtype) == port) { 953202181Sthompsa UHSO_DPRINTF(2, "Found mux port %x (%d)\n", port, i); 954202181Sthompsa tty = uhso_alloc_tty(sc); 955202181Sthompsa if (tty < 0) 956202181Sthompsa return (ENOMEM); 957202181Sthompsa sc->sc_tty[tty].ht_muxport = i; 958202181Sthompsa uerr = usbd_transfer_setup(sc->sc_udev, 959202181Sthompsa &sc->sc_iface_index, sc->sc_tty[tty].ht_xfer, 960202181Sthompsa uhso_ctrl_config, UHSO_CTRL_MAX, sc, &sc->sc_mtx); 961202181Sthompsa if (uerr) { 962202181Sthompsa device_printf(sc->sc_dev, 963202181Sthompsa "Failed to setup control pipe: %s\n", 964202181Sthompsa usbd_errstr(uerr)); 965202181Sthompsa return (ENXIO); 966202181Sthompsa } 967202181Sthompsa } 968202181Sthompsa } 969202181Sthompsa 970202243Sthompsa /* Setup the intr. endpoint */ 971202181Sthompsa uerr = usbd_transfer_setup(sc->sc_udev, 972202181Sthompsa &iface->idesc->bInterfaceNumber, sc->sc_xfer, 973202181Sthompsa uhso_mux_config, 1, sc, &sc->sc_mtx); 974202181Sthompsa if (uerr) 975202181Sthompsa return (ENXIO); 976202181Sthompsa 977202181Sthompsa return (0); 978202181Sthompsa} 979202181Sthompsa 980202243Sthompsa/* 981202243Sthompsa * Interrupt callback for the multiplexed serial port. Indicates 982202270Sthompsa * which serial port has data waiting. 983202243Sthompsa */ 984202181Sthompsastatic void 985202181Sthompsauhso_mux_intr_callback(struct usb_xfer *xfer, usb_error_t error) 986202181Sthompsa{ 987202181Sthompsa struct usb_page_cache *pc; 988202181Sthompsa struct usb_page_search res; 989202181Sthompsa struct uhso_softc *sc = usbd_xfer_softc(xfer); 990202181Sthompsa unsigned int i, mux; 991202181Sthompsa 992202181Sthompsa UHSO_DPRINTF(3, "status %d\n", USB_GET_STATE(xfer)); 993202181Sthompsa 994202181Sthompsa switch (USB_GET_STATE(xfer)) { 995202181Sthompsa case USB_ST_TRANSFERRED: 996202181Sthompsa /* 997202181Sthompsa * The multiplexed port number can be found at the first byte. 998202181Sthompsa * It contains a bit mask, we transform this in to an integer. 999202181Sthompsa */ 1000202181Sthompsa pc = usbd_xfer_get_frame(xfer, 0); 1001202181Sthompsa usbd_get_page(pc, 0, &res); 1002202181Sthompsa 1003202181Sthompsa i = *((unsigned char *)res.buffer); 1004202181Sthompsa mux = 0; 1005202181Sthompsa while (i >>= 1) { 1006202181Sthompsa mux++; 1007202181Sthompsa } 1008202181Sthompsa 1009202181Sthompsa UHSO_DPRINTF(3, "mux port %d (%d)\n", mux, i); 1010202181Sthompsa if (mux > UHSO_MPORT_TYPE_NOMAX) 1011202181Sthompsa break; 1012202181Sthompsa 1013202243Sthompsa /* Issue a read for this serial port */ 1014202181Sthompsa usbd_xfer_set_priv( 1015202181Sthompsa sc->sc_tty[mux].ht_xfer[UHSO_CTRL_READ], 1016202181Sthompsa &sc->sc_tty[mux]); 1017202181Sthompsa usbd_transfer_start(sc->sc_tty[mux].ht_xfer[UHSO_CTRL_READ]); 1018202181Sthompsa 1019202181Sthompsa break; 1020202181Sthompsa case USB_ST_SETUP: 1021202181Sthompsatr_setup: 1022202181Sthompsa usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer)); 1023202181Sthompsa usbd_transfer_submit(xfer); 1024202181Sthompsa break; 1025202181Sthompsa default: 1026202181Sthompsa UHSO_DPRINTF(0, "error: %s\n", usbd_errstr(error)); 1027202181Sthompsa if (error == USB_ERR_CANCELLED) 1028202181Sthompsa break; 1029202181Sthompsa 1030202181Sthompsa usbd_xfer_set_stall(xfer); 1031202181Sthompsa goto tr_setup; 1032202181Sthompsa } 1033202181Sthompsa} 1034202181Sthompsa 1035202181Sthompsastatic void 1036202181Sthompsauhso_mux_read_callback(struct usb_xfer *xfer, usb_error_t error) 1037202181Sthompsa{ 1038202181Sthompsa struct uhso_softc *sc = usbd_xfer_softc(xfer); 1039202181Sthompsa struct usb_page_cache *pc; 1040202181Sthompsa struct usb_device_request req; 1041202181Sthompsa struct uhso_tty *ht; 1042202181Sthompsa int actlen, len; 1043202181Sthompsa 1044202181Sthompsa usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL); 1045202181Sthompsa 1046202181Sthompsa UHSO_DPRINTF(3, "status %d\n", USB_GET_STATE(xfer)); 1047202181Sthompsa 1048202181Sthompsa ht = usbd_xfer_get_priv(xfer); 1049202181Sthompsa UHSO_DPRINTF(3, "ht=%p open=%d\n", ht, ht->ht_open); 1050202181Sthompsa 1051202181Sthompsa switch (USB_GET_STATE(xfer)) { 1052202181Sthompsa case USB_ST_TRANSFERRED: 1053202181Sthompsa /* Got data, send to ucom */ 1054202181Sthompsa pc = usbd_xfer_get_frame(xfer, 1); 1055202181Sthompsa len = usbd_xfer_frame_len(xfer, 1); 1056202181Sthompsa 1057202181Sthompsa UHSO_DPRINTF(3, "got %d bytes on mux port %d\n", len, 1058202181Sthompsa ht->ht_muxport); 1059202181Sthompsa if (len <= 0) { 1060202181Sthompsa usbd_transfer_start(sc->sc_xfer[UHSO_MUX_ENDPT_INTR]); 1061202181Sthompsa break; 1062202181Sthompsa } 1063202181Sthompsa 1064202181Sthompsa /* Deliver data if the TTY is open, discard otherwise */ 1065202181Sthompsa if (ht->ht_open) 1066202181Sthompsa ucom_put_data(&sc->sc_ucom[ht->ht_muxport], pc, 0, len); 1067202181Sthompsa /* FALLTHROUGH */ 1068202181Sthompsa case USB_ST_SETUP: 1069202181Sthompsatr_setup: 1070202181Sthompsa bzero(&req, sizeof(struct usb_device_request)); 1071202181Sthompsa req.bmRequestType = UT_READ_CLASS_INTERFACE; 1072202181Sthompsa req.bRequest = UCDC_GET_ENCAPSULATED_RESPONSE; 1073202181Sthompsa USETW(req.wValue, 0); 1074202181Sthompsa USETW(req.wIndex, ht->ht_muxport); 1075202181Sthompsa USETW(req.wLength, 1024); 1076202181Sthompsa 1077202181Sthompsa pc = usbd_xfer_get_frame(xfer, 0); 1078202181Sthompsa usbd_copy_in(pc, 0, &req, sizeof(req)); 1079202181Sthompsa 1080202181Sthompsa usbd_xfer_set_frame_len(xfer, 0, sizeof(req)); 1081202181Sthompsa usbd_xfer_set_frame_len(xfer, 1, 1024); 1082202181Sthompsa usbd_xfer_set_frames(xfer, 2); 1083202181Sthompsa usbd_transfer_submit(xfer); 1084202181Sthompsa break; 1085202181Sthompsa default: 1086202181Sthompsa UHSO_DPRINTF(0, "error: %s\n", usbd_errstr(error)); 1087202181Sthompsa if (error == USB_ERR_CANCELLED) 1088202181Sthompsa break; 1089202181Sthompsa usbd_xfer_set_stall(xfer); 1090202181Sthompsa goto tr_setup; 1091202181Sthompsa } 1092202181Sthompsa} 1093202181Sthompsa 1094202181Sthompsastatic void 1095202181Sthompsauhso_mux_write_callback(struct usb_xfer *xfer, usb_error_t error) 1096202181Sthompsa{ 1097202181Sthompsa struct uhso_softc *sc = usbd_xfer_softc(xfer); 1098202181Sthompsa struct uhso_tty *ht; 1099202181Sthompsa struct usb_page_cache *pc; 1100202181Sthompsa struct usb_device_request req; 1101202181Sthompsa int actlen; 1102202181Sthompsa struct usb_page_search res; 1103202181Sthompsa 1104202181Sthompsa usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL); 1105202181Sthompsa 1106202181Sthompsa ht = usbd_xfer_get_priv(xfer); 1107202181Sthompsa UHSO_DPRINTF(3, "status=%d, using mux port %d\n", 1108202181Sthompsa USB_GET_STATE(xfer), ht->ht_muxport); 1109202181Sthompsa 1110202181Sthompsa switch (USB_GET_STATE(xfer)) { 1111202181Sthompsa case USB_ST_TRANSFERRED: 1112202181Sthompsa UHSO_DPRINTF(3, "wrote %zd data bytes to muxport %d\n", 1113202181Sthompsa actlen - sizeof(struct usb_device_request) , 1114202181Sthompsa ht->ht_muxport); 1115202181Sthompsa /* FALLTHROUGH */ 1116202181Sthompsa case USB_ST_SETUP: 1117202181Sthompsa pc = usbd_xfer_get_frame(xfer, 1); 1118202181Sthompsa if (ucom_get_data(&sc->sc_ucom[ht->ht_muxport], pc, 1119202181Sthompsa 0, 32, &actlen)) { 1120202181Sthompsa 1121202181Sthompsa usbd_get_page(pc, 0, &res); 1122202181Sthompsa 1123202181Sthompsa bzero(&req, sizeof(struct usb_device_request)); 1124202181Sthompsa req.bmRequestType = UT_WRITE_CLASS_INTERFACE; 1125202181Sthompsa req.bRequest = UCDC_SEND_ENCAPSULATED_COMMAND; 1126202181Sthompsa USETW(req.wValue, 0); 1127202181Sthompsa USETW(req.wIndex, ht->ht_muxport); 1128202181Sthompsa USETW(req.wLength, actlen); 1129202181Sthompsa 1130202181Sthompsa pc = usbd_xfer_get_frame(xfer, 0); 1131202181Sthompsa usbd_copy_in(pc, 0, &req, sizeof(req)); 1132202181Sthompsa 1133202181Sthompsa usbd_xfer_set_frame_len(xfer, 0, sizeof(req)); 1134202181Sthompsa usbd_xfer_set_frame_len(xfer, 1, actlen); 1135202181Sthompsa usbd_xfer_set_frames(xfer, 2); 1136202181Sthompsa 1137202181Sthompsa UHSO_DPRINTF(3, "Prepared %d bytes for transmit " 1138202181Sthompsa "on muxport %d\n", actlen, ht->ht_muxport); 1139202181Sthompsa 1140202181Sthompsa usbd_transfer_submit(xfer); 1141202181Sthompsa } 1142202181Sthompsa break; 1143202181Sthompsa default: 1144202181Sthompsa UHSO_DPRINTF(0, "error: %s\n", usbd_errstr(error)); 1145202181Sthompsa if (error == USB_ERR_CANCELLED) 1146202181Sthompsa break; 1147202181Sthompsa break; 1148202181Sthompsa } 1149202181Sthompsa} 1150202181Sthompsa 1151202181Sthompsastatic int 1152202181Sthompsauhso_attach_bulkserial(struct uhso_softc *sc, struct usb_interface *iface, 1153202181Sthompsa int type) 1154202181Sthompsa{ 1155202181Sthompsa usb_error_t uerr; 1156202181Sthompsa int tty; 1157202181Sthompsa 1158202243Sthompsa /* Try attaching RD/WR/INTR first */ 1159202181Sthompsa uerr = usbd_transfer_setup(sc->sc_udev, 1160202181Sthompsa &iface->idesc->bInterfaceNumber, sc->sc_xfer, 1161202181Sthompsa uhso_bs_config, UHSO_BULK_ENDPT_MAX, sc, &sc->sc_mtx); 1162202181Sthompsa if (uerr) { 1163202181Sthompsa /* Try only RD/WR */ 1164202181Sthompsa uerr = usbd_transfer_setup(sc->sc_udev, 1165202181Sthompsa &iface->idesc->bInterfaceNumber, sc->sc_xfer, 1166202181Sthompsa uhso_bs_config, UHSO_BULK_ENDPT_MAX - 1, sc, &sc->sc_mtx); 1167202181Sthompsa } 1168202181Sthompsa if (uerr) { 1169202181Sthompsa UHSO_DPRINTF(0, "usbd_transfer_setup failed"); 1170202181Sthompsa return (-1); 1171202181Sthompsa } 1172202181Sthompsa 1173202181Sthompsa tty = uhso_alloc_tty(sc); 1174202181Sthompsa if (tty < 0) { 1175202181Sthompsa usbd_transfer_unsetup(sc->sc_xfer, UHSO_BULK_ENDPT_MAX); 1176202181Sthompsa return (ENOMEM); 1177202181Sthompsa } 1178202181Sthompsa 1179202181Sthompsa sc->sc_tty[tty].ht_muxport = -1; 1180202181Sthompsa return (0); 1181202181Sthompsa} 1182202181Sthompsa 1183202181Sthompsastatic void 1184202181Sthompsauhso_bs_read_callback(struct usb_xfer *xfer, usb_error_t error) 1185202181Sthompsa{ 1186202181Sthompsa struct uhso_softc *sc = usbd_xfer_softc(xfer); 1187202181Sthompsa struct usb_page_cache *pc; 1188202181Sthompsa int actlen; 1189202181Sthompsa 1190202181Sthompsa usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL); 1191202181Sthompsa 1192202181Sthompsa UHSO_DPRINTF(3, "status %d, actlen=%d\n", USB_GET_STATE(xfer), actlen); 1193202181Sthompsa 1194202181Sthompsa switch (USB_GET_STATE(xfer)) { 1195202181Sthompsa case USB_ST_TRANSFERRED: 1196202181Sthompsa pc = usbd_xfer_get_frame(xfer, 0); 1197202181Sthompsa ucom_put_data(&sc->sc_ucom[0], pc, 0, actlen); 1198202181Sthompsa /* FALLTHROUGH */ 1199202181Sthompsa case USB_ST_SETUP: 1200202181Sthompsatr_setup: 1201202181Sthompsa usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer)); 1202202181Sthompsa usbd_transfer_submit(xfer); 1203202181Sthompsa break; 1204202181Sthompsa default: 1205202181Sthompsa UHSO_DPRINTF(0, "error: %s\n", usbd_errstr(error)); 1206202181Sthompsa if (error == USB_ERR_CANCELLED) 1207202181Sthompsa break; 1208202181Sthompsa usbd_xfer_set_stall(xfer); 1209202181Sthompsa goto tr_setup; 1210202181Sthompsa } 1211202181Sthompsa} 1212202181Sthompsa 1213202181Sthompsastatic void 1214202181Sthompsauhso_bs_write_callback(struct usb_xfer *xfer, usb_error_t error) 1215202181Sthompsa{ 1216202181Sthompsa struct uhso_softc *sc = usbd_xfer_softc(xfer); 1217202181Sthompsa struct usb_page_cache *pc; 1218202181Sthompsa int actlen; 1219202181Sthompsa 1220202181Sthompsa usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL); 1221202181Sthompsa 1222202181Sthompsa UHSO_DPRINTF(3, "status %d, actlen=%d\n", USB_GET_STATE(xfer), actlen); 1223202181Sthompsa 1224202181Sthompsa switch (USB_GET_STATE(xfer)) { 1225202181Sthompsa case USB_ST_TRANSFERRED: 1226202181Sthompsa case USB_ST_SETUP: 1227202181Sthompsatr_setup: 1228202181Sthompsa pc = usbd_xfer_get_frame(xfer, 0); 1229202181Sthompsa if (ucom_get_data(&sc->sc_ucom[0], pc, 0, 8192, &actlen)) { 1230202181Sthompsa usbd_xfer_set_frame_len(xfer, 0, actlen); 1231202181Sthompsa usbd_transfer_submit(xfer); 1232202181Sthompsa } 1233202181Sthompsa break; 1234202181Sthompsa break; 1235202181Sthompsa default: 1236202181Sthompsa UHSO_DPRINTF(0, "error: %s\n", usbd_errstr(error)); 1237202181Sthompsa if (error == USB_ERR_CANCELLED) 1238202181Sthompsa break; 1239202181Sthompsa usbd_xfer_set_stall(xfer); 1240202181Sthompsa goto tr_setup; 1241202181Sthompsa } 1242202181Sthompsa} 1243202181Sthompsa 1244202181Sthompsastatic void 1245202181Sthompsauhso_bs_cfg(struct uhso_softc *sc) 1246202181Sthompsa{ 1247202181Sthompsa struct usb_device_request req; 1248202181Sthompsa usb_error_t uerr; 1249202181Sthompsa 1250202181Sthompsa if (!(UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_BULK)) 1251202181Sthompsa return; 1252202181Sthompsa 1253202181Sthompsa req.bmRequestType = UT_WRITE_CLASS_INTERFACE; 1254202181Sthompsa req.bRequest = UCDC_SET_CONTROL_LINE_STATE; 1255202181Sthompsa USETW(req.wValue, sc->sc_line); 1256202181Sthompsa USETW(req.wIndex, sc->sc_iface_no); 1257202181Sthompsa USETW(req.wLength, 0); 1258202181Sthompsa 1259202181Sthompsa uerr = ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom[0], &req, NULL, 0, 1000); 1260202181Sthompsa if (uerr != 0) { 1261202181Sthompsa device_printf(sc->sc_dev, "failed to set ctrl line state to " 1262202181Sthompsa "0x%02x: %s\n", sc->sc_line, usbd_errstr(uerr)); 1263202181Sthompsa } 1264202181Sthompsa} 1265202181Sthompsa 1266202181Sthompsastatic void 1267202181Sthompsauhso_bs_intr_callback(struct usb_xfer *xfer, usb_error_t error) 1268202181Sthompsa{ 1269202181Sthompsa struct uhso_softc *sc = usbd_xfer_softc(xfer); 1270202181Sthompsa struct usb_page_cache *pc; 1271202181Sthompsa int actlen; 1272202181Sthompsa struct usb_cdc_notification cdc; 1273202181Sthompsa 1274202181Sthompsa usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL); 1275202181Sthompsa UHSO_DPRINTF(3, "status %d, actlen=%d\n", USB_GET_STATE(xfer), actlen); 1276202181Sthompsa 1277202181Sthompsa switch (USB_GET_STATE(xfer)) { 1278202181Sthompsa case USB_ST_TRANSFERRED: 1279202181Sthompsa if (actlen < UCDC_NOTIFICATION_LENGTH) { 1280202181Sthompsa UHSO_DPRINTF(0, "UCDC notification too short: %d\n", actlen); 1281202181Sthompsa goto tr_setup; 1282202181Sthompsa } 1283202181Sthompsa else if (actlen > sizeof(struct usb_cdc_notification)) { 1284202181Sthompsa UHSO_DPRINTF(0, "UCDC notification too large: %d\n", actlen); 1285202181Sthompsa actlen = sizeof(struct usb_cdc_notification); 1286202181Sthompsa } 1287202181Sthompsa 1288202181Sthompsa pc = usbd_xfer_get_frame(xfer, 0); 1289202181Sthompsa usbd_copy_out(pc, 0, &cdc, actlen); 1290202181Sthompsa 1291202181Sthompsa if (UGETW(cdc.wIndex) != sc->sc_iface_no) { 1292202243Sthompsa UHSO_DPRINTF(0, "Interface mismatch, got %d expected %d\n", 1293202181Sthompsa UGETW(cdc.wIndex), sc->sc_iface_no); 1294202181Sthompsa goto tr_setup; 1295202181Sthompsa } 1296202181Sthompsa 1297202181Sthompsa if (cdc.bmRequestType == UCDC_NOTIFICATION && 1298202181Sthompsa cdc.bNotification == UCDC_N_SERIAL_STATE) { 1299202243Sthompsa UHSO_DPRINTF(2, "notify = 0x%02x\n", cdc.data[0]); 1300202181Sthompsa 1301202181Sthompsa sc->sc_msr = 0; 1302202181Sthompsa sc->sc_lsr = 0; 1303202181Sthompsa if (cdc.data[0] & UCDC_N_SERIAL_RI) 1304202181Sthompsa sc->sc_msr |= SER_RI; 1305202181Sthompsa if (cdc.data[0] & UCDC_N_SERIAL_DSR) 1306202181Sthompsa sc->sc_msr |= SER_DSR; 1307202181Sthompsa if (cdc.data[0] & UCDC_N_SERIAL_DCD) 1308202181Sthompsa sc->sc_msr |= SER_DCD; 1309202181Sthompsa 1310202181Sthompsa ucom_status_change(&sc->sc_ucom[0]); 1311202181Sthompsa } 1312202181Sthompsa case USB_ST_SETUP: 1313202181Sthompsatr_setup: 1314202181Sthompsa default: 1315202181Sthompsa if (error == USB_ERR_CANCELLED) 1316202181Sthompsa break; 1317202181Sthompsa usbd_xfer_set_stall(xfer); 1318202181Sthompsa goto tr_setup; 1319202181Sthompsa } 1320202181Sthompsa} 1321202181Sthompsa 1322202181Sthompsastatic void 1323202181Sthompsauhso_ucom_cfg_get_status(struct ucom_softc *ucom, uint8_t *lsr, uint8_t *msr) 1324202181Sthompsa{ 1325202181Sthompsa struct uhso_softc *sc = ucom->sc_parent; 1326202181Sthompsa 1327202181Sthompsa *lsr = sc->sc_lsr; 1328202181Sthompsa *msr = sc->sc_msr; 1329202181Sthompsa} 1330202181Sthompsa 1331202181Sthompsastatic void 1332202181Sthompsauhso_ucom_cfg_set_dtr(struct ucom_softc *ucom, uint8_t onoff) 1333202181Sthompsa{ 1334202181Sthompsa struct uhso_softc *sc = ucom->sc_parent; 1335202181Sthompsa 1336202181Sthompsa if (!(UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_BULK)) 1337202181Sthompsa return; 1338202181Sthompsa 1339202181Sthompsa if (onoff) 1340202181Sthompsa sc->sc_line |= UCDC_LINE_DTR; 1341202181Sthompsa else 1342202181Sthompsa sc->sc_line &= UCDC_LINE_DTR; 1343202181Sthompsa 1344202181Sthompsa uhso_bs_cfg(sc); 1345202181Sthompsa} 1346202181Sthompsa 1347202181Sthompsastatic void 1348202181Sthompsauhso_ucom_cfg_set_rts(struct ucom_softc *ucom, uint8_t onoff) 1349202181Sthompsa{ 1350202181Sthompsa struct uhso_softc *sc = ucom->sc_parent; 1351202181Sthompsa 1352202181Sthompsa if (!(UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_BULK)) 1353202181Sthompsa return; 1354202181Sthompsa 1355202181Sthompsa if (onoff) 1356202181Sthompsa sc->sc_line |= UCDC_LINE_RTS; 1357202181Sthompsa else 1358202181Sthompsa sc->sc_line &= UCDC_LINE_DTR; 1359202181Sthompsa 1360202181Sthompsa uhso_bs_cfg(sc); 1361202181Sthompsa} 1362202181Sthompsa 1363202181Sthompsastatic void 1364202181Sthompsauhso_ucom_start_read(struct ucom_softc *ucom) 1365202181Sthompsa{ 1366202181Sthompsa struct uhso_softc *sc = ucom->sc_parent; 1367202181Sthompsa 1368202181Sthompsa UHSO_DPRINTF(3, "unit=%d, local_unit=%d\n", 1369202181Sthompsa ucom->sc_unit, ucom->sc_local_unit); 1370202181Sthompsa 1371202181Sthompsa if (UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_MUX) { 1372202181Sthompsa sc->sc_tty[ucom->sc_local_unit].ht_open = 1; 1373202181Sthompsa usbd_transfer_start(sc->sc_xfer[UHSO_MUX_ENDPT_INTR]); 1374202181Sthompsa } 1375202181Sthompsa else if (UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_BULK) { 1376202181Sthompsa sc->sc_tty[0].ht_open = 1; 1377202181Sthompsa usbd_transfer_start(sc->sc_xfer[UHSO_BULK_ENDPT_READ]); 1378202181Sthompsa if (sc->sc_xfer[UHSO_BULK_ENDPT_INTR] != NULL) 1379202181Sthompsa usbd_transfer_start(sc->sc_xfer[UHSO_BULK_ENDPT_INTR]); 1380202181Sthompsa } 1381202181Sthompsa} 1382202181Sthompsa 1383202181Sthompsastatic void 1384202181Sthompsauhso_ucom_stop_read(struct ucom_softc *ucom) 1385202181Sthompsa{ 1386202181Sthompsa 1387202181Sthompsa struct uhso_softc *sc = ucom->sc_parent; 1388202181Sthompsa 1389202181Sthompsa if (UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_MUX) { 1390202181Sthompsa sc->sc_tty[ucom->sc_local_unit].ht_open = 0; 1391202181Sthompsa usbd_transfer_stop( 1392202181Sthompsa sc->sc_tty[ucom->sc_local_unit].ht_xfer[UHSO_CTRL_READ]); 1393202181Sthompsa } 1394202181Sthompsa else if (UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_BULK) { 1395202181Sthompsa sc->sc_tty[0].ht_open = 0; 1396202181Sthompsa usbd_transfer_start(sc->sc_xfer[UHSO_BULK_ENDPT_READ]); 1397202181Sthompsa if (sc->sc_xfer[UHSO_BULK_ENDPT_INTR] != NULL) 1398202181Sthompsa usbd_transfer_stop(sc->sc_xfer[UHSO_BULK_ENDPT_INTR]); 1399202181Sthompsa } 1400202181Sthompsa} 1401202181Sthompsa 1402202181Sthompsastatic void 1403202181Sthompsauhso_ucom_start_write(struct ucom_softc *ucom) 1404202181Sthompsa{ 1405202181Sthompsa struct uhso_softc *sc = ucom->sc_parent; 1406202181Sthompsa 1407202181Sthompsa if (UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_MUX) { 1408202181Sthompsa UHSO_DPRINTF(3, "local unit %d\n", ucom->sc_local_unit); 1409202181Sthompsa 1410202181Sthompsa usbd_transfer_start(sc->sc_xfer[UHSO_MUX_ENDPT_INTR]); 1411202181Sthompsa 1412202181Sthompsa usbd_xfer_set_priv( 1413202181Sthompsa sc->sc_tty[ucom->sc_local_unit].ht_xfer[UHSO_CTRL_WRITE], 1414202181Sthompsa &sc->sc_tty[ucom->sc_local_unit]); 1415202181Sthompsa usbd_transfer_start( 1416202181Sthompsa sc->sc_tty[ucom->sc_local_unit].ht_xfer[UHSO_CTRL_WRITE]); 1417202181Sthompsa 1418202181Sthompsa } 1419202181Sthompsa else if (UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_BULK) { 1420202181Sthompsa usbd_transfer_start(sc->sc_xfer[UHSO_BULK_ENDPT_WRITE]); 1421202181Sthompsa } 1422202181Sthompsa} 1423202181Sthompsa 1424202181Sthompsastatic void 1425202181Sthompsauhso_ucom_stop_write(struct ucom_softc *ucom) 1426202181Sthompsa{ 1427202181Sthompsa struct uhso_softc *sc = ucom->sc_parent; 1428202181Sthompsa 1429202181Sthompsa if (UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_MUX) { 1430202181Sthompsa usbd_transfer_stop( 1431202181Sthompsa sc->sc_tty[ucom->sc_local_unit].ht_xfer[UHSO_CTRL_WRITE]); 1432202181Sthompsa } 1433202181Sthompsa else if (UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_BULK) { 1434202181Sthompsa usbd_transfer_stop(sc->sc_xfer[UHSO_BULK_ENDPT_WRITE]); 1435202181Sthompsa } 1436202181Sthompsa} 1437202181Sthompsa 1438202181Sthompsastatic int uhso_attach_ifnet(struct uhso_softc *sc, struct usb_interface *iface, 1439202181Sthompsa int type) 1440202181Sthompsa{ 1441202181Sthompsa struct ifnet *ifp; 1442202181Sthompsa usb_error_t uerr; 1443202181Sthompsa struct sysctl_ctx_list *sctx; 1444202181Sthompsa struct sysctl_oid *soid; 1445202181Sthompsa 1446202181Sthompsa uerr = usbd_transfer_setup(sc->sc_udev, 1447202181Sthompsa &iface->idesc->bInterfaceNumber, sc->sc_if_xfer, 1448202181Sthompsa uhso_ifnet_config, UHSO_IFNET_MAX, sc, &sc->sc_mtx); 1449202181Sthompsa if (uerr) { 1450202181Sthompsa UHSO_DPRINTF(0, "usbd_transfer_setup failed: %s\n", 1451202181Sthompsa usbd_errstr(uerr)); 1452202181Sthompsa return (-1); 1453202181Sthompsa } 1454202181Sthompsa 1455202243Sthompsa sc->sc_ifp = ifp = if_alloc(IFT_OTHER); 1456202181Sthompsa if (sc->sc_ifp == NULL) { 1457202181Sthompsa device_printf(sc->sc_dev, "if_alloc() failed\n"); 1458202181Sthompsa return (-1); 1459202181Sthompsa } 1460202181Sthompsa 1461202181Sthompsa callout_init_mtx(&sc->sc_c, &sc->sc_mtx, 0); 1462202181Sthompsa mtx_lock(&sc->sc_mtx); 1463202181Sthompsa callout_reset(&sc->sc_c, 1, uhso_if_rxflush, sc); 1464202181Sthompsa mtx_unlock(&sc->sc_mtx); 1465202181Sthompsa 1466202181Sthompsa if_initname(ifp, device_get_name(sc->sc_dev), device_get_unit(sc->sc_dev)); 1467202181Sthompsa ifp->if_mtu = UHSO_MAX_MTU; 1468202181Sthompsa ifp->if_ioctl = uhso_if_ioctl; 1469202181Sthompsa ifp->if_init = uhso_if_init; 1470202181Sthompsa ifp->if_start = uhso_if_start; 1471202181Sthompsa ifp->if_output = uhso_if_output; 1472202181Sthompsa ifp->if_flags = 0; 1473202181Sthompsa ifp->if_softc = sc; 1474207554Ssobomax IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen); 1475207554Ssobomax ifp->if_snd.ifq_drv_maxlen = ifqmaxlen; 1476202181Sthompsa IFQ_SET_READY(&ifp->if_snd); 1477202181Sthompsa 1478202181Sthompsa if_attach(ifp); 1479202181Sthompsa bpfattach(ifp, DLT_RAW, 0); 1480202181Sthompsa 1481202181Sthompsa sctx = device_get_sysctl_ctx(sc->sc_dev); 1482202181Sthompsa soid = device_get_sysctl_tree(sc->sc_dev); 1483202181Sthompsa /* Unlocked read... */ 1484202181Sthompsa SYSCTL_ADD_STRING(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "netif", 1485202181Sthompsa CTLFLAG_RD, ifp->if_xname, 0, "Attached network interface"); 1486202181Sthompsa 1487202181Sthompsa return (0); 1488202181Sthompsa} 1489202181Sthompsa 1490202181Sthompsastatic void 1491202181Sthompsauhso_ifnet_read_callback(struct usb_xfer *xfer, usb_error_t error) 1492202181Sthompsa{ 1493202181Sthompsa struct uhso_softc *sc = usbd_xfer_softc(xfer); 1494202181Sthompsa struct mbuf *m; 1495202181Sthompsa struct usb_page_cache *pc; 1496202181Sthompsa int actlen; 1497202181Sthompsa 1498202181Sthompsa usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL); 1499202181Sthompsa 1500202181Sthompsa UHSO_DPRINTF(3, "status=%d, actlen=%d\n", USB_GET_STATE(xfer), actlen); 1501202181Sthompsa 1502202181Sthompsa switch (USB_GET_STATE(xfer)) { 1503202181Sthompsa case USB_ST_TRANSFERRED: 1504202181Sthompsa if (actlen > 0 && (sc->sc_ifp->if_drv_flags & IFF_DRV_RUNNING)) { 1505202181Sthompsa pc = usbd_xfer_get_frame(xfer, 0); 1506202181Sthompsa m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR); 1507202181Sthompsa usbd_copy_out(pc, 0, mtod(m, uint8_t *), actlen); 1508202181Sthompsa m->m_pkthdr.len = m->m_len = actlen; 1509202243Sthompsa /* Enqueue frame for further processing */ 1510202181Sthompsa _IF_ENQUEUE(&sc->sc_rxq, m); 1511202181Sthompsa if (!callout_pending(&sc->sc_c) || 1512202181Sthompsa !callout_active(&sc->sc_c)) { 1513202181Sthompsa callout_schedule(&sc->sc_c, 1); 1514202181Sthompsa } 1515202181Sthompsa } 1516202181Sthompsa /* FALLTHROUGH */ 1517202181Sthompsa case USB_ST_SETUP: 1518202181Sthompsatr_setup: 1519202181Sthompsa usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer)); 1520202181Sthompsa usbd_transfer_submit(xfer); 1521202181Sthompsa break; 1522202181Sthompsa default: 1523202181Sthompsa UHSO_DPRINTF(0, "error: %s\n", usbd_errstr(error)); 1524202181Sthompsa if (error == USB_ERR_CANCELLED) 1525202181Sthompsa break; 1526202181Sthompsa usbd_xfer_set_stall(xfer); 1527202181Sthompsa goto tr_setup; 1528202181Sthompsa } 1529202181Sthompsa} 1530202181Sthompsa 1531202181Sthompsa/* 1532202243Sthompsa * Deferred RX processing, called with mutex locked. 1533202243Sthompsa * 1534202270Sthompsa * Each frame we receive might contain several small ip-packets as well 1535202243Sthompsa * as partial ip-packets. We need to separate/assemble them into individual 1536202243Sthompsa * packets before sending them to the ip-layer. 1537202181Sthompsa */ 1538202181Sthompsastatic void 1539202181Sthompsauhso_if_rxflush(void *arg) 1540202181Sthompsa{ 1541202181Sthompsa struct uhso_softc *sc = arg; 1542202181Sthompsa struct ifnet *ifp = sc->sc_ifp; 1543202181Sthompsa uint8_t *cp; 1544202181Sthompsa struct mbuf *m, *m0, *mwait; 1545202181Sthompsa struct ip *ip; 1546202181Sthompsa#ifdef INET6 1547202181Sthompsa struct ip6_hdr *ip6; 1548202181Sthompsa#endif 1549202181Sthompsa uint16_t iplen; 1550202181Sthompsa int len, isr; 1551202181Sthompsa 1552202181Sthompsa m = NULL; 1553202181Sthompsa mwait = sc->sc_mwait; 1554202181Sthompsa for (;;) { 1555202181Sthompsa if (m == NULL) { 1556202181Sthompsa _IF_DEQUEUE(&sc->sc_rxq, m); 1557202181Sthompsa if (m == NULL) 1558202181Sthompsa break; 1559202243Sthompsa UHSO_DPRINTF(3, "dequeue m=%p, len=%d\n", m, m->m_len); 1560202181Sthompsa } 1561202181Sthompsa mtx_unlock(&sc->sc_mtx); 1562202181Sthompsa 1563202181Sthompsa /* Do we have a partial packet waiting? */ 1564202181Sthompsa if (mwait != NULL) { 1565202181Sthompsa m0 = mwait; 1566202181Sthompsa mwait = NULL; 1567202181Sthompsa 1568202243Sthompsa UHSO_DPRINTF(3, "partial m0=%p(%d), concat w/ m=%p(%d)\n", 1569202181Sthompsa m0, m0->m_len, m, m->m_len); 1570202181Sthompsa len = m->m_len + m0->m_len; 1571202181Sthompsa 1572202181Sthompsa /* Concat mbufs and fix headers */ 1573202181Sthompsa m_cat(m0, m); 1574202181Sthompsa m0->m_pkthdr.len = len; 1575202181Sthompsa m->m_flags &= ~M_PKTHDR; 1576202181Sthompsa 1577202181Sthompsa m = m_pullup(m0, sizeof(struct ip)); 1578202181Sthompsa if (m == NULL) { 1579202181Sthompsa ifp->if_ierrors++; 1580202181Sthompsa UHSO_DPRINTF(0, "m_pullup failed\n"); 1581202181Sthompsa mtx_lock(&sc->sc_mtx); 1582202181Sthompsa continue; 1583202181Sthompsa } 1584202243Sthompsa UHSO_DPRINTF(3, "Constructed mbuf=%p, len=%d\n", 1585202181Sthompsa m, m->m_pkthdr.len); 1586202181Sthompsa } 1587202181Sthompsa 1588202181Sthompsa cp = mtod(m, uint8_t *); 1589202181Sthompsa ip = (struct ip *)cp; 1590202181Sthompsa#ifdef INET6 1591202181Sthompsa ip6 = (struct ip6_hdr *)cp; 1592202181Sthompsa#endif 1593202181Sthompsa 1594202181Sthompsa /* Check for IPv4 */ 1595202181Sthompsa if (ip->ip_v == IPVERSION) { 1596202181Sthompsa iplen = htons(ip->ip_len); 1597202181Sthompsa isr = NETISR_IP; 1598202181Sthompsa } 1599202181Sthompsa#ifdef INET6 1600202181Sthompsa /* Check for IPv6 */ 1601202181Sthompsa else if ((ip6->ip6_vfc & IPV6_VERSION_MASK) == IPV6_VERSION) { 1602202181Sthompsa iplen = htons(ip6->ip6_plen); 1603202181Sthompsa isr = NETISR_IPV6; 1604202181Sthompsa } 1605202181Sthompsa#endif 1606202181Sthompsa else { 1607202181Sthompsa UHSO_DPRINTF(0, "got unexpected ip version %d, " 1608202181Sthompsa "m=%p, len=%d\n", (*cp & 0xf0) >> 4, m, m->m_len); 1609202181Sthompsa ifp->if_ierrors++; 1610202181Sthompsa UHSO_HEXDUMP(cp, 4); 1611202181Sthompsa m_freem(m); 1612202181Sthompsa m = NULL; 1613202181Sthompsa mtx_lock(&sc->sc_mtx); 1614202181Sthompsa continue; 1615202181Sthompsa } 1616202181Sthompsa 1617202181Sthompsa if (iplen == 0) { 1618202181Sthompsa UHSO_DPRINTF(0, "Zero IP length\n"); 1619202181Sthompsa ifp->if_ierrors++; 1620202181Sthompsa m_freem(m); 1621202181Sthompsa m = NULL; 1622202181Sthompsa mtx_lock(&sc->sc_mtx); 1623202181Sthompsa continue; 1624202181Sthompsa } 1625202181Sthompsa 1626202243Sthompsa UHSO_DPRINTF(3, "m=%p, len=%d, cp=%p, iplen=%d\n", 1627202181Sthompsa m, m->m_pkthdr.len, cp, iplen); 1628202181Sthompsa 1629202181Sthompsa m0 = NULL; 1630202181Sthompsa 1631202181Sthompsa /* More IP packets in this mbuf */ 1632202181Sthompsa if (iplen < m->m_pkthdr.len) { 1633202181Sthompsa m0 = m; 1634202181Sthompsa 1635202181Sthompsa /* 1636202181Sthompsa * Allocate a new mbuf for this IP packet and 1637202181Sthompsa * copy the IP-packet into it. 1638202181Sthompsa */ 1639202181Sthompsa m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR); 1640202181Sthompsa bcopy(mtod(m0, uint8_t *), mtod(m, uint8_t *), iplen); 1641202181Sthompsa m->m_pkthdr.len = m->m_len = iplen; 1642202181Sthompsa 1643202181Sthompsa /* Adjust the size of the original mbuf */ 1644202181Sthompsa m_adj(m0, iplen); 1645202181Sthompsa m0 = m_defrag(m0, M_WAIT); 1646202181Sthompsa 1647202243Sthompsa UHSO_DPRINTF(3, "New mbuf=%p, len=%d/%d, m0=%p, " 1648202181Sthompsa "m0_len=%d/%d\n", m, m->m_pkthdr.len, m->m_len, 1649202181Sthompsa m0, m0->m_pkthdr.len, m0->m_len); 1650202181Sthompsa } 1651202181Sthompsa else if (iplen > m->m_pkthdr.len) { 1652202243Sthompsa UHSO_DPRINTF(3, "Deferred mbuf=%p, len=%d\n", 1653202181Sthompsa m, m->m_pkthdr.len); 1654202181Sthompsa mwait = m; 1655202181Sthompsa m = NULL; 1656202181Sthompsa mtx_lock(&sc->sc_mtx); 1657202181Sthompsa continue; 1658202181Sthompsa } 1659202181Sthompsa 1660202181Sthompsa ifp->if_ipackets++; 1661202181Sthompsa m->m_pkthdr.rcvif = ifp; 1662202181Sthompsa 1663202181Sthompsa /* Dispatch to IP layer */ 1664202181Sthompsa BPF_MTAP(sc->sc_ifp, m); 1665202181Sthompsa netisr_dispatch(isr, m); 1666202181Sthompsa m = m0 != NULL ? m0 : NULL; 1667202181Sthompsa mtx_lock(&sc->sc_mtx); 1668202181Sthompsa } 1669202181Sthompsa sc->sc_mwait = mwait; 1670202181Sthompsa} 1671202181Sthompsa 1672202181Sthompsastatic void 1673202181Sthompsauhso_ifnet_write_callback(struct usb_xfer *xfer, usb_error_t error) 1674202181Sthompsa{ 1675202181Sthompsa struct uhso_softc *sc = usbd_xfer_softc(xfer); 1676202181Sthompsa struct ifnet *ifp = sc->sc_ifp; 1677202181Sthompsa struct usb_page_cache *pc; 1678202181Sthompsa struct mbuf *m; 1679202181Sthompsa int actlen; 1680202181Sthompsa 1681202181Sthompsa usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL); 1682202181Sthompsa 1683202181Sthompsa UHSO_DPRINTF(3, "status %d, actlen=%d\n", USB_GET_STATE(xfer), actlen); 1684202181Sthompsa 1685202181Sthompsa switch (USB_GET_STATE(xfer)) { 1686202181Sthompsa case USB_ST_TRANSFERRED: 1687202181Sthompsa ifp->if_opackets++; 1688202181Sthompsa ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 1689202181Sthompsa case USB_ST_SETUP: 1690202181Sthompsatr_setup: 1691202181Sthompsa IFQ_DRV_DEQUEUE(&ifp->if_snd, m); 1692202181Sthompsa if (m == NULL) 1693202181Sthompsa break; 1694202181Sthompsa 1695202181Sthompsa ifp->if_drv_flags |= IFF_DRV_OACTIVE; 1696202181Sthompsa 1697202181Sthompsa if (m->m_pkthdr.len > MCLBYTES) 1698202181Sthompsa m->m_pkthdr.len = MCLBYTES; 1699202181Sthompsa 1700202181Sthompsa usbd_xfer_set_frame_len(xfer, 0, m->m_pkthdr.len); 1701202181Sthompsa pc = usbd_xfer_get_frame(xfer, 0); 1702202181Sthompsa usbd_m_copy_in(pc, 0, m, 0, m->m_pkthdr.len); 1703202181Sthompsa usbd_transfer_submit(xfer); 1704202181Sthompsa 1705202181Sthompsa BPF_MTAP(ifp, m); 1706202181Sthompsa m_freem(m); 1707202181Sthompsa break; 1708202181Sthompsa default: 1709202181Sthompsa UHSO_DPRINTF(0, "error: %s\n", usbd_errstr(error)); 1710202181Sthompsa if (error == USB_ERR_CANCELLED) 1711202181Sthompsa break; 1712202181Sthompsa usbd_xfer_set_stall(xfer); 1713202181Sthompsa goto tr_setup; 1714202181Sthompsa } 1715202181Sthompsa} 1716202181Sthompsa 1717202181Sthompsastatic int 1718202181Sthompsauhso_if_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 1719202181Sthompsa{ 1720202181Sthompsa struct uhso_softc *sc; 1721202181Sthompsa 1722202181Sthompsa sc = ifp->if_softc; 1723202181Sthompsa 1724202181Sthompsa switch (cmd) { 1725202181Sthompsa case SIOCSIFFLAGS: 1726202181Sthompsa if (ifp->if_flags & IFF_UP) { 1727202181Sthompsa if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) { 1728202181Sthompsa uhso_if_init(sc); 1729202181Sthompsa } 1730202181Sthompsa } 1731202181Sthompsa else { 1732202181Sthompsa if (ifp->if_drv_flags & IFF_DRV_RUNNING) { 1733202181Sthompsa mtx_lock(&sc->sc_mtx); 1734202181Sthompsa uhso_if_stop(sc); 1735202181Sthompsa mtx_unlock(&sc->sc_mtx); 1736202181Sthompsa } 1737202181Sthompsa } 1738202181Sthompsa break; 1739202181Sthompsa case SIOCSIFADDR: 1740202181Sthompsa case SIOCSIFDSTADDR: 1741202181Sthompsa case SIOCADDMULTI: 1742202181Sthompsa case SIOCDELMULTI: 1743202181Sthompsa break; 1744202181Sthompsa default: 1745202181Sthompsa return (EINVAL); 1746202181Sthompsa } 1747202181Sthompsa return (0); 1748202181Sthompsa} 1749202181Sthompsa 1750202181Sthompsastatic void 1751202181Sthompsauhso_if_init(void *priv) 1752202181Sthompsa{ 1753202181Sthompsa struct uhso_softc *sc = priv; 1754202181Sthompsa struct ifnet *ifp = sc->sc_ifp; 1755202181Sthompsa 1756202181Sthompsa mtx_lock(&sc->sc_mtx); 1757202181Sthompsa uhso_if_stop(sc); 1758202181Sthompsa ifp = sc->sc_ifp; 1759202181Sthompsa ifp->if_flags |= IFF_UP; 1760202181Sthompsa ifp->if_drv_flags |= IFF_DRV_RUNNING; 1761202181Sthompsa mtx_unlock(&sc->sc_mtx); 1762202181Sthompsa 1763202243Sthompsa UHSO_DPRINTF(2, "ifnet initialized\n"); 1764202181Sthompsa} 1765202181Sthompsa 1766202181Sthompsastatic int 1767202181Sthompsauhso_if_output(struct ifnet *ifp, struct mbuf *m0, struct sockaddr *dst, 1768202181Sthompsa struct route *ro) 1769202181Sthompsa{ 1770202181Sthompsa int error; 1771202181Sthompsa 1772202181Sthompsa /* Only IPv4/6 support */ 1773202181Sthompsa if (dst->sa_family != AF_INET 1774202181Sthompsa#ifdef INET6 1775202181Sthompsa && dst->sa_family != AF_INET6 1776202181Sthompsa#endif 1777202181Sthompsa ) { 1778202181Sthompsa return (EAFNOSUPPORT); 1779202181Sthompsa } 1780202181Sthompsa 1781202181Sthompsa error = (ifp->if_transmit)(ifp, m0); 1782202181Sthompsa if (error) { 1783202181Sthompsa ifp->if_oerrors++; 1784202181Sthompsa return (ENOBUFS); 1785202181Sthompsa } 1786202181Sthompsa ifp->if_opackets++; 1787202181Sthompsa return (0); 1788202181Sthompsa} 1789202181Sthompsa 1790202181Sthompsastatic void 1791202181Sthompsauhso_if_start(struct ifnet *ifp) 1792202181Sthompsa{ 1793202181Sthompsa struct uhso_softc *sc = ifp->if_softc; 1794202181Sthompsa 1795202181Sthompsa if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) { 1796202181Sthompsa UHSO_DPRINTF(1, "Not running\n"); 1797202181Sthompsa return; 1798202181Sthompsa } 1799202181Sthompsa 1800202181Sthompsa mtx_lock(&sc->sc_mtx); 1801202181Sthompsa usbd_transfer_start(sc->sc_if_xfer[UHSO_IFNET_READ]); 1802202181Sthompsa usbd_transfer_start(sc->sc_if_xfer[UHSO_IFNET_WRITE]); 1803202181Sthompsa mtx_unlock(&sc->sc_mtx); 1804202181Sthompsa UHSO_DPRINTF(3, "interface started\n"); 1805202181Sthompsa} 1806202181Sthompsa 1807202181Sthompsastatic void 1808202181Sthompsauhso_if_stop(struct uhso_softc *sc) 1809202181Sthompsa{ 1810202181Sthompsa 1811202181Sthompsa usbd_transfer_stop(sc->sc_if_xfer[UHSO_IFNET_READ]); 1812202181Sthompsa usbd_transfer_stop(sc->sc_if_xfer[UHSO_IFNET_WRITE]); 1813202181Sthompsa sc->sc_ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE); 1814202181Sthompsa} 1815