1 /* $NetBSD: umodem.c,v 1.45 2002/09/23 05:51:23 simonb Exp $ */
3 #define UFOMA_HANDSFREE
5 * Copyright (c) 2005, Takanori Watanabe
6 * Copyright (c) 2003, M. Warner Losh <imp@FreeBSD.org>.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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
32 * Copyright (c) 1998 The NetBSD Foundation, Inc.
33 * All rights reserved.
35 * This code is derived from software contributed to The NetBSD Foundation
36 * by Lennart Augustsson (lennart@augustsson.net) at
37 * Carlstedt Research & Technology.
39 * Redistribution and use in source and binary forms, with or without
40 * modification, are permitted provided that the following conditions
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.
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.
69 * Comm Class spec: http://www.usb.org/developers/devclass_docs/usbccs10.pdf
70 * http://www.usb.org/developers/devclass_docs/usbcdc11.pdf
75 * - Implement a Call Device for modems without multiplexed commands.
79 * NOTE: all function names beginning like "ufoma_cfg_" can only
80 * be called from within the config thread function !
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>
90 #include <sys/module.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>
99 #include <sys/serial.h>
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>
107 #define USB_DEBUG_VAR usb_debug
108 #include <bus/u4b/usb_debug.h>
109 #include <bus/u4b/usb_process.h>
111 #include <bus/u4b/serial/usb_serial.h>
113 typedef struct ufoma_mobile_acm_descriptor {
114 uint8_t bFunctionLength;
115 uint8_t bDescriptorType;
116 uint8_t bDescriptorSubtype;
119 } __packed usb_mcpc_acm_descriptor;
121 #define UISUBCLASS_MCPC 0x88
123 #define UDESC_VS_INTERFACE 0x44
124 #define UDESCSUB_MCPC_ACM 0x11
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
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
139 #define UMCPC_CM_MOBILE_ACM 0x0
141 #define UMCPC_ACTIVATE_MODE 0x60
142 #define UMCPC_GET_MODETABLE 0x61
143 #define UMCPC_SET_LINK 0x62
144 #define UMCPC_CLEAR_LINK 0x63
146 #define UMCPC_REQUEST_ACKNOWLEDGE 0x31
148 #define UFOMA_MAX_TIMEOUT 15 /* standard says 10 seconds */
149 #define UFOMA_CMD_BUF_SIZE 64 /* bytes */
151 #define UFOMA_BULK_BUF_SIZE 1024 /* bytes */
154 UFOMA_CTRL_ENDPT_INTR,
155 UFOMA_CTRL_ENDPT_READ,
156 UFOMA_CTRL_ENDPT_WRITE,
157 UFOMA_CTRL_ENDPT_MAX,
161 UFOMA_BULK_ENDPT_WRITE,
162 UFOMA_BULK_ENDPT_READ,
163 UFOMA_BULK_ENDPT_MAX,
167 struct ucom_super_softc sc_super_ucom;
168 struct ucom_softc sc_ucom;
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;
176 struct usb_device *sc_udev;
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;
192 uint8_t sc_modetoactivate;
193 uint8_t sc_currentmode;
199 static device_probe_t ufoma_probe;
200 static device_attach_t ufoma_attach;
201 static device_detach_t ufoma_detach;
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;
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 *,
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);
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);
235 static const struct usb_config
236 ufoma_ctrl_config[UFOMA_CTRL_ENDPT_MAX] = {
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,
247 [UFOMA_CTRL_ENDPT_READ] = {
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 */
257 [UFOMA_CTRL_ENDPT_WRITE] = {
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 */
267 static const struct usb_config
268 ufoma_bulk_config[UFOMA_BULK_ENDPT_MAX] = {
270 [UFOMA_BULK_ENDPT_WRITE] = {
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,
279 [UFOMA_BULK_ENDPT_READ] = {
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,
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,
305 static device_method_t ufoma_methods[] = {
307 DEVMETHOD(device_probe, ufoma_probe),
308 DEVMETHOD(device_attach, ufoma_attach),
309 DEVMETHOD(device_detach, ufoma_detach),
313 static devclass_t ufoma_devclass;
315 static driver_t ufoma_driver = {
317 .methods = ufoma_methods,
318 .size = sizeof(struct ufoma_softc),
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);
326 static const STRUCT_USB_HOST_ID ufoma_devs[] = {
327 {USB_IFACE_CLASS(UICLASS_CDC),
328 USB_IFACE_SUBCLASS(UISUBCLASS_MCPC),},
332 ufoma_probe(device_t dev)
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;
340 if (uaa->usb_mode != USB_MODE_HOST)
343 error = usbd_lookup_id_by_uaa(ufoma_devs, sizeof(ufoma_devs), uaa);
347 id = usbd_get_interface_descriptor(uaa->iface);
348 cd = usbd_get_config_descriptor(uaa->device);
350 if (id == NULL || cd == NULL)
353 mad = ufoma_get_intconf(cd, id, UDESC_VS_INTERFACE, UDESCSUB_MCPC_ACM);
357 #ifndef UFOMA_HANDSFREE
358 if ((mad->bType == UMCPC_ACM_TYPE_AB5) ||
359 (mad->bType == UMCPC_ACM_TYPE_AB6))
362 return (BUS_PROBE_GENERIC);
366 ufoma_attach(device_t dev)
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;
375 usb_mcpc_acm_descriptor *mad;
379 sc->sc_udev = uaa->device;
381 sc->sc_unit = device_get_unit(dev);
383 lockinit(&sc->sc_lock, "ufoma", 0, LK_CANRECURSE);
384 cv_init(&sc->sc_cv, "CWAIT");
386 device_set_usb_desc(dev);
388 ksnprintf(sc->sc_name, sizeof(sc->sc_name),
389 "%s", device_get_nameunit(dev));
393 /* setup control transfers */
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;
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);
405 device_printf(dev, "allocating control USB "
406 "transfers failed\n");
409 mad = ufoma_get_intconf(cd, id, UDESC_VS_INTERFACE, UDESCSUB_MCPC_ACM);
413 if (mad->bFunctionLength < sizeof(*mad)) {
414 device_printf(dev, "invalid MAD descriptor\n");
417 if ((mad->bType == UMCPC_ACM_TYPE_AB5) ||
418 (mad->bType == UMCPC_ACM_TYPE_AB6)) {
422 if (ufoma_modem_setup(dev, sc, uaa)) {
427 elements = (mad->bFunctionLength - sizeof(*mad) + 1);
429 /* initialize mode variables */
431 sc->sc_modetable = kmalloc(elements + 1, M_USBDEV, M_WAITOK);
433 if (sc->sc_modetable == NULL) {
436 sc->sc_modetable[0] = (elements + 1);
437 memcpy(&sc->sc_modetable[1], mad->bMode, elements);
439 sc->sc_currentmode = UMCPC_ACM_MODE_UNLINKED;
440 sc->sc_modetoactivate = mad->bMode[0];
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);
448 error = ucom_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc,
449 &ufoma_callback, &sc->sc_lock);
451 DPRINTF("ucom_attach failed\n");
454 ucom_set_pnpinfo_usb(&sc->sc_super_ucom, dev);
457 sctx = device_get_sysctl_ctx(dev);
458 soid = device_get_sysctl_tree(dev);
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");
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");
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");
475 return (0); /* success */
479 return (ENXIO); /* failure */
483 ufoma_detach(device_t dev)
485 struct ufoma_softc *sc = device_get_softc(dev);
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);
491 if (sc->sc_modetable) {
492 kfree(sc->sc_modetable, M_USBDEV);
494 lockuninit(&sc->sc_lock);
495 cv_destroy(&sc->sc_cv);
501 ufoma_get_intconf(struct usb_config_descriptor *cd, struct usb_interface_descriptor *id,
502 uint8_t type, uint8_t subtype)
504 struct usb_descriptor *desc = (void *)id;
506 while ((desc = usb_desc_foreach(cd, desc))) {
508 if (desc->bDescriptorType == UDESC_INTERFACE) {
511 if ((desc->bDescriptorType == type) &&
512 (desc->bDescriptorSubtype == subtype)) {
520 ufoma_cfg_link_state(struct ufoma_softc *sc)
522 struct usb_device_request req;
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]);
531 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
532 &req, sc->sc_modetable, 0, 1000);
534 error = cv_timedwait(&sc->sc_cv, &sc->sc_lock, hz);
537 DPRINTF("NO response\n");
542 ufoma_cfg_activate_state(struct ufoma_softc *sc, uint16_t state)
544 struct usb_device_request req;
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);
553 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
554 &req, NULL, 0, 1000);
556 error = cv_timedwait(&sc->sc_cv, &sc->sc_lock,
557 (UFOMA_MAX_TIMEOUT * hz));
559 DPRINTF("No response\n");
564 ufoma_ctrl_read_callback(struct usb_xfer *xfer, usb_error_t error)
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;
571 usbd_xfer_status(xfer, NULL, NULL, &aframes, &nframes);
573 switch (USB_GET_STATE(xfer)) {
574 case USB_ST_TRANSFERRED:
576 if (aframes != nframes)
578 pc1 = usbd_xfer_get_frame(xfer, 1);
579 len = usbd_xfer_frame_len(xfer, 1);
581 ucom_put_data(&sc->sc_ucom, pc1, 0, len);
585 if (sc->sc_num_msg) {
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);
594 pc0 = usbd_xfer_get_frame(xfer, 0);
595 usbd_copy_in(pc0, 0, &req, sizeof(req));
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);
605 DPRINTF("error = %s\n",
608 if (error == USB_ERR_CANCELLED) {
619 ufoma_ctrl_write_callback(struct usb_xfer *xfer, usb_error_t error)
621 struct ufoma_softc *sc = usbd_xfer_softc(xfer);
622 struct usb_device_request req;
623 struct usb_page_cache *pc;
626 switch (USB_GET_STATE(xfer)) {
627 case USB_ST_TRANSFERRED:
631 pc = usbd_xfer_get_frame(xfer, 1);
632 if (ucom_get_data(&sc->sc_ucom, pc, 0, 1, &actlen)) {
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);
640 pc = usbd_xfer_get_frame(xfer, 0);
641 usbd_copy_in(pc, 0, &req, sizeof(req));
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);
647 usbd_transfer_submit(xfer);
652 DPRINTF("error = %s\n", usbd_errstr(error));
654 if (error == USB_ERR_CANCELLED) {
665 ufoma_intr_callback(struct usb_xfer *xfer, usb_error_t error)
667 struct ufoma_softc *sc = usbd_xfer_softc(xfer);
668 struct usb_cdc_notification pkt;
669 struct usb_page_cache *pc;
675 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
677 switch (USB_GET_STATE(xfer)) {
678 case USB_ST_TRANSFERRED:
680 DPRINTF("too short message\n");
683 if (actlen > sizeof(pkt)) {
684 DPRINTF("truncating message\n");
685 actlen = sizeof(pkt);
687 pc = usbd_xfer_get_frame(xfer, 0);
688 usbd_copy_out(pc, 0, &pkt, actlen);
692 wLen = UGETW(pkt.wLength);
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");
703 cv_signal(&sc->sc_cv);
705 if (pkt.bmRequestType != UCDC_NOTIFICATION) {
708 switch (pkt.bNotification) {
709 case UCDC_N_RESPONSE_AVAILABLE:
710 if (!(sc->sc_nobulk)) {
711 DPRINTF("Wrong serial state!\n");
714 if (sc->sc_num_msg != 0xFF) {
717 usbd_transfer_start(sc->sc_ctrl_xfer[UFOMA_CTRL_ENDPT_READ]);
720 case UCDC_N_SERIAL_STATE:
722 DPRINTF("Wrong serial state!\n");
726 * Set the serial state in ucom driver based on
727 * the bits from the notify message
730 DPRINTF("invalid notification "
731 "length, %d bytes!\n", actlen);
734 DPRINTF("notify bytes = 0x%02x, 0x%02x\n",
735 pkt.data[0], pkt.data[1]);
737 /* currently, lsr is always zero. */
741 mstatus = pkt.data[0];
743 if (mstatus & UCDC_N_SERIAL_RI) {
744 sc->sc_msr |= SER_RI;
746 if (mstatus & UCDC_N_SERIAL_DSR) {
747 sc->sc_msr |= SER_DSR;
749 if (mstatus & UCDC_N_SERIAL_DCD) {
750 sc->sc_msr |= SER_DCD;
752 ucom_status_change(&sc->sc_ucom);
761 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
762 usbd_transfer_submit(xfer);
766 if (error != USB_ERR_CANCELLED) {
767 /* try to clear stall first */
768 usbd_xfer_set_stall(xfer);
776 ufoma_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
778 struct ufoma_softc *sc = usbd_xfer_softc(xfer);
779 struct usb_page_cache *pc;
782 switch (USB_GET_STATE(xfer)) {
784 case USB_ST_TRANSFERRED:
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);
795 if (error != USB_ERR_CANCELLED) {
796 /* try to clear stall first */
797 usbd_xfer_set_stall(xfer);
805 ufoma_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
807 struct ufoma_softc *sc = usbd_xfer_softc(xfer);
808 struct usb_page_cache *pc;
811 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
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);
820 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
821 usbd_transfer_submit(xfer);
825 if (error != USB_ERR_CANCELLED) {
826 /* try to clear stall first */
827 usbd_xfer_set_stall(xfer);
835 ufoma_cfg_open(struct ucom_softc *ucom)
837 struct ufoma_softc *sc = ucom->sc_parent;
839 /* empty input queue */
841 if (sc->sc_num_msg != 0xFF) {
844 if (sc->sc_currentmode == UMCPC_ACM_MODE_UNLINKED) {
845 ufoma_cfg_link_state(sc);
847 if (sc->sc_currentmode == UMCPC_ACM_MODE_DEACTIVATED) {
848 ufoma_cfg_activate_state(sc, sc->sc_modetoactivate);
853 ufoma_cfg_close(struct ucom_softc *ucom)
855 struct ufoma_softc *sc = ucom->sc_parent;
857 ufoma_cfg_activate_state(sc, UMCPC_ACM_MODE_DEACTIVATED);
861 ufoma_cfg_set_break(struct ucom_softc *ucom, uint8_t onoff)
863 struct ufoma_softc *sc = ucom->sc_parent;
864 struct usb_device_request req;
868 (sc->sc_currentmode == UMCPC_ACM_MODE_OBEX)) {
871 if (!(sc->sc_acm_cap & USB_CDC_ACM_HAS_BREAK)) {
874 wValue = onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF;
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;
881 USETW(req.wLength, 0);
883 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
884 &req, NULL, 0, 1000);
888 ufoma_cfg_get_status(struct ucom_softc *ucom, uint8_t *lsr, uint8_t *msr)
890 struct ufoma_softc *sc = ucom->sc_parent;
897 ufoma_cfg_set_line_state(struct ufoma_softc *sc)
899 struct usb_device_request req;
901 /* Don't send line state emulation request for OBEX port */
902 if (sc->sc_currentmode == UMCPC_ACM_MODE_OBEX) {
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;
910 USETW(req.wLength, 0);
912 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
913 &req, NULL, 0, 1000);
917 ufoma_cfg_set_dtr(struct ucom_softc *ucom, uint8_t onoff)
919 struct ufoma_softc *sc = ucom->sc_parent;
925 sc->sc_line |= UCDC_LINE_DTR;
927 sc->sc_line &= ~UCDC_LINE_DTR;
929 ufoma_cfg_set_line_state(sc);
933 ufoma_cfg_set_rts(struct ucom_softc *ucom, uint8_t onoff)
935 struct ufoma_softc *sc = ucom->sc_parent;
941 sc->sc_line |= UCDC_LINE_RTS;
943 sc->sc_line &= ~UCDC_LINE_RTS;
945 ufoma_cfg_set_line_state(sc);
949 ufoma_pre_param(struct ucom_softc *ucom, struct termios *t)
951 return (0); /* we accept anything */
955 ufoma_cfg_param(struct ucom_softc *ucom, struct termios *t)
957 struct ufoma_softc *sc = ucom->sc_parent;
958 struct usb_device_request req;
959 struct usb_cdc_line_state ls;
962 (sc->sc_currentmode == UMCPC_ACM_MODE_OBEX)) {
967 memset(&ls, 0, sizeof(ls));
969 USETDW(ls.dwDTERate, t->c_ospeed);
971 if (t->c_cflag & CSTOPB) {
972 ls.bCharFormat = UCDC_STOP_BIT_2;
974 ls.bCharFormat = UCDC_STOP_BIT_1;
977 if (t->c_cflag & PARENB) {
978 if (t->c_cflag & PARODD) {
979 ls.bParityType = UCDC_PARITY_ODD;
981 ls.bParityType = UCDC_PARITY_EVEN;
984 ls.bParityType = UCDC_PARITY_NONE;
987 switch (t->c_cflag & CSIZE) {
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;
1007 USETW(req.wLength, UCDC_LINE_STATE_LENGTH);
1009 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
1010 &req, &ls, 0, 1000);
1014 ufoma_modem_setup(device_t dev, struct ufoma_softc *sc,
1015 struct usb_attach_arg *uaa)
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;
1025 cd = usbd_get_config_descriptor(uaa->device);
1026 id = usbd_get_interface_descriptor(uaa->iface);
1028 cmd = ufoma_get_intconf(cd, id, UDESC_CS_INTERFACE, UDESCSUB_CDC_CM);
1030 if ((cmd == NULL) ||
1031 (cmd->bLength < sizeof(*cmd))) {
1034 sc->sc_cm_cap = cmd->bmCapabilities;
1035 sc->sc_data_iface_no = cmd->bDataInterface;
1037 acm = ufoma_get_intconf(cd, id, UDESC_CS_INTERFACE, UDESCSUB_CDC_ACM);
1039 if ((acm == NULL) ||
1040 (acm->bLength < sizeof(*acm))) {
1043 sc->sc_acm_cap = acm->bmCapabilities;
1045 device_printf(dev, "data interface %d, has %sCM over data, "
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 ");
1051 /* get the data interface too */
1055 iface = usbd_get_iface(uaa->device, i);
1059 id = usbd_get_interface_descriptor(iface);
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);
1067 device_printf(dev, "no data interface\n");
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);
1077 device_printf(dev, "allocating BULK USB "
1078 "transfers failed\n");
1085 ufoma_start_read(struct ucom_softc *ucom)
1087 struct ufoma_softc *sc = ucom->sc_parent;
1089 /* start interrupt transfer */
1090 usbd_transfer_start(sc->sc_ctrl_xfer[UFOMA_CTRL_ENDPT_INTR]);
1092 /* start data transfer */
1093 if (sc->sc_nobulk) {
1094 usbd_transfer_start(sc->sc_ctrl_xfer[UFOMA_CTRL_ENDPT_READ]);
1096 usbd_transfer_start(sc->sc_bulk_xfer[UFOMA_BULK_ENDPT_READ]);
1101 ufoma_stop_read(struct ucom_softc *ucom)
1103 struct ufoma_softc *sc = ucom->sc_parent;
1105 /* stop interrupt transfer */
1106 usbd_transfer_stop(sc->sc_ctrl_xfer[UFOMA_CTRL_ENDPT_INTR]);
1108 /* stop data transfer */
1109 if (sc->sc_nobulk) {
1110 usbd_transfer_stop(sc->sc_ctrl_xfer[UFOMA_CTRL_ENDPT_READ]);
1112 usbd_transfer_stop(sc->sc_bulk_xfer[UFOMA_BULK_ENDPT_READ]);
1117 ufoma_start_write(struct ucom_softc *ucom)
1119 struct ufoma_softc *sc = ucom->sc_parent;
1121 if (sc->sc_nobulk) {
1122 usbd_transfer_start(sc->sc_ctrl_xfer[UFOMA_CTRL_ENDPT_WRITE]);
1124 usbd_transfer_start(sc->sc_bulk_xfer[UFOMA_BULK_ENDPT_WRITE]);
1129 ufoma_stop_write(struct ucom_softc *ucom)
1131 struct ufoma_softc *sc = ucom->sc_parent;
1133 if (sc->sc_nobulk) {
1134 usbd_transfer_stop(sc->sc_ctrl_xfer[UFOMA_CTRL_ENDPT_WRITE]);
1136 usbd_transfer_stop(sc->sc_bulk_xfer[UFOMA_BULK_ENDPT_WRITE]);
1140 static struct umcpc_modetostr_tab{
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"},
1154 static char *ufoma_mode_to_str(int mode)
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;
1165 static int ufoma_str_to_mode(char *str)
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;
1176 static int ufoma_sysctl_support(SYSCTL_HANDLER_ARGS)
1178 struct ufoma_softc *sc = (struct ufoma_softc *)oidp->oid_arg1;
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]);
1187 sbuf_cat(&sb, mode);
1189 sbuf_printf(&sb, "(%02x)", sc->sc_modetable[i]);
1191 if(i < (sc->sc_modetable[0]-1))
1196 sysctl_handle_string(oidp, sbuf_data(&sb), sbuf_len(&sb), req);
1201 static int ufoma_sysctl_current(SYSCTL_HANDLER_ARGS)
1203 struct ufoma_softc *sc = (struct ufoma_softc *)oidp->oid_arg1;
1205 char subbuf[]="(XXX)";
1206 mode = ufoma_mode_to_str(sc->sc_currentmode);
1209 ksnprintf(subbuf, sizeof(subbuf), "(%02x)", sc->sc_currentmode);
1211 sysctl_handle_string(oidp, mode, strlen(mode), req);
1216 static int ufoma_sysctl_open(SYSCTL_HANDLER_ARGS)
1218 struct ufoma_softc *sc = (struct ufoma_softc *)oidp->oid_arg1;
1225 mode = ufoma_mode_to_str(sc->sc_modetoactivate);
1227 strncpy(subbuf, mode, sizeof(subbuf));
1229 ksnprintf(subbuf, sizeof(subbuf), "(%02x)", sc->sc_modetoactivate);
1231 error = sysctl_handle_string(oidp, subbuf, sizeof(subbuf), req);
1232 if(error != 0 || req->newptr == NULL){
1236 if((newmode = ufoma_str_to_mode(subbuf)) == -1){
1240 for(i = 1 ; i < sc->sc_modetable[0] ; i++){
1241 if(sc->sc_modetable[i] == newmode){
1242 sc->sc_modetoactivate = newmode;
1251 ufoma_poll(struct ucom_softc *ucom)
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);