Home | History | Annotate | Line # | Download | only in usb
umodem_common.c revision 1.33
      1 /*	$NetBSD: umodem_common.c,v 1.33 2020/04/12 01:10:54 simonb 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 2020/04/12 01:10:54 simonb 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_sm_loc(self, "ucombus", NULL, ucaa,
    267 					    ucomprint, ucomsubmatch);
    268 
    269 	return 0;
    270 
    271  bad:
    272 	sc->sc_dying = true;
    273 	return 1;
    274 }
    275 
    276 int
    277 umodem_open(void *addr, int portno)
    278 {
    279 	struct umodem_softc *sc = addr;
    280 	int err;
    281 
    282 	if (sc->sc_dying)
    283 		return EIO;
    284 
    285 	DPRINTF(("umodem_open: sc=%p\n", sc));
    286 
    287 	if (sc->sc_ctl_notify != -1 && sc->sc_notify_pipe == NULL) {
    288 		err = usbd_open_pipe_intr(sc->sc_ctl_iface, sc->sc_ctl_notify,
    289 		    USBD_SHORT_XFER_OK, &sc->sc_notify_pipe, sc,
    290 		    &sc->sc_notify_buf, sizeof(sc->sc_notify_buf),
    291 		    umodem_intr, USBD_DEFAULT_INTERVAL);
    292 
    293 		if (err) {
    294 			DPRINTF(("Failed to establish notify pipe: %s\n",
    295 				usbd_errstr(err)));
    296 			return EIO;
    297 		}
    298 	}
    299 
    300 	return 0;
    301 }
    302 
    303 static void
    304 umodem_close_pipe(struct umodem_softc *sc)
    305 {
    306 
    307 	if (sc->sc_notify_pipe != NULL) {
    308 		usbd_abort_pipe(sc->sc_notify_pipe);
    309 		usbd_close_pipe(sc->sc_notify_pipe);
    310 		sc->sc_notify_pipe = NULL;
    311 	}
    312 }
    313 
    314 void
    315 umodem_close(void *addr, int portno)
    316 {
    317 	struct umodem_softc *sc = addr;
    318 
    319 	DPRINTF(("umodem_close: sc=%p\n", sc));
    320 
    321 	if (sc->sc_dying)
    322 		return;
    323 
    324 	umodem_close_pipe(sc);
    325 }
    326 
    327 static void
    328 umodem_intr(struct usbd_xfer *xfer, void *priv,
    329     usbd_status status)
    330 {
    331 	struct umodem_softc *sc = priv;
    332 	u_char mstatus;
    333 
    334 	if (sc->sc_dying)
    335 		return;
    336 
    337 	if (status != USBD_NORMAL_COMPLETION) {
    338 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
    339 			return;
    340 		printf("%s: abnormal status: %s\n", device_xname(sc->sc_dev),
    341 		       usbd_errstr(status));
    342 		if (status == USBD_STALLED)
    343 			usbd_clear_endpoint_stall_async(sc->sc_notify_pipe);
    344 		return;
    345 	}
    346 
    347 	if (sc->sc_notify_buf.bmRequestType != UCDC_NOTIFICATION) {
    348 		DPRINTF(("%s: unknown message type (%02x) on notify pipe\n",
    349 			 device_xname(sc->sc_dev),
    350 			 sc->sc_notify_buf.bmRequestType));
    351 		return;
    352 	}
    353 
    354 	switch (sc->sc_notify_buf.bNotification) {
    355 	case UCDC_N_SERIAL_STATE:
    356 		/*
    357 		 * Set the serial state in ucom driver based on
    358 		 * the bits from the notify message
    359 		 */
    360 		if (UGETW(sc->sc_notify_buf.wLength) != 2) {
    361 			printf("%s: Invalid notification length! (%d)\n",
    362 			       device_xname(sc->sc_dev),
    363 			       UGETW(sc->sc_notify_buf.wLength));
    364 			break;
    365 		}
    366 		DPRINTF(("%s: notify bytes = %02x%02x\n",
    367 			 device_xname(sc->sc_dev),
    368 			 sc->sc_notify_buf.data[0],
    369 			 sc->sc_notify_buf.data[1]));
    370 		/* Currently, lsr is always zero. */
    371 	 	sc->sc_lsr = sc->sc_msr = 0;
    372 		mstatus = sc->sc_notify_buf.data[0];
    373 
    374 		if (ISSET(mstatus, UCDC_N_SERIAL_RI))
    375 			sc->sc_msr |= UMSR_RI;
    376 		if (ISSET(mstatus, UCDC_N_SERIAL_DSR))
    377 			sc->sc_msr |= UMSR_DSR;
    378 		if (ISSET(mstatus, UCDC_N_SERIAL_DCD))
    379 			sc->sc_msr |= UMSR_DCD;
    380 		ucom_status_change(device_private(sc->sc_subdev));
    381 		break;
    382 	default:
    383 		DPRINTF(("%s: unknown notify message: %02x\n",
    384 			 device_xname(sc->sc_dev),
    385 			 sc->sc_notify_buf.bNotification));
    386 		break;
    387 	}
    388 }
    389 
    390 int
    391 umodem_get_caps(struct usbd_device *dev, int *cm, int *acm,
    392 		usb_interface_descriptor_t *id)
    393 {
    394 	const usb_cdc_cm_descriptor_t *cmd;
    395 	const usb_cdc_acm_descriptor_t *cad;
    396 	const usb_cdc_union_descriptor_t *cud;
    397 	uint32_t uq_flags;
    398 
    399 	*cm = *acm = 0;
    400 	uq_flags = usbd_get_quirks(dev)->uq_flags;
    401 
    402 	if (uq_flags & UQ_NO_UNION_NRM) {
    403 		DPRINTF(("umodem_get_caps: NO_UNION_NRM quirk - returning 0\n"));
    404 		return 0;
    405 	}
    406 
    407 	if (uq_flags & UQ_LOST_CS_DESC)
    408 		id = NULL;
    409 
    410 	cmd = (const usb_cdc_cm_descriptor_t *)usb_find_desc_if(dev,
    411 							  UDESC_CS_INTERFACE,
    412 							  UDESCSUB_CDC_CM, id);
    413 	if (cmd == NULL) {
    414 		DPRINTF(("umodem_get_caps: no CM desc\n"));
    415 	} else {
    416 		*cm = cmd->bmCapabilities;
    417 	}
    418 
    419 	cad = (const usb_cdc_acm_descriptor_t *)usb_find_desc_if(dev,
    420 							   UDESC_CS_INTERFACE,
    421 							   UDESCSUB_CDC_ACM,
    422 							   id);
    423 	if (cad == NULL) {
    424 		DPRINTF(("umodem_get_caps: no ACM desc\n"));
    425 	} else {
    426 		*acm = cad->bmCapabilities;
    427 	}
    428 
    429 	cud = (const usb_cdc_union_descriptor_t *)usb_find_desc_if(dev,
    430 							     UDESC_CS_INTERFACE,
    431 							     UDESCSUB_CDC_UNION,
    432 							     id);
    433 	if (cud == NULL) {
    434 		DPRINTF(("umodem_get_caps: no UNION desc\n"));
    435 	}
    436 
    437 	return cmd ? cmd->bDataInterface : cud ? cud->bSlaveInterface[0] : -1;
    438 }
    439 
    440 void
    441 umodem_get_status(void *addr, int portno, u_char *lsr, u_char *msr)
    442 {
    443 	struct umodem_softc *sc = addr;
    444 
    445 	DPRINTF(("umodem_get_status:\n"));
    446 
    447 	*lsr = sc->sc_lsr;
    448 	*msr = sc->sc_msr;
    449 }
    450 
    451 int
    452 umodem_param(void *addr, int portno, struct termios *t)
    453 {
    454 	struct umodem_softc *sc = addr;
    455 	usbd_status err;
    456 	usb_cdc_line_state_t ls;
    457 
    458 	DPRINTF(("umodem_param: sc=%p\n", sc));
    459 
    460 	if (sc->sc_dying)
    461 		return EIO;
    462 
    463 	USETDW(ls.dwDTERate, t->c_ospeed);
    464 	if (ISSET(t->c_cflag, CSTOPB))
    465 		ls.bCharFormat = UCDC_STOP_BIT_2;
    466 	else
    467 		ls.bCharFormat = UCDC_STOP_BIT_1;
    468 	if (ISSET(t->c_cflag, PARENB)) {
    469 		if (ISSET(t->c_cflag, PARODD))
    470 			ls.bParityType = UCDC_PARITY_ODD;
    471 		else
    472 			ls.bParityType = UCDC_PARITY_EVEN;
    473 	} else
    474 		ls.bParityType = UCDC_PARITY_NONE;
    475 	switch (ISSET(t->c_cflag, CSIZE)) {
    476 	case CS5:
    477 		ls.bDataBits = 5;
    478 		break;
    479 	case CS6:
    480 		ls.bDataBits = 6;
    481 		break;
    482 	case CS7:
    483 		ls.bDataBits = 7;
    484 		break;
    485 	case CS8:
    486 		ls.bDataBits = 8;
    487 		break;
    488 	}
    489 
    490 	err = umodem_set_line_coding(sc, &ls);
    491 	if (err) {
    492 		DPRINTF(("umodem_param: err=%s\n", usbd_errstr(err)));
    493 		return EPASSTHROUGH;
    494 	}
    495 	return 0;
    496 }
    497 
    498 int
    499 umodem_ioctl(void *addr, int portno, u_long cmd, void *data,
    500     int flag, proc_t *p)
    501 {
    502 	struct umodem_softc *sc = addr;
    503 	int error = 0;
    504 
    505 	DPRINTF(("umodem_ioctl: cmd=0x%08lx\n", cmd));
    506 
    507 	if (sc->sc_dying)
    508 		return EIO;
    509 
    510 	switch (cmd) {
    511 	case USB_GET_CM_OVER_DATA:
    512 		*(int *)data = sc->sc_cm_over_data;
    513 		break;
    514 
    515 	case USB_SET_CM_OVER_DATA:
    516 		if (*(int *)data != sc->sc_cm_over_data) {
    517 			/* XXX change it */
    518 		}
    519 		break;
    520 
    521 	default:
    522 		DPRINTF(("umodem_ioctl: unknown\n"));
    523 		error = EPASSTHROUGH;
    524 		break;
    525 	}
    526 
    527 	return error;
    528 }
    529 
    530 static void
    531 umodem_dtr(struct umodem_softc *sc, int onoff)
    532 {
    533 	DPRINTF(("umodem_dtr: onoff=%d\n", onoff));
    534 
    535 	if (sc->sc_dtr == onoff)
    536 		return;
    537 	sc->sc_dtr = onoff;
    538 
    539 	umodem_set_line_state(sc);
    540 }
    541 
    542 static void
    543 umodem_rts(struct umodem_softc *sc, int onoff)
    544 {
    545 	DPRINTF(("umodem_rts: onoff=%d\n", onoff));
    546 
    547 	if (sc->sc_rts == onoff)
    548 		return;
    549 	sc->sc_rts = onoff;
    550 
    551 	umodem_set_line_state(sc);
    552 }
    553 
    554 static void
    555 umodem_set_line_state(struct umodem_softc *sc)
    556 {
    557 	usb_device_request_t req;
    558 	int ls;
    559 
    560 	ls = (sc->sc_dtr ? UCDC_LINE_DTR : 0) |
    561 	     (sc->sc_rts ? UCDC_LINE_RTS : 0);
    562 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
    563 	req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
    564 	USETW(req.wValue, ls);
    565 	USETW(req.wIndex, sc->sc_ctl_iface_no);
    566 	USETW(req.wLength, 0);
    567 
    568 	(void)usbd_do_request(sc->sc_udev, &req, 0);
    569 
    570 }
    571 
    572 static void
    573 umodem_break(struct umodem_softc *sc, int onoff)
    574 {
    575 	usb_device_request_t req;
    576 
    577 	DPRINTF(("umodem_break: onoff=%d\n", onoff));
    578 
    579 	if (!(sc->sc_acm_cap & USB_CDC_ACM_HAS_BREAK))
    580 		return;
    581 
    582 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
    583 	req.bRequest = UCDC_SEND_BREAK;
    584 	USETW(req.wValue, onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF);
    585 	USETW(req.wIndex, sc->sc_ctl_iface_no);
    586 	USETW(req.wLength, 0);
    587 
    588 	(void)usbd_do_request(sc->sc_udev, &req, 0);
    589 }
    590 
    591 void
    592 umodem_set(void *addr, int portno, int reg, int onoff)
    593 {
    594 	struct umodem_softc *sc = addr;
    595 
    596 	if (sc->sc_dying)
    597 		return;
    598 
    599 	switch (reg) {
    600 	case UCOM_SET_DTR:
    601 		umodem_dtr(sc, onoff);
    602 		break;
    603 	case UCOM_SET_RTS:
    604 		umodem_rts(sc, onoff);
    605 		break;
    606 	case UCOM_SET_BREAK:
    607 		umodem_break(sc, onoff);
    608 		break;
    609 	default:
    610 		break;
    611 	}
    612 }
    613 
    614 static usbd_status
    615 umodem_set_line_coding(struct umodem_softc *sc, usb_cdc_line_state_t *state)
    616 {
    617 	usb_device_request_t req;
    618 	usbd_status err;
    619 
    620 	DPRINTF(("umodem_set_line_coding: rate=%d fmt=%d parity=%d bits=%d\n",
    621 		 UGETDW(state->dwDTERate), state->bCharFormat,
    622 		 state->bParityType, state->bDataBits));
    623 
    624 	if (memcmp(state, &sc->sc_line_state, UCDC_LINE_STATE_LENGTH) == 0) {
    625 		DPRINTF(("umodem_set_line_coding: already set\n"));
    626 		return USBD_NORMAL_COMPLETION;
    627 	}
    628 
    629 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
    630 	req.bRequest = UCDC_SET_LINE_CODING;
    631 	USETW(req.wValue, 0);
    632 	USETW(req.wIndex, sc->sc_ctl_iface_no);
    633 	USETW(req.wLength, UCDC_LINE_STATE_LENGTH);
    634 
    635 	err = usbd_do_request(sc->sc_udev, &req, state);
    636 	if (err) {
    637 		DPRINTF(("umodem_set_line_coding: failed, err=%s\n",
    638 			 usbd_errstr(err)));
    639 		return err;
    640 	}
    641 
    642 	sc->sc_line_state = *state;
    643 
    644 	return USBD_NORMAL_COMPLETION;
    645 }
    646 
    647 static usbd_status
    648 umodem_set_comm_feature(struct umodem_softc *sc, int feature, int state)
    649 {
    650 	usb_device_request_t req;
    651 	usbd_status err;
    652 	usb_cdc_abstract_state_t ast;
    653 
    654 	DPRINTF(("umodem_set_comm_feature: feature=%d state=%d\n", feature,
    655 		 state));
    656 
    657 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
    658 	req.bRequest = UCDC_SET_COMM_FEATURE;
    659 	USETW(req.wValue, feature);
    660 	USETW(req.wIndex, sc->sc_ctl_iface_no);
    661 	USETW(req.wLength, UCDC_ABSTRACT_STATE_LENGTH);
    662 	USETW(ast.wState, state);
    663 
    664 	err = usbd_do_request(sc->sc_udev, &req, &ast);
    665 	if (err) {
    666 		DPRINTF(("umodem_set_comm_feature: feature=%d, err=%s\n",
    667 			 feature, usbd_errstr(err)));
    668 		return err;
    669 	}
    670 
    671 	return USBD_NORMAL_COMPLETION;
    672 }
    673 
    674 void
    675 umodem_common_childdet(struct umodem_softc *sc, device_t child)
    676 {
    677 	KASSERT(sc->sc_subdev == child);
    678 	sc->sc_subdev = NULL;
    679 }
    680 
    681 int
    682 umodem_common_detach(struct umodem_softc *sc, int flags)
    683 {
    684 	int rv = 0;
    685 
    686 	DPRINTF(("umodem_common_detach: sc=%p flags=%d\n", sc, flags));
    687 
    688 	sc->sc_dying = true;
    689 
    690 	umodem_close_pipe(sc);
    691 
    692 	if (sc->sc_subdev != NULL) {
    693 		rv = config_detach(sc->sc_subdev, flags);
    694 		sc->sc_subdev = NULL;
    695 	}
    696 
    697 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, sc->sc_dev);
    698 
    699 	return rv;
    700 }
    701