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