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$");
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),
272260534Shselasky	/* Option GTM661W */
273260534Shselasky	UHSO_DEV(OPTION, GTM661W, UHSO_AUTO_IFACE),
274202181Sthompsa	/* Option iCON EDGE */
275202181Sthompsa	UHSO_DEV(OPTION, ICONEDGE, UHSO_STATIC_IFACE),
276202181Sthompsa	/* Option Module HSxPA */
277202181Sthompsa	UHSO_DEV(OPTION, MODHSXPA, UHSO_STATIC_IFACE),
278202181Sthompsa	/* Option iCON 321 */
279202181Sthompsa	UHSO_DEV(OPTION, ICON321, UHSO_STATIC_IFACE),
280202181Sthompsa	/* Option iCON 322 */
281202243Sthompsa	UHSO_DEV(OPTION, GTICON322, UHSO_STATIC_IFACE),
282202243Sthompsa	/* Option iCON 505 */
283202243Sthompsa	UHSO_DEV(OPTION, ICON505, UHSO_AUTO_IFACE),
284210275Sthompsa	/* Option iCON 452 */
285210275Sthompsa	UHSO_DEV(OPTION, ICON505, UHSO_AUTO_IFACE),
286202181Sthompsa#undef UHSO_DEV
287202181Sthompsa};
288202181Sthompsa
289227309Sedstatic SYSCTL_NODE(_hw_usb, OID_AUTO, uhso, CTLFLAG_RW, 0, "USB uhso");
290202243Sthompsastatic int uhso_autoswitch = 1;
291276701ShselaskySYSCTL_INT(_hw_usb_uhso, OID_AUTO, auto_switch, CTLFLAG_RWTUN,
292202243Sthompsa    &uhso_autoswitch, 0, "Automatically switch to modem mode");
293202181Sthompsa
294202181Sthompsa#ifdef USB_DEBUG
295202181Sthompsa#ifdef UHSO_DEBUG
296202181Sthompsastatic int uhso_debug = UHSO_DEBUG;
297202181Sthompsa#else
298202181Sthompsastatic int uhso_debug = -1;
299202181Sthompsa#endif
300202181Sthompsa
301276701ShselaskySYSCTL_INT(_hw_usb_uhso, OID_AUTO, debug, CTLFLAG_RWTUN,
302202181Sthompsa    &uhso_debug, 0, "Debug level");
303202181Sthompsa
304202181Sthompsa#define UHSO_DPRINTF(n, x, ...) {\
305202181Sthompsa	if (uhso_debug >= n) {\
306202181Sthompsa		printf("%s: " x, __func__, ##__VA_ARGS__);\
307202181Sthompsa	}\
308202181Sthompsa}
309202181Sthompsa#else
310202181Sthompsa#define UHSO_DPRINTF(n, x, ...)
311202181Sthompsa#endif
312202181Sthompsa
313202181Sthompsa#ifdef UHSO_DEBUG_HEXDUMP
314202181Sthompsa# define UHSO_HEXDUMP(_buf, _len) do { \
315202181Sthompsa  { \
316202181Sthompsa        size_t __tmp; \
317202181Sthompsa        const char *__buf = (const char *)_buf; \
318202181Sthompsa        for (__tmp = 0; __tmp < _len; __tmp++) \
319202181Sthompsa                printf("%02hhx ", *__buf++); \
320202181Sthompsa    printf("\n"); \
321202181Sthompsa  } \
322202181Sthompsa} while(0)
323202181Sthompsa#else
324202181Sthompsa# define UHSO_HEXDUMP(_buf, _len)
325202181Sthompsa#endif
326202181Sthompsa
327202181Sthompsaenum {
328202181Sthompsa	UHSO_MUX_ENDPT_INTR = 0,
329202181Sthompsa	UHSO_MUX_ENDPT_MAX
330202181Sthompsa};
331202181Sthompsa
332202181Sthompsaenum {
333202181Sthompsa	UHSO_CTRL_READ = 0,
334202181Sthompsa	UHSO_CTRL_WRITE,
335202181Sthompsa	UHSO_CTRL_MAX
336202181Sthompsa};
337202181Sthompsa
338202181Sthompsaenum {
339202181Sthompsa	UHSO_IFNET_READ = 0,
340202181Sthompsa	UHSO_IFNET_WRITE,
341202181Sthompsa	UHSO_IFNET_MAX
342202181Sthompsa};
343202181Sthompsa
344202181Sthompsaenum {
345202181Sthompsa	UHSO_BULK_ENDPT_READ = 0,
346202181Sthompsa	UHSO_BULK_ENDPT_WRITE,
347202181Sthompsa	UHSO_BULK_ENDPT_INTR,
348202181Sthompsa	UHSO_BULK_ENDPT_MAX
349202181Sthompsa};
350202181Sthompsa
351202181Sthompsastatic usb_callback_t uhso_mux_intr_callback;
352202181Sthompsastatic usb_callback_t uhso_mux_read_callback;
353202181Sthompsastatic usb_callback_t uhso_mux_write_callback;
354202181Sthompsastatic usb_callback_t uhso_bs_read_callback;
355202181Sthompsastatic usb_callback_t uhso_bs_write_callback;
356202181Sthompsastatic usb_callback_t uhso_bs_intr_callback;
357202181Sthompsastatic usb_callback_t uhso_ifnet_read_callback;
358202181Sthompsastatic usb_callback_t uhso_ifnet_write_callback;
359202181Sthompsa
360202243Sthompsa/* Config used for the default control pipes */
361202181Sthompsastatic const struct usb_config uhso_ctrl_config[UHSO_CTRL_MAX] = {
362202181Sthompsa	[UHSO_CTRL_READ] = {
363202181Sthompsa		.type = UE_CONTROL,
364202181Sthompsa		.endpoint = 0x00,
365202181Sthompsa		.direction = UE_DIR_ANY,
366202181Sthompsa		.flags = { .pipe_bof = 1, .short_xfer_ok = 1 },
367202181Sthompsa		.bufsize = sizeof(struct usb_device_request) + 1024,
368202181Sthompsa		.callback = &uhso_mux_read_callback
369202181Sthompsa	},
370202181Sthompsa
371202181Sthompsa	[UHSO_CTRL_WRITE] = {
372202181Sthompsa		.type = UE_CONTROL,
373202181Sthompsa		.endpoint = 0x00,
374202181Sthompsa		.direction = UE_DIR_ANY,
375202181Sthompsa		.flags = { .pipe_bof = 1, .force_short_xfer = 1 },
376202181Sthompsa		.bufsize = sizeof(struct usb_device_request) + 1024,
377202181Sthompsa		.timeout = 1000,
378202181Sthompsa		.callback = &uhso_mux_write_callback
379202181Sthompsa	}
380202181Sthompsa};
381202181Sthompsa
382202243Sthompsa/* Config for the multiplexed serial ports */
383202181Sthompsastatic const struct usb_config uhso_mux_config[UHSO_MUX_ENDPT_MAX] = {
384202181Sthompsa	[UHSO_MUX_ENDPT_INTR] = {
385202181Sthompsa		.type = UE_INTERRUPT,
386202181Sthompsa		.endpoint = UE_ADDR_ANY,
387202181Sthompsa		.direction = UE_DIR_IN,
388202181Sthompsa		.flags = { .short_xfer_ok = 1 },
389202181Sthompsa		.bufsize = 0,
390202181Sthompsa		.callback = &uhso_mux_intr_callback,
391202181Sthompsa	}
392202181Sthompsa};
393202181Sthompsa
394202243Sthompsa/* Config for the raw IP-packet interface */
395202181Sthompsastatic const struct usb_config uhso_ifnet_config[UHSO_IFNET_MAX] = {
396202181Sthompsa	[UHSO_IFNET_READ] = {
397202181Sthompsa		.type = UE_BULK,
398202181Sthompsa		.endpoint = UE_ADDR_ANY,
399202181Sthompsa		.direction = UE_DIR_IN,
400202181Sthompsa		.flags = { .pipe_bof = 1, .short_xfer_ok = 1 },
401202181Sthompsa		.bufsize = MCLBYTES,
402202181Sthompsa		.callback = &uhso_ifnet_read_callback
403202181Sthompsa	},
404202181Sthompsa	[UHSO_IFNET_WRITE] = {
405202181Sthompsa		.type = UE_BULK,
406202181Sthompsa		.endpoint = UE_ADDR_ANY,
407202181Sthompsa		.direction = UE_DIR_OUT,
408202181Sthompsa		.flags = { .pipe_bof = 1, .force_short_xfer = 1 },
409202181Sthompsa		.bufsize = MCLBYTES,
410202181Sthompsa		.timeout = 5 * USB_MS_HZ,
411202181Sthompsa		.callback = &uhso_ifnet_write_callback
412202181Sthompsa	}
413202181Sthompsa};
414202181Sthompsa
415202243Sthompsa/* Config for interfaces with normal bulk serial ports */
416202181Sthompsastatic const struct usb_config uhso_bs_config[UHSO_BULK_ENDPT_MAX] = {
417202181Sthompsa	[UHSO_BULK_ENDPT_READ] = {
418202181Sthompsa		.type = UE_BULK,
419202181Sthompsa		.endpoint = UE_ADDR_ANY,
420202181Sthompsa		.direction = UE_DIR_IN,
421202181Sthompsa		.flags = { .pipe_bof = 1, .short_xfer_ok = 1 },
422202181Sthompsa		.bufsize = 4096,
423202181Sthompsa		.callback = &uhso_bs_read_callback
424202181Sthompsa	},
425202181Sthompsa
426202181Sthompsa	[UHSO_BULK_ENDPT_WRITE] = {
427202181Sthompsa		.type = UE_BULK,
428202181Sthompsa		.endpoint = UE_ADDR_ANY,
429202181Sthompsa		.direction = UE_DIR_OUT,
430202181Sthompsa		.flags = { .pipe_bof = 1, .force_short_xfer = 1 },
431202181Sthompsa		.bufsize = 8192,
432202181Sthompsa		.callback = &uhso_bs_write_callback
433202181Sthompsa	},
434202181Sthompsa
435202181Sthompsa	[UHSO_BULK_ENDPT_INTR] = {
436202181Sthompsa		.type = UE_INTERRUPT,
437202181Sthompsa		.endpoint = UE_ADDR_ANY,
438202181Sthompsa		.direction = UE_DIR_IN,
439202181Sthompsa		.flags = { .short_xfer_ok = 1 },
440202181Sthompsa		.bufsize = 0,
441202181Sthompsa		.callback = &uhso_bs_intr_callback,
442202181Sthompsa	}
443202181Sthompsa};
444202181Sthompsa
445202243Sthompsastatic int  uhso_probe_iface(struct uhso_softc *, int,
446210275Sthompsa    int (*probe)(struct usb_device *, int));
447210275Sthompsastatic int  uhso_probe_iface_auto(struct usb_device *, int);
448210275Sthompsastatic int  uhso_probe_iface_static(struct usb_device *, int);
449202243Sthompsastatic int  uhso_attach_muxserial(struct uhso_softc *, struct usb_interface *,
450202181Sthompsa    int type);
451202243Sthompsastatic int  uhso_attach_bulkserial(struct uhso_softc *, struct usb_interface *,
452202181Sthompsa    int type);
453202243Sthompsastatic int  uhso_attach_ifnet(struct uhso_softc *, struct usb_interface *,
454202181Sthompsa    int type);
455202181Sthompsastatic void uhso_test_autoinst(void *, struct usb_device *,
456202181Sthompsa		struct usb_attach_arg *);
457202243Sthompsastatic int  uhso_driver_loaded(struct module *, int, void *);
458210275Sthompsastatic int uhso_radio_sysctl(SYSCTL_HANDLER_ARGS);
459210275Sthompsastatic int uhso_radio_ctrl(struct uhso_softc *, int);
460202181Sthompsa
461239180Shselaskystatic void uhso_free(struct ucom_softc *);
462202181Sthompsastatic void uhso_ucom_start_read(struct ucom_softc *);
463202181Sthompsastatic void uhso_ucom_stop_read(struct ucom_softc *);
464202181Sthompsastatic void uhso_ucom_start_write(struct ucom_softc *);
465202181Sthompsastatic void uhso_ucom_stop_write(struct ucom_softc *);
466202181Sthompsastatic void uhso_ucom_cfg_get_status(struct ucom_softc *, uint8_t *, uint8_t *);
467202181Sthompsastatic void uhso_ucom_cfg_set_dtr(struct ucom_softc *, uint8_t);
468202181Sthompsastatic void uhso_ucom_cfg_set_rts(struct ucom_softc *, uint8_t);
469202181Sthompsastatic void uhso_if_init(void *);
470202181Sthompsastatic void uhso_if_start(struct ifnet *);
471202181Sthompsastatic void uhso_if_stop(struct uhso_softc *);
472202243Sthompsastatic int  uhso_if_ioctl(struct ifnet *, u_long, caddr_t);
473249925Sglebiusstatic int  uhso_if_output(struct ifnet *, struct mbuf *,
474249925Sglebius    const struct sockaddr *, struct route *);
475202181Sthompsastatic void uhso_if_rxflush(void *);
476202181Sthompsa
477202181Sthompsastatic device_probe_t uhso_probe;
478202181Sthompsastatic device_attach_t uhso_attach;
479202181Sthompsastatic device_detach_t uhso_detach;
480239299Shselaskystatic void uhso_free_softc(struct uhso_softc *);
481202181Sthompsa
482202181Sthompsastatic device_method_t uhso_methods[] = {
483202181Sthompsa	DEVMETHOD(device_probe,		uhso_probe),
484202181Sthompsa	DEVMETHOD(device_attach,	uhso_attach),
485202181Sthompsa	DEVMETHOD(device_detach,	uhso_detach),
486202181Sthompsa	{ 0, 0 }
487202181Sthompsa};
488202181Sthompsa
489202181Sthompsastatic driver_t uhso_driver = {
490233774Shselasky	.name = "uhso",
491233774Shselasky	.methods = uhso_methods,
492233774Shselasky	.size = sizeof(struct uhso_softc)
493202181Sthompsa};
494202181Sthompsa
495202181Sthompsastatic devclass_t uhso_devclass;
496202181SthompsaDRIVER_MODULE(uhso, uhub, uhso_driver, uhso_devclass, uhso_driver_loaded, 0);
497202181SthompsaMODULE_DEPEND(uhso, ucom, 1, 1, 1);
498202181SthompsaMODULE_DEPEND(uhso, usb, 1, 1, 1);
499202181SthompsaMODULE_VERSION(uhso, 1);
500292080SimpUSB_PNP_HOST_INFO(uhso_devs);
501202181Sthompsa
502202181Sthompsastatic struct ucom_callback uhso_ucom_callback = {
503202181Sthompsa	.ucom_cfg_get_status = &uhso_ucom_cfg_get_status,
504202181Sthompsa	.ucom_cfg_set_dtr = &uhso_ucom_cfg_set_dtr,
505202181Sthompsa	.ucom_cfg_set_rts = &uhso_ucom_cfg_set_rts,
506202181Sthompsa	.ucom_start_read = uhso_ucom_start_read,
507202181Sthompsa	.ucom_stop_read = uhso_ucom_stop_read,
508202181Sthompsa	.ucom_start_write = uhso_ucom_start_write,
509239180Shselasky	.ucom_stop_write = uhso_ucom_stop_write,
510239180Shselasky	.ucom_free = &uhso_free,
511202181Sthompsa};
512202181Sthompsa
513202181Sthompsastatic int
514202181Sthompsauhso_probe(device_t self)
515202181Sthompsa{
516202181Sthompsa	struct usb_attach_arg *uaa = device_get_ivars(self);
517210275Sthompsa	int error;
518202181Sthompsa
519202181Sthompsa	if (uaa->usb_mode != USB_MODE_HOST)
520202181Sthompsa		return (ENXIO);
521202181Sthompsa	if (uaa->info.bConfigIndex != 0)
522202181Sthompsa		return (ENXIO);
523212136Sthompsa	if (uaa->info.bDeviceClass != 0xff)
524202181Sthompsa		return (ENXIO);
525202181Sthompsa
526210275Sthompsa	error = usbd_lookup_id_by_uaa(uhso_devs, sizeof(uhso_devs), uaa);
527210275Sthompsa	if (error != 0)
528210275Sthompsa		return (error);
529210275Sthompsa
530210275Sthompsa	/*
531210275Sthompsa	 * Probe device to see if we are able to attach
532210275Sthompsa	 * to this interface or not.
533210275Sthompsa	 */
534210275Sthompsa	if (USB_GET_DRIVER_INFO(uaa) == UHSO_AUTO_IFACE) {
535210275Sthompsa		if (uhso_probe_iface_auto(uaa->device,
536210275Sthompsa		    uaa->info.bIfaceNum) == 0)
537210275Sthompsa			return (ENXIO);
538210275Sthompsa	}
539210275Sthompsa	return (error);
540202181Sthompsa}
541202181Sthompsa
542202181Sthompsastatic int
543202181Sthompsauhso_attach(device_t self)
544202181Sthompsa{
545202181Sthompsa	struct uhso_softc *sc = device_get_softc(self);
546202181Sthompsa	struct usb_attach_arg *uaa = device_get_ivars(self);
547202181Sthompsa	struct usb_interface_descriptor *id;
548202181Sthompsa	struct sysctl_ctx_list *sctx;
549202181Sthompsa	struct sysctl_oid *soid;
550210275Sthompsa	struct sysctl_oid *tree = NULL, *tty_node;
551202181Sthompsa	struct ucom_softc *ucom;
552202181Sthompsa	struct uhso_tty *ht;
553202181Sthompsa	int i, error, port;
554202181Sthompsa	void *probe_f;
555202181Sthompsa	usb_error_t uerr;
556202181Sthompsa	char *desc;
557202181Sthompsa
558202181Sthompsa	sc->sc_dev = self;
559202181Sthompsa	sc->sc_udev = uaa->device;
560202181Sthompsa	mtx_init(&sc->sc_mtx, "uhso", NULL, MTX_DEF);
561239180Shselasky	ucom_ref(&sc->sc_super_ucom);
562202181Sthompsa
563210275Sthompsa	sc->sc_radio = 1;
564202181Sthompsa
565202181Sthompsa	id = usbd_get_interface_descriptor(uaa->iface);
566202181Sthompsa	sc->sc_ctrl_iface_no = id->bInterfaceNumber;
567202181Sthompsa
568202181Sthompsa	sc->sc_iface_no = uaa->info.bIfaceNum;
569202181Sthompsa	sc->sc_iface_index = uaa->info.bIfaceIndex;
570202181Sthompsa
571202181Sthompsa	/* Setup control pipe */
572202181Sthompsa	uerr = usbd_transfer_setup(uaa->device,
573202181Sthompsa	    &sc->sc_iface_index, sc->sc_ctrl_xfer,
574202181Sthompsa	    uhso_ctrl_config, UHSO_CTRL_MAX, sc, &sc->sc_mtx);
575202181Sthompsa	if (uerr) {
576202181Sthompsa		device_printf(self, "Failed to setup control pipe: %s\n",
577202181Sthompsa		    usbd_errstr(uerr));
578202181Sthompsa		goto out;
579202181Sthompsa	}
580202181Sthompsa
581202181Sthompsa	if (USB_GET_DRIVER_INFO(uaa) == UHSO_STATIC_IFACE)
582202181Sthompsa		probe_f = uhso_probe_iface_static;
583202181Sthompsa	else if (USB_GET_DRIVER_INFO(uaa) == UHSO_AUTO_IFACE)
584202181Sthompsa		probe_f = uhso_probe_iface_auto;
585202181Sthompsa	else
586202181Sthompsa		goto out;
587202181Sthompsa
588202181Sthompsa	error = uhso_probe_iface(sc, uaa->info.bIfaceNum, probe_f);
589202181Sthompsa	if (error != 0)
590202181Sthompsa		goto out;
591202181Sthompsa
592202181Sthompsa	sctx = device_get_sysctl_ctx(sc->sc_dev);
593202181Sthompsa	soid = device_get_sysctl_tree(sc->sc_dev);
594202181Sthompsa
595202181Sthompsa	SYSCTL_ADD_STRING(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "type",
596202181Sthompsa	    CTLFLAG_RD, uhso_port[UHSO_IFACE_PORT(sc->sc_type)], 0,
597202181Sthompsa	    "Port available at this interface");
598210275Sthompsa	SYSCTL_ADD_PROC(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "radio",
599276701Shselasky	    CTLTYPE_INT | CTLFLAG_RWTUN, sc, 0, uhso_radio_sysctl, "I", "Enable radio");
600202181Sthompsa
601202243Sthompsa	/*
602202270Sthompsa	 * The default interface description on most Option devices isn't
603202243Sthompsa	 * very helpful. So we skip device_set_usb_desc and set the
604202243Sthompsa	 * device description manually.
605202243Sthompsa	 */
606202243Sthompsa	device_set_desc_copy(self, uhso_port_type[UHSO_IFACE_PORT_TYPE(sc->sc_type)]);
607202243Sthompsa	/* Announce device */
608202243Sthompsa	device_printf(self, "<%s port> at <%s %s> on %s\n",
609202243Sthompsa	    uhso_port_type[UHSO_IFACE_PORT_TYPE(sc->sc_type)],
610212136Sthompsa	    usb_get_manufacturer(uaa->device),
611212136Sthompsa	    usb_get_product(uaa->device),
612212136Sthompsa	    device_get_nameunit(device_get_parent(self)));
613202243Sthompsa
614202181Sthompsa	if (sc->sc_ttys > 0) {
615202181Sthompsa		SYSCTL_ADD_INT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "ports",
616202181Sthompsa		    CTLFLAG_RD, &sc->sc_ttys, 0, "Number of attached serial ports");
617202181Sthompsa
618202181Sthompsa		tree = SYSCTL_ADD_NODE(sctx, SYSCTL_CHILDREN(soid), OID_AUTO,
619202181Sthompsa		    "port", CTLFLAG_RD, NULL, "Serial ports");
620202181Sthompsa	}
621202181Sthompsa
622202243Sthompsa	/*
623202243Sthompsa	 * Loop through the number of found TTYs and create sysctl
624202243Sthompsa	 * nodes for them.
625202243Sthompsa	 */
626202181Sthompsa	for (i = 0; i < sc->sc_ttys; i++) {
627202181Sthompsa		ht = &sc->sc_tty[i];
628202181Sthompsa		ucom = &sc->sc_ucom[i];
629202181Sthompsa
630202181Sthompsa		if (UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_MUX)
631202181Sthompsa			port = uhso_mux_port_map[ht->ht_muxport];
632202181Sthompsa		else
633202181Sthompsa			port = UHSO_IFACE_PORT_TYPE(sc->sc_type);
634202181Sthompsa
635202181Sthompsa		desc = uhso_port_type_sysctl[port];
636202181Sthompsa
637202181Sthompsa		tty_node = SYSCTL_ADD_NODE(sctx, SYSCTL_CHILDREN(tree), OID_AUTO,
638202181Sthompsa		    desc, CTLFLAG_RD, NULL, "");
639202181Sthompsa
640202181Sthompsa		ht->ht_name[0] = 0;
641202181Sthompsa		if (sc->sc_ttys == 1)
642214761Sn_hibma			snprintf(ht->ht_name, 32, "cuaU%d", ucom->sc_super->sc_unit);
643202181Sthompsa		else {
644202181Sthompsa			snprintf(ht->ht_name, 32, "cuaU%d.%d",
645214761Sn_hibma			    ucom->sc_super->sc_unit, ucom->sc_subunit);
646202181Sthompsa		}
647202181Sthompsa
648202181Sthompsa		desc = uhso_port_type[port];
649202181Sthompsa		SYSCTL_ADD_STRING(sctx, SYSCTL_CHILDREN(tty_node), OID_AUTO,
650202181Sthompsa		    "tty", CTLFLAG_RD, ht->ht_name, 0, "");
651202181Sthompsa		SYSCTL_ADD_STRING(sctx, SYSCTL_CHILDREN(tty_node), OID_AUTO,
652202181Sthompsa		    "desc", CTLFLAG_RD, desc, 0, "");
653202181Sthompsa
654202181Sthompsa		if (bootverbose)
655202181Sthompsa			device_printf(sc->sc_dev,
656202181Sthompsa			    "\"%s\" port at %s\n", desc, ht->ht_name);
657202181Sthompsa	}
658202181Sthompsa
659202181Sthompsa	return (0);
660202181Sthompsaout:
661202181Sthompsa	uhso_detach(sc->sc_dev);
662202181Sthompsa	return (ENXIO);
663202181Sthompsa}
664202181Sthompsa
665202181Sthompsastatic int
666202181Sthompsauhso_detach(device_t self)
667202181Sthompsa{
668202181Sthompsa	struct uhso_softc *sc = device_get_softc(self);
669202181Sthompsa	int i;
670202181Sthompsa
671202181Sthompsa	usbd_transfer_unsetup(sc->sc_xfer, 3);
672202181Sthompsa	usbd_transfer_unsetup(sc->sc_ctrl_xfer, UHSO_CTRL_MAX);
673202181Sthompsa	if (sc->sc_ttys > 0) {
674214761Sn_hibma		ucom_detach(&sc->sc_super_ucom, sc->sc_ucom);
675202181Sthompsa
676202181Sthompsa		for (i = 0; i < sc->sc_ttys; i++) {
677202181Sthompsa			if (sc->sc_tty[i].ht_muxport != -1) {
678202181Sthompsa				usbd_transfer_unsetup(sc->sc_tty[i].ht_xfer,
679202181Sthompsa				    UHSO_CTRL_MAX);
680202181Sthompsa			}
681202181Sthompsa		}
682202181Sthompsa	}
683202181Sthompsa
684202181Sthompsa	if (sc->sc_ifp != NULL) {
685202181Sthompsa		callout_drain(&sc->sc_c);
686210275Sthompsa		free_unr(uhso_ifnet_unit, sc->sc_ifp->if_dunit);
687202181Sthompsa		mtx_lock(&sc->sc_mtx);
688202181Sthompsa		uhso_if_stop(sc);
689202181Sthompsa		bpfdetach(sc->sc_ifp);
690202181Sthompsa		if_detach(sc->sc_ifp);
691202181Sthompsa		if_free(sc->sc_ifp);
692202181Sthompsa		mtx_unlock(&sc->sc_mtx);
693202181Sthompsa		usbd_transfer_unsetup(sc->sc_if_xfer, UHSO_IFNET_MAX);
694202181Sthompsa	}
695202181Sthompsa
696239299Shselasky	device_claim_softc(self);
697239299Shselasky
698239299Shselasky	uhso_free_softc(sc);
699239299Shselasky
700202181Sthompsa	return (0);
701202181Sthompsa}
702202181Sthompsa
703239180ShselaskyUCOM_UNLOAD_DRAIN(uhso);
704239180Shselasky
705202181Sthompsastatic void
706239299Shselaskyuhso_free_softc(struct uhso_softc *sc)
707239180Shselasky{
708239180Shselasky	if (ucom_unref(&sc->sc_super_ucom)) {
709268078Shselasky		free(sc->sc_tty, M_USBDEV);
710268078Shselasky		free(sc->sc_ucom, M_USBDEV);
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:
818260903Shselasky	case UHSO_PORT_TYPE_GPS:
819260903Shselasky	case UHSO_PORT_TYPE_GPSCTL:
820210275Sthompsa	case UHSO_PORT_TYPE_CTL:
821210275Sthompsa	case UHSO_PORT_TYPE_APP:
822210275Sthompsa	case UHSO_PORT_TYPE_APP2:
823210275Sthompsa	case UHSO_PORT_TYPE_MODEM:
824210275Sthompsa		return (UHSO_IFACE_SPEC(UHSO_IF_BULK,
825210275Sthompsa		    UHSO_PORT_SERIAL, port));
826210275Sthompsa	case UHSO_PORT_TYPE_MSD:
827202181Sthompsa		return (0);
828210275Sthompsa	case UHSO_PORT_TYPE_UNKNOWN:
829202243Sthompsa	default:
830210275Sthompsa		return (0);
831202243Sthompsa	}
832202181Sthompsa
833202181Sthompsa	return (0);
834202181Sthompsa}
835202181Sthompsa
836210275Sthompsa/*
837210275Sthompsa * Returns the capabilities of interfaces for devices that don't
838210275Sthompsa * support the automatic query.
839210275Sthompsa * Returns a bit mask with the interface capabilities.
840210275Sthompsa */
841202181Sthompsastatic int
842210275Sthompsauhso_probe_iface_static(struct usb_device *udev, int index)
843202181Sthompsa{
844202181Sthompsa	struct usb_config_descriptor *cd;
845202181Sthompsa
846210275Sthompsa	cd = usbd_get_config_descriptor(udev);
847202181Sthompsa	if (cd->bNumInterface <= 3) {
848202243Sthompsa		/* Cards with 3 or less interfaces */
849202181Sthompsa		switch (index) {
850202181Sthompsa		case 0:
851202181Sthompsa			return UHSO_IFACE_SPEC(UHSO_IF_NET | UHSO_IF_MUX,
852202243Sthompsa			    UHSO_PORT_SERIAL | UHSO_PORT_NETWORK,
853202243Sthompsa			    UHSO_PORT_TYPE_NETWORK);
854202181Sthompsa		case 1:
855202181Sthompsa			return UHSO_IFACE_SPEC(UHSO_IF_BULK,
856202181Sthompsa			    UHSO_PORT_SERIAL, UHSO_PORT_TYPE_DIAG);
857202181Sthompsa		case 2:
858202181Sthompsa			return UHSO_IFACE_SPEC(UHSO_IF_BULK,
859202181Sthompsa			    UHSO_PORT_SERIAL, UHSO_PORT_TYPE_MODEM);
860202181Sthompsa		}
861202243Sthompsa	} else {
862202243Sthompsa		/* Cards with 4 interfaces */
863202181Sthompsa		switch (index) {
864202181Sthompsa		case 0:
865202181Sthompsa			return UHSO_IFACE_SPEC(UHSO_IF_NET | UHSO_IF_MUX,
866202243Sthompsa			    UHSO_PORT_SERIAL | UHSO_PORT_NETWORK,
867202243Sthompsa			    UHSO_PORT_TYPE_NETWORK);
868202181Sthompsa		case 1:
869202181Sthompsa			return UHSO_IFACE_SPEC(UHSO_IF_BULK,
870202181Sthompsa			    UHSO_PORT_SERIAL, UHSO_PORT_TYPE_DIAG2);
871202181Sthompsa		case 2:
872202181Sthompsa			return UHSO_IFACE_SPEC(UHSO_IF_BULK,
873202181Sthompsa			    UHSO_PORT_SERIAL, UHSO_PORT_TYPE_MODEM);
874202181Sthompsa		case 3:
875202181Sthompsa			return UHSO_IFACE_SPEC(UHSO_IF_BULK,
876202181Sthompsa			    UHSO_PORT_SERIAL, UHSO_PORT_TYPE_DIAG);
877202181Sthompsa		}
878202181Sthompsa	}
879202181Sthompsa	return (0);
880202181Sthompsa}
881202181Sthompsa
882202243Sthompsa/*
883202243Sthompsa * Probes an interface for its particular capabilities and attaches if
884202243Sthompsa * it's a supported interface.
885202243Sthompsa */
886202181Sthompsastatic int
887202181Sthompsauhso_probe_iface(struct uhso_softc *sc, int index,
888210275Sthompsa    int (*probe)(struct usb_device *, int))
889202181Sthompsa{
890202181Sthompsa	struct usb_interface *iface;
891202243Sthompsa	int type, error;
892202181Sthompsa
893202243Sthompsa	UHSO_DPRINTF(1, "Probing for interface %d, probe_func=%p\n", index, probe);
894202181Sthompsa
895210275Sthompsa	type = probe(sc->sc_udev, index);
896202181Sthompsa	UHSO_DPRINTF(1, "Probe result %x\n", type);
897202181Sthompsa	if (type <= 0)
898202181Sthompsa		return (ENXIO);
899202181Sthompsa
900202181Sthompsa	sc->sc_type = type;
901202181Sthompsa	iface = usbd_get_iface(sc->sc_udev, index);
902202181Sthompsa
903202243Sthompsa	if (UHSO_IFACE_PORT_TYPE(type) == UHSO_PORT_TYPE_NETWORK) {
904202181Sthompsa		error = uhso_attach_ifnet(sc, iface, type);
905202243Sthompsa		if (error) {
906202243Sthompsa			UHSO_DPRINTF(1, "uhso_attach_ifnet failed");
907202181Sthompsa			return (ENXIO);
908202243Sthompsa		}
909202181Sthompsa
910202243Sthompsa		/*
911202243Sthompsa		 * If there is an additional interrupt endpoint on this
912202270Sthompsa		 * interface then we most likely have a multiplexed serial port
913202243Sthompsa		 * available.
914202243Sthompsa		 */
915202243Sthompsa		if (iface->idesc->bNumEndpoints < 3) {
916202243Sthompsa			sc->sc_type = UHSO_IFACE_SPEC(
917202243Sthompsa			    UHSO_IFACE_USB_TYPE(type) & ~UHSO_IF_MUX,
918202243Sthompsa			    UHSO_IFACE_PORT(type) & ~UHSO_PORT_SERIAL,
919202243Sthompsa			    UHSO_IFACE_PORT_TYPE(type));
920202243Sthompsa			return (0);
921202243Sthompsa		}
922202243Sthompsa
923202243Sthompsa		UHSO_DPRINTF(1, "Trying to attach mux. serial\n");
924202243Sthompsa		error = uhso_attach_muxserial(sc, iface, type);
925202243Sthompsa		if (error == 0 && sc->sc_ttys > 0) {
926202181Sthompsa			error = ucom_attach(&sc->sc_super_ucom, sc->sc_ucom,
927202181Sthompsa			    sc->sc_ttys, sc, &uhso_ucom_callback, &sc->sc_mtx);
928202181Sthompsa			if (error) {
929202181Sthompsa				device_printf(sc->sc_dev, "ucom_attach failed\n");
930202181Sthompsa				return (ENXIO);
931202181Sthompsa			}
932214843Sn_hibma			ucom_set_pnpinfo_usb(&sc->sc_super_ucom, sc->sc_dev);
933202243Sthompsa
934202243Sthompsa			mtx_lock(&sc->sc_mtx);
935202243Sthompsa			usbd_transfer_start(sc->sc_xfer[UHSO_MUX_ENDPT_INTR]);
936202243Sthompsa			mtx_unlock(&sc->sc_mtx);
937202181Sthompsa		}
938202243Sthompsa	} else if ((UHSO_IFACE_USB_TYPE(type) & UHSO_IF_BULK) &&
939202181Sthompsa	    UHSO_IFACE_PORT(type) & UHSO_PORT_SERIAL) {
940202181Sthompsa
941202181Sthompsa		error = uhso_attach_bulkserial(sc, iface, type);
942202181Sthompsa		if (error)
943202181Sthompsa			return (ENXIO);
944202181Sthompsa
945202181Sthompsa		error = ucom_attach(&sc->sc_super_ucom, sc->sc_ucom,
946202181Sthompsa		    sc->sc_ttys, sc, &uhso_ucom_callback, &sc->sc_mtx);
947202181Sthompsa		if (error) {
948202181Sthompsa			device_printf(sc->sc_dev, "ucom_attach failed\n");
949202181Sthompsa			return (ENXIO);
950202181Sthompsa		}
951214843Sn_hibma		ucom_set_pnpinfo_usb(&sc->sc_super_ucom, sc->sc_dev);
952202181Sthompsa	}
953202181Sthompsa	else {
954202243Sthompsa		UHSO_DPRINTF(0, "Unknown type %x\n", type);
955202181Sthompsa		return (ENXIO);
956202181Sthompsa	}
957202181Sthompsa
958202181Sthompsa	return (0);
959202181Sthompsa}
960202181Sthompsa
961210275Sthompsastatic int
962210275Sthompsauhso_radio_ctrl(struct uhso_softc *sc, int onoff)
963210275Sthompsa{
964210275Sthompsa	struct usb_device_request req;
965210275Sthompsa	usb_error_t uerr;
966210275Sthompsa
967210275Sthompsa	req.bmRequestType = UT_VENDOR;
968210275Sthompsa	req.bRequest = onoff ? 0x82 : 0x81;
969210275Sthompsa	USETW(req.wValue, 0);
970210275Sthompsa	USETW(req.wIndex, 0);
971210275Sthompsa	USETW(req.wLength, 0);
972210275Sthompsa
973210275Sthompsa	uerr = usbd_do_request(sc->sc_udev, NULL, &req, NULL);
974210275Sthompsa	if (uerr != 0) {
975210275Sthompsa		device_printf(sc->sc_dev, "usbd_do_request_flags failed: %s\n",
976210275Sthompsa		    usbd_errstr(uerr));
977210275Sthompsa		return (-1);
978210275Sthompsa	}
979210275Sthompsa	return (onoff);
980210275Sthompsa}
981210275Sthompsa
982210275Sthompsastatic int
983210275Sthompsauhso_radio_sysctl(SYSCTL_HANDLER_ARGS)
984210275Sthompsa{
985210275Sthompsa	struct uhso_softc *sc = arg1;
986210275Sthompsa	int error, radio;
987210275Sthompsa
988210275Sthompsa	radio = sc->sc_radio;
989210275Sthompsa	error = sysctl_handle_int(oidp, &radio, 0, req);
990210275Sthompsa	if (error)
991210275Sthompsa		return (error);
992210275Sthompsa	if (radio != sc->sc_radio) {
993210275Sthompsa		radio = radio != 0 ? 1 : 0;
994210275Sthompsa		error = uhso_radio_ctrl(sc, radio);
995210275Sthompsa		if (error != -1)
996210275Sthompsa			sc->sc_radio = radio;
997210275Sthompsa
998210275Sthompsa	}
999210275Sthompsa	return (0);
1000210275Sthompsa}
1001210275Sthompsa
1002202243Sthompsa/*
1003202243Sthompsa * Expands allocated memory to fit an additional TTY.
1004202243Sthompsa * Two arrays are kept with matching indexes, one for ucom and one
1005202243Sthompsa * for our private data.
1006202243Sthompsa */
1007202181Sthompsastatic int
1008202181Sthompsauhso_alloc_tty(struct uhso_softc *sc)
1009202181Sthompsa{
1010202181Sthompsa
1011202181Sthompsa	sc->sc_ttys++;
1012202181Sthompsa	sc->sc_tty = reallocf(sc->sc_tty, sizeof(struct uhso_tty) * sc->sc_ttys,
1013202181Sthompsa	    M_USBDEV, M_WAITOK | M_ZERO);
1014202181Sthompsa	if (sc->sc_tty == NULL)
1015202181Sthompsa		return (-1);
1016202181Sthompsa
1017202181Sthompsa	sc->sc_ucom = reallocf(sc->sc_ucom,
1018202181Sthompsa	    sizeof(struct ucom_softc) * sc->sc_ttys, M_USBDEV, M_WAITOK | M_ZERO);
1019202181Sthompsa	if (sc->sc_ucom == NULL)
1020202181Sthompsa		return (-1);
1021202181Sthompsa
1022202181Sthompsa	sc->sc_tty[sc->sc_ttys - 1].ht_sc = sc;
1023202181Sthompsa
1024202243Sthompsa	UHSO_DPRINTF(1, "Allocated TTY %d\n", sc->sc_ttys - 1);
1025202181Sthompsa	return (sc->sc_ttys - 1);
1026202181Sthompsa}
1027202181Sthompsa
1028202243Sthompsa/*
1029202243Sthompsa * Attach a multiplexed serial port
1030202243Sthompsa * Data is read/written with requests on the default control pipe. An interrupt
1031202243Sthompsa * endpoint returns when there is new data to be read.
1032202243Sthompsa */
1033202181Sthompsastatic int
1034202181Sthompsauhso_attach_muxserial(struct uhso_softc *sc, struct usb_interface *iface,
1035202181Sthompsa    int type)
1036202181Sthompsa{
1037202181Sthompsa	struct usb_descriptor *desc;
1038202181Sthompsa	int i, port, tty;
1039202181Sthompsa	usb_error_t uerr;
1040202181Sthompsa
1041202181Sthompsa	/*
1042202181Sthompsa	 * The class specific interface (type 0x24) descriptor subtype field
1043202181Sthompsa	 * contains a bitmask that specifies which (and how many) ports that
1044202181Sthompsa	 * are available through this multiplexed serial port.
1045202181Sthompsa 	 */
1046202181Sthompsa	desc = usbd_find_descriptor(sc->sc_udev, NULL,
1047202181Sthompsa	    iface->idesc->bInterfaceNumber, UDESC_CS_INTERFACE, 0xff, 0, 0);
1048202181Sthompsa	if (desc == NULL) {
1049202181Sthompsa		UHSO_DPRINTF(0, "Failed to find UDESC_CS_INTERFACE\n");
1050202181Sthompsa		return (ENXIO);
1051202181Sthompsa	}
1052202181Sthompsa
1053202181Sthompsa	UHSO_DPRINTF(1, "Mux port mask %x\n", desc->bDescriptorSubtype);
1054202181Sthompsa	if (desc->bDescriptorSubtype == 0)
1055202181Sthompsa		return (ENXIO);
1056202181Sthompsa
1057202243Sthompsa	/*
1058202243Sthompsa	 * The bitmask is one octet, loop through the number of
1059202243Sthompsa	 * bits that are set and create a TTY for each.
1060202243Sthompsa	 */
1061202181Sthompsa	for (i = 0; i < 8; i++) {
1062202181Sthompsa		port = (1 << i);
1063202181Sthompsa		if ((port & desc->bDescriptorSubtype) == port) {
1064202181Sthompsa			UHSO_DPRINTF(2, "Found mux port %x (%d)\n", port, i);
1065202181Sthompsa			tty = uhso_alloc_tty(sc);
1066202181Sthompsa			if (tty < 0)
1067202181Sthompsa				return (ENOMEM);
1068202181Sthompsa			sc->sc_tty[tty].ht_muxport = i;
1069202181Sthompsa			uerr = usbd_transfer_setup(sc->sc_udev,
1070202181Sthompsa			    &sc->sc_iface_index, sc->sc_tty[tty].ht_xfer,
1071202181Sthompsa			    uhso_ctrl_config, UHSO_CTRL_MAX, sc, &sc->sc_mtx);
1072202181Sthompsa			if (uerr) {
1073202181Sthompsa				device_printf(sc->sc_dev,
1074202181Sthompsa				    "Failed to setup control pipe: %s\n",
1075202181Sthompsa				    usbd_errstr(uerr));
1076202181Sthompsa				return (ENXIO);
1077202181Sthompsa			}
1078202181Sthompsa		}
1079202181Sthompsa	}
1080202181Sthompsa
1081202243Sthompsa	/* Setup the intr. endpoint */
1082202181Sthompsa	uerr = usbd_transfer_setup(sc->sc_udev,
1083202181Sthompsa	    &iface->idesc->bInterfaceNumber, sc->sc_xfer,
1084202181Sthompsa	    uhso_mux_config, 1, sc, &sc->sc_mtx);
1085202181Sthompsa	if (uerr)
1086202181Sthompsa		return (ENXIO);
1087202181Sthompsa
1088202181Sthompsa	return (0);
1089202181Sthompsa}
1090202181Sthompsa
1091202243Sthompsa/*
1092202243Sthompsa * Interrupt callback for the multiplexed serial port. Indicates
1093202270Sthompsa * which serial port has data waiting.
1094202243Sthompsa */
1095202181Sthompsastatic void
1096202181Sthompsauhso_mux_intr_callback(struct usb_xfer *xfer, usb_error_t error)
1097202181Sthompsa{
1098202181Sthompsa	struct usb_page_cache *pc;
1099202181Sthompsa	struct usb_page_search res;
1100202181Sthompsa	struct uhso_softc *sc = usbd_xfer_softc(xfer);
1101202181Sthompsa	unsigned int i, mux;
1102202181Sthompsa
1103202181Sthompsa	UHSO_DPRINTF(3, "status %d\n", USB_GET_STATE(xfer));
1104202181Sthompsa
1105202181Sthompsa	switch (USB_GET_STATE(xfer)) {
1106202181Sthompsa	case USB_ST_TRANSFERRED:
1107202181Sthompsa		/*
1108202181Sthompsa		 * The multiplexed port number can be found at the first byte.
1109202181Sthompsa		 * It contains a bit mask, we transform this in to an integer.
1110202181Sthompsa		 */
1111202181Sthompsa		pc = usbd_xfer_get_frame(xfer, 0);
1112202181Sthompsa		usbd_get_page(pc, 0, &res);
1113202181Sthompsa
1114202181Sthompsa		i = *((unsigned char *)res.buffer);
1115202181Sthompsa		mux = 0;
1116202181Sthompsa		while (i >>= 1) {
1117202181Sthompsa			mux++;
1118202181Sthompsa		}
1119202181Sthompsa
1120202181Sthompsa		UHSO_DPRINTF(3, "mux port %d (%d)\n", mux, i);
1121202181Sthompsa		if (mux > UHSO_MPORT_TYPE_NOMAX)
1122202181Sthompsa			break;
1123202181Sthompsa
1124202243Sthompsa		/* Issue a read for this serial port */
1125202181Sthompsa		usbd_xfer_set_priv(
1126202181Sthompsa		    sc->sc_tty[mux].ht_xfer[UHSO_CTRL_READ],
1127202181Sthompsa		    &sc->sc_tty[mux]);
1128202181Sthompsa		usbd_transfer_start(sc->sc_tty[mux].ht_xfer[UHSO_CTRL_READ]);
1129202181Sthompsa
1130202181Sthompsa		break;
1131202181Sthompsa	case USB_ST_SETUP:
1132202181Sthompsatr_setup:
1133202181Sthompsa		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
1134202181Sthompsa		usbd_transfer_submit(xfer);
1135202181Sthompsa		break;
1136202181Sthompsa	default:
1137202181Sthompsa		UHSO_DPRINTF(0, "error: %s\n", usbd_errstr(error));
1138202181Sthompsa		if (error == USB_ERR_CANCELLED)
1139202181Sthompsa			break;
1140202181Sthompsa
1141202181Sthompsa		usbd_xfer_set_stall(xfer);
1142202181Sthompsa		goto tr_setup;
1143202181Sthompsa	}
1144202181Sthompsa}
1145202181Sthompsa
1146202181Sthompsastatic void
1147202181Sthompsauhso_mux_read_callback(struct usb_xfer *xfer, usb_error_t error)
1148202181Sthompsa{
1149202181Sthompsa	struct uhso_softc *sc = usbd_xfer_softc(xfer);
1150202181Sthompsa	struct usb_page_cache *pc;
1151202181Sthompsa	struct usb_device_request req;
1152202181Sthompsa	struct uhso_tty *ht;
1153202181Sthompsa	int actlen, len;
1154202181Sthompsa
1155202181Sthompsa	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
1156202181Sthompsa
1157202181Sthompsa	UHSO_DPRINTF(3, "status %d\n", USB_GET_STATE(xfer));
1158202181Sthompsa
1159202181Sthompsa	ht = usbd_xfer_get_priv(xfer);
1160202181Sthompsa	UHSO_DPRINTF(3, "ht=%p open=%d\n", ht, ht->ht_open);
1161202181Sthompsa
1162202181Sthompsa	switch (USB_GET_STATE(xfer)) {
1163202181Sthompsa	case USB_ST_TRANSFERRED:
1164202181Sthompsa		/* Got data, send to ucom */
1165202181Sthompsa		pc = usbd_xfer_get_frame(xfer, 1);
1166202181Sthompsa		len = usbd_xfer_frame_len(xfer, 1);
1167202181Sthompsa
1168202181Sthompsa		UHSO_DPRINTF(3, "got %d bytes on mux port %d\n", len,
1169202181Sthompsa		    ht->ht_muxport);
1170202181Sthompsa		if (len <= 0) {
1171202181Sthompsa			usbd_transfer_start(sc->sc_xfer[UHSO_MUX_ENDPT_INTR]);
1172202181Sthompsa			break;
1173202181Sthompsa		}
1174202181Sthompsa
1175202181Sthompsa		/* Deliver data if the TTY is open, discard otherwise */
1176202181Sthompsa		if (ht->ht_open)
1177202181Sthompsa			ucom_put_data(&sc->sc_ucom[ht->ht_muxport], pc, 0, len);
1178202181Sthompsa		/* FALLTHROUGH */
1179202181Sthompsa	case USB_ST_SETUP:
1180202181Sthompsatr_setup:
1181227461Shselasky		memset(&req, 0, sizeof(struct usb_device_request));
1182202181Sthompsa		req.bmRequestType = UT_READ_CLASS_INTERFACE;
1183202181Sthompsa		req.bRequest = UCDC_GET_ENCAPSULATED_RESPONSE;
1184202181Sthompsa		USETW(req.wValue, 0);
1185202181Sthompsa		USETW(req.wIndex, ht->ht_muxport);
1186202181Sthompsa		USETW(req.wLength, 1024);
1187202181Sthompsa
1188202181Sthompsa		pc = usbd_xfer_get_frame(xfer, 0);
1189202181Sthompsa		usbd_copy_in(pc, 0, &req, sizeof(req));
1190202181Sthompsa
1191202181Sthompsa		usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
1192202181Sthompsa		usbd_xfer_set_frame_len(xfer, 1, 1024);
1193202181Sthompsa		usbd_xfer_set_frames(xfer, 2);
1194202181Sthompsa		usbd_transfer_submit(xfer);
1195202181Sthompsa		break;
1196202181Sthompsa	default:
1197202181Sthompsa		UHSO_DPRINTF(0, "error: %s\n", usbd_errstr(error));
1198202181Sthompsa		if (error == USB_ERR_CANCELLED)
1199202181Sthompsa			break;
1200202181Sthompsa		usbd_xfer_set_stall(xfer);
1201202181Sthompsa		goto tr_setup;
1202202181Sthompsa	}
1203202181Sthompsa}
1204202181Sthompsa
1205202181Sthompsastatic void
1206202181Sthompsauhso_mux_write_callback(struct usb_xfer *xfer, usb_error_t error)
1207202181Sthompsa{
1208202181Sthompsa	struct uhso_softc *sc = usbd_xfer_softc(xfer);
1209202181Sthompsa	struct uhso_tty *ht;
1210202181Sthompsa	struct usb_page_cache *pc;
1211202181Sthompsa	struct usb_device_request req;
1212202181Sthompsa	int actlen;
1213202181Sthompsa	struct usb_page_search res;
1214202181Sthompsa
1215202181Sthompsa	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
1216202181Sthompsa
1217202181Sthompsa	ht = usbd_xfer_get_priv(xfer);
1218202181Sthompsa	UHSO_DPRINTF(3, "status=%d, using mux port %d\n",
1219202181Sthompsa	    USB_GET_STATE(xfer), ht->ht_muxport);
1220202181Sthompsa
1221202181Sthompsa	switch (USB_GET_STATE(xfer)) {
1222202181Sthompsa	case USB_ST_TRANSFERRED:
1223202181Sthompsa		UHSO_DPRINTF(3, "wrote %zd data bytes to muxport %d\n",
1224202181Sthompsa		    actlen - sizeof(struct usb_device_request) ,
1225202181Sthompsa		    ht->ht_muxport);
1226202181Sthompsa		/* FALLTHROUGH */
1227202181Sthompsa	case USB_ST_SETUP:
1228301206Spfgtr_setup:
1229202181Sthompsa		pc = usbd_xfer_get_frame(xfer, 1);
1230202181Sthompsa		if (ucom_get_data(&sc->sc_ucom[ht->ht_muxport], pc,
1231202181Sthompsa		    0, 32, &actlen)) {
1232202181Sthompsa
1233202181Sthompsa			usbd_get_page(pc, 0, &res);
1234202181Sthompsa
1235227461Shselasky			memset(&req, 0, sizeof(struct usb_device_request));
1236202181Sthompsa			req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
1237202181Sthompsa			req.bRequest = UCDC_SEND_ENCAPSULATED_COMMAND;
1238202181Sthompsa			USETW(req.wValue, 0);
1239202181Sthompsa			USETW(req.wIndex, ht->ht_muxport);
1240202181Sthompsa			USETW(req.wLength, actlen);
1241202181Sthompsa
1242202181Sthompsa			pc = usbd_xfer_get_frame(xfer, 0);
1243202181Sthompsa			usbd_copy_in(pc, 0, &req, sizeof(req));
1244202181Sthompsa
1245202181Sthompsa			usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
1246202181Sthompsa			usbd_xfer_set_frame_len(xfer, 1, actlen);
1247202181Sthompsa			usbd_xfer_set_frames(xfer, 2);
1248202181Sthompsa
1249202181Sthompsa			UHSO_DPRINTF(3, "Prepared %d bytes for transmit "
1250202181Sthompsa			    "on muxport %d\n", actlen, ht->ht_muxport);
1251202181Sthompsa
1252202181Sthompsa			usbd_transfer_submit(xfer);
1253202181Sthompsa		}
1254202181Sthompsa		break;
1255202181Sthompsa	default:
1256202181Sthompsa		UHSO_DPRINTF(0, "error: %s\n", usbd_errstr(error));
1257202181Sthompsa		if (error == USB_ERR_CANCELLED)
1258202181Sthompsa			break;
1259301206Spfg		usbd_xfer_set_stall(xfer);
1260301206Spfg		goto tr_setup;
1261202181Sthompsa	}
1262202181Sthompsa}
1263202181Sthompsa
1264202181Sthompsastatic int
1265202181Sthompsauhso_attach_bulkserial(struct uhso_softc *sc, struct usb_interface *iface,
1266202181Sthompsa    int type)
1267202181Sthompsa{
1268202181Sthompsa	usb_error_t uerr;
1269202181Sthompsa	int tty;
1270202181Sthompsa
1271202243Sthompsa	/* Try attaching RD/WR/INTR first */
1272202181Sthompsa	uerr = usbd_transfer_setup(sc->sc_udev,
1273202181Sthompsa	    &iface->idesc->bInterfaceNumber, sc->sc_xfer,
1274202181Sthompsa	    uhso_bs_config, UHSO_BULK_ENDPT_MAX, sc, &sc->sc_mtx);
1275202181Sthompsa	if (uerr) {
1276202181Sthompsa		/* Try only RD/WR */
1277202181Sthompsa		uerr = usbd_transfer_setup(sc->sc_udev,
1278202181Sthompsa		    &iface->idesc->bInterfaceNumber, sc->sc_xfer,
1279202181Sthompsa		    uhso_bs_config, UHSO_BULK_ENDPT_MAX - 1, sc, &sc->sc_mtx);
1280202181Sthompsa	}
1281202181Sthompsa	if (uerr) {
1282202181Sthompsa		UHSO_DPRINTF(0, "usbd_transfer_setup failed");
1283202181Sthompsa		return (-1);
1284202181Sthompsa	}
1285202181Sthompsa
1286202181Sthompsa	tty = uhso_alloc_tty(sc);
1287202181Sthompsa	if (tty < 0) {
1288202181Sthompsa		usbd_transfer_unsetup(sc->sc_xfer, UHSO_BULK_ENDPT_MAX);
1289202181Sthompsa		return (ENOMEM);
1290202181Sthompsa	}
1291202181Sthompsa
1292202181Sthompsa	sc->sc_tty[tty].ht_muxport = -1;
1293202181Sthompsa	return (0);
1294202181Sthompsa}
1295202181Sthompsa
1296202181Sthompsastatic void
1297202181Sthompsauhso_bs_read_callback(struct usb_xfer *xfer, usb_error_t error)
1298202181Sthompsa{
1299202181Sthompsa	struct uhso_softc *sc = usbd_xfer_softc(xfer);
1300202181Sthompsa	struct usb_page_cache *pc;
1301202181Sthompsa	int actlen;
1302202181Sthompsa
1303202181Sthompsa	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
1304202181Sthompsa
1305202181Sthompsa	UHSO_DPRINTF(3, "status %d, actlen=%d\n", USB_GET_STATE(xfer), actlen);
1306202181Sthompsa
1307202181Sthompsa	switch (USB_GET_STATE(xfer)) {
1308202181Sthompsa	case USB_ST_TRANSFERRED:
1309202181Sthompsa		pc = usbd_xfer_get_frame(xfer, 0);
1310202181Sthompsa		ucom_put_data(&sc->sc_ucom[0], pc, 0, actlen);
1311202181Sthompsa		/* FALLTHROUGH */
1312202181Sthompsa	case USB_ST_SETUP:
1313202181Sthompsatr_setup:
1314202181Sthompsa		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
1315202181Sthompsa		usbd_transfer_submit(xfer);
1316202181Sthompsa	break;
1317202181Sthompsa	default:
1318202181Sthompsa		UHSO_DPRINTF(0, "error: %s\n", usbd_errstr(error));
1319202181Sthompsa		if (error == USB_ERR_CANCELLED)
1320202181Sthompsa			break;
1321202181Sthompsa		usbd_xfer_set_stall(xfer);
1322202181Sthompsa		goto tr_setup;
1323202181Sthompsa	}
1324202181Sthompsa}
1325202181Sthompsa
1326202181Sthompsastatic void
1327202181Sthompsauhso_bs_write_callback(struct usb_xfer *xfer, usb_error_t error)
1328202181Sthompsa{
1329202181Sthompsa	struct uhso_softc *sc = usbd_xfer_softc(xfer);
1330202181Sthompsa	struct usb_page_cache *pc;
1331202181Sthompsa	int actlen;
1332202181Sthompsa
1333202181Sthompsa	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
1334202181Sthompsa
1335202181Sthompsa	UHSO_DPRINTF(3, "status %d, actlen=%d\n", USB_GET_STATE(xfer), actlen);
1336202181Sthompsa
1337202181Sthompsa	switch (USB_GET_STATE(xfer)) {
1338202181Sthompsa	case USB_ST_TRANSFERRED:
1339202181Sthompsa	case USB_ST_SETUP:
1340202181Sthompsatr_setup:
1341202181Sthompsa		pc = usbd_xfer_get_frame(xfer, 0);
1342202181Sthompsa		if (ucom_get_data(&sc->sc_ucom[0], pc, 0, 8192, &actlen)) {
1343202181Sthompsa			usbd_xfer_set_frame_len(xfer, 0, actlen);
1344202181Sthompsa			usbd_transfer_submit(xfer);
1345202181Sthompsa		}
1346202181Sthompsa		break;
1347202181Sthompsa	break;
1348202181Sthompsa	default:
1349202181Sthompsa		UHSO_DPRINTF(0, "error: %s\n", usbd_errstr(error));
1350202181Sthompsa		if (error == USB_ERR_CANCELLED)
1351202181Sthompsa			break;
1352202181Sthompsa		usbd_xfer_set_stall(xfer);
1353202181Sthompsa		goto tr_setup;
1354202181Sthompsa	}
1355202181Sthompsa}
1356202181Sthompsa
1357202181Sthompsastatic void
1358202181Sthompsauhso_bs_cfg(struct uhso_softc *sc)
1359202181Sthompsa{
1360202181Sthompsa	struct usb_device_request req;
1361202181Sthompsa	usb_error_t uerr;
1362202181Sthompsa
1363202181Sthompsa	if (!(UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_BULK))
1364202181Sthompsa		return;
1365202181Sthompsa
1366202181Sthompsa	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
1367202181Sthompsa	req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
1368202181Sthompsa	USETW(req.wValue, sc->sc_line);
1369202181Sthompsa	USETW(req.wIndex, sc->sc_iface_no);
1370202181Sthompsa	USETW(req.wLength, 0);
1371202181Sthompsa
1372202181Sthompsa	uerr = ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom[0], &req, NULL, 0, 1000);
1373202181Sthompsa	if (uerr != 0) {
1374202181Sthompsa		device_printf(sc->sc_dev, "failed to set ctrl line state to "
1375202181Sthompsa		    "0x%02x: %s\n", sc->sc_line, usbd_errstr(uerr));
1376202181Sthompsa	}
1377202181Sthompsa}
1378202181Sthompsa
1379202181Sthompsastatic void
1380202181Sthompsauhso_bs_intr_callback(struct usb_xfer *xfer, usb_error_t error)
1381202181Sthompsa{
1382202181Sthompsa	struct uhso_softc *sc = usbd_xfer_softc(xfer);
1383202181Sthompsa	struct usb_page_cache *pc;
1384202181Sthompsa	int actlen;
1385202181Sthompsa	struct usb_cdc_notification cdc;
1386202181Sthompsa
1387202181Sthompsa	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
1388202181Sthompsa	UHSO_DPRINTF(3, "status %d, actlen=%d\n", USB_GET_STATE(xfer), actlen);
1389202181Sthompsa
1390202181Sthompsa	switch (USB_GET_STATE(xfer)) {
1391202181Sthompsa	case USB_ST_TRANSFERRED:
1392202181Sthompsa		if (actlen < UCDC_NOTIFICATION_LENGTH) {
1393202181Sthompsa			UHSO_DPRINTF(0, "UCDC notification too short: %d\n", actlen);
1394202181Sthompsa			goto tr_setup;
1395202181Sthompsa		}
1396233774Shselasky		else if (actlen > (int)sizeof(struct usb_cdc_notification)) {
1397202181Sthompsa			UHSO_DPRINTF(0, "UCDC notification too large: %d\n", actlen);
1398202181Sthompsa			actlen = sizeof(struct usb_cdc_notification);
1399202181Sthompsa		}
1400202181Sthompsa
1401202181Sthompsa		pc = usbd_xfer_get_frame(xfer, 0);
1402202181Sthompsa		usbd_copy_out(pc, 0, &cdc, actlen);
1403202181Sthompsa
1404202181Sthompsa		if (UGETW(cdc.wIndex) != sc->sc_iface_no) {
1405202243Sthompsa			UHSO_DPRINTF(0, "Interface mismatch, got %d expected %d\n",
1406202181Sthompsa			    UGETW(cdc.wIndex), sc->sc_iface_no);
1407202181Sthompsa			goto tr_setup;
1408202181Sthompsa		}
1409202181Sthompsa
1410202181Sthompsa		if (cdc.bmRequestType == UCDC_NOTIFICATION &&
1411202181Sthompsa		    cdc.bNotification == UCDC_N_SERIAL_STATE) {
1412202243Sthompsa			UHSO_DPRINTF(2, "notify = 0x%02x\n", cdc.data[0]);
1413202181Sthompsa
1414202181Sthompsa			sc->sc_msr = 0;
1415202181Sthompsa			sc->sc_lsr = 0;
1416202181Sthompsa			if (cdc.data[0] & UCDC_N_SERIAL_RI)
1417202181Sthompsa				sc->sc_msr |= SER_RI;
1418202181Sthompsa			if (cdc.data[0] & UCDC_N_SERIAL_DSR)
1419202181Sthompsa				sc->sc_msr |= SER_DSR;
1420202181Sthompsa			if (cdc.data[0] & UCDC_N_SERIAL_DCD)
1421202181Sthompsa				sc->sc_msr |= SER_DCD;
1422202181Sthompsa
1423202181Sthompsa			ucom_status_change(&sc->sc_ucom[0]);
1424202181Sthompsa		}
1425202181Sthompsa	case USB_ST_SETUP:
1426202181Sthompsatr_setup:
1427202181Sthompsa	default:
1428202181Sthompsa		if (error == USB_ERR_CANCELLED)
1429202181Sthompsa			break;
1430202181Sthompsa		usbd_xfer_set_stall(xfer);
1431202181Sthompsa		goto tr_setup;
1432202181Sthompsa	}
1433202181Sthompsa}
1434202181Sthompsa
1435202181Sthompsastatic void
1436202181Sthompsauhso_ucom_cfg_get_status(struct ucom_softc *ucom, uint8_t *lsr, uint8_t *msr)
1437202181Sthompsa{
1438202181Sthompsa	struct uhso_softc *sc = ucom->sc_parent;
1439202181Sthompsa
1440202181Sthompsa	*lsr = sc->sc_lsr;
1441202181Sthompsa	*msr = sc->sc_msr;
1442202181Sthompsa}
1443202181Sthompsa
1444202181Sthompsastatic void
1445202181Sthompsauhso_ucom_cfg_set_dtr(struct ucom_softc *ucom, uint8_t onoff)
1446202181Sthompsa{
1447202181Sthompsa	struct uhso_softc *sc = ucom->sc_parent;
1448202181Sthompsa
1449202181Sthompsa	if (!(UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_BULK))
1450202181Sthompsa		return;
1451202181Sthompsa
1452202181Sthompsa	if (onoff)
1453202181Sthompsa		sc->sc_line |= UCDC_LINE_DTR;
1454202181Sthompsa	else
1455208017Sthompsa		sc->sc_line &= ~UCDC_LINE_DTR;
1456202181Sthompsa
1457202181Sthompsa	uhso_bs_cfg(sc);
1458202181Sthompsa}
1459202181Sthompsa
1460202181Sthompsastatic void
1461202181Sthompsauhso_ucom_cfg_set_rts(struct ucom_softc *ucom, uint8_t onoff)
1462202181Sthompsa{
1463202181Sthompsa	struct uhso_softc *sc = ucom->sc_parent;
1464202181Sthompsa
1465202181Sthompsa	if (!(UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_BULK))
1466202181Sthompsa		return;
1467202181Sthompsa
1468202181Sthompsa	if (onoff)
1469202181Sthompsa		sc->sc_line |= UCDC_LINE_RTS;
1470202181Sthompsa	else
1471208017Sthompsa		sc->sc_line &= ~UCDC_LINE_RTS;
1472202181Sthompsa
1473202181Sthompsa	uhso_bs_cfg(sc);
1474202181Sthompsa}
1475202181Sthompsa
1476202181Sthompsastatic void
1477202181Sthompsauhso_ucom_start_read(struct ucom_softc *ucom)
1478202181Sthompsa{
1479202181Sthompsa	struct uhso_softc *sc = ucom->sc_parent;
1480202181Sthompsa
1481214761Sn_hibma	UHSO_DPRINTF(3, "unit=%d, subunit=%d\n",
1482214761Sn_hibma	    ucom->sc_super->sc_unit, ucom->sc_subunit);
1483202181Sthompsa
1484202181Sthompsa	if (UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_MUX) {
1485214761Sn_hibma		sc->sc_tty[ucom->sc_subunit].ht_open = 1;
1486202181Sthompsa		usbd_transfer_start(sc->sc_xfer[UHSO_MUX_ENDPT_INTR]);
1487202181Sthompsa	}
1488202181Sthompsa	else if (UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_BULK) {
1489202181Sthompsa		sc->sc_tty[0].ht_open = 1;
1490202181Sthompsa		usbd_transfer_start(sc->sc_xfer[UHSO_BULK_ENDPT_READ]);
1491202181Sthompsa		if (sc->sc_xfer[UHSO_BULK_ENDPT_INTR] != NULL)
1492202181Sthompsa			usbd_transfer_start(sc->sc_xfer[UHSO_BULK_ENDPT_INTR]);
1493202181Sthompsa	}
1494202181Sthompsa}
1495202181Sthompsa
1496202181Sthompsastatic void
1497202181Sthompsauhso_ucom_stop_read(struct ucom_softc *ucom)
1498202181Sthompsa{
1499202181Sthompsa
1500202181Sthompsa	struct uhso_softc *sc = ucom->sc_parent;
1501202181Sthompsa
1502202181Sthompsa	if (UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_MUX) {
1503214761Sn_hibma		sc->sc_tty[ucom->sc_subunit].ht_open = 0;
1504202181Sthompsa		usbd_transfer_stop(
1505214761Sn_hibma		    sc->sc_tty[ucom->sc_subunit].ht_xfer[UHSO_CTRL_READ]);
1506202181Sthompsa	}
1507202181Sthompsa	else if (UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_BULK) {
1508202181Sthompsa		sc->sc_tty[0].ht_open = 0;
1509202181Sthompsa		usbd_transfer_start(sc->sc_xfer[UHSO_BULK_ENDPT_READ]);
1510202181Sthompsa		if (sc->sc_xfer[UHSO_BULK_ENDPT_INTR] != NULL)
1511202181Sthompsa			usbd_transfer_stop(sc->sc_xfer[UHSO_BULK_ENDPT_INTR]);
1512202181Sthompsa	}
1513202181Sthompsa}
1514202181Sthompsa
1515202181Sthompsastatic void
1516202181Sthompsauhso_ucom_start_write(struct ucom_softc *ucom)
1517202181Sthompsa{
1518202181Sthompsa	struct uhso_softc *sc = ucom->sc_parent;
1519202181Sthompsa
1520202181Sthompsa	if (UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_MUX) {
1521214761Sn_hibma		UHSO_DPRINTF(3, "local unit %d\n", ucom->sc_subunit);
1522202181Sthompsa
1523202181Sthompsa		usbd_transfer_start(sc->sc_xfer[UHSO_MUX_ENDPT_INTR]);
1524202181Sthompsa
1525202181Sthompsa		usbd_xfer_set_priv(
1526214761Sn_hibma		    sc->sc_tty[ucom->sc_subunit].ht_xfer[UHSO_CTRL_WRITE],
1527214761Sn_hibma		    &sc->sc_tty[ucom->sc_subunit]);
1528202181Sthompsa		usbd_transfer_start(
1529214761Sn_hibma		    sc->sc_tty[ucom->sc_subunit].ht_xfer[UHSO_CTRL_WRITE]);
1530202181Sthompsa
1531202181Sthompsa	}
1532202181Sthompsa	else if (UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_BULK) {
1533202181Sthompsa		usbd_transfer_start(sc->sc_xfer[UHSO_BULK_ENDPT_WRITE]);
1534202181Sthompsa	}
1535202181Sthompsa}
1536202181Sthompsa
1537202181Sthompsastatic void
1538202181Sthompsauhso_ucom_stop_write(struct ucom_softc *ucom)
1539202181Sthompsa{
1540202181Sthompsa	struct uhso_softc *sc = ucom->sc_parent;
1541202181Sthompsa
1542202181Sthompsa	if (UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_MUX) {
1543202181Sthompsa		usbd_transfer_stop(
1544214761Sn_hibma		    sc->sc_tty[ucom->sc_subunit].ht_xfer[UHSO_CTRL_WRITE]);
1545202181Sthompsa	}
1546202181Sthompsa	else if (UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_BULK) {
1547202181Sthompsa		usbd_transfer_stop(sc->sc_xfer[UHSO_BULK_ENDPT_WRITE]);
1548202181Sthompsa	}
1549202181Sthompsa}
1550202181Sthompsa
1551210275Sthompsastatic int
1552210275Sthompsauhso_attach_ifnet(struct uhso_softc *sc, struct usb_interface *iface, int type)
1553202181Sthompsa{
1554202181Sthompsa	struct ifnet *ifp;
1555202181Sthompsa	usb_error_t uerr;
1556202181Sthompsa	struct sysctl_ctx_list *sctx;
1557202181Sthompsa	struct sysctl_oid *soid;
1558210275Sthompsa	unsigned int devunit;
1559202181Sthompsa
1560202181Sthompsa	uerr = usbd_transfer_setup(sc->sc_udev,
1561202181Sthompsa	    &iface->idesc->bInterfaceNumber, sc->sc_if_xfer,
1562202181Sthompsa	    uhso_ifnet_config, UHSO_IFNET_MAX, sc, &sc->sc_mtx);
1563202181Sthompsa	if (uerr) {
1564202181Sthompsa		UHSO_DPRINTF(0, "usbd_transfer_setup failed: %s\n",
1565202181Sthompsa		    usbd_errstr(uerr));
1566202181Sthompsa		return (-1);
1567202181Sthompsa	}
1568202181Sthompsa
1569202243Sthompsa	sc->sc_ifp = ifp = if_alloc(IFT_OTHER);
1570202181Sthompsa	if (sc->sc_ifp == NULL) {
1571202181Sthompsa		device_printf(sc->sc_dev, "if_alloc() failed\n");
1572202181Sthompsa		return (-1);
1573202181Sthompsa	}
1574202181Sthompsa
1575202181Sthompsa	callout_init_mtx(&sc->sc_c, &sc->sc_mtx, 0);
1576202181Sthompsa	mtx_lock(&sc->sc_mtx);
1577202181Sthompsa	callout_reset(&sc->sc_c, 1, uhso_if_rxflush, sc);
1578202181Sthompsa	mtx_unlock(&sc->sc_mtx);
1579202181Sthompsa
1580210275Sthompsa	/*
1581210275Sthompsa	 * We create our own unit numbers for ifnet devices because the
1582210275Sthompsa	 * USB interface unit numbers can be at arbitrary positions yielding
1583210275Sthompsa	 * odd looking device names.
1584210275Sthompsa	 */
1585210275Sthompsa	devunit = alloc_unr(uhso_ifnet_unit);
1586210275Sthompsa
1587210275Sthompsa	if_initname(ifp, device_get_name(sc->sc_dev), devunit);
1588202181Sthompsa	ifp->if_mtu = UHSO_MAX_MTU;
1589202181Sthompsa	ifp->if_ioctl = uhso_if_ioctl;
1590202181Sthompsa	ifp->if_init = uhso_if_init;
1591202181Sthompsa	ifp->if_start = uhso_if_start;
1592202181Sthompsa	ifp->if_output = uhso_if_output;
1593213803Shselasky	ifp->if_flags = IFF_BROADCAST | IFF_MULTICAST | IFF_NOARP;
1594202181Sthompsa	ifp->if_softc = sc;
1595207554Ssobomax	IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
1596207554Ssobomax	ifp->if_snd.ifq_drv_maxlen = ifqmaxlen;
1597202181Sthompsa	IFQ_SET_READY(&ifp->if_snd);
1598202181Sthompsa
1599202181Sthompsa	if_attach(ifp);
1600202181Sthompsa	bpfattach(ifp, DLT_RAW, 0);
1601202181Sthompsa
1602202181Sthompsa	sctx = device_get_sysctl_ctx(sc->sc_dev);
1603202181Sthompsa	soid = device_get_sysctl_tree(sc->sc_dev);
1604202181Sthompsa	/* Unlocked read... */
1605202181Sthompsa	SYSCTL_ADD_STRING(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "netif",
1606202181Sthompsa	    CTLFLAG_RD, ifp->if_xname, 0, "Attached network interface");
1607202181Sthompsa
1608202181Sthompsa	return (0);
1609202181Sthompsa}
1610202181Sthompsa
1611202181Sthompsastatic void
1612202181Sthompsauhso_ifnet_read_callback(struct usb_xfer *xfer, usb_error_t error)
1613202181Sthompsa{
1614202181Sthompsa	struct uhso_softc *sc = usbd_xfer_softc(xfer);
1615202181Sthompsa	struct mbuf *m;
1616202181Sthompsa	struct usb_page_cache *pc;
1617202181Sthompsa	int actlen;
1618202181Sthompsa
1619202181Sthompsa	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
1620202181Sthompsa
1621202181Sthompsa	UHSO_DPRINTF(3, "status=%d, actlen=%d\n", USB_GET_STATE(xfer), actlen);
1622202181Sthompsa
1623202181Sthompsa	switch (USB_GET_STATE(xfer)) {
1624202181Sthompsa	case USB_ST_TRANSFERRED:
1625202181Sthompsa		if (actlen > 0 && (sc->sc_ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1626202181Sthompsa			pc = usbd_xfer_get_frame(xfer, 0);
1627243857Sglebius			m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
1628202181Sthompsa			usbd_copy_out(pc, 0, mtod(m, uint8_t *), actlen);
1629202181Sthompsa			m->m_pkthdr.len = m->m_len = actlen;
1630202243Sthompsa			/* Enqueue frame for further processing */
1631202181Sthompsa			_IF_ENQUEUE(&sc->sc_rxq, m);
1632202181Sthompsa			if (!callout_pending(&sc->sc_c) ||
1633202181Sthompsa			    !callout_active(&sc->sc_c)) {
1634202181Sthompsa				callout_schedule(&sc->sc_c, 1);
1635202181Sthompsa			}
1636202181Sthompsa		}
1637202181Sthompsa	/* FALLTHROUGH */
1638202181Sthompsa	case USB_ST_SETUP:
1639202181Sthompsatr_setup:
1640202181Sthompsa		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
1641202181Sthompsa		usbd_transfer_submit(xfer);
1642202181Sthompsa		break;
1643202181Sthompsa	default:
1644202181Sthompsa		UHSO_DPRINTF(0, "error: %s\n", usbd_errstr(error));
1645202181Sthompsa		if (error == USB_ERR_CANCELLED)
1646202181Sthompsa			break;
1647202181Sthompsa		usbd_xfer_set_stall(xfer);
1648202181Sthompsa		goto tr_setup;
1649202181Sthompsa	}
1650202181Sthompsa}
1651202181Sthompsa
1652202181Sthompsa/*
1653202243Sthompsa * Deferred RX processing, called with mutex locked.
1654202243Sthompsa *
1655202270Sthompsa * Each frame we receive might contain several small ip-packets as well
1656202243Sthompsa * as partial ip-packets. We need to separate/assemble them into individual
1657202243Sthompsa * packets before sending them to the ip-layer.
1658202181Sthompsa */
1659202181Sthompsastatic void
1660202181Sthompsauhso_if_rxflush(void *arg)
1661202181Sthompsa{
1662202181Sthompsa	struct uhso_softc *sc = arg;
1663202181Sthompsa	struct ifnet *ifp = sc->sc_ifp;
1664202181Sthompsa	uint8_t *cp;
1665202181Sthompsa	struct mbuf *m, *m0, *mwait;
1666202181Sthompsa	struct ip *ip;
1667202181Sthompsa#ifdef INET6
1668202181Sthompsa	struct ip6_hdr *ip6;
1669202181Sthompsa#endif
1670202181Sthompsa	uint16_t iplen;
1671296304Smarkj	int isr;
1672202181Sthompsa
1673202181Sthompsa	m = NULL;
1674202181Sthompsa	mwait = sc->sc_mwait;
1675202181Sthompsa	for (;;) {
1676202181Sthompsa		if (m == NULL) {
1677202181Sthompsa			_IF_DEQUEUE(&sc->sc_rxq, m);
1678202181Sthompsa			if (m == NULL)
1679202181Sthompsa				break;
1680202243Sthompsa			UHSO_DPRINTF(3, "dequeue m=%p, len=%d\n", m, m->m_len);
1681202181Sthompsa		}
1682202181Sthompsa		mtx_unlock(&sc->sc_mtx);
1683202181Sthompsa
1684202181Sthompsa		/* Do we have a partial packet waiting? */
1685202181Sthompsa		if (mwait != NULL) {
1686202181Sthompsa			m0 = mwait;
1687202181Sthompsa			mwait = NULL;
1688202181Sthompsa
1689202243Sthompsa			UHSO_DPRINTF(3, "partial m0=%p(%d), concat w/ m=%p(%d)\n",
1690202181Sthompsa			    m0, m0->m_len, m, m->m_len);
1691202181Sthompsa
1692296304Smarkj			m_catpkt(m0, m);
1693202181Sthompsa			m = m_pullup(m0, sizeof(struct ip));
1694202181Sthompsa			if (m == NULL) {
1695271832Sglebius				if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1696202181Sthompsa				UHSO_DPRINTF(0, "m_pullup failed\n");
1697202181Sthompsa				mtx_lock(&sc->sc_mtx);
1698202181Sthompsa				continue;
1699202181Sthompsa			}
1700202243Sthompsa			UHSO_DPRINTF(3, "Constructed mbuf=%p, len=%d\n",
1701202181Sthompsa			    m, m->m_pkthdr.len);
1702202181Sthompsa		}
1703202181Sthompsa
1704202181Sthompsa		cp = mtod(m, uint8_t *);
1705202181Sthompsa		ip = (struct ip *)cp;
1706202181Sthompsa#ifdef INET6
1707202181Sthompsa		ip6 = (struct ip6_hdr *)cp;
1708202181Sthompsa#endif
1709202181Sthompsa
1710202181Sthompsa		/* Check for IPv4 */
1711202181Sthompsa		if (ip->ip_v == IPVERSION) {
1712202181Sthompsa			iplen = htons(ip->ip_len);
1713202181Sthompsa			isr = NETISR_IP;
1714202181Sthompsa		}
1715202181Sthompsa#ifdef INET6
1716202181Sthompsa		/* Check for IPv6 */
1717202181Sthompsa		else if ((ip6->ip6_vfc & IPV6_VERSION_MASK) == IPV6_VERSION) {
1718202181Sthompsa			iplen = htons(ip6->ip6_plen);
1719202181Sthompsa			isr = NETISR_IPV6;
1720202181Sthompsa		}
1721202181Sthompsa#endif
1722202181Sthompsa		else {
1723202181Sthompsa			UHSO_DPRINTF(0, "got unexpected ip version %d, "
1724202181Sthompsa			    "m=%p, len=%d\n", (*cp & 0xf0) >> 4, m, m->m_len);
1725271832Sglebius			if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1726202181Sthompsa			UHSO_HEXDUMP(cp, 4);
1727202181Sthompsa			m_freem(m);
1728202181Sthompsa			m = NULL;
1729202181Sthompsa			mtx_lock(&sc->sc_mtx);
1730202181Sthompsa			continue;
1731202181Sthompsa		}
1732202181Sthompsa
1733202181Sthompsa		if (iplen == 0) {
1734202181Sthompsa			UHSO_DPRINTF(0, "Zero IP length\n");
1735271832Sglebius			if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1736202181Sthompsa			m_freem(m);
1737202181Sthompsa			m = NULL;
1738202181Sthompsa			mtx_lock(&sc->sc_mtx);
1739202181Sthompsa			continue;
1740202181Sthompsa		}
1741202181Sthompsa
1742202243Sthompsa		UHSO_DPRINTF(3, "m=%p, len=%d, cp=%p, iplen=%d\n",
1743202181Sthompsa		    m, m->m_pkthdr.len, cp, iplen);
1744202181Sthompsa
1745202181Sthompsa		m0 = NULL;
1746202181Sthompsa
1747202181Sthompsa		/* More IP packets in this mbuf */
1748202181Sthompsa		if (iplen < m->m_pkthdr.len) {
1749202181Sthompsa			m0 = m;
1750202181Sthompsa
1751202181Sthompsa			/*
1752202181Sthompsa			 * Allocate a new mbuf for this IP packet and
1753202181Sthompsa			 * copy the IP-packet into it.
1754202181Sthompsa			 */
1755243857Sglebius			m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
1756227461Shselasky			memcpy(mtod(m, uint8_t *), mtod(m0, uint8_t *), iplen);
1757202181Sthompsa			m->m_pkthdr.len = m->m_len = iplen;
1758202181Sthompsa
1759202181Sthompsa			/* Adjust the size of the original mbuf */
1760202181Sthompsa			m_adj(m0, iplen);
1761243857Sglebius			m0 = m_defrag(m0, M_WAITOK);
1762202181Sthompsa
1763202243Sthompsa			UHSO_DPRINTF(3, "New mbuf=%p, len=%d/%d, m0=%p, "
1764202181Sthompsa			    "m0_len=%d/%d\n", m, m->m_pkthdr.len, m->m_len,
1765202181Sthompsa			    m0, m0->m_pkthdr.len, m0->m_len);
1766202181Sthompsa		}
1767202181Sthompsa		else if (iplen > m->m_pkthdr.len) {
1768202243Sthompsa			UHSO_DPRINTF(3, "Deferred mbuf=%p, len=%d\n",
1769202181Sthompsa			    m, m->m_pkthdr.len);
1770202181Sthompsa			mwait = m;
1771202181Sthompsa			m = NULL;
1772202181Sthompsa			mtx_lock(&sc->sc_mtx);
1773202181Sthompsa			continue;
1774202181Sthompsa		}
1775202181Sthompsa
1776271832Sglebius		if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
1777202181Sthompsa		m->m_pkthdr.rcvif = ifp;
1778202181Sthompsa
1779202181Sthompsa		/* Dispatch to IP layer */
1780202181Sthompsa		BPF_MTAP(sc->sc_ifp, m);
1781223741Sbz		M_SETFIB(m, ifp->if_fib);
1782202181Sthompsa		netisr_dispatch(isr, m);
1783202181Sthompsa		m = m0 != NULL ? m0 : NULL;
1784202181Sthompsa		mtx_lock(&sc->sc_mtx);
1785202181Sthompsa	}
1786202181Sthompsa	sc->sc_mwait = mwait;
1787202181Sthompsa}
1788202181Sthompsa
1789202181Sthompsastatic void
1790202181Sthompsauhso_ifnet_write_callback(struct usb_xfer *xfer, usb_error_t error)
1791202181Sthompsa{
1792202181Sthompsa	struct uhso_softc *sc = usbd_xfer_softc(xfer);
1793202181Sthompsa	struct ifnet *ifp = sc->sc_ifp;
1794202181Sthompsa	struct usb_page_cache *pc;
1795202181Sthompsa	struct mbuf *m;
1796202181Sthompsa	int actlen;
1797202181Sthompsa
1798202181Sthompsa	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
1799202181Sthompsa
1800202181Sthompsa	UHSO_DPRINTF(3, "status %d, actlen=%d\n", USB_GET_STATE(xfer), actlen);
1801202181Sthompsa
1802202181Sthompsa	switch (USB_GET_STATE(xfer)) {
1803202181Sthompsa	case USB_ST_TRANSFERRED:
1804271832Sglebius		if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
1805202181Sthompsa		ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1806202181Sthompsa	case USB_ST_SETUP:
1807202181Sthompsatr_setup:
1808202181Sthompsa		IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
1809202181Sthompsa		if (m == NULL)
1810202181Sthompsa			break;
1811202181Sthompsa
1812202181Sthompsa		ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1813202181Sthompsa
1814202181Sthompsa		if (m->m_pkthdr.len > MCLBYTES)
1815202181Sthompsa			m->m_pkthdr.len = MCLBYTES;
1816202181Sthompsa
1817202181Sthompsa		usbd_xfer_set_frame_len(xfer, 0, m->m_pkthdr.len);
1818202181Sthompsa		pc = usbd_xfer_get_frame(xfer, 0);
1819202181Sthompsa		usbd_m_copy_in(pc, 0, m, 0, m->m_pkthdr.len);
1820202181Sthompsa		usbd_transfer_submit(xfer);
1821202181Sthompsa
1822202181Sthompsa		BPF_MTAP(ifp, m);
1823202181Sthompsa		m_freem(m);
1824202181Sthompsa		break;
1825202181Sthompsa	default:
1826202181Sthompsa		UHSO_DPRINTF(0, "error: %s\n", usbd_errstr(error));
1827202181Sthompsa		if (error == USB_ERR_CANCELLED)
1828202181Sthompsa			break;
1829202181Sthompsa		usbd_xfer_set_stall(xfer);
1830202181Sthompsa		goto tr_setup;
1831202181Sthompsa	}
1832202181Sthompsa}
1833202181Sthompsa
1834202181Sthompsastatic int
1835202181Sthompsauhso_if_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1836202181Sthompsa{
1837202181Sthompsa	struct uhso_softc *sc;
1838202181Sthompsa
1839202181Sthompsa	sc = ifp->if_softc;
1840202181Sthompsa
1841202181Sthompsa	switch (cmd) {
1842202181Sthompsa	case SIOCSIFFLAGS:
1843202181Sthompsa		if (ifp->if_flags & IFF_UP) {
1844202181Sthompsa			if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1845202181Sthompsa				uhso_if_init(sc);
1846202181Sthompsa			}
1847202181Sthompsa		}
1848202181Sthompsa		else {
1849202181Sthompsa			if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1850202181Sthompsa				mtx_lock(&sc->sc_mtx);
1851202181Sthompsa				uhso_if_stop(sc);
1852202181Sthompsa				mtx_unlock(&sc->sc_mtx);
1853202181Sthompsa			}
1854202181Sthompsa		}
1855202181Sthompsa		break;
1856202181Sthompsa	case SIOCSIFADDR:
1857202181Sthompsa	case SIOCADDMULTI:
1858202181Sthompsa	case SIOCDELMULTI:
1859202181Sthompsa		break;
1860202181Sthompsa	default:
1861202181Sthompsa		return (EINVAL);
1862202181Sthompsa	}
1863202181Sthompsa	return (0);
1864202181Sthompsa}
1865202181Sthompsa
1866202181Sthompsastatic void
1867202181Sthompsauhso_if_init(void *priv)
1868202181Sthompsa{
1869202181Sthompsa	struct uhso_softc *sc = priv;
1870202181Sthompsa	struct ifnet *ifp = sc->sc_ifp;
1871202181Sthompsa
1872202181Sthompsa	mtx_lock(&sc->sc_mtx);
1873202181Sthompsa	uhso_if_stop(sc);
1874202181Sthompsa	ifp = sc->sc_ifp;
1875202181Sthompsa	ifp->if_flags |= IFF_UP;
1876202181Sthompsa	ifp->if_drv_flags |= IFF_DRV_RUNNING;
1877202181Sthompsa	mtx_unlock(&sc->sc_mtx);
1878202181Sthompsa
1879202243Sthompsa	UHSO_DPRINTF(2, "ifnet initialized\n");
1880202181Sthompsa}
1881202181Sthompsa
1882202181Sthompsastatic int
1883249925Sglebiusuhso_if_output(struct ifnet *ifp, struct mbuf *m0, const struct sockaddr *dst,
1884202181Sthompsa    struct route *ro)
1885202181Sthompsa{
1886202181Sthompsa	int error;
1887202181Sthompsa
1888202181Sthompsa	/* Only IPv4/6 support */
1889202181Sthompsa	if (dst->sa_family != AF_INET
1890202181Sthompsa#ifdef INET6
1891202181Sthompsa	   && dst->sa_family != AF_INET6
1892202181Sthompsa#endif
1893202181Sthompsa	 ) {
1894202181Sthompsa		return (EAFNOSUPPORT);
1895202181Sthompsa	}
1896202181Sthompsa
1897202181Sthompsa	error = (ifp->if_transmit)(ifp, m0);
1898202181Sthompsa	if (error) {
1899271832Sglebius		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1900202181Sthompsa		return (ENOBUFS);
1901202181Sthompsa	}
1902271832Sglebius	if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
1903202181Sthompsa	return (0);
1904202181Sthompsa}
1905202181Sthompsa
1906202181Sthompsastatic void
1907202181Sthompsauhso_if_start(struct ifnet *ifp)
1908202181Sthompsa{
1909202181Sthompsa	struct uhso_softc *sc = ifp->if_softc;
1910202181Sthompsa
1911202181Sthompsa	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
1912202181Sthompsa		UHSO_DPRINTF(1, "Not running\n");
1913202181Sthompsa		return;
1914202181Sthompsa	}
1915202181Sthompsa
1916202181Sthompsa	mtx_lock(&sc->sc_mtx);
1917202181Sthompsa	usbd_transfer_start(sc->sc_if_xfer[UHSO_IFNET_READ]);
1918202181Sthompsa	usbd_transfer_start(sc->sc_if_xfer[UHSO_IFNET_WRITE]);
1919202181Sthompsa	mtx_unlock(&sc->sc_mtx);
1920202181Sthompsa	UHSO_DPRINTF(3, "interface started\n");
1921202181Sthompsa}
1922202181Sthompsa
1923202181Sthompsastatic void
1924202181Sthompsauhso_if_stop(struct uhso_softc *sc)
1925202181Sthompsa{
1926202181Sthompsa
1927202181Sthompsa	usbd_transfer_stop(sc->sc_if_xfer[UHSO_IFNET_READ]);
1928202181Sthompsa	usbd_transfer_stop(sc->sc_if_xfer[UHSO_IFNET_WRITE]);
1929202181Sthompsa	sc->sc_ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
1930202181Sthompsa}
1931