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