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