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