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