1 /* $OpenBSD: umodem.c,v 1.65 2020/02/22 14:01:35 jasper Exp $ */ 2 /* $NetBSD: umodem.c,v 1.45 2002/09/23 05:51:23 simonb Exp $ */ 3 4 /* 5 * Copyright (c) 1998 The NetBSD Foundation, Inc. 6 * All rights reserved. 7 * 8 * This code is derived from software contributed to The NetBSD Foundation 9 * by Lennart Augustsson (lennart@augustsson.net) at 10 * Carlstedt Research & Technology. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 23 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 31 * POSSIBILITY OF SUCH DAMAGE. 32 */ 33 34 /* 35 * Comm Class spec: http://www.usb.org/developers/devclass_docs/usbccs10.pdf 36 * http://www.usb.org/developers/devclass_docs/usbcdc11.pdf 37 */ 38 39 /* 40 * TODO: 41 * - Add error recovery in various places; the big problem is what 42 * to do in a callback if there is an error. 43 * - Implement a Call Device for modems without multiplexed commands. 44 * 45 */ 46 47 #include <sys/param.h> 48 #include <sys/systm.h> 49 #include <sys/kernel.h> 50 #include <sys/conf.h> 51 #include <sys/tty.h> 52 #include <sys/selinfo.h> 53 #include <sys/device.h> 54 #include <sys/poll.h> 55 56 #include <dev/usb/usb.h> 57 #include <dev/usb/usbcdc.h> 58 59 #include <dev/usb/usbdi.h> 60 #include <dev/usb/usbdevs.h> 61 #include <dev/usb/usb_quirks.h> 62 63 #include <dev/usb/ucomvar.h> 64 65 #ifdef UMODEM_DEBUG 66 #define DPRINTFN(n, x) do { if (umodemdebug > (n)) printf x; } while (0) 67 int umodemdebug = 0; 68 #else 69 #define DPRINTFN(n, x) 70 #endif 71 #define DPRINTF(x) DPRINTFN(0, x) 72 73 /* 74 * These are the maximum number of bytes transferred per frame. 75 * Buffers are this large to deal with high speed wireless devices. 76 * Capped at 1024 as ttymalloc() is limited to this amount. 77 */ 78 #define UMODEMIBUFSIZE 1024 79 #define UMODEMOBUFSIZE 1024 80 81 struct umodem_softc { 82 struct device sc_dev; /* base device */ 83 84 struct usbd_device *sc_udev; /* USB device */ 85 86 int sc_ctl_iface_no; 87 struct usbd_interface *sc_ctl_iface; /* control interface */ 88 struct usbd_interface *sc_data_iface; /* data interface */ 89 90 int sc_cm_cap; /* CM capabilities */ 91 int sc_acm_cap; /* ACM capabilities */ 92 93 int sc_cm_over_data; 94 95 struct usb_cdc_line_state sc_line_state;/* current line state */ 96 u_char sc_dtr; /* current DTR state */ 97 u_char sc_rts; /* current RTS state */ 98 99 struct device *sc_subdev; /* ucom device */ 100 101 int sc_ctl_notify; /* Notification endpoint */ 102 struct usbd_pipe *sc_notify_pipe; /* Notification pipe */ 103 struct usb_cdc_notification sc_notify_buf; /* Notification structure */ 104 u_char sc_lsr; /* Local status register */ 105 u_char sc_msr; /* Modem status register */ 106 }; 107 108 usbd_status umodem_set_comm_feature(struct umodem_softc *sc, 109 int feature, int state); 110 usbd_status umodem_set_line_coding(struct umodem_softc *sc, 111 struct usb_cdc_line_state *state); 112 113 void umodem_get_status(void *, int portno, u_char *lsr, u_char *msr); 114 void umodem_set(void *, int, int, int); 115 void umodem_dtr(struct umodem_softc *, int); 116 void umodem_rts(struct umodem_softc *, int); 117 void umodem_break(struct umodem_softc *, int); 118 void umodem_set_line_state(struct umodem_softc *); 119 int umodem_param(void *, int, struct termios *); 120 int umodem_open(void *, int portno); 121 void umodem_close(void *, int portno); 122 void umodem_intr(struct usbd_xfer *, void *, usbd_status); 123 124 struct ucom_methods umodem_methods = { 125 umodem_get_status, 126 umodem_set, 127 umodem_param, 128 NULL, 129 umodem_open, 130 umodem_close, 131 NULL, 132 NULL, 133 }; 134 135 int umodem_match(struct device *, void *, void *); 136 void umodem_attach(struct device *, struct device *, void *); 137 int umodem_detach(struct device *, int); 138 139 void umodem_get_caps(struct usb_attach_arg *, int, int *, int *, int *); 140 141 struct cfdriver umodem_cd = { 142 NULL, "umodem", DV_DULL 143 }; 144 145 const struct cfattach umodem_ca = { 146 sizeof(struct umodem_softc), umodem_match, umodem_attach, umodem_detach 147 }; 148 149 void 150 umodem_get_caps(struct usb_attach_arg *uaa, int ctl_iface_no, 151 int *data_iface_idx, int *cm_cap, int *acm_cap) 152 { 153 const usb_descriptor_t *desc; 154 const usb_interface_descriptor_t *id; 155 const struct usb_cdc_cm_descriptor *cmd; 156 const struct usb_cdc_acm_descriptor *acmd; 157 const struct usb_cdc_union_descriptor *uniond; 158 struct usbd_desc_iter iter; 159 int current_iface_no = -1; 160 int data_iface_no = -1; 161 int i; 162 163 *data_iface_idx = -1; 164 *cm_cap = *acm_cap = 0; 165 usbd_desc_iter_init(uaa->device, &iter); 166 desc = usbd_desc_iter_next(&iter); 167 while (desc) { 168 if (desc->bDescriptorType == UDESC_INTERFACE) { 169 id = (usb_interface_descriptor_t *)desc; 170 current_iface_no = id->bInterfaceNumber; 171 if (current_iface_no != ctl_iface_no && 172 id->bInterfaceClass == UICLASS_CDC_DATA && 173 id->bInterfaceSubClass == UISUBCLASS_DATA && 174 data_iface_no == -1) 175 data_iface_no = current_iface_no; 176 } 177 if (current_iface_no == ctl_iface_no && 178 desc->bDescriptorType == UDESC_CS_INTERFACE) { 179 switch(desc->bDescriptorSubtype) { 180 case UDESCSUB_CDC_CM: 181 cmd = (struct usb_cdc_cm_descriptor *)desc; 182 *cm_cap = cmd->bmCapabilities; 183 data_iface_no = cmd->bDataInterface; 184 break; 185 case UDESCSUB_CDC_ACM: 186 acmd = (struct usb_cdc_acm_descriptor *)desc; 187 *acm_cap = acmd->bmCapabilities; 188 break; 189 case UDESCSUB_CDC_UNION: 190 uniond = 191 (struct usb_cdc_union_descriptor *)desc; 192 data_iface_no = uniond->bSlaveInterface[0]; 193 break; 194 } 195 } 196 desc = usbd_desc_iter_next(&iter); 197 } 198 199 /* 200 * If we got a data interface number, make sure the corresponding 201 * interface exists and is not already claimed. 202 */ 203 if (data_iface_no != -1) { 204 for (i = 0; i < uaa->nifaces; i++) { 205 id = usbd_get_interface_descriptor(uaa->ifaces[i]); 206 207 if (id == NULL) 208 continue; 209 210 if (id->bInterfaceNumber == data_iface_no) { 211 if (!usbd_iface_claimed(uaa->device, i)) 212 *data_iface_idx = i; 213 break; 214 } 215 } 216 } 217 } 218 219 int 220 umodem_match(struct device *parent, void *match, void *aux) 221 { 222 struct usb_attach_arg *uaa = aux; 223 usb_interface_descriptor_t *id; 224 usb_device_descriptor_t *dd; 225 int data_iface_idx, cm_cap, acm_cap, ret = UMATCH_NONE; 226 227 if (uaa->iface == NULL) 228 return (ret); 229 230 id = usbd_get_interface_descriptor(uaa->iface); 231 dd = usbd_get_device_descriptor(uaa->device); 232 if (id == NULL || dd == NULL) 233 return (ret); 234 235 ret = UMATCH_NONE; 236 237 if (UGETW(dd->idVendor) == USB_VENDOR_KYOCERA && 238 UGETW(dd->idProduct) == USB_PRODUCT_KYOCERA_AHK3001V && 239 id->bInterfaceNumber == 0) 240 ret = UMATCH_VENDOR_PRODUCT; 241 242 if (ret == UMATCH_NONE && 243 id->bInterfaceClass == UICLASS_CDC && 244 id->bInterfaceSubClass == UISUBCLASS_ABSTRACT_CONTROL_MODEL && 245 (id->bInterfaceProtocol == UIPROTO_CDC_AT || 246 id->bInterfaceProtocol == UIPROTO_CDC_NOCLASS)) 247 ret = UMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO; 248 249 if (ret == UMATCH_NONE) 250 return (ret); 251 252 /* umodem doesn't support devices without a data iface */ 253 umodem_get_caps(uaa, id->bInterfaceNumber, &data_iface_idx, 254 &cm_cap, &acm_cap); 255 if (data_iface_idx == -1) 256 ret = UMATCH_NONE; 257 258 return (ret); 259 } 260 261 void 262 umodem_attach(struct device *parent, struct device *self, void *aux) 263 { 264 struct umodem_softc *sc = (struct umodem_softc *)self; 265 struct usb_attach_arg *uaa = aux; 266 struct usbd_device *dev = uaa->device; 267 usb_interface_descriptor_t *id; 268 usb_endpoint_descriptor_t *ed; 269 usbd_status err; 270 int data_iface_idx = -1; 271 int i; 272 struct ucom_attach_args uca; 273 274 sc->sc_udev = dev; 275 sc->sc_ctl_iface = uaa->iface; 276 277 id = usbd_get_interface_descriptor(sc->sc_ctl_iface); 278 //printf("%s: iclass %d/%d\n", sc->sc_dev.dv_xname, 279 // id->bInterfaceClass, id->bInterfaceSubClass); 280 sc->sc_ctl_iface_no = id->bInterfaceNumber; 281 282 /* Get the capabilities. */ 283 umodem_get_caps(uaa, id->bInterfaceNumber, &data_iface_idx, 284 &sc->sc_cm_cap, &sc->sc_acm_cap); 285 286 usbd_claim_iface(sc->sc_udev, data_iface_idx); 287 sc->sc_data_iface = uaa->ifaces[data_iface_idx]; 288 id = usbd_get_interface_descriptor(sc->sc_data_iface); 289 290 printf("%s: data interface %d, has %sCM over data, has %sbreak\n", 291 sc->sc_dev.dv_xname, id->bInterfaceNumber, 292 sc->sc_cm_cap & USB_CDC_CM_OVER_DATA ? "" : "no ", 293 sc->sc_acm_cap & USB_CDC_ACM_HAS_BREAK ? "" : "no "); 294 295 /* 296 * Find the bulk endpoints. 297 * Iterate over all endpoints in the data interface and take note. 298 */ 299 uca.bulkin = uca.bulkout = -1; 300 301 for (i = 0; i < id->bNumEndpoints; i++) { 302 ed = usbd_interface2endpoint_descriptor(sc->sc_data_iface, i); 303 if (ed == NULL) { 304 printf("%s: no endpoint descriptor for %d\n", 305 sc->sc_dev.dv_xname, i); 306 goto bad; 307 } 308 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN && 309 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) { 310 uca.bulkin = ed->bEndpointAddress; 311 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT && 312 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) { 313 uca.bulkout = ed->bEndpointAddress; 314 } 315 } 316 317 if (uca.bulkin == -1) { 318 printf("%s: Could not find data bulk in\n", 319 sc->sc_dev.dv_xname); 320 goto bad; 321 } 322 if (uca.bulkout == -1) { 323 printf("%s: Could not find data bulk out\n", 324 sc->sc_dev.dv_xname); 325 goto bad; 326 } 327 328 if (usbd_get_quirks(sc->sc_udev)->uq_flags & UQ_ASSUME_CM_OVER_DATA) { 329 sc->sc_cm_over_data = 1; 330 } else { 331 if (sc->sc_cm_cap & USB_CDC_CM_OVER_DATA) { 332 if (sc->sc_acm_cap & USB_CDC_ACM_HAS_FEATURE) 333 err = umodem_set_comm_feature(sc, 334 UCDC_ABSTRACT_STATE, UCDC_DATA_MULTIPLEXED); 335 else 336 err = 0; 337 if (err) { 338 printf("%s: could not set data multiplex mode\n", 339 sc->sc_dev.dv_xname); 340 goto bad; 341 } 342 sc->sc_cm_over_data = 1; 343 } 344 } 345 346 /* 347 * The standard allows for notification messages (to indicate things 348 * like a modem hangup) to come in via an interrupt endpoint 349 * off of the control interface. Iterate over the endpoints on 350 * the control interface and see if there are any interrupt 351 * endpoints; if there are, then register it. 352 */ 353 354 sc->sc_ctl_notify = -1; 355 sc->sc_notify_pipe = NULL; 356 357 id = usbd_get_interface_descriptor(sc->sc_ctl_iface); 358 for (i = 0; i < id->bNumEndpoints; i++) { 359 ed = usbd_interface2endpoint_descriptor(sc->sc_ctl_iface, i); 360 if (ed == NULL) 361 continue; 362 363 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN && 364 UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) { 365 printf("%s: status change notification available\n", 366 sc->sc_dev.dv_xname); 367 sc->sc_ctl_notify = ed->bEndpointAddress; 368 } 369 } 370 371 sc->sc_dtr = -1; 372 373 uca.portno = UCOM_UNK_PORTNO; 374 /* bulkin, bulkout set above */ 375 uca.ibufsize = UMODEMIBUFSIZE; 376 uca.obufsize = UMODEMOBUFSIZE; 377 uca.ibufsizepad = UMODEMIBUFSIZE; 378 uca.opkthdrlen = 0; 379 uca.device = sc->sc_udev; 380 uca.iface = sc->sc_data_iface; 381 uca.methods = &umodem_methods; 382 uca.arg = sc; 383 uca.info = NULL; 384 385 DPRINTF(("umodem_attach: sc=%p\n", sc)); 386 sc->sc_subdev = config_found_sm(self, &uca, ucomprint, ucomsubmatch); 387 388 return; 389 390 bad: 391 usbd_deactivate(sc->sc_udev); 392 } 393 394 int 395 umodem_open(void *addr, int portno) 396 { 397 struct umodem_softc *sc = addr; 398 int err; 399 400 DPRINTF(("umodem_open: sc=%p\n", sc)); 401 402 if (sc->sc_ctl_notify != -1 && sc->sc_notify_pipe == NULL) { 403 err = usbd_open_pipe_intr(sc->sc_ctl_iface, sc->sc_ctl_notify, 404 USBD_SHORT_XFER_OK, &sc->sc_notify_pipe, sc, 405 &sc->sc_notify_buf, sizeof(sc->sc_notify_buf), 406 umodem_intr, USBD_DEFAULT_INTERVAL); 407 408 if (err) { 409 DPRINTF(("Failed to establish notify pipe: %s\n", 410 usbd_errstr(err))); 411 return EIO; 412 } 413 } 414 415 return 0; 416 } 417 418 void 419 umodem_close(void *addr, int portno) 420 { 421 struct umodem_softc *sc = addr; 422 int err; 423 424 DPRINTF(("umodem_close: sc=%p\n", sc)); 425 426 if (sc->sc_notify_pipe != NULL) { 427 usbd_abort_pipe(sc->sc_notify_pipe); 428 err = usbd_close_pipe(sc->sc_notify_pipe); 429 if (err) 430 printf("%s: close notify pipe failed: %s\n", 431 sc->sc_dev.dv_xname, usbd_errstr(err)); 432 sc->sc_notify_pipe = NULL; 433 } 434 } 435 436 void 437 umodem_intr(struct usbd_xfer *xfer, void *priv, usbd_status status) 438 { 439 struct umodem_softc *sc = priv; 440 u_char mstatus; 441 442 if (usbd_is_dying(sc->sc_udev)) 443 return; 444 445 if (status != USBD_NORMAL_COMPLETION) { 446 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) 447 return; 448 DPRINTF(("%s: abnormal status: %s\n", sc->sc_dev.dv_xname, 449 usbd_errstr(status))); 450 usbd_clear_endpoint_stall_async(sc->sc_notify_pipe); 451 return; 452 } 453 454 if (sc->sc_notify_buf.bmRequestType != UCDC_NOTIFICATION) { 455 DPRINTF(("%s: unknown message type (%02x) on notify pipe\n", 456 sc->sc_dev.dv_xname, 457 sc->sc_notify_buf.bmRequestType)); 458 return; 459 } 460 461 switch (sc->sc_notify_buf.bNotification) { 462 case UCDC_N_SERIAL_STATE: 463 /* 464 * Set the serial state in ucom driver based on 465 * the bits from the notify message 466 */ 467 if (UGETW(sc->sc_notify_buf.wLength) != 2) { 468 printf("%s: Invalid notification length! (%d)\n", 469 sc->sc_dev.dv_xname, 470 UGETW(sc->sc_notify_buf.wLength)); 471 break; 472 } 473 DPRINTF(("%s: notify bytes = %02x%02x\n", 474 sc->sc_dev.dv_xname, 475 sc->sc_notify_buf.data[0], 476 sc->sc_notify_buf.data[1])); 477 /* Currently, lsr is always zero. */ 478 sc->sc_lsr = sc->sc_msr = 0; 479 mstatus = sc->sc_notify_buf.data[0]; 480 481 if (ISSET(mstatus, UCDC_N_SERIAL_RI)) 482 sc->sc_msr |= UMSR_RI; 483 if (ISSET(mstatus, UCDC_N_SERIAL_DSR)) 484 sc->sc_msr |= UMSR_DSR; 485 if (ISSET(mstatus, UCDC_N_SERIAL_DCD)) 486 sc->sc_msr |= UMSR_DCD; 487 ucom_status_change((struct ucom_softc *)sc->sc_subdev); 488 break; 489 default: 490 DPRINTF(("%s: unknown notify message: %02x\n", 491 sc->sc_dev.dv_xname, 492 sc->sc_notify_buf.bNotification)); 493 break; 494 } 495 } 496 497 void 498 umodem_get_status(void *addr, int portno, u_char *lsr, u_char *msr) 499 { 500 struct umodem_softc *sc = addr; 501 502 DPRINTF(("umodem_get_status:\n")); 503 504 if (lsr != NULL) 505 *lsr = sc->sc_lsr; 506 if (msr != NULL) 507 *msr = sc->sc_msr; 508 } 509 510 int 511 umodem_param(void *addr, int portno, struct termios *t) 512 { 513 struct umodem_softc *sc = addr; 514 usbd_status err; 515 struct usb_cdc_line_state ls; 516 517 DPRINTF(("umodem_param: sc=%p\n", sc)); 518 519 USETDW(ls.dwDTERate, t->c_ospeed); 520 if (ISSET(t->c_cflag, CSTOPB)) 521 ls.bCharFormat = UCDC_STOP_BIT_2; 522 else 523 ls.bCharFormat = UCDC_STOP_BIT_1; 524 if (ISSET(t->c_cflag, PARENB)) { 525 if (ISSET(t->c_cflag, PARODD)) 526 ls.bParityType = UCDC_PARITY_ODD; 527 else 528 ls.bParityType = UCDC_PARITY_EVEN; 529 } else 530 ls.bParityType = UCDC_PARITY_NONE; 531 switch (ISSET(t->c_cflag, CSIZE)) { 532 case CS5: 533 ls.bDataBits = 5; 534 break; 535 case CS6: 536 ls.bDataBits = 6; 537 break; 538 case CS7: 539 ls.bDataBits = 7; 540 break; 541 case CS8: 542 ls.bDataBits = 8; 543 break; 544 } 545 546 err = umodem_set_line_coding(sc, &ls); 547 if (err) { 548 DPRINTF(("umodem_param: err=%s\n", usbd_errstr(err))); 549 return (1); 550 } 551 return (0); 552 } 553 554 void 555 umodem_dtr(struct umodem_softc *sc, int onoff) 556 { 557 DPRINTF(("umodem_dtr: onoff=%d\n", onoff)); 558 559 if (sc->sc_dtr == onoff) 560 return; 561 sc->sc_dtr = onoff; 562 563 umodem_set_line_state(sc); 564 } 565 566 void 567 umodem_rts(struct umodem_softc *sc, int onoff) 568 { 569 DPRINTF(("umodem_rts: onoff=%d\n", onoff)); 570 571 if (sc->sc_rts == onoff) 572 return; 573 sc->sc_rts = onoff; 574 575 umodem_set_line_state(sc); 576 } 577 578 void 579 umodem_set_line_state(struct umodem_softc *sc) 580 { 581 usb_device_request_t req; 582 int ls; 583 584 ls = (sc->sc_dtr ? UCDC_LINE_DTR : 0) | 585 (sc->sc_rts ? UCDC_LINE_RTS : 0); 586 req.bmRequestType = UT_WRITE_CLASS_INTERFACE; 587 req.bRequest = UCDC_SET_CONTROL_LINE_STATE; 588 USETW(req.wValue, ls); 589 USETW(req.wIndex, sc->sc_ctl_iface_no); 590 USETW(req.wLength, 0); 591 592 (void)usbd_do_request(sc->sc_udev, &req, 0); 593 594 } 595 596 void 597 umodem_break(struct umodem_softc *sc, int onoff) 598 { 599 usb_device_request_t req; 600 601 DPRINTF(("umodem_break: onoff=%d\n", onoff)); 602 603 if (!(sc->sc_acm_cap & USB_CDC_ACM_HAS_BREAK)) 604 return; 605 606 req.bmRequestType = UT_WRITE_CLASS_INTERFACE; 607 req.bRequest = UCDC_SEND_BREAK; 608 USETW(req.wValue, onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF); 609 USETW(req.wIndex, sc->sc_ctl_iface_no); 610 USETW(req.wLength, 0); 611 612 (void)usbd_do_request(sc->sc_udev, &req, 0); 613 } 614 615 void 616 umodem_set(void *addr, int portno, int reg, int onoff) 617 { 618 struct umodem_softc *sc = addr; 619 620 switch (reg) { 621 case UCOM_SET_DTR: 622 umodem_dtr(sc, onoff); 623 break; 624 case UCOM_SET_RTS: 625 umodem_rts(sc, onoff); 626 break; 627 case UCOM_SET_BREAK: 628 umodem_break(sc, onoff); 629 break; 630 default: 631 break; 632 } 633 } 634 635 usbd_status 636 umodem_set_line_coding(struct umodem_softc *sc, 637 struct usb_cdc_line_state *state) 638 { 639 usb_device_request_t req; 640 usbd_status err; 641 642 DPRINTF(("umodem_set_line_coding: rate=%d fmt=%d parity=%d bits=%d\n", 643 UGETDW(state->dwDTERate), state->bCharFormat, 644 state->bParityType, state->bDataBits)); 645 646 if (memcmp(state, &sc->sc_line_state, UCDC_LINE_STATE_LENGTH) == 0) { 647 DPRINTF(("umodem_set_line_coding: already set\n")); 648 return (USBD_NORMAL_COMPLETION); 649 } 650 651 req.bmRequestType = UT_WRITE_CLASS_INTERFACE; 652 req.bRequest = UCDC_SET_LINE_CODING; 653 USETW(req.wValue, 0); 654 USETW(req.wIndex, sc->sc_ctl_iface_no); 655 USETW(req.wLength, UCDC_LINE_STATE_LENGTH); 656 657 err = usbd_do_request(sc->sc_udev, &req, state); 658 if (err) { 659 DPRINTF(("umodem_set_line_coding: failed, err=%s\n", 660 usbd_errstr(err))); 661 return (err); 662 } 663 664 sc->sc_line_state = *state; 665 666 return (USBD_NORMAL_COMPLETION); 667 } 668 669 usbd_status 670 umodem_set_comm_feature(struct umodem_softc *sc, int feature, int state) 671 { 672 usb_device_request_t req; 673 usbd_status err; 674 struct usb_cdc_abstract_state ast; 675 676 DPRINTF(("umodem_set_comm_feature: feature=%d state=%d\n", feature, 677 state)); 678 679 req.bmRequestType = UT_WRITE_CLASS_INTERFACE; 680 req.bRequest = UCDC_SET_COMM_FEATURE; 681 USETW(req.wValue, feature); 682 USETW(req.wIndex, sc->sc_ctl_iface_no); 683 USETW(req.wLength, UCDC_ABSTRACT_STATE_LENGTH); 684 USETW(ast.wState, state); 685 686 err = usbd_do_request(sc->sc_udev, &req, &ast); 687 if (err) { 688 DPRINTF(("umodem_set_comm_feature: feature=%d, err=%s\n", 689 feature, usbd_errstr(err))); 690 return (err); 691 } 692 693 return (USBD_NORMAL_COMPLETION); 694 } 695 696 int 697 umodem_detach(struct device *self, int flags) 698 { 699 struct umodem_softc *sc = (struct umodem_softc *)self; 700 int rv = 0; 701 702 DPRINTF(("umodem_detach: sc=%p flags=%d\n", sc, flags)); 703 704 if (sc->sc_notify_pipe != NULL) { 705 usbd_abort_pipe(sc->sc_notify_pipe); 706 usbd_close_pipe(sc->sc_notify_pipe); 707 sc->sc_notify_pipe = NULL; 708 } 709 710 if (sc->sc_subdev != NULL) 711 rv = config_detach(sc->sc_subdev, flags); 712 713 return (rv); 714 } 715