Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
/*	$NetBSD: uhmodem.c,v 1.20 2019/05/27 03:08:13 maya Exp $	*/

/*
 * Copyright (c) 2008 Yojiro UO <yuo@nui.org>.
 * All rights reserved.
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER IN
 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */
/*-
 * Copyright (c) 2002, Alexander Kabaev <kan.FreeBSD.org>.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */
/*
 * Copyright (c) 2001 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by Ichiro FUKUHARA (ichiro@ichiro.org).
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: uhmodem.c,v 1.20 2019/05/27 03:08:13 maya Exp $");

#ifdef _KERNEL_OPT
#include "opt_usb.h"
#endif

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/kmem.h>
#include <sys/ioccom.h>
#include <sys/fcntl.h>
#include <sys/conf.h>
#include <sys/tty.h>
#include <sys/file.h>
#include <sys/select.h>
#include <sys/proc.h>
#include <sys/device.h>
#include <sys/poll.h>
#include <sys/sysctl.h>
#include <sys/bus.h>

#include <dev/usb/usb.h>
#include <dev/usb/usbdi.h>
#include <dev/usb/usbdi_util.h>
#include <dev/usb/usbdivar.h>

#include <dev/usb/usbcdc.h>
#include <dev/usb/usbdevs.h>
#include <dev/usb/usb_quirks.h>
#include <dev/usb/ucomvar.h>
#include <dev/usb/ubsavar.h>

/* vendor specific bRequest */
#define	UHMODEM_REGWRITE	0x20
#define	UHMODEM_REGREAD		0x21
#define UHMODEM_SETUP		0x22

#define UHMODEMIBUFSIZE	4096
#define UHMODEMOBUFSIZE	4096

#ifdef UHMODEM_DEBUG
static int	uhmodemdebug = 0;
#define DPRINTFN(n, x)  do { \
				if (uhmodemdebug > (n)) \
					printf x; \
			} while (0)
#else
#define DPRINTFN(n, x)
#endif
#define DPRINTF(x) DPRINTFN(0, x)

static int	uhmodem_open(void *, int);
static usbd_status e220_modechange_request(struct usbd_device *);
static usbd_status uhmodem_endpointhalt(struct ubsa_softc *, int);
static usbd_status uhmodem_regwrite(struct usbd_device *, uint8_t *, size_t);
static usbd_status uhmodem_regread(struct usbd_device *, uint8_t *, size_t);
static usbd_status a2502_init(struct usbd_device *);
#if 0
static usbd_status uhmodem_regsetup(struct usbd_device *, uint16_t);
static usbd_status e220_init(struct usbd_device *);
#endif

/* this driver uses the ubsa_softc and methods directly as-is. */
struct	ucom_methods uhmodem_methods = {
	.ucom_get_status = ubsa_get_status,
	.ucom_set = ubsa_set,
	.ucom_param = ubsa_param,
	.ucom_open = uhmodem_open,
	.ucom_close = ubsa_close,
};

struct uhmodem_type {
	struct usb_devno	uhmodem_dev;
	uint16_t		uhmodem_coms;	/* number of serial interfaces on the device */
	uint16_t		uhmodem_flags;
#define	E220	0x0001
#define	A2502	0x0002
#define	E620	0x0004		/* XXX */
				/* Whether or not it is a device different from E220 is not clear. */
};

static const struct uhmodem_type uhmodem_devs[] = {
	/* HUAWEI E220 / Emobile D0[12]HW */
	{{ USB_VENDOR_HUAWEI, USB_PRODUCT_HUAWEI_E220 }, 2,	E220},
	/* ANYDATA / NTT DoCoMo A2502 */
	{{ USB_VENDOR_ANYDATA, USB_PRODUCT_ANYDATA_A2502 }, 3,	A2502},
	/* HUAWEI E620 */
	{{ USB_VENDOR_HUAWEI, USB_PRODUCT_HUAWEI_MOBILE }, 3,   E620},
};
#define uhmodem_lookup(v, p) ((const struct uhmodem_type *)usb_lookup(uhmodem_devs, v, p))

