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