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