Home | History | Annotate | Line # | Download | only in usb
ugensa.c revision 1.10.12.3
      1 /*	ugensa.c,v 1.10.12.2 2008/01/09 01:54:42 matt 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  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 #include <sys/cdefs.h>
     40 __KERNEL_RCSID(0, "ugensa.c,v 1.10.12.2 2008/01/09 01:54:42 matt Exp");
     41 
     42 #include <sys/param.h>
     43 #include <sys/systm.h>
     44 #include <sys/kernel.h>
     45 #include <sys/device.h>
     46 #include <sys/conf.h>
     47 #include <sys/tty.h>
     48 
     49 #include <dev/usb/usb.h>
     50 
     51 #include <dev/usb/usbdi.h>
     52 #include <dev/usb/usbdi_util.h>
     53 #include <dev/usb/usbdevs.h>
     54 
     55 #include <dev/usb/ucomvar.h>
     56 
     57 /* XXXrcd: heh */
     58 #define UGENSA_DEBUG 1
     59 
     60 #ifdef UGENSA_DEBUG
     61 #define DPRINTF(x)	if (ugensadebug) printf x
     62 #define DPRINTFN(n,x)	if (ugensadebug>(n)) printf x
     63 int ugensadebug = 0;
     64 #else
     65 #define DPRINTF(x)
     66 #define DPRINTFN(n,x)
     67 #endif
     68 
     69 struct ugensa_softc {
     70 	USBBASEDEVICE		sc_dev;		/* base device */
     71 	usbd_device_handle	sc_udev;	/* device */
     72 	usbd_interface_handle	sc_iface;	/* interface */
     73 
     74 	device_t		sc_subdev;
     75 	int			sc_numcon;
     76 
     77 	u_char			sc_dying;
     78 };
     79 
     80 struct ucom_methods ugensa_methods = {
     81 	NULL,
     82 	NULL,
     83 	NULL,
     84 	NULL,
     85 	NULL,
     86 	NULL,
     87 	NULL,
     88 	NULL,
     89 };
     90 
     91 #define UGENSA_CONFIG_INDEX	0
     92 #define UGENSA_IFACE_INDEX	0
     93 #define UGENSA_BUFSIZE		1024
     94 
     95 struct ugensa_type {
     96 	struct usb_devno	ugensa_dev;
     97 	u_int16_t		ugensa_flags;
     98 #define UNTESTED		0x0001
     99 };
    100 
    101 static const struct ugensa_type ugensa_devs[] = {
    102 	{{ USB_VENDOR_AIRPRIME, USB_PRODUCT_AIRPRIME_PC5220 }, 0 },
    103 	{{ USB_VENDOR_NOVATEL, USB_PRODUCT_NOVATEL_FLEXPACKGPS }, 0 },
    104 	{{ USB_VENDOR_QUALCOMM_K, USB_PRODUCT_QUALCOMM_K_CDMA_MSM_K }, 0 },
    105 	{{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AIRCARD580 }, 0 },
    106 	{{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AIRCARD595 }, 0 },
    107 	{{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MINI5725}, 0 },
    108 	{{ USB_VENDOR_DELL, USB_PRODUCT_DELL_HSDPA }, 0 },
    109 
    110 	/*
    111 	 * The following devices are untested, but they are purported to
    112 	 * to work in similar device drivers on other OSes:
    113 	 */
    114 
    115         {{ USB_VENDOR_ANYDATA, USB_PRODUCT_ANYDATA_ADU_500A }, UNTESTED },
    116         {{ USB_VENDOR_DELL, USB_PRODUCT_DELL_W5500 }, UNTESTED },
    117         {{ USB_VENDOR_NOVATEL2, USB_PRODUCT_NOVATEL2_EXPRESSCARD }, UNTESTED },
    118 	{{ USB_VENDOR_NOVATEL2, USB_PRODUCT_NOVATEL2_MERLINV620 }, UNTESTED },
    119 	{{ USB_VENDOR_NOVATEL2, USB_PRODUCT_NOVATEL2_S720 }, UNTESTED },
    120 	{{ USB_VENDOR_NOVATEL2, USB_PRODUCT_NOVATEL2_U720 }, UNTESTED },
    121 	{{ USB_VENDOR_NOVATEL2, USB_PRODUCT_NOVATEL2_XU870 }, UNTESTED },
    122 	{{ USB_VENDOR_NOVATEL2, USB_PRODUCT_NOVATEL2_ES620 }, UNTESTED },
    123 	{{ USB_VENDOR_QUALCOMM, USB_PRODUCT_QUALCOMM_MSM_HSDPA }, UNTESTED },
    124 	{{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_EM5625 }, UNTESTED },
    125 	{{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AIRCARD875 }, UNTESTED },
    126 	{{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC5720 }, UNTESTED },
    127 	{{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC5725 }, UNTESTED },
    128 	{{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC8755 }, UNTESTED },
    129 	{{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC8755_2 }, UNTESTED },
    130 	{{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC8755_3 }, UNTESTED },
    131 	{{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC8765 }, UNTESTED },
    132 	{{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC8775 }, UNTESTED }
    133 };
    134 #define ugensa_lookup(v, p) \
    135 	((const struct ugensa_type *)usb_lookup(ugensa_devs, v, p))
    136 
    137 int ugensa_match(device_t, struct cfdata *, void *);
    138 void ugensa_attach(device_t, device_t, void *);
    139 void ugensa_childdet(device_t, device_t);
    140 int ugensa_detach(device_t, int);
    141 int ugensa_activate(device_t, enum devact);
    142 extern struct cfdriver ugensa_cd;
    143 CFATTACH_DECL2(ugensa, sizeof(struct ugensa_softc), ugensa_match,
    144     ugensa_attach, ugensa_detach, ugensa_activate, NULL, ugensa_childdet);
    145 
    146 USB_MATCH(ugensa)
    147 {
    148 	USB_MATCH_START(ugensa, uaa);
    149 
    150 	DPRINTFN(20,("ugensa: vendor=0x%x, product=0x%x\n",
    151 		     uaa->vendor, uaa->product));
    152 
    153 	return (ugensa_lookup(uaa->vendor, uaa->product) != NULL ?
    154 		UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
    155 }
    156 
    157 USB_ATTACH(ugensa)
    158 {
    159 	USB_ATTACH_START(ugensa, sc, uaa);
    160 	usbd_device_handle dev = uaa->device;
    161 	usbd_interface_handle iface;
    162 	usb_interface_descriptor_t *id;
    163 	usb_endpoint_descriptor_t *ed;
    164 	char *devinfop;
    165 	char *devname = USBDEVNAME(sc->sc_dev);
    166 	usbd_status err;
    167 	struct ucom_attach_args uca;
    168 	int i;
    169 
    170 	DPRINTFN(10,("\nugensa_attach: sc=%p\n", sc));
    171 
    172 	/* Move the device into the configured state. */
    173 	err = usbd_set_config_index(dev, UGENSA_CONFIG_INDEX, 1);
    174 	if (err) {
    175 		printf("\n%s: failed to set configuration, err=%s\n",
    176 		       devname, usbd_errstr(err));
    177 		goto bad;
    178 	}
    179 
    180 	err = usbd_device2interface_handle(dev, UGENSA_IFACE_INDEX, &iface);
    181 	if (err) {
    182 		printf("\n%s: failed to get interface, err=%s\n",
    183 		       devname, usbd_errstr(err));
    184 		goto bad;
    185 	}
    186 
    187 	devinfop = usbd_devinfo_alloc(dev, 0);
    188 	USB_ATTACH_SETUP;
    189 	printf("%s: %s\n", devname, devinfop);
    190 	usbd_devinfo_free(devinfop);
    191 
    192 	if (ugensa_lookup(uaa->vendor, uaa->product)->ugensa_flags & UNTESTED)
    193 		printf("%s: WARNING: This device is marked as untested. "
    194 		    "Please submit a report via send-pr(1).\n", devname);
    195 
    196 	id = usbd_get_interface_descriptor(iface);
    197 
    198 	sc->sc_udev = dev;
    199 	sc->sc_iface = iface;
    200 
    201 	uca.info = "Generic Serial Device";
    202 	uca.ibufsize = UGENSA_BUFSIZE;
    203 	uca.obufsize = UGENSA_BUFSIZE;
    204 	uca.ibufsizepad = UGENSA_BUFSIZE;
    205 	uca.portno = UCOM_UNK_PORTNO;
    206 	uca.opkthdrlen = 0;
    207 	uca.device = dev;
    208 	uca.iface = iface;
    209 	uca.methods = &ugensa_methods;
    210 	uca.arg = sc;
    211 
    212 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
    213 			   USBDEV(sc->sc_dev));
    214 
    215 	uca.bulkin = uca.bulkout = -1;
    216 	for (i = 0; i < id->bNumEndpoints; i++) {
    217 		int addr, dir, attr;
    218 
    219 		ed = usbd_interface2endpoint_descriptor(iface, i);
    220 		if (ed == NULL) {
    221 			printf("%s: could not read endpoint descriptor"
    222 			       ": %s\n", devname, usbd_errstr(err));
    223 			goto bad;
    224 		}
    225 
    226 		addr = ed->bEndpointAddress;
    227 		dir = UE_GET_DIR(ed->bEndpointAddress);
    228 		attr = ed->bmAttributes & UE_XFERTYPE;
    229 		if (attr == UE_BULK) {
    230 			if (uca.bulkin == -1 && dir == UE_DIR_IN) {
    231 				DPRINTF(("%s: Bulk in %d\n", devname, i));
    232 				uca.bulkin = addr;
    233 				continue;
    234 			}
    235 			if (uca.bulkout == -1 && dir == UE_DIR_OUT) {
    236 				DPRINTF(("%s: Bulk out %d\n", devname, i));
    237 				uca.bulkout = addr;
    238 				continue;
    239 			}
    240 		}
    241 		printf("%s: unexpected endpoint\n", devname);
    242 	}
    243 	if (uca.bulkin == -1) {
    244 		printf("%s: Could not find data bulk in\n",
    245 		       USBDEVNAME(sc->sc_dev));
    246 		goto bad;
    247 	}
    248 	if (uca.bulkout == -1) {
    249 		printf("%s: Could not find data bulk out\n",
    250 		       USBDEVNAME(sc->sc_dev));
    251 		goto bad;
    252 	}
    253 
    254 	DPRINTF(("ugensa: in=0x%x out=0x%x\n", uca.bulkin, uca.bulkout));
    255 	sc->sc_subdev = config_found_sm_loc(self, "ucombus", NULL, &uca,
    256 					    ucomprint, ucomsubmatch);
    257 
    258 	if (!pmf_device_register(self, NULL, NULL))
    259 		aprint_error_dev(self, "couldn't establish power handler\n");
    260 	USB_ATTACH_SUCCESS_RETURN;
    261 
    262 bad:
    263 	DPRINTF(("ugensa_attach: ATTACH ERROR\n"));
    264 	sc->sc_dying = 1;
    265 	USB_ATTACH_ERROR_RETURN;
    266 }
    267 
    268 void
    269 ugensa_childdet(device_t self, device_t child)
    270 {
    271 	struct ugensa_softc *sc = device_private(self);
    272 
    273 	KASSERT(sc->sc_subdev == child);
    274 	sc->sc_subdev = NULL;
    275 }
    276 
    277 int
    278 ugensa_activate(device_t self, enum devact act)
    279 {
    280 	struct ugensa_softc *sc = device_private(self);
    281 	int rv = 0;
    282 
    283 	DPRINTF(("ugensa_activate: sc=%p\n", sc));
    284 
    285 	switch (act) {
    286 	case DVACT_ACTIVATE:
    287 		return (EOPNOTSUPP);
    288 		break;
    289 
    290 	case DVACT_DEACTIVATE:
    291 		sc->sc_dying = 1;
    292 		if (sc->sc_subdev)
    293 			rv = config_deactivate(sc->sc_subdev);
    294 		break;
    295 	}
    296 	return (rv);
    297 }
    298 
    299 USB_DETACH(ugensa)
    300 {
    301 	USB_DETACH_START(ugensa, sc);
    302 	int rv = 0;
    303 
    304 	DPRINTF(("ugensa_detach: sc=%p flags=%d\n", sc, flags));
    305 
    306 	sc->sc_dying = 1;
    307 	pmf_device_deregister(self);
    308 
    309 	if (sc->sc_subdev != NULL)
    310 		rv = config_detach(sc->sc_subdev, flags);
    311 
    312 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
    313 			   USBDEV(sc->sc_dev));
    314 
    315 	return (rv);
    316 }
    317