static int	uhmodem_match(device_t, cfdata_t, void *);
static void	uhmodem_attach(device_t, device_t, void *);
static void	uhmodem_childdet(device_t, device_t);
static int	uhmodem_detach(device_t, int);

CFATTACH_DECL2_NEW(uhmodem, sizeof(struct ubsa_softc), uhmodem_match,
    uhmodem_attach, uhmodem_detach, NULL, NULL, uhmodem_childdet);

static int
uhmodem_match(device_t parent, cfdata_t match, void *aux)
{
	struct usbif_attach_arg *uiaa = aux;

	if (uhmodem_lookup(uiaa->uiaa_vendor, uiaa->uiaa_product) != NULL)
		/* XXX interface# 0,1 provide modem function, but this driver
		   handles all modem in single device.  */
		if (uiaa->uiaa_ifaceno == 0)
			return UMATCH_VENDOR_PRODUCT;
	return UMATCH_NONE;
}

static void
uhmodem_attach(device_t parent, device_t self, void *aux)
{
	struct ubsa_softc *sc = device_private(self);
	struct usbif_attach_arg *uiaa = aux;
	struct usbd_device *dev = uiaa->uiaa_device;
	usb_config_descriptor_t *cdesc;
	usb_interface_descriptor_t *id;
	usb_endpoint_descriptor_t *ed;
	char *devinfop;
	usbd_status err;
	struct ucom_attach_args ucaa;
	int i;
	int j;
	char comname[16];

	aprint_naive("\n");
	aprint_normal("\n");

	devinfop = usbd_devinfo_alloc(dev, 0);
	aprint_normal_dev(self, "%s\n", devinfop);
	usbd_devinfo_free(devinfop);

	sc->sc_dev = self;
	sc->sc_udev = dev;
	sc->sc_config_index = UBSA_DEFAULT_CONFIG_INDEX;
	sc->sc_numif = 1; /* default device has one interface */
	sc->sc_dying = false;

	/* Hauwei E220 need special request to change its mode to modem */
	if ((uiaa->uiaa_ifaceno == 0) && (uiaa->uiaa_class != 255)) {
		err = e220_modechange_request(dev);
		if (err) {
			aprint_error_dev(self, "failed to change mode: %s\n",
				usbd_errstr(err));
			goto error;
		}
		aprint_error_dev(self,
		    "mass storage only mode, reattach to enable modem\n");
		goto error;
	}

	/*
	 * initialize rts, dtr variables to something
	 * different from boolean 0, 1
	 */
	sc->sc_dtr = -1;
	sc->sc_rts = -1;

	sc->sc_quadumts = 1;
	sc->sc_config_index = 0;
	sc->sc_numif = uhmodem_lookup(uiaa->uiaa_vendor, uiaa->uiaa_product)->uhmodem_coms;
	sc->sc_devflags = uhmodem_lookup(uiaa->uiaa_vendor, uiaa->uiaa_product)->uhmodem_flags;

	DPRINTF(("uhmodem attach: sc = %p\n", sc));

	/* Move the device into the configured state. */
	err = usbd_set_config_index(dev, sc->sc_config_index, 1);
	if (err) {
		aprint_error_dev(self, "failed to set configuration: %s\n",
		    usbd_errstr(err));
		goto error;
	}

	/* get the config descriptor */
	cdesc = usbd_get_config_descriptor(sc->sc_udev);
	if (cdesc == NULL) {
		aprint_error_dev(self,
		    "failed to get configuration descriptor\n");
		goto error;
	}

	sc->sc_intr_number = -1;
	sc->sc_intr_pipe = NULL;

	/* get the interfaces */
	for (i = 0; i < sc->sc_numif; i++) {
		err = usbd_device2interface_handle(dev, UBSA_IFACE_INDEX_OFFSET+i,
				 &sc->sc_iface[i]);
		if (err) {
			if (i == 0){
				/* can not get main interface */
				goto error;
			} else
				break;
		}

		/* Find the endpoints */
		id = usbd_get_interface_descriptor(sc->sc_iface[i]);
		sc->sc_iface_number[i] = id->bInterfaceNumber;

		/* initialize endpoints */
		ucaa.ucaa_bulkin = ucaa.ucaa_bulkout = -1;

		for (j = 0; j < id->bNumEndpoints; j++) {
			ed = usbd_interface2endpoint_descriptor(
				sc->sc_iface[i], j);
			if (ed == NULL) {
				aprint_error_dev(self,
				    "no endpoint descriptor for %d "
				    "(interface: %d)\n", j, i);
				break;
			}

			if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
			    UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
				sc->sc_intr_number = ed->bEndpointAddress;
				sc->sc_isize = UGETW(ed->wMaxPacketSize);
			} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
			    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
				ucaa.ucaa_bulkin = ed->bEndpointAddress;
			} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
			    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
				ucaa.ucaa_bulkout = ed->bEndpointAddress;
			}
		} /* end of Endpoint loop */

		if (sc->sc_intr_number == -1) {
			aprint_error_dev(self, "HUAWEI E220 need to re-attach "
			    "to enable modem function\n");
			if (i == 0) {
				/* could not get intr for main tty */
				goto error;
			} else
				break;
		}
		if (ucaa.ucaa_bulkin == -1) {
			aprint_error_dev(self,
			    "Could not find data bulk in\n");
			goto error;
		}

		if (ucaa.ucaa_bulkout == -1) {
			aprint_error_dev(self,
			    "Could not find data bulk out\n");
			goto error;
		}

		switch (i) {
		case 0:
			snprintf(comname, sizeof(comname), "modem");
			break;
		case 1:
			snprintf(comname, sizeof(comname), "alt#1");
			break;
		case 2:
			snprintf(comname, sizeof(comname), "alt#2");
			break;
		default:
			snprintf(comname, sizeof(comname), "int#%d", i);
			break;
		}

		ucaa.ucaa_portno = i;
		/* ucaa_bulkin, ucaa_bulkout set above */
		ucaa.ucaa_ibufsize = UHMODEMIBUFSIZE;
		ucaa.ucaa_obufsize = UHMODEMOBUFSIZE;
		ucaa.ucaa_ibufsizepad = UHMODEMIBUFSIZE;
		ucaa.ucaa_opkthdrlen = 0;
		ucaa.ucaa_device = dev;
		ucaa.ucaa_iface = sc->sc_iface[i];
		ucaa.ucaa_methods = &uhmodem_methods;
		ucaa.ucaa_arg = sc;
		ucaa.ucaa_info = comname;
		DPRINTF(("uhmodem: int#=%d, in = 0x%x, out = 0x%x, intr = 0x%x\n",
		    i, ucaa.ucaa_bulkin, ucaa.ucaa_bulkout,
		    sc->sc_intr_number));
		sc->sc_subdevs[i] = config_found_sm_loc(self, "ucombus", NULL,
				 &ucaa, ucomprint, ucomsubmatch);

		/* issue endpoint halt to each interface */
		err = uhmodem_endpointhalt(sc, i);
		if (err)
			aprint_error("%s: endpointhalt fail\n", __func__);
		else
			usbd_delay_ms(sc->sc_udev, 50);
	} /* end of Interface loop */

	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
			   sc->sc_dev);

	if (!pmf_device_register(self, NULL, NULL))
		aprint_error_dev(self, "couldn't establish power handler\n");

	return;

