xref: /netbsd-src/sys/dev/usb/umodem.c (revision 23c8222edbfb0f0932d88a8351d3a0cf817dfb9e)
1 /*	$NetBSD: umodem.c,v 1.49 2004/10/23 13:28:26 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/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.49 2004/10/23 13:28:26 augustss 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 usbd_status umodem_set_comm_feature(struct umodem_softc *sc,
128 					   int feature, int state);
129 Static usbd_status umodem_set_line_coding(struct umodem_softc *sc,
130 					  usb_cdc_line_state_t *state);
131 
132 Static void	umodem_get_caps(usbd_device_handle, int *, int *);
133 
134 Static void	umodem_get_status(void *, int portno, u_char *lsr, u_char *msr);
135 Static void	umodem_set(void *, int, int, int);
136 Static void	umodem_dtr(struct umodem_softc *, int);
137 Static void	umodem_rts(struct umodem_softc *, int);
138 Static void	umodem_break(struct umodem_softc *, int);
139 Static void	umodem_set_line_state(struct umodem_softc *);
140 Static int	umodem_param(void *, int, struct termios *);
141 Static int	umodem_ioctl(void *, int, u_long, caddr_t, int, usb_proc_ptr );
142 Static int	umodem_open(void *, int portno);
143 Static void	umodem_close(void *, int portno);
144 Static void	umodem_intr(usbd_xfer_handle, usbd_private_handle, usbd_status);
145 
146 Static struct ucom_methods umodem_methods = {
147 	umodem_get_status,
148 	umodem_set,
149 	umodem_param,
150 	umodem_ioctl,
151 	umodem_open,
152 	umodem_close,
153 	NULL,
154 	NULL,
155 };
156 
157 USB_DECLARE_DRIVER(umodem);
158 
159 USB_MATCH(umodem)
160 {
161 	USB_MATCH_START(umodem, uaa);
162 	usb_interface_descriptor_t *id;
163 	int cm, acm;
164 
165 	if (uaa->iface == NULL)
166 		return (UMATCH_NONE);
167 
168 	id = usbd_get_interface_descriptor(uaa->iface);
169 	if (id == NULL ||
170 	    id->bInterfaceClass != UICLASS_CDC ||
171 	    id->bInterfaceSubClass != UISUBCLASS_ABSTRACT_CONTROL_MODEL ||
172 	    id->bInterfaceProtocol != UIPROTO_CDC_AT)
173 		return (UMATCH_NONE);
174 
175 	umodem_get_caps(uaa->device, &cm, &acm);
176 	if (!(cm & USB_CDC_CM_DOES_CM) ||
177 	    !(cm & USB_CDC_CM_OVER_DATA) ||
178 	    !(acm & USB_CDC_ACM_HAS_LINE))
179 		return (UMATCH_NONE);
180 
181 	return (UMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO);
182 }
183 
184 USB_ATTACH(umodem)
185 {
186 	USB_ATTACH_START(umodem, sc, uaa);
187 	usbd_device_handle dev = uaa->device;
188 	usb_interface_descriptor_t *id;
189 	usb_endpoint_descriptor_t *ed;
190 	const usb_cdc_cm_descriptor_t *cmd;
191 	char devinfo[1024];
192 	usbd_status err;
193 	int data_ifcno;
194 	int i;
195 	struct ucom_attach_args uca;
196 
197 	usbd_devinfo(uaa->device, 0, devinfo, sizeof(devinfo));
198 	USB_ATTACH_SETUP;
199 
200 	sc->sc_udev = dev;
201 	sc->sc_ctl_iface = uaa->iface;
202 
203 	id = usbd_get_interface_descriptor(sc->sc_ctl_iface);
204 	printf("%s: %s, iclass %d/%d\n", USBDEVNAME(sc->sc_dev),
205 	       devinfo, id->bInterfaceClass, id->bInterfaceSubClass);
206 	sc->sc_ctl_iface_no = id->bInterfaceNumber;
207 
208 	umodem_get_caps(dev, &sc->sc_cm_cap, &sc->sc_acm_cap);
209 
210 	/* Get the data interface no. */
211 	cmd = (usb_cdc_cm_descriptor_t *)usb_find_desc(dev, UDESC_CS_INTERFACE,
212 						       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 	const usb_cdc_cm_descriptor_t *cmd;
452 	const usb_cdc_acm_descriptor_t *cad;
453 
454 	*cm = *acm = 0;
455 
456 	cmd = (usb_cdc_cm_descriptor_t *)usb_find_desc(dev, UDESC_CS_INTERFACE,
457 						       UDESCSUB_CDC_CM);
458 	if (cmd == NULL) {
459 		DPRINTF(("umodem_get_desc: no CM desc\n"));
460 		return;
461 	}
462 	*cm = cmd->bmCapabilities;
463 
464 	cad = (usb_cdc_acm_descriptor_t *)usb_find_desc(dev, UDESC_CS_INTERFACE,
465 							UDESCSUB_CDC_ACM);
466 	if (cad == NULL) {
467 		DPRINTF(("umodem_get_desc: no ACM desc\n"));
468 		return;
469 	}
470 	*acm = cad->bmCapabilities;
471 }
472 
473 void
474 umodem_get_status(void *addr, int portno, u_char *lsr, u_char *msr)
475 {
476 	struct umodem_softc *sc = addr;
477 
478 	DPRINTF(("umodem_get_status:\n"));
479 
480 	if (lsr != NULL)
481 		*lsr = sc->sc_lsr;
482 	if (msr != NULL)
483 		*msr = sc->sc_msr;
484 }
485 
486 int
487 umodem_param(void *addr, int portno, struct termios *t)
488 {
489 	struct umodem_softc *sc = addr;
490 	usbd_status err;
491 	usb_cdc_line_state_t ls;
492 
493 	DPRINTF(("umodem_param: sc=%p\n", sc));
494 
495 	USETDW(ls.dwDTERate, t->c_ospeed);
496 	if (ISSET(t->c_cflag, CSTOPB))
497 		ls.bCharFormat = UCDC_STOP_BIT_2;
498 	else
499 		ls.bCharFormat = UCDC_STOP_BIT_1;
500 	if (ISSET(t->c_cflag, PARENB)) {
501 		if (ISSET(t->c_cflag, PARODD))
502 			ls.bParityType = UCDC_PARITY_ODD;
503 		else
504 			ls.bParityType = UCDC_PARITY_EVEN;
505 	} else
506 		ls.bParityType = UCDC_PARITY_NONE;
507 	switch (ISSET(t->c_cflag, CSIZE)) {
508 	case CS5:
509 		ls.bDataBits = 5;
510 		break;
511 	case CS6:
512 		ls.bDataBits = 6;
513 		break;
514 	case CS7:
515 		ls.bDataBits = 7;
516 		break;
517 	case CS8:
518 		ls.bDataBits = 8;
519 		break;
520 	}
521 
522 	err = umodem_set_line_coding(sc, &ls);
523 	if (err) {
524 		DPRINTF(("umodem_param: err=%s\n", usbd_errstr(err)));
525 		return (EPASSTHROUGH);
526 	}
527 	return (0);
528 }
529 
530 int
531 umodem_ioctl(void *addr, int portno, u_long cmd, caddr_t data, int flag,
532 	     usb_proc_ptr p)
533 {
534 	struct umodem_softc *sc = addr;
535 	int error = 0;
536 
537 	if (sc->sc_dying)
538 		return (EIO);
539 
540 	DPRINTF(("umodemioctl: cmd=0x%08lx\n", cmd));
541 
542 	switch (cmd) {
543 	case USB_GET_CM_OVER_DATA:
544 		*(int *)data = sc->sc_cm_over_data;
545 		break;
546 
547 	case USB_SET_CM_OVER_DATA:
548 		if (*(int *)data != sc->sc_cm_over_data) {
549 			/* XXX change it */
550 		}
551 		break;
552 
553 	default:
554 		DPRINTF(("umodemioctl: unknown\n"));
555 		error = EPASSTHROUGH;
556 		break;
557 	}
558 
559 	return (error);
560 }
561 
562 void
563 umodem_dtr(struct umodem_softc *sc, int onoff)
564 {
565 	DPRINTF(("umodem_modem: onoff=%d\n", onoff));
566 
567 	if (sc->sc_dtr == onoff)
568 		return;
569 	sc->sc_dtr = onoff;
570 
571 	umodem_set_line_state(sc);
572 }
573 
574 void
575 umodem_rts(struct umodem_softc *sc, int onoff)
576 {
577 	DPRINTF(("umodem_modem: onoff=%d\n", onoff));
578 
579 	if (sc->sc_rts == onoff)
580 		return;
581 	sc->sc_rts = onoff;
582 
583 	umodem_set_line_state(sc);
584 }
585 
586 void
587 umodem_set_line_state(struct umodem_softc *sc)
588 {
589 	usb_device_request_t req;
590 	int ls;
591 
592 	ls = (sc->sc_dtr ? UCDC_LINE_DTR : 0) |
593 	     (sc->sc_rts ? UCDC_LINE_RTS : 0);
594 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
595 	req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
596 	USETW(req.wValue, ls);
597 	USETW(req.wIndex, sc->sc_ctl_iface_no);
598 	USETW(req.wLength, 0);
599 
600 	(void)usbd_do_request(sc->sc_udev, &req, 0);
601 
602 }
603 
604 void
605 umodem_break(struct umodem_softc *sc, int onoff)
606 {
607 	usb_device_request_t req;
608 
609 	DPRINTF(("umodem_break: onoff=%d\n", onoff));
610 
611 	if (!(sc->sc_acm_cap & USB_CDC_ACM_HAS_BREAK))
612 		return;
613 
614 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
615 	req.bRequest = UCDC_SEND_BREAK;
616 	USETW(req.wValue, onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF);
617 	USETW(req.wIndex, sc->sc_ctl_iface_no);
618 	USETW(req.wLength, 0);
619 
620 	(void)usbd_do_request(sc->sc_udev, &req, 0);
621 }
622 
623 void
624 umodem_set(void *addr, int portno, int reg, int onoff)
625 {
626 	struct umodem_softc *sc = addr;
627 
628 	switch (reg) {
629 	case UCOM_SET_DTR:
630 		umodem_dtr(sc, onoff);
631 		break;
632 	case UCOM_SET_RTS:
633 		umodem_rts(sc, onoff);
634 		break;
635 	case UCOM_SET_BREAK:
636 		umodem_break(sc, onoff);
637 		break;
638 	default:
639 		break;
640 	}
641 }
642 
643 usbd_status
644 umodem_set_line_coding(struct umodem_softc *sc, usb_cdc_line_state_t *state)
645 {
646 	usb_device_request_t req;
647 	usbd_status err;
648 
649 	DPRINTF(("umodem_set_line_coding: rate=%d fmt=%d parity=%d bits=%d\n",
650 		 UGETDW(state->dwDTERate), state->bCharFormat,
651 		 state->bParityType, state->bDataBits));
652 
653 	if (memcmp(state, &sc->sc_line_state, UCDC_LINE_STATE_LENGTH) == 0) {
654 		DPRINTF(("umodem_set_line_coding: already set\n"));
655 		return (USBD_NORMAL_COMPLETION);
656 	}
657 
658 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
659 	req.bRequest = UCDC_SET_LINE_CODING;
660 	USETW(req.wValue, 0);
661 	USETW(req.wIndex, sc->sc_ctl_iface_no);
662 	USETW(req.wLength, UCDC_LINE_STATE_LENGTH);
663 
664 	err = usbd_do_request(sc->sc_udev, &req, state);
665 	if (err) {
666 		DPRINTF(("umodem_set_line_coding: failed, err=%s\n",
667 			 usbd_errstr(err)));
668 		return (err);
669 	}
670 
671 	sc->sc_line_state = *state;
672 
673 	return (USBD_NORMAL_COMPLETION);
674 }
675 
676 usbd_status
677 umodem_set_comm_feature(struct umodem_softc *sc, int feature, int state)
678 {
679 	usb_device_request_t req;
680 	usbd_status err;
681 	usb_cdc_abstract_state_t ast;
682 
683 	DPRINTF(("umodem_set_comm_feature: feature=%d state=%d\n", feature,
684 		 state));
685 
686 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
687 	req.bRequest = UCDC_SET_COMM_FEATURE;
688 	USETW(req.wValue, feature);
689 	USETW(req.wIndex, sc->sc_ctl_iface_no);
690 	USETW(req.wLength, UCDC_ABSTRACT_STATE_LENGTH);
691 	USETW(ast.wState, state);
692 
693 	err = usbd_do_request(sc->sc_udev, &req, &ast);
694 	if (err) {
695 		DPRINTF(("umodem_set_comm_feature: feature=%d, err=%s\n",
696 			 feature, usbd_errstr(err)));
697 		return (err);
698 	}
699 
700 	return (USBD_NORMAL_COMPLETION);
701 }
702 
703 int
704 umodem_activate(device_ptr_t self, enum devact act)
705 {
706 	struct umodem_softc *sc = (struct umodem_softc *)self;
707 	int rv = 0;
708 
709 	switch (act) {
710 	case DVACT_ACTIVATE:
711 		return (EOPNOTSUPP);
712 
713 	case DVACT_DEACTIVATE:
714 		sc->sc_dying = 1;
715 		if (sc->sc_subdev)
716 			rv = config_deactivate(sc->sc_subdev);
717 		break;
718 	}
719 	return (rv);
720 }
721 
722 USB_DETACH(umodem)
723 {
724 	USB_DETACH_START(umodem, sc);
725 	int rv = 0;
726 
727 	DPRINTF(("umodem_detach: sc=%p flags=%d\n", sc, flags));
728 
729 	sc->sc_dying = 1;
730 
731 	if (sc->sc_subdev != NULL)
732 		rv = config_detach(sc->sc_subdev, flags);
733 
734 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
735 			   USBDEV(sc->sc_dev));
736 
737 	return (rv);
738 }
739