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