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