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