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_device.h>
66 #include <dev/usb/usb_busdma.h>
67 #include <dev/usb/usb_controller.h>
68 #include <dev/usb/usb_bus.h>
69 #include <dev/usb/serial/usb_serial.h>
70 #include <dev/usb/usb_msctest.h>
73 struct uhso_softc *ht_sc;
74 struct usb_xfer *ht_xfer[3];
75 int ht_muxport; /* Mux. port no */
82 struct usb_device *sc_udev;
84 uint32_t sc_type; /* Interface definition */
87 struct usb_xfer *sc_xfer[3];
89 uint8_t sc_iface_index;
92 struct usb_xfer * sc_ctrl_xfer[2];
93 uint8_t sc_ctrl_iface_no;
96 struct usb_xfer *sc_if_xfer[2];
98 struct mbuf *sc_mwait; /* Partial packet */
99 size_t sc_waitlen; /* No. of outstanding bytes */
100 struct ifqueue sc_rxq;
103 /* TTY related structures */
104 struct ucom_super_softc sc_super_ucom;
106 struct uhso_tty *sc_tty;
107 struct ucom_softc *sc_ucom;
113 #define UHSO_MAX_MTU 2048
116 * There are mainly two type of cards floating around.
117 * The first one has 2,3 or 4 interfaces with a multiplexed serial port
118 * and packet interface on the first interface and bulk serial ports
120 * The second type of card has several other interfaces, their purpose
121 * can be detected during run-time.
123 #define UHSO_IFACE_SPEC(usb_type, port, port_type) \
124 (((usb_type) << 24) | ((port) << 16) | (port_type))
126 #define UHSO_IFACE_USB_TYPE(x) ((x >> 24) & 0xff)
127 #define UHSO_IFACE_PORT(x) ((x >> 16) & 0xff)
128 #define UHSO_IFACE_PORT_TYPE(x) (x & 0xff)
131 * USB interface types
133 #define UHSO_IF_NET 0x01 /* Network packet interface */
134 #define UHSO_IF_MUX 0x02 /* Multiplexed serial port */
135 #define UHSO_IF_BULK 0x04 /* Bulk interface */
140 #define UHSO_PORT_UNKNOWN 0x00
141 #define UHSO_PORT_SERIAL 0x01 /* Serial port */
142 #define UHSO_PORT_NETWORK 0x02 /* Network packet interface */
145 * Multiplexed serial port destination sub-port names
147 #define UHSO_MPORT_TYPE_CTL 0x00 /* Control port */
148 #define UHSO_MPORT_TYPE_APP 0x01 /* Application */
149 #define UHSO_MPORT_TYPE_PCSC 0x02
150 #define UHSO_MPORT_TYPE_GPS 0x03
151 #define UHSO_MPORT_TYPE_APP2 0x04 /* Secondary application */
152 #define UHSO_MPORT_TYPE_MAX UHSO_MPORT_TYPE_APP2
153 #define UHSO_MPORT_TYPE_NOMAX 8 /* Max number of mux ports */
157 * Note that these definitions are arbitrary and do not match the values
158 * returned by the auto config descriptor.
160 #define UHSO_PORT_TYPE_UNKNOWN 0x00
161 #define UHSO_PORT_TYPE_CTL 0x01
162 #define UHSO_PORT_TYPE_APP 0x02
163 #define UHSO_PORT_TYPE_APP2 0x03
164 #define UHSO_PORT_TYPE_MODEM 0x04
165 #define UHSO_PORT_TYPE_NETWORK 0x05
166 #define UHSO_PORT_TYPE_DIAG 0x06
167 #define UHSO_PORT_TYPE_DIAG2 0x07
168 #define UHSO_PORT_TYPE_GPS 0x08
169 #define UHSO_PORT_TYPE_GPSCTL 0x09
170 #define UHSO_PORT_TYPE_PCSC 0x0a
171 #define UHSO_PORT_TYPE_MSD 0x0b
172 #define UHSO_PORT_TYPE_VOICE 0x0c
173 #define UHSO_PORT_TYPE_MAX 0x0c
175 static eventhandler_tag uhso_etag;
177 /* Overall port type */
178 static char *uhso_port[] = {
186 * Map between interface port type read from device and description type.
187 * The position in this array is a direct map to the auto config
190 static unsigned char uhso_port_map[] = {
191 UHSO_PORT_TYPE_UNKNOWN,
194 UHSO_PORT_TYPE_GPSCTL,
198 UHSO_PORT_TYPE_NETWORK,
199 UHSO_PORT_TYPE_MODEM,
204 static char uhso_port_map_max = sizeof(uhso_port_map) / sizeof(char);
206 static unsigned char uhso_mux_port_map[] = {
214 static char *uhso_port_type[] = {
215 "Unknown", /* Not a valid port */
218 "Application (Secondary)",
222 "Diagnostic (Secondary)",
230 static char *uhso_port_type_sysctl[] = {
246 #define UHSO_STATIC_IFACE 0x01
247 #define UHSO_AUTO_IFACE 0x02
249 /* ifnet device unit allocations */
250 static struct unrhdr *uhso_ifnet_unit = NULL;
252 static const struct usb_device_id uhso_devs[] = {
253 #define UHSO_DEV(v,p,i) { USB_VPI(USB_VENDOR_##v, USB_PRODUCT_##v##_##p, i) }
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),
271 /* Option iCON EDGE */
272 UHSO_DEV(OPTION, ICONEDGE, UHSO_STATIC_IFACE),
273 /* Option Module HSxPA */
274 UHSO_DEV(OPTION, MODHSXPA, UHSO_STATIC_IFACE),
275 /* Option iCON 321 */
276 UHSO_DEV(OPTION, ICON321, UHSO_STATIC_IFACE),
277 /* Option iCON 322 */
278 UHSO_DEV(OPTION, GTICON322, UHSO_STATIC_IFACE),
279 /* Option iCON 505 */
280 UHSO_DEV(OPTION, ICON505, UHSO_AUTO_IFACE),
281 /* Option iCON 452 */
282 UHSO_DEV(OPTION, ICON505, UHSO_AUTO_IFACE),
286 SYSCTL_NODE(_hw_usb, OID_AUTO, uhso, CTLFLAG_RW, 0, "USB uhso");
287 static int uhso_autoswitch = 1;
288 SYSCTL_INT(_hw_usb_uhso, OID_AUTO, auto_switch, CTLFLAG_RW,
289 &uhso_autoswitch, 0, "Automatically switch to modem mode");
293 static int uhso_debug = UHSO_DEBUG;
295 static int uhso_debug = -1;
298 SYSCTL_INT(_hw_usb_uhso, OID_AUTO, debug, CTLFLAG_RW,
299 &uhso_debug, 0, "Debug level");
301 #define UHSO_DPRINTF(n, x, ...) {\
302 if (uhso_debug >= n) {\
303 printf("%s: " x, __func__, ##__VA_ARGS__);\
307 #define UHSO_DPRINTF(n, x, ...)
310 #ifdef UHSO_DEBUG_HEXDUMP
311 # define UHSO_HEXDUMP(_buf, _len) do { \
314 const char *__buf = (const char *)_buf; \
315 for (__tmp = 0; __tmp < _len; __tmp++) \
316 printf("%02hhx ", *__buf++); \
321 # define UHSO_HEXDUMP(_buf, _len)
325 UHSO_MUX_ENDPT_INTR = 0,
342 UHSO_BULK_ENDPT_READ = 0,
343 UHSO_BULK_ENDPT_WRITE,
344 UHSO_BULK_ENDPT_INTR,
348 static usb_callback_t uhso_mux_intr_callback;
349 static usb_callback_t uhso_mux_read_callback;
350 static usb_callback_t uhso_mux_write_callback;
351 static usb_callback_t uhso_bs_read_callback;
352 static usb_callback_t uhso_bs_write_callback;
353 static usb_callback_t uhso_bs_intr_callback;
354 static usb_callback_t uhso_ifnet_read_callback;
355 static usb_callback_t uhso_ifnet_write_callback;
357 /* Config used for the default control pipes */
358 static const struct usb_config uhso_ctrl_config[UHSO_CTRL_MAX] = {
362 .direction = UE_DIR_ANY,
363 .flags = { .pipe_bof = 1, .short_xfer_ok = 1 },
364 .bufsize = sizeof(struct usb_device_request) + 1024,
365 .callback = &uhso_mux_read_callback
368 [UHSO_CTRL_WRITE] = {
371 .direction = UE_DIR_ANY,
372 .flags = { .pipe_bof = 1, .force_short_xfer = 1 },
373 .bufsize = sizeof(struct usb_device_request) + 1024,
375 .callback = &uhso_mux_write_callback
379 /* Config for the multiplexed serial ports */
380 static const struct usb_config uhso_mux_config[UHSO_MUX_ENDPT_MAX] = {
381 [UHSO_MUX_ENDPT_INTR] = {
382 .type = UE_INTERRUPT,
383 .endpoint = UE_ADDR_ANY,
384 .direction = UE_DIR_IN,
385 .flags = { .short_xfer_ok = 1 },
387 .callback = &uhso_mux_intr_callback,
391 /* Config for the raw IP-packet interface */
392 static const struct usb_config uhso_ifnet_config[UHSO_IFNET_MAX] = {
393 [UHSO_IFNET_READ] = {
395 .endpoint = UE_ADDR_ANY,
396 .direction = UE_DIR_IN,
397 .flags = { .pipe_bof = 1, .short_xfer_ok = 1 },
399 .callback = &uhso_ifnet_read_callback
401 [UHSO_IFNET_WRITE] = {
403 .endpoint = UE_ADDR_ANY,
404 .direction = UE_DIR_OUT,
405 .flags = { .pipe_bof = 1, .force_short_xfer = 1 },
407 .timeout = 5 * USB_MS_HZ,
408 .callback = &uhso_ifnet_write_callback
412 /* Config for interfaces with normal bulk serial ports */
413 static const struct usb_config uhso_bs_config[UHSO_BULK_ENDPT_MAX] = {
414 [UHSO_BULK_ENDPT_READ] = {
416 .endpoint = UE_ADDR_ANY,
417 .direction = UE_DIR_IN,
418 .flags = { .pipe_bof = 1, .short_xfer_ok = 1 },
420 .callback = &uhso_bs_read_callback
423 [UHSO_BULK_ENDPT_WRITE] = {
425 .endpoint = UE_ADDR_ANY,
426 .direction = UE_DIR_OUT,
427 .flags = { .pipe_bof = 1, .force_short_xfer = 1 },
429 .callback = &uhso_bs_write_callback
432 [UHSO_BULK_ENDPT_INTR] = {
433 .type = UE_INTERRUPT,
434 .endpoint = UE_ADDR_ANY,
435 .direction = UE_DIR_IN,
436 .flags = { .short_xfer_ok = 1 },
438 .callback = &uhso_bs_intr_callback,
442 static int uhso_probe_iface(struct uhso_softc *, int,
443 int (*probe)(struct usb_device *, int));
444 static int uhso_probe_iface_auto(struct usb_device *, int);
445 static int uhso_probe_iface_static(struct usb_device *, int);
446 static int uhso_attach_muxserial(struct uhso_softc *, struct usb_interface *,
448 static int uhso_attach_bulkserial(struct uhso_softc *, struct usb_interface *,
450 static int uhso_attach_ifnet(struct uhso_softc *, struct usb_interface *,
452 static void uhso_test_autoinst(void *, struct usb_device *,
453 struct usb_attach_arg *);
454 static int uhso_driver_loaded(struct module *, int, void *);
455 static int uhso_radio_sysctl(SYSCTL_HANDLER_ARGS);
456 static int uhso_radio_ctrl(struct uhso_softc *, int);
458 static void uhso_ucom_start_read(struct ucom_softc *);
459 static void uhso_ucom_stop_read(struct ucom_softc *);
460 static void uhso_ucom_start_write(struct ucom_softc *);
461 static void uhso_ucom_stop_write(struct ucom_softc *);
462 static void uhso_ucom_cfg_get_status(struct ucom_softc *, uint8_t *, uint8_t *);
463 static void uhso_ucom_cfg_set_dtr(struct ucom_softc *, uint8_t);
464 static void uhso_ucom_cfg_set_rts(struct ucom_softc *, uint8_t);
465 static void uhso_if_init(void *);
466 static void uhso_if_start(struct ifnet *);
467 static void uhso_if_stop(struct uhso_softc *);
468 static int uhso_if_ioctl(struct ifnet *, u_long, caddr_t);
469 static int uhso_if_output(struct ifnet *, struct mbuf *, struct sockaddr *,
471 static void uhso_if_rxflush(void *);
473 static device_probe_t uhso_probe;
474 static device_attach_t uhso_attach;
475 static device_detach_t uhso_detach;
477 static device_method_t uhso_methods[] = {
478 DEVMETHOD(device_probe, uhso_probe),
479 DEVMETHOD(device_attach, uhso_attach),
480 DEVMETHOD(device_detach, uhso_detach),
484 static driver_t uhso_driver = {
487 sizeof(struct uhso_softc)
490 static devclass_t uhso_devclass;
491 DRIVER_MODULE(uhso, uhub, uhso_driver, uhso_devclass, uhso_driver_loaded, 0);
492 MODULE_DEPEND(uhso, ucom, 1, 1, 1);
493 MODULE_DEPEND(uhso, usb, 1, 1, 1);
494 MODULE_VERSION(uhso, 1);
496 static struct ucom_callback uhso_ucom_callback = {
497 .ucom_cfg_get_status = &uhso_ucom_cfg_get_status,
498 .ucom_cfg_set_dtr = &uhso_ucom_cfg_set_dtr,
499 .ucom_cfg_set_rts = &uhso_ucom_cfg_set_rts,
500 .ucom_start_read = uhso_ucom_start_read,
501 .ucom_stop_read = uhso_ucom_stop_read,
502 .ucom_start_write = uhso_ucom_start_write,
503 .ucom_stop_write = uhso_ucom_stop_write
507 uhso_probe(device_t self)
509 struct usb_attach_arg *uaa = device_get_ivars(self);
512 if (uaa->usb_mode != USB_MODE_HOST)
514 if (uaa->info.bConfigIndex != 0)
516 if (uaa->device->ddesc.bDeviceClass != 0xff)
519 error = usbd_lookup_id_by_uaa(uhso_devs, sizeof(uhso_devs), uaa);
524 * Probe device to see if we are able to attach
525 * to this interface or not.
527 if (USB_GET_DRIVER_INFO(uaa) == UHSO_AUTO_IFACE) {
528 if (uhso_probe_iface_auto(uaa->device,
529 uaa->info.bIfaceNum) == 0)
536 uhso_attach(device_t self)
538 struct uhso_softc *sc = device_get_softc(self);
539 struct usb_attach_arg *uaa = device_get_ivars(self);
540 struct usb_config_descriptor *cd;
541 struct usb_interface_descriptor *id;
542 struct sysctl_ctx_list *sctx;
543 struct sysctl_oid *soid;
544 struct sysctl_oid *tree = NULL, *tty_node;
545 struct ucom_softc *ucom;
553 sc->sc_udev = uaa->device;
554 mtx_init(&sc->sc_mtx, "uhso", NULL, MTX_DEF);
560 cd = usbd_get_config_descriptor(uaa->device);
561 id = usbd_get_interface_descriptor(uaa->iface);
562 sc->sc_ctrl_iface_no = id->bInterfaceNumber;
564 sc->sc_iface_no = uaa->info.bIfaceNum;
565 sc->sc_iface_index = uaa->info.bIfaceIndex;
567 /* Setup control pipe */
568 uerr = usbd_transfer_setup(uaa->device,
569 &sc->sc_iface_index, sc->sc_ctrl_xfer,
570 uhso_ctrl_config, UHSO_CTRL_MAX, sc, &sc->sc_mtx);
572 device_printf(self, "Failed to setup control pipe: %s\n",
577 if (USB_GET_DRIVER_INFO(uaa) == UHSO_STATIC_IFACE)
578 probe_f = uhso_probe_iface_static;
579 else if (USB_GET_DRIVER_INFO(uaa) == UHSO_AUTO_IFACE)
580 probe_f = uhso_probe_iface_auto;
584 error = uhso_probe_iface(sc, uaa->info.bIfaceNum, probe_f);
588 sctx = device_get_sysctl_ctx(sc->sc_dev);
589 soid = device_get_sysctl_tree(sc->sc_dev);
591 SYSCTL_ADD_STRING(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "type",
592 CTLFLAG_RD, uhso_port[UHSO_IFACE_PORT(sc->sc_type)], 0,
593 "Port available at this interface");
594 SYSCTL_ADD_PROC(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "radio",
595 CTLTYPE_INT | CTLFLAG_RW, sc, 0, uhso_radio_sysctl, "I", "Enable radio");
598 * The default interface description on most Option devices isn't
599 * very helpful. So we skip device_set_usb_desc and set the
600 * device description manually.
602 device_set_desc_copy(self, uhso_port_type[UHSO_IFACE_PORT_TYPE(sc->sc_type)]);
603 /* Announce device */
604 device_printf(self, "<%s port> at <%s %s> on %s\n",
605 uhso_port_type[UHSO_IFACE_PORT_TYPE(sc->sc_type)],
606 uaa->device->manufacturer, uaa->device->product,
607 device_get_nameunit(uaa->device->bus->bdev));
609 if (sc->sc_ttys > 0) {
610 SYSCTL_ADD_INT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "ports",
611 CTLFLAG_RD, &sc->sc_ttys, 0, "Number of attached serial ports");
613 tree = SYSCTL_ADD_NODE(sctx, SYSCTL_CHILDREN(soid), OID_AUTO,
614 "port", CTLFLAG_RD, NULL, "Serial ports");
618 * Loop through the number of found TTYs and create sysctl
621 for (i = 0; i < sc->sc_ttys; i++) {
623 ucom = &sc->sc_ucom[i];
625 if (UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_MUX)
626 port = uhso_mux_port_map[ht->ht_muxport];
628 port = UHSO_IFACE_PORT_TYPE(sc->sc_type);
630 desc = uhso_port_type_sysctl[port];
632 tty_node = SYSCTL_ADD_NODE(sctx, SYSCTL_CHILDREN(tree), OID_AUTO,
633 desc, CTLFLAG_RD, NULL, "");
636 if (sc->sc_ttys == 1)
637 snprintf(ht->ht_name, 32, "cuaU%d", ucom->sc_unit);
639 snprintf(ht->ht_name, 32, "cuaU%d.%d",
640 ucom->sc_unit - ucom->sc_local_unit,
641 ucom->sc_local_unit);
644 desc = uhso_port_type[port];
645 SYSCTL_ADD_STRING(sctx, SYSCTL_CHILDREN(tty_node), OID_AUTO,
646 "tty", CTLFLAG_RD, ht->ht_name, 0, "");
647 SYSCTL_ADD_STRING(sctx, SYSCTL_CHILDREN(tty_node), OID_AUTO,
648 "desc", CTLFLAG_RD, desc, 0, "");
651 device_printf(sc->sc_dev,
652 "\"%s\" port at %s\n", desc, ht->ht_name);
657 uhso_detach(sc->sc_dev);
662 uhso_detach(device_t self)
664 struct uhso_softc *sc = device_get_softc(self);
667 usbd_transfer_unsetup(sc->sc_xfer, 3);
668 usbd_transfer_unsetup(sc->sc_ctrl_xfer, UHSO_CTRL_MAX);
669 if (sc->sc_ttys > 0) {
670 ucom_detach(&sc->sc_super_ucom, sc->sc_ucom, sc->sc_ttys);
672 for (i = 0; i < sc->sc_ttys; i++) {
673 if (sc->sc_tty[i].ht_muxport != -1) {
674 usbd_transfer_unsetup(sc->sc_tty[i].ht_xfer,
679 free(sc->sc_tty, M_USBDEV);
680 free(sc->sc_ucom, M_USBDEV);
683 if (sc->sc_ifp != NULL) {
684 callout_drain(&sc->sc_c);
685 free_unr(uhso_ifnet_unit, sc->sc_ifp->if_dunit);
686 mtx_lock(&sc->sc_mtx);
688 bpfdetach(sc->sc_ifp);
689 if_detach(sc->sc_ifp);
691 mtx_unlock(&sc->sc_mtx);
692 usbd_transfer_unsetup(sc->sc_if_xfer, UHSO_IFNET_MAX);
695 mtx_destroy(&sc->sc_mtx);
700 uhso_test_autoinst(void *arg, struct usb_device *udev,
701 struct usb_attach_arg *uaa)
703 struct usb_interface *iface;
704 struct usb_interface_descriptor *id;
706 if (uaa->dev_state != UAA_DEV_READY || !uhso_autoswitch)
709 iface = usbd_get_iface(udev, 0);
713 if (id == NULL || id->bInterfaceClass != UICLASS_MASS)
715 if (usbd_lookup_id_by_uaa(uhso_devs, sizeof(uhso_devs), uaa))
716 return; /* no device match */
718 if (usb_msc_eject(udev, 0, MSC_EJECT_REZERO) == 0) {
719 /* success, mark the udev as disappearing */
720 uaa->dev_state = UAA_DEV_EJECTING;
725 uhso_driver_loaded(struct module *mod, int what, void *arg)
729 /* register our autoinstall handler */
730 uhso_etag = EVENTHANDLER_REGISTER(usb_dev_configured,
731 uhso_test_autoinst, NULL, EVENTHANDLER_PRI_ANY);
732 /* create our unit allocator for inet devs */
733 uhso_ifnet_unit = new_unrhdr(0, INT_MAX, NULL);
736 EVENTHANDLER_DEREGISTER(usb_dev_configured, uhso_etag);
737 delete_unrhdr(uhso_ifnet_unit);
746 * Probe the interface type by querying the device. The elements
747 * of an array indicates the capabilities of a particular interface.
748 * Returns a bit mask with the interface capabilities.
751 uhso_probe_iface_auto(struct usb_device *udev, int index)
753 struct usb_device_request req;
759 req.bmRequestType = UT_READ_VENDOR_DEVICE;
761 USETW(req.wValue, 0);
762 USETW(req.wIndex, 0);
763 USETW(req.wLength, 17);
765 uerr = usbd_do_request_flags(udev, NULL, &req, buf,
766 0, &actlen, USB_MS_HZ);
768 printf("%s: usbd_do_request_flags failed, %s\n",
769 __func__, usbd_errstr(uerr));
773 UHSO_DPRINTF(1, "actlen=%d\n", actlen);
774 UHSO_HEXDUMP(buf, 17);
776 if (index < 0 || index > 16) {
777 UHSO_DPRINTF(0, "Index %d out of range\n", index);
781 UHSO_DPRINTF(1, "index=%d, type=%x[%s]\n", index, buf[index],
782 uhso_port_type[(int)uhso_port_map[(int)buf[index]]]);
784 if (buf[index] >= uhso_port_map_max)
787 port = uhso_port_map[(int)buf[index]];
790 case UHSO_PORT_TYPE_NETWORK:
791 return (UHSO_IFACE_SPEC(UHSO_IF_NET | UHSO_IF_MUX,
792 UHSO_PORT_SERIAL | UHSO_PORT_NETWORK, port));
793 case UHSO_PORT_TYPE_DIAG:
794 case UHSO_PORT_TYPE_DIAG2:
795 case UHSO_PORT_TYPE_CTL:
796 case UHSO_PORT_TYPE_APP:
797 case UHSO_PORT_TYPE_APP2:
798 case UHSO_PORT_TYPE_MODEM:
799 return (UHSO_IFACE_SPEC(UHSO_IF_BULK,
800 UHSO_PORT_SERIAL, port));
801 case UHSO_PORT_TYPE_MSD:
803 case UHSO_PORT_TYPE_UNKNOWN:
812 * Returns the capabilities of interfaces for devices that don't
813 * support the automatic query.
814 * Returns a bit mask with the interface capabilities.
817 uhso_probe_iface_static(struct usb_device *udev, int index)
819 struct usb_config_descriptor *cd;
821 cd = usbd_get_config_descriptor(udev);
822 if (cd->bNumInterface <= 3) {
823 /* Cards with 3 or less interfaces */
826 return UHSO_IFACE_SPEC(UHSO_IF_NET | UHSO_IF_MUX,
827 UHSO_PORT_SERIAL | UHSO_PORT_NETWORK,
828 UHSO_PORT_TYPE_NETWORK);
830 return UHSO_IFACE_SPEC(UHSO_IF_BULK,
831 UHSO_PORT_SERIAL, UHSO_PORT_TYPE_DIAG);
833 return UHSO_IFACE_SPEC(UHSO_IF_BULK,
834 UHSO_PORT_SERIAL, UHSO_PORT_TYPE_MODEM);
837 /* Cards with 4 interfaces */
840 return UHSO_IFACE_SPEC(UHSO_IF_NET | UHSO_IF_MUX,
841 UHSO_PORT_SERIAL | UHSO_PORT_NETWORK,
842 UHSO_PORT_TYPE_NETWORK);
844 return UHSO_IFACE_SPEC(UHSO_IF_BULK,
845 UHSO_PORT_SERIAL, UHSO_PORT_TYPE_DIAG2);
847 return UHSO_IFACE_SPEC(UHSO_IF_BULK,
848 UHSO_PORT_SERIAL, UHSO_PORT_TYPE_MODEM);
850 return UHSO_IFACE_SPEC(UHSO_IF_BULK,
851 UHSO_PORT_SERIAL, UHSO_PORT_TYPE_DIAG);
858 * Probes an interface for its particular capabilities and attaches if
859 * it's a supported interface.
862 uhso_probe_iface(struct uhso_softc *sc, int index,
863 int (*probe)(struct usb_device *, int))
865 struct usb_interface *iface;
868 UHSO_DPRINTF(1, "Probing for interface %d, probe_func=%p\n", index, probe);
870 type = probe(sc->sc_udev, index);
871 UHSO_DPRINTF(1, "Probe result %x\n", type);
876 iface = usbd_get_iface(sc->sc_udev, index);
878 if (UHSO_IFACE_PORT_TYPE(type) == UHSO_PORT_TYPE_NETWORK) {
879 error = uhso_attach_ifnet(sc, iface, type);
881 UHSO_DPRINTF(1, "uhso_attach_ifnet failed");
886 * If there is an additional interrupt endpoint on this
887 * interface then we most likely have a multiplexed serial port
890 if (iface->idesc->bNumEndpoints < 3) {
891 sc->sc_type = UHSO_IFACE_SPEC(
892 UHSO_IFACE_USB_TYPE(type) & ~UHSO_IF_MUX,
893 UHSO_IFACE_PORT(type) & ~UHSO_PORT_SERIAL,
894 UHSO_IFACE_PORT_TYPE(type));
898 UHSO_DPRINTF(1, "Trying to attach mux. serial\n");
899 error = uhso_attach_muxserial(sc, iface, type);
900 if (error == 0 && sc->sc_ttys > 0) {
901 error = ucom_attach(&sc->sc_super_ucom, sc->sc_ucom,
902 sc->sc_ttys, sc, &uhso_ucom_callback, &sc->sc_mtx);
904 device_printf(sc->sc_dev, "ucom_attach failed\n");
908 mtx_lock(&sc->sc_mtx);
909 usbd_transfer_start(sc->sc_xfer[UHSO_MUX_ENDPT_INTR]);
910 mtx_unlock(&sc->sc_mtx);
912 } else if ((UHSO_IFACE_USB_TYPE(type) & UHSO_IF_BULK) &&
913 UHSO_IFACE_PORT(type) & UHSO_PORT_SERIAL) {
915 error = uhso_attach_bulkserial(sc, iface, type);
919 error = ucom_attach(&sc->sc_super_ucom, sc->sc_ucom,
920 sc->sc_ttys, sc, &uhso_ucom_callback, &sc->sc_mtx);
922 device_printf(sc->sc_dev, "ucom_attach failed\n");
927 UHSO_DPRINTF(0, "Unknown type %x\n", type);
935 uhso_radio_ctrl(struct uhso_softc *sc, int onoff)
937 struct usb_device_request req;
940 req.bmRequestType = UT_VENDOR;
941 req.bRequest = onoff ? 0x82 : 0x81;
942 USETW(req.wValue, 0);
943 USETW(req.wIndex, 0);
944 USETW(req.wLength, 0);
946 uerr = usbd_do_request(sc->sc_udev, NULL, &req, NULL);
948 device_printf(sc->sc_dev, "usbd_do_request_flags failed: %s\n",
956 uhso_radio_sysctl(SYSCTL_HANDLER_ARGS)
958 struct uhso_softc *sc = arg1;
961 radio = sc->sc_radio;
962 error = sysctl_handle_int(oidp, &radio, 0, req);
965 if (radio != sc->sc_radio) {
966 radio = radio != 0 ? 1 : 0;
967 error = uhso_radio_ctrl(sc, radio);
969 sc->sc_radio = radio;
976 * Expands allocated memory to fit an additional TTY.
977 * Two arrays are kept with matching indexes, one for ucom and one
978 * for our private data.
981 uhso_alloc_tty(struct uhso_softc *sc)
985 sc->sc_tty = reallocf(sc->sc_tty, sizeof(struct uhso_tty) * sc->sc_ttys,
986 M_USBDEV, M_WAITOK | M_ZERO);
987 if (sc->sc_tty == NULL)
990 sc->sc_ucom = reallocf(sc->sc_ucom,
991 sizeof(struct ucom_softc) * sc->sc_ttys, M_USBDEV, M_WAITOK | M_ZERO);
992 if (sc->sc_ucom == NULL)
995 sc->sc_tty[sc->sc_ttys - 1].ht_sc = sc;
997 UHSO_DPRINTF(1, "Allocated TTY %d\n", sc->sc_ttys - 1);
998 return (sc->sc_ttys - 1);
1002 * Attach a multiplexed serial port
1003 * Data is read/written with requests on the default control pipe. An interrupt
1004 * endpoint returns when there is new data to be read.
1007 uhso_attach_muxserial(struct uhso_softc *sc, struct usb_interface *iface,
1010 struct usb_descriptor *desc;
1015 * The class specific interface (type 0x24) descriptor subtype field
1016 * contains a bitmask that specifies which (and how many) ports that
1017 * are available through this multiplexed serial port.
1019 desc = usbd_find_descriptor(sc->sc_udev, NULL,
1020 iface->idesc->bInterfaceNumber, UDESC_CS_INTERFACE, 0xff, 0, 0);
1022 UHSO_DPRINTF(0, "Failed to find UDESC_CS_INTERFACE\n");
1026 UHSO_DPRINTF(1, "Mux port mask %x\n", desc->bDescriptorSubtype);
1027 if (desc->bDescriptorSubtype == 0)
1031 * The bitmask is one octet, loop through the number of
1032 * bits that are set and create a TTY for each.
1034 for (i = 0; i < 8; i++) {
1036 if ((port & desc->bDescriptorSubtype) == port) {
1037 UHSO_DPRINTF(2, "Found mux port %x (%d)\n", port, i);
1038 tty = uhso_alloc_tty(sc);
1041 sc->sc_tty[tty].ht_muxport = i;
1042 uerr = usbd_transfer_setup(sc->sc_udev,
1043 &sc->sc_iface_index, sc->sc_tty[tty].ht_xfer,
1044 uhso_ctrl_config, UHSO_CTRL_MAX, sc, &sc->sc_mtx);
1046 device_printf(sc->sc_dev,
1047 "Failed to setup control pipe: %s\n",
1054 /* Setup the intr. endpoint */
1055 uerr = usbd_transfer_setup(sc->sc_udev,
1056 &iface->idesc->bInterfaceNumber, sc->sc_xfer,
1057 uhso_mux_config, 1, sc, &sc->sc_mtx);
1065 * Interrupt callback for the multiplexed serial port. Indicates
1066 * which serial port has data waiting.
1069 uhso_mux_intr_callback(struct usb_xfer *xfer, usb_error_t error)
1071 struct usb_page_cache *pc;
1072 struct usb_page_search res;
1073 struct uhso_softc *sc = usbd_xfer_softc(xfer);
1074 unsigned int i, mux;
1076 UHSO_DPRINTF(3, "status %d\n", USB_GET_STATE(xfer));
1078 switch (USB_GET_STATE(xfer)) {
1079 case USB_ST_TRANSFERRED:
1081 * The multiplexed port number can be found at the first byte.
1082 * It contains a bit mask, we transform this in to an integer.
1084 pc = usbd_xfer_get_frame(xfer, 0);
1085 usbd_get_page(pc, 0, &res);
1087 i = *((unsigned char *)res.buffer);
1093 UHSO_DPRINTF(3, "mux port %d (%d)\n", mux, i);
1094 if (mux > UHSO_MPORT_TYPE_NOMAX)
1097 /* Issue a read for this serial port */
1099 sc->sc_tty[mux].ht_xfer[UHSO_CTRL_READ],
1101 usbd_transfer_start(sc->sc_tty[mux].ht_xfer[UHSO_CTRL_READ]);
1106 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
1107 usbd_transfer_submit(xfer);
1110 UHSO_DPRINTF(0, "error: %s\n", usbd_errstr(error));
1111 if (error == USB_ERR_CANCELLED)
1114 usbd_xfer_set_stall(xfer);
1120 uhso_mux_read_callback(struct usb_xfer *xfer, usb_error_t error)
1122 struct uhso_softc *sc = usbd_xfer_softc(xfer);
1123 struct usb_page_cache *pc;
1124 struct usb_device_request req;
1125 struct uhso_tty *ht;
1128 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
1130 UHSO_DPRINTF(3, "status %d\n", USB_GET_STATE(xfer));
1132 ht = usbd_xfer_get_priv(xfer);
1133 UHSO_DPRINTF(3, "ht=%p open=%d\n", ht, ht->ht_open);
1135 switch (USB_GET_STATE(xfer)) {
1136 case USB_ST_TRANSFERRED:
1137 /* Got data, send to ucom */
1138 pc = usbd_xfer_get_frame(xfer, 1);
1139 len = usbd_xfer_frame_len(xfer, 1);
1141 UHSO_DPRINTF(3, "got %d bytes on mux port %d\n", len,
1144 usbd_transfer_start(sc->sc_xfer[UHSO_MUX_ENDPT_INTR]);
1148 /* Deliver data if the TTY is open, discard otherwise */
1150 ucom_put_data(&sc->sc_ucom[ht->ht_muxport], pc, 0, len);
1154 bzero(&req, sizeof(struct usb_device_request));
1155 req.bmRequestType = UT_READ_CLASS_INTERFACE;
1156 req.bRequest = UCDC_GET_ENCAPSULATED_RESPONSE;
1157 USETW(req.wValue, 0);
1158 USETW(req.wIndex, ht->ht_muxport);
1159 USETW(req.wLength, 1024);
1161 pc = usbd_xfer_get_frame(xfer, 0);
1162 usbd_copy_in(pc, 0, &req, sizeof(req));
1164 usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
1165 usbd_xfer_set_frame_len(xfer, 1, 1024);
1166 usbd_xfer_set_frames(xfer, 2);
1167 usbd_transfer_submit(xfer);
1170 UHSO_DPRINTF(0, "error: %s\n", usbd_errstr(error));
1171 if (error == USB_ERR_CANCELLED)
1173 usbd_xfer_set_stall(xfer);
1179 uhso_mux_write_callback(struct usb_xfer *xfer, usb_error_t error)
1181 struct uhso_softc *sc = usbd_xfer_softc(xfer);
1182 struct uhso_tty *ht;
1183 struct usb_page_cache *pc;
1184 struct usb_device_request req;
1186 struct usb_page_search res;
1188 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
1190 ht = usbd_xfer_get_priv(xfer);
1191 UHSO_DPRINTF(3, "status=%d, using mux port %d\n",
1192 USB_GET_STATE(xfer), ht->ht_muxport);
1194 switch (USB_GET_STATE(xfer)) {
1195 case USB_ST_TRANSFERRED:
1196 UHSO_DPRINTF(3, "wrote %zd data bytes to muxport %d\n",
1197 actlen - sizeof(struct usb_device_request) ,
1201 pc = usbd_xfer_get_frame(xfer, 1);
1202 if (ucom_get_data(&sc->sc_ucom[ht->ht_muxport], pc,
1205 usbd_get_page(pc, 0, &res);
1207 bzero(&req, sizeof(struct usb_device_request));
1208 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
1209 req.bRequest = UCDC_SEND_ENCAPSULATED_COMMAND;
1210 USETW(req.wValue, 0);
1211 USETW(req.wIndex, ht->ht_muxport);
1212 USETW(req.wLength, actlen);
1214 pc = usbd_xfer_get_frame(xfer, 0);
1215 usbd_copy_in(pc, 0, &req, sizeof(req));
1217 usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
1218 usbd_xfer_set_frame_len(xfer, 1, actlen);
1219 usbd_xfer_set_frames(xfer, 2);
1221 UHSO_DPRINTF(3, "Prepared %d bytes for transmit "
1222 "on muxport %d\n", actlen, ht->ht_muxport);
1224 usbd_transfer_submit(xfer);
1228 UHSO_DPRINTF(0, "error: %s\n", usbd_errstr(error));
1229 if (error == USB_ERR_CANCELLED)
1236 uhso_attach_bulkserial(struct uhso_softc *sc, struct usb_interface *iface,
1242 /* Try attaching RD/WR/INTR first */
1243 uerr = usbd_transfer_setup(sc->sc_udev,
1244 &iface->idesc->bInterfaceNumber, sc->sc_xfer,
1245 uhso_bs_config, UHSO_BULK_ENDPT_MAX, sc, &sc->sc_mtx);
1247 /* Try only RD/WR */
1248 uerr = usbd_transfer_setup(sc->sc_udev,
1249 &iface->idesc->bInterfaceNumber, sc->sc_xfer,
1250 uhso_bs_config, UHSO_BULK_ENDPT_MAX - 1, sc, &sc->sc_mtx);
1253 UHSO_DPRINTF(0, "usbd_transfer_setup failed");
1257 tty = uhso_alloc_tty(sc);
1259 usbd_transfer_unsetup(sc->sc_xfer, UHSO_BULK_ENDPT_MAX);
1263 sc->sc_tty[tty].ht_muxport = -1;
1268 uhso_bs_read_callback(struct usb_xfer *xfer, usb_error_t error)
1270 struct uhso_softc *sc = usbd_xfer_softc(xfer);
1271 struct usb_page_cache *pc;
1274 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
1276 UHSO_DPRINTF(3, "status %d, actlen=%d\n", USB_GET_STATE(xfer), actlen);
1278 switch (USB_GET_STATE(xfer)) {
1279 case USB_ST_TRANSFERRED:
1280 pc = usbd_xfer_get_frame(xfer, 0);
1281 ucom_put_data(&sc->sc_ucom[0], pc, 0, actlen);
1285 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
1286 usbd_transfer_submit(xfer);
1289 UHSO_DPRINTF(0, "error: %s\n", usbd_errstr(error));
1290 if (error == USB_ERR_CANCELLED)
1292 usbd_xfer_set_stall(xfer);
1298 uhso_bs_write_callback(struct usb_xfer *xfer, usb_error_t error)
1300 struct uhso_softc *sc = usbd_xfer_softc(xfer);
1301 struct usb_page_cache *pc;
1304 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
1306 UHSO_DPRINTF(3, "status %d, actlen=%d\n", USB_GET_STATE(xfer), actlen);
1308 switch (USB_GET_STATE(xfer)) {
1309 case USB_ST_TRANSFERRED:
1312 pc = usbd_xfer_get_frame(xfer, 0);
1313 if (ucom_get_data(&sc->sc_ucom[0], pc, 0, 8192, &actlen)) {
1314 usbd_xfer_set_frame_len(xfer, 0, actlen);
1315 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_cfg(struct uhso_softc *sc)
1331 struct usb_device_request req;
1334 if (!(UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_BULK))
1337 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
1338 req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
1339 USETW(req.wValue, sc->sc_line);
1340 USETW(req.wIndex, sc->sc_iface_no);
1341 USETW(req.wLength, 0);
1343 uerr = ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom[0], &req, NULL, 0, 1000);
1345 device_printf(sc->sc_dev, "failed to set ctrl line state to "
1346 "0x%02x: %s\n", sc->sc_line, usbd_errstr(uerr));
1351 uhso_bs_intr_callback(struct usb_xfer *xfer, usb_error_t error)
1353 struct uhso_softc *sc = usbd_xfer_softc(xfer);
1354 struct usb_page_cache *pc;
1356 struct usb_cdc_notification cdc;
1358 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
1359 UHSO_DPRINTF(3, "status %d, actlen=%d\n", USB_GET_STATE(xfer), actlen);
1361 switch (USB_GET_STATE(xfer)) {
1362 case USB_ST_TRANSFERRED:
1363 if (actlen < UCDC_NOTIFICATION_LENGTH) {
1364 UHSO_DPRINTF(0, "UCDC notification too short: %d\n", actlen);
1367 else if (actlen > sizeof(struct usb_cdc_notification)) {
1368 UHSO_DPRINTF(0, "UCDC notification too large: %d\n", actlen);
1369 actlen = sizeof(struct usb_cdc_notification);
1372 pc = usbd_xfer_get_frame(xfer, 0);
1373 usbd_copy_out(pc, 0, &cdc, actlen);
1375 if (UGETW(cdc.wIndex) != sc->sc_iface_no) {
1376 UHSO_DPRINTF(0, "Interface mismatch, got %d expected %d\n",
1377 UGETW(cdc.wIndex), sc->sc_iface_no);
1381 if (cdc.bmRequestType == UCDC_NOTIFICATION &&
1382 cdc.bNotification == UCDC_N_SERIAL_STATE) {
1383 UHSO_DPRINTF(2, "notify = 0x%02x\n", cdc.data[0]);
1387 if (cdc.data[0] & UCDC_N_SERIAL_RI)
1388 sc->sc_msr |= SER_RI;
1389 if (cdc.data[0] & UCDC_N_SERIAL_DSR)
1390 sc->sc_msr |= SER_DSR;
1391 if (cdc.data[0] & UCDC_N_SERIAL_DCD)
1392 sc->sc_msr |= SER_DCD;
1394 ucom_status_change(&sc->sc_ucom[0]);
1399 if (error == USB_ERR_CANCELLED)
1401 usbd_xfer_set_stall(xfer);
1407 uhso_ucom_cfg_get_status(struct ucom_softc *ucom, uint8_t *lsr, uint8_t *msr)
1409 struct uhso_softc *sc = ucom->sc_parent;
1416 uhso_ucom_cfg_set_dtr(struct ucom_softc *ucom, uint8_t onoff)
1418 struct uhso_softc *sc = ucom->sc_parent;
1420 if (!(UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_BULK))
1424 sc->sc_line |= UCDC_LINE_DTR;
1426 sc->sc_line &= ~UCDC_LINE_DTR;
1432 uhso_ucom_cfg_set_rts(struct ucom_softc *ucom, uint8_t onoff)
1434 struct uhso_softc *sc = ucom->sc_parent;
1436 if (!(UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_BULK))
1440 sc->sc_line |= UCDC_LINE_RTS;
1442 sc->sc_line &= ~UCDC_LINE_RTS;
1448 uhso_ucom_start_read(struct ucom_softc *ucom)
1450 struct uhso_softc *sc = ucom->sc_parent;
1452 UHSO_DPRINTF(3, "unit=%d, local_unit=%d\n",
1453 ucom->sc_unit, ucom->sc_local_unit);
1455 if (UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_MUX) {
1456 sc->sc_tty[ucom->sc_local_unit].ht_open = 1;
1457 usbd_transfer_start(sc->sc_xfer[UHSO_MUX_ENDPT_INTR]);
1459 else if (UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_BULK) {
1460 sc->sc_tty[0].ht_open = 1;
1461 usbd_transfer_start(sc->sc_xfer[UHSO_BULK_ENDPT_READ]);
1462 if (sc->sc_xfer[UHSO_BULK_ENDPT_INTR] != NULL)
1463 usbd_transfer_start(sc->sc_xfer[UHSO_BULK_ENDPT_INTR]);
1468 uhso_ucom_stop_read(struct ucom_softc *ucom)
1471 struct uhso_softc *sc = ucom->sc_parent;
1473 if (UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_MUX) {
1474 sc->sc_tty[ucom->sc_local_unit].ht_open = 0;
1476 sc->sc_tty[ucom->sc_local_unit].ht_xfer[UHSO_CTRL_READ]);
1478 else if (UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_BULK) {
1479 sc->sc_tty[0].ht_open = 0;
1480 usbd_transfer_start(sc->sc_xfer[UHSO_BULK_ENDPT_READ]);
1481 if (sc->sc_xfer[UHSO_BULK_ENDPT_INTR] != NULL)
1482 usbd_transfer_stop(sc->sc_xfer[UHSO_BULK_ENDPT_INTR]);
1487 uhso_ucom_start_write(struct ucom_softc *ucom)
1489 struct uhso_softc *sc = ucom->sc_parent;
1491 if (UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_MUX) {
1492 UHSO_DPRINTF(3, "local unit %d\n", ucom->sc_local_unit);
1494 usbd_transfer_start(sc->sc_xfer[UHSO_MUX_ENDPT_INTR]);
1497 sc->sc_tty[ucom->sc_local_unit].ht_xfer[UHSO_CTRL_WRITE],
1498 &sc->sc_tty[ucom->sc_local_unit]);
1499 usbd_transfer_start(
1500 sc->sc_tty[ucom->sc_local_unit].ht_xfer[UHSO_CTRL_WRITE]);
1503 else if (UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_BULK) {
1504 usbd_transfer_start(sc->sc_xfer[UHSO_BULK_ENDPT_WRITE]);
1509 uhso_ucom_stop_write(struct ucom_softc *ucom)
1511 struct uhso_softc *sc = ucom->sc_parent;
1513 if (UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_MUX) {
1515 sc->sc_tty[ucom->sc_local_unit].ht_xfer[UHSO_CTRL_WRITE]);
1517 else if (UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_BULK) {
1518 usbd_transfer_stop(sc->sc_xfer[UHSO_BULK_ENDPT_WRITE]);
1523 uhso_attach_ifnet(struct uhso_softc *sc, struct usb_interface *iface, int type)
1527 struct sysctl_ctx_list *sctx;
1528 struct sysctl_oid *soid;
1529 unsigned int devunit;
1531 uerr = usbd_transfer_setup(sc->sc_udev,
1532 &iface->idesc->bInterfaceNumber, sc->sc_if_xfer,
1533 uhso_ifnet_config, UHSO_IFNET_MAX, sc, &sc->sc_mtx);
1535 UHSO_DPRINTF(0, "usbd_transfer_setup failed: %s\n",
1540 sc->sc_ifp = ifp = if_alloc(IFT_OTHER);
1541 if (sc->sc_ifp == NULL) {
1542 device_printf(sc->sc_dev, "if_alloc() failed\n");
1546 callout_init_mtx(&sc->sc_c, &sc->sc_mtx, 0);
1547 mtx_lock(&sc->sc_mtx);
1548 callout_reset(&sc->sc_c, 1, uhso_if_rxflush, sc);
1549 mtx_unlock(&sc->sc_mtx);
1552 * We create our own unit numbers for ifnet devices because the
1553 * USB interface unit numbers can be at arbitrary positions yielding
1554 * odd looking device names.
1556 devunit = alloc_unr(uhso_ifnet_unit);
1558 if_initname(ifp, device_get_name(sc->sc_dev), devunit);
1559 ifp->if_mtu = UHSO_MAX_MTU;
1560 ifp->if_ioctl = uhso_if_ioctl;
1561 ifp->if_init = uhso_if_init;
1562 ifp->if_start = uhso_if_start;
1563 ifp->if_output = uhso_if_output;
1566 IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
1567 ifp->if_snd.ifq_drv_maxlen = ifqmaxlen;
1568 IFQ_SET_READY(&ifp->if_snd);
1571 bpfattach(ifp, DLT_RAW, 0);
1573 sctx = device_get_sysctl_ctx(sc->sc_dev);
1574 soid = device_get_sysctl_tree(sc->sc_dev);
1575 /* Unlocked read... */
1576 SYSCTL_ADD_STRING(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "netif",
1577 CTLFLAG_RD, ifp->if_xname, 0, "Attached network interface");
1583 uhso_ifnet_read_callback(struct usb_xfer *xfer, usb_error_t error)
1585 struct uhso_softc *sc = usbd_xfer_softc(xfer);
1587 struct usb_page_cache *pc;
1590 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
1592 UHSO_DPRINTF(3, "status=%d, actlen=%d\n", USB_GET_STATE(xfer), actlen);
1594 switch (USB_GET_STATE(xfer)) {
1595 case USB_ST_TRANSFERRED:
1596 if (actlen > 0 && (sc->sc_ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1597 pc = usbd_xfer_get_frame(xfer, 0);
1598 m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
1599 usbd_copy_out(pc, 0, mtod(m, uint8_t *), actlen);
1600 m->m_pkthdr.len = m->m_len = actlen;
1601 /* Enqueue frame for further processing */
1602 _IF_ENQUEUE(&sc->sc_rxq, m);
1603 if (!callout_pending(&sc->sc_c) ||
1604 !callout_active(&sc->sc_c)) {
1605 callout_schedule(&sc->sc_c, 1);
1611 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
1612 usbd_transfer_submit(xfer);
1615 UHSO_DPRINTF(0, "error: %s\n", usbd_errstr(error));
1616 if (error == USB_ERR_CANCELLED)
1618 usbd_xfer_set_stall(xfer);
1624 * Deferred RX processing, called with mutex locked.
1626 * Each frame we receive might contain several small ip-packets as well
1627 * as partial ip-packets. We need to separate/assemble them into individual
1628 * packets before sending them to the ip-layer.
1631 uhso_if_rxflush(void *arg)
1633 struct uhso_softc *sc = arg;
1634 struct ifnet *ifp = sc->sc_ifp;
1636 struct mbuf *m, *m0, *mwait;
1639 struct ip6_hdr *ip6;
1645 mwait = sc->sc_mwait;
1648 _IF_DEQUEUE(&sc->sc_rxq, m);
1651 UHSO_DPRINTF(3, "dequeue m=%p, len=%d\n", m, m->m_len);
1653 mtx_unlock(&sc->sc_mtx);
1655 /* Do we have a partial packet waiting? */
1656 if (mwait != NULL) {
1660 UHSO_DPRINTF(3, "partial m0=%p(%d), concat w/ m=%p(%d)\n",
1661 m0, m0->m_len, m, m->m_len);
1662 len = m->m_len + m0->m_len;
1664 /* Concat mbufs and fix headers */
1666 m0->m_pkthdr.len = len;
1667 m->m_flags &= ~M_PKTHDR;
1669 m = m_pullup(m0, sizeof(struct ip));
1672 UHSO_DPRINTF(0, "m_pullup failed\n");
1673 mtx_lock(&sc->sc_mtx);
1676 UHSO_DPRINTF(3, "Constructed mbuf=%p, len=%d\n",
1677 m, m->m_pkthdr.len);
1680 cp = mtod(m, uint8_t *);
1681 ip = (struct ip *)cp;
1683 ip6 = (struct ip6_hdr *)cp;
1686 /* Check for IPv4 */
1687 if (ip->ip_v == IPVERSION) {
1688 iplen = htons(ip->ip_len);
1692 /* Check for IPv6 */
1693 else if ((ip6->ip6_vfc & IPV6_VERSION_MASK) == IPV6_VERSION) {
1694 iplen = htons(ip6->ip6_plen);
1699 UHSO_DPRINTF(0, "got unexpected ip version %d, "
1700 "m=%p, len=%d\n", (*cp & 0xf0) >> 4, m, m->m_len);
1702 UHSO_HEXDUMP(cp, 4);
1705 mtx_lock(&sc->sc_mtx);
1710 UHSO_DPRINTF(0, "Zero IP length\n");
1714 mtx_lock(&sc->sc_mtx);
1718 UHSO_DPRINTF(3, "m=%p, len=%d, cp=%p, iplen=%d\n",
1719 m, m->m_pkthdr.len, cp, iplen);
1723 /* More IP packets in this mbuf */
1724 if (iplen < m->m_pkthdr.len) {
1728 * Allocate a new mbuf for this IP packet and
1729 * copy the IP-packet into it.
1731 m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
1732 bcopy(mtod(m0, uint8_t *), mtod(m, uint8_t *), iplen);
1733 m->m_pkthdr.len = m->m_len = iplen;
1735 /* Adjust the size of the original mbuf */
1737 m0 = m_defrag(m0, M_WAIT);
1739 UHSO_DPRINTF(3, "New mbuf=%p, len=%d/%d, m0=%p, "
1740 "m0_len=%d/%d\n", m, m->m_pkthdr.len, m->m_len,
1741 m0, m0->m_pkthdr.len, m0->m_len);
1743 else if (iplen > m->m_pkthdr.len) {
1744 UHSO_DPRINTF(3, "Deferred mbuf=%p, len=%d\n",
1745 m, m->m_pkthdr.len);
1748 mtx_lock(&sc->sc_mtx);
1753 m->m_pkthdr.rcvif = ifp;
1755 /* Dispatch to IP layer */
1756 BPF_MTAP(sc->sc_ifp, m);
1757 netisr_dispatch(isr, m);
1758 m = m0 != NULL ? m0 : NULL;
1759 mtx_lock(&sc->sc_mtx);
1761 sc->sc_mwait = mwait;
1765 uhso_ifnet_write_callback(struct usb_xfer *xfer, usb_error_t error)
1767 struct uhso_softc *sc = usbd_xfer_softc(xfer);
1768 struct ifnet *ifp = sc->sc_ifp;
1769 struct usb_page_cache *pc;
1773 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
1775 UHSO_DPRINTF(3, "status %d, actlen=%d\n", USB_GET_STATE(xfer), actlen);
1777 switch (USB_GET_STATE(xfer)) {
1778 case USB_ST_TRANSFERRED:
1780 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1783 IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
1787 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1789 if (m->m_pkthdr.len > MCLBYTES)
1790 m->m_pkthdr.len = MCLBYTES;
1792 usbd_xfer_set_frame_len(xfer, 0, m->m_pkthdr.len);
1793 pc = usbd_xfer_get_frame(xfer, 0);
1794 usbd_m_copy_in(pc, 0, m, 0, m->m_pkthdr.len);
1795 usbd_transfer_submit(xfer);
1801 UHSO_DPRINTF(0, "error: %s\n", usbd_errstr(error));
1802 if (error == USB_ERR_CANCELLED)
1804 usbd_xfer_set_stall(xfer);
1810 uhso_if_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1812 struct uhso_softc *sc;
1818 if (ifp->if_flags & IFF_UP) {
1819 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1824 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1825 mtx_lock(&sc->sc_mtx);
1827 mtx_unlock(&sc->sc_mtx);
1832 case SIOCSIFDSTADDR:
1843 uhso_if_init(void *priv)
1845 struct uhso_softc *sc = priv;
1846 struct ifnet *ifp = sc->sc_ifp;
1848 mtx_lock(&sc->sc_mtx);
1851 ifp->if_flags |= IFF_UP;
1852 ifp->if_drv_flags |= IFF_DRV_RUNNING;
1853 mtx_unlock(&sc->sc_mtx);
1855 UHSO_DPRINTF(2, "ifnet initialized\n");
1859 uhso_if_output(struct ifnet *ifp, struct mbuf *m0, struct sockaddr *dst,
1864 /* Only IPv4/6 support */
1865 if (dst->sa_family != AF_INET
1867 && dst->sa_family != AF_INET6
1870 return (EAFNOSUPPORT);
1873 error = (ifp->if_transmit)(ifp, m0);
1883 uhso_if_start(struct ifnet *ifp)
1885 struct uhso_softc *sc = ifp->if_softc;
1887 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
1888 UHSO_DPRINTF(1, "Not running\n");
1892 mtx_lock(&sc->sc_mtx);
1893 usbd_transfer_start(sc->sc_if_xfer[UHSO_IFNET_READ]);
1894 usbd_transfer_start(sc->sc_if_xfer[UHSO_IFNET_WRITE]);
1895 mtx_unlock(&sc->sc_mtx);
1896 UHSO_DPRINTF(3, "interface started\n");
1900 uhso_if_stop(struct uhso_softc *sc)
1903 usbd_transfer_stop(sc->sc_if_xfer[UHSO_IFNET_READ]);
1904 usbd_transfer_stop(sc->sc_if_xfer[UHSO_IFNET_WRITE]);
1905 sc->sc_ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);