xref: /freebsd/sys/dev/usb/serial/ufoma.c (revision 22cf89c938886d14f5796fc49f9f020c23ea8eaf)
1 /*	$NetBSD: umodem.c,v 1.45 2002/09/23 05:51:23 simonb Exp $	*/
2 
3 #include <sys/cdefs.h>
4 #define UFOMA_HANDSFREE
5 /*-
6  * SPDX-License-Identifier: BSD-2-Clause
7  *
8  * Copyright (c) 2005, Takanori Watanabe All rights reserved.
9  * Copyright (c) 2003 M. Warner Losh <imp@FreeBSD.org>
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 	[UFOMA_CTRL_ENDPT_INTR] = {
236 		.type = UE_INTERRUPT,
237 		.endpoint = UE_ADDR_ANY,
238 		.direction = UE_DIR_IN,
239 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
240 		.bufsize = sizeof(struct usb_cdc_notification),
241 		.callback = &ufoma_intr_callback,
242 	},
243 
244 	[UFOMA_CTRL_ENDPT_READ] = {
245 		.type = UE_CONTROL,
246 		.endpoint = 0x00,	/* Control pipe */
247 		.direction = UE_DIR_ANY,
248 		.bufsize = (sizeof(struct usb_device_request) + UFOMA_CMD_BUF_SIZE),
249 		.flags = {.short_xfer_ok = 1,},
250 		.callback = &ufoma_ctrl_read_callback,
251 		.timeout = 1000,	/* 1 second */
252 	},
253 
254 	[UFOMA_CTRL_ENDPT_WRITE] = {
255 		.type = UE_CONTROL,
256 		.endpoint = 0x00,	/* Control pipe */
257 		.direction = UE_DIR_ANY,
258 		.bufsize = (sizeof(struct usb_device_request) + 1),
259 		.callback = &ufoma_ctrl_write_callback,
260 		.timeout = 1000,	/* 1 second */
261 	},
262 };
263 
264 static const struct usb_config
265 	ufoma_bulk_config[UFOMA_BULK_ENDPT_MAX] = {
266 	[UFOMA_BULK_ENDPT_WRITE] = {
267 		.type = UE_BULK,
268 		.endpoint = UE_ADDR_ANY,
269 		.direction = UE_DIR_OUT,
270 		.bufsize = UFOMA_BULK_BUF_SIZE,
271 		.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
272 		.callback = &ufoma_bulk_write_callback,
273 	},
274 
275 	[UFOMA_BULK_ENDPT_READ] = {
276 		.type = UE_BULK,
277 		.endpoint = UE_ADDR_ANY,
278 		.direction = UE_DIR_IN,
279 		.bufsize = UFOMA_BULK_BUF_SIZE,
280 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
281 		.callback = &ufoma_bulk_read_callback,
282 	},
283 };
284 
285 static const struct ucom_callback ufoma_callback = {
286 	.ucom_cfg_get_status = &ufoma_cfg_get_status,
287 	.ucom_cfg_set_dtr = &ufoma_cfg_set_dtr,
288 	.ucom_cfg_set_rts = &ufoma_cfg_set_rts,
289 	.ucom_cfg_set_break = &ufoma_cfg_set_break,
290 	.ucom_cfg_param = &ufoma_cfg_param,
291 	.ucom_cfg_open = &ufoma_cfg_open,
292 	.ucom_cfg_close = &ufoma_cfg_close,
293 	.ucom_pre_param = &ufoma_pre_param,
294 	.ucom_start_read = &ufoma_start_read,
295 	.ucom_stop_read = &ufoma_stop_read,
296 	.ucom_start_write = &ufoma_start_write,
297 	.ucom_stop_write = &ufoma_stop_write,
298 	.ucom_poll = &ufoma_poll,
299 	.ucom_free = &ufoma_free,
300 };
301 
302 static device_method_t ufoma_methods[] = {
303 	/* Device methods */
304 	DEVMETHOD(device_probe, ufoma_probe),
305 	DEVMETHOD(device_attach, ufoma_attach),
306 	DEVMETHOD(device_detach, ufoma_detach),
307 	DEVMETHOD_END
308 };
309 
310 static driver_t ufoma_driver = {
311 	.name = "ufoma",
312 	.methods = ufoma_methods,
313 	.size = sizeof(struct ufoma_softc),
314 };
315 
316 static const STRUCT_USB_HOST_ID ufoma_devs[] = {
317 	{USB_IFACE_CLASS(UICLASS_CDC),
318 	 USB_IFACE_SUBCLASS(UISUBCLASS_MCPC),},
319 };
320 
321 DRIVER_MODULE(ufoma, uhub, ufoma_driver, NULL, NULL);
322 MODULE_DEPEND(ufoma, ucom, 1, 1, 1);
323 MODULE_DEPEND(ufoma, usb, 1, 1, 1);
324 MODULE_VERSION(ufoma, 1);
325 USB_PNP_HOST_INFO(ufoma_devs);
326 
327 static int
328 ufoma_probe(device_t dev)
329 {
330 	struct usb_attach_arg *uaa = device_get_ivars(dev);
331 	struct usb_interface_descriptor *id;
332 	struct usb_config_descriptor *cd;
333 	usb_mcpc_acm_descriptor *mad;
334 	int error;
335 
336 	if (uaa->usb_mode != USB_MODE_HOST)
337 		return (ENXIO);
338 
339 	error = usbd_lookup_id_by_uaa(ufoma_devs, sizeof(ufoma_devs), uaa);
340 	if (error)
341 		return (error);
342 
343 	id = usbd_get_interface_descriptor(uaa->iface);
344 	cd = usbd_get_config_descriptor(uaa->device);
345 
346 	if (id == NULL || cd == NULL)
347 		return (ENXIO);
348 
349 	mad = ufoma_get_intconf(cd, id, UDESC_VS_INTERFACE, UDESCSUB_MCPC_ACM);
350 	if (mad == NULL)
351 		return (ENXIO);
352 
353 #ifndef UFOMA_HANDSFREE
354 	if ((mad->bType == UMCPC_ACM_TYPE_AB5) ||
355 	    (mad->bType == UMCPC_ACM_TYPE_AB6))
356 		return (ENXIO);
357 #endif
358 	return (BUS_PROBE_GENERIC);
359 }
360 
361 static int
362 ufoma_attach(device_t dev)
363 {
364 	struct usb_attach_arg *uaa = device_get_ivars(dev);
365 	struct ufoma_softc *sc = device_get_softc(dev);
366 	struct usb_config_descriptor *cd;
367 	struct usb_interface_descriptor *id;
368 	struct sysctl_ctx_list *sctx;
369 	struct sysctl_oid *soid;
370 
371 	usb_mcpc_acm_descriptor *mad;
372 	uint8_t elements;
373 	int32_t error;
374 
375 	sc->sc_udev = uaa->device;
376 	sc->sc_dev = dev;
377 	sc->sc_unit = device_get_unit(dev);
378 
379 	mtx_init(&sc->sc_mtx, "ufoma", NULL, MTX_DEF);
380 	ucom_ref(&sc->sc_super_ucom);
381 	cv_init(&sc->sc_cv, "CWAIT");
382 
383 	device_set_usb_desc(dev);
384 
385 	DPRINTF("\n");
386 
387 	/* setup control transfers */
388 
389 	cd = usbd_get_config_descriptor(uaa->device);
390 	id = usbd_get_interface_descriptor(uaa->iface);
391 	sc->sc_ctrl_iface_no = id->bInterfaceNumber;
392 	sc->sc_ctrl_iface_index = uaa->info.bIfaceIndex;
393 
394 	error = usbd_transfer_setup(uaa->device,
395 	    &sc->sc_ctrl_iface_index, sc->sc_ctrl_xfer,
396 	    ufoma_ctrl_config, UFOMA_CTRL_ENDPT_MAX, sc, &sc->sc_mtx);
397 
398 	if (error) {
399 		device_printf(dev, "allocating control USB "
400 		    "transfers failed\n");
401 		goto detach;
402 	}
403 	mad = ufoma_get_intconf(cd, id, UDESC_VS_INTERFACE, UDESCSUB_MCPC_ACM);
404 	if (mad == NULL) {
405 		goto detach;
406 	}
407 	if (mad->bFunctionLength < sizeof(*mad)) {
408 		device_printf(dev, "invalid MAD descriptor\n");
409 		goto detach;
410 	}
411 	if ((mad->bType == UMCPC_ACM_TYPE_AB5) ||
412 	    (mad->bType == UMCPC_ACM_TYPE_AB6)) {
413 		sc->sc_nobulk = 1;
414 	} else {
415 		sc->sc_nobulk = 0;
416 		if (ufoma_modem_setup(dev, sc, uaa)) {
417 			goto detach;
418 		}
419 	}
420 
421 	elements = (mad->bFunctionLength - sizeof(*mad) + 1);
422 
423 	/* initialize mode variables */
424 
425 	sc->sc_modetable = malloc(elements + 1, M_USBDEV, M_WAITOK);
426 
427 	if (sc->sc_modetable == NULL) {
428 		goto detach;
429 	}
430 	sc->sc_modetable[0] = (elements + 1);
431 	memcpy(&sc->sc_modetable[1], mad->bMode, elements);
432 
433 	sc->sc_currentmode = UMCPC_ACM_MODE_UNLINKED;
434 	sc->sc_modetoactivate = mad->bMode[0];
435 
436 	/* clear stall at first run, if any */
437 	mtx_lock(&sc->sc_mtx);
438 	usbd_xfer_set_stall(sc->sc_bulk_xfer[UFOMA_BULK_ENDPT_WRITE]);
439 	usbd_xfer_set_stall(sc->sc_bulk_xfer[UFOMA_BULK_ENDPT_READ]);
440 	mtx_unlock(&sc->sc_mtx);
441 
442 	error = ucom_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc,
443 	    &ufoma_callback, &sc->sc_mtx);
444 	if (error) {
445 		DPRINTF("ucom_attach failed\n");
446 		goto detach;
447 	}
448 	ucom_set_pnpinfo_usb(&sc->sc_super_ucom, dev);
449 
450 	/*Sysctls*/
451 	sctx = device_get_sysctl_ctx(dev);
452 	soid = device_get_sysctl_tree(dev);
453 
454 	SYSCTL_ADD_PROC(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "supportmode",
455 	    CTLFLAG_RD | CTLTYPE_STRING | CTLFLAG_MPSAFE, sc, 0,
456 	    ufoma_sysctl_support, "A", "Supporting port role");
457 
458 	SYSCTL_ADD_PROC(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "currentmode",
459 	CTLFLAG_RD | CTLTYPE_STRING | CTLFLAG_MPSAFE, sc, 0,
460 	ufoma_sysctl_current, "A", "Current port role");
461 
462 	SYSCTL_ADD_PROC(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "openmode",
463 	CTLFLAG_RW | CTLTYPE_STRING | CTLFLAG_MPSAFE, sc, 0,
464 	ufoma_sysctl_open, "A", "Mode to transit when port is opened");
465 	SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "comunit",
466 			CTLFLAG_RD, &(sc->sc_super_ucom.sc_unit), 0,
467 			"Unit number as USB serial");
468 
469 	return (0);			/* success */
470 
471 detach:
472 	ufoma_detach(dev);
473 	return (ENXIO);			/* failure */
474 }
475 
476 static int
477 ufoma_detach(device_t dev)
478 {
479 	struct ufoma_softc *sc = device_get_softc(dev);
480 
481 	ucom_detach(&sc->sc_super_ucom, &sc->sc_ucom);
482 	usbd_transfer_unsetup(sc->sc_ctrl_xfer, UFOMA_CTRL_ENDPT_MAX);
483 	usbd_transfer_unsetup(sc->sc_bulk_xfer, UFOMA_BULK_ENDPT_MAX);
484 
485 	if (sc->sc_modetable) {
486 		free(sc->sc_modetable, M_USBDEV);
487 	}
488 	cv_destroy(&sc->sc_cv);
489 
490 	device_claim_softc(dev);
491 
492 	ufoma_free_softc(sc);
493 
494 	return (0);
495 }
496 
497 UCOM_UNLOAD_DRAIN(ufoma);
498 
499 static void
500 ufoma_free_softc(struct ufoma_softc *sc)
501 {
502 	if (ucom_unref(&sc->sc_super_ucom)) {
503 		mtx_destroy(&sc->sc_mtx);
504 		device_free_softc(sc);
505 	}
506 }
507 
508 static void
509 ufoma_free(struct ucom_softc *ucom)
510 {
511 	ufoma_free_softc(ucom->sc_parent);
512 }
513 
514 static void *
515 ufoma_get_intconf(struct usb_config_descriptor *cd, struct usb_interface_descriptor *id,
516     uint8_t type, uint8_t subtype)
517 {
518 	struct usb_descriptor *desc = (void *)id;
519 
520 	while ((desc = usb_desc_foreach(cd, desc))) {
521 		if (desc->bDescriptorType == UDESC_INTERFACE) {
522 			return (NULL);
523 		}
524 		if ((desc->bDescriptorType == type) &&
525 		    (desc->bDescriptorSubtype == subtype)) {
526 			break;
527 		}
528 	}
529 	return (desc);
530 }
531 
532 static void
533 ufoma_cfg_link_state(struct ufoma_softc *sc)
534 {
535 	struct usb_device_request req;
536 	int32_t error;
537 
538 	req.bmRequestType = UT_WRITE_VENDOR_INTERFACE;
539 	req.bRequest = UMCPC_SET_LINK;
540 	USETW(req.wValue, UMCPC_CM_MOBILE_ACM);
541 	USETW(req.wIndex, sc->sc_ctrl_iface_no);
542 	USETW(req.wLength, sc->sc_modetable[0]);
543 
544 	ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
545 	    &req, sc->sc_modetable, 0, 1000);
546 
547 	error = cv_timedwait(&sc->sc_cv, &sc->sc_mtx, hz);
548 
549 	if (error) {
550 		DPRINTF("NO response\n");
551 	}
552 }
553 
554 static void
555 ufoma_cfg_activate_state(struct ufoma_softc *sc, uint16_t state)
556 {
557 	struct usb_device_request req;
558 	int32_t error;
559 
560 	req.bmRequestType = UT_WRITE_VENDOR_INTERFACE;
561 	req.bRequest = UMCPC_ACTIVATE_MODE;
562 	USETW(req.wValue, state);
563 	USETW(req.wIndex, sc->sc_ctrl_iface_no);
564 	USETW(req.wLength, 0);
565 
566 	ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
567 	    &req, NULL, 0, 1000);
568 
569 	error = cv_timedwait(&sc->sc_cv, &sc->sc_mtx,
570 	    (UFOMA_MAX_TIMEOUT * hz));
571 	if (error) {
572 		DPRINTF("No response\n");
573 	}
574 }
575 
576 static void
577 ufoma_ctrl_read_callback(struct usb_xfer *xfer, usb_error_t error)
578 {
579 	struct ufoma_softc *sc = usbd_xfer_softc(xfer);
580 	struct usb_device_request req;
581 	struct usb_page_cache *pc0, *pc1;
582 	int len, aframes, nframes;
583 
584 	usbd_xfer_status(xfer, NULL, NULL, &aframes, &nframes);
585 
586 	switch (USB_GET_STATE(xfer)) {
587 	case USB_ST_TRANSFERRED:
588 tr_transferred:
589 		if (aframes != nframes)
590 			goto tr_setup;
591 		pc1 = usbd_xfer_get_frame(xfer, 1);
592 		len = usbd_xfer_frame_len(xfer, 1);
593 		if (len > 0)
594 			ucom_put_data(&sc->sc_ucom, pc1, 0, len);
595 		/* FALLTHROUGH */
596 	case USB_ST_SETUP:
597 tr_setup:
598 		if (sc->sc_num_msg) {
599 			sc->sc_num_msg--;
600 
601 			req.bmRequestType = UT_READ_CLASS_INTERFACE;
602 			req.bRequest = UCDC_GET_ENCAPSULATED_RESPONSE;
603 			USETW(req.wIndex, sc->sc_ctrl_iface_no);
604 			USETW(req.wValue, 0);
605 			USETW(req.wLength, UFOMA_CMD_BUF_SIZE);
606 
607 			pc0 = usbd_xfer_get_frame(xfer, 0);
608 			usbd_copy_in(pc0, 0, &req, sizeof(req));
609 
610 			usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
611 			usbd_xfer_set_frame_len(xfer, 1, UFOMA_CMD_BUF_SIZE);
612 			usbd_xfer_set_frames(xfer, 2);
613 			usbd_transfer_submit(xfer);
614 		}
615 		return;
616 
617 	default:			/* Error */
618 		DPRINTF("error = %s\n",
619 		    usbd_errstr(error));
620 
621 		if (error == USB_ERR_CANCELLED) {
622 			return;
623 		}
624 		goto tr_transferred;
625 	}
626 }
627 
628 static void
629 ufoma_ctrl_write_callback(struct usb_xfer *xfer, usb_error_t error)
630 {
631 	struct ufoma_softc *sc = usbd_xfer_softc(xfer);
632 	struct usb_device_request req;
633 	struct usb_page_cache *pc;
634 	uint32_t actlen;
635 
636 	switch (USB_GET_STATE(xfer)) {
637 	case USB_ST_TRANSFERRED:
638 tr_transferred:
639 	case USB_ST_SETUP:
640 		pc = usbd_xfer_get_frame(xfer, 1);
641 		if (ucom_get_data(&sc->sc_ucom, pc, 0, 1, &actlen)) {
642 			req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
643 			req.bRequest = UCDC_SEND_ENCAPSULATED_COMMAND;
644 			USETW(req.wIndex, sc->sc_ctrl_iface_no);
645 			USETW(req.wValue, 0);
646 			USETW(req.wLength, 1);
647 
648 			pc = usbd_xfer_get_frame(xfer, 0);
649 			usbd_copy_in(pc, 0, &req, sizeof(req));
650 
651 			usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
652 			usbd_xfer_set_frame_len(xfer, 1, 1);
653 			usbd_xfer_set_frames(xfer, 2);
654 
655 			usbd_transfer_submit(xfer);
656 		}
657 		return;
658 
659 	default:			/* Error */
660 		DPRINTF("error = %s\n", usbd_errstr(error));
661 
662 		if (error == USB_ERR_CANCELLED) {
663 			return;
664 		}
665 		goto tr_transferred;
666 	}
667 }
668 
669 static void
670 ufoma_intr_callback(struct usb_xfer *xfer, usb_error_t error)
671 {
672 	struct ufoma_softc *sc = usbd_xfer_softc(xfer);
673 	struct usb_cdc_notification pkt;
674 	struct usb_page_cache *pc;
675 	uint16_t wLen;
676 	uint16_t temp;
677 	uint8_t mstatus;
678 	int actlen;
679 
680 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
681 
682 	switch (USB_GET_STATE(xfer)) {
683 	case USB_ST_TRANSFERRED:
684 		if (actlen < 8) {
685 			DPRINTF("too short message\n");
686 			goto tr_setup;
687 		}
688 		if (actlen > (int)sizeof(pkt)) {
689 			DPRINTF("truncating message\n");
690 			actlen = sizeof(pkt);
691 		}
692 		pc = usbd_xfer_get_frame(xfer, 0);
693 		usbd_copy_out(pc, 0, &pkt, actlen);
694 
695 		actlen -= 8;
696 
697 		wLen = UGETW(pkt.wLength);
698 		if (actlen > wLen) {
699 			actlen = wLen;
700 		}
701 		if ((pkt.bmRequestType == UT_READ_VENDOR_INTERFACE) &&
702 		    (pkt.bNotification == UMCPC_REQUEST_ACKNOWLEDGE)) {
703 			temp = UGETW(pkt.wValue);
704 			sc->sc_currentmode = (temp >> 8);
705 			if (!(temp & 0xff)) {
706 				DPRINTF("Mode change failed!\n");
707 			}
708 			cv_signal(&sc->sc_cv);
709 		}
710 		if (pkt.bmRequestType != UCDC_NOTIFICATION) {
711 			goto tr_setup;
712 		}
713 		switch (pkt.bNotification) {
714 		case UCDC_N_RESPONSE_AVAILABLE:
715 			if (!(sc->sc_nobulk)) {
716 				DPRINTF("Wrong serial state!\n");
717 				break;
718 			}
719 			if (sc->sc_num_msg != 0xFF) {
720 				sc->sc_num_msg++;
721 			}
722 			usbd_transfer_start(sc->sc_ctrl_xfer[UFOMA_CTRL_ENDPT_READ]);
723 			break;
724 
725 		case UCDC_N_SERIAL_STATE:
726 			if (sc->sc_nobulk) {
727 				DPRINTF("Wrong serial state!\n");
728 				break;
729 			}
730 			/*
731 		         * Set the serial state in ucom driver based on
732 		         * the bits from the notify message
733 		         */
734 			if (actlen < 2) {
735 				DPRINTF("invalid notification "
736 				    "length, %d bytes!\n", actlen);
737 				break;
738 			}
739 			DPRINTF("notify bytes = 0x%02x, 0x%02x\n",
740 			    pkt.data[0], pkt.data[1]);
741 
742 			/* currently, lsr is always zero. */
743 			sc->sc_lsr = 0;
744 			sc->sc_msr = 0;
745 
746 			mstatus = pkt.data[0];
747 
748 			if (mstatus & UCDC_N_SERIAL_RI) {
749 				sc->sc_msr |= SER_RI;
750 			}
751 			if (mstatus & UCDC_N_SERIAL_DSR) {
752 				sc->sc_msr |= SER_DSR;
753 			}
754 			if (mstatus & UCDC_N_SERIAL_DCD) {
755 				sc->sc_msr |= SER_DCD;
756 			}
757 			ucom_status_change(&sc->sc_ucom);
758 			break;
759 
760 		default:
761 			break;
762 		}
763 
764 	case USB_ST_SETUP:
765 tr_setup:
766 		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
767 		usbd_transfer_submit(xfer);
768 		return;
769 
770 	default:			/* Error */
771 		if (error != USB_ERR_CANCELLED) {
772 			/* try to clear stall first */
773 			usbd_xfer_set_stall(xfer);
774 			goto tr_setup;
775 		}
776 		return;
777 	}
778 }
779 
780 static void
781 ufoma_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
782 {
783 	struct ufoma_softc *sc = usbd_xfer_softc(xfer);
784 	struct usb_page_cache *pc;
785 	uint32_t actlen;
786 
787 	switch (USB_GET_STATE(xfer)) {
788 	case USB_ST_SETUP:
789 	case USB_ST_TRANSFERRED:
790 tr_setup:
791 		pc = usbd_xfer_get_frame(xfer, 0);
792 		if (ucom_get_data(&sc->sc_ucom, pc, 0,
793 		    UFOMA_BULK_BUF_SIZE, &actlen)) {
794 			usbd_xfer_set_frame_len(xfer, 0, actlen);
795 			usbd_transfer_submit(xfer);
796 		}
797 		return;
798 
799 	default:			/* Error */
800 		if (error != USB_ERR_CANCELLED) {
801 			/* try to clear stall first */
802 			usbd_xfer_set_stall(xfer);
803 			goto tr_setup;
804 		}
805 		return;
806 	}
807 }
808 
809 static void
810 ufoma_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
811 {
812 	struct ufoma_softc *sc = usbd_xfer_softc(xfer);
813 	struct usb_page_cache *pc;
814 	int actlen;
815 
816 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
817 
818 	switch (USB_GET_STATE(xfer)) {
819 	case USB_ST_TRANSFERRED:
820 		pc = usbd_xfer_get_frame(xfer, 0);
821 		ucom_put_data(&sc->sc_ucom, pc, 0, actlen);
822 
823 	case USB_ST_SETUP:
824 tr_setup:
825 		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
826 		usbd_transfer_submit(xfer);
827 		return;
828 
829 	default:			/* Error */
830 		if (error != USB_ERR_CANCELLED) {
831 			/* try to clear stall first */
832 			usbd_xfer_set_stall(xfer);
833 			goto tr_setup;
834 		}
835 		return;
836 	}
837 }
838 
839 static void
840 ufoma_cfg_open(struct ucom_softc *ucom)
841 {
842 	struct ufoma_softc *sc = ucom->sc_parent;
843 
844 	/* empty input queue */
845 
846 	if (sc->sc_num_msg != 0xFF) {
847 		sc->sc_num_msg++;
848 	}
849 	if (sc->sc_currentmode == UMCPC_ACM_MODE_UNLINKED) {
850 		ufoma_cfg_link_state(sc);
851 	}
852 	if (sc->sc_currentmode == UMCPC_ACM_MODE_DEACTIVATED) {
853 		ufoma_cfg_activate_state(sc, sc->sc_modetoactivate);
854 	}
855 }
856 
857 static void
858 ufoma_cfg_close(struct ucom_softc *ucom)
859 {
860 	struct ufoma_softc *sc = ucom->sc_parent;
861 
862 	ufoma_cfg_activate_state(sc, UMCPC_ACM_MODE_DEACTIVATED);
863 }
864 
865 static void
866 ufoma_cfg_set_break(struct ucom_softc *ucom, uint8_t onoff)
867 {
868 	struct ufoma_softc *sc = ucom->sc_parent;
869 	struct usb_device_request req;
870 	uint16_t wValue;
871 
872 	if (sc->sc_nobulk ||
873 	    (sc->sc_currentmode == UMCPC_ACM_MODE_OBEX)) {
874 		return;
875 	}
876 	if (!(sc->sc_acm_cap & USB_CDC_ACM_HAS_BREAK)) {
877 		return;
878 	}
879 	wValue = onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF;
880 
881 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
882 	req.bRequest = UCDC_SEND_BREAK;
883 	USETW(req.wValue, wValue);
884 	req.wIndex[0] = sc->sc_ctrl_iface_no;
885 	req.wIndex[1] = 0;
886 	USETW(req.wLength, 0);
887 
888 	ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
889 	    &req, NULL, 0, 1000);
890 }
891 
892 static void
893 ufoma_cfg_get_status(struct ucom_softc *ucom, uint8_t *lsr, uint8_t *msr)
894 {
895 	struct ufoma_softc *sc = ucom->sc_parent;
896 
897 	/* XXX Note: sc_lsr is always zero */
898 	*lsr = sc->sc_lsr;
899 	*msr = sc->sc_msr;
900 }
901 
902 static void
903 ufoma_cfg_set_line_state(struct ufoma_softc *sc)
904 {
905 	struct usb_device_request req;
906 
907 	/* Don't send line state emulation request for OBEX port */
908 	if (sc->sc_currentmode == UMCPC_ACM_MODE_OBEX) {
909 		return;
910 	}
911 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
912 	req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
913 	USETW(req.wValue, sc->sc_line);
914 	req.wIndex[0] = sc->sc_ctrl_iface_no;
915 	req.wIndex[1] = 0;
916 	USETW(req.wLength, 0);
917 
918 	ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
919 	    &req, NULL, 0, 1000);
920 }
921 
922 static void
923 ufoma_cfg_set_dtr(struct ucom_softc *ucom, uint8_t onoff)
924 {
925 	struct ufoma_softc *sc = ucom->sc_parent;
926 
927 	if (sc->sc_nobulk) {
928 		return;
929 	}
930 	if (onoff)
931 		sc->sc_line |= UCDC_LINE_DTR;
932 	else
933 		sc->sc_line &= ~UCDC_LINE_DTR;
934 
935 	ufoma_cfg_set_line_state(sc);
936 }
937 
938 static void
939 ufoma_cfg_set_rts(struct ucom_softc *ucom, uint8_t onoff)
940 {
941 	struct ufoma_softc *sc = ucom->sc_parent;
942 
943 	if (sc->sc_nobulk) {
944 		return;
945 	}
946 	if (onoff)
947 		sc->sc_line |= UCDC_LINE_RTS;
948 	else
949 		sc->sc_line &= ~UCDC_LINE_RTS;
950 
951 	ufoma_cfg_set_line_state(sc);
952 }
953 
954 static int
955 ufoma_pre_param(struct ucom_softc *ucom, struct termios *t)
956 {
957 	return (0);			/* we accept anything */
958 }
959 
960 static void
961 ufoma_cfg_param(struct ucom_softc *ucom, struct termios *t)
962 {
963 	struct ufoma_softc *sc = ucom->sc_parent;
964 	struct usb_device_request req;
965 	struct usb_cdc_line_state ls;
966 
967 	if (sc->sc_nobulk ||
968 	    (sc->sc_currentmode == UMCPC_ACM_MODE_OBEX)) {
969 		return;
970 	}
971 	DPRINTF("\n");
972 
973 	memset(&ls, 0, sizeof(ls));
974 
975 	USETDW(ls.dwDTERate, t->c_ospeed);
976 
977 	if (t->c_cflag & CSTOPB) {
978 		ls.bCharFormat = UCDC_STOP_BIT_2;
979 	} else {
980 		ls.bCharFormat = UCDC_STOP_BIT_1;
981 	}
982 
983 	if (t->c_cflag & PARENB) {
984 		if (t->c_cflag & PARODD) {
985 			ls.bParityType = UCDC_PARITY_ODD;
986 		} else {
987 			ls.bParityType = UCDC_PARITY_EVEN;
988 		}
989 	} else {
990 		ls.bParityType = UCDC_PARITY_NONE;
991 	}
992 
993 	switch (t->c_cflag & CSIZE) {
994 	case CS5:
995 		ls.bDataBits = 5;
996 		break;
997 	case CS6:
998 		ls.bDataBits = 6;
999 		break;
1000 	case CS7:
1001 		ls.bDataBits = 7;
1002 		break;
1003 	case CS8:
1004 		ls.bDataBits = 8;
1005 		break;
1006 	}
1007 
1008 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
1009 	req.bRequest = UCDC_SET_LINE_CODING;
1010 	USETW(req.wValue, 0);
1011 	req.wIndex[0] = sc->sc_ctrl_iface_no;
1012 	req.wIndex[1] = 0;
1013 	USETW(req.wLength, UCDC_LINE_STATE_LENGTH);
1014 
1015 	ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
1016 	    &req, &ls, 0, 1000);
1017 }
1018 
1019 static int
1020 ufoma_modem_setup(device_t dev, struct ufoma_softc *sc,
1021     struct usb_attach_arg *uaa)
1022 {
1023 	struct usb_config_descriptor *cd;
1024 	struct usb_cdc_acm_descriptor *acm;
1025 	struct usb_cdc_cm_descriptor *cmd;
1026 	struct usb_interface_descriptor *id;
1027 	struct usb_interface *iface;
1028 	uint8_t i;
1029 	int32_t error;
1030 
1031 	cd = usbd_get_config_descriptor(uaa->device);
1032 	id = usbd_get_interface_descriptor(uaa->iface);
1033 
1034 	cmd = ufoma_get_intconf(cd, id, UDESC_CS_INTERFACE, UDESCSUB_CDC_CM);
1035 
1036 	if ((cmd == NULL) ||
1037 	    (cmd->bLength < sizeof(*cmd))) {
1038 		return (EINVAL);
1039 	}
1040 	sc->sc_cm_cap = cmd->bmCapabilities;
1041 	sc->sc_data_iface_no = cmd->bDataInterface;
1042 
1043 	acm = ufoma_get_intconf(cd, id, UDESC_CS_INTERFACE, UDESCSUB_CDC_ACM);
1044 
1045 	if ((acm == NULL) ||
1046 	    (acm->bLength < sizeof(*acm))) {
1047 		return (EINVAL);
1048 	}
1049 	sc->sc_acm_cap = acm->bmCapabilities;
1050 
1051 	device_printf(dev, "data interface %d, has %sCM over data, "
1052 	    "has %sbreak\n",
1053 	    sc->sc_data_iface_no,
1054 	    sc->sc_cm_cap & USB_CDC_CM_OVER_DATA ? "" : "no ",
1055 	    sc->sc_acm_cap & USB_CDC_ACM_HAS_BREAK ? "" : "no ");
1056 
1057 	/* get the data interface too */
1058 
1059 	for (i = 0;; i++) {
1060 		iface = usbd_get_iface(uaa->device, i);
1061 
1062 		if (iface) {
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