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