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