xref: /netbsd-src/sys/dev/usb/uhmodem.c (revision c7fb772b85b2b5d4cfb282f868f454b4701534fd)
1 /*	$NetBSD: uhmodem.c,v 1.24 2021/08/07 16:19:17 thorpej 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.24 2021/08/07 16:19:17 thorpej Exp $");
75 
76 #ifdef _KERNEL_OPT
77 #include "opt_usb.h"
78 #endif
79 
80 #include <sys/param.h>
81 #include <sys/systm.h>
82 #include <sys/kernel.h>
83 #include <sys/kmem.h>
84 #include <sys/ioccom.h>
85 #include <sys/fcntl.h>
86 #include <sys/conf.h>
87 #include <sys/tty.h>
88 #include <sys/file.h>
89 #include <sys/select.h>
90 #include <sys/proc.h>
91 #include <sys/device.h>
92 #include <sys/poll.h>
93 #include <sys/sysctl.h>
94 #include <sys/bus.h>
95 
96 #include <dev/usb/usb.h>
97 #include <dev/usb/usbdi.h>
98 #include <dev/usb/usbdi_util.h>
99 #include <dev/usb/usbdivar.h>
100 
101 #include <dev/usb/usbcdc.h>
102 #include <dev/usb/usbdevs.h>
103 #include <dev/usb/usb_quirks.h>
104 #include <dev/usb/ucomvar.h>
105 #include <dev/usb/ubsavar.h>
106 
107 /* vendor specific bRequest */
108 #define	UHMODEM_REGWRITE	0x20
109 #define	UHMODEM_REGREAD		0x21
110 #define UHMODEM_SETUP		0x22
111 
112 #define UHMODEMIBUFSIZE	4096
113 #define UHMODEMOBUFSIZE	4096
114 
115 #ifdef UHMODEM_DEBUG
116 static int	uhmodemdebug = 0;
117 #define DPRINTFN(n, x)  do { \
118 				if (uhmodemdebug > (n)) \
119 					printf x; \
120 			} while (0)
121 #else
122 #define DPRINTFN(n, x)
123 #endif
124 #define DPRINTF(x) DPRINTFN(0, x)
125 
126 static int	uhmodem_open(void *, int);
127 static usbd_status e220_modechange_request(struct usbd_device *);
128 static usbd_status uhmodem_endpointhalt(struct ubsa_softc *, int);
129 static usbd_status uhmodem_regwrite(struct usbd_device *, uint8_t *, size_t);
130 static usbd_status uhmodem_regread(struct usbd_device *, uint8_t *, size_t);
131 static usbd_status a2502_init(struct usbd_device *);
132 #if 0
133 static usbd_status uhmodem_regsetup(struct usbd_device *, uint16_t);
134 static usbd_status e220_init(struct usbd_device *);
135 #endif
136 
137 /* this driver uses the ubsa_softc and methods directly as-is. */
138 struct	ucom_methods uhmodem_methods = {
139 	.ucom_get_status = ubsa_get_status,
140 	.ucom_set = ubsa_set,
141 	.ucom_param = ubsa_param,
142 	.ucom_open = uhmodem_open,
143 	.ucom_close = ubsa_close,
144 };
145 
146 struct uhmodem_type {
147 	struct usb_devno	uhmodem_dev;
148 	uint16_t		uhmodem_coms;	/* number of serial interfaces on the device */
149 	uint16_t		uhmodem_flags;
150 #define	E220	0x0001
151 #define	A2502	0x0002
152 #define	E620	0x0004		/* XXX */
153 				/* Whether or not it is a device different from E220 is not clear. */
154 };
155 
156 static const struct uhmodem_type uhmodem_devs[] = {
157 	/* HUAWEI E220 / Emobile D0[12]HW */
158 	{{ USB_VENDOR_HUAWEI, USB_PRODUCT_HUAWEI_E220 }, 2,	E220},
159 	/* ANYDATA / NTT DoCoMo A2502 */
160 	{{ USB_VENDOR_ANYDATA, USB_PRODUCT_ANYDATA_A2502 }, 3,	A2502},
161 	/* HUAWEI E620 */
162 	{{ USB_VENDOR_HUAWEI, USB_PRODUCT_HUAWEI_MOBILE }, 3,   E620},
163 };
164 #define uhmodem_lookup(v, p) ((const struct uhmodem_type *)usb_lookup(uhmodem_devs, v, p))
165 
166 static int	uhmodem_match(device_t, cfdata_t, void *);
167 static void	uhmodem_attach(device_t, device_t, void *);
168 static void	uhmodem_childdet(device_t, device_t);
169 static int	uhmodem_detach(device_t, int);
170 
171 CFATTACH_DECL2_NEW(uhmodem, sizeof(struct ubsa_softc), uhmodem_match,
172     uhmodem_attach, uhmodem_detach, NULL, NULL, uhmodem_childdet);
173 
174 static int
uhmodem_match(device_t parent,cfdata_t match,void * aux)175 uhmodem_match(device_t parent, cfdata_t match, void *aux)
176 {
177 	struct usbif_attach_arg *uiaa = aux;
178 
179 	if (uhmodem_lookup(uiaa->uiaa_vendor, uiaa->uiaa_product) != NULL)
180 		/* XXX interface# 0,1 provide modem function, but this driver
181 		   handles all modem in single device.  */
182 		if (uiaa->uiaa_ifaceno == 0)
183 			return UMATCH_VENDOR_PRODUCT;
184 	return UMATCH_NONE;
185 }
186 
187 static void
uhmodem_attach(device_t parent,device_t self,void * aux)188 uhmodem_attach(device_t parent, device_t self, void *aux)
189 {
190 	struct ubsa_softc *sc = device_private(self);
191 	struct usbif_attach_arg *uiaa = aux;
192 	struct usbd_device *dev = uiaa->uiaa_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 ucaa;
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_dev = self;
211 	sc->sc_udev = dev;
212 	sc->sc_config_index = UBSA_DEFAULT_CONFIG_INDEX;
213 	sc->sc_numif = 1; /* default device has one interface */
214 	sc->sc_dying = false;
215 
216 	/* Hauwei E220 need special request to change its mode to modem */
217 	if ((uiaa->uiaa_ifaceno == 0) && (uiaa->uiaa_class != 255)) {
218 		err = e220_modechange_request(dev);
219 		if (err) {
220 			aprint_error_dev(self, "failed to change mode: %s\n",
221 				usbd_errstr(err));
222 			goto error;
223 		}
224 		aprint_error_dev(self,
225 		    "mass storage only mode, reattach to enable modem\n");
226 		goto error;
227 	}
228 
229 	/*
230 	 * initialize rts, dtr variables to something
231 	 * different from boolean 0, 1
232 	 */
233 	sc->sc_dtr = -1;
234 	sc->sc_rts = -1;
235 
236 	sc->sc_quadumts = 1;
237 	sc->sc_config_index = 0;
238 	sc->sc_numif = uhmodem_lookup(uiaa->uiaa_vendor, uiaa->uiaa_product)->uhmodem_coms;
239 	sc->sc_devflags = uhmodem_lookup(uiaa->uiaa_vendor, uiaa->uiaa_product)->uhmodem_flags;
240 
241 	DPRINTF(("uhmodem attach: sc = %p\n", sc));
242 
243 	/* Move the device into the configured state. */
244 	err = usbd_set_config_index(dev, sc->sc_config_index, 1);
245 	if (err) {
246 		aprint_error_dev(self, "failed to set configuration: %s\n",
247 		    usbd_errstr(err));
248 		goto error;
249 	}
250 
251 	/* get the config descriptor */
252 	cdesc = usbd_get_config_descriptor(sc->sc_udev);
253 	if (cdesc == NULL) {
254 		aprint_error_dev(self,
255 		    "failed to get configuration descriptor\n");
256 		goto error;
257 	}
258 
259 	sc->sc_intr_number = -1;
260 	sc->sc_intr_pipe = NULL;
261 
262 	/* get the interfaces */
263 	for (i = 0; i < sc->sc_numif; i++) {
264 		err = usbd_device2interface_handle(dev, UBSA_IFACE_INDEX_OFFSET+i,
265 				 &sc->sc_iface[i]);
266 		if (err) {
267 			if (i == 0){
268 				/* can not get main interface */
269 				goto error;
270 			} else
271 				break;
272 		}
273 
274 		/* Find the endpoints */
275 		id = usbd_get_interface_descriptor(sc->sc_iface[i]);
276 		sc->sc_iface_number[i] = id->bInterfaceNumber;
277 
278 		/* initialize endpoints */
279 		ucaa.ucaa_bulkin = ucaa.ucaa_bulkout = -1;
280 
281 		for (j = 0; j < id->bNumEndpoints; j++) {
282 			ed = usbd_interface2endpoint_descriptor(
283 				sc->sc_iface[i], j);
284 			if (ed == NULL) {
285 				aprint_error_dev(self,
286 				    "no endpoint descriptor for %d "
287 				    "(interface: %d)\n", j, i);
288 				break;
289 			}
290 
291 			if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
292 			    UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
293 				sc->sc_intr_number = ed->bEndpointAddress;
294 				sc->sc_isize = UGETW(ed->wMaxPacketSize);
295 			} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
296 			    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
297 				ucaa.ucaa_bulkin = ed->bEndpointAddress;
298 			} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
299 			    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
300 				ucaa.ucaa_bulkout = ed->bEndpointAddress;
301 			}
302 		} /* end of Endpoint loop */
303 
304 		if (sc->sc_intr_number == -1) {
305 			aprint_error_dev(self, "HUAWEI E220 need to re-attach "
306 			    "to enable modem function\n");
307 			if (i == 0) {
308 				/* could not get intr for main tty */
309 				goto error;
310 			} else
311 				break;
312 		}
313 		if (ucaa.ucaa_bulkin == -1) {
314 			aprint_error_dev(self,
315 			    "Could not find data bulk in\n");
316 			goto error;
317 		}
318 
319 		if (ucaa.ucaa_bulkout == -1) {
320 			aprint_error_dev(self,
321 			    "Could not find data bulk out\n");
322 			goto error;
323 		}
324 
325 		switch (i) {
326 		case 0:
327 			snprintf(comname, sizeof(comname), "modem");
328 			break;
329 		case 1:
330 			snprintf(comname, sizeof(comname), "alt#1");
331 			break;
332 		case 2:
333 			snprintf(comname, sizeof(comname), "alt#2");
334 			break;
335 		default:
336 			snprintf(comname, sizeof(comname), "int#%d", i);
337 			break;
338 		}
339 
340 		ucaa.ucaa_portno = i;
341 		/* ucaa_bulkin, ucaa_bulkout set above */
342 		ucaa.ucaa_ibufsize = UHMODEMIBUFSIZE;
343 		ucaa.ucaa_obufsize = UHMODEMOBUFSIZE;
344 		ucaa.ucaa_ibufsizepad = UHMODEMIBUFSIZE;
345 		ucaa.ucaa_opkthdrlen = 0;
346 		ucaa.ucaa_device = dev;
347 		ucaa.ucaa_iface = sc->sc_iface[i];
348 		ucaa.ucaa_methods = &uhmodem_methods;
349 		ucaa.ucaa_arg = sc;
350 		ucaa.ucaa_info = comname;
351 		DPRINTF(("uhmodem: int#=%d, in = %#x, out = %#x, intr = %#x\n",
352 		    i, ucaa.ucaa_bulkin, ucaa.ucaa_bulkout,
353 		    sc->sc_intr_number));
354 		sc->sc_subdevs[i] = config_found(self, &ucaa, ucomprint,
355 		    CFARGS(.submatch = ucomsubmatch));
356 
357 		/* issue endpoint halt to each interface */
358 		err = uhmodem_endpointhalt(sc, i);
359 		if (err)
360 			aprint_error("%s: endpointhalt fail\n", __func__);
361 		else
362 			usbd_delay_ms(sc->sc_udev, 50);
363 	} /* end of Interface loop */
364 
365 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
366 			   sc->sc_dev);
367 
368 	if (!pmf_device_register(self, NULL, NULL))
369 		aprint_error_dev(self, "couldn't establish power handler\n");
370 
371 	return;
372 
373 error:
374 	sc->sc_dying = true;
375 	return;
376 }
377 
378 static void
uhmodem_childdet(device_t self,device_t child)379 uhmodem_childdet(device_t self, device_t child)
380 {
381 	int i;
382 	struct ubsa_softc *sc = device_private(self);
383 
384 	for (i = 0; i < sc->sc_numif; i++) {
385 		if (sc->sc_subdevs[i] == child)
386 			break;
387 	}
388 	KASSERT(i < sc->sc_numif);
389 	sc->sc_subdevs[i] = NULL;
390 }
391 
392 static int
uhmodem_detach(device_t self,int flags)393 uhmodem_detach(device_t self, int flags)
394 {
395 	struct ubsa_softc *sc = device_private(self);
396 	int i;
397 	int rv = 0;
398 
399 	DPRINTF(("uhmodem_detach: sc = %p\n", sc));
400 
401 	sc->sc_dying = true;
402 
403 	ubsa_close_pipe(sc);
404 
405 	for (i = 0; i < sc->sc_numif; i++) {
406 		if (sc->sc_subdevs[i] != NULL) {
407 			rv |= config_detach(sc->sc_subdevs[i], flags);
408 			sc->sc_subdevs[i] = NULL;
409 		}
410 	}
411 
412 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
413 			   sc->sc_dev);
414 
415 	return rv;
416 }
417 
418 static int
uhmodem_open(void * addr,int portno)419 uhmodem_open(void *addr, int portno)
420 {
421 	struct ubsa_softc *sc = addr;
422 	usbd_status err;
423 
424 	DPRINTF(("%s: sc = %p\n", __func__, sc));
425 
426 	if (sc->sc_dying)
427 		return EIO;
428 
429 	err = uhmodem_endpointhalt(sc, 0);
430 	if (err) {
431 		aprint_error("%s: endpointhalt fail\n", __func__);
432 		return EIO;
433 	} else
434 		usbd_delay_ms(sc->sc_udev, 50);
435 
436 	if (sc->sc_devflags & A2502) {
437 		err = a2502_init(sc->sc_udev);
438 		if (err) {
439 			aprint_error("%s: a2502init fail\n", __func__);
440 			return EIO;
441 		} else
442 			usbd_delay_ms(sc->sc_udev, 50);
443 	}
444 #if 0 /* currently disabled */
445 	if (sc->sc_devflags & E220) {
446 		err = e220_init(sc->sc_udev);
447 		if (err) {
448 			aprint_error("%s: e220init fail\n", __func__);
449 			return EIO;
450 		} else
451 			usbd_delay_ms(sc->sc_udev, 50);
452 	}
453 #endif
454 	if (sc->sc_intr_number != -1 && sc->sc_intr_pipe == NULL) {
455 		sc->sc_intr_buf = kmem_alloc(sc->sc_isize, KM_SLEEP);
456 		/* XXX only iface# = 0 has intr line */
457 		/* XXX E220 specific? need to check */
458 		err = usbd_open_pipe_intr(sc->sc_iface[0],
459 		    sc->sc_intr_number,
460 		    USBD_SHORT_XFER_OK,
461 		    &sc->sc_intr_pipe,
462 		    sc,
463 		    sc->sc_intr_buf,
464 		    sc->sc_isize,
465 		    ubsa_intr,
466 		    UBSA_INTR_INTERVAL);
467 		if (err) {
468 			aprint_error_dev(sc->sc_dev,
469 			    "cannot open interrupt pipe (addr %d)\n",
470 			    sc->sc_intr_number);
471 			return EIO;
472 		}
473 	}
474 
475 	return 0;
476 }
477 
478 /*
479  * Hauwei E220 needs special request to enable modem function.
480  * -- DEVICE_REMOTE_WAKEUP ruquest to endpoint 2.
481  */
482 static  usbd_status
e220_modechange_request(struct usbd_device * dev)483 e220_modechange_request(struct usbd_device *dev)
484 {
485 #define E220_MODE_CHANGE_REQUEST 0x2
486 	usb_device_request_t req;
487 	usbd_status err;
488 
489 	req.bmRequestType = UT_WRITE_DEVICE;
490 	req.bRequest = UR_SET_FEATURE;
491 	USETW(req.wValue, UF_DEVICE_REMOTE_WAKEUP);
492 	USETW(req.wIndex, E220_MODE_CHANGE_REQUEST);
493 	USETW(req.wLength, 0);
494 
495 	DPRINTF(("%s: send e220 mode change request\n", __func__));
496 	err = usbd_do_request(dev, &req, 0);
497 	if (err) {
498 		DPRINTF(("%s: E220 mode change fail\n", __func__));
499 		return EIO;
500 	}
501 
502 	return 0;
503 #undef E220_MODE_CHANGE_REQUEST
504 }
505 
506 static  usbd_status
uhmodem_endpointhalt(struct ubsa_softc * sc,int iface)507 uhmodem_endpointhalt(struct ubsa_softc *sc, int iface)
508 {
509 	usb_endpoint_descriptor_t *ed;
510 	usb_interface_descriptor_t *id;
511 	usbd_status err;
512 	int i;
513 
514 	/* Find the endpoints */
515 	id = usbd_get_interface_descriptor(sc->sc_iface[iface]);
516 
517 	for (i = 0; i < id->bNumEndpoints; i++) {
518 		ed = usbd_interface2endpoint_descriptor(sc->sc_iface[iface], i);
519 		if (ed == NULL)
520 			return EIO;
521 
522 		if (UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
523 			err = usbd_clear_endpoint_feature(sc->sc_udev,
524 			    ed->bEndpointAddress, UF_ENDPOINT_HALT);
525 			if (err) {
526 				DPRINTF(("%s: ENDPOINT_HALT to EP:%d fail\n",
527 					__func__, ed->bEndpointAddress));
528 				return EIO;
529 			}
530 
531 		}
532 	} /* end of Endpoint loop */
533 
534 	return 0;
535 }
536 
537 static usbd_status
uhmodem_regwrite(struct usbd_device * dev,uint8_t * data,size_t len)538 uhmodem_regwrite(struct usbd_device *dev, uint8_t *data, size_t len)
539 {
540 	usb_device_request_t req;
541 	usbd_status err;
542 
543 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
544 	req.bRequest = UHMODEM_REGWRITE;
545 	USETW(req.wValue, 0x0000);
546 	USETW(req.wIndex, 0x0000);
547 	USETW(req.wLength, len);
548 	err = usbd_do_request(dev, &req, data);
549 	if (err)
550 		return err;
551 
552 	return 0;
553 }
554 
555 static usbd_status
uhmodem_regread(struct usbd_device * dev,uint8_t * data,size_t len)556 uhmodem_regread(struct usbd_device *dev, uint8_t *data, size_t len)
557 {
558 	usb_device_request_t req;
559 	usbd_status err;
560 
561 	req.bmRequestType = UT_READ_CLASS_INTERFACE;
562 	req.bRequest = UHMODEM_REGREAD;
563 	USETW(req.wValue, 0x0000);
564 	USETW(req.wIndex, 0x0000);
565 	USETW(req.wLength, len);
566 	err = usbd_do_request(dev, &req, data);
567 
568 	if (err)
569 		return err;
570 
571 	return 0;
572 }
573 
574 #if 0
575 static usbd_status
576 uhmodem_regsetup(struct usbd_device *dev, uint16_t cmd)
577 {
578 	usb_device_request_t req;
579 	usbd_status err;
580 
581 	req.bmRequestType = UT_READ_CLASS_INTERFACE;
582 	req.bRequest = UHMODEM_SETUP;
583 	USETW(req.wValue, cmd);
584 	USETW(req.wIndex, 0x0000);
585 	USETW(req.wLength, 0);
586 	err = usbd_do_request(dev, &req, 0);
587 
588 	if (err)
589 		return err;
590 
591 	return 0;
592 }
593 #endif
594 
595 static  usbd_status
a2502_init(struct usbd_device * dev)596 a2502_init(struct usbd_device *dev)
597 {
598 	uint8_t data[8];
599 	static uint8_t init_cmd[] = {0x00, 0xE1, 0x00, 0x00, 0x00, 0x00, 0x08};
600 #ifdef UHMODEM_DEBUG
601 	int i;
602 #endif
603 	if (uhmodem_regread(dev, data, 7)) {
604 		DPRINTF(("%s: read fail\n", __func__));
605 		return EIO;
606 	}
607 #ifdef UHMODEM_DEBUG
608 	printf("%s: readdata: ", __func__);
609 	for (i = 0; i < 7; i++)
610 		printf("%#x ", data[i]);
611 #endif
612 	if (uhmodem_regwrite(dev, init_cmd, sizeof(init_cmd)) ) {
613 		DPRINTF(("%s: write fail\n", __func__));
614 		return EIO;
615 	}
616 
617 	if (uhmodem_regread(dev, data, 7)) {
618 		DPRINTF(("%s: read fail\n", __func__));
619 		return EIO;
620 	}
621 #ifdef UHMODEM_DEBUG
622 	printf("%s: readdata: ", __func__);
623 	printf(" => ");
624 	for (i = 0; i < 7; i++)
625 		printf("%#x ", data[i]);
626 	printf("\n");
627 #endif
628 	return 0;
629 }
630 
631 
632 #if 0
633 /*
634  * Windows device driver send these sequens of USB requests.
635  * However currently I can't understand what the messege is,
636  * disable this code when I get more information about it.
637  */
638 static  usbd_status
639 e220_init(struct usbd_device *dev)
640 {
641 	uint8_t data[8];
642 	usb_device_request_t req;
643 	int i;
644 
645 	/* vendor specific unknown request */
646 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
647 	req.bRequest = 0x02;
648 	USETW(req.wValue, 0x0001);
649 	USETW(req.wIndex, 0x0000);
650 	USETW(req.wLength, 2);
651 	data[0] = 0x0;
652 	data[1] = 0x0;
653 	if (usbd_do_request(dev, &req, data))
654 		goto error;
655 
656 	/* vendor specific unknown sequence */
657 	if(uhmodem_regsetup(dev, 0x1))
658 		goto error;
659 
660 	if (uhmodem_regread(dev, data, 7))
661 		goto error;
662 
663 	data[1] = 0x8;
664 	data[2] = 0x7;
665 	if (uhmodem_regwrite(dev, data, sizeof(data)) )
666 		goto error;
667 
668 	if (uhmodem_regread(dev, data, 7))
669 		goto error;
670 		/* XXX should verify the read data ? */
671 
672 	if (uhmodem_regsetup(dev, 0x3))
673 		goto error;
674 
675 	return 0;
676 error:
677 	DPRINTF(("%s: E220 init request fail\n", __func__));
678 	return EIO;
679 }
680 #endif
681 
682