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