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