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