ufoma.c revision 239299
1/*	$NetBSD: umodem.c,v 1.45 2002/09/23 05:51:23 simonb Exp $	*/
2
3#include <sys/cdefs.h>
4__FBSDID("$FreeBSD: head/sys/dev/usb/serial/ufoma.c 239299 2012-08-15 15:42:57Z hselasky $");
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
117typedef 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
157enum {
158	UFOMA_CTRL_ENDPT_INTR,
159	UFOMA_CTRL_ENDPT_READ,
160	UFOMA_CTRL_ENDPT_WRITE,
161	UFOMA_CTRL_ENDPT_MAX,
162};
163
164enum {
165	UFOMA_BULK_ENDPT_WRITE,
166	UFOMA_BULK_ENDPT_READ,
167	UFOMA_BULK_ENDPT_MAX,
168};
169
170struct 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
202static device_probe_t ufoma_probe;
203static device_attach_t ufoma_attach;
204static device_detach_t ufoma_detach;
205static void ufoma_free_softc(struct ufoma_softc *);
206
207static usb_callback_t ufoma_ctrl_read_callback;
208static usb_callback_t ufoma_ctrl_write_callback;
209static usb_callback_t ufoma_intr_callback;
210static usb_callback_t ufoma_bulk_write_callback;
211static usb_callback_t ufoma_bulk_read_callback;
212
213static void	*ufoma_get_intconf(struct usb_config_descriptor *,
214		    struct usb_interface_descriptor *, uint8_t, uint8_t);
215static void	ufoma_cfg_link_state(struct ufoma_softc *);
216static void	ufoma_cfg_activate_state(struct ufoma_softc *, uint16_t);
217static void	ufoma_free(struct ucom_softc *);
218static void	ufoma_cfg_open(struct ucom_softc *);
219static void	ufoma_cfg_close(struct ucom_softc *);
220static void	ufoma_cfg_set_break(struct ucom_softc *, uint8_t);
221static void	ufoma_cfg_get_status(struct ucom_softc *, uint8_t *,
222		    uint8_t *);
223static void	ufoma_cfg_set_dtr(struct ucom_softc *, uint8_t);
224static void	ufoma_cfg_set_rts(struct ucom_softc *, uint8_t);
225static int	ufoma_pre_param(struct ucom_softc *, struct termios *);
226static void	ufoma_cfg_param(struct ucom_softc *, struct termios *);
227static int	ufoma_modem_setup(device_t, struct ufoma_softc *,
228		    struct usb_attach_arg *);
229static void	ufoma_start_read(struct ucom_softc *);
230static void	ufoma_stop_read(struct ucom_softc *);
231static void	ufoma_start_write(struct ucom_softc *);
232static void	ufoma_stop_write(struct ucom_softc *);
233static void	ufoma_poll(struct ucom_softc *ucom);
234
235/*sysctl stuff*/
236static int ufoma_sysctl_support(SYSCTL_HANDLER_ARGS);
237static int ufoma_sysctl_current(SYSCTL_HANDLER_ARGS);
238static int ufoma_sysctl_open(SYSCTL_HANDLER_ARGS);
239
240static 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
272static 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
294static 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
311static 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
319static devclass_t ufoma_devclass;
320
321static driver_t ufoma_driver = {
322	.name = "ufoma",
323	.methods = ufoma_methods,
324	.size = sizeof(struct ufoma_softc),
325};
326
327DRIVER_MODULE(ufoma, uhub, ufoma_driver, ufoma_devclass, NULL, 0);
328MODULE_DEPEND(ufoma, ucom, 1, 1, 1);
329MODULE_DEPEND(ufoma, usb, 1, 1, 1);
330MODULE_VERSION(ufoma, 1);
331
332static const STRUCT_USB_HOST_ID ufoma_devs[] = {
333	{USB_IFACE_CLASS(UICLASS_CDC),
334	 USB_IFACE_SUBCLASS(UISUBCLASS_MCPC),},
335};
336
337static int
338ufoma_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
371static int
372ufoma_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
481detach:
482	ufoma_detach(dev);
483	return (ENXIO);			/* failure */
484}
485
486static int
487ufoma_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
507UCOM_UNLOAD_DRAIN(ufoma);
508
509static void
510ufoma_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
518static void
519ufoma_free(struct ucom_softc *ucom)
520{
521	ufoma_free_softc(ucom->sc_parent);
522}
523
524static void *
525ufoma_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
543static void
544ufoma_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
565static void
566ufoma_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
587static void
588ufoma_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:
599tr_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:
608tr_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		} else {
635			goto tr_setup;
636		}
637
638		goto tr_transferred;
639	}
640}
641
642static void
643ufoma_ctrl_write_callback(struct usb_xfer *xfer, usb_error_t error)
644{
645	struct ufoma_softc *sc = usbd_xfer_softc(xfer);
646	struct usb_device_request req;
647	struct usb_page_cache *pc;
648	uint32_t actlen;
649
650	switch (USB_GET_STATE(xfer)) {
651	case USB_ST_TRANSFERRED:
652tr_transferred:
653	case USB_ST_SETUP:
654tr_setup:
655		pc = usbd_xfer_get_frame(xfer, 1);
656		if (ucom_get_data(&sc->sc_ucom, pc, 0, 1, &actlen)) {
657
658			req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
659			req.bRequest = UCDC_SEND_ENCAPSULATED_COMMAND;
660			USETW(req.wIndex, sc->sc_ctrl_iface_no);
661			USETW(req.wValue, 0);
662			USETW(req.wLength, 1);
663
664			pc = usbd_xfer_get_frame(xfer, 0);
665			usbd_copy_in(pc, 0, &req, sizeof(req));
666
667			usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
668			usbd_xfer_set_frame_len(xfer, 1, 1);
669			usbd_xfer_set_frames(xfer, 2);
670
671			usbd_transfer_submit(xfer);
672		}
673		return;
674
675	default:			/* Error */
676		DPRINTF("error = %s\n", usbd_errstr(error));
677
678		if (error == USB_ERR_CANCELLED) {
679			return;
680		} else {
681			goto tr_setup;
682		}
683
684		goto tr_transferred;
685	}
686}
687
688static void
689ufoma_intr_callback(struct usb_xfer *xfer, usb_error_t error)
690{
691	struct ufoma_softc *sc = usbd_xfer_softc(xfer);
692	struct usb_cdc_notification pkt;
693	struct usb_page_cache *pc;
694	uint16_t wLen;
695	uint16_t temp;
696	uint8_t mstatus;
697	int actlen;
698
699	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
700
701	switch (USB_GET_STATE(xfer)) {
702	case USB_ST_TRANSFERRED:
703		if (actlen < 8) {
704			DPRINTF("too short message\n");
705			goto tr_setup;
706		}
707		if (actlen > (int)sizeof(pkt)) {
708			DPRINTF("truncating message\n");
709			actlen = sizeof(pkt);
710		}
711		pc = usbd_xfer_get_frame(xfer, 0);
712		usbd_copy_out(pc, 0, &pkt, actlen);
713
714		actlen -= 8;
715
716		wLen = UGETW(pkt.wLength);
717		if (actlen > wLen) {
718			actlen = wLen;
719		}
720		if ((pkt.bmRequestType == UT_READ_VENDOR_INTERFACE) &&
721		    (pkt.bNotification == UMCPC_REQUEST_ACKNOWLEDGE)) {
722			temp = UGETW(pkt.wValue);
723			sc->sc_currentmode = (temp >> 8);
724			if (!(temp & 0xff)) {
725				DPRINTF("Mode change failed!\n");
726			}
727			cv_signal(&sc->sc_cv);
728		}
729		if (pkt.bmRequestType != UCDC_NOTIFICATION) {
730			goto tr_setup;
731		}
732		switch (pkt.bNotification) {
733		case UCDC_N_RESPONSE_AVAILABLE:
734			if (!(sc->sc_nobulk)) {
735				DPRINTF("Wrong serial state!\n");
736				break;
737			}
738			if (sc->sc_num_msg != 0xFF) {
739				sc->sc_num_msg++;
740			}
741			usbd_transfer_start(sc->sc_ctrl_xfer[UFOMA_CTRL_ENDPT_READ]);
742			break;
743
744		case UCDC_N_SERIAL_STATE:
745			if (sc->sc_nobulk) {
746				DPRINTF("Wrong serial state!\n");
747				break;
748			}
749			/*
750		         * Set the serial state in ucom driver based on
751		         * the bits from the notify message
752		         */
753			if (actlen < 2) {
754				DPRINTF("invalid notification "
755				    "length, %d bytes!\n", actlen);
756				break;
757			}
758			DPRINTF("notify bytes = 0x%02x, 0x%02x\n",
759			    pkt.data[0], pkt.data[1]);
760
761			/* currently, lsr is always zero. */
762			sc->sc_lsr = 0;
763			sc->sc_msr = 0;
764
765			mstatus = pkt.data[0];
766
767			if (mstatus & UCDC_N_SERIAL_RI) {
768				sc->sc_msr |= SER_RI;
769			}
770			if (mstatus & UCDC_N_SERIAL_DSR) {
771				sc->sc_msr |= SER_DSR;
772			}
773			if (mstatus & UCDC_N_SERIAL_DCD) {
774				sc->sc_msr |= SER_DCD;
775			}
776			ucom_status_change(&sc->sc_ucom);
777			break;
778
779		default:
780			break;
781		}
782
783	case USB_ST_SETUP:
784tr_setup:
785		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
786		usbd_transfer_submit(xfer);
787		return;
788
789	default:			/* Error */
790		if (error != USB_ERR_CANCELLED) {
791			/* try to clear stall first */
792			usbd_xfer_set_stall(xfer);
793			goto tr_setup;
794		}
795		return;
796	}
797}
798
799static void
800ufoma_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
801{
802	struct ufoma_softc *sc = usbd_xfer_softc(xfer);
803	struct usb_page_cache *pc;
804	uint32_t actlen;
805
806	switch (USB_GET_STATE(xfer)) {
807	case USB_ST_SETUP:
808	case USB_ST_TRANSFERRED:
809tr_setup:
810		pc = usbd_xfer_get_frame(xfer, 0);
811		if (ucom_get_data(&sc->sc_ucom, pc, 0,
812		    UFOMA_BULK_BUF_SIZE, &actlen)) {
813			usbd_xfer_set_frame_len(xfer, 0, actlen);
814			usbd_transfer_submit(xfer);
815		}
816		return;
817
818	default:			/* Error */
819		if (error != USB_ERR_CANCELLED) {
820			/* try to clear stall first */
821			usbd_xfer_set_stall(xfer);
822			goto tr_setup;
823		}
824		return;
825	}
826}
827
828static void
829ufoma_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
830{
831	struct ufoma_softc *sc = usbd_xfer_softc(xfer);
832	struct usb_page_cache *pc;
833	int actlen;
834
835	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
836
837	switch (USB_GET_STATE(xfer)) {
838	case USB_ST_TRANSFERRED:
839		pc = usbd_xfer_get_frame(xfer, 0);
840		ucom_put_data(&sc->sc_ucom, pc, 0, actlen);
841
842	case USB_ST_SETUP:
843tr_setup:
844		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
845		usbd_transfer_submit(xfer);
846		return;
847
848	default:			/* Error */
849		if (error != USB_ERR_CANCELLED) {
850			/* try to clear stall first */
851			usbd_xfer_set_stall(xfer);
852			goto tr_setup;
853		}
854		return;
855	}
856}
857
858static void
859ufoma_cfg_open(struct ucom_softc *ucom)
860{
861	struct ufoma_softc *sc = ucom->sc_parent;
862
863	/* empty input queue */
864
865	if (sc->sc_num_msg != 0xFF) {
866		sc->sc_num_msg++;
867	}
868	if (sc->sc_currentmode == UMCPC_ACM_MODE_UNLINKED) {
869		ufoma_cfg_link_state(sc);
870	}
871	if (sc->sc_currentmode == UMCPC_ACM_MODE_DEACTIVATED) {
872		ufoma_cfg_activate_state(sc, sc->sc_modetoactivate);
873	}
874}
875
876static void
877ufoma_cfg_close(struct ucom_softc *ucom)
878{
879	struct ufoma_softc *sc = ucom->sc_parent;
880
881	ufoma_cfg_activate_state(sc, UMCPC_ACM_MODE_DEACTIVATED);
882}
883
884static void
885ufoma_cfg_set_break(struct ucom_softc *ucom, uint8_t onoff)
886{
887	struct ufoma_softc *sc = ucom->sc_parent;
888	struct usb_device_request req;
889	uint16_t wValue;
890
891	if (sc->sc_nobulk ||
892	    (sc->sc_currentmode == UMCPC_ACM_MODE_OBEX)) {
893		return;
894	}
895	if (!(sc->sc_acm_cap & USB_CDC_ACM_HAS_BREAK)) {
896		return;
897	}
898	wValue = onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF;
899
900	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
901	req.bRequest = UCDC_SEND_BREAK;
902	USETW(req.wValue, wValue);
903	req.wIndex[0] = sc->sc_ctrl_iface_no;
904	req.wIndex[1] = 0;
905	USETW(req.wLength, 0);
906
907	ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
908	    &req, NULL, 0, 1000);
909}
910
911static void
912ufoma_cfg_get_status(struct ucom_softc *ucom, uint8_t *lsr, uint8_t *msr)
913{
914	struct ufoma_softc *sc = ucom->sc_parent;
915
916	*lsr = sc->sc_lsr;
917	*msr = sc->sc_msr;
918}
919
920static void
921ufoma_cfg_set_line_state(struct ufoma_softc *sc)
922{
923	struct usb_device_request req;
924
925	/* Don't send line state emulation request for OBEX port */
926	if (sc->sc_currentmode == UMCPC_ACM_MODE_OBEX) {
927		return;
928	}
929	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
930	req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
931	USETW(req.wValue, sc->sc_line);
932	req.wIndex[0] = sc->sc_ctrl_iface_no;
933	req.wIndex[1] = 0;
934	USETW(req.wLength, 0);
935
936	ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
937	    &req, NULL, 0, 1000);
938}
939
940static void
941ufoma_cfg_set_dtr(struct ucom_softc *ucom, uint8_t onoff)
942{
943	struct ufoma_softc *sc = ucom->sc_parent;
944
945	if (sc->sc_nobulk) {
946		return;
947	}
948	if (onoff)
949		sc->sc_line |= UCDC_LINE_DTR;
950	else
951		sc->sc_line &= ~UCDC_LINE_DTR;
952
953	ufoma_cfg_set_line_state(sc);
954}
955
956static void
957ufoma_cfg_set_rts(struct ucom_softc *ucom, uint8_t onoff)
958{
959	struct ufoma_softc *sc = ucom->sc_parent;
960
961	if (sc->sc_nobulk) {
962		return;
963	}
964	if (onoff)
965		sc->sc_line |= UCDC_LINE_RTS;
966	else
967		sc->sc_line &= ~UCDC_LINE_RTS;
968
969	ufoma_cfg_set_line_state(sc);
970}
971
972static int
973ufoma_pre_param(struct ucom_softc *ucom, struct termios *t)
974{
975	return (0);			/* we accept anything */
976}
977
978static void
979ufoma_cfg_param(struct ucom_softc *ucom, struct termios *t)
980{
981	struct ufoma_softc *sc = ucom->sc_parent;
982	struct usb_device_request req;
983	struct usb_cdc_line_state ls;
984
985	if (sc->sc_nobulk ||
986	    (sc->sc_currentmode == UMCPC_ACM_MODE_OBEX)) {
987		return;
988	}
989	DPRINTF("\n");
990
991	memset(&ls, 0, sizeof(ls));
992
993	USETDW(ls.dwDTERate, t->c_ospeed);
994
995	if (t->c_cflag & CSTOPB) {
996		ls.bCharFormat = UCDC_STOP_BIT_2;
997	} else {
998		ls.bCharFormat = UCDC_STOP_BIT_1;
999	}
1000
1001	if (t->c_cflag & PARENB) {
1002		if (t->c_cflag & PARODD) {
1003			ls.bParityType = UCDC_PARITY_ODD;
1004		} else {
1005			ls.bParityType = UCDC_PARITY_EVEN;
1006		}
1007	} else {
1008		ls.bParityType = UCDC_PARITY_NONE;
1009	}
1010
1011	switch (t->c_cflag & CSIZE) {
1012	case CS5:
1013		ls.bDataBits = 5;
1014		break;
1015	case CS6:
1016		ls.bDataBits = 6;
1017		break;
1018	case CS7:
1019		ls.bDataBits = 7;
1020		break;
1021	case CS8:
1022		ls.bDataBits = 8;
1023		break;
1024	}
1025
1026	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
1027	req.bRequest = UCDC_SET_LINE_CODING;
1028	USETW(req.wValue, 0);
1029	req.wIndex[0] = sc->sc_ctrl_iface_no;
1030	req.wIndex[1] = 0;
1031	USETW(req.wLength, UCDC_LINE_STATE_LENGTH);
1032
1033	ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
1034	    &req, &ls, 0, 1000);
1035}
1036
1037static int
1038ufoma_modem_setup(device_t dev, struct ufoma_softc *sc,
1039    struct usb_attach_arg *uaa)
1040{
1041	struct usb_config_descriptor *cd;
1042	struct usb_cdc_acm_descriptor *acm;
1043	struct usb_cdc_cm_descriptor *cmd;
1044	struct usb_interface_descriptor *id;
1045	struct usb_interface *iface;
1046	uint8_t i;
1047	int32_t error;
1048
1049	cd = usbd_get_config_descriptor(uaa->device);
1050	id = usbd_get_interface_descriptor(uaa->iface);
1051
1052	cmd = ufoma_get_intconf(cd, id, UDESC_CS_INTERFACE, UDESCSUB_CDC_CM);
1053
1054	if ((cmd == NULL) ||
1055	    (cmd->bLength < sizeof(*cmd))) {
1056		return (EINVAL);
1057	}
1058	sc->sc_cm_cap = cmd->bmCapabilities;
1059	sc->sc_data_iface_no = cmd->bDataInterface;
1060
1061	acm = ufoma_get_intconf(cd, id, UDESC_CS_INTERFACE, UDESCSUB_CDC_ACM);
1062
1063	if ((acm == NULL) ||
1064	    (acm->bLength < sizeof(*acm))) {
1065		return (EINVAL);
1066	}
1067	sc->sc_acm_cap = acm->bmCapabilities;
1068
1069	device_printf(dev, "data interface %d, has %sCM over data, "
1070	    "has %sbreak\n",
1071	    sc->sc_data_iface_no,
1072	    sc->sc_cm_cap & USB_CDC_CM_OVER_DATA ? "" : "no ",
1073	    sc->sc_acm_cap & USB_CDC_ACM_HAS_BREAK ? "" : "no ");
1074
1075	/* get the data interface too */
1076
1077	for (i = 0;; i++) {
1078
1079		iface = usbd_get_iface(uaa->device, i);
1080
1081		if (iface) {
1082
1083			id = usbd_get_interface_descriptor(iface);
1084
1085			if (id && (id->bInterfaceNumber == sc->sc_data_iface_no)) {
1086				sc->sc_data_iface_index = i;
1087				usbd_set_parent_iface(uaa->device, i, uaa->info.bIfaceIndex);
1088				break;
1089			}
1090		} else {
1091			device_printf(dev, "no data interface\n");
1092			return (EINVAL);
1093		}
1094	}
1095
1096	error = usbd_transfer_setup(uaa->device,
1097	    &sc->sc_data_iface_index, sc->sc_bulk_xfer,
1098	    ufoma_bulk_config, UFOMA_BULK_ENDPT_MAX, sc, &sc->sc_mtx);
1099
1100	if (error) {
1101		device_printf(dev, "allocating BULK USB "
1102		    "transfers failed\n");
1103		return (EINVAL);
1104	}
1105	return (0);
1106}
1107
1108static void
1109ufoma_start_read(struct ucom_softc *ucom)
1110{
1111	struct ufoma_softc *sc = ucom->sc_parent;
1112
1113	/* start interrupt transfer */
1114	usbd_transfer_start(sc->sc_ctrl_xfer[UFOMA_CTRL_ENDPT_INTR]);
1115
1116	/* start data transfer */
1117	if (sc->sc_nobulk) {
1118		usbd_transfer_start(sc->sc_ctrl_xfer[UFOMA_CTRL_ENDPT_READ]);
1119	} else {
1120		usbd_transfer_start(sc->sc_bulk_xfer[UFOMA_BULK_ENDPT_READ]);
1121	}
1122}
1123
1124static void
1125ufoma_stop_read(struct ucom_softc *ucom)
1126{
1127	struct ufoma_softc *sc = ucom->sc_parent;
1128
1129	/* stop interrupt transfer */
1130	usbd_transfer_stop(sc->sc_ctrl_xfer[UFOMA_CTRL_ENDPT_INTR]);
1131
1132	/* stop data transfer */
1133	if (sc->sc_nobulk) {
1134		usbd_transfer_stop(sc->sc_ctrl_xfer[UFOMA_CTRL_ENDPT_READ]);
1135	} else {
1136		usbd_transfer_stop(sc->sc_bulk_xfer[UFOMA_BULK_ENDPT_READ]);
1137	}
1138}
1139
1140static void
1141ufoma_start_write(struct ucom_softc *ucom)
1142{
1143	struct ufoma_softc *sc = ucom->sc_parent;
1144
1145	if (sc->sc_nobulk) {
1146		usbd_transfer_start(sc->sc_ctrl_xfer[UFOMA_CTRL_ENDPT_WRITE]);
1147	} else {
1148		usbd_transfer_start(sc->sc_bulk_xfer[UFOMA_BULK_ENDPT_WRITE]);
1149	}
1150}
1151
1152static void
1153ufoma_stop_write(struct ucom_softc *ucom)
1154{
1155	struct ufoma_softc *sc = ucom->sc_parent;
1156
1157	if (sc->sc_nobulk) {
1158		usbd_transfer_stop(sc->sc_ctrl_xfer[UFOMA_CTRL_ENDPT_WRITE]);
1159	} else {
1160		usbd_transfer_stop(sc->sc_bulk_xfer[UFOMA_BULK_ENDPT_WRITE]);
1161	}
1162}
1163
1164static struct umcpc_modetostr_tab{
1165	int mode;
1166	char *str;
1167}umcpc_modetostr_tab[]={
1168	{UMCPC_ACM_MODE_DEACTIVATED, "deactivated"},
1169	{UMCPC_ACM_MODE_MODEM, "modem"},
1170	{UMCPC_ACM_MODE_ATCOMMAND, "handsfree"},
1171	{UMCPC_ACM_MODE_OBEX, "obex"},
1172	{UMCPC_ACM_MODE_VENDOR1, "vendor1"},
1173	{UMCPC_ACM_MODE_VENDOR2, "vendor2"},
1174	{UMCPC_ACM_MODE_UNLINKED, "unlinked"},
1175	{0, NULL}
1176};
1177
1178static char *ufoma_mode_to_str(int mode)
1179{
1180	int i;
1181	for(i = 0 ;umcpc_modetostr_tab[i].str != NULL; i++){
1182		if(umcpc_modetostr_tab[i].mode == mode){
1183			return umcpc_modetostr_tab[i].str;
1184		}
1185	}
1186	return NULL;
1187}
1188
1189static int ufoma_str_to_mode(char *str)
1190{
1191	int i;
1192	for(i = 0 ;umcpc_modetostr_tab[i].str != NULL; i++){
1193		if(strcmp(str, umcpc_modetostr_tab[i].str)==0){
1194			return umcpc_modetostr_tab[i].mode;
1195		}
1196	}
1197	return -1;
1198}
1199
1200static int ufoma_sysctl_support(SYSCTL_HANDLER_ARGS)
1201{
1202	struct ufoma_softc *sc = (struct ufoma_softc *)oidp->oid_arg1;
1203	struct sbuf sb;
1204	int i;
1205	char *mode;
1206
1207	sbuf_new(&sb, NULL, 1, SBUF_AUTOEXTEND);
1208	for(i = 1; i < sc->sc_modetable[0]; i++){
1209		mode = ufoma_mode_to_str(sc->sc_modetable[i]);
1210		if(mode !=NULL){
1211			sbuf_cat(&sb, mode);
1212		}else{
1213			sbuf_printf(&sb, "(%02x)", sc->sc_modetable[i]);
1214		}
1215		if(i < (sc->sc_modetable[0]-1))
1216			sbuf_cat(&sb, ",");
1217	}
1218	sbuf_trim(&sb);
1219	sbuf_finish(&sb);
1220	sysctl_handle_string(oidp, sbuf_data(&sb), sbuf_len(&sb), req);
1221	sbuf_delete(&sb);
1222
1223	return 0;
1224}
1225static int ufoma_sysctl_current(SYSCTL_HANDLER_ARGS)
1226{
1227	struct ufoma_softc *sc = (struct ufoma_softc *)oidp->oid_arg1;
1228	char *mode;
1229	char subbuf[]="(XXX)";
1230	mode = ufoma_mode_to_str(sc->sc_currentmode);
1231	if(!mode){
1232		mode = subbuf;
1233		snprintf(subbuf, sizeof(subbuf), "(%02x)", sc->sc_currentmode);
1234	}
1235	sysctl_handle_string(oidp, mode, strlen(mode), req);
1236
1237	return 0;
1238
1239}
1240static int ufoma_sysctl_open(SYSCTL_HANDLER_ARGS)
1241{
1242	struct ufoma_softc *sc = (struct ufoma_softc *)oidp->oid_arg1;
1243	char *mode;
1244	char subbuf[40];
1245	int newmode;
1246	int error;
1247	int i;
1248
1249	mode = ufoma_mode_to_str(sc->sc_modetoactivate);
1250	if(mode){
1251		strncpy(subbuf, mode, sizeof(subbuf));
1252	}else{
1253		snprintf(subbuf, sizeof(subbuf), "(%02x)", sc->sc_modetoactivate);
1254	}
1255	error = sysctl_handle_string(oidp, subbuf, sizeof(subbuf), req);
1256	if(error != 0 || req->newptr == NULL){
1257		return error;
1258	}
1259
1260	if((newmode = ufoma_str_to_mode(subbuf)) == -1){
1261		return EINVAL;
1262	}
1263
1264	for(i = 1 ; i < sc->sc_modetable[0] ; i++){
1265		if(sc->sc_modetable[i] == newmode){
1266			sc->sc_modetoactivate = newmode;
1267			return 0;
1268		}
1269	}
1270
1271	return EINVAL;
1272}
1273
1274static void
1275ufoma_poll(struct ucom_softc *ucom)
1276{
1277	struct ufoma_softc *sc = ucom->sc_parent;
1278	usbd_transfer_poll(sc->sc_ctrl_xfer, UFOMA_CTRL_ENDPT_MAX);
1279	usbd_transfer_poll(sc->sc_bulk_xfer, UFOMA_BULK_ENDPT_MAX);
1280}
1281