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