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 error = ucom_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc,
461 &umodem_callback, &sc->sc_mtx);
463 device_printf(dev, "Can't attach com\n");
466 ucom_set_pnpinfo_usb(&sc->sc_super_ucom, dev);
476 umodem_find_data_iface(struct usb_attach_arg *uaa,
477 uint8_t iface_index, uint8_t *p_data_no, uint8_t *p_match_class)
479 struct usb_interface_descriptor *id;
480 struct usb_interface *iface;
482 iface = usbd_get_iface(uaa->device, iface_index);
484 /* check for end of interfaces */
488 id = usbd_get_interface_descriptor(iface);
490 /* check for non-matching interface class */
491 if (id->bInterfaceClass != UICLASS_CDC_DATA ||
492 id->bInterfaceSubClass != UISUBCLASS_DATA) {
493 /* if we got a class match then return */
500 DPRINTFN(11, "Match at index %u\n", iface_index);
502 *p_data_no = id->bInterfaceNumber;
506 umodem_start_read(struct ucom_softc *ucom)
508 struct umodem_softc *sc = ucom->sc_parent;
510 /* start interrupt endpoint, if any */
511 usbd_transfer_start(sc->sc_xfer[UMODEM_INTR_RD]);
513 /* start read endpoint */
514 usbd_transfer_start(sc->sc_xfer[UMODEM_BULK_RD]);
518 umodem_stop_read(struct ucom_softc *ucom)
520 struct umodem_softc *sc = ucom->sc_parent;
522 /* stop interrupt endpoint, if any */
523 usbd_transfer_stop(sc->sc_xfer[UMODEM_INTR_RD]);
525 /* stop read endpoint */
526 usbd_transfer_stop(sc->sc_xfer[UMODEM_BULK_RD]);
530 umodem_start_write(struct ucom_softc *ucom)
532 struct umodem_softc *sc = ucom->sc_parent;
534 usbd_transfer_start(sc->sc_xfer[UMODEM_INTR_WR]);
535 usbd_transfer_start(sc->sc_xfer[UMODEM_BULK_WR]);
539 umodem_stop_write(struct ucom_softc *ucom)
541 struct umodem_softc *sc = ucom->sc_parent;
543 usbd_transfer_stop(sc->sc_xfer[UMODEM_INTR_WR]);
544 usbd_transfer_stop(sc->sc_xfer[UMODEM_BULK_WR]);
548 umodem_get_caps(struct usb_attach_arg *uaa, uint8_t *cm, uint8_t *acm)
550 struct usb_cdc_cm_descriptor *cmd;
551 struct usb_cdc_acm_descriptor *cad;
553 cmd = umodem_get_desc(uaa, UDESC_CS_INTERFACE, UDESCSUB_CDC_CM);
554 if ((cmd == NULL) || (cmd->bLength < sizeof(*cmd))) {
555 DPRINTF("no CM desc (faking one)\n");
556 *cm = USB_CDC_CM_DOES_CM | USB_CDC_CM_OVER_DATA;
558 *cm = cmd->bmCapabilities;
560 cad = umodem_get_desc(uaa, UDESC_CS_INTERFACE, UDESCSUB_CDC_ACM);
561 if ((cad == NULL) || (cad->bLength < sizeof(*cad))) {
562 DPRINTF("no ACM desc\n");
565 *acm = cad->bmCapabilities;
569 umodem_cfg_get_status(struct ucom_softc *ucom, uint8_t *lsr, uint8_t *msr)
571 struct umodem_softc *sc = ucom->sc_parent;
575 /* XXX Note: sc_lsr is always zero */
581 umodem_pre_param(struct ucom_softc *ucom, struct termios *t)
583 return (0); /* we accept anything */
587 umodem_cfg_param(struct ucom_softc *ucom, struct termios *t)
589 struct umodem_softc *sc = ucom->sc_parent;
590 struct usb_cdc_line_state ls;
591 struct usb_device_request req;
593 DPRINTF("sc=%p\n", sc);
595 memset(&ls, 0, sizeof(ls));
597 USETDW(ls.dwDTERate, t->c_ospeed);
599 ls.bCharFormat = (t->c_cflag & CSTOPB) ?
600 UCDC_STOP_BIT_2 : UCDC_STOP_BIT_1;
602 ls.bParityType = (t->c_cflag & PARENB) ?
603 ((t->c_cflag & PARODD) ?
604 UCDC_PARITY_ODD : UCDC_PARITY_EVEN) : UCDC_PARITY_NONE;
606 switch (t->c_cflag & CSIZE) {
621 DPRINTF("rate=%d fmt=%d parity=%d bits=%d\n",
622 UGETDW(ls.dwDTERate), ls.bCharFormat,
623 ls.bParityType, ls.bDataBits);
625 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
626 req.bRequest = UCDC_SET_LINE_CODING;
627 USETW(req.wValue, 0);
628 req.wIndex[0] = sc->sc_ctrl_iface_no;
630 USETW(req.wLength, sizeof(ls));
632 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
637 umodem_ioctl(struct ucom_softc *ucom, uint32_t cmd, caddr_t data,
638 int flag, struct thread *td)
640 struct umodem_softc *sc = ucom->sc_parent;
643 DPRINTF("cmd=0x%08x\n", cmd);
646 case USB_GET_CM_OVER_DATA:
647 *(int *)data = sc->sc_cm_over_data;
650 case USB_SET_CM_OVER_DATA:
651 if (*(int *)data != sc->sc_cm_over_data) {
657 DPRINTF("unknown\n");
666 umodem_cfg_set_dtr(struct ucom_softc *ucom, uint8_t onoff)
668 struct umodem_softc *sc = ucom->sc_parent;
669 struct usb_device_request req;
671 DPRINTF("onoff=%d\n", onoff);
674 sc->sc_line |= UCDC_LINE_DTR;
676 sc->sc_line &= ~UCDC_LINE_DTR;
678 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
679 req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
680 USETW(req.wValue, sc->sc_line);
681 req.wIndex[0] = sc->sc_ctrl_iface_no;
683 USETW(req.wLength, 0);
685 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
686 &req, NULL, 0, 1000);
690 umodem_cfg_set_rts(struct ucom_softc *ucom, uint8_t onoff)
692 struct umodem_softc *sc = ucom->sc_parent;
693 struct usb_device_request req;
695 DPRINTF("onoff=%d\n", onoff);
698 sc->sc_line |= UCDC_LINE_RTS;
700 sc->sc_line &= ~UCDC_LINE_RTS;
702 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
703 req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
704 USETW(req.wValue, sc->sc_line);
705 req.wIndex[0] = sc->sc_ctrl_iface_no;
707 USETW(req.wLength, 0);
709 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
710 &req, NULL, 0, 1000);
714 umodem_cfg_set_break(struct ucom_softc *ucom, uint8_t onoff)
716 struct umodem_softc *sc = ucom->sc_parent;
717 struct usb_device_request req;
720 DPRINTF("onoff=%d\n", onoff);
722 if (sc->sc_acm_cap & USB_CDC_ACM_HAS_BREAK) {
724 temp = onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF;
726 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
727 req.bRequest = UCDC_SEND_BREAK;
728 USETW(req.wValue, temp);
729 req.wIndex[0] = sc->sc_ctrl_iface_no;
731 USETW(req.wLength, 0);
733 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
734 &req, NULL, 0, 1000);
739 umodem_intr_write_callback(struct usb_xfer *xfer, usb_error_t error)
743 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
745 switch (USB_GET_STATE(xfer)) {
746 case USB_ST_TRANSFERRED:
748 DPRINTF("Transferred %d bytes\n", actlen);
756 if (error != USB_ERR_CANCELLED) {
757 /* start clear stall */
758 usbd_xfer_set_stall(xfer);
766 umodem_intr_read_callback(struct usb_xfer *xfer, usb_error_t error)
768 struct usb_cdc_notification pkt;
769 struct umodem_softc *sc = usbd_xfer_softc(xfer);
770 struct usb_page_cache *pc;
774 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
776 switch (USB_GET_STATE(xfer)) {
777 case USB_ST_TRANSFERRED:
780 DPRINTF("received short packet, "
781 "%d bytes\n", actlen);
784 if (actlen > (int)sizeof(pkt)) {
785 DPRINTF("truncating message\n");
786 actlen = sizeof(pkt);
788 pc = usbd_xfer_get_frame(xfer, 0);
789 usbd_copy_out(pc, 0, &pkt, actlen);
793 wLen = UGETW(pkt.wLength);
797 if (pkt.bmRequestType != UCDC_NOTIFICATION) {
798 DPRINTF("unknown message type, "
799 "0x%02x, on notify pipe!\n",
803 switch (pkt.bNotification) {
804 case UCDC_N_SERIAL_STATE:
806 * Set the serial state in ucom driver based on
807 * the bits from the notify message
810 DPRINTF("invalid notification "
811 "length, %d bytes!\n", actlen);
814 DPRINTF("notify bytes = %02x%02x\n",
818 /* Currently, lsr is always zero. */
822 if (pkt.data[0] & UCDC_N_SERIAL_RI) {
823 sc->sc_msr |= SER_RI;
825 if (pkt.data[0] & UCDC_N_SERIAL_DSR) {
826 sc->sc_msr |= SER_DSR;
828 if (pkt.data[0] & UCDC_N_SERIAL_DCD) {
829 sc->sc_msr |= SER_DCD;
831 ucom_status_change(&sc->sc_ucom);
835 DPRINTF("unknown notify message: 0x%02x\n",
842 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
843 usbd_transfer_submit(xfer);
847 if (error != USB_ERR_CANCELLED) {
848 /* try to clear stall first */
849 usbd_xfer_set_stall(xfer);
858 umodem_write_callback(struct usb_xfer *xfer, usb_error_t error)
860 struct umodem_softc *sc = usbd_xfer_softc(xfer);
861 struct usb_page_cache *pc;
864 switch (USB_GET_STATE(xfer)) {
866 case USB_ST_TRANSFERRED:
868 pc = usbd_xfer_get_frame(xfer, 0);
869 if (ucom_get_data(&sc->sc_ucom, pc, 0,
870 UMODEM_BUF_SIZE, &actlen)) {
872 usbd_xfer_set_frame_len(xfer, 0, actlen);
873 usbd_transfer_submit(xfer);
878 if (error != USB_ERR_CANCELLED) {
879 /* try to clear stall first */
880 usbd_xfer_set_stall(xfer);
888 umodem_read_callback(struct usb_xfer *xfer, usb_error_t error)
890 struct umodem_softc *sc = usbd_xfer_softc(xfer);
891 struct usb_page_cache *pc;
894 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
896 switch (USB_GET_STATE(xfer)) {
897 case USB_ST_TRANSFERRED:
899 DPRINTF("actlen=%d\n", actlen);
901 pc = usbd_xfer_get_frame(xfer, 0);
902 ucom_put_data(&sc->sc_ucom, pc, 0, actlen);
906 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
907 usbd_transfer_submit(xfer);
911 if (error != USB_ERR_CANCELLED) {
912 /* try to clear stall first */
913 usbd_xfer_set_stall(xfer);
921 umodem_get_desc(struct usb_attach_arg *uaa, uint8_t type, uint8_t subtype)
923 return (usbd_find_descriptor(uaa->device, NULL, uaa->info.bIfaceIndex,
924 type, 0xFF, subtype, 0xFF));
928 umodem_set_comm_feature(struct usb_device *udev, uint8_t iface_no,
929 uint16_t feature, uint16_t state)
931 struct usb_device_request req;
932 struct usb_cdc_abstract_state ast;
934 DPRINTF("feature=%d state=%d\n",
937 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
938 req.bRequest = UCDC_SET_COMM_FEATURE;
939 USETW(req.wValue, feature);
940 req.wIndex[0] = iface_no;
942 USETW(req.wLength, UCDC_ABSTRACT_STATE_LENGTH);
943 USETW(ast.wState, state);
945 return (usbd_do_request(udev, NULL, &req, &ast));
949 umodem_detach(device_t dev)
951 struct umodem_softc *sc = device_get_softc(dev);
953 DPRINTF("sc=%p\n", sc);
955 ucom_detach(&sc->sc_super_ucom, &sc->sc_ucom);
956 usbd_transfer_unsetup(sc->sc_xfer, UMODEM_N_TRANSFER);
958 device_claim_softc(dev);
960 umodem_free_softc(sc);
965 UCOM_UNLOAD_DRAIN(umodem);
968 umodem_free_softc(struct umodem_softc *sc)
970 if (ucom_unref(&sc->sc_super_ucom)) {
971 mtx_destroy(&sc->sc_mtx);
972 device_free_softc(sc);
977 umodem_free(struct ucom_softc *ucom)
979 umodem_free_softc(ucom->sc_parent);
983 umodem_poll(struct ucom_softc *ucom)
985 struct umodem_softc *sc = ucom->sc_parent;
986 usbd_transfer_poll(sc->sc_xfer, UMODEM_N_TRANSFER);
990 umodem_handle_request(device_t dev,
991 const void *preq, void **pptr, uint16_t *plen,
992 uint16_t offset, uint8_t *pstate)
994 struct umodem_softc *sc = device_get_softc(dev);
995 const struct usb_device_request *req = preq;
996 uint8_t is_complete = *pstate;
998 DPRINTF("sc=%p\n", sc);
1001 if ((req->bmRequestType == UT_WRITE_CLASS_INTERFACE) &&
1002 (req->bRequest == UCDC_SET_LINE_CODING) &&
1003 (req->wIndex[0] == sc->sc_ctrl_iface_no) &&
1004 (req->wIndex[1] == 0x00) &&
1005 (req->wValue[0] == 0x00) &&
1006 (req->wValue[1] == 0x00)) {
1008 *plen = sizeof(sc->sc_line_coding);
1009 *pptr = &sc->sc_line_coding;
1014 } else if ((req->bmRequestType == UT_WRITE_CLASS_INTERFACE) &&
1015 (req->wIndex[0] == sc->sc_ctrl_iface_no) &&
1016 (req->wIndex[1] == 0x00) &&
1017 (req->bRequest == UCDC_SET_COMM_FEATURE)) {
1019 *plen = sizeof(sc->sc_abstract_state);
1020 *pptr = &sc->sc_abstract_state;
1025 } else if ((req->bmRequestType == UT_WRITE_CLASS_INTERFACE) &&
1026 (req->wIndex[0] == sc->sc_ctrl_iface_no) &&
1027 (req->wIndex[1] == 0x00) &&
1028 (req->bRequest == UCDC_SET_CONTROL_LINE_STATE)) {
1031 } else if ((req->bmRequestType == UT_WRITE_CLASS_INTERFACE) &&
1032 (req->wIndex[0] == sc->sc_ctrl_iface_no) &&
1033 (req->wIndex[1] == 0x00) &&
1034 (req->bRequest == UCDC_SEND_BREAK)) {
1039 return (ENXIO); /* use builtin handler */