2 * Copyright (c) 2010 Fredrik Lindberg <fli@shapeshifter.se>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
29 #include <sys/param.h>
30 #include <sys/types.h>
31 #include <sys/sockio.h>
33 #include <sys/malloc.h>
34 #include <sys/kernel.h>
35 #include <sys/module.h>
36 #include <sys/socket.h>
38 #include <sys/sysctl.h>
39 #include <sys/condvar.h>
44 #include <sys/systm.h>
45 #include <sys/limits.h>
47 #include <machine/bus.h>
50 #include <net/if_types.h>
51 #include <net/netisr.h>
53 #include <netinet/in.h>
54 #include <netinet/ip.h>
55 #include <netinet/ip6.h>
57 #include <dev/usb/usb.h>
58 #include <dev/usb/usbdi.h>
59 #include <dev/usb/usbdi_util.h>
60 #include <dev/usb/usb_cdc.h>
62 #define USB_DEBUG_VAR uhso_debug
63 #include <dev/usb/usb_debug.h>
64 #include <dev/usb/usb_process.h>
65 #include <dev/usb/usb_busdma.h>
66 #include <dev/usb/usb_msctest.h>
68 #include <dev/usb/serial/usb_serial.h>
71 struct uhso_softc *ht_sc;
72 struct usb_xfer *ht_xfer[3];
73 int ht_muxport; /* Mux. port no */
80 struct usb_device *sc_udev;
82 uint32_t sc_type; /* Interface definition */
85 struct usb_xfer *sc_xfer[3];
87 uint8_t sc_iface_index;
90 struct usb_xfer * sc_ctrl_xfer[2];
91 uint8_t sc_ctrl_iface_no;
94 struct usb_xfer *sc_if_xfer[2];
96 struct mbuf *sc_mwait; /* Partial packet */
97 size_t sc_waitlen; /* No. of outstanding bytes */
98 struct ifqueue sc_rxq;
101 /* TTY related structures */
102 struct ucom_super_softc sc_super_ucom;
104 struct uhso_tty *sc_tty;
105 struct ucom_softc *sc_ucom;
111 #define UHSO_MAX_MTU 2048
114 * There are mainly two type of cards floating around.
115 * The first one has 2,3 or 4 interfaces with a multiplexed serial port
116 * and packet interface on the first interface and bulk serial ports
118 * The second type of card has several other interfaces, their purpose
119 * can be detected during run-time.
121 #define UHSO_IFACE_SPEC(usb_type, port, port_type) \
122 (((usb_type) << 24) | ((port) << 16) | (port_type))
124 #define UHSO_IFACE_USB_TYPE(x) ((x >> 24) & 0xff)
125 #define UHSO_IFACE_PORT(x) ((x >> 16) & 0xff)
126 #define UHSO_IFACE_PORT_TYPE(x) (x & 0xff)
129 * USB interface types
131 #define UHSO_IF_NET 0x01 /* Network packet interface */
132 #define UHSO_IF_MUX 0x02 /* Multiplexed serial port */
133 #define UHSO_IF_BULK 0x04 /* Bulk interface */
138 #define UHSO_PORT_UNKNOWN 0x00
139 #define UHSO_PORT_SERIAL 0x01 /* Serial port */
140 #define UHSO_PORT_NETWORK 0x02 /* Network packet interface */
143 * Multiplexed serial port destination sub-port names
145 #define UHSO_MPORT_TYPE_CTL 0x00 /* Control port */
146 #define UHSO_MPORT_TYPE_APP 0x01 /* Application */
147 #define UHSO_MPORT_TYPE_PCSC 0x02
148 #define UHSO_MPORT_TYPE_GPS 0x03
149 #define UHSO_MPORT_TYPE_APP2 0x04 /* Secondary application */
150 #define UHSO_MPORT_TYPE_MAX UHSO_MPORT_TYPE_APP2
151 #define UHSO_MPORT_TYPE_NOMAX 8 /* Max number of mux ports */
155 * Note that these definitions are arbitrary and do not match the values
156 * returned by the auto config descriptor.
158 #define UHSO_PORT_TYPE_UNKNOWN 0x00
159 #define UHSO_PORT_TYPE_CTL 0x01
160 #define UHSO_PORT_TYPE_APP 0x02
161 #define UHSO_PORT_TYPE_APP2 0x03
162 #define UHSO_PORT_TYPE_MODEM 0x04
163 #define UHSO_PORT_TYPE_NETWORK 0x05
164 #define UHSO_PORT_TYPE_DIAG 0x06
165 #define UHSO_PORT_TYPE_DIAG2 0x07
166 #define UHSO_PORT_TYPE_GPS 0x08
167 #define UHSO_PORT_TYPE_GPSCTL 0x09
168 #define UHSO_PORT_TYPE_PCSC 0x0a
169 #define UHSO_PORT_TYPE_MSD 0x0b
170 #define UHSO_PORT_TYPE_VOICE 0x0c
171 #define UHSO_PORT_TYPE_MAX 0x0c
173 static eventhandler_tag uhso_etag;
175 /* Overall port type */
176 static char *uhso_port[] = {
184 * Map between interface port type read from device and description type.
185 * The position in this array is a direct map to the auto config
188 static unsigned char uhso_port_map[] = {
189 UHSO_PORT_TYPE_UNKNOWN,
192 UHSO_PORT_TYPE_GPSCTL,
196 UHSO_PORT_TYPE_NETWORK,
197 UHSO_PORT_TYPE_MODEM,
202 static char uhso_port_map_max = sizeof(uhso_port_map) / sizeof(char);
204 static unsigned char uhso_mux_port_map[] = {
212 static char *uhso_port_type[] = {
213 "Unknown", /* Not a valid port */
216 "Application (Secondary)",
220 "Diagnostic (Secondary)",
228 static char *uhso_port_type_sysctl[] = {
244 #define UHSO_STATIC_IFACE 0x01
245 #define UHSO_AUTO_IFACE 0x02
247 /* ifnet device unit allocations */
248 static struct unrhdr *uhso_ifnet_unit = NULL;
250 static const STRUCT_USB_HOST_ID uhso_devs[] = {
251 #define UHSO_DEV(v,p,i) { USB_VPI(USB_VENDOR_##v, USB_PRODUCT_##v##_##p, i) }
252 /* Option GlobeTrotter MAX 7.2 with upgraded firmware */
253 UHSO_DEV(OPTION, GTMAX72, UHSO_STATIC_IFACE),
254 /* Option GlobeSurfer iCON 7.2 */
255 UHSO_DEV(OPTION, GSICON72, UHSO_STATIC_IFACE),
256 /* Option iCON 225 */
257 UHSO_DEV(OPTION, GTHSDPA, UHSO_STATIC_IFACE),
258 /* Option GlobeSurfer iCON HSUPA */
259 UHSO_DEV(OPTION, GSICONHSUPA, UHSO_STATIC_IFACE),
260 /* Option GlobeTrotter HSUPA */
261 UHSO_DEV(OPTION, GTHSUPA, UHSO_STATIC_IFACE),
263 UHSO_DEV(OPTION, GE40X, UHSO_AUTO_IFACE),
264 UHSO_DEV(OPTION, GE40X_1, UHSO_AUTO_IFACE),
265 UHSO_DEV(OPTION, GE40X_2, UHSO_AUTO_IFACE),
266 UHSO_DEV(OPTION, GE40X_3, UHSO_AUTO_IFACE),
267 /* Option GlobeSurfer iCON 401 */
268 UHSO_DEV(OPTION, ICON401, UHSO_AUTO_IFACE),
269 /* Option GlobeTrotter Module 382 */
270 UHSO_DEV(OPTION, GMT382, UHSO_AUTO_IFACE),
272 UHSO_DEV(OPTION, GTM661W, UHSO_AUTO_IFACE),
273 /* Option iCON EDGE */
274 UHSO_DEV(OPTION, ICONEDGE, UHSO_STATIC_IFACE),
275 /* Option Module HSxPA */
276 UHSO_DEV(OPTION, MODHSXPA, UHSO_STATIC_IFACE),
277 /* Option iCON 321 */
278 UHSO_DEV(OPTION, ICON321, UHSO_STATIC_IFACE),
279 /* Option iCON 322 */
280 UHSO_DEV(OPTION, GTICON322, UHSO_STATIC_IFACE),
281 /* Option iCON 505 */
282 UHSO_DEV(OPTION, ICON505, UHSO_AUTO_IFACE),
283 /* Option iCON 452 */
284 UHSO_DEV(OPTION, ICON505, UHSO_AUTO_IFACE),
288 static SYSCTL_NODE(_hw_usb, OID_AUTO, uhso, CTLFLAG_RW, 0, "USB uhso");
289 static int uhso_autoswitch = 1;
290 SYSCTL_INT(_hw_usb_uhso, OID_AUTO, auto_switch, CTLFLAG_RW,
291 &uhso_autoswitch, 0, "Automatically switch to modem mode");
295 static int uhso_debug = UHSO_DEBUG;
297 static int uhso_debug = -1;
300 SYSCTL_INT(_hw_usb_uhso, OID_AUTO, debug, CTLFLAG_RW,
301 &uhso_debug, 0, "Debug level");
303 #define UHSO_DPRINTF(n, x, ...) {\
304 if (uhso_debug >= n) {\
305 printf("%s: " x, __func__, ##__VA_ARGS__);\
309 #define UHSO_DPRINTF(n, x, ...)
312 #ifdef UHSO_DEBUG_HEXDUMP
313 # define UHSO_HEXDUMP(_buf, _len) do { \
316 const char *__buf = (const char *)_buf; \
317 for (__tmp = 0; __tmp < _len; __tmp++) \
318 printf("%02hhx ", *__buf++); \
323 # define UHSO_HEXDUMP(_buf, _len)
327 UHSO_MUX_ENDPT_INTR = 0,
344 UHSO_BULK_ENDPT_READ = 0,
345 UHSO_BULK_ENDPT_WRITE,
346 UHSO_BULK_ENDPT_INTR,
350 static usb_callback_t uhso_mux_intr_callback;
351 static usb_callback_t uhso_mux_read_callback;
352 static usb_callback_t uhso_mux_write_callback;
353 static usb_callback_t uhso_bs_read_callback;
354 static usb_callback_t uhso_bs_write_callback;
355 static usb_callback_t uhso_bs_intr_callback;
356 static usb_callback_t uhso_ifnet_read_callback;
357 static usb_callback_t uhso_ifnet_write_callback;
359 /* Config used for the default control pipes */
360 static const struct usb_config uhso_ctrl_config[UHSO_CTRL_MAX] = {
364 .direction = UE_DIR_ANY,
365 .flags = { .pipe_bof = 1, .short_xfer_ok = 1 },
366 .bufsize = sizeof(struct usb_device_request) + 1024,
367 .callback = &uhso_mux_read_callback
370 [UHSO_CTRL_WRITE] = {
373 .direction = UE_DIR_ANY,
374 .flags = { .pipe_bof = 1, .force_short_xfer = 1 },
375 .bufsize = sizeof(struct usb_device_request) + 1024,
377 .callback = &uhso_mux_write_callback
381 /* Config for the multiplexed serial ports */
382 static const struct usb_config uhso_mux_config[UHSO_MUX_ENDPT_MAX] = {
383 [UHSO_MUX_ENDPT_INTR] = {
384 .type = UE_INTERRUPT,
385 .endpoint = UE_ADDR_ANY,
386 .direction = UE_DIR_IN,
387 .flags = { .short_xfer_ok = 1 },
389 .callback = &uhso_mux_intr_callback,
393 /* Config for the raw IP-packet interface */
394 static const struct usb_config uhso_ifnet_config[UHSO_IFNET_MAX] = {
395 [UHSO_IFNET_READ] = {
397 .endpoint = UE_ADDR_ANY,
398 .direction = UE_DIR_IN,
399 .flags = { .pipe_bof = 1, .short_xfer_ok = 1 },
401 .callback = &uhso_ifnet_read_callback
403 [UHSO_IFNET_WRITE] = {
405 .endpoint = UE_ADDR_ANY,
406 .direction = UE_DIR_OUT,
407 .flags = { .pipe_bof = 1, .force_short_xfer = 1 },
409 .timeout = 5 * USB_MS_HZ,
410 .callback = &uhso_ifnet_write_callback
414 /* Config for interfaces with normal bulk serial ports */
415 static const struct usb_config uhso_bs_config[UHSO_BULK_ENDPT_MAX] = {
416 [UHSO_BULK_ENDPT_READ] = {
418 .endpoint = UE_ADDR_ANY,
419 .direction = UE_DIR_IN,
420 .flags = { .pipe_bof = 1, .short_xfer_ok = 1 },
422 .callback = &uhso_bs_read_callback
425 [UHSO_BULK_ENDPT_WRITE] = {
427 .endpoint = UE_ADDR_ANY,
428 .direction = UE_DIR_OUT,
429 .flags = { .pipe_bof = 1, .force_short_xfer = 1 },
431 .callback = &uhso_bs_write_callback
434 [UHSO_BULK_ENDPT_INTR] = {
435 .type = UE_INTERRUPT,
436 .endpoint = UE_ADDR_ANY,
437 .direction = UE_DIR_IN,
438 .flags = { .short_xfer_ok = 1 },
440 .callback = &uhso_bs_intr_callback,
444 static int uhso_probe_iface(struct uhso_softc *, int,
445 int (*probe)(struct usb_device *, int));
446 static int uhso_probe_iface_auto(struct usb_device *, int);
447 static int uhso_probe_iface_static(struct usb_device *, int);
448 static int uhso_attach_muxserial(struct uhso_softc *, struct usb_interface *,
450 static int uhso_attach_bulkserial(struct uhso_softc *, struct usb_interface *,
452 static int uhso_attach_ifnet(struct uhso_softc *, struct usb_interface *,
454 static void uhso_test_autoinst(void *, struct usb_device *,
455 struct usb_attach_arg *);
456 static int uhso_driver_loaded(struct module *, int, void *);
457 static int uhso_radio_sysctl(SYSCTL_HANDLER_ARGS);
458 static int uhso_radio_ctrl(struct uhso_softc *, int);
460 static void uhso_free(struct ucom_softc *);
461 static void uhso_ucom_start_read(struct ucom_softc *);
462 static void uhso_ucom_stop_read(struct ucom_softc *);
463 static void uhso_ucom_start_write(struct ucom_softc *);
464 static void uhso_ucom_stop_write(struct ucom_softc *);
465 static void uhso_ucom_cfg_get_status(struct ucom_softc *, uint8_t *, uint8_t *);
466 static void uhso_ucom_cfg_set_dtr(struct ucom_softc *, uint8_t);
467 static void uhso_ucom_cfg_set_rts(struct ucom_softc *, uint8_t);
468 static void uhso_if_init(void *);
469 static void uhso_if_start(struct ifnet *);
470 static void uhso_if_stop(struct uhso_softc *);
471 static int uhso_if_ioctl(struct ifnet *, u_long, caddr_t);
472 static int uhso_if_output(struct ifnet *, struct mbuf *, struct sockaddr *,
474 static void uhso_if_rxflush(void *);
476 static device_probe_t uhso_probe;
477 static device_attach_t uhso_attach;
478 static device_detach_t uhso_detach;
479 static void uhso_free_softc(struct uhso_softc *);
481 static device_method_t uhso_methods[] = {
482 DEVMETHOD(device_probe, uhso_probe),
483 DEVMETHOD(device_attach, uhso_attach),
484 DEVMETHOD(device_detach, uhso_detach),
488 static driver_t uhso_driver = {
490 .methods = uhso_methods,
491 .size = sizeof(struct uhso_softc)
494 static devclass_t uhso_devclass;
495 DRIVER_MODULE(uhso, uhub, uhso_driver, uhso_devclass, uhso_driver_loaded, 0);
496 MODULE_DEPEND(uhso, ucom, 1, 1, 1);
497 MODULE_DEPEND(uhso, usb, 1, 1, 1);
498 MODULE_VERSION(uhso, 1);
500 static struct ucom_callback uhso_ucom_callback = {
501 .ucom_cfg_get_status = &uhso_ucom_cfg_get_status,
502 .ucom_cfg_set_dtr = &uhso_ucom_cfg_set_dtr,
503 .ucom_cfg_set_rts = &uhso_ucom_cfg_set_rts,
504 .ucom_start_read = uhso_ucom_start_read,
505 .ucom_stop_read = uhso_ucom_stop_read,
506 .ucom_start_write = uhso_ucom_start_write,
507 .ucom_stop_write = uhso_ucom_stop_write,
508 .ucom_free = &uhso_free,
512 uhso_probe(device_t self)
514 struct usb_attach_arg *uaa = device_get_ivars(self);
517 if (uaa->usb_mode != USB_MODE_HOST)
519 if (uaa->info.bConfigIndex != 0)
521 if (uaa->info.bDeviceClass != 0xff)
524 error = usbd_lookup_id_by_uaa(uhso_devs, sizeof(uhso_devs), uaa);
529 * Probe device to see if we are able to attach
530 * to this interface or not.
532 if (USB_GET_DRIVER_INFO(uaa) == UHSO_AUTO_IFACE) {
533 if (uhso_probe_iface_auto(uaa->device,
534 uaa->info.bIfaceNum) == 0)
541 uhso_attach(device_t self)
543 struct uhso_softc *sc = device_get_softc(self);
544 struct usb_attach_arg *uaa = device_get_ivars(self);
545 struct usb_interface_descriptor *id;
546 struct sysctl_ctx_list *sctx;
547 struct sysctl_oid *soid;
548 struct sysctl_oid *tree = NULL, *tty_node;
549 struct ucom_softc *ucom;
557 sc->sc_udev = uaa->device;
558 mtx_init(&sc->sc_mtx, "uhso", NULL, MTX_DEF);
559 ucom_ref(&sc->sc_super_ucom);
565 id = usbd_get_interface_descriptor(uaa->iface);
566 sc->sc_ctrl_iface_no = id->bInterfaceNumber;
568 sc->sc_iface_no = uaa->info.bIfaceNum;
569 sc->sc_iface_index = uaa->info.bIfaceIndex;
571 /* Setup control pipe */
572 uerr = usbd_transfer_setup(uaa->device,
573 &sc->sc_iface_index, sc->sc_ctrl_xfer,
574 uhso_ctrl_config, UHSO_CTRL_MAX, sc, &sc->sc_mtx);
576 device_printf(self, "Failed to setup control pipe: %s\n",
581 if (USB_GET_DRIVER_INFO(uaa) == UHSO_STATIC_IFACE)
582 probe_f = uhso_probe_iface_static;
583 else if (USB_GET_DRIVER_INFO(uaa) == UHSO_AUTO_IFACE)
584 probe_f = uhso_probe_iface_auto;
588 error = uhso_probe_iface(sc, uaa->info.bIfaceNum, probe_f);
592 sctx = device_get_sysctl_ctx(sc->sc_dev);
593 soid = device_get_sysctl_tree(sc->sc_dev);
595 SYSCTL_ADD_STRING(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "type",
596 CTLFLAG_RD, uhso_port[UHSO_IFACE_PORT(sc->sc_type)], 0,
597 "Port available at this interface");
598 SYSCTL_ADD_PROC(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "radio",
599 CTLTYPE_INT | CTLFLAG_RW, sc, 0, uhso_radio_sysctl, "I", "Enable radio");
602 * The default interface description on most Option devices isn't
603 * very helpful. So we skip device_set_usb_desc and set the
604 * device description manually.
606 device_set_desc_copy(self, uhso_port_type[UHSO_IFACE_PORT_TYPE(sc->sc_type)]);
607 /* Announce device */
608 device_printf(self, "<%s port> at <%s %s> on %s\n",
609 uhso_port_type[UHSO_IFACE_PORT_TYPE(sc->sc_type)],
610 usb_get_manufacturer(uaa->device),
611 usb_get_product(uaa->device),
612 device_get_nameunit(device_get_parent(self)));
614 if (sc->sc_ttys > 0) {
615 SYSCTL_ADD_INT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "ports",
616 CTLFLAG_RD, &sc->sc_ttys, 0, "Number of attached serial ports");
618 tree = SYSCTL_ADD_NODE(sctx, SYSCTL_CHILDREN(soid), OID_AUTO,
619 "port", CTLFLAG_RD, NULL, "Serial ports");
623 * Loop through the number of found TTYs and create sysctl
626 for (i = 0; i < sc->sc_ttys; i++) {
628 ucom = &sc->sc_ucom[i];
630 if (UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_MUX)
631 port = uhso_mux_port_map[ht->ht_muxport];
633 port = UHSO_IFACE_PORT_TYPE(sc->sc_type);
635 desc = uhso_port_type_sysctl[port];
637 tty_node = SYSCTL_ADD_NODE(sctx, SYSCTL_CHILDREN(tree), OID_AUTO,
638 desc, CTLFLAG_RD, NULL, "");
641 if (sc->sc_ttys == 1)
642 snprintf(ht->ht_name, 32, "cuaU%d", ucom->sc_super->sc_unit);
644 snprintf(ht->ht_name, 32, "cuaU%d.%d",
645 ucom->sc_super->sc_unit, ucom->sc_subunit);
648 desc = uhso_port_type[port];
649 SYSCTL_ADD_STRING(sctx, SYSCTL_CHILDREN(tty_node), OID_AUTO,
650 "tty", CTLFLAG_RD, ht->ht_name, 0, "");
651 SYSCTL_ADD_STRING(sctx, SYSCTL_CHILDREN(tty_node), OID_AUTO,
652 "desc", CTLFLAG_RD, desc, 0, "");
655 device_printf(sc->sc_dev,
656 "\"%s\" port at %s\n", desc, ht->ht_name);
661 uhso_detach(sc->sc_dev);
666 uhso_detach(device_t self)
668 struct uhso_softc *sc = device_get_softc(self);
671 usbd_transfer_unsetup(sc->sc_xfer, 3);
672 usbd_transfer_unsetup(sc->sc_ctrl_xfer, UHSO_CTRL_MAX);
673 if (sc->sc_ttys > 0) {
674 ucom_detach(&sc->sc_super_ucom, sc->sc_ucom);
676 for (i = 0; i < sc->sc_ttys; i++) {
677 if (sc->sc_tty[i].ht_muxport != -1) {
678 usbd_transfer_unsetup(sc->sc_tty[i].ht_xfer,
683 free(sc->sc_tty, M_USBDEV);
684 free(sc->sc_ucom, M_USBDEV);
687 if (sc->sc_ifp != NULL) {
688 callout_drain(&sc->sc_c);
689 free_unr(uhso_ifnet_unit, sc->sc_ifp->if_dunit);
690 mtx_lock(&sc->sc_mtx);
692 bpfdetach(sc->sc_ifp);
693 if_detach(sc->sc_ifp);
695 mtx_unlock(&sc->sc_mtx);
696 usbd_transfer_unsetup(sc->sc_if_xfer, UHSO_IFNET_MAX);
699 device_claim_softc(self);
706 UCOM_UNLOAD_DRAIN(uhso);
709 uhso_free_softc(struct uhso_softc *sc)
711 if (ucom_unref(&sc->sc_super_ucom)) {
712 mtx_destroy(&sc->sc_mtx);
713 device_free_softc(sc);
718 uhso_free(struct ucom_softc *ucom)
720 uhso_free_softc(ucom->sc_parent);
724 uhso_test_autoinst(void *arg, struct usb_device *udev,
725 struct usb_attach_arg *uaa)
727 struct usb_interface *iface;
728 struct usb_interface_descriptor *id;
730 if (uaa->dev_state != UAA_DEV_READY || !uhso_autoswitch)
733 iface = usbd_get_iface(udev, 0);
737 if (id == NULL || id->bInterfaceClass != UICLASS_MASS)
739 if (usbd_lookup_id_by_uaa(uhso_devs, sizeof(uhso_devs), uaa))
740 return; /* no device match */
742 if (usb_msc_eject(udev, 0, MSC_EJECT_REZERO) == 0) {
743 /* success, mark the udev as disappearing */
744 uaa->dev_state = UAA_DEV_EJECTING;
749 uhso_driver_loaded(struct module *mod, int what, void *arg)
753 /* register our autoinstall handler */
754 uhso_etag = EVENTHANDLER_REGISTER(usb_dev_configured,
755 uhso_test_autoinst, NULL, EVENTHANDLER_PRI_ANY);
756 /* create our unit allocator for inet devs */
757 uhso_ifnet_unit = new_unrhdr(0, INT_MAX, NULL);
760 EVENTHANDLER_DEREGISTER(usb_dev_configured, uhso_etag);
761 delete_unrhdr(uhso_ifnet_unit);
770 * Probe the interface type by querying the device. The elements
771 * of an array indicates the capabilities of a particular interface.
772 * Returns a bit mask with the interface capabilities.
775 uhso_probe_iface_auto(struct usb_device *udev, int index)
777 struct usb_device_request req;
783 req.bmRequestType = UT_READ_VENDOR_DEVICE;
785 USETW(req.wValue, 0);
786 USETW(req.wIndex, 0);
787 USETW(req.wLength, 17);
789 uerr = usbd_do_request_flags(udev, NULL, &req, buf,
790 0, &actlen, USB_MS_HZ);
792 printf("%s: usbd_do_request_flags failed, %s\n",
793 __func__, usbd_errstr(uerr));
797 UHSO_DPRINTF(1, "actlen=%d\n", actlen);
798 UHSO_HEXDUMP(buf, 17);
800 if (index < 0 || index > 16) {
801 UHSO_DPRINTF(0, "Index %d out of range\n", index);
805 UHSO_DPRINTF(1, "index=%d, type=%x[%s]\n", index, buf[index],
806 uhso_port_type[(int)uhso_port_map[(int)buf[index]]]);
808 if (buf[index] >= uhso_port_map_max)
811 port = uhso_port_map[(int)buf[index]];
814 case UHSO_PORT_TYPE_NETWORK:
815 return (UHSO_IFACE_SPEC(UHSO_IF_NET | UHSO_IF_MUX,
816 UHSO_PORT_SERIAL | UHSO_PORT_NETWORK, port));
817 case UHSO_PORT_TYPE_DIAG:
818 case UHSO_PORT_TYPE_DIAG2:
819 case UHSO_PORT_TYPE_GPS:
820 case UHSO_PORT_TYPE_GPSCTL:
821 case UHSO_PORT_TYPE_CTL:
822 case UHSO_PORT_TYPE_APP:
823 case UHSO_PORT_TYPE_APP2:
824 case UHSO_PORT_TYPE_MODEM:
825 return (UHSO_IFACE_SPEC(UHSO_IF_BULK,
826 UHSO_PORT_SERIAL, port));
827 case UHSO_PORT_TYPE_MSD:
829 case UHSO_PORT_TYPE_UNKNOWN:
838 * Returns the capabilities of interfaces for devices that don't
839 * support the automatic query.
840 * Returns a bit mask with the interface capabilities.
843 uhso_probe_iface_static(struct usb_device *udev, int index)
845 struct usb_config_descriptor *cd;
847 cd = usbd_get_config_descriptor(udev);
848 if (cd->bNumInterface <= 3) {
849 /* Cards with 3 or less interfaces */
852 return UHSO_IFACE_SPEC(UHSO_IF_NET | UHSO_IF_MUX,
853 UHSO_PORT_SERIAL | UHSO_PORT_NETWORK,
854 UHSO_PORT_TYPE_NETWORK);
856 return UHSO_IFACE_SPEC(UHSO_IF_BULK,
857 UHSO_PORT_SERIAL, UHSO_PORT_TYPE_DIAG);
859 return UHSO_IFACE_SPEC(UHSO_IF_BULK,
860 UHSO_PORT_SERIAL, UHSO_PORT_TYPE_MODEM);
863 /* Cards with 4 interfaces */
866 return UHSO_IFACE_SPEC(UHSO_IF_NET | UHSO_IF_MUX,
867 UHSO_PORT_SERIAL | UHSO_PORT_NETWORK,
868 UHSO_PORT_TYPE_NETWORK);
870 return UHSO_IFACE_SPEC(UHSO_IF_BULK,
871 UHSO_PORT_SERIAL, UHSO_PORT_TYPE_DIAG2);
873 return UHSO_IFACE_SPEC(UHSO_IF_BULK,
874 UHSO_PORT_SERIAL, UHSO_PORT_TYPE_MODEM);
876 return UHSO_IFACE_SPEC(UHSO_IF_BULK,
877 UHSO_PORT_SERIAL, UHSO_PORT_TYPE_DIAG);
884 * Probes an interface for its particular capabilities and attaches if
885 * it's a supported interface.
888 uhso_probe_iface(struct uhso_softc *sc, int index,
889 int (*probe)(struct usb_device *, int))
891 struct usb_interface *iface;
894 UHSO_DPRINTF(1, "Probing for interface %d, probe_func=%p\n", index, probe);
896 type = probe(sc->sc_udev, index);
897 UHSO_DPRINTF(1, "Probe result %x\n", type);
902 iface = usbd_get_iface(sc->sc_udev, index);
904 if (UHSO_IFACE_PORT_TYPE(type) == UHSO_PORT_TYPE_NETWORK) {
905 error = uhso_attach_ifnet(sc, iface, type);
907 UHSO_DPRINTF(1, "uhso_attach_ifnet failed");
912 * If there is an additional interrupt endpoint on this
913 * interface then we most likely have a multiplexed serial port
916 if (iface->idesc->bNumEndpoints < 3) {
917 sc->sc_type = UHSO_IFACE_SPEC(
918 UHSO_IFACE_USB_TYPE(type) & ~UHSO_IF_MUX,
919 UHSO_IFACE_PORT(type) & ~UHSO_PORT_SERIAL,
920 UHSO_IFACE_PORT_TYPE(type));
924 UHSO_DPRINTF(1, "Trying to attach mux. serial\n");
925 error = uhso_attach_muxserial(sc, iface, type);
926 if (error == 0 && sc->sc_ttys > 0) {
927 error = ucom_attach(&sc->sc_super_ucom, sc->sc_ucom,
928 sc->sc_ttys, sc, &uhso_ucom_callback, &sc->sc_mtx);
930 device_printf(sc->sc_dev, "ucom_attach failed\n");
933 ucom_set_pnpinfo_usb(&sc->sc_super_ucom, sc->sc_dev);
935 mtx_lock(&sc->sc_mtx);
936 usbd_transfer_start(sc->sc_xfer[UHSO_MUX_ENDPT_INTR]);
937 mtx_unlock(&sc->sc_mtx);
939 } else if ((UHSO_IFACE_USB_TYPE(type) & UHSO_IF_BULK) &&
940 UHSO_IFACE_PORT(type) & UHSO_PORT_SERIAL) {
942 error = uhso_attach_bulkserial(sc, iface, type);
946 error = ucom_attach(&sc->sc_super_ucom, sc->sc_ucom,
947 sc->sc_ttys, sc, &uhso_ucom_callback, &sc->sc_mtx);
949 device_printf(sc->sc_dev, "ucom_attach failed\n");
952 ucom_set_pnpinfo_usb(&sc->sc_super_ucom, sc->sc_dev);
955 UHSO_DPRINTF(0, "Unknown type %x\n", type);
963 uhso_radio_ctrl(struct uhso_softc *sc, int onoff)
965 struct usb_device_request req;
968 req.bmRequestType = UT_VENDOR;
969 req.bRequest = onoff ? 0x82 : 0x81;
970 USETW(req.wValue, 0);
971 USETW(req.wIndex, 0);
972 USETW(req.wLength, 0);
974 uerr = usbd_do_request(sc->sc_udev, NULL, &req, NULL);
976 device_printf(sc->sc_dev, "usbd_do_request_flags failed: %s\n",
984 uhso_radio_sysctl(SYSCTL_HANDLER_ARGS)
986 struct uhso_softc *sc = arg1;
989 radio = sc->sc_radio;
990 error = sysctl_handle_int(oidp, &radio, 0, req);
993 if (radio != sc->sc_radio) {
994 radio = radio != 0 ? 1 : 0;
995 error = uhso_radio_ctrl(sc, radio);
997 sc->sc_radio = radio;
1004 * Expands allocated memory to fit an additional TTY.
1005 * Two arrays are kept with matching indexes, one for ucom and one
1006 * for our private data.
1009 uhso_alloc_tty(struct uhso_softc *sc)
1013 sc->sc_tty = reallocf(sc->sc_tty, sizeof(struct uhso_tty) * sc->sc_ttys,
1014 M_USBDEV, M_WAITOK | M_ZERO);
1015 if (sc->sc_tty == NULL)
1018 sc->sc_ucom = reallocf(sc->sc_ucom,
1019 sizeof(struct ucom_softc) * sc->sc_ttys, M_USBDEV, M_WAITOK | M_ZERO);
1020 if (sc->sc_ucom == NULL)
1023 sc->sc_tty[sc->sc_ttys - 1].ht_sc = sc;
1025 UHSO_DPRINTF(1, "Allocated TTY %d\n", sc->sc_ttys - 1);
1026 return (sc->sc_ttys - 1);
1030 * Attach a multiplexed serial port
1031 * Data is read/written with requests on the default control pipe. An interrupt
1032 * endpoint returns when there is new data to be read.
1035 uhso_attach_muxserial(struct uhso_softc *sc, struct usb_interface *iface,
1038 struct usb_descriptor *desc;
1043 * The class specific interface (type 0x24) descriptor subtype field
1044 * contains a bitmask that specifies which (and how many) ports that
1045 * are available through this multiplexed serial port.
1047 desc = usbd_find_descriptor(sc->sc_udev, NULL,
1048 iface->idesc->bInterfaceNumber, UDESC_CS_INTERFACE, 0xff, 0, 0);
1050 UHSO_DPRINTF(0, "Failed to find UDESC_CS_INTERFACE\n");
1054 UHSO_DPRINTF(1, "Mux port mask %x\n", desc->bDescriptorSubtype);
1055 if (desc->bDescriptorSubtype == 0)
1059 * The bitmask is one octet, loop through the number of
1060 * bits that are set and create a TTY for each.
1062 for (i = 0; i < 8; i++) {
1064 if ((port & desc->bDescriptorSubtype) == port) {
1065 UHSO_DPRINTF(2, "Found mux port %x (%d)\n", port, i);
1066 tty = uhso_alloc_tty(sc);
1069 sc->sc_tty[tty].ht_muxport = i;
1070 uerr = usbd_transfer_setup(sc->sc_udev,
1071 &sc->sc_iface_index, sc->sc_tty[tty].ht_xfer,
1072 uhso_ctrl_config, UHSO_CTRL_MAX, sc, &sc->sc_mtx);
1074 device_printf(sc->sc_dev,
1075 "Failed to setup control pipe: %s\n",
1082 /* Setup the intr. endpoint */
1083 uerr = usbd_transfer_setup(sc->sc_udev,
1084 &iface->idesc->bInterfaceNumber, sc->sc_xfer,
1085 uhso_mux_config, 1, sc, &sc->sc_mtx);
1093 * Interrupt callback for the multiplexed serial port. Indicates
1094 * which serial port has data waiting.
1097 uhso_mux_intr_callback(struct usb_xfer *xfer, usb_error_t error)
1099 struct usb_page_cache *pc;
1100 struct usb_page_search res;
1101 struct uhso_softc *sc = usbd_xfer_softc(xfer);
1102 unsigned int i, mux;
1104 UHSO_DPRINTF(3, "status %d\n", USB_GET_STATE(xfer));
1106 switch (USB_GET_STATE(xfer)) {
1107 case USB_ST_TRANSFERRED:
1109 * The multiplexed port number can be found at the first byte.
1110 * It contains a bit mask, we transform this in to an integer.
1112 pc = usbd_xfer_get_frame(xfer, 0);
1113 usbd_get_page(pc, 0, &res);
1115 i = *((unsigned char *)res.buffer);
1121 UHSO_DPRINTF(3, "mux port %d (%d)\n", mux, i);
1122 if (mux > UHSO_MPORT_TYPE_NOMAX)
1125 /* Issue a read for this serial port */
1127 sc->sc_tty[mux].ht_xfer[UHSO_CTRL_READ],
1129 usbd_transfer_start(sc->sc_tty[mux].ht_xfer[UHSO_CTRL_READ]);
1134 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
1135 usbd_transfer_submit(xfer);
1138 UHSO_DPRINTF(0, "error: %s\n", usbd_errstr(error));
1139 if (error == USB_ERR_CANCELLED)
1142 usbd_xfer_set_stall(xfer);
1148 uhso_mux_read_callback(struct usb_xfer *xfer, usb_error_t error)
1150 struct uhso_softc *sc = usbd_xfer_softc(xfer);
1151 struct usb_page_cache *pc;
1152 struct usb_device_request req;
1153 struct uhso_tty *ht;
1156 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
1158 UHSO_DPRINTF(3, "status %d\n", USB_GET_STATE(xfer));
1160 ht = usbd_xfer_get_priv(xfer);
1161 UHSO_DPRINTF(3, "ht=%p open=%d\n", ht, ht->ht_open);
1163 switch (USB_GET_STATE(xfer)) {
1164 case USB_ST_TRANSFERRED:
1165 /* Got data, send to ucom */
1166 pc = usbd_xfer_get_frame(xfer, 1);
1167 len = usbd_xfer_frame_len(xfer, 1);
1169 UHSO_DPRINTF(3, "got %d bytes on mux port %d\n", len,
1172 usbd_transfer_start(sc->sc_xfer[UHSO_MUX_ENDPT_INTR]);
1176 /* Deliver data if the TTY is open, discard otherwise */
1178 ucom_put_data(&sc->sc_ucom[ht->ht_muxport], pc, 0, len);
1182 memset(&req, 0, sizeof(struct usb_device_request));
1183 req.bmRequestType = UT_READ_CLASS_INTERFACE;
1184 req.bRequest = UCDC_GET_ENCAPSULATED_RESPONSE;
1185 USETW(req.wValue, 0);
1186 USETW(req.wIndex, ht->ht_muxport);
1187 USETW(req.wLength, 1024);
1189 pc = usbd_xfer_get_frame(xfer, 0);
1190 usbd_copy_in(pc, 0, &req, sizeof(req));
1192 usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
1193 usbd_xfer_set_frame_len(xfer, 1, 1024);
1194 usbd_xfer_set_frames(xfer, 2);
1195 usbd_transfer_submit(xfer);
1198 UHSO_DPRINTF(0, "error: %s\n", usbd_errstr(error));
1199 if (error == USB_ERR_CANCELLED)
1201 usbd_xfer_set_stall(xfer);
1207 uhso_mux_write_callback(struct usb_xfer *xfer, usb_error_t error)
1209 struct uhso_softc *sc = usbd_xfer_softc(xfer);
1210 struct uhso_tty *ht;
1211 struct usb_page_cache *pc;
1212 struct usb_device_request req;
1214 struct usb_page_search res;
1216 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
1218 ht = usbd_xfer_get_priv(xfer);
1219 UHSO_DPRINTF(3, "status=%d, using mux port %d\n",
1220 USB_GET_STATE(xfer), ht->ht_muxport);
1222 switch (USB_GET_STATE(xfer)) {
1223 case USB_ST_TRANSFERRED:
1224 UHSO_DPRINTF(3, "wrote %zd data bytes to muxport %d\n",
1225 actlen - sizeof(struct usb_device_request) ,
1229 pc = usbd_xfer_get_frame(xfer, 1);
1230 if (ucom_get_data(&sc->sc_ucom[ht->ht_muxport], pc,
1233 usbd_get_page(pc, 0, &res);
1235 memset(&req, 0, sizeof(struct usb_device_request));
1236 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
1237 req.bRequest = UCDC_SEND_ENCAPSULATED_COMMAND;
1238 USETW(req.wValue, 0);
1239 USETW(req.wIndex, ht->ht_muxport);
1240 USETW(req.wLength, actlen);
1242 pc = usbd_xfer_get_frame(xfer, 0);
1243 usbd_copy_in(pc, 0, &req, sizeof(req));
1245 usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
1246 usbd_xfer_set_frame_len(xfer, 1, actlen);
1247 usbd_xfer_set_frames(xfer, 2);
1249 UHSO_DPRINTF(3, "Prepared %d bytes for transmit "
1250 "on muxport %d\n", actlen, ht->ht_muxport);
1252 usbd_transfer_submit(xfer);
1256 UHSO_DPRINTF(0, "error: %s\n", usbd_errstr(error));
1257 if (error == USB_ERR_CANCELLED)
1264 uhso_attach_bulkserial(struct uhso_softc *sc, struct usb_interface *iface,
1270 /* Try attaching RD/WR/INTR first */
1271 uerr = usbd_transfer_setup(sc->sc_udev,
1272 &iface->idesc->bInterfaceNumber, sc->sc_xfer,
1273 uhso_bs_config, UHSO_BULK_ENDPT_MAX, sc, &sc->sc_mtx);
1275 /* Try only RD/WR */
1276 uerr = usbd_transfer_setup(sc->sc_udev,
1277 &iface->idesc->bInterfaceNumber, sc->sc_xfer,
1278 uhso_bs_config, UHSO_BULK_ENDPT_MAX - 1, sc, &sc->sc_mtx);
1281 UHSO_DPRINTF(0, "usbd_transfer_setup failed");
1285 tty = uhso_alloc_tty(sc);
1287 usbd_transfer_unsetup(sc->sc_xfer, UHSO_BULK_ENDPT_MAX);
1291 sc->sc_tty[tty].ht_muxport = -1;
1296 uhso_bs_read_callback(struct usb_xfer *xfer, usb_error_t error)
1298 struct uhso_softc *sc = usbd_xfer_softc(xfer);
1299 struct usb_page_cache *pc;
1302 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
1304 UHSO_DPRINTF(3, "status %d, actlen=%d\n", USB_GET_STATE(xfer), actlen);
1306 switch (USB_GET_STATE(xfer)) {
1307 case USB_ST_TRANSFERRED:
1308 pc = usbd_xfer_get_frame(xfer, 0);
1309 ucom_put_data(&sc->sc_ucom[0], pc, 0, actlen);
1313 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
1314 usbd_transfer_submit(xfer);
1317 UHSO_DPRINTF(0, "error: %s\n", usbd_errstr(error));
1318 if (error == USB_ERR_CANCELLED)
1320 usbd_xfer_set_stall(xfer);
1326 uhso_bs_write_callback(struct usb_xfer *xfer, usb_error_t error)
1328 struct uhso_softc *sc = usbd_xfer_softc(xfer);
1329 struct usb_page_cache *pc;
1332 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
1334 UHSO_DPRINTF(3, "status %d, actlen=%d\n", USB_GET_STATE(xfer), actlen);
1336 switch (USB_GET_STATE(xfer)) {
1337 case USB_ST_TRANSFERRED:
1340 pc = usbd_xfer_get_frame(xfer, 0);
1341 if (ucom_get_data(&sc->sc_ucom[0], pc, 0, 8192, &actlen)) {
1342 usbd_xfer_set_frame_len(xfer, 0, actlen);
1343 usbd_transfer_submit(xfer);
1348 UHSO_DPRINTF(0, "error: %s\n", usbd_errstr(error));
1349 if (error == USB_ERR_CANCELLED)
1351 usbd_xfer_set_stall(xfer);
1357 uhso_bs_cfg(struct uhso_softc *sc)
1359 struct usb_device_request req;
1362 if (!(UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_BULK))
1365 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
1366 req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
1367 USETW(req.wValue, sc->sc_line);
1368 USETW(req.wIndex, sc->sc_iface_no);
1369 USETW(req.wLength, 0);
1371 uerr = ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom[0], &req, NULL, 0, 1000);
1373 device_printf(sc->sc_dev, "failed to set ctrl line state to "
1374 "0x%02x: %s\n", sc->sc_line, usbd_errstr(uerr));
1379 uhso_bs_intr_callback(struct usb_xfer *xfer, usb_error_t error)
1381 struct uhso_softc *sc = usbd_xfer_softc(xfer);
1382 struct usb_page_cache *pc;
1384 struct usb_cdc_notification cdc;
1386 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
1387 UHSO_DPRINTF(3, "status %d, actlen=%d\n", USB_GET_STATE(xfer), actlen);
1389 switch (USB_GET_STATE(xfer)) {
1390 case USB_ST_TRANSFERRED:
1391 if (actlen < UCDC_NOTIFICATION_LENGTH) {
1392 UHSO_DPRINTF(0, "UCDC notification too short: %d\n", actlen);
1395 else if (actlen > (int)sizeof(struct usb_cdc_notification)) {
1396 UHSO_DPRINTF(0, "UCDC notification too large: %d\n", actlen);
1397 actlen = sizeof(struct usb_cdc_notification);
1400 pc = usbd_xfer_get_frame(xfer, 0);
1401 usbd_copy_out(pc, 0, &cdc, actlen);
1403 if (UGETW(cdc.wIndex) != sc->sc_iface_no) {
1404 UHSO_DPRINTF(0, "Interface mismatch, got %d expected %d\n",
1405 UGETW(cdc.wIndex), sc->sc_iface_no);
1409 if (cdc.bmRequestType == UCDC_NOTIFICATION &&
1410 cdc.bNotification == UCDC_N_SERIAL_STATE) {
1411 UHSO_DPRINTF(2, "notify = 0x%02x\n", cdc.data[0]);
1415 if (cdc.data[0] & UCDC_N_SERIAL_RI)
1416 sc->sc_msr |= SER_RI;
1417 if (cdc.data[0] & UCDC_N_SERIAL_DSR)
1418 sc->sc_msr |= SER_DSR;
1419 if (cdc.data[0] & UCDC_N_SERIAL_DCD)
1420 sc->sc_msr |= SER_DCD;
1422 ucom_status_change(&sc->sc_ucom[0]);
1427 if (error == USB_ERR_CANCELLED)
1429 usbd_xfer_set_stall(xfer);
1435 uhso_ucom_cfg_get_status(struct ucom_softc *ucom, uint8_t *lsr, uint8_t *msr)
1437 struct uhso_softc *sc = ucom->sc_parent;
1444 uhso_ucom_cfg_set_dtr(struct ucom_softc *ucom, uint8_t onoff)
1446 struct uhso_softc *sc = ucom->sc_parent;
1448 if (!(UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_BULK))
1452 sc->sc_line |= UCDC_LINE_DTR;
1454 sc->sc_line &= ~UCDC_LINE_DTR;
1460 uhso_ucom_cfg_set_rts(struct ucom_softc *ucom, uint8_t onoff)
1462 struct uhso_softc *sc = ucom->sc_parent;
1464 if (!(UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_BULK))
1468 sc->sc_line |= UCDC_LINE_RTS;
1470 sc->sc_line &= ~UCDC_LINE_RTS;
1476 uhso_ucom_start_read(struct ucom_softc *ucom)
1478 struct uhso_softc *sc = ucom->sc_parent;
1480 UHSO_DPRINTF(3, "unit=%d, subunit=%d\n",
1481 ucom->sc_super->sc_unit, ucom->sc_subunit);
1483 if (UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_MUX) {
1484 sc->sc_tty[ucom->sc_subunit].ht_open = 1;
1485 usbd_transfer_start(sc->sc_xfer[UHSO_MUX_ENDPT_INTR]);
1487 else if (UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_BULK) {
1488 sc->sc_tty[0].ht_open = 1;
1489 usbd_transfer_start(sc->sc_xfer[UHSO_BULK_ENDPT_READ]);
1490 if (sc->sc_xfer[UHSO_BULK_ENDPT_INTR] != NULL)
1491 usbd_transfer_start(sc->sc_xfer[UHSO_BULK_ENDPT_INTR]);
1496 uhso_ucom_stop_read(struct ucom_softc *ucom)
1499 struct uhso_softc *sc = ucom->sc_parent;
1501 if (UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_MUX) {
1502 sc->sc_tty[ucom->sc_subunit].ht_open = 0;
1504 sc->sc_tty[ucom->sc_subunit].ht_xfer[UHSO_CTRL_READ]);
1506 else if (UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_BULK) {
1507 sc->sc_tty[0].ht_open = 0;
1508 usbd_transfer_start(sc->sc_xfer[UHSO_BULK_ENDPT_READ]);
1509 if (sc->sc_xfer[UHSO_BULK_ENDPT_INTR] != NULL)
1510 usbd_transfer_stop(sc->sc_xfer[UHSO_BULK_ENDPT_INTR]);
1515 uhso_ucom_start_write(struct ucom_softc *ucom)
1517 struct uhso_softc *sc = ucom->sc_parent;
1519 if (UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_MUX) {
1520 UHSO_DPRINTF(3, "local unit %d\n", ucom->sc_subunit);
1522 usbd_transfer_start(sc->sc_xfer[UHSO_MUX_ENDPT_INTR]);
1525 sc->sc_tty[ucom->sc_subunit].ht_xfer[UHSO_CTRL_WRITE],
1526 &sc->sc_tty[ucom->sc_subunit]);
1527 usbd_transfer_start(
1528 sc->sc_tty[ucom->sc_subunit].ht_xfer[UHSO_CTRL_WRITE]);
1531 else if (UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_BULK) {
1532 usbd_transfer_start(sc->sc_xfer[UHSO_BULK_ENDPT_WRITE]);
1537 uhso_ucom_stop_write(struct ucom_softc *ucom)
1539 struct uhso_softc *sc = ucom->sc_parent;
1541 if (UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_MUX) {
1543 sc->sc_tty[ucom->sc_subunit].ht_xfer[UHSO_CTRL_WRITE]);
1545 else if (UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_BULK) {
1546 usbd_transfer_stop(sc->sc_xfer[UHSO_BULK_ENDPT_WRITE]);
1551 uhso_attach_ifnet(struct uhso_softc *sc, struct usb_interface *iface, int type)
1555 struct sysctl_ctx_list *sctx;
1556 struct sysctl_oid *soid;
1557 unsigned int devunit;
1559 uerr = usbd_transfer_setup(sc->sc_udev,
1560 &iface->idesc->bInterfaceNumber, sc->sc_if_xfer,
1561 uhso_ifnet_config, UHSO_IFNET_MAX, sc, &sc->sc_mtx);
1563 UHSO_DPRINTF(0, "usbd_transfer_setup failed: %s\n",
1568 sc->sc_ifp = ifp = if_alloc(IFT_OTHER);
1569 if (sc->sc_ifp == NULL) {
1570 device_printf(sc->sc_dev, "if_alloc() failed\n");
1574 callout_init_mtx(&sc->sc_c, &sc->sc_mtx, 0);
1575 mtx_lock(&sc->sc_mtx);
1576 callout_reset(&sc->sc_c, 1, uhso_if_rxflush, sc);
1577 mtx_unlock(&sc->sc_mtx);
1580 * We create our own unit numbers for ifnet devices because the
1581 * USB interface unit numbers can be at arbitrary positions yielding
1582 * odd looking device names.
1584 devunit = alloc_unr(uhso_ifnet_unit);
1586 if_initname(ifp, device_get_name(sc->sc_dev), devunit);
1587 ifp->if_mtu = UHSO_MAX_MTU;
1588 ifp->if_ioctl = uhso_if_ioctl;
1589 ifp->if_init = uhso_if_init;
1590 ifp->if_start = uhso_if_start;
1591 ifp->if_output = uhso_if_output;
1592 ifp->if_flags = IFF_BROADCAST | IFF_MULTICAST | IFF_NOARP;
1594 IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
1595 ifp->if_snd.ifq_drv_maxlen = ifqmaxlen;
1596 IFQ_SET_READY(&ifp->if_snd);
1599 bpfattach(ifp, DLT_RAW, 0);
1601 sctx = device_get_sysctl_ctx(sc->sc_dev);
1602 soid = device_get_sysctl_tree(sc->sc_dev);
1603 /* Unlocked read... */
1604 SYSCTL_ADD_STRING(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "netif",
1605 CTLFLAG_RD, ifp->if_xname, 0, "Attached network interface");
1611 uhso_ifnet_read_callback(struct usb_xfer *xfer, usb_error_t error)
1613 struct uhso_softc *sc = usbd_xfer_softc(xfer);
1615 struct usb_page_cache *pc;
1618 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
1620 UHSO_DPRINTF(3, "status=%d, actlen=%d\n", USB_GET_STATE(xfer), actlen);
1622 switch (USB_GET_STATE(xfer)) {
1623 case USB_ST_TRANSFERRED:
1624 if (actlen > 0 && (sc->sc_ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1625 pc = usbd_xfer_get_frame(xfer, 0);
1626 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
1627 usbd_copy_out(pc, 0, mtod(m, uint8_t *), actlen);
1628 m->m_pkthdr.len = m->m_len = actlen;
1629 /* Enqueue frame for further processing */
1630 _IF_ENQUEUE(&sc->sc_rxq, m);
1631 if (!callout_pending(&sc->sc_c) ||
1632 !callout_active(&sc->sc_c)) {
1633 callout_schedule(&sc->sc_c, 1);
1639 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
1640 usbd_transfer_submit(xfer);
1643 UHSO_DPRINTF(0, "error: %s\n", usbd_errstr(error));
1644 if (error == USB_ERR_CANCELLED)
1646 usbd_xfer_set_stall(xfer);
1652 * Deferred RX processing, called with mutex locked.
1654 * Each frame we receive might contain several small ip-packets as well
1655 * as partial ip-packets. We need to separate/assemble them into individual
1656 * packets before sending them to the ip-layer.
1659 uhso_if_rxflush(void *arg)
1661 struct uhso_softc *sc = arg;
1662 struct ifnet *ifp = sc->sc_ifp;
1664 struct mbuf *m, *m0, *mwait;
1667 struct ip6_hdr *ip6;
1673 mwait = sc->sc_mwait;
1676 _IF_DEQUEUE(&sc->sc_rxq, m);
1679 UHSO_DPRINTF(3, "dequeue m=%p, len=%d\n", m, m->m_len);
1681 mtx_unlock(&sc->sc_mtx);
1683 /* Do we have a partial packet waiting? */
1684 if (mwait != NULL) {
1688 UHSO_DPRINTF(3, "partial m0=%p(%d), concat w/ m=%p(%d)\n",
1689 m0, m0->m_len, m, m->m_len);
1690 len = m->m_len + m0->m_len;
1692 /* Concat mbufs and fix headers */
1694 m0->m_pkthdr.len = len;
1695 m->m_flags &= ~M_PKTHDR;
1697 m = m_pullup(m0, sizeof(struct ip));
1700 UHSO_DPRINTF(0, "m_pullup failed\n");
1701 mtx_lock(&sc->sc_mtx);
1704 UHSO_DPRINTF(3, "Constructed mbuf=%p, len=%d\n",
1705 m, m->m_pkthdr.len);
1708 cp = mtod(m, uint8_t *);
1709 ip = (struct ip *)cp;
1711 ip6 = (struct ip6_hdr *)cp;
1714 /* Check for IPv4 */
1715 if (ip->ip_v == IPVERSION) {
1716 iplen = htons(ip->ip_len);
1720 /* Check for IPv6 */
1721 else if ((ip6->ip6_vfc & IPV6_VERSION_MASK) == IPV6_VERSION) {
1722 iplen = htons(ip6->ip6_plen);
1727 UHSO_DPRINTF(0, "got unexpected ip version %d, "
1728 "m=%p, len=%d\n", (*cp & 0xf0) >> 4, m, m->m_len);
1730 UHSO_HEXDUMP(cp, 4);
1733 mtx_lock(&sc->sc_mtx);
1738 UHSO_DPRINTF(0, "Zero IP length\n");
1742 mtx_lock(&sc->sc_mtx);
1746 UHSO_DPRINTF(3, "m=%p, len=%d, cp=%p, iplen=%d\n",
1747 m, m->m_pkthdr.len, cp, iplen);
1751 /* More IP packets in this mbuf */
1752 if (iplen < m->m_pkthdr.len) {
1756 * Allocate a new mbuf for this IP packet and
1757 * copy the IP-packet into it.
1759 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
1760 memcpy(mtod(m, uint8_t *), mtod(m0, uint8_t *), iplen);
1761 m->m_pkthdr.len = m->m_len = iplen;
1763 /* Adjust the size of the original mbuf */
1765 m0 = m_defrag(m0, M_WAITOK);
1767 UHSO_DPRINTF(3, "New mbuf=%p, len=%d/%d, m0=%p, "
1768 "m0_len=%d/%d\n", m, m->m_pkthdr.len, m->m_len,
1769 m0, m0->m_pkthdr.len, m0->m_len);
1771 else if (iplen > m->m_pkthdr.len) {
1772 UHSO_DPRINTF(3, "Deferred mbuf=%p, len=%d\n",
1773 m, m->m_pkthdr.len);
1776 mtx_lock(&sc->sc_mtx);
1781 m->m_pkthdr.rcvif = ifp;
1783 /* Dispatch to IP layer */
1784 BPF_MTAP(sc->sc_ifp, m);
1785 M_SETFIB(m, ifp->if_fib);
1786 netisr_dispatch(isr, m);
1787 m = m0 != NULL ? m0 : NULL;
1788 mtx_lock(&sc->sc_mtx);
1790 sc->sc_mwait = mwait;
1794 uhso_ifnet_write_callback(struct usb_xfer *xfer, usb_error_t error)
1796 struct uhso_softc *sc = usbd_xfer_softc(xfer);
1797 struct ifnet *ifp = sc->sc_ifp;
1798 struct usb_page_cache *pc;
1802 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
1804 UHSO_DPRINTF(3, "status %d, actlen=%d\n", USB_GET_STATE(xfer), actlen);
1806 switch (USB_GET_STATE(xfer)) {
1807 case USB_ST_TRANSFERRED:
1809 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1812 IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
1816 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1818 if (m->m_pkthdr.len > MCLBYTES)
1819 m->m_pkthdr.len = MCLBYTES;
1821 usbd_xfer_set_frame_len(xfer, 0, m->m_pkthdr.len);
1822 pc = usbd_xfer_get_frame(xfer, 0);
1823 usbd_m_copy_in(pc, 0, m, 0, m->m_pkthdr.len);
1824 usbd_transfer_submit(xfer);
1830 UHSO_DPRINTF(0, "error: %s\n", usbd_errstr(error));
1831 if (error == USB_ERR_CANCELLED)
1833 usbd_xfer_set_stall(xfer);
1839 uhso_if_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1841 struct uhso_softc *sc;
1847 if (ifp->if_flags & IFF_UP) {
1848 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1853 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1854 mtx_lock(&sc->sc_mtx);
1856 mtx_unlock(&sc->sc_mtx);
1861 case SIOCSIFDSTADDR:
1872 uhso_if_init(void *priv)
1874 struct uhso_softc *sc = priv;
1875 struct ifnet *ifp = sc->sc_ifp;
1877 mtx_lock(&sc->sc_mtx);
1880 ifp->if_flags |= IFF_UP;
1881 ifp->if_drv_flags |= IFF_DRV_RUNNING;
1882 mtx_unlock(&sc->sc_mtx);
1884 UHSO_DPRINTF(2, "ifnet initialized\n");
1888 uhso_if_output(struct ifnet *ifp, struct mbuf *m0, struct sockaddr *dst,
1893 /* Only IPv4/6 support */
1894 if (dst->sa_family != AF_INET
1896 && dst->sa_family != AF_INET6
1899 return (EAFNOSUPPORT);
1902 error = (ifp->if_transmit)(ifp, m0);
1912 uhso_if_start(struct ifnet *ifp)
1914 struct uhso_softc *sc = ifp->if_softc;
1916 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
1917 UHSO_DPRINTF(1, "Not running\n");
1921 mtx_lock(&sc->sc_mtx);
1922 usbd_transfer_start(sc->sc_if_xfer[UHSO_IFNET_READ]);
1923 usbd_transfer_start(sc->sc_if_xfer[UHSO_IFNET_WRITE]);
1924 mtx_unlock(&sc->sc_mtx);
1925 UHSO_DPRINTF(3, "interface started\n");
1929 uhso_if_stop(struct uhso_softc *sc)
1932 usbd_transfer_stop(sc->sc_if_xfer[UHSO_IFNET_READ]);
1933 usbd_transfer_stop(sc->sc_if_xfer[UHSO_IFNET_WRITE]);
1934 sc->sc_ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);