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