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