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