xref: /openbsd-src/sys/dev/usb/umodem.c (revision db3296cf5c1dd9058ceecc3a29fe4aaa0bd26000)
1 /*	$OpenBSD: umodem.c,v 1.14 2003/05/07 04:33:33 deraadt 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  * 3. All advertising materials mentioning features or use of this software
21  *    must display the following acknowledgement:
22  *        This product includes software developed by the NetBSD
23  *        Foundation, Inc. and its contributors.
24  * 4. Neither the name of The NetBSD Foundation nor the names of its
25  *    contributors may be used to endorse or promote products derived
26  *    from this software without specific prior written permission.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
29  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
31  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
32  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38  * POSSIBILITY OF SUCH DAMAGE.
39  */
40 
41 /*
42  * Comm Class spec:  http://www.usb.org/developers/data/devclass/usbcdc10.pdf
43  *                   http://www.usb.org/developers/data/devclass/usbcdc11.pdf
44  */
45 
46 /*
47  * TODO:
48  * - Add error recovery in various places; the big problem is what
49  *   to do in a callback if there is an error.
50  * - Implement a Call Device for modems without multiplexed commands.
51  *
52  */
53 
54 #include <sys/param.h>
55 #include <sys/systm.h>
56 #include <sys/kernel.h>
57 #include <sys/ioctl.h>
58 #include <sys/conf.h>
59 #include <sys/tty.h>
60 #include <sys/file.h>
61 #include <sys/select.h>
62 #include <sys/proc.h>
63 #include <sys/vnode.h>
64 #include <sys/device.h>
65 #include <sys/poll.h>
66 
67 #include <dev/usb/usb.h>
68 #include <dev/usb/usbcdc.h>
69 
70 #include <dev/usb/usbdi.h>
71 #include <dev/usb/usbdi_util.h>
72 #include <dev/usb/usbdevs.h>
73 #include <dev/usb/usb_quirks.h>
74 
75 #include <dev/usb/usbdevs.h>
76 #include <dev/usb/ucomvar.h>
77 
78 #ifdef UMODEM_DEBUG
79 #define DPRINTFN(n, x)	if (umodemdebug > (n)) logprintf x
80 int	umodemdebug = 0;
81 #else
82 #define DPRINTFN(n, x)
83 #endif
84 #define DPRINTF(x) DPRINTFN(0, x)
85 
86 /*
87  * These are the maximum number of bytes transferred per frame.
88  * If some really high speed devices should use this driver they
89  * may need to be increased, but this is good enough for normal modems.
90  */
91 #define UMODEMIBUFSIZE 64
92 #define UMODEMOBUFSIZE 256
93 
94 struct umodem_softc {
95 	USBBASEDEVICE		sc_dev;		/* base device */
96 
97 	usbd_device_handle	sc_udev;	/* USB device */
98 
99 	int			sc_ctl_iface_no;
100 	usbd_interface_handle	sc_ctl_iface;	/* control interface */
101 	int			sc_data_iface_no;
102 	usbd_interface_handle	sc_data_iface;	/* data interface */
103 
104 	int			sc_cm_cap;	/* CM capabilities */
105 	int			sc_acm_cap;	/* ACM capabilities */
106 
107 	int			sc_cm_over_data;
108 
109 	usb_cdc_line_state_t	sc_line_state;	/* current line state */
110 	u_char			sc_dtr;		/* current DTR state */
111 	u_char			sc_rts;		/* current RTS state */
112 
113 	device_ptr_t		sc_subdev;	/* ucom device */
114 
115 	u_char			sc_opening;	/* lock during open */
116 	u_char			sc_dying;	/* disconnecting */
117 
118 	int			sc_ctl_notify;	/* Notification endpoint */
119 	usbd_pipe_handle	sc_notify_pipe; /* Notification pipe */
120 	usb_cdc_notification_t	sc_notify_buf;	/* Notification structure */
121 	u_char			sc_lsr;		/* Local status register */
122 	u_char			sc_msr;		/* Modem status register */
123 };
124 
125 Static void	*umodem_get_desc(usbd_device_handle dev, int type, int subtype);
126 Static usbd_status umodem_set_comm_feature(struct umodem_softc *sc,
127 					   int feature, int state);
128 Static usbd_status umodem_set_line_coding(struct umodem_softc *sc,
129 					  usb_cdc_line_state_t *state);
130 
131 Static void	umodem_get_caps(usbd_device_handle, int *, int *);
132 
133 Static void	umodem_get_status(void *, int portno, u_char *lsr, u_char *msr);
134 Static void	umodem_set(void *, int, int, int);
135 Static void	umodem_dtr(struct umodem_softc *, int);
136 Static void	umodem_rts(struct umodem_softc *, int);
137 Static void	umodem_break(struct umodem_softc *, int);
138 Static void	umodem_set_line_state(struct umodem_softc *);
139 Static int	umodem_param(void *, int, struct termios *);
140 Static int	umodem_ioctl(void *, int, u_long, caddr_t, int, usb_proc_ptr);
141 Static int	umodem_open(void *, int portno);
142 Static void	umodem_close(void *, int portno);
143 Static void	umodem_intr(usbd_xfer_handle, usbd_private_handle, usbd_status);
144 
145 Static struct ucom_methods umodem_methods = {
146 	umodem_get_status,
147 	umodem_set,
148 	umodem_param,
149 	umodem_ioctl,
150 	umodem_open,
151 	umodem_close,
152 	NULL,
153 	NULL,
154 };
155 
156 USB_DECLARE_DRIVER(umodem);
157 
158 USB_MATCH(umodem)
159 {
160 	USB_MATCH_START(umodem, uaa);
161 	usb_interface_descriptor_t *id;
162 	int cm, acm;
163 
164 	if (uaa->iface == NULL)
165 		return (UMATCH_NONE);
166 
167 	id = usbd_get_interface_descriptor(uaa->iface);
168 	if (id == NULL ||
169 	    id->bInterfaceClass != UICLASS_CDC ||
170 	    id->bInterfaceSubClass != UISUBCLASS_ABSTRACT_CONTROL_MODEL ||
171 	    id->bInterfaceProtocol != UIPROTO_CDC_AT)
172 		return (UMATCH_NONE);
173 
174 	umodem_get_caps(uaa->device, &cm, &acm);
175 	if (!(cm & USB_CDC_CM_DOES_CM) ||
176 	    !(cm & USB_CDC_CM_OVER_DATA) ||
177 	    !(acm & USB_CDC_ACM_HAS_LINE))
178 		return (UMATCH_NONE);
179 
180 	return (UMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO);
181 }
182 
183 USB_ATTACH(umodem)
184 {
185 	USB_ATTACH_START(umodem, sc, uaa);
186 	usbd_device_handle dev = uaa->device;
187 	usb_interface_descriptor_t *id;
188 	usb_endpoint_descriptor_t *ed;
189 	usb_cdc_cm_descriptor_t *cmd;
190 	char devinfo[1024];
191 	usbd_status err;
192 	int data_ifcno;
193 	int i;
194 	struct ucom_attach_args uca;
195 
196 	usbd_devinfo(uaa->device, 0, devinfo, sizeof devinfo);
197 	USB_ATTACH_SETUP;
198 
199 	sc->sc_udev = dev;
200 	sc->sc_ctl_iface = uaa->iface;
201 
202 	id = usbd_get_interface_descriptor(sc->sc_ctl_iface);
203 	printf("%s: %s, iclass %d/%d\n", USBDEVNAME(sc->sc_dev),
204 	       devinfo, id->bInterfaceClass, id->bInterfaceSubClass);
205 	sc->sc_ctl_iface_no = id->bInterfaceNumber;
206 
207 	umodem_get_caps(dev, &sc->sc_cm_cap, &sc->sc_acm_cap);
208 
209 	/* Get the data interface no. */
210 	cmd = umodem_get_desc(dev, UDESC_CS_INTERFACE, UDESCSUB_CDC_CM);
211 	if (cmd == NULL) {
212 		printf("%s: no CM descriptor\n", USBDEVNAME(sc->sc_dev));
213 		goto bad;
214 	}
215 	sc->sc_data_iface_no = data_ifcno = cmd->bDataInterface;
216 
217 	printf("%s: data interface %d, has %sCM over data, has %sbreak\n",
218 	       USBDEVNAME(sc->sc_dev), data_ifcno,
219 	       sc->sc_cm_cap & USB_CDC_CM_OVER_DATA ? "" : "no ",
220 	       sc->sc_acm_cap & USB_CDC_ACM_HAS_BREAK ? "" : "no ");
221 
222 	/* Get the data interface too. */
223 	for (i = 0; i < uaa->nifaces; i++) {
224 		if (uaa->ifaces[i] != NULL) {
225 			id = usbd_get_interface_descriptor(uaa->ifaces[i]);
226 			if (id != NULL && id->bInterfaceNumber == data_ifcno) {
227 				sc->sc_data_iface = uaa->ifaces[i];
228 				uaa->ifaces[i] = NULL;
229 			}
230 		}
231 	}
232 	if (sc->sc_data_iface == NULL) {
233 		printf("%s: no data interface\n", USBDEVNAME(sc->sc_dev));
234 		goto bad;
235 	}
236 
237 	/*
238 	 * Find the bulk endpoints.
239 	 * Iterate over all endpoints in the data interface and take note.
240 	 */
241 	uca.bulkin = uca.bulkout = -1;
242 
243 	id = usbd_get_interface_descriptor(sc->sc_data_iface);
244 	for (i = 0; i < id->bNumEndpoints; i++) {
245 		ed = usbd_interface2endpoint_descriptor(sc->sc_data_iface, i);
246 		if (ed == NULL) {
247 			printf("%s: no endpoint descriptor for %d\n",
248 				USBDEVNAME(sc->sc_dev), i);
249 			goto bad;
250 		}
251 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
252 		    (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
253                         uca.bulkin = ed->bEndpointAddress;
254                 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
255 			   (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
256                         uca.bulkout = ed->bEndpointAddress;
257                 }
258         }
259 
260 	if (uca.bulkin == -1) {
261 		printf("%s: Could not find data bulk in\n",
262 		       USBDEVNAME(sc->sc_dev));
263 		goto bad;
264 	}
265 	if (uca.bulkout == -1) {
266 		printf("%s: Could not find data bulk out\n",
267 			USBDEVNAME(sc->sc_dev));
268 		goto bad;
269 	}
270 
271 	if (usbd_get_quirks(sc->sc_udev)->uq_flags & UQ_ASSUME_CM_OVER_DATA) {
272 		sc->sc_cm_over_data = 1;
273 	} else {
274 		if (sc->sc_cm_cap & USB_CDC_CM_OVER_DATA) {
275 			if (sc->sc_acm_cap & USB_CDC_ACM_HAS_FEATURE)
276 				err = umodem_set_comm_feature(sc,
277 				    UCDC_ABSTRACT_STATE, UCDC_DATA_MULTIPLEXED);
278 			else
279 				err = 0;
280 			if (err) {
281 				printf("%s: could not set data multiplex mode\n",
282 				       USBDEVNAME(sc->sc_dev));
283 				goto bad;
284 			}
285 			sc->sc_cm_over_data = 1;
286 		}
287 	}
288 
289 	/*
290 	 * The standard allows for notification messages (to indicate things
291 	 * like a modem hangup) to come in via an interrupt endpoint
292 	 * off of the control interface.  Iterate over the endpoints on
293 	 * the control interface and see if there are any interrupt
294 	 * endpoints; if there are, then register it.
295 	 */
296 
297 	sc->sc_ctl_notify = -1;
298 	sc->sc_notify_pipe = NULL;
299 
300 	for (i = 0; i < id->bNumEndpoints; i++) {
301 		ed = usbd_interface2endpoint_descriptor(sc->sc_ctl_iface, i);
302 		if (ed == NULL)
303 			continue;
304 
305 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
306 		    (ed->bmAttributes & UE_XFERTYPE) == UE_INTERRUPT) {
307 			printf("%s: status change notification available\n",
308 			       USBDEVNAME(sc->sc_dev));
309 			sc->sc_ctl_notify = ed->bEndpointAddress;
310 		}
311 	}
312 
313 	sc->sc_dtr = -1;
314 
315 	uca.portno = UCOM_UNK_PORTNO;
316 	/* bulkin, bulkout set above */
317 	uca.ibufsize = UMODEMIBUFSIZE;
318 	uca.obufsize = UMODEMOBUFSIZE;
319 	uca.ibufsizepad = UMODEMIBUFSIZE;
320 	uca.opkthdrlen = 0;
321 	uca.device = sc->sc_udev;
322 	uca.iface = sc->sc_data_iface;
323 	uca.methods = &umodem_methods;
324 	uca.arg = sc;
325 	uca.info = NULL;
326 
327 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
328 			   USBDEV(sc->sc_dev));
329 
330 	DPRINTF(("umodem_attach: sc=%p\n", sc));
331 	sc->sc_subdev = config_found_sm(self, &uca, ucomprint, ucomsubmatch);
332 
333 	USB_ATTACH_SUCCESS_RETURN;
334 
335  bad:
336 	sc->sc_dying = 1;
337 	USB_ATTACH_ERROR_RETURN;
338 }
339 
340 Static int
341 umodem_open(void *addr, int portno)
342 {
343 	struct umodem_softc *sc = addr;
344 	int err;
345 
346 	DPRINTF(("umodem_open: sc=%p\n", sc));
347 
348 	if (sc->sc_ctl_notify != -1 && sc->sc_notify_pipe == NULL) {
349 		err = usbd_open_pipe_intr(sc->sc_ctl_iface, sc->sc_ctl_notify,
350 		    USBD_SHORT_XFER_OK, &sc->sc_notify_pipe, sc,
351 		    &sc->sc_notify_buf, sizeof(sc->sc_notify_buf),
352 		    umodem_intr, USBD_DEFAULT_INTERVAL);
353 
354 		if (err) {
355 			DPRINTF(("Failed to establish notify pipe: %s\n",
356 				usbd_errstr(err)));
357 			return EIO;
358 		}
359 	}
360 
361 	return 0;
362 }
363 
364 Static void
365 umodem_close(void *addr, int portno)
366 {
367 	struct umodem_softc *sc = addr;
368 	int err;
369 
370 	DPRINTF(("umodem_close: sc=%p\n", sc));
371 
372 	if (sc->sc_notify_pipe != NULL) {
373 		err = usbd_abort_pipe(sc->sc_notify_pipe);
374 		if (err)
375 			printf("%s: abort notify pipe failed: %s\n",
376 			    USBDEVNAME(sc->sc_dev), usbd_errstr(err));
377 		err = usbd_close_pipe(sc->sc_notify_pipe);
378 		if (err)
379 			printf("%s: close notify pipe failed: %s\n",
380 			    USBDEVNAME(sc->sc_dev), usbd_errstr(err));
381 		sc->sc_notify_pipe = NULL;
382 	}
383 }
384 
385 Static void
386 umodem_intr(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
387 {
388 	struct umodem_softc *sc = priv;
389 	u_char mstatus;
390 
391 	if (sc->sc_dying)
392 		return;
393 
394 	if (status != USBD_NORMAL_COMPLETION) {
395 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
396 			return;
397 		printf("%s: abnormal status: %s\n", USBDEVNAME(sc->sc_dev),
398 		       usbd_errstr(status));
399 		return;
400 	}
401 
402 	if (sc->sc_notify_buf.bmRequestType != UCDC_NOTIFICATION) {
403 		DPRINTF(("%s: unknown message type (%02x) on notify pipe\n",
404 			 USBDEVNAME(sc->sc_dev),
405 			 sc->sc_notify_buf.bmRequestType));
406 		return;
407 	}
408 
409 	switch (sc->sc_notify_buf.bNotification) {
410 	case UCDC_N_SERIAL_STATE:
411 		/*
412 		 * Set the serial state in ucom driver based on
413 		 * the bits from the notify message
414 		 */
415 		if (UGETW(sc->sc_notify_buf.wLength) != 2) {
416 			printf("%s: Invalid notification length! (%d)\n",
417 			       USBDEVNAME(sc->sc_dev),
418 			       UGETW(sc->sc_notify_buf.wLength));
419 			break;
420 		}
421 		DPRINTF(("%s: notify bytes = %02x%02x\n",
422 			 USBDEVNAME(sc->sc_dev),
423 			 sc->sc_notify_buf.data[0],
424 			 sc->sc_notify_buf.data[1]));
425 		/* Currently, lsr is always zero. */
426 		sc->sc_lsr = sc->sc_msr = 0;
427 		mstatus = sc->sc_notify_buf.data[0];
428 
429 		if (ISSET(mstatus, UCDC_N_SERIAL_RI))
430 			sc->sc_msr |= UMSR_RI;
431 		if (ISSET(mstatus, UCDC_N_SERIAL_DSR))
432 			sc->sc_msr |= UMSR_DSR;
433 		if (ISSET(mstatus, UCDC_N_SERIAL_DCD))
434 			sc->sc_msr |= UMSR_DCD;
435 		ucom_status_change((struct ucom_softc *)sc->sc_subdev);
436 		break;
437 	default:
438 		DPRINTF(("%s: unknown notify message: %02x\n",
439 			 USBDEVNAME(sc->sc_dev),
440 			 sc->sc_notify_buf.bNotification));
441 		break;
442 	}
443 }
444 
445 void
446 umodem_get_caps(usbd_device_handle dev, int *cm, int *acm)
447 {
448 	usb_cdc_cm_descriptor_t *cmd;
449 	usb_cdc_acm_descriptor_t *cad;
450 
451 	*cm = *acm = 0;
452 
453 	cmd = umodem_get_desc(dev, UDESC_CS_INTERFACE, UDESCSUB_CDC_CM);
454 	if (cmd == NULL) {
455 		DPRINTF(("umodem_get_desc: no CM desc\n"));
456 		return;
457 	}
458 	*cm = cmd->bmCapabilities;
459 
460 	cad = umodem_get_desc(dev, UDESC_CS_INTERFACE, UDESCSUB_CDC_ACM);
461 	if (cad == NULL) {
462 		DPRINTF(("umodem_get_desc: no ACM desc\n"));
463 		return;
464 	}
465 	*acm = cad->bmCapabilities;
466 }
467 
468 void
469 umodem_get_status(void *addr, int portno, u_char *lsr, u_char *msr)
470 {
471 	struct umodem_softc *sc = addr;
472 
473 	DPRINTF(("umodem_get_status:\n"));
474 
475 	if (lsr != NULL)
476 		*lsr = sc->sc_lsr;
477 	if (msr != NULL)
478 		*msr = sc->sc_msr;
479 }
480 
481 int
482 umodem_param(void *addr, int portno, struct termios *t)
483 {
484 	struct umodem_softc *sc = addr;
485 	usbd_status err;
486 	usb_cdc_line_state_t ls;
487 
488 	DPRINTF(("umodem_param: sc=%p\n", sc));
489 
490 	USETDW(ls.dwDTERate, t->c_ospeed);
491 	if (ISSET(t->c_cflag, CSTOPB))
492 		ls.bCharFormat = UCDC_STOP_BIT_2;
493 	else
494 		ls.bCharFormat = UCDC_STOP_BIT_1;
495 	if (ISSET(t->c_cflag, PARENB)) {
496 		if (ISSET(t->c_cflag, PARODD))
497 			ls.bParityType = UCDC_PARITY_ODD;
498 		else
499 			ls.bParityType = UCDC_PARITY_EVEN;
500 	} else
501 		ls.bParityType = UCDC_PARITY_NONE;
502 	switch (ISSET(t->c_cflag, CSIZE)) {
503 	case CS5:
504 		ls.bDataBits = 5;
505 		break;
506 	case CS6:
507 		ls.bDataBits = 6;
508 		break;
509 	case CS7:
510 		ls.bDataBits = 7;
511 		break;
512 	case CS8:
513 		ls.bDataBits = 8;
514 		break;
515 	}
516 
517 	err = umodem_set_line_coding(sc, &ls);
518 	if (err) {
519 		DPRINTF(("umodem_param: err=%s\n", usbd_errstr(err)));
520 		return (1);
521 	}
522 	return (0);
523 }
524 
525 int
526 umodem_ioctl(void *addr, int portno, u_long cmd, caddr_t data, int flag,
527 	     usb_proc_ptr p)
528 {
529 	struct umodem_softc *sc = addr;
530 	int error = 0;
531 
532 	if (sc->sc_dying)
533 		return (EIO);
534 
535 	DPRINTF(("umodemioctl: cmd=0x%08lx\n", cmd));
536 
537 	switch (cmd) {
538 	case USB_GET_CM_OVER_DATA:
539 		*(int *)data = sc->sc_cm_over_data;
540 		break;
541 
542 	case USB_SET_CM_OVER_DATA:
543 		if (*(int *)data != sc->sc_cm_over_data) {
544 			/* XXX change it */
545 		}
546 		break;
547 
548 	default:
549 		DPRINTF(("umodemioctl: unknown\n"));
550 		error = ENOTTY;
551 		break;
552 	}
553 
554 	return (error);
555 }
556 
557 void
558 umodem_dtr(struct umodem_softc *sc, int onoff)
559 {
560 	DPRINTF(("umodem_modem: 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_modem: 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, usb_cdc_line_state_t *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 void *
672 umodem_get_desc(usbd_device_handle dev, int type, int subtype)
673 {
674 	usb_descriptor_t *desc;
675 	usb_config_descriptor_t *cd = usbd_get_config_descriptor(dev);
676         uByte *p = (uByte *)cd;
677         uByte *end = p + UGETW(cd->wTotalLength);
678 
679 	while (p < end) {
680 		desc = (usb_descriptor_t *)p;
681 		if (desc->bDescriptorType == type &&
682 		    desc->bDescriptorSubtype == subtype)
683 			return (desc);
684 		p += desc->bLength;
685 	}
686 
687 	return (0);
688 }
689 
690 usbd_status
691 umodem_set_comm_feature(struct umodem_softc *sc, int feature, int state)
692 {
693 	usb_device_request_t req;
694 	usbd_status err;
695 	usb_cdc_abstract_state_t ast;
696 
697 	DPRINTF(("umodem_set_comm_feature: feature=%d state=%d\n", feature,
698 		 state));
699 
700 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
701 	req.bRequest = UCDC_SET_COMM_FEATURE;
702 	USETW(req.wValue, feature);
703 	USETW(req.wIndex, sc->sc_ctl_iface_no);
704 	USETW(req.wLength, UCDC_ABSTRACT_STATE_LENGTH);
705 	USETW(ast.wState, state);
706 
707 	err = usbd_do_request(sc->sc_udev, &req, &ast);
708 	if (err) {
709 		DPRINTF(("umodem_set_comm_feature: feature=%d, err=%s\n",
710 			 feature, usbd_errstr(err)));
711 		return (err);
712 	}
713 
714 	return (USBD_NORMAL_COMPLETION);
715 }
716 
717 int
718 umodem_activate(device_ptr_t self, enum devact act)
719 {
720 	struct umodem_softc *sc = (struct umodem_softc *)self;
721 	int rv = 0;
722 
723 	switch (act) {
724 	case DVACT_ACTIVATE:
725 		return (EOPNOTSUPP);
726 
727 	case DVACT_DEACTIVATE:
728 		sc->sc_dying = 1;
729 		if (sc->sc_subdev)
730 			rv = config_deactivate(sc->sc_subdev);
731 		break;
732 	}
733 	return (rv);
734 }
735 
736 USB_DETACH(umodem)
737 {
738 	USB_DETACH_START(umodem, sc);
739 	int rv = 0;
740 
741 	DPRINTF(("umodem_detach: sc=%p flags=%d\n", sc, flags));
742 
743 	sc->sc_dying = 1;
744 
745 	if (sc->sc_subdev != NULL)
746 		rv = config_detach(sc->sc_subdev, flags);
747 
748 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
749 			   USBDEV(sc->sc_dev));
750 
751 	return (rv);
752 }
753