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/eventhandler.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 */
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 | CTLFLAG_MPSAFE, 0,
293 static int uhso_autoswitch = 1;
294 SYSCTL_INT(_hw_usb_uhso, OID_AUTO, auto_switch, CTLFLAG_RWTUN,
295 &uhso_autoswitch, 0, "Automatically switch to modem mode");
299 static int uhso_debug = UHSO_DEBUG;
301 static int uhso_debug = -1;
304 SYSCTL_INT(_hw_usb_uhso, OID_AUTO, debug, CTLFLAG_RWTUN,
305 &uhso_debug, 0, "Debug level");
307 #define UHSO_DPRINTF(n, x, ...) {\
308 if (uhso_debug >= n) {\
309 printf("%s: " x, __func__, ##__VA_ARGS__);\
313 #define UHSO_DPRINTF(n, x, ...)
316 #ifdef UHSO_DEBUG_HEXDUMP
317 # define UHSO_HEXDUMP(_buf, _len) do { \
320 const char *__buf = (const char *)_buf; \
321 for (__tmp = 0; __tmp < _len; __tmp++) \
322 printf("%02hhx ", *__buf++); \
327 # define UHSO_HEXDUMP(_buf, _len)
331 UHSO_MUX_ENDPT_INTR = 0,
348 UHSO_BULK_ENDPT_READ = 0,
349 UHSO_BULK_ENDPT_WRITE,
350 UHSO_BULK_ENDPT_INTR,
354 static usb_callback_t uhso_mux_intr_callback;
355 static usb_callback_t uhso_mux_read_callback;
356 static usb_callback_t uhso_mux_write_callback;
357 static usb_callback_t uhso_bs_read_callback;
358 static usb_callback_t uhso_bs_write_callback;
359 static usb_callback_t uhso_bs_intr_callback;
360 static usb_callback_t uhso_ifnet_read_callback;
361 static usb_callback_t uhso_ifnet_write_callback;
363 /* Config used for the default control pipes */
364 static const struct usb_config uhso_ctrl_config[UHSO_CTRL_MAX] = {
368 .direction = UE_DIR_ANY,
369 .flags = { .pipe_bof = 1, .short_xfer_ok = 1 },
370 .bufsize = sizeof(struct usb_device_request) + 1024,
371 .callback = &uhso_mux_read_callback
374 [UHSO_CTRL_WRITE] = {
377 .direction = UE_DIR_ANY,
378 .flags = { .pipe_bof = 1, .force_short_xfer = 1 },
379 .bufsize = sizeof(struct usb_device_request) + 1024,
381 .callback = &uhso_mux_write_callback
385 /* Config for the multiplexed serial ports */
386 static const struct usb_config uhso_mux_config[UHSO_MUX_ENDPT_MAX] = {
387 [UHSO_MUX_ENDPT_INTR] = {
388 .type = UE_INTERRUPT,
389 .endpoint = UE_ADDR_ANY,
390 .direction = UE_DIR_IN,
391 .flags = { .short_xfer_ok = 1 },
393 .callback = &uhso_mux_intr_callback,
397 /* Config for the raw IP-packet interface */
398 static const struct usb_config uhso_ifnet_config[UHSO_IFNET_MAX] = {
399 [UHSO_IFNET_READ] = {
401 .endpoint = UE_ADDR_ANY,
402 .direction = UE_DIR_IN,
403 .flags = { .pipe_bof = 1, .short_xfer_ok = 1 },
405 .callback = &uhso_ifnet_read_callback
407 [UHSO_IFNET_WRITE] = {
409 .endpoint = UE_ADDR_ANY,
410 .direction = UE_DIR_OUT,
411 .flags = { .pipe_bof = 1, .force_short_xfer = 1 },
413 .timeout = 5 * USB_MS_HZ,
414 .callback = &uhso_ifnet_write_callback
418 /* Config for interfaces with normal bulk serial ports */
419 static const struct usb_config uhso_bs_config[UHSO_BULK_ENDPT_MAX] = {
420 [UHSO_BULK_ENDPT_READ] = {
422 .endpoint = UE_ADDR_ANY,
423 .direction = UE_DIR_IN,
424 .flags = { .pipe_bof = 1, .short_xfer_ok = 1 },
426 .callback = &uhso_bs_read_callback
429 [UHSO_BULK_ENDPT_WRITE] = {
431 .endpoint = UE_ADDR_ANY,
432 .direction = UE_DIR_OUT,
433 .flags = { .pipe_bof = 1, .force_short_xfer = 1 },
435 .callback = &uhso_bs_write_callback
438 [UHSO_BULK_ENDPT_INTR] = {
439 .type = UE_INTERRUPT,
440 .endpoint = UE_ADDR_ANY,
441 .direction = UE_DIR_IN,
442 .flags = { .short_xfer_ok = 1 },
444 .callback = &uhso_bs_intr_callback,
448 static int uhso_probe_iface(struct uhso_softc *, int,
449 int (*probe)(struct usb_device *, int));
450 static int uhso_probe_iface_auto(struct usb_device *, int);
451 static int uhso_probe_iface_static(struct usb_device *, int);
452 static int uhso_attach_muxserial(struct uhso_softc *, struct usb_interface *,
454 static int uhso_attach_bulkserial(struct uhso_softc *, struct usb_interface *,
456 static int uhso_attach_ifnet(struct uhso_softc *, struct usb_interface *,
458 static void uhso_test_autoinst(void *, struct usb_device *,
459 struct usb_attach_arg *);
460 static int uhso_driver_loaded(struct module *, int, void *);
461 static int uhso_radio_sysctl(SYSCTL_HANDLER_ARGS);
462 static int uhso_radio_ctrl(struct uhso_softc *, int);
464 static void uhso_free(struct ucom_softc *);
465 static void uhso_ucom_start_read(struct ucom_softc *);
466 static void uhso_ucom_stop_read(struct ucom_softc *);
467 static void uhso_ucom_start_write(struct ucom_softc *);
468 static void uhso_ucom_stop_write(struct ucom_softc *);
469 static void uhso_ucom_cfg_get_status(struct ucom_softc *, uint8_t *, uint8_t *);
470 static void uhso_ucom_cfg_set_dtr(struct ucom_softc *, uint8_t);
471 static void uhso_ucom_cfg_set_rts(struct ucom_softc *, uint8_t);
472 static void uhso_if_init(void *);
473 static void uhso_if_start(struct ifnet *);
474 static void uhso_if_stop(struct uhso_softc *);
475 static int uhso_if_ioctl(struct ifnet *, u_long, caddr_t);
476 static int uhso_if_output(struct ifnet *, struct mbuf *,
477 const struct sockaddr *, struct route *);
478 static void uhso_if_rxflush(void *);
480 static device_probe_t uhso_probe;
481 static device_attach_t uhso_attach;
482 static device_detach_t uhso_detach;
483 static void uhso_free_softc(struct uhso_softc *);
485 static device_method_t uhso_methods[] = {
486 DEVMETHOD(device_probe, uhso_probe),
487 DEVMETHOD(device_attach, uhso_attach),
488 DEVMETHOD(device_detach, uhso_detach),
492 static driver_t uhso_driver = {
494 .methods = uhso_methods,
495 .size = sizeof(struct uhso_softc)
498 static devclass_t uhso_devclass;
499 DRIVER_MODULE(uhso, uhub, uhso_driver, uhso_devclass, uhso_driver_loaded, 0);
500 MODULE_DEPEND(uhso, ucom, 1, 1, 1);
501 MODULE_DEPEND(uhso, usb, 1, 1, 1);
502 MODULE_VERSION(uhso, 1);
503 USB_PNP_HOST_INFO(uhso_devs);
505 static struct ucom_callback uhso_ucom_callback = {
506 .ucom_cfg_get_status = &uhso_ucom_cfg_get_status,
507 .ucom_cfg_set_dtr = &uhso_ucom_cfg_set_dtr,
508 .ucom_cfg_set_rts = &uhso_ucom_cfg_set_rts,
509 .ucom_start_read = uhso_ucom_start_read,
510 .ucom_stop_read = uhso_ucom_stop_read,
511 .ucom_start_write = uhso_ucom_start_write,
512 .ucom_stop_write = uhso_ucom_stop_write,
513 .ucom_free = &uhso_free,
517 uhso_probe(device_t self)
519 struct usb_attach_arg *uaa = device_get_ivars(self);
522 if (uaa->usb_mode != USB_MODE_HOST)
524 if (uaa->info.bConfigIndex != 0)
526 if (uaa->info.bDeviceClass != 0xff)
529 error = usbd_lookup_id_by_uaa(uhso_devs, sizeof(uhso_devs), uaa);
534 * Probe device to see if we are able to attach
535 * to this interface or not.
537 if (USB_GET_DRIVER_INFO(uaa) == UHSO_AUTO_IFACE) {
538 if (uhso_probe_iface_auto(uaa->device,
539 uaa->info.bIfaceNum) == 0)
546 uhso_attach(device_t self)
548 struct uhso_softc *sc = device_get_softc(self);
549 struct usb_attach_arg *uaa = device_get_ivars(self);
550 struct usb_interface_descriptor *id;
551 struct sysctl_ctx_list *sctx;
552 struct sysctl_oid *soid;
553 struct sysctl_oid *tree = NULL, *tty_node;
554 struct ucom_softc *ucom;
562 sc->sc_udev = uaa->device;
563 mtx_init(&sc->sc_mtx, "uhso", NULL, MTX_DEF);
564 mbufq_init(&sc->sc_rxq, INT_MAX); /* XXXGL: sane maximum */
565 ucom_ref(&sc->sc_super_ucom);
569 id = usbd_get_interface_descriptor(uaa->iface);
570 sc->sc_ctrl_iface_no = id->bInterfaceNumber;
572 sc->sc_iface_no = uaa->info.bIfaceNum;
573 sc->sc_iface_index = uaa->info.bIfaceIndex;
575 /* Setup control pipe */
576 uerr = usbd_transfer_setup(uaa->device,
577 &sc->sc_iface_index, sc->sc_ctrl_xfer,
578 uhso_ctrl_config, UHSO_CTRL_MAX, sc, &sc->sc_mtx);
580 device_printf(self, "Failed to setup control pipe: %s\n",
585 if (USB_GET_DRIVER_INFO(uaa) == UHSO_STATIC_IFACE)
586 probe_f = uhso_probe_iface_static;
587 else if (USB_GET_DRIVER_INFO(uaa) == UHSO_AUTO_IFACE)
588 probe_f = uhso_probe_iface_auto;
592 error = uhso_probe_iface(sc, uaa->info.bIfaceNum, probe_f);
596 sctx = device_get_sysctl_ctx(sc->sc_dev);
597 soid = device_get_sysctl_tree(sc->sc_dev);
599 SYSCTL_ADD_STRING(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "type",
600 CTLFLAG_RD, uhso_port[UHSO_IFACE_PORT(sc->sc_type)], 0,
601 "Port available at this interface");
602 SYSCTL_ADD_PROC(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "radio",
603 CTLTYPE_INT | CTLFLAG_RWTUN | CTLFLAG_MPSAFE, sc, 0,
604 uhso_radio_sysctl, "I", "Enable radio");
607 * The default interface description on most Option devices isn't
608 * very helpful. So we skip device_set_usb_desc and set the
609 * device description manually.
611 device_set_desc_copy(self, uhso_port_type[UHSO_IFACE_PORT_TYPE(sc->sc_type)]);
612 /* Announce device */
613 device_printf(self, "<%s port> at <%s %s> on %s\n",
614 uhso_port_type[UHSO_IFACE_PORT_TYPE(sc->sc_type)],
615 usb_get_manufacturer(uaa->device),
616 usb_get_product(uaa->device),
617 device_get_nameunit(device_get_parent(self)));
619 if (sc->sc_ttys > 0) {
620 SYSCTL_ADD_INT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "ports",
621 CTLFLAG_RD, &sc->sc_ttys, 0, "Number of attached serial ports");
623 tree = SYSCTL_ADD_NODE(sctx, SYSCTL_CHILDREN(soid), OID_AUTO,
624 "port", CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "Serial ports");
628 * Loop through the number of found TTYs and create sysctl
631 for (i = 0; i < sc->sc_ttys; i++) {
633 ucom = &sc->sc_ucom[i];
635 if (UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_MUX)
636 port = uhso_mux_port_map[ht->ht_muxport];
638 port = UHSO_IFACE_PORT_TYPE(sc->sc_type);
640 desc = uhso_port_type_sysctl[port];
642 tty_node = SYSCTL_ADD_NODE(sctx, SYSCTL_CHILDREN(tree), OID_AUTO,
643 desc, CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "");
646 if (sc->sc_ttys == 1)
647 snprintf(ht->ht_name, 32, "cuaU%d", ucom->sc_super->sc_unit);
649 snprintf(ht->ht_name, 32, "cuaU%d.%d",
650 ucom->sc_super->sc_unit, ucom->sc_subunit);
653 desc = uhso_port_type[port];
654 SYSCTL_ADD_STRING(sctx, SYSCTL_CHILDREN(tty_node), OID_AUTO,
655 "tty", CTLFLAG_RD, ht->ht_name, 0, "");
656 SYSCTL_ADD_STRING(sctx, SYSCTL_CHILDREN(tty_node), OID_AUTO,
657 "desc", CTLFLAG_RD, desc, 0, "");
660 device_printf(sc->sc_dev,
661 "\"%s\" port at %s\n", desc, ht->ht_name);
666 uhso_detach(sc->sc_dev);
671 uhso_detach(device_t self)
673 struct uhso_softc *sc = device_get_softc(self);
676 usbd_transfer_unsetup(sc->sc_xfer, 3);
677 usbd_transfer_unsetup(sc->sc_ctrl_xfer, UHSO_CTRL_MAX);
678 if (sc->sc_ttys > 0) {
679 ucom_detach(&sc->sc_super_ucom, sc->sc_ucom);
681 for (i = 0; i < sc->sc_ttys; i++) {
682 if (sc->sc_tty[i].ht_muxport != -1) {
683 usbd_transfer_unsetup(sc->sc_tty[i].ht_xfer,
689 if (sc->sc_ifp != NULL) {
690 callout_drain(&sc->sc_c);
691 free_unr(uhso_ifnet_unit, sc->sc_ifp->if_dunit);
692 mtx_lock(&sc->sc_mtx);
694 bpfdetach(sc->sc_ifp);
695 if_detach(sc->sc_ifp);
697 mtx_unlock(&sc->sc_mtx);
698 usbd_transfer_unsetup(sc->sc_if_xfer, UHSO_IFNET_MAX);
701 device_claim_softc(self);
708 UCOM_UNLOAD_DRAIN(uhso);
711 uhso_free_softc(struct uhso_softc *sc)
713 if (ucom_unref(&sc->sc_super_ucom)) {
714 free(sc->sc_tty, M_USBDEV);
715 free(sc->sc_ucom, M_USBDEV);
716 mtx_destroy(&sc->sc_mtx);
717 device_free_softc(sc);
722 uhso_free(struct ucom_softc *ucom)
724 uhso_free_softc(ucom->sc_parent);
728 uhso_test_autoinst(void *arg, struct usb_device *udev,
729 struct usb_attach_arg *uaa)
731 struct usb_interface *iface;
732 struct usb_interface_descriptor *id;
734 if (uaa->dev_state != UAA_DEV_READY || !uhso_autoswitch)
737 iface = usbd_get_iface(udev, 0);
741 if (id == NULL || id->bInterfaceClass != UICLASS_MASS)
743 if (usbd_lookup_id_by_uaa(uhso_devs, sizeof(uhso_devs), uaa))
744 return; /* no device match */
746 if (usb_msc_eject(udev, 0, MSC_EJECT_REZERO) == 0) {
747 /* success, mark the udev as disappearing */
748 uaa->dev_state = UAA_DEV_EJECTING;
753 uhso_driver_loaded(struct module *mod, int what, void *arg)
757 /* register our autoinstall handler */
758 uhso_etag = EVENTHANDLER_REGISTER(usb_dev_configured,
759 uhso_test_autoinst, NULL, EVENTHANDLER_PRI_ANY);
760 /* create our unit allocator for inet devs */
761 uhso_ifnet_unit = new_unrhdr(0, INT_MAX, NULL);
764 EVENTHANDLER_DEREGISTER(usb_dev_configured, uhso_etag);
765 delete_unrhdr(uhso_ifnet_unit);
774 * Probe the interface type by querying the device. The elements
775 * of an array indicates the capabilities of a particular interface.
776 * Returns a bit mask with the interface capabilities.
779 uhso_probe_iface_auto(struct usb_device *udev, int index)
781 struct usb_device_request req;
787 req.bmRequestType = UT_READ_VENDOR_DEVICE;
789 USETW(req.wValue, 0);
790 USETW(req.wIndex, 0);
791 USETW(req.wLength, 17);
793 uerr = usbd_do_request_flags(udev, NULL, &req, buf,
794 0, &actlen, USB_MS_HZ);
796 printf("%s: usbd_do_request_flags failed, %s\n",
797 __func__, usbd_errstr(uerr));
801 UHSO_DPRINTF(1, "actlen=%d\n", actlen);
802 UHSO_HEXDUMP(buf, 17);
804 if (index < 0 || index > 16) {
805 UHSO_DPRINTF(0, "Index %d out of range\n", index);
809 UHSO_DPRINTF(1, "index=%d, type=%x[%s]\n", index, buf[index],
810 uhso_port_type[(int)uhso_port_map[(int)buf[index]]]);
812 if (buf[index] >= uhso_port_map_max)
815 port = uhso_port_map[(int)buf[index]];
818 case UHSO_PORT_TYPE_NETWORK:
819 return (UHSO_IFACE_SPEC(UHSO_IF_NET | UHSO_IF_MUX,
820 UHSO_PORT_SERIAL | UHSO_PORT_NETWORK, port));
821 case UHSO_PORT_TYPE_DIAG:
822 case UHSO_PORT_TYPE_DIAG2:
823 case UHSO_PORT_TYPE_GPS:
824 case UHSO_PORT_TYPE_GPSCTL:
825 case UHSO_PORT_TYPE_CTL:
826 case UHSO_PORT_TYPE_APP:
827 case UHSO_PORT_TYPE_APP2:
828 case UHSO_PORT_TYPE_MODEM:
829 return (UHSO_IFACE_SPEC(UHSO_IF_BULK,
830 UHSO_PORT_SERIAL, port));
831 case UHSO_PORT_TYPE_MSD:
833 case UHSO_PORT_TYPE_UNKNOWN:
842 * Returns the capabilities of interfaces for devices that don't
843 * support the automatic query.
844 * Returns a bit mask with the interface capabilities.
847 uhso_probe_iface_static(struct usb_device *udev, int index)
849 struct usb_config_descriptor *cd;
851 cd = usbd_get_config_descriptor(udev);
852 if (cd->bNumInterface <= 3) {
853 /* Cards with 3 or less interfaces */
856 return UHSO_IFACE_SPEC(UHSO_IF_NET | UHSO_IF_MUX,
857 UHSO_PORT_SERIAL | UHSO_PORT_NETWORK,
858 UHSO_PORT_TYPE_NETWORK);
860 return UHSO_IFACE_SPEC(UHSO_IF_BULK,
861 UHSO_PORT_SERIAL, UHSO_PORT_TYPE_DIAG);
863 return UHSO_IFACE_SPEC(UHSO_IF_BULK,
864 UHSO_PORT_SERIAL, UHSO_PORT_TYPE_MODEM);
867 /* Cards with 4 interfaces */
870 return UHSO_IFACE_SPEC(UHSO_IF_NET | UHSO_IF_MUX,
871 UHSO_PORT_SERIAL | UHSO_PORT_NETWORK,
872 UHSO_PORT_TYPE_NETWORK);
874 return UHSO_IFACE_SPEC(UHSO_IF_BULK,
875 UHSO_PORT_SERIAL, UHSO_PORT_TYPE_DIAG2);
877 return UHSO_IFACE_SPEC(UHSO_IF_BULK,
878 UHSO_PORT_SERIAL, UHSO_PORT_TYPE_MODEM);
880 return UHSO_IFACE_SPEC(UHSO_IF_BULK,
881 UHSO_PORT_SERIAL, UHSO_PORT_TYPE_DIAG);
888 * Probes an interface for its particular capabilities and attaches if
889 * it's a supported interface.
892 uhso_probe_iface(struct uhso_softc *sc, int index,
893 int (*probe)(struct usb_device *, int))
895 struct usb_interface *iface;
898 UHSO_DPRINTF(1, "Probing for interface %d, probe_func=%p\n", index, probe);
900 type = probe(sc->sc_udev, index);
901 UHSO_DPRINTF(1, "Probe result %x\n", type);
906 iface = usbd_get_iface(sc->sc_udev, index);
908 if (UHSO_IFACE_PORT_TYPE(type) == UHSO_PORT_TYPE_NETWORK) {
909 error = uhso_attach_ifnet(sc, iface, type);
911 UHSO_DPRINTF(1, "uhso_attach_ifnet failed");
916 * If there is an additional interrupt endpoint on this
917 * interface then we most likely have a multiplexed serial port
920 if (iface->idesc->bNumEndpoints < 3) {
921 sc->sc_type = UHSO_IFACE_SPEC(
922 UHSO_IFACE_USB_TYPE(type) & ~UHSO_IF_MUX,
923 UHSO_IFACE_PORT(type) & ~UHSO_PORT_SERIAL,
924 UHSO_IFACE_PORT_TYPE(type));
928 UHSO_DPRINTF(1, "Trying to attach mux. serial\n");
929 error = uhso_attach_muxserial(sc, iface, type);
930 if (error == 0 && sc->sc_ttys > 0) {
931 error = ucom_attach(&sc->sc_super_ucom, sc->sc_ucom,
932 sc->sc_ttys, sc, &uhso_ucom_callback, &sc->sc_mtx);
934 device_printf(sc->sc_dev, "ucom_attach failed\n");
937 ucom_set_pnpinfo_usb(&sc->sc_super_ucom, sc->sc_dev);
939 mtx_lock(&sc->sc_mtx);
940 usbd_transfer_start(sc->sc_xfer[UHSO_MUX_ENDPT_INTR]);
941 mtx_unlock(&sc->sc_mtx);
943 } else if ((UHSO_IFACE_USB_TYPE(type) & UHSO_IF_BULK) &&
944 UHSO_IFACE_PORT(type) & UHSO_PORT_SERIAL) {
945 error = uhso_attach_bulkserial(sc, iface, type);
949 error = ucom_attach(&sc->sc_super_ucom, sc->sc_ucom,
950 sc->sc_ttys, sc, &uhso_ucom_callback, &sc->sc_mtx);
952 device_printf(sc->sc_dev, "ucom_attach failed\n");
955 ucom_set_pnpinfo_usb(&sc->sc_super_ucom, sc->sc_dev);
958 UHSO_DPRINTF(0, "Unknown type %x\n", type);
966 uhso_radio_ctrl(struct uhso_softc *sc, int onoff)
968 struct usb_device_request req;
971 req.bmRequestType = UT_VENDOR;
972 req.bRequest = onoff ? 0x82 : 0x81;
973 USETW(req.wValue, 0);
974 USETW(req.wIndex, 0);
975 USETW(req.wLength, 0);
977 uerr = usbd_do_request(sc->sc_udev, NULL, &req, NULL);
979 device_printf(sc->sc_dev, "usbd_do_request_flags failed: %s\n",
987 uhso_radio_sysctl(SYSCTL_HANDLER_ARGS)
989 struct uhso_softc *sc = arg1;
992 radio = sc->sc_radio;
993 error = sysctl_handle_int(oidp, &radio, 0, req);
996 if (radio != sc->sc_radio) {
997 radio = radio != 0 ? 1 : 0;
998 error = uhso_radio_ctrl(sc, radio);
1000 sc->sc_radio = radio;
1007 * Expands allocated memory to fit an additional TTY.
1008 * Two arrays are kept with matching indexes, one for ucom and one
1009 * for our private data.
1012 uhso_alloc_tty(struct uhso_softc *sc)
1016 sc->sc_tty = reallocf(sc->sc_tty, sizeof(struct uhso_tty) * sc->sc_ttys,
1017 M_USBDEV, M_WAITOK | M_ZERO);
1018 if (sc->sc_tty == NULL)
1021 sc->sc_ucom = reallocf(sc->sc_ucom,
1022 sizeof(struct ucom_softc) * sc->sc_ttys, M_USBDEV, M_WAITOK | M_ZERO);
1023 if (sc->sc_ucom == NULL)
1026 sc->sc_tty[sc->sc_ttys - 1].ht_sc = sc;
1028 UHSO_DPRINTF(1, "Allocated TTY %d\n", sc->sc_ttys - 1);
1029 return (sc->sc_ttys - 1);
1033 * Attach a multiplexed serial port
1034 * Data is read/written with requests on the default control pipe. An interrupt
1035 * endpoint returns when there is new data to be read.
1038 uhso_attach_muxserial(struct uhso_softc *sc, struct usb_interface *iface,
1041 struct usb_descriptor *desc;
1046 * The class specific interface (type 0x24) descriptor subtype field
1047 * contains a bitmask that specifies which (and how many) ports that
1048 * are available through this multiplexed serial port.
1050 desc = usbd_find_descriptor(sc->sc_udev, NULL,
1051 iface->idesc->bInterfaceNumber, UDESC_CS_INTERFACE, 0xff, 0, 0);
1053 UHSO_DPRINTF(0, "Failed to find UDESC_CS_INTERFACE\n");
1057 UHSO_DPRINTF(1, "Mux port mask %x\n", desc->bDescriptorSubtype);
1058 if (desc->bDescriptorSubtype == 0)
1062 * The bitmask is one octet, loop through the number of
1063 * bits that are set and create a TTY for each.
1065 for (i = 0; i < 8; i++) {
1067 if ((port & desc->bDescriptorSubtype) == port) {
1068 UHSO_DPRINTF(2, "Found mux port %x (%d)\n", port, i);
1069 tty = uhso_alloc_tty(sc);
1072 sc->sc_tty[tty].ht_muxport = i;
1073 uerr = usbd_transfer_setup(sc->sc_udev,
1074 &sc->sc_iface_index, sc->sc_tty[tty].ht_xfer,
1075 uhso_ctrl_config, UHSO_CTRL_MAX, sc, &sc->sc_mtx);
1077 device_printf(sc->sc_dev,
1078 "Failed to setup control pipe: %s\n",
1085 /* Setup the intr. endpoint */
1086 uerr = usbd_transfer_setup(sc->sc_udev,
1087 &iface->idesc->bInterfaceNumber, sc->sc_xfer,
1088 uhso_mux_config, 1, sc, &sc->sc_mtx);
1096 * Interrupt callback for the multiplexed serial port. Indicates
1097 * which serial port has data waiting.
1100 uhso_mux_intr_callback(struct usb_xfer *xfer, usb_error_t error)
1102 struct usb_page_cache *pc;
1103 struct usb_page_search res;
1104 struct uhso_softc *sc = usbd_xfer_softc(xfer);
1105 unsigned int i, mux;
1107 UHSO_DPRINTF(3, "status %d\n", USB_GET_STATE(xfer));
1109 switch (USB_GET_STATE(xfer)) {
1110 case USB_ST_TRANSFERRED:
1112 * The multiplexed port number can be found at the first byte.
1113 * It contains a bit mask, we transform this in to an integer.
1115 pc = usbd_xfer_get_frame(xfer, 0);
1116 usbd_get_page(pc, 0, &res);
1118 i = *((unsigned char *)res.buffer);
1124 UHSO_DPRINTF(3, "mux port %d (%d)\n", mux, i);
1125 if (mux > UHSO_MPORT_TYPE_NOMAX)
1128 /* Issue a read for this serial port */
1130 sc->sc_tty[mux].ht_xfer[UHSO_CTRL_READ],
1132 usbd_transfer_start(sc->sc_tty[mux].ht_xfer[UHSO_CTRL_READ]);
1137 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
1138 usbd_transfer_submit(xfer);
1141 UHSO_DPRINTF(0, "error: %s\n", usbd_errstr(error));
1142 if (error == USB_ERR_CANCELLED)
1145 usbd_xfer_set_stall(xfer);
1151 uhso_mux_read_callback(struct usb_xfer *xfer, usb_error_t error)
1153 struct uhso_softc *sc = usbd_xfer_softc(xfer);
1154 struct usb_page_cache *pc;
1155 struct usb_device_request req;
1156 struct uhso_tty *ht;
1159 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
1161 UHSO_DPRINTF(3, "status %d\n", USB_GET_STATE(xfer));
1163 ht = usbd_xfer_get_priv(xfer);
1164 UHSO_DPRINTF(3, "ht=%p open=%d\n", ht, ht->ht_open);
1166 switch (USB_GET_STATE(xfer)) {
1167 case USB_ST_TRANSFERRED:
1168 /* Got data, send to ucom */
1169 pc = usbd_xfer_get_frame(xfer, 1);
1170 len = usbd_xfer_frame_len(xfer, 1);
1172 UHSO_DPRINTF(3, "got %d bytes on mux port %d\n", len,
1175 usbd_transfer_start(sc->sc_xfer[UHSO_MUX_ENDPT_INTR]);
1179 /* Deliver data if the TTY is open, discard otherwise */
1181 ucom_put_data(&sc->sc_ucom[ht->ht_muxport], pc, 0, len);
1185 memset(&req, 0, sizeof(struct usb_device_request));
1186 req.bmRequestType = UT_READ_CLASS_INTERFACE;
1187 req.bRequest = UCDC_GET_ENCAPSULATED_RESPONSE;
1188 USETW(req.wValue, 0);
1189 USETW(req.wIndex, ht->ht_muxport);
1190 USETW(req.wLength, 1024);
1192 pc = usbd_xfer_get_frame(xfer, 0);
1193 usbd_copy_in(pc, 0, &req, sizeof(req));
1195 usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
1196 usbd_xfer_set_frame_len(xfer, 1, 1024);
1197 usbd_xfer_set_frames(xfer, 2);
1198 usbd_transfer_submit(xfer);
1201 UHSO_DPRINTF(0, "error: %s\n", usbd_errstr(error));
1202 if (error == USB_ERR_CANCELLED)
1204 usbd_xfer_set_stall(xfer);
1210 uhso_mux_write_callback(struct usb_xfer *xfer, usb_error_t error)
1212 struct uhso_softc *sc = usbd_xfer_softc(xfer);
1213 struct uhso_tty *ht;
1214 struct usb_page_cache *pc;
1215 struct usb_device_request req;
1217 struct usb_page_search res;
1219 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
1221 ht = usbd_xfer_get_priv(xfer);
1222 UHSO_DPRINTF(3, "status=%d, using mux port %d\n",
1223 USB_GET_STATE(xfer), ht->ht_muxport);
1225 switch (USB_GET_STATE(xfer)) {
1226 case USB_ST_TRANSFERRED:
1227 UHSO_DPRINTF(3, "wrote %zd data bytes to muxport %d\n",
1228 actlen - sizeof(struct usb_device_request) ,
1233 pc = usbd_xfer_get_frame(xfer, 1);
1234 if (ucom_get_data(&sc->sc_ucom[ht->ht_muxport], pc,
1236 usbd_get_page(pc, 0, &res);
1238 memset(&req, 0, sizeof(struct usb_device_request));
1239 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
1240 req.bRequest = UCDC_SEND_ENCAPSULATED_COMMAND;
1241 USETW(req.wValue, 0);
1242 USETW(req.wIndex, ht->ht_muxport);
1243 USETW(req.wLength, actlen);
1245 pc = usbd_xfer_get_frame(xfer, 0);
1246 usbd_copy_in(pc, 0, &req, sizeof(req));
1248 usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
1249 usbd_xfer_set_frame_len(xfer, 1, actlen);
1250 usbd_xfer_set_frames(xfer, 2);
1252 UHSO_DPRINTF(3, "Prepared %d bytes for transmit "
1253 "on muxport %d\n", actlen, ht->ht_muxport);
1255 usbd_transfer_submit(xfer);
1259 UHSO_DPRINTF(0, "error: %s\n", usbd_errstr(error));
1260 if (error == USB_ERR_CANCELLED)
1262 usbd_xfer_set_stall(xfer);
1268 uhso_attach_bulkserial(struct uhso_softc *sc, struct usb_interface *iface,
1274 /* Try attaching RD/WR/INTR first */
1275 uerr = usbd_transfer_setup(sc->sc_udev,
1276 &iface->idesc->bInterfaceNumber, sc->sc_xfer,
1277 uhso_bs_config, UHSO_BULK_ENDPT_MAX, sc, &sc->sc_mtx);
1279 /* Try only RD/WR */
1280 uerr = usbd_transfer_setup(sc->sc_udev,
1281 &iface->idesc->bInterfaceNumber, sc->sc_xfer,
1282 uhso_bs_config, UHSO_BULK_ENDPT_MAX - 1, sc, &sc->sc_mtx);
1285 UHSO_DPRINTF(0, "usbd_transfer_setup failed");
1289 tty = uhso_alloc_tty(sc);
1291 usbd_transfer_unsetup(sc->sc_xfer, UHSO_BULK_ENDPT_MAX);
1295 sc->sc_tty[tty].ht_muxport = -1;
1300 uhso_bs_read_callback(struct usb_xfer *xfer, usb_error_t error)
1302 struct uhso_softc *sc = usbd_xfer_softc(xfer);
1303 struct usb_page_cache *pc;
1306 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
1308 UHSO_DPRINTF(3, "status %d, actlen=%d\n", USB_GET_STATE(xfer), actlen);
1310 switch (USB_GET_STATE(xfer)) {
1311 case USB_ST_TRANSFERRED:
1312 pc = usbd_xfer_get_frame(xfer, 0);
1313 ucom_put_data(&sc->sc_ucom[0], pc, 0, actlen);
1317 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
1318 usbd_transfer_submit(xfer);
1321 UHSO_DPRINTF(0, "error: %s\n", usbd_errstr(error));
1322 if (error == USB_ERR_CANCELLED)
1324 usbd_xfer_set_stall(xfer);
1330 uhso_bs_write_callback(struct usb_xfer *xfer, usb_error_t error)
1332 struct uhso_softc *sc = usbd_xfer_softc(xfer);
1333 struct usb_page_cache *pc;
1336 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
1338 UHSO_DPRINTF(3, "status %d, actlen=%d\n", USB_GET_STATE(xfer), actlen);
1340 switch (USB_GET_STATE(xfer)) {
1341 case USB_ST_TRANSFERRED:
1344 pc = usbd_xfer_get_frame(xfer, 0);
1345 if (ucom_get_data(&sc->sc_ucom[0], pc, 0, 8192, &actlen)) {
1346 usbd_xfer_set_frame_len(xfer, 0, actlen);
1347 usbd_transfer_submit(xfer);
1352 UHSO_DPRINTF(0, "error: %s\n", usbd_errstr(error));
1353 if (error == USB_ERR_CANCELLED)
1355 usbd_xfer_set_stall(xfer);
1361 uhso_bs_cfg(struct uhso_softc *sc)
1363 struct usb_device_request req;
1366 if (!(UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_BULK))
1369 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
1370 req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
1371 USETW(req.wValue, sc->sc_line);
1372 USETW(req.wIndex, sc->sc_iface_no);
1373 USETW(req.wLength, 0);
1375 uerr = ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom[0], &req, NULL, 0, 1000);
1377 device_printf(sc->sc_dev, "failed to set ctrl line state to "
1378 "0x%02x: %s\n", sc->sc_line, usbd_errstr(uerr));
1383 uhso_bs_intr_callback(struct usb_xfer *xfer, usb_error_t error)
1385 struct uhso_softc *sc = usbd_xfer_softc(xfer);
1386 struct usb_page_cache *pc;
1388 struct usb_cdc_notification cdc;
1390 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
1391 UHSO_DPRINTF(3, "status %d, actlen=%d\n", USB_GET_STATE(xfer), actlen);
1393 switch (USB_GET_STATE(xfer)) {
1394 case USB_ST_TRANSFERRED:
1395 if (actlen < UCDC_NOTIFICATION_LENGTH) {
1396 UHSO_DPRINTF(0, "UCDC notification too short: %d\n", actlen);
1399 else if (actlen > (int)sizeof(struct usb_cdc_notification)) {
1400 UHSO_DPRINTF(0, "UCDC notification too large: %d\n", actlen);
1401 actlen = sizeof(struct usb_cdc_notification);
1404 pc = usbd_xfer_get_frame(xfer, 0);
1405 usbd_copy_out(pc, 0, &cdc, actlen);
1407 if (UGETW(cdc.wIndex) != sc->sc_iface_no) {
1408 UHSO_DPRINTF(0, "Interface mismatch, got %d expected %d\n",
1409 UGETW(cdc.wIndex), sc->sc_iface_no);
1413 if (cdc.bmRequestType == UCDC_NOTIFICATION &&
1414 cdc.bNotification == UCDC_N_SERIAL_STATE) {
1415 UHSO_DPRINTF(2, "notify = 0x%02x\n", cdc.data[0]);
1419 if (cdc.data[0] & UCDC_N_SERIAL_RI)
1420 sc->sc_msr |= SER_RI;
1421 if (cdc.data[0] & UCDC_N_SERIAL_DSR)
1422 sc->sc_msr |= SER_DSR;
1423 if (cdc.data[0] & UCDC_N_SERIAL_DCD)
1424 sc->sc_msr |= SER_DCD;
1426 ucom_status_change(&sc->sc_ucom[0]);
1431 if (error == USB_ERR_CANCELLED)
1433 usbd_xfer_set_stall(xfer);
1439 uhso_ucom_cfg_get_status(struct ucom_softc *ucom, uint8_t *lsr, uint8_t *msr)
1441 struct uhso_softc *sc = ucom->sc_parent;
1448 uhso_ucom_cfg_set_dtr(struct ucom_softc *ucom, uint8_t onoff)
1450 struct uhso_softc *sc = ucom->sc_parent;
1452 if (!(UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_BULK))
1456 sc->sc_line |= UCDC_LINE_DTR;
1458 sc->sc_line &= ~UCDC_LINE_DTR;
1464 uhso_ucom_cfg_set_rts(struct ucom_softc *ucom, uint8_t onoff)
1466 struct uhso_softc *sc = ucom->sc_parent;
1468 if (!(UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_BULK))
1472 sc->sc_line |= UCDC_LINE_RTS;
1474 sc->sc_line &= ~UCDC_LINE_RTS;
1480 uhso_ucom_start_read(struct ucom_softc *ucom)
1482 struct uhso_softc *sc = ucom->sc_parent;
1484 UHSO_DPRINTF(3, "unit=%d, subunit=%d\n",
1485 ucom->sc_super->sc_unit, ucom->sc_subunit);
1487 if (UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_MUX) {
1488 sc->sc_tty[ucom->sc_subunit].ht_open = 1;
1489 usbd_transfer_start(sc->sc_xfer[UHSO_MUX_ENDPT_INTR]);
1491 else if (UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_BULK) {
1492 sc->sc_tty[0].ht_open = 1;
1493 usbd_transfer_start(sc->sc_xfer[UHSO_BULK_ENDPT_READ]);
1494 if (sc->sc_xfer[UHSO_BULK_ENDPT_INTR] != NULL)
1495 usbd_transfer_start(sc->sc_xfer[UHSO_BULK_ENDPT_INTR]);
1500 uhso_ucom_stop_read(struct ucom_softc *ucom)
1503 struct uhso_softc *sc = ucom->sc_parent;
1505 if (UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_MUX) {
1506 sc->sc_tty[ucom->sc_subunit].ht_open = 0;
1508 sc->sc_tty[ucom->sc_subunit].ht_xfer[UHSO_CTRL_READ]);
1510 else if (UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_BULK) {
1511 sc->sc_tty[0].ht_open = 0;
1512 usbd_transfer_start(sc->sc_xfer[UHSO_BULK_ENDPT_READ]);
1513 if (sc->sc_xfer[UHSO_BULK_ENDPT_INTR] != NULL)
1514 usbd_transfer_stop(sc->sc_xfer[UHSO_BULK_ENDPT_INTR]);
1519 uhso_ucom_start_write(struct ucom_softc *ucom)
1521 struct uhso_softc *sc = ucom->sc_parent;
1523 if (UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_MUX) {
1524 UHSO_DPRINTF(3, "local unit %d\n", ucom->sc_subunit);
1526 usbd_transfer_start(sc->sc_xfer[UHSO_MUX_ENDPT_INTR]);
1529 sc->sc_tty[ucom->sc_subunit].ht_xfer[UHSO_CTRL_WRITE],
1530 &sc->sc_tty[ucom->sc_subunit]);
1531 usbd_transfer_start(
1532 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 if (mbufq_full(&sc->sc_rxq))
1631 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
1632 usbd_copy_out(pc, 0, mtod(m, uint8_t *), actlen);
1633 m->m_pkthdr.len = m->m_len = actlen;
1634 /* Enqueue frame for further processing */
1635 mbufq_enqueue(&sc->sc_rxq, m);
1636 if (!callout_pending(&sc->sc_c) ||
1637 !callout_active(&sc->sc_c)) {
1638 callout_schedule(&sc->sc_c, 1);
1644 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
1645 usbd_transfer_submit(xfer);
1648 UHSO_DPRINTF(0, "error: %s\n", usbd_errstr(error));
1649 if (error == USB_ERR_CANCELLED)
1651 usbd_xfer_set_stall(xfer);
1657 * Deferred RX processing, called with mutex locked.
1659 * Each frame we receive might contain several small ip-packets as well
1660 * as partial ip-packets. We need to separate/assemble them into individual
1661 * packets before sending them to the ip-layer.
1664 uhso_if_rxflush(void *arg)
1666 struct epoch_tracker et;
1667 struct uhso_softc *sc = arg;
1668 struct ifnet *ifp = sc->sc_ifp;
1670 struct mbuf *m, *m0, *mwait;
1673 struct ip6_hdr *ip6;
1679 mwait = sc->sc_mwait;
1680 NET_EPOCH_ENTER(et);
1683 if ((m = mbufq_dequeue(&sc->sc_rxq)) == NULL)
1685 UHSO_DPRINTF(3, "dequeue m=%p, len=%d\n", m, m->m_len);
1687 mtx_unlock(&sc->sc_mtx);
1689 /* Do we have a partial packet waiting? */
1690 if (mwait != NULL) {
1694 UHSO_DPRINTF(3, "partial m0=%p(%d), concat w/ m=%p(%d)\n",
1695 m0, m0->m_len, m, m->m_len);
1698 m = m_pullup(m0, sizeof(struct ip));
1700 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1701 UHSO_DPRINTF(0, "m_pullup failed\n");
1702 mtx_lock(&sc->sc_mtx);
1705 UHSO_DPRINTF(3, "Constructed mbuf=%p, len=%d\n",
1706 m, m->m_pkthdr.len);
1709 cp = mtod(m, uint8_t *);
1710 ip = (struct ip *)cp;
1712 ip6 = (struct ip6_hdr *)cp;
1715 /* Check for IPv4 */
1716 if (ip->ip_v == IPVERSION) {
1717 iplen = htons(ip->ip_len);
1721 /* Check for IPv6 */
1722 else if ((ip6->ip6_vfc & IPV6_VERSION_MASK) == IPV6_VERSION) {
1723 iplen = htons(ip6->ip6_plen);
1728 UHSO_DPRINTF(0, "got unexpected ip version %d, "
1729 "m=%p, len=%d\n", (*cp & 0xf0) >> 4, m, m->m_len);
1730 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1731 UHSO_HEXDUMP(cp, 4);
1734 mtx_lock(&sc->sc_mtx);
1739 UHSO_DPRINTF(0, "Zero IP length\n");
1740 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1743 mtx_lock(&sc->sc_mtx);
1747 UHSO_DPRINTF(3, "m=%p, len=%d, cp=%p, iplen=%d\n",
1748 m, m->m_pkthdr.len, cp, iplen);
1752 /* More IP packets in this mbuf */
1753 if (iplen < m->m_pkthdr.len) {
1757 * Allocate a new mbuf for this IP packet and
1758 * copy the IP-packet into it.
1760 m = m_getcl(M_WAITOK, MT_DATA, M_PKTHDR);
1761 memcpy(mtod(m, uint8_t *), mtod(m0, uint8_t *), iplen);
1762 m->m_pkthdr.len = m->m_len = iplen;
1764 /* Adjust the size of the original mbuf */
1766 m0 = m_defrag(m0, M_WAITOK);
1768 UHSO_DPRINTF(3, "New mbuf=%p, len=%d/%d, m0=%p, "
1769 "m0_len=%d/%d\n", m, m->m_pkthdr.len, m->m_len,
1770 m0, m0->m_pkthdr.len, m0->m_len);
1772 else if (iplen > m->m_pkthdr.len) {
1773 UHSO_DPRINTF(3, "Deferred mbuf=%p, len=%d\n",
1774 m, m->m_pkthdr.len);
1777 mtx_lock(&sc->sc_mtx);
1781 if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
1782 m->m_pkthdr.rcvif = ifp;
1784 /* Dispatch to IP layer */
1785 BPF_MTAP(sc->sc_ifp, m);
1786 M_SETFIB(m, ifp->if_fib);
1787 netisr_dispatch(isr, m);
1788 m = m0 != NULL ? m0 : NULL;
1789 mtx_lock(&sc->sc_mtx);
1792 sc->sc_mwait = mwait;
1796 uhso_ifnet_write_callback(struct usb_xfer *xfer, usb_error_t error)
1798 struct uhso_softc *sc = usbd_xfer_softc(xfer);
1799 struct ifnet *ifp = sc->sc_ifp;
1800 struct usb_page_cache *pc;
1804 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
1806 UHSO_DPRINTF(3, "status %d, actlen=%d\n", USB_GET_STATE(xfer), actlen);
1808 switch (USB_GET_STATE(xfer)) {
1809 case USB_ST_TRANSFERRED:
1810 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
1811 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1814 IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
1818 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1820 if (m->m_pkthdr.len > MCLBYTES)
1821 m->m_pkthdr.len = MCLBYTES;
1823 usbd_xfer_set_frame_len(xfer, 0, m->m_pkthdr.len);
1824 pc = usbd_xfer_get_frame(xfer, 0);
1825 usbd_m_copy_in(pc, 0, m, 0, m->m_pkthdr.len);
1826 usbd_transfer_submit(xfer);
1832 UHSO_DPRINTF(0, "error: %s\n", usbd_errstr(error));
1833 if (error == USB_ERR_CANCELLED)
1835 usbd_xfer_set_stall(xfer);
1841 uhso_if_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1843 struct uhso_softc *sc;
1849 if (ifp->if_flags & IFF_UP) {
1850 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1855 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1856 mtx_lock(&sc->sc_mtx);
1858 mtx_unlock(&sc->sc_mtx);
1873 uhso_if_init(void *priv)
1875 struct uhso_softc *sc = priv;
1876 struct ifnet *ifp = sc->sc_ifp;
1878 mtx_lock(&sc->sc_mtx);
1881 ifp->if_flags |= IFF_UP;
1882 ifp->if_drv_flags |= IFF_DRV_RUNNING;
1883 mtx_unlock(&sc->sc_mtx);
1885 UHSO_DPRINTF(2, "ifnet initialized\n");
1889 uhso_if_output(struct ifnet *ifp, struct mbuf *m0, const struct sockaddr *dst,
1894 /* Only IPv4/6 support */
1895 if (dst->sa_family != AF_INET
1897 && dst->sa_family != AF_INET6
1900 return (EAFNOSUPPORT);
1903 error = (ifp->if_transmit)(ifp, m0);
1905 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1908 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
1913 uhso_if_start(struct ifnet *ifp)
1915 struct uhso_softc *sc = ifp->if_softc;
1917 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
1918 UHSO_DPRINTF(1, "Not running\n");
1922 mtx_lock(&sc->sc_mtx);
1923 usbd_transfer_start(sc->sc_if_xfer[UHSO_IFNET_READ]);
1924 usbd_transfer_start(sc->sc_if_xfer[UHSO_IFNET_WRITE]);
1925 mtx_unlock(&sc->sc_mtx);
1926 UHSO_DPRINTF(3, "interface started\n");
1930 uhso_if_stop(struct uhso_softc *sc)
1933 usbd_transfer_stop(sc->sc_if_xfer[UHSO_IFNET_READ]);
1934 usbd_transfer_stop(sc->sc_if_xfer[UHSO_IFNET_WRITE]);
1935 sc->sc_ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);