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.
24 * - The detour through the tty layer is ridiculously expensive wrt buffering
25 * due to the high speeds.
26 * We should consider adding a simple r/w device which allows attaching of PPP
27 * in a more efficient way.
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/kernel.h>
33 #include <sys/malloc.h>
34 #include <sys/module.h>
36 #include <sys/ioccom.h>
37 #include <sys/fcntl.h>
41 #include <sys/selinfo.h>
43 #include <dev/usb/usb.h>
44 #include <dev/usb/usbdi.h>
45 #include <dev/usb/usbdi_util.h>
47 #include <dev/usb/ucomvar.h>
49 #if __FreeBSD_version >= 800000
56 #define DPRINTF(x...) do { if (u3gdebug) device_printf(sc->sc_dev, ##x); } while (0)
59 #define DPRINTF(x...) /* nop */
62 #define U3G_MAXPORTS 4
63 #define U3G_CONFIG_INDEX 0
66 struct ucom_softc sc_ucom[U3G_MAXPORTS];
68 usbd_device_handle sc_udev;
74 static int u3g_open(void *addr, int portno);
75 static void u3g_close(void *addr, int portno);
77 struct ucom_callback u3g_callback = {
94 static const struct u3g_speeds_s u3g_speeds[] = {
103 #define U3GSP_HSDPA 4
105 #define U3GSP_HSUPA 5
111 #define U3GIBUFSIZE 1024
112 #define U3GOBUFSIZE 1024
115 * Various supported device vendors/products.
117 struct u3g_dev_type_s {
118 struct usb_devno devno;
121 #define U3GFL_NONE 0x00
122 #define U3GFL_HUAWEI_INIT 0x01 // Requires init command (Huawei cards)
123 #define U3GFL_SCSI_EJECT 0x02 // Requires SCSI eject command (Novatel)
124 #define U3GFL_SIERRA_INIT 0x04 // Requires init command (Sierra cards)
125 #define U3GFL_CMOTECH_INIT 0x08 // Requires init command (CMOTECH cards)
126 #define U3GFL_STUB_WAIT 0x80 // Device reappears after a short delay
129 // Note: The entries marked with XXX should be checked for the correct speed
130 // indication to set the buffer sizes.
131 static const struct u3g_dev_type_s u3g_devs[] = {
133 {{ USB_VENDOR_OPTION, USB_PRODUCT_OPTION_GT3G }, U3GSP_UMTS, U3GFL_NONE },
134 {{ USB_VENDOR_OPTION, USB_PRODUCT_OPTION_GT3GQUAD }, U3GSP_UMTS, U3GFL_NONE },
135 {{ USB_VENDOR_OPTION, USB_PRODUCT_OPTION_GT3GPLUS }, U3GSP_UMTS, U3GFL_NONE },
136 {{ USB_VENDOR_OPTION, USB_PRODUCT_OPTION_GTMAX36 }, U3GSP_HSDPA, U3GFL_NONE },
137 {{ USB_VENDOR_OPTION, USB_PRODUCT_OPTION_GTMAXHSUPA }, U3GSP_HSDPA, U3GFL_NONE },
138 {{ USB_VENDOR_OPTION, USB_PRODUCT_OPTION_VODAFONEMC3G }, U3GSP_UMTS, U3GFL_NONE },
139 /* OEM: Qualcomm, Inc. */
140 {{ USB_VENDOR_QUALCOMMINC, USB_PRODUCT_QUALCOMMINC_ZTE_STOR }, U3GSP_CDMA, U3GFL_SCSI_EJECT },
141 {{ USB_VENDOR_QUALCOMMINC, USB_PRODUCT_QUALCOMMINC_CDMA_MSM }, U3GSP_CDMA, U3GFL_SCSI_EJECT },
143 {{ USB_VENDOR_HUAWEI, USB_PRODUCT_HUAWEI_MOBILE }, U3GSP_HSDPA, U3GFL_HUAWEI_INIT },
144 {{ USB_VENDOR_HUAWEI, USB_PRODUCT_HUAWEI_E220 }, U3GSP_HSPA, U3GFL_HUAWEI_INIT },
146 {{ USB_VENDOR_NOVATEL, USB_PRODUCT_NOVATEL_CDMA_MODEM }, U3GSP_CDMA, U3GFL_SCSI_EJECT },
147 {{ USB_VENDOR_NOVATEL, USB_PRODUCT_NOVATEL_ES620 }, U3GSP_UMTS, U3GFL_SCSI_EJECT }, // XXX
148 {{ USB_VENDOR_NOVATEL, USB_PRODUCT_NOVATEL_MC950D }, U3GSP_HSUPA, U3GFL_SCSI_EJECT },
149 {{ USB_VENDOR_NOVATEL, USB_PRODUCT_NOVATEL_U720 }, U3GSP_UMTS, U3GFL_SCSI_EJECT }, // XXX
150 {{ USB_VENDOR_NOVATEL, USB_PRODUCT_NOVATEL_U727 }, U3GSP_UMTS, U3GFL_SCSI_EJECT }, // XXX
151 {{ USB_VENDOR_NOVATEL, USB_PRODUCT_NOVATEL_U740 }, U3GSP_HSDPA, U3GFL_SCSI_EJECT },
152 {{ USB_VENDOR_NOVATEL, USB_PRODUCT_NOVATEL_U740_2 }, U3GSP_HSDPA, U3GFL_SCSI_EJECT },
153 {{ USB_VENDOR_NOVATEL, USB_PRODUCT_NOVATEL_U870 }, U3GSP_UMTS, U3GFL_SCSI_EJECT }, // XXX
154 {{ USB_VENDOR_NOVATEL, USB_PRODUCT_NOVATEL_V620 }, U3GSP_UMTS, U3GFL_SCSI_EJECT }, // XXX
155 {{ USB_VENDOR_NOVATEL, USB_PRODUCT_NOVATEL_V640 }, U3GSP_UMTS, U3GFL_SCSI_EJECT }, // XXX
156 {{ USB_VENDOR_NOVATEL, USB_PRODUCT_NOVATEL_V720 }, U3GSP_UMTS, U3GFL_SCSI_EJECT }, // XXX
157 {{ USB_VENDOR_NOVATEL, USB_PRODUCT_NOVATEL_V740 }, U3GSP_HSDPA, U3GFL_SCSI_EJECT },
158 {{ USB_VENDOR_NOVATEL, USB_PRODUCT_NOVATEL_X950D }, U3GSP_HSUPA, U3GFL_SCSI_EJECT },
159 {{ USB_VENDOR_NOVATEL, USB_PRODUCT_NOVATEL_XU870 }, U3GSP_HSDPA, U3GFL_SCSI_EJECT },
160 {{ USB_VENDOR_NOVATEL, USB_PRODUCT_NOVATEL_ZEROCD }, U3GSP_HSUPA, U3GFL_SCSI_EJECT },
161 {{ USB_VENDOR_DELL, USB_PRODUCT_DELL_U740 }, U3GSP_HSDPA, U3GFL_SCSI_EJECT },
163 {{ USB_VENDOR_MERLIN, USB_PRODUCT_MERLIN_V620 }, U3GSP_UMTS, U3GFL_NONE }, // XXX
164 /* OEM: Sierra Wireless: */
165 {{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AIRCARD580 }, U3GSP_UMTS, U3GFL_NONE }, // XXX
166 {{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AIRCARD595 }, U3GSP_UMTS, U3GFL_NONE }, // XXX
167 {{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AC595U }, U3GSP_UMTS, U3GFL_NONE }, // XXX
168 {{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AC597E }, U3GSP_UMTS, U3GFL_NONE }, // XXX
169 {{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_C597 }, U3GSP_UMTS, U3GFL_NONE }, // XXX
170 {{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AC880 }, U3GSP_UMTS, U3GFL_NONE }, // XXX
171 {{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AC880E }, U3GSP_UMTS, U3GFL_NONE }, // XXX
172 {{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AC880U }, U3GSP_UMTS, U3GFL_NONE }, // XXX
173 {{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AC881 }, U3GSP_UMTS, U3GFL_NONE }, // XXX
174 {{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AC881E }, U3GSP_UMTS, U3GFL_NONE }, // XXX
175 {{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AC881U }, U3GSP_UMTS, U3GFL_NONE }, // XXX
176 {{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_EM5625 }, U3GSP_UMTS, U3GFL_NONE }, // XXX
177 {{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC5720 }, U3GSP_UMTS, U3GFL_NONE }, // XXX
178 {{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC5720_2 }, U3GSP_UMTS, U3GFL_NONE }, // XXX
179 {{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC5725 }, U3GSP_UMTS, U3GFL_NONE }, // XXX
180 {{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MINI5725 }, U3GSP_UMTS, U3GFL_NONE }, // XXX
181 {{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AIRCARD875 }, U3GSP_UMTS, U3GFL_NONE }, // XXX
182 {{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC8755 }, U3GSP_UMTS, U3GFL_NONE }, // XXX
183 {{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC8755_2 }, U3GSP_UMTS, U3GFL_NONE }, // XXX
184 {{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC8755_3 }, U3GSP_UMTS, U3GFL_NONE }, // XXX
185 {{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC8765 }, U3GSP_UMTS, U3GFL_NONE }, // XXX
186 {{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AC875U }, U3GSP_UMTS, U3GFL_NONE }, // XXX
187 {{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC8775_2 }, U3GSP_HSDPA, U3GFL_NONE }, // XXX
188 {{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC8780 }, U3GSP_UMTS, U3GFL_NONE }, // XXX
189 {{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC8781 }, U3GSP_UMTS, U3GFL_NONE }, // XXX
190 {{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_TRUINSTALL }, U3GSP_UMTS, U3GFL_SIERRA_INIT },
191 {{ USB_VENDOR_HP, USB_PRODUCT_HP_HS2300 }, U3GSP_HSDPA, U3GFL_NONE },
193 {{ USB_VENDOR_CMOTECH, USB_PRODUCT_CMOTECH_CGU628 }, U3GSP_HSDPA, U3GFL_CMOTECH_INIT },
194 {{ USB_VENDOR_CMOTECH, USB_PRODUCT_CMOTECH_DISK }, U3GSP_HSDPA, U3GFL_NONE },
196 #define u3g_lookup(v, p) ((const struct u3g_dev_type_s *)usb_lookup(u3g_devs, v, p))
199 u3g_match(device_t self)
201 struct usb_attach_arg *uaa = device_get_ivars(self);
202 const struct u3g_dev_type_s *u3g_dev_type;
207 u3g_dev_type = u3g_lookup(uaa->vendor, uaa->product);
211 if (u3g_dev_type->flags&U3GFL_HUAWEI_INIT) {
212 /* If the interface class of the first interface is no longer
213 * mass storage the card has changed to modem (see u3g_attach()
216 usb_interface_descriptor_t *id;
217 id = usbd_get_interface_descriptor(uaa->iface);
218 if (!id || id->bInterfaceClass == UICLASS_MASS)
222 return UMATCH_VENDOR_PRODUCT_CONF_IFACE;
226 u3g_attach(device_t self)
228 struct u3g_softc *sc = device_get_softc(self);
229 struct usb_attach_arg *uaa = device_get_ivars(self);
230 const struct u3g_dev_type_s *u3g_dev_type;
231 usbd_device_handle dev = uaa->device;
232 usb_interface_descriptor_t *id;
233 usb_endpoint_descriptor_t *ed;
235 usb_config_descriptor_t *cd;
238 #if __FreeBSD_version < 700000
239 char *devinfo = malloc(1024, M_USBDEV, M_WAITOK);
240 usbd_devinfo(dev, 0, devinfo);
241 device_printf(self, "%s\n", devinfo);
242 free(devinfo, M_USBDEV);
245 /* get the config descriptor */
246 cd = usbd_get_config_descriptor(dev);
248 device_printf(self, "failed to get configuration descriptor\n");
255 u3g_dev_type = u3g_lookup(uaa->vendor, uaa->product);
256 sc->sc_flags = u3g_dev_type->flags;
257 sc->sc_speed = u3g_dev_type->speed;
259 sprintf(devnamefmt,"U%d.%%d", device_get_unit(self));
261 for (i = 0; i < uaa->nifaces && portno < U3G_MAXPORTS; i++) {
262 if (uaa->ifaces[i] == NULL)
265 id = usbd_get_interface_descriptor(uaa->ifaces[i]);
266 if (id && id->bInterfaceClass == UICLASS_MASS) {
267 /* We attach to the interface instead of the device as
268 * some devices have a built-in SD card reader.
269 * Claim the first umass device (cdX) as it contains
270 * only Windows drivers anyway (CD-ROM), hiding it.
274 if (uaa->vendor == USB_VENDOR_HUAWEI)
275 if (id->bInterfaceNumber == 2)
276 uaa->ifaces[i] = NULL;
281 int bulkin_no = -1, bulkout_no = -1;
283 for (n = 0; n < id->bNumEndpoints && portno < U3G_MAXPORTS; n++) {
284 ed = usbd_interface2endpoint_descriptor(uaa->ifaces[i], n);
287 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN
288 && UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK)
289 bulkin_no = ed->bEndpointAddress;
290 else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT
291 && UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK)
292 bulkout_no = ed->bEndpointAddress;
294 /* If we have found a pair of bulk-in/-out endpoints
295 * create a serial port for it. Note: We assume that
296 * the bulk-in and bulk-out endpoints appear in pairs.
298 if (bulkin_no != -1 && bulkout_no != -1) {
299 struct ucom_softc *ucom = &sc->sc_ucom[portno];
303 ucom->sc_iface = uaa->ifaces[i];
304 ucom->sc_bulkin_no = bulkin_no;
305 ucom->sc_bulkout_no = bulkout_no;
306 ucom->sc_ibufsize = U3GIBUFSIZE;
307 ucom->sc_ibufsizepad = U3GIBUFSIZE;
308 ucom->sc_obufsize = U3GOBUFSIZE;
309 ucom->sc_opkthdrlen = 0;
311 ucom->sc_callback = &u3g_callback;
312 ucom->sc_parent = sc;
313 ucom->sc_portno = portno;
315 DPRINTF("port=%d iface=%d in=0x%x out=0x%x\n",
318 ucom->sc_bulkout_no);
319 #if __FreeBSD_version < 700000
320 ucom_attach_tty(ucom, MINOR_CALLOUT, devnamefmt, portno);
321 #elif __FreeBSD_version < 800000
322 ucom_attach_tty(ucom, TS_CALLOUT, devnamefmt, portno);
324 ucom_attach_tty(ucom, devnamefmt, portno);
329 bulkin_no = bulkout_no = -1;
333 uaa->ifaces[i] = NULL; // claim the interface
335 sc->sc_numports = portno;
337 device_printf(self, "configured %d serial ports (%s)\n",
338 sc->sc_numports, devnamefmt);
343 u3g_detach(device_t self)
345 struct u3g_softc *sc = device_get_softc(self);
349 for (i = 0; i < sc->sc_numports; i++) {
350 sc->sc_ucom[i].sc_dying = 1;
351 rv = ucom_detach(&sc->sc_ucom[i]);
353 device_printf(self, "ucom_detach(U%d.%d\n", device_get_unit(self), i);
362 u3g_open(void *addr, int portno)
364 #if __FreeBSD_version < 800000
365 /* Supply generous buffering for these cards to avoid disappointments
366 * when setting the speed incorrectly. Only do this for the first port
367 * assuming that the rest of the ports are used for diagnostics only
369 * Note: We abuse the fact that ucom sets the speed through
370 * ispeed/ospeed, not through ispeedwat/ospeedwat.
373 struct u3g_softc *sc = addr;
374 struct ucom_softc *ucom = &sc->sc_ucom[portno];
375 struct tty *tp = ucom->sc_tty;
377 tp->t_ispeedwat = u3g_speeds[sc->sc_speed].ispeed;
378 tp->t_ospeedwat = u3g_speeds[sc->sc_speed].ospeed;
380 /* Avoid excessive buffer sizes.
381 * XXX The values here should be checked. Lower them and see
382 * whether 'lost chars' messages appear.
384 if (tp->t_ispeedwat > 384000)
385 tp->t_ispeedwat = 384000;
386 if (tp->t_ospeedwat > 384000)
387 tp->t_ospeedwat = 384000;
397 u3g_close(void *addr, int portno)
399 #if __FreeBSD_version < 800000
400 if (portno == 0) { /* see u3g_open() */
401 /* Reduce the buffers allocated above again */
402 struct u3g_softc *sc = addr;
403 struct ucom_softc *ucom = &sc->sc_ucom[portno];
404 struct tty *tp = ucom->sc_tty;
406 device_t self = sc->sc_dev;
409 tp->t_ispeedwat = (speed_t)-1;
410 tp->t_ospeedwat = (speed_t)-1;
417 static device_method_t u3g_methods[] = {
418 /* Device interface */
419 DEVMETHOD(device_probe, u3g_match),
420 DEVMETHOD(device_attach, u3g_attach),
421 DEVMETHOD(device_detach, u3g_detach),
426 static driver_t u3g_driver = {
429 sizeof (struct u3g_softc)
432 DRIVER_MODULE(u3g, uhub, u3g_driver, ucom_devclass, usbd_driver_load, 0);
433 MODULE_DEPEND(u3g, usb, 1, 1, 1);
434 MODULE_DEPEND(u3g, ucom, UCOM_MINVER, UCOM_PREFVER, UCOM_MAXVER);
435 MODULE_VERSION(u3g, 1);
437 /*******************************************************************
438 ****** Stub driver to hide devices that need to reinitialise ******
439 *******************************************************************/
441 struct u3gstub_softc {
443 usbd_device_handle sc_udev;
444 usbd_pipe_handle sc_pipe_out, sc_pipe_in;
445 usbd_xfer_handle sc_xfer;
449 u3gstub_huawei_init(struct u3gstub_softc *sc, struct usb_attach_arg *uaa)
451 usb_device_request_t req;
453 req.bmRequestType = UT_WRITE_DEVICE;
454 req.bRequest = UR_SET_FEATURE;
455 USETW(req.wValue, UF_DEVICE_REMOTE_WAKEUP);
456 USETW(req.wIndex, UHF_PORT_SUSPEND);
457 USETW(req.wLength, 0);
459 (void) usbd_do_request(uaa->device, &req, 0); /* ignore any error */
465 u3gstub_BBB_cb(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status err)
467 struct u3gstub_softc *sc = (struct u3gstub_softc *) priv;
468 unsigned char cmd[13];
471 device_printf(sc->sc_dev,
472 "Failed to send CD eject command to "
473 "change to modem mode\n");
475 usbd_setup_xfer(sc->sc_xfer, sc->sc_pipe_in, NULL, cmd, sizeof(cmd),
476 0, USBD_DEFAULT_TIMEOUT, NULL);
477 int err = usbd_transfer(sc->sc_xfer) != USBD_NORMAL_COMPLETION;
478 if (err != USBD_NORMAL_COMPLETION && err != USBD_IN_PROGRESS)
479 DPRINTF("failed to start transfer (CSW)\n");
484 u3gstub_scsi_eject(struct u3gstub_softc *sc, struct usb_attach_arg *uaa)
486 /* See definition of umass_bbb_cbw_t in sys/dev/usb/umass.c and struct
487 * scsi_start_stop_unit in sys/cam/scsi/scsi_all.h .
489 unsigned char cmd[31] = {
490 0x55, 0x53, 0x42, 0x43, /* 0..3: Command Block Wrapper (CBW) signature */
491 0x01, 0x00, 0x00, 0x00, /* 4..7: CBW Tag, unique 32-bit number */
492 0x00, 0x00, 0x00, 0x00, /* 8..11: CBW Transfer Length, no data here */
493 0x00, /* 12: CBW Flag: output */
494 0x00, /* 13: CBW Lun */
495 0x06, /* 14: CBW Length */
497 0x1b, /* 15+0: opcode: SCSI START/STOP */
498 0x00, /* 15+1: byte2: Not immediate */
499 0x00, 0x00, /* 15+2..3: reserved */
500 0x02, /* 15+4: Load/Eject command */
501 0x00, /* 15+5: control */
502 0x00, 0x00, 0x00, 0x00, /* 15+6..15: unused */
503 0x00, 0x00, 0x00, 0x00,
507 usb_interface_descriptor_t *id;
508 usb_endpoint_descriptor_t *ed = NULL;
512 /* Find the bulk-out endpoints */
513 id = usbd_get_interface_descriptor(uaa->iface);
514 for (i = 0 ; i < id->bNumEndpoints; i++) {
515 ed = usbd_interface2endpoint_descriptor(uaa->iface, i);
517 && (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
518 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT) {
519 if (usbd_open_pipe(uaa->iface,
520 ed->bEndpointAddress,
523 != USBD_NORMAL_COMPLETION) {
524 DPRINTF("failed to open bulk-out pipe on endpoint %d\n",
525 ed->bEndpointAddress);
529 if (usbd_open_pipe(uaa->iface,
530 ed->bEndpointAddress,
533 != USBD_NORMAL_COMPLETION) {
534 DPRINTF("failed to open bulk-in pipe on endpoint %d\n",
535 ed->bEndpointAddress);
540 if (sc->sc_pipe_out && sc->sc_pipe_in)
544 if (i == id->bNumEndpoints) {
545 DPRINTF("failed to find bulk-out pipe\n");
549 sc->sc_xfer = usbd_alloc_xfer(uaa->device);
550 if (sc->sc_xfer == NULL) {
551 DPRINTF("failed to allocate xfer\n");
555 usbd_setup_xfer(sc->sc_xfer, sc->sc_pipe_out, NULL, cmd, sizeof(cmd),
556 0, USBD_DEFAULT_TIMEOUT, u3gstub_BBB_cb);
557 int err = usbd_transfer(sc->sc_xfer) != USBD_NORMAL_COMPLETION;
558 if (err != USBD_NORMAL_COMPLETION && err != USBD_IN_PROGRESS) {
559 DPRINTF("failed to start transfer (CBW)\n");
567 u3gstub_cmotech_init(struct u3gstub_softc *sc, struct usb_attach_arg *uaa)
569 /* See definition of umass_bbb_cbw_t in sys/dev/usb/umass.c
570 * in sys/cam/scsi/scsi_all.h .
572 unsigned char cmd[31] = {
573 0x55, 0x53, 0x42, 0x43, /* 0..3: Command Block Wrapper (CBW) signature */
574 0x01, 0x00, 0x00, 0x00, /* 4..7: CBW Tag, unique 32-bit number */
575 0x00, 0x00, 0x00, 0x00, /* 8..11: CBW Transfer Length, no data here */
576 0x80, /* 12: CBW Flag: output, so 0 */
577 0x00, /* 13: CBW Lun */
578 0x08, /* 14: CBW Length */
588 0x00, 0x00, 0x00, 0x00, /* 15+8..15: unused */
589 0x00, 0x00, 0x00, 0x00
592 usb_interface_descriptor_t *id;
593 usb_endpoint_descriptor_t *ed = NULL;
597 /* Find the bulk-out endpoints */
598 id = usbd_get_interface_descriptor(uaa->iface);
599 for (i = 0 ; i < id->bNumEndpoints ; i++) {
600 ed = usbd_interface2endpoint_descriptor(uaa->iface, i);
602 && (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
603 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT) {
604 if (usbd_open_pipe(uaa->iface,
605 ed->bEndpointAddress,
608 != USBD_NORMAL_COMPLETION) {
609 DPRINTF("failed to open bulk-out pipe on endpoint %d\n",
610 ed->bEndpointAddress);
614 if (usbd_open_pipe(uaa->iface,
615 ed->bEndpointAddress,
618 != USBD_NORMAL_COMPLETION) {
619 DPRINTF("failed to open bulk-in pipe on endpoint %d\n",
620 ed->bEndpointAddress);
625 if (sc->sc_pipe_out && sc->sc_pipe_in)
629 if (i == id->bNumEndpoints) {
630 DPRINTF("failed to find bulk-out pipe\n");
634 sc->sc_xfer = usbd_alloc_xfer(uaa->device);
635 if (sc->sc_xfer == NULL) {
636 DPRINTF("failed to allocate xfer\n");
640 usbd_setup_xfer(sc->sc_xfer, sc->sc_pipe_out, NULL, cmd, sizeof(cmd),
641 0, USBD_DEFAULT_TIMEOUT, u3gstub_BBB_cb);
642 int err = usbd_transfer(sc->sc_xfer) != USBD_NORMAL_COMPLETION;
643 if (err != USBD_NORMAL_COMPLETION && err != USBD_IN_PROGRESS) {
644 DPRINTF("failed to start transfer (CBW)\n");
652 u3gstub_sierra_init(struct u3gstub_softc *sc, struct usb_attach_arg *uaa)
654 usb_device_request_t req;
656 req.bmRequestType = UT_VENDOR;
657 req.bRequest = UR_SET_INTERFACE;
658 USETW(req.wValue, UF_DEVICE_REMOTE_WAKEUP);
659 USETW(req.wIndex, UHF_PORT_CONNECTION);
660 USETW(req.wLength, 0);
662 (void) usbd_do_request(uaa->device, &req, 0); /* ignore any error */
668 u3gstub_match(device_t self)
670 struct usb_attach_arg *uaa = device_get_ivars(self);
671 const struct u3g_dev_type_s *u3g_dev_type;
672 usb_interface_descriptor_t *id;
674 /* This stub handles 3G modem devices (E220, Mobile, etc.) with
675 * auto-install flash disks for Windows/MacOSX on the first interface.
676 * After some command or some delay they change appearance to a modem.
682 u3g_dev_type = u3g_lookup(uaa->vendor, uaa->product);
686 if (u3g_dev_type->flags&U3GFL_HUAWEI_INIT
687 || u3g_dev_type->flags&U3GFL_SCSI_EJECT
688 || u3g_dev_type->flags&U3GFL_SIERRA_INIT
689 || u3g_dev_type->flags&U3GFL_CMOTECH_INIT
690 || u3g_dev_type->flags&U3GFL_STUB_WAIT) {
691 /* We assume that if the first interface is still a mass
692 * storage device the device has not yet changed appearance.
694 id = usbd_get_interface_descriptor(uaa->iface);
695 if (id && id->bInterfaceNumber == 0
696 && id->bInterfaceClass == UICLASS_MASS) {
702 return UMATCH_VENDOR_PRODUCT;
710 u3gstub_attach(device_t self)
712 struct u3gstub_softc *sc = device_get_softc(self);
713 struct usb_attach_arg *uaa = device_get_ivars(self);
714 const struct u3g_dev_type_s *u3g_dev_type;
723 sc->sc_udev = uaa->device;
725 for (i = 0; i < uaa->nifaces; i++)
726 uaa->ifaces[i] = NULL; // claim all interfaces
728 u3g_dev_type = u3g_lookup(uaa->vendor, uaa->product);
729 if (u3g_dev_type->flags&U3GFL_HUAWEI_INIT) {
731 device_printf(sc->sc_dev,
732 "changing Huawei modem to modem mode\n");
733 if (!u3gstub_huawei_init(sc, uaa))
735 } else if (u3g_dev_type->flags&U3GFL_SCSI_EJECT) {
737 device_printf(sc->sc_dev, "sending CD eject command to "
738 "change to modem mode\n");
739 if (!u3gstub_scsi_eject(sc, uaa))
741 } else if (u3g_dev_type->flags&U3GFL_SIERRA_INIT) {
743 device_printf(sc->sc_dev,
744 "changing Sierra modem to modem mode\n");
745 if (!u3gstub_sierra_init(sc, uaa))
747 } else if (u3g_dev_type->flags&U3GFL_CMOTECH_INIT) {
749 device_printf(sc->sc_dev,
750 "changing CMOTECH modem to modem mode\n");
751 if (!u3gstub_cmotech_init(sc, uaa))
753 } else if (u3g_dev_type->flags&U3GFL_STUB_WAIT) {
755 device_printf(sc->sc_dev, "waiting for modem to change "
764 u3gstub_detach(device_t self)
766 struct u3gstub_softc *sc = device_get_softc(self);
769 usbd_free_xfer(sc->sc_xfer);
771 if (sc->sc_pipe_in) {
772 usbd_abort_pipe(sc->sc_pipe_in);
773 usbd_close_pipe(sc->sc_pipe_in);
775 if (sc->sc_pipe_out) {
776 usbd_abort_pipe(sc->sc_pipe_out);
777 usbd_close_pipe(sc->sc_pipe_out);
783 static device_method_t u3gstub_methods[] = {
784 /* Device interface */
785 DEVMETHOD(device_probe, u3gstub_match),
786 DEVMETHOD(device_attach, u3gstub_attach),
787 DEVMETHOD(device_detach, u3gstub_detach),
792 static driver_t u3gstub_driver = {
795 sizeof (struct u3gstub_softc)
798 DRIVER_MODULE(u3gstub, uhub, u3gstub_driver, ucom_devclass, usbd_driver_load, 0);
799 MODULE_DEPEND(u3gstub, usb, 1, 1, 1);