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