xref: /openbsd-src/sys/dev/usb/umodem.c (revision a28daedfc357b214be5c701aa8ba8adb29a7f1c2)
1 /*	$OpenBSD: umodem.c,v 1.36 2008/06/26 05:42:19 ray Exp $ */
2 /*	$NetBSD: umodem.c,v 1.45 2002/09/23 05:51:23 simonb Exp $	*/
3 
4 /*
5  * Copyright (c) 1998 The NetBSD Foundation, Inc.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to The NetBSD Foundation
9  * by Lennart Augustsson (lennart@augustsson.net) at
10  * Carlstedt Research & Technology.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
25  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 /*
35  * Comm Class spec:  http://www.usb.org/developers/devclass_docs/usbccs10.pdf
36  *                   http://www.usb.org/developers/devclass_docs/usbcdc11.pdf
37  */
38 
39 /*
40  * TODO:
41  * - Add error recovery in various places; the big problem is what
42  *   to do in a callback if there is an error.
43  * - Implement a Call Device for modems without multiplexed commands.
44  *
45  */
46 
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/kernel.h>
50 #include <sys/ioctl.h>
51 #include <sys/conf.h>
52 #include <sys/tty.h>
53 #include <sys/file.h>
54 #include <sys/selinfo.h>
55 #include <sys/proc.h>
56 #include <sys/vnode.h>
57 #include <sys/device.h>
58 #include <sys/poll.h>
59 
60 #include <dev/usb/usb.h>
61 #include <dev/usb/usbcdc.h>
62 
63 #include <dev/usb/usbdi.h>
64 #include <dev/usb/usbdi_util.h>
65 #include <dev/usb/usbdevs.h>
66 #include <dev/usb/usb_quirks.h>
67 
68 #include <dev/usb/usbdevs.h>
69 #include <dev/usb/ucomvar.h>
70 
71 #ifdef UMODEM_DEBUG
72 #define DPRINTFN(n, x)	do { if (umodemdebug > (n)) printf x; } while (0)
73 int	umodemdebug = 0;
74 #else
75 #define DPRINTFN(n, x)
76 #endif
77 #define DPRINTF(x) DPRINTFN(0, x)
78 
79 /*
80  * These are the maximum number of bytes transferred per frame.
81  * Buffers are this large to deal with high speed wireless devices.
82  * Capped at 1024 as ttymalloc() is limited to this amount.
83  */
84 #define UMODEMIBUFSIZE 1024
85 #define UMODEMOBUFSIZE 1024
86 
87 struct umodem_softc {
88 	struct device		 sc_dev;	/* base device */
89 
90 	usbd_device_handle	 sc_udev;	/* USB device */
91 
92 	int			 sc_ctl_iface_no;
93 	usbd_interface_handle	 sc_ctl_iface;	/* control interface */
94 	int			 sc_data_iface_no;
95 	usbd_interface_handle	 sc_data_iface;	/* data interface */
96 
97 	int			 sc_cm_cap;	/* CM capabilities */
98 	int			 sc_acm_cap;	/* ACM capabilities */
99 
100 	int			 sc_cm_over_data;
101 
102 	usb_cdc_line_state_t	 sc_line_state;	/* current line state */
103 	u_char			 sc_dtr;	/* current DTR state */
104 	u_char			 sc_rts;	/* current RTS state */
105 
106 	struct device		*sc_subdev;	/* ucom device */
107 
108 	u_char			 sc_opening;	/* lock during open */
109 	u_char			 sc_dying;	/* disconnecting */
110 
111 	int			 sc_ctl_notify;	/* Notification endpoint */
112 	usbd_pipe_handle	 sc_notify_pipe; /* Notification pipe */
113 	usb_cdc_notification_t	 sc_notify_buf;	/* Notification structure */
114 	u_char			 sc_lsr;	/* Local status register */
115 	u_char			 sc_msr;	/* Modem status register */
116 };
117 
118 usbd_status umodem_set_comm_feature(struct umodem_softc *sc,
119 					   int feature, int state);
120 usbd_status umodem_set_line_coding(struct umodem_softc *sc,
121 					  usb_cdc_line_state_t *state);
122 
123 void	umodem_get_status(void *, int portno, u_char *lsr, u_char *msr);
124 void	umodem_set(void *, int, int, int);
125 void	umodem_dtr(struct umodem_softc *, int);
126 void	umodem_rts(struct umodem_softc *, int);
127 void	umodem_break(struct umodem_softc *, int);
128 void	umodem_set_line_state(struct umodem_softc *);
129 int	umodem_param(void *, int, struct termios *);
130 int	umodem_ioctl(void *, int, u_long, caddr_t, int, struct proc *);
131 int	umodem_open(void *, int portno);
132 void	umodem_close(void *, int portno);
133 void	umodem_intr(usbd_xfer_handle, usbd_private_handle, usbd_status);
134 
135 struct ucom_methods umodem_methods = {
136 	umodem_get_status,
137 	umodem_set,
138 	umodem_param,
139 	umodem_ioctl,
140 	umodem_open,
141 	umodem_close,
142 	NULL,
143 	NULL,
144 };
145 
146 int umodem_match(struct device *, void *, void *);
147 void umodem_attach(struct device *, struct device *, void *);
148 int umodem_detach(struct device *, int);
149 int umodem_activate(struct device *, enum devact);
150 
151 struct cfdriver umodem_cd = {
152 	NULL, "umodem", DV_DULL
153 };
154 
155 const struct cfattach umodem_ca = {
156 	sizeof(struct umodem_softc),
157 	umodem_match,
158 	umodem_attach,
159 	umodem_detach,
160 	umodem_activate,
161 };
162 
163 int
164 umodem_match(struct device *parent, void *match, void *aux)
165 {
166 	struct usb_attach_arg *uaa = aux;
167 	usb_interface_descriptor_t *id;
168 	usb_device_descriptor_t *dd;
169 	int ret;
170 
171 	if (uaa->iface == NULL)
172 		return (UMATCH_NONE);
173 
174 	id = usbd_get_interface_descriptor(uaa->iface);
175 	dd = usbd_get_device_descriptor(uaa->device);
176 	if (id == NULL || dd == NULL)
177 		return (UMATCH_NONE);
178 
179 	ret = UMATCH_NONE;
180 	if (UGETW(dd->idVendor) == USB_VENDOR_KYOCERA &&
181 	    UGETW(dd->idProduct) == USB_PRODUCT_KYOCERA_AHK3001V &&
182 	    id->bInterfaceNumber == 0)
183 		ret = UMATCH_VENDOR_PRODUCT;
184 
185 	if (ret == UMATCH_NONE &&
186 	    id->bInterfaceClass == UICLASS_CDC &&
187 	    id->bInterfaceSubClass == UISUBCLASS_ABSTRACT_CONTROL_MODEL &&
188 	    id->bInterfaceProtocol == UIPROTO_CDC_AT)
189 		ret = UMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO;
190 
191 	return (ret);
192 }
193 
194 void
195 umodem_attach(struct device *parent, struct device *self, void *aux)
196 {
197 	struct umodem_softc *sc = (struct umodem_softc *)self;
198 	struct usb_attach_arg *uaa = aux;
199 	usbd_device_handle dev = uaa->device;
200 	usb_interface_descriptor_t *id;
201 	usb_endpoint_descriptor_t *ed;
202 	usb_cdc_cm_descriptor_t *cmd;
203 	usb_interface_descriptor_t *idesc;
204 	const usb_cdc_acm_descriptor_t *acmd;
205 	const usb_cdc_union_descriptor_t *uniond;
206 	const usb_descriptor_t *desc;
207 	usbd_desc_iter_t iter;
208 	usbd_status err;
209 	int current_iface_no = -1;
210 	int i;
211 	struct ucom_attach_args uca;
212 
213 	sc->sc_udev = dev;
214 	sc->sc_ctl_iface = uaa->iface;
215 
216 	id = usbd_get_interface_descriptor(sc->sc_ctl_iface);
217 	//printf("%s: iclass %d/%d\n", sc->sc_dev.dv_xname,
218 	//    id->bInterfaceClass, id->bInterfaceSubClass);
219 	sc->sc_ctl_iface_no = id->bInterfaceNumber;
220 
221 	/* Get the data interface no. and capabilities */
222 	sc->sc_cm_cap = 0;
223 	sc->sc_data_iface_no = 0;
224 	sc->sc_acm_cap = 0;
225 	usb_desc_iter_init(dev, &iter);
226 	desc = usb_desc_iter_next(&iter);
227 	while (desc) {
228 		if (desc->bDescriptorType == UDESC_INTERFACE) {
229 		    idesc = (usb_interface_descriptor_t *)desc;
230 		    current_iface_no = idesc->bInterfaceNumber;
231 		}
232 		if (current_iface_no == sc->sc_ctl_iface_no &&
233 		    desc->bDescriptorType == UDESC_CS_INTERFACE) {
234 			switch(desc->bDescriptorSubtype) {
235 			case UDESCSUB_CDC_CM:
236 				cmd = (usb_cdc_cm_descriptor_t *)desc;
237 				sc->sc_cm_cap = cmd->bmCapabilities;
238 				sc->sc_data_iface_no = cmd->bDataInterface;
239 				break;
240 			case UDESCSUB_CDC_ACM:
241 				acmd = (usb_cdc_acm_descriptor_t *)desc;
242 				sc->sc_acm_cap = acmd->bmCapabilities;
243 				break;
244 			case UDESCSUB_CDC_UNION:
245 				uniond = (usb_cdc_union_descriptor_t *)desc;
246 				sc->sc_data_iface_no =
247 				    uniond->bSlaveInterface[0];
248 				break;
249 			}
250 		}
251 		desc = usb_desc_iter_next(&iter);
252 	}
253 
254 	if (sc->sc_data_iface_no == 0) {
255 		printf("%s: no pointer to data interface\n",
256 		       sc->sc_dev.dv_xname);
257 		goto bad;
258 	}
259 
260 	printf("%s: data interface %d, has %sCM over data, has %sbreak\n",
261 	       sc->sc_dev.dv_xname, sc->sc_data_iface_no,
262 	       sc->sc_cm_cap & USB_CDC_CM_OVER_DATA ? "" : "no ",
263 	       sc->sc_acm_cap & USB_CDC_ACM_HAS_BREAK ? "" : "no ");
264 
265 	/* Get the data interface too. */
266 	for (i = 0; i < uaa->nifaces; i++) {
267 		if (uaa->ifaces[i] != NULL) {
268 			id = usbd_get_interface_descriptor(uaa->ifaces[i]);
269 			if (id != NULL &&
270                             id->bInterfaceNumber == sc->sc_data_iface_no) {
271 				sc->sc_data_iface = uaa->ifaces[i];
272 				uaa->ifaces[i] = NULL;
273 			}
274 		}
275 	}
276 	if (sc->sc_data_iface == NULL) {
277 		printf("%s: no data interface\n", sc->sc_dev.dv_xname);
278 		goto bad;
279 	}
280 
281 	/*
282 	 * Find the bulk endpoints.
283 	 * Iterate over all endpoints in the data interface and take note.
284 	 */
285 	uca.bulkin = uca.bulkout = -1;
286 
287 	id = usbd_get_interface_descriptor(sc->sc_data_iface);
288 	for (i = 0; i < id->bNumEndpoints; i++) {
289 		ed = usbd_interface2endpoint_descriptor(sc->sc_data_iface, i);
290 		if (ed == NULL) {
291 			printf("%s: no endpoint descriptor for %d\n",
292 				sc->sc_dev.dv_xname, i);
293 			goto bad;
294 		}
295 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
296 		    (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
297                         uca.bulkin = ed->bEndpointAddress;
298                 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
299 			   (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
300                         uca.bulkout = ed->bEndpointAddress;
301                 }
302         }
303 
304 	if (uca.bulkin == -1) {
305 		printf("%s: Could not find data bulk in\n",
306 		       sc->sc_dev.dv_xname);
307 		goto bad;
308 	}
309 	if (uca.bulkout == -1) {
310 		printf("%s: Could not find data bulk out\n",
311 			sc->sc_dev.dv_xname);
312 		goto bad;
313 	}
314 
315 	if (usbd_get_quirks(sc->sc_udev)->uq_flags & UQ_ASSUME_CM_OVER_DATA) {
316 		sc->sc_cm_over_data = 1;
317 	} else {
318 		if (sc->sc_cm_cap & USB_CDC_CM_OVER_DATA) {
319 			if (sc->sc_acm_cap & USB_CDC_ACM_HAS_FEATURE)
320 				err = umodem_set_comm_feature(sc,
321 				    UCDC_ABSTRACT_STATE, UCDC_DATA_MULTIPLEXED);
322 			else
323 				err = 0;
324 			if (err) {
325 				printf("%s: could not set data multiplex mode\n",
326 				       sc->sc_dev.dv_xname);
327 				goto bad;
328 			}
329 			sc->sc_cm_over_data = 1;
330 		}
331 	}
332 
333 	/*
334 	 * The standard allows for notification messages (to indicate things
335 	 * like a modem hangup) to come in via an interrupt endpoint
336 	 * off of the control interface.  Iterate over the endpoints on
337 	 * the control interface and see if there are any interrupt
338 	 * endpoints; if there are, then register it.
339 	 */
340 
341 	sc->sc_ctl_notify = -1;
342 	sc->sc_notify_pipe = NULL;
343 
344 	id = usbd_get_interface_descriptor(sc->sc_ctl_iface);
345 	for (i = 0; i < id->bNumEndpoints; i++) {
346 		ed = usbd_interface2endpoint_descriptor(sc->sc_ctl_iface, i);
347 		if (ed == NULL)
348 			continue;
349 
350 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
351 		    (ed->bmAttributes & UE_XFERTYPE) == UE_INTERRUPT) {
352 			printf("%s: status change notification available\n",
353 			       sc->sc_dev.dv_xname);
354 			sc->sc_ctl_notify = ed->bEndpointAddress;
355 		}
356 	}
357 
358 	sc->sc_dtr = -1;
359 
360 	uca.portno = UCOM_UNK_PORTNO;
361 	/* bulkin, bulkout set above */
362 	uca.ibufsize = UMODEMIBUFSIZE;
363 	uca.obufsize = UMODEMOBUFSIZE;
364 	uca.ibufsizepad = UMODEMIBUFSIZE;
365 	uca.opkthdrlen = 0;
366 	uca.device = sc->sc_udev;
367 	uca.iface = sc->sc_data_iface;
368 	uca.methods = &umodem_methods;
369 	uca.arg = sc;
370 	uca.info = NULL;
371 
372 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
373 			   &sc->sc_dev);
374 
375 	DPRINTF(("umodem_attach: sc=%p\n", sc));
376 	sc->sc_subdev = config_found_sm(self, &uca, ucomprint, ucomsubmatch);
377 
378 	return;
379 
380  bad:
381 	sc->sc_dying = 1;
382 }
383 
384 int
385 umodem_open(void *addr, int portno)
386 {
387 	struct umodem_softc *sc = addr;
388 	int err;
389 
390 	DPRINTF(("umodem_open: sc=%p\n", sc));
391 
392 	if (sc->sc_ctl_notify != -1 && sc->sc_notify_pipe == NULL) {
393 		err = usbd_open_pipe_intr(sc->sc_ctl_iface, sc->sc_ctl_notify,
394 		    USBD_SHORT_XFER_OK, &sc->sc_notify_pipe, sc,
395 		    &sc->sc_notify_buf, sizeof(sc->sc_notify_buf),
396 		    umodem_intr, USBD_DEFAULT_INTERVAL);
397 
398 		if (err) {
399 			DPRINTF(("Failed to establish notify pipe: %s\n",
400 				usbd_errstr(err)));
401 			return EIO;
402 		}
403 	}
404 
405 	return 0;
406 }
407 
408 void
409 umodem_close(void *addr, int portno)
410 {
411 	struct umodem_softc *sc = addr;
412 	int err;
413 
414 	DPRINTF(("umodem_close: sc=%p\n", sc));
415 
416 	if (sc->sc_notify_pipe != NULL) {
417 		err = usbd_abort_pipe(sc->sc_notify_pipe);
418 		if (err)
419 			printf("%s: abort notify pipe failed: %s\n",
420 			    sc->sc_dev.dv_xname, usbd_errstr(err));
421 		err = usbd_close_pipe(sc->sc_notify_pipe);
422 		if (err)
423 			printf("%s: close notify pipe failed: %s\n",
424 			    sc->sc_dev.dv_xname, usbd_errstr(err));
425 		sc->sc_notify_pipe = NULL;
426 	}
427 }
428 
429 void
430 umodem_intr(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
431 {
432 	struct umodem_softc *sc = priv;
433 	u_char mstatus;
434 
435 	if (sc->sc_dying)
436 		return;
437 
438 	if (status != USBD_NORMAL_COMPLETION) {
439 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
440 			return;
441 		printf("%s: abnormal status: %s\n", sc->sc_dev.dv_xname,
442 		       usbd_errstr(status));
443 		return;
444 	}
445 
446 	if (sc->sc_notify_buf.bmRequestType != UCDC_NOTIFICATION) {
447 		DPRINTF(("%s: unknown message type (%02x) on notify pipe\n",
448 			 sc->sc_dev.dv_xname,
449 			 sc->sc_notify_buf.bmRequestType));
450 		return;
451 	}
452 
453 	switch (sc->sc_notify_buf.bNotification) {
454 	case UCDC_N_SERIAL_STATE:
455 		/*
456 		 * Set the serial state in ucom driver based on
457 		 * the bits from the notify message
458 		 */
459 		if (UGETW(sc->sc_notify_buf.wLength) != 2) {
460 			printf("%s: Invalid notification length! (%d)\n",
461 			       sc->sc_dev.dv_xname,
462 			       UGETW(sc->sc_notify_buf.wLength));
463 			break;
464 		}
465 		DPRINTF(("%s: notify bytes = %02x%02x\n",
466 			 sc->sc_dev.dv_xname,
467 			 sc->sc_notify_buf.data[0],
468 			 sc->sc_notify_buf.data[1]));
469 		/* Currently, lsr is always zero. */
470 		sc->sc_lsr = sc->sc_msr = 0;
471 		mstatus = sc->sc_notify_buf.data[0];
472 
473 		if (ISSET(mstatus, UCDC_N_SERIAL_RI))
474 			sc->sc_msr |= UMSR_RI;
475 		if (ISSET(mstatus, UCDC_N_SERIAL_DSR))
476 			sc->sc_msr |= UMSR_DSR;
477 		if (ISSET(mstatus, UCDC_N_SERIAL_DCD))
478 			sc->sc_msr |= UMSR_DCD;
479 		ucom_status_change((struct ucom_softc *)sc->sc_subdev);
480 		break;
481 	default:
482 		DPRINTF(("%s: unknown notify message: %02x\n",
483 			 sc->sc_dev.dv_xname,
484 			 sc->sc_notify_buf.bNotification));
485 		break;
486 	}
487 }
488 
489 void
490 umodem_get_status(void *addr, int portno, u_char *lsr, u_char *msr)
491 {
492 	struct umodem_softc *sc = addr;
493 
494 	DPRINTF(("umodem_get_status:\n"));
495 
496 	if (lsr != NULL)
497 		*lsr = sc->sc_lsr;
498 	if (msr != NULL)
499 		*msr = sc->sc_msr;
500 }
501 
502 int
503 umodem_param(void *addr, int portno, struct termios *t)
504 {
505 	struct umodem_softc *sc = addr;
506 	usbd_status err;
507 	usb_cdc_line_state_t ls;
508 
509 	DPRINTF(("umodem_param: sc=%p\n", sc));
510 
511 	USETDW(ls.dwDTERate, t->c_ospeed);
512 	if (ISSET(t->c_cflag, CSTOPB))
513 		ls.bCharFormat = UCDC_STOP_BIT_2;
514 	else
515 		ls.bCharFormat = UCDC_STOP_BIT_1;
516 	if (ISSET(t->c_cflag, PARENB)) {
517 		if (ISSET(t->c_cflag, PARODD))
518 			ls.bParityType = UCDC_PARITY_ODD;
519 		else
520 			ls.bParityType = UCDC_PARITY_EVEN;
521 	} else
522 		ls.bParityType = UCDC_PARITY_NONE;
523 	switch (ISSET(t->c_cflag, CSIZE)) {
524 	case CS5:
525 		ls.bDataBits = 5;
526 		break;
527 	case CS6:
528 		ls.bDataBits = 6;
529 		break;
530 	case CS7:
531 		ls.bDataBits = 7;
532 		break;
533 	case CS8:
534 		ls.bDataBits = 8;
535 		break;
536 	}
537 
538 	err = umodem_set_line_coding(sc, &ls);
539 	if (err) {
540 		DPRINTF(("umodem_param: err=%s\n", usbd_errstr(err)));
541 		return (1);
542 	}
543 	return (0);
544 }
545 
546 int
547 umodem_ioctl(void *addr, int portno, u_long cmd, caddr_t data, int flag,
548 	     struct proc *p)
549 {
550 	struct umodem_softc *sc = addr;
551 	int error = 0;
552 
553 	if (sc->sc_dying)
554 		return (EIO);
555 
556 	DPRINTF(("umodemioctl: cmd=0x%08lx\n", cmd));
557 
558 	switch (cmd) {
559 	case USB_GET_CM_OVER_DATA:
560 		*(int *)data = sc->sc_cm_over_data;
561 		break;
562 
563 	case USB_SET_CM_OVER_DATA:
564 		if (*(int *)data != sc->sc_cm_over_data) {
565 			/* XXX change it */
566 		}
567 		break;
568 
569 	default:
570 		DPRINTF(("umodemioctl: unknown\n"));
571 		error = ENOTTY;
572 		break;
573 	}
574 
575 	return (error);
576 }
577 
578 void
579 umodem_dtr(struct umodem_softc *sc, int onoff)
580 {
581 	DPRINTF(("umodem_modem: onoff=%d\n", onoff));
582 
583 	if (sc->sc_dtr == onoff)
584 		return;
585 	sc->sc_dtr = onoff;
586 
587 	umodem_set_line_state(sc);
588 }
589 
590 void
591 umodem_rts(struct umodem_softc *sc, int onoff)
592 {
593 	DPRINTF(("umodem_modem: onoff=%d\n", onoff));
594 
595 	if (sc->sc_rts == onoff)
596 		return;
597 	sc->sc_rts = onoff;
598 
599 	umodem_set_line_state(sc);
600 }
601 
602 void
603 umodem_set_line_state(struct umodem_softc *sc)
604 {
605 	usb_device_request_t req;
606 	int ls;
607 
608 	ls = (sc->sc_dtr ? UCDC_LINE_DTR : 0) |
609 	     (sc->sc_rts ? UCDC_LINE_RTS : 0);
610 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
611 	req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
612 	USETW(req.wValue, ls);
613 	USETW(req.wIndex, sc->sc_ctl_iface_no);
614 	USETW(req.wLength, 0);
615 
616 	(void)usbd_do_request(sc->sc_udev, &req, 0);
617 
618 }
619 
620 void
621 umodem_break(struct umodem_softc *sc, int onoff)
622 {
623 	usb_device_request_t req;
624 
625 	DPRINTF(("umodem_break: onoff=%d\n", onoff));
626 
627 	if (!(sc->sc_acm_cap & USB_CDC_ACM_HAS_BREAK))
628 		return;
629 
630 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
631 	req.bRequest = UCDC_SEND_BREAK;
632 	USETW(req.wValue, onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF);
633 	USETW(req.wIndex, sc->sc_ctl_iface_no);
634 	USETW(req.wLength, 0);
635 
636 	(void)usbd_do_request(sc->sc_udev, &req, 0);
637 }
638 
639 void
640 umodem_set(void *addr, int portno, int reg, int onoff)
641 {
642 	struct umodem_softc *sc = addr;
643 
644 	switch (reg) {
645 	case UCOM_SET_DTR:
646 		umodem_dtr(sc, onoff);
647 		break;
648 	case UCOM_SET_RTS:
649 		umodem_rts(sc, onoff);
650 		break;
651 	case UCOM_SET_BREAK:
652 		umodem_break(sc, onoff);
653 		break;
654 	default:
655 		break;
656 	}
657 }
658 
659 usbd_status
660 umodem_set_line_coding(struct umodem_softc *sc, usb_cdc_line_state_t *state)
661 {
662 	usb_device_request_t req;
663 	usbd_status err;
664 
665 	DPRINTF(("umodem_set_line_coding: rate=%d fmt=%d parity=%d bits=%d\n",
666 		 UGETDW(state->dwDTERate), state->bCharFormat,
667 		 state->bParityType, state->bDataBits));
668 
669 	if (memcmp(state, &sc->sc_line_state, UCDC_LINE_STATE_LENGTH) == 0) {
670 		DPRINTF(("umodem_set_line_coding: already set\n"));
671 		return (USBD_NORMAL_COMPLETION);
672 	}
673 
674 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
675 	req.bRequest = UCDC_SET_LINE_CODING;
676 	USETW(req.wValue, 0);
677 	USETW(req.wIndex, sc->sc_ctl_iface_no);
678 	USETW(req.wLength, UCDC_LINE_STATE_LENGTH);
679 
680 	err = usbd_do_request(sc->sc_udev, &req, state);
681 	if (err) {
682 		DPRINTF(("umodem_set_line_coding: failed, err=%s\n",
683 			 usbd_errstr(err)));
684 		return (err);
685 	}
686 
687 	sc->sc_line_state = *state;
688 
689 	return (USBD_NORMAL_COMPLETION);
690 }
691 
692 usbd_status
693 umodem_set_comm_feature(struct umodem_softc *sc, int feature, int state)
694 {
695 	usb_device_request_t req;
696 	usbd_status err;
697 	usb_cdc_abstract_state_t ast;
698 
699 	DPRINTF(("umodem_set_comm_feature: feature=%d state=%d\n", feature,
700 		 state));
701 
702 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
703 	req.bRequest = UCDC_SET_COMM_FEATURE;
704 	USETW(req.wValue, feature);
705 	USETW(req.wIndex, sc->sc_ctl_iface_no);
706 	USETW(req.wLength, UCDC_ABSTRACT_STATE_LENGTH);
707 	USETW(ast.wState, state);
708 
709 	err = usbd_do_request(sc->sc_udev, &req, &ast);
710 	if (err) {
711 		DPRINTF(("umodem_set_comm_feature: feature=%d, err=%s\n",
712 			 feature, usbd_errstr(err)));
713 		return (err);
714 	}
715 
716 	return (USBD_NORMAL_COMPLETION);
717 }
718 
719 int
720 umodem_activate(struct device *self, enum devact act)
721 {
722 	struct umodem_softc *sc = (struct umodem_softc *)self;
723 	int rv = 0;
724 
725 	switch (act) {
726 	case DVACT_ACTIVATE:
727 		break;
728 
729 	case DVACT_DEACTIVATE:
730 		sc->sc_dying = 1;
731 		if (sc->sc_subdev)
732 			rv = config_deactivate(sc->sc_subdev);
733 		break;
734 	}
735 	return (rv);
736 }
737 
738 int
739 umodem_detach(struct device *self, int flags)
740 {
741 	struct umodem_softc *sc = (struct umodem_softc *)self;
742 	int rv = 0;
743 
744 	DPRINTF(("umodem_detach: sc=%p flags=%d\n", sc, flags));
745 
746 	sc->sc_dying = 1;
747 
748 	if (sc->sc_subdev != NULL)
749 		rv = config_detach(sc->sc_subdev, flags);
750 
751 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
752 			   &sc->sc_dev);
753 
754 	return (rv);
755 }
756