1 /* $NetBSD: umodem.c,v 1.45 2002/09/23 05:51:23 simonb Exp $ */
4 * Copyright (c) 2003, M. Warner Losh <imp@FreeBSD.org>.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * Copyright (c) 1998 The NetBSD Foundation, Inc.
31 * All rights reserved.
33 * This code is derived from software contributed to The NetBSD Foundation
34 * by Lennart Augustsson (lennart@augustsson.net) at
35 * Carlstedt Research & Technology.
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
40 * 1. Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in the
44 * documentation and/or other materials provided with the distribution.
45 * 3. All advertising materials mentioning features or use of this software
46 * must display the following acknowledgement:
47 * This product includes software developed by the NetBSD
48 * Foundation, Inc. and its contributors.
49 * 4. Neither the name of The NetBSD Foundation nor the names of its
50 * contributors may be used to endorse or promote products derived
51 * from this software without specific prior written permission.
53 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
54 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
55 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
56 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
57 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
58 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
59 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
60 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
61 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
62 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
63 * POSSIBILITY OF SUCH DAMAGE.
67 * Comm Class spec: http://www.usb.org/developers/devclass_docs/usbccs10.pdf
68 * http://www.usb.org/developers/devclass_docs/usbcdc11.pdf
69 * http://www.usb.org/developers/devclass_docs/cdc_wmc10.zip
74 * - Add error recovery in various places; the big problem is what
75 * to do in a callback if there is an error.
76 * - Implement a Call Device for modems without multiplexed commands.
80 #include <sys/stdint.h>
81 #include <sys/param.h>
82 #include <sys/queue.h>
83 #include <sys/types.h>
84 #include <sys/systm.h>
85 #include <sys/kernel.h>
87 #include <sys/module.h>
89 #include <sys/condvar.h>
90 #include <sys/sysctl.h>
91 #include <sys/unistd.h>
92 #include <sys/callout.h>
93 #include <sys/malloc.h>
96 #include <bus/u4b/usb.h>
97 #include <bus/u4b/usbdi.h>
98 #include <bus/u4b/usbdi_util.h>
99 #include <bus/u4b/usbhid.h>
100 #include <bus/u4b/usb_cdc.h>
101 #include <bus/u4b/usbdevs.h>
103 #include <bus/u4b/usb_ioctl.h>
105 #define USB_DEBUG_VAR umodem_debug
106 #include <bus/u4b/usb_debug.h>
107 #include <bus/u4b/usb_process.h>
108 #include <bus/u4b/quirk/usb_quirk.h>
110 #include <bus/u4b/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_RW,
117 &umodem_debug, 0, "Debug level");
120 static const STRUCT_USB_HOST_ID umodem_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 /* Huawei Modem class match */
126 {USB_IFACE_CLASS(UICLASS_CDC),
127 USB_IFACE_SUBCLASS(UISUBCLASS_ABSTRACT_CONTROL_MODEL),
128 USB_IFACE_PROTOCOL(0xFF)},
129 /* Kyocera AH-K3001V */
130 {USB_VPI(USB_VENDOR_KYOCERA, USB_PRODUCT_KYOCERA_AHK3001V, 1)},
131 {USB_VPI(USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC5720, 1)},
132 {USB_VPI(USB_VENDOR_CURITEL, USB_PRODUCT_CURITEL_PC5740, 1)},
136 * As speeds for umodem deivces increase, these numbers will need to
137 * be increased. They should be good for G3 speeds and below.
139 * TODO: The TTY buffers should be increased!
141 #define UMODEM_BUF_SIZE 1024
150 #define UMODEM_MODVER 1 /* module version */
152 struct umodem_softc {
153 struct ucom_super_softc sc_super_ucom;
154 struct ucom_softc sc_ucom;
156 struct usb_xfer *sc_xfer[UMODEM_N_TRANSFER];
157 struct usb_device *sc_udev;
162 uint8_t sc_lsr; /* local status register */
163 uint8_t sc_msr; /* modem status register */
164 uint8_t sc_ctrl_iface_no;
165 uint8_t sc_data_iface_no;
166 uint8_t sc_iface_index[2];
167 uint8_t sc_cm_over_data;
168 uint8_t sc_cm_cap; /* CM capabilities */
169 uint8_t sc_acm_cap; /* ACM capabilities */
172 static device_probe_t umodem_probe;
173 static device_attach_t umodem_attach;
174 static device_detach_t umodem_detach;
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_start_read(struct ucom_softc *);
181 static void umodem_stop_read(struct ucom_softc *);
182 static void umodem_start_write(struct ucom_softc *);
183 static void umodem_stop_write(struct ucom_softc *);
184 static void umodem_get_caps(struct usb_attach_arg *, uint8_t *, uint8_t *);
185 static void umodem_cfg_get_status(struct ucom_softc *, uint8_t *,
187 static int umodem_pre_param(struct ucom_softc *, struct termios *);
188 static void umodem_cfg_param(struct ucom_softc *, struct termios *);
189 static int umodem_ioctl(struct ucom_softc *, uint32_t, caddr_t, int,
191 static void umodem_cfg_set_dtr(struct ucom_softc *, uint8_t);
192 static void umodem_cfg_set_rts(struct ucom_softc *, uint8_t);
193 static void umodem_cfg_set_break(struct ucom_softc *, uint8_t);
194 static void *umodem_get_desc(struct usb_attach_arg *, uint8_t, uint8_t);
195 static usb_error_t umodem_set_comm_feature(struct usb_device *, uint8_t,
197 static void umodem_poll(struct ucom_softc *ucom);
198 static void umodem_find_data_iface(struct usb_attach_arg *uaa,
199 uint8_t, uint8_t *, uint8_t *);
201 static const struct usb_config umodem_config[UMODEM_N_TRANSFER] = {
205 .endpoint = UE_ADDR_ANY,
206 .direction = UE_DIR_OUT,
208 .bufsize = UMODEM_BUF_SIZE,
209 .flags = {.pipe_bof = 1,.force_short_xfer = 1,},
210 .callback = &umodem_write_callback,
215 .endpoint = UE_ADDR_ANY,
216 .direction = UE_DIR_IN,
218 .bufsize = UMODEM_BUF_SIZE,
219 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
220 .callback = &umodem_read_callback,
224 .type = UE_INTERRUPT,
225 .endpoint = UE_ADDR_ANY,
226 .direction = UE_DIR_IN,
228 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,.no_pipe_ok = 1,},
229 .bufsize = 0, /* use wMaxPacketSize */
230 .callback = &umodem_intr_callback,
234 static const struct ucom_callback umodem_callback = {
235 .ucom_cfg_get_status = &umodem_cfg_get_status,
236 .ucom_cfg_set_dtr = &umodem_cfg_set_dtr,
237 .ucom_cfg_set_rts = &umodem_cfg_set_rts,
238 .ucom_cfg_set_break = &umodem_cfg_set_break,
239 .ucom_cfg_param = &umodem_cfg_param,
240 .ucom_pre_param = &umodem_pre_param,
241 .ucom_ioctl = &umodem_ioctl,
242 .ucom_start_read = &umodem_start_read,
243 .ucom_stop_read = &umodem_stop_read,
244 .ucom_start_write = &umodem_start_write,
245 .ucom_stop_write = &umodem_stop_write,
246 .ucom_poll = &umodem_poll,
249 static device_method_t umodem_methods[] = {
250 DEVMETHOD(device_probe, umodem_probe),
251 DEVMETHOD(device_attach, umodem_attach),
252 DEVMETHOD(device_detach, umodem_detach),
256 static devclass_t umodem_devclass;
258 static driver_t umodem_driver = {
260 .methods = umodem_methods,
261 .size = sizeof(struct umodem_softc),
264 DRIVER_MODULE(umodem, uhub, umodem_driver, umodem_devclass, NULL, 0);
265 MODULE_DEPEND(umodem, ucom, 1, 1, 1);
266 MODULE_DEPEND(umodem, usb, 1, 1, 1);
267 MODULE_VERSION(umodem, UMODEM_MODVER);
270 umodem_probe(device_t dev)
272 struct usb_attach_arg *uaa = device_get_ivars(dev);
277 if (uaa->usb_mode != USB_MODE_HOST)
280 error = usbd_lookup_id_by_uaa(umodem_devs, sizeof(umodem_devs), uaa);
284 return (BUS_PROBE_GENERIC);
288 umodem_attach(device_t dev)
290 struct usb_attach_arg *uaa = device_get_ivars(dev);
291 struct umodem_softc *sc = device_get_softc(dev);
292 struct usb_cdc_cm_descriptor *cmd;
293 struct usb_cdc_union_descriptor *cud;
297 device_set_usb_desc(dev);
298 lockinit(&sc->sc_lock, "umodem", 0, LK_CANRECURSE);
300 sc->sc_ctrl_iface_no = uaa->info.bIfaceNum;
301 sc->sc_iface_index[1] = uaa->info.bIfaceIndex;
302 sc->sc_udev = uaa->device;
304 umodem_get_caps(uaa, &sc->sc_cm_cap, &sc->sc_acm_cap);
306 /* get the data interface number */
308 cmd = umodem_get_desc(uaa, UDESC_CS_INTERFACE, UDESCSUB_CDC_CM);
310 if ((cmd == NULL) || (cmd->bLength < sizeof(*cmd))) {
312 cud = usbd_find_descriptor(uaa->device, NULL,
313 uaa->info.bIfaceIndex, UDESC_CS_INTERFACE,
314 0 - 1, UDESCSUB_CDC_UNION, 0 - 1);
316 if ((cud == NULL) || (cud->bLength < sizeof(*cud))) {
317 DPRINTF("Missing descriptor. "
318 "Assuming data interface is next.\n");
319 if (sc->sc_ctrl_iface_no == 0xFF) {
322 uint8_t class_match = 0;
324 /* set default interface number */
325 sc->sc_data_iface_no = 0xFF;
327 /* try to find the data interface backwards */
328 umodem_find_data_iface(uaa,
329 uaa->info.bIfaceIndex - 1,
330 &sc->sc_data_iface_no, &class_match);
332 /* try to find the data interface forwards */
333 umodem_find_data_iface(uaa,
334 uaa->info.bIfaceIndex + 1,
335 &sc->sc_data_iface_no, &class_match);
337 /* check if nothing was found */
338 if (sc->sc_data_iface_no == 0xFF)
342 sc->sc_data_iface_no = cud->bSlaveInterface[0];
345 sc->sc_data_iface_no = cmd->bDataInterface;
348 device_printf(dev, "data interface %d, has %sCM over "
349 "data, has %sbreak\n",
350 sc->sc_data_iface_no,
351 sc->sc_cm_cap & USB_CDC_CM_OVER_DATA ? "" : "no ",
352 sc->sc_acm_cap & USB_CDC_ACM_HAS_BREAK ? "" : "no ");
354 /* get the data interface too */
357 struct usb_interface *iface;
358 struct usb_interface_descriptor *id;
360 iface = usbd_get_iface(uaa->device, i);
364 id = usbd_get_interface_descriptor(iface);
366 if (id && (id->bInterfaceNumber == sc->sc_data_iface_no)) {
367 sc->sc_iface_index[0] = i;
368 usbd_set_parent_iface(uaa->device, i, uaa->info.bIfaceIndex);
372 device_printf(dev, "no data interface\n");
377 if (usb_test_quirk(uaa, UQ_ASSUME_CM_OVER_DATA)) {
378 sc->sc_cm_over_data = 1;
380 if (sc->sc_cm_cap & USB_CDC_CM_OVER_DATA) {
381 if (sc->sc_acm_cap & USB_CDC_ACM_HAS_FEATURE) {
383 error = umodem_set_comm_feature
384 (uaa->device, sc->sc_ctrl_iface_no,
385 UCDC_ABSTRACT_STATE, UCDC_DATA_MULTIPLEXED);
387 /* ignore any errors */
389 sc->sc_cm_over_data = 1;
392 error = usbd_transfer_setup(uaa->device,
393 sc->sc_iface_index, sc->sc_xfer,
394 umodem_config, UMODEM_N_TRANSFER,
400 /* clear stall at first run */
401 lockmgr(&sc->sc_lock, LK_EXCLUSIVE);
402 usbd_xfer_set_stall(sc->sc_xfer[UMODEM_BULK_WR]);
403 usbd_xfer_set_stall(sc->sc_xfer[UMODEM_BULK_RD]);
404 lockmgr(&sc->sc_lock, LK_RELEASE);
406 error = ucom_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc,
407 &umodem_callback, &sc->sc_lock);
411 ucom_set_pnpinfo_usb(&sc->sc_super_ucom, dev);
421 umodem_find_data_iface(struct usb_attach_arg *uaa,
422 uint8_t iface_index, uint8_t *p_data_no, uint8_t *p_match_class)
424 struct usb_interface_descriptor *id;
425 struct usb_interface *iface;
427 iface = usbd_get_iface(uaa->device, iface_index);
429 /* check for end of interfaces */
433 id = usbd_get_interface_descriptor(iface);
435 /* check for non-matching interface class */
436 if (id->bInterfaceClass != UICLASS_CDC_DATA ||
437 id->bInterfaceSubClass != UISUBCLASS_DATA) {
438 /* if we got a class match then return */
445 DPRINTFN(11, "Match at index %u\n", iface_index);
447 *p_data_no = id->bInterfaceNumber;
451 umodem_start_read(struct ucom_softc *ucom)
453 struct umodem_softc *sc = ucom->sc_parent;
455 /* start interrupt endpoint, if any */
456 usbd_transfer_start(sc->sc_xfer[UMODEM_INTR_RD]);
458 /* start read endpoint */
459 usbd_transfer_start(sc->sc_xfer[UMODEM_BULK_RD]);
463 umodem_stop_read(struct ucom_softc *ucom)
465 struct umodem_softc *sc = ucom->sc_parent;
467 /* stop interrupt endpoint, if any */
468 usbd_transfer_stop(sc->sc_xfer[UMODEM_INTR_RD]);
470 /* stop read endpoint */
471 usbd_transfer_stop(sc->sc_xfer[UMODEM_BULK_RD]);
475 umodem_start_write(struct ucom_softc *ucom)
477 struct umodem_softc *sc = ucom->sc_parent;
479 usbd_transfer_start(sc->sc_xfer[UMODEM_BULK_WR]);
483 umodem_stop_write(struct ucom_softc *ucom)
485 struct umodem_softc *sc = ucom->sc_parent;
487 usbd_transfer_stop(sc->sc_xfer[UMODEM_BULK_WR]);
491 umodem_get_caps(struct usb_attach_arg *uaa, uint8_t *cm, uint8_t *acm)
493 struct usb_cdc_cm_descriptor *cmd;
494 struct usb_cdc_acm_descriptor *cad;
496 cmd = umodem_get_desc(uaa, UDESC_CS_INTERFACE, UDESCSUB_CDC_CM);
497 if ((cmd == NULL) || (cmd->bLength < sizeof(*cmd))) {
498 DPRINTF("no CM desc (faking one)\n");
499 *cm = USB_CDC_CM_DOES_CM | USB_CDC_CM_OVER_DATA;
501 *cm = cmd->bmCapabilities;
503 cad = umodem_get_desc(uaa, UDESC_CS_INTERFACE, UDESCSUB_CDC_ACM);
504 if ((cad == NULL) || (cad->bLength < sizeof(*cad))) {
505 DPRINTF("no ACM desc\n");
508 *acm = cad->bmCapabilities;
512 umodem_cfg_get_status(struct ucom_softc *ucom, uint8_t *lsr, uint8_t *msr)
514 struct umodem_softc *sc = ucom->sc_parent;
523 umodem_pre_param(struct ucom_softc *ucom, struct termios *t)
525 return (0); /* we accept anything */
529 umodem_cfg_param(struct ucom_softc *ucom, struct termios *t)
531 struct umodem_softc *sc = ucom->sc_parent;
532 struct usb_cdc_line_state ls;
533 struct usb_device_request req;
535 DPRINTF("sc=%p\n", sc);
537 memset(&ls, 0, sizeof(ls));
539 USETDW(ls.dwDTERate, t->c_ospeed);
541 ls.bCharFormat = (t->c_cflag & CSTOPB) ?
542 UCDC_STOP_BIT_2 : UCDC_STOP_BIT_1;
544 ls.bParityType = (t->c_cflag & PARENB) ?
545 ((t->c_cflag & PARODD) ?
546 UCDC_PARITY_ODD : UCDC_PARITY_EVEN) : UCDC_PARITY_NONE;
548 switch (t->c_cflag & CSIZE) {
563 DPRINTF("rate=%d fmt=%d parity=%d bits=%d\n",
564 UGETDW(ls.dwDTERate), ls.bCharFormat,
565 ls.bParityType, ls.bDataBits);
567 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
568 req.bRequest = UCDC_SET_LINE_CODING;
569 USETW(req.wValue, 0);
570 req.wIndex[0] = sc->sc_ctrl_iface_no;
572 USETW(req.wLength, sizeof(ls));
574 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
579 umodem_ioctl(struct ucom_softc *ucom, uint32_t cmd, caddr_t data,
580 int flag, struct thread *td)
582 struct umodem_softc *sc = ucom->sc_parent;
585 DPRINTF("cmd=0x%08x\n", cmd);
588 case USB_GET_CM_OVER_DATA:
589 *(int *)data = sc->sc_cm_over_data;
592 case USB_SET_CM_OVER_DATA:
593 if (*(int *)data != sc->sc_cm_over_data) {
599 DPRINTF("unknown\n");
608 umodem_cfg_set_dtr(struct ucom_softc *ucom, uint8_t onoff)
610 struct umodem_softc *sc = ucom->sc_parent;
611 struct usb_device_request req;
613 DPRINTF("onoff=%d\n", onoff);
616 sc->sc_line |= UCDC_LINE_DTR;
618 sc->sc_line &= ~UCDC_LINE_DTR;
620 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
621 req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
622 USETW(req.wValue, sc->sc_line);
623 req.wIndex[0] = sc->sc_ctrl_iface_no;
625 USETW(req.wLength, 0);
627 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
628 &req, NULL, 0, 1000);
632 umodem_cfg_set_rts(struct ucom_softc *ucom, uint8_t onoff)
634 struct umodem_softc *sc = ucom->sc_parent;
635 struct usb_device_request req;
637 DPRINTF("onoff=%d\n", onoff);
640 sc->sc_line |= UCDC_LINE_RTS;
642 sc->sc_line &= ~UCDC_LINE_RTS;
644 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
645 req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
646 USETW(req.wValue, sc->sc_line);
647 req.wIndex[0] = sc->sc_ctrl_iface_no;
649 USETW(req.wLength, 0);
651 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
652 &req, NULL, 0, 1000);
656 umodem_cfg_set_break(struct ucom_softc *ucom, uint8_t onoff)
658 struct umodem_softc *sc = ucom->sc_parent;
659 struct usb_device_request req;
662 DPRINTF("onoff=%d\n", onoff);
664 if (sc->sc_acm_cap & USB_CDC_ACM_HAS_BREAK) {
666 temp = onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF;
668 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
669 req.bRequest = UCDC_SEND_BREAK;
670 USETW(req.wValue, temp);
671 req.wIndex[0] = sc->sc_ctrl_iface_no;
673 USETW(req.wLength, 0);
675 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
676 &req, NULL, 0, 1000);
681 umodem_intr_callback(struct usb_xfer *xfer, usb_error_t error)
683 struct usb_cdc_notification pkt;
684 struct umodem_softc *sc = usbd_xfer_softc(xfer);
685 struct usb_page_cache *pc;
689 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
691 switch (USB_GET_STATE(xfer)) {
692 case USB_ST_TRANSFERRED:
695 DPRINTF("received short packet, "
696 "%d bytes\n", actlen);
699 if (actlen > sizeof(pkt)) {
700 DPRINTF("truncating message\n");
701 actlen = sizeof(pkt);
703 pc = usbd_xfer_get_frame(xfer, 0);
704 usbd_copy_out(pc, 0, &pkt, actlen);
708 wLen = UGETW(pkt.wLength);
712 if (pkt.bmRequestType != UCDC_NOTIFICATION) {
713 DPRINTF("unknown message type, "
714 "0x%02x, on notify pipe!\n",
718 switch (pkt.bNotification) {
719 case UCDC_N_SERIAL_STATE:
721 * Set the serial state in ucom driver based on
722 * the bits from the notify message
725 DPRINTF("invalid notification "
726 "length, %d bytes!\n", actlen);
729 DPRINTF("notify bytes = %02x%02x\n",
733 /* Currently, lsr is always zero. */
737 if (pkt.data[0] & UCDC_N_SERIAL_RI) {
738 sc->sc_msr |= SER_RI;
740 if (pkt.data[0] & UCDC_N_SERIAL_DSR) {
741 sc->sc_msr |= SER_DSR;
743 if (pkt.data[0] & UCDC_N_SERIAL_DCD) {
744 sc->sc_msr |= SER_DCD;
746 ucom_status_change(&sc->sc_ucom);
750 DPRINTF("unknown notify message: 0x%02x\n",
757 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
758 usbd_transfer_submit(xfer);
762 if (error != USB_ERR_CANCELLED) {
763 /* try to clear stall first */
764 usbd_xfer_set_stall(xfer);
773 umodem_write_callback(struct usb_xfer *xfer, usb_error_t error)
775 struct umodem_softc *sc = usbd_xfer_softc(xfer);
776 struct usb_page_cache *pc;
779 switch (USB_GET_STATE(xfer)) {
781 case USB_ST_TRANSFERRED:
783 pc = usbd_xfer_get_frame(xfer, 0);
784 if (ucom_get_data(&sc->sc_ucom, pc, 0,
785 UMODEM_BUF_SIZE, &actlen)) {
787 usbd_xfer_set_frame_len(xfer, 0, actlen);
788 usbd_transfer_submit(xfer);
793 if (error != USB_ERR_CANCELLED) {
794 /* try to clear stall first */
795 usbd_xfer_set_stall(xfer);
803 umodem_read_callback(struct usb_xfer *xfer, usb_error_t error)
805 struct umodem_softc *sc = usbd_xfer_softc(xfer);
806 struct usb_page_cache *pc;
809 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
811 switch (USB_GET_STATE(xfer)) {
812 case USB_ST_TRANSFERRED:
814 DPRINTF("actlen=%d\n", actlen);
816 pc = usbd_xfer_get_frame(xfer, 0);
817 ucom_put_data(&sc->sc_ucom, pc, 0, actlen);
821 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
822 usbd_transfer_submit(xfer);
826 if (error != USB_ERR_CANCELLED) {
827 /* try to clear stall first */
828 usbd_xfer_set_stall(xfer);
836 umodem_get_desc(struct usb_attach_arg *uaa, uint8_t type, uint8_t subtype)
838 return (usbd_find_descriptor(uaa->device, NULL, uaa->info.bIfaceIndex,
839 type, 0 - 1, subtype, 0 - 1));
843 umodem_set_comm_feature(struct usb_device *udev, uint8_t iface_no,
844 uint16_t feature, uint16_t state)
846 struct usb_device_request req;
847 struct usb_cdc_abstract_state ast;
849 DPRINTF("feature=%d state=%d\n",
852 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
853 req.bRequest = UCDC_SET_COMM_FEATURE;
854 USETW(req.wValue, feature);
855 req.wIndex[0] = iface_no;
857 USETW(req.wLength, UCDC_ABSTRACT_STATE_LENGTH);
858 USETW(ast.wState, state);
860 return (usbd_do_request(udev, NULL, &req, &ast));
864 umodem_detach(device_t dev)
866 struct umodem_softc *sc = device_get_softc(dev);
868 DPRINTF("sc=%p\n", sc);
870 ucom_detach(&sc->sc_super_ucom, &sc->sc_ucom);
871 usbd_transfer_unsetup(sc->sc_xfer, UMODEM_N_TRANSFER);
872 lockuninit(&sc->sc_lock);
878 umodem_poll(struct ucom_softc *ucom)
880 struct umodem_softc *sc = ucom->sc_parent;
881 usbd_transfer_poll(sc->sc_xfer, UMODEM_N_TRANSFER);