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