xref: /openbsd-src/sys/dev/usb/umodem.c (revision 673b2bfbd5d73bb9c8f4cc4d9c00f4d35f73410a)
1 /*	$OpenBSD: umodem.c,v 1.46 2013/03/28 03:31:55 tedu Exp $ */
2 /*	$NetBSD: umodem.c,v 1.45 2002/09/23 05:51:23 simonb Exp $	*/
3 
4 /*
5  * Copyright (c) 1998 The NetBSD Foundation, Inc.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to The NetBSD Foundation
9  * by Lennart Augustsson (lennart@augustsson.net) at
10  * Carlstedt Research & Technology.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
25  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 /*
35  * Comm Class spec:  http://www.usb.org/developers/devclass_docs/usbccs10.pdf
36  *                   http://www.usb.org/developers/devclass_docs/usbcdc11.pdf
37  */
38 
39 /*
40  * TODO:
41  * - Add error recovery in various places; the big problem is what
42  *   to do in a callback if there is an error.
43  * - Implement a Call Device for modems without multiplexed commands.
44  *
45  */
46 
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/kernel.h>
50 #include <sys/ioctl.h>
51 #include <sys/conf.h>
52 #include <sys/tty.h>
53 #include <sys/file.h>
54 #include <sys/selinfo.h>
55 #include <sys/proc.h>
56 #include <sys/device.h>
57 #include <sys/poll.h>
58 
59 #include <dev/usb/usb.h>
60 #include <dev/usb/usbcdc.h>
61 
62 #include <dev/usb/usbdi.h>
63 #include <dev/usb/usbdi_util.h>
64 #include <dev/usb/usbdevs.h>
65 #include <dev/usb/usb_quirks.h>
66 
67 #include <dev/usb/usbdevs.h>
68 #include <dev/usb/ucomvar.h>
69 
70 #ifdef UMODEM_DEBUG
71 #define DPRINTFN(n, x)	do { if (umodemdebug > (n)) printf x; } while (0)
72 int	umodemdebug = 0;
73 #else
74 #define DPRINTFN(n, x)
75 #endif
76 #define DPRINTF(x) DPRINTFN(0, x)
77 
78 /*
79  * These are the maximum number of bytes transferred per frame.
80  * Buffers are this large to deal with high speed wireless devices.
81  * Capped at 1024 as ttymalloc() is limited to this amount.
82  */
83 #define UMODEMIBUFSIZE 1024
84 #define UMODEMOBUFSIZE 1024
85 
86 struct umodem_softc {
87 	struct device		 sc_dev;	/* base device */
88 
89 	usbd_device_handle	 sc_udev;	/* USB device */
90 
91 	int			 sc_ctl_iface_no;
92 	usbd_interface_handle	 sc_ctl_iface;	/* control interface */
93 	usbd_interface_handle	 sc_data_iface;	/* data interface */
94 
95 	int			 sc_cm_cap;	/* CM capabilities */
96 	int			 sc_acm_cap;	/* ACM capabilities */
97 
98 	int			 sc_cm_over_data;
99 
100 	usb_cdc_line_state_t	 sc_line_state;	/* current line state */
101 	u_char			 sc_dtr;	/* current DTR state */
102 	u_char			 sc_rts;	/* current RTS state */
103 
104 	struct device		*sc_subdev;	/* ucom device */
105 
106 	u_char			 sc_opening;	/* lock during open */
107 	u_char			 sc_dying;	/* disconnecting */
108 
109 	int			 sc_ctl_notify;	/* Notification endpoint */
110 	usbd_pipe_handle	 sc_notify_pipe; /* Notification pipe */
111 	usb_cdc_notification_t	 sc_notify_buf;	/* Notification structure */
112 	u_char			 sc_lsr;	/* Local status register */
113 	u_char			 sc_msr;	/* Modem status register */
114 };
115 
116 usbd_status umodem_set_comm_feature(struct umodem_softc *sc,
117 					   int feature, int state);
118 usbd_status umodem_set_line_coding(struct umodem_softc *sc,
119 					  usb_cdc_line_state_t *state);
120 
121 void	umodem_get_status(void *, int portno, u_char *lsr, u_char *msr);
122 void	umodem_set(void *, int, int, int);
123 void	umodem_dtr(struct umodem_softc *, int);
124 void	umodem_rts(struct umodem_softc *, int);
125 void	umodem_break(struct umodem_softc *, int);
126 void	umodem_set_line_state(struct umodem_softc *);
127 int	umodem_param(void *, int, struct termios *);
128 int	umodem_ioctl(void *, int, u_long, caddr_t, int, struct proc *);
129 int	umodem_open(void *, int portno);
130 void	umodem_close(void *, int portno);
131 void	umodem_intr(usbd_xfer_handle, usbd_private_handle, usbd_status);
132 
133 struct ucom_methods umodem_methods = {
134 	umodem_get_status,
135 	umodem_set,
136 	umodem_param,
137 	umodem_ioctl,
138 	umodem_open,
139 	umodem_close,
140 	NULL,
141 	NULL,
142 };
143 
144 int umodem_match(struct device *, void *, void *);
145 void umodem_attach(struct device *, struct device *, void *);
146 int umodem_detach(struct device *, int);
147 int umodem_activate(struct device *, int);
148 
149 void umodem_get_caps(struct usb_attach_arg *, int, int *, int *, int *);
150 
151 struct cfdriver umodem_cd = {
152 	NULL, "umodem", DV_DULL
153 };
154 
155 const struct cfattach umodem_ca = {
156 	sizeof(struct umodem_softc),
157 	umodem_match,
158 	umodem_attach,
159 	umodem_detach,
160 	umodem_activate,
161 };
162 
163 void
164 umodem_get_caps(struct usb_attach_arg *uaa, int ctl_iface_no,
165     int *data_iface_no, int *cm_cap, int *acm_cap)
166 {
167 	const usb_descriptor_t *desc;
168 	const usb_interface_descriptor_t *id;
169 	const usb_cdc_cm_descriptor_t *cmd;
170 	const usb_cdc_acm_descriptor_t *acmd;
171 	const usb_cdc_union_descriptor_t *uniond;
172 	usbd_desc_iter_t iter;
173 	int current_iface_no = -1;
174 
175 	*data_iface_no = -1;
176 	*cm_cap = *acm_cap = 0;
177 	usb_desc_iter_init(uaa->device, &iter);
178 	desc = usb_desc_iter_next(&iter);
179 	while (desc) {
180 		if (desc->bDescriptorType == UDESC_INTERFACE) {
181 			id = (usb_interface_descriptor_t *)desc;
182 			current_iface_no = id->bInterfaceNumber;
183 			if (current_iface_no != ctl_iface_no &&
184 			    id->bInterfaceClass == UICLASS_CDC_DATA &&
185 			    id->bInterfaceSubClass == UISUBCLASS_DATA &&
186 			    *data_iface_no == -1)
187 				*data_iface_no = current_iface_no;
188 		}
189 		if (current_iface_no == ctl_iface_no &&
190 		    desc->bDescriptorType == UDESC_CS_INTERFACE) {
191 			switch(desc->bDescriptorSubtype) {
192 			case UDESCSUB_CDC_CM:
193 				cmd = (usb_cdc_cm_descriptor_t *)desc;
194 				*cm_cap = cmd->bmCapabilities;
195 				*data_iface_no = cmd->bDataInterface;
196 				break;
197 			case UDESCSUB_CDC_ACM:
198 				acmd = (usb_cdc_acm_descriptor_t *)desc;
199 				*acm_cap = acmd->bmCapabilities;
200 				break;
201 			case UDESCSUB_CDC_UNION:
202 				uniond = (usb_cdc_union_descriptor_t *)desc;
203 				*data_iface_no = uniond->bSlaveInterface[0];
204 				break;
205 			}
206 		}
207 		desc = usb_desc_iter_next(&iter);
208 	}
209 }
210 
211 int
212 umodem_match(struct device *parent, void *match, void *aux)
213 {
214 	struct usb_attach_arg *uaa = aux;
215 	usb_interface_descriptor_t *id;
216 	usb_device_descriptor_t *dd;
217 	int data_iface_no, cm_cap, acm_cap, ret = UMATCH_NONE;
218 
219 	if (uaa->iface == NULL)
220 		return (ret);
221 
222 	id = usbd_get_interface_descriptor(uaa->iface);
223 	dd = usbd_get_device_descriptor(uaa->device);
224 	if (id == NULL || dd == NULL)
225 		return (ret);
226 
227 	ret = UMATCH_NONE;
228 
229 	/* protocol of 0 so won't match the test below */
230 	if (UGETW(dd->idVendor) == USB_VENDOR_ATMEL &&
231 	    UGETW(dd->idProduct) == USB_PRODUCT_ATMEL_AT91_CDC_ACM)
232 		ret = UMATCH_VENDOR_PRODUCT;
233 
234 	if (UGETW(dd->idVendor) == USB_VENDOR_KYOCERA &&
235 	    UGETW(dd->idProduct) == USB_PRODUCT_KYOCERA_AHK3001V &&
236 	    id->bInterfaceNumber == 0)
237 		ret = UMATCH_VENDOR_PRODUCT;
238 
239 	if (ret == UMATCH_NONE &&
240 	    id->bInterfaceClass == UICLASS_CDC &&
241 	    id->bInterfaceSubClass == UISUBCLASS_ABSTRACT_CONTROL_MODEL &&
242 	    id->bInterfaceProtocol == UIPROTO_CDC_AT)
243 		ret = UMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO;
244 
245 	if (ret == UMATCH_NONE)
246 		return (ret);
247 
248 	/* umodem doesn't support devices without a data iface */
249 	umodem_get_caps(uaa, id->bInterfaceNumber, &data_iface_no,
250 	    &cm_cap, &acm_cap);
251 	if (data_iface_no == -1)
252 		ret = UMATCH_NONE;
253 
254 	return (ret);
255 }
256 
257 void
258 umodem_attach(struct device *parent, struct device *self, void *aux)
259 {
260 	struct umodem_softc *sc = (struct umodem_softc *)self;
261 	struct usb_attach_arg *uaa = aux;
262 	usbd_device_handle dev = uaa->device;
263 	usb_interface_descriptor_t *id;
264 	usb_endpoint_descriptor_t *ed;
265 	usbd_status err;
266 	int data_iface_no = 0;
267 	int i;
268 	struct ucom_attach_args uca;
269 
270 	sc->sc_udev = dev;
271 	sc->sc_ctl_iface = uaa->iface;
272 
273 	id = usbd_get_interface_descriptor(sc->sc_ctl_iface);
274 	//printf("%s: iclass %d/%d\n", sc->sc_dev.dv_xname,
275 	//    id->bInterfaceClass, id->bInterfaceSubClass);
276 	sc->sc_ctl_iface_no = id->bInterfaceNumber;
277 
278 	/* Get the capabilities. */
279 	umodem_get_caps(uaa, id->bInterfaceNumber, &data_iface_no,
280 	    &sc->sc_cm_cap, &sc->sc_acm_cap);
281 	if (data_iface_no == -1) {
282 		printf("%s: no data interface\n",
283 		       sc->sc_dev.dv_xname);
284 		goto bad;
285 	}
286 
287 	printf("%s: data interface %d, has %sCM over data, has %sbreak\n",
288 	       sc->sc_dev.dv_xname, data_iface_no,
289 	       sc->sc_cm_cap & USB_CDC_CM_OVER_DATA ? "" : "no ",
290 	       sc->sc_acm_cap & USB_CDC_ACM_HAS_BREAK ? "" : "no ");
291 
292 	/* Get the data interface too. */
293 	for (i = 0; i < uaa->nifaces; i++) {
294 		if (!usbd_iface_claimed(sc->sc_udev, i)) {
295 			id = usbd_get_interface_descriptor(uaa->ifaces[i]);
296 			if (id != NULL &&
297                             id->bInterfaceNumber == data_iface_no) {
298 				sc->sc_data_iface = uaa->ifaces[i];
299 				usbd_claim_iface(sc->sc_udev, i);
300 			}
301 		}
302 	}
303 	if (sc->sc_data_iface == NULL) {
304 		printf("%s: no data interface\n", sc->sc_dev.dv_xname);
305 		goto bad;
306 	}
307 
308 	/*
309 	 * Find the bulk endpoints.
310 	 * Iterate over all endpoints in the data interface and take note.
311 	 */
312 	uca.bulkin = uca.bulkout = -1;
313 
314 	id = usbd_get_interface_descriptor(sc->sc_data_iface);
315 	for (i = 0; i < id->bNumEndpoints; i++) {
316 		ed = usbd_interface2endpoint_descriptor(sc->sc_data_iface, i);
317 		if (ed == NULL) {
318 			printf("%s: no endpoint descriptor for %d\n",
319 				sc->sc_dev.dv_xname, i);
320 			goto bad;
321 		}
322 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
323 		    (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
324                         uca.bulkin = ed->bEndpointAddress;
325                 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
326 			   (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
327                         uca.bulkout = ed->bEndpointAddress;
328                 }
329         }
330 
331 	if (uca.bulkin == -1) {
332 		printf("%s: Could not find data bulk in\n",
333 		       sc->sc_dev.dv_xname);
334 		goto bad;
335 	}
336 	if (uca.bulkout == -1) {
337 		printf("%s: Could not find data bulk out\n",
338 			sc->sc_dev.dv_xname);
339 		goto bad;
340 	}
341 
342 	if (usbd_get_quirks(sc->sc_udev)->uq_flags & UQ_ASSUME_CM_OVER_DATA) {
343 		sc->sc_cm_over_data = 1;
344 	} else {
345 		if (sc->sc_cm_cap & USB_CDC_CM_OVER_DATA) {
346 			if (sc->sc_acm_cap & USB_CDC_ACM_HAS_FEATURE)
347 				err = umodem_set_comm_feature(sc,
348 				    UCDC_ABSTRACT_STATE, UCDC_DATA_MULTIPLEXED);
349 			else
350 				err = 0;
351 			if (err) {
352 				printf("%s: could not set data multiplex mode\n",
353 				       sc->sc_dev.dv_xname);
354 				goto bad;
355 			}
356 			sc->sc_cm_over_data = 1;
357 		}
358 	}
359 
360 	/*
361 	 * The standard allows for notification messages (to indicate things
362 	 * like a modem hangup) to come in via an interrupt endpoint
363 	 * off of the control interface.  Iterate over the endpoints on
364 	 * the control interface and see if there are any interrupt
365 	 * endpoints; if there are, then register it.
366 	 */
367 
368 	sc->sc_ctl_notify = -1;
369 	sc->sc_notify_pipe = NULL;
370 
371 	id = usbd_get_interface_descriptor(sc->sc_ctl_iface);
372 	for (i = 0; i < id->bNumEndpoints; i++) {
373 		ed = usbd_interface2endpoint_descriptor(sc->sc_ctl_iface, i);
374 		if (ed == NULL)
375 			continue;
376 
377 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
378 		    (ed->bmAttributes & UE_XFERTYPE) == UE_INTERRUPT) {
379 			printf("%s: status change notification available\n",
380 			       sc->sc_dev.dv_xname);
381 			sc->sc_ctl_notify = ed->bEndpointAddress;
382 		}
383 	}
384 
385 	sc->sc_dtr = -1;
386 
387 	uca.portno = UCOM_UNK_PORTNO;
388 	/* bulkin, bulkout set above */
389 	uca.ibufsize = UMODEMIBUFSIZE;
390 	uca.obufsize = UMODEMOBUFSIZE;
391 	uca.ibufsizepad = UMODEMIBUFSIZE;
392 	uca.opkthdrlen = 0;
393 	uca.device = sc->sc_udev;
394 	uca.iface = sc->sc_data_iface;
395 	uca.methods = &umodem_methods;
396 	uca.arg = sc;
397 	uca.info = NULL;
398 
399 	DPRINTF(("umodem_attach: sc=%p\n", sc));
400 	sc->sc_subdev = config_found_sm(self, &uca, ucomprint, ucomsubmatch);
401 
402 	return;
403 
404  bad:
405 	sc->sc_dying = 1;
406 }
407 
408 int
409 umodem_open(void *addr, int portno)
410 {
411 	struct umodem_softc *sc = addr;
412 	int err;
413 
414 	DPRINTF(("umodem_open: sc=%p\n", sc));
415 
416 	if (sc->sc_ctl_notify != -1 && sc->sc_notify_pipe == NULL) {
417 		err = usbd_open_pipe_intr(sc->sc_ctl_iface, sc->sc_ctl_notify,
418 		    USBD_SHORT_XFER_OK, &sc->sc_notify_pipe, sc,
419 		    &sc->sc_notify_buf, sizeof(sc->sc_notify_buf),
420 		    umodem_intr, USBD_DEFAULT_INTERVAL);
421 
422 		if (err) {
423 			DPRINTF(("Failed to establish notify pipe: %s\n",
424 				usbd_errstr(err)));
425 			return EIO;
426 		}
427 	}
428 
429 	return 0;
430 }
431 
432 void
433 umodem_close(void *addr, int portno)
434 {
435 	struct umodem_softc *sc = addr;
436 	int err;
437 
438 	DPRINTF(("umodem_close: sc=%p\n", sc));
439 
440 	if (sc->sc_notify_pipe != NULL) {
441 		err = usbd_abort_pipe(sc->sc_notify_pipe);
442 		if (err)
443 			printf("%s: abort notify pipe failed: %s\n",
444 			    sc->sc_dev.dv_xname, usbd_errstr(err));
445 		err = usbd_close_pipe(sc->sc_notify_pipe);
446 		if (err)
447 			printf("%s: close notify pipe failed: %s\n",
448 			    sc->sc_dev.dv_xname, usbd_errstr(err));
449 		sc->sc_notify_pipe = NULL;
450 	}
451 }
452 
453 void
454 umodem_intr(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
455 {
456 	struct umodem_softc *sc = priv;
457 	u_char mstatus;
458 
459 	if (sc->sc_dying)
460 		return;
461 
462 	if (status != USBD_NORMAL_COMPLETION) {
463 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
464 			return;
465 		DPRINTF(("%s: abnormal status: %s\n", sc->sc_dev.dv_xname,
466 		       usbd_errstr(status)));
467 		usbd_clear_endpoint_stall_async(sc->sc_notify_pipe);
468 		return;
469 	}
470 
471 	if (sc->sc_notify_buf.bmRequestType != UCDC_NOTIFICATION) {
472 		DPRINTF(("%s: unknown message type (%02x) on notify pipe\n",
473 			 sc->sc_dev.dv_xname,
474 			 sc->sc_notify_buf.bmRequestType));
475 		return;
476 	}
477 
478 	switch (sc->sc_notify_buf.bNotification) {
479 	case UCDC_N_SERIAL_STATE:
480 		/*
481 		 * Set the serial state in ucom driver based on
482 		 * the bits from the notify message
483 		 */
484 		if (UGETW(sc->sc_notify_buf.wLength) != 2) {
485 			printf("%s: Invalid notification length! (%d)\n",
486 			       sc->sc_dev.dv_xname,
487 			       UGETW(sc->sc_notify_buf.wLength));
488 			break;
489 		}
490 		DPRINTF(("%s: notify bytes = %02x%02x\n",
491 			 sc->sc_dev.dv_xname,
492 			 sc->sc_notify_buf.data[0],
493 			 sc->sc_notify_buf.data[1]));
494 		/* Currently, lsr is always zero. */
495 		sc->sc_lsr = sc->sc_msr = 0;
496 		mstatus = sc->sc_notify_buf.data[0];
497 
498 		if (ISSET(mstatus, UCDC_N_SERIAL_RI))
499 			sc->sc_msr |= UMSR_RI;
500 		if (ISSET(mstatus, UCDC_N_SERIAL_DSR))
501 			sc->sc_msr |= UMSR_DSR;
502 		if (ISSET(mstatus, UCDC_N_SERIAL_DCD))
503 			sc->sc_msr |= UMSR_DCD;
504 		ucom_status_change((struct ucom_softc *)sc->sc_subdev);
505 		break;
506 	default:
507 		DPRINTF(("%s: unknown notify message: %02x\n",
508 			 sc->sc_dev.dv_xname,
509 			 sc->sc_notify_buf.bNotification));
510 		break;
511 	}
512 }
513 
514 void
515 umodem_get_status(void *addr, int portno, u_char *lsr, u_char *msr)
516 {
517 	struct umodem_softc *sc = addr;
518 
519 	DPRINTF(("umodem_get_status:\n"));
520 
521 	if (lsr != NULL)
522 		*lsr = sc->sc_lsr;
523 	if (msr != NULL)
524 		*msr = sc->sc_msr;
525 }
526 
527 int
528 umodem_param(void *addr, int portno, struct termios *t)
529 {
530 	struct umodem_softc *sc = addr;
531 	usbd_status err;
532 	usb_cdc_line_state_t ls;
533 
534 	DPRINTF(("umodem_param: sc=%p\n", sc));
535 
536 	USETDW(ls.dwDTERate, t->c_ospeed);
537 	if (ISSET(t->c_cflag, CSTOPB))
538 		ls.bCharFormat = UCDC_STOP_BIT_2;
539 	else
540 		ls.bCharFormat = UCDC_STOP_BIT_1;
541 	if (ISSET(t->c_cflag, PARENB)) {
542 		if (ISSET(t->c_cflag, PARODD))
543 			ls.bParityType = UCDC_PARITY_ODD;
544 		else
545 			ls.bParityType = UCDC_PARITY_EVEN;
546 	} else
547 		ls.bParityType = UCDC_PARITY_NONE;
548 	switch (ISSET(t->c_cflag, CSIZE)) {
549 	case CS5:
550 		ls.bDataBits = 5;
551 		break;
552 	case CS6:
553 		ls.bDataBits = 6;
554 		break;
555 	case CS7:
556 		ls.bDataBits = 7;
557 		break;
558 	case CS8:
559 		ls.bDataBits = 8;
560 		break;
561 	}
562 
563 	err = umodem_set_line_coding(sc, &ls);
564 	if (err) {
565 		DPRINTF(("umodem_param: err=%s\n", usbd_errstr(err)));
566 		return (1);
567 	}
568 	return (0);
569 }
570 
571 int
572 umodem_ioctl(void *addr, int portno, u_long cmd, caddr_t data, int flag,
573 	     struct proc *p)
574 {
575 	struct umodem_softc *sc = addr;
576 	int error = 0;
577 
578 	if (sc->sc_dying)
579 		return (EIO);
580 
581 	DPRINTF(("umodemioctl: cmd=0x%08lx\n", cmd));
582 
583 	switch (cmd) {
584 	case USB_GET_CM_OVER_DATA:
585 		*(int *)data = sc->sc_cm_over_data;
586 		break;
587 
588 	case USB_SET_CM_OVER_DATA:
589 		if (*(int *)data != sc->sc_cm_over_data) {
590 			/* XXX change it */
591 		}
592 		break;
593 
594 	default:
595 		DPRINTF(("umodemioctl: unknown\n"));
596 		error = ENOTTY;
597 		break;
598 	}
599 
600 	return (error);
601 }
602 
603 void
604 umodem_dtr(struct umodem_softc *sc, int onoff)
605 {
606 	DPRINTF(("umodem_modem: onoff=%d\n", onoff));
607 
608 	if (sc->sc_dtr == onoff)
609 		return;
610 	sc->sc_dtr = onoff;
611 
612 	umodem_set_line_state(sc);
613 }
614 
615 void
616 umodem_rts(struct umodem_softc *sc, int onoff)
617 {
618 	DPRINTF(("umodem_modem: onoff=%d\n", onoff));
619 
620 	if (sc->sc_rts == onoff)
621 		return;
622 	sc->sc_rts = onoff;
623 
624 	umodem_set_line_state(sc);
625 }
626 
627 void
628 umodem_set_line_state(struct umodem_softc *sc)
629 {
630 	usb_device_request_t req;
631 	int ls;
632 
633 	ls = (sc->sc_dtr ? UCDC_LINE_DTR : 0) |
634 	     (sc->sc_rts ? UCDC_LINE_RTS : 0);
635 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
636 	req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
637 	USETW(req.wValue, ls);
638 	USETW(req.wIndex, sc->sc_ctl_iface_no);
639 	USETW(req.wLength, 0);
640 
641 	(void)usbd_do_request(sc->sc_udev, &req, 0);
642 
643 }
644 
645 void
646 umodem_break(struct umodem_softc *sc, int onoff)
647 {
648 	usb_device_request_t req;
649 
650 	DPRINTF(("umodem_break: onoff=%d\n", onoff));
651 
652 	if (!(sc->sc_acm_cap & USB_CDC_ACM_HAS_BREAK))
653 		return;
654 
655 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
656 	req.bRequest = UCDC_SEND_BREAK;
657 	USETW(req.wValue, onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF);
658 	USETW(req.wIndex, sc->sc_ctl_iface_no);
659 	USETW(req.wLength, 0);
660 
661 	(void)usbd_do_request(sc->sc_udev, &req, 0);
662 }
663 
664 void
665 umodem_set(void *addr, int portno, int reg, int onoff)
666 {
667 	struct umodem_softc *sc = addr;
668 
669 	switch (reg) {
670 	case UCOM_SET_DTR:
671 		umodem_dtr(sc, onoff);
672 		break;
673 	case UCOM_SET_RTS:
674 		umodem_rts(sc, onoff);
675 		break;
676 	case UCOM_SET_BREAK:
677 		umodem_break(sc, onoff);
678 		break;
679 	default:
680 		break;
681 	}
682 }
683 
684 usbd_status
685 umodem_set_line_coding(struct umodem_softc *sc, usb_cdc_line_state_t *state)
686 {
687 	usb_device_request_t req;
688 	usbd_status err;
689 
690 	DPRINTF(("umodem_set_line_coding: rate=%d fmt=%d parity=%d bits=%d\n",
691 		 UGETDW(state->dwDTERate), state->bCharFormat,
692 		 state->bParityType, state->bDataBits));
693 
694 	if (memcmp(state, &sc->sc_line_state, UCDC_LINE_STATE_LENGTH) == 0) {
695 		DPRINTF(("umodem_set_line_coding: already set\n"));
696 		return (USBD_NORMAL_COMPLETION);
697 	}
698 
699 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
700 	req.bRequest = UCDC_SET_LINE_CODING;
701 	USETW(req.wValue, 0);
702 	USETW(req.wIndex, sc->sc_ctl_iface_no);
703 	USETW(req.wLength, UCDC_LINE_STATE_LENGTH);
704 
705 	err = usbd_do_request(sc->sc_udev, &req, state);
706 	if (err) {
707 		DPRINTF(("umodem_set_line_coding: failed, err=%s\n",
708 			 usbd_errstr(err)));
709 		return (err);
710 	}
711 
712 	sc->sc_line_state = *state;
713 
714 	return (USBD_NORMAL_COMPLETION);
715 }
716 
717 usbd_status
718 umodem_set_comm_feature(struct umodem_softc *sc, int feature, int state)
719 {
720 	usb_device_request_t req;
721 	usbd_status err;
722 	usb_cdc_abstract_state_t ast;
723 
724 	DPRINTF(("umodem_set_comm_feature: feature=%d state=%d\n", feature,
725 		 state));
726 
727 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
728 	req.bRequest = UCDC_SET_COMM_FEATURE;
729 	USETW(req.wValue, feature);
730 	USETW(req.wIndex, sc->sc_ctl_iface_no);
731 	USETW(req.wLength, UCDC_ABSTRACT_STATE_LENGTH);
732 	USETW(ast.wState, state);
733 
734 	err = usbd_do_request(sc->sc_udev, &req, &ast);
735 	if (err) {
736 		DPRINTF(("umodem_set_comm_feature: feature=%d, err=%s\n",
737 			 feature, usbd_errstr(err)));
738 		return (err);
739 	}
740 
741 	return (USBD_NORMAL_COMPLETION);
742 }
743 
744 int
745 umodem_activate(struct device *self, int act)
746 {
747 	struct umodem_softc *sc = (struct umodem_softc *)self;
748 	int rv = 0;
749 
750 	switch (act) {
751 	case DVACT_DEACTIVATE:
752 		sc->sc_dying = 1;
753 		if (sc->sc_subdev)
754 			rv = config_deactivate(sc->sc_subdev);
755 		break;
756 	}
757 	return (rv);
758 }
759 
760 int
761 umodem_detach(struct device *self, int flags)
762 {
763 	struct umodem_softc *sc = (struct umodem_softc *)self;
764 	int rv = 0;
765 
766 	DPRINTF(("umodem_detach: sc=%p flags=%d\n", sc, flags));
767 
768 	if (sc->sc_subdev != NULL)
769 		rv = config_detach(sc->sc_subdev, flags);
770 
771 	return (rv);
772 }
773