Home | History | Annotate | Line # | Download | only in usb
ubsa.c revision 1.19.18.1
      1 /*	$NetBSD: ubsa.c,v 1.19.18.1 2008/02/18 21:06:25 mjf 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  * 3. All advertising materials mentioning features or use of this software
     43  *    must display the following acknowledgement:
     44  *        This product includes software developed by the NetBSD
     45  *        Foundation, Inc. and its contributors.
     46  * 4. Neither the name of The NetBSD Foundation nor the names of its
     47  *    contributors may be used to endorse or promote products derived
     48  *    from this software without specific prior written permission.
     49  *
     50  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     51  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     52  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     53  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     54  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     55  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     56  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     57  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     58  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     59  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     60  * POSSIBILITY OF SUCH DAMAGE.
     61  */
     62 
     63 #include <sys/cdefs.h>
     64 __KERNEL_RCSID(0, "$NetBSD: ubsa.c,v 1.19.18.1 2008/02/18 21:06:25 mjf Exp $");
     65 
     66 #include <sys/param.h>
     67 #include <sys/systm.h>
     68 #include <sys/kernel.h>
     69 #include <sys/malloc.h>
     70 #ifdef __FreeBSD__
     71 #include <sys/bus.h>
     72 #endif
     73 #include <sys/ioccom.h>
     74 #include <sys/fcntl.h>
     75 #include <sys/conf.h>
     76 #include <sys/tty.h>
     77 #include <sys/file.h>
     78 #if __FreeBSD_version >= 500014
     79 #include <sys/selinfo.h>
     80 #else
     81 #include <sys/select.h>
     82 #endif
     83 #include <sys/proc.h>
     84 #include <sys/device.h>
     85 #include <sys/poll.h>
     86 #include <sys/sysctl.h>
     87 
     88 #include <dev/usb/usb.h>
     89 #include <dev/usb/usbcdc.h>
     90 
     91 #include <dev/usb/usbdi.h>
     92 #include <dev/usb/usbdi_util.h>
     93 #include <dev/usb/usbdevs.h>
     94 #include <dev/usb/usb_quirks.h>
     95 
     96 #include <dev/usb/ucomvar.h>
     97 #include <dev/usb/ubsavar.h>
     98 
     99 #ifdef UBSA_DEBUG
    100 Static int	ubsadebug = 0;
    101 #ifdef __FreeBSD__
    102 SYSCTL_NODE(_hw_usb, OID_AUTO, ubsa, CTLFLAG_RW, 0, "USB ubsa");
    103 SYSCTL_INT(_hw_usb_ubsa, OID_AUTO, debug, CTLFLAG_RW,
    104 	   &ubsadebug, 0, "ubsa debug level");
    105 #endif
    106 
    107 #define	DPRINTFN(n, x)	do { \
    108 				if (ubsadebug > (n)) \
    109 					logprintf x; \
    110 			} while (0)
    111 #else
    112 #define	DPRINTFN(n, x)
    113 #endif
    114 #define	DPRINTF(x) DPRINTFN(0, x)
    115 
    116 struct	ucom_methods ubsa_methods = {
    117 	ubsa_get_status,
    118 	ubsa_set,
    119 	ubsa_param,
    120 	NULL,
    121 	ubsa_open,
    122 	ubsa_close,
    123 	NULL,
    124 	NULL
    125 };
    126 
    127 Static const struct usb_devno ubsa_devs[] = {
    128 	/* BELKIN F5U103 */
    129 	{ USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5U103 },
    130 	/* BELKIN F5U120 */
    131 	{ USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5U120 },
    132 	/* GoHubs GO-COM232 */
    133 	{ USB_VENDOR_ETEK, USB_PRODUCT_ETEK_1COM },
    134 	/* GoHubs GO-COM232 */
    135 	{ USB_VENDOR_GOHUBS, USB_PRODUCT_GOHUBS_GOCOM232 },
    136 	/* Peracom */
    137 	{ USB_VENDOR_PERACOM, USB_PRODUCT_PERACOM_SERIAL1 },
    138 	/* Option N.V. */
    139 	{ USB_VENDOR_OPTIONNV, USB_PRODUCT_OPTIONNV_MC3G },
    140 	{ USB_VENDOR_OPTIONNV, USB_PRODUCT_OPTIONNV_QUADUMTS2 },
    141 	{ USB_VENDOR_OPTIONNV, USB_PRODUCT_OPTIONNV_QUADUMTS },
    142 	/* AnyDATA ADU-E100H */
    143 	{ USB_VENDOR_ANYDATA, USB_PRODUCT_ANYDATA_ADU_E100H },
    144 };
    145 #define ubsa_lookup(v, p) usb_lookup(ubsa_devs, v, p)
    146 
    147 int ubsa_match(device_t, struct cfdata *, void *);
    148 void ubsa_attach(device_t, device_t, void *);
    149 void ubsa_childdet(device_t, device_t);
    150 int ubsa_detach(device_t, int);
    151 int ubsa_activate(device_t, enum devact);
    152 
    153 extern struct cfdriver ubsa_cd;
    154 
    155 CFATTACH_DECL2(ubsa, sizeof(struct ubsa_softc),
    156     ubsa_match, ubsa_attach, ubsa_detach, ubsa_activate, NULL, ubsa_childdet);
    157 
    158 USB_MATCH(ubsa)
    159 {
    160 	USB_MATCH_START(ubsa, uaa);
    161 
    162 	return (ubsa_lookup(uaa->vendor, uaa->product) != NULL ?
    163 		UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
    164 }
    165 
    166 USB_ATTACH(ubsa)
    167 {
    168 	USB_ATTACH_START(ubsa, sc, uaa);
    169 	usbd_device_handle dev = uaa->device;
    170 	usb_config_descriptor_t *cdesc;
    171 	usb_interface_descriptor_t *id;
    172 	usb_endpoint_descriptor_t *ed;
    173 	char *devinfop;
    174 	const char *devname = USBDEVNAME(sc->sc_dev);
    175 	usbd_status err;
    176 	struct ucom_attach_args uca;
    177 	int i;
    178 
    179 	devinfop = usbd_devinfo_alloc(dev, 0);
    180 	USB_ATTACH_SETUP;
    181 	printf("%s: %s\n", devname, devinfop);
    182 	usbd_devinfo_free(devinfop);
    183 
    184         sc->sc_udev = dev;
    185 	sc->sc_config_index = UBSA_DEFAULT_CONFIG_INDEX;
    186 	sc->sc_numif = 1; /* default device has one interface */
    187 
    188 	/*
    189 	 * initialize rts, dtr variables to something
    190 	 * different from boolean 0, 1
    191 	 */
    192 	sc->sc_dtr = -1;
    193 	sc->sc_rts = -1;
    194 
    195 	/*
    196 	 * Quad UMTS cards use different requests to
    197 	 * control com settings and only some.
    198 	 */
    199 	sc->sc_quadumts = 0;
    200 	if (uaa->vendor == USB_VENDOR_OPTIONNV) {
    201 		switch (uaa->product) {
    202 		case USB_PRODUCT_OPTIONNV_QUADUMTS:
    203 		case USB_PRODUCT_OPTIONNV_QUADUMTS2:
    204 			sc->sc_quadumts = 1;
    205 			break;
    206 		}
    207 	}
    208 
    209 	DPRINTF(("ubsa attach: sc = %p\n", sc));
    210 
    211 	/* Move the device into the configured state. */
    212 	err = usbd_set_config_index(dev, sc->sc_config_index, 1);
    213 	if (err) {
    214 		printf("%s: failed to set configuration: %s\n",
    215 		    devname, usbd_errstr(err));
    216 		sc->sc_dying = 1;
    217 		goto error;
    218 	}
    219 
    220 	/* get the config descriptor */
    221 	cdesc = usbd_get_config_descriptor(sc->sc_udev);
    222 
    223 	if (cdesc == NULL) {
    224 		printf("%s: failed to get configuration descriptor\n",
    225 		    devname);
    226 		sc->sc_dying = 1;
    227 		goto error;
    228 	}
    229 
    230 	sc->sc_intr_number = -1;
    231 	sc->sc_intr_pipe = NULL;
    232 
    233 	/* get the interfaces */
    234 	err = usbd_device2interface_handle(dev, UBSA_IFACE_INDEX_OFFSET,
    235 			 &sc->sc_iface[0]);
    236 	if (err) {
    237 		/* can not get main interface */
    238 		sc->sc_dying = 1;
    239 		goto error;
    240 	}
    241 
    242 	/* Find the endpoints */
    243 	id = usbd_get_interface_descriptor(sc->sc_iface[0]);
    244 	sc->sc_iface_number[0] = id->bInterfaceNumber;
    245 
    246 	/* initialize endpoints */
    247 	uca.bulkin = uca.bulkout = -1;
    248 
    249 	for (i = 0; i < id->bNumEndpoints; i++) {
    250 		ed = usbd_interface2endpoint_descriptor(sc->sc_iface[0], i);
    251 		if (ed == NULL) {
    252 			printf("%s: no endpoint descriptor for %d\n",
    253 		    		USBDEVNAME(sc->sc_dev), i);
    254 			break;
    255 		}
    256 
    257 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    258 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
    259 			sc->sc_intr_number = ed->bEndpointAddress;
    260 			sc->sc_isize = UGETW(ed->wMaxPacketSize);
    261 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    262 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
    263 			uca.bulkin = ed->bEndpointAddress;
    264 			uca.ibufsize = UGETW(ed->wMaxPacketSize);
    265 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
    266 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
    267 			uca.bulkout = ed->bEndpointAddress;
    268 			uca.obufsize = UGETW(ed->wMaxPacketSize);
    269 		}
    270 	} /* end of Endpoint loop */
    271 
    272 	if (sc->sc_intr_number == -1) {
    273 		printf("%s: Could not find interrupt in\n", devname);
    274 		sc->sc_dying = 1;
    275 		goto error;
    276 	}
    277 
    278 	if (uca.bulkin == -1) {
    279 		printf("%s: Could not find data bulk in\n", devname);
    280 		sc->sc_dying = 1;
    281 		goto error;
    282 	}
    283 
    284 	if (uca.bulkout == -1) {
    285 		printf("%s: Could not find data bulk out\n", devname);
    286 		sc->sc_dying = 1;
    287 		goto error;
    288 	}
    289 
    290 	uca.portno = 0;
    291 	/* bulkin, bulkout set above */
    292 	uca.ibufsizepad = uca.ibufsize;
    293 	uca.opkthdrlen = 0;
    294 	uca.device = dev;
    295 	uca.iface = sc->sc_iface[0];
    296 	uca.methods = &ubsa_methods;
    297 	uca.arg = sc;
    298 	uca.info = NULL;
    299 	DPRINTF(("ubsa: int#=%d, in = 0x%x, out = 0x%x, intr = 0x%x\n",
    300     		i, uca.bulkin, uca.bulkout, sc->sc_intr_number));
    301 	sc->sc_subdevs[0] = config_found_sm_loc(self, "ucombus", NULL, &uca,
    302 				    ucomprint, ucomsubmatch);
    303 
    304 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
    305 			   USBDEV(sc->sc_dev));
    306 
    307 	USB_ATTACH_SUCCESS_RETURN;
    308 
    309 error:
    310 	USB_ATTACH_ERROR_RETURN;
    311 }
    312 
    313 
    314 void
    315 ubsa_childdet(device_t self, device_t child)
    316 {
    317 	int i;
    318 	struct ubsa_softc *sc = device_private(self);
    319 
    320 	for (i = 0; i < sc->sc_numif; i++) {
    321 		if (sc->sc_subdevs[i] == child)
    322 			break;
    323 	}
    324 	KASSERT(i < sc->sc_numif);
    325 		sc->sc_subdevs[i] = NULL;
    326 }
    327 
    328 USB_DETACH(ubsa)
    329 {
    330 	USB_DETACH_START(ubsa, sc);
    331 	int i;
    332 	int rv = 0;
    333 
    334 
    335 	DPRINTF(("ubsa_detach: sc = %p\n", sc));
    336 
    337 	if (sc->sc_intr_pipe != NULL) {
    338 		usbd_abort_pipe(sc->sc_intr_pipe);
    339 		usbd_close_pipe(sc->sc_intr_pipe);
    340 		free(sc->sc_intr_buf, M_USBDEV);
    341 		sc->sc_intr_pipe = NULL;
    342 	}
    343 
    344 	sc->sc_dying = 1;
    345 	for (i = 0; i < sc->sc_numif; i++) {
    346 		if (sc->sc_subdevs[i] != NULL)
    347 			rv |= config_detach(sc->sc_subdevs[i], flags);
    348 	}
    349 
    350 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
    351 			   USBDEV(sc->sc_dev));
    352 
    353 	return (rv);
    354 }
    355 
    356 int
    357 ubsa_activate(device_ptr_t self, enum devact act)
    358 {
    359 	struct ubsa_softc *sc = device_private(self);
    360 	int rv = 0;
    361 	int i;
    362 
    363 	switch (act) {
    364 	case DVACT_ACTIVATE:
    365 		return (EOPNOTSUPP);
    366 
    367 	case DVACT_DEACTIVATE:
    368 		for (i = 0; i < sc->sc_numif; i++) {
    369 			if (sc->sc_subdevs[i] != NULL)
    370 				rv |= config_deactivate(sc->sc_subdevs[i]);
    371 		}
    372 		sc->sc_dying = 1;
    373 		break;
    374 	}
    375 	return (rv);
    376 }
    377 
    378