uhso.c revision 243857
1202181Sthompsa/*- 2210275Sthompsa * Copyright (c) 2010 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 243857 2012-12-04 09:32:43Z glebius $"); 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> 45210275Sthompsa#include <sys/limits.h> 46202181Sthompsa 47202181Sthompsa#include <machine/bus.h> 48202181Sthompsa 49202181Sthompsa#include <net/if.h> 50202181Sthompsa#include <net/if_types.h> 51202181Sthompsa#include <net/netisr.h> 52202181Sthompsa#include <net/bpf.h> 53202181Sthompsa#include <netinet/in.h> 54202181Sthompsa#include <netinet/ip.h> 55202181Sthompsa#include <netinet/ip6.h> 56202181Sthompsa 57202181Sthompsa#include <dev/usb/usb.h> 58202181Sthompsa#include <dev/usb/usbdi.h> 59202181Sthompsa#include <dev/usb/usbdi_util.h> 60202181Sthompsa#include <dev/usb/usb_cdc.h> 61202181Sthompsa#include "usbdevs.h" 62202181Sthompsa#define USB_DEBUG_VAR uhso_debug 63202181Sthompsa#include <dev/usb/usb_debug.h> 64202181Sthompsa#include <dev/usb/usb_process.h> 65202181Sthompsa#include <dev/usb/usb_busdma.h> 66202181Sthompsa#include <dev/usb/usb_msctest.h> 67202181Sthompsa 68212136Sthompsa#include <dev/usb/serial/usb_serial.h> 69212136Sthompsa 70202181Sthompsastruct uhso_tty { 71202181Sthompsa struct uhso_softc *ht_sc; 72202181Sthompsa struct usb_xfer *ht_xfer[3]; 73202243Sthompsa int ht_muxport; /* Mux. port no */ 74202181Sthompsa int ht_open; 75202181Sthompsa char ht_name[32]; 76202181Sthompsa}; 77202181Sthompsa 78202181Sthompsastruct uhso_softc { 79202181Sthompsa device_t sc_dev; 80202181Sthompsa struct usb_device *sc_udev; 81202181Sthompsa struct mtx sc_mtx; 82202243Sthompsa uint32_t sc_type; /* Interface definition */ 83210275Sthompsa int sc_radio; 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 */ 158210275Sthompsa#define UHSO_PORT_TYPE_UNKNOWN 0x00 159202181Sthompsa#define UHSO_PORT_TYPE_CTL 0x01 160202181Sthompsa#define UHSO_PORT_TYPE_APP 0x02 161202181Sthompsa#define UHSO_PORT_TYPE_APP2 0x03 162202181Sthompsa#define UHSO_PORT_TYPE_MODEM 0x04 163202181Sthompsa#define UHSO_PORT_TYPE_NETWORK 0x05 164202181Sthompsa#define UHSO_PORT_TYPE_DIAG 0x06 165202181Sthompsa#define UHSO_PORT_TYPE_DIAG2 0x07 166202181Sthompsa#define UHSO_PORT_TYPE_GPS 0x08 167202181Sthompsa#define UHSO_PORT_TYPE_GPSCTL 0x09 168202181Sthompsa#define UHSO_PORT_TYPE_PCSC 0x0a 169202181Sthompsa#define UHSO_PORT_TYPE_MSD 0x0b 170202181Sthompsa#define UHSO_PORT_TYPE_VOICE 0x0c 171202181Sthompsa#define UHSO_PORT_TYPE_MAX 0x0c 172202181Sthompsa 173202181Sthompsastatic eventhandler_tag uhso_etag; 174202181Sthompsa 175202181Sthompsa/* Overall port type */ 176202181Sthompsastatic char *uhso_port[] = { 177202181Sthompsa "Unknown", 178202181Sthompsa "Serial", 179202181Sthompsa "Network", 180202181Sthompsa "Network/Serial" 181202181Sthompsa}; 182202181Sthompsa 183202243Sthompsa/* 184202243Sthompsa * Map between interface port type read from device and description type. 185202243Sthompsa * The position in this array is a direct map to the auto config 186202243Sthompsa * descriptor values. 187202243Sthompsa */ 188202243Sthompsastatic unsigned char uhso_port_map[] = { 189210275Sthompsa UHSO_PORT_TYPE_UNKNOWN, 190202181Sthompsa UHSO_PORT_TYPE_DIAG, 191202181Sthompsa UHSO_PORT_TYPE_GPS, 192202181Sthompsa UHSO_PORT_TYPE_GPSCTL, 193202181Sthompsa UHSO_PORT_TYPE_APP, 194202181Sthompsa UHSO_PORT_TYPE_APP2, 195202181Sthompsa UHSO_PORT_TYPE_CTL, 196202181Sthompsa UHSO_PORT_TYPE_NETWORK, 197202181Sthompsa UHSO_PORT_TYPE_MODEM, 198202181Sthompsa UHSO_PORT_TYPE_MSD, 199202181Sthompsa UHSO_PORT_TYPE_PCSC, 200202181Sthompsa UHSO_PORT_TYPE_VOICE 201202181Sthompsa}; 202202181Sthompsastatic char uhso_port_map_max = sizeof(uhso_port_map) / sizeof(char); 203202181Sthompsa 204202243Sthompsastatic unsigned char uhso_mux_port_map[] = { 205202181Sthompsa UHSO_PORT_TYPE_CTL, 206202181Sthompsa UHSO_PORT_TYPE_APP, 207202181Sthompsa UHSO_PORT_TYPE_PCSC, 208202181Sthompsa UHSO_PORT_TYPE_GPS, 209202181Sthompsa UHSO_PORT_TYPE_APP2 210202181Sthompsa}; 211202181Sthompsa 212202181Sthompsastatic char *uhso_port_type[] = { 213202243Sthompsa "Unknown", /* Not a valid port */ 214202181Sthompsa "Control", 215202181Sthompsa "Application", 216202181Sthompsa "Application (Secondary)", 217202181Sthompsa "Modem", 218202181Sthompsa "Network", 219202181Sthompsa "Diagnostic", 220202181Sthompsa "Diagnostic (Secondary)", 221202181Sthompsa "GPS", 222202181Sthompsa "GPS Control", 223202181Sthompsa "PC Smartcard", 224202181Sthompsa "MSD", 225202181Sthompsa "Voice", 226202181Sthompsa}; 227202181Sthompsa 228202181Sthompsastatic char *uhso_port_type_sysctl[] = { 229202181Sthompsa "unknown", 230202181Sthompsa "control", 231202181Sthompsa "application", 232202181Sthompsa "application", 233202181Sthompsa "modem", 234202181Sthompsa "network", 235202181Sthompsa "diagnostic", 236202181Sthompsa "diagnostic", 237202181Sthompsa "gps", 238202181Sthompsa "gps_control", 239202181Sthompsa "pcsc", 240202181Sthompsa "msd", 241202181Sthompsa "voice", 242202181Sthompsa}; 243202181Sthompsa 244202181Sthompsa#define UHSO_STATIC_IFACE 0x01 245202181Sthompsa#define UHSO_AUTO_IFACE 0x02 246202181Sthompsa 247210275Sthompsa/* ifnet device unit allocations */ 248210275Sthompsastatic struct unrhdr *uhso_ifnet_unit = NULL; 249210275Sthompsa 250223486Shselaskystatic const STRUCT_USB_HOST_ID uhso_devs[] = { 251202181Sthompsa#define UHSO_DEV(v,p,i) { USB_VPI(USB_VENDOR_##v, USB_PRODUCT_##v##_##p, i) } 252225037Shselasky /* Option GlobeTrotter MAX 7.2 with upgraded firmware */ 253225037Shselasky UHSO_DEV(OPTION, GTMAX72, UHSO_STATIC_IFACE), 254202181Sthompsa /* Option GlobeSurfer iCON 7.2 */ 255202181Sthompsa UHSO_DEV(OPTION, GSICON72, UHSO_STATIC_IFACE), 256202181Sthompsa /* Option iCON 225 */ 257202181Sthompsa UHSO_DEV(OPTION, GTHSDPA, UHSO_STATIC_IFACE), 258202181Sthompsa /* Option GlobeSurfer iCON HSUPA */ 259202181Sthompsa UHSO_DEV(OPTION, GSICONHSUPA, UHSO_STATIC_IFACE), 260202181Sthompsa /* Option GlobeTrotter HSUPA */ 261202181Sthompsa UHSO_DEV(OPTION, GTHSUPA, UHSO_STATIC_IFACE), 262202181Sthompsa /* GE40x */ 263202181Sthompsa UHSO_DEV(OPTION, GE40X, UHSO_AUTO_IFACE), 264202181Sthompsa UHSO_DEV(OPTION, GE40X_1, UHSO_AUTO_IFACE), 265202181Sthompsa UHSO_DEV(OPTION, GE40X_2, UHSO_AUTO_IFACE), 266202181Sthompsa UHSO_DEV(OPTION, GE40X_3, UHSO_AUTO_IFACE), 267202181Sthompsa /* Option GlobeSurfer iCON 401 */ 268202181Sthompsa UHSO_DEV(OPTION, ICON401, UHSO_AUTO_IFACE), 269202181Sthompsa /* Option GlobeTrotter Module 382 */ 270202181Sthompsa UHSO_DEV(OPTION, GMT382, UHSO_AUTO_IFACE), 271202181Sthompsa /* Option iCON EDGE */ 272202181Sthompsa UHSO_DEV(OPTION, ICONEDGE, UHSO_STATIC_IFACE), 273202181Sthompsa /* Option Module HSxPA */ 274202181Sthompsa UHSO_DEV(OPTION, MODHSXPA, UHSO_STATIC_IFACE), 275202181Sthompsa /* Option iCON 321 */ 276202181Sthompsa UHSO_DEV(OPTION, ICON321, UHSO_STATIC_IFACE), 277202181Sthompsa /* Option iCON 322 */ 278202243Sthompsa UHSO_DEV(OPTION, GTICON322, UHSO_STATIC_IFACE), 279202243Sthompsa /* Option iCON 505 */ 280202243Sthompsa UHSO_DEV(OPTION, ICON505, UHSO_AUTO_IFACE), 281210275Sthompsa /* Option iCON 452 */ 282210275Sthompsa UHSO_DEV(OPTION, ICON505, UHSO_AUTO_IFACE), 283202181Sthompsa#undef UHSO_DEV 284202181Sthompsa}; 285202181Sthompsa 286227309Sedstatic SYSCTL_NODE(_hw_usb, OID_AUTO, uhso, CTLFLAG_RW, 0, "USB uhso"); 287202243Sthompsastatic int uhso_autoswitch = 1; 288202243SthompsaSYSCTL_INT(_hw_usb_uhso, OID_AUTO, auto_switch, CTLFLAG_RW, 289202243Sthompsa &uhso_autoswitch, 0, "Automatically switch to modem mode"); 290202181Sthompsa 291202181Sthompsa#ifdef USB_DEBUG 292202181Sthompsa#ifdef UHSO_DEBUG 293202181Sthompsastatic int uhso_debug = UHSO_DEBUG; 294202181Sthompsa#else 295202181Sthompsastatic int uhso_debug = -1; 296202181Sthompsa#endif 297202181Sthompsa 298202181SthompsaSYSCTL_INT(_hw_usb_uhso, OID_AUTO, debug, CTLFLAG_RW, 299202181Sthompsa &uhso_debug, 0, "Debug level"); 300202181Sthompsa 301202181Sthompsa#define UHSO_DPRINTF(n, x, ...) {\ 302202181Sthompsa if (uhso_debug >= n) {\ 303202181Sthompsa printf("%s: " x, __func__, ##__VA_ARGS__);\ 304202181Sthompsa }\ 305202181Sthompsa} 306202181Sthompsa#else 307202181Sthompsa#define UHSO_DPRINTF(n, x, ...) 308202181Sthompsa#endif 309202181Sthompsa 310202181Sthompsa#ifdef UHSO_DEBUG_HEXDUMP 311202181Sthompsa# define UHSO_HEXDUMP(_buf, _len) do { \ 312202181Sthompsa { \ 313202181Sthompsa size_t __tmp; \ 314202181Sthompsa const char *__buf = (const char *)_buf; \ 315202181Sthompsa for (__tmp = 0; __tmp < _len; __tmp++) \ 316202181Sthompsa printf("%02hhx ", *__buf++); \ 317202181Sthompsa printf("\n"); \ 318202181Sthompsa } \ 319202181Sthompsa} while(0) 320202181Sthompsa#else 321202181Sthompsa# define UHSO_HEXDUMP(_buf, _len) 322202181Sthompsa#endif 323202181Sthompsa 324202181Sthompsaenum { 325202181Sthompsa UHSO_MUX_ENDPT_INTR = 0, 326202181Sthompsa UHSO_MUX_ENDPT_MAX 327202181Sthompsa}; 328202181Sthompsa 329202181Sthompsaenum { 330202181Sthompsa UHSO_CTRL_READ = 0, 331202181Sthompsa UHSO_CTRL_WRITE, 332202181Sthompsa UHSO_CTRL_MAX 333202181Sthompsa}; 334202181Sthompsa 335202181Sthompsaenum { 336202181Sthompsa UHSO_IFNET_READ = 0, 337202181Sthompsa UHSO_IFNET_WRITE, 338202181Sthompsa UHSO_IFNET_MAX 339202181Sthompsa}; 340202181Sthompsa 341202181Sthompsaenum { 342202181Sthompsa UHSO_BULK_ENDPT_READ = 0, 343202181Sthompsa UHSO_BULK_ENDPT_WRITE, 344202181Sthompsa UHSO_BULK_ENDPT_INTR, 345202181Sthompsa UHSO_BULK_ENDPT_MAX 346202181Sthompsa}; 347202181Sthompsa 348202181Sthompsastatic usb_callback_t uhso_mux_intr_callback; 349202181Sthompsastatic usb_callback_t uhso_mux_read_callback; 350202181Sthompsastatic usb_callback_t uhso_mux_write_callback; 351202181Sthompsastatic usb_callback_t uhso_bs_read_callback; 352202181Sthompsastatic usb_callback_t uhso_bs_write_callback; 353202181Sthompsastatic usb_callback_t uhso_bs_intr_callback; 354202181Sthompsastatic usb_callback_t uhso_ifnet_read_callback; 355202181Sthompsastatic usb_callback_t uhso_ifnet_write_callback; 356202181Sthompsa 357202243Sthompsa/* Config used for the default control pipes */ 358202181Sthompsastatic const struct usb_config uhso_ctrl_config[UHSO_CTRL_MAX] = { 359202181Sthompsa [UHSO_CTRL_READ] = { 360202181Sthompsa .type = UE_CONTROL, 361202181Sthompsa .endpoint = 0x00, 362202181Sthompsa .direction = UE_DIR_ANY, 363202181Sthompsa .flags = { .pipe_bof = 1, .short_xfer_ok = 1 }, 364202181Sthompsa .bufsize = sizeof(struct usb_device_request) + 1024, 365202181Sthompsa .callback = &uhso_mux_read_callback 366202181Sthompsa }, 367202181Sthompsa 368202181Sthompsa [UHSO_CTRL_WRITE] = { 369202181Sthompsa .type = UE_CONTROL, 370202181Sthompsa .endpoint = 0x00, 371202181Sthompsa .direction = UE_DIR_ANY, 372202181Sthompsa .flags = { .pipe_bof = 1, .force_short_xfer = 1 }, 373202181Sthompsa .bufsize = sizeof(struct usb_device_request) + 1024, 374202181Sthompsa .timeout = 1000, 375202181Sthompsa .callback = &uhso_mux_write_callback 376202181Sthompsa } 377202181Sthompsa}; 378202181Sthompsa 379202243Sthompsa/* Config for the multiplexed serial ports */ 380202181Sthompsastatic const struct usb_config uhso_mux_config[UHSO_MUX_ENDPT_MAX] = { 381202181Sthompsa [UHSO_MUX_ENDPT_INTR] = { 382202181Sthompsa .type = UE_INTERRUPT, 383202181Sthompsa .endpoint = UE_ADDR_ANY, 384202181Sthompsa .direction = UE_DIR_IN, 385202181Sthompsa .flags = { .short_xfer_ok = 1 }, 386202181Sthompsa .bufsize = 0, 387202181Sthompsa .callback = &uhso_mux_intr_callback, 388202181Sthompsa } 389202181Sthompsa}; 390202181Sthompsa 391202243Sthompsa/* Config for the raw IP-packet interface */ 392202181Sthompsastatic const struct usb_config uhso_ifnet_config[UHSO_IFNET_MAX] = { 393202181Sthompsa [UHSO_IFNET_READ] = { 394202181Sthompsa .type = UE_BULK, 395202181Sthompsa .endpoint = UE_ADDR_ANY, 396202181Sthompsa .direction = UE_DIR_IN, 397202181Sthompsa .flags = { .pipe_bof = 1, .short_xfer_ok = 1 }, 398202181Sthompsa .bufsize = MCLBYTES, 399202181Sthompsa .callback = &uhso_ifnet_read_callback 400202181Sthompsa }, 401202181Sthompsa [UHSO_IFNET_WRITE] = { 402202181Sthompsa .type = UE_BULK, 403202181Sthompsa .endpoint = UE_ADDR_ANY, 404202181Sthompsa .direction = UE_DIR_OUT, 405202181Sthompsa .flags = { .pipe_bof = 1, .force_short_xfer = 1 }, 406202181Sthompsa .bufsize = MCLBYTES, 407202181Sthompsa .timeout = 5 * USB_MS_HZ, 408202181Sthompsa .callback = &uhso_ifnet_write_callback 409202181Sthompsa } 410202181Sthompsa}; 411202181Sthompsa 412202243Sthompsa/* Config for interfaces with normal bulk serial ports */ 413202181Sthompsastatic const struct usb_config uhso_bs_config[UHSO_BULK_ENDPT_MAX] = { 414202181Sthompsa [UHSO_BULK_ENDPT_READ] = { 415202181Sthompsa .type = UE_BULK, 416202181Sthompsa .endpoint = UE_ADDR_ANY, 417202181Sthompsa .direction = UE_DIR_IN, 418202181Sthompsa .flags = { .pipe_bof = 1, .short_xfer_ok = 1 }, 419202181Sthompsa .bufsize = 4096, 420202181Sthompsa .callback = &uhso_bs_read_callback 421202181Sthompsa }, 422202181Sthompsa 423202181Sthompsa [UHSO_BULK_ENDPT_WRITE] = { 424202181Sthompsa .type = UE_BULK, 425202181Sthompsa .endpoint = UE_ADDR_ANY, 426202181Sthompsa .direction = UE_DIR_OUT, 427202181Sthompsa .flags = { .pipe_bof = 1, .force_short_xfer = 1 }, 428202181Sthompsa .bufsize = 8192, 429202181Sthompsa .callback = &uhso_bs_write_callback 430202181Sthompsa }, 431202181Sthompsa 432202181Sthompsa [UHSO_BULK_ENDPT_INTR] = { 433202181Sthompsa .type = UE_INTERRUPT, 434202181Sthompsa .endpoint = UE_ADDR_ANY, 435202181Sthompsa .direction = UE_DIR_IN, 436202181Sthompsa .flags = { .short_xfer_ok = 1 }, 437202181Sthompsa .bufsize = 0, 438202181Sthompsa .callback = &uhso_bs_intr_callback, 439202181Sthompsa } 440202181Sthompsa}; 441202181Sthompsa 442202243Sthompsastatic int uhso_probe_iface(struct uhso_softc *, int, 443210275Sthompsa int (*probe)(struct usb_device *, int)); 444210275Sthompsastatic int uhso_probe_iface_auto(struct usb_device *, int); 445210275Sthompsastatic int uhso_probe_iface_static(struct usb_device *, int); 446202243Sthompsastatic int uhso_attach_muxserial(struct uhso_softc *, struct usb_interface *, 447202181Sthompsa int type); 448202243Sthompsastatic int uhso_attach_bulkserial(struct uhso_softc *, struct usb_interface *, 449202181Sthompsa int type); 450202243Sthompsastatic int uhso_attach_ifnet(struct uhso_softc *, struct usb_interface *, 451202181Sthompsa int type); 452202181Sthompsastatic void uhso_test_autoinst(void *, struct usb_device *, 453202181Sthompsa struct usb_attach_arg *); 454202243Sthompsastatic int uhso_driver_loaded(struct module *, int, void *); 455210275Sthompsastatic int uhso_radio_sysctl(SYSCTL_HANDLER_ARGS); 456210275Sthompsastatic int uhso_radio_ctrl(struct uhso_softc *, int); 457202181Sthompsa 458239180Shselaskystatic void uhso_free(struct ucom_softc *); 459202181Sthompsastatic void uhso_ucom_start_read(struct ucom_softc *); 460202181Sthompsastatic void uhso_ucom_stop_read(struct ucom_softc *); 461202181Sthompsastatic void uhso_ucom_start_write(struct ucom_softc *); 462202181Sthompsastatic void uhso_ucom_stop_write(struct ucom_softc *); 463202181Sthompsastatic void uhso_ucom_cfg_get_status(struct ucom_softc *, uint8_t *, uint8_t *); 464202181Sthompsastatic void uhso_ucom_cfg_set_dtr(struct ucom_softc *, uint8_t); 465202181Sthompsastatic void uhso_ucom_cfg_set_rts(struct ucom_softc *, uint8_t); 466202181Sthompsastatic void uhso_if_init(void *); 467202181Sthompsastatic void uhso_if_start(struct ifnet *); 468202181Sthompsastatic void uhso_if_stop(struct uhso_softc *); 469202243Sthompsastatic int uhso_if_ioctl(struct ifnet *, u_long, caddr_t); 470202243Sthompsastatic int uhso_if_output(struct ifnet *, struct mbuf *, struct sockaddr *, 471202181Sthompsa struct route *); 472202181Sthompsastatic void uhso_if_rxflush(void *); 473202181Sthompsa 474202181Sthompsastatic device_probe_t uhso_probe; 475202181Sthompsastatic device_attach_t uhso_attach; 476202181Sthompsastatic device_detach_t uhso_detach; 477239299Shselaskystatic void uhso_free_softc(struct uhso_softc *); 478202181Sthompsa 479202181Sthompsastatic device_method_t uhso_methods[] = { 480202181Sthompsa DEVMETHOD(device_probe, uhso_probe), 481202181Sthompsa DEVMETHOD(device_attach, uhso_attach), 482202181Sthompsa DEVMETHOD(device_detach, uhso_detach), 483202181Sthompsa { 0, 0 } 484202181Sthompsa}; 485202181Sthompsa 486202181Sthompsastatic driver_t uhso_driver = { 487233774Shselasky .name = "uhso", 488233774Shselasky .methods = uhso_methods, 489233774Shselasky .size = sizeof(struct uhso_softc) 490202181Sthompsa}; 491202181Sthompsa 492202181Sthompsastatic devclass_t uhso_devclass; 493202181SthompsaDRIVER_MODULE(uhso, uhub, uhso_driver, uhso_devclass, uhso_driver_loaded, 0); 494202181SthompsaMODULE_DEPEND(uhso, ucom, 1, 1, 1); 495202181SthompsaMODULE_DEPEND(uhso, usb, 1, 1, 1); 496202181SthompsaMODULE_VERSION(uhso, 1); 497202181Sthompsa 498202181Sthompsastatic struct ucom_callback uhso_ucom_callback = { 499202181Sthompsa .ucom_cfg_get_status = &uhso_ucom_cfg_get_status, 500202181Sthompsa .ucom_cfg_set_dtr = &uhso_ucom_cfg_set_dtr, 501202181Sthompsa .ucom_cfg_set_rts = &uhso_ucom_cfg_set_rts, 502202181Sthompsa .ucom_start_read = uhso_ucom_start_read, 503202181Sthompsa .ucom_stop_read = uhso_ucom_stop_read, 504202181Sthompsa .ucom_start_write = uhso_ucom_start_write, 505239180Shselasky .ucom_stop_write = uhso_ucom_stop_write, 506239180Shselasky .ucom_free = &uhso_free, 507202181Sthompsa}; 508202181Sthompsa 509202181Sthompsastatic int 510202181Sthompsauhso_probe(device_t self) 511202181Sthompsa{ 512202181Sthompsa struct usb_attach_arg *uaa = device_get_ivars(self); 513210275Sthompsa int error; 514202181Sthompsa 515202181Sthompsa if (uaa->usb_mode != USB_MODE_HOST) 516202181Sthompsa return (ENXIO); 517202181Sthompsa if (uaa->info.bConfigIndex != 0) 518202181Sthompsa return (ENXIO); 519212136Sthompsa if (uaa->info.bDeviceClass != 0xff) 520202181Sthompsa return (ENXIO); 521202181Sthompsa 522210275Sthompsa error = usbd_lookup_id_by_uaa(uhso_devs, sizeof(uhso_devs), uaa); 523210275Sthompsa if (error != 0) 524210275Sthompsa return (error); 525210275Sthompsa 526210275Sthompsa /* 527210275Sthompsa * Probe device to see if we are able to attach 528210275Sthompsa * to this interface or not. 529210275Sthompsa */ 530210275Sthompsa if (USB_GET_DRIVER_INFO(uaa) == UHSO_AUTO_IFACE) { 531210275Sthompsa if (uhso_probe_iface_auto(uaa->device, 532210275Sthompsa uaa->info.bIfaceNum) == 0) 533210275Sthompsa return (ENXIO); 534210275Sthompsa } 535210275Sthompsa return (error); 536202181Sthompsa} 537202181Sthompsa 538202181Sthompsastatic int 539202181Sthompsauhso_attach(device_t self) 540202181Sthompsa{ 541202181Sthompsa struct uhso_softc *sc = device_get_softc(self); 542202181Sthompsa struct usb_attach_arg *uaa = device_get_ivars(self); 543202181Sthompsa struct usb_interface_descriptor *id; 544202181Sthompsa struct sysctl_ctx_list *sctx; 545202181Sthompsa struct sysctl_oid *soid; 546210275Sthompsa struct sysctl_oid *tree = NULL, *tty_node; 547202181Sthompsa struct ucom_softc *ucom; 548202181Sthompsa struct uhso_tty *ht; 549202181Sthompsa int i, error, port; 550202181Sthompsa void *probe_f; 551202181Sthompsa usb_error_t uerr; 552202181Sthompsa char *desc; 553202181Sthompsa 554202181Sthompsa sc->sc_dev = self; 555202181Sthompsa sc->sc_udev = uaa->device; 556202181Sthompsa mtx_init(&sc->sc_mtx, "uhso", NULL, MTX_DEF); 557239180Shselasky ucom_ref(&sc->sc_super_ucom); 558202181Sthompsa 559202181Sthompsa sc->sc_ucom = NULL; 560202181Sthompsa sc->sc_ttys = 0; 561210275Sthompsa sc->sc_radio = 1; 562202181Sthompsa 563202181Sthompsa id = usbd_get_interface_descriptor(uaa->iface); 564202181Sthompsa sc->sc_ctrl_iface_no = id->bInterfaceNumber; 565202181Sthompsa 566202181Sthompsa sc->sc_iface_no = uaa->info.bIfaceNum; 567202181Sthompsa sc->sc_iface_index = uaa->info.bIfaceIndex; 568202181Sthompsa 569202181Sthompsa /* Setup control pipe */ 570202181Sthompsa uerr = usbd_transfer_setup(uaa->device, 571202181Sthompsa &sc->sc_iface_index, sc->sc_ctrl_xfer, 572202181Sthompsa uhso_ctrl_config, UHSO_CTRL_MAX, sc, &sc->sc_mtx); 573202181Sthompsa if (uerr) { 574202181Sthompsa device_printf(self, "Failed to setup control pipe: %s\n", 575202181Sthompsa usbd_errstr(uerr)); 576202181Sthompsa goto out; 577202181Sthompsa } 578202181Sthompsa 579202181Sthompsa if (USB_GET_DRIVER_INFO(uaa) == UHSO_STATIC_IFACE) 580202181Sthompsa probe_f = uhso_probe_iface_static; 581202181Sthompsa else if (USB_GET_DRIVER_INFO(uaa) == UHSO_AUTO_IFACE) 582202181Sthompsa probe_f = uhso_probe_iface_auto; 583202181Sthompsa else 584202181Sthompsa goto out; 585202181Sthompsa 586202181Sthompsa error = uhso_probe_iface(sc, uaa->info.bIfaceNum, probe_f); 587202181Sthompsa if (error != 0) 588202181Sthompsa goto out; 589202181Sthompsa 590202181Sthompsa sctx = device_get_sysctl_ctx(sc->sc_dev); 591202181Sthompsa soid = device_get_sysctl_tree(sc->sc_dev); 592202181Sthompsa 593202181Sthompsa SYSCTL_ADD_STRING(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "type", 594202181Sthompsa CTLFLAG_RD, uhso_port[UHSO_IFACE_PORT(sc->sc_type)], 0, 595202181Sthompsa "Port available at this interface"); 596210275Sthompsa SYSCTL_ADD_PROC(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "radio", 597210275Sthompsa CTLTYPE_INT | CTLFLAG_RW, sc, 0, uhso_radio_sysctl, "I", "Enable radio"); 598202181Sthompsa 599202243Sthompsa /* 600202270Sthompsa * The default interface description on most Option devices isn't 601202243Sthompsa * very helpful. So we skip device_set_usb_desc and set the 602202243Sthompsa * device description manually. 603202243Sthompsa */ 604202243Sthompsa device_set_desc_copy(self, uhso_port_type[UHSO_IFACE_PORT_TYPE(sc->sc_type)]); 605202243Sthompsa /* Announce device */ 606202243Sthompsa device_printf(self, "<%s port> at <%s %s> on %s\n", 607202243Sthompsa uhso_port_type[UHSO_IFACE_PORT_TYPE(sc->sc_type)], 608212136Sthompsa usb_get_manufacturer(uaa->device), 609212136Sthompsa usb_get_product(uaa->device), 610212136Sthompsa device_get_nameunit(device_get_parent(self))); 611202243Sthompsa 612202181Sthompsa if (sc->sc_ttys > 0) { 613202181Sthompsa SYSCTL_ADD_INT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "ports", 614202181Sthompsa CTLFLAG_RD, &sc->sc_ttys, 0, "Number of attached serial ports"); 615202181Sthompsa 616202181Sthompsa tree = SYSCTL_ADD_NODE(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, 617202181Sthompsa "port", CTLFLAG_RD, NULL, "Serial ports"); 618202181Sthompsa } 619202181Sthompsa 620202243Sthompsa /* 621202243Sthompsa * Loop through the number of found TTYs and create sysctl 622202243Sthompsa * nodes for them. 623202243Sthompsa */ 624202181Sthompsa for (i = 0; i < sc->sc_ttys; i++) { 625202181Sthompsa ht = &sc->sc_tty[i]; 626202181Sthompsa ucom = &sc->sc_ucom[i]; 627202181Sthompsa 628202181Sthompsa if (UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_MUX) 629202181Sthompsa port = uhso_mux_port_map[ht->ht_muxport]; 630202181Sthompsa else 631202181Sthompsa port = UHSO_IFACE_PORT_TYPE(sc->sc_type); 632202181Sthompsa 633202181Sthompsa desc = uhso_port_type_sysctl[port]; 634202181Sthompsa 635202181Sthompsa tty_node = SYSCTL_ADD_NODE(sctx, SYSCTL_CHILDREN(tree), OID_AUTO, 636202181Sthompsa desc, CTLFLAG_RD, NULL, ""); 637202181Sthompsa 638202181Sthompsa ht->ht_name[0] = 0; 639202181Sthompsa if (sc->sc_ttys == 1) 640214761Sn_hibma snprintf(ht->ht_name, 32, "cuaU%d", ucom->sc_super->sc_unit); 641202181Sthompsa else { 642202181Sthompsa snprintf(ht->ht_name, 32, "cuaU%d.%d", 643214761Sn_hibma ucom->sc_super->sc_unit, ucom->sc_subunit); 644202181Sthompsa } 645202181Sthompsa 646202181Sthompsa desc = uhso_port_type[port]; 647202181Sthompsa SYSCTL_ADD_STRING(sctx, SYSCTL_CHILDREN(tty_node), OID_AUTO, 648202181Sthompsa "tty", CTLFLAG_RD, ht->ht_name, 0, ""); 649202181Sthompsa SYSCTL_ADD_STRING(sctx, SYSCTL_CHILDREN(tty_node), OID_AUTO, 650202181Sthompsa "desc", CTLFLAG_RD, desc, 0, ""); 651202181Sthompsa 652202181Sthompsa if (bootverbose) 653202181Sthompsa device_printf(sc->sc_dev, 654202181Sthompsa "\"%s\" port at %s\n", desc, ht->ht_name); 655202181Sthompsa } 656202181Sthompsa 657202181Sthompsa return (0); 658202181Sthompsaout: 659202181Sthompsa uhso_detach(sc->sc_dev); 660202181Sthompsa return (ENXIO); 661202181Sthompsa} 662202181Sthompsa 663202181Sthompsastatic int 664202181Sthompsauhso_detach(device_t self) 665202181Sthompsa{ 666202181Sthompsa struct uhso_softc *sc = device_get_softc(self); 667202181Sthompsa int i; 668202181Sthompsa 669202181Sthompsa usbd_transfer_unsetup(sc->sc_xfer, 3); 670202181Sthompsa usbd_transfer_unsetup(sc->sc_ctrl_xfer, UHSO_CTRL_MAX); 671202181Sthompsa if (sc->sc_ttys > 0) { 672214761Sn_hibma ucom_detach(&sc->sc_super_ucom, sc->sc_ucom); 673202181Sthompsa 674202181Sthompsa for (i = 0; i < sc->sc_ttys; i++) { 675202181Sthompsa if (sc->sc_tty[i].ht_muxport != -1) { 676202181Sthompsa usbd_transfer_unsetup(sc->sc_tty[i].ht_xfer, 677202181Sthompsa UHSO_CTRL_MAX); 678202181Sthompsa } 679202181Sthompsa } 680202181Sthompsa 681202181Sthompsa free(sc->sc_tty, M_USBDEV); 682202181Sthompsa free(sc->sc_ucom, M_USBDEV); 683202181Sthompsa } 684202181Sthompsa 685202181Sthompsa if (sc->sc_ifp != NULL) { 686202181Sthompsa callout_drain(&sc->sc_c); 687210275Sthompsa free_unr(uhso_ifnet_unit, sc->sc_ifp->if_dunit); 688202181Sthompsa mtx_lock(&sc->sc_mtx); 689202181Sthompsa uhso_if_stop(sc); 690202181Sthompsa bpfdetach(sc->sc_ifp); 691202181Sthompsa if_detach(sc->sc_ifp); 692202181Sthompsa if_free(sc->sc_ifp); 693202181Sthompsa mtx_unlock(&sc->sc_mtx); 694202181Sthompsa usbd_transfer_unsetup(sc->sc_if_xfer, UHSO_IFNET_MAX); 695202181Sthompsa } 696202181Sthompsa 697239299Shselasky device_claim_softc(self); 698239299Shselasky 699239299Shselasky uhso_free_softc(sc); 700239299Shselasky 701202181Sthompsa return (0); 702202181Sthompsa} 703202181Sthompsa 704239180ShselaskyUCOM_UNLOAD_DRAIN(uhso); 705239180Shselasky 706202181Sthompsastatic void 707239299Shselaskyuhso_free_softc(struct uhso_softc *sc) 708239180Shselasky{ 709239180Shselasky if (ucom_unref(&sc->sc_super_ucom)) { 710239299Shselasky mtx_destroy(&sc->sc_mtx); 711239299Shselasky device_free_softc(sc); 712239180Shselasky } 713239180Shselasky} 714239180Shselasky 715239180Shselaskystatic void 716239180Shselaskyuhso_free(struct ucom_softc *ucom) 717239180Shselasky{ 718239299Shselasky uhso_free_softc(ucom->sc_parent); 719239180Shselasky} 720239180Shselasky 721239180Shselaskystatic void 722202181Sthompsauhso_test_autoinst(void *arg, struct usb_device *udev, 723202181Sthompsa struct usb_attach_arg *uaa) 724202181Sthompsa{ 725202181Sthompsa struct usb_interface *iface; 726202181Sthompsa struct usb_interface_descriptor *id; 727202181Sthompsa 728202243Sthompsa if (uaa->dev_state != UAA_DEV_READY || !uhso_autoswitch) 729202181Sthompsa return; 730202181Sthompsa 731202181Sthompsa iface = usbd_get_iface(udev, 0); 732202181Sthompsa if (iface == NULL) 733202181Sthompsa return; 734202181Sthompsa id = iface->idesc; 735202181Sthompsa if (id == NULL || id->bInterfaceClass != UICLASS_MASS) 736202181Sthompsa return; 737202181Sthompsa if (usbd_lookup_id_by_uaa(uhso_devs, sizeof(uhso_devs), uaa)) 738202181Sthompsa return; /* no device match */ 739202181Sthompsa 740202181Sthompsa if (usb_msc_eject(udev, 0, MSC_EJECT_REZERO) == 0) { 741202181Sthompsa /* success, mark the udev as disappearing */ 742202181Sthompsa uaa->dev_state = UAA_DEV_EJECTING; 743202181Sthompsa } 744202181Sthompsa} 745202181Sthompsa 746202181Sthompsastatic int 747202181Sthompsauhso_driver_loaded(struct module *mod, int what, void *arg) 748202181Sthompsa{ 749202181Sthompsa switch (what) { 750202181Sthompsa case MOD_LOAD: 751202181Sthompsa /* register our autoinstall handler */ 752202181Sthompsa uhso_etag = EVENTHANDLER_REGISTER(usb_dev_configured, 753202181Sthompsa uhso_test_autoinst, NULL, EVENTHANDLER_PRI_ANY); 754210275Sthompsa /* create our unit allocator for inet devs */ 755210275Sthompsa uhso_ifnet_unit = new_unrhdr(0, INT_MAX, NULL); 756202181Sthompsa break; 757202181Sthompsa case MOD_UNLOAD: 758202181Sthompsa EVENTHANDLER_DEREGISTER(usb_dev_configured, uhso_etag); 759210275Sthompsa delete_unrhdr(uhso_ifnet_unit); 760202181Sthompsa break; 761202181Sthompsa default: 762202181Sthompsa return (EOPNOTSUPP); 763202181Sthompsa } 764202181Sthompsa return (0); 765202181Sthompsa} 766202181Sthompsa 767202243Sthompsa/* 768202243Sthompsa * Probe the interface type by querying the device. The elements 769202243Sthompsa * of an array indicates the capabilities of a particular interface. 770202243Sthompsa * Returns a bit mask with the interface capabilities. 771202243Sthompsa */ 772202243Sthompsastatic int 773210275Sthompsauhso_probe_iface_auto(struct usb_device *udev, int index) 774202181Sthompsa{ 775202181Sthompsa struct usb_device_request req; 776202181Sthompsa usb_error_t uerr; 777202181Sthompsa uint16_t actlen = 0; 778202181Sthompsa char port; 779202181Sthompsa char buf[17] = {0}; 780202181Sthompsa 781202181Sthompsa req.bmRequestType = UT_READ_VENDOR_DEVICE; 782202181Sthompsa req.bRequest = 0x86; 783202181Sthompsa USETW(req.wValue, 0); 784202181Sthompsa USETW(req.wIndex, 0); 785202181Sthompsa USETW(req.wLength, 17); 786202181Sthompsa 787210275Sthompsa uerr = usbd_do_request_flags(udev, NULL, &req, buf, 788202181Sthompsa 0, &actlen, USB_MS_HZ); 789202181Sthompsa if (uerr != 0) { 790210275Sthompsa printf("%s: usbd_do_request_flags failed, %s\n", 791210275Sthompsa __func__, usbd_errstr(uerr)); 792202181Sthompsa return (0); 793202181Sthompsa } 794202181Sthompsa 795202243Sthompsa UHSO_DPRINTF(1, "actlen=%d\n", actlen); 796202181Sthompsa UHSO_HEXDUMP(buf, 17); 797202181Sthompsa 798202181Sthompsa if (index < 0 || index > 16) { 799202181Sthompsa UHSO_DPRINTF(0, "Index %d out of range\n", index); 800202181Sthompsa return (0); 801202181Sthompsa } 802202181Sthompsa 803202243Sthompsa UHSO_DPRINTF(1, "index=%d, type=%x[%s]\n", index, buf[index], 804202243Sthompsa uhso_port_type[(int)uhso_port_map[(int)buf[index]]]); 805202181Sthompsa 806202181Sthompsa if (buf[index] >= uhso_port_map_max) 807202181Sthompsa port = 0; 808202181Sthompsa else 809202181Sthompsa port = uhso_port_map[(int)buf[index]]; 810202181Sthompsa 811202243Sthompsa switch (port) { 812202243Sthompsa case UHSO_PORT_TYPE_NETWORK: 813202243Sthompsa return (UHSO_IFACE_SPEC(UHSO_IF_NET | UHSO_IF_MUX, 814202243Sthompsa UHSO_PORT_SERIAL | UHSO_PORT_NETWORK, port)); 815210275Sthompsa case UHSO_PORT_TYPE_DIAG: 816210275Sthompsa case UHSO_PORT_TYPE_DIAG2: 817210275Sthompsa case UHSO_PORT_TYPE_CTL: 818210275Sthompsa case UHSO_PORT_TYPE_APP: 819210275Sthompsa case UHSO_PORT_TYPE_APP2: 820210275Sthompsa case UHSO_PORT_TYPE_MODEM: 821210275Sthompsa return (UHSO_IFACE_SPEC(UHSO_IF_BULK, 822210275Sthompsa UHSO_PORT_SERIAL, port)); 823210275Sthompsa case UHSO_PORT_TYPE_MSD: 824202181Sthompsa return (0); 825210275Sthompsa case UHSO_PORT_TYPE_UNKNOWN: 826202243Sthompsa default: 827210275Sthompsa return (0); 828202243Sthompsa } 829202181Sthompsa 830202181Sthompsa return (0); 831202181Sthompsa} 832202181Sthompsa 833210275Sthompsa/* 834210275Sthompsa * Returns the capabilities of interfaces for devices that don't 835210275Sthompsa * support the automatic query. 836210275Sthompsa * Returns a bit mask with the interface capabilities. 837210275Sthompsa */ 838202181Sthompsastatic int 839210275Sthompsauhso_probe_iface_static(struct usb_device *udev, int index) 840202181Sthompsa{ 841202181Sthompsa struct usb_config_descriptor *cd; 842202181Sthompsa 843210275Sthompsa cd = usbd_get_config_descriptor(udev); 844202181Sthompsa if (cd->bNumInterface <= 3) { 845202243Sthompsa /* Cards with 3 or less interfaces */ 846202181Sthompsa switch (index) { 847202181Sthompsa case 0: 848202181Sthompsa return UHSO_IFACE_SPEC(UHSO_IF_NET | UHSO_IF_MUX, 849202243Sthompsa UHSO_PORT_SERIAL | UHSO_PORT_NETWORK, 850202243Sthompsa UHSO_PORT_TYPE_NETWORK); 851202181Sthompsa case 1: 852202181Sthompsa return UHSO_IFACE_SPEC(UHSO_IF_BULK, 853202181Sthompsa UHSO_PORT_SERIAL, UHSO_PORT_TYPE_DIAG); 854202181Sthompsa case 2: 855202181Sthompsa return UHSO_IFACE_SPEC(UHSO_IF_BULK, 856202181Sthompsa UHSO_PORT_SERIAL, UHSO_PORT_TYPE_MODEM); 857202181Sthompsa } 858202243Sthompsa } else { 859202243Sthompsa /* Cards with 4 interfaces */ 860202181Sthompsa switch (index) { 861202181Sthompsa case 0: 862202181Sthompsa return UHSO_IFACE_SPEC(UHSO_IF_NET | UHSO_IF_MUX, 863202243Sthompsa UHSO_PORT_SERIAL | UHSO_PORT_NETWORK, 864202243Sthompsa UHSO_PORT_TYPE_NETWORK); 865202181Sthompsa case 1: 866202181Sthompsa return UHSO_IFACE_SPEC(UHSO_IF_BULK, 867202181Sthompsa UHSO_PORT_SERIAL, UHSO_PORT_TYPE_DIAG2); 868202181Sthompsa case 2: 869202181Sthompsa return UHSO_IFACE_SPEC(UHSO_IF_BULK, 870202181Sthompsa UHSO_PORT_SERIAL, UHSO_PORT_TYPE_MODEM); 871202181Sthompsa case 3: 872202181Sthompsa return UHSO_IFACE_SPEC(UHSO_IF_BULK, 873202181Sthompsa UHSO_PORT_SERIAL, UHSO_PORT_TYPE_DIAG); 874202181Sthompsa } 875202181Sthompsa } 876202181Sthompsa return (0); 877202181Sthompsa} 878202181Sthompsa 879202243Sthompsa/* 880202243Sthompsa * Probes an interface for its particular capabilities and attaches if 881202243Sthompsa * it's a supported interface. 882202243Sthompsa */ 883202181Sthompsastatic int 884202181Sthompsauhso_probe_iface(struct uhso_softc *sc, int index, 885210275Sthompsa int (*probe)(struct usb_device *, int)) 886202181Sthompsa{ 887202181Sthompsa struct usb_interface *iface; 888202243Sthompsa int type, error; 889202181Sthompsa 890202243Sthompsa UHSO_DPRINTF(1, "Probing for interface %d, probe_func=%p\n", index, probe); 891202181Sthompsa 892210275Sthompsa type = probe(sc->sc_udev, index); 893202181Sthompsa UHSO_DPRINTF(1, "Probe result %x\n", type); 894202181Sthompsa if (type <= 0) 895202181Sthompsa return (ENXIO); 896202181Sthompsa 897202181Sthompsa sc->sc_type = type; 898202181Sthompsa iface = usbd_get_iface(sc->sc_udev, index); 899202181Sthompsa 900202243Sthompsa if (UHSO_IFACE_PORT_TYPE(type) == UHSO_PORT_TYPE_NETWORK) { 901202181Sthompsa error = uhso_attach_ifnet(sc, iface, type); 902202243Sthompsa if (error) { 903202243Sthompsa UHSO_DPRINTF(1, "uhso_attach_ifnet failed"); 904202181Sthompsa return (ENXIO); 905202243Sthompsa } 906202181Sthompsa 907202243Sthompsa /* 908202243Sthompsa * If there is an additional interrupt endpoint on this 909202270Sthompsa * interface then we most likely have a multiplexed serial port 910202243Sthompsa * available. 911202243Sthompsa */ 912202243Sthompsa if (iface->idesc->bNumEndpoints < 3) { 913202243Sthompsa sc->sc_type = UHSO_IFACE_SPEC( 914202243Sthompsa UHSO_IFACE_USB_TYPE(type) & ~UHSO_IF_MUX, 915202243Sthompsa UHSO_IFACE_PORT(type) & ~UHSO_PORT_SERIAL, 916202243Sthompsa UHSO_IFACE_PORT_TYPE(type)); 917202243Sthompsa return (0); 918202243Sthompsa } 919202243Sthompsa 920202243Sthompsa UHSO_DPRINTF(1, "Trying to attach mux. serial\n"); 921202243Sthompsa error = uhso_attach_muxserial(sc, iface, type); 922202243Sthompsa if (error == 0 && sc->sc_ttys > 0) { 923202181Sthompsa error = ucom_attach(&sc->sc_super_ucom, sc->sc_ucom, 924202181Sthompsa sc->sc_ttys, sc, &uhso_ucom_callback, &sc->sc_mtx); 925202181Sthompsa if (error) { 926202181Sthompsa device_printf(sc->sc_dev, "ucom_attach failed\n"); 927202181Sthompsa return (ENXIO); 928202181Sthompsa } 929214843Sn_hibma ucom_set_pnpinfo_usb(&sc->sc_super_ucom, sc->sc_dev); 930202243Sthompsa 931202243Sthompsa mtx_lock(&sc->sc_mtx); 932202243Sthompsa usbd_transfer_start(sc->sc_xfer[UHSO_MUX_ENDPT_INTR]); 933202243Sthompsa mtx_unlock(&sc->sc_mtx); 934202181Sthompsa } 935202243Sthompsa } else if ((UHSO_IFACE_USB_TYPE(type) & UHSO_IF_BULK) && 936202181Sthompsa UHSO_IFACE_PORT(type) & UHSO_PORT_SERIAL) { 937202181Sthompsa 938202181Sthompsa error = uhso_attach_bulkserial(sc, iface, type); 939202181Sthompsa if (error) 940202181Sthompsa return (ENXIO); 941202181Sthompsa 942202181Sthompsa error = ucom_attach(&sc->sc_super_ucom, sc->sc_ucom, 943202181Sthompsa sc->sc_ttys, sc, &uhso_ucom_callback, &sc->sc_mtx); 944202181Sthompsa if (error) { 945202181Sthompsa device_printf(sc->sc_dev, "ucom_attach failed\n"); 946202181Sthompsa return (ENXIO); 947202181Sthompsa } 948214843Sn_hibma ucom_set_pnpinfo_usb(&sc->sc_super_ucom, sc->sc_dev); 949202181Sthompsa } 950202181Sthompsa else { 951202243Sthompsa UHSO_DPRINTF(0, "Unknown type %x\n", type); 952202181Sthompsa return (ENXIO); 953202181Sthompsa } 954202181Sthompsa 955202181Sthompsa return (0); 956202181Sthompsa} 957202181Sthompsa 958210275Sthompsastatic int 959210275Sthompsauhso_radio_ctrl(struct uhso_softc *sc, int onoff) 960210275Sthompsa{ 961210275Sthompsa struct usb_device_request req; 962210275Sthompsa usb_error_t uerr; 963210275Sthompsa 964210275Sthompsa req.bmRequestType = UT_VENDOR; 965210275Sthompsa req.bRequest = onoff ? 0x82 : 0x81; 966210275Sthompsa USETW(req.wValue, 0); 967210275Sthompsa USETW(req.wIndex, 0); 968210275Sthompsa USETW(req.wLength, 0); 969210275Sthompsa 970210275Sthompsa uerr = usbd_do_request(sc->sc_udev, NULL, &req, NULL); 971210275Sthompsa if (uerr != 0) { 972210275Sthompsa device_printf(sc->sc_dev, "usbd_do_request_flags failed: %s\n", 973210275Sthompsa usbd_errstr(uerr)); 974210275Sthompsa return (-1); 975210275Sthompsa } 976210275Sthompsa return (onoff); 977210275Sthompsa} 978210275Sthompsa 979210275Sthompsastatic int 980210275Sthompsauhso_radio_sysctl(SYSCTL_HANDLER_ARGS) 981210275Sthompsa{ 982210275Sthompsa struct uhso_softc *sc = arg1; 983210275Sthompsa int error, radio; 984210275Sthompsa 985210275Sthompsa radio = sc->sc_radio; 986210275Sthompsa error = sysctl_handle_int(oidp, &radio, 0, req); 987210275Sthompsa if (error) 988210275Sthompsa return (error); 989210275Sthompsa if (radio != sc->sc_radio) { 990210275Sthompsa radio = radio != 0 ? 1 : 0; 991210275Sthompsa error = uhso_radio_ctrl(sc, radio); 992210275Sthompsa if (error != -1) 993210275Sthompsa sc->sc_radio = radio; 994210275Sthompsa 995210275Sthompsa } 996210275Sthompsa return (0); 997210275Sthompsa} 998210275Sthompsa 999202243Sthompsa/* 1000202243Sthompsa * Expands allocated memory to fit an additional TTY. 1001202243Sthompsa * Two arrays are kept with matching indexes, one for ucom and one 1002202243Sthompsa * for our private data. 1003202243Sthompsa */ 1004202181Sthompsastatic int 1005202181Sthompsauhso_alloc_tty(struct uhso_softc *sc) 1006202181Sthompsa{ 1007202181Sthompsa 1008202181Sthompsa sc->sc_ttys++; 1009202181Sthompsa sc->sc_tty = reallocf(sc->sc_tty, sizeof(struct uhso_tty) * sc->sc_ttys, 1010202181Sthompsa M_USBDEV, M_WAITOK | M_ZERO); 1011202181Sthompsa if (sc->sc_tty == NULL) 1012202181Sthompsa return (-1); 1013202181Sthompsa 1014202181Sthompsa sc->sc_ucom = reallocf(sc->sc_ucom, 1015202181Sthompsa sizeof(struct ucom_softc) * sc->sc_ttys, M_USBDEV, M_WAITOK | M_ZERO); 1016202181Sthompsa if (sc->sc_ucom == NULL) 1017202181Sthompsa return (-1); 1018202181Sthompsa 1019202181Sthompsa sc->sc_tty[sc->sc_ttys - 1].ht_sc = sc; 1020202181Sthompsa 1021202243Sthompsa UHSO_DPRINTF(1, "Allocated TTY %d\n", sc->sc_ttys - 1); 1022202181Sthompsa return (sc->sc_ttys - 1); 1023202181Sthompsa} 1024202181Sthompsa 1025202243Sthompsa/* 1026202243Sthompsa * Attach a multiplexed serial port 1027202243Sthompsa * Data is read/written with requests on the default control pipe. An interrupt 1028202243Sthompsa * endpoint returns when there is new data to be read. 1029202243Sthompsa */ 1030202181Sthompsastatic int 1031202181Sthompsauhso_attach_muxserial(struct uhso_softc *sc, struct usb_interface *iface, 1032202181Sthompsa int type) 1033202181Sthompsa{ 1034202181Sthompsa struct usb_descriptor *desc; 1035202181Sthompsa int i, port, tty; 1036202181Sthompsa usb_error_t uerr; 1037202181Sthompsa 1038202181Sthompsa /* 1039202181Sthompsa * The class specific interface (type 0x24) descriptor subtype field 1040202181Sthompsa * contains a bitmask that specifies which (and how many) ports that 1041202181Sthompsa * are available through this multiplexed serial port. 1042202181Sthompsa */ 1043202181Sthompsa desc = usbd_find_descriptor(sc->sc_udev, NULL, 1044202181Sthompsa iface->idesc->bInterfaceNumber, UDESC_CS_INTERFACE, 0xff, 0, 0); 1045202181Sthompsa if (desc == NULL) { 1046202181Sthompsa UHSO_DPRINTF(0, "Failed to find UDESC_CS_INTERFACE\n"); 1047202181Sthompsa return (ENXIO); 1048202181Sthompsa } 1049202181Sthompsa 1050202181Sthompsa UHSO_DPRINTF(1, "Mux port mask %x\n", desc->bDescriptorSubtype); 1051202181Sthompsa if (desc->bDescriptorSubtype == 0) 1052202181Sthompsa return (ENXIO); 1053202181Sthompsa 1054202243Sthompsa /* 1055202243Sthompsa * The bitmask is one octet, loop through the number of 1056202243Sthompsa * bits that are set and create a TTY for each. 1057202243Sthompsa */ 1058202181Sthompsa for (i = 0; i < 8; i++) { 1059202181Sthompsa port = (1 << i); 1060202181Sthompsa if ((port & desc->bDescriptorSubtype) == port) { 1061202181Sthompsa UHSO_DPRINTF(2, "Found mux port %x (%d)\n", port, i); 1062202181Sthompsa tty = uhso_alloc_tty(sc); 1063202181Sthompsa if (tty < 0) 1064202181Sthompsa return (ENOMEM); 1065202181Sthompsa sc->sc_tty[tty].ht_muxport = i; 1066202181Sthompsa uerr = usbd_transfer_setup(sc->sc_udev, 1067202181Sthompsa &sc->sc_iface_index, sc->sc_tty[tty].ht_xfer, 1068202181Sthompsa uhso_ctrl_config, UHSO_CTRL_MAX, sc, &sc->sc_mtx); 1069202181Sthompsa if (uerr) { 1070202181Sthompsa device_printf(sc->sc_dev, 1071202181Sthompsa "Failed to setup control pipe: %s\n", 1072202181Sthompsa usbd_errstr(uerr)); 1073202181Sthompsa return (ENXIO); 1074202181Sthompsa } 1075202181Sthompsa } 1076202181Sthompsa } 1077202181Sthompsa 1078202243Sthompsa /* Setup the intr. endpoint */ 1079202181Sthompsa uerr = usbd_transfer_setup(sc->sc_udev, 1080202181Sthompsa &iface->idesc->bInterfaceNumber, sc->sc_xfer, 1081202181Sthompsa uhso_mux_config, 1, sc, &sc->sc_mtx); 1082202181Sthompsa if (uerr) 1083202181Sthompsa return (ENXIO); 1084202181Sthompsa 1085202181Sthompsa return (0); 1086202181Sthompsa} 1087202181Sthompsa 1088202243Sthompsa/* 1089202243Sthompsa * Interrupt callback for the multiplexed serial port. Indicates 1090202270Sthompsa * which serial port has data waiting. 1091202243Sthompsa */ 1092202181Sthompsastatic void 1093202181Sthompsauhso_mux_intr_callback(struct usb_xfer *xfer, usb_error_t error) 1094202181Sthompsa{ 1095202181Sthompsa struct usb_page_cache *pc; 1096202181Sthompsa struct usb_page_search res; 1097202181Sthompsa struct uhso_softc *sc = usbd_xfer_softc(xfer); 1098202181Sthompsa unsigned int i, mux; 1099202181Sthompsa 1100202181Sthompsa UHSO_DPRINTF(3, "status %d\n", USB_GET_STATE(xfer)); 1101202181Sthompsa 1102202181Sthompsa switch (USB_GET_STATE(xfer)) { 1103202181Sthompsa case USB_ST_TRANSFERRED: 1104202181Sthompsa /* 1105202181Sthompsa * The multiplexed port number can be found at the first byte. 1106202181Sthompsa * It contains a bit mask, we transform this in to an integer. 1107202181Sthompsa */ 1108202181Sthompsa pc = usbd_xfer_get_frame(xfer, 0); 1109202181Sthompsa usbd_get_page(pc, 0, &res); 1110202181Sthompsa 1111202181Sthompsa i = *((unsigned char *)res.buffer); 1112202181Sthompsa mux = 0; 1113202181Sthompsa while (i >>= 1) { 1114202181Sthompsa mux++; 1115202181Sthompsa } 1116202181Sthompsa 1117202181Sthompsa UHSO_DPRINTF(3, "mux port %d (%d)\n", mux, i); 1118202181Sthompsa if (mux > UHSO_MPORT_TYPE_NOMAX) 1119202181Sthompsa break; 1120202181Sthompsa 1121202243Sthompsa /* Issue a read for this serial port */ 1122202181Sthompsa usbd_xfer_set_priv( 1123202181Sthompsa sc->sc_tty[mux].ht_xfer[UHSO_CTRL_READ], 1124202181Sthompsa &sc->sc_tty[mux]); 1125202181Sthompsa usbd_transfer_start(sc->sc_tty[mux].ht_xfer[UHSO_CTRL_READ]); 1126202181Sthompsa 1127202181Sthompsa break; 1128202181Sthompsa case USB_ST_SETUP: 1129202181Sthompsatr_setup: 1130202181Sthompsa usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer)); 1131202181Sthompsa usbd_transfer_submit(xfer); 1132202181Sthompsa break; 1133202181Sthompsa default: 1134202181Sthompsa UHSO_DPRINTF(0, "error: %s\n", usbd_errstr(error)); 1135202181Sthompsa if (error == USB_ERR_CANCELLED) 1136202181Sthompsa break; 1137202181Sthompsa 1138202181Sthompsa usbd_xfer_set_stall(xfer); 1139202181Sthompsa goto tr_setup; 1140202181Sthompsa } 1141202181Sthompsa} 1142202181Sthompsa 1143202181Sthompsastatic void 1144202181Sthompsauhso_mux_read_callback(struct usb_xfer *xfer, usb_error_t error) 1145202181Sthompsa{ 1146202181Sthompsa struct uhso_softc *sc = usbd_xfer_softc(xfer); 1147202181Sthompsa struct usb_page_cache *pc; 1148202181Sthompsa struct usb_device_request req; 1149202181Sthompsa struct uhso_tty *ht; 1150202181Sthompsa int actlen, len; 1151202181Sthompsa 1152202181Sthompsa usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL); 1153202181Sthompsa 1154202181Sthompsa UHSO_DPRINTF(3, "status %d\n", USB_GET_STATE(xfer)); 1155202181Sthompsa 1156202181Sthompsa ht = usbd_xfer_get_priv(xfer); 1157202181Sthompsa UHSO_DPRINTF(3, "ht=%p open=%d\n", ht, ht->ht_open); 1158202181Sthompsa 1159202181Sthompsa switch (USB_GET_STATE(xfer)) { 1160202181Sthompsa case USB_ST_TRANSFERRED: 1161202181Sthompsa /* Got data, send to ucom */ 1162202181Sthompsa pc = usbd_xfer_get_frame(xfer, 1); 1163202181Sthompsa len = usbd_xfer_frame_len(xfer, 1); 1164202181Sthompsa 1165202181Sthompsa UHSO_DPRINTF(3, "got %d bytes on mux port %d\n", len, 1166202181Sthompsa ht->ht_muxport); 1167202181Sthompsa if (len <= 0) { 1168202181Sthompsa usbd_transfer_start(sc->sc_xfer[UHSO_MUX_ENDPT_INTR]); 1169202181Sthompsa break; 1170202181Sthompsa } 1171202181Sthompsa 1172202181Sthompsa /* Deliver data if the TTY is open, discard otherwise */ 1173202181Sthompsa if (ht->ht_open) 1174202181Sthompsa ucom_put_data(&sc->sc_ucom[ht->ht_muxport], pc, 0, len); 1175202181Sthompsa /* FALLTHROUGH */ 1176202181Sthompsa case USB_ST_SETUP: 1177202181Sthompsatr_setup: 1178227461Shselasky memset(&req, 0, sizeof(struct usb_device_request)); 1179202181Sthompsa req.bmRequestType = UT_READ_CLASS_INTERFACE; 1180202181Sthompsa req.bRequest = UCDC_GET_ENCAPSULATED_RESPONSE; 1181202181Sthompsa USETW(req.wValue, 0); 1182202181Sthompsa USETW(req.wIndex, ht->ht_muxport); 1183202181Sthompsa USETW(req.wLength, 1024); 1184202181Sthompsa 1185202181Sthompsa pc = usbd_xfer_get_frame(xfer, 0); 1186202181Sthompsa usbd_copy_in(pc, 0, &req, sizeof(req)); 1187202181Sthompsa 1188202181Sthompsa usbd_xfer_set_frame_len(xfer, 0, sizeof(req)); 1189202181Sthompsa usbd_xfer_set_frame_len(xfer, 1, 1024); 1190202181Sthompsa usbd_xfer_set_frames(xfer, 2); 1191202181Sthompsa usbd_transfer_submit(xfer); 1192202181Sthompsa break; 1193202181Sthompsa default: 1194202181Sthompsa UHSO_DPRINTF(0, "error: %s\n", usbd_errstr(error)); 1195202181Sthompsa if (error == USB_ERR_CANCELLED) 1196202181Sthompsa break; 1197202181Sthompsa usbd_xfer_set_stall(xfer); 1198202181Sthompsa goto tr_setup; 1199202181Sthompsa } 1200202181Sthompsa} 1201202181Sthompsa 1202202181Sthompsastatic void 1203202181Sthompsauhso_mux_write_callback(struct usb_xfer *xfer, usb_error_t error) 1204202181Sthompsa{ 1205202181Sthompsa struct uhso_softc *sc = usbd_xfer_softc(xfer); 1206202181Sthompsa struct uhso_tty *ht; 1207202181Sthompsa struct usb_page_cache *pc; 1208202181Sthompsa struct usb_device_request req; 1209202181Sthompsa int actlen; 1210202181Sthompsa struct usb_page_search res; 1211202181Sthompsa 1212202181Sthompsa usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL); 1213202181Sthompsa 1214202181Sthompsa ht = usbd_xfer_get_priv(xfer); 1215202181Sthompsa UHSO_DPRINTF(3, "status=%d, using mux port %d\n", 1216202181Sthompsa USB_GET_STATE(xfer), ht->ht_muxport); 1217202181Sthompsa 1218202181Sthompsa switch (USB_GET_STATE(xfer)) { 1219202181Sthompsa case USB_ST_TRANSFERRED: 1220202181Sthompsa UHSO_DPRINTF(3, "wrote %zd data bytes to muxport %d\n", 1221202181Sthompsa actlen - sizeof(struct usb_device_request) , 1222202181Sthompsa ht->ht_muxport); 1223202181Sthompsa /* FALLTHROUGH */ 1224202181Sthompsa case USB_ST_SETUP: 1225202181Sthompsa pc = usbd_xfer_get_frame(xfer, 1); 1226202181Sthompsa if (ucom_get_data(&sc->sc_ucom[ht->ht_muxport], pc, 1227202181Sthompsa 0, 32, &actlen)) { 1228202181Sthompsa 1229202181Sthompsa usbd_get_page(pc, 0, &res); 1230202181Sthompsa 1231227461Shselasky memset(&req, 0, sizeof(struct usb_device_request)); 1232202181Sthompsa req.bmRequestType = UT_WRITE_CLASS_INTERFACE; 1233202181Sthompsa req.bRequest = UCDC_SEND_ENCAPSULATED_COMMAND; 1234202181Sthompsa USETW(req.wValue, 0); 1235202181Sthompsa USETW(req.wIndex, ht->ht_muxport); 1236202181Sthompsa USETW(req.wLength, actlen); 1237202181Sthompsa 1238202181Sthompsa pc = usbd_xfer_get_frame(xfer, 0); 1239202181Sthompsa usbd_copy_in(pc, 0, &req, sizeof(req)); 1240202181Sthompsa 1241202181Sthompsa usbd_xfer_set_frame_len(xfer, 0, sizeof(req)); 1242202181Sthompsa usbd_xfer_set_frame_len(xfer, 1, actlen); 1243202181Sthompsa usbd_xfer_set_frames(xfer, 2); 1244202181Sthompsa 1245202181Sthompsa UHSO_DPRINTF(3, "Prepared %d bytes for transmit " 1246202181Sthompsa "on muxport %d\n", actlen, ht->ht_muxport); 1247202181Sthompsa 1248202181Sthompsa usbd_transfer_submit(xfer); 1249202181Sthompsa } 1250202181Sthompsa break; 1251202181Sthompsa default: 1252202181Sthompsa UHSO_DPRINTF(0, "error: %s\n", usbd_errstr(error)); 1253202181Sthompsa if (error == USB_ERR_CANCELLED) 1254202181Sthompsa break; 1255202181Sthompsa break; 1256202181Sthompsa } 1257202181Sthompsa} 1258202181Sthompsa 1259202181Sthompsastatic int 1260202181Sthompsauhso_attach_bulkserial(struct uhso_softc *sc, struct usb_interface *iface, 1261202181Sthompsa int type) 1262202181Sthompsa{ 1263202181Sthompsa usb_error_t uerr; 1264202181Sthompsa int tty; 1265202181Sthompsa 1266202243Sthompsa /* Try attaching RD/WR/INTR first */ 1267202181Sthompsa uerr = usbd_transfer_setup(sc->sc_udev, 1268202181Sthompsa &iface->idesc->bInterfaceNumber, sc->sc_xfer, 1269202181Sthompsa uhso_bs_config, UHSO_BULK_ENDPT_MAX, sc, &sc->sc_mtx); 1270202181Sthompsa if (uerr) { 1271202181Sthompsa /* Try only RD/WR */ 1272202181Sthompsa uerr = usbd_transfer_setup(sc->sc_udev, 1273202181Sthompsa &iface->idesc->bInterfaceNumber, sc->sc_xfer, 1274202181Sthompsa uhso_bs_config, UHSO_BULK_ENDPT_MAX - 1, sc, &sc->sc_mtx); 1275202181Sthompsa } 1276202181Sthompsa if (uerr) { 1277202181Sthompsa UHSO_DPRINTF(0, "usbd_transfer_setup failed"); 1278202181Sthompsa return (-1); 1279202181Sthompsa } 1280202181Sthompsa 1281202181Sthompsa tty = uhso_alloc_tty(sc); 1282202181Sthompsa if (tty < 0) { 1283202181Sthompsa usbd_transfer_unsetup(sc->sc_xfer, UHSO_BULK_ENDPT_MAX); 1284202181Sthompsa return (ENOMEM); 1285202181Sthompsa } 1286202181Sthompsa 1287202181Sthompsa sc->sc_tty[tty].ht_muxport = -1; 1288202181Sthompsa return (0); 1289202181Sthompsa} 1290202181Sthompsa 1291202181Sthompsastatic void 1292202181Sthompsauhso_bs_read_callback(struct usb_xfer *xfer, usb_error_t error) 1293202181Sthompsa{ 1294202181Sthompsa struct uhso_softc *sc = usbd_xfer_softc(xfer); 1295202181Sthompsa struct usb_page_cache *pc; 1296202181Sthompsa int actlen; 1297202181Sthompsa 1298202181Sthompsa usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL); 1299202181Sthompsa 1300202181Sthompsa UHSO_DPRINTF(3, "status %d, actlen=%d\n", USB_GET_STATE(xfer), actlen); 1301202181Sthompsa 1302202181Sthompsa switch (USB_GET_STATE(xfer)) { 1303202181Sthompsa case USB_ST_TRANSFERRED: 1304202181Sthompsa pc = usbd_xfer_get_frame(xfer, 0); 1305202181Sthompsa ucom_put_data(&sc->sc_ucom[0], pc, 0, actlen); 1306202181Sthompsa /* FALLTHROUGH */ 1307202181Sthompsa case USB_ST_SETUP: 1308202181Sthompsatr_setup: 1309202181Sthompsa usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer)); 1310202181Sthompsa usbd_transfer_submit(xfer); 1311202181Sthompsa break; 1312202181Sthompsa default: 1313202181Sthompsa UHSO_DPRINTF(0, "error: %s\n", usbd_errstr(error)); 1314202181Sthompsa if (error == USB_ERR_CANCELLED) 1315202181Sthompsa break; 1316202181Sthompsa usbd_xfer_set_stall(xfer); 1317202181Sthompsa goto tr_setup; 1318202181Sthompsa } 1319202181Sthompsa} 1320202181Sthompsa 1321202181Sthompsastatic void 1322202181Sthompsauhso_bs_write_callback(struct usb_xfer *xfer, usb_error_t error) 1323202181Sthompsa{ 1324202181Sthompsa struct uhso_softc *sc = usbd_xfer_softc(xfer); 1325202181Sthompsa struct usb_page_cache *pc; 1326202181Sthompsa int actlen; 1327202181Sthompsa 1328202181Sthompsa usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL); 1329202181Sthompsa 1330202181Sthompsa UHSO_DPRINTF(3, "status %d, actlen=%d\n", USB_GET_STATE(xfer), actlen); 1331202181Sthompsa 1332202181Sthompsa switch (USB_GET_STATE(xfer)) { 1333202181Sthompsa case USB_ST_TRANSFERRED: 1334202181Sthompsa case USB_ST_SETUP: 1335202181Sthompsatr_setup: 1336202181Sthompsa pc = usbd_xfer_get_frame(xfer, 0); 1337202181Sthompsa if (ucom_get_data(&sc->sc_ucom[0], pc, 0, 8192, &actlen)) { 1338202181Sthompsa usbd_xfer_set_frame_len(xfer, 0, actlen); 1339202181Sthompsa usbd_transfer_submit(xfer); 1340202181Sthompsa } 1341202181Sthompsa break; 1342202181Sthompsa break; 1343202181Sthompsa default: 1344202181Sthompsa UHSO_DPRINTF(0, "error: %s\n", usbd_errstr(error)); 1345202181Sthompsa if (error == USB_ERR_CANCELLED) 1346202181Sthompsa break; 1347202181Sthompsa usbd_xfer_set_stall(xfer); 1348202181Sthompsa goto tr_setup; 1349202181Sthompsa } 1350202181Sthompsa} 1351202181Sthompsa 1352202181Sthompsastatic void 1353202181Sthompsauhso_bs_cfg(struct uhso_softc *sc) 1354202181Sthompsa{ 1355202181Sthompsa struct usb_device_request req; 1356202181Sthompsa usb_error_t uerr; 1357202181Sthompsa 1358202181Sthompsa if (!(UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_BULK)) 1359202181Sthompsa return; 1360202181Sthompsa 1361202181Sthompsa req.bmRequestType = UT_WRITE_CLASS_INTERFACE; 1362202181Sthompsa req.bRequest = UCDC_SET_CONTROL_LINE_STATE; 1363202181Sthompsa USETW(req.wValue, sc->sc_line); 1364202181Sthompsa USETW(req.wIndex, sc->sc_iface_no); 1365202181Sthompsa USETW(req.wLength, 0); 1366202181Sthompsa 1367202181Sthompsa uerr = ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom[0], &req, NULL, 0, 1000); 1368202181Sthompsa if (uerr != 0) { 1369202181Sthompsa device_printf(sc->sc_dev, "failed to set ctrl line state to " 1370202181Sthompsa "0x%02x: %s\n", sc->sc_line, usbd_errstr(uerr)); 1371202181Sthompsa } 1372202181Sthompsa} 1373202181Sthompsa 1374202181Sthompsastatic void 1375202181Sthompsauhso_bs_intr_callback(struct usb_xfer *xfer, usb_error_t error) 1376202181Sthompsa{ 1377202181Sthompsa struct uhso_softc *sc = usbd_xfer_softc(xfer); 1378202181Sthompsa struct usb_page_cache *pc; 1379202181Sthompsa int actlen; 1380202181Sthompsa struct usb_cdc_notification cdc; 1381202181Sthompsa 1382202181Sthompsa usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL); 1383202181Sthompsa UHSO_DPRINTF(3, "status %d, actlen=%d\n", USB_GET_STATE(xfer), actlen); 1384202181Sthompsa 1385202181Sthompsa switch (USB_GET_STATE(xfer)) { 1386202181Sthompsa case USB_ST_TRANSFERRED: 1387202181Sthompsa if (actlen < UCDC_NOTIFICATION_LENGTH) { 1388202181Sthompsa UHSO_DPRINTF(0, "UCDC notification too short: %d\n", actlen); 1389202181Sthompsa goto tr_setup; 1390202181Sthompsa } 1391233774Shselasky else if (actlen > (int)sizeof(struct usb_cdc_notification)) { 1392202181Sthompsa UHSO_DPRINTF(0, "UCDC notification too large: %d\n", actlen); 1393202181Sthompsa actlen = sizeof(struct usb_cdc_notification); 1394202181Sthompsa } 1395202181Sthompsa 1396202181Sthompsa pc = usbd_xfer_get_frame(xfer, 0); 1397202181Sthompsa usbd_copy_out(pc, 0, &cdc, actlen); 1398202181Sthompsa 1399202181Sthompsa if (UGETW(cdc.wIndex) != sc->sc_iface_no) { 1400202243Sthompsa UHSO_DPRINTF(0, "Interface mismatch, got %d expected %d\n", 1401202181Sthompsa UGETW(cdc.wIndex), sc->sc_iface_no); 1402202181Sthompsa goto tr_setup; 1403202181Sthompsa } 1404202181Sthompsa 1405202181Sthompsa if (cdc.bmRequestType == UCDC_NOTIFICATION && 1406202181Sthompsa cdc.bNotification == UCDC_N_SERIAL_STATE) { 1407202243Sthompsa UHSO_DPRINTF(2, "notify = 0x%02x\n", cdc.data[0]); 1408202181Sthompsa 1409202181Sthompsa sc->sc_msr = 0; 1410202181Sthompsa sc->sc_lsr = 0; 1411202181Sthompsa if (cdc.data[0] & UCDC_N_SERIAL_RI) 1412202181Sthompsa sc->sc_msr |= SER_RI; 1413202181Sthompsa if (cdc.data[0] & UCDC_N_SERIAL_DSR) 1414202181Sthompsa sc->sc_msr |= SER_DSR; 1415202181Sthompsa if (cdc.data[0] & UCDC_N_SERIAL_DCD) 1416202181Sthompsa sc->sc_msr |= SER_DCD; 1417202181Sthompsa 1418202181Sthompsa ucom_status_change(&sc->sc_ucom[0]); 1419202181Sthompsa } 1420202181Sthompsa case USB_ST_SETUP: 1421202181Sthompsatr_setup: 1422202181Sthompsa default: 1423202181Sthompsa if (error == USB_ERR_CANCELLED) 1424202181Sthompsa break; 1425202181Sthompsa usbd_xfer_set_stall(xfer); 1426202181Sthompsa goto tr_setup; 1427202181Sthompsa } 1428202181Sthompsa} 1429202181Sthompsa 1430202181Sthompsastatic void 1431202181Sthompsauhso_ucom_cfg_get_status(struct ucom_softc *ucom, uint8_t *lsr, uint8_t *msr) 1432202181Sthompsa{ 1433202181Sthompsa struct uhso_softc *sc = ucom->sc_parent; 1434202181Sthompsa 1435202181Sthompsa *lsr = sc->sc_lsr; 1436202181Sthompsa *msr = sc->sc_msr; 1437202181Sthompsa} 1438202181Sthompsa 1439202181Sthompsastatic void 1440202181Sthompsauhso_ucom_cfg_set_dtr(struct ucom_softc *ucom, uint8_t onoff) 1441202181Sthompsa{ 1442202181Sthompsa struct uhso_softc *sc = ucom->sc_parent; 1443202181Sthompsa 1444202181Sthompsa if (!(UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_BULK)) 1445202181Sthompsa return; 1446202181Sthompsa 1447202181Sthompsa if (onoff) 1448202181Sthompsa sc->sc_line |= UCDC_LINE_DTR; 1449202181Sthompsa else 1450208017Sthompsa sc->sc_line &= ~UCDC_LINE_DTR; 1451202181Sthompsa 1452202181Sthompsa uhso_bs_cfg(sc); 1453202181Sthompsa} 1454202181Sthompsa 1455202181Sthompsastatic void 1456202181Sthompsauhso_ucom_cfg_set_rts(struct ucom_softc *ucom, uint8_t onoff) 1457202181Sthompsa{ 1458202181Sthompsa struct uhso_softc *sc = ucom->sc_parent; 1459202181Sthompsa 1460202181Sthompsa if (!(UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_BULK)) 1461202181Sthompsa return; 1462202181Sthompsa 1463202181Sthompsa if (onoff) 1464202181Sthompsa sc->sc_line |= UCDC_LINE_RTS; 1465202181Sthompsa else 1466208017Sthompsa sc->sc_line &= ~UCDC_LINE_RTS; 1467202181Sthompsa 1468202181Sthompsa uhso_bs_cfg(sc); 1469202181Sthompsa} 1470202181Sthompsa 1471202181Sthompsastatic void 1472202181Sthompsauhso_ucom_start_read(struct ucom_softc *ucom) 1473202181Sthompsa{ 1474202181Sthompsa struct uhso_softc *sc = ucom->sc_parent; 1475202181Sthompsa 1476214761Sn_hibma UHSO_DPRINTF(3, "unit=%d, subunit=%d\n", 1477214761Sn_hibma ucom->sc_super->sc_unit, ucom->sc_subunit); 1478202181Sthompsa 1479202181Sthompsa if (UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_MUX) { 1480214761Sn_hibma sc->sc_tty[ucom->sc_subunit].ht_open = 1; 1481202181Sthompsa usbd_transfer_start(sc->sc_xfer[UHSO_MUX_ENDPT_INTR]); 1482202181Sthompsa } 1483202181Sthompsa else if (UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_BULK) { 1484202181Sthompsa sc->sc_tty[0].ht_open = 1; 1485202181Sthompsa usbd_transfer_start(sc->sc_xfer[UHSO_BULK_ENDPT_READ]); 1486202181Sthompsa if (sc->sc_xfer[UHSO_BULK_ENDPT_INTR] != NULL) 1487202181Sthompsa usbd_transfer_start(sc->sc_xfer[UHSO_BULK_ENDPT_INTR]); 1488202181Sthompsa } 1489202181Sthompsa} 1490202181Sthompsa 1491202181Sthompsastatic void 1492202181Sthompsauhso_ucom_stop_read(struct ucom_softc *ucom) 1493202181Sthompsa{ 1494202181Sthompsa 1495202181Sthompsa struct uhso_softc *sc = ucom->sc_parent; 1496202181Sthompsa 1497202181Sthompsa if (UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_MUX) { 1498214761Sn_hibma sc->sc_tty[ucom->sc_subunit].ht_open = 0; 1499202181Sthompsa usbd_transfer_stop( 1500214761Sn_hibma sc->sc_tty[ucom->sc_subunit].ht_xfer[UHSO_CTRL_READ]); 1501202181Sthompsa } 1502202181Sthompsa else if (UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_BULK) { 1503202181Sthompsa sc->sc_tty[0].ht_open = 0; 1504202181Sthompsa usbd_transfer_start(sc->sc_xfer[UHSO_BULK_ENDPT_READ]); 1505202181Sthompsa if (sc->sc_xfer[UHSO_BULK_ENDPT_INTR] != NULL) 1506202181Sthompsa usbd_transfer_stop(sc->sc_xfer[UHSO_BULK_ENDPT_INTR]); 1507202181Sthompsa } 1508202181Sthompsa} 1509202181Sthompsa 1510202181Sthompsastatic void 1511202181Sthompsauhso_ucom_start_write(struct ucom_softc *ucom) 1512202181Sthompsa{ 1513202181Sthompsa struct uhso_softc *sc = ucom->sc_parent; 1514202181Sthompsa 1515202181Sthompsa if (UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_MUX) { 1516214761Sn_hibma UHSO_DPRINTF(3, "local unit %d\n", ucom->sc_subunit); 1517202181Sthompsa 1518202181Sthompsa usbd_transfer_start(sc->sc_xfer[UHSO_MUX_ENDPT_INTR]); 1519202181Sthompsa 1520202181Sthompsa usbd_xfer_set_priv( 1521214761Sn_hibma sc->sc_tty[ucom->sc_subunit].ht_xfer[UHSO_CTRL_WRITE], 1522214761Sn_hibma &sc->sc_tty[ucom->sc_subunit]); 1523202181Sthompsa usbd_transfer_start( 1524214761Sn_hibma sc->sc_tty[ucom->sc_subunit].ht_xfer[UHSO_CTRL_WRITE]); 1525202181Sthompsa 1526202181Sthompsa } 1527202181Sthompsa else if (UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_BULK) { 1528202181Sthompsa usbd_transfer_start(sc->sc_xfer[UHSO_BULK_ENDPT_WRITE]); 1529202181Sthompsa } 1530202181Sthompsa} 1531202181Sthompsa 1532202181Sthompsastatic void 1533202181Sthompsauhso_ucom_stop_write(struct ucom_softc *ucom) 1534202181Sthompsa{ 1535202181Sthompsa struct uhso_softc *sc = ucom->sc_parent; 1536202181Sthompsa 1537202181Sthompsa if (UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_MUX) { 1538202181Sthompsa usbd_transfer_stop( 1539214761Sn_hibma sc->sc_tty[ucom->sc_subunit].ht_xfer[UHSO_CTRL_WRITE]); 1540202181Sthompsa } 1541202181Sthompsa else if (UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_BULK) { 1542202181Sthompsa usbd_transfer_stop(sc->sc_xfer[UHSO_BULK_ENDPT_WRITE]); 1543202181Sthompsa } 1544202181Sthompsa} 1545202181Sthompsa 1546210275Sthompsastatic int 1547210275Sthompsauhso_attach_ifnet(struct uhso_softc *sc, struct usb_interface *iface, int type) 1548202181Sthompsa{ 1549202181Sthompsa struct ifnet *ifp; 1550202181Sthompsa usb_error_t uerr; 1551202181Sthompsa struct sysctl_ctx_list *sctx; 1552202181Sthompsa struct sysctl_oid *soid; 1553210275Sthompsa unsigned int devunit; 1554202181Sthompsa 1555202181Sthompsa uerr = usbd_transfer_setup(sc->sc_udev, 1556202181Sthompsa &iface->idesc->bInterfaceNumber, sc->sc_if_xfer, 1557202181Sthompsa uhso_ifnet_config, UHSO_IFNET_MAX, sc, &sc->sc_mtx); 1558202181Sthompsa if (uerr) { 1559202181Sthompsa UHSO_DPRINTF(0, "usbd_transfer_setup failed: %s\n", 1560202181Sthompsa usbd_errstr(uerr)); 1561202181Sthompsa return (-1); 1562202181Sthompsa } 1563202181Sthompsa 1564202243Sthompsa sc->sc_ifp = ifp = if_alloc(IFT_OTHER); 1565202181Sthompsa if (sc->sc_ifp == NULL) { 1566202181Sthompsa device_printf(sc->sc_dev, "if_alloc() failed\n"); 1567202181Sthompsa return (-1); 1568202181Sthompsa } 1569202181Sthompsa 1570202181Sthompsa callout_init_mtx(&sc->sc_c, &sc->sc_mtx, 0); 1571202181Sthompsa mtx_lock(&sc->sc_mtx); 1572202181Sthompsa callout_reset(&sc->sc_c, 1, uhso_if_rxflush, sc); 1573202181Sthompsa mtx_unlock(&sc->sc_mtx); 1574202181Sthompsa 1575210275Sthompsa /* 1576210275Sthompsa * We create our own unit numbers for ifnet devices because the 1577210275Sthompsa * USB interface unit numbers can be at arbitrary positions yielding 1578210275Sthompsa * odd looking device names. 1579210275Sthompsa */ 1580210275Sthompsa devunit = alloc_unr(uhso_ifnet_unit); 1581210275Sthompsa 1582210275Sthompsa if_initname(ifp, device_get_name(sc->sc_dev), devunit); 1583202181Sthompsa ifp->if_mtu = UHSO_MAX_MTU; 1584202181Sthompsa ifp->if_ioctl = uhso_if_ioctl; 1585202181Sthompsa ifp->if_init = uhso_if_init; 1586202181Sthompsa ifp->if_start = uhso_if_start; 1587202181Sthompsa ifp->if_output = uhso_if_output; 1588213803Shselasky ifp->if_flags = IFF_BROADCAST | IFF_MULTICAST | IFF_NOARP; 1589202181Sthompsa ifp->if_softc = sc; 1590207554Ssobomax IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen); 1591207554Ssobomax ifp->if_snd.ifq_drv_maxlen = ifqmaxlen; 1592202181Sthompsa IFQ_SET_READY(&ifp->if_snd); 1593202181Sthompsa 1594202181Sthompsa if_attach(ifp); 1595202181Sthompsa bpfattach(ifp, DLT_RAW, 0); 1596202181Sthompsa 1597202181Sthompsa sctx = device_get_sysctl_ctx(sc->sc_dev); 1598202181Sthompsa soid = device_get_sysctl_tree(sc->sc_dev); 1599202181Sthompsa /* Unlocked read... */ 1600202181Sthompsa SYSCTL_ADD_STRING(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "netif", 1601202181Sthompsa CTLFLAG_RD, ifp->if_xname, 0, "Attached network interface"); 1602202181Sthompsa 1603202181Sthompsa return (0); 1604202181Sthompsa} 1605202181Sthompsa 1606202181Sthompsastatic void 1607202181Sthompsauhso_ifnet_read_callback(struct usb_xfer *xfer, usb_error_t error) 1608202181Sthompsa{ 1609202181Sthompsa struct uhso_softc *sc = usbd_xfer_softc(xfer); 1610202181Sthompsa struct mbuf *m; 1611202181Sthompsa struct usb_page_cache *pc; 1612202181Sthompsa int actlen; 1613202181Sthompsa 1614202181Sthompsa usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL); 1615202181Sthompsa 1616202181Sthompsa UHSO_DPRINTF(3, "status=%d, actlen=%d\n", USB_GET_STATE(xfer), actlen); 1617202181Sthompsa 1618202181Sthompsa switch (USB_GET_STATE(xfer)) { 1619202181Sthompsa case USB_ST_TRANSFERRED: 1620202181Sthompsa if (actlen > 0 && (sc->sc_ifp->if_drv_flags & IFF_DRV_RUNNING)) { 1621202181Sthompsa pc = usbd_xfer_get_frame(xfer, 0); 1622243857Sglebius m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR); 1623202181Sthompsa usbd_copy_out(pc, 0, mtod(m, uint8_t *), actlen); 1624202181Sthompsa m->m_pkthdr.len = m->m_len = actlen; 1625202243Sthompsa /* Enqueue frame for further processing */ 1626202181Sthompsa _IF_ENQUEUE(&sc->sc_rxq, m); 1627202181Sthompsa if (!callout_pending(&sc->sc_c) || 1628202181Sthompsa !callout_active(&sc->sc_c)) { 1629202181Sthompsa callout_schedule(&sc->sc_c, 1); 1630202181Sthompsa } 1631202181Sthompsa } 1632202181Sthompsa /* FALLTHROUGH */ 1633202181Sthompsa case USB_ST_SETUP: 1634202181Sthompsatr_setup: 1635202181Sthompsa usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer)); 1636202181Sthompsa usbd_transfer_submit(xfer); 1637202181Sthompsa break; 1638202181Sthompsa default: 1639202181Sthompsa UHSO_DPRINTF(0, "error: %s\n", usbd_errstr(error)); 1640202181Sthompsa if (error == USB_ERR_CANCELLED) 1641202181Sthompsa break; 1642202181Sthompsa usbd_xfer_set_stall(xfer); 1643202181Sthompsa goto tr_setup; 1644202181Sthompsa } 1645202181Sthompsa} 1646202181Sthompsa 1647202181Sthompsa/* 1648202243Sthompsa * Deferred RX processing, called with mutex locked. 1649202243Sthompsa * 1650202270Sthompsa * Each frame we receive might contain several small ip-packets as well 1651202243Sthompsa * as partial ip-packets. We need to separate/assemble them into individual 1652202243Sthompsa * packets before sending them to the ip-layer. 1653202181Sthompsa */ 1654202181Sthompsastatic void 1655202181Sthompsauhso_if_rxflush(void *arg) 1656202181Sthompsa{ 1657202181Sthompsa struct uhso_softc *sc = arg; 1658202181Sthompsa struct ifnet *ifp = sc->sc_ifp; 1659202181Sthompsa uint8_t *cp; 1660202181Sthompsa struct mbuf *m, *m0, *mwait; 1661202181Sthompsa struct ip *ip; 1662202181Sthompsa#ifdef INET6 1663202181Sthompsa struct ip6_hdr *ip6; 1664202181Sthompsa#endif 1665202181Sthompsa uint16_t iplen; 1666202181Sthompsa int len, isr; 1667202181Sthompsa 1668202181Sthompsa m = NULL; 1669202181Sthompsa mwait = sc->sc_mwait; 1670202181Sthompsa for (;;) { 1671202181Sthompsa if (m == NULL) { 1672202181Sthompsa _IF_DEQUEUE(&sc->sc_rxq, m); 1673202181Sthompsa if (m == NULL) 1674202181Sthompsa break; 1675202243Sthompsa UHSO_DPRINTF(3, "dequeue m=%p, len=%d\n", m, m->m_len); 1676202181Sthompsa } 1677202181Sthompsa mtx_unlock(&sc->sc_mtx); 1678202181Sthompsa 1679202181Sthompsa /* Do we have a partial packet waiting? */ 1680202181Sthompsa if (mwait != NULL) { 1681202181Sthompsa m0 = mwait; 1682202181Sthompsa mwait = NULL; 1683202181Sthompsa 1684202243Sthompsa UHSO_DPRINTF(3, "partial m0=%p(%d), concat w/ m=%p(%d)\n", 1685202181Sthompsa m0, m0->m_len, m, m->m_len); 1686202181Sthompsa len = m->m_len + m0->m_len; 1687202181Sthompsa 1688202181Sthompsa /* Concat mbufs and fix headers */ 1689202181Sthompsa m_cat(m0, m); 1690202181Sthompsa m0->m_pkthdr.len = len; 1691202181Sthompsa m->m_flags &= ~M_PKTHDR; 1692202181Sthompsa 1693202181Sthompsa m = m_pullup(m0, sizeof(struct ip)); 1694202181Sthompsa if (m == NULL) { 1695202181Sthompsa ifp->if_ierrors++; 1696202181Sthompsa UHSO_DPRINTF(0, "m_pullup failed\n"); 1697202181Sthompsa mtx_lock(&sc->sc_mtx); 1698202181Sthompsa continue; 1699202181Sthompsa } 1700202243Sthompsa UHSO_DPRINTF(3, "Constructed mbuf=%p, len=%d\n", 1701202181Sthompsa m, m->m_pkthdr.len); 1702202181Sthompsa } 1703202181Sthompsa 1704202181Sthompsa cp = mtod(m, uint8_t *); 1705202181Sthompsa ip = (struct ip *)cp; 1706202181Sthompsa#ifdef INET6 1707202181Sthompsa ip6 = (struct ip6_hdr *)cp; 1708202181Sthompsa#endif 1709202181Sthompsa 1710202181Sthompsa /* Check for IPv4 */ 1711202181Sthompsa if (ip->ip_v == IPVERSION) { 1712202181Sthompsa iplen = htons(ip->ip_len); 1713202181Sthompsa isr = NETISR_IP; 1714202181Sthompsa } 1715202181Sthompsa#ifdef INET6 1716202181Sthompsa /* Check for IPv6 */ 1717202181Sthompsa else if ((ip6->ip6_vfc & IPV6_VERSION_MASK) == IPV6_VERSION) { 1718202181Sthompsa iplen = htons(ip6->ip6_plen); 1719202181Sthompsa isr = NETISR_IPV6; 1720202181Sthompsa } 1721202181Sthompsa#endif 1722202181Sthompsa else { 1723202181Sthompsa UHSO_DPRINTF(0, "got unexpected ip version %d, " 1724202181Sthompsa "m=%p, len=%d\n", (*cp & 0xf0) >> 4, m, m->m_len); 1725202181Sthompsa ifp->if_ierrors++; 1726202181Sthompsa UHSO_HEXDUMP(cp, 4); 1727202181Sthompsa m_freem(m); 1728202181Sthompsa m = NULL; 1729202181Sthompsa mtx_lock(&sc->sc_mtx); 1730202181Sthompsa continue; 1731202181Sthompsa } 1732202181Sthompsa 1733202181Sthompsa if (iplen == 0) { 1734202181Sthompsa UHSO_DPRINTF(0, "Zero IP length\n"); 1735202181Sthompsa ifp->if_ierrors++; 1736202181Sthompsa m_freem(m); 1737202181Sthompsa m = NULL; 1738202181Sthompsa mtx_lock(&sc->sc_mtx); 1739202181Sthompsa continue; 1740202181Sthompsa } 1741202181Sthompsa 1742202243Sthompsa UHSO_DPRINTF(3, "m=%p, len=%d, cp=%p, iplen=%d\n", 1743202181Sthompsa m, m->m_pkthdr.len, cp, iplen); 1744202181Sthompsa 1745202181Sthompsa m0 = NULL; 1746202181Sthompsa 1747202181Sthompsa /* More IP packets in this mbuf */ 1748202181Sthompsa if (iplen < m->m_pkthdr.len) { 1749202181Sthompsa m0 = m; 1750202181Sthompsa 1751202181Sthompsa /* 1752202181Sthompsa * Allocate a new mbuf for this IP packet and 1753202181Sthompsa * copy the IP-packet into it. 1754202181Sthompsa */ 1755243857Sglebius m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR); 1756227461Shselasky memcpy(mtod(m, uint8_t *), mtod(m0, uint8_t *), iplen); 1757202181Sthompsa m->m_pkthdr.len = m->m_len = iplen; 1758202181Sthompsa 1759202181Sthompsa /* Adjust the size of the original mbuf */ 1760202181Sthompsa m_adj(m0, iplen); 1761243857Sglebius m0 = m_defrag(m0, M_WAITOK); 1762202181Sthompsa 1763202243Sthompsa UHSO_DPRINTF(3, "New mbuf=%p, len=%d/%d, m0=%p, " 1764202181Sthompsa "m0_len=%d/%d\n", m, m->m_pkthdr.len, m->m_len, 1765202181Sthompsa m0, m0->m_pkthdr.len, m0->m_len); 1766202181Sthompsa } 1767202181Sthompsa else if (iplen > m->m_pkthdr.len) { 1768202243Sthompsa UHSO_DPRINTF(3, "Deferred mbuf=%p, len=%d\n", 1769202181Sthompsa m, m->m_pkthdr.len); 1770202181Sthompsa mwait = m; 1771202181Sthompsa m = NULL; 1772202181Sthompsa mtx_lock(&sc->sc_mtx); 1773202181Sthompsa continue; 1774202181Sthompsa } 1775202181Sthompsa 1776202181Sthompsa ifp->if_ipackets++; 1777202181Sthompsa m->m_pkthdr.rcvif = ifp; 1778202181Sthompsa 1779202181Sthompsa /* Dispatch to IP layer */ 1780202181Sthompsa BPF_MTAP(sc->sc_ifp, m); 1781223741Sbz M_SETFIB(m, ifp->if_fib); 1782202181Sthompsa netisr_dispatch(isr, m); 1783202181Sthompsa m = m0 != NULL ? m0 : NULL; 1784202181Sthompsa mtx_lock(&sc->sc_mtx); 1785202181Sthompsa } 1786202181Sthompsa sc->sc_mwait = mwait; 1787202181Sthompsa} 1788202181Sthompsa 1789202181Sthompsastatic void 1790202181Sthompsauhso_ifnet_write_callback(struct usb_xfer *xfer, usb_error_t error) 1791202181Sthompsa{ 1792202181Sthompsa struct uhso_softc *sc = usbd_xfer_softc(xfer); 1793202181Sthompsa struct ifnet *ifp = sc->sc_ifp; 1794202181Sthompsa struct usb_page_cache *pc; 1795202181Sthompsa struct mbuf *m; 1796202181Sthompsa int actlen; 1797202181Sthompsa 1798202181Sthompsa usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL); 1799202181Sthompsa 1800202181Sthompsa UHSO_DPRINTF(3, "status %d, actlen=%d\n", USB_GET_STATE(xfer), actlen); 1801202181Sthompsa 1802202181Sthompsa switch (USB_GET_STATE(xfer)) { 1803202181Sthompsa case USB_ST_TRANSFERRED: 1804202181Sthompsa ifp->if_opackets++; 1805202181Sthompsa ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 1806202181Sthompsa case USB_ST_SETUP: 1807202181Sthompsatr_setup: 1808202181Sthompsa IFQ_DRV_DEQUEUE(&ifp->if_snd, m); 1809202181Sthompsa if (m == NULL) 1810202181Sthompsa break; 1811202181Sthompsa 1812202181Sthompsa ifp->if_drv_flags |= IFF_DRV_OACTIVE; 1813202181Sthompsa 1814202181Sthompsa if (m->m_pkthdr.len > MCLBYTES) 1815202181Sthompsa m->m_pkthdr.len = MCLBYTES; 1816202181Sthompsa 1817202181Sthompsa usbd_xfer_set_frame_len(xfer, 0, m->m_pkthdr.len); 1818202181Sthompsa pc = usbd_xfer_get_frame(xfer, 0); 1819202181Sthompsa usbd_m_copy_in(pc, 0, m, 0, m->m_pkthdr.len); 1820202181Sthompsa usbd_transfer_submit(xfer); 1821202181Sthompsa 1822202181Sthompsa BPF_MTAP(ifp, m); 1823202181Sthompsa m_freem(m); 1824202181Sthompsa break; 1825202181Sthompsa default: 1826202181Sthompsa UHSO_DPRINTF(0, "error: %s\n", usbd_errstr(error)); 1827202181Sthompsa if (error == USB_ERR_CANCELLED) 1828202181Sthompsa break; 1829202181Sthompsa usbd_xfer_set_stall(xfer); 1830202181Sthompsa goto tr_setup; 1831202181Sthompsa } 1832202181Sthompsa} 1833202181Sthompsa 1834202181Sthompsastatic int 1835202181Sthompsauhso_if_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 1836202181Sthompsa{ 1837202181Sthompsa struct uhso_softc *sc; 1838202181Sthompsa 1839202181Sthompsa sc = ifp->if_softc; 1840202181Sthompsa 1841202181Sthompsa switch (cmd) { 1842202181Sthompsa case SIOCSIFFLAGS: 1843202181Sthompsa if (ifp->if_flags & IFF_UP) { 1844202181Sthompsa if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) { 1845202181Sthompsa uhso_if_init(sc); 1846202181Sthompsa } 1847202181Sthompsa } 1848202181Sthompsa else { 1849202181Sthompsa if (ifp->if_drv_flags & IFF_DRV_RUNNING) { 1850202181Sthompsa mtx_lock(&sc->sc_mtx); 1851202181Sthompsa uhso_if_stop(sc); 1852202181Sthompsa mtx_unlock(&sc->sc_mtx); 1853202181Sthompsa } 1854202181Sthompsa } 1855202181Sthompsa break; 1856202181Sthompsa case SIOCSIFADDR: 1857202181Sthompsa case SIOCSIFDSTADDR: 1858202181Sthompsa case SIOCADDMULTI: 1859202181Sthompsa case SIOCDELMULTI: 1860202181Sthompsa break; 1861202181Sthompsa default: 1862202181Sthompsa return (EINVAL); 1863202181Sthompsa } 1864202181Sthompsa return (0); 1865202181Sthompsa} 1866202181Sthompsa 1867202181Sthompsastatic void 1868202181Sthompsauhso_if_init(void *priv) 1869202181Sthompsa{ 1870202181Sthompsa struct uhso_softc *sc = priv; 1871202181Sthompsa struct ifnet *ifp = sc->sc_ifp; 1872202181Sthompsa 1873202181Sthompsa mtx_lock(&sc->sc_mtx); 1874202181Sthompsa uhso_if_stop(sc); 1875202181Sthompsa ifp = sc->sc_ifp; 1876202181Sthompsa ifp->if_flags |= IFF_UP; 1877202181Sthompsa ifp->if_drv_flags |= IFF_DRV_RUNNING; 1878202181Sthompsa mtx_unlock(&sc->sc_mtx); 1879202181Sthompsa 1880202243Sthompsa UHSO_DPRINTF(2, "ifnet initialized\n"); 1881202181Sthompsa} 1882202181Sthompsa 1883202181Sthompsastatic int 1884202181Sthompsauhso_if_output(struct ifnet *ifp, struct mbuf *m0, struct sockaddr *dst, 1885202181Sthompsa struct route *ro) 1886202181Sthompsa{ 1887202181Sthompsa int error; 1888202181Sthompsa 1889202181Sthompsa /* Only IPv4/6 support */ 1890202181Sthompsa if (dst->sa_family != AF_INET 1891202181Sthompsa#ifdef INET6 1892202181Sthompsa && dst->sa_family != AF_INET6 1893202181Sthompsa#endif 1894202181Sthompsa ) { 1895202181Sthompsa return (EAFNOSUPPORT); 1896202181Sthompsa } 1897202181Sthompsa 1898202181Sthompsa error = (ifp->if_transmit)(ifp, m0); 1899202181Sthompsa if (error) { 1900202181Sthompsa ifp->if_oerrors++; 1901202181Sthompsa return (ENOBUFS); 1902202181Sthompsa } 1903202181Sthompsa ifp->if_opackets++; 1904202181Sthompsa return (0); 1905202181Sthompsa} 1906202181Sthompsa 1907202181Sthompsastatic void 1908202181Sthompsauhso_if_start(struct ifnet *ifp) 1909202181Sthompsa{ 1910202181Sthompsa struct uhso_softc *sc = ifp->if_softc; 1911202181Sthompsa 1912202181Sthompsa if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) { 1913202181Sthompsa UHSO_DPRINTF(1, "Not running\n"); 1914202181Sthompsa return; 1915202181Sthompsa } 1916202181Sthompsa 1917202181Sthompsa mtx_lock(&sc->sc_mtx); 1918202181Sthompsa usbd_transfer_start(sc->sc_if_xfer[UHSO_IFNET_READ]); 1919202181Sthompsa usbd_transfer_start(sc->sc_if_xfer[UHSO_IFNET_WRITE]); 1920202181Sthompsa mtx_unlock(&sc->sc_mtx); 1921202181Sthompsa UHSO_DPRINTF(3, "interface started\n"); 1922202181Sthompsa} 1923202181Sthompsa 1924202181Sthompsastatic void 1925202181Sthompsauhso_if_stop(struct uhso_softc *sc) 1926202181Sthompsa{ 1927202181Sthompsa 1928202181Sthompsa usbd_transfer_stop(sc->sc_if_xfer[UHSO_IFNET_READ]); 1929202181Sthompsa usbd_transfer_stop(sc->sc_if_xfer[UHSO_IFNET_WRITE]); 1930202181Sthompsa sc->sc_ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE); 1931202181Sthompsa} 1932