xref: /freebsd/sys/dev/usb/net/if_usie.c (revision 3b68c491d37196bb76a95bce3c02f7c6d5ba22fd)
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 	if_initname(ifp, "usie", device_get_unit(self));
434 
435 	if_setsoftc(ifp, sc);
436 	if_setmtu(ifp, USIE_MTU_MAX);
437 	if_setflagbits(ifp, IFF_NOARP, 0);
438 	if_setinitfn(ifp, usie_if_init);
439 	if_setioctlfn(ifp, usie_if_ioctl);
440 	if_setstartfn(ifp, usie_if_start);
441 	if_setoutputfn(ifp, usie_if_output);
442 	if_setsendqlen(ifp, ifqmaxlen);
443 	if_setsendqready(ifp);
444 
445 	if_attach(ifp);
446 	bpfattach(ifp, DLT_RAW, 0);
447 
448 	if (fwattr & USIE_PM_AUTO) {
449 		usbd_set_power_mode(uaa->device, USB_POWER_MODE_SAVE);
450 		DPRINTF("enabling automatic suspend and resume\n");
451 	} else {
452 		usbd_set_power_mode(uaa->device, USB_POWER_MODE_ON);
453 		DPRINTF("USB power is always ON\n");
454 	}
455 
456 	DPRINTF("device attached\n");
457 	return (0);
458 
459 detach:
460 	usie_detach(self);
461 	return (ENOMEM);
462 }
463 
464 static int
465 usie_detach(device_t self)
466 {
467 	struct usie_softc *sc = device_get_softc(self);
468 	uint8_t x;
469 
470 	/* detach ifnet */
471 	if (sc->sc_ifp != NULL) {
472 		usie_if_stop(sc);
473 		usbd_transfer_unsetup(sc->sc_if_xfer, USIE_IF_N_XFER);
474 		bpfdetach(sc->sc_ifp);
475 		if_detach(sc->sc_ifp);
476 		if_free(sc->sc_ifp);
477 		sc->sc_ifp = NULL;
478 	}
479 	/* detach ucom */
480 	if (sc->sc_nucom > 0)
481 		ucom_detach(&sc->sc_super_ucom, sc->sc_ucom);
482 
483 	/* stop all USB transfers */
484 	usbd_transfer_unsetup(sc->sc_if_xfer, USIE_IF_N_XFER);
485 
486 	for (x = 0; x != USIE_UCOM_MAX; x++)
487 		usbd_transfer_unsetup(sc->sc_uc_xfer[x], USIE_UC_N_XFER);
488 
489 	device_claim_softc(self);
490 
491 	usie_free_softc(sc);
492 
493 	return (0);
494 }
495 
496 UCOM_UNLOAD_DRAIN(usie);
497 
498 static void
499 usie_free_softc(struct usie_softc *sc)
500 {
501 	if (ucom_unref(&sc->sc_super_ucom)) {
502 		mtx_destroy(&sc->sc_mtx);
503 		device_free_softc(sc);
504 	}
505 }
506 
507 static void
508 usie_free(struct ucom_softc *ucom)
509 {
510 	usie_free_softc(ucom->sc_parent);
511 }
512 
513 static void
514 usie_uc_update_line_state(struct ucom_softc *ucom, uint8_t ls)
515 {
516 	struct usie_softc *sc = ucom->sc_parent;
517 	struct usb_device_request req;
518 
519 	if (sc->sc_uc_xfer[ucom->sc_subunit][USIE_UC_STATUS] == NULL)
520 		return;
521 
522 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
523 	req.bRequest = USIE_LINK_STATE;
524 	USETW(req.wValue, ls);
525 	USETW(req.wIndex, sc->sc_uc_ifnum[ucom->sc_subunit]);
526 	USETW(req.wLength, 0);
527 
528 	DPRINTF("sc_uc_ifnum=%d\n", sc->sc_uc_ifnum[ucom->sc_subunit]);
529 
530 	usie_do_request(sc, &req, NULL);
531 }
532 
533 static void
534 usie_uc_cfg_get_status(struct ucom_softc *ucom, uint8_t *lsr, uint8_t *msr)
535 {
536 	struct usie_softc *sc = ucom->sc_parent;
537 
538 	*msr = sc->sc_msr;
539 	*lsr = sc->sc_lsr;
540 }
541 
542 static void
543 usie_uc_cfg_set_dtr(struct ucom_softc *ucom, uint8_t flag)
544 {
545 	uint8_t dtr;
546 
547 	dtr = flag ? USIE_LS_DTR : 0;
548 	usie_uc_update_line_state(ucom, dtr);
549 }
550 
551 static void
552 usie_uc_cfg_set_rts(struct ucom_softc *ucom, uint8_t flag)
553 {
554 	uint8_t rts;
555 
556 	rts = flag ? USIE_LS_RTS : 0;
557 	usie_uc_update_line_state(ucom, rts);
558 }
559 
560 static void
561 usie_uc_cfg_open(struct ucom_softc *ucom)
562 {
563 	struct usie_softc *sc = ucom->sc_parent;
564 
565 	/* usbd_transfer_start() is NULL safe */
566 
567 	usbd_transfer_start(sc->sc_uc_xfer[ucom->sc_subunit][USIE_UC_STATUS]);
568 }
569 
570 static void
571 usie_uc_cfg_close(struct ucom_softc *ucom)
572 {
573 	struct usie_softc *sc = ucom->sc_parent;
574 
575 	usbd_transfer_stop(sc->sc_uc_xfer[ucom->sc_subunit][USIE_UC_STATUS]);
576 }
577 
578 static void
579 usie_uc_start_read(struct ucom_softc *ucom)
580 {
581 	struct usie_softc *sc = ucom->sc_parent;
582 
583 	usbd_transfer_start(sc->sc_uc_xfer[ucom->sc_subunit][USIE_UC_RX]);
584 }
585 
586 static void
587 usie_uc_stop_read(struct ucom_softc *ucom)
588 {
589 	struct usie_softc *sc = ucom->sc_parent;
590 
591 	usbd_transfer_stop(sc->sc_uc_xfer[ucom->sc_subunit][USIE_UC_RX]);
592 }
593 
594 static void
595 usie_uc_start_write(struct ucom_softc *ucom)
596 {
597 	struct usie_softc *sc = ucom->sc_parent;
598 
599 	usbd_transfer_start(sc->sc_uc_xfer[ucom->sc_subunit][USIE_UC_TX]);
600 }
601 
602 static void
603 usie_uc_stop_write(struct ucom_softc *ucom)
604 {
605 	struct usie_softc *sc = ucom->sc_parent;
606 
607 	usbd_transfer_stop(sc->sc_uc_xfer[ucom->sc_subunit][USIE_UC_TX]);
608 }
609 
610 static void
611 usie_uc_rx_callback(struct usb_xfer *xfer, usb_error_t error)
612 {
613 	struct ucom_softc *ucom = usbd_xfer_softc(xfer);
614 	struct usie_softc *sc = ucom->sc_parent;
615 	struct usb_page_cache *pc;
616 	uint32_t actlen;
617 
618 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
619 
620 	switch (USB_GET_STATE(xfer)) {
621 	case USB_ST_TRANSFERRED:
622 		pc = usbd_xfer_get_frame(xfer, 0);
623 
624 		/* handle CnS response */
625 		if (ucom == sc->sc_ucom && actlen >= USIE_HIPCNS_MIN) {
626 			DPRINTF("transferred=%u\n", actlen);
627 
628 			/* check if it is really CnS reply */
629 			usbd_copy_out(pc, 0, sc->sc_resp_temp, 1);
630 
631 			if (sc->sc_resp_temp[0] == USIE_HIP_FRM_CHR) {
632 				/* verify actlen */
633 				if (actlen > USIE_BUFSIZE)
634 					actlen = USIE_BUFSIZE;
635 
636 				/* get complete message */
637 				usbd_copy_out(pc, 0, sc->sc_resp_temp, actlen);
638 				usie_hip_rsp(sc, sc->sc_resp_temp, actlen);
639 
640 				/* need to fall though */
641 				goto tr_setup;
642 			}
643 			/* else call ucom_put_data() */
644 		}
645 		/* standard ucom transfer */
646 		ucom_put_data(ucom, pc, 0, actlen);
647 
648 		/* fall though */
649 	case USB_ST_SETUP:
650 tr_setup:
651 		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
652 		usbd_transfer_submit(xfer);
653 		break;
654 
655 	default:			/* Error */
656 		if (error != USB_ERR_CANCELLED) {
657 			usbd_xfer_set_stall(xfer);
658 			goto tr_setup;
659 		}
660 		break;
661 	}
662 }
663 
664 static void
665 usie_uc_tx_callback(struct usb_xfer *xfer, usb_error_t error)
666 {
667 	struct ucom_softc *ucom = usbd_xfer_softc(xfer);
668 	struct usb_page_cache *pc;
669 	uint32_t actlen;
670 
671 	switch (USB_GET_STATE(xfer)) {
672 	case USB_ST_TRANSFERRED:
673 	case USB_ST_SETUP:
674 tr_setup:
675 		pc = usbd_xfer_get_frame(xfer, 0);
676 
677 		/* handle CnS request */
678 		struct mbuf *m = usbd_xfer_get_priv(xfer);
679 
680 		if (m != NULL) {
681 			usbd_m_copy_in(pc, 0, m, 0, m->m_pkthdr.len);
682 			usbd_xfer_set_frame_len(xfer, 0, m->m_pkthdr.len);
683 			usbd_xfer_set_priv(xfer, NULL);
684 			usbd_transfer_submit(xfer);
685 			m_freem(m);
686 			break;
687 		}
688 		/* standard ucom transfer */
689 		if (ucom_get_data(ucom, pc, 0, USIE_BUFSIZE, &actlen)) {
690 			usbd_xfer_set_frame_len(xfer, 0, actlen);
691 			usbd_transfer_submit(xfer);
692 		}
693 		break;
694 
695 	default:			/* Error */
696 		if (error != USB_ERR_CANCELLED) {
697 			usbd_xfer_set_stall(xfer);
698 			goto tr_setup;
699 		}
700 		break;
701 	}
702 }
703 
704 static void
705 usie_uc_status_callback(struct usb_xfer *xfer, usb_error_t error)
706 {
707 	struct usb_page_cache *pc;
708 	struct {
709 		struct usb_device_request req;
710 		uint16_t param;
711 	}      st;
712 	uint32_t actlen;
713 	uint16_t param;
714 
715 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
716 
717 	switch (USB_GET_STATE(xfer)) {
718 	case USB_ST_TRANSFERRED:
719 		DPRINTFN(4, "info received, actlen=%u\n", actlen);
720 
721 		if (actlen < sizeof(st)) {
722 			DPRINTF("data too short actlen=%u\n", actlen);
723 			goto tr_setup;
724 		}
725 		pc = usbd_xfer_get_frame(xfer, 0);
726 		usbd_copy_out(pc, 0, &st, sizeof(st));
727 
728 		if (st.req.bmRequestType == 0xa1 && st.req.bRequest == 0x20) {
729 			struct ucom_softc *ucom = usbd_xfer_softc(xfer);
730 			struct usie_softc *sc = ucom->sc_parent;
731 
732 			param = le16toh(st.param);
733 			DPRINTF("param=%x\n", param);
734 			sc->sc_msr = sc->sc_lsr = 0;
735 			sc->sc_msr |= (param & USIE_DCD) ? SER_DCD : 0;
736 			sc->sc_msr |= (param & USIE_DSR) ? SER_DSR : 0;
737 			sc->sc_msr |= (param & USIE_RI) ? SER_RI : 0;
738 			sc->sc_msr |= (param & USIE_CTS) ? 0 : SER_CTS;
739 			sc->sc_msr |= (param & USIE_RTS) ? SER_RTS : 0;
740 			sc->sc_msr |= (param & USIE_DTR) ? SER_DTR : 0;
741 		}
742 		/* fall though */
743 	case USB_ST_SETUP:
744 tr_setup:
745 		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
746 		usbd_transfer_submit(xfer);
747 		break;
748 
749 	default:			/* Error */
750 		DPRINTF("USB transfer error, %s\n",
751 		    usbd_errstr(error));
752 
753 		if (error != USB_ERR_CANCELLED) {
754 			usbd_xfer_set_stall(xfer);
755 			goto tr_setup;
756 		}
757 		break;
758 	}
759 }
760 
761 static void
762 usie_if_rx_callback(struct usb_xfer *xfer, usb_error_t error)
763 {
764 	struct epoch_tracker et;
765 	struct usie_softc *sc = usbd_xfer_softc(xfer);
766 	if_t ifp = sc->sc_ifp;
767 	struct mbuf *m0;
768 	struct mbuf *m = NULL;
769 	struct usie_desc *rxd;
770 	uint32_t actlen;
771 	uint16_t err;
772 	uint16_t pkt;
773 	uint16_t ipl;
774 	uint16_t len;
775 	uint16_t diff;
776 	uint8_t pad;
777 	uint8_t ipv;
778 
779 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
780 
781 	switch (USB_GET_STATE(xfer)) {
782 	case USB_ST_TRANSFERRED:
783 		DPRINTFN(15, "rx done, actlen=%u\n", actlen);
784 
785 		if (actlen < sizeof(struct usie_hip)) {
786 			DPRINTF("data too short %u\n", actlen);
787 			goto tr_setup;
788 		}
789 		m = sc->sc_rxm;
790 		sc->sc_rxm = NULL;
791 
792 		/* fall though */
793 	case USB_ST_SETUP:
794 tr_setup:
795 
796 		if (sc->sc_rxm == NULL) {
797 			sc->sc_rxm = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR,
798 			    MJUMPAGESIZE /* could be bigger than MCLBYTES */ );
799 		}
800 		if (sc->sc_rxm == NULL) {
801 			DPRINTF("could not allocate Rx mbuf\n");
802 			if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
803 			usbd_xfer_set_stall(xfer);
804 			usbd_xfer_set_frames(xfer, 0);
805 		} else {
806 			/*
807 			 * Directly loading a mbuf cluster into DMA to
808 			 * save some data copying. This works because
809 			 * there is only one cluster.
810 			 */
811 			usbd_xfer_set_frame_data(xfer, 0,
812 			    mtod(sc->sc_rxm, caddr_t), MIN(MJUMPAGESIZE, USIE_RXSZ_MAX));
813 			usbd_xfer_set_frames(xfer, 1);
814 		}
815 		usbd_transfer_submit(xfer);
816 		break;
817 
818 	default:			/* Error */
819 		DPRINTF("USB transfer error, %s\n", usbd_errstr(error));
820 
821 		if (error != USB_ERR_CANCELLED) {
822 			/* try to clear stall first */
823 			usbd_xfer_set_stall(xfer);
824 			if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
825 			goto tr_setup;
826 		}
827 		if (sc->sc_rxm != NULL) {
828 			m_freem(sc->sc_rxm);
829 			sc->sc_rxm = NULL;
830 		}
831 		break;
832 	}
833 
834 	if (m == NULL)
835 		return;
836 
837 	mtx_unlock(&sc->sc_mtx);
838 
839 	m->m_pkthdr.len = m->m_len = actlen;
840 
841 	err = pkt = 0;
842 
843 	/* HW can aggregate multiple frames in a single USB xfer */
844 	NET_EPOCH_ENTER(et);
845 	for (;;) {
846 		rxd = mtod(m, struct usie_desc *);
847 
848 		len = be16toh(rxd->hip.len) & USIE_HIP_IP_LEN_MASK;
849 		pad = (rxd->hip.id & USIE_HIP_PAD) ? 1 : 0;
850 		ipl = (len - pad - ETHER_HDR_LEN);
851 		if (ipl >= len) {
852 			DPRINTF("Corrupt frame\n");
853 			m_freem(m);
854 			break;
855 		}
856 		diff = sizeof(struct usie_desc) + ipl + pad;
857 
858 		if (((rxd->hip.id & USIE_HIP_MASK) != USIE_HIP_IP) ||
859 		    (be16toh(rxd->desc_type) & USIE_TYPE_MASK) != USIE_IP_RX) {
860 			DPRINTF("received wrong type of packet\n");
861 			m->m_data += diff;
862 			m->m_pkthdr.len = (m->m_len -= diff);
863 			err++;
864 			if (m->m_pkthdr.len > 0)
865 				continue;
866 			m_freem(m);
867 			break;
868 		}
869 		switch (be16toh(rxd->ethhdr.ether_type)) {
870 		case ETHERTYPE_IP:
871 			ipv = NETISR_IP;
872 			break;
873 #ifdef INET6
874 		case ETHERTYPE_IPV6:
875 			ipv = NETISR_IPV6;
876 			break;
877 #endif
878 		default:
879 			DPRINTF("unsupported ether type\n");
880 			err++;
881 			break;
882 		}
883 
884 		/* the last packet */
885 		if (m->m_pkthdr.len <= diff) {
886 			m->m_data += (sizeof(struct usie_desc) + pad);
887 			m->m_pkthdr.len = m->m_len = ipl;
888 			m->m_pkthdr.rcvif = ifp;
889 			BPF_MTAP(sc->sc_ifp, m);
890 			netisr_dispatch(ipv, m);
891 			break;
892 		}
893 		/* copy aggregated frames to another mbuf */
894 		m0 = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
895 		if (__predict_false(m0 == NULL)) {
896 			DPRINTF("could not allocate mbuf\n");
897 			err++;
898 			m_freem(m);
899 			break;
900 		}
901 		m_copydata(m, sizeof(struct usie_desc) + pad, ipl, mtod(m0, caddr_t));
902 		m0->m_pkthdr.rcvif = ifp;
903 		m0->m_pkthdr.len = m0->m_len = ipl;
904 
905 		BPF_MTAP(sc->sc_ifp, m0);
906 		netisr_dispatch(ipv, m0);
907 
908 		m->m_data += diff;
909 		m->m_pkthdr.len = (m->m_len -= diff);
910 	}
911 	NET_EPOCH_EXIT(et);
912 
913 	mtx_lock(&sc->sc_mtx);
914 
915 	if_inc_counter(ifp, IFCOUNTER_IERRORS, err);
916 	if_inc_counter(ifp, IFCOUNTER_IPACKETS, pkt);
917 }
918 
919 static void
920 usie_if_tx_callback(struct usb_xfer *xfer, usb_error_t error)
921 {
922 	struct usie_softc *sc = usbd_xfer_softc(xfer);
923 	struct usb_page_cache *pc;
924 	if_t ifp = sc->sc_ifp;
925 	struct mbuf *m;
926 	uint16_t size;
927 
928 	switch (USB_GET_STATE(xfer)) {
929 	case USB_ST_TRANSFERRED:
930 		DPRINTFN(11, "transfer complete\n");
931 		if_setdrvflagbits(ifp, 0, IFF_DRV_OACTIVE);
932 		if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
933 
934 		/* fall though */
935 	case USB_ST_SETUP:
936 tr_setup:
937 
938 		if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) == 0)
939 			break;
940 
941 		m = if_dequeue(ifp);
942 		if (m == NULL)
943 			break;
944 
945 		if (m->m_pkthdr.len > (int)(MCLBYTES - ETHER_HDR_LEN +
946 		    ETHER_CRC_LEN - sizeof(sc->sc_txd))) {
947 			DPRINTF("packet len is too big: %d\n",
948 			    m->m_pkthdr.len);
949 			break;
950 		}
951 		pc = usbd_xfer_get_frame(xfer, 0);
952 
953 		sc->sc_txd.hip.len = htobe16(m->m_pkthdr.len +
954 		    ETHER_HDR_LEN + ETHER_CRC_LEN);
955 		size = sizeof(sc->sc_txd);
956 
957 		usbd_copy_in(pc, 0, &sc->sc_txd, size);
958 		usbd_m_copy_in(pc, size, m, 0, m->m_pkthdr.len);
959 		usbd_xfer_set_frame_len(xfer, 0, m->m_pkthdr.len +
960 		    size + ETHER_CRC_LEN);
961 
962 		BPF_MTAP(ifp, m);
963 
964 		m_freem(m);
965 
966 		usbd_transfer_submit(xfer);
967 		break;
968 
969 	default:			/* Error */
970 		DPRINTF("USB transfer error, %s\n",
971 		    usbd_errstr(error));
972 		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
973 
974 		if (error != USB_ERR_CANCELLED) {
975 			usbd_xfer_set_stall(xfer);
976 			if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
977 			goto tr_setup;
978 		}
979 		break;
980 	}
981 }
982 
983 static void
984 usie_if_status_callback(struct usb_xfer *xfer, usb_error_t error)
985 {
986 	struct usie_softc *sc = usbd_xfer_softc(xfer);
987 	struct usb_page_cache *pc;
988 	struct usb_cdc_notification cdc;
989 	uint32_t actlen;
990 
991 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
992 
993 	switch (USB_GET_STATE(xfer)) {
994 	case USB_ST_TRANSFERRED:
995 		DPRINTFN(4, "info received, actlen=%d\n", actlen);
996 
997 		/* usb_cdc_notification - .data[16] */
998 		if (actlen < (sizeof(cdc) - 16)) {
999 			DPRINTF("data too short %d\n", actlen);
1000 			goto tr_setup;
1001 		}
1002 		pc = usbd_xfer_get_frame(xfer, 0);
1003 		usbd_copy_out(pc, 0, &cdc, (sizeof(cdc) - 16));
1004 
1005 		DPRINTFN(4, "bNotification=%x\n", cdc.bNotification);
1006 
1007 		if (cdc.bNotification & UCDC_N_RESPONSE_AVAILABLE) {
1008 			taskqueue_enqueue(taskqueue_thread,
1009 			    &sc->sc_if_status_task);
1010 		}
1011 		/* fall though */
1012 	case USB_ST_SETUP:
1013 tr_setup:
1014 		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
1015 		usbd_transfer_submit(xfer);
1016 		break;
1017 
1018 	default:			/* Error */
1019 		DPRINTF("USB transfer error, %s\n",
1020 		    usbd_errstr(error));
1021 
1022 		if (error != USB_ERR_CANCELLED) {
1023 			usbd_xfer_set_stall(xfer);
1024 			goto tr_setup;
1025 		}
1026 		break;
1027 	}
1028 }
1029 
1030 static void
1031 usie_if_sync_to(void *arg)
1032 {
1033 	struct usie_softc *sc = arg;
1034 
1035 	taskqueue_enqueue(taskqueue_thread, &sc->sc_if_sync_task);
1036 }
1037 
1038 static void
1039 usie_if_sync_cb(void *arg, int pending)
1040 {
1041 	struct usie_softc *sc = arg;
1042 
1043 	mtx_lock(&sc->sc_mtx);
1044 
1045 	/* call twice */
1046 	usie_if_cmd(sc, USIE_HIP_SYNC2M);
1047 	usie_if_cmd(sc, USIE_HIP_SYNC2M);
1048 
1049 	usb_callout_reset(&sc->sc_if_sync_ch, 2 * hz, usie_if_sync_to, sc);
1050 
1051 	mtx_unlock(&sc->sc_mtx);
1052 }
1053 
1054 static void
1055 usie_if_status_cb(void *arg, int pending)
1056 {
1057 	struct usie_softc *sc = arg;
1058 	if_t ifp = sc->sc_ifp;
1059 	struct usb_device_request req;
1060 	struct usie_hip *hip;
1061 	struct usie_lsi *lsi;
1062 	uint16_t actlen;
1063 	uint8_t ntries;
1064 	uint8_t pad;
1065 
1066 	mtx_lock(&sc->sc_mtx);
1067 
1068 	req.bmRequestType = UT_READ_CLASS_INTERFACE;
1069 	req.bRequest = UCDC_GET_ENCAPSULATED_RESPONSE;
1070 	USETW(req.wValue, 0);
1071 	USETW(req.wIndex, sc->sc_if_ifnum);
1072 	USETW(req.wLength, sizeof(sc->sc_status_temp));
1073 
1074 	for (ntries = 0; ntries != 10; ntries++) {
1075 		int err;
1076 
1077 		err = usbd_do_request_flags(sc->sc_udev,
1078 		    &sc->sc_mtx, &req, sc->sc_status_temp, USB_SHORT_XFER_OK,
1079 		    &actlen, USB_DEFAULT_TIMEOUT);
1080 
1081 		if (err == 0)
1082 			break;
1083 
1084 		DPRINTF("Control request failed: %s %d/10\n",
1085 		    usbd_errstr(err), ntries);
1086 
1087 		usb_pause_mtx(&sc->sc_mtx, USB_MS_TO_TICKS(10));
1088 	}
1089 
1090 	if (ntries == 10) {
1091 		mtx_unlock(&sc->sc_mtx);
1092 		DPRINTF("Timeout\n");
1093 		return;
1094 	}
1095 
1096 	hip = (struct usie_hip *)sc->sc_status_temp;
1097 
1098 	pad = (hip->id & USIE_HIP_PAD) ? 1 : 0;
1099 
1100 	DPRINTF("hip.id=%x hip.len=%d actlen=%u pad=%d\n",
1101 	    hip->id, be16toh(hip->len), actlen, pad);
1102 
1103 	switch (hip->id & USIE_HIP_MASK) {
1104 	case USIE_HIP_SYNC2H:
1105 		usie_if_cmd(sc, USIE_HIP_SYNC2M);
1106 		break;
1107 	case USIE_HIP_RESTR:
1108 		usb_callout_stop(&sc->sc_if_sync_ch);
1109 		break;
1110 	case USIE_HIP_UMTS:
1111 		lsi = (struct usie_lsi *)(
1112 		    sc->sc_status_temp + sizeof(struct usie_hip) + pad);
1113 
1114 		DPRINTF("lsi.proto=%x lsi.len=%d\n", lsi->proto,
1115 		    be16toh(lsi->len));
1116 
1117 		if (lsi->proto != USIE_LSI_UMTS)
1118 			break;
1119 
1120 		if (lsi->area == USIE_LSI_AREA_NO ||
1121 		    lsi->area == USIE_LSI_AREA_NODATA) {
1122 			device_printf(sc->sc_dev, "no service available\n");
1123 			break;
1124 		}
1125 		if (lsi->state == USIE_LSI_STATE_IDLE) {
1126 			DPRINTF("lsi.state=%x\n", lsi->state);
1127 			break;
1128 		}
1129 		DPRINTF("ctx=%x\n", hip->param);
1130 		sc->sc_txd.hip.param = hip->param;
1131 
1132 		sc->sc_net.addr_len = lsi->pdp_addr_len;
1133 		memcpy(&sc->sc_net.dns1_addr, &lsi->dns1_addr, 16);
1134 		memcpy(&sc->sc_net.dns2_addr, &lsi->dns2_addr, 16);
1135 		memcpy(sc->sc_net.pdp_addr, lsi->pdp_addr, 16);
1136 		memcpy(sc->sc_net.gw_addr, lsi->gw_addr, 16);
1137 		if_setflagbits(ifp, IFF_UP, 0);
1138 		if_setdrvflagbits(ifp, IFF_DRV_RUNNING, 0);
1139 
1140 		device_printf(sc->sc_dev, "IP Addr=%d.%d.%d.%d\n",
1141 		    *lsi->pdp_addr, *(lsi->pdp_addr + 1),
1142 		    *(lsi->pdp_addr + 2), *(lsi->pdp_addr + 3));
1143 		device_printf(sc->sc_dev, "Gateway Addr=%d.%d.%d.%d\n",
1144 		    *lsi->gw_addr, *(lsi->gw_addr + 1),
1145 		    *(lsi->gw_addr + 2), *(lsi->gw_addr + 3));
1146 		device_printf(sc->sc_dev, "Prim NS Addr=%d.%d.%d.%d\n",
1147 		    *lsi->dns1_addr, *(lsi->dns1_addr + 1),
1148 		    *(lsi->dns1_addr + 2), *(lsi->dns1_addr + 3));
1149 		device_printf(sc->sc_dev, "Scnd NS Addr=%d.%d.%d.%d\n",
1150 		    *lsi->dns2_addr, *(lsi->dns2_addr + 1),
1151 		    *(lsi->dns2_addr + 2), *(lsi->dns2_addr + 3));
1152 
1153 		usie_cns_req(sc, USIE_CNS_ID_RSSI, USIE_CNS_OB_RSSI);
1154 		break;
1155 
1156 	case USIE_HIP_RCGI:
1157 		/* ignore, workaround for sloppy windows */
1158 		break;
1159 	default:
1160 		DPRINTF("undefined msgid: %x\n", hip->id);
1161 		break;
1162 	}
1163 
1164 	mtx_unlock(&sc->sc_mtx);
1165 }
1166 
1167 static void
1168 usie_if_start(if_t ifp)
1169 {
1170 	struct usie_softc *sc = if_getsoftc(ifp);
1171 
1172 	if (!(if_getdrvflags(ifp) & IFF_DRV_RUNNING)) {
1173 		DPRINTF("Not running\n");
1174 		return;
1175 	}
1176 	mtx_lock(&sc->sc_mtx);
1177 	usbd_transfer_start(sc->sc_if_xfer[USIE_IF_TX]);
1178 	mtx_unlock(&sc->sc_mtx);
1179 
1180 	DPRINTFN(3, "interface started\n");
1181 }
1182 
1183 static int
1184 usie_if_output(if_t ifp, struct mbuf *m, const struct sockaddr *dst,
1185     struct route *ro)
1186 {
1187 	int err;
1188 
1189 	DPRINTF("proto=%x\n", dst->sa_family);
1190 
1191 	switch (dst->sa_family) {
1192 #ifdef INET6
1193 	case AF_INET6;
1194 	/* fall though */
1195 #endif
1196 	case AF_INET:
1197 		break;
1198 
1199 		/* silently drop dhclient packets */
1200 	case AF_UNSPEC:
1201 		m_freem(m);
1202 		return (0);
1203 
1204 		/* drop other packet types */
1205 	default:
1206 		m_freem(m);
1207 		return (EAFNOSUPPORT);
1208 	}
1209 
1210 	err = if_transmit(ifp, m);
1211 	if (err) {
1212 		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1213 		return (ENOBUFS);
1214 	}
1215 	if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
1216 
1217 	return (0);
1218 }
1219 
1220 static void
1221 usie_if_init(void *arg)
1222 {
1223 	struct usie_softc *sc = arg;
1224 	if_t ifp = sc->sc_ifp;
1225 	uint8_t i;
1226 
1227 	mtx_lock(&sc->sc_mtx);
1228 
1229 	/* write tx descriptor */
1230 	sc->sc_txd.hip.id = USIE_HIP_CTX;
1231 	sc->sc_txd.hip.param = 0;	/* init value */
1232 	sc->sc_txd.desc_type = htobe16(USIE_IP_TX);
1233 
1234 	for (i = 0; i != USIE_IF_N_XFER; i++)
1235 		usbd_xfer_set_stall(sc->sc_if_xfer[i]);
1236 
1237 	usbd_transfer_start(sc->sc_uc_xfer[USIE_HIP_IF][USIE_UC_RX]);
1238 	usbd_transfer_start(sc->sc_if_xfer[USIE_IF_STATUS]);
1239 	usbd_transfer_start(sc->sc_if_xfer[USIE_IF_RX]);
1240 
1241 	/* if not running, initiate the modem */
1242 	if (!(if_getdrvflags(ifp) & IFF_DRV_RUNNING))
1243 		usie_cns_req(sc, USIE_CNS_ID_INIT, USIE_CNS_OB_LINK_UPDATE);
1244 
1245 	mtx_unlock(&sc->sc_mtx);
1246 
1247 	DPRINTF("ifnet initialized\n");
1248 }
1249 
1250 static void
1251 usie_if_stop(struct usie_softc *sc)
1252 {
1253 	usb_callout_drain(&sc->sc_if_sync_ch);
1254 
1255 	mtx_lock(&sc->sc_mtx);
1256 
1257 	/* usie_cns_req() clears IFF_* flags */
1258 	usie_cns_req(sc, USIE_CNS_ID_STOP, USIE_CNS_OB_LINK_UPDATE);
1259 
1260 	usbd_transfer_stop(sc->sc_if_xfer[USIE_IF_TX]);
1261 	usbd_transfer_stop(sc->sc_if_xfer[USIE_IF_RX]);
1262 	usbd_transfer_stop(sc->sc_if_xfer[USIE_IF_STATUS]);
1263 
1264 	/* shutdown device */
1265 	usie_if_cmd(sc, USIE_HIP_DOWN);
1266 
1267 	mtx_unlock(&sc->sc_mtx);
1268 }
1269 
1270 static int
1271 usie_if_ioctl(if_t ifp, u_long cmd, caddr_t data)
1272 {
1273 	struct usie_softc *sc = if_getsoftc(ifp);
1274 	struct ieee80211req *ireq;
1275 	struct ieee80211req_sta_info si;
1276 	struct ifmediareq *ifmr;
1277 
1278 	switch (cmd) {
1279 	case SIOCSIFFLAGS:
1280 		if (if_getflags(ifp) & IFF_UP) {
1281 			if (!(if_getdrvflags(ifp) & IFF_DRV_RUNNING))
1282 				usie_if_init(sc);
1283 		} else {
1284 			if (if_getdrvflags(ifp) & IFF_DRV_RUNNING)
1285 				usie_if_stop(sc);
1286 		}
1287 		break;
1288 
1289 	case SIOCSIFCAP:
1290 		if (!(if_getdrvflags(ifp) & IFF_DRV_RUNNING)) {
1291 			device_printf(sc->sc_dev,
1292 			    "Connect to the network first.\n");
1293 			break;
1294 		}
1295 		mtx_lock(&sc->sc_mtx);
1296 		usie_cns_req(sc, USIE_CNS_ID_RSSI, USIE_CNS_OB_RSSI);
1297 		mtx_unlock(&sc->sc_mtx);
1298 		break;
1299 
1300 	case SIOCG80211:
1301 		ireq = (struct ieee80211req *)data;
1302 
1303 		if (ireq->i_type != IEEE80211_IOC_STA_INFO)
1304 			break;
1305 
1306 		memset(&si, 0, sizeof(si));
1307 		si.isi_len = sizeof(si);
1308 		/*
1309 		 * ifconfig expects RSSI in 0.5dBm units
1310 		 * relative to the noise floor.
1311 		 */
1312 		si.isi_rssi = 2 * sc->sc_rssi;
1313 		if (copyout(&si, (uint8_t *)ireq->i_data + 8,
1314 		    sizeof(struct ieee80211req_sta_info)))
1315 			DPRINTF("copyout failed\n");
1316 		DPRINTF("80211\n");
1317 		break;
1318 
1319 	case SIOCGIFMEDIA:		/* to fool ifconfig */
1320 		ifmr = (struct ifmediareq *)data;
1321 		ifmr->ifm_count = 1;
1322 		DPRINTF("media\n");
1323 		break;
1324 
1325 	case SIOCSIFADDR:
1326 		break;
1327 
1328 	default:
1329 		return (EINVAL);
1330 	}
1331 	return (0);
1332 }
1333 
1334 static int
1335 usie_do_request(struct usie_softc *sc, struct usb_device_request *req,
1336     void *data)
1337 {
1338 	int err = 0;
1339 	int ntries;
1340 
1341 	mtx_assert(&sc->sc_mtx, MA_OWNED);
1342 
1343 	for (ntries = 0; ntries != 10; ntries++) {
1344 		err = usbd_do_request(sc->sc_udev,
1345 		    &sc->sc_mtx, req, data);
1346 		if (err == 0)
1347 			break;
1348 
1349 		DPRINTF("Control request failed: %s %d/10\n",
1350 		    usbd_errstr(err), ntries);
1351 
1352 		usb_pause_mtx(&sc->sc_mtx, USB_MS_TO_TICKS(10));
1353 	}
1354 	return (err);
1355 }
1356 
1357 static int
1358 usie_if_cmd(struct usie_softc *sc, uint8_t cmd)
1359 {
1360 	struct usb_device_request req;
1361 	struct usie_hip msg;
1362 
1363 	msg.len = 0;
1364 	msg.id = cmd;
1365 	msg.param = 0;
1366 
1367 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
1368 	req.bRequest = UCDC_SEND_ENCAPSULATED_COMMAND;
1369 	USETW(req.wValue, 0);
1370 	USETW(req.wIndex, sc->sc_if_ifnum);
1371 	USETW(req.wLength, sizeof(msg));
1372 
1373 	DPRINTF("cmd=%x\n", cmd);
1374 
1375 	return (usie_do_request(sc, &req, &msg));
1376 }
1377 
1378 static void
1379 usie_cns_req(struct usie_softc *sc, uint32_t id, uint16_t obj)
1380 {
1381 	if_t ifp = sc->sc_ifp;
1382 	struct mbuf *m;
1383 	struct usb_xfer *xfer;
1384 	struct usie_hip *hip;
1385 	struct usie_cns *cns;
1386 	uint8_t *param;
1387 	uint8_t *tmp;
1388 	uint8_t cns_len;
1389 
1390 	m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
1391 	if (__predict_false(m == NULL)) {
1392 		DPRINTF("could not allocate mbuf\n");
1393 		if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1394 		return;
1395 	}
1396 	/* to align usie_hip{} on 32 bit */
1397 	m->m_data += 3;
1398 	param = mtod(m, uint8_t *);
1399 	*param++ = USIE_HIP_FRM_CHR;
1400 	hip = (struct usie_hip *)param;
1401 	cns = (struct usie_cns *)(hip + 1);
1402 
1403 	tmp = param + USIE_HIPCNS_MIN - 2;
1404 
1405 	switch (obj) {
1406 	case USIE_CNS_OB_LINK_UPDATE:
1407 		cns_len = 2;
1408 		cns->op = USIE_CNS_OP_SET;
1409 		*tmp++ = 1;		/* profile ID, always use 1 for now */
1410 		*tmp++ = id == USIE_CNS_ID_INIT ? 1 : 0;
1411 		break;
1412 
1413 	case USIE_CNS_OB_PROF_WRITE:
1414 		cns_len = 245;
1415 		cns->op = USIE_CNS_OP_SET;
1416 		*tmp++ = 1;		/* profile ID, always use 1 for now */
1417 		*tmp++ = 2;
1418 		memcpy(tmp, &sc->sc_net, 34);
1419 		memset(tmp + 35, 0, 245 - 36);
1420 		tmp += 243;
1421 		break;
1422 
1423 	case USIE_CNS_OB_RSSI:
1424 		cns_len = 0;
1425 		cns->op = USIE_CNS_OP_REQ;
1426 		break;
1427 
1428 	default:
1429 		DPRINTF("unsupported CnS object type\n");
1430 		return;
1431 	}
1432 	*tmp = USIE_HIP_FRM_CHR;
1433 
1434 	hip->len = htobe16(sizeof(struct usie_cns) + cns_len);
1435 	hip->id = USIE_HIP_CNS2M;
1436 	hip->param = 0;			/* none for CnS */
1437 
1438 	cns->obj = htobe16(obj);
1439 	cns->id = htobe32(id);
1440 	cns->len = cns_len;
1441 	cns->rsv0 = cns->rsv1 = 0;	/* always '0' */
1442 
1443 	param = (uint8_t *)(cns + 1);
1444 
1445 	DPRINTF("param: %16D\n", param, ":");
1446 
1447 	m->m_pkthdr.len = m->m_len = USIE_HIPCNS_MIN + cns_len + 2;
1448 
1449 	xfer = sc->sc_uc_xfer[USIE_HIP_IF][USIE_UC_TX];
1450 
1451 	if (usbd_xfer_get_priv(xfer) == NULL) {
1452 		usbd_xfer_set_priv(xfer, m);
1453 		usbd_transfer_start(xfer);
1454 	} else {
1455 		DPRINTF("Dropped CNS event\n");
1456 		m_freem(m);
1457 	}
1458 }
1459 
1460 static void
1461 usie_cns_rsp(struct usie_softc *sc, struct usie_cns *cns)
1462 {
1463 	if_t ifp = sc->sc_ifp;
1464 
1465 	DPRINTF("received CnS\n");
1466 
1467 	switch (be16toh(cns->obj)) {
1468 	case USIE_CNS_OB_LINK_UPDATE:
1469 		if (be32toh(cns->id) & USIE_CNS_ID_INIT)
1470 			usie_if_sync_to(sc);
1471 		else if (be32toh(cns->id) & USIE_CNS_ID_STOP) {
1472 			if_setflagbits(ifp, 0, IFF_UP);
1473 			if_setdrvflagbits(ifp, 0,
1474 			    IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
1475 		} else
1476 			DPRINTF("undefined link update\n");
1477 		break;
1478 
1479 	case USIE_CNS_OB_RSSI:
1480 		sc->sc_rssi = be16toh(*(int16_t *)(cns + 1));
1481 		if (sc->sc_rssi <= 0)
1482 			device_printf(sc->sc_dev, "No signal\n");
1483 		else {
1484 			device_printf(sc->sc_dev, "RSSI=%ddBm\n",
1485 			    sc->sc_rssi - 110);
1486 		}
1487 		break;
1488 
1489 	case USIE_CNS_OB_PROF_WRITE:
1490 		break;
1491 
1492 	case USIE_CNS_OB_PDP_READ:
1493 		break;
1494 
1495 	default:
1496 		DPRINTF("undefined CnS\n");
1497 		break;
1498 	}
1499 }
1500 
1501 static void
1502 usie_hip_rsp(struct usie_softc *sc, uint8_t *rsp, uint32_t len)
1503 {
1504 	struct usie_hip *hip;
1505 	struct usie_cns *cns;
1506 	uint32_t i;
1507 	uint32_t j;
1508 	uint32_t off;
1509 	uint8_t tmp[USIE_HIPCNS_MAX] __aligned(4);
1510 
1511 	for (off = 0; (off + USIE_HIPCNS_MIN) <= len; off++) {
1512 		uint8_t pad;
1513 
1514 		while ((off < len) && (rsp[off] == USIE_HIP_FRM_CHR))
1515 			off++;
1516 
1517 		/* Unstuff the bytes */
1518 		for (i = j = 0; ((i + off) < len) &&
1519 		    (j < USIE_HIPCNS_MAX); i++) {
1520 			if (rsp[i + off] == USIE_HIP_FRM_CHR)
1521 				break;
1522 
1523 			if (rsp[i + off] == USIE_HIP_ESC_CHR) {
1524 				if ((i + off + 1) >= len)
1525 					break;
1526 				tmp[j++] = rsp[i++ + off + 1] ^ 0x20;
1527 			} else {
1528 				tmp[j++] = rsp[i + off];
1529 			}
1530 		}
1531 
1532 		off += i;
1533 
1534 		DPRINTF("frame len=%d\n", j);
1535 
1536 		if (j < sizeof(struct usie_hip)) {
1537 			DPRINTF("too little data\n");
1538 			break;
1539 		}
1540 		/*
1541 		 * Make sure we are not reading the stack if something
1542 		 * is wrong.
1543 		 */
1544 		memset(tmp + j, 0, sizeof(tmp) - j);
1545 
1546 		hip = (struct usie_hip *)tmp;
1547 
1548 		DPRINTF("hip: len=%d msgID=%02x, param=%02x\n",
1549 		    be16toh(hip->len), hip->id, hip->param);
1550 
1551 		pad = (hip->id & USIE_HIP_PAD) ? 1 : 0;
1552 
1553 		if ((hip->id & USIE_HIP_MASK) == USIE_HIP_CNS2H) {
1554 			cns = (struct usie_cns *)(((uint8_t *)(hip + 1)) + pad);
1555 
1556 			if (j < (sizeof(struct usie_cns) +
1557 			    sizeof(struct usie_hip) + pad)) {
1558 				DPRINTF("too little data\n");
1559 				break;
1560 			}
1561 			DPRINTF("cns: obj=%04x, op=%02x, rsv0=%02x, "
1562 			    "app=%08x, rsv1=%02x, len=%d\n",
1563 			    be16toh(cns->obj), cns->op, cns->rsv0,
1564 			    be32toh(cns->id), cns->rsv1, cns->len);
1565 
1566 			if (cns->op & USIE_CNS_OP_ERR)
1567 				DPRINTF("CnS error response\n");
1568 			else
1569 				usie_cns_rsp(sc, cns);
1570 
1571 			i = sizeof(struct usie_hip) + pad + sizeof(struct usie_cns);
1572 			j = cns->len;
1573 		} else {
1574 			i = sizeof(struct usie_hip) + pad;
1575 			j = be16toh(hip->len);
1576 		}
1577 #ifdef	USB_DEBUG
1578 		if (usie_debug == 0)
1579 			continue;
1580 
1581 		while (i < USIE_HIPCNS_MAX && j > 0) {
1582 			DPRINTF("param[0x%02x] = 0x%02x\n", i, tmp[i]);
1583 			i++;
1584 			j--;
1585 		}
1586 #endif
1587 	}
1588 }
1589 
1590 static int
1591 usie_driver_loaded(struct module *mod, int what, void *arg)
1592 {
1593 	switch (what) {
1594 	case MOD_LOAD:
1595 		/* register autoinstall handler */
1596 		usie_etag = EVENTHANDLER_REGISTER(usb_dev_configured,
1597 		    usie_autoinst, NULL, EVENTHANDLER_PRI_ANY);
1598 		break;
1599 	case MOD_UNLOAD:
1600 		EVENTHANDLER_DEREGISTER(usb_dev_configured, usie_etag);
1601 		break;
1602 	default:
1603 		return (EOPNOTSUPP);
1604 	}
1605 	return (0);
1606 }
1607