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