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