Home | History | Annotate | Line # | Download | only in usb
ugensa.c revision 1.42
      1  1.42     skrll /*	$NetBSD: ugensa.c,v 1.42 2020/06/05 08:01:49 skrll 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.42     skrll __KERNEL_RCSID(0, "$NetBSD: ugensa.c,v 1.42 2020/06/05 08:01:49 skrll Exp $");
     34  1.35     skrll 
     35  1.35     skrll #ifdef _KERNEL_OPT
     36  1.35     skrll #include "opt_usb.h"
     37  1.35     skrll #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.40      maxv 
     66  1.40      maxv 	enum {
     67  1.40      maxv 		UGENSA_INIT_NONE,
     68  1.40      maxv 		UGENSA_INIT_INITED
     69  1.40      maxv 	} sc_init_state;
     70  1.40      maxv 
     71  1.32     skrll 	struct usbd_device *	sc_udev;	/* device */
     72  1.32     skrll 	struct usbd_interface *	sc_iface;	/* interface */
     73   1.1     elric 
     74  1.15    dyoung 	device_t		sc_subdev;
     75   1.1     elric 	int			sc_numcon;
     76   1.1     elric 
     77  1.39       mrg 	bool			sc_dying;
     78   1.1     elric };
     79   1.1     elric 
     80  1.39       mrg struct ucom_methods ugensa_methods = { 0 };
     81   1.1     elric 
     82   1.1     elric #define UGENSA_CONFIG_INDEX	0
     83   1.1     elric #define UGENSA_IFACE_INDEX	0
     84   1.1     elric #define UGENSA_BUFSIZE		1024
     85   1.1     elric 
     86  1.16     elric struct ugensa_type {
     87  1.16     elric 	struct usb_devno	ugensa_dev;
     88  1.32     skrll 	uint16_t		ugensa_flags;
     89  1.16     elric #define UNTESTED		0x0001
     90   1.1     elric };
     91  1.16     elric 
     92  1.16     elric static const struct ugensa_type ugensa_devs[] = {
     93  1.16     elric 	{{ USB_VENDOR_AIRPRIME, USB_PRODUCT_AIRPRIME_PC5220 }, 0 },
     94  1.27       riz 	{{ USB_VENDOR_DELL, USB_PRODUCT_DELL_HSDPA }, 0 },
     95  1.16     elric 	{{ USB_VENDOR_NOVATEL, USB_PRODUCT_NOVATEL_FLEXPACKGPS }, 0 },
     96  1.16     elric 	{{ USB_VENDOR_QUALCOMM_K, USB_PRODUCT_QUALCOMM_K_CDMA_MSM_K }, 0 },
     97  1.31     soren 	{{ USB_VENDOR_ZTE, USB_PRODUCT_ZTE_AC8700 }, 0 },
     98  1.16     elric 
     99  1.16     elric 	/*
    100  1.16     elric 	 * The following devices are untested, but they are purported to
    101  1.16     elric 	 * to work in similar device drivers on other OSes:
    102  1.16     elric 	 */
    103  1.16     elric 
    104  1.32     skrll 	{{ USB_VENDOR_ANYDATA, USB_PRODUCT_ANYDATA_ADU_500A }, UNTESTED },
    105  1.32     skrll 	{{ USB_VENDOR_NOVATEL2, USB_PRODUCT_NOVATEL2_EXPRESSCARD }, UNTESTED },
    106  1.36   msaitoh 	{{ USB_VENDOR_LG, USB_PRODUCT_LG_MSM_HSDPA }, UNTESTED },
    107  1.27       riz 	{{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AIRCARD875 }, UNTESTED },
    108  1.17     elric 	{{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_EM5625 }, UNTESTED },
    109  1.16     elric };
    110  1.16     elric #define ugensa_lookup(v, p) \
    111  1.16     elric 	((const struct ugensa_type *)usb_lookup(ugensa_devs, v, p))
    112   1.1     elric 
    113  1.39       mrg static int	ugensa_match(device_t, cfdata_t, void *);
    114  1.39       mrg static void	ugensa_attach(device_t, device_t, void *);
    115  1.39       mrg static void	ugensa_childdet(device_t, device_t);
    116  1.39       mrg static int	ugensa_detach(device_t, int);
    117  1.38       mrg 
    118  1.20      cube CFATTACH_DECL2_NEW(ugensa, sizeof(struct ugensa_softc), ugensa_match,
    119  1.39       mrg     ugensa_attach, ugensa_detach, NULL, NULL, ugensa_childdet);
    120   1.1     elric 
    121  1.39       mrg static int
    122  1.28    dyoung ugensa_match(device_t parent, cfdata_t match, void *aux)
    123   1.1     elric {
    124  1.28    dyoung 	struct usb_attach_arg *uaa = aux;
    125   1.1     elric 
    126  1.41  christos 	DPRINTFN(20,("ugensa: vendor=%#x, product=%#x\n",
    127  1.32     skrll 		     uaa->uaa_vendor, uaa->uaa_product));
    128   1.1     elric 
    129  1.32     skrll 	return ugensa_lookup(uaa->uaa_vendor, uaa->uaa_product) != NULL ?
    130  1.32     skrll 		UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
    131   1.1     elric }
    132   1.1     elric 
    133  1.39       mrg static void
    134  1.28    dyoung ugensa_attach(device_t parent, device_t self, void *aux)
    135   1.1     elric {
    136  1.28    dyoung 	struct ugensa_softc *sc = device_private(self);
    137  1.28    dyoung 	struct usb_attach_arg *uaa = aux;
    138  1.32     skrll 	struct usbd_device *dev = uaa->uaa_device;
    139  1.32     skrll 	struct usbd_interface *iface;
    140   1.1     elric 	usb_interface_descriptor_t *id;
    141   1.1     elric 	usb_endpoint_descriptor_t *ed;
    142   1.3  augustss 	char *devinfop;
    143  1.20      cube 	const char *devname = device_xname(self);
    144   1.1     elric 	usbd_status err;
    145  1.32     skrll 	struct ucom_attach_args ucaa;
    146   1.1     elric 	int i;
    147   1.1     elric 
    148   1.1     elric 	DPRINTFN(10,("\nugensa_attach: sc=%p\n", sc));
    149   1.1     elric 
    150  1.20      cube 	sc->sc_dev = self;
    151  1.39       mrg 	sc->sc_dying = false;
    152  1.40      maxv 	sc->sc_init_state = UGENSA_INIT_NONE;
    153  1.20      cube 
    154  1.23    plunky 	aprint_naive("\n");
    155  1.23    plunky 	aprint_normal("\n");
    156  1.23    plunky 
    157  1.23    plunky 	devinfop = usbd_devinfo_alloc(dev, 0);
    158  1.23    plunky 	aprint_normal_dev(self, "%s\n", devinfop);
    159  1.23    plunky 	usbd_devinfo_free(devinfop);
    160  1.23    plunky 
    161   1.1     elric 	/* Move the device into the configured state. */
    162   1.1     elric 	err = usbd_set_config_index(dev, UGENSA_CONFIG_INDEX, 1);
    163   1.1     elric 	if (err) {
    164  1.20      cube 		aprint_error("\n%s: failed to set configuration, err=%s\n",
    165   1.1     elric 		       devname, usbd_errstr(err));
    166   1.1     elric 		goto bad;
    167   1.1     elric 	}
    168   1.1     elric 
    169   1.1     elric 	err = usbd_device2interface_handle(dev, UGENSA_IFACE_INDEX, &iface);
    170   1.1     elric 	if (err) {
    171  1.20      cube 		aprint_error("\n%s: failed to get interface, err=%s\n",
    172   1.1     elric 		       devname, usbd_errstr(err));
    173   1.1     elric 		goto bad;
    174   1.1     elric 	}
    175   1.1     elric 
    176  1.32     skrll 	if (ugensa_lookup(uaa->uaa_vendor, uaa->uaa_product)->ugensa_flags & UNTESTED)
    177  1.20      cube 		aprint_normal_dev(self, "WARNING: This device is marked as "
    178  1.20      cube 		    "untested. Please submit a report via send-pr(1).\n");
    179  1.16     elric 
    180   1.1     elric 	id = usbd_get_interface_descriptor(iface);
    181   1.1     elric 
    182   1.1     elric 	sc->sc_udev = dev;
    183   1.1     elric 	sc->sc_iface = iface;
    184   1.1     elric 
    185  1.32     skrll 	ucaa.ucaa_info = "Generic Serial Device";
    186  1.32     skrll 	ucaa.ucaa_ibufsize = UGENSA_BUFSIZE;
    187  1.32     skrll 	ucaa.ucaa_obufsize = UGENSA_BUFSIZE;
    188  1.32     skrll 	ucaa.ucaa_ibufsizepad = UGENSA_BUFSIZE;
    189  1.32     skrll 	ucaa.ucaa_portno = UCOM_UNK_PORTNO;
    190  1.32     skrll 	ucaa.ucaa_opkthdrlen = 0;
    191  1.32     skrll 	ucaa.ucaa_device = dev;
    192  1.32     skrll 	ucaa.ucaa_iface = iface;
    193  1.32     skrll 	ucaa.ucaa_methods = &ugensa_methods;
    194  1.32     skrll 	ucaa.ucaa_arg = sc;
    195   1.1     elric 
    196  1.32     skrll 	ucaa.ucaa_bulkin = ucaa.ucaa_bulkout = -1;
    197   1.1     elric 	for (i = 0; i < id->bNumEndpoints; i++) {
    198   1.1     elric 		int addr, dir, attr;
    199   1.1     elric 
    200   1.1     elric 		ed = usbd_interface2endpoint_descriptor(iface, i);
    201   1.1     elric 		if (ed == NULL) {
    202  1.20      cube 			aprint_error_dev(self,
    203  1.20      cube 			    "could not read endpoint descriptor: %s\n",
    204  1.20      cube 			    usbd_errstr(err));
    205   1.1     elric 			goto bad;
    206   1.1     elric 		}
    207   1.1     elric 
    208   1.1     elric 		addr = ed->bEndpointAddress;
    209   1.1     elric 		dir = UE_GET_DIR(ed->bEndpointAddress);
    210   1.1     elric 		attr = ed->bmAttributes & UE_XFERTYPE;
    211  1.16     elric 		if (attr == UE_BULK) {
    212  1.32     skrll 			if (ucaa.ucaa_bulkin == -1 && dir == UE_DIR_IN) {
    213  1.16     elric 				DPRINTF(("%s: Bulk in %d\n", devname, i));
    214  1.32     skrll 				ucaa.ucaa_bulkin = addr;
    215  1.16     elric 				continue;
    216  1.16     elric 			}
    217  1.32     skrll 			if (ucaa.ucaa_bulkout == -1 && dir == UE_DIR_OUT) {
    218  1.16     elric 				DPRINTF(("%s: Bulk out %d\n", devname, i));
    219  1.32     skrll 				ucaa.ucaa_bulkout = addr;
    220  1.16     elric 				continue;
    221  1.16     elric 			}
    222  1.16     elric 		}
    223  1.20      cube 		aprint_error_dev(self, "unexpected endpoint\n");
    224   1.1     elric 	}
    225  1.32     skrll 	if (ucaa.ucaa_bulkin == -1) {
    226  1.20      cube 		aprint_error_dev(self, "Could not find data bulk in\n");
    227   1.1     elric 		goto bad;
    228   1.1     elric 	}
    229  1.32     skrll 	if (ucaa.ucaa_bulkout == -1) {
    230  1.20      cube 		aprint_error_dev(self, "Could not find data bulk out\n");
    231   1.1     elric 		goto bad;
    232   1.1     elric 	}
    233   1.1     elric 
    234  1.40      maxv 	sc->sc_init_state = UGENSA_INIT_INITED;
    235  1.40      maxv 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, sc->sc_dev);
    236  1.40      maxv 
    237  1.41  christos 	DPRINTF(("ugensa: in=%#x out=%#x\n", ucaa.ucaa_bulkin,
    238  1.32     skrll 	    ucaa.ucaa_bulkout));
    239  1.32     skrll 	sc->sc_subdev = config_found_sm_loc(self, "ucombus", NULL, &ucaa,
    240   1.1     elric 					    ucomprint, ucomsubmatch);
    241   1.1     elric 
    242  1.14       smb 	if (!pmf_device_register(self, NULL, NULL))
    243  1.14       smb 		aprint_error_dev(self, "couldn't establish power handler\n");
    244  1.28    dyoung 	return;
    245   1.1     elric 
    246   1.1     elric bad:
    247   1.1     elric 	DPRINTF(("ugensa_attach: ATTACH ERROR\n"));
    248  1.39       mrg 	sc->sc_dying = true;
    249  1.28    dyoung 	return;
    250   1.1     elric }
    251   1.1     elric 
    252  1.39       mrg static void
    253  1.15    dyoung ugensa_childdet(device_t self, device_t child)
    254  1.15    dyoung {
    255  1.15    dyoung 	struct ugensa_softc *sc = device_private(self);
    256  1.15    dyoung 
    257  1.15    dyoung 	KASSERT(sc->sc_subdev == child);
    258  1.15    dyoung 	sc->sc_subdev = NULL;
    259  1.15    dyoung }
    260  1.15    dyoung 
    261  1.39       mrg static int
    262  1.28    dyoung ugensa_detach(device_t self, int flags)
    263   1.1     elric {
    264  1.28    dyoung 	struct ugensa_softc *sc = device_private(self);
    265   1.1     elric 	int rv = 0;
    266   1.1     elric 
    267   1.1     elric 	DPRINTF(("ugensa_detach: sc=%p flags=%d\n", sc, flags));
    268   1.1     elric 
    269  1.39       mrg 	sc->sc_dying = true;
    270   1.1     elric 
    271  1.40      maxv 	if (sc->sc_init_state < UGENSA_INIT_INITED)
    272  1.40      maxv 		return 0;
    273  1.40      maxv 
    274  1.39       mrg 	if (sc->sc_subdev != NULL) {
    275   1.1     elric 		rv = config_detach(sc->sc_subdev, flags);
    276  1.39       mrg 		sc->sc_subdev = NULL;
    277  1.39       mrg 	}
    278   1.1     elric 
    279  1.33   msaitoh 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, sc->sc_dev);
    280   1.1     elric 
    281  1.39       mrg 	pmf_device_deregister(self);
    282  1.39       mrg 
    283  1.32     skrll 	return rv;
    284   1.1     elric }
    285