error:
	sc->sc_dying = true;
	return;
}

static void
uhmodem_childdet(device_t self, device_t child)
{
	int i;
	struct ubsa_softc *sc = device_private(self);

	for (i = 0; i < sc->sc_numif; i++) {
		if (sc->sc_subdevs[i] == child)
			break;
	}
	KASSERT(i < sc->sc_numif);
	sc->sc_subdevs[i] = NULL;
}

static int
uhmodem_detach(device_t self, int flags)
{
	struct ubsa_softc *sc = device_private(self);
	int i;
	int rv = 0;

	DPRINTF(("uhmodem_detach: sc = %p\n", sc));

	sc->sc_dying = true;

	ubsa_close_pipe(sc);

	for (i = 0; i < sc->sc_numif; i++) {
		if (sc->sc_subdevs[i] != NULL) {
			rv |= config_detach(sc->sc_subdevs[i], flags);
			sc->sc_subdevs[i] = NULL;
		}
	}

	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
			   sc->sc_dev);

	return rv;
}

static int
uhmodem_open(void *addr, int portno)
{
	struct ubsa_softc *sc = addr;
	usbd_status err;

	DPRINTF(("%s: sc = %p\n", __func__, sc));

	if (sc->sc_dying)
		return EIO;

	err = uhmodem_endpointhalt(sc, 0);
	if (err) {
		aprint_error("%s: endpointhalt fail\n", __func__);
		return EIO;
	} else
		usbd_delay_ms(sc->sc_udev, 50);

	if (sc->sc_devflags & A2502) {
		err = a2502_init(sc->sc_udev);
		if (err) {
			aprint_error("%s: a2502init fail\n", __func__);
			return EIO;
		} else
			usbd_delay_ms(sc->sc_udev, 50);
	}
#if 0 /* currently disabled */
	if (sc->sc_devflags & E220) {
		err = e220_init(sc->sc_udev);
		if (err) {
			aprint_error("%s: e220init fail\n", __func__);
			return EIO;
		} else
			usbd_delay_ms(sc->sc_udev, 50);
	}
#endif
	if (sc->sc_intr_number != -1 && sc->sc_intr_pipe == NULL) {
		sc->sc_intr_buf = kmem_alloc(sc->sc_isize, KM_SLEEP);
		/* XXX only iface# = 0 has intr line */
		/* XXX E220 specific? need to check */
		err = usbd_open_pipe_intr(sc->sc_iface[0],
		    sc->sc_intr_number,
		    USBD_SHORT_XFER_OK,
		    &sc->sc_intr_pipe,
		    sc,
		    sc->sc_intr_buf,
		    sc->sc_isize,
		    ubsa_intr,
		    UBSA_INTR_INTERVAL);
		if (err) {
			aprint_error_dev(sc->sc_dev,
			    "cannot open interrupt pipe (addr %d)\n",
			    sc->sc_intr_number);
			return EIO;
		}
	}

	return 0;
}

