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