Home | History | Annotate | Line # | Download | only in usb
u3g.c revision 1.34.10.2
      1 /*	$NetBSD: u3g.c,v 1.34.10.2 2020/04/08 14:08:13 martin Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2009 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     19  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     20  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     21  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     22  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     28  * POSSIBILITY OF SUCH DAMAGE.
     29  */
     30 
     31 /*
     32  * Copyright (c) 2008 AnyWi Technologies
     33  *   Author: Andrea Guzzo <aguzzo (at) anywi.com>
     34  *   * based on uark.c 1.1 2006/08/14 08:30:22 jsg *
     35  *   * parts from ubsa.c 183348 2008-09-25 12:00:56Z phk *
     36  *
     37  * Permission to use, copy, modify, and distribute this software for any
     38  * purpose with or without fee is hereby granted, provided that the above
     39  * copyright notice and this permission notice appear in all copies.
     40  *
     41  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
     42  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     43  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
     44  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     45  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     46  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
     47  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     48  *
     49  * $FreeBSD$
     50  */
     51 
     52 #include <sys/cdefs.h>
     53 __KERNEL_RCSID(0, "$NetBSD: u3g.c,v 1.34.10.2 2020/04/08 14:08:13 martin Exp $");
     54 
     55 #include <sys/param.h>
     56 #include <sys/systm.h>
     57 #include <sys/kernel.h>
     58 #include <sys/kmem.h>
     59 #include <sys/bus.h>
     60 #include <sys/conf.h>
     61 #include <sys/tty.h>
     62 
     63 #include <dev/usb/usb.h>
     64 #include <dev/usb/usbdi.h>
     65 #include <dev/usb/usbdivar.h>
     66 #include <dev/usb/usbdi_util.h>
     67 
     68 #include <dev/usb/ucomvar.h>
     69 
     70 #include "usbdevs.h"
     71 
     72 /*
     73  * We read/write data from/to the device in 4KB chunks to maximise
     74  * performance.
     75  */
     76 #define U3G_BUFF_SIZE	4096
     77 
     78 /*
     79  * Some 3G devices (the Huawei E160/E220 springs to mind here) buffer up
     80  * data internally even when the USB pipes are closed. So on first open,
     81  * we can receive a large chunk of stale data.
     82  *
     83  * This causes a real problem because the default TTYDEF_LFLAG (applied
     84  * on first open) has the ECHO flag set, resulting in all the stale data
     85  * being echoed straight back to the device by the tty(4) layer. Some
     86  * devices (again, the Huawei E160/E220 for example) react to this spew
     87  * by going catatonic.
     88  *
     89  * All this happens before the application gets a chance to disable ECHO.
     90  *
     91  * We work around this by ignoring all data received from the device for
     92  * a period of two seconds, or until the application starts sending data -
     93  * whichever comes first.
     94  */
     95 #define	U3G_PURGE_SECS	2
     96 
     97 /*
     98  * Define bits for the virtual modem control pins.
     99  * The input pin states are reported via the interrupt pipe on some devices.
    100  */
    101 #define	U3G_OUTPIN_DTR	(1u << 0)
    102 #define	U3G_OUTPIN_RTS	(1u << 1)
    103 #define	U3G_INPIN_DCD	(1u << 0)
    104 #define	U3G_INPIN_DSR	(1u << 1)
    105 #define	U3G_INPIN_RI	(1u << 3)
    106 
    107 /*
    108  * USB request to set the output pin status
    109  */
    110 #define	U3G_SET_PIN	0x22
    111 
    112 struct u3g_softc {
    113 	device_t		sc_dev;
    114 	struct usbd_device *	sc_udev;
    115 	bool			sc_dying;	/* We're going away */
    116 	int			sc_ifaceno;	/* Device interface number */
    117 
    118 	struct u3g_com {
    119 		device_t	c_dev;		/* Child ucom(4) handle */
    120 
    121 		bool		c_open;		/* Device is in use */
    122 		bool		c_purging;	/* Purging stale data */
    123 		struct timeval	c_purge_start;	/* Control duration of purge */
    124 
    125 		u_char		c_msr;		/* Emulated 'msr' */
    126 		uint16_t	c_outpins;	/* Output pin state */
    127 	} sc_com[10];
    128 	size_t			sc_ncom;
    129 
    130 	struct usbd_pipe *	sc_intr_pipe;	/* Interrupt pipe */
    131 	u_char			*sc_intr_buff;	/* Interrupt buffer */
    132 	size_t			sc_intr_size;	/* buffer size */
    133 };
    134 
    135 /*
    136  * The device driver has two personalities. The first uses the 'usbdevif'
    137  * interface attribute so that a match will claim the entire USB device
    138  * for itself. This is used for when a device needs to be mode-switched
    139  * and ensures any other interfaces present cannot be claimed by other
    140  * drivers while the mode-switch is in progress. This is implemented by
    141  * the umodeswitch driver.
    142  *
    143  * The second personality uses the 'usbifif' interface attribute so that
    144  * it can claim the 3G modem interfaces for itself, leaving others (such
    145  * as the mass storage interfaces on some devices) for other drivers.
    146  */
    147 
    148 static int u3g_match(device_t, cfdata_t, void *);
    149 static void u3g_attach(device_t, device_t, void *);
    150 static int u3g_detach(device_t, int);
    151 static void u3g_childdet(device_t, device_t);
    152 
    153 CFATTACH_DECL2_NEW(u3g, sizeof(struct u3g_softc), u3g_match,
    154     u3g_attach, u3g_detach, NULL, NULL, u3g_childdet);
    155 
    156 
    157 static void u3g_intr(struct usbd_xfer *, void *, usbd_status);
    158 static void u3g_get_status(void *, int, u_char *, u_char *);
    159 static void u3g_set(void *, int, int, int);
    160 static int  u3g_open(void *, int);
    161 static void u3g_close(void *, int);
    162 static void u3g_read(void *, int, u_char **, uint32_t *);
    163 static void u3g_write(void *, int, u_char *, u_char *, uint32_t *);
    164 
    165 static const struct ucom_methods u3g_methods = {
    166 	.ucom_get_status = u3g_get_status,
    167 	.ucom_set = u3g_set,
    168 	.ucom_open = u3g_open,
    169 	.ucom_close = u3g_close,
    170 	.ucom_read = u3g_read,
    171 	.ucom_write = u3g_write,
    172 };
    173 
    174 /*
    175  * Allegedly supported devices
    176  */
    177 static const struct usb_devno u3g_devs[] = {
    178 	{ USB_VENDOR_DELL, USB_PRODUCT_DELL_W5500 },
    179 	{ USB_VENDOR_HP, USB_PRODUCT_HP_UN2430 },
    180 	/* OEM: Huawei */
    181 	{ USB_VENDOR_HUAWEI, USB_PRODUCT_HUAWEI_E1750 },
    182 	{ USB_VENDOR_HUAWEI, USB_PRODUCT_HUAWEI_E1820 },
    183 	{ USB_VENDOR_HUAWEI, USB_PRODUCT_HUAWEI_E220 },
    184 	{ USB_VENDOR_HUAWEI, USB_PRODUCT_HUAWEI_EM770W },
    185 	{ USB_VENDOR_HUAWEI, USB_PRODUCT_HUAWEI_K3765 },
    186 	{ USB_VENDOR_HUAWEI, USB_PRODUCT_HUAWEI_MOBILE },
    187 	{ USB_VENDOR_HUAWEI, USB_PRODUCT_HUAWEI_E171 },
    188 	{ USB_VENDOR_HUAWEI, USB_PRODUCT_HUAWEI_E353 },
    189 	/* LG Electronics */
    190 	{ USB_VENDOR_LG, USB_PRODUCT_LG_NTT_DOCOMO_L02C_MODEM },
    191 	/* OEM: Merlin */
    192 	{ USB_VENDOR_MERLIN, USB_PRODUCT_MERLIN_V620 },
    193 	/* OEM: Novatel */
    194 	{ USB_VENDOR_NOVATEL2, USB_PRODUCT_NOVATEL2_ES620 },
    195 	{ USB_VENDOR_NOVATEL2, USB_PRODUCT_NOVATEL2_EU8X0D },
    196 	{ USB_VENDOR_NOVATEL2, USB_PRODUCT_NOVATEL2_MC950D },
    197 #if 0
    198 	/* These are matched in u3ginit_match() */
    199 	{ USB_VENDOR_NOVATEL2, USB_PRODUCT_NOVATEL2_MC950D_DRIVER },
    200 	{ USB_VENDOR_NOVATEL2, USB_PRODUCT_NOVATEL2_U760_DRIVER },
    201 #endif
    202 	{ USB_VENDOR_NOVATEL2, USB_PRODUCT_NOVATEL2_MERLINU740 },
    203 	{ USB_VENDOR_NOVATEL2, USB_PRODUCT_NOVATEL2_MERLINV620 },
    204 	{ USB_VENDOR_NOVATEL2, USB_PRODUCT_NOVATEL2_S720 },
    205 	{ USB_VENDOR_NOVATEL2, USB_PRODUCT_NOVATEL2_U720 },
    206 	{ USB_VENDOR_NOVATEL2, USB_PRODUCT_NOVATEL2_U727 },
    207 	{ USB_VENDOR_NOVATEL2, USB_PRODUCT_NOVATEL2_U740_2 },
    208 	{ USB_VENDOR_NOVATEL2, USB_PRODUCT_NOVATEL2_U760 },
    209 	{ USB_VENDOR_NOVATEL2, USB_PRODUCT_NOVATEL2_U870 },
    210 	{ USB_VENDOR_NOVATEL2, USB_PRODUCT_NOVATEL2_V740 },
    211 	{ USB_VENDOR_NOVATEL2, USB_PRODUCT_NOVATEL2_X950D },
    212 	{ USB_VENDOR_NOVATEL2, USB_PRODUCT_NOVATEL2_XU870 },
    213 	/* OEM: Option N.V. */
    214 	{ USB_VENDOR_OPTIONNV, USB_PRODUCT_OPTIONNV_QUADPLUSUMTS },
    215 	{ USB_VENDOR_OPTIONNV, USB_PRODUCT_OPTIONNV_HSDPA },
    216 	{ USB_VENDOR_OPTIONNV, USB_PRODUCT_OPTIONNV_GTMAXHSUPA },
    217 
    218 	/* OEM: Sierra Wireless: */
    219 	{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AC595U },
    220 	{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AC597E },
    221 	{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AC875U },
    222 	{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AC880 },
    223 	{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AC880E },
    224 	{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AC880U },
    225 	{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AC881 },
    226 	{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AC881E },
    227 	{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AC881U },
    228 	{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AIRCARD580 },
    229 	{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AIRCARD595 },
    230 	{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AIRCARD875 },
    231 	{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_C597 },
    232 	{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_EM5625 },
    233 	{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC5720 },
    234 	{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC5720_2 },
    235 	{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC5725 },
    236 	{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC8755 },
    237 	{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC8755_2 },
    238 	{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC8755_3 },
    239 	{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC8765 },
    240 	{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC8775_2 },
    241 	{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC8780 },
    242 	{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC8781 },
    243 	{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MINI5725 },
    244 	{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_USB305 },
    245 	{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_250U },
    246 	/* Toshiba */
    247 	{ USB_VENDOR_TOSHIBA, USB_PRODUCT_TOSHIBA_HSDPA_MODEM_EU870DT1 },
    248 
    249 	/* ZTE */
    250 	{ USB_VENDOR_ZTE, USB_PRODUCT_ZTE_MF622 },
    251 	{ USB_VENDOR_ZTE, USB_PRODUCT_ZTE_MF626 },
    252 	{ USB_VENDOR_ZTE, USB_PRODUCT_ZTE_MF628 },
    253 	{ USB_VENDOR_ZTE, USB_PRODUCT_ZTE_MF820D },
    254 
    255 	/* 4G Systems */
    256 	{ USB_VENDOR_LONGCHEER, USB_PRODUCT_LONGCHEER_XSSTICK_P14 },
    257 	{ USB_VENDOR_LONGCHEER, USB_PRODUCT_LONGCHEER_XSSTICK_W14 },
    258 
    259 	/* DLink */
    260 	{ USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DWM157 },
    261 	{ USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DWM157E },
    262 };
    263 
    264 /*
    265  * Second personality:
    266  *
    267  * Claim only those interfaces required for 3G modem operation.
    268  */
    269 
    270 static int
    271 u3g_match(device_t parent, cfdata_t match, void *aux)
    272 {
    273 	struct usbif_attach_arg *uiaa = aux;
    274 	struct usbd_interface *iface;
    275 	usb_interface_descriptor_t *id;
    276 	usbd_status error;
    277 
    278 	if (!usb_lookup(u3g_devs, uiaa->uiaa_vendor, uiaa->uiaa_product))
    279 		return UMATCH_NONE;
    280 
    281 	error = usbd_device2interface_handle(uiaa->uiaa_device,
    282 	    uiaa->uiaa_ifaceno, &iface);
    283 	if (error) {
    284 		printf("u3g_match: failed to get interface, err=%s\n",
    285 		    usbd_errstr(error));
    286 		return UMATCH_NONE;
    287 	}
    288 
    289 	id = usbd_get_interface_descriptor(iface);
    290 	if (id == NULL) {
    291 		printf("u3g_match: failed to get interface descriptor\n");
    292 		return UMATCH_NONE;
    293 	}
    294 
    295 	/*
    296 	 * Huawei modems use the vendor-specific class for all interfaces,
    297 	 * both tty and CDC NCM, which we should avoid attaching to.
    298 	 */
    299 	if (uiaa->uiaa_vendor == USB_VENDOR_HUAWEI &&
    300 	    id->bInterfaceSubClass == 2 &&
    301 	    (id->bInterfaceProtocol & 0xf) == 6)	/* 0x16, 0x46, 0x76 */
    302 		return UMATCH_NONE;
    303 
    304 	/*
    305 	 * 3G modems generally report vendor-specific class
    306 	 *
    307 	 * XXX: this may be too generalised.
    308 	 */
    309 	return id->bInterfaceClass == UICLASS_VENDOR ?
    310 	    UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
    311 }
    312 
    313 static void
    314 u3g_attach(device_t parent, device_t self, void *aux)
    315 {
    316 	struct u3g_softc *sc = device_private(self);
    317 	struct usbif_attach_arg *uiaa = aux;
    318 	struct usbd_device *dev = uiaa->uiaa_device;
    319 	struct usbd_interface *iface;
    320 	usb_interface_descriptor_t *id;
    321 	usb_endpoint_descriptor_t *ed;
    322 	struct ucom_attach_args ucaa;
    323 	usbd_status error;
    324 	int n, intr_address, intr_size;
    325 
    326 	aprint_naive("\n");
    327 	aprint_normal("\n");
    328 
    329 	sc->sc_dev = self;
    330 	sc->sc_dying = false;
    331 	sc->sc_udev = dev;
    332 
    333 	error = usbd_device2interface_handle(dev, uiaa->uiaa_ifaceno, &iface);
    334 	if (error) {
    335 		aprint_error_dev(self, "failed to get interface, err=%s\n",
    336 		    usbd_errstr(error));
    337 		return;
    338 	}
    339 
    340 	id = usbd_get_interface_descriptor(iface);
    341 
    342 	ucaa.ucaa_info = "3G Modem";
    343 	ucaa.ucaa_ibufsize = U3G_BUFF_SIZE;
    344 	ucaa.ucaa_obufsize = U3G_BUFF_SIZE;
    345 	ucaa.ucaa_ibufsizepad = U3G_BUFF_SIZE;
    346 	ucaa.ucaa_opkthdrlen = 0;
    347 	ucaa.ucaa_device = dev;
    348 	ucaa.ucaa_iface = iface;
    349 	ucaa.ucaa_methods = &u3g_methods;
    350 	ucaa.ucaa_arg = sc;
    351 	ucaa.ucaa_portno = -1;
    352 	ucaa.ucaa_bulkin = ucaa.ucaa_bulkout = -1;
    353 
    354 	sc->sc_ifaceno = uiaa->uiaa_ifaceno;
    355 	intr_address = -1;
    356 	intr_size = 0;
    357 
    358 	for (n = 0; n < id->bNumEndpoints; n++) {
    359 		ed = usbd_interface2endpoint_descriptor(iface, n);
    360 		if (ed == NULL) {
    361 			aprint_error_dev(self, "no endpoint descriptor "
    362 			    "for %d (interface: %d)\n", n, sc->sc_ifaceno);
    363 			sc->sc_dying = true;
    364 			return;
    365 		}
    366 
    367 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    368 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
    369 			intr_address = ed->bEndpointAddress;
    370 			intr_size = UGETW(ed->wMaxPacketSize);
    371 		} else
    372 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    373 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
    374 			ucaa.ucaa_bulkin = ed->bEndpointAddress;
    375 		} else
    376 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
    377 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
    378 			ucaa.ucaa_bulkout = ed->bEndpointAddress;
    379 		}
    380 		if (ucaa.ucaa_bulkin != -1 && ucaa.ucaa_bulkout != -1) {
    381 			struct u3g_com *com;
    382 			if (sc->sc_ncom == __arraycount(sc->sc_com)) {
    383 				aprint_error_dev(self, "Need to configure "
    384 				    "more than %zu ttys", sc->sc_ncom);
    385 				continue;
    386 			}
    387 			ucaa.ucaa_portno = sc->sc_ncom++;
    388 			com = &sc->sc_com[ucaa.ucaa_portno];
    389 			com->c_outpins = 0;
    390 			com->c_msr = UMSR_DSR | UMSR_CTS | UMSR_DCD;
    391 			com->c_open = false;
    392 			com->c_purging = false;
    393 			com->c_dev = config_found_sm_loc(self, "ucombus",
    394 				NULL, &ucaa, ucomprint, ucomsubmatch);
    395 			ucaa.ucaa_bulkin = -1;
    396 			ucaa.ucaa_bulkout = -1;
    397 		}
    398 	}
    399 
    400 	if (sc->sc_ncom == 0) {
    401 		aprint_error_dev(self, "Missing bulk in/out for interface %d\n",
    402 		    sc->sc_ifaceno);
    403 		sc->sc_dying = true;
    404 		return;
    405 	}
    406 
    407 	/*
    408 	 * If the interface has an interrupt pipe, open it immediately so
    409 	 * that we can track input pin state changes regardless of whether
    410 	 * the tty(4) device is open or not.
    411 	 */
    412 	if (intr_address != -1) {
    413 		sc->sc_intr_size = intr_size;
    414 		sc->sc_intr_buff = kmem_alloc(intr_size, KM_SLEEP);
    415 		error = usbd_open_pipe_intr(iface, intr_address,
    416 		    USBD_SHORT_XFER_OK, &sc->sc_intr_pipe, sc, sc->sc_intr_buff,
    417 		    intr_size, u3g_intr, 100);
    418 		if (error) {
    419 			aprint_error_dev(self, "cannot open interrupt pipe "
    420 			    "(addr %d)\n", intr_address);
    421 			return;
    422 		}
    423 	} else {
    424 		sc->sc_intr_pipe = NULL;
    425 		sc->sc_intr_buff = NULL;
    426 	}
    427 
    428 	if (!pmf_device_register(self, NULL, NULL))
    429 		aprint_error_dev(self, "couldn't establish power handler\n");
    430 }
    431 
    432 static int
    433 u3g_detach(device_t self, int flags)
    434 {
    435 	struct u3g_softc *sc = device_private(self);
    436 	int rv = 0;
    437 
    438 	sc->sc_dying = true;
    439 
    440 	if (sc->sc_intr_pipe != NULL) {
    441 		usbd_abort_pipe(sc->sc_intr_pipe);
    442 		usbd_close_pipe(sc->sc_intr_pipe);
    443 		sc->sc_intr_pipe = NULL;
    444 	}
    445 	if (sc->sc_intr_buff != NULL) {
    446 		kmem_free(sc->sc_intr_buff, sc->sc_intr_size);
    447 		sc->sc_intr_buff = NULL;
    448 	}
    449 
    450 	for (size_t i = 0; i < sc->sc_ncom; i++)
    451 		if (sc->sc_com[i].c_dev != NULL) {
    452 			int port_rv;
    453 
    454 			port_rv = config_detach(sc->sc_com[i].c_dev, flags);
    455 			if (port_rv != 0) {
    456 				aprint_verbose_dev(self, "Can't deallocate "
    457 				    "port (%d)", port_rv);
    458 			}
    459 			rv |= port_rv;
    460 			sc->sc_com[i].c_dev = NULL;
    461 		}
    462 
    463 	pmf_device_deregister(self);
    464 
    465 	return rv;
    466 }
    467 
    468 static void
    469 u3g_childdet(device_t self, device_t child)
    470 {
    471 	struct u3g_softc *sc = device_private(self);
    472 
    473 	for (size_t i = 0; i < sc->sc_ncom; i++)
    474 		    if (sc->sc_com[i].c_dev == child)
    475 			    sc->sc_com[i].c_dev = NULL;
    476 }
    477 
    478 static void
    479 u3g_intr(struct usbd_xfer *xfer, void *priv, usbd_status status)
    480 {
    481 	struct u3g_softc *sc = (struct u3g_softc *)priv;
    482 	u_char *buf;
    483 	int portno = 0;	/* XXX */
    484 	struct u3g_com *com = &sc->sc_com[portno];
    485 
    486 	if (sc->sc_dying)
    487 		return;
    488 
    489 	if (status != USBD_NORMAL_COMPLETION) {
    490 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
    491 			return;
    492 		usbd_clear_endpoint_stall_async(sc->sc_intr_pipe);
    493 		return;
    494 	}
    495 
    496 	buf = sc->sc_intr_buff;
    497 	if (buf[0] == 0xa1 && buf[1] == 0x20) {
    498 		u_char msr;
    499 
    500 		msr = com->c_msr & ~(UMSR_DCD | UMSR_DSR | UMSR_RI);
    501 
    502 		if (buf[8] & U3G_INPIN_DCD)
    503 			msr |= UMSR_DCD;
    504 
    505 		if (buf[8] & U3G_INPIN_DSR)
    506 			msr |= UMSR_DSR;
    507 
    508 		if (buf[8] & U3G_INPIN_RI)
    509 			msr |= UMSR_RI;
    510 
    511 		if (msr != com->c_msr) {
    512 			com->c_msr = msr;
    513 			if (com->c_open)
    514 				ucom_status_change(device_private(com->c_dev));
    515 		}
    516 	}
    517 }
    518 
    519 /*ARGSUSED*/
    520 static void
    521 u3g_get_status(void *arg, int portno, u_char *lsr, u_char *msr)
    522 {
    523 	struct u3g_softc *sc = arg;
    524 
    525 	*lsr = 0;	/* LSR isn't supported */
    526 	*msr = sc->sc_com[portno].c_msr;
    527 }
    528 
    529 /*ARGSUSED*/
    530 static void
    531 u3g_set(void *arg, int portno, int reg, int onoff)
    532 {
    533 	struct u3g_softc *sc = arg;
    534 	usb_device_request_t req;
    535 	uint16_t mask, new_state;
    536 	usbd_status err;
    537 	struct u3g_com *com = &sc->sc_com[portno];
    538 
    539 	if (sc->sc_dying)
    540 		return;
    541 
    542 	switch (reg) {
    543 	case UCOM_SET_DTR:
    544 		mask = U3G_OUTPIN_DTR;
    545 		break;
    546 	case UCOM_SET_RTS:
    547 		mask = U3G_OUTPIN_RTS;
    548 		break;
    549 	default:
    550 		return;
    551 	}
    552 
    553 	new_state = com->c_outpins & ~mask;
    554 	if (onoff)
    555 		new_state |= mask;
    556 
    557 	if (new_state == com->c_outpins)
    558 		return;
    559 
    560 	com->c_outpins = new_state;
    561 
    562 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
    563 	req.bRequest = U3G_SET_PIN;
    564 	USETW(req.wValue, new_state);
    565 	USETW(req.wIndex, sc->sc_ifaceno);
    566 	USETW(req.wLength, 0);
    567 
    568 	err = usbd_do_request(sc->sc_udev, &req, 0);
    569 	if (err == USBD_STALLED)
    570 		usbd_clear_endpoint_stall(sc->sc_udev->ud_pipe0);
    571 }
    572 
    573 /*ARGSUSED*/
    574 static int
    575 u3g_open(void *arg, int portno)
    576 {
    577 	struct u3g_softc *sc = arg;
    578 	usb_endpoint_descriptor_t *ed;
    579 	usb_interface_descriptor_t *id;
    580 	struct usbd_interface *ih;
    581 	usbd_status err;
    582 	struct u3g_com *com = &sc->sc_com[portno];
    583 	int i, nin;
    584 
    585 	if (sc->sc_dying)
    586  		return EIO;
    587 
    588 	err = usbd_device2interface_handle(sc->sc_udev, sc->sc_ifaceno, &ih);
    589 	if (err)
    590 		return EIO;
    591 
    592 	id = usbd_get_interface_descriptor(ih);
    593 
    594 	for (nin = i = 0; i < id->bNumEndpoints; i++) {
    595 		ed = usbd_interface2endpoint_descriptor(ih, i);
    596 		if (ed == NULL)
    597 			return EIO;
    598 
    599 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    600 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK &&
    601 		    nin++ == portno) {
    602 			err = usbd_clear_endpoint_feature(sc->sc_udev,
    603 			    ed->bEndpointAddress, UF_ENDPOINT_HALT);
    604 			if (err)
    605 				return EIO;
    606 		}
    607 	}
    608 
    609 	com->c_open = true;
    610 	com->c_purging = true;
    611 	getmicrotime(&com->c_purge_start);
    612 
    613 	return 0;
    614 }
    615 
    616 /*ARGSUSED*/
    617 static void
    618 u3g_close(void *arg, int portno)
    619 {
    620 	struct u3g_softc *sc = arg;
    621 	struct u3g_com *com = &sc->sc_com[portno];
    622 
    623 	com->c_open = false;
    624 }
    625 
    626 /*ARGSUSED*/
    627 static void
    628 u3g_read(void *arg, int portno, u_char **cpp, uint32_t *ccp)
    629 {
    630 	struct u3g_softc *sc = arg;
    631 	struct timeval curr_tv, diff_tv;
    632 	struct u3g_com *com = &sc->sc_com[portno];
    633 
    634 	/*
    635 	 * If we're not purging input data following first open, do nothing.
    636 	 */
    637 	if (com->c_purging == false)
    638 		return;
    639 
    640 	/*
    641 	 * Otherwise check if the purge timeout has expired
    642 	 */
    643 	getmicrotime(&curr_tv);
    644 	timersub(&curr_tv, &com->c_purge_start, &diff_tv);
    645 
    646 	if (diff_tv.tv_sec >= U3G_PURGE_SECS) {
    647 		/* Timeout expired. */
    648 		com->c_purging = false;
    649 	} else {
    650 		/* Still purging. Adjust the caller's byte count. */
    651 		*ccp = 0;
    652 	}
    653 }
    654 
    655 /*ARGSUSED*/
    656 static void
    657 u3g_write(void *arg, int portno, u_char *to, u_char *from, uint32_t *count)
    658 {
    659 	struct u3g_softc *sc = arg;
    660 	struct u3g_com *com = &sc->sc_com[portno];
    661 
    662 	/*
    663 	 * Stop purging as soon as the first data is written to the device.
    664 	 */
    665 	com->c_purging = false;
    666 	memcpy(to, from, *count);
    667 }
    668