Home | History | Annotate | Line # | Download | only in usb
uhmodem.c revision 1.3
      1 /*	$NetBSD: uhmodem.c,v 1.3 2008/02/18 05:24:24 dyoung 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.3 2008/02/18 05:24:24 dyoung 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 };
    162 
    163 Static const struct uhmodem_type uhmodem_devs[] = {
    164 	/* HUAWEI E220 / Emobile D0[12]HW */
    165 	{{ USB_VENDOR_HUAWEI, USB_PRODUCT_HUAWEI_E220 }, 2,	E220},
    166 	/* ANYDATA / NTT DoCoMo A2502 */
    167 	{{ USB_VENDOR_ANYDATA, USB_PRODUCT_ANYDATA_A2502 }, 3,	A2502},
    168 };
    169 #define uhmodem_lookup(v, p) ((const struct uhmodem_type *)usb_lookup(uhmodem_devs, v, p))
    170 
    171 int uhmodem_match(device_t, struct cfdata *, void *);
    172 void uhmodem_attach(device_t, device_t, void *);
    173 void uhmodem_childdet(device_t, device_t);
    174 int uhmodem_detach(device_t, int);
    175 int uhmodem_activate(device_t, enum devact);
    176 extern struct cfdriver uhmodem_cd;
    177 CFATTACH_DECL2(uhmodem, sizeof(struct uhmodem_softc), uhmodem_match,
    178     uhmodem_attach, uhmodem_detach, uhmodem_activate, NULL, uhmodem_childdet);
    179 
    180 USB_MATCH(uhmodem)
    181 {
    182 	USB_IFMATCH_START(uhmodem, uaa);
    183 
    184 	if (uhmodem_lookup(uaa->vendor, uaa->product) != NULL)
    185 		/* XXX interface# 0,1 provide modem function, but this driver
    186 		   handles all modem in single device.  */
    187 		if (uaa->ifaceno == 0)
    188 			return (UMATCH_VENDOR_PRODUCT);
    189 	return (UMATCH_NONE);
    190 }
    191 
    192 USB_ATTACH(uhmodem)
    193 {
    194 	USB_IFATTACH_START(uhmodem, sc, uaa);
    195 	usbd_device_handle dev = uaa->device;
    196 	usb_config_descriptor_t *cdesc;
    197 	usb_interface_descriptor_t *id;
    198 	usb_endpoint_descriptor_t *ed;
    199 	char *devinfop;
    200 	const char *devname = USBDEVNAME(sc->sc_ubsa.sc_dev);
    201 	usbd_status err;
    202 	struct ucom_attach_args uca;
    203 	int i;
    204 	int j;
    205 	char comname[16];
    206 
    207 	devinfop = usbd_devinfo_alloc(dev, 0);
    208 	USB_ATTACH_SETUP;
    209 	printf("%s: %s\n", devname, devinfop);
    210 	usbd_devinfo_free(devinfop);
    211 
    212         sc->sc_ubsa.sc_udev = dev;
    213 	sc->sc_ubsa.sc_config_index = UBSA_DEFAULT_CONFIG_INDEX;
    214 	sc->sc_ubsa.sc_numif = 1; /* defaut device has one interface */
    215 
    216 	/* Hauwei E220 need special request to change its mode to modem */
    217 	if ((uaa->ifaceno == 0) && (uaa->class != 255)) {
    218 		err = e220_modechange_request(dev);
    219 		if (err) {
    220 			printf("%s: failed to change mode: %s\n",
    221 				devname, usbd_errstr(err));
    222 			sc->sc_ubsa.sc_dying = 1;
    223 			goto error;
    224 		}
    225 		printf("%s: mass storage only mode, reattach to enable modem\n",
    226 			devname);
    227 		sc->sc_ubsa.sc_dying = 1;
    228 		goto error;
    229 	}
    230 
    231 	/*
    232 	 * initialize rts, dtr variables to something
    233 	 * different from boolean 0, 1
    234 	 */
    235 	sc->sc_ubsa.sc_dtr = -1;
    236 	sc->sc_ubsa.sc_rts = -1;
    237 
    238 	sc->sc_ubsa.sc_quadumts = 1;
    239 	sc->sc_ubsa.sc_config_index = 0;
    240 	sc->sc_ubsa.sc_numif = uhmodem_lookup(uaa->vendor, uaa->product)->uhmodem_coms;
    241 	sc->sc_ubsa.sc_devflags = uhmodem_lookup(uaa->vendor, uaa->product)->uhmodem_flags;
    242 
    243 	DPRINTF(("uhmodem attach: sc = %p\n", sc));
    244 
    245 	/* Move the device into the configured state. */
    246 	err = usbd_set_config_index(dev, sc->sc_ubsa.sc_config_index, 1);
    247 	if (err) {
    248 		printf("%s: failed to set configuration: %s\n",
    249 		    devname, usbd_errstr(err));
    250 		sc->sc_ubsa.sc_dying = 1;
    251 		goto error;
    252 	}
    253 
    254 	/* get the config descriptor */
    255 	cdesc = usbd_get_config_descriptor(sc->sc_ubsa.sc_udev);
    256 	if (cdesc == NULL) {
    257 		printf("%s: failed to get configuration descriptor\n",
    258 		    devname);
    259 		sc->sc_ubsa.sc_dying = 1;
    260 		goto error;
    261 	}
    262 
    263 	sc->sc_ubsa.sc_intr_number = -1;
    264 	sc->sc_ubsa.sc_intr_pipe = NULL;
    265 
    266 	/* get the interfaces */
    267 	for (i = 0; i < sc->sc_ubsa.sc_numif; i++) {
    268 		err = usbd_device2interface_handle(dev, UBSA_IFACE_INDEX_OFFSET+i,
    269 				 &sc->sc_ubsa.sc_iface[i]);
    270 		if (err) {
    271 			if (i == 0){
    272 				/* can not get main interface */
    273 				sc->sc_ubsa.sc_dying = 1;
    274 				goto error;
    275 			} else
    276 				break;
    277 		}
    278 
    279 		/* Find the endpoints */
    280 		id = usbd_get_interface_descriptor(sc->sc_ubsa.sc_iface[i]);
    281 		sc->sc_ubsa.sc_iface_number[i] = id->bInterfaceNumber;
    282 
    283 		/* initialize endpoints */
    284 		uca.bulkin = uca.bulkout = -1;
    285 
    286 		for (j = 0; j < id->bNumEndpoints; j++) {
    287 			ed = usbd_interface2endpoint_descriptor(
    288 				sc->sc_ubsa.sc_iface[i], j);
    289 			if (ed == NULL) {
    290 				printf("%s: no endpoint descriptor for %d (interface: %d)\n",
    291 			    		devname, j, i);
    292 				break;
    293 			}
    294 
    295 			if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    296 			    UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
    297 				sc->sc_ubsa.sc_intr_number = ed->bEndpointAddress;
    298 				sc->sc_ubsa.sc_isize = UGETW(ed->wMaxPacketSize);
    299 			} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    300 			    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
    301 				uca.bulkin = ed->bEndpointAddress;
    302 			} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
    303 			    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
    304 				uca.bulkout = ed->bEndpointAddress;
    305 			}
    306 		} /* end of Endpoint loop */
    307 
    308 		if (sc->sc_ubsa.sc_intr_number == -1) {
    309 			printf("%s: HUAWEI E220 need to re-attach to enable modem function\n", devname);
    310 			if (i == 0) {
    311 				/* could not get intr for main tty */
    312 				sc->sc_ubsa.sc_dying = 1;
    313 				goto error;
    314 			} else
    315 				break;
    316 		}
    317 		if (uca.bulkin == -1) {
    318 			printf("%s: Could not find data bulk in\n", devname);
    319 			sc->sc_ubsa.sc_dying = 1;
    320 			goto error;
    321 		}
    322 
    323 		if (uca.bulkout == -1) {
    324 			printf("%s: Could not find data bulk out\n", devname);
    325 			sc->sc_ubsa.sc_dying = 1;
    326 			goto error;
    327 		}
    328 
    329 		switch (i) {
    330 		case 0:
    331 			sprintf(comname, "modem");
    332 			break;
    333 		case 1:
    334 			sprintf(comname, "alt#1");
    335 			break;
    336 		case 2:
    337 			sprintf(comname, "alt#2");
    338 			break;
    339 		default:
    340 			sprintf(comname, "int#%d", i);
    341 		}
    342 
    343 		uca.portno = i;
    344 		/* bulkin, bulkout set above */
    345 		uca.ibufsize = UHMODEMIBUFSIZE;
    346 		uca.obufsize = UHMODEMOBUFSIZE;
    347 		uca.ibufsizepad = UHMODEMIBUFSIZE;
    348 		uca.opkthdrlen = 0;
    349 		uca.device = dev;
    350 		uca.iface = sc->sc_ubsa.sc_iface[i];
    351 		uca.methods = &uhmodem_methods;
    352 		uca.arg = &sc->sc_ubsa;
    353 		uca.info = comname;
    354 		DPRINTF(("uhmodem: int#=%d, in = 0x%x, out = 0x%x, intr = 0x%x\n",
    355 	    		i, uca.bulkin, uca.bulkout, sc->sc_ubsa.sc_intr_number));
    356 		sc->sc_ubsa.sc_subdevs[i] = config_found_sm_loc(self, "ucombus", NULL,
    357 				 &uca, ucomprint, ucomsubmatch);
    358 
    359 		/* issue endpoint halt to each interface */
    360 		err = uhmodem_endpointhalt(&sc->sc_ubsa, i);
    361 		if (err)
    362 			printf("%s: endpointhalt fail\n", __func__);
    363 		else
    364 			usbd_delay_ms(sc->sc_ubsa.sc_udev, 50);
    365 	} /* end of Interface loop */
    366 
    367 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_ubsa.sc_udev,
    368 			   USBDEV(sc->sc_ubsa.sc_dev));
    369 
    370 	USB_ATTACH_SUCCESS_RETURN;
    371 
    372 error:
    373 	USB_ATTACH_ERROR_RETURN;
    374 }
    375 
    376 void
    377 uhmodem_childdet(device_t self, device_t child)
    378 {
    379 	int i;
    380 	struct uhmodem_softc *sc = device_private(self);
    381 
    382 	for (i = 0; i < sc->sc_ubsa.sc_numif; i++) {
    383 		if (sc->sc_ubsa.sc_subdevs[i] == child)
    384 			break;
    385 	}
    386 	KASSERT(i < sc->sc_ubsa.sc_numif);
    387 	sc->sc_ubsa.sc_subdevs[i] = NULL;
    388 }
    389 
    390 USB_DETACH(uhmodem)
    391 {
    392 	USB_DETACH_START(uhmodem, sc);
    393 	int i;
    394 	int rv = 0;
    395 
    396 	DPRINTF(("uhmodem_detach: sc = %p\n", sc));
    397 
    398 	if (sc->sc_ubsa.sc_intr_pipe != NULL) {
    399 		usbd_abort_pipe(sc->sc_ubsa.sc_intr_pipe);
    400 		usbd_close_pipe(sc->sc_ubsa.sc_intr_pipe);
    401 		free(sc->sc_ubsa.sc_intr_buf, M_USBDEV);
    402 		sc->sc_ubsa.sc_intr_pipe = NULL;
    403 	}
    404 
    405 	sc->sc_ubsa.sc_dying = 1;
    406 	for (i = 0; i < sc->sc_ubsa.sc_numif; i++) {
    407 		if (sc->sc_ubsa.sc_subdevs[i] != NULL)
    408 			rv |= config_detach(sc->sc_ubsa.sc_subdevs[i], flags);
    409 	}
    410 
    411 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_ubsa.sc_udev,
    412 			   USBDEV(sc->sc_ubsa.sc_dev));
    413 
    414 	return (rv);
    415 }
    416 
    417 int
    418 uhmodem_activate(device_t self, enum devact act)
    419 {
    420 	struct uhmodem_softc *sc = device_private(self);
    421 	int rv = 0;
    422 	int i;
    423 
    424 	switch (act) {
    425 	case DVACT_ACTIVATE:
    426 		return (EOPNOTSUPP);
    427 
    428 	case DVACT_DEACTIVATE:
    429 		for (i = 0; i < sc->sc_ubsa.sc_numif; i++) {
    430 			if (sc->sc_ubsa.sc_subdevs[i] != NULL)
    431 				rv |= config_deactivate(sc->sc_ubsa.sc_subdevs[i]);
    432 		}
    433 		sc->sc_ubsa.sc_dying = 1;
    434 		break;
    435 	}
    436 	return (rv);
    437 }
    438 
    439 Static int
    440 uhmodem_open(void *addr, int portno)
    441 {
    442 	struct ubsa_softc *sc = addr;
    443 	usbd_status err;
    444 
    445 	if (sc->sc_dying)
    446 		return (ENXIO);
    447 
    448 	DPRINTF(("%s: sc = %p\n", __func__, sc));
    449 
    450 	err = uhmodem_endpointhalt(sc, 0);
    451 	if (err)
    452 		printf("%s: endpointhalt fail\n", __func__);
    453 	else
    454 		usbd_delay_ms(sc->sc_udev, 50);
    455 
    456 	if (sc->sc_devflags & A2502) {
    457 		err = a2502_init(sc->sc_udev);
    458 		if (err)
    459 			printf("%s: a2502init fail\n", __func__);
    460 		else
    461 			usbd_delay_ms(sc->sc_udev, 50);
    462 	}
    463 #if 0 /* currently disabled */
    464 	if (sc->sc_devflags & E220) {
    465 		err = e220_init(sc->sc_udev);
    466 		if (err)
    467 			printf("%s: e220init fail\n", __func__);
    468 		else
    469 			usbd_delay_ms(sc->sc_udev, 50);
    470 	}
    471 #endif
    472 	if (sc->sc_intr_number != -1 && sc->sc_intr_pipe == NULL) {
    473 		sc->sc_intr_buf = malloc(sc->sc_isize, M_USBDEV, M_WAITOK);
    474 		/* XXX only iface# = 0 has intr line */
    475 		/* XXX E220 specific? need to check */
    476 		err = usbd_open_pipe_intr(sc->sc_iface[0],
    477 		    sc->sc_intr_number,
    478 		    USBD_SHORT_XFER_OK,
    479 		    &sc->sc_intr_pipe,
    480 		    sc,
    481 		    sc->sc_intr_buf,
    482 		    sc->sc_isize,
    483 		    ubsa_intr,
    484 		    UBSA_INTR_INTERVAL);
    485 		if (err) {
    486 			printf("%s: cannot open interrupt pipe (addr %d)\n",
    487 			    USBDEVNAME(sc->sc_dev),
    488 			    sc->sc_intr_number);
    489 			return (EIO);
    490 		}
    491 	}
    492 
    493 	return (0);
    494 }
    495 
    496 /*
    497  * Hauwei E220 needs special request to enable modem function.
    498  * -- DEVICE_REMOTE_WAKEUP ruquest to endpoint 2.
    499  */
    500 Static  usbd_status
    501 e220_modechange_request(usbd_device_handle dev)
    502 {
    503 #define E220_MODE_CHANGE_REQUEST 0x2
    504 	usb_device_request_t req;
    505 	usbd_status err;
    506 
    507 	req.bmRequestType = UT_WRITE_DEVICE;
    508 	req.bRequest = UR_SET_FEATURE;
    509 	USETW(req.wValue, UF_DEVICE_REMOTE_WAKEUP);
    510 	USETW(req.wIndex, E220_MODE_CHANGE_REQUEST);
    511 	USETW(req.wLength, 0);
    512 
    513 	DPRINTF(("%s: send e220 mode change request\n", __func__));
    514 	err = usbd_do_request(dev, &req, 0);
    515 	if (err) {
    516 		DPRINTF(("%s: E220 mode change fail\n", __func__));
    517 		return (EIO);
    518 	}
    519 
    520 	return (0);
    521 #undef E220_MODE_CHANGE_REQUEST
    522 }
    523 
    524 Static  usbd_status
    525 uhmodem_endpointhalt(struct ubsa_softc *sc, int iface)
    526 {
    527 	usb_device_request_t req;
    528 	usb_endpoint_descriptor_t *ed;
    529 	usb_interface_descriptor_t *id;
    530 	usbd_status err;
    531 	int i;
    532 
    533 	/* Find the endpoints */
    534 	id = usbd_get_interface_descriptor(sc->sc_iface[iface]);
    535 
    536 	for (i = 0; i < id->bNumEndpoints; i++) {
    537 		ed = usbd_interface2endpoint_descriptor(sc->sc_iface[iface], i);
    538 		if (ed == NULL)
    539 			return (EIO);
    540 
    541 		if (UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
    542 			/* issue ENDPOINT_HALT request */
    543 			req.bmRequestType = UT_WRITE_ENDPOINT;
    544 			req.bRequest = UR_CLEAR_FEATURE;
    545 			USETW(req.wValue, UF_ENDPOINT_HALT);
    546 			USETW(req.wIndex, ed->bEndpointAddress);
    547 			USETW(req.wLength, 0);
    548 			err = usbd_do_request(sc->sc_udev, &req, 0);
    549 			if (err) {
    550 				DPRINTF(("%s: ENDPOINT_HALT to EP:%d fail\n",
    551 					__func__, ed->bEndpointAddress));
    552 				return (EIO);
    553 			}
    554 
    555 		}
    556 	} /* end of Endpoint loop */
    557 
    558 	return (0);
    559 }
    560 
    561 Static usbd_status
    562 uhmodem_regwrite(usbd_device_handle dev, uint8_t *data, size_t len)
    563 {
    564 	usb_device_request_t req;
    565 	usbd_status err;
    566 
    567 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
    568 	req.bRequest = UHMODEM_REGWRITE;
    569 	USETW(req.wValue, 0x0000);
    570 	USETW(req.wIndex, 0x0000);
    571 	USETW(req.wLength, len);
    572 	err = usbd_do_request(dev, &req, data);
    573 	if (err)
    574 		return err;
    575 
    576 	return 0;
    577 }
    578 
    579 Static usbd_status
    580 uhmodem_regread(usbd_device_handle dev, uint8_t *data, size_t len)
    581 {
    582 	usb_device_request_t req;
    583 	usbd_status err;
    584 
    585 	req.bmRequestType = UT_READ_CLASS_INTERFACE;
    586 	req.bRequest = UHMODEM_REGREAD;
    587 	USETW(req.wValue, 0x0000);
    588 	USETW(req.wIndex, 0x0000);
    589 	USETW(req.wLength, len);
    590 	err = usbd_do_request(dev, &req, data);
    591 
    592 	if (err)
    593 		return err;
    594 
    595 	return 0;
    596 }
    597 
    598 #if 0
    599 Static usbd_status
    600 uhmodem_regsetup(usbd_device_handle dev, uint16_t cmd)
    601 {
    602 	usb_device_request_t req;
    603 	usbd_status err;
    604 
    605 	req.bmRequestType = UT_READ_CLASS_INTERFACE;
    606 	req.bRequest = UHMODEM_SETUP;
    607 	USETW(req.wValue, cmd);
    608 	USETW(req.wIndex, 0x0000);
    609 	USETW(req.wLength, 0);
    610 	err = usbd_do_request(dev, &req, 0);
    611 
    612 	if (err)
    613 		return err;
    614 
    615 	return 0;
    616 }
    617 #endif
    618 
    619 Static  usbd_status
    620 a2502_init(usbd_device_handle dev)
    621 {
    622 	uint8_t data[8];
    623 	static uint8_t init_cmd[] = {0x00, 0xE1, 0x00, 0x00, 0x00, 0x00, 0x08};
    624 #ifdef UHMODEM_DEBUG
    625 	int i;
    626 #endif
    627 	if (uhmodem_regread(dev, data, 7)) {
    628 		DPRINTF(("%s: read fail\n", __func__));
    629 		return EIO;
    630 	}
    631 #ifdef UHMODEM_DEBUG
    632 	printf("%s: readdata: ", __func__);
    633 	for (i = 0; i < 7; i++)
    634 		printf("0x%x ", data[i]);
    635 #endif
    636 	if (uhmodem_regwrite(dev, init_cmd, sizeof(init_cmd)) ) {
    637 		DPRINTF(("%s: write fail\n", __func__));
    638 		return EIO;
    639 	}
    640 
    641 	if (uhmodem_regread(dev, data, 7)) {
    642 		DPRINTF(("%s: read fail\n", __func__));
    643 		return EIO;
    644 	}
    645 #ifdef UHMODEM_DEBUG
    646 	printf("%s: readdata: ", __func__);
    647 	printf(" => ");
    648 	for (i = 0; i < 7; i++)
    649 		printf("0x%x ", data[i]);
    650 	printf("\n");
    651 #endif
    652 	return 0;
    653 }
    654 
    655 
    656 #if 0
    657 /*
    658  * Windows device driver send these sequens of USB requests.
    659  * However currently I can't understand what the messege is,
    660  * disable this code when I get more information about it.
    661  */
    662 Static  usbd_status
    663 e220_init(usbd_device_handle dev)
    664 {
    665 	uint8_t data[8];
    666 	usb_device_request_t req;
    667 	int i;
    668 
    669 	/* vendor specific unknown request */
    670 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
    671 	req.bRequest = 0x02;
    672 	USETW(req.wValue, 0x0001);
    673 	USETW(req.wIndex, 0x0000);
    674 	USETW(req.wLength, 2);
    675 	data[0] = 0x0;
    676 	data[1] = 0x0;
    677 	if (usbd_do_request(dev, &req, data))
    678 		goto error;
    679 
    680 	/* vendor specific unknown sequence */
    681 	if(uhmodem_regsetup(dev, 0x1))
    682 		goto error;
    683 
    684 	if (uhmodem_regread(dev, data, 7))
    685 		goto error;
    686 
    687 	data[1] = 0x8;
    688 	data[2] = 0x7;
    689 	if (uhmodem_regwrite(dev, data, sizeof(data)) )
    690 		goto error;
    691 
    692 	if (uhmodem_regread(dev, data, 7))
    693 		goto error;
    694 		/* XXX should verify the read data ? */
    695 
    696 	if (uhmodem_regsetup(dev, 0x3))
    697 		goto error;
    698 
    699 	return (0);
    700 error:
    701 	DPRINTF(("%s: E220 init request fail\n", __func__));
    702 	return (EIO);
    703 }
    704 #endif
    705 
    706