Home | History | Annotate | Line # | Download | only in usb
uhmodem.c revision 1.10
      1 /*	$NetBSD: uhmodem.c,v 1.10 2010/11/03 23:46:35 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  *
     60  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     61  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     62  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     63  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     64  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     65  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     66  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     67  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     68  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     69  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     70  * POSSIBILITY OF SUCH DAMAGE.
     71  */
     72 
     73 #include <sys/cdefs.h>
     74 __KERNEL_RCSID(0, "$NetBSD: uhmodem.c,v 1.10 2010/11/03 23:46:35 dyoung Exp $");
     75 
     76 #include <sys/param.h>
     77 #include <sys/systm.h>
     78 #include <sys/kernel.h>
     79 #include <sys/malloc.h>
     80 #include <sys/ioccom.h>
     81 #include <sys/fcntl.h>
     82 #include <sys/conf.h>
     83 #include <sys/tty.h>
     84 #include <sys/file.h>
     85 #include <sys/select.h>
     86 #include <sys/proc.h>
     87 #include <sys/device.h>
     88 #include <sys/poll.h>
     89 #include <sys/sysctl.h>
     90 #include <sys/bus.h>
     91 
     92 #include <dev/usb/usb.h>
     93 #include <dev/usb/usbdi.h>
     94 #include <dev/usb/usbdi_util.h>
     95 #include <dev/usb/usbdivar.h>
     96 
     97 #include <dev/usb/usb_port.h>
     98 
     99 #include <dev/usb/usbcdc.h>
    100 #include <dev/usb/usbdevs.h>
    101 #include <dev/usb/usb_quirks.h>
    102 #include <dev/usb/ucomvar.h>
    103 #include <dev/usb/ubsavar.h>
    104 
    105 /* vendor specific bRequest */
    106 #define	UHMODEM_REGWRITE	0x20
    107 #define	UHMODEM_REGREAD		0x21
    108 #define UHMODEM_SETUP		0x22
    109 
    110 #define UHMODEMIBUFSIZE	4096
    111 #define UHMODEMOBUFSIZE	4096
    112 
    113 #ifdef UHMODEM_DEBUG
    114 Static int	uhmodemdebug = 0;
    115 #define DPRINTFN(n, x)  do { \
    116 				if (uhmodemdebug > (n)) \
    117 					printf x; \
    118 			} while (0)
    119 #else
    120 #define DPRINTFN(n, x)
    121 #endif
    122 #define DPRINTF(x) DPRINTFN(0, x)
    123 
    124 Static int uhmodem_open(void *, int);
    125 Static  usbd_status e220_modechange_request(usbd_device_handle);
    126 Static	usbd_status uhmodem_endpointhalt(struct ubsa_softc *, int);
    127 Static	usbd_status uhmodem_regwrite(usbd_device_handle, uint8_t *, size_t);
    128 Static	usbd_status uhmodem_regread(usbd_device_handle, uint8_t *, size_t);
    129 Static  usbd_status a2502_init(usbd_device_handle);
    130 #if 0
    131 Static	usbd_status uhmodem_regsetup(usbd_device_handle, uint16_t);
    132 Static  usbd_status e220_init(usbd_device_handle);
    133 #endif
    134 
    135 struct	uhmodem_softc {
    136 	struct ubsa_softc	sc_ubsa;
    137 };
    138 
    139 struct	ucom_methods uhmodem_methods = {
    140 	ubsa_get_status,
    141 	ubsa_set,
    142 	ubsa_param,
    143 	NULL,
    144 	uhmodem_open,
    145 	ubsa_close,
    146 	NULL,
    147 	NULL
    148 };
    149 
    150 struct uhmodem_type {
    151 	struct usb_devno	uhmodem_dev;
    152 	u_int16_t		uhmodem_coms;	/* number of serial interfaces on the device */
    153 	u_int16_t		uhmodem_flags;
    154 #define	E220	0x0001
    155 #define	A2502	0x0002
    156 #define	E620	0x0004		/* XXX */
    157 				/* Whether or not it is a device different from E220 is not clear. */
    158 };
    159 
    160 Static const struct uhmodem_type uhmodem_devs[] = {
    161 	/* HUAWEI E220 / Emobile D0[12]HW */
    162 	{{ USB_VENDOR_HUAWEI, USB_PRODUCT_HUAWEI_E220 }, 2,	E220},
    163 	/* ANYDATA / NTT DoCoMo A2502 */
    164 	{{ USB_VENDOR_ANYDATA, USB_PRODUCT_ANYDATA_A2502 }, 3,	A2502},
    165 	/* HUAWEI E620 */
    166 	{{ USB_VENDOR_HUAWEI, USB_PRODUCT_HUAWEI_MOBILE }, 3,   E620},
    167 };
    168 #define uhmodem_lookup(v, p) ((const struct uhmodem_type *)usb_lookup(uhmodem_devs, v, p))
    169 
    170 int uhmodem_match(device_t, cfdata_t, void *);
    171 void uhmodem_attach(device_t, device_t, void *);
    172 void uhmodem_childdet(device_t, device_t);
    173 int uhmodem_detach(device_t, int);
    174 int uhmodem_activate(device_t, enum devact);
    175 extern struct cfdriver uhmodem_cd;
    176 CFATTACH_DECL2_NEW(uhmodem, sizeof(struct uhmodem_softc), uhmodem_match,
    177     uhmodem_attach, uhmodem_detach, uhmodem_activate, NULL, uhmodem_childdet);
    178 
    179 int
    180 uhmodem_match(device_t parent, cfdata_t match, void *aux)
    181 {
    182 	struct usbif_attach_arg *uaa = aux;
    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 void
    193 uhmodem_attach(device_t parent, device_t self, void *aux)
    194 {
    195 	struct uhmodem_softc *sc = device_private(self);
    196 	struct usbif_attach_arg *uaa = aux;
    197 	usbd_device_handle dev = uaa->device;
    198 	usb_config_descriptor_t *cdesc;
    199 	usb_interface_descriptor_t *id;
    200 	usb_endpoint_descriptor_t *ed;
    201 	char *devinfop;
    202 	usbd_status err;
    203 	struct ucom_attach_args uca;
    204 	int i;
    205 	int j;
    206 	char comname[16];
    207 
    208 	aprint_naive("\n");
    209 	aprint_normal("\n");
    210 
    211 	devinfop = usbd_devinfo_alloc(dev, 0);
    212 	aprint_normal_dev(self, "%s\n", devinfop);
    213 	usbd_devinfo_free(devinfop);
    214 
    215 	sc->sc_ubsa.sc_dev = self;
    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 			aprint_error_dev(self, "failed to change mode: %s\n",
    225 				usbd_errstr(err));
    226 			sc->sc_ubsa.sc_dying = 1;
    227 			goto error;
    228 		}
    229 		aprint_error_dev(self,
    230 		    "mass storage only mode, reattach to enable modem\n");
    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 		aprint_error_dev(self, "failed to set configuration: %s\n",
    253 		    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 		aprint_error_dev(self,
    262 		    "failed to get configuration descriptor\n");
    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 				aprint_error_dev(self,
    295 				    "no endpoint descriptor for %d "
    296 				    "(interface: %d)\n", j, i);
    297 				break;
    298 			}
    299 
    300 			if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    301 			    UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
    302 				sc->sc_ubsa.sc_intr_number = ed->bEndpointAddress;
    303 				sc->sc_ubsa.sc_isize = UGETW(ed->wMaxPacketSize);
    304 			} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    305 			    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
    306 				uca.bulkin = ed->bEndpointAddress;
    307 			} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
    308 			    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
    309 				uca.bulkout = ed->bEndpointAddress;
    310 			}
    311 		} /* end of Endpoint loop */
    312 
    313 		if (sc->sc_ubsa.sc_intr_number == -1) {
    314 			aprint_error_dev(self, "HUAWEI E220 need to re-attach "
    315 			    "to enable modem function\n");
    316 			if (i == 0) {
    317 				/* could not get intr for main tty */
    318 				sc->sc_ubsa.sc_dying = 1;
    319 				goto error;
    320 			} else
    321 				break;
    322 		}
    323 		if (uca.bulkin == -1) {
    324 			aprint_error_dev(self,
    325 			    "Could not find data bulk in\n");
    326 			sc->sc_ubsa.sc_dying = 1;
    327 			goto error;
    328 		}
    329 
    330 		if (uca.bulkout == -1) {
    331 			aprint_error_dev(self,
    332 			    "Could not find data bulk out\n");
    333 			sc->sc_ubsa.sc_dying = 1;
    334 			goto error;
    335 		}
    336 
    337 		switch (i) {
    338 		case 0:
    339 			snprintf(comname, sizeof(comname), "modem");
    340 			break;
    341 		case 1:
    342 			snprintf(comname, sizeof(comname), "alt#1");
    343 			break;
    344 		case 2:
    345 			snprintf(comname, sizeof(comname), "alt#2");
    346 			break;
    347 		default:
    348 			snprintf(comname, sizeof(comname), "int#%d", i);
    349 			break;
    350 		}
    351 
    352 		uca.portno = i;
    353 		/* bulkin, bulkout set above */
    354 		uca.ibufsize = UHMODEMIBUFSIZE;
    355 		uca.obufsize = UHMODEMOBUFSIZE;
    356 		uca.ibufsizepad = UHMODEMIBUFSIZE;
    357 		uca.opkthdrlen = 0;
    358 		uca.device = dev;
    359 		uca.iface = sc->sc_ubsa.sc_iface[i];
    360 		uca.methods = &uhmodem_methods;
    361 		uca.arg = &sc->sc_ubsa;
    362 		uca.info = comname;
    363 		DPRINTF(("uhmodem: int#=%d, in = 0x%x, out = 0x%x, intr = 0x%x\n",
    364 	    		i, uca.bulkin, uca.bulkout, sc->sc_ubsa.sc_intr_number));
    365 		sc->sc_ubsa.sc_subdevs[i] = config_found_sm_loc(self, "ucombus", NULL,
    366 				 &uca, ucomprint, ucomsubmatch);
    367 
    368 		/* issue endpoint halt to each interface */
    369 		err = uhmodem_endpointhalt(&sc->sc_ubsa, i);
    370 		if (err)
    371 			aprint_error("%s: endpointhalt fail\n", __func__);
    372 		else
    373 			usbd_delay_ms(sc->sc_ubsa.sc_udev, 50);
    374 	} /* end of Interface loop */
    375 
    376 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_ubsa.sc_udev,
    377 			   sc->sc_ubsa.sc_dev);
    378 
    379 	return;
    380 
    381 error:
    382 	return;
    383 }
    384 
    385 void
    386 uhmodem_childdet(device_t self, device_t child)
    387 {
    388 	int i;
    389 	struct uhmodem_softc *sc = device_private(self);
    390 
    391 	for (i = 0; i < sc->sc_ubsa.sc_numif; i++) {
    392 		if (sc->sc_ubsa.sc_subdevs[i] == child)
    393 			break;
    394 	}
    395 	KASSERT(i < sc->sc_ubsa.sc_numif);
    396 	sc->sc_ubsa.sc_subdevs[i] = NULL;
    397 }
    398 
    399 int
    400 uhmodem_detach(device_t self, int flags)
    401 {
    402 	struct uhmodem_softc *sc = device_private(self);
    403 	int i;
    404 	int rv = 0;
    405 
    406 	DPRINTF(("uhmodem_detach: sc = %p\n", sc));
    407 
    408 	if (sc->sc_ubsa.sc_intr_pipe != NULL) {
    409 		usbd_abort_pipe(sc->sc_ubsa.sc_intr_pipe);
    410 		usbd_close_pipe(sc->sc_ubsa.sc_intr_pipe);
    411 		free(sc->sc_ubsa.sc_intr_buf, M_USBDEV);
    412 		sc->sc_ubsa.sc_intr_pipe = NULL;
    413 	}
    414 
    415 	sc->sc_ubsa.sc_dying = 1;
    416 	for (i = 0; i < sc->sc_ubsa.sc_numif; i++) {
    417 		if (sc->sc_ubsa.sc_subdevs[i] != NULL)
    418 			rv |= config_detach(sc->sc_ubsa.sc_subdevs[i], flags);
    419 	}
    420 
    421 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_ubsa.sc_udev,
    422 			   sc->sc_ubsa.sc_dev);
    423 
    424 	return (rv);
    425 }
    426 
    427 int
    428 uhmodem_activate(device_t self, enum devact act)
    429 {
    430 	struct uhmodem_softc *sc = device_private(self);
    431 
    432 	switch (act) {
    433 	case DVACT_DEACTIVATE:
    434 		sc->sc_ubsa.sc_dying = 1;
    435 		return 0;
    436 	default:
    437 		return EOPNOTSUPP;
    438 	}
    439 }
    440 
    441 Static int
    442 uhmodem_open(void *addr, int portno)
    443 {
    444 	struct ubsa_softc *sc = addr;
    445 	usbd_status err;
    446 
    447 	if (sc->sc_dying)
    448 		return (ENXIO);
    449 
    450 	DPRINTF(("%s: sc = %p\n", __func__, sc));
    451 
    452 	err = uhmodem_endpointhalt(sc, 0);
    453 	if (err)
    454 		aprint_error("%s: endpointhalt fail\n", __func__);
    455 	else
    456 		usbd_delay_ms(sc->sc_udev, 50);
    457 
    458 	if (sc->sc_devflags & A2502) {
    459 		err = a2502_init(sc->sc_udev);
    460 		if (err)
    461 			aprint_error("%s: a2502init fail\n", __func__);
    462 		else
    463 			usbd_delay_ms(sc->sc_udev, 50);
    464 	}
    465 #if 0 /* currently disabled */
    466 	if (sc->sc_devflags & E220) {
    467 		err = e220_init(sc->sc_udev);
    468 		if (err)
    469 			aprint_error("%s: e220init fail\n", __func__);
    470 		else
    471 			usbd_delay_ms(sc->sc_udev, 50);
    472 	}
    473 #endif
    474 	if (sc->sc_intr_number != -1 && sc->sc_intr_pipe == NULL) {
    475 		sc->sc_intr_buf = malloc(sc->sc_isize, M_USBDEV, M_WAITOK);
    476 		/* XXX only iface# = 0 has intr line */
    477 		/* XXX E220 specific? need to check */
    478 		err = usbd_open_pipe_intr(sc->sc_iface[0],
    479 		    sc->sc_intr_number,
    480 		    USBD_SHORT_XFER_OK,
    481 		    &sc->sc_intr_pipe,
    482 		    sc,
    483 		    sc->sc_intr_buf,
    484 		    sc->sc_isize,
    485 		    ubsa_intr,
    486 		    UBSA_INTR_INTERVAL);
    487 		if (err) {
    488 			aprint_error_dev(sc->sc_dev,
    489 			    "cannot open interrupt pipe (addr %d)\n",
    490 			    sc->sc_intr_number);
    491 			return (EIO);
    492 		}
    493 	}
    494 
    495 	return (0);
    496 }
    497 
    498 /*
    499  * Hauwei E220 needs special request to enable modem function.
    500  * -- DEVICE_REMOTE_WAKEUP ruquest to endpoint 2.
    501  */
    502 Static  usbd_status
    503 e220_modechange_request(usbd_device_handle dev)
    504 {
    505 #define E220_MODE_CHANGE_REQUEST 0x2
    506 	usb_device_request_t req;
    507 	usbd_status err;
    508 
    509 	req.bmRequestType = UT_WRITE_DEVICE;
    510 	req.bRequest = UR_SET_FEATURE;
    511 	USETW(req.wValue, UF_DEVICE_REMOTE_WAKEUP);
    512 	USETW(req.wIndex, E220_MODE_CHANGE_REQUEST);
    513 	USETW(req.wLength, 0);
    514 
    515 	DPRINTF(("%s: send e220 mode change request\n", __func__));
    516 	err = usbd_do_request(dev, &req, 0);
    517 	if (err) {
    518 		DPRINTF(("%s: E220 mode change fail\n", __func__));
    519 		return (EIO);
    520 	}
    521 
    522 	return (0);
    523 #undef E220_MODE_CHANGE_REQUEST
    524 }
    525 
    526 Static  usbd_status
    527 uhmodem_endpointhalt(struct ubsa_softc *sc, int iface)
    528 {
    529 	usb_device_request_t req;
    530 	usb_endpoint_descriptor_t *ed;
    531 	usb_interface_descriptor_t *id;
    532 	usbd_status err;
    533 	int i;
    534 
    535 	/* Find the endpoints */
    536 	id = usbd_get_interface_descriptor(sc->sc_iface[iface]);
    537 
    538 	for (i = 0; i < id->bNumEndpoints; i++) {
    539 		ed = usbd_interface2endpoint_descriptor(sc->sc_iface[iface], i);
    540 		if (ed == NULL)
    541 			return (EIO);
    542 
    543 		if (UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
    544 			/* issue ENDPOINT_HALT request */
    545 			req.bmRequestType = UT_WRITE_ENDPOINT;
    546 			req.bRequest = UR_CLEAR_FEATURE;
    547 			USETW(req.wValue, UF_ENDPOINT_HALT);
    548 			USETW(req.wIndex, ed->bEndpointAddress);
    549 			USETW(req.wLength, 0);
    550 			err = usbd_do_request(sc->sc_udev, &req, 0);
    551 			if (err) {
    552 				DPRINTF(("%s: ENDPOINT_HALT to EP:%d fail\n",
    553 					__func__, ed->bEndpointAddress));
    554 				return (EIO);
    555 			}
    556 
    557 		}
    558 	} /* end of Endpoint loop */
    559 
    560 	return (0);
    561 }
    562 
    563 Static usbd_status
    564 uhmodem_regwrite(usbd_device_handle dev, uint8_t *data, size_t len)
    565 {
    566 	usb_device_request_t req;
    567 	usbd_status err;
    568 
    569 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
    570 	req.bRequest = UHMODEM_REGWRITE;
    571 	USETW(req.wValue, 0x0000);
    572 	USETW(req.wIndex, 0x0000);
    573 	USETW(req.wLength, len);
    574 	err = usbd_do_request(dev, &req, data);
    575 	if (err)
    576 		return err;
    577 
    578 	return 0;
    579 }
    580 
    581 Static usbd_status
    582 uhmodem_regread(usbd_device_handle dev, uint8_t *data, size_t len)
    583 {
    584 	usb_device_request_t req;
    585 	usbd_status err;
    586 
    587 	req.bmRequestType = UT_READ_CLASS_INTERFACE;
    588 	req.bRequest = UHMODEM_REGREAD;
    589 	USETW(req.wValue, 0x0000);
    590 	USETW(req.wIndex, 0x0000);
    591 	USETW(req.wLength, len);
    592 	err = usbd_do_request(dev, &req, data);
    593 
    594 	if (err)
    595 		return err;
    596 
    597 	return 0;
    598 }
    599 
    600 #if 0
    601 Static usbd_status
    602 uhmodem_regsetup(usbd_device_handle dev, uint16_t cmd)
    603 {
    604 	usb_device_request_t req;
    605 	usbd_status err;
    606 
    607 	req.bmRequestType = UT_READ_CLASS_INTERFACE;
    608 	req.bRequest = UHMODEM_SETUP;
    609 	USETW(req.wValue, cmd);
    610 	USETW(req.wIndex, 0x0000);
    611 	USETW(req.wLength, 0);
    612 	err = usbd_do_request(dev, &req, 0);
    613 
    614 	if (err)
    615 		return err;
    616 
    617 	return 0;
    618 }
    619 #endif
    620 
    621 Static  usbd_status
    622 a2502_init(usbd_device_handle dev)
    623 {
    624 	uint8_t data[8];
    625 	static uint8_t init_cmd[] = {0x00, 0xE1, 0x00, 0x00, 0x00, 0x00, 0x08};
    626 #ifdef UHMODEM_DEBUG
    627 	int i;
    628 #endif
    629 	if (uhmodem_regread(dev, data, 7)) {
    630 		DPRINTF(("%s: read fail\n", __func__));
    631 		return EIO;
    632 	}
    633 #ifdef UHMODEM_DEBUG
    634 	printf("%s: readdata: ", __func__);
    635 	for (i = 0; i < 7; i++)
    636 		printf("0x%x ", data[i]);
    637 #endif
    638 	if (uhmodem_regwrite(dev, init_cmd, sizeof(init_cmd)) ) {
    639 		DPRINTF(("%s: write fail\n", __func__));
    640 		return EIO;
    641 	}
    642 
    643 	if (uhmodem_regread(dev, data, 7)) {
    644 		DPRINTF(("%s: read fail\n", __func__));
    645 		return EIO;
    646 	}
    647 #ifdef UHMODEM_DEBUG
    648 	printf("%s: readdata: ", __func__);
    649 	printf(" => ");
    650 	for (i = 0; i < 7; i++)
    651 		printf("0x%x ", data[i]);
    652 	printf("\n");
    653 #endif
    654 	return 0;
    655 }
    656 
    657 
    658 #if 0
    659 /*
    660  * Windows device driver send these sequens of USB requests.
    661  * However currently I can't understand what the messege is,
    662  * disable this code when I get more information about it.
    663  */
    664 Static  usbd_status
    665 e220_init(usbd_device_handle dev)
    666 {
    667 	uint8_t data[8];
    668 	usb_device_request_t req;
    669 	int i;
    670 
    671 	/* vendor specific unknown request */
    672 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
    673 	req.bRequest = 0x02;
    674 	USETW(req.wValue, 0x0001);
    675 	USETW(req.wIndex, 0x0000);
    676 	USETW(req.wLength, 2);
    677 	data[0] = 0x0;
    678 	data[1] = 0x0;
    679 	if (usbd_do_request(dev, &req, data))
    680 		goto error;
    681 
    682 	/* vendor specific unknown sequence */
    683 	if(uhmodem_regsetup(dev, 0x1))
    684 		goto error;
    685 
    686 	if (uhmodem_regread(dev, data, 7))
    687 		goto error;
    688 
    689 	data[1] = 0x8;
    690 	data[2] = 0x7;
    691 	if (uhmodem_regwrite(dev, data, sizeof(data)) )
    692 		goto error;
    693 
    694 	if (uhmodem_regread(dev, data, 7))
    695 		goto error;
    696 		/* XXX should verify the read data ? */
    697 
    698 	if (uhmodem_regsetup(dev, 0x3))
    699 		goto error;
    700 
    701 	return (0);
    702 error:
    703 	DPRINTF(("%s: E220 init request fail\n", __func__));
    704 	return (EIO);
    705 }
    706 #endif
    707 
    708