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>
102 #include <dev/usb/usb_ioctl.h>
104 #define USB_DEBUG_VAR umodem_debug
105 #include <dev/usb/usb_debug.h>
106 #include <dev/usb/usb_process.h>
107 #include <dev/usb/quirk/usb_quirk.h>
109 #include <dev/usb/serial/usb_serial.h>
112 static int umodem_debug = 0;
114 static SYSCTL_NODE(_hw_usb, OID_AUTO, umodem, CTLFLAG_RW, 0, "USB umodem");
115 SYSCTL_INT(_hw_usb_umodem, OID_AUTO, debug, CTLFLAG_RW,
116 &umodem_debug, 0, "Debug level");
119 static const STRUCT_USB_HOST_ID umodem_devs[] = {
120 /* Generic Modem class match */
121 {USB_IFACE_CLASS(UICLASS_CDC),
122 USB_IFACE_SUBCLASS(UISUBCLASS_ABSTRACT_CONTROL_MODEL),
123 USB_IFACE_PROTOCOL(UIPROTO_CDC_AT)},
124 /* Huawei Modem class match */
125 {USB_IFACE_CLASS(UICLASS_CDC),
126 USB_IFACE_SUBCLASS(UISUBCLASS_ABSTRACT_CONTROL_MODEL),
127 USB_IFACE_PROTOCOL(0xFF)},
128 /* Kyocera AH-K3001V */
129 {USB_VPI(USB_VENDOR_KYOCERA, USB_PRODUCT_KYOCERA_AHK3001V, 1)},
130 {USB_VPI(USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC5720, 1)},
131 {USB_VPI(USB_VENDOR_CURITEL, USB_PRODUCT_CURITEL_PC5740, 1)},
135 * As speeds for umodem devices increase, these numbers will need to
136 * be increased. They should be good for G3 speeds and below.
138 * TODO: The TTY buffers should be increased!
140 #define UMODEM_BUF_SIZE 1024
149 #define UMODEM_MODVER 1 /* module version */
151 struct umodem_softc {
152 struct ucom_super_softc sc_super_ucom;
153 struct ucom_softc sc_ucom;
155 struct usb_xfer *sc_xfer[UMODEM_N_TRANSFER];
156 struct usb_device *sc_udev;
161 uint8_t sc_lsr; /* local status register */
162 uint8_t sc_msr; /* modem status register */
163 uint8_t sc_ctrl_iface_no;
164 uint8_t sc_data_iface_no;
165 uint8_t sc_iface_index[2];
166 uint8_t sc_cm_over_data;
167 uint8_t sc_cm_cap; /* CM capabilities */
168 uint8_t sc_acm_cap; /* ACM capabilities */
171 static device_probe_t umodem_probe;
172 static device_attach_t umodem_attach;
173 static device_detach_t umodem_detach;
174 static void umodem_free_softc(struct umodem_softc *);
176 static usb_callback_t umodem_intr_callback;
177 static usb_callback_t umodem_write_callback;
178 static usb_callback_t umodem_read_callback;
180 static void umodem_free(struct ucom_softc *);
181 static void umodem_start_read(struct ucom_softc *);
182 static void umodem_stop_read(struct ucom_softc *);
183 static void umodem_start_write(struct ucom_softc *);
184 static void umodem_stop_write(struct ucom_softc *);
185 static void umodem_get_caps(struct usb_attach_arg *, uint8_t *, uint8_t *);
186 static void umodem_cfg_get_status(struct ucom_softc *, uint8_t *,
188 static int umodem_pre_param(struct ucom_softc *, struct termios *);
189 static void umodem_cfg_param(struct ucom_softc *, struct termios *);
190 static int umodem_ioctl(struct ucom_softc *, uint32_t, caddr_t, int,
192 static void umodem_cfg_set_dtr(struct ucom_softc *, uint8_t);
193 static void umodem_cfg_set_rts(struct ucom_softc *, uint8_t);
194 static void umodem_cfg_set_break(struct ucom_softc *, uint8_t);
195 static void *umodem_get_desc(struct usb_attach_arg *, uint8_t, uint8_t);
196 static usb_error_t umodem_set_comm_feature(struct usb_device *, uint8_t,
198 static void umodem_poll(struct ucom_softc *ucom);
199 static void umodem_find_data_iface(struct usb_attach_arg *uaa,
200 uint8_t, uint8_t *, uint8_t *);
202 static const struct usb_config umodem_config[UMODEM_N_TRANSFER] = {
206 .endpoint = UE_ADDR_ANY,
207 .direction = UE_DIR_OUT,
209 .bufsize = UMODEM_BUF_SIZE,
210 .flags = {.pipe_bof = 1,.force_short_xfer = 1,},
211 .callback = &umodem_write_callback,
216 .endpoint = UE_ADDR_ANY,
217 .direction = UE_DIR_IN,
219 .bufsize = UMODEM_BUF_SIZE,
220 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
221 .callback = &umodem_read_callback,
225 .type = UE_INTERRUPT,
226 .endpoint = UE_ADDR_ANY,
227 .direction = UE_DIR_IN,
229 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,.no_pipe_ok = 1,},
230 .bufsize = 0, /* use wMaxPacketSize */
231 .callback = &umodem_intr_callback,
235 static const struct ucom_callback umodem_callback = {
236 .ucom_cfg_get_status = &umodem_cfg_get_status,
237 .ucom_cfg_set_dtr = &umodem_cfg_set_dtr,
238 .ucom_cfg_set_rts = &umodem_cfg_set_rts,
239 .ucom_cfg_set_break = &umodem_cfg_set_break,
240 .ucom_cfg_param = &umodem_cfg_param,
241 .ucom_pre_param = &umodem_pre_param,
242 .ucom_ioctl = &umodem_ioctl,
243 .ucom_start_read = &umodem_start_read,
244 .ucom_stop_read = &umodem_stop_read,
245 .ucom_start_write = &umodem_start_write,
246 .ucom_stop_write = &umodem_stop_write,
247 .ucom_poll = &umodem_poll,
248 .ucom_free = &umodem_free,
251 static device_method_t umodem_methods[] = {
252 DEVMETHOD(device_probe, umodem_probe),
253 DEVMETHOD(device_attach, umodem_attach),
254 DEVMETHOD(device_detach, umodem_detach),
258 static devclass_t umodem_devclass;
260 static driver_t umodem_driver = {
262 .methods = umodem_methods,
263 .size = sizeof(struct umodem_softc),
266 DRIVER_MODULE(umodem, uhub, umodem_driver, umodem_devclass, NULL, 0);
267 MODULE_DEPEND(umodem, ucom, 1, 1, 1);
268 MODULE_DEPEND(umodem, usb, 1, 1, 1);
269 MODULE_VERSION(umodem, UMODEM_MODVER);
272 umodem_probe(device_t dev)
274 struct usb_attach_arg *uaa = device_get_ivars(dev);
279 if (uaa->usb_mode != USB_MODE_HOST)
282 error = usbd_lookup_id_by_uaa(umodem_devs, sizeof(umodem_devs), uaa);
286 return (BUS_PROBE_GENERIC);
290 umodem_attach(device_t dev)
292 struct usb_attach_arg *uaa = device_get_ivars(dev);
293 struct umodem_softc *sc = device_get_softc(dev);
294 struct usb_cdc_cm_descriptor *cmd;
295 struct usb_cdc_union_descriptor *cud;
299 device_set_usb_desc(dev);
300 mtx_init(&sc->sc_mtx, "umodem", NULL, MTX_DEF);
301 ucom_ref(&sc->sc_super_ucom);
303 sc->sc_ctrl_iface_no = uaa->info.bIfaceNum;
304 sc->sc_iface_index[1] = uaa->info.bIfaceIndex;
305 sc->sc_udev = uaa->device;
307 umodem_get_caps(uaa, &sc->sc_cm_cap, &sc->sc_acm_cap);
309 /* get the data interface number */
311 cmd = umodem_get_desc(uaa, UDESC_CS_INTERFACE, UDESCSUB_CDC_CM);
313 if ((cmd == NULL) || (cmd->bLength < sizeof(*cmd))) {
315 cud = usbd_find_descriptor(uaa->device, NULL,
316 uaa->info.bIfaceIndex, UDESC_CS_INTERFACE,
317 0xFF, UDESCSUB_CDC_UNION, 0xFF);
319 if ((cud == NULL) || (cud->bLength < sizeof(*cud))) {
320 DPRINTF("Missing descriptor. "
321 "Assuming data interface is next.\n");
322 if (sc->sc_ctrl_iface_no == 0xFF) {
325 uint8_t class_match = 0;
327 /* set default interface number */
328 sc->sc_data_iface_no = 0xFF;
330 /* try to find the data interface backwards */
331 umodem_find_data_iface(uaa,
332 uaa->info.bIfaceIndex - 1,
333 &sc->sc_data_iface_no, &class_match);
335 /* try to find the data interface forwards */
336 umodem_find_data_iface(uaa,
337 uaa->info.bIfaceIndex + 1,
338 &sc->sc_data_iface_no, &class_match);
340 /* check if nothing was found */
341 if (sc->sc_data_iface_no == 0xFF)
345 sc->sc_data_iface_no = cud->bSlaveInterface[0];
348 sc->sc_data_iface_no = cmd->bDataInterface;
351 device_printf(dev, "data interface %d, has %sCM over "
352 "data, has %sbreak\n",
353 sc->sc_data_iface_no,
354 sc->sc_cm_cap & USB_CDC_CM_OVER_DATA ? "" : "no ",
355 sc->sc_acm_cap & USB_CDC_ACM_HAS_BREAK ? "" : "no ");
357 /* get the data interface too */
360 struct usb_interface *iface;
361 struct usb_interface_descriptor *id;
363 iface = usbd_get_iface(uaa->device, i);
367 id = usbd_get_interface_descriptor(iface);
369 if (id && (id->bInterfaceNumber == sc->sc_data_iface_no)) {
370 sc->sc_iface_index[0] = i;
371 usbd_set_parent_iface(uaa->device, i, uaa->info.bIfaceIndex);
375 device_printf(dev, "no data interface\n");
380 if (usb_test_quirk(uaa, UQ_ASSUME_CM_OVER_DATA)) {
381 sc->sc_cm_over_data = 1;
383 if (sc->sc_cm_cap & USB_CDC_CM_OVER_DATA) {
384 if (sc->sc_acm_cap & USB_CDC_ACM_HAS_FEATURE) {
386 error = umodem_set_comm_feature
387 (uaa->device, sc->sc_ctrl_iface_no,
388 UCDC_ABSTRACT_STATE, UCDC_DATA_MULTIPLEXED);
390 /* ignore any errors */
392 sc->sc_cm_over_data = 1;
395 error = usbd_transfer_setup(uaa->device,
396 sc->sc_iface_index, sc->sc_xfer,
397 umodem_config, UMODEM_N_TRANSFER,
403 /* clear stall at first run */
404 mtx_lock(&sc->sc_mtx);
405 usbd_xfer_set_stall(sc->sc_xfer[UMODEM_BULK_WR]);
406 usbd_xfer_set_stall(sc->sc_xfer[UMODEM_BULK_RD]);
407 mtx_unlock(&sc->sc_mtx);
409 error = ucom_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc,
410 &umodem_callback, &sc->sc_mtx);
414 ucom_set_pnpinfo_usb(&sc->sc_super_ucom, dev);
424 umodem_find_data_iface(struct usb_attach_arg *uaa,
425 uint8_t iface_index, uint8_t *p_data_no, uint8_t *p_match_class)
427 struct usb_interface_descriptor *id;
428 struct usb_interface *iface;
430 iface = usbd_get_iface(uaa->device, iface_index);
432 /* check for end of interfaces */
436 id = usbd_get_interface_descriptor(iface);
438 /* check for non-matching interface class */
439 if (id->bInterfaceClass != UICLASS_CDC_DATA ||
440 id->bInterfaceSubClass != UISUBCLASS_DATA) {
441 /* if we got a class match then return */
448 DPRINTFN(11, "Match at index %u\n", iface_index);
450 *p_data_no = id->bInterfaceNumber;
454 umodem_start_read(struct ucom_softc *ucom)
456 struct umodem_softc *sc = ucom->sc_parent;
458 /* start interrupt endpoint, if any */
459 usbd_transfer_start(sc->sc_xfer[UMODEM_INTR_RD]);
461 /* start read endpoint */
462 usbd_transfer_start(sc->sc_xfer[UMODEM_BULK_RD]);
466 umodem_stop_read(struct ucom_softc *ucom)
468 struct umodem_softc *sc = ucom->sc_parent;
470 /* stop interrupt endpoint, if any */
471 usbd_transfer_stop(sc->sc_xfer[UMODEM_INTR_RD]);
473 /* stop read endpoint */
474 usbd_transfer_stop(sc->sc_xfer[UMODEM_BULK_RD]);
478 umodem_start_write(struct ucom_softc *ucom)
480 struct umodem_softc *sc = ucom->sc_parent;
482 usbd_transfer_start(sc->sc_xfer[UMODEM_BULK_WR]);
486 umodem_stop_write(struct ucom_softc *ucom)
488 struct umodem_softc *sc = ucom->sc_parent;
490 usbd_transfer_stop(sc->sc_xfer[UMODEM_BULK_WR]);
494 umodem_get_caps(struct usb_attach_arg *uaa, uint8_t *cm, uint8_t *acm)
496 struct usb_cdc_cm_descriptor *cmd;
497 struct usb_cdc_acm_descriptor *cad;
499 cmd = umodem_get_desc(uaa, UDESC_CS_INTERFACE, UDESCSUB_CDC_CM);
500 if ((cmd == NULL) || (cmd->bLength < sizeof(*cmd))) {
501 DPRINTF("no CM desc (faking one)\n");
502 *cm = USB_CDC_CM_DOES_CM | USB_CDC_CM_OVER_DATA;
504 *cm = cmd->bmCapabilities;
506 cad = umodem_get_desc(uaa, UDESC_CS_INTERFACE, UDESCSUB_CDC_ACM);
507 if ((cad == NULL) || (cad->bLength < sizeof(*cad))) {
508 DPRINTF("no ACM desc\n");
511 *acm = cad->bmCapabilities;
515 umodem_cfg_get_status(struct ucom_softc *ucom, uint8_t *lsr, uint8_t *msr)
517 struct umodem_softc *sc = ucom->sc_parent;
526 umodem_pre_param(struct ucom_softc *ucom, struct termios *t)
528 return (0); /* we accept anything */
532 umodem_cfg_param(struct ucom_softc *ucom, struct termios *t)
534 struct umodem_softc *sc = ucom->sc_parent;
535 struct usb_cdc_line_state ls;
536 struct usb_device_request req;
538 DPRINTF("sc=%p\n", sc);
540 memset(&ls, 0, sizeof(ls));
542 USETDW(ls.dwDTERate, t->c_ospeed);
544 ls.bCharFormat = (t->c_cflag & CSTOPB) ?
545 UCDC_STOP_BIT_2 : UCDC_STOP_BIT_1;
547 ls.bParityType = (t->c_cflag & PARENB) ?
548 ((t->c_cflag & PARODD) ?
549 UCDC_PARITY_ODD : UCDC_PARITY_EVEN) : UCDC_PARITY_NONE;
551 switch (t->c_cflag & CSIZE) {
566 DPRINTF("rate=%d fmt=%d parity=%d bits=%d\n",
567 UGETDW(ls.dwDTERate), ls.bCharFormat,
568 ls.bParityType, ls.bDataBits);
570 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
571 req.bRequest = UCDC_SET_LINE_CODING;
572 USETW(req.wValue, 0);
573 req.wIndex[0] = sc->sc_ctrl_iface_no;
575 USETW(req.wLength, sizeof(ls));
577 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
582 umodem_ioctl(struct ucom_softc *ucom, uint32_t cmd, caddr_t data,
583 int flag, struct thread *td)
585 struct umodem_softc *sc = ucom->sc_parent;
588 DPRINTF("cmd=0x%08x\n", cmd);
591 case USB_GET_CM_OVER_DATA:
592 *(int *)data = sc->sc_cm_over_data;
595 case USB_SET_CM_OVER_DATA:
596 if (*(int *)data != sc->sc_cm_over_data) {
602 DPRINTF("unknown\n");
611 umodem_cfg_set_dtr(struct ucom_softc *ucom, uint8_t onoff)
613 struct umodem_softc *sc = ucom->sc_parent;
614 struct usb_device_request req;
616 DPRINTF("onoff=%d\n", onoff);
619 sc->sc_line |= UCDC_LINE_DTR;
621 sc->sc_line &= ~UCDC_LINE_DTR;
623 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
624 req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
625 USETW(req.wValue, sc->sc_line);
626 req.wIndex[0] = sc->sc_ctrl_iface_no;
628 USETW(req.wLength, 0);
630 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
631 &req, NULL, 0, 1000);
635 umodem_cfg_set_rts(struct ucom_softc *ucom, uint8_t onoff)
637 struct umodem_softc *sc = ucom->sc_parent;
638 struct usb_device_request req;
640 DPRINTF("onoff=%d\n", onoff);
643 sc->sc_line |= UCDC_LINE_RTS;
645 sc->sc_line &= ~UCDC_LINE_RTS;
647 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
648 req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
649 USETW(req.wValue, sc->sc_line);
650 req.wIndex[0] = sc->sc_ctrl_iface_no;
652 USETW(req.wLength, 0);
654 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
655 &req, NULL, 0, 1000);
659 umodem_cfg_set_break(struct ucom_softc *ucom, uint8_t onoff)
661 struct umodem_softc *sc = ucom->sc_parent;
662 struct usb_device_request req;
665 DPRINTF("onoff=%d\n", onoff);
667 if (sc->sc_acm_cap & USB_CDC_ACM_HAS_BREAK) {
669 temp = onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF;
671 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
672 req.bRequest = UCDC_SEND_BREAK;
673 USETW(req.wValue, temp);
674 req.wIndex[0] = sc->sc_ctrl_iface_no;
676 USETW(req.wLength, 0);
678 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
679 &req, NULL, 0, 1000);
684 umodem_intr_callback(struct usb_xfer *xfer, usb_error_t error)
686 struct usb_cdc_notification pkt;
687 struct umodem_softc *sc = usbd_xfer_softc(xfer);
688 struct usb_page_cache *pc;
692 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
694 switch (USB_GET_STATE(xfer)) {
695 case USB_ST_TRANSFERRED:
698 DPRINTF("received short packet, "
699 "%d bytes\n", actlen);
702 if (actlen > (int)sizeof(pkt)) {
703 DPRINTF("truncating message\n");
704 actlen = sizeof(pkt);
706 pc = usbd_xfer_get_frame(xfer, 0);
707 usbd_copy_out(pc, 0, &pkt, actlen);
711 wLen = UGETW(pkt.wLength);
715 if (pkt.bmRequestType != UCDC_NOTIFICATION) {
716 DPRINTF("unknown message type, "
717 "0x%02x, on notify pipe!\n",
721 switch (pkt.bNotification) {
722 case UCDC_N_SERIAL_STATE:
724 * Set the serial state in ucom driver based on
725 * the bits from the notify message
728 DPRINTF("invalid notification "
729 "length, %d bytes!\n", actlen);
732 DPRINTF("notify bytes = %02x%02x\n",
736 /* Currently, lsr is always zero. */
740 if (pkt.data[0] & UCDC_N_SERIAL_RI) {
741 sc->sc_msr |= SER_RI;
743 if (pkt.data[0] & UCDC_N_SERIAL_DSR) {
744 sc->sc_msr |= SER_DSR;
746 if (pkt.data[0] & UCDC_N_SERIAL_DCD) {
747 sc->sc_msr |= SER_DCD;
749 ucom_status_change(&sc->sc_ucom);
753 DPRINTF("unknown notify message: 0x%02x\n",
760 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
761 usbd_transfer_submit(xfer);
765 if (error != USB_ERR_CANCELLED) {
766 /* try to clear stall first */
767 usbd_xfer_set_stall(xfer);
776 umodem_write_callback(struct usb_xfer *xfer, usb_error_t error)
778 struct umodem_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 UMODEM_BUF_SIZE, &actlen)) {
790 usbd_xfer_set_frame_len(xfer, 0, actlen);
791 usbd_transfer_submit(xfer);
796 if (error != USB_ERR_CANCELLED) {
797 /* try to clear stall first */
798 usbd_xfer_set_stall(xfer);
806 umodem_read_callback(struct usb_xfer *xfer, usb_error_t error)
808 struct umodem_softc *sc = usbd_xfer_softc(xfer);
809 struct usb_page_cache *pc;
812 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
814 switch (USB_GET_STATE(xfer)) {
815 case USB_ST_TRANSFERRED:
817 DPRINTF("actlen=%d\n", actlen);
819 pc = usbd_xfer_get_frame(xfer, 0);
820 ucom_put_data(&sc->sc_ucom, pc, 0, actlen);
824 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
825 usbd_transfer_submit(xfer);
829 if (error != USB_ERR_CANCELLED) {
830 /* try to clear stall first */
831 usbd_xfer_set_stall(xfer);
839 umodem_get_desc(struct usb_attach_arg *uaa, uint8_t type, uint8_t subtype)
841 return (usbd_find_descriptor(uaa->device, NULL, uaa->info.bIfaceIndex,
842 type, 0xFF, subtype, 0xFF));
846 umodem_set_comm_feature(struct usb_device *udev, uint8_t iface_no,
847 uint16_t feature, uint16_t state)
849 struct usb_device_request req;
850 struct usb_cdc_abstract_state ast;
852 DPRINTF("feature=%d state=%d\n",
855 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
856 req.bRequest = UCDC_SET_COMM_FEATURE;
857 USETW(req.wValue, feature);
858 req.wIndex[0] = iface_no;
860 USETW(req.wLength, UCDC_ABSTRACT_STATE_LENGTH);
861 USETW(ast.wState, state);
863 return (usbd_do_request(udev, NULL, &req, &ast));
867 umodem_detach(device_t dev)
869 struct umodem_softc *sc = device_get_softc(dev);
871 DPRINTF("sc=%p\n", sc);
873 ucom_detach(&sc->sc_super_ucom, &sc->sc_ucom);
874 usbd_transfer_unsetup(sc->sc_xfer, UMODEM_N_TRANSFER);
876 device_claim_softc(dev);
878 umodem_free_softc(sc);
883 UCOM_UNLOAD_DRAIN(umodem);
886 umodem_free_softc(struct umodem_softc *sc)
888 if (ucom_unref(&sc->sc_super_ucom)) {
889 mtx_destroy(&sc->sc_mtx);
890 device_free_softc(sc);
895 umodem_free(struct ucom_softc *ucom)
897 umodem_free_softc(ucom->sc_parent);
901 umodem_poll(struct ucom_softc *ucom)
903 struct umodem_softc *sc = ucom->sc_parent;
904 usbd_transfer_poll(sc->sc_xfer, UMODEM_N_TRANSFER);