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>
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * Copyright (c) 1998 The NetBSD Foundation, Inc.
35 * All rights reserved.
37 * This code is derived from software contributed to The NetBSD Foundation
38 * by Lennart Augustsson (lennart@augustsson.net) at
39 * Carlstedt Research & Technology.
41 * Redistribution and use in source and binary forms, with or without
42 * modification, are permitted provided that the following conditions
44 * 1. Redistributions of source code must retain the above copyright
45 * notice, this list of conditions and the following disclaimer.
46 * 2. Redistributions in binary form must reproduce the above copyright
47 * notice, this list of conditions and the following disclaimer in the
48 * documentation and/or other materials provided with the distribution.
50 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
51 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
52 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
53 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
54 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
55 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
56 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
57 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
58 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
59 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
60 * POSSIBILITY OF SUCH DAMAGE.
64 * Comm Class spec: http://www.usb.org/developers/devclass_docs/usbccs10.pdf
65 * http://www.usb.org/developers/devclass_docs/usbcdc11.pdf
66 * http://www.usb.org/developers/devclass_docs/cdc_wmc10.zip
71 * - Add error recovery in various places; the big problem is what
72 * to do in a callback if there is an error.
73 * - Implement a Call Device for modems without multiplexed commands.
77 #include <sys/stdint.h>
78 #include <sys/stddef.h>
79 #include <sys/param.h>
80 #include <sys/queue.h>
81 #include <sys/types.h>
82 #include <sys/systm.h>
83 #include <sys/kernel.h>
85 #include <sys/module.h>
87 #include <sys/mutex.h>
88 #include <sys/condvar.h>
89 #include <sys/sysctl.h>
91 #include <sys/unistd.h>
92 #include <sys/callout.h>
93 #include <sys/malloc.h>
96 #include <dev/usb/usb.h>
97 #include <dev/usb/usbdi.h>
98 #include <dev/usb/usbdi_util.h>
99 #include <dev/usb/usbhid.h>
100 #include <dev/usb/usb_cdc.h>
104 #include <dev/usb/usb_ioctl.h>
106 #define USB_DEBUG_VAR umodem_debug
107 #include <dev/usb/usb_debug.h>
108 #include <dev/usb/usb_process.h>
109 #include <dev/usb/quirk/usb_quirk.h>
111 #include <dev/usb/serial/usb_serial.h>
114 static int umodem_debug = 0;
116 static SYSCTL_NODE(_hw_usb, OID_AUTO, umodem, CTLFLAG_RW, 0, "USB umodem");
117 SYSCTL_INT(_hw_usb_umodem, OID_AUTO, debug, CTLFLAG_RWTUN,
118 &umodem_debug, 0, "Debug level");
121 static const STRUCT_USB_DUAL_ID umodem_dual_devs[] = {
122 /* Generic Modem class match */
123 {USB_IFACE_CLASS(UICLASS_CDC),
124 USB_IFACE_SUBCLASS(UISUBCLASS_ABSTRACT_CONTROL_MODEL),
125 USB_IFACE_PROTOCOL(UIPROTO_CDC_AT)},
126 {USB_IFACE_CLASS(UICLASS_CDC),
127 USB_IFACE_SUBCLASS(UISUBCLASS_ABSTRACT_CONTROL_MODEL),
128 USB_IFACE_PROTOCOL(UIPROTO_CDC_NONE)},
131 static const STRUCT_USB_HOST_ID umodem_host_devs[] = {
132 /* Huawei Modem class match */
133 {USB_VENDOR(USB_VENDOR_HUAWEI), USB_IFACE_CLASS(UICLASS_VENDOR),
134 USB_IFACE_SUBCLASS(0x02), USB_IFACE_PROTOCOL(0x01)},
135 {USB_VENDOR(USB_VENDOR_HUAWEI), USB_IFACE_CLASS(UICLASS_VENDOR),
136 USB_IFACE_SUBCLASS(0x02), USB_IFACE_PROTOCOL(0x02)},
137 {USB_VENDOR(USB_VENDOR_HUAWEI), USB_IFACE_CLASS(UICLASS_VENDOR),
138 USB_IFACE_SUBCLASS(0x02), USB_IFACE_PROTOCOL(0x10)},
139 {USB_VENDOR(USB_VENDOR_HUAWEI), USB_IFACE_CLASS(UICLASS_VENDOR),
140 USB_IFACE_SUBCLASS(0x02), USB_IFACE_PROTOCOL(0x12)},
141 {USB_VENDOR(USB_VENDOR_HUAWEI), USB_IFACE_CLASS(UICLASS_VENDOR),
142 USB_IFACE_SUBCLASS(0x02), USB_IFACE_PROTOCOL(0x61)},
143 {USB_VENDOR(USB_VENDOR_HUAWEI), USB_IFACE_CLASS(UICLASS_VENDOR),
144 USB_IFACE_SUBCLASS(0x02), USB_IFACE_PROTOCOL(0x62)},
145 {USB_VENDOR(USB_VENDOR_HUAWEI),USB_IFACE_CLASS(UICLASS_CDC),
146 USB_IFACE_SUBCLASS(UISUBCLASS_ABSTRACT_CONTROL_MODEL),
147 USB_IFACE_PROTOCOL(0xFF)},
148 /* Kyocera AH-K3001V */
149 {USB_VPI(USB_VENDOR_KYOCERA, USB_PRODUCT_KYOCERA_AHK3001V, 1)},
150 {USB_VPI(USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC5720, 1)},
151 {USB_VPI(USB_VENDOR_CURITEL, USB_PRODUCT_CURITEL_PC5740, 1)},
155 * As speeds for umodem devices increase, these numbers will need to
156 * be increased. They should be good for G3 speeds and below.
158 * TODO: The TTY buffers should be increased!
160 #define UMODEM_BUF_SIZE 1024
170 #define UMODEM_MODVER 1 /* module version */
172 struct umodem_softc {
173 struct ucom_super_softc sc_super_ucom;
174 struct ucom_softc sc_ucom;
176 struct usb_xfer *sc_xfer[UMODEM_N_TRANSFER];
177 struct usb_device *sc_udev;
182 uint8_t sc_lsr; /* local status register */
183 uint8_t sc_msr; /* modem status register */
184 uint8_t sc_ctrl_iface_no;
185 uint8_t sc_data_iface_no;
186 uint8_t sc_iface_index[2];
187 uint8_t sc_cm_over_data;
188 uint8_t sc_cm_cap; /* CM capabilities */
189 uint8_t sc_acm_cap; /* ACM capabilities */
190 uint8_t sc_line_coding[32]; /* used in USB device mode */
191 uint8_t sc_abstract_state[32]; /* used in USB device mode */
194 static device_probe_t umodem_probe;
195 static device_attach_t umodem_attach;
196 static device_detach_t umodem_detach;
197 static usb_handle_request_t umodem_handle_request;
199 static void umodem_free_softc(struct umodem_softc *);
201 static usb_callback_t umodem_intr_read_callback;
202 static usb_callback_t umodem_intr_write_callback;
203 static usb_callback_t umodem_write_callback;
204 static usb_callback_t umodem_read_callback;
206 static void umodem_free(struct ucom_softc *);
207 static void umodem_start_read(struct ucom_softc *);
208 static void umodem_stop_read(struct ucom_softc *);
209 static void umodem_start_write(struct ucom_softc *);
210 static void umodem_stop_write(struct ucom_softc *);
211 static void umodem_get_caps(struct usb_attach_arg *, uint8_t *, uint8_t *);
212 static void umodem_cfg_get_status(struct ucom_softc *, uint8_t *,
214 static int umodem_pre_param(struct ucom_softc *, struct termios *);
215 static void umodem_cfg_param(struct ucom_softc *, struct termios *);
216 static int umodem_ioctl(struct ucom_softc *, uint32_t, caddr_t, int,
218 static void umodem_cfg_set_dtr(struct ucom_softc *, uint8_t);
219 static void umodem_cfg_set_rts(struct ucom_softc *, uint8_t);
220 static void umodem_cfg_set_break(struct ucom_softc *, uint8_t);
221 static void *umodem_get_desc(struct usb_attach_arg *, uint8_t, uint8_t);
222 static usb_error_t umodem_set_comm_feature(struct usb_device *, uint8_t,
224 static void umodem_poll(struct ucom_softc *ucom);
225 static void umodem_find_data_iface(struct usb_attach_arg *uaa,
226 uint8_t, uint8_t *, uint8_t *);
228 static const struct usb_config umodem_config[UMODEM_N_TRANSFER] = {
232 .endpoint = UE_ADDR_ANY,
233 .direction = UE_DIR_TX,
235 .bufsize = UMODEM_BUF_SIZE,
236 .flags = {.pipe_bof = 1,.force_short_xfer = 1,},
237 .callback = &umodem_write_callback,
238 .usb_mode = USB_MODE_DUAL,
243 .endpoint = UE_ADDR_ANY,
244 .direction = UE_DIR_RX,
246 .bufsize = UMODEM_BUF_SIZE,
247 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
248 .callback = &umodem_read_callback,
249 .usb_mode = USB_MODE_DUAL,
253 .type = UE_INTERRUPT,
254 .endpoint = UE_ADDR_ANY,
255 .direction = UE_DIR_TX,
257 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,.no_pipe_ok = 1,},
258 .bufsize = 0, /* use wMaxPacketSize */
259 .callback = &umodem_intr_write_callback,
260 .usb_mode = USB_MODE_DEVICE,
264 .type = UE_INTERRUPT,
265 .endpoint = UE_ADDR_ANY,
266 .direction = UE_DIR_RX,
268 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,.no_pipe_ok = 1,},
269 .bufsize = 0, /* use wMaxPacketSize */
270 .callback = &umodem_intr_read_callback,
271 .usb_mode = USB_MODE_HOST,
275 static const struct ucom_callback umodem_callback = {
276 .ucom_cfg_get_status = &umodem_cfg_get_status,
277 .ucom_cfg_set_dtr = &umodem_cfg_set_dtr,
278 .ucom_cfg_set_rts = &umodem_cfg_set_rts,
279 .ucom_cfg_set_break = &umodem_cfg_set_break,
280 .ucom_cfg_param = &umodem_cfg_param,
281 .ucom_pre_param = &umodem_pre_param,
282 .ucom_ioctl = &umodem_ioctl,
283 .ucom_start_read = &umodem_start_read,
284 .ucom_stop_read = &umodem_stop_read,
285 .ucom_start_write = &umodem_start_write,
286 .ucom_stop_write = &umodem_stop_write,
287 .ucom_poll = &umodem_poll,
288 .ucom_free = &umodem_free,
291 static device_method_t umodem_methods[] = {
293 DEVMETHOD(usb_handle_request, umodem_handle_request),
295 /* Device interface */
296 DEVMETHOD(device_probe, umodem_probe),
297 DEVMETHOD(device_attach, umodem_attach),
298 DEVMETHOD(device_detach, umodem_detach),
302 static devclass_t umodem_devclass;
304 static driver_t umodem_driver = {
306 .methods = umodem_methods,
307 .size = sizeof(struct umodem_softc),
310 DRIVER_MODULE(umodem, uhub, umodem_driver, umodem_devclass, NULL, 0);
311 MODULE_DEPEND(umodem, ucom, 1, 1, 1);
312 MODULE_DEPEND(umodem, usb, 1, 1, 1);
313 MODULE_VERSION(umodem, UMODEM_MODVER);
314 USB_PNP_DUAL_INFO(umodem_dual_devs);
315 USB_PNP_HOST_INFO(umodem_host_devs);
318 umodem_probe(device_t dev)
320 struct usb_attach_arg *uaa = device_get_ivars(dev);
325 error = usbd_lookup_id_by_uaa(umodem_host_devs,
326 sizeof(umodem_host_devs), uaa);
328 error = usbd_lookup_id_by_uaa(umodem_dual_devs,
329 sizeof(umodem_dual_devs), uaa);
333 return (BUS_PROBE_GENERIC);
337 umodem_attach(device_t dev)
339 struct usb_attach_arg *uaa = device_get_ivars(dev);
340 struct umodem_softc *sc = device_get_softc(dev);
341 struct usb_cdc_cm_descriptor *cmd;
342 struct usb_cdc_union_descriptor *cud;
346 device_set_usb_desc(dev);
347 mtx_init(&sc->sc_mtx, "umodem", NULL, MTX_DEF);
348 ucom_ref(&sc->sc_super_ucom);
350 sc->sc_ctrl_iface_no = uaa->info.bIfaceNum;
351 sc->sc_iface_index[1] = uaa->info.bIfaceIndex;
352 sc->sc_udev = uaa->device;
354 umodem_get_caps(uaa, &sc->sc_cm_cap, &sc->sc_acm_cap);
356 /* get the data interface number */
358 cmd = umodem_get_desc(uaa, UDESC_CS_INTERFACE, UDESCSUB_CDC_CM);
360 if ((cmd == NULL) || (cmd->bLength < sizeof(*cmd))) {
362 cud = usbd_find_descriptor(uaa->device, NULL,
363 uaa->info.bIfaceIndex, UDESC_CS_INTERFACE,
364 0xFF, UDESCSUB_CDC_UNION, 0xFF);
366 if ((cud == NULL) || (cud->bLength < sizeof(*cud))) {
367 DPRINTF("Missing descriptor. "
368 "Assuming data interface is next.\n");
369 if (sc->sc_ctrl_iface_no == 0xFF) {
372 uint8_t class_match = 0;
374 /* set default interface number */
375 sc->sc_data_iface_no = 0xFF;
377 /* try to find the data interface backwards */
378 umodem_find_data_iface(uaa,
379 uaa->info.bIfaceIndex - 1,
380 &sc->sc_data_iface_no, &class_match);
382 /* try to find the data interface forwards */
383 umodem_find_data_iface(uaa,
384 uaa->info.bIfaceIndex + 1,
385 &sc->sc_data_iface_no, &class_match);
387 /* check if nothing was found */
388 if (sc->sc_data_iface_no == 0xFF)
392 sc->sc_data_iface_no = cud->bSlaveInterface[0];
395 sc->sc_data_iface_no = cmd->bDataInterface;
398 device_printf(dev, "data interface %d, has %sCM over "
399 "data, has %sbreak\n",
400 sc->sc_data_iface_no,
401 sc->sc_cm_cap & USB_CDC_CM_OVER_DATA ? "" : "no ",
402 sc->sc_acm_cap & USB_CDC_ACM_HAS_BREAK ? "" : "no ");
404 /* get the data interface too */
407 struct usb_interface *iface;
408 struct usb_interface_descriptor *id;
410 iface = usbd_get_iface(uaa->device, i);
414 id = usbd_get_interface_descriptor(iface);
416 if (id && (id->bInterfaceNumber == sc->sc_data_iface_no)) {
417 sc->sc_iface_index[0] = i;
418 usbd_set_parent_iface(uaa->device, i, uaa->info.bIfaceIndex);
422 device_printf(dev, "no data interface\n");
427 if (usb_test_quirk(uaa, UQ_ASSUME_CM_OVER_DATA)) {
428 sc->sc_cm_over_data = 1;
430 if (sc->sc_cm_cap & USB_CDC_CM_OVER_DATA) {
431 if (sc->sc_acm_cap & USB_CDC_ACM_HAS_FEATURE) {
433 error = umodem_set_comm_feature
434 (uaa->device, sc->sc_ctrl_iface_no,
435 UCDC_ABSTRACT_STATE, UCDC_DATA_MULTIPLEXED);
437 /* ignore any errors */
439 sc->sc_cm_over_data = 1;
442 error = usbd_transfer_setup(uaa->device,
443 sc->sc_iface_index, sc->sc_xfer,
444 umodem_config, UMODEM_N_TRANSFER,
447 device_printf(dev, "Can't setup transfer\n");
451 /* clear stall at first run, if USB host mode */
452 if (uaa->usb_mode == USB_MODE_HOST) {
453 mtx_lock(&sc->sc_mtx);
454 usbd_xfer_set_stall(sc->sc_xfer[UMODEM_BULK_WR]);
455 usbd_xfer_set_stall(sc->sc_xfer[UMODEM_BULK_RD]);
456 mtx_unlock(&sc->sc_mtx);
459 ucom_set_usb_mode(&sc->sc_super_ucom, uaa->usb_mode);
461 error = ucom_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc,
462 &umodem_callback, &sc->sc_mtx);
464 device_printf(dev, "Can't attach com\n");
467 ucom_set_pnpinfo_usb(&sc->sc_super_ucom, dev);
477 umodem_find_data_iface(struct usb_attach_arg *uaa,
478 uint8_t iface_index, uint8_t *p_data_no, uint8_t *p_match_class)
480 struct usb_interface_descriptor *id;
481 struct usb_interface *iface;
483 iface = usbd_get_iface(uaa->device, iface_index);
485 /* check for end of interfaces */
489 id = usbd_get_interface_descriptor(iface);
491 /* check for non-matching interface class */
492 if (id->bInterfaceClass != UICLASS_CDC_DATA ||
493 id->bInterfaceSubClass != UISUBCLASS_DATA) {
494 /* if we got a class match then return */
501 DPRINTFN(11, "Match at index %u\n", iface_index);
503 *p_data_no = id->bInterfaceNumber;
507 umodem_start_read(struct ucom_softc *ucom)
509 struct umodem_softc *sc = ucom->sc_parent;
511 /* start interrupt endpoint, if any */
512 usbd_transfer_start(sc->sc_xfer[UMODEM_INTR_RD]);
514 /* start read endpoint */
515 usbd_transfer_start(sc->sc_xfer[UMODEM_BULK_RD]);
519 umodem_stop_read(struct ucom_softc *ucom)
521 struct umodem_softc *sc = ucom->sc_parent;
523 /* stop interrupt endpoint, if any */
524 usbd_transfer_stop(sc->sc_xfer[UMODEM_INTR_RD]);
526 /* stop read endpoint */
527 usbd_transfer_stop(sc->sc_xfer[UMODEM_BULK_RD]);
531 umodem_start_write(struct ucom_softc *ucom)
533 struct umodem_softc *sc = ucom->sc_parent;
535 usbd_transfer_start(sc->sc_xfer[UMODEM_INTR_WR]);
536 usbd_transfer_start(sc->sc_xfer[UMODEM_BULK_WR]);
540 umodem_stop_write(struct ucom_softc *ucom)
542 struct umodem_softc *sc = ucom->sc_parent;
544 usbd_transfer_stop(sc->sc_xfer[UMODEM_INTR_WR]);
545 usbd_transfer_stop(sc->sc_xfer[UMODEM_BULK_WR]);
549 umodem_get_caps(struct usb_attach_arg *uaa, uint8_t *cm, uint8_t *acm)
551 struct usb_cdc_cm_descriptor *cmd;
552 struct usb_cdc_acm_descriptor *cad;
554 cmd = umodem_get_desc(uaa, UDESC_CS_INTERFACE, UDESCSUB_CDC_CM);
555 if ((cmd == NULL) || (cmd->bLength < sizeof(*cmd))) {
556 DPRINTF("no CM desc (faking one)\n");
557 *cm = USB_CDC_CM_DOES_CM | USB_CDC_CM_OVER_DATA;
559 *cm = cmd->bmCapabilities;
561 cad = umodem_get_desc(uaa, UDESC_CS_INTERFACE, UDESCSUB_CDC_ACM);
562 if ((cad == NULL) || (cad->bLength < sizeof(*cad))) {
563 DPRINTF("no ACM desc\n");
566 *acm = cad->bmCapabilities;
570 umodem_cfg_get_status(struct ucom_softc *ucom, uint8_t *lsr, uint8_t *msr)
572 struct umodem_softc *sc = ucom->sc_parent;
576 /* XXX Note: sc_lsr is always zero */
582 umodem_pre_param(struct ucom_softc *ucom, struct termios *t)
584 return (0); /* we accept anything */
588 umodem_cfg_param(struct ucom_softc *ucom, struct termios *t)
590 struct umodem_softc *sc = ucom->sc_parent;
591 struct usb_cdc_line_state ls;
592 struct usb_device_request req;
594 DPRINTF("sc=%p\n", sc);
596 memset(&ls, 0, sizeof(ls));
598 USETDW(ls.dwDTERate, t->c_ospeed);
600 ls.bCharFormat = (t->c_cflag & CSTOPB) ?
601 UCDC_STOP_BIT_2 : UCDC_STOP_BIT_1;
603 ls.bParityType = (t->c_cflag & PARENB) ?
604 ((t->c_cflag & PARODD) ?
605 UCDC_PARITY_ODD : UCDC_PARITY_EVEN) : UCDC_PARITY_NONE;
607 switch (t->c_cflag & CSIZE) {
622 DPRINTF("rate=%d fmt=%d parity=%d bits=%d\n",
623 UGETDW(ls.dwDTERate), ls.bCharFormat,
624 ls.bParityType, ls.bDataBits);
626 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
627 req.bRequest = UCDC_SET_LINE_CODING;
628 USETW(req.wValue, 0);
629 req.wIndex[0] = sc->sc_ctrl_iface_no;
631 USETW(req.wLength, sizeof(ls));
633 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
638 umodem_ioctl(struct ucom_softc *ucom, uint32_t cmd, caddr_t data,
639 int flag, struct thread *td)
641 struct umodem_softc *sc = ucom->sc_parent;
644 DPRINTF("cmd=0x%08x\n", cmd);
647 case USB_GET_CM_OVER_DATA:
648 *(int *)data = sc->sc_cm_over_data;
651 case USB_SET_CM_OVER_DATA:
652 if (*(int *)data != sc->sc_cm_over_data) {
658 DPRINTF("unknown\n");
667 umodem_cfg_set_dtr(struct ucom_softc *ucom, uint8_t onoff)
669 struct umodem_softc *sc = ucom->sc_parent;
670 struct usb_device_request req;
672 DPRINTF("onoff=%d\n", onoff);
675 sc->sc_line |= UCDC_LINE_DTR;
677 sc->sc_line &= ~UCDC_LINE_DTR;
679 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
680 req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
681 USETW(req.wValue, sc->sc_line);
682 req.wIndex[0] = sc->sc_ctrl_iface_no;
684 USETW(req.wLength, 0);
686 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
687 &req, NULL, 0, 1000);
691 umodem_cfg_set_rts(struct ucom_softc *ucom, uint8_t onoff)
693 struct umodem_softc *sc = ucom->sc_parent;
694 struct usb_device_request req;
696 DPRINTF("onoff=%d\n", onoff);
699 sc->sc_line |= UCDC_LINE_RTS;
701 sc->sc_line &= ~UCDC_LINE_RTS;
703 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
704 req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
705 USETW(req.wValue, sc->sc_line);
706 req.wIndex[0] = sc->sc_ctrl_iface_no;
708 USETW(req.wLength, 0);
710 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
711 &req, NULL, 0, 1000);
715 umodem_cfg_set_break(struct ucom_softc *ucom, uint8_t onoff)
717 struct umodem_softc *sc = ucom->sc_parent;
718 struct usb_device_request req;
721 DPRINTF("onoff=%d\n", onoff);
723 if (sc->sc_acm_cap & USB_CDC_ACM_HAS_BREAK) {
725 temp = onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF;
727 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
728 req.bRequest = UCDC_SEND_BREAK;
729 USETW(req.wValue, temp);
730 req.wIndex[0] = sc->sc_ctrl_iface_no;
732 USETW(req.wLength, 0);
734 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
735 &req, NULL, 0, 1000);
740 umodem_intr_write_callback(struct usb_xfer *xfer, usb_error_t error)
744 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
746 switch (USB_GET_STATE(xfer)) {
747 case USB_ST_TRANSFERRED:
749 DPRINTF("Transferred %d bytes\n", actlen);
757 if (error != USB_ERR_CANCELLED) {
758 /* start clear stall */
759 usbd_xfer_set_stall(xfer);
767 umodem_intr_read_callback(struct usb_xfer *xfer, usb_error_t error)
769 struct usb_cdc_notification pkt;
770 struct umodem_softc *sc = usbd_xfer_softc(xfer);
771 struct usb_page_cache *pc;
775 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
777 switch (USB_GET_STATE(xfer)) {
778 case USB_ST_TRANSFERRED:
781 DPRINTF("received short packet, "
782 "%d bytes\n", actlen);
785 if (actlen > (int)sizeof(pkt)) {
786 DPRINTF("truncating message\n");
787 actlen = sizeof(pkt);
789 pc = usbd_xfer_get_frame(xfer, 0);
790 usbd_copy_out(pc, 0, &pkt, actlen);
794 wLen = UGETW(pkt.wLength);
798 if (pkt.bmRequestType != UCDC_NOTIFICATION) {
799 DPRINTF("unknown message type, "
800 "0x%02x, on notify pipe!\n",
804 switch (pkt.bNotification) {
805 case UCDC_N_SERIAL_STATE:
807 * Set the serial state in ucom driver based on
808 * the bits from the notify message
811 DPRINTF("invalid notification "
812 "length, %d bytes!\n", actlen);
815 DPRINTF("notify bytes = %02x%02x\n",
819 /* Currently, lsr is always zero. */
823 if (pkt.data[0] & UCDC_N_SERIAL_RI) {
824 sc->sc_msr |= SER_RI;
826 if (pkt.data[0] & UCDC_N_SERIAL_DSR) {
827 sc->sc_msr |= SER_DSR;
829 if (pkt.data[0] & UCDC_N_SERIAL_DCD) {
830 sc->sc_msr |= SER_DCD;
832 ucom_status_change(&sc->sc_ucom);
836 DPRINTF("unknown notify message: 0x%02x\n",
843 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
844 usbd_transfer_submit(xfer);
848 if (error != USB_ERR_CANCELLED) {
849 /* try to clear stall first */
850 usbd_xfer_set_stall(xfer);
859 umodem_write_callback(struct usb_xfer *xfer, usb_error_t error)
861 struct umodem_softc *sc = usbd_xfer_softc(xfer);
862 struct usb_page_cache *pc;
865 switch (USB_GET_STATE(xfer)) {
867 case USB_ST_TRANSFERRED:
869 pc = usbd_xfer_get_frame(xfer, 0);
870 if (ucom_get_data(&sc->sc_ucom, pc, 0,
871 UMODEM_BUF_SIZE, &actlen)) {
873 usbd_xfer_set_frame_len(xfer, 0, actlen);
874 usbd_transfer_submit(xfer);
879 if (error != USB_ERR_CANCELLED) {
880 /* try to clear stall first */
881 usbd_xfer_set_stall(xfer);
889 umodem_read_callback(struct usb_xfer *xfer, usb_error_t error)
891 struct umodem_softc *sc = usbd_xfer_softc(xfer);
892 struct usb_page_cache *pc;
895 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
897 switch (USB_GET_STATE(xfer)) {
898 case USB_ST_TRANSFERRED:
900 DPRINTF("actlen=%d\n", actlen);
902 pc = usbd_xfer_get_frame(xfer, 0);
903 ucom_put_data(&sc->sc_ucom, pc, 0, actlen);
907 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
908 usbd_transfer_submit(xfer);
912 if (error != USB_ERR_CANCELLED) {
913 /* try to clear stall first */
914 usbd_xfer_set_stall(xfer);
922 umodem_get_desc(struct usb_attach_arg *uaa, uint8_t type, uint8_t subtype)
924 return (usbd_find_descriptor(uaa->device, NULL, uaa->info.bIfaceIndex,
925 type, 0xFF, subtype, 0xFF));
929 umodem_set_comm_feature(struct usb_device *udev, uint8_t iface_no,
930 uint16_t feature, uint16_t state)
932 struct usb_device_request req;
933 struct usb_cdc_abstract_state ast;
935 DPRINTF("feature=%d state=%d\n",
938 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
939 req.bRequest = UCDC_SET_COMM_FEATURE;
940 USETW(req.wValue, feature);
941 req.wIndex[0] = iface_no;
943 USETW(req.wLength, UCDC_ABSTRACT_STATE_LENGTH);
944 USETW(ast.wState, state);
946 return (usbd_do_request(udev, NULL, &req, &ast));
950 umodem_detach(device_t dev)
952 struct umodem_softc *sc = device_get_softc(dev);
954 DPRINTF("sc=%p\n", sc);
956 ucom_detach(&sc->sc_super_ucom, &sc->sc_ucom);
957 usbd_transfer_unsetup(sc->sc_xfer, UMODEM_N_TRANSFER);
959 device_claim_softc(dev);
961 umodem_free_softc(sc);
966 UCOM_UNLOAD_DRAIN(umodem);
969 umodem_free_softc(struct umodem_softc *sc)
971 if (ucom_unref(&sc->sc_super_ucom)) {
972 mtx_destroy(&sc->sc_mtx);
973 device_free_softc(sc);
978 umodem_free(struct ucom_softc *ucom)
980 umodem_free_softc(ucom->sc_parent);
984 umodem_poll(struct ucom_softc *ucom)
986 struct umodem_softc *sc = ucom->sc_parent;
987 usbd_transfer_poll(sc->sc_xfer, UMODEM_N_TRANSFER);
991 umodem_handle_request(device_t dev,
992 const void *preq, void **pptr, uint16_t *plen,
993 uint16_t offset, uint8_t *pstate)
995 struct umodem_softc *sc = device_get_softc(dev);
996 const struct usb_device_request *req = preq;
997 uint8_t is_complete = *pstate;
999 DPRINTF("sc=%p\n", sc);
1002 if ((req->bmRequestType == UT_WRITE_CLASS_INTERFACE) &&
1003 (req->bRequest == UCDC_SET_LINE_CODING) &&
1004 (req->wIndex[0] == sc->sc_ctrl_iface_no) &&
1005 (req->wIndex[1] == 0x00) &&
1006 (req->wValue[0] == 0x00) &&
1007 (req->wValue[1] == 0x00)) {
1009 *plen = sizeof(sc->sc_line_coding);
1010 *pptr = &sc->sc_line_coding;
1015 } else if ((req->bmRequestType == UT_WRITE_CLASS_INTERFACE) &&
1016 (req->wIndex[0] == sc->sc_ctrl_iface_no) &&
1017 (req->wIndex[1] == 0x00) &&
1018 (req->bRequest == UCDC_SET_COMM_FEATURE)) {
1020 *plen = sizeof(sc->sc_abstract_state);
1021 *pptr = &sc->sc_abstract_state;
1026 } else if ((req->bmRequestType == UT_WRITE_CLASS_INTERFACE) &&
1027 (req->wIndex[0] == sc->sc_ctrl_iface_no) &&
1028 (req->wIndex[1] == 0x00) &&
1029 (req->bRequest == UCDC_SET_CONTROL_LINE_STATE)) {
1032 } else if ((req->bmRequestType == UT_WRITE_CLASS_INTERFACE) &&
1033 (req->wIndex[0] == sc->sc_ctrl_iface_no) &&
1034 (req->wIndex[1] == 0x00) &&
1035 (req->bRequest == UCDC_SEND_BREAK)) {
1040 return (ENXIO); /* use builtin handler */