xref: /openbsd-src/sys/dev/usb/umodem.c (revision b047b92c592efa9f707e9c00b25c78beb029b32a)
1 /*	$OpenBSD: umodem.c,v 1.47 2013/03/28 03:58:03 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/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 	usbd_device_handle	 sc_udev;	/* USB device */
89 
90 	int			 sc_ctl_iface_no;
91 	usbd_interface_handle	 sc_ctl_iface;	/* control interface */
92 	usbd_interface_handle	 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 	usb_cdc_line_state_t	 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 	u_char			 sc_dying;	/* disconnecting */
107 
108 	int			 sc_ctl_notify;	/* Notification endpoint */
109 	usbd_pipe_handle	 sc_notify_pipe; /* Notification pipe */
110 	usb_cdc_notification_t	 sc_notify_buf;	/* Notification structure */
111 	u_char			 sc_lsr;	/* Local status register */
112 	u_char			 sc_msr;	/* Modem status register */
113 };
114 
115 usbd_status umodem_set_comm_feature(struct umodem_softc *sc,
116 					   int feature, int state);
117 usbd_status umodem_set_line_coding(struct umodem_softc *sc,
118 					  usb_cdc_line_state_t *state);
119 
120 void	umodem_get_status(void *, int portno, u_char *lsr, u_char *msr);
121 void	umodem_set(void *, int, int, int);
122 void	umodem_dtr(struct umodem_softc *, int);
123 void	umodem_rts(struct umodem_softc *, int);
124 void	umodem_break(struct umodem_softc *, int);
125 void	umodem_set_line_state(struct umodem_softc *);
126 int	umodem_param(void *, int, struct termios *);
127 int	umodem_ioctl(void *, int, u_long, caddr_t, int, struct proc *);
128 int	umodem_open(void *, int portno);
129 void	umodem_close(void *, int portno);
130 void	umodem_intr(usbd_xfer_handle, usbd_private_handle, usbd_status);
131 
132 struct ucom_methods umodem_methods = {
133 	umodem_get_status,
134 	umodem_set,
135 	umodem_param,
136 	umodem_ioctl,
137 	umodem_open,
138 	umodem_close,
139 	NULL,
140 	NULL,
141 };
142 
143 int umodem_match(struct device *, void *, void *);
144 void umodem_attach(struct device *, struct device *, void *);
145 int umodem_detach(struct device *, int);
146 int umodem_activate(struct device *, int);
147 
148 void umodem_get_caps(struct usb_attach_arg *, int, int *, int *, int *);
149 
150 struct cfdriver umodem_cd = {
151 	NULL, "umodem", DV_DULL
152 };
153 
154 const struct cfattach umodem_ca = {
155 	sizeof(struct umodem_softc),
156 	umodem_match,
157 	umodem_attach,
158 	umodem_detach,
159 	umodem_activate,
160 };
161 
162 void
163 umodem_get_caps(struct usb_attach_arg *uaa, int ctl_iface_no,
164     int *data_iface_no, int *cm_cap, int *acm_cap)
165 {
166 	const usb_descriptor_t *desc;
167 	const usb_interface_descriptor_t *id;
168 	const usb_cdc_cm_descriptor_t *cmd;
169 	const usb_cdc_acm_descriptor_t *acmd;
170 	const usb_cdc_union_descriptor_t *uniond;
171 	usbd_desc_iter_t iter;
172 	int current_iface_no = -1;
173 
174 	*data_iface_no = -1;
175 	*cm_cap = *acm_cap = 0;
176 	usb_desc_iter_init(uaa->device, &iter);
177 	desc = usb_desc_iter_next(&iter);
178 	while (desc) {
179 		if (desc->bDescriptorType == UDESC_INTERFACE) {
180 			id = (usb_interface_descriptor_t *)desc;
181 			current_iface_no = id->bInterfaceNumber;
182 			if (current_iface_no != ctl_iface_no &&
183 			    id->bInterfaceClass == UICLASS_CDC_DATA &&
184 			    id->bInterfaceSubClass == UISUBCLASS_DATA &&
185 			    *data_iface_no == -1)
186 				*data_iface_no = current_iface_no;
187 		}
188 		if (current_iface_no == ctl_iface_no &&
189 		    desc->bDescriptorType == UDESC_CS_INTERFACE) {
190 			switch(desc->bDescriptorSubtype) {
191 			case UDESCSUB_CDC_CM:
192 				cmd = (usb_cdc_cm_descriptor_t *)desc;
193 				*cm_cap = cmd->bmCapabilities;
194 				*data_iface_no = cmd->bDataInterface;
195 				break;
196 			case UDESCSUB_CDC_ACM:
197 				acmd = (usb_cdc_acm_descriptor_t *)desc;
198 				*acm_cap = acmd->bmCapabilities;
199 				break;
200 			case UDESCSUB_CDC_UNION:
201 				uniond = (usb_cdc_union_descriptor_t *)desc;
202 				*data_iface_no = uniond->bSlaveInterface[0];
203 				break;
204 			}
205 		}
206 		desc = usb_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 	usbd_device_handle 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 	sc->sc_dying = 1;
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 		err = usbd_abort_pipe(sc->sc_notify_pipe);
441 		if (err)
442 			printf("%s: abort notify pipe failed: %s\n",
443 			    sc->sc_dev.dv_xname, usbd_errstr(err));
444 		err = usbd_close_pipe(sc->sc_notify_pipe);
445 		if (err)
446 			printf("%s: close notify pipe failed: %s\n",
447 			    sc->sc_dev.dv_xname, usbd_errstr(err));
448 		sc->sc_notify_pipe = NULL;
449 	}
450 }
451 
452 void
453 umodem_intr(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
454 {
455 	struct umodem_softc *sc = priv;
456 	u_char mstatus;
457 
458 	if (sc->sc_dying)
459 		return;
460 
461 	if (status != USBD_NORMAL_COMPLETION) {
462 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
463 			return;
464 		DPRINTF(("%s: abnormal status: %s\n", sc->sc_dev.dv_xname,
465 		       usbd_errstr(status)));
466 		usbd_clear_endpoint_stall_async(sc->sc_notify_pipe);
467 		return;
468 	}
469 
470 	if (sc->sc_notify_buf.bmRequestType != UCDC_NOTIFICATION) {
471 		DPRINTF(("%s: unknown message type (%02x) on notify pipe\n",
472 			 sc->sc_dev.dv_xname,
473 			 sc->sc_notify_buf.bmRequestType));
474 		return;
475 	}
476 
477 	switch (sc->sc_notify_buf.bNotification) {
478 	case UCDC_N_SERIAL_STATE:
479 		/*
480 		 * Set the serial state in ucom driver based on
481 		 * the bits from the notify message
482 		 */
483 		if (UGETW(sc->sc_notify_buf.wLength) != 2) {
484 			printf("%s: Invalid notification length! (%d)\n",
485 			       sc->sc_dev.dv_xname,
486 			       UGETW(sc->sc_notify_buf.wLength));
487 			break;
488 		}
489 		DPRINTF(("%s: notify bytes = %02x%02x\n",
490 			 sc->sc_dev.dv_xname,
491 			 sc->sc_notify_buf.data[0],
492 			 sc->sc_notify_buf.data[1]));
493 		/* Currently, lsr is always zero. */
494 		sc->sc_lsr = sc->sc_msr = 0;
495 		mstatus = sc->sc_notify_buf.data[0];
496 
497 		if (ISSET(mstatus, UCDC_N_SERIAL_RI))
498 			sc->sc_msr |= UMSR_RI;
499 		if (ISSET(mstatus, UCDC_N_SERIAL_DSR))
500 			sc->sc_msr |= UMSR_DSR;
501 		if (ISSET(mstatus, UCDC_N_SERIAL_DCD))
502 			sc->sc_msr |= UMSR_DCD;
503 		ucom_status_change((struct ucom_softc *)sc->sc_subdev);
504 		break;
505 	default:
506 		DPRINTF(("%s: unknown notify message: %02x\n",
507 			 sc->sc_dev.dv_xname,
508 			 sc->sc_notify_buf.bNotification));
509 		break;
510 	}
511 }
512 
513 void
514 umodem_get_status(void *addr, int portno, u_char *lsr, u_char *msr)
515 {
516 	struct umodem_softc *sc = addr;
517 
518 	DPRINTF(("umodem_get_status:\n"));
519 
520 	if (lsr != NULL)
521 		*lsr = sc->sc_lsr;
522 	if (msr != NULL)
523 		*msr = sc->sc_msr;
524 }
525 
526 int
527 umodem_param(void *addr, int portno, struct termios *t)
528 {
529 	struct umodem_softc *sc = addr;
530 	usbd_status err;
531 	usb_cdc_line_state_t ls;
532 
533 	DPRINTF(("umodem_param: sc=%p\n", sc));
534 
535 	USETDW(ls.dwDTERate, t->c_ospeed);
536 	if (ISSET(t->c_cflag, CSTOPB))
537 		ls.bCharFormat = UCDC_STOP_BIT_2;
538 	else
539 		ls.bCharFormat = UCDC_STOP_BIT_1;
540 	if (ISSET(t->c_cflag, PARENB)) {
541 		if (ISSET(t->c_cflag, PARODD))
542 			ls.bParityType = UCDC_PARITY_ODD;
543 		else
544 			ls.bParityType = UCDC_PARITY_EVEN;
545 	} else
546 		ls.bParityType = UCDC_PARITY_NONE;
547 	switch (ISSET(t->c_cflag, CSIZE)) {
548 	case CS5:
549 		ls.bDataBits = 5;
550 		break;
551 	case CS6:
552 		ls.bDataBits = 6;
553 		break;
554 	case CS7:
555 		ls.bDataBits = 7;
556 		break;
557 	case CS8:
558 		ls.bDataBits = 8;
559 		break;
560 	}
561 
562 	err = umodem_set_line_coding(sc, &ls);
563 	if (err) {
564 		DPRINTF(("umodem_param: err=%s\n", usbd_errstr(err)));
565 		return (1);
566 	}
567 	return (0);
568 }
569 
570 int
571 umodem_ioctl(void *addr, int portno, u_long cmd, caddr_t data, int flag,
572 	     struct proc *p)
573 {
574 	struct umodem_softc *sc = addr;
575 	int error = 0;
576 
577 	if (sc->sc_dying)
578 		return (EIO);
579 
580 	DPRINTF(("umodemioctl: cmd=0x%08lx\n", cmd));
581 
582 	switch (cmd) {
583 	case USB_GET_CM_OVER_DATA:
584 		*(int *)data = sc->sc_cm_over_data;
585 		break;
586 
587 	case USB_SET_CM_OVER_DATA:
588 		if (*(int *)data != sc->sc_cm_over_data) {
589 			/* XXX change it */
590 		}
591 		break;
592 
593 	default:
594 		DPRINTF(("umodemioctl: unknown\n"));
595 		error = ENOTTY;
596 		break;
597 	}
598 
599 	return (error);
600 }
601 
602 void
603 umodem_dtr(struct umodem_softc *sc, int onoff)
604 {
605 	DPRINTF(("umodem_modem: onoff=%d\n", onoff));
606 
607 	if (sc->sc_dtr == onoff)
608 		return;
609 	sc->sc_dtr = onoff;
610 
611 	umodem_set_line_state(sc);
612 }
613 
614 void
615 umodem_rts(struct umodem_softc *sc, int onoff)
616 {
617 	DPRINTF(("umodem_modem: onoff=%d\n", onoff));
618 
619 	if (sc->sc_rts == onoff)
620 		return;
621 	sc->sc_rts = onoff;
622 
623 	umodem_set_line_state(sc);
624 }
625 
626 void
627 umodem_set_line_state(struct umodem_softc *sc)
628 {
629 	usb_device_request_t req;
630 	int ls;
631 
632 	ls = (sc->sc_dtr ? UCDC_LINE_DTR : 0) |
633 	     (sc->sc_rts ? UCDC_LINE_RTS : 0);
634 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
635 	req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
636 	USETW(req.wValue, ls);
637 	USETW(req.wIndex, sc->sc_ctl_iface_no);
638 	USETW(req.wLength, 0);
639 
640 	(void)usbd_do_request(sc->sc_udev, &req, 0);
641 
642 }
643 
644 void
645 umodem_break(struct umodem_softc *sc, int onoff)
646 {
647 	usb_device_request_t req;
648 
649 	DPRINTF(("umodem_break: onoff=%d\n", onoff));
650 
651 	if (!(sc->sc_acm_cap & USB_CDC_ACM_HAS_BREAK))
652 		return;
653 
654 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
655 	req.bRequest = UCDC_SEND_BREAK;
656 	USETW(req.wValue, onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF);
657 	USETW(req.wIndex, sc->sc_ctl_iface_no);
658 	USETW(req.wLength, 0);
659 
660 	(void)usbd_do_request(sc->sc_udev, &req, 0);
661 }
662 
663 void
664 umodem_set(void *addr, int portno, int reg, int onoff)
665 {
666 	struct umodem_softc *sc = addr;
667 
668 	switch (reg) {
669 	case UCOM_SET_DTR:
670 		umodem_dtr(sc, onoff);
671 		break;
672 	case UCOM_SET_RTS:
673 		umodem_rts(sc, onoff);
674 		break;
675 	case UCOM_SET_BREAK:
676 		umodem_break(sc, onoff);
677 		break;
678 	default:
679 		break;
680 	}
681 }
682 
683 usbd_status
684 umodem_set_line_coding(struct umodem_softc *sc, usb_cdc_line_state_t *state)
685 {
686 	usb_device_request_t req;
687 	usbd_status err;
688 
689 	DPRINTF(("umodem_set_line_coding: rate=%d fmt=%d parity=%d bits=%d\n",
690 		 UGETDW(state->dwDTERate), state->bCharFormat,
691 		 state->bParityType, state->bDataBits));
692 
693 	if (memcmp(state, &sc->sc_line_state, UCDC_LINE_STATE_LENGTH) == 0) {
694 		DPRINTF(("umodem_set_line_coding: already set\n"));
695 		return (USBD_NORMAL_COMPLETION);
696 	}
697 
698 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
699 	req.bRequest = UCDC_SET_LINE_CODING;
700 	USETW(req.wValue, 0);
701 	USETW(req.wIndex, sc->sc_ctl_iface_no);
702 	USETW(req.wLength, UCDC_LINE_STATE_LENGTH);
703 
704 	err = usbd_do_request(sc->sc_udev, &req, state);
705 	if (err) {
706 		DPRINTF(("umodem_set_line_coding: failed, err=%s\n",
707 			 usbd_errstr(err)));
708 		return (err);
709 	}
710 
711 	sc->sc_line_state = *state;
712 
713 	return (USBD_NORMAL_COMPLETION);
714 }
715 
716 usbd_status
717 umodem_set_comm_feature(struct umodem_softc *sc, int feature, int state)
718 {
719 	usb_device_request_t req;
720 	usbd_status err;
721 	usb_cdc_abstract_state_t ast;
722 
723 	DPRINTF(("umodem_set_comm_feature: feature=%d state=%d\n", feature,
724 		 state));
725 
726 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
727 	req.bRequest = UCDC_SET_COMM_FEATURE;
728 	USETW(req.wValue, feature);
729 	USETW(req.wIndex, sc->sc_ctl_iface_no);
730 	USETW(req.wLength, UCDC_ABSTRACT_STATE_LENGTH);
731 	USETW(ast.wState, state);
732 
733 	err = usbd_do_request(sc->sc_udev, &req, &ast);
734 	if (err) {
735 		DPRINTF(("umodem_set_comm_feature: feature=%d, err=%s\n",
736 			 feature, usbd_errstr(err)));
737 		return (err);
738 	}
739 
740 	return (USBD_NORMAL_COMPLETION);
741 }
742 
743 int
744 umodem_activate(struct device *self, int act)
745 {
746 	struct umodem_softc *sc = (struct umodem_softc *)self;
747 	int rv = 0;
748 
749 	switch (act) {
750 	case DVACT_DEACTIVATE:
751 		sc->sc_dying = 1;
752 		if (sc->sc_subdev)
753 			rv = config_deactivate(sc->sc_subdev);
754 		break;
755 	}
756 	return (rv);
757 }
758 
759 int
760 umodem_detach(struct device *self, int flags)
761 {
762 	struct umodem_softc *sc = (struct umodem_softc *)self;
763 	int rv = 0;
764 
765 	DPRINTF(("umodem_detach: sc=%p flags=%d\n", sc, flags));
766 
767 	if (sc->sc_subdev != NULL)
768 		rv = config_detach(sc->sc_subdev, flags);
769 
770 	return (rv);
771 }
772