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