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