LCOV - code coverage report
Current view: top level - dev/usb - umodem.c (source / functions) Hit Total Coverage
Test: 6.4 Lines: 0 299 0.0 %
Date: 2018-10-19 03:25:38 Functions: 0 16 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*      $OpenBSD: umodem.c,v 1.64 2018/08/29 20:18:14 kettenis 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           0 : 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           0 :         struct usbd_desc_iter iter;
     159             :         int current_iface_no = -1;
     160             :         int data_iface_no = -1;
     161             :         int i;
     162             : 
     163           0 :         *data_iface_idx = -1;
     164           0 :         *cm_cap = *acm_cap = 0;
     165           0 :         usbd_desc_iter_init(uaa->device, &iter);
     166           0 :         desc = usbd_desc_iter_next(&iter);
     167           0 :         while (desc) {
     168           0 :                 if (desc->bDescriptorType == UDESC_INTERFACE) {
     169           0 :                         id = (usb_interface_descriptor_t *)desc;
     170           0 :                         current_iface_no = id->bInterfaceNumber;
     171           0 :                         if (current_iface_no != ctl_iface_no &&
     172           0 :                             id->bInterfaceClass == UICLASS_CDC_DATA &&
     173           0 :                             id->bInterfaceSubClass == UISUBCLASS_DATA &&
     174           0 :                             data_iface_no == -1)
     175           0 :                                 data_iface_no = current_iface_no;
     176             :                 }
     177           0 :                 if (current_iface_no == ctl_iface_no &&
     178           0 :                     desc->bDescriptorType == UDESC_CS_INTERFACE) {
     179           0 :                         switch(desc->bDescriptorSubtype) {
     180             :                         case UDESCSUB_CDC_CM:
     181           0 :                                 cmd = (struct usb_cdc_cm_descriptor *)desc;
     182           0 :                                 *cm_cap = cmd->bmCapabilities;
     183           0 :                                 data_iface_no = cmd->bDataInterface;
     184           0 :                                 break;
     185             :                         case UDESCSUB_CDC_ACM:
     186           0 :                                 acmd = (struct usb_cdc_acm_descriptor *)desc;
     187           0 :                                 *acm_cap = acmd->bmCapabilities;
     188           0 :                                 break;
     189             :                         case UDESCSUB_CDC_UNION:
     190             :                                 uniond =
     191           0 :                                     (struct usb_cdc_union_descriptor *)desc;
     192           0 :                                 data_iface_no = uniond->bSlaveInterface[0];
     193           0 :                                 break;
     194             :                         }
     195             :                 }
     196           0 :                 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           0 :         if (data_iface_no != -1) {
     204           0 :                 for (i = 0; i < uaa->nifaces; i++) {
     205           0 :                         id = usbd_get_interface_descriptor(uaa->ifaces[i]);
     206             : 
     207           0 :                         if (id == NULL)
     208             :                                 continue;
     209             : 
     210           0 :                         if (id->bInterfaceNumber == data_iface_no) {
     211           0 :                                 if (!usbd_iface_claimed(uaa->device, i))
     212           0 :                                         *data_iface_idx = i;
     213             :                                 break;
     214             :                         }
     215             :                 }
     216             :         }
     217           0 : }
     218             : 
     219             : int
     220           0 : umodem_match(struct device *parent, void *match, void *aux)
     221             : {
     222           0 :         struct usb_attach_arg *uaa = aux;
     223             :         usb_interface_descriptor_t *id;
     224             :         usb_device_descriptor_t *dd;
     225           0 :         int data_iface_idx, cm_cap, acm_cap, ret = UMATCH_NONE;
     226             : 
     227           0 :         if (uaa->iface == NULL)
     228           0 :                 return (ret);
     229             : 
     230           0 :         id = usbd_get_interface_descriptor(uaa->iface);
     231           0 :         dd = usbd_get_device_descriptor(uaa->device);
     232           0 :         if (id == NULL || dd == NULL)
     233           0 :                 return (ret);
     234             : 
     235             :         ret = UMATCH_NONE;
     236             : 
     237           0 :         if (UGETW(dd->idVendor) == USB_VENDOR_KYOCERA &&
     238           0 :             UGETW(dd->idProduct) == USB_PRODUCT_KYOCERA_AHK3001V &&
     239           0 :             id->bInterfaceNumber == 0)
     240           0 :                 ret = UMATCH_VENDOR_PRODUCT;
     241             : 
     242           0 :         if (ret == UMATCH_NONE &&
     243           0 :             id->bInterfaceClass == UICLASS_CDC &&
     244           0 :             id->bInterfaceSubClass == UISUBCLASS_ABSTRACT_CONTROL_MODEL &&
     245           0 :             (id->bInterfaceProtocol == UIPROTO_CDC_AT ||
     246           0 :             id->bInterfaceProtocol == UIPROTO_CDC_NOCLASS))
     247           0 :                 ret = UMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO;
     248             : 
     249           0 :         if (ret == UMATCH_NONE)
     250           0 :                 return (ret);
     251             : 
     252             :         /* umodem doesn't support devices without a data iface */
     253           0 :         umodem_get_caps(uaa, id->bInterfaceNumber, &data_iface_idx,
     254             :             &cm_cap, &acm_cap);
     255           0 :         if (data_iface_idx == -1)
     256           0 :                 ret = UMATCH_NONE;
     257             : 
     258           0 :         return (ret);
     259           0 : }
     260             : 
     261             : void
     262           0 : umodem_attach(struct device *parent, struct device *self, void *aux)
     263             : {
     264           0 :         struct umodem_softc *sc = (struct umodem_softc *)self;
     265           0 :         struct usb_attach_arg *uaa = aux;
     266           0 :         struct usbd_device *dev = uaa->device;
     267             :         usb_interface_descriptor_t *id;
     268             :         usb_endpoint_descriptor_t *ed;
     269             :         usbd_status err;
     270           0 :         int data_iface_idx = -1;
     271             :         int i;
     272           0 :         struct ucom_attach_args uca;
     273             : 
     274           0 :         sc->sc_udev = dev;
     275           0 :         sc->sc_ctl_iface = uaa->iface;
     276             : 
     277           0 :         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           0 :         sc->sc_ctl_iface_no = id->bInterfaceNumber;
     281             : 
     282             :         /* Get the capabilities. */
     283           0 :         umodem_get_caps(uaa, id->bInterfaceNumber, &data_iface_idx,
     284           0 :             &sc->sc_cm_cap, &sc->sc_acm_cap);
     285             : 
     286           0 :         usbd_claim_iface(sc->sc_udev, data_iface_idx);
     287           0 :         sc->sc_data_iface = uaa->ifaces[data_iface_idx];
     288           0 :         id = usbd_get_interface_descriptor(sc->sc_data_iface);
     289             : 
     290           0 :         printf("%s: data interface %d, has %sCM over data, has %sbreak\n",
     291           0 :                sc->sc_dev.dv_xname, id->bInterfaceNumber,
     292           0 :                sc->sc_cm_cap & USB_CDC_CM_OVER_DATA ? "" : "no ",
     293           0 :                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           0 :         uca.bulkin = uca.bulkout = -1;
     300             : 
     301           0 :         for (i = 0; i < id->bNumEndpoints; i++) {
     302           0 :                 ed = usbd_interface2endpoint_descriptor(sc->sc_data_iface, i);
     303           0 :                 if (ed == NULL) {
     304           0 :                         printf("%s: no endpoint descriptor for %d\n",
     305             :                                 sc->sc_dev.dv_xname, i);
     306           0 :                         goto bad;
     307             :                 }
     308           0 :                 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
     309           0 :                     (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
     310           0 :                         uca.bulkin = ed->bEndpointAddress;
     311           0 :                 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
     312           0 :                            (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
     313           0 :                         uca.bulkout = ed->bEndpointAddress;
     314           0 :                 }
     315             :         }
     316             : 
     317           0 :         if (uca.bulkin == -1) {
     318           0 :                 printf("%s: Could not find data bulk in\n",
     319             :                        sc->sc_dev.dv_xname);
     320           0 :                 goto bad;
     321             :         }
     322           0 :         if (uca.bulkout == -1) {
     323           0 :                 printf("%s: Could not find data bulk out\n",
     324             :                         sc->sc_dev.dv_xname);
     325           0 :                 goto bad;
     326             :         }
     327             : 
     328           0 :         if (usbd_get_quirks(sc->sc_udev)->uq_flags & UQ_ASSUME_CM_OVER_DATA) {
     329           0 :                 sc->sc_cm_over_data = 1;
     330           0 :         } else {
     331           0 :                 if (sc->sc_cm_cap & USB_CDC_CM_OVER_DATA) {
     332           0 :                         if (sc->sc_acm_cap & USB_CDC_ACM_HAS_FEATURE)
     333           0 :                                 err = umodem_set_comm_feature(sc,
     334             :                                     UCDC_ABSTRACT_STATE, UCDC_DATA_MULTIPLEXED);
     335             :                         else
     336             :                                 err = 0;
     337           0 :                         if (err) {
     338           0 :                                 printf("%s: could not set data multiplex mode\n",
     339             :                                        sc->sc_dev.dv_xname);
     340           0 :                                 goto bad;
     341             :                         }
     342           0 :                         sc->sc_cm_over_data = 1;
     343           0 :                 }
     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           0 :         sc->sc_ctl_notify = -1;
     355           0 :         sc->sc_notify_pipe = NULL;
     356             : 
     357           0 :         id = usbd_get_interface_descriptor(sc->sc_ctl_iface);
     358           0 :         for (i = 0; i < id->bNumEndpoints; i++) {
     359           0 :                 ed = usbd_interface2endpoint_descriptor(sc->sc_ctl_iface, i);
     360           0 :                 if (ed == NULL)
     361             :                         continue;
     362             : 
     363           0 :                 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
     364           0 :                     (ed->bmAttributes & UE_XFERTYPE) == UE_INTERRUPT) {
     365           0 :                         printf("%s: status change notification available\n",
     366             :                                sc->sc_dev.dv_xname);
     367           0 :                         sc->sc_ctl_notify = ed->bEndpointAddress;
     368           0 :                 }
     369             :         }
     370             : 
     371           0 :         sc->sc_dtr = -1;
     372             : 
     373           0 :         uca.portno = UCOM_UNK_PORTNO;
     374             :         /* bulkin, bulkout set above */
     375           0 :         uca.ibufsize = UMODEMIBUFSIZE;
     376           0 :         uca.obufsize = UMODEMOBUFSIZE;
     377           0 :         uca.ibufsizepad = UMODEMIBUFSIZE;
     378           0 :         uca.opkthdrlen = 0;
     379           0 :         uca.device = sc->sc_udev;
     380           0 :         uca.iface = sc->sc_data_iface;
     381           0 :         uca.methods = &umodem_methods;
     382           0 :         uca.arg = sc;
     383           0 :         uca.info = NULL;
     384             : 
     385             :         DPRINTF(("umodem_attach: sc=%p\n", sc));
     386           0 :         sc->sc_subdev = config_found_sm(self, &uca, ucomprint, ucomsubmatch);
     387             : 
     388           0 :         return;
     389             : 
     390             :  bad:
     391           0 :         usbd_deactivate(sc->sc_udev);
     392           0 : }
     393             : 
     394             : int
     395           0 : umodem_open(void *addr, int portno)
     396             : {
     397           0 :         struct umodem_softc *sc = addr;
     398             :         int err;
     399             : 
     400             :         DPRINTF(("umodem_open: sc=%p\n", sc));
     401             : 
     402           0 :         if (sc->sc_ctl_notify != -1 && sc->sc_notify_pipe == NULL) {
     403           0 :                 err = usbd_open_pipe_intr(sc->sc_ctl_iface, sc->sc_ctl_notify,
     404             :                     USBD_SHORT_XFER_OK, &sc->sc_notify_pipe, sc,
     405           0 :                     &sc->sc_notify_buf, sizeof(sc->sc_notify_buf),
     406             :                     umodem_intr, USBD_DEFAULT_INTERVAL);
     407             : 
     408           0 :                 if (err) {
     409             :                         DPRINTF(("Failed to establish notify pipe: %s\n",
     410             :                                 usbd_errstr(err)));
     411           0 :                         return EIO;
     412             :                 }
     413             :         }
     414             : 
     415           0 :         return 0;
     416           0 : }
     417             : 
     418             : void
     419           0 : umodem_close(void *addr, int portno)
     420             : {
     421           0 :         struct umodem_softc *sc = addr;
     422             :         int err;
     423             : 
     424             :         DPRINTF(("umodem_close: sc=%p\n", sc));
     425             : 
     426           0 :         if (sc->sc_notify_pipe != NULL) {
     427           0 :                 usbd_abort_pipe(sc->sc_notify_pipe);
     428           0 :                 err = usbd_close_pipe(sc->sc_notify_pipe);
     429           0 :                 if (err)
     430           0 :                         printf("%s: close notify pipe failed: %s\n",
     431           0 :                             sc->sc_dev.dv_xname, usbd_errstr(err));
     432           0 :                 sc->sc_notify_pipe = NULL;
     433           0 :         }
     434           0 : }
     435             : 
     436             : void
     437           0 : umodem_intr(struct usbd_xfer *xfer, void *priv, usbd_status status)
     438             : {
     439           0 :         struct umodem_softc *sc = priv;
     440             :         u_char mstatus;
     441             : 
     442           0 :         if (usbd_is_dying(sc->sc_udev))
     443           0 :                 return;
     444             : 
     445           0 :         if (status != USBD_NORMAL_COMPLETION) {
     446           0 :                 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
     447           0 :                         return;
     448             :                 DPRINTF(("%s: abnormal status: %s\n", sc->sc_dev.dv_xname,
     449             :                        usbd_errstr(status)));
     450           0 :                 usbd_clear_endpoint_stall_async(sc->sc_notify_pipe);
     451           0 :                 return;
     452             :         }
     453             : 
     454           0 :         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           0 :                 return;
     459             :         }
     460             : 
     461           0 :         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           0 :                 if (UGETW(sc->sc_notify_buf.wLength) != 2) {
     468           0 :                         printf("%s: Invalid notification length! (%d)\n",
     469           0 :                                sc->sc_dev.dv_xname,
     470             :                                UGETW(sc->sc_notify_buf.wLength));
     471           0 :                         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           0 :                 sc->sc_lsr = sc->sc_msr = 0;
     479           0 :                 mstatus = sc->sc_notify_buf.data[0];
     480             : 
     481           0 :                 if (ISSET(mstatus, UCDC_N_SERIAL_RI))
     482           0 :                         sc->sc_msr |= UMSR_RI;
     483           0 :                 if (ISSET(mstatus, UCDC_N_SERIAL_DSR))
     484           0 :                         sc->sc_msr |= UMSR_DSR;
     485           0 :                 if (ISSET(mstatus, UCDC_N_SERIAL_DCD))
     486           0 :                         sc->sc_msr |= UMSR_DCD;
     487           0 :                 ucom_status_change((struct ucom_softc *)sc->sc_subdev);
     488           0 :                 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           0 : }
     496             : 
     497             : void
     498           0 : umodem_get_status(void *addr, int portno, u_char *lsr, u_char *msr)
     499             : {
     500           0 :         struct umodem_softc *sc = addr;
     501             : 
     502             :         DPRINTF(("umodem_get_status:\n"));
     503             : 
     504           0 :         if (lsr != NULL)
     505           0 :                 *lsr = sc->sc_lsr;
     506           0 :         if (msr != NULL)
     507           0 :                 *msr = sc->sc_msr;
     508           0 : }
     509             : 
     510             : int
     511           0 : umodem_param(void *addr, int portno, struct termios *t)
     512             : {
     513           0 :         struct umodem_softc *sc = addr;
     514             :         usbd_status err;
     515           0 :         struct usb_cdc_line_state ls;
     516             : 
     517             :         DPRINTF(("umodem_param: sc=%p\n", sc));
     518             : 
     519           0 :         USETDW(ls.dwDTERate, t->c_ospeed);
     520           0 :         if (ISSET(t->c_cflag, CSTOPB))
     521           0 :                 ls.bCharFormat = UCDC_STOP_BIT_2;
     522             :         else
     523           0 :                 ls.bCharFormat = UCDC_STOP_BIT_1;
     524           0 :         if (ISSET(t->c_cflag, PARENB)) {
     525           0 :                 if (ISSET(t->c_cflag, PARODD))
     526           0 :                         ls.bParityType = UCDC_PARITY_ODD;
     527             :                 else
     528           0 :                         ls.bParityType = UCDC_PARITY_EVEN;
     529             :         } else
     530           0 :                 ls.bParityType = UCDC_PARITY_NONE;
     531           0 :         switch (ISSET(t->c_cflag, CSIZE)) {
     532             :         case CS5:
     533           0 :                 ls.bDataBits = 5;
     534           0 :                 break;
     535             :         case CS6:
     536           0 :                 ls.bDataBits = 6;
     537           0 :                 break;
     538             :         case CS7:
     539           0 :                 ls.bDataBits = 7;
     540           0 :                 break;
     541             :         case CS8:
     542           0 :                 ls.bDataBits = 8;
     543           0 :                 break;
     544             :         }
     545             : 
     546           0 :         err = umodem_set_line_coding(sc, &ls);
     547           0 :         if (err) {
     548             :                 DPRINTF(("umodem_param: err=%s\n", usbd_errstr(err)));
     549           0 :                 return (1);
     550             :         }
     551           0 :         return (0);
     552           0 : }
     553             : 
     554             : void
     555           0 : umodem_dtr(struct umodem_softc *sc, int onoff)
     556             : {
     557             :         DPRINTF(("umodem_dtr: onoff=%d\n", onoff));
     558             : 
     559           0 :         if (sc->sc_dtr == onoff)
     560             :                 return;
     561           0 :         sc->sc_dtr = onoff;
     562             : 
     563           0 :         umodem_set_line_state(sc);
     564           0 : }
     565             : 
     566             : void
     567           0 : umodem_rts(struct umodem_softc *sc, int onoff)
     568             : {
     569             :         DPRINTF(("umodem_rts: onoff=%d\n", onoff));
     570             : 
     571           0 :         if (sc->sc_rts == onoff)
     572             :                 return;
     573           0 :         sc->sc_rts = onoff;
     574             : 
     575           0 :         umodem_set_line_state(sc);
     576           0 : }
     577             : 
     578             : void
     579           0 : umodem_set_line_state(struct umodem_softc *sc)
     580             : {
     581           0 :         usb_device_request_t req;
     582             :         int ls;
     583             : 
     584           0 :         ls = (sc->sc_dtr ? UCDC_LINE_DTR : 0) |
     585           0 :              (sc->sc_rts ? UCDC_LINE_RTS : 0);
     586           0 :         req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
     587           0 :         req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
     588           0 :         USETW(req.wValue, ls);
     589           0 :         USETW(req.wIndex, sc->sc_ctl_iface_no);
     590           0 :         USETW(req.wLength, 0);
     591             : 
     592           0 :         (void)usbd_do_request(sc->sc_udev, &req, 0);
     593             : 
     594           0 : }
     595             : 
     596             : void
     597           0 : umodem_break(struct umodem_softc *sc, int onoff)
     598             : {
     599           0 :         usb_device_request_t req;
     600             : 
     601             :         DPRINTF(("umodem_break: onoff=%d\n", onoff));
     602             : 
     603           0 :         if (!(sc->sc_acm_cap & USB_CDC_ACM_HAS_BREAK))
     604           0 :                 return;
     605             : 
     606           0 :         req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
     607           0 :         req.bRequest = UCDC_SEND_BREAK;
     608           0 :         USETW(req.wValue, onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF);
     609           0 :         USETW(req.wIndex, sc->sc_ctl_iface_no);
     610           0 :         USETW(req.wLength, 0);
     611             : 
     612           0 :         (void)usbd_do_request(sc->sc_udev, &req, 0);
     613           0 : }
     614             : 
     615             : void
     616           0 : umodem_set(void *addr, int portno, int reg, int onoff)
     617             : {
     618           0 :         struct umodem_softc *sc = addr;
     619             : 
     620           0 :         switch (reg) {
     621             :         case UCOM_SET_DTR:
     622           0 :                 umodem_dtr(sc, onoff);
     623           0 :                 break;
     624             :         case UCOM_SET_RTS:
     625           0 :                 umodem_rts(sc, onoff);
     626           0 :                 break;
     627             :         case UCOM_SET_BREAK:
     628           0 :                 umodem_break(sc, onoff);
     629           0 :                 break;
     630             :         default:
     631             :                 break;
     632             :         }
     633           0 : }
     634             : 
     635             : usbd_status
     636           0 : umodem_set_line_coding(struct umodem_softc *sc,
     637             :     struct usb_cdc_line_state *state)
     638             : {
     639           0 :         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           0 :         if (memcmp(state, &sc->sc_line_state, UCDC_LINE_STATE_LENGTH) == 0) {
     647             :                 DPRINTF(("umodem_set_line_coding: already set\n"));
     648           0 :                 return (USBD_NORMAL_COMPLETION);
     649             :         }
     650             : 
     651           0 :         req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
     652           0 :         req.bRequest = UCDC_SET_LINE_CODING;
     653           0 :         USETW(req.wValue, 0);
     654           0 :         USETW(req.wIndex, sc->sc_ctl_iface_no);
     655           0 :         USETW(req.wLength, UCDC_LINE_STATE_LENGTH);
     656             : 
     657           0 :         err = usbd_do_request(sc->sc_udev, &req, state);
     658           0 :         if (err) {
     659             :                 DPRINTF(("umodem_set_line_coding: failed, err=%s\n",
     660             :                          usbd_errstr(err)));
     661           0 :                 return (err);
     662             :         }
     663             : 
     664           0 :         sc->sc_line_state = *state;
     665             : 
     666           0 :         return (USBD_NORMAL_COMPLETION);
     667           0 : }
     668             : 
     669             : usbd_status
     670           0 : umodem_set_comm_feature(struct umodem_softc *sc, int feature, int state)
     671             : {
     672           0 :         usb_device_request_t req;
     673             :         usbd_status err;
     674           0 :         struct usb_cdc_abstract_state ast;
     675             : 
     676             :         DPRINTF(("umodem_set_comm_feature: feature=%d state=%d\n", feature,
     677             :                  state));
     678             : 
     679           0 :         req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
     680           0 :         req.bRequest = UCDC_SET_COMM_FEATURE;
     681           0 :         USETW(req.wValue, feature);
     682           0 :         USETW(req.wIndex, sc->sc_ctl_iface_no);
     683           0 :         USETW(req.wLength, UCDC_ABSTRACT_STATE_LENGTH);
     684           0 :         USETW(ast.wState, state);
     685             : 
     686           0 :         err = usbd_do_request(sc->sc_udev, &req, &ast);
     687           0 :         if (err) {
     688             :                 DPRINTF(("umodem_set_comm_feature: feature=%d, err=%s\n",
     689             :                          feature, usbd_errstr(err)));
     690           0 :                 return (err);
     691             :         }
     692             : 
     693           0 :         return (USBD_NORMAL_COMPLETION);
     694           0 : }
     695             : 
     696             : int
     697           0 : umodem_detach(struct device *self, int flags)
     698             : {
     699           0 :         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           0 :         if (sc->sc_notify_pipe != NULL) {
     705           0 :                 usbd_abort_pipe(sc->sc_notify_pipe);
     706           0 :                 usbd_close_pipe(sc->sc_notify_pipe);
     707           0 :                 sc->sc_notify_pipe = NULL;
     708           0 :         }
     709             : 
     710           0 :         if (sc->sc_subdev != NULL)
     711           0 :                 rv = config_detach(sc->sc_subdev, flags);
     712             : 
     713           0 :         return (rv);
     714             : }

Generated by: LCOV version 1.13