Home | History | Annotate | Line # | Download | only in usb
umodem_common.c revision 1.25.16.1
      1  1.25.16.1  christos /*	$NetBSD: umodem_common.c,v 1.25.16.1 2019/06/10 22:07:34 christos Exp $	*/
      2        1.1     itohy 
      3        1.1     itohy /*
      4        1.1     itohy  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      5        1.1     itohy  * All rights reserved.
      6        1.1     itohy  *
      7        1.1     itohy  * This code is derived from software contributed to The NetBSD Foundation
      8        1.1     itohy  * by Lennart Augustsson (lennart (at) augustsson.net) at
      9        1.1     itohy  * Carlstedt Research & Technology.
     10        1.1     itohy  *
     11        1.1     itohy  * Redistribution and use in source and binary forms, with or without
     12        1.1     itohy  * modification, are permitted provided that the following conditions
     13        1.1     itohy  * are met:
     14        1.1     itohy  * 1. Redistributions of source code must retain the above copyright
     15        1.1     itohy  *    notice, this list of conditions and the following disclaimer.
     16        1.1     itohy  * 2. Redistributions in binary form must reproduce the above copyright
     17        1.1     itohy  *    notice, this list of conditions and the following disclaimer in the
     18        1.1     itohy  *    documentation and/or other materials provided with the distribution.
     19        1.1     itohy  *
     20        1.1     itohy  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21        1.1     itohy  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22        1.1     itohy  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23        1.1     itohy  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24        1.1     itohy  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25        1.1     itohy  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26        1.1     itohy  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27        1.1     itohy  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28        1.1     itohy  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29        1.1     itohy  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30        1.1     itohy  * POSSIBILITY OF SUCH DAMAGE.
     31        1.1     itohy  */
     32        1.1     itohy 
     33        1.1     itohy /*
     34        1.1     itohy  * Comm Class spec:  http://www.usb.org/developers/devclass_docs/usbccs10.pdf
     35        1.1     itohy  *                   http://www.usb.org/developers/devclass_docs/usbcdc11.pdf
     36        1.1     itohy  */
     37        1.1     itohy 
     38        1.1     itohy /*
     39        1.1     itohy  * TODO:
     40        1.1     itohy  * - Add error recovery in various places; the big problem is what
     41        1.1     itohy  *   to do in a callback if there is an error.
     42        1.1     itohy  * - Implement a Call Device for modems without multiplexed commands.
     43        1.1     itohy  *
     44        1.1     itohy  */
     45        1.1     itohy 
     46        1.1     itohy #include <sys/cdefs.h>
     47  1.25.16.1  christos __KERNEL_RCSID(0, "$NetBSD: umodem_common.c,v 1.25.16.1 2019/06/10 22:07:34 christos Exp $");
     48       1.25     skrll 
     49       1.25     skrll #ifdef _KERNEL_OPT
     50       1.25     skrll #include "opt_usb.h"
     51       1.25     skrll #endif
     52        1.1     itohy 
     53        1.1     itohy #include <sys/param.h>
     54        1.1     itohy #include <sys/systm.h>
     55        1.1     itohy #include <sys/kernel.h>
     56        1.1     itohy #include <sys/ioctl.h>
     57        1.1     itohy #include <sys/conf.h>
     58        1.1     itohy #include <sys/tty.h>
     59        1.1     itohy #include <sys/file.h>
     60        1.1     itohy #include <sys/select.h>
     61        1.1     itohy #include <sys/proc.h>
     62        1.1     itohy #include <sys/vnode.h>
     63        1.1     itohy #include <sys/device.h>
     64        1.1     itohy #include <sys/poll.h>
     65        1.1     itohy 
     66        1.1     itohy #include <dev/usb/usb.h>
     67        1.1     itohy #include <dev/usb/usbcdc.h>
     68        1.1     itohy 
     69        1.1     itohy #include <dev/usb/usbdi.h>
     70        1.1     itohy #include <dev/usb/usbdi_util.h>
     71        1.1     itohy #include <dev/usb/usbdevs.h>
     72        1.1     itohy #include <dev/usb/usb_quirks.h>
     73        1.1     itohy 
     74        1.1     itohy #include <dev/usb/ucomvar.h>
     75        1.1     itohy #include <dev/usb/umodemvar.h>
     76        1.1     itohy 
     77        1.1     itohy #ifdef UMODEM_DEBUG
     78       1.22    dyoung #define DPRINTFN(n, x)	if (umodemdebug > (n)) printf x
     79        1.1     itohy int	umodemdebug = 0;
     80        1.1     itohy #else
     81        1.1     itohy #define DPRINTFN(n, x)
     82        1.1     itohy #endif
     83        1.1     itohy #define DPRINTF(x) DPRINTFN(0, x)
     84        1.1     itohy 
     85        1.1     itohy /*
     86        1.1     itohy  * These are the maximum number of bytes transferred per frame.
     87        1.1     itohy  * If some really high speed devices should use this driver they
     88        1.1     itohy  * may need to be increased, but this is good enough for normal modems.
     89        1.7       smb  *
     90        1.7       smb  * Note: increased from 64/256, to better support EVDO wireless PPP.
     91        1.7       smb  * The sizes should not be increased further, or there
     92        1.7       smb  * will be problems with contiguous storage allocation.
     93        1.1     itohy  */
     94        1.7       smb #define UMODEMIBUFSIZE 4096
     95        1.7       smb #define UMODEMOBUFSIZE 4096
     96        1.1     itohy 
     97  1.25.16.1  christos static usbd_status umodem_set_comm_feature(struct umodem_softc *,
     98       1.23     skrll 					   int, int);
     99  1.25.16.1  christos static usbd_status umodem_set_line_coding(struct umodem_softc *,
    100       1.23     skrll 					  usb_cdc_line_state_t *);
    101        1.1     itohy 
    102  1.25.16.1  christos static void	umodem_dtr(struct umodem_softc *, int);
    103  1.25.16.1  christos static void	umodem_rts(struct umodem_softc *, int);
    104  1.25.16.1  christos static void	umodem_break(struct umodem_softc *, int);
    105  1.25.16.1  christos static void	umodem_set_line_state(struct umodem_softc *);
    106  1.25.16.1  christos static void	umodem_intr(struct usbd_xfer *, void *, usbd_status);
    107        1.1     itohy 
    108        1.1     itohy int
    109       1.22    dyoung umodem_common_attach(device_t self, struct umodem_softc *sc,
    110       1.23     skrll     struct usbif_attach_arg *uiaa, struct ucom_attach_args *ucaa)
    111        1.1     itohy {
    112       1.23     skrll 	struct usbd_device *dev = uiaa->uiaa_device;
    113        1.1     itohy 	usb_interface_descriptor_t *id;
    114        1.1     itohy 	usb_endpoint_descriptor_t *ed;
    115        1.4  augustss 	char *devinfop;
    116        1.1     itohy 	usbd_status err;
    117        1.1     itohy 	int data_ifcno;
    118        1.1     itohy 	int i;
    119        1.1     itohy 
    120       1.15      cube 	sc->sc_dev = self;
    121        1.1     itohy 	sc->sc_udev = dev;
    122       1.23     skrll 	sc->sc_ctl_iface = uiaa->uiaa_iface;
    123  1.25.16.1  christos 	sc->sc_dying = false;
    124        1.1     itohy 
    125       1.16    plunky 	aprint_naive("\n");
    126       1.16    plunky 	aprint_normal("\n");
    127       1.16    plunky 
    128        1.1     itohy 	id = usbd_get_interface_descriptor(sc->sc_ctl_iface);
    129       1.23     skrll 	devinfop = usbd_devinfo_alloc(uiaa->uiaa_device, 0);
    130       1.15      cube 	aprint_normal_dev(self, "%s, iclass %d/%d\n",
    131        1.4  augustss 	       devinfop, id->bInterfaceClass, id->bInterfaceSubClass);
    132        1.4  augustss 	usbd_devinfo_free(devinfop);
    133       1.16    plunky 
    134        1.1     itohy 	sc->sc_ctl_iface_no = id->bInterfaceNumber;
    135        1.1     itohy 
    136        1.2     itohy 	/* Get the data interface no. */
    137        1.2     itohy 	sc->sc_data_iface_no = data_ifcno =
    138        1.2     itohy 	    umodem_get_caps(dev, &sc->sc_cm_cap, &sc->sc_acm_cap, id);
    139        1.1     itohy 
    140        1.2     itohy 	if (data_ifcno == -1) {
    141       1.15      cube 		aprint_error_dev(self, "no pointer to data interface\n");
    142        1.1     itohy 		goto bad;
    143        1.1     itohy 	}
    144        1.1     itohy 
    145       1.15      cube 	aprint_normal_dev(self,
    146       1.15      cube 	    "data interface %d, has %sCM over data, has %sbreak\n",
    147       1.15      cube 	    data_ifcno, sc->sc_cm_cap & USB_CDC_CM_OVER_DATA ? "" : "no ",
    148       1.15      cube 	    sc->sc_acm_cap & USB_CDC_ACM_HAS_BREAK ? "" : "no ");
    149        1.1     itohy 
    150        1.1     itohy 	/* Get the data interface too. */
    151       1.23     skrll 	for (i = 0; i < uiaa->uiaa_nifaces; i++) {
    152       1.23     skrll 		if (uiaa->uiaa_ifaces[i] != NULL) {
    153       1.23     skrll 			id = usbd_get_interface_descriptor(uiaa->uiaa_ifaces[i]);
    154        1.1     itohy 			if (id != NULL && id->bInterfaceNumber == data_ifcno) {
    155       1.23     skrll 				sc->sc_data_iface = uiaa->uiaa_ifaces[i];
    156       1.23     skrll 				uiaa->uiaa_ifaces[i] = NULL;
    157        1.1     itohy 			}
    158        1.1     itohy 		}
    159        1.1     itohy 	}
    160        1.1     itohy 	if (sc->sc_data_iface == NULL) {
    161       1.15      cube 		aprint_error_dev(self, "no data interface\n");
    162        1.1     itohy 		goto bad;
    163        1.1     itohy 	}
    164        1.1     itohy 
    165        1.1     itohy 	/*
    166        1.1     itohy 	 * Find the bulk endpoints.
    167        1.1     itohy 	 * Iterate over all endpoints in the data interface and take note.
    168        1.1     itohy 	 */
    169       1.23     skrll 	ucaa->ucaa_bulkin = ucaa->ucaa_bulkout = -1;
    170        1.1     itohy 
    171        1.1     itohy 	id = usbd_get_interface_descriptor(sc->sc_data_iface);
    172        1.1     itohy 	for (i = 0; i < id->bNumEndpoints; i++) {
    173        1.1     itohy 		ed = usbd_interface2endpoint_descriptor(sc->sc_data_iface, i);
    174        1.1     itohy 		if (ed == NULL) {
    175       1.15      cube 			aprint_error_dev(self,
    176       1.15      cube 			    "no endpoint descriptor for %d\n)", i);
    177        1.1     itohy 			goto bad;
    178        1.1     itohy 		}
    179        1.1     itohy 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    180        1.1     itohy 		    (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
    181       1.23     skrll 			ucaa->ucaa_bulkin = ed->bEndpointAddress;
    182        1.3     itohy 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
    183        1.1     itohy 			   (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
    184       1.23     skrll 			ucaa->ucaa_bulkout = ed->bEndpointAddress;
    185        1.3     itohy 		}
    186        1.3     itohy 	}
    187        1.1     itohy 
    188       1.23     skrll 	if (ucaa->ucaa_bulkin == -1) {
    189       1.15      cube 		aprint_error_dev(self, "Could not find data bulk in\n");
    190        1.1     itohy 		goto bad;
    191        1.1     itohy 	}
    192       1.23     skrll 	if (ucaa->ucaa_bulkout == -1) {
    193       1.15      cube 		aprint_error_dev(self, "Could not find data bulk out\n");
    194        1.1     itohy 		goto bad;
    195        1.1     itohy 	}
    196        1.1     itohy 
    197        1.1     itohy 	if (usbd_get_quirks(sc->sc_udev)->uq_flags & UQ_ASSUME_CM_OVER_DATA) {
    198        1.1     itohy 		sc->sc_cm_over_data = 1;
    199        1.1     itohy 	} else {
    200        1.1     itohy 		if (sc->sc_cm_cap & USB_CDC_CM_OVER_DATA) {
    201        1.1     itohy 			if (sc->sc_acm_cap & USB_CDC_ACM_HAS_FEATURE)
    202        1.1     itohy 				err = umodem_set_comm_feature(sc,
    203        1.1     itohy 				    UCDC_ABSTRACT_STATE, UCDC_DATA_MULTIPLEXED);
    204        1.1     itohy 			else
    205        1.1     itohy 				err = 0;
    206        1.1     itohy 			if (err) {
    207       1.15      cube 				aprint_error_dev(self,
    208       1.15      cube 				    "could not set data multiplex mode\n");
    209        1.1     itohy 				goto bad;
    210        1.1     itohy 			}
    211        1.1     itohy 			sc->sc_cm_over_data = 1;
    212        1.1     itohy 		}
    213        1.1     itohy 	}
    214        1.1     itohy 
    215        1.1     itohy 	/*
    216        1.1     itohy 	 * The standard allows for notification messages (to indicate things
    217        1.1     itohy 	 * like a modem hangup) to come in via an interrupt endpoint
    218        1.1     itohy 	 * off of the control interface.  Iterate over the endpoints on
    219        1.1     itohy 	 * the control interface and see if there are any interrupt
    220        1.1     itohy 	 * endpoints; if there are, then register it.
    221        1.1     itohy 	 */
    222        1.1     itohy 
    223        1.1     itohy 	sc->sc_ctl_notify = -1;
    224        1.1     itohy 	sc->sc_notify_pipe = NULL;
    225        1.1     itohy 
    226       1.19  christos 	id = usbd_get_interface_descriptor(sc->sc_ctl_iface);
    227        1.1     itohy 	for (i = 0; i < id->bNumEndpoints; i++) {
    228        1.1     itohy 		ed = usbd_interface2endpoint_descriptor(sc->sc_ctl_iface, i);
    229        1.1     itohy 		if (ed == NULL)
    230        1.1     itohy 			continue;
    231        1.1     itohy 
    232        1.1     itohy 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    233        1.1     itohy 		    (ed->bmAttributes & UE_XFERTYPE) == UE_INTERRUPT) {
    234  1.25.16.1  christos 			aprint_verbose_dev(self,
    235       1.15      cube 			    "status change notification available\n");
    236        1.1     itohy 			sc->sc_ctl_notify = ed->bEndpointAddress;
    237        1.1     itohy 		}
    238        1.1     itohy 	}
    239        1.1     itohy 
    240        1.1     itohy 	sc->sc_dtr = -1;
    241        1.1     itohy 
    242       1.23     skrll 	/* ucaa_bulkin, ucaa_bulkout set above */
    243       1.23     skrll 	ucaa->ucaa_ibufsize = UMODEMIBUFSIZE;
    244       1.23     skrll 	ucaa->ucaa_obufsize = UMODEMOBUFSIZE;
    245       1.23     skrll 	ucaa->ucaa_ibufsizepad = UMODEMIBUFSIZE;
    246       1.23     skrll 	ucaa->ucaa_opkthdrlen = 0;
    247       1.23     skrll 	ucaa->ucaa_device = sc->sc_udev;
    248       1.23     skrll 	ucaa->ucaa_iface = sc->sc_data_iface;
    249       1.23     skrll 	ucaa->ucaa_arg = sc;
    250        1.1     itohy 
    251       1.24   msaitoh 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, sc->sc_dev);
    252        1.1     itohy 
    253        1.1     itohy 	DPRINTF(("umodem_common_attach: sc=%p\n", sc));
    254       1.23     skrll 	sc->sc_subdev = config_found_sm_loc(self, "ucombus", NULL, ucaa,
    255        1.1     itohy 					    ucomprint, ucomsubmatch);
    256        1.1     itohy 
    257        1.1     itohy 	return 0;
    258        1.1     itohy 
    259        1.1     itohy  bad:
    260  1.25.16.1  christos 	sc->sc_dying = true;
    261        1.1     itohy 	return 1;
    262        1.1     itohy }
    263        1.1     itohy 
    264        1.1     itohy int
    265        1.9  christos umodem_open(void *addr, int portno)
    266        1.1     itohy {
    267        1.1     itohy 	struct umodem_softc *sc = addr;
    268        1.1     itohy 	int err;
    269        1.1     itohy 
    270  1.25.16.1  christos 	if (sc->sc_dying)
    271  1.25.16.1  christos 		return EIO;
    272  1.25.16.1  christos 
    273        1.1     itohy 	DPRINTF(("umodem_open: sc=%p\n", sc));
    274        1.1     itohy 
    275        1.1     itohy 	if (sc->sc_ctl_notify != -1 && sc->sc_notify_pipe == NULL) {
    276        1.1     itohy 		err = usbd_open_pipe_intr(sc->sc_ctl_iface, sc->sc_ctl_notify,
    277        1.1     itohy 		    USBD_SHORT_XFER_OK, &sc->sc_notify_pipe, sc,
    278        1.1     itohy 		    &sc->sc_notify_buf, sizeof(sc->sc_notify_buf),
    279        1.1     itohy 		    umodem_intr, USBD_DEFAULT_INTERVAL);
    280        1.1     itohy 
    281        1.1     itohy 		if (err) {
    282        1.1     itohy 			DPRINTF(("Failed to establish notify pipe: %s\n",
    283        1.1     itohy 				usbd_errstr(err)));
    284        1.1     itohy 			return EIO;
    285        1.1     itohy 		}
    286        1.1     itohy 	}
    287        1.1     itohy 
    288        1.1     itohy 	return 0;
    289        1.1     itohy }
    290        1.1     itohy 
    291  1.25.16.1  christos static void
    292  1.25.16.1  christos umodem_close_pipe(struct umodem_softc *sc)
    293  1.25.16.1  christos {
    294  1.25.16.1  christos 
    295  1.25.16.1  christos 	if (sc->sc_notify_pipe != NULL) {
    296  1.25.16.1  christos 		usbd_abort_pipe(sc->sc_notify_pipe);
    297  1.25.16.1  christos 		usbd_close_pipe(sc->sc_notify_pipe);
    298  1.25.16.1  christos 		sc->sc_notify_pipe = NULL;
    299  1.25.16.1  christos 	}
    300  1.25.16.1  christos }
    301  1.25.16.1  christos 
    302        1.1     itohy void
    303        1.9  christos umodem_close(void *addr, int portno)
    304        1.1     itohy {
    305        1.1     itohy 	struct umodem_softc *sc = addr;
    306        1.1     itohy 
    307        1.1     itohy 	DPRINTF(("umodem_close: sc=%p\n", sc));
    308        1.1     itohy 
    309  1.25.16.1  christos 	if (sc->sc_dying)
    310  1.25.16.1  christos 		return;
    311  1.25.16.1  christos 
    312  1.25.16.1  christos 	umodem_close_pipe(sc);
    313        1.1     itohy }
    314        1.1     itohy 
    315  1.25.16.1  christos static void
    316       1.23     skrll umodem_intr(struct usbd_xfer *xfer, void *priv,
    317        1.8  christos     usbd_status status)
    318        1.1     itohy {
    319        1.1     itohy 	struct umodem_softc *sc = priv;
    320        1.1     itohy 	u_char mstatus;
    321        1.1     itohy 
    322        1.1     itohy 	if (sc->sc_dying)
    323        1.1     itohy 		return;
    324        1.1     itohy 
    325        1.1     itohy 	if (status != USBD_NORMAL_COMPLETION) {
    326        1.1     itohy 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
    327        1.1     itohy 			return;
    328       1.22    dyoung 		printf("%s: abnormal status: %s\n", device_xname(sc->sc_dev),
    329        1.1     itohy 		       usbd_errstr(status));
    330       1.21  christos 		if (status == USBD_STALLED)
    331       1.21  christos 			usbd_clear_endpoint_stall_async(sc->sc_notify_pipe);
    332        1.1     itohy 		return;
    333        1.1     itohy 	}
    334        1.1     itohy 
    335        1.1     itohy 	if (sc->sc_notify_buf.bmRequestType != UCDC_NOTIFICATION) {
    336        1.1     itohy 		DPRINTF(("%s: unknown message type (%02x) on notify pipe\n",
    337       1.22    dyoung 			 device_xname(sc->sc_dev),
    338        1.1     itohy 			 sc->sc_notify_buf.bmRequestType));
    339        1.1     itohy 		return;
    340        1.1     itohy 	}
    341        1.1     itohy 
    342        1.1     itohy 	switch (sc->sc_notify_buf.bNotification) {
    343        1.1     itohy 	case UCDC_N_SERIAL_STATE:
    344        1.1     itohy 		/*
    345        1.1     itohy 		 * Set the serial state in ucom driver based on
    346        1.1     itohy 		 * the bits from the notify message
    347        1.1     itohy 		 */
    348        1.1     itohy 		if (UGETW(sc->sc_notify_buf.wLength) != 2) {
    349        1.1     itohy 			printf("%s: Invalid notification length! (%d)\n",
    350       1.22    dyoung 			       device_xname(sc->sc_dev),
    351        1.1     itohy 			       UGETW(sc->sc_notify_buf.wLength));
    352        1.1     itohy 			break;
    353        1.1     itohy 		}
    354        1.1     itohy 		DPRINTF(("%s: notify bytes = %02x%02x\n",
    355       1.22    dyoung 			 device_xname(sc->sc_dev),
    356        1.1     itohy 			 sc->sc_notify_buf.data[0],
    357        1.1     itohy 			 sc->sc_notify_buf.data[1]));
    358        1.1     itohy 		/* Currently, lsr is always zero. */
    359        1.1     itohy 		sc->sc_lsr = sc->sc_msr = 0;
    360        1.1     itohy 		mstatus = sc->sc_notify_buf.data[0];
    361        1.1     itohy 
    362        1.1     itohy 		if (ISSET(mstatus, UCDC_N_SERIAL_RI))
    363        1.1     itohy 			sc->sc_msr |= UMSR_RI;
    364        1.1     itohy 		if (ISSET(mstatus, UCDC_N_SERIAL_DSR))
    365        1.1     itohy 			sc->sc_msr |= UMSR_DSR;
    366        1.1     itohy 		if (ISSET(mstatus, UCDC_N_SERIAL_DCD))
    367        1.1     itohy 			sc->sc_msr |= UMSR_DCD;
    368       1.15      cube 		ucom_status_change(device_private(sc->sc_subdev));
    369        1.1     itohy 		break;
    370        1.1     itohy 	default:
    371        1.1     itohy 		DPRINTF(("%s: unknown notify message: %02x\n",
    372       1.22    dyoung 			 device_xname(sc->sc_dev),
    373        1.1     itohy 			 sc->sc_notify_buf.bNotification));
    374        1.1     itohy 		break;
    375        1.1     itohy 	}
    376        1.1     itohy }
    377        1.1     itohy 
    378        1.2     itohy int
    379       1.23     skrll umodem_get_caps(struct usbd_device *dev, int *cm, int *acm,
    380        1.1     itohy 		usb_interface_descriptor_t *id)
    381        1.1     itohy {
    382        1.1     itohy 	const usb_cdc_cm_descriptor_t *cmd;
    383        1.1     itohy 	const usb_cdc_acm_descriptor_t *cad;
    384        1.2     itohy 	const usb_cdc_union_descriptor_t *cud;
    385       1.23     skrll 	uint32_t uq_flags;
    386        1.1     itohy 
    387        1.1     itohy 	*cm = *acm = 0;
    388       1.20  christos 	uq_flags = usbd_get_quirks(dev)->uq_flags;
    389        1.1     itohy 
    390       1.20  christos 	if (uq_flags & UQ_NO_UNION_NRM) {
    391       1.18    kardel 		DPRINTF(("umodem_get_caps: NO_UNION_NRM quirk - returning 0\n"));
    392       1.18    kardel 		return 0;
    393       1.18    kardel 	}
    394       1.18    kardel 
    395       1.20  christos 	if (uq_flags & UQ_LOST_CS_DESC)
    396       1.20  christos 		id = NULL;
    397       1.20  christos 
    398        1.5  christos 	cmd = (const usb_cdc_cm_descriptor_t *)usb_find_desc_if(dev,
    399        1.1     itohy 							  UDESC_CS_INTERFACE,
    400        1.1     itohy 							  UDESCSUB_CDC_CM, id);
    401        1.1     itohy 	if (cmd == NULL) {
    402        1.3     itohy 		DPRINTF(("umodem_get_caps: no CM desc\n"));
    403        1.2     itohy 	} else {
    404        1.2     itohy 		*cm = cmd->bmCapabilities;
    405        1.1     itohy 	}
    406        1.1     itohy 
    407        1.5  christos 	cad = (const usb_cdc_acm_descriptor_t *)usb_find_desc_if(dev,
    408        1.1     itohy 							   UDESC_CS_INTERFACE,
    409        1.1     itohy 							   UDESCSUB_CDC_ACM,
    410        1.1     itohy 							   id);
    411        1.1     itohy 	if (cad == NULL) {
    412        1.3     itohy 		DPRINTF(("umodem_get_caps: no ACM desc\n"));
    413        1.2     itohy 	} else {
    414        1.2     itohy 		*acm = cad->bmCapabilities;
    415        1.2     itohy 	}
    416        1.2     itohy 
    417        1.5  christos 	cud = (const usb_cdc_union_descriptor_t *)usb_find_desc_if(dev,
    418        1.2     itohy 							     UDESC_CS_INTERFACE,
    419        1.2     itohy 							     UDESCSUB_CDC_UNION,
    420        1.2     itohy 							     id);
    421        1.2     itohy 	if (cud == NULL) {
    422        1.3     itohy 		DPRINTF(("umodem_get_caps: no UNION desc\n"));
    423        1.1     itohy 	}
    424        1.3     itohy 
    425        1.2     itohy 	return cmd ? cmd->bDataInterface : cud ? cud->bSlaveInterface[0] : -1;
    426        1.1     itohy }
    427        1.1     itohy 
    428        1.1     itohy void
    429        1.9  christos umodem_get_status(void *addr, int portno, u_char *lsr, u_char *msr)
    430        1.1     itohy {
    431        1.1     itohy 	struct umodem_softc *sc = addr;
    432        1.1     itohy 
    433        1.1     itohy 	DPRINTF(("umodem_get_status:\n"));
    434        1.1     itohy 
    435  1.25.16.1  christos 	*lsr = sc->sc_lsr;
    436  1.25.16.1  christos 	*msr = sc->sc_msr;
    437        1.1     itohy }
    438        1.1     itohy 
    439        1.1     itohy int
    440        1.9  christos umodem_param(void *addr, int portno, struct termios *t)
    441        1.1     itohy {
    442        1.1     itohy 	struct umodem_softc *sc = addr;
    443        1.1     itohy 	usbd_status err;
    444        1.1     itohy 	usb_cdc_line_state_t ls;
    445        1.1     itohy 
    446        1.1     itohy 	DPRINTF(("umodem_param: sc=%p\n", sc));
    447        1.1     itohy 
    448  1.25.16.1  christos 	if (sc->sc_dying)
    449  1.25.16.1  christos 		return EIO;
    450  1.25.16.1  christos 
    451        1.1     itohy 	USETDW(ls.dwDTERate, t->c_ospeed);
    452        1.1     itohy 	if (ISSET(t->c_cflag, CSTOPB))
    453        1.1     itohy 		ls.bCharFormat = UCDC_STOP_BIT_2;
    454        1.1     itohy 	else
    455        1.1     itohy 		ls.bCharFormat = UCDC_STOP_BIT_1;
    456        1.1     itohy 	if (ISSET(t->c_cflag, PARENB)) {
    457        1.1     itohy 		if (ISSET(t->c_cflag, PARODD))
    458        1.1     itohy 			ls.bParityType = UCDC_PARITY_ODD;
    459        1.1     itohy 		else
    460        1.1     itohy 			ls.bParityType = UCDC_PARITY_EVEN;
    461        1.1     itohy 	} else
    462        1.1     itohy 		ls.bParityType = UCDC_PARITY_NONE;
    463        1.1     itohy 	switch (ISSET(t->c_cflag, CSIZE)) {
    464        1.1     itohy 	case CS5:
    465        1.1     itohy 		ls.bDataBits = 5;
    466        1.1     itohy 		break;
    467        1.1     itohy 	case CS6:
    468        1.1     itohy 		ls.bDataBits = 6;
    469        1.1     itohy 		break;
    470        1.1     itohy 	case CS7:
    471        1.1     itohy 		ls.bDataBits = 7;
    472        1.1     itohy 		break;
    473        1.1     itohy 	case CS8:
    474        1.1     itohy 		ls.bDataBits = 8;
    475        1.1     itohy 		break;
    476        1.1     itohy 	}
    477        1.1     itohy 
    478        1.1     itohy 	err = umodem_set_line_coding(sc, &ls);
    479        1.1     itohy 	if (err) {
    480        1.1     itohy 		DPRINTF(("umodem_param: err=%s\n", usbd_errstr(err)));
    481       1.23     skrll 		return EPASSTHROUGH;
    482        1.1     itohy 	}
    483       1.23     skrll 	return 0;
    484        1.1     itohy }
    485        1.1     itohy 
    486        1.1     itohy int
    487       1.11  christos umodem_ioctl(void *addr, int portno, u_long cmd, void *data,
    488       1.22    dyoung     int flag, proc_t *p)
    489        1.1     itohy {
    490        1.1     itohy 	struct umodem_softc *sc = addr;
    491        1.1     itohy 	int error = 0;
    492        1.1     itohy 
    493  1.25.16.1  christos 	DPRINTF(("umodem_ioctl: cmd=0x%08lx\n", cmd));
    494  1.25.16.1  christos 
    495        1.1     itohy 	if (sc->sc_dying)
    496       1.23     skrll 		return EIO;
    497        1.1     itohy 
    498        1.1     itohy 	switch (cmd) {
    499        1.1     itohy 	case USB_GET_CM_OVER_DATA:
    500        1.1     itohy 		*(int *)data = sc->sc_cm_over_data;
    501        1.1     itohy 		break;
    502        1.1     itohy 
    503        1.1     itohy 	case USB_SET_CM_OVER_DATA:
    504        1.1     itohy 		if (*(int *)data != sc->sc_cm_over_data) {
    505        1.1     itohy 			/* XXX change it */
    506        1.1     itohy 		}
    507        1.1     itohy 		break;
    508        1.1     itohy 
    509        1.1     itohy 	default:
    510        1.3     itohy 		DPRINTF(("umodem_ioctl: unknown\n"));
    511        1.1     itohy 		error = EPASSTHROUGH;
    512        1.1     itohy 		break;
    513        1.1     itohy 	}
    514        1.1     itohy 
    515       1.23     skrll 	return error;
    516        1.1     itohy }
    517        1.1     itohy 
    518  1.25.16.1  christos static void
    519        1.1     itohy umodem_dtr(struct umodem_softc *sc, int onoff)
    520        1.1     itohy {
    521        1.3     itohy 	DPRINTF(("umodem_dtr: onoff=%d\n", onoff));
    522        1.1     itohy 
    523        1.1     itohy 	if (sc->sc_dtr == onoff)
    524        1.1     itohy 		return;
    525        1.1     itohy 	sc->sc_dtr = onoff;
    526        1.1     itohy 
    527        1.1     itohy 	umodem_set_line_state(sc);
    528        1.1     itohy }
    529        1.1     itohy 
    530  1.25.16.1  christos static void
    531        1.1     itohy umodem_rts(struct umodem_softc *sc, int onoff)
    532        1.1     itohy {
    533        1.3     itohy 	DPRINTF(("umodem_rts: onoff=%d\n", onoff));
    534        1.1     itohy 
    535        1.1     itohy 	if (sc->sc_rts == onoff)
    536        1.1     itohy 		return;
    537        1.1     itohy 	sc->sc_rts = onoff;
    538        1.1     itohy 
    539        1.1     itohy 	umodem_set_line_state(sc);
    540        1.1     itohy }
    541        1.1     itohy 
    542  1.25.16.1  christos static void
    543        1.1     itohy umodem_set_line_state(struct umodem_softc *sc)
    544        1.1     itohy {
    545        1.1     itohy 	usb_device_request_t req;
    546        1.1     itohy 	int ls;
    547        1.1     itohy 
    548        1.1     itohy 	ls = (sc->sc_dtr ? UCDC_LINE_DTR : 0) |
    549        1.1     itohy 	     (sc->sc_rts ? UCDC_LINE_RTS : 0);
    550        1.1     itohy 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
    551        1.1     itohy 	req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
    552        1.1     itohy 	USETW(req.wValue, ls);
    553        1.1     itohy 	USETW(req.wIndex, sc->sc_ctl_iface_no);
    554        1.1     itohy 	USETW(req.wLength, 0);
    555        1.1     itohy 
    556        1.1     itohy 	(void)usbd_do_request(sc->sc_udev, &req, 0);
    557        1.1     itohy 
    558        1.1     itohy }
    559        1.1     itohy 
    560  1.25.16.1  christos static void
    561        1.1     itohy umodem_break(struct umodem_softc *sc, int onoff)
    562        1.1     itohy {
    563        1.1     itohy 	usb_device_request_t req;
    564        1.1     itohy 
    565        1.1     itohy 	DPRINTF(("umodem_break: onoff=%d\n", onoff));
    566        1.1     itohy 
    567        1.1     itohy 	if (!(sc->sc_acm_cap & USB_CDC_ACM_HAS_BREAK))
    568        1.1     itohy 		return;
    569        1.1     itohy 
    570        1.1     itohy 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
    571        1.1     itohy 	req.bRequest = UCDC_SEND_BREAK;
    572        1.1     itohy 	USETW(req.wValue, onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF);
    573        1.1     itohy 	USETW(req.wIndex, sc->sc_ctl_iface_no);
    574        1.1     itohy 	USETW(req.wLength, 0);
    575        1.1     itohy 
    576        1.1     itohy 	(void)usbd_do_request(sc->sc_udev, &req, 0);
    577        1.1     itohy }
    578        1.1     itohy 
    579        1.1     itohy void
    580        1.9  christos umodem_set(void *addr, int portno, int reg, int onoff)
    581        1.1     itohy {
    582        1.1     itohy 	struct umodem_softc *sc = addr;
    583        1.1     itohy 
    584  1.25.16.1  christos 	if (sc->sc_dying)
    585  1.25.16.1  christos 		return;
    586  1.25.16.1  christos 
    587        1.1     itohy 	switch (reg) {
    588        1.1     itohy 	case UCOM_SET_DTR:
    589        1.1     itohy 		umodem_dtr(sc, onoff);
    590        1.1     itohy 		break;
    591        1.1     itohy 	case UCOM_SET_RTS:
    592        1.1     itohy 		umodem_rts(sc, onoff);
    593        1.1     itohy 		break;
    594        1.1     itohy 	case UCOM_SET_BREAK:
    595        1.1     itohy 		umodem_break(sc, onoff);
    596        1.1     itohy 		break;
    597        1.1     itohy 	default:
    598        1.1     itohy 		break;
    599        1.1     itohy 	}
    600        1.1     itohy }
    601        1.1     itohy 
    602  1.25.16.1  christos static usbd_status
    603        1.1     itohy umodem_set_line_coding(struct umodem_softc *sc, usb_cdc_line_state_t *state)
    604        1.1     itohy {
    605        1.1     itohy 	usb_device_request_t req;
    606        1.1     itohy 	usbd_status err;
    607        1.1     itohy 
    608        1.1     itohy 	DPRINTF(("umodem_set_line_coding: rate=%d fmt=%d parity=%d bits=%d\n",
    609        1.1     itohy 		 UGETDW(state->dwDTERate), state->bCharFormat,
    610        1.1     itohy 		 state->bParityType, state->bDataBits));
    611        1.1     itohy 
    612        1.1     itohy 	if (memcmp(state, &sc->sc_line_state, UCDC_LINE_STATE_LENGTH) == 0) {
    613        1.1     itohy 		DPRINTF(("umodem_set_line_coding: already set\n"));
    614       1.23     skrll 		return USBD_NORMAL_COMPLETION;
    615        1.1     itohy 	}
    616        1.1     itohy 
    617        1.1     itohy 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
    618        1.1     itohy 	req.bRequest = UCDC_SET_LINE_CODING;
    619        1.1     itohy 	USETW(req.wValue, 0);
    620        1.1     itohy 	USETW(req.wIndex, sc->sc_ctl_iface_no);
    621        1.1     itohy 	USETW(req.wLength, UCDC_LINE_STATE_LENGTH);
    622        1.1     itohy 
    623        1.1     itohy 	err = usbd_do_request(sc->sc_udev, &req, state);
    624        1.1     itohy 	if (err) {
    625        1.1     itohy 		DPRINTF(("umodem_set_line_coding: failed, err=%s\n",
    626        1.1     itohy 			 usbd_errstr(err)));
    627       1.23     skrll 		return err;
    628        1.1     itohy 	}
    629        1.1     itohy 
    630        1.1     itohy 	sc->sc_line_state = *state;
    631        1.1     itohy 
    632       1.23     skrll 	return USBD_NORMAL_COMPLETION;
    633        1.1     itohy }
    634        1.1     itohy 
    635  1.25.16.1  christos static usbd_status
    636        1.1     itohy umodem_set_comm_feature(struct umodem_softc *sc, int feature, int state)
    637        1.1     itohy {
    638        1.1     itohy 	usb_device_request_t req;
    639        1.1     itohy 	usbd_status err;
    640        1.1     itohy 	usb_cdc_abstract_state_t ast;
    641        1.1     itohy 
    642        1.1     itohy 	DPRINTF(("umodem_set_comm_feature: feature=%d state=%d\n", feature,
    643        1.1     itohy 		 state));
    644        1.1     itohy 
    645        1.1     itohy 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
    646        1.1     itohy 	req.bRequest = UCDC_SET_COMM_FEATURE;
    647        1.1     itohy 	USETW(req.wValue, feature);
    648        1.1     itohy 	USETW(req.wIndex, sc->sc_ctl_iface_no);
    649        1.1     itohy 	USETW(req.wLength, UCDC_ABSTRACT_STATE_LENGTH);
    650        1.1     itohy 	USETW(ast.wState, state);
    651        1.1     itohy 
    652        1.1     itohy 	err = usbd_do_request(sc->sc_udev, &req, &ast);
    653        1.1     itohy 	if (err) {
    654        1.1     itohy 		DPRINTF(("umodem_set_comm_feature: feature=%d, err=%s\n",
    655        1.1     itohy 			 feature, usbd_errstr(err)));
    656       1.23     skrll 		return err;
    657        1.1     itohy 	}
    658        1.1     itohy 
    659       1.23     skrll 	return USBD_NORMAL_COMPLETION;
    660        1.1     itohy }
    661        1.1     itohy 
    662       1.13    dyoung void
    663       1.13    dyoung umodem_common_childdet(struct umodem_softc *sc, device_t child)
    664       1.13    dyoung {
    665       1.13    dyoung 	KASSERT(sc->sc_subdev == child);
    666       1.13    dyoung 	sc->sc_subdev = NULL;
    667       1.13    dyoung }
    668       1.13    dyoung 
    669        1.1     itohy int
    670        1.1     itohy umodem_common_detach(struct umodem_softc *sc, int flags)
    671        1.1     itohy {
    672        1.1     itohy 	int rv = 0;
    673        1.1     itohy 
    674        1.1     itohy 	DPRINTF(("umodem_common_detach: sc=%p flags=%d\n", sc, flags));
    675        1.1     itohy 
    676  1.25.16.1  christos 	sc->sc_dying = true;
    677        1.1     itohy 
    678  1.25.16.1  christos 	umodem_close_pipe(sc);
    679  1.25.16.1  christos 
    680  1.25.16.1  christos 	if (sc->sc_subdev != NULL) {
    681        1.1     itohy 		rv = config_detach(sc->sc_subdev, flags);
    682  1.25.16.1  christos 		sc->sc_subdev = NULL;
    683  1.25.16.1  christos 	}
    684        1.1     itohy 
    685       1.24   msaitoh 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, sc->sc_dev);
    686        1.1     itohy 
    687       1.23     skrll 	return rv;
    688        1.1     itohy }
    689