Home | History | Annotate | Line # | Download | only in usb
uhso.c revision 1.17.2.5
      1 /*	$NetBSD: uhso.c,v 1.17.2.5 2015/10/06 21:32:15 skrll Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2009 Iain Hibbert
      5  * Copyright (c) 2008 Fredrik Lindberg
      6  * All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  *
     17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27  */
     28 
     29 /*
     30  *   This driver originated as the hso module for FreeBSD written by
     31  * Fredrik Lindberg[1]. It has been rewritten almost completely for
     32  * NetBSD, and to support more devices with information extracted from
     33  * the Linux hso driver provided by Option N.V.[2]
     34  *
     35  *   [1] http://www.shapeshifter.se/code/hso
     36  *   [2] http://www.pharscape.org/hso.htm
     37  */
     38 
     39 #include <sys/cdefs.h>
     40 __KERNEL_RCSID(0, "$NetBSD: uhso.c,v 1.17.2.5 2015/10/06 21:32:15 skrll Exp $");
     41 
     42 #ifdef _KERNEL_OPT
     43 #include "opt_inet.h"
     44 #endif
     45 
     46 #include <sys/param.h>
     47 #include <sys/conf.h>
     48 #include <sys/fcntl.h>
     49 #include <sys/kauth.h>
     50 #include <sys/kernel.h>
     51 #include <sys/kmem.h>
     52 #include <sys/mbuf.h>
     53 #include <sys/poll.h>
     54 #include <sys/queue.h>
     55 #include <sys/socket.h>
     56 #include <sys/sysctl.h>
     57 #include <sys/systm.h>
     58 #include <sys/tty.h>
     59 #include <sys/vnode.h>
     60 #include <sys/lwp.h>
     61 
     62 #include <net/bpf.h>
     63 #include <net/if.h>
     64 #include <net/if_dl.h>
     65 #include <net/if_types.h>
     66 #include <net/netisr.h>
     67 
     68 #include <netinet/in_systm.h>
     69 #include <netinet/in_var.h>
     70 #include <netinet/ip.h>
     71 
     72 #include <dev/usb/usb.h>
     73 #include <dev/usb/usbcdc.h>
     74 #include <dev/usb/usbdi.h>
     75 #include <dev/usb/usbdi_util.h>
     76 #include <dev/usb/umassvar.h>
     77 
     78 #include <dev/scsipi/scsi_disk.h>
     79 
     80 #include "usbdevs.h"
     81 
     82 #undef DPRINTF
     83 #ifdef UHSO_DEBUG
     84 /*
     85  * defined levels
     86  *	0	warnings only
     87  *	1	informational
     88  *	5	really chatty
     89  */
     90 int uhso_debug = 0;
     91 
     92 #define DPRINTF(n, ...)	do {			\
     93 	if (uhso_debug >= (n)) {		\
     94 		printf("%s: ", __func__);	\
     95 		printf(__VA_ARGS__);		\
     96 	}					\
     97 } while (/* CONSTCOND */0)
     98 #else
     99 #define DPRINTF(...)	((void)0)
    100 #endif
    101 
    102 /*
    103  * When first attached, the device class will be 0 and the modem
    104  * will attach as UMASS until a SCSI REZERO_UNIT command is sent,
    105  * in which case it will detach and reattach with device class set
    106  * to UDCLASS_VENDOR (0xff) and provide the serial interfaces.
    107  *
    108  * If autoswitch is set (the default) this will happen automatically.
    109  */
    110 Static int uhso_autoswitch = 1;
    111 
    112 SYSCTL_SETUP(sysctl_hw_uhso_setup, "uhso sysctl setup")
    113 {
    114 	const struct sysctlnode *node = NULL;
    115 
    116 	sysctl_createv(clog, 0, NULL, &node,
    117 		CTLFLAG_PERMANENT,
    118 		CTLTYPE_NODE, "uhso",
    119 		NULL,
    120 		NULL, 0,
    121 		NULL, 0,
    122 		CTL_HW, CTL_CREATE, CTL_EOL);
    123 
    124 	if (node == NULL)
    125 		return;
    126 
    127 #ifdef UHSO_DEBUG
    128 	sysctl_createv(clog, 0, &node, NULL,
    129 		CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
    130 		CTLTYPE_INT, "debug",
    131 		SYSCTL_DESCR("uhso debug level (0, 1, 5)"),
    132 		NULL, 0,
    133 		&uhso_debug, sizeof(uhso_debug),
    134 		CTL_CREATE, CTL_EOL);
    135 #endif
    136 
    137 	sysctl_createv(clog, 0, &node, NULL,
    138 		CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
    139 		CTLTYPE_INT, "autoswitch",
    140 		SYSCTL_DESCR("automatically switch device into modem mode"),
    141 		NULL, 0,
    142 		&uhso_autoswitch, sizeof(uhso_autoswitch),
    143 		CTL_CREATE, CTL_EOL);
    144 }
    145 
    146 /*
    147  * The uhso modems have a number of interfaces providing a variety of
    148  * IO ports using the bulk endpoints, or multiplexed on the control
    149  * endpoints. We separate the ports by function and provide each with
    150  * a predictable index number used to construct the device minor number.
    151  *
    152  * The Network port is configured as a network interface rather than
    153  * a tty as it provides raw IPv4 packets.
    154  */
    155 
    156 Static const char *uhso_port_name[] = {
    157 	"Control",
    158 	"Diagnostic",
    159 	"Diagnostic2",
    160 	"Application",
    161 	"Application2",
    162 	"GPS",
    163 	"GPS Control",
    164 	"PC Smartcard",
    165 	"Modem",
    166 	"MSD",			/* "Modem Sharing Device" ? */
    167 	"Voice",
    168 	"Network",
    169 };
    170 
    171 #define UHSO_PORT_CONTROL	0x00
    172 #define UHSO_PORT_DIAG		0x01
    173 #define UHSO_PORT_DIAG2		0x02
    174 #define UHSO_PORT_APP		0x03
    175 #define UHSO_PORT_APP2		0x04
    176 #define UHSO_PORT_GPS		0x05
    177 #define UHSO_PORT_GPS_CONTROL	0x06
    178 #define UHSO_PORT_PCSC		0x07
    179 #define UHSO_PORT_MODEM		0x08
    180 #define UHSO_PORT_MSD		0x09
    181 #define UHSO_PORT_VOICE		0x0a
    182 #define UHSO_PORT_NETWORK	0x0b
    183 
    184 #define UHSO_PORT_MAX		__arraycount(uhso_port_name)
    185 
    186 #define UHSO_IFACE_MUX		0x20
    187 #define UHSO_IFACE_BULK		0x40
    188 #define UHSO_IFACE_IFNET	0x80
    189 
    190 /*
    191  * The interface specification can sometimes be deduced from the device
    192  * type and interface number, or some modems support a vendor specific
    193  * way to read config info which we can translate to the port index.
    194  */
    195 Static const uint8_t uhso_spec_default[] = {
    196 	UHSO_IFACE_IFNET | UHSO_PORT_NETWORK | UHSO_IFACE_MUX,
    197 	UHSO_IFACE_BULK | UHSO_PORT_DIAG,
    198 	UHSO_IFACE_BULK | UHSO_PORT_MODEM,
    199 };
    200 
    201 Static const uint8_t uhso_spec_icon321[] = {
    202 	UHSO_IFACE_IFNET | UHSO_PORT_NETWORK | UHSO_IFACE_MUX,
    203 	UHSO_IFACE_BULK | UHSO_PORT_DIAG2,
    204 	UHSO_IFACE_BULK | UHSO_PORT_MODEM,
    205 	UHSO_IFACE_BULK | UHSO_PORT_DIAG,
    206 };
    207 
    208 Static const uint8_t uhso_spec_config[] = {
    209 	0,
    210 	UHSO_IFACE_BULK | UHSO_PORT_DIAG,
    211 	UHSO_IFACE_BULK | UHSO_PORT_GPS,
    212 	UHSO_IFACE_BULK | UHSO_PORT_GPS_CONTROL,
    213 	UHSO_IFACE_BULK | UHSO_PORT_APP,
    214 	UHSO_IFACE_BULK | UHSO_PORT_APP2,
    215 	UHSO_IFACE_BULK | UHSO_PORT_CONTROL,
    216 	UHSO_IFACE_IFNET | UHSO_PORT_NETWORK,
    217 	UHSO_IFACE_BULK | UHSO_PORT_MODEM,
    218 	UHSO_IFACE_BULK | UHSO_PORT_MSD,
    219 	UHSO_IFACE_BULK | UHSO_PORT_PCSC,
    220 	UHSO_IFACE_BULK | UHSO_PORT_VOICE,
    221 };
    222 
    223 struct uhso_dev {
    224 	uint16_t vendor;
    225 	uint16_t product;
    226 	uint16_t type;
    227 };
    228 
    229 #define UHSOTYPE_DEFAULT	1
    230 #define UHSOTYPE_ICON321	2
    231 #define UHSOTYPE_CONFIG		3
    232 
    233 Static const struct uhso_dev uhso_devs[] = {
    234     { USB_VENDOR_OPTIONNV, USB_PRODUCT_OPTIONNV_MAXHSDPA,    UHSOTYPE_DEFAULT },
    235     { USB_VENDOR_OPTIONNV, USB_PRODUCT_OPTIONNV_GSICON72,    UHSOTYPE_DEFAULT },
    236     { USB_VENDOR_OPTIONNV, USB_PRODUCT_OPTIONNV_ICON225,     UHSOTYPE_DEFAULT },
    237     { USB_VENDOR_OPTIONNV, USB_PRODUCT_OPTIONNV_GEHSUPA,     UHSOTYPE_DEFAULT },
    238     { USB_VENDOR_OPTIONNV, USB_PRODUCT_OPTIONNV_GTHSUPA,     UHSOTYPE_DEFAULT },
    239     { USB_VENDOR_OPTIONNV, USB_PRODUCT_OPTIONNV_GSHSUPA,     UHSOTYPE_DEFAULT },
    240     { USB_VENDOR_OPTIONNV, USB_PRODUCT_OPTIONNV_GE40X1,      UHSOTYPE_CONFIG },
    241     { USB_VENDOR_OPTIONNV, USB_PRODUCT_OPTIONNV_GE40X2,      UHSOTYPE_CONFIG },
    242     { USB_VENDOR_OPTIONNV, USB_PRODUCT_OPTIONNV_GE40X3,      UHSOTYPE_CONFIG },
    243     { USB_VENDOR_OPTIONNV, USB_PRODUCT_OPTIONNV_ICON401,     UHSOTYPE_CONFIG },
    244     { USB_VENDOR_OPTIONNV, USB_PRODUCT_OPTIONNV_GTM382,	     UHSOTYPE_CONFIG },
    245     { USB_VENDOR_OPTIONNV, USB_PRODUCT_OPTIONNV_GE40X4,      UHSOTYPE_CONFIG },
    246     { USB_VENDOR_OPTIONNV, USB_PRODUCT_OPTIONNV_GTHSUPAM,    UHSOTYPE_CONFIG },
    247     { USB_VENDOR_OPTIONNV, USB_PRODUCT_OPTIONNV_ICONEDGE,    UHSOTYPE_DEFAULT },
    248     { USB_VENDOR_OPTIONNV, USB_PRODUCT_OPTIONNV_MODHSXPA,    UHSOTYPE_ICON321 },
    249     { USB_VENDOR_OPTIONNV, USB_PRODUCT_OPTIONNV_ICON321,     UHSOTYPE_ICON321 },
    250     { USB_VENDOR_OPTIONNV, USB_PRODUCT_OPTIONNV_ICON322,     UHSOTYPE_ICON321 },
    251     { USB_VENDOR_OPTIONNV, USB_PRODUCT_OPTIONNV_ICON505,     UHSOTYPE_CONFIG },
    252 };
    253 
    254 #define uhso_lookup(p, v)  ((const struct uhso_dev *)usb_lookup(uhso_devs, (p), (v)))
    255 
    256 /* IO buffer sizes */
    257 #define UHSO_MUX_WSIZE		64
    258 #define UHSO_MUX_RSIZE		1024
    259 #define UHSO_BULK_WSIZE		8192
    260 #define UHSO_BULK_RSIZE		4096
    261 #define UHSO_IFNET_MTU		1500
    262 
    263 /*
    264  * Each IO port provided by the modem can be mapped to a network
    265  * interface (when hp_ifp != NULL) or a tty (when hp_tp != NULL)
    266  * which may be multiplexed and sharing interrupt and control endpoints
    267  * from an interface, or using the dedicated bulk endpoints.
    268  */
    269 
    270 struct uhso_port;
    271 struct uhso_softc;
    272 
    273 /* uhso callback functions return errno on failure */
    274 typedef int (*uhso_callback)(struct uhso_port *);
    275 
    276 struct uhso_port {
    277 	struct uhso_softc      *hp_sc;		/* master softc */
    278 	struct tty	       *hp_tp;		/* tty pointer */
    279 	struct ifnet	       *hp_ifp;		/* ifnet pointer */
    280 	unsigned int		hp_flags;	/* see below */
    281 	int			hp_swflags;	/* persistent tty flags */
    282 	int			hp_status;	/* modem status */
    283 
    284 	/* port type specific handlers */
    285 	uhso_callback		hp_abort;	/* abort any transfers */
    286 	uhso_callback		hp_detach;	/* detach port completely */
    287 	uhso_callback		hp_init;	/* init port (first open) */
    288 	uhso_callback		hp_clean;	/* clean port (last close) */
    289 	uhso_callback		hp_write;	/* write data */
    290 	usbd_callback		hp_write_cb;	/* write callback */
    291 	uhso_callback		hp_read;	/* read data */
    292 	usbd_callback		hp_read_cb;	/* read callback */
    293 	uhso_callback		hp_control;	/* set control lines */
    294 
    295 	struct usbd_interface *	hp_ifh;		/* interface handle */
    296 	unsigned int		hp_index;	/* usb request index */
    297 
    298 	int			hp_iaddr;	/* interrupt endpoint */
    299 	struct usbd_pipe *	hp_ipipe;	/* interrupt pipe */
    300 	void		       *hp_ibuf;	/* interrupt buffer */
    301 	size_t			hp_isize;	/* allocated size */
    302 
    303 	int			hp_raddr;	/* bulk in endpoint */
    304 	struct usbd_pipe *	hp_rpipe;	/* bulk in pipe */
    305 	struct usbd_xfer *	hp_rxfer;	/* input xfer */
    306 	void		       *hp_rbuf;	/* input buffer */
    307 	size_t			hp_rlen;	/* fill length */
    308 	size_t			hp_rsize;	/* allocated size */
    309 
    310 	int			hp_waddr;	/* bulk out endpoint */
    311 	struct usbd_pipe *	hp_wpipe;	/* bulk out pipe */
    312 	struct usbd_xfer *	hp_wxfer;	/* output xfer */
    313 	void		       *hp_wbuf;	/* output buffer */
    314 	size_t			hp_wlen;	/* fill length */
    315 	size_t			hp_wsize;	/* allocated size */
    316 
    317 	struct mbuf	       *hp_mbuf;	/* partial packet */
    318 };
    319 
    320 /* hp_flags */
    321 #define UHSO_PORT_MUXPIPE	__BIT(0)	/* duplicate ipipe/ibuf references */
    322 #define UHSO_PORT_MUXREADY	__BIT(1)	/* input is ready */
    323 #define UHSO_PORT_MUXBUSY	__BIT(2)	/* read in progress */
    324 
    325 struct uhso_softc {
    326 	device_t		sc_dev;		/* self */
    327 	struct usbd_device *	sc_udev;
    328 	int			sc_refcnt;
    329 	struct uhso_port       *sc_port[UHSO_PORT_MAX];
    330 };
    331 
    332 #define UHSO_CONFIG_NO		1
    333 
    334 int uhso_match(device_t, cfdata_t, void *);
    335 void uhso_attach(device_t, device_t, void *);
    336 int uhso_detach(device_t, int);
    337 
    338 extern struct cfdriver uhso_cd;
    339 
    340 CFATTACH_DECL_NEW(uhso, sizeof(struct uhso_softc), uhso_match, uhso_attach,
    341     uhso_detach, NULL);
    342 
    343 Static int uhso_switch_mode(struct usbd_device *);
    344 Static int uhso_get_iface_spec(struct usb_attach_arg *, uint8_t, uint8_t *);
    345 Static usb_endpoint_descriptor_t *uhso_get_endpoint(struct usbd_interface *, int, int);
    346 
    347 Static void uhso_mux_attach(struct uhso_softc *, struct usbd_interface *, int);
    348 Static int  uhso_mux_abort(struct uhso_port *);
    349 Static int  uhso_mux_detach(struct uhso_port *);
    350 Static int  uhso_mux_init(struct uhso_port *);
    351 Static int  uhso_mux_clean(struct uhso_port *);
    352 Static int  uhso_mux_write(struct uhso_port *);
    353 Static int  uhso_mux_read(struct uhso_port *);
    354 Static int  uhso_mux_control(struct uhso_port *);
    355 Static void uhso_mux_intr(struct usbd_xfer *, void *, usbd_status);
    356 
    357 Static void uhso_bulk_attach(struct uhso_softc *, struct usbd_interface *, int);
    358 Static int  uhso_bulk_abort(struct uhso_port *);
    359 Static int  uhso_bulk_detach(struct uhso_port *);
    360 Static int  uhso_bulk_init(struct uhso_port *);
    361 Static int  uhso_bulk_clean(struct uhso_port *);
    362 Static int  uhso_bulk_write(struct uhso_port *);
    363 Static int  uhso_bulk_read(struct uhso_port *);
    364 Static int  uhso_bulk_control(struct uhso_port *);
    365 Static void uhso_bulk_intr(struct usbd_xfer *, void *, usbd_status);
    366 
    367 Static void uhso_tty_attach(struct uhso_port *);
    368 Static void uhso_tty_detach(struct uhso_port *);
    369 Static void uhso_tty_read_cb(struct usbd_xfer *, void *, usbd_status);
    370 Static void uhso_tty_write_cb(struct usbd_xfer *, void *, usbd_status);
    371 
    372 dev_type_open(uhso_tty_open);
    373 dev_type_close(uhso_tty_close);
    374 dev_type_read(uhso_tty_read);
    375 dev_type_write(uhso_tty_write);
    376 dev_type_ioctl(uhso_tty_ioctl);
    377 dev_type_stop(uhso_tty_stop);
    378 dev_type_tty(uhso_tty_tty);
    379 dev_type_poll(uhso_tty_poll);
    380 
    381 const struct cdevsw uhso_cdevsw = {
    382 	.d_open = uhso_tty_open,
    383 	.d_close = uhso_tty_close,
    384 	.d_read = uhso_tty_read,
    385 	.d_write = uhso_tty_write,
    386 	.d_ioctl = uhso_tty_ioctl,
    387 	.d_stop = uhso_tty_stop,
    388 	.d_tty = uhso_tty_tty,
    389 	.d_poll = uhso_tty_poll,
    390 	.d_mmap = nommap,
    391 	.d_kqfilter = ttykqfilter,
    392 	.d_discard = nodiscard,
    393 	.d_flag = D_TTY
    394 };
    395 
    396 Static int  uhso_tty_init(struct uhso_port *);
    397 Static void uhso_tty_clean(struct uhso_port *);
    398 Static int  uhso_tty_do_ioctl(struct uhso_port *, u_long, void *, int, struct lwp *);
    399 Static void uhso_tty_start(struct tty *);
    400 Static int  uhso_tty_param(struct tty *, struct termios *);
    401 Static int  uhso_tty_control(struct uhso_port *, u_long, int);
    402 
    403 #define UHSO_UNIT_MASK		TTUNIT_MASK
    404 #define UHSO_PORT_MASK		0x0000f
    405 #define UHSO_DIALOUT_MASK	TTDIALOUT_MASK
    406 #define UHSO_CALLUNIT_MASK	TTCALLUNIT_MASK
    407 
    408 #define UHSOUNIT(x)	(TTUNIT(x) >> 4)
    409 #define UHSOPORT(x)	(TTUNIT(x) & UHSO_PORT_MASK)
    410 #define UHSODIALOUT(x)	TTDIALOUT(x)
    411 #define UHSOMINOR(u, p)	((((u) << 4) & UHSO_UNIT_MASK) | ((p) & UHSO_UNIT_MASK))
    412 
    413 Static void uhso_ifnet_attach(struct uhso_softc *, struct usbd_interface *, int);
    414 Static int  uhso_ifnet_abort(struct uhso_port *);
    415 Static int  uhso_ifnet_detach(struct uhso_port *);
    416 Static void uhso_ifnet_read_cb(struct usbd_xfer *, void *, usbd_status);
    417 Static void uhso_ifnet_input(struct ifnet *, struct mbuf **, uint8_t *, size_t);
    418 Static void uhso_ifnet_write_cb(struct usbd_xfer *, void *, usbd_status);
    419 
    420 Static int  uhso_ifnet_ioctl(struct ifnet *, u_long, void *);
    421 Static int  uhso_ifnet_init(struct uhso_port *);
    422 Static void uhso_ifnet_clean(struct uhso_port *);
    423 Static void uhso_ifnet_start(struct ifnet *);
    424 Static int  uhso_ifnet_output(struct ifnet *, struct mbuf *, const struct sockaddr *, struct rtentry *);
    425 
    426 
    427 /*******************************************************************************
    428  *
    429  *	USB autoconfig
    430  *
    431  */
    432 
    433 int
    434 uhso_match(device_t parent, cfdata_t match, void *aux)
    435 {
    436 	struct usb_attach_arg *uaa = aux;
    437 
    438 	/*
    439 	 * don't claim this device if autoswitch is disabled
    440 	 * and it is not in modem mode already
    441 	 */
    442 	if (!uhso_autoswitch && uaa->uaa_class != UDCLASS_VENDOR)
    443 		return UMATCH_NONE;
    444 
    445 	if (uhso_lookup(uaa->uaa_vendor, uaa->uaa_product))
    446 		return UMATCH_VENDOR_PRODUCT;
    447 
    448 	return UMATCH_NONE;
    449 }
    450 
    451 void
    452 uhso_attach(device_t parent, device_t self, void *aux)
    453 {
    454 	struct uhso_softc *sc = device_private(self);
    455 	struct usb_attach_arg *uaa = aux;
    456 	struct usbd_interface * ifh;
    457 	char *devinfop;
    458 	uint8_t count, i, spec;
    459 	usbd_status status;
    460 
    461 	DPRINTF(1, ": sc = %p, self=%p", sc, self);
    462 
    463 	sc->sc_dev = self;
    464 	sc->sc_udev = uaa->uaa_device;
    465 
    466 	aprint_naive("\n");
    467 	aprint_normal("\n");
    468 
    469 	devinfop = usbd_devinfo_alloc(uaa->uaa_device, 0);
    470 	aprint_normal_dev(self, "%s\n", devinfop);
    471 	usbd_devinfo_free(devinfop);
    472 
    473 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, sc->sc_dev);
    474 
    475 	status = usbd_set_config_no(sc->sc_udev, UHSO_CONFIG_NO, 1);
    476 	if (status != USBD_NORMAL_COMPLETION) {
    477 		aprint_error_dev(self, "failed to set configuration"
    478 		    ", err=%s\n", usbd_errstr(status));
    479 		return;
    480 	}
    481 
    482 	if (uaa->uaa_class != UDCLASS_VENDOR) {
    483 		aprint_verbose_dev(self, "Switching device into modem mode..\n");
    484 		if (uhso_switch_mode(uaa->uaa_device) != 0)
    485 			aprint_error_dev(self, "modem switch failed\n");
    486 
    487 		return;
    488 	}
    489 
    490 	count = 0;
    491 	(void)usbd_interface_count(sc->sc_udev, &count);
    492 	DPRINTF(1, "interface count %d\n", count);
    493 
    494 	for (i = 0; i < count; i++) {
    495 		status = usbd_device2interface_handle(sc->sc_udev, i, &ifh);
    496 		if (status != USBD_NORMAL_COMPLETION) {
    497 			aprint_error_dev(self,
    498 			    "could not get interface %d: %s\n",
    499 			    i, usbd_errstr(status));
    500 
    501 			return;
    502 		}
    503 
    504 		if (!uhso_get_iface_spec(uaa, i, &spec)) {
    505 			aprint_error_dev(self,
    506 			    "could not get interface %d specification\n", i);
    507 
    508 			return;
    509 		}
    510 
    511 		if (ISSET(spec, UHSO_IFACE_MUX))
    512 			uhso_mux_attach(sc, ifh, UHSOPORT(spec));
    513 
    514 		if (ISSET(spec, UHSO_IFACE_BULK))
    515 			uhso_bulk_attach(sc, ifh, UHSOPORT(spec));
    516 
    517 		if (ISSET(spec, UHSO_IFACE_IFNET))
    518 			uhso_ifnet_attach(sc, ifh, UHSOPORT(spec));
    519 	}
    520 
    521 	if (!pmf_device_register(self, NULL, NULL))
    522 		aprint_error_dev(self, "couldn't establish power handler\n");
    523 }
    524 
    525 int
    526 uhso_detach(device_t self, int flags)
    527 {
    528 	struct uhso_softc *sc = device_private(self);
    529 	struct uhso_port *hp;
    530 	devmajor_t major;
    531 	devminor_t minor;
    532 	unsigned int i;
    533 	int s;
    534 
    535 	pmf_device_deregister(self);
    536 
    537 	for (i = 0; i < UHSO_PORT_MAX; i++) {
    538 		hp = sc->sc_port[i];
    539 		if (hp != NULL)
    540 			(*hp->hp_abort)(hp);
    541 	}
    542 
    543 	s = splusb();
    544 	if (sc->sc_refcnt-- > 0) {
    545 		DPRINTF(1, "waiting for refcnt (%d)..\n", sc->sc_refcnt);
    546 		usb_detach_waitold(sc->sc_dev);
    547 	}
    548 	splx(s);
    549 
    550 	/*
    551 	 * XXX the tty close routine increases/decreases refcnt causing
    552 	 * XXX another usb_detach_wakeupold() does it matter, should these
    553 	 * XXX be before the detach_wait? or before the abort?
    554 	 */
    555 
    556 	/* Nuke the vnodes for any open instances (calls close). */
    557 	major = cdevsw_lookup_major(&uhso_cdevsw);
    558 	minor = UHSOMINOR(device_unit(sc->sc_dev), 0);
    559 	vdevgone(major, minor, minor + UHSO_PORT_MAX, VCHR);
    560 	minor = UHSOMINOR(device_unit(sc->sc_dev), 0) | UHSO_DIALOUT_MASK;
    561 	vdevgone(major, minor, minor + UHSO_PORT_MAX, VCHR);
    562 	minor = UHSOMINOR(device_unit(sc->sc_dev), 0) | UHSO_CALLUNIT_MASK;
    563 	vdevgone(major, minor, minor + UHSO_PORT_MAX, VCHR);
    564 
    565 	for (i = 0; i < UHSO_PORT_MAX; i++) {
    566 		hp = sc->sc_port[i];
    567 		if (hp != NULL)
    568 			(*hp->hp_detach)(hp);
    569 	}
    570 
    571 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, sc->sc_dev);
    572 
    573 	return 0;
    574 }
    575 
    576 /*
    577  * Send SCSI REZERO_UNIT command to switch device into modem mode
    578  */
    579 Static int
    580 uhso_switch_mode(struct usbd_device *udev)
    581 {
    582 	umass_bbb_cbw_t	cmd;
    583 	usb_endpoint_descriptor_t *ed;
    584 	struct usbd_interface * ifh;
    585 	struct usbd_pipe * pipe;
    586 	struct usbd_xfer *xfer;
    587 	usbd_status status;
    588 
    589 	status = usbd_device2interface_handle(udev, 0, &ifh);
    590 	if (status != USBD_NORMAL_COMPLETION)
    591 		return EIO;
    592 
    593 	ed = uhso_get_endpoint(ifh, UE_BULK, UE_DIR_OUT);
    594 	if (ed == NULL)
    595 		return ENODEV;
    596 
    597 	status = usbd_open_pipe(ifh, ed->bEndpointAddress, 0, &pipe);
    598 	if (status != USBD_NORMAL_COMPLETION)
    599 		return EIO;
    600 
    601 	int error = usbd_create_xfer(pipe, sizeof(cmd), 0, 0, &xfer);
    602 	if (error)
    603 		return error;
    604 
    605 	USETDW(cmd.dCBWSignature, CBWSIGNATURE);
    606 	USETDW(cmd.dCBWTag, 1);
    607 	USETDW(cmd.dCBWDataTransferLength, 0);
    608 	cmd.bCBWFlags = CBWFLAGS_OUT;
    609 	cmd.bCBWLUN = 0;
    610 	cmd.bCDBLength = 6;
    611 
    612 	memset(&cmd.CBWCDB, 0, CBWCDBLENGTH);
    613 	cmd.CBWCDB[0] = SCSI_REZERO_UNIT;
    614 
    615 	usbd_setup_xfer(xfer, NULL, &cmd, sizeof(cmd),
    616 		USBD_SYNCHRONOUS, USBD_DEFAULT_TIMEOUT, NULL);
    617 
    618 	status = usbd_transfer(xfer);
    619 
    620 	usbd_abort_pipe(pipe);
    621 	usbd_close_pipe(pipe);
    622 	usbd_destroy_xfer(xfer);
    623 
    624 	return (status == USBD_NORMAL_COMPLETION ? 0 : EIO);
    625 }
    626 
    627 Static int
    628 uhso_get_iface_spec(struct usb_attach_arg *uaa, uint8_t ifnum, uint8_t *spec)
    629 {
    630 	const struct uhso_dev *hd;
    631 	uint8_t config[17];
    632 	usb_device_request_t req;
    633 	usbd_status status;
    634 
    635 	hd = uhso_lookup(uaa->uaa_vendor, uaa->uaa_product);
    636 	KASSERT(hd != NULL);
    637 
    638 	switch (hd->type) {
    639 	case UHSOTYPE_DEFAULT:
    640 		if (ifnum > __arraycount(uhso_spec_default))
    641 			break;
    642 
    643 		*spec = uhso_spec_default[ifnum];
    644 		return 1;
    645 
    646 	case UHSOTYPE_ICON321:
    647 		if (ifnum > __arraycount(uhso_spec_icon321))
    648 			break;
    649 
    650 		*spec = uhso_spec_icon321[ifnum];
    651 		return 1;
    652 
    653 	case UHSOTYPE_CONFIG:
    654 		req.bmRequestType = UT_READ_VENDOR_DEVICE;
    655 		req.bRequest = 0x86;	/* "Config Info" */
    656 		USETW(req.wValue, 0);
    657 		USETW(req.wIndex, 0);
    658 		USETW(req.wLength, sizeof(config));
    659 
    660 		status = usbd_do_request(uaa->uaa_device, &req, config);
    661 		if (status != USBD_NORMAL_COMPLETION)
    662 			break;
    663 
    664 		if (ifnum > __arraycount(config)
    665 		    || config[ifnum] > __arraycount(uhso_spec_config))
    666 			break;
    667 
    668 		*spec = uhso_spec_config[config[ifnum]];
    669 
    670 		/*
    671 		 * Apparently some modems also have a CRC bug that is
    672 		 * indicated by ISSET(config[16], __BIT(0)) but we dont
    673 		 * handle it at this time.
    674 		 */
    675 		return 1;
    676 
    677 	default:
    678 		DPRINTF(0, "unknown interface type\n");
    679 		break;
    680 	}
    681 
    682 	return 0;
    683 }
    684 
    685 Static usb_endpoint_descriptor_t *
    686 uhso_get_endpoint(struct usbd_interface * ifh, int type, int dir)
    687 {
    688 	usb_endpoint_descriptor_t *ed;
    689 	uint8_t count, i;
    690 
    691 	count = 0;
    692 	(void)usbd_endpoint_count(ifh, &count);
    693 
    694 	for (i = 0; i < count; i++) {
    695 		ed = usbd_interface2endpoint_descriptor(ifh, i);
    696 		if (ed != NULL
    697 		    && UE_GET_XFERTYPE(ed->bmAttributes) == type
    698 		    && UE_GET_DIR(ed->bEndpointAddress) == dir)
    699 			return ed;
    700 	}
    701 
    702 	return NULL;
    703 }
    704 
    705 
    706 /******************************************************************************
    707  *
    708  *	Multiplexed ports signal with the interrupt endpoint to indicate
    709  *  when data is available for reading, and a separate request is made on
    710  *  the control endpoint to read or write on each port. The offsets in the
    711  *  table below relate to bit numbers in the mux mask, identifying each port.
    712  */
    713 
    714 Static const int uhso_mux_port[] = {
    715 	UHSO_PORT_CONTROL,
    716 	UHSO_PORT_APP,
    717 	UHSO_PORT_PCSC,
    718 	UHSO_PORT_GPS,
    719 	UHSO_PORT_APP2,
    720 };
    721 
    722 Static void
    723 uhso_mux_attach(struct uhso_softc *sc, struct usbd_interface * ifh, int index)
    724 {
    725 	usbd_desc_iter_t iter;
    726 	const usb_descriptor_t *desc;
    727 	usb_endpoint_descriptor_t *ed;
    728 	struct usbd_pipe * pipe;
    729 	struct uhso_port *hp;
    730 	uint8_t *buf;
    731 	size_t size;
    732 	unsigned int i, mux, flags;
    733 	int addr;
    734 	usbd_status status;
    735 
    736 	ed = uhso_get_endpoint(ifh, UE_INTERRUPT, UE_DIR_IN);
    737 	if (ed == NULL) {
    738 		aprint_error_dev(sc->sc_dev, "no interrupt endpoint\n");
    739 		return;
    740 	}
    741 	addr = ed->bEndpointAddress;
    742 	size = UGETW(ed->wMaxPacketSize);
    743 
    744 	/*
    745 	 * There should be an additional "Class Specific" descriptor on
    746 	 * the mux interface containing a single byte with a bitmask of
    747 	 * enabled ports. We need to look through the device descriptor
    748 	 * to find it and the port index is found from the uhso_mux_port
    749 	 * array, above.
    750 	 */
    751 	usb_desc_iter_init(sc->sc_udev, &iter);
    752 
    753 	/* skip past the current interface descriptor */
    754 	iter.cur = (const uByte *)usbd_get_interface_descriptor(ifh);
    755 	desc = usb_desc_iter_next(&iter);
    756 
    757 	for (;;) {
    758 		desc = usb_desc_iter_next(&iter);
    759 		if (desc == NULL
    760 		    || desc->bDescriptorType == UDESC_INTERFACE) {
    761 			mux = 0;
    762 			break;	/* not found */
    763 		}
    764 
    765 		if (desc->bDescriptorType == UDESC_CS_INTERFACE
    766 		    && desc->bLength == 3) {
    767 			mux = ((const uint8_t *)desc)[2];
    768 			break;
    769 		}
    770 	}
    771 
    772 	DPRINTF(1, "addr=%d, size=%zd, mux=0x%02x\n", addr, size, mux);
    773 
    774 	buf = kmem_alloc(size, KM_SLEEP);
    775 	status = usbd_open_pipe_intr(ifh, addr, USBD_SHORT_XFER_OK, &pipe,
    776 	    sc, buf, size, uhso_mux_intr, USBD_DEFAULT_INTERVAL);
    777 
    778 	if (status != USBD_NORMAL_COMPLETION) {
    779 		aprint_error_dev(sc->sc_dev, "failed to open interrupt pipe: %s",
    780 		    usbd_errstr(status));
    781 
    782 		kmem_free(buf, size);
    783 		return;
    784 	}
    785 
    786 	flags = 0;
    787 	for (i = 0; i < __arraycount(uhso_mux_port); i++) {
    788 		if (ISSET(mux, __BIT(i))) {
    789 			if (sc->sc_port[uhso_mux_port[i]] != NULL) {
    790 				aprint_error_dev(sc->sc_dev,
    791 				    "mux port %d is duplicate!\n", i);
    792 
    793 				continue;
    794 			}
    795 
    796 			hp = kmem_zalloc(sizeof(struct uhso_port), KM_SLEEP);
    797 			sc->sc_port[uhso_mux_port[i]] = hp;
    798 
    799 			hp->hp_sc = sc;
    800 			hp->hp_index = i;
    801 			hp->hp_ipipe = pipe;
    802 			hp->hp_ibuf = buf;
    803 			hp->hp_isize = size;
    804 			hp->hp_flags = flags;
    805 			hp->hp_abort = uhso_mux_abort;
    806 			hp->hp_detach = uhso_mux_detach;
    807 			hp->hp_init = uhso_mux_init;
    808 			hp->hp_clean = uhso_mux_clean;
    809 			hp->hp_write = uhso_mux_write;
    810 			hp->hp_write_cb = uhso_tty_write_cb;
    811 			hp->hp_read = uhso_mux_read;
    812 			hp->hp_read_cb = uhso_tty_read_cb;
    813 			hp->hp_control = uhso_mux_control;
    814 			hp->hp_wsize = UHSO_MUX_WSIZE;
    815 			hp->hp_rsize = UHSO_MUX_RSIZE;
    816 
    817 			uhso_tty_attach(hp);
    818 
    819 			aprint_normal_dev(sc->sc_dev,
    820 			    "%s (port %d) attached as mux tty\n",
    821 			    uhso_port_name[uhso_mux_port[i]], uhso_mux_port[i]);
    822 
    823 			/*
    824 			 * As the pipe handle is stored in each mux, mark
    825 			 * secondary references so they don't get released
    826 			 */
    827 			flags = UHSO_PORT_MUXPIPE;
    828 		}
    829 	}
    830 
    831 	if (flags == 0) {
    832 		/* for whatever reasons, nothing was attached */
    833 		usbd_abort_pipe(pipe);
    834 		usbd_close_pipe(pipe);
    835 		kmem_free(buf, size);
    836 	}
    837 }
    838 
    839 Static int
    840 uhso_mux_abort(struct uhso_port *hp)
    841 {
    842 	struct uhso_softc *sc = hp->hp_sc;
    843 
    844 	DPRINTF(1, "hp=%p\n", hp);
    845 
    846 	if (!ISSET(hp->hp_flags, UHSO_PORT_MUXPIPE))
    847 		usbd_abort_pipe(hp->hp_ipipe);
    848 
    849 	usbd_abort_default_pipe(sc->sc_udev);
    850 
    851 	return (*hp->hp_clean)(hp);
    852 }
    853 
    854 Static int
    855 uhso_mux_detach(struct uhso_port *hp)
    856 {
    857 
    858 	DPRINTF(1, "hp=%p\n", hp);
    859 
    860 	if (!ISSET(hp->hp_flags, UHSO_PORT_MUXPIPE)) {
    861 		DPRINTF(1, "interrupt pipe closed\n");
    862 		usbd_abort_pipe(hp->hp_ipipe);
    863 		usbd_close_pipe(hp->hp_ipipe);
    864 		kmem_free(hp->hp_ibuf, hp->hp_isize);
    865 	}
    866 
    867 	uhso_tty_detach(hp);
    868 	kmem_free(hp, sizeof(struct uhso_port));
    869 	return 0;
    870 }
    871 
    872 Static int
    873 uhso_mux_init(struct uhso_port *hp)
    874 {
    875 
    876 	DPRINTF(1, "hp=%p\n", hp);
    877 
    878 	CLR(hp->hp_flags, UHSO_PORT_MUXBUSY | UHSO_PORT_MUXREADY);
    879 	SET(hp->hp_status, TIOCM_DSR | TIOCM_CAR);
    880 
    881 	struct uhso_softc *sc = hp->hp_sc;
    882 	struct usbd_pipe *pipe0 = usbd_get_pipe0(sc->sc_udev);
    883 	int error;
    884 
    885 	error = usbd_create_xfer(pipe0, hp->hp_rsize, 0, 0, &hp->hp_rxfer);
    886 	if (error)
    887 		return error;
    888 
    889 	hp->hp_rbuf = usbd_get_buffer(hp->hp_rxfer);
    890 
    891 	error = usbd_create_xfer(pipe0, hp->hp_wsize, 0, 0, &hp->hp_wxfer);
    892 	if (error)
    893 		return error;
    894 
    895 	hp->hp_wbuf = usbd_get_buffer(hp->hp_wxfer);
    896 
    897 	return 0;
    898 }
    899 
    900 Static int
    901 uhso_mux_clean(struct uhso_port *hp)
    902 {
    903 
    904 	DPRINTF(1, "hp=%p\n", hp);
    905 
    906 	CLR(hp->hp_flags, UHSO_PORT_MUXREADY);
    907 	CLR(hp->hp_status, TIOCM_DTR | TIOCM_DSR | TIOCM_CAR);
    908 	return 0;
    909 }
    910 
    911 Static int
    912 uhso_mux_write(struct uhso_port *hp)
    913 {
    914 	struct uhso_softc *sc = hp->hp_sc;
    915 	usb_device_request_t req;
    916 	usbd_status status;
    917 
    918 	DPRINTF(5, "hp=%p, index=%d, wlen=%zd\n", hp, hp->hp_index, hp->hp_wlen);
    919 
    920 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
    921 	req.bRequest = UCDC_SEND_ENCAPSULATED_COMMAND;
    922 	USETW(req.wValue, 0);
    923 	USETW(req.wIndex, hp->hp_index);
    924 	USETW(req.wLength, hp->hp_wlen);
    925 
    926 	usbd_setup_default_xfer(hp->hp_wxfer, sc->sc_udev, hp, USBD_NO_TIMEOUT,
    927 	    &req, hp->hp_wbuf, hp->hp_wlen, 0, hp->hp_write_cb);
    928 
    929 	status = usbd_transfer(hp->hp_wxfer);
    930 	if (status != USBD_IN_PROGRESS) {
    931 		DPRINTF(0, "non-normal status %s\n", usbd_errstr(status));
    932 		return EIO;
    933 	}
    934 
    935 	sc->sc_refcnt++;
    936 	return 0;
    937 }
    938 
    939 Static int
    940 uhso_mux_read(struct uhso_port *hp)
    941 {
    942 	struct uhso_softc *sc = hp->hp_sc;
    943 	usb_device_request_t req;
    944 	usbd_status status;
    945 
    946 	CLR(hp->hp_flags, UHSO_PORT_MUXBUSY);
    947 
    948 	if (hp->hp_rlen == 0 && !ISSET(hp->hp_flags, UHSO_PORT_MUXREADY))
    949 		return 0;
    950 
    951 	SET(hp->hp_flags, UHSO_PORT_MUXBUSY);
    952 	CLR(hp->hp_flags, UHSO_PORT_MUXREADY);
    953 
    954 	DPRINTF(5, "hp=%p, index=%d\n", hp, hp->hp_index);
    955 
    956 	req.bmRequestType = UT_READ_CLASS_INTERFACE;
    957 	req.bRequest = UCDC_GET_ENCAPSULATED_RESPONSE;
    958 	USETW(req.wValue, 0);
    959 	USETW(req.wIndex, hp->hp_index);
    960 	USETW(req.wLength, hp->hp_rsize);
    961 
    962 	usbd_setup_default_xfer(hp->hp_rxfer, sc->sc_udev, hp, USBD_NO_TIMEOUT,
    963 	    &req, hp->hp_rbuf, hp->hp_rsize, USBD_SHORT_XFER_OK,
    964 	    hp->hp_read_cb);
    965 
    966 	status = usbd_transfer(hp->hp_rxfer);
    967 	if (status != USBD_IN_PROGRESS) {
    968 		DPRINTF(0, "non-normal status %s\n", usbd_errstr(status));
    969 		CLR(hp->hp_flags, UHSO_PORT_MUXBUSY);
    970 		return EIO;
    971 	}
    972 
    973 	sc->sc_refcnt++;
    974 	return 0;
    975 }
    976 
    977 Static int
    978 uhso_mux_control(struct uhso_port *hp)
    979 {
    980 
    981 	DPRINTF(1, "hp=%p\n", hp);
    982 
    983 	return 0;
    984 }
    985 
    986 Static void
    987 uhso_mux_intr(struct usbd_xfer *xfer, void * p, usbd_status status)
    988 {
    989 	struct uhso_softc *sc = p;
    990 	struct uhso_port *hp;
    991 	uint32_t cc;
    992 	uint8_t *buf;
    993 	unsigned int i;
    994 
    995 	if (status != USBD_NORMAL_COMPLETION) {
    996 		DPRINTF(0, "non-normal status %s\n", usbd_errstr(status));
    997 		return;
    998 	}
    999 
   1000 	usbd_get_xfer_status(xfer, NULL, (void **)&buf, &cc, NULL);
   1001 	if (cc == 0)
   1002 		return;
   1003 
   1004 	DPRINTF(5, "mux mask 0x%02x, cc=%u\n", buf[0], cc);
   1005 
   1006 	for (i = 0; i < __arraycount(uhso_mux_port); i++) {
   1007 		if (!ISSET(buf[0], __BIT(i)))
   1008 			continue;
   1009 
   1010 		DPRINTF(5, "mux %d port %d\n", i, uhso_mux_port[i]);
   1011 		hp = sc->sc_port[uhso_mux_port[i]];
   1012 		if (hp == NULL
   1013 		    || hp->hp_tp == NULL
   1014 		    || !ISSET(hp->hp_status, TIOCM_DTR))
   1015 			continue;
   1016 
   1017 		SET(hp->hp_flags, UHSO_PORT_MUXREADY);
   1018 		if (ISSET(hp->hp_flags, UHSO_PORT_MUXBUSY))
   1019 			continue;
   1020 
   1021 		uhso_mux_read(hp);
   1022 	}
   1023 }
   1024 
   1025 
   1026 /******************************************************************************
   1027  *
   1028  *	Bulk ports operate using the bulk endpoints on an interface, though
   1029  *   the Modem port (at least) may have an interrupt endpoint that will pass
   1030  *   CDC Notification messages with the modem status.
   1031  */
   1032 
   1033 Static void
   1034 uhso_bulk_attach(struct uhso_softc *sc, struct usbd_interface * ifh, int index)
   1035 {
   1036 	usb_endpoint_descriptor_t *ed;
   1037 	usb_interface_descriptor_t *id;
   1038 	struct uhso_port *hp;
   1039 	int in, out;
   1040 
   1041 	ed = uhso_get_endpoint(ifh, UE_BULK, UE_DIR_IN);
   1042 	if (ed == NULL) {
   1043 		aprint_error_dev(sc->sc_dev, "bulk-in endpoint not found\n");
   1044 		return;
   1045 	}
   1046 	in = ed->bEndpointAddress;
   1047 
   1048 	ed = uhso_get_endpoint(ifh, UE_BULK, UE_DIR_OUT);
   1049 	if (ed == NULL) {
   1050 		aprint_error_dev(sc->sc_dev, "bulk-out endpoint not found\n");
   1051 		return;
   1052 	}
   1053 	out = ed->bEndpointAddress;
   1054 
   1055 	id = usbd_get_interface_descriptor(ifh);
   1056 	if (id == NULL) {
   1057 		aprint_error_dev(sc->sc_dev, "interface descriptor not found\n");
   1058 		return;
   1059 	}
   1060 
   1061 	DPRINTF(1, "bulk endpoints in=%x, out=%x\n", in, out);
   1062 
   1063 	if (sc->sc_port[index] != NULL) {
   1064 		aprint_error_dev(sc->sc_dev, "bulk port %d is duplicate!\n",
   1065 		    index);
   1066 
   1067 		return;
   1068 	}
   1069 
   1070 	hp = kmem_zalloc(sizeof(struct uhso_port), KM_SLEEP);
   1071 	sc->sc_port[index] = hp;
   1072 
   1073 	hp->hp_sc = sc;
   1074 	hp->hp_ifh = ifh;
   1075 	hp->hp_index = id->bInterfaceNumber;
   1076 	hp->hp_raddr = in;
   1077 	hp->hp_waddr = out;
   1078 	hp->hp_abort = uhso_bulk_abort;
   1079 	hp->hp_detach = uhso_bulk_detach;
   1080 	hp->hp_init = uhso_bulk_init;
   1081 	hp->hp_clean = uhso_bulk_clean;
   1082 	hp->hp_write = uhso_bulk_write;
   1083 	hp->hp_write_cb = uhso_tty_write_cb;
   1084 	hp->hp_read = uhso_bulk_read;
   1085 	hp->hp_read_cb = uhso_tty_read_cb;
   1086 	hp->hp_control = uhso_bulk_control;
   1087 	hp->hp_wsize = UHSO_BULK_WSIZE;
   1088 	hp->hp_rsize = UHSO_BULK_RSIZE;
   1089 
   1090 	if (index == UHSO_PORT_MODEM) {
   1091 		ed = uhso_get_endpoint(ifh, UE_INTERRUPT, UE_DIR_IN);
   1092 		if (ed != NULL) {
   1093 			hp->hp_iaddr = ed->bEndpointAddress;
   1094 			hp->hp_isize = UGETW(ed->wMaxPacketSize);
   1095 		}
   1096 	}
   1097 
   1098 	uhso_tty_attach(hp);
   1099 
   1100 	aprint_normal_dev(sc->sc_dev,
   1101 	    "%s (port %d) attached as bulk tty\n",
   1102 	    uhso_port_name[index], index);
   1103 }
   1104 
   1105 Static int
   1106 uhso_bulk_abort(struct uhso_port *hp)
   1107 {
   1108 
   1109 	DPRINTF(1, "hp=%p\n", hp);
   1110 
   1111 	return (*hp->hp_clean)(hp);
   1112 }
   1113 
   1114 Static int
   1115 uhso_bulk_detach(struct uhso_port *hp)
   1116 {
   1117 
   1118 	DPRINTF(1, "hp=%p\n", hp);
   1119 
   1120 	uhso_tty_detach(hp);
   1121 	kmem_free(hp, sizeof(struct uhso_port));
   1122 	return 0;
   1123 }
   1124 
   1125 Static int
   1126 uhso_bulk_init(struct uhso_port *hp)
   1127 {
   1128 	usbd_status status;
   1129 
   1130 	DPRINTF(1, "hp=%p\n", hp);
   1131 
   1132 	if (hp->hp_isize > 0) {
   1133 		hp->hp_ibuf = kmem_alloc(hp->hp_isize, KM_SLEEP);
   1134 
   1135 		status = usbd_open_pipe_intr(hp->hp_ifh, hp->hp_iaddr,
   1136 		    USBD_SHORT_XFER_OK, &hp->hp_ipipe, hp, hp->hp_ibuf,
   1137 		    hp->hp_isize, uhso_bulk_intr, USBD_DEFAULT_INTERVAL);
   1138 
   1139 		if (status != USBD_NORMAL_COMPLETION) {
   1140 			DPRINTF(0, "interrupt pipe open failed: %s\n",
   1141 			    usbd_errstr(status));
   1142 
   1143 			return EIO;
   1144 		}
   1145 	}
   1146 
   1147 	status = usbd_open_pipe(hp->hp_ifh, hp->hp_raddr, 0, &hp->hp_rpipe);
   1148 	if (status != USBD_NORMAL_COMPLETION) {
   1149 		DPRINTF(0, "read pipe open failed: %s\n", usbd_errstr(status));
   1150 		return EIO;
   1151 	}
   1152 
   1153 	status = usbd_open_pipe(hp->hp_ifh, hp->hp_waddr, 0, &hp->hp_wpipe);
   1154 	if (status != USBD_NORMAL_COMPLETION) {
   1155 		DPRINTF(0, "write pipe open failed: %s\n", usbd_errstr(status));
   1156 		return EIO;
   1157 	}
   1158 
   1159 	int error = usbd_create_xfer(hp->hp_rpipe, hp->hp_rsize,
   1160 	    USBD_SHORT_XFER_OK, 0, &hp->hp_rxfer);
   1161 	if (error)
   1162 		return error;
   1163 
   1164 	hp->hp_rbuf = usbd_get_buffer(hp->hp_rxfer);
   1165 
   1166 	error = usbd_create_xfer(hp->hp_wpipe, hp->hp_wsize, 0, 0,
   1167 	    &hp->hp_wxfer);
   1168 	if (error)
   1169 		return error;
   1170 	hp->hp_wbuf = usbd_get_buffer(hp->hp_wxfer);
   1171 
   1172 	return 0;
   1173 }
   1174 
   1175 Static int
   1176 uhso_bulk_clean(struct uhso_port *hp)
   1177 {
   1178 
   1179 	DPRINTF(1, "hp=%p\n", hp);
   1180 
   1181 	if (hp->hp_ipipe != NULL) {
   1182 		usbd_abort_pipe(hp->hp_ipipe);
   1183 		usbd_close_pipe(hp->hp_ipipe);
   1184 		hp->hp_ipipe = NULL;
   1185 	}
   1186 
   1187 	if (hp->hp_ibuf != NULL) {
   1188 		kmem_free(hp->hp_ibuf, hp->hp_isize);
   1189 		hp->hp_ibuf = NULL;
   1190 	}
   1191 
   1192 	if (hp->hp_rpipe != NULL) {
   1193 		usbd_abort_pipe(hp->hp_rpipe);
   1194 		usbd_close_pipe(hp->hp_rpipe);
   1195 		hp->hp_rpipe = NULL;
   1196 	}
   1197 
   1198 	if (hp->hp_wpipe != NULL) {
   1199 		usbd_abort_pipe(hp->hp_wpipe);
   1200 		usbd_close_pipe(hp->hp_wpipe);
   1201 		hp->hp_wpipe = NULL;
   1202 	}
   1203 
   1204 	return 0;
   1205 }
   1206 
   1207 Static int
   1208 uhso_bulk_write(struct uhso_port *hp)
   1209 {
   1210 	struct uhso_softc *sc = hp->hp_sc;
   1211 	usbd_status status;
   1212 
   1213 	DPRINTF(5, "hp=%p, wlen=%zd\n", hp, hp->hp_wlen);
   1214 
   1215 	usbd_setup_xfer(hp->hp_wxfer, hp, hp->hp_wbuf, hp->hp_wlen, 0,
   1216 	     USBD_NO_TIMEOUT, hp->hp_write_cb);
   1217 
   1218 	status = usbd_transfer(hp->hp_wxfer);
   1219 	if (status != USBD_IN_PROGRESS) {
   1220 		DPRINTF(0, "non-normal status %s\n", usbd_errstr(status));
   1221 		return EIO;
   1222 	}
   1223 
   1224 	sc->sc_refcnt++;
   1225 	return 0;
   1226 }
   1227 
   1228 Static int
   1229 uhso_bulk_read(struct uhso_port *hp)
   1230 {
   1231 	struct uhso_softc *sc = hp->hp_sc;
   1232 	usbd_status status;
   1233 
   1234 	DPRINTF(5, "hp=%p\n", hp);
   1235 
   1236 	usbd_setup_xfer(hp->hp_rxfer, hp, hp->hp_rbuf, hp->hp_rsize,
   1237 	    USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, hp->hp_read_cb);
   1238 
   1239 	status = usbd_transfer(hp->hp_rxfer);
   1240 	if (status != USBD_IN_PROGRESS) {
   1241 		DPRINTF(0, "non-normal status %s\n", usbd_errstr(status));
   1242 		return EIO;
   1243 	}
   1244 
   1245 	sc->sc_refcnt++;
   1246 	return 0;
   1247 }
   1248 
   1249 Static int
   1250 uhso_bulk_control(struct uhso_port *hp)
   1251 {
   1252 	struct uhso_softc *sc = hp->hp_sc;
   1253 	usb_device_request_t req;
   1254 	usbd_status status;
   1255 	int val;
   1256 
   1257 	DPRINTF(1, "hp=%p\n", hp);
   1258 
   1259 	if (hp->hp_isize == 0)
   1260 		return 0;
   1261 
   1262 	val = 0;
   1263 	if (ISSET(hp->hp_status, TIOCM_DTR))
   1264 		SET(val, UCDC_LINE_DTR);
   1265 	if (ISSET(hp->hp_status, TIOCM_RTS))
   1266 		SET(val, UCDC_LINE_RTS);
   1267 
   1268 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
   1269 	req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
   1270 	USETW(req.wValue, val);
   1271 	USETW(req.wIndex, hp->hp_index);
   1272 	USETW(req.wLength, 0);
   1273 
   1274 	sc->sc_refcnt++;
   1275 
   1276 	status = usbd_do_request(sc->sc_udev, &req, NULL);
   1277 
   1278 	if (--sc->sc_refcnt < 0)
   1279 		usb_detach_wakeupold(sc->sc_dev);
   1280 
   1281 	if (status != USBD_NORMAL_COMPLETION) {
   1282 		DPRINTF(0, "non-normal status %s\n", usbd_errstr(status));
   1283 		return EIO;
   1284 	}
   1285 
   1286 	return 0;
   1287 }
   1288 
   1289 Static void
   1290 uhso_bulk_intr(struct usbd_xfer *xfer, void * p, usbd_status status)
   1291 {
   1292 	struct uhso_port *hp = p;
   1293 	struct tty *tp = hp->hp_tp;
   1294 	usb_cdc_notification_t *msg;
   1295 	uint32_t cc;
   1296 	int s, old;
   1297 
   1298 	if (status != USBD_NORMAL_COMPLETION) {
   1299 		DPRINTF(0, "non-normal status %s\n", usbd_errstr(status));
   1300 		return;
   1301 	}
   1302 
   1303 	usbd_get_xfer_status(xfer, NULL, (void **)&msg, &cc, NULL);
   1304 
   1305 	if (cc < UCDC_NOTIFICATION_LENGTH
   1306 	    || msg->bmRequestType != UCDC_NOTIFICATION
   1307 	    || msg->bNotification != UCDC_N_SERIAL_STATE
   1308 	    || UGETW(msg->wValue) != 0
   1309 	    || UGETW(msg->wIndex) != hp->hp_index
   1310 	    || UGETW(msg->wLength) < 1)
   1311 		return;
   1312 
   1313 	DPRINTF(5, "state=%02x\n", msg->data[0]);
   1314 
   1315 	old = hp->hp_status;
   1316 	CLR(hp->hp_status, TIOCM_RNG | TIOCM_DSR | TIOCM_CAR);
   1317 	if (ISSET(msg->data[0], UCDC_N_SERIAL_RI))
   1318 		SET(hp->hp_status, TIOCM_RNG);
   1319 	if (ISSET(msg->data[0], UCDC_N_SERIAL_DSR))
   1320 		SET(hp->hp_status, TIOCM_DSR);
   1321 	if (ISSET(msg->data[0], UCDC_N_SERIAL_DCD))
   1322 		SET(hp->hp_status, TIOCM_CAR);
   1323 
   1324 	if (ISSET(hp->hp_status ^ old, TIOCM_CAR)) {
   1325 		s = spltty();
   1326 		tp->t_linesw->l_modem(tp, ISSET(hp->hp_status, TIOCM_CAR));
   1327 		splx(s);
   1328 	}
   1329 
   1330 	if (ISSET((hp->hp_status ^ old), TIOCM_RNG | TIOCM_DSR | TIOCM_CAR))
   1331 		DPRINTF(1, "RNG %s, DSR %s, DCD %s\n",
   1332 		    (ISSET(hp->hp_status, TIOCM_RNG) ? "on" : "off"),
   1333 		    (ISSET(hp->hp_status, TIOCM_DSR) ? "on" : "off"),
   1334 		    (ISSET(hp->hp_status, TIOCM_CAR) ? "on" : "off"));
   1335 }
   1336 
   1337 
   1338 /******************************************************************************
   1339  *
   1340  *	TTY management
   1341  *
   1342  */
   1343 
   1344 Static void
   1345 uhso_tty_attach(struct uhso_port *hp)
   1346 {
   1347 	struct tty *tp;
   1348 
   1349 	tp = tty_alloc();
   1350 	tp->t_oproc = uhso_tty_start;
   1351 	tp->t_param = uhso_tty_param;
   1352 
   1353 	hp->hp_tp = tp;
   1354 	tty_attach(tp);
   1355 
   1356 	DPRINTF(1, "hp=%p, tp=%p\n", hp, tp);
   1357 }
   1358 
   1359 Static void
   1360 uhso_tty_detach(struct uhso_port *hp)
   1361 {
   1362 
   1363 	DPRINTF(1, "hp=%p\n", hp);
   1364 
   1365 	uhso_tty_clean(hp);
   1366 
   1367 	tty_detach(hp->hp_tp);
   1368 	tty_free(hp->hp_tp);
   1369 	hp->hp_tp = NULL;
   1370 }
   1371 
   1372 Static void
   1373 uhso_tty_write_cb(struct usbd_xfer *xfer, void * p, usbd_status status)
   1374 {
   1375 	struct uhso_port *hp = p;
   1376 	struct uhso_softc *sc = hp->hp_sc;
   1377 	struct tty *tp = hp->hp_tp;
   1378 	uint32_t cc;
   1379 	int s;
   1380 
   1381 	if (--sc->sc_refcnt < 0)
   1382 		usb_detach_wakeupold(sc->sc_dev);
   1383 
   1384 	if (status != USBD_NORMAL_COMPLETION) {
   1385 		DPRINTF(0, "non-normal status %s\n", usbd_errstr(status));
   1386 
   1387 		if (status == USBD_STALLED && hp->hp_wpipe != NULL)
   1388 			usbd_clear_endpoint_stall_async(hp->hp_wpipe);
   1389 		else
   1390 			return;
   1391 	} else {
   1392 		usbd_get_xfer_status(xfer, NULL, NULL, &cc, NULL);
   1393 
   1394 		DPRINTF(5, "wrote %d bytes (of %zd)\n", cc, hp->hp_wlen);
   1395 		if (cc != hp->hp_wlen)
   1396 			DPRINTF(0, "cc=%u, wlen=%zd\n", cc, hp->hp_wlen);
   1397 	}
   1398 
   1399 	s = spltty();
   1400 	CLR(tp->t_state, TS_BUSY);
   1401 	tp->t_linesw->l_start(tp);
   1402 	splx(s);
   1403 }
   1404 
   1405 Static void
   1406 uhso_tty_read_cb(struct usbd_xfer *xfer, void * p, usbd_status status)
   1407 {
   1408 	struct uhso_port *hp = p;
   1409 	struct uhso_softc *sc = hp->hp_sc;
   1410 	struct tty *tp = hp->hp_tp;
   1411 	uint8_t *cp;
   1412 	uint32_t cc;
   1413 	int s;
   1414 
   1415 	if (--sc->sc_refcnt < 0)
   1416 		usb_detach_wakeupold(sc->sc_dev);
   1417 
   1418 	if (status != USBD_NORMAL_COMPLETION) {
   1419 		DPRINTF(0, "non-normal status: %s\n", usbd_errstr(status));
   1420 
   1421 		if (status == USBD_STALLED && hp->hp_rpipe != NULL)
   1422 			usbd_clear_endpoint_stall_async(hp->hp_rpipe);
   1423 		else
   1424 			return;
   1425 
   1426 		hp->hp_rlen = 0;
   1427 	} else {
   1428 		usbd_get_xfer_status(xfer, NULL, (void **)&cp, &cc, NULL);
   1429 
   1430 		hp->hp_rlen = cc;
   1431 		DPRINTF(5, "read %d bytes\n", cc);
   1432 
   1433 		s = spltty();
   1434 		while (cc > 0) {
   1435 			if (tp->t_linesw->l_rint(*cp++, tp) == -1) {
   1436 				DPRINTF(0, "lost %d bytes\n", cc);
   1437 				break;
   1438 			}
   1439 
   1440 			cc--;
   1441 		}
   1442 		splx(s);
   1443 	}
   1444 
   1445 	(*hp->hp_read)(hp);
   1446 }
   1447 
   1448 
   1449 /******************************************************************************
   1450  *
   1451  *	TTY subsystem
   1452  *
   1453  */
   1454 
   1455 int
   1456 uhso_tty_open(dev_t dev, int flag, int mode, struct lwp *l)
   1457 {
   1458 	struct uhso_softc *sc;
   1459 	struct uhso_port *hp;
   1460 	struct tty *tp;
   1461 	int error, s;
   1462 
   1463 	DPRINTF(1, "unit %d port %d\n", UHSOUNIT(dev), UHSOPORT(dev));
   1464 
   1465 	sc = device_lookup_private(&uhso_cd, UHSOUNIT(dev));
   1466 	if (sc == NULL
   1467 	    || !device_is_active(sc->sc_dev)
   1468 	    || UHSOPORT(dev) >= UHSO_PORT_MAX)
   1469 		return ENXIO;
   1470 
   1471 	hp = sc->sc_port[UHSOPORT(dev)];
   1472 	if (hp == NULL || hp->hp_tp == NULL)
   1473 		return ENXIO;
   1474 
   1475 	tp = hp->hp_tp;
   1476 	if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp))
   1477 		return EBUSY;
   1478 
   1479 	error = 0;
   1480 	s = spltty();
   1481 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
   1482 		tp->t_dev = dev;
   1483 		error = uhso_tty_init(hp);
   1484 	}
   1485 	splx(s);
   1486 
   1487 	if (error == 0) {
   1488 		error = ttyopen(tp, UHSODIALOUT(dev), ISSET(flag, O_NONBLOCK));
   1489 		if (error == 0) {
   1490 			error = tp->t_linesw->l_open(dev, tp);
   1491 		}
   1492 	}
   1493 
   1494 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0)
   1495 		uhso_tty_clean(hp);
   1496 
   1497 	DPRINTF(1, "sc=%p, hp=%p, tp=%p, error=%d\n", sc, hp, tp, error);
   1498 
   1499 	return error;
   1500 }
   1501 
   1502 Static int
   1503 uhso_tty_init(struct uhso_port *hp)
   1504 {
   1505 	struct tty *tp = hp->hp_tp;
   1506 	struct termios t;
   1507 	int error;
   1508 
   1509 	DPRINTF(1, "sc=%p, hp=%p, tp=%p\n", sc, hp, tp);
   1510 
   1511 	/*
   1512 	 * Initialize the termios status to the defaults.  Add in the
   1513 	 * sticky bits from TIOCSFLAGS.
   1514 	 */
   1515 	t.c_ispeed = 0;
   1516 	t.c_ospeed = TTYDEF_SPEED;
   1517 	t.c_cflag = TTYDEF_CFLAG;
   1518 	if (ISSET(hp->hp_swflags, TIOCFLAG_CLOCAL))
   1519 		SET(t.c_cflag, CLOCAL);
   1520 	if (ISSET(hp->hp_swflags, TIOCFLAG_CRTSCTS))
   1521 		SET(t.c_cflag, CRTSCTS);
   1522 	if (ISSET(hp->hp_swflags, TIOCFLAG_MDMBUF))
   1523 		SET(t.c_cflag, MDMBUF);
   1524 
   1525 	/* Ensure uhso_tty_param() will do something. */
   1526 	tp->t_ospeed = 0;
   1527 	(void)uhso_tty_param(tp, &t);
   1528 
   1529 	tp->t_iflag = TTYDEF_IFLAG;
   1530 	tp->t_oflag = TTYDEF_OFLAG;
   1531 	tp->t_lflag = TTYDEF_LFLAG;
   1532 	ttychars(tp);
   1533 	ttsetwater(tp);
   1534 
   1535 	hp->hp_status = 0;
   1536 	error = (*hp->hp_init)(hp);
   1537 	if (error != 0)
   1538 		return error;
   1539 
   1540 	/*
   1541 	 * Turn on DTR.  We must always do this, even if carrier is not
   1542 	 * present, because otherwise we'd have to use TIOCSDTR
   1543 	 * immediately after setting CLOCAL, which applications do not
   1544 	 * expect.  We always assert DTR while the port is open
   1545 	 * unless explicitly requested to deassert it.  Ditto RTS.
   1546 	 */
   1547 	uhso_tty_control(hp, TIOCMBIS, TIOCM_DTR | TIOCM_RTS);
   1548 
   1549 	/* and start reading */
   1550 	error = (*hp->hp_read)(hp);
   1551 	if (error != 0)
   1552 		return error;
   1553 
   1554 	return 0;
   1555 }
   1556 
   1557 int
   1558 uhso_tty_close(dev_t dev, int flag, int mode, struct lwp *l)
   1559 {
   1560 	struct uhso_softc *sc = device_lookup_private(&uhso_cd, UHSOUNIT(dev));
   1561 	struct uhso_port *hp = sc->sc_port[UHSOPORT(dev)];
   1562 	struct tty *tp = hp->hp_tp;
   1563 
   1564 	if (!ISSET(tp->t_state, TS_ISOPEN))
   1565 		return 0;
   1566 
   1567 	DPRINTF(1, "sc=%p, hp=%p, tp=%p\n", sc, hp, tp);
   1568 
   1569 	sc->sc_refcnt++;
   1570 
   1571 	tp->t_linesw->l_close(tp, flag);
   1572 	ttyclose(tp);
   1573 
   1574 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0)
   1575 		uhso_tty_clean(hp);
   1576 
   1577 	if (--sc->sc_refcnt < 0)
   1578 		usb_detach_wakeupold(sc->sc_dev);
   1579 
   1580 	return 0;
   1581 }
   1582 
   1583 Static void
   1584 uhso_tty_clean(struct uhso_port *hp)
   1585 {
   1586 
   1587 	DPRINTF(1, "hp=%p\n", hp);
   1588 
   1589 	if (ISSET(hp->hp_status, TIOCM_DTR)
   1590 	    && ISSET(hp->hp_tp->t_cflag, HUPCL))
   1591 		uhso_tty_control(hp, TIOCMBIC, TIOCM_DTR);
   1592 
   1593 	(*hp->hp_clean)(hp);
   1594 
   1595 	if (hp->hp_rxfer != NULL) {
   1596 		usbd_destroy_xfer(hp->hp_rxfer);
   1597 		hp->hp_rxfer = NULL;
   1598 		hp->hp_rbuf = NULL;
   1599 	}
   1600 
   1601 	if (hp->hp_wxfer != NULL) {
   1602 		usbd_destroy_xfer(hp->hp_wxfer);
   1603 		hp->hp_wxfer = NULL;
   1604 		hp->hp_wbuf = NULL;
   1605 	}
   1606 }
   1607 
   1608 int
   1609 uhso_tty_read(dev_t dev, struct uio *uio, int flag)
   1610 {
   1611 	struct uhso_softc *sc = device_lookup_private(&uhso_cd, UHSOUNIT(dev));
   1612 	struct uhso_port *hp = sc->sc_port[UHSOPORT(dev)];
   1613 	struct tty *tp = hp->hp_tp;
   1614 	int error;
   1615 
   1616 	if (!device_is_active(sc->sc_dev))
   1617 		return EIO;
   1618 
   1619 	DPRINTF(5, "sc=%p, hp=%p, tp=%p\n", sc, hp, tp);
   1620 
   1621 	sc->sc_refcnt++;
   1622 
   1623 	error = tp->t_linesw->l_read(tp, uio, flag);
   1624 
   1625 	if (--sc->sc_refcnt < 0)
   1626 		usb_detach_wakeupold(sc->sc_dev);
   1627 
   1628 	return error;
   1629 }
   1630 
   1631 int
   1632 uhso_tty_write(dev_t dev, struct uio *uio, int flag)
   1633 {
   1634 	struct uhso_softc *sc = device_lookup_private(&uhso_cd, UHSOUNIT(dev));
   1635 	struct uhso_port *hp = sc->sc_port[UHSOPORT(dev)];
   1636 	struct tty *tp = hp->hp_tp;
   1637 	int error;
   1638 
   1639 	if (!device_is_active(sc->sc_dev))
   1640 		return EIO;
   1641 
   1642 	DPRINTF(5, "sc=%p, hp=%p, tp=%p\n", sc, hp, tp);
   1643 
   1644 	sc->sc_refcnt++;
   1645 
   1646 	error = tp->t_linesw->l_write(tp, uio, flag);
   1647 
   1648 	if (--sc->sc_refcnt < 0)
   1649 		usb_detach_wakeupold(sc->sc_dev);
   1650 
   1651 	return error;
   1652 }
   1653 
   1654 int
   1655 uhso_tty_ioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
   1656 {
   1657 	struct uhso_softc *sc = device_lookup_private(&uhso_cd, UHSOUNIT(dev));
   1658 	struct uhso_port *hp = sc->sc_port[UHSOPORT(dev)];
   1659 	int error;
   1660 
   1661 	if (!device_is_active(sc->sc_dev))
   1662 		return EIO;
   1663 
   1664 	DPRINTF(1, "sc=%p, hp=%p\n", sc, hp);
   1665 
   1666 	sc->sc_refcnt++;
   1667 
   1668 	error = uhso_tty_do_ioctl(hp, cmd, data, flag, l);
   1669 
   1670 	if (--sc->sc_refcnt < 0)
   1671 		usb_detach_wakeupold(sc->sc_dev);
   1672 
   1673 	return error;
   1674 }
   1675 
   1676 Static int
   1677 uhso_tty_do_ioctl(struct uhso_port *hp, u_long cmd, void *data, int flag,
   1678     struct lwp *l)
   1679 {
   1680 	struct tty *tp = hp->hp_tp;
   1681 	int error, s;
   1682 
   1683 	error = tp->t_linesw->l_ioctl(tp, cmd, data, flag, l);
   1684 	if (error != EPASSTHROUGH)
   1685 		return error;
   1686 
   1687 	error = ttioctl(tp, cmd, data, flag, l);
   1688 	if (error != EPASSTHROUGH)
   1689 		return error;
   1690 
   1691 	error = 0;
   1692 
   1693 	s = spltty();
   1694 
   1695 	switch (cmd) {
   1696 	case TIOCSDTR:
   1697 		error = uhso_tty_control(hp, TIOCMBIS, TIOCM_DTR);
   1698 		break;
   1699 
   1700 	case TIOCCDTR:
   1701 		error = uhso_tty_control(hp, TIOCMBIC, TIOCM_DTR);
   1702 		break;
   1703 
   1704 	case TIOCGFLAGS:
   1705 		*(int *)data = hp->hp_swflags;
   1706 		break;
   1707 
   1708 	case TIOCSFLAGS:
   1709 		error = kauth_authorize_device_tty(l->l_cred,
   1710 		    KAUTH_DEVICE_TTY_PRIVSET, tp);
   1711 
   1712 		if (error)
   1713 			break;
   1714 
   1715 		hp->hp_swflags = *(int *)data;
   1716 		break;
   1717 
   1718 	case TIOCMSET:
   1719 	case TIOCMBIS:
   1720 	case TIOCMBIC:
   1721 		error = uhso_tty_control(hp, cmd, *(int *)data);
   1722 		break;
   1723 
   1724 	case TIOCMGET:
   1725 		*(int *)data = hp->hp_status;
   1726 		break;
   1727 
   1728 	default:
   1729 		error = EPASSTHROUGH;
   1730 		break;
   1731 	}
   1732 
   1733 	splx(s);
   1734 
   1735 	return error;
   1736 }
   1737 
   1738 /* this is called with tty_lock held */
   1739 void
   1740 uhso_tty_stop(struct tty *tp, int flag)
   1741 {
   1742 #if 0
   1743 	struct uhso_softc *sc = device_lookup_private(&uhso_cd, UHSOUNIT(tp->t_dev));
   1744 	struct uhso_port *hp = sc->sc_port[UHSOPORT(tp->t_dev)];
   1745 #endif
   1746 }
   1747 
   1748 struct tty *
   1749 uhso_tty_tty(dev_t dev)
   1750 {
   1751 	struct uhso_softc *sc = device_lookup_private(&uhso_cd, UHSOUNIT(dev));
   1752 	struct uhso_port *hp = sc->sc_port[UHSOPORT(dev)];
   1753 
   1754 	return hp->hp_tp;
   1755 }
   1756 
   1757 int
   1758 uhso_tty_poll(dev_t dev, int events, struct lwp *l)
   1759 {
   1760 	struct uhso_softc *sc = device_lookup_private(&uhso_cd, UHSOUNIT(dev));
   1761 	struct uhso_port *hp = sc->sc_port[UHSOPORT(dev)];
   1762 	struct tty *tp = hp->hp_tp;
   1763         int revents;
   1764 
   1765 	if (!device_is_active(sc->sc_dev))
   1766                 return POLLHUP;
   1767 
   1768 	sc->sc_refcnt++;
   1769 
   1770         revents = tp->t_linesw->l_poll(tp, events, l);
   1771 
   1772 	if (--sc->sc_refcnt < 0)
   1773 		usb_detach_wakeupold(sc->sc_dev);
   1774 
   1775         return revents;
   1776 }
   1777 
   1778 Static int
   1779 uhso_tty_param(struct tty *tp, struct termios *t)
   1780 {
   1781 	struct uhso_softc *sc = device_lookup_private(&uhso_cd, UHSOUNIT(tp->t_dev));
   1782 	struct uhso_port *hp = sc->sc_port[UHSOPORT(tp->t_dev)];
   1783 
   1784 	if (!device_is_active(sc->sc_dev))
   1785 		return EIO;
   1786 
   1787 	DPRINTF(1, "hp=%p, tp=%p, termios iflag=%x, oflag=%x, cflag=%x\n",
   1788 	    hp, tp, t->c_iflag, t->c_oflag, t->c_cflag);
   1789 
   1790 	/* Check requested parameters. */
   1791 	if (t->c_ispeed != 0
   1792 	    && t->c_ispeed != t->c_ospeed)
   1793 		return EINVAL;
   1794 
   1795 	/* force CLOCAL and !HUPCL for console */
   1796 	if (ISSET(hp->hp_swflags, TIOCFLAG_SOFTCAR)) {
   1797 		SET(t->c_cflag, CLOCAL);
   1798 		CLR(t->c_cflag, HUPCL);
   1799 	}
   1800 
   1801 	/* If there were no changes, don't do anything.  */
   1802 	if (tp->t_ospeed == t->c_ospeed
   1803 	    && tp->t_cflag == t->c_cflag)
   1804 		return 0;
   1805 
   1806 	tp->t_ispeed = 0;
   1807 	tp->t_ospeed = t->c_ospeed;
   1808 	tp->t_cflag = t->c_cflag;
   1809 
   1810 	/* update tty layers idea of carrier bit */
   1811 	tp->t_linesw->l_modem(tp, ISSET(hp->hp_status, TIOCM_CAR));
   1812 	return 0;
   1813 }
   1814 
   1815 /* this is called with tty_lock held */
   1816 Static void
   1817 uhso_tty_start(struct tty *tp)
   1818 {
   1819 	struct uhso_softc *sc = device_lookup_private(&uhso_cd, UHSOUNIT(tp->t_dev));
   1820 	struct uhso_port *hp = sc->sc_port[UHSOPORT(tp->t_dev)];
   1821 	int s;
   1822 
   1823 	if (!device_is_active(sc->sc_dev))
   1824 		return;
   1825 
   1826 	s = spltty();
   1827 
   1828 	if (!ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP)
   1829 	    && ttypull(tp) != 0) {
   1830 		hp->hp_wlen = q_to_b(&tp->t_outq, hp->hp_wbuf, hp->hp_wsize);
   1831 		if (hp->hp_wlen > 0) {
   1832 			SET(tp->t_state, TS_BUSY);
   1833 			(*hp->hp_write)(hp);
   1834 		}
   1835 	}
   1836 
   1837 	splx(s);
   1838 }
   1839 
   1840 Static int
   1841 uhso_tty_control(struct uhso_port *hp, u_long cmd, int bits)
   1842 {
   1843 
   1844 	bits &= (TIOCM_DTR | TIOCM_RTS);
   1845 	DPRINTF(1, "cmd %s, DTR=%d, RTS=%d\n",
   1846 	    (cmd == TIOCMBIC ? "BIC" : (cmd == TIOCMBIS ? "BIS" : "SET")),
   1847 	    (bits & TIOCM_DTR) ? 1 : 0,
   1848 	    (bits & TIOCM_RTS) ? 1 : 0);
   1849 
   1850 	switch (cmd) {
   1851 	case TIOCMBIC:
   1852 		CLR(hp->hp_status, bits);
   1853 		break;
   1854 
   1855 	case TIOCMBIS:
   1856 		SET(hp->hp_status, bits);
   1857 		break;
   1858 
   1859 	case TIOCMSET:
   1860 		CLR(hp->hp_status, TIOCM_DTR | TIOCM_RTS);
   1861 		SET(hp->hp_status, bits);
   1862 		break;
   1863 	}
   1864 
   1865 	return (*hp->hp_control)(hp);
   1866 }
   1867 
   1868 
   1869 /******************************************************************************
   1870  *
   1871  *	Network Interface
   1872  *
   1873  */
   1874 
   1875 Static void
   1876 uhso_ifnet_attach(struct uhso_softc *sc, struct usbd_interface * ifh, int index)
   1877 {
   1878 	usb_endpoint_descriptor_t *ed;
   1879 	struct uhso_port *hp;
   1880 	struct ifnet *ifp;
   1881 	int in, out;
   1882 
   1883 	ed = uhso_get_endpoint(ifh, UE_BULK, UE_DIR_IN);
   1884 	if (ed == NULL) {
   1885 		aprint_error_dev(sc->sc_dev,
   1886 		    "could not find bulk-in endpoint\n");
   1887 
   1888 		return;
   1889 	}
   1890 	in = ed->bEndpointAddress;
   1891 
   1892 	ed = uhso_get_endpoint(ifh, UE_BULK, UE_DIR_OUT);
   1893 	if (ed == NULL) {
   1894 		aprint_error_dev(sc->sc_dev,
   1895 		    "could not find bulk-out endpoint\n");
   1896 
   1897 		return;
   1898 	}
   1899 	out = ed->bEndpointAddress;
   1900 
   1901 	DPRINTF(1, "in=%d, out=%d\n", in, out);
   1902 
   1903 	if (sc->sc_port[index] != NULL) {
   1904 		aprint_error_dev(sc->sc_dev,
   1905 		    "ifnet port %d is duplicate!\n", index);
   1906 
   1907 		return;
   1908 	}
   1909 
   1910 	hp = kmem_zalloc(sizeof(struct uhso_port), KM_SLEEP);
   1911 	sc->sc_port[index] = hp;
   1912 
   1913 	ifp = if_alloc(IFT_IP);
   1914 	strlcpy(ifp->if_xname, device_xname(sc->sc_dev), IFNAMSIZ);
   1915 	ifp->if_softc = hp;
   1916 	ifp->if_mtu = UHSO_IFNET_MTU;
   1917 	ifp->if_dlt = DLT_RAW;
   1918 	ifp->if_type = IFT_IP;
   1919 	ifp->if_flags = IFF_NOARP | IFF_SIMPLEX;
   1920 	ifp->if_ioctl = uhso_ifnet_ioctl;
   1921 	ifp->if_start = uhso_ifnet_start;
   1922 	ifp->if_output = uhso_ifnet_output;
   1923 	IFQ_SET_READY(&ifp->if_snd);
   1924 
   1925 	hp->hp_sc = sc;
   1926 	hp->hp_ifp = ifp;
   1927 	hp->hp_ifh = ifh;
   1928 	hp->hp_raddr = in;
   1929 	hp->hp_waddr = out;
   1930 	hp->hp_abort = uhso_ifnet_abort;
   1931 	hp->hp_detach = uhso_ifnet_detach;
   1932 	hp->hp_init = uhso_bulk_init;
   1933 	hp->hp_clean = uhso_bulk_clean;
   1934 	hp->hp_write = uhso_bulk_write;
   1935 	hp->hp_write_cb = uhso_ifnet_write_cb;
   1936 	hp->hp_read = uhso_bulk_read;
   1937 	hp->hp_read_cb = uhso_ifnet_read_cb;
   1938 	hp->hp_wsize = MCLBYTES;
   1939 	hp->hp_rsize = MCLBYTES;
   1940 
   1941 	if_attach(ifp);
   1942 	if_alloc_sadl(ifp);
   1943 	bpf_attach(ifp, DLT_RAW, 0);
   1944 
   1945 	aprint_normal_dev(sc->sc_dev, "%s (port %d) attached as ifnet\n",
   1946 	    uhso_port_name[index], index);
   1947 }
   1948 
   1949 Static int
   1950 uhso_ifnet_abort(struct uhso_port *hp)
   1951 {
   1952 	struct ifnet *ifp = hp->hp_ifp;
   1953 
   1954 	/* All ifnet IO will abort when IFF_RUNNING is not set */
   1955 	CLR(ifp->if_flags, IFF_RUNNING);
   1956 
   1957 	return (*hp->hp_clean)(hp);
   1958 }
   1959 
   1960 Static int
   1961 uhso_ifnet_detach(struct uhso_port *hp)
   1962 {
   1963 	struct ifnet *ifp = hp->hp_ifp;
   1964 	int s;
   1965 
   1966 	s = splnet();
   1967 	bpf_detach(ifp);
   1968 	if_detach(ifp);
   1969 	splx(s);
   1970 
   1971 	kmem_free(hp, sizeof(struct uhso_port));
   1972 	return 0;
   1973 }
   1974 
   1975 Static void
   1976 uhso_ifnet_write_cb(struct usbd_xfer *xfer, void * p, usbd_status status)
   1977 {
   1978 	struct uhso_port *hp = p;
   1979 	struct uhso_softc *sc= hp->hp_sc;
   1980 	struct ifnet *ifp = hp->hp_ifp;
   1981 	uint32_t cc;
   1982 	int s;
   1983 
   1984 	if (--sc->sc_refcnt < 0)
   1985 		usb_detach_wakeupold(sc->sc_dev);
   1986 
   1987 	if (!ISSET(ifp->if_flags, IFF_RUNNING))
   1988 		return;
   1989 
   1990 	if (status != USBD_NORMAL_COMPLETION) {
   1991 		DPRINTF(0, "non-normal status %s\n", usbd_errstr(status));
   1992 
   1993 		if (status == USBD_STALLED && hp->hp_wpipe != NULL)
   1994 			usbd_clear_endpoint_stall_async(hp->hp_wpipe);
   1995 		else
   1996 			return;
   1997 
   1998 		ifp->if_oerrors++;
   1999 	} else {
   2000 		usbd_get_xfer_status(xfer, NULL, NULL, &cc, NULL);
   2001 		DPRINTF(5, "wrote %d bytes (of %zd)\n", cc, hp->hp_wlen);
   2002 
   2003 		if (cc != hp->hp_wlen)
   2004 			DPRINTF(0, "cc=%u, wlen=%zd\n", cc, hp->hp_wlen);
   2005 
   2006 		ifp->if_opackets++;
   2007 	}
   2008 
   2009 	s = splnet();
   2010 	CLR(ifp->if_flags, IFF_OACTIVE);
   2011 	ifp->if_start(ifp);
   2012 	splx(s);
   2013 }
   2014 
   2015 Static void
   2016 uhso_ifnet_read_cb(struct usbd_xfer *xfer, void * p,
   2017     usbd_status status)
   2018 {
   2019 	struct uhso_port *hp = p;
   2020 	struct uhso_softc *sc= hp->hp_sc;
   2021 	struct ifnet *ifp = hp->hp_ifp;
   2022 	void *cp;
   2023 	uint32_t cc;
   2024 
   2025 	if (--sc->sc_refcnt < 0)
   2026 		usb_detach_wakeupold(sc->sc_dev);
   2027 
   2028 	if (!ISSET(ifp->if_flags, IFF_RUNNING))
   2029 		return;
   2030 
   2031 	if (status != USBD_NORMAL_COMPLETION) {
   2032 		DPRINTF(0, "non-normal status: %s\n", usbd_errstr(status));
   2033 
   2034 		if (status == USBD_STALLED && hp->hp_rpipe != NULL)
   2035 			usbd_clear_endpoint_stall_async(hp->hp_rpipe);
   2036 		else
   2037 			return;
   2038 
   2039 		ifp->if_ierrors++;
   2040 		hp->hp_rlen = 0;
   2041 	} else {
   2042 		usbd_get_xfer_status(xfer, NULL, (void **)&cp, &cc, NULL);
   2043 
   2044 		hp->hp_rlen = cc;
   2045 		DPRINTF(5, "read %d bytes\n", cc);
   2046 
   2047 		uhso_ifnet_input(ifp, &hp->hp_mbuf, cp, cc);
   2048 	}
   2049 
   2050 	(*hp->hp_read)(hp);
   2051 }
   2052 
   2053 Static void
   2054 uhso_ifnet_input(struct ifnet *ifp, struct mbuf **mb, uint8_t *cp, size_t cc)
   2055 {
   2056 	struct mbuf *m;
   2057 	size_t got, len, want;
   2058 	int s;
   2059 
   2060 	/*
   2061 	 * Several IP packets might be in the same buffer, we need to
   2062 	 * separate them before handing it to the ip-stack.  We might
   2063 	 * also receive partial packets which we need to defer until
   2064 	 * we get more data.
   2065 	 */
   2066 	while (cc > 0) {
   2067 		if (*mb == NULL) {
   2068 			MGETHDR(m, M_DONTWAIT, MT_DATA);
   2069 			if (m == NULL) {
   2070 				aprint_error_ifnet(ifp, "no mbufs\n");
   2071 				ifp->if_ierrors++;
   2072 				break;
   2073 			}
   2074 
   2075 			MCLGET(m, M_DONTWAIT);
   2076 			if (!ISSET(m->m_flags, M_EXT)) {
   2077 				aprint_error_ifnet(ifp, "no mbuf clusters\n");
   2078 				ifp->if_ierrors++;
   2079 				m_freem(m);
   2080 				break;
   2081 			}
   2082 
   2083 			got = 0;
   2084 		} else {
   2085 			m = *mb;
   2086 			*mb = NULL;
   2087 			got = m->m_pkthdr.len;
   2088 		}
   2089 
   2090 		/* make sure that the incoming packet is ok */
   2091 		if (got == 0)
   2092 			mtod(m, uint8_t *)[0] = cp[0];
   2093 
   2094 		want = mtod(m, struct ip *)->ip_hl << 2;
   2095 		if (mtod(m, struct ip *)->ip_v != 4
   2096 		    || want != sizeof(struct ip)) {
   2097 			aprint_error_ifnet(ifp, "bad IP header (v=%d, hl=%zd)\n",
   2098 			    mtod(m, struct ip *)->ip_v, want);
   2099 
   2100 			ifp->if_ierrors++;
   2101 			m_freem(m);
   2102 			break;
   2103 		}
   2104 
   2105 		/* ensure we have the IP header.. */
   2106 		if (got < want) {
   2107 			len = MIN(want - got, cc);
   2108 			memcpy(mtod(m, uint8_t *) + got, cp, len);
   2109 			got += len;
   2110 			cc -= len;
   2111 			cp += len;
   2112 
   2113 			if (got < want) {
   2114 				DPRINTF(5, "waiting for IP header "
   2115 					   "(got %zd want %zd)\n", got, want);
   2116 
   2117 				m->m_pkthdr.len = got;
   2118 				*mb = m;
   2119 				break;
   2120 			}
   2121 		}
   2122 
   2123 		/* ..and the packet body */
   2124 		want = ntohs(mtod(m, struct ip *)->ip_len);
   2125 		if (got < want) {
   2126 			len = MIN(want - got, cc);
   2127 			memcpy(mtod(m, uint8_t *) + got, cp, len);
   2128 			got += len;
   2129 			cc -= len;
   2130 			cp += len;
   2131 
   2132 			if (got < want) {
   2133 				DPRINTF(5, "waiting for IP packet "
   2134 					   "(got %zd want %zd)\n", got, want);
   2135 
   2136 				m->m_pkthdr.len = got;
   2137 				*mb = m;
   2138 				break;
   2139 			}
   2140 		} else if (want > got) {
   2141 			aprint_error_ifnet(ifp, "bad IP packet (len=%zd)\n",
   2142 			    want);
   2143 
   2144 			ifp->if_ierrors++;
   2145 			m_freem(m);
   2146 			break;
   2147 		}
   2148 
   2149 		m->m_pkthdr.rcvif = ifp;
   2150 		m->m_pkthdr.len = m->m_len = got;
   2151 
   2152 		s = splnet();
   2153 
   2154 		bpf_mtap(ifp, m);
   2155 
   2156 		if (__predict_false(!pktq_enqueue(ip_pktq, m, 0))) {
   2157 			m_freem(m);
   2158 		} else {
   2159 			ifp->if_ipackets++;
   2160 			ifp->if_ibytes += got;
   2161 		}
   2162 		splx(s);
   2163 	}
   2164 }
   2165 
   2166 Static int
   2167 uhso_ifnet_ioctl(struct ifnet *ifp, u_long cmd, void *data)
   2168 {
   2169 	struct uhso_port *hp = ifp->if_softc;
   2170 	int error, s;
   2171 
   2172 	s = splnet();
   2173 
   2174 	switch (cmd) {
   2175 	case SIOCINITIFADDR:
   2176 		switch (((struct ifaddr *)data)->ifa_addr->sa_family) {
   2177 #ifdef INET
   2178 		case AF_INET:
   2179 			if (!ISSET(ifp->if_flags, IFF_RUNNING)) {
   2180 				SET(ifp->if_flags, IFF_UP);
   2181 				error = uhso_ifnet_init(hp);
   2182 				if (error != 0) {
   2183 					uhso_ifnet_clean(hp);
   2184 					break;
   2185 				}
   2186 
   2187 				SET(ifp->if_flags, IFF_RUNNING);
   2188 				DPRINTF(1, "hp=%p, ifp=%p INITIFADDR\n", hp, ifp);
   2189 				break;
   2190 			}
   2191 
   2192 			error = 0;
   2193 			break;
   2194 #endif
   2195 
   2196 		default:
   2197 			error = EAFNOSUPPORT;
   2198 			break;
   2199 		}
   2200 		break;
   2201 
   2202 	case SIOCSIFMTU:
   2203 		if (((struct ifreq *)data)->ifr_mtu > hp->hp_wsize) {
   2204 			error = EINVAL;
   2205 			break;
   2206 		}
   2207 
   2208 		error = ifioctl_common(ifp, cmd, data);
   2209 		if (error == ENETRESET)
   2210 			error = 0;
   2211 
   2212 		break;
   2213 
   2214 	case SIOCSIFFLAGS:
   2215 		error = ifioctl_common(ifp, cmd, data);
   2216 		if (error != 0)
   2217 			break;
   2218 
   2219 		switch (ifp->if_flags & (IFF_UP | IFF_RUNNING)) {
   2220 		case IFF_UP:
   2221 			error = uhso_ifnet_init(hp);
   2222 			if (error != 0) {
   2223 				uhso_ifnet_clean(hp);
   2224 				break;
   2225 			}
   2226 
   2227 			SET(ifp->if_flags, IFF_RUNNING);
   2228 			DPRINTF(1, "hp=%p, ifp=%p RUNNING\n", hp, ifp);
   2229 			break;
   2230 
   2231 		case IFF_RUNNING:
   2232 			uhso_ifnet_clean(hp);
   2233 			CLR(ifp->if_flags, IFF_RUNNING);
   2234 			DPRINTF(1, "hp=%p, ifp=%p STOPPED\n", hp, ifp);
   2235 			break;
   2236 
   2237 		default:
   2238 			break;
   2239 		}
   2240 		break;
   2241 
   2242 	default:
   2243 		error = ifioctl_common(ifp, cmd, data);
   2244 		break;
   2245 	}
   2246 
   2247 	splx(s);
   2248 
   2249 	return error;
   2250 }
   2251 
   2252 /* is only called if IFF_RUNNING not set */
   2253 Static int
   2254 uhso_ifnet_init(struct uhso_port *hp)
   2255 {
   2256 	struct uhso_softc *sc = hp->hp_sc;
   2257 	int error;
   2258 
   2259 	DPRINTF(1, "sc=%p, hp=%p\n", sc, hp);
   2260 
   2261 	if (!device_is_active(sc->sc_dev))
   2262 		return EIO;
   2263 
   2264 	error = (*hp->hp_init)(hp);
   2265 	if (error != 0)
   2266 		return error;
   2267 
   2268 	error = (*hp->hp_read)(hp);
   2269 	if (error != 0)
   2270 		return error;
   2271 
   2272 	return 0;
   2273 }
   2274 
   2275 Static void
   2276 uhso_ifnet_clean(struct uhso_port *hp)
   2277 {
   2278 
   2279 	DPRINTF(1, "hp=%p\n", hp);
   2280 
   2281 	(*hp->hp_clean)(hp);
   2282 
   2283 	if (hp->hp_rxfer != NULL) {
   2284 		usbd_destroy_xfer(hp->hp_rxfer);
   2285 		hp->hp_rxfer = NULL;
   2286 		hp->hp_rbuf = NULL;
   2287 	}
   2288 
   2289 	if (hp->hp_wxfer != NULL) {
   2290 		usbd_destroy_xfer(hp->hp_wxfer);
   2291 		hp->hp_wxfer = NULL;
   2292 		hp->hp_wbuf = NULL;
   2293 	}
   2294 }
   2295 
   2296 /* called at splnet() with IFF_OACTIVE not set */
   2297 Static void
   2298 uhso_ifnet_start(struct ifnet *ifp)
   2299 {
   2300 	struct uhso_port *hp = ifp->if_softc;
   2301 	struct mbuf *m;
   2302 
   2303 	KASSERT(!ISSET(ifp->if_flags, IFF_OACTIVE));
   2304 
   2305 	if (!ISSET(ifp->if_flags, IFF_RUNNING))
   2306 		return;
   2307 
   2308 	if (IFQ_IS_EMPTY(&ifp->if_snd)) {
   2309 		DPRINTF(5, "finished sending\n");
   2310 		return;
   2311 	}
   2312 
   2313 	SET(ifp->if_flags, IFF_OACTIVE);
   2314 	IFQ_DEQUEUE(&ifp->if_snd, m);
   2315 	hp->hp_wlen = m->m_pkthdr.len;
   2316 	if (hp->hp_wlen > hp->hp_wsize) {
   2317 		aprint_error_ifnet(ifp,
   2318 		    "packet too long (%zd > %zd), truncating\n",
   2319 		    hp->hp_wlen, hp->hp_wsize);
   2320 
   2321 		hp->hp_wlen = hp->hp_wsize;
   2322 	}
   2323 
   2324 	bpf_mtap(ifp, m);
   2325 
   2326 	m_copydata(m, 0, hp->hp_wlen, hp->hp_wbuf);
   2327 	m_freem(m);
   2328 
   2329 	if ((*hp->hp_write)(hp) != 0) {
   2330 		ifp->if_oerrors++;
   2331 		CLR(ifp->if_flags, IFF_OACTIVE);
   2332 	}
   2333 }
   2334 
   2335 Static int
   2336 uhso_ifnet_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
   2337     struct rtentry *rt0)
   2338 {
   2339 	ALTQ_DECL(struct altq_pktattr pktattr);
   2340 	int error;
   2341 
   2342 	if (!ISSET(ifp->if_flags, IFF_RUNNING))
   2343 		return EIO;
   2344 
   2345 	IFQ_CLASSIFY(&ifp->if_snd, m, dst->sa_family, &pktattr);
   2346 
   2347 	switch (dst->sa_family) {
   2348 #ifdef INET
   2349 	case AF_INET:
   2350 		error = ifq_enqueue(ifp, m ALTQ_COMMA ALTQ_DECL(&pktattr));
   2351 		break;
   2352 #endif
   2353 
   2354 	default:
   2355 		DPRINTF(0, "unsupported address family %d\n", dst->sa_family);
   2356 		error = EAFNOSUPPORT;
   2357 		m_freem(m);
   2358 		break;
   2359 	}
   2360 
   2361 	return error;
   2362 }
   2363