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