Home | History | Annotate | Line # | Download | only in usb
ugensa.c revision 1.31.4.1
      1  1.31.4.1       snj /*	$NetBSD: ugensa.c,v 1.31.4.1 2017/04/05 19:54:20 snj Exp $	*/
      2       1.1     elric 
      3       1.1     elric /*
      4       1.1     elric  * Copyright (c) 2004, 2005 The NetBSD Foundation, Inc.
      5       1.1     elric  * All rights reserved.
      6       1.1     elric  *
      7       1.1     elric  * This code is derived from software contributed to The NetBSD Foundation
      8       1.1     elric  * by Roland C. Dowdeswell <elric (at) netbsd.org>.
      9       1.1     elric  *
     10       1.1     elric  * Redistribution and use in source and binary forms, with or without
     11       1.1     elric  * modification, are permitted provided that the following conditions
     12       1.1     elric  * are met:
     13       1.1     elric  * 1. Redistributions of source code must retain the above copyright
     14       1.1     elric  *    notice, this list of conditions and the following disclaimer.
     15       1.1     elric  * 2. Redistributions in binary form must reproduce the above copyright
     16       1.1     elric  *    notice, this list of conditions and the following disclaimer in the
     17       1.1     elric  *    documentation and/or other materials provided with the distribution.
     18       1.1     elric  *
     19       1.1     elric  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20       1.1     elric  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21       1.1     elric  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22       1.1     elric  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23       1.1     elric  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24       1.1     elric  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25       1.1     elric  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26       1.1     elric  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27       1.1     elric  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28       1.1     elric  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29       1.1     elric  * POSSIBILITY OF SUCH DAMAGE.
     30       1.1     elric  */
     31       1.1     elric 
     32      1.13     lukem #include <sys/cdefs.h>
     33  1.31.4.1       snj __KERNEL_RCSID(0, "$NetBSD: ugensa.c,v 1.31.4.1 2017/04/05 19:54:20 snj Exp $");
     34  1.31.4.1       snj 
     35  1.31.4.1       snj #ifdef _KERNEL_OPT
     36  1.31.4.1       snj #include "opt_usb.h"
     37  1.31.4.1       snj #endif
     38      1.13     lukem 
     39       1.1     elric #include <sys/param.h>
     40       1.1     elric #include <sys/systm.h>
     41       1.1     elric #include <sys/kernel.h>
     42       1.1     elric #include <sys/device.h>
     43       1.1     elric #include <sys/conf.h>
     44       1.1     elric #include <sys/tty.h>
     45       1.1     elric 
     46       1.1     elric #include <dev/usb/usb.h>
     47       1.1     elric 
     48       1.1     elric #include <dev/usb/usbdi.h>
     49       1.1     elric #include <dev/usb/usbdi_util.h>
     50       1.1     elric #include <dev/usb/usbdevs.h>
     51       1.1     elric 
     52       1.1     elric #include <dev/usb/ucomvar.h>
     53       1.1     elric 
     54       1.1     elric #ifdef UGENSA_DEBUG
     55       1.1     elric #define DPRINTF(x)	if (ugensadebug) printf x
     56       1.1     elric #define DPRINTFN(n,x)	if (ugensadebug>(n)) printf x
     57       1.1     elric int ugensadebug = 0;
     58       1.1     elric #else
     59       1.1     elric #define DPRINTF(x)
     60       1.1     elric #define DPRINTFN(n,x)
     61       1.1     elric #endif
     62       1.1     elric 
     63       1.1     elric struct ugensa_softc {
     64      1.28    dyoung 	device_t		sc_dev;		/* base device */
     65  1.31.4.1       snj 	struct usbd_device *	sc_udev;	/* device */
     66  1.31.4.1       snj 	struct usbd_interface *	sc_iface;	/* interface */
     67       1.1     elric 
     68      1.15    dyoung 	device_t		sc_subdev;
     69       1.1     elric 	int			sc_numcon;
     70       1.1     elric 
     71       1.1     elric 	u_char			sc_dying;
     72       1.1     elric };
     73       1.1     elric 
     74       1.1     elric struct ucom_methods ugensa_methods = {
     75  1.31.4.1       snj 	.ucom_get_status = NULL,
     76  1.31.4.1       snj 	.ucom_set = NULL,
     77  1.31.4.1       snj 	.ucom_param = NULL,
     78  1.31.4.1       snj 	.ucom_ioctl = NULL,
     79  1.31.4.1       snj 	.ucom_open = NULL,
     80  1.31.4.1       snj 	.ucom_close = NULL,
     81  1.31.4.1       snj 	.ucom_read = NULL,
     82  1.31.4.1       snj 	.ucom_write = NULL,
     83       1.1     elric };
     84       1.1     elric 
     85       1.1     elric #define UGENSA_CONFIG_INDEX	0
     86       1.1     elric #define UGENSA_IFACE_INDEX	0
     87       1.1     elric #define UGENSA_BUFSIZE		1024
     88       1.1     elric 
     89      1.16     elric struct ugensa_type {
     90      1.16     elric 	struct usb_devno	ugensa_dev;
     91  1.31.4.1       snj 	uint16_t		ugensa_flags;
     92      1.16     elric #define UNTESTED		0x0001
     93       1.1     elric };
     94      1.16     elric 
     95      1.16     elric static const struct ugensa_type ugensa_devs[] = {
     96      1.16     elric 	{{ USB_VENDOR_AIRPRIME, USB_PRODUCT_AIRPRIME_PC5220 }, 0 },
     97      1.27       riz 	{{ USB_VENDOR_DELL, USB_PRODUCT_DELL_HSDPA }, 0 },
     98      1.16     elric 	{{ USB_VENDOR_NOVATEL, USB_PRODUCT_NOVATEL_FLEXPACKGPS }, 0 },
     99      1.16     elric 	{{ USB_VENDOR_QUALCOMM_K, USB_PRODUCT_QUALCOMM_K_CDMA_MSM_K }, 0 },
    100      1.26       riz 	{{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_USB305 }, 0 },
    101      1.31     soren 	{{ USB_VENDOR_ZTE, USB_PRODUCT_ZTE_AC8700 }, 0 },
    102      1.16     elric 
    103      1.16     elric 	/*
    104      1.16     elric 	 * The following devices are untested, but they are purported to
    105      1.16     elric 	 * to work in similar device drivers on other OSes:
    106      1.16     elric 	 */
    107      1.16     elric 
    108  1.31.4.1       snj 	{{ USB_VENDOR_ANYDATA, USB_PRODUCT_ANYDATA_ADU_500A }, UNTESTED },
    109  1.31.4.1       snj 	{{ USB_VENDOR_NOVATEL2, USB_PRODUCT_NOVATEL2_EXPRESSCARD }, UNTESTED },
    110      1.17     elric 	{{ USB_VENDOR_QUALCOMM, USB_PRODUCT_QUALCOMM_MSM_HSDPA }, UNTESTED },
    111      1.27       riz 	{{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AIRCARD875 }, UNTESTED },
    112      1.17     elric 	{{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_EM5625 }, UNTESTED },
    113      1.16     elric };
    114      1.16     elric #define ugensa_lookup(v, p) \
    115      1.16     elric 	((const struct ugensa_type *)usb_lookup(ugensa_devs, v, p))
    116       1.1     elric 
    117      1.20      cube int ugensa_match(device_t, cfdata_t, void *);
    118      1.15    dyoung void ugensa_attach(device_t, device_t, void *);
    119      1.15    dyoung void ugensa_childdet(device_t, device_t);
    120      1.15    dyoung int ugensa_detach(device_t, int);
    121      1.15    dyoung int ugensa_activate(device_t, enum devact);
    122      1.15    dyoung extern struct cfdriver ugensa_cd;
    123      1.20      cube CFATTACH_DECL2_NEW(ugensa, sizeof(struct ugensa_softc), ugensa_match,
    124      1.15    dyoung     ugensa_attach, ugensa_detach, ugensa_activate, NULL, ugensa_childdet);
    125       1.1     elric 
    126  1.31.4.1       snj int
    127      1.28    dyoung ugensa_match(device_t parent, cfdata_t match, void *aux)
    128       1.1     elric {
    129      1.28    dyoung 	struct usb_attach_arg *uaa = aux;
    130       1.1     elric 
    131       1.1     elric 	DPRINTFN(20,("ugensa: vendor=0x%x, product=0x%x\n",
    132  1.31.4.1       snj 		     uaa->uaa_vendor, uaa->uaa_product));
    133       1.1     elric 
    134  1.31.4.1       snj 	return ugensa_lookup(uaa->uaa_vendor, uaa->uaa_product) != NULL ?
    135  1.31.4.1       snj 		UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
    136       1.1     elric }
    137       1.1     elric 
    138  1.31.4.1       snj void
    139      1.28    dyoung ugensa_attach(device_t parent, device_t self, void *aux)
    140       1.1     elric {
    141      1.28    dyoung 	struct ugensa_softc *sc = device_private(self);
    142      1.28    dyoung 	struct usb_attach_arg *uaa = aux;
    143  1.31.4.1       snj 	struct usbd_device *dev = uaa->uaa_device;
    144  1.31.4.1       snj 	struct usbd_interface *iface;
    145       1.1     elric 	usb_interface_descriptor_t *id;
    146       1.1     elric 	usb_endpoint_descriptor_t *ed;
    147       1.3  augustss 	char *devinfop;
    148      1.20      cube 	const char *devname = device_xname(self);
    149       1.1     elric 	usbd_status err;
    150  1.31.4.1       snj 	struct ucom_attach_args ucaa;
    151       1.1     elric 	int i;
    152       1.1     elric 
    153       1.1     elric 	DPRINTFN(10,("\nugensa_attach: sc=%p\n", sc));
    154       1.1     elric 
    155      1.20      cube 	sc->sc_dev = self;
    156      1.20      cube 
    157      1.23    plunky 	aprint_naive("\n");
    158      1.23    plunky 	aprint_normal("\n");
    159      1.23    plunky 
    160      1.23    plunky 	devinfop = usbd_devinfo_alloc(dev, 0);
    161      1.23    plunky 	aprint_normal_dev(self, "%s\n", devinfop);
    162      1.23    plunky 	usbd_devinfo_free(devinfop);
    163      1.23    plunky 
    164       1.1     elric 	/* Move the device into the configured state. */
    165       1.1     elric 	err = usbd_set_config_index(dev, UGENSA_CONFIG_INDEX, 1);
    166       1.1     elric 	if (err) {
    167      1.20      cube 		aprint_error("\n%s: failed to set configuration, err=%s\n",
    168       1.1     elric 		       devname, usbd_errstr(err));
    169       1.1     elric 		goto bad;
    170       1.1     elric 	}
    171       1.1     elric 
    172       1.1     elric 	err = usbd_device2interface_handle(dev, UGENSA_IFACE_INDEX, &iface);
    173       1.1     elric 	if (err) {
    174      1.20      cube 		aprint_error("\n%s: failed to get interface, err=%s\n",
    175       1.1     elric 		       devname, usbd_errstr(err));
    176       1.1     elric 		goto bad;
    177       1.1     elric 	}
    178       1.1     elric 
    179  1.31.4.1       snj 	if (ugensa_lookup(uaa->uaa_vendor, uaa->uaa_product)->ugensa_flags & UNTESTED)
    180      1.20      cube 		aprint_normal_dev(self, "WARNING: This device is marked as "
    181      1.20      cube 		    "untested. Please submit a report via send-pr(1).\n");
    182      1.16     elric 
    183       1.1     elric 	id = usbd_get_interface_descriptor(iface);
    184       1.1     elric 
    185       1.1     elric 	sc->sc_udev = dev;
    186       1.1     elric 	sc->sc_iface = iface;
    187       1.1     elric 
    188  1.31.4.1       snj 	ucaa.ucaa_info = "Generic Serial Device";
    189  1.31.4.1       snj 	ucaa.ucaa_ibufsize = UGENSA_BUFSIZE;
    190  1.31.4.1       snj 	ucaa.ucaa_obufsize = UGENSA_BUFSIZE;
    191  1.31.4.1       snj 	ucaa.ucaa_ibufsizepad = UGENSA_BUFSIZE;
    192  1.31.4.1       snj 	ucaa.ucaa_portno = UCOM_UNK_PORTNO;
    193  1.31.4.1       snj 	ucaa.ucaa_opkthdrlen = 0;
    194  1.31.4.1       snj 	ucaa.ucaa_device = dev;
    195  1.31.4.1       snj 	ucaa.ucaa_iface = iface;
    196  1.31.4.1       snj 	ucaa.ucaa_methods = &ugensa_methods;
    197  1.31.4.1       snj 	ucaa.ucaa_arg = sc;
    198       1.1     elric 
    199  1.31.4.1       snj 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, sc->sc_dev);
    200       1.1     elric 
    201  1.31.4.1       snj 	ucaa.ucaa_bulkin = ucaa.ucaa_bulkout = -1;
    202       1.1     elric 	for (i = 0; i < id->bNumEndpoints; i++) {
    203       1.1     elric 		int addr, dir, attr;
    204       1.1     elric 
    205       1.1     elric 		ed = usbd_interface2endpoint_descriptor(iface, i);
    206       1.1     elric 		if (ed == NULL) {
    207      1.20      cube 			aprint_error_dev(self,
    208      1.20      cube 			    "could not read endpoint descriptor: %s\n",
    209      1.20      cube 			    usbd_errstr(err));
    210       1.1     elric 			goto bad;
    211       1.1     elric 		}
    212       1.1     elric 
    213       1.1     elric 		addr = ed->bEndpointAddress;
    214       1.1     elric 		dir = UE_GET_DIR(ed->bEndpointAddress);
    215       1.1     elric 		attr = ed->bmAttributes & UE_XFERTYPE;
    216      1.16     elric 		if (attr == UE_BULK) {
    217  1.31.4.1       snj 			if (ucaa.ucaa_bulkin == -1 && dir == UE_DIR_IN) {
    218      1.16     elric 				DPRINTF(("%s: Bulk in %d\n", devname, i));
    219  1.31.4.1       snj 				ucaa.ucaa_bulkin = addr;
    220      1.16     elric 				continue;
    221      1.16     elric 			}
    222  1.31.4.1       snj 			if (ucaa.ucaa_bulkout == -1 && dir == UE_DIR_OUT) {
    223      1.16     elric 				DPRINTF(("%s: Bulk out %d\n", devname, i));
    224  1.31.4.1       snj 				ucaa.ucaa_bulkout = addr;
    225      1.16     elric 				continue;
    226      1.16     elric 			}
    227      1.16     elric 		}
    228      1.20      cube 		aprint_error_dev(self, "unexpected endpoint\n");
    229       1.1     elric 	}
    230  1.31.4.1       snj 	if (ucaa.ucaa_bulkin == -1) {
    231      1.20      cube 		aprint_error_dev(self, "Could not find data bulk in\n");
    232       1.1     elric 		goto bad;
    233       1.1     elric 	}
    234  1.31.4.1       snj 	if (ucaa.ucaa_bulkout == -1) {
    235      1.20      cube 		aprint_error_dev(self, "Could not find data bulk out\n");
    236       1.1     elric 		goto bad;
    237       1.1     elric 	}
    238       1.1     elric 
    239  1.31.4.1       snj 	DPRINTF(("ugensa: in=0x%x out=0x%x\n", ucaa.ucaa_bulkin,
    240  1.31.4.1       snj 	    ucaa.ucaa_bulkout));
    241  1.31.4.1       snj 	sc->sc_subdev = config_found_sm_loc(self, "ucombus", NULL, &ucaa,
    242       1.1     elric 					    ucomprint, ucomsubmatch);
    243       1.1     elric 
    244      1.14       smb 	if (!pmf_device_register(self, NULL, NULL))
    245      1.14       smb 		aprint_error_dev(self, "couldn't establish power handler\n");
    246      1.28    dyoung 	return;
    247       1.1     elric 
    248       1.1     elric bad:
    249       1.1     elric 	DPRINTF(("ugensa_attach: ATTACH ERROR\n"));
    250       1.1     elric 	sc->sc_dying = 1;
    251      1.28    dyoung 	return;
    252       1.1     elric }
    253       1.1     elric 
    254      1.15    dyoung void
    255      1.15    dyoung ugensa_childdet(device_t self, device_t child)
    256      1.15    dyoung {
    257      1.15    dyoung 	struct ugensa_softc *sc = device_private(self);
    258      1.15    dyoung 
    259      1.15    dyoung 	KASSERT(sc->sc_subdev == child);
    260      1.15    dyoung 	sc->sc_subdev = NULL;
    261      1.15    dyoung }
    262      1.15    dyoung 
    263       1.1     elric int
    264      1.15    dyoung ugensa_activate(device_t self, enum devact act)
    265       1.1     elric {
    266      1.15    dyoung 	struct ugensa_softc *sc = device_private(self);
    267       1.1     elric 
    268      1.16     elric 	DPRINTF(("ugensa_activate: sc=%p\n", sc));
    269      1.16     elric 
    270       1.1     elric 	switch (act) {
    271       1.1     elric 	case DVACT_DEACTIVATE:
    272       1.1     elric 		sc->sc_dying = 1;
    273      1.24    dyoung 		return 0;
    274      1.24    dyoung 	default:
    275      1.24    dyoung 		return EOPNOTSUPP;
    276       1.1     elric 	}
    277       1.1     elric }
    278       1.1     elric 
    279  1.31.4.1       snj int
    280      1.28    dyoung ugensa_detach(device_t self, int flags)
    281       1.1     elric {
    282      1.28    dyoung 	struct ugensa_softc *sc = device_private(self);
    283       1.1     elric 	int rv = 0;
    284       1.1     elric 
    285       1.1     elric 	DPRINTF(("ugensa_detach: sc=%p flags=%d\n", sc, flags));
    286       1.1     elric 
    287       1.1     elric 	sc->sc_dying = 1;
    288      1.14       smb 	pmf_device_deregister(self);
    289       1.1     elric 
    290       1.1     elric 	if (sc->sc_subdev != NULL)
    291       1.1     elric 		rv = config_detach(sc->sc_subdev, flags);
    292       1.1     elric 
    293  1.31.4.1       snj 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, sc->sc_dev);
    294       1.1     elric 
    295  1.31.4.1       snj 	return rv;
    296       1.1     elric }
    297