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