xref: /freebsd/sys/dev/usb/serial/ufoma.c (revision 3f0164abf32b9b761e0a2cb4bdca3a8b84f156d4)
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 };
199 
200 /* prototypes */
201 
202 static device_probe_t ufoma_probe;
203 static device_attach_t ufoma_attach;
204 static device_detach_t ufoma_detach;
205 static void ufoma_free_softc(struct ufoma_softc *);
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_free(struct ucom_softc *);
218 static void	ufoma_cfg_open(struct ucom_softc *);
219 static void	ufoma_cfg_close(struct ucom_softc *);
220 static void	ufoma_cfg_set_break(struct ucom_softc *, uint8_t);
221 static void	ufoma_cfg_get_status(struct ucom_softc *, uint8_t *,
222 		    uint8_t *);
223 static void	ufoma_cfg_set_dtr(struct ucom_softc *, uint8_t);
224 static void	ufoma_cfg_set_rts(struct ucom_softc *, uint8_t);
225 static int	ufoma_pre_param(struct ucom_softc *, struct termios *);
226 static void	ufoma_cfg_param(struct ucom_softc *, struct termios *);
227 static int	ufoma_modem_setup(device_t, struct ufoma_softc *,
228 		    struct usb_attach_arg *);
229 static void	ufoma_start_read(struct ucom_softc *);
230 static void	ufoma_stop_read(struct ucom_softc *);
231 static void	ufoma_start_write(struct ucom_softc *);
232 static void	ufoma_stop_write(struct ucom_softc *);
233 static void	ufoma_poll(struct ucom_softc *ucom);
234 
235 /*sysctl stuff*/
236 static int ufoma_sysctl_support(SYSCTL_HANDLER_ARGS);
237 static int ufoma_sysctl_current(SYSCTL_HANDLER_ARGS);
238 static int ufoma_sysctl_open(SYSCTL_HANDLER_ARGS);
239 
240 static const struct usb_config
241 	ufoma_ctrl_config[UFOMA_CTRL_ENDPT_MAX] = {
242 
243 	[UFOMA_CTRL_ENDPT_INTR] = {
244 		.type = UE_INTERRUPT,
245 		.endpoint = UE_ADDR_ANY,
246 		.direction = UE_DIR_IN,
247 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
248 		.bufsize = sizeof(struct usb_cdc_notification),
249 		.callback = &ufoma_intr_callback,
250 	},
251 
252 	[UFOMA_CTRL_ENDPT_READ] = {
253 		.type = UE_CONTROL,
254 		.endpoint = 0x00,	/* Control pipe */
255 		.direction = UE_DIR_ANY,
256 		.bufsize = (sizeof(struct usb_device_request) + UFOMA_CMD_BUF_SIZE),
257 		.flags = {.short_xfer_ok = 1,},
258 		.callback = &ufoma_ctrl_read_callback,
259 		.timeout = 1000,	/* 1 second */
260 	},
261 
262 	[UFOMA_CTRL_ENDPT_WRITE] = {
263 		.type = UE_CONTROL,
264 		.endpoint = 0x00,	/* Control pipe */
265 		.direction = UE_DIR_ANY,
266 		.bufsize = (sizeof(struct usb_device_request) + 1),
267 		.callback = &ufoma_ctrl_write_callback,
268 		.timeout = 1000,	/* 1 second */
269 	},
270 };
271 
272 static const struct usb_config
273 	ufoma_bulk_config[UFOMA_BULK_ENDPT_MAX] = {
274 
275 	[UFOMA_BULK_ENDPT_WRITE] = {
276 		.type = UE_BULK,
277 		.endpoint = UE_ADDR_ANY,
278 		.direction = UE_DIR_OUT,
279 		.bufsize = UFOMA_BULK_BUF_SIZE,
280 		.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
281 		.callback = &ufoma_bulk_write_callback,
282 	},
283 
284 	[UFOMA_BULK_ENDPT_READ] = {
285 		.type = UE_BULK,
286 		.endpoint = UE_ADDR_ANY,
287 		.direction = UE_DIR_IN,
288 		.bufsize = UFOMA_BULK_BUF_SIZE,
289 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
290 		.callback = &ufoma_bulk_read_callback,
291 	},
292 };
293 
294 static const struct ucom_callback ufoma_callback = {
295 	.ucom_cfg_get_status = &ufoma_cfg_get_status,
296 	.ucom_cfg_set_dtr = &ufoma_cfg_set_dtr,
297 	.ucom_cfg_set_rts = &ufoma_cfg_set_rts,
298 	.ucom_cfg_set_break = &ufoma_cfg_set_break,
299 	.ucom_cfg_param = &ufoma_cfg_param,
300 	.ucom_cfg_open = &ufoma_cfg_open,
301 	.ucom_cfg_close = &ufoma_cfg_close,
302 	.ucom_pre_param = &ufoma_pre_param,
303 	.ucom_start_read = &ufoma_start_read,
304 	.ucom_stop_read = &ufoma_stop_read,
305 	.ucom_start_write = &ufoma_start_write,
306 	.ucom_stop_write = &ufoma_stop_write,
307 	.ucom_poll = &ufoma_poll,
308 	.ucom_free = &ufoma_free,
309 };
310 
311 static device_method_t ufoma_methods[] = {
312 	/* Device methods */
313 	DEVMETHOD(device_probe, ufoma_probe),
314 	DEVMETHOD(device_attach, ufoma_attach),
315 	DEVMETHOD(device_detach, ufoma_detach),
316 	DEVMETHOD_END
317 };
318 
319 static devclass_t ufoma_devclass;
320 
321 static driver_t ufoma_driver = {
322 	.name = "ufoma",
323 	.methods = ufoma_methods,
324 	.size = sizeof(struct ufoma_softc),
325 };
326 
327 DRIVER_MODULE(ufoma, uhub, ufoma_driver, ufoma_devclass, NULL, 0);
328 MODULE_DEPEND(ufoma, ucom, 1, 1, 1);
329 MODULE_DEPEND(ufoma, usb, 1, 1, 1);
330 MODULE_VERSION(ufoma, 1);
331 
332 static const STRUCT_USB_HOST_ID ufoma_devs[] = {
333 	{USB_IFACE_CLASS(UICLASS_CDC),
334 	 USB_IFACE_SUBCLASS(UISUBCLASS_MCPC),},
335 };
336 
337 static int
338 ufoma_probe(device_t dev)
339 {
340 	struct usb_attach_arg *uaa = device_get_ivars(dev);
341 	struct usb_interface_descriptor *id;
342 	struct usb_config_descriptor *cd;
343 	usb_mcpc_acm_descriptor *mad;
344 	int error;
345 
346 	if (uaa->usb_mode != USB_MODE_HOST)
347 		return (ENXIO);
348 
349 	error = usbd_lookup_id_by_uaa(ufoma_devs, sizeof(ufoma_devs), uaa);
350 	if (error)
351 		return (error);
352 
353 	id = usbd_get_interface_descriptor(uaa->iface);
354 	cd = usbd_get_config_descriptor(uaa->device);
355 
356 	if (id == NULL || cd == NULL)
357 		return (ENXIO);
358 
359 	mad = ufoma_get_intconf(cd, id, UDESC_VS_INTERFACE, UDESCSUB_MCPC_ACM);
360 	if (mad == NULL)
361 		return (ENXIO);
362 
363 #ifndef UFOMA_HANDSFREE
364 	if ((mad->bType == UMCPC_ACM_TYPE_AB5) ||
365 	    (mad->bType == UMCPC_ACM_TYPE_AB6))
366 		return (ENXIO);
367 #endif
368 	return (BUS_PROBE_GENERIC);
369 }
370 
371 static int
372 ufoma_attach(device_t dev)
373 {
374 	struct usb_attach_arg *uaa = device_get_ivars(dev);
375 	struct ufoma_softc *sc = device_get_softc(dev);
376 	struct usb_config_descriptor *cd;
377 	struct usb_interface_descriptor *id;
378 	struct sysctl_ctx_list *sctx;
379 	struct sysctl_oid *soid;
380 
381 	usb_mcpc_acm_descriptor *mad;
382 	uint8_t elements;
383 	int32_t error;
384 
385 	sc->sc_udev = uaa->device;
386 	sc->sc_dev = dev;
387 	sc->sc_unit = device_get_unit(dev);
388 
389 	mtx_init(&sc->sc_mtx, "ufoma", NULL, MTX_DEF);
390 	ucom_ref(&sc->sc_super_ucom);
391 	cv_init(&sc->sc_cv, "CWAIT");
392 
393 	device_set_usb_desc(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 	cv_destroy(&sc->sc_cv);
499 
500 	device_claim_softc(dev);
501 
502 	ufoma_free_softc(sc);
503 
504 	return (0);
505 }
506 
507 UCOM_UNLOAD_DRAIN(ufoma);
508 
509 static void
510 ufoma_free_softc(struct ufoma_softc *sc)
511 {
512 	if (ucom_unref(&sc->sc_super_ucom)) {
513 		mtx_destroy(&sc->sc_mtx);
514 		device_free_softc(sc);
515 	}
516 }
517 
518 static void
519 ufoma_free(struct ucom_softc *ucom)
520 {
521 	ufoma_free_softc(ucom->sc_parent);
522 }
523 
524 static void *
525 ufoma_get_intconf(struct usb_config_descriptor *cd, struct usb_interface_descriptor *id,
526     uint8_t type, uint8_t subtype)
527 {
528 	struct usb_descriptor *desc = (void *)id;
529 
530 	while ((desc = usb_desc_foreach(cd, desc))) {
531 
532 		if (desc->bDescriptorType == UDESC_INTERFACE) {
533 			return (NULL);
534 		}
535 		if ((desc->bDescriptorType == type) &&
536 		    (desc->bDescriptorSubtype == subtype)) {
537 			break;
538 		}
539 	}
540 	return (desc);
541 }
542 
543 static void
544 ufoma_cfg_link_state(struct ufoma_softc *sc)
545 {
546 	struct usb_device_request req;
547 	int32_t error;
548 
549 	req.bmRequestType = UT_WRITE_VENDOR_INTERFACE;
550 	req.bRequest = UMCPC_SET_LINK;
551 	USETW(req.wValue, UMCPC_CM_MOBILE_ACM);
552 	USETW(req.wIndex, sc->sc_ctrl_iface_no);
553 	USETW(req.wLength, sc->sc_modetable[0]);
554 
555 	ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
556 	    &req, sc->sc_modetable, 0, 1000);
557 
558 	error = cv_timedwait(&sc->sc_cv, &sc->sc_mtx, hz);
559 
560 	if (error) {
561 		DPRINTF("NO response\n");
562 	}
563 }
564 
565 static void
566 ufoma_cfg_activate_state(struct ufoma_softc *sc, uint16_t state)
567 {
568 	struct usb_device_request req;
569 	int32_t error;
570 
571 	req.bmRequestType = UT_WRITE_VENDOR_INTERFACE;
572 	req.bRequest = UMCPC_ACTIVATE_MODE;
573 	USETW(req.wValue, state);
574 	USETW(req.wIndex, sc->sc_ctrl_iface_no);
575 	USETW(req.wLength, 0);
576 
577 	ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
578 	    &req, NULL, 0, 1000);
579 
580 	error = cv_timedwait(&sc->sc_cv, &sc->sc_mtx,
581 	    (UFOMA_MAX_TIMEOUT * hz));
582 	if (error) {
583 		DPRINTF("No response\n");
584 	}
585 }
586 
587 static void
588 ufoma_ctrl_read_callback(struct usb_xfer *xfer, usb_error_t error)
589 {
590 	struct ufoma_softc *sc = usbd_xfer_softc(xfer);
591 	struct usb_device_request req;
592 	struct usb_page_cache *pc0, *pc1;
593 	int len, aframes, nframes;
594 
595 	usbd_xfer_status(xfer, NULL, NULL, &aframes, &nframes);
596 
597 	switch (USB_GET_STATE(xfer)) {
598 	case USB_ST_TRANSFERRED:
599 tr_transferred:
600 		if (aframes != nframes)
601 			goto tr_setup;
602 		pc1 = usbd_xfer_get_frame(xfer, 1);
603 		len = usbd_xfer_frame_len(xfer, 1);
604 		if (len > 0)
605 			ucom_put_data(&sc->sc_ucom, pc1, 0, len);
606 		/* FALLTHROUGH */
607 	case USB_ST_SETUP:
608 tr_setup:
609 		if (sc->sc_num_msg) {
610 			sc->sc_num_msg--;
611 
612 			req.bmRequestType = UT_READ_CLASS_INTERFACE;
613 			req.bRequest = UCDC_GET_ENCAPSULATED_RESPONSE;
614 			USETW(req.wIndex, sc->sc_ctrl_iface_no);
615 			USETW(req.wValue, 0);
616 			USETW(req.wLength, UFOMA_CMD_BUF_SIZE);
617 
618 			pc0 = usbd_xfer_get_frame(xfer, 0);
619 			usbd_copy_in(pc0, 0, &req, sizeof(req));
620 
621 			usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
622 			usbd_xfer_set_frame_len(xfer, 1, UFOMA_CMD_BUF_SIZE);
623 			usbd_xfer_set_frames(xfer, 2);
624 			usbd_transfer_submit(xfer);
625 		}
626 		return;
627 
628 	default:			/* Error */
629 		DPRINTF("error = %s\n",
630 		    usbd_errstr(error));
631 
632 		if (error == USB_ERR_CANCELLED) {
633 			return;
634 		}
635 		goto tr_transferred;
636 	}
637 }
638 
639 static void
640 ufoma_ctrl_write_callback(struct usb_xfer *xfer, usb_error_t error)
641 {
642 	struct ufoma_softc *sc = usbd_xfer_softc(xfer);
643 	struct usb_device_request req;
644 	struct usb_page_cache *pc;
645 	uint32_t actlen;
646 
647 	switch (USB_GET_STATE(xfer)) {
648 	case USB_ST_TRANSFERRED:
649 tr_transferred:
650 	case USB_ST_SETUP:
651 		pc = usbd_xfer_get_frame(xfer, 1);
652 		if (ucom_get_data(&sc->sc_ucom, pc, 0, 1, &actlen)) {
653 
654 			req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
655 			req.bRequest = UCDC_SEND_ENCAPSULATED_COMMAND;
656 			USETW(req.wIndex, sc->sc_ctrl_iface_no);
657 			USETW(req.wValue, 0);
658 			USETW(req.wLength, 1);
659 
660 			pc = usbd_xfer_get_frame(xfer, 0);
661 			usbd_copy_in(pc, 0, &req, sizeof(req));
662 
663 			usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
664 			usbd_xfer_set_frame_len(xfer, 1, 1);
665 			usbd_xfer_set_frames(xfer, 2);
666 
667 			usbd_transfer_submit(xfer);
668 		}
669 		return;
670 
671 	default:			/* Error */
672 		DPRINTF("error = %s\n", usbd_errstr(error));
673 
674 		if (error == USB_ERR_CANCELLED) {
675 			return;
676 		}
677 		goto tr_transferred;
678 	}
679 }
680 
681 static void
682 ufoma_intr_callback(struct usb_xfer *xfer, usb_error_t error)
683 {
684 	struct ufoma_softc *sc = usbd_xfer_softc(xfer);
685 	struct usb_cdc_notification pkt;
686 	struct usb_page_cache *pc;
687 	uint16_t wLen;
688 	uint16_t temp;
689 	uint8_t mstatus;
690 	int actlen;
691 
692 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
693 
694 	switch (USB_GET_STATE(xfer)) {
695 	case USB_ST_TRANSFERRED:
696 		if (actlen < 8) {
697 			DPRINTF("too short message\n");
698 			goto tr_setup;
699 		}
700 		if (actlen > (int)sizeof(pkt)) {
701 			DPRINTF("truncating message\n");
702 			actlen = sizeof(pkt);
703 		}
704 		pc = usbd_xfer_get_frame(xfer, 0);
705 		usbd_copy_out(pc, 0, &pkt, actlen);
706 
707 		actlen -= 8;
708 
709 		wLen = UGETW(pkt.wLength);
710 		if (actlen > wLen) {
711 			actlen = wLen;
712 		}
713 		if ((pkt.bmRequestType == UT_READ_VENDOR_INTERFACE) &&
714 		    (pkt.bNotification == UMCPC_REQUEST_ACKNOWLEDGE)) {
715 			temp = UGETW(pkt.wValue);
716 			sc->sc_currentmode = (temp >> 8);
717 			if (!(temp & 0xff)) {
718 				DPRINTF("Mode change failed!\n");
719 			}
720 			cv_signal(&sc->sc_cv);
721 		}
722 		if (pkt.bmRequestType != UCDC_NOTIFICATION) {
723 			goto tr_setup;
724 		}
725 		switch (pkt.bNotification) {
726 		case UCDC_N_RESPONSE_AVAILABLE:
727 			if (!(sc->sc_nobulk)) {
728 				DPRINTF("Wrong serial state!\n");
729 				break;
730 			}
731 			if (sc->sc_num_msg != 0xFF) {
732 				sc->sc_num_msg++;
733 			}
734 			usbd_transfer_start(sc->sc_ctrl_xfer[UFOMA_CTRL_ENDPT_READ]);
735 			break;
736 
737 		case UCDC_N_SERIAL_STATE:
738 			if (sc->sc_nobulk) {
739 				DPRINTF("Wrong serial state!\n");
740 				break;
741 			}
742 			/*
743 		         * Set the serial state in ucom driver based on
744 		         * the bits from the notify message
745 		         */
746 			if (actlen < 2) {
747 				DPRINTF("invalid notification "
748 				    "length, %d bytes!\n", actlen);
749 				break;
750 			}
751 			DPRINTF("notify bytes = 0x%02x, 0x%02x\n",
752 			    pkt.data[0], pkt.data[1]);
753 
754 			/* currently, lsr is always zero. */
755 			sc->sc_lsr = 0;
756 			sc->sc_msr = 0;
757 
758 			mstatus = pkt.data[0];
759 
760 			if (mstatus & UCDC_N_SERIAL_RI) {
761 				sc->sc_msr |= SER_RI;
762 			}
763 			if (mstatus & UCDC_N_SERIAL_DSR) {
764 				sc->sc_msr |= SER_DSR;
765 			}
766 			if (mstatus & UCDC_N_SERIAL_DCD) {
767 				sc->sc_msr |= SER_DCD;
768 			}
769 			ucom_status_change(&sc->sc_ucom);
770 			break;
771 
772 		default:
773 			break;
774 		}
775 
776 	case USB_ST_SETUP:
777 tr_setup:
778 		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
779 		usbd_transfer_submit(xfer);
780 		return;
781 
782 	default:			/* Error */
783 		if (error != USB_ERR_CANCELLED) {
784 			/* try to clear stall first */
785 			usbd_xfer_set_stall(xfer);
786 			goto tr_setup;
787 		}
788 		return;
789 	}
790 }
791 
792 static void
793 ufoma_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
794 {
795 	struct ufoma_softc *sc = usbd_xfer_softc(xfer);
796 	struct usb_page_cache *pc;
797 	uint32_t actlen;
798 
799 	switch (USB_GET_STATE(xfer)) {
800 	case USB_ST_SETUP:
801 	case USB_ST_TRANSFERRED:
802 tr_setup:
803 		pc = usbd_xfer_get_frame(xfer, 0);
804 		if (ucom_get_data(&sc->sc_ucom, pc, 0,
805 		    UFOMA_BULK_BUF_SIZE, &actlen)) {
806 			usbd_xfer_set_frame_len(xfer, 0, actlen);
807 			usbd_transfer_submit(xfer);
808 		}
809 		return;
810 
811 	default:			/* Error */
812 		if (error != USB_ERR_CANCELLED) {
813 			/* try to clear stall first */
814 			usbd_xfer_set_stall(xfer);
815 			goto tr_setup;
816 		}
817 		return;
818 	}
819 }
820 
821 static void
822 ufoma_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
823 {
824 	struct ufoma_softc *sc = usbd_xfer_softc(xfer);
825 	struct usb_page_cache *pc;
826 	int actlen;
827 
828 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
829 
830 	switch (USB_GET_STATE(xfer)) {
831 	case USB_ST_TRANSFERRED:
832 		pc = usbd_xfer_get_frame(xfer, 0);
833 		ucom_put_data(&sc->sc_ucom, pc, 0, actlen);
834 
835 	case USB_ST_SETUP:
836 tr_setup:
837 		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
838 		usbd_transfer_submit(xfer);
839 		return;
840 
841 	default:			/* Error */
842 		if (error != USB_ERR_CANCELLED) {
843 			/* try to clear stall first */
844 			usbd_xfer_set_stall(xfer);
845 			goto tr_setup;
846 		}
847 		return;
848 	}
849 }
850 
851 static void
852 ufoma_cfg_open(struct ucom_softc *ucom)
853 {
854 	struct ufoma_softc *sc = ucom->sc_parent;
855 
856 	/* empty input queue */
857 
858 	if (sc->sc_num_msg != 0xFF) {
859 		sc->sc_num_msg++;
860 	}
861 	if (sc->sc_currentmode == UMCPC_ACM_MODE_UNLINKED) {
862 		ufoma_cfg_link_state(sc);
863 	}
864 	if (sc->sc_currentmode == UMCPC_ACM_MODE_DEACTIVATED) {
865 		ufoma_cfg_activate_state(sc, sc->sc_modetoactivate);
866 	}
867 }
868 
869 static void
870 ufoma_cfg_close(struct ucom_softc *ucom)
871 {
872 	struct ufoma_softc *sc = ucom->sc_parent;
873 
874 	ufoma_cfg_activate_state(sc, UMCPC_ACM_MODE_DEACTIVATED);
875 }
876 
877 static void
878 ufoma_cfg_set_break(struct ucom_softc *ucom, uint8_t onoff)
879 {
880 	struct ufoma_softc *sc = ucom->sc_parent;
881 	struct usb_device_request req;
882 	uint16_t wValue;
883 
884 	if (sc->sc_nobulk ||
885 	    (sc->sc_currentmode == UMCPC_ACM_MODE_OBEX)) {
886 		return;
887 	}
888 	if (!(sc->sc_acm_cap & USB_CDC_ACM_HAS_BREAK)) {
889 		return;
890 	}
891 	wValue = onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF;
892 
893 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
894 	req.bRequest = UCDC_SEND_BREAK;
895 	USETW(req.wValue, wValue);
896 	req.wIndex[0] = sc->sc_ctrl_iface_no;
897 	req.wIndex[1] = 0;
898 	USETW(req.wLength, 0);
899 
900 	ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
901 	    &req, NULL, 0, 1000);
902 }
903 
904 static void
905 ufoma_cfg_get_status(struct ucom_softc *ucom, uint8_t *lsr, uint8_t *msr)
906 {
907 	struct ufoma_softc *sc = ucom->sc_parent;
908 
909 	*lsr = sc->sc_lsr;
910 	*msr = sc->sc_msr;
911 }
912 
913 static void
914 ufoma_cfg_set_line_state(struct ufoma_softc *sc)
915 {
916 	struct usb_device_request req;
917 
918 	/* Don't send line state emulation request for OBEX port */
919 	if (sc->sc_currentmode == UMCPC_ACM_MODE_OBEX) {
920 		return;
921 	}
922 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
923 	req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
924 	USETW(req.wValue, sc->sc_line);
925 	req.wIndex[0] = sc->sc_ctrl_iface_no;
926 	req.wIndex[1] = 0;
927 	USETW(req.wLength, 0);
928 
929 	ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
930 	    &req, NULL, 0, 1000);
931 }
932 
933 static void
934 ufoma_cfg_set_dtr(struct ucom_softc *ucom, uint8_t onoff)
935 {
936 	struct ufoma_softc *sc = ucom->sc_parent;
937 
938 	if (sc->sc_nobulk) {
939 		return;
940 	}
941 	if (onoff)
942 		sc->sc_line |= UCDC_LINE_DTR;
943 	else
944 		sc->sc_line &= ~UCDC_LINE_DTR;
945 
946 	ufoma_cfg_set_line_state(sc);
947 }
948 
949 static void
950 ufoma_cfg_set_rts(struct ucom_softc *ucom, uint8_t onoff)
951 {
952 	struct ufoma_softc *sc = ucom->sc_parent;
953 
954 	if (sc->sc_nobulk) {
955 		return;
956 	}
957 	if (onoff)
958 		sc->sc_line |= UCDC_LINE_RTS;
959 	else
960 		sc->sc_line &= ~UCDC_LINE_RTS;
961 
962 	ufoma_cfg_set_line_state(sc);
963 }
964 
965 static int
966 ufoma_pre_param(struct ucom_softc *ucom, struct termios *t)
967 {
968 	return (0);			/* we accept anything */
969 }
970 
971 static void
972 ufoma_cfg_param(struct ucom_softc *ucom, struct termios *t)
973 {
974 	struct ufoma_softc *sc = ucom->sc_parent;
975 	struct usb_device_request req;
976 	struct usb_cdc_line_state ls;
977 
978 	if (sc->sc_nobulk ||
979 	    (sc->sc_currentmode == UMCPC_ACM_MODE_OBEX)) {
980 		return;
981 	}
982 	DPRINTF("\n");
983 
984 	memset(&ls, 0, sizeof(ls));
985 
986 	USETDW(ls.dwDTERate, t->c_ospeed);
987 
988 	if (t->c_cflag & CSTOPB) {
989 		ls.bCharFormat = UCDC_STOP_BIT_2;
990 	} else {
991 		ls.bCharFormat = UCDC_STOP_BIT_1;
992 	}
993 
994 	if (t->c_cflag & PARENB) {
995 		if (t->c_cflag & PARODD) {
996 			ls.bParityType = UCDC_PARITY_ODD;
997 		} else {
998 			ls.bParityType = UCDC_PARITY_EVEN;
999 		}
1000 	} else {
1001 		ls.bParityType = UCDC_PARITY_NONE;
1002 	}
1003 
1004 	switch (t->c_cflag & CSIZE) {
1005 	case CS5:
1006 		ls.bDataBits = 5;
1007 		break;
1008 	case CS6:
1009 		ls.bDataBits = 6;
1010 		break;
1011 	case CS7:
1012 		ls.bDataBits = 7;
1013 		break;
1014 	case CS8:
1015 		ls.bDataBits = 8;
1016 		break;
1017 	}
1018 
1019 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
1020 	req.bRequest = UCDC_SET_LINE_CODING;
1021 	USETW(req.wValue, 0);
1022 	req.wIndex[0] = sc->sc_ctrl_iface_no;
1023 	req.wIndex[1] = 0;
1024 	USETW(req.wLength, UCDC_LINE_STATE_LENGTH);
1025 
1026 	ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
1027 	    &req, &ls, 0, 1000);
1028 }
1029 
1030 static int
1031 ufoma_modem_setup(device_t dev, struct ufoma_softc *sc,
1032     struct usb_attach_arg *uaa)
1033 {
1034 	struct usb_config_descriptor *cd;
1035 	struct usb_cdc_acm_descriptor *acm;
1036 	struct usb_cdc_cm_descriptor *cmd;
1037 	struct usb_interface_descriptor *id;
1038 	struct usb_interface *iface;
1039 	uint8_t i;
1040 	int32_t error;
1041 
1042 	cd = usbd_get_config_descriptor(uaa->device);
1043 	id = usbd_get_interface_descriptor(uaa->iface);
1044 
1045 	cmd = ufoma_get_intconf(cd, id, UDESC_CS_INTERFACE, UDESCSUB_CDC_CM);
1046 
1047 	if ((cmd == NULL) ||
1048 	    (cmd->bLength < sizeof(*cmd))) {
1049 		return (EINVAL);
1050 	}
1051 	sc->sc_cm_cap = cmd->bmCapabilities;
1052 	sc->sc_data_iface_no = cmd->bDataInterface;
1053 
1054 	acm = ufoma_get_intconf(cd, id, UDESC_CS_INTERFACE, UDESCSUB_CDC_ACM);
1055 
1056 	if ((acm == NULL) ||
1057 	    (acm->bLength < sizeof(*acm))) {
1058 		return (EINVAL);
1059 	}
1060 	sc->sc_acm_cap = acm->bmCapabilities;
1061 
1062 	device_printf(dev, "data interface %d, has %sCM over data, "
1063 	    "has %sbreak\n",
1064 	    sc->sc_data_iface_no,
1065 	    sc->sc_cm_cap & USB_CDC_CM_OVER_DATA ? "" : "no ",
1066 	    sc->sc_acm_cap & USB_CDC_ACM_HAS_BREAK ? "" : "no ");
1067 
1068 	/* get the data interface too */
1069 
1070 	for (i = 0;; i++) {
1071 
1072 		iface = usbd_get_iface(uaa->device, i);
1073 
1074 		if (iface) {
1075 
1076 			id = usbd_get_interface_descriptor(iface);
1077 
1078 			if (id && (id->bInterfaceNumber == sc->sc_data_iface_no)) {
1079 				sc->sc_data_iface_index = i;
1080 				usbd_set_parent_iface(uaa->device, i, uaa->info.bIfaceIndex);
1081 				break;
1082 			}
1083 		} else {
1084 			device_printf(dev, "no data interface\n");
1085 			return (EINVAL);
1086 		}
1087 	}
1088 
1089 	error = usbd_transfer_setup(uaa->device,
1090 	    &sc->sc_data_iface_index, sc->sc_bulk_xfer,
1091 	    ufoma_bulk_config, UFOMA_BULK_ENDPT_MAX, sc, &sc->sc_mtx);
1092 
1093 	if (error) {
1094 		device_printf(dev, "allocating BULK USB "
1095 		    "transfers failed\n");
1096 		return (EINVAL);
1097 	}
1098 	return (0);
1099 }
1100 
1101 static void
1102 ufoma_start_read(struct ucom_softc *ucom)
1103 {
1104 	struct ufoma_softc *sc = ucom->sc_parent;
1105 
1106 	/* start interrupt transfer */
1107 	usbd_transfer_start(sc->sc_ctrl_xfer[UFOMA_CTRL_ENDPT_INTR]);
1108 
1109 	/* start data transfer */
1110 	if (sc->sc_nobulk) {
1111 		usbd_transfer_start(sc->sc_ctrl_xfer[UFOMA_CTRL_ENDPT_READ]);
1112 	} else {
1113 		usbd_transfer_start(sc->sc_bulk_xfer[UFOMA_BULK_ENDPT_READ]);
1114 	}
1115 }
1116 
1117 static void
1118 ufoma_stop_read(struct ucom_softc *ucom)
1119 {
1120 	struct ufoma_softc *sc = ucom->sc_parent;
1121 
1122 	/* stop interrupt transfer */
1123 	usbd_transfer_stop(sc->sc_ctrl_xfer[UFOMA_CTRL_ENDPT_INTR]);
1124 
1125 	/* stop data transfer */
1126 	if (sc->sc_nobulk) {
1127 		usbd_transfer_stop(sc->sc_ctrl_xfer[UFOMA_CTRL_ENDPT_READ]);
1128 	} else {
1129 		usbd_transfer_stop(sc->sc_bulk_xfer[UFOMA_BULK_ENDPT_READ]);
1130 	}
1131 }
1132 
1133 static void
1134 ufoma_start_write(struct ucom_softc *ucom)
1135 {
1136 	struct ufoma_softc *sc = ucom->sc_parent;
1137 
1138 	if (sc->sc_nobulk) {
1139 		usbd_transfer_start(sc->sc_ctrl_xfer[UFOMA_CTRL_ENDPT_WRITE]);
1140 	} else {
1141 		usbd_transfer_start(sc->sc_bulk_xfer[UFOMA_BULK_ENDPT_WRITE]);
1142 	}
1143 }
1144 
1145 static void
1146 ufoma_stop_write(struct ucom_softc *ucom)
1147 {
1148 	struct ufoma_softc *sc = ucom->sc_parent;
1149 
1150 	if (sc->sc_nobulk) {
1151 		usbd_transfer_stop(sc->sc_ctrl_xfer[UFOMA_CTRL_ENDPT_WRITE]);
1152 	} else {
1153 		usbd_transfer_stop(sc->sc_bulk_xfer[UFOMA_BULK_ENDPT_WRITE]);
1154 	}
1155 }
1156 
1157 static struct umcpc_modetostr_tab{
1158 	int mode;
1159 	char *str;
1160 }umcpc_modetostr_tab[]={
1161 	{UMCPC_ACM_MODE_DEACTIVATED, "deactivated"},
1162 	{UMCPC_ACM_MODE_MODEM, "modem"},
1163 	{UMCPC_ACM_MODE_ATCOMMAND, "handsfree"},
1164 	{UMCPC_ACM_MODE_OBEX, "obex"},
1165 	{UMCPC_ACM_MODE_VENDOR1, "vendor1"},
1166 	{UMCPC_ACM_MODE_VENDOR2, "vendor2"},
1167 	{UMCPC_ACM_MODE_UNLINKED, "unlinked"},
1168 	{0, NULL}
1169 };
1170 
1171 static char *ufoma_mode_to_str(int mode)
1172 {
1173 	int i;
1174 	for(i = 0 ;umcpc_modetostr_tab[i].str != NULL; i++){
1175 		if(umcpc_modetostr_tab[i].mode == mode){
1176 			return umcpc_modetostr_tab[i].str;
1177 		}
1178 	}
1179 	return NULL;
1180 }
1181 
1182 static int ufoma_str_to_mode(char *str)
1183 {
1184 	int i;
1185 	for(i = 0 ;umcpc_modetostr_tab[i].str != NULL; i++){
1186 		if(strcmp(str, umcpc_modetostr_tab[i].str)==0){
1187 			return umcpc_modetostr_tab[i].mode;
1188 		}
1189 	}
1190 	return -1;
1191 }
1192 
1193 static int ufoma_sysctl_support(SYSCTL_HANDLER_ARGS)
1194 {
1195 	struct ufoma_softc *sc = (struct ufoma_softc *)oidp->oid_arg1;
1196 	struct sbuf sb;
1197 	int i;
1198 	char *mode;
1199 
1200 	sbuf_new(&sb, NULL, 1, SBUF_AUTOEXTEND);
1201 	for(i = 1; i < sc->sc_modetable[0]; i++){
1202 		mode = ufoma_mode_to_str(sc->sc_modetable[i]);
1203 		if(mode !=NULL){
1204 			sbuf_cat(&sb, mode);
1205 		}else{
1206 			sbuf_printf(&sb, "(%02x)", sc->sc_modetable[i]);
1207 		}
1208 		if(i < (sc->sc_modetable[0]-1))
1209 			sbuf_cat(&sb, ",");
1210 	}
1211 	sbuf_trim(&sb);
1212 	sbuf_finish(&sb);
1213 	sysctl_handle_string(oidp, sbuf_data(&sb), sbuf_len(&sb), req);
1214 	sbuf_delete(&sb);
1215 
1216 	return 0;
1217 }
1218 static int ufoma_sysctl_current(SYSCTL_HANDLER_ARGS)
1219 {
1220 	struct ufoma_softc *sc = (struct ufoma_softc *)oidp->oid_arg1;
1221 	char *mode;
1222 	char subbuf[]="(XXX)";
1223 	mode = ufoma_mode_to_str(sc->sc_currentmode);
1224 	if(!mode){
1225 		mode = subbuf;
1226 		snprintf(subbuf, sizeof(subbuf), "(%02x)", sc->sc_currentmode);
1227 	}
1228 	sysctl_handle_string(oidp, mode, strlen(mode), req);
1229 
1230 	return 0;
1231 
1232 }
1233 static int ufoma_sysctl_open(SYSCTL_HANDLER_ARGS)
1234 {
1235 	struct ufoma_softc *sc = (struct ufoma_softc *)oidp->oid_arg1;
1236 	char *mode;
1237 	char subbuf[40];
1238 	int newmode;
1239 	int error;
1240 	int i;
1241 
1242 	mode = ufoma_mode_to_str(sc->sc_modetoactivate);
1243 	if(mode){
1244 		strncpy(subbuf, mode, sizeof(subbuf));
1245 	}else{
1246 		snprintf(subbuf, sizeof(subbuf), "(%02x)", sc->sc_modetoactivate);
1247 	}
1248 	error = sysctl_handle_string(oidp, subbuf, sizeof(subbuf), req);
1249 	if(error != 0 || req->newptr == NULL){
1250 		return error;
1251 	}
1252 
1253 	if((newmode = ufoma_str_to_mode(subbuf)) == -1){
1254 		return EINVAL;
1255 	}
1256 
1257 	for(i = 1 ; i < sc->sc_modetable[0] ; i++){
1258 		if(sc->sc_modetable[i] == newmode){
1259 			sc->sc_modetoactivate = newmode;
1260 			return 0;
1261 		}
1262 	}
1263 
1264 	return EINVAL;
1265 }
1266 
1267 static void
1268 ufoma_poll(struct ucom_softc *ucom)
1269 {
1270 	struct ufoma_softc *sc = ucom->sc_parent;
1271 	usbd_transfer_poll(sc->sc_ctrl_xfer, UFOMA_CTRL_ENDPT_MAX);
1272 	usbd_transfer_poll(sc->sc_bulk_xfer, UFOMA_BULK_ENDPT_MAX);
1273 }
1274