/*
 * Hauwei E220 needs special request to enable modem function.
 * -- DEVICE_REMOTE_WAKEUP ruquest to endpoint 2.
 */
static  usbd_status
e220_modechange_request(struct usbd_device *dev)
{
#define E220_MODE_CHANGE_REQUEST 0x2
	usb_device_request_t req;
	usbd_status err;

	req.bmRequestType = UT_WRITE_DEVICE;
	req.bRequest = UR_SET_FEATURE;
	USETW(req.wValue, UF_DEVICE_REMOTE_WAKEUP);
	USETW(req.wIndex, E220_MODE_CHANGE_REQUEST);
	USETW(req.wLength, 0);

	DPRINTF(("%s: send e220 mode change request\n", __func__));
	err = usbd_do_request(dev, &req, 0);
	if (err) {
		DPRINTF(("%s: E220 mode change fail\n", __func__));
		return EIO;
	}

	return 0;
#undef E220_MODE_CHANGE_REQUEST
}

static  usbd_status
uhmodem_endpointhalt(struct ubsa_softc *sc, int iface)
{
	usb_device_request_t req;
	usb_endpoint_descriptor_t *ed;
	usb_interface_descriptor_t *id;
	usbd_status err;
	int i;

	/* Find the endpoints */
	id = usbd_get_interface_descriptor(sc->sc_iface[iface]);

	for (i = 0; i < id->bNumEndpoints; i++) {
		ed = usbd_interface2endpoint_descriptor(sc->sc_iface[iface], i);
		if (ed == NULL)
			return EIO;

		if (UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
			/* issue ENDPOINT_HALT request */
			req.bmRequestType = UT_WRITE_ENDPOINT;
			req.bRequest = UR_CLEAR_FEATURE;
			USETW(req.wValue, UF_ENDPOINT_HALT);
			USETW(req.wIndex, ed->bEndpointAddress);
			USETW(req.wLength, 0);
			err = usbd_do_request(sc->sc_udev, &req, 0);
			if (err) {
				DPRINTF(("%s: ENDPOINT_HALT to EP:%d fail\n",
					__func__, ed->bEndpointAddress));
				return EIO;
			}

		}
	} /* end of Endpoint loop */

	return 0;
}

