kernel: Use the new auto-created sysctl ctx/tree in various drivers.
[dragonfly.git] / sys / bus / u4b / serial / ufoma.c
1 /*      $NetBSD: umodem.c,v 1.45 2002/09/23 05:51:23 simonb Exp $       */
2
3 #define UFOMA_HANDSFREE
4 /*-
5  * Copyright (c) 2005, Takanori Watanabe
6  * Copyright (c) 2003, M. Warner Losh <imp@FreeBSD.org>.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30
31 /*-
32  * Copyright (c) 1998 The NetBSD Foundation, Inc.
33  * All rights reserved.
34  *
35  * This code is derived from software contributed to The NetBSD Foundation
36  * by Lennart Augustsson (lennart@augustsson.net) at
37  * Carlstedt Research & Technology.
38  *
39  * Redistribution and use in source and binary forms, with or without
40  * modification, are permitted provided that the following conditions
41  * are met:
42  * 1. Redistributions of source code must retain the above copyright
43  *    notice, this list of conditions and the following disclaimer.
44  * 2. Redistributions in binary form must reproduce the above copyright
45  *    notice, this list of conditions and the following disclaimer in the
46  *    documentation and/or other materials provided with the distribution.
47  * 3. All advertising materials mentioning features or use of this software
48  *    must display the following acknowledgement:
49  *        This product includes software developed by the NetBSD
50  *        Foundation, Inc. and its contributors.
51  * 4. Neither the name of The NetBSD Foundation nor the names of its
52  *    contributors may be used to endorse or promote products derived
53  *    from this software without specific prior written permission.
54  *
55  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
56  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
57  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
58  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
59  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
60  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
61  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
62  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
63  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
64  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
65  * POSSIBILITY OF SUCH DAMAGE.
66  */
67
68 /*
69  * Comm Class spec:  http://www.usb.org/developers/devclass_docs/usbccs10.pdf
70  *                   http://www.usb.org/developers/devclass_docs/usbcdc11.pdf
71  */
72
73 /*
74  * TODO:
75  * - Implement a Call Device for modems without multiplexed commands.
76  */
77
78 /*
79  * NOTE: all function names beginning like "ufoma_cfg_" can only
80  * be called from within the config thread function !
81  */
82
83 #include <sys/stdint.h>
84 #include <sys/param.h>
85 #include <sys/queue.h>
86 #include <sys/types.h>
87 #include <sys/systm.h>
88 #include <sys/kernel.h>
89 #include <sys/bus.h>
90 #include <sys/module.h>
91 #include <sys/lock.h>
92 #include <sys/condvar.h>
93 #include <sys/sysctl.h>
94 #include <sys/unistd.h>
95 #include <sys/callout.h>
96 #include <sys/malloc.h>
97 #include <sys/priv.h>
98 #include <sys/sbuf.h>
99 #include <sys/serial.h>
100
101 #include <bus/u4b/usb.h>
102 #include <bus/u4b/usbdi.h>
103 #include <bus/u4b/usbdi_util.h>
104 #include <bus/u4b/usb_cdc.h>
105 #include "usbdevs.h"
106
107 #define USB_DEBUG_VAR usb_debug
108 #include <bus/u4b/usb_debug.h>
109 #include <bus/u4b/usb_process.h>
110
111 #include <bus/u4b/serial/usb_serial.h>
112
113 typedef struct ufoma_mobile_acm_descriptor {
114         uint8_t bFunctionLength;
115         uint8_t bDescriptorType;
116         uint8_t bDescriptorSubtype;
117         uint8_t bType;
118         uint8_t bMode[1];
119 } __packed usb_mcpc_acm_descriptor;
120
121 #define UISUBCLASS_MCPC 0x88
122
123 #define UDESC_VS_INTERFACE 0x44
124 #define UDESCSUB_MCPC_ACM  0x11
125
126 #define UMCPC_ACM_TYPE_AB1 0x1
127 #define UMCPC_ACM_TYPE_AB2 0x2
128 #define UMCPC_ACM_TYPE_AB5 0x5
129 #define UMCPC_ACM_TYPE_AB6 0x6
130
131 #define UMCPC_ACM_MODE_DEACTIVATED 0x0
132 #define UMCPC_ACM_MODE_MODEM 0x1
133 #define UMCPC_ACM_MODE_ATCOMMAND 0x2
134 #define UMCPC_ACM_MODE_OBEX 0x60
135 #define UMCPC_ACM_MODE_VENDOR1 0xc0
136 #define UMCPC_ACM_MODE_VENDOR2 0xfe
137 #define UMCPC_ACM_MODE_UNLINKED 0xff
138
139 #define UMCPC_CM_MOBILE_ACM 0x0
140
141 #define UMCPC_ACTIVATE_MODE 0x60
142 #define UMCPC_GET_MODETABLE 0x61
143 #define UMCPC_SET_LINK 0x62
144 #define UMCPC_CLEAR_LINK 0x63
145
146 #define UMCPC_REQUEST_ACKNOWLEDGE 0x31
147
148 #define UFOMA_MAX_TIMEOUT 15            /* standard says 10 seconds */
149 #define UFOMA_CMD_BUF_SIZE 64           /* bytes */
150
151 #define UFOMA_BULK_BUF_SIZE 1024        /* bytes */
152
153 enum {
154         UFOMA_CTRL_ENDPT_INTR,
155         UFOMA_CTRL_ENDPT_READ,
156         UFOMA_CTRL_ENDPT_WRITE,
157         UFOMA_CTRL_ENDPT_MAX,
158 };
159
160 enum {
161         UFOMA_BULK_ENDPT_WRITE,
162         UFOMA_BULK_ENDPT_READ,  
163         UFOMA_BULK_ENDPT_MAX,
164 };
165
166 struct ufoma_softc {
167         struct ucom_super_softc sc_super_ucom;
168         struct ucom_softc sc_ucom;
169         struct cv sc_cv;
170         struct lock sc_lock;
171
172         struct usb_xfer *sc_ctrl_xfer[UFOMA_CTRL_ENDPT_MAX];
173         struct usb_xfer *sc_bulk_xfer[UFOMA_BULK_ENDPT_MAX];
174         uint8_t *sc_modetable;
175         device_t sc_dev;
176         struct usb_device *sc_udev;
177
178         uint32_t sc_unit;
179
180         uint16_t sc_line;
181
182         uint8_t sc_num_msg;
183         uint8_t sc_nobulk;
184         uint8_t sc_ctrl_iface_no;
185         uint8_t sc_ctrl_iface_index;
186         uint8_t sc_data_iface_no;
187         uint8_t sc_data_iface_index;
188         uint8_t sc_cm_cap;
189         uint8_t sc_acm_cap;
190         uint8_t sc_lsr;
191         uint8_t sc_msr;
192         uint8_t sc_modetoactivate;
193         uint8_t sc_currentmode;
194         uint8_t sc_name[16];
195 };
196
197 /* prototypes */
198
199 static device_probe_t ufoma_probe;
200 static device_attach_t ufoma_attach;
201 static device_detach_t ufoma_detach;
202
203 static usb_callback_t ufoma_ctrl_read_callback;
204 static usb_callback_t ufoma_ctrl_write_callback;
205 static usb_callback_t ufoma_intr_callback;
206 static usb_callback_t ufoma_bulk_write_callback;
207 static usb_callback_t ufoma_bulk_read_callback;
208
209 static void     *ufoma_get_intconf(struct usb_config_descriptor *,
210                     struct usb_interface_descriptor *, uint8_t, uint8_t);
211 static void     ufoma_cfg_link_state(struct ufoma_softc *);
212 static void     ufoma_cfg_activate_state(struct ufoma_softc *, uint16_t);
213 static void     ufoma_cfg_open(struct ucom_softc *);
214 static void     ufoma_cfg_close(struct ucom_softc *);
215 static void     ufoma_cfg_set_break(struct ucom_softc *, uint8_t);
216 static void     ufoma_cfg_get_status(struct ucom_softc *, uint8_t *,
217                     uint8_t *);
218 static void     ufoma_cfg_set_dtr(struct ucom_softc *, uint8_t);
219 static void     ufoma_cfg_set_rts(struct ucom_softc *, uint8_t);
220 static int      ufoma_pre_param(struct ucom_softc *, struct termios *);
221 static void     ufoma_cfg_param(struct ucom_softc *, struct termios *);
222 static int      ufoma_modem_setup(device_t, struct ufoma_softc *,
223                     struct usb_attach_arg *);
224 static void     ufoma_start_read(struct ucom_softc *);
225 static void     ufoma_stop_read(struct ucom_softc *);
226 static void     ufoma_start_write(struct ucom_softc *);
227 static void     ufoma_stop_write(struct ucom_softc *);
228 static void     ufoma_poll(struct ucom_softc *ucom);
229
230 /*sysctl stuff*/
231 static int ufoma_sysctl_support(SYSCTL_HANDLER_ARGS);
232 static int ufoma_sysctl_current(SYSCTL_HANDLER_ARGS);
233 static int ufoma_sysctl_open(SYSCTL_HANDLER_ARGS);
234
235 static const struct usb_config
236         ufoma_ctrl_config[UFOMA_CTRL_ENDPT_MAX] = {
237
238         [UFOMA_CTRL_ENDPT_INTR] = {
239                 .type = UE_INTERRUPT,
240                 .endpoint = UE_ADDR_ANY,
241                 .direction = UE_DIR_IN,
242                 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
243                 .bufsize = sizeof(struct usb_cdc_notification),
244                 .callback = &ufoma_intr_callback,
245         },
246
247         [UFOMA_CTRL_ENDPT_READ] = {
248                 .type = UE_CONTROL,
249                 .endpoint = 0x00,       /* Control pipe */
250                 .direction = UE_DIR_ANY,
251                 .bufsize = (sizeof(struct usb_device_request) + UFOMA_CMD_BUF_SIZE),
252                 .flags = {.short_xfer_ok = 1,},
253                 .callback = &ufoma_ctrl_read_callback,
254                 .timeout = 1000,        /* 1 second */
255         },
256
257         [UFOMA_CTRL_ENDPT_WRITE] = {
258                 .type = UE_CONTROL,
259                 .endpoint = 0x00,       /* Control pipe */
260                 .direction = UE_DIR_ANY,
261                 .bufsize = (sizeof(struct usb_device_request) + 1),
262                 .callback = &ufoma_ctrl_write_callback,
263                 .timeout = 1000,        /* 1 second */
264         },
265 };
266
267 static const struct usb_config
268         ufoma_bulk_config[UFOMA_BULK_ENDPT_MAX] = {
269
270         [UFOMA_BULK_ENDPT_WRITE] = {
271                 .type = UE_BULK,
272                 .endpoint = UE_ADDR_ANY,
273                 .direction = UE_DIR_OUT,
274                 .bufsize = UFOMA_BULK_BUF_SIZE,
275                 .flags = {.pipe_bof = 1,.force_short_xfer = 1,},
276                 .callback = &ufoma_bulk_write_callback,
277         },
278
279         [UFOMA_BULK_ENDPT_READ] = {
280                 .type = UE_BULK,
281                 .endpoint = UE_ADDR_ANY,
282                 .direction = UE_DIR_IN,
283                 .bufsize = UFOMA_BULK_BUF_SIZE,
284                 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
285                 .callback = &ufoma_bulk_read_callback,
286         },
287 };
288
289 static const struct ucom_callback ufoma_callback = {
290         .ucom_cfg_get_status = &ufoma_cfg_get_status,
291         .ucom_cfg_set_dtr = &ufoma_cfg_set_dtr,
292         .ucom_cfg_set_rts = &ufoma_cfg_set_rts,
293         .ucom_cfg_set_break = &ufoma_cfg_set_break,
294         .ucom_cfg_param = &ufoma_cfg_param,
295         .ucom_cfg_open = &ufoma_cfg_open,
296         .ucom_cfg_close = &ufoma_cfg_close,
297         .ucom_pre_param = &ufoma_pre_param,
298         .ucom_start_read = &ufoma_start_read,
299         .ucom_stop_read = &ufoma_stop_read,
300         .ucom_start_write = &ufoma_start_write,
301         .ucom_stop_write = &ufoma_stop_write,
302         .ucom_poll = &ufoma_poll,
303 };
304
305 static device_method_t ufoma_methods[] = {
306         /* Device methods */
307         DEVMETHOD(device_probe, ufoma_probe),
308         DEVMETHOD(device_attach, ufoma_attach),
309         DEVMETHOD(device_detach, ufoma_detach),
310         DEVMETHOD_END
311 };
312
313 static devclass_t ufoma_devclass;
314
315 static driver_t ufoma_driver = {
316         .name = "ufoma",
317         .methods = ufoma_methods,
318         .size = sizeof(struct ufoma_softc),
319 };
320
321 DRIVER_MODULE(ufoma, uhub, ufoma_driver, ufoma_devclass, NULL, NULL);
322 MODULE_DEPEND(ufoma, ucom, 1, 1, 1);
323 MODULE_DEPEND(ufoma, usb, 1, 1, 1);
324 MODULE_VERSION(ufoma, 1);
325
326 static const STRUCT_USB_HOST_ID ufoma_devs[] = {
327         {USB_IFACE_CLASS(UICLASS_CDC),
328          USB_IFACE_SUBCLASS(UISUBCLASS_MCPC),},
329 };
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         int error;
339
340         if (uaa->usb_mode != USB_MODE_HOST)
341                 return (ENXIO);
342
343         error = usbd_lookup_id_by_uaa(ufoma_devs, sizeof(ufoma_devs), uaa);
344         if (error)
345                 return (error);
346
347         id = usbd_get_interface_descriptor(uaa->iface);
348         cd = usbd_get_config_descriptor(uaa->device);
349
350         if (id == NULL || cd == NULL)
351                 return (ENXIO);
352
353         mad = ufoma_get_intconf(cd, id, UDESC_VS_INTERFACE, UDESCSUB_MCPC_ACM);
354         if (mad == NULL)
355                 return (ENXIO);
356
357 #ifndef UFOMA_HANDSFREE
358         if ((mad->bType == UMCPC_ACM_TYPE_AB5) ||
359             (mad->bType == UMCPC_ACM_TYPE_AB6))
360                 return (ENXIO);
361 #endif
362         return (BUS_PROBE_GENERIC);
363 }
364
365 static int
366 ufoma_attach(device_t dev)
367 {
368         struct usb_attach_arg *uaa = device_get_ivars(dev);
369         struct ufoma_softc *sc = device_get_softc(dev);
370         struct usb_config_descriptor *cd;
371         struct usb_interface_descriptor *id;
372         struct sysctl_ctx_list *sctx;
373         struct sysctl_oid *soid;
374
375         usb_mcpc_acm_descriptor *mad;
376         uint8_t elements;
377         int32_t error;
378
379         sc->sc_udev = uaa->device;
380         sc->sc_dev = dev;
381         sc->sc_unit = device_get_unit(dev);
382
383         lockinit(&sc->sc_lock, "ufoma", 0, LK_CANRECURSE);
384         cv_init(&sc->sc_cv, "CWAIT");
385
386         device_set_usb_desc(dev);
387
388         ksnprintf(sc->sc_name, sizeof(sc->sc_name),
389             "%s", device_get_nameunit(dev));
390
391         DPRINTF("\n");
392
393         /* setup control transfers */
394
395         cd = usbd_get_config_descriptor(uaa->device);
396         id = usbd_get_interface_descriptor(uaa->iface);
397         sc->sc_ctrl_iface_no = id->bInterfaceNumber;
398         sc->sc_ctrl_iface_index = uaa->info.bIfaceIndex;
399
400         error = usbd_transfer_setup(uaa->device,
401             &sc->sc_ctrl_iface_index, sc->sc_ctrl_xfer,
402             ufoma_ctrl_config, UFOMA_CTRL_ENDPT_MAX, sc, &sc->sc_lock);
403
404         if (error) {
405                 device_printf(dev, "allocating control USB "
406                     "transfers failed\n");
407                 goto detach;
408         }
409         mad = ufoma_get_intconf(cd, id, UDESC_VS_INTERFACE, UDESCSUB_MCPC_ACM);
410         if (mad == NULL) {
411                 goto detach;
412         }
413         if (mad->bFunctionLength < sizeof(*mad)) {
414                 device_printf(dev, "invalid MAD descriptor\n");
415                 goto detach;
416         }
417         if ((mad->bType == UMCPC_ACM_TYPE_AB5) ||
418             (mad->bType == UMCPC_ACM_TYPE_AB6)) {
419                 sc->sc_nobulk = 1;
420         } else {
421                 sc->sc_nobulk = 0;
422                 if (ufoma_modem_setup(dev, sc, uaa)) {
423                         goto detach;
424                 }
425         }
426
427         elements = (mad->bFunctionLength - sizeof(*mad) + 1);
428
429         /* initialize mode variables */
430
431         sc->sc_modetable = kmalloc(elements + 1, M_USBDEV, M_WAITOK);
432
433         if (sc->sc_modetable == NULL) {
434                 goto detach;
435         }
436         sc->sc_modetable[0] = (elements + 1);
437         memcpy(&sc->sc_modetable[1], mad->bMode, elements);
438
439         sc->sc_currentmode = UMCPC_ACM_MODE_UNLINKED;
440         sc->sc_modetoactivate = mad->bMode[0];
441
442         /* clear stall at first run, if any */
443         lockmgr(&sc->sc_lock, LK_EXCLUSIVE);
444         usbd_xfer_set_stall(sc->sc_bulk_xfer[UFOMA_BULK_ENDPT_WRITE]);
445         usbd_xfer_set_stall(sc->sc_bulk_xfer[UFOMA_BULK_ENDPT_READ]);
446         lockmgr(&sc->sc_lock, LK_RELEASE);
447
448         error = ucom_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc,
449             &ufoma_callback, &sc->sc_lock);
450         if (error) {
451                 DPRINTF("ucom_attach failed\n");
452                 goto detach;
453         }
454         ucom_set_pnpinfo_usb(&sc->sc_super_ucom, dev);
455
456         /*Sysctls*/
457         sctx = device_get_sysctl_ctx(dev);
458         soid = device_get_sysctl_tree(dev);
459
460         SYSCTL_ADD_PROC(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "supportmode",
461                         CTLFLAG_RD|CTLTYPE_STRING, sc, 0, ufoma_sysctl_support,
462                         "A", "Supporting port role");
463
464         SYSCTL_ADD_PROC(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "currentmode",
465                         CTLFLAG_RD|CTLTYPE_STRING, sc, 0, ufoma_sysctl_current,
466                         "A", "Current port role");
467
468         SYSCTL_ADD_PROC(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "openmode",
469                         CTLFLAG_RW|CTLTYPE_STRING, sc, 0, ufoma_sysctl_open,
470                         "A", "Mode to transit when port is opened");
471         SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "comunit",
472                         CTLFLAG_RD, &(sc->sc_super_ucom.sc_unit), 0, 
473                         "Unit number as USB serial");
474
475         return (0);                     /* success */
476
477 detach:
478         ufoma_detach(dev);
479         return (ENXIO);                 /* failure */
480 }
481
482 static int
483 ufoma_detach(device_t dev)
484 {
485         struct ufoma_softc *sc = device_get_softc(dev);
486
487         ucom_detach(&sc->sc_super_ucom, &sc->sc_ucom);
488         usbd_transfer_unsetup(sc->sc_ctrl_xfer, UFOMA_CTRL_ENDPT_MAX);
489         usbd_transfer_unsetup(sc->sc_bulk_xfer, UFOMA_BULK_ENDPT_MAX);
490
491         if (sc->sc_modetable) {
492                 kfree(sc->sc_modetable, M_USBDEV);
493         }
494         lockuninit(&sc->sc_lock);
495         cv_destroy(&sc->sc_cv);
496
497         return (0);
498 }
499
500 static void *
501 ufoma_get_intconf(struct usb_config_descriptor *cd, struct usb_interface_descriptor *id,
502     uint8_t type, uint8_t subtype)
503 {
504         struct usb_descriptor *desc = (void *)id;
505
506         while ((desc = usb_desc_foreach(cd, desc))) {
507
508                 if (desc->bDescriptorType == UDESC_INTERFACE) {
509                         return (NULL);
510                 }
511                 if ((desc->bDescriptorType == type) &&
512                     (desc->bDescriptorSubtype == subtype)) {
513                         break;
514                 }
515         }
516         return (desc);
517 }
518
519 static void
520 ufoma_cfg_link_state(struct ufoma_softc *sc)
521 {
522         struct usb_device_request req;
523         int32_t error;
524
525         req.bmRequestType = UT_WRITE_VENDOR_INTERFACE;
526         req.bRequest = UMCPC_SET_LINK;
527         USETW(req.wValue, UMCPC_CM_MOBILE_ACM);
528         USETW(req.wIndex, sc->sc_ctrl_iface_no);
529         USETW(req.wLength, sc->sc_modetable[0]);
530
531         ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 
532             &req, sc->sc_modetable, 0, 1000);
533
534         error = cv_timedwait(&sc->sc_cv, &sc->sc_lock, hz);
535
536         if (error) {
537                 DPRINTF("NO response\n");
538         }
539 }
540
541 static void
542 ufoma_cfg_activate_state(struct ufoma_softc *sc, uint16_t state)
543 {
544         struct usb_device_request req;
545         int32_t error;
546
547         req.bmRequestType = UT_WRITE_VENDOR_INTERFACE;
548         req.bRequest = UMCPC_ACTIVATE_MODE;
549         USETW(req.wValue, state);
550         USETW(req.wIndex, sc->sc_ctrl_iface_no);
551         USETW(req.wLength, 0);
552
553         ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 
554             &req, NULL, 0, 1000);
555
556         error = cv_timedwait(&sc->sc_cv, &sc->sc_lock,
557             (UFOMA_MAX_TIMEOUT * hz));
558         if (error) {
559                 DPRINTF("No response\n");
560         }
561 }
562
563 static void
564 ufoma_ctrl_read_callback(struct usb_xfer *xfer, usb_error_t error)
565 {
566         struct ufoma_softc *sc = usbd_xfer_softc(xfer);
567         struct usb_device_request req;
568         struct usb_page_cache *pc0, *pc1;
569         int len, aframes, nframes;
570
571         usbd_xfer_status(xfer, NULL, NULL, &aframes, &nframes);
572
573         switch (USB_GET_STATE(xfer)) {
574         case USB_ST_TRANSFERRED:
575 tr_transferred:
576                 if (aframes != nframes)
577                         goto tr_setup;
578                 pc1 = usbd_xfer_get_frame(xfer, 1);
579                 len = usbd_xfer_frame_len(xfer, 1);
580                 if (len > 0)
581                         ucom_put_data(&sc->sc_ucom, pc1, 0, len);
582                 /* FALLTHROUGH */
583         case USB_ST_SETUP:
584 tr_setup:
585                 if (sc->sc_num_msg) {
586                         sc->sc_num_msg--;
587
588                         req.bmRequestType = UT_READ_CLASS_INTERFACE;
589                         req.bRequest = UCDC_GET_ENCAPSULATED_RESPONSE;
590                         USETW(req.wIndex, sc->sc_ctrl_iface_no);
591                         USETW(req.wValue, 0);
592                         USETW(req.wLength, UFOMA_CMD_BUF_SIZE);
593
594                         pc0 = usbd_xfer_get_frame(xfer, 0);
595                         usbd_copy_in(pc0, 0, &req, sizeof(req));
596
597                         usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
598                         usbd_xfer_set_frame_len(xfer, 1, UFOMA_CMD_BUF_SIZE);
599                         usbd_xfer_set_frames(xfer, 2);
600                         usbd_transfer_submit(xfer);
601                 }
602                 return;
603
604         default:                        /* Error */
605                 DPRINTF("error = %s\n",
606                     usbd_errstr(error));
607
608                 if (error == USB_ERR_CANCELLED) {
609                         return;
610                 } else {
611                         goto tr_setup;
612                 }
613
614                 goto tr_transferred;
615         }
616 }
617
618 static void
619 ufoma_ctrl_write_callback(struct usb_xfer *xfer, usb_error_t error)
620 {
621         struct ufoma_softc *sc = usbd_xfer_softc(xfer);
622         struct usb_device_request req;
623         struct usb_page_cache *pc;
624         uint32_t actlen;
625
626         switch (USB_GET_STATE(xfer)) {
627         case USB_ST_TRANSFERRED:
628 tr_transferred:
629         case USB_ST_SETUP:
630 tr_setup:
631                 pc = usbd_xfer_get_frame(xfer, 1);
632                 if (ucom_get_data(&sc->sc_ucom, pc, 0, 1, &actlen)) {
633
634                         req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
635                         req.bRequest = UCDC_SEND_ENCAPSULATED_COMMAND;
636                         USETW(req.wIndex, sc->sc_ctrl_iface_no);
637                         USETW(req.wValue, 0);
638                         USETW(req.wLength, 1);
639
640                         pc = usbd_xfer_get_frame(xfer, 0);
641                         usbd_copy_in(pc, 0, &req, sizeof(req));
642
643                         usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
644                         usbd_xfer_set_frame_len(xfer, 1, 1);
645                         usbd_xfer_set_frames(xfer, 2);
646
647                         usbd_transfer_submit(xfer);
648                 }
649                 return;
650
651         default:                        /* Error */
652                 DPRINTF("error = %s\n", usbd_errstr(error));
653
654                 if (error == USB_ERR_CANCELLED) {
655                         return;
656                 } else {
657                         goto tr_setup;
658                 }
659
660                 goto tr_transferred;
661         }
662 }
663
664 static void
665 ufoma_intr_callback(struct usb_xfer *xfer, usb_error_t error)
666 {
667         struct ufoma_softc *sc = usbd_xfer_softc(xfer);
668         struct usb_cdc_notification pkt;
669         struct usb_page_cache *pc;
670         uint16_t wLen;
671         uint16_t temp;
672         uint8_t mstatus;
673         int actlen;
674
675         usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
676
677         switch (USB_GET_STATE(xfer)) {
678         case USB_ST_TRANSFERRED:
679                 if (actlen < 8) {
680                         DPRINTF("too short message\n");
681                         goto tr_setup;
682                 }
683                 if (actlen > sizeof(pkt)) {
684                         DPRINTF("truncating message\n");
685                         actlen = sizeof(pkt);
686                 }
687                 pc = usbd_xfer_get_frame(xfer, 0);
688                 usbd_copy_out(pc, 0, &pkt, actlen);
689
690                 actlen -= 8;
691
692                 wLen = UGETW(pkt.wLength);
693                 if (actlen > wLen) {
694                         actlen = wLen;
695                 }
696                 if ((pkt.bmRequestType == UT_READ_VENDOR_INTERFACE) &&
697                     (pkt.bNotification == UMCPC_REQUEST_ACKNOWLEDGE)) {
698                         temp = UGETW(pkt.wValue);
699                         sc->sc_currentmode = (temp >> 8);
700                         if (!(temp & 0xff)) {
701                                 DPRINTF("Mode change failed!\n");
702                         }
703                         cv_signal(&sc->sc_cv);
704                 }
705                 if (pkt.bmRequestType != UCDC_NOTIFICATION) {
706                         goto tr_setup;
707                 }
708                 switch (pkt.bNotification) {
709                 case UCDC_N_RESPONSE_AVAILABLE:
710                         if (!(sc->sc_nobulk)) {
711                                 DPRINTF("Wrong serial state!\n");
712                                 break;
713                         }
714                         if (sc->sc_num_msg != 0xFF) {
715                                 sc->sc_num_msg++;
716                         }
717                         usbd_transfer_start(sc->sc_ctrl_xfer[UFOMA_CTRL_ENDPT_READ]);
718                         break;
719
720                 case UCDC_N_SERIAL_STATE:
721                         if (sc->sc_nobulk) {
722                                 DPRINTF("Wrong serial state!\n");
723                                 break;
724                         }
725                         /*
726                          * Set the serial state in ucom driver based on
727                          * the bits from the notify message
728                          */
729                         if (actlen < 2) {
730                                 DPRINTF("invalid notification "
731                                     "length, %d bytes!\n", actlen);
732                                 break;
733                         }
734                         DPRINTF("notify bytes = 0x%02x, 0x%02x\n",
735                             pkt.data[0], pkt.data[1]);
736
737                         /* currently, lsr is always zero. */
738                         sc->sc_lsr = 0;
739                         sc->sc_msr = 0;
740
741                         mstatus = pkt.data[0];
742
743                         if (mstatus & UCDC_N_SERIAL_RI) {
744                                 sc->sc_msr |= SER_RI;
745                         }
746                         if (mstatus & UCDC_N_SERIAL_DSR) {
747                                 sc->sc_msr |= SER_DSR;
748                         }
749                         if (mstatus & UCDC_N_SERIAL_DCD) {
750                                 sc->sc_msr |= SER_DCD;
751                         }
752                         ucom_status_change(&sc->sc_ucom);
753                         break;
754
755                 default:
756                         break;
757                 }
758
759         case USB_ST_SETUP:
760 tr_setup:
761                 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
762                 usbd_transfer_submit(xfer);
763                 return;
764
765         default:                        /* Error */
766                 if (error != USB_ERR_CANCELLED) {
767                         /* try to clear stall first */
768                         usbd_xfer_set_stall(xfer);
769                         goto tr_setup;
770                 }
771                 return;
772         }
773 }
774
775 static void
776 ufoma_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
777 {
778         struct ufoma_softc *sc = usbd_xfer_softc(xfer);
779         struct usb_page_cache *pc;
780         uint32_t actlen;
781
782         switch (USB_GET_STATE(xfer)) {
783         case USB_ST_SETUP:
784         case USB_ST_TRANSFERRED:
785 tr_setup:
786                 pc = usbd_xfer_get_frame(xfer, 0);
787                 if (ucom_get_data(&sc->sc_ucom, pc, 0,
788                     UFOMA_BULK_BUF_SIZE, &actlen)) {
789                         usbd_xfer_set_frame_len(xfer, 0, actlen);
790                         usbd_transfer_submit(xfer);
791                 }
792                 return;
793
794         default:                        /* Error */
795                 if (error != USB_ERR_CANCELLED) {
796                         /* try to clear stall first */
797                         usbd_xfer_set_stall(xfer);
798                         goto tr_setup;
799                 }
800                 return;
801         }
802 }
803
804 static void
805 ufoma_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
806 {
807         struct ufoma_softc *sc = usbd_xfer_softc(xfer);
808         struct usb_page_cache *pc;
809         int actlen;
810
811         usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
812
813         switch (USB_GET_STATE(xfer)) {
814         case USB_ST_TRANSFERRED:
815                 pc = usbd_xfer_get_frame(xfer, 0);
816                 ucom_put_data(&sc->sc_ucom, pc, 0, actlen);
817
818         case USB_ST_SETUP:
819 tr_setup:
820                 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
821                 usbd_transfer_submit(xfer);
822                 return;
823
824         default:                        /* Error */
825                 if (error != USB_ERR_CANCELLED) {
826                         /* try to clear stall first */
827                         usbd_xfer_set_stall(xfer);
828                         goto tr_setup;
829                 }
830                 return;
831         }
832 }
833
834 static void
835 ufoma_cfg_open(struct ucom_softc *ucom)
836 {
837         struct ufoma_softc *sc = ucom->sc_parent;
838
839         /* empty input queue */
840
841         if (sc->sc_num_msg != 0xFF) {
842                 sc->sc_num_msg++;
843         }
844         if (sc->sc_currentmode == UMCPC_ACM_MODE_UNLINKED) {
845                 ufoma_cfg_link_state(sc);
846         }
847         if (sc->sc_currentmode == UMCPC_ACM_MODE_DEACTIVATED) {
848                 ufoma_cfg_activate_state(sc, sc->sc_modetoactivate);
849         }
850 }
851
852 static void
853 ufoma_cfg_close(struct ucom_softc *ucom)
854 {
855         struct ufoma_softc *sc = ucom->sc_parent;
856
857         ufoma_cfg_activate_state(sc, UMCPC_ACM_MODE_DEACTIVATED);
858 }
859
860 static void
861 ufoma_cfg_set_break(struct ucom_softc *ucom, uint8_t onoff)
862 {
863         struct ufoma_softc *sc = ucom->sc_parent;
864         struct usb_device_request req;
865         uint16_t wValue;
866
867         if (sc->sc_nobulk ||
868             (sc->sc_currentmode == UMCPC_ACM_MODE_OBEX)) {
869                 return;
870         }
871         if (!(sc->sc_acm_cap & USB_CDC_ACM_HAS_BREAK)) {
872                 return;
873         }
874         wValue = onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF;
875
876         req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
877         req.bRequest = UCDC_SEND_BREAK;
878         USETW(req.wValue, wValue);
879         req.wIndex[0] = sc->sc_ctrl_iface_no;
880         req.wIndex[1] = 0;
881         USETW(req.wLength, 0);
882
883         ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 
884             &req, NULL, 0, 1000);
885 }
886
887 static void
888 ufoma_cfg_get_status(struct ucom_softc *ucom, uint8_t *lsr, uint8_t *msr)
889 {
890         struct ufoma_softc *sc = ucom->sc_parent;
891
892         *lsr = sc->sc_lsr;
893         *msr = sc->sc_msr;
894 }
895
896 static void
897 ufoma_cfg_set_line_state(struct ufoma_softc *sc)
898 {
899         struct usb_device_request req;
900
901         /* Don't send line state emulation request for OBEX port */
902         if (sc->sc_currentmode == UMCPC_ACM_MODE_OBEX) {
903                 return;
904         }
905         req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
906         req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
907         USETW(req.wValue, sc->sc_line);
908         req.wIndex[0] = sc->sc_ctrl_iface_no;
909         req.wIndex[1] = 0;
910         USETW(req.wLength, 0);
911
912         ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 
913             &req, NULL, 0, 1000);
914 }
915
916 static void
917 ufoma_cfg_set_dtr(struct ucom_softc *ucom, uint8_t onoff)
918 {
919         struct ufoma_softc *sc = ucom->sc_parent;
920
921         if (sc->sc_nobulk) {
922                 return;
923         }
924         if (onoff)
925                 sc->sc_line |= UCDC_LINE_DTR;
926         else
927                 sc->sc_line &= ~UCDC_LINE_DTR;
928
929         ufoma_cfg_set_line_state(sc);
930 }
931
932 static void
933 ufoma_cfg_set_rts(struct ucom_softc *ucom, uint8_t onoff)
934 {
935         struct ufoma_softc *sc = ucom->sc_parent;
936
937         if (sc->sc_nobulk) {
938                 return;
939         }
940         if (onoff)
941                 sc->sc_line |= UCDC_LINE_RTS;
942         else
943                 sc->sc_line &= ~UCDC_LINE_RTS;
944
945         ufoma_cfg_set_line_state(sc);
946 }
947
948 static int
949 ufoma_pre_param(struct ucom_softc *ucom, struct termios *t)
950 {
951         return (0);                     /* we accept anything */
952 }
953
954 static void
955 ufoma_cfg_param(struct ucom_softc *ucom, struct termios *t)
956 {
957         struct ufoma_softc *sc = ucom->sc_parent;
958         struct usb_device_request req;
959         struct usb_cdc_line_state ls;
960
961         if (sc->sc_nobulk ||
962             (sc->sc_currentmode == UMCPC_ACM_MODE_OBEX)) {
963                 return;
964         }
965         DPRINTF("\n");
966
967         memset(&ls, 0, sizeof(ls));
968
969         USETDW(ls.dwDTERate, t->c_ospeed);
970
971         if (t->c_cflag & CSTOPB) {
972                 ls.bCharFormat = UCDC_STOP_BIT_2;
973         } else {
974                 ls.bCharFormat = UCDC_STOP_BIT_1;
975         }
976
977         if (t->c_cflag & PARENB) {
978                 if (t->c_cflag & PARODD) {
979                         ls.bParityType = UCDC_PARITY_ODD;
980                 } else {
981                         ls.bParityType = UCDC_PARITY_EVEN;
982                 }
983         } else {
984                 ls.bParityType = UCDC_PARITY_NONE;
985         }
986
987         switch (t->c_cflag & CSIZE) {
988         case CS5:
989                 ls.bDataBits = 5;
990                 break;
991         case CS6:
992                 ls.bDataBits = 6;
993                 break;
994         case CS7:
995                 ls.bDataBits = 7;
996                 break;
997         case CS8:
998                 ls.bDataBits = 8;
999                 break;
1000         }
1001
1002         req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
1003         req.bRequest = UCDC_SET_LINE_CODING;
1004         USETW(req.wValue, 0);
1005         req.wIndex[0] = sc->sc_ctrl_iface_no;
1006         req.wIndex[1] = 0;
1007         USETW(req.wLength, UCDC_LINE_STATE_LENGTH);
1008
1009         ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 
1010             &req, &ls, 0, 1000);
1011 }
1012
1013 static int
1014 ufoma_modem_setup(device_t dev, struct ufoma_softc *sc,
1015     struct usb_attach_arg *uaa)
1016 {
1017         struct usb_config_descriptor *cd;
1018         struct usb_cdc_acm_descriptor *acm;
1019         struct usb_cdc_cm_descriptor *cmd;
1020         struct usb_interface_descriptor *id;
1021         struct usb_interface *iface;
1022         uint8_t i;
1023         int32_t error;
1024
1025         cd = usbd_get_config_descriptor(uaa->device);
1026         id = usbd_get_interface_descriptor(uaa->iface);
1027
1028         cmd = ufoma_get_intconf(cd, id, UDESC_CS_INTERFACE, UDESCSUB_CDC_CM);
1029
1030         if ((cmd == NULL) ||
1031             (cmd->bLength < sizeof(*cmd))) {
1032                 return (EINVAL);
1033         }
1034         sc->sc_cm_cap = cmd->bmCapabilities;
1035         sc->sc_data_iface_no = cmd->bDataInterface;
1036
1037         acm = ufoma_get_intconf(cd, id, UDESC_CS_INTERFACE, UDESCSUB_CDC_ACM);
1038
1039         if ((acm == NULL) ||
1040             (acm->bLength < sizeof(*acm))) {
1041                 return (EINVAL);
1042         }
1043         sc->sc_acm_cap = acm->bmCapabilities;
1044
1045         device_printf(dev, "data interface %d, has %sCM over data, "
1046             "has %sbreak\n",
1047             sc->sc_data_iface_no,
1048             sc->sc_cm_cap & USB_CDC_CM_OVER_DATA ? "" : "no ",
1049             sc->sc_acm_cap & USB_CDC_ACM_HAS_BREAK ? "" : "no ");
1050
1051         /* get the data interface too */
1052
1053         for (i = 0;; i++) {
1054
1055                 iface = usbd_get_iface(uaa->device, i);
1056
1057                 if (iface) {
1058
1059                         id = usbd_get_interface_descriptor(iface);
1060
1061                         if (id && (id->bInterfaceNumber == sc->sc_data_iface_no)) {
1062                                 sc->sc_data_iface_index = i;
1063                                 usbd_set_parent_iface(uaa->device, i, uaa->info.bIfaceIndex);
1064                                 break;
1065                         }
1066                 } else {
1067                         device_printf(dev, "no data interface\n");
1068                         return (EINVAL);
1069                 }
1070         }
1071
1072         error = usbd_transfer_setup(uaa->device,
1073             &sc->sc_data_iface_index, sc->sc_bulk_xfer,
1074             ufoma_bulk_config, UFOMA_BULK_ENDPT_MAX, sc, &sc->sc_lock);
1075
1076         if (error) {
1077                 device_printf(dev, "allocating BULK USB "
1078                     "transfers failed\n");
1079                 return (EINVAL);
1080         }
1081         return (0);
1082 }
1083
1084 static void
1085 ufoma_start_read(struct ucom_softc *ucom)
1086 {
1087         struct ufoma_softc *sc = ucom->sc_parent;
1088
1089         /* start interrupt transfer */
1090         usbd_transfer_start(sc->sc_ctrl_xfer[UFOMA_CTRL_ENDPT_INTR]);
1091
1092         /* start data transfer */
1093         if (sc->sc_nobulk) {
1094                 usbd_transfer_start(sc->sc_ctrl_xfer[UFOMA_CTRL_ENDPT_READ]);
1095         } else {
1096                 usbd_transfer_start(sc->sc_bulk_xfer[UFOMA_BULK_ENDPT_READ]);
1097         }
1098 }
1099
1100 static void
1101 ufoma_stop_read(struct ucom_softc *ucom)
1102 {
1103         struct ufoma_softc *sc = ucom->sc_parent;
1104
1105         /* stop interrupt transfer */
1106         usbd_transfer_stop(sc->sc_ctrl_xfer[UFOMA_CTRL_ENDPT_INTR]);
1107
1108         /* stop data transfer */
1109         if (sc->sc_nobulk) {
1110                 usbd_transfer_stop(sc->sc_ctrl_xfer[UFOMA_CTRL_ENDPT_READ]);
1111         } else {
1112                 usbd_transfer_stop(sc->sc_bulk_xfer[UFOMA_BULK_ENDPT_READ]);
1113         }
1114 }
1115
1116 static void
1117 ufoma_start_write(struct ucom_softc *ucom)
1118 {
1119         struct ufoma_softc *sc = ucom->sc_parent;
1120
1121         if (sc->sc_nobulk) {
1122                 usbd_transfer_start(sc->sc_ctrl_xfer[UFOMA_CTRL_ENDPT_WRITE]);
1123         } else {
1124                 usbd_transfer_start(sc->sc_bulk_xfer[UFOMA_BULK_ENDPT_WRITE]);
1125         }
1126 }
1127
1128 static void
1129 ufoma_stop_write(struct ucom_softc *ucom)
1130 {
1131         struct ufoma_softc *sc = ucom->sc_parent;
1132
1133         if (sc->sc_nobulk) {
1134                 usbd_transfer_stop(sc->sc_ctrl_xfer[UFOMA_CTRL_ENDPT_WRITE]);
1135         } else {
1136                 usbd_transfer_stop(sc->sc_bulk_xfer[UFOMA_BULK_ENDPT_WRITE]);
1137         }
1138 }
1139
1140 static struct umcpc_modetostr_tab{
1141         int mode;
1142         char *str;
1143 }umcpc_modetostr_tab[]={
1144         {UMCPC_ACM_MODE_DEACTIVATED, "deactivated"},
1145         {UMCPC_ACM_MODE_MODEM, "modem"},
1146         {UMCPC_ACM_MODE_ATCOMMAND, "handsfree"},
1147         {UMCPC_ACM_MODE_OBEX, "obex"},
1148         {UMCPC_ACM_MODE_VENDOR1, "vendor1"},
1149         {UMCPC_ACM_MODE_VENDOR2, "vendor2"},
1150         {UMCPC_ACM_MODE_UNLINKED, "unlinked"},
1151         {0, NULL}
1152 };
1153
1154 static char *ufoma_mode_to_str(int mode)
1155 {
1156         int i;
1157         for(i = 0 ;umcpc_modetostr_tab[i].str != NULL; i++){
1158                 if(umcpc_modetostr_tab[i].mode == mode){
1159                         return umcpc_modetostr_tab[i].str;
1160                 }
1161         }
1162         return NULL;
1163 }
1164
1165 static int ufoma_str_to_mode(char *str)
1166 {
1167         int i;
1168         for(i = 0 ;umcpc_modetostr_tab[i].str != NULL; i++){
1169                 if(strcmp(str, umcpc_modetostr_tab[i].str)==0){
1170                         return umcpc_modetostr_tab[i].mode;
1171                 }
1172         }
1173         return -1;
1174 }
1175
1176 static int ufoma_sysctl_support(SYSCTL_HANDLER_ARGS)
1177 {
1178         struct ufoma_softc *sc = (struct ufoma_softc *)oidp->oid_arg1;
1179         struct sbuf sb;
1180         int i;
1181         char *mode;
1182
1183         sbuf_new(&sb, NULL, 1, SBUF_AUTOEXTEND);
1184         for(i = 1; i < sc->sc_modetable[0]; i++){
1185                 mode = ufoma_mode_to_str(sc->sc_modetable[i]);
1186                 if(mode !=NULL){
1187                         sbuf_cat(&sb, mode);
1188                 }else{
1189                         sbuf_printf(&sb, "(%02x)", sc->sc_modetable[i]);
1190                 }
1191                 if(i < (sc->sc_modetable[0]-1))
1192                         sbuf_cat(&sb, ",");
1193         }
1194         sbuf_trim(&sb);
1195         sbuf_finish(&sb);
1196         sysctl_handle_string(oidp, sbuf_data(&sb), sbuf_len(&sb), req);
1197         sbuf_delete(&sb);
1198         
1199         return 0;
1200 }
1201 static int ufoma_sysctl_current(SYSCTL_HANDLER_ARGS)
1202 {
1203         struct ufoma_softc *sc = (struct ufoma_softc *)oidp->oid_arg1;
1204         char *mode;
1205         char subbuf[]="(XXX)";
1206         mode = ufoma_mode_to_str(sc->sc_currentmode);
1207         if(!mode){
1208                 mode = subbuf;
1209                 ksnprintf(subbuf, sizeof(subbuf), "(%02x)", sc->sc_currentmode);
1210         }
1211         sysctl_handle_string(oidp, mode, strlen(mode), req);
1212         
1213         return 0;
1214         
1215 }
1216 static int ufoma_sysctl_open(SYSCTL_HANDLER_ARGS)
1217 {
1218         struct ufoma_softc *sc = (struct ufoma_softc *)oidp->oid_arg1;
1219         char *mode;
1220         char subbuf[40];
1221         int newmode;
1222         int error;
1223         int i;
1224
1225         mode = ufoma_mode_to_str(sc->sc_modetoactivate);
1226         if(mode){
1227                 strncpy(subbuf, mode, sizeof(subbuf));
1228         }else{
1229                 ksnprintf(subbuf, sizeof(subbuf), "(%02x)", sc->sc_modetoactivate);
1230         }
1231         error = sysctl_handle_string(oidp, subbuf, sizeof(subbuf), req);
1232         if(error != 0 || req->newptr == NULL){
1233                 return error;
1234         }
1235         
1236         if((newmode = ufoma_str_to_mode(subbuf)) == -1){
1237                 return EINVAL;
1238         }
1239         
1240         for(i = 1 ; i < sc->sc_modetable[0] ; i++){
1241                 if(sc->sc_modetable[i] == newmode){
1242                         sc->sc_modetoactivate = newmode;
1243                         return 0;
1244                 }
1245         }
1246         
1247         return EINVAL;
1248 }
1249
1250 static void
1251 ufoma_poll(struct ucom_softc *ucom)
1252 {
1253         struct ufoma_softc *sc = ucom->sc_parent;
1254         usbd_transfer_poll(sc->sc_ctrl_xfer, UFOMA_CTRL_ENDPT_MAX);
1255         usbd_transfer_poll(sc->sc_bulk_xfer, UFOMA_BULK_ENDPT_MAX);
1256 }