Home | History | Annotate | Line # | Download | only in usb
ubsa.c revision 1.41
      1 /*	$NetBSD: ubsa.c,v 1.41 2021/04/24 23:36:59 thorpej Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2002, Alexander Kabaev <kan.FreeBSD.org>.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  * SUCH DAMAGE.
     27  */
     28 /*
     29  * Copyright (c) 2001 The NetBSD Foundation, Inc.
     30  * All rights reserved.
     31  *
     32  * This code is derived from software contributed to The NetBSD Foundation
     33  * by Ichiro FUKUHARA (ichiro (at) ichiro.org).
     34  *
     35  * Redistribution and use in source and binary forms, with or without
     36  * modification, are permitted provided that the following conditions
     37  * are met:
     38  * 1. Redistributions of source code must retain the above copyright
     39  *    notice, this list of conditions and the following disclaimer.
     40  * 2. Redistributions in binary form must reproduce the above copyright
     41  *    notice, this list of conditions and the following disclaimer in the
     42  *    documentation and/or other materials provided with the distribution.
     43  *
     44  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     45  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     46  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     47  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     48  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     49  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     50  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     51  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     52  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     53  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     54  * POSSIBILITY OF SUCH DAMAGE.
     55  */
     56 
     57 #include <sys/cdefs.h>
     58 __KERNEL_RCSID(0, "$NetBSD: ubsa.c,v 1.41 2021/04/24 23:36:59 thorpej Exp $");
     59 
     60 #ifdef _KERNEL_OPT
     61 #include "opt_usb.h"
     62 #endif
     63 
     64 #include <sys/param.h>
     65 #include <sys/systm.h>
     66 #include <sys/kernel.h>
     67 #include <sys/kmem.h>
     68 #include <sys/ioccom.h>
     69 #include <sys/fcntl.h>
     70 #include <sys/conf.h>
     71 #include <sys/tty.h>
     72 #include <sys/file.h>
     73 #include <sys/select.h>
     74 #include <sys/proc.h>
     75 #include <sys/device.h>
     76 #include <sys/poll.h>
     77 #include <sys/sysctl.h>
     78 
     79 #include <dev/usb/usb.h>
     80 #include <dev/usb/usbcdc.h>
     81 
     82 #include <dev/usb/usbdi.h>
     83 #include <dev/usb/usbdi_util.h>
     84 #include <dev/usb/usbdevs.h>
     85 #include <dev/usb/usb_quirks.h>
     86 
     87 #include <dev/usb/ucomvar.h>
     88 #include <dev/usb/ubsavar.h>
     89 
     90 #ifdef UBSA_DEBUG
     91 int		ubsadebug = 0;
     92 
     93 #define	DPRINTFN(n, x)	do { \
     94 				if (ubsadebug > (n)) \
     95 					printf x; \
     96 			} while (0)
     97 #else
     98 #define	DPRINTFN(n, x)
     99 #endif
    100 #define	DPRINTF(x) DPRINTFN(0, x)
    101 
    102 static const struct	ucom_methods ubsa_methods = {
    103 	.ucom_get_status = ubsa_get_status,
    104 	.ucom_set = ubsa_set,
    105 	.ucom_param = ubsa_param,
    106 	.ucom_open = ubsa_open,
    107 	.ucom_close = ubsa_close,
    108 };
    109 
    110 Static const struct usb_devno ubsa_devs[] = {
    111 	/* BELKIN F5U103 */
    112 	{ USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5U103 },
    113 	/* BELKIN F5U120 */
    114 	{ USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5U120 },
    115 	/* GoHubs GO-COM232 */
    116 	{ USB_VENDOR_ETEK, USB_PRODUCT_ETEK_1COM },
    117 	/* GoHubs GO-COM232 */
    118 	{ USB_VENDOR_GOHUBS, USB_PRODUCT_GOHUBS_GOCOM232 },
    119 	/* Peracom */
    120 	{ USB_VENDOR_PERACOM, USB_PRODUCT_PERACOM_SERIAL1 },
    121 	/* Option N.V. */
    122 	{ USB_VENDOR_OPTIONNV, USB_PRODUCT_OPTIONNV_MC3G },
    123 	{ USB_VENDOR_OPTIONNV, USB_PRODUCT_OPTIONNV_QUADUMTS2 },
    124 	{ USB_VENDOR_OPTIONNV, USB_PRODUCT_OPTIONNV_QUADUMTS },
    125 	/* AnyDATA ADU-E100H */
    126 	{ USB_VENDOR_ANYDATA, USB_PRODUCT_ANYDATA_ADU_E100H },
    127 };
    128 #define ubsa_lookup(v, p) usb_lookup(ubsa_devs, v, p)
    129 
    130 int ubsa_match(device_t, cfdata_t, void *);
    131 void ubsa_attach(device_t, device_t, void *);
    132 void ubsa_childdet(device_t, device_t);
    133 int ubsa_detach(device_t, int);
    134 
    135 CFATTACH_DECL2_NEW(ubsa, sizeof(struct ubsa_softc),
    136     ubsa_match, ubsa_attach, ubsa_detach, NULL, NULL, ubsa_childdet);
    137 
    138 int
    139 ubsa_match(device_t parent, cfdata_t match, void *aux)
    140 {
    141 	struct usb_attach_arg *uaa = aux;
    142 
    143 	return (ubsa_lookup(uaa->uaa_vendor, uaa->uaa_product) != NULL ?
    144 		UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
    145 }
    146 
    147 void
    148 ubsa_attach(device_t parent, device_t self, void *aux)
    149 {
    150 	struct ubsa_softc *sc = device_private(self);
    151 	struct usb_attach_arg *uaa = aux;
    152 	struct usbd_device *dev = uaa->uaa_device;
    153 	usb_config_descriptor_t *cdesc;
    154 	usb_interface_descriptor_t *id;
    155 	usb_endpoint_descriptor_t *ed;
    156 	char *devinfop;
    157 	usbd_status err;
    158 	struct ucom_attach_args ucaa;
    159 	int i;
    160 
    161 	sc->sc_dev = self;
    162 	sc->sc_dying = false;
    163 
    164 	aprint_naive("\n");
    165 	aprint_normal("\n");
    166 
    167 	devinfop = usbd_devinfo_alloc(dev, 0);
    168 	aprint_normal_dev(self, "%s\n", devinfop);
    169 	usbd_devinfo_free(devinfop);
    170 
    171         sc->sc_udev = dev;
    172 	sc->sc_config_index = UBSA_DEFAULT_CONFIG_INDEX;
    173 	sc->sc_numif = 1; /* default device has one interface */
    174 
    175 	/*
    176 	 * initialize rts, dtr variables to something
    177 	 * different from boolean 0, 1
    178 	 */
    179 	sc->sc_dtr = -1;
    180 	sc->sc_rts = -1;
    181 
    182 	/*
    183 	 * Quad UMTS cards use different requests to
    184 	 * control com settings and only some.
    185 	 */
    186 	sc->sc_quadumts = 0;
    187 	if (uaa->uaa_vendor == USB_VENDOR_OPTIONNV) {
    188 		switch (uaa->uaa_product) {
    189 		case USB_PRODUCT_OPTIONNV_QUADUMTS:
    190 		case USB_PRODUCT_OPTIONNV_QUADUMTS2:
    191 			sc->sc_quadumts = 1;
    192 			break;
    193 		}
    194 	}
    195 
    196 	DPRINTF(("ubsa attach: sc = %p\n", sc));
    197 
    198 	/* Move the device into the configured state. */
    199 	err = usbd_set_config_index(dev, sc->sc_config_index, 1);
    200 	if (err) {
    201 		aprint_error_dev(self,
    202 		    "failed to set configuration: %s\n",
    203 		    usbd_errstr(err));
    204 		goto error;
    205 	}
    206 
    207 	/* get the config descriptor */
    208 	cdesc = usbd_get_config_descriptor(sc->sc_udev);
    209 
    210 	if (cdesc == NULL) {
    211 		aprint_error_dev(self,
    212 		    "failed to get configuration descriptor\n");
    213 		goto error;
    214 	}
    215 
    216 	sc->sc_intr_number = -1;
    217 	sc->sc_intr_pipe = NULL;
    218 
    219 	/* get the interfaces */
    220 	err = usbd_device2interface_handle(dev, UBSA_IFACE_INDEX_OFFSET,
    221 			 &sc->sc_iface[0]);
    222 	if (err) {
    223 		/* can not get main interface */
    224 		goto error;
    225 	}
    226 
    227 	/* Find the endpoints */
    228 	id = usbd_get_interface_descriptor(sc->sc_iface[0]);
    229 	sc->sc_iface_number[0] = id->bInterfaceNumber;
    230 
    231 	/* initialize endpoints */
    232 	ucaa.ucaa_bulkin = ucaa.ucaa_bulkout = -1;
    233 
    234 	for (i = 0; i < id->bNumEndpoints; i++) {
    235 		ed = usbd_interface2endpoint_descriptor(sc->sc_iface[0], i);
    236 		if (ed == NULL) {
    237 			aprint_error_dev(self,
    238 			     "no endpoint descriptor for %d\n", i);
    239 			break;
    240 		}
    241 
    242 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    243 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
    244 			sc->sc_intr_number = ed->bEndpointAddress;
    245 			sc->sc_isize = UGETW(ed->wMaxPacketSize);
    246 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    247 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
    248 			ucaa.ucaa_bulkin = ed->bEndpointAddress;
    249 			ucaa.ucaa_ibufsize = UGETW(ed->wMaxPacketSize);
    250 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
    251 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
    252 			ucaa.ucaa_bulkout = ed->bEndpointAddress;
    253 			ucaa.ucaa_obufsize = UGETW(ed->wMaxPacketSize);
    254 		}
    255 	} /* end of Endpoint loop */
    256 
    257 	if (sc->sc_intr_number == -1) {
    258 		aprint_error_dev(self, "Could not find interrupt in\n");
    259 		goto error;
    260 	}
    261 
    262 	if (ucaa.ucaa_bulkin == -1) {
    263 		aprint_error_dev(self, "Could not find data bulk in\n");
    264 		goto error;
    265 	}
    266 
    267 	if (ucaa.ucaa_bulkout == -1) {
    268 		aprint_error_dev(self, "Could not find data bulk out\n");
    269 		goto error;
    270 	}
    271 
    272 	ucaa.ucaa_portno = 0;
    273 	/* bulkin, bulkout set above */
    274 	ucaa.ucaa_ibufsizepad = ucaa.ucaa_ibufsize;
    275 	ucaa.ucaa_opkthdrlen = 0;
    276 	ucaa.ucaa_device = dev;
    277 	ucaa.ucaa_iface = sc->sc_iface[0];
    278 	ucaa.ucaa_methods = &ubsa_methods;
    279 	ucaa.ucaa_arg = sc;
    280 	ucaa.ucaa_info = NULL;
    281 	DPRINTF(("ubsa: int#=%d, in = %#x, out = %#x, intr = %#x\n",
    282 	    i, ucaa.ucaa_bulkin, ucaa.ucaa_bulkout, sc->sc_intr_number));
    283 	sc->sc_subdevs[0] = config_found(self, &ucaa, ucomprint,
    284 					 CFARG_SUBMATCH, ucomsubmatch,
    285 					 CFARG_EOL);
    286 
    287 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, sc->sc_dev);
    288 
    289 	return;
    290 
    291 error:
    292 	sc->sc_dying = true;
    293 	return;
    294 }
    295 
    296 
    297 void
    298 ubsa_childdet(device_t self, device_t child)
    299 {
    300 	int i;
    301 	struct ubsa_softc *sc = device_private(self);
    302 
    303 	for (i = 0; i < sc->sc_numif; i++) {
    304 		if (sc->sc_subdevs[i] == child)
    305 			break;
    306 	}
    307 	KASSERT(i < sc->sc_numif);
    308 		sc->sc_subdevs[i] = NULL;
    309 }
    310 
    311 int
    312 ubsa_detach(device_t self, int flags)
    313 {
    314 	struct ubsa_softc *sc = device_private(self);
    315 	int i;
    316 	int rv = 0;
    317 
    318 	DPRINTF(("ubsa_detach: sc = %p\n", sc));
    319 
    320 	sc->sc_dying = true;
    321 
    322 	ubsa_close_pipe(sc);
    323 
    324 	for (i = 0; i < sc->sc_numif; i++) {
    325 		if (sc->sc_subdevs[i] != NULL) {
    326 			rv |= config_detach(sc->sc_subdevs[i], flags);
    327 			sc->sc_subdevs[i] = NULL;
    328 		}
    329 	}
    330 
    331 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, sc->sc_dev);
    332 
    333 	return rv;
    334 }
    335