Home | History | Annotate | Line # | Download | only in usb
uhmodem.c revision 1.5
      1 /*	$NetBSD: uhmodem.c,v 1.5 2008/04/10 19:49:14 cegger Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2008 Yojiro UO <yuo (at) nui.org>.
      5  * All rights reserved.
      6  *
      7  * Permission to use, copy, modify, and distribute this software for any
      8  * purpose with or without fee is hereby granted, provided that the above
      9  * copyright notice and this permission notice appear in all copies.
     10  *
     11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
     12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
     14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     15  * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER IN
     16  * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
     17  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     18  */
     19 /*-
     20  * Copyright (c) 2002, Alexander Kabaev <kan.FreeBSD.org>.
     21  * All rights reserved.
     22  *
     23  * Redistribution and use in source and binary forms, with or without
     24  * modification, are permitted provided that the following conditions
     25  * are met:
     26  * 1. Redistributions of source code must retain the above copyright
     27  *    notice, this list of conditions and the following disclaimer.
     28  * 2. Redistributions in binary form must reproduce the above copyright
     29  *    notice, this list of conditions and the following disclaimer in the
     30  *    documentation and/or other materials provided with the distribution.
     31  *
     32  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     33  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     34  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     35  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     36  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     37  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     38  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     39  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     40  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     41  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     42  * SUCH DAMAGE.
     43  */
     44 /*
     45  * Copyright (c) 2001 The NetBSD Foundation, Inc.
     46  * All rights reserved.
     47  *
     48  * This code is derived from software contributed to The NetBSD Foundation
     49  * by Ichiro FUKUHARA (ichiro (at) ichiro.org).
     50  *
     51  * Redistribution and use in source and binary forms, with or without
     52  * modification, are permitted provided that the following conditions
     53  * are met:
     54  * 1. Redistributions of source code must retain the above copyright
     55  *    notice, this list of conditions and the following disclaimer.
     56  * 2. Redistributions in binary form must reproduce the above copyright
     57  *    notice, this list of conditions and the following disclaimer in the
     58  *    documentation and/or other materials provided with the distribution.
     59  * 3. All advertising materials mentioning features or use of this software
     60  *    must display the following acknowledgement:
     61  *        This product includes software developed by the NetBSD
     62  *        Foundation, Inc. and its contributors.
     63  * 4. Neither the name of The NetBSD Foundation nor the names of its
     64  *    contributors may be used to endorse or promote products derived
     65  *    from this software without specific prior written permission.
     66  *
     67  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     68  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     69  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     70  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     71  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     72  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     73  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     74  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     75  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     76  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     77  * POSSIBILITY OF SUCH DAMAGE.
     78  */
     79 
     80 #include <sys/cdefs.h>
     81 __KERNEL_RCSID(0, "$NetBSD: uhmodem.c,v 1.5 2008/04/10 19:49:14 cegger Exp $");
     82 
     83 #include <sys/param.h>
     84 #include <sys/systm.h>
     85 #include <sys/kernel.h>
     86 #include <sys/malloc.h>
     87 #include <sys/ioccom.h>
     88 #include <sys/fcntl.h>
     89 #include <sys/conf.h>
     90 #include <sys/tty.h>
     91 #include <sys/file.h>
     92 #include <sys/select.h>
     93 #include <sys/proc.h>
     94 #include <sys/device.h>
     95 #include <sys/poll.h>
     96 #include <sys/sysctl.h>
     97 #include <sys/bus.h>
     98 
     99 #include <dev/usb/usb.h>
    100 #include <dev/usb/usbdi.h>
    101 #include <dev/usb/usbdi_util.h>
    102 #include <dev/usb/usbdivar.h>
    103 
    104 #include <dev/usb/usbcdc.h>
    105 #include <dev/usb/usbdevs.h>
    106 #include <dev/usb/usb_quirks.h>
    107 #include <dev/usb/ucomvar.h>
    108 #include <dev/usb/ubsavar.h>
    109 
    110 /* vendor specific bRequest */
    111 #define	UHMODEM_REGWRITE	0x20
    112 #define	UHMODEM_REGREAD		0x21
    113 #define UHMODEM_SETUP		0x22
    114 
    115 #define UHMODEMIBUFSIZE	4096
    116 #define UHMODEMOBUFSIZE	4096
    117 
    118 #ifdef UHMODEM_DEBUG
    119 Static int	uhmodemdebug = 0;
    120 #define DPRINTFN(n, x)  do { \
    121 				if (uhmodemdebug > (n)) \
    122 					logprintf x; \
    123 			} while (0)
    124 #else
    125 #define DPRINTFN(n, x)
    126 #endif
    127 #define DPRINTF(x) DPRINTFN(0, x)
    128 
    129 Static int uhmodem_open(void *, int);
    130 Static  usbd_status e220_modechange_request(usbd_device_handle);
    131 Static	usbd_status uhmodem_endpointhalt(struct ubsa_softc *, int);
    132 Static	usbd_status uhmodem_regwrite(usbd_device_handle, uint8_t *, size_t);
    133 Static	usbd_status uhmodem_regread(usbd_device_handle, uint8_t *, size_t);
    134 Static  usbd_status a2502_init(usbd_device_handle);
    135 #if 0
    136 Static	usbd_status uhmodem_regsetup(usbd_device_handle, uint16_t);
    137 Static  usbd_status e220_init(usbd_device_handle);
    138 #endif
    139 
    140 struct	uhmodem_softc {
    141 	struct ubsa_softc	sc_ubsa;
    142 };
    143 
    144 struct	ucom_methods uhmodem_methods = {
    145 	ubsa_get_status,
    146 	ubsa_set,
    147 	ubsa_param,
    148 	NULL,
    149 	uhmodem_open,
    150 	ubsa_close,
    151 	NULL,
    152 	NULL
    153 };
    154 
    155 struct uhmodem_type {
    156 	struct usb_devno	uhmodem_dev;
    157 	u_int16_t		uhmodem_coms;	/* number of serial interfaces on the device */
    158 	u_int16_t		uhmodem_flags;
    159 #define	E220	0x0001
    160 #define	A2502	0x0002
    161 #define	E620	0x0004		/* XXX */
    162 				/* Whether or not it is a device different from E220 is not clear. */
    163 };
    164 
    165 Static const struct uhmodem_type uhmodem_devs[] = {
    166 	/* HUAWEI E220 / Emobile D0[12]HW */
    167 	{{ USB_VENDOR_HUAWEI, USB_PRODUCT_HUAWEI_E220 }, 2,	E220},
    168 	/* ANYDATA / NTT DoCoMo A2502 */
    169 	{{ USB_VENDOR_ANYDATA, USB_PRODUCT_ANYDATA_A2502 }, 3,	A2502},
    170 	/* HUAWEI E620 */
    171 	{{ USB_VENDOR_HUAWEI, USB_PRODUCT_HUAWEI_MOBILE }, 3,   E620},
    172 };
    173 #define uhmodem_lookup(v, p) ((const struct uhmodem_type *)usb_lookup(uhmodem_devs, v, p))
    174 
    175 int uhmodem_match(device_t, struct cfdata *, void *);
    176 void uhmodem_attach(device_t, device_t, void *);
    177 void uhmodem_childdet(device_t, device_t);
    178 int uhmodem_detach(device_t, int);
    179 int uhmodem_activate(device_t, enum devact);
    180 extern struct cfdriver uhmodem_cd;
    181 CFATTACH_DECL2(uhmodem, sizeof(struct uhmodem_softc), uhmodem_match,
    182     uhmodem_attach, uhmodem_detach, uhmodem_activate, NULL, uhmodem_childdet);
    183 
    184 USB_MATCH(uhmodem)
    185 {
    186 	USB_IFMATCH_START(uhmodem, uaa);
    187 
    188 	if (uhmodem_lookup(uaa->vendor, uaa->product) != NULL)
    189 		/* XXX interface# 0,1 provide modem function, but this driver
    190 		   handles all modem in single device.  */
    191 		if (uaa->ifaceno == 0)
    192 			return (UMATCH_VENDOR_PRODUCT);
    193 	return (UMATCH_NONE);
    194 }
    195 
    196 USB_ATTACH(uhmodem)
    197 {
    198 	USB_IFATTACH_START(uhmodem, sc, uaa);
    199 	usbd_device_handle dev = uaa->device;
    200 	usb_config_descriptor_t *cdesc;
    201 	usb_interface_descriptor_t *id;
    202 	usb_endpoint_descriptor_t *ed;
    203 	char *devinfop;
    204 	const char *devname = USBDEVNAME(sc->sc_ubsa.sc_dev);
    205 	usbd_status err;
    206 	struct ucom_attach_args uca;
    207 	int i;
    208 	int j;
    209 	char comname[16];
    210 
    211 	devinfop = usbd_devinfo_alloc(dev, 0);
    212 	USB_ATTACH_SETUP;
    213 	printf("%s: %s\n", devname, devinfop);
    214 	usbd_devinfo_free(devinfop);
    215 
    216         sc->sc_ubsa.sc_udev = dev;
    217 	sc->sc_ubsa.sc_config_index = UBSA_DEFAULT_CONFIG_INDEX;
    218 	sc->sc_ubsa.sc_numif = 1; /* defaut device has one interface */
    219 
    220 	/* Hauwei E220 need special request to change its mode to modem */
    221 	if ((uaa->ifaceno == 0) && (uaa->class != 255)) {
    222 		err = e220_modechange_request(dev);
    223 		if (err) {
    224 			printf("%s: failed to change mode: %s\n",
    225 				devname, usbd_errstr(err));
    226 			sc->sc_ubsa.sc_dying = 1;
    227 			goto error;
    228 		}
    229 		printf("%s: mass storage only mode, reattach to enable modem\n",
    230 			devname);
    231 		sc->sc_ubsa.sc_dying = 1;
    232 		goto error;
    233 	}
    234 
    235 	/*
    236 	 * initialize rts, dtr variables to something
    237 	 * different from boolean 0, 1
    238 	 */
    239 	sc->sc_ubsa.sc_dtr = -1;
    240 	sc->sc_ubsa.sc_rts = -1;
    241 
    242 	sc->sc_ubsa.sc_quadumts = 1;
    243 	sc->sc_ubsa.sc_config_index = 0;
    244 	sc->sc_ubsa.sc_numif = uhmodem_lookup(uaa->vendor, uaa->product)->uhmodem_coms;
    245 	sc->sc_ubsa.sc_devflags = uhmodem_lookup(uaa->vendor, uaa->product)->uhmodem_flags;
    246 
    247 	DPRINTF(("uhmodem attach: sc = %p\n", sc));
    248 
    249 	/* Move the device into the configured state. */
    250 	err = usbd_set_config_index(dev, sc->sc_ubsa.sc_config_index, 1);
    251 	if (err) {
    252 		printf("%s: failed to set configuration: %s\n",
    253 		    devname, usbd_errstr(err));
    254 		sc->sc_ubsa.sc_dying = 1;
    255 		goto error;
    256 	}
    257 
    258 	/* get the config descriptor */
    259 	cdesc = usbd_get_config_descriptor(sc->sc_ubsa.sc_udev);
    260 	if (cdesc == NULL) {
    261 		printf("%s: failed to get configuration descriptor\n",
    262 		    devname);
    263 		sc->sc_ubsa.sc_dying = 1;
    264 		goto error;
    265 	}
    266 
    267 	sc->sc_ubsa.sc_intr_number = -1;
    268 	sc->sc_ubsa.sc_intr_pipe = NULL;
    269 
    270 	/* get the interfaces */
    271 	for (i = 0; i < sc->sc_ubsa.sc_numif; i++) {
    272 		err = usbd_device2interface_handle(dev, UBSA_IFACE_INDEX_OFFSET+i,
    273 				 &sc->sc_ubsa.sc_iface[i]);
    274 		if (err) {
    275 			if (i == 0){
    276 				/* can not get main interface */
    277 				sc->sc_ubsa.sc_dying = 1;
    278 				goto error;
    279 			} else
    280 				break;
    281 		}
    282 
    283 		/* Find the endpoints */
    284 		id = usbd_get_interface_descriptor(sc->sc_ubsa.sc_iface[i]);
    285 		sc->sc_ubsa.sc_iface_number[i] = id->bInterfaceNumber;
    286 
    287 		/* initialize endpoints */
    288 		uca.bulkin = uca.bulkout = -1;
    289 
    290 		for (j = 0; j < id->bNumEndpoints; j++) {
    291 			ed = usbd_interface2endpoint_descriptor(
    292 				sc->sc_ubsa.sc_iface[i], j);
    293 			if (ed == NULL) {
    294 				printf("%s: no endpoint descriptor for %d (interface: %d)\n",
    295 			    		devname, j, i);
    296 				break;
    297 			}
    298 
    299 			if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    300 			    UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
    301 				sc->sc_ubsa.sc_intr_number = ed->bEndpointAddress;
    302 				sc->sc_ubsa.sc_isize = UGETW(ed->wMaxPacketSize);
    303 			} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    304 			    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
    305 				uca.bulkin = ed->bEndpointAddress;
    306 			} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
    307 			    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
    308 				uca.bulkout = ed->bEndpointAddress;
    309 			}
    310 		} /* end of Endpoint loop */
    311 
    312 		if (sc->sc_ubsa.sc_intr_number == -1) {
    313 			printf("%s: HUAWEI E220 need to re-attach to enable modem function\n", devname);
    314 			if (i == 0) {
    315 				/* could not get intr for main tty */
    316 				sc->sc_ubsa.sc_dying = 1;
    317 				goto error;
    318 			} else
    319 				break;
    320 		}
    321 		if (uca.bulkin == -1) {
    322 			printf("%s: Could not find data bulk in\n", devname);
    323 			sc->sc_ubsa.sc_dying = 1;
    324 			goto error;
    325 		}
    326 
    327 		if (uca.bulkout == -1) {
    328 			printf("%s: Could not find data bulk out\n", devname);
    329 			sc->sc_ubsa.sc_dying = 1;
    330 			goto error;
    331 		}
    332 
    333 		switch (i) {
    334 		case 0:
    335 			snprintf(comname, sizeof(comname), "modem");
    336 			break;
    337 		case 1:
    338 			snprintf(comname, sizeof(comname), "alt#1");
    339 			break;
    340 		case 2:
    341 			snprintf(comname, sizeof(comname), "alt#2");
    342 			break;
    343 		default:
    344 			snprintf(comname, sizeof(comname), "int#%d", i);
    345 			break;
    346 		}
    347 
    348 		uca.portno = i;
    349 		/* bulkin, bulkout set above */
    350 		uca.ibufsize = UHMODEMIBUFSIZE;
    351 		uca.obufsize = UHMODEMOBUFSIZE;
    352 		uca.ibufsizepad = UHMODEMIBUFSIZE;
    353 		uca.opkthdrlen = 0;
    354 		uca.device = dev;
    355 		uca.iface = sc->sc_ubsa.sc_iface[i];
    356 		uca.methods = &uhmodem_methods;
    357 		uca.arg = &sc->sc_ubsa;
    358 		uca.info = comname;
    359 		DPRINTF(("uhmodem: int#=%d, in = 0x%x, out = 0x%x, intr = 0x%x\n",
    360 	    		i, uca.bulkin, uca.bulkout, sc->sc_ubsa.sc_intr_number));
    361 		sc->sc_ubsa.sc_subdevs[i] = config_found_sm_loc(self, "ucombus", NULL,
    362 				 &uca, ucomprint, ucomsubmatch);
    363 
    364 		/* issue endpoint halt to each interface */
    365 		err = uhmodem_endpointhalt(&sc->sc_ubsa, i);
    366 		if (err)
    367 			printf("%s: endpointhalt fail\n", __func__);
    368 		else
    369 			usbd_delay_ms(sc->sc_ubsa.sc_udev, 50);
    370 	} /* end of Interface loop */
    371 
    372 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_ubsa.sc_udev,
    373 			   USBDEV(sc->sc_ubsa.sc_dev));
    374 
    375 	USB_ATTACH_SUCCESS_RETURN;
    376 
    377 error:
    378 	USB_ATTACH_ERROR_RETURN;
    379 }
    380 
    381 void
    382 uhmodem_childdet(device_t self, device_t child)
    383 {
    384 	int i;
    385 	struct uhmodem_softc *sc = device_private(self);
    386 
    387 	for (i = 0; i < sc->sc_ubsa.sc_numif; i++) {
    388 		if (sc->sc_ubsa.sc_subdevs[i] == child)
    389 			break;
    390 	}
    391 	KASSERT(i < sc->sc_ubsa.sc_numif);
    392 	sc->sc_ubsa.sc_subdevs[i] = NULL;
    393 }
    394 
    395 USB_DETACH(uhmodem)
    396 {
    397 	USB_DETACH_START(uhmodem, sc);
    398 	int i;
    399 	int rv = 0;
    400 
    401 	DPRINTF(("uhmodem_detach: sc = %p\n", sc));
    402 
    403 	if (sc->sc_ubsa.sc_intr_pipe != NULL) {
    404 		usbd_abort_pipe(sc->sc_ubsa.sc_intr_pipe);
    405 		usbd_close_pipe(sc->sc_ubsa.sc_intr_pipe);
    406 		free(sc->sc_ubsa.sc_intr_buf, M_USBDEV);
    407 		sc->sc_ubsa.sc_intr_pipe = NULL;
    408 	}
    409 
    410 	sc->sc_ubsa.sc_dying = 1;
    411 	for (i = 0; i < sc->sc_ubsa.sc_numif; i++) {
    412 		if (sc->sc_ubsa.sc_subdevs[i] != NULL)
    413 			rv |= config_detach(sc->sc_ubsa.sc_subdevs[i], flags);
    414 	}
    415 
    416 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_ubsa.sc_udev,
    417 			   USBDEV(sc->sc_ubsa.sc_dev));
    418 
    419 	return (rv);
    420 }
    421 
    422 int
    423 uhmodem_activate(device_t self, enum devact act)
    424 {
    425 	struct uhmodem_softc *sc = device_private(self);
    426 	int rv = 0;
    427 	int i;
    428 
    429 	switch (act) {
    430 	case DVACT_ACTIVATE:
    431 		return (EOPNOTSUPP);
    432 
    433 	case DVACT_DEACTIVATE:
    434 		for (i = 0; i < sc->sc_ubsa.sc_numif; i++) {
    435 			if (sc->sc_ubsa.sc_subdevs[i] != NULL)
    436 				rv |= config_deactivate(sc->sc_ubsa.sc_subdevs[i]);
    437 		}
    438 		sc->sc_ubsa.sc_dying = 1;
    439 		break;
    440 	}
    441 	return (rv);
    442 }
    443 
    444 Static int
    445 uhmodem_open(void *addr, int portno)
    446 {
    447 	struct ubsa_softc *sc = addr;
    448 	usbd_status err;
    449 
    450 	if (sc->sc_dying)
    451 		return (ENXIO);
    452 
    453 	DPRINTF(("%s: sc = %p\n", __func__, sc));
    454 
    455 	err = uhmodem_endpointhalt(sc, 0);
    456 	if (err)
    457 		printf("%s: endpointhalt fail\n", __func__);
    458 	else
    459 		usbd_delay_ms(sc->sc_udev, 50);
    460 
    461 	if (sc->sc_devflags & A2502) {
    462 		err = a2502_init(sc->sc_udev);
    463 		if (err)
    464 			printf("%s: a2502init fail\n", __func__);
    465 		else
    466 			usbd_delay_ms(sc->sc_udev, 50);
    467 	}
    468 #if 0 /* currently disabled */
    469 	if (sc->sc_devflags & E220) {
    470 		err = e220_init(sc->sc_udev);
    471 		if (err)
    472 			printf("%s: e220init fail\n", __func__);
    473 		else
    474 			usbd_delay_ms(sc->sc_udev, 50);
    475 	}
    476 #endif
    477 	if (sc->sc_intr_number != -1 && sc->sc_intr_pipe == NULL) {
    478 		sc->sc_intr_buf = malloc(sc->sc_isize, M_USBDEV, M_WAITOK);
    479 		/* XXX only iface# = 0 has intr line */
    480 		/* XXX E220 specific? need to check */
    481 		err = usbd_open_pipe_intr(sc->sc_iface[0],
    482 		    sc->sc_intr_number,
    483 		    USBD_SHORT_XFER_OK,
    484 		    &sc->sc_intr_pipe,
    485 		    sc,
    486 		    sc->sc_intr_buf,
    487 		    sc->sc_isize,
    488 		    ubsa_intr,
    489 		    UBSA_INTR_INTERVAL);
    490 		if (err) {
    491 			printf("%s: cannot open interrupt pipe (addr %d)\n",
    492 			    USBDEVNAME(sc->sc_dev),
    493 			    sc->sc_intr_number);
    494 			return (EIO);
    495 		}
    496 	}
    497 
    498 	return (0);
    499 }
    500 
    501 /*
    502  * Hauwei E220 needs special request to enable modem function.
    503  * -- DEVICE_REMOTE_WAKEUP ruquest to endpoint 2.
    504  */
    505 Static  usbd_status
    506 e220_modechange_request(usbd_device_handle dev)
    507 {
    508 #define E220_MODE_CHANGE_REQUEST 0x2
    509 	usb_device_request_t req;
    510 	usbd_status err;
    511 
    512 	req.bmRequestType = UT_WRITE_DEVICE;
    513 	req.bRequest = UR_SET_FEATURE;
    514 	USETW(req.wValue, UF_DEVICE_REMOTE_WAKEUP);
    515 	USETW(req.wIndex, E220_MODE_CHANGE_REQUEST);
    516 	USETW(req.wLength, 0);
    517 
    518 	DPRINTF(("%s: send e220 mode change request\n", __func__));
    519 	err = usbd_do_request(dev, &req, 0);
    520 	if (err) {
    521 		DPRINTF(("%s: E220 mode change fail\n", __func__));
    522 		return (EIO);
    523 	}
    524 
    525 	return (0);
    526 #undef E220_MODE_CHANGE_REQUEST
    527 }
    528 
    529 Static  usbd_status
    530 uhmodem_endpointhalt(struct ubsa_softc *sc, int iface)
    531 {
    532 	usb_device_request_t req;
    533 	usb_endpoint_descriptor_t *ed;
    534 	usb_interface_descriptor_t *id;
    535 	usbd_status err;
    536 	int i;
    537 
    538 	/* Find the endpoints */
    539 	id = usbd_get_interface_descriptor(sc->sc_iface[iface]);
    540 
    541 	for (i = 0; i < id->bNumEndpoints; i++) {
    542 		ed = usbd_interface2endpoint_descriptor(sc->sc_iface[iface], i);
    543 		if (ed == NULL)
    544 			return (EIO);
    545 
    546 		if (UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
    547 			/* issue ENDPOINT_HALT request */
    548 			req.bmRequestType = UT_WRITE_ENDPOINT;
    549 			req.bRequest = UR_CLEAR_FEATURE;
    550 			USETW(req.wValue, UF_ENDPOINT_HALT);
    551 			USETW(req.wIndex, ed->bEndpointAddress);
    552 			USETW(req.wLength, 0);
    553 			err = usbd_do_request(sc->sc_udev, &req, 0);
    554 			if (err) {
    555 				DPRINTF(("%s: ENDPOINT_HALT to EP:%d fail\n",
    556 					__func__, ed->bEndpointAddress));
    557 				return (EIO);
    558 			}
    559 
    560 		}
    561 	} /* end of Endpoint loop */
    562 
    563 	return (0);
    564 }
    565 
    566 Static usbd_status
    567 uhmodem_regwrite(usbd_device_handle dev, uint8_t *data, size_t len)
    568 {
    569 	usb_device_request_t req;
    570 	usbd_status err;
    571 
    572 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
    573 	req.bRequest = UHMODEM_REGWRITE;
    574 	USETW(req.wValue, 0x0000);
    575 	USETW(req.wIndex, 0x0000);
    576 	USETW(req.wLength, len);
    577 	err = usbd_do_request(dev, &req, data);
    578 	if (err)
    579 		return err;
    580 
    581 	return 0;
    582 }
    583 
    584 Static usbd_status
    585 uhmodem_regread(usbd_device_handle dev, uint8_t *data, size_t len)
    586 {
    587 	usb_device_request_t req;
    588 	usbd_status err;
    589 
    590 	req.bmRequestType = UT_READ_CLASS_INTERFACE;
    591 	req.bRequest = UHMODEM_REGREAD;
    592 	USETW(req.wValue, 0x0000);
    593 	USETW(req.wIndex, 0x0000);
    594 	USETW(req.wLength, len);
    595 	err = usbd_do_request(dev, &req, data);
    596 
    597 	if (err)
    598 		return err;
    599 
    600 	return 0;
    601 }
    602 
    603 #if 0
    604 Static usbd_status
    605 uhmodem_regsetup(usbd_device_handle dev, uint16_t cmd)
    606 {
    607 	usb_device_request_t req;
    608 	usbd_status err;
    609 
    610 	req.bmRequestType = UT_READ_CLASS_INTERFACE;
    611 	req.bRequest = UHMODEM_SETUP;
    612 	USETW(req.wValue, cmd);
    613 	USETW(req.wIndex, 0x0000);
    614 	USETW(req.wLength, 0);
    615 	err = usbd_do_request(dev, &req, 0);
    616 
    617 	if (err)
    618 		return err;
    619 
    620 	return 0;
    621 }
    622 #endif
    623 
    624 Static  usbd_status
    625 a2502_init(usbd_device_handle dev)
    626 {
    627 	uint8_t data[8];
    628 	static uint8_t init_cmd[] = {0x00, 0xE1, 0x00, 0x00, 0x00, 0x00, 0x08};
    629 #ifdef UHMODEM_DEBUG
    630 	int i;
    631 #endif
    632 	if (uhmodem_regread(dev, data, 7)) {
    633 		DPRINTF(("%s: read fail\n", __func__));
    634 		return EIO;
    635 	}
    636 #ifdef UHMODEM_DEBUG
    637 	printf("%s: readdata: ", __func__);
    638 	for (i = 0; i < 7; i++)
    639 		printf("0x%x ", data[i]);
    640 #endif
    641 	if (uhmodem_regwrite(dev, init_cmd, sizeof(init_cmd)) ) {
    642 		DPRINTF(("%s: write fail\n", __func__));
    643 		return EIO;
    644 	}
    645 
    646 	if (uhmodem_regread(dev, data, 7)) {
    647 		DPRINTF(("%s: read fail\n", __func__));
    648 		return EIO;
    649 	}
    650 #ifdef UHMODEM_DEBUG
    651 	printf("%s: readdata: ", __func__);
    652 	printf(" => ");
    653 	for (i = 0; i < 7; i++)
    654 		printf("0x%x ", data[i]);
    655 	printf("\n");
    656 #endif
    657 	return 0;
    658 }
    659 
    660 
    661 #if 0
    662 /*
    663  * Windows device driver send these sequens of USB requests.
    664  * However currently I can't understand what the messege is,
    665  * disable this code when I get more information about it.
    666  */
    667 Static  usbd_status
    668 e220_init(usbd_device_handle dev)
    669 {
    670 	uint8_t data[8];
    671 	usb_device_request_t req;
    672 	int i;
    673 
    674 	/* vendor specific unknown request */
    675 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
    676 	req.bRequest = 0x02;
    677 	USETW(req.wValue, 0x0001);
    678 	USETW(req.wIndex, 0x0000);
    679 	USETW(req.wLength, 2);
    680 	data[0] = 0x0;
    681 	data[1] = 0x0;
    682 	if (usbd_do_request(dev, &req, data))
    683 		goto error;
    684 
    685 	/* vendor specific unknown sequence */
    686 	if(uhmodem_regsetup(dev, 0x1))
    687 		goto error;
    688 
    689 	if (uhmodem_regread(dev, data, 7))
    690 		goto error;
    691 
    692 	data[1] = 0x8;
    693 	data[2] = 0x7;
    694 	if (uhmodem_regwrite(dev, data, sizeof(data)) )
    695 		goto error;
    696 
    697 	if (uhmodem_regread(dev, data, 7))
    698 		goto error;
    699 		/* XXX should verify the read data ? */
    700 
    701 	if (uhmodem_regsetup(dev, 0x3))
    702 		goto error;
    703 
    704 	return (0);
    705 error:
    706 	DPRINTF(("%s: E220 init request fail\n", __func__));
    707 	return (EIO);
    708 }
    709 #endif
    710 
    711