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