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