Home | History | Annotate | Line # | Download | only in usb
umodem_common.c revision 1.25
      1 /*	$NetBSD: umodem_common.c,v 1.25 2016/11/25 12:56:29 skrll 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.25 2016/11/25 12:56:29 skrll 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 
    124 	aprint_naive("\n");
    125 	aprint_normal("\n");
    126 
    127 	id = usbd_get_interface_descriptor(sc->sc_ctl_iface);
    128 	devinfop = usbd_devinfo_alloc(uiaa->uiaa_device, 0);
    129 	aprint_normal_dev(self, "%s, iclass %d/%d\n",
    130 	       devinfop, id->bInterfaceClass, id->bInterfaceSubClass);
    131 	usbd_devinfo_free(devinfop);
    132 
    133 	sc->sc_ctl_iface_no = id->bInterfaceNumber;
    134 
    135 	/* Get the data interface no. */
    136 	sc->sc_data_iface_no = data_ifcno =
    137 	    umodem_get_caps(dev, &sc->sc_cm_cap, &sc->sc_acm_cap, id);
    138 
    139 	if (data_ifcno == -1) {
    140 		aprint_error_dev(self, "no pointer to data interface\n");
    141 		goto bad;
    142 	}
    143 
    144 	aprint_normal_dev(self,
    145 	    "data interface %d, has %sCM over data, has %sbreak\n",
    146 	    data_ifcno, sc->sc_cm_cap & USB_CDC_CM_OVER_DATA ? "" : "no ",
    147 	    sc->sc_acm_cap & USB_CDC_ACM_HAS_BREAK ? "" : "no ");
    148 
    149 	/* Get the data interface too. */
    150 	for (i = 0; i < uiaa->uiaa_nifaces; i++) {
    151 		if (uiaa->uiaa_ifaces[i] != NULL) {
    152 			id = usbd_get_interface_descriptor(uiaa->uiaa_ifaces[i]);
    153 			if (id != NULL && id->bInterfaceNumber == data_ifcno) {
    154 				sc->sc_data_iface = uiaa->uiaa_ifaces[i];
    155 				uiaa->uiaa_ifaces[i] = NULL;
    156 			}
    157 		}
    158 	}
    159 	if (sc->sc_data_iface == NULL) {
    160 		aprint_error_dev(self, "no data interface\n");
    161 		goto bad;
    162 	}
    163 
    164 	/*
    165 	 * Find the bulk endpoints.
    166 	 * Iterate over all endpoints in the data interface and take note.
    167 	 */
    168 	ucaa->ucaa_bulkin = ucaa->ucaa_bulkout = -1;
    169 
    170 	id = usbd_get_interface_descriptor(sc->sc_data_iface);
    171 	for (i = 0; i < id->bNumEndpoints; i++) {
    172 		ed = usbd_interface2endpoint_descriptor(sc->sc_data_iface, i);
    173 		if (ed == NULL) {
    174 			aprint_error_dev(self,
    175 			    "no endpoint descriptor for %d\n)", i);
    176 			goto bad;
    177 		}
    178 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    179 		    (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
    180 			ucaa->ucaa_bulkin = ed->bEndpointAddress;
    181 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
    182 			   (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
    183 			ucaa->ucaa_bulkout = ed->bEndpointAddress;
    184 		}
    185 	}
    186 
    187 	if (ucaa->ucaa_bulkin == -1) {
    188 		aprint_error_dev(self, "Could not find data bulk in\n");
    189 		goto bad;
    190 	}
    191 	if (ucaa->ucaa_bulkout == -1) {
    192 		aprint_error_dev(self, "Could not find data bulk out\n");
    193 		goto bad;
    194 	}
    195 
    196 	if (usbd_get_quirks(sc->sc_udev)->uq_flags & UQ_ASSUME_CM_OVER_DATA) {
    197 		sc->sc_cm_over_data = 1;
    198 	} else {
    199 		if (sc->sc_cm_cap & USB_CDC_CM_OVER_DATA) {
    200 			if (sc->sc_acm_cap & USB_CDC_ACM_HAS_FEATURE)
    201 				err = umodem_set_comm_feature(sc,
    202 				    UCDC_ABSTRACT_STATE, UCDC_DATA_MULTIPLEXED);
    203 			else
    204 				err = 0;
    205 			if (err) {
    206 				aprint_error_dev(self,
    207 				    "could not set data multiplex mode\n");
    208 				goto bad;
    209 			}
    210 			sc->sc_cm_over_data = 1;
    211 		}
    212 	}
    213 
    214 	/*
    215 	 * The standard allows for notification messages (to indicate things
    216 	 * like a modem hangup) to come in via an interrupt endpoint
    217 	 * off of the control interface.  Iterate over the endpoints on
    218 	 * the control interface and see if there are any interrupt
    219 	 * endpoints; if there are, then register it.
    220 	 */
    221 
    222 	sc->sc_ctl_notify = -1;
    223 	sc->sc_notify_pipe = NULL;
    224 
    225 	id = usbd_get_interface_descriptor(sc->sc_ctl_iface);
    226 	for (i = 0; i < id->bNumEndpoints; i++) {
    227 		ed = usbd_interface2endpoint_descriptor(sc->sc_ctl_iface, i);
    228 		if (ed == NULL)
    229 			continue;
    230 
    231 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    232 		    (ed->bmAttributes & UE_XFERTYPE) == UE_INTERRUPT) {
    233 			aprint_error_dev(self,
    234 			    "status change notification available\n");
    235 			sc->sc_ctl_notify = ed->bEndpointAddress;
    236 		}
    237 	}
    238 
    239 	sc->sc_dtr = -1;
    240 
    241 	/* ucaa_bulkin, ucaa_bulkout set above */
    242 	ucaa->ucaa_ibufsize = UMODEMIBUFSIZE;
    243 	ucaa->ucaa_obufsize = UMODEMOBUFSIZE;
    244 	ucaa->ucaa_ibufsizepad = UMODEMIBUFSIZE;
    245 	ucaa->ucaa_opkthdrlen = 0;
    246 	ucaa->ucaa_device = sc->sc_udev;
    247 	ucaa->ucaa_iface = sc->sc_data_iface;
    248 	ucaa->ucaa_arg = sc;
    249 
    250 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, sc->sc_dev);
    251 
    252 	DPRINTF(("umodem_common_attach: sc=%p\n", sc));
    253 	sc->sc_subdev = config_found_sm_loc(self, "ucombus", NULL, ucaa,
    254 					    ucomprint, ucomsubmatch);
    255 
    256 	return 0;
    257 
    258  bad:
    259 	sc->sc_dying = 1;
    260 	return 1;
    261 }
    262 
    263 int
    264 umodem_open(void *addr, int portno)
    265 {
    266 	struct umodem_softc *sc = addr;
    267 	int err;
    268 
    269 	DPRINTF(("umodem_open: sc=%p\n", sc));
    270 
    271 	if (sc->sc_ctl_notify != -1 && sc->sc_notify_pipe == NULL) {
    272 		err = usbd_open_pipe_intr(sc->sc_ctl_iface, sc->sc_ctl_notify,
    273 		    USBD_SHORT_XFER_OK, &sc->sc_notify_pipe, sc,
    274 		    &sc->sc_notify_buf, sizeof(sc->sc_notify_buf),
    275 		    umodem_intr, USBD_DEFAULT_INTERVAL);
    276 
    277 		if (err) {
    278 			DPRINTF(("Failed to establish notify pipe: %s\n",
    279 				usbd_errstr(err)));
    280 			return EIO;
    281 		}
    282 	}
    283 
    284 	return 0;
    285 }
    286 
    287 void
    288 umodem_close(void *addr, int portno)
    289 {
    290 	struct umodem_softc *sc = addr;
    291 	int err;
    292 
    293 	DPRINTF(("umodem_close: sc=%p\n", sc));
    294 
    295 	if (sc->sc_notify_pipe != NULL) {
    296 		err = usbd_abort_pipe(sc->sc_notify_pipe);
    297 		if (err)
    298 			printf("%s: abort notify pipe failed: %s\n",
    299 			    device_xname(sc->sc_dev), usbd_errstr(err));
    300 		err = usbd_close_pipe(sc->sc_notify_pipe);
    301 		if (err)
    302 			printf("%s: close notify pipe failed: %s\n",
    303 			    device_xname(sc->sc_dev), usbd_errstr(err));
    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 	if (lsr != NULL)
    429 		*lsr = sc->sc_lsr;
    430 	if (msr != NULL)
    431 		*msr = sc->sc_msr;
    432 }
    433 
    434 int
    435 umodem_param(void *addr, int portno, struct termios *t)
    436 {
    437 	struct umodem_softc *sc = addr;
    438 	usbd_status err;
    439 	usb_cdc_line_state_t ls;
    440 
    441 	DPRINTF(("umodem_param: sc=%p\n", sc));
    442 
    443 	USETDW(ls.dwDTERate, t->c_ospeed);
    444 	if (ISSET(t->c_cflag, CSTOPB))
    445 		ls.bCharFormat = UCDC_STOP_BIT_2;
    446 	else
    447 		ls.bCharFormat = UCDC_STOP_BIT_1;
    448 	if (ISSET(t->c_cflag, PARENB)) {
    449 		if (ISSET(t->c_cflag, PARODD))
    450 			ls.bParityType = UCDC_PARITY_ODD;
    451 		else
    452 			ls.bParityType = UCDC_PARITY_EVEN;
    453 	} else
    454 		ls.bParityType = UCDC_PARITY_NONE;
    455 	switch (ISSET(t->c_cflag, CSIZE)) {
    456 	case CS5:
    457 		ls.bDataBits = 5;
    458 		break;
    459 	case CS6:
    460 		ls.bDataBits = 6;
    461 		break;
    462 	case CS7:
    463 		ls.bDataBits = 7;
    464 		break;
    465 	case CS8:
    466 		ls.bDataBits = 8;
    467 		break;
    468 	}
    469 
    470 	err = umodem_set_line_coding(sc, &ls);
    471 	if (err) {
    472 		DPRINTF(("umodem_param: err=%s\n", usbd_errstr(err)));
    473 		return EPASSTHROUGH;
    474 	}
    475 	return 0;
    476 }
    477 
    478 int
    479 umodem_ioctl(void *addr, int portno, u_long cmd, void *data,
    480     int flag, proc_t *p)
    481 {
    482 	struct umodem_softc *sc = addr;
    483 	int error = 0;
    484 
    485 	if (sc->sc_dying)
    486 		return EIO;
    487 
    488 	DPRINTF(("umodem_ioctl: cmd=0x%08lx\n", cmd));
    489 
    490 	switch (cmd) {
    491 	case USB_GET_CM_OVER_DATA:
    492 		*(int *)data = sc->sc_cm_over_data;
    493 		break;
    494 
    495 	case USB_SET_CM_OVER_DATA:
    496 		if (*(int *)data != sc->sc_cm_over_data) {
    497 			/* XXX change it */
    498 		}
    499 		break;
    500 
    501 	default:
    502 		DPRINTF(("umodem_ioctl: unknown\n"));
    503 		error = EPASSTHROUGH;
    504 		break;
    505 	}
    506 
    507 	return error;
    508 }
    509 
    510 void
    511 umodem_dtr(struct umodem_softc *sc, int onoff)
    512 {
    513 	DPRINTF(("umodem_dtr: onoff=%d\n", onoff));
    514 
    515 	if (sc->sc_dtr == onoff)
    516 		return;
    517 	sc->sc_dtr = onoff;
    518 
    519 	umodem_set_line_state(sc);
    520 }
    521 
    522 void
    523 umodem_rts(struct umodem_softc *sc, int onoff)
    524 {
    525 	DPRINTF(("umodem_rts: onoff=%d\n", onoff));
    526 
    527 	if (sc->sc_rts == onoff)
    528 		return;
    529 	sc->sc_rts = onoff;
    530 
    531 	umodem_set_line_state(sc);
    532 }
    533 
    534 void
    535 umodem_set_line_state(struct umodem_softc *sc)
    536 {
    537 	usb_device_request_t req;
    538 	int ls;
    539 
    540 	ls = (sc->sc_dtr ? UCDC_LINE_DTR : 0) |
    541 	     (sc->sc_rts ? UCDC_LINE_RTS : 0);
    542 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
    543 	req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
    544 	USETW(req.wValue, ls);
    545 	USETW(req.wIndex, sc->sc_ctl_iface_no);
    546 	USETW(req.wLength, 0);
    547 
    548 	(void)usbd_do_request(sc->sc_udev, &req, 0);
    549 
    550 }
    551 
    552 void
    553 umodem_break(struct umodem_softc *sc, int onoff)
    554 {
    555 	usb_device_request_t req;
    556 
    557 	DPRINTF(("umodem_break: onoff=%d\n", onoff));
    558 
    559 	if (!(sc->sc_acm_cap & USB_CDC_ACM_HAS_BREAK))
    560 		return;
    561 
    562 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
    563 	req.bRequest = UCDC_SEND_BREAK;
    564 	USETW(req.wValue, onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF);
    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 void
    572 umodem_set(void *addr, int portno, int reg, int onoff)
    573 {
    574 	struct umodem_softc *sc = addr;
    575 
    576 	switch (reg) {
    577 	case UCOM_SET_DTR:
    578 		umodem_dtr(sc, onoff);
    579 		break;
    580 	case UCOM_SET_RTS:
    581 		umodem_rts(sc, onoff);
    582 		break;
    583 	case UCOM_SET_BREAK:
    584 		umodem_break(sc, onoff);
    585 		break;
    586 	default:
    587 		break;
    588 	}
    589 }
    590 
    591 usbd_status
    592 umodem_set_line_coding(struct umodem_softc *sc, usb_cdc_line_state_t *state)
    593 {
    594 	usb_device_request_t req;
    595 	usbd_status err;
    596 
    597 	DPRINTF(("umodem_set_line_coding: rate=%d fmt=%d parity=%d bits=%d\n",
    598 		 UGETDW(state->dwDTERate), state->bCharFormat,
    599 		 state->bParityType, state->bDataBits));
    600 
    601 	if (memcmp(state, &sc->sc_line_state, UCDC_LINE_STATE_LENGTH) == 0) {
    602 		DPRINTF(("umodem_set_line_coding: already set\n"));
    603 		return USBD_NORMAL_COMPLETION;
    604 	}
    605 
    606 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
    607 	req.bRequest = UCDC_SET_LINE_CODING;
    608 	USETW(req.wValue, 0);
    609 	USETW(req.wIndex, sc->sc_ctl_iface_no);
    610 	USETW(req.wLength, UCDC_LINE_STATE_LENGTH);
    611 
    612 	err = usbd_do_request(sc->sc_udev, &req, state);
    613 	if (err) {
    614 		DPRINTF(("umodem_set_line_coding: failed, err=%s\n",
    615 			 usbd_errstr(err)));
    616 		return err;
    617 	}
    618 
    619 	sc->sc_line_state = *state;
    620 
    621 	return USBD_NORMAL_COMPLETION;
    622 }
    623 
    624 usbd_status
    625 umodem_set_comm_feature(struct umodem_softc *sc, int feature, int state)
    626 {
    627 	usb_device_request_t req;
    628 	usbd_status err;
    629 	usb_cdc_abstract_state_t ast;
    630 
    631 	DPRINTF(("umodem_set_comm_feature: feature=%d state=%d\n", feature,
    632 		 state));
    633 
    634 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
    635 	req.bRequest = UCDC_SET_COMM_FEATURE;
    636 	USETW(req.wValue, feature);
    637 	USETW(req.wIndex, sc->sc_ctl_iface_no);
    638 	USETW(req.wLength, UCDC_ABSTRACT_STATE_LENGTH);
    639 	USETW(ast.wState, state);
    640 
    641 	err = usbd_do_request(sc->sc_udev, &req, &ast);
    642 	if (err) {
    643 		DPRINTF(("umodem_set_comm_feature: feature=%d, err=%s\n",
    644 			 feature, usbd_errstr(err)));
    645 		return err;
    646 	}
    647 
    648 	return USBD_NORMAL_COMPLETION;
    649 }
    650 
    651 int
    652 umodem_common_activate(struct umodem_softc *sc, enum devact act)
    653 {
    654 	switch (act) {
    655 	case DVACT_DEACTIVATE:
    656 		sc->sc_dying = 1;
    657 		return 0;
    658 	default:
    659 		return EOPNOTSUPP;
    660 	}
    661 }
    662 
    663 void
    664 umodem_common_childdet(struct umodem_softc *sc, device_t child)
    665 {
    666 	KASSERT(sc->sc_subdev == child);
    667 	sc->sc_subdev = NULL;
    668 }
    669 
    670 int
    671 umodem_common_detach(struct umodem_softc *sc, int flags)
    672 {
    673 	int rv = 0;
    674 
    675 	DPRINTF(("umodem_common_detach: sc=%p flags=%d\n", sc, flags));
    676 
    677 	sc->sc_dying = 1;
    678 
    679 	if (sc->sc_subdev != NULL)
    680 		rv = config_detach(sc->sc_subdev, flags);
    681 
    682 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, sc->sc_dev);
    683 
    684 	return rv;
    685 }
    686