xref: /freebsd/sys/dev/usb/serial/umodem.c (revision 830940567b49bb0c08dfaed40418999e76616909)
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  * 3. All advertising materials mentioning features or use of this software
49  *    must display the following acknowledgement:
50  *        This product includes software developed by the NetBSD
51  *        Foundation, Inc. and its contributors.
52  * 4. Neither the name of The NetBSD Foundation nor the names of its
53  *    contributors may be used to endorse or promote products derived
54  *    from this software without specific prior written permission.
55  *
56  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
57  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
58  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
59  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
60  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
61  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
62  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
63  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
64  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
65  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
66  * POSSIBILITY OF SUCH DAMAGE.
67  */
68 
69 /*
70  * Comm Class spec:  http://www.usb.org/developers/devclass_docs/usbccs10.pdf
71  *                   http://www.usb.org/developers/devclass_docs/usbcdc11.pdf
72  *                   http://www.usb.org/developers/devclass_docs/cdc_wmc10.zip
73  */
74 
75 /*
76  * TODO:
77  * - Add error recovery in various places; the big problem is what
78  *   to do in a callback if there is an error.
79  * - Implement a Call Device for modems without multiplexed commands.
80  *
81  */
82 
83 #include <sys/stdint.h>
84 #include <sys/stddef.h>
85 #include <sys/param.h>
86 #include <sys/queue.h>
87 #include <sys/types.h>
88 #include <sys/systm.h>
89 #include <sys/kernel.h>
90 #include <sys/bus.h>
91 #include <sys/linker_set.h>
92 #include <sys/module.h>
93 #include <sys/lock.h>
94 #include <sys/mutex.h>
95 #include <sys/condvar.h>
96 #include <sys/sysctl.h>
97 #include <sys/sx.h>
98 #include <sys/unistd.h>
99 #include <sys/callout.h>
100 #include <sys/malloc.h>
101 #include <sys/priv.h>
102 
103 #include <dev/usb/usb.h>
104 #include <dev/usb/usbdi.h>
105 #include <dev/usb/usbdi_util.h>
106 #include <dev/usb/usbhid.h>
107 #include <dev/usb/usb_cdc.h>
108 #include "usbdevs.h"
109 
110 #include <dev/usb/usb_ioctl.h>
111 
112 #define	USB_DEBUG_VAR umodem_debug
113 #include <dev/usb/usb_debug.h>
114 #include <dev/usb/usb_process.h>
115 
116 #include <dev/usb/serial/usb_serial.h>
117 
118 #if USB_DEBUG
119 static int umodem_debug = 0;
120 
121 SYSCTL_NODE(_hw_usb, OID_AUTO, umodem, CTLFLAG_RW, 0, "USB umodem");
122 SYSCTL_INT(_hw_usb_umodem, OID_AUTO, debug, CTLFLAG_RW,
123     &umodem_debug, 0, "Debug level");
124 #endif
125 
126 static const struct usb_device_id umodem_devs[] = {
127 	/* Generic Modem class match */
128 	{USB_IFACE_CLASS(UICLASS_CDC),
129 		USB_IFACE_SUBCLASS(UISUBCLASS_ABSTRACT_CONTROL_MODEL),
130 	USB_IFACE_PROTOCOL(UIPROTO_CDC_AT)},
131 	/* Kyocera AH-K3001V */
132 	{USB_VPI(USB_VENDOR_KYOCERA, USB_PRODUCT_KYOCERA_AHK3001V, 1)},
133 	{USB_VPI(USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC5720, 1)},
134 	{USB_VPI(USB_VENDOR_CURITEL, USB_PRODUCT_CURITEL_PC5740, 1)},
135 };
136 
137 /*
138  * As speeds for umodem deivces increase, these numbers will need to
139  * be increased. They should be good for G3 speeds and below.
140  *
141  * TODO: The TTY buffers should be increased!
142  */
143 #define	UMODEM_BUF_SIZE 1024
144 
145 enum {
146 	UMODEM_BULK_WR,
147 	UMODEM_BULK_RD,
148 	UMODEM_INTR_RD,
149 	UMODEM_N_TRANSFER,
150 };
151 
152 #define	UMODEM_MODVER			1	/* module version */
153 
154 struct umodem_softc {
155 	struct ucom_super_softc sc_super_ucom;
156 	struct ucom_softc sc_ucom;
157 
158 	struct usb_xfer *sc_xfer[UMODEM_N_TRANSFER];
159 	struct usb_device *sc_udev;
160 	struct mtx sc_mtx;
161 
162 	uint16_t sc_line;
163 
164 	uint8_t	sc_lsr;			/* local status register */
165 	uint8_t	sc_msr;			/* modem status register */
166 	uint8_t	sc_ctrl_iface_no;
167 	uint8_t	sc_data_iface_no;
168 	uint8_t sc_iface_index[2];
169 	uint8_t	sc_cm_over_data;
170 	uint8_t	sc_cm_cap;		/* CM capabilities */
171 	uint8_t	sc_acm_cap;		/* ACM capabilities */
172 };
173 
174 static device_probe_t umodem_probe;
175 static device_attach_t umodem_attach;
176 static device_detach_t umodem_detach;
177 
178 static usb_callback_t umodem_intr_callback;
179 static usb_callback_t umodem_write_callback;
180 static usb_callback_t umodem_read_callback;
181 
182 static void	umodem_start_read(struct ucom_softc *);
183 static void	umodem_stop_read(struct ucom_softc *);
184 static void	umodem_start_write(struct ucom_softc *);
185 static void	umodem_stop_write(struct ucom_softc *);
186 static void	umodem_get_caps(struct usb_attach_arg *, uint8_t *, uint8_t *);
187 static void	umodem_cfg_get_status(struct ucom_softc *, uint8_t *,
188 		    uint8_t *);
189 static int	umodem_pre_param(struct ucom_softc *, struct termios *);
190 static void	umodem_cfg_param(struct ucom_softc *, struct termios *);
191 static int	umodem_ioctl(struct ucom_softc *, uint32_t, caddr_t, int,
192 		    struct thread *);
193 static void	umodem_cfg_set_dtr(struct ucom_softc *, uint8_t);
194 static void	umodem_cfg_set_rts(struct ucom_softc *, uint8_t);
195 static void	umodem_cfg_set_break(struct ucom_softc *, uint8_t);
196 static void	*umodem_get_desc(struct usb_attach_arg *, uint8_t, uint8_t);
197 static usb_error_t umodem_set_comm_feature(struct usb_device *, uint8_t,
198 		    uint16_t, uint16_t);
199 
200 static const struct usb_config umodem_config[UMODEM_N_TRANSFER] = {
201 
202 	[UMODEM_BULK_WR] = {
203 		.type = UE_BULK,
204 		.endpoint = UE_ADDR_ANY,
205 		.direction = UE_DIR_OUT,
206 		.if_index = 0,
207 		.bufsize = UMODEM_BUF_SIZE,
208 		.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
209 		.callback = &umodem_write_callback,
210 	},
211 
212 	[UMODEM_BULK_RD] = {
213 		.type = UE_BULK,
214 		.endpoint = UE_ADDR_ANY,
215 		.direction = UE_DIR_IN,
216 		.if_index = 0,
217 		.bufsize = UMODEM_BUF_SIZE,
218 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
219 		.callback = &umodem_read_callback,
220 	},
221 
222 	[UMODEM_INTR_RD] = {
223 		.type = UE_INTERRUPT,
224 		.endpoint = UE_ADDR_ANY,
225 		.direction = UE_DIR_IN,
226 		.if_index = 1,
227 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,.no_pipe_ok = 1,},
228 		.bufsize = 0,	/* use wMaxPacketSize */
229 		.callback = &umodem_intr_callback,
230 	},
231 };
232 
233 static const struct ucom_callback umodem_callback = {
234 	.ucom_cfg_get_status = &umodem_cfg_get_status,
235 	.ucom_cfg_set_dtr = &umodem_cfg_set_dtr,
236 	.ucom_cfg_set_rts = &umodem_cfg_set_rts,
237 	.ucom_cfg_set_break = &umodem_cfg_set_break,
238 	.ucom_cfg_param = &umodem_cfg_param,
239 	.ucom_pre_param = &umodem_pre_param,
240 	.ucom_ioctl = &umodem_ioctl,
241 	.ucom_start_read = &umodem_start_read,
242 	.ucom_stop_read = &umodem_stop_read,
243 	.ucom_start_write = &umodem_start_write,
244 	.ucom_stop_write = &umodem_stop_write,
245 };
246 
247 static device_method_t umodem_methods[] = {
248 	DEVMETHOD(device_probe, umodem_probe),
249 	DEVMETHOD(device_attach, umodem_attach),
250 	DEVMETHOD(device_detach, umodem_detach),
251 	{0, 0}
252 };
253 
254 static devclass_t umodem_devclass;
255 
256 static driver_t umodem_driver = {
257 	.name = "umodem",
258 	.methods = umodem_methods,
259 	.size = sizeof(struct umodem_softc),
260 };
261 
262 DRIVER_MODULE(umodem, uhub, umodem_driver, umodem_devclass, NULL, 0);
263 MODULE_DEPEND(umodem, ucom, 1, 1, 1);
264 MODULE_DEPEND(umodem, usb, 1, 1, 1);
265 MODULE_VERSION(umodem, UMODEM_MODVER);
266 
267 static int
268 umodem_probe(device_t dev)
269 {
270 	struct usb_attach_arg *uaa = device_get_ivars(dev);
271 	int error;
272 
273 	DPRINTFN(11, "\n");
274 
275 	if (uaa->usb_mode != USB_MODE_HOST) {
276 		return (ENXIO);
277 	}
278 	error = usbd_lookup_id_by_uaa(umodem_devs, sizeof(umodem_devs), uaa);
279 	return (error);
280 }
281 
282 static int
283 umodem_attach(device_t dev)
284 {
285 	struct usb_attach_arg *uaa = device_get_ivars(dev);
286 	struct umodem_softc *sc = device_get_softc(dev);
287 	struct usb_cdc_cm_descriptor *cmd;
288 	struct usb_cdc_union_descriptor *cud;
289 	uint8_t i;
290 	int error;
291 
292 	device_set_usb_desc(dev);
293 	mtx_init(&sc->sc_mtx, "umodem", NULL, MTX_DEF);
294 
295 	sc->sc_ctrl_iface_no = uaa->info.bIfaceNum;
296 	sc->sc_iface_index[1] = uaa->info.bIfaceIndex;
297 	sc->sc_udev = uaa->device;
298 
299 	umodem_get_caps(uaa, &sc->sc_cm_cap, &sc->sc_acm_cap);
300 
301 	/* get the data interface number */
302 
303 	cmd = umodem_get_desc(uaa, UDESC_CS_INTERFACE, UDESCSUB_CDC_CM);
304 
305 	if ((cmd == NULL) || (cmd->bLength < sizeof(*cmd))) {
306 
307 		cud = usbd_find_descriptor(uaa->device, NULL,
308 		    uaa->info.bIfaceIndex, UDESC_CS_INTERFACE,
309 		    0 - 1, UDESCSUB_CDC_UNION, 0 - 1);
310 
311 		if ((cud == NULL) || (cud->bLength < sizeof(*cud))) {
312 			device_printf(dev, "no CM or union descriptor!\n");
313 			goto detach;
314 		}
315 
316 		sc->sc_data_iface_no = cud->bSlaveInterface[0];
317 	} else {
318 		sc->sc_data_iface_no = cmd->bDataInterface;
319 	}
320 
321 	device_printf(dev, "data interface %d, has %sCM over "
322 	    "data, has %sbreak\n",
323 	    sc->sc_data_iface_no,
324 	    sc->sc_cm_cap & USB_CDC_CM_OVER_DATA ? "" : "no ",
325 	    sc->sc_acm_cap & USB_CDC_ACM_HAS_BREAK ? "" : "no ");
326 
327 	/* get the data interface too */
328 
329 	for (i = 0;; i++) {
330 		struct usb_interface *iface;
331 		struct usb_interface_descriptor *id;
332 
333 		iface = usbd_get_iface(uaa->device, i);
334 
335 		if (iface) {
336 
337 			id = usbd_get_interface_descriptor(iface);
338 
339 			if (id && (id->bInterfaceNumber == sc->sc_data_iface_no)) {
340 				sc->sc_iface_index[0] = i;
341 				usbd_set_parent_iface(uaa->device, i, uaa->info.bIfaceIndex);
342 				break;
343 			}
344 		} else {
345 			device_printf(dev, "no data interface!\n");
346 			goto detach;
347 		}
348 	}
349 
350 	if (sc->sc_cm_cap & USB_CDC_CM_OVER_DATA) {
351 		if (sc->sc_acm_cap & USB_CDC_ACM_HAS_FEATURE) {
352 
353 			error = umodem_set_comm_feature
354 			    (uaa->device, sc->sc_ctrl_iface_no,
355 			    UCDC_ABSTRACT_STATE, UCDC_DATA_MULTIPLEXED);
356 
357 			/* ignore any errors */
358 		}
359 		sc->sc_cm_over_data = 1;
360 	}
361 	error = usbd_transfer_setup(uaa->device,
362 	    sc->sc_iface_index, sc->sc_xfer,
363 	    umodem_config, UMODEM_N_TRANSFER,
364 	    sc, &sc->sc_mtx);
365 	if (error) {
366 		goto detach;
367 	}
368 
369 	/* clear stall at first run */
370 	mtx_lock(&sc->sc_mtx);
371 	usbd_xfer_set_stall(sc->sc_xfer[UMODEM_BULK_WR]);
372 	usbd_xfer_set_stall(sc->sc_xfer[UMODEM_BULK_RD]);
373 	mtx_unlock(&sc->sc_mtx);
374 
375 	error = ucom_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc,
376 	    &umodem_callback, &sc->sc_mtx);
377 	if (error) {
378 		goto detach;
379 	}
380 	return (0);
381 
382 detach:
383 	umodem_detach(dev);
384 	return (ENXIO);
385 }
386 
387 static void
388 umodem_start_read(struct ucom_softc *ucom)
389 {
390 	struct umodem_softc *sc = ucom->sc_parent;
391 
392 	/* start interrupt endpoint, if any */
393 	usbd_transfer_start(sc->sc_xfer[UMODEM_INTR_RD]);
394 
395 	/* start read endpoint */
396 	usbd_transfer_start(sc->sc_xfer[UMODEM_BULK_RD]);
397 }
398 
399 static void
400 umodem_stop_read(struct ucom_softc *ucom)
401 {
402 	struct umodem_softc *sc = ucom->sc_parent;
403 
404 	/* stop interrupt endpoint, if any */
405 	usbd_transfer_stop(sc->sc_xfer[UMODEM_INTR_RD]);
406 
407 	/* stop read endpoint */
408 	usbd_transfer_stop(sc->sc_xfer[UMODEM_BULK_RD]);
409 }
410 
411 static void
412 umodem_start_write(struct ucom_softc *ucom)
413 {
414 	struct umodem_softc *sc = ucom->sc_parent;
415 
416 	usbd_transfer_start(sc->sc_xfer[UMODEM_BULK_WR]);
417 }
418 
419 static void
420 umodem_stop_write(struct ucom_softc *ucom)
421 {
422 	struct umodem_softc *sc = ucom->sc_parent;
423 
424 	usbd_transfer_stop(sc->sc_xfer[UMODEM_BULK_WR]);
425 }
426 
427 static void
428 umodem_get_caps(struct usb_attach_arg *uaa, uint8_t *cm, uint8_t *acm)
429 {
430 	struct usb_cdc_cm_descriptor *cmd;
431 	struct usb_cdc_acm_descriptor *cad;
432 
433 	cmd = umodem_get_desc(uaa, UDESC_CS_INTERFACE, UDESCSUB_CDC_CM);
434 	if ((cmd == NULL) || (cmd->bLength < sizeof(*cmd))) {
435 		DPRINTF("no CM desc (faking one)\n");
436 		*cm = USB_CDC_CM_DOES_CM | USB_CDC_CM_OVER_DATA;
437 	} else
438 		*cm = cmd->bmCapabilities;
439 
440 	cad = umodem_get_desc(uaa, UDESC_CS_INTERFACE, UDESCSUB_CDC_ACM);
441 	if ((cad == NULL) || (cad->bLength < sizeof(*cad))) {
442 		DPRINTF("no ACM desc\n");
443 		*acm = 0;
444 	} else
445 		*acm = cad->bmCapabilities;
446 }
447 
448 static void
449 umodem_cfg_get_status(struct ucom_softc *ucom, uint8_t *lsr, uint8_t *msr)
450 {
451 	struct umodem_softc *sc = ucom->sc_parent;
452 
453 	DPRINTF("\n");
454 
455 	*lsr = sc->sc_lsr;
456 	*msr = sc->sc_msr;
457 }
458 
459 static int
460 umodem_pre_param(struct ucom_softc *ucom, struct termios *t)
461 {
462 	return (0);			/* we accept anything */
463 }
464 
465 static void
466 umodem_cfg_param(struct ucom_softc *ucom, struct termios *t)
467 {
468 	struct umodem_softc *sc = ucom->sc_parent;
469 	struct usb_cdc_line_state ls;
470 	struct usb_device_request req;
471 
472 	DPRINTF("sc=%p\n", sc);
473 
474 	bzero(&ls, sizeof(ls));
475 
476 	USETDW(ls.dwDTERate, t->c_ospeed);
477 
478 	ls.bCharFormat = (t->c_cflag & CSTOPB) ?
479 	    UCDC_STOP_BIT_2 : UCDC_STOP_BIT_1;
480 
481 	ls.bParityType = (t->c_cflag & PARENB) ?
482 	    ((t->c_cflag & PARODD) ?
483 	    UCDC_PARITY_ODD : UCDC_PARITY_EVEN) : UCDC_PARITY_NONE;
484 
485 	switch (t->c_cflag & CSIZE) {
486 	case CS5:
487 		ls.bDataBits = 5;
488 		break;
489 	case CS6:
490 		ls.bDataBits = 6;
491 		break;
492 	case CS7:
493 		ls.bDataBits = 7;
494 		break;
495 	case CS8:
496 		ls.bDataBits = 8;
497 		break;
498 	}
499 
500 	DPRINTF("rate=%d fmt=%d parity=%d bits=%d\n",
501 	    UGETDW(ls.dwDTERate), ls.bCharFormat,
502 	    ls.bParityType, ls.bDataBits);
503 
504 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
505 	req.bRequest = UCDC_SET_LINE_CODING;
506 	USETW(req.wValue, 0);
507 	req.wIndex[0] = sc->sc_ctrl_iface_no;
508 	req.wIndex[1] = 0;
509 	USETW(req.wLength, sizeof(ls));
510 
511 	ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
512 	    &req, &ls, 0, 1000);
513 }
514 
515 static int
516 umodem_ioctl(struct ucom_softc *ucom, uint32_t cmd, caddr_t data,
517     int flag, struct thread *td)
518 {
519 	struct umodem_softc *sc = ucom->sc_parent;
520 	int error = 0;
521 
522 	DPRINTF("cmd=0x%08x\n", cmd);
523 
524 	switch (cmd) {
525 	case USB_GET_CM_OVER_DATA:
526 		*(int *)data = sc->sc_cm_over_data;
527 		break;
528 
529 	case USB_SET_CM_OVER_DATA:
530 		if (*(int *)data != sc->sc_cm_over_data) {
531 			/* XXX change it */
532 		}
533 		break;
534 
535 	default:
536 		DPRINTF("unknown\n");
537 		error = ENOIOCTL;
538 		break;
539 	}
540 
541 	return (error);
542 }
543 
544 static void
545 umodem_cfg_set_dtr(struct ucom_softc *ucom, uint8_t onoff)
546 {
547 	struct umodem_softc *sc = ucom->sc_parent;
548 	struct usb_device_request req;
549 
550 	DPRINTF("onoff=%d\n", onoff);
551 
552 	if (onoff)
553 		sc->sc_line |= UCDC_LINE_DTR;
554 	else
555 		sc->sc_line &= ~UCDC_LINE_DTR;
556 
557 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
558 	req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
559 	USETW(req.wValue, sc->sc_line);
560 	req.wIndex[0] = sc->sc_ctrl_iface_no;
561 	req.wIndex[1] = 0;
562 	USETW(req.wLength, 0);
563 
564 	ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
565 	    &req, NULL, 0, 1000);
566 }
567 
568 static void
569 umodem_cfg_set_rts(struct ucom_softc *ucom, uint8_t onoff)
570 {
571 	struct umodem_softc *sc = ucom->sc_parent;
572 	struct usb_device_request req;
573 
574 	DPRINTF("onoff=%d\n", onoff);
575 
576 	if (onoff)
577 		sc->sc_line |= UCDC_LINE_RTS;
578 	else
579 		sc->sc_line &= ~UCDC_LINE_RTS;
580 
581 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
582 	req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
583 	USETW(req.wValue, sc->sc_line);
584 	req.wIndex[0] = sc->sc_ctrl_iface_no;
585 	req.wIndex[1] = 0;
586 	USETW(req.wLength, 0);
587 
588 	ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
589 	    &req, NULL, 0, 1000);
590 }
591 
592 static void
593 umodem_cfg_set_break(struct ucom_softc *ucom, uint8_t onoff)
594 {
595 	struct umodem_softc *sc = ucom->sc_parent;
596 	struct usb_device_request req;
597 	uint16_t temp;
598 
599 	DPRINTF("onoff=%d\n", onoff);
600 
601 	if (sc->sc_acm_cap & USB_CDC_ACM_HAS_BREAK) {
602 
603 		temp = onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF;
604 
605 		req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
606 		req.bRequest = UCDC_SEND_BREAK;
607 		USETW(req.wValue, temp);
608 		req.wIndex[0] = sc->sc_ctrl_iface_no;
609 		req.wIndex[1] = 0;
610 		USETW(req.wLength, 0);
611 
612 		ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
613 		    &req, NULL, 0, 1000);
614 	}
615 }
616 
617 static void
618 umodem_intr_callback(struct usb_xfer *xfer, usb_error_t error)
619 {
620 	struct usb_cdc_notification pkt;
621 	struct umodem_softc *sc = usbd_xfer_softc(xfer);
622 	struct usb_page_cache *pc;
623 	uint16_t wLen;
624 	int actlen;
625 
626 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
627 
628 	switch (USB_GET_STATE(xfer)) {
629 	case USB_ST_TRANSFERRED:
630 
631 		if (actlen < 8) {
632 			DPRINTF("received short packet, "
633 			    "%d bytes\n", actlen);
634 			goto tr_setup;
635 		}
636 		if (actlen > sizeof(pkt)) {
637 			DPRINTF("truncating message\n");
638 			actlen = sizeof(pkt);
639 		}
640 		pc = usbd_xfer_get_frame(xfer, 0);
641 		usbd_copy_out(pc, 0, &pkt, actlen);
642 
643 		actlen -= 8;
644 
645 		wLen = UGETW(pkt.wLength);
646 		if (actlen > wLen) {
647 			actlen = wLen;
648 		}
649 		if (pkt.bmRequestType != UCDC_NOTIFICATION) {
650 			DPRINTF("unknown message type, "
651 			    "0x%02x, on notify pipe!\n",
652 			    pkt.bmRequestType);
653 			goto tr_setup;
654 		}
655 		switch (pkt.bNotification) {
656 		case UCDC_N_SERIAL_STATE:
657 			/*
658 			 * Set the serial state in ucom driver based on
659 			 * the bits from the notify message
660 			 */
661 			if (actlen < 2) {
662 				DPRINTF("invalid notification "
663 				    "length, %d bytes!\n", actlen);
664 				break;
665 			}
666 			DPRINTF("notify bytes = %02x%02x\n",
667 			    pkt.data[0],
668 			    pkt.data[1]);
669 
670 			/* Currently, lsr is always zero. */
671 			sc->sc_lsr = 0;
672 			sc->sc_msr = 0;
673 
674 			if (pkt.data[0] & UCDC_N_SERIAL_RI) {
675 				sc->sc_msr |= SER_RI;
676 			}
677 			if (pkt.data[0] & UCDC_N_SERIAL_DSR) {
678 				sc->sc_msr |= SER_DSR;
679 			}
680 			if (pkt.data[0] & UCDC_N_SERIAL_DCD) {
681 				sc->sc_msr |= SER_DCD;
682 			}
683 			ucom_status_change(&sc->sc_ucom);
684 			break;
685 
686 		default:
687 			DPRINTF("unknown notify message: 0x%02x\n",
688 			    pkt.bNotification);
689 			break;
690 		}
691 
692 	case USB_ST_SETUP:
693 tr_setup:
694 		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
695 		usbd_transfer_submit(xfer);
696 		return;
697 
698 	default:			/* Error */
699 		if (error != USB_ERR_CANCELLED) {
700 			/* try to clear stall first */
701 			usbd_xfer_set_stall(xfer);
702 			goto tr_setup;
703 		}
704 		return;
705 
706 	}
707 }
708 
709 static void
710 umodem_write_callback(struct usb_xfer *xfer, usb_error_t error)
711 {
712 	struct umodem_softc *sc = usbd_xfer_softc(xfer);
713 	struct usb_page_cache *pc;
714 	uint32_t actlen;
715 
716 	switch (USB_GET_STATE(xfer)) {
717 	case USB_ST_SETUP:
718 	case USB_ST_TRANSFERRED:
719 tr_setup:
720 		pc = usbd_xfer_get_frame(xfer, 0);
721 		if (ucom_get_data(&sc->sc_ucom, pc, 0,
722 		    UMODEM_BUF_SIZE, &actlen)) {
723 
724 			usbd_xfer_set_frame_len(xfer, 0, actlen);
725 			usbd_transfer_submit(xfer);
726 		}
727 		return;
728 
729 	default:			/* Error */
730 		if (error != USB_ERR_CANCELLED) {
731 			/* try to clear stall first */
732 			usbd_xfer_set_stall(xfer);
733 			goto tr_setup;
734 		}
735 		return;
736 	}
737 }
738 
739 static void
740 umodem_read_callback(struct usb_xfer *xfer, usb_error_t error)
741 {
742 	struct umodem_softc *sc = usbd_xfer_softc(xfer);
743 	struct usb_page_cache *pc;
744 	int actlen;
745 
746 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
747 
748 	switch (USB_GET_STATE(xfer)) {
749 	case USB_ST_TRANSFERRED:
750 
751 		DPRINTF("actlen=%d\n", actlen);
752 
753 		pc = usbd_xfer_get_frame(xfer, 0);
754 		ucom_put_data(&sc->sc_ucom, pc, 0, actlen);
755 
756 	case USB_ST_SETUP:
757 tr_setup:
758 		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
759 		usbd_transfer_submit(xfer);
760 		return;
761 
762 	default:			/* Error */
763 		if (error != USB_ERR_CANCELLED) {
764 			/* try to clear stall first */
765 			usbd_xfer_set_stall(xfer);
766 			goto tr_setup;
767 		}
768 		return;
769 	}
770 }
771 
772 static void *
773 umodem_get_desc(struct usb_attach_arg *uaa, uint8_t type, uint8_t subtype)
774 {
775 	return (usbd_find_descriptor(uaa->device, NULL, uaa->info.bIfaceIndex,
776 	    type, 0 - 1, subtype, 0 - 1));
777 }
778 
779 static usb_error_t
780 umodem_set_comm_feature(struct usb_device *udev, uint8_t iface_no,
781     uint16_t feature, uint16_t state)
782 {
783 	struct usb_device_request req;
784 	struct usb_cdc_abstract_state ast;
785 
786 	DPRINTF("feature=%d state=%d\n",
787 	    feature, state);
788 
789 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
790 	req.bRequest = UCDC_SET_COMM_FEATURE;
791 	USETW(req.wValue, feature);
792 	req.wIndex[0] = iface_no;
793 	req.wIndex[1] = 0;
794 	USETW(req.wLength, UCDC_ABSTRACT_STATE_LENGTH);
795 	USETW(ast.wState, state);
796 
797 	return (usbd_do_request(udev, NULL, &req, &ast));
798 }
799 
800 static int
801 umodem_detach(device_t dev)
802 {
803 	struct umodem_softc *sc = device_get_softc(dev);
804 
805 	DPRINTF("sc=%p\n", sc);
806 
807 	ucom_detach(&sc->sc_super_ucom, &sc->sc_ucom, 1);
808 	usbd_transfer_unsetup(sc->sc_xfer, UMODEM_N_TRANSFER);
809 	mtx_destroy(&sc->sc_mtx);
810 
811 	return (0);
812 }
813