static usbd_status
uhmodem_regwrite(struct usbd_device *dev, uint8_t *data, size_t len)
{
	usb_device_request_t req;
	usbd_status err;

	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
	req.bRequest = UHMODEM_REGWRITE;
	USETW(req.wValue, 0x0000);
	USETW(req.wIndex, 0x0000);
	USETW(req.wLength, len);
	err = usbd_do_request(dev, &req, data);
	if (err)
		return err;

	return 0;
}

static usbd_status
uhmodem_regread(struct usbd_device *dev, uint8_t *data, size_t len)
{
	usb_device_request_t req;
	usbd_status err;

	req.bmRequestType = UT_READ_CLASS_INTERFACE;
	req.bRequest = UHMODEM_REGREAD;
	USETW(req.wValue, 0x0000);
	USETW(req.wIndex, 0x0000);
	USETW(req.wLength, len);
	err = usbd_do_request(dev, &req, data);

	if (err)
		return err;

	return 0;
}

#if 0
static usbd_status
uhmodem_regsetup(struct usbd_device *dev, uint16_t cmd)
{
	usb_device_request_t req;
	usbd_status err;

	req.bmRequestType = UT_READ_CLASS_INTERFACE;
	req.bRequest = UHMODEM_SETUP;
	USETW(req.wValue, cmd);
	USETW(req.wIndex, 0x0000);
	USETW(req.wLength, 0);
	err = usbd_do_request(dev, &req, 0);

	if (err)
		return err;

	return 0;
}
#endif

static  usbd_status
a2502_init(struct usbd_device *dev)
{
	uint8_t data[8];
	static uint8_t init_cmd[] = {0x00, 0xE1, 0x00, 0x00, 0x00, 0x00, 0x08};
#ifdef UHMODEM_DEBUG
	int i;
#endif
	if (uhmodem_regread(dev, data, 7)) {
		DPRINTF(("%s: read fail\n", __func__));
		return EIO;
	}
#ifdef UHMODEM_DEBUG
	printf("%s: readdata: ", __func__);
	for (i = 0; i < 7; i++)
		printf("0x%x ", data[i]);
#endif
	if (uhmodem_regwrite(dev, init_cmd, sizeof(init_cmd)) ) {
		DPRINTF(("%s: write fail\n", __func__));
		return EIO;
	}

	if (uhmodem_regread(dev, data, 7)) {
		DPRINTF(("%s: read fail\n", __func__));
		return EIO;
	}
#ifdef UHMODEM_DEBUG
	printf("%s: readdata: ", __func__);
	printf(" => ");
	for (i = 0; i < 7; i++)
		printf("0x%x ", data[i]);
	printf("\n");
#endif
	return 0;
}


#if 0
/*
 * Windows device driver send these sequens of USB requests.
 * However currently I can't understand what the messege is,
 * disable this code when I get more information about it.
 */
static  usbd_status
e220_init(struct usbd_device *dev)
{
	uint8_t data[8];
	usb_device_request_t req;
	int i;

	/* vendor specific unknown request */
	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
	req.bRequest = 0x02;
	USETW(req.wValue, 0x0001);
	USETW(req.wIndex, 0x0000);
	USETW(req.wLength, 2);
	data[0] = 0x0;
	data[1] = 0x0;
	if (usbd_do_request(dev, &req, data))
		goto error;

	/* vendor specific unknown sequence */
	if(uhmodem_regsetup(dev, 0x1))
		goto error;

	if (uhmodem_regread(dev, data, 7))
		goto error;

	data[1] = 0x8;
	data[2] = 0x7;
	if (uhmodem_regwrite(dev, data, sizeof(data)) )
		goto error;

	if (uhmodem_regread(dev, data, 7))
		goto error;
		/* XXX should verify the read data ? */

	if (uhmodem_regsetup(dev, 0x3))
		goto error;

	return 0;
error:
	DPRINTF(("%s: E220 init request fail\n", __func__));
	return EIO;
}
#endif