xref: /freebsd/sys/dev/usb/serial/umodem.c (revision 19261079)
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 | 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 int	umodem_ioctl(struct ucom_softc *, uint32_t, caddr_t, int,
222 		    struct thread *);
223 static void	umodem_cfg_set_dtr(struct ucom_softc *, uint8_t);
224 static void	umodem_cfg_set_rts(struct ucom_softc *, uint8_t);
225 static void	umodem_cfg_set_break(struct ucom_softc *, uint8_t);
226 static void	*umodem_get_desc(struct usb_attach_arg *, uint8_t, uint8_t);
227 static usb_error_t umodem_set_comm_feature(struct usb_device *, uint8_t,
228 		    uint16_t, uint16_t);
229 static void	umodem_poll(struct ucom_softc *ucom);
230 static void	umodem_find_data_iface(struct usb_attach_arg *uaa,
231 		    uint8_t, uint8_t *, uint8_t *);
232 
233 static const struct usb_config umodem_config[UMODEM_N_TRANSFER] = {
234 	[UMODEM_BULK_WR] = {
235 		.type = UE_BULK,
236 		.endpoint = UE_ADDR_ANY,
237 		.direction = UE_DIR_TX,
238 		.if_index = 0,
239 		.bufsize = UMODEM_BUF_SIZE,
240 		.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
241 		.callback = &umodem_write_callback,
242 		.usb_mode = USB_MODE_DUAL,
243 	},
244 
245 	[UMODEM_BULK_RD] = {
246 		.type = UE_BULK,
247 		.endpoint = UE_ADDR_ANY,
248 		.direction = UE_DIR_RX,
249 		.if_index = 0,
250 		.bufsize = UMODEM_BUF_SIZE,
251 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
252 		.callback = &umodem_read_callback,
253 		.usb_mode = USB_MODE_DUAL,
254 	},
255 
256 	[UMODEM_INTR_WR] = {
257 		.type = UE_INTERRUPT,
258 		.endpoint = UE_ADDR_ANY,
259 		.direction = UE_DIR_TX,
260 		.if_index = 1,
261 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,.no_pipe_ok = 1,},
262 		.bufsize = 0,	/* use wMaxPacketSize */
263 		.callback = &umodem_intr_write_callback,
264 		.usb_mode = USB_MODE_DEVICE,
265 	},
266 
267 	[UMODEM_INTR_RD] = {
268 		.type = UE_INTERRUPT,
269 		.endpoint = UE_ADDR_ANY,
270 		.direction = UE_DIR_RX,
271 		.if_index = 1,
272 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,.no_pipe_ok = 1,},
273 		.bufsize = 0,	/* use wMaxPacketSize */
274 		.callback = &umodem_intr_read_callback,
275 		.usb_mode = USB_MODE_HOST,
276 	},
277 };
278 
279 static const struct ucom_callback umodem_callback = {
280 	.ucom_cfg_get_status = &umodem_cfg_get_status,
281 	.ucom_cfg_set_dtr = &umodem_cfg_set_dtr,
282 	.ucom_cfg_set_rts = &umodem_cfg_set_rts,
283 	.ucom_cfg_set_break = &umodem_cfg_set_break,
284 	.ucom_cfg_param = &umodem_cfg_param,
285 	.ucom_pre_param = &umodem_pre_param,
286 	.ucom_ioctl = &umodem_ioctl,
287 	.ucom_start_read = &umodem_start_read,
288 	.ucom_stop_read = &umodem_stop_read,
289 	.ucom_start_write = &umodem_start_write,
290 	.ucom_stop_write = &umodem_stop_write,
291 	.ucom_poll = &umodem_poll,
292 	.ucom_free = &umodem_free,
293 };
294 
295 static device_method_t umodem_methods[] = {
296 	/* USB interface */
297 	DEVMETHOD(usb_handle_request, umodem_handle_request),
298 
299 	/* Device interface */
300 	DEVMETHOD(device_probe, umodem_probe),
301 	DEVMETHOD(device_attach, umodem_attach),
302 	DEVMETHOD(device_detach, umodem_detach),
303 	DEVMETHOD_END
304 };
305 
306 static devclass_t umodem_devclass;
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, umodem_devclass, NULL, 0);
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 = umodem_get_desc(uaa, UDESC_CS_INTERFACE, UDESCSUB_CDC_CM);
363 
364 	if ((cmd == NULL) || (cmd->bLength < sizeof(*cmd))) {
365 		cud = usbd_find_descriptor(uaa->device, NULL,
366 		    uaa->info.bIfaceIndex, UDESC_CS_INTERFACE,
367 		    0xFF, UDESCSUB_CDC_UNION, 0xFF);
368 
369 		if ((cud == NULL) || (cud->bLength < sizeof(*cud))) {
370 			DPRINTF("Missing descriptor. "
371 			    "Assuming data interface is next.\n");
372 			if (sc->sc_ctrl_iface_no == 0xFF) {
373 				goto detach;
374 			} else {
375 				uint8_t class_match = 0;
376 
377 				/* set default interface number */
378 				sc->sc_data_iface_no = 0xFF;
379 
380 				/* try to find the data interface backwards */
381 				umodem_find_data_iface(uaa,
382 				    uaa->info.bIfaceIndex - 1,
383 				    &sc->sc_data_iface_no, &class_match);
384 
385 				/* try to find the data interface forwards */
386 				umodem_find_data_iface(uaa,
387 				    uaa->info.bIfaceIndex + 1,
388 				    &sc->sc_data_iface_no, &class_match);
389 
390 				/* check if nothing was found */
391 				if (sc->sc_data_iface_no == 0xFF)
392 					goto detach;
393 			}
394 		} else {
395 			sc->sc_data_iface_no = cud->bSlaveInterface[0];
396 		}
397 	} else {
398 		sc->sc_data_iface_no = cmd->bDataInterface;
399 	}
400 
401 	device_printf(dev, "data interface %d, has %sCM over "
402 	    "data, has %sbreak\n",
403 	    sc->sc_data_iface_no,
404 	    sc->sc_cm_cap & USB_CDC_CM_OVER_DATA ? "" : "no ",
405 	    sc->sc_acm_cap & USB_CDC_ACM_HAS_BREAK ? "" : "no ");
406 
407 	/* get the data interface too */
408 
409 	for (i = 0;; i++) {
410 		struct usb_interface *iface;
411 		struct usb_interface_descriptor *id;
412 
413 		iface = usbd_get_iface(uaa->device, i);
414 
415 		if (iface) {
416 			id = usbd_get_interface_descriptor(iface);
417 
418 			if (id && (id->bInterfaceNumber == sc->sc_data_iface_no)) {
419 				sc->sc_iface_index[0] = i;
420 				usbd_set_parent_iface(uaa->device, i, uaa->info.bIfaceIndex);
421 				break;
422 			}
423 		} else {
424 			device_printf(dev, "no data interface\n");
425 			goto detach;
426 		}
427 	}
428 
429 	if (usb_test_quirk(uaa, UQ_ASSUME_CM_OVER_DATA)) {
430 		sc->sc_cm_over_data = 1;
431 	} else {
432 		if (sc->sc_cm_cap & USB_CDC_CM_OVER_DATA) {
433 			if (sc->sc_acm_cap & USB_CDC_ACM_HAS_FEATURE) {
434 				error = umodem_set_comm_feature
435 				(uaa->device, sc->sc_ctrl_iface_no,
436 				 UCDC_ABSTRACT_STATE, UCDC_DATA_MULTIPLEXED);
437 
438 				/* ignore any errors */
439 			}
440 			sc->sc_cm_over_data = 1;
441 		}
442 	}
443 	error = usbd_transfer_setup(uaa->device,
444 	    sc->sc_iface_index, sc->sc_xfer,
445 	    umodem_config, UMODEM_N_TRANSFER,
446 	    sc, &sc->sc_mtx);
447 	if (error) {
448 		device_printf(dev, "Can't setup transfer\n");
449 		goto detach;
450 	}
451 
452 	/* clear stall at first run, if USB host mode */
453 	if (uaa->usb_mode == USB_MODE_HOST) {
454 		mtx_lock(&sc->sc_mtx);
455 		usbd_xfer_set_stall(sc->sc_xfer[UMODEM_BULK_WR]);
456 		usbd_xfer_set_stall(sc->sc_xfer[UMODEM_BULK_RD]);
457 		mtx_unlock(&sc->sc_mtx);
458 	}
459 
460 	ucom_set_usb_mode(&sc->sc_super_ucom, uaa->usb_mode);
461 
462 	error = ucom_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc,
463 	    &umodem_callback, &sc->sc_mtx);
464 	if (error) {
465 		device_printf(dev, "Can't attach com\n");
466 		goto detach;
467 	}
468 	ucom_set_pnpinfo_usb(&sc->sc_super_ucom, dev);
469 
470 	return (0);
471 
472 detach:
473 	umodem_detach(dev);
474 	return (ENXIO);
475 }
476 
477 static void
478 umodem_find_data_iface(struct usb_attach_arg *uaa,
479     uint8_t iface_index, uint8_t *p_data_no, uint8_t *p_match_class)
480 {
481 	struct usb_interface_descriptor *id;
482 	struct usb_interface *iface;
483 
484 	iface = usbd_get_iface(uaa->device, iface_index);
485 
486 	/* check for end of interfaces */
487 	if (iface == NULL)
488 		return;
489 
490 	id = usbd_get_interface_descriptor(iface);
491 
492 	/* check for non-matching interface class */
493 	if (id->bInterfaceClass != UICLASS_CDC_DATA ||
494 	    id->bInterfaceSubClass != UISUBCLASS_DATA) {
495 		/* if we got a class match then return */
496 		if (*p_match_class)
497 			return;
498 	} else {
499 		*p_match_class = 1;
500 	}
501 
502 	DPRINTFN(11, "Match at index %u\n", iface_index);
503 
504 	*p_data_no = id->bInterfaceNumber;
505 }
506 
507 static void
508 umodem_start_read(struct ucom_softc *ucom)
509 {
510 	struct umodem_softc *sc = ucom->sc_parent;
511 
512 	/* start interrupt endpoint, if any */
513 	usbd_transfer_start(sc->sc_xfer[UMODEM_INTR_RD]);
514 
515 	/* start read endpoint */
516 	usbd_transfer_start(sc->sc_xfer[UMODEM_BULK_RD]);
517 }
518 
519 static void
520 umodem_stop_read(struct ucom_softc *ucom)
521 {
522 	struct umodem_softc *sc = ucom->sc_parent;
523 
524 	/* stop interrupt endpoint, if any */
525 	usbd_transfer_stop(sc->sc_xfer[UMODEM_INTR_RD]);
526 
527 	/* stop read endpoint */
528 	usbd_transfer_stop(sc->sc_xfer[UMODEM_BULK_RD]);
529 }
530 
531 static void
532 umodem_start_write(struct ucom_softc *ucom)
533 {
534 	struct umodem_softc *sc = ucom->sc_parent;
535 
536 	usbd_transfer_start(sc->sc_xfer[UMODEM_INTR_WR]);
537 	usbd_transfer_start(sc->sc_xfer[UMODEM_BULK_WR]);
538 }
539 
540 static void
541 umodem_stop_write(struct ucom_softc *ucom)
542 {
543 	struct umodem_softc *sc = ucom->sc_parent;
544 
545 	usbd_transfer_stop(sc->sc_xfer[UMODEM_INTR_WR]);
546 	usbd_transfer_stop(sc->sc_xfer[UMODEM_BULK_WR]);
547 }
548 
549 static void
550 umodem_get_caps(struct usb_attach_arg *uaa, uint8_t *cm, uint8_t *acm)
551 {
552 	struct usb_cdc_cm_descriptor *cmd;
553 	struct usb_cdc_acm_descriptor *cad;
554 
555 	cmd = umodem_get_desc(uaa, UDESC_CS_INTERFACE, UDESCSUB_CDC_CM);
556 	if ((cmd == NULL) || (cmd->bLength < sizeof(*cmd))) {
557 		DPRINTF("no CM desc (faking one)\n");
558 		*cm = USB_CDC_CM_DOES_CM | USB_CDC_CM_OVER_DATA;
559 	} else
560 		*cm = cmd->bmCapabilities;
561 
562 	cad = umodem_get_desc(uaa, UDESC_CS_INTERFACE, UDESCSUB_CDC_ACM);
563 	if ((cad == NULL) || (cad->bLength < sizeof(*cad))) {
564 		DPRINTF("no ACM desc\n");
565 		*acm = 0;
566 	} else
567 		*acm = cad->bmCapabilities;
568 }
569 
570 static void
571 umodem_cfg_get_status(struct ucom_softc *ucom, uint8_t *lsr, uint8_t *msr)
572 {
573 	struct umodem_softc *sc = ucom->sc_parent;
574 
575 	DPRINTF("\n");
576 
577 	/* XXX Note: sc_lsr is always zero */
578 	*lsr = sc->sc_lsr;
579 	*msr = sc->sc_msr;
580 }
581 
582 static int
583 umodem_pre_param(struct ucom_softc *ucom, struct termios *t)
584 {
585 	return (0);			/* we accept anything */
586 }
587 
588 static void
589 umodem_cfg_param(struct ucom_softc *ucom, struct termios *t)
590 {
591 	struct umodem_softc *sc = ucom->sc_parent;
592 	struct usb_cdc_line_state ls;
593 	struct usb_device_request req;
594 
595 	DPRINTF("sc=%p\n", sc);
596 
597 	memset(&ls, 0, sizeof(ls));
598 
599 	USETDW(ls.dwDTERate, t->c_ospeed);
600 
601 	ls.bCharFormat = (t->c_cflag & CSTOPB) ?
602 	    UCDC_STOP_BIT_2 : UCDC_STOP_BIT_1;
603 
604 	ls.bParityType = (t->c_cflag & PARENB) ?
605 	    ((t->c_cflag & PARODD) ?
606 	    UCDC_PARITY_ODD : UCDC_PARITY_EVEN) : UCDC_PARITY_NONE;
607 
608 	switch (t->c_cflag & CSIZE) {
609 	case CS5:
610 		ls.bDataBits = 5;
611 		break;
612 	case CS6:
613 		ls.bDataBits = 6;
614 		break;
615 	case CS7:
616 		ls.bDataBits = 7;
617 		break;
618 	case CS8:
619 		ls.bDataBits = 8;
620 		break;
621 	}
622 
623 	DPRINTF("rate=%d fmt=%d parity=%d bits=%d\n",
624 	    UGETDW(ls.dwDTERate), ls.bCharFormat,
625 	    ls.bParityType, ls.bDataBits);
626 
627 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
628 	req.bRequest = UCDC_SET_LINE_CODING;
629 	USETW(req.wValue, 0);
630 	req.wIndex[0] = sc->sc_ctrl_iface_no;
631 	req.wIndex[1] = 0;
632 	USETW(req.wLength, sizeof(ls));
633 
634 	ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
635 	    &req, &ls, 0, 1000);
636 }
637 
638 static int
639 umodem_ioctl(struct ucom_softc *ucom, uint32_t cmd, caddr_t data,
640     int flag, struct thread *td)
641 {
642 	struct umodem_softc *sc = ucom->sc_parent;
643 	int error = 0;
644 
645 	DPRINTF("cmd=0x%08x\n", cmd);
646 
647 	switch (cmd) {
648 	case USB_GET_CM_OVER_DATA:
649 		*(int *)data = sc->sc_cm_over_data;
650 		break;
651 
652 	case USB_SET_CM_OVER_DATA:
653 		if (*(int *)data != sc->sc_cm_over_data) {
654 			/* XXX change it */
655 		}
656 		break;
657 
658 	default:
659 		DPRINTF("unknown\n");
660 		error = ENOIOCTL;
661 		break;
662 	}
663 
664 	return (error);
665 }
666 
667 static void
668 umodem_cfg_set_dtr(struct ucom_softc *ucom, uint8_t onoff)
669 {
670 	struct umodem_softc *sc = ucom->sc_parent;
671 	struct usb_device_request req;
672 
673 	DPRINTF("onoff=%d\n", onoff);
674 
675 	if (onoff)
676 		sc->sc_line |= UCDC_LINE_DTR;
677 	else
678 		sc->sc_line &= ~UCDC_LINE_DTR;
679 
680 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
681 	req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
682 	USETW(req.wValue, sc->sc_line);
683 	req.wIndex[0] = sc->sc_ctrl_iface_no;
684 	req.wIndex[1] = 0;
685 	USETW(req.wLength, 0);
686 
687 	ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
688 	    &req, NULL, 0, 1000);
689 }
690 
691 static void
692 umodem_cfg_set_rts(struct ucom_softc *ucom, uint8_t onoff)
693 {
694 	struct umodem_softc *sc = ucom->sc_parent;
695 	struct usb_device_request req;
696 
697 	DPRINTF("onoff=%d\n", onoff);
698 
699 	if (onoff)
700 		sc->sc_line |= UCDC_LINE_RTS;
701 	else
702 		sc->sc_line &= ~UCDC_LINE_RTS;
703 
704 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
705 	req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
706 	USETW(req.wValue, sc->sc_line);
707 	req.wIndex[0] = sc->sc_ctrl_iface_no;
708 	req.wIndex[1] = 0;
709 	USETW(req.wLength, 0);
710 
711 	ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
712 	    &req, NULL, 0, 1000);
713 }
714 
715 static void
716 umodem_cfg_set_break(struct ucom_softc *ucom, uint8_t onoff)
717 {
718 	struct umodem_softc *sc = ucom->sc_parent;
719 	struct usb_device_request req;
720 	uint16_t temp;
721 
722 	DPRINTF("onoff=%d\n", onoff);
723 
724 	if (sc->sc_acm_cap & USB_CDC_ACM_HAS_BREAK) {
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 static void
858 umodem_write_callback(struct usb_xfer *xfer, usb_error_t error)
859 {
860 	struct umodem_softc *sc = usbd_xfer_softc(xfer);
861 	struct usb_page_cache *pc;
862 	uint32_t actlen;
863 
864 	switch (USB_GET_STATE(xfer)) {
865 	case USB_ST_SETUP:
866 	case USB_ST_TRANSFERRED:
867 tr_setup:
868 		pc = usbd_xfer_get_frame(xfer, 0);
869 		if (ucom_get_data(&sc->sc_ucom, pc, 0,
870 		    UMODEM_BUF_SIZE, &actlen)) {
871 			usbd_xfer_set_frame_len(xfer, 0, actlen);
872 			usbd_transfer_submit(xfer);
873 		}
874 		return;
875 
876 	default:			/* Error */
877 		if (error != USB_ERR_CANCELLED) {
878 			/* try to clear stall first */
879 			usbd_xfer_set_stall(xfer);
880 			goto tr_setup;
881 		}
882 		return;
883 	}
884 }
885 
886 static void
887 umodem_read_callback(struct usb_xfer *xfer, usb_error_t error)
888 {
889 	struct umodem_softc *sc = usbd_xfer_softc(xfer);
890 	struct usb_page_cache *pc;
891 	int actlen;
892 
893 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
894 
895 	switch (USB_GET_STATE(xfer)) {
896 	case USB_ST_TRANSFERRED:
897 
898 		DPRINTF("actlen=%d\n", actlen);
899 
900 		pc = usbd_xfer_get_frame(xfer, 0);
901 		ucom_put_data(&sc->sc_ucom, pc, 0, actlen);
902 
903 	case USB_ST_SETUP:
904 tr_setup:
905 		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
906 		usbd_transfer_submit(xfer);
907 		return;
908 
909 	default:			/* Error */
910 		if (error != USB_ERR_CANCELLED) {
911 			/* try to clear stall first */
912 			usbd_xfer_set_stall(xfer);
913 			goto tr_setup;
914 		}
915 		return;
916 	}
917 }
918 
919 static void *
920 umodem_get_desc(struct usb_attach_arg *uaa, uint8_t type, uint8_t subtype)
921 {
922 	return (usbd_find_descriptor(uaa->device, NULL, uaa->info.bIfaceIndex,
923 	    type, 0xFF, subtype, 0xFF));
924 }
925 
926 static usb_error_t
927 umodem_set_comm_feature(struct usb_device *udev, uint8_t iface_no,
928     uint16_t feature, uint16_t state)
929 {
930 	struct usb_device_request req;
931 	struct usb_cdc_abstract_state ast;
932 
933 	DPRINTF("feature=%d state=%d\n",
934 	    feature, state);
935 
936 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
937 	req.bRequest = UCDC_SET_COMM_FEATURE;
938 	USETW(req.wValue, feature);
939 	req.wIndex[0] = iface_no;
940 	req.wIndex[1] = 0;
941 	USETW(req.wLength, UCDC_ABSTRACT_STATE_LENGTH);
942 	USETW(ast.wState, state);
943 
944 	return (usbd_do_request(udev, NULL, &req, &ast));
945 }
946 
947 static int
948 umodem_detach(device_t dev)
949 {
950 	struct umodem_softc *sc = device_get_softc(dev);
951 
952 	DPRINTF("sc=%p\n", sc);
953 
954 	ucom_detach(&sc->sc_super_ucom, &sc->sc_ucom);
955 	usbd_transfer_unsetup(sc->sc_xfer, UMODEM_N_TRANSFER);
956 
957 	device_claim_softc(dev);
958 
959 	umodem_free_softc(sc);
960 
961 	return (0);
962 }
963 
964 UCOM_UNLOAD_DRAIN(umodem);
965 
966 static void
967 umodem_free_softc(struct umodem_softc *sc)
968 {
969 	if (ucom_unref(&sc->sc_super_ucom)) {
970 		mtx_destroy(&sc->sc_mtx);
971 		device_free_softc(sc);
972 	}
973 }
974 
975 static void
976 umodem_free(struct ucom_softc *ucom)
977 {
978 	umodem_free_softc(ucom->sc_parent);
979 }
980 
981 static void
982 umodem_poll(struct ucom_softc *ucom)
983 {
984 	struct umodem_softc *sc = ucom->sc_parent;
985 	usbd_transfer_poll(sc->sc_xfer, UMODEM_N_TRANSFER);
986 }
987 
988 static int
989 umodem_handle_request(device_t dev,
990     const void *preq, void **pptr, uint16_t *plen,
991     uint16_t offset, uint8_t *pstate)
992 {
993 	struct umodem_softc *sc = device_get_softc(dev);
994 	const struct usb_device_request *req = preq;
995 	uint8_t is_complete = *pstate;
996 
997 	DPRINTF("sc=%p\n", sc);
998 
999 	if (!is_complete) {
1000 		if ((req->bmRequestType == UT_WRITE_CLASS_INTERFACE) &&
1001 		    (req->bRequest == UCDC_SET_LINE_CODING) &&
1002 		    (req->wIndex[0] == sc->sc_ctrl_iface_no) &&
1003 		    (req->wIndex[1] == 0x00) &&
1004 		    (req->wValue[0] == 0x00) &&
1005 		    (req->wValue[1] == 0x00)) {
1006 			if (offset == 0) {
1007 				*plen = sizeof(sc->sc_line_coding);
1008 				*pptr = &sc->sc_line_coding;
1009 			} else {
1010 				*plen = 0;
1011 			}
1012 			return (0);
1013 		} else if ((req->bmRequestType == UT_WRITE_CLASS_INTERFACE) &&
1014 		    (req->wIndex[0] == sc->sc_ctrl_iface_no) &&
1015 		    (req->wIndex[1] == 0x00) &&
1016 		    (req->bRequest == UCDC_SET_COMM_FEATURE)) {
1017 			if (offset == 0) {
1018 				*plen = sizeof(sc->sc_abstract_state);
1019 				*pptr = &sc->sc_abstract_state;
1020 			} else {
1021 				*plen = 0;
1022 			}
1023 			return (0);
1024 		} else if ((req->bmRequestType == UT_WRITE_CLASS_INTERFACE) &&
1025 		    (req->wIndex[0] == sc->sc_ctrl_iface_no) &&
1026 		    (req->wIndex[1] == 0x00) &&
1027 		    (req->bRequest == UCDC_SET_CONTROL_LINE_STATE)) {
1028 			*plen = 0;
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_SEND_BREAK)) {
1034 			*plen = 0;
1035 			return (0);
1036 		}
1037 	}
1038 	return (ENXIO);			/* use builtin handler */
1039 }
1040