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