1 /* $NetBSD: umodem.c,v 1.45 2002/09/23 05:51:23 simonb Exp $ */ 2 3 /*- 4 * Copyright (c) 2003, M. Warner Losh <imp@FreeBSD.org>. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 /*- 30 * Copyright (c) 1998 The NetBSD Foundation, Inc. 31 * All rights reserved. 32 * 33 * This code is derived from software contributed to The NetBSD Foundation 34 * by Lennart Augustsson (lennart@augustsson.net) at 35 * Carlstedt Research & Technology. 36 * 37 * Redistribution and use in source and binary forms, with or without 38 * modification, are permitted provided that the following conditions 39 * are met: 40 * 1. Redistributions of source code must retain the above copyright 41 * notice, this list of conditions and the following disclaimer. 42 * 2. Redistributions in binary form must reproduce the above copyright 43 * notice, this list of conditions and the following disclaimer in the 44 * documentation and/or other materials provided with the distribution. 45 * 3. All advertising materials mentioning features or use of this software 46 * must display the following acknowledgement: 47 * This product includes software developed by the NetBSD 48 * Foundation, Inc. and its contributors. 49 * 4. Neither the name of The NetBSD Foundation nor the names of its 50 * contributors may be used to endorse or promote products derived 51 * from this software without specific prior written permission. 52 * 53 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 54 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 55 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 56 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 57 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 58 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 59 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 60 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 61 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 62 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 63 * POSSIBILITY OF SUCH DAMAGE. 64 */ 65 66 /* 67 * Comm Class spec: http://www.usb.org/developers/devclass_docs/usbccs10.pdf 68 * http://www.usb.org/developers/devclass_docs/usbcdc11.pdf 69 * http://www.usb.org/developers/devclass_docs/cdc_wmc10.zip 70 */ 71 72 /* 73 * TODO: 74 * - Add error recovery in various places; the big problem is what 75 * to do in a callback if there is an error. 76 * - Implement a Call Device for modems without multiplexed commands. 77 * 78 */ 79 80 #include <sys/stdint.h> 81 #include <sys/param.h> 82 #include <sys/queue.h> 83 #include <sys/types.h> 84 #include <sys/systm.h> 85 #include <sys/kernel.h> 86 #include <sys/bus.h> 87 #include <sys/module.h> 88 #include <sys/lock.h> 89 #include <sys/condvar.h> 90 #include <sys/sysctl.h> 91 #include <sys/unistd.h> 92 #include <sys/callout.h> 93 #include <sys/malloc.h> 94 #include <sys/priv.h> 95 96 #include <bus/u4b/usb.h> 97 #include <bus/u4b/usbdi.h> 98 #include <bus/u4b/usbdi_util.h> 99 #include <bus/u4b/usbhid.h> 100 #include <bus/u4b/usb_cdc.h> 101 #include <bus/u4b/usbdevs.h> 102 103 #include <bus/u4b/usb_ioctl.h> 104 105 #define USB_DEBUG_VAR umodem_debug 106 #include <bus/u4b/usb_debug.h> 107 #include <bus/u4b/usb_process.h> 108 #include <bus/u4b/quirk/usb_quirk.h> 109 110 #include <bus/u4b/serial/usb_serial.h> 111 112 #ifdef USB_DEBUG 113 static int umodem_debug = 0; 114 115 static SYSCTL_NODE(_hw_usb, OID_AUTO, umodem, CTLFLAG_RW, 0, "USB umodem"); 116 SYSCTL_INT(_hw_usb_umodem, OID_AUTO, debug, CTLFLAG_RW, 117 &umodem_debug, 0, "Debug level"); 118 #endif 119 120 static const STRUCT_USB_HOST_ID umodem_devs[] = { 121 /* Generic Modem class match */ 122 {USB_IFACE_CLASS(UICLASS_CDC), 123 USB_IFACE_SUBCLASS(UISUBCLASS_ABSTRACT_CONTROL_MODEL), 124 USB_IFACE_PROTOCOL(UIPROTO_CDC_AT)}, 125 /* Huawei Modem class match */ 126 {USB_IFACE_CLASS(UICLASS_CDC), 127 USB_IFACE_SUBCLASS(UISUBCLASS_ABSTRACT_CONTROL_MODEL), 128 USB_IFACE_PROTOCOL(0xFF)}, 129 /* Kyocera AH-K3001V */ 130 {USB_VPI(USB_VENDOR_KYOCERA, USB_PRODUCT_KYOCERA_AHK3001V, 1)}, 131 {USB_VPI(USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC5720, 1)}, 132 {USB_VPI(USB_VENDOR_CURITEL, USB_PRODUCT_CURITEL_PC5740, 1)}, 133 }; 134 135 /* 136 * As speeds for umodem deivces increase, these numbers will need to 137 * be increased. They should be good for G3 speeds and below. 138 * 139 * TODO: The TTY buffers should be increased! 140 */ 141 #define UMODEM_BUF_SIZE 1024 142 143 enum { 144 UMODEM_BULK_WR, 145 UMODEM_BULK_RD, 146 UMODEM_INTR_RD, 147 UMODEM_N_TRANSFER, 148 }; 149 150 #define UMODEM_MODVER 1 /* module version */ 151 152 struct umodem_softc { 153 struct ucom_super_softc sc_super_ucom; 154 struct ucom_softc sc_ucom; 155 156 struct usb_xfer *sc_xfer[UMODEM_N_TRANSFER]; 157 struct usb_device *sc_udev; 158 struct lock sc_lock; 159 160 uint16_t sc_line; 161 162 uint8_t sc_lsr; /* local status register */ 163 uint8_t sc_msr; /* modem status register */ 164 uint8_t sc_ctrl_iface_no; 165 uint8_t sc_data_iface_no; 166 uint8_t sc_iface_index[2]; 167 uint8_t sc_cm_over_data; 168 uint8_t sc_cm_cap; /* CM capabilities */ 169 uint8_t sc_acm_cap; /* ACM capabilities */ 170 }; 171 172 static device_probe_t umodem_probe; 173 static device_attach_t umodem_attach; 174 static device_detach_t umodem_detach; 175 176 static usb_callback_t umodem_intr_callback; 177 static usb_callback_t umodem_write_callback; 178 static usb_callback_t umodem_read_callback; 179 180 static void umodem_start_read(struct ucom_softc *); 181 static void umodem_stop_read(struct ucom_softc *); 182 static void umodem_start_write(struct ucom_softc *); 183 static void umodem_stop_write(struct ucom_softc *); 184 static void umodem_get_caps(struct usb_attach_arg *, uint8_t *, uint8_t *); 185 static void umodem_cfg_get_status(struct ucom_softc *, uint8_t *, 186 uint8_t *); 187 static int umodem_pre_param(struct ucom_softc *, struct termios *); 188 static void umodem_cfg_param(struct ucom_softc *, struct termios *); 189 static int umodem_ioctl(struct ucom_softc *, uint32_t, caddr_t, int, 190 struct thread *); 191 static void umodem_cfg_set_dtr(struct ucom_softc *, uint8_t); 192 static void umodem_cfg_set_rts(struct ucom_softc *, uint8_t); 193 static void umodem_cfg_set_break(struct ucom_softc *, uint8_t); 194 static void *umodem_get_desc(struct usb_attach_arg *, uint8_t, uint8_t); 195 static usb_error_t umodem_set_comm_feature(struct usb_device *, uint8_t, 196 uint16_t, uint16_t); 197 static void umodem_poll(struct ucom_softc *ucom); 198 static void umodem_find_data_iface(struct usb_attach_arg *uaa, 199 uint8_t, uint8_t *, uint8_t *); 200 201 static const struct usb_config umodem_config[UMODEM_N_TRANSFER] = { 202 203 [UMODEM_BULK_WR] = { 204 .type = UE_BULK, 205 .endpoint = UE_ADDR_ANY, 206 .direction = UE_DIR_OUT, 207 .if_index = 0, 208 .bufsize = UMODEM_BUF_SIZE, 209 .flags = {.pipe_bof = 1,.force_short_xfer = 1,}, 210 .callback = &umodem_write_callback, 211 }, 212 213 [UMODEM_BULK_RD] = { 214 .type = UE_BULK, 215 .endpoint = UE_ADDR_ANY, 216 .direction = UE_DIR_IN, 217 .if_index = 0, 218 .bufsize = UMODEM_BUF_SIZE, 219 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,}, 220 .callback = &umodem_read_callback, 221 }, 222 223 [UMODEM_INTR_RD] = { 224 .type = UE_INTERRUPT, 225 .endpoint = UE_ADDR_ANY, 226 .direction = UE_DIR_IN, 227 .if_index = 1, 228 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,.no_pipe_ok = 1,}, 229 .bufsize = 0, /* use wMaxPacketSize */ 230 .callback = &umodem_intr_callback, 231 }, 232 }; 233 234 static const struct ucom_callback umodem_callback = { 235 .ucom_cfg_get_status = &umodem_cfg_get_status, 236 .ucom_cfg_set_dtr = &umodem_cfg_set_dtr, 237 .ucom_cfg_set_rts = &umodem_cfg_set_rts, 238 .ucom_cfg_set_break = &umodem_cfg_set_break, 239 .ucom_cfg_param = &umodem_cfg_param, 240 .ucom_pre_param = &umodem_pre_param, 241 .ucom_ioctl = &umodem_ioctl, 242 .ucom_start_read = &umodem_start_read, 243 .ucom_stop_read = &umodem_stop_read, 244 .ucom_start_write = &umodem_start_write, 245 .ucom_stop_write = &umodem_stop_write, 246 .ucom_poll = &umodem_poll, 247 }; 248 249 static device_method_t umodem_methods[] = { 250 DEVMETHOD(device_probe, umodem_probe), 251 DEVMETHOD(device_attach, umodem_attach), 252 DEVMETHOD(device_detach, umodem_detach), 253 DEVMETHOD_END 254 }; 255 256 static devclass_t umodem_devclass; 257 258 static driver_t umodem_driver = { 259 .name = "umodem", 260 .methods = umodem_methods, 261 .size = sizeof(struct umodem_softc), 262 }; 263 264 DRIVER_MODULE(umodem, uhub, umodem_driver, umodem_devclass, NULL, 0); 265 MODULE_DEPEND(umodem, ucom, 1, 1, 1); 266 MODULE_DEPEND(umodem, usb, 1, 1, 1); 267 MODULE_VERSION(umodem, UMODEM_MODVER); 268 269 static int 270 umodem_probe(device_t dev) 271 { 272 struct usb_attach_arg *uaa = device_get_ivars(dev); 273 int error; 274 275 DPRINTFN(11, "\n"); 276 277 if (uaa->usb_mode != USB_MODE_HOST) 278 return (ENXIO); 279 280 error = usbd_lookup_id_by_uaa(umodem_devs, sizeof(umodem_devs), uaa); 281 if (error) 282 return (error); 283 284 return (BUS_PROBE_GENERIC); 285 } 286 287 static int 288 umodem_attach(device_t dev) 289 { 290 struct usb_attach_arg *uaa = device_get_ivars(dev); 291 struct umodem_softc *sc = device_get_softc(dev); 292 struct usb_cdc_cm_descriptor *cmd; 293 struct usb_cdc_union_descriptor *cud; 294 uint8_t i; 295 int error; 296 297 device_set_usb_desc(dev); 298 lockinit(&sc->sc_lock, "umodem", 0, LK_CANRECURSE); 299 300 sc->sc_ctrl_iface_no = uaa->info.bIfaceNum; 301 sc->sc_iface_index[1] = uaa->info.bIfaceIndex; 302 sc->sc_udev = uaa->device; 303 304 umodem_get_caps(uaa, &sc->sc_cm_cap, &sc->sc_acm_cap); 305 306 /* get the data interface number */ 307 308 cmd = umodem_get_desc(uaa, UDESC_CS_INTERFACE, UDESCSUB_CDC_CM); 309 310 if ((cmd == NULL) || (cmd->bLength < sizeof(*cmd))) { 311 312 cud = usbd_find_descriptor(uaa->device, NULL, 313 uaa->info.bIfaceIndex, UDESC_CS_INTERFACE, 314 0 - 1, UDESCSUB_CDC_UNION, 0 - 1); 315 316 if ((cud == NULL) || (cud->bLength < sizeof(*cud))) { 317 DPRINTF("Missing descriptor. " 318 "Assuming data interface is next.\n"); 319 if (sc->sc_ctrl_iface_no == 0xFF) { 320 goto detach; 321 } else { 322 uint8_t class_match = 0; 323 324 /* set default interface number */ 325 sc->sc_data_iface_no = 0xFF; 326 327 /* try to find the data interface backwards */ 328 umodem_find_data_iface(uaa, 329 uaa->info.bIfaceIndex - 1, 330 &sc->sc_data_iface_no, &class_match); 331 332 /* try to find the data interface forwards */ 333 umodem_find_data_iface(uaa, 334 uaa->info.bIfaceIndex + 1, 335 &sc->sc_data_iface_no, &class_match); 336 337 /* check if nothing was found */ 338 if (sc->sc_data_iface_no == 0xFF) 339 goto detach; 340 } 341 } else { 342 sc->sc_data_iface_no = cud->bSlaveInterface[0]; 343 } 344 } else { 345 sc->sc_data_iface_no = cmd->bDataInterface; 346 } 347 348 device_printf(dev, "data interface %d, has %sCM over " 349 "data, has %sbreak\n", 350 sc->sc_data_iface_no, 351 sc->sc_cm_cap & USB_CDC_CM_OVER_DATA ? "" : "no ", 352 sc->sc_acm_cap & USB_CDC_ACM_HAS_BREAK ? "" : "no "); 353 354 /* get the data interface too */ 355 356 for (i = 0;; i++) { 357 struct usb_interface *iface; 358 struct usb_interface_descriptor *id; 359 360 iface = usbd_get_iface(uaa->device, i); 361 362 if (iface) { 363 364 id = usbd_get_interface_descriptor(iface); 365 366 if (id && (id->bInterfaceNumber == sc->sc_data_iface_no)) { 367 sc->sc_iface_index[0] = i; 368 usbd_set_parent_iface(uaa->device, i, uaa->info.bIfaceIndex); 369 break; 370 } 371 } else { 372 device_printf(dev, "no data interface\n"); 373 goto detach; 374 } 375 } 376 377 if (usb_test_quirk(uaa, UQ_ASSUME_CM_OVER_DATA)) { 378 sc->sc_cm_over_data = 1; 379 } else { 380 if (sc->sc_cm_cap & USB_CDC_CM_OVER_DATA) { 381 if (sc->sc_acm_cap & USB_CDC_ACM_HAS_FEATURE) { 382 383 error = umodem_set_comm_feature 384 (uaa->device, sc->sc_ctrl_iface_no, 385 UCDC_ABSTRACT_STATE, UCDC_DATA_MULTIPLEXED); 386 387 /* ignore any errors */ 388 } 389 sc->sc_cm_over_data = 1; 390 } 391 } 392 error = usbd_transfer_setup(uaa->device, 393 sc->sc_iface_index, sc->sc_xfer, 394 umodem_config, UMODEM_N_TRANSFER, 395 sc, &sc->sc_lock); 396 if (error) { 397 goto detach; 398 } 399 400 /* clear stall at first run */ 401 lockmgr(&sc->sc_lock, LK_EXCLUSIVE); 402 usbd_xfer_set_stall(sc->sc_xfer[UMODEM_BULK_WR]); 403 usbd_xfer_set_stall(sc->sc_xfer[UMODEM_BULK_RD]); 404 lockmgr(&sc->sc_lock, LK_RELEASE); 405 406 error = ucom_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc, 407 &umodem_callback, &sc->sc_lock); 408 if (error) { 409 goto detach; 410 } 411 ucom_set_pnpinfo_usb(&sc->sc_super_ucom, dev); 412 413 return (0); 414 415 detach: 416 umodem_detach(dev); 417 return (ENXIO); 418 } 419 420 static void 421 umodem_find_data_iface(struct usb_attach_arg *uaa, 422 uint8_t iface_index, uint8_t *p_data_no, uint8_t *p_match_class) 423 { 424 struct usb_interface_descriptor *id; 425 struct usb_interface *iface; 426 427 iface = usbd_get_iface(uaa->device, iface_index); 428 429 /* check for end of interfaces */ 430 if (iface == NULL) 431 return; 432 433 id = usbd_get_interface_descriptor(iface); 434 435 /* check for non-matching interface class */ 436 if (id->bInterfaceClass != UICLASS_CDC_DATA || 437 id->bInterfaceSubClass != UISUBCLASS_DATA) { 438 /* if we got a class match then return */ 439 if (*p_match_class) 440 return; 441 } else { 442 *p_match_class = 1; 443 } 444 445 DPRINTFN(11, "Match at index %u\n", iface_index); 446 447 *p_data_no = id->bInterfaceNumber; 448 } 449 450 static void 451 umodem_start_read(struct ucom_softc *ucom) 452 { 453 struct umodem_softc *sc = ucom->sc_parent; 454 455 /* start interrupt endpoint, if any */ 456 usbd_transfer_start(sc->sc_xfer[UMODEM_INTR_RD]); 457 458 /* start read endpoint */ 459 usbd_transfer_start(sc->sc_xfer[UMODEM_BULK_RD]); 460 } 461 462 static void 463 umodem_stop_read(struct ucom_softc *ucom) 464 { 465 struct umodem_softc *sc = ucom->sc_parent; 466 467 /* stop interrupt endpoint, if any */ 468 usbd_transfer_stop(sc->sc_xfer[UMODEM_INTR_RD]); 469 470 /* stop read endpoint */ 471 usbd_transfer_stop(sc->sc_xfer[UMODEM_BULK_RD]); 472 } 473 474 static void 475 umodem_start_write(struct ucom_softc *ucom) 476 { 477 struct umodem_softc *sc = ucom->sc_parent; 478 479 usbd_transfer_start(sc->sc_xfer[UMODEM_BULK_WR]); 480 } 481 482 static void 483 umodem_stop_write(struct ucom_softc *ucom) 484 { 485 struct umodem_softc *sc = ucom->sc_parent; 486 487 usbd_transfer_stop(sc->sc_xfer[UMODEM_BULK_WR]); 488 } 489 490 static void 491 umodem_get_caps(struct usb_attach_arg *uaa, uint8_t *cm, uint8_t *acm) 492 { 493 struct usb_cdc_cm_descriptor *cmd; 494 struct usb_cdc_acm_descriptor *cad; 495 496 cmd = umodem_get_desc(uaa, UDESC_CS_INTERFACE, UDESCSUB_CDC_CM); 497 if ((cmd == NULL) || (cmd->bLength < sizeof(*cmd))) { 498 DPRINTF("no CM desc (faking one)\n"); 499 *cm = USB_CDC_CM_DOES_CM | USB_CDC_CM_OVER_DATA; 500 } else 501 *cm = cmd->bmCapabilities; 502 503 cad = umodem_get_desc(uaa, UDESC_CS_INTERFACE, UDESCSUB_CDC_ACM); 504 if ((cad == NULL) || (cad->bLength < sizeof(*cad))) { 505 DPRINTF("no ACM desc\n"); 506 *acm = 0; 507 } else 508 *acm = cad->bmCapabilities; 509 } 510 511 static void 512 umodem_cfg_get_status(struct ucom_softc *ucom, uint8_t *lsr, uint8_t *msr) 513 { 514 struct umodem_softc *sc = ucom->sc_parent; 515 516 DPRINTF("\n"); 517 518 *lsr = sc->sc_lsr; 519 *msr = sc->sc_msr; 520 } 521 522 static int 523 umodem_pre_param(struct ucom_softc *ucom, struct termios *t) 524 { 525 return (0); /* we accept anything */ 526 } 527 528 static void 529 umodem_cfg_param(struct ucom_softc *ucom, struct termios *t) 530 { 531 struct umodem_softc *sc = ucom->sc_parent; 532 struct usb_cdc_line_state ls; 533 struct usb_device_request req; 534 535 DPRINTF("sc=%p\n", sc); 536 537 memset(&ls, 0, sizeof(ls)); 538 539 USETDW(ls.dwDTERate, t->c_ospeed); 540 541 ls.bCharFormat = (t->c_cflag & CSTOPB) ? 542 UCDC_STOP_BIT_2 : UCDC_STOP_BIT_1; 543 544 ls.bParityType = (t->c_cflag & PARENB) ? 545 ((t->c_cflag & PARODD) ? 546 UCDC_PARITY_ODD : UCDC_PARITY_EVEN) : UCDC_PARITY_NONE; 547 548 switch (t->c_cflag & CSIZE) { 549 case CS5: 550 ls.bDataBits = 5; 551 break; 552 case CS6: 553 ls.bDataBits = 6; 554 break; 555 case CS7: 556 ls.bDataBits = 7; 557 break; 558 case CS8: 559 ls.bDataBits = 8; 560 break; 561 } 562 563 DPRINTF("rate=%d fmt=%d parity=%d bits=%d\n", 564 UGETDW(ls.dwDTERate), ls.bCharFormat, 565 ls.bParityType, ls.bDataBits); 566 567 req.bmRequestType = UT_WRITE_CLASS_INTERFACE; 568 req.bRequest = UCDC_SET_LINE_CODING; 569 USETW(req.wValue, 0); 570 req.wIndex[0] = sc->sc_ctrl_iface_no; 571 req.wIndex[1] = 0; 572 USETW(req.wLength, sizeof(ls)); 573 574 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 575 &req, &ls, 0, 1000); 576 } 577 578 static int 579 umodem_ioctl(struct ucom_softc *ucom, uint32_t cmd, caddr_t data, 580 int flag, struct thread *td) 581 { 582 struct umodem_softc *sc = ucom->sc_parent; 583 int error = 0; 584 585 DPRINTF("cmd=0x%08x\n", cmd); 586 587 switch (cmd) { 588 case USB_GET_CM_OVER_DATA: 589 *(int *)data = sc->sc_cm_over_data; 590 break; 591 592 case USB_SET_CM_OVER_DATA: 593 if (*(int *)data != sc->sc_cm_over_data) { 594 /* XXX change it */ 595 } 596 break; 597 598 default: 599 DPRINTF("unknown\n"); 600 error = ENOIOCTL; 601 break; 602 } 603 604 return (error); 605 } 606 607 static void 608 umodem_cfg_set_dtr(struct ucom_softc *ucom, uint8_t onoff) 609 { 610 struct umodem_softc *sc = ucom->sc_parent; 611 struct usb_device_request req; 612 613 DPRINTF("onoff=%d\n", onoff); 614 615 if (onoff) 616 sc->sc_line |= UCDC_LINE_DTR; 617 else 618 sc->sc_line &= ~UCDC_LINE_DTR; 619 620 req.bmRequestType = UT_WRITE_CLASS_INTERFACE; 621 req.bRequest = UCDC_SET_CONTROL_LINE_STATE; 622 USETW(req.wValue, sc->sc_line); 623 req.wIndex[0] = sc->sc_ctrl_iface_no; 624 req.wIndex[1] = 0; 625 USETW(req.wLength, 0); 626 627 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 628 &req, NULL, 0, 1000); 629 } 630 631 static void 632 umodem_cfg_set_rts(struct ucom_softc *ucom, uint8_t onoff) 633 { 634 struct umodem_softc *sc = ucom->sc_parent; 635 struct usb_device_request req; 636 637 DPRINTF("onoff=%d\n", onoff); 638 639 if (onoff) 640 sc->sc_line |= UCDC_LINE_RTS; 641 else 642 sc->sc_line &= ~UCDC_LINE_RTS; 643 644 req.bmRequestType = UT_WRITE_CLASS_INTERFACE; 645 req.bRequest = UCDC_SET_CONTROL_LINE_STATE; 646 USETW(req.wValue, sc->sc_line); 647 req.wIndex[0] = sc->sc_ctrl_iface_no; 648 req.wIndex[1] = 0; 649 USETW(req.wLength, 0); 650 651 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 652 &req, NULL, 0, 1000); 653 } 654 655 static void 656 umodem_cfg_set_break(struct ucom_softc *ucom, uint8_t onoff) 657 { 658 struct umodem_softc *sc = ucom->sc_parent; 659 struct usb_device_request req; 660 uint16_t temp; 661 662 DPRINTF("onoff=%d\n", onoff); 663 664 if (sc->sc_acm_cap & USB_CDC_ACM_HAS_BREAK) { 665 666 temp = onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF; 667 668 req.bmRequestType = UT_WRITE_CLASS_INTERFACE; 669 req.bRequest = UCDC_SEND_BREAK; 670 USETW(req.wValue, temp); 671 req.wIndex[0] = sc->sc_ctrl_iface_no; 672 req.wIndex[1] = 0; 673 USETW(req.wLength, 0); 674 675 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 676 &req, NULL, 0, 1000); 677 } 678 } 679 680 static void 681 umodem_intr_callback(struct usb_xfer *xfer, usb_error_t error) 682 { 683 struct usb_cdc_notification pkt; 684 struct umodem_softc *sc = usbd_xfer_softc(xfer); 685 struct usb_page_cache *pc; 686 uint16_t wLen; 687 int actlen; 688 689 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL); 690 691 switch (USB_GET_STATE(xfer)) { 692 case USB_ST_TRANSFERRED: 693 694 if (actlen < 8) { 695 DPRINTF("received short packet, " 696 "%d bytes\n", actlen); 697 goto tr_setup; 698 } 699 if (actlen > sizeof(pkt)) { 700 DPRINTF("truncating message\n"); 701 actlen = sizeof(pkt); 702 } 703 pc = usbd_xfer_get_frame(xfer, 0); 704 usbd_copy_out(pc, 0, &pkt, actlen); 705 706 actlen -= 8; 707 708 wLen = UGETW(pkt.wLength); 709 if (actlen > wLen) { 710 actlen = wLen; 711 } 712 if (pkt.bmRequestType != UCDC_NOTIFICATION) { 713 DPRINTF("unknown message type, " 714 "0x%02x, on notify pipe!\n", 715 pkt.bmRequestType); 716 goto tr_setup; 717 } 718 switch (pkt.bNotification) { 719 case UCDC_N_SERIAL_STATE: 720 /* 721 * Set the serial state in ucom driver based on 722 * the bits from the notify message 723 */ 724 if (actlen < 2) { 725 DPRINTF("invalid notification " 726 "length, %d bytes!\n", actlen); 727 break; 728 } 729 DPRINTF("notify bytes = %02x%02x\n", 730 pkt.data[0], 731 pkt.data[1]); 732 733 /* Currently, lsr is always zero. */ 734 sc->sc_lsr = 0; 735 sc->sc_msr = 0; 736 737 if (pkt.data[0] & UCDC_N_SERIAL_RI) { 738 sc->sc_msr |= SER_RI; 739 } 740 if (pkt.data[0] & UCDC_N_SERIAL_DSR) { 741 sc->sc_msr |= SER_DSR; 742 } 743 if (pkt.data[0] & UCDC_N_SERIAL_DCD) { 744 sc->sc_msr |= SER_DCD; 745 } 746 ucom_status_change(&sc->sc_ucom); 747 break; 748 749 default: 750 DPRINTF("unknown notify message: 0x%02x\n", 751 pkt.bNotification); 752 break; 753 } 754 755 case USB_ST_SETUP: 756 tr_setup: 757 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer)); 758 usbd_transfer_submit(xfer); 759 return; 760 761 default: /* Error */ 762 if (error != USB_ERR_CANCELLED) { 763 /* try to clear stall first */ 764 usbd_xfer_set_stall(xfer); 765 goto tr_setup; 766 } 767 return; 768 769 } 770 } 771 772 static void 773 umodem_write_callback(struct usb_xfer *xfer, usb_error_t error) 774 { 775 struct umodem_softc *sc = usbd_xfer_softc(xfer); 776 struct usb_page_cache *pc; 777 uint32_t actlen; 778 779 switch (USB_GET_STATE(xfer)) { 780 case USB_ST_SETUP: 781 case USB_ST_TRANSFERRED: 782 tr_setup: 783 pc = usbd_xfer_get_frame(xfer, 0); 784 if (ucom_get_data(&sc->sc_ucom, pc, 0, 785 UMODEM_BUF_SIZE, &actlen)) { 786 787 usbd_xfer_set_frame_len(xfer, 0, actlen); 788 usbd_transfer_submit(xfer); 789 } 790 return; 791 792 default: /* Error */ 793 if (error != USB_ERR_CANCELLED) { 794 /* try to clear stall first */ 795 usbd_xfer_set_stall(xfer); 796 goto tr_setup; 797 } 798 return; 799 } 800 } 801 802 static void 803 umodem_read_callback(struct usb_xfer *xfer, usb_error_t error) 804 { 805 struct umodem_softc *sc = usbd_xfer_softc(xfer); 806 struct usb_page_cache *pc; 807 int actlen; 808 809 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL); 810 811 switch (USB_GET_STATE(xfer)) { 812 case USB_ST_TRANSFERRED: 813 814 DPRINTF("actlen=%d\n", actlen); 815 816 pc = usbd_xfer_get_frame(xfer, 0); 817 ucom_put_data(&sc->sc_ucom, pc, 0, actlen); 818 819 case USB_ST_SETUP: 820 tr_setup: 821 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer)); 822 usbd_transfer_submit(xfer); 823 return; 824 825 default: /* Error */ 826 if (error != USB_ERR_CANCELLED) { 827 /* try to clear stall first */ 828 usbd_xfer_set_stall(xfer); 829 goto tr_setup; 830 } 831 return; 832 } 833 } 834 835 static void * 836 umodem_get_desc(struct usb_attach_arg *uaa, uint8_t type, uint8_t subtype) 837 { 838 return (usbd_find_descriptor(uaa->device, NULL, uaa->info.bIfaceIndex, 839 type, 0 - 1, subtype, 0 - 1)); 840 } 841 842 static usb_error_t 843 umodem_set_comm_feature(struct usb_device *udev, uint8_t iface_no, 844 uint16_t feature, uint16_t state) 845 { 846 struct usb_device_request req; 847 struct usb_cdc_abstract_state ast; 848 849 DPRINTF("feature=%d state=%d\n", 850 feature, state); 851 852 req.bmRequestType = UT_WRITE_CLASS_INTERFACE; 853 req.bRequest = UCDC_SET_COMM_FEATURE; 854 USETW(req.wValue, feature); 855 req.wIndex[0] = iface_no; 856 req.wIndex[1] = 0; 857 USETW(req.wLength, UCDC_ABSTRACT_STATE_LENGTH); 858 USETW(ast.wState, state); 859 860 return (usbd_do_request(udev, NULL, &req, &ast)); 861 } 862 863 static int 864 umodem_detach(device_t dev) 865 { 866 struct umodem_softc *sc = device_get_softc(dev); 867 868 DPRINTF("sc=%p\n", sc); 869 870 ucom_detach(&sc->sc_super_ucom, &sc->sc_ucom); 871 usbd_transfer_unsetup(sc->sc_xfer, UMODEM_N_TRANSFER); 872 lockuninit(&sc->sc_lock); 873 874 return (0); 875 } 876 877 static void 878 umodem_poll(struct ucom_softc *ucom) 879 { 880 struct umodem_softc *sc = ucom->sc_parent; 881 usbd_transfer_poll(sc->sc_xfer, UMODEM_N_TRANSFER); 882 } 883