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