xref: /openbsd-src/sys/dev/usb/umodem.c (revision 21dab745d772244ad59a415114e48be2888cfbc8)
1 /*	$OpenBSD: umodem.c,v 1.59 2015/03/14 03:38:50 jsg 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/usbdevs.h>
63 #include <dev/usb/usb_quirks.h>
64 
65 #include <dev/usb/ucomvar.h>
66 
67 #ifdef UMODEM_DEBUG
68 #define DPRINTFN(n, x)	do { if (umodemdebug > (n)) printf x; } while (0)
69 int	umodemdebug = 0;
70 #else
71 #define DPRINTFN(n, x)
72 #endif
73 #define DPRINTF(x) DPRINTFN(0, x)
74 
75 /*
76  * These are the maximum number of bytes transferred per frame.
77  * Buffers are this large to deal with high speed wireless devices.
78  * Capped at 1024 as ttymalloc() is limited to this amount.
79  */
80 #define UMODEMIBUFSIZE 1024
81 #define UMODEMOBUFSIZE 1024
82 
83 struct umodem_softc {
84 	struct device		 sc_dev;	/* base device */
85 
86 	struct usbd_device	*sc_udev;	/* USB device */
87 
88 	int			 sc_ctl_iface_no;
89 	struct usbd_interface	*sc_ctl_iface;	/* control interface */
90 	struct usbd_interface	*sc_data_iface;	/* data interface */
91 
92 	int			 sc_cm_cap;	/* CM capabilities */
93 	int			 sc_acm_cap;	/* ACM capabilities */
94 
95 	int			 sc_cm_over_data;
96 
97 	struct usb_cdc_line_state sc_line_state;/* current line state */
98 	u_char			 sc_dtr;	/* current DTR state */
99 	u_char			 sc_rts;	/* current RTS state */
100 
101 	struct device		*sc_subdev;	/* ucom device */
102 
103 	u_char			 sc_opening;	/* lock during open */
104 
105 	int			 sc_ctl_notify;	/* Notification endpoint */
106 	struct usbd_pipe	*sc_notify_pipe; /* Notification pipe */
107 	struct usb_cdc_notification sc_notify_buf; /* Notification structure */
108 	u_char			 sc_lsr;	/* Local status register */
109 	u_char			 sc_msr;	/* Modem status register */
110 };
111 
112 usbd_status umodem_set_comm_feature(struct umodem_softc *sc,
113 					   int feature, int state);
114 usbd_status umodem_set_line_coding(struct umodem_softc *sc,
115 					  struct usb_cdc_line_state *state);
116 
117 void	umodem_get_status(void *, int portno, u_char *lsr, u_char *msr);
118 void	umodem_set(void *, int, int, int);
119 void	umodem_dtr(struct umodem_softc *, int);
120 void	umodem_rts(struct umodem_softc *, int);
121 void	umodem_break(struct umodem_softc *, int);
122 void	umodem_set_line_state(struct umodem_softc *);
123 int	umodem_param(void *, int, struct termios *);
124 int	umodem_ioctl(void *, int, u_long, caddr_t, int, struct proc *);
125 int	umodem_open(void *, int portno);
126 void	umodem_close(void *, int portno);
127 void	umodem_intr(struct usbd_xfer *, void *, usbd_status);
128 
129 struct ucom_methods umodem_methods = {
130 	umodem_get_status,
131 	umodem_set,
132 	umodem_param,
133 	umodem_ioctl,
134 	umodem_open,
135 	umodem_close,
136 	NULL,
137 	NULL,
138 };
139 
140 int umodem_match(struct device *, void *, void *);
141 void umodem_attach(struct device *, struct device *, void *);
142 int umodem_detach(struct device *, int);
143 
144 void umodem_get_caps(struct usb_attach_arg *, int, int *, int *, int *);
145 
146 struct cfdriver umodem_cd = {
147 	NULL, "umodem", DV_DULL
148 };
149 
150 const struct cfattach umodem_ca = {
151 	sizeof(struct umodem_softc), umodem_match, umodem_attach, umodem_detach
152 };
153 
154 void
155 umodem_get_caps(struct usb_attach_arg *uaa, int ctl_iface_no,
156     int *data_iface_idx, int *cm_cap, int *acm_cap)
157 {
158 	const usb_descriptor_t *desc;
159 	const usb_interface_descriptor_t *id;
160 	const struct usb_cdc_cm_descriptor *cmd;
161 	const struct usb_cdc_acm_descriptor *acmd;
162 	const struct usb_cdc_union_descriptor *uniond;
163 	struct usbd_desc_iter iter;
164 	int current_iface_no = -1;
165 	int data_iface_no = -1;
166 	int i;
167 
168 	*data_iface_idx = -1;
169 	*cm_cap = *acm_cap = 0;
170 	usbd_desc_iter_init(uaa->device, &iter);
171 	desc = usbd_desc_iter_next(&iter);
172 	while (desc) {
173 		if (desc->bDescriptorType == UDESC_INTERFACE) {
174 			id = (usb_interface_descriptor_t *)desc;
175 			current_iface_no = id->bInterfaceNumber;
176 			if (current_iface_no != ctl_iface_no &&
177 			    id->bInterfaceClass == UICLASS_CDC_DATA &&
178 			    id->bInterfaceSubClass == UISUBCLASS_DATA &&
179 			    data_iface_no == -1)
180 				data_iface_no = current_iface_no;
181 		}
182 		if (current_iface_no == ctl_iface_no &&
183 		    desc->bDescriptorType == UDESC_CS_INTERFACE) {
184 			switch(desc->bDescriptorSubtype) {
185 			case UDESCSUB_CDC_CM:
186 				cmd = (struct usb_cdc_cm_descriptor *)desc;
187 				*cm_cap = cmd->bmCapabilities;
188 				data_iface_no = cmd->bDataInterface;
189 				break;
190 			case UDESCSUB_CDC_ACM:
191 				acmd = (struct usb_cdc_acm_descriptor *)desc;
192 				*acm_cap = acmd->bmCapabilities;
193 				break;
194 			case UDESCSUB_CDC_UNION:
195 				uniond =
196 				    (struct usb_cdc_union_descriptor *)desc;
197 				data_iface_no = uniond->bSlaveInterface[0];
198 				break;
199 			}
200 		}
201 		desc = usbd_desc_iter_next(&iter);
202 	}
203 
204 	/*
205 	 * If we got a data interface number, make sure the corresponding
206 	 * interface exists and is not already claimed.
207 	 */
208 	if (data_iface_no != -1) {
209 		for (i = 0; i < uaa->nifaces; i++) {
210 			id = usbd_get_interface_descriptor(uaa->ifaces[i]);
211 
212 			if (id == NULL)
213 				continue;
214 
215 			if (id->bInterfaceNumber == data_iface_no) {
216 				if (!usbd_iface_claimed(uaa->device, i))
217 					*data_iface_idx = i;
218 				break;
219 			}
220 		}
221 	}
222 }
223 
224 int
225 umodem_match(struct device *parent, void *match, void *aux)
226 {
227 	struct usb_attach_arg *uaa = aux;
228 	usb_interface_descriptor_t *id;
229 	usb_device_descriptor_t *dd;
230 	int data_iface_idx, cm_cap, acm_cap, ret = UMATCH_NONE;
231 
232 	if (uaa->iface == NULL)
233 		return (ret);
234 
235 	id = usbd_get_interface_descriptor(uaa->iface);
236 	dd = usbd_get_device_descriptor(uaa->device);
237 	if (id == NULL || dd == NULL)
238 		return (ret);
239 
240 	ret = UMATCH_NONE;
241 
242 	/* protocol of 0 so won't match the test below */
243 	if (UGETW(dd->idVendor) == USB_VENDOR_ATMEL &&
244 	    UGETW(dd->idProduct) == USB_PRODUCT_ATMEL_AT91_CDC_ACM)
245 		ret = UMATCH_VENDOR_PRODUCT;
246 
247 	if (UGETW(dd->idVendor) == USB_VENDOR_KYOCERA &&
248 	    UGETW(dd->idProduct) == USB_PRODUCT_KYOCERA_AHK3001V &&
249 	    id->bInterfaceNumber == 0)
250 		ret = UMATCH_VENDOR_PRODUCT;
251 
252 	if (UGETW(dd->idVendor) == USB_VENDOR_ARDUINO &&
253 	    UGETW(dd->idProduct) == USB_PRODUCT_ARDUINO_LEONARDO)
254 	    ret = UMATCH_VENDOR_PRODUCT;
255 
256 	if (ret == UMATCH_NONE &&
257 	    id->bInterfaceClass == UICLASS_CDC &&
258 	    id->bInterfaceSubClass == UISUBCLASS_ABSTRACT_CONTROL_MODEL &&
259 	    id->bInterfaceProtocol == UIPROTO_CDC_AT)
260 		ret = UMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO;
261 
262 	if (ret == UMATCH_NONE)
263 		return (ret);
264 
265 	/* umodem doesn't support devices without a data iface */
266 	umodem_get_caps(uaa, id->bInterfaceNumber, &data_iface_idx,
267 	    &cm_cap, &acm_cap);
268 	if (data_iface_idx == -1)
269 		ret = UMATCH_NONE;
270 
271 	return (ret);
272 }
273 
274 void
275 umodem_attach(struct device *parent, struct device *self, void *aux)
276 {
277 	struct umodem_softc *sc = (struct umodem_softc *)self;
278 	struct usb_attach_arg *uaa = aux;
279 	struct usbd_device *dev = uaa->device;
280 	usb_interface_descriptor_t *id;
281 	usb_endpoint_descriptor_t *ed;
282 	usbd_status err;
283 	int data_iface_idx = -1;
284 	int i;
285 	struct ucom_attach_args uca;
286 
287 	sc->sc_udev = dev;
288 	sc->sc_ctl_iface = uaa->iface;
289 
290 	id = usbd_get_interface_descriptor(sc->sc_ctl_iface);
291 	//printf("%s: iclass %d/%d\n", sc->sc_dev.dv_xname,
292 	//    id->bInterfaceClass, id->bInterfaceSubClass);
293 	sc->sc_ctl_iface_no = id->bInterfaceNumber;
294 
295 	/* Get the capabilities. */
296 	umodem_get_caps(uaa, id->bInterfaceNumber, &data_iface_idx,
297 	    &sc->sc_cm_cap, &sc->sc_acm_cap);
298 
299 	usbd_claim_iface(sc->sc_udev, data_iface_idx);
300 	sc->sc_data_iface = uaa->ifaces[data_iface_idx];
301 	id = usbd_get_interface_descriptor(sc->sc_data_iface);
302 
303 	printf("%s: data interface %d, has %sCM over data, has %sbreak\n",
304 	       sc->sc_dev.dv_xname, id->bInterfaceNumber,
305 	       sc->sc_cm_cap & USB_CDC_CM_OVER_DATA ? "" : "no ",
306 	       sc->sc_acm_cap & USB_CDC_ACM_HAS_BREAK ? "" : "no ");
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 	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(("umodem_ioctl: 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(("umodem_ioctl: 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_dtr: 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_rts: 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_detach(struct device *self, int flags)
743 {
744 	struct umodem_softc *sc = (struct umodem_softc *)self;
745 	int rv = 0;
746 
747 	DPRINTF(("umodem_detach: sc=%p flags=%d\n", sc, flags));
748 
749 	if (sc->sc_notify_pipe != NULL) {
750 		usbd_abort_pipe(sc->sc_notify_pipe);
751 		usbd_close_pipe(sc->sc_notify_pipe);
752 		sc->sc_notify_pipe = NULL;
753 	}
754 
755 	if (sc->sc_subdev != NULL)
756 		rv = config_detach(sc->sc_subdev, flags);
757 
758 	return (rv);
759 }
760