2 * Copyright (c) 2008 AnyWi Technologies
3 * Author: Andrea Guzzo <aguzzo@anywi.com>
4 * * based on uark.c 1.1 2006/08/14 08:30:22 jsg *
5 * * parts from ubsa.c 183348 2008-09-25 12:00:56Z phk *
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
25 * - The detour through the tty layer is ridiculously expensive wrt
26 * buffering due to the high speeds.
28 * We should consider adding a simple r/w device which allows
29 * attaching of PPP in a more efficient way.
34 #include <sys/stdint.h>
35 #include <sys/stddef.h>
36 #include <sys/param.h>
37 #include <sys/queue.h>
38 #include <sys/types.h>
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
42 #include <sys/linker_set.h>
43 #include <sys/module.h>
45 #include <sys/mutex.h>
46 #include <sys/condvar.h>
47 #include <sys/sysctl.h>
49 #include <sys/unistd.h>
50 #include <sys/callout.h>
51 #include <sys/malloc.h>
54 #include <dev/usb/usb.h>
55 #include <dev/usb/usbdi.h>
56 #include <dev/usb/usbdi_util.h>
59 #define USB_DEBUG_VAR u3g_debug
60 #include <dev/usb/usb_debug.h>
61 #include <dev/usb/usb_process.h>
62 #include <dev/usb/usb_dynamic.h>
63 #include <dev/usb/usb_msctest.h>
64 #include <dev/usb/usb_device.h>
66 #include <dev/usb/serial/usb_serial.h>
69 static int u3g_debug = 0;
71 SYSCTL_NODE(_hw_usb, OID_AUTO, u3g, CTLFLAG_RW, 0, "USB 3g");
72 SYSCTL_INT(_hw_usb_u3g, OID_AUTO, debug, CTLFLAG_RW,
73 &u3g_debug, 0, "Debug level");
76 #define U3G_MAXPORTS 8
77 #define U3G_CONFIG_INDEX 0
78 #define U3G_BSIZE 2048
89 #define U3GFL_HUAWEI_INIT 0x0001 /* Init command required */
90 #define U3GFL_SCSI_EJECT 0x0002 /* SCSI eject command required */
91 #define U3GFL_SIERRA_INIT 0x0004 /* Init command required */
92 #define U3GFL_SAEL_M460_INIT 0x0008 /* Init device */
101 struct ucom_super_softc sc_super_ucom;
102 struct ucom_softc sc_ucom[U3G_MAXPORTS];
104 struct usb_xfer *sc_xfer[U3G_MAXPORTS][U3G_N_TRANSFER];
105 struct usb_device *sc_udev;
108 uint8_t sc_lsr; /* local status register */
109 uint8_t sc_msr; /* U3G status register */
113 static device_probe_t u3g_probe;
114 static device_attach_t u3g_attach;
115 static device_detach_t u3g_detach;
117 static usb_callback_t u3g_write_callback;
118 static usb_callback_t u3g_read_callback;
120 static void u3g_start_read(struct ucom_softc *ucom);
121 static void u3g_stop_read(struct ucom_softc *ucom);
122 static void u3g_start_write(struct ucom_softc *ucom);
123 static void u3g_stop_write(struct ucom_softc *ucom);
125 static int u3g_driver_loaded(struct module *mod, int what, void *arg);
127 static const struct usb_config u3g_config[U3G_N_TRANSFER] = {
131 .endpoint = UE_ADDR_ANY,
132 .direction = UE_DIR_OUT,
133 .bufsize = U3G_BSIZE,/* bytes */
134 .flags = {.pipe_bof = 1,.force_short_xfer = 1,},
135 .callback = &u3g_write_callback,
140 .endpoint = UE_ADDR_ANY,
141 .direction = UE_DIR_IN,
142 .bufsize = U3G_BSIZE,/* bytes */
143 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
144 .callback = &u3g_read_callback,
148 static const struct ucom_callback u3g_callback = {
149 .ucom_start_read = &u3g_start_read,
150 .ucom_stop_read = &u3g_stop_read,
151 .ucom_start_write = &u3g_start_write,
152 .ucom_stop_write = &u3g_stop_write,
155 static device_method_t u3g_methods[] = {
156 DEVMETHOD(device_probe, u3g_probe),
157 DEVMETHOD(device_attach, u3g_attach),
158 DEVMETHOD(device_detach, u3g_detach),
162 static devclass_t u3g_devclass;
164 static driver_t u3g_driver = {
166 .methods = u3g_methods,
167 .size = sizeof(struct u3g_softc),
170 DRIVER_MODULE(u3g, uhub, u3g_driver, u3g_devclass, u3g_driver_loaded, 0);
171 MODULE_DEPEND(u3g, ucom, 1, 1, 1);
172 MODULE_DEPEND(u3g, usb, 1, 1, 1);
174 static const struct usb_device_id u3g_devs[] = {
175 #define U3G_DEV(v,p,i) { USB_VPI(USB_VENDOR_##v, USB_PRODUCT_##v##_##p, i) }
177 U3G_DEV(OPTION, GT3G, 0),
178 U3G_DEV(OPTION, GT3GQUAD, 0),
179 U3G_DEV(OPTION, GT3GPLUS, 0),
180 U3G_DEV(OPTION, GTMAX36, 0),
181 U3G_DEV(OPTION, GTHSDPA, 0),
182 U3G_DEV(OPTION, GTMAXHSUPA, 0),
183 U3G_DEV(OPTION, GTMAXHSUPAE, 0),
184 U3G_DEV(OPTION, GTMAX380HSUPAE, 0),
185 U3G_DEV(OPTION, VODAFONEMC3G, 0),
186 /* OEM: Qualcomm, Inc. */
187 U3G_DEV(QUALCOMMINC, ZTE_STOR, U3GFL_SCSI_EJECT),
188 U3G_DEV(QUALCOMMINC, CDMA_MSM, U3GFL_SCSI_EJECT),
190 U3G_DEV(HUAWEI, MOBILE, U3GFL_HUAWEI_INIT),
191 U3G_DEV(HUAWEI, E180V, U3GFL_HUAWEI_INIT),
192 U3G_DEV(HUAWEI, E220, U3GFL_HUAWEI_INIT),
194 U3G_DEV(NOVATEL, CDMA_MODEM, 0),
195 U3G_DEV(NOVATEL, ES620, 0),
196 U3G_DEV(NOVATEL, MC950D, 0),
197 U3G_DEV(NOVATEL, U720, 0),
198 U3G_DEV(NOVATEL, U727, 0),
199 U3G_DEV(NOVATEL, U740, 0),
200 U3G_DEV(NOVATEL, U740_2, 0),
201 U3G_DEV(NOVATEL, U870, 0),
202 U3G_DEV(NOVATEL, V620, 0),
203 U3G_DEV(NOVATEL, V640, 0),
204 U3G_DEV(NOVATEL, V720, 0),
205 U3G_DEV(NOVATEL, V740, 0),
206 U3G_DEV(NOVATEL, X950D, 0),
207 U3G_DEV(NOVATEL, XU870, 0),
208 U3G_DEV(NOVATEL, ZEROCD, U3GFL_SCSI_EJECT),
209 U3G_DEV(NOVATEL, U760, U3GFL_SCSI_EJECT),
210 U3G_DEV(DELL, U740, 0),
212 U3G_DEV(MERLIN, V620, 0),
213 /* OEM: Sierra Wireless: */
214 U3G_DEV(SIERRA, AIRCARD580, 0),
215 U3G_DEV(SIERRA, AIRCARD595, 0),
216 U3G_DEV(SIERRA, AC595U, 0),
217 U3G_DEV(SIERRA, AC597E, 0),
218 U3G_DEV(SIERRA, C597, 0),
219 U3G_DEV(SIERRA, AC880, 0),
220 U3G_DEV(SIERRA, AC880E, 0),
221 U3G_DEV(SIERRA, AC880U, 0),
222 U3G_DEV(SIERRA, AC881, 0),
223 U3G_DEV(SIERRA, AC881E, 0),
224 U3G_DEV(SIERRA, AC881U, 0),
225 U3G_DEV(SIERRA, AC885U, 0),
226 U3G_DEV(SIERRA, EM5625, 0),
227 U3G_DEV(SIERRA, MC5720, 0),
228 U3G_DEV(SIERRA, MC5720_2, 0),
229 U3G_DEV(SIERRA, MC5725, 0),
230 U3G_DEV(SIERRA, MINI5725, 0),
231 U3G_DEV(SIERRA, AIRCARD875, 0),
232 U3G_DEV(SIERRA, MC8755, 0),
233 U3G_DEV(SIERRA, MC8755_2, 0),
234 U3G_DEV(SIERRA, MC8755_3, 0),
235 U3G_DEV(SIERRA, MC8765, 0),
236 U3G_DEV(SIERRA, AC875U, 0),
237 U3G_DEV(SIERRA, MC8775_2, 0),
238 U3G_DEV(SIERRA, MC8780, 0),
239 U3G_DEV(SIERRA, MC8781, 0),
240 U3G_DEV(HP, HS2300, 0),
241 /* Sierra TruInstaller device ID */
242 U3G_DEV(SIERRA, TRUINSTALL, U3GFL_SIERRA_INIT),
244 U3G_DEV(SILABS, SAEL, U3GFL_SAEL_M460_INIT),
248 u3g_sierra_init(struct usb_device *udev)
250 struct usb_device_request req;
252 req.bmRequestType = UT_VENDOR;
253 req.bRequest = UR_SET_INTERFACE;
254 USETW(req.wValue, UF_DEVICE_REMOTE_WAKEUP);
255 USETW(req.wIndex, UHF_PORT_CONNECTION);
256 USETW(req.wLength, 0);
258 if (usbd_do_request_flags(udev, NULL, &req,
259 NULL, 0, NULL, USB_MS_HZ)) {
260 /* ignore any errors */
266 u3g_huawei_init(struct usb_device *udev)
268 struct usb_device_request req;
270 req.bmRequestType = UT_WRITE_DEVICE;
271 req.bRequest = UR_SET_FEATURE;
272 USETW(req.wValue, UF_DEVICE_REMOTE_WAKEUP);
273 USETW(req.wIndex, UHF_PORT_SUSPEND);
274 USETW(req.wLength, 0);
276 if (usbd_do_request_flags(udev, NULL, &req,
277 NULL, 0, NULL, USB_MS_HZ)) {
278 /* ignore any errors */
284 u3g_sael_m460_init(struct usb_device *udev)
286 static const uint8_t setup[][24] = {
287 { 0x41, 0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
288 { 0x41, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00 },
289 { 0x41, 0x13, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00,
290 0x01, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00,
291 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
292 { 0xc1, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x40, 0x02 },
293 { 0xc1, 0x08, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00 },
294 { 0x41, 0x07, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00 },
295 { 0xc1, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02 },
296 { 0x41, 0x01, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00 },
297 { 0x41, 0x07, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00 },
298 { 0x41, 0x03, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00 },
299 { 0x41, 0x19, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00,
300 0x00, 0x00, 0x00, 0x00, 0x11, 0x13 },
301 { 0x41, 0x13, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00,
302 0x09, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00,
303 0x0a, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00 },
304 { 0x41, 0x12, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00 },
305 { 0x41, 0x01, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00 },
306 { 0x41, 0x07, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00 },
307 { 0x41, 0x03, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00 },
308 { 0x41, 0x19, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00,
309 0x00, 0x00, 0x00, 0x00, 0x11, 0x13 },
310 { 0x41, 0x13, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00,
311 0x09, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00,
312 0x0a, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00 },
313 { 0x41, 0x07, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00 },
316 struct usb_device_request req;
324 if (usbd_req_set_alt_interface_no(udev, NULL, 0, 0)) {
325 DPRINTFN(0, "Alt setting 0 failed\n");
329 for (n = 0; n != (sizeof(setup)/sizeof(setup[0])); n++) {
331 memcpy(&req, setup[n], sizeof(req));
333 len = UGETW(req.wLength);
334 if (req.bmRequestType & UE_DIR_IN) {
335 if (len > sizeof(buf)) {
336 DPRINTFN(0, "too small buffer\n");
339 err = usbd_do_request(udev, NULL, &req, buf);
341 if (len > (sizeof(setup[0]) - 8)) {
342 DPRINTFN(0, "too small buffer\n");
345 err = usbd_do_request(udev, NULL, &req,
346 __DECONST(uint8_t *, &setup[n][8]));
349 DPRINTFN(1, "request %u failed\n",
352 * Some of the requests will fail. Stop doing
353 * requests when we are getting timeouts so
354 * that we don't block the explore/attach
357 if (err == USB_ERR_TIMEOUT)
364 u3g_lookup_huawei(struct usb_attach_arg *uaa)
366 /* Calling the lookup function will also set the driver info! */
367 return (usbd_lookup_id_by_uaa(u3g_devs, sizeof(u3g_devs), uaa));
371 * The following function handles 3G modem devices (E220, Mobile,
372 * etc.) with auto-install flash disks for Windows/MacOSX on the first
373 * interface. After some command or some delay they change appearance
377 u3g_test_huawei_autoinst(struct usb_device *udev,
378 struct usb_attach_arg *uaa)
380 struct usb_interface *iface;
381 struct usb_interface_descriptor *id;
385 return (USB_ERR_INVAL);
387 iface = usbd_get_iface(udev, 0);
389 return (USB_ERR_INVAL);
393 return (USB_ERR_INVAL);
395 if (id->bInterfaceClass != UICLASS_MASS) {
396 return (USB_ERR_INVAL);
398 if (u3g_lookup_huawei(uaa)) {
399 /* no device match */
400 return (USB_ERR_INVAL);
402 flags = USB_GET_DRIVER_INFO(uaa);
404 if (flags & U3GFL_HUAWEI_INIT) {
405 u3g_huawei_init(udev);
406 } else if (flags & U3GFL_SCSI_EJECT) {
407 return (usb_test_autoinstall(udev, 0, 1));
408 } else if (flags & U3GFL_SIERRA_INIT) {
409 u3g_sierra_init(udev);
412 return (USB_ERR_INVAL);
414 return (0); /* success */
418 u3g_driver_loaded(struct module *mod, int what, void *arg)
422 /* register our autoinstall handler */
423 usb_test_huawei_autoinst_p = &u3g_test_huawei_autoinst;
426 usb_test_huawei_unload(NULL);
435 u3g_probe(device_t self)
437 struct usb_attach_arg *uaa = device_get_ivars(self);
439 if (uaa->usb_mode != USB_MODE_HOST) {
442 if (uaa->info.bConfigIndex != U3G_CONFIG_INDEX) {
445 if (uaa->info.bInterfaceClass != UICLASS_VENDOR) {
448 return (u3g_lookup_huawei(uaa));
452 u3g_attach(device_t dev)
454 struct usb_config u3g_config_tmp[U3G_N_TRANSFER];
455 struct usb_attach_arg *uaa = device_get_ivars(dev);
456 struct u3g_softc *sc = device_get_softc(dev);
457 struct usb_interface *iface;
458 struct usb_interface_descriptor *id;
459 uint32_t iface_valid;
460 int error, flags, nports;
464 DPRINTF("sc=%p\n", sc);
466 flags = USB_GET_DRIVER_INFO(uaa);
468 if (flags & U3GFL_SAEL_M460_INIT)
469 u3g_sael_m460_init(uaa->device);
471 /* copy in USB config */
472 for (n = 0; n != U3G_N_TRANSFER; n++)
473 u3g_config_tmp[n] = u3g_config[n];
475 device_set_usb_desc(dev);
476 mtx_init(&sc->sc_mtx, "u3g", NULL, MTX_DEF);
478 sc->sc_udev = uaa->device;
480 /* Claim all interfaces on the device */
482 for (i = uaa->info.bIfaceIndex; i < USB_IFACE_MAX; i++) {
483 iface = usbd_get_iface(uaa->device, i);
486 id = usbd_get_interface_descriptor(iface);
487 if (id == NULL || id->bInterfaceClass != UICLASS_VENDOR)
489 usbd_set_parent_iface(uaa->device, i, uaa->info.bIfaceIndex);
490 iface_valid |= (1<<i);
493 i = 0; /* interface index */
494 ep = 0; /* endpoint index */
495 nports = 0; /* number of ports */
496 while (i < USB_IFACE_MAX) {
497 if ((iface_valid & (1<<i)) == 0) {
502 /* update BULK endpoint index */
503 for (n = 0; n < U3G_N_TRANSFER; n++)
504 u3g_config_tmp[n].ep_index = ep;
506 /* try to allocate a set of BULK endpoints */
507 error = usbd_transfer_setup(uaa->device, &i,
508 sc->sc_xfer[nports], u3g_config_tmp, U3G_N_TRANSFER,
509 &sc->sc_ucom[nports], &sc->sc_mtx);
517 /* set stall by default */
518 mtx_lock(&sc->sc_mtx);
519 usbd_xfer_set_stall(sc->sc_xfer[nports][U3G_BULK_WR]);
520 usbd_xfer_set_stall(sc->sc_xfer[nports][U3G_BULK_RD]);
521 mtx_unlock(&sc->sc_mtx);
523 nports++; /* found one port */
525 if (nports == U3G_MAXPORTS)
529 device_printf(dev, "no ports found\n");
532 sc->sc_numports = nports;
534 error = ucom_attach(&sc->sc_super_ucom, sc->sc_ucom,
535 sc->sc_numports, sc, &u3g_callback, &sc->sc_mtx);
537 DPRINTF("ucom_attach failed\n");
540 if (sc->sc_numports > 1)
541 device_printf(dev, "Found %u ports.\n", sc->sc_numports);
550 u3g_detach(device_t dev)
552 struct u3g_softc *sc = device_get_softc(dev);
555 DPRINTF("sc=%p\n", sc);
557 /* NOTE: It is not dangerous to detach more ports than attached! */
558 ucom_detach(&sc->sc_super_ucom, sc->sc_ucom, U3G_MAXPORTS);
560 for (m = 0; m != U3G_MAXPORTS; m++)
561 usbd_transfer_unsetup(sc->sc_xfer[m], U3G_N_TRANSFER);
562 mtx_destroy(&sc->sc_mtx);
568 u3g_start_read(struct ucom_softc *ucom)
570 struct u3g_softc *sc = ucom->sc_parent;
572 /* start read endpoint */
573 usbd_transfer_start(sc->sc_xfer[ucom->sc_local_unit][U3G_BULK_RD]);
578 u3g_stop_read(struct ucom_softc *ucom)
580 struct u3g_softc *sc = ucom->sc_parent;
582 /* stop read endpoint */
583 usbd_transfer_stop(sc->sc_xfer[ucom->sc_local_unit][U3G_BULK_RD]);
588 u3g_start_write(struct ucom_softc *ucom)
590 struct u3g_softc *sc = ucom->sc_parent;
592 usbd_transfer_start(sc->sc_xfer[ucom->sc_local_unit][U3G_BULK_WR]);
597 u3g_stop_write(struct ucom_softc *ucom)
599 struct u3g_softc *sc = ucom->sc_parent;
601 usbd_transfer_stop(sc->sc_xfer[ucom->sc_local_unit][U3G_BULK_WR]);
606 u3g_write_callback(struct usb_xfer *xfer, usb_error_t error)
608 struct ucom_softc *ucom = usbd_xfer_softc(xfer);
609 struct usb_page_cache *pc;
612 switch (USB_GET_STATE(xfer)) {
613 case USB_ST_TRANSFERRED:
616 pc = usbd_xfer_get_frame(xfer, 0);
617 if (ucom_get_data(ucom, pc, 0, U3G_BSIZE, &actlen)) {
618 usbd_xfer_set_frame_len(xfer, 0, actlen);
619 usbd_transfer_submit(xfer);
624 if (error != USB_ERR_CANCELLED) {
625 /* do a builtin clear-stall */
626 usbd_xfer_set_stall(xfer);
635 u3g_read_callback(struct usb_xfer *xfer, usb_error_t error)
637 struct ucom_softc *ucom = usbd_xfer_softc(xfer);
638 struct usb_page_cache *pc;
641 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
643 switch (USB_GET_STATE(xfer)) {
644 case USB_ST_TRANSFERRED:
645 pc = usbd_xfer_get_frame(xfer, 0);
646 ucom_put_data(ucom, pc, 0, actlen);
650 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
651 usbd_transfer_submit(xfer);
655 if (error != USB_ERR_CANCELLED) {
656 /* do a builtin clear-stall */
657 usbd_xfer_set_stall(xfer);