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