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 569360ae40SAndrew Thompson SYSCTL_NODE(_hw_usb, OID_AUTO, u3g, CTLFLAG_RW, 0, "USB 3g"); 579360ae40SAndrew Thompson SYSCTL_INT(_hw_usb_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 { 86760bc48eSAndrew Thompson struct ucom_super_softc sc_super_ucom; 87760bc48eSAndrew Thompson struct ucom_softc sc_ucom[U3G_MAXPORTS]; 8802ac6454SAndrew Thompson 89760bc48eSAndrew Thompson struct usb_xfer *sc_xfer[U3G_MAXPORTS][U3G_N_TRANSFER]; 90760bc48eSAndrew Thompson struct usb_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 102e0a69b51SAndrew Thompson static usb_callback_t u3g_write_callback; 103e0a69b51SAndrew Thompson static usb_callback_t u3g_read_callback; 10402ac6454SAndrew Thompson 105760bc48eSAndrew Thompson static void u3g_start_read(struct ucom_softc *ucom); 106760bc48eSAndrew Thompson static void u3g_stop_read(struct ucom_softc *ucom); 107760bc48eSAndrew Thompson static void u3g_start_write(struct ucom_softc *ucom); 108760bc48eSAndrew Thompson static void u3g_stop_write(struct ucom_softc *ucom); 10902ac6454SAndrew Thompson 11002ac6454SAndrew Thompson static int u3g_driver_loaded(struct module *mod, int what, void *arg); 11102ac6454SAndrew Thompson 112760bc48eSAndrew Thompson static const struct usb_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 133760bc48eSAndrew Thompson static const struct ucom_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 159760bc48eSAndrew Thompson static const struct usb_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), 166df05202dSAndrew Thompson U3G_DEV(OPTION, GTHSDPA, 0), 167a173706bSAndrew Thompson U3G_DEV(OPTION, GTMAXHSUPA, 0), 168a173706bSAndrew Thompson U3G_DEV(OPTION, VODAFONEMC3G, 0), 16902ac6454SAndrew Thompson /* OEM: Qualcomm, Inc. */ 170a173706bSAndrew Thompson U3G_DEV(QUALCOMMINC, ZTE_STOR, U3GFL_SCSI_EJECT), 171a173706bSAndrew Thompson U3G_DEV(QUALCOMMINC, CDMA_MSM, U3GFL_SCSI_EJECT), 17202ac6454SAndrew Thompson /* OEM: Huawei */ 173a173706bSAndrew Thompson U3G_DEV(HUAWEI, MOBILE, U3GFL_HUAWEI_INIT), 174a173706bSAndrew Thompson U3G_DEV(HUAWEI, E220, U3GFL_HUAWEI_INIT), 17502ac6454SAndrew Thompson /* OEM: Novatel */ 176a173706bSAndrew Thompson U3G_DEV(NOVATEL, CDMA_MODEM, 0), 177a173706bSAndrew Thompson U3G_DEV(NOVATEL, ES620, 0), 178a173706bSAndrew Thompson U3G_DEV(NOVATEL, MC950D, 0), 179a173706bSAndrew Thompson U3G_DEV(NOVATEL, U720, 0), 180a173706bSAndrew Thompson U3G_DEV(NOVATEL, U727, 0), 181a173706bSAndrew Thompson U3G_DEV(NOVATEL, U740, 0), 182a173706bSAndrew Thompson U3G_DEV(NOVATEL, U740_2, 0), 183a173706bSAndrew Thompson U3G_DEV(NOVATEL, U870, 0), 184a173706bSAndrew Thompson U3G_DEV(NOVATEL, V620, 0), 185a173706bSAndrew Thompson U3G_DEV(NOVATEL, V640, 0), 186a173706bSAndrew Thompson U3G_DEV(NOVATEL, V720, 0), 187a173706bSAndrew Thompson U3G_DEV(NOVATEL, V740, 0), 188a173706bSAndrew Thompson U3G_DEV(NOVATEL, X950D, 0), 189a173706bSAndrew Thompson U3G_DEV(NOVATEL, XU870, 0), 190a173706bSAndrew Thompson U3G_DEV(NOVATEL, ZEROCD, U3GFL_SCSI_EJECT), 191a173706bSAndrew Thompson U3G_DEV(DELL, U740, 0), 19202ac6454SAndrew Thompson /* OEM: Merlin */ 193a173706bSAndrew Thompson U3G_DEV(MERLIN, V620, 0), 19402ac6454SAndrew Thompson /* OEM: Sierra Wireless: */ 195a173706bSAndrew Thompson U3G_DEV(SIERRA, AIRCARD580, 0), 196a173706bSAndrew Thompson U3G_DEV(SIERRA, AIRCARD595, 0), 197a173706bSAndrew Thompson U3G_DEV(SIERRA, AC595U, 0), 198a173706bSAndrew Thompson U3G_DEV(SIERRA, AC597E, 0), 199a173706bSAndrew Thompson U3G_DEV(SIERRA, C597, 0), 200a173706bSAndrew Thompson U3G_DEV(SIERRA, AC880, 0), 201a173706bSAndrew Thompson U3G_DEV(SIERRA, AC880E, 0), 202a173706bSAndrew Thompson U3G_DEV(SIERRA, AC880U, 0), 203a173706bSAndrew Thompson U3G_DEV(SIERRA, AC881, 0), 204a173706bSAndrew Thompson U3G_DEV(SIERRA, AC881E, 0), 205a173706bSAndrew Thompson U3G_DEV(SIERRA, AC881U, 0), 206426909d9SAndrew Thompson U3G_DEV(SIERRA, AC885U, 0), 207a173706bSAndrew Thompson U3G_DEV(SIERRA, EM5625, 0), 208a173706bSAndrew Thompson U3G_DEV(SIERRA, MC5720, 0), 209a173706bSAndrew Thompson U3G_DEV(SIERRA, MC5720_2, 0), 210a173706bSAndrew Thompson U3G_DEV(SIERRA, MC5725, 0), 211a173706bSAndrew Thompson U3G_DEV(SIERRA, MINI5725, 0), 212a173706bSAndrew Thompson U3G_DEV(SIERRA, AIRCARD875, 0), 213a173706bSAndrew Thompson U3G_DEV(SIERRA, MC8755, 0), 214a173706bSAndrew Thompson U3G_DEV(SIERRA, MC8755_2, 0), 215a173706bSAndrew Thompson U3G_DEV(SIERRA, MC8755_3, 0), 216a173706bSAndrew Thompson U3G_DEV(SIERRA, MC8765, 0), 217a173706bSAndrew Thompson U3G_DEV(SIERRA, AC875U, 0), 218a173706bSAndrew Thompson U3G_DEV(SIERRA, MC8775_2, 0), 219a173706bSAndrew Thompson U3G_DEV(SIERRA, MC8780, 0), 220a173706bSAndrew Thompson U3G_DEV(SIERRA, MC8781, 0), 221a173706bSAndrew Thompson U3G_DEV(HP, HS2300, 0), 22202ac6454SAndrew Thompson /* Sierra TruInstaller device ID */ 223a173706bSAndrew Thompson U3G_DEV(SIERRA, TRUINSTALL, U3GFL_SIERRA_INIT), 224750a2682SAndrew Thompson /* PRUEBA SILABS */ 225a173706bSAndrew Thompson U3G_DEV(SILABS, SAEL, U3GFL_SAEL_M460_INIT), 22602ac6454SAndrew Thompson }; 22702ac6454SAndrew Thompson 22802ac6454SAndrew Thompson static void 229760bc48eSAndrew Thompson u3g_sierra_init(struct usb_device *udev) 23002ac6454SAndrew Thompson { 231760bc48eSAndrew Thompson struct usb_device_request req; 23202ac6454SAndrew Thompson 23302ac6454SAndrew Thompson DPRINTFN(0, "\n"); 23402ac6454SAndrew Thompson 23502ac6454SAndrew Thompson req.bmRequestType = UT_VENDOR; 23602ac6454SAndrew Thompson req.bRequest = UR_SET_INTERFACE; 23702ac6454SAndrew Thompson USETW(req.wValue, UF_DEVICE_REMOTE_WAKEUP); 23802ac6454SAndrew Thompson USETW(req.wIndex, UHF_PORT_CONNECTION); 23902ac6454SAndrew Thompson USETW(req.wLength, 0); 24002ac6454SAndrew Thompson 24102ac6454SAndrew Thompson if (usb2_do_request_flags(udev, NULL, &req, 24202ac6454SAndrew Thompson NULL, 0, NULL, USB_MS_HZ)) { 24302ac6454SAndrew Thompson /* ignore any errors */ 24402ac6454SAndrew Thompson } 24502ac6454SAndrew Thompson return; 24602ac6454SAndrew Thompson } 24702ac6454SAndrew Thompson 24802ac6454SAndrew Thompson static void 249760bc48eSAndrew Thompson u3g_huawei_init(struct usb_device *udev) 25002ac6454SAndrew Thompson { 251760bc48eSAndrew Thompson struct usb_device_request req; 25202ac6454SAndrew Thompson 25302ac6454SAndrew Thompson DPRINTFN(0, "\n"); 25402ac6454SAndrew Thompson 25502ac6454SAndrew Thompson req.bmRequestType = UT_WRITE_DEVICE; 25602ac6454SAndrew Thompson req.bRequest = UR_SET_FEATURE; 25702ac6454SAndrew Thompson USETW(req.wValue, UF_DEVICE_REMOTE_WAKEUP); 25802ac6454SAndrew Thompson USETW(req.wIndex, UHF_PORT_SUSPEND); 25902ac6454SAndrew Thompson USETW(req.wLength, 0); 26002ac6454SAndrew Thompson 26102ac6454SAndrew Thompson if (usb2_do_request_flags(udev, NULL, &req, 26202ac6454SAndrew Thompson NULL, 0, NULL, USB_MS_HZ)) { 26302ac6454SAndrew Thompson /* ignore any errors */ 26402ac6454SAndrew Thompson } 26502ac6454SAndrew Thompson return; 26602ac6454SAndrew Thompson } 26702ac6454SAndrew Thompson 268750a2682SAndrew Thompson static void 269760bc48eSAndrew Thompson u3g_sael_m460_init(struct usb_device *udev) 270750a2682SAndrew Thompson { 271750a2682SAndrew Thompson static const uint8_t setup[][24] = { 272750a2682SAndrew Thompson { 0x41, 0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, 273750a2682SAndrew Thompson { 0x41, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00 }, 274750a2682SAndrew Thompson { 0x41, 0x13, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 275750a2682SAndrew Thompson 0x01, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 276750a2682SAndrew Thompson 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, 277750a2682SAndrew Thompson { 0xc1, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x40, 0x02 }, 278750a2682SAndrew Thompson { 0xc1, 0x08, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00 }, 279750a2682SAndrew Thompson { 0x41, 0x07, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00 }, 280750a2682SAndrew Thompson { 0xc1, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02 }, 281750a2682SAndrew Thompson { 0x41, 0x01, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00 }, 282750a2682SAndrew Thompson { 0x41, 0x07, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00 }, 283750a2682SAndrew Thompson { 0x41, 0x03, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00 }, 284750a2682SAndrew Thompson { 0x41, 0x19, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 285750a2682SAndrew Thompson 0x00, 0x00, 0x00, 0x00, 0x11, 0x13 }, 286750a2682SAndrew Thompson { 0x41, 0x13, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 287750a2682SAndrew Thompson 0x09, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 288750a2682SAndrew Thompson 0x0a, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00 }, 289750a2682SAndrew Thompson { 0x41, 0x12, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00 }, 290750a2682SAndrew Thompson { 0x41, 0x01, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00 }, 291750a2682SAndrew Thompson { 0x41, 0x07, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00 }, 292750a2682SAndrew Thompson { 0x41, 0x03, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00 }, 293750a2682SAndrew Thompson { 0x41, 0x19, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 294750a2682SAndrew Thompson 0x00, 0x00, 0x00, 0x00, 0x11, 0x13 }, 295750a2682SAndrew Thompson { 0x41, 0x13, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 296750a2682SAndrew Thompson 0x09, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 297750a2682SAndrew Thompson 0x0a, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00 }, 298750a2682SAndrew Thompson { 0x41, 0x07, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00 }, 299750a2682SAndrew Thompson }; 300750a2682SAndrew Thompson 301760bc48eSAndrew Thompson struct usb_device_request req; 302e0a69b51SAndrew Thompson usb_error_t err; 303750a2682SAndrew Thompson uint16_t len; 304750a2682SAndrew Thompson uint8_t buf[0x300]; 305750a2682SAndrew Thompson uint8_t n; 306750a2682SAndrew Thompson 307750a2682SAndrew Thompson DPRINTFN(1, "\n"); 308750a2682SAndrew Thompson 309750a2682SAndrew Thompson if (usb2_req_set_alt_interface_no(udev, NULL, 0, 0)) { 310750a2682SAndrew Thompson DPRINTFN(0, "Alt setting 0 failed\n"); 311750a2682SAndrew Thompson return; 312750a2682SAndrew Thompson } 313750a2682SAndrew Thompson 314750a2682SAndrew Thompson for (n = 0; n != (sizeof(setup)/sizeof(setup[0])); n++) { 315750a2682SAndrew Thompson 316750a2682SAndrew Thompson memcpy(&req, setup[n], sizeof(req)); 317750a2682SAndrew Thompson 318750a2682SAndrew Thompson len = UGETW(req.wLength); 319750a2682SAndrew Thompson if (req.bmRequestType & UE_DIR_IN) { 320750a2682SAndrew Thompson if (len > sizeof(buf)) { 321750a2682SAndrew Thompson DPRINTFN(0, "too small buffer\n"); 322750a2682SAndrew Thompson continue; 323750a2682SAndrew Thompson } 3244309a3fbSAndrew Thompson err = usb2_do_request(udev, NULL, &req, buf); 325750a2682SAndrew Thompson } else { 326750a2682SAndrew Thompson if (len > (sizeof(setup[0]) - 8)) { 327750a2682SAndrew Thompson DPRINTFN(0, "too small buffer\n"); 328750a2682SAndrew Thompson continue; 329750a2682SAndrew Thompson } 3304309a3fbSAndrew Thompson err = usb2_do_request(udev, NULL, &req, 3314309a3fbSAndrew Thompson __DECONST(uint8_t *, &setup[n][8])); 3324309a3fbSAndrew Thompson } 3334309a3fbSAndrew Thompson if (err) { 3344309a3fbSAndrew Thompson DPRINTFN(1, "request %u failed\n", 335750a2682SAndrew Thompson (unsigned int)n); 3364309a3fbSAndrew Thompson /* 3374309a3fbSAndrew Thompson * Some of the requests will fail. Stop doing 3384309a3fbSAndrew Thompson * requests when we are getting timeouts so 3394309a3fbSAndrew Thompson * that we don't block the explore/attach 3404309a3fbSAndrew Thompson * thread forever. 3414309a3fbSAndrew Thompson */ 3424309a3fbSAndrew Thompson if (err == USB_ERR_TIMEOUT) 343750a2682SAndrew Thompson break; 344750a2682SAndrew Thompson } 345750a2682SAndrew Thompson } 346750a2682SAndrew Thompson } 347750a2682SAndrew Thompson 34802ac6454SAndrew Thompson static int 349760bc48eSAndrew Thompson u3g_lookup_huawei(struct usb_attach_arg *uaa) 35002ac6454SAndrew Thompson { 35102ac6454SAndrew Thompson /* Calling the lookup function will also set the driver info! */ 35202ac6454SAndrew Thompson return (usb2_lookup_id_by_uaa(u3g_devs, sizeof(u3g_devs), uaa)); 35302ac6454SAndrew Thompson } 35402ac6454SAndrew Thompson 35502ac6454SAndrew Thompson /* 35602ac6454SAndrew Thompson * The following function handles 3G modem devices (E220, Mobile, 35702ac6454SAndrew Thompson * etc.) with auto-install flash disks for Windows/MacOSX on the first 35802ac6454SAndrew Thompson * interface. After some command or some delay they change appearance 35902ac6454SAndrew Thompson * to a modem. 36002ac6454SAndrew Thompson */ 361e0a69b51SAndrew Thompson static usb_error_t 362760bc48eSAndrew Thompson u3g_test_huawei_autoinst(struct usb_device *udev, 363760bc48eSAndrew Thompson struct usb_attach_arg *uaa) 36402ac6454SAndrew Thompson { 365760bc48eSAndrew Thompson struct usb_interface *iface; 366760bc48eSAndrew Thompson struct usb_interface_descriptor *id; 36702ac6454SAndrew Thompson uint32_t flags; 36802ac6454SAndrew Thompson 36902ac6454SAndrew Thompson if (udev == NULL) { 37002ac6454SAndrew Thompson return (USB_ERR_INVAL); 37102ac6454SAndrew Thompson } 37202ac6454SAndrew Thompson iface = usb2_get_iface(udev, 0); 37302ac6454SAndrew Thompson if (iface == NULL) { 37402ac6454SAndrew Thompson return (USB_ERR_INVAL); 37502ac6454SAndrew Thompson } 37602ac6454SAndrew Thompson id = iface->idesc; 37702ac6454SAndrew Thompson if (id == NULL) { 37802ac6454SAndrew Thompson return (USB_ERR_INVAL); 37902ac6454SAndrew Thompson } 38002ac6454SAndrew Thompson if (id->bInterfaceClass != UICLASS_MASS) { 38102ac6454SAndrew Thompson return (USB_ERR_INVAL); 38202ac6454SAndrew Thompson } 38302ac6454SAndrew Thompson if (u3g_lookup_huawei(uaa)) { 38402ac6454SAndrew Thompson /* no device match */ 38502ac6454SAndrew Thompson return (USB_ERR_INVAL); 38602ac6454SAndrew Thompson } 38702ac6454SAndrew Thompson flags = USB_GET_DRIVER_INFO(uaa); 38802ac6454SAndrew Thompson 38902ac6454SAndrew Thompson if (flags & U3GFL_HUAWEI_INIT) { 39002ac6454SAndrew Thompson u3g_huawei_init(udev); 39102ac6454SAndrew Thompson } else if (flags & U3GFL_SCSI_EJECT) { 39202ac6454SAndrew Thompson return (usb2_test_autoinstall(udev, 0, 1)); 39302ac6454SAndrew Thompson } else if (flags & U3GFL_SIERRA_INIT) { 39402ac6454SAndrew Thompson u3g_sierra_init(udev); 39502ac6454SAndrew Thompson } else { 39602ac6454SAndrew Thompson /* no quirks */ 39702ac6454SAndrew Thompson return (USB_ERR_INVAL); 39802ac6454SAndrew Thompson } 39902ac6454SAndrew Thompson return (0); /* success */ 40002ac6454SAndrew Thompson } 40102ac6454SAndrew Thompson 40202ac6454SAndrew Thompson static int 40302ac6454SAndrew Thompson u3g_driver_loaded(struct module *mod, int what, void *arg) 40402ac6454SAndrew Thompson { 40502ac6454SAndrew Thompson switch (what) { 40602ac6454SAndrew Thompson case MOD_LOAD: 40702ac6454SAndrew Thompson /* register our autoinstall handler */ 40802ac6454SAndrew Thompson usb2_test_huawei_autoinst_p = &u3g_test_huawei_autoinst; 40902ac6454SAndrew Thompson break; 41002ac6454SAndrew Thompson case MOD_UNLOAD: 41102ac6454SAndrew Thompson usb2_test_huawei_unload(NULL); 41202ac6454SAndrew Thompson break; 41302ac6454SAndrew Thompson default: 41402ac6454SAndrew Thompson return (EOPNOTSUPP); 41502ac6454SAndrew Thompson } 41602ac6454SAndrew Thompson return (0); 41702ac6454SAndrew Thompson } 41802ac6454SAndrew Thompson 41902ac6454SAndrew Thompson static int 42002ac6454SAndrew Thompson u3g_probe(device_t self) 42102ac6454SAndrew Thompson { 422760bc48eSAndrew Thompson struct usb_attach_arg *uaa = device_get_ivars(self); 42302ac6454SAndrew Thompson 424f29a0724SAndrew Thompson if (uaa->usb_mode != USB_MODE_HOST) { 42502ac6454SAndrew Thompson return (ENXIO); 42602ac6454SAndrew Thompson } 42702ac6454SAndrew Thompson if (uaa->info.bConfigIndex != U3G_CONFIG_INDEX) { 42802ac6454SAndrew Thompson return (ENXIO); 42902ac6454SAndrew Thompson } 43002ac6454SAndrew Thompson if (uaa->info.bInterfaceClass != UICLASS_VENDOR) { 43102ac6454SAndrew Thompson return (ENXIO); 43202ac6454SAndrew Thompson } 43302ac6454SAndrew Thompson return (u3g_lookup_huawei(uaa)); 43402ac6454SAndrew Thompson } 43502ac6454SAndrew Thompson 43602ac6454SAndrew Thompson static int 43702ac6454SAndrew Thompson u3g_attach(device_t dev) 43802ac6454SAndrew Thompson { 439760bc48eSAndrew Thompson struct usb_config u3g_config_tmp[U3G_N_TRANSFER]; 440760bc48eSAndrew Thompson struct usb_attach_arg *uaa = device_get_ivars(dev); 44102ac6454SAndrew Thompson struct u3g_softc *sc = device_get_softc(dev); 442760bc48eSAndrew Thompson struct usb_interface *iface; 443760bc48eSAndrew Thompson struct usb_interface_descriptor *id; 444822e5d76SAndrew Thompson uint32_t iface_valid; 445822e5d76SAndrew Thompson int error, flags, nports; 446e92a4515SAndrew Thompson int ep, n; 44702ac6454SAndrew Thompson uint8_t i; 44802ac6454SAndrew Thompson 44902ac6454SAndrew Thompson DPRINTF("sc=%p\n", sc); 45002ac6454SAndrew Thompson 451750a2682SAndrew Thompson flags = USB_GET_DRIVER_INFO(uaa); 452750a2682SAndrew Thompson 453750a2682SAndrew Thompson if (flags & U3GFL_SAEL_M460_INIT) 454750a2682SAndrew Thompson u3g_sael_m460_init(uaa->device); 455750a2682SAndrew Thompson 45602ac6454SAndrew Thompson /* copy in USB config */ 45702ac6454SAndrew Thompson for (n = 0; n != U3G_N_TRANSFER; n++) 45802ac6454SAndrew Thompson u3g_config_tmp[n] = u3g_config[n]; 45902ac6454SAndrew Thompson 46002ac6454SAndrew Thompson device_set_usb2_desc(dev); 461deefe583SAndrew Thompson mtx_init(&sc->sc_mtx, "u3g", NULL, MTX_DEF); 46202ac6454SAndrew Thompson 46302ac6454SAndrew Thompson sc->sc_udev = uaa->device; 46402ac6454SAndrew Thompson 465e92a4515SAndrew Thompson /* Claim all interfaces on the device */ 466e92a4515SAndrew Thompson iface_valid = 0; 467e92a4515SAndrew Thompson for (i = uaa->info.bIfaceIndex; i < USB_IFACE_MAX; i++) { 468e92a4515SAndrew Thompson iface = usb2_get_iface(uaa->device, i); 469e92a4515SAndrew Thompson if (iface == NULL) 470e92a4515SAndrew Thompson break; 471e92a4515SAndrew Thompson id = usb2_get_interface_descriptor(iface); 472e92a4515SAndrew Thompson if (id == NULL || id->bInterfaceClass != UICLASS_VENDOR) 473e92a4515SAndrew Thompson continue; 474e92a4515SAndrew Thompson usb2_set_parent_iface(uaa->device, i, uaa->info.bIfaceIndex); 475e92a4515SAndrew Thompson iface_valid |= (1<<i); 476e92a4515SAndrew Thompson } 47702ac6454SAndrew Thompson 478e92a4515SAndrew Thompson i = 0; /* interface index */ 479e92a4515SAndrew Thompson ep = 0; /* endpoint index */ 480e92a4515SAndrew Thompson nports = 0; /* number of ports */ 481e92a4515SAndrew Thompson while (i < USB_IFACE_MAX) { 482e92a4515SAndrew Thompson if ((iface_valid & (1<<i)) == 0) { 483e92a4515SAndrew Thompson i++; 484e92a4515SAndrew Thompson continue; 485e92a4515SAndrew Thompson } 48602ac6454SAndrew Thompson 48702ac6454SAndrew Thompson /* update BULK endpoint index */ 488e92a4515SAndrew Thompson for (n = 0; n < U3G_N_TRANSFER; n++) 489e92a4515SAndrew Thompson u3g_config_tmp[n].ep_index = ep; 49002ac6454SAndrew Thompson 49102ac6454SAndrew Thompson /* try to allocate a set of BULK endpoints */ 492e92a4515SAndrew Thompson error = usb2_transfer_setup(uaa->device, &i, 493e92a4515SAndrew Thompson sc->sc_xfer[nports], u3g_config_tmp, U3G_N_TRANSFER, 494e92a4515SAndrew Thompson &sc->sc_ucom[nports], &sc->sc_mtx); 49502ac6454SAndrew Thompson if (error) { 49602ac6454SAndrew Thompson /* next interface */ 497e92a4515SAndrew Thompson i++; 498e92a4515SAndrew Thompson ep = 0; 49902ac6454SAndrew Thompson continue; 50002ac6454SAndrew Thompson } 50102ac6454SAndrew Thompson 50202ac6454SAndrew Thompson /* set stall by default */ 503deefe583SAndrew Thompson mtx_lock(&sc->sc_mtx); 504e92a4515SAndrew Thompson usb2_transfer_set_stall(sc->sc_xfer[nports][U3G_BULK_WR]); 505e92a4515SAndrew Thompson usb2_transfer_set_stall(sc->sc_xfer[nports][U3G_BULK_RD]); 506deefe583SAndrew Thompson mtx_unlock(&sc->sc_mtx); 50702ac6454SAndrew Thompson 508e92a4515SAndrew Thompson nports++; /* found one port */ 509e92a4515SAndrew Thompson ep++; 510e92a4515SAndrew Thompson if (nports == U3G_MAXPORTS) 511e92a4515SAndrew Thompson break; 51202ac6454SAndrew Thompson } 513e92a4515SAndrew Thompson if (nports == 0) { 514e92a4515SAndrew Thompson device_printf(dev, "no ports found\n"); 515e92a4515SAndrew Thompson goto detach; 516e92a4515SAndrew Thompson } 517e92a4515SAndrew Thompson sc->sc_numports = nports; 51802ac6454SAndrew Thompson 51902ac6454SAndrew Thompson error = usb2_com_attach(&sc->sc_super_ucom, sc->sc_ucom, 520deefe583SAndrew Thompson sc->sc_numports, sc, &u3g_callback, &sc->sc_mtx); 52102ac6454SAndrew Thompson if (error) { 52202ac6454SAndrew Thompson DPRINTF("usb2_com_attach failed\n"); 52302ac6454SAndrew Thompson goto detach; 52402ac6454SAndrew Thompson } 525e92a4515SAndrew Thompson if (sc->sc_numports > 1) 526e92a4515SAndrew Thompson device_printf(dev, "Found %u ports.\n", sc->sc_numports); 52702ac6454SAndrew Thompson return (0); 52802ac6454SAndrew Thompson 52902ac6454SAndrew Thompson detach: 53002ac6454SAndrew Thompson u3g_detach(dev); 53102ac6454SAndrew Thompson return (ENXIO); 53202ac6454SAndrew Thompson } 53302ac6454SAndrew Thompson 53402ac6454SAndrew Thompson static int 53502ac6454SAndrew Thompson u3g_detach(device_t dev) 53602ac6454SAndrew Thompson { 53702ac6454SAndrew Thompson struct u3g_softc *sc = device_get_softc(dev); 53802ac6454SAndrew Thompson uint8_t m; 53902ac6454SAndrew Thompson 54002ac6454SAndrew Thompson DPRINTF("sc=%p\n", sc); 54102ac6454SAndrew Thompson 54202ac6454SAndrew Thompson /* NOTE: It is not dangerous to detach more ports than attached! */ 54302ac6454SAndrew Thompson usb2_com_detach(&sc->sc_super_ucom, sc->sc_ucom, U3G_MAXPORTS); 54402ac6454SAndrew Thompson 54502ac6454SAndrew Thompson for (m = 0; m != U3G_MAXPORTS; m++) 54602ac6454SAndrew Thompson usb2_transfer_unsetup(sc->sc_xfer[m], U3G_N_TRANSFER); 547deefe583SAndrew Thompson mtx_destroy(&sc->sc_mtx); 54802ac6454SAndrew Thompson 54902ac6454SAndrew Thompson return (0); 55002ac6454SAndrew Thompson } 55102ac6454SAndrew Thompson 55202ac6454SAndrew Thompson static void 553760bc48eSAndrew Thompson u3g_start_read(struct ucom_softc *ucom) 55402ac6454SAndrew Thompson { 55502ac6454SAndrew Thompson struct u3g_softc *sc = ucom->sc_parent; 55602ac6454SAndrew Thompson 55702ac6454SAndrew Thompson /* start read endpoint */ 55802ac6454SAndrew Thompson usb2_transfer_start(sc->sc_xfer[ucom->sc_local_unit][U3G_BULK_RD]); 55902ac6454SAndrew Thompson return; 56002ac6454SAndrew Thompson } 56102ac6454SAndrew Thompson 56202ac6454SAndrew Thompson static void 563760bc48eSAndrew Thompson u3g_stop_read(struct ucom_softc *ucom) 56402ac6454SAndrew Thompson { 56502ac6454SAndrew Thompson struct u3g_softc *sc = ucom->sc_parent; 56602ac6454SAndrew Thompson 56702ac6454SAndrew Thompson /* stop read endpoint */ 56802ac6454SAndrew Thompson usb2_transfer_stop(sc->sc_xfer[ucom->sc_local_unit][U3G_BULK_RD]); 56902ac6454SAndrew Thompson return; 57002ac6454SAndrew Thompson } 57102ac6454SAndrew Thompson 57202ac6454SAndrew Thompson static void 573760bc48eSAndrew Thompson u3g_start_write(struct ucom_softc *ucom) 57402ac6454SAndrew Thompson { 57502ac6454SAndrew Thompson struct u3g_softc *sc = ucom->sc_parent; 57602ac6454SAndrew Thompson 57702ac6454SAndrew Thompson usb2_transfer_start(sc->sc_xfer[ucom->sc_local_unit][U3G_BULK_WR]); 57802ac6454SAndrew Thompson return; 57902ac6454SAndrew Thompson } 58002ac6454SAndrew Thompson 58102ac6454SAndrew Thompson static void 582760bc48eSAndrew Thompson u3g_stop_write(struct ucom_softc *ucom) 58302ac6454SAndrew Thompson { 58402ac6454SAndrew Thompson struct u3g_softc *sc = ucom->sc_parent; 58502ac6454SAndrew Thompson 58602ac6454SAndrew Thompson usb2_transfer_stop(sc->sc_xfer[ucom->sc_local_unit][U3G_BULK_WR]); 58702ac6454SAndrew Thompson return; 58802ac6454SAndrew Thompson } 58902ac6454SAndrew Thompson 59002ac6454SAndrew Thompson static void 591760bc48eSAndrew Thompson u3g_write_callback(struct usb_xfer *xfer) 59202ac6454SAndrew Thompson { 593760bc48eSAndrew Thompson struct ucom_softc *ucom = xfer->priv_sc; 59402ac6454SAndrew Thompson uint32_t actlen; 59502ac6454SAndrew Thompson 59602ac6454SAndrew Thompson switch (USB_GET_STATE(xfer)) { 59702ac6454SAndrew Thompson case USB_ST_TRANSFERRED: 59802ac6454SAndrew Thompson case USB_ST_SETUP: 59902ac6454SAndrew Thompson tr_setup: 60002ac6454SAndrew Thompson if (usb2_com_get_data(ucom, xfer->frbuffers, 0, 60102ac6454SAndrew Thompson U3G_BSIZE, &actlen)) { 60202ac6454SAndrew Thompson xfer->frlengths[0] = actlen; 60302ac6454SAndrew Thompson usb2_start_hardware(xfer); 60402ac6454SAndrew Thompson } 60502ac6454SAndrew Thompson break; 60602ac6454SAndrew Thompson 60702ac6454SAndrew Thompson default: /* Error */ 60802ac6454SAndrew Thompson if (xfer->error != USB_ERR_CANCELLED) { 60902ac6454SAndrew Thompson /* do a builtin clear-stall */ 61002ac6454SAndrew Thompson xfer->flags.stall_pipe = 1; 61102ac6454SAndrew Thompson goto tr_setup; 61202ac6454SAndrew Thompson } 61302ac6454SAndrew Thompson break; 61402ac6454SAndrew Thompson } 61502ac6454SAndrew Thompson return; 61602ac6454SAndrew Thompson } 61702ac6454SAndrew Thompson 61802ac6454SAndrew Thompson static void 619760bc48eSAndrew Thompson u3g_read_callback(struct usb_xfer *xfer) 62002ac6454SAndrew Thompson { 621760bc48eSAndrew Thompson struct ucom_softc *ucom = xfer->priv_sc; 62202ac6454SAndrew Thompson 62302ac6454SAndrew Thompson switch (USB_GET_STATE(xfer)) { 62402ac6454SAndrew Thompson case USB_ST_TRANSFERRED: 62502ac6454SAndrew Thompson usb2_com_put_data(ucom, xfer->frbuffers, 0, xfer->actlen); 62602ac6454SAndrew Thompson 62702ac6454SAndrew Thompson case USB_ST_SETUP: 62802ac6454SAndrew Thompson tr_setup: 62902ac6454SAndrew Thompson xfer->frlengths[0] = xfer->max_data_length; 63002ac6454SAndrew Thompson usb2_start_hardware(xfer); 63102ac6454SAndrew Thompson break; 63202ac6454SAndrew Thompson 63302ac6454SAndrew Thompson default: /* Error */ 63402ac6454SAndrew Thompson if (xfer->error != USB_ERR_CANCELLED) { 63502ac6454SAndrew Thompson /* do a builtin clear-stall */ 63602ac6454SAndrew Thompson xfer->flags.stall_pipe = 1; 63702ac6454SAndrew Thompson goto tr_setup; 63802ac6454SAndrew Thompson } 63902ac6454SAndrew Thompson break; 64002ac6454SAndrew Thompson } 64102ac6454SAndrew Thompson return; 64202ac6454SAndrew Thompson } 643