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