Home | History | Annotate | Line # | Download | only in usb
umodem_common.c revision 1.8
      1 /*	$NetBSD: umodem_common.c,v 1.8 2006/10/12 01:32:00 christos 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.8 2006/10/12 01:32:00 christos 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  * Note: increased from 64/256, to better support EVDO wireless PPP.
     95  * The sizes should not be increased further, or there
     96  * will be problems with contiguous storage allocation.
     97  */
     98 #define UMODEMIBUFSIZE 4096
     99 #define UMODEMOBUFSIZE 4096
    100 
    101 Static usbd_status umodem_set_comm_feature(struct umodem_softc *sc,
    102 					   int feature, int state);
    103 Static usbd_status umodem_set_line_coding(struct umodem_softc *sc,
    104 					  usb_cdc_line_state_t *state);
    105 
    106 Static void	umodem_dtr(struct umodem_softc *, int);
    107 Static void	umodem_rts(struct umodem_softc *, int);
    108 Static void	umodem_break(struct umodem_softc *, int);
    109 Static void	umodem_set_line_state(struct umodem_softc *);
    110 Static void	umodem_intr(usbd_xfer_handle, usbd_private_handle, usbd_status);
    111 
    112 int
    113 umodem_common_attach(device_ptr_t self, struct umodem_softc *sc,
    114 		     struct usb_attach_arg *uaa, struct ucom_attach_args *uca)
    115 {
    116 	usbd_device_handle dev = uaa->device;
    117 	usb_interface_descriptor_t *id;
    118 	usb_endpoint_descriptor_t *ed;
    119 	char *devinfop;
    120 	usbd_status err;
    121 	int data_ifcno;
    122 	int i;
    123 
    124 	devinfop = usbd_devinfo_alloc(uaa->device, 0);
    125 	USB_ATTACH_SETUP;
    126 
    127 	sc->sc_udev = dev;
    128 	sc->sc_ctl_iface = uaa->iface;
    129 
    130 	id = usbd_get_interface_descriptor(sc->sc_ctl_iface);
    131 	printf("%s: %s, iclass %d/%d\n", USBDEVNAME(sc->sc_dev),
    132 	       devinfop, id->bInterfaceClass, id->bInterfaceSubClass);
    133 	usbd_devinfo_free(devinfop);
    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 		printf("%s: no pointer to data interface\n",
    142 		       USBDEVNAME(sc->sc_dev));
    143 		goto bad;
    144 	}
    145 
    146 	printf("%s: data interface %d, has %sCM over data, has %sbreak\n",
    147 	       USBDEVNAME(sc->sc_dev), data_ifcno,
    148 	       sc->sc_cm_cap & USB_CDC_CM_OVER_DATA ? "" : "no ",
    149 	       sc->sc_acm_cap & USB_CDC_ACM_HAS_BREAK ? "" : "no ");
    150 
    151 	/* Get the data interface too. */
    152 	for (i = 0; i < uaa->nifaces; i++) {
    153 		if (uaa->ifaces[i] != NULL) {
    154 			id = usbd_get_interface_descriptor(uaa->ifaces[i]);
    155 			if (id != NULL && id->bInterfaceNumber == data_ifcno) {
    156 				sc->sc_data_iface = uaa->ifaces[i];
    157 				uaa->ifaces[i] = NULL;
    158 			}
    159 		}
    160 	}
    161 	if (sc->sc_data_iface == NULL) {
    162 		printf("%s: no data interface\n", USBDEVNAME(sc->sc_dev));
    163 		goto bad;
    164 	}
    165 
    166 	/*
    167 	 * Find the bulk endpoints.
    168 	 * Iterate over all endpoints in the data interface and take note.
    169 	 */
    170 	uca->bulkin = uca->bulkout = -1;
    171 
    172 	id = usbd_get_interface_descriptor(sc->sc_data_iface);
    173 	for (i = 0; i < id->bNumEndpoints; i++) {
    174 		ed = usbd_interface2endpoint_descriptor(sc->sc_data_iface, i);
    175 		if (ed == NULL) {
    176 			printf("%s: no endpoint descriptor for %d\n",
    177 				USBDEVNAME(sc->sc_dev), i);
    178 			goto bad;
    179 		}
    180 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    181 		    (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
    182 			uca->bulkin = ed->bEndpointAddress;
    183 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
    184 			   (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
    185 			uca->bulkout = ed->bEndpointAddress;
    186 		}
    187 	}
    188 
    189 	if (uca->bulkin == -1) {
    190 		printf("%s: Could not find data bulk in\n",
    191 		       USBDEVNAME(sc->sc_dev));
    192 		goto bad;
    193 	}
    194 	if (uca->bulkout == -1) {
    195 		printf("%s: Could not find data bulk out\n",
    196 			USBDEVNAME(sc->sc_dev));
    197 		goto bad;
    198 	}
    199 
    200 	if (usbd_get_quirks(sc->sc_udev)->uq_flags & UQ_ASSUME_CM_OVER_DATA) {
    201 		sc->sc_cm_over_data = 1;
    202 	} else {
    203 		if (sc->sc_cm_cap & USB_CDC_CM_OVER_DATA) {
    204 			if (sc->sc_acm_cap & USB_CDC_ACM_HAS_FEATURE)
    205 				err = umodem_set_comm_feature(sc,
    206 				    UCDC_ABSTRACT_STATE, UCDC_DATA_MULTIPLEXED);
    207 			else
    208 				err = 0;
    209 			if (err) {
    210 				printf("%s: could not set data multiplex mode\n",
    211 				       USBDEVNAME(sc->sc_dev));
    212 				goto bad;
    213 			}
    214 			sc->sc_cm_over_data = 1;
    215 		}
    216 	}
    217 
    218 	/*
    219 	 * The standard allows for notification messages (to indicate things
    220 	 * like a modem hangup) to come in via an interrupt endpoint
    221 	 * off of the control interface.  Iterate over the endpoints on
    222 	 * the control interface and see if there are any interrupt
    223 	 * endpoints; if there are, then register it.
    224 	 */
    225 
    226 	sc->sc_ctl_notify = -1;
    227 	sc->sc_notify_pipe = NULL;
    228 
    229 	for (i = 0; i < id->bNumEndpoints; i++) {
    230 		ed = usbd_interface2endpoint_descriptor(sc->sc_ctl_iface, i);
    231 		if (ed == NULL)
    232 			continue;
    233 
    234 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    235 		    (ed->bmAttributes & UE_XFERTYPE) == UE_INTERRUPT) {
    236 			printf("%s: status change notification available\n",
    237 			       USBDEVNAME(sc->sc_dev));
    238 			sc->sc_ctl_notify = ed->bEndpointAddress;
    239 		}
    240 	}
    241 
    242 	sc->sc_dtr = -1;
    243 
    244 	/* bulkin, bulkout set above */
    245 	uca->ibufsize = UMODEMIBUFSIZE;
    246 	uca->obufsize = UMODEMOBUFSIZE;
    247 	uca->ibufsizepad = UMODEMIBUFSIZE;
    248 	uca->opkthdrlen = 0;
    249 	uca->device = sc->sc_udev;
    250 	uca->iface = sc->sc_data_iface;
    251 	uca->arg = sc;
    252 
    253 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
    254 			   USBDEV(sc->sc_dev));
    255 
    256 	DPRINTF(("umodem_common_attach: sc=%p\n", sc));
    257 	sc->sc_subdev = config_found_sm_loc(self, "ucombus", NULL, uca,
    258 					    ucomprint, ucomsubmatch);
    259 
    260 	return 0;
    261 
    262  bad:
    263 	sc->sc_dying = 1;
    264 	return 1;
    265 }
    266 
    267 int
    268 umodem_open(void *addr, int portno __unused)
    269 {
    270 	struct umodem_softc *sc = addr;
    271 	int err;
    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 __unused)
    293 {
    294 	struct umodem_softc *sc = addr;
    295 	int err;
    296 
    297 	DPRINTF(("umodem_close: sc=%p\n", sc));
    298 
    299 	if (sc->sc_notify_pipe != NULL) {
    300 		err = usbd_abort_pipe(sc->sc_notify_pipe);
    301 		if (err)
    302 			printf("%s: abort notify pipe failed: %s\n",
    303 			    USBDEVNAME(sc->sc_dev), usbd_errstr(err));
    304 		err = usbd_close_pipe(sc->sc_notify_pipe);
    305 		if (err)
    306 			printf("%s: close notify pipe failed: %s\n",
    307 			    USBDEVNAME(sc->sc_dev), usbd_errstr(err));
    308 		sc->sc_notify_pipe = NULL;
    309 	}
    310 }
    311 
    312 Static void
    313 umodem_intr(usbd_xfer_handle xfer __unused, usbd_private_handle priv,
    314     usbd_status status)
    315 {
    316 	struct umodem_softc *sc = priv;
    317 	u_char mstatus;
    318 
    319 	if (sc->sc_dying)
    320 		return;
    321 
    322 	if (status != USBD_NORMAL_COMPLETION) {
    323 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
    324 			return;
    325 		printf("%s: abnormal status: %s\n", USBDEVNAME(sc->sc_dev),
    326 		       usbd_errstr(status));
    327 		return;
    328 	}
    329 
    330 	if (sc->sc_notify_buf.bmRequestType != UCDC_NOTIFICATION) {
    331 		DPRINTF(("%s: unknown message type (%02x) on notify pipe\n",
    332 			 USBDEVNAME(sc->sc_dev),
    333 			 sc->sc_notify_buf.bmRequestType));
    334 		return;
    335 	}
    336 
    337 	switch (sc->sc_notify_buf.bNotification) {
    338 	case UCDC_N_SERIAL_STATE:
    339 		/*
    340 		 * Set the serial state in ucom driver based on
    341 		 * the bits from the notify message
    342 		 */
    343 		if (UGETW(sc->sc_notify_buf.wLength) != 2) {
    344 			printf("%s: Invalid notification length! (%d)\n",
    345 			       USBDEVNAME(sc->sc_dev),
    346 			       UGETW(sc->sc_notify_buf.wLength));
    347 			break;
    348 		}
    349 		DPRINTF(("%s: notify bytes = %02x%02x\n",
    350 			 USBDEVNAME(sc->sc_dev),
    351 			 sc->sc_notify_buf.data[0],
    352 			 sc->sc_notify_buf.data[1]));
    353 		/* Currently, lsr is always zero. */
    354 		sc->sc_lsr = sc->sc_msr = 0;
    355 		mstatus = sc->sc_notify_buf.data[0];
    356 
    357 		if (ISSET(mstatus, UCDC_N_SERIAL_RI))
    358 			sc->sc_msr |= UMSR_RI;
    359 		if (ISSET(mstatus, UCDC_N_SERIAL_DSR))
    360 			sc->sc_msr |= UMSR_DSR;
    361 		if (ISSET(mstatus, UCDC_N_SERIAL_DCD))
    362 			sc->sc_msr |= UMSR_DCD;
    363 		ucom_status_change((struct ucom_softc *)sc->sc_subdev);
    364 		break;
    365 	default:
    366 		DPRINTF(("%s: unknown notify message: %02x\n",
    367 			 USBDEVNAME(sc->sc_dev),
    368 			 sc->sc_notify_buf.bNotification));
    369 		break;
    370 	}
    371 }
    372 
    373 int
    374 umodem_get_caps(usbd_device_handle dev, int *cm, int *acm,
    375 		usb_interface_descriptor_t *id)
    376 {
    377 	const usb_cdc_cm_descriptor_t *cmd;
    378 	const usb_cdc_acm_descriptor_t *cad;
    379 	const usb_cdc_union_descriptor_t *cud;
    380 
    381 	*cm = *acm = 0;
    382 
    383 	cmd = (const usb_cdc_cm_descriptor_t *)usb_find_desc_if(dev,
    384 							  UDESC_CS_INTERFACE,
    385 							  UDESCSUB_CDC_CM, id);
    386 	if (cmd == NULL) {
    387 		DPRINTF(("umodem_get_caps: no CM desc\n"));
    388 	} else {
    389 		*cm = cmd->bmCapabilities;
    390 	}
    391 
    392 	cad = (const usb_cdc_acm_descriptor_t *)usb_find_desc_if(dev,
    393 							   UDESC_CS_INTERFACE,
    394 							   UDESCSUB_CDC_ACM,
    395 							   id);
    396 	if (cad == NULL) {
    397 		DPRINTF(("umodem_get_caps: no ACM desc\n"));
    398 	} else {
    399 		*acm = cad->bmCapabilities;
    400 	}
    401 
    402 	cud = (const usb_cdc_union_descriptor_t *)usb_find_desc_if(dev,
    403 							     UDESC_CS_INTERFACE,
    404 							     UDESCSUB_CDC_UNION,
    405 							     id);
    406 	if (cud == NULL) {
    407 		DPRINTF(("umodem_get_caps: no UNION desc\n"));
    408 	}
    409 
    410 	return cmd ? cmd->bDataInterface : cud ? cud->bSlaveInterface[0] : -1;
    411 }
    412 
    413 void
    414 umodem_get_status(void *addr, int portno __unused, u_char *lsr, u_char *msr)
    415 {
    416 	struct umodem_softc *sc = addr;
    417 
    418 	DPRINTF(("umodem_get_status:\n"));
    419 
    420 	if (lsr != NULL)
    421 		*lsr = sc->sc_lsr;
    422 	if (msr != NULL)
    423 		*msr = sc->sc_msr;
    424 }
    425 
    426 int
    427 umodem_param(void *addr, int portno __unused, struct termios *t)
    428 {
    429 	struct umodem_softc *sc = addr;
    430 	usbd_status err;
    431 	usb_cdc_line_state_t ls;
    432 
    433 	DPRINTF(("umodem_param: sc=%p\n", sc));
    434 
    435 	USETDW(ls.dwDTERate, t->c_ospeed);
    436 	if (ISSET(t->c_cflag, CSTOPB))
    437 		ls.bCharFormat = UCDC_STOP_BIT_2;
    438 	else
    439 		ls.bCharFormat = UCDC_STOP_BIT_1;
    440 	if (ISSET(t->c_cflag, PARENB)) {
    441 		if (ISSET(t->c_cflag, PARODD))
    442 			ls.bParityType = UCDC_PARITY_ODD;
    443 		else
    444 			ls.bParityType = UCDC_PARITY_EVEN;
    445 	} else
    446 		ls.bParityType = UCDC_PARITY_NONE;
    447 	switch (ISSET(t->c_cflag, CSIZE)) {
    448 	case CS5:
    449 		ls.bDataBits = 5;
    450 		break;
    451 	case CS6:
    452 		ls.bDataBits = 6;
    453 		break;
    454 	case CS7:
    455 		ls.bDataBits = 7;
    456 		break;
    457 	case CS8:
    458 		ls.bDataBits = 8;
    459 		break;
    460 	}
    461 
    462 	err = umodem_set_line_coding(sc, &ls);
    463 	if (err) {
    464 		DPRINTF(("umodem_param: err=%s\n", usbd_errstr(err)));
    465 		return (EPASSTHROUGH);
    466 	}
    467 	return (0);
    468 }
    469 
    470 int
    471 umodem_ioctl(void *addr, int portno __unused, u_long cmd, caddr_t data,
    472     int flag __unused, usb_proc_ptr p __unused)
    473 {
    474 	struct umodem_softc *sc = addr;
    475 	int error = 0;
    476 
    477 	if (sc->sc_dying)
    478 		return (EIO);
    479 
    480 	DPRINTF(("umodem_ioctl: cmd=0x%08lx\n", cmd));
    481 
    482 	switch (cmd) {
    483 	case USB_GET_CM_OVER_DATA:
    484 		*(int *)data = sc->sc_cm_over_data;
    485 		break;
    486 
    487 	case USB_SET_CM_OVER_DATA:
    488 		if (*(int *)data != sc->sc_cm_over_data) {
    489 			/* XXX change it */
    490 		}
    491 		break;
    492 
    493 	default:
    494 		DPRINTF(("umodem_ioctl: unknown\n"));
    495 		error = EPASSTHROUGH;
    496 		break;
    497 	}
    498 
    499 	return (error);
    500 }
    501 
    502 void
    503 umodem_dtr(struct umodem_softc *sc, int onoff)
    504 {
    505 	DPRINTF(("umodem_dtr: onoff=%d\n", onoff));
    506 
    507 	if (sc->sc_dtr == onoff)
    508 		return;
    509 	sc->sc_dtr = onoff;
    510 
    511 	umodem_set_line_state(sc);
    512 }
    513 
    514 void
    515 umodem_rts(struct umodem_softc *sc, int onoff)
    516 {
    517 	DPRINTF(("umodem_rts: onoff=%d\n", onoff));
    518 
    519 	if (sc->sc_rts == onoff)
    520 		return;
    521 	sc->sc_rts = onoff;
    522 
    523 	umodem_set_line_state(sc);
    524 }
    525 
    526 void
    527 umodem_set_line_state(struct umodem_softc *sc)
    528 {
    529 	usb_device_request_t req;
    530 	int ls;
    531 
    532 	ls = (sc->sc_dtr ? UCDC_LINE_DTR : 0) |
    533 	     (sc->sc_rts ? UCDC_LINE_RTS : 0);
    534 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
    535 	req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
    536 	USETW(req.wValue, ls);
    537 	USETW(req.wIndex, sc->sc_ctl_iface_no);
    538 	USETW(req.wLength, 0);
    539 
    540 	(void)usbd_do_request(sc->sc_udev, &req, 0);
    541 
    542 }
    543 
    544 void
    545 umodem_break(struct umodem_softc *sc, int onoff)
    546 {
    547 	usb_device_request_t req;
    548 
    549 	DPRINTF(("umodem_break: onoff=%d\n", onoff));
    550 
    551 	if (!(sc->sc_acm_cap & USB_CDC_ACM_HAS_BREAK))
    552 		return;
    553 
    554 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
    555 	req.bRequest = UCDC_SEND_BREAK;
    556 	USETW(req.wValue, onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF);
    557 	USETW(req.wIndex, sc->sc_ctl_iface_no);
    558 	USETW(req.wLength, 0);
    559 
    560 	(void)usbd_do_request(sc->sc_udev, &req, 0);
    561 }
    562 
    563 void
    564 umodem_set(void *addr, int portno __unused, int reg, int onoff)
    565 {
    566 	struct umodem_softc *sc = addr;
    567 
    568 	switch (reg) {
    569 	case UCOM_SET_DTR:
    570 		umodem_dtr(sc, onoff);
    571 		break;
    572 	case UCOM_SET_RTS:
    573 		umodem_rts(sc, onoff);
    574 		break;
    575 	case UCOM_SET_BREAK:
    576 		umodem_break(sc, onoff);
    577 		break;
    578 	default:
    579 		break;
    580 	}
    581 }
    582 
    583 usbd_status
    584 umodem_set_line_coding(struct umodem_softc *sc, usb_cdc_line_state_t *state)
    585 {
    586 	usb_device_request_t req;
    587 	usbd_status err;
    588 
    589 	DPRINTF(("umodem_set_line_coding: rate=%d fmt=%d parity=%d bits=%d\n",
    590 		 UGETDW(state->dwDTERate), state->bCharFormat,
    591 		 state->bParityType, state->bDataBits));
    592 
    593 	if (memcmp(state, &sc->sc_line_state, UCDC_LINE_STATE_LENGTH) == 0) {
    594 		DPRINTF(("umodem_set_line_coding: already set\n"));
    595 		return (USBD_NORMAL_COMPLETION);
    596 	}
    597 
    598 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
    599 	req.bRequest = UCDC_SET_LINE_CODING;
    600 	USETW(req.wValue, 0);
    601 	USETW(req.wIndex, sc->sc_ctl_iface_no);
    602 	USETW(req.wLength, UCDC_LINE_STATE_LENGTH);
    603 
    604 	err = usbd_do_request(sc->sc_udev, &req, state);
    605 	if (err) {
    606 		DPRINTF(("umodem_set_line_coding: failed, err=%s\n",
    607 			 usbd_errstr(err)));
    608 		return (err);
    609 	}
    610 
    611 	sc->sc_line_state = *state;
    612 
    613 	return (USBD_NORMAL_COMPLETION);
    614 }
    615 
    616 usbd_status
    617 umodem_set_comm_feature(struct umodem_softc *sc, int feature, int state)
    618 {
    619 	usb_device_request_t req;
    620 	usbd_status err;
    621 	usb_cdc_abstract_state_t ast;
    622 
    623 	DPRINTF(("umodem_set_comm_feature: feature=%d state=%d\n", feature,
    624 		 state));
    625 
    626 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
    627 	req.bRequest = UCDC_SET_COMM_FEATURE;
    628 	USETW(req.wValue, feature);
    629 	USETW(req.wIndex, sc->sc_ctl_iface_no);
    630 	USETW(req.wLength, UCDC_ABSTRACT_STATE_LENGTH);
    631 	USETW(ast.wState, state);
    632 
    633 	err = usbd_do_request(sc->sc_udev, &req, &ast);
    634 	if (err) {
    635 		DPRINTF(("umodem_set_comm_feature: feature=%d, err=%s\n",
    636 			 feature, usbd_errstr(err)));
    637 		return (err);
    638 	}
    639 
    640 	return (USBD_NORMAL_COMPLETION);
    641 }
    642 
    643 int
    644 umodem_common_activate(struct umodem_softc *sc, enum devact act)
    645 {
    646 	int rv = 0;
    647 
    648 	switch (act) {
    649 	case DVACT_ACTIVATE:
    650 		return (EOPNOTSUPP);
    651 
    652 	case DVACT_DEACTIVATE:
    653 		sc->sc_dying = 1;
    654 		if (sc->sc_subdev)
    655 			rv = config_deactivate(sc->sc_subdev);
    656 		break;
    657 	}
    658 	return (rv);
    659 }
    660 
    661 int
    662 umodem_common_detach(struct umodem_softc *sc, int flags)
    663 {
    664 	int rv = 0;
    665 
    666 	DPRINTF(("umodem_common_detach: sc=%p flags=%d\n", sc, flags));
    667 
    668 	sc->sc_dying = 1;
    669 
    670 	if (sc->sc_subdev != NULL)
    671 		rv = config_detach(sc->sc_subdev, flags);
    672 
    673 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
    674 			   USBDEV(sc->sc_dev));
    675 
    676 	return (rv);
    677 }
    678