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