1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 2011 Anybots Inc
5 * written by Akinori Furukoshi <[email protected]>
6 * - ucom part is based on u3g.c
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32
33 #include <sys/param.h>
34 #include <sys/eventhandler.h>
35 #include <sys/systm.h>
36 #include <sys/queue.h>
37 #include <sys/systm.h>
38 #include <sys/socket.h>
39 #include <sys/kernel.h>
40 #include <sys/bus.h>
41 #include <sys/module.h>
42 #include <sys/sockio.h>
43 #include <sys/socket.h>
44 #include <sys/lock.h>
45 #include <sys/mutex.h>
46 #include <sys/condvar.h>
47 #include <sys/sysctl.h>
48 #include <sys/malloc.h>
49 #include <sys/taskqueue.h>
50
51 #include <net/if.h>
52 #include <net/if_var.h>
53
54 #include <machine/bus.h>
55
56 #include <net/if.h>
57 #include <net/if_types.h>
58 #include <net/netisr.h>
59 #include <net/bpf.h>
60 #include <net/ethernet.h>
61
62 #include <netinet/in.h>
63 #include <netinet/ip.h>
64 #include <netinet/ip6.h>
65 #include <netinet/udp.h>
66
67 #include <net80211/ieee80211_ioctl.h>
68
69 #include <dev/usb/usb.h>
70 #include <dev/usb/usbdi.h>
71 #include <dev/usb/usbdi_util.h>
72 #include <dev/usb/usb_cdc.h>
73 #include "usbdevs.h"
74
75 #define USB_DEBUG_VAR usie_debug
76 #include <dev/usb/usb_debug.h>
77 #include <dev/usb/usb_process.h>
78 #include <dev/usb/usb_msctest.h>
79
80 #include <dev/usb/serial/usb_serial.h>
81
82 #include <dev/usb/net/if_usievar.h>
83
84 #ifdef USB_DEBUG
85 static int usie_debug = 0;
86
87 static SYSCTL_NODE(_hw_usb, OID_AUTO, usie, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
88 "sierra USB modem");
89 SYSCTL_INT(_hw_usb_usie, OID_AUTO, debug, CTLFLAG_RWTUN, &usie_debug, 0,
90 "usie debug level");
91 #endif
92
93 /* Sierra Wireless Direct IP modems */
94 static const STRUCT_USB_HOST_ID usie_devs[] = {
95 #define USIE_DEV(v, d) { \
96 USB_VP(USB_VENDOR_##v, USB_PRODUCT_##v##_##d) }
97 USIE_DEV(SIERRA, MC8700),
98 USIE_DEV(SIERRA, TRUINSTALL),
99 USIE_DEV(AIRPRIME, USB308),
100 #undef USIE_DEV
101 };
102
103 static device_probe_t usie_probe;
104 static device_attach_t usie_attach;
105 static device_detach_t usie_detach;
106 static void usie_free_softc(struct usie_softc *);
107
108 static void usie_free(struct ucom_softc *);
109 static void usie_uc_update_line_state(struct ucom_softc *, uint8_t);
110 static void usie_uc_cfg_get_status(struct ucom_softc *, uint8_t *, uint8_t *);
111 static void usie_uc_cfg_set_dtr(struct ucom_softc *, uint8_t);
112 static void usie_uc_cfg_set_rts(struct ucom_softc *, uint8_t);
113 static void usie_uc_cfg_open(struct ucom_softc *);
114 static void usie_uc_cfg_close(struct ucom_softc *);
115 static void usie_uc_start_read(struct ucom_softc *);
116 static void usie_uc_stop_read(struct ucom_softc *);
117 static void usie_uc_start_write(struct ucom_softc *);
118 static void usie_uc_stop_write(struct ucom_softc *);
119
120 static usb_callback_t usie_uc_tx_callback;
121 static usb_callback_t usie_uc_rx_callback;
122 static usb_callback_t usie_uc_status_callback;
123 static usb_callback_t usie_if_tx_callback;
124 static usb_callback_t usie_if_rx_callback;
125 static usb_callback_t usie_if_status_callback;
126
127 static void usie_if_sync_to(void *);
128 static void usie_if_sync_cb(void *, int);
129 static void usie_if_status_cb(void *, int);
130
131 static void usie_if_start(struct ifnet *);
132 static int usie_if_output(struct ifnet *, struct mbuf *,
133 const struct sockaddr *, struct route *);
134 static void usie_if_init(void *);
135 static void usie_if_stop(struct usie_softc *);
136 static int usie_if_ioctl(struct ifnet *, u_long, caddr_t);
137
138 static int usie_do_request(struct usie_softc *, struct usb_device_request *, void *);
139 static int usie_if_cmd(struct usie_softc *, uint8_t);
140 static void usie_cns_req(struct usie_softc *, uint32_t, uint16_t);
141 static void usie_cns_rsp(struct usie_softc *, struct usie_cns *);
142 static void usie_hip_rsp(struct usie_softc *, uint8_t *, uint32_t);
143 static int usie_driver_loaded(struct module *, int, void *);
144
145 static const struct usb_config usie_uc_config[USIE_UC_N_XFER] = {
146 [USIE_UC_STATUS] = {
147 .type = UE_INTERRUPT,
148 .endpoint = UE_ADDR_ANY,
149 .direction = UE_DIR_IN,
150 .bufsize = 0, /* use wMaxPacketSize */
151 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
152 .callback = &usie_uc_status_callback,
153 },
154 [USIE_UC_RX] = {
155 .type = UE_BULK,
156 .endpoint = UE_ADDR_ANY,
157 .direction = UE_DIR_IN,
158 .bufsize = USIE_BUFSIZE,
159 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,.proxy_buffer = 1,},
160 .callback = &usie_uc_rx_callback,
161 },
162 [USIE_UC_TX] = {
163 .type = UE_BULK,
164 .endpoint = UE_ADDR_ANY,
165 .direction = UE_DIR_OUT,
166 .bufsize = USIE_BUFSIZE,
167 .flags = {.pipe_bof = 1,.force_short_xfer = 1,},
168 .callback = &usie_uc_tx_callback,
169 }
170 };
171
172 static const struct usb_config usie_if_config[USIE_IF_N_XFER] = {
173 [USIE_IF_STATUS] = {
174 .type = UE_INTERRUPT,
175 .endpoint = UE_ADDR_ANY,
176 .direction = UE_DIR_IN,
177 .bufsize = 0, /* use wMaxPacketSize */
178 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
179 .callback = &usie_if_status_callback,
180 },
181 [USIE_IF_RX] = {
182 .type = UE_BULK,
183 .endpoint = UE_ADDR_ANY,
184 .direction = UE_DIR_IN,
185 .bufsize = USIE_BUFSIZE,
186 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
187 .callback = &usie_if_rx_callback,
188 },
189 [USIE_IF_TX] = {
190 .type = UE_BULK,
191 .endpoint = UE_ADDR_ANY,
192 .direction = UE_DIR_OUT,
193 .bufsize = MAX(USIE_BUFSIZE, MCLBYTES),
194 .flags = {.pipe_bof = 1,.force_short_xfer = 1,},
195 .callback = &usie_if_tx_callback,
196 }
197 };
198
199 static device_method_t usie_methods[] = {
200 DEVMETHOD(device_probe, usie_probe),
201 DEVMETHOD(device_attach, usie_attach),
202 DEVMETHOD(device_detach, usie_detach),
203 DEVMETHOD_END
204 };
205
206 static driver_t usie_driver = {
207 .name = "usie",
208 .methods = usie_methods,
209 .size = sizeof(struct usie_softc),
210 };
211
212 static devclass_t usie_devclass;
213 static eventhandler_tag usie_etag;
214
215 DRIVER_MODULE(usie, uhub, usie_driver, usie_devclass, usie_driver_loaded, 0);
216 MODULE_DEPEND(usie, ucom, 1, 1, 1);
217 MODULE_DEPEND(usie, usb, 1, 1, 1);
218 MODULE_VERSION(usie, 1);
219 USB_PNP_HOST_INFO(usie_devs);
220
221 static const struct ucom_callback usie_uc_callback = {
222 .ucom_cfg_get_status = &usie_uc_cfg_get_status,
223 .ucom_cfg_set_dtr = &usie_uc_cfg_set_dtr,
224 .ucom_cfg_set_rts = &usie_uc_cfg_set_rts,
225 .ucom_cfg_open = &usie_uc_cfg_open,
226 .ucom_cfg_close = &usie_uc_cfg_close,
227 .ucom_start_read = &usie_uc_start_read,
228 .ucom_stop_read = &usie_uc_stop_read,
229 .ucom_start_write = &usie_uc_start_write,
230 .ucom_stop_write = &usie_uc_stop_write,
231 .ucom_free = &usie_free,
232 };
233
234 static void
usie_autoinst(void * arg,struct usb_device * udev,struct usb_attach_arg * uaa)235 usie_autoinst(void *arg, struct usb_device *udev,
236 struct usb_attach_arg *uaa)
237 {
238 struct usb_interface *iface;
239 struct usb_interface_descriptor *id;
240 struct usb_device_request req;
241 int err;
242
243 if (uaa->dev_state != UAA_DEV_READY)
244 return;
245
246 iface = usbd_get_iface(udev, 0);
247 if (iface == NULL)
248 return;
249
250 id = iface->idesc;
251 if (id == NULL || id->bInterfaceClass != UICLASS_MASS)
252 return;
253
254 if (usbd_lookup_id_by_uaa(usie_devs, sizeof(usie_devs), uaa) != 0)
255 return; /* no device match */
256
257 if (bootverbose) {
258 DPRINTF("Ejecting %s %s\n",
259 usb_get_manufacturer(udev),
260 usb_get_product(udev));
261 }
262 req.bmRequestType = UT_VENDOR;
263 req.bRequest = UR_SET_INTERFACE;
264 USETW(req.wValue, UF_DEVICE_REMOTE_WAKEUP);
265 USETW(req.wIndex, UHF_PORT_CONNECTION);
266 USETW(req.wLength, 0);
267
268 /* at this moment there is no mutex */
269 err = usbd_do_request_flags(udev, NULL, &req,
270 NULL, 0, NULL, 250 /* ms */ );
271
272 /* success, mark the udev as disappearing */
273 if (err == 0)
274 uaa->dev_state = UAA_DEV_EJECTING;
275 }
276
277 static int
usie_probe(device_t self)278 usie_probe(device_t self)
279 {
280 struct usb_attach_arg *uaa = device_get_ivars(self);
281
282 if (uaa->usb_mode != USB_MODE_HOST)
283 return (ENXIO);
284 if (uaa->info.bConfigIndex != USIE_CNFG_INDEX)
285 return (ENXIO);
286 if (uaa->info.bIfaceIndex != USIE_IFACE_INDEX)
287 return (ENXIO);
288 if (uaa->info.bInterfaceClass != UICLASS_VENDOR)
289 return (ENXIO);
290
291 return (usbd_lookup_id_by_uaa(usie_devs, sizeof(usie_devs), uaa));
292 }
293
294 static int
usie_attach(device_t self)295 usie_attach(device_t self)
296 {
297 struct usie_softc *sc = device_get_softc(self);
298 struct usb_attach_arg *uaa = device_get_ivars(self);
299 struct ifnet *ifp;
300 struct usb_interface *iface;
301 struct usb_interface_descriptor *id;
302 struct usb_device_request req;
303 int err;
304 uint16_t fwattr;
305 uint8_t iface_index;
306 uint8_t ifidx;
307 uint8_t start;
308
309 device_set_usb_desc(self);
310 sc->sc_udev = uaa->device;
311 sc->sc_dev = self;
312
313 mtx_init(&sc->sc_mtx, "usie", MTX_NETWORK_LOCK, MTX_DEF);
314 ucom_ref(&sc->sc_super_ucom);
315
316 TASK_INIT(&sc->sc_if_status_task, 0, usie_if_status_cb, sc);
317 TASK_INIT(&sc->sc_if_sync_task, 0, usie_if_sync_cb, sc);
318
319 usb_callout_init_mtx(&sc->sc_if_sync_ch, &sc->sc_mtx, 0);
320
321 mtx_lock(&sc->sc_mtx);
322
323 /* set power mode to D0 */
324 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
325 req.bRequest = USIE_POWER;
326 USETW(req.wValue, 0);
327 USETW(req.wIndex, 0);
328 USETW(req.wLength, 0);
329 if (usie_do_request(sc, &req, NULL)) {
330 mtx_unlock(&sc->sc_mtx);
331 goto detach;
332 }
333 /* read fw attr */
334 fwattr = 0;
335 req.bmRequestType = UT_READ_VENDOR_DEVICE;
336 req.bRequest = USIE_FW_ATTR;
337 USETW(req.wValue, 0);
338 USETW(req.wIndex, 0);
339 USETW(req.wLength, sizeof(fwattr));
340 if (usie_do_request(sc, &req, &fwattr)) {
341 mtx_unlock(&sc->sc_mtx);
342 goto detach;
343 }
344 mtx_unlock(&sc->sc_mtx);
345
346 /* check DHCP supports */
347 DPRINTF("fwattr=%x\n", fwattr);
348 if (!(fwattr & USIE_FW_DHCP)) {
349 device_printf(self, "DHCP is not supported. A firmware upgrade might be needed.\n");
350 }
351
352 /* find available interfaces */
353 sc->sc_nucom = 0;
354 for (ifidx = 0; ifidx < USIE_IFACE_MAX; ifidx++) {
355 iface = usbd_get_iface(uaa->device, ifidx);
356 if (iface == NULL)
357 break;
358
359 id = usbd_get_interface_descriptor(iface);
360 if ((id == NULL) || (id->bInterfaceClass != UICLASS_VENDOR))
361 continue;
362
363 /* setup Direct IP transfer */
364 if (id->bInterfaceNumber >= 7 && id->bNumEndpoints == 3) {
365 sc->sc_if_ifnum = id->bInterfaceNumber;
366 iface_index = ifidx;
367
368 DPRINTF("ifnum=%d, ifidx=%d\n",
369 sc->sc_if_ifnum, ifidx);
370
371 err = usbd_transfer_setup(uaa->device,
372 &iface_index, sc->sc_if_xfer, usie_if_config,
373 USIE_IF_N_XFER, sc, &sc->sc_mtx);
374
375 if (err == 0)
376 continue;
377
378 device_printf(self,
379 "could not allocate USB transfers on "
380 "iface_index=%d, err=%s\n",
381 iface_index, usbd_errstr(err));
382 goto detach;
383 }
384
385 /* setup ucom */
386 if (sc->sc_nucom >= USIE_UCOM_MAX)
387 continue;
388
389 usbd_set_parent_iface(uaa->device, ifidx,
390 uaa->info.bIfaceIndex);
391
392 DPRINTF("NumEndpoints=%d bInterfaceNumber=%d\n",
393 id->bNumEndpoints, id->bInterfaceNumber);
394
395 if (id->bNumEndpoints == 2) {
396 sc->sc_uc_xfer[sc->sc_nucom][0] = NULL;
397 start = 1;
398 } else
399 start = 0;
400
401 err = usbd_transfer_setup(uaa->device, &ifidx,
402 sc->sc_uc_xfer[sc->sc_nucom] + start,
403 usie_uc_config + start, USIE_UC_N_XFER - start,
404 &sc->sc_ucom[sc->sc_nucom], &sc->sc_mtx);
405
406 if (err != 0) {
407 DPRINTF("usbd_transfer_setup error=%s\n", usbd_errstr(err));
408 continue;
409 }
410
411 mtx_lock(&sc->sc_mtx);
412 for (; start < USIE_UC_N_XFER; start++)
413 usbd_xfer_set_stall(sc->sc_uc_xfer[sc->sc_nucom][start]);
414 mtx_unlock(&sc->sc_mtx);
415
416 sc->sc_uc_ifnum[sc->sc_nucom] = id->bInterfaceNumber;
417
418 sc->sc_nucom++; /* found a port */
419 }
420
421 if (sc->sc_nucom == 0) {
422 device_printf(self, "no comports found\n");
423 goto detach;
424 }
425
426 err = ucom_attach(&sc->sc_super_ucom, sc->sc_ucom,
427 sc->sc_nucom, sc, &usie_uc_callback, &sc->sc_mtx);
428
429 if (err != 0) {
430 DPRINTF("ucom_attach failed\n");
431 goto detach;
432 }
433 DPRINTF("Found %d interfaces.\n", sc->sc_nucom);
434
435 /* setup ifnet (Direct IP) */
436 sc->sc_ifp = ifp = if_alloc(IFT_OTHER);
437
438 if (ifp == NULL) {
439 device_printf(self, "Could not allocate a network interface\n");
440 goto detach;
441 }
442 if_initname(ifp, "usie", device_get_unit(self));
443
444 ifp->if_softc = sc;
445 ifp->if_mtu = USIE_MTU_MAX;
446 ifp->if_flags |= IFF_NOARP;
447 ifp->if_init = usie_if_init;
448 ifp->if_ioctl = usie_if_ioctl;
449 ifp->if_start = usie_if_start;
450 ifp->if_output = usie_if_output;
451 IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
452 ifp->if_snd.ifq_drv_maxlen = ifqmaxlen;
453 IFQ_SET_READY(&ifp->if_snd);
454
455 if_attach(ifp);
456 bpfattach(ifp, DLT_RAW, 0);
457
458 if (fwattr & USIE_PM_AUTO) {
459 usbd_set_power_mode(uaa->device, USB_POWER_MODE_SAVE);
460 DPRINTF("enabling automatic suspend and resume\n");
461 } else {
462 usbd_set_power_mode(uaa->device, USB_POWER_MODE_ON);
463 DPRINTF("USB power is always ON\n");
464 }
465
466 DPRINTF("device attached\n");
467 return (0);
468
469 detach:
470 usie_detach(self);
471 return (ENOMEM);
472 }
473
474 static int
usie_detach(device_t self)475 usie_detach(device_t self)
476 {
477 struct usie_softc *sc = device_get_softc(self);
478 uint8_t x;
479
480 /* detach ifnet */
481 if (sc->sc_ifp != NULL) {
482 usie_if_stop(sc);
483 usbd_transfer_unsetup(sc->sc_if_xfer, USIE_IF_N_XFER);
484 bpfdetach(sc->sc_ifp);
485 if_detach(sc->sc_ifp);
486 if_free(sc->sc_ifp);
487 sc->sc_ifp = NULL;
488 }
489 /* detach ucom */
490 if (sc->sc_nucom > 0)
491 ucom_detach(&sc->sc_super_ucom, sc->sc_ucom);
492
493 /* stop all USB transfers */
494 usbd_transfer_unsetup(sc->sc_if_xfer, USIE_IF_N_XFER);
495
496 for (x = 0; x != USIE_UCOM_MAX; x++)
497 usbd_transfer_unsetup(sc->sc_uc_xfer[x], USIE_UC_N_XFER);
498
499 device_claim_softc(self);
500
501 usie_free_softc(sc);
502
503 return (0);
504 }
505
506 UCOM_UNLOAD_DRAIN(usie);
507
508 static void
usie_free_softc(struct usie_softc * sc)509 usie_free_softc(struct usie_softc *sc)
510 {
511 if (ucom_unref(&sc->sc_super_ucom)) {
512 mtx_destroy(&sc->sc_mtx);
513 device_free_softc(sc);
514 }
515 }
516
517 static void
usie_free(struct ucom_softc * ucom)518 usie_free(struct ucom_softc *ucom)
519 {
520 usie_free_softc(ucom->sc_parent);
521 }
522
523 static void
usie_uc_update_line_state(struct ucom_softc * ucom,uint8_t ls)524 usie_uc_update_line_state(struct ucom_softc *ucom, uint8_t ls)
525 {
526 struct usie_softc *sc = ucom->sc_parent;
527 struct usb_device_request req;
528
529 if (sc->sc_uc_xfer[ucom->sc_subunit][USIE_UC_STATUS] == NULL)
530 return;
531
532 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
533 req.bRequest = USIE_LINK_STATE;
534 USETW(req.wValue, ls);
535 USETW(req.wIndex, sc->sc_uc_ifnum[ucom->sc_subunit]);
536 USETW(req.wLength, 0);
537
538 DPRINTF("sc_uc_ifnum=%d\n", sc->sc_uc_ifnum[ucom->sc_subunit]);
539
540 usie_do_request(sc, &req, NULL);
541 }
542
543 static void
usie_uc_cfg_get_status(struct ucom_softc * ucom,uint8_t * lsr,uint8_t * msr)544 usie_uc_cfg_get_status(struct ucom_softc *ucom, uint8_t *lsr, uint8_t *msr)
545 {
546 struct usie_softc *sc = ucom->sc_parent;
547
548 *msr = sc->sc_msr;
549 *lsr = sc->sc_lsr;
550 }
551
552 static void
usie_uc_cfg_set_dtr(struct ucom_softc * ucom,uint8_t flag)553 usie_uc_cfg_set_dtr(struct ucom_softc *ucom, uint8_t flag)
554 {
555 uint8_t dtr;
556
557 dtr = flag ? USIE_LS_DTR : 0;
558 usie_uc_update_line_state(ucom, dtr);
559 }
560
561 static void
usie_uc_cfg_set_rts(struct ucom_softc * ucom,uint8_t flag)562 usie_uc_cfg_set_rts(struct ucom_softc *ucom, uint8_t flag)
563 {
564 uint8_t rts;
565
566 rts = flag ? USIE_LS_RTS : 0;
567 usie_uc_update_line_state(ucom, rts);
568 }
569
570 static void
usie_uc_cfg_open(struct ucom_softc * ucom)571 usie_uc_cfg_open(struct ucom_softc *ucom)
572 {
573 struct usie_softc *sc = ucom->sc_parent;
574
575 /* usbd_transfer_start() is NULL safe */
576
577 usbd_transfer_start(sc->sc_uc_xfer[ucom->sc_subunit][USIE_UC_STATUS]);
578 }
579
580 static void
usie_uc_cfg_close(struct ucom_softc * ucom)581 usie_uc_cfg_close(struct ucom_softc *ucom)
582 {
583 struct usie_softc *sc = ucom->sc_parent;
584
585 usbd_transfer_stop(sc->sc_uc_xfer[ucom->sc_subunit][USIE_UC_STATUS]);
586 }
587
588 static void
usie_uc_start_read(struct ucom_softc * ucom)589 usie_uc_start_read(struct ucom_softc *ucom)
590 {
591 struct usie_softc *sc = ucom->sc_parent;
592
593 usbd_transfer_start(sc->sc_uc_xfer[ucom->sc_subunit][USIE_UC_RX]);
594 }
595
596 static void
usie_uc_stop_read(struct ucom_softc * ucom)597 usie_uc_stop_read(struct ucom_softc *ucom)
598 {
599 struct usie_softc *sc = ucom->sc_parent;
600
601 usbd_transfer_stop(sc->sc_uc_xfer[ucom->sc_subunit][USIE_UC_RX]);
602 }
603
604 static void
usie_uc_start_write(struct ucom_softc * ucom)605 usie_uc_start_write(struct ucom_softc *ucom)
606 {
607 struct usie_softc *sc = ucom->sc_parent;
608
609 usbd_transfer_start(sc->sc_uc_xfer[ucom->sc_subunit][USIE_UC_TX]);
610 }
611
612 static void
usie_uc_stop_write(struct ucom_softc * ucom)613 usie_uc_stop_write(struct ucom_softc *ucom)
614 {
615 struct usie_softc *sc = ucom->sc_parent;
616
617 usbd_transfer_stop(sc->sc_uc_xfer[ucom->sc_subunit][USIE_UC_TX]);
618 }
619
620 static void
usie_uc_rx_callback(struct usb_xfer * xfer,usb_error_t error)621 usie_uc_rx_callback(struct usb_xfer *xfer, usb_error_t error)
622 {
623 struct ucom_softc *ucom = usbd_xfer_softc(xfer);
624 struct usie_softc *sc = ucom->sc_parent;
625 struct usb_page_cache *pc;
626 uint32_t actlen;
627
628 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
629
630 switch (USB_GET_STATE(xfer)) {
631 case USB_ST_TRANSFERRED:
632 pc = usbd_xfer_get_frame(xfer, 0);
633
634 /* handle CnS response */
635 if (ucom == sc->sc_ucom && actlen >= USIE_HIPCNS_MIN) {
636 DPRINTF("transferred=%u\n", actlen);
637
638 /* check if it is really CnS reply */
639 usbd_copy_out(pc, 0, sc->sc_resp_temp, 1);
640
641 if (sc->sc_resp_temp[0] == USIE_HIP_FRM_CHR) {
642 /* verify actlen */
643 if (actlen > USIE_BUFSIZE)
644 actlen = USIE_BUFSIZE;
645
646 /* get complete message */
647 usbd_copy_out(pc, 0, sc->sc_resp_temp, actlen);
648 usie_hip_rsp(sc, sc->sc_resp_temp, actlen);
649
650 /* need to fall though */
651 goto tr_setup;
652 }
653 /* else call ucom_put_data() */
654 }
655 /* standard ucom transfer */
656 ucom_put_data(ucom, pc, 0, actlen);
657
658 /* fall though */
659 case USB_ST_SETUP:
660 tr_setup:
661 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
662 usbd_transfer_submit(xfer);
663 break;
664
665 default: /* Error */
666 if (error != USB_ERR_CANCELLED) {
667 usbd_xfer_set_stall(xfer);
668 goto tr_setup;
669 }
670 break;
671 }
672 }
673
674 static void
usie_uc_tx_callback(struct usb_xfer * xfer,usb_error_t error)675 usie_uc_tx_callback(struct usb_xfer *xfer, usb_error_t error)
676 {
677 struct ucom_softc *ucom = usbd_xfer_softc(xfer);
678 struct usb_page_cache *pc;
679 uint32_t actlen;
680
681 switch (USB_GET_STATE(xfer)) {
682 case USB_ST_TRANSFERRED:
683 case USB_ST_SETUP:
684 tr_setup:
685 pc = usbd_xfer_get_frame(xfer, 0);
686
687 /* handle CnS request */
688 struct mbuf *m = usbd_xfer_get_priv(xfer);
689
690 if (m != NULL) {
691 usbd_m_copy_in(pc, 0, m, 0, m->m_pkthdr.len);
692 usbd_xfer_set_frame_len(xfer, 0, m->m_pkthdr.len);
693 usbd_xfer_set_priv(xfer, NULL);
694 usbd_transfer_submit(xfer);
695 m_freem(m);
696 break;
697 }
698 /* standard ucom transfer */
699 if (ucom_get_data(ucom, pc, 0, USIE_BUFSIZE, &actlen)) {
700 usbd_xfer_set_frame_len(xfer, 0, actlen);
701 usbd_transfer_submit(xfer);
702 }
703 break;
704
705 default: /* Error */
706 if (error != USB_ERR_CANCELLED) {
707 usbd_xfer_set_stall(xfer);
708 goto tr_setup;
709 }
710 break;
711 }
712 }
713
714 static void
usie_uc_status_callback(struct usb_xfer * xfer,usb_error_t error)715 usie_uc_status_callback(struct usb_xfer *xfer, usb_error_t error)
716 {
717 struct usb_page_cache *pc;
718 struct {
719 struct usb_device_request req;
720 uint16_t param;
721 } st;
722 uint32_t actlen;
723 uint16_t param;
724
725 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
726
727 switch (USB_GET_STATE(xfer)) {
728 case USB_ST_TRANSFERRED:
729 DPRINTFN(4, "info received, actlen=%u\n", actlen);
730
731 if (actlen < sizeof(st)) {
732 DPRINTF("data too short actlen=%u\n", actlen);
733 goto tr_setup;
734 }
735 pc = usbd_xfer_get_frame(xfer, 0);
736 usbd_copy_out(pc, 0, &st, sizeof(st));
737
738 if (st.req.bmRequestType == 0xa1 && st.req.bRequest == 0x20) {
739 struct ucom_softc *ucom = usbd_xfer_softc(xfer);
740 struct usie_softc *sc = ucom->sc_parent;
741
742 param = le16toh(st.param);
743 DPRINTF("param=%x\n", param);
744 sc->sc_msr = sc->sc_lsr = 0;
745 sc->sc_msr |= (param & USIE_DCD) ? SER_DCD : 0;
746 sc->sc_msr |= (param & USIE_DSR) ? SER_DSR : 0;
747 sc->sc_msr |= (param & USIE_RI) ? SER_RI : 0;
748 sc->sc_msr |= (param & USIE_CTS) ? 0 : SER_CTS;
749 sc->sc_msr |= (param & USIE_RTS) ? SER_RTS : 0;
750 sc->sc_msr |= (param & USIE_DTR) ? SER_DTR : 0;
751 }
752 /* fall though */
753 case USB_ST_SETUP:
754 tr_setup:
755 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
756 usbd_transfer_submit(xfer);
757 break;
758
759 default: /* Error */
760 DPRINTF("USB transfer error, %s\n",
761 usbd_errstr(error));
762
763 if (error != USB_ERR_CANCELLED) {
764 usbd_xfer_set_stall(xfer);
765 goto tr_setup;
766 }
767 break;
768 }
769 }
770
771 static void
usie_if_rx_callback(struct usb_xfer * xfer,usb_error_t error)772 usie_if_rx_callback(struct usb_xfer *xfer, usb_error_t error)
773 {
774 struct epoch_tracker et;
775 struct usie_softc *sc = usbd_xfer_softc(xfer);
776 struct ifnet *ifp = sc->sc_ifp;
777 struct mbuf *m0;
778 struct mbuf *m = NULL;
779 struct usie_desc *rxd;
780 uint32_t actlen;
781 uint16_t err;
782 uint16_t pkt;
783 uint16_t ipl;
784 uint16_t len;
785 uint16_t diff;
786 uint8_t pad;
787 uint8_t ipv;
788
789 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
790
791 switch (USB_GET_STATE(xfer)) {
792 case USB_ST_TRANSFERRED:
793 DPRINTFN(15, "rx done, actlen=%u\n", actlen);
794
795 if (actlen < sizeof(struct usie_hip)) {
796 DPRINTF("data too short %u\n", actlen);
797 goto tr_setup;
798 }
799 m = sc->sc_rxm;
800 sc->sc_rxm = NULL;
801
802 /* fall though */
803 case USB_ST_SETUP:
804 tr_setup:
805
806 if (sc->sc_rxm == NULL) {
807 sc->sc_rxm = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR,
808 MJUMPAGESIZE /* could be bigger than MCLBYTES */ );
809 }
810 if (sc->sc_rxm == NULL) {
811 DPRINTF("could not allocate Rx mbuf\n");
812 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
813 usbd_xfer_set_stall(xfer);
814 usbd_xfer_set_frames(xfer, 0);
815 } else {
816 /*
817 * Directly loading a mbuf cluster into DMA to
818 * save some data copying. This works because
819 * there is only one cluster.
820 */
821 usbd_xfer_set_frame_data(xfer, 0,
822 mtod(sc->sc_rxm, caddr_t), MIN(MJUMPAGESIZE, USIE_RXSZ_MAX));
823 usbd_xfer_set_frames(xfer, 1);
824 }
825 usbd_transfer_submit(xfer);
826 break;
827
828 default: /* Error */
829 DPRINTF("USB transfer error, %s\n", usbd_errstr(error));
830
831 if (error != USB_ERR_CANCELLED) {
832 /* try to clear stall first */
833 usbd_xfer_set_stall(xfer);
834 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
835 goto tr_setup;
836 }
837 if (sc->sc_rxm != NULL) {
838 m_freem(sc->sc_rxm);
839 sc->sc_rxm = NULL;
840 }
841 break;
842 }
843
844 if (m == NULL)
845 return;
846
847 mtx_unlock(&sc->sc_mtx);
848
849 m->m_pkthdr.len = m->m_len = actlen;
850
851 err = pkt = 0;
852
853 /* HW can aggregate multiple frames in a single USB xfer */
854 NET_EPOCH_ENTER(et);
855 for (;;) {
856 rxd = mtod(m, struct usie_desc *);
857
858 len = be16toh(rxd->hip.len) & USIE_HIP_IP_LEN_MASK;
859 pad = (rxd->hip.id & USIE_HIP_PAD) ? 1 : 0;
860 ipl = (len - pad - ETHER_HDR_LEN);
861 if (ipl >= len) {
862 DPRINTF("Corrupt frame\n");
863 m_freem(m);
864 break;
865 }
866 diff = sizeof(struct usie_desc) + ipl + pad;
867
868 if (((rxd->hip.id & USIE_HIP_MASK) != USIE_HIP_IP) ||
869 (be16toh(rxd->desc_type) & USIE_TYPE_MASK) != USIE_IP_RX) {
870 DPRINTF("received wrong type of packet\n");
871 m->m_data += diff;
872 m->m_pkthdr.len = (m->m_len -= diff);
873 err++;
874 if (m->m_pkthdr.len > 0)
875 continue;
876 m_freem(m);
877 break;
878 }
879 switch (be16toh(rxd->ethhdr.ether_type)) {
880 case ETHERTYPE_IP:
881 ipv = NETISR_IP;
882 break;
883 #ifdef INET6
884 case ETHERTYPE_IPV6:
885 ipv = NETISR_IPV6;
886 break;
887 #endif
888 default:
889 DPRINTF("unsupported ether type\n");
890 err++;
891 break;
892 }
893
894 /* the last packet */
895 if (m->m_pkthdr.len <= diff) {
896 m->m_data += (sizeof(struct usie_desc) + pad);
897 m->m_pkthdr.len = m->m_len = ipl;
898 m->m_pkthdr.rcvif = ifp;
899 BPF_MTAP(sc->sc_ifp, m);
900 netisr_dispatch(ipv, m);
901 break;
902 }
903 /* copy aggregated frames to another mbuf */
904 m0 = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
905 if (__predict_false(m0 == NULL)) {
906 DPRINTF("could not allocate mbuf\n");
907 err++;
908 m_freem(m);
909 break;
910 }
911 m_copydata(m, sizeof(struct usie_desc) + pad, ipl, mtod(m0, caddr_t));
912 m0->m_pkthdr.rcvif = ifp;
913 m0->m_pkthdr.len = m0->m_len = ipl;
914
915 BPF_MTAP(sc->sc_ifp, m0);
916 netisr_dispatch(ipv, m0);
917
918 m->m_data += diff;
919 m->m_pkthdr.len = (m->m_len -= diff);
920 }
921 NET_EPOCH_EXIT(et);
922
923 mtx_lock(&sc->sc_mtx);
924
925 if_inc_counter(ifp, IFCOUNTER_IERRORS, err);
926 if_inc_counter(ifp, IFCOUNTER_IPACKETS, pkt);
927 }
928
929 static void
usie_if_tx_callback(struct usb_xfer * xfer,usb_error_t error)930 usie_if_tx_callback(struct usb_xfer *xfer, usb_error_t error)
931 {
932 struct usie_softc *sc = usbd_xfer_softc(xfer);
933 struct usb_page_cache *pc;
934 struct ifnet *ifp = sc->sc_ifp;
935 struct mbuf *m;
936 uint16_t size;
937
938 switch (USB_GET_STATE(xfer)) {
939 case USB_ST_TRANSFERRED:
940 DPRINTFN(11, "transfer complete\n");
941 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
942 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
943
944 /* fall though */
945 case USB_ST_SETUP:
946 tr_setup:
947
948 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
949 break;
950
951 IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
952 if (m == NULL)
953 break;
954
955 if (m->m_pkthdr.len > (int)(MCLBYTES - ETHER_HDR_LEN +
956 ETHER_CRC_LEN - sizeof(sc->sc_txd))) {
957 DPRINTF("packet len is too big: %d\n",
958 m->m_pkthdr.len);
959 break;
960 }
961 pc = usbd_xfer_get_frame(xfer, 0);
962
963 sc->sc_txd.hip.len = htobe16(m->m_pkthdr.len +
964 ETHER_HDR_LEN + ETHER_CRC_LEN);
965 size = sizeof(sc->sc_txd);
966
967 usbd_copy_in(pc, 0, &sc->sc_txd, size);
968 usbd_m_copy_in(pc, size, m, 0, m->m_pkthdr.len);
969 usbd_xfer_set_frame_len(xfer, 0, m->m_pkthdr.len +
970 size + ETHER_CRC_LEN);
971
972 BPF_MTAP(ifp, m);
973
974 m_freem(m);
975
976 usbd_transfer_submit(xfer);
977 break;
978
979 default: /* Error */
980 DPRINTF("USB transfer error, %s\n",
981 usbd_errstr(error));
982 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
983
984 if (error != USB_ERR_CANCELLED) {
985 usbd_xfer_set_stall(xfer);
986 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
987 goto tr_setup;
988 }
989 break;
990 }
991 }
992
993 static void
usie_if_status_callback(struct usb_xfer * xfer,usb_error_t error)994 usie_if_status_callback(struct usb_xfer *xfer, usb_error_t error)
995 {
996 struct usie_softc *sc = usbd_xfer_softc(xfer);
997 struct usb_page_cache *pc;
998 struct usb_cdc_notification cdc;
999 uint32_t actlen;
1000
1001 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
1002
1003 switch (USB_GET_STATE(xfer)) {
1004 case USB_ST_TRANSFERRED:
1005 DPRINTFN(4, "info received, actlen=%d\n", actlen);
1006
1007 /* usb_cdc_notification - .data[16] */
1008 if (actlen < (sizeof(cdc) - 16)) {
1009 DPRINTF("data too short %d\n", actlen);
1010 goto tr_setup;
1011 }
1012 pc = usbd_xfer_get_frame(xfer, 0);
1013 usbd_copy_out(pc, 0, &cdc, (sizeof(cdc) - 16));
1014
1015 DPRINTFN(4, "bNotification=%x\n", cdc.bNotification);
1016
1017 if (cdc.bNotification & UCDC_N_RESPONSE_AVAILABLE) {
1018 taskqueue_enqueue(taskqueue_thread,
1019 &sc->sc_if_status_task);
1020 }
1021 /* fall though */
1022 case USB_ST_SETUP:
1023 tr_setup:
1024 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
1025 usbd_transfer_submit(xfer);
1026 break;
1027
1028 default: /* Error */
1029 DPRINTF("USB transfer error, %s\n",
1030 usbd_errstr(error));
1031
1032 if (error != USB_ERR_CANCELLED) {
1033 usbd_xfer_set_stall(xfer);
1034 goto tr_setup;
1035 }
1036 break;
1037 }
1038 }
1039
1040 static void
usie_if_sync_to(void * arg)1041 usie_if_sync_to(void *arg)
1042 {
1043 struct usie_softc *sc = arg;
1044
1045 taskqueue_enqueue(taskqueue_thread, &sc->sc_if_sync_task);
1046 }
1047
1048 static void
usie_if_sync_cb(void * arg,int pending)1049 usie_if_sync_cb(void *arg, int pending)
1050 {
1051 struct usie_softc *sc = arg;
1052
1053 mtx_lock(&sc->sc_mtx);
1054
1055 /* call twice */
1056 usie_if_cmd(sc, USIE_HIP_SYNC2M);
1057 usie_if_cmd(sc, USIE_HIP_SYNC2M);
1058
1059 usb_callout_reset(&sc->sc_if_sync_ch, 2 * hz, usie_if_sync_to, sc);
1060
1061 mtx_unlock(&sc->sc_mtx);
1062 }
1063
1064 static void
usie_if_status_cb(void * arg,int pending)1065 usie_if_status_cb(void *arg, int pending)
1066 {
1067 struct usie_softc *sc = arg;
1068 struct ifnet *ifp = sc->sc_ifp;
1069 struct usb_device_request req;
1070 struct usie_hip *hip;
1071 struct usie_lsi *lsi;
1072 uint16_t actlen;
1073 uint8_t ntries;
1074 uint8_t pad;
1075
1076 mtx_lock(&sc->sc_mtx);
1077
1078 req.bmRequestType = UT_READ_CLASS_INTERFACE;
1079 req.bRequest = UCDC_GET_ENCAPSULATED_RESPONSE;
1080 USETW(req.wValue, 0);
1081 USETW(req.wIndex, sc->sc_if_ifnum);
1082 USETW(req.wLength, sizeof(sc->sc_status_temp));
1083
1084 for (ntries = 0; ntries != 10; ntries++) {
1085 int err;
1086
1087 err = usbd_do_request_flags(sc->sc_udev,
1088 &sc->sc_mtx, &req, sc->sc_status_temp, USB_SHORT_XFER_OK,
1089 &actlen, USB_DEFAULT_TIMEOUT);
1090
1091 if (err == 0)
1092 break;
1093
1094 DPRINTF("Control request failed: %s %d/10\n",
1095 usbd_errstr(err), ntries);
1096
1097 usb_pause_mtx(&sc->sc_mtx, USB_MS_TO_TICKS(10));
1098 }
1099
1100 if (ntries == 10) {
1101 mtx_unlock(&sc->sc_mtx);
1102 DPRINTF("Timeout\n");
1103 return;
1104 }
1105
1106 hip = (struct usie_hip *)sc->sc_status_temp;
1107
1108 pad = (hip->id & USIE_HIP_PAD) ? 1 : 0;
1109
1110 DPRINTF("hip.id=%x hip.len=%d actlen=%u pad=%d\n",
1111 hip->id, be16toh(hip->len), actlen, pad);
1112
1113 switch (hip->id & USIE_HIP_MASK) {
1114 case USIE_HIP_SYNC2H:
1115 usie_if_cmd(sc, USIE_HIP_SYNC2M);
1116 break;
1117 case USIE_HIP_RESTR:
1118 usb_callout_stop(&sc->sc_if_sync_ch);
1119 break;
1120 case USIE_HIP_UMTS:
1121 lsi = (struct usie_lsi *)(
1122 sc->sc_status_temp + sizeof(struct usie_hip) + pad);
1123
1124 DPRINTF("lsi.proto=%x lsi.len=%d\n", lsi->proto,
1125 be16toh(lsi->len));
1126
1127 if (lsi->proto != USIE_LSI_UMTS)
1128 break;
1129
1130 if (lsi->area == USIE_LSI_AREA_NO ||
1131 lsi->area == USIE_LSI_AREA_NODATA) {
1132 device_printf(sc->sc_dev, "no service available\n");
1133 break;
1134 }
1135 if (lsi->state == USIE_LSI_STATE_IDLE) {
1136 DPRINTF("lsi.state=%x\n", lsi->state);
1137 break;
1138 }
1139 DPRINTF("ctx=%x\n", hip->param);
1140 sc->sc_txd.hip.param = hip->param;
1141
1142 sc->sc_net.addr_len = lsi->pdp_addr_len;
1143 memcpy(&sc->sc_net.dns1_addr, &lsi->dns1_addr, 16);
1144 memcpy(&sc->sc_net.dns2_addr, &lsi->dns2_addr, 16);
1145 memcpy(sc->sc_net.pdp_addr, lsi->pdp_addr, 16);
1146 memcpy(sc->sc_net.gw_addr, lsi->gw_addr, 16);
1147 ifp->if_flags |= IFF_UP;
1148 ifp->if_drv_flags |= IFF_DRV_RUNNING;
1149
1150 device_printf(sc->sc_dev, "IP Addr=%d.%d.%d.%d\n",
1151 *lsi->pdp_addr, *(lsi->pdp_addr + 1),
1152 *(lsi->pdp_addr + 2), *(lsi->pdp_addr + 3));
1153 device_printf(sc->sc_dev, "Gateway Addr=%d.%d.%d.%d\n",
1154 *lsi->gw_addr, *(lsi->gw_addr + 1),
1155 *(lsi->gw_addr + 2), *(lsi->gw_addr + 3));
1156 device_printf(sc->sc_dev, "Prim NS Addr=%d.%d.%d.%d\n",
1157 *lsi->dns1_addr, *(lsi->dns1_addr + 1),
1158 *(lsi->dns1_addr + 2), *(lsi->dns1_addr + 3));
1159 device_printf(sc->sc_dev, "Scnd NS Addr=%d.%d.%d.%d\n",
1160 *lsi->dns2_addr, *(lsi->dns2_addr + 1),
1161 *(lsi->dns2_addr + 2), *(lsi->dns2_addr + 3));
1162
1163 usie_cns_req(sc, USIE_CNS_ID_RSSI, USIE_CNS_OB_RSSI);
1164 break;
1165
1166 case USIE_HIP_RCGI:
1167 /* ignore, workaround for sloppy windows */
1168 break;
1169 default:
1170 DPRINTF("undefined msgid: %x\n", hip->id);
1171 break;
1172 }
1173
1174 mtx_unlock(&sc->sc_mtx);
1175 }
1176
1177 static void
usie_if_start(struct ifnet * ifp)1178 usie_if_start(struct ifnet *ifp)
1179 {
1180 struct usie_softc *sc = ifp->if_softc;
1181
1182 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1183 DPRINTF("Not running\n");
1184 return;
1185 }
1186 mtx_lock(&sc->sc_mtx);
1187 usbd_transfer_start(sc->sc_if_xfer[USIE_IF_TX]);
1188 mtx_unlock(&sc->sc_mtx);
1189
1190 DPRINTFN(3, "interface started\n");
1191 }
1192
1193 static int
usie_if_output(struct ifnet * ifp,struct mbuf * m,const struct sockaddr * dst,struct route * ro)1194 usie_if_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
1195 struct route *ro)
1196 {
1197 int err;
1198
1199 DPRINTF("proto=%x\n", dst->sa_family);
1200
1201 switch (dst->sa_family) {
1202 #ifdef INET6
1203 case AF_INET6;
1204 /* fall though */
1205 #endif
1206 case AF_INET:
1207 break;
1208
1209 /* silently drop dhclient packets */
1210 case AF_UNSPEC:
1211 m_freem(m);
1212 return (0);
1213
1214 /* drop other packet types */
1215 default:
1216 m_freem(m);
1217 return (EAFNOSUPPORT);
1218 }
1219
1220 err = (ifp->if_transmit)(ifp, m);
1221 if (err) {
1222 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1223 return (ENOBUFS);
1224 }
1225 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
1226
1227 return (0);
1228 }
1229
1230 static void
usie_if_init(void * arg)1231 usie_if_init(void *arg)
1232 {
1233 struct usie_softc *sc = arg;
1234 struct ifnet *ifp = sc->sc_ifp;
1235 uint8_t i;
1236
1237 mtx_lock(&sc->sc_mtx);
1238
1239 /* write tx descriptor */
1240 sc->sc_txd.hip.id = USIE_HIP_CTX;
1241 sc->sc_txd.hip.param = 0; /* init value */
1242 sc->sc_txd.desc_type = htobe16(USIE_IP_TX);
1243
1244 for (i = 0; i != USIE_IF_N_XFER; i++)
1245 usbd_xfer_set_stall(sc->sc_if_xfer[i]);
1246
1247 usbd_transfer_start(sc->sc_uc_xfer[USIE_HIP_IF][USIE_UC_RX]);
1248 usbd_transfer_start(sc->sc_if_xfer[USIE_IF_STATUS]);
1249 usbd_transfer_start(sc->sc_if_xfer[USIE_IF_RX]);
1250
1251 /* if not running, initiate the modem */
1252 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
1253 usie_cns_req(sc, USIE_CNS_ID_INIT, USIE_CNS_OB_LINK_UPDATE);
1254
1255 mtx_unlock(&sc->sc_mtx);
1256
1257 DPRINTF("ifnet initialized\n");
1258 }
1259
1260 static void
usie_if_stop(struct usie_softc * sc)1261 usie_if_stop(struct usie_softc *sc)
1262 {
1263 usb_callout_drain(&sc->sc_if_sync_ch);
1264
1265 mtx_lock(&sc->sc_mtx);
1266
1267 /* usie_cns_req() clears IFF_* flags */
1268 usie_cns_req(sc, USIE_CNS_ID_STOP, USIE_CNS_OB_LINK_UPDATE);
1269
1270 usbd_transfer_stop(sc->sc_if_xfer[USIE_IF_TX]);
1271 usbd_transfer_stop(sc->sc_if_xfer[USIE_IF_RX]);
1272 usbd_transfer_stop(sc->sc_if_xfer[USIE_IF_STATUS]);
1273
1274 /* shutdown device */
1275 usie_if_cmd(sc, USIE_HIP_DOWN);
1276
1277 mtx_unlock(&sc->sc_mtx);
1278 }
1279
1280 static int
usie_if_ioctl(struct ifnet * ifp,u_long cmd,caddr_t data)1281 usie_if_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1282 {
1283 struct usie_softc *sc = ifp->if_softc;
1284 struct ieee80211req *ireq;
1285 struct ieee80211req_sta_info si;
1286 struct ifmediareq *ifmr;
1287
1288 switch (cmd) {
1289 case SIOCSIFFLAGS:
1290 if (ifp->if_flags & IFF_UP) {
1291 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
1292 usie_if_init(sc);
1293 } else {
1294 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1295 usie_if_stop(sc);
1296 }
1297 break;
1298
1299 case SIOCSIFCAP:
1300 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1301 device_printf(sc->sc_dev,
1302 "Connect to the network first.\n");
1303 break;
1304 }
1305 mtx_lock(&sc->sc_mtx);
1306 usie_cns_req(sc, USIE_CNS_ID_RSSI, USIE_CNS_OB_RSSI);
1307 mtx_unlock(&sc->sc_mtx);
1308 break;
1309
1310 case SIOCG80211:
1311 ireq = (struct ieee80211req *)data;
1312
1313 if (ireq->i_type != IEEE80211_IOC_STA_INFO)
1314 break;
1315
1316 memset(&si, 0, sizeof(si));
1317 si.isi_len = sizeof(si);
1318 /*
1319 * ifconfig expects RSSI in 0.5dBm units
1320 * relative to the noise floor.
1321 */
1322 si.isi_rssi = 2 * sc->sc_rssi;
1323 if (copyout(&si, (uint8_t *)ireq->i_data + 8,
1324 sizeof(struct ieee80211req_sta_info)))
1325 DPRINTF("copyout failed\n");
1326 DPRINTF("80211\n");
1327 break;
1328
1329 case SIOCGIFMEDIA: /* to fool ifconfig */
1330 ifmr = (struct ifmediareq *)data;
1331 ifmr->ifm_count = 1;
1332 DPRINTF("media\n");
1333 break;
1334
1335 case SIOCSIFADDR:
1336 break;
1337
1338 default:
1339 return (EINVAL);
1340 }
1341 return (0);
1342 }
1343
1344 static int
usie_do_request(struct usie_softc * sc,struct usb_device_request * req,void * data)1345 usie_do_request(struct usie_softc *sc, struct usb_device_request *req,
1346 void *data)
1347 {
1348 int err = 0;
1349 int ntries;
1350
1351 mtx_assert(&sc->sc_mtx, MA_OWNED);
1352
1353 for (ntries = 0; ntries != 10; ntries++) {
1354 err = usbd_do_request(sc->sc_udev,
1355 &sc->sc_mtx, req, data);
1356 if (err == 0)
1357 break;
1358
1359 DPRINTF("Control request failed: %s %d/10\n",
1360 usbd_errstr(err), ntries);
1361
1362 usb_pause_mtx(&sc->sc_mtx, USB_MS_TO_TICKS(10));
1363 }
1364 return (err);
1365 }
1366
1367 static int
usie_if_cmd(struct usie_softc * sc,uint8_t cmd)1368 usie_if_cmd(struct usie_softc *sc, uint8_t cmd)
1369 {
1370 struct usb_device_request req;
1371 struct usie_hip msg;
1372
1373 msg.len = 0;
1374 msg.id = cmd;
1375 msg.param = 0;
1376
1377 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
1378 req.bRequest = UCDC_SEND_ENCAPSULATED_COMMAND;
1379 USETW(req.wValue, 0);
1380 USETW(req.wIndex, sc->sc_if_ifnum);
1381 USETW(req.wLength, sizeof(msg));
1382
1383 DPRINTF("cmd=%x\n", cmd);
1384
1385 return (usie_do_request(sc, &req, &msg));
1386 }
1387
1388 static void
usie_cns_req(struct usie_softc * sc,uint32_t id,uint16_t obj)1389 usie_cns_req(struct usie_softc *sc, uint32_t id, uint16_t obj)
1390 {
1391 struct ifnet *ifp = sc->sc_ifp;
1392 struct mbuf *m;
1393 struct usb_xfer *xfer;
1394 struct usie_hip *hip;
1395 struct usie_cns *cns;
1396 uint8_t *param;
1397 uint8_t *tmp;
1398 uint8_t cns_len;
1399
1400 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
1401 if (__predict_false(m == NULL)) {
1402 DPRINTF("could not allocate mbuf\n");
1403 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1404 return;
1405 }
1406 /* to align usie_hip{} on 32 bit */
1407 m->m_data += 3;
1408 param = mtod(m, uint8_t *);
1409 *param++ = USIE_HIP_FRM_CHR;
1410 hip = (struct usie_hip *)param;
1411 cns = (struct usie_cns *)(hip + 1);
1412
1413 tmp = param + USIE_HIPCNS_MIN - 2;
1414
1415 switch (obj) {
1416 case USIE_CNS_OB_LINK_UPDATE:
1417 cns_len = 2;
1418 cns->op = USIE_CNS_OP_SET;
1419 *tmp++ = 1; /* profile ID, always use 1 for now */
1420 *tmp++ = id == USIE_CNS_ID_INIT ? 1 : 0;
1421 break;
1422
1423 case USIE_CNS_OB_PROF_WRITE:
1424 cns_len = 245;
1425 cns->op = USIE_CNS_OP_SET;
1426 *tmp++ = 1; /* profile ID, always use 1 for now */
1427 *tmp++ = 2;
1428 memcpy(tmp, &sc->sc_net, 34);
1429 memset(tmp + 35, 0, 245 - 36);
1430 tmp += 243;
1431 break;
1432
1433 case USIE_CNS_OB_RSSI:
1434 cns_len = 0;
1435 cns->op = USIE_CNS_OP_REQ;
1436 break;
1437
1438 default:
1439 DPRINTF("unsupported CnS object type\n");
1440 return;
1441 }
1442 *tmp = USIE_HIP_FRM_CHR;
1443
1444 hip->len = htobe16(sizeof(struct usie_cns) + cns_len);
1445 hip->id = USIE_HIP_CNS2M;
1446 hip->param = 0; /* none for CnS */
1447
1448 cns->obj = htobe16(obj);
1449 cns->id = htobe32(id);
1450 cns->len = cns_len;
1451 cns->rsv0 = cns->rsv1 = 0; /* always '0' */
1452
1453 param = (uint8_t *)(cns + 1);
1454
1455 DPRINTF("param: %16D\n", param, ":");
1456
1457 m->m_pkthdr.len = m->m_len = USIE_HIPCNS_MIN + cns_len + 2;
1458
1459 xfer = sc->sc_uc_xfer[USIE_HIP_IF][USIE_UC_TX];
1460
1461 if (usbd_xfer_get_priv(xfer) == NULL) {
1462 usbd_xfer_set_priv(xfer, m);
1463 usbd_transfer_start(xfer);
1464 } else {
1465 DPRINTF("Dropped CNS event\n");
1466 m_freem(m);
1467 }
1468 }
1469
1470 static void
usie_cns_rsp(struct usie_softc * sc,struct usie_cns * cns)1471 usie_cns_rsp(struct usie_softc *sc, struct usie_cns *cns)
1472 {
1473 struct ifnet *ifp = sc->sc_ifp;
1474
1475 DPRINTF("received CnS\n");
1476
1477 switch (be16toh(cns->obj)) {
1478 case USIE_CNS_OB_LINK_UPDATE:
1479 if (be32toh(cns->id) & USIE_CNS_ID_INIT)
1480 usie_if_sync_to(sc);
1481 else if (be32toh(cns->id) & USIE_CNS_ID_STOP) {
1482 ifp->if_flags &= ~IFF_UP;
1483 ifp->if_drv_flags &=
1484 ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
1485 } else
1486 DPRINTF("undefined link update\n");
1487 break;
1488
1489 case USIE_CNS_OB_RSSI:
1490 sc->sc_rssi = be16toh(*(int16_t *)(cns + 1));
1491 if (sc->sc_rssi <= 0)
1492 device_printf(sc->sc_dev, "No signal\n");
1493 else {
1494 device_printf(sc->sc_dev, "RSSI=%ddBm\n",
1495 sc->sc_rssi - 110);
1496 }
1497 break;
1498
1499 case USIE_CNS_OB_PROF_WRITE:
1500 break;
1501
1502 case USIE_CNS_OB_PDP_READ:
1503 break;
1504
1505 default:
1506 DPRINTF("undefined CnS\n");
1507 break;
1508 }
1509 }
1510
1511 static void
usie_hip_rsp(struct usie_softc * sc,uint8_t * rsp,uint32_t len)1512 usie_hip_rsp(struct usie_softc *sc, uint8_t *rsp, uint32_t len)
1513 {
1514 struct usie_hip *hip;
1515 struct usie_cns *cns;
1516 uint32_t i;
1517 uint32_t j;
1518 uint32_t off;
1519 uint8_t tmp[USIE_HIPCNS_MAX] __aligned(4);
1520
1521 for (off = 0; (off + USIE_HIPCNS_MIN) <= len; off++) {
1522 uint8_t pad;
1523
1524 while ((off < len) && (rsp[off] == USIE_HIP_FRM_CHR))
1525 off++;
1526
1527 /* Unstuff the bytes */
1528 for (i = j = 0; ((i + off) < len) &&
1529 (j < USIE_HIPCNS_MAX); i++) {
1530 if (rsp[i + off] == USIE_HIP_FRM_CHR)
1531 break;
1532
1533 if (rsp[i + off] == USIE_HIP_ESC_CHR) {
1534 if ((i + off + 1) >= len)
1535 break;
1536 tmp[j++] = rsp[i++ + off + 1] ^ 0x20;
1537 } else {
1538 tmp[j++] = rsp[i + off];
1539 }
1540 }
1541
1542 off += i;
1543
1544 DPRINTF("frame len=%d\n", j);
1545
1546 if (j < sizeof(struct usie_hip)) {
1547 DPRINTF("too little data\n");
1548 break;
1549 }
1550 /*
1551 * Make sure we are not reading the stack if something
1552 * is wrong.
1553 */
1554 memset(tmp + j, 0, sizeof(tmp) - j);
1555
1556 hip = (struct usie_hip *)tmp;
1557
1558 DPRINTF("hip: len=%d msgID=%02x, param=%02x\n",
1559 be16toh(hip->len), hip->id, hip->param);
1560
1561 pad = (hip->id & USIE_HIP_PAD) ? 1 : 0;
1562
1563 if ((hip->id & USIE_HIP_MASK) == USIE_HIP_CNS2H) {
1564 cns = (struct usie_cns *)(((uint8_t *)(hip + 1)) + pad);
1565
1566 if (j < (sizeof(struct usie_cns) +
1567 sizeof(struct usie_hip) + pad)) {
1568 DPRINTF("too little data\n");
1569 break;
1570 }
1571 DPRINTF("cns: obj=%04x, op=%02x, rsv0=%02x, "
1572 "app=%08x, rsv1=%02x, len=%d\n",
1573 be16toh(cns->obj), cns->op, cns->rsv0,
1574 be32toh(cns->id), cns->rsv1, cns->len);
1575
1576 if (cns->op & USIE_CNS_OP_ERR)
1577 DPRINTF("CnS error response\n");
1578 else
1579 usie_cns_rsp(sc, cns);
1580
1581 i = sizeof(struct usie_hip) + pad + sizeof(struct usie_cns);
1582 j = cns->len;
1583 } else {
1584 i = sizeof(struct usie_hip) + pad;
1585 j = be16toh(hip->len);
1586 }
1587 #ifdef USB_DEBUG
1588 if (usie_debug == 0)
1589 continue;
1590
1591 while (i < USIE_HIPCNS_MAX && j > 0) {
1592 DPRINTF("param[0x%02x] = 0x%02x\n", i, tmp[i]);
1593 i++;
1594 j--;
1595 }
1596 #endif
1597 }
1598 }
1599
1600 static int
usie_driver_loaded(struct module * mod,int what,void * arg)1601 usie_driver_loaded(struct module *mod, int what, void *arg)
1602 {
1603 switch (what) {
1604 case MOD_LOAD:
1605 /* register autoinstall handler */
1606 usie_etag = EVENTHANDLER_REGISTER(usb_dev_configured,
1607 usie_autoinst, NULL, EVENTHANDLER_PRI_ANY);
1608 break;
1609 case MOD_UNLOAD:
1610 EVENTHANDLER_DEREGISTER(usb_dev_configured, usie_etag);
1611 break;
1612 default:
1613 return (EOPNOTSUPP);
1614 }
1615 return (0);
1616 }
1617