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