Home | History | Annotate | Line # | Download | only in usb
uhmodem.c revision 1.9.4.1
      1 /*	$NetBSD: uhmodem.c,v 1.9.4.1 2011/03/05 20:54:14 rmind 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.9.4.1 2011/03/05 20:54:14 rmind 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/usbcdc.h>
     98 #include <dev/usb/usbdevs.h>
     99 #include <dev/usb/usb_quirks.h>
    100 #include <dev/usb/ucomvar.h>
    101 #include <dev/usb/ubsavar.h>
    102 
    103 /* vendor specific bRequest */
    104 #define	UHMODEM_REGWRITE	0x20
    105 #define	UHMODEM_REGREAD		0x21
    106 #define UHMODEM_SETUP		0x22
    107 
    108 #define UHMODEMIBUFSIZE	4096
    109 #define UHMODEMOBUFSIZE	4096
    110 
    111 #ifdef UHMODEM_DEBUG
    112 Static int	uhmodemdebug = 0;
    113 #define DPRINTFN(n, x)  do { \
    114 				if (uhmodemdebug > (n)) \
    115 					printf x; \
    116 			} while (0)
    117 #else
    118 #define DPRINTFN(n, x)
    119 #endif
    120 #define DPRINTF(x) DPRINTFN(0, x)
    121 
    122 Static int uhmodem_open(void *, int);
    123 Static  usbd_status e220_modechange_request(usbd_device_handle);
    124 Static	usbd_status uhmodem_endpointhalt(struct ubsa_softc *, int);
    125 Static	usbd_status uhmodem_regwrite(usbd_device_handle, uint8_t *, size_t);
    126 Static	usbd_status uhmodem_regread(usbd_device_handle, uint8_t *, size_t);
    127 Static  usbd_status a2502_init(usbd_device_handle);
    128 #if 0
    129 Static	usbd_status uhmodem_regsetup(usbd_device_handle, uint16_t);
    130 Static  usbd_status e220_init(usbd_device_handle);
    131 #endif
    132 
    133 struct	uhmodem_softc {
    134 	struct ubsa_softc	sc_ubsa;
    135 };
    136 
    137 struct	ucom_methods uhmodem_methods = {
    138 	ubsa_get_status,
    139 	ubsa_set,
    140 	ubsa_param,
    141 	NULL,
    142 	uhmodem_open,
    143 	ubsa_close,
    144 	NULL,
    145 	NULL
    146 };
    147 
    148 struct uhmodem_type {
    149 	struct usb_devno	uhmodem_dev;
    150 	u_int16_t		uhmodem_coms;	/* number of serial interfaces on the device */
    151 	u_int16_t		uhmodem_flags;
    152 #define	E220	0x0001
    153 #define	A2502	0x0002
    154 #define	E620	0x0004		/* XXX */
    155 				/* Whether or not it is a device different from E220 is not clear. */
    156 };
    157 
    158 Static const struct uhmodem_type uhmodem_devs[] = {
    159 	/* HUAWEI E220 / Emobile D0[12]HW */
    160 	{{ USB_VENDOR_HUAWEI, USB_PRODUCT_HUAWEI_E220 }, 2,	E220},
    161 	/* ANYDATA / NTT DoCoMo A2502 */
    162 	{{ USB_VENDOR_ANYDATA, USB_PRODUCT_ANYDATA_A2502 }, 3,	A2502},
    163 	/* HUAWEI E620 */
    164 	{{ USB_VENDOR_HUAWEI, USB_PRODUCT_HUAWEI_MOBILE }, 3,   E620},
    165 };
    166 #define uhmodem_lookup(v, p) ((const struct uhmodem_type *)usb_lookup(uhmodem_devs, v, p))
    167 
    168 int uhmodem_match(device_t, cfdata_t, void *);
    169 void uhmodem_attach(device_t, device_t, void *);
    170 void uhmodem_childdet(device_t, device_t);
    171 int uhmodem_detach(device_t, int);
    172 int uhmodem_activate(device_t, enum devact);
    173 extern struct cfdriver uhmodem_cd;
    174 CFATTACH_DECL2_NEW(uhmodem, sizeof(struct uhmodem_softc), uhmodem_match,
    175     uhmodem_attach, uhmodem_detach, uhmodem_activate, NULL, uhmodem_childdet);
    176 
    177 int
    178 uhmodem_match(device_t parent, cfdata_t match, void *aux)
    179 {
    180 	struct usbif_attach_arg *uaa = aux;
    181 
    182 	if (uhmodem_lookup(uaa->vendor, uaa->product) != NULL)
    183 		/* XXX interface# 0,1 provide modem function, but this driver
    184 		   handles all modem in single device.  */
    185 		if (uaa->ifaceno == 0)
    186 			return (UMATCH_VENDOR_PRODUCT);
    187 	return (UMATCH_NONE);
    188 }
    189 
    190 void
    191 uhmodem_attach(device_t parent, device_t self, void *aux)
    192 {
    193 	struct uhmodem_softc *sc = device_private(self);
    194 	struct usbif_attach_arg *uaa = aux;
    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 	usbd_status err;
    201 	struct ucom_attach_args uca;
    202 	int i;
    203 	int j;
    204 	char comname[16];
    205 
    206 	aprint_naive("\n");
    207 	aprint_normal("\n");
    208 
    209 	devinfop = usbd_devinfo_alloc(dev, 0);
    210 	aprint_normal_dev(self, "%s\n", devinfop);
    211 	usbd_devinfo_free(devinfop);
    212 
    213 	sc->sc_ubsa.sc_dev = self;
    214         sc->sc_ubsa.sc_udev = dev;
    215 	sc->sc_ubsa.sc_config_index = UBSA_DEFAULT_CONFIG_INDEX;
    216 	sc->sc_ubsa.sc_numif = 1; /* defaut device has one interface */
    217 
    218 	/* Hauwei E220 need special request to change its mode to modem */
    219 	if ((uaa->ifaceno == 0) && (uaa->class != 255)) {
    220 		err = e220_modechange_request(dev);
    221 		if (err) {
    222 			aprint_error_dev(self, "failed to change mode: %s\n",
    223 				usbd_errstr(err));
    224 			sc->sc_ubsa.sc_dying = 1;
    225 			goto error;
    226 		}
    227 		aprint_error_dev(self,
    228 		    "mass storage only mode, reattach to enable modem\n");
    229 		sc->sc_ubsa.sc_dying = 1;
    230 		goto error;
    231 	}
    232 
    233 	/*
    234 	 * initialize rts, dtr variables to something
    235 	 * different from boolean 0, 1
    236 	 */
    237 	sc->sc_ubsa.sc_dtr = -1;
    238 	sc->sc_ubsa.sc_rts = -1;
    239 
    240 	sc->sc_ubsa.sc_quadumts = 1;
    241 	sc->sc_ubsa.sc_config_index = 0;
    242 	sc->sc_ubsa.sc_numif = uhmodem_lookup(uaa->vendor, uaa->product)->uhmodem_coms;
    243 	sc->sc_ubsa.sc_devflags = uhmodem_lookup(uaa->vendor, uaa->product)->uhmodem_flags;
    244 
    245 	DPRINTF(("uhmodem attach: sc = %p\n", sc));
    246 
    247 	/* Move the device into the configured state. */
    248 	err = usbd_set_config_index(dev, sc->sc_ubsa.sc_config_index, 1);
    249 	if (err) {
    250 		aprint_error_dev(self, "failed to set configuration: %s\n",
    251 		    usbd_errstr(err));
    252 		sc->sc_ubsa.sc_dying = 1;
    253 		goto error;
    254 	}
    255 
    256 	/* get the config descriptor */
    257 	cdesc = usbd_get_config_descriptor(sc->sc_ubsa.sc_udev);
    258 	if (cdesc == NULL) {
    259 		aprint_error_dev(self,
    260 		    "failed to get configuration descriptor\n");
    261 		sc->sc_ubsa.sc_dying = 1;
    262 		goto error;
    263 	}
    264 
    265 	sc->sc_ubsa.sc_intr_number = -1;
    266 	sc->sc_ubsa.sc_intr_pipe = NULL;
    267 
    268 	/* get the interfaces */
    269 	for (i = 0; i < sc->sc_ubsa.sc_numif; i++) {
    270 		err = usbd_device2interface_handle(dev, UBSA_IFACE_INDEX_OFFSET+i,
    271 				 &sc->sc_ubsa.sc_iface[i]);
    272 		if (err) {
    273 			if (i == 0){
    274 				/* can not get main interface */
    275 				sc->sc_ubsa.sc_dying = 1;
    276 				goto error;
    277 			} else
    278 				break;
    279 		}
    280 
    281 		/* Find the endpoints */
    282 		id = usbd_get_interface_descriptor(sc->sc_ubsa.sc_iface[i]);
    283 		sc->sc_ubsa.sc_iface_number[i] = id->bInterfaceNumber;
    284 
    285 		/* initialize endpoints */
    286 		uca.bulkin = uca.bulkout = -1;
    287 
    288 		for (j = 0; j < id->bNumEndpoints; j++) {
    289 			ed = usbd_interface2endpoint_descriptor(
    290 				sc->sc_ubsa.sc_iface[i], j);
    291 			if (ed == NULL) {
    292 				aprint_error_dev(self,
    293 				    "no endpoint descriptor for %d "
    294 				    "(interface: %d)\n", j, i);
    295 				break;
    296 			}
    297 
    298 			if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    299 			    UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
    300 				sc->sc_ubsa.sc_intr_number = ed->bEndpointAddress;
    301 				sc->sc_ubsa.sc_isize = UGETW(ed->wMaxPacketSize);
    302 			} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    303 			    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
    304 				uca.bulkin = ed->bEndpointAddress;
    305 			} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
    306 			    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
    307 				uca.bulkout = ed->bEndpointAddress;
    308 			}
    309 		} /* end of Endpoint loop */
    310 
    311 		if (sc->sc_ubsa.sc_intr_number == -1) {
    312 			aprint_error_dev(self, "HUAWEI E220 need to re-attach "
    313 			    "to enable modem function\n");
    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 			aprint_error_dev(self,
    323 			    "Could not find data bulk in\n");
    324 			sc->sc_ubsa.sc_dying = 1;
    325 			goto error;
    326 		}
    327 
    328 		if (uca.bulkout == -1) {
    329 			aprint_error_dev(self,
    330 			    "Could not find data bulk out\n");
    331 			sc->sc_ubsa.sc_dying = 1;
    332 			goto error;
    333 		}
    334 
    335 		switch (i) {
    336 		case 0:
    337 			snprintf(comname, sizeof(comname), "modem");
    338 			break;
    339 		case 1:
    340 			snprintf(comname, sizeof(comname), "alt#1");
    341 			break;
    342 		case 2:
    343 			snprintf(comname, sizeof(comname), "alt#2");
    344 			break;
    345 		default:
    346 			snprintf(comname, sizeof(comname), "int#%d", i);
    347 			break;
    348 		}
    349 
    350 		uca.portno = i;
    351 		/* bulkin, bulkout set above */
    352 		uca.ibufsize = UHMODEMIBUFSIZE;
    353 		uca.obufsize = UHMODEMOBUFSIZE;
    354 		uca.ibufsizepad = UHMODEMIBUFSIZE;
    355 		uca.opkthdrlen = 0;
    356 		uca.device = dev;
    357 		uca.iface = sc->sc_ubsa.sc_iface[i];
    358 		uca.methods = &uhmodem_methods;
    359 		uca.arg = &sc->sc_ubsa;
    360 		uca.info = comname;
    361 		DPRINTF(("uhmodem: int#=%d, in = 0x%x, out = 0x%x, intr = 0x%x\n",
    362 	    		i, uca.bulkin, uca.bulkout, sc->sc_ubsa.sc_intr_number));
    363 		sc->sc_ubsa.sc_subdevs[i] = config_found_sm_loc(self, "ucombus", NULL,
    364 				 &uca, ucomprint, ucomsubmatch);
    365 
    366 		/* issue endpoint halt to each interface */
    367 		err = uhmodem_endpointhalt(&sc->sc_ubsa, i);
    368 		if (err)
    369 			aprint_error("%s: endpointhalt fail\n", __func__);
    370 		else
    371 			usbd_delay_ms(sc->sc_ubsa.sc_udev, 50);
    372 	} /* end of Interface loop */
    373 
    374 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_ubsa.sc_udev,
    375 			   sc->sc_ubsa.sc_dev);
    376 
    377 	return;
    378 
    379 error:
    380 	return;
    381 }
    382 
    383 void
    384 uhmodem_childdet(device_t self, device_t child)
    385 {
    386 	int i;
    387 	struct uhmodem_softc *sc = device_private(self);
    388 
    389 	for (i = 0; i < sc->sc_ubsa.sc_numif; i++) {
    390 		if (sc->sc_ubsa.sc_subdevs[i] == child)
    391 			break;
    392 	}
    393 	KASSERT(i < sc->sc_ubsa.sc_numif);
    394 	sc->sc_ubsa.sc_subdevs[i] = NULL;
    395 }
    396 
    397 int
    398 uhmodem_detach(device_t self, int flags)
    399 {
    400 	struct uhmodem_softc *sc = device_private(self);
    401 	int i;
    402 	int rv = 0;
    403 
    404 	DPRINTF(("uhmodem_detach: sc = %p\n", sc));
    405 
    406 	if (sc->sc_ubsa.sc_intr_pipe != NULL) {
    407 		usbd_abort_pipe(sc->sc_ubsa.sc_intr_pipe);
    408 		usbd_close_pipe(sc->sc_ubsa.sc_intr_pipe);
    409 		free(sc->sc_ubsa.sc_intr_buf, M_USBDEV);
    410 		sc->sc_ubsa.sc_intr_pipe = NULL;
    411 	}
    412 
    413 	sc->sc_ubsa.sc_dying = 1;
    414 	for (i = 0; i < sc->sc_ubsa.sc_numif; i++) {
    415 		if (sc->sc_ubsa.sc_subdevs[i] != NULL)
    416 			rv |= config_detach(sc->sc_ubsa.sc_subdevs[i], flags);
    417 	}
    418 
    419 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_ubsa.sc_udev,
    420 			   sc->sc_ubsa.sc_dev);
    421 
    422 	return (rv);
    423 }
    424 
    425 int
    426 uhmodem_activate(device_t self, enum devact act)
    427 {
    428 	struct uhmodem_softc *sc = device_private(self);
    429 
    430 	switch (act) {
    431 	case DVACT_DEACTIVATE:
    432 		sc->sc_ubsa.sc_dying = 1;
    433 		return 0;
    434 	default:
    435 		return EOPNOTSUPP;
    436 	}
    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 		aprint_error("%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 			aprint_error("%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 			aprint_error("%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 			aprint_error_dev(sc->sc_dev,
    487 			    "cannot open interrupt pipe (addr %d)\n",
    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