Home | History | Annotate | Line # | Download | only in usb
u3g.c revision 1.31.2.8
      1 /*	$NetBSD: u3g.c,v 1.31.2.8 2015/03/19 17:26:43 skrll Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2009 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     19  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     20  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     21  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     22  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     28  * POSSIBILITY OF SUCH DAMAGE.
     29  */
     30 
     31 /*
     32  * Copyright (c) 2008 AnyWi Technologies
     33  *   Author: Andrea Guzzo <aguzzo (at) anywi.com>
     34  *   * based on uark.c 1.1 2006/08/14 08:30:22 jsg *
     35  *   * parts from ubsa.c 183348 2008-09-25 12:00:56Z phk *
     36  *
     37  * Permission to use, copy, modify, and distribute this software for any
     38  * purpose with or without fee is hereby granted, provided that the above
     39  * copyright notice and this permission notice appear in all copies.
     40  *
     41  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
     42  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     43  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
     44  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     45  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     46  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
     47  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     48  *
     49  * $FreeBSD$
     50  */
     51 
     52 #include <sys/cdefs.h>
     53 __KERNEL_RCSID(0, "$NetBSD: u3g.c,v 1.31.2.8 2015/03/19 17:26:43 skrll Exp $");
     54 
     55 #include <sys/param.h>
     56 #include <sys/systm.h>
     57 #include <sys/kernel.h>
     58 #include <sys/kmem.h>
     59 #include <sys/bus.h>
     60 #include <sys/conf.h>
     61 #include <sys/tty.h>
     62 
     63 #include <dev/usb/usb.h>
     64 #include <dev/usb/usbdi.h>
     65 #include <dev/usb/usbdivar.h>
     66 #include <dev/usb/usbdi_util.h>
     67 
     68 #include <dev/usb/ucomvar.h>
     69 
     70 #include "usbdevs.h"
     71 
     72 /*
     73  * We read/write data from/to the device in 4KB chunks to maximise
     74  * performance.
     75  */
     76 #define U3G_BUFF_SIZE	4096
     77 
     78 /*
     79  * Some 3G devices (the Huawei E160/E220 springs to mind here) buffer up
     80  * data internally even when the USB pipes are closed. So on first open,
     81  * we can receive a large chunk of stale data.
     82  *
     83  * This causes a real problem because the default TTYDEF_LFLAG (applied
     84  * on first open) has the ECHO flag set, resulting in all the stale data
     85  * being echoed straight back to the device by the tty(4) layer. Some
     86  * devices (again, the Huawei E160/E220 for example) react to this spew
     87  * by going catatonic.
     88  *
     89  * All this happens before the application gets a chance to disable ECHO.
     90  *
     91  * We work around this by ignoring all data received from the device for
     92  * a period of two seconds, or until the application starts sending data -
     93  * whichever comes first.
     94  */
     95 #define	U3G_PURGE_SECS	2
     96 
     97 /*
     98  * Define bits for the virtual modem control pins.
     99  * The input pin states are reported via the interrupt pipe on some devices.
    100  */
    101 #define	U3G_OUTPIN_DTR	(1u << 0)
    102 #define	U3G_OUTPIN_RTS	(1u << 1)
    103 #define	U3G_INPIN_DCD	(1u << 0)
    104 #define	U3G_INPIN_DSR	(1u << 1)
    105 #define	U3G_INPIN_RI	(1u << 3)
    106 
    107 /*
    108  * USB request to set the output pin status
    109  */
    110 #define	U3G_SET_PIN	0x22
    111 
    112 struct u3g_softc {
    113 	device_t		sc_dev;
    114 	struct usbd_device *	sc_udev;
    115 	bool			sc_dying;	/* We're going away */
    116 	int			sc_ifaceno;	/* Device interface number */
    117 
    118 	struct u3g_com {
    119 		device_t	c_dev;		/* Child ucom(4) handle */
    120 
    121 		bool		c_open;		/* Device is in use */
    122 		bool		c_purging;	/* Purging stale data */
    123 		struct timeval	c_purge_start;	/* Control duration of purge */
    124 
    125 		u_char		c_msr;		/* Emulated 'msr' */
    126 		uint16_t	c_outpins;	/* Output pin state */
    127 	} sc_com[10];
    128 	size_t			sc_ncom;
    129 
    130 	struct usbd_pipe *	sc_intr_pipe;	/* Interrupt pipe */
    131 	u_char			*sc_intr_buff;	/* Interrupt buffer */
    132 	size_t			sc_intr_size;	/* buffer size */
    133 };
    134 
    135 /*
    136  * The device driver has two personalities. The first uses the 'usbdevif'
    137  * interface attribute so that a match will claim the entire USB device
    138  * for itself. This is used for when a device needs to be mode-switched
    139  * and ensures any other interfaces present cannot be claimed by other
    140  * drivers while the mode-switch is in progress.
    141  *
    142  * The second personality uses the 'usbifif' interface attribute so that
    143  * it can claim the 3G modem interfaces for itself, leaving others (such
    144  * as the mass storage interfaces on some devices) for other drivers.
    145  */
    146 static int u3ginit_match(device_t, cfdata_t, void *);
    147 static void u3ginit_attach(device_t, device_t, void *);
    148 static int u3ginit_detach(device_t, int);
    149 
    150 CFATTACH_DECL2_NEW(u3ginit, 0, u3ginit_match,
    151     u3ginit_attach, u3ginit_detach, NULL, NULL, NULL);
    152 
    153 
    154 static int u3g_match(device_t, cfdata_t, void *);
    155 static void u3g_attach(device_t, device_t, void *);
    156 static int u3g_detach(device_t, int);
    157 static int u3g_activate(device_t, enum devact);
    158 static void u3g_childdet(device_t, device_t);
    159 
    160 CFATTACH_DECL2_NEW(u3g, sizeof(struct u3g_softc), u3g_match,
    161     u3g_attach, u3g_detach, u3g_activate, NULL, u3g_childdet);
    162 
    163 
    164 static void u3g_intr(struct usbd_xfer *, void *, usbd_status);
    165 static void u3g_get_status(void *, int, u_char *, u_char *);
    166 static void u3g_set(void *, int, int, int);
    167 static int  u3g_open(void *, int);
    168 static void u3g_close(void *, int);
    169 static void u3g_read(void *, int, u_char **, uint32_t *);
    170 static void u3g_write(void *, int, u_char *, u_char *, uint32_t *);
    171 
    172 struct ucom_methods u3g_methods = {
    173 	.ucom_get_status = u3g_get_status,
    174 	.ucom_set = u3g_set,
    175 	.ucom_param = NULL,
    176 	.ucom_ioctl = NULL,
    177 	.ucom_open = u3g_open,
    178 	.ucom_close = u3g_close,
    179 	.ucom_read = u3g_read,
    180 	.ucom_write = u3g_write,
    181 };
    182 
    183 /*
    184  * Allegedly supported devices
    185  */
    186 static const struct usb_devno u3g_devs[] = {
    187 	{ USB_VENDOR_DELL, USB_PRODUCT_DELL_W5500 },
    188 	/* OEM: Huawei */
    189 	{ USB_VENDOR_HUAWEI, USB_PRODUCT_HUAWEI_E1750 },
    190 	{ USB_VENDOR_HUAWEI, USB_PRODUCT_HUAWEI_E1820 },
    191 	{ USB_VENDOR_HUAWEI, USB_PRODUCT_HUAWEI_E220 },
    192 	{ USB_VENDOR_HUAWEI, USB_PRODUCT_HUAWEI_EM770W },
    193 	{ USB_VENDOR_HUAWEI, USB_PRODUCT_HUAWEI_K3765 },
    194 	{ USB_VENDOR_HUAWEI, USB_PRODUCT_HUAWEI_MOBILE },
    195 	{ USB_VENDOR_HUAWEI, USB_PRODUCT_HUAWEI_E171 },
    196 	{ USB_VENDOR_HUAWEI, USB_PRODUCT_HUAWEI_E353 },
    197 	/* OEM: Merlin */
    198 	{ USB_VENDOR_MERLIN, USB_PRODUCT_MERLIN_V620 },
    199 	/* OEM: Novatel */
    200 	{ USB_VENDOR_NOVATEL2, USB_PRODUCT_NOVATEL2_ES620 },
    201 	{ USB_VENDOR_NOVATEL2, USB_PRODUCT_NOVATEL2_EU8X0D },
    202 	{ USB_VENDOR_NOVATEL2, USB_PRODUCT_NOVATEL2_MC950D },
    203 #if 0
    204 	/* These are matched in u3ginit_match() */
    205 	{ USB_VENDOR_NOVATEL2, USB_PRODUCT_NOVATEL2_MC950D_DRIVER },
    206 	{ USB_VENDOR_NOVATEL2, USB_PRODUCT_NOVATEL2_U760_DRIVER },
    207 #endif
    208 	{ USB_VENDOR_NOVATEL2, USB_PRODUCT_NOVATEL2_MERLINU740 },
    209 	{ USB_VENDOR_NOVATEL2, USB_PRODUCT_NOVATEL2_MERLINV620 },
    210 	{ USB_VENDOR_NOVATEL2, USB_PRODUCT_NOVATEL2_S720 },
    211 	{ USB_VENDOR_NOVATEL2, USB_PRODUCT_NOVATEL2_U720 },
    212 	{ USB_VENDOR_NOVATEL2, USB_PRODUCT_NOVATEL2_U727 },
    213 	{ USB_VENDOR_NOVATEL2, USB_PRODUCT_NOVATEL2_U740_2 },
    214 	{ USB_VENDOR_NOVATEL2, USB_PRODUCT_NOVATEL2_U760 },
    215 	{ USB_VENDOR_NOVATEL2, USB_PRODUCT_NOVATEL2_U870 },
    216 	{ USB_VENDOR_NOVATEL2, USB_PRODUCT_NOVATEL2_V740 },
    217 	{ USB_VENDOR_NOVATEL2, USB_PRODUCT_NOVATEL2_X950D },
    218 	{ USB_VENDOR_NOVATEL2, USB_PRODUCT_NOVATEL2_XU870 },
    219 	/* OEM: Option N.V. */
    220 	{ USB_VENDOR_OPTIONNV, USB_PRODUCT_OPTIONNV_QUADPLUSUMTS },
    221 	{ USB_VENDOR_OPTIONNV, USB_PRODUCT_OPTIONNV_HSDPA },
    222 	{ USB_VENDOR_OPTIONNV, USB_PRODUCT_OPTIONNV_GTMAXHSUPA },
    223 	/* OEM: Qualcomm, Inc. */
    224 	{ USB_VENDOR_QUALCOMM, USB_PRODUCT_QUALCOMM_NTT_DOCOMO_L02C_MODEM },
    225 
    226 	/* OEM: Sierra Wireless: */
    227 	{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AC595U },
    228 	{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AC597E },
    229 	{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AC875U },
    230 	{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AC880 },
    231 	{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AC880E },
    232 	{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AC880U },
    233 	{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AC881 },
    234 	{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AC881E },
    235 	{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AC881U },
    236 	{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AIRCARD580 },
    237 	{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AIRCARD595 },
    238 	{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AIRCARD875 },
    239 	{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_C597 },
    240 	{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_EM5625 },
    241 	{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC5720 },
    242 	{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC5720_2 },
    243 	{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC5725 },
    244 	{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC8755 },
    245 	{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC8755_2 },
    246 	{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC8755_3 },
    247 	{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC8765 },
    248 	{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC8775_2 },
    249 	{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC8780 },
    250 	{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC8781 },
    251 	{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MINI5725 },
    252 	{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_USB305 },
    253 	{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_250U },
    254 	/* Toshiba */
    255 	{ USB_VENDOR_TOSHIBA, USB_PRODUCT_TOSHIBA_HSDPA_MODEM_EU870DT1 },
    256 
    257 	/* ZTE */
    258 	{ USB_VENDOR_ZTE, USB_PRODUCT_ZTE_MF622 },
    259 	{ USB_VENDOR_ZTE, USB_PRODUCT_ZTE_MF626 },
    260 	{ USB_VENDOR_ZTE, USB_PRODUCT_ZTE_MF628 },
    261 	{ USB_VENDOR_ZTE, USB_PRODUCT_ZTE_MF820D },
    262 
    263 	/* 4G Systems */
    264 	{ USB_VENDOR_4GSYSTEMS, USB_PRODUCT_4GSYSTEMS_XSSTICK_P14 },
    265 	{ USB_VENDOR_4GSYSTEMS, USB_PRODUCT_4GSYSTEMS_XSSTICK_W14 },
    266 };
    267 
    268 static int
    269 send_bulkmsg(struct usbd_device *dev, void *cmd, size_t cmdlen)
    270 {
    271 	struct usbd_interface *iface;
    272 	usb_interface_descriptor_t *id;
    273 	usb_endpoint_descriptor_t *ed;
    274 	struct usbd_pipe *pipe;
    275 	struct usbd_xfer *xfer;
    276 	int err, i;
    277 
    278 	/* Move the device into the configured state. */
    279 	err = usbd_set_config_index(dev, 0, 0);
    280 	if (err) {
    281 		aprint_error("u3ginit: failed to set config index\n");
    282 		return UMATCH_NONE;
    283 	}
    284 
    285 	err = usbd_device2interface_handle(dev, 0, &iface);
    286 	if (err != 0) {
    287 		aprint_error("u3ginit: failed to get interface\n");
    288 		return UMATCH_NONE;
    289 	}
    290 
    291 	id = usbd_get_interface_descriptor(iface);
    292 	ed = NULL;
    293 	for (i = 0 ; i < id->bNumEndpoints ; i++) {
    294 		ed = usbd_interface2endpoint_descriptor(iface, i);
    295 		if (ed == NULL)
    296 			continue;
    297 		if (UE_GET_DIR(ed->bEndpointAddress) != UE_DIR_OUT)
    298 			continue;
    299 		if ((ed->bmAttributes & UE_XFERTYPE) == UE_BULK)
    300 			break;
    301 	}
    302 
    303 	if (i == id->bNumEndpoints)
    304 		return UMATCH_NONE;
    305 
    306 	err = usbd_open_pipe(iface, ed->bEndpointAddress,
    307 	    USBD_EXCLUSIVE_USE, &pipe);
    308 	if (err != 0) {
    309 		aprint_error("u3ginit: failed to open bulk transfer pipe %d\n",
    310 		    ed->bEndpointAddress);
    311 		return UMATCH_NONE;
    312 	}
    313 
    314 	xfer = usbd_alloc_xfer(dev);
    315 	if (xfer != NULL) {
    316 		usbd_setup_xfer(xfer, pipe, NULL, cmd, cmdlen,
    317 		    USBD_SYNCHRONOUS, USBD_DEFAULT_TIMEOUT, NULL);
    318 
    319 		err = usbd_transfer(xfer);
    320 
    321 #if 0 /* XXXpooka: at least my huawei "fails" this always, but still detaches */
    322 		if (err)
    323 			aprint_error("u3ginit: transfer failed\n");
    324 #else
    325 		err = 0;
    326 #endif
    327 		usbd_free_xfer(xfer);
    328 	} else {
    329 		aprint_error("u3ginit: failed to allocate xfer\n");
    330 		err = USBD_NOMEM;
    331 	}
    332 
    333 	usbd_abort_pipe(pipe);
    334 	usbd_close_pipe(pipe);
    335 
    336 	return err == USBD_NORMAL_COMPLETION ? UMATCH_HIGHEST : UMATCH_NONE;
    337 }
    338 
    339 /* Byte 0..3: Command Block Wrapper (CBW) signature */
    340 static void
    341 set_cbw(unsigned char *cmd)
    342 {
    343 	cmd[0] = 0x55;
    344 	cmd[1] = 0x53;
    345 	cmd[2] = 0x42;
    346 	cmd[3] = 0x43;
    347 }
    348 
    349 static int
    350 u3g_bulk_scsi_eject(struct usbd_device *dev)
    351 {
    352 	unsigned char cmd[31];
    353 
    354 	memset(cmd, 0, sizeof(cmd));
    355 	/* Byte 0..3: Command Block Wrapper (CBW) signature */
    356 	set_cbw(cmd);
    357 	/* 4..7: CBW Tag, has to unique, but only a single transfer used. */
    358 	cmd[4] = 0x01;
    359 	/* 8..11: CBW Transfer Length, no data here */
    360 	/* 12: CBW Flag: output, so 0 */
    361 	/* 13: CBW Lun: 0 */
    362 	/* 14: CBW Length */
    363 	cmd[14] = 0x06;
    364 
    365 	/* Rest is the SCSI payload */
    366 
    367 	/* 0: SCSI START/STOP opcode */
    368 	cmd[15] = 0x1b;
    369 	/* 1..3 unused */
    370 	/* 4 Load/Eject command */
    371 	cmd[19] = 0x02;
    372 	/* 5: unused */
    373 
    374 	return send_bulkmsg(dev, cmd, sizeof(cmd));
    375 }
    376 
    377 static int
    378 u3g_bulk_ata_eject(struct usbd_device *dev)
    379 {
    380 	unsigned char cmd[31];
    381 
    382 	memset(cmd, 0, sizeof(cmd));
    383 	/* Byte 0..3: Command Block Wrapper (CBW) signature */
    384 	set_cbw(cmd);
    385 	/* 4..7: CBW Tag, has to unique, but only a single transfer used. */
    386 	cmd[4] = 0x01;
    387 	/* 8..11: CBW Transfer Length, no data here */
    388 	/* 12: CBW Flag: output, so 0 */
    389 	/* 13: CBW Lun: 0 */
    390 	/* 14: CBW Length */
    391 	cmd[14] = 0x06;
    392 
    393 	/* Rest is the SCSI payload */
    394 
    395 	/* 0: ATA pass-through */
    396 	cmd[15] = 0x85;
    397 	/* 1..3 unused */
    398 	/* 4 XXX What is this command? */
    399 	cmd[19] = 0x24;
    400 	/* 5: unused */
    401 
    402 	return send_bulkmsg(dev, cmd, sizeof(cmd));
    403 }
    404 
    405 static int
    406 u3g_huawei_reinit(struct usbd_device *dev)
    407 {
    408 	/*
    409 	 * The Huawei device presents itself as a umass device with Windows
    410 	 * drivers on it. After installation of the driver, it reinits into a
    411 	 * 3G serial device.
    412 	 */
    413 	usb_device_request_t req;
    414 	usb_config_descriptor_t *cdesc;
    415 
    416 	/* Get the config descriptor */
    417 	cdesc = usbd_get_config_descriptor(dev);
    418 	if (cdesc == NULL) {
    419 		usb_device_descriptor_t dd;
    420 
    421 		if (usbd_get_device_desc(dev, &dd) != 0)
    422 			return UMATCH_NONE;
    423 
    424 		if (dd.bNumConfigurations != 1)
    425 			return UMATCH_NONE;
    426 
    427 		if (usbd_set_config_index(dev, 0, 1) != 0)
    428 			return UMATCH_NONE;
    429 
    430 		cdesc = usbd_get_config_descriptor(dev);
    431 
    432 		if (cdesc == NULL)
    433 			return UMATCH_NONE;
    434 	}
    435 
    436 	/*
    437 	 * One iface means umass mode, more than 1 (4 usually) means 3G mode.
    438 	 *
    439 	 * XXX: We should check the first interface's device class just to be
    440 	 * sure. If it's a mass storage device, then we can be fairly certain
    441 	 * it needs a mode-switch.
    442 	 */
    443 	if (cdesc->bNumInterface > 1)
    444 		return UMATCH_NONE;
    445 
    446 	req.bmRequestType = UT_WRITE_DEVICE;
    447 	req.bRequest = UR_SET_FEATURE;
    448 	USETW(req.wValue, UF_DEVICE_REMOTE_WAKEUP);
    449 	USETW(req.wIndex, UHF_PORT_SUSPEND);
    450 	USETW(req.wLength, 0);
    451 
    452 	(void) usbd_do_request(dev, &req, 0);
    453 
    454 	return UMATCH_HIGHEST; /* Prevent umass from attaching */
    455 }
    456 
    457 static int
    458 u3g_huawei_k3765_reinit(struct usbd_device *dev)
    459 {
    460 	unsigned char cmd[31];
    461 
    462 	/* magic string adapted from some webpage */
    463 	memset(cmd, 0, sizeof(cmd));
    464 	/* Byte 0..3: Command Block Wrapper (CBW) signature */
    465 	set_cbw(cmd);
    466 
    467 	cmd[15]= 0x11;
    468 	cmd[16]= 0x06;
    469 
    470 	return send_bulkmsg(dev, cmd, sizeof(cmd));
    471 }
    472 static int
    473 u3g_huawei_e171_reinit(struct usbd_device *dev)
    474 {
    475 	unsigned char cmd[31];
    476 
    477 	/* magic string adapted from some webpage */
    478 	memset(cmd, 0, sizeof(cmd));
    479 	/* Byte 0..3: Command Block Wrapper (CBW) signature */
    480 	set_cbw(cmd);
    481 
    482 	cmd[15]= 0x11;
    483 	cmd[16]= 0x06;
    484 	cmd[17]= 0x20;
    485 	cmd[20]= 0x01;
    486 
    487 	return send_bulkmsg(dev, cmd, sizeof(cmd));
    488 }
    489 
    490 static int
    491 u3g_huawei_e353_reinit(struct usbd_device *dev)
    492 {
    493 	unsigned char cmd[31];
    494 
    495 	/* magic string adapted from some webpage */
    496 	memset(cmd, 0, sizeof(cmd));
    497 	/* Byte 0..3: Command Block Wrapper (CBW) signature */
    498 	set_cbw(cmd);
    499 
    500 	cmd[4] = 0x7f;
    501 	cmd[9] = 0x02;
    502 	cmd[12] = 0x80;
    503 	cmd[14] = 0x0a;
    504 	cmd[15] = 0x11;
    505 	cmd[16] = 0x06;
    506 	cmd[17] = 0x20;
    507 	cmd[23] = 0x01;
    508 
    509 	return send_bulkmsg(dev, cmd, sizeof(cmd));
    510 }
    511 
    512 static int
    513 u3g_sierra_reinit(struct usbd_device *dev)
    514 {
    515 	/* Some Sierra devices presents themselves as a umass device with
    516 	 * Windows drivers on it. After installation of the driver, it
    517 	 * reinits into a * 3G serial device.
    518 	 */
    519 	usb_device_request_t req;
    520 
    521 	req.bmRequestType = UT_VENDOR;
    522 	req.bRequest = UR_SET_INTERFACE;
    523 	USETW(req.wValue, UF_DEVICE_REMOTE_WAKEUP);
    524 	USETW(req.wIndex, UHF_PORT_CONNECTION);
    525 	USETW(req.wLength, 0);
    526 
    527 	(void) usbd_do_request(dev, &req, 0);
    528 
    529 	return UMATCH_HIGHEST; /* Match to prevent umass from attaching */
    530 }
    531 
    532 static int
    533 u3g_4gsystems_reinit(struct usbd_device *dev)
    534 {
    535 	/* magic string adapted from usb_modeswitch database */
    536 	unsigned char cmd[31];
    537 
    538 	memset(cmd, 0, sizeof(cmd));
    539 	/* Byte 0..3: Command Block Wrapper (CBW) signature */
    540 	set_cbw(cmd);
    541 
    542 	cmd[4] = 0x12;
    543 	cmd[5] = 0x34;
    544 	cmd[6] = 0x56;
    545 	cmd[7] = 0x78;
    546 	cmd[8] = 0x80;
    547 	cmd[12] = 0x80;
    548 	cmd[14] = 0x06;
    549 	cmd[15] = 0x06;
    550 	cmd[16] = 0xf5;
    551 	cmd[17] = 0x04;
    552 	cmd[18] = 0x02;
    553 	cmd[19] = 0x52;
    554 	cmd[20] = 0x70;
    555 
    556 	return send_bulkmsg(dev, cmd, sizeof(cmd));
    557 }
    558 
    559 /*
    560  * First personality:
    561  *
    562  * Claim the entire device if a mode-switch is required.
    563  */
    564 
    565 static int
    566 u3ginit_match(device_t parent, cfdata_t match, void *aux)
    567 {
    568 	struct usb_attach_arg *uaa = aux;
    569 
    570 	/*
    571 	 * Huawei changes product when it is configured as a modem.
    572 	 */
    573 	switch (uaa->vendor) {
    574 	case USB_VENDOR_HUAWEI:
    575 		if (uaa->product == USB_PRODUCT_HUAWEI_K3765)
    576 			return UMATCH_NONE;
    577 
    578 		switch (uaa->product) {
    579 		case USB_PRODUCT_HUAWEI_E1750INIT:
    580 		case USB_PRODUCT_HUAWEI_K3765INIT:
    581 			return u3g_huawei_k3765_reinit(uaa->device);
    582 			break;
    583 		case USB_PRODUCT_HUAWEI_E171INIT:
    584 			return u3g_huawei_e171_reinit(uaa->device);
    585 			break;
    586 		case USB_PRODUCT_HUAWEI_E353INIT:
    587 			return u3g_huawei_e353_reinit(uaa->device);
    588 			break;
    589 		default:
    590 			return u3g_huawei_reinit(uaa->device);
    591 			break;
    592 		}
    593 		break;
    594 
    595 	case USB_VENDOR_NOVATEL2:
    596 		switch (uaa->product){
    597 		case USB_PRODUCT_NOVATEL2_MC950D_DRIVER:
    598 		case USB_PRODUCT_NOVATEL2_U760_DRIVER:
    599 			return u3g_bulk_scsi_eject(uaa->device);
    600 			break;
    601 		default:
    602 			break;
    603 		}
    604 		break;
    605 
    606 	case USB_VENDOR_SIERRA:
    607 		if (uaa->product == USB_PRODUCT_SIERRA_INSTALLER)
    608 			return u3g_sierra_reinit(uaa->device);
    609 		break;
    610 
    611 	case USB_VENDOR_QUALCOMM:
    612 		if (uaa->product == USB_PRODUCT_QUALCOMM_NTT_DOCOMO_L02C_STORAGE)
    613 			return u3g_bulk_scsi_eject(uaa->device);
    614 		break;
    615 
    616 	case USB_VENDOR_ZTE:
    617 		switch (uaa->product){
    618 		case USB_PRODUCT_ZTE_INSTALLER:
    619 		case USB_PRODUCT_ZTE_MF820D_INSTALLER:
    620 			(void)u3g_bulk_ata_eject(uaa->device);
    621 			(void)u3g_bulk_scsi_eject(uaa->device);
    622 			return UMATCH_HIGHEST;
    623 		default:
    624 			break;
    625 		}
    626 		break;
    627 
    628 	case USB_VENDOR_4GSYSTEMS:
    629 		if (uaa->product == USB_PRODUCT_4GSYSTEMS_XSSTICK_P14_INSTALLER)
    630 			return u3g_4gsystems_reinit(uaa->device);
    631 		break;
    632 
    633 	default:
    634 		break;
    635 	}
    636 
    637 	return UMATCH_NONE;
    638 }
    639 
    640 static void
    641 u3ginit_attach(device_t parent, device_t self, void *aux)
    642 {
    643 	struct usb_attach_arg *uaa = aux;
    644 
    645 	aprint_naive("\n");
    646 	aprint_normal(": Switching to 3G mode\n");
    647 
    648 	if (uaa->vendor == USB_VENDOR_NOVATEL2) {
    649 		switch (uaa->product) {
    650 	    	case USB_PRODUCT_NOVATEL2_MC950D_DRIVER:
    651 	    	case USB_PRODUCT_NOVATEL2_U760_DRIVER:
    652 			/* About to disappear... */
    653 			return;
    654 			break;
    655 		default:
    656 			break;
    657 		}
    658 	}
    659 
    660 	/* Move the device into the configured state. */
    661 	(void) usbd_set_config_index(uaa->device, 0, 1);
    662 }
    663 
    664 static int
    665 u3ginit_detach(device_t self, int flags)
    666 {
    667 
    668 	return 0;
    669 }
    670 
    671 
    672 /*
    673  * Second personality:
    674  *
    675  * Claim only those interfaces required for 3G modem operation.
    676  */
    677 
    678 static int
    679 u3g_match(device_t parent, cfdata_t match, void *aux)
    680 {
    681 	struct usbif_attach_arg *uaa = aux;
    682 	struct usbd_interface *iface;
    683 	usb_interface_descriptor_t *id;
    684 	usbd_status error;
    685 
    686 	if (!usb_lookup(u3g_devs, uaa->vendor, uaa->product))
    687 		return UMATCH_NONE;
    688 
    689 	error = usbd_device2interface_handle(uaa->device, uaa->ifaceno, &iface);
    690 	if (error) {
    691 		printf("u3g_match: failed to get interface, err=%s\n",
    692 		    usbd_errstr(error));
    693 		return UMATCH_NONE;
    694 	}
    695 
    696 	id = usbd_get_interface_descriptor(iface);
    697 	if (id == NULL) {
    698 		printf("u3g_match: failed to get interface descriptor\n");
    699 		return UMATCH_NONE;
    700 	}
    701 
    702 	/*
    703 	 * Huawei modems use the vendor-specific class for all interfaces,
    704 	 * both tty and CDC NCM, which we should avoid attaching to.
    705 	 */
    706 	if (uaa->vendor == USB_VENDOR_HUAWEI && id->bInterfaceSubClass == 2 &&
    707 	    (id->bInterfaceProtocol & 0xf) == 6)	/* 0x16, 0x46, 0x76 */
    708 		return UMATCH_NONE;
    709 
    710 	/*
    711 	 * 3G modems generally report vendor-specific class
    712 	 *
    713 	 * XXX: this may be too generalised.
    714 	 */
    715 	return id->bInterfaceClass == UICLASS_VENDOR ?
    716 	    UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
    717 }
    718 
    719 static void
    720 u3g_attach(device_t parent, device_t self, void *aux)
    721 {
    722 	struct u3g_softc *sc = device_private(self);
    723 	struct usbif_attach_arg *uaa = aux;
    724 	struct usbd_device *dev = uaa->device;
    725 	struct usbd_interface *iface;
    726 	usb_interface_descriptor_t *id;
    727 	usb_endpoint_descriptor_t *ed;
    728 	struct ucom_attach_args uca;
    729 	usbd_status error;
    730 	int n, intr_address, intr_size;
    731 
    732 	aprint_naive("\n");
    733 	aprint_normal("\n");
    734 
    735 	sc->sc_dev = self;
    736 	sc->sc_dying = false;
    737 	sc->sc_udev = dev;
    738 
    739 	error = usbd_device2interface_handle(dev, uaa->ifaceno, &iface);
    740 	if (error) {
    741 		aprint_error_dev(self, "failed to get interface, err=%s\n",
    742 		    usbd_errstr(error));
    743 		return;
    744 	}
    745 
    746 	id = usbd_get_interface_descriptor(iface);
    747 
    748 	uca.info = "3G Modem";
    749 	uca.ibufsize = U3G_BUFF_SIZE;
    750 	uca.obufsize = U3G_BUFF_SIZE;
    751 	uca.ibufsizepad = U3G_BUFF_SIZE;
    752 	uca.opkthdrlen = 0;
    753 	uca.device = dev;
    754 	uca.iface = iface;
    755 	uca.methods = &u3g_methods;
    756 	uca.arg = sc;
    757 	uca.portno = -1;
    758 	uca.bulkin = uca.bulkout = -1;
    759 
    760 
    761 	sc->sc_ifaceno = uaa->ifaceno;
    762 	intr_address = -1;
    763 	intr_size = 0;
    764 
    765 	for (n = 0; n < id->bNumEndpoints; n++) {
    766 		ed = usbd_interface2endpoint_descriptor(iface, n);
    767 		if (ed == NULL) {
    768 			aprint_error_dev(self, "no endpoint descriptor "
    769 			    "for %d (interface: %d)\n", n, sc->sc_ifaceno);
    770 			sc->sc_dying = true;
    771 			return;
    772 		}
    773 
    774 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    775 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
    776 			intr_address = ed->bEndpointAddress;
    777 			intr_size = UGETW(ed->wMaxPacketSize);
    778 		} else
    779 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    780 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
    781 			uca.bulkin = ed->bEndpointAddress;
    782 		} else
    783 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
    784 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
    785 			uca.bulkout = ed->bEndpointAddress;
    786 		}
    787 		if (uca.bulkin != -1 && uca.bulkout != -1) {
    788 			struct u3g_com *com;
    789 			if (sc->sc_ncom == __arraycount(sc->sc_com)) {
    790 				aprint_error_dev(self, "Need to configure "
    791 				    "more than %zu ttys", sc->sc_ncom);
    792 				continue;
    793 			}
    794 			uca.portno = sc->sc_ncom++;
    795 			com = &sc->sc_com[uca.portno];
    796 			com->c_outpins = 0;
    797 			com->c_msr = UMSR_DSR | UMSR_CTS | UMSR_DCD;
    798 			com->c_open = false;
    799 			com->c_purging = false;
    800 			com->c_dev = config_found_sm_loc(self, "ucombus",
    801 				NULL, &uca, ucomprint, ucomsubmatch);
    802 			uca.bulkin = -1;
    803 			uca.bulkout = -1;
    804 		}
    805 	}
    806 
    807 	if (sc->sc_ncom == 0) {
    808 		aprint_error_dev(self, "Missing bulk in/out for interface %d\n",
    809 		    sc->sc_ifaceno);
    810 		sc->sc_dying = true;
    811 		return;
    812 	}
    813 
    814 	/*
    815 	 * If the interface has an interrupt pipe, open it immediately so
    816 	 * that we can track input pin state changes regardless of whether
    817 	 * the tty(4) device is open or not.
    818 	 */
    819 	if (intr_address != -1) {
    820 		sc->sc_intr_size = intr_size;
    821 		sc->sc_intr_buff = kmem_alloc(intr_size, KM_SLEEP);
    822 		error = usbd_open_pipe_intr(iface, intr_address,
    823 		    USBD_SHORT_XFER_OK, &sc->sc_intr_pipe, sc, sc->sc_intr_buff,
    824 		    intr_size, u3g_intr, 100);
    825 		if (error) {
    826 			aprint_error_dev(self, "cannot open interrupt pipe "
    827 			    "(addr %d)\n", intr_address);
    828 			return;
    829 		}
    830 	} else {
    831 		sc->sc_intr_pipe = NULL;
    832 		sc->sc_intr_buff = NULL;
    833 	}
    834 
    835 	if (!pmf_device_register(self, NULL, NULL))
    836 		aprint_error_dev(self, "couldn't establish power handler\n");
    837 }
    838 
    839 static int
    840 u3g_detach(device_t self, int flags)
    841 {
    842 	struct u3g_softc *sc = device_private(self);
    843 	int rv;
    844 
    845 	if (sc->sc_dying)
    846 		return 0;
    847 
    848 	pmf_device_deregister(self);
    849 
    850 	for (size_t i = 0; i < sc->sc_ncom; i++)
    851 		if (sc->sc_com[i].c_dev != NULL) {
    852 			rv = config_detach(sc->sc_com[i].c_dev, flags);
    853 			if (rv != 0) {
    854 				aprint_verbose_dev(self, "Can't deallocate "
    855 				    "port (%d)", rv);
    856 			}
    857 		}
    858 
    859 	if (sc->sc_intr_pipe != NULL) {
    860 		(void) usbd_abort_pipe(sc->sc_intr_pipe);
    861 		(void) usbd_close_pipe(sc->sc_intr_pipe);
    862 		sc->sc_intr_pipe = NULL;
    863 	}
    864 	if (sc->sc_intr_buff != NULL) {
    865 		kmem_free(sc->sc_intr_buff, sc->sc_intr_size);
    866 		sc->sc_intr_buff = NULL;
    867 	}
    868 
    869 	return 0;
    870 }
    871 
    872 static void
    873 u3g_childdet(device_t self, device_t child)
    874 {
    875 	struct u3g_softc *sc = device_private(self);
    876 
    877 	for (size_t i = 0; i < sc->sc_ncom; i++)
    878 		    if (sc->sc_com[i].c_dev == child)
    879 			    sc->sc_com[i].c_dev = NULL;
    880 }
    881 
    882 static int
    883 u3g_activate(device_t self, enum devact act)
    884 {
    885 	struct u3g_softc *sc = device_private(self);
    886 	int rv = 0;
    887 
    888 	switch (act) {
    889 	case DVACT_DEACTIVATE:
    890 		for (size_t i = 0; i < sc->sc_ncom; i++)
    891 			if (sc->sc_com[i].c_dev != NULL &&
    892 			    config_deactivate(sc->sc_com[i].c_dev) && rv == 0)
    893 			rv = -1;
    894 		else
    895 			rv = 0;
    896 		break;
    897 
    898 	default:
    899 		break;
    900 	}
    901 
    902 	return rv;
    903 }
    904 
    905 static void
    906 u3g_intr(struct usbd_xfer *xfer, void *priv, usbd_status status)
    907 {
    908 	struct u3g_softc *sc = (struct u3g_softc *)priv;
    909 	u_char *buf;
    910 	int portno = 0;	/* XXX */
    911 	struct u3g_com *com = &sc->sc_com[portno];
    912 
    913 	if (sc->sc_dying)
    914 		return;
    915 
    916 	if (status != USBD_NORMAL_COMPLETION) {
    917 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
    918 			return;
    919 		usbd_clear_endpoint_stall_async(sc->sc_intr_pipe);
    920 		return;
    921 	}
    922 
    923 	buf = sc->sc_intr_buff;
    924 	if (buf[0] == 0xa1 && buf[1] == 0x20) {
    925 		u_char msr;
    926 
    927 		msr = com->c_msr & ~(UMSR_DCD | UMSR_DSR | UMSR_RI);
    928 
    929 		if (buf[8] & U3G_INPIN_DCD)
    930 			msr |= UMSR_DCD;
    931 
    932 		if (buf[8] & U3G_INPIN_DSR)
    933 			msr |= UMSR_DSR;
    934 
    935 		if (buf[8] & U3G_INPIN_RI)
    936 			msr |= UMSR_RI;
    937 
    938 		if (msr != com->c_msr) {
    939 			com->c_msr = msr;
    940 			if (com->c_open)
    941 				ucom_status_change(device_private(com->c_dev));
    942 		}
    943 	}
    944 }
    945 
    946 /*ARGSUSED*/
    947 static void
    948 u3g_get_status(void *arg, int portno, u_char *lsr, u_char *msr)
    949 {
    950 	struct u3g_softc *sc = arg;
    951 
    952 	if (lsr != NULL)
    953 		*lsr = 0;	/* LSR isn't supported */
    954 	if (msr != NULL)
    955 		*msr = sc->sc_com[portno].c_msr;
    956 }
    957 
    958 /*ARGSUSED*/
    959 static void
    960 u3g_set(void *arg, int portno, int reg, int onoff)
    961 {
    962 	struct u3g_softc *sc = arg;
    963 	usb_device_request_t req;
    964 	uint16_t mask, new_state;
    965 	usbd_status err;
    966 	struct u3g_com *com = &sc->sc_com[portno];
    967 
    968 	if (sc->sc_dying)
    969 		return;
    970 
    971 	switch (reg) {
    972 	case UCOM_SET_DTR:
    973 		mask = U3G_OUTPIN_DTR;
    974 		break;
    975 	case UCOM_SET_RTS:
    976 		mask = U3G_OUTPIN_RTS;
    977 		break;
    978 	default:
    979 		return;
    980 	}
    981 
    982 	new_state = com->c_outpins & ~mask;
    983 	if (onoff)
    984 		new_state |= mask;
    985 
    986 	if (new_state == com->c_outpins)
    987 		return;
    988 
    989 	com->c_outpins = new_state;
    990 
    991 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
    992 	req.bRequest = U3G_SET_PIN;
    993 	USETW(req.wValue, new_state);
    994 	USETW(req.wIndex, sc->sc_ifaceno);
    995 	USETW(req.wLength, 0);
    996 
    997 	err = usbd_do_request(sc->sc_udev, &req, 0);
    998 	if (err == USBD_STALLED)
    999 		usbd_clear_endpoint_stall(sc->sc_udev->ud_pipe0);
   1000 }
   1001 
   1002 /*ARGSUSED*/
   1003 static int
   1004 u3g_open(void *arg, int portno)
   1005 {
   1006 	struct u3g_softc *sc = arg;
   1007 	usb_device_request_t req;
   1008 	usb_endpoint_descriptor_t *ed;
   1009 	usb_interface_descriptor_t *id;
   1010 	struct usbd_interface *ih;
   1011 	usbd_status err;
   1012 	struct u3g_com *com = &sc->sc_com[portno];
   1013 	int i, nin;
   1014 
   1015 	if (sc->sc_dying)
   1016 		return 0;
   1017 
   1018 	err = usbd_device2interface_handle(sc->sc_udev, sc->sc_ifaceno, &ih);
   1019 	if (err)
   1020 		return EIO;
   1021 
   1022 	id = usbd_get_interface_descriptor(ih);
   1023 
   1024 	for (nin = i = 0; i < id->bNumEndpoints; i++) {
   1025 		ed = usbd_interface2endpoint_descriptor(ih, i);
   1026 		if (ed == NULL)
   1027 			return EIO;
   1028 
   1029 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
   1030 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK &&
   1031 		    nin++ == portno) {
   1032 			/* Issue ENDPOINT_HALT request */
   1033 			req.bmRequestType = UT_WRITE_ENDPOINT;
   1034 			req.bRequest = UR_CLEAR_FEATURE;
   1035 			USETW(req.wValue, UF_ENDPOINT_HALT);
   1036 			USETW(req.wIndex, ed->bEndpointAddress);
   1037 			USETW(req.wLength, 0);
   1038 			err = usbd_do_request(sc->sc_udev, &req, 0);
   1039 			if (err)
   1040 				return EIO;
   1041 		}
   1042 	}
   1043 
   1044 	com->c_open = true;
   1045 	com->c_purging = true;
   1046 	getmicrotime(&com->c_purge_start);
   1047 
   1048 	return 0;
   1049 }
   1050 
   1051 /*ARGSUSED*/
   1052 static void
   1053 u3g_close(void *arg, int portno)
   1054 {
   1055 	struct u3g_softc *sc = arg;
   1056 	struct u3g_com *com = &sc->sc_com[portno];
   1057 
   1058 	com->c_open = false;
   1059 }
   1060 
   1061 /*ARGSUSED*/
   1062 static void
   1063 u3g_read(void *arg, int portno, u_char **cpp, uint32_t *ccp)
   1064 {
   1065 	struct u3g_softc *sc = arg;
   1066 	struct timeval curr_tv, diff_tv;
   1067 	struct u3g_com *com = &sc->sc_com[portno];
   1068 
   1069 	/*
   1070 	 * If we're not purging input data following first open, do nothing.
   1071 	 */
   1072 	if (com->c_purging == false)
   1073 		return;
   1074 
   1075 	/*
   1076 	 * Otherwise check if the purge timeout has expired
   1077 	 */
   1078 	getmicrotime(&curr_tv);
   1079 	timersub(&curr_tv, &com->c_purge_start, &diff_tv);
   1080 
   1081 	if (diff_tv.tv_sec >= U3G_PURGE_SECS) {
   1082 		/* Timeout expired. */
   1083 		com->c_purging = false;
   1084 	} else {
   1085 		/* Still purging. Adjust the caller's byte count. */
   1086 		*ccp = 0;
   1087 	}
   1088 }
   1089 
   1090 /*ARGSUSED*/
   1091 static void
   1092 u3g_write(void *arg, int portno, u_char *to, u_char *from, uint32_t *count)
   1093 {
   1094 	struct u3g_softc *sc = arg;
   1095 	struct u3g_com *com = &sc->sc_com[portno];
   1096 
   1097 	/*
   1098 	 * Stop purging as soon as the first data is written to the device.
   1099 	 */
   1100 	com->c_purging = false;
   1101 	memcpy(to, from, *count);
   1102 }
   1103