1 /* $NetBSD: umodem.c,v 1.45 2002/09/23 05:51:23 simonb Exp $ */
7 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD AND BSD-2-Clause-NetBSD
9 * Copyright (c) 2003, M. Warner Losh <imp@FreeBSD.org>.
10 * All rights reserved.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * Copyright (c) 1998 The NetBSD Foundation, Inc.
36 * All rights reserved.
38 * This code is derived from software contributed to The NetBSD Foundation
39 * by Lennart Augustsson (lennart@augustsson.net) at
40 * Carlstedt Research & Technology.
42 * Redistribution and use in source and binary forms, with or without
43 * modification, are permitted provided that the following conditions
45 * 1. Redistributions of source code must retain the above copyright
46 * notice, this list of conditions and the following disclaimer.
47 * 2. Redistributions in binary form must reproduce the above copyright
48 * notice, this list of conditions and the following disclaimer in the
49 * documentation and/or other materials provided with the distribution.
51 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
52 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
53 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
54 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
55 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
56 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
57 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
58 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
59 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
60 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
61 * POSSIBILITY OF SUCH DAMAGE.
65 * Comm Class spec: http://www.usb.org/developers/devclass_docs/usbccs10.pdf
66 * http://www.usb.org/developers/devclass_docs/usbcdc11.pdf
67 * http://www.usb.org/developers/devclass_docs/cdc_wmc10.zip
72 * - Add error recovery in various places; the big problem is what
73 * to do in a callback if there is an error.
74 * - Implement a Call Device for modems without multiplexed commands.
78 #include <sys/stdint.h>
79 #include <sys/stddef.h>
80 #include <sys/param.h>
81 #include <sys/queue.h>
82 #include <sys/types.h>
83 #include <sys/systm.h>
84 #include <sys/kernel.h>
86 #include <sys/module.h>
88 #include <sys/mutex.h>
89 #include <sys/condvar.h>
90 #include <sys/sysctl.h>
92 #include <sys/unistd.h>
93 #include <sys/callout.h>
94 #include <sys/malloc.h>
97 #include <dev/usb/usb.h>
98 #include <dev/usb/usbdi.h>
99 #include <dev/usb/usbdi_util.h>
100 #include <dev/usb/usbhid.h>
101 #include <dev/usb/usb_cdc.h>
105 #include <dev/usb/usb_ioctl.h>
107 #define USB_DEBUG_VAR umodem_debug
108 #include <dev/usb/usb_debug.h>
109 #include <dev/usb/usb_process.h>
110 #include <dev/usb/quirk/usb_quirk.h>
112 #include <dev/usb/serial/usb_serial.h>
115 static int umodem_debug = 0;
117 static SYSCTL_NODE(_hw_usb, OID_AUTO, umodem, CTLFLAG_RW, 0, "USB umodem");
118 SYSCTL_INT(_hw_usb_umodem, OID_AUTO, debug, CTLFLAG_RWTUN,
119 &umodem_debug, 0, "Debug level");
122 static const STRUCT_USB_DUAL_ID umodem_dual_devs[] = {
123 /* Generic Modem class match */
124 {USB_IFACE_CLASS(UICLASS_CDC),
125 USB_IFACE_SUBCLASS(UISUBCLASS_ABSTRACT_CONTROL_MODEL),
126 USB_IFACE_PROTOCOL(UIPROTO_CDC_AT)},
127 {USB_IFACE_CLASS(UICLASS_CDC),
128 USB_IFACE_SUBCLASS(UISUBCLASS_ABSTRACT_CONTROL_MODEL),
129 USB_IFACE_PROTOCOL(UIPROTO_CDC_NONE)},
132 static const STRUCT_USB_HOST_ID umodem_host_devs[] = {
133 /* Huawei Modem class match */
134 {USB_VENDOR(USB_VENDOR_HUAWEI), USB_IFACE_CLASS(UICLASS_VENDOR),
135 USB_IFACE_SUBCLASS(0x02), USB_IFACE_PROTOCOL(0x01)},
136 {USB_VENDOR(USB_VENDOR_HUAWEI), USB_IFACE_CLASS(UICLASS_VENDOR),
137 USB_IFACE_SUBCLASS(0x02), USB_IFACE_PROTOCOL(0x02)},
138 {USB_VENDOR(USB_VENDOR_HUAWEI), USB_IFACE_CLASS(UICLASS_VENDOR),
139 USB_IFACE_SUBCLASS(0x02), USB_IFACE_PROTOCOL(0x10)},
140 {USB_VENDOR(USB_VENDOR_HUAWEI), USB_IFACE_CLASS(UICLASS_VENDOR),
141 USB_IFACE_SUBCLASS(0x02), USB_IFACE_PROTOCOL(0x12)},
142 {USB_VENDOR(USB_VENDOR_HUAWEI), USB_IFACE_CLASS(UICLASS_VENDOR),
143 USB_IFACE_SUBCLASS(0x02), USB_IFACE_PROTOCOL(0x61)},
144 {USB_VENDOR(USB_VENDOR_HUAWEI), USB_IFACE_CLASS(UICLASS_VENDOR),
145 USB_IFACE_SUBCLASS(0x02), USB_IFACE_PROTOCOL(0x62)},
146 {USB_VENDOR(USB_VENDOR_HUAWEI),USB_IFACE_CLASS(UICLASS_CDC),
147 USB_IFACE_SUBCLASS(UISUBCLASS_ABSTRACT_CONTROL_MODEL),
148 USB_IFACE_PROTOCOL(0xFF)},
149 /* Kyocera AH-K3001V */
150 {USB_VPI(USB_VENDOR_KYOCERA, USB_PRODUCT_KYOCERA_AHK3001V, 1)},
151 {USB_VPI(USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC5720, 1)},
152 {USB_VPI(USB_VENDOR_CURITEL, USB_PRODUCT_CURITEL_PC5740, 1)},
156 * As speeds for umodem devices increase, these numbers will need to
157 * be increased. They should be good for G3 speeds and below.
159 * TODO: The TTY buffers should be increased!
161 #define UMODEM_BUF_SIZE 1024
171 #define UMODEM_MODVER 1 /* module version */
173 struct umodem_softc {
174 struct ucom_super_softc sc_super_ucom;
175 struct ucom_softc sc_ucom;
177 struct usb_xfer *sc_xfer[UMODEM_N_TRANSFER];
178 struct usb_device *sc_udev;
183 uint8_t sc_lsr; /* local status register */
184 uint8_t sc_msr; /* modem status register */
185 uint8_t sc_ctrl_iface_no;
186 uint8_t sc_data_iface_no;
187 uint8_t sc_iface_index[2];
188 uint8_t sc_cm_over_data;
189 uint8_t sc_cm_cap; /* CM capabilities */
190 uint8_t sc_acm_cap; /* ACM capabilities */
191 uint8_t sc_line_coding[32]; /* used in USB device mode */
192 uint8_t sc_abstract_state[32]; /* used in USB device mode */
195 static device_probe_t umodem_probe;
196 static device_attach_t umodem_attach;
197 static device_detach_t umodem_detach;
198 static usb_handle_request_t umodem_handle_request;
200 static void umodem_free_softc(struct umodem_softc *);
202 static usb_callback_t umodem_intr_read_callback;
203 static usb_callback_t umodem_intr_write_callback;
204 static usb_callback_t umodem_write_callback;
205 static usb_callback_t umodem_read_callback;
207 static void umodem_free(struct ucom_softc *);
208 static void umodem_start_read(struct ucom_softc *);
209 static void umodem_stop_read(struct ucom_softc *);
210 static void umodem_start_write(struct ucom_softc *);
211 static void umodem_stop_write(struct ucom_softc *);
212 static void umodem_get_caps(struct usb_attach_arg *, uint8_t *, uint8_t *);
213 static void umodem_cfg_get_status(struct ucom_softc *, uint8_t *,
215 static int umodem_pre_param(struct ucom_softc *, struct termios *);
216 static void umodem_cfg_param(struct ucom_softc *, struct termios *);
217 static int umodem_ioctl(struct ucom_softc *, uint32_t, caddr_t, int,
219 static void umodem_cfg_set_dtr(struct ucom_softc *, uint8_t);
220 static void umodem_cfg_set_rts(struct ucom_softc *, uint8_t);
221 static void umodem_cfg_set_break(struct ucom_softc *, uint8_t);
222 static void *umodem_get_desc(struct usb_attach_arg *, uint8_t, uint8_t);
223 static usb_error_t umodem_set_comm_feature(struct usb_device *, uint8_t,
225 static void umodem_poll(struct ucom_softc *ucom);
226 static void umodem_find_data_iface(struct usb_attach_arg *uaa,
227 uint8_t, uint8_t *, uint8_t *);
229 static const struct usb_config umodem_config[UMODEM_N_TRANSFER] = {
233 .endpoint = UE_ADDR_ANY,
234 .direction = UE_DIR_TX,
236 .bufsize = UMODEM_BUF_SIZE,
237 .flags = {.pipe_bof = 1,.force_short_xfer = 1,},
238 .callback = &umodem_write_callback,
239 .usb_mode = USB_MODE_DUAL,
244 .endpoint = UE_ADDR_ANY,
245 .direction = UE_DIR_RX,
247 .bufsize = UMODEM_BUF_SIZE,
248 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
249 .callback = &umodem_read_callback,
250 .usb_mode = USB_MODE_DUAL,
254 .type = UE_INTERRUPT,
255 .endpoint = UE_ADDR_ANY,
256 .direction = UE_DIR_TX,
258 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,.no_pipe_ok = 1,},
259 .bufsize = 0, /* use wMaxPacketSize */
260 .callback = &umodem_intr_write_callback,
261 .usb_mode = USB_MODE_DEVICE,
265 .type = UE_INTERRUPT,
266 .endpoint = UE_ADDR_ANY,
267 .direction = UE_DIR_RX,
269 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,.no_pipe_ok = 1,},
270 .bufsize = 0, /* use wMaxPacketSize */
271 .callback = &umodem_intr_read_callback,
272 .usb_mode = USB_MODE_HOST,
276 static const struct ucom_callback umodem_callback = {
277 .ucom_cfg_get_status = &umodem_cfg_get_status,
278 .ucom_cfg_set_dtr = &umodem_cfg_set_dtr,
279 .ucom_cfg_set_rts = &umodem_cfg_set_rts,
280 .ucom_cfg_set_break = &umodem_cfg_set_break,
281 .ucom_cfg_param = &umodem_cfg_param,
282 .ucom_pre_param = &umodem_pre_param,
283 .ucom_ioctl = &umodem_ioctl,
284 .ucom_start_read = &umodem_start_read,
285 .ucom_stop_read = &umodem_stop_read,
286 .ucom_start_write = &umodem_start_write,
287 .ucom_stop_write = &umodem_stop_write,
288 .ucom_poll = &umodem_poll,
289 .ucom_free = &umodem_free,
292 static device_method_t umodem_methods[] = {
294 DEVMETHOD(usb_handle_request, umodem_handle_request),
296 /* Device interface */
297 DEVMETHOD(device_probe, umodem_probe),
298 DEVMETHOD(device_attach, umodem_attach),
299 DEVMETHOD(device_detach, umodem_detach),
303 static devclass_t umodem_devclass;
305 static driver_t umodem_driver = {
307 .methods = umodem_methods,
308 .size = sizeof(struct umodem_softc),
311 DRIVER_MODULE(umodem, uhub, umodem_driver, umodem_devclass, NULL, 0);
312 MODULE_DEPEND(umodem, ucom, 1, 1, 1);
313 MODULE_DEPEND(umodem, usb, 1, 1, 1);
314 MODULE_VERSION(umodem, UMODEM_MODVER);
315 USB_PNP_DUAL_INFO(umodem_dual_devs);
316 USB_PNP_HOST_INFO(umodem_host_devs);
319 umodem_probe(device_t dev)
321 struct usb_attach_arg *uaa = device_get_ivars(dev);
326 error = usbd_lookup_id_by_uaa(umodem_host_devs,
327 sizeof(umodem_host_devs), uaa);
329 error = usbd_lookup_id_by_uaa(umodem_dual_devs,
330 sizeof(umodem_dual_devs), uaa);
334 return (BUS_PROBE_GENERIC);
338 umodem_attach(device_t dev)
340 struct usb_attach_arg *uaa = device_get_ivars(dev);
341 struct umodem_softc *sc = device_get_softc(dev);
342 struct usb_cdc_cm_descriptor *cmd;
343 struct usb_cdc_union_descriptor *cud;
347 device_set_usb_desc(dev);
348 mtx_init(&sc->sc_mtx, "umodem", NULL, MTX_DEF);
349 ucom_ref(&sc->sc_super_ucom);
351 sc->sc_ctrl_iface_no = uaa->info.bIfaceNum;
352 sc->sc_iface_index[1] = uaa->info.bIfaceIndex;
353 sc->sc_udev = uaa->device;
355 umodem_get_caps(uaa, &sc->sc_cm_cap, &sc->sc_acm_cap);
357 /* get the data interface number */
359 cmd = umodem_get_desc(uaa, UDESC_CS_INTERFACE, UDESCSUB_CDC_CM);
361 if ((cmd == NULL) || (cmd->bLength < sizeof(*cmd))) {
363 cud = usbd_find_descriptor(uaa->device, NULL,
364 uaa->info.bIfaceIndex, UDESC_CS_INTERFACE,
365 0xFF, UDESCSUB_CDC_UNION, 0xFF);
367 if ((cud == NULL) || (cud->bLength < sizeof(*cud))) {
368 DPRINTF("Missing descriptor. "
369 "Assuming data interface is next.\n");
370 if (sc->sc_ctrl_iface_no == 0xFF) {
373 uint8_t class_match = 0;
375 /* set default interface number */
376 sc->sc_data_iface_no = 0xFF;
378 /* try to find the data interface backwards */
379 umodem_find_data_iface(uaa,
380 uaa->info.bIfaceIndex - 1,
381 &sc->sc_data_iface_no, &class_match);
383 /* try to find the data interface forwards */
384 umodem_find_data_iface(uaa,
385 uaa->info.bIfaceIndex + 1,
386 &sc->sc_data_iface_no, &class_match);
388 /* check if nothing was found */
389 if (sc->sc_data_iface_no == 0xFF)
393 sc->sc_data_iface_no = cud->bSlaveInterface[0];
396 sc->sc_data_iface_no = cmd->bDataInterface;
399 device_printf(dev, "data interface %d, has %sCM over "
400 "data, has %sbreak\n",
401 sc->sc_data_iface_no,
402 sc->sc_cm_cap & USB_CDC_CM_OVER_DATA ? "" : "no ",
403 sc->sc_acm_cap & USB_CDC_ACM_HAS_BREAK ? "" : "no ");
405 /* get the data interface too */
408 struct usb_interface *iface;
409 struct usb_interface_descriptor *id;
411 iface = usbd_get_iface(uaa->device, i);
415 id = usbd_get_interface_descriptor(iface);
417 if (id && (id->bInterfaceNumber == sc->sc_data_iface_no)) {
418 sc->sc_iface_index[0] = i;
419 usbd_set_parent_iface(uaa->device, i, uaa->info.bIfaceIndex);
423 device_printf(dev, "no data interface\n");
428 if (usb_test_quirk(uaa, UQ_ASSUME_CM_OVER_DATA)) {
429 sc->sc_cm_over_data = 1;
431 if (sc->sc_cm_cap & USB_CDC_CM_OVER_DATA) {
432 if (sc->sc_acm_cap & USB_CDC_ACM_HAS_FEATURE) {
434 error = umodem_set_comm_feature
435 (uaa->device, sc->sc_ctrl_iface_no,
436 UCDC_ABSTRACT_STATE, UCDC_DATA_MULTIPLEXED);
438 /* ignore any errors */
440 sc->sc_cm_over_data = 1;
443 error = usbd_transfer_setup(uaa->device,
444 sc->sc_iface_index, sc->sc_xfer,
445 umodem_config, UMODEM_N_TRANSFER,
448 device_printf(dev, "Can't setup transfer\n");
452 /* clear stall at first run, if USB host mode */
453 if (uaa->usb_mode == USB_MODE_HOST) {
454 mtx_lock(&sc->sc_mtx);
455 usbd_xfer_set_stall(sc->sc_xfer[UMODEM_BULK_WR]);
456 usbd_xfer_set_stall(sc->sc_xfer[UMODEM_BULK_RD]);
457 mtx_unlock(&sc->sc_mtx);
460 ucom_set_usb_mode(&sc->sc_super_ucom, uaa->usb_mode);
462 error = ucom_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc,
463 &umodem_callback, &sc->sc_mtx);
465 device_printf(dev, "Can't attach com\n");
468 ucom_set_pnpinfo_usb(&sc->sc_super_ucom, dev);
478 umodem_find_data_iface(struct usb_attach_arg *uaa,
479 uint8_t iface_index, uint8_t *p_data_no, uint8_t *p_match_class)
481 struct usb_interface_descriptor *id;
482 struct usb_interface *iface;
484 iface = usbd_get_iface(uaa->device, iface_index);
486 /* check for end of interfaces */
490 id = usbd_get_interface_descriptor(iface);
492 /* check for non-matching interface class */
493 if (id->bInterfaceClass != UICLASS_CDC_DATA ||
494 id->bInterfaceSubClass != UISUBCLASS_DATA) {
495 /* if we got a class match then return */
502 DPRINTFN(11, "Match at index %u\n", iface_index);
504 *p_data_no = id->bInterfaceNumber;
508 umodem_start_read(struct ucom_softc *ucom)
510 struct umodem_softc *sc = ucom->sc_parent;
512 /* start interrupt endpoint, if any */
513 usbd_transfer_start(sc->sc_xfer[UMODEM_INTR_RD]);
515 /* start read endpoint */
516 usbd_transfer_start(sc->sc_xfer[UMODEM_BULK_RD]);
520 umodem_stop_read(struct ucom_softc *ucom)
522 struct umodem_softc *sc = ucom->sc_parent;
524 /* stop interrupt endpoint, if any */
525 usbd_transfer_stop(sc->sc_xfer[UMODEM_INTR_RD]);
527 /* stop read endpoint */
528 usbd_transfer_stop(sc->sc_xfer[UMODEM_BULK_RD]);
532 umodem_start_write(struct ucom_softc *ucom)
534 struct umodem_softc *sc = ucom->sc_parent;
536 usbd_transfer_start(sc->sc_xfer[UMODEM_INTR_WR]);
537 usbd_transfer_start(sc->sc_xfer[UMODEM_BULK_WR]);
541 umodem_stop_write(struct ucom_softc *ucom)
543 struct umodem_softc *sc = ucom->sc_parent;
545 usbd_transfer_stop(sc->sc_xfer[UMODEM_INTR_WR]);
546 usbd_transfer_stop(sc->sc_xfer[UMODEM_BULK_WR]);
550 umodem_get_caps(struct usb_attach_arg *uaa, uint8_t *cm, uint8_t *acm)
552 struct usb_cdc_cm_descriptor *cmd;
553 struct usb_cdc_acm_descriptor *cad;
555 cmd = umodem_get_desc(uaa, UDESC_CS_INTERFACE, UDESCSUB_CDC_CM);
556 if ((cmd == NULL) || (cmd->bLength < sizeof(*cmd))) {
557 DPRINTF("no CM desc (faking one)\n");
558 *cm = USB_CDC_CM_DOES_CM | USB_CDC_CM_OVER_DATA;
560 *cm = cmd->bmCapabilities;
562 cad = umodem_get_desc(uaa, UDESC_CS_INTERFACE, UDESCSUB_CDC_ACM);
563 if ((cad == NULL) || (cad->bLength < sizeof(*cad))) {
564 DPRINTF("no ACM desc\n");
567 *acm = cad->bmCapabilities;
571 umodem_cfg_get_status(struct ucom_softc *ucom, uint8_t *lsr, uint8_t *msr)
573 struct umodem_softc *sc = ucom->sc_parent;
577 /* XXX Note: sc_lsr is always zero */
583 umodem_pre_param(struct ucom_softc *ucom, struct termios *t)
585 return (0); /* we accept anything */
589 umodem_cfg_param(struct ucom_softc *ucom, struct termios *t)
591 struct umodem_softc *sc = ucom->sc_parent;
592 struct usb_cdc_line_state ls;
593 struct usb_device_request req;
595 DPRINTF("sc=%p\n", sc);
597 memset(&ls, 0, sizeof(ls));
599 USETDW(ls.dwDTERate, t->c_ospeed);
601 ls.bCharFormat = (t->c_cflag & CSTOPB) ?
602 UCDC_STOP_BIT_2 : UCDC_STOP_BIT_1;
604 ls.bParityType = (t->c_cflag & PARENB) ?
605 ((t->c_cflag & PARODD) ?
606 UCDC_PARITY_ODD : UCDC_PARITY_EVEN) : UCDC_PARITY_NONE;
608 switch (t->c_cflag & CSIZE) {
623 DPRINTF("rate=%d fmt=%d parity=%d bits=%d\n",
624 UGETDW(ls.dwDTERate), ls.bCharFormat,
625 ls.bParityType, ls.bDataBits);
627 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
628 req.bRequest = UCDC_SET_LINE_CODING;
629 USETW(req.wValue, 0);
630 req.wIndex[0] = sc->sc_ctrl_iface_no;
632 USETW(req.wLength, sizeof(ls));
634 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
639 umodem_ioctl(struct ucom_softc *ucom, uint32_t cmd, caddr_t data,
640 int flag, struct thread *td)
642 struct umodem_softc *sc = ucom->sc_parent;
645 DPRINTF("cmd=0x%08x\n", cmd);
648 case USB_GET_CM_OVER_DATA:
649 *(int *)data = sc->sc_cm_over_data;
652 case USB_SET_CM_OVER_DATA:
653 if (*(int *)data != sc->sc_cm_over_data) {
659 DPRINTF("unknown\n");
668 umodem_cfg_set_dtr(struct ucom_softc *ucom, uint8_t onoff)
670 struct umodem_softc *sc = ucom->sc_parent;
671 struct usb_device_request req;
673 DPRINTF("onoff=%d\n", onoff);
676 sc->sc_line |= UCDC_LINE_DTR;
678 sc->sc_line &= ~UCDC_LINE_DTR;
680 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
681 req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
682 USETW(req.wValue, sc->sc_line);
683 req.wIndex[0] = sc->sc_ctrl_iface_no;
685 USETW(req.wLength, 0);
687 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
688 &req, NULL, 0, 1000);
692 umodem_cfg_set_rts(struct ucom_softc *ucom, uint8_t onoff)
694 struct umodem_softc *sc = ucom->sc_parent;
695 struct usb_device_request req;
697 DPRINTF("onoff=%d\n", onoff);
700 sc->sc_line |= UCDC_LINE_RTS;
702 sc->sc_line &= ~UCDC_LINE_RTS;
704 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
705 req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
706 USETW(req.wValue, sc->sc_line);
707 req.wIndex[0] = sc->sc_ctrl_iface_no;
709 USETW(req.wLength, 0);
711 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
712 &req, NULL, 0, 1000);
716 umodem_cfg_set_break(struct ucom_softc *ucom, uint8_t onoff)
718 struct umodem_softc *sc = ucom->sc_parent;
719 struct usb_device_request req;
722 DPRINTF("onoff=%d\n", onoff);
724 if (sc->sc_acm_cap & USB_CDC_ACM_HAS_BREAK) {
726 temp = onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF;
728 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
729 req.bRequest = UCDC_SEND_BREAK;
730 USETW(req.wValue, temp);
731 req.wIndex[0] = sc->sc_ctrl_iface_no;
733 USETW(req.wLength, 0);
735 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
736 &req, NULL, 0, 1000);
741 umodem_intr_write_callback(struct usb_xfer *xfer, usb_error_t error)
745 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
747 switch (USB_GET_STATE(xfer)) {
748 case USB_ST_TRANSFERRED:
750 DPRINTF("Transferred %d bytes\n", actlen);
758 if (error != USB_ERR_CANCELLED) {
759 /* start clear stall */
760 usbd_xfer_set_stall(xfer);
768 umodem_intr_read_callback(struct usb_xfer *xfer, usb_error_t error)
770 struct usb_cdc_notification pkt;
771 struct umodem_softc *sc = usbd_xfer_softc(xfer);
772 struct usb_page_cache *pc;
776 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
778 switch (USB_GET_STATE(xfer)) {
779 case USB_ST_TRANSFERRED:
782 DPRINTF("received short packet, "
783 "%d bytes\n", actlen);
786 if (actlen > (int)sizeof(pkt)) {
787 DPRINTF("truncating message\n");
788 actlen = sizeof(pkt);
790 pc = usbd_xfer_get_frame(xfer, 0);
791 usbd_copy_out(pc, 0, &pkt, actlen);
795 wLen = UGETW(pkt.wLength);
799 if (pkt.bmRequestType != UCDC_NOTIFICATION) {
800 DPRINTF("unknown message type, "
801 "0x%02x, on notify pipe!\n",
805 switch (pkt.bNotification) {
806 case UCDC_N_SERIAL_STATE:
808 * Set the serial state in ucom driver based on
809 * the bits from the notify message
812 DPRINTF("invalid notification "
813 "length, %d bytes!\n", actlen);
816 DPRINTF("notify bytes = %02x%02x\n",
820 /* Currently, lsr is always zero. */
824 if (pkt.data[0] & UCDC_N_SERIAL_RI) {
825 sc->sc_msr |= SER_RI;
827 if (pkt.data[0] & UCDC_N_SERIAL_DSR) {
828 sc->sc_msr |= SER_DSR;
830 if (pkt.data[0] & UCDC_N_SERIAL_DCD) {
831 sc->sc_msr |= SER_DCD;
833 ucom_status_change(&sc->sc_ucom);
837 DPRINTF("unknown notify message: 0x%02x\n",
844 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
845 usbd_transfer_submit(xfer);
849 if (error != USB_ERR_CANCELLED) {
850 /* try to clear stall first */
851 usbd_xfer_set_stall(xfer);
860 umodem_write_callback(struct usb_xfer *xfer, usb_error_t error)
862 struct umodem_softc *sc = usbd_xfer_softc(xfer);
863 struct usb_page_cache *pc;
866 switch (USB_GET_STATE(xfer)) {
868 case USB_ST_TRANSFERRED:
870 pc = usbd_xfer_get_frame(xfer, 0);
871 if (ucom_get_data(&sc->sc_ucom, pc, 0,
872 UMODEM_BUF_SIZE, &actlen)) {
874 usbd_xfer_set_frame_len(xfer, 0, actlen);
875 usbd_transfer_submit(xfer);
880 if (error != USB_ERR_CANCELLED) {
881 /* try to clear stall first */
882 usbd_xfer_set_stall(xfer);
890 umodem_read_callback(struct usb_xfer *xfer, usb_error_t error)
892 struct umodem_softc *sc = usbd_xfer_softc(xfer);
893 struct usb_page_cache *pc;
896 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
898 switch (USB_GET_STATE(xfer)) {
899 case USB_ST_TRANSFERRED:
901 DPRINTF("actlen=%d\n", actlen);
903 pc = usbd_xfer_get_frame(xfer, 0);
904 ucom_put_data(&sc->sc_ucom, pc, 0, actlen);
908 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
909 usbd_transfer_submit(xfer);
913 if (error != USB_ERR_CANCELLED) {
914 /* try to clear stall first */
915 usbd_xfer_set_stall(xfer);
923 umodem_get_desc(struct usb_attach_arg *uaa, uint8_t type, uint8_t subtype)
925 return (usbd_find_descriptor(uaa->device, NULL, uaa->info.bIfaceIndex,
926 type, 0xFF, subtype, 0xFF));
930 umodem_set_comm_feature(struct usb_device *udev, uint8_t iface_no,
931 uint16_t feature, uint16_t state)
933 struct usb_device_request req;
934 struct usb_cdc_abstract_state ast;
936 DPRINTF("feature=%d state=%d\n",
939 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
940 req.bRequest = UCDC_SET_COMM_FEATURE;
941 USETW(req.wValue, feature);
942 req.wIndex[0] = iface_no;
944 USETW(req.wLength, UCDC_ABSTRACT_STATE_LENGTH);
945 USETW(ast.wState, state);
947 return (usbd_do_request(udev, NULL, &req, &ast));
951 umodem_detach(device_t dev)
953 struct umodem_softc *sc = device_get_softc(dev);
955 DPRINTF("sc=%p\n", sc);
957 ucom_detach(&sc->sc_super_ucom, &sc->sc_ucom);
958 usbd_transfer_unsetup(sc->sc_xfer, UMODEM_N_TRANSFER);
960 device_claim_softc(dev);
962 umodem_free_softc(sc);
967 UCOM_UNLOAD_DRAIN(umodem);
970 umodem_free_softc(struct umodem_softc *sc)
972 if (ucom_unref(&sc->sc_super_ucom)) {
973 mtx_destroy(&sc->sc_mtx);
974 device_free_softc(sc);
979 umodem_free(struct ucom_softc *ucom)
981 umodem_free_softc(ucom->sc_parent);
985 umodem_poll(struct ucom_softc *ucom)
987 struct umodem_softc *sc = ucom->sc_parent;
988 usbd_transfer_poll(sc->sc_xfer, UMODEM_N_TRANSFER);
992 umodem_handle_request(device_t dev,
993 const void *preq, void **pptr, uint16_t *plen,
994 uint16_t offset, uint8_t *pstate)
996 struct umodem_softc *sc = device_get_softc(dev);
997 const struct usb_device_request *req = preq;
998 uint8_t is_complete = *pstate;
1000 DPRINTF("sc=%p\n", sc);
1003 if ((req->bmRequestType == UT_WRITE_CLASS_INTERFACE) &&
1004 (req->bRequest == UCDC_SET_LINE_CODING) &&
1005 (req->wIndex[0] == sc->sc_ctrl_iface_no) &&
1006 (req->wIndex[1] == 0x00) &&
1007 (req->wValue[0] == 0x00) &&
1008 (req->wValue[1] == 0x00)) {
1010 *plen = sizeof(sc->sc_line_coding);
1011 *pptr = &sc->sc_line_coding;
1016 } else if ((req->bmRequestType == UT_WRITE_CLASS_INTERFACE) &&
1017 (req->wIndex[0] == sc->sc_ctrl_iface_no) &&
1018 (req->wIndex[1] == 0x00) &&
1019 (req->bRequest == UCDC_SET_COMM_FEATURE)) {
1021 *plen = sizeof(sc->sc_abstract_state);
1022 *pptr = &sc->sc_abstract_state;
1027 } else if ((req->bmRequestType == UT_WRITE_CLASS_INTERFACE) &&
1028 (req->wIndex[0] == sc->sc_ctrl_iface_no) &&
1029 (req->wIndex[1] == 0x00) &&
1030 (req->bRequest == UCDC_SET_CONTROL_LINE_STATE)) {
1033 } else if ((req->bmRequestType == UT_WRITE_CLASS_INTERFACE) &&
1034 (req->wIndex[0] == sc->sc_ctrl_iface_no) &&
1035 (req->wIndex[1] == 0x00) &&
1036 (req->bRequest == UCDC_SEND_BREAK)) {
1041 return (ENXIO); /* use builtin handler */