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