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