xref: /freebsd/sys/dev/usb/serial/ufoma.c (revision 4e99f45480598189d49d45a825533a6c9e12f02c)
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 
237 	[UFOMA_CTRL_ENDPT_INTR] = {
238 		.type = UE_INTERRUPT,
239 		.endpoint = UE_ADDR_ANY,
240 		.direction = UE_DIR_IN,
241 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
242 		.bufsize = sizeof(struct usb_cdc_notification),
243 		.callback = &ufoma_intr_callback,
244 	},
245 
246 	[UFOMA_CTRL_ENDPT_READ] = {
247 		.type = UE_CONTROL,
248 		.endpoint = 0x00,	/* Control pipe */
249 		.direction = UE_DIR_ANY,
250 		.bufsize = (sizeof(struct usb_device_request) + UFOMA_CMD_BUF_SIZE),
251 		.flags = {.short_xfer_ok = 1,},
252 		.callback = &ufoma_ctrl_read_callback,
253 		.timeout = 1000,	/* 1 second */
254 	},
255 
256 	[UFOMA_CTRL_ENDPT_WRITE] = {
257 		.type = UE_CONTROL,
258 		.endpoint = 0x00,	/* Control pipe */
259 		.direction = UE_DIR_ANY,
260 		.bufsize = (sizeof(struct usb_device_request) + 1),
261 		.callback = &ufoma_ctrl_write_callback,
262 		.timeout = 1000,	/* 1 second */
263 	},
264 };
265 
266 static const struct usb_config
267 	ufoma_bulk_config[UFOMA_BULK_ENDPT_MAX] = {
268 
269 	[UFOMA_BULK_ENDPT_WRITE] = {
270 		.type = UE_BULK,
271 		.endpoint = UE_ADDR_ANY,
272 		.direction = UE_DIR_OUT,
273 		.bufsize = UFOMA_BULK_BUF_SIZE,
274 		.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
275 		.callback = &ufoma_bulk_write_callback,
276 	},
277 
278 	[UFOMA_BULK_ENDPT_READ] = {
279 		.type = UE_BULK,
280 		.endpoint = UE_ADDR_ANY,
281 		.direction = UE_DIR_IN,
282 		.bufsize = UFOMA_BULK_BUF_SIZE,
283 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
284 		.callback = &ufoma_bulk_read_callback,
285 	},
286 };
287 
288 static const struct ucom_callback ufoma_callback = {
289 	.ucom_cfg_get_status = &ufoma_cfg_get_status,
290 	.ucom_cfg_set_dtr = &ufoma_cfg_set_dtr,
291 	.ucom_cfg_set_rts = &ufoma_cfg_set_rts,
292 	.ucom_cfg_set_break = &ufoma_cfg_set_break,
293 	.ucom_cfg_param = &ufoma_cfg_param,
294 	.ucom_cfg_open = &ufoma_cfg_open,
295 	.ucom_cfg_close = &ufoma_cfg_close,
296 	.ucom_pre_param = &ufoma_pre_param,
297 	.ucom_start_read = &ufoma_start_read,
298 	.ucom_stop_read = &ufoma_stop_read,
299 	.ucom_start_write = &ufoma_start_write,
300 	.ucom_stop_write = &ufoma_stop_write,
301 	.ucom_poll = &ufoma_poll,
302 	.ucom_free = &ufoma_free,
303 };
304 
305 static device_method_t ufoma_methods[] = {
306 	/* Device methods */
307 	DEVMETHOD(device_probe, ufoma_probe),
308 	DEVMETHOD(device_attach, ufoma_attach),
309 	DEVMETHOD(device_detach, ufoma_detach),
310 	DEVMETHOD_END
311 };
312 
313 static devclass_t ufoma_devclass;
314 
315 static driver_t ufoma_driver = {
316 	.name = "ufoma",
317 	.methods = ufoma_methods,
318 	.size = sizeof(struct ufoma_softc),
319 };
320 
321 static const STRUCT_USB_HOST_ID ufoma_devs[] = {
322 	{USB_IFACE_CLASS(UICLASS_CDC),
323 	 USB_IFACE_SUBCLASS(UISUBCLASS_MCPC),},
324 };
325 
326 DRIVER_MODULE(ufoma, uhub, ufoma_driver, ufoma_devclass, NULL, 0);
327 MODULE_DEPEND(ufoma, ucom, 1, 1, 1);
328 MODULE_DEPEND(ufoma, usb, 1, 1, 1);
329 MODULE_VERSION(ufoma, 1);
330 USB_PNP_HOST_INFO(ufoma_devs);
331 
332 static int
333 ufoma_probe(device_t dev)
334 {
335 	struct usb_attach_arg *uaa = device_get_ivars(dev);
336 	struct usb_interface_descriptor *id;
337 	struct usb_config_descriptor *cd;
338 	usb_mcpc_acm_descriptor *mad;
339 	int error;
340 
341 	if (uaa->usb_mode != USB_MODE_HOST)
342 		return (ENXIO);
343 
344 	error = usbd_lookup_id_by_uaa(ufoma_devs, sizeof(ufoma_devs), uaa);
345 	if (error)
346 		return (error);
347 
348 	id = usbd_get_interface_descriptor(uaa->iface);
349 	cd = usbd_get_config_descriptor(uaa->device);
350 
351 	if (id == NULL || cd == NULL)
352 		return (ENXIO);
353 
354 	mad = ufoma_get_intconf(cd, id, UDESC_VS_INTERFACE, UDESCSUB_MCPC_ACM);
355 	if (mad == NULL)
356 		return (ENXIO);
357 
358 #ifndef UFOMA_HANDSFREE
359 	if ((mad->bType == UMCPC_ACM_TYPE_AB5) ||
360 	    (mad->bType == UMCPC_ACM_TYPE_AB6))
361 		return (ENXIO);
362 #endif
363 	return (BUS_PROBE_GENERIC);
364 }
365 
366 static int
367 ufoma_attach(device_t dev)
368 {
369 	struct usb_attach_arg *uaa = device_get_ivars(dev);
370 	struct ufoma_softc *sc = device_get_softc(dev);
371 	struct usb_config_descriptor *cd;
372 	struct usb_interface_descriptor *id;
373 	struct sysctl_ctx_list *sctx;
374 	struct sysctl_oid *soid;
375 
376 	usb_mcpc_acm_descriptor *mad;
377 	uint8_t elements;
378 	int32_t error;
379 
380 	sc->sc_udev = uaa->device;
381 	sc->sc_dev = dev;
382 	sc->sc_unit = device_get_unit(dev);
383 
384 	mtx_init(&sc->sc_mtx, "ufoma", NULL, MTX_DEF);
385 	ucom_ref(&sc->sc_super_ucom);
386 	cv_init(&sc->sc_cv, "CWAIT");
387 
388 	device_set_usb_desc(dev);
389 
390 	DPRINTF("\n");
391 
392 	/* setup control transfers */
393 
394 	cd = usbd_get_config_descriptor(uaa->device);
395 	id = usbd_get_interface_descriptor(uaa->iface);
396 	sc->sc_ctrl_iface_no = id->bInterfaceNumber;
397 	sc->sc_ctrl_iface_index = uaa->info.bIfaceIndex;
398 
399 	error = usbd_transfer_setup(uaa->device,
400 	    &sc->sc_ctrl_iface_index, sc->sc_ctrl_xfer,
401 	    ufoma_ctrl_config, UFOMA_CTRL_ENDPT_MAX, sc, &sc->sc_mtx);
402 
403 	if (error) {
404 		device_printf(dev, "allocating control USB "
405 		    "transfers failed\n");
406 		goto detach;
407 	}
408 	mad = ufoma_get_intconf(cd, id, UDESC_VS_INTERFACE, UDESCSUB_MCPC_ACM);
409 	if (mad == NULL) {
410 		goto detach;
411 	}
412 	if (mad->bFunctionLength < sizeof(*mad)) {
413 		device_printf(dev, "invalid MAD descriptor\n");
414 		goto detach;
415 	}
416 	if ((mad->bType == UMCPC_ACM_TYPE_AB5) ||
417 	    (mad->bType == UMCPC_ACM_TYPE_AB6)) {
418 		sc->sc_nobulk = 1;
419 	} else {
420 		sc->sc_nobulk = 0;
421 		if (ufoma_modem_setup(dev, sc, uaa)) {
422 			goto detach;
423 		}
424 	}
425 
426 	elements = (mad->bFunctionLength - sizeof(*mad) + 1);
427 
428 	/* initialize mode variables */
429 
430 	sc->sc_modetable = malloc(elements + 1, M_USBDEV, M_WAITOK);
431 
432 	if (sc->sc_modetable == NULL) {
433 		goto detach;
434 	}
435 	sc->sc_modetable[0] = (elements + 1);
436 	memcpy(&sc->sc_modetable[1], mad->bMode, elements);
437 
438 	sc->sc_currentmode = UMCPC_ACM_MODE_UNLINKED;
439 	sc->sc_modetoactivate = mad->bMode[0];
440 
441 	/* clear stall at first run, if any */
442 	mtx_lock(&sc->sc_mtx);
443 	usbd_xfer_set_stall(sc->sc_bulk_xfer[UFOMA_BULK_ENDPT_WRITE]);
444 	usbd_xfer_set_stall(sc->sc_bulk_xfer[UFOMA_BULK_ENDPT_READ]);
445 	mtx_unlock(&sc->sc_mtx);
446 
447 	error = ucom_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc,
448 	    &ufoma_callback, &sc->sc_mtx);
449 	if (error) {
450 		DPRINTF("ucom_attach failed\n");
451 		goto detach;
452 	}
453 	ucom_set_pnpinfo_usb(&sc->sc_super_ucom, dev);
454 
455 	/*Sysctls*/
456 	sctx = device_get_sysctl_ctx(dev);
457 	soid = device_get_sysctl_tree(dev);
458 
459 	SYSCTL_ADD_PROC(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "supportmode",
460 	    CTLFLAG_RD | CTLTYPE_STRING | CTLFLAG_MPSAFE, sc, 0,
461 	    ufoma_sysctl_support, "A", "Supporting port role");
462 
463 	SYSCTL_ADD_PROC(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "currentmode",
464 	CTLFLAG_RD | CTLTYPE_STRING | CTLFLAG_MPSAFE, sc, 0,
465 	ufoma_sysctl_current, "A", "Current port role");
466 
467 	SYSCTL_ADD_PROC(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "openmode",
468 	CTLFLAG_RW | CTLTYPE_STRING | CTLFLAG_MPSAFE, sc, 0,
469 	ufoma_sysctl_open, "A", "Mode to transit when port is opened");
470 	SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "comunit",
471 			CTLFLAG_RD, &(sc->sc_super_ucom.sc_unit), 0,
472 			"Unit number as USB serial");
473 
474 	return (0);			/* success */
475 
476 detach:
477 	ufoma_detach(dev);
478 	return (ENXIO);			/* failure */
479 }
480 
481 static int
482 ufoma_detach(device_t dev)
483 {
484 	struct ufoma_softc *sc = device_get_softc(dev);
485 
486 	ucom_detach(&sc->sc_super_ucom, &sc->sc_ucom);
487 	usbd_transfer_unsetup(sc->sc_ctrl_xfer, UFOMA_CTRL_ENDPT_MAX);
488 	usbd_transfer_unsetup(sc->sc_bulk_xfer, UFOMA_BULK_ENDPT_MAX);
489 
490 	if (sc->sc_modetable) {
491 		free(sc->sc_modetable, M_USBDEV);
492 	}
493 	cv_destroy(&sc->sc_cv);
494 
495 	device_claim_softc(dev);
496 
497 	ufoma_free_softc(sc);
498 
499 	return (0);
500 }
501 
502 UCOM_UNLOAD_DRAIN(ufoma);
503 
504 static void
505 ufoma_free_softc(struct ufoma_softc *sc)
506 {
507 	if (ucom_unref(&sc->sc_super_ucom)) {
508 		mtx_destroy(&sc->sc_mtx);
509 		device_free_softc(sc);
510 	}
511 }
512 
513 static void
514 ufoma_free(struct ucom_softc *ucom)
515 {
516 	ufoma_free_softc(ucom->sc_parent);
517 }
518 
519 static void *
520 ufoma_get_intconf(struct usb_config_descriptor *cd, struct usb_interface_descriptor *id,
521     uint8_t type, uint8_t subtype)
522 {
523 	struct usb_descriptor *desc = (void *)id;
524 
525 	while ((desc = usb_desc_foreach(cd, desc))) {
526 
527 		if (desc->bDescriptorType == UDESC_INTERFACE) {
528 			return (NULL);
529 		}
530 		if ((desc->bDescriptorType == type) &&
531 		    (desc->bDescriptorSubtype == subtype)) {
532 			break;
533 		}
534 	}
535 	return (desc);
536 }
537 
538 static void
539 ufoma_cfg_link_state(struct ufoma_softc *sc)
540 {
541 	struct usb_device_request req;
542 	int32_t error;
543 
544 	req.bmRequestType = UT_WRITE_VENDOR_INTERFACE;
545 	req.bRequest = UMCPC_SET_LINK;
546 	USETW(req.wValue, UMCPC_CM_MOBILE_ACM);
547 	USETW(req.wIndex, sc->sc_ctrl_iface_no);
548 	USETW(req.wLength, sc->sc_modetable[0]);
549 
550 	ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
551 	    &req, sc->sc_modetable, 0, 1000);
552 
553 	error = cv_timedwait(&sc->sc_cv, &sc->sc_mtx, hz);
554 
555 	if (error) {
556 		DPRINTF("NO response\n");
557 	}
558 }
559 
560 static void
561 ufoma_cfg_activate_state(struct ufoma_softc *sc, uint16_t state)
562 {
563 	struct usb_device_request req;
564 	int32_t error;
565 
566 	req.bmRequestType = UT_WRITE_VENDOR_INTERFACE;
567 	req.bRequest = UMCPC_ACTIVATE_MODE;
568 	USETW(req.wValue, state);
569 	USETW(req.wIndex, sc->sc_ctrl_iface_no);
570 	USETW(req.wLength, 0);
571 
572 	ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
573 	    &req, NULL, 0, 1000);
574 
575 	error = cv_timedwait(&sc->sc_cv, &sc->sc_mtx,
576 	    (UFOMA_MAX_TIMEOUT * hz));
577 	if (error) {
578 		DPRINTF("No response\n");
579 	}
580 }
581 
582 static void
583 ufoma_ctrl_read_callback(struct usb_xfer *xfer, usb_error_t error)
584 {
585 	struct ufoma_softc *sc = usbd_xfer_softc(xfer);
586 	struct usb_device_request req;
587 	struct usb_page_cache *pc0, *pc1;
588 	int len, aframes, nframes;
589 
590 	usbd_xfer_status(xfer, NULL, NULL, &aframes, &nframes);
591 
592 	switch (USB_GET_STATE(xfer)) {
593 	case USB_ST_TRANSFERRED:
594 tr_transferred:
595 		if (aframes != nframes)
596 			goto tr_setup;
597 		pc1 = usbd_xfer_get_frame(xfer, 1);
598 		len = usbd_xfer_frame_len(xfer, 1);
599 		if (len > 0)
600 			ucom_put_data(&sc->sc_ucom, pc1, 0, len);
601 		/* FALLTHROUGH */
602 	case USB_ST_SETUP:
603 tr_setup:
604 		if (sc->sc_num_msg) {
605 			sc->sc_num_msg--;
606 
607 			req.bmRequestType = UT_READ_CLASS_INTERFACE;
608 			req.bRequest = UCDC_GET_ENCAPSULATED_RESPONSE;
609 			USETW(req.wIndex, sc->sc_ctrl_iface_no);
610 			USETW(req.wValue, 0);
611 			USETW(req.wLength, UFOMA_CMD_BUF_SIZE);
612 
613 			pc0 = usbd_xfer_get_frame(xfer, 0);
614 			usbd_copy_in(pc0, 0, &req, sizeof(req));
615 
616 			usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
617 			usbd_xfer_set_frame_len(xfer, 1, UFOMA_CMD_BUF_SIZE);
618 			usbd_xfer_set_frames(xfer, 2);
619 			usbd_transfer_submit(xfer);
620 		}
621 		return;
622 
623 	default:			/* Error */
624 		DPRINTF("error = %s\n",
625 		    usbd_errstr(error));
626 
627 		if (error == USB_ERR_CANCELLED) {
628 			return;
629 		}
630 		goto tr_transferred;
631 	}
632 }
633 
634 static void
635 ufoma_ctrl_write_callback(struct usb_xfer *xfer, usb_error_t error)
636 {
637 	struct ufoma_softc *sc = usbd_xfer_softc(xfer);
638 	struct usb_device_request req;
639 	struct usb_page_cache *pc;
640 	uint32_t actlen;
641 
642 	switch (USB_GET_STATE(xfer)) {
643 	case USB_ST_TRANSFERRED:
644 tr_transferred:
645 	case USB_ST_SETUP:
646 		pc = usbd_xfer_get_frame(xfer, 1);
647 		if (ucom_get_data(&sc->sc_ucom, pc, 0, 1, &actlen)) {
648 
649 			req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
650 			req.bRequest = UCDC_SEND_ENCAPSULATED_COMMAND;
651 			USETW(req.wIndex, sc->sc_ctrl_iface_no);
652 			USETW(req.wValue, 0);
653 			USETW(req.wLength, 1);
654 
655 			pc = usbd_xfer_get_frame(xfer, 0);
656 			usbd_copy_in(pc, 0, &req, sizeof(req));
657 
658 			usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
659 			usbd_xfer_set_frame_len(xfer, 1, 1);
660 			usbd_xfer_set_frames(xfer, 2);
661 
662 			usbd_transfer_submit(xfer);
663 		}
664 		return;
665 
666 	default:			/* Error */
667 		DPRINTF("error = %s\n", usbd_errstr(error));
668 
669 		if (error == USB_ERR_CANCELLED) {
670 			return;
671 		}
672 		goto tr_transferred;
673 	}
674 }
675 
676 static void
677 ufoma_intr_callback(struct usb_xfer *xfer, usb_error_t error)
678 {
679 	struct ufoma_softc *sc = usbd_xfer_softc(xfer);
680 	struct usb_cdc_notification pkt;
681 	struct usb_page_cache *pc;
682 	uint16_t wLen;
683 	uint16_t temp;
684 	uint8_t mstatus;
685 	int actlen;
686 
687 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
688 
689 	switch (USB_GET_STATE(xfer)) {
690 	case USB_ST_TRANSFERRED:
691 		if (actlen < 8) {
692 			DPRINTF("too short message\n");
693 			goto tr_setup;
694 		}
695 		if (actlen > (int)sizeof(pkt)) {
696 			DPRINTF("truncating message\n");
697 			actlen = sizeof(pkt);
698 		}
699 		pc = usbd_xfer_get_frame(xfer, 0);
700 		usbd_copy_out(pc, 0, &pkt, actlen);
701 
702 		actlen -= 8;
703 
704 		wLen = UGETW(pkt.wLength);
705 		if (actlen > wLen) {
706 			actlen = wLen;
707 		}
708 		if ((pkt.bmRequestType == UT_READ_VENDOR_INTERFACE) &&
709 		    (pkt.bNotification == UMCPC_REQUEST_ACKNOWLEDGE)) {
710 			temp = UGETW(pkt.wValue);
711 			sc->sc_currentmode = (temp >> 8);
712 			if (!(temp & 0xff)) {
713 				DPRINTF("Mode change failed!\n");
714 			}
715 			cv_signal(&sc->sc_cv);
716 		}
717 		if (pkt.bmRequestType != UCDC_NOTIFICATION) {
718 			goto tr_setup;
719 		}
720 		switch (pkt.bNotification) {
721 		case UCDC_N_RESPONSE_AVAILABLE:
722 			if (!(sc->sc_nobulk)) {
723 				DPRINTF("Wrong serial state!\n");
724 				break;
725 			}
726 			if (sc->sc_num_msg != 0xFF) {
727 				sc->sc_num_msg++;
728 			}
729 			usbd_transfer_start(sc->sc_ctrl_xfer[UFOMA_CTRL_ENDPT_READ]);
730 			break;
731 
732 		case UCDC_N_SERIAL_STATE:
733 			if (sc->sc_nobulk) {
734 				DPRINTF("Wrong serial state!\n");
735 				break;
736 			}
737 			/*
738 		         * Set the serial state in ucom driver based on
739 		         * the bits from the notify message
740 		         */
741 			if (actlen < 2) {
742 				DPRINTF("invalid notification "
743 				    "length, %d bytes!\n", actlen);
744 				break;
745 			}
746 			DPRINTF("notify bytes = 0x%02x, 0x%02x\n",
747 			    pkt.data[0], pkt.data[1]);
748 
749 			/* currently, lsr is always zero. */
750 			sc->sc_lsr = 0;
751 			sc->sc_msr = 0;
752 
753 			mstatus = pkt.data[0];
754 
755 			if (mstatus & UCDC_N_SERIAL_RI) {
756 				sc->sc_msr |= SER_RI;
757 			}
758 			if (mstatus & UCDC_N_SERIAL_DSR) {
759 				sc->sc_msr |= SER_DSR;
760 			}
761 			if (mstatus & UCDC_N_SERIAL_DCD) {
762 				sc->sc_msr |= SER_DCD;
763 			}
764 			ucom_status_change(&sc->sc_ucom);
765 			break;
766 
767 		default:
768 			break;
769 		}
770 
771 	case USB_ST_SETUP:
772 tr_setup:
773 		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
774 		usbd_transfer_submit(xfer);
775 		return;
776 
777 	default:			/* Error */
778 		if (error != USB_ERR_CANCELLED) {
779 			/* try to clear stall first */
780 			usbd_xfer_set_stall(xfer);
781 			goto tr_setup;
782 		}
783 		return;
784 	}
785 }
786 
787 static void
788 ufoma_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
789 {
790 	struct ufoma_softc *sc = usbd_xfer_softc(xfer);
791 	struct usb_page_cache *pc;
792 	uint32_t actlen;
793 
794 	switch (USB_GET_STATE(xfer)) {
795 	case USB_ST_SETUP:
796 	case USB_ST_TRANSFERRED:
797 tr_setup:
798 		pc = usbd_xfer_get_frame(xfer, 0);
799 		if (ucom_get_data(&sc->sc_ucom, pc, 0,
800 		    UFOMA_BULK_BUF_SIZE, &actlen)) {
801 			usbd_xfer_set_frame_len(xfer, 0, actlen);
802 			usbd_transfer_submit(xfer);
803 		}
804 		return;
805 
806 	default:			/* Error */
807 		if (error != USB_ERR_CANCELLED) {
808 			/* try to clear stall first */
809 			usbd_xfer_set_stall(xfer);
810 			goto tr_setup;
811 		}
812 		return;
813 	}
814 }
815 
816 static void
817 ufoma_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
818 {
819 	struct ufoma_softc *sc = usbd_xfer_softc(xfer);
820 	struct usb_page_cache *pc;
821 	int actlen;
822 
823 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
824 
825 	switch (USB_GET_STATE(xfer)) {
826 	case USB_ST_TRANSFERRED:
827 		pc = usbd_xfer_get_frame(xfer, 0);
828 		ucom_put_data(&sc->sc_ucom, pc, 0, actlen);
829 
830 	case USB_ST_SETUP:
831 tr_setup:
832 		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
833 		usbd_transfer_submit(xfer);
834 		return;
835 
836 	default:			/* Error */
837 		if (error != USB_ERR_CANCELLED) {
838 			/* try to clear stall first */
839 			usbd_xfer_set_stall(xfer);
840 			goto tr_setup;
841 		}
842 		return;
843 	}
844 }
845 
846 static void
847 ufoma_cfg_open(struct ucom_softc *ucom)
848 {
849 	struct ufoma_softc *sc = ucom->sc_parent;
850 
851 	/* empty input queue */
852 
853 	if (sc->sc_num_msg != 0xFF) {
854 		sc->sc_num_msg++;
855 	}
856 	if (sc->sc_currentmode == UMCPC_ACM_MODE_UNLINKED) {
857 		ufoma_cfg_link_state(sc);
858 	}
859 	if (sc->sc_currentmode == UMCPC_ACM_MODE_DEACTIVATED) {
860 		ufoma_cfg_activate_state(sc, sc->sc_modetoactivate);
861 	}
862 }
863 
864 static void
865 ufoma_cfg_close(struct ucom_softc *ucom)
866 {
867 	struct ufoma_softc *sc = ucom->sc_parent;
868 
869 	ufoma_cfg_activate_state(sc, UMCPC_ACM_MODE_DEACTIVATED);
870 }
871 
872 static void
873 ufoma_cfg_set_break(struct ucom_softc *ucom, uint8_t onoff)
874 {
875 	struct ufoma_softc *sc = ucom->sc_parent;
876 	struct usb_device_request req;
877 	uint16_t wValue;
878 
879 	if (sc->sc_nobulk ||
880 	    (sc->sc_currentmode == UMCPC_ACM_MODE_OBEX)) {
881 		return;
882 	}
883 	if (!(sc->sc_acm_cap & USB_CDC_ACM_HAS_BREAK)) {
884 		return;
885 	}
886 	wValue = onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF;
887 
888 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
889 	req.bRequest = UCDC_SEND_BREAK;
890 	USETW(req.wValue, wValue);
891 	req.wIndex[0] = sc->sc_ctrl_iface_no;
892 	req.wIndex[1] = 0;
893 	USETW(req.wLength, 0);
894 
895 	ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
896 	    &req, NULL, 0, 1000);
897 }
898 
899 static void
900 ufoma_cfg_get_status(struct ucom_softc *ucom, uint8_t *lsr, uint8_t *msr)
901 {
902 	struct ufoma_softc *sc = ucom->sc_parent;
903 
904 	/* XXX Note: sc_lsr is always zero */
905 	*lsr = sc->sc_lsr;
906 	*msr = sc->sc_msr;
907 }
908 
909 static void
910 ufoma_cfg_set_line_state(struct ufoma_softc *sc)
911 {
912 	struct usb_device_request req;
913 
914 	/* Don't send line state emulation request for OBEX port */
915 	if (sc->sc_currentmode == UMCPC_ACM_MODE_OBEX) {
916 		return;
917 	}
918 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
919 	req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
920 	USETW(req.wValue, sc->sc_line);
921 	req.wIndex[0] = sc->sc_ctrl_iface_no;
922 	req.wIndex[1] = 0;
923 	USETW(req.wLength, 0);
924 
925 	ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
926 	    &req, NULL, 0, 1000);
927 }
928 
929 static void
930 ufoma_cfg_set_dtr(struct ucom_softc *ucom, uint8_t onoff)
931 {
932 	struct ufoma_softc *sc = ucom->sc_parent;
933 
934 	if (sc->sc_nobulk) {
935 		return;
936 	}
937 	if (onoff)
938 		sc->sc_line |= UCDC_LINE_DTR;
939 	else
940 		sc->sc_line &= ~UCDC_LINE_DTR;
941 
942 	ufoma_cfg_set_line_state(sc);
943 }
944 
945 static void
946 ufoma_cfg_set_rts(struct ucom_softc *ucom, uint8_t onoff)
947 {
948 	struct ufoma_softc *sc = ucom->sc_parent;
949 
950 	if (sc->sc_nobulk) {
951 		return;
952 	}
953 	if (onoff)
954 		sc->sc_line |= UCDC_LINE_RTS;
955 	else
956 		sc->sc_line &= ~UCDC_LINE_RTS;
957 
958 	ufoma_cfg_set_line_state(sc);
959 }
960 
961 static int
962 ufoma_pre_param(struct ucom_softc *ucom, struct termios *t)
963 {
964 	return (0);			/* we accept anything */
965 }
966 
967 static void
968 ufoma_cfg_param(struct ucom_softc *ucom, struct termios *t)
969 {
970 	struct ufoma_softc *sc = ucom->sc_parent;
971 	struct usb_device_request req;
972 	struct usb_cdc_line_state ls;
973 
974 	if (sc->sc_nobulk ||
975 	    (sc->sc_currentmode == UMCPC_ACM_MODE_OBEX)) {
976 		return;
977 	}
978 	DPRINTF("\n");
979 
980 	memset(&ls, 0, sizeof(ls));
981 
982 	USETDW(ls.dwDTERate, t->c_ospeed);
983 
984 	if (t->c_cflag & CSTOPB) {
985 		ls.bCharFormat = UCDC_STOP_BIT_2;
986 	} else {
987 		ls.bCharFormat = UCDC_STOP_BIT_1;
988 	}
989 
990 	if (t->c_cflag & PARENB) {
991 		if (t->c_cflag & PARODD) {
992 			ls.bParityType = UCDC_PARITY_ODD;
993 		} else {
994 			ls.bParityType = UCDC_PARITY_EVEN;
995 		}
996 	} else {
997 		ls.bParityType = UCDC_PARITY_NONE;
998 	}
999 
1000 	switch (t->c_cflag & CSIZE) {
1001 	case CS5:
1002 		ls.bDataBits = 5;
1003 		break;
1004 	case CS6:
1005 		ls.bDataBits = 6;
1006 		break;
1007 	case CS7:
1008 		ls.bDataBits = 7;
1009 		break;
1010 	case CS8:
1011 		ls.bDataBits = 8;
1012 		break;
1013 	}
1014 
1015 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
1016 	req.bRequest = UCDC_SET_LINE_CODING;
1017 	USETW(req.wValue, 0);
1018 	req.wIndex[0] = sc->sc_ctrl_iface_no;
1019 	req.wIndex[1] = 0;
1020 	USETW(req.wLength, UCDC_LINE_STATE_LENGTH);
1021 
1022 	ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
1023 	    &req, &ls, 0, 1000);
1024 }
1025 
1026 static int
1027 ufoma_modem_setup(device_t dev, struct ufoma_softc *sc,
1028     struct usb_attach_arg *uaa)
1029 {
1030 	struct usb_config_descriptor *cd;
1031 	struct usb_cdc_acm_descriptor *acm;
1032 	struct usb_cdc_cm_descriptor *cmd;
1033 	struct usb_interface_descriptor *id;
1034 	struct usb_interface *iface;
1035 	uint8_t i;
1036 	int32_t error;
1037 
1038 	cd = usbd_get_config_descriptor(uaa->device);
1039 	id = usbd_get_interface_descriptor(uaa->iface);
1040 
1041 	cmd = ufoma_get_intconf(cd, id, UDESC_CS_INTERFACE, UDESCSUB_CDC_CM);
1042 
1043 	if ((cmd == NULL) ||
1044 	    (cmd->bLength < sizeof(*cmd))) {
1045 		return (EINVAL);
1046 	}
1047 	sc->sc_cm_cap = cmd->bmCapabilities;
1048 	sc->sc_data_iface_no = cmd->bDataInterface;
1049 
1050 	acm = ufoma_get_intconf(cd, id, UDESC_CS_INTERFACE, UDESCSUB_CDC_ACM);
1051 
1052 	if ((acm == NULL) ||
1053 	    (acm->bLength < sizeof(*acm))) {
1054 		return (EINVAL);
1055 	}
1056 	sc->sc_acm_cap = acm->bmCapabilities;
1057 
1058 	device_printf(dev, "data interface %d, has %sCM over data, "
1059 	    "has %sbreak\n",
1060 	    sc->sc_data_iface_no,
1061 	    sc->sc_cm_cap & USB_CDC_CM_OVER_DATA ? "" : "no ",
1062 	    sc->sc_acm_cap & USB_CDC_ACM_HAS_BREAK ? "" : "no ");
1063 
1064 	/* get the data interface too */
1065 
1066 	for (i = 0;; i++) {
1067 
1068 		iface = usbd_get_iface(uaa->device, i);
1069 
1070 		if (iface) {
1071 
1072 			id = usbd_get_interface_descriptor(iface);
1073 
1074 			if (id && (id->bInterfaceNumber == sc->sc_data_iface_no)) {
1075 				sc->sc_data_iface_index = i;
1076 				usbd_set_parent_iface(uaa->device, i, uaa->info.bIfaceIndex);
1077 				break;
1078 			}
1079 		} else {
1080 			device_printf(dev, "no data interface\n");
1081 			return (EINVAL);
1082 		}
1083 	}
1084 
1085 	error = usbd_transfer_setup(uaa->device,
1086 	    &sc->sc_data_iface_index, sc->sc_bulk_xfer,
1087 	    ufoma_bulk_config, UFOMA_BULK_ENDPT_MAX, sc, &sc->sc_mtx);
1088 
1089 	if (error) {
1090 		device_printf(dev, "allocating BULK USB "
1091 		    "transfers failed\n");
1092 		return (EINVAL);
1093 	}
1094 	return (0);
1095 }
1096 
1097 static void
1098 ufoma_start_read(struct ucom_softc *ucom)
1099 {
1100 	struct ufoma_softc *sc = ucom->sc_parent;
1101 
1102 	/* start interrupt transfer */
1103 	usbd_transfer_start(sc->sc_ctrl_xfer[UFOMA_CTRL_ENDPT_INTR]);
1104 
1105 	/* start data transfer */
1106 	if (sc->sc_nobulk) {
1107 		usbd_transfer_start(sc->sc_ctrl_xfer[UFOMA_CTRL_ENDPT_READ]);
1108 	} else {
1109 		usbd_transfer_start(sc->sc_bulk_xfer[UFOMA_BULK_ENDPT_READ]);
1110 	}
1111 }
1112 
1113 static void
1114 ufoma_stop_read(struct ucom_softc *ucom)
1115 {
1116 	struct ufoma_softc *sc = ucom->sc_parent;
1117 
1118 	/* stop interrupt transfer */
1119 	usbd_transfer_stop(sc->sc_ctrl_xfer[UFOMA_CTRL_ENDPT_INTR]);
1120 
1121 	/* stop data transfer */
1122 	if (sc->sc_nobulk) {
1123 		usbd_transfer_stop(sc->sc_ctrl_xfer[UFOMA_CTRL_ENDPT_READ]);
1124 	} else {
1125 		usbd_transfer_stop(sc->sc_bulk_xfer[UFOMA_BULK_ENDPT_READ]);
1126 	}
1127 }
1128 
1129 static void
1130 ufoma_start_write(struct ucom_softc *ucom)
1131 {
1132 	struct ufoma_softc *sc = ucom->sc_parent;
1133 
1134 	if (sc->sc_nobulk) {
1135 		usbd_transfer_start(sc->sc_ctrl_xfer[UFOMA_CTRL_ENDPT_WRITE]);
1136 	} else {
1137 		usbd_transfer_start(sc->sc_bulk_xfer[UFOMA_BULK_ENDPT_WRITE]);
1138 	}
1139 }
1140 
1141 static void
1142 ufoma_stop_write(struct ucom_softc *ucom)
1143 {
1144 	struct ufoma_softc *sc = ucom->sc_parent;
1145 
1146 	if (sc->sc_nobulk) {
1147 		usbd_transfer_stop(sc->sc_ctrl_xfer[UFOMA_CTRL_ENDPT_WRITE]);
1148 	} else {
1149 		usbd_transfer_stop(sc->sc_bulk_xfer[UFOMA_BULK_ENDPT_WRITE]);
1150 	}
1151 }
1152 
1153 static struct umcpc_modetostr_tab{
1154 	int mode;
1155 	char *str;
1156 }umcpc_modetostr_tab[]={
1157 	{UMCPC_ACM_MODE_DEACTIVATED, "deactivated"},
1158 	{UMCPC_ACM_MODE_MODEM, "modem"},
1159 	{UMCPC_ACM_MODE_ATCOMMAND, "handsfree"},
1160 	{UMCPC_ACM_MODE_OBEX, "obex"},
1161 	{UMCPC_ACM_MODE_VENDOR1, "vendor1"},
1162 	{UMCPC_ACM_MODE_VENDOR2, "vendor2"},
1163 	{UMCPC_ACM_MODE_UNLINKED, "unlinked"},
1164 	{0, NULL}
1165 };
1166 
1167 static char *ufoma_mode_to_str(int mode)
1168 {
1169 	int i;
1170 	for(i = 0 ;umcpc_modetostr_tab[i].str != NULL; i++){
1171 		if(umcpc_modetostr_tab[i].mode == mode){
1172 			return umcpc_modetostr_tab[i].str;
1173 		}
1174 	}
1175 	return NULL;
1176 }
1177 
1178 static int ufoma_str_to_mode(char *str)
1179 {
1180 	int i;
1181 	for(i = 0 ;umcpc_modetostr_tab[i].str != NULL; i++){
1182 		if(strcmp(str, umcpc_modetostr_tab[i].str)==0){
1183 			return umcpc_modetostr_tab[i].mode;
1184 		}
1185 	}
1186 	return -1;
1187 }
1188 
1189 static int ufoma_sysctl_support(SYSCTL_HANDLER_ARGS)
1190 {
1191 	struct ufoma_softc *sc = (struct ufoma_softc *)oidp->oid_arg1;
1192 	struct sbuf sb;
1193 	int i;
1194 	char *mode;
1195 
1196 	sbuf_new(&sb, NULL, 1, SBUF_AUTOEXTEND);
1197 	for(i = 1; i < sc->sc_modetable[0]; i++){
1198 		mode = ufoma_mode_to_str(sc->sc_modetable[i]);
1199 		if(mode !=NULL){
1200 			sbuf_cat(&sb, mode);
1201 		}else{
1202 			sbuf_printf(&sb, "(%02x)", sc->sc_modetable[i]);
1203 		}
1204 		if(i < (sc->sc_modetable[0]-1))
1205 			sbuf_cat(&sb, ",");
1206 	}
1207 	sbuf_trim(&sb);
1208 	sbuf_finish(&sb);
1209 	sysctl_handle_string(oidp, sbuf_data(&sb), sbuf_len(&sb), req);
1210 	sbuf_delete(&sb);
1211 
1212 	return 0;
1213 }
1214 static int ufoma_sysctl_current(SYSCTL_HANDLER_ARGS)
1215 {
1216 	struct ufoma_softc *sc = (struct ufoma_softc *)oidp->oid_arg1;
1217 	char *mode;
1218 	char subbuf[]="(XXX)";
1219 	mode = ufoma_mode_to_str(sc->sc_currentmode);
1220 	if(!mode){
1221 		mode = subbuf;
1222 		snprintf(subbuf, sizeof(subbuf), "(%02x)", sc->sc_currentmode);
1223 	}
1224 	sysctl_handle_string(oidp, mode, strlen(mode), req);
1225 
1226 	return 0;
1227 
1228 }
1229 static int ufoma_sysctl_open(SYSCTL_HANDLER_ARGS)
1230 {
1231 	struct ufoma_softc *sc = (struct ufoma_softc *)oidp->oid_arg1;
1232 	char *mode;
1233 	char subbuf[40];
1234 	int newmode;
1235 	int error;
1236 	int i;
1237 
1238 	mode = ufoma_mode_to_str(sc->sc_modetoactivate);
1239 	if(mode){
1240 		strncpy(subbuf, mode, sizeof(subbuf));
1241 	}else{
1242 		snprintf(subbuf, sizeof(subbuf), "(%02x)", sc->sc_modetoactivate);
1243 	}
1244 	error = sysctl_handle_string(oidp, subbuf, sizeof(subbuf), req);
1245 	if(error != 0 || req->newptr == NULL){
1246 		return error;
1247 	}
1248 
1249 	if((newmode = ufoma_str_to_mode(subbuf)) == -1){
1250 		return EINVAL;
1251 	}
1252 
1253 	for(i = 1 ; i < sc->sc_modetable[0] ; i++){
1254 		if(sc->sc_modetable[i] == newmode){
1255 			sc->sc_modetoactivate = newmode;
1256 			return 0;
1257 		}
1258 	}
1259 
1260 	return EINVAL;
1261 }
1262 
1263 static void
1264 ufoma_poll(struct ucom_softc *ucom)
1265 {
1266 	struct ufoma_softc *sc = ucom->sc_parent;
1267 	usbd_transfer_poll(sc->sc_ctrl_xfer, UFOMA_CTRL_ENDPT_MAX);
1268 	usbd_transfer_poll(sc->sc_bulk_xfer, UFOMA_BULK_ENDPT_MAX);
1269 }
1270