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 }; 154 155 /* 156 * As speeds for umodem devices increase, these numbers will need to 157 * be increased. They should be good for G3 speeds and below. 158 * 159 * TODO: The TTY buffers should be increased! 160 */ 161 #define UMODEM_BUF_SIZE 1024 162 163 enum { 164 UMODEM_BULK_WR, 165 UMODEM_BULK_RD, 166 UMODEM_INTR_WR, 167 UMODEM_INTR_RD, 168 UMODEM_N_TRANSFER, 169 }; 170 171 #define UMODEM_MODVER 1 /* module version */ 172 173 struct umodem_softc { 174 struct ucom_super_softc sc_super_ucom; 175 struct ucom_softc sc_ucom; 176 177 struct usb_xfer *sc_xfer[UMODEM_N_TRANSFER]; 178 struct usb_device *sc_udev; 179 struct mtx sc_mtx; 180 181 uint16_t sc_line; 182 183 uint8_t sc_lsr; /* local status register */ 184 uint8_t sc_msr; /* modem status register */ 185 uint8_t sc_ctrl_iface_no; 186 uint8_t sc_data_iface_no; 187 uint8_t sc_iface_index[2]; 188 uint8_t sc_cm_over_data; 189 uint8_t sc_cm_cap; /* CM capabilities */ 190 uint8_t sc_acm_cap; /* ACM capabilities */ 191 uint8_t sc_line_coding[32]; /* used in USB device mode */ 192 uint8_t sc_abstract_state[32]; /* used in USB device mode */ 193 }; 194 195 static device_probe_t umodem_probe; 196 static device_attach_t umodem_attach; 197 static device_detach_t umodem_detach; 198 static usb_handle_request_t umodem_handle_request; 199 200 static void umodem_free_softc(struct umodem_softc *); 201 202 static usb_callback_t umodem_intr_read_callback; 203 static usb_callback_t umodem_intr_write_callback; 204 static usb_callback_t umodem_write_callback; 205 static usb_callback_t umodem_read_callback; 206 207 static void umodem_free(struct ucom_softc *); 208 static void umodem_start_read(struct ucom_softc *); 209 static void umodem_stop_read(struct ucom_softc *); 210 static void umodem_start_write(struct ucom_softc *); 211 static void umodem_stop_write(struct ucom_softc *); 212 static void umodem_get_caps(struct usb_attach_arg *, uint8_t *, uint8_t *); 213 static void umodem_cfg_get_status(struct ucom_softc *, uint8_t *, 214 uint8_t *); 215 static int umodem_pre_param(struct ucom_softc *, struct termios *); 216 static void umodem_cfg_param(struct ucom_softc *, struct termios *); 217 static int umodem_ioctl(struct ucom_softc *, uint32_t, caddr_t, int, 218 struct thread *); 219 static void umodem_cfg_set_dtr(struct ucom_softc *, uint8_t); 220 static void umodem_cfg_set_rts(struct ucom_softc *, uint8_t); 221 static void umodem_cfg_set_break(struct ucom_softc *, uint8_t); 222 static void *umodem_get_desc(struct usb_attach_arg *, uint8_t, uint8_t); 223 static usb_error_t umodem_set_comm_feature(struct usb_device *, uint8_t, 224 uint16_t, uint16_t); 225 static void umodem_poll(struct ucom_softc *ucom); 226 static void umodem_find_data_iface(struct usb_attach_arg *uaa, 227 uint8_t, uint8_t *, uint8_t *); 228 229 static const struct usb_config umodem_config[UMODEM_N_TRANSFER] = { 230 [UMODEM_BULK_WR] = { 231 .type = UE_BULK, 232 .endpoint = UE_ADDR_ANY, 233 .direction = UE_DIR_TX, 234 .if_index = 0, 235 .bufsize = UMODEM_BUF_SIZE, 236 .flags = {.pipe_bof = 1,.force_short_xfer = 1,}, 237 .callback = &umodem_write_callback, 238 .usb_mode = USB_MODE_DUAL, 239 }, 240 241 [UMODEM_BULK_RD] = { 242 .type = UE_BULK, 243 .endpoint = UE_ADDR_ANY, 244 .direction = UE_DIR_RX, 245 .if_index = 0, 246 .bufsize = UMODEM_BUF_SIZE, 247 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,}, 248 .callback = &umodem_read_callback, 249 .usb_mode = USB_MODE_DUAL, 250 }, 251 252 [UMODEM_INTR_WR] = { 253 .type = UE_INTERRUPT, 254 .endpoint = UE_ADDR_ANY, 255 .direction = UE_DIR_TX, 256 .if_index = 1, 257 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,.no_pipe_ok = 1,}, 258 .bufsize = 0, /* use wMaxPacketSize */ 259 .callback = &umodem_intr_write_callback, 260 .usb_mode = USB_MODE_DEVICE, 261 }, 262 263 [UMODEM_INTR_RD] = { 264 .type = UE_INTERRUPT, 265 .endpoint = UE_ADDR_ANY, 266 .direction = UE_DIR_RX, 267 .if_index = 1, 268 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,.no_pipe_ok = 1,}, 269 .bufsize = 0, /* use wMaxPacketSize */ 270 .callback = &umodem_intr_read_callback, 271 .usb_mode = USB_MODE_HOST, 272 }, 273 }; 274 275 static const struct ucom_callback umodem_callback = { 276 .ucom_cfg_get_status = &umodem_cfg_get_status, 277 .ucom_cfg_set_dtr = &umodem_cfg_set_dtr, 278 .ucom_cfg_set_rts = &umodem_cfg_set_rts, 279 .ucom_cfg_set_break = &umodem_cfg_set_break, 280 .ucom_cfg_param = &umodem_cfg_param, 281 .ucom_pre_param = &umodem_pre_param, 282 .ucom_ioctl = &umodem_ioctl, 283 .ucom_start_read = &umodem_start_read, 284 .ucom_stop_read = &umodem_stop_read, 285 .ucom_start_write = &umodem_start_write, 286 .ucom_stop_write = &umodem_stop_write, 287 .ucom_poll = &umodem_poll, 288 .ucom_free = &umodem_free, 289 }; 290 291 static device_method_t umodem_methods[] = { 292 /* USB interface */ 293 DEVMETHOD(usb_handle_request, umodem_handle_request), 294 295 /* Device interface */ 296 DEVMETHOD(device_probe, umodem_probe), 297 DEVMETHOD(device_attach, umodem_attach), 298 DEVMETHOD(device_detach, umodem_detach), 299 DEVMETHOD_END 300 }; 301 302 static devclass_t umodem_devclass; 303 304 static driver_t umodem_driver = { 305 .name = "umodem", 306 .methods = umodem_methods, 307 .size = sizeof(struct umodem_softc), 308 }; 309 310 DRIVER_MODULE(umodem, uhub, umodem_driver, umodem_devclass, NULL, 0); 311 MODULE_DEPEND(umodem, ucom, 1, 1, 1); 312 MODULE_DEPEND(umodem, usb, 1, 1, 1); 313 MODULE_VERSION(umodem, UMODEM_MODVER); 314 USB_PNP_DUAL_INFO(umodem_dual_devs); 315 USB_PNP_HOST_INFO(umodem_host_devs); 316 317 static int 318 umodem_probe(device_t dev) 319 { 320 struct usb_attach_arg *uaa = device_get_ivars(dev); 321 int error; 322 323 DPRINTFN(11, "\n"); 324 325 error = usbd_lookup_id_by_uaa(umodem_host_devs, 326 sizeof(umodem_host_devs), uaa); 327 if (error) { 328 error = usbd_lookup_id_by_uaa(umodem_dual_devs, 329 sizeof(umodem_dual_devs), uaa); 330 if (error) 331 return (error); 332 } 333 return (BUS_PROBE_GENERIC); 334 } 335 336 static int 337 umodem_attach(device_t dev) 338 { 339 struct usb_attach_arg *uaa = device_get_ivars(dev); 340 struct umodem_softc *sc = device_get_softc(dev); 341 struct usb_cdc_cm_descriptor *cmd; 342 struct usb_cdc_union_descriptor *cud; 343 uint8_t i; 344 int error; 345 346 device_set_usb_desc(dev); 347 mtx_init(&sc->sc_mtx, "umodem", NULL, MTX_DEF); 348 ucom_ref(&sc->sc_super_ucom); 349 350 sc->sc_ctrl_iface_no = uaa->info.bIfaceNum; 351 sc->sc_iface_index[1] = uaa->info.bIfaceIndex; 352 sc->sc_udev = uaa->device; 353 354 umodem_get_caps(uaa, &sc->sc_cm_cap, &sc->sc_acm_cap); 355 356 /* get the data interface number */ 357 358 cmd = umodem_get_desc(uaa, UDESC_CS_INTERFACE, UDESCSUB_CDC_CM); 359 360 if ((cmd == NULL) || (cmd->bLength < sizeof(*cmd))) { 361 cud = usbd_find_descriptor(uaa->device, NULL, 362 uaa->info.bIfaceIndex, UDESC_CS_INTERFACE, 363 0xFF, UDESCSUB_CDC_UNION, 0xFF); 364 365 if ((cud == NULL) || (cud->bLength < sizeof(*cud))) { 366 DPRINTF("Missing descriptor. " 367 "Assuming data interface is next.\n"); 368 if (sc->sc_ctrl_iface_no == 0xFF) { 369 goto detach; 370 } else { 371 uint8_t class_match = 0; 372 373 /* set default interface number */ 374 sc->sc_data_iface_no = 0xFF; 375 376 /* try to find the data interface backwards */ 377 umodem_find_data_iface(uaa, 378 uaa->info.bIfaceIndex - 1, 379 &sc->sc_data_iface_no, &class_match); 380 381 /* try to find the data interface forwards */ 382 umodem_find_data_iface(uaa, 383 uaa->info.bIfaceIndex + 1, 384 &sc->sc_data_iface_no, &class_match); 385 386 /* check if nothing was found */ 387 if (sc->sc_data_iface_no == 0xFF) 388 goto detach; 389 } 390 } else { 391 sc->sc_data_iface_no = cud->bSlaveInterface[0]; 392 } 393 } else { 394 sc->sc_data_iface_no = cmd->bDataInterface; 395 } 396 397 device_printf(dev, "data interface %d, has %sCM over " 398 "data, has %sbreak\n", 399 sc->sc_data_iface_no, 400 sc->sc_cm_cap & USB_CDC_CM_OVER_DATA ? "" : "no ", 401 sc->sc_acm_cap & USB_CDC_ACM_HAS_BREAK ? "" : "no "); 402 403 /* get the data interface too */ 404 405 for (i = 0;; i++) { 406 struct usb_interface *iface; 407 struct usb_interface_descriptor *id; 408 409 iface = usbd_get_iface(uaa->device, i); 410 411 if (iface) { 412 id = usbd_get_interface_descriptor(iface); 413 414 if (id && (id->bInterfaceNumber == sc->sc_data_iface_no)) { 415 sc->sc_iface_index[0] = i; 416 usbd_set_parent_iface(uaa->device, i, uaa->info.bIfaceIndex); 417 break; 418 } 419 } else { 420 device_printf(dev, "no data interface\n"); 421 goto detach; 422 } 423 } 424 425 if (usb_test_quirk(uaa, UQ_ASSUME_CM_OVER_DATA)) { 426 sc->sc_cm_over_data = 1; 427 } else { 428 if (sc->sc_cm_cap & USB_CDC_CM_OVER_DATA) { 429 if (sc->sc_acm_cap & USB_CDC_ACM_HAS_FEATURE) { 430 error = umodem_set_comm_feature 431 (uaa->device, sc->sc_ctrl_iface_no, 432 UCDC_ABSTRACT_STATE, UCDC_DATA_MULTIPLEXED); 433 434 /* ignore any errors */ 435 } 436 sc->sc_cm_over_data = 1; 437 } 438 } 439 error = usbd_transfer_setup(uaa->device, 440 sc->sc_iface_index, sc->sc_xfer, 441 umodem_config, UMODEM_N_TRANSFER, 442 sc, &sc->sc_mtx); 443 if (error) { 444 device_printf(dev, "Can't setup transfer\n"); 445 goto detach; 446 } 447 448 /* clear stall at first run, if USB host mode */ 449 if (uaa->usb_mode == USB_MODE_HOST) { 450 mtx_lock(&sc->sc_mtx); 451 usbd_xfer_set_stall(sc->sc_xfer[UMODEM_BULK_WR]); 452 usbd_xfer_set_stall(sc->sc_xfer[UMODEM_BULK_RD]); 453 mtx_unlock(&sc->sc_mtx); 454 } 455 456 ucom_set_usb_mode(&sc->sc_super_ucom, uaa->usb_mode); 457 458 error = ucom_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc, 459 &umodem_callback, &sc->sc_mtx); 460 if (error) { 461 device_printf(dev, "Can't attach com\n"); 462 goto detach; 463 } 464 ucom_set_pnpinfo_usb(&sc->sc_super_ucom, dev); 465 466 return (0); 467 468 detach: 469 umodem_detach(dev); 470 return (ENXIO); 471 } 472 473 static void 474 umodem_find_data_iface(struct usb_attach_arg *uaa, 475 uint8_t iface_index, uint8_t *p_data_no, uint8_t *p_match_class) 476 { 477 struct usb_interface_descriptor *id; 478 struct usb_interface *iface; 479 480 iface = usbd_get_iface(uaa->device, iface_index); 481 482 /* check for end of interfaces */ 483 if (iface == NULL) 484 return; 485 486 id = usbd_get_interface_descriptor(iface); 487 488 /* check for non-matching interface class */ 489 if (id->bInterfaceClass != UICLASS_CDC_DATA || 490 id->bInterfaceSubClass != UISUBCLASS_DATA) { 491 /* if we got a class match then return */ 492 if (*p_match_class) 493 return; 494 } else { 495 *p_match_class = 1; 496 } 497 498 DPRINTFN(11, "Match at index %u\n", iface_index); 499 500 *p_data_no = id->bInterfaceNumber; 501 } 502 503 static void 504 umodem_start_read(struct ucom_softc *ucom) 505 { 506 struct umodem_softc *sc = ucom->sc_parent; 507 508 /* start interrupt endpoint, if any */ 509 usbd_transfer_start(sc->sc_xfer[UMODEM_INTR_RD]); 510 511 /* start read endpoint */ 512 usbd_transfer_start(sc->sc_xfer[UMODEM_BULK_RD]); 513 } 514 515 static void 516 umodem_stop_read(struct ucom_softc *ucom) 517 { 518 struct umodem_softc *sc = ucom->sc_parent; 519 520 /* stop interrupt endpoint, if any */ 521 usbd_transfer_stop(sc->sc_xfer[UMODEM_INTR_RD]); 522 523 /* stop read endpoint */ 524 usbd_transfer_stop(sc->sc_xfer[UMODEM_BULK_RD]); 525 } 526 527 static void 528 umodem_start_write(struct ucom_softc *ucom) 529 { 530 struct umodem_softc *sc = ucom->sc_parent; 531 532 usbd_transfer_start(sc->sc_xfer[UMODEM_INTR_WR]); 533 usbd_transfer_start(sc->sc_xfer[UMODEM_BULK_WR]); 534 } 535 536 static void 537 umodem_stop_write(struct ucom_softc *ucom) 538 { 539 struct umodem_softc *sc = ucom->sc_parent; 540 541 usbd_transfer_stop(sc->sc_xfer[UMODEM_INTR_WR]); 542 usbd_transfer_stop(sc->sc_xfer[UMODEM_BULK_WR]); 543 } 544 545 static void 546 umodem_get_caps(struct usb_attach_arg *uaa, uint8_t *cm, uint8_t *acm) 547 { 548 struct usb_cdc_cm_descriptor *cmd; 549 struct usb_cdc_acm_descriptor *cad; 550 551 cmd = umodem_get_desc(uaa, UDESC_CS_INTERFACE, UDESCSUB_CDC_CM); 552 if ((cmd == NULL) || (cmd->bLength < sizeof(*cmd))) { 553 DPRINTF("no CM desc (faking one)\n"); 554 *cm = USB_CDC_CM_DOES_CM | USB_CDC_CM_OVER_DATA; 555 } else 556 *cm = cmd->bmCapabilities; 557 558 cad = umodem_get_desc(uaa, UDESC_CS_INTERFACE, UDESCSUB_CDC_ACM); 559 if ((cad == NULL) || (cad->bLength < sizeof(*cad))) { 560 DPRINTF("no ACM desc\n"); 561 *acm = 0; 562 } else 563 *acm = cad->bmCapabilities; 564 } 565 566 static void 567 umodem_cfg_get_status(struct ucom_softc *ucom, uint8_t *lsr, uint8_t *msr) 568 { 569 struct umodem_softc *sc = ucom->sc_parent; 570 571 DPRINTF("\n"); 572 573 /* XXX Note: sc_lsr is always zero */ 574 *lsr = sc->sc_lsr; 575 *msr = sc->sc_msr; 576 } 577 578 static int 579 umodem_pre_param(struct ucom_softc *ucom, struct termios *t) 580 { 581 return (0); /* we accept anything */ 582 } 583 584 static void 585 umodem_cfg_param(struct ucom_softc *ucom, struct termios *t) 586 { 587 struct umodem_softc *sc = ucom->sc_parent; 588 struct usb_cdc_line_state ls; 589 struct usb_device_request req; 590 591 DPRINTF("sc=%p\n", sc); 592 593 memset(&ls, 0, sizeof(ls)); 594 595 USETDW(ls.dwDTERate, t->c_ospeed); 596 597 ls.bCharFormat = (t->c_cflag & CSTOPB) ? 598 UCDC_STOP_BIT_2 : UCDC_STOP_BIT_1; 599 600 ls.bParityType = (t->c_cflag & PARENB) ? 601 ((t->c_cflag & PARODD) ? 602 UCDC_PARITY_ODD : UCDC_PARITY_EVEN) : UCDC_PARITY_NONE; 603 604 switch (t->c_cflag & CSIZE) { 605 case CS5: 606 ls.bDataBits = 5; 607 break; 608 case CS6: 609 ls.bDataBits = 6; 610 break; 611 case CS7: 612 ls.bDataBits = 7; 613 break; 614 case CS8: 615 ls.bDataBits = 8; 616 break; 617 } 618 619 DPRINTF("rate=%d fmt=%d parity=%d bits=%d\n", 620 UGETDW(ls.dwDTERate), ls.bCharFormat, 621 ls.bParityType, ls.bDataBits); 622 623 req.bmRequestType = UT_WRITE_CLASS_INTERFACE; 624 req.bRequest = UCDC_SET_LINE_CODING; 625 USETW(req.wValue, 0); 626 req.wIndex[0] = sc->sc_ctrl_iface_no; 627 req.wIndex[1] = 0; 628 USETW(req.wLength, sizeof(ls)); 629 630 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 631 &req, &ls, 0, 1000); 632 } 633 634 static int 635 umodem_ioctl(struct ucom_softc *ucom, uint32_t cmd, caddr_t data, 636 int flag, struct thread *td) 637 { 638 struct umodem_softc *sc = ucom->sc_parent; 639 int error = 0; 640 641 DPRINTF("cmd=0x%08x\n", cmd); 642 643 switch (cmd) { 644 case USB_GET_CM_OVER_DATA: 645 *(int *)data = sc->sc_cm_over_data; 646 break; 647 648 case USB_SET_CM_OVER_DATA: 649 if (*(int *)data != sc->sc_cm_over_data) { 650 /* XXX change it */ 651 } 652 break; 653 654 default: 655 DPRINTF("unknown\n"); 656 error = ENOIOCTL; 657 break; 658 } 659 660 return (error); 661 } 662 663 static void 664 umodem_cfg_set_dtr(struct ucom_softc *ucom, uint8_t onoff) 665 { 666 struct umodem_softc *sc = ucom->sc_parent; 667 struct usb_device_request req; 668 669 DPRINTF("onoff=%d\n", onoff); 670 671 if (onoff) 672 sc->sc_line |= UCDC_LINE_DTR; 673 else 674 sc->sc_line &= ~UCDC_LINE_DTR; 675 676 req.bmRequestType = UT_WRITE_CLASS_INTERFACE; 677 req.bRequest = UCDC_SET_CONTROL_LINE_STATE; 678 USETW(req.wValue, sc->sc_line); 679 req.wIndex[0] = sc->sc_ctrl_iface_no; 680 req.wIndex[1] = 0; 681 USETW(req.wLength, 0); 682 683 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 684 &req, NULL, 0, 1000); 685 } 686 687 static void 688 umodem_cfg_set_rts(struct ucom_softc *ucom, uint8_t onoff) 689 { 690 struct umodem_softc *sc = ucom->sc_parent; 691 struct usb_device_request req; 692 693 DPRINTF("onoff=%d\n", onoff); 694 695 if (onoff) 696 sc->sc_line |= UCDC_LINE_RTS; 697 else 698 sc->sc_line &= ~UCDC_LINE_RTS; 699 700 req.bmRequestType = UT_WRITE_CLASS_INTERFACE; 701 req.bRequest = UCDC_SET_CONTROL_LINE_STATE; 702 USETW(req.wValue, sc->sc_line); 703 req.wIndex[0] = sc->sc_ctrl_iface_no; 704 req.wIndex[1] = 0; 705 USETW(req.wLength, 0); 706 707 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 708 &req, NULL, 0, 1000); 709 } 710 711 static void 712 umodem_cfg_set_break(struct ucom_softc *ucom, uint8_t onoff) 713 { 714 struct umodem_softc *sc = ucom->sc_parent; 715 struct usb_device_request req; 716 uint16_t temp; 717 718 DPRINTF("onoff=%d\n", onoff); 719 720 if (sc->sc_acm_cap & USB_CDC_ACM_HAS_BREAK) { 721 temp = onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF; 722 723 req.bmRequestType = UT_WRITE_CLASS_INTERFACE; 724 req.bRequest = UCDC_SEND_BREAK; 725 USETW(req.wValue, temp); 726 req.wIndex[0] = sc->sc_ctrl_iface_no; 727 req.wIndex[1] = 0; 728 USETW(req.wLength, 0); 729 730 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 731 &req, NULL, 0, 1000); 732 } 733 } 734 735 static void 736 umodem_intr_write_callback(struct usb_xfer *xfer, usb_error_t error) 737 { 738 int actlen; 739 740 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL); 741 742 switch (USB_GET_STATE(xfer)) { 743 case USB_ST_TRANSFERRED: 744 745 DPRINTF("Transferred %d bytes\n", actlen); 746 747 /* FALLTHROUGH */ 748 case USB_ST_SETUP: 749 tr_setup: 750 break; 751 752 default: /* Error */ 753 if (error != USB_ERR_CANCELLED) { 754 /* start clear stall */ 755 usbd_xfer_set_stall(xfer); 756 goto tr_setup; 757 } 758 break; 759 } 760 } 761 762 static void 763 umodem_intr_read_callback(struct usb_xfer *xfer, usb_error_t error) 764 { 765 struct usb_cdc_notification pkt; 766 struct umodem_softc *sc = usbd_xfer_softc(xfer); 767 struct usb_page_cache *pc; 768 uint16_t wLen; 769 int actlen; 770 771 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL); 772 773 switch (USB_GET_STATE(xfer)) { 774 case USB_ST_TRANSFERRED: 775 776 if (actlen < 8) { 777 DPRINTF("received short packet, " 778 "%d bytes\n", actlen); 779 goto tr_setup; 780 } 781 if (actlen > (int)sizeof(pkt)) { 782 DPRINTF("truncating message\n"); 783 actlen = sizeof(pkt); 784 } 785 pc = usbd_xfer_get_frame(xfer, 0); 786 usbd_copy_out(pc, 0, &pkt, actlen); 787 788 actlen -= 8; 789 790 wLen = UGETW(pkt.wLength); 791 if (actlen > wLen) { 792 actlen = wLen; 793 } 794 if (pkt.bmRequestType != UCDC_NOTIFICATION) { 795 DPRINTF("unknown message type, " 796 "0x%02x, on notify pipe!\n", 797 pkt.bmRequestType); 798 goto tr_setup; 799 } 800 switch (pkt.bNotification) { 801 case UCDC_N_SERIAL_STATE: 802 /* 803 * Set the serial state in ucom driver based on 804 * the bits from the notify message 805 */ 806 if (actlen < 2) { 807 DPRINTF("invalid notification " 808 "length, %d bytes!\n", actlen); 809 break; 810 } 811 DPRINTF("notify bytes = %02x%02x\n", 812 pkt.data[0], 813 pkt.data[1]); 814 815 /* Currently, lsr is always zero. */ 816 sc->sc_lsr = 0; 817 sc->sc_msr = 0; 818 819 if (pkt.data[0] & UCDC_N_SERIAL_RI) { 820 sc->sc_msr |= SER_RI; 821 } 822 if (pkt.data[0] & UCDC_N_SERIAL_DSR) { 823 sc->sc_msr |= SER_DSR; 824 } 825 if (pkt.data[0] & UCDC_N_SERIAL_DCD) { 826 sc->sc_msr |= SER_DCD; 827 } 828 ucom_status_change(&sc->sc_ucom); 829 break; 830 831 default: 832 DPRINTF("unknown notify message: 0x%02x\n", 833 pkt.bNotification); 834 break; 835 } 836 837 case USB_ST_SETUP: 838 tr_setup: 839 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer)); 840 usbd_transfer_submit(xfer); 841 return; 842 843 default: /* Error */ 844 if (error != USB_ERR_CANCELLED) { 845 /* try to clear stall first */ 846 usbd_xfer_set_stall(xfer); 847 goto tr_setup; 848 } 849 return; 850 } 851 } 852 853 static void 854 umodem_write_callback(struct usb_xfer *xfer, usb_error_t error) 855 { 856 struct umodem_softc *sc = usbd_xfer_softc(xfer); 857 struct usb_page_cache *pc; 858 uint32_t actlen; 859 860 switch (USB_GET_STATE(xfer)) { 861 case USB_ST_SETUP: 862 case USB_ST_TRANSFERRED: 863 tr_setup: 864 pc = usbd_xfer_get_frame(xfer, 0); 865 if (ucom_get_data(&sc->sc_ucom, pc, 0, 866 UMODEM_BUF_SIZE, &actlen)) { 867 usbd_xfer_set_frame_len(xfer, 0, actlen); 868 usbd_transfer_submit(xfer); 869 } 870 return; 871 872 default: /* Error */ 873 if (error != USB_ERR_CANCELLED) { 874 /* try to clear stall first */ 875 usbd_xfer_set_stall(xfer); 876 goto tr_setup; 877 } 878 return; 879 } 880 } 881 882 static void 883 umodem_read_callback(struct usb_xfer *xfer, usb_error_t error) 884 { 885 struct umodem_softc *sc = usbd_xfer_softc(xfer); 886 struct usb_page_cache *pc; 887 int actlen; 888 889 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL); 890 891 switch (USB_GET_STATE(xfer)) { 892 case USB_ST_TRANSFERRED: 893 894 DPRINTF("actlen=%d\n", actlen); 895 896 pc = usbd_xfer_get_frame(xfer, 0); 897 ucom_put_data(&sc->sc_ucom, pc, 0, actlen); 898 899 case USB_ST_SETUP: 900 tr_setup: 901 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer)); 902 usbd_transfer_submit(xfer); 903 return; 904 905 default: /* Error */ 906 if (error != USB_ERR_CANCELLED) { 907 /* try to clear stall first */ 908 usbd_xfer_set_stall(xfer); 909 goto tr_setup; 910 } 911 return; 912 } 913 } 914 915 static void * 916 umodem_get_desc(struct usb_attach_arg *uaa, uint8_t type, uint8_t subtype) 917 { 918 return (usbd_find_descriptor(uaa->device, NULL, uaa->info.bIfaceIndex, 919 type, 0xFF, subtype, 0xFF)); 920 } 921 922 static usb_error_t 923 umodem_set_comm_feature(struct usb_device *udev, uint8_t iface_no, 924 uint16_t feature, uint16_t state) 925 { 926 struct usb_device_request req; 927 struct usb_cdc_abstract_state ast; 928 929 DPRINTF("feature=%d state=%d\n", 930 feature, state); 931 932 req.bmRequestType = UT_WRITE_CLASS_INTERFACE; 933 req.bRequest = UCDC_SET_COMM_FEATURE; 934 USETW(req.wValue, feature); 935 req.wIndex[0] = iface_no; 936 req.wIndex[1] = 0; 937 USETW(req.wLength, UCDC_ABSTRACT_STATE_LENGTH); 938 USETW(ast.wState, state); 939 940 return (usbd_do_request(udev, NULL, &req, &ast)); 941 } 942 943 static int 944 umodem_detach(device_t dev) 945 { 946 struct umodem_softc *sc = device_get_softc(dev); 947 948 DPRINTF("sc=%p\n", sc); 949 950 ucom_detach(&sc->sc_super_ucom, &sc->sc_ucom); 951 usbd_transfer_unsetup(sc->sc_xfer, UMODEM_N_TRANSFER); 952 953 device_claim_softc(dev); 954 955 umodem_free_softc(sc); 956 957 return (0); 958 } 959 960 UCOM_UNLOAD_DRAIN(umodem); 961 962 static void 963 umodem_free_softc(struct umodem_softc *sc) 964 { 965 if (ucom_unref(&sc->sc_super_ucom)) { 966 mtx_destroy(&sc->sc_mtx); 967 device_free_softc(sc); 968 } 969 } 970 971 static void 972 umodem_free(struct ucom_softc *ucom) 973 { 974 umodem_free_softc(ucom->sc_parent); 975 } 976 977 static void 978 umodem_poll(struct ucom_softc *ucom) 979 { 980 struct umodem_softc *sc = ucom->sc_parent; 981 usbd_transfer_poll(sc->sc_xfer, UMODEM_N_TRANSFER); 982 } 983 984 static int 985 umodem_handle_request(device_t dev, 986 const void *preq, void **pptr, uint16_t *plen, 987 uint16_t offset, uint8_t *pstate) 988 { 989 struct umodem_softc *sc = device_get_softc(dev); 990 const struct usb_device_request *req = preq; 991 uint8_t is_complete = *pstate; 992 993 DPRINTF("sc=%p\n", sc); 994 995 if (!is_complete) { 996 if ((req->bmRequestType == UT_WRITE_CLASS_INTERFACE) && 997 (req->bRequest == UCDC_SET_LINE_CODING) && 998 (req->wIndex[0] == sc->sc_ctrl_iface_no) && 999 (req->wIndex[1] == 0x00) && 1000 (req->wValue[0] == 0x00) && 1001 (req->wValue[1] == 0x00)) { 1002 if (offset == 0) { 1003 *plen = sizeof(sc->sc_line_coding); 1004 *pptr = &sc->sc_line_coding; 1005 } else { 1006 *plen = 0; 1007 } 1008 return (0); 1009 } else if ((req->bmRequestType == UT_WRITE_CLASS_INTERFACE) && 1010 (req->wIndex[0] == sc->sc_ctrl_iface_no) && 1011 (req->wIndex[1] == 0x00) && 1012 (req->bRequest == UCDC_SET_COMM_FEATURE)) { 1013 if (offset == 0) { 1014 *plen = sizeof(sc->sc_abstract_state); 1015 *pptr = &sc->sc_abstract_state; 1016 } else { 1017 *plen = 0; 1018 } 1019 return (0); 1020 } else if ((req->bmRequestType == UT_WRITE_CLASS_INTERFACE) && 1021 (req->wIndex[0] == sc->sc_ctrl_iface_no) && 1022 (req->wIndex[1] == 0x00) && 1023 (req->bRequest == UCDC_SET_CONTROL_LINE_STATE)) { 1024 *plen = 0; 1025 return (0); 1026 } else if ((req->bmRequestType == UT_WRITE_CLASS_INTERFACE) && 1027 (req->wIndex[0] == sc->sc_ctrl_iface_no) && 1028 (req->wIndex[1] == 0x00) && 1029 (req->bRequest == UCDC_SEND_BREAK)) { 1030 *plen = 0; 1031 return (0); 1032 } 1033 } 1034 return (ENXIO); /* use builtin handler */ 1035 } 1036