xref: /openbsd-src/sys/dev/usb/umodem.c (revision a1b4eae8a9734e8ce45f46e4482c3892c0bb6828)
1 /*	$OpenBSD: umodem.c,v 1.60 2016/05/24 05:35:01 mpi 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/conf.h>
51 #include <sys/tty.h>
52 #include <sys/file.h>
53 #include <sys/selinfo.h>
54 #include <sys/device.h>
55 #include <sys/poll.h>
56 
57 #include <dev/usb/usb.h>
58 #include <dev/usb/usbcdc.h>
59 
60 #include <dev/usb/usbdi.h>
61 #include <dev/usb/usbdevs.h>
62 #include <dev/usb/usb_quirks.h>
63 
64 #include <dev/usb/ucomvar.h>
65 
66 #ifdef UMODEM_DEBUG
67 #define DPRINTFN(n, x)	do { if (umodemdebug > (n)) printf x; } while (0)
68 int	umodemdebug = 0;
69 #else
70 #define DPRINTFN(n, x)
71 #endif
72 #define DPRINTF(x) DPRINTFN(0, x)
73 
74 /*
75  * These are the maximum number of bytes transferred per frame.
76  * Buffers are this large to deal with high speed wireless devices.
77  * Capped at 1024 as ttymalloc() is limited to this amount.
78  */
79 #define UMODEMIBUFSIZE 1024
80 #define UMODEMOBUFSIZE 1024
81 
82 struct umodem_softc {
83 	struct device		 sc_dev;	/* base device */
84 
85 	struct usbd_device	*sc_udev;	/* USB device */
86 
87 	int			 sc_ctl_iface_no;
88 	struct usbd_interface	*sc_ctl_iface;	/* control interface */
89 	struct usbd_interface	*sc_data_iface;	/* data interface */
90 
91 	int			 sc_cm_cap;	/* CM capabilities */
92 	int			 sc_acm_cap;	/* ACM capabilities */
93 
94 	int			 sc_cm_over_data;
95 
96 	struct usb_cdc_line_state sc_line_state;/* current line state */
97 	u_char			 sc_dtr;	/* current DTR state */
98 	u_char			 sc_rts;	/* current RTS state */
99 
100 	struct device		*sc_subdev;	/* ucom device */
101 
102 	u_char			 sc_opening;	/* lock during open */
103 
104 	int			 sc_ctl_notify;	/* Notification endpoint */
105 	struct usbd_pipe	*sc_notify_pipe; /* Notification pipe */
106 	struct usb_cdc_notification sc_notify_buf; /* Notification structure */
107 	u_char			 sc_lsr;	/* Local status register */
108 	u_char			 sc_msr;	/* Modem status register */
109 };
110 
111 usbd_status umodem_set_comm_feature(struct umodem_softc *sc,
112 					   int feature, int state);
113 usbd_status umodem_set_line_coding(struct umodem_softc *sc,
114 					  struct usb_cdc_line_state *state);
115 
116 void	umodem_get_status(void *, int portno, u_char *lsr, u_char *msr);
117 void	umodem_set(void *, int, int, int);
118 void	umodem_dtr(struct umodem_softc *, int);
119 void	umodem_rts(struct umodem_softc *, int);
120 void	umodem_break(struct umodem_softc *, int);
121 void	umodem_set_line_state(struct umodem_softc *);
122 int	umodem_param(void *, int, struct termios *);
123 int	umodem_open(void *, int portno);
124 void	umodem_close(void *, int portno);
125 void	umodem_intr(struct usbd_xfer *, void *, usbd_status);
126 
127 struct ucom_methods umodem_methods = {
128 	umodem_get_status,
129 	umodem_set,
130 	umodem_param,
131 	NULL,
132 	umodem_open,
133 	umodem_close,
134 	NULL,
135 	NULL,
136 };
137 
138 int umodem_match(struct device *, void *, void *);
139 void umodem_attach(struct device *, struct device *, void *);
140 int umodem_detach(struct device *, int);
141 
142 void umodem_get_caps(struct usb_attach_arg *, int, int *, int *, int *);
143 
144 struct cfdriver umodem_cd = {
145 	NULL, "umodem", DV_DULL
146 };
147 
148 const struct cfattach umodem_ca = {
149 	sizeof(struct umodem_softc), umodem_match, umodem_attach, umodem_detach
150 };
151 
152 void
153 umodem_get_caps(struct usb_attach_arg *uaa, int ctl_iface_no,
154     int *data_iface_idx, int *cm_cap, int *acm_cap)
155 {
156 	const usb_descriptor_t *desc;
157 	const usb_interface_descriptor_t *id;
158 	const struct usb_cdc_cm_descriptor *cmd;
159 	const struct usb_cdc_acm_descriptor *acmd;
160 	const struct usb_cdc_union_descriptor *uniond;
161 	struct usbd_desc_iter iter;
162 	int current_iface_no = -1;
163 	int data_iface_no = -1;
164 	int i;
165 
166 	*data_iface_idx = -1;
167 	*cm_cap = *acm_cap = 0;
168 	usbd_desc_iter_init(uaa->device, &iter);
169 	desc = usbd_desc_iter_next(&iter);
170 	while (desc) {
171 		if (desc->bDescriptorType == UDESC_INTERFACE) {
172 			id = (usb_interface_descriptor_t *)desc;
173 			current_iface_no = id->bInterfaceNumber;
174 			if (current_iface_no != ctl_iface_no &&
175 			    id->bInterfaceClass == UICLASS_CDC_DATA &&
176 			    id->bInterfaceSubClass == UISUBCLASS_DATA &&
177 			    data_iface_no == -1)
178 				data_iface_no = current_iface_no;
179 		}
180 		if (current_iface_no == ctl_iface_no &&
181 		    desc->bDescriptorType == UDESC_CS_INTERFACE) {
182 			switch(desc->bDescriptorSubtype) {
183 			case UDESCSUB_CDC_CM:
184 				cmd = (struct usb_cdc_cm_descriptor *)desc;
185 				*cm_cap = cmd->bmCapabilities;
186 				data_iface_no = cmd->bDataInterface;
187 				break;
188 			case UDESCSUB_CDC_ACM:
189 				acmd = (struct usb_cdc_acm_descriptor *)desc;
190 				*acm_cap = acmd->bmCapabilities;
191 				break;
192 			case UDESCSUB_CDC_UNION:
193 				uniond =
194 				    (struct usb_cdc_union_descriptor *)desc;
195 				data_iface_no = uniond->bSlaveInterface[0];
196 				break;
197 			}
198 		}
199 		desc = usbd_desc_iter_next(&iter);
200 	}
201 
202 	/*
203 	 * If we got a data interface number, make sure the corresponding
204 	 * interface exists and is not already claimed.
205 	 */
206 	if (data_iface_no != -1) {
207 		for (i = 0; i < uaa->nifaces; i++) {
208 			id = usbd_get_interface_descriptor(uaa->ifaces[i]);
209 
210 			if (id == NULL)
211 				continue;
212 
213 			if (id->bInterfaceNumber == data_iface_no) {
214 				if (!usbd_iface_claimed(uaa->device, i))
215 					*data_iface_idx = i;
216 				break;
217 			}
218 		}
219 	}
220 }
221 
222 int
223 umodem_match(struct device *parent, void *match, void *aux)
224 {
225 	struct usb_attach_arg *uaa = aux;
226 	usb_interface_descriptor_t *id;
227 	usb_device_descriptor_t *dd;
228 	int data_iface_idx, cm_cap, acm_cap, ret = UMATCH_NONE;
229 
230 	if (uaa->iface == NULL)
231 		return (ret);
232 
233 	id = usbd_get_interface_descriptor(uaa->iface);
234 	dd = usbd_get_device_descriptor(uaa->device);
235 	if (id == NULL || dd == NULL)
236 		return (ret);
237 
238 	ret = UMATCH_NONE;
239 
240 	/* protocol of 0 so won't match the test below */
241 	if (UGETW(dd->idVendor) == USB_VENDOR_ATMEL &&
242 	    UGETW(dd->idProduct) == USB_PRODUCT_ATMEL_AT91_CDC_ACM)
243 		ret = UMATCH_VENDOR_PRODUCT;
244 
245 	if (UGETW(dd->idVendor) == USB_VENDOR_KYOCERA &&
246 	    UGETW(dd->idProduct) == USB_PRODUCT_KYOCERA_AHK3001V &&
247 	    id->bInterfaceNumber == 0)
248 		ret = UMATCH_VENDOR_PRODUCT;
249 
250 	if (UGETW(dd->idVendor) == USB_VENDOR_ARDUINO &&
251 	    UGETW(dd->idProduct) == USB_PRODUCT_ARDUINO_LEONARDO)
252 	    ret = UMATCH_VENDOR_PRODUCT;
253 
254 	if (ret == UMATCH_NONE &&
255 	    id->bInterfaceClass == UICLASS_CDC &&
256 	    id->bInterfaceSubClass == UISUBCLASS_ABSTRACT_CONTROL_MODEL &&
257 	    id->bInterfaceProtocol == UIPROTO_CDC_AT)
258 		ret = UMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO;
259 
260 	if (ret == UMATCH_NONE)
261 		return (ret);
262 
263 	/* umodem doesn't support devices without a data iface */
264 	umodem_get_caps(uaa, id->bInterfaceNumber, &data_iface_idx,
265 	    &cm_cap, &acm_cap);
266 	if (data_iface_idx == -1)
267 		ret = UMATCH_NONE;
268 
269 	return (ret);
270 }
271 
272 void
273 umodem_attach(struct device *parent, struct device *self, void *aux)
274 {
275 	struct umodem_softc *sc = (struct umodem_softc *)self;
276 	struct usb_attach_arg *uaa = aux;
277 	struct usbd_device *dev = uaa->device;
278 	usb_interface_descriptor_t *id;
279 	usb_endpoint_descriptor_t *ed;
280 	usbd_status err;
281 	int data_iface_idx = -1;
282 	int i;
283 	struct ucom_attach_args uca;
284 
285 	sc->sc_udev = dev;
286 	sc->sc_ctl_iface = uaa->iface;
287 
288 	id = usbd_get_interface_descriptor(sc->sc_ctl_iface);
289 	//printf("%s: iclass %d/%d\n", sc->sc_dev.dv_xname,
290 	//    id->bInterfaceClass, id->bInterfaceSubClass);
291 	sc->sc_ctl_iface_no = id->bInterfaceNumber;
292 
293 	/* Get the capabilities. */
294 	umodem_get_caps(uaa, id->bInterfaceNumber, &data_iface_idx,
295 	    &sc->sc_cm_cap, &sc->sc_acm_cap);
296 
297 	usbd_claim_iface(sc->sc_udev, data_iface_idx);
298 	sc->sc_data_iface = uaa->ifaces[data_iface_idx];
299 	id = usbd_get_interface_descriptor(sc->sc_data_iface);
300 
301 	printf("%s: data interface %d, has %sCM over data, has %sbreak\n",
302 	       sc->sc_dev.dv_xname, id->bInterfaceNumber,
303 	       sc->sc_cm_cap & USB_CDC_CM_OVER_DATA ? "" : "no ",
304 	       sc->sc_acm_cap & USB_CDC_ACM_HAS_BREAK ? "" : "no ");
305 
306 	/*
307 	 * Find the bulk endpoints.
308 	 * Iterate over all endpoints in the data interface and take note.
309 	 */
310 	uca.bulkin = uca.bulkout = -1;
311 
312 	for (i = 0; i < id->bNumEndpoints; i++) {
313 		ed = usbd_interface2endpoint_descriptor(sc->sc_data_iface, i);
314 		if (ed == NULL) {
315 			printf("%s: no endpoint descriptor for %d\n",
316 				sc->sc_dev.dv_xname, i);
317 			goto bad;
318 		}
319 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
320 		    (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
321                         uca.bulkin = ed->bEndpointAddress;
322                 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
323 			   (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
324                         uca.bulkout = ed->bEndpointAddress;
325                 }
326         }
327 
328 	if (uca.bulkin == -1) {
329 		printf("%s: Could not find data bulk in\n",
330 		       sc->sc_dev.dv_xname);
331 		goto bad;
332 	}
333 	if (uca.bulkout == -1) {
334 		printf("%s: Could not find data bulk out\n",
335 			sc->sc_dev.dv_xname);
336 		goto bad;
337 	}
338 
339 	if (usbd_get_quirks(sc->sc_udev)->uq_flags & UQ_ASSUME_CM_OVER_DATA) {
340 		sc->sc_cm_over_data = 1;
341 	} else {
342 		if (sc->sc_cm_cap & USB_CDC_CM_OVER_DATA) {
343 			if (sc->sc_acm_cap & USB_CDC_ACM_HAS_FEATURE)
344 				err = umodem_set_comm_feature(sc,
345 				    UCDC_ABSTRACT_STATE, UCDC_DATA_MULTIPLEXED);
346 			else
347 				err = 0;
348 			if (err) {
349 				printf("%s: could not set data multiplex mode\n",
350 				       sc->sc_dev.dv_xname);
351 				goto bad;
352 			}
353 			sc->sc_cm_over_data = 1;
354 		}
355 	}
356 
357 	/*
358 	 * The standard allows for notification messages (to indicate things
359 	 * like a modem hangup) to come in via an interrupt endpoint
360 	 * off of the control interface.  Iterate over the endpoints on
361 	 * the control interface and see if there are any interrupt
362 	 * endpoints; if there are, then register it.
363 	 */
364 
365 	sc->sc_ctl_notify = -1;
366 	sc->sc_notify_pipe = NULL;
367 
368 	id = usbd_get_interface_descriptor(sc->sc_ctl_iface);
369 	for (i = 0; i < id->bNumEndpoints; i++) {
370 		ed = usbd_interface2endpoint_descriptor(sc->sc_ctl_iface, i);
371 		if (ed == NULL)
372 			continue;
373 
374 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
375 		    (ed->bmAttributes & UE_XFERTYPE) == UE_INTERRUPT) {
376 			printf("%s: status change notification available\n",
377 			       sc->sc_dev.dv_xname);
378 			sc->sc_ctl_notify = ed->bEndpointAddress;
379 		}
380 	}
381 
382 	sc->sc_dtr = -1;
383 
384 	uca.portno = UCOM_UNK_PORTNO;
385 	/* bulkin, bulkout set above */
386 	uca.ibufsize = UMODEMIBUFSIZE;
387 	uca.obufsize = UMODEMOBUFSIZE;
388 	uca.ibufsizepad = UMODEMIBUFSIZE;
389 	uca.opkthdrlen = 0;
390 	uca.device = sc->sc_udev;
391 	uca.iface = sc->sc_data_iface;
392 	uca.methods = &umodem_methods;
393 	uca.arg = sc;
394 	uca.info = NULL;
395 
396 	DPRINTF(("umodem_attach: sc=%p\n", sc));
397 	sc->sc_subdev = config_found_sm(self, &uca, ucomprint, ucomsubmatch);
398 
399 	return;
400 
401  bad:
402 	usbd_deactivate(sc->sc_udev);
403 }
404 
405 int
406 umodem_open(void *addr, int portno)
407 {
408 	struct umodem_softc *sc = addr;
409 	int err;
410 
411 	DPRINTF(("umodem_open: sc=%p\n", sc));
412 
413 	if (sc->sc_ctl_notify != -1 && sc->sc_notify_pipe == NULL) {
414 		err = usbd_open_pipe_intr(sc->sc_ctl_iface, sc->sc_ctl_notify,
415 		    USBD_SHORT_XFER_OK, &sc->sc_notify_pipe, sc,
416 		    &sc->sc_notify_buf, sizeof(sc->sc_notify_buf),
417 		    umodem_intr, USBD_DEFAULT_INTERVAL);
418 
419 		if (err) {
420 			DPRINTF(("Failed to establish notify pipe: %s\n",
421 				usbd_errstr(err)));
422 			return EIO;
423 		}
424 	}
425 
426 	return 0;
427 }
428 
429 void
430 umodem_close(void *addr, int portno)
431 {
432 	struct umodem_softc *sc = addr;
433 	int err;
434 
435 	DPRINTF(("umodem_close: sc=%p\n", sc));
436 
437 	if (sc->sc_notify_pipe != NULL) {
438 		usbd_abort_pipe(sc->sc_notify_pipe);
439 		err = usbd_close_pipe(sc->sc_notify_pipe);
440 		if (err)
441 			printf("%s: close notify pipe failed: %s\n",
442 			    sc->sc_dev.dv_xname, usbd_errstr(err));
443 		sc->sc_notify_pipe = NULL;
444 	}
445 }
446 
447 void
448 umodem_intr(struct usbd_xfer *xfer, void *priv, usbd_status status)
449 {
450 	struct umodem_softc *sc = priv;
451 	u_char mstatus;
452 
453 	if (usbd_is_dying(sc->sc_udev))
454 		return;
455 
456 	if (status != USBD_NORMAL_COMPLETION) {
457 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
458 			return;
459 		DPRINTF(("%s: abnormal status: %s\n", sc->sc_dev.dv_xname,
460 		       usbd_errstr(status)));
461 		usbd_clear_endpoint_stall_async(sc->sc_notify_pipe);
462 		return;
463 	}
464 
465 	if (sc->sc_notify_buf.bmRequestType != UCDC_NOTIFICATION) {
466 		DPRINTF(("%s: unknown message type (%02x) on notify pipe\n",
467 			 sc->sc_dev.dv_xname,
468 			 sc->sc_notify_buf.bmRequestType));
469 		return;
470 	}
471 
472 	switch (sc->sc_notify_buf.bNotification) {
473 	case UCDC_N_SERIAL_STATE:
474 		/*
475 		 * Set the serial state in ucom driver based on
476 		 * the bits from the notify message
477 		 */
478 		if (UGETW(sc->sc_notify_buf.wLength) != 2) {
479 			printf("%s: Invalid notification length! (%d)\n",
480 			       sc->sc_dev.dv_xname,
481 			       UGETW(sc->sc_notify_buf.wLength));
482 			break;
483 		}
484 		DPRINTF(("%s: notify bytes = %02x%02x\n",
485 			 sc->sc_dev.dv_xname,
486 			 sc->sc_notify_buf.data[0],
487 			 sc->sc_notify_buf.data[1]));
488 		/* Currently, lsr is always zero. */
489 		sc->sc_lsr = sc->sc_msr = 0;
490 		mstatus = sc->sc_notify_buf.data[0];
491 
492 		if (ISSET(mstatus, UCDC_N_SERIAL_RI))
493 			sc->sc_msr |= UMSR_RI;
494 		if (ISSET(mstatus, UCDC_N_SERIAL_DSR))
495 			sc->sc_msr |= UMSR_DSR;
496 		if (ISSET(mstatus, UCDC_N_SERIAL_DCD))
497 			sc->sc_msr |= UMSR_DCD;
498 		ucom_status_change((struct ucom_softc *)sc->sc_subdev);
499 		break;
500 	default:
501 		DPRINTF(("%s: unknown notify message: %02x\n",
502 			 sc->sc_dev.dv_xname,
503 			 sc->sc_notify_buf.bNotification));
504 		break;
505 	}
506 }
507 
508 void
509 umodem_get_status(void *addr, int portno, u_char *lsr, u_char *msr)
510 {
511 	struct umodem_softc *sc = addr;
512 
513 	DPRINTF(("umodem_get_status:\n"));
514 
515 	if (lsr != NULL)
516 		*lsr = sc->sc_lsr;
517 	if (msr != NULL)
518 		*msr = sc->sc_msr;
519 }
520 
521 int
522 umodem_param(void *addr, int portno, struct termios *t)
523 {
524 	struct umodem_softc *sc = addr;
525 	usbd_status err;
526 	struct usb_cdc_line_state ls;
527 
528 	DPRINTF(("umodem_param: sc=%p\n", sc));
529 
530 	USETDW(ls.dwDTERate, t->c_ospeed);
531 	if (ISSET(t->c_cflag, CSTOPB))
532 		ls.bCharFormat = UCDC_STOP_BIT_2;
533 	else
534 		ls.bCharFormat = UCDC_STOP_BIT_1;
535 	if (ISSET(t->c_cflag, PARENB)) {
536 		if (ISSET(t->c_cflag, PARODD))
537 			ls.bParityType = UCDC_PARITY_ODD;
538 		else
539 			ls.bParityType = UCDC_PARITY_EVEN;
540 	} else
541 		ls.bParityType = UCDC_PARITY_NONE;
542 	switch (ISSET(t->c_cflag, CSIZE)) {
543 	case CS5:
544 		ls.bDataBits = 5;
545 		break;
546 	case CS6:
547 		ls.bDataBits = 6;
548 		break;
549 	case CS7:
550 		ls.bDataBits = 7;
551 		break;
552 	case CS8:
553 		ls.bDataBits = 8;
554 		break;
555 	}
556 
557 	err = umodem_set_line_coding(sc, &ls);
558 	if (err) {
559 		DPRINTF(("umodem_param: err=%s\n", usbd_errstr(err)));
560 		return (1);
561 	}
562 	return (0);
563 }
564 
565 void
566 umodem_dtr(struct umodem_softc *sc, int onoff)
567 {
568 	DPRINTF(("umodem_dtr: onoff=%d\n", onoff));
569 
570 	if (sc->sc_dtr == onoff)
571 		return;
572 	sc->sc_dtr = onoff;
573 
574 	umodem_set_line_state(sc);
575 }
576 
577 void
578 umodem_rts(struct umodem_softc *sc, int onoff)
579 {
580 	DPRINTF(("umodem_rts: onoff=%d\n", onoff));
581 
582 	if (sc->sc_rts == onoff)
583 		return;
584 	sc->sc_rts = onoff;
585 
586 	umodem_set_line_state(sc);
587 }
588 
589 void
590 umodem_set_line_state(struct umodem_softc *sc)
591 {
592 	usb_device_request_t req;
593 	int ls;
594 
595 	ls = (sc->sc_dtr ? UCDC_LINE_DTR : 0) |
596 	     (sc->sc_rts ? UCDC_LINE_RTS : 0);
597 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
598 	req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
599 	USETW(req.wValue, ls);
600 	USETW(req.wIndex, sc->sc_ctl_iface_no);
601 	USETW(req.wLength, 0);
602 
603 	(void)usbd_do_request(sc->sc_udev, &req, 0);
604 
605 }
606 
607 void
608 umodem_break(struct umodem_softc *sc, int onoff)
609 {
610 	usb_device_request_t req;
611 
612 	DPRINTF(("umodem_break: onoff=%d\n", onoff));
613 
614 	if (!(sc->sc_acm_cap & USB_CDC_ACM_HAS_BREAK))
615 		return;
616 
617 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
618 	req.bRequest = UCDC_SEND_BREAK;
619 	USETW(req.wValue, onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF);
620 	USETW(req.wIndex, sc->sc_ctl_iface_no);
621 	USETW(req.wLength, 0);
622 
623 	(void)usbd_do_request(sc->sc_udev, &req, 0);
624 }
625 
626 void
627 umodem_set(void *addr, int portno, int reg, int onoff)
628 {
629 	struct umodem_softc *sc = addr;
630 
631 	switch (reg) {
632 	case UCOM_SET_DTR:
633 		umodem_dtr(sc, onoff);
634 		break;
635 	case UCOM_SET_RTS:
636 		umodem_rts(sc, onoff);
637 		break;
638 	case UCOM_SET_BREAK:
639 		umodem_break(sc, onoff);
640 		break;
641 	default:
642 		break;
643 	}
644 }
645 
646 usbd_status
647 umodem_set_line_coding(struct umodem_softc *sc,
648     struct usb_cdc_line_state *state)
649 {
650 	usb_device_request_t req;
651 	usbd_status err;
652 
653 	DPRINTF(("umodem_set_line_coding: rate=%d fmt=%d parity=%d bits=%d\n",
654 		 UGETDW(state->dwDTERate), state->bCharFormat,
655 		 state->bParityType, state->bDataBits));
656 
657 	if (memcmp(state, &sc->sc_line_state, UCDC_LINE_STATE_LENGTH) == 0) {
658 		DPRINTF(("umodem_set_line_coding: already set\n"));
659 		return (USBD_NORMAL_COMPLETION);
660 	}
661 
662 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
663 	req.bRequest = UCDC_SET_LINE_CODING;
664 	USETW(req.wValue, 0);
665 	USETW(req.wIndex, sc->sc_ctl_iface_no);
666 	USETW(req.wLength, UCDC_LINE_STATE_LENGTH);
667 
668 	err = usbd_do_request(sc->sc_udev, &req, state);
669 	if (err) {
670 		DPRINTF(("umodem_set_line_coding: failed, err=%s\n",
671 			 usbd_errstr(err)));
672 		return (err);
673 	}
674 
675 	sc->sc_line_state = *state;
676 
677 	return (USBD_NORMAL_COMPLETION);
678 }
679 
680 usbd_status
681 umodem_set_comm_feature(struct umodem_softc *sc, int feature, int state)
682 {
683 	usb_device_request_t req;
684 	usbd_status err;
685 	struct usb_cdc_abstract_state ast;
686 
687 	DPRINTF(("umodem_set_comm_feature: feature=%d state=%d\n", feature,
688 		 state));
689 
690 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
691 	req.bRequest = UCDC_SET_COMM_FEATURE;
692 	USETW(req.wValue, feature);
693 	USETW(req.wIndex, sc->sc_ctl_iface_no);
694 	USETW(req.wLength, UCDC_ABSTRACT_STATE_LENGTH);
695 	USETW(ast.wState, state);
696 
697 	err = usbd_do_request(sc->sc_udev, &req, &ast);
698 	if (err) {
699 		DPRINTF(("umodem_set_comm_feature: feature=%d, err=%s\n",
700 			 feature, usbd_errstr(err)));
701 		return (err);
702 	}
703 
704 	return (USBD_NORMAL_COMPLETION);
705 }
706 
707 int
708 umodem_detach(struct device *self, int flags)
709 {
710 	struct umodem_softc *sc = (struct umodem_softc *)self;
711 	int rv = 0;
712 
713 	DPRINTF(("umodem_detach: sc=%p flags=%d\n", sc, flags));
714 
715 	if (sc->sc_notify_pipe != NULL) {
716 		usbd_abort_pipe(sc->sc_notify_pipe);
717 		usbd_close_pipe(sc->sc_notify_pipe);
718 		sc->sc_notify_pipe = NULL;
719 	}
720 
721 	if (sc->sc_subdev != NULL)
722 		rv = config_detach(sc->sc_subdev, flags);
723 
724 	return (rv);
725 }
726