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