Home | History | Annotate | Line # | Download | only in usb
ugensa.c revision 1.12
      1 /*	$NetBSD: ugensa.c,v 1.12 2007/11/29 18:38:23 xtraeme 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/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 /* XXXrcd: heh */
     55 #define UGENSA_DEBUG 1
     56 
     57 #ifdef UGENSA_DEBUG
     58 #define DPRINTF(x)	if (ugensadebug) printf x
     59 #define DPRINTFN(n,x)	if (ugensadebug>(n)) printf x
     60 int ugensadebug = 0;
     61 #else
     62 #define DPRINTF(x)
     63 #define DPRINTFN(n,x)
     64 #endif
     65 
     66 struct ugensa_softc {
     67 	USBBASEDEVICE		sc_dev;		/* base device */
     68 	usbd_device_handle	sc_udev;	/* device */
     69 	usbd_interface_handle	sc_iface;	/* interface */
     70 
     71 	device_ptr_t		sc_subdev;
     72 	int			sc_numcon;
     73 
     74 	u_char			sc_dying;
     75 };
     76 
     77 struct ucom_methods ugensa_methods = {
     78 	NULL,
     79 	NULL,
     80 	NULL,
     81 	NULL,
     82 	NULL,
     83 	NULL,
     84 	NULL,
     85 	NULL,
     86 };
     87 
     88 #define UGENSA_CONFIG_INDEX	0
     89 #define UGENSA_IFACE_INDEX	0
     90 #define UGENSA_BUFSIZE		1024
     91 
     92 static const struct usb_devno ugensa_devs[] = {
     93 	{ USB_VENDOR_AIRPRIME, USB_PRODUCT_AIRPRIME_PC5220 },
     94 	{ USB_VENDOR_NOVATEL, USB_PRODUCT_NOVATEL_FLEXPACKGPS },
     95 	{ USB_VENDOR_QUALCOMM_K, USB_PRODUCT_QUALCOMM_K_CDMA_MSM_K },
     96 	{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AIRCARD580 },
     97 	{ USB_VENDOR_NOVATEL2, USB_PRODUCT_NOVATEL2_CDMA_MODEM },
     98 	{ USB_VENDOR_DELL, USB_PRODUCT_DELL_HSDPA }
     99 };
    100 #define ugensa_lookup(v, p) usb_lookup(ugensa_devs, v, p)
    101 
    102 USB_DECLARE_DRIVER(ugensa);
    103 
    104 USB_MATCH(ugensa)
    105 {
    106 	USB_MATCH_START(ugensa, uaa);
    107 
    108 	DPRINTFN(20,("ugensa: vendor=0x%x, product=0x%x\n",
    109 		     uaa->vendor, uaa->product));
    110 
    111 	return (ugensa_lookup(uaa->vendor, uaa->product) != NULL ?
    112 		UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
    113 }
    114 
    115 USB_ATTACH(ugensa)
    116 {
    117 	USB_ATTACH_START(ugensa, sc, uaa);
    118 	usbd_device_handle dev = uaa->device;
    119 	usbd_interface_handle iface;
    120 	usb_interface_descriptor_t *id;
    121 	usb_endpoint_descriptor_t *ed;
    122 	char *devinfop;
    123 	char *devname = USBDEVNAME(sc->sc_dev);
    124 	usbd_status err;
    125 	struct ucom_attach_args uca;
    126 	int i;
    127 
    128 	DPRINTFN(10,("\nugensa_attach: sc=%p\n", sc));
    129 
    130 	/* Move the device into the configured state. */
    131 	err = usbd_set_config_index(dev, UGENSA_CONFIG_INDEX, 1);
    132 	if (err) {
    133 		printf("\n%s: failed to set configuration, err=%s\n",
    134 		       devname, usbd_errstr(err));
    135 		goto bad;
    136 	}
    137 
    138 	err = usbd_device2interface_handle(dev, UGENSA_IFACE_INDEX, &iface);
    139 	if (err) {
    140 		printf("\n%s: failed to get interface, err=%s\n",
    141 		       devname, usbd_errstr(err));
    142 		goto bad;
    143 	}
    144 
    145 	devinfop = usbd_devinfo_alloc(dev, 0);
    146 	USB_ATTACH_SETUP;
    147 	printf("%s: %s\n", devname, devinfop);
    148 	usbd_devinfo_free(devinfop);
    149 
    150 	id = usbd_get_interface_descriptor(iface);
    151 
    152 	sc->sc_udev = dev;
    153 	sc->sc_iface = iface;
    154 
    155 	uca.info = "Generic Serial Device";
    156 	uca.ibufsize = UGENSA_BUFSIZE;
    157 	uca.obufsize = UGENSA_BUFSIZE;
    158 	uca.ibufsizepad = UGENSA_BUFSIZE;
    159 	uca.portno = UCOM_UNK_PORTNO;
    160 	uca.opkthdrlen = 0;
    161 	uca.device = dev;
    162 	uca.iface = iface;
    163 	uca.methods = &ugensa_methods;
    164 	uca.arg = sc;
    165 
    166 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
    167 			   USBDEV(sc->sc_dev));
    168 
    169 	uca.bulkin = uca.bulkout = -1;
    170 	for (i = 0; i < id->bNumEndpoints; i++) {
    171 		int addr, dir, attr;
    172 
    173 		ed = usbd_interface2endpoint_descriptor(iface, i);
    174 		if (ed == NULL) {
    175 			printf("%s: could not read endpoint descriptor"
    176 			       ": %s\n", devname, usbd_errstr(err));
    177 			goto bad;
    178 		}
    179 
    180 		addr = ed->bEndpointAddress;
    181 		dir = UE_GET_DIR(ed->bEndpointAddress);
    182 		attr = ed->bmAttributes & UE_XFERTYPE;
    183 		if (dir == UE_DIR_IN && attr == UE_BULK)
    184 			uca.bulkin = addr;
    185 		else if (dir == UE_DIR_OUT && attr == UE_BULK)
    186 			uca.bulkout = addr;
    187 		else
    188 			printf("%s: unexpected endpoint\n", devname);
    189 	}
    190 	if (uca.bulkin == -1) {
    191 		printf("%s: Could not find data bulk in\n",
    192 		       USBDEVNAME(sc->sc_dev));
    193 		goto bad;
    194 	}
    195 	if (uca.bulkout == -1) {
    196 		printf("%s: Could not find data bulk out\n",
    197 		       USBDEVNAME(sc->sc_dev));
    198 		goto bad;
    199 	}
    200 
    201 	DPRINTF(("ugensa: in=0x%x out=0x%x\n", uca.bulkin, uca.bulkout));
    202 	sc->sc_subdev = config_found_sm_loc(self, "ucombus", NULL, &uca,
    203 					    ucomprint, ucomsubmatch);
    204 
    205 	USB_ATTACH_SUCCESS_RETURN;
    206 
    207 bad:
    208 	DPRINTF(("ugensa_attach: ATTACH ERROR\n"));
    209 	sc->sc_dying = 1;
    210 	USB_ATTACH_ERROR_RETURN;
    211 }
    212 
    213 int
    214 ugensa_activate(device_ptr_t self, enum devact act)
    215 {
    216 	struct ugensa_softc *sc = (struct ugensa_softc *)self;
    217 	int rv = 0;
    218 
    219 	switch (act) {
    220 	case DVACT_ACTIVATE:
    221 		return (EOPNOTSUPP);
    222 		break;
    223 
    224 	case DVACT_DEACTIVATE:
    225 		sc->sc_dying = 1;
    226 		if (sc->sc_subdev)
    227 			rv = config_deactivate(sc->sc_subdev);
    228 		break;
    229 	}
    230 	return (rv);
    231 }
    232 
    233 USB_DETACH(ugensa)
    234 {
    235 	USB_DETACH_START(ugensa, sc);
    236 	int rv = 0;
    237 
    238 	DPRINTF(("ugensa_detach: sc=%p flags=%d\n", sc, flags));
    239 
    240 	sc->sc_dying = 1;
    241 
    242 	if (sc->sc_subdev != NULL)
    243 		rv = config_detach(sc->sc_subdev, flags);
    244 
    245 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
    246 			   USBDEV(sc->sc_dev));
    247 
    248 	return (rv);
    249 }
    250