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