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