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