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