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