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