Home | History | Annotate | Line # | Download | only in usb
uipaq.c revision 1.5.10.2
      1 /*	$NetBSD: uipaq.c,v 1.5.10.2 2007/06/28 03:18:13 itohy Exp $	*/
      2 /*	$OpenBSD: uipaq.c,v 1.1 2005/06/17 23:50:33 deraadt Exp $	*/
      3 
      4 /*
      5  * Copyright (c) 2000-2005 The NetBSD Foundation, Inc.
      6  * All rights reserved.
      7  *
      8  * This code is derived from software contributed to The NetBSD Foundation
      9  * by Lennart Augustsson (lennart (at) augustsson.net) at
     10  * Carlstedt Research & Technology.
     11  *
     12  * Redistribution and use in source and binary forms, with or without
     13  * modification, are permitted provided that the following conditions
     14  * are met:
     15  * 1. Redistributions of source code must retain the above copyright
     16  *    notice, this list of conditions and the following disclaimer.
     17  * 2. Redistributions in binary form must reproduce the above copyright
     18  *    notice, this list of conditions and the following disclaimer in the
     19  *    documentation and/or other materials provided with the distribution.
     20  * 3. All advertising materials mentioning features or use of this software
     21  *    must display the following acknowledgement:
     22  *        This product includes software developed by the NetBSD
     23  *        Foundation, Inc. and its contributors.
     24  * 4. Neither the name of The NetBSD Foundation nor the names of its
     25  *    contributors may be used to endorse or promote products derived
     26  *    from this software without specific prior written permission.
     27  *
     28  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     29  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     31  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     32  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     33  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     34  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     35  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     36  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     37  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     38  * POSSIBILITY OF SUCH DAMAGE.
     39  */
     40 
     41 /*
     42  * iPAQ driver
     43  *
     44  * 19 July 2003:	Incorporated changes suggested by Sam Lawrance from
     45  * 			the uppc module
     46  *
     47  *
     48  * Contact isis (at) cs.umd.edu if you have any questions/comments about this driver
     49  */
     50 
     51 #include <sys/param.h>
     52 #include <sys/systm.h>
     53 #include <sys/kernel.h>
     54 #include <sys/device.h>
     55 #include <sys/conf.h>
     56 #include <sys/tty.h>
     57 
     58 #include <dev/usb/usb.h>
     59 #include <dev/usb/usbhid.h>
     60 
     61 #include <dev/usb/usbcdc.h>	/*UCDC_* stuff */
     62 
     63 #include <dev/usb/usbdi.h>
     64 #include <dev/usb/usbdi_util.h>
     65 #include <dev/usb/usbdevs.h>
     66 
     67 #include <dev/usb/ucomvar.h>
     68 
     69 #ifdef UIPAQ_DEBUG
     70 #define DPRINTF(x)	if (uipaqdebug) printf x
     71 #define DPRINTFN(n,x)	if (uipaqdebug>(n)) printf x
     72 int uipaqdebug = 0;
     73 #else
     74 #define DPRINTF(x)
     75 #define DPRINTFN(n,x)
     76 #endif
     77 
     78 #define UIPAQ_CONFIG_NO		1
     79 #define UIPAQ_IFACE_INDEX	0
     80 
     81 #define UIPAQIBUFSIZE 1024
     82 #define UIPAQOBUFSIZE 1024
     83 
     84 struct uipaq_softc {
     85 	USBBASEDEVICE		sc_dev;		/* base device */
     86 	usbd_device_handle	sc_udev;	/* device */
     87 	usbd_interface_handle	sc_iface;	/* interface */
     88 
     89 	device_ptr_t		sc_subdev;	/* ucom uses that */
     90 	u_int16_t		sc_lcr;		/* state for DTR/RTS */
     91 
     92 	u_int16_t		sc_flags;
     93 
     94 	u_char			sc_dying;
     95 };
     96 
     97 /* Callback routines */
     98 Static void	uipaq_set(void *, int, int, int);
     99 
    100 
    101 /* Support routines. */
    102 /* based on uppc module by Sam Lawrance */
    103 Static void	uipaq_dtr(struct uipaq_softc *sc, int onoff);
    104 Static void	uipaq_rts(struct uipaq_softc *sc, int onoff);
    105 Static void	uipaq_break(struct uipaq_softc* sc, int onoff);
    106 
    107 
    108 struct ucom_methods uipaq_methods = {
    109 	NULL,
    110 	uipaq_set,
    111 	NULL,
    112 	NULL,
    113 	NULL,	/*open*/
    114 	NULL,	/*close*/
    115 	NULL,
    116 	NULL
    117 };
    118 
    119 struct uipaq_type {
    120 	struct usb_devno	uv_dev;
    121 	u_int16_t		uv_flags;
    122 };
    123 
    124 static const struct uipaq_type uipaq_devs[] = {
    125 	{{ USB_VENDOR_ASUSTEK, USB_PRODUCT_ASUSTEK_MYPAL_A730} , 0},
    126 	{{ USB_VENDOR_CASIO, USB_PRODUCT_CASIO_BE300} , 0},
    127 	{{ USB_VENDOR_COMPAQ, USB_PRODUCT_COMPAQ_IPAQPOCKETPC} , 0},
    128 	{{ USB_VENDOR_HP, USB_PRODUCT_HP_2215 }, 0 },
    129 	{{ USB_VENDOR_HP, USB_PRODUCT_HP_568J }, 0},
    130 	{{ USB_VENDOR_SHARP, USB_PRODUCT_SHARP_WS007SH} , 0}
    131 };
    132 
    133 #define uipaq_lookup(v, p) ((const struct uipaq_type *)usb_lookup(uipaq_devs, v, p))
    134 
    135 USB_DECLARE_DRIVER(uipaq);
    136 
    137 USB_MATCH(uipaq)
    138 {
    139 	USB_MATCH_START(uipaq, uaa);
    140 
    141 #ifndef USB_USE_IFATTACH
    142 	if (uaa->iface != NULL)
    143 		return (UMATCH_NONE);
    144 #endif /* USB_USE_IFATTACH */
    145 
    146 	DPRINTFN(20,("uipaq: vendor=0x%x, product=0x%x\n",
    147 	    uaa->vendor, uaa->product));
    148 
    149 	return (uipaq_lookup(uaa->vendor, uaa->product) != NULL ?
    150 	    UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
    151 }
    152 
    153 USB_ATTACH(uipaq)
    154 {
    155 	USB_ATTACH_START(uipaq, sc, uaa);
    156 	usbd_device_handle dev = uaa->device;
    157 	usbd_interface_handle iface;
    158 	usb_interface_descriptor_t *id;
    159 	usb_endpoint_descriptor_t *ed;
    160 	char *devinfop;
    161 	char *devname = USBDEVNAME(sc->sc_dev);
    162 	int i;
    163 	usbd_status err;
    164 	struct ucom_attach_args uca;
    165 
    166 	DPRINTFN(10,("\nuipaq_attach: sc=%p\n", sc));
    167 
    168 	/* Move the device into the configured state. */
    169 	err = usbd_set_config_no(dev, UIPAQ_CONFIG_NO, 1);
    170 	if (err) {
    171 		printf("\n%s: failed to set configuration, err=%s\n",
    172 		    devname, usbd_errstr(err));
    173 		goto bad;
    174 	}
    175 
    176 	err = usbd_device2interface_handle(dev, UIPAQ_IFACE_INDEX, &iface);
    177 	if (err) {
    178 		printf("\n%s: failed to get interface, err=%s\n",
    179 		    devname, usbd_errstr(err));
    180 		goto bad;
    181 	}
    182 
    183 	devinfop = usbd_devinfo_alloc(dev, 0);
    184 	USB_ATTACH_SETUP;
    185 	printf("%s: %s\n", devname, devinfop);
    186 	usbd_devinfo_free(devinfop);
    187 
    188 	sc->sc_flags = uipaq_lookup(uaa->vendor, uaa->product)->uv_flags;
    189 
    190 	id = usbd_get_interface_descriptor(iface);
    191 
    192 	sc->sc_udev = dev;
    193 	sc->sc_iface = iface;
    194 
    195 	uca.ibufsize = UIPAQIBUFSIZE;
    196 	uca.obufsize = UIPAQOBUFSIZE;
    197 	uca.ibufsizepad = UIPAQIBUFSIZE;
    198 	uca.opkthdrlen = 0;
    199 	uca.device = dev;
    200 	uca.iface = iface;
    201 	uca.methods = &uipaq_methods;
    202 	uca.arg = sc;
    203 	uca.portno = UCOM_UNK_PORTNO;
    204 	uca.info = "Generic";
    205 
    206 /*	err = uipaq_init(sc);
    207 	if (err) {
    208 		printf("%s: init failed, %s\n", USBDEVNAME(sc->sc_dev),
    209 		    usbd_errstr(err));
    210 		goto bad;
    211 	}*/
    212 
    213 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
    214 	    USBDEV(sc->sc_dev));
    215 
    216 	uca.bulkin = uca.bulkout = -1;
    217 	for (i=0; i<id->bNumEndpoints; i++) {
    218 		ed = usbd_interface2endpoint_descriptor(iface, i);
    219 		if (ed == NULL) {
    220 			printf("%s: no endpoint descriptor for %d\n",
    221 					devname,i);
    222 			goto bad;
    223 		}
    224 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    225 		    (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
    226 			uca.bulkin = ed->bEndpointAddress;
    227 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
    228 		    (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
    229 			uca.bulkout = ed->bEndpointAddress;
    230 		}
    231 	}
    232 	if (uca.bulkin == -1 || uca.bulkout == -1) {
    233 		printf("%s: no proper endpoints found (%d,%d) \n",
    234 		    devname, uca.bulkin, uca.bulkout);
    235 		USB_ATTACH_ERROR_RETURN;
    236 	}
    237 
    238 	sc->sc_subdev = config_found_sm_loc(self, "ucombus", NULL, &uca,
    239 					    ucomprint, ucomsubmatch);
    240 
    241 	USB_ATTACH_SUCCESS_RETURN;
    242 
    243 bad:
    244 	DPRINTF(("uipaq_attach: ATTACH ERROR\n"));
    245 	sc->sc_dying = 1;
    246 	USB_ATTACH_ERROR_RETURN;
    247 }
    248 
    249 
    250 void
    251 uipaq_dtr(struct uipaq_softc* sc, int onoff)
    252 {
    253 	usb_device_request_t req;
    254 	usbd_status err;
    255 	int retries = 3;
    256 
    257 	DPRINTF(("%s: uipaq_dtr: onoff=%x\n", USBDEVNAME(sc->sc_dev), onoff));
    258 
    259 	/* Avoid sending unnecessary requests */
    260 	if (onoff && (sc->sc_lcr & UCDC_LINE_DTR))
    261 		return;
    262 	if (!onoff && !(sc->sc_lcr & UCDC_LINE_DTR))
    263 		return;
    264 
    265 	/* Other parameters depend on reg */
    266 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
    267 	req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
    268 	sc->sc_lcr = onoff ? sc->sc_lcr | UCDC_LINE_DTR : sc->sc_lcr & ~UCDC_LINE_DTR;
    269 	USETW(req.wValue, sc->sc_lcr);
    270 	USETW(req.wIndex, 0x0);
    271 	USETW(req.wLength, 0);
    272 
    273 	/* Fire off the request a few times if necessary */
    274 	while (retries) {
    275 		err = usbd_do_request(sc->sc_udev, &req, NULL);
    276 		if (!err)
    277 			break;
    278 		retries--;
    279 	}
    280 }
    281 
    282 
    283 void
    284 uipaq_rts(struct uipaq_softc* sc, int onoff)
    285 {
    286 	usb_device_request_t req;
    287 	usbd_status err;
    288 	int retries = 3;
    289 
    290 	DPRINTF(("%s: uipaq_rts: onoff=%x\n", USBDEVNAME(sc->sc_dev), onoff));
    291 
    292 	/* Avoid sending unnecessary requests */
    293 	if (onoff && (sc->sc_lcr & UCDC_LINE_RTS)) return;
    294 	if (!onoff && !(sc->sc_lcr & UCDC_LINE_RTS)) return;
    295 
    296 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
    297 	req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
    298 	sc->sc_lcr = onoff ? sc->sc_lcr | UCDC_LINE_RTS : sc->sc_lcr & ~UCDC_LINE_RTS;
    299 	USETW(req.wValue, sc->sc_lcr);
    300 	USETW(req.wIndex, 0x0);
    301 	USETW(req.wLength, 0);
    302 
    303 	while (retries) {
    304 		err = usbd_do_request(sc->sc_udev, &req, NULL);
    305 		if (!err)
    306 			break;
    307 		retries--;
    308 	}
    309 }
    310 
    311 
    312 void
    313 uipaq_break(struct uipaq_softc* sc, int onoff)
    314 {
    315 	usb_device_request_t req;
    316 	usbd_status err;
    317 	int retries = 3;
    318 
    319 	DPRINTF(("%s: uipaq_break: onoff=%x\n", USBDEVNAME(sc->sc_dev), onoff));
    320 
    321 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
    322 	req.bRequest = UCDC_SEND_BREAK;
    323 
    324 	USETW(req.wValue, onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF);
    325 	USETW(req.wIndex, 0x0);
    326 	USETW(req.wLength, 0);
    327 
    328 	while (retries) {
    329 		err = usbd_do_request(sc->sc_udev, &req, NULL);
    330 		if (!err)
    331 			break;
    332 		retries--;
    333 	}
    334 }
    335 
    336 
    337 void
    338 uipaq_set(void *addr, int portno, int reg, int onoff)
    339 {
    340 	struct uipaq_softc* sc = addr;
    341 
    342 	switch (reg) {
    343 	case UCOM_SET_DTR:
    344 		uipaq_dtr(addr, onoff);
    345 		break;
    346 	case UCOM_SET_RTS:
    347 		uipaq_rts(addr, onoff);
    348 		break;
    349 	case UCOM_SET_BREAK:
    350 		uipaq_break(addr, onoff);
    351 		break;
    352 	default:
    353 		printf("%s: unhandled set request: reg=%x onoff=%x\n",
    354 		    USBDEVNAME(sc->sc_dev), reg, onoff);
    355 		return;
    356 	}
    357 }
    358 
    359 
    360 int
    361 uipaq_activate(device_ptr_t self, enum devact act)
    362 {
    363 	struct uipaq_softc *sc = (struct uipaq_softc *)self;
    364 	int rv = 0;
    365 
    366 	switch (act) {
    367 	case DVACT_ACTIVATE:
    368 		return (EOPNOTSUPP);
    369 		break;
    370 
    371 	case DVACT_DEACTIVATE:
    372 		if (sc->sc_subdev != NULL)
    373 			rv = config_deactivate(sc->sc_subdev);
    374 		sc->sc_dying = 1;
    375 		break;
    376 	}
    377 	return (rv);
    378 }
    379 
    380 int
    381 uipaq_detach(device_ptr_t self, int flags)
    382 {
    383 	struct uipaq_softc *sc = (struct uipaq_softc *)self;
    384 	int rv = 0;
    385 
    386 	DPRINTF(("uipaq_detach: sc=%p flags=%d\n", sc, flags));
    387 	sc->sc_dying = 1;
    388 	if (sc->sc_subdev != NULL) {
    389 		rv |= config_detach(sc->sc_subdev, flags);
    390 		sc->sc_subdev = NULL;
    391 	}
    392 
    393 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
    394 	    USBDEV(sc->sc_dev));
    395 
    396 	return (rv);
    397 }
    398 
    399