xref: /openbsd-src/sys/dev/usb/umodem.c (revision c7e8ea31cd41a963f06f0a8ba93948b06aa6b4a4)
1 /*	$OpenBSD: umodem.c,v 1.62 2017/03/29 01:27:40 jsg 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 	if (UGETW(dd->idVendor) == USB_VENDOR_KYOCERA &&
241 	    UGETW(dd->idProduct) == USB_PRODUCT_KYOCERA_AHK3001V &&
242 	    id->bInterfaceNumber == 0)
243 		ret = UMATCH_VENDOR_PRODUCT;
244 
245 	if (ret == UMATCH_NONE &&
246 	    id->bInterfaceClass == UICLASS_CDC &&
247 	    id->bInterfaceSubClass == UISUBCLASS_ABSTRACT_CONTROL_MODEL &&
248 	    (id->bInterfaceProtocol == UIPROTO_CDC_AT ||
249 	    id->bInterfaceProtocol == UIPROTO_CDC_NOCLASS))
250 		ret = UMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO;
251 
252 	if (ret == UMATCH_NONE)
253 		return (ret);
254 
255 	/* umodem doesn't support devices without a data iface */
256 	umodem_get_caps(uaa, id->bInterfaceNumber, &data_iface_idx,
257 	    &cm_cap, &acm_cap);
258 	if (data_iface_idx == -1)
259 		ret = UMATCH_NONE;
260 
261 	return (ret);
262 }
263 
264 void
265 umodem_attach(struct device *parent, struct device *self, void *aux)
266 {
267 	struct umodem_softc *sc = (struct umodem_softc *)self;
268 	struct usb_attach_arg *uaa = aux;
269 	struct usbd_device *dev = uaa->device;
270 	usb_interface_descriptor_t *id;
271 	usb_endpoint_descriptor_t *ed;
272 	usbd_status err;
273 	int data_iface_idx = -1;
274 	int i;
275 	struct ucom_attach_args uca;
276 
277 	sc->sc_udev = dev;
278 	sc->sc_ctl_iface = uaa->iface;
279 
280 	id = usbd_get_interface_descriptor(sc->sc_ctl_iface);
281 	//printf("%s: iclass %d/%d\n", sc->sc_dev.dv_xname,
282 	//    id->bInterfaceClass, id->bInterfaceSubClass);
283 	sc->sc_ctl_iface_no = id->bInterfaceNumber;
284 
285 	/* Get the capabilities. */
286 	umodem_get_caps(uaa, id->bInterfaceNumber, &data_iface_idx,
287 	    &sc->sc_cm_cap, &sc->sc_acm_cap);
288 
289 	usbd_claim_iface(sc->sc_udev, data_iface_idx);
290 	sc->sc_data_iface = uaa->ifaces[data_iface_idx];
291 	id = usbd_get_interface_descriptor(sc->sc_data_iface);
292 
293 	printf("%s: data interface %d, has %sCM over data, has %sbreak\n",
294 	       sc->sc_dev.dv_xname, id->bInterfaceNumber,
295 	       sc->sc_cm_cap & USB_CDC_CM_OVER_DATA ? "" : "no ",
296 	       sc->sc_acm_cap & USB_CDC_ACM_HAS_BREAK ? "" : "no ");
297 
298 	/*
299 	 * Find the bulk endpoints.
300 	 * Iterate over all endpoints in the data interface and take note.
301 	 */
302 	uca.bulkin = uca.bulkout = -1;
303 
304 	for (i = 0; i < id->bNumEndpoints; i++) {
305 		ed = usbd_interface2endpoint_descriptor(sc->sc_data_iface, i);
306 		if (ed == NULL) {
307 			printf("%s: no endpoint descriptor for %d\n",
308 				sc->sc_dev.dv_xname, i);
309 			goto bad;
310 		}
311 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
312 		    (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
313                         uca.bulkin = ed->bEndpointAddress;
314                 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
315 			   (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
316                         uca.bulkout = ed->bEndpointAddress;
317                 }
318         }
319 
320 	if (uca.bulkin == -1) {
321 		printf("%s: Could not find data bulk in\n",
322 		       sc->sc_dev.dv_xname);
323 		goto bad;
324 	}
325 	if (uca.bulkout == -1) {
326 		printf("%s: Could not find data bulk out\n",
327 			sc->sc_dev.dv_xname);
328 		goto bad;
329 	}
330 
331 	if (usbd_get_quirks(sc->sc_udev)->uq_flags & UQ_ASSUME_CM_OVER_DATA) {
332 		sc->sc_cm_over_data = 1;
333 	} else {
334 		if (sc->sc_cm_cap & USB_CDC_CM_OVER_DATA) {
335 			if (sc->sc_acm_cap & USB_CDC_ACM_HAS_FEATURE)
336 				err = umodem_set_comm_feature(sc,
337 				    UCDC_ABSTRACT_STATE, UCDC_DATA_MULTIPLEXED);
338 			else
339 				err = 0;
340 			if (err) {
341 				printf("%s: could not set data multiplex mode\n",
342 				       sc->sc_dev.dv_xname);
343 				goto bad;
344 			}
345 			sc->sc_cm_over_data = 1;
346 		}
347 	}
348 
349 	/*
350 	 * The standard allows for notification messages (to indicate things
351 	 * like a modem hangup) to come in via an interrupt endpoint
352 	 * off of the control interface.  Iterate over the endpoints on
353 	 * the control interface and see if there are any interrupt
354 	 * endpoints; if there are, then register it.
355 	 */
356 
357 	sc->sc_ctl_notify = -1;
358 	sc->sc_notify_pipe = NULL;
359 
360 	id = usbd_get_interface_descriptor(sc->sc_ctl_iface);
361 	for (i = 0; i < id->bNumEndpoints; i++) {
362 		ed = usbd_interface2endpoint_descriptor(sc->sc_ctl_iface, i);
363 		if (ed == NULL)
364 			continue;
365 
366 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
367 		    (ed->bmAttributes & UE_XFERTYPE) == UE_INTERRUPT) {
368 			printf("%s: status change notification available\n",
369 			       sc->sc_dev.dv_xname);
370 			sc->sc_ctl_notify = ed->bEndpointAddress;
371 		}
372 	}
373 
374 	sc->sc_dtr = -1;
375 
376 	uca.portno = UCOM_UNK_PORTNO;
377 	/* bulkin, bulkout set above */
378 	uca.ibufsize = UMODEMIBUFSIZE;
379 	uca.obufsize = UMODEMOBUFSIZE;
380 	uca.ibufsizepad = UMODEMIBUFSIZE;
381 	uca.opkthdrlen = 0;
382 	uca.device = sc->sc_udev;
383 	uca.iface = sc->sc_data_iface;
384 	uca.methods = &umodem_methods;
385 	uca.arg = sc;
386 	uca.info = NULL;
387 
388 	DPRINTF(("umodem_attach: sc=%p\n", sc));
389 	sc->sc_subdev = config_found_sm(self, &uca, ucomprint, ucomsubmatch);
390 
391 	return;
392 
393  bad:
394 	usbd_deactivate(sc->sc_udev);
395 }
396 
397 int
398 umodem_open(void *addr, int portno)
399 {
400 	struct umodem_softc *sc = addr;
401 	int err;
402 
403 	DPRINTF(("umodem_open: sc=%p\n", sc));
404 
405 	if (sc->sc_ctl_notify != -1 && sc->sc_notify_pipe == NULL) {
406 		err = usbd_open_pipe_intr(sc->sc_ctl_iface, sc->sc_ctl_notify,
407 		    USBD_SHORT_XFER_OK, &sc->sc_notify_pipe, sc,
408 		    &sc->sc_notify_buf, sizeof(sc->sc_notify_buf),
409 		    umodem_intr, USBD_DEFAULT_INTERVAL);
410 
411 		if (err) {
412 			DPRINTF(("Failed to establish notify pipe: %s\n",
413 				usbd_errstr(err)));
414 			return EIO;
415 		}
416 	}
417 
418 	return 0;
419 }
420 
421 void
422 umodem_close(void *addr, int portno)
423 {
424 	struct umodem_softc *sc = addr;
425 	int err;
426 
427 	DPRINTF(("umodem_close: sc=%p\n", sc));
428 
429 	if (sc->sc_notify_pipe != NULL) {
430 		usbd_abort_pipe(sc->sc_notify_pipe);
431 		err = usbd_close_pipe(sc->sc_notify_pipe);
432 		if (err)
433 			printf("%s: close notify pipe failed: %s\n",
434 			    sc->sc_dev.dv_xname, usbd_errstr(err));
435 		sc->sc_notify_pipe = NULL;
436 	}
437 }
438 
439 void
440 umodem_intr(struct usbd_xfer *xfer, void *priv, usbd_status status)
441 {
442 	struct umodem_softc *sc = priv;
443 	u_char mstatus;
444 
445 	if (usbd_is_dying(sc->sc_udev))
446 		return;
447 
448 	if (status != USBD_NORMAL_COMPLETION) {
449 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
450 			return;
451 		DPRINTF(("%s: abnormal status: %s\n", sc->sc_dev.dv_xname,
452 		       usbd_errstr(status)));
453 		usbd_clear_endpoint_stall_async(sc->sc_notify_pipe);
454 		return;
455 	}
456 
457 	if (sc->sc_notify_buf.bmRequestType != UCDC_NOTIFICATION) {
458 		DPRINTF(("%s: unknown message type (%02x) on notify pipe\n",
459 			 sc->sc_dev.dv_xname,
460 			 sc->sc_notify_buf.bmRequestType));
461 		return;
462 	}
463 
464 	switch (sc->sc_notify_buf.bNotification) {
465 	case UCDC_N_SERIAL_STATE:
466 		/*
467 		 * Set the serial state in ucom driver based on
468 		 * the bits from the notify message
469 		 */
470 		if (UGETW(sc->sc_notify_buf.wLength) != 2) {
471 			printf("%s: Invalid notification length! (%d)\n",
472 			       sc->sc_dev.dv_xname,
473 			       UGETW(sc->sc_notify_buf.wLength));
474 			break;
475 		}
476 		DPRINTF(("%s: notify bytes = %02x%02x\n",
477 			 sc->sc_dev.dv_xname,
478 			 sc->sc_notify_buf.data[0],
479 			 sc->sc_notify_buf.data[1]));
480 		/* Currently, lsr is always zero. */
481 		sc->sc_lsr = sc->sc_msr = 0;
482 		mstatus = sc->sc_notify_buf.data[0];
483 
484 		if (ISSET(mstatus, UCDC_N_SERIAL_RI))
485 			sc->sc_msr |= UMSR_RI;
486 		if (ISSET(mstatus, UCDC_N_SERIAL_DSR))
487 			sc->sc_msr |= UMSR_DSR;
488 		if (ISSET(mstatus, UCDC_N_SERIAL_DCD))
489 			sc->sc_msr |= UMSR_DCD;
490 		ucom_status_change((struct ucom_softc *)sc->sc_subdev);
491 		break;
492 	default:
493 		DPRINTF(("%s: unknown notify message: %02x\n",
494 			 sc->sc_dev.dv_xname,
495 			 sc->sc_notify_buf.bNotification));
496 		break;
497 	}
498 }
499 
500 void
501 umodem_get_status(void *addr, int portno, u_char *lsr, u_char *msr)
502 {
503 	struct umodem_softc *sc = addr;
504 
505 	DPRINTF(("umodem_get_status:\n"));
506 
507 	if (lsr != NULL)
508 		*lsr = sc->sc_lsr;
509 	if (msr != NULL)
510 		*msr = sc->sc_msr;
511 }
512 
513 int
514 umodem_param(void *addr, int portno, struct termios *t)
515 {
516 	struct umodem_softc *sc = addr;
517 	usbd_status err;
518 	struct usb_cdc_line_state ls;
519 
520 	DPRINTF(("umodem_param: sc=%p\n", sc));
521 
522 	USETDW(ls.dwDTERate, t->c_ospeed);
523 	if (ISSET(t->c_cflag, CSTOPB))
524 		ls.bCharFormat = UCDC_STOP_BIT_2;
525 	else
526 		ls.bCharFormat = UCDC_STOP_BIT_1;
527 	if (ISSET(t->c_cflag, PARENB)) {
528 		if (ISSET(t->c_cflag, PARODD))
529 			ls.bParityType = UCDC_PARITY_ODD;
530 		else
531 			ls.bParityType = UCDC_PARITY_EVEN;
532 	} else
533 		ls.bParityType = UCDC_PARITY_NONE;
534 	switch (ISSET(t->c_cflag, CSIZE)) {
535 	case CS5:
536 		ls.bDataBits = 5;
537 		break;
538 	case CS6:
539 		ls.bDataBits = 6;
540 		break;
541 	case CS7:
542 		ls.bDataBits = 7;
543 		break;
544 	case CS8:
545 		ls.bDataBits = 8;
546 		break;
547 	}
548 
549 	err = umodem_set_line_coding(sc, &ls);
550 	if (err) {
551 		DPRINTF(("umodem_param: err=%s\n", usbd_errstr(err)));
552 		return (1);
553 	}
554 	return (0);
555 }
556 
557 void
558 umodem_dtr(struct umodem_softc *sc, int onoff)
559 {
560 	DPRINTF(("umodem_dtr: onoff=%d\n", onoff));
561 
562 	if (sc->sc_dtr == onoff)
563 		return;
564 	sc->sc_dtr = onoff;
565 
566 	umodem_set_line_state(sc);
567 }
568 
569 void
570 umodem_rts(struct umodem_softc *sc, int onoff)
571 {
572 	DPRINTF(("umodem_rts: onoff=%d\n", onoff));
573 
574 	if (sc->sc_rts == onoff)
575 		return;
576 	sc->sc_rts = onoff;
577 
578 	umodem_set_line_state(sc);
579 }
580 
581 void
582 umodem_set_line_state(struct umodem_softc *sc)
583 {
584 	usb_device_request_t req;
585 	int ls;
586 
587 	ls = (sc->sc_dtr ? UCDC_LINE_DTR : 0) |
588 	     (sc->sc_rts ? UCDC_LINE_RTS : 0);
589 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
590 	req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
591 	USETW(req.wValue, ls);
592 	USETW(req.wIndex, sc->sc_ctl_iface_no);
593 	USETW(req.wLength, 0);
594 
595 	(void)usbd_do_request(sc->sc_udev, &req, 0);
596 
597 }
598 
599 void
600 umodem_break(struct umodem_softc *sc, int onoff)
601 {
602 	usb_device_request_t req;
603 
604 	DPRINTF(("umodem_break: onoff=%d\n", onoff));
605 
606 	if (!(sc->sc_acm_cap & USB_CDC_ACM_HAS_BREAK))
607 		return;
608 
609 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
610 	req.bRequest = UCDC_SEND_BREAK;
611 	USETW(req.wValue, onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF);
612 	USETW(req.wIndex, sc->sc_ctl_iface_no);
613 	USETW(req.wLength, 0);
614 
615 	(void)usbd_do_request(sc->sc_udev, &req, 0);
616 }
617 
618 void
619 umodem_set(void *addr, int portno, int reg, int onoff)
620 {
621 	struct umodem_softc *sc = addr;
622 
623 	switch (reg) {
624 	case UCOM_SET_DTR:
625 		umodem_dtr(sc, onoff);
626 		break;
627 	case UCOM_SET_RTS:
628 		umodem_rts(sc, onoff);
629 		break;
630 	case UCOM_SET_BREAK:
631 		umodem_break(sc, onoff);
632 		break;
633 	default:
634 		break;
635 	}
636 }
637 
638 usbd_status
639 umodem_set_line_coding(struct umodem_softc *sc,
640     struct usb_cdc_line_state *state)
641 {
642 	usb_device_request_t req;
643 	usbd_status err;
644 
645 	DPRINTF(("umodem_set_line_coding: rate=%d fmt=%d parity=%d bits=%d\n",
646 		 UGETDW(state->dwDTERate), state->bCharFormat,
647 		 state->bParityType, state->bDataBits));
648 
649 	if (memcmp(state, &sc->sc_line_state, UCDC_LINE_STATE_LENGTH) == 0) {
650 		DPRINTF(("umodem_set_line_coding: already set\n"));
651 		return (USBD_NORMAL_COMPLETION);
652 	}
653 
654 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
655 	req.bRequest = UCDC_SET_LINE_CODING;
656 	USETW(req.wValue, 0);
657 	USETW(req.wIndex, sc->sc_ctl_iface_no);
658 	USETW(req.wLength, UCDC_LINE_STATE_LENGTH);
659 
660 	err = usbd_do_request(sc->sc_udev, &req, state);
661 	if (err) {
662 		DPRINTF(("umodem_set_line_coding: failed, err=%s\n",
663 			 usbd_errstr(err)));
664 		return (err);
665 	}
666 
667 	sc->sc_line_state = *state;
668 
669 	return (USBD_NORMAL_COMPLETION);
670 }
671 
672 usbd_status
673 umodem_set_comm_feature(struct umodem_softc *sc, int feature, int state)
674 {
675 	usb_device_request_t req;
676 	usbd_status err;
677 	struct usb_cdc_abstract_state ast;
678 
679 	DPRINTF(("umodem_set_comm_feature: feature=%d state=%d\n", feature,
680 		 state));
681 
682 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
683 	req.bRequest = UCDC_SET_COMM_FEATURE;
684 	USETW(req.wValue, feature);
685 	USETW(req.wIndex, sc->sc_ctl_iface_no);
686 	USETW(req.wLength, UCDC_ABSTRACT_STATE_LENGTH);
687 	USETW(ast.wState, state);
688 
689 	err = usbd_do_request(sc->sc_udev, &req, &ast);
690 	if (err) {
691 		DPRINTF(("umodem_set_comm_feature: feature=%d, err=%s\n",
692 			 feature, usbd_errstr(err)));
693 		return (err);
694 	}
695 
696 	return (USBD_NORMAL_COMPLETION);
697 }
698 
699 int
700 umodem_detach(struct device *self, int flags)
701 {
702 	struct umodem_softc *sc = (struct umodem_softc *)self;
703 	int rv = 0;
704 
705 	DPRINTF(("umodem_detach: sc=%p flags=%d\n", sc, flags));
706 
707 	if (sc->sc_notify_pipe != NULL) {
708 		usbd_abort_pipe(sc->sc_notify_pipe);
709 		usbd_close_pipe(sc->sc_notify_pipe);
710 		sc->sc_notify_pipe = NULL;
711 	}
712 
713 	if (sc->sc_subdev != NULL)
714 		rv = config_detach(sc->sc_subdev, flags);
715 
716 	return (rv);
717 }
718