xref: /netbsd-src/sys/dev/usb/umodem.c (revision 20e85ad185ab16980f1219a557c42e057edb42ea)
1 /*	$NetBSD: umodem.c,v 1.50 2005/04/12 13:12:10 itohy 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.50 2005/04/12 13:12:10 itohy 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 				usb_interface_descriptor_t *);
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, id);
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 	const 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, id);
210 
211 	/* Get the data interface no. */
212 	cmd = (usb_cdc_cm_descriptor_t *)usb_find_desc_if(dev,
213 							  UDESC_CS_INTERFACE,
214 							  UDESCSUB_CDC_CM, id);
215 	if (cmd == NULL) {
216 		printf("%s: no CM descriptor\n", USBDEVNAME(sc->sc_dev));
217 		goto bad;
218 	}
219 	sc->sc_data_iface_no = data_ifcno = cmd->bDataInterface;
220 
221 	printf("%s: data interface %d, has %sCM over data, has %sbreak\n",
222 	       USBDEVNAME(sc->sc_dev), data_ifcno,
223 	       sc->sc_cm_cap & USB_CDC_CM_OVER_DATA ? "" : "no ",
224 	       sc->sc_acm_cap & USB_CDC_ACM_HAS_BREAK ? "" : "no ");
225 
226 	/* Get the data interface too. */
227 	for (i = 0; i < uaa->nifaces; i++) {
228 		if (uaa->ifaces[i] != NULL) {
229 			id = usbd_get_interface_descriptor(uaa->ifaces[i]);
230 			if (id != NULL && id->bInterfaceNumber == data_ifcno) {
231 				sc->sc_data_iface = uaa->ifaces[i];
232 				uaa->ifaces[i] = NULL;
233 			}
234 		}
235 	}
236 	if (sc->sc_data_iface == NULL) {
237 		printf("%s: no data interface\n", USBDEVNAME(sc->sc_dev));
238 		goto bad;
239 	}
240 
241 	/*
242 	 * Find the bulk endpoints.
243 	 * Iterate over all endpoints in the data interface and take note.
244 	 */
245 	uca.bulkin = uca.bulkout = -1;
246 
247 	id = usbd_get_interface_descriptor(sc->sc_data_iface);
248 	for (i = 0; i < id->bNumEndpoints; i++) {
249 		ed = usbd_interface2endpoint_descriptor(sc->sc_data_iface, i);
250 		if (ed == NULL) {
251 			printf("%s: no endpoint descriptor for %d\n",
252 				USBDEVNAME(sc->sc_dev), i);
253 			goto bad;
254 		}
255 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
256 		    (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
257                         uca.bulkin = ed->bEndpointAddress;
258                 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
259 			   (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
260                         uca.bulkout = ed->bEndpointAddress;
261                 }
262         }
263 
264 	if (uca.bulkin == -1) {
265 		printf("%s: Could not find data bulk in\n",
266 		       USBDEVNAME(sc->sc_dev));
267 		goto bad;
268 	}
269 	if (uca.bulkout == -1) {
270 		printf("%s: Could not find data bulk out\n",
271 			USBDEVNAME(sc->sc_dev));
272 		goto bad;
273 	}
274 
275 	if (usbd_get_quirks(sc->sc_udev)->uq_flags & UQ_ASSUME_CM_OVER_DATA) {
276 		sc->sc_cm_over_data = 1;
277 	} else {
278 		if (sc->sc_cm_cap & USB_CDC_CM_OVER_DATA) {
279 			if (sc->sc_acm_cap & USB_CDC_ACM_HAS_FEATURE)
280 				err = umodem_set_comm_feature(sc,
281 				    UCDC_ABSTRACT_STATE, UCDC_DATA_MULTIPLEXED);
282 			else
283 				err = 0;
284 			if (err) {
285 				printf("%s: could not set data multiplex mode\n",
286 				       USBDEVNAME(sc->sc_dev));
287 				goto bad;
288 			}
289 			sc->sc_cm_over_data = 1;
290 		}
291 	}
292 
293 	/*
294 	 * The standard allows for notification messages (to indicate things
295 	 * like a modem hangup) to come in via an interrupt endpoint
296 	 * off of the control interface.  Iterate over the endpoints on
297 	 * the control interface and see if there are any interrupt
298 	 * endpoints; if there are, then register it.
299 	 */
300 
301 	sc->sc_ctl_notify = -1;
302 	sc->sc_notify_pipe = NULL;
303 
304 	for (i = 0; i < id->bNumEndpoints; i++) {
305 		ed = usbd_interface2endpoint_descriptor(sc->sc_ctl_iface, i);
306 		if (ed == NULL)
307 			continue;
308 
309 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
310 		    (ed->bmAttributes & UE_XFERTYPE) == UE_INTERRUPT) {
311 			printf("%s: status change notification available\n",
312 			       USBDEVNAME(sc->sc_dev));
313 			sc->sc_ctl_notify = ed->bEndpointAddress;
314 		}
315 	}
316 
317 	sc->sc_dtr = -1;
318 
319 	uca.portno = UCOM_UNK_PORTNO;
320 	/* bulkin, bulkout set above */
321 	uca.ibufsize = UMODEMIBUFSIZE;
322 	uca.obufsize = UMODEMOBUFSIZE;
323 	uca.ibufsizepad = UMODEMIBUFSIZE;
324 	uca.opkthdrlen = 0;
325 	uca.device = sc->sc_udev;
326 	uca.iface = sc->sc_data_iface;
327 	uca.methods = &umodem_methods;
328 	uca.arg = sc;
329 	uca.info = NULL;
330 
331 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
332 			   USBDEV(sc->sc_dev));
333 
334 	DPRINTF(("umodem_attach: sc=%p\n", sc));
335 	sc->sc_subdev = config_found_sm_loc(self, "ucombus", NULL, &uca,
336 					    ucomprint, ucomsubmatch);
337 
338 	USB_ATTACH_SUCCESS_RETURN;
339 
340  bad:
341 	sc->sc_dying = 1;
342 	USB_ATTACH_ERROR_RETURN;
343 }
344 
345 Static int
346 umodem_open(void *addr, int portno)
347 {
348 	struct umodem_softc *sc = addr;
349 	int err;
350 
351 	DPRINTF(("umodem_open: sc=%p\n", sc));
352 
353 	if (sc->sc_ctl_notify != -1 && sc->sc_notify_pipe == NULL) {
354 		err = usbd_open_pipe_intr(sc->sc_ctl_iface, sc->sc_ctl_notify,
355 		    USBD_SHORT_XFER_OK, &sc->sc_notify_pipe, sc,
356 		    &sc->sc_notify_buf, sizeof(sc->sc_notify_buf),
357 		    umodem_intr, USBD_DEFAULT_INTERVAL);
358 
359 		if (err) {
360 			DPRINTF(("Failed to establish notify pipe: %s\n",
361 				usbd_errstr(err)));
362 			return EIO;
363 		}
364 	}
365 
366 	return 0;
367 }
368 
369 Static void
370 umodem_close(void *addr, int portno)
371 {
372 	struct umodem_softc *sc = addr;
373 	int err;
374 
375 	DPRINTF(("umodem_close: sc=%p\n", sc));
376 
377 	if (sc->sc_notify_pipe != NULL) {
378 		err = usbd_abort_pipe(sc->sc_notify_pipe);
379 		if (err)
380 			printf("%s: abort notify pipe failed: %s\n",
381 			    USBDEVNAME(sc->sc_dev), usbd_errstr(err));
382 		err = usbd_close_pipe(sc->sc_notify_pipe);
383 		if (err)
384 			printf("%s: close notify pipe failed: %s\n",
385 			    USBDEVNAME(sc->sc_dev), usbd_errstr(err));
386 		sc->sc_notify_pipe = NULL;
387 	}
388 }
389 
390 Static void
391 umodem_intr(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
392 {
393 	struct umodem_softc *sc = priv;
394 	u_char mstatus;
395 
396 	if (sc->sc_dying)
397 		return;
398 
399 	if (status != USBD_NORMAL_COMPLETION) {
400 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
401 			return;
402 		printf("%s: abnormal status: %s\n", USBDEVNAME(sc->sc_dev),
403 		       usbd_errstr(status));
404 		return;
405 	}
406 
407 	if (sc->sc_notify_buf.bmRequestType != UCDC_NOTIFICATION) {
408 		DPRINTF(("%s: unknown message type (%02x) on notify pipe\n",
409 			 USBDEVNAME(sc->sc_dev),
410 			 sc->sc_notify_buf.bmRequestType));
411 		return;
412 	}
413 
414 	switch (sc->sc_notify_buf.bNotification) {
415 	case UCDC_N_SERIAL_STATE:
416 		/*
417 		 * Set the serial state in ucom driver based on
418 		 * the bits from the notify message
419 		 */
420 		if (UGETW(sc->sc_notify_buf.wLength) != 2) {
421 			printf("%s: Invalid notification length! (%d)\n",
422 			       USBDEVNAME(sc->sc_dev),
423 			       UGETW(sc->sc_notify_buf.wLength));
424 			break;
425 		}
426 		DPRINTF(("%s: notify bytes = %02x%02x\n",
427 			 USBDEVNAME(sc->sc_dev),
428 			 sc->sc_notify_buf.data[0],
429 			 sc->sc_notify_buf.data[1]));
430 		/* Currently, lsr is always zero. */
431 		sc->sc_lsr = sc->sc_msr = 0;
432 		mstatus = sc->sc_notify_buf.data[0];
433 
434 		if (ISSET(mstatus, UCDC_N_SERIAL_RI))
435 			sc->sc_msr |= UMSR_RI;
436 		if (ISSET(mstatus, UCDC_N_SERIAL_DSR))
437 			sc->sc_msr |= UMSR_DSR;
438 		if (ISSET(mstatus, UCDC_N_SERIAL_DCD))
439 			sc->sc_msr |= UMSR_DCD;
440 		ucom_status_change((struct ucom_softc *)sc->sc_subdev);
441 		break;
442 	default:
443 		DPRINTF(("%s: unknown notify message: %02x\n",
444 			 USBDEVNAME(sc->sc_dev),
445 			 sc->sc_notify_buf.bNotification));
446 		break;
447 	}
448 }
449 
450 void
451 umodem_get_caps(usbd_device_handle dev, int *cm, int *acm,
452 		usb_interface_descriptor_t *id)
453 {
454 	const usb_cdc_cm_descriptor_t *cmd;
455 	const usb_cdc_acm_descriptor_t *cad;
456 
457 	*cm = *acm = 0;
458 
459 	cmd = (usb_cdc_cm_descriptor_t *)usb_find_desc_if(dev,
460 							  UDESC_CS_INTERFACE,
461 							  UDESCSUB_CDC_CM, id);
462 	if (cmd == NULL) {
463 		DPRINTF(("umodem_get_desc: no CM desc\n"));
464 		return;
465 	}
466 	*cm = cmd->bmCapabilities;
467 
468 	cad = (usb_cdc_acm_descriptor_t *)usb_find_desc_if(dev,
469 							   UDESC_CS_INTERFACE,
470 							   UDESCSUB_CDC_ACM,
471 							   id);
472 	if (cad == NULL) {
473 		DPRINTF(("umodem_get_desc: no ACM desc\n"));
474 		return;
475 	}
476 	*acm = cad->bmCapabilities;
477 }
478 
479 void
480 umodem_get_status(void *addr, int portno, u_char *lsr, u_char *msr)
481 {
482 	struct umodem_softc *sc = addr;
483 
484 	DPRINTF(("umodem_get_status:\n"));
485 
486 	if (lsr != NULL)
487 		*lsr = sc->sc_lsr;
488 	if (msr != NULL)
489 		*msr = sc->sc_msr;
490 }
491 
492 int
493 umodem_param(void *addr, int portno, struct termios *t)
494 {
495 	struct umodem_softc *sc = addr;
496 	usbd_status err;
497 	usb_cdc_line_state_t ls;
498 
499 	DPRINTF(("umodem_param: sc=%p\n", sc));
500 
501 	USETDW(ls.dwDTERate, t->c_ospeed);
502 	if (ISSET(t->c_cflag, CSTOPB))
503 		ls.bCharFormat = UCDC_STOP_BIT_2;
504 	else
505 		ls.bCharFormat = UCDC_STOP_BIT_1;
506 	if (ISSET(t->c_cflag, PARENB)) {
507 		if (ISSET(t->c_cflag, PARODD))
508 			ls.bParityType = UCDC_PARITY_ODD;
509 		else
510 			ls.bParityType = UCDC_PARITY_EVEN;
511 	} else
512 		ls.bParityType = UCDC_PARITY_NONE;
513 	switch (ISSET(t->c_cflag, CSIZE)) {
514 	case CS5:
515 		ls.bDataBits = 5;
516 		break;
517 	case CS6:
518 		ls.bDataBits = 6;
519 		break;
520 	case CS7:
521 		ls.bDataBits = 7;
522 		break;
523 	case CS8:
524 		ls.bDataBits = 8;
525 		break;
526 	}
527 
528 	err = umodem_set_line_coding(sc, &ls);
529 	if (err) {
530 		DPRINTF(("umodem_param: err=%s\n", usbd_errstr(err)));
531 		return (EPASSTHROUGH);
532 	}
533 	return (0);
534 }
535 
536 int
537 umodem_ioctl(void *addr, int portno, u_long cmd, caddr_t data, int flag,
538 	     usb_proc_ptr p)
539 {
540 	struct umodem_softc *sc = addr;
541 	int error = 0;
542 
543 	if (sc->sc_dying)
544 		return (EIO);
545 
546 	DPRINTF(("umodemioctl: cmd=0x%08lx\n", cmd));
547 
548 	switch (cmd) {
549 	case USB_GET_CM_OVER_DATA:
550 		*(int *)data = sc->sc_cm_over_data;
551 		break;
552 
553 	case USB_SET_CM_OVER_DATA:
554 		if (*(int *)data != sc->sc_cm_over_data) {
555 			/* XXX change it */
556 		}
557 		break;
558 
559 	default:
560 		DPRINTF(("umodemioctl: unknown\n"));
561 		error = EPASSTHROUGH;
562 		break;
563 	}
564 
565 	return (error);
566 }
567 
568 void
569 umodem_dtr(struct umodem_softc *sc, int onoff)
570 {
571 	DPRINTF(("umodem_modem: onoff=%d\n", onoff));
572 
573 	if (sc->sc_dtr == onoff)
574 		return;
575 	sc->sc_dtr = onoff;
576 
577 	umodem_set_line_state(sc);
578 }
579 
580 void
581 umodem_rts(struct umodem_softc *sc, int onoff)
582 {
583 	DPRINTF(("umodem_modem: onoff=%d\n", onoff));
584 
585 	if (sc->sc_rts == onoff)
586 		return;
587 	sc->sc_rts = onoff;
588 
589 	umodem_set_line_state(sc);
590 }
591 
592 void
593 umodem_set_line_state(struct umodem_softc *sc)
594 {
595 	usb_device_request_t req;
596 	int ls;
597 
598 	ls = (sc->sc_dtr ? UCDC_LINE_DTR : 0) |
599 	     (sc->sc_rts ? UCDC_LINE_RTS : 0);
600 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
601 	req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
602 	USETW(req.wValue, ls);
603 	USETW(req.wIndex, sc->sc_ctl_iface_no);
604 	USETW(req.wLength, 0);
605 
606 	(void)usbd_do_request(sc->sc_udev, &req, 0);
607 
608 }
609 
610 void
611 umodem_break(struct umodem_softc *sc, int onoff)
612 {
613 	usb_device_request_t req;
614 
615 	DPRINTF(("umodem_break: onoff=%d\n", onoff));
616 
617 	if (!(sc->sc_acm_cap & USB_CDC_ACM_HAS_BREAK))
618 		return;
619 
620 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
621 	req.bRequest = UCDC_SEND_BREAK;
622 	USETW(req.wValue, onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF);
623 	USETW(req.wIndex, sc->sc_ctl_iface_no);
624 	USETW(req.wLength, 0);
625 
626 	(void)usbd_do_request(sc->sc_udev, &req, 0);
627 }
628 
629 void
630 umodem_set(void *addr, int portno, int reg, int onoff)
631 {
632 	struct umodem_softc *sc = addr;
633 
634 	switch (reg) {
635 	case UCOM_SET_DTR:
636 		umodem_dtr(sc, onoff);
637 		break;
638 	case UCOM_SET_RTS:
639 		umodem_rts(sc, onoff);
640 		break;
641 	case UCOM_SET_BREAK:
642 		umodem_break(sc, onoff);
643 		break;
644 	default:
645 		break;
646 	}
647 }
648 
649 usbd_status
650 umodem_set_line_coding(struct umodem_softc *sc, usb_cdc_line_state_t *state)
651 {
652 	usb_device_request_t req;
653 	usbd_status err;
654 
655 	DPRINTF(("umodem_set_line_coding: rate=%d fmt=%d parity=%d bits=%d\n",
656 		 UGETDW(state->dwDTERate), state->bCharFormat,
657 		 state->bParityType, state->bDataBits));
658 
659 	if (memcmp(state, &sc->sc_line_state, UCDC_LINE_STATE_LENGTH) == 0) {
660 		DPRINTF(("umodem_set_line_coding: already set\n"));
661 		return (USBD_NORMAL_COMPLETION);
662 	}
663 
664 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
665 	req.bRequest = UCDC_SET_LINE_CODING;
666 	USETW(req.wValue, 0);
667 	USETW(req.wIndex, sc->sc_ctl_iface_no);
668 	USETW(req.wLength, UCDC_LINE_STATE_LENGTH);
669 
670 	err = usbd_do_request(sc->sc_udev, &req, state);
671 	if (err) {
672 		DPRINTF(("umodem_set_line_coding: failed, err=%s\n",
673 			 usbd_errstr(err)));
674 		return (err);
675 	}
676 
677 	sc->sc_line_state = *state;
678 
679 	return (USBD_NORMAL_COMPLETION);
680 }
681 
682 usbd_status
683 umodem_set_comm_feature(struct umodem_softc *sc, int feature, int state)
684 {
685 	usb_device_request_t req;
686 	usbd_status err;
687 	usb_cdc_abstract_state_t ast;
688 
689 	DPRINTF(("umodem_set_comm_feature: feature=%d state=%d\n", feature,
690 		 state));
691 
692 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
693 	req.bRequest = UCDC_SET_COMM_FEATURE;
694 	USETW(req.wValue, feature);
695 	USETW(req.wIndex, sc->sc_ctl_iface_no);
696 	USETW(req.wLength, UCDC_ABSTRACT_STATE_LENGTH);
697 	USETW(ast.wState, state);
698 
699 	err = usbd_do_request(sc->sc_udev, &req, &ast);
700 	if (err) {
701 		DPRINTF(("umodem_set_comm_feature: feature=%d, err=%s\n",
702 			 feature, usbd_errstr(err)));
703 		return (err);
704 	}
705 
706 	return (USBD_NORMAL_COMPLETION);
707 }
708 
709 int
710 umodem_activate(device_ptr_t self, enum devact act)
711 {
712 	struct umodem_softc *sc = (struct umodem_softc *)self;
713 	int rv = 0;
714 
715 	switch (act) {
716 	case DVACT_ACTIVATE:
717 		return (EOPNOTSUPP);
718 
719 	case DVACT_DEACTIVATE:
720 		sc->sc_dying = 1;
721 		if (sc->sc_subdev)
722 			rv = config_deactivate(sc->sc_subdev);
723 		break;
724 	}
725 	return (rv);
726 }
727 
728 USB_DETACH(umodem)
729 {
730 	USB_DETACH_START(umodem, sc);
731 	int rv = 0;
732 
733 	DPRINTF(("umodem_detach: sc=%p flags=%d\n", sc, flags));
734 
735 	sc->sc_dying = 1;
736 
737 	if (sc->sc_subdev != NULL)
738 		rv = config_detach(sc->sc_subdev, flags);
739 
740 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
741 			   USBDEV(sc->sc_dev));
742 
743 	return (rv);
744 }
745