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