1 /* $NetBSD: uhmodem.c,v 1.20 2019/05/27 03:08:13 maya 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.20 2019/05/27 03:08:13 maya 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 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 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 = 0x%x, out = 0x%x, intr = 0x%x\n", 352 i, ucaa.ucaa_bulkin, ucaa.ucaa_bulkout, 353 sc->sc_intr_number)); 354 sc->sc_subdevs[i] = config_found_sm_loc(self, "ucombus", NULL, 355 &ucaa, ucomprint, 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 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 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 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 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 507 uhmodem_endpointhalt(struct ubsa_softc *sc, int iface) 508 { 509 usb_device_request_t req; 510 usb_endpoint_descriptor_t *ed; 511 usb_interface_descriptor_t *id; 512 usbd_status err; 513 int i; 514 515 /* Find the endpoints */ 516 id = usbd_get_interface_descriptor(sc->sc_iface[iface]); 517 518 for (i = 0; i < id->bNumEndpoints; i++) { 519 ed = usbd_interface2endpoint_descriptor(sc->sc_iface[iface], i); 520 if (ed == NULL) 521 return EIO; 522 523 if (UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) { 524 /* issue ENDPOINT_HALT request */ 525 req.bmRequestType = UT_WRITE_ENDPOINT; 526 req.bRequest = UR_CLEAR_FEATURE; 527 USETW(req.wValue, UF_ENDPOINT_HALT); 528 USETW(req.wIndex, ed->bEndpointAddress); 529 USETW(req.wLength, 0); 530 err = usbd_do_request(sc->sc_udev, &req, 0); 531 if (err) { 532 DPRINTF(("%s: ENDPOINT_HALT to EP:%d fail\n", 533 __func__, ed->bEndpointAddress)); 534 return EIO; 535 } 536 537 } 538 } /* end of Endpoint loop */ 539 540 return 0; 541 } 542 543 static usbd_status 544 uhmodem_regwrite(struct usbd_device *dev, uint8_t *data, size_t len) 545 { 546 usb_device_request_t req; 547 usbd_status err; 548 549 req.bmRequestType = UT_WRITE_CLASS_INTERFACE; 550 req.bRequest = UHMODEM_REGWRITE; 551 USETW(req.wValue, 0x0000); 552 USETW(req.wIndex, 0x0000); 553 USETW(req.wLength, len); 554 err = usbd_do_request(dev, &req, data); 555 if (err) 556 return err; 557 558 return 0; 559 } 560 561 static usbd_status 562 uhmodem_regread(struct usbd_device *dev, uint8_t *data, size_t len) 563 { 564 usb_device_request_t req; 565 usbd_status err; 566 567 req.bmRequestType = UT_READ_CLASS_INTERFACE; 568 req.bRequest = UHMODEM_REGREAD; 569 USETW(req.wValue, 0x0000); 570 USETW(req.wIndex, 0x0000); 571 USETW(req.wLength, len); 572 err = usbd_do_request(dev, &req, data); 573 574 if (err) 575 return err; 576 577 return 0; 578 } 579 580 #if 0 581 static usbd_status 582 uhmodem_regsetup(struct usbd_device *dev, uint16_t cmd) 583 { 584 usb_device_request_t req; 585 usbd_status err; 586 587 req.bmRequestType = UT_READ_CLASS_INTERFACE; 588 req.bRequest = UHMODEM_SETUP; 589 USETW(req.wValue, cmd); 590 USETW(req.wIndex, 0x0000); 591 USETW(req.wLength, 0); 592 err = usbd_do_request(dev, &req, 0); 593 594 if (err) 595 return err; 596 597 return 0; 598 } 599 #endif 600 601 static usbd_status 602 a2502_init(struct usbd_device *dev) 603 { 604 uint8_t data[8]; 605 static uint8_t init_cmd[] = {0x00, 0xE1, 0x00, 0x00, 0x00, 0x00, 0x08}; 606 #ifdef UHMODEM_DEBUG 607 int i; 608 #endif 609 if (uhmodem_regread(dev, data, 7)) { 610 DPRINTF(("%s: read fail\n", __func__)); 611 return EIO; 612 } 613 #ifdef UHMODEM_DEBUG 614 printf("%s: readdata: ", __func__); 615 for (i = 0; i < 7; i++) 616 printf("0x%x ", data[i]); 617 #endif 618 if (uhmodem_regwrite(dev, init_cmd, sizeof(init_cmd)) ) { 619 DPRINTF(("%s: write fail\n", __func__)); 620 return EIO; 621 } 622 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 printf(" => "); 630 for (i = 0; i < 7; i++) 631 printf("0x%x ", data[i]); 632 printf("\n"); 633 #endif 634 return 0; 635 } 636 637 638 #if 0 639 /* 640 * Windows device driver send these sequens of USB requests. 641 * However currently I can't understand what the messege is, 642 * disable this code when I get more information about it. 643 */ 644 static usbd_status 645 e220_init(struct usbd_device *dev) 646 { 647 uint8_t data[8]; 648 usb_device_request_t req; 649 int i; 650 651 /* vendor specific unknown request */ 652 req.bmRequestType = UT_WRITE_CLASS_INTERFACE; 653 req.bRequest = 0x02; 654 USETW(req.wValue, 0x0001); 655 USETW(req.wIndex, 0x0000); 656 USETW(req.wLength, 2); 657 data[0] = 0x0; 658 data[1] = 0x0; 659 if (usbd_do_request(dev, &req, data)) 660 goto error; 661 662 /* vendor specific unknown sequence */ 663 if(uhmodem_regsetup(dev, 0x1)) 664 goto error; 665 666 if (uhmodem_regread(dev, data, 7)) 667 goto error; 668 669 data[1] = 0x8; 670 data[2] = 0x7; 671 if (uhmodem_regwrite(dev, data, sizeof(data)) ) 672 goto error; 673 674 if (uhmodem_regread(dev, data, 7)) 675 goto error; 676 /* XXX should verify the read data ? */ 677 678 if (uhmodem_regsetup(dev, 0x3)) 679 goto error; 680 681 return 0; 682 error: 683 DPRINTF(("%s: E220 init request fail\n", __func__)); 684 return EIO; 685 } 686 #endif 687 688