1 /* $NetBSD: umodem.c,v 1.45 2002/09/23 05:51:23 simonb Exp $ */
7 * Copyright (c) 2003, M. Warner Losh <imp@FreeBSD.org>.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * Copyright (c) 1998 The NetBSD Foundation, Inc.
34 * All rights reserved.
36 * This code is derived from software contributed to The NetBSD Foundation
37 * by Lennart Augustsson (lennart@augustsson.net) at
38 * Carlstedt Research & Technology.
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
43 * 1. Redistributions of source code must retain the above copyright
44 * notice, this list of conditions and the following disclaimer.
45 * 2. Redistributions in binary form must reproduce the above copyright
46 * notice, this list of conditions and the following disclaimer in the
47 * documentation and/or other materials provided with the distribution.
49 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
50 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
51 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
52 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
53 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
54 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
55 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
56 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
57 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
58 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
59 * POSSIBILITY OF SUCH DAMAGE.
63 * Comm Class spec: http://www.usb.org/developers/devclass_docs/usbccs10.pdf
64 * http://www.usb.org/developers/devclass_docs/usbcdc11.pdf
65 * http://www.usb.org/developers/devclass_docs/cdc_wmc10.zip
70 * - Add error recovery in various places; the big problem is what
71 * to do in a callback if there is an error.
72 * - Implement a Call Device for modems without multiplexed commands.
76 #include <sys/stdint.h>
77 #include <sys/stddef.h>
78 #include <sys/param.h>
79 #include <sys/queue.h>
80 #include <sys/types.h>
81 #include <sys/systm.h>
82 #include <sys/kernel.h>
84 #include <sys/module.h>
86 #include <sys/mutex.h>
87 #include <sys/condvar.h>
88 #include <sys/sysctl.h>
90 #include <sys/unistd.h>
91 #include <sys/callout.h>
92 #include <sys/malloc.h>
95 #include <dev/usb/usb.h>
96 #include <dev/usb/usbdi.h>
97 #include <dev/usb/usbdi_util.h>
98 #include <dev/usb/usbhid.h>
99 #include <dev/usb/usb_cdc.h>
103 #include <dev/usb/usb_ioctl.h>
105 #define USB_DEBUG_VAR umodem_debug
106 #include <dev/usb/usb_debug.h>
107 #include <dev/usb/usb_process.h>
108 #include <dev/usb/quirk/usb_quirk.h>
110 #include <dev/usb/serial/usb_serial.h>
113 static int umodem_debug = 0;
115 static SYSCTL_NODE(_hw_usb, OID_AUTO, umodem, CTLFLAG_RW, 0, "USB umodem");
116 SYSCTL_INT(_hw_usb_umodem, OID_AUTO, debug, CTLFLAG_RWTUN,
117 &umodem_debug, 0, "Debug level");
120 static const STRUCT_USB_DUAL_ID umodem_dual_devs[] = {
121 /* Generic Modem class match */
122 {USB_IFACE_CLASS(UICLASS_CDC),
123 USB_IFACE_SUBCLASS(UISUBCLASS_ABSTRACT_CONTROL_MODEL),
124 USB_IFACE_PROTOCOL(UIPROTO_CDC_AT)},
125 {USB_IFACE_CLASS(UICLASS_CDC),
126 USB_IFACE_SUBCLASS(UISUBCLASS_ABSTRACT_CONTROL_MODEL),
127 USB_IFACE_PROTOCOL(UIPROTO_CDC_NONE)},
130 static const STRUCT_USB_HOST_ID umodem_host_devs[] = {
131 /* Huawei Modem class match */
132 {USB_VENDOR(USB_VENDOR_HUAWEI),USB_IFACE_CLASS(UICLASS_CDC),
133 USB_IFACE_SUBCLASS(UISUBCLASS_ABSTRACT_CONTROL_MODEL),
134 USB_IFACE_PROTOCOL(0xFF)},
135 /* Kyocera AH-K3001V */
136 {USB_VPI(USB_VENDOR_KYOCERA, USB_PRODUCT_KYOCERA_AHK3001V, 1)},
137 {USB_VPI(USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC5720, 1)},
138 {USB_VPI(USB_VENDOR_CURITEL, USB_PRODUCT_CURITEL_PC5740, 1)},
142 * As speeds for umodem devices increase, these numbers will need to
143 * be increased. They should be good for G3 speeds and below.
145 * TODO: The TTY buffers should be increased!
147 #define UMODEM_BUF_SIZE 1024
157 #define UMODEM_MODVER 1 /* module version */
159 struct umodem_softc {
160 struct ucom_super_softc sc_super_ucom;
161 struct ucom_softc sc_ucom;
163 struct usb_xfer *sc_xfer[UMODEM_N_TRANSFER];
164 struct usb_device *sc_udev;
169 uint8_t sc_lsr; /* local status register */
170 uint8_t sc_msr; /* modem status register */
171 uint8_t sc_ctrl_iface_no;
172 uint8_t sc_data_iface_no;
173 uint8_t sc_iface_index[2];
174 uint8_t sc_cm_over_data;
175 uint8_t sc_cm_cap; /* CM capabilities */
176 uint8_t sc_acm_cap; /* ACM capabilities */
177 uint8_t sc_line_coding[32]; /* used in USB device mode */
178 uint8_t sc_abstract_state[32]; /* used in USB device mode */
181 static device_probe_t umodem_probe;
182 static device_attach_t umodem_attach;
183 static device_detach_t umodem_detach;
184 static usb_handle_request_t umodem_handle_request;
186 static void umodem_free_softc(struct umodem_softc *);
188 static usb_callback_t umodem_intr_read_callback;
189 static usb_callback_t umodem_intr_write_callback;
190 static usb_callback_t umodem_write_callback;
191 static usb_callback_t umodem_read_callback;
193 static void umodem_free(struct ucom_softc *);
194 static void umodem_start_read(struct ucom_softc *);
195 static void umodem_stop_read(struct ucom_softc *);
196 static void umodem_start_write(struct ucom_softc *);
197 static void umodem_stop_write(struct ucom_softc *);
198 static void umodem_get_caps(struct usb_attach_arg *, uint8_t *, uint8_t *);
199 static void umodem_cfg_get_status(struct ucom_softc *, uint8_t *,
201 static int umodem_pre_param(struct ucom_softc *, struct termios *);
202 static void umodem_cfg_param(struct ucom_softc *, struct termios *);
203 static int umodem_ioctl(struct ucom_softc *, uint32_t, caddr_t, int,
205 static void umodem_cfg_set_dtr(struct ucom_softc *, uint8_t);
206 static void umodem_cfg_set_rts(struct ucom_softc *, uint8_t);
207 static void umodem_cfg_set_break(struct ucom_softc *, uint8_t);
208 static void *umodem_get_desc(struct usb_attach_arg *, uint8_t, uint8_t);
209 static usb_error_t umodem_set_comm_feature(struct usb_device *, uint8_t,
211 static void umodem_poll(struct ucom_softc *ucom);
212 static void umodem_find_data_iface(struct usb_attach_arg *uaa,
213 uint8_t, uint8_t *, uint8_t *);
215 static const struct usb_config umodem_config[UMODEM_N_TRANSFER] = {
219 .endpoint = UE_ADDR_ANY,
220 .direction = UE_DIR_TX,
222 .bufsize = UMODEM_BUF_SIZE,
223 .flags = {.pipe_bof = 1,.force_short_xfer = 1,},
224 .callback = &umodem_write_callback,
225 .usb_mode = USB_MODE_DUAL,
230 .endpoint = UE_ADDR_ANY,
231 .direction = UE_DIR_RX,
233 .bufsize = UMODEM_BUF_SIZE,
234 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
235 .callback = &umodem_read_callback,
236 .usb_mode = USB_MODE_DUAL,
240 .type = UE_INTERRUPT,
241 .endpoint = UE_ADDR_ANY,
242 .direction = UE_DIR_TX,
244 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,.no_pipe_ok = 1,},
245 .bufsize = 0, /* use wMaxPacketSize */
246 .callback = &umodem_intr_write_callback,
247 .usb_mode = USB_MODE_DEVICE,
251 .type = UE_INTERRUPT,
252 .endpoint = UE_ADDR_ANY,
253 .direction = UE_DIR_RX,
255 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,.no_pipe_ok = 1,},
256 .bufsize = 0, /* use wMaxPacketSize */
257 .callback = &umodem_intr_read_callback,
258 .usb_mode = USB_MODE_HOST,
262 static const struct ucom_callback umodem_callback = {
263 .ucom_cfg_get_status = &umodem_cfg_get_status,
264 .ucom_cfg_set_dtr = &umodem_cfg_set_dtr,
265 .ucom_cfg_set_rts = &umodem_cfg_set_rts,
266 .ucom_cfg_set_break = &umodem_cfg_set_break,
267 .ucom_cfg_param = &umodem_cfg_param,
268 .ucom_pre_param = &umodem_pre_param,
269 .ucom_ioctl = &umodem_ioctl,
270 .ucom_start_read = &umodem_start_read,
271 .ucom_stop_read = &umodem_stop_read,
272 .ucom_start_write = &umodem_start_write,
273 .ucom_stop_write = &umodem_stop_write,
274 .ucom_poll = &umodem_poll,
275 .ucom_free = &umodem_free,
278 static device_method_t umodem_methods[] = {
280 DEVMETHOD(usb_handle_request, umodem_handle_request),
282 /* Device interface */
283 DEVMETHOD(device_probe, umodem_probe),
284 DEVMETHOD(device_attach, umodem_attach),
285 DEVMETHOD(device_detach, umodem_detach),
289 static devclass_t umodem_devclass;
291 static driver_t umodem_driver = {
293 .methods = umodem_methods,
294 .size = sizeof(struct umodem_softc),
297 DRIVER_MODULE(umodem, uhub, umodem_driver, umodem_devclass, NULL, 0);
298 MODULE_DEPEND(umodem, ucom, 1, 1, 1);
299 MODULE_DEPEND(umodem, usb, 1, 1, 1);
300 MODULE_VERSION(umodem, UMODEM_MODVER);
301 USB_PNP_DUAL_INFO(umodem_dual_devs);
302 USB_PNP_HOST_INFO(umodem_host_devs);
305 umodem_probe(device_t dev)
307 struct usb_attach_arg *uaa = device_get_ivars(dev);
312 error = usbd_lookup_id_by_uaa(umodem_host_devs,
313 sizeof(umodem_host_devs), uaa);
315 error = usbd_lookup_id_by_uaa(umodem_dual_devs,
316 sizeof(umodem_dual_devs), uaa);
320 return (BUS_PROBE_GENERIC);
324 umodem_attach(device_t dev)
326 struct usb_attach_arg *uaa = device_get_ivars(dev);
327 struct umodem_softc *sc = device_get_softc(dev);
328 struct usb_cdc_cm_descriptor *cmd;
329 struct usb_cdc_union_descriptor *cud;
333 device_set_usb_desc(dev);
334 mtx_init(&sc->sc_mtx, "umodem", NULL, MTX_DEF);
335 ucom_ref(&sc->sc_super_ucom);
337 sc->sc_ctrl_iface_no = uaa->info.bIfaceNum;
338 sc->sc_iface_index[1] = uaa->info.bIfaceIndex;
339 sc->sc_udev = uaa->device;
341 umodem_get_caps(uaa, &sc->sc_cm_cap, &sc->sc_acm_cap);
343 /* get the data interface number */
345 cmd = umodem_get_desc(uaa, UDESC_CS_INTERFACE, UDESCSUB_CDC_CM);
347 if ((cmd == NULL) || (cmd->bLength < sizeof(*cmd))) {
349 cud = usbd_find_descriptor(uaa->device, NULL,
350 uaa->info.bIfaceIndex, UDESC_CS_INTERFACE,
351 0xFF, UDESCSUB_CDC_UNION, 0xFF);
353 if ((cud == NULL) || (cud->bLength < sizeof(*cud))) {
354 DPRINTF("Missing descriptor. "
355 "Assuming data interface is next.\n");
356 if (sc->sc_ctrl_iface_no == 0xFF) {
359 uint8_t class_match = 0;
361 /* set default interface number */
362 sc->sc_data_iface_no = 0xFF;
364 /* try to find the data interface backwards */
365 umodem_find_data_iface(uaa,
366 uaa->info.bIfaceIndex - 1,
367 &sc->sc_data_iface_no, &class_match);
369 /* try to find the data interface forwards */
370 umodem_find_data_iface(uaa,
371 uaa->info.bIfaceIndex + 1,
372 &sc->sc_data_iface_no, &class_match);
374 /* check if nothing was found */
375 if (sc->sc_data_iface_no == 0xFF)
379 sc->sc_data_iface_no = cud->bSlaveInterface[0];
382 sc->sc_data_iface_no = cmd->bDataInterface;
385 device_printf(dev, "data interface %d, has %sCM over "
386 "data, has %sbreak\n",
387 sc->sc_data_iface_no,
388 sc->sc_cm_cap & USB_CDC_CM_OVER_DATA ? "" : "no ",
389 sc->sc_acm_cap & USB_CDC_ACM_HAS_BREAK ? "" : "no ");
391 /* get the data interface too */
394 struct usb_interface *iface;
395 struct usb_interface_descriptor *id;
397 iface = usbd_get_iface(uaa->device, i);
401 id = usbd_get_interface_descriptor(iface);
403 if (id && (id->bInterfaceNumber == sc->sc_data_iface_no)) {
404 sc->sc_iface_index[0] = i;
405 usbd_set_parent_iface(uaa->device, i, uaa->info.bIfaceIndex);
409 device_printf(dev, "no data interface\n");
414 if (usb_test_quirk(uaa, UQ_ASSUME_CM_OVER_DATA)) {
415 sc->sc_cm_over_data = 1;
417 if (sc->sc_cm_cap & USB_CDC_CM_OVER_DATA) {
418 if (sc->sc_acm_cap & USB_CDC_ACM_HAS_FEATURE) {
420 error = umodem_set_comm_feature
421 (uaa->device, sc->sc_ctrl_iface_no,
422 UCDC_ABSTRACT_STATE, UCDC_DATA_MULTIPLEXED);
424 /* ignore any errors */
426 sc->sc_cm_over_data = 1;
429 error = usbd_transfer_setup(uaa->device,
430 sc->sc_iface_index, sc->sc_xfer,
431 umodem_config, UMODEM_N_TRANSFER,
434 device_printf(dev, "Can't setup transfer\n");
438 /* clear stall at first run, if USB host mode */
439 if (uaa->usb_mode == USB_MODE_HOST) {
440 mtx_lock(&sc->sc_mtx);
441 usbd_xfer_set_stall(sc->sc_xfer[UMODEM_BULK_WR]);
442 usbd_xfer_set_stall(sc->sc_xfer[UMODEM_BULK_RD]);
443 mtx_unlock(&sc->sc_mtx);
446 error = ucom_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc,
447 &umodem_callback, &sc->sc_mtx);
449 device_printf(dev, "Can't attach com\n");
452 ucom_set_pnpinfo_usb(&sc->sc_super_ucom, dev);
462 umodem_find_data_iface(struct usb_attach_arg *uaa,
463 uint8_t iface_index, uint8_t *p_data_no, uint8_t *p_match_class)
465 struct usb_interface_descriptor *id;
466 struct usb_interface *iface;
468 iface = usbd_get_iface(uaa->device, iface_index);
470 /* check for end of interfaces */
474 id = usbd_get_interface_descriptor(iface);
476 /* check for non-matching interface class */
477 if (id->bInterfaceClass != UICLASS_CDC_DATA ||
478 id->bInterfaceSubClass != UISUBCLASS_DATA) {
479 /* if we got a class match then return */
486 DPRINTFN(11, "Match at index %u\n", iface_index);
488 *p_data_no = id->bInterfaceNumber;
492 umodem_start_read(struct ucom_softc *ucom)
494 struct umodem_softc *sc = ucom->sc_parent;
496 /* start interrupt endpoint, if any */
497 usbd_transfer_start(sc->sc_xfer[UMODEM_INTR_RD]);
499 /* start read endpoint */
500 usbd_transfer_start(sc->sc_xfer[UMODEM_BULK_RD]);
504 umodem_stop_read(struct ucom_softc *ucom)
506 struct umodem_softc *sc = ucom->sc_parent;
508 /* stop interrupt endpoint, if any */
509 usbd_transfer_stop(sc->sc_xfer[UMODEM_INTR_RD]);
511 /* stop read endpoint */
512 usbd_transfer_stop(sc->sc_xfer[UMODEM_BULK_RD]);
516 umodem_start_write(struct ucom_softc *ucom)
518 struct umodem_softc *sc = ucom->sc_parent;
520 usbd_transfer_start(sc->sc_xfer[UMODEM_INTR_WR]);
521 usbd_transfer_start(sc->sc_xfer[UMODEM_BULK_WR]);
525 umodem_stop_write(struct ucom_softc *ucom)
527 struct umodem_softc *sc = ucom->sc_parent;
529 usbd_transfer_stop(sc->sc_xfer[UMODEM_INTR_WR]);
530 usbd_transfer_stop(sc->sc_xfer[UMODEM_BULK_WR]);
534 umodem_get_caps(struct usb_attach_arg *uaa, uint8_t *cm, uint8_t *acm)
536 struct usb_cdc_cm_descriptor *cmd;
537 struct usb_cdc_acm_descriptor *cad;
539 cmd = umodem_get_desc(uaa, UDESC_CS_INTERFACE, UDESCSUB_CDC_CM);
540 if ((cmd == NULL) || (cmd->bLength < sizeof(*cmd))) {
541 DPRINTF("no CM desc (faking one)\n");
542 *cm = USB_CDC_CM_DOES_CM | USB_CDC_CM_OVER_DATA;
544 *cm = cmd->bmCapabilities;
546 cad = umodem_get_desc(uaa, UDESC_CS_INTERFACE, UDESCSUB_CDC_ACM);
547 if ((cad == NULL) || (cad->bLength < sizeof(*cad))) {
548 DPRINTF("no ACM desc\n");
551 *acm = cad->bmCapabilities;
555 umodem_cfg_get_status(struct ucom_softc *ucom, uint8_t *lsr, uint8_t *msr)
557 struct umodem_softc *sc = ucom->sc_parent;
561 /* XXX Note: sc_lsr is always zero */
567 umodem_pre_param(struct ucom_softc *ucom, struct termios *t)
569 return (0); /* we accept anything */
573 umodem_cfg_param(struct ucom_softc *ucom, struct termios *t)
575 struct umodem_softc *sc = ucom->sc_parent;
576 struct usb_cdc_line_state ls;
577 struct usb_device_request req;
579 DPRINTF("sc=%p\n", sc);
581 memset(&ls, 0, sizeof(ls));
583 USETDW(ls.dwDTERate, t->c_ospeed);
585 ls.bCharFormat = (t->c_cflag & CSTOPB) ?
586 UCDC_STOP_BIT_2 : UCDC_STOP_BIT_1;
588 ls.bParityType = (t->c_cflag & PARENB) ?
589 ((t->c_cflag & PARODD) ?
590 UCDC_PARITY_ODD : UCDC_PARITY_EVEN) : UCDC_PARITY_NONE;
592 switch (t->c_cflag & CSIZE) {
607 DPRINTF("rate=%d fmt=%d parity=%d bits=%d\n",
608 UGETDW(ls.dwDTERate), ls.bCharFormat,
609 ls.bParityType, ls.bDataBits);
611 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
612 req.bRequest = UCDC_SET_LINE_CODING;
613 USETW(req.wValue, 0);
614 req.wIndex[0] = sc->sc_ctrl_iface_no;
616 USETW(req.wLength, sizeof(ls));
618 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
623 umodem_ioctl(struct ucom_softc *ucom, uint32_t cmd, caddr_t data,
624 int flag, struct thread *td)
626 struct umodem_softc *sc = ucom->sc_parent;
629 DPRINTF("cmd=0x%08x\n", cmd);
632 case USB_GET_CM_OVER_DATA:
633 *(int *)data = sc->sc_cm_over_data;
636 case USB_SET_CM_OVER_DATA:
637 if (*(int *)data != sc->sc_cm_over_data) {
643 DPRINTF("unknown\n");
652 umodem_cfg_set_dtr(struct ucom_softc *ucom, uint8_t onoff)
654 struct umodem_softc *sc = ucom->sc_parent;
655 struct usb_device_request req;
657 DPRINTF("onoff=%d\n", onoff);
660 sc->sc_line |= UCDC_LINE_DTR;
662 sc->sc_line &= ~UCDC_LINE_DTR;
664 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
665 req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
666 USETW(req.wValue, sc->sc_line);
667 req.wIndex[0] = sc->sc_ctrl_iface_no;
669 USETW(req.wLength, 0);
671 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
672 &req, NULL, 0, 1000);
676 umodem_cfg_set_rts(struct ucom_softc *ucom, uint8_t onoff)
678 struct umodem_softc *sc = ucom->sc_parent;
679 struct usb_device_request req;
681 DPRINTF("onoff=%d\n", onoff);
684 sc->sc_line |= UCDC_LINE_RTS;
686 sc->sc_line &= ~UCDC_LINE_RTS;
688 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
689 req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
690 USETW(req.wValue, sc->sc_line);
691 req.wIndex[0] = sc->sc_ctrl_iface_no;
693 USETW(req.wLength, 0);
695 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
696 &req, NULL, 0, 1000);
700 umodem_cfg_set_break(struct ucom_softc *ucom, uint8_t onoff)
702 struct umodem_softc *sc = ucom->sc_parent;
703 struct usb_device_request req;
706 DPRINTF("onoff=%d\n", onoff);
708 if (sc->sc_acm_cap & USB_CDC_ACM_HAS_BREAK) {
710 temp = onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF;
712 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
713 req.bRequest = UCDC_SEND_BREAK;
714 USETW(req.wValue, temp);
715 req.wIndex[0] = sc->sc_ctrl_iface_no;
717 USETW(req.wLength, 0);
719 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
720 &req, NULL, 0, 1000);
725 umodem_intr_write_callback(struct usb_xfer *xfer, usb_error_t error)
729 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
731 switch (USB_GET_STATE(xfer)) {
732 case USB_ST_TRANSFERRED:
734 DPRINTF("Transferred %d bytes\n", actlen);
742 if (error != USB_ERR_CANCELLED) {
743 /* start clear stall */
744 usbd_xfer_set_stall(xfer);
752 umodem_intr_read_callback(struct usb_xfer *xfer, usb_error_t error)
754 struct usb_cdc_notification pkt;
755 struct umodem_softc *sc = usbd_xfer_softc(xfer);
756 struct usb_page_cache *pc;
760 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
762 switch (USB_GET_STATE(xfer)) {
763 case USB_ST_TRANSFERRED:
766 DPRINTF("received short packet, "
767 "%d bytes\n", actlen);
770 if (actlen > (int)sizeof(pkt)) {
771 DPRINTF("truncating message\n");
772 actlen = sizeof(pkt);
774 pc = usbd_xfer_get_frame(xfer, 0);
775 usbd_copy_out(pc, 0, &pkt, actlen);
779 wLen = UGETW(pkt.wLength);
783 if (pkt.bmRequestType != UCDC_NOTIFICATION) {
784 DPRINTF("unknown message type, "
785 "0x%02x, on notify pipe!\n",
789 switch (pkt.bNotification) {
790 case UCDC_N_SERIAL_STATE:
792 * Set the serial state in ucom driver based on
793 * the bits from the notify message
796 DPRINTF("invalid notification "
797 "length, %d bytes!\n", actlen);
800 DPRINTF("notify bytes = %02x%02x\n",
804 /* Currently, lsr is always zero. */
808 if (pkt.data[0] & UCDC_N_SERIAL_RI) {
809 sc->sc_msr |= SER_RI;
811 if (pkt.data[0] & UCDC_N_SERIAL_DSR) {
812 sc->sc_msr |= SER_DSR;
814 if (pkt.data[0] & UCDC_N_SERIAL_DCD) {
815 sc->sc_msr |= SER_DCD;
817 ucom_status_change(&sc->sc_ucom);
821 DPRINTF("unknown notify message: 0x%02x\n",
828 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
829 usbd_transfer_submit(xfer);
833 if (error != USB_ERR_CANCELLED) {
834 /* try to clear stall first */
835 usbd_xfer_set_stall(xfer);
844 umodem_write_callback(struct usb_xfer *xfer, usb_error_t error)
846 struct umodem_softc *sc = usbd_xfer_softc(xfer);
847 struct usb_page_cache *pc;
850 switch (USB_GET_STATE(xfer)) {
852 case USB_ST_TRANSFERRED:
854 pc = usbd_xfer_get_frame(xfer, 0);
855 if (ucom_get_data(&sc->sc_ucom, pc, 0,
856 UMODEM_BUF_SIZE, &actlen)) {
858 usbd_xfer_set_frame_len(xfer, 0, actlen);
859 usbd_transfer_submit(xfer);
864 if (error != USB_ERR_CANCELLED) {
865 /* try to clear stall first */
866 usbd_xfer_set_stall(xfer);
874 umodem_read_callback(struct usb_xfer *xfer, usb_error_t error)
876 struct umodem_softc *sc = usbd_xfer_softc(xfer);
877 struct usb_page_cache *pc;
880 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
882 switch (USB_GET_STATE(xfer)) {
883 case USB_ST_TRANSFERRED:
885 DPRINTF("actlen=%d\n", actlen);
887 pc = usbd_xfer_get_frame(xfer, 0);
888 ucom_put_data(&sc->sc_ucom, pc, 0, actlen);
892 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
893 usbd_transfer_submit(xfer);
897 if (error != USB_ERR_CANCELLED) {
898 /* try to clear stall first */
899 usbd_xfer_set_stall(xfer);
907 umodem_get_desc(struct usb_attach_arg *uaa, uint8_t type, uint8_t subtype)
909 return (usbd_find_descriptor(uaa->device, NULL, uaa->info.bIfaceIndex,
910 type, 0xFF, subtype, 0xFF));
914 umodem_set_comm_feature(struct usb_device *udev, uint8_t iface_no,
915 uint16_t feature, uint16_t state)
917 struct usb_device_request req;
918 struct usb_cdc_abstract_state ast;
920 DPRINTF("feature=%d state=%d\n",
923 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
924 req.bRequest = UCDC_SET_COMM_FEATURE;
925 USETW(req.wValue, feature);
926 req.wIndex[0] = iface_no;
928 USETW(req.wLength, UCDC_ABSTRACT_STATE_LENGTH);
929 USETW(ast.wState, state);
931 return (usbd_do_request(udev, NULL, &req, &ast));
935 umodem_detach(device_t dev)
937 struct umodem_softc *sc = device_get_softc(dev);
939 DPRINTF("sc=%p\n", sc);
941 ucom_detach(&sc->sc_super_ucom, &sc->sc_ucom);
942 usbd_transfer_unsetup(sc->sc_xfer, UMODEM_N_TRANSFER);
944 device_claim_softc(dev);
946 umodem_free_softc(sc);
951 UCOM_UNLOAD_DRAIN(umodem);
954 umodem_free_softc(struct umodem_softc *sc)
956 if (ucom_unref(&sc->sc_super_ucom)) {
957 mtx_destroy(&sc->sc_mtx);
958 device_free_softc(sc);
963 umodem_free(struct ucom_softc *ucom)
965 umodem_free_softc(ucom->sc_parent);
969 umodem_poll(struct ucom_softc *ucom)
971 struct umodem_softc *sc = ucom->sc_parent;
972 usbd_transfer_poll(sc->sc_xfer, UMODEM_N_TRANSFER);
976 umodem_handle_request(device_t dev,
977 const void *preq, void **pptr, uint16_t *plen,
978 uint16_t offset, uint8_t *pstate)
980 struct umodem_softc *sc = device_get_softc(dev);
981 const struct usb_device_request *req = preq;
982 uint8_t is_complete = *pstate;
984 DPRINTF("sc=%p\n", sc);
987 if ((req->bmRequestType == UT_WRITE_CLASS_INTERFACE) &&
988 (req->bRequest == UCDC_SET_LINE_CODING) &&
989 (req->wIndex[0] == sc->sc_ctrl_iface_no) &&
990 (req->wIndex[1] == 0x00) &&
991 (req->wValue[0] == 0x00) &&
992 (req->wValue[1] == 0x00)) {
994 *plen = sizeof(sc->sc_line_coding);
995 *pptr = &sc->sc_line_coding;
1000 } else if ((req->bmRequestType == UT_WRITE_CLASS_INTERFACE) &&
1001 (req->wIndex[0] == sc->sc_ctrl_iface_no) &&
1002 (req->wIndex[1] == 0x00) &&
1003 (req->bRequest == UCDC_SET_COMM_FEATURE)) {
1005 *plen = sizeof(sc->sc_abstract_state);
1006 *pptr = &sc->sc_abstract_state;
1011 } else if ((req->bmRequestType == UT_WRITE_CLASS_INTERFACE) &&
1012 (req->wIndex[0] == sc->sc_ctrl_iface_no) &&
1013 (req->wIndex[1] == 0x00) &&
1014 (req->bRequest == UCDC_SET_CONTROL_LINE_STATE)) {
1017 } else if ((req->bmRequestType == UT_WRITE_CLASS_INTERFACE) &&
1018 (req->wIndex[0] == sc->sc_ctrl_iface_no) &&
1019 (req->wIndex[1] == 0x00) &&
1020 (req->bRequest == UCDC_SEND_BREAK)) {
1025 return (ENXIO); /* use builtin handler */