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