xref: /freebsd/sys/dev/usb/serial/u3g.c (revision f29a072444d6e31d57f7edd94f952395ae7bb22e)
102ac6454SAndrew Thompson /*
202ac6454SAndrew Thompson  * Copyright (c) 2008 AnyWi Technologies
302ac6454SAndrew Thompson  * Author: Andrea Guzzo <aguzzo@anywi.com>
402ac6454SAndrew Thompson  * * based on uark.c 1.1 2006/08/14 08:30:22 jsg *
502ac6454SAndrew Thompson  * * parts from ubsa.c 183348 2008-09-25 12:00:56Z phk *
602ac6454SAndrew Thompson  *
702ac6454SAndrew Thompson  * Permission to use, copy, modify, and distribute this software for any
802ac6454SAndrew Thompson  * purpose with or without fee is hereby granted, provided that the above
902ac6454SAndrew Thompson  * copyright notice and this permission notice appear in all copies.
1002ac6454SAndrew Thompson  *
1102ac6454SAndrew Thompson  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1202ac6454SAndrew Thompson  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1302ac6454SAndrew Thompson  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1402ac6454SAndrew Thompson  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1502ac6454SAndrew Thompson  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1602ac6454SAndrew Thompson  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1702ac6454SAndrew Thompson  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1802ac6454SAndrew Thompson  *
1902ac6454SAndrew Thompson  * $FreeBSD$
2002ac6454SAndrew Thompson  */
2102ac6454SAndrew Thompson 
2202ac6454SAndrew Thompson /*
2302ac6454SAndrew Thompson  * NOTE:
2402ac6454SAndrew Thompson  *
2502ac6454SAndrew Thompson  * - The detour through the tty layer is ridiculously expensive wrt
2602ac6454SAndrew Thompson  *   buffering due to the high speeds.
2702ac6454SAndrew Thompson  *
2802ac6454SAndrew Thompson  *   We should consider adding a simple r/w device which allows
2902ac6454SAndrew Thompson  *   attaching of PPP in a more efficient way.
3002ac6454SAndrew Thompson  *
3102ac6454SAndrew Thompson  */
3202ac6454SAndrew Thompson 
3302ac6454SAndrew Thompson #include "usbdevs.h"
3402ac6454SAndrew Thompson #include <dev/usb/usb.h>
3502ac6454SAndrew Thompson #include <dev/usb/usb_mfunc.h>
3602ac6454SAndrew Thompson #include <dev/usb/usb_error.h>
3702ac6454SAndrew Thompson 
3802ac6454SAndrew Thompson #define	USB_DEBUG_VAR u3g_debug
3902ac6454SAndrew Thompson 
4002ac6454SAndrew Thompson #include <dev/usb/usb_core.h>
4102ac6454SAndrew Thompson #include <dev/usb/usb_debug.h>
4202ac6454SAndrew Thompson #include <dev/usb/usb_process.h>
4302ac6454SAndrew Thompson #include <dev/usb/usb_request.h>
4402ac6454SAndrew Thompson #include <dev/usb/usb_lookup.h>
4502ac6454SAndrew Thompson #include <dev/usb/usb_util.h>
4602ac6454SAndrew Thompson #include <dev/usb/usb_busdma.h>
4702ac6454SAndrew Thompson #include <dev/usb/usb_msctest.h>
4802ac6454SAndrew Thompson #include <dev/usb/usb_dynamic.h>
4902ac6454SAndrew Thompson #include <dev/usb/usb_device.h>
5002ac6454SAndrew Thompson 
5102ac6454SAndrew Thompson #include <dev/usb/serial/usb_serial.h>
5202ac6454SAndrew Thompson 
5302ac6454SAndrew Thompson #if USB_DEBUG
5402ac6454SAndrew Thompson static int u3g_debug = 0;
5502ac6454SAndrew Thompson 
56750a2682SAndrew Thompson SYSCTL_NODE(_hw_usb2, OID_AUTO, u3g, CTLFLAG_RW, 0, "USB 3g");
5702ac6454SAndrew Thompson SYSCTL_INT(_hw_usb2_u3g, OID_AUTO, debug, CTLFLAG_RW,
58750a2682SAndrew Thompson     &u3g_debug, 0, "Debug level");
5902ac6454SAndrew Thompson #endif
6002ac6454SAndrew Thompson 
61426909d9SAndrew Thompson #define	U3G_MAXPORTS		8
6202ac6454SAndrew Thompson #define	U3G_CONFIG_INDEX	0
6302ac6454SAndrew Thompson #define	U3G_BSIZE		2048
6402ac6454SAndrew Thompson 
6502ac6454SAndrew Thompson #define	U3GSP_GPRS		0
6602ac6454SAndrew Thompson #define	U3GSP_EDGE		1
6702ac6454SAndrew Thompson #define	U3GSP_CDMA		2
6802ac6454SAndrew Thompson #define	U3GSP_UMTS		3
6902ac6454SAndrew Thompson #define	U3GSP_HSDPA		4
7002ac6454SAndrew Thompson #define	U3GSP_HSUPA		5
7102ac6454SAndrew Thompson #define	U3GSP_HSPA		6
7202ac6454SAndrew Thompson #define	U3GSP_MAX		7
7302ac6454SAndrew Thompson 
74750a2682SAndrew Thompson #define	U3GFL_HUAWEI_INIT	0x0001	/* Init command required */
75750a2682SAndrew Thompson #define	U3GFL_SCSI_EJECT	0x0002	/* SCSI eject command required */
76750a2682SAndrew Thompson #define	U3GFL_SIERRA_INIT	0x0004	/* Init command required */
77750a2682SAndrew Thompson #define	U3GFL_SAEL_M460_INIT	0x0008	/* Init device */
7802ac6454SAndrew Thompson 
7902ac6454SAndrew Thompson enum {
8002ac6454SAndrew Thompson 	U3G_BULK_WR,
8102ac6454SAndrew Thompson 	U3G_BULK_RD,
8202ac6454SAndrew Thompson 	U3G_N_TRANSFER,
8302ac6454SAndrew Thompson };
8402ac6454SAndrew Thompson 
8502ac6454SAndrew Thompson struct u3g_softc {
8602ac6454SAndrew Thompson 	struct usb2_com_super_softc sc_super_ucom;
8702ac6454SAndrew Thompson 	struct usb2_com_softc sc_ucom[U3G_MAXPORTS];
8802ac6454SAndrew Thompson 
8902ac6454SAndrew Thompson 	struct usb2_xfer *sc_xfer[U3G_MAXPORTS][U3G_N_TRANSFER];
9002ac6454SAndrew Thompson 	struct usb2_device *sc_udev;
91deefe583SAndrew Thompson 	struct mtx sc_mtx;
9202ac6454SAndrew Thompson 
9302ac6454SAndrew Thompson 	uint8_t	sc_lsr;			/* local status register */
9402ac6454SAndrew Thompson 	uint8_t	sc_msr;			/* U3G status register */
9502ac6454SAndrew Thompson 	uint8_t	sc_numports;
9602ac6454SAndrew Thompson };
9702ac6454SAndrew Thompson 
9802ac6454SAndrew Thompson static device_probe_t u3g_probe;
9902ac6454SAndrew Thompson static device_attach_t u3g_attach;
10002ac6454SAndrew Thompson static device_detach_t u3g_detach;
10102ac6454SAndrew Thompson 
10202ac6454SAndrew Thompson static usb2_callback_t u3g_write_callback;
10302ac6454SAndrew Thompson static usb2_callback_t u3g_read_callback;
10402ac6454SAndrew Thompson 
10502ac6454SAndrew Thompson static void u3g_start_read(struct usb2_com_softc *ucom);
10602ac6454SAndrew Thompson static void u3g_stop_read(struct usb2_com_softc *ucom);
10702ac6454SAndrew Thompson static void u3g_start_write(struct usb2_com_softc *ucom);
10802ac6454SAndrew Thompson static void u3g_stop_write(struct usb2_com_softc *ucom);
10902ac6454SAndrew Thompson 
11002ac6454SAndrew Thompson static int u3g_driver_loaded(struct module *mod, int what, void *arg);
11102ac6454SAndrew Thompson 
11202ac6454SAndrew Thompson static const struct usb2_config u3g_config[U3G_N_TRANSFER] = {
11302ac6454SAndrew Thompson 
11402ac6454SAndrew Thompson 	[U3G_BULK_WR] = {
11502ac6454SAndrew Thompson 		.type = UE_BULK,
11602ac6454SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
11702ac6454SAndrew Thompson 		.direction = UE_DIR_OUT,
1184eae601eSAndrew Thompson 		.bufsize = U3G_BSIZE,/* bytes */
1194eae601eSAndrew Thompson 		.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
1204eae601eSAndrew Thompson 		.callback = &u3g_write_callback,
12102ac6454SAndrew Thompson 	},
12202ac6454SAndrew Thompson 
12302ac6454SAndrew Thompson 	[U3G_BULK_RD] = {
12402ac6454SAndrew Thompson 		.type = UE_BULK,
12502ac6454SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
12602ac6454SAndrew Thompson 		.direction = UE_DIR_IN,
1274eae601eSAndrew Thompson 		.bufsize = U3G_BSIZE,/* bytes */
1284eae601eSAndrew Thompson 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
1294eae601eSAndrew Thompson 		.callback = &u3g_read_callback,
13002ac6454SAndrew Thompson 	},
13102ac6454SAndrew Thompson };
13202ac6454SAndrew Thompson 
13302ac6454SAndrew Thompson static const struct usb2_com_callback u3g_callback = {
13402ac6454SAndrew Thompson 	.usb2_com_start_read = &u3g_start_read,
13502ac6454SAndrew Thompson 	.usb2_com_stop_read = &u3g_stop_read,
13602ac6454SAndrew Thompson 	.usb2_com_start_write = &u3g_start_write,
13702ac6454SAndrew Thompson 	.usb2_com_stop_write = &u3g_stop_write,
13802ac6454SAndrew Thompson };
13902ac6454SAndrew Thompson 
14002ac6454SAndrew Thompson static device_method_t u3g_methods[] = {
14102ac6454SAndrew Thompson 	DEVMETHOD(device_probe, u3g_probe),
14202ac6454SAndrew Thompson 	DEVMETHOD(device_attach, u3g_attach),
14302ac6454SAndrew Thompson 	DEVMETHOD(device_detach, u3g_detach),
14402ac6454SAndrew Thompson 	{0, 0}
14502ac6454SAndrew Thompson };
14602ac6454SAndrew Thompson 
14702ac6454SAndrew Thompson static devclass_t u3g_devclass;
14802ac6454SAndrew Thompson 
14902ac6454SAndrew Thompson static driver_t u3g_driver = {
15002ac6454SAndrew Thompson 	.name = "u3g",
15102ac6454SAndrew Thompson 	.methods = u3g_methods,
15202ac6454SAndrew Thompson 	.size = sizeof(struct u3g_softc),
15302ac6454SAndrew Thompson };
15402ac6454SAndrew Thompson 
1559aef556dSAndrew Thompson DRIVER_MODULE(u3g, uhub, u3g_driver, u3g_devclass, u3g_driver_loaded, 0);
15602ac6454SAndrew Thompson MODULE_DEPEND(u3g, ucom, 1, 1, 1);
15702ac6454SAndrew Thompson MODULE_DEPEND(u3g, usb, 1, 1, 1);
15802ac6454SAndrew Thompson 
15902ac6454SAndrew Thompson static const struct usb2_device_id u3g_devs[] = {
160a173706bSAndrew Thompson #define	U3G_DEV(v,p,i) { USB_VPI(USB_VENDOR_##v, USB_PRODUCT_##v##_##p, i) }
16102ac6454SAndrew Thompson 	/* OEM: Option */
162a173706bSAndrew Thompson 	U3G_DEV(OPTION, GT3G, 0),
163a173706bSAndrew Thompson 	U3G_DEV(OPTION, GT3GQUAD, 0),
164a173706bSAndrew Thompson 	U3G_DEV(OPTION, GT3GPLUS, 0),
165a173706bSAndrew Thompson 	U3G_DEV(OPTION, GTMAX36, 0),
166a173706bSAndrew Thompson 	U3G_DEV(OPTION, GTMAXHSUPA, 0),
167a173706bSAndrew Thompson 	U3G_DEV(OPTION, VODAFONEMC3G, 0),
16802ac6454SAndrew Thompson 	/* OEM: Qualcomm, Inc. */
169a173706bSAndrew Thompson 	U3G_DEV(QUALCOMMINC, ZTE_STOR, U3GFL_SCSI_EJECT),
170a173706bSAndrew Thompson 	U3G_DEV(QUALCOMMINC, CDMA_MSM, U3GFL_SCSI_EJECT),
17102ac6454SAndrew Thompson 	/* OEM: Huawei */
172a173706bSAndrew Thompson 	U3G_DEV(HUAWEI, MOBILE, U3GFL_HUAWEI_INIT),
173a173706bSAndrew Thompson 	U3G_DEV(HUAWEI, E220, U3GFL_HUAWEI_INIT),
17402ac6454SAndrew Thompson 	/* OEM: Novatel */
175a173706bSAndrew Thompson 	U3G_DEV(NOVATEL, CDMA_MODEM, 0),
176a173706bSAndrew Thompson 	U3G_DEV(NOVATEL, ES620, 0),
177a173706bSAndrew Thompson 	U3G_DEV(NOVATEL, MC950D, 0),
178a173706bSAndrew Thompson 	U3G_DEV(NOVATEL, U720, 0),
179a173706bSAndrew Thompson 	U3G_DEV(NOVATEL, U727, 0),
180a173706bSAndrew Thompson 	U3G_DEV(NOVATEL, U740, 0),
181a173706bSAndrew Thompson 	U3G_DEV(NOVATEL, U740_2, 0),
182a173706bSAndrew Thompson 	U3G_DEV(NOVATEL, U870, 0),
183a173706bSAndrew Thompson 	U3G_DEV(NOVATEL, V620, 0),
184a173706bSAndrew Thompson 	U3G_DEV(NOVATEL, V640, 0),
185a173706bSAndrew Thompson 	U3G_DEV(NOVATEL, V720, 0),
186a173706bSAndrew Thompson 	U3G_DEV(NOVATEL, V740, 0),
187a173706bSAndrew Thompson 	U3G_DEV(NOVATEL, X950D, 0),
188a173706bSAndrew Thompson 	U3G_DEV(NOVATEL, XU870, 0),
189a173706bSAndrew Thompson 	U3G_DEV(NOVATEL, ZEROCD, U3GFL_SCSI_EJECT),
190a173706bSAndrew Thompson 	U3G_DEV(DELL, U740, 0),
19102ac6454SAndrew Thompson 	/* OEM: Merlin */
192a173706bSAndrew Thompson 	U3G_DEV(MERLIN, V620, 0),
19302ac6454SAndrew Thompson 	/* OEM: Sierra Wireless: */
194a173706bSAndrew Thompson 	U3G_DEV(SIERRA, AIRCARD580, 0),
195a173706bSAndrew Thompson 	U3G_DEV(SIERRA, AIRCARD595, 0),
196a173706bSAndrew Thompson 	U3G_DEV(SIERRA, AC595U, 0),
197a173706bSAndrew Thompson 	U3G_DEV(SIERRA, AC597E, 0),
198a173706bSAndrew Thompson 	U3G_DEV(SIERRA, C597, 0),
199a173706bSAndrew Thompson 	U3G_DEV(SIERRA, AC880, 0),
200a173706bSAndrew Thompson 	U3G_DEV(SIERRA, AC880E, 0),
201a173706bSAndrew Thompson 	U3G_DEV(SIERRA, AC880U, 0),
202a173706bSAndrew Thompson 	U3G_DEV(SIERRA, AC881, 0),
203a173706bSAndrew Thompson 	U3G_DEV(SIERRA, AC881E, 0),
204a173706bSAndrew Thompson 	U3G_DEV(SIERRA, AC881U, 0),
205426909d9SAndrew Thompson 	U3G_DEV(SIERRA, AC885U, 0),
206a173706bSAndrew Thompson 	U3G_DEV(SIERRA, EM5625, 0),
207a173706bSAndrew Thompson 	U3G_DEV(SIERRA, MC5720, 0),
208a173706bSAndrew Thompson 	U3G_DEV(SIERRA, MC5720_2, 0),
209a173706bSAndrew Thompson 	U3G_DEV(SIERRA, MC5725, 0),
210a173706bSAndrew Thompson 	U3G_DEV(SIERRA, MINI5725, 0),
211a173706bSAndrew Thompson 	U3G_DEV(SIERRA, AIRCARD875, 0),
212a173706bSAndrew Thompson 	U3G_DEV(SIERRA, MC8755, 0),
213a173706bSAndrew Thompson 	U3G_DEV(SIERRA, MC8755_2, 0),
214a173706bSAndrew Thompson 	U3G_DEV(SIERRA, MC8755_3, 0),
215a173706bSAndrew Thompson 	U3G_DEV(SIERRA, MC8765, 0),
216a173706bSAndrew Thompson 	U3G_DEV(SIERRA, AC875U, 0),
217a173706bSAndrew Thompson 	U3G_DEV(SIERRA, MC8775_2, 0),
218a173706bSAndrew Thompson 	U3G_DEV(SIERRA, MC8780, 0),
219a173706bSAndrew Thompson 	U3G_DEV(SIERRA, MC8781, 0),
220a173706bSAndrew Thompson 	U3G_DEV(HP, HS2300, 0),
22102ac6454SAndrew Thompson 	/* Sierra TruInstaller device ID */
222a173706bSAndrew Thompson 	U3G_DEV(SIERRA, TRUINSTALL, U3GFL_SIERRA_INIT),
223750a2682SAndrew Thompson 	/* PRUEBA SILABS */
224a173706bSAndrew Thompson 	U3G_DEV(SILABS, SAEL, U3GFL_SAEL_M460_INIT),
22502ac6454SAndrew Thompson };
22602ac6454SAndrew Thompson 
22702ac6454SAndrew Thompson static void
22802ac6454SAndrew Thompson u3g_sierra_init(struct usb2_device *udev)
22902ac6454SAndrew Thompson {
23002ac6454SAndrew Thompson 	struct usb2_device_request req;
23102ac6454SAndrew Thompson 
23202ac6454SAndrew Thompson 	DPRINTFN(0, "\n");
23302ac6454SAndrew Thompson 
23402ac6454SAndrew Thompson 	req.bmRequestType = UT_VENDOR;
23502ac6454SAndrew Thompson 	req.bRequest = UR_SET_INTERFACE;
23602ac6454SAndrew Thompson 	USETW(req.wValue, UF_DEVICE_REMOTE_WAKEUP);
23702ac6454SAndrew Thompson 	USETW(req.wIndex, UHF_PORT_CONNECTION);
23802ac6454SAndrew Thompson 	USETW(req.wLength, 0);
23902ac6454SAndrew Thompson 
24002ac6454SAndrew Thompson 	if (usb2_do_request_flags(udev, NULL, &req,
24102ac6454SAndrew Thompson 	    NULL, 0, NULL, USB_MS_HZ)) {
24202ac6454SAndrew Thompson 		/* ignore any errors */
24302ac6454SAndrew Thompson 	}
24402ac6454SAndrew Thompson 	return;
24502ac6454SAndrew Thompson }
24602ac6454SAndrew Thompson 
24702ac6454SAndrew Thompson static void
24802ac6454SAndrew Thompson u3g_huawei_init(struct usb2_device *udev)
24902ac6454SAndrew Thompson {
25002ac6454SAndrew Thompson 	struct usb2_device_request req;
25102ac6454SAndrew Thompson 
25202ac6454SAndrew Thompson 	DPRINTFN(0, "\n");
25302ac6454SAndrew Thompson 
25402ac6454SAndrew Thompson 	req.bmRequestType = UT_WRITE_DEVICE;
25502ac6454SAndrew Thompson 	req.bRequest = UR_SET_FEATURE;
25602ac6454SAndrew Thompson 	USETW(req.wValue, UF_DEVICE_REMOTE_WAKEUP);
25702ac6454SAndrew Thompson 	USETW(req.wIndex, UHF_PORT_SUSPEND);
25802ac6454SAndrew Thompson 	USETW(req.wLength, 0);
25902ac6454SAndrew Thompson 
26002ac6454SAndrew Thompson 	if (usb2_do_request_flags(udev, NULL, &req,
26102ac6454SAndrew Thompson 	    NULL, 0, NULL, USB_MS_HZ)) {
26202ac6454SAndrew Thompson 		/* ignore any errors */
26302ac6454SAndrew Thompson 	}
26402ac6454SAndrew Thompson 	return;
26502ac6454SAndrew Thompson }
26602ac6454SAndrew Thompson 
267750a2682SAndrew Thompson static void
268750a2682SAndrew Thompson u3g_sael_m460_init(struct usb2_device *udev)
269750a2682SAndrew Thompson {
270750a2682SAndrew Thompson 	static const uint8_t setup[][24] = {
271750a2682SAndrew Thompson 	     { 0x41, 0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
272750a2682SAndrew Thompson 	     { 0x41, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00 },
273750a2682SAndrew Thompson 	     { 0x41, 0x13, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00,
274750a2682SAndrew Thompson 	       0x01, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00,
275750a2682SAndrew Thompson 	       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
276750a2682SAndrew Thompson 	     { 0xc1, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x40, 0x02 },
277750a2682SAndrew Thompson 	     { 0xc1, 0x08, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00 },
278750a2682SAndrew Thompson 	     { 0x41, 0x07, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00 },
279750a2682SAndrew Thompson 	     { 0xc1, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02 },
280750a2682SAndrew Thompson 	     { 0x41, 0x01, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00 },
281750a2682SAndrew Thompson 	     { 0x41, 0x07, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00 },
282750a2682SAndrew Thompson 	     { 0x41, 0x03, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00 },
283750a2682SAndrew Thompson 	     { 0x41, 0x19, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00,
284750a2682SAndrew Thompson 	       0x00, 0x00, 0x00, 0x00, 0x11, 0x13 },
285750a2682SAndrew Thompson 	     { 0x41, 0x13, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00,
286750a2682SAndrew Thompson 	       0x09, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00,
287750a2682SAndrew Thompson 	       0x0a, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00 },
288750a2682SAndrew Thompson 	     { 0x41, 0x12, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00 },
289750a2682SAndrew Thompson 	     { 0x41, 0x01, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00 },
290750a2682SAndrew Thompson 	     { 0x41, 0x07, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00 },
291750a2682SAndrew Thompson 	     { 0x41, 0x03, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00 },
292750a2682SAndrew Thompson 	     { 0x41, 0x19, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00,
293750a2682SAndrew Thompson 	       0x00, 0x00, 0x00, 0x00, 0x11, 0x13 },
294750a2682SAndrew Thompson 	     { 0x41, 0x13, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00,
295750a2682SAndrew Thompson 	       0x09, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00,
296750a2682SAndrew Thompson 	       0x0a, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00 },
297750a2682SAndrew Thompson 	     { 0x41, 0x07, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00 },
298750a2682SAndrew Thompson 	};
299750a2682SAndrew Thompson 
300750a2682SAndrew Thompson 	struct usb2_device_request req;
3014309a3fbSAndrew Thompson 	usb2_error_t err;
302750a2682SAndrew Thompson 	uint16_t len;
303750a2682SAndrew Thompson 	uint8_t buf[0x300];
304750a2682SAndrew Thompson 	uint8_t n;
305750a2682SAndrew Thompson 
306750a2682SAndrew Thompson 	DPRINTFN(1, "\n");
307750a2682SAndrew Thompson 
308750a2682SAndrew Thompson 	if (usb2_req_set_alt_interface_no(udev, NULL, 0, 0)) {
309750a2682SAndrew Thompson 		DPRINTFN(0, "Alt setting 0 failed\n");
310750a2682SAndrew Thompson 		return;
311750a2682SAndrew Thompson 	}
312750a2682SAndrew Thompson 
313750a2682SAndrew Thompson 	for (n = 0; n != (sizeof(setup)/sizeof(setup[0])); n++) {
314750a2682SAndrew Thompson 
315750a2682SAndrew Thompson 		memcpy(&req, setup[n], sizeof(req));
316750a2682SAndrew Thompson 
317750a2682SAndrew Thompson 		len = UGETW(req.wLength);
318750a2682SAndrew Thompson 		if (req.bmRequestType & UE_DIR_IN) {
319750a2682SAndrew Thompson 			if (len > sizeof(buf)) {
320750a2682SAndrew Thompson 				DPRINTFN(0, "too small buffer\n");
321750a2682SAndrew Thompson 				continue;
322750a2682SAndrew Thompson 			}
3234309a3fbSAndrew Thompson 			err = usb2_do_request(udev, NULL, &req, buf);
324750a2682SAndrew Thompson 		} else {
325750a2682SAndrew Thompson 			if (len > (sizeof(setup[0]) - 8)) {
326750a2682SAndrew Thompson 				DPRINTFN(0, "too small buffer\n");
327750a2682SAndrew Thompson 				continue;
328750a2682SAndrew Thompson 			}
3294309a3fbSAndrew Thompson 			err = usb2_do_request(udev, NULL, &req,
3304309a3fbSAndrew Thompson 			    __DECONST(uint8_t *, &setup[n][8]));
3314309a3fbSAndrew Thompson 		}
3324309a3fbSAndrew Thompson 		if (err) {
3334309a3fbSAndrew Thompson 			DPRINTFN(1, "request %u failed\n",
334750a2682SAndrew Thompson 			    (unsigned int)n);
3354309a3fbSAndrew Thompson 			/*
3364309a3fbSAndrew Thompson 			 * Some of the requests will fail. Stop doing
3374309a3fbSAndrew Thompson 			 * requests when we are getting timeouts so
3384309a3fbSAndrew Thompson 			 * that we don't block the explore/attach
3394309a3fbSAndrew Thompson 			 * thread forever.
3404309a3fbSAndrew Thompson 			 */
3414309a3fbSAndrew Thompson 			if (err == USB_ERR_TIMEOUT)
342750a2682SAndrew Thompson 				break;
343750a2682SAndrew Thompson 		}
344750a2682SAndrew Thompson 	}
345750a2682SAndrew Thompson }
346750a2682SAndrew Thompson 
34702ac6454SAndrew Thompson static int
34802ac6454SAndrew Thompson u3g_lookup_huawei(struct usb2_attach_arg *uaa)
34902ac6454SAndrew Thompson {
35002ac6454SAndrew Thompson 	/* Calling the lookup function will also set the driver info! */
35102ac6454SAndrew Thompson 	return (usb2_lookup_id_by_uaa(u3g_devs, sizeof(u3g_devs), uaa));
35202ac6454SAndrew Thompson }
35302ac6454SAndrew Thompson 
35402ac6454SAndrew Thompson /*
35502ac6454SAndrew Thompson  * The following function handles 3G modem devices (E220, Mobile,
35602ac6454SAndrew Thompson  * etc.) with auto-install flash disks for Windows/MacOSX on the first
35702ac6454SAndrew Thompson  * interface.  After some command or some delay they change appearance
35802ac6454SAndrew Thompson  * to a modem.
35902ac6454SAndrew Thompson  */
36002ac6454SAndrew Thompson static usb2_error_t
36102ac6454SAndrew Thompson u3g_test_huawei_autoinst(struct usb2_device *udev,
36202ac6454SAndrew Thompson     struct usb2_attach_arg *uaa)
36302ac6454SAndrew Thompson {
36402ac6454SAndrew Thompson 	struct usb2_interface *iface;
36502ac6454SAndrew Thompson 	struct usb2_interface_descriptor *id;
36602ac6454SAndrew Thompson 	uint32_t flags;
36702ac6454SAndrew Thompson 
36802ac6454SAndrew Thompson 	if (udev == NULL) {
36902ac6454SAndrew Thompson 		return (USB_ERR_INVAL);
37002ac6454SAndrew Thompson 	}
37102ac6454SAndrew Thompson 	iface = usb2_get_iface(udev, 0);
37202ac6454SAndrew Thompson 	if (iface == NULL) {
37302ac6454SAndrew Thompson 		return (USB_ERR_INVAL);
37402ac6454SAndrew Thompson 	}
37502ac6454SAndrew Thompson 	id = iface->idesc;
37602ac6454SAndrew Thompson 	if (id == NULL) {
37702ac6454SAndrew Thompson 		return (USB_ERR_INVAL);
37802ac6454SAndrew Thompson 	}
37902ac6454SAndrew Thompson 	if (id->bInterfaceClass != UICLASS_MASS) {
38002ac6454SAndrew Thompson 		return (USB_ERR_INVAL);
38102ac6454SAndrew Thompson 	}
38202ac6454SAndrew Thompson 	if (u3g_lookup_huawei(uaa)) {
38302ac6454SAndrew Thompson 		/* no device match */
38402ac6454SAndrew Thompson 		return (USB_ERR_INVAL);
38502ac6454SAndrew Thompson 	}
38602ac6454SAndrew Thompson 	flags = USB_GET_DRIVER_INFO(uaa);
38702ac6454SAndrew Thompson 
38802ac6454SAndrew Thompson 	if (flags & U3GFL_HUAWEI_INIT) {
38902ac6454SAndrew Thompson 		u3g_huawei_init(udev);
39002ac6454SAndrew Thompson 	} else if (flags & U3GFL_SCSI_EJECT) {
39102ac6454SAndrew Thompson 		return (usb2_test_autoinstall(udev, 0, 1));
39202ac6454SAndrew Thompson 	} else if (flags & U3GFL_SIERRA_INIT) {
39302ac6454SAndrew Thompson 		u3g_sierra_init(udev);
39402ac6454SAndrew Thompson 	} else {
39502ac6454SAndrew Thompson 		/* no quirks */
39602ac6454SAndrew Thompson 		return (USB_ERR_INVAL);
39702ac6454SAndrew Thompson 	}
39802ac6454SAndrew Thompson 	return (0);			/* success */
39902ac6454SAndrew Thompson }
40002ac6454SAndrew Thompson 
40102ac6454SAndrew Thompson static int
40202ac6454SAndrew Thompson u3g_driver_loaded(struct module *mod, int what, void *arg)
40302ac6454SAndrew Thompson {
40402ac6454SAndrew Thompson 	switch (what) {
40502ac6454SAndrew Thompson 	case MOD_LOAD:
40602ac6454SAndrew Thompson 		/* register our autoinstall handler */
40702ac6454SAndrew Thompson 		usb2_test_huawei_autoinst_p = &u3g_test_huawei_autoinst;
40802ac6454SAndrew Thompson 		break;
40902ac6454SAndrew Thompson 	case MOD_UNLOAD:
41002ac6454SAndrew Thompson 		usb2_test_huawei_unload(NULL);
41102ac6454SAndrew Thompson 		break;
41202ac6454SAndrew Thompson 	default:
41302ac6454SAndrew Thompson 		return (EOPNOTSUPP);
41402ac6454SAndrew Thompson 	}
41502ac6454SAndrew Thompson  	return (0);
41602ac6454SAndrew Thompson }
41702ac6454SAndrew Thompson 
41802ac6454SAndrew Thompson static int
41902ac6454SAndrew Thompson u3g_probe(device_t self)
42002ac6454SAndrew Thompson {
42102ac6454SAndrew Thompson 	struct usb2_attach_arg *uaa = device_get_ivars(self);
42202ac6454SAndrew Thompson 
423f29a0724SAndrew Thompson 	if (uaa->usb_mode != USB_MODE_HOST) {
42402ac6454SAndrew Thompson 		return (ENXIO);
42502ac6454SAndrew Thompson 	}
42602ac6454SAndrew Thompson 	if (uaa->info.bConfigIndex != U3G_CONFIG_INDEX) {
42702ac6454SAndrew Thompson 		return (ENXIO);
42802ac6454SAndrew Thompson 	}
42902ac6454SAndrew Thompson 	if (uaa->info.bInterfaceClass != UICLASS_VENDOR) {
43002ac6454SAndrew Thompson 		return (ENXIO);
43102ac6454SAndrew Thompson 	}
43202ac6454SAndrew Thompson 	return (u3g_lookup_huawei(uaa));
43302ac6454SAndrew Thompson }
43402ac6454SAndrew Thompson 
43502ac6454SAndrew Thompson static int
43602ac6454SAndrew Thompson u3g_attach(device_t dev)
43702ac6454SAndrew Thompson {
43802ac6454SAndrew Thompson 	struct usb2_config u3g_config_tmp[U3G_N_TRANSFER];
43902ac6454SAndrew Thompson 	struct usb2_attach_arg *uaa = device_get_ivars(dev);
44002ac6454SAndrew Thompson 	struct u3g_softc *sc = device_get_softc(dev);
44102ac6454SAndrew Thompson 	struct usb2_interface *iface;
44202ac6454SAndrew Thompson 	struct usb2_interface_descriptor *id;
443822e5d76SAndrew Thompson 	uint32_t iface_valid;
444822e5d76SAndrew Thompson 	int error, flags, nports;
445e92a4515SAndrew Thompson 	int ep, n;
44602ac6454SAndrew Thompson 	uint8_t i;
44702ac6454SAndrew Thompson 
44802ac6454SAndrew Thompson 	DPRINTF("sc=%p\n", sc);
44902ac6454SAndrew Thompson 
450750a2682SAndrew Thompson 	flags = USB_GET_DRIVER_INFO(uaa);
451750a2682SAndrew Thompson 
452750a2682SAndrew Thompson 	if (flags & U3GFL_SAEL_M460_INIT)
453750a2682SAndrew Thompson 		u3g_sael_m460_init(uaa->device);
454750a2682SAndrew Thompson 
45502ac6454SAndrew Thompson 	/* copy in USB config */
45602ac6454SAndrew Thompson 	for (n = 0; n != U3G_N_TRANSFER; n++)
45702ac6454SAndrew Thompson 		u3g_config_tmp[n] = u3g_config[n];
45802ac6454SAndrew Thompson 
45902ac6454SAndrew Thompson 	device_set_usb2_desc(dev);
460deefe583SAndrew Thompson 	mtx_init(&sc->sc_mtx, "u3g", NULL, MTX_DEF);
46102ac6454SAndrew Thompson 
46202ac6454SAndrew Thompson 	sc->sc_udev = uaa->device;
46302ac6454SAndrew Thompson 
464e92a4515SAndrew Thompson 	/* Claim all interfaces on the device */
465e92a4515SAndrew Thompson 	iface_valid = 0;
466e92a4515SAndrew Thompson 	for (i = uaa->info.bIfaceIndex; i < USB_IFACE_MAX; i++) {
467e92a4515SAndrew Thompson 		iface = usb2_get_iface(uaa->device, i);
468e92a4515SAndrew Thompson 		if (iface == NULL)
469e92a4515SAndrew Thompson 			break;
470e92a4515SAndrew Thompson 		id = usb2_get_interface_descriptor(iface);
471e92a4515SAndrew Thompson 		if (id == NULL || id->bInterfaceClass != UICLASS_VENDOR)
472e92a4515SAndrew Thompson 			continue;
473e92a4515SAndrew Thompson 		usb2_set_parent_iface(uaa->device, i, uaa->info.bIfaceIndex);
474e92a4515SAndrew Thompson 		iface_valid |= (1<<i);
475e92a4515SAndrew Thompson 	}
47602ac6454SAndrew Thompson 
477e92a4515SAndrew Thompson 	i = 0;		/* interface index */
478e92a4515SAndrew Thompson 	ep = 0;		/* endpoint index */
479e92a4515SAndrew Thompson 	nports = 0;	/* number of ports */
480e92a4515SAndrew Thompson 	while (i < USB_IFACE_MAX) {
481e92a4515SAndrew Thompson 		if ((iface_valid & (1<<i)) == 0) {
482e92a4515SAndrew Thompson 			i++;
483e92a4515SAndrew Thompson 			continue;
484e92a4515SAndrew Thompson 		}
48502ac6454SAndrew Thompson 
48602ac6454SAndrew Thompson 		/* update BULK endpoint index */
487e92a4515SAndrew Thompson 		for (n = 0; n < U3G_N_TRANSFER; n++)
488e92a4515SAndrew Thompson 			u3g_config_tmp[n].ep_index = ep;
48902ac6454SAndrew Thompson 
49002ac6454SAndrew Thompson 		/* try to allocate a set of BULK endpoints */
491e92a4515SAndrew Thompson 		error = usb2_transfer_setup(uaa->device, &i,
492e92a4515SAndrew Thompson 		    sc->sc_xfer[nports], u3g_config_tmp, U3G_N_TRANSFER,
493e92a4515SAndrew Thompson 		    &sc->sc_ucom[nports], &sc->sc_mtx);
49402ac6454SAndrew Thompson 		if (error) {
49502ac6454SAndrew Thompson 			/* next interface */
496e92a4515SAndrew Thompson 			i++;
497e92a4515SAndrew Thompson 			ep = 0;
49802ac6454SAndrew Thompson 			continue;
49902ac6454SAndrew Thompson 		}
50002ac6454SAndrew Thompson 
50102ac6454SAndrew Thompson 		/* set stall by default */
502deefe583SAndrew Thompson 		mtx_lock(&sc->sc_mtx);
503e92a4515SAndrew Thompson 		usb2_transfer_set_stall(sc->sc_xfer[nports][U3G_BULK_WR]);
504e92a4515SAndrew Thompson 		usb2_transfer_set_stall(sc->sc_xfer[nports][U3G_BULK_RD]);
505deefe583SAndrew Thompson 		mtx_unlock(&sc->sc_mtx);
50602ac6454SAndrew Thompson 
507e92a4515SAndrew Thompson 		nports++;	/* found one port */
508e92a4515SAndrew Thompson 		ep++;
509e92a4515SAndrew Thompson 		if (nports == U3G_MAXPORTS)
510e92a4515SAndrew Thompson 			break;
51102ac6454SAndrew Thompson 	}
512e92a4515SAndrew Thompson 	if (nports == 0) {
513e92a4515SAndrew Thompson 		device_printf(dev, "no ports found\n");
514e92a4515SAndrew Thompson 		goto detach;
515e92a4515SAndrew Thompson 	}
516e92a4515SAndrew Thompson 	sc->sc_numports = nports;
51702ac6454SAndrew Thompson 
51802ac6454SAndrew Thompson 	error = usb2_com_attach(&sc->sc_super_ucom, sc->sc_ucom,
519deefe583SAndrew Thompson 	    sc->sc_numports, sc, &u3g_callback, &sc->sc_mtx);
52002ac6454SAndrew Thompson 	if (error) {
52102ac6454SAndrew Thompson 		DPRINTF("usb2_com_attach failed\n");
52202ac6454SAndrew Thompson 		goto detach;
52302ac6454SAndrew Thompson 	}
524e92a4515SAndrew Thompson 	if (sc->sc_numports > 1)
525e92a4515SAndrew Thompson 		device_printf(dev, "Found %u ports.\n", sc->sc_numports);
52602ac6454SAndrew Thompson 	return (0);
52702ac6454SAndrew Thompson 
52802ac6454SAndrew Thompson detach:
52902ac6454SAndrew Thompson 	u3g_detach(dev);
53002ac6454SAndrew Thompson 	return (ENXIO);
53102ac6454SAndrew Thompson }
53202ac6454SAndrew Thompson 
53302ac6454SAndrew Thompson static int
53402ac6454SAndrew Thompson u3g_detach(device_t dev)
53502ac6454SAndrew Thompson {
53602ac6454SAndrew Thompson 	struct u3g_softc *sc = device_get_softc(dev);
53702ac6454SAndrew Thompson 	uint8_t m;
53802ac6454SAndrew Thompson 
53902ac6454SAndrew Thompson 	DPRINTF("sc=%p\n", sc);
54002ac6454SAndrew Thompson 
54102ac6454SAndrew Thompson 	/* NOTE: It is not dangerous to detach more ports than attached! */
54202ac6454SAndrew Thompson 	usb2_com_detach(&sc->sc_super_ucom, sc->sc_ucom, U3G_MAXPORTS);
54302ac6454SAndrew Thompson 
54402ac6454SAndrew Thompson 	for (m = 0; m != U3G_MAXPORTS; m++)
54502ac6454SAndrew Thompson 		usb2_transfer_unsetup(sc->sc_xfer[m], U3G_N_TRANSFER);
546deefe583SAndrew Thompson 	mtx_destroy(&sc->sc_mtx);
54702ac6454SAndrew Thompson 
54802ac6454SAndrew Thompson 	return (0);
54902ac6454SAndrew Thompson }
55002ac6454SAndrew Thompson 
55102ac6454SAndrew Thompson static void
55202ac6454SAndrew Thompson u3g_start_read(struct usb2_com_softc *ucom)
55302ac6454SAndrew Thompson {
55402ac6454SAndrew Thompson 	struct u3g_softc *sc = ucom->sc_parent;
55502ac6454SAndrew Thompson 
55602ac6454SAndrew Thompson 	/* start read endpoint */
55702ac6454SAndrew Thompson 	usb2_transfer_start(sc->sc_xfer[ucom->sc_local_unit][U3G_BULK_RD]);
55802ac6454SAndrew Thompson 	return;
55902ac6454SAndrew Thompson }
56002ac6454SAndrew Thompson 
56102ac6454SAndrew Thompson static void
56202ac6454SAndrew Thompson u3g_stop_read(struct usb2_com_softc *ucom)
56302ac6454SAndrew Thompson {
56402ac6454SAndrew Thompson 	struct u3g_softc *sc = ucom->sc_parent;
56502ac6454SAndrew Thompson 
56602ac6454SAndrew Thompson 	/* stop read endpoint */
56702ac6454SAndrew Thompson 	usb2_transfer_stop(sc->sc_xfer[ucom->sc_local_unit][U3G_BULK_RD]);
56802ac6454SAndrew Thompson 	return;
56902ac6454SAndrew Thompson }
57002ac6454SAndrew Thompson 
57102ac6454SAndrew Thompson static void
57202ac6454SAndrew Thompson u3g_start_write(struct usb2_com_softc *ucom)
57302ac6454SAndrew Thompson {
57402ac6454SAndrew Thompson 	struct u3g_softc *sc = ucom->sc_parent;
57502ac6454SAndrew Thompson 
57602ac6454SAndrew Thompson 	usb2_transfer_start(sc->sc_xfer[ucom->sc_local_unit][U3G_BULK_WR]);
57702ac6454SAndrew Thompson 	return;
57802ac6454SAndrew Thompson }
57902ac6454SAndrew Thompson 
58002ac6454SAndrew Thompson static void
58102ac6454SAndrew Thompson u3g_stop_write(struct usb2_com_softc *ucom)
58202ac6454SAndrew Thompson {
58302ac6454SAndrew Thompson 	struct u3g_softc *sc = ucom->sc_parent;
58402ac6454SAndrew Thompson 
58502ac6454SAndrew Thompson 	usb2_transfer_stop(sc->sc_xfer[ucom->sc_local_unit][U3G_BULK_WR]);
58602ac6454SAndrew Thompson 	return;
58702ac6454SAndrew Thompson }
58802ac6454SAndrew Thompson 
58902ac6454SAndrew Thompson static void
59002ac6454SAndrew Thompson u3g_write_callback(struct usb2_xfer *xfer)
59102ac6454SAndrew Thompson {
59202ac6454SAndrew Thompson 	struct usb2_com_softc *ucom = xfer->priv_sc;
59302ac6454SAndrew Thompson 	uint32_t actlen;
59402ac6454SAndrew Thompson 
59502ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
59602ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
59702ac6454SAndrew Thompson 	case USB_ST_SETUP:
59802ac6454SAndrew Thompson tr_setup:
59902ac6454SAndrew Thompson 		if (usb2_com_get_data(ucom, xfer->frbuffers, 0,
60002ac6454SAndrew Thompson 		    U3G_BSIZE, &actlen)) {
60102ac6454SAndrew Thompson 			xfer->frlengths[0] = actlen;
60202ac6454SAndrew Thompson 			usb2_start_hardware(xfer);
60302ac6454SAndrew Thompson 		}
60402ac6454SAndrew Thompson 		break;
60502ac6454SAndrew Thompson 
60602ac6454SAndrew Thompson 	default:			/* Error */
60702ac6454SAndrew Thompson 		if (xfer->error != USB_ERR_CANCELLED) {
60802ac6454SAndrew Thompson 			/* do a builtin clear-stall */
60902ac6454SAndrew Thompson 			xfer->flags.stall_pipe = 1;
61002ac6454SAndrew Thompson 			goto tr_setup;
61102ac6454SAndrew Thompson 		}
61202ac6454SAndrew Thompson 		break;
61302ac6454SAndrew Thompson 	}
61402ac6454SAndrew Thompson 	return;
61502ac6454SAndrew Thompson }
61602ac6454SAndrew Thompson 
61702ac6454SAndrew Thompson static void
61802ac6454SAndrew Thompson u3g_read_callback(struct usb2_xfer *xfer)
61902ac6454SAndrew Thompson {
62002ac6454SAndrew Thompson 	struct usb2_com_softc *ucom = xfer->priv_sc;
62102ac6454SAndrew Thompson 
62202ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
62302ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
62402ac6454SAndrew Thompson 		usb2_com_put_data(ucom, xfer->frbuffers, 0, xfer->actlen);
62502ac6454SAndrew Thompson 
62602ac6454SAndrew Thompson 	case USB_ST_SETUP:
62702ac6454SAndrew Thompson tr_setup:
62802ac6454SAndrew Thompson 		xfer->frlengths[0] = xfer->max_data_length;
62902ac6454SAndrew Thompson 		usb2_start_hardware(xfer);
63002ac6454SAndrew Thompson 		break;
63102ac6454SAndrew Thompson 
63202ac6454SAndrew Thompson 	default:			/* Error */
63302ac6454SAndrew Thompson 		if (xfer->error != USB_ERR_CANCELLED) {
63402ac6454SAndrew Thompson 			/* do a builtin clear-stall */
63502ac6454SAndrew Thompson 			xfer->flags.stall_pipe = 1;
63602ac6454SAndrew Thompson 			goto tr_setup;
63702ac6454SAndrew Thompson 		}
63802ac6454SAndrew Thompson 		break;
63902ac6454SAndrew Thompson 	}
64002ac6454SAndrew Thompson 	return;
64102ac6454SAndrew Thompson }
642