2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2010 Fredrik Lindberg <fli@shapeshifter.se>
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 ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
32 #include <sys/types.h>
33 #include <sys/sockio.h>
35 #include <sys/malloc.h>
36 #include <sys/kernel.h>
37 #include <sys/module.h>
38 #include <sys/socket.h>
40 #include <sys/sysctl.h>
41 #include <sys/condvar.h>
46 #include <sys/systm.h>
47 #include <sys/limits.h>
49 #include <machine/bus.h>
52 #include <net/if_var.h>
53 #include <net/if_types.h>
54 #include <net/netisr.h>
56 #include <netinet/in.h>
57 #include <netinet/ip.h>
58 #include <netinet/ip6.h>
60 #include <dev/usb/usb.h>
61 #include <dev/usb/usbdi.h>
62 #include <dev/usb/usbdi_util.h>
63 #include <dev/usb/usb_cdc.h>
65 #define USB_DEBUG_VAR uhso_debug
66 #include <dev/usb/usb_debug.h>
67 #include <dev/usb/usb_process.h>
68 #include <dev/usb/usb_busdma.h>
69 #include <dev/usb/usb_msctest.h>
71 #include <dev/usb/serial/usb_serial.h>
74 struct uhso_softc *ht_sc;
75 struct usb_xfer *ht_xfer[3];
76 int ht_muxport; /* Mux. port no */
83 struct usb_device *sc_udev;
85 uint32_t sc_type; /* Interface definition */
88 struct usb_xfer *sc_xfer[3];
90 uint8_t sc_iface_index;
93 struct usb_xfer * sc_ctrl_xfer[2];
94 uint8_t sc_ctrl_iface_no;
97 struct usb_xfer *sc_if_xfer[2];
99 struct mbuf *sc_mwait; /* Partial packet */
100 size_t sc_waitlen; /* No. of outstanding bytes */
101 struct ifqueue sc_rxq;
104 /* TTY related structures */
105 struct ucom_super_softc sc_super_ucom;
107 struct uhso_tty *sc_tty;
108 struct ucom_softc *sc_ucom;
114 #define UHSO_MAX_MTU 2048
117 * There are mainly two type of cards floating around.
118 * The first one has 2,3 or 4 interfaces with a multiplexed serial port
119 * and packet interface on the first interface and bulk serial ports
121 * The second type of card has several other interfaces, their purpose
122 * can be detected during run-time.
124 #define UHSO_IFACE_SPEC(usb_type, port, port_type) \
125 (((usb_type) << 24) | ((port) << 16) | (port_type))
127 #define UHSO_IFACE_USB_TYPE(x) ((x >> 24) & 0xff)
128 #define UHSO_IFACE_PORT(x) ((x >> 16) & 0xff)
129 #define UHSO_IFACE_PORT_TYPE(x) (x & 0xff)
132 * USB interface types
134 #define UHSO_IF_NET 0x01 /* Network packet interface */
135 #define UHSO_IF_MUX 0x02 /* Multiplexed serial port */
136 #define UHSO_IF_BULK 0x04 /* Bulk interface */
141 #define UHSO_PORT_UNKNOWN 0x00
142 #define UHSO_PORT_SERIAL 0x01 /* Serial port */
143 #define UHSO_PORT_NETWORK 0x02 /* Network packet interface */
146 * Multiplexed serial port destination sub-port names
148 #define UHSO_MPORT_TYPE_CTL 0x00 /* Control port */
149 #define UHSO_MPORT_TYPE_APP 0x01 /* Application */
150 #define UHSO_MPORT_TYPE_PCSC 0x02
151 #define UHSO_MPORT_TYPE_GPS 0x03
152 #define UHSO_MPORT_TYPE_APP2 0x04 /* Secondary application */
153 #define UHSO_MPORT_TYPE_MAX UHSO_MPORT_TYPE_APP2
154 #define UHSO_MPORT_TYPE_NOMAX 8 /* Max number of mux ports */
158 * Note that these definitions are arbitrary and do not match the values
159 * returned by the auto config descriptor.
161 #define UHSO_PORT_TYPE_UNKNOWN 0x00
162 #define UHSO_PORT_TYPE_CTL 0x01
163 #define UHSO_PORT_TYPE_APP 0x02
164 #define UHSO_PORT_TYPE_APP2 0x03
165 #define UHSO_PORT_TYPE_MODEM 0x04
166 #define UHSO_PORT_TYPE_NETWORK 0x05
167 #define UHSO_PORT_TYPE_DIAG 0x06
168 #define UHSO_PORT_TYPE_DIAG2 0x07
169 #define UHSO_PORT_TYPE_GPS 0x08
170 #define UHSO_PORT_TYPE_GPSCTL 0x09
171 #define UHSO_PORT_TYPE_PCSC 0x0a
172 #define UHSO_PORT_TYPE_MSD 0x0b
173 #define UHSO_PORT_TYPE_VOICE 0x0c
174 #define UHSO_PORT_TYPE_MAX 0x0c
176 static eventhandler_tag uhso_etag;
178 /* Overall port type */
179 static char *uhso_port[] = {
187 * Map between interface port type read from device and description type.
188 * The position in this array is a direct map to the auto config
191 static unsigned char uhso_port_map[] = {
192 UHSO_PORT_TYPE_UNKNOWN,
195 UHSO_PORT_TYPE_GPSCTL,
199 UHSO_PORT_TYPE_NETWORK,
200 UHSO_PORT_TYPE_MODEM,
205 static char uhso_port_map_max = sizeof(uhso_port_map) / sizeof(char);
207 static unsigned char uhso_mux_port_map[] = {
215 static char *uhso_port_type[] = {
216 "Unknown", /* Not a valid port */
219 "Application (Secondary)",
223 "Diagnostic (Secondary)",
231 static char *uhso_port_type_sysctl[] = {
247 #define UHSO_STATIC_IFACE 0x01
248 #define UHSO_AUTO_IFACE 0x02
250 /* ifnet device unit allocations */
251 static struct unrhdr *uhso_ifnet_unit = NULL;
253 static const STRUCT_USB_HOST_ID uhso_devs[] = {
254 #define UHSO_DEV(v,p,i) { USB_VPI(USB_VENDOR_##v, USB_PRODUCT_##v##_##p, i) }
255 /* Option GlobeTrotter MAX 7.2 with upgraded firmware */
256 UHSO_DEV(OPTION, GTMAX72, UHSO_STATIC_IFACE),
257 /* Option GlobeSurfer iCON 7.2 */
258 UHSO_DEV(OPTION, GSICON72, UHSO_STATIC_IFACE),
259 /* Option iCON 225 */
260 UHSO_DEV(OPTION, GTHSDPA, UHSO_STATIC_IFACE),
261 /* Option GlobeSurfer iCON HSUPA */
262 UHSO_DEV(OPTION, GSICONHSUPA, UHSO_STATIC_IFACE),
263 /* Option GlobeTrotter HSUPA */
264 UHSO_DEV(OPTION, GTHSUPA, UHSO_STATIC_IFACE),
266 UHSO_DEV(OPTION, GE40X, UHSO_AUTO_IFACE),
267 UHSO_DEV(OPTION, GE40X_1, UHSO_AUTO_IFACE),
268 UHSO_DEV(OPTION, GE40X_2, UHSO_AUTO_IFACE),
269 UHSO_DEV(OPTION, GE40X_3, UHSO_AUTO_IFACE),
270 /* Option GlobeSurfer iCON 401 */
271 UHSO_DEV(OPTION, ICON401, UHSO_AUTO_IFACE),
272 /* Option GlobeTrotter Module 382 */
273 UHSO_DEV(OPTION, GMT382, UHSO_AUTO_IFACE),
275 UHSO_DEV(OPTION, GTM661W, UHSO_AUTO_IFACE),
276 /* Option iCON EDGE */
277 UHSO_DEV(OPTION, ICONEDGE, UHSO_STATIC_IFACE),
278 /* Option Module HSxPA */
279 UHSO_DEV(OPTION, MODHSXPA, UHSO_STATIC_IFACE),
280 /* Option iCON 321 */
281 UHSO_DEV(OPTION, ICON321, UHSO_STATIC_IFACE),
282 /* Option iCON 322 */
283 UHSO_DEV(OPTION, GTICON322, UHSO_STATIC_IFACE),
284 /* Option iCON 505 */
285 UHSO_DEV(OPTION, ICON505, UHSO_AUTO_IFACE),
286 /* Option iCON 452 */
287 UHSO_DEV(OPTION, ICON505, UHSO_AUTO_IFACE),
291 static SYSCTL_NODE(_hw_usb, OID_AUTO, uhso, CTLFLAG_RW, 0, "USB uhso");
292 static int uhso_autoswitch = 1;
293 SYSCTL_INT(_hw_usb_uhso, OID_AUTO, auto_switch, CTLFLAG_RWTUN,
294 &uhso_autoswitch, 0, "Automatically switch to modem mode");
298 static int uhso_debug = UHSO_DEBUG;
300 static int uhso_debug = -1;
303 SYSCTL_INT(_hw_usb_uhso, OID_AUTO, debug, CTLFLAG_RWTUN,
304 &uhso_debug, 0, "Debug level");
306 #define UHSO_DPRINTF(n, x, ...) {\
307 if (uhso_debug >= n) {\
308 printf("%s: " x, __func__, ##__VA_ARGS__);\
312 #define UHSO_DPRINTF(n, x, ...)
315 #ifdef UHSO_DEBUG_HEXDUMP
316 # define UHSO_HEXDUMP(_buf, _len) do { \
319 const char *__buf = (const char *)_buf; \
320 for (__tmp = 0; __tmp < _len; __tmp++) \
321 printf("%02hhx ", *__buf++); \
326 # define UHSO_HEXDUMP(_buf, _len)
330 UHSO_MUX_ENDPT_INTR = 0,
347 UHSO_BULK_ENDPT_READ = 0,
348 UHSO_BULK_ENDPT_WRITE,
349 UHSO_BULK_ENDPT_INTR,
353 static usb_callback_t uhso_mux_intr_callback;
354 static usb_callback_t uhso_mux_read_callback;
355 static usb_callback_t uhso_mux_write_callback;
356 static usb_callback_t uhso_bs_read_callback;
357 static usb_callback_t uhso_bs_write_callback;
358 static usb_callback_t uhso_bs_intr_callback;
359 static usb_callback_t uhso_ifnet_read_callback;
360 static usb_callback_t uhso_ifnet_write_callback;
362 /* Config used for the default control pipes */
363 static const struct usb_config uhso_ctrl_config[UHSO_CTRL_MAX] = {
367 .direction = UE_DIR_ANY,
368 .flags = { .pipe_bof = 1, .short_xfer_ok = 1 },
369 .bufsize = sizeof(struct usb_device_request) + 1024,
370 .callback = &uhso_mux_read_callback
373 [UHSO_CTRL_WRITE] = {
376 .direction = UE_DIR_ANY,
377 .flags = { .pipe_bof = 1, .force_short_xfer = 1 },
378 .bufsize = sizeof(struct usb_device_request) + 1024,
380 .callback = &uhso_mux_write_callback
384 /* Config for the multiplexed serial ports */
385 static const struct usb_config uhso_mux_config[UHSO_MUX_ENDPT_MAX] = {
386 [UHSO_MUX_ENDPT_INTR] = {
387 .type = UE_INTERRUPT,
388 .endpoint = UE_ADDR_ANY,
389 .direction = UE_DIR_IN,
390 .flags = { .short_xfer_ok = 1 },
392 .callback = &uhso_mux_intr_callback,
396 /* Config for the raw IP-packet interface */
397 static const struct usb_config uhso_ifnet_config[UHSO_IFNET_MAX] = {
398 [UHSO_IFNET_READ] = {
400 .endpoint = UE_ADDR_ANY,
401 .direction = UE_DIR_IN,
402 .flags = { .pipe_bof = 1, .short_xfer_ok = 1 },
404 .callback = &uhso_ifnet_read_callback
406 [UHSO_IFNET_WRITE] = {
408 .endpoint = UE_ADDR_ANY,
409 .direction = UE_DIR_OUT,
410 .flags = { .pipe_bof = 1, .force_short_xfer = 1 },
412 .timeout = 5 * USB_MS_HZ,
413 .callback = &uhso_ifnet_write_callback
417 /* Config for interfaces with normal bulk serial ports */
418 static const struct usb_config uhso_bs_config[UHSO_BULK_ENDPT_MAX] = {
419 [UHSO_BULK_ENDPT_READ] = {
421 .endpoint = UE_ADDR_ANY,
422 .direction = UE_DIR_IN,
423 .flags = { .pipe_bof = 1, .short_xfer_ok = 1 },
425 .callback = &uhso_bs_read_callback
428 [UHSO_BULK_ENDPT_WRITE] = {
430 .endpoint = UE_ADDR_ANY,
431 .direction = UE_DIR_OUT,
432 .flags = { .pipe_bof = 1, .force_short_xfer = 1 },
434 .callback = &uhso_bs_write_callback
437 [UHSO_BULK_ENDPT_INTR] = {
438 .type = UE_INTERRUPT,
439 .endpoint = UE_ADDR_ANY,
440 .direction = UE_DIR_IN,
441 .flags = { .short_xfer_ok = 1 },
443 .callback = &uhso_bs_intr_callback,
447 static int uhso_probe_iface(struct uhso_softc *, int,
448 int (*probe)(struct usb_device *, int));
449 static int uhso_probe_iface_auto(struct usb_device *, int);
450 static int uhso_probe_iface_static(struct usb_device *, int);
451 static int uhso_attach_muxserial(struct uhso_softc *, struct usb_interface *,
453 static int uhso_attach_bulkserial(struct uhso_softc *, struct usb_interface *,
455 static int uhso_attach_ifnet(struct uhso_softc *, struct usb_interface *,
457 static void uhso_test_autoinst(void *, struct usb_device *,
458 struct usb_attach_arg *);
459 static int uhso_driver_loaded(struct module *, int, void *);
460 static int uhso_radio_sysctl(SYSCTL_HANDLER_ARGS);
461 static int uhso_radio_ctrl(struct uhso_softc *, int);
463 static void uhso_free(struct ucom_softc *);
464 static void uhso_ucom_start_read(struct ucom_softc *);
465 static void uhso_ucom_stop_read(struct ucom_softc *);
466 static void uhso_ucom_start_write(struct ucom_softc *);
467 static void uhso_ucom_stop_write(struct ucom_softc *);
468 static void uhso_ucom_cfg_get_status(struct ucom_softc *, uint8_t *, uint8_t *);
469 static void uhso_ucom_cfg_set_dtr(struct ucom_softc *, uint8_t);
470 static void uhso_ucom_cfg_set_rts(struct ucom_softc *, uint8_t);
471 static void uhso_if_init(void *);
472 static void uhso_if_start(struct ifnet *);
473 static void uhso_if_stop(struct uhso_softc *);
474 static int uhso_if_ioctl(struct ifnet *, u_long, caddr_t);
475 static int uhso_if_output(struct ifnet *, struct mbuf *,
476 const struct sockaddr *, struct route *);
477 static void uhso_if_rxflush(void *);
479 static device_probe_t uhso_probe;
480 static device_attach_t uhso_attach;
481 static device_detach_t uhso_detach;
482 static void uhso_free_softc(struct uhso_softc *);
484 static device_method_t uhso_methods[] = {
485 DEVMETHOD(device_probe, uhso_probe),
486 DEVMETHOD(device_attach, uhso_attach),
487 DEVMETHOD(device_detach, uhso_detach),
491 static driver_t uhso_driver = {
493 .methods = uhso_methods,
494 .size = sizeof(struct uhso_softc)
497 static devclass_t uhso_devclass;
498 DRIVER_MODULE(uhso, uhub, uhso_driver, uhso_devclass, uhso_driver_loaded, 0);
499 MODULE_DEPEND(uhso, ucom, 1, 1, 1);
500 MODULE_DEPEND(uhso, usb, 1, 1, 1);
501 MODULE_VERSION(uhso, 1);
502 USB_PNP_HOST_INFO(uhso_devs);
504 static struct ucom_callback uhso_ucom_callback = {
505 .ucom_cfg_get_status = &uhso_ucom_cfg_get_status,
506 .ucom_cfg_set_dtr = &uhso_ucom_cfg_set_dtr,
507 .ucom_cfg_set_rts = &uhso_ucom_cfg_set_rts,
508 .ucom_start_read = uhso_ucom_start_read,
509 .ucom_stop_read = uhso_ucom_stop_read,
510 .ucom_start_write = uhso_ucom_start_write,
511 .ucom_stop_write = uhso_ucom_stop_write,
512 .ucom_free = &uhso_free,
516 uhso_probe(device_t self)
518 struct usb_attach_arg *uaa = device_get_ivars(self);
521 if (uaa->usb_mode != USB_MODE_HOST)
523 if (uaa->info.bConfigIndex != 0)
525 if (uaa->info.bDeviceClass != 0xff)
528 error = usbd_lookup_id_by_uaa(uhso_devs, sizeof(uhso_devs), uaa);
533 * Probe device to see if we are able to attach
534 * to this interface or not.
536 if (USB_GET_DRIVER_INFO(uaa) == UHSO_AUTO_IFACE) {
537 if (uhso_probe_iface_auto(uaa->device,
538 uaa->info.bIfaceNum) == 0)
545 uhso_attach(device_t self)
547 struct uhso_softc *sc = device_get_softc(self);
548 struct usb_attach_arg *uaa = device_get_ivars(self);
549 struct usb_interface_descriptor *id;
550 struct sysctl_ctx_list *sctx;
551 struct sysctl_oid *soid;
552 struct sysctl_oid *tree = NULL, *tty_node;
553 struct ucom_softc *ucom;
561 sc->sc_udev = uaa->device;
562 mtx_init(&sc->sc_mtx, "uhso", NULL, MTX_DEF);
563 ucom_ref(&sc->sc_super_ucom);
567 id = usbd_get_interface_descriptor(uaa->iface);
568 sc->sc_ctrl_iface_no = id->bInterfaceNumber;
570 sc->sc_iface_no = uaa->info.bIfaceNum;
571 sc->sc_iface_index = uaa->info.bIfaceIndex;
573 /* Setup control pipe */
574 uerr = usbd_transfer_setup(uaa->device,
575 &sc->sc_iface_index, sc->sc_ctrl_xfer,
576 uhso_ctrl_config, UHSO_CTRL_MAX, sc, &sc->sc_mtx);
578 device_printf(self, "Failed to setup control pipe: %s\n",
583 if (USB_GET_DRIVER_INFO(uaa) == UHSO_STATIC_IFACE)
584 probe_f = uhso_probe_iface_static;
585 else if (USB_GET_DRIVER_INFO(uaa) == UHSO_AUTO_IFACE)
586 probe_f = uhso_probe_iface_auto;
590 error = uhso_probe_iface(sc, uaa->info.bIfaceNum, probe_f);
594 sctx = device_get_sysctl_ctx(sc->sc_dev);
595 soid = device_get_sysctl_tree(sc->sc_dev);
597 SYSCTL_ADD_STRING(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "type",
598 CTLFLAG_RD, uhso_port[UHSO_IFACE_PORT(sc->sc_type)], 0,
599 "Port available at this interface");
600 SYSCTL_ADD_PROC(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "radio",
601 CTLTYPE_INT | CTLFLAG_RWTUN, sc, 0, uhso_radio_sysctl, "I", "Enable radio");
604 * The default interface description on most Option devices isn't
605 * very helpful. So we skip device_set_usb_desc and set the
606 * device description manually.
608 device_set_desc_copy(self, uhso_port_type[UHSO_IFACE_PORT_TYPE(sc->sc_type)]);
609 /* Announce device */
610 device_printf(self, "<%s port> at <%s %s> on %s\n",
611 uhso_port_type[UHSO_IFACE_PORT_TYPE(sc->sc_type)],
612 usb_get_manufacturer(uaa->device),
613 usb_get_product(uaa->device),
614 device_get_nameunit(device_get_parent(self)));
616 if (sc->sc_ttys > 0) {
617 SYSCTL_ADD_INT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "ports",
618 CTLFLAG_RD, &sc->sc_ttys, 0, "Number of attached serial ports");
620 tree = SYSCTL_ADD_NODE(sctx, SYSCTL_CHILDREN(soid), OID_AUTO,
621 "port", CTLFLAG_RD, NULL, "Serial ports");
625 * Loop through the number of found TTYs and create sysctl
628 for (i = 0; i < sc->sc_ttys; i++) {
630 ucom = &sc->sc_ucom[i];
632 if (UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_MUX)
633 port = uhso_mux_port_map[ht->ht_muxport];
635 port = UHSO_IFACE_PORT_TYPE(sc->sc_type);
637 desc = uhso_port_type_sysctl[port];
639 tty_node = SYSCTL_ADD_NODE(sctx, SYSCTL_CHILDREN(tree), OID_AUTO,
640 desc, CTLFLAG_RD, NULL, "");
643 if (sc->sc_ttys == 1)
644 snprintf(ht->ht_name, 32, "cuaU%d", ucom->sc_super->sc_unit);
646 snprintf(ht->ht_name, 32, "cuaU%d.%d",
647 ucom->sc_super->sc_unit, ucom->sc_subunit);
650 desc = uhso_port_type[port];
651 SYSCTL_ADD_STRING(sctx, SYSCTL_CHILDREN(tty_node), OID_AUTO,
652 "tty", CTLFLAG_RD, ht->ht_name, 0, "");
653 SYSCTL_ADD_STRING(sctx, SYSCTL_CHILDREN(tty_node), OID_AUTO,
654 "desc", CTLFLAG_RD, desc, 0, "");
657 device_printf(sc->sc_dev,
658 "\"%s\" port at %s\n", desc, ht->ht_name);
663 uhso_detach(sc->sc_dev);
668 uhso_detach(device_t self)
670 struct uhso_softc *sc = device_get_softc(self);
673 usbd_transfer_unsetup(sc->sc_xfer, 3);
674 usbd_transfer_unsetup(sc->sc_ctrl_xfer, UHSO_CTRL_MAX);
675 if (sc->sc_ttys > 0) {
676 ucom_detach(&sc->sc_super_ucom, sc->sc_ucom);
678 for (i = 0; i < sc->sc_ttys; i++) {
679 if (sc->sc_tty[i].ht_muxport != -1) {
680 usbd_transfer_unsetup(sc->sc_tty[i].ht_xfer,
686 if (sc->sc_ifp != NULL) {
687 callout_drain(&sc->sc_c);
688 free_unr(uhso_ifnet_unit, sc->sc_ifp->if_dunit);
689 mtx_lock(&sc->sc_mtx);
691 bpfdetach(sc->sc_ifp);
692 if_detach(sc->sc_ifp);
694 mtx_unlock(&sc->sc_mtx);
695 usbd_transfer_unsetup(sc->sc_if_xfer, UHSO_IFNET_MAX);
698 device_claim_softc(self);
705 UCOM_UNLOAD_DRAIN(uhso);
708 uhso_free_softc(struct uhso_softc *sc)
710 if (ucom_unref(&sc->sc_super_ucom)) {
711 free(sc->sc_tty, M_USBDEV);
712 free(sc->sc_ucom, M_USBDEV);
713 mtx_destroy(&sc->sc_mtx);
714 device_free_softc(sc);
719 uhso_free(struct ucom_softc *ucom)
721 uhso_free_softc(ucom->sc_parent);
725 uhso_test_autoinst(void *arg, struct usb_device *udev,
726 struct usb_attach_arg *uaa)
728 struct usb_interface *iface;
729 struct usb_interface_descriptor *id;
731 if (uaa->dev_state != UAA_DEV_READY || !uhso_autoswitch)
734 iface = usbd_get_iface(udev, 0);
738 if (id == NULL || id->bInterfaceClass != UICLASS_MASS)
740 if (usbd_lookup_id_by_uaa(uhso_devs, sizeof(uhso_devs), uaa))
741 return; /* no device match */
743 if (usb_msc_eject(udev, 0, MSC_EJECT_REZERO) == 0) {
744 /* success, mark the udev as disappearing */
745 uaa->dev_state = UAA_DEV_EJECTING;
750 uhso_driver_loaded(struct module *mod, int what, void *arg)
754 /* register our autoinstall handler */
755 uhso_etag = EVENTHANDLER_REGISTER(usb_dev_configured,
756 uhso_test_autoinst, NULL, EVENTHANDLER_PRI_ANY);
757 /* create our unit allocator for inet devs */
758 uhso_ifnet_unit = new_unrhdr(0, INT_MAX, NULL);
761 EVENTHANDLER_DEREGISTER(usb_dev_configured, uhso_etag);
762 delete_unrhdr(uhso_ifnet_unit);
771 * Probe the interface type by querying the device. The elements
772 * of an array indicates the capabilities of a particular interface.
773 * Returns a bit mask with the interface capabilities.
776 uhso_probe_iface_auto(struct usb_device *udev, int index)
778 struct usb_device_request req;
784 req.bmRequestType = UT_READ_VENDOR_DEVICE;
786 USETW(req.wValue, 0);
787 USETW(req.wIndex, 0);
788 USETW(req.wLength, 17);
790 uerr = usbd_do_request_flags(udev, NULL, &req, buf,
791 0, &actlen, USB_MS_HZ);
793 printf("%s: usbd_do_request_flags failed, %s\n",
794 __func__, usbd_errstr(uerr));
798 UHSO_DPRINTF(1, "actlen=%d\n", actlen);
799 UHSO_HEXDUMP(buf, 17);
801 if (index < 0 || index > 16) {
802 UHSO_DPRINTF(0, "Index %d out of range\n", index);
806 UHSO_DPRINTF(1, "index=%d, type=%x[%s]\n", index, buf[index],
807 uhso_port_type[(int)uhso_port_map[(int)buf[index]]]);
809 if (buf[index] >= uhso_port_map_max)
812 port = uhso_port_map[(int)buf[index]];
815 case UHSO_PORT_TYPE_NETWORK:
816 return (UHSO_IFACE_SPEC(UHSO_IF_NET | UHSO_IF_MUX,
817 UHSO_PORT_SERIAL | UHSO_PORT_NETWORK, port));
818 case UHSO_PORT_TYPE_DIAG:
819 case UHSO_PORT_TYPE_DIAG2:
820 case UHSO_PORT_TYPE_GPS:
821 case UHSO_PORT_TYPE_GPSCTL:
822 case UHSO_PORT_TYPE_CTL:
823 case UHSO_PORT_TYPE_APP:
824 case UHSO_PORT_TYPE_APP2:
825 case UHSO_PORT_TYPE_MODEM:
826 return (UHSO_IFACE_SPEC(UHSO_IF_BULK,
827 UHSO_PORT_SERIAL, port));
828 case UHSO_PORT_TYPE_MSD:
830 case UHSO_PORT_TYPE_UNKNOWN:
839 * Returns the capabilities of interfaces for devices that don't
840 * support the automatic query.
841 * Returns a bit mask with the interface capabilities.
844 uhso_probe_iface_static(struct usb_device *udev, int index)
846 struct usb_config_descriptor *cd;
848 cd = usbd_get_config_descriptor(udev);
849 if (cd->bNumInterface <= 3) {
850 /* Cards with 3 or less interfaces */
853 return UHSO_IFACE_SPEC(UHSO_IF_NET | UHSO_IF_MUX,
854 UHSO_PORT_SERIAL | UHSO_PORT_NETWORK,
855 UHSO_PORT_TYPE_NETWORK);
857 return UHSO_IFACE_SPEC(UHSO_IF_BULK,
858 UHSO_PORT_SERIAL, UHSO_PORT_TYPE_DIAG);
860 return UHSO_IFACE_SPEC(UHSO_IF_BULK,
861 UHSO_PORT_SERIAL, UHSO_PORT_TYPE_MODEM);
864 /* Cards with 4 interfaces */
867 return UHSO_IFACE_SPEC(UHSO_IF_NET | UHSO_IF_MUX,
868 UHSO_PORT_SERIAL | UHSO_PORT_NETWORK,
869 UHSO_PORT_TYPE_NETWORK);
871 return UHSO_IFACE_SPEC(UHSO_IF_BULK,
872 UHSO_PORT_SERIAL, UHSO_PORT_TYPE_DIAG2);
874 return UHSO_IFACE_SPEC(UHSO_IF_BULK,
875 UHSO_PORT_SERIAL, UHSO_PORT_TYPE_MODEM);
877 return UHSO_IFACE_SPEC(UHSO_IF_BULK,
878 UHSO_PORT_SERIAL, UHSO_PORT_TYPE_DIAG);
885 * Probes an interface for its particular capabilities and attaches if
886 * it's a supported interface.
889 uhso_probe_iface(struct uhso_softc *sc, int index,
890 int (*probe)(struct usb_device *, int))
892 struct usb_interface *iface;
895 UHSO_DPRINTF(1, "Probing for interface %d, probe_func=%p\n", index, probe);
897 type = probe(sc->sc_udev, index);
898 UHSO_DPRINTF(1, "Probe result %x\n", type);
903 iface = usbd_get_iface(sc->sc_udev, index);
905 if (UHSO_IFACE_PORT_TYPE(type) == UHSO_PORT_TYPE_NETWORK) {
906 error = uhso_attach_ifnet(sc, iface, type);
908 UHSO_DPRINTF(1, "uhso_attach_ifnet failed");
913 * If there is an additional interrupt endpoint on this
914 * interface then we most likely have a multiplexed serial port
917 if (iface->idesc->bNumEndpoints < 3) {
918 sc->sc_type = UHSO_IFACE_SPEC(
919 UHSO_IFACE_USB_TYPE(type) & ~UHSO_IF_MUX,
920 UHSO_IFACE_PORT(type) & ~UHSO_PORT_SERIAL,
921 UHSO_IFACE_PORT_TYPE(type));
925 UHSO_DPRINTF(1, "Trying to attach mux. serial\n");
926 error = uhso_attach_muxserial(sc, iface, type);
927 if (error == 0 && sc->sc_ttys > 0) {
928 error = ucom_attach(&sc->sc_super_ucom, sc->sc_ucom,
929 sc->sc_ttys, sc, &uhso_ucom_callback, &sc->sc_mtx);
931 device_printf(sc->sc_dev, "ucom_attach failed\n");
934 ucom_set_pnpinfo_usb(&sc->sc_super_ucom, sc->sc_dev);
936 mtx_lock(&sc->sc_mtx);
937 usbd_transfer_start(sc->sc_xfer[UHSO_MUX_ENDPT_INTR]);
938 mtx_unlock(&sc->sc_mtx);
940 } else if ((UHSO_IFACE_USB_TYPE(type) & UHSO_IF_BULK) &&
941 UHSO_IFACE_PORT(type) & UHSO_PORT_SERIAL) {
943 error = uhso_attach_bulkserial(sc, iface, type);
947 error = ucom_attach(&sc->sc_super_ucom, sc->sc_ucom,
948 sc->sc_ttys, sc, &uhso_ucom_callback, &sc->sc_mtx);
950 device_printf(sc->sc_dev, "ucom_attach failed\n");
953 ucom_set_pnpinfo_usb(&sc->sc_super_ucom, sc->sc_dev);
956 UHSO_DPRINTF(0, "Unknown type %x\n", type);
964 uhso_radio_ctrl(struct uhso_softc *sc, int onoff)
966 struct usb_device_request req;
969 req.bmRequestType = UT_VENDOR;
970 req.bRequest = onoff ? 0x82 : 0x81;
971 USETW(req.wValue, 0);
972 USETW(req.wIndex, 0);
973 USETW(req.wLength, 0);
975 uerr = usbd_do_request(sc->sc_udev, NULL, &req, NULL);
977 device_printf(sc->sc_dev, "usbd_do_request_flags failed: %s\n",
985 uhso_radio_sysctl(SYSCTL_HANDLER_ARGS)
987 struct uhso_softc *sc = arg1;
990 radio = sc->sc_radio;
991 error = sysctl_handle_int(oidp, &radio, 0, req);
994 if (radio != sc->sc_radio) {
995 radio = radio != 0 ? 1 : 0;
996 error = uhso_radio_ctrl(sc, radio);
998 sc->sc_radio = radio;
1005 * Expands allocated memory to fit an additional TTY.
1006 * Two arrays are kept with matching indexes, one for ucom and one
1007 * for our private data.
1010 uhso_alloc_tty(struct uhso_softc *sc)
1014 sc->sc_tty = reallocf(sc->sc_tty, sizeof(struct uhso_tty) * sc->sc_ttys,
1015 M_USBDEV, M_WAITOK | M_ZERO);
1016 if (sc->sc_tty == NULL)
1019 sc->sc_ucom = reallocf(sc->sc_ucom,
1020 sizeof(struct ucom_softc) * sc->sc_ttys, M_USBDEV, M_WAITOK | M_ZERO);
1021 if (sc->sc_ucom == NULL)
1024 sc->sc_tty[sc->sc_ttys - 1].ht_sc = sc;
1026 UHSO_DPRINTF(1, "Allocated TTY %d\n", sc->sc_ttys - 1);
1027 return (sc->sc_ttys - 1);
1031 * Attach a multiplexed serial port
1032 * Data is read/written with requests on the default control pipe. An interrupt
1033 * endpoint returns when there is new data to be read.
1036 uhso_attach_muxserial(struct uhso_softc *sc, struct usb_interface *iface,
1039 struct usb_descriptor *desc;
1044 * The class specific interface (type 0x24) descriptor subtype field
1045 * contains a bitmask that specifies which (and how many) ports that
1046 * are available through this multiplexed serial port.
1048 desc = usbd_find_descriptor(sc->sc_udev, NULL,
1049 iface->idesc->bInterfaceNumber, UDESC_CS_INTERFACE, 0xff, 0, 0);
1051 UHSO_DPRINTF(0, "Failed to find UDESC_CS_INTERFACE\n");
1055 UHSO_DPRINTF(1, "Mux port mask %x\n", desc->bDescriptorSubtype);
1056 if (desc->bDescriptorSubtype == 0)
1060 * The bitmask is one octet, loop through the number of
1061 * bits that are set and create a TTY for each.
1063 for (i = 0; i < 8; i++) {
1065 if ((port & desc->bDescriptorSubtype) == port) {
1066 UHSO_DPRINTF(2, "Found mux port %x (%d)\n", port, i);
1067 tty = uhso_alloc_tty(sc);
1070 sc->sc_tty[tty].ht_muxport = i;
1071 uerr = usbd_transfer_setup(sc->sc_udev,
1072 &sc->sc_iface_index, sc->sc_tty[tty].ht_xfer,
1073 uhso_ctrl_config, UHSO_CTRL_MAX, sc, &sc->sc_mtx);
1075 device_printf(sc->sc_dev,
1076 "Failed to setup control pipe: %s\n",
1083 /* Setup the intr. endpoint */
1084 uerr = usbd_transfer_setup(sc->sc_udev,
1085 &iface->idesc->bInterfaceNumber, sc->sc_xfer,
1086 uhso_mux_config, 1, sc, &sc->sc_mtx);
1094 * Interrupt callback for the multiplexed serial port. Indicates
1095 * which serial port has data waiting.
1098 uhso_mux_intr_callback(struct usb_xfer *xfer, usb_error_t error)
1100 struct usb_page_cache *pc;
1101 struct usb_page_search res;
1102 struct uhso_softc *sc = usbd_xfer_softc(xfer);
1103 unsigned int i, mux;
1105 UHSO_DPRINTF(3, "status %d\n", USB_GET_STATE(xfer));
1107 switch (USB_GET_STATE(xfer)) {
1108 case USB_ST_TRANSFERRED:
1110 * The multiplexed port number can be found at the first byte.
1111 * It contains a bit mask, we transform this in to an integer.
1113 pc = usbd_xfer_get_frame(xfer, 0);
1114 usbd_get_page(pc, 0, &res);
1116 i = *((unsigned char *)res.buffer);
1122 UHSO_DPRINTF(3, "mux port %d (%d)\n", mux, i);
1123 if (mux > UHSO_MPORT_TYPE_NOMAX)
1126 /* Issue a read for this serial port */
1128 sc->sc_tty[mux].ht_xfer[UHSO_CTRL_READ],
1130 usbd_transfer_start(sc->sc_tty[mux].ht_xfer[UHSO_CTRL_READ]);
1135 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
1136 usbd_transfer_submit(xfer);
1139 UHSO_DPRINTF(0, "error: %s\n", usbd_errstr(error));
1140 if (error == USB_ERR_CANCELLED)
1143 usbd_xfer_set_stall(xfer);
1149 uhso_mux_read_callback(struct usb_xfer *xfer, usb_error_t error)
1151 struct uhso_softc *sc = usbd_xfer_softc(xfer);
1152 struct usb_page_cache *pc;
1153 struct usb_device_request req;
1154 struct uhso_tty *ht;
1157 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
1159 UHSO_DPRINTF(3, "status %d\n", USB_GET_STATE(xfer));
1161 ht = usbd_xfer_get_priv(xfer);
1162 UHSO_DPRINTF(3, "ht=%p open=%d\n", ht, ht->ht_open);
1164 switch (USB_GET_STATE(xfer)) {
1165 case USB_ST_TRANSFERRED:
1166 /* Got data, send to ucom */
1167 pc = usbd_xfer_get_frame(xfer, 1);
1168 len = usbd_xfer_frame_len(xfer, 1);
1170 UHSO_DPRINTF(3, "got %d bytes on mux port %d\n", len,
1173 usbd_transfer_start(sc->sc_xfer[UHSO_MUX_ENDPT_INTR]);
1177 /* Deliver data if the TTY is open, discard otherwise */
1179 ucom_put_data(&sc->sc_ucom[ht->ht_muxport], pc, 0, len);
1183 memset(&req, 0, sizeof(struct usb_device_request));
1184 req.bmRequestType = UT_READ_CLASS_INTERFACE;
1185 req.bRequest = UCDC_GET_ENCAPSULATED_RESPONSE;
1186 USETW(req.wValue, 0);
1187 USETW(req.wIndex, ht->ht_muxport);
1188 USETW(req.wLength, 1024);
1190 pc = usbd_xfer_get_frame(xfer, 0);
1191 usbd_copy_in(pc, 0, &req, sizeof(req));
1193 usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
1194 usbd_xfer_set_frame_len(xfer, 1, 1024);
1195 usbd_xfer_set_frames(xfer, 2);
1196 usbd_transfer_submit(xfer);
1199 UHSO_DPRINTF(0, "error: %s\n", usbd_errstr(error));
1200 if (error == USB_ERR_CANCELLED)
1202 usbd_xfer_set_stall(xfer);
1208 uhso_mux_write_callback(struct usb_xfer *xfer, usb_error_t error)
1210 struct uhso_softc *sc = usbd_xfer_softc(xfer);
1211 struct uhso_tty *ht;
1212 struct usb_page_cache *pc;
1213 struct usb_device_request req;
1215 struct usb_page_search res;
1217 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
1219 ht = usbd_xfer_get_priv(xfer);
1220 UHSO_DPRINTF(3, "status=%d, using mux port %d\n",
1221 USB_GET_STATE(xfer), ht->ht_muxport);
1223 switch (USB_GET_STATE(xfer)) {
1224 case USB_ST_TRANSFERRED:
1225 UHSO_DPRINTF(3, "wrote %zd data bytes to muxport %d\n",
1226 actlen - sizeof(struct usb_device_request) ,
1231 pc = usbd_xfer_get_frame(xfer, 1);
1232 if (ucom_get_data(&sc->sc_ucom[ht->ht_muxport], pc,
1235 usbd_get_page(pc, 0, &res);
1237 memset(&req, 0, sizeof(struct usb_device_request));
1238 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
1239 req.bRequest = UCDC_SEND_ENCAPSULATED_COMMAND;
1240 USETW(req.wValue, 0);
1241 USETW(req.wIndex, ht->ht_muxport);
1242 USETW(req.wLength, actlen);
1244 pc = usbd_xfer_get_frame(xfer, 0);
1245 usbd_copy_in(pc, 0, &req, sizeof(req));
1247 usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
1248 usbd_xfer_set_frame_len(xfer, 1, actlen);
1249 usbd_xfer_set_frames(xfer, 2);
1251 UHSO_DPRINTF(3, "Prepared %d bytes for transmit "
1252 "on muxport %d\n", actlen, ht->ht_muxport);
1254 usbd_transfer_submit(xfer);
1258 UHSO_DPRINTF(0, "error: %s\n", usbd_errstr(error));
1259 if (error == USB_ERR_CANCELLED)
1261 usbd_xfer_set_stall(xfer);
1267 uhso_attach_bulkserial(struct uhso_softc *sc, struct usb_interface *iface,
1273 /* Try attaching RD/WR/INTR first */
1274 uerr = usbd_transfer_setup(sc->sc_udev,
1275 &iface->idesc->bInterfaceNumber, sc->sc_xfer,
1276 uhso_bs_config, UHSO_BULK_ENDPT_MAX, sc, &sc->sc_mtx);
1278 /* Try only RD/WR */
1279 uerr = usbd_transfer_setup(sc->sc_udev,
1280 &iface->idesc->bInterfaceNumber, sc->sc_xfer,
1281 uhso_bs_config, UHSO_BULK_ENDPT_MAX - 1, sc, &sc->sc_mtx);
1284 UHSO_DPRINTF(0, "usbd_transfer_setup failed");
1288 tty = uhso_alloc_tty(sc);
1290 usbd_transfer_unsetup(sc->sc_xfer, UHSO_BULK_ENDPT_MAX);
1294 sc->sc_tty[tty].ht_muxport = -1;
1299 uhso_bs_read_callback(struct usb_xfer *xfer, usb_error_t error)
1301 struct uhso_softc *sc = usbd_xfer_softc(xfer);
1302 struct usb_page_cache *pc;
1305 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
1307 UHSO_DPRINTF(3, "status %d, actlen=%d\n", USB_GET_STATE(xfer), actlen);
1309 switch (USB_GET_STATE(xfer)) {
1310 case USB_ST_TRANSFERRED:
1311 pc = usbd_xfer_get_frame(xfer, 0);
1312 ucom_put_data(&sc->sc_ucom[0], pc, 0, actlen);
1316 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
1317 usbd_transfer_submit(xfer);
1320 UHSO_DPRINTF(0, "error: %s\n", usbd_errstr(error));
1321 if (error == USB_ERR_CANCELLED)
1323 usbd_xfer_set_stall(xfer);
1329 uhso_bs_write_callback(struct usb_xfer *xfer, usb_error_t error)
1331 struct uhso_softc *sc = usbd_xfer_softc(xfer);
1332 struct usb_page_cache *pc;
1335 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
1337 UHSO_DPRINTF(3, "status %d, actlen=%d\n", USB_GET_STATE(xfer), actlen);
1339 switch (USB_GET_STATE(xfer)) {
1340 case USB_ST_TRANSFERRED:
1343 pc = usbd_xfer_get_frame(xfer, 0);
1344 if (ucom_get_data(&sc->sc_ucom[0], pc, 0, 8192, &actlen)) {
1345 usbd_xfer_set_frame_len(xfer, 0, actlen);
1346 usbd_transfer_submit(xfer);
1351 UHSO_DPRINTF(0, "error: %s\n", usbd_errstr(error));
1352 if (error == USB_ERR_CANCELLED)
1354 usbd_xfer_set_stall(xfer);
1360 uhso_bs_cfg(struct uhso_softc *sc)
1362 struct usb_device_request req;
1365 if (!(UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_BULK))
1368 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
1369 req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
1370 USETW(req.wValue, sc->sc_line);
1371 USETW(req.wIndex, sc->sc_iface_no);
1372 USETW(req.wLength, 0);
1374 uerr = ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom[0], &req, NULL, 0, 1000);
1376 device_printf(sc->sc_dev, "failed to set ctrl line state to "
1377 "0x%02x: %s\n", sc->sc_line, usbd_errstr(uerr));
1382 uhso_bs_intr_callback(struct usb_xfer *xfer, usb_error_t error)
1384 struct uhso_softc *sc = usbd_xfer_softc(xfer);
1385 struct usb_page_cache *pc;
1387 struct usb_cdc_notification cdc;
1389 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
1390 UHSO_DPRINTF(3, "status %d, actlen=%d\n", USB_GET_STATE(xfer), actlen);
1392 switch (USB_GET_STATE(xfer)) {
1393 case USB_ST_TRANSFERRED:
1394 if (actlen < UCDC_NOTIFICATION_LENGTH) {
1395 UHSO_DPRINTF(0, "UCDC notification too short: %d\n", actlen);
1398 else if (actlen > (int)sizeof(struct usb_cdc_notification)) {
1399 UHSO_DPRINTF(0, "UCDC notification too large: %d\n", actlen);
1400 actlen = sizeof(struct usb_cdc_notification);
1403 pc = usbd_xfer_get_frame(xfer, 0);
1404 usbd_copy_out(pc, 0, &cdc, actlen);
1406 if (UGETW(cdc.wIndex) != sc->sc_iface_no) {
1407 UHSO_DPRINTF(0, "Interface mismatch, got %d expected %d\n",
1408 UGETW(cdc.wIndex), sc->sc_iface_no);
1412 if (cdc.bmRequestType == UCDC_NOTIFICATION &&
1413 cdc.bNotification == UCDC_N_SERIAL_STATE) {
1414 UHSO_DPRINTF(2, "notify = 0x%02x\n", cdc.data[0]);
1418 if (cdc.data[0] & UCDC_N_SERIAL_RI)
1419 sc->sc_msr |= SER_RI;
1420 if (cdc.data[0] & UCDC_N_SERIAL_DSR)
1421 sc->sc_msr |= SER_DSR;
1422 if (cdc.data[0] & UCDC_N_SERIAL_DCD)
1423 sc->sc_msr |= SER_DCD;
1425 ucom_status_change(&sc->sc_ucom[0]);
1430 if (error == USB_ERR_CANCELLED)
1432 usbd_xfer_set_stall(xfer);
1438 uhso_ucom_cfg_get_status(struct ucom_softc *ucom, uint8_t *lsr, uint8_t *msr)
1440 struct uhso_softc *sc = ucom->sc_parent;
1447 uhso_ucom_cfg_set_dtr(struct ucom_softc *ucom, uint8_t onoff)
1449 struct uhso_softc *sc = ucom->sc_parent;
1451 if (!(UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_BULK))
1455 sc->sc_line |= UCDC_LINE_DTR;
1457 sc->sc_line &= ~UCDC_LINE_DTR;
1463 uhso_ucom_cfg_set_rts(struct ucom_softc *ucom, uint8_t onoff)
1465 struct uhso_softc *sc = ucom->sc_parent;
1467 if (!(UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_BULK))
1471 sc->sc_line |= UCDC_LINE_RTS;
1473 sc->sc_line &= ~UCDC_LINE_RTS;
1479 uhso_ucom_start_read(struct ucom_softc *ucom)
1481 struct uhso_softc *sc = ucom->sc_parent;
1483 UHSO_DPRINTF(3, "unit=%d, subunit=%d\n",
1484 ucom->sc_super->sc_unit, ucom->sc_subunit);
1486 if (UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_MUX) {
1487 sc->sc_tty[ucom->sc_subunit].ht_open = 1;
1488 usbd_transfer_start(sc->sc_xfer[UHSO_MUX_ENDPT_INTR]);
1490 else if (UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_BULK) {
1491 sc->sc_tty[0].ht_open = 1;
1492 usbd_transfer_start(sc->sc_xfer[UHSO_BULK_ENDPT_READ]);
1493 if (sc->sc_xfer[UHSO_BULK_ENDPT_INTR] != NULL)
1494 usbd_transfer_start(sc->sc_xfer[UHSO_BULK_ENDPT_INTR]);
1499 uhso_ucom_stop_read(struct ucom_softc *ucom)
1502 struct uhso_softc *sc = ucom->sc_parent;
1504 if (UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_MUX) {
1505 sc->sc_tty[ucom->sc_subunit].ht_open = 0;
1507 sc->sc_tty[ucom->sc_subunit].ht_xfer[UHSO_CTRL_READ]);
1509 else if (UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_BULK) {
1510 sc->sc_tty[0].ht_open = 0;
1511 usbd_transfer_start(sc->sc_xfer[UHSO_BULK_ENDPT_READ]);
1512 if (sc->sc_xfer[UHSO_BULK_ENDPT_INTR] != NULL)
1513 usbd_transfer_stop(sc->sc_xfer[UHSO_BULK_ENDPT_INTR]);
1518 uhso_ucom_start_write(struct ucom_softc *ucom)
1520 struct uhso_softc *sc = ucom->sc_parent;
1522 if (UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_MUX) {
1523 UHSO_DPRINTF(3, "local unit %d\n", ucom->sc_subunit);
1525 usbd_transfer_start(sc->sc_xfer[UHSO_MUX_ENDPT_INTR]);
1528 sc->sc_tty[ucom->sc_subunit].ht_xfer[UHSO_CTRL_WRITE],
1529 &sc->sc_tty[ucom->sc_subunit]);
1530 usbd_transfer_start(
1531 sc->sc_tty[ucom->sc_subunit].ht_xfer[UHSO_CTRL_WRITE]);
1534 else if (UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_BULK) {
1535 usbd_transfer_start(sc->sc_xfer[UHSO_BULK_ENDPT_WRITE]);
1540 uhso_ucom_stop_write(struct ucom_softc *ucom)
1542 struct uhso_softc *sc = ucom->sc_parent;
1544 if (UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_MUX) {
1546 sc->sc_tty[ucom->sc_subunit].ht_xfer[UHSO_CTRL_WRITE]);
1548 else if (UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_BULK) {
1549 usbd_transfer_stop(sc->sc_xfer[UHSO_BULK_ENDPT_WRITE]);
1554 uhso_attach_ifnet(struct uhso_softc *sc, struct usb_interface *iface, int type)
1558 struct sysctl_ctx_list *sctx;
1559 struct sysctl_oid *soid;
1560 unsigned int devunit;
1562 uerr = usbd_transfer_setup(sc->sc_udev,
1563 &iface->idesc->bInterfaceNumber, sc->sc_if_xfer,
1564 uhso_ifnet_config, UHSO_IFNET_MAX, sc, &sc->sc_mtx);
1566 UHSO_DPRINTF(0, "usbd_transfer_setup failed: %s\n",
1571 sc->sc_ifp = ifp = if_alloc(IFT_OTHER);
1572 if (sc->sc_ifp == NULL) {
1573 device_printf(sc->sc_dev, "if_alloc() failed\n");
1577 callout_init_mtx(&sc->sc_c, &sc->sc_mtx, 0);
1578 mtx_lock(&sc->sc_mtx);
1579 callout_reset(&sc->sc_c, 1, uhso_if_rxflush, sc);
1580 mtx_unlock(&sc->sc_mtx);
1583 * We create our own unit numbers for ifnet devices because the
1584 * USB interface unit numbers can be at arbitrary positions yielding
1585 * odd looking device names.
1587 devunit = alloc_unr(uhso_ifnet_unit);
1589 if_initname(ifp, device_get_name(sc->sc_dev), devunit);
1590 ifp->if_mtu = UHSO_MAX_MTU;
1591 ifp->if_ioctl = uhso_if_ioctl;
1592 ifp->if_init = uhso_if_init;
1593 ifp->if_start = uhso_if_start;
1594 ifp->if_output = uhso_if_output;
1595 ifp->if_flags = IFF_BROADCAST | IFF_MULTICAST | IFF_NOARP;
1597 IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
1598 ifp->if_snd.ifq_drv_maxlen = ifqmaxlen;
1599 IFQ_SET_READY(&ifp->if_snd);
1602 bpfattach(ifp, DLT_RAW, 0);
1604 sctx = device_get_sysctl_ctx(sc->sc_dev);
1605 soid = device_get_sysctl_tree(sc->sc_dev);
1606 /* Unlocked read... */
1607 SYSCTL_ADD_STRING(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "netif",
1608 CTLFLAG_RD, ifp->if_xname, 0, "Attached network interface");
1614 uhso_ifnet_read_callback(struct usb_xfer *xfer, usb_error_t error)
1616 struct uhso_softc *sc = usbd_xfer_softc(xfer);
1618 struct usb_page_cache *pc;
1621 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
1623 UHSO_DPRINTF(3, "status=%d, actlen=%d\n", USB_GET_STATE(xfer), actlen);
1625 switch (USB_GET_STATE(xfer)) {
1626 case USB_ST_TRANSFERRED:
1627 if (actlen > 0 && (sc->sc_ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1628 pc = usbd_xfer_get_frame(xfer, 0);
1629 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
1630 usbd_copy_out(pc, 0, mtod(m, uint8_t *), actlen);
1631 m->m_pkthdr.len = m->m_len = actlen;
1632 /* Enqueue frame for further processing */
1633 _IF_ENQUEUE(&sc->sc_rxq, m);
1634 if (!callout_pending(&sc->sc_c) ||
1635 !callout_active(&sc->sc_c)) {
1636 callout_schedule(&sc->sc_c, 1);
1642 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
1643 usbd_transfer_submit(xfer);
1646 UHSO_DPRINTF(0, "error: %s\n", usbd_errstr(error));
1647 if (error == USB_ERR_CANCELLED)
1649 usbd_xfer_set_stall(xfer);
1655 * Deferred RX processing, called with mutex locked.
1657 * Each frame we receive might contain several small ip-packets as well
1658 * as partial ip-packets. We need to separate/assemble them into individual
1659 * packets before sending them to the ip-layer.
1662 uhso_if_rxflush(void *arg)
1664 struct uhso_softc *sc = arg;
1665 struct ifnet *ifp = sc->sc_ifp;
1667 struct mbuf *m, *m0, *mwait;
1670 struct ip6_hdr *ip6;
1676 mwait = sc->sc_mwait;
1679 _IF_DEQUEUE(&sc->sc_rxq, m);
1682 UHSO_DPRINTF(3, "dequeue m=%p, len=%d\n", m, m->m_len);
1684 mtx_unlock(&sc->sc_mtx);
1686 /* Do we have a partial packet waiting? */
1687 if (mwait != NULL) {
1691 UHSO_DPRINTF(3, "partial m0=%p(%d), concat w/ m=%p(%d)\n",
1692 m0, m0->m_len, m, m->m_len);
1695 m = m_pullup(m0, sizeof(struct ip));
1697 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1698 UHSO_DPRINTF(0, "m_pullup failed\n");
1699 mtx_lock(&sc->sc_mtx);
1702 UHSO_DPRINTF(3, "Constructed mbuf=%p, len=%d\n",
1703 m, m->m_pkthdr.len);
1706 cp = mtod(m, uint8_t *);
1707 ip = (struct ip *)cp;
1709 ip6 = (struct ip6_hdr *)cp;
1712 /* Check for IPv4 */
1713 if (ip->ip_v == IPVERSION) {
1714 iplen = htons(ip->ip_len);
1718 /* Check for IPv6 */
1719 else if ((ip6->ip6_vfc & IPV6_VERSION_MASK) == IPV6_VERSION) {
1720 iplen = htons(ip6->ip6_plen);
1725 UHSO_DPRINTF(0, "got unexpected ip version %d, "
1726 "m=%p, len=%d\n", (*cp & 0xf0) >> 4, m, m->m_len);
1727 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1728 UHSO_HEXDUMP(cp, 4);
1731 mtx_lock(&sc->sc_mtx);
1736 UHSO_DPRINTF(0, "Zero IP length\n");
1737 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1740 mtx_lock(&sc->sc_mtx);
1744 UHSO_DPRINTF(3, "m=%p, len=%d, cp=%p, iplen=%d\n",
1745 m, m->m_pkthdr.len, cp, iplen);
1749 /* More IP packets in this mbuf */
1750 if (iplen < m->m_pkthdr.len) {
1754 * Allocate a new mbuf for this IP packet and
1755 * copy the IP-packet into it.
1757 m = m_getcl(M_WAITOK, MT_DATA, M_PKTHDR);
1758 memcpy(mtod(m, uint8_t *), mtod(m0, uint8_t *), iplen);
1759 m->m_pkthdr.len = m->m_len = iplen;
1761 /* Adjust the size of the original mbuf */
1763 m0 = m_defrag(m0, M_WAITOK);
1765 UHSO_DPRINTF(3, "New mbuf=%p, len=%d/%d, m0=%p, "
1766 "m0_len=%d/%d\n", m, m->m_pkthdr.len, m->m_len,
1767 m0, m0->m_pkthdr.len, m0->m_len);
1769 else if (iplen > m->m_pkthdr.len) {
1770 UHSO_DPRINTF(3, "Deferred mbuf=%p, len=%d\n",
1771 m, m->m_pkthdr.len);
1774 mtx_lock(&sc->sc_mtx);
1778 if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
1779 m->m_pkthdr.rcvif = ifp;
1781 /* Dispatch to IP layer */
1782 BPF_MTAP(sc->sc_ifp, m);
1783 M_SETFIB(m, ifp->if_fib);
1784 netisr_dispatch(isr, m);
1785 m = m0 != NULL ? m0 : NULL;
1786 mtx_lock(&sc->sc_mtx);
1788 sc->sc_mwait = mwait;
1792 uhso_ifnet_write_callback(struct usb_xfer *xfer, usb_error_t error)
1794 struct uhso_softc *sc = usbd_xfer_softc(xfer);
1795 struct ifnet *ifp = sc->sc_ifp;
1796 struct usb_page_cache *pc;
1800 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
1802 UHSO_DPRINTF(3, "status %d, actlen=%d\n", USB_GET_STATE(xfer), actlen);
1804 switch (USB_GET_STATE(xfer)) {
1805 case USB_ST_TRANSFERRED:
1806 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
1807 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1810 IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
1814 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1816 if (m->m_pkthdr.len > MCLBYTES)
1817 m->m_pkthdr.len = MCLBYTES;
1819 usbd_xfer_set_frame_len(xfer, 0, m->m_pkthdr.len);
1820 pc = usbd_xfer_get_frame(xfer, 0);
1821 usbd_m_copy_in(pc, 0, m, 0, m->m_pkthdr.len);
1822 usbd_transfer_submit(xfer);
1828 UHSO_DPRINTF(0, "error: %s\n", usbd_errstr(error));
1829 if (error == USB_ERR_CANCELLED)
1831 usbd_xfer_set_stall(xfer);
1837 uhso_if_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1839 struct uhso_softc *sc;
1845 if (ifp->if_flags & IFF_UP) {
1846 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1851 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1852 mtx_lock(&sc->sc_mtx);
1854 mtx_unlock(&sc->sc_mtx);
1869 uhso_if_init(void *priv)
1871 struct uhso_softc *sc = priv;
1872 struct ifnet *ifp = sc->sc_ifp;
1874 mtx_lock(&sc->sc_mtx);
1877 ifp->if_flags |= IFF_UP;
1878 ifp->if_drv_flags |= IFF_DRV_RUNNING;
1879 mtx_unlock(&sc->sc_mtx);
1881 UHSO_DPRINTF(2, "ifnet initialized\n");
1885 uhso_if_output(struct ifnet *ifp, struct mbuf *m0, const struct sockaddr *dst,
1890 /* Only IPv4/6 support */
1891 if (dst->sa_family != AF_INET
1893 && dst->sa_family != AF_INET6
1896 return (EAFNOSUPPORT);
1899 error = (ifp->if_transmit)(ifp, m0);
1901 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1904 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
1909 uhso_if_start(struct ifnet *ifp)
1911 struct uhso_softc *sc = ifp->if_softc;
1913 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
1914 UHSO_DPRINTF(1, "Not running\n");
1918 mtx_lock(&sc->sc_mtx);
1919 usbd_transfer_start(sc->sc_if_xfer[UHSO_IFNET_READ]);
1920 usbd_transfer_start(sc->sc_if_xfer[UHSO_IFNET_WRITE]);
1921 mtx_unlock(&sc->sc_mtx);
1922 UHSO_DPRINTF(3, "interface started\n");
1926 uhso_if_stop(struct uhso_softc *sc)
1929 usbd_transfer_stop(sc->sc_if_xfer[UHSO_IFNET_READ]);
1930 usbd_transfer_stop(sc->sc_if_xfer[UHSO_IFNET_WRITE]);
1931 sc->sc_ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);