uhso.c revision 257176
1202181Sthompsa/*-
2210275Sthompsa * Copyright (c) 2010 Fredrik Lindberg <fli@shapeshifter.se>
3202181Sthompsa * All rights reserved.
4202181Sthompsa *
5202181Sthompsa * Redistribution and use in source and binary forms, with or without
6202181Sthompsa * modification, are permitted provided that the following conditions
7202181Sthompsa * are met:
8202181Sthompsa * 1. Redistributions of source code must retain the above copyright
9202181Sthompsa *    notice, this list of conditions and the following disclaimer.
10202181Sthompsa * 2. Redistributions in binary form must reproduce the above copyright
11202181Sthompsa *    notice, this list of conditions and the following disclaimer in the
12202181Sthompsa *    documentation and/or other materials provided with the distribution.
13202181Sthompsa *
14202181Sthompsa * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15202181Sthompsa * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16202181Sthompsa * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17202181Sthompsa * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18202181Sthompsa * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19202181Sthompsa * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20202181Sthompsa * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21202181Sthompsa * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22202181Sthompsa * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23202181Sthompsa * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24202181Sthompsa *
25202181Sthompsa */
26202181Sthompsa#include <sys/cdefs.h>
27202181Sthompsa__FBSDID("$FreeBSD: head/sys/dev/usb/net/uhso.c 257176 2013-10-26 17:58:36Z glebius $");
28202181Sthompsa
29202181Sthompsa#include <sys/param.h>
30202181Sthompsa#include <sys/types.h>
31202181Sthompsa#include <sys/sockio.h>
32202181Sthompsa#include <sys/mbuf.h>
33202181Sthompsa#include <sys/malloc.h>
34202181Sthompsa#include <sys/kernel.h>
35202181Sthompsa#include <sys/module.h>
36202181Sthompsa#include <sys/socket.h>
37202181Sthompsa#include <sys/tty.h>
38202181Sthompsa#include <sys/sysctl.h>
39202181Sthompsa#include <sys/condvar.h>
40202181Sthompsa#include <sys/sx.h>
41202181Sthompsa#include <sys/proc.h>
42202181Sthompsa#include <sys/conf.h>
43202181Sthompsa#include <sys/bus.h>
44202181Sthompsa#include <sys/systm.h>
45210275Sthompsa#include <sys/limits.h>
46202181Sthompsa
47202181Sthompsa#include <machine/bus.h>
48202181Sthompsa
49202181Sthompsa#include <net/if.h>
50257176Sglebius#include <net/if_var.h>
51202181Sthompsa#include <net/if_types.h>
52202181Sthompsa#include <net/netisr.h>
53202181Sthompsa#include <net/bpf.h>
54202181Sthompsa#include <netinet/in.h>
55202181Sthompsa#include <netinet/ip.h>
56202181Sthompsa#include <netinet/ip6.h>
57202181Sthompsa
58202181Sthompsa#include <dev/usb/usb.h>
59202181Sthompsa#include <dev/usb/usbdi.h>
60202181Sthompsa#include <dev/usb/usbdi_util.h>
61202181Sthompsa#include <dev/usb/usb_cdc.h>
62202181Sthompsa#include "usbdevs.h"
63202181Sthompsa#define USB_DEBUG_VAR uhso_debug
64202181Sthompsa#include <dev/usb/usb_debug.h>
65202181Sthompsa#include <dev/usb/usb_process.h>
66202181Sthompsa#include <dev/usb/usb_busdma.h>
67202181Sthompsa#include <dev/usb/usb_msctest.h>
68202181Sthompsa
69212136Sthompsa#include <dev/usb/serial/usb_serial.h>
70212136Sthompsa
71202181Sthompsastruct uhso_tty {
72202181Sthompsa	struct uhso_softc *ht_sc;
73202181Sthompsa	struct usb_xfer	*ht_xfer[3];
74202243Sthompsa	int		ht_muxport; /* Mux. port no */
75202181Sthompsa	int		ht_open;
76202181Sthompsa	char		ht_name[32];
77202181Sthompsa};
78202181Sthompsa
79202181Sthompsastruct uhso_softc {
80202181Sthompsa	device_t		sc_dev;
81202181Sthompsa	struct usb_device	*sc_udev;
82202181Sthompsa	struct mtx		sc_mtx;
83202243Sthompsa	uint32_t		sc_type;	/* Interface definition */
84210275Sthompsa	int			sc_radio;
85202181Sthompsa
86202181Sthompsa	struct usb_xfer		*sc_xfer[3];
87202181Sthompsa	uint8_t			sc_iface_no;
88202181Sthompsa	uint8_t			sc_iface_index;
89202181Sthompsa
90202181Sthompsa	/* Control pipe */
91202181Sthompsa	struct usb_xfer	*	sc_ctrl_xfer[2];
92202181Sthompsa	uint8_t			sc_ctrl_iface_no;
93202181Sthompsa
94202181Sthompsa	/* Network */
95202181Sthompsa	struct usb_xfer		*sc_if_xfer[2];
96202181Sthompsa	struct ifnet		*sc_ifp;
97202243Sthompsa	struct mbuf		*sc_mwait;	/* Partial packet */
98202243Sthompsa	size_t			sc_waitlen;	/* No. of outstanding bytes */
99202181Sthompsa	struct ifqueue		sc_rxq;
100202181Sthompsa	struct callout		sc_c;
101202181Sthompsa
102202181Sthompsa	/* TTY related structures */
103202181Sthompsa	struct ucom_super_softc sc_super_ucom;
104202243Sthompsa	int			sc_ttys;
105202181Sthompsa	struct uhso_tty		*sc_tty;
106202181Sthompsa	struct ucom_softc	*sc_ucom;
107202181Sthompsa	int			sc_msr;
108202181Sthompsa	int			sc_lsr;
109202181Sthompsa	int			sc_line;
110202181Sthompsa};
111202181Sthompsa
112202181Sthompsa#define UHSO_MAX_MTU		2048
113202181Sthompsa
114202181Sthompsa/*
115202181Sthompsa * There are mainly two type of cards floating around.
116202181Sthompsa * The first one has 2,3 or 4 interfaces with a multiplexed serial port
117202181Sthompsa * and packet interface on the first interface and bulk serial ports
118202181Sthompsa * on the others.
119202181Sthompsa * The second type of card has several other interfaces, their purpose
120202181Sthompsa * can be detected during run-time.
121202181Sthompsa */
122202181Sthompsa#define UHSO_IFACE_SPEC(usb_type, port, port_type) \
123202181Sthompsa	(((usb_type) << 24) | ((port) << 16) | (port_type))
124202181Sthompsa
125202181Sthompsa#define UHSO_IFACE_USB_TYPE(x) ((x >> 24) & 0xff)
126202181Sthompsa#define UHSO_IFACE_PORT(x) ((x >> 16) & 0xff)
127202181Sthompsa#define UHSO_IFACE_PORT_TYPE(x) (x & 0xff)
128202181Sthompsa
129202181Sthompsa/*
130202181Sthompsa * USB interface types
131202181Sthompsa */
132202181Sthompsa#define UHSO_IF_NET		0x01	/* Network packet interface */
133202181Sthompsa#define UHSO_IF_MUX		0x02	/* Multiplexed serial port */
134202181Sthompsa#define UHSO_IF_BULK		0x04	/* Bulk interface */
135202181Sthompsa
136202181Sthompsa/*
137202181Sthompsa * Port types
138202181Sthompsa */
139202181Sthompsa#define UHSO_PORT_UNKNOWN	0x00
140202243Sthompsa#define UHSO_PORT_SERIAL	0x01	/* Serial port */
141202181Sthompsa#define UHSO_PORT_NETWORK	0x02	/* Network packet interface */
142202181Sthompsa
143202181Sthompsa/*
144202181Sthompsa * Multiplexed serial port destination sub-port names
145202181Sthompsa */
146202181Sthompsa#define UHSO_MPORT_TYPE_CTL	0x00	/* Control port */
147202181Sthompsa#define UHSO_MPORT_TYPE_APP	0x01	/* Application */
148202181Sthompsa#define UHSO_MPORT_TYPE_PCSC	0x02
149202181Sthompsa#define UHSO_MPORT_TYPE_GPS	0x03
150202243Sthompsa#define UHSO_MPORT_TYPE_APP2	0x04	/* Secondary application */
151202181Sthompsa#define UHSO_MPORT_TYPE_MAX	UHSO_MPORT_TYPE_APP2
152202181Sthompsa#define UHSO_MPORT_TYPE_NOMAX	8	/* Max number of mux ports */
153202181Sthompsa
154202181Sthompsa/*
155202181Sthompsa * Port definitions
156202270Sthompsa * Note that these definitions are arbitrary and do not match the values
157202270Sthompsa * returned by the auto config descriptor.
158202181Sthompsa */
159210275Sthompsa#define UHSO_PORT_TYPE_UNKNOWN	0x00
160202181Sthompsa#define UHSO_PORT_TYPE_CTL	0x01
161202181Sthompsa#define UHSO_PORT_TYPE_APP	0x02
162202181Sthompsa#define UHSO_PORT_TYPE_APP2	0x03
163202181Sthompsa#define UHSO_PORT_TYPE_MODEM	0x04
164202181Sthompsa#define UHSO_PORT_TYPE_NETWORK	0x05
165202181Sthompsa#define UHSO_PORT_TYPE_DIAG	0x06
166202181Sthompsa#define UHSO_PORT_TYPE_DIAG2	0x07
167202181Sthompsa#define UHSO_PORT_TYPE_GPS	0x08
168202181Sthompsa#define UHSO_PORT_TYPE_GPSCTL	0x09
169202181Sthompsa#define UHSO_PORT_TYPE_PCSC	0x0a
170202181Sthompsa#define UHSO_PORT_TYPE_MSD	0x0b
171202181Sthompsa#define UHSO_PORT_TYPE_VOICE	0x0c
172202181Sthompsa#define UHSO_PORT_TYPE_MAX	0x0c
173202181Sthompsa
174202181Sthompsastatic eventhandler_tag uhso_etag;
175202181Sthompsa
176202181Sthompsa/* Overall port type */
177202181Sthompsastatic char *uhso_port[] = {
178202181Sthompsa	"Unknown",
179202181Sthompsa	"Serial",
180202181Sthompsa	"Network",
181202181Sthompsa	"Network/Serial"
182202181Sthompsa};
183202181Sthompsa
184202243Sthompsa/*
185202243Sthompsa * Map between interface port type read from device and description type.
186202243Sthompsa * The position in this array is a direct map to the auto config
187202243Sthompsa * descriptor values.
188202243Sthompsa */
189202243Sthompsastatic unsigned char uhso_port_map[] = {
190210275Sthompsa	UHSO_PORT_TYPE_UNKNOWN,
191202181Sthompsa	UHSO_PORT_TYPE_DIAG,
192202181Sthompsa	UHSO_PORT_TYPE_GPS,
193202181Sthompsa	UHSO_PORT_TYPE_GPSCTL,
194202181Sthompsa	UHSO_PORT_TYPE_APP,
195202181Sthompsa	UHSO_PORT_TYPE_APP2,
196202181Sthompsa	UHSO_PORT_TYPE_CTL,
197202181Sthompsa	UHSO_PORT_TYPE_NETWORK,
198202181Sthompsa	UHSO_PORT_TYPE_MODEM,
199202181Sthompsa	UHSO_PORT_TYPE_MSD,
200202181Sthompsa	UHSO_PORT_TYPE_PCSC,
201202181Sthompsa	UHSO_PORT_TYPE_VOICE
202202181Sthompsa};
203202181Sthompsastatic char uhso_port_map_max = sizeof(uhso_port_map) / sizeof(char);
204202181Sthompsa
205202243Sthompsastatic unsigned char uhso_mux_port_map[] = {
206202181Sthompsa	UHSO_PORT_TYPE_CTL,
207202181Sthompsa	UHSO_PORT_TYPE_APP,
208202181Sthompsa	UHSO_PORT_TYPE_PCSC,
209202181Sthompsa	UHSO_PORT_TYPE_GPS,
210202181Sthompsa	UHSO_PORT_TYPE_APP2
211202181Sthompsa};
212202181Sthompsa
213202181Sthompsastatic char *uhso_port_type[] = {
214202243Sthompsa	"Unknown",  /* Not a valid port */
215202181Sthompsa	"Control",
216202181Sthompsa	"Application",
217202181Sthompsa	"Application (Secondary)",
218202181Sthompsa	"Modem",
219202181Sthompsa	"Network",
220202181Sthompsa	"Diagnostic",
221202181Sthompsa	"Diagnostic (Secondary)",
222202181Sthompsa	"GPS",
223202181Sthompsa	"GPS Control",
224202181Sthompsa	"PC Smartcard",
225202181Sthompsa	"MSD",
226202181Sthompsa	"Voice",
227202181Sthompsa};
228202181Sthompsa
229202181Sthompsastatic char *uhso_port_type_sysctl[] = {
230202181Sthompsa	"unknown",
231202181Sthompsa	"control",
232202181Sthompsa	"application",
233202181Sthompsa	"application",
234202181Sthompsa	"modem",
235202181Sthompsa	"network",
236202181Sthompsa	"diagnostic",
237202181Sthompsa	"diagnostic",
238202181Sthompsa	"gps",
239202181Sthompsa	"gps_control",
240202181Sthompsa	"pcsc",
241202181Sthompsa	"msd",
242202181Sthompsa	"voice",
243202181Sthompsa};
244202181Sthompsa
245202181Sthompsa#define UHSO_STATIC_IFACE	0x01
246202181Sthompsa#define UHSO_AUTO_IFACE		0x02
247202181Sthompsa
248210275Sthompsa/* ifnet device unit allocations */
249210275Sthompsastatic struct unrhdr *uhso_ifnet_unit = NULL;
250210275Sthompsa
251223486Shselaskystatic const STRUCT_USB_HOST_ID uhso_devs[] = {
252202181Sthompsa#define	UHSO_DEV(v,p,i) { USB_VPI(USB_VENDOR_##v, USB_PRODUCT_##v##_##p, i) }
253225037Shselasky	/* Option GlobeTrotter MAX 7.2 with upgraded firmware */
254225037Shselasky	UHSO_DEV(OPTION, GTMAX72, UHSO_STATIC_IFACE),
255202181Sthompsa	/* Option GlobeSurfer iCON 7.2 */
256202181Sthompsa	UHSO_DEV(OPTION, GSICON72, UHSO_STATIC_IFACE),
257202181Sthompsa	/* Option iCON 225 */
258202181Sthompsa	UHSO_DEV(OPTION, GTHSDPA, UHSO_STATIC_IFACE),
259202181Sthompsa	/* Option GlobeSurfer iCON HSUPA */
260202181Sthompsa	UHSO_DEV(OPTION, GSICONHSUPA, UHSO_STATIC_IFACE),
261202181Sthompsa	/* Option GlobeTrotter HSUPA */
262202181Sthompsa	UHSO_DEV(OPTION, GTHSUPA, UHSO_STATIC_IFACE),
263202181Sthompsa	/* GE40x */
264202181Sthompsa	UHSO_DEV(OPTION, GE40X, UHSO_AUTO_IFACE),
265202181Sthompsa	UHSO_DEV(OPTION, GE40X_1, UHSO_AUTO_IFACE),
266202181Sthompsa	UHSO_DEV(OPTION, GE40X_2, UHSO_AUTO_IFACE),
267202181Sthompsa	UHSO_DEV(OPTION, GE40X_3, UHSO_AUTO_IFACE),
268202181Sthompsa	/* Option GlobeSurfer iCON 401 */
269202181Sthompsa	UHSO_DEV(OPTION, ICON401, UHSO_AUTO_IFACE),
270202181Sthompsa	/* Option GlobeTrotter Module 382 */
271202181Sthompsa	UHSO_DEV(OPTION, GMT382, UHSO_AUTO_IFACE),
272202181Sthompsa	/* Option iCON EDGE */
273202181Sthompsa	UHSO_DEV(OPTION, ICONEDGE, UHSO_STATIC_IFACE),
274202181Sthompsa	/* Option Module HSxPA */
275202181Sthompsa	UHSO_DEV(OPTION, MODHSXPA, UHSO_STATIC_IFACE),
276202181Sthompsa	/* Option iCON 321 */
277202181Sthompsa	UHSO_DEV(OPTION, ICON321, UHSO_STATIC_IFACE),
278202181Sthompsa	/* Option iCON 322 */
279202243Sthompsa	UHSO_DEV(OPTION, GTICON322, UHSO_STATIC_IFACE),
280202243Sthompsa	/* Option iCON 505 */
281202243Sthompsa	UHSO_DEV(OPTION, ICON505, UHSO_AUTO_IFACE),
282210275Sthompsa	/* Option iCON 452 */
283210275Sthompsa	UHSO_DEV(OPTION, ICON505, UHSO_AUTO_IFACE),
284202181Sthompsa#undef UHSO_DEV
285202181Sthompsa};
286202181Sthompsa
287227309Sedstatic SYSCTL_NODE(_hw_usb, OID_AUTO, uhso, CTLFLAG_RW, 0, "USB uhso");
288202243Sthompsastatic int uhso_autoswitch = 1;
289202243SthompsaSYSCTL_INT(_hw_usb_uhso, OID_AUTO, auto_switch, CTLFLAG_RW,
290202243Sthompsa    &uhso_autoswitch, 0, "Automatically switch to modem mode");
291202181Sthompsa
292202181Sthompsa#ifdef USB_DEBUG
293202181Sthompsa#ifdef UHSO_DEBUG
294202181Sthompsastatic int uhso_debug = UHSO_DEBUG;
295202181Sthompsa#else
296202181Sthompsastatic int uhso_debug = -1;
297202181Sthompsa#endif
298202181Sthompsa
299202181SthompsaSYSCTL_INT(_hw_usb_uhso, OID_AUTO, debug, CTLFLAG_RW,
300202181Sthompsa    &uhso_debug, 0, "Debug level");
301202181Sthompsa
302202181Sthompsa#define UHSO_DPRINTF(n, x, ...) {\
303202181Sthompsa	if (uhso_debug >= n) {\
304202181Sthompsa		printf("%s: " x, __func__, ##__VA_ARGS__);\
305202181Sthompsa	}\
306202181Sthompsa}
307202181Sthompsa#else
308202181Sthompsa#define UHSO_DPRINTF(n, x, ...)
309202181Sthompsa#endif
310202181Sthompsa
311202181Sthompsa#ifdef UHSO_DEBUG_HEXDUMP
312202181Sthompsa# define UHSO_HEXDUMP(_buf, _len) do { \
313202181Sthompsa  { \
314202181Sthompsa        size_t __tmp; \
315202181Sthompsa        const char *__buf = (const char *)_buf; \
316202181Sthompsa        for (__tmp = 0; __tmp < _len; __tmp++) \
317202181Sthompsa                printf("%02hhx ", *__buf++); \
318202181Sthompsa    printf("\n"); \
319202181Sthompsa  } \
320202181Sthompsa} while(0)
321202181Sthompsa#else
322202181Sthompsa# define UHSO_HEXDUMP(_buf, _len)
323202181Sthompsa#endif
324202181Sthompsa
325202181Sthompsaenum {
326202181Sthompsa	UHSO_MUX_ENDPT_INTR = 0,
327202181Sthompsa	UHSO_MUX_ENDPT_MAX
328202181Sthompsa};
329202181Sthompsa
330202181Sthompsaenum {
331202181Sthompsa	UHSO_CTRL_READ = 0,
332202181Sthompsa	UHSO_CTRL_WRITE,
333202181Sthompsa	UHSO_CTRL_MAX
334202181Sthompsa};
335202181Sthompsa
336202181Sthompsaenum {
337202181Sthompsa	UHSO_IFNET_READ = 0,
338202181Sthompsa	UHSO_IFNET_WRITE,
339202181Sthompsa	UHSO_IFNET_MAX
340202181Sthompsa};
341202181Sthompsa
342202181Sthompsaenum {
343202181Sthompsa	UHSO_BULK_ENDPT_READ = 0,
344202181Sthompsa	UHSO_BULK_ENDPT_WRITE,
345202181Sthompsa	UHSO_BULK_ENDPT_INTR,
346202181Sthompsa	UHSO_BULK_ENDPT_MAX
347202181Sthompsa};
348202181Sthompsa
349202181Sthompsastatic usb_callback_t uhso_mux_intr_callback;
350202181Sthompsastatic usb_callback_t uhso_mux_read_callback;
351202181Sthompsastatic usb_callback_t uhso_mux_write_callback;
352202181Sthompsastatic usb_callback_t uhso_bs_read_callback;
353202181Sthompsastatic usb_callback_t uhso_bs_write_callback;
354202181Sthompsastatic usb_callback_t uhso_bs_intr_callback;
355202181Sthompsastatic usb_callback_t uhso_ifnet_read_callback;
356202181Sthompsastatic usb_callback_t uhso_ifnet_write_callback;
357202181Sthompsa
358202243Sthompsa/* Config used for the default control pipes */
359202181Sthompsastatic const struct usb_config uhso_ctrl_config[UHSO_CTRL_MAX] = {
360202181Sthompsa	[UHSO_CTRL_READ] = {
361202181Sthompsa		.type = UE_CONTROL,
362202181Sthompsa		.endpoint = 0x00,
363202181Sthompsa		.direction = UE_DIR_ANY,
364202181Sthompsa		.flags = { .pipe_bof = 1, .short_xfer_ok = 1 },
365202181Sthompsa		.bufsize = sizeof(struct usb_device_request) + 1024,
366202181Sthompsa		.callback = &uhso_mux_read_callback
367202181Sthompsa	},
368202181Sthompsa
369202181Sthompsa	[UHSO_CTRL_WRITE] = {
370202181Sthompsa		.type = UE_CONTROL,
371202181Sthompsa		.endpoint = 0x00,
372202181Sthompsa		.direction = UE_DIR_ANY,
373202181Sthompsa		.flags = { .pipe_bof = 1, .force_short_xfer = 1 },
374202181Sthompsa		.bufsize = sizeof(struct usb_device_request) + 1024,
375202181Sthompsa		.timeout = 1000,
376202181Sthompsa		.callback = &uhso_mux_write_callback
377202181Sthompsa	}
378202181Sthompsa};
379202181Sthompsa
380202243Sthompsa/* Config for the multiplexed serial ports */
381202181Sthompsastatic const struct usb_config uhso_mux_config[UHSO_MUX_ENDPT_MAX] = {
382202181Sthompsa	[UHSO_MUX_ENDPT_INTR] = {
383202181Sthompsa		.type = UE_INTERRUPT,
384202181Sthompsa		.endpoint = UE_ADDR_ANY,
385202181Sthompsa		.direction = UE_DIR_IN,
386202181Sthompsa		.flags = { .short_xfer_ok = 1 },
387202181Sthompsa		.bufsize = 0,
388202181Sthompsa		.callback = &uhso_mux_intr_callback,
389202181Sthompsa	}
390202181Sthompsa};
391202181Sthompsa
392202243Sthompsa/* Config for the raw IP-packet interface */
393202181Sthompsastatic const struct usb_config uhso_ifnet_config[UHSO_IFNET_MAX] = {
394202181Sthompsa	[UHSO_IFNET_READ] = {
395202181Sthompsa		.type = UE_BULK,
396202181Sthompsa		.endpoint = UE_ADDR_ANY,
397202181Sthompsa		.direction = UE_DIR_IN,
398202181Sthompsa		.flags = { .pipe_bof = 1, .short_xfer_ok = 1 },
399202181Sthompsa		.bufsize = MCLBYTES,
400202181Sthompsa		.callback = &uhso_ifnet_read_callback
401202181Sthompsa	},
402202181Sthompsa	[UHSO_IFNET_WRITE] = {
403202181Sthompsa		.type = UE_BULK,
404202181Sthompsa		.endpoint = UE_ADDR_ANY,
405202181Sthompsa		.direction = UE_DIR_OUT,
406202181Sthompsa		.flags = { .pipe_bof = 1, .force_short_xfer = 1 },
407202181Sthompsa		.bufsize = MCLBYTES,
408202181Sthompsa		.timeout = 5 * USB_MS_HZ,
409202181Sthompsa		.callback = &uhso_ifnet_write_callback
410202181Sthompsa	}
411202181Sthompsa};
412202181Sthompsa
413202243Sthompsa/* Config for interfaces with normal bulk serial ports */
414202181Sthompsastatic const struct usb_config uhso_bs_config[UHSO_BULK_ENDPT_MAX] = {
415202181Sthompsa	[UHSO_BULK_ENDPT_READ] = {
416202181Sthompsa		.type = UE_BULK,
417202181Sthompsa		.endpoint = UE_ADDR_ANY,
418202181Sthompsa		.direction = UE_DIR_IN,
419202181Sthompsa		.flags = { .pipe_bof = 1, .short_xfer_ok = 1 },
420202181Sthompsa		.bufsize = 4096,
421202181Sthompsa		.callback = &uhso_bs_read_callback
422202181Sthompsa	},
423202181Sthompsa
424202181Sthompsa	[UHSO_BULK_ENDPT_WRITE] = {
425202181Sthompsa		.type = UE_BULK,
426202181Sthompsa		.endpoint = UE_ADDR_ANY,
427202181Sthompsa		.direction = UE_DIR_OUT,
428202181Sthompsa		.flags = { .pipe_bof = 1, .force_short_xfer = 1 },
429202181Sthompsa		.bufsize = 8192,
430202181Sthompsa		.callback = &uhso_bs_write_callback
431202181Sthompsa	},
432202181Sthompsa
433202181Sthompsa	[UHSO_BULK_ENDPT_INTR] = {
434202181Sthompsa		.type = UE_INTERRUPT,
435202181Sthompsa		.endpoint = UE_ADDR_ANY,
436202181Sthompsa		.direction = UE_DIR_IN,
437202181Sthompsa		.flags = { .short_xfer_ok = 1 },
438202181Sthompsa		.bufsize = 0,
439202181Sthompsa		.callback = &uhso_bs_intr_callback,
440202181Sthompsa	}
441202181Sthompsa};
442202181Sthompsa
443202243Sthompsastatic int  uhso_probe_iface(struct uhso_softc *, int,
444210275Sthompsa    int (*probe)(struct usb_device *, int));
445210275Sthompsastatic int  uhso_probe_iface_auto(struct usb_device *, int);
446210275Sthompsastatic int  uhso_probe_iface_static(struct usb_device *, int);
447202243Sthompsastatic int  uhso_attach_muxserial(struct uhso_softc *, struct usb_interface *,
448202181Sthompsa    int type);
449202243Sthompsastatic int  uhso_attach_bulkserial(struct uhso_softc *, struct usb_interface *,
450202181Sthompsa    int type);
451202243Sthompsastatic int  uhso_attach_ifnet(struct uhso_softc *, struct usb_interface *,
452202181Sthompsa    int type);
453202181Sthompsastatic void uhso_test_autoinst(void *, struct usb_device *,
454202181Sthompsa		struct usb_attach_arg *);
455202243Sthompsastatic int  uhso_driver_loaded(struct module *, int, void *);
456210275Sthompsastatic int uhso_radio_sysctl(SYSCTL_HANDLER_ARGS);
457210275Sthompsastatic int uhso_radio_ctrl(struct uhso_softc *, int);
458202181Sthompsa
459239180Shselaskystatic void uhso_free(struct ucom_softc *);
460202181Sthompsastatic void uhso_ucom_start_read(struct ucom_softc *);
461202181Sthompsastatic void uhso_ucom_stop_read(struct ucom_softc *);
462202181Sthompsastatic void uhso_ucom_start_write(struct ucom_softc *);
463202181Sthompsastatic void uhso_ucom_stop_write(struct ucom_softc *);
464202181Sthompsastatic void uhso_ucom_cfg_get_status(struct ucom_softc *, uint8_t *, uint8_t *);
465202181Sthompsastatic void uhso_ucom_cfg_set_dtr(struct ucom_softc *, uint8_t);
466202181Sthompsastatic void uhso_ucom_cfg_set_rts(struct ucom_softc *, uint8_t);
467202181Sthompsastatic void uhso_if_init(void *);
468202181Sthompsastatic void uhso_if_start(struct ifnet *);
469202181Sthompsastatic void uhso_if_stop(struct uhso_softc *);
470202243Sthompsastatic int  uhso_if_ioctl(struct ifnet *, u_long, caddr_t);
471249925Sglebiusstatic int  uhso_if_output(struct ifnet *, struct mbuf *,
472249925Sglebius    const struct sockaddr *, struct route *);
473202181Sthompsastatic void uhso_if_rxflush(void *);
474202181Sthompsa
475202181Sthompsastatic device_probe_t uhso_probe;
476202181Sthompsastatic device_attach_t uhso_attach;
477202181Sthompsastatic device_detach_t uhso_detach;
478239299Shselaskystatic void uhso_free_softc(struct uhso_softc *);
479202181Sthompsa
480202181Sthompsastatic device_method_t uhso_methods[] = {
481202181Sthompsa	DEVMETHOD(device_probe,		uhso_probe),
482202181Sthompsa	DEVMETHOD(device_attach,	uhso_attach),
483202181Sthompsa	DEVMETHOD(device_detach,	uhso_detach),
484202181Sthompsa	{ 0, 0 }
485202181Sthompsa};
486202181Sthompsa
487202181Sthompsastatic driver_t uhso_driver = {
488233774Shselasky	.name = "uhso",
489233774Shselasky	.methods = uhso_methods,
490233774Shselasky	.size = sizeof(struct uhso_softc)
491202181Sthompsa};
492202181Sthompsa
493202181Sthompsastatic devclass_t uhso_devclass;
494202181SthompsaDRIVER_MODULE(uhso, uhub, uhso_driver, uhso_devclass, uhso_driver_loaded, 0);
495202181SthompsaMODULE_DEPEND(uhso, ucom, 1, 1, 1);
496202181SthompsaMODULE_DEPEND(uhso, usb, 1, 1, 1);
497202181SthompsaMODULE_VERSION(uhso, 1);
498202181Sthompsa
499202181Sthompsastatic struct ucom_callback uhso_ucom_callback = {
500202181Sthompsa	.ucom_cfg_get_status = &uhso_ucom_cfg_get_status,
501202181Sthompsa	.ucom_cfg_set_dtr = &uhso_ucom_cfg_set_dtr,
502202181Sthompsa	.ucom_cfg_set_rts = &uhso_ucom_cfg_set_rts,
503202181Sthompsa	.ucom_start_read = uhso_ucom_start_read,
504202181Sthompsa	.ucom_stop_read = uhso_ucom_stop_read,
505202181Sthompsa	.ucom_start_write = uhso_ucom_start_write,
506239180Shselasky	.ucom_stop_write = uhso_ucom_stop_write,
507239180Shselasky	.ucom_free = &uhso_free,
508202181Sthompsa};
509202181Sthompsa
510202181Sthompsastatic int
511202181Sthompsauhso_probe(device_t self)
512202181Sthompsa{
513202181Sthompsa	struct usb_attach_arg *uaa = device_get_ivars(self);
514210275Sthompsa	int error;
515202181Sthompsa
516202181Sthompsa	if (uaa->usb_mode != USB_MODE_HOST)
517202181Sthompsa		return (ENXIO);
518202181Sthompsa	if (uaa->info.bConfigIndex != 0)
519202181Sthompsa		return (ENXIO);
520212136Sthompsa	if (uaa->info.bDeviceClass != 0xff)
521202181Sthompsa		return (ENXIO);
522202181Sthompsa
523210275Sthompsa	error = usbd_lookup_id_by_uaa(uhso_devs, sizeof(uhso_devs), uaa);
524210275Sthompsa	if (error != 0)
525210275Sthompsa		return (error);
526210275Sthompsa
527210275Sthompsa	/*
528210275Sthompsa	 * Probe device to see if we are able to attach
529210275Sthompsa	 * to this interface or not.
530210275Sthompsa	 */
531210275Sthompsa	if (USB_GET_DRIVER_INFO(uaa) == UHSO_AUTO_IFACE) {
532210275Sthompsa		if (uhso_probe_iface_auto(uaa->device,
533210275Sthompsa		    uaa->info.bIfaceNum) == 0)
534210275Sthompsa			return (ENXIO);
535210275Sthompsa	}
536210275Sthompsa	return (error);
537202181Sthompsa}
538202181Sthompsa
539202181Sthompsastatic int
540202181Sthompsauhso_attach(device_t self)
541202181Sthompsa{
542202181Sthompsa	struct uhso_softc *sc = device_get_softc(self);
543202181Sthompsa	struct usb_attach_arg *uaa = device_get_ivars(self);
544202181Sthompsa	struct usb_interface_descriptor *id;
545202181Sthompsa	struct sysctl_ctx_list *sctx;
546202181Sthompsa	struct sysctl_oid *soid;
547210275Sthompsa	struct sysctl_oid *tree = NULL, *tty_node;
548202181Sthompsa	struct ucom_softc *ucom;
549202181Sthompsa	struct uhso_tty *ht;
550202181Sthompsa	int i, error, port;
551202181Sthompsa	void *probe_f;
552202181Sthompsa	usb_error_t uerr;
553202181Sthompsa	char *desc;
554202181Sthompsa
555202181Sthompsa	sc->sc_dev = self;
556202181Sthompsa	sc->sc_udev = uaa->device;
557202181Sthompsa	mtx_init(&sc->sc_mtx, "uhso", NULL, MTX_DEF);
558239180Shselasky	ucom_ref(&sc->sc_super_ucom);
559202181Sthompsa
560202181Sthompsa	sc->sc_ucom = NULL;
561202181Sthompsa	sc->sc_ttys = 0;
562210275Sthompsa	sc->sc_radio = 1;
563202181Sthompsa
564202181Sthompsa	id = usbd_get_interface_descriptor(uaa->iface);
565202181Sthompsa	sc->sc_ctrl_iface_no = id->bInterfaceNumber;
566202181Sthompsa
567202181Sthompsa	sc->sc_iface_no = uaa->info.bIfaceNum;
568202181Sthompsa	sc->sc_iface_index = uaa->info.bIfaceIndex;
569202181Sthompsa
570202181Sthompsa	/* Setup control pipe */
571202181Sthompsa	uerr = usbd_transfer_setup(uaa->device,
572202181Sthompsa	    &sc->sc_iface_index, sc->sc_ctrl_xfer,
573202181Sthompsa	    uhso_ctrl_config, UHSO_CTRL_MAX, sc, &sc->sc_mtx);
574202181Sthompsa	if (uerr) {
575202181Sthompsa		device_printf(self, "Failed to setup control pipe: %s\n",
576202181Sthompsa		    usbd_errstr(uerr));
577202181Sthompsa		goto out;
578202181Sthompsa	}
579202181Sthompsa
580202181Sthompsa	if (USB_GET_DRIVER_INFO(uaa) == UHSO_STATIC_IFACE)
581202181Sthompsa		probe_f = uhso_probe_iface_static;
582202181Sthompsa	else if (USB_GET_DRIVER_INFO(uaa) == UHSO_AUTO_IFACE)
583202181Sthompsa		probe_f = uhso_probe_iface_auto;
584202181Sthompsa	else
585202181Sthompsa		goto out;
586202181Sthompsa
587202181Sthompsa	error = uhso_probe_iface(sc, uaa->info.bIfaceNum, probe_f);
588202181Sthompsa	if (error != 0)
589202181Sthompsa		goto out;
590202181Sthompsa
591202181Sthompsa	sctx = device_get_sysctl_ctx(sc->sc_dev);
592202181Sthompsa	soid = device_get_sysctl_tree(sc->sc_dev);
593202181Sthompsa
594202181Sthompsa	SYSCTL_ADD_STRING(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "type",
595202181Sthompsa	    CTLFLAG_RD, uhso_port[UHSO_IFACE_PORT(sc->sc_type)], 0,
596202181Sthompsa	    "Port available at this interface");
597210275Sthompsa	SYSCTL_ADD_PROC(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "radio",
598210275Sthompsa	    CTLTYPE_INT | CTLFLAG_RW, sc, 0, uhso_radio_sysctl, "I", "Enable radio");
599202181Sthompsa
600202243Sthompsa	/*
601202270Sthompsa	 * The default interface description on most Option devices isn't
602202243Sthompsa	 * very helpful. So we skip device_set_usb_desc and set the
603202243Sthompsa	 * device description manually.
604202243Sthompsa	 */
605202243Sthompsa	device_set_desc_copy(self, uhso_port_type[UHSO_IFACE_PORT_TYPE(sc->sc_type)]);
606202243Sthompsa	/* Announce device */
607202243Sthompsa	device_printf(self, "<%s port> at <%s %s> on %s\n",
608202243Sthompsa	    uhso_port_type[UHSO_IFACE_PORT_TYPE(sc->sc_type)],
609212136Sthompsa	    usb_get_manufacturer(uaa->device),
610212136Sthompsa	    usb_get_product(uaa->device),
611212136Sthompsa	    device_get_nameunit(device_get_parent(self)));
612202243Sthompsa
613202181Sthompsa	if (sc->sc_ttys > 0) {
614202181Sthompsa		SYSCTL_ADD_INT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "ports",
615202181Sthompsa		    CTLFLAG_RD, &sc->sc_ttys, 0, "Number of attached serial ports");
616202181Sthompsa
617202181Sthompsa		tree = SYSCTL_ADD_NODE(sctx, SYSCTL_CHILDREN(soid), OID_AUTO,
618202181Sthompsa		    "port", CTLFLAG_RD, NULL, "Serial ports");
619202181Sthompsa	}
620202181Sthompsa
621202243Sthompsa	/*
622202243Sthompsa	 * Loop through the number of found TTYs and create sysctl
623202243Sthompsa	 * nodes for them.
624202243Sthompsa	 */
625202181Sthompsa	for (i = 0; i < sc->sc_ttys; i++) {
626202181Sthompsa		ht = &sc->sc_tty[i];
627202181Sthompsa		ucom = &sc->sc_ucom[i];
628202181Sthompsa
629202181Sthompsa		if (UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_MUX)
630202181Sthompsa			port = uhso_mux_port_map[ht->ht_muxport];
631202181Sthompsa		else
632202181Sthompsa			port = UHSO_IFACE_PORT_TYPE(sc->sc_type);
633202181Sthompsa
634202181Sthompsa		desc = uhso_port_type_sysctl[port];
635202181Sthompsa
636202181Sthompsa		tty_node = SYSCTL_ADD_NODE(sctx, SYSCTL_CHILDREN(tree), OID_AUTO,
637202181Sthompsa		    desc, CTLFLAG_RD, NULL, "");
638202181Sthompsa
639202181Sthompsa		ht->ht_name[0] = 0;
640202181Sthompsa		if (sc->sc_ttys == 1)
641214761Sn_hibma			snprintf(ht->ht_name, 32, "cuaU%d", ucom->sc_super->sc_unit);
642202181Sthompsa		else {
643202181Sthompsa			snprintf(ht->ht_name, 32, "cuaU%d.%d",
644214761Sn_hibma			    ucom->sc_super->sc_unit, ucom->sc_subunit);
645202181Sthompsa		}
646202181Sthompsa
647202181Sthompsa		desc = uhso_port_type[port];
648202181Sthompsa		SYSCTL_ADD_STRING(sctx, SYSCTL_CHILDREN(tty_node), OID_AUTO,
649202181Sthompsa		    "tty", CTLFLAG_RD, ht->ht_name, 0, "");
650202181Sthompsa		SYSCTL_ADD_STRING(sctx, SYSCTL_CHILDREN(tty_node), OID_AUTO,
651202181Sthompsa		    "desc", CTLFLAG_RD, desc, 0, "");
652202181Sthompsa
653202181Sthompsa		if (bootverbose)
654202181Sthompsa			device_printf(sc->sc_dev,
655202181Sthompsa			    "\"%s\" port at %s\n", desc, ht->ht_name);
656202181Sthompsa	}
657202181Sthompsa
658202181Sthompsa	return (0);
659202181Sthompsaout:
660202181Sthompsa	uhso_detach(sc->sc_dev);
661202181Sthompsa	return (ENXIO);
662202181Sthompsa}
663202181Sthompsa
664202181Sthompsastatic int
665202181Sthompsauhso_detach(device_t self)
666202181Sthompsa{
667202181Sthompsa	struct uhso_softc *sc = device_get_softc(self);
668202181Sthompsa	int i;
669202181Sthompsa
670202181Sthompsa	usbd_transfer_unsetup(sc->sc_xfer, 3);
671202181Sthompsa	usbd_transfer_unsetup(sc->sc_ctrl_xfer, UHSO_CTRL_MAX);
672202181Sthompsa	if (sc->sc_ttys > 0) {
673214761Sn_hibma		ucom_detach(&sc->sc_super_ucom, sc->sc_ucom);
674202181Sthompsa
675202181Sthompsa		for (i = 0; i < sc->sc_ttys; i++) {
676202181Sthompsa			if (sc->sc_tty[i].ht_muxport != -1) {
677202181Sthompsa				usbd_transfer_unsetup(sc->sc_tty[i].ht_xfer,
678202181Sthompsa				    UHSO_CTRL_MAX);
679202181Sthompsa			}
680202181Sthompsa		}
681202181Sthompsa
682202181Sthompsa		free(sc->sc_tty, M_USBDEV);
683202181Sthompsa		free(sc->sc_ucom, M_USBDEV);
684202181Sthompsa	}
685202181Sthompsa
686202181Sthompsa	if (sc->sc_ifp != NULL) {
687202181Sthompsa		callout_drain(&sc->sc_c);
688210275Sthompsa		free_unr(uhso_ifnet_unit, sc->sc_ifp->if_dunit);
689202181Sthompsa		mtx_lock(&sc->sc_mtx);
690202181Sthompsa		uhso_if_stop(sc);
691202181Sthompsa		bpfdetach(sc->sc_ifp);
692202181Sthompsa		if_detach(sc->sc_ifp);
693202181Sthompsa		if_free(sc->sc_ifp);
694202181Sthompsa		mtx_unlock(&sc->sc_mtx);
695202181Sthompsa		usbd_transfer_unsetup(sc->sc_if_xfer, UHSO_IFNET_MAX);
696202181Sthompsa	}
697202181Sthompsa
698239299Shselasky	device_claim_softc(self);
699239299Shselasky
700239299Shselasky	uhso_free_softc(sc);
701239299Shselasky
702202181Sthompsa	return (0);
703202181Sthompsa}
704202181Sthompsa
705239180ShselaskyUCOM_UNLOAD_DRAIN(uhso);
706239180Shselasky
707202181Sthompsastatic void
708239299Shselaskyuhso_free_softc(struct uhso_softc *sc)
709239180Shselasky{
710239180Shselasky	if (ucom_unref(&sc->sc_super_ucom)) {
711239299Shselasky		mtx_destroy(&sc->sc_mtx);
712239299Shselasky		device_free_softc(sc);
713239180Shselasky	}
714239180Shselasky}
715239180Shselasky
716239180Shselaskystatic void
717239180Shselaskyuhso_free(struct ucom_softc *ucom)
718239180Shselasky{
719239299Shselasky	uhso_free_softc(ucom->sc_parent);
720239180Shselasky}
721239180Shselasky
722239180Shselaskystatic void
723202181Sthompsauhso_test_autoinst(void *arg, struct usb_device *udev,
724202181Sthompsa    struct usb_attach_arg *uaa)
725202181Sthompsa{
726202181Sthompsa	struct usb_interface *iface;
727202181Sthompsa	struct usb_interface_descriptor *id;
728202181Sthompsa
729202243Sthompsa	if (uaa->dev_state != UAA_DEV_READY || !uhso_autoswitch)
730202181Sthompsa		return;
731202181Sthompsa
732202181Sthompsa	iface = usbd_get_iface(udev, 0);
733202181Sthompsa	if (iface == NULL)
734202181Sthompsa		return;
735202181Sthompsa	id = iface->idesc;
736202181Sthompsa	if (id == NULL || id->bInterfaceClass != UICLASS_MASS)
737202181Sthompsa		return;
738202181Sthompsa	if (usbd_lookup_id_by_uaa(uhso_devs, sizeof(uhso_devs), uaa))
739202181Sthompsa		return;		/* no device match */
740202181Sthompsa
741202181Sthompsa	if (usb_msc_eject(udev, 0, MSC_EJECT_REZERO) == 0) {
742202181Sthompsa		/* success, mark the udev as disappearing */
743202181Sthompsa		uaa->dev_state = UAA_DEV_EJECTING;
744202181Sthompsa	}
745202181Sthompsa}
746202181Sthompsa
747202181Sthompsastatic int
748202181Sthompsauhso_driver_loaded(struct module *mod, int what, void *arg)
749202181Sthompsa{
750202181Sthompsa	switch (what) {
751202181Sthompsa	case MOD_LOAD:
752202181Sthompsa		/* register our autoinstall handler */
753202181Sthompsa		uhso_etag = EVENTHANDLER_REGISTER(usb_dev_configured,
754202181Sthompsa		    uhso_test_autoinst, NULL, EVENTHANDLER_PRI_ANY);
755210275Sthompsa		/* create our unit allocator for inet devs */
756210275Sthompsa		uhso_ifnet_unit = new_unrhdr(0, INT_MAX, NULL);
757202181Sthompsa		break;
758202181Sthompsa	case MOD_UNLOAD:
759202181Sthompsa		EVENTHANDLER_DEREGISTER(usb_dev_configured, uhso_etag);
760210275Sthompsa		delete_unrhdr(uhso_ifnet_unit);
761202181Sthompsa		break;
762202181Sthompsa	default:
763202181Sthompsa		return (EOPNOTSUPP);
764202181Sthompsa	}
765202181Sthompsa	return (0);
766202181Sthompsa}
767202181Sthompsa
768202243Sthompsa/*
769202243Sthompsa * Probe the interface type by querying the device. The elements
770202243Sthompsa * of an array indicates the capabilities of a particular interface.
771202243Sthompsa * Returns a bit mask with the interface capabilities.
772202243Sthompsa */
773202243Sthompsastatic int
774210275Sthompsauhso_probe_iface_auto(struct usb_device *udev, int index)
775202181Sthompsa{
776202181Sthompsa	struct usb_device_request req;
777202181Sthompsa	usb_error_t uerr;
778202181Sthompsa	uint16_t actlen = 0;
779202181Sthompsa	char port;
780202181Sthompsa	char buf[17] = {0};
781202181Sthompsa
782202181Sthompsa	req.bmRequestType = UT_READ_VENDOR_DEVICE;
783202181Sthompsa	req.bRequest = 0x86;
784202181Sthompsa	USETW(req.wValue, 0);
785202181Sthompsa	USETW(req.wIndex, 0);
786202181Sthompsa	USETW(req.wLength, 17);
787202181Sthompsa
788210275Sthompsa	uerr = usbd_do_request_flags(udev, NULL, &req, buf,
789202181Sthompsa	    0, &actlen, USB_MS_HZ);
790202181Sthompsa	if (uerr != 0) {
791210275Sthompsa		printf("%s: usbd_do_request_flags failed, %s\n",
792210275Sthompsa		    __func__, usbd_errstr(uerr));
793202181Sthompsa		return (0);
794202181Sthompsa	}
795202181Sthompsa
796202243Sthompsa	UHSO_DPRINTF(1, "actlen=%d\n", actlen);
797202181Sthompsa	UHSO_HEXDUMP(buf, 17);
798202181Sthompsa
799202181Sthompsa	if (index < 0 || index > 16) {
800202181Sthompsa		UHSO_DPRINTF(0, "Index %d out of range\n", index);
801202181Sthompsa		return (0);
802202181Sthompsa	}
803202181Sthompsa
804202243Sthompsa	UHSO_DPRINTF(1, "index=%d, type=%x[%s]\n", index, buf[index],
805202243Sthompsa	    uhso_port_type[(int)uhso_port_map[(int)buf[index]]]);
806202181Sthompsa
807202181Sthompsa	if (buf[index] >= uhso_port_map_max)
808202181Sthompsa		port = 0;
809202181Sthompsa	else
810202181Sthompsa		port = uhso_port_map[(int)buf[index]];
811202181Sthompsa
812202243Sthompsa	switch (port) {
813202243Sthompsa	case UHSO_PORT_TYPE_NETWORK:
814202243Sthompsa		return (UHSO_IFACE_SPEC(UHSO_IF_NET | UHSO_IF_MUX,
815202243Sthompsa		    UHSO_PORT_SERIAL | UHSO_PORT_NETWORK, port));
816210275Sthompsa	case UHSO_PORT_TYPE_DIAG:
817210275Sthompsa	case UHSO_PORT_TYPE_DIAG2:
818210275Sthompsa	case UHSO_PORT_TYPE_CTL:
819210275Sthompsa	case UHSO_PORT_TYPE_APP:
820210275Sthompsa	case UHSO_PORT_TYPE_APP2:
821210275Sthompsa	case UHSO_PORT_TYPE_MODEM:
822210275Sthompsa		return (UHSO_IFACE_SPEC(UHSO_IF_BULK,
823210275Sthompsa		    UHSO_PORT_SERIAL, port));
824210275Sthompsa	case UHSO_PORT_TYPE_MSD:
825202181Sthompsa		return (0);
826210275Sthompsa	case UHSO_PORT_TYPE_UNKNOWN:
827202243Sthompsa	default:
828210275Sthompsa		return (0);
829202243Sthompsa	}
830202181Sthompsa
831202181Sthompsa	return (0);
832202181Sthompsa}
833202181Sthompsa
834210275Sthompsa/*
835210275Sthompsa * Returns the capabilities of interfaces for devices that don't
836210275Sthompsa * support the automatic query.
837210275Sthompsa * Returns a bit mask with the interface capabilities.
838210275Sthompsa */
839202181Sthompsastatic int
840210275Sthompsauhso_probe_iface_static(struct usb_device *udev, int index)
841202181Sthompsa{
842202181Sthompsa	struct usb_config_descriptor *cd;
843202181Sthompsa
844210275Sthompsa	cd = usbd_get_config_descriptor(udev);
845202181Sthompsa	if (cd->bNumInterface <= 3) {
846202243Sthompsa		/* Cards with 3 or less interfaces */
847202181Sthompsa		switch (index) {
848202181Sthompsa		case 0:
849202181Sthompsa			return UHSO_IFACE_SPEC(UHSO_IF_NET | UHSO_IF_MUX,
850202243Sthompsa			    UHSO_PORT_SERIAL | UHSO_PORT_NETWORK,
851202243Sthompsa			    UHSO_PORT_TYPE_NETWORK);
852202181Sthompsa		case 1:
853202181Sthompsa			return UHSO_IFACE_SPEC(UHSO_IF_BULK,
854202181Sthompsa			    UHSO_PORT_SERIAL, UHSO_PORT_TYPE_DIAG);
855202181Sthompsa		case 2:
856202181Sthompsa			return UHSO_IFACE_SPEC(UHSO_IF_BULK,
857202181Sthompsa			    UHSO_PORT_SERIAL, UHSO_PORT_TYPE_MODEM);
858202181Sthompsa		}
859202243Sthompsa	} else {
860202243Sthompsa		/* Cards with 4 interfaces */
861202181Sthompsa		switch (index) {
862202181Sthompsa		case 0:
863202181Sthompsa			return UHSO_IFACE_SPEC(UHSO_IF_NET | UHSO_IF_MUX,
864202243Sthompsa			    UHSO_PORT_SERIAL | UHSO_PORT_NETWORK,
865202243Sthompsa			    UHSO_PORT_TYPE_NETWORK);
866202181Sthompsa		case 1:
867202181Sthompsa			return UHSO_IFACE_SPEC(UHSO_IF_BULK,
868202181Sthompsa			    UHSO_PORT_SERIAL, UHSO_PORT_TYPE_DIAG2);
869202181Sthompsa		case 2:
870202181Sthompsa			return UHSO_IFACE_SPEC(UHSO_IF_BULK,
871202181Sthompsa			    UHSO_PORT_SERIAL, UHSO_PORT_TYPE_MODEM);
872202181Sthompsa		case 3:
873202181Sthompsa			return UHSO_IFACE_SPEC(UHSO_IF_BULK,
874202181Sthompsa			    UHSO_PORT_SERIAL, UHSO_PORT_TYPE_DIAG);
875202181Sthompsa		}
876202181Sthompsa	}
877202181Sthompsa	return (0);
878202181Sthompsa}
879202181Sthompsa
880202243Sthompsa/*
881202243Sthompsa * Probes an interface for its particular capabilities and attaches if
882202243Sthompsa * it's a supported interface.
883202243Sthompsa */
884202181Sthompsastatic int
885202181Sthompsauhso_probe_iface(struct uhso_softc *sc, int index,
886210275Sthompsa    int (*probe)(struct usb_device *, int))
887202181Sthompsa{
888202181Sthompsa	struct usb_interface *iface;
889202243Sthompsa	int type, error;
890202181Sthompsa
891202243Sthompsa	UHSO_DPRINTF(1, "Probing for interface %d, probe_func=%p\n", index, probe);
892202181Sthompsa
893210275Sthompsa	type = probe(sc->sc_udev, index);
894202181Sthompsa	UHSO_DPRINTF(1, "Probe result %x\n", type);
895202181Sthompsa	if (type <= 0)
896202181Sthompsa		return (ENXIO);
897202181Sthompsa
898202181Sthompsa	sc->sc_type = type;
899202181Sthompsa	iface = usbd_get_iface(sc->sc_udev, index);
900202181Sthompsa
901202243Sthompsa	if (UHSO_IFACE_PORT_TYPE(type) == UHSO_PORT_TYPE_NETWORK) {
902202181Sthompsa		error = uhso_attach_ifnet(sc, iface, type);
903202243Sthompsa		if (error) {
904202243Sthompsa			UHSO_DPRINTF(1, "uhso_attach_ifnet failed");
905202181Sthompsa			return (ENXIO);
906202243Sthompsa		}
907202181Sthompsa
908202243Sthompsa		/*
909202243Sthompsa		 * If there is an additional interrupt endpoint on this
910202270Sthompsa		 * interface then we most likely have a multiplexed serial port
911202243Sthompsa		 * available.
912202243Sthompsa		 */
913202243Sthompsa		if (iface->idesc->bNumEndpoints < 3) {
914202243Sthompsa			sc->sc_type = UHSO_IFACE_SPEC(
915202243Sthompsa			    UHSO_IFACE_USB_TYPE(type) & ~UHSO_IF_MUX,
916202243Sthompsa			    UHSO_IFACE_PORT(type) & ~UHSO_PORT_SERIAL,
917202243Sthompsa			    UHSO_IFACE_PORT_TYPE(type));
918202243Sthompsa			return (0);
919202243Sthompsa		}
920202243Sthompsa
921202243Sthompsa		UHSO_DPRINTF(1, "Trying to attach mux. serial\n");
922202243Sthompsa		error = uhso_attach_muxserial(sc, iface, type);
923202243Sthompsa		if (error == 0 && sc->sc_ttys > 0) {
924202181Sthompsa			error = ucom_attach(&sc->sc_super_ucom, sc->sc_ucom,
925202181Sthompsa			    sc->sc_ttys, sc, &uhso_ucom_callback, &sc->sc_mtx);
926202181Sthompsa			if (error) {
927202181Sthompsa				device_printf(sc->sc_dev, "ucom_attach failed\n");
928202181Sthompsa				return (ENXIO);
929202181Sthompsa			}
930214843Sn_hibma			ucom_set_pnpinfo_usb(&sc->sc_super_ucom, sc->sc_dev);
931202243Sthompsa
932202243Sthompsa			mtx_lock(&sc->sc_mtx);
933202243Sthompsa			usbd_transfer_start(sc->sc_xfer[UHSO_MUX_ENDPT_INTR]);
934202243Sthompsa			mtx_unlock(&sc->sc_mtx);
935202181Sthompsa		}
936202243Sthompsa	} else if ((UHSO_IFACE_USB_TYPE(type) & UHSO_IF_BULK) &&
937202181Sthompsa	    UHSO_IFACE_PORT(type) & UHSO_PORT_SERIAL) {
938202181Sthompsa
939202181Sthompsa		error = uhso_attach_bulkserial(sc, iface, type);
940202181Sthompsa		if (error)
941202181Sthompsa			return (ENXIO);
942202181Sthompsa
943202181Sthompsa		error = ucom_attach(&sc->sc_super_ucom, sc->sc_ucom,
944202181Sthompsa		    sc->sc_ttys, sc, &uhso_ucom_callback, &sc->sc_mtx);
945202181Sthompsa		if (error) {
946202181Sthompsa			device_printf(sc->sc_dev, "ucom_attach failed\n");
947202181Sthompsa			return (ENXIO);
948202181Sthompsa		}
949214843Sn_hibma		ucom_set_pnpinfo_usb(&sc->sc_super_ucom, sc->sc_dev);
950202181Sthompsa	}
951202181Sthompsa	else {
952202243Sthompsa		UHSO_DPRINTF(0, "Unknown type %x\n", type);
953202181Sthompsa		return (ENXIO);
954202181Sthompsa	}
955202181Sthompsa
956202181Sthompsa	return (0);
957202181Sthompsa}
958202181Sthompsa
959210275Sthompsastatic int
960210275Sthompsauhso_radio_ctrl(struct uhso_softc *sc, int onoff)
961210275Sthompsa{
962210275Sthompsa	struct usb_device_request req;
963210275Sthompsa	usb_error_t uerr;
964210275Sthompsa
965210275Sthompsa	req.bmRequestType = UT_VENDOR;
966210275Sthompsa	req.bRequest = onoff ? 0x82 : 0x81;
967210275Sthompsa	USETW(req.wValue, 0);
968210275Sthompsa	USETW(req.wIndex, 0);
969210275Sthompsa	USETW(req.wLength, 0);
970210275Sthompsa
971210275Sthompsa	uerr = usbd_do_request(sc->sc_udev, NULL, &req, NULL);
972210275Sthompsa	if (uerr != 0) {
973210275Sthompsa		device_printf(sc->sc_dev, "usbd_do_request_flags failed: %s\n",
974210275Sthompsa		    usbd_errstr(uerr));
975210275Sthompsa		return (-1);
976210275Sthompsa	}
977210275Sthompsa	return (onoff);
978210275Sthompsa}
979210275Sthompsa
980210275Sthompsastatic int
981210275Sthompsauhso_radio_sysctl(SYSCTL_HANDLER_ARGS)
982210275Sthompsa{
983210275Sthompsa	struct uhso_softc *sc = arg1;
984210275Sthompsa	int error, radio;
985210275Sthompsa
986210275Sthompsa	radio = sc->sc_radio;
987210275Sthompsa	error = sysctl_handle_int(oidp, &radio, 0, req);
988210275Sthompsa	if (error)
989210275Sthompsa		return (error);
990210275Sthompsa	if (radio != sc->sc_radio) {
991210275Sthompsa		radio = radio != 0 ? 1 : 0;
992210275Sthompsa		error = uhso_radio_ctrl(sc, radio);
993210275Sthompsa		if (error != -1)
994210275Sthompsa			sc->sc_radio = radio;
995210275Sthompsa
996210275Sthompsa	}
997210275Sthompsa	return (0);
998210275Sthompsa}
999210275Sthompsa
1000202243Sthompsa/*
1001202243Sthompsa * Expands allocated memory to fit an additional TTY.
1002202243Sthompsa * Two arrays are kept with matching indexes, one for ucom and one
1003202243Sthompsa * for our private data.
1004202243Sthompsa */
1005202181Sthompsastatic int
1006202181Sthompsauhso_alloc_tty(struct uhso_softc *sc)
1007202181Sthompsa{
1008202181Sthompsa
1009202181Sthompsa	sc->sc_ttys++;
1010202181Sthompsa	sc->sc_tty = reallocf(sc->sc_tty, sizeof(struct uhso_tty) * sc->sc_ttys,
1011202181Sthompsa	    M_USBDEV, M_WAITOK | M_ZERO);
1012202181Sthompsa	if (sc->sc_tty == NULL)
1013202181Sthompsa		return (-1);
1014202181Sthompsa
1015202181Sthompsa	sc->sc_ucom = reallocf(sc->sc_ucom,
1016202181Sthompsa	    sizeof(struct ucom_softc) * sc->sc_ttys, M_USBDEV, M_WAITOK | M_ZERO);
1017202181Sthompsa	if (sc->sc_ucom == NULL)
1018202181Sthompsa		return (-1);
1019202181Sthompsa
1020202181Sthompsa	sc->sc_tty[sc->sc_ttys - 1].ht_sc = sc;
1021202181Sthompsa
1022202243Sthompsa	UHSO_DPRINTF(1, "Allocated TTY %d\n", sc->sc_ttys - 1);
1023202181Sthompsa	return (sc->sc_ttys - 1);
1024202181Sthompsa}
1025202181Sthompsa
1026202243Sthompsa/*
1027202243Sthompsa * Attach a multiplexed serial port
1028202243Sthompsa * Data is read/written with requests on the default control pipe. An interrupt
1029202243Sthompsa * endpoint returns when there is new data to be read.
1030202243Sthompsa */
1031202181Sthompsastatic int
1032202181Sthompsauhso_attach_muxserial(struct uhso_softc *sc, struct usb_interface *iface,
1033202181Sthompsa    int type)
1034202181Sthompsa{
1035202181Sthompsa	struct usb_descriptor *desc;
1036202181Sthompsa	int i, port, tty;
1037202181Sthompsa	usb_error_t uerr;
1038202181Sthompsa
1039202181Sthompsa	/*
1040202181Sthompsa	 * The class specific interface (type 0x24) descriptor subtype field
1041202181Sthompsa	 * contains a bitmask that specifies which (and how many) ports that
1042202181Sthompsa	 * are available through this multiplexed serial port.
1043202181Sthompsa 	 */
1044202181Sthompsa	desc = usbd_find_descriptor(sc->sc_udev, NULL,
1045202181Sthompsa	    iface->idesc->bInterfaceNumber, UDESC_CS_INTERFACE, 0xff, 0, 0);
1046202181Sthompsa	if (desc == NULL) {
1047202181Sthompsa		UHSO_DPRINTF(0, "Failed to find UDESC_CS_INTERFACE\n");
1048202181Sthompsa		return (ENXIO);
1049202181Sthompsa	}
1050202181Sthompsa
1051202181Sthompsa	UHSO_DPRINTF(1, "Mux port mask %x\n", desc->bDescriptorSubtype);
1052202181Sthompsa	if (desc->bDescriptorSubtype == 0)
1053202181Sthompsa		return (ENXIO);
1054202181Sthompsa
1055202243Sthompsa	/*
1056202243Sthompsa	 * The bitmask is one octet, loop through the number of
1057202243Sthompsa	 * bits that are set and create a TTY for each.
1058202243Sthompsa	 */
1059202181Sthompsa	for (i = 0; i < 8; i++) {
1060202181Sthompsa		port = (1 << i);
1061202181Sthompsa		if ((port & desc->bDescriptorSubtype) == port) {
1062202181Sthompsa			UHSO_DPRINTF(2, "Found mux port %x (%d)\n", port, i);
1063202181Sthompsa			tty = uhso_alloc_tty(sc);
1064202181Sthompsa			if (tty < 0)
1065202181Sthompsa				return (ENOMEM);
1066202181Sthompsa			sc->sc_tty[tty].ht_muxport = i;
1067202181Sthompsa			uerr = usbd_transfer_setup(sc->sc_udev,
1068202181Sthompsa			    &sc->sc_iface_index, sc->sc_tty[tty].ht_xfer,
1069202181Sthompsa			    uhso_ctrl_config, UHSO_CTRL_MAX, sc, &sc->sc_mtx);
1070202181Sthompsa			if (uerr) {
1071202181Sthompsa				device_printf(sc->sc_dev,
1072202181Sthompsa				    "Failed to setup control pipe: %s\n",
1073202181Sthompsa				    usbd_errstr(uerr));
1074202181Sthompsa				return (ENXIO);
1075202181Sthompsa			}
1076202181Sthompsa		}
1077202181Sthompsa	}
1078202181Sthompsa
1079202243Sthompsa	/* Setup the intr. endpoint */
1080202181Sthompsa	uerr = usbd_transfer_setup(sc->sc_udev,
1081202181Sthompsa	    &iface->idesc->bInterfaceNumber, sc->sc_xfer,
1082202181Sthompsa	    uhso_mux_config, 1, sc, &sc->sc_mtx);
1083202181Sthompsa	if (uerr)
1084202181Sthompsa		return (ENXIO);
1085202181Sthompsa
1086202181Sthompsa	return (0);
1087202181Sthompsa}
1088202181Sthompsa
1089202243Sthompsa/*
1090202243Sthompsa * Interrupt callback for the multiplexed serial port. Indicates
1091202270Sthompsa * which serial port has data waiting.
1092202243Sthompsa */
1093202181Sthompsastatic void
1094202181Sthompsauhso_mux_intr_callback(struct usb_xfer *xfer, usb_error_t error)
1095202181Sthompsa{
1096202181Sthompsa	struct usb_page_cache *pc;
1097202181Sthompsa	struct usb_page_search res;
1098202181Sthompsa	struct uhso_softc *sc = usbd_xfer_softc(xfer);
1099202181Sthompsa	unsigned int i, mux;
1100202181Sthompsa
1101202181Sthompsa	UHSO_DPRINTF(3, "status %d\n", USB_GET_STATE(xfer));
1102202181Sthompsa
1103202181Sthompsa	switch (USB_GET_STATE(xfer)) {
1104202181Sthompsa	case USB_ST_TRANSFERRED:
1105202181Sthompsa		/*
1106202181Sthompsa		 * The multiplexed port number can be found at the first byte.
1107202181Sthompsa		 * It contains a bit mask, we transform this in to an integer.
1108202181Sthompsa		 */
1109202181Sthompsa		pc = usbd_xfer_get_frame(xfer, 0);
1110202181Sthompsa		usbd_get_page(pc, 0, &res);
1111202181Sthompsa
1112202181Sthompsa		i = *((unsigned char *)res.buffer);
1113202181Sthompsa		mux = 0;
1114202181Sthompsa		while (i >>= 1) {
1115202181Sthompsa			mux++;
1116202181Sthompsa		}
1117202181Sthompsa
1118202181Sthompsa		UHSO_DPRINTF(3, "mux port %d (%d)\n", mux, i);
1119202181Sthompsa		if (mux > UHSO_MPORT_TYPE_NOMAX)
1120202181Sthompsa			break;
1121202181Sthompsa
1122202243Sthompsa		/* Issue a read for this serial port */
1123202181Sthompsa		usbd_xfer_set_priv(
1124202181Sthompsa		    sc->sc_tty[mux].ht_xfer[UHSO_CTRL_READ],
1125202181Sthompsa		    &sc->sc_tty[mux]);
1126202181Sthompsa		usbd_transfer_start(sc->sc_tty[mux].ht_xfer[UHSO_CTRL_READ]);
1127202181Sthompsa
1128202181Sthompsa		break;
1129202181Sthompsa	case USB_ST_SETUP:
1130202181Sthompsatr_setup:
1131202181Sthompsa		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
1132202181Sthompsa		usbd_transfer_submit(xfer);
1133202181Sthompsa		break;
1134202181Sthompsa	default:
1135202181Sthompsa		UHSO_DPRINTF(0, "error: %s\n", usbd_errstr(error));
1136202181Sthompsa		if (error == USB_ERR_CANCELLED)
1137202181Sthompsa			break;
1138202181Sthompsa
1139202181Sthompsa		usbd_xfer_set_stall(xfer);
1140202181Sthompsa		goto tr_setup;
1141202181Sthompsa	}
1142202181Sthompsa}
1143202181Sthompsa
1144202181Sthompsastatic void
1145202181Sthompsauhso_mux_read_callback(struct usb_xfer *xfer, usb_error_t error)
1146202181Sthompsa{
1147202181Sthompsa	struct uhso_softc *sc = usbd_xfer_softc(xfer);
1148202181Sthompsa	struct usb_page_cache *pc;
1149202181Sthompsa	struct usb_device_request req;
1150202181Sthompsa	struct uhso_tty *ht;
1151202181Sthompsa	int actlen, len;
1152202181Sthompsa
1153202181Sthompsa	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
1154202181Sthompsa
1155202181Sthompsa	UHSO_DPRINTF(3, "status %d\n", USB_GET_STATE(xfer));
1156202181Sthompsa
1157202181Sthompsa	ht = usbd_xfer_get_priv(xfer);
1158202181Sthompsa	UHSO_DPRINTF(3, "ht=%p open=%d\n", ht, ht->ht_open);
1159202181Sthompsa
1160202181Sthompsa	switch (USB_GET_STATE(xfer)) {
1161202181Sthompsa	case USB_ST_TRANSFERRED:
1162202181Sthompsa		/* Got data, send to ucom */
1163202181Sthompsa		pc = usbd_xfer_get_frame(xfer, 1);
1164202181Sthompsa		len = usbd_xfer_frame_len(xfer, 1);
1165202181Sthompsa
1166202181Sthompsa		UHSO_DPRINTF(3, "got %d bytes on mux port %d\n", len,
1167202181Sthompsa		    ht->ht_muxport);
1168202181Sthompsa		if (len <= 0) {
1169202181Sthompsa			usbd_transfer_start(sc->sc_xfer[UHSO_MUX_ENDPT_INTR]);
1170202181Sthompsa			break;
1171202181Sthompsa		}
1172202181Sthompsa
1173202181Sthompsa		/* Deliver data if the TTY is open, discard otherwise */
1174202181Sthompsa		if (ht->ht_open)
1175202181Sthompsa			ucom_put_data(&sc->sc_ucom[ht->ht_muxport], pc, 0, len);
1176202181Sthompsa		/* FALLTHROUGH */
1177202181Sthompsa	case USB_ST_SETUP:
1178202181Sthompsatr_setup:
1179227461Shselasky		memset(&req, 0, sizeof(struct usb_device_request));
1180202181Sthompsa		req.bmRequestType = UT_READ_CLASS_INTERFACE;
1181202181Sthompsa		req.bRequest = UCDC_GET_ENCAPSULATED_RESPONSE;
1182202181Sthompsa		USETW(req.wValue, 0);
1183202181Sthompsa		USETW(req.wIndex, ht->ht_muxport);
1184202181Sthompsa		USETW(req.wLength, 1024);
1185202181Sthompsa
1186202181Sthompsa		pc = usbd_xfer_get_frame(xfer, 0);
1187202181Sthompsa		usbd_copy_in(pc, 0, &req, sizeof(req));
1188202181Sthompsa
1189202181Sthompsa		usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
1190202181Sthompsa		usbd_xfer_set_frame_len(xfer, 1, 1024);
1191202181Sthompsa		usbd_xfer_set_frames(xfer, 2);
1192202181Sthompsa		usbd_transfer_submit(xfer);
1193202181Sthompsa		break;
1194202181Sthompsa	default:
1195202181Sthompsa		UHSO_DPRINTF(0, "error: %s\n", usbd_errstr(error));
1196202181Sthompsa		if (error == USB_ERR_CANCELLED)
1197202181Sthompsa			break;
1198202181Sthompsa		usbd_xfer_set_stall(xfer);
1199202181Sthompsa		goto tr_setup;
1200202181Sthompsa	}
1201202181Sthompsa}
1202202181Sthompsa
1203202181Sthompsastatic void
1204202181Sthompsauhso_mux_write_callback(struct usb_xfer *xfer, usb_error_t error)
1205202181Sthompsa{
1206202181Sthompsa	struct uhso_softc *sc = usbd_xfer_softc(xfer);
1207202181Sthompsa	struct uhso_tty *ht;
1208202181Sthompsa	struct usb_page_cache *pc;
1209202181Sthompsa	struct usb_device_request req;
1210202181Sthompsa	int actlen;
1211202181Sthompsa	struct usb_page_search res;
1212202181Sthompsa
1213202181Sthompsa	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
1214202181Sthompsa
1215202181Sthompsa	ht = usbd_xfer_get_priv(xfer);
1216202181Sthompsa	UHSO_DPRINTF(3, "status=%d, using mux port %d\n",
1217202181Sthompsa	    USB_GET_STATE(xfer), ht->ht_muxport);
1218202181Sthompsa
1219202181Sthompsa	switch (USB_GET_STATE(xfer)) {
1220202181Sthompsa	case USB_ST_TRANSFERRED:
1221202181Sthompsa		UHSO_DPRINTF(3, "wrote %zd data bytes to muxport %d\n",
1222202181Sthompsa		    actlen - sizeof(struct usb_device_request) ,
1223202181Sthompsa		    ht->ht_muxport);
1224202181Sthompsa		/* FALLTHROUGH */
1225202181Sthompsa	case USB_ST_SETUP:
1226202181Sthompsa		pc = usbd_xfer_get_frame(xfer, 1);
1227202181Sthompsa		if (ucom_get_data(&sc->sc_ucom[ht->ht_muxport], pc,
1228202181Sthompsa		    0, 32, &actlen)) {
1229202181Sthompsa
1230202181Sthompsa			usbd_get_page(pc, 0, &res);
1231202181Sthompsa
1232227461Shselasky			memset(&req, 0, sizeof(struct usb_device_request));
1233202181Sthompsa			req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
1234202181Sthompsa			req.bRequest = UCDC_SEND_ENCAPSULATED_COMMAND;
1235202181Sthompsa			USETW(req.wValue, 0);
1236202181Sthompsa			USETW(req.wIndex, ht->ht_muxport);
1237202181Sthompsa			USETW(req.wLength, actlen);
1238202181Sthompsa
1239202181Sthompsa			pc = usbd_xfer_get_frame(xfer, 0);
1240202181Sthompsa			usbd_copy_in(pc, 0, &req, sizeof(req));
1241202181Sthompsa
1242202181Sthompsa			usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
1243202181Sthompsa			usbd_xfer_set_frame_len(xfer, 1, actlen);
1244202181Sthompsa			usbd_xfer_set_frames(xfer, 2);
1245202181Sthompsa
1246202181Sthompsa			UHSO_DPRINTF(3, "Prepared %d bytes for transmit "
1247202181Sthompsa			    "on muxport %d\n", actlen, ht->ht_muxport);
1248202181Sthompsa
1249202181Sthompsa			usbd_transfer_submit(xfer);
1250202181Sthompsa		}
1251202181Sthompsa		break;
1252202181Sthompsa	default:
1253202181Sthompsa		UHSO_DPRINTF(0, "error: %s\n", usbd_errstr(error));
1254202181Sthompsa		if (error == USB_ERR_CANCELLED)
1255202181Sthompsa			break;
1256202181Sthompsa		break;
1257202181Sthompsa	}
1258202181Sthompsa}
1259202181Sthompsa
1260202181Sthompsastatic int
1261202181Sthompsauhso_attach_bulkserial(struct uhso_softc *sc, struct usb_interface *iface,
1262202181Sthompsa    int type)
1263202181Sthompsa{
1264202181Sthompsa	usb_error_t uerr;
1265202181Sthompsa	int tty;
1266202181Sthompsa
1267202243Sthompsa	/* Try attaching RD/WR/INTR first */
1268202181Sthompsa	uerr = usbd_transfer_setup(sc->sc_udev,
1269202181Sthompsa	    &iface->idesc->bInterfaceNumber, sc->sc_xfer,
1270202181Sthompsa	    uhso_bs_config, UHSO_BULK_ENDPT_MAX, sc, &sc->sc_mtx);
1271202181Sthompsa	if (uerr) {
1272202181Sthompsa		/* Try only RD/WR */
1273202181Sthompsa		uerr = usbd_transfer_setup(sc->sc_udev,
1274202181Sthompsa		    &iface->idesc->bInterfaceNumber, sc->sc_xfer,
1275202181Sthompsa		    uhso_bs_config, UHSO_BULK_ENDPT_MAX - 1, sc, &sc->sc_mtx);
1276202181Sthompsa	}
1277202181Sthompsa	if (uerr) {
1278202181Sthompsa		UHSO_DPRINTF(0, "usbd_transfer_setup failed");
1279202181Sthompsa		return (-1);
1280202181Sthompsa	}
1281202181Sthompsa
1282202181Sthompsa	tty = uhso_alloc_tty(sc);
1283202181Sthompsa	if (tty < 0) {
1284202181Sthompsa		usbd_transfer_unsetup(sc->sc_xfer, UHSO_BULK_ENDPT_MAX);
1285202181Sthompsa		return (ENOMEM);
1286202181Sthompsa	}
1287202181Sthompsa
1288202181Sthompsa	sc->sc_tty[tty].ht_muxport = -1;
1289202181Sthompsa	return (0);
1290202181Sthompsa}
1291202181Sthompsa
1292202181Sthompsastatic void
1293202181Sthompsauhso_bs_read_callback(struct usb_xfer *xfer, usb_error_t error)
1294202181Sthompsa{
1295202181Sthompsa	struct uhso_softc *sc = usbd_xfer_softc(xfer);
1296202181Sthompsa	struct usb_page_cache *pc;
1297202181Sthompsa	int actlen;
1298202181Sthompsa
1299202181Sthompsa	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
1300202181Sthompsa
1301202181Sthompsa	UHSO_DPRINTF(3, "status %d, actlen=%d\n", USB_GET_STATE(xfer), actlen);
1302202181Sthompsa
1303202181Sthompsa	switch (USB_GET_STATE(xfer)) {
1304202181Sthompsa	case USB_ST_TRANSFERRED:
1305202181Sthompsa		pc = usbd_xfer_get_frame(xfer, 0);
1306202181Sthompsa		ucom_put_data(&sc->sc_ucom[0], pc, 0, actlen);
1307202181Sthompsa		/* FALLTHROUGH */
1308202181Sthompsa	case USB_ST_SETUP:
1309202181Sthompsatr_setup:
1310202181Sthompsa		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
1311202181Sthompsa		usbd_transfer_submit(xfer);
1312202181Sthompsa	break;
1313202181Sthompsa	default:
1314202181Sthompsa		UHSO_DPRINTF(0, "error: %s\n", usbd_errstr(error));
1315202181Sthompsa		if (error == USB_ERR_CANCELLED)
1316202181Sthompsa			break;
1317202181Sthompsa		usbd_xfer_set_stall(xfer);
1318202181Sthompsa		goto tr_setup;
1319202181Sthompsa	}
1320202181Sthompsa}
1321202181Sthompsa
1322202181Sthompsastatic void
1323202181Sthompsauhso_bs_write_callback(struct usb_xfer *xfer, usb_error_t error)
1324202181Sthompsa{
1325202181Sthompsa	struct uhso_softc *sc = usbd_xfer_softc(xfer);
1326202181Sthompsa	struct usb_page_cache *pc;
1327202181Sthompsa	int actlen;
1328202181Sthompsa
1329202181Sthompsa	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
1330202181Sthompsa
1331202181Sthompsa	UHSO_DPRINTF(3, "status %d, actlen=%d\n", USB_GET_STATE(xfer), actlen);
1332202181Sthompsa
1333202181Sthompsa	switch (USB_GET_STATE(xfer)) {
1334202181Sthompsa	case USB_ST_TRANSFERRED:
1335202181Sthompsa	case USB_ST_SETUP:
1336202181Sthompsatr_setup:
1337202181Sthompsa		pc = usbd_xfer_get_frame(xfer, 0);
1338202181Sthompsa		if (ucom_get_data(&sc->sc_ucom[0], pc, 0, 8192, &actlen)) {
1339202181Sthompsa			usbd_xfer_set_frame_len(xfer, 0, actlen);
1340202181Sthompsa			usbd_transfer_submit(xfer);
1341202181Sthompsa		}
1342202181Sthompsa		break;
1343202181Sthompsa	break;
1344202181Sthompsa	default:
1345202181Sthompsa		UHSO_DPRINTF(0, "error: %s\n", usbd_errstr(error));
1346202181Sthompsa		if (error == USB_ERR_CANCELLED)
1347202181Sthompsa			break;
1348202181Sthompsa		usbd_xfer_set_stall(xfer);
1349202181Sthompsa		goto tr_setup;
1350202181Sthompsa	}
1351202181Sthompsa}
1352202181Sthompsa
1353202181Sthompsastatic void
1354202181Sthompsauhso_bs_cfg(struct uhso_softc *sc)
1355202181Sthompsa{
1356202181Sthompsa	struct usb_device_request req;
1357202181Sthompsa	usb_error_t uerr;
1358202181Sthompsa
1359202181Sthompsa	if (!(UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_BULK))
1360202181Sthompsa		return;
1361202181Sthompsa
1362202181Sthompsa	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
1363202181Sthompsa	req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
1364202181Sthompsa	USETW(req.wValue, sc->sc_line);
1365202181Sthompsa	USETW(req.wIndex, sc->sc_iface_no);
1366202181Sthompsa	USETW(req.wLength, 0);
1367202181Sthompsa
1368202181Sthompsa	uerr = ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom[0], &req, NULL, 0, 1000);
1369202181Sthompsa	if (uerr != 0) {
1370202181Sthompsa		device_printf(sc->sc_dev, "failed to set ctrl line state to "
1371202181Sthompsa		    "0x%02x: %s\n", sc->sc_line, usbd_errstr(uerr));
1372202181Sthompsa	}
1373202181Sthompsa}
1374202181Sthompsa
1375202181Sthompsastatic void
1376202181Sthompsauhso_bs_intr_callback(struct usb_xfer *xfer, usb_error_t error)
1377202181Sthompsa{
1378202181Sthompsa	struct uhso_softc *sc = usbd_xfer_softc(xfer);
1379202181Sthompsa	struct usb_page_cache *pc;
1380202181Sthompsa	int actlen;
1381202181Sthompsa	struct usb_cdc_notification cdc;
1382202181Sthompsa
1383202181Sthompsa	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
1384202181Sthompsa	UHSO_DPRINTF(3, "status %d, actlen=%d\n", USB_GET_STATE(xfer), actlen);
1385202181Sthompsa
1386202181Sthompsa	switch (USB_GET_STATE(xfer)) {
1387202181Sthompsa	case USB_ST_TRANSFERRED:
1388202181Sthompsa		if (actlen < UCDC_NOTIFICATION_LENGTH) {
1389202181Sthompsa			UHSO_DPRINTF(0, "UCDC notification too short: %d\n", actlen);
1390202181Sthompsa			goto tr_setup;
1391202181Sthompsa		}
1392233774Shselasky		else if (actlen > (int)sizeof(struct usb_cdc_notification)) {
1393202181Sthompsa			UHSO_DPRINTF(0, "UCDC notification too large: %d\n", actlen);
1394202181Sthompsa			actlen = sizeof(struct usb_cdc_notification);
1395202181Sthompsa		}
1396202181Sthompsa
1397202181Sthompsa		pc = usbd_xfer_get_frame(xfer, 0);
1398202181Sthompsa		usbd_copy_out(pc, 0, &cdc, actlen);
1399202181Sthompsa
1400202181Sthompsa		if (UGETW(cdc.wIndex) != sc->sc_iface_no) {
1401202243Sthompsa			UHSO_DPRINTF(0, "Interface mismatch, got %d expected %d\n",
1402202181Sthompsa			    UGETW(cdc.wIndex), sc->sc_iface_no);
1403202181Sthompsa			goto tr_setup;
1404202181Sthompsa		}
1405202181Sthompsa
1406202181Sthompsa		if (cdc.bmRequestType == UCDC_NOTIFICATION &&
1407202181Sthompsa		    cdc.bNotification == UCDC_N_SERIAL_STATE) {
1408202243Sthompsa			UHSO_DPRINTF(2, "notify = 0x%02x\n", cdc.data[0]);
1409202181Sthompsa
1410202181Sthompsa			sc->sc_msr = 0;
1411202181Sthompsa			sc->sc_lsr = 0;
1412202181Sthompsa			if (cdc.data[0] & UCDC_N_SERIAL_RI)
1413202181Sthompsa				sc->sc_msr |= SER_RI;
1414202181Sthompsa			if (cdc.data[0] & UCDC_N_SERIAL_DSR)
1415202181Sthompsa				sc->sc_msr |= SER_DSR;
1416202181Sthompsa			if (cdc.data[0] & UCDC_N_SERIAL_DCD)
1417202181Sthompsa				sc->sc_msr |= SER_DCD;
1418202181Sthompsa
1419202181Sthompsa			ucom_status_change(&sc->sc_ucom[0]);
1420202181Sthompsa		}
1421202181Sthompsa	case USB_ST_SETUP:
1422202181Sthompsatr_setup:
1423202181Sthompsa	default:
1424202181Sthompsa		if (error == USB_ERR_CANCELLED)
1425202181Sthompsa			break;
1426202181Sthompsa		usbd_xfer_set_stall(xfer);
1427202181Sthompsa		goto tr_setup;
1428202181Sthompsa	}
1429202181Sthompsa}
1430202181Sthompsa
1431202181Sthompsastatic void
1432202181Sthompsauhso_ucom_cfg_get_status(struct ucom_softc *ucom, uint8_t *lsr, uint8_t *msr)
1433202181Sthompsa{
1434202181Sthompsa	struct uhso_softc *sc = ucom->sc_parent;
1435202181Sthompsa
1436202181Sthompsa	*lsr = sc->sc_lsr;
1437202181Sthompsa	*msr = sc->sc_msr;
1438202181Sthompsa}
1439202181Sthompsa
1440202181Sthompsastatic void
1441202181Sthompsauhso_ucom_cfg_set_dtr(struct ucom_softc *ucom, uint8_t onoff)
1442202181Sthompsa{
1443202181Sthompsa	struct uhso_softc *sc = ucom->sc_parent;
1444202181Sthompsa
1445202181Sthompsa	if (!(UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_BULK))
1446202181Sthompsa		return;
1447202181Sthompsa
1448202181Sthompsa	if (onoff)
1449202181Sthompsa		sc->sc_line |= UCDC_LINE_DTR;
1450202181Sthompsa	else
1451208017Sthompsa		sc->sc_line &= ~UCDC_LINE_DTR;
1452202181Sthompsa
1453202181Sthompsa	uhso_bs_cfg(sc);
1454202181Sthompsa}
1455202181Sthompsa
1456202181Sthompsastatic void
1457202181Sthompsauhso_ucom_cfg_set_rts(struct ucom_softc *ucom, uint8_t onoff)
1458202181Sthompsa{
1459202181Sthompsa	struct uhso_softc *sc = ucom->sc_parent;
1460202181Sthompsa
1461202181Sthompsa	if (!(UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_BULK))
1462202181Sthompsa		return;
1463202181Sthompsa
1464202181Sthompsa	if (onoff)
1465202181Sthompsa		sc->sc_line |= UCDC_LINE_RTS;
1466202181Sthompsa	else
1467208017Sthompsa		sc->sc_line &= ~UCDC_LINE_RTS;
1468202181Sthompsa
1469202181Sthompsa	uhso_bs_cfg(sc);
1470202181Sthompsa}
1471202181Sthompsa
1472202181Sthompsastatic void
1473202181Sthompsauhso_ucom_start_read(struct ucom_softc *ucom)
1474202181Sthompsa{
1475202181Sthompsa	struct uhso_softc *sc = ucom->sc_parent;
1476202181Sthompsa
1477214761Sn_hibma	UHSO_DPRINTF(3, "unit=%d, subunit=%d\n",
1478214761Sn_hibma	    ucom->sc_super->sc_unit, ucom->sc_subunit);
1479202181Sthompsa
1480202181Sthompsa	if (UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_MUX) {
1481214761Sn_hibma		sc->sc_tty[ucom->sc_subunit].ht_open = 1;
1482202181Sthompsa		usbd_transfer_start(sc->sc_xfer[UHSO_MUX_ENDPT_INTR]);
1483202181Sthompsa	}
1484202181Sthompsa	else if (UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_BULK) {
1485202181Sthompsa		sc->sc_tty[0].ht_open = 1;
1486202181Sthompsa		usbd_transfer_start(sc->sc_xfer[UHSO_BULK_ENDPT_READ]);
1487202181Sthompsa		if (sc->sc_xfer[UHSO_BULK_ENDPT_INTR] != NULL)
1488202181Sthompsa			usbd_transfer_start(sc->sc_xfer[UHSO_BULK_ENDPT_INTR]);
1489202181Sthompsa	}
1490202181Sthompsa}
1491202181Sthompsa
1492202181Sthompsastatic void
1493202181Sthompsauhso_ucom_stop_read(struct ucom_softc *ucom)
1494202181Sthompsa{
1495202181Sthompsa
1496202181Sthompsa	struct uhso_softc *sc = ucom->sc_parent;
1497202181Sthompsa
1498202181Sthompsa	if (UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_MUX) {
1499214761Sn_hibma		sc->sc_tty[ucom->sc_subunit].ht_open = 0;
1500202181Sthompsa		usbd_transfer_stop(
1501214761Sn_hibma		    sc->sc_tty[ucom->sc_subunit].ht_xfer[UHSO_CTRL_READ]);
1502202181Sthompsa	}
1503202181Sthompsa	else if (UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_BULK) {
1504202181Sthompsa		sc->sc_tty[0].ht_open = 0;
1505202181Sthompsa		usbd_transfer_start(sc->sc_xfer[UHSO_BULK_ENDPT_READ]);
1506202181Sthompsa		if (sc->sc_xfer[UHSO_BULK_ENDPT_INTR] != NULL)
1507202181Sthompsa			usbd_transfer_stop(sc->sc_xfer[UHSO_BULK_ENDPT_INTR]);
1508202181Sthompsa	}
1509202181Sthompsa}
1510202181Sthompsa
1511202181Sthompsastatic void
1512202181Sthompsauhso_ucom_start_write(struct ucom_softc *ucom)
1513202181Sthompsa{
1514202181Sthompsa	struct uhso_softc *sc = ucom->sc_parent;
1515202181Sthompsa
1516202181Sthompsa	if (UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_MUX) {
1517214761Sn_hibma		UHSO_DPRINTF(3, "local unit %d\n", ucom->sc_subunit);
1518202181Sthompsa
1519202181Sthompsa		usbd_transfer_start(sc->sc_xfer[UHSO_MUX_ENDPT_INTR]);
1520202181Sthompsa
1521202181Sthompsa		usbd_xfer_set_priv(
1522214761Sn_hibma		    sc->sc_tty[ucom->sc_subunit].ht_xfer[UHSO_CTRL_WRITE],
1523214761Sn_hibma		    &sc->sc_tty[ucom->sc_subunit]);
1524202181Sthompsa		usbd_transfer_start(
1525214761Sn_hibma		    sc->sc_tty[ucom->sc_subunit].ht_xfer[UHSO_CTRL_WRITE]);
1526202181Sthompsa
1527202181Sthompsa	}
1528202181Sthompsa	else if (UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_BULK) {
1529202181Sthompsa		usbd_transfer_start(sc->sc_xfer[UHSO_BULK_ENDPT_WRITE]);
1530202181Sthompsa	}
1531202181Sthompsa}
1532202181Sthompsa
1533202181Sthompsastatic void
1534202181Sthompsauhso_ucom_stop_write(struct ucom_softc *ucom)
1535202181Sthompsa{
1536202181Sthompsa	struct uhso_softc *sc = ucom->sc_parent;
1537202181Sthompsa
1538202181Sthompsa	if (UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_MUX) {
1539202181Sthompsa		usbd_transfer_stop(
1540214761Sn_hibma		    sc->sc_tty[ucom->sc_subunit].ht_xfer[UHSO_CTRL_WRITE]);
1541202181Sthompsa	}
1542202181Sthompsa	else if (UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_BULK) {
1543202181Sthompsa		usbd_transfer_stop(sc->sc_xfer[UHSO_BULK_ENDPT_WRITE]);
1544202181Sthompsa	}
1545202181Sthompsa}
1546202181Sthompsa
1547210275Sthompsastatic int
1548210275Sthompsauhso_attach_ifnet(struct uhso_softc *sc, struct usb_interface *iface, int type)
1549202181Sthompsa{
1550202181Sthompsa	struct ifnet *ifp;
1551202181Sthompsa	usb_error_t uerr;
1552202181Sthompsa	struct sysctl_ctx_list *sctx;
1553202181Sthompsa	struct sysctl_oid *soid;
1554210275Sthompsa	unsigned int devunit;
1555202181Sthompsa
1556202181Sthompsa	uerr = usbd_transfer_setup(sc->sc_udev,
1557202181Sthompsa	    &iface->idesc->bInterfaceNumber, sc->sc_if_xfer,
1558202181Sthompsa	    uhso_ifnet_config, UHSO_IFNET_MAX, sc, &sc->sc_mtx);
1559202181Sthompsa	if (uerr) {
1560202181Sthompsa		UHSO_DPRINTF(0, "usbd_transfer_setup failed: %s\n",
1561202181Sthompsa		    usbd_errstr(uerr));
1562202181Sthompsa		return (-1);
1563202181Sthompsa	}
1564202181Sthompsa
1565202243Sthompsa	sc->sc_ifp = ifp = if_alloc(IFT_OTHER);
1566202181Sthompsa	if (sc->sc_ifp == NULL) {
1567202181Sthompsa		device_printf(sc->sc_dev, "if_alloc() failed\n");
1568202181Sthompsa		return (-1);
1569202181Sthompsa	}
1570202181Sthompsa
1571202181Sthompsa	callout_init_mtx(&sc->sc_c, &sc->sc_mtx, 0);
1572202181Sthompsa	mtx_lock(&sc->sc_mtx);
1573202181Sthompsa	callout_reset(&sc->sc_c, 1, uhso_if_rxflush, sc);
1574202181Sthompsa	mtx_unlock(&sc->sc_mtx);
1575202181Sthompsa
1576210275Sthompsa	/*
1577210275Sthompsa	 * We create our own unit numbers for ifnet devices because the
1578210275Sthompsa	 * USB interface unit numbers can be at arbitrary positions yielding
1579210275Sthompsa	 * odd looking device names.
1580210275Sthompsa	 */
1581210275Sthompsa	devunit = alloc_unr(uhso_ifnet_unit);
1582210275Sthompsa
1583210275Sthompsa	if_initname(ifp, device_get_name(sc->sc_dev), devunit);
1584202181Sthompsa	ifp->if_mtu = UHSO_MAX_MTU;
1585202181Sthompsa	ifp->if_ioctl = uhso_if_ioctl;
1586202181Sthompsa	ifp->if_init = uhso_if_init;
1587202181Sthompsa	ifp->if_start = uhso_if_start;
1588202181Sthompsa	ifp->if_output = uhso_if_output;
1589213803Shselasky	ifp->if_flags = IFF_BROADCAST | IFF_MULTICAST | IFF_NOARP;
1590202181Sthompsa	ifp->if_softc = sc;
1591207554Ssobomax	IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
1592207554Ssobomax	ifp->if_snd.ifq_drv_maxlen = ifqmaxlen;
1593202181Sthompsa	IFQ_SET_READY(&ifp->if_snd);
1594202181Sthompsa
1595202181Sthompsa	if_attach(ifp);
1596202181Sthompsa	bpfattach(ifp, DLT_RAW, 0);
1597202181Sthompsa
1598202181Sthompsa	sctx = device_get_sysctl_ctx(sc->sc_dev);
1599202181Sthompsa	soid = device_get_sysctl_tree(sc->sc_dev);
1600202181Sthompsa	/* Unlocked read... */
1601202181Sthompsa	SYSCTL_ADD_STRING(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "netif",
1602202181Sthompsa	    CTLFLAG_RD, ifp->if_xname, 0, "Attached network interface");
1603202181Sthompsa
1604202181Sthompsa	return (0);
1605202181Sthompsa}
1606202181Sthompsa
1607202181Sthompsastatic void
1608202181Sthompsauhso_ifnet_read_callback(struct usb_xfer *xfer, usb_error_t error)
1609202181Sthompsa{
1610202181Sthompsa	struct uhso_softc *sc = usbd_xfer_softc(xfer);
1611202181Sthompsa	struct mbuf *m;
1612202181Sthompsa	struct usb_page_cache *pc;
1613202181Sthompsa	int actlen;
1614202181Sthompsa
1615202181Sthompsa	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
1616202181Sthompsa
1617202181Sthompsa	UHSO_DPRINTF(3, "status=%d, actlen=%d\n", USB_GET_STATE(xfer), actlen);
1618202181Sthompsa
1619202181Sthompsa	switch (USB_GET_STATE(xfer)) {
1620202181Sthompsa	case USB_ST_TRANSFERRED:
1621202181Sthompsa		if (actlen > 0 && (sc->sc_ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1622202181Sthompsa			pc = usbd_xfer_get_frame(xfer, 0);
1623243857Sglebius			m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
1624202181Sthompsa			usbd_copy_out(pc, 0, mtod(m, uint8_t *), actlen);
1625202181Sthompsa			m->m_pkthdr.len = m->m_len = actlen;
1626202243Sthompsa			/* Enqueue frame for further processing */
1627202181Sthompsa			_IF_ENQUEUE(&sc->sc_rxq, m);
1628202181Sthompsa			if (!callout_pending(&sc->sc_c) ||
1629202181Sthompsa			    !callout_active(&sc->sc_c)) {
1630202181Sthompsa				callout_schedule(&sc->sc_c, 1);
1631202181Sthompsa			}
1632202181Sthompsa		}
1633202181Sthompsa	/* FALLTHROUGH */
1634202181Sthompsa	case USB_ST_SETUP:
1635202181Sthompsatr_setup:
1636202181Sthompsa		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
1637202181Sthompsa		usbd_transfer_submit(xfer);
1638202181Sthompsa		break;
1639202181Sthompsa	default:
1640202181Sthompsa		UHSO_DPRINTF(0, "error: %s\n", usbd_errstr(error));
1641202181Sthompsa		if (error == USB_ERR_CANCELLED)
1642202181Sthompsa			break;
1643202181Sthompsa		usbd_xfer_set_stall(xfer);
1644202181Sthompsa		goto tr_setup;
1645202181Sthompsa	}
1646202181Sthompsa}
1647202181Sthompsa
1648202181Sthompsa/*
1649202243Sthompsa * Deferred RX processing, called with mutex locked.
1650202243Sthompsa *
1651202270Sthompsa * Each frame we receive might contain several small ip-packets as well
1652202243Sthompsa * as partial ip-packets. We need to separate/assemble them into individual
1653202243Sthompsa * packets before sending them to the ip-layer.
1654202181Sthompsa */
1655202181Sthompsastatic void
1656202181Sthompsauhso_if_rxflush(void *arg)
1657202181Sthompsa{
1658202181Sthompsa	struct uhso_softc *sc = arg;
1659202181Sthompsa	struct ifnet *ifp = sc->sc_ifp;
1660202181Sthompsa	uint8_t *cp;
1661202181Sthompsa	struct mbuf *m, *m0, *mwait;
1662202181Sthompsa	struct ip *ip;
1663202181Sthompsa#ifdef INET6
1664202181Sthompsa	struct ip6_hdr *ip6;
1665202181Sthompsa#endif
1666202181Sthompsa	uint16_t iplen;
1667202181Sthompsa	int len, isr;
1668202181Sthompsa
1669202181Sthompsa	m = NULL;
1670202181Sthompsa	mwait = sc->sc_mwait;
1671202181Sthompsa	for (;;) {
1672202181Sthompsa		if (m == NULL) {
1673202181Sthompsa			_IF_DEQUEUE(&sc->sc_rxq, m);
1674202181Sthompsa			if (m == NULL)
1675202181Sthompsa				break;
1676202243Sthompsa			UHSO_DPRINTF(3, "dequeue m=%p, len=%d\n", m, m->m_len);
1677202181Sthompsa		}
1678202181Sthompsa		mtx_unlock(&sc->sc_mtx);
1679202181Sthompsa
1680202181Sthompsa		/* Do we have a partial packet waiting? */
1681202181Sthompsa		if (mwait != NULL) {
1682202181Sthompsa			m0 = mwait;
1683202181Sthompsa			mwait = NULL;
1684202181Sthompsa
1685202243Sthompsa			UHSO_DPRINTF(3, "partial m0=%p(%d), concat w/ m=%p(%d)\n",
1686202181Sthompsa			    m0, m0->m_len, m, m->m_len);
1687202181Sthompsa			len = m->m_len + m0->m_len;
1688202181Sthompsa
1689202181Sthompsa			/* Concat mbufs and fix headers */
1690202181Sthompsa			m_cat(m0, m);
1691202181Sthompsa			m0->m_pkthdr.len = len;
1692202181Sthompsa			m->m_flags &= ~M_PKTHDR;
1693202181Sthompsa
1694202181Sthompsa			m = m_pullup(m0, sizeof(struct ip));
1695202181Sthompsa			if (m == NULL) {
1696202181Sthompsa				ifp->if_ierrors++;
1697202181Sthompsa				UHSO_DPRINTF(0, "m_pullup failed\n");
1698202181Sthompsa				mtx_lock(&sc->sc_mtx);
1699202181Sthompsa				continue;
1700202181Sthompsa			}
1701202243Sthompsa			UHSO_DPRINTF(3, "Constructed mbuf=%p, len=%d\n",
1702202181Sthompsa			    m, m->m_pkthdr.len);
1703202181Sthompsa		}
1704202181Sthompsa
1705202181Sthompsa		cp = mtod(m, uint8_t *);
1706202181Sthompsa		ip = (struct ip *)cp;
1707202181Sthompsa#ifdef INET6
1708202181Sthompsa		ip6 = (struct ip6_hdr *)cp;
1709202181Sthompsa#endif
1710202181Sthompsa
1711202181Sthompsa		/* Check for IPv4 */
1712202181Sthompsa		if (ip->ip_v == IPVERSION) {
1713202181Sthompsa			iplen = htons(ip->ip_len);
1714202181Sthompsa			isr = NETISR_IP;
1715202181Sthompsa		}
1716202181Sthompsa#ifdef INET6
1717202181Sthompsa		/* Check for IPv6 */
1718202181Sthompsa		else if ((ip6->ip6_vfc & IPV6_VERSION_MASK) == IPV6_VERSION) {
1719202181Sthompsa			iplen = htons(ip6->ip6_plen);
1720202181Sthompsa			isr = NETISR_IPV6;
1721202181Sthompsa		}
1722202181Sthompsa#endif
1723202181Sthompsa		else {
1724202181Sthompsa			UHSO_DPRINTF(0, "got unexpected ip version %d, "
1725202181Sthompsa			    "m=%p, len=%d\n", (*cp & 0xf0) >> 4, m, m->m_len);
1726202181Sthompsa			ifp->if_ierrors++;
1727202181Sthompsa			UHSO_HEXDUMP(cp, 4);
1728202181Sthompsa			m_freem(m);
1729202181Sthompsa			m = NULL;
1730202181Sthompsa			mtx_lock(&sc->sc_mtx);
1731202181Sthompsa			continue;
1732202181Sthompsa		}
1733202181Sthompsa
1734202181Sthompsa		if (iplen == 0) {
1735202181Sthompsa			UHSO_DPRINTF(0, "Zero IP length\n");
1736202181Sthompsa			ifp->if_ierrors++;
1737202181Sthompsa			m_freem(m);
1738202181Sthompsa			m = NULL;
1739202181Sthompsa			mtx_lock(&sc->sc_mtx);
1740202181Sthompsa			continue;
1741202181Sthompsa		}
1742202181Sthompsa
1743202243Sthompsa		UHSO_DPRINTF(3, "m=%p, len=%d, cp=%p, iplen=%d\n",
1744202181Sthompsa		    m, m->m_pkthdr.len, cp, iplen);
1745202181Sthompsa
1746202181Sthompsa		m0 = NULL;
1747202181Sthompsa
1748202181Sthompsa		/* More IP packets in this mbuf */
1749202181Sthompsa		if (iplen < m->m_pkthdr.len) {
1750202181Sthompsa			m0 = m;
1751202181Sthompsa
1752202181Sthompsa			/*
1753202181Sthompsa			 * Allocate a new mbuf for this IP packet and
1754202181Sthompsa			 * copy the IP-packet into it.
1755202181Sthompsa			 */
1756243857Sglebius			m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
1757227461Shselasky			memcpy(mtod(m, uint8_t *), mtod(m0, uint8_t *), iplen);
1758202181Sthompsa			m->m_pkthdr.len = m->m_len = iplen;
1759202181Sthompsa
1760202181Sthompsa			/* Adjust the size of the original mbuf */
1761202181Sthompsa			m_adj(m0, iplen);
1762243857Sglebius			m0 = m_defrag(m0, M_WAITOK);
1763202181Sthompsa
1764202243Sthompsa			UHSO_DPRINTF(3, "New mbuf=%p, len=%d/%d, m0=%p, "
1765202181Sthompsa			    "m0_len=%d/%d\n", m, m->m_pkthdr.len, m->m_len,
1766202181Sthompsa			    m0, m0->m_pkthdr.len, m0->m_len);
1767202181Sthompsa		}
1768202181Sthompsa		else if (iplen > m->m_pkthdr.len) {
1769202243Sthompsa			UHSO_DPRINTF(3, "Deferred mbuf=%p, len=%d\n",
1770202181Sthompsa			    m, m->m_pkthdr.len);
1771202181Sthompsa			mwait = m;
1772202181Sthompsa			m = NULL;
1773202181Sthompsa			mtx_lock(&sc->sc_mtx);
1774202181Sthompsa			continue;
1775202181Sthompsa		}
1776202181Sthompsa
1777202181Sthompsa		ifp->if_ipackets++;
1778202181Sthompsa		m->m_pkthdr.rcvif = ifp;
1779202181Sthompsa
1780202181Sthompsa		/* Dispatch to IP layer */
1781202181Sthompsa		BPF_MTAP(sc->sc_ifp, m);
1782223741Sbz		M_SETFIB(m, ifp->if_fib);
1783202181Sthompsa		netisr_dispatch(isr, m);
1784202181Sthompsa		m = m0 != NULL ? m0 : NULL;
1785202181Sthompsa		mtx_lock(&sc->sc_mtx);
1786202181Sthompsa	}
1787202181Sthompsa	sc->sc_mwait = mwait;
1788202181Sthompsa}
1789202181Sthompsa
1790202181Sthompsastatic void
1791202181Sthompsauhso_ifnet_write_callback(struct usb_xfer *xfer, usb_error_t error)
1792202181Sthompsa{
1793202181Sthompsa	struct uhso_softc *sc = usbd_xfer_softc(xfer);
1794202181Sthompsa	struct ifnet *ifp = sc->sc_ifp;
1795202181Sthompsa	struct usb_page_cache *pc;
1796202181Sthompsa	struct mbuf *m;
1797202181Sthompsa	int actlen;
1798202181Sthompsa
1799202181Sthompsa	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
1800202181Sthompsa
1801202181Sthompsa	UHSO_DPRINTF(3, "status %d, actlen=%d\n", USB_GET_STATE(xfer), actlen);
1802202181Sthompsa
1803202181Sthompsa	switch (USB_GET_STATE(xfer)) {
1804202181Sthompsa	case USB_ST_TRANSFERRED:
1805202181Sthompsa		ifp->if_opackets++;
1806202181Sthompsa		ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1807202181Sthompsa	case USB_ST_SETUP:
1808202181Sthompsatr_setup:
1809202181Sthompsa		IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
1810202181Sthompsa		if (m == NULL)
1811202181Sthompsa			break;
1812202181Sthompsa
1813202181Sthompsa		ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1814202181Sthompsa
1815202181Sthompsa		if (m->m_pkthdr.len > MCLBYTES)
1816202181Sthompsa			m->m_pkthdr.len = MCLBYTES;
1817202181Sthompsa
1818202181Sthompsa		usbd_xfer_set_frame_len(xfer, 0, m->m_pkthdr.len);
1819202181Sthompsa		pc = usbd_xfer_get_frame(xfer, 0);
1820202181Sthompsa		usbd_m_copy_in(pc, 0, m, 0, m->m_pkthdr.len);
1821202181Sthompsa		usbd_transfer_submit(xfer);
1822202181Sthompsa
1823202181Sthompsa		BPF_MTAP(ifp, m);
1824202181Sthompsa		m_freem(m);
1825202181Sthompsa		break;
1826202181Sthompsa	default:
1827202181Sthompsa		UHSO_DPRINTF(0, "error: %s\n", usbd_errstr(error));
1828202181Sthompsa		if (error == USB_ERR_CANCELLED)
1829202181Sthompsa			break;
1830202181Sthompsa		usbd_xfer_set_stall(xfer);
1831202181Sthompsa		goto tr_setup;
1832202181Sthompsa	}
1833202181Sthompsa}
1834202181Sthompsa
1835202181Sthompsastatic int
1836202181Sthompsauhso_if_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1837202181Sthompsa{
1838202181Sthompsa	struct uhso_softc *sc;
1839202181Sthompsa
1840202181Sthompsa	sc = ifp->if_softc;
1841202181Sthompsa
1842202181Sthompsa	switch (cmd) {
1843202181Sthompsa	case SIOCSIFFLAGS:
1844202181Sthompsa		if (ifp->if_flags & IFF_UP) {
1845202181Sthompsa			if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1846202181Sthompsa				uhso_if_init(sc);
1847202181Sthompsa			}
1848202181Sthompsa		}
1849202181Sthompsa		else {
1850202181Sthompsa			if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1851202181Sthompsa				mtx_lock(&sc->sc_mtx);
1852202181Sthompsa				uhso_if_stop(sc);
1853202181Sthompsa				mtx_unlock(&sc->sc_mtx);
1854202181Sthompsa			}
1855202181Sthompsa		}
1856202181Sthompsa		break;
1857202181Sthompsa	case SIOCSIFADDR:
1858202181Sthompsa	case SIOCADDMULTI:
1859202181Sthompsa	case SIOCDELMULTI:
1860202181Sthompsa		break;
1861202181Sthompsa	default:
1862202181Sthompsa		return (EINVAL);
1863202181Sthompsa	}
1864202181Sthompsa	return (0);
1865202181Sthompsa}
1866202181Sthompsa
1867202181Sthompsastatic void
1868202181Sthompsauhso_if_init(void *priv)
1869202181Sthompsa{
1870202181Sthompsa	struct uhso_softc *sc = priv;
1871202181Sthompsa	struct ifnet *ifp = sc->sc_ifp;
1872202181Sthompsa
1873202181Sthompsa	mtx_lock(&sc->sc_mtx);
1874202181Sthompsa	uhso_if_stop(sc);
1875202181Sthompsa	ifp = sc->sc_ifp;
1876202181Sthompsa	ifp->if_flags |= IFF_UP;
1877202181Sthompsa	ifp->if_drv_flags |= IFF_DRV_RUNNING;
1878202181Sthompsa	mtx_unlock(&sc->sc_mtx);
1879202181Sthompsa
1880202243Sthompsa	UHSO_DPRINTF(2, "ifnet initialized\n");
1881202181Sthompsa}
1882202181Sthompsa
1883202181Sthompsastatic int
1884249925Sglebiusuhso_if_output(struct ifnet *ifp, struct mbuf *m0, const struct sockaddr *dst,
1885202181Sthompsa    struct route *ro)
1886202181Sthompsa{
1887202181Sthompsa	int error;
1888202181Sthompsa
1889202181Sthompsa	/* Only IPv4/6 support */
1890202181Sthompsa	if (dst->sa_family != AF_INET
1891202181Sthompsa#ifdef INET6
1892202181Sthompsa	   && dst->sa_family != AF_INET6
1893202181Sthompsa#endif
1894202181Sthompsa	 ) {
1895202181Sthompsa		return (EAFNOSUPPORT);
1896202181Sthompsa	}
1897202181Sthompsa
1898202181Sthompsa	error = (ifp->if_transmit)(ifp, m0);
1899202181Sthompsa	if (error) {
1900202181Sthompsa		ifp->if_oerrors++;
1901202181Sthompsa		return (ENOBUFS);
1902202181Sthompsa	}
1903202181Sthompsa	ifp->if_opackets++;
1904202181Sthompsa	return (0);
1905202181Sthompsa}
1906202181Sthompsa
1907202181Sthompsastatic void
1908202181Sthompsauhso_if_start(struct ifnet *ifp)
1909202181Sthompsa{
1910202181Sthompsa	struct uhso_softc *sc = ifp->if_softc;
1911202181Sthompsa
1912202181Sthompsa	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
1913202181Sthompsa		UHSO_DPRINTF(1, "Not running\n");
1914202181Sthompsa		return;
1915202181Sthompsa	}
1916202181Sthompsa
1917202181Sthompsa	mtx_lock(&sc->sc_mtx);
1918202181Sthompsa	usbd_transfer_start(sc->sc_if_xfer[UHSO_IFNET_READ]);
1919202181Sthompsa	usbd_transfer_start(sc->sc_if_xfer[UHSO_IFNET_WRITE]);
1920202181Sthompsa	mtx_unlock(&sc->sc_mtx);
1921202181Sthompsa	UHSO_DPRINTF(3, "interface started\n");
1922202181Sthompsa}
1923202181Sthompsa
1924202181Sthompsastatic void
1925202181Sthompsauhso_if_stop(struct uhso_softc *sc)
1926202181Sthompsa{
1927202181Sthompsa
1928202181Sthompsa	usbd_transfer_stop(sc->sc_if_xfer[UHSO_IFNET_READ]);
1929202181Sthompsa	usbd_transfer_stop(sc->sc_if_xfer[UHSO_IFNET_WRITE]);
1930202181Sthompsa	sc->sc_ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
1931202181Sthompsa}
1932