xref: /netbsd-src/sys/dev/usb/umodem.c (revision de1dfb1250df962f1ff3a011772cf58e605aed11)
1 /*	$NetBSD: umodem.c,v 1.48 2004/09/13 12:55:49 drochner 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/devclass_docs/usbccs10.pdf
42  *                   http://www.usb.org/developers/devclass_docs/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.48 2004/09/13 12:55:49 drochner 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, sizeof(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_loc(self, "ucombus", NULL, &uca,
334 					    ucomprint, ucomsubmatch);
335 
336 	USB_ATTACH_SUCCESS_RETURN;
337 
338  bad:
339 	sc->sc_dying = 1;
340 	USB_ATTACH_ERROR_RETURN;
341 }
342 
343 Static int
344 umodem_open(void *addr, int portno)
345 {
346 	struct umodem_softc *sc = addr;
347 	int err;
348 
349 	DPRINTF(("umodem_open: sc=%p\n", sc));
350 
351 	if (sc->sc_ctl_notify != -1 && sc->sc_notify_pipe == NULL) {
352 		err = usbd_open_pipe_intr(sc->sc_ctl_iface, sc->sc_ctl_notify,
353 		    USBD_SHORT_XFER_OK, &sc->sc_notify_pipe, sc,
354 		    &sc->sc_notify_buf, sizeof(sc->sc_notify_buf),
355 		    umodem_intr, USBD_DEFAULT_INTERVAL);
356 
357 		if (err) {
358 			DPRINTF(("Failed to establish notify pipe: %s\n",
359 				usbd_errstr(err)));
360 			return EIO;
361 		}
362 	}
363 
364 	return 0;
365 }
366 
367 Static void
368 umodem_close(void *addr, int portno)
369 {
370 	struct umodem_softc *sc = addr;
371 	int err;
372 
373 	DPRINTF(("umodem_close: sc=%p\n", sc));
374 
375 	if (sc->sc_notify_pipe != NULL) {
376 		err = usbd_abort_pipe(sc->sc_notify_pipe);
377 		if (err)
378 			printf("%s: abort notify pipe failed: %s\n",
379 			    USBDEVNAME(sc->sc_dev), usbd_errstr(err));
380 		err = usbd_close_pipe(sc->sc_notify_pipe);
381 		if (err)
382 			printf("%s: close notify pipe failed: %s\n",
383 			    USBDEVNAME(sc->sc_dev), usbd_errstr(err));
384 		sc->sc_notify_pipe = NULL;
385 	}
386 }
387 
388 Static void
389 umodem_intr(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
390 {
391 	struct umodem_softc *sc = priv;
392 	u_char mstatus;
393 
394 	if (sc->sc_dying)
395 		return;
396 
397 	if (status != USBD_NORMAL_COMPLETION) {
398 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
399 			return;
400 		printf("%s: abnormal status: %s\n", USBDEVNAME(sc->sc_dev),
401 		       usbd_errstr(status));
402 		return;
403 	}
404 
405 	if (sc->sc_notify_buf.bmRequestType != UCDC_NOTIFICATION) {
406 		DPRINTF(("%s: unknown message type (%02x) on notify pipe\n",
407 			 USBDEVNAME(sc->sc_dev),
408 			 sc->sc_notify_buf.bmRequestType));
409 		return;
410 	}
411 
412 	switch (sc->sc_notify_buf.bNotification) {
413 	case UCDC_N_SERIAL_STATE:
414 		/*
415 		 * Set the serial state in ucom driver based on
416 		 * the bits from the notify message
417 		 */
418 		if (UGETW(sc->sc_notify_buf.wLength) != 2) {
419 			printf("%s: Invalid notification length! (%d)\n",
420 			       USBDEVNAME(sc->sc_dev),
421 			       UGETW(sc->sc_notify_buf.wLength));
422 			break;
423 		}
424 		DPRINTF(("%s: notify bytes = %02x%02x\n",
425 			 USBDEVNAME(sc->sc_dev),
426 			 sc->sc_notify_buf.data[0],
427 			 sc->sc_notify_buf.data[1]));
428 		/* Currently, lsr is always zero. */
429 		sc->sc_lsr = sc->sc_msr = 0;
430 		mstatus = sc->sc_notify_buf.data[0];
431 
432 		if (ISSET(mstatus, UCDC_N_SERIAL_RI))
433 			sc->sc_msr |= UMSR_RI;
434 		if (ISSET(mstatus, UCDC_N_SERIAL_DSR))
435 			sc->sc_msr |= UMSR_DSR;
436 		if (ISSET(mstatus, UCDC_N_SERIAL_DCD))
437 			sc->sc_msr |= UMSR_DCD;
438 		ucom_status_change((struct ucom_softc *)sc->sc_subdev);
439 		break;
440 	default:
441 		DPRINTF(("%s: unknown notify message: %02x\n",
442 			 USBDEVNAME(sc->sc_dev),
443 			 sc->sc_notify_buf.bNotification));
444 		break;
445 	}
446 }
447 
448 void
449 umodem_get_caps(usbd_device_handle dev, int *cm, int *acm)
450 {
451 	usb_cdc_cm_descriptor_t *cmd;
452 	usb_cdc_acm_descriptor_t *cad;
453 
454 	*cm = *acm = 0;
455 
456 	cmd = umodem_get_desc(dev, UDESC_CS_INTERFACE, UDESCSUB_CDC_CM);
457 	if (cmd == NULL) {
458 		DPRINTF(("umodem_get_desc: no CM desc\n"));
459 		return;
460 	}
461 	*cm = cmd->bmCapabilities;
462 
463 	cad = umodem_get_desc(dev, UDESC_CS_INTERFACE, UDESCSUB_CDC_ACM);
464 	if (cad == NULL) {
465 		DPRINTF(("umodem_get_desc: no ACM desc\n"));
466 		return;
467 	}
468 	*acm = cad->bmCapabilities;
469 }
470 
471 void
472 umodem_get_status(void *addr, int portno, u_char *lsr, u_char *msr)
473 {
474 	struct umodem_softc *sc = addr;
475 
476 	DPRINTF(("umodem_get_status:\n"));
477 
478 	if (lsr != NULL)
479 		*lsr = sc->sc_lsr;
480 	if (msr != NULL)
481 		*msr = sc->sc_msr;
482 }
483 
484 int
485 umodem_param(void *addr, int portno, struct termios *t)
486 {
487 	struct umodem_softc *sc = addr;
488 	usbd_status err;
489 	usb_cdc_line_state_t ls;
490 
491 	DPRINTF(("umodem_param: sc=%p\n", sc));
492 
493 	USETDW(ls.dwDTERate, t->c_ospeed);
494 	if (ISSET(t->c_cflag, CSTOPB))
495 		ls.bCharFormat = UCDC_STOP_BIT_2;
496 	else
497 		ls.bCharFormat = UCDC_STOP_BIT_1;
498 	if (ISSET(t->c_cflag, PARENB)) {
499 		if (ISSET(t->c_cflag, PARODD))
500 			ls.bParityType = UCDC_PARITY_ODD;
501 		else
502 			ls.bParityType = UCDC_PARITY_EVEN;
503 	} else
504 		ls.bParityType = UCDC_PARITY_NONE;
505 	switch (ISSET(t->c_cflag, CSIZE)) {
506 	case CS5:
507 		ls.bDataBits = 5;
508 		break;
509 	case CS6:
510 		ls.bDataBits = 6;
511 		break;
512 	case CS7:
513 		ls.bDataBits = 7;
514 		break;
515 	case CS8:
516 		ls.bDataBits = 8;
517 		break;
518 	}
519 
520 	err = umodem_set_line_coding(sc, &ls);
521 	if (err) {
522 		DPRINTF(("umodem_param: err=%s\n", usbd_errstr(err)));
523 		return (EPASSTHROUGH);
524 	}
525 	return (0);
526 }
527 
528 int
529 umodem_ioctl(void *addr, int portno, u_long cmd, caddr_t data, int flag,
530 	     usb_proc_ptr p)
531 {
532 	struct umodem_softc *sc = addr;
533 	int error = 0;
534 
535 	if (sc->sc_dying)
536 		return (EIO);
537 
538 	DPRINTF(("umodemioctl: cmd=0x%08lx\n", cmd));
539 
540 	switch (cmd) {
541 	case USB_GET_CM_OVER_DATA:
542 		*(int *)data = sc->sc_cm_over_data;
543 		break;
544 
545 	case USB_SET_CM_OVER_DATA:
546 		if (*(int *)data != sc->sc_cm_over_data) {
547 			/* XXX change it */
548 		}
549 		break;
550 
551 	default:
552 		DPRINTF(("umodemioctl: unknown\n"));
553 		error = EPASSTHROUGH;
554 		break;
555 	}
556 
557 	return (error);
558 }
559 
560 void
561 umodem_dtr(struct umodem_softc *sc, int onoff)
562 {
563 	DPRINTF(("umodem_modem: onoff=%d\n", onoff));
564 
565 	if (sc->sc_dtr == onoff)
566 		return;
567 	sc->sc_dtr = onoff;
568 
569 	umodem_set_line_state(sc);
570 }
571 
572 void
573 umodem_rts(struct umodem_softc *sc, int onoff)
574 {
575 	DPRINTF(("umodem_modem: onoff=%d\n", onoff));
576 
577 	if (sc->sc_rts == onoff)
578 		return;
579 	sc->sc_rts = onoff;
580 
581 	umodem_set_line_state(sc);
582 }
583 
584 void
585 umodem_set_line_state(struct umodem_softc *sc)
586 {
587 	usb_device_request_t req;
588 	int ls;
589 
590 	ls = (sc->sc_dtr ? UCDC_LINE_DTR : 0) |
591 	     (sc->sc_rts ? UCDC_LINE_RTS : 0);
592 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
593 	req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
594 	USETW(req.wValue, ls);
595 	USETW(req.wIndex, sc->sc_ctl_iface_no);
596 	USETW(req.wLength, 0);
597 
598 	(void)usbd_do_request(sc->sc_udev, &req, 0);
599 
600 }
601 
602 void
603 umodem_break(struct umodem_softc *sc, int onoff)
604 {
605 	usb_device_request_t req;
606 
607 	DPRINTF(("umodem_break: onoff=%d\n", onoff));
608 
609 	if (!(sc->sc_acm_cap & USB_CDC_ACM_HAS_BREAK))
610 		return;
611 
612 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
613 	req.bRequest = UCDC_SEND_BREAK;
614 	USETW(req.wValue, onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF);
615 	USETW(req.wIndex, sc->sc_ctl_iface_no);
616 	USETW(req.wLength, 0);
617 
618 	(void)usbd_do_request(sc->sc_udev, &req, 0);
619 }
620 
621 void
622 umodem_set(void *addr, int portno, int reg, int onoff)
623 {
624 	struct umodem_softc *sc = addr;
625 
626 	switch (reg) {
627 	case UCOM_SET_DTR:
628 		umodem_dtr(sc, onoff);
629 		break;
630 	case UCOM_SET_RTS:
631 		umodem_rts(sc, onoff);
632 		break;
633 	case UCOM_SET_BREAK:
634 		umodem_break(sc, onoff);
635 		break;
636 	default:
637 		break;
638 	}
639 }
640 
641 usbd_status
642 umodem_set_line_coding(struct umodem_softc *sc, usb_cdc_line_state_t *state)
643 {
644 	usb_device_request_t req;
645 	usbd_status err;
646 
647 	DPRINTF(("umodem_set_line_coding: rate=%d fmt=%d parity=%d bits=%d\n",
648 		 UGETDW(state->dwDTERate), state->bCharFormat,
649 		 state->bParityType, state->bDataBits));
650 
651 	if (memcmp(state, &sc->sc_line_state, UCDC_LINE_STATE_LENGTH) == 0) {
652 		DPRINTF(("umodem_set_line_coding: already set\n"));
653 		return (USBD_NORMAL_COMPLETION);
654 	}
655 
656 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
657 	req.bRequest = UCDC_SET_LINE_CODING;
658 	USETW(req.wValue, 0);
659 	USETW(req.wIndex, sc->sc_ctl_iface_no);
660 	USETW(req.wLength, UCDC_LINE_STATE_LENGTH);
661 
662 	err = usbd_do_request(sc->sc_udev, &req, state);
663 	if (err) {
664 		DPRINTF(("umodem_set_line_coding: failed, err=%s\n",
665 			 usbd_errstr(err)));
666 		return (err);
667 	}
668 
669 	sc->sc_line_state = *state;
670 
671 	return (USBD_NORMAL_COMPLETION);
672 }
673 
674 void *
675 umodem_get_desc(usbd_device_handle dev, int type, int subtype)
676 {
677 	usb_descriptor_t *desc;
678 	usb_config_descriptor_t *cd = usbd_get_config_descriptor(dev);
679         uByte *p = (uByte *)cd;
680         uByte *end = p + UGETW(cd->wTotalLength);
681 
682 	while (p < end) {
683 		desc = (usb_descriptor_t *)p;
684 		if (desc->bDescriptorType == type &&
685 		    desc->bDescriptorSubtype == subtype)
686 			return (desc);
687 		p += desc->bLength;
688 	}
689 
690 	return (0);
691 }
692 
693 usbd_status
694 umodem_set_comm_feature(struct umodem_softc *sc, int feature, int state)
695 {
696 	usb_device_request_t req;
697 	usbd_status err;
698 	usb_cdc_abstract_state_t ast;
699 
700 	DPRINTF(("umodem_set_comm_feature: feature=%d state=%d\n", feature,
701 		 state));
702 
703 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
704 	req.bRequest = UCDC_SET_COMM_FEATURE;
705 	USETW(req.wValue, feature);
706 	USETW(req.wIndex, sc->sc_ctl_iface_no);
707 	USETW(req.wLength, UCDC_ABSTRACT_STATE_LENGTH);
708 	USETW(ast.wState, state);
709 
710 	err = usbd_do_request(sc->sc_udev, &req, &ast);
711 	if (err) {
712 		DPRINTF(("umodem_set_comm_feature: feature=%d, err=%s\n",
713 			 feature, usbd_errstr(err)));
714 		return (err);
715 	}
716 
717 	return (USBD_NORMAL_COMPLETION);
718 }
719 
720 int
721 umodem_activate(device_ptr_t self, enum devact act)
722 {
723 	struct umodem_softc *sc = (struct umodem_softc *)self;
724 	int rv = 0;
725 
726 	switch (act) {
727 	case DVACT_ACTIVATE:
728 		return (EOPNOTSUPP);
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