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