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