xref: /freebsd/sys/dev/usb/serial/umodem.c (revision ba3c1f5972d7b90feb6e6da47905ff2757e0fe57)
1 /*	$NetBSD: umodem.c,v 1.45 2002/09/23 05:51:23 simonb Exp $	*/
2 
3 #include <sys/cdefs.h>
4 __FBSDID("$FreeBSD$");
5 
6 /*-
7  * SPDX-License-Identifier: BSD-2-Clause
8  *
9  * Copyright (c) 2003 M. Warner Losh <imp@FreeBSD.org>
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 /*-
34  * Copyright (c) 1998 The NetBSD Foundation, Inc.
35  * All rights reserved.
36  *
37  * This code is derived from software contributed to The NetBSD Foundation
38  * by Lennart Augustsson (lennart@augustsson.net) at
39  * Carlstedt Research & Technology.
40  *
41  * Redistribution and use in source and binary forms, with or without
42  * modification, are permitted provided that the following conditions
43  * are met:
44  * 1. Redistributions of source code must retain the above copyright
45  *    notice, this list of conditions and the following disclaimer.
46  * 2. Redistributions in binary form must reproduce the above copyright
47  *    notice, this list of conditions and the following disclaimer in the
48  *    documentation and/or other materials provided with the distribution.
49  *
50  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
51  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
52  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
53  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
54  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
55  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
56  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
57  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
58  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
59  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
60  * POSSIBILITY OF SUCH DAMAGE.
61  */
62 
63 /*
64  * Comm Class spec:  http://www.usb.org/developers/devclass_docs/usbccs10.pdf
65  *                   http://www.usb.org/developers/devclass_docs/usbcdc11.pdf
66  *                   http://www.usb.org/developers/devclass_docs/cdc_wmc10.zip
67  */
68 
69 /*
70  * TODO:
71  * - Add error recovery in various places; the big problem is what
72  *   to do in a callback if there is an error.
73  * - Implement a Call Device for modems without multiplexed commands.
74  *
75  */
76 
77 #include <sys/stdint.h>
78 #include <sys/stddef.h>
79 #include <sys/param.h>
80 #include <sys/queue.h>
81 #include <sys/types.h>
82 #include <sys/systm.h>
83 #include <sys/kernel.h>
84 #include <sys/bus.h>
85 #include <sys/module.h>
86 #include <sys/lock.h>
87 #include <sys/mutex.h>
88 #include <sys/condvar.h>
89 #include <sys/sysctl.h>
90 #include <sys/sx.h>
91 #include <sys/unistd.h>
92 #include <sys/callout.h>
93 #include <sys/malloc.h>
94 #include <sys/priv.h>
95 
96 #include <dev/usb/usb.h>
97 #include <dev/usb/usbdi.h>
98 #include <dev/usb/usbdi_util.h>
99 #include <dev/usb/usbhid.h>
100 #include <dev/usb/usb_cdc.h>
101 #include "usbdevs.h"
102 #include "usb_if.h"
103 
104 #include <dev/usb/usb_ioctl.h>
105 
106 #define	USB_DEBUG_VAR umodem_debug
107 #include <dev/usb/usb_debug.h>
108 #include <dev/usb/usb_process.h>
109 #include <dev/usb/quirk/usb_quirk.h>
110 
111 #include <dev/usb/serial/usb_serial.h>
112 
113 #ifdef USB_DEBUG
114 static int umodem_debug = 0;
115 
116 static SYSCTL_NODE(_hw_usb, OID_AUTO, umodem, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
117     "USB umodem");
118 SYSCTL_INT(_hw_usb_umodem, OID_AUTO, debug, CTLFLAG_RWTUN,
119     &umodem_debug, 0, "Debug level");
120 #endif
121 
122 static const STRUCT_USB_DUAL_ID umodem_dual_devs[] = {
123 	/* Generic Modem class match */
124 	{USB_IFACE_CLASS(UICLASS_CDC),
125 		USB_IFACE_SUBCLASS(UISUBCLASS_ABSTRACT_CONTROL_MODEL),
126 		USB_IFACE_PROTOCOL(UIPROTO_CDC_AT)},
127 	{USB_IFACE_CLASS(UICLASS_CDC),
128 		USB_IFACE_SUBCLASS(UISUBCLASS_ABSTRACT_CONTROL_MODEL),
129 		USB_IFACE_PROTOCOL(UIPROTO_CDC_NONE)},
130 };
131 
132 static const STRUCT_USB_HOST_ID umodem_host_devs[] = {
133 	/* Huawei Modem class match */
134 	{USB_VENDOR(USB_VENDOR_HUAWEI), USB_IFACE_CLASS(UICLASS_VENDOR),
135 		USB_IFACE_SUBCLASS(0x02), USB_IFACE_PROTOCOL(0x01)},
136 	{USB_VENDOR(USB_VENDOR_HUAWEI), USB_IFACE_CLASS(UICLASS_VENDOR),
137 		USB_IFACE_SUBCLASS(0x02), USB_IFACE_PROTOCOL(0x02)},
138 	{USB_VENDOR(USB_VENDOR_HUAWEI), USB_IFACE_CLASS(UICLASS_VENDOR),
139 		USB_IFACE_SUBCLASS(0x02), USB_IFACE_PROTOCOL(0x10)},
140 	{USB_VENDOR(USB_VENDOR_HUAWEI), USB_IFACE_CLASS(UICLASS_VENDOR),
141 		USB_IFACE_SUBCLASS(0x02), USB_IFACE_PROTOCOL(0x12)},
142 	{USB_VENDOR(USB_VENDOR_HUAWEI), USB_IFACE_CLASS(UICLASS_VENDOR),
143 		USB_IFACE_SUBCLASS(0x02), USB_IFACE_PROTOCOL(0x61)},
144 	{USB_VENDOR(USB_VENDOR_HUAWEI), USB_IFACE_CLASS(UICLASS_VENDOR),
145 		USB_IFACE_SUBCLASS(0x02), USB_IFACE_PROTOCOL(0x62)},
146 	{USB_VENDOR(USB_VENDOR_HUAWEI),USB_IFACE_CLASS(UICLASS_CDC),
147 		USB_IFACE_SUBCLASS(UISUBCLASS_ABSTRACT_CONTROL_MODEL),
148 		USB_IFACE_PROTOCOL(0xFF)},
149 	{USB_VENDOR(USB_VENDOR_HUAWEI), USB_IFACE_CLASS(0xFF),
150 		USB_IFACE_SUBCLASS(0xF), USB_IFACE_PROTOCOL(0xFF)},
151 	/* Kyocera AH-K3001V */
152 	{USB_VPI(USB_VENDOR_KYOCERA, USB_PRODUCT_KYOCERA_AHK3001V, 1)},
153 	{USB_VPI(USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC5720, 1)},
154 	{USB_VPI(USB_VENDOR_CURITEL, USB_PRODUCT_CURITEL_PC5740, 1)},
155 	/* Winbond */
156 	{USB_VENDOR(USB_VENDOR_WINBOND), USB_PRODUCT(USB_PRODUCT_WINBOND_CDC)},
157 };
158 
159 /*
160  * As speeds for umodem devices increase, these numbers will need to
161  * be increased. They should be good for G3 speeds and below.
162  *
163  * TODO: The TTY buffers should be increased!
164  */
165 #define	UMODEM_BUF_SIZE 1024
166 
167 enum {
168 	UMODEM_BULK_WR,
169 	UMODEM_BULK_RD,
170 	UMODEM_INTR_WR,
171 	UMODEM_INTR_RD,
172 	UMODEM_N_TRANSFER,
173 };
174 
175 #define	UMODEM_MODVER			1	/* module version */
176 
177 struct umodem_softc {
178 	struct ucom_super_softc sc_super_ucom;
179 	struct ucom_softc sc_ucom;
180 
181 	struct usb_xfer *sc_xfer[UMODEM_N_TRANSFER];
182 	struct usb_device *sc_udev;
183 	struct mtx sc_mtx;
184 
185 	uint16_t sc_line;
186 
187 	uint8_t	sc_lsr;			/* local status register */
188 	uint8_t	sc_msr;			/* modem status register */
189 	uint8_t	sc_ctrl_iface_no;
190 	uint8_t	sc_data_iface_no;
191 	uint8_t sc_iface_index[2];
192 	uint8_t	sc_cm_over_data;
193 	uint8_t	sc_cm_cap;		/* CM capabilities */
194 	uint8_t	sc_acm_cap;		/* ACM capabilities */
195 	uint8_t	sc_line_coding[32];	/* used in USB device mode */
196 	uint8_t	sc_abstract_state[32];	/* used in USB device mode */
197 };
198 
199 static device_probe_t umodem_probe;
200 static device_attach_t umodem_attach;
201 static device_detach_t umodem_detach;
202 static usb_handle_request_t umodem_handle_request;
203 
204 static void umodem_free_softc(struct umodem_softc *);
205 
206 static usb_callback_t umodem_intr_read_callback;
207 static usb_callback_t umodem_intr_write_callback;
208 static usb_callback_t umodem_write_callback;
209 static usb_callback_t umodem_read_callback;
210 
211 static void	umodem_free(struct ucom_softc *);
212 static void	umodem_start_read(struct ucom_softc *);
213 static void	umodem_stop_read(struct ucom_softc *);
214 static void	umodem_start_write(struct ucom_softc *);
215 static void	umodem_stop_write(struct ucom_softc *);
216 static void	umodem_get_caps(struct usb_attach_arg *, uint8_t *, uint8_t *);
217 static void	umodem_cfg_get_status(struct ucom_softc *, uint8_t *,
218 		    uint8_t *);
219 static int	umodem_pre_param(struct ucom_softc *, struct termios *);
220 static void	umodem_cfg_param(struct ucom_softc *, struct termios *);
221 static void	umodem_cfg_open(struct ucom_softc *);
222 static int	umodem_ioctl(struct ucom_softc *, uint32_t, caddr_t, int,
223 		    struct thread *);
224 static void	umodem_cfg_set_dtr(struct ucom_softc *, uint8_t);
225 static void	umodem_cfg_set_rts(struct ucom_softc *, uint8_t);
226 static void	umodem_cfg_set_break(struct ucom_softc *, uint8_t);
227 static void	*umodem_get_desc(struct usb_attach_arg *, uint8_t, uint8_t);
228 static usb_error_t umodem_set_comm_feature(struct usb_device *, uint8_t,
229 		    uint16_t, uint16_t);
230 static void	umodem_poll(struct ucom_softc *ucom);
231 static void	umodem_find_data_iface(struct usb_attach_arg *uaa,
232 		    uint8_t, uint8_t *, uint8_t *);
233 
234 static const struct usb_config umodem_config[UMODEM_N_TRANSFER] = {
235 	[UMODEM_BULK_WR] = {
236 		.type = UE_BULK,
237 		.endpoint = UE_ADDR_ANY,
238 		.direction = UE_DIR_TX,
239 		.if_index = 0,
240 		.bufsize = UMODEM_BUF_SIZE,
241 		.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
242 		.callback = &umodem_write_callback,
243 		.usb_mode = USB_MODE_DUAL,
244 	},
245 
246 	[UMODEM_BULK_RD] = {
247 		.type = UE_BULK,
248 		.endpoint = UE_ADDR_ANY,
249 		.direction = UE_DIR_RX,
250 		.if_index = 0,
251 		.bufsize = UMODEM_BUF_SIZE,
252 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
253 		.callback = &umodem_read_callback,
254 		.usb_mode = USB_MODE_DUAL,
255 	},
256 
257 	[UMODEM_INTR_WR] = {
258 		.type = UE_INTERRUPT,
259 		.endpoint = UE_ADDR_ANY,
260 		.direction = UE_DIR_TX,
261 		.if_index = 1,
262 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,.no_pipe_ok = 1,},
263 		.bufsize = 0,	/* use wMaxPacketSize */
264 		.callback = &umodem_intr_write_callback,
265 		.usb_mode = USB_MODE_DEVICE,
266 	},
267 
268 	[UMODEM_INTR_RD] = {
269 		.type = UE_INTERRUPT,
270 		.endpoint = UE_ADDR_ANY,
271 		.direction = UE_DIR_RX,
272 		.if_index = 1,
273 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,.no_pipe_ok = 1,},
274 		.bufsize = 0,	/* use wMaxPacketSize */
275 		.callback = &umodem_intr_read_callback,
276 		.usb_mode = USB_MODE_HOST,
277 	},
278 };
279 
280 static const struct ucom_callback umodem_callback = {
281 	.ucom_cfg_get_status = &umodem_cfg_get_status,
282 	.ucom_cfg_set_dtr = &umodem_cfg_set_dtr,
283 	.ucom_cfg_set_rts = &umodem_cfg_set_rts,
284 	.ucom_cfg_set_break = &umodem_cfg_set_break,
285 	.ucom_cfg_param = &umodem_cfg_param,
286 	.ucom_pre_param = &umodem_pre_param,
287 	.ucom_cfg_open = &umodem_cfg_open,
288 	.ucom_ioctl = &umodem_ioctl,
289 	.ucom_start_read = &umodem_start_read,
290 	.ucom_stop_read = &umodem_stop_read,
291 	.ucom_start_write = &umodem_start_write,
292 	.ucom_stop_write = &umodem_stop_write,
293 	.ucom_poll = &umodem_poll,
294 	.ucom_free = &umodem_free,
295 };
296 
297 static device_method_t umodem_methods[] = {
298 	/* USB interface */
299 	DEVMETHOD(usb_handle_request, umodem_handle_request),
300 
301 	/* Device interface */
302 	DEVMETHOD(device_probe, umodem_probe),
303 	DEVMETHOD(device_attach, umodem_attach),
304 	DEVMETHOD(device_detach, umodem_detach),
305 	DEVMETHOD_END
306 };
307 
308 static driver_t umodem_driver = {
309 	.name = "umodem",
310 	.methods = umodem_methods,
311 	.size = sizeof(struct umodem_softc),
312 };
313 
314 DRIVER_MODULE(umodem, uhub, umodem_driver, NULL, NULL);
315 MODULE_DEPEND(umodem, ucom, 1, 1, 1);
316 MODULE_DEPEND(umodem, usb, 1, 1, 1);
317 MODULE_VERSION(umodem, UMODEM_MODVER);
318 USB_PNP_DUAL_INFO(umodem_dual_devs);
319 USB_PNP_HOST_INFO(umodem_host_devs);
320 
321 static int
322 umodem_probe(device_t dev)
323 {
324 	struct usb_attach_arg *uaa = device_get_ivars(dev);
325 	int error;
326 
327 	DPRINTFN(11, "\n");
328 
329 	error = usbd_lookup_id_by_uaa(umodem_host_devs,
330 	    sizeof(umodem_host_devs), uaa);
331 	if (error) {
332 		error = usbd_lookup_id_by_uaa(umodem_dual_devs,
333 		    sizeof(umodem_dual_devs), uaa);
334 		if (error)
335 			return (error);
336 	}
337 	return (BUS_PROBE_GENERIC);
338 }
339 
340 static int
341 umodem_attach(device_t dev)
342 {
343 	struct usb_attach_arg *uaa = device_get_ivars(dev);
344 	struct umodem_softc *sc = device_get_softc(dev);
345 	struct usb_cdc_cm_descriptor *cmd;
346 	struct usb_cdc_union_descriptor *cud;
347 	uint8_t i;
348 	int error;
349 
350 	device_set_usb_desc(dev);
351 	mtx_init(&sc->sc_mtx, "umodem", NULL, MTX_DEF);
352 	ucom_ref(&sc->sc_super_ucom);
353 
354 	sc->sc_ctrl_iface_no = uaa->info.bIfaceNum;
355 	sc->sc_iface_index[1] = uaa->info.bIfaceIndex;
356 	sc->sc_udev = uaa->device;
357 
358 	umodem_get_caps(uaa, &sc->sc_cm_cap, &sc->sc_acm_cap);
359 
360 	/* get the data interface number */
361 
362 	cmd = NULL;
363 	if (!usb_test_quirk(uaa, UQ_IGNORE_CDC_CM))
364 		cmd = umodem_get_desc(uaa, UDESC_CS_INTERFACE, UDESCSUB_CDC_CM);
365 
366 	if ((cmd == NULL) || (cmd->bLength < sizeof(*cmd))) {
367 		cud = usbd_find_descriptor(uaa->device, NULL,
368 		    uaa->info.bIfaceIndex, UDESC_CS_INTERFACE,
369 		    0xFF, UDESCSUB_CDC_UNION, 0xFF);
370 
371 		if ((cud == NULL) || (cud->bLength < sizeof(*cud))) {
372 			DPRINTF("Missing descriptor. "
373 			    "Assuming data interface is next.\n");
374 			if (sc->sc_ctrl_iface_no == 0xFF) {
375 				goto detach;
376 			} else {
377 				uint8_t class_match = 0;
378 
379 				/* set default interface number */
380 				sc->sc_data_iface_no = 0xFF;
381 
382 				/* try to find the data interface backwards */
383 				umodem_find_data_iface(uaa,
384 				    uaa->info.bIfaceIndex - 1,
385 				    &sc->sc_data_iface_no, &class_match);
386 
387 				/* try to find the data interface forwards */
388 				umodem_find_data_iface(uaa,
389 				    uaa->info.bIfaceIndex + 1,
390 				    &sc->sc_data_iface_no, &class_match);
391 
392 				/* check if nothing was found */
393 				if (sc->sc_data_iface_no == 0xFF)
394 					goto detach;
395 			}
396 		} else {
397 			sc->sc_data_iface_no = cud->bSlaveInterface[0];
398 		}
399 	} else {
400 		sc->sc_data_iface_no = cmd->bDataInterface;
401 	}
402 
403 	device_printf(dev, "data interface %d, has %sCM over "
404 	    "data, has %sbreak\n",
405 	    sc->sc_data_iface_no,
406 	    sc->sc_cm_cap & USB_CDC_CM_OVER_DATA ? "" : "no ",
407 	    sc->sc_acm_cap & USB_CDC_ACM_HAS_BREAK ? "" : "no ");
408 
409 	/* get the data interface too */
410 
411 	for (i = 0;; i++) {
412 		struct usb_interface *iface;
413 		struct usb_interface_descriptor *id;
414 
415 		iface = usbd_get_iface(uaa->device, i);
416 
417 		if (iface) {
418 			id = usbd_get_interface_descriptor(iface);
419 
420 			if (id && (id->bInterfaceNumber == sc->sc_data_iface_no)) {
421 				sc->sc_iface_index[0] = i;
422 				usbd_set_parent_iface(uaa->device, i, uaa->info.bIfaceIndex);
423 				break;
424 			}
425 		} else {
426 			device_printf(dev, "no data interface\n");
427 			goto detach;
428 		}
429 	}
430 
431 	if (usb_test_quirk(uaa, UQ_ASSUME_CM_OVER_DATA)) {
432 		sc->sc_cm_over_data = 1;
433 	} else {
434 		if (sc->sc_cm_cap & USB_CDC_CM_OVER_DATA) {
435 			if (sc->sc_acm_cap & USB_CDC_ACM_HAS_FEATURE) {
436 				error = umodem_set_comm_feature
437 				(uaa->device, sc->sc_ctrl_iface_no,
438 				 UCDC_ABSTRACT_STATE, UCDC_DATA_MULTIPLEXED);
439 
440 				/* ignore any errors */
441 			}
442 			sc->sc_cm_over_data = 1;
443 		}
444 	}
445 	error = usbd_transfer_setup(uaa->device,
446 	    sc->sc_iface_index, sc->sc_xfer,
447 	    umodem_config, UMODEM_N_TRANSFER,
448 	    sc, &sc->sc_mtx);
449 	if (error) {
450 		device_printf(dev, "Can't setup transfer\n");
451 		goto detach;
452 	}
453 
454 	ucom_set_usb_mode(&sc->sc_super_ucom, uaa->usb_mode);
455 
456 	error = ucom_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc,
457 	    &umodem_callback, &sc->sc_mtx);
458 	if (error) {
459 		device_printf(dev, "Can't attach com\n");
460 		goto detach;
461 	}
462 	ucom_set_pnpinfo_usb(&sc->sc_super_ucom, dev);
463 
464 	return (0);
465 
466 detach:
467 	umodem_detach(dev);
468 	return (ENXIO);
469 }
470 
471 static void
472 umodem_find_data_iface(struct usb_attach_arg *uaa,
473     uint8_t iface_index, uint8_t *p_data_no, uint8_t *p_match_class)
474 {
475 	struct usb_interface_descriptor *id;
476 	struct usb_interface *iface;
477 
478 	iface = usbd_get_iface(uaa->device, iface_index);
479 
480 	/* check for end of interfaces */
481 	if (iface == NULL)
482 		return;
483 
484 	id = usbd_get_interface_descriptor(iface);
485 
486 	/* check for non-matching interface class */
487 	if (id->bInterfaceClass != UICLASS_CDC_DATA ||
488 	    id->bInterfaceSubClass != UISUBCLASS_DATA) {
489 		/* if we got a class match then return */
490 		if (*p_match_class)
491 			return;
492 	} else {
493 		*p_match_class = 1;
494 	}
495 
496 	DPRINTFN(11, "Match at index %u\n", iface_index);
497 
498 	*p_data_no = id->bInterfaceNumber;
499 }
500 
501 static void
502 umodem_start_read(struct ucom_softc *ucom)
503 {
504 	struct umodem_softc *sc = ucom->sc_parent;
505 
506 	/* start interrupt endpoint, if any */
507 	usbd_transfer_start(sc->sc_xfer[UMODEM_INTR_RD]);
508 
509 	/* start read endpoint */
510 	usbd_transfer_start(sc->sc_xfer[UMODEM_BULK_RD]);
511 }
512 
513 static void
514 umodem_stop_read(struct ucom_softc *ucom)
515 {
516 	struct umodem_softc *sc = ucom->sc_parent;
517 
518 	/* stop interrupt endpoint, if any */
519 	usbd_transfer_stop(sc->sc_xfer[UMODEM_INTR_RD]);
520 
521 	/* stop read endpoint */
522 	usbd_transfer_stop(sc->sc_xfer[UMODEM_BULK_RD]);
523 }
524 
525 static void
526 umodem_start_write(struct ucom_softc *ucom)
527 {
528 	struct umodem_softc *sc = ucom->sc_parent;
529 
530 	usbd_transfer_start(sc->sc_xfer[UMODEM_INTR_WR]);
531 	usbd_transfer_start(sc->sc_xfer[UMODEM_BULK_WR]);
532 }
533 
534 static void
535 umodem_stop_write(struct ucom_softc *ucom)
536 {
537 	struct umodem_softc *sc = ucom->sc_parent;
538 
539 	usbd_transfer_stop(sc->sc_xfer[UMODEM_INTR_WR]);
540 	usbd_transfer_stop(sc->sc_xfer[UMODEM_BULK_WR]);
541 }
542 
543 static void
544 umodem_get_caps(struct usb_attach_arg *uaa, uint8_t *cm, uint8_t *acm)
545 {
546 	struct usb_cdc_cm_descriptor *cmd;
547 	struct usb_cdc_acm_descriptor *cad;
548 
549 	cmd = umodem_get_desc(uaa, UDESC_CS_INTERFACE, UDESCSUB_CDC_CM);
550 	if ((cmd == NULL) || (cmd->bLength < sizeof(*cmd))) {
551 		DPRINTF("no CM desc (faking one)\n");
552 		*cm = USB_CDC_CM_DOES_CM | USB_CDC_CM_OVER_DATA;
553 	} else
554 		*cm = cmd->bmCapabilities;
555 
556 	cad = umodem_get_desc(uaa, UDESC_CS_INTERFACE, UDESCSUB_CDC_ACM);
557 	if ((cad == NULL) || (cad->bLength < sizeof(*cad))) {
558 		DPRINTF("no ACM desc\n");
559 		*acm = 0;
560 	} else
561 		*acm = cad->bmCapabilities;
562 }
563 
564 static void
565 umodem_cfg_get_status(struct ucom_softc *ucom, uint8_t *lsr, uint8_t *msr)
566 {
567 	struct umodem_softc *sc = ucom->sc_parent;
568 
569 	DPRINTF("\n");
570 
571 	/* XXX Note: sc_lsr is always zero */
572 	*lsr = sc->sc_lsr;
573 	*msr = sc->sc_msr;
574 }
575 
576 static int
577 umodem_pre_param(struct ucom_softc *ucom, struct termios *t)
578 {
579 	return (0);			/* we accept anything */
580 }
581 
582 static void
583 umodem_cfg_param(struct ucom_softc *ucom, struct termios *t)
584 {
585 	struct umodem_softc *sc = ucom->sc_parent;
586 	struct usb_cdc_line_state ls;
587 	struct usb_device_request req;
588 
589 	DPRINTF("sc=%p\n", sc);
590 
591 	memset(&ls, 0, sizeof(ls));
592 
593 	USETDW(ls.dwDTERate, t->c_ospeed);
594 
595 	ls.bCharFormat = (t->c_cflag & CSTOPB) ?
596 	    UCDC_STOP_BIT_2 : UCDC_STOP_BIT_1;
597 
598 	ls.bParityType = (t->c_cflag & PARENB) ?
599 	    ((t->c_cflag & PARODD) ?
600 	    UCDC_PARITY_ODD : UCDC_PARITY_EVEN) : UCDC_PARITY_NONE;
601 
602 	switch (t->c_cflag & CSIZE) {
603 	case CS5:
604 		ls.bDataBits = 5;
605 		break;
606 	case CS6:
607 		ls.bDataBits = 6;
608 		break;
609 	case CS7:
610 		ls.bDataBits = 7;
611 		break;
612 	case CS8:
613 		ls.bDataBits = 8;
614 		break;
615 	}
616 
617 	DPRINTF("rate=%d fmt=%d parity=%d bits=%d\n",
618 	    UGETDW(ls.dwDTERate), ls.bCharFormat,
619 	    ls.bParityType, ls.bDataBits);
620 
621 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
622 	req.bRequest = UCDC_SET_LINE_CODING;
623 	USETW(req.wValue, 0);
624 	req.wIndex[0] = sc->sc_ctrl_iface_no;
625 	req.wIndex[1] = 0;
626 	USETW(req.wLength, sizeof(ls));
627 
628 	ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
629 	    &req, &ls, 0, 1000);
630 }
631 
632 static void
633 umodem_cfg_open(struct ucom_softc *ucom)
634 {
635 	struct umodem_softc *sc = ucom->sc_parent;
636 
637 	/* clear stall, if in USB host mode */
638 	if ((sc->sc_super_ucom.sc_flag & UCOM_FLAG_DEVICE_MODE) == 0) {
639 		usbd_xfer_set_stall(sc->sc_xfer[UMODEM_BULK_WR]);
640 		usbd_xfer_set_stall(sc->sc_xfer[UMODEM_BULK_RD]);
641 	}
642 }
643 
644 static int
645 umodem_ioctl(struct ucom_softc *ucom, uint32_t cmd, caddr_t data,
646     int flag, struct thread *td)
647 {
648 	struct umodem_softc *sc = ucom->sc_parent;
649 	int error = 0;
650 
651 	DPRINTF("cmd=0x%08x\n", cmd);
652 
653 	switch (cmd) {
654 	case USB_GET_CM_OVER_DATA:
655 		*(int *)data = sc->sc_cm_over_data;
656 		break;
657 
658 	case USB_SET_CM_OVER_DATA:
659 		if (*(int *)data != sc->sc_cm_over_data) {
660 			/* XXX change it */
661 		}
662 		break;
663 
664 	default:
665 		DPRINTF("unknown\n");
666 		error = ENOIOCTL;
667 		break;
668 	}
669 
670 	return (error);
671 }
672 
673 static void
674 umodem_cfg_set_dtr(struct ucom_softc *ucom, uint8_t onoff)
675 {
676 	struct umodem_softc *sc = ucom->sc_parent;
677 	struct usb_device_request req;
678 
679 	DPRINTF("onoff=%d\n", onoff);
680 
681 	if (onoff)
682 		sc->sc_line |= UCDC_LINE_DTR;
683 	else
684 		sc->sc_line &= ~UCDC_LINE_DTR;
685 
686 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
687 	req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
688 	USETW(req.wValue, sc->sc_line);
689 	req.wIndex[0] = sc->sc_ctrl_iface_no;
690 	req.wIndex[1] = 0;
691 	USETW(req.wLength, 0);
692 
693 	ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
694 	    &req, NULL, 0, 1000);
695 }
696 
697 static void
698 umodem_cfg_set_rts(struct ucom_softc *ucom, uint8_t onoff)
699 {
700 	struct umodem_softc *sc = ucom->sc_parent;
701 	struct usb_device_request req;
702 
703 	DPRINTF("onoff=%d\n", onoff);
704 
705 	if (onoff)
706 		sc->sc_line |= UCDC_LINE_RTS;
707 	else
708 		sc->sc_line &= ~UCDC_LINE_RTS;
709 
710 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
711 	req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
712 	USETW(req.wValue, sc->sc_line);
713 	req.wIndex[0] = sc->sc_ctrl_iface_no;
714 	req.wIndex[1] = 0;
715 	USETW(req.wLength, 0);
716 
717 	ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
718 	    &req, NULL, 0, 1000);
719 }
720 
721 static void
722 umodem_cfg_set_break(struct ucom_softc *ucom, uint8_t onoff)
723 {
724 	struct umodem_softc *sc = ucom->sc_parent;
725 	struct usb_device_request req;
726 	uint16_t temp;
727 
728 	DPRINTF("onoff=%d\n", onoff);
729 
730 	if (sc->sc_acm_cap & USB_CDC_ACM_HAS_BREAK) {
731 		temp = onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF;
732 
733 		req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
734 		req.bRequest = UCDC_SEND_BREAK;
735 		USETW(req.wValue, temp);
736 		req.wIndex[0] = sc->sc_ctrl_iface_no;
737 		req.wIndex[1] = 0;
738 		USETW(req.wLength, 0);
739 
740 		ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
741 		    &req, NULL, 0, 1000);
742 	}
743 }
744 
745 static void
746 umodem_intr_write_callback(struct usb_xfer *xfer, usb_error_t error)
747 {
748 	int actlen;
749 
750 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
751 
752 	switch (USB_GET_STATE(xfer)) {
753 	case USB_ST_TRANSFERRED:
754 
755 		DPRINTF("Transferred %d bytes\n", actlen);
756 
757 		/* FALLTHROUGH */
758 	case USB_ST_SETUP:
759 tr_setup:
760 		break;
761 
762 	default:			/* Error */
763 		if (error != USB_ERR_CANCELLED) {
764 			/* start clear stall */
765 			usbd_xfer_set_stall(xfer);
766 			goto tr_setup;
767 		}
768 		break;
769 	}
770 }
771 
772 static void
773 umodem_intr_read_callback(struct usb_xfer *xfer, usb_error_t error)
774 {
775 	struct usb_cdc_notification pkt;
776 	struct umodem_softc *sc = usbd_xfer_softc(xfer);
777 	struct usb_page_cache *pc;
778 	uint16_t wLen;
779 	int actlen;
780 
781 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
782 
783 	switch (USB_GET_STATE(xfer)) {
784 	case USB_ST_TRANSFERRED:
785 
786 		if (actlen < 8) {
787 			DPRINTF("received short packet, "
788 			    "%d bytes\n", actlen);
789 			goto tr_setup;
790 		}
791 		if (actlen > (int)sizeof(pkt)) {
792 			DPRINTF("truncating message\n");
793 			actlen = sizeof(pkt);
794 		}
795 		pc = usbd_xfer_get_frame(xfer, 0);
796 		usbd_copy_out(pc, 0, &pkt, actlen);
797 
798 		actlen -= 8;
799 
800 		wLen = UGETW(pkt.wLength);
801 		if (actlen > wLen) {
802 			actlen = wLen;
803 		}
804 		if (pkt.bmRequestType != UCDC_NOTIFICATION) {
805 			DPRINTF("unknown message type, "
806 			    "0x%02x, on notify pipe!\n",
807 			    pkt.bmRequestType);
808 			goto tr_setup;
809 		}
810 		switch (pkt.bNotification) {
811 		case UCDC_N_SERIAL_STATE:
812 			/*
813 			 * Set the serial state in ucom driver based on
814 			 * the bits from the notify message
815 			 */
816 			if (actlen < 2) {
817 				DPRINTF("invalid notification "
818 				    "length, %d bytes!\n", actlen);
819 				break;
820 			}
821 			DPRINTF("notify bytes = %02x%02x\n",
822 			    pkt.data[0],
823 			    pkt.data[1]);
824 
825 			/* Currently, lsr is always zero. */
826 			sc->sc_lsr = 0;
827 			sc->sc_msr = 0;
828 
829 			if (pkt.data[0] & UCDC_N_SERIAL_RI) {
830 				sc->sc_msr |= SER_RI;
831 			}
832 			if (pkt.data[0] & UCDC_N_SERIAL_DSR) {
833 				sc->sc_msr |= SER_DSR;
834 			}
835 			if (pkt.data[0] & UCDC_N_SERIAL_DCD) {
836 				sc->sc_msr |= SER_DCD;
837 			}
838 			ucom_status_change(&sc->sc_ucom);
839 			break;
840 
841 		default:
842 			DPRINTF("unknown notify message: 0x%02x\n",
843 			    pkt.bNotification);
844 			break;
845 		}
846 
847 	case USB_ST_SETUP:
848 tr_setup:
849 		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
850 		usbd_transfer_submit(xfer);
851 		return;
852 
853 	default:			/* Error */
854 		if (error != USB_ERR_CANCELLED) {
855 			/* try to clear stall first */
856 			usbd_xfer_set_stall(xfer);
857 			goto tr_setup;
858 		}
859 		return;
860 	}
861 }
862 
863 static void
864 umodem_write_callback(struct usb_xfer *xfer, usb_error_t error)
865 {
866 	struct umodem_softc *sc = usbd_xfer_softc(xfer);
867 	struct usb_page_cache *pc;
868 	uint32_t actlen;
869 
870 	switch (USB_GET_STATE(xfer)) {
871 	case USB_ST_SETUP:
872 	case USB_ST_TRANSFERRED:
873 tr_setup:
874 		pc = usbd_xfer_get_frame(xfer, 0);
875 		if (ucom_get_data(&sc->sc_ucom, pc, 0,
876 		    UMODEM_BUF_SIZE, &actlen)) {
877 			usbd_xfer_set_frame_len(xfer, 0, actlen);
878 			usbd_transfer_submit(xfer);
879 		}
880 		return;
881 
882 	default:			/* Error */
883 		if (error != USB_ERR_CANCELLED) {
884 			/* try to clear stall first */
885 			usbd_xfer_set_stall(xfer);
886 			goto tr_setup;
887 		}
888 		return;
889 	}
890 }
891 
892 static void
893 umodem_read_callback(struct usb_xfer *xfer, usb_error_t error)
894 {
895 	struct umodem_softc *sc = usbd_xfer_softc(xfer);
896 	struct usb_page_cache *pc;
897 	int actlen;
898 
899 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
900 
901 	switch (USB_GET_STATE(xfer)) {
902 	case USB_ST_TRANSFERRED:
903 
904 		DPRINTF("actlen=%d\n", actlen);
905 
906 		pc = usbd_xfer_get_frame(xfer, 0);
907 		ucom_put_data(&sc->sc_ucom, pc, 0, actlen);
908 
909 	case USB_ST_SETUP:
910 tr_setup:
911 		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
912 		usbd_transfer_submit(xfer);
913 		return;
914 
915 	default:			/* Error */
916 		if (error != USB_ERR_CANCELLED) {
917 			/* try to clear stall first */
918 			usbd_xfer_set_stall(xfer);
919 			goto tr_setup;
920 		}
921 		return;
922 	}
923 }
924 
925 static void *
926 umodem_get_desc(struct usb_attach_arg *uaa, uint8_t type, uint8_t subtype)
927 {
928 	return (usbd_find_descriptor(uaa->device, NULL, uaa->info.bIfaceIndex,
929 	    type, 0xFF, subtype, 0xFF));
930 }
931 
932 static usb_error_t
933 umodem_set_comm_feature(struct usb_device *udev, uint8_t iface_no,
934     uint16_t feature, uint16_t state)
935 {
936 	struct usb_device_request req;
937 	struct usb_cdc_abstract_state ast;
938 
939 	DPRINTF("feature=%d state=%d\n",
940 	    feature, state);
941 
942 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
943 	req.bRequest = UCDC_SET_COMM_FEATURE;
944 	USETW(req.wValue, feature);
945 	req.wIndex[0] = iface_no;
946 	req.wIndex[1] = 0;
947 	USETW(req.wLength, UCDC_ABSTRACT_STATE_LENGTH);
948 	USETW(ast.wState, state);
949 
950 	return (usbd_do_request(udev, NULL, &req, &ast));
951 }
952 
953 static int
954 umodem_detach(device_t dev)
955 {
956 	struct umodem_softc *sc = device_get_softc(dev);
957 
958 	DPRINTF("sc=%p\n", sc);
959 
960 	ucom_detach(&sc->sc_super_ucom, &sc->sc_ucom);
961 	usbd_transfer_unsetup(sc->sc_xfer, UMODEM_N_TRANSFER);
962 
963 	device_claim_softc(dev);
964 
965 	umodem_free_softc(sc);
966 
967 	return (0);
968 }
969 
970 UCOM_UNLOAD_DRAIN(umodem);
971 
972 static void
973 umodem_free_softc(struct umodem_softc *sc)
974 {
975 	if (ucom_unref(&sc->sc_super_ucom)) {
976 		mtx_destroy(&sc->sc_mtx);
977 		device_free_softc(sc);
978 	}
979 }
980 
981 static void
982 umodem_free(struct ucom_softc *ucom)
983 {
984 	umodem_free_softc(ucom->sc_parent);
985 }
986 
987 static void
988 umodem_poll(struct ucom_softc *ucom)
989 {
990 	struct umodem_softc *sc = ucom->sc_parent;
991 	usbd_transfer_poll(sc->sc_xfer, UMODEM_N_TRANSFER);
992 }
993 
994 static int
995 umodem_handle_request(device_t dev,
996     const void *preq, void **pptr, uint16_t *plen,
997     uint16_t offset, uint8_t *pstate)
998 {
999 	struct umodem_softc *sc = device_get_softc(dev);
1000 	const struct usb_device_request *req = preq;
1001 	uint8_t is_complete = *pstate;
1002 
1003 	DPRINTF("sc=%p\n", sc);
1004 
1005 	if (!is_complete) {
1006 		if ((req->bmRequestType == UT_WRITE_CLASS_INTERFACE) &&
1007 		    (req->bRequest == UCDC_SET_LINE_CODING) &&
1008 		    (req->wIndex[0] == sc->sc_ctrl_iface_no) &&
1009 		    (req->wIndex[1] == 0x00) &&
1010 		    (req->wValue[0] == 0x00) &&
1011 		    (req->wValue[1] == 0x00)) {
1012 			if (offset == 0) {
1013 				*plen = sizeof(sc->sc_line_coding);
1014 				*pptr = &sc->sc_line_coding;
1015 			} else {
1016 				*plen = 0;
1017 			}
1018 			return (0);
1019 		} else if ((req->bmRequestType == UT_WRITE_CLASS_INTERFACE) &&
1020 		    (req->wIndex[0] == sc->sc_ctrl_iface_no) &&
1021 		    (req->wIndex[1] == 0x00) &&
1022 		    (req->bRequest == UCDC_SET_COMM_FEATURE)) {
1023 			if (offset == 0) {
1024 				*plen = sizeof(sc->sc_abstract_state);
1025 				*pptr = &sc->sc_abstract_state;
1026 			} else {
1027 				*plen = 0;
1028 			}
1029 			return (0);
1030 		} else if ((req->bmRequestType == UT_WRITE_CLASS_INTERFACE) &&
1031 		    (req->wIndex[0] == sc->sc_ctrl_iface_no) &&
1032 		    (req->wIndex[1] == 0x00) &&
1033 		    (req->bRequest == UCDC_SET_CONTROL_LINE_STATE)) {
1034 			*plen = 0;
1035 			return (0);
1036 		} else if ((req->bmRequestType == UT_WRITE_CLASS_INTERFACE) &&
1037 		    (req->wIndex[0] == sc->sc_ctrl_iface_no) &&
1038 		    (req->wIndex[1] == 0x00) &&
1039 		    (req->bRequest == UCDC_SEND_BREAK)) {
1040 			*plen = 0;
1041 			return (0);
1042 		}
1043 	}
1044 	return (ENXIO);			/* use builtin handler */
1045 }
1046