1 /*	$OpenBSD: umodem.c,v 1.19 2005/09/28 00:20:12 dlg 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  * 3. All advertising materials mentioning features or use of this software
21  *    must display the following acknowledgement:
22  *        This product includes software developed by the NetBSD
23  *        Foundation, Inc. and its contributors.
24  * 4. Neither the name of The NetBSD Foundation nor the names of its
25  *    contributors may be used to endorse or promote products derived
26  *    from this software without specific prior written permission.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
29  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
31  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
32  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38  * POSSIBILITY OF SUCH DAMAGE.
39  */
40 
41 /*
42  * Comm Class spec:  http://www.usb.org/developers/devclass_docs/usbccs10.pdf
43  *                   http://www.usb.org/developers/devclass_docs/usbcdc11.pdf
44  */
45 
46 /*
47  * TODO:
48  * - Add error recovery in various places; the big problem is what
49  *   to do in a callback if there is an error.
50  * - Implement a Call Device for modems without multiplexed commands.
51  *
52  */
53 
54 #include <sys/param.h>
55 #include <sys/systm.h>
56 #include <sys/kernel.h>
57 #include <sys/ioctl.h>
58 #include <sys/conf.h>
59 #include <sys/tty.h>
60 #include <sys/file.h>
61 #include <sys/select.h>
62 #include <sys/proc.h>
63 #include <sys/vnode.h>
64 #include <sys/device.h>
65 #include <sys/poll.h>
66 
67 #include <dev/usb/usb.h>
68 #include <dev/usb/usbcdc.h>
69 
70 #include <dev/usb/usbdi.h>
71 #include <dev/usb/usbdi_util.h>
72 #include <dev/usb/usbdevs.h>
73 #include <dev/usb/usb_quirks.h>
74 
75 #include <dev/usb/usbdevs.h>
76 #include <dev/usb/ucomvar.h>
77 
78 #ifdef UMODEM_DEBUG
79 #define DPRINTFN(n, x)	do { if (umodemdebug > (n)) logprintf x; } while (0)
80 int	umodemdebug = 0;
81 #else
82 #define DPRINTFN(n, x)
83 #endif
84 #define DPRINTF(x) DPRINTFN(0, x)
85 
86 /*
87  * These are the maximum number of bytes transferred per frame.
88  * If some really high speed devices should use this driver they
89  * may need to be increased, but this is good enough for normal modems.
90  */
91 #define UMODEMIBUFSIZE 64
92 #define UMODEMOBUFSIZE 256
93 
94 struct umodem_softc {
95 	USBBASEDEVICE		sc_dev;		/* base device */
96 
97 	usbd_device_handle	sc_udev;	/* USB device */
98 
99 	int			sc_ctl_iface_no;
100 	usbd_interface_handle	sc_ctl_iface;	/* control interface */
101 	int			sc_data_iface_no;
102 	usbd_interface_handle	sc_data_iface;	/* data interface */
103 
104 	int			sc_cm_cap;	/* CM capabilities */
105 	int			sc_acm_cap;	/* ACM capabilities */
106 
107 	int			sc_cm_over_data;
108 
109 	usb_cdc_line_state_t	sc_line_state;	/* current line state */
110 	u_char			sc_dtr;		/* current DTR state */
111 	u_char			sc_rts;		/* current RTS state */
112 
113 	device_ptr_t		sc_subdev;	/* ucom device */
114 
115 	u_char			sc_opening;	/* lock during open */
116 	u_char			sc_dying;	/* disconnecting */
117 
118 	int			sc_ctl_notify;	/* Notification endpoint */
119 	usbd_pipe_handle	sc_notify_pipe; /* Notification pipe */
120 	usb_cdc_notification_t	sc_notify_buf;	/* Notification structure */
121 	u_char			sc_lsr;		/* Local status register */
122 	u_char			sc_msr;		/* Modem status register */
123 };
124 
125 Static usbd_status umodem_set_comm_feature(struct umodem_softc *sc,
126 					   int feature, int state);
127 Static usbd_status umodem_set_line_coding(struct umodem_softc *sc,
128 					  usb_cdc_line_state_t *state);
129 
130 Static void	umodem_get_status(void *, int portno, u_char *lsr, u_char *msr);
131 Static void	umodem_set(void *, int, int, int);
132 Static void	umodem_dtr(struct umodem_softc *, int);
133 Static void	umodem_rts(struct umodem_softc *, int);
134 Static void	umodem_break(struct umodem_softc *, int);
135 Static void	umodem_set_line_state(struct umodem_softc *);
136 Static int	umodem_param(void *, int, struct termios *);
137 Static int	umodem_ioctl(void *, int, u_long, caddr_t, int, usb_proc_ptr);
138 Static int	umodem_open(void *, int portno);
139 Static void	umodem_close(void *, int portno);
140 Static void	umodem_intr(usbd_xfer_handle, usbd_private_handle, usbd_status);
141 
142 Static struct ucom_methods umodem_methods = {
143 	umodem_get_status,
144 	umodem_set,
145 	umodem_param,
146 	umodem_ioctl,
147 	umodem_open,
148 	umodem_close,
149 	NULL,
150 	NULL,
151 };
152 
153 USB_DECLARE_DRIVER(umodem);
154 
USB_MATCH(umodem)155 USB_MATCH(umodem)
156 {
157 	USB_MATCH_START(umodem, uaa);
158 	usb_interface_descriptor_t *id;
159 	usb_device_descriptor_t *dd;
160 	int ret;
161 
162 	if (uaa->iface == NULL)
163 		return (UMATCH_NONE);
164 
165 	id = usbd_get_interface_descriptor(uaa->iface);
166 	dd = usbd_get_device_descriptor(uaa->device);
167 	if (id == NULL || dd == NULL)
168 		return (UMATCH_NONE);
169 
170 	ret = UMATCH_NONE;
171 	if (UGETW(dd->idVendor) == USB_VENDOR_KYOCERA &&
172 	    UGETW(dd->idProduct) == USB_PRODUCT_KYOCERA_AHK3001V &&
173 	    id->bInterfaceNumber == 0)
174 		ret = UMATCH_VENDOR_PRODUCT;
175 
176 	if (ret == UMATCH_NONE &&
177 	    id->bInterfaceClass == UICLASS_CDC &&
178 	    id->bInterfaceSubClass == UISUBCLASS_ABSTRACT_CONTROL_MODEL &&
179 	    id->bInterfaceProtocol == UIPROTO_CDC_AT)
180 		ret = UMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO;
181 
182 	return (ret);
183 }
184 
USB_ATTACH(umodem)185 USB_ATTACH(umodem)
186 {
187 	USB_ATTACH_START(umodem, sc, uaa);
188 	usbd_device_handle dev = uaa->device;
189 	usb_interface_descriptor_t *id;
190 	usb_endpoint_descriptor_t *ed;
191 	usb_cdc_cm_descriptor_t *cmd;
192 	usb_interface_descriptor_t *idesc;
193 	const usb_cdc_acm_descriptor_t *acmd;
194 	const usb_cdc_union_descriptor_t *uniond;
195 	const usb_descriptor_t *desc;
196 	usbd_desc_iter_t iter;
197 	char devinfo[1024];
198 	usbd_status err;
199 	int current_iface_no = -1;
200 	int i;
201 	struct ucom_attach_args uca;
202 
203 	usbd_devinfo(uaa->device, 0, devinfo, sizeof devinfo);
204 	USB_ATTACH_SETUP;
205 
206 	sc->sc_udev = dev;
207 	sc->sc_ctl_iface = uaa->iface;
208 
209 	id = usbd_get_interface_descriptor(sc->sc_ctl_iface);
210 	printf("%s: %s, iclass %d/%d\n", USBDEVNAME(sc->sc_dev),
211 	       devinfo, id->bInterfaceClass, id->bInterfaceSubClass);
212 	sc->sc_ctl_iface_no = id->bInterfaceNumber;
213 
214 	/* Get the data interface no. and capabilities */
215 	sc->sc_cm_cap = 0;
216 	sc->sc_data_iface_no = 0;
217 	sc->sc_acm_cap = 0;
218 	usb_desc_iter_init(dev, &iter);
219 	desc = usb_desc_iter_next(&iter);
220 	while (desc) {
221 		if (desc->bDescriptorType == UDESC_INTERFACE) {
222 		    idesc = (usb_interface_descriptor_t *)desc;
223 		    current_iface_no = idesc->bInterfaceNumber;
224 		}
225 		if (current_iface_no == sc->sc_ctl_iface_no &&
226 		    desc->bDescriptorType == UDESC_CS_INTERFACE) {
227 			switch(desc->bDescriptorSubtype) {
228 			case UDESCSUB_CDC_CM:
229 				cmd = (usb_cdc_cm_descriptor_t *)desc;
230 				sc->sc_cm_cap = cmd->bmCapabilities;
231 				sc->sc_data_iface_no = cmd->bDataInterface;
232 			break;
233 			case UDESCSUB_CDC_ACM:
234 				acmd = (usb_cdc_acm_descriptor_t *)desc;
235 				sc->sc_acm_cap = acmd->bmCapabilities;
236 			break;
237 			case UDESCSUB_CDC_UNION:
238 				uniond = (usb_cdc_union_descriptor_t *)desc;
239 				sc->sc_data_iface_no =
240 				    uniond->bSlaveInterface[0];
241 			break;
242 			}
243 		}
244 		desc = usb_desc_iter_next(&iter);
245 	}
246 
247 	if (sc->sc_data_iface_no == 0) {
248 		printf("%s: no pointer to data interface\n",
249 		       USBDEVNAME(sc->sc_dev));
250 		goto bad;
251 	}
252 
253 	printf("%s: data interface %d, has %sCM over data, has %sbreak\n",
254 	       USBDEVNAME(sc->sc_dev), sc->sc_data_iface_no,
255 	       sc->sc_cm_cap & USB_CDC_CM_OVER_DATA ? "" : "no ",
256 	       sc->sc_acm_cap & USB_CDC_ACM_HAS_BREAK ? "" : "no ");
257 
258 	/* Get the data interface too. */
259 	for (i = 0; i < uaa->nifaces; i++) {
260 		if (uaa->ifaces[i] != NULL) {
261 			id = usbd_get_interface_descriptor(uaa->ifaces[i]);
262 			if (id != NULL &&
263                             id->bInterfaceNumber == sc->sc_data_iface_no) {
264 				sc->sc_data_iface = uaa->ifaces[i];
265 				uaa->ifaces[i] = NULL;
266 			}
267 		}
268 	}
269 	if (sc->sc_data_iface == NULL) {
270 		printf("%s: no data interface\n", USBDEVNAME(sc->sc_dev));
271 		goto bad;
272 	}
273 
274 	/*
275 	 * Find the bulk endpoints.
276 	 * Iterate over all endpoints in the data interface and take note.
277 	 */
278 	uca.bulkin = uca.bulkout = -1;
279 
280 	id = usbd_get_interface_descriptor(sc->sc_data_iface);
281 	for (i = 0; i < id->bNumEndpoints; i++) {
282 		ed = usbd_interface2endpoint_descriptor(sc->sc_data_iface, i);
283 		if (ed == NULL) {
284 			printf("%s: no endpoint descriptor for %d\n",
285 				USBDEVNAME(sc->sc_dev), i);
286 			goto bad;
287 		}
288 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
289 		    (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
290                         uca.bulkin = ed->bEndpointAddress;
291                 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
292 			   (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
293                         uca.bulkout = ed->bEndpointAddress;
294                 }
295         }
296 
297 	if (uca.bulkin == -1) {
298 		printf("%s: Could not find data bulk in\n",
299 		       USBDEVNAME(sc->sc_dev));
300 		goto bad;
301 	}
302 	if (uca.bulkout == -1) {
303 		printf("%s: Could not find data bulk out\n",
304 			USBDEVNAME(sc->sc_dev));
305 		goto bad;
306 	}
307 
308 	if (usbd_get_quirks(sc->sc_udev)->uq_flags & UQ_ASSUME_CM_OVER_DATA) {
309 		sc->sc_cm_over_data = 1;
310 	} else {
311 		if (sc->sc_cm_cap & USB_CDC_CM_OVER_DATA) {
312 			if (sc->sc_acm_cap & USB_CDC_ACM_HAS_FEATURE)
313 				err = umodem_set_comm_feature(sc,
314 				    UCDC_ABSTRACT_STATE, UCDC_DATA_MULTIPLEXED);
315 			else
316 				err = 0;
317 			if (err) {
318 				printf("%s: could not set data multiplex mode\n",
319 				       USBDEVNAME(sc->sc_dev));
320 				goto bad;
321 			}
322 			sc->sc_cm_over_data = 1;
323 		}
324 	}
325 
326 	/*
327 	 * The standard allows for notification messages (to indicate things
328 	 * like a modem hangup) to come in via an interrupt endpoint
329 	 * off of the control interface.  Iterate over the endpoints on
330 	 * the control interface and see if there are any interrupt
331 	 * endpoints; if there are, then register it.
332 	 */
333 
334 	sc->sc_ctl_notify = -1;
335 	sc->sc_notify_pipe = NULL;
336 
337 	for (i = 0; i < id->bNumEndpoints; i++) {
338 		ed = usbd_interface2endpoint_descriptor(sc->sc_ctl_iface, i);
339 		if (ed == NULL)
340 			continue;
341 
342 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
343 		    (ed->bmAttributes & UE_XFERTYPE) == UE_INTERRUPT) {
344 			printf("%s: status change notification available\n",
345 			       USBDEVNAME(sc->sc_dev));
346 			sc->sc_ctl_notify = ed->bEndpointAddress;
347 		}
348 	}
349 
350 	sc->sc_dtr = -1;
351 
352 	uca.portno = UCOM_UNK_PORTNO;
353 	/* bulkin, bulkout set above */
354 	uca.ibufsize = UMODEMIBUFSIZE;
355 	uca.obufsize = UMODEMOBUFSIZE;
356 	uca.ibufsizepad = UMODEMIBUFSIZE;
357 	uca.opkthdrlen = 0;
358 	uca.device = sc->sc_udev;
359 	uca.iface = sc->sc_data_iface;
360 	uca.methods = &umodem_methods;
361 	uca.arg = sc;
362 	uca.info = NULL;
363 
364 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
365 			   USBDEV(sc->sc_dev));
366 
367 	DPRINTF(("umodem_attach: sc=%p\n", sc));
368 	sc->sc_subdev = config_found_sm(self, &uca, ucomprint, ucomsubmatch);
369 
370 	USB_ATTACH_SUCCESS_RETURN;
371 
372  bad:
373 	sc->sc_dying = 1;
374 	USB_ATTACH_ERROR_RETURN;
375 }
376 
377 Static int
umodem_open(void * addr,int portno)378 umodem_open(void *addr, int portno)
379 {
380 	struct umodem_softc *sc = addr;
381 	int err;
382 
383 	DPRINTF(("umodem_open: sc=%p\n", sc));
384 
385 	if (sc->sc_ctl_notify != -1 && sc->sc_notify_pipe == NULL) {
386 		err = usbd_open_pipe_intr(sc->sc_ctl_iface, sc->sc_ctl_notify,
387 		    USBD_SHORT_XFER_OK, &sc->sc_notify_pipe, sc,
388 		    &sc->sc_notify_buf, sizeof(sc->sc_notify_buf),
389 		    umodem_intr, USBD_DEFAULT_INTERVAL);
390 
391 		if (err) {
392 			DPRINTF(("Failed to establish notify pipe: %s\n",
393 				usbd_errstr(err)));
394 			return EIO;
395 		}
396 	}
397 
398 	return 0;
399 }
400 
401 Static void
umodem_close(void * addr,int portno)402 umodem_close(void *addr, int portno)
403 {
404 	struct umodem_softc *sc = addr;
405 	int err;
406 
407 	DPRINTF(("umodem_close: sc=%p\n", sc));
408 
409 	if (sc->sc_notify_pipe != NULL) {
410 		err = usbd_abort_pipe(sc->sc_notify_pipe);
411 		if (err)
412 			printf("%s: abort notify pipe failed: %s\n",
413 			    USBDEVNAME(sc->sc_dev), usbd_errstr(err));
414 		err = usbd_close_pipe(sc->sc_notify_pipe);
415 		if (err)
416 			printf("%s: close notify pipe failed: %s\n",
417 			    USBDEVNAME(sc->sc_dev), usbd_errstr(err));
418 		sc->sc_notify_pipe = NULL;
419 	}
420 }
421 
422 Static void
umodem_intr(usbd_xfer_handle xfer,usbd_private_handle priv,usbd_status status)423 umodem_intr(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
424 {
425 	struct umodem_softc *sc = priv;
426 	u_char mstatus;
427 
428 	if (sc->sc_dying)
429 		return;
430 
431 	if (status != USBD_NORMAL_COMPLETION) {
432 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
433 			return;
434 		printf("%s: abnormal status: %s\n", USBDEVNAME(sc->sc_dev),
435 		       usbd_errstr(status));
436 		return;
437 	}
438 
439 	if (sc->sc_notify_buf.bmRequestType != UCDC_NOTIFICATION) {
440 		DPRINTF(("%s: unknown message type (%02x) on notify pipe\n",
441 			 USBDEVNAME(sc->sc_dev),
442 			 sc->sc_notify_buf.bmRequestType));
443 		return;
444 	}
445 
446 	switch (sc->sc_notify_buf.bNotification) {
447 	case UCDC_N_SERIAL_STATE:
448 		/*
449 		 * Set the serial state in ucom driver based on
450 		 * the bits from the notify message
451 		 */
452 		if (UGETW(sc->sc_notify_buf.wLength) != 2) {
453 			printf("%s: Invalid notification length! (%d)\n",
454 			       USBDEVNAME(sc->sc_dev),
455 			       UGETW(sc->sc_notify_buf.wLength));
456 			break;
457 		}
458 		DPRINTF(("%s: notify bytes = %02x%02x\n",
459 			 USBDEVNAME(sc->sc_dev),
460 			 sc->sc_notify_buf.data[0],
461 			 sc->sc_notify_buf.data[1]));
462 		/* Currently, lsr is always zero. */
463 		sc->sc_lsr = sc->sc_msr = 0;
464 		mstatus = sc->sc_notify_buf.data[0];
465 
466 		if (ISSET(mstatus, UCDC_N_SERIAL_RI))
467 			sc->sc_msr |= UMSR_RI;
468 		if (ISSET(mstatus, UCDC_N_SERIAL_DSR))
469 			sc->sc_msr |= UMSR_DSR;
470 		if (ISSET(mstatus, UCDC_N_SERIAL_DCD))
471 			sc->sc_msr |= UMSR_DCD;
472 		ucom_status_change((struct ucom_softc *)sc->sc_subdev);
473 		break;
474 	default:
475 		DPRINTF(("%s: unknown notify message: %02x\n",
476 			 USBDEVNAME(sc->sc_dev),
477 			 sc->sc_notify_buf.bNotification));
478 		break;
479 	}
480 }
481 
482 void
umodem_get_status(void * addr,int portno,u_char * lsr,u_char * msr)483 umodem_get_status(void *addr, int portno, u_char *lsr, u_char *msr)
484 {
485 	struct umodem_softc *sc = addr;
486 
487 	DPRINTF(("umodem_get_status:\n"));
488 
489 	if (lsr != NULL)
490 		*lsr = sc->sc_lsr;
491 	if (msr != NULL)
492 		*msr = sc->sc_msr;
493 }
494 
495 int
umodem_param(void * addr,int portno,struct termios * t)496 umodem_param(void *addr, int portno, struct termios *t)
497 {
498 	struct umodem_softc *sc = addr;
499 	usbd_status err;
500 	usb_cdc_line_state_t ls;
501 
502 	DPRINTF(("umodem_param: sc=%p\n", sc));
503 
504 	USETDW(ls.dwDTERate, t->c_ospeed);
505 	if (ISSET(t->c_cflag, CSTOPB))
506 		ls.bCharFormat = UCDC_STOP_BIT_2;
507 	else
508 		ls.bCharFormat = UCDC_STOP_BIT_1;
509 	if (ISSET(t->c_cflag, PARENB)) {
510 		if (ISSET(t->c_cflag, PARODD))
511 			ls.bParityType = UCDC_PARITY_ODD;
512 		else
513 			ls.bParityType = UCDC_PARITY_EVEN;
514 	} else
515 		ls.bParityType = UCDC_PARITY_NONE;
516 	switch (ISSET(t->c_cflag, CSIZE)) {
517 	case CS5:
518 		ls.bDataBits = 5;
519 		break;
520 	case CS6:
521 		ls.bDataBits = 6;
522 		break;
523 	case CS7:
524 		ls.bDataBits = 7;
525 		break;
526 	case CS8:
527 		ls.bDataBits = 8;
528 		break;
529 	}
530 
531 	err = umodem_set_line_coding(sc, &ls);
532 	if (err) {
533 		DPRINTF(("umodem_param: err=%s\n", usbd_errstr(err)));
534 		return (1);
535 	}
536 	return (0);
537 }
538 
539 int
umodem_ioctl(void * addr,int portno,u_long cmd,caddr_t data,int flag,usb_proc_ptr p)540 umodem_ioctl(void *addr, int portno, u_long cmd, caddr_t data, int flag,
541 	     usb_proc_ptr p)
542 {
543 	struct umodem_softc *sc = addr;
544 	int error = 0;
545 
546 	if (sc->sc_dying)
547 		return (EIO);
548 
549 	DPRINTF(("umodemioctl: cmd=0x%08lx\n", cmd));
550 
551 	switch (cmd) {
552 	case USB_GET_CM_OVER_DATA:
553 		*(int *)data = sc->sc_cm_over_data;
554 		break;
555 
556 	case USB_SET_CM_OVER_DATA:
557 		if (*(int *)data != sc->sc_cm_over_data) {
558 			/* XXX change it */
559 		}
560 		break;
561 
562 	default:
563 		DPRINTF(("umodemioctl: unknown\n"));
564 		error = ENOTTY;
565 		break;
566 	}
567 
568 	return (error);
569 }
570 
571 void
umodem_dtr(struct umodem_softc * sc,int onoff)572 umodem_dtr(struct umodem_softc *sc, int onoff)
573 {
574 	DPRINTF(("umodem_modem: onoff=%d\n", onoff));
575 
576 	if (sc->sc_dtr == onoff)
577 		return;
578 	sc->sc_dtr = onoff;
579 
580 	umodem_set_line_state(sc);
581 }
582 
583 void
umodem_rts(struct umodem_softc * sc,int onoff)584 umodem_rts(struct umodem_softc *sc, int onoff)
585 {
586 	DPRINTF(("umodem_modem: onoff=%d\n", onoff));
587 
588 	if (sc->sc_rts == onoff)
589 		return;
590 	sc->sc_rts = onoff;
591 
592 	umodem_set_line_state(sc);
593 }
594 
595 void
umodem_set_line_state(struct umodem_softc * sc)596 umodem_set_line_state(struct umodem_softc *sc)
597 {
598 	usb_device_request_t req;
599 	int ls;
600 
601 	ls = (sc->sc_dtr ? UCDC_LINE_DTR : 0) |
602 	     (sc->sc_rts ? UCDC_LINE_RTS : 0);
603 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
604 	req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
605 	USETW(req.wValue, ls);
606 	USETW(req.wIndex, sc->sc_ctl_iface_no);
607 	USETW(req.wLength, 0);
608 
609 	(void)usbd_do_request(sc->sc_udev, &req, 0);
610 
611 }
612 
613 void
umodem_break(struct umodem_softc * sc,int onoff)614 umodem_break(struct umodem_softc *sc, int onoff)
615 {
616 	usb_device_request_t req;
617 
618 	DPRINTF(("umodem_break: onoff=%d\n", onoff));
619 
620 	if (!(sc->sc_acm_cap & USB_CDC_ACM_HAS_BREAK))
621 		return;
622 
623 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
624 	req.bRequest = UCDC_SEND_BREAK;
625 	USETW(req.wValue, onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF);
626 	USETW(req.wIndex, sc->sc_ctl_iface_no);
627 	USETW(req.wLength, 0);
628 
629 	(void)usbd_do_request(sc->sc_udev, &req, 0);
630 }
631 
632 void
umodem_set(void * addr,int portno,int reg,int onoff)633 umodem_set(void *addr, int portno, int reg, int onoff)
634 {
635 	struct umodem_softc *sc = addr;
636 
637 	switch (reg) {
638 	case UCOM_SET_DTR:
639 		umodem_dtr(sc, onoff);
640 		break;
641 	case UCOM_SET_RTS:
642 		umodem_rts(sc, onoff);
643 		break;
644 	case UCOM_SET_BREAK:
645 		umodem_break(sc, onoff);
646 		break;
647 	default:
648 		break;
649 	}
650 }
651 
652 usbd_status
umodem_set_line_coding(struct umodem_softc * sc,usb_cdc_line_state_t * state)653 umodem_set_line_coding(struct umodem_softc *sc, usb_cdc_line_state_t *state)
654 {
655 	usb_device_request_t req;
656 	usbd_status err;
657 
658 	DPRINTF(("umodem_set_line_coding: rate=%d fmt=%d parity=%d bits=%d\n",
659 		 UGETDW(state->dwDTERate), state->bCharFormat,
660 		 state->bParityType, state->bDataBits));
661 
662 	if (memcmp(state, &sc->sc_line_state, UCDC_LINE_STATE_LENGTH) == 0) {
663 		DPRINTF(("umodem_set_line_coding: already set\n"));
664 		return (USBD_NORMAL_COMPLETION);
665 	}
666 
667 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
668 	req.bRequest = UCDC_SET_LINE_CODING;
669 	USETW(req.wValue, 0);
670 	USETW(req.wIndex, sc->sc_ctl_iface_no);
671 	USETW(req.wLength, UCDC_LINE_STATE_LENGTH);
672 
673 	err = usbd_do_request(sc->sc_udev, &req, state);
674 	if (err) {
675 		DPRINTF(("umodem_set_line_coding: failed, err=%s\n",
676 			 usbd_errstr(err)));
677 		return (err);
678 	}
679 
680 	sc->sc_line_state = *state;
681 
682 	return (USBD_NORMAL_COMPLETION);
683 }
684 
685 usbd_status
umodem_set_comm_feature(struct umodem_softc * sc,int feature,int state)686 umodem_set_comm_feature(struct umodem_softc *sc, int feature, int state)
687 {
688 	usb_device_request_t req;
689 	usbd_status err;
690 	usb_cdc_abstract_state_t ast;
691 
692 	DPRINTF(("umodem_set_comm_feature: feature=%d state=%d\n", feature,
693 		 state));
694 
695 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
696 	req.bRequest = UCDC_SET_COMM_FEATURE;
697 	USETW(req.wValue, feature);
698 	USETW(req.wIndex, sc->sc_ctl_iface_no);
699 	USETW(req.wLength, UCDC_ABSTRACT_STATE_LENGTH);
700 	USETW(ast.wState, state);
701 
702 	err = usbd_do_request(sc->sc_udev, &req, &ast);
703 	if (err) {
704 		DPRINTF(("umodem_set_comm_feature: feature=%d, err=%s\n",
705 			 feature, usbd_errstr(err)));
706 		return (err);
707 	}
708 
709 	return (USBD_NORMAL_COMPLETION);
710 }
711 
712 int
umodem_activate(device_ptr_t self,enum devact act)713 umodem_activate(device_ptr_t self, enum devact act)
714 {
715 	struct umodem_softc *sc = (struct umodem_softc *)self;
716 	int rv = 0;
717 
718 	switch (act) {
719 	case DVACT_ACTIVATE:
720 		return (EOPNOTSUPP);
721 
722 	case DVACT_DEACTIVATE:
723 		sc->sc_dying = 1;
724 		if (sc->sc_subdev)
725 			rv = config_deactivate(sc->sc_subdev);
726 		break;
727 	}
728 	return (rv);
729 }
730 
USB_DETACH(umodem)731 USB_DETACH(umodem)
732 {
733 	USB_DETACH_START(umodem, sc);
734 	int rv = 0;
735 
736 	DPRINTF(("umodem_detach: sc=%p flags=%d\n", sc, flags));
737 
738 	sc->sc_dying = 1;
739 
740 	if (sc->sc_subdev != NULL)
741 		rv = config_detach(sc->sc_subdev, flags);
742 
743 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
744 			   USBDEV(sc->sc_dev));
745 
746 	return (rv);
747 }
748