uhso.c revision 239180
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 239180 2012-08-10 15:29:41Z hselasky $");
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>
50202181Sthompsa#include <net/if_types.h>
51202181Sthompsa#include <net/netisr.h>
52202181Sthompsa#include <net/bpf.h>
53202181Sthompsa#include <netinet/in.h>
54202181Sthompsa#include <netinet/ip.h>
55202181Sthompsa#include <netinet/ip6.h>
56202181Sthompsa
57202181Sthompsa#include <dev/usb/usb.h>
58202181Sthompsa#include <dev/usb/usbdi.h>
59202181Sthompsa#include <dev/usb/usbdi_util.h>
60202181Sthompsa#include <dev/usb/usb_cdc.h>
61202181Sthompsa#include "usbdevs.h"
62202181Sthompsa#define USB_DEBUG_VAR uhso_debug
63202181Sthompsa#include <dev/usb/usb_debug.h>
64202181Sthompsa#include <dev/usb/usb_process.h>
65202181Sthompsa#include <dev/usb/usb_busdma.h>
66202181Sthompsa#include <dev/usb/usb_msctest.h>
67202181Sthompsa
68212136Sthompsa#include <dev/usb/serial/usb_serial.h>
69212136Sthompsa
70202181Sthompsastruct uhso_tty {
71202181Sthompsa	struct uhso_softc *ht_sc;
72202181Sthompsa	struct usb_xfer	*ht_xfer[3];
73202243Sthompsa	int		ht_muxport; /* Mux. port no */
74202181Sthompsa	int		ht_open;
75202181Sthompsa	char		ht_name[32];
76202181Sthompsa};
77202181Sthompsa
78202181Sthompsastruct uhso_softc {
79202181Sthompsa	device_t		sc_dev;
80202181Sthompsa	struct usb_device	*sc_udev;
81202181Sthompsa	struct mtx		sc_mtx;
82202243Sthompsa	uint32_t		sc_type;	/* Interface definition */
83210275Sthompsa	int			sc_radio;
84202181Sthompsa
85202181Sthompsa	struct usb_xfer		*sc_xfer[3];
86202181Sthompsa	uint8_t			sc_iface_no;
87202181Sthompsa	uint8_t			sc_iface_index;
88202181Sthompsa
89202181Sthompsa	/* Control pipe */
90202181Sthompsa	struct usb_xfer	*	sc_ctrl_xfer[2];
91202181Sthompsa	uint8_t			sc_ctrl_iface_no;
92202181Sthompsa
93202181Sthompsa	/* Network */
94202181Sthompsa	struct usb_xfer		*sc_if_xfer[2];
95202181Sthompsa	struct ifnet		*sc_ifp;
96202243Sthompsa	struct mbuf		*sc_mwait;	/* Partial packet */
97202243Sthompsa	size_t			sc_waitlen;	/* No. of outstanding bytes */
98202181Sthompsa	struct ifqueue		sc_rxq;
99202181Sthompsa	struct callout		sc_c;
100202181Sthompsa
101202181Sthompsa	/* TTY related structures */
102202181Sthompsa	struct ucom_super_softc sc_super_ucom;
103202243Sthompsa	int			sc_ttys;
104202181Sthompsa	struct uhso_tty		*sc_tty;
105202181Sthompsa	struct ucom_softc	*sc_ucom;
106202181Sthompsa	int			sc_msr;
107202181Sthompsa	int			sc_lsr;
108202181Sthompsa	int			sc_line;
109202181Sthompsa};
110202181Sthompsa
111202181Sthompsa#define UHSO_MAX_MTU		2048
112202181Sthompsa
113202181Sthompsa/*
114202181Sthompsa * There are mainly two type of cards floating around.
115202181Sthompsa * The first one has 2,3 or 4 interfaces with a multiplexed serial port
116202181Sthompsa * and packet interface on the first interface and bulk serial ports
117202181Sthompsa * on the others.
118202181Sthompsa * The second type of card has several other interfaces, their purpose
119202181Sthompsa * can be detected during run-time.
120202181Sthompsa */
121202181Sthompsa#define UHSO_IFACE_SPEC(usb_type, port, port_type) \
122202181Sthompsa	(((usb_type) << 24) | ((port) << 16) | (port_type))
123202181Sthompsa
124202181Sthompsa#define UHSO_IFACE_USB_TYPE(x) ((x >> 24) & 0xff)
125202181Sthompsa#define UHSO_IFACE_PORT(x) ((x >> 16) & 0xff)
126202181Sthompsa#define UHSO_IFACE_PORT_TYPE(x) (x & 0xff)
127202181Sthompsa
128202181Sthompsa/*
129202181Sthompsa * USB interface types
130202181Sthompsa */
131202181Sthompsa#define UHSO_IF_NET		0x01	/* Network packet interface */
132202181Sthompsa#define UHSO_IF_MUX		0x02	/* Multiplexed serial port */
133202181Sthompsa#define UHSO_IF_BULK		0x04	/* Bulk interface */
134202181Sthompsa
135202181Sthompsa/*
136202181Sthompsa * Port types
137202181Sthompsa */
138202181Sthompsa#define UHSO_PORT_UNKNOWN	0x00
139202243Sthompsa#define UHSO_PORT_SERIAL	0x01	/* Serial port */
140202181Sthompsa#define UHSO_PORT_NETWORK	0x02	/* Network packet interface */
141202181Sthompsa
142202181Sthompsa/*
143202181Sthompsa * Multiplexed serial port destination sub-port names
144202181Sthompsa */
145202181Sthompsa#define UHSO_MPORT_TYPE_CTL	0x00	/* Control port */
146202181Sthompsa#define UHSO_MPORT_TYPE_APP	0x01	/* Application */
147202181Sthompsa#define UHSO_MPORT_TYPE_PCSC	0x02
148202181Sthompsa#define UHSO_MPORT_TYPE_GPS	0x03
149202243Sthompsa#define UHSO_MPORT_TYPE_APP2	0x04	/* Secondary application */
150202181Sthompsa#define UHSO_MPORT_TYPE_MAX	UHSO_MPORT_TYPE_APP2
151202181Sthompsa#define UHSO_MPORT_TYPE_NOMAX	8	/* Max number of mux ports */
152202181Sthompsa
153202181Sthompsa/*
154202181Sthompsa * Port definitions
155202270Sthompsa * Note that these definitions are arbitrary and do not match the values
156202270Sthompsa * returned by the auto config descriptor.
157202181Sthompsa */
158210275Sthompsa#define UHSO_PORT_TYPE_UNKNOWN	0x00
159202181Sthompsa#define UHSO_PORT_TYPE_CTL	0x01
160202181Sthompsa#define UHSO_PORT_TYPE_APP	0x02
161202181Sthompsa#define UHSO_PORT_TYPE_APP2	0x03
162202181Sthompsa#define UHSO_PORT_TYPE_MODEM	0x04
163202181Sthompsa#define UHSO_PORT_TYPE_NETWORK	0x05
164202181Sthompsa#define UHSO_PORT_TYPE_DIAG	0x06
165202181Sthompsa#define UHSO_PORT_TYPE_DIAG2	0x07
166202181Sthompsa#define UHSO_PORT_TYPE_GPS	0x08
167202181Sthompsa#define UHSO_PORT_TYPE_GPSCTL	0x09
168202181Sthompsa#define UHSO_PORT_TYPE_PCSC	0x0a
169202181Sthompsa#define UHSO_PORT_TYPE_MSD	0x0b
170202181Sthompsa#define UHSO_PORT_TYPE_VOICE	0x0c
171202181Sthompsa#define UHSO_PORT_TYPE_MAX	0x0c
172202181Sthompsa
173202181Sthompsastatic eventhandler_tag uhso_etag;
174202181Sthompsa
175202181Sthompsa/* Overall port type */
176202181Sthompsastatic char *uhso_port[] = {
177202181Sthompsa	"Unknown",
178202181Sthompsa	"Serial",
179202181Sthompsa	"Network",
180202181Sthompsa	"Network/Serial"
181202181Sthompsa};
182202181Sthompsa
183202243Sthompsa/*
184202243Sthompsa * Map between interface port type read from device and description type.
185202243Sthompsa * The position in this array is a direct map to the auto config
186202243Sthompsa * descriptor values.
187202243Sthompsa */
188202243Sthompsastatic unsigned char uhso_port_map[] = {
189210275Sthompsa	UHSO_PORT_TYPE_UNKNOWN,
190202181Sthompsa	UHSO_PORT_TYPE_DIAG,
191202181Sthompsa	UHSO_PORT_TYPE_GPS,
192202181Sthompsa	UHSO_PORT_TYPE_GPSCTL,
193202181Sthompsa	UHSO_PORT_TYPE_APP,
194202181Sthompsa	UHSO_PORT_TYPE_APP2,
195202181Sthompsa	UHSO_PORT_TYPE_CTL,
196202181Sthompsa	UHSO_PORT_TYPE_NETWORK,
197202181Sthompsa	UHSO_PORT_TYPE_MODEM,
198202181Sthompsa	UHSO_PORT_TYPE_MSD,
199202181Sthompsa	UHSO_PORT_TYPE_PCSC,
200202181Sthompsa	UHSO_PORT_TYPE_VOICE
201202181Sthompsa};
202202181Sthompsastatic char uhso_port_map_max = sizeof(uhso_port_map) / sizeof(char);
203202181Sthompsa
204202243Sthompsastatic unsigned char uhso_mux_port_map[] = {
205202181Sthompsa	UHSO_PORT_TYPE_CTL,
206202181Sthompsa	UHSO_PORT_TYPE_APP,
207202181Sthompsa	UHSO_PORT_TYPE_PCSC,
208202181Sthompsa	UHSO_PORT_TYPE_GPS,
209202181Sthompsa	UHSO_PORT_TYPE_APP2
210202181Sthompsa};
211202181Sthompsa
212202181Sthompsastatic char *uhso_port_type[] = {
213202243Sthompsa	"Unknown",  /* Not a valid port */
214202181Sthompsa	"Control",
215202181Sthompsa	"Application",
216202181Sthompsa	"Application (Secondary)",
217202181Sthompsa	"Modem",
218202181Sthompsa	"Network",
219202181Sthompsa	"Diagnostic",
220202181Sthompsa	"Diagnostic (Secondary)",
221202181Sthompsa	"GPS",
222202181Sthompsa	"GPS Control",
223202181Sthompsa	"PC Smartcard",
224202181Sthompsa	"MSD",
225202181Sthompsa	"Voice",
226202181Sthompsa};
227202181Sthompsa
228202181Sthompsastatic char *uhso_port_type_sysctl[] = {
229202181Sthompsa	"unknown",
230202181Sthompsa	"control",
231202181Sthompsa	"application",
232202181Sthompsa	"application",
233202181Sthompsa	"modem",
234202181Sthompsa	"network",
235202181Sthompsa	"diagnostic",
236202181Sthompsa	"diagnostic",
237202181Sthompsa	"gps",
238202181Sthompsa	"gps_control",
239202181Sthompsa	"pcsc",
240202181Sthompsa	"msd",
241202181Sthompsa	"voice",
242202181Sthompsa};
243202181Sthompsa
244202181Sthompsa#define UHSO_STATIC_IFACE	0x01
245202181Sthompsa#define UHSO_AUTO_IFACE		0x02
246202181Sthompsa
247210275Sthompsa/* ifnet device unit allocations */
248210275Sthompsastatic struct unrhdr *uhso_ifnet_unit = NULL;
249210275Sthompsa
250223486Shselaskystatic const STRUCT_USB_HOST_ID uhso_devs[] = {
251202181Sthompsa#define	UHSO_DEV(v,p,i) { USB_VPI(USB_VENDOR_##v, USB_PRODUCT_##v##_##p, i) }
252225037Shselasky	/* Option GlobeTrotter MAX 7.2 with upgraded firmware */
253225037Shselasky	UHSO_DEV(OPTION, GTMAX72, UHSO_STATIC_IFACE),
254202181Sthompsa	/* Option GlobeSurfer iCON 7.2 */
255202181Sthompsa	UHSO_DEV(OPTION, GSICON72, UHSO_STATIC_IFACE),
256202181Sthompsa	/* Option iCON 225 */
257202181Sthompsa	UHSO_DEV(OPTION, GTHSDPA, UHSO_STATIC_IFACE),
258202181Sthompsa	/* Option GlobeSurfer iCON HSUPA */
259202181Sthompsa	UHSO_DEV(OPTION, GSICONHSUPA, UHSO_STATIC_IFACE),
260202181Sthompsa	/* Option GlobeTrotter HSUPA */
261202181Sthompsa	UHSO_DEV(OPTION, GTHSUPA, UHSO_STATIC_IFACE),
262202181Sthompsa	/* GE40x */
263202181Sthompsa	UHSO_DEV(OPTION, GE40X, UHSO_AUTO_IFACE),
264202181Sthompsa	UHSO_DEV(OPTION, GE40X_1, UHSO_AUTO_IFACE),
265202181Sthompsa	UHSO_DEV(OPTION, GE40X_2, UHSO_AUTO_IFACE),
266202181Sthompsa	UHSO_DEV(OPTION, GE40X_3, UHSO_AUTO_IFACE),
267202181Sthompsa	/* Option GlobeSurfer iCON 401 */
268202181Sthompsa	UHSO_DEV(OPTION, ICON401, UHSO_AUTO_IFACE),
269202181Sthompsa	/* Option GlobeTrotter Module 382 */
270202181Sthompsa	UHSO_DEV(OPTION, GMT382, UHSO_AUTO_IFACE),
271202181Sthompsa	/* Option iCON EDGE */
272202181Sthompsa	UHSO_DEV(OPTION, ICONEDGE, UHSO_STATIC_IFACE),
273202181Sthompsa	/* Option Module HSxPA */
274202181Sthompsa	UHSO_DEV(OPTION, MODHSXPA, UHSO_STATIC_IFACE),
275202181Sthompsa	/* Option iCON 321 */
276202181Sthompsa	UHSO_DEV(OPTION, ICON321, UHSO_STATIC_IFACE),
277202181Sthompsa	/* Option iCON 322 */
278202243Sthompsa	UHSO_DEV(OPTION, GTICON322, UHSO_STATIC_IFACE),
279202243Sthompsa	/* Option iCON 505 */
280202243Sthompsa	UHSO_DEV(OPTION, ICON505, UHSO_AUTO_IFACE),
281210275Sthompsa	/* Option iCON 452 */
282210275Sthompsa	UHSO_DEV(OPTION, ICON505, UHSO_AUTO_IFACE),
283202181Sthompsa#undef UHSO_DEV
284202181Sthompsa};
285202181Sthompsa
286227309Sedstatic SYSCTL_NODE(_hw_usb, OID_AUTO, uhso, CTLFLAG_RW, 0, "USB uhso");
287202243Sthompsastatic int uhso_autoswitch = 1;
288202243SthompsaSYSCTL_INT(_hw_usb_uhso, OID_AUTO, auto_switch, CTLFLAG_RW,
289202243Sthompsa    &uhso_autoswitch, 0, "Automatically switch to modem mode");
290202181Sthompsa
291202181Sthompsa#ifdef USB_DEBUG
292202181Sthompsa#ifdef UHSO_DEBUG
293202181Sthompsastatic int uhso_debug = UHSO_DEBUG;
294202181Sthompsa#else
295202181Sthompsastatic int uhso_debug = -1;
296202181Sthompsa#endif
297202181Sthompsa
298202181SthompsaSYSCTL_INT(_hw_usb_uhso, OID_AUTO, debug, CTLFLAG_RW,
299202181Sthompsa    &uhso_debug, 0, "Debug level");
300202181Sthompsa
301202181Sthompsa#define UHSO_DPRINTF(n, x, ...) {\
302202181Sthompsa	if (uhso_debug >= n) {\
303202181Sthompsa		printf("%s: " x, __func__, ##__VA_ARGS__);\
304202181Sthompsa	}\
305202181Sthompsa}
306202181Sthompsa#else
307202181Sthompsa#define UHSO_DPRINTF(n, x, ...)
308202181Sthompsa#endif
309202181Sthompsa
310202181Sthompsa#ifdef UHSO_DEBUG_HEXDUMP
311202181Sthompsa# define UHSO_HEXDUMP(_buf, _len) do { \
312202181Sthompsa  { \
313202181Sthompsa        size_t __tmp; \
314202181Sthompsa        const char *__buf = (const char *)_buf; \
315202181Sthompsa        for (__tmp = 0; __tmp < _len; __tmp++) \
316202181Sthompsa                printf("%02hhx ", *__buf++); \
317202181Sthompsa    printf("\n"); \
318202181Sthompsa  } \
319202181Sthompsa} while(0)
320202181Sthompsa#else
321202181Sthompsa# define UHSO_HEXDUMP(_buf, _len)
322202181Sthompsa#endif
323202181Sthompsa
324202181Sthompsaenum {
325202181Sthompsa	UHSO_MUX_ENDPT_INTR = 0,
326202181Sthompsa	UHSO_MUX_ENDPT_MAX
327202181Sthompsa};
328202181Sthompsa
329202181Sthompsaenum {
330202181Sthompsa	UHSO_CTRL_READ = 0,
331202181Sthompsa	UHSO_CTRL_WRITE,
332202181Sthompsa	UHSO_CTRL_MAX
333202181Sthompsa};
334202181Sthompsa
335202181Sthompsaenum {
336202181Sthompsa	UHSO_IFNET_READ = 0,
337202181Sthompsa	UHSO_IFNET_WRITE,
338202181Sthompsa	UHSO_IFNET_MAX
339202181Sthompsa};
340202181Sthompsa
341202181Sthompsaenum {
342202181Sthompsa	UHSO_BULK_ENDPT_READ = 0,
343202181Sthompsa	UHSO_BULK_ENDPT_WRITE,
344202181Sthompsa	UHSO_BULK_ENDPT_INTR,
345202181Sthompsa	UHSO_BULK_ENDPT_MAX
346202181Sthompsa};
347202181Sthompsa
348202181Sthompsastatic usb_callback_t uhso_mux_intr_callback;
349202181Sthompsastatic usb_callback_t uhso_mux_read_callback;
350202181Sthompsastatic usb_callback_t uhso_mux_write_callback;
351202181Sthompsastatic usb_callback_t uhso_bs_read_callback;
352202181Sthompsastatic usb_callback_t uhso_bs_write_callback;
353202181Sthompsastatic usb_callback_t uhso_bs_intr_callback;
354202181Sthompsastatic usb_callback_t uhso_ifnet_read_callback;
355202181Sthompsastatic usb_callback_t uhso_ifnet_write_callback;
356202181Sthompsa
357202243Sthompsa/* Config used for the default control pipes */
358202181Sthompsastatic const struct usb_config uhso_ctrl_config[UHSO_CTRL_MAX] = {
359202181Sthompsa	[UHSO_CTRL_READ] = {
360202181Sthompsa		.type = UE_CONTROL,
361202181Sthompsa		.endpoint = 0x00,
362202181Sthompsa		.direction = UE_DIR_ANY,
363202181Sthompsa		.flags = { .pipe_bof = 1, .short_xfer_ok = 1 },
364202181Sthompsa		.bufsize = sizeof(struct usb_device_request) + 1024,
365202181Sthompsa		.callback = &uhso_mux_read_callback
366202181Sthompsa	},
367202181Sthompsa
368202181Sthompsa	[UHSO_CTRL_WRITE] = {
369202181Sthompsa		.type = UE_CONTROL,
370202181Sthompsa		.endpoint = 0x00,
371202181Sthompsa		.direction = UE_DIR_ANY,
372202181Sthompsa		.flags = { .pipe_bof = 1, .force_short_xfer = 1 },
373202181Sthompsa		.bufsize = sizeof(struct usb_device_request) + 1024,
374202181Sthompsa		.timeout = 1000,
375202181Sthompsa		.callback = &uhso_mux_write_callback
376202181Sthompsa	}
377202181Sthompsa};
378202181Sthompsa
379202243Sthompsa/* Config for the multiplexed serial ports */
380202181Sthompsastatic const struct usb_config uhso_mux_config[UHSO_MUX_ENDPT_MAX] = {
381202181Sthompsa	[UHSO_MUX_ENDPT_INTR] = {
382202181Sthompsa		.type = UE_INTERRUPT,
383202181Sthompsa		.endpoint = UE_ADDR_ANY,
384202181Sthompsa		.direction = UE_DIR_IN,
385202181Sthompsa		.flags = { .short_xfer_ok = 1 },
386202181Sthompsa		.bufsize = 0,
387202181Sthompsa		.callback = &uhso_mux_intr_callback,
388202181Sthompsa	}
389202181Sthompsa};
390202181Sthompsa
391202243Sthompsa/* Config for the raw IP-packet interface */
392202181Sthompsastatic const struct usb_config uhso_ifnet_config[UHSO_IFNET_MAX] = {
393202181Sthompsa	[UHSO_IFNET_READ] = {
394202181Sthompsa		.type = UE_BULK,
395202181Sthompsa		.endpoint = UE_ADDR_ANY,
396202181Sthompsa		.direction = UE_DIR_IN,
397202181Sthompsa		.flags = { .pipe_bof = 1, .short_xfer_ok = 1 },
398202181Sthompsa		.bufsize = MCLBYTES,
399202181Sthompsa		.callback = &uhso_ifnet_read_callback
400202181Sthompsa	},
401202181Sthompsa	[UHSO_IFNET_WRITE] = {
402202181Sthompsa		.type = UE_BULK,
403202181Sthompsa		.endpoint = UE_ADDR_ANY,
404202181Sthompsa		.direction = UE_DIR_OUT,
405202181Sthompsa		.flags = { .pipe_bof = 1, .force_short_xfer = 1 },
406202181Sthompsa		.bufsize = MCLBYTES,
407202181Sthompsa		.timeout = 5 * USB_MS_HZ,
408202181Sthompsa		.callback = &uhso_ifnet_write_callback
409202181Sthompsa	}
410202181Sthompsa};
411202181Sthompsa
412202243Sthompsa/* Config for interfaces with normal bulk serial ports */
413202181Sthompsastatic const struct usb_config uhso_bs_config[UHSO_BULK_ENDPT_MAX] = {
414202181Sthompsa	[UHSO_BULK_ENDPT_READ] = {
415202181Sthompsa		.type = UE_BULK,
416202181Sthompsa		.endpoint = UE_ADDR_ANY,
417202181Sthompsa		.direction = UE_DIR_IN,
418202181Sthompsa		.flags = { .pipe_bof = 1, .short_xfer_ok = 1 },
419202181Sthompsa		.bufsize = 4096,
420202181Sthompsa		.callback = &uhso_bs_read_callback
421202181Sthompsa	},
422202181Sthompsa
423202181Sthompsa	[UHSO_BULK_ENDPT_WRITE] = {
424202181Sthompsa		.type = UE_BULK,
425202181Sthompsa		.endpoint = UE_ADDR_ANY,
426202181Sthompsa		.direction = UE_DIR_OUT,
427202181Sthompsa		.flags = { .pipe_bof = 1, .force_short_xfer = 1 },
428202181Sthompsa		.bufsize = 8192,
429202181Sthompsa		.callback = &uhso_bs_write_callback
430202181Sthompsa	},
431202181Sthompsa
432202181Sthompsa	[UHSO_BULK_ENDPT_INTR] = {
433202181Sthompsa		.type = UE_INTERRUPT,
434202181Sthompsa		.endpoint = UE_ADDR_ANY,
435202181Sthompsa		.direction = UE_DIR_IN,
436202181Sthompsa		.flags = { .short_xfer_ok = 1 },
437202181Sthompsa		.bufsize = 0,
438202181Sthompsa		.callback = &uhso_bs_intr_callback,
439202181Sthompsa	}
440202181Sthompsa};
441202181Sthompsa
442202243Sthompsastatic int  uhso_probe_iface(struct uhso_softc *, int,
443210275Sthompsa    int (*probe)(struct usb_device *, int));
444210275Sthompsastatic int  uhso_probe_iface_auto(struct usb_device *, int);
445210275Sthompsastatic int  uhso_probe_iface_static(struct usb_device *, int);
446202243Sthompsastatic int  uhso_attach_muxserial(struct uhso_softc *, struct usb_interface *,
447202181Sthompsa    int type);
448202243Sthompsastatic int  uhso_attach_bulkserial(struct uhso_softc *, struct usb_interface *,
449202181Sthompsa    int type);
450202243Sthompsastatic int  uhso_attach_ifnet(struct uhso_softc *, struct usb_interface *,
451202181Sthompsa    int type);
452202181Sthompsastatic void uhso_test_autoinst(void *, struct usb_device *,
453202181Sthompsa		struct usb_attach_arg *);
454202243Sthompsastatic int  uhso_driver_loaded(struct module *, int, void *);
455210275Sthompsastatic int uhso_radio_sysctl(SYSCTL_HANDLER_ARGS);
456210275Sthompsastatic int uhso_radio_ctrl(struct uhso_softc *, int);
457202181Sthompsa
458239180Shselaskystatic void uhso_free(struct ucom_softc *);
459202181Sthompsastatic void uhso_ucom_start_read(struct ucom_softc *);
460202181Sthompsastatic void uhso_ucom_stop_read(struct ucom_softc *);
461202181Sthompsastatic void uhso_ucom_start_write(struct ucom_softc *);
462202181Sthompsastatic void uhso_ucom_stop_write(struct ucom_softc *);
463202181Sthompsastatic void uhso_ucom_cfg_get_status(struct ucom_softc *, uint8_t *, uint8_t *);
464202181Sthompsastatic void uhso_ucom_cfg_set_dtr(struct ucom_softc *, uint8_t);
465202181Sthompsastatic void uhso_ucom_cfg_set_rts(struct ucom_softc *, uint8_t);
466202181Sthompsastatic void uhso_if_init(void *);
467202181Sthompsastatic void uhso_if_start(struct ifnet *);
468202181Sthompsastatic void uhso_if_stop(struct uhso_softc *);
469202243Sthompsastatic int  uhso_if_ioctl(struct ifnet *, u_long, caddr_t);
470202243Sthompsastatic int  uhso_if_output(struct ifnet *, struct mbuf *, struct sockaddr *,
471202181Sthompsa    struct route *);
472202181Sthompsastatic void uhso_if_rxflush(void *);
473202181Sthompsa
474202181Sthompsastatic device_probe_t uhso_probe;
475202181Sthompsastatic device_attach_t uhso_attach;
476202181Sthompsastatic device_detach_t uhso_detach;
477239180Shselaskystatic device_free_softc_t uhso_free_softc;
478202181Sthompsa
479202181Sthompsastatic device_method_t uhso_methods[] = {
480202181Sthompsa	DEVMETHOD(device_probe,		uhso_probe),
481202181Sthompsa	DEVMETHOD(device_attach,	uhso_attach),
482202181Sthompsa	DEVMETHOD(device_detach,	uhso_detach),
483239180Shselasky	DEVMETHOD(device_free_softc,	uhso_free_softc),
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_config_descriptor *cd;
545202181Sthompsa	struct usb_interface_descriptor *id;
546202181Sthompsa	struct sysctl_ctx_list *sctx;
547202181Sthompsa	struct sysctl_oid *soid;
548210275Sthompsa	struct sysctl_oid *tree = NULL, *tty_node;
549202181Sthompsa	struct ucom_softc *ucom;
550202181Sthompsa	struct uhso_tty *ht;
551202181Sthompsa	int i, error, port;
552202181Sthompsa	void *probe_f;
553202181Sthompsa	usb_error_t uerr;
554202181Sthompsa	char *desc;
555202181Sthompsa
556202181Sthompsa	sc->sc_dev = self;
557202181Sthompsa	sc->sc_udev = uaa->device;
558202181Sthompsa	mtx_init(&sc->sc_mtx, "uhso", NULL, MTX_DEF);
559239180Shselasky	ucom_ref(&sc->sc_super_ucom);
560202181Sthompsa
561202181Sthompsa	sc->sc_ucom = NULL;
562202181Sthompsa	sc->sc_ttys = 0;
563210275Sthompsa	sc->sc_radio = 1;
564202181Sthompsa
565202181Sthompsa	cd = usbd_get_config_descriptor(uaa->device);
566202181Sthompsa	id = usbd_get_interface_descriptor(uaa->iface);
567202181Sthompsa	sc->sc_ctrl_iface_no = id->bInterfaceNumber;
568202181Sthompsa
569202181Sthompsa	sc->sc_iface_no = uaa->info.bIfaceNum;
570202181Sthompsa	sc->sc_iface_index = uaa->info.bIfaceIndex;
571202181Sthompsa
572202181Sthompsa	/* Setup control pipe */
573202181Sthompsa	uerr = usbd_transfer_setup(uaa->device,
574202181Sthompsa	    &sc->sc_iface_index, sc->sc_ctrl_xfer,
575202181Sthompsa	    uhso_ctrl_config, UHSO_CTRL_MAX, sc, &sc->sc_mtx);
576202181Sthompsa	if (uerr) {
577202181Sthompsa		device_printf(self, "Failed to setup control pipe: %s\n",
578202181Sthompsa		    usbd_errstr(uerr));
579202181Sthompsa		goto out;
580202181Sthompsa	}
581202181Sthompsa
582202181Sthompsa	if (USB_GET_DRIVER_INFO(uaa) == UHSO_STATIC_IFACE)
583202181Sthompsa		probe_f = uhso_probe_iface_static;
584202181Sthompsa	else if (USB_GET_DRIVER_INFO(uaa) == UHSO_AUTO_IFACE)
585202181Sthompsa		probe_f = uhso_probe_iface_auto;
586202181Sthompsa	else
587202181Sthompsa		goto out;
588202181Sthompsa
589202181Sthompsa	error = uhso_probe_iface(sc, uaa->info.bIfaceNum, probe_f);
590202181Sthompsa	if (error != 0)
591202181Sthompsa		goto out;
592202181Sthompsa
593202181Sthompsa	sctx = device_get_sysctl_ctx(sc->sc_dev);
594202181Sthompsa	soid = device_get_sysctl_tree(sc->sc_dev);
595202181Sthompsa
596202181Sthompsa	SYSCTL_ADD_STRING(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "type",
597202181Sthompsa	    CTLFLAG_RD, uhso_port[UHSO_IFACE_PORT(sc->sc_type)], 0,
598202181Sthompsa	    "Port available at this interface");
599210275Sthompsa	SYSCTL_ADD_PROC(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "radio",
600210275Sthompsa	    CTLTYPE_INT | CTLFLAG_RW, sc, 0, uhso_radio_sysctl, "I", "Enable radio");
601202181Sthompsa
602202243Sthompsa	/*
603202270Sthompsa	 * The default interface description on most Option devices isn't
604202243Sthompsa	 * very helpful. So we skip device_set_usb_desc and set the
605202243Sthompsa	 * device description manually.
606202243Sthompsa	 */
607202243Sthompsa	device_set_desc_copy(self, uhso_port_type[UHSO_IFACE_PORT_TYPE(sc->sc_type)]);
608202243Sthompsa	/* Announce device */
609202243Sthompsa	device_printf(self, "<%s port> at <%s %s> on %s\n",
610202243Sthompsa	    uhso_port_type[UHSO_IFACE_PORT_TYPE(sc->sc_type)],
611212136Sthompsa	    usb_get_manufacturer(uaa->device),
612212136Sthompsa	    usb_get_product(uaa->device),
613212136Sthompsa	    device_get_nameunit(device_get_parent(self)));
614202243Sthompsa
615202181Sthompsa	if (sc->sc_ttys > 0) {
616202181Sthompsa		SYSCTL_ADD_INT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "ports",
617202181Sthompsa		    CTLFLAG_RD, &sc->sc_ttys, 0, "Number of attached serial ports");
618202181Sthompsa
619202181Sthompsa		tree = SYSCTL_ADD_NODE(sctx, SYSCTL_CHILDREN(soid), OID_AUTO,
620202181Sthompsa		    "port", CTLFLAG_RD, NULL, "Serial ports");
621202181Sthompsa	}
622202181Sthompsa
623202243Sthompsa	/*
624202243Sthompsa	 * Loop through the number of found TTYs and create sysctl
625202243Sthompsa	 * nodes for them.
626202243Sthompsa	 */
627202181Sthompsa	for (i = 0; i < sc->sc_ttys; i++) {
628202181Sthompsa		ht = &sc->sc_tty[i];
629202181Sthompsa		ucom = &sc->sc_ucom[i];
630202181Sthompsa
631202181Sthompsa		if (UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_MUX)
632202181Sthompsa			port = uhso_mux_port_map[ht->ht_muxport];
633202181Sthompsa		else
634202181Sthompsa			port = UHSO_IFACE_PORT_TYPE(sc->sc_type);
635202181Sthompsa
636202181Sthompsa		desc = uhso_port_type_sysctl[port];
637202181Sthompsa
638202181Sthompsa		tty_node = SYSCTL_ADD_NODE(sctx, SYSCTL_CHILDREN(tree), OID_AUTO,
639202181Sthompsa		    desc, CTLFLAG_RD, NULL, "");
640202181Sthompsa
641202181Sthompsa		ht->ht_name[0] = 0;
642202181Sthompsa		if (sc->sc_ttys == 1)
643214761Sn_hibma			snprintf(ht->ht_name, 32, "cuaU%d", ucom->sc_super->sc_unit);
644202181Sthompsa		else {
645202181Sthompsa			snprintf(ht->ht_name, 32, "cuaU%d.%d",
646214761Sn_hibma			    ucom->sc_super->sc_unit, ucom->sc_subunit);
647202181Sthompsa		}
648202181Sthompsa
649202181Sthompsa		desc = uhso_port_type[port];
650202181Sthompsa		SYSCTL_ADD_STRING(sctx, SYSCTL_CHILDREN(tty_node), OID_AUTO,
651202181Sthompsa		    "tty", CTLFLAG_RD, ht->ht_name, 0, "");
652202181Sthompsa		SYSCTL_ADD_STRING(sctx, SYSCTL_CHILDREN(tty_node), OID_AUTO,
653202181Sthompsa		    "desc", CTLFLAG_RD, desc, 0, "");
654202181Sthompsa
655202181Sthompsa		if (bootverbose)
656202181Sthompsa			device_printf(sc->sc_dev,
657202181Sthompsa			    "\"%s\" port at %s\n", desc, ht->ht_name);
658202181Sthompsa	}
659202181Sthompsa
660202181Sthompsa	return (0);
661202181Sthompsaout:
662202181Sthompsa	uhso_detach(sc->sc_dev);
663202181Sthompsa	return (ENXIO);
664202181Sthompsa}
665202181Sthompsa
666202181Sthompsastatic int
667202181Sthompsauhso_detach(device_t self)
668202181Sthompsa{
669202181Sthompsa	struct uhso_softc *sc = device_get_softc(self);
670202181Sthompsa	int i;
671202181Sthompsa
672202181Sthompsa	usbd_transfer_unsetup(sc->sc_xfer, 3);
673202181Sthompsa	usbd_transfer_unsetup(sc->sc_ctrl_xfer, UHSO_CTRL_MAX);
674202181Sthompsa	if (sc->sc_ttys > 0) {
675214761Sn_hibma		ucom_detach(&sc->sc_super_ucom, sc->sc_ucom);
676202181Sthompsa
677202181Sthompsa		for (i = 0; i < sc->sc_ttys; i++) {
678202181Sthompsa			if (sc->sc_tty[i].ht_muxport != -1) {
679202181Sthompsa				usbd_transfer_unsetup(sc->sc_tty[i].ht_xfer,
680202181Sthompsa				    UHSO_CTRL_MAX);
681202181Sthompsa			}
682202181Sthompsa		}
683202181Sthompsa
684202181Sthompsa		free(sc->sc_tty, M_USBDEV);
685202181Sthompsa		free(sc->sc_ucom, M_USBDEV);
686202181Sthompsa	}
687202181Sthompsa
688202181Sthompsa	if (sc->sc_ifp != NULL) {
689202181Sthompsa		callout_drain(&sc->sc_c);
690210275Sthompsa		free_unr(uhso_ifnet_unit, sc->sc_ifp->if_dunit);
691202181Sthompsa		mtx_lock(&sc->sc_mtx);
692202181Sthompsa		uhso_if_stop(sc);
693202181Sthompsa		bpfdetach(sc->sc_ifp);
694202181Sthompsa		if_detach(sc->sc_ifp);
695202181Sthompsa		if_free(sc->sc_ifp);
696202181Sthompsa		mtx_unlock(&sc->sc_mtx);
697202181Sthompsa		usbd_transfer_unsetup(sc->sc_if_xfer, UHSO_IFNET_MAX);
698202181Sthompsa	}
699202181Sthompsa
700202181Sthompsa	return (0);
701202181Sthompsa}
702202181Sthompsa
703239180ShselaskyUCOM_UNLOAD_DRAIN(uhso);
704239180Shselasky
705202181Sthompsastatic void
706239180Shselaskyuhso_free_softc(device_t dev, void *arg)
707239180Shselasky{
708239180Shselasky	struct uhso_softc *sc = arg;
709239180Shselasky
710239180Shselasky	if (ucom_unref(&sc->sc_super_ucom)) {
711239180Shselasky		if (mtx_initialized(&sc->sc_mtx))
712239180Shselasky			mtx_destroy(&sc->sc_mtx);
713239180Shselasky		device_free_softc(dev, sc);
714239180Shselasky	}
715239180Shselasky}
716239180Shselasky
717239180Shselaskystatic void
718239180Shselaskyuhso_free(struct ucom_softc *ucom)
719239180Shselasky{
720239180Shselasky	uhso_free_softc(NULL, ucom->sc_parent);
721239180Shselasky}
722239180Shselasky
723239180Shselaskystatic void
724202181Sthompsauhso_test_autoinst(void *arg, struct usb_device *udev,
725202181Sthompsa    struct usb_attach_arg *uaa)
726202181Sthompsa{
727202181Sthompsa	struct usb_interface *iface;
728202181Sthompsa	struct usb_interface_descriptor *id;
729202181Sthompsa
730202243Sthompsa	if (uaa->dev_state != UAA_DEV_READY || !uhso_autoswitch)
731202181Sthompsa		return;
732202181Sthompsa
733202181Sthompsa	iface = usbd_get_iface(udev, 0);
734202181Sthompsa	if (iface == NULL)
735202181Sthompsa		return;
736202181Sthompsa	id = iface->idesc;
737202181Sthompsa	if (id == NULL || id->bInterfaceClass != UICLASS_MASS)
738202181Sthompsa		return;
739202181Sthompsa	if (usbd_lookup_id_by_uaa(uhso_devs, sizeof(uhso_devs), uaa))
740202181Sthompsa		return;		/* no device match */
741202181Sthompsa
742202181Sthompsa	if (usb_msc_eject(udev, 0, MSC_EJECT_REZERO) == 0) {
743202181Sthompsa		/* success, mark the udev as disappearing */
744202181Sthompsa		uaa->dev_state = UAA_DEV_EJECTING;
745202181Sthompsa	}
746202181Sthompsa}
747202181Sthompsa
748202181Sthompsastatic int
749202181Sthompsauhso_driver_loaded(struct module *mod, int what, void *arg)
750202181Sthompsa{
751202181Sthompsa	switch (what) {
752202181Sthompsa	case MOD_LOAD:
753202181Sthompsa		/* register our autoinstall handler */
754202181Sthompsa		uhso_etag = EVENTHANDLER_REGISTER(usb_dev_configured,
755202181Sthompsa		    uhso_test_autoinst, NULL, EVENTHANDLER_PRI_ANY);
756210275Sthompsa		/* create our unit allocator for inet devs */
757210275Sthompsa		uhso_ifnet_unit = new_unrhdr(0, INT_MAX, NULL);
758202181Sthompsa		break;
759202181Sthompsa	case MOD_UNLOAD:
760202181Sthompsa		EVENTHANDLER_DEREGISTER(usb_dev_configured, uhso_etag);
761210275Sthompsa		delete_unrhdr(uhso_ifnet_unit);
762202181Sthompsa		break;
763202181Sthompsa	default:
764202181Sthompsa		return (EOPNOTSUPP);
765202181Sthompsa	}
766202181Sthompsa	return (0);
767202181Sthompsa}
768202181Sthompsa
769202243Sthompsa/*
770202243Sthompsa * Probe the interface type by querying the device. The elements
771202243Sthompsa * of an array indicates the capabilities of a particular interface.
772202243Sthompsa * Returns a bit mask with the interface capabilities.
773202243Sthompsa */
774202243Sthompsastatic int
775210275Sthompsauhso_probe_iface_auto(struct usb_device *udev, int index)
776202181Sthompsa{
777202181Sthompsa	struct usb_device_request req;
778202181Sthompsa	usb_error_t uerr;
779202181Sthompsa	uint16_t actlen = 0;
780202181Sthompsa	char port;
781202181Sthompsa	char buf[17] = {0};
782202181Sthompsa
783202181Sthompsa	req.bmRequestType = UT_READ_VENDOR_DEVICE;
784202181Sthompsa	req.bRequest = 0x86;
785202181Sthompsa	USETW(req.wValue, 0);
786202181Sthompsa	USETW(req.wIndex, 0);
787202181Sthompsa	USETW(req.wLength, 17);
788202181Sthompsa
789210275Sthompsa	uerr = usbd_do_request_flags(udev, NULL, &req, buf,
790202181Sthompsa	    0, &actlen, USB_MS_HZ);
791202181Sthompsa	if (uerr != 0) {
792210275Sthompsa		printf("%s: usbd_do_request_flags failed, %s\n",
793210275Sthompsa		    __func__, usbd_errstr(uerr));
794202181Sthompsa		return (0);
795202181Sthompsa	}
796202181Sthompsa
797202243Sthompsa	UHSO_DPRINTF(1, "actlen=%d\n", actlen);
798202181Sthompsa	UHSO_HEXDUMP(buf, 17);
799202181Sthompsa
800202181Sthompsa	if (index < 0 || index > 16) {
801202181Sthompsa		UHSO_DPRINTF(0, "Index %d out of range\n", index);
802202181Sthompsa		return (0);
803202181Sthompsa	}
804202181Sthompsa
805202243Sthompsa	UHSO_DPRINTF(1, "index=%d, type=%x[%s]\n", index, buf[index],
806202243Sthompsa	    uhso_port_type[(int)uhso_port_map[(int)buf[index]]]);
807202181Sthompsa
808202181Sthompsa	if (buf[index] >= uhso_port_map_max)
809202181Sthompsa		port = 0;
810202181Sthompsa	else
811202181Sthompsa		port = uhso_port_map[(int)buf[index]];
812202181Sthompsa
813202243Sthompsa	switch (port) {
814202243Sthompsa	case UHSO_PORT_TYPE_NETWORK:
815202243Sthompsa		return (UHSO_IFACE_SPEC(UHSO_IF_NET | UHSO_IF_MUX,
816202243Sthompsa		    UHSO_PORT_SERIAL | UHSO_PORT_NETWORK, port));
817210275Sthompsa	case UHSO_PORT_TYPE_DIAG:
818210275Sthompsa	case UHSO_PORT_TYPE_DIAG2:
819210275Sthompsa	case UHSO_PORT_TYPE_CTL:
820210275Sthompsa	case UHSO_PORT_TYPE_APP:
821210275Sthompsa	case UHSO_PORT_TYPE_APP2:
822210275Sthompsa	case UHSO_PORT_TYPE_MODEM:
823210275Sthompsa		return (UHSO_IFACE_SPEC(UHSO_IF_BULK,
824210275Sthompsa		    UHSO_PORT_SERIAL, port));
825210275Sthompsa	case UHSO_PORT_TYPE_MSD:
826202181Sthompsa		return (0);
827210275Sthompsa	case UHSO_PORT_TYPE_UNKNOWN:
828202243Sthompsa	default:
829210275Sthompsa		return (0);
830202243Sthompsa	}
831202181Sthompsa
832202181Sthompsa	return (0);
833202181Sthompsa}
834202181Sthompsa
835210275Sthompsa/*
836210275Sthompsa * Returns the capabilities of interfaces for devices that don't
837210275Sthompsa * support the automatic query.
838210275Sthompsa * Returns a bit mask with the interface capabilities.
839210275Sthompsa */
840202181Sthompsastatic int
841210275Sthompsauhso_probe_iface_static(struct usb_device *udev, int index)
842202181Sthompsa{
843202181Sthompsa	struct usb_config_descriptor *cd;
844202181Sthompsa
845210275Sthompsa	cd = usbd_get_config_descriptor(udev);
846202181Sthompsa	if (cd->bNumInterface <= 3) {
847202243Sthompsa		/* Cards with 3 or less interfaces */
848202181Sthompsa		switch (index) {
849202181Sthompsa		case 0:
850202181Sthompsa			return UHSO_IFACE_SPEC(UHSO_IF_NET | UHSO_IF_MUX,
851202243Sthompsa			    UHSO_PORT_SERIAL | UHSO_PORT_NETWORK,
852202243Sthompsa			    UHSO_PORT_TYPE_NETWORK);
853202181Sthompsa		case 1:
854202181Sthompsa			return UHSO_IFACE_SPEC(UHSO_IF_BULK,
855202181Sthompsa			    UHSO_PORT_SERIAL, UHSO_PORT_TYPE_DIAG);
856202181Sthompsa		case 2:
857202181Sthompsa			return UHSO_IFACE_SPEC(UHSO_IF_BULK,
858202181Sthompsa			    UHSO_PORT_SERIAL, UHSO_PORT_TYPE_MODEM);
859202181Sthompsa		}
860202243Sthompsa	} else {
861202243Sthompsa		/* Cards with 4 interfaces */
862202181Sthompsa		switch (index) {
863202181Sthompsa		case 0:
864202181Sthompsa			return UHSO_IFACE_SPEC(UHSO_IF_NET | UHSO_IF_MUX,
865202243Sthompsa			    UHSO_PORT_SERIAL | UHSO_PORT_NETWORK,
866202243Sthompsa			    UHSO_PORT_TYPE_NETWORK);
867202181Sthompsa		case 1:
868202181Sthompsa			return UHSO_IFACE_SPEC(UHSO_IF_BULK,
869202181Sthompsa			    UHSO_PORT_SERIAL, UHSO_PORT_TYPE_DIAG2);
870202181Sthompsa		case 2:
871202181Sthompsa			return UHSO_IFACE_SPEC(UHSO_IF_BULK,
872202181Sthompsa			    UHSO_PORT_SERIAL, UHSO_PORT_TYPE_MODEM);
873202181Sthompsa		case 3:
874202181Sthompsa			return UHSO_IFACE_SPEC(UHSO_IF_BULK,
875202181Sthompsa			    UHSO_PORT_SERIAL, UHSO_PORT_TYPE_DIAG);
876202181Sthompsa		}
877202181Sthompsa	}
878202181Sthompsa	return (0);
879202181Sthompsa}
880202181Sthompsa
881202243Sthompsa/*
882202243Sthompsa * Probes an interface for its particular capabilities and attaches if
883202243Sthompsa * it's a supported interface.
884202243Sthompsa */
885202181Sthompsastatic int
886202181Sthompsauhso_probe_iface(struct uhso_softc *sc, int index,
887210275Sthompsa    int (*probe)(struct usb_device *, int))
888202181Sthompsa{
889202181Sthompsa	struct usb_interface *iface;
890202243Sthompsa	int type, error;
891202181Sthompsa
892202243Sthompsa	UHSO_DPRINTF(1, "Probing for interface %d, probe_func=%p\n", index, probe);
893202181Sthompsa
894210275Sthompsa	type = probe(sc->sc_udev, index);
895202181Sthompsa	UHSO_DPRINTF(1, "Probe result %x\n", type);
896202181Sthompsa	if (type <= 0)
897202181Sthompsa		return (ENXIO);
898202181Sthompsa
899202181Sthompsa	sc->sc_type = type;
900202181Sthompsa	iface = usbd_get_iface(sc->sc_udev, index);
901202181Sthompsa
902202243Sthompsa	if (UHSO_IFACE_PORT_TYPE(type) == UHSO_PORT_TYPE_NETWORK) {
903202181Sthompsa		error = uhso_attach_ifnet(sc, iface, type);
904202243Sthompsa		if (error) {
905202243Sthompsa			UHSO_DPRINTF(1, "uhso_attach_ifnet failed");
906202181Sthompsa			return (ENXIO);
907202243Sthompsa		}
908202181Sthompsa
909202243Sthompsa		/*
910202243Sthompsa		 * If there is an additional interrupt endpoint on this
911202270Sthompsa		 * interface then we most likely have a multiplexed serial port
912202243Sthompsa		 * available.
913202243Sthompsa		 */
914202243Sthompsa		if (iface->idesc->bNumEndpoints < 3) {
915202243Sthompsa			sc->sc_type = UHSO_IFACE_SPEC(
916202243Sthompsa			    UHSO_IFACE_USB_TYPE(type) & ~UHSO_IF_MUX,
917202243Sthompsa			    UHSO_IFACE_PORT(type) & ~UHSO_PORT_SERIAL,
918202243Sthompsa			    UHSO_IFACE_PORT_TYPE(type));
919202243Sthompsa			return (0);
920202243Sthompsa		}
921202243Sthompsa
922202243Sthompsa		UHSO_DPRINTF(1, "Trying to attach mux. serial\n");
923202243Sthompsa		error = uhso_attach_muxserial(sc, iface, type);
924202243Sthompsa		if (error == 0 && sc->sc_ttys > 0) {
925202181Sthompsa			error = ucom_attach(&sc->sc_super_ucom, sc->sc_ucom,
926202181Sthompsa			    sc->sc_ttys, sc, &uhso_ucom_callback, &sc->sc_mtx);
927202181Sthompsa			if (error) {
928202181Sthompsa				device_printf(sc->sc_dev, "ucom_attach failed\n");
929202181Sthompsa				return (ENXIO);
930202181Sthompsa			}
931214843Sn_hibma			ucom_set_pnpinfo_usb(&sc->sc_super_ucom, sc->sc_dev);
932202243Sthompsa
933202243Sthompsa			mtx_lock(&sc->sc_mtx);
934202243Sthompsa			usbd_transfer_start(sc->sc_xfer[UHSO_MUX_ENDPT_INTR]);
935202243Sthompsa			mtx_unlock(&sc->sc_mtx);
936202181Sthompsa		}
937202243Sthompsa	} else if ((UHSO_IFACE_USB_TYPE(type) & UHSO_IF_BULK) &&
938202181Sthompsa	    UHSO_IFACE_PORT(type) & UHSO_PORT_SERIAL) {
939202181Sthompsa
940202181Sthompsa		error = uhso_attach_bulkserial(sc, iface, type);
941202181Sthompsa		if (error)
942202181Sthompsa			return (ENXIO);
943202181Sthompsa
944202181Sthompsa		error = ucom_attach(&sc->sc_super_ucom, sc->sc_ucom,
945202181Sthompsa		    sc->sc_ttys, sc, &uhso_ucom_callback, &sc->sc_mtx);
946202181Sthompsa		if (error) {
947202181Sthompsa			device_printf(sc->sc_dev, "ucom_attach failed\n");
948202181Sthompsa			return (ENXIO);
949202181Sthompsa		}
950214843Sn_hibma		ucom_set_pnpinfo_usb(&sc->sc_super_ucom, sc->sc_dev);
951202181Sthompsa	}
952202181Sthompsa	else {
953202243Sthompsa		UHSO_DPRINTF(0, "Unknown type %x\n", type);
954202181Sthompsa		return (ENXIO);
955202181Sthompsa	}
956202181Sthompsa
957202181Sthompsa	return (0);
958202181Sthompsa}
959202181Sthompsa
960210275Sthompsastatic int
961210275Sthompsauhso_radio_ctrl(struct uhso_softc *sc, int onoff)
962210275Sthompsa{
963210275Sthompsa	struct usb_device_request req;
964210275Sthompsa	usb_error_t uerr;
965210275Sthompsa
966210275Sthompsa	req.bmRequestType = UT_VENDOR;
967210275Sthompsa	req.bRequest = onoff ? 0x82 : 0x81;
968210275Sthompsa	USETW(req.wValue, 0);
969210275Sthompsa	USETW(req.wIndex, 0);
970210275Sthompsa	USETW(req.wLength, 0);
971210275Sthompsa
972210275Sthompsa	uerr = usbd_do_request(sc->sc_udev, NULL, &req, NULL);
973210275Sthompsa	if (uerr != 0) {
974210275Sthompsa		device_printf(sc->sc_dev, "usbd_do_request_flags failed: %s\n",
975210275Sthompsa		    usbd_errstr(uerr));
976210275Sthompsa		return (-1);
977210275Sthompsa	}
978210275Sthompsa	return (onoff);
979210275Sthompsa}
980210275Sthompsa
981210275Sthompsastatic int
982210275Sthompsauhso_radio_sysctl(SYSCTL_HANDLER_ARGS)
983210275Sthompsa{
984210275Sthompsa	struct uhso_softc *sc = arg1;
985210275Sthompsa	int error, radio;
986210275Sthompsa
987210275Sthompsa	radio = sc->sc_radio;
988210275Sthompsa	error = sysctl_handle_int(oidp, &radio, 0, req);
989210275Sthompsa	if (error)
990210275Sthompsa		return (error);
991210275Sthompsa	if (radio != sc->sc_radio) {
992210275Sthompsa		radio = radio != 0 ? 1 : 0;
993210275Sthompsa		error = uhso_radio_ctrl(sc, radio);
994210275Sthompsa		if (error != -1)
995210275Sthompsa			sc->sc_radio = radio;
996210275Sthompsa
997210275Sthompsa	}
998210275Sthompsa	return (0);
999210275Sthompsa}
1000210275Sthompsa
1001202243Sthompsa/*
1002202243Sthompsa * Expands allocated memory to fit an additional TTY.
1003202243Sthompsa * Two arrays are kept with matching indexes, one for ucom and one
1004202243Sthompsa * for our private data.
1005202243Sthompsa */
1006202181Sthompsastatic int
1007202181Sthompsauhso_alloc_tty(struct uhso_softc *sc)
1008202181Sthompsa{
1009202181Sthompsa
1010202181Sthompsa	sc->sc_ttys++;
1011202181Sthompsa	sc->sc_tty = reallocf(sc->sc_tty, sizeof(struct uhso_tty) * sc->sc_ttys,
1012202181Sthompsa	    M_USBDEV, M_WAITOK | M_ZERO);
1013202181Sthompsa	if (sc->sc_tty == NULL)
1014202181Sthompsa		return (-1);
1015202181Sthompsa
1016202181Sthompsa	sc->sc_ucom = reallocf(sc->sc_ucom,
1017202181Sthompsa	    sizeof(struct ucom_softc) * sc->sc_ttys, M_USBDEV, M_WAITOK | M_ZERO);
1018202181Sthompsa	if (sc->sc_ucom == NULL)
1019202181Sthompsa		return (-1);
1020202181Sthompsa
1021202181Sthompsa	sc->sc_tty[sc->sc_ttys - 1].ht_sc = sc;
1022202181Sthompsa
1023202243Sthompsa	UHSO_DPRINTF(1, "Allocated TTY %d\n", sc->sc_ttys - 1);
1024202181Sthompsa	return (sc->sc_ttys - 1);
1025202181Sthompsa}
1026202181Sthompsa
1027202243Sthompsa/*
1028202243Sthompsa * Attach a multiplexed serial port
1029202243Sthompsa * Data is read/written with requests on the default control pipe. An interrupt
1030202243Sthompsa * endpoint returns when there is new data to be read.
1031202243Sthompsa */
1032202181Sthompsastatic int
1033202181Sthompsauhso_attach_muxserial(struct uhso_softc *sc, struct usb_interface *iface,
1034202181Sthompsa    int type)
1035202181Sthompsa{
1036202181Sthompsa	struct usb_descriptor *desc;
1037202181Sthompsa	int i, port, tty;
1038202181Sthompsa	usb_error_t uerr;
1039202181Sthompsa
1040202181Sthompsa	/*
1041202181Sthompsa	 * The class specific interface (type 0x24) descriptor subtype field
1042202181Sthompsa	 * contains a bitmask that specifies which (and how many) ports that
1043202181Sthompsa	 * are available through this multiplexed serial port.
1044202181Sthompsa 	 */
1045202181Sthompsa	desc = usbd_find_descriptor(sc->sc_udev, NULL,
1046202181Sthompsa	    iface->idesc->bInterfaceNumber, UDESC_CS_INTERFACE, 0xff, 0, 0);
1047202181Sthompsa	if (desc == NULL) {
1048202181Sthompsa		UHSO_DPRINTF(0, "Failed to find UDESC_CS_INTERFACE\n");
1049202181Sthompsa		return (ENXIO);
1050202181Sthompsa	}
1051202181Sthompsa
1052202181Sthompsa	UHSO_DPRINTF(1, "Mux port mask %x\n", desc->bDescriptorSubtype);
1053202181Sthompsa	if (desc->bDescriptorSubtype == 0)
1054202181Sthompsa		return (ENXIO);
1055202181Sthompsa
1056202243Sthompsa	/*
1057202243Sthompsa	 * The bitmask is one octet, loop through the number of
1058202243Sthompsa	 * bits that are set and create a TTY for each.
1059202243Sthompsa	 */
1060202181Sthompsa	for (i = 0; i < 8; i++) {
1061202181Sthompsa		port = (1 << i);
1062202181Sthompsa		if ((port & desc->bDescriptorSubtype) == port) {
1063202181Sthompsa			UHSO_DPRINTF(2, "Found mux port %x (%d)\n", port, i);
1064202181Sthompsa			tty = uhso_alloc_tty(sc);
1065202181Sthompsa			if (tty < 0)
1066202181Sthompsa				return (ENOMEM);
1067202181Sthompsa			sc->sc_tty[tty].ht_muxport = i;
1068202181Sthompsa			uerr = usbd_transfer_setup(sc->sc_udev,
1069202181Sthompsa			    &sc->sc_iface_index, sc->sc_tty[tty].ht_xfer,
1070202181Sthompsa			    uhso_ctrl_config, UHSO_CTRL_MAX, sc, &sc->sc_mtx);
1071202181Sthompsa			if (uerr) {
1072202181Sthompsa				device_printf(sc->sc_dev,
1073202181Sthompsa				    "Failed to setup control pipe: %s\n",
1074202181Sthompsa				    usbd_errstr(uerr));
1075202181Sthompsa				return (ENXIO);
1076202181Sthompsa			}
1077202181Sthompsa		}
1078202181Sthompsa	}
1079202181Sthompsa
1080202243Sthompsa	/* Setup the intr. endpoint */
1081202181Sthompsa	uerr = usbd_transfer_setup(sc->sc_udev,
1082202181Sthompsa	    &iface->idesc->bInterfaceNumber, sc->sc_xfer,
1083202181Sthompsa	    uhso_mux_config, 1, sc, &sc->sc_mtx);
1084202181Sthompsa	if (uerr)
1085202181Sthompsa		return (ENXIO);
1086202181Sthompsa
1087202181Sthompsa	return (0);
1088202181Sthompsa}
1089202181Sthompsa
1090202243Sthompsa/*
1091202243Sthompsa * Interrupt callback for the multiplexed serial port. Indicates
1092202270Sthompsa * which serial port has data waiting.
1093202243Sthompsa */
1094202181Sthompsastatic void
1095202181Sthompsauhso_mux_intr_callback(struct usb_xfer *xfer, usb_error_t error)
1096202181Sthompsa{
1097202181Sthompsa	struct usb_page_cache *pc;
1098202181Sthompsa	struct usb_page_search res;
1099202181Sthompsa	struct uhso_softc *sc = usbd_xfer_softc(xfer);
1100202181Sthompsa	unsigned int i, mux;
1101202181Sthompsa
1102202181Sthompsa	UHSO_DPRINTF(3, "status %d\n", USB_GET_STATE(xfer));
1103202181Sthompsa
1104202181Sthompsa	switch (USB_GET_STATE(xfer)) {
1105202181Sthompsa	case USB_ST_TRANSFERRED:
1106202181Sthompsa		/*
1107202181Sthompsa		 * The multiplexed port number can be found at the first byte.
1108202181Sthompsa		 * It contains a bit mask, we transform this in to an integer.
1109202181Sthompsa		 */
1110202181Sthompsa		pc = usbd_xfer_get_frame(xfer, 0);
1111202181Sthompsa		usbd_get_page(pc, 0, &res);
1112202181Sthompsa
1113202181Sthompsa		i = *((unsigned char *)res.buffer);
1114202181Sthompsa		mux = 0;
1115202181Sthompsa		while (i >>= 1) {
1116202181Sthompsa			mux++;
1117202181Sthompsa		}
1118202181Sthompsa
1119202181Sthompsa		UHSO_DPRINTF(3, "mux port %d (%d)\n", mux, i);
1120202181Sthompsa		if (mux > UHSO_MPORT_TYPE_NOMAX)
1121202181Sthompsa			break;
1122202181Sthompsa
1123202243Sthompsa		/* Issue a read for this serial port */
1124202181Sthompsa		usbd_xfer_set_priv(
1125202181Sthompsa		    sc->sc_tty[mux].ht_xfer[UHSO_CTRL_READ],
1126202181Sthompsa		    &sc->sc_tty[mux]);
1127202181Sthompsa		usbd_transfer_start(sc->sc_tty[mux].ht_xfer[UHSO_CTRL_READ]);
1128202181Sthompsa
1129202181Sthompsa		break;
1130202181Sthompsa	case USB_ST_SETUP:
1131202181Sthompsatr_setup:
1132202181Sthompsa		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
1133202181Sthompsa		usbd_transfer_submit(xfer);
1134202181Sthompsa		break;
1135202181Sthompsa	default:
1136202181Sthompsa		UHSO_DPRINTF(0, "error: %s\n", usbd_errstr(error));
1137202181Sthompsa		if (error == USB_ERR_CANCELLED)
1138202181Sthompsa			break;
1139202181Sthompsa
1140202181Sthompsa		usbd_xfer_set_stall(xfer);
1141202181Sthompsa		goto tr_setup;
1142202181Sthompsa	}
1143202181Sthompsa}
1144202181Sthompsa
1145202181Sthompsastatic void
1146202181Sthompsauhso_mux_read_callback(struct usb_xfer *xfer, usb_error_t error)
1147202181Sthompsa{
1148202181Sthompsa	struct uhso_softc *sc = usbd_xfer_softc(xfer);
1149202181Sthompsa	struct usb_page_cache *pc;
1150202181Sthompsa	struct usb_device_request req;
1151202181Sthompsa	struct uhso_tty *ht;
1152202181Sthompsa	int actlen, len;
1153202181Sthompsa
1154202181Sthompsa	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
1155202181Sthompsa
1156202181Sthompsa	UHSO_DPRINTF(3, "status %d\n", USB_GET_STATE(xfer));
1157202181Sthompsa
1158202181Sthompsa	ht = usbd_xfer_get_priv(xfer);
1159202181Sthompsa	UHSO_DPRINTF(3, "ht=%p open=%d\n", ht, ht->ht_open);
1160202181Sthompsa
1161202181Sthompsa	switch (USB_GET_STATE(xfer)) {
1162202181Sthompsa	case USB_ST_TRANSFERRED:
1163202181Sthompsa		/* Got data, send to ucom */
1164202181Sthompsa		pc = usbd_xfer_get_frame(xfer, 1);
1165202181Sthompsa		len = usbd_xfer_frame_len(xfer, 1);
1166202181Sthompsa
1167202181Sthompsa		UHSO_DPRINTF(3, "got %d bytes on mux port %d\n", len,
1168202181Sthompsa		    ht->ht_muxport);
1169202181Sthompsa		if (len <= 0) {
1170202181Sthompsa			usbd_transfer_start(sc->sc_xfer[UHSO_MUX_ENDPT_INTR]);
1171202181Sthompsa			break;
1172202181Sthompsa		}
1173202181Sthompsa
1174202181Sthompsa		/* Deliver data if the TTY is open, discard otherwise */
1175202181Sthompsa		if (ht->ht_open)
1176202181Sthompsa			ucom_put_data(&sc->sc_ucom[ht->ht_muxport], pc, 0, len);
1177202181Sthompsa		/* FALLTHROUGH */
1178202181Sthompsa	case USB_ST_SETUP:
1179202181Sthompsatr_setup:
1180227461Shselasky		memset(&req, 0, sizeof(struct usb_device_request));
1181202181Sthompsa		req.bmRequestType = UT_READ_CLASS_INTERFACE;
1182202181Sthompsa		req.bRequest = UCDC_GET_ENCAPSULATED_RESPONSE;
1183202181Sthompsa		USETW(req.wValue, 0);
1184202181Sthompsa		USETW(req.wIndex, ht->ht_muxport);
1185202181Sthompsa		USETW(req.wLength, 1024);
1186202181Sthompsa
1187202181Sthompsa		pc = usbd_xfer_get_frame(xfer, 0);
1188202181Sthompsa		usbd_copy_in(pc, 0, &req, sizeof(req));
1189202181Sthompsa
1190202181Sthompsa		usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
1191202181Sthompsa		usbd_xfer_set_frame_len(xfer, 1, 1024);
1192202181Sthompsa		usbd_xfer_set_frames(xfer, 2);
1193202181Sthompsa		usbd_transfer_submit(xfer);
1194202181Sthompsa		break;
1195202181Sthompsa	default:
1196202181Sthompsa		UHSO_DPRINTF(0, "error: %s\n", usbd_errstr(error));
1197202181Sthompsa		if (error == USB_ERR_CANCELLED)
1198202181Sthompsa			break;
1199202181Sthompsa		usbd_xfer_set_stall(xfer);
1200202181Sthompsa		goto tr_setup;
1201202181Sthompsa	}
1202202181Sthompsa}
1203202181Sthompsa
1204202181Sthompsastatic void
1205202181Sthompsauhso_mux_write_callback(struct usb_xfer *xfer, usb_error_t error)
1206202181Sthompsa{
1207202181Sthompsa	struct uhso_softc *sc = usbd_xfer_softc(xfer);
1208202181Sthompsa	struct uhso_tty *ht;
1209202181Sthompsa	struct usb_page_cache *pc;
1210202181Sthompsa	struct usb_device_request req;
1211202181Sthompsa	int actlen;
1212202181Sthompsa	struct usb_page_search res;
1213202181Sthompsa
1214202181Sthompsa	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
1215202181Sthompsa
1216202181Sthompsa	ht = usbd_xfer_get_priv(xfer);
1217202181Sthompsa	UHSO_DPRINTF(3, "status=%d, using mux port %d\n",
1218202181Sthompsa	    USB_GET_STATE(xfer), ht->ht_muxport);
1219202181Sthompsa
1220202181Sthompsa	switch (USB_GET_STATE(xfer)) {
1221202181Sthompsa	case USB_ST_TRANSFERRED:
1222202181Sthompsa		UHSO_DPRINTF(3, "wrote %zd data bytes to muxport %d\n",
1223202181Sthompsa		    actlen - sizeof(struct usb_device_request) ,
1224202181Sthompsa		    ht->ht_muxport);
1225202181Sthompsa		/* FALLTHROUGH */
1226202181Sthompsa	case USB_ST_SETUP:
1227202181Sthompsa		pc = usbd_xfer_get_frame(xfer, 1);
1228202181Sthompsa		if (ucom_get_data(&sc->sc_ucom[ht->ht_muxport], pc,
1229202181Sthompsa		    0, 32, &actlen)) {
1230202181Sthompsa
1231202181Sthompsa			usbd_get_page(pc, 0, &res);
1232202181Sthompsa
1233227461Shselasky			memset(&req, 0, sizeof(struct usb_device_request));
1234202181Sthompsa			req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
1235202181Sthompsa			req.bRequest = UCDC_SEND_ENCAPSULATED_COMMAND;
1236202181Sthompsa			USETW(req.wValue, 0);
1237202181Sthompsa			USETW(req.wIndex, ht->ht_muxport);
1238202181Sthompsa			USETW(req.wLength, actlen);
1239202181Sthompsa
1240202181Sthompsa			pc = usbd_xfer_get_frame(xfer, 0);
1241202181Sthompsa			usbd_copy_in(pc, 0, &req, sizeof(req));
1242202181Sthompsa
1243202181Sthompsa			usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
1244202181Sthompsa			usbd_xfer_set_frame_len(xfer, 1, actlen);
1245202181Sthompsa			usbd_xfer_set_frames(xfer, 2);
1246202181Sthompsa
1247202181Sthompsa			UHSO_DPRINTF(3, "Prepared %d bytes for transmit "
1248202181Sthompsa			    "on muxport %d\n", actlen, ht->ht_muxport);
1249202181Sthompsa
1250202181Sthompsa			usbd_transfer_submit(xfer);
1251202181Sthompsa		}
1252202181Sthompsa		break;
1253202181Sthompsa	default:
1254202181Sthompsa		UHSO_DPRINTF(0, "error: %s\n", usbd_errstr(error));
1255202181Sthompsa		if (error == USB_ERR_CANCELLED)
1256202181Sthompsa			break;
1257202181Sthompsa		break;
1258202181Sthompsa	}
1259202181Sthompsa}
1260202181Sthompsa
1261202181Sthompsastatic int
1262202181Sthompsauhso_attach_bulkserial(struct uhso_softc *sc, struct usb_interface *iface,
1263202181Sthompsa    int type)
1264202181Sthompsa{
1265202181Sthompsa	usb_error_t uerr;
1266202181Sthompsa	int tty;
1267202181Sthompsa
1268202243Sthompsa	/* Try attaching RD/WR/INTR first */
1269202181Sthompsa	uerr = usbd_transfer_setup(sc->sc_udev,
1270202181Sthompsa	    &iface->idesc->bInterfaceNumber, sc->sc_xfer,
1271202181Sthompsa	    uhso_bs_config, UHSO_BULK_ENDPT_MAX, sc, &sc->sc_mtx);
1272202181Sthompsa	if (uerr) {
1273202181Sthompsa		/* Try only RD/WR */
1274202181Sthompsa		uerr = usbd_transfer_setup(sc->sc_udev,
1275202181Sthompsa		    &iface->idesc->bInterfaceNumber, sc->sc_xfer,
1276202181Sthompsa		    uhso_bs_config, UHSO_BULK_ENDPT_MAX - 1, sc, &sc->sc_mtx);
1277202181Sthompsa	}
1278202181Sthompsa	if (uerr) {
1279202181Sthompsa		UHSO_DPRINTF(0, "usbd_transfer_setup failed");
1280202181Sthompsa		return (-1);
1281202181Sthompsa	}
1282202181Sthompsa
1283202181Sthompsa	tty = uhso_alloc_tty(sc);
1284202181Sthompsa	if (tty < 0) {
1285202181Sthompsa		usbd_transfer_unsetup(sc->sc_xfer, UHSO_BULK_ENDPT_MAX);
1286202181Sthompsa		return (ENOMEM);
1287202181Sthompsa	}
1288202181Sthompsa
1289202181Sthompsa	sc->sc_tty[tty].ht_muxport = -1;
1290202181Sthompsa	return (0);
1291202181Sthompsa}
1292202181Sthompsa
1293202181Sthompsastatic void
1294202181Sthompsauhso_bs_read_callback(struct usb_xfer *xfer, usb_error_t error)
1295202181Sthompsa{
1296202181Sthompsa	struct uhso_softc *sc = usbd_xfer_softc(xfer);
1297202181Sthompsa	struct usb_page_cache *pc;
1298202181Sthompsa	int actlen;
1299202181Sthompsa
1300202181Sthompsa	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
1301202181Sthompsa
1302202181Sthompsa	UHSO_DPRINTF(3, "status %d, actlen=%d\n", USB_GET_STATE(xfer), actlen);
1303202181Sthompsa
1304202181Sthompsa	switch (USB_GET_STATE(xfer)) {
1305202181Sthompsa	case USB_ST_TRANSFERRED:
1306202181Sthompsa		pc = usbd_xfer_get_frame(xfer, 0);
1307202181Sthompsa		ucom_put_data(&sc->sc_ucom[0], pc, 0, actlen);
1308202181Sthompsa		/* FALLTHROUGH */
1309202181Sthompsa	case USB_ST_SETUP:
1310202181Sthompsatr_setup:
1311202181Sthompsa		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
1312202181Sthompsa		usbd_transfer_submit(xfer);
1313202181Sthompsa	break;
1314202181Sthompsa	default:
1315202181Sthompsa		UHSO_DPRINTF(0, "error: %s\n", usbd_errstr(error));
1316202181Sthompsa		if (error == USB_ERR_CANCELLED)
1317202181Sthompsa			break;
1318202181Sthompsa		usbd_xfer_set_stall(xfer);
1319202181Sthompsa		goto tr_setup;
1320202181Sthompsa	}
1321202181Sthompsa}
1322202181Sthompsa
1323202181Sthompsastatic void
1324202181Sthompsauhso_bs_write_callback(struct usb_xfer *xfer, usb_error_t error)
1325202181Sthompsa{
1326202181Sthompsa	struct uhso_softc *sc = usbd_xfer_softc(xfer);
1327202181Sthompsa	struct usb_page_cache *pc;
1328202181Sthompsa	int actlen;
1329202181Sthompsa
1330202181Sthompsa	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
1331202181Sthompsa
1332202181Sthompsa	UHSO_DPRINTF(3, "status %d, actlen=%d\n", USB_GET_STATE(xfer), actlen);
1333202181Sthompsa
1334202181Sthompsa	switch (USB_GET_STATE(xfer)) {
1335202181Sthompsa	case USB_ST_TRANSFERRED:
1336202181Sthompsa	case USB_ST_SETUP:
1337202181Sthompsatr_setup:
1338202181Sthompsa		pc = usbd_xfer_get_frame(xfer, 0);
1339202181Sthompsa		if (ucom_get_data(&sc->sc_ucom[0], pc, 0, 8192, &actlen)) {
1340202181Sthompsa			usbd_xfer_set_frame_len(xfer, 0, actlen);
1341202181Sthompsa			usbd_transfer_submit(xfer);
1342202181Sthompsa		}
1343202181Sthompsa		break;
1344202181Sthompsa	break;
1345202181Sthompsa	default:
1346202181Sthompsa		UHSO_DPRINTF(0, "error: %s\n", usbd_errstr(error));
1347202181Sthompsa		if (error == USB_ERR_CANCELLED)
1348202181Sthompsa			break;
1349202181Sthompsa		usbd_xfer_set_stall(xfer);
1350202181Sthompsa		goto tr_setup;
1351202181Sthompsa	}
1352202181Sthompsa}
1353202181Sthompsa
1354202181Sthompsastatic void
1355202181Sthompsauhso_bs_cfg(struct uhso_softc *sc)
1356202181Sthompsa{
1357202181Sthompsa	struct usb_device_request req;
1358202181Sthompsa	usb_error_t uerr;
1359202181Sthompsa
1360202181Sthompsa	if (!(UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_BULK))
1361202181Sthompsa		return;
1362202181Sthompsa
1363202181Sthompsa	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
1364202181Sthompsa	req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
1365202181Sthompsa	USETW(req.wValue, sc->sc_line);
1366202181Sthompsa	USETW(req.wIndex, sc->sc_iface_no);
1367202181Sthompsa	USETW(req.wLength, 0);
1368202181Sthompsa
1369202181Sthompsa	uerr = ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom[0], &req, NULL, 0, 1000);
1370202181Sthompsa	if (uerr != 0) {
1371202181Sthompsa		device_printf(sc->sc_dev, "failed to set ctrl line state to "
1372202181Sthompsa		    "0x%02x: %s\n", sc->sc_line, usbd_errstr(uerr));
1373202181Sthompsa	}
1374202181Sthompsa}
1375202181Sthompsa
1376202181Sthompsastatic void
1377202181Sthompsauhso_bs_intr_callback(struct usb_xfer *xfer, usb_error_t error)
1378202181Sthompsa{
1379202181Sthompsa	struct uhso_softc *sc = usbd_xfer_softc(xfer);
1380202181Sthompsa	struct usb_page_cache *pc;
1381202181Sthompsa	int actlen;
1382202181Sthompsa	struct usb_cdc_notification cdc;
1383202181Sthompsa
1384202181Sthompsa	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
1385202181Sthompsa	UHSO_DPRINTF(3, "status %d, actlen=%d\n", USB_GET_STATE(xfer), actlen);
1386202181Sthompsa
1387202181Sthompsa	switch (USB_GET_STATE(xfer)) {
1388202181Sthompsa	case USB_ST_TRANSFERRED:
1389202181Sthompsa		if (actlen < UCDC_NOTIFICATION_LENGTH) {
1390202181Sthompsa			UHSO_DPRINTF(0, "UCDC notification too short: %d\n", actlen);
1391202181Sthompsa			goto tr_setup;
1392202181Sthompsa		}
1393233774Shselasky		else if (actlen > (int)sizeof(struct usb_cdc_notification)) {
1394202181Sthompsa			UHSO_DPRINTF(0, "UCDC notification too large: %d\n", actlen);
1395202181Sthompsa			actlen = sizeof(struct usb_cdc_notification);
1396202181Sthompsa		}
1397202181Sthompsa
1398202181Sthompsa		pc = usbd_xfer_get_frame(xfer, 0);
1399202181Sthompsa		usbd_copy_out(pc, 0, &cdc, actlen);
1400202181Sthompsa
1401202181Sthompsa		if (UGETW(cdc.wIndex) != sc->sc_iface_no) {
1402202243Sthompsa			UHSO_DPRINTF(0, "Interface mismatch, got %d expected %d\n",
1403202181Sthompsa			    UGETW(cdc.wIndex), sc->sc_iface_no);
1404202181Sthompsa			goto tr_setup;
1405202181Sthompsa		}
1406202181Sthompsa
1407202181Sthompsa		if (cdc.bmRequestType == UCDC_NOTIFICATION &&
1408202181Sthompsa		    cdc.bNotification == UCDC_N_SERIAL_STATE) {
1409202243Sthompsa			UHSO_DPRINTF(2, "notify = 0x%02x\n", cdc.data[0]);
1410202181Sthompsa
1411202181Sthompsa			sc->sc_msr = 0;
1412202181Sthompsa			sc->sc_lsr = 0;
1413202181Sthompsa			if (cdc.data[0] & UCDC_N_SERIAL_RI)
1414202181Sthompsa				sc->sc_msr |= SER_RI;
1415202181Sthompsa			if (cdc.data[0] & UCDC_N_SERIAL_DSR)
1416202181Sthompsa				sc->sc_msr |= SER_DSR;
1417202181Sthompsa			if (cdc.data[0] & UCDC_N_SERIAL_DCD)
1418202181Sthompsa				sc->sc_msr |= SER_DCD;
1419202181Sthompsa
1420202181Sthompsa			ucom_status_change(&sc->sc_ucom[0]);
1421202181Sthompsa		}
1422202181Sthompsa	case USB_ST_SETUP:
1423202181Sthompsatr_setup:
1424202181Sthompsa	default:
1425202181Sthompsa		if (error == USB_ERR_CANCELLED)
1426202181Sthompsa			break;
1427202181Sthompsa		usbd_xfer_set_stall(xfer);
1428202181Sthompsa		goto tr_setup;
1429202181Sthompsa	}
1430202181Sthompsa}
1431202181Sthompsa
1432202181Sthompsastatic void
1433202181Sthompsauhso_ucom_cfg_get_status(struct ucom_softc *ucom, uint8_t *lsr, uint8_t *msr)
1434202181Sthompsa{
1435202181Sthompsa	struct uhso_softc *sc = ucom->sc_parent;
1436202181Sthompsa
1437202181Sthompsa	*lsr = sc->sc_lsr;
1438202181Sthompsa	*msr = sc->sc_msr;
1439202181Sthompsa}
1440202181Sthompsa
1441202181Sthompsastatic void
1442202181Sthompsauhso_ucom_cfg_set_dtr(struct ucom_softc *ucom, uint8_t onoff)
1443202181Sthompsa{
1444202181Sthompsa	struct uhso_softc *sc = ucom->sc_parent;
1445202181Sthompsa
1446202181Sthompsa	if (!(UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_BULK))
1447202181Sthompsa		return;
1448202181Sthompsa
1449202181Sthompsa	if (onoff)
1450202181Sthompsa		sc->sc_line |= UCDC_LINE_DTR;
1451202181Sthompsa	else
1452208017Sthompsa		sc->sc_line &= ~UCDC_LINE_DTR;
1453202181Sthompsa
1454202181Sthompsa	uhso_bs_cfg(sc);
1455202181Sthompsa}
1456202181Sthompsa
1457202181Sthompsastatic void
1458202181Sthompsauhso_ucom_cfg_set_rts(struct ucom_softc *ucom, uint8_t onoff)
1459202181Sthompsa{
1460202181Sthompsa	struct uhso_softc *sc = ucom->sc_parent;
1461202181Sthompsa
1462202181Sthompsa	if (!(UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_BULK))
1463202181Sthompsa		return;
1464202181Sthompsa
1465202181Sthompsa	if (onoff)
1466202181Sthompsa		sc->sc_line |= UCDC_LINE_RTS;
1467202181Sthompsa	else
1468208017Sthompsa		sc->sc_line &= ~UCDC_LINE_RTS;
1469202181Sthompsa
1470202181Sthompsa	uhso_bs_cfg(sc);
1471202181Sthompsa}
1472202181Sthompsa
1473202181Sthompsastatic void
1474202181Sthompsauhso_ucom_start_read(struct ucom_softc *ucom)
1475202181Sthompsa{
1476202181Sthompsa	struct uhso_softc *sc = ucom->sc_parent;
1477202181Sthompsa
1478214761Sn_hibma	UHSO_DPRINTF(3, "unit=%d, subunit=%d\n",
1479214761Sn_hibma	    ucom->sc_super->sc_unit, ucom->sc_subunit);
1480202181Sthompsa
1481202181Sthompsa	if (UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_MUX) {
1482214761Sn_hibma		sc->sc_tty[ucom->sc_subunit].ht_open = 1;
1483202181Sthompsa		usbd_transfer_start(sc->sc_xfer[UHSO_MUX_ENDPT_INTR]);
1484202181Sthompsa	}
1485202181Sthompsa	else if (UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_BULK) {
1486202181Sthompsa		sc->sc_tty[0].ht_open = 1;
1487202181Sthompsa		usbd_transfer_start(sc->sc_xfer[UHSO_BULK_ENDPT_READ]);
1488202181Sthompsa		if (sc->sc_xfer[UHSO_BULK_ENDPT_INTR] != NULL)
1489202181Sthompsa			usbd_transfer_start(sc->sc_xfer[UHSO_BULK_ENDPT_INTR]);
1490202181Sthompsa	}
1491202181Sthompsa}
1492202181Sthompsa
1493202181Sthompsastatic void
1494202181Sthompsauhso_ucom_stop_read(struct ucom_softc *ucom)
1495202181Sthompsa{
1496202181Sthompsa
1497202181Sthompsa	struct uhso_softc *sc = ucom->sc_parent;
1498202181Sthompsa
1499202181Sthompsa	if (UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_MUX) {
1500214761Sn_hibma		sc->sc_tty[ucom->sc_subunit].ht_open = 0;
1501202181Sthompsa		usbd_transfer_stop(
1502214761Sn_hibma		    sc->sc_tty[ucom->sc_subunit].ht_xfer[UHSO_CTRL_READ]);
1503202181Sthompsa	}
1504202181Sthompsa	else if (UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_BULK) {
1505202181Sthompsa		sc->sc_tty[0].ht_open = 0;
1506202181Sthompsa		usbd_transfer_start(sc->sc_xfer[UHSO_BULK_ENDPT_READ]);
1507202181Sthompsa		if (sc->sc_xfer[UHSO_BULK_ENDPT_INTR] != NULL)
1508202181Sthompsa			usbd_transfer_stop(sc->sc_xfer[UHSO_BULK_ENDPT_INTR]);
1509202181Sthompsa	}
1510202181Sthompsa}
1511202181Sthompsa
1512202181Sthompsastatic void
1513202181Sthompsauhso_ucom_start_write(struct ucom_softc *ucom)
1514202181Sthompsa{
1515202181Sthompsa	struct uhso_softc *sc = ucom->sc_parent;
1516202181Sthompsa
1517202181Sthompsa	if (UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_MUX) {
1518214761Sn_hibma		UHSO_DPRINTF(3, "local unit %d\n", ucom->sc_subunit);
1519202181Sthompsa
1520202181Sthompsa		usbd_transfer_start(sc->sc_xfer[UHSO_MUX_ENDPT_INTR]);
1521202181Sthompsa
1522202181Sthompsa		usbd_xfer_set_priv(
1523214761Sn_hibma		    sc->sc_tty[ucom->sc_subunit].ht_xfer[UHSO_CTRL_WRITE],
1524214761Sn_hibma		    &sc->sc_tty[ucom->sc_subunit]);
1525202181Sthompsa		usbd_transfer_start(
1526214761Sn_hibma		    sc->sc_tty[ucom->sc_subunit].ht_xfer[UHSO_CTRL_WRITE]);
1527202181Sthompsa
1528202181Sthompsa	}
1529202181Sthompsa	else if (UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_BULK) {
1530202181Sthompsa		usbd_transfer_start(sc->sc_xfer[UHSO_BULK_ENDPT_WRITE]);
1531202181Sthompsa	}
1532202181Sthompsa}
1533202181Sthompsa
1534202181Sthompsastatic void
1535202181Sthompsauhso_ucom_stop_write(struct ucom_softc *ucom)
1536202181Sthompsa{
1537202181Sthompsa	struct uhso_softc *sc = ucom->sc_parent;
1538202181Sthompsa
1539202181Sthompsa	if (UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_MUX) {
1540202181Sthompsa		usbd_transfer_stop(
1541214761Sn_hibma		    sc->sc_tty[ucom->sc_subunit].ht_xfer[UHSO_CTRL_WRITE]);
1542202181Sthompsa	}
1543202181Sthompsa	else if (UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_BULK) {
1544202181Sthompsa		usbd_transfer_stop(sc->sc_xfer[UHSO_BULK_ENDPT_WRITE]);
1545202181Sthompsa	}
1546202181Sthompsa}
1547202181Sthompsa
1548210275Sthompsastatic int
1549210275Sthompsauhso_attach_ifnet(struct uhso_softc *sc, struct usb_interface *iface, int type)
1550202181Sthompsa{
1551202181Sthompsa	struct ifnet *ifp;
1552202181Sthompsa	usb_error_t uerr;
1553202181Sthompsa	struct sysctl_ctx_list *sctx;
1554202181Sthompsa	struct sysctl_oid *soid;
1555210275Sthompsa	unsigned int devunit;
1556202181Sthompsa
1557202181Sthompsa	uerr = usbd_transfer_setup(sc->sc_udev,
1558202181Sthompsa	    &iface->idesc->bInterfaceNumber, sc->sc_if_xfer,
1559202181Sthompsa	    uhso_ifnet_config, UHSO_IFNET_MAX, sc, &sc->sc_mtx);
1560202181Sthompsa	if (uerr) {
1561202181Sthompsa		UHSO_DPRINTF(0, "usbd_transfer_setup failed: %s\n",
1562202181Sthompsa		    usbd_errstr(uerr));
1563202181Sthompsa		return (-1);
1564202181Sthompsa	}
1565202181Sthompsa
1566202243Sthompsa	sc->sc_ifp = ifp = if_alloc(IFT_OTHER);
1567202181Sthompsa	if (sc->sc_ifp == NULL) {
1568202181Sthompsa		device_printf(sc->sc_dev, "if_alloc() failed\n");
1569202181Sthompsa		return (-1);
1570202181Sthompsa	}
1571202181Sthompsa
1572202181Sthompsa	callout_init_mtx(&sc->sc_c, &sc->sc_mtx, 0);
1573202181Sthompsa	mtx_lock(&sc->sc_mtx);
1574202181Sthompsa	callout_reset(&sc->sc_c, 1, uhso_if_rxflush, sc);
1575202181Sthompsa	mtx_unlock(&sc->sc_mtx);
1576202181Sthompsa
1577210275Sthompsa	/*
1578210275Sthompsa	 * We create our own unit numbers for ifnet devices because the
1579210275Sthompsa	 * USB interface unit numbers can be at arbitrary positions yielding
1580210275Sthompsa	 * odd looking device names.
1581210275Sthompsa	 */
1582210275Sthompsa	devunit = alloc_unr(uhso_ifnet_unit);
1583210275Sthompsa
1584210275Sthompsa	if_initname(ifp, device_get_name(sc->sc_dev), devunit);
1585202181Sthompsa	ifp->if_mtu = UHSO_MAX_MTU;
1586202181Sthompsa	ifp->if_ioctl = uhso_if_ioctl;
1587202181Sthompsa	ifp->if_init = uhso_if_init;
1588202181Sthompsa	ifp->if_start = uhso_if_start;
1589202181Sthompsa	ifp->if_output = uhso_if_output;
1590213803Shselasky	ifp->if_flags = IFF_BROADCAST | IFF_MULTICAST | IFF_NOARP;
1591202181Sthompsa	ifp->if_softc = sc;
1592207554Ssobomax	IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
1593207554Ssobomax	ifp->if_snd.ifq_drv_maxlen = ifqmaxlen;
1594202181Sthompsa	IFQ_SET_READY(&ifp->if_snd);
1595202181Sthompsa
1596202181Sthompsa	if_attach(ifp);
1597202181Sthompsa	bpfattach(ifp, DLT_RAW, 0);
1598202181Sthompsa
1599202181Sthompsa	sctx = device_get_sysctl_ctx(sc->sc_dev);
1600202181Sthompsa	soid = device_get_sysctl_tree(sc->sc_dev);
1601202181Sthompsa	/* Unlocked read... */
1602202181Sthompsa	SYSCTL_ADD_STRING(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "netif",
1603202181Sthompsa	    CTLFLAG_RD, ifp->if_xname, 0, "Attached network interface");
1604202181Sthompsa
1605202181Sthompsa	return (0);
1606202181Sthompsa}
1607202181Sthompsa
1608202181Sthompsastatic void
1609202181Sthompsauhso_ifnet_read_callback(struct usb_xfer *xfer, usb_error_t error)
1610202181Sthompsa{
1611202181Sthompsa	struct uhso_softc *sc = usbd_xfer_softc(xfer);
1612202181Sthompsa	struct mbuf *m;
1613202181Sthompsa	struct usb_page_cache *pc;
1614202181Sthompsa	int actlen;
1615202181Sthompsa
1616202181Sthompsa	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
1617202181Sthompsa
1618202181Sthompsa	UHSO_DPRINTF(3, "status=%d, actlen=%d\n", USB_GET_STATE(xfer), actlen);
1619202181Sthompsa
1620202181Sthompsa	switch (USB_GET_STATE(xfer)) {
1621202181Sthompsa	case USB_ST_TRANSFERRED:
1622202181Sthompsa		if (actlen > 0 && (sc->sc_ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1623202181Sthompsa			pc = usbd_xfer_get_frame(xfer, 0);
1624202181Sthompsa			m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
1625202181Sthompsa			usbd_copy_out(pc, 0, mtod(m, uint8_t *), actlen);
1626202181Sthompsa			m->m_pkthdr.len = m->m_len = actlen;
1627202243Sthompsa			/* Enqueue frame for further processing */
1628202181Sthompsa			_IF_ENQUEUE(&sc->sc_rxq, m);
1629202181Sthompsa			if (!callout_pending(&sc->sc_c) ||
1630202181Sthompsa			    !callout_active(&sc->sc_c)) {
1631202181Sthompsa				callout_schedule(&sc->sc_c, 1);
1632202181Sthompsa			}
1633202181Sthompsa		}
1634202181Sthompsa	/* FALLTHROUGH */
1635202181Sthompsa	case USB_ST_SETUP:
1636202181Sthompsatr_setup:
1637202181Sthompsa		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
1638202181Sthompsa		usbd_transfer_submit(xfer);
1639202181Sthompsa		break;
1640202181Sthompsa	default:
1641202181Sthompsa		UHSO_DPRINTF(0, "error: %s\n", usbd_errstr(error));
1642202181Sthompsa		if (error == USB_ERR_CANCELLED)
1643202181Sthompsa			break;
1644202181Sthompsa		usbd_xfer_set_stall(xfer);
1645202181Sthompsa		goto tr_setup;
1646202181Sthompsa	}
1647202181Sthompsa}
1648202181Sthompsa
1649202181Sthompsa/*
1650202243Sthompsa * Deferred RX processing, called with mutex locked.
1651202243Sthompsa *
1652202270Sthompsa * Each frame we receive might contain several small ip-packets as well
1653202243Sthompsa * as partial ip-packets. We need to separate/assemble them into individual
1654202243Sthompsa * packets before sending them to the ip-layer.
1655202181Sthompsa */
1656202181Sthompsastatic void
1657202181Sthompsauhso_if_rxflush(void *arg)
1658202181Sthompsa{
1659202181Sthompsa	struct uhso_softc *sc = arg;
1660202181Sthompsa	struct ifnet *ifp = sc->sc_ifp;
1661202181Sthompsa	uint8_t *cp;
1662202181Sthompsa	struct mbuf *m, *m0, *mwait;
1663202181Sthompsa	struct ip *ip;
1664202181Sthompsa#ifdef INET6
1665202181Sthompsa	struct ip6_hdr *ip6;
1666202181Sthompsa#endif
1667202181Sthompsa	uint16_t iplen;
1668202181Sthompsa	int len, isr;
1669202181Sthompsa
1670202181Sthompsa	m = NULL;
1671202181Sthompsa	mwait = sc->sc_mwait;
1672202181Sthompsa	for (;;) {
1673202181Sthompsa		if (m == NULL) {
1674202181Sthompsa			_IF_DEQUEUE(&sc->sc_rxq, m);
1675202181Sthompsa			if (m == NULL)
1676202181Sthompsa				break;
1677202243Sthompsa			UHSO_DPRINTF(3, "dequeue m=%p, len=%d\n", m, m->m_len);
1678202181Sthompsa		}
1679202181Sthompsa		mtx_unlock(&sc->sc_mtx);
1680202181Sthompsa
1681202181Sthompsa		/* Do we have a partial packet waiting? */
1682202181Sthompsa		if (mwait != NULL) {
1683202181Sthompsa			m0 = mwait;
1684202181Sthompsa			mwait = NULL;
1685202181Sthompsa
1686202243Sthompsa			UHSO_DPRINTF(3, "partial m0=%p(%d), concat w/ m=%p(%d)\n",
1687202181Sthompsa			    m0, m0->m_len, m, m->m_len);
1688202181Sthompsa			len = m->m_len + m0->m_len;
1689202181Sthompsa
1690202181Sthompsa			/* Concat mbufs and fix headers */
1691202181Sthompsa			m_cat(m0, m);
1692202181Sthompsa			m0->m_pkthdr.len = len;
1693202181Sthompsa			m->m_flags &= ~M_PKTHDR;
1694202181Sthompsa
1695202181Sthompsa			m = m_pullup(m0, sizeof(struct ip));
1696202181Sthompsa			if (m == NULL) {
1697202181Sthompsa				ifp->if_ierrors++;
1698202181Sthompsa				UHSO_DPRINTF(0, "m_pullup failed\n");
1699202181Sthompsa				mtx_lock(&sc->sc_mtx);
1700202181Sthompsa				continue;
1701202181Sthompsa			}
1702202243Sthompsa			UHSO_DPRINTF(3, "Constructed mbuf=%p, len=%d\n",
1703202181Sthompsa			    m, m->m_pkthdr.len);
1704202181Sthompsa		}
1705202181Sthompsa
1706202181Sthompsa		cp = mtod(m, uint8_t *);
1707202181Sthompsa		ip = (struct ip *)cp;
1708202181Sthompsa#ifdef INET6
1709202181Sthompsa		ip6 = (struct ip6_hdr *)cp;
1710202181Sthompsa#endif
1711202181Sthompsa
1712202181Sthompsa		/* Check for IPv4 */
1713202181Sthompsa		if (ip->ip_v == IPVERSION) {
1714202181Sthompsa			iplen = htons(ip->ip_len);
1715202181Sthompsa			isr = NETISR_IP;
1716202181Sthompsa		}
1717202181Sthompsa#ifdef INET6
1718202181Sthompsa		/* Check for IPv6 */
1719202181Sthompsa		else if ((ip6->ip6_vfc & IPV6_VERSION_MASK) == IPV6_VERSION) {
1720202181Sthompsa			iplen = htons(ip6->ip6_plen);
1721202181Sthompsa			isr = NETISR_IPV6;
1722202181Sthompsa		}
1723202181Sthompsa#endif
1724202181Sthompsa		else {
1725202181Sthompsa			UHSO_DPRINTF(0, "got unexpected ip version %d, "
1726202181Sthompsa			    "m=%p, len=%d\n", (*cp & 0xf0) >> 4, m, m->m_len);
1727202181Sthompsa			ifp->if_ierrors++;
1728202181Sthompsa			UHSO_HEXDUMP(cp, 4);
1729202181Sthompsa			m_freem(m);
1730202181Sthompsa			m = NULL;
1731202181Sthompsa			mtx_lock(&sc->sc_mtx);
1732202181Sthompsa			continue;
1733202181Sthompsa		}
1734202181Sthompsa
1735202181Sthompsa		if (iplen == 0) {
1736202181Sthompsa			UHSO_DPRINTF(0, "Zero IP length\n");
1737202181Sthompsa			ifp->if_ierrors++;
1738202181Sthompsa			m_freem(m);
1739202181Sthompsa			m = NULL;
1740202181Sthompsa			mtx_lock(&sc->sc_mtx);
1741202181Sthompsa			continue;
1742202181Sthompsa		}
1743202181Sthompsa
1744202243Sthompsa		UHSO_DPRINTF(3, "m=%p, len=%d, cp=%p, iplen=%d\n",
1745202181Sthompsa		    m, m->m_pkthdr.len, cp, iplen);
1746202181Sthompsa
1747202181Sthompsa		m0 = NULL;
1748202181Sthompsa
1749202181Sthompsa		/* More IP packets in this mbuf */
1750202181Sthompsa		if (iplen < m->m_pkthdr.len) {
1751202181Sthompsa			m0 = m;
1752202181Sthompsa
1753202181Sthompsa			/*
1754202181Sthompsa			 * Allocate a new mbuf for this IP packet and
1755202181Sthompsa			 * copy the IP-packet into it.
1756202181Sthompsa			 */
1757202181Sthompsa			m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
1758227461Shselasky			memcpy(mtod(m, uint8_t *), mtod(m0, uint8_t *), iplen);
1759202181Sthompsa			m->m_pkthdr.len = m->m_len = iplen;
1760202181Sthompsa
1761202181Sthompsa			/* Adjust the size of the original mbuf */
1762202181Sthompsa			m_adj(m0, iplen);
1763202181Sthompsa			m0 = m_defrag(m0, M_WAIT);
1764202181Sthompsa
1765202243Sthompsa			UHSO_DPRINTF(3, "New mbuf=%p, len=%d/%d, m0=%p, "
1766202181Sthompsa			    "m0_len=%d/%d\n", m, m->m_pkthdr.len, m->m_len,
1767202181Sthompsa			    m0, m0->m_pkthdr.len, m0->m_len);
1768202181Sthompsa		}
1769202181Sthompsa		else if (iplen > m->m_pkthdr.len) {
1770202243Sthompsa			UHSO_DPRINTF(3, "Deferred mbuf=%p, len=%d\n",
1771202181Sthompsa			    m, m->m_pkthdr.len);
1772202181Sthompsa			mwait = m;
1773202181Sthompsa			m = NULL;
1774202181Sthompsa			mtx_lock(&sc->sc_mtx);
1775202181Sthompsa			continue;
1776202181Sthompsa		}
1777202181Sthompsa
1778202181Sthompsa		ifp->if_ipackets++;
1779202181Sthompsa		m->m_pkthdr.rcvif = ifp;
1780202181Sthompsa
1781202181Sthompsa		/* Dispatch to IP layer */
1782202181Sthompsa		BPF_MTAP(sc->sc_ifp, m);
1783223741Sbz		M_SETFIB(m, ifp->if_fib);
1784202181Sthompsa		netisr_dispatch(isr, m);
1785202181Sthompsa		m = m0 != NULL ? m0 : NULL;
1786202181Sthompsa		mtx_lock(&sc->sc_mtx);
1787202181Sthompsa	}
1788202181Sthompsa	sc->sc_mwait = mwait;
1789202181Sthompsa}
1790202181Sthompsa
1791202181Sthompsastatic void
1792202181Sthompsauhso_ifnet_write_callback(struct usb_xfer *xfer, usb_error_t error)
1793202181Sthompsa{
1794202181Sthompsa	struct uhso_softc *sc = usbd_xfer_softc(xfer);
1795202181Sthompsa	struct ifnet *ifp = sc->sc_ifp;
1796202181Sthompsa	struct usb_page_cache *pc;
1797202181Sthompsa	struct mbuf *m;
1798202181Sthompsa	int actlen;
1799202181Sthompsa
1800202181Sthompsa	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
1801202181Sthompsa
1802202181Sthompsa	UHSO_DPRINTF(3, "status %d, actlen=%d\n", USB_GET_STATE(xfer), actlen);
1803202181Sthompsa
1804202181Sthompsa	switch (USB_GET_STATE(xfer)) {
1805202181Sthompsa	case USB_ST_TRANSFERRED:
1806202181Sthompsa		ifp->if_opackets++;
1807202181Sthompsa		ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1808202181Sthompsa	case USB_ST_SETUP:
1809202181Sthompsatr_setup:
1810202181Sthompsa		IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
1811202181Sthompsa		if (m == NULL)
1812202181Sthompsa			break;
1813202181Sthompsa
1814202181Sthompsa		ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1815202181Sthompsa
1816202181Sthompsa		if (m->m_pkthdr.len > MCLBYTES)
1817202181Sthompsa			m->m_pkthdr.len = MCLBYTES;
1818202181Sthompsa
1819202181Sthompsa		usbd_xfer_set_frame_len(xfer, 0, m->m_pkthdr.len);
1820202181Sthompsa		pc = usbd_xfer_get_frame(xfer, 0);
1821202181Sthompsa		usbd_m_copy_in(pc, 0, m, 0, m->m_pkthdr.len);
1822202181Sthompsa		usbd_transfer_submit(xfer);
1823202181Sthompsa
1824202181Sthompsa		BPF_MTAP(ifp, m);
1825202181Sthompsa		m_freem(m);
1826202181Sthompsa		break;
1827202181Sthompsa	default:
1828202181Sthompsa		UHSO_DPRINTF(0, "error: %s\n", usbd_errstr(error));
1829202181Sthompsa		if (error == USB_ERR_CANCELLED)
1830202181Sthompsa			break;
1831202181Sthompsa		usbd_xfer_set_stall(xfer);
1832202181Sthompsa		goto tr_setup;
1833202181Sthompsa	}
1834202181Sthompsa}
1835202181Sthompsa
1836202181Sthompsastatic int
1837202181Sthompsauhso_if_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1838202181Sthompsa{
1839202181Sthompsa	struct uhso_softc *sc;
1840202181Sthompsa
1841202181Sthompsa	sc = ifp->if_softc;
1842202181Sthompsa
1843202181Sthompsa	switch (cmd) {
1844202181Sthompsa	case SIOCSIFFLAGS:
1845202181Sthompsa		if (ifp->if_flags & IFF_UP) {
1846202181Sthompsa			if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1847202181Sthompsa				uhso_if_init(sc);
1848202181Sthompsa			}
1849202181Sthompsa		}
1850202181Sthompsa		else {
1851202181Sthompsa			if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1852202181Sthompsa				mtx_lock(&sc->sc_mtx);
1853202181Sthompsa				uhso_if_stop(sc);
1854202181Sthompsa				mtx_unlock(&sc->sc_mtx);
1855202181Sthompsa			}
1856202181Sthompsa		}
1857202181Sthompsa		break;
1858202181Sthompsa	case SIOCSIFADDR:
1859202181Sthompsa	case SIOCSIFDSTADDR:
1860202181Sthompsa	case SIOCADDMULTI:
1861202181Sthompsa	case SIOCDELMULTI:
1862202181Sthompsa		break;
1863202181Sthompsa	default:
1864202181Sthompsa		return (EINVAL);
1865202181Sthompsa	}
1866202181Sthompsa	return (0);
1867202181Sthompsa}
1868202181Sthompsa
1869202181Sthompsastatic void
1870202181Sthompsauhso_if_init(void *priv)
1871202181Sthompsa{
1872202181Sthompsa	struct uhso_softc *sc = priv;
1873202181Sthompsa	struct ifnet *ifp = sc->sc_ifp;
1874202181Sthompsa
1875202181Sthompsa	mtx_lock(&sc->sc_mtx);
1876202181Sthompsa	uhso_if_stop(sc);
1877202181Sthompsa	ifp = sc->sc_ifp;
1878202181Sthompsa	ifp->if_flags |= IFF_UP;
1879202181Sthompsa	ifp->if_drv_flags |= IFF_DRV_RUNNING;
1880202181Sthompsa	mtx_unlock(&sc->sc_mtx);
1881202181Sthompsa
1882202243Sthompsa	UHSO_DPRINTF(2, "ifnet initialized\n");
1883202181Sthompsa}
1884202181Sthompsa
1885202181Sthompsastatic int
1886202181Sthompsauhso_if_output(struct ifnet *ifp, struct mbuf *m0, struct sockaddr *dst,
1887202181Sthompsa    struct route *ro)
1888202181Sthompsa{
1889202181Sthompsa	int error;
1890202181Sthompsa
1891202181Sthompsa	/* Only IPv4/6 support */
1892202181Sthompsa	if (dst->sa_family != AF_INET
1893202181Sthompsa#ifdef INET6
1894202181Sthompsa	   && dst->sa_family != AF_INET6
1895202181Sthompsa#endif
1896202181Sthompsa	 ) {
1897202181Sthompsa		return (EAFNOSUPPORT);
1898202181Sthompsa	}
1899202181Sthompsa
1900202181Sthompsa	error = (ifp->if_transmit)(ifp, m0);
1901202181Sthompsa	if (error) {
1902202181Sthompsa		ifp->if_oerrors++;
1903202181Sthompsa		return (ENOBUFS);
1904202181Sthompsa	}
1905202181Sthompsa	ifp->if_opackets++;
1906202181Sthompsa	return (0);
1907202181Sthompsa}
1908202181Sthompsa
1909202181Sthompsastatic void
1910202181Sthompsauhso_if_start(struct ifnet *ifp)
1911202181Sthompsa{
1912202181Sthompsa	struct uhso_softc *sc = ifp->if_softc;
1913202181Sthompsa
1914202181Sthompsa	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
1915202181Sthompsa		UHSO_DPRINTF(1, "Not running\n");
1916202181Sthompsa		return;
1917202181Sthompsa	}
1918202181Sthompsa
1919202181Sthompsa	mtx_lock(&sc->sc_mtx);
1920202181Sthompsa	usbd_transfer_start(sc->sc_if_xfer[UHSO_IFNET_READ]);
1921202181Sthompsa	usbd_transfer_start(sc->sc_if_xfer[UHSO_IFNET_WRITE]);
1922202181Sthompsa	mtx_unlock(&sc->sc_mtx);
1923202181Sthompsa	UHSO_DPRINTF(3, "interface started\n");
1924202181Sthompsa}
1925202181Sthompsa
1926202181Sthompsastatic void
1927202181Sthompsauhso_if_stop(struct uhso_softc *sc)
1928202181Sthompsa{
1929202181Sthompsa
1930202181Sthompsa	usbd_transfer_stop(sc->sc_if_xfer[UHSO_IFNET_READ]);
1931202181Sthompsa	usbd_transfer_stop(sc->sc_if_xfer[UHSO_IFNET_WRITE]);
1932202181Sthompsa	sc->sc_ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
1933202181Sthompsa}
1934