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 #include <sys/serial.h> 96 97 #include <bus/u4b/usb.h> 98 #include <bus/u4b/usbdi.h> 99 #include <bus/u4b/usbdi_util.h> 100 #include <bus/u4b/usbhid.h> 101 #include <bus/u4b/usb_cdc.h> 102 #include "usbdevs.h" 103 104 #include <bus/u4b/usb_ioctl.h> 105 106 #define USB_DEBUG_VAR umodem_debug 107 #include <bus/u4b/usb_debug.h> 108 #include <bus/u4b/usb_process.h> 109 #include <bus/u4b/quirk/usb_quirk.h> 110 111 #include <bus/u4b/serial/usb_serial.h> 112 113 #ifdef USB_DEBUG 114 static int umodem_debug = 0; 115 116 static SYSCTL_NODE(_hw_usb, OID_AUTO, umodem, CTLFLAG_RW, 0, "USB umodem"); 117 SYSCTL_INT(_hw_usb_umodem, OID_AUTO, debug, CTLFLAG_RW, 118 &umodem_debug, 0, "Debug level"); 119 #endif 120 121 static const STRUCT_USB_HOST_ID umodem_devs[] = { 122 /* Generic Modem class match */ 123 {USB_IFACE_CLASS(UICLASS_CDC), 124 USB_IFACE_SUBCLASS(UISUBCLASS_ABSTRACT_CONTROL_MODEL), 125 USB_IFACE_PROTOCOL(UIPROTO_CDC_AT)}, 126 /* Huawei Modem class match */ 127 {USB_VENDOR(USB_VENDOR_HUAWEI), 128 USB_IFACE_CLASS(UICLASS_CDC), 129 USB_IFACE_SUBCLASS(UISUBCLASS_ABSTRACT_CONTROL_MODEL), 130 USB_IFACE_PROTOCOL(0xFF)}, 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 lock sc_lock; 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 DEVMETHOD_END 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, NULL); 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 if (error) 284 return (error); 285 286 return (BUS_PROBE_GENERIC); 287 } 288 289 static int 290 umodem_attach(device_t dev) 291 { 292 struct usb_attach_arg *uaa = device_get_ivars(dev); 293 struct umodem_softc *sc = device_get_softc(dev); 294 struct usb_cdc_cm_descriptor *cmd; 295 struct usb_cdc_union_descriptor *cud; 296 uint8_t i; 297 int error; 298 299 device_set_usb_desc(dev); 300 lockinit(&sc->sc_lock, "umodem", 0, LK_CANRECURSE); 301 302 sc->sc_ctrl_iface_no = uaa->info.bIfaceNum; 303 sc->sc_iface_index[1] = uaa->info.bIfaceIndex; 304 sc->sc_udev = uaa->device; 305 306 umodem_get_caps(uaa, &sc->sc_cm_cap, &sc->sc_acm_cap); 307 308 /* get the data interface number */ 309 310 cmd = umodem_get_desc(uaa, UDESC_CS_INTERFACE, UDESCSUB_CDC_CM); 311 312 if ((cmd == NULL) || (cmd->bLength < sizeof(*cmd))) { 313 314 cud = usbd_find_descriptor(uaa->device, NULL, 315 uaa->info.bIfaceIndex, UDESC_CS_INTERFACE, 316 0 - 1, UDESCSUB_CDC_UNION, 0 - 1); 317 318 if ((cud == NULL) || (cud->bLength < sizeof(*cud))) { 319 DPRINTF("Missing descriptor. " 320 "Assuming data interface is next.\n"); 321 if (sc->sc_ctrl_iface_no == 0xFF) { 322 goto detach; 323 } else { 324 uint8_t class_match = 0; 325 326 /* set default interface number */ 327 sc->sc_data_iface_no = 0xFF; 328 329 /* try to find the data interface backwards */ 330 umodem_find_data_iface(uaa, 331 uaa->info.bIfaceIndex - 1, 332 &sc->sc_data_iface_no, &class_match); 333 334 /* try to find the data interface forwards */ 335 umodem_find_data_iface(uaa, 336 uaa->info.bIfaceIndex + 1, 337 &sc->sc_data_iface_no, &class_match); 338 339 /* check if nothing was found */ 340 if (sc->sc_data_iface_no == 0xFF) 341 goto detach; 342 } 343 } else { 344 sc->sc_data_iface_no = cud->bSlaveInterface[0]; 345 } 346 } else { 347 sc->sc_data_iface_no = cmd->bDataInterface; 348 } 349 350 device_printf(dev, "data interface %d, has %sCM over " 351 "data, has %sbreak\n", 352 sc->sc_data_iface_no, 353 sc->sc_cm_cap & USB_CDC_CM_OVER_DATA ? "" : "no ", 354 sc->sc_acm_cap & USB_CDC_ACM_HAS_BREAK ? "" : "no "); 355 356 /* get the data interface too */ 357 358 for (i = 0;; i++) { 359 struct usb_interface *iface; 360 struct usb_interface_descriptor *id; 361 362 iface = usbd_get_iface(uaa->device, i); 363 364 if (iface) { 365 366 id = usbd_get_interface_descriptor(iface); 367 368 if (id && (id->bInterfaceNumber == sc->sc_data_iface_no)) { 369 sc->sc_iface_index[0] = i; 370 usbd_set_parent_iface(uaa->device, i, uaa->info.bIfaceIndex); 371 break; 372 } 373 } else { 374 device_printf(dev, "no data interface\n"); 375 goto detach; 376 } 377 } 378 379 if (usb_test_quirk(uaa, UQ_ASSUME_CM_OVER_DATA)) { 380 sc->sc_cm_over_data = 1; 381 } else { 382 if (sc->sc_cm_cap & USB_CDC_CM_OVER_DATA) { 383 if (sc->sc_acm_cap & USB_CDC_ACM_HAS_FEATURE) { 384 385 error = umodem_set_comm_feature 386 (uaa->device, sc->sc_ctrl_iface_no, 387 UCDC_ABSTRACT_STATE, UCDC_DATA_MULTIPLEXED); 388 389 /* ignore any errors */ 390 } 391 sc->sc_cm_over_data = 1; 392 } 393 } 394 error = usbd_transfer_setup(uaa->device, 395 sc->sc_iface_index, sc->sc_xfer, 396 umodem_config, UMODEM_N_TRANSFER, 397 sc, &sc->sc_lock); 398 if (error) { 399 goto detach; 400 } 401 402 /* clear stall at first run */ 403 lockmgr(&sc->sc_lock, LK_EXCLUSIVE); 404 usbd_xfer_set_stall(sc->sc_xfer[UMODEM_BULK_WR]); 405 usbd_xfer_set_stall(sc->sc_xfer[UMODEM_BULK_RD]); 406 lockmgr(&sc->sc_lock, LK_RELEASE); 407 408 error = ucom_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc, 409 &umodem_callback, &sc->sc_lock); 410 if (error) { 411 goto detach; 412 } 413 ucom_set_pnpinfo_usb(&sc->sc_super_ucom, dev); 414 415 return (0); 416 417 detach: 418 umodem_detach(dev); 419 return (ENXIO); 420 } 421 422 static void 423 umodem_find_data_iface(struct usb_attach_arg *uaa, 424 uint8_t iface_index, uint8_t *p_data_no, uint8_t *p_match_class) 425 { 426 struct usb_interface_descriptor *id; 427 struct usb_interface *iface; 428 429 iface = usbd_get_iface(uaa->device, iface_index); 430 431 /* check for end of interfaces */ 432 if (iface == NULL) 433 return; 434 435 id = usbd_get_interface_descriptor(iface); 436 437 /* check for non-matching interface class */ 438 if (id->bInterfaceClass != UICLASS_CDC_DATA || 439 id->bInterfaceSubClass != UISUBCLASS_DATA) { 440 /* if we got a class match then return */ 441 if (*p_match_class) 442 return; 443 } else { 444 *p_match_class = 1; 445 } 446 447 DPRINTFN(11, "Match at index %u\n", iface_index); 448 449 *p_data_no = id->bInterfaceNumber; 450 } 451 452 static void 453 umodem_start_read(struct ucom_softc *ucom) 454 { 455 struct umodem_softc *sc = ucom->sc_parent; 456 457 /* start interrupt endpoint, if any */ 458 usbd_transfer_start(sc->sc_xfer[UMODEM_INTR_RD]); 459 460 /* start read endpoint */ 461 usbd_transfer_start(sc->sc_xfer[UMODEM_BULK_RD]); 462 } 463 464 static void 465 umodem_stop_read(struct ucom_softc *ucom) 466 { 467 struct umodem_softc *sc = ucom->sc_parent; 468 469 /* stop interrupt endpoint, if any */ 470 usbd_transfer_stop(sc->sc_xfer[UMODEM_INTR_RD]); 471 472 /* stop read endpoint */ 473 usbd_transfer_stop(sc->sc_xfer[UMODEM_BULK_RD]); 474 } 475 476 static void 477 umodem_start_write(struct ucom_softc *ucom) 478 { 479 struct umodem_softc *sc = ucom->sc_parent; 480 481 usbd_transfer_start(sc->sc_xfer[UMODEM_BULK_WR]); 482 } 483 484 static void 485 umodem_stop_write(struct ucom_softc *ucom) 486 { 487 struct umodem_softc *sc = ucom->sc_parent; 488 489 usbd_transfer_stop(sc->sc_xfer[UMODEM_BULK_WR]); 490 } 491 492 static void 493 umodem_get_caps(struct usb_attach_arg *uaa, uint8_t *cm, uint8_t *acm) 494 { 495 struct usb_cdc_cm_descriptor *cmd; 496 struct usb_cdc_acm_descriptor *cad; 497 498 cmd = umodem_get_desc(uaa, UDESC_CS_INTERFACE, UDESCSUB_CDC_CM); 499 if ((cmd == NULL) || (cmd->bLength < sizeof(*cmd))) { 500 DPRINTF("no CM desc (faking one)\n"); 501 *cm = USB_CDC_CM_DOES_CM | USB_CDC_CM_OVER_DATA; 502 } else 503 *cm = cmd->bmCapabilities; 504 505 cad = umodem_get_desc(uaa, UDESC_CS_INTERFACE, UDESCSUB_CDC_ACM); 506 if ((cad == NULL) || (cad->bLength < sizeof(*cad))) { 507 DPRINTF("no ACM desc\n"); 508 *acm = 0; 509 } else 510 *acm = cad->bmCapabilities; 511 } 512 513 static void 514 umodem_cfg_get_status(struct ucom_softc *ucom, uint8_t *lsr, uint8_t *msr) 515 { 516 struct umodem_softc *sc = ucom->sc_parent; 517 518 DPRINTF("\n"); 519 520 *lsr = sc->sc_lsr; 521 *msr = sc->sc_msr; 522 } 523 524 static int 525 umodem_pre_param(struct ucom_softc *ucom, struct termios *t) 526 { 527 return (0); /* we accept anything */ 528 } 529 530 static void 531 umodem_cfg_param(struct ucom_softc *ucom, struct termios *t) 532 { 533 struct umodem_softc *sc = ucom->sc_parent; 534 struct usb_cdc_line_state ls; 535 struct usb_device_request req; 536 537 DPRINTF("sc=%p\n", sc); 538 539 memset(&ls, 0, sizeof(ls)); 540 541 USETDW(ls.dwDTERate, t->c_ospeed); 542 543 ls.bCharFormat = (t->c_cflag & CSTOPB) ? 544 UCDC_STOP_BIT_2 : UCDC_STOP_BIT_1; 545 546 ls.bParityType = (t->c_cflag & PARENB) ? 547 ((t->c_cflag & PARODD) ? 548 UCDC_PARITY_ODD : UCDC_PARITY_EVEN) : UCDC_PARITY_NONE; 549 550 switch (t->c_cflag & CSIZE) { 551 case CS5: 552 ls.bDataBits = 5; 553 break; 554 case CS6: 555 ls.bDataBits = 6; 556 break; 557 case CS7: 558 ls.bDataBits = 7; 559 break; 560 case CS8: 561 ls.bDataBits = 8; 562 break; 563 } 564 565 DPRINTF("rate=%d fmt=%d parity=%d bits=%d\n", 566 UGETDW(ls.dwDTERate), ls.bCharFormat, 567 ls.bParityType, ls.bDataBits); 568 569 req.bmRequestType = UT_WRITE_CLASS_INTERFACE; 570 req.bRequest = UCDC_SET_LINE_CODING; 571 USETW(req.wValue, 0); 572 req.wIndex[0] = sc->sc_ctrl_iface_no; 573 req.wIndex[1] = 0; 574 USETW(req.wLength, sizeof(ls)); 575 576 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 577 &req, &ls, 0, 1000); 578 } 579 580 static int 581 umodem_ioctl(struct ucom_softc *ucom, uint32_t cmd, caddr_t data, 582 int flag, struct thread *td) 583 { 584 struct umodem_softc *sc = ucom->sc_parent; 585 int error = 0; 586 587 DPRINTF("cmd=0x%08x\n", cmd); 588 589 switch (cmd) { 590 case USB_GET_CM_OVER_DATA: 591 *(int *)data = sc->sc_cm_over_data; 592 break; 593 594 case USB_SET_CM_OVER_DATA: 595 if (*(int *)data != sc->sc_cm_over_data) { 596 /* XXX change it */ 597 } 598 break; 599 600 default: 601 DPRINTF("unknown\n"); 602 error = ENOIOCTL; 603 break; 604 } 605 606 return (error); 607 } 608 609 static void 610 umodem_cfg_set_dtr(struct ucom_softc *ucom, uint8_t onoff) 611 { 612 struct umodem_softc *sc = ucom->sc_parent; 613 struct usb_device_request req; 614 615 DPRINTF("onoff=%d\n", onoff); 616 617 if (onoff) 618 sc->sc_line |= UCDC_LINE_DTR; 619 else 620 sc->sc_line &= ~UCDC_LINE_DTR; 621 622 req.bmRequestType = UT_WRITE_CLASS_INTERFACE; 623 req.bRequest = UCDC_SET_CONTROL_LINE_STATE; 624 USETW(req.wValue, sc->sc_line); 625 req.wIndex[0] = sc->sc_ctrl_iface_no; 626 req.wIndex[1] = 0; 627 USETW(req.wLength, 0); 628 629 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 630 &req, NULL, 0, 1000); 631 } 632 633 static void 634 umodem_cfg_set_rts(struct ucom_softc *ucom, uint8_t onoff) 635 { 636 struct umodem_softc *sc = ucom->sc_parent; 637 struct usb_device_request req; 638 639 DPRINTF("onoff=%d\n", onoff); 640 641 if (onoff) 642 sc->sc_line |= UCDC_LINE_RTS; 643 else 644 sc->sc_line &= ~UCDC_LINE_RTS; 645 646 req.bmRequestType = UT_WRITE_CLASS_INTERFACE; 647 req.bRequest = UCDC_SET_CONTROL_LINE_STATE; 648 USETW(req.wValue, sc->sc_line); 649 req.wIndex[0] = sc->sc_ctrl_iface_no; 650 req.wIndex[1] = 0; 651 USETW(req.wLength, 0); 652 653 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 654 &req, NULL, 0, 1000); 655 } 656 657 static void 658 umodem_cfg_set_break(struct ucom_softc *ucom, uint8_t onoff) 659 { 660 struct umodem_softc *sc = ucom->sc_parent; 661 struct usb_device_request req; 662 uint16_t temp; 663 664 DPRINTF("onoff=%d\n", onoff); 665 666 if (sc->sc_acm_cap & USB_CDC_ACM_HAS_BREAK) { 667 668 temp = onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF; 669 670 req.bmRequestType = UT_WRITE_CLASS_INTERFACE; 671 req.bRequest = UCDC_SEND_BREAK; 672 USETW(req.wValue, temp); 673 req.wIndex[0] = sc->sc_ctrl_iface_no; 674 req.wIndex[1] = 0; 675 USETW(req.wLength, 0); 676 677 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 678 &req, NULL, 0, 1000); 679 } 680 } 681 682 static void 683 umodem_intr_callback(struct usb_xfer *xfer, usb_error_t error) 684 { 685 struct usb_cdc_notification pkt; 686 struct umodem_softc *sc = usbd_xfer_softc(xfer); 687 struct usb_page_cache *pc; 688 uint16_t wLen; 689 int actlen; 690 691 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL); 692 693 switch (USB_GET_STATE(xfer)) { 694 case USB_ST_TRANSFERRED: 695 696 if (actlen < 8) { 697 DPRINTF("received short packet, " 698 "%d bytes\n", actlen); 699 goto tr_setup; 700 } 701 if (actlen > sizeof(pkt)) { 702 DPRINTF("truncating message\n"); 703 actlen = sizeof(pkt); 704 } 705 pc = usbd_xfer_get_frame(xfer, 0); 706 usbd_copy_out(pc, 0, &pkt, actlen); 707 708 actlen -= 8; 709 710 wLen = UGETW(pkt.wLength); 711 if (actlen > wLen) { 712 actlen = wLen; 713 } 714 if (pkt.bmRequestType != UCDC_NOTIFICATION) { 715 DPRINTF("unknown message type, " 716 "0x%02x, on notify pipe!\n", 717 pkt.bmRequestType); 718 goto tr_setup; 719 } 720 switch (pkt.bNotification) { 721 case UCDC_N_SERIAL_STATE: 722 /* 723 * Set the serial state in ucom driver based on 724 * the bits from the notify message 725 */ 726 if (actlen < 2) { 727 DPRINTF("invalid notification " 728 "length, %d bytes!\n", actlen); 729 break; 730 } 731 DPRINTF("notify bytes = %02x%02x\n", 732 pkt.data[0], 733 pkt.data[1]); 734 735 /* Currently, lsr is always zero. */ 736 sc->sc_lsr = 0; 737 sc->sc_msr = 0; 738 739 if (pkt.data[0] & UCDC_N_SERIAL_RI) { 740 sc->sc_msr |= SER_RI; 741 } 742 if (pkt.data[0] & UCDC_N_SERIAL_DSR) { 743 sc->sc_msr |= SER_DSR; 744 } 745 if (pkt.data[0] & UCDC_N_SERIAL_DCD) { 746 sc->sc_msr |= SER_DCD; 747 } 748 ucom_status_change(&sc->sc_ucom); 749 break; 750 751 default: 752 DPRINTF("unknown notify message: 0x%02x\n", 753 pkt.bNotification); 754 break; 755 } 756 757 case USB_ST_SETUP: 758 tr_setup: 759 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer)); 760 usbd_transfer_submit(xfer); 761 return; 762 763 default: /* Error */ 764 if (error != USB_ERR_CANCELLED) { 765 /* try to clear stall first */ 766 usbd_xfer_set_stall(xfer); 767 goto tr_setup; 768 } 769 return; 770 771 } 772 } 773 774 static void 775 umodem_write_callback(struct usb_xfer *xfer, usb_error_t error) 776 { 777 struct umodem_softc *sc = usbd_xfer_softc(xfer); 778 struct usb_page_cache *pc; 779 uint32_t actlen; 780 781 switch (USB_GET_STATE(xfer)) { 782 case USB_ST_SETUP: 783 case USB_ST_TRANSFERRED: 784 tr_setup: 785 pc = usbd_xfer_get_frame(xfer, 0); 786 if (ucom_get_data(&sc->sc_ucom, pc, 0, 787 UMODEM_BUF_SIZE, &actlen)) { 788 789 usbd_xfer_set_frame_len(xfer, 0, actlen); 790 usbd_transfer_submit(xfer); 791 } 792 return; 793 794 default: /* Error */ 795 if (error != USB_ERR_CANCELLED) { 796 /* try to clear stall first */ 797 usbd_xfer_set_stall(xfer); 798 goto tr_setup; 799 } 800 return; 801 } 802 } 803 804 static void 805 umodem_read_callback(struct usb_xfer *xfer, usb_error_t error) 806 { 807 struct umodem_softc *sc = usbd_xfer_softc(xfer); 808 struct usb_page_cache *pc; 809 int actlen; 810 811 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL); 812 813 switch (USB_GET_STATE(xfer)) { 814 case USB_ST_TRANSFERRED: 815 816 DPRINTF("actlen=%d\n", actlen); 817 818 pc = usbd_xfer_get_frame(xfer, 0); 819 ucom_put_data(&sc->sc_ucom, pc, 0, actlen); 820 821 case USB_ST_SETUP: 822 tr_setup: 823 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer)); 824 usbd_transfer_submit(xfer); 825 return; 826 827 default: /* Error */ 828 if (error != USB_ERR_CANCELLED) { 829 /* try to clear stall first */ 830 usbd_xfer_set_stall(xfer); 831 goto tr_setup; 832 } 833 return; 834 } 835 } 836 837 static void * 838 umodem_get_desc(struct usb_attach_arg *uaa, uint8_t type, uint8_t subtype) 839 { 840 return (usbd_find_descriptor(uaa->device, NULL, uaa->info.bIfaceIndex, 841 type, 0 - 1, subtype, 0 - 1)); 842 } 843 844 static usb_error_t 845 umodem_set_comm_feature(struct usb_device *udev, uint8_t iface_no, 846 uint16_t feature, uint16_t state) 847 { 848 struct usb_device_request req; 849 struct usb_cdc_abstract_state ast; 850 851 DPRINTF("feature=%d state=%d\n", 852 feature, state); 853 854 req.bmRequestType = UT_WRITE_CLASS_INTERFACE; 855 req.bRequest = UCDC_SET_COMM_FEATURE; 856 USETW(req.wValue, feature); 857 req.wIndex[0] = iface_no; 858 req.wIndex[1] = 0; 859 USETW(req.wLength, UCDC_ABSTRACT_STATE_LENGTH); 860 USETW(ast.wState, state); 861 862 return (usbd_do_request(udev, NULL, &req, &ast)); 863 } 864 865 static int 866 umodem_detach(device_t dev) 867 { 868 struct umodem_softc *sc = device_get_softc(dev); 869 870 DPRINTF("sc=%p\n", sc); 871 872 ucom_detach(&sc->sc_super_ucom, &sc->sc_ucom); 873 usbd_transfer_unsetup(sc->sc_xfer, UMODEM_N_TRANSFER); 874 lockuninit(&sc->sc_lock); 875 876 return (0); 877 } 878 879 static void 880 umodem_poll(struct ucom_softc *ucom) 881 { 882 struct umodem_softc *sc = ucom->sc_parent; 883 usbd_transfer_poll(sc->sc_xfer, UMODEM_N_TRANSFER); 884 } 885