Home | History | Annotate | Line # | Download | only in usb
umodem_common.c revision 1.30
      1 /*	$NetBSD: umodem_common.c,v 1.30 2019/05/09 02:43:35 mrg 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.30 2019/05/09 02:43:35 mrg 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/usbdevs.h>
     72 #include <dev/usb/usb_quirks.h>
     73 
     74 #include <dev/usb/ucomvar.h>
     75 #include <dev/usb/umodemvar.h>
     76 
     77 #ifdef UMODEM_DEBUG
     78 #define DPRINTFN(n, x)	if (umodemdebug > (n)) printf x
     79 int	umodemdebug = 0;
     80 #else
     81 #define DPRINTFN(n, x)
     82 #endif
     83 #define DPRINTF(x) DPRINTFN(0, x)
     84 
     85 /*
     86  * These are the maximum number of bytes transferred per frame.
     87  * If some really high speed devices should use this driver they
     88  * may need to be increased, but this is good enough for normal modems.
     89  *
     90  * Note: increased from 64/256, to better support EVDO wireless PPP.
     91  * The sizes should not be increased further, or there
     92  * will be problems with contiguous storage allocation.
     93  */
     94 #define UMODEMIBUFSIZE 4096
     95 #define UMODEMOBUFSIZE 4096
     96 
     97 static usbd_status umodem_set_comm_feature(struct umodem_softc *,
     98 					   int, int);
     99 static usbd_status umodem_set_line_coding(struct umodem_softc *,
    100 					  usb_cdc_line_state_t *);
    101 
    102 static void	umodem_dtr(struct umodem_softc *, int);
    103 static void	umodem_rts(struct umodem_softc *, int);
    104 static void	umodem_break(struct umodem_softc *, int);
    105 static void	umodem_set_line_state(struct umodem_softc *);
    106 static void	umodem_intr(struct usbd_xfer *, void *, usbd_status);
    107 
    108 int
    109 umodem_common_attach(device_t self, struct umodem_softc *sc,
    110     struct usbif_attach_arg *uiaa, struct ucom_attach_args *ucaa)
    111 {
    112 	struct usbd_device *dev = uiaa->uiaa_device;
    113 	usb_interface_descriptor_t *id;
    114 	usb_endpoint_descriptor_t *ed;
    115 	char *devinfop;
    116 	usbd_status err;
    117 	int data_ifcno;
    118 	int i;
    119 
    120 	sc->sc_dev = self;
    121 	sc->sc_udev = dev;
    122 	sc->sc_ctl_iface = uiaa->uiaa_iface;
    123 	sc->sc_dying = false;
    124 
    125 	aprint_naive("\n");
    126 	aprint_normal("\n");
    127 
    128 	id = usbd_get_interface_descriptor(sc->sc_ctl_iface);
    129 	devinfop = usbd_devinfo_alloc(uiaa->uiaa_device, 0);
    130 	aprint_normal_dev(self, "%s, iclass %d/%d\n",
    131 	       devinfop, id->bInterfaceClass, id->bInterfaceSubClass);
    132 	usbd_devinfo_free(devinfop);
    133 
    134 	sc->sc_ctl_iface_no = id->bInterfaceNumber;
    135 
    136 	/* Get the data interface no. */
    137 	sc->sc_data_iface_no = data_ifcno =
    138 	    umodem_get_caps(dev, &sc->sc_cm_cap, &sc->sc_acm_cap, id);
    139 
    140 	if (data_ifcno == -1) {
    141 		aprint_error_dev(self, "no pointer to data interface\n");
    142 		goto bad;
    143 	}
    144 
    145 	aprint_normal_dev(self,
    146 	    "data interface %d, has %sCM over data, has %sbreak\n",
    147 	    data_ifcno, sc->sc_cm_cap & USB_CDC_CM_OVER_DATA ? "" : "no ",
    148 	    sc->sc_acm_cap & USB_CDC_ACM_HAS_BREAK ? "" : "no ");
    149 
    150 	/* Get the data interface too. */
    151 	for (i = 0; i < uiaa->uiaa_nifaces; i++) {
    152 		if (uiaa->uiaa_ifaces[i] != NULL) {
    153 			id = usbd_get_interface_descriptor(uiaa->uiaa_ifaces[i]);
    154 			if (id != NULL && id->bInterfaceNumber == data_ifcno) {
    155 				sc->sc_data_iface = uiaa->uiaa_ifaces[i];
    156 				uiaa->uiaa_ifaces[i] = NULL;
    157 			}
    158 		}
    159 	}
    160 	if (sc->sc_data_iface == NULL) {
    161 		aprint_error_dev(self, "no data interface\n");
    162 		goto bad;
    163 	}
    164 
    165 	/*
    166 	 * Find the bulk endpoints.
    167 	 * Iterate over all endpoints in the data interface and take note.
    168 	 */
    169 	ucaa->ucaa_bulkin = ucaa->ucaa_bulkout = -1;
    170 
    171 	id = usbd_get_interface_descriptor(sc->sc_data_iface);
    172 	for (i = 0; i < id->bNumEndpoints; i++) {
    173 		ed = usbd_interface2endpoint_descriptor(sc->sc_data_iface, i);
    174 		if (ed == NULL) {
    175 			aprint_error_dev(self,
    176 			    "no endpoint descriptor for %d\n)", i);
    177 			goto bad;
    178 		}
    179 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    180 		    (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
    181 			ucaa->ucaa_bulkin = ed->bEndpointAddress;
    182 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
    183 			   (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
    184 			ucaa->ucaa_bulkout = ed->bEndpointAddress;
    185 		}
    186 	}
    187 
    188 	if (ucaa->ucaa_bulkin == -1) {
    189 		aprint_error_dev(self, "Could not find data bulk in\n");
    190 		goto bad;
    191 	}
    192 	if (ucaa->ucaa_bulkout == -1) {
    193 		aprint_error_dev(self, "Could not find data bulk out\n");
    194 		goto bad;
    195 	}
    196 
    197 	if (usbd_get_quirks(sc->sc_udev)->uq_flags & UQ_ASSUME_CM_OVER_DATA) {
    198 		sc->sc_cm_over_data = 1;
    199 	} else {
    200 		if (sc->sc_cm_cap & USB_CDC_CM_OVER_DATA) {
    201 			if (sc->sc_acm_cap & USB_CDC_ACM_HAS_FEATURE)
    202 				err = umodem_set_comm_feature(sc,
    203 				    UCDC_ABSTRACT_STATE, UCDC_DATA_MULTIPLEXED);
    204 			else
    205 				err = 0;
    206 			if (err) {
    207 				aprint_error_dev(self,
    208 				    "could not set data multiplex mode\n");
    209 				goto bad;
    210 			}
    211 			sc->sc_cm_over_data = 1;
    212 		}
    213 	}
    214 
    215 	/*
    216 	 * The standard allows for notification messages (to indicate things
    217 	 * like a modem hangup) to come in via an interrupt endpoint
    218 	 * off of the control interface.  Iterate over the endpoints on
    219 	 * the control interface and see if there are any interrupt
    220 	 * endpoints; if there are, then register it.
    221 	 */
    222 
    223 	sc->sc_ctl_notify = -1;
    224 	sc->sc_notify_pipe = NULL;
    225 
    226 	id = usbd_get_interface_descriptor(sc->sc_ctl_iface);
    227 	for (i = 0; i < id->bNumEndpoints; i++) {
    228 		ed = usbd_interface2endpoint_descriptor(sc->sc_ctl_iface, i);
    229 		if (ed == NULL)
    230 			continue;
    231 
    232 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    233 		    (ed->bmAttributes & UE_XFERTYPE) == UE_INTERRUPT) {
    234 			aprint_verbose_dev(self,
    235 			    "status change notification available\n");
    236 			sc->sc_ctl_notify = ed->bEndpointAddress;
    237 		}
    238 	}
    239 
    240 	sc->sc_dtr = -1;
    241 
    242 	/* ucaa_bulkin, ucaa_bulkout set above */
    243 	ucaa->ucaa_ibufsize = UMODEMIBUFSIZE;
    244 	ucaa->ucaa_obufsize = UMODEMOBUFSIZE;
    245 	ucaa->ucaa_ibufsizepad = UMODEMIBUFSIZE;
    246 	ucaa->ucaa_opkthdrlen = 0;
    247 	ucaa->ucaa_device = sc->sc_udev;
    248 	ucaa->ucaa_iface = sc->sc_data_iface;
    249 	ucaa->ucaa_arg = sc;
    250 
    251 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, sc->sc_dev);
    252 
    253 	DPRINTF(("umodem_common_attach: sc=%p\n", sc));
    254 	sc->sc_subdev = config_found_sm_loc(self, "ucombus", NULL, ucaa,
    255 					    ucomprint, ucomsubmatch);
    256 
    257 	return 0;
    258 
    259  bad:
    260 	sc->sc_dying = true;
    261 	return 1;
    262 }
    263 
    264 int
    265 umodem_open(void *addr, int portno)
    266 {
    267 	struct umodem_softc *sc = addr;
    268 	int err;
    269 
    270 	if (sc->sc_dying)
    271 		return EIO;
    272 
    273 	DPRINTF(("umodem_open: sc=%p\n", sc));
    274 
    275 	if (sc->sc_ctl_notify != -1 && sc->sc_notify_pipe == NULL) {
    276 		err = usbd_open_pipe_intr(sc->sc_ctl_iface, sc->sc_ctl_notify,
    277 		    USBD_SHORT_XFER_OK, &sc->sc_notify_pipe, sc,
    278 		    &sc->sc_notify_buf, sizeof(sc->sc_notify_buf),
    279 		    umodem_intr, USBD_DEFAULT_INTERVAL);
    280 
    281 		if (err) {
    282 			DPRINTF(("Failed to establish notify pipe: %s\n",
    283 				usbd_errstr(err)));
    284 			return EIO;
    285 		}
    286 	}
    287 
    288 	return 0;
    289 }
    290 
    291 static void
    292 umodem_close_pipe(struct umodem_softc *sc)
    293 {
    294 
    295 	if (sc->sc_notify_pipe != NULL) {
    296 		usbd_abort_pipe(sc->sc_notify_pipe);
    297 		usbd_close_pipe(sc->sc_notify_pipe);
    298 		sc->sc_notify_pipe = NULL;
    299 	}
    300 }
    301 
    302 void
    303 umodem_close(void *addr, int portno)
    304 {
    305 	struct umodem_softc *sc = addr;
    306 
    307 	DPRINTF(("umodem_close: sc=%p\n", sc));
    308 
    309 	if (sc->sc_dying)
    310 		return;
    311 
    312 	umodem_close_pipe(sc);
    313 }
    314 
    315 static void
    316 umodem_intr(struct usbd_xfer *xfer, void *priv,
    317     usbd_status status)
    318 {
    319 	struct umodem_softc *sc = priv;
    320 	u_char mstatus;
    321 
    322 	if (sc->sc_dying)
    323 		return;
    324 
    325 	if (status != USBD_NORMAL_COMPLETION) {
    326 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
    327 			return;
    328 		printf("%s: abnormal status: %s\n", device_xname(sc->sc_dev),
    329 		       usbd_errstr(status));
    330 		if (status == USBD_STALLED)
    331 			usbd_clear_endpoint_stall_async(sc->sc_notify_pipe);
    332 		return;
    333 	}
    334 
    335 	if (sc->sc_notify_buf.bmRequestType != UCDC_NOTIFICATION) {
    336 		DPRINTF(("%s: unknown message type (%02x) on notify pipe\n",
    337 			 device_xname(sc->sc_dev),
    338 			 sc->sc_notify_buf.bmRequestType));
    339 		return;
    340 	}
    341 
    342 	switch (sc->sc_notify_buf.bNotification) {
    343 	case UCDC_N_SERIAL_STATE:
    344 		/*
    345 		 * Set the serial state in ucom driver based on
    346 		 * the bits from the notify message
    347 		 */
    348 		if (UGETW(sc->sc_notify_buf.wLength) != 2) {
    349 			printf("%s: Invalid notification length! (%d)\n",
    350 			       device_xname(sc->sc_dev),
    351 			       UGETW(sc->sc_notify_buf.wLength));
    352 			break;
    353 		}
    354 		DPRINTF(("%s: notify bytes = %02x%02x\n",
    355 			 device_xname(sc->sc_dev),
    356 			 sc->sc_notify_buf.data[0],
    357 			 sc->sc_notify_buf.data[1]));
    358 		/* Currently, lsr is always zero. */
    359 		sc->sc_lsr = sc->sc_msr = 0;
    360 		mstatus = sc->sc_notify_buf.data[0];
    361 
    362 		if (ISSET(mstatus, UCDC_N_SERIAL_RI))
    363 			sc->sc_msr |= UMSR_RI;
    364 		if (ISSET(mstatus, UCDC_N_SERIAL_DSR))
    365 			sc->sc_msr |= UMSR_DSR;
    366 		if (ISSET(mstatus, UCDC_N_SERIAL_DCD))
    367 			sc->sc_msr |= UMSR_DCD;
    368 		ucom_status_change(device_private(sc->sc_subdev));
    369 		break;
    370 	default:
    371 		DPRINTF(("%s: unknown notify message: %02x\n",
    372 			 device_xname(sc->sc_dev),
    373 			 sc->sc_notify_buf.bNotification));
    374 		break;
    375 	}
    376 }
    377 
    378 int
    379 umodem_get_caps(struct usbd_device *dev, int *cm, int *acm,
    380 		usb_interface_descriptor_t *id)
    381 {
    382 	const usb_cdc_cm_descriptor_t *cmd;
    383 	const usb_cdc_acm_descriptor_t *cad;
    384 	const usb_cdc_union_descriptor_t *cud;
    385 	uint32_t uq_flags;
    386 
    387 	*cm = *acm = 0;
    388 	uq_flags = usbd_get_quirks(dev)->uq_flags;
    389 
    390 	if (uq_flags & UQ_NO_UNION_NRM) {
    391 		DPRINTF(("umodem_get_caps: NO_UNION_NRM quirk - returning 0\n"));
    392 		return 0;
    393 	}
    394 
    395 	if (uq_flags & UQ_LOST_CS_DESC)
    396 		id = NULL;
    397 
    398 	cmd = (const usb_cdc_cm_descriptor_t *)usb_find_desc_if(dev,
    399 							  UDESC_CS_INTERFACE,
    400 							  UDESCSUB_CDC_CM, id);
    401 	if (cmd == NULL) {
    402 		DPRINTF(("umodem_get_caps: no CM desc\n"));
    403 	} else {
    404 		*cm = cmd->bmCapabilities;
    405 	}
    406 
    407 	cad = (const usb_cdc_acm_descriptor_t *)usb_find_desc_if(dev,
    408 							   UDESC_CS_INTERFACE,
    409 							   UDESCSUB_CDC_ACM,
    410 							   id);
    411 	if (cad == NULL) {
    412 		DPRINTF(("umodem_get_caps: no ACM desc\n"));
    413 	} else {
    414 		*acm = cad->bmCapabilities;
    415 	}
    416 
    417 	cud = (const usb_cdc_union_descriptor_t *)usb_find_desc_if(dev,
    418 							     UDESC_CS_INTERFACE,
    419 							     UDESCSUB_CDC_UNION,
    420 							     id);
    421 	if (cud == NULL) {
    422 		DPRINTF(("umodem_get_caps: no UNION desc\n"));
    423 	}
    424 
    425 	return cmd ? cmd->bDataInterface : cud ? cud->bSlaveInterface[0] : -1;
    426 }
    427 
    428 void
    429 umodem_get_status(void *addr, int portno, u_char *lsr, u_char *msr)
    430 {
    431 	struct umodem_softc *sc = addr;
    432 
    433 	DPRINTF(("umodem_get_status:\n"));
    434 
    435 	*lsr = sc->sc_lsr;
    436 	*msr = sc->sc_msr;
    437 }
    438 
    439 int
    440 umodem_param(void *addr, int portno, struct termios *t)
    441 {
    442 	struct umodem_softc *sc = addr;
    443 	usbd_status err;
    444 	usb_cdc_line_state_t ls;
    445 
    446 	DPRINTF(("umodem_param: sc=%p\n", sc));
    447 
    448 	if (sc->sc_dying)
    449 		return EIO;
    450 
    451 	USETDW(ls.dwDTERate, t->c_ospeed);
    452 	if (ISSET(t->c_cflag, CSTOPB))
    453 		ls.bCharFormat = UCDC_STOP_BIT_2;
    454 	else
    455 		ls.bCharFormat = UCDC_STOP_BIT_1;
    456 	if (ISSET(t->c_cflag, PARENB)) {
    457 		if (ISSET(t->c_cflag, PARODD))
    458 			ls.bParityType = UCDC_PARITY_ODD;
    459 		else
    460 			ls.bParityType = UCDC_PARITY_EVEN;
    461 	} else
    462 		ls.bParityType = UCDC_PARITY_NONE;
    463 	switch (ISSET(t->c_cflag, CSIZE)) {
    464 	case CS5:
    465 		ls.bDataBits = 5;
    466 		break;
    467 	case CS6:
    468 		ls.bDataBits = 6;
    469 		break;
    470 	case CS7:
    471 		ls.bDataBits = 7;
    472 		break;
    473 	case CS8:
    474 		ls.bDataBits = 8;
    475 		break;
    476 	}
    477 
    478 	err = umodem_set_line_coding(sc, &ls);
    479 	if (err) {
    480 		DPRINTF(("umodem_param: err=%s\n", usbd_errstr(err)));
    481 		return EPASSTHROUGH;
    482 	}
    483 	return 0;
    484 }
    485 
    486 int
    487 umodem_ioctl(void *addr, int portno, u_long cmd, void *data,
    488     int flag, proc_t *p)
    489 {
    490 	struct umodem_softc *sc = addr;
    491 	int error = 0;
    492 
    493 	DPRINTF(("umodem_ioctl: cmd=0x%08lx\n", cmd));
    494 
    495 	if (sc->sc_dying)
    496 		return EIO;
    497 
    498 	switch (cmd) {
    499 	case USB_GET_CM_OVER_DATA:
    500 		*(int *)data = sc->sc_cm_over_data;
    501 		break;
    502 
    503 	case USB_SET_CM_OVER_DATA:
    504 		if (*(int *)data != sc->sc_cm_over_data) {
    505 			/* XXX change it */
    506 		}
    507 		break;
    508 
    509 	default:
    510 		DPRINTF(("umodem_ioctl: unknown\n"));
    511 		error = EPASSTHROUGH;
    512 		break;
    513 	}
    514 
    515 	return error;
    516 }
    517 
    518 static void
    519 umodem_dtr(struct umodem_softc *sc, int onoff)
    520 {
    521 	DPRINTF(("umodem_dtr: onoff=%d\n", onoff));
    522 
    523 	if (sc->sc_dtr == onoff)
    524 		return;
    525 	sc->sc_dtr = onoff;
    526 
    527 	umodem_set_line_state(sc);
    528 }
    529 
    530 static void
    531 umodem_rts(struct umodem_softc *sc, int onoff)
    532 {
    533 	DPRINTF(("umodem_rts: onoff=%d\n", onoff));
    534 
    535 	if (sc->sc_rts == onoff)
    536 		return;
    537 	sc->sc_rts = onoff;
    538 
    539 	umodem_set_line_state(sc);
    540 }
    541 
    542 static void
    543 umodem_set_line_state(struct umodem_softc *sc)
    544 {
    545 	usb_device_request_t req;
    546 	int ls;
    547 
    548 	ls = (sc->sc_dtr ? UCDC_LINE_DTR : 0) |
    549 	     (sc->sc_rts ? UCDC_LINE_RTS : 0);
    550 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
    551 	req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
    552 	USETW(req.wValue, ls);
    553 	USETW(req.wIndex, sc->sc_ctl_iface_no);
    554 	USETW(req.wLength, 0);
    555 
    556 	(void)usbd_do_request(sc->sc_udev, &req, 0);
    557 
    558 }
    559 
    560 static void
    561 umodem_break(struct umodem_softc *sc, int onoff)
    562 {
    563 	usb_device_request_t req;
    564 
    565 	DPRINTF(("umodem_break: onoff=%d\n", onoff));
    566 
    567 	if (!(sc->sc_acm_cap & USB_CDC_ACM_HAS_BREAK))
    568 		return;
    569 
    570 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
    571 	req.bRequest = UCDC_SEND_BREAK;
    572 	USETW(req.wValue, onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF);
    573 	USETW(req.wIndex, sc->sc_ctl_iface_no);
    574 	USETW(req.wLength, 0);
    575 
    576 	(void)usbd_do_request(sc->sc_udev, &req, 0);
    577 }
    578 
    579 void
    580 umodem_set(void *addr, int portno, int reg, int onoff)
    581 {
    582 	struct umodem_softc *sc = addr;
    583 
    584 	if (sc->sc_dying)
    585 		return;
    586 
    587 	switch (reg) {
    588 	case UCOM_SET_DTR:
    589 		umodem_dtr(sc, onoff);
    590 		break;
    591 	case UCOM_SET_RTS:
    592 		umodem_rts(sc, onoff);
    593 		break;
    594 	case UCOM_SET_BREAK:
    595 		umodem_break(sc, onoff);
    596 		break;
    597 	default:
    598 		break;
    599 	}
    600 }
    601 
    602 static usbd_status
    603 umodem_set_line_coding(struct umodem_softc *sc, usb_cdc_line_state_t *state)
    604 {
    605 	usb_device_request_t req;
    606 	usbd_status err;
    607 
    608 	DPRINTF(("umodem_set_line_coding: rate=%d fmt=%d parity=%d bits=%d\n",
    609 		 UGETDW(state->dwDTERate), state->bCharFormat,
    610 		 state->bParityType, state->bDataBits));
    611 
    612 	if (memcmp(state, &sc->sc_line_state, UCDC_LINE_STATE_LENGTH) == 0) {
    613 		DPRINTF(("umodem_set_line_coding: already set\n"));
    614 		return USBD_NORMAL_COMPLETION;
    615 	}
    616 
    617 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
    618 	req.bRequest = UCDC_SET_LINE_CODING;
    619 	USETW(req.wValue, 0);
    620 	USETW(req.wIndex, sc->sc_ctl_iface_no);
    621 	USETW(req.wLength, UCDC_LINE_STATE_LENGTH);
    622 
    623 	err = usbd_do_request(sc->sc_udev, &req, state);
    624 	if (err) {
    625 		DPRINTF(("umodem_set_line_coding: failed, err=%s\n",
    626 			 usbd_errstr(err)));
    627 		return err;
    628 	}
    629 
    630 	sc->sc_line_state = *state;
    631 
    632 	return USBD_NORMAL_COMPLETION;
    633 }
    634 
    635 static usbd_status
    636 umodem_set_comm_feature(struct umodem_softc *sc, int feature, int state)
    637 {
    638 	usb_device_request_t req;
    639 	usbd_status err;
    640 	usb_cdc_abstract_state_t ast;
    641 
    642 	DPRINTF(("umodem_set_comm_feature: feature=%d state=%d\n", feature,
    643 		 state));
    644 
    645 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
    646 	req.bRequest = UCDC_SET_COMM_FEATURE;
    647 	USETW(req.wValue, feature);
    648 	USETW(req.wIndex, sc->sc_ctl_iface_no);
    649 	USETW(req.wLength, UCDC_ABSTRACT_STATE_LENGTH);
    650 	USETW(ast.wState, state);
    651 
    652 	err = usbd_do_request(sc->sc_udev, &req, &ast);
    653 	if (err) {
    654 		DPRINTF(("umodem_set_comm_feature: feature=%d, err=%s\n",
    655 			 feature, usbd_errstr(err)));
    656 		return err;
    657 	}
    658 
    659 	return USBD_NORMAL_COMPLETION;
    660 }
    661 
    662 void
    663 umodem_common_childdet(struct umodem_softc *sc, device_t child)
    664 {
    665 	KASSERT(sc->sc_subdev == child);
    666 	sc->sc_subdev = NULL;
    667 }
    668 
    669 int
    670 umodem_common_detach(struct umodem_softc *sc, int flags)
    671 {
    672 	int rv = 0;
    673 
    674 	DPRINTF(("umodem_common_detach: sc=%p flags=%d\n", sc, flags));
    675 
    676 	sc->sc_dying = true;
    677 
    678 	umodem_close_pipe(sc);
    679 
    680 	if (sc->sc_subdev != NULL) {
    681 		rv = config_detach(sc->sc_subdev, flags);
    682 		sc->sc_subdev = NULL;
    683 	}
    684 
    685 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, sc->sc_dev);
    686 
    687 	return rv;
    688 }
    689