]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - sys/dev/usb/net/uhso.c
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.git] / sys / dev / usb / net / uhso.c
1 /*-
2  * Copyright (c) 2010 Fredrik Lindberg <fli@shapeshifter.se>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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.
24  *
25  */
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
28
29 #include <sys/param.h>
30 #include <sys/types.h>
31 #include <sys/sockio.h>
32 #include <sys/mbuf.h>
33 #include <sys/malloc.h>
34 #include <sys/kernel.h>
35 #include <sys/module.h>
36 #include <sys/socket.h>
37 #include <sys/tty.h>
38 #include <sys/sysctl.h>
39 #include <sys/condvar.h>
40 #include <sys/sx.h>
41 #include <sys/proc.h>
42 #include <sys/conf.h>
43 #include <sys/bus.h>
44 #include <sys/systm.h>
45 #include <sys/limits.h>
46
47 #include <machine/bus.h>
48
49 #include <net/if.h>
50 #include <net/if_types.h>
51 #include <net/netisr.h>
52 #include <net/bpf.h>
53 #include <netinet/in.h>
54 #include <netinet/ip.h>
55 #include <netinet/ip6.h>
56
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>
61 #include "usbdevs.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>
67
68 #include <dev/usb/serial/usb_serial.h>
69
70 struct uhso_tty {
71         struct uhso_softc *ht_sc;
72         struct usb_xfer *ht_xfer[3];
73         int             ht_muxport; /* Mux. port no */
74         int             ht_open;
75         char            ht_name[32];
76 };
77
78 struct uhso_softc {
79         device_t                sc_dev;
80         struct usb_device       *sc_udev;
81         struct mtx              sc_mtx;
82         uint32_t                sc_type;        /* Interface definition */
83         int                     sc_radio;
84
85         struct usb_xfer         *sc_xfer[3];
86         uint8_t                 sc_iface_no;
87         uint8_t                 sc_iface_index;
88
89         /* Control pipe */
90         struct usb_xfer *       sc_ctrl_xfer[2];
91         uint8_t                 sc_ctrl_iface_no;
92
93         /* Network */
94         struct usb_xfer         *sc_if_xfer[2];
95         struct ifnet            *sc_ifp;
96         struct mbuf             *sc_mwait;      /* Partial packet */
97         size_t                  sc_waitlen;     /* No. of outstanding bytes */
98         struct ifqueue          sc_rxq;
99         struct callout          sc_c;
100
101         /* TTY related structures */
102         struct ucom_super_softc sc_super_ucom;
103         int                     sc_ttys;
104         struct uhso_tty         *sc_tty;
105         struct ucom_softc       *sc_ucom;
106         int                     sc_msr;
107         int                     sc_lsr;
108         int                     sc_line;
109 };
110
111 #define UHSO_MAX_MTU            2048
112
113 /*
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
117  * on the others.
118  * The second type of card has several other interfaces, their purpose
119  * can be detected during run-time.
120  */
121 #define UHSO_IFACE_SPEC(usb_type, port, port_type) \
122         (((usb_type) << 24) | ((port) << 16) | (port_type))
123
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)
127
128 /*
129  * USB interface types
130  */
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 */
134
135 /*
136  * Port types
137  */
138 #define UHSO_PORT_UNKNOWN       0x00
139 #define UHSO_PORT_SERIAL        0x01    /* Serial port */
140 #define UHSO_PORT_NETWORK       0x02    /* Network packet interface */
141
142 /*
143  * Multiplexed serial port destination sub-port names
144  */
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 */
152
153 /*
154  * Port definitions
155  * Note that these definitions are arbitrary and do not match the values
156  * returned by the auto config descriptor.
157  */
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
172
173 static eventhandler_tag uhso_etag;
174
175 /* Overall port type */
176 static char *uhso_port[] = {
177         "Unknown",
178         "Serial",
179         "Network",
180         "Network/Serial"
181 };
182
183 /*
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
186  * descriptor values.
187  */
188 static unsigned char uhso_port_map[] = {
189         UHSO_PORT_TYPE_UNKNOWN,
190         UHSO_PORT_TYPE_DIAG,
191         UHSO_PORT_TYPE_GPS,
192         UHSO_PORT_TYPE_GPSCTL,
193         UHSO_PORT_TYPE_APP,
194         UHSO_PORT_TYPE_APP2,
195         UHSO_PORT_TYPE_CTL,
196         UHSO_PORT_TYPE_NETWORK,
197         UHSO_PORT_TYPE_MODEM,
198         UHSO_PORT_TYPE_MSD,
199         UHSO_PORT_TYPE_PCSC,
200         UHSO_PORT_TYPE_VOICE
201 };
202 static char uhso_port_map_max = sizeof(uhso_port_map) / sizeof(char);
203
204 static unsigned char uhso_mux_port_map[] = {
205         UHSO_PORT_TYPE_CTL,
206         UHSO_PORT_TYPE_APP,
207         UHSO_PORT_TYPE_PCSC,
208         UHSO_PORT_TYPE_GPS,
209         UHSO_PORT_TYPE_APP2
210 };
211
212 static char *uhso_port_type[] = {
213         "Unknown",  /* Not a valid port */
214         "Control",
215         "Application",
216         "Application (Secondary)",
217         "Modem",
218         "Network",
219         "Diagnostic",
220         "Diagnostic (Secondary)",
221         "GPS",
222         "GPS Control",
223         "PC Smartcard",
224         "MSD",
225         "Voice",
226 };
227
228 static char *uhso_port_type_sysctl[] = {
229         "unknown",
230         "control",
231         "application",
232         "application",
233         "modem",
234         "network",
235         "diagnostic",
236         "diagnostic",
237         "gps",
238         "gps_control",
239         "pcsc",
240         "msd",
241         "voice",
242 };
243
244 #define UHSO_STATIC_IFACE       0x01
245 #define UHSO_AUTO_IFACE         0x02
246
247 /* ifnet device unit allocations */
248 static struct unrhdr *uhso_ifnet_unit = NULL;
249
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),
262         /* GE40x */
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),
283 #undef UHSO_DEV
284 };
285
286 static 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");
290
291 #ifdef USB_DEBUG
292 #ifdef UHSO_DEBUG
293 static int uhso_debug = UHSO_DEBUG;
294 #else
295 static int uhso_debug = -1;
296 #endif
297
298 SYSCTL_INT(_hw_usb_uhso, OID_AUTO, debug, CTLFLAG_RW,
299     &uhso_debug, 0, "Debug level");
300
301 #define UHSO_DPRINTF(n, x, ...) {\
302         if (uhso_debug >= n) {\
303                 printf("%s: " x, __func__, ##__VA_ARGS__);\
304         }\
305 }
306 #else
307 #define UHSO_DPRINTF(n, x, ...)
308 #endif
309
310 #ifdef UHSO_DEBUG_HEXDUMP
311 # define UHSO_HEXDUMP(_buf, _len) do { \
312   { \
313         size_t __tmp; \
314         const char *__buf = (const char *)_buf; \
315         for (__tmp = 0; __tmp < _len; __tmp++) \
316                 printf("%02hhx ", *__buf++); \
317     printf("\n"); \
318   } \
319 } while(0)
320 #else
321 # define UHSO_HEXDUMP(_buf, _len)
322 #endif
323
324 enum {
325         UHSO_MUX_ENDPT_INTR = 0,
326         UHSO_MUX_ENDPT_MAX
327 };
328
329 enum {
330         UHSO_CTRL_READ = 0,
331         UHSO_CTRL_WRITE,
332         UHSO_CTRL_MAX
333 };
334
335 enum {
336         UHSO_IFNET_READ = 0,
337         UHSO_IFNET_WRITE,
338         UHSO_IFNET_MAX
339 };
340
341 enum {
342         UHSO_BULK_ENDPT_READ = 0,
343         UHSO_BULK_ENDPT_WRITE,
344         UHSO_BULK_ENDPT_INTR,
345         UHSO_BULK_ENDPT_MAX
346 };
347
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;
356
357 /* Config used for the default control pipes */
358 static const struct usb_config uhso_ctrl_config[UHSO_CTRL_MAX] = {
359         [UHSO_CTRL_READ] = {
360                 .type = UE_CONTROL,
361                 .endpoint = 0x00,
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
366         },
367
368         [UHSO_CTRL_WRITE] = {
369                 .type = UE_CONTROL,
370                 .endpoint = 0x00,
371                 .direction = UE_DIR_ANY,
372                 .flags = { .pipe_bof = 1, .force_short_xfer = 1 },
373                 .bufsize = sizeof(struct usb_device_request) + 1024,
374                 .timeout = 1000,
375                 .callback = &uhso_mux_write_callback
376         }
377 };
378
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 },
386                 .bufsize = 0,
387                 .callback = &uhso_mux_intr_callback,
388         }
389 };
390
391 /* Config for the raw IP-packet interface */
392 static const struct usb_config uhso_ifnet_config[UHSO_IFNET_MAX] = {
393         [UHSO_IFNET_READ] = {
394                 .type = UE_BULK,
395                 .endpoint = UE_ADDR_ANY,
396                 .direction = UE_DIR_IN,
397                 .flags = { .pipe_bof = 1, .short_xfer_ok = 1 },
398                 .bufsize = MCLBYTES,
399                 .callback = &uhso_ifnet_read_callback
400         },
401         [UHSO_IFNET_WRITE] = {
402                 .type = UE_BULK,
403                 .endpoint = UE_ADDR_ANY,
404                 .direction = UE_DIR_OUT,
405                 .flags = { .pipe_bof = 1, .force_short_xfer = 1 },
406                 .bufsize = MCLBYTES,
407                 .timeout = 5 * USB_MS_HZ,
408                 .callback = &uhso_ifnet_write_callback
409         }
410 };
411
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] = {
415                 .type = UE_BULK,
416                 .endpoint = UE_ADDR_ANY,
417                 .direction = UE_DIR_IN,
418                 .flags = { .pipe_bof = 1, .short_xfer_ok = 1 },
419                 .bufsize = 4096,
420                 .callback = &uhso_bs_read_callback
421         },
422
423         [UHSO_BULK_ENDPT_WRITE] = {
424                 .type = UE_BULK,
425                 .endpoint = UE_ADDR_ANY,
426                 .direction = UE_DIR_OUT,
427                 .flags = { .pipe_bof = 1, .force_short_xfer = 1 },
428                 .bufsize = 8192,
429                 .callback = &uhso_bs_write_callback
430         },
431
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 },
437                 .bufsize = 0,
438                 .callback = &uhso_bs_intr_callback,
439         }
440 };
441
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 *,
447     int type);
448 static int  uhso_attach_bulkserial(struct uhso_softc *, struct usb_interface *,
449     int type);
450 static int  uhso_attach_ifnet(struct uhso_softc *, struct usb_interface *,
451     int type);
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);
457
458 static void uhso_free(struct ucom_softc *);
459 static void uhso_ucom_start_read(struct ucom_softc *);
460 static void uhso_ucom_stop_read(struct ucom_softc *);
461 static void uhso_ucom_start_write(struct ucom_softc *);
462 static void uhso_ucom_stop_write(struct ucom_softc *);
463 static void uhso_ucom_cfg_get_status(struct ucom_softc *, uint8_t *, uint8_t *);
464 static void uhso_ucom_cfg_set_dtr(struct ucom_softc *, uint8_t);
465 static void uhso_ucom_cfg_set_rts(struct ucom_softc *, uint8_t);
466 static void uhso_if_init(void *);
467 static void uhso_if_start(struct ifnet *);
468 static void uhso_if_stop(struct uhso_softc *);
469 static int  uhso_if_ioctl(struct ifnet *, u_long, caddr_t);
470 static int  uhso_if_output(struct ifnet *, struct mbuf *,
471     const struct sockaddr *, struct route *);
472 static void uhso_if_rxflush(void *);
473
474 static device_probe_t uhso_probe;
475 static device_attach_t uhso_attach;
476 static device_detach_t uhso_detach;
477 static void uhso_free_softc(struct uhso_softc *);
478
479 static device_method_t uhso_methods[] = {
480         DEVMETHOD(device_probe,         uhso_probe),
481         DEVMETHOD(device_attach,        uhso_attach),
482         DEVMETHOD(device_detach,        uhso_detach),
483         { 0, 0 }
484 };
485
486 static driver_t uhso_driver = {
487         .name = "uhso",
488         .methods = uhso_methods,
489         .size = sizeof(struct uhso_softc)
490 };
491
492 static devclass_t uhso_devclass;
493 DRIVER_MODULE(uhso, uhub, uhso_driver, uhso_devclass, uhso_driver_loaded, 0);
494 MODULE_DEPEND(uhso, ucom, 1, 1, 1);
495 MODULE_DEPEND(uhso, usb, 1, 1, 1);
496 MODULE_VERSION(uhso, 1);
497
498 static struct ucom_callback uhso_ucom_callback = {
499         .ucom_cfg_get_status = &uhso_ucom_cfg_get_status,
500         .ucom_cfg_set_dtr = &uhso_ucom_cfg_set_dtr,
501         .ucom_cfg_set_rts = &uhso_ucom_cfg_set_rts,
502         .ucom_start_read = uhso_ucom_start_read,
503         .ucom_stop_read = uhso_ucom_stop_read,
504         .ucom_start_write = uhso_ucom_start_write,
505         .ucom_stop_write = uhso_ucom_stop_write,
506         .ucom_free = &uhso_free,
507 };
508
509 static int
510 uhso_probe(device_t self)
511 {
512         struct usb_attach_arg *uaa = device_get_ivars(self);
513         int error;
514
515         if (uaa->usb_mode != USB_MODE_HOST)
516                 return (ENXIO);
517         if (uaa->info.bConfigIndex != 0)
518                 return (ENXIO);
519         if (uaa->info.bDeviceClass != 0xff)
520                 return (ENXIO);
521
522         error = usbd_lookup_id_by_uaa(uhso_devs, sizeof(uhso_devs), uaa);
523         if (error != 0)
524                 return (error);
525
526         /*
527          * Probe device to see if we are able to attach
528          * to this interface or not.
529          */
530         if (USB_GET_DRIVER_INFO(uaa) == UHSO_AUTO_IFACE) {
531                 if (uhso_probe_iface_auto(uaa->device,
532                     uaa->info.bIfaceNum) == 0)
533                         return (ENXIO);
534         }
535         return (error);
536 }
537
538 static int
539 uhso_attach(device_t self)
540 {
541         struct uhso_softc *sc = device_get_softc(self);
542         struct usb_attach_arg *uaa = device_get_ivars(self);
543         struct usb_interface_descriptor *id;
544         struct sysctl_ctx_list *sctx;
545         struct sysctl_oid *soid;
546         struct sysctl_oid *tree = NULL, *tty_node;
547         struct ucom_softc *ucom;
548         struct uhso_tty *ht;
549         int i, error, port;
550         void *probe_f;
551         usb_error_t uerr;
552         char *desc;
553
554         sc->sc_dev = self;
555         sc->sc_udev = uaa->device;
556         mtx_init(&sc->sc_mtx, "uhso", NULL, MTX_DEF);
557         ucom_ref(&sc->sc_super_ucom);
558
559         sc->sc_ucom = NULL;
560         sc->sc_ttys = 0;
561         sc->sc_radio = 1;
562
563         id = usbd_get_interface_descriptor(uaa->iface);
564         sc->sc_ctrl_iface_no = id->bInterfaceNumber;
565
566         sc->sc_iface_no = uaa->info.bIfaceNum;
567         sc->sc_iface_index = uaa->info.bIfaceIndex;
568
569         /* Setup control pipe */
570         uerr = usbd_transfer_setup(uaa->device,
571             &sc->sc_iface_index, sc->sc_ctrl_xfer,
572             uhso_ctrl_config, UHSO_CTRL_MAX, sc, &sc->sc_mtx);
573         if (uerr) {
574                 device_printf(self, "Failed to setup control pipe: %s\n",
575                     usbd_errstr(uerr));
576                 goto out;
577         }
578
579         if (USB_GET_DRIVER_INFO(uaa) == UHSO_STATIC_IFACE)
580                 probe_f = uhso_probe_iface_static;
581         else if (USB_GET_DRIVER_INFO(uaa) == UHSO_AUTO_IFACE)
582                 probe_f = uhso_probe_iface_auto;
583         else
584                 goto out;
585
586         error = uhso_probe_iface(sc, uaa->info.bIfaceNum, probe_f);
587         if (error != 0)
588                 goto out;
589
590         sctx = device_get_sysctl_ctx(sc->sc_dev);
591         soid = device_get_sysctl_tree(sc->sc_dev);
592
593         SYSCTL_ADD_STRING(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "type",
594             CTLFLAG_RD, uhso_port[UHSO_IFACE_PORT(sc->sc_type)], 0,
595             "Port available at this interface");
596         SYSCTL_ADD_PROC(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "radio",
597             CTLTYPE_INT | CTLFLAG_RW, sc, 0, uhso_radio_sysctl, "I", "Enable radio");
598
599         /*
600          * The default interface description on most Option devices isn't
601          * very helpful. So we skip device_set_usb_desc and set the
602          * device description manually.
603          */
604         device_set_desc_copy(self, uhso_port_type[UHSO_IFACE_PORT_TYPE(sc->sc_type)]); 
605         /* Announce device */
606         device_printf(self, "<%s port> at <%s %s> on %s\n",
607             uhso_port_type[UHSO_IFACE_PORT_TYPE(sc->sc_type)],
608             usb_get_manufacturer(uaa->device),
609             usb_get_product(uaa->device),
610             device_get_nameunit(device_get_parent(self)));
611
612         if (sc->sc_ttys > 0) {
613                 SYSCTL_ADD_INT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "ports",
614                     CTLFLAG_RD, &sc->sc_ttys, 0, "Number of attached serial ports");
615
616                 tree = SYSCTL_ADD_NODE(sctx, SYSCTL_CHILDREN(soid), OID_AUTO,
617                     "port", CTLFLAG_RD, NULL, "Serial ports");
618         }
619
620         /*
621          * Loop through the number of found TTYs and create sysctl
622          * nodes for them.
623          */
624         for (i = 0; i < sc->sc_ttys; i++) {
625                 ht = &sc->sc_tty[i];
626                 ucom = &sc->sc_ucom[i];
627
628                 if (UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_MUX)
629                         port = uhso_mux_port_map[ht->ht_muxport];
630                 else
631                         port = UHSO_IFACE_PORT_TYPE(sc->sc_type);
632
633                 desc = uhso_port_type_sysctl[port];
634
635                 tty_node = SYSCTL_ADD_NODE(sctx, SYSCTL_CHILDREN(tree), OID_AUTO,
636                     desc, CTLFLAG_RD, NULL, "");
637
638                 ht->ht_name[0] = 0;
639                 if (sc->sc_ttys == 1)
640                         snprintf(ht->ht_name, 32, "cuaU%d", ucom->sc_super->sc_unit);
641                 else {
642                         snprintf(ht->ht_name, 32, "cuaU%d.%d",
643                             ucom->sc_super->sc_unit, ucom->sc_subunit);
644                 }
645
646                 desc = uhso_port_type[port];
647                 SYSCTL_ADD_STRING(sctx, SYSCTL_CHILDREN(tty_node), OID_AUTO,
648                     "tty", CTLFLAG_RD, ht->ht_name, 0, "");
649                 SYSCTL_ADD_STRING(sctx, SYSCTL_CHILDREN(tty_node), OID_AUTO,
650                     "desc", CTLFLAG_RD, desc, 0, "");
651
652                 if (bootverbose)
653                         device_printf(sc->sc_dev,
654                             "\"%s\" port at %s\n", desc, ht->ht_name);
655         }
656
657         return (0);
658 out:
659         uhso_detach(sc->sc_dev);
660         return (ENXIO);
661 }
662
663 static int
664 uhso_detach(device_t self)
665 {
666         struct uhso_softc *sc = device_get_softc(self);
667         int i;
668
669         usbd_transfer_unsetup(sc->sc_xfer, 3);
670         usbd_transfer_unsetup(sc->sc_ctrl_xfer, UHSO_CTRL_MAX);
671         if (sc->sc_ttys > 0) {
672                 ucom_detach(&sc->sc_super_ucom, sc->sc_ucom);
673
674                 for (i = 0; i < sc->sc_ttys; i++) {
675                         if (sc->sc_tty[i].ht_muxport != -1) {
676                                 usbd_transfer_unsetup(sc->sc_tty[i].ht_xfer,
677                                     UHSO_CTRL_MAX);
678                         }
679                 }
680
681                 free(sc->sc_tty, M_USBDEV);
682                 free(sc->sc_ucom, M_USBDEV);
683         }
684
685         if (sc->sc_ifp != NULL) {
686                 callout_drain(&sc->sc_c);
687                 free_unr(uhso_ifnet_unit, sc->sc_ifp->if_dunit);
688                 mtx_lock(&sc->sc_mtx);
689                 uhso_if_stop(sc);
690                 bpfdetach(sc->sc_ifp);
691                 if_detach(sc->sc_ifp);
692                 if_free(sc->sc_ifp);
693                 mtx_unlock(&sc->sc_mtx);
694                 usbd_transfer_unsetup(sc->sc_if_xfer, UHSO_IFNET_MAX);
695         }
696
697         device_claim_softc(self);
698
699         uhso_free_softc(sc);
700
701         return (0);
702 }
703
704 UCOM_UNLOAD_DRAIN(uhso);
705
706 static void
707 uhso_free_softc(struct uhso_softc *sc)
708 {
709         if (ucom_unref(&sc->sc_super_ucom)) {
710                 mtx_destroy(&sc->sc_mtx);
711                 device_free_softc(sc);
712         }
713 }
714
715 static void
716 uhso_free(struct ucom_softc *ucom)
717 {
718         uhso_free_softc(ucom->sc_parent);
719 }
720
721 static void
722 uhso_test_autoinst(void *arg, struct usb_device *udev,
723     struct usb_attach_arg *uaa)
724 {
725         struct usb_interface *iface;
726         struct usb_interface_descriptor *id;
727
728         if (uaa->dev_state != UAA_DEV_READY || !uhso_autoswitch)
729                 return;
730
731         iface = usbd_get_iface(udev, 0);
732         if (iface == NULL)
733                 return;
734         id = iface->idesc;
735         if (id == NULL || id->bInterfaceClass != UICLASS_MASS)
736                 return;
737         if (usbd_lookup_id_by_uaa(uhso_devs, sizeof(uhso_devs), uaa))
738                 return;         /* no device match */
739
740         if (usb_msc_eject(udev, 0, MSC_EJECT_REZERO) == 0) {
741                 /* success, mark the udev as disappearing */
742                 uaa->dev_state = UAA_DEV_EJECTING;
743         }
744 }
745
746 static int
747 uhso_driver_loaded(struct module *mod, int what, void *arg)
748 {
749         switch (what) {
750         case MOD_LOAD:
751                 /* register our autoinstall handler */
752                 uhso_etag = EVENTHANDLER_REGISTER(usb_dev_configured,
753                     uhso_test_autoinst, NULL, EVENTHANDLER_PRI_ANY);
754                 /* create our unit allocator for inet devs */
755                 uhso_ifnet_unit = new_unrhdr(0, INT_MAX, NULL);
756                 break;
757         case MOD_UNLOAD:
758                 EVENTHANDLER_DEREGISTER(usb_dev_configured, uhso_etag);
759                 delete_unrhdr(uhso_ifnet_unit);
760                 break;
761         default:
762                 return (EOPNOTSUPP);
763         }
764         return (0);
765 }
766
767 /*
768  * Probe the interface type by querying the device. The elements
769  * of an array indicates the capabilities of a particular interface.
770  * Returns a bit mask with the interface capabilities.
771  */
772 static int
773 uhso_probe_iface_auto(struct usb_device *udev, int index)
774 {
775         struct usb_device_request req;
776         usb_error_t uerr;
777         uint16_t actlen = 0;
778         char port;
779         char buf[17] = {0};
780
781         req.bmRequestType = UT_READ_VENDOR_DEVICE;
782         req.bRequest = 0x86;
783         USETW(req.wValue, 0);
784         USETW(req.wIndex, 0);
785         USETW(req.wLength, 17);
786
787         uerr = usbd_do_request_flags(udev, NULL, &req, buf,
788             0, &actlen, USB_MS_HZ);
789         if (uerr != 0) {
790                 printf("%s: usbd_do_request_flags failed, %s\n",
791                     __func__, usbd_errstr(uerr));
792                 return (0);
793         }
794
795         UHSO_DPRINTF(1, "actlen=%d\n", actlen);
796         UHSO_HEXDUMP(buf, 17);
797
798         if (index < 0 || index > 16) {
799                 UHSO_DPRINTF(0, "Index %d out of range\n", index);
800                 return (0);
801         }
802
803         UHSO_DPRINTF(1, "index=%d, type=%x[%s]\n", index, buf[index],
804             uhso_port_type[(int)uhso_port_map[(int)buf[index]]]);
805
806         if (buf[index] >= uhso_port_map_max)
807                 port = 0;
808         else
809                 port = uhso_port_map[(int)buf[index]];
810
811         switch (port) {
812         case UHSO_PORT_TYPE_NETWORK:
813                 return (UHSO_IFACE_SPEC(UHSO_IF_NET | UHSO_IF_MUX,
814                     UHSO_PORT_SERIAL | UHSO_PORT_NETWORK, port));
815         case UHSO_PORT_TYPE_DIAG:
816         case UHSO_PORT_TYPE_DIAG2:
817         case UHSO_PORT_TYPE_CTL:
818         case UHSO_PORT_TYPE_APP:
819         case UHSO_PORT_TYPE_APP2:
820         case UHSO_PORT_TYPE_MODEM:
821                 return (UHSO_IFACE_SPEC(UHSO_IF_BULK,
822                     UHSO_PORT_SERIAL, port));
823         case UHSO_PORT_TYPE_MSD:
824                 return (0);
825         case UHSO_PORT_TYPE_UNKNOWN:
826         default:
827                 return (0);
828         }
829
830         return (0);
831 }
832
833 /*
834  * Returns the capabilities of interfaces for devices that don't
835  * support the automatic query.
836  * Returns a bit mask with the interface capabilities.
837  */
838 static int
839 uhso_probe_iface_static(struct usb_device *udev, int index)
840 {
841         struct usb_config_descriptor *cd;
842
843         cd = usbd_get_config_descriptor(udev);
844         if (cd->bNumInterface <= 3) {
845                 /* Cards with 3 or less interfaces */
846                 switch (index) {
847                 case 0:
848                         return UHSO_IFACE_SPEC(UHSO_IF_NET | UHSO_IF_MUX,
849                             UHSO_PORT_SERIAL | UHSO_PORT_NETWORK,
850                             UHSO_PORT_TYPE_NETWORK);
851                 case 1:
852                         return UHSO_IFACE_SPEC(UHSO_IF_BULK,
853                             UHSO_PORT_SERIAL, UHSO_PORT_TYPE_DIAG);
854                 case 2:
855                         return UHSO_IFACE_SPEC(UHSO_IF_BULK,
856                             UHSO_PORT_SERIAL, UHSO_PORT_TYPE_MODEM);
857                 }
858         } else {
859                 /* Cards with 4 interfaces */
860                 switch (index) {
861                 case 0:
862                         return UHSO_IFACE_SPEC(UHSO_IF_NET | UHSO_IF_MUX,
863                             UHSO_PORT_SERIAL | UHSO_PORT_NETWORK,
864                             UHSO_PORT_TYPE_NETWORK);
865                 case 1:
866                         return UHSO_IFACE_SPEC(UHSO_IF_BULK,
867                             UHSO_PORT_SERIAL, UHSO_PORT_TYPE_DIAG2);
868                 case 2:
869                         return UHSO_IFACE_SPEC(UHSO_IF_BULK,
870                             UHSO_PORT_SERIAL, UHSO_PORT_TYPE_MODEM);
871                 case 3:
872                         return UHSO_IFACE_SPEC(UHSO_IF_BULK,
873                             UHSO_PORT_SERIAL, UHSO_PORT_TYPE_DIAG);
874                 }
875         }
876         return (0);
877 }
878
879 /*
880  * Probes an interface for its particular capabilities and attaches if
881  * it's a supported interface.
882  */
883 static int
884 uhso_probe_iface(struct uhso_softc *sc, int index,
885     int (*probe)(struct usb_device *, int))
886 {
887         struct usb_interface *iface;
888         int type, error;
889
890         UHSO_DPRINTF(1, "Probing for interface %d, probe_func=%p\n", index, probe);
891
892         type = probe(sc->sc_udev, index);
893         UHSO_DPRINTF(1, "Probe result %x\n", type);
894         if (type <= 0)
895                 return (ENXIO);
896
897         sc->sc_type = type;
898         iface = usbd_get_iface(sc->sc_udev, index);
899
900         if (UHSO_IFACE_PORT_TYPE(type) == UHSO_PORT_TYPE_NETWORK) {
901                 error = uhso_attach_ifnet(sc, iface, type);
902                 if (error) {
903                         UHSO_DPRINTF(1, "uhso_attach_ifnet failed");
904                         return (ENXIO);
905                 }
906
907                 /*
908                  * If there is an additional interrupt endpoint on this
909                  * interface then we most likely have a multiplexed serial port
910                  * available.
911                  */
912                 if (iface->idesc->bNumEndpoints < 3) {
913                         sc->sc_type = UHSO_IFACE_SPEC( 
914                             UHSO_IFACE_USB_TYPE(type) & ~UHSO_IF_MUX,
915                             UHSO_IFACE_PORT(type) & ~UHSO_PORT_SERIAL,
916                             UHSO_IFACE_PORT_TYPE(type));
917                         return (0);
918                 }
919
920                 UHSO_DPRINTF(1, "Trying to attach mux. serial\n");
921                 error = uhso_attach_muxserial(sc, iface, type);
922                 if (error == 0 && sc->sc_ttys > 0) {
923                         error = ucom_attach(&sc->sc_super_ucom, sc->sc_ucom,
924                             sc->sc_ttys, sc, &uhso_ucom_callback, &sc->sc_mtx);
925                         if (error) {
926                                 device_printf(sc->sc_dev, "ucom_attach failed\n");
927                                 return (ENXIO);
928                         }
929                         ucom_set_pnpinfo_usb(&sc->sc_super_ucom, sc->sc_dev);
930
931                         mtx_lock(&sc->sc_mtx);
932                         usbd_transfer_start(sc->sc_xfer[UHSO_MUX_ENDPT_INTR]);
933                         mtx_unlock(&sc->sc_mtx);
934                 }
935         } else if ((UHSO_IFACE_USB_TYPE(type) & UHSO_IF_BULK) &&
936             UHSO_IFACE_PORT(type) & UHSO_PORT_SERIAL) {
937
938                 error = uhso_attach_bulkserial(sc, iface, type);
939                 if (error)
940                         return (ENXIO);
941
942                 error = ucom_attach(&sc->sc_super_ucom, sc->sc_ucom,
943                     sc->sc_ttys, sc, &uhso_ucom_callback, &sc->sc_mtx);
944                 if (error) {
945                         device_printf(sc->sc_dev, "ucom_attach failed\n");
946                         return (ENXIO);
947                 }
948                 ucom_set_pnpinfo_usb(&sc->sc_super_ucom, sc->sc_dev);
949         }
950         else {
951                 UHSO_DPRINTF(0, "Unknown type %x\n", type);
952                 return (ENXIO);
953         }
954
955         return (0);
956 }
957
958 static int
959 uhso_radio_ctrl(struct uhso_softc *sc, int onoff)
960 {
961         struct usb_device_request req;
962         usb_error_t uerr;
963
964         req.bmRequestType = UT_VENDOR;
965         req.bRequest = onoff ? 0x82 : 0x81;
966         USETW(req.wValue, 0);
967         USETW(req.wIndex, 0);
968         USETW(req.wLength, 0);
969
970         uerr = usbd_do_request(sc->sc_udev, NULL, &req, NULL);
971         if (uerr != 0) {
972                 device_printf(sc->sc_dev, "usbd_do_request_flags failed: %s\n",
973                     usbd_errstr(uerr));
974                 return (-1);
975         }
976         return (onoff);
977 }
978
979 static int
980 uhso_radio_sysctl(SYSCTL_HANDLER_ARGS)
981 {
982         struct uhso_softc *sc = arg1;
983         int error, radio;
984
985         radio = sc->sc_radio;
986         error = sysctl_handle_int(oidp, &radio, 0, req);
987         if (error)
988                 return (error);
989         if (radio != sc->sc_radio) {
990                 radio = radio != 0 ? 1 : 0;
991                 error = uhso_radio_ctrl(sc, radio);
992                 if (error != -1)
993                         sc->sc_radio = radio;
994                         
995         }       
996         return (0);
997 }
998
999 /*
1000  * Expands allocated memory to fit an additional TTY.
1001  * Two arrays are kept with matching indexes, one for ucom and one
1002  * for our private data.
1003  */
1004 static int
1005 uhso_alloc_tty(struct uhso_softc *sc)
1006 {
1007
1008         sc->sc_ttys++;
1009         sc->sc_tty = reallocf(sc->sc_tty, sizeof(struct uhso_tty) * sc->sc_ttys,
1010             M_USBDEV, M_WAITOK | M_ZERO);
1011         if (sc->sc_tty == NULL)
1012                 return (-1);
1013
1014         sc->sc_ucom = reallocf(sc->sc_ucom,
1015             sizeof(struct ucom_softc) * sc->sc_ttys, M_USBDEV, M_WAITOK | M_ZERO);
1016         if (sc->sc_ucom == NULL)
1017                 return (-1);
1018
1019         sc->sc_tty[sc->sc_ttys - 1].ht_sc = sc;
1020
1021         UHSO_DPRINTF(1, "Allocated TTY %d\n", sc->sc_ttys - 1); 
1022         return (sc->sc_ttys - 1);
1023 }
1024
1025 /*
1026  * Attach a multiplexed serial port
1027  * Data is read/written with requests on the default control pipe. An interrupt
1028  * endpoint returns when there is new data to be read.
1029  */
1030 static int
1031 uhso_attach_muxserial(struct uhso_softc *sc, struct usb_interface *iface,
1032     int type)
1033 {
1034         struct usb_descriptor *desc;
1035         int i, port, tty;
1036         usb_error_t uerr;
1037
1038         /*
1039          * The class specific interface (type 0x24) descriptor subtype field
1040          * contains a bitmask that specifies which (and how many) ports that
1041          * are available through this multiplexed serial port.
1042          */
1043         desc = usbd_find_descriptor(sc->sc_udev, NULL,
1044             iface->idesc->bInterfaceNumber, UDESC_CS_INTERFACE, 0xff, 0, 0);
1045         if (desc == NULL) {
1046                 UHSO_DPRINTF(0, "Failed to find UDESC_CS_INTERFACE\n");
1047                 return (ENXIO);
1048         }
1049
1050         UHSO_DPRINTF(1, "Mux port mask %x\n", desc->bDescriptorSubtype);
1051         if (desc->bDescriptorSubtype == 0)
1052                 return (ENXIO);
1053
1054         /*
1055          * The bitmask is one octet, loop through the number of
1056          * bits that are set and create a TTY for each.
1057          */
1058         for (i = 0; i < 8; i++) {
1059                 port = (1 << i);
1060                 if ((port & desc->bDescriptorSubtype) == port) {
1061                         UHSO_DPRINTF(2, "Found mux port %x (%d)\n", port, i);
1062                         tty = uhso_alloc_tty(sc);
1063                         if (tty < 0)
1064                                 return (ENOMEM);
1065                         sc->sc_tty[tty].ht_muxport = i;
1066                         uerr = usbd_transfer_setup(sc->sc_udev, 
1067                             &sc->sc_iface_index, sc->sc_tty[tty].ht_xfer,
1068                             uhso_ctrl_config, UHSO_CTRL_MAX, sc, &sc->sc_mtx);
1069                         if (uerr) {
1070                                 device_printf(sc->sc_dev,
1071                                     "Failed to setup control pipe: %s\n",
1072                                     usbd_errstr(uerr));
1073                                 return (ENXIO);
1074                         }
1075                 }
1076         }
1077
1078         /* Setup the intr. endpoint */
1079         uerr = usbd_transfer_setup(sc->sc_udev,
1080             &iface->idesc->bInterfaceNumber, sc->sc_xfer,
1081             uhso_mux_config, 1, sc, &sc->sc_mtx);
1082         if (uerr)
1083                 return (ENXIO);
1084
1085         return (0);
1086 }
1087
1088 /*
1089  * Interrupt callback for the multiplexed serial port. Indicates
1090  * which serial port has data waiting.
1091  */
1092 static void
1093 uhso_mux_intr_callback(struct usb_xfer *xfer, usb_error_t error)
1094 {
1095         struct usb_page_cache *pc;
1096         struct usb_page_search res;
1097         struct uhso_softc *sc = usbd_xfer_softc(xfer);
1098         unsigned int i, mux;
1099
1100         UHSO_DPRINTF(3, "status %d\n", USB_GET_STATE(xfer));
1101
1102         switch (USB_GET_STATE(xfer)) {
1103         case USB_ST_TRANSFERRED:
1104                 /*
1105                  * The multiplexed port number can be found at the first byte.
1106                  * It contains a bit mask, we transform this in to an integer.
1107                  */
1108                 pc = usbd_xfer_get_frame(xfer, 0);
1109                 usbd_get_page(pc, 0, &res);
1110
1111                 i = *((unsigned char *)res.buffer);
1112                 mux = 0;
1113                 while (i >>= 1) {
1114                         mux++;
1115                 }
1116
1117                 UHSO_DPRINTF(3, "mux port %d (%d)\n", mux, i);
1118                 if (mux > UHSO_MPORT_TYPE_NOMAX)
1119                         break;
1120
1121                 /* Issue a read for this serial port */
1122                 usbd_xfer_set_priv(
1123                     sc->sc_tty[mux].ht_xfer[UHSO_CTRL_READ],
1124                     &sc->sc_tty[mux]);
1125                 usbd_transfer_start(sc->sc_tty[mux].ht_xfer[UHSO_CTRL_READ]);
1126
1127                 break;
1128         case USB_ST_SETUP:
1129 tr_setup:
1130                 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
1131                 usbd_transfer_submit(xfer);
1132                 break;
1133         default:
1134                 UHSO_DPRINTF(0, "error: %s\n", usbd_errstr(error));
1135                 if (error == USB_ERR_CANCELLED)
1136                         break;
1137
1138                 usbd_xfer_set_stall(xfer);
1139                 goto tr_setup;
1140         }
1141 }
1142
1143 static void
1144 uhso_mux_read_callback(struct usb_xfer *xfer, usb_error_t error)
1145 {
1146         struct uhso_softc *sc = usbd_xfer_softc(xfer);
1147         struct usb_page_cache *pc;
1148         struct usb_device_request req;
1149         struct uhso_tty *ht;
1150         int actlen, len;
1151
1152         usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
1153
1154         UHSO_DPRINTF(3, "status %d\n", USB_GET_STATE(xfer));
1155
1156         ht = usbd_xfer_get_priv(xfer);
1157         UHSO_DPRINTF(3, "ht=%p open=%d\n", ht, ht->ht_open);
1158
1159         switch (USB_GET_STATE(xfer)) {
1160         case USB_ST_TRANSFERRED:
1161                 /* Got data, send to ucom */
1162                 pc = usbd_xfer_get_frame(xfer, 1);
1163                 len = usbd_xfer_frame_len(xfer, 1);
1164
1165                 UHSO_DPRINTF(3, "got %d bytes on mux port %d\n", len,
1166                     ht->ht_muxport);
1167                 if (len <= 0) {
1168                         usbd_transfer_start(sc->sc_xfer[UHSO_MUX_ENDPT_INTR]);
1169                         break;
1170                 }
1171
1172                 /* Deliver data if the TTY is open, discard otherwise */
1173                 if (ht->ht_open)
1174                         ucom_put_data(&sc->sc_ucom[ht->ht_muxport], pc, 0, len);
1175                 /* FALLTHROUGH */
1176         case USB_ST_SETUP:
1177 tr_setup:
1178                 memset(&req, 0, sizeof(struct usb_device_request));
1179                 req.bmRequestType = UT_READ_CLASS_INTERFACE;
1180                 req.bRequest = UCDC_GET_ENCAPSULATED_RESPONSE;
1181                 USETW(req.wValue, 0);
1182                 USETW(req.wIndex, ht->ht_muxport);
1183                 USETW(req.wLength, 1024);
1184
1185                 pc = usbd_xfer_get_frame(xfer, 0);
1186                 usbd_copy_in(pc, 0, &req, sizeof(req));
1187
1188                 usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
1189                 usbd_xfer_set_frame_len(xfer, 1, 1024);
1190                 usbd_xfer_set_frames(xfer, 2);
1191                 usbd_transfer_submit(xfer);
1192                 break;
1193         default:
1194                 UHSO_DPRINTF(0, "error: %s\n", usbd_errstr(error));
1195                 if (error == USB_ERR_CANCELLED)
1196                         break;
1197                 usbd_xfer_set_stall(xfer);
1198                 goto tr_setup;
1199         }
1200 }
1201
1202 static void
1203 uhso_mux_write_callback(struct usb_xfer *xfer, usb_error_t error)
1204 {
1205         struct uhso_softc *sc = usbd_xfer_softc(xfer);
1206         struct uhso_tty *ht;
1207         struct usb_page_cache *pc;
1208         struct usb_device_request req;
1209         int actlen;
1210         struct usb_page_search res;
1211
1212         usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
1213
1214         ht = usbd_xfer_get_priv(xfer);
1215         UHSO_DPRINTF(3, "status=%d, using mux port %d\n",
1216             USB_GET_STATE(xfer), ht->ht_muxport);
1217
1218         switch (USB_GET_STATE(xfer)) {
1219         case USB_ST_TRANSFERRED:
1220                 UHSO_DPRINTF(3, "wrote %zd data bytes to muxport %d\n",
1221                     actlen - sizeof(struct usb_device_request) ,
1222                     ht->ht_muxport);
1223                 /* FALLTHROUGH */
1224         case USB_ST_SETUP:
1225                 pc = usbd_xfer_get_frame(xfer, 1);
1226                 if (ucom_get_data(&sc->sc_ucom[ht->ht_muxport], pc,
1227                     0, 32, &actlen)) {
1228
1229                         usbd_get_page(pc, 0, &res);
1230
1231                         memset(&req, 0, sizeof(struct usb_device_request));
1232                         req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
1233                         req.bRequest = UCDC_SEND_ENCAPSULATED_COMMAND;
1234                         USETW(req.wValue, 0);
1235                         USETW(req.wIndex, ht->ht_muxport);
1236                         USETW(req.wLength, actlen);
1237
1238                         pc = usbd_xfer_get_frame(xfer, 0);
1239                         usbd_copy_in(pc, 0, &req, sizeof(req));
1240
1241                         usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
1242                         usbd_xfer_set_frame_len(xfer, 1, actlen);
1243                         usbd_xfer_set_frames(xfer, 2);
1244
1245                         UHSO_DPRINTF(3, "Prepared %d bytes for transmit "
1246                             "on muxport %d\n", actlen, ht->ht_muxport);
1247
1248                         usbd_transfer_submit(xfer);
1249                 }
1250                 break;
1251         default:
1252                 UHSO_DPRINTF(0, "error: %s\n", usbd_errstr(error));
1253                 if (error == USB_ERR_CANCELLED)
1254                         break;
1255                 break;
1256         }
1257 }
1258
1259 static int
1260 uhso_attach_bulkserial(struct uhso_softc *sc, struct usb_interface *iface,
1261     int type)
1262 {
1263         usb_error_t uerr;
1264         int tty;
1265
1266         /* Try attaching RD/WR/INTR first */
1267         uerr = usbd_transfer_setup(sc->sc_udev,
1268             &iface->idesc->bInterfaceNumber, sc->sc_xfer,
1269             uhso_bs_config, UHSO_BULK_ENDPT_MAX, sc, &sc->sc_mtx);
1270         if (uerr) {
1271                 /* Try only RD/WR */
1272                 uerr = usbd_transfer_setup(sc->sc_udev,
1273                     &iface->idesc->bInterfaceNumber, sc->sc_xfer,
1274                     uhso_bs_config, UHSO_BULK_ENDPT_MAX - 1, sc, &sc->sc_mtx);
1275         }
1276         if (uerr) {
1277                 UHSO_DPRINTF(0, "usbd_transfer_setup failed");
1278                 return (-1);
1279         }
1280
1281         tty = uhso_alloc_tty(sc);
1282         if (tty < 0) {
1283                 usbd_transfer_unsetup(sc->sc_xfer, UHSO_BULK_ENDPT_MAX);
1284                 return (ENOMEM);
1285         }
1286
1287         sc->sc_tty[tty].ht_muxport = -1;
1288         return (0);
1289 }
1290
1291 static void
1292 uhso_bs_read_callback(struct usb_xfer *xfer, usb_error_t error)
1293 {
1294         struct uhso_softc *sc = usbd_xfer_softc(xfer);
1295         struct usb_page_cache *pc;
1296         int actlen;
1297
1298         usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
1299
1300         UHSO_DPRINTF(3, "status %d, actlen=%d\n", USB_GET_STATE(xfer), actlen);
1301
1302         switch (USB_GET_STATE(xfer)) {
1303         case USB_ST_TRANSFERRED:
1304                 pc = usbd_xfer_get_frame(xfer, 0);
1305                 ucom_put_data(&sc->sc_ucom[0], pc, 0, actlen);
1306                 /* FALLTHROUGH */
1307         case USB_ST_SETUP:
1308 tr_setup:
1309                 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
1310                 usbd_transfer_submit(xfer);
1311         break;
1312         default:
1313                 UHSO_DPRINTF(0, "error: %s\n", usbd_errstr(error));
1314                 if (error == USB_ERR_CANCELLED)
1315                         break;
1316                 usbd_xfer_set_stall(xfer);
1317                 goto tr_setup;
1318         }
1319 }
1320
1321 static void
1322 uhso_bs_write_callback(struct usb_xfer *xfer, usb_error_t error)
1323 {
1324         struct uhso_softc *sc = usbd_xfer_softc(xfer);
1325         struct usb_page_cache *pc;
1326         int actlen;
1327
1328         usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
1329
1330         UHSO_DPRINTF(3, "status %d, actlen=%d\n", USB_GET_STATE(xfer), actlen);
1331
1332         switch (USB_GET_STATE(xfer)) {
1333         case USB_ST_TRANSFERRED:
1334         case USB_ST_SETUP:
1335 tr_setup:
1336                 pc = usbd_xfer_get_frame(xfer, 0);
1337                 if (ucom_get_data(&sc->sc_ucom[0], pc, 0, 8192, &actlen)) {
1338                         usbd_xfer_set_frame_len(xfer, 0, actlen);
1339                         usbd_transfer_submit(xfer);
1340                 }
1341                 break;
1342         break;
1343         default:
1344                 UHSO_DPRINTF(0, "error: %s\n", usbd_errstr(error));
1345                 if (error == USB_ERR_CANCELLED)
1346                         break;
1347                 usbd_xfer_set_stall(xfer);
1348                 goto tr_setup;
1349         }
1350 }
1351
1352 static void
1353 uhso_bs_cfg(struct uhso_softc *sc)
1354 {
1355         struct usb_device_request req;
1356         usb_error_t uerr;
1357
1358         if (!(UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_BULK))
1359                 return;
1360
1361         req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
1362         req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
1363         USETW(req.wValue, sc->sc_line);
1364         USETW(req.wIndex, sc->sc_iface_no);
1365         USETW(req.wLength, 0);
1366
1367         uerr = ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom[0], &req, NULL, 0, 1000);
1368         if (uerr != 0) {
1369                 device_printf(sc->sc_dev, "failed to set ctrl line state to "
1370                     "0x%02x: %s\n", sc->sc_line, usbd_errstr(uerr));
1371         }
1372 }
1373
1374 static void
1375 uhso_bs_intr_callback(struct usb_xfer *xfer, usb_error_t error)
1376 {
1377         struct uhso_softc *sc = usbd_xfer_softc(xfer);
1378         struct usb_page_cache *pc;
1379         int actlen;
1380         struct usb_cdc_notification cdc;
1381
1382         usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
1383         UHSO_DPRINTF(3, "status %d, actlen=%d\n", USB_GET_STATE(xfer), actlen);
1384
1385         switch (USB_GET_STATE(xfer)) {
1386         case USB_ST_TRANSFERRED:
1387                 if (actlen < UCDC_NOTIFICATION_LENGTH) {
1388                         UHSO_DPRINTF(0, "UCDC notification too short: %d\n", actlen);
1389                         goto tr_setup;
1390                 }
1391                 else if (actlen > (int)sizeof(struct usb_cdc_notification)) {
1392                         UHSO_DPRINTF(0, "UCDC notification too large: %d\n", actlen);
1393                         actlen = sizeof(struct usb_cdc_notification);
1394                 }
1395
1396                 pc = usbd_xfer_get_frame(xfer, 0);
1397                 usbd_copy_out(pc, 0, &cdc, actlen);
1398
1399                 if (UGETW(cdc.wIndex) != sc->sc_iface_no) {
1400                         UHSO_DPRINTF(0, "Interface mismatch, got %d expected %d\n",
1401                             UGETW(cdc.wIndex), sc->sc_iface_no);
1402                         goto tr_setup;
1403                 }
1404
1405                 if (cdc.bmRequestType == UCDC_NOTIFICATION &&
1406                     cdc.bNotification == UCDC_N_SERIAL_STATE) {
1407                         UHSO_DPRINTF(2, "notify = 0x%02x\n", cdc.data[0]);
1408
1409                         sc->sc_msr = 0;
1410                         sc->sc_lsr = 0;
1411                         if (cdc.data[0] & UCDC_N_SERIAL_RI)
1412                                 sc->sc_msr |= SER_RI;
1413                         if (cdc.data[0] & UCDC_N_SERIAL_DSR)
1414                                 sc->sc_msr |= SER_DSR;  
1415                         if (cdc.data[0] & UCDC_N_SERIAL_DCD)
1416                                 sc->sc_msr |= SER_DCD;
1417
1418                         ucom_status_change(&sc->sc_ucom[0]);
1419                 }
1420         case USB_ST_SETUP:
1421 tr_setup:
1422         default:
1423                 if (error == USB_ERR_CANCELLED)
1424                         break;
1425                 usbd_xfer_set_stall(xfer);
1426                 goto tr_setup;
1427         }
1428 }
1429
1430 static void
1431 uhso_ucom_cfg_get_status(struct ucom_softc *ucom, uint8_t *lsr, uint8_t *msr)
1432 {
1433         struct uhso_softc *sc = ucom->sc_parent;
1434
1435         *lsr = sc->sc_lsr;
1436         *msr = sc->sc_msr;
1437 }
1438
1439 static void
1440 uhso_ucom_cfg_set_dtr(struct ucom_softc *ucom, uint8_t onoff)
1441 {
1442         struct uhso_softc *sc = ucom->sc_parent;
1443
1444         if (!(UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_BULK))
1445                 return;
1446
1447         if (onoff)
1448                 sc->sc_line |= UCDC_LINE_DTR;
1449         else
1450                 sc->sc_line &= ~UCDC_LINE_DTR;
1451
1452         uhso_bs_cfg(sc);
1453 }
1454
1455 static void
1456 uhso_ucom_cfg_set_rts(struct ucom_softc *ucom, uint8_t onoff)
1457 {
1458         struct uhso_softc *sc = ucom->sc_parent;
1459
1460         if (!(UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_BULK))
1461                 return;
1462
1463         if (onoff)
1464                 sc->sc_line |= UCDC_LINE_RTS;
1465         else
1466                 sc->sc_line &= ~UCDC_LINE_RTS;
1467
1468         uhso_bs_cfg(sc);
1469 }
1470
1471 static void
1472 uhso_ucom_start_read(struct ucom_softc *ucom)
1473 {
1474         struct uhso_softc *sc = ucom->sc_parent;
1475
1476         UHSO_DPRINTF(3, "unit=%d, subunit=%d\n",
1477             ucom->sc_super->sc_unit, ucom->sc_subunit);
1478
1479         if (UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_MUX) {
1480                 sc->sc_tty[ucom->sc_subunit].ht_open = 1;
1481                 usbd_transfer_start(sc->sc_xfer[UHSO_MUX_ENDPT_INTR]);
1482         }
1483         else if (UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_BULK) {
1484                 sc->sc_tty[0].ht_open = 1;
1485                 usbd_transfer_start(sc->sc_xfer[UHSO_BULK_ENDPT_READ]);
1486                 if (sc->sc_xfer[UHSO_BULK_ENDPT_INTR] != NULL)
1487                         usbd_transfer_start(sc->sc_xfer[UHSO_BULK_ENDPT_INTR]);
1488         }
1489 }
1490
1491 static void
1492 uhso_ucom_stop_read(struct ucom_softc *ucom)
1493 {
1494
1495         struct uhso_softc *sc = ucom->sc_parent;
1496
1497         if (UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_MUX) {
1498                 sc->sc_tty[ucom->sc_subunit].ht_open = 0;
1499                 usbd_transfer_stop(
1500                     sc->sc_tty[ucom->sc_subunit].ht_xfer[UHSO_CTRL_READ]);
1501         }
1502         else if (UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_BULK) {
1503                 sc->sc_tty[0].ht_open = 0;
1504                 usbd_transfer_start(sc->sc_xfer[UHSO_BULK_ENDPT_READ]);
1505                 if (sc->sc_xfer[UHSO_BULK_ENDPT_INTR] != NULL)
1506                         usbd_transfer_stop(sc->sc_xfer[UHSO_BULK_ENDPT_INTR]);
1507         }
1508 }
1509
1510 static void
1511 uhso_ucom_start_write(struct ucom_softc *ucom)
1512 {
1513         struct uhso_softc *sc = ucom->sc_parent;
1514
1515         if (UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_MUX) {
1516                 UHSO_DPRINTF(3, "local unit %d\n", ucom->sc_subunit);
1517
1518                 usbd_transfer_start(sc->sc_xfer[UHSO_MUX_ENDPT_INTR]);
1519
1520                 usbd_xfer_set_priv(
1521                     sc->sc_tty[ucom->sc_subunit].ht_xfer[UHSO_CTRL_WRITE],
1522                     &sc->sc_tty[ucom->sc_subunit]);
1523                 usbd_transfer_start(
1524                     sc->sc_tty[ucom->sc_subunit].ht_xfer[UHSO_CTRL_WRITE]);
1525
1526         }
1527         else if (UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_BULK) {
1528                 usbd_transfer_start(sc->sc_xfer[UHSO_BULK_ENDPT_WRITE]);
1529         }
1530 }
1531
1532 static void
1533 uhso_ucom_stop_write(struct ucom_softc *ucom)
1534 {
1535         struct uhso_softc *sc = ucom->sc_parent;
1536
1537         if (UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_MUX) {
1538                 usbd_transfer_stop(
1539                     sc->sc_tty[ucom->sc_subunit].ht_xfer[UHSO_CTRL_WRITE]);
1540         }
1541         else if (UHSO_IFACE_USB_TYPE(sc->sc_type) & UHSO_IF_BULK) {
1542                 usbd_transfer_stop(sc->sc_xfer[UHSO_BULK_ENDPT_WRITE]);
1543         }
1544 }
1545
1546 static int
1547 uhso_attach_ifnet(struct uhso_softc *sc, struct usb_interface *iface, int type)
1548 {
1549         struct ifnet *ifp;
1550         usb_error_t uerr;
1551         struct sysctl_ctx_list *sctx;
1552         struct sysctl_oid *soid;
1553         unsigned int devunit;
1554
1555         uerr = usbd_transfer_setup(sc->sc_udev,
1556             &iface->idesc->bInterfaceNumber, sc->sc_if_xfer,
1557             uhso_ifnet_config, UHSO_IFNET_MAX, sc, &sc->sc_mtx);
1558         if (uerr) {
1559                 UHSO_DPRINTF(0, "usbd_transfer_setup failed: %s\n",
1560                     usbd_errstr(uerr));
1561                 return (-1);
1562         }
1563
1564         sc->sc_ifp = ifp = if_alloc(IFT_OTHER);
1565         if (sc->sc_ifp == NULL) {
1566                 device_printf(sc->sc_dev, "if_alloc() failed\n");
1567                 return (-1);
1568         }
1569
1570         callout_init_mtx(&sc->sc_c, &sc->sc_mtx, 0);
1571         mtx_lock(&sc->sc_mtx);
1572         callout_reset(&sc->sc_c, 1, uhso_if_rxflush, sc);
1573         mtx_unlock(&sc->sc_mtx);
1574
1575         /*
1576          * We create our own unit numbers for ifnet devices because the
1577          * USB interface unit numbers can be at arbitrary positions yielding
1578          * odd looking device names.
1579          */
1580         devunit = alloc_unr(uhso_ifnet_unit);
1581
1582         if_initname(ifp, device_get_name(sc->sc_dev), devunit);
1583         ifp->if_mtu = UHSO_MAX_MTU;
1584         ifp->if_ioctl = uhso_if_ioctl;
1585         ifp->if_init = uhso_if_init;
1586         ifp->if_start = uhso_if_start;
1587         ifp->if_output = uhso_if_output;
1588         ifp->if_flags = IFF_BROADCAST | IFF_MULTICAST | IFF_NOARP;
1589         ifp->if_softc = sc;
1590         IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
1591         ifp->if_snd.ifq_drv_maxlen = ifqmaxlen;
1592         IFQ_SET_READY(&ifp->if_snd);
1593
1594         if_attach(ifp);
1595         bpfattach(ifp, DLT_RAW, 0);
1596
1597         sctx = device_get_sysctl_ctx(sc->sc_dev);
1598         soid = device_get_sysctl_tree(sc->sc_dev);
1599         /* Unlocked read... */
1600         SYSCTL_ADD_STRING(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "netif",
1601             CTLFLAG_RD, ifp->if_xname, 0, "Attached network interface");
1602
1603         return (0);
1604 }
1605
1606 static void
1607 uhso_ifnet_read_callback(struct usb_xfer *xfer, usb_error_t error)
1608 {
1609         struct uhso_softc *sc = usbd_xfer_softc(xfer);
1610         struct mbuf *m; 
1611         struct usb_page_cache *pc;
1612         int actlen;
1613
1614         usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
1615
1616         UHSO_DPRINTF(3, "status=%d, actlen=%d\n", USB_GET_STATE(xfer), actlen);
1617
1618         switch (USB_GET_STATE(xfer)) {
1619         case USB_ST_TRANSFERRED:
1620                 if (actlen > 0 && (sc->sc_ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1621                         pc = usbd_xfer_get_frame(xfer, 0);
1622                         m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
1623                         usbd_copy_out(pc, 0, mtod(m, uint8_t *), actlen);
1624                         m->m_pkthdr.len = m->m_len = actlen;
1625                         /* Enqueue frame for further processing */
1626                         _IF_ENQUEUE(&sc->sc_rxq, m);
1627                         if (!callout_pending(&sc->sc_c) ||
1628                             !callout_active(&sc->sc_c)) {
1629                                 callout_schedule(&sc->sc_c, 1);
1630                         }
1631                 }
1632         /* FALLTHROUGH */
1633         case USB_ST_SETUP:
1634 tr_setup:
1635                 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
1636                 usbd_transfer_submit(xfer);
1637                 break;
1638         default:
1639                 UHSO_DPRINTF(0, "error: %s\n", usbd_errstr(error));
1640                 if (error == USB_ERR_CANCELLED)
1641                         break;
1642                 usbd_xfer_set_stall(xfer);
1643                 goto tr_setup;
1644         }
1645 }
1646
1647 /*
1648  * Deferred RX processing, called with mutex locked.
1649  *
1650  * Each frame we receive might contain several small ip-packets as well
1651  * as partial ip-packets. We need to separate/assemble them into individual
1652  * packets before sending them to the ip-layer.
1653  */
1654 static void
1655 uhso_if_rxflush(void *arg)
1656 {
1657         struct uhso_softc *sc = arg;
1658         struct ifnet *ifp = sc->sc_ifp;
1659         uint8_t *cp;
1660         struct mbuf *m, *m0, *mwait;
1661         struct ip *ip;
1662 #ifdef INET6
1663         struct ip6_hdr *ip6;
1664 #endif
1665         uint16_t iplen;
1666         int len, isr;
1667
1668         m = NULL;
1669         mwait = sc->sc_mwait;
1670         for (;;) {
1671                 if (m == NULL) {
1672                         _IF_DEQUEUE(&sc->sc_rxq, m);
1673                         if (m == NULL)
1674                                 break;
1675                         UHSO_DPRINTF(3, "dequeue m=%p, len=%d\n", m, m->m_len);
1676                 }
1677                 mtx_unlock(&sc->sc_mtx);
1678
1679                 /* Do we have a partial packet waiting? */
1680                 if (mwait != NULL) {
1681                         m0 = mwait;
1682                         mwait = NULL;
1683
1684                         UHSO_DPRINTF(3, "partial m0=%p(%d), concat w/ m=%p(%d)\n",
1685                             m0, m0->m_len, m, m->m_len);
1686                         len = m->m_len + m0->m_len;
1687
1688                         /* Concat mbufs and fix headers */
1689                         m_cat(m0, m);
1690                         m0->m_pkthdr.len = len;
1691                         m->m_flags &= ~M_PKTHDR;
1692
1693                         m = m_pullup(m0, sizeof(struct ip));
1694                         if (m == NULL) {
1695                                 ifp->if_ierrors++;
1696                                 UHSO_DPRINTF(0, "m_pullup failed\n");
1697                                 mtx_lock(&sc->sc_mtx);
1698                                 continue;
1699                         }
1700                         UHSO_DPRINTF(3, "Constructed mbuf=%p, len=%d\n",
1701                             m, m->m_pkthdr.len);
1702                 }
1703
1704                 cp = mtod(m, uint8_t *);
1705                 ip = (struct ip *)cp;
1706 #ifdef INET6
1707                 ip6 = (struct ip6_hdr *)cp;
1708 #endif
1709
1710                 /* Check for IPv4 */
1711                 if (ip->ip_v == IPVERSION) {
1712                         iplen = htons(ip->ip_len);
1713                         isr = NETISR_IP;
1714                 }
1715 #ifdef INET6
1716                 /* Check for IPv6 */
1717                 else if ((ip6->ip6_vfc & IPV6_VERSION_MASK) == IPV6_VERSION) {
1718                         iplen = htons(ip6->ip6_plen);
1719                         isr = NETISR_IPV6;
1720                 }
1721 #endif
1722                 else {
1723                         UHSO_DPRINTF(0, "got unexpected ip version %d, "
1724                             "m=%p, len=%d\n", (*cp & 0xf0) >> 4, m, m->m_len);
1725                         ifp->if_ierrors++;
1726                         UHSO_HEXDUMP(cp, 4);
1727                         m_freem(m);
1728                         m = NULL;
1729                         mtx_lock(&sc->sc_mtx);
1730                         continue;
1731                 }
1732
1733                 if (iplen == 0) {
1734                         UHSO_DPRINTF(0, "Zero IP length\n");
1735                         ifp->if_ierrors++;
1736                         m_freem(m);
1737                         m = NULL;
1738                         mtx_lock(&sc->sc_mtx);
1739                         continue;
1740                 }
1741
1742                 UHSO_DPRINTF(3, "m=%p, len=%d, cp=%p, iplen=%d\n",
1743                     m, m->m_pkthdr.len, cp, iplen);
1744
1745                 m0 = NULL;
1746
1747                 /* More IP packets in this mbuf */
1748                 if (iplen < m->m_pkthdr.len) {
1749                         m0 = m;
1750
1751                         /*
1752                          * Allocate a new mbuf for this IP packet and
1753                          * copy the IP-packet into it.
1754                          */
1755                         m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
1756                         memcpy(mtod(m, uint8_t *), mtod(m0, uint8_t *), iplen);
1757                         m->m_pkthdr.len = m->m_len = iplen;
1758
1759                         /* Adjust the size of the original mbuf */
1760                         m_adj(m0, iplen);
1761                         m0 = m_defrag(m0, M_WAITOK);
1762
1763                         UHSO_DPRINTF(3, "New mbuf=%p, len=%d/%d, m0=%p, "
1764                             "m0_len=%d/%d\n", m, m->m_pkthdr.len, m->m_len,
1765                             m0, m0->m_pkthdr.len, m0->m_len);
1766                 }
1767                 else if (iplen > m->m_pkthdr.len) {
1768                         UHSO_DPRINTF(3, "Deferred mbuf=%p, len=%d\n",
1769                             m, m->m_pkthdr.len);
1770                         mwait = m;
1771                         m = NULL;
1772                         mtx_lock(&sc->sc_mtx);
1773                         continue;
1774                 }
1775
1776                 ifp->if_ipackets++;
1777                 m->m_pkthdr.rcvif = ifp;
1778
1779                 /* Dispatch to IP layer */
1780                 BPF_MTAP(sc->sc_ifp, m);
1781                 M_SETFIB(m, ifp->if_fib);
1782                 netisr_dispatch(isr, m);
1783                 m = m0 != NULL ? m0 : NULL;
1784                 mtx_lock(&sc->sc_mtx);
1785         }
1786         sc->sc_mwait = mwait;
1787 }
1788
1789 static void
1790 uhso_ifnet_write_callback(struct usb_xfer *xfer, usb_error_t error)
1791 {
1792         struct uhso_softc *sc = usbd_xfer_softc(xfer);
1793         struct ifnet *ifp = sc->sc_ifp;
1794         struct usb_page_cache *pc;
1795         struct mbuf *m;
1796         int actlen;
1797
1798         usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
1799
1800         UHSO_DPRINTF(3, "status %d, actlen=%d\n", USB_GET_STATE(xfer), actlen);
1801
1802         switch (USB_GET_STATE(xfer)) {
1803         case USB_ST_TRANSFERRED:
1804                 ifp->if_opackets++;
1805                 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1806         case USB_ST_SETUP:
1807 tr_setup:
1808                 IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
1809                 if (m == NULL)
1810                         break;
1811
1812                 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1813
1814                 if (m->m_pkthdr.len > MCLBYTES)
1815                         m->m_pkthdr.len = MCLBYTES;
1816
1817                 usbd_xfer_set_frame_len(xfer, 0, m->m_pkthdr.len);
1818                 pc = usbd_xfer_get_frame(xfer, 0);
1819                 usbd_m_copy_in(pc, 0, m, 0, m->m_pkthdr.len);
1820                 usbd_transfer_submit(xfer);
1821
1822                 BPF_MTAP(ifp, m);
1823                 m_freem(m);
1824                 break;
1825         default:
1826                 UHSO_DPRINTF(0, "error: %s\n", usbd_errstr(error));
1827                 if (error == USB_ERR_CANCELLED)
1828                         break;
1829                 usbd_xfer_set_stall(xfer);
1830                 goto tr_setup;
1831         }
1832 }
1833
1834 static int
1835 uhso_if_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1836 {
1837         struct uhso_softc *sc;
1838
1839         sc = ifp->if_softc;
1840
1841         switch (cmd) {
1842         case SIOCSIFFLAGS:
1843                 if (ifp->if_flags & IFF_UP) {
1844                         if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1845                                 uhso_if_init(sc);
1846                         }
1847                 }
1848                 else {
1849                         if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1850                                 mtx_lock(&sc->sc_mtx);
1851                                 uhso_if_stop(sc);
1852                                 mtx_unlock(&sc->sc_mtx);
1853                         }
1854                 }
1855                 break;
1856         case SIOCSIFADDR:
1857         case SIOCADDMULTI:
1858         case SIOCDELMULTI:
1859                 break;
1860         default:
1861                 return (EINVAL);
1862         }
1863         return (0);
1864 }
1865
1866 static void
1867 uhso_if_init(void *priv)
1868 {
1869         struct uhso_softc *sc = priv;
1870         struct ifnet *ifp = sc->sc_ifp;
1871
1872         mtx_lock(&sc->sc_mtx);
1873         uhso_if_stop(sc);
1874         ifp = sc->sc_ifp;
1875         ifp->if_flags |= IFF_UP;
1876         ifp->if_drv_flags |= IFF_DRV_RUNNING;
1877         mtx_unlock(&sc->sc_mtx);
1878
1879         UHSO_DPRINTF(2, "ifnet initialized\n");
1880 }
1881
1882 static int
1883 uhso_if_output(struct ifnet *ifp, struct mbuf *m0, const struct sockaddr *dst,
1884     struct route *ro)
1885 {
1886         int error;
1887
1888         /* Only IPv4/6 support */
1889         if (dst->sa_family != AF_INET
1890 #ifdef INET6
1891            && dst->sa_family != AF_INET6
1892 #endif
1893          ) {
1894                 return (EAFNOSUPPORT);
1895         }
1896
1897         error = (ifp->if_transmit)(ifp, m0);
1898         if (error) {
1899                 ifp->if_oerrors++;
1900                 return (ENOBUFS);
1901         }
1902         ifp->if_opackets++;
1903         return (0);
1904 }
1905
1906 static void
1907 uhso_if_start(struct ifnet *ifp)
1908 {
1909         struct uhso_softc *sc = ifp->if_softc;
1910
1911         if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
1912                 UHSO_DPRINTF(1, "Not running\n");
1913                 return;
1914         }
1915
1916         mtx_lock(&sc->sc_mtx);
1917         usbd_transfer_start(sc->sc_if_xfer[UHSO_IFNET_READ]);
1918         usbd_transfer_start(sc->sc_if_xfer[UHSO_IFNET_WRITE]);
1919         mtx_unlock(&sc->sc_mtx);
1920         UHSO_DPRINTF(3, "interface started\n");
1921 }
1922
1923 static void
1924 uhso_if_stop(struct uhso_softc *sc)
1925 {
1926
1927         usbd_transfer_stop(sc->sc_if_xfer[UHSO_IFNET_READ]);
1928         usbd_transfer_stop(sc->sc_if_xfer[UHSO_IFNET_WRITE]);
1929         sc->sc_ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
1930 }