xref: /freebsd/sys/dev/usb/serial/ufoma.c (revision 43a5ec4eb41567cc92586503212743d89686d78f)
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 #define UFOMA_HANDSFREE
6 /*-
7  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD AND BSD-2-Clause-NetBSD
8  *
9  * Copyright (c) 2005, Takanori Watanabe All rights reserved.
10  * Copyright (c) 2003 M. Warner Losh <imp@FreeBSD.org>
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 /*-
35  * Copyright (c) 1998 The NetBSD Foundation, Inc.
36  * All rights reserved.
37  *
38  * This code is derived from software contributed to The NetBSD Foundation
39  * by Lennart Augustsson (lennart@augustsson.net) at
40  * Carlstedt Research & Technology.
41  *
42  * Redistribution and use in source and binary forms, with or without
43  * modification, are permitted provided that the following conditions
44  * are met:
45  * 1. Redistributions of source code must retain the above copyright
46  *    notice, this list of conditions and the following disclaimer.
47  * 2. Redistributions in binary form must reproduce the above copyright
48  *    notice, this list of conditions and the following disclaimer in the
49  *    documentation and/or other materials provided with the distribution.
50  *
51  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
52  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
53  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
54  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
55  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
56  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
57  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
58  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
59  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
60  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
61  * POSSIBILITY OF SUCH DAMAGE.
62  */
63 
64 /*
65  * Comm Class spec:  http://www.usb.org/developers/devclass_docs/usbccs10.pdf
66  *                   http://www.usb.org/developers/devclass_docs/usbcdc11.pdf
67  */
68 
69 /*
70  * TODO:
71  * - Implement a Call Device for modems without multiplexed commands.
72  */
73 
74 /*
75  * NOTE: all function names beginning like "ufoma_cfg_" can only
76  * be called from within the config thread function !
77  */
78 
79 #include <sys/stdint.h>
80 #include <sys/stddef.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/mutex.h>
90 #include <sys/condvar.h>
91 #include <sys/sysctl.h>
92 #include <sys/sx.h>
93 #include <sys/unistd.h>
94 #include <sys/callout.h>
95 #include <sys/malloc.h>
96 #include <sys/priv.h>
97 #include <sys/sbuf.h>
98 
99 #include <dev/usb/usb.h>
100 #include <dev/usb/usbdi.h>
101 #include <dev/usb/usbdi_util.h>
102 #include <dev/usb/usb_cdc.h>
103 #include "usbdevs.h"
104 
105 #define	USB_DEBUG_VAR usb_debug
106 #include <dev/usb/usb_debug.h>
107 #include <dev/usb/usb_process.h>
108 
109 #include <dev/usb/serial/usb_serial.h>
110 
111 typedef struct ufoma_mobile_acm_descriptor {
112 	uint8_t	bFunctionLength;
113 	uint8_t	bDescriptorType;
114 	uint8_t	bDescriptorSubtype;
115 	uint8_t	bType;
116 	uint8_t	bMode[1];
117 } __packed usb_mcpc_acm_descriptor;
118 
119 #define	UISUBCLASS_MCPC 0x88
120 
121 #define	UDESC_VS_INTERFACE 0x44
122 #define	UDESCSUB_MCPC_ACM  0x11
123 
124 #define	UMCPC_ACM_TYPE_AB1 0x1
125 #define	UMCPC_ACM_TYPE_AB2 0x2
126 #define	UMCPC_ACM_TYPE_AB5 0x5
127 #define	UMCPC_ACM_TYPE_AB6 0x6
128 
129 #define	UMCPC_ACM_MODE_DEACTIVATED 0x0
130 #define	UMCPC_ACM_MODE_MODEM 0x1
131 #define	UMCPC_ACM_MODE_ATCOMMAND 0x2
132 #define	UMCPC_ACM_MODE_OBEX 0x60
133 #define	UMCPC_ACM_MODE_VENDOR1 0xc0
134 #define	UMCPC_ACM_MODE_VENDOR2 0xfe
135 #define	UMCPC_ACM_MODE_UNLINKED 0xff
136 
137 #define	UMCPC_CM_MOBILE_ACM 0x0
138 
139 #define	UMCPC_ACTIVATE_MODE 0x60
140 #define	UMCPC_GET_MODETABLE 0x61
141 #define	UMCPC_SET_LINK 0x62
142 #define	UMCPC_CLEAR_LINK 0x63
143 
144 #define	UMCPC_REQUEST_ACKNOWLEDGE 0x31
145 
146 #define	UFOMA_MAX_TIMEOUT 15		/* standard says 10 seconds */
147 #define	UFOMA_CMD_BUF_SIZE 64		/* bytes */
148 
149 #define	UFOMA_BULK_BUF_SIZE 1024	/* bytes */
150 
151 enum {
152 	UFOMA_CTRL_ENDPT_INTR,
153 	UFOMA_CTRL_ENDPT_READ,
154 	UFOMA_CTRL_ENDPT_WRITE,
155 	UFOMA_CTRL_ENDPT_MAX,
156 };
157 
158 enum {
159 	UFOMA_BULK_ENDPT_WRITE,
160 	UFOMA_BULK_ENDPT_READ,
161 	UFOMA_BULK_ENDPT_MAX,
162 };
163 
164 struct ufoma_softc {
165 	struct ucom_super_softc sc_super_ucom;
166 	struct ucom_softc sc_ucom;
167 	struct cv sc_cv;
168 	struct mtx sc_mtx;
169 
170 	struct usb_xfer *sc_ctrl_xfer[UFOMA_CTRL_ENDPT_MAX];
171 	struct usb_xfer *sc_bulk_xfer[UFOMA_BULK_ENDPT_MAX];
172 	uint8_t *sc_modetable;
173 	device_t sc_dev;
174 	struct usb_device *sc_udev;
175 
176 	uint32_t sc_unit;
177 
178 	uint16_t sc_line;
179 
180 	uint8_t	sc_num_msg;
181 	uint8_t	sc_nobulk;
182 	uint8_t	sc_ctrl_iface_no;
183 	uint8_t	sc_ctrl_iface_index;
184 	uint8_t	sc_data_iface_no;
185 	uint8_t	sc_data_iface_index;
186 	uint8_t	sc_cm_cap;
187 	uint8_t	sc_acm_cap;
188 	uint8_t	sc_lsr;
189 	uint8_t	sc_msr;
190 	uint8_t	sc_modetoactivate;
191 	uint8_t	sc_currentmode;
192 };
193 
194 /* prototypes */
195 
196 static device_probe_t ufoma_probe;
197 static device_attach_t ufoma_attach;
198 static device_detach_t ufoma_detach;
199 static void ufoma_free_softc(struct ufoma_softc *);
200 
201 static usb_callback_t ufoma_ctrl_read_callback;
202 static usb_callback_t ufoma_ctrl_write_callback;
203 static usb_callback_t ufoma_intr_callback;
204 static usb_callback_t ufoma_bulk_write_callback;
205 static usb_callback_t ufoma_bulk_read_callback;
206 
207 static void	*ufoma_get_intconf(struct usb_config_descriptor *,
208 		    struct usb_interface_descriptor *, uint8_t, uint8_t);
209 static void	ufoma_cfg_link_state(struct ufoma_softc *);
210 static void	ufoma_cfg_activate_state(struct ufoma_softc *, uint16_t);
211 static void	ufoma_free(struct ucom_softc *);
212 static void	ufoma_cfg_open(struct ucom_softc *);
213 static void	ufoma_cfg_close(struct ucom_softc *);
214 static void	ufoma_cfg_set_break(struct ucom_softc *, uint8_t);
215 static void	ufoma_cfg_get_status(struct ucom_softc *, uint8_t *,
216 		    uint8_t *);
217 static void	ufoma_cfg_set_dtr(struct ucom_softc *, uint8_t);
218 static void	ufoma_cfg_set_rts(struct ucom_softc *, uint8_t);
219 static int	ufoma_pre_param(struct ucom_softc *, struct termios *);
220 static void	ufoma_cfg_param(struct ucom_softc *, struct termios *);
221 static int	ufoma_modem_setup(device_t, struct ufoma_softc *,
222 		    struct usb_attach_arg *);
223 static void	ufoma_start_read(struct ucom_softc *);
224 static void	ufoma_stop_read(struct ucom_softc *);
225 static void	ufoma_start_write(struct ucom_softc *);
226 static void	ufoma_stop_write(struct ucom_softc *);
227 static void	ufoma_poll(struct ucom_softc *ucom);
228 
229 /*sysctl stuff*/
230 static int ufoma_sysctl_support(SYSCTL_HANDLER_ARGS);
231 static int ufoma_sysctl_current(SYSCTL_HANDLER_ARGS);
232 static int ufoma_sysctl_open(SYSCTL_HANDLER_ARGS);
233 
234 static const struct usb_config
235 	ufoma_ctrl_config[UFOMA_CTRL_ENDPT_MAX] = {
236 	[UFOMA_CTRL_ENDPT_INTR] = {
237 		.type = UE_INTERRUPT,
238 		.endpoint = UE_ADDR_ANY,
239 		.direction = UE_DIR_IN,
240 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
241 		.bufsize = sizeof(struct usb_cdc_notification),
242 		.callback = &ufoma_intr_callback,
243 	},
244 
245 	[UFOMA_CTRL_ENDPT_READ] = {
246 		.type = UE_CONTROL,
247 		.endpoint = 0x00,	/* Control pipe */
248 		.direction = UE_DIR_ANY,
249 		.bufsize = (sizeof(struct usb_device_request) + UFOMA_CMD_BUF_SIZE),
250 		.flags = {.short_xfer_ok = 1,},
251 		.callback = &ufoma_ctrl_read_callback,
252 		.timeout = 1000,	/* 1 second */
253 	},
254 
255 	[UFOMA_CTRL_ENDPT_WRITE] = {
256 		.type = UE_CONTROL,
257 		.endpoint = 0x00,	/* Control pipe */
258 		.direction = UE_DIR_ANY,
259 		.bufsize = (sizeof(struct usb_device_request) + 1),
260 		.callback = &ufoma_ctrl_write_callback,
261 		.timeout = 1000,	/* 1 second */
262 	},
263 };
264 
265 static const struct usb_config
266 	ufoma_bulk_config[UFOMA_BULK_ENDPT_MAX] = {
267 	[UFOMA_BULK_ENDPT_WRITE] = {
268 		.type = UE_BULK,
269 		.endpoint = UE_ADDR_ANY,
270 		.direction = UE_DIR_OUT,
271 		.bufsize = UFOMA_BULK_BUF_SIZE,
272 		.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
273 		.callback = &ufoma_bulk_write_callback,
274 	},
275 
276 	[UFOMA_BULK_ENDPT_READ] = {
277 		.type = UE_BULK,
278 		.endpoint = UE_ADDR_ANY,
279 		.direction = UE_DIR_IN,
280 		.bufsize = UFOMA_BULK_BUF_SIZE,
281 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
282 		.callback = &ufoma_bulk_read_callback,
283 	},
284 };
285 
286 static const struct ucom_callback ufoma_callback = {
287 	.ucom_cfg_get_status = &ufoma_cfg_get_status,
288 	.ucom_cfg_set_dtr = &ufoma_cfg_set_dtr,
289 	.ucom_cfg_set_rts = &ufoma_cfg_set_rts,
290 	.ucom_cfg_set_break = &ufoma_cfg_set_break,
291 	.ucom_cfg_param = &ufoma_cfg_param,
292 	.ucom_cfg_open = &ufoma_cfg_open,
293 	.ucom_cfg_close = &ufoma_cfg_close,
294 	.ucom_pre_param = &ufoma_pre_param,
295 	.ucom_start_read = &ufoma_start_read,
296 	.ucom_stop_read = &ufoma_stop_read,
297 	.ucom_start_write = &ufoma_start_write,
298 	.ucom_stop_write = &ufoma_stop_write,
299 	.ucom_poll = &ufoma_poll,
300 	.ucom_free = &ufoma_free,
301 };
302 
303 static device_method_t ufoma_methods[] = {
304 	/* Device methods */
305 	DEVMETHOD(device_probe, ufoma_probe),
306 	DEVMETHOD(device_attach, ufoma_attach),
307 	DEVMETHOD(device_detach, ufoma_detach),
308 	DEVMETHOD_END
309 };
310 
311 static devclass_t ufoma_devclass;
312 
313 static driver_t ufoma_driver = {
314 	.name = "ufoma",
315 	.methods = ufoma_methods,
316 	.size = sizeof(struct ufoma_softc),
317 };
318 
319 static const STRUCT_USB_HOST_ID ufoma_devs[] = {
320 	{USB_IFACE_CLASS(UICLASS_CDC),
321 	 USB_IFACE_SUBCLASS(UISUBCLASS_MCPC),},
322 };
323 
324 DRIVER_MODULE(ufoma, uhub, ufoma_driver, ufoma_devclass, NULL, 0);
325 MODULE_DEPEND(ufoma, ucom, 1, 1, 1);
326 MODULE_DEPEND(ufoma, usb, 1, 1, 1);
327 MODULE_VERSION(ufoma, 1);
328 USB_PNP_HOST_INFO(ufoma_devs);
329 
330 static int
331 ufoma_probe(device_t dev)
332 {
333 	struct usb_attach_arg *uaa = device_get_ivars(dev);
334 	struct usb_interface_descriptor *id;
335 	struct usb_config_descriptor *cd;
336 	usb_mcpc_acm_descriptor *mad;
337 	int error;
338 
339 	if (uaa->usb_mode != USB_MODE_HOST)
340 		return (ENXIO);
341 
342 	error = usbd_lookup_id_by_uaa(ufoma_devs, sizeof(ufoma_devs), uaa);
343 	if (error)
344 		return (error);
345 
346 	id = usbd_get_interface_descriptor(uaa->iface);
347 	cd = usbd_get_config_descriptor(uaa->device);
348 
349 	if (id == NULL || cd == NULL)
350 		return (ENXIO);
351 
352 	mad = ufoma_get_intconf(cd, id, UDESC_VS_INTERFACE, UDESCSUB_MCPC_ACM);
353 	if (mad == NULL)
354 		return (ENXIO);
355 
356 #ifndef UFOMA_HANDSFREE
357 	if ((mad->bType == UMCPC_ACM_TYPE_AB5) ||
358 	    (mad->bType == UMCPC_ACM_TYPE_AB6))
359 		return (ENXIO);
360 #endif
361 	return (BUS_PROBE_GENERIC);
362 }
363 
364 static int
365 ufoma_attach(device_t dev)
366 {
367 	struct usb_attach_arg *uaa = device_get_ivars(dev);
368 	struct ufoma_softc *sc = device_get_softc(dev);
369 	struct usb_config_descriptor *cd;
370 	struct usb_interface_descriptor *id;
371 	struct sysctl_ctx_list *sctx;
372 	struct sysctl_oid *soid;
373 
374 	usb_mcpc_acm_descriptor *mad;
375 	uint8_t elements;
376 	int32_t error;
377 
378 	sc->sc_udev = uaa->device;
379 	sc->sc_dev = dev;
380 	sc->sc_unit = device_get_unit(dev);
381 
382 	mtx_init(&sc->sc_mtx, "ufoma", NULL, MTX_DEF);
383 	ucom_ref(&sc->sc_super_ucom);
384 	cv_init(&sc->sc_cv, "CWAIT");
385 
386 	device_set_usb_desc(dev);
387 
388 	DPRINTF("\n");
389 
390 	/* setup control transfers */
391 
392 	cd = usbd_get_config_descriptor(uaa->device);
393 	id = usbd_get_interface_descriptor(uaa->iface);
394 	sc->sc_ctrl_iface_no = id->bInterfaceNumber;
395 	sc->sc_ctrl_iface_index = uaa->info.bIfaceIndex;
396 
397 	error = usbd_transfer_setup(uaa->device,
398 	    &sc->sc_ctrl_iface_index, sc->sc_ctrl_xfer,
399 	    ufoma_ctrl_config, UFOMA_CTRL_ENDPT_MAX, sc, &sc->sc_mtx);
400 
401 	if (error) {
402 		device_printf(dev, "allocating control USB "
403 		    "transfers failed\n");
404 		goto detach;
405 	}
406 	mad = ufoma_get_intconf(cd, id, UDESC_VS_INTERFACE, UDESCSUB_MCPC_ACM);
407 	if (mad == NULL) {
408 		goto detach;
409 	}
410 	if (mad->bFunctionLength < sizeof(*mad)) {
411 		device_printf(dev, "invalid MAD descriptor\n");
412 		goto detach;
413 	}
414 	if ((mad->bType == UMCPC_ACM_TYPE_AB5) ||
415 	    (mad->bType == UMCPC_ACM_TYPE_AB6)) {
416 		sc->sc_nobulk = 1;
417 	} else {
418 		sc->sc_nobulk = 0;
419 		if (ufoma_modem_setup(dev, sc, uaa)) {
420 			goto detach;
421 		}
422 	}
423 
424 	elements = (mad->bFunctionLength - sizeof(*mad) + 1);
425 
426 	/* initialize mode variables */
427 
428 	sc->sc_modetable = malloc(elements + 1, M_USBDEV, M_WAITOK);
429 
430 	if (sc->sc_modetable == NULL) {
431 		goto detach;
432 	}
433 	sc->sc_modetable[0] = (elements + 1);
434 	memcpy(&sc->sc_modetable[1], mad->bMode, elements);
435 
436 	sc->sc_currentmode = UMCPC_ACM_MODE_UNLINKED;
437 	sc->sc_modetoactivate = mad->bMode[0];
438 
439 	/* clear stall at first run, if any */
440 	mtx_lock(&sc->sc_mtx);
441 	usbd_xfer_set_stall(sc->sc_bulk_xfer[UFOMA_BULK_ENDPT_WRITE]);
442 	usbd_xfer_set_stall(sc->sc_bulk_xfer[UFOMA_BULK_ENDPT_READ]);
443 	mtx_unlock(&sc->sc_mtx);
444 
445 	error = ucom_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc,
446 	    &ufoma_callback, &sc->sc_mtx);
447 	if (error) {
448 		DPRINTF("ucom_attach failed\n");
449 		goto detach;
450 	}
451 	ucom_set_pnpinfo_usb(&sc->sc_super_ucom, dev);
452 
453 	/*Sysctls*/
454 	sctx = device_get_sysctl_ctx(dev);
455 	soid = device_get_sysctl_tree(dev);
456 
457 	SYSCTL_ADD_PROC(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "supportmode",
458 	    CTLFLAG_RD | CTLTYPE_STRING | CTLFLAG_MPSAFE, sc, 0,
459 	    ufoma_sysctl_support, "A", "Supporting port role");
460 
461 	SYSCTL_ADD_PROC(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "currentmode",
462 	CTLFLAG_RD | CTLTYPE_STRING | CTLFLAG_MPSAFE, sc, 0,
463 	ufoma_sysctl_current, "A", "Current port role");
464 
465 	SYSCTL_ADD_PROC(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "openmode",
466 	CTLFLAG_RW | CTLTYPE_STRING | CTLFLAG_MPSAFE, sc, 0,
467 	ufoma_sysctl_open, "A", "Mode to transit when port is opened");
468 	SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "comunit",
469 			CTLFLAG_RD, &(sc->sc_super_ucom.sc_unit), 0,
470 			"Unit number as USB serial");
471 
472 	return (0);			/* success */
473 
474 detach:
475 	ufoma_detach(dev);
476 	return (ENXIO);			/* failure */
477 }
478 
479 static int
480 ufoma_detach(device_t dev)
481 {
482 	struct ufoma_softc *sc = device_get_softc(dev);
483 
484 	ucom_detach(&sc->sc_super_ucom, &sc->sc_ucom);
485 	usbd_transfer_unsetup(sc->sc_ctrl_xfer, UFOMA_CTRL_ENDPT_MAX);
486 	usbd_transfer_unsetup(sc->sc_bulk_xfer, UFOMA_BULK_ENDPT_MAX);
487 
488 	if (sc->sc_modetable) {
489 		free(sc->sc_modetable, M_USBDEV);
490 	}
491 	cv_destroy(&sc->sc_cv);
492 
493 	device_claim_softc(dev);
494 
495 	ufoma_free_softc(sc);
496 
497 	return (0);
498 }
499 
500 UCOM_UNLOAD_DRAIN(ufoma);
501 
502 static void
503 ufoma_free_softc(struct ufoma_softc *sc)
504 {
505 	if (ucom_unref(&sc->sc_super_ucom)) {
506 		mtx_destroy(&sc->sc_mtx);
507 		device_free_softc(sc);
508 	}
509 }
510 
511 static void
512 ufoma_free(struct ucom_softc *ucom)
513 {
514 	ufoma_free_softc(ucom->sc_parent);
515 }
516 
517 static void *
518 ufoma_get_intconf(struct usb_config_descriptor *cd, struct usb_interface_descriptor *id,
519     uint8_t type, uint8_t subtype)
520 {
521 	struct usb_descriptor *desc = (void *)id;
522 
523 	while ((desc = usb_desc_foreach(cd, desc))) {
524 		if (desc->bDescriptorType == UDESC_INTERFACE) {
525 			return (NULL);
526 		}
527 		if ((desc->bDescriptorType == type) &&
528 		    (desc->bDescriptorSubtype == subtype)) {
529 			break;
530 		}
531 	}
532 	return (desc);
533 }
534 
535 static void
536 ufoma_cfg_link_state(struct ufoma_softc *sc)
537 {
538 	struct usb_device_request req;
539 	int32_t error;
540 
541 	req.bmRequestType = UT_WRITE_VENDOR_INTERFACE;
542 	req.bRequest = UMCPC_SET_LINK;
543 	USETW(req.wValue, UMCPC_CM_MOBILE_ACM);
544 	USETW(req.wIndex, sc->sc_ctrl_iface_no);
545 	USETW(req.wLength, sc->sc_modetable[0]);
546 
547 	ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
548 	    &req, sc->sc_modetable, 0, 1000);
549 
550 	error = cv_timedwait(&sc->sc_cv, &sc->sc_mtx, hz);
551 
552 	if (error) {
553 		DPRINTF("NO response\n");
554 	}
555 }
556 
557 static void
558 ufoma_cfg_activate_state(struct ufoma_softc *sc, uint16_t state)
559 {
560 	struct usb_device_request req;
561 	int32_t error;
562 
563 	req.bmRequestType = UT_WRITE_VENDOR_INTERFACE;
564 	req.bRequest = UMCPC_ACTIVATE_MODE;
565 	USETW(req.wValue, state);
566 	USETW(req.wIndex, sc->sc_ctrl_iface_no);
567 	USETW(req.wLength, 0);
568 
569 	ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
570 	    &req, NULL, 0, 1000);
571 
572 	error = cv_timedwait(&sc->sc_cv, &sc->sc_mtx,
573 	    (UFOMA_MAX_TIMEOUT * hz));
574 	if (error) {
575 		DPRINTF("No response\n");
576 	}
577 }
578 
579 static void
580 ufoma_ctrl_read_callback(struct usb_xfer *xfer, usb_error_t error)
581 {
582 	struct ufoma_softc *sc = usbd_xfer_softc(xfer);
583 	struct usb_device_request req;
584 	struct usb_page_cache *pc0, *pc1;
585 	int len, aframes, nframes;
586 
587 	usbd_xfer_status(xfer, NULL, NULL, &aframes, &nframes);
588 
589 	switch (USB_GET_STATE(xfer)) {
590 	case USB_ST_TRANSFERRED:
591 tr_transferred:
592 		if (aframes != nframes)
593 			goto tr_setup;
594 		pc1 = usbd_xfer_get_frame(xfer, 1);
595 		len = usbd_xfer_frame_len(xfer, 1);
596 		if (len > 0)
597 			ucom_put_data(&sc->sc_ucom, pc1, 0, len);
598 		/* FALLTHROUGH */
599 	case USB_ST_SETUP:
600 tr_setup:
601 		if (sc->sc_num_msg) {
602 			sc->sc_num_msg--;
603 
604 			req.bmRequestType = UT_READ_CLASS_INTERFACE;
605 			req.bRequest = UCDC_GET_ENCAPSULATED_RESPONSE;
606 			USETW(req.wIndex, sc->sc_ctrl_iface_no);
607 			USETW(req.wValue, 0);
608 			USETW(req.wLength, UFOMA_CMD_BUF_SIZE);
609 
610 			pc0 = usbd_xfer_get_frame(xfer, 0);
611 			usbd_copy_in(pc0, 0, &req, sizeof(req));
612 
613 			usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
614 			usbd_xfer_set_frame_len(xfer, 1, UFOMA_CMD_BUF_SIZE);
615 			usbd_xfer_set_frames(xfer, 2);
616 			usbd_transfer_submit(xfer);
617 		}
618 		return;
619 
620 	default:			/* Error */
621 		DPRINTF("error = %s\n",
622 		    usbd_errstr(error));
623 
624 		if (error == USB_ERR_CANCELLED) {
625 			return;
626 		}
627 		goto tr_transferred;
628 	}
629 }
630 
631 static void
632 ufoma_ctrl_write_callback(struct usb_xfer *xfer, usb_error_t error)
633 {
634 	struct ufoma_softc *sc = usbd_xfer_softc(xfer);
635 	struct usb_device_request req;
636 	struct usb_page_cache *pc;
637 	uint32_t actlen;
638 
639 	switch (USB_GET_STATE(xfer)) {
640 	case USB_ST_TRANSFERRED:
641 tr_transferred:
642 	case USB_ST_SETUP:
643 		pc = usbd_xfer_get_frame(xfer, 1);
644 		if (ucom_get_data(&sc->sc_ucom, pc, 0, 1, &actlen)) {
645 			req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
646 			req.bRequest = UCDC_SEND_ENCAPSULATED_COMMAND;
647 			USETW(req.wIndex, sc->sc_ctrl_iface_no);
648 			USETW(req.wValue, 0);
649 			USETW(req.wLength, 1);
650 
651 			pc = usbd_xfer_get_frame(xfer, 0);
652 			usbd_copy_in(pc, 0, &req, sizeof(req));
653 
654 			usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
655 			usbd_xfer_set_frame_len(xfer, 1, 1);
656 			usbd_xfer_set_frames(xfer, 2);
657 
658 			usbd_transfer_submit(xfer);
659 		}
660 		return;
661 
662 	default:			/* Error */
663 		DPRINTF("error = %s\n", usbd_errstr(error));
664 
665 		if (error == USB_ERR_CANCELLED) {
666 			return;
667 		}
668 		goto tr_transferred;
669 	}
670 }
671 
672 static void
673 ufoma_intr_callback(struct usb_xfer *xfer, usb_error_t error)
674 {
675 	struct ufoma_softc *sc = usbd_xfer_softc(xfer);
676 	struct usb_cdc_notification pkt;
677 	struct usb_page_cache *pc;
678 	uint16_t wLen;
679 	uint16_t temp;
680 	uint8_t mstatus;
681 	int actlen;
682 
683 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
684 
685 	switch (USB_GET_STATE(xfer)) {
686 	case USB_ST_TRANSFERRED:
687 		if (actlen < 8) {
688 			DPRINTF("too short message\n");
689 			goto tr_setup;
690 		}
691 		if (actlen > (int)sizeof(pkt)) {
692 			DPRINTF("truncating message\n");
693 			actlen = sizeof(pkt);
694 		}
695 		pc = usbd_xfer_get_frame(xfer, 0);
696 		usbd_copy_out(pc, 0, &pkt, actlen);
697 
698 		actlen -= 8;
699 
700 		wLen = UGETW(pkt.wLength);
701 		if (actlen > wLen) {
702 			actlen = wLen;
703 		}
704 		if ((pkt.bmRequestType == UT_READ_VENDOR_INTERFACE) &&
705 		    (pkt.bNotification == UMCPC_REQUEST_ACKNOWLEDGE)) {
706 			temp = UGETW(pkt.wValue);
707 			sc->sc_currentmode = (temp >> 8);
708 			if (!(temp & 0xff)) {
709 				DPRINTF("Mode change failed!\n");
710 			}
711 			cv_signal(&sc->sc_cv);
712 		}
713 		if (pkt.bmRequestType != UCDC_NOTIFICATION) {
714 			goto tr_setup;
715 		}
716 		switch (pkt.bNotification) {
717 		case UCDC_N_RESPONSE_AVAILABLE:
718 			if (!(sc->sc_nobulk)) {
719 				DPRINTF("Wrong serial state!\n");
720 				break;
721 			}
722 			if (sc->sc_num_msg != 0xFF) {
723 				sc->sc_num_msg++;
724 			}
725 			usbd_transfer_start(sc->sc_ctrl_xfer[UFOMA_CTRL_ENDPT_READ]);
726 			break;
727 
728 		case UCDC_N_SERIAL_STATE:
729 			if (sc->sc_nobulk) {
730 				DPRINTF("Wrong serial state!\n");
731 				break;
732 			}
733 			/*
734 		         * Set the serial state in ucom driver based on
735 		         * the bits from the notify message
736 		         */
737 			if (actlen < 2) {
738 				DPRINTF("invalid notification "
739 				    "length, %d bytes!\n", actlen);
740 				break;
741 			}
742 			DPRINTF("notify bytes = 0x%02x, 0x%02x\n",
743 			    pkt.data[0], pkt.data[1]);
744 
745 			/* currently, lsr is always zero. */
746 			sc->sc_lsr = 0;
747 			sc->sc_msr = 0;
748 
749 			mstatus = pkt.data[0];
750 
751 			if (mstatus & UCDC_N_SERIAL_RI) {
752 				sc->sc_msr |= SER_RI;
753 			}
754 			if (mstatus & UCDC_N_SERIAL_DSR) {
755 				sc->sc_msr |= SER_DSR;
756 			}
757 			if (mstatus & UCDC_N_SERIAL_DCD) {
758 				sc->sc_msr |= SER_DCD;
759 			}
760 			ucom_status_change(&sc->sc_ucom);
761 			break;
762 
763 		default:
764 			break;
765 		}
766 
767 	case USB_ST_SETUP:
768 tr_setup:
769 		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
770 		usbd_transfer_submit(xfer);
771 		return;
772 
773 	default:			/* Error */
774 		if (error != USB_ERR_CANCELLED) {
775 			/* try to clear stall first */
776 			usbd_xfer_set_stall(xfer);
777 			goto tr_setup;
778 		}
779 		return;
780 	}
781 }
782 
783 static void
784 ufoma_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
785 {
786 	struct ufoma_softc *sc = usbd_xfer_softc(xfer);
787 	struct usb_page_cache *pc;
788 	uint32_t actlen;
789 
790 	switch (USB_GET_STATE(xfer)) {
791 	case USB_ST_SETUP:
792 	case USB_ST_TRANSFERRED:
793 tr_setup:
794 		pc = usbd_xfer_get_frame(xfer, 0);
795 		if (ucom_get_data(&sc->sc_ucom, pc, 0,
796 		    UFOMA_BULK_BUF_SIZE, &actlen)) {
797 			usbd_xfer_set_frame_len(xfer, 0, actlen);
798 			usbd_transfer_submit(xfer);
799 		}
800 		return;
801 
802 	default:			/* Error */
803 		if (error != USB_ERR_CANCELLED) {
804 			/* try to clear stall first */
805 			usbd_xfer_set_stall(xfer);
806 			goto tr_setup;
807 		}
808 		return;
809 	}
810 }
811 
812 static void
813 ufoma_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
814 {
815 	struct ufoma_softc *sc = usbd_xfer_softc(xfer);
816 	struct usb_page_cache *pc;
817 	int actlen;
818 
819 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
820 
821 	switch (USB_GET_STATE(xfer)) {
822 	case USB_ST_TRANSFERRED:
823 		pc = usbd_xfer_get_frame(xfer, 0);
824 		ucom_put_data(&sc->sc_ucom, pc, 0, actlen);
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 static void
843 ufoma_cfg_open(struct ucom_softc *ucom)
844 {
845 	struct ufoma_softc *sc = ucom->sc_parent;
846 
847 	/* empty input queue */
848 
849 	if (sc->sc_num_msg != 0xFF) {
850 		sc->sc_num_msg++;
851 	}
852 	if (sc->sc_currentmode == UMCPC_ACM_MODE_UNLINKED) {
853 		ufoma_cfg_link_state(sc);
854 	}
855 	if (sc->sc_currentmode == UMCPC_ACM_MODE_DEACTIVATED) {
856 		ufoma_cfg_activate_state(sc, sc->sc_modetoactivate);
857 	}
858 }
859 
860 static void
861 ufoma_cfg_close(struct ucom_softc *ucom)
862 {
863 	struct ufoma_softc *sc = ucom->sc_parent;
864 
865 	ufoma_cfg_activate_state(sc, UMCPC_ACM_MODE_DEACTIVATED);
866 }
867 
868 static void
869 ufoma_cfg_set_break(struct ucom_softc *ucom, uint8_t onoff)
870 {
871 	struct ufoma_softc *sc = ucom->sc_parent;
872 	struct usb_device_request req;
873 	uint16_t wValue;
874 
875 	if (sc->sc_nobulk ||
876 	    (sc->sc_currentmode == UMCPC_ACM_MODE_OBEX)) {
877 		return;
878 	}
879 	if (!(sc->sc_acm_cap & USB_CDC_ACM_HAS_BREAK)) {
880 		return;
881 	}
882 	wValue = onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF;
883 
884 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
885 	req.bRequest = UCDC_SEND_BREAK;
886 	USETW(req.wValue, wValue);
887 	req.wIndex[0] = sc->sc_ctrl_iface_no;
888 	req.wIndex[1] = 0;
889 	USETW(req.wLength, 0);
890 
891 	ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
892 	    &req, NULL, 0, 1000);
893 }
894 
895 static void
896 ufoma_cfg_get_status(struct ucom_softc *ucom, uint8_t *lsr, uint8_t *msr)
897 {
898 	struct ufoma_softc *sc = ucom->sc_parent;
899 
900 	/* XXX Note: sc_lsr is always zero */
901 	*lsr = sc->sc_lsr;
902 	*msr = sc->sc_msr;
903 }
904 
905 static void
906 ufoma_cfg_set_line_state(struct ufoma_softc *sc)
907 {
908 	struct usb_device_request req;
909 
910 	/* Don't send line state emulation request for OBEX port */
911 	if (sc->sc_currentmode == UMCPC_ACM_MODE_OBEX) {
912 		return;
913 	}
914 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
915 	req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
916 	USETW(req.wValue, sc->sc_line);
917 	req.wIndex[0] = sc->sc_ctrl_iface_no;
918 	req.wIndex[1] = 0;
919 	USETW(req.wLength, 0);
920 
921 	ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
922 	    &req, NULL, 0, 1000);
923 }
924 
925 static void
926 ufoma_cfg_set_dtr(struct ucom_softc *ucom, uint8_t onoff)
927 {
928 	struct ufoma_softc *sc = ucom->sc_parent;
929 
930 	if (sc->sc_nobulk) {
931 		return;
932 	}
933 	if (onoff)
934 		sc->sc_line |= UCDC_LINE_DTR;
935 	else
936 		sc->sc_line &= ~UCDC_LINE_DTR;
937 
938 	ufoma_cfg_set_line_state(sc);
939 }
940 
941 static void
942 ufoma_cfg_set_rts(struct ucom_softc *ucom, uint8_t onoff)
943 {
944 	struct ufoma_softc *sc = ucom->sc_parent;
945 
946 	if (sc->sc_nobulk) {
947 		return;
948 	}
949 	if (onoff)
950 		sc->sc_line |= UCDC_LINE_RTS;
951 	else
952 		sc->sc_line &= ~UCDC_LINE_RTS;
953 
954 	ufoma_cfg_set_line_state(sc);
955 }
956 
957 static int
958 ufoma_pre_param(struct ucom_softc *ucom, struct termios *t)
959 {
960 	return (0);			/* we accept anything */
961 }
962 
963 static void
964 ufoma_cfg_param(struct ucom_softc *ucom, struct termios *t)
965 {
966 	struct ufoma_softc *sc = ucom->sc_parent;
967 	struct usb_device_request req;
968 	struct usb_cdc_line_state ls;
969 
970 	if (sc->sc_nobulk ||
971 	    (sc->sc_currentmode == UMCPC_ACM_MODE_OBEX)) {
972 		return;
973 	}
974 	DPRINTF("\n");
975 
976 	memset(&ls, 0, sizeof(ls));
977 
978 	USETDW(ls.dwDTERate, t->c_ospeed);
979 
980 	if (t->c_cflag & CSTOPB) {
981 		ls.bCharFormat = UCDC_STOP_BIT_2;
982 	} else {
983 		ls.bCharFormat = UCDC_STOP_BIT_1;
984 	}
985 
986 	if (t->c_cflag & PARENB) {
987 		if (t->c_cflag & PARODD) {
988 			ls.bParityType = UCDC_PARITY_ODD;
989 		} else {
990 			ls.bParityType = UCDC_PARITY_EVEN;
991 		}
992 	} else {
993 		ls.bParityType = UCDC_PARITY_NONE;
994 	}
995 
996 	switch (t->c_cflag & CSIZE) {
997 	case CS5:
998 		ls.bDataBits = 5;
999 		break;
1000 	case CS6:
1001 		ls.bDataBits = 6;
1002 		break;
1003 	case CS7:
1004 		ls.bDataBits = 7;
1005 		break;
1006 	case CS8:
1007 		ls.bDataBits = 8;
1008 		break;
1009 	}
1010 
1011 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
1012 	req.bRequest = UCDC_SET_LINE_CODING;
1013 	USETW(req.wValue, 0);
1014 	req.wIndex[0] = sc->sc_ctrl_iface_no;
1015 	req.wIndex[1] = 0;
1016 	USETW(req.wLength, UCDC_LINE_STATE_LENGTH);
1017 
1018 	ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
1019 	    &req, &ls, 0, 1000);
1020 }
1021 
1022 static int
1023 ufoma_modem_setup(device_t dev, struct ufoma_softc *sc,
1024     struct usb_attach_arg *uaa)
1025 {
1026 	struct usb_config_descriptor *cd;
1027 	struct usb_cdc_acm_descriptor *acm;
1028 	struct usb_cdc_cm_descriptor *cmd;
1029 	struct usb_interface_descriptor *id;
1030 	struct usb_interface *iface;
1031 	uint8_t i;
1032 	int32_t error;
1033 
1034 	cd = usbd_get_config_descriptor(uaa->device);
1035 	id = usbd_get_interface_descriptor(uaa->iface);
1036 
1037 	cmd = ufoma_get_intconf(cd, id, UDESC_CS_INTERFACE, UDESCSUB_CDC_CM);
1038 
1039 	if ((cmd == NULL) ||
1040 	    (cmd->bLength < sizeof(*cmd))) {
1041 		return (EINVAL);
1042 	}
1043 	sc->sc_cm_cap = cmd->bmCapabilities;
1044 	sc->sc_data_iface_no = cmd->bDataInterface;
1045 
1046 	acm = ufoma_get_intconf(cd, id, UDESC_CS_INTERFACE, UDESCSUB_CDC_ACM);
1047 
1048 	if ((acm == NULL) ||
1049 	    (acm->bLength < sizeof(*acm))) {
1050 		return (EINVAL);
1051 	}
1052 	sc->sc_acm_cap = acm->bmCapabilities;
1053 
1054 	device_printf(dev, "data interface %d, has %sCM over data, "
1055 	    "has %sbreak\n",
1056 	    sc->sc_data_iface_no,
1057 	    sc->sc_cm_cap & USB_CDC_CM_OVER_DATA ? "" : "no ",
1058 	    sc->sc_acm_cap & USB_CDC_ACM_HAS_BREAK ? "" : "no ");
1059 
1060 	/* get the data interface too */
1061 
1062 	for (i = 0;; i++) {
1063 		iface = usbd_get_iface(uaa->device, i);
1064 
1065 		if (iface) {
1066 			id = usbd_get_interface_descriptor(iface);
1067 
1068 			if (id && (id->bInterfaceNumber == sc->sc_data_iface_no)) {
1069 				sc->sc_data_iface_index = i;
1070 				usbd_set_parent_iface(uaa->device, i, uaa->info.bIfaceIndex);
1071 				break;
1072 			}
1073 		} else {
1074 			device_printf(dev, "no data interface\n");
1075 			return (EINVAL);
1076 		}
1077 	}
1078 
1079 	error = usbd_transfer_setup(uaa->device,
1080 	    &sc->sc_data_iface_index, sc->sc_bulk_xfer,
1081 	    ufoma_bulk_config, UFOMA_BULK_ENDPT_MAX, sc, &sc->sc_mtx);
1082 
1083 	if (error) {
1084 		device_printf(dev, "allocating BULK USB "
1085 		    "transfers failed\n");
1086 		return (EINVAL);
1087 	}
1088 	return (0);
1089 }
1090 
1091 static void
1092 ufoma_start_read(struct ucom_softc *ucom)
1093 {
1094 	struct ufoma_softc *sc = ucom->sc_parent;
1095 
1096 	/* start interrupt transfer */
1097 	usbd_transfer_start(sc->sc_ctrl_xfer[UFOMA_CTRL_ENDPT_INTR]);
1098 
1099 	/* start data transfer */
1100 	if (sc->sc_nobulk) {
1101 		usbd_transfer_start(sc->sc_ctrl_xfer[UFOMA_CTRL_ENDPT_READ]);
1102 	} else {
1103 		usbd_transfer_start(sc->sc_bulk_xfer[UFOMA_BULK_ENDPT_READ]);
1104 	}
1105 }
1106 
1107 static void
1108 ufoma_stop_read(struct ucom_softc *ucom)
1109 {
1110 	struct ufoma_softc *sc = ucom->sc_parent;
1111 
1112 	/* stop interrupt transfer */
1113 	usbd_transfer_stop(sc->sc_ctrl_xfer[UFOMA_CTRL_ENDPT_INTR]);
1114 
1115 	/* stop data transfer */
1116 	if (sc->sc_nobulk) {
1117 		usbd_transfer_stop(sc->sc_ctrl_xfer[UFOMA_CTRL_ENDPT_READ]);
1118 	} else {
1119 		usbd_transfer_stop(sc->sc_bulk_xfer[UFOMA_BULK_ENDPT_READ]);
1120 	}
1121 }
1122 
1123 static void
1124 ufoma_start_write(struct ucom_softc *ucom)
1125 {
1126 	struct ufoma_softc *sc = ucom->sc_parent;
1127 
1128 	if (sc->sc_nobulk) {
1129 		usbd_transfer_start(sc->sc_ctrl_xfer[UFOMA_CTRL_ENDPT_WRITE]);
1130 	} else {
1131 		usbd_transfer_start(sc->sc_bulk_xfer[UFOMA_BULK_ENDPT_WRITE]);
1132 	}
1133 }
1134 
1135 static void
1136 ufoma_stop_write(struct ucom_softc *ucom)
1137 {
1138 	struct ufoma_softc *sc = ucom->sc_parent;
1139 
1140 	if (sc->sc_nobulk) {
1141 		usbd_transfer_stop(sc->sc_ctrl_xfer[UFOMA_CTRL_ENDPT_WRITE]);
1142 	} else {
1143 		usbd_transfer_stop(sc->sc_bulk_xfer[UFOMA_BULK_ENDPT_WRITE]);
1144 	}
1145 }
1146 
1147 static struct umcpc_modetostr_tab{
1148 	int mode;
1149 	char *str;
1150 }umcpc_modetostr_tab[]={
1151 	{UMCPC_ACM_MODE_DEACTIVATED, "deactivated"},
1152 	{UMCPC_ACM_MODE_MODEM, "modem"},
1153 	{UMCPC_ACM_MODE_ATCOMMAND, "handsfree"},
1154 	{UMCPC_ACM_MODE_OBEX, "obex"},
1155 	{UMCPC_ACM_MODE_VENDOR1, "vendor1"},
1156 	{UMCPC_ACM_MODE_VENDOR2, "vendor2"},
1157 	{UMCPC_ACM_MODE_UNLINKED, "unlinked"},
1158 	{0, NULL}
1159 };
1160 
1161 static char *ufoma_mode_to_str(int mode)
1162 {
1163 	int i;
1164 	for(i = 0 ;umcpc_modetostr_tab[i].str != NULL; i++){
1165 		if(umcpc_modetostr_tab[i].mode == mode){
1166 			return umcpc_modetostr_tab[i].str;
1167 		}
1168 	}
1169 	return NULL;
1170 }
1171 
1172 static int ufoma_str_to_mode(char *str)
1173 {
1174 	int i;
1175 	for(i = 0 ;umcpc_modetostr_tab[i].str != NULL; i++){
1176 		if(strcmp(str, umcpc_modetostr_tab[i].str)==0){
1177 			return umcpc_modetostr_tab[i].mode;
1178 		}
1179 	}
1180 	return -1;
1181 }
1182 
1183 static int ufoma_sysctl_support(SYSCTL_HANDLER_ARGS)
1184 {
1185 	struct ufoma_softc *sc = (struct ufoma_softc *)oidp->oid_arg1;
1186 	struct sbuf sb;
1187 	int i;
1188 	char *mode;
1189 
1190 	sbuf_new(&sb, NULL, 1, SBUF_AUTOEXTEND);
1191 	for(i = 1; i < sc->sc_modetable[0]; i++){
1192 		mode = ufoma_mode_to_str(sc->sc_modetable[i]);
1193 		if(mode !=NULL){
1194 			sbuf_cat(&sb, mode);
1195 		}else{
1196 			sbuf_printf(&sb, "(%02x)", sc->sc_modetable[i]);
1197 		}
1198 		if(i < (sc->sc_modetable[0]-1))
1199 			sbuf_cat(&sb, ",");
1200 	}
1201 	sbuf_trim(&sb);
1202 	sbuf_finish(&sb);
1203 	sysctl_handle_string(oidp, sbuf_data(&sb), sbuf_len(&sb), req);
1204 	sbuf_delete(&sb);
1205 
1206 	return 0;
1207 }
1208 static int ufoma_sysctl_current(SYSCTL_HANDLER_ARGS)
1209 {
1210 	struct ufoma_softc *sc = (struct ufoma_softc *)oidp->oid_arg1;
1211 	char *mode;
1212 	char subbuf[]="(XXX)";
1213 	mode = ufoma_mode_to_str(sc->sc_currentmode);
1214 	if(!mode){
1215 		mode = subbuf;
1216 		snprintf(subbuf, sizeof(subbuf), "(%02x)", sc->sc_currentmode);
1217 	}
1218 	sysctl_handle_string(oidp, mode, strlen(mode), req);
1219 
1220 	return 0;
1221 
1222 }
1223 static int ufoma_sysctl_open(SYSCTL_HANDLER_ARGS)
1224 {
1225 	struct ufoma_softc *sc = (struct ufoma_softc *)oidp->oid_arg1;
1226 	char *mode;
1227 	char subbuf[40];
1228 	int newmode;
1229 	int error;
1230 	int i;
1231 
1232 	mode = ufoma_mode_to_str(sc->sc_modetoactivate);
1233 	if(mode){
1234 		strncpy(subbuf, mode, sizeof(subbuf));
1235 	}else{
1236 		snprintf(subbuf, sizeof(subbuf), "(%02x)", sc->sc_modetoactivate);
1237 	}
1238 	error = sysctl_handle_string(oidp, subbuf, sizeof(subbuf), req);
1239 	if(error != 0 || req->newptr == NULL){
1240 		return error;
1241 	}
1242 
1243 	if((newmode = ufoma_str_to_mode(subbuf)) == -1){
1244 		return EINVAL;
1245 	}
1246 
1247 	for(i = 1 ; i < sc->sc_modetable[0] ; i++){
1248 		if(sc->sc_modetable[i] == newmode){
1249 			sc->sc_modetoactivate = newmode;
1250 			return 0;
1251 		}
1252 	}
1253 
1254 	return EINVAL;
1255 }
1256 
1257 static void
1258 ufoma_poll(struct ucom_softc *ucom)
1259 {
1260 	struct ufoma_softc *sc = ucom->sc_parent;
1261 	usbd_transfer_poll(sc->sc_ctrl_xfer, UFOMA_CTRL_ENDPT_MAX);
1262 	usbd_transfer_poll(sc->sc_bulk_xfer, UFOMA_BULK_ENDPT_MAX);
1263 }
1264