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