Home | History | Annotate | Line # | Download | only in usb
u3g.c revision 1.34
      1  1.34  christos /*	$NetBSD: u3g.c,v 1.34 2017/05/24 20:23:58 christos 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.34  christos __KERNEL_RCSID(0, "$NetBSD: u3g.c,v 1.34 2017/05/24 20:23:58 christos 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.32     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.32     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.32     skrll 	struct usbd_pipe *	sc_intr_pipe;	/* Interrupt pipe */
    131   1.9    martin 	u_char			*sc_intr_buff;	/* Interrupt buffer */
    132  1.32     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.34  christos  * drivers while the mode-switch is in progress. This is implemented by
    141  1.34  christos  * the umodeswitch driver.
    142   1.9    martin  *
    143   1.9    martin  * The second personality uses the 'usbifif' interface attribute so that
    144   1.9    martin  * it can claim the 3G modem interfaces for itself, leaving others (such
    145   1.9    martin  * as the mass storage interfaces on some devices) for other drivers.
    146   1.9    martin  */
    147   1.9    martin 
    148   1.9    martin static int u3g_match(device_t, cfdata_t, void *);
    149   1.9    martin static void u3g_attach(device_t, device_t, void *);
    150   1.9    martin static int u3g_detach(device_t, int);
    151   1.9    martin static int u3g_activate(device_t, enum devact);
    152   1.9    martin static void u3g_childdet(device_t, device_t);
    153   1.9    martin 
    154   1.9    martin CFATTACH_DECL2_NEW(u3g, sizeof(struct u3g_softc), u3g_match,
    155   1.9    martin     u3g_attach, u3g_detach, u3g_activate, NULL, u3g_childdet);
    156   1.9    martin 
    157   1.9    martin 
    158  1.32     skrll static void u3g_intr(struct usbd_xfer *, void *, usbd_status);
    159   1.9    martin static void u3g_get_status(void *, int, u_char *, u_char *);
    160   1.9    martin static void u3g_set(void *, int, int, int);
    161   1.9    martin static int  u3g_open(void *, int);
    162   1.9    martin static void u3g_close(void *, int);
    163   1.9    martin static void u3g_read(void *, int, u_char **, uint32_t *);
    164  1.32     skrll static void u3g_write(void *, int, u_char *, u_char *, uint32_t *);
    165   1.9    martin 
    166   1.1     joerg struct ucom_methods u3g_methods = {
    167  1.32     skrll 	.ucom_get_status = u3g_get_status,
    168  1.32     skrll 	.ucom_set = u3g_set,
    169  1.32     skrll 	.ucom_param = NULL,
    170  1.32     skrll 	.ucom_ioctl = NULL,
    171  1.32     skrll 	.ucom_open = u3g_open,
    172  1.32     skrll 	.ucom_close = u3g_close,
    173  1.32     skrll 	.ucom_read = u3g_read,
    174  1.32     skrll 	.ucom_write = u3g_write,
    175   1.1     joerg };
    176   1.1     joerg 
    177   1.9    martin /*
    178   1.9    martin  * Allegedly supported devices
    179   1.9    martin  */
    180   1.1     joerg static const struct usb_devno u3g_devs[] = {
    181  1.32     skrll 	{ USB_VENDOR_DELL, USB_PRODUCT_DELL_W5500 },
    182  1.33     hauke 	{ USB_VENDOR_HP, USB_PRODUCT_HP_UN2430 },
    183   1.1     joerg 	/* OEM: Huawei */
    184  1.19     veego 	{ USB_VENDOR_HUAWEI, USB_PRODUCT_HUAWEI_E1750 },
    185  1.18       apb 	{ USB_VENDOR_HUAWEI, USB_PRODUCT_HUAWEI_E1820 },
    186   1.1     joerg 	{ USB_VENDOR_HUAWEI, USB_PRODUCT_HUAWEI_E220 },
    187  1.26   khorben 	{ USB_VENDOR_HUAWEI, USB_PRODUCT_HUAWEI_EM770W },
    188  1.11     pooka 	{ USB_VENDOR_HUAWEI, USB_PRODUCT_HUAWEI_K3765 },
    189  1.13       riz 	{ USB_VENDOR_HUAWEI, USB_PRODUCT_HUAWEI_MOBILE },
    190  1.28    nonaka 	{ USB_VENDOR_HUAWEI, USB_PRODUCT_HUAWEI_E171 },
    191  1.31  christos 	{ USB_VENDOR_HUAWEI, USB_PRODUCT_HUAWEI_E353 },
    192  1.13       riz 	/* OEM: Merlin */
    193  1.13       riz 	{ USB_VENDOR_MERLIN, USB_PRODUCT_MERLIN_V620 },
    194   1.1     joerg 	/* OEM: Novatel */
    195   1.3     joerg 	{ USB_VENDOR_NOVATEL2, USB_PRODUCT_NOVATEL2_ES620 },
    196  1.22    nonaka 	{ USB_VENDOR_NOVATEL2, USB_PRODUCT_NOVATEL2_EU8X0D },
    197   1.1     joerg 	{ USB_VENDOR_NOVATEL2, USB_PRODUCT_NOVATEL2_MC950D },
    198  1.13       riz #if 0
    199  1.14       riz 	/* These are matched in u3ginit_match() */
    200  1.13       riz 	{ USB_VENDOR_NOVATEL2, USB_PRODUCT_NOVATEL2_MC950D_DRIVER },
    201  1.14       riz 	{ USB_VENDOR_NOVATEL2, USB_PRODUCT_NOVATEL2_U760_DRIVER },
    202  1.13       riz #endif
    203  1.13       riz 	{ USB_VENDOR_NOVATEL2, USB_PRODUCT_NOVATEL2_MERLINU740 },
    204  1.13       riz 	{ USB_VENDOR_NOVATEL2, USB_PRODUCT_NOVATEL2_MERLINV620 },
    205  1.13       riz 	{ USB_VENDOR_NOVATEL2, USB_PRODUCT_NOVATEL2_S720 },
    206   1.3     joerg 	{ USB_VENDOR_NOVATEL2, USB_PRODUCT_NOVATEL2_U720 },
    207   1.3     joerg 	{ USB_VENDOR_NOVATEL2, USB_PRODUCT_NOVATEL2_U727 },
    208   1.3     joerg 	{ USB_VENDOR_NOVATEL2, USB_PRODUCT_NOVATEL2_U740_2 },
    209  1.14       riz 	{ USB_VENDOR_NOVATEL2, USB_PRODUCT_NOVATEL2_U760 },
    210   1.3     joerg 	{ USB_VENDOR_NOVATEL2, USB_PRODUCT_NOVATEL2_U870 },
    211   1.3     joerg 	{ USB_VENDOR_NOVATEL2, USB_PRODUCT_NOVATEL2_V740 },
    212   1.3     joerg 	{ USB_VENDOR_NOVATEL2, USB_PRODUCT_NOVATEL2_X950D },
    213   1.3     joerg 	{ USB_VENDOR_NOVATEL2, USB_PRODUCT_NOVATEL2_XU870 },
    214  1.13       riz 	/* OEM: Option N.V. */
    215  1.13       riz 	{ USB_VENDOR_OPTIONNV, USB_PRODUCT_OPTIONNV_QUADPLUSUMTS },
    216  1.13       riz 	{ USB_VENDOR_OPTIONNV, USB_PRODUCT_OPTIONNV_HSDPA },
    217  1.13       riz 	{ USB_VENDOR_OPTIONNV, USB_PRODUCT_OPTIONNV_GTMAXHSUPA },
    218  1.13       riz 	/* OEM: Qualcomm, Inc. */
    219  1.25    nonaka 	{ USB_VENDOR_QUALCOMM, USB_PRODUCT_QUALCOMM_NTT_DOCOMO_L02C_MODEM },
    220   1.1     joerg 
    221   1.3     joerg 	/* OEM: Sierra Wireless: */
    222   1.3     joerg 	{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AC595U },
    223   1.3     joerg 	{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AC597E },
    224  1.13       riz 	{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AC875U },
    225   1.3     joerg 	{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AC880 },
    226   1.3     joerg 	{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AC880E },
    227   1.3     joerg 	{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AC880U },
    228   1.3     joerg 	{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AC881 },
    229   1.3     joerg 	{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AC881E },
    230   1.3     joerg 	{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AC881U },
    231  1.13       riz 	{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AIRCARD580 },
    232  1.13       riz 	{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AIRCARD595 },
    233  1.13       riz 	{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AIRCARD875 },
    234  1.13       riz 	{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_C597 },
    235   1.3     joerg 	{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_EM5625 },
    236   1.3     joerg 	{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC5720 },
    237   1.3     joerg 	{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC5720_2 },
    238   1.3     joerg 	{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC5725 },
    239   1.3     joerg 	{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC8755 },
    240   1.3     joerg 	{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC8755_2 },
    241   1.3     joerg 	{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC8755_3 },
    242   1.3     joerg 	{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC8765 },
    243   1.3     joerg 	{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC8775_2 },
    244   1.3     joerg 	{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC8780 },
    245   1.3     joerg 	{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC8781 },
    246  1.13       riz 	{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MINI5725 },
    247  1.12       riz 	{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_USB305 },
    248  1.27  christos 	{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_250U },
    249  1.17       scw 	/* Toshiba */
    250  1.17       scw 	{ USB_VENDOR_TOSHIBA, USB_PRODUCT_TOSHIBA_HSDPA_MODEM_EU870DT1 },
    251  1.20  christos 
    252  1.29     soren 	/* ZTE */
    253  1.29     soren 	{ USB_VENDOR_ZTE, USB_PRODUCT_ZTE_MF622 },
    254  1.29     soren 	{ USB_VENDOR_ZTE, USB_PRODUCT_ZTE_MF626 },
    255  1.29     soren 	{ USB_VENDOR_ZTE, USB_PRODUCT_ZTE_MF628 },
    256  1.29     soren 	{ USB_VENDOR_ZTE, USB_PRODUCT_ZTE_MF820D },
    257  1.29     soren 
    258  1.20  christos 	/* 4G Systems */
    259  1.20  christos 	{ USB_VENDOR_4GSYSTEMS, USB_PRODUCT_4GSYSTEMS_XSSTICK_P14 },
    260  1.30  christos 	{ USB_VENDOR_4GSYSTEMS, USB_PRODUCT_4GSYSTEMS_XSSTICK_W14 },
    261   1.1     joerg };
    262   1.1     joerg 
    263   1.9    martin /*
    264   1.9    martin  * Second personality:
    265   1.9    martin  *
    266   1.9    martin  * Claim only those interfaces required for 3G modem operation.
    267   1.9    martin  */
    268   1.9    martin 
    269   1.9    martin static int
    270   1.9    martin u3g_match(device_t parent, cfdata_t match, void *aux)
    271   1.9    martin {
    272  1.32     skrll 	struct usbif_attach_arg *uiaa = aux;
    273  1.32     skrll 	struct usbd_interface *iface;
    274   1.9    martin 	usb_interface_descriptor_t *id;
    275   1.9    martin 	usbd_status error;
    276   1.9    martin 
    277  1.32     skrll 	if (!usb_lookup(u3g_devs, uiaa->uiaa_vendor, uiaa->uiaa_product))
    278  1.32     skrll 		return UMATCH_NONE;
    279   1.9    martin 
    280  1.32     skrll 	error = usbd_device2interface_handle(uiaa->uiaa_device,
    281  1.32     skrll 	    uiaa->uiaa_ifaceno, &iface);
    282   1.1     joerg 	if (error) {
    283   1.9    martin 		printf("u3g_match: failed to get interface, err=%s\n",
    284   1.9    martin 		    usbd_errstr(error));
    285  1.32     skrll 		return UMATCH_NONE;
    286   1.9    martin 	}
    287   1.9    martin 
    288   1.9    martin 	id = usbd_get_interface_descriptor(iface);
    289   1.9    martin 	if (id == NULL) {
    290   1.9    martin 		printf("u3g_match: failed to get interface descriptor\n");
    291  1.32     skrll 		return UMATCH_NONE;
    292   1.1     joerg 	}
    293   1.1     joerg 
    294   1.9    martin 	/*
    295  1.29     soren 	 * Huawei modems use the vendor-specific class for all interfaces,
    296  1.29     soren 	 * both tty and CDC NCM, which we should avoid attaching to.
    297  1.29     soren 	 */
    298  1.32     skrll 	if (uiaa->uiaa_vendor == USB_VENDOR_HUAWEI &&
    299  1.32     skrll 	    id->bInterfaceSubClass == 2 &&
    300  1.29     soren 	    (id->bInterfaceProtocol & 0xf) == 6)	/* 0x16, 0x46, 0x76 */
    301  1.32     skrll 		return UMATCH_NONE;
    302  1.29     soren 
    303  1.29     soren 	/*
    304   1.9    martin 	 * 3G modems generally report vendor-specific class
    305   1.9    martin 	 *
    306   1.9    martin 	 * XXX: this may be too generalised.
    307   1.9    martin 	 */
    308  1.32     skrll 	return id->bInterfaceClass == UICLASS_VENDOR ?
    309  1.32     skrll 	    UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
    310   1.9    martin }
    311   1.9    martin 
    312   1.9    martin static void
    313   1.9    martin u3g_attach(device_t parent, device_t self, void *aux)
    314   1.9    martin {
    315   1.9    martin 	struct u3g_softc *sc = device_private(self);
    316  1.32     skrll 	struct usbif_attach_arg *uiaa = aux;
    317  1.32     skrll 	struct usbd_device *dev = uiaa->uiaa_device;
    318  1.32     skrll 	struct usbd_interface *iface;
    319   1.9    martin 	usb_interface_descriptor_t *id;
    320   1.9    martin 	usb_endpoint_descriptor_t *ed;
    321  1.32     skrll 	struct ucom_attach_args ucaa;
    322   1.9    martin 	usbd_status error;
    323  1.32     skrll 	int n, intr_address, intr_size;
    324   1.1     joerg 
    325   1.9    martin 	aprint_naive("\n");
    326   1.9    martin 	aprint_normal("\n");
    327   1.1     joerg 
    328   1.9    martin 	sc->sc_dev = self;
    329   1.9    martin 	sc->sc_dying = false;
    330   1.9    martin 	sc->sc_udev = dev;
    331   1.2     joerg 
    332  1.32     skrll 	error = usbd_device2interface_handle(dev, uiaa->uiaa_ifaceno, &iface);
    333   1.9    martin 	if (error) {
    334   1.9    martin 		aprint_error_dev(self, "failed to get interface, err=%s\n",
    335   1.9    martin 		    usbd_errstr(error));
    336   1.4  christos 		return;
    337   1.4  christos 	}
    338   1.4  christos 
    339   1.9    martin 	id = usbd_get_interface_descriptor(iface);
    340   1.1     joerg 
    341  1.32     skrll 	ucaa.ucaa_info = "3G Modem";
    342  1.32     skrll 	ucaa.ucaa_ibufsize = U3G_BUFF_SIZE;
    343  1.32     skrll 	ucaa.ucaa_obufsize = U3G_BUFF_SIZE;
    344  1.32     skrll 	ucaa.ucaa_ibufsizepad = U3G_BUFF_SIZE;
    345  1.32     skrll 	ucaa.ucaa_opkthdrlen = 0;
    346  1.32     skrll 	ucaa.ucaa_device = dev;
    347  1.32     skrll 	ucaa.ucaa_iface = iface;
    348  1.32     skrll 	ucaa.ucaa_methods = &u3g_methods;
    349  1.32     skrll 	ucaa.ucaa_arg = sc;
    350  1.32     skrll 	ucaa.ucaa_portno = -1;
    351  1.32     skrll 	ucaa.ucaa_bulkin = ucaa.ucaa_bulkout = -1;
    352   1.9    martin 
    353  1.27  christos 
    354  1.32     skrll 	sc->sc_ifaceno = uiaa->uiaa_ifaceno;
    355   1.9    martin 	intr_address = -1;
    356   1.9    martin 	intr_size = 0;
    357   1.9    martin 
    358   1.9    martin 	for (n = 0; n < id->bNumEndpoints; n++) {
    359   1.9    martin 		ed = usbd_interface2endpoint_descriptor(iface, n);
    360   1.9    martin 		if (ed == NULL) {
    361   1.9    martin 			aprint_error_dev(self, "no endpoint descriptor "
    362   1.9    martin 			    "for %d (interface: %d)\n", n, sc->sc_ifaceno);
    363   1.9    martin 			sc->sc_dying = true;
    364   1.1     joerg 			return;
    365   1.1     joerg 		}
    366   1.1     joerg 
    367   1.9    martin 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    368   1.9    martin 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
    369   1.9    martin 			intr_address = ed->bEndpointAddress;
    370   1.9    martin 			intr_size = UGETW(ed->wMaxPacketSize);
    371   1.9    martin 		} else
    372   1.9    martin 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    373   1.9    martin 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
    374  1.32     skrll 			ucaa.ucaa_bulkin = ed->bEndpointAddress;
    375   1.9    martin 		} else
    376   1.9    martin 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
    377   1.9    martin 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
    378  1.32     skrll 			ucaa.ucaa_bulkout = ed->bEndpointAddress;
    379   1.1     joerg 		}
    380  1.32     skrll 		if (ucaa.ucaa_bulkin != -1 && ucaa.ucaa_bulkout != -1) {
    381  1.27  christos 			struct u3g_com *com;
    382  1.27  christos 			if (sc->sc_ncom == __arraycount(sc->sc_com)) {
    383  1.27  christos 				aprint_error_dev(self, "Need to configure "
    384  1.27  christos 				    "more than %zu ttys", sc->sc_ncom);
    385  1.27  christos 				continue;
    386  1.27  christos 			}
    387  1.32     skrll 			ucaa.ucaa_portno = sc->sc_ncom++;
    388  1.32     skrll 			com = &sc->sc_com[ucaa.ucaa_portno];
    389  1.27  christos 			com->c_outpins = 0;
    390  1.27  christos 			com->c_msr = UMSR_DSR | UMSR_CTS | UMSR_DCD;
    391  1.27  christos 			com->c_open = false;
    392  1.27  christos 			com->c_purging = false;
    393  1.27  christos 			com->c_dev = config_found_sm_loc(self, "ucombus",
    394  1.32     skrll 				NULL, &ucaa, ucomprint, ucomsubmatch);
    395  1.32     skrll 			ucaa.ucaa_bulkin = -1;
    396  1.32     skrll 			ucaa.ucaa_bulkout = -1;
    397  1.27  christos 		}
    398   1.9    martin 	}
    399   1.9    martin 
    400  1.27  christos 	if (sc->sc_ncom == 0) {
    401  1.27  christos 		aprint_error_dev(self, "Missing bulk in/out for interface %d\n",
    402   1.9    martin 		    sc->sc_ifaceno);
    403   1.9    martin 		sc->sc_dying = true;
    404   1.9    martin 		return;
    405   1.1     joerg 	}
    406   1.1     joerg 
    407   1.9    martin 	/*
    408   1.9    martin 	 * If the interface has an interrupt pipe, open it immediately so
    409   1.9    martin 	 * that we can track input pin state changes regardless of whether
    410   1.9    martin 	 * the tty(4) device is open or not.
    411   1.9    martin 	 */
    412   1.9    martin 	if (intr_address != -1) {
    413  1.32     skrll 		sc->sc_intr_size = intr_size;
    414  1.32     skrll 		sc->sc_intr_buff = kmem_alloc(intr_size, KM_SLEEP);
    415   1.9    martin 		error = usbd_open_pipe_intr(iface, intr_address,
    416   1.9    martin 		    USBD_SHORT_XFER_OK, &sc->sc_intr_pipe, sc, sc->sc_intr_buff,
    417   1.9    martin 		    intr_size, u3g_intr, 100);
    418   1.1     joerg 		if (error) {
    419   1.9    martin 			aprint_error_dev(self, "cannot open interrupt pipe "
    420   1.9    martin 			    "(addr %d)\n", intr_address);
    421   1.1     joerg 			return;
    422   1.1     joerg 		}
    423   1.9    martin 	} else {
    424   1.9    martin 		sc->sc_intr_pipe = NULL;
    425   1.9    martin 		sc->sc_intr_buff = NULL;
    426   1.1     joerg 	}
    427   1.1     joerg 
    428   1.1     joerg 	if (!pmf_device_register(self, NULL, NULL))
    429   1.1     joerg 		aprint_error_dev(self, "couldn't establish power handler\n");
    430   1.1     joerg }
    431   1.1     joerg 
    432   1.1     joerg static int
    433   1.1     joerg u3g_detach(device_t self, int flags)
    434   1.1     joerg {
    435   1.1     joerg 	struct u3g_softc *sc = device_private(self);
    436   1.9    martin 	int rv;
    437   1.1     joerg 
    438   1.9    martin 	if (sc->sc_dying)
    439   1.2     joerg 		return 0;
    440   1.2     joerg 
    441   1.1     joerg 	pmf_device_deregister(self);
    442   1.1     joerg 
    443  1.27  christos 	for (size_t i = 0; i < sc->sc_ncom; i++)
    444  1.27  christos 		if (sc->sc_com[i].c_dev != NULL) {
    445  1.27  christos 			rv = config_detach(sc->sc_com[i].c_dev, flags);
    446  1.27  christos 			if (rv != 0) {
    447  1.27  christos 				aprint_verbose_dev(self, "Can't deallocate "
    448  1.27  christos 				    "port (%d)", rv);
    449  1.27  christos 			}
    450   1.1     joerg 		}
    451   1.1     joerg 
    452   1.1     joerg 	if (sc->sc_intr_pipe != NULL) {
    453   1.9    martin 		(void) usbd_abort_pipe(sc->sc_intr_pipe);
    454   1.9    martin 		(void) usbd_close_pipe(sc->sc_intr_pipe);
    455   1.1     joerg 		sc->sc_intr_pipe = NULL;
    456   1.1     joerg 	}
    457   1.9    martin 	if (sc->sc_intr_buff != NULL) {
    458  1.32     skrll 		kmem_free(sc->sc_intr_buff, sc->sc_intr_size);
    459   1.9    martin 		sc->sc_intr_buff = NULL;
    460   1.9    martin 	}
    461   1.1     joerg 
    462  1.32     skrll 	return 0;
    463   1.1     joerg }
    464   1.1     joerg 
    465   1.1     joerg static void
    466   1.1     joerg u3g_childdet(device_t self, device_t child)
    467   1.1     joerg {
    468   1.1     joerg 	struct u3g_softc *sc = device_private(self);
    469   1.9    martin 
    470  1.27  christos 	for (size_t i = 0; i < sc->sc_ncom; i++)
    471  1.27  christos 		    if (sc->sc_com[i].c_dev == child)
    472  1.27  christos 			    sc->sc_com[i].c_dev = NULL;
    473   1.9    martin }
    474   1.9    martin 
    475   1.9    martin static int
    476   1.9    martin u3g_activate(device_t self, enum devact act)
    477   1.9    martin {
    478   1.9    martin 	struct u3g_softc *sc = device_private(self);
    479  1.27  christos 	int rv = 0;
    480   1.9    martin 
    481   1.9    martin 	switch (act) {
    482   1.9    martin 	case DVACT_DEACTIVATE:
    483  1.27  christos 		for (size_t i = 0; i < sc->sc_ncom; i++)
    484  1.27  christos 			if (sc->sc_com[i].c_dev != NULL &&
    485  1.27  christos 			    config_deactivate(sc->sc_com[i].c_dev) && rv == 0)
    486   1.9    martin 			rv = -1;
    487   1.9    martin 		else
    488   1.9    martin 			rv = 0;
    489   1.9    martin 		break;
    490   1.9    martin 
    491   1.9    martin 	default:
    492   1.9    martin 		break;
    493   1.9    martin 	}
    494   1.9    martin 
    495  1.27  christos 	return rv;
    496   1.9    martin }
    497   1.9    martin 
    498   1.9    martin static void
    499  1.32     skrll u3g_intr(struct usbd_xfer *xfer, void *priv, usbd_status status)
    500   1.9    martin {
    501   1.9    martin 	struct u3g_softc *sc = (struct u3g_softc *)priv;
    502   1.9    martin 	u_char *buf;
    503  1.27  christos 	int portno = 0;	/* XXX */
    504  1.27  christos 	struct u3g_com *com = &sc->sc_com[portno];
    505   1.9    martin 
    506   1.9    martin 	if (sc->sc_dying)
    507   1.9    martin 		return;
    508   1.9    martin 
    509   1.9    martin 	if (status != USBD_NORMAL_COMPLETION) {
    510   1.9    martin 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
    511   1.9    martin 			return;
    512   1.9    martin 		usbd_clear_endpoint_stall_async(sc->sc_intr_pipe);
    513   1.9    martin 		return;
    514   1.9    martin 	}
    515   1.9    martin 
    516   1.9    martin 	buf = sc->sc_intr_buff;
    517   1.9    martin 	if (buf[0] == 0xa1 && buf[1] == 0x20) {
    518   1.9    martin 		u_char msr;
    519   1.9    martin 
    520  1.27  christos 		msr = com->c_msr & ~(UMSR_DCD | UMSR_DSR | UMSR_RI);
    521   1.9    martin 
    522   1.9    martin 		if (buf[8] & U3G_INPIN_DCD)
    523   1.9    martin 			msr |= UMSR_DCD;
    524   1.9    martin 
    525   1.9    martin 		if (buf[8] & U3G_INPIN_DSR)
    526   1.9    martin 			msr |= UMSR_DSR;
    527   1.9    martin 
    528   1.9    martin 		if (buf[8] & U3G_INPIN_RI)
    529   1.9    martin 			msr |= UMSR_RI;
    530   1.9    martin 
    531  1.27  christos 		if (msr != com->c_msr) {
    532  1.27  christos 			com->c_msr = msr;
    533  1.27  christos 			if (com->c_open)
    534  1.27  christos 				ucom_status_change(device_private(com->c_dev));
    535   1.9    martin 		}
    536   1.9    martin 	}
    537   1.9    martin }
    538   1.9    martin 
    539   1.9    martin /*ARGSUSED*/
    540   1.9    martin static void
    541   1.9    martin u3g_get_status(void *arg, int portno, u_char *lsr, u_char *msr)
    542   1.9    martin {
    543   1.9    martin 	struct u3g_softc *sc = arg;
    544   1.9    martin 
    545   1.9    martin 	if (lsr != NULL)
    546   1.9    martin 		*lsr = 0;	/* LSR isn't supported */
    547   1.9    martin 	if (msr != NULL)
    548  1.27  christos 		*msr = sc->sc_com[portno].c_msr;
    549   1.9    martin }
    550   1.9    martin 
    551   1.9    martin /*ARGSUSED*/
    552   1.9    martin static void
    553   1.9    martin u3g_set(void *arg, int portno, int reg, int onoff)
    554   1.9    martin {
    555   1.9    martin 	struct u3g_softc *sc = arg;
    556   1.9    martin 	usb_device_request_t req;
    557   1.9    martin 	uint16_t mask, new_state;
    558   1.9    martin 	usbd_status err;
    559  1.27  christos 	struct u3g_com *com = &sc->sc_com[portno];
    560   1.9    martin 
    561   1.9    martin 	if (sc->sc_dying)
    562   1.9    martin 		return;
    563   1.9    martin 
    564   1.9    martin 	switch (reg) {
    565   1.9    martin 	case UCOM_SET_DTR:
    566   1.9    martin 		mask = U3G_OUTPIN_DTR;
    567   1.9    martin 		break;
    568   1.9    martin 	case UCOM_SET_RTS:
    569   1.9    martin 		mask = U3G_OUTPIN_RTS;
    570   1.9    martin 		break;
    571   1.9    martin 	default:
    572   1.9    martin 		return;
    573   1.9    martin 	}
    574   1.9    martin 
    575  1.27  christos 	new_state = com->c_outpins & ~mask;
    576   1.9    martin 	if (onoff)
    577   1.9    martin 		new_state |= mask;
    578   1.9    martin 
    579  1.27  christos 	if (new_state == com->c_outpins)
    580   1.9    martin 		return;
    581   1.9    martin 
    582  1.27  christos 	com->c_outpins = new_state;
    583   1.9    martin 
    584   1.9    martin 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
    585   1.9    martin 	req.bRequest = U3G_SET_PIN;
    586   1.9    martin 	USETW(req.wValue, new_state);
    587   1.9    martin 	USETW(req.wIndex, sc->sc_ifaceno);
    588   1.9    martin 	USETW(req.wLength, 0);
    589   1.9    martin 
    590   1.9    martin 	err = usbd_do_request(sc->sc_udev, &req, 0);
    591   1.9    martin 	if (err == USBD_STALLED)
    592  1.32     skrll 		usbd_clear_endpoint_stall(sc->sc_udev->ud_pipe0);
    593   1.9    martin }
    594   1.9    martin 
    595   1.9    martin /*ARGSUSED*/
    596  1.32     skrll static int
    597   1.9    martin u3g_open(void *arg, int portno)
    598   1.9    martin {
    599   1.9    martin 	struct u3g_softc *sc = arg;
    600   1.9    martin 	usb_device_request_t req;
    601   1.9    martin 	usb_endpoint_descriptor_t *ed;
    602   1.9    martin 	usb_interface_descriptor_t *id;
    603  1.32     skrll 	struct usbd_interface *ih;
    604   1.9    martin 	usbd_status err;
    605  1.27  christos 	struct u3g_com *com = &sc->sc_com[portno];
    606  1.27  christos 	int i, nin;
    607   1.1     joerg 
    608   1.9    martin 	if (sc->sc_dying)
    609  1.32     skrll 		return 0;
    610   1.9    martin 
    611  1.27  christos 	err = usbd_device2interface_handle(sc->sc_udev, sc->sc_ifaceno, &ih);
    612   1.9    martin 	if (err)
    613  1.32     skrll 		return EIO;
    614   1.9    martin 
    615   1.9    martin 	id = usbd_get_interface_descriptor(ih);
    616   1.9    martin 
    617  1.27  christos 	for (nin = i = 0; i < id->bNumEndpoints; i++) {
    618   1.9    martin 		ed = usbd_interface2endpoint_descriptor(ih, i);
    619  1.32     skrll 		if (ed == NULL)
    620  1.32     skrll 			return EIO;
    621   1.9    martin 
    622  1.27  christos 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    623  1.27  christos 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK &&
    624  1.27  christos 		    nin++ == portno) {
    625   1.9    martin 			/* Issue ENDPOINT_HALT request */
    626   1.9    martin 			req.bmRequestType = UT_WRITE_ENDPOINT;
    627   1.9    martin 			req.bRequest = UR_CLEAR_FEATURE;
    628   1.9    martin 			USETW(req.wValue, UF_ENDPOINT_HALT);
    629   1.9    martin 			USETW(req.wIndex, ed->bEndpointAddress);
    630   1.9    martin 			USETW(req.wLength, 0);
    631   1.9    martin 			err = usbd_do_request(sc->sc_udev, &req, 0);
    632   1.9    martin 			if (err)
    633  1.32     skrll 				return EIO;
    634   1.9    martin 		}
    635   1.1     joerg 	}
    636   1.9    martin 
    637  1.27  christos 	com->c_open = true;
    638  1.27  christos 	com->c_purging = true;
    639  1.27  christos 	getmicrotime(&com->c_purge_start);
    640   1.9    martin 
    641  1.32     skrll 	return 0;
    642   1.1     joerg }
    643   1.1     joerg 
    644   1.9    martin /*ARGSUSED*/
    645  1.32     skrll static void
    646   1.9    martin u3g_close(void *arg, int portno)
    647   1.9    martin {
    648   1.9    martin 	struct u3g_softc *sc = arg;
    649  1.27  christos 	struct u3g_com *com = &sc->sc_com[portno];
    650   1.9    martin 
    651  1.27  christos 	com->c_open = false;
    652   1.9    martin }
    653   1.9    martin 
    654   1.9    martin /*ARGSUSED*/
    655   1.9    martin static void
    656   1.9    martin u3g_read(void *arg, int portno, u_char **cpp, uint32_t *ccp)
    657   1.9    martin {
    658   1.9    martin 	struct u3g_softc *sc = arg;
    659   1.9    martin 	struct timeval curr_tv, diff_tv;
    660  1.27  christos 	struct u3g_com *com = &sc->sc_com[portno];
    661   1.9    martin 
    662   1.9    martin 	/*
    663   1.9    martin 	 * If we're not purging input data following first open, do nothing.
    664   1.9    martin 	 */
    665  1.27  christos 	if (com->c_purging == false)
    666   1.9    martin 		return;
    667   1.9    martin 
    668   1.9    martin 	/*
    669   1.9    martin 	 * Otherwise check if the purge timeout has expired
    670   1.9    martin 	 */
    671   1.9    martin 	getmicrotime(&curr_tv);
    672  1.27  christos 	timersub(&curr_tv, &com->c_purge_start, &diff_tv);
    673   1.9    martin 
    674   1.9    martin 	if (diff_tv.tv_sec >= U3G_PURGE_SECS) {
    675   1.9    martin 		/* Timeout expired. */
    676  1.27  christos 		com->c_purging = false;
    677   1.9    martin 	} else {
    678   1.9    martin 		/* Still purging. Adjust the caller's byte count. */
    679   1.9    martin 		*ccp = 0;
    680   1.9    martin 	}
    681   1.9    martin }
    682   1.9    martin 
    683   1.9    martin /*ARGSUSED*/
    684   1.9    martin static void
    685  1.32     skrll u3g_write(void *arg, int portno, u_char *to, u_char *from, uint32_t *count)
    686   1.9    martin {
    687   1.9    martin 	struct u3g_softc *sc = arg;
    688  1.27  christos 	struct u3g_com *com = &sc->sc_com[portno];
    689   1.9    martin 
    690   1.9    martin 	/*
    691   1.9    martin 	 * Stop purging as soon as the first data is written to the device.
    692   1.9    martin 	 */
    693  1.27  christos 	com->c_purging = false;
    694   1.9    martin 	memcpy(to, from, *count);
    695   1.9    martin }
    696