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