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