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, VODAFONEMC3G, 0),
184 /* OEM: Qualcomm, Inc. */
185 U3G_DEV(QUALCOMMINC, ZTE_STOR, U3GFL_SCSI_EJECT),
186 U3G_DEV(QUALCOMMINC, CDMA_MSM, U3GFL_SCSI_EJECT),
188 U3G_DEV(HUAWEI, MOBILE, U3GFL_HUAWEI_INIT),
189 U3G_DEV(HUAWEI, E180V, U3GFL_HUAWEI_INIT),
190 U3G_DEV(HUAWEI, E220, U3GFL_HUAWEI_INIT),
192 U3G_DEV(NOVATEL, CDMA_MODEM, 0),
193 U3G_DEV(NOVATEL, ES620, 0),
194 U3G_DEV(NOVATEL, MC950D, 0),
195 U3G_DEV(NOVATEL, U720, 0),
196 U3G_DEV(NOVATEL, U727, 0),
197 U3G_DEV(NOVATEL, U740, 0),
198 U3G_DEV(NOVATEL, U740_2, 0),
199 U3G_DEV(NOVATEL, U870, 0),
200 U3G_DEV(NOVATEL, V620, 0),
201 U3G_DEV(NOVATEL, V640, 0),
202 U3G_DEV(NOVATEL, V720, 0),
203 U3G_DEV(NOVATEL, V740, 0),
204 U3G_DEV(NOVATEL, X950D, 0),
205 U3G_DEV(NOVATEL, XU870, 0),
206 U3G_DEV(NOVATEL, ZEROCD, U3GFL_SCSI_EJECT),
207 U3G_DEV(DELL, U740, 0),
209 U3G_DEV(MERLIN, V620, 0),
210 /* OEM: Sierra Wireless: */
211 U3G_DEV(SIERRA, AIRCARD580, 0),
212 U3G_DEV(SIERRA, AIRCARD595, 0),
213 U3G_DEV(SIERRA, AC595U, 0),
214 U3G_DEV(SIERRA, AC597E, 0),
215 U3G_DEV(SIERRA, C597, 0),
216 U3G_DEV(SIERRA, AC880, 0),
217 U3G_DEV(SIERRA, AC880E, 0),
218 U3G_DEV(SIERRA, AC880U, 0),
219 U3G_DEV(SIERRA, AC881, 0),
220 U3G_DEV(SIERRA, AC881E, 0),
221 U3G_DEV(SIERRA, AC881U, 0),
222 U3G_DEV(SIERRA, AC885U, 0),
223 U3G_DEV(SIERRA, EM5625, 0),
224 U3G_DEV(SIERRA, MC5720, 0),
225 U3G_DEV(SIERRA, MC5720_2, 0),
226 U3G_DEV(SIERRA, MC5725, 0),
227 U3G_DEV(SIERRA, MINI5725, 0),
228 U3G_DEV(SIERRA, AIRCARD875, 0),
229 U3G_DEV(SIERRA, MC8755, 0),
230 U3G_DEV(SIERRA, MC8755_2, 0),
231 U3G_DEV(SIERRA, MC8755_3, 0),
232 U3G_DEV(SIERRA, MC8765, 0),
233 U3G_DEV(SIERRA, AC875U, 0),
234 U3G_DEV(SIERRA, MC8775_2, 0),
235 U3G_DEV(SIERRA, MC8780, 0),
236 U3G_DEV(SIERRA, MC8781, 0),
237 U3G_DEV(HP, HS2300, 0),
238 /* Sierra TruInstaller device ID */
239 U3G_DEV(SIERRA, TRUINSTALL, U3GFL_SIERRA_INIT),
241 U3G_DEV(SILABS, SAEL, U3GFL_SAEL_M460_INIT),
245 u3g_sierra_init(struct usb_device *udev)
247 struct usb_device_request req;
251 req.bmRequestType = UT_VENDOR;
252 req.bRequest = UR_SET_INTERFACE;
253 USETW(req.wValue, UF_DEVICE_REMOTE_WAKEUP);
254 USETW(req.wIndex, UHF_PORT_CONNECTION);
255 USETW(req.wLength, 0);
257 if (usbd_do_request_flags(udev, NULL, &req,
258 NULL, 0, NULL, USB_MS_HZ)) {
259 /* ignore any errors */
265 u3g_huawei_init(struct usb_device *udev)
267 struct usb_device_request req;
271 req.bmRequestType = UT_WRITE_DEVICE;
272 req.bRequest = UR_SET_FEATURE;
273 USETW(req.wValue, UF_DEVICE_REMOTE_WAKEUP);
274 USETW(req.wIndex, UHF_PORT_SUSPEND);
275 USETW(req.wLength, 0);
277 if (usbd_do_request_flags(udev, NULL, &req,
278 NULL, 0, NULL, USB_MS_HZ)) {
279 /* ignore any errors */
285 u3g_sael_m460_init(struct usb_device *udev)
287 static const uint8_t setup[][24] = {
288 { 0x41, 0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
289 { 0x41, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00 },
290 { 0x41, 0x13, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00,
291 0x01, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00,
292 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
293 { 0xc1, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x40, 0x02 },
294 { 0xc1, 0x08, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00 },
295 { 0x41, 0x07, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00 },
296 { 0xc1, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02 },
297 { 0x41, 0x01, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00 },
298 { 0x41, 0x07, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00 },
299 { 0x41, 0x03, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00 },
300 { 0x41, 0x19, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00,
301 0x00, 0x00, 0x00, 0x00, 0x11, 0x13 },
302 { 0x41, 0x13, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00,
303 0x09, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00,
304 0x0a, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00 },
305 { 0x41, 0x12, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00 },
306 { 0x41, 0x01, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00 },
307 { 0x41, 0x07, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00 },
308 { 0x41, 0x03, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00 },
309 { 0x41, 0x19, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00,
310 0x00, 0x00, 0x00, 0x00, 0x11, 0x13 },
311 { 0x41, 0x13, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00,
312 0x09, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00,
313 0x0a, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00 },
314 { 0x41, 0x07, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00 },
317 struct usb_device_request req;
325 if (usbd_req_set_alt_interface_no(udev, NULL, 0, 0)) {
326 DPRINTFN(0, "Alt setting 0 failed\n");
330 for (n = 0; n != (sizeof(setup)/sizeof(setup[0])); n++) {
332 memcpy(&req, setup[n], sizeof(req));
334 len = UGETW(req.wLength);
335 if (req.bmRequestType & UE_DIR_IN) {
336 if (len > sizeof(buf)) {
337 DPRINTFN(0, "too small buffer\n");
340 err = usbd_do_request(udev, NULL, &req, buf);
342 if (len > (sizeof(setup[0]) - 8)) {
343 DPRINTFN(0, "too small buffer\n");
346 err = usbd_do_request(udev, NULL, &req,
347 __DECONST(uint8_t *, &setup[n][8]));
350 DPRINTFN(1, "request %u failed\n",
353 * Some of the requests will fail. Stop doing
354 * requests when we are getting timeouts so
355 * that we don't block the explore/attach
358 if (err == USB_ERR_TIMEOUT)
365 u3g_lookup_huawei(struct usb_attach_arg *uaa)
367 /* Calling the lookup function will also set the driver info! */
368 return (usbd_lookup_id_by_uaa(u3g_devs, sizeof(u3g_devs), uaa));
372 * The following function handles 3G modem devices (E220, Mobile,
373 * etc.) with auto-install flash disks for Windows/MacOSX on the first
374 * interface. After some command or some delay they change appearance
378 u3g_test_huawei_autoinst(struct usb_device *udev,
379 struct usb_attach_arg *uaa)
381 struct usb_interface *iface;
382 struct usb_interface_descriptor *id;
386 return (USB_ERR_INVAL);
388 iface = usbd_get_iface(udev, 0);
390 return (USB_ERR_INVAL);
394 return (USB_ERR_INVAL);
396 if (id->bInterfaceClass != UICLASS_MASS) {
397 return (USB_ERR_INVAL);
399 if (u3g_lookup_huawei(uaa)) {
400 /* no device match */
401 return (USB_ERR_INVAL);
403 flags = USB_GET_DRIVER_INFO(uaa);
405 if (flags & U3GFL_HUAWEI_INIT) {
406 u3g_huawei_init(udev);
407 } else if (flags & U3GFL_SCSI_EJECT) {
408 return (usb_test_autoinstall(udev, 0, 1));
409 } else if (flags & U3GFL_SIERRA_INIT) {
410 u3g_sierra_init(udev);
413 return (USB_ERR_INVAL);
415 return (0); /* success */
419 u3g_driver_loaded(struct module *mod, int what, void *arg)
423 /* register our autoinstall handler */
424 usb_test_huawei_autoinst_p = &u3g_test_huawei_autoinst;
427 usb_test_huawei_unload(NULL);
436 u3g_probe(device_t self)
438 struct usb_attach_arg *uaa = device_get_ivars(self);
440 if (uaa->usb_mode != USB_MODE_HOST) {
443 if (uaa->info.bConfigIndex != U3G_CONFIG_INDEX) {
446 if (uaa->info.bInterfaceClass != UICLASS_VENDOR) {
449 return (u3g_lookup_huawei(uaa));
453 u3g_attach(device_t dev)
455 struct usb_config u3g_config_tmp[U3G_N_TRANSFER];
456 struct usb_attach_arg *uaa = device_get_ivars(dev);
457 struct u3g_softc *sc = device_get_softc(dev);
458 struct usb_interface *iface;
459 struct usb_interface_descriptor *id;
460 uint32_t iface_valid;
461 int error, flags, nports;
465 DPRINTF("sc=%p\n", sc);
467 flags = USB_GET_DRIVER_INFO(uaa);
469 if (flags & U3GFL_SAEL_M460_INIT)
470 u3g_sael_m460_init(uaa->device);
472 /* copy in USB config */
473 for (n = 0; n != U3G_N_TRANSFER; n++)
474 u3g_config_tmp[n] = u3g_config[n];
476 device_set_usb_desc(dev);
477 mtx_init(&sc->sc_mtx, "u3g", NULL, MTX_DEF);
479 sc->sc_udev = uaa->device;
481 /* Claim all interfaces on the device */
483 for (i = uaa->info.bIfaceIndex; i < USB_IFACE_MAX; i++) {
484 iface = usbd_get_iface(uaa->device, i);
487 id = usbd_get_interface_descriptor(iface);
488 if (id == NULL || id->bInterfaceClass != UICLASS_VENDOR)
490 usbd_set_parent_iface(uaa->device, i, uaa->info.bIfaceIndex);
491 iface_valid |= (1<<i);
494 i = 0; /* interface index */
495 ep = 0; /* endpoint index */
496 nports = 0; /* number of ports */
497 while (i < USB_IFACE_MAX) {
498 if ((iface_valid & (1<<i)) == 0) {
503 /* update BULK endpoint index */
504 for (n = 0; n < U3G_N_TRANSFER; n++)
505 u3g_config_tmp[n].ep_index = ep;
507 /* try to allocate a set of BULK endpoints */
508 error = usbd_transfer_setup(uaa->device, &i,
509 sc->sc_xfer[nports], u3g_config_tmp, U3G_N_TRANSFER,
510 &sc->sc_ucom[nports], &sc->sc_mtx);
518 /* set stall by default */
519 mtx_lock(&sc->sc_mtx);
520 usbd_xfer_set_stall(sc->sc_xfer[nports][U3G_BULK_WR]);
521 usbd_xfer_set_stall(sc->sc_xfer[nports][U3G_BULK_RD]);
522 mtx_unlock(&sc->sc_mtx);
524 nports++; /* found one port */
526 if (nports == U3G_MAXPORTS)
530 device_printf(dev, "no ports found\n");
533 sc->sc_numports = nports;
535 error = ucom_attach(&sc->sc_super_ucom, sc->sc_ucom,
536 sc->sc_numports, sc, &u3g_callback, &sc->sc_mtx);
538 DPRINTF("ucom_attach failed\n");
541 if (sc->sc_numports > 1)
542 device_printf(dev, "Found %u ports.\n", sc->sc_numports);
551 u3g_detach(device_t dev)
553 struct u3g_softc *sc = device_get_softc(dev);
556 DPRINTF("sc=%p\n", sc);
558 /* NOTE: It is not dangerous to detach more ports than attached! */
559 ucom_detach(&sc->sc_super_ucom, sc->sc_ucom, U3G_MAXPORTS);
561 for (m = 0; m != U3G_MAXPORTS; m++)
562 usbd_transfer_unsetup(sc->sc_xfer[m], U3G_N_TRANSFER);
563 mtx_destroy(&sc->sc_mtx);
569 u3g_start_read(struct ucom_softc *ucom)
571 struct u3g_softc *sc = ucom->sc_parent;
573 /* start read endpoint */
574 usbd_transfer_start(sc->sc_xfer[ucom->sc_local_unit][U3G_BULK_RD]);
579 u3g_stop_read(struct ucom_softc *ucom)
581 struct u3g_softc *sc = ucom->sc_parent;
583 /* stop read endpoint */
584 usbd_transfer_stop(sc->sc_xfer[ucom->sc_local_unit][U3G_BULK_RD]);
589 u3g_start_write(struct ucom_softc *ucom)
591 struct u3g_softc *sc = ucom->sc_parent;
593 usbd_transfer_start(sc->sc_xfer[ucom->sc_local_unit][U3G_BULK_WR]);
598 u3g_stop_write(struct ucom_softc *ucom)
600 struct u3g_softc *sc = ucom->sc_parent;
602 usbd_transfer_stop(sc->sc_xfer[ucom->sc_local_unit][U3G_BULK_WR]);
607 u3g_write_callback(struct usb_xfer *xfer, usb_error_t error)
609 struct ucom_softc *ucom = usbd_xfer_softc(xfer);
610 struct usb_page_cache *pc;
613 switch (USB_GET_STATE(xfer)) {
614 case USB_ST_TRANSFERRED:
617 pc = usbd_xfer_get_frame(xfer, 0);
618 if (ucom_get_data(ucom, pc, 0, U3G_BSIZE, &actlen)) {
619 usbd_xfer_set_frame_len(xfer, 0, actlen);
620 usbd_transfer_submit(xfer);
625 if (error != USB_ERR_CANCELLED) {
626 /* do a builtin clear-stall */
627 usbd_xfer_set_stall(xfer);
636 u3g_read_callback(struct usb_xfer *xfer, usb_error_t error)
638 struct ucom_softc *ucom = usbd_xfer_softc(xfer);
639 struct usb_page_cache *pc;
642 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
644 switch (USB_GET_STATE(xfer)) {
645 case USB_ST_TRANSFERRED:
646 pc = usbd_xfer_get_frame(xfer, 0);
647 ucom_put_data(ucom, pc, 0, actlen);
651 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
652 usbd_transfer_submit(xfer);
656 if (error != USB_ERR_CANCELLED) {
657 /* do a builtin clear-stall */
658 usbd_xfer_set_stall(xfer);