1 /* $NetBSD: uhmodem.c,v 1.23 2021/04/24 23:36:59 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.23 2021/04/24 23:36:59 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 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 = %#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 CFARG_SUBMATCH, ucomsubmatch, 356 CFARG_EOL); 357 358 /* issue endpoint halt to each interface */ 359 err = uhmodem_endpointhalt(sc, i); 360 if (err) 361 aprint_error("%s: endpointhalt fail\n", __func__); 362 else 363 usbd_delay_ms(sc->sc_udev, 50); 364 } /* end of Interface loop */ 365 366 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, 367 sc->sc_dev); 368 369 if (!pmf_device_register(self, NULL, NULL)) 370 aprint_error_dev(self, "couldn't establish power handler\n"); 371 372 return; 373 374 error: 375 sc->sc_dying = true; 376 return; 377 } 378 379 static void 380 uhmodem_childdet(device_t self, device_t child) 381 { 382 int i; 383 struct ubsa_softc *sc = device_private(self); 384 385 for (i = 0; i < sc->sc_numif; i++) { 386 if (sc->sc_subdevs[i] == child) 387 break; 388 } 389 KASSERT(i < sc->sc_numif); 390 sc->sc_subdevs[i] = NULL; 391 } 392 393 static int 394 uhmodem_detach(device_t self, int flags) 395 { 396 struct ubsa_softc *sc = device_private(self); 397 int i; 398 int rv = 0; 399 400 DPRINTF(("uhmodem_detach: sc = %p\n", sc)); 401 402 sc->sc_dying = true; 403 404 ubsa_close_pipe(sc); 405 406 for (i = 0; i < sc->sc_numif; i++) { 407 if (sc->sc_subdevs[i] != NULL) { 408 rv |= config_detach(sc->sc_subdevs[i], flags); 409 sc->sc_subdevs[i] = NULL; 410 } 411 } 412 413 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, 414 sc->sc_dev); 415 416 return rv; 417 } 418 419 static int 420 uhmodem_open(void *addr, int portno) 421 { 422 struct ubsa_softc *sc = addr; 423 usbd_status err; 424 425 DPRINTF(("%s: sc = %p\n", __func__, sc)); 426 427 if (sc->sc_dying) 428 return EIO; 429 430 err = uhmodem_endpointhalt(sc, 0); 431 if (err) { 432 aprint_error("%s: endpointhalt fail\n", __func__); 433 return EIO; 434 } else 435 usbd_delay_ms(sc->sc_udev, 50); 436 437 if (sc->sc_devflags & A2502) { 438 err = a2502_init(sc->sc_udev); 439 if (err) { 440 aprint_error("%s: a2502init fail\n", __func__); 441 return EIO; 442 } else 443 usbd_delay_ms(sc->sc_udev, 50); 444 } 445 #if 0 /* currently disabled */ 446 if (sc->sc_devflags & E220) { 447 err = e220_init(sc->sc_udev); 448 if (err) { 449 aprint_error("%s: e220init fail\n", __func__); 450 return EIO; 451 } else 452 usbd_delay_ms(sc->sc_udev, 50); 453 } 454 #endif 455 if (sc->sc_intr_number != -1 && sc->sc_intr_pipe == NULL) { 456 sc->sc_intr_buf = kmem_alloc(sc->sc_isize, KM_SLEEP); 457 /* XXX only iface# = 0 has intr line */ 458 /* XXX E220 specific? need to check */ 459 err = usbd_open_pipe_intr(sc->sc_iface[0], 460 sc->sc_intr_number, 461 USBD_SHORT_XFER_OK, 462 &sc->sc_intr_pipe, 463 sc, 464 sc->sc_intr_buf, 465 sc->sc_isize, 466 ubsa_intr, 467 UBSA_INTR_INTERVAL); 468 if (err) { 469 aprint_error_dev(sc->sc_dev, 470 "cannot open interrupt pipe (addr %d)\n", 471 sc->sc_intr_number); 472 return EIO; 473 } 474 } 475 476 return 0; 477 } 478 479 /* 480 * Hauwei E220 needs special request to enable modem function. 481 * -- DEVICE_REMOTE_WAKEUP ruquest to endpoint 2. 482 */ 483 static usbd_status 484 e220_modechange_request(struct usbd_device *dev) 485 { 486 #define E220_MODE_CHANGE_REQUEST 0x2 487 usb_device_request_t req; 488 usbd_status err; 489 490 req.bmRequestType = UT_WRITE_DEVICE; 491 req.bRequest = UR_SET_FEATURE; 492 USETW(req.wValue, UF_DEVICE_REMOTE_WAKEUP); 493 USETW(req.wIndex, E220_MODE_CHANGE_REQUEST); 494 USETW(req.wLength, 0); 495 496 DPRINTF(("%s: send e220 mode change request\n", __func__)); 497 err = usbd_do_request(dev, &req, 0); 498 if (err) { 499 DPRINTF(("%s: E220 mode change fail\n", __func__)); 500 return EIO; 501 } 502 503 return 0; 504 #undef E220_MODE_CHANGE_REQUEST 505 } 506 507 static usbd_status 508 uhmodem_endpointhalt(struct ubsa_softc *sc, int iface) 509 { 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 err = usbd_clear_endpoint_feature(sc->sc_udev, 525 ed->bEndpointAddress, UF_ENDPOINT_HALT); 526 if (err) { 527 DPRINTF(("%s: ENDPOINT_HALT to EP:%d fail\n", 528 __func__, ed->bEndpointAddress)); 529 return EIO; 530 } 531 532 } 533 } /* end of Endpoint loop */ 534 535 return 0; 536 } 537 538 static usbd_status 539 uhmodem_regwrite(struct usbd_device *dev, uint8_t *data, size_t len) 540 { 541 usb_device_request_t req; 542 usbd_status err; 543 544 req.bmRequestType = UT_WRITE_CLASS_INTERFACE; 545 req.bRequest = UHMODEM_REGWRITE; 546 USETW(req.wValue, 0x0000); 547 USETW(req.wIndex, 0x0000); 548 USETW(req.wLength, len); 549 err = usbd_do_request(dev, &req, data); 550 if (err) 551 return err; 552 553 return 0; 554 } 555 556 static usbd_status 557 uhmodem_regread(struct usbd_device *dev, uint8_t *data, size_t len) 558 { 559 usb_device_request_t req; 560 usbd_status err; 561 562 req.bmRequestType = UT_READ_CLASS_INTERFACE; 563 req.bRequest = UHMODEM_REGREAD; 564 USETW(req.wValue, 0x0000); 565 USETW(req.wIndex, 0x0000); 566 USETW(req.wLength, len); 567 err = usbd_do_request(dev, &req, data); 568 569 if (err) 570 return err; 571 572 return 0; 573 } 574 575 #if 0 576 static usbd_status 577 uhmodem_regsetup(struct usbd_device *dev, uint16_t cmd) 578 { 579 usb_device_request_t req; 580 usbd_status err; 581 582 req.bmRequestType = UT_READ_CLASS_INTERFACE; 583 req.bRequest = UHMODEM_SETUP; 584 USETW(req.wValue, cmd); 585 USETW(req.wIndex, 0x0000); 586 USETW(req.wLength, 0); 587 err = usbd_do_request(dev, &req, 0); 588 589 if (err) 590 return err; 591 592 return 0; 593 } 594 #endif 595 596 static usbd_status 597 a2502_init(struct usbd_device *dev) 598 { 599 uint8_t data[8]; 600 static uint8_t init_cmd[] = {0x00, 0xE1, 0x00, 0x00, 0x00, 0x00, 0x08}; 601 #ifdef UHMODEM_DEBUG 602 int i; 603 #endif 604 if (uhmodem_regread(dev, data, 7)) { 605 DPRINTF(("%s: read fail\n", __func__)); 606 return EIO; 607 } 608 #ifdef UHMODEM_DEBUG 609 printf("%s: readdata: ", __func__); 610 for (i = 0; i < 7; i++) 611 printf("%#x ", data[i]); 612 #endif 613 if (uhmodem_regwrite(dev, init_cmd, sizeof(init_cmd)) ) { 614 DPRINTF(("%s: write fail\n", __func__)); 615 return EIO; 616 } 617 618 if (uhmodem_regread(dev, data, 7)) { 619 DPRINTF(("%s: read fail\n", __func__)); 620 return EIO; 621 } 622 #ifdef UHMODEM_DEBUG 623 printf("%s: readdata: ", __func__); 624 printf(" => "); 625 for (i = 0; i < 7; i++) 626 printf("%#x ", data[i]); 627 printf("\n"); 628 #endif 629 return 0; 630 } 631 632 633 #if 0 634 /* 635 * Windows device driver send these sequens of USB requests. 636 * However currently I can't understand what the messege is, 637 * disable this code when I get more information about it. 638 */ 639 static usbd_status 640 e220_init(struct usbd_device *dev) 641 { 642 uint8_t data[8]; 643 usb_device_request_t req; 644 int i; 645 646 /* vendor specific unknown request */ 647 req.bmRequestType = UT_WRITE_CLASS_INTERFACE; 648 req.bRequest = 0x02; 649 USETW(req.wValue, 0x0001); 650 USETW(req.wIndex, 0x0000); 651 USETW(req.wLength, 2); 652 data[0] = 0x0; 653 data[1] = 0x0; 654 if (usbd_do_request(dev, &req, data)) 655 goto error; 656 657 /* vendor specific unknown sequence */ 658 if(uhmodem_regsetup(dev, 0x1)) 659 goto error; 660 661 if (uhmodem_regread(dev, data, 7)) 662 goto error; 663 664 data[1] = 0x8; 665 data[2] = 0x7; 666 if (uhmodem_regwrite(dev, data, sizeof(data)) ) 667 goto error; 668 669 if (uhmodem_regread(dev, data, 7)) 670 goto error; 671 /* XXX should verify the read data ? */ 672 673 if (uhmodem_regsetup(dev, 0x3)) 674 goto error; 675 676 return 0; 677 error: 678 DPRINTF(("%s: E220 init request fail\n", __func__)); 679 return EIO; 680 } 681 #endif 682 683