Home | History | Annotate | Line # | Download | only in usb
umodem_common.c revision 1.1
      1 /*	$NetBSD: umodem_common.c,v 1.1 2005/04/15 14:14:09 itohy 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  * 3. All advertising materials mentioning features or use of this software
     20  *    must display the following acknowledgement:
     21  *        This product includes software developed by the NetBSD
     22  *        Foundation, Inc. and its contributors.
     23  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24  *    contributors may be used to endorse or promote products derived
     25  *    from this software without specific prior written permission.
     26  *
     27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37  * POSSIBILITY OF SUCH DAMAGE.
     38  */
     39 
     40 /*
     41  * Comm Class spec:  http://www.usb.org/developers/devclass_docs/usbccs10.pdf
     42  *                   http://www.usb.org/developers/devclass_docs/usbcdc11.pdf
     43  */
     44 
     45 /*
     46  * TODO:
     47  * - Add error recovery in various places; the big problem is what
     48  *   to do in a callback if there is an error.
     49  * - Implement a Call Device for modems without multiplexed commands.
     50  *
     51  */
     52 
     53 #include <sys/cdefs.h>
     54 __KERNEL_RCSID(0, "$NetBSD: umodem_common.c,v 1.1 2005/04/15 14:14:09 itohy Exp $");
     55 
     56 #include <sys/param.h>
     57 #include <sys/systm.h>
     58 #include <sys/kernel.h>
     59 #include <sys/ioctl.h>
     60 #include <sys/conf.h>
     61 #include <sys/tty.h>
     62 #include <sys/file.h>
     63 #include <sys/select.h>
     64 #include <sys/proc.h>
     65 #include <sys/vnode.h>
     66 #include <sys/device.h>
     67 #include <sys/poll.h>
     68 
     69 #include <dev/usb/usb.h>
     70 #include <dev/usb/usbcdc.h>
     71 
     72 #include <dev/usb/usbdi.h>
     73 #include <dev/usb/usbdi_util.h>
     74 #include <dev/usb/usbdevs.h>
     75 #include <dev/usb/usb_quirks.h>
     76 
     77 #include <dev/usb/usbdevs.h>
     78 #include <dev/usb/ucomvar.h>
     79 #include <dev/usb/umodemvar.h>
     80 
     81 #ifdef UMODEM_DEBUG
     82 #define DPRINTFN(n, x)	if (umodemdebug > (n)) logprintf x
     83 int	umodemdebug = 0;
     84 #else
     85 #define DPRINTFN(n, x)
     86 #endif
     87 #define DPRINTF(x) DPRINTFN(0, x)
     88 
     89 /*
     90  * These are the maximum number of bytes transferred per frame.
     91  * If some really high speed devices should use this driver they
     92  * may need to be increased, but this is good enough for normal modems.
     93  */
     94 #define UMODEMIBUFSIZE 64
     95 #define UMODEMOBUFSIZE 256
     96 
     97 Static usbd_status umodem_set_comm_feature(struct umodem_softc *sc,
     98 					   int feature, int state);
     99 Static usbd_status umodem_set_line_coding(struct umodem_softc *sc,
    100 					  usb_cdc_line_state_t *state);
    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(usbd_xfer_handle, usbd_private_handle, usbd_status);
    107 
    108 int
    109 umodem_common_attach(device_ptr_t self, struct umodem_softc *sc,
    110 		     struct usb_attach_arg *uaa, struct ucom_attach_args *uca)
    111 {
    112 	usbd_device_handle dev = uaa->device;
    113 	usb_interface_descriptor_t *id;
    114 	usb_endpoint_descriptor_t *ed;
    115 	const usb_cdc_cm_descriptor_t *cmd;
    116 	char devinfo[1024];
    117 	usbd_status err;
    118 	int data_ifcno;
    119 	int i;
    120 
    121 	usbd_devinfo(uaa->device, 0, devinfo, sizeof(devinfo));
    122 	USB_ATTACH_SETUP;
    123 
    124 	sc->sc_udev = dev;
    125 	sc->sc_ctl_iface = uaa->iface;
    126 
    127 	id = usbd_get_interface_descriptor(sc->sc_ctl_iface);
    128 	printf("%s: %s, iclass %d/%d\n", USBDEVNAME(sc->sc_dev),
    129 	       devinfo, id->bInterfaceClass, id->bInterfaceSubClass);
    130 	sc->sc_ctl_iface_no = id->bInterfaceNumber;
    131 
    132 	umodem_get_caps(dev, &sc->sc_cm_cap, &sc->sc_acm_cap, id);
    133 
    134 	/* Get the data interface no. */
    135 	cmd = (usb_cdc_cm_descriptor_t *)usb_find_desc_if(dev,
    136 							  UDESC_CS_INTERFACE,
    137 							  UDESCSUB_CDC_CM, id);
    138 	if (cmd == NULL) {
    139 		printf("%s: no CM descriptor\n", USBDEVNAME(sc->sc_dev));
    140 		goto bad;
    141 	}
    142 	sc->sc_data_iface_no = data_ifcno = cmd->bDataInterface;
    143 
    144 	printf("%s: data interface %d, has %sCM over data, has %sbreak\n",
    145 	       USBDEVNAME(sc->sc_dev), data_ifcno,
    146 	       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 < uaa->nifaces; i++) {
    151 		if (uaa->ifaces[i] != NULL) {
    152 			id = usbd_get_interface_descriptor(uaa->ifaces[i]);
    153 			if (id != NULL && id->bInterfaceNumber == data_ifcno) {
    154 				sc->sc_data_iface = uaa->ifaces[i];
    155 				uaa->ifaces[i] = NULL;
    156 			}
    157 		}
    158 	}
    159 	if (sc->sc_data_iface == NULL) {
    160 		printf("%s: no data interface\n", USBDEVNAME(sc->sc_dev));
    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 	uca->bulkin = uca->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 			printf("%s: no endpoint descriptor for %d\n",
    175 				USBDEVNAME(sc->sc_dev), i);
    176 			goto bad;
    177 		}
    178 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    179 		    (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
    180                         uca->bulkin = ed->bEndpointAddress;
    181                 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
    182 			   (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
    183                         uca->bulkout = ed->bEndpointAddress;
    184                 }
    185         }
    186 
    187 	if (uca->bulkin == -1) {
    188 		printf("%s: Could not find data bulk in\n",
    189 		       USBDEVNAME(sc->sc_dev));
    190 		goto bad;
    191 	}
    192 	if (uca->bulkout == -1) {
    193 		printf("%s: Could not find data bulk out\n",
    194 			USBDEVNAME(sc->sc_dev));
    195 		goto bad;
    196 	}
    197 
    198 	if (usbd_get_quirks(sc->sc_udev)->uq_flags & UQ_ASSUME_CM_OVER_DATA) {
    199 		sc->sc_cm_over_data = 1;
    200 	} else {
    201 		if (sc->sc_cm_cap & USB_CDC_CM_OVER_DATA) {
    202 			if (sc->sc_acm_cap & USB_CDC_ACM_HAS_FEATURE)
    203 				err = umodem_set_comm_feature(sc,
    204 				    UCDC_ABSTRACT_STATE, UCDC_DATA_MULTIPLEXED);
    205 			else
    206 				err = 0;
    207 			if (err) {
    208 				printf("%s: could not set data multiplex mode\n",
    209 				       USBDEVNAME(sc->sc_dev));
    210 				goto bad;
    211 			}
    212 			sc->sc_cm_over_data = 1;
    213 		}
    214 	}
    215 
    216 	/*
    217 	 * The standard allows for notification messages (to indicate things
    218 	 * like a modem hangup) to come in via an interrupt endpoint
    219 	 * off of the control interface.  Iterate over the endpoints on
    220 	 * the control interface and see if there are any interrupt
    221 	 * endpoints; if there are, then register it.
    222 	 */
    223 
    224 	sc->sc_ctl_notify = -1;
    225 	sc->sc_notify_pipe = NULL;
    226 
    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 			printf("%s: status change notification available\n",
    235 			       USBDEVNAME(sc->sc_dev));
    236 			sc->sc_ctl_notify = ed->bEndpointAddress;
    237 		}
    238 	}
    239 
    240 	sc->sc_dtr = -1;
    241 
    242 	/* bulkin, bulkout set above */
    243 	uca->ibufsize = UMODEMIBUFSIZE;
    244 	uca->obufsize = UMODEMOBUFSIZE;
    245 	uca->ibufsizepad = UMODEMIBUFSIZE;
    246 	uca->opkthdrlen = 0;
    247 	uca->device = sc->sc_udev;
    248 	uca->iface = sc->sc_data_iface;
    249 	uca->arg = sc;
    250 
    251 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
    252 			   USBDEV(sc->sc_dev));
    253 
    254 	DPRINTF(("umodem_common_attach: sc=%p\n", sc));
    255 	sc->sc_subdev = config_found_sm_loc(self, "ucombus", NULL, uca,
    256 					    ucomprint, ucomsubmatch);
    257 
    258 	return 0;
    259 
    260  bad:
    261 	sc->sc_dying = 1;
    262 	return 1;
    263 }
    264 
    265 int
    266 umodem_open(void *addr, int portno)
    267 {
    268 	struct umodem_softc *sc = addr;
    269 	int err;
    270 
    271 	DPRINTF(("umodem_open: sc=%p\n", sc));
    272 
    273 	if (sc->sc_ctl_notify != -1 && sc->sc_notify_pipe == NULL) {
    274 		err = usbd_open_pipe_intr(sc->sc_ctl_iface, sc->sc_ctl_notify,
    275 		    USBD_SHORT_XFER_OK, &sc->sc_notify_pipe, sc,
    276 		    &sc->sc_notify_buf, sizeof(sc->sc_notify_buf),
    277 		    umodem_intr, USBD_DEFAULT_INTERVAL);
    278 
    279 		if (err) {
    280 			DPRINTF(("Failed to establish notify pipe: %s\n",
    281 				usbd_errstr(err)));
    282 			return EIO;
    283 		}
    284 	}
    285 
    286 	return 0;
    287 }
    288 
    289 void
    290 umodem_close(void *addr, int portno)
    291 {
    292 	struct umodem_softc *sc = addr;
    293 	int err;
    294 
    295 	DPRINTF(("umodem_close: sc=%p\n", sc));
    296 
    297 	if (sc->sc_notify_pipe != NULL) {
    298 		err = usbd_abort_pipe(sc->sc_notify_pipe);
    299 		if (err)
    300 			printf("%s: abort notify pipe failed: %s\n",
    301 			    USBDEVNAME(sc->sc_dev), usbd_errstr(err));
    302 		err = usbd_close_pipe(sc->sc_notify_pipe);
    303 		if (err)
    304 			printf("%s: close notify pipe failed: %s\n",
    305 			    USBDEVNAME(sc->sc_dev), usbd_errstr(err));
    306 		sc->sc_notify_pipe = NULL;
    307 	}
    308 }
    309 
    310 Static void
    311 umodem_intr(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
    312 {
    313 	struct umodem_softc *sc = priv;
    314 	u_char mstatus;
    315 
    316 	if (sc->sc_dying)
    317 		return;
    318 
    319 	if (status != USBD_NORMAL_COMPLETION) {
    320 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
    321 			return;
    322 		printf("%s: abnormal status: %s\n", USBDEVNAME(sc->sc_dev),
    323 		       usbd_errstr(status));
    324 		return;
    325 	}
    326 
    327 	if (sc->sc_notify_buf.bmRequestType != UCDC_NOTIFICATION) {
    328 		DPRINTF(("%s: unknown message type (%02x) on notify pipe\n",
    329 			 USBDEVNAME(sc->sc_dev),
    330 			 sc->sc_notify_buf.bmRequestType));
    331 		return;
    332 	}
    333 
    334 	switch (sc->sc_notify_buf.bNotification) {
    335 	case UCDC_N_SERIAL_STATE:
    336 		/*
    337 		 * Set the serial state in ucom driver based on
    338 		 * the bits from the notify message
    339 		 */
    340 		if (UGETW(sc->sc_notify_buf.wLength) != 2) {
    341 			printf("%s: Invalid notification length! (%d)\n",
    342 			       USBDEVNAME(sc->sc_dev),
    343 			       UGETW(sc->sc_notify_buf.wLength));
    344 			break;
    345 		}
    346 		DPRINTF(("%s: notify bytes = %02x%02x\n",
    347 			 USBDEVNAME(sc->sc_dev),
    348 			 sc->sc_notify_buf.data[0],
    349 			 sc->sc_notify_buf.data[1]));
    350 		/* Currently, lsr is always zero. */
    351 		sc->sc_lsr = sc->sc_msr = 0;
    352 		mstatus = sc->sc_notify_buf.data[0];
    353 
    354 		if (ISSET(mstatus, UCDC_N_SERIAL_RI))
    355 			sc->sc_msr |= UMSR_RI;
    356 		if (ISSET(mstatus, UCDC_N_SERIAL_DSR))
    357 			sc->sc_msr |= UMSR_DSR;
    358 		if (ISSET(mstatus, UCDC_N_SERIAL_DCD))
    359 			sc->sc_msr |= UMSR_DCD;
    360 		ucom_status_change((struct ucom_softc *)sc->sc_subdev);
    361 		break;
    362 	default:
    363 		DPRINTF(("%s: unknown notify message: %02x\n",
    364 			 USBDEVNAME(sc->sc_dev),
    365 			 sc->sc_notify_buf.bNotification));
    366 		break;
    367 	}
    368 }
    369 
    370 void
    371 umodem_get_caps(usbd_device_handle dev, int *cm, int *acm,
    372 		usb_interface_descriptor_t *id)
    373 {
    374 	const usb_cdc_cm_descriptor_t *cmd;
    375 	const usb_cdc_acm_descriptor_t *cad;
    376 
    377 	*cm = *acm = 0;
    378 
    379 	cmd = (usb_cdc_cm_descriptor_t *)usb_find_desc_if(dev,
    380 							  UDESC_CS_INTERFACE,
    381 							  UDESCSUB_CDC_CM, id);
    382 	if (cmd == NULL) {
    383 		DPRINTF(("umodem_get_desc: no CM desc\n"));
    384 		return;
    385 	}
    386 	*cm = cmd->bmCapabilities;
    387 
    388 	cad = (usb_cdc_acm_descriptor_t *)usb_find_desc_if(dev,
    389 							   UDESC_CS_INTERFACE,
    390 							   UDESCSUB_CDC_ACM,
    391 							   id);
    392 	if (cad == NULL) {
    393 		DPRINTF(("umodem_get_desc: no ACM desc\n"));
    394 		return;
    395 	}
    396 	*acm = cad->bmCapabilities;
    397 }
    398 
    399 void
    400 umodem_get_status(void *addr, int portno, u_char *lsr, u_char *msr)
    401 {
    402 	struct umodem_softc *sc = addr;
    403 
    404 	DPRINTF(("umodem_get_status:\n"));
    405 
    406 	if (lsr != NULL)
    407 		*lsr = sc->sc_lsr;
    408 	if (msr != NULL)
    409 		*msr = sc->sc_msr;
    410 }
    411 
    412 int
    413 umodem_param(void *addr, int portno, struct termios *t)
    414 {
    415 	struct umodem_softc *sc = addr;
    416 	usbd_status err;
    417 	usb_cdc_line_state_t ls;
    418 
    419 	DPRINTF(("umodem_param: sc=%p\n", sc));
    420 
    421 	USETDW(ls.dwDTERate, t->c_ospeed);
    422 	if (ISSET(t->c_cflag, CSTOPB))
    423 		ls.bCharFormat = UCDC_STOP_BIT_2;
    424 	else
    425 		ls.bCharFormat = UCDC_STOP_BIT_1;
    426 	if (ISSET(t->c_cflag, PARENB)) {
    427 		if (ISSET(t->c_cflag, PARODD))
    428 			ls.bParityType = UCDC_PARITY_ODD;
    429 		else
    430 			ls.bParityType = UCDC_PARITY_EVEN;
    431 	} else
    432 		ls.bParityType = UCDC_PARITY_NONE;
    433 	switch (ISSET(t->c_cflag, CSIZE)) {
    434 	case CS5:
    435 		ls.bDataBits = 5;
    436 		break;
    437 	case CS6:
    438 		ls.bDataBits = 6;
    439 		break;
    440 	case CS7:
    441 		ls.bDataBits = 7;
    442 		break;
    443 	case CS8:
    444 		ls.bDataBits = 8;
    445 		break;
    446 	}
    447 
    448 	err = umodem_set_line_coding(sc, &ls);
    449 	if (err) {
    450 		DPRINTF(("umodem_param: err=%s\n", usbd_errstr(err)));
    451 		return (EPASSTHROUGH);
    452 	}
    453 	return (0);
    454 }
    455 
    456 int
    457 umodem_ioctl(void *addr, int portno, u_long cmd, caddr_t data, int flag,
    458 	     usb_proc_ptr p)
    459 {
    460 	struct umodem_softc *sc = addr;
    461 	int error = 0;
    462 
    463 	if (sc->sc_dying)
    464 		return (EIO);
    465 
    466 	DPRINTF(("umodemioctl: cmd=0x%08lx\n", cmd));
    467 
    468 	switch (cmd) {
    469 	case USB_GET_CM_OVER_DATA:
    470 		*(int *)data = sc->sc_cm_over_data;
    471 		break;
    472 
    473 	case USB_SET_CM_OVER_DATA:
    474 		if (*(int *)data != sc->sc_cm_over_data) {
    475 			/* XXX change it */
    476 		}
    477 		break;
    478 
    479 	default:
    480 		DPRINTF(("umodemioctl: unknown\n"));
    481 		error = EPASSTHROUGH;
    482 		break;
    483 	}
    484 
    485 	return (error);
    486 }
    487 
    488 void
    489 umodem_dtr(struct umodem_softc *sc, int onoff)
    490 {
    491 	DPRINTF(("umodem_modem: onoff=%d\n", onoff));
    492 
    493 	if (sc->sc_dtr == onoff)
    494 		return;
    495 	sc->sc_dtr = onoff;
    496 
    497 	umodem_set_line_state(sc);
    498 }
    499 
    500 void
    501 umodem_rts(struct umodem_softc *sc, int onoff)
    502 {
    503 	DPRINTF(("umodem_modem: onoff=%d\n", onoff));
    504 
    505 	if (sc->sc_rts == onoff)
    506 		return;
    507 	sc->sc_rts = onoff;
    508 
    509 	umodem_set_line_state(sc);
    510 }
    511 
    512 void
    513 umodem_set_line_state(struct umodem_softc *sc)
    514 {
    515 	usb_device_request_t req;
    516 	int ls;
    517 
    518 	ls = (sc->sc_dtr ? UCDC_LINE_DTR : 0) |
    519 	     (sc->sc_rts ? UCDC_LINE_RTS : 0);
    520 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
    521 	req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
    522 	USETW(req.wValue, ls);
    523 	USETW(req.wIndex, sc->sc_ctl_iface_no);
    524 	USETW(req.wLength, 0);
    525 
    526 	(void)usbd_do_request(sc->sc_udev, &req, 0);
    527 
    528 }
    529 
    530 void
    531 umodem_break(struct umodem_softc *sc, int onoff)
    532 {
    533 	usb_device_request_t req;
    534 
    535 	DPRINTF(("umodem_break: onoff=%d\n", onoff));
    536 
    537 	if (!(sc->sc_acm_cap & USB_CDC_ACM_HAS_BREAK))
    538 		return;
    539 
    540 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
    541 	req.bRequest = UCDC_SEND_BREAK;
    542 	USETW(req.wValue, onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF);
    543 	USETW(req.wIndex, sc->sc_ctl_iface_no);
    544 	USETW(req.wLength, 0);
    545 
    546 	(void)usbd_do_request(sc->sc_udev, &req, 0);
    547 }
    548 
    549 void
    550 umodem_set(void *addr, int portno, int reg, int onoff)
    551 {
    552 	struct umodem_softc *sc = addr;
    553 
    554 	switch (reg) {
    555 	case UCOM_SET_DTR:
    556 		umodem_dtr(sc, onoff);
    557 		break;
    558 	case UCOM_SET_RTS:
    559 		umodem_rts(sc, onoff);
    560 		break;
    561 	case UCOM_SET_BREAK:
    562 		umodem_break(sc, onoff);
    563 		break;
    564 	default:
    565 		break;
    566 	}
    567 }
    568 
    569 usbd_status
    570 umodem_set_line_coding(struct umodem_softc *sc, usb_cdc_line_state_t *state)
    571 {
    572 	usb_device_request_t req;
    573 	usbd_status err;
    574 
    575 	DPRINTF(("umodem_set_line_coding: rate=%d fmt=%d parity=%d bits=%d\n",
    576 		 UGETDW(state->dwDTERate), state->bCharFormat,
    577 		 state->bParityType, state->bDataBits));
    578 
    579 	if (memcmp(state, &sc->sc_line_state, UCDC_LINE_STATE_LENGTH) == 0) {
    580 		DPRINTF(("umodem_set_line_coding: already set\n"));
    581 		return (USBD_NORMAL_COMPLETION);
    582 	}
    583 
    584 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
    585 	req.bRequest = UCDC_SET_LINE_CODING;
    586 	USETW(req.wValue, 0);
    587 	USETW(req.wIndex, sc->sc_ctl_iface_no);
    588 	USETW(req.wLength, UCDC_LINE_STATE_LENGTH);
    589 
    590 	err = usbd_do_request(sc->sc_udev, &req, state);
    591 	if (err) {
    592 		DPRINTF(("umodem_set_line_coding: failed, err=%s\n",
    593 			 usbd_errstr(err)));
    594 		return (err);
    595 	}
    596 
    597 	sc->sc_line_state = *state;
    598 
    599 	return (USBD_NORMAL_COMPLETION);
    600 }
    601 
    602 usbd_status
    603 umodem_set_comm_feature(struct umodem_softc *sc, int feature, int state)
    604 {
    605 	usb_device_request_t req;
    606 	usbd_status err;
    607 	usb_cdc_abstract_state_t ast;
    608 
    609 	DPRINTF(("umodem_set_comm_feature: feature=%d state=%d\n", feature,
    610 		 state));
    611 
    612 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
    613 	req.bRequest = UCDC_SET_COMM_FEATURE;
    614 	USETW(req.wValue, feature);
    615 	USETW(req.wIndex, sc->sc_ctl_iface_no);
    616 	USETW(req.wLength, UCDC_ABSTRACT_STATE_LENGTH);
    617 	USETW(ast.wState, state);
    618 
    619 	err = usbd_do_request(sc->sc_udev, &req, &ast);
    620 	if (err) {
    621 		DPRINTF(("umodem_set_comm_feature: feature=%d, err=%s\n",
    622 			 feature, usbd_errstr(err)));
    623 		return (err);
    624 	}
    625 
    626 	return (USBD_NORMAL_COMPLETION);
    627 }
    628 
    629 int
    630 umodem_common_activate(struct umodem_softc *sc, enum devact act)
    631 {
    632 	int rv = 0;
    633 
    634 	switch (act) {
    635 	case DVACT_ACTIVATE:
    636 		return (EOPNOTSUPP);
    637 
    638 	case DVACT_DEACTIVATE:
    639 		sc->sc_dying = 1;
    640 		if (sc->sc_subdev)
    641 			rv = config_deactivate(sc->sc_subdev);
    642 		break;
    643 	}
    644 	return (rv);
    645 }
    646 
    647 int
    648 umodem_common_detach(struct umodem_softc *sc, int flags)
    649 {
    650 	int rv = 0;
    651 
    652 	DPRINTF(("umodem_common_detach: sc=%p flags=%d\n", sc, flags));
    653 
    654 	sc->sc_dying = 1;
    655 
    656 	if (sc->sc_subdev != NULL)
    657 		rv = config_detach(sc->sc_subdev, flags);
    658 
    659 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
    660 			   USBDEV(sc->sc_dev));
    661 
    662 	return (rv);
    663 }
    664