1/* $OpenBSD: if_upgt.c,v 1.35 2008/04/16 18:32:15 damien Exp $ */
2/* $FreeBSD: head/sys/dev/usb/wlan/if_upgt.c 212131 2010-09-02 03:50:11Z thompsa $ */
3
4/*
5 * Copyright (c) 2007 Marcus Glocker <mglocker@openbsd.org>
6 *
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 */
19
20#include <sys/param.h>
21#include <sys/systm.h>
22#include <sys/kernel.h>
23#include <sys/endian.h>
24#include <sys/firmware.h>
25#include <sys/linker.h>
26#include <sys/mbuf.h>
27#include <sys/malloc.h>
28#include <sys/module.h>
29#include <sys/socket.h>
30#include <sys/sockio.h>
31#include <sys/sysctl.h>
32
33#include <net/if.h>
34#include <net/if_arp.h>
35#include <net/ethernet.h>
36#include <net/if_dl.h>
37#include <net/if_media.h>
38#include <net/if_types.h>
39
40#include <sys/bus.h>
41#include <machine/bus.h>
42
43#include <net80211/ieee80211_var.h>
44#include <net80211/ieee80211_phy.h>
45#include <net80211/ieee80211_radiotap.h>
46#include <net80211/ieee80211_regdomain.h>
47
48#include <net/bpf.h>
49
50#include <dev/usb/usb.h>
51#include <dev/usb/usbdi.h>
52#include "usbdevs.h"
53
54#include <dev/usb/wlan/if_upgtvar.h>
55
56/*
57 * Driver for the USB PrismGT devices.
58 *
59 * For now just USB 2.0 devices with the GW3887 chipset are supported.
60 * The driver has been written based on the firmware version 2.13.1.0_LM87.
61 *
62 * TODO's:
63 * - MONITOR mode test.
64 * - Add HOSTAP mode.
65 * - Add IBSS mode.
66 * - Support the USB 1.0 devices (NET2280, ISL3880, ISL3886 chipsets).
67 *
68 * Parts of this driver has been influenced by reading the p54u driver
69 * written by Jean-Baptiste Note <jean-baptiste.note@m4x.org> and
70 * Sebastien Bourdeauducq <lekernel@prism54.org>.
71 */
72
73SYSCTL_NODE(_hw, OID_AUTO, upgt, CTLFLAG_RD, 0,
74 "USB PrismGT GW3887 driver parameters");
75
76#ifdef UPGT_DEBUG
77int upgt_debug = 0;
78SYSCTL_INT(_hw_upgt, OID_AUTO, debug, CTLFLAG_RW, &upgt_debug,
79 0, "control debugging printfs");
80TUNABLE_INT("hw.upgt.debug", &upgt_debug);
81enum {
82 UPGT_DEBUG_XMIT = 0x00000001, /* basic xmit operation */
83 UPGT_DEBUG_RECV = 0x00000002, /* basic recv operation */
84 UPGT_DEBUG_RESET = 0x00000004, /* reset processing */
85 UPGT_DEBUG_INTR = 0x00000008, /* INTR */
86 UPGT_DEBUG_TX_PROC = 0x00000010, /* tx ISR proc */
87 UPGT_DEBUG_RX_PROC = 0x00000020, /* rx ISR proc */
88 UPGT_DEBUG_STATE = 0x00000040, /* 802.11 state transitions */
89 UPGT_DEBUG_STAT = 0x00000080, /* statistic */
90 UPGT_DEBUG_FW = 0x00000100, /* firmware */
91 UPGT_DEBUG_ANY = 0xffffffff
92};
93#define DPRINTF(sc, m, fmt, ...) do { \
94 if (sc->sc_debug & (m)) \
95 printf(fmt, __VA_ARGS__); \
96} while (0)
97#else
98#define DPRINTF(sc, m, fmt, ...) do { \
99 (void) sc; \
100} while (0)
101#endif
102
103/*
104 * Prototypes.
105 */
106static device_probe_t upgt_match;
107static device_attach_t upgt_attach;
108static device_detach_t upgt_detach;
109static int upgt_alloc_tx(struct upgt_softc *);
110static int upgt_alloc_rx(struct upgt_softc *);
111static int upgt_device_reset(struct upgt_softc *);
112static void upgt_bulk_tx(struct upgt_softc *, struct upgt_data *);
113static int upgt_fw_verify(struct upgt_softc *);
114static int upgt_mem_init(struct upgt_softc *);
115static int upgt_fw_load(struct upgt_softc *);
116static int upgt_fw_copy(const uint8_t *, char *, int);
117static uint32_t upgt_crc32_le(const void *, size_t);
118static struct mbuf *
119 upgt_rxeof(struct usb_xfer *, struct upgt_data *, int *);
120static struct mbuf *
121 upgt_rx(struct upgt_softc *, uint8_t *, int, int *);
122static void upgt_txeof(struct usb_xfer *, struct upgt_data *);
123static int upgt_eeprom_read(struct upgt_softc *);
124static int upgt_eeprom_parse(struct upgt_softc *);
125static void upgt_eeprom_parse_hwrx(struct upgt_softc *, uint8_t *);
126static void upgt_eeprom_parse_freq3(struct upgt_softc *, uint8_t *, int);
127static void upgt_eeprom_parse_freq4(struct upgt_softc *, uint8_t *, int);
128static void upgt_eeprom_parse_freq6(struct upgt_softc *, uint8_t *, int);
129static uint32_t upgt_chksum_le(const uint32_t *, size_t);
130static void upgt_tx_done(struct upgt_softc *, uint8_t *);
131static void upgt_init(void *);
132static void upgt_init_locked(struct upgt_softc *);
133static int upgt_ioctl(struct ifnet *, u_long, caddr_t);
134static void upgt_start(struct ifnet *);
135static int upgt_raw_xmit(struct ieee80211_node *, struct mbuf *,
136 const struct ieee80211_bpf_params *);
137static void upgt_scan_start(struct ieee80211com *);
138static void upgt_scan_end(struct ieee80211com *);
139static void upgt_set_channel(struct ieee80211com *);
140static struct ieee80211vap *upgt_vap_create(struct ieee80211com *,
141 const char name[IFNAMSIZ], int unit, int opmode,
142 int flags, const uint8_t bssid[IEEE80211_ADDR_LEN],
143 const uint8_t mac[IEEE80211_ADDR_LEN]);
144static void upgt_vap_delete(struct ieee80211vap *);
145static void upgt_update_mcast(struct ifnet *);
146static uint8_t upgt_rx_rate(struct upgt_softc *, const int);
147static void upgt_set_multi(void *);
148static void upgt_stop(struct upgt_softc *);
149static void upgt_setup_rates(struct ieee80211vap *, struct ieee80211com *);
150static int upgt_set_macfilter(struct upgt_softc *, uint8_t);
151static int upgt_newstate(struct ieee80211vap *, enum ieee80211_state, int);
152static void upgt_set_chan(struct upgt_softc *, struct ieee80211_channel *);
153static void upgt_set_led(struct upgt_softc *, int);
154static void upgt_set_led_blink(void *);
155static void upgt_get_stats(struct upgt_softc *);
156static void upgt_mem_free(struct upgt_softc *, uint32_t);
157static uint32_t upgt_mem_alloc(struct upgt_softc *);
158static void upgt_free_tx(struct upgt_softc *);
159static void upgt_free_rx(struct upgt_softc *);
160static void upgt_watchdog(void *);
161static void upgt_abort_xfers(struct upgt_softc *);
162static void upgt_abort_xfers_locked(struct upgt_softc *);
163static void upgt_sysctl_node(struct upgt_softc *);
164static struct upgt_data *
165 upgt_getbuf(struct upgt_softc *);
166static struct upgt_data *
167 upgt_gettxbuf(struct upgt_softc *);
168static int upgt_tx_start(struct upgt_softc *, struct mbuf *,
169 struct ieee80211_node *, struct upgt_data *);
170
171static const char *upgt_fwname = "upgt-gw3887";
172
173static const struct usb_device_id upgt_devs_2[] = {
174#define UPGT_DEV(v,p) { USB_VP(USB_VENDOR_##v, USB_PRODUCT_##v##_##p) }
175 /* version 2 devices */
176 UPGT_DEV(ACCTON, PRISM_GT),
177 UPGT_DEV(BELKIN, F5D7050),
178 UPGT_DEV(CISCOLINKSYS, WUSB54AG),
179 UPGT_DEV(CONCEPTRONIC, PRISM_GT),
180 UPGT_DEV(DELL, PRISM_GT_1),
181 UPGT_DEV(DELL, PRISM_GT_2),
182 UPGT_DEV(FSC, E5400),
183 UPGT_DEV(GLOBESPAN, PRISM_GT_1),
184 UPGT_DEV(GLOBESPAN, PRISM_GT_2),
185 UPGT_DEV(INTERSIL, PRISM_GT),
186 UPGT_DEV(SMC, 2862WG),
187 UPGT_DEV(USR, USR5422),
188 UPGT_DEV(WISTRONNEWEB, UR045G),
189 UPGT_DEV(XYRATEX, PRISM_GT_1),
190 UPGT_DEV(XYRATEX, PRISM_GT_2),
191 UPGT_DEV(ZCOM, XG703A),
192 UPGT_DEV(ZCOM, XM142)
193};
194
195static usb_callback_t upgt_bulk_rx_callback;
196static usb_callback_t upgt_bulk_tx_callback;
197
198static const struct usb_config upgt_config[UPGT_N_XFERS] = {
199 [UPGT_BULK_TX] = {
200 .type = UE_BULK,
201 .endpoint = UE_ADDR_ANY,
202 .direction = UE_DIR_OUT,
203 .bufsize = MCLBYTES,
204 .flags = {
205 .ext_buffer = 1,
206 .force_short_xfer = 1,
207 .pipe_bof = 1
208 },
209 .callback = upgt_bulk_tx_callback,
210 .timeout = UPGT_USB_TIMEOUT, /* ms */
211 },
212 [UPGT_BULK_RX] = {
213 .type = UE_BULK,
214 .endpoint = UE_ADDR_ANY,
215 .direction = UE_DIR_IN,
216 .bufsize = MCLBYTES,
217 .flags = {
218 .ext_buffer = 1,
219 .pipe_bof = 1,
220 .short_xfer_ok = 1
221 },
222 .callback = upgt_bulk_rx_callback,
223 },
224};
225
226static int
227upgt_match(device_t dev)
228{
229 struct usb_attach_arg *uaa = device_get_ivars(dev);
230
231 if (uaa->usb_mode != USB_MODE_HOST)
232 return (ENXIO);
233 if (uaa->info.bConfigIndex != UPGT_CONFIG_INDEX)
234 return (ENXIO);
235 if (uaa->info.bIfaceIndex != UPGT_IFACE_INDEX)
236 return (ENXIO);
237
238 return (usbd_lookup_id_by_uaa(upgt_devs_2, sizeof(upgt_devs_2), uaa));
239}
240
241static int
242upgt_attach(device_t dev)
243{
244 int error;
245 struct ieee80211com *ic;
246 struct ifnet *ifp;
247 struct upgt_softc *sc = device_get_softc(dev);
248 struct usb_attach_arg *uaa = device_get_ivars(dev);
249 uint8_t bands, iface_index = UPGT_IFACE_INDEX;
250
251 sc->sc_dev = dev;
252 sc->sc_udev = uaa->device;
253#ifdef UPGT_DEBUG
254 sc->sc_debug = upgt_debug;
255#endif
256 device_set_usb_desc(dev);
257
258 mtx_init(&sc->sc_mtx, device_get_nameunit(sc->sc_dev), MTX_NETWORK_LOCK,
259 MTX_DEF);
260 callout_init(&sc->sc_led_ch, 0);
261 callout_init(&sc->sc_watchdog_ch, 0);
262
263 /* Allocate TX and RX xfers. */
264 error = upgt_alloc_tx(sc);
265 if (error)
266 goto fail1;
267 error = upgt_alloc_rx(sc);
268 if (error)
269 goto fail2;
270
271 error = usbd_transfer_setup(uaa->device, &iface_index, sc->sc_xfer,
272 upgt_config, UPGT_N_XFERS, sc, &sc->sc_mtx);
273 if (error) {
274 device_printf(dev, "could not allocate USB transfers, "
275 "err=%s\n", usbd_errstr(error));
276 goto fail3;
277 }
278
279 ifp = sc->sc_ifp = if_alloc(IFT_IEEE80211);
280 if (ifp == NULL) {
281 device_printf(dev, "can not if_alloc()\n");
282 goto fail4;
283 }
284
285 /* Initialize the device. */
286 error = upgt_device_reset(sc);
287 if (error)
288 goto fail5;
289 /* Verify the firmware. */
290 error = upgt_fw_verify(sc);
291 if (error)
292 goto fail5;
293 /* Calculate device memory space. */
294 if (sc->sc_memaddr_frame_start == 0 || sc->sc_memaddr_frame_end == 0) {
295 device_printf(dev,
296 "could not find memory space addresses on FW\n");
297 error = EIO;
298 goto fail5;
299 }
300 sc->sc_memaddr_frame_end -= UPGT_MEMSIZE_RX + 1;
301 sc->sc_memaddr_rx_start = sc->sc_memaddr_frame_end + 1;
302
303 DPRINTF(sc, UPGT_DEBUG_FW, "memory address frame start=0x%08x\n",
304 sc->sc_memaddr_frame_start);
305 DPRINTF(sc, UPGT_DEBUG_FW, "memory address frame end=0x%08x\n",
306 sc->sc_memaddr_frame_end);
307 DPRINTF(sc, UPGT_DEBUG_FW, "memory address rx start=0x%08x\n",
308 sc->sc_memaddr_rx_start);
309
310 upgt_mem_init(sc);
311
312 /* Load the firmware. */
313 error = upgt_fw_load(sc);
314 if (error)
315 goto fail5;
316
317 /* Read the whole EEPROM content and parse it. */
318 error = upgt_eeprom_read(sc);
319 if (error)
320 goto fail5;
321 error = upgt_eeprom_parse(sc);
322 if (error)
323 goto fail5;
324
325 /* all works related with the device have done here. */
326 upgt_abort_xfers(sc);
327
328 /* Setup the 802.11 device. */
329 ifp->if_softc = sc;
330 if_initname(ifp, "upgt", device_get_unit(sc->sc_dev));
331 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
332 ifp->if_init = upgt_init;
333 ifp->if_ioctl = upgt_ioctl;
334 ifp->if_start = upgt_start;
335 IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
336 IFQ_SET_READY(&ifp->if_snd);
337
338 ic = ifp->if_l2com;
339 ic->ic_ifp = ifp;
340 ic->ic_phytype = IEEE80211_T_OFDM; /* not only, but not used */
341 ic->ic_opmode = IEEE80211_M_STA;
342 /* set device capabilities */
343 ic->ic_caps =
344 IEEE80211_C_STA /* station mode */
345 | IEEE80211_C_MONITOR /* monitor mode */
346 | IEEE80211_C_SHPREAMBLE /* short preamble supported */
347 | IEEE80211_C_SHSLOT /* short slot time supported */
348 | IEEE80211_C_BGSCAN /* capable of bg scanning */
349 | IEEE80211_C_WPA /* 802.11i */
350 ;
351
352 bands = 0;
353 setbit(&bands, IEEE80211_MODE_11B);
354 setbit(&bands, IEEE80211_MODE_11G);
355 ieee80211_init_channels(ic, NULL, &bands);
356
357 ieee80211_ifattach(ic, sc->sc_myaddr);
358 ic->ic_raw_xmit = upgt_raw_xmit;
359 ic->ic_scan_start = upgt_scan_start;
360 ic->ic_scan_end = upgt_scan_end;
361 ic->ic_set_channel = upgt_set_channel;
362
363 ic->ic_vap_create = upgt_vap_create;
364 ic->ic_vap_delete = upgt_vap_delete;
365 ic->ic_update_mcast = upgt_update_mcast;
366
367 ieee80211_radiotap_attach(ic,
368 &sc->sc_txtap.wt_ihdr, sizeof(sc->sc_txtap),
369 UPGT_TX_RADIOTAP_PRESENT,
370 &sc->sc_rxtap.wr_ihdr, sizeof(sc->sc_rxtap),
371 UPGT_RX_RADIOTAP_PRESENT);
372
373 upgt_sysctl_node(sc);
374
375 if (bootverbose)
376 ieee80211_announce(ic);
377
378 return (0);
379
380fail5: if_free(ifp);
381fail4: usbd_transfer_unsetup(sc->sc_xfer, UPGT_N_XFERS);
382fail3: upgt_free_rx(sc);
383fail2: upgt_free_tx(sc);
384fail1: mtx_destroy(&sc->sc_mtx);
385
386 return (error);
387}
388
389static void
390upgt_txeof(struct usb_xfer *xfer, struct upgt_data *data)
391{
392 struct upgt_softc *sc = usbd_xfer_softc(xfer);
393 struct ifnet *ifp = sc->sc_ifp;
394 struct mbuf *m;
395
396 UPGT_ASSERT_LOCKED(sc);
397
398 /*
399 * Do any tx complete callback. Note this must be done before releasing
400 * the node reference.
401 */
402 if (data->m) {
403 m = data->m;
404 if (m->m_flags & M_TXCB) {
405 /* XXX status? */
406 ieee80211_process_callback(data->ni, m, 0);
407 }
408 m_freem(m);
409 data->m = NULL;
410 }
411 if (data->ni) {
412 ieee80211_free_node(data->ni);
413 data->ni = NULL;
414 }
415 ifp->if_opackets++;
416}
417
418static void
419upgt_get_stats(struct upgt_softc *sc)
420{
421 struct upgt_data *data_cmd;
422 struct upgt_lmac_mem *mem;
423 struct upgt_lmac_stats *stats;
424
425 data_cmd = upgt_getbuf(sc);
426 if (data_cmd == NULL) {
427 device_printf(sc->sc_dev, "%s: out of buffer.\n", __func__);
428 return;
429 }
430
431 /*
432 * Transmit the URB containing the CMD data.
433 */
434 bzero(data_cmd->buf, MCLBYTES);
435
436 mem = (struct upgt_lmac_mem *)data_cmd->buf;
437 mem->addr = htole32(sc->sc_memaddr_frame_start +
438 UPGT_MEMSIZE_FRAME_HEAD);
439
440 stats = (struct upgt_lmac_stats *)(mem + 1);
441
442 stats->header1.flags = 0;
443 stats->header1.type = UPGT_H1_TYPE_CTRL;
444 stats->header1.len = htole16(
445 sizeof(struct upgt_lmac_stats) - sizeof(struct upgt_lmac_header));
446
447 stats->header2.reqid = htole32(sc->sc_memaddr_frame_start);
448 stats->header2.type = htole16(UPGT_H2_TYPE_STATS);
449 stats->header2.flags = 0;
450
451 data_cmd->buflen = sizeof(*mem) + sizeof(*stats);
452
453 mem->chksum = upgt_chksum_le((uint32_t *)stats,
454 data_cmd->buflen - sizeof(*mem));
455
456 upgt_bulk_tx(sc, data_cmd);
457}
458
459static int
460upgt_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
461{
462 struct upgt_softc *sc = ifp->if_softc;
463 struct ieee80211com *ic = ifp->if_l2com;
464 struct ifreq *ifr = (struct ifreq *) data;
465 int error = 0, startall = 0;
466
467 switch (cmd) {
468 case SIOCSIFFLAGS:
469 if (ifp->if_flags & IFF_UP) {
470 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
471 if ((ifp->if_flags ^ sc->sc_if_flags) &
472 (IFF_ALLMULTI | IFF_PROMISC))
473 upgt_set_multi(sc);
474 } else {
475 upgt_init(sc);
476 startall = 1;
477 }
478 } else {
479 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
480 upgt_stop(sc);
481 }
482 sc->sc_if_flags = ifp->if_flags;
483 if (startall)
484 ieee80211_start_all(ic);
485 break;
486 case SIOCGIFMEDIA:
487 error = ifmedia_ioctl(ifp, ifr, &ic->ic_media, cmd);
488 break;
489 case SIOCGIFADDR:
490 error = ether_ioctl(ifp, cmd, data);
491 break;
492 default:
493 error = EINVAL;
494 break;
495 }
496 return error;
497}
498
499static void
500upgt_stop_locked(struct upgt_softc *sc)
501{
502 struct ifnet *ifp = sc->sc_ifp;
503
504 UPGT_ASSERT_LOCKED(sc);
505
506 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
507 upgt_set_macfilter(sc, IEEE80211_S_INIT);
508 upgt_abort_xfers_locked(sc);
509}
510
511static void
512upgt_stop(struct upgt_softc *sc)
513{
514 struct ifnet *ifp = sc->sc_ifp;
515
516 UPGT_LOCK(sc);
517 upgt_stop_locked(sc);
518 UPGT_UNLOCK(sc);
519
520 /* device down */
521 sc->sc_tx_timer = 0;
522 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
523 sc->sc_flags &= ~UPGT_FLAG_INITDONE;
524}
525
526static void
527upgt_set_led(struct upgt_softc *sc, int action)
528{
529 struct upgt_data *data_cmd;
530 struct upgt_lmac_mem *mem;
531 struct upgt_lmac_led *led;
532
533 data_cmd = upgt_getbuf(sc);
534 if (data_cmd == NULL) {
535 device_printf(sc->sc_dev, "%s: out of buffers.\n", __func__);
536 return;
537 }
538
539 /*
540 * Transmit the URB containing the CMD data.
541 */
542 bzero(data_cmd->buf, MCLBYTES);
543
544 mem = (struct upgt_lmac_mem *)data_cmd->buf;
545 mem->addr = htole32(sc->sc_memaddr_frame_start +
546 UPGT_MEMSIZE_FRAME_HEAD);
547
548 led = (struct upgt_lmac_led *)(mem + 1);
549
550 led->header1.flags = UPGT_H1_FLAGS_TX_NO_CALLBACK;
551 led->header1.type = UPGT_H1_TYPE_CTRL;
552 led->header1.len = htole16(
553 sizeof(struct upgt_lmac_led) -
554 sizeof(struct upgt_lmac_header));
555
556 led->header2.reqid = htole32(sc->sc_memaddr_frame_start);
557 led->header2.type = htole16(UPGT_H2_TYPE_LED);
558 led->header2.flags = 0;
559
560 switch (action) {
561 case UPGT_LED_OFF:
562 led->mode = htole16(UPGT_LED_MODE_SET);
563 led->action_fix = 0;
564 led->action_tmp = htole16(UPGT_LED_ACTION_OFF);
565 led->action_tmp_dur = 0;
566 break;
567 case UPGT_LED_ON:
568 led->mode = htole16(UPGT_LED_MODE_SET);
569 led->action_fix = 0;
570 led->action_tmp = htole16(UPGT_LED_ACTION_ON);
571 led->action_tmp_dur = 0;
572 break;
573 case UPGT_LED_BLINK:
574 if (sc->sc_state != IEEE80211_S_RUN) {
575 STAILQ_INSERT_TAIL(&sc->sc_tx_inactive, data_cmd, next);
576 return;
577 }
578 if (sc->sc_led_blink) {
579 /* previous blink was not finished */
580 STAILQ_INSERT_TAIL(&sc->sc_tx_inactive, data_cmd, next);
581 return;
582 }
583 led->mode = htole16(UPGT_LED_MODE_SET);
584 led->action_fix = htole16(UPGT_LED_ACTION_OFF);
585 led->action_tmp = htole16(UPGT_LED_ACTION_ON);
586 led->action_tmp_dur = htole16(UPGT_LED_ACTION_TMP_DUR);
587 /* lock blink */
588 sc->sc_led_blink = 1;
589 callout_reset(&sc->sc_led_ch, hz, upgt_set_led_blink, sc);
590 break;
591 default:
592 STAILQ_INSERT_TAIL(&sc->sc_tx_inactive, data_cmd, next);
593 return;
594 }
595
596 data_cmd->buflen = sizeof(*mem) + sizeof(*led);
597
598 mem->chksum = upgt_chksum_le((uint32_t *)led,
599 data_cmd->buflen - sizeof(*mem));
600
601 upgt_bulk_tx(sc, data_cmd);
602}
603
604static void
605upgt_set_led_blink(void *arg)
606{
607 struct upgt_softc *sc = arg;
608
609 /* blink finished, we are ready for a next one */
610 sc->sc_led_blink = 0;
611}
612
613static void
614upgt_init(void *priv)
615{
616 struct upgt_softc *sc = priv;
617 struct ifnet *ifp = sc->sc_ifp;
618 struct ieee80211com *ic = ifp->if_l2com;
619
620 UPGT_LOCK(sc);
621 upgt_init_locked(sc);
622 UPGT_UNLOCK(sc);
623
624 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
625 ieee80211_start_all(ic); /* start all vap's */
626}
627
628static void
629upgt_init_locked(struct upgt_softc *sc)
630{
631 struct ifnet *ifp = sc->sc_ifp;
632
633 UPGT_ASSERT_LOCKED(sc);
634
635 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
636 upgt_stop_locked(sc);
637
638 usbd_transfer_start(sc->sc_xfer[UPGT_BULK_RX]);
639
640 (void)upgt_set_macfilter(sc, IEEE80211_S_SCAN);
641
642 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
643 ifp->if_drv_flags |= IFF_DRV_RUNNING;
644 sc->sc_flags |= UPGT_FLAG_INITDONE;
645
646 callout_reset(&sc->sc_watchdog_ch, hz, upgt_watchdog, sc);
647}
648
649static int
650upgt_set_macfilter(struct upgt_softc *sc, uint8_t state)
651{
652 struct ifnet *ifp = sc->sc_ifp;
653 struct ieee80211com *ic = ifp->if_l2com;
654 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
655 struct ieee80211_node *ni;
656 struct upgt_data *data_cmd;
657 struct upgt_lmac_mem *mem;
658 struct upgt_lmac_filter *filter;
659 uint8_t broadcast[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
660
661 UPGT_ASSERT_LOCKED(sc);
662
663 data_cmd = upgt_getbuf(sc);
664 if (data_cmd == NULL) {
665 device_printf(sc->sc_dev, "out of TX buffers.\n");
666 return (ENOBUFS);
667 }
668
669 /*
670 * Transmit the URB containing the CMD data.
671 */
672 bzero(data_cmd->buf, MCLBYTES);
673
674 mem = (struct upgt_lmac_mem *)data_cmd->buf;
675 mem->addr = htole32(sc->sc_memaddr_frame_start +
676 UPGT_MEMSIZE_FRAME_HEAD);
677
678 filter = (struct upgt_lmac_filter *)(mem + 1);
679
680 filter->header1.flags = UPGT_H1_FLAGS_TX_NO_CALLBACK;
681 filter->header1.type = UPGT_H1_TYPE_CTRL;
682 filter->header1.len = htole16(
683 sizeof(struct upgt_lmac_filter) -
684 sizeof(struct upgt_lmac_header));
685
686 filter->header2.reqid = htole32(sc->sc_memaddr_frame_start);
687 filter->header2.type = htole16(UPGT_H2_TYPE_MACFILTER);
688 filter->header2.flags = 0;
689
690 switch (state) {
691 case IEEE80211_S_INIT:
692 DPRINTF(sc, UPGT_DEBUG_STATE, "%s: set MAC filter to INIT\n",
693 __func__);
694 filter->type = htole16(UPGT_FILTER_TYPE_RESET);
695 break;
696 case IEEE80211_S_SCAN:
697 DPRINTF(sc, UPGT_DEBUG_STATE,
698 "set MAC filter to SCAN (bssid %s)\n",
699 ether_sprintf(broadcast));
700 filter->type = htole16(UPGT_FILTER_TYPE_NONE);
701 IEEE80211_ADDR_COPY(filter->dst, sc->sc_myaddr);
702 IEEE80211_ADDR_COPY(filter->src, broadcast);
703 filter->unknown1 = htole16(UPGT_FILTER_UNKNOWN1);
704 filter->rxaddr = htole32(sc->sc_memaddr_rx_start);
705 filter->unknown2 = htole16(UPGT_FILTER_UNKNOWN2);
706 filter->rxhw = htole32(sc->sc_eeprom_hwrx);
707 filter->unknown3 = htole16(UPGT_FILTER_UNKNOWN3);
708 break;
709 case IEEE80211_S_RUN:
710 ni = ieee80211_ref_node(vap->iv_bss);
711 /* XXX monitor mode isn't tested yet. */
712 if (vap->iv_opmode == IEEE80211_M_MONITOR) {
713 filter->type = htole16(UPGT_FILTER_TYPE_MONITOR);
714 IEEE80211_ADDR_COPY(filter->dst, sc->sc_myaddr);
715 IEEE80211_ADDR_COPY(filter->src, ni->ni_bssid);
716 filter->unknown1 = htole16(UPGT_FILTER_MONITOR_UNKNOWN1);
717 filter->rxaddr = htole32(sc->sc_memaddr_rx_start);
718 filter->unknown2 = htole16(UPGT_FILTER_MONITOR_UNKNOWN2);
719 filter->rxhw = htole32(sc->sc_eeprom_hwrx);
720 filter->unknown3 = htole16(UPGT_FILTER_MONITOR_UNKNOWN3);
721 } else {
722 DPRINTF(sc, UPGT_DEBUG_STATE,
723 "set MAC filter to RUN (bssid %s)\n",
724 ether_sprintf(ni->ni_bssid));
725 filter->type = htole16(UPGT_FILTER_TYPE_STA);
726 IEEE80211_ADDR_COPY(filter->dst, sc->sc_myaddr);
727 IEEE80211_ADDR_COPY(filter->src, ni->ni_bssid);
728 filter->unknown1 = htole16(UPGT_FILTER_UNKNOWN1);
729 filter->rxaddr = htole32(sc->sc_memaddr_rx_start);
730 filter->unknown2 = htole16(UPGT_FILTER_UNKNOWN2);
731 filter->rxhw = htole32(sc->sc_eeprom_hwrx);
732 filter->unknown3 = htole16(UPGT_FILTER_UNKNOWN3);
733 }
734 ieee80211_free_node(ni);
735 break;
736 default:
737 device_printf(sc->sc_dev,
738 "MAC filter does not know that state\n");
739 break;
740 }
741
742 data_cmd->buflen = sizeof(*mem) + sizeof(*filter);
743
744 mem->chksum = upgt_chksum_le((uint32_t *)filter,
745 data_cmd->buflen - sizeof(*mem));
746
747 upgt_bulk_tx(sc, data_cmd);
748
749 return (0);
750}
751
752static void
753upgt_setup_rates(struct ieee80211vap *vap, struct ieee80211com *ic)
754{
755 struct ifnet *ifp = ic->ic_ifp;
756 struct upgt_softc *sc = ifp->if_softc;
757 const struct ieee80211_txparam *tp;
758
759 /*
760 * 0x01 = OFMD6 0x10 = DS1
761 * 0x04 = OFDM9 0x11 = DS2
762 * 0x06 = OFDM12 0x12 = DS5
763 * 0x07 = OFDM18 0x13 = DS11
764 * 0x08 = OFDM24
765 * 0x09 = OFDM36
766 * 0x0a = OFDM48
767 * 0x0b = OFDM54
768 */
769 const uint8_t rateset_auto_11b[] =
770 { 0x13, 0x13, 0x12, 0x11, 0x11, 0x10, 0x10, 0x10 };
771 const uint8_t rateset_auto_11g[] =
772 { 0x0b, 0x0a, 0x09, 0x08, 0x07, 0x06, 0x04, 0x01 };
773 const uint8_t rateset_fix_11bg[] =
774 { 0x10, 0x11, 0x12, 0x13, 0x01, 0x04, 0x06, 0x07,
775 0x08, 0x09, 0x0a, 0x0b };
776
777 tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
778
779 /* XXX */
780 if (tp->ucastrate == IEEE80211_FIXED_RATE_NONE) {
781 /*
782 * Automatic rate control is done by the device.
783 * We just pass the rateset from which the device
784 * will pickup a rate.
785 */
786 if (ic->ic_curmode == IEEE80211_MODE_11B)
787 bcopy(rateset_auto_11b, sc->sc_cur_rateset,
788 sizeof(sc->sc_cur_rateset));
789 if (ic->ic_curmode == IEEE80211_MODE_11G ||
790 ic->ic_curmode == IEEE80211_MODE_AUTO)
791 bcopy(rateset_auto_11g, sc->sc_cur_rateset,
792 sizeof(sc->sc_cur_rateset));
793 } else {
794 /* set a fixed rate */
795 memset(sc->sc_cur_rateset, rateset_fix_11bg[tp->ucastrate],
796 sizeof(sc->sc_cur_rateset));
797 }
798}
799
800static void
801upgt_set_multi(void *arg)
802{
803 struct upgt_softc *sc = arg;
804 struct ifnet *ifp = sc->sc_ifp;
805
806 if (!(ifp->if_flags & IFF_UP))
807 return;
808
809 /*
810 * XXX don't know how to set a device. Lack of docs. Just try to set
811 * IFF_ALLMULTI flag here.
812 */
813 ifp->if_flags |= IFF_ALLMULTI;
814}
815
816static void
817upgt_start(struct ifnet *ifp)
818{
819 struct upgt_softc *sc = ifp->if_softc;
820 struct upgt_data *data_tx;
821 struct ieee80211_node *ni;
822 struct mbuf *m;
823
824 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
825 return;
826
827 UPGT_LOCK(sc);
828 for (;;) {
829 IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
830 if (m == NULL)
831 break;
832
833 data_tx = upgt_gettxbuf(sc);
834 if (data_tx == NULL) {
835 IFQ_DRV_PREPEND(&ifp->if_snd, m);
836 break;
837 }
838
839 ni = (struct ieee80211_node *)m->m_pkthdr.rcvif;
840 m->m_pkthdr.rcvif = NULL;
841
842 if (upgt_tx_start(sc, m, ni, data_tx) != 0) {
843 STAILQ_INSERT_HEAD(&sc->sc_tx_inactive, data_tx, next);
844 UPGT_STAT_INC(sc, st_tx_inactive);
845 ieee80211_free_node(ni);
846 ifp->if_oerrors++;
847 continue;
848 }
849 sc->sc_tx_timer = 5;
850 }
851 UPGT_UNLOCK(sc);
852}
853
854static int
855upgt_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
856 const struct ieee80211_bpf_params *params)
857{
858 struct ieee80211com *ic = ni->ni_ic;
859 struct ifnet *ifp = ic->ic_ifp;
860 struct upgt_softc *sc = ifp->if_softc;
861 struct upgt_data *data_tx = NULL;
862
863 /* prevent management frames from being sent if we're not ready */
864 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
865 m_freem(m);
866 ieee80211_free_node(ni);
867 return ENETDOWN;
868 }
869
870 UPGT_LOCK(sc);
871 data_tx = upgt_gettxbuf(sc);
872 if (data_tx == NULL) {
873 ieee80211_free_node(ni);
874 m_freem(m);
875 UPGT_UNLOCK(sc);
876 return (ENOBUFS);
877 }
878
879 if (upgt_tx_start(sc, m, ni, data_tx) != 0) {
880 STAILQ_INSERT_HEAD(&sc->sc_tx_inactive, data_tx, next);
881 UPGT_STAT_INC(sc, st_tx_inactive);
882 ieee80211_free_node(ni);
883 ifp->if_oerrors++;
884 UPGT_UNLOCK(sc);
885 return (EIO);
886 }
887 UPGT_UNLOCK(sc);
888
889 sc->sc_tx_timer = 5;
890 return (0);
891}
892
893static void
894upgt_watchdog(void *arg)
895{
896 struct upgt_softc *sc = arg;
897 struct ifnet *ifp = sc->sc_ifp;
898
899 if (sc->sc_tx_timer > 0) {
900 if (--sc->sc_tx_timer == 0) {
901 device_printf(sc->sc_dev, "watchdog timeout\n");
902 /* upgt_init(ifp); XXX needs a process context ? */
903 ifp->if_oerrors++;
904 return;
905 }
906 callout_reset(&sc->sc_watchdog_ch, hz, upgt_watchdog, sc);
907 }
908}
909
910static uint32_t
911upgt_mem_alloc(struct upgt_softc *sc)
912{
913 int i;
914
915 for (i = 0; i < sc->sc_memory.pages; i++) {
916 if (sc->sc_memory.page[i].used == 0) {
917 sc->sc_memory.page[i].used = 1;
918 return (sc->sc_memory.page[i].addr);
919 }
920 }
921
922 return (0);
923}
924
925static void
926upgt_scan_start(struct ieee80211com *ic)
927{
928 /* do nothing. */
929}
930
931static void
932upgt_scan_end(struct ieee80211com *ic)
933{
934 /* do nothing. */
935}
936
937static void
938upgt_set_channel(struct ieee80211com *ic)
939{
940 struct upgt_softc *sc = ic->ic_ifp->if_softc;
941
942 UPGT_LOCK(sc);
943 upgt_set_chan(sc, ic->ic_curchan);
944 UPGT_UNLOCK(sc);
945}
946
947static void
948upgt_set_chan(struct upgt_softc *sc, struct ieee80211_channel *c)
949{
950 struct ifnet *ifp = sc->sc_ifp;
951 struct ieee80211com *ic = ifp->if_l2com;
952 struct upgt_data *data_cmd;
953 struct upgt_lmac_mem *mem;
954 struct upgt_lmac_channel *chan;
955 int channel;
956
957 UPGT_ASSERT_LOCKED(sc);
958
959 channel = ieee80211_chan2ieee(ic, c);
960 if (channel == 0 || channel == IEEE80211_CHAN_ANY) {
961 /* XXX should NEVER happen */
962 device_printf(sc->sc_dev,
963 "%s: invalid channel %x\n", __func__, channel);
964 return;
965 }
966
967 DPRINTF(sc, UPGT_DEBUG_STATE, "%s: channel %d\n", __func__, channel);
968
969 data_cmd = upgt_getbuf(sc);
970 if (data_cmd == NULL) {
971 device_printf(sc->sc_dev, "%s: out of buffers.\n", __func__);
972 return;
973 }
974 /*
975 * Transmit the URB containing the CMD data.
976 */
977 bzero(data_cmd->buf, MCLBYTES);
978
979 mem = (struct upgt_lmac_mem *)data_cmd->buf;
980 mem->addr = htole32(sc->sc_memaddr_frame_start +
981 UPGT_MEMSIZE_FRAME_HEAD);
982
983 chan = (struct upgt_lmac_channel *)(mem + 1);
984
985 chan->header1.flags = UPGT_H1_FLAGS_TX_NO_CALLBACK;
986 chan->header1.type = UPGT_H1_TYPE_CTRL;
987 chan->header1.len = htole16(
988 sizeof(struct upgt_lmac_channel) - sizeof(struct upgt_lmac_header));
989
990 chan->header2.reqid = htole32(sc->sc_memaddr_frame_start);
991 chan->header2.type = htole16(UPGT_H2_TYPE_CHANNEL);
992 chan->header2.flags = 0;
993
994 chan->unknown1 = htole16(UPGT_CHANNEL_UNKNOWN1);
995 chan->unknown2 = htole16(UPGT_CHANNEL_UNKNOWN2);
996 chan->freq6 = sc->sc_eeprom_freq6[channel];
997 chan->settings = sc->sc_eeprom_freq6_settings;
998 chan->unknown3 = UPGT_CHANNEL_UNKNOWN3;
999
1000 bcopy(&sc->sc_eeprom_freq3[channel].data, chan->freq3_1,
1001 sizeof(chan->freq3_1));
1002 bcopy(&sc->sc_eeprom_freq4[channel], chan->freq4,
1003 sizeof(sc->sc_eeprom_freq4[channel]));
1004 bcopy(&sc->sc_eeprom_freq3[channel].data, chan->freq3_2,
1005 sizeof(chan->freq3_2));
1006
1007 data_cmd->buflen = sizeof(*mem) + sizeof(*chan);
1008
1009 mem->chksum = upgt_chksum_le((uint32_t *)chan,
1010 data_cmd->buflen - sizeof(*mem));
1011
1012 upgt_bulk_tx(sc, data_cmd);
1013}
1014
1015static struct ieee80211vap *
1016upgt_vap_create(struct ieee80211com *ic,
1017 const char name[IFNAMSIZ], int unit, int opmode, int flags,
1018 const uint8_t bssid[IEEE80211_ADDR_LEN],
1019 const uint8_t mac[IEEE80211_ADDR_LEN])
1020{
1021 struct upgt_vap *uvp;
1022 struct ieee80211vap *vap;
1023
1024 if (!TAILQ_EMPTY(&ic->ic_vaps)) /* only one at a time */
1025 return NULL;
1026 uvp = (struct upgt_vap *) malloc(sizeof(struct upgt_vap),
1027 M_80211_VAP, M_NOWAIT | M_ZERO);
1028 if (uvp == NULL)
1029 return NULL;
1030 vap = &uvp->vap;
1031 /* enable s/w bmiss handling for sta mode */
1032 ieee80211_vap_setup(ic, vap, name, unit, opmode,
1033 flags | IEEE80211_CLONE_NOBEACONS, bssid, mac);
1034
1035 /* override state transition machine */
1036 uvp->newstate = vap->iv_newstate;
1037 vap->iv_newstate = upgt_newstate;
1038
1039 /* setup device rates */
1040 upgt_setup_rates(vap, ic);
1041
1042 /* complete setup */
1043 ieee80211_vap_attach(vap, ieee80211_media_change,
1044 ieee80211_media_status);
1045 ic->ic_opmode = opmode;
1046 return vap;
1047}
1048
1049static int
1050upgt_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
1051{
1052 struct upgt_vap *uvp = UPGT_VAP(vap);
1053 struct ieee80211com *ic = vap->iv_ic;
1054 struct upgt_softc *sc = ic->ic_ifp->if_softc;
1055
1056 /* do it in a process context */
1057 sc->sc_state = nstate;
1058
1059 IEEE80211_UNLOCK(ic);
1060 UPGT_LOCK(sc);
1061 callout_stop(&sc->sc_led_ch);
1062 callout_stop(&sc->sc_watchdog_ch);
1063
1064 switch (nstate) {
1065 case IEEE80211_S_INIT:
1066 /* do not accept any frames if the device is down */
1067 (void)upgt_set_macfilter(sc, sc->sc_state);
1068 upgt_set_led(sc, UPGT_LED_OFF);
1069 break;
1070 case IEEE80211_S_SCAN:
1071 upgt_set_chan(sc, ic->ic_curchan);
1072 break;
1073 case IEEE80211_S_AUTH:
1074 upgt_set_chan(sc, ic->ic_curchan);
1075 break;
1076 case IEEE80211_S_ASSOC:
1077 break;
1078 case IEEE80211_S_RUN:
1079 upgt_set_macfilter(sc, sc->sc_state);
1080 upgt_set_led(sc, UPGT_LED_ON);
1081 break;
1082 default:
1083 break;
1084 }
1085 UPGT_UNLOCK(sc);
1086 IEEE80211_LOCK(ic);
1087 return (uvp->newstate(vap, nstate, arg));
1088}
1089
1090static void
1091upgt_vap_delete(struct ieee80211vap *vap)
1092{
1093 struct upgt_vap *uvp = UPGT_VAP(vap);
1094
1095 ieee80211_vap_detach(vap);
1096 free(uvp, M_80211_VAP);
1097}
1098
1099static void
1100upgt_update_mcast(struct ifnet *ifp)
1101{
1102 struct upgt_softc *sc = ifp->if_softc;
1103
1104 upgt_set_multi(sc);
1105}
1106
1107static int
1108upgt_eeprom_parse(struct upgt_softc *sc)
1109{
1110 struct upgt_eeprom_header *eeprom_header;
1111 struct upgt_eeprom_option *eeprom_option;
1112 uint16_t option_len;
1113 uint16_t option_type;
1114 uint16_t preamble_len;
1115 int option_end = 0;
1116
1117 /* calculate eeprom options start offset */
1118 eeprom_header = (struct upgt_eeprom_header *)sc->sc_eeprom;
1119 preamble_len = le16toh(eeprom_header->preamble_len);
1120 eeprom_option = (struct upgt_eeprom_option *)(sc->sc_eeprom +
1121 (sizeof(struct upgt_eeprom_header) + preamble_len));
1122
1123 while (!option_end) {
1124 /* the eeprom option length is stored in words */
1125 option_len =
1126 (le16toh(eeprom_option->len) - 1) * sizeof(uint16_t);
1127 option_type =
1128 le16toh(eeprom_option->type);
1129
1130 switch (option_type) {
1131 case UPGT_EEPROM_TYPE_NAME:
1132 DPRINTF(sc, UPGT_DEBUG_FW,
1133 "EEPROM name len=%d\n", option_len);
1134 break;
1135 case UPGT_EEPROM_TYPE_SERIAL:
1136 DPRINTF(sc, UPGT_DEBUG_FW,
1137 "EEPROM serial len=%d\n", option_len);
1138 break;
1139 case UPGT_EEPROM_TYPE_MAC:
1140 DPRINTF(sc, UPGT_DEBUG_FW,
1141 "EEPROM mac len=%d\n", option_len);
1142
1143 IEEE80211_ADDR_COPY(sc->sc_myaddr, eeprom_option->data);
1144 break;
1145 case UPGT_EEPROM_TYPE_HWRX:
1146 DPRINTF(sc, UPGT_DEBUG_FW,
1147 "EEPROM hwrx len=%d\n", option_len);
1148
1149 upgt_eeprom_parse_hwrx(sc, eeprom_option->data);
1150 break;
1151 case UPGT_EEPROM_TYPE_CHIP:
1152 DPRINTF(sc, UPGT_DEBUG_FW,
1153 "EEPROM chip len=%d\n", option_len);
1154 break;
1155 case UPGT_EEPROM_TYPE_FREQ3:
1156 DPRINTF(sc, UPGT_DEBUG_FW,
1157 "EEPROM freq3 len=%d\n", option_len);
1158
1159 upgt_eeprom_parse_freq3(sc, eeprom_option->data,
1160 option_len);
1161 break;
1162 case UPGT_EEPROM_TYPE_FREQ4:
1163 DPRINTF(sc, UPGT_DEBUG_FW,
1164 "EEPROM freq4 len=%d\n", option_len);
1165
1166 upgt_eeprom_parse_freq4(sc, eeprom_option->data,
1167 option_len);
1168 break;
1169 case UPGT_EEPROM_TYPE_FREQ5:
1170 DPRINTF(sc, UPGT_DEBUG_FW,
1171 "EEPROM freq5 len=%d\n", option_len);
1172 break;
1173 case UPGT_EEPROM_TYPE_FREQ6:
1174 DPRINTF(sc, UPGT_DEBUG_FW,
1175 "EEPROM freq6 len=%d\n", option_len);
1176
1177 upgt_eeprom_parse_freq6(sc, eeprom_option->data,
1178 option_len);
1179 break;
1180 case UPGT_EEPROM_TYPE_END:
1181 DPRINTF(sc, UPGT_DEBUG_FW,
1182 "EEPROM end len=%d\n", option_len);
1183 option_end = 1;
1184 break;
1185 case UPGT_EEPROM_TYPE_OFF:
1186 DPRINTF(sc, UPGT_DEBUG_FW,
1187 "%s: EEPROM off without end option\n", __func__);
1188 return (EIO);
1189 default:
1190 DPRINTF(sc, UPGT_DEBUG_FW,
1191 "EEPROM unknown type 0x%04x len=%d\n",
1192 option_type, option_len);
1193 break;
1194 }
1195
1196 /* jump to next EEPROM option */
1197 eeprom_option = (struct upgt_eeprom_option *)
1198 (eeprom_option->data + option_len);
1199 }
1200
1201 return (0);
1202}
1203
1204static void
1205upgt_eeprom_parse_freq3(struct upgt_softc *sc, uint8_t *data, int len)
1206{
1207 struct upgt_eeprom_freq3_header *freq3_header;
1208 struct upgt_lmac_freq3 *freq3;
1209 int i, elements, flags;
1210 unsigned channel;
1211
1212 freq3_header = (struct upgt_eeprom_freq3_header *)data;
1213 freq3 = (struct upgt_lmac_freq3 *)(freq3_header + 1);
1214
1215 flags = freq3_header->flags;
1216 elements = freq3_header->elements;
1217
1218 DPRINTF(sc, UPGT_DEBUG_FW, "flags=0x%02x elements=%d\n",
1219 flags, elements);
1220
1221 for (i = 0; i < elements; i++) {
1222 channel = ieee80211_mhz2ieee(le16toh(freq3[i].freq), 0);
1223 if (!(channel >= 0 && channel < IEEE80211_CHAN_MAX))
1224 continue;
1225
1226 sc->sc_eeprom_freq3[channel] = freq3[i];
1227
1228 DPRINTF(sc, UPGT_DEBUG_FW, "frequence=%d, channel=%d\n",
1229 le16toh(sc->sc_eeprom_freq3[channel].freq), channel);
1230 }
1231}
1232
1233void
1234upgt_eeprom_parse_freq4(struct upgt_softc *sc, uint8_t *data, int len)
1235{
1236 struct upgt_eeprom_freq4_header *freq4_header;
1237 struct upgt_eeprom_freq4_1 *freq4_1;
1238 struct upgt_eeprom_freq4_2 *freq4_2;
1239 int i, j, elements, settings, flags;
1240 unsigned channel;
1241
1242 freq4_header = (struct upgt_eeprom_freq4_header *)data;
1243 freq4_1 = (struct upgt_eeprom_freq4_1 *)(freq4_header + 1);
1244 flags = freq4_header->flags;
1245 elements = freq4_header->elements;
1246 settings = freq4_header->settings;
1247
1248 /* we need this value later */
1249 sc->sc_eeprom_freq6_settings = freq4_header->settings;
1250
1251 DPRINTF(sc, UPGT_DEBUG_FW, "flags=0x%02x elements=%d settings=%d\n",
1252 flags, elements, settings);
1253
1254 for (i = 0; i < elements; i++) {
1255 channel = ieee80211_mhz2ieee(le16toh(freq4_1[i].freq), 0);
1256 if (!(channel >= 0 && channel < IEEE80211_CHAN_MAX))
1257 continue;
1258
1259 freq4_2 = (struct upgt_eeprom_freq4_2 *)freq4_1[i].data;
1260 for (j = 0; j < settings; j++) {
1261 sc->sc_eeprom_freq4[channel][j].cmd = freq4_2[j];
1262 sc->sc_eeprom_freq4[channel][j].pad = 0;
1263 }
1264
1265 DPRINTF(sc, UPGT_DEBUG_FW, "frequence=%d, channel=%d\n",
1266 le16toh(freq4_1[i].freq), channel);
1267 }
1268}
1269
1270void
1271upgt_eeprom_parse_freq6(struct upgt_softc *sc, uint8_t *data, int len)
1272{
1273 struct upgt_lmac_freq6 *freq6;
1274 int i, elements;
1275 unsigned channel;
1276
1277 freq6 = (struct upgt_lmac_freq6 *)data;
1278 elements = len / sizeof(struct upgt_lmac_freq6);
1279
1280 DPRINTF(sc, UPGT_DEBUG_FW, "elements=%d\n", elements);
1281
1282 for (i = 0; i < elements; i++) {
1283 channel = ieee80211_mhz2ieee(le16toh(freq6[i].freq), 0);
1284 if (!(channel >= 0 && channel < IEEE80211_CHAN_MAX))
1285 continue;
1286
1287 sc->sc_eeprom_freq6[channel] = freq6[i];
1288
1289 DPRINTF(sc, UPGT_DEBUG_FW, "frequence=%d, channel=%d\n",
1290 le16toh(sc->sc_eeprom_freq6[channel].freq), channel);
1291 }
1292}
1293
1294static void
1295upgt_eeprom_parse_hwrx(struct upgt_softc *sc, uint8_t *data)
1296{
1297 struct upgt_eeprom_option_hwrx *option_hwrx;
1298
1299 option_hwrx = (struct upgt_eeprom_option_hwrx *)data;
1300
1301 sc->sc_eeprom_hwrx = option_hwrx->rxfilter - UPGT_EEPROM_RX_CONST;
1302
1303 DPRINTF(sc, UPGT_DEBUG_FW, "hwrx option value=0x%04x\n",
1304 sc->sc_eeprom_hwrx);
1305}
1306
1307static int
1308upgt_eeprom_read(struct upgt_softc *sc)
1309{
1310 struct upgt_data *data_cmd;
1311 struct upgt_lmac_mem *mem;
1312 struct upgt_lmac_eeprom *eeprom;
1313 int block, error, offset;
1314
1315 UPGT_LOCK(sc);
1316 usb_pause_mtx(&sc->sc_mtx, 100);
1317
1318 offset = 0;
1319 block = UPGT_EEPROM_BLOCK_SIZE;
1320 while (offset < UPGT_EEPROM_SIZE) {
1321 DPRINTF(sc, UPGT_DEBUG_FW,
1322 "request EEPROM block (offset=%d, len=%d)\n", offset, block);
1323
1324 data_cmd = upgt_getbuf(sc);
1325 if (data_cmd == NULL) {
1326 UPGT_UNLOCK(sc);
1327 return (ENOBUFS);
1328 }
1329
1330 /*
1331 * Transmit the URB containing the CMD data.
1332 */
1333 bzero(data_cmd->buf, MCLBYTES);
1334
1335 mem = (struct upgt_lmac_mem *)data_cmd->buf;
1336 mem->addr = htole32(sc->sc_memaddr_frame_start +
1337 UPGT_MEMSIZE_FRAME_HEAD);
1338
1339 eeprom = (struct upgt_lmac_eeprom *)(mem + 1);
1340 eeprom->header1.flags = 0;
1341 eeprom->header1.type = UPGT_H1_TYPE_CTRL;
1342 eeprom->header1.len = htole16((
1343 sizeof(struct upgt_lmac_eeprom) -
1344 sizeof(struct upgt_lmac_header)) + block);
1345
1346 eeprom->header2.reqid = htole32(sc->sc_memaddr_frame_start);
1347 eeprom->header2.type = htole16(UPGT_H2_TYPE_EEPROM);
1348 eeprom->header2.flags = 0;
1349
1350 eeprom->offset = htole16(offset);
1351 eeprom->len = htole16(block);
1352
1353 data_cmd->buflen = sizeof(*mem) + sizeof(*eeprom) + block;
1354
1355 mem->chksum = upgt_chksum_le((uint32_t *)eeprom,
1356 data_cmd->buflen - sizeof(*mem));
1357 upgt_bulk_tx(sc, data_cmd);
1358
1359 error = mtx_sleep(sc, &sc->sc_mtx, 0, "eeprom_request", hz);
1360 if (error != 0) {
1361 device_printf(sc->sc_dev,
1362 "timeout while waiting for EEPROM data\n");
1363 UPGT_UNLOCK(sc);
1364 return (EIO);
1365 }
1366
1367 offset += block;
1368 if (UPGT_EEPROM_SIZE - offset < block)
1369 block = UPGT_EEPROM_SIZE - offset;
1370 }
1371
1372 UPGT_UNLOCK(sc);
1373 return (0);
1374}
1375
1376/*
1377 * When a rx data came in the function returns a mbuf and a rssi values.
1378 */
1379static struct mbuf *
1380upgt_rxeof(struct usb_xfer *xfer, struct upgt_data *data, int *rssi)
1381{
1382 struct mbuf *m = NULL;
1383 struct upgt_softc *sc = usbd_xfer_softc(xfer);
1384 struct upgt_lmac_header *header;
1385 struct upgt_lmac_eeprom *eeprom;
1386 uint8_t h1_type;
1387 uint16_t h2_type;
1388 int actlen, sumlen;
1389
1390 usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL);
1391
1392 UPGT_ASSERT_LOCKED(sc);
1393
1394 if (actlen < 1)
1395 return (NULL);
1396
1397 /* Check only at the very beginning. */
1398 if (!(sc->sc_flags & UPGT_FLAG_FWLOADED) &&
1399 (memcmp(data->buf, "OK", 2) == 0)) {
1400 sc->sc_flags |= UPGT_FLAG_FWLOADED;
1401 wakeup_one(sc);
1402 return (NULL);
1403 }
1404
1405 if (actlen < UPGT_RX_MINSZ)
1406 return (NULL);
1407
1408 /*
1409 * Check what type of frame came in.
1410 */
1411 header = (struct upgt_lmac_header *)(data->buf + 4);
1412
1413 h1_type = header->header1.type;
1414 h2_type = le16toh(header->header2.type);
1415
1416 if (h1_type == UPGT_H1_TYPE_CTRL && h2_type == UPGT_H2_TYPE_EEPROM) {
1417 eeprom = (struct upgt_lmac_eeprom *)(data->buf + 4);
1418 uint16_t eeprom_offset = le16toh(eeprom->offset);
1419 uint16_t eeprom_len = le16toh(eeprom->len);
1420
1421 DPRINTF(sc, UPGT_DEBUG_FW,
1422 "received EEPROM block (offset=%d, len=%d)\n",
1423 eeprom_offset, eeprom_len);
1424
1425 bcopy(data->buf + sizeof(struct upgt_lmac_eeprom) + 4,
1426 sc->sc_eeprom + eeprom_offset, eeprom_len);
1427
1428 /* EEPROM data has arrived in time, wakeup. */
1429 wakeup(sc);
1430 } else if (h1_type == UPGT_H1_TYPE_CTRL &&
1431 h2_type == UPGT_H2_TYPE_TX_DONE) {
1432 DPRINTF(sc, UPGT_DEBUG_XMIT, "%s: received 802.11 TX done\n",
1433 __func__);
1434 upgt_tx_done(sc, data->buf + 4);
1435 } else if (h1_type == UPGT_H1_TYPE_RX_DATA ||
1436 h1_type == UPGT_H1_TYPE_RX_DATA_MGMT) {
1437 DPRINTF(sc, UPGT_DEBUG_RECV, "%s: received 802.11 RX data\n",
1438 __func__);
1439 m = upgt_rx(sc, data->buf + 4, le16toh(header->header1.len),
1440 rssi);
1441 } else if (h1_type == UPGT_H1_TYPE_CTRL &&
1442 h2_type == UPGT_H2_TYPE_STATS) {
1443 DPRINTF(sc, UPGT_DEBUG_STAT, "%s: received statistic data\n",
1444 __func__);
1445 /* TODO: what could we do with the statistic data? */
1446 } else {
1447 /* ignore unknown frame types */
1448 DPRINTF(sc, UPGT_DEBUG_INTR,
1449 "received unknown frame type 0x%02x\n",
1450 header->header1.type);
1451 }
1452 return (m);
1453}
1454
1455/*
1456 * The firmware awaits a checksum for each frame we send to it.
1457 * The algorithm used therefor is uncommon but somehow similar to CRC32.
1458 */
1459static uint32_t
1460upgt_chksum_le(const uint32_t *buf, size_t size)
1461{
1462 int i;
1463 uint32_t crc = 0;
1464
1465 for (i = 0; i < size; i += sizeof(uint32_t)) {
1466 crc = htole32(crc ^ *buf++);
1467 crc = htole32((crc >> 5) ^ (crc << 3));
1468 }
1469
1470 return (crc);
1471}
1472
1473static struct mbuf *
1474upgt_rx(struct upgt_softc *sc, uint8_t *data, int pkglen, int *rssi)
1475{
1476 struct ifnet *ifp = sc->sc_ifp;
1477 struct ieee80211com *ic = ifp->if_l2com;
1478 struct upgt_lmac_rx_desc *rxdesc;
1479 struct mbuf *m;
1480
1481 /*
1482 * don't pass packets to the ieee80211 framework if the driver isn't
1483 * RUNNING.
1484 */
1485 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
1486 return (NULL);
1487
1488 /* access RX packet descriptor */
1489 rxdesc = (struct upgt_lmac_rx_desc *)data;
1490
1491 /* create mbuf which is suitable for strict alignment archs */
1492 KASSERT((pkglen + ETHER_ALIGN) < MCLBYTES,
1493 ("A current mbuf storage is small (%d)", pkglen + ETHER_ALIGN));
1494 m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
1495 if (m == NULL) {
1496 device_printf(sc->sc_dev, "could not create RX mbuf\n");
1497 return (NULL);
1498 }
1499 m_adj(m, ETHER_ALIGN);
1500 bcopy(rxdesc->data, mtod(m, char *), pkglen);
1501 /* trim FCS */
1502 m->m_len = m->m_pkthdr.len = pkglen - IEEE80211_CRC_LEN;
1503 m->m_pkthdr.rcvif = ifp;
1504
1505 if (ieee80211_radiotap_active(ic)) {
1506 struct upgt_rx_radiotap_header *tap = &sc->sc_rxtap;
1507
1508 tap->wr_flags = 0;
1509 tap->wr_rate = upgt_rx_rate(sc, rxdesc->rate);
1510 tap->wr_antsignal = rxdesc->rssi;
1511 }
1512 ifp->if_ipackets++;
1513
1514 DPRINTF(sc, UPGT_DEBUG_RX_PROC, "%s: RX done\n", __func__);
1515 *rssi = rxdesc->rssi;
1516 return (m);
1517}
1518
1519static uint8_t
1520upgt_rx_rate(struct upgt_softc *sc, const int rate)
1521{
1522 struct ifnet *ifp = sc->sc_ifp;
1523 struct ieee80211com *ic = ifp->if_l2com;
1524 static const uint8_t cck_upgt2rate[4] = { 2, 4, 11, 22 };
1525 static const uint8_t ofdm_upgt2rate[12] =
1526 { 2, 4, 11, 22, 12, 18, 24, 36, 48, 72, 96, 108 };
1527
1528 if (ic->ic_curmode == IEEE80211_MODE_11B &&
1529 !(rate < 0 || rate > 3))
1530 return cck_upgt2rate[rate & 0xf];
1531
1532 if (ic->ic_curmode == IEEE80211_MODE_11G &&
1533 !(rate < 0 || rate > 11))
1534 return ofdm_upgt2rate[rate & 0xf];
1535
1536 return (0);
1537}
1538
1539static void
1540upgt_tx_done(struct upgt_softc *sc, uint8_t *data)
1541{
1542 struct ifnet *ifp = sc->sc_ifp;
1543 struct upgt_lmac_tx_done_desc *desc;
1544 int i, freed = 0;
1545
1546 UPGT_ASSERT_LOCKED(sc);
1547
1548 desc = (struct upgt_lmac_tx_done_desc *)data;
1549
1550 for (i = 0; i < UPGT_TX_MAXCOUNT; i++) {
1551 struct upgt_data *data_tx = &sc->sc_tx_data[i];
1552
1553 if (data_tx->addr == le32toh(desc->header2.reqid)) {
1554 upgt_mem_free(sc, data_tx->addr);
1555 data_tx->ni = NULL;
1556 data_tx->addr = 0;
1557 data_tx->m = NULL;
1558 data_tx->use = 0;
1559
1560 DPRINTF(sc, UPGT_DEBUG_TX_PROC,
1561 "TX done: memaddr=0x%08x, status=0x%04x, rssi=%d, ",
1562 le32toh(desc->header2.reqid),
1563 le16toh(desc->status), le16toh(desc->rssi));
1564 DPRINTF(sc, UPGT_DEBUG_TX_PROC, "seq=%d\n",
1565 le16toh(desc->seq));
1566
1567 freed++;
1568 }
1569 }
1570
1571 if (freed != 0) {
1572 sc->sc_tx_timer = 0;
1573 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1574 UPGT_UNLOCK(sc);
1575 upgt_start(ifp);
1576 UPGT_LOCK(sc);
1577 }
1578}
1579
1580static void
1581upgt_mem_free(struct upgt_softc *sc, uint32_t addr)
1582{
1583 int i;
1584
1585 for (i = 0; i < sc->sc_memory.pages; i++) {
1586 if (sc->sc_memory.page[i].addr == addr) {
1587 sc->sc_memory.page[i].used = 0;
1588 return;
1589 }
1590 }
1591
1592 device_printf(sc->sc_dev,
1593 "could not free memory address 0x%08x\n", addr);
1594}
1595
1596static int
1597upgt_fw_load(struct upgt_softc *sc)
1598{
1599 const struct firmware *fw;
1600 struct upgt_data *data_cmd;
1601 struct upgt_fw_x2_header *x2;
1602 char start_fwload_cmd[] = { 0x3c, 0x0d };
1603 int error = 0, offset, bsize, n;
1604 uint32_t crc32;
1605
1606 fw = firmware_get(upgt_fwname);
1607 if (fw == NULL) {
1608 device_printf(sc->sc_dev, "could not read microcode %s\n",
1609 upgt_fwname);
1610 return (EIO);
1611 }
1612
1613 UPGT_LOCK(sc);
1614
1615 /* send firmware start load command */
1616 data_cmd = upgt_getbuf(sc);
1617 if (data_cmd == NULL) {
1618 error = ENOBUFS;
1619 goto fail;
1620 }
1621 data_cmd->buflen = sizeof(start_fwload_cmd);
1622 bcopy(start_fwload_cmd, data_cmd->buf, data_cmd->buflen);
1623 upgt_bulk_tx(sc, data_cmd);
1624
1625 /* send X2 header */
1626 data_cmd = upgt_getbuf(sc);
1627 if (data_cmd == NULL) {
1628 error = ENOBUFS;
1629 goto fail;
1630 }
1631 data_cmd->buflen = sizeof(struct upgt_fw_x2_header);
1632 x2 = (struct upgt_fw_x2_header *)data_cmd->buf;
1633 bcopy(UPGT_X2_SIGNATURE, x2->signature, UPGT_X2_SIGNATURE_SIZE);
1634 x2->startaddr = htole32(UPGT_MEMADDR_FIRMWARE_START);
1635 x2->len = htole32(fw->datasize);
1636 x2->crc = upgt_crc32_le((uint8_t *)data_cmd->buf +
1637 UPGT_X2_SIGNATURE_SIZE,
1638 sizeof(struct upgt_fw_x2_header) - UPGT_X2_SIGNATURE_SIZE -
1639 sizeof(uint32_t));
1640 upgt_bulk_tx(sc, data_cmd);
1641
1642 /* download firmware */
1643 for (offset = 0; offset < fw->datasize; offset += bsize) {
1644 if (fw->datasize - offset > UPGT_FW_BLOCK_SIZE)
1645 bsize = UPGT_FW_BLOCK_SIZE;
1646 else
1647 bsize = fw->datasize - offset;
1648
1649 data_cmd = upgt_getbuf(sc);
1650 if (data_cmd == NULL) {
1651 error = ENOBUFS;
1652 goto fail;
1653 }
1654 n = upgt_fw_copy((const uint8_t *)fw->data + offset,
1655 data_cmd->buf, bsize);
1656 data_cmd->buflen = bsize;
1657 upgt_bulk_tx(sc, data_cmd);
1658
1659 DPRINTF(sc, UPGT_DEBUG_FW, "FW offset=%d, read=%d, sent=%d\n",
1660 offset, n, bsize);
1661 bsize = n;
1662 }
1663 DPRINTF(sc, UPGT_DEBUG_FW, "%s: firmware downloaded\n", __func__);
1664
1665 /* load firmware */
1666 data_cmd = upgt_getbuf(sc);
1667 if (data_cmd == NULL) {
1668 error = ENOBUFS;
1669 goto fail;
1670 }
1671 crc32 = upgt_crc32_le(fw->data, fw->datasize);
1672 *((uint32_t *)(data_cmd->buf) ) = crc32;
1673 *((uint8_t *)(data_cmd->buf) + 4) = 'g';
1674 *((uint8_t *)(data_cmd->buf) + 5) = '\r';
1675 data_cmd->buflen = 6;
1676 upgt_bulk_tx(sc, data_cmd);
1677
1678 /* waiting 'OK' response. */
1679 usbd_transfer_start(sc->sc_xfer[UPGT_BULK_RX]);
1680 error = mtx_sleep(sc, &sc->sc_mtx, 0, "upgtfw", 2 * hz);
1681 if (error != 0) {
1682 device_printf(sc->sc_dev, "firmware load failed\n");
1683 error = EIO;
1684 }
1685
1686 DPRINTF(sc, UPGT_DEBUG_FW, "%s: firmware loaded\n", __func__);
1687fail:
1688 UPGT_UNLOCK(sc);
1689 firmware_put(fw, FIRMWARE_UNLOAD);
1690 return (error);
1691}
1692
1693static uint32_t
1694upgt_crc32_le(const void *buf, size_t size)
1695{
1696 uint32_t crc;
1697
1698 crc = ether_crc32_le(buf, size);
1699
1700 /* apply final XOR value as common for CRC-32 */
1701 crc = htole32(crc ^ 0xffffffffU);
1702
1703 return (crc);
1704}
1705
1706/*
1707 * While copying the version 2 firmware, we need to replace two characters:
1708 *
1709 * 0x7e -> 0x7d 0x5e
1710 * 0x7d -> 0x7d 0x5d
1711 */
1712static int
1713upgt_fw_copy(const uint8_t *src, char *dst, int size)
1714{
1715 int i, j;
1716
1717 for (i = 0, j = 0; i < size && j < size; i++) {
1718 switch (src[i]) {
1719 case 0x7e:
1720 dst[j] = 0x7d;
1721 j++;
1722 dst[j] = 0x5e;
1723 j++;
1724 break;
1725 case 0x7d:
1726 dst[j] = 0x7d;
1727 j++;
1728 dst[j] = 0x5d;
1729 j++;
1730 break;
1731 default:
1732 dst[j] = src[i];
1733 j++;
1734 break;
1735 }
1736 }
1737
1738 return (i);
1739}
1740
1741static int
1742upgt_mem_init(struct upgt_softc *sc)
1743{
1744 int i;
1745
1746 for (i = 0; i < UPGT_MEMORY_MAX_PAGES; i++) {
1747 sc->sc_memory.page[i].used = 0;
1748
1749 if (i == 0) {
1750 /*
1751 * The first memory page is always reserved for
1752 * command data.
1753 */
1754 sc->sc_memory.page[i].addr =
1755 sc->sc_memaddr_frame_start + MCLBYTES;
1756 } else {
1757 sc->sc_memory.page[i].addr =
1758 sc->sc_memory.page[i - 1].addr + MCLBYTES;
1759 }
1760
1761 if (sc->sc_memory.page[i].addr + MCLBYTES >=
1762 sc->sc_memaddr_frame_end)
1763 break;
1764
1765 DPRINTF(sc, UPGT_DEBUG_FW, "memory address page %d=0x%08x\n",
1766 i, sc->sc_memory.page[i].addr);
1767 }
1768
1769 sc->sc_memory.pages = i;
1770
1771 DPRINTF(sc, UPGT_DEBUG_FW, "memory pages=%d\n", sc->sc_memory.pages);
1772 return (0);
1773}
1774
1775static int
1776upgt_fw_verify(struct upgt_softc *sc)
1777{
1778 const struct firmware *fw;
1779 const struct upgt_fw_bra_option *bra_opt;
1780 const struct upgt_fw_bra_descr *descr;
1781 const uint8_t *p;
1782 const uint32_t *uc;
1783 uint32_t bra_option_type, bra_option_len;
1784 int offset, bra_end = 0, error = 0;
1785
1786 fw = firmware_get(upgt_fwname);
1787 if (fw == NULL) {
1788 device_printf(sc->sc_dev, "could not read microcode %s\n",
1789 upgt_fwname);
1790 return EIO;
1791 }
1792
1793 /*
1794 * Seek to beginning of Boot Record Area (BRA).
1795 */
1796 for (offset = 0; offset < fw->datasize; offset += sizeof(*uc)) {
1797 uc = (const uint32_t *)((const uint8_t *)fw->data + offset);
1798 if (*uc == 0)
1799 break;
1800 }
1801 for (; offset < fw->datasize; offset += sizeof(*uc)) {
1802 uc = (const uint32_t *)((const uint8_t *)fw->data + offset);
1803 if (*uc != 0)
1804 break;
1805 }
1806 if (offset == fw->datasize) {
1807 device_printf(sc->sc_dev,
1808 "firmware Boot Record Area not found\n");
1809 error = EIO;
1810 goto fail;
1811 }
1812
1813 DPRINTF(sc, UPGT_DEBUG_FW,
1814 "firmware Boot Record Area found at offset %d\n", offset);
1815
1816 /*
1817 * Parse Boot Record Area (BRA) options.
1818 */
1819 while (offset < fw->datasize && bra_end == 0) {
1820 /* get current BRA option */
1821 p = (const uint8_t *)fw->data + offset;
1822 bra_opt = (const struct upgt_fw_bra_option *)p;
1823 bra_option_type = le32toh(bra_opt->type);
1824 bra_option_len = le32toh(bra_opt->len) * sizeof(*uc);
1825
1826 switch (bra_option_type) {
1827 case UPGT_BRA_TYPE_FW:
1828 DPRINTF(sc, UPGT_DEBUG_FW, "UPGT_BRA_TYPE_FW len=%d\n",
1829 bra_option_len);
1830
1831 if (bra_option_len != UPGT_BRA_FWTYPE_SIZE) {
1832 device_printf(sc->sc_dev,
1833 "wrong UPGT_BRA_TYPE_FW len\n");
1834 error = EIO;
1835 goto fail;
1836 }
1837 if (memcmp(UPGT_BRA_FWTYPE_LM86, bra_opt->data,
1838 bra_option_len) == 0) {
1839 sc->sc_fw_type = UPGT_FWTYPE_LM86;
1840 break;
1841 }
1842 if (memcmp(UPGT_BRA_FWTYPE_LM87, bra_opt->data,
1843 bra_option_len) == 0) {
1844 sc->sc_fw_type = UPGT_FWTYPE_LM87;
1845 break;
1846 }
1847 device_printf(sc->sc_dev,
1848 "unsupported firmware type\n");
1849 error = EIO;
1850 goto fail;
1851 case UPGT_BRA_TYPE_VERSION:
1852 DPRINTF(sc, UPGT_DEBUG_FW,
1853 "UPGT_BRA_TYPE_VERSION len=%d\n", bra_option_len);
1854 break;
1855 case UPGT_BRA_TYPE_DEPIF:
1856 DPRINTF(sc, UPGT_DEBUG_FW,
1857 "UPGT_BRA_TYPE_DEPIF len=%d\n", bra_option_len);
1858 break;
1859 case UPGT_BRA_TYPE_EXPIF:
1860 DPRINTF(sc, UPGT_DEBUG_FW,
1861 "UPGT_BRA_TYPE_EXPIF len=%d\n", bra_option_len);
1862 break;
1863 case UPGT_BRA_TYPE_DESCR:
1864 DPRINTF(sc, UPGT_DEBUG_FW,
1865 "UPGT_BRA_TYPE_DESCR len=%d\n", bra_option_len);
1866
1867 descr = (const struct upgt_fw_bra_descr *)bra_opt->data;
1868
1869 sc->sc_memaddr_frame_start =
1870 le32toh(descr->memaddr_space_start);
1871 sc->sc_memaddr_frame_end =
1872 le32toh(descr->memaddr_space_end);
1873
1874 DPRINTF(sc, UPGT_DEBUG_FW,
1875 "memory address space start=0x%08x\n",
1876 sc->sc_memaddr_frame_start);
1877 DPRINTF(sc, UPGT_DEBUG_FW,
1878 "memory address space end=0x%08x\n",
1879 sc->sc_memaddr_frame_end);
1880 break;
1881 case UPGT_BRA_TYPE_END:
1882 DPRINTF(sc, UPGT_DEBUG_FW, "UPGT_BRA_TYPE_END len=%d\n",
1883 bra_option_len);
1884 bra_end = 1;
1885 break;
1886 default:
1887 DPRINTF(sc, UPGT_DEBUG_FW, "unknown BRA option len=%d\n",
1888 bra_option_len);
1889 error = EIO;
1890 goto fail;
1891 }
1892
1893 /* jump to next BRA option */
1894 offset += sizeof(struct upgt_fw_bra_option) + bra_option_len;
1895 }
1896
1897 DPRINTF(sc, UPGT_DEBUG_FW, "%s: firmware verified", __func__);
1898fail:
1899 firmware_put(fw, FIRMWARE_UNLOAD);
1900 return (error);
1901}
1902
1903static void
1904upgt_bulk_tx(struct upgt_softc *sc, struct upgt_data *data)
1905{
1906
1907 UPGT_ASSERT_LOCKED(sc);
1908
1909 STAILQ_INSERT_TAIL(&sc->sc_tx_pending, data, next);
1910 UPGT_STAT_INC(sc, st_tx_pending);
1911 usbd_transfer_start(sc->sc_xfer[UPGT_BULK_TX]);
1912}
1913
1914static int
1915upgt_device_reset(struct upgt_softc *sc)
1916{
1917 struct upgt_data *data;
1918 char init_cmd[] = { 0x7e, 0x7e, 0x7e, 0x7e };
1919
1920 UPGT_LOCK(sc);
1921
1922 data = upgt_getbuf(sc);
1923 if (data == NULL) {
1924 UPGT_UNLOCK(sc);
1925 return (ENOBUFS);
1926 }
1927 bcopy(init_cmd, data->buf, sizeof(init_cmd));
1928 data->buflen = sizeof(init_cmd);
1929 upgt_bulk_tx(sc, data);
1930 usb_pause_mtx(&sc->sc_mtx, 100);
1931
1932 UPGT_UNLOCK(sc);
1933 DPRINTF(sc, UPGT_DEBUG_FW, "%s: device initialized\n", __func__);
1934 return (0);
1935}
1936
1937static int
1938upgt_alloc_tx(struct upgt_softc *sc)
1939{
1940 int i;
1941
1942 STAILQ_INIT(&sc->sc_tx_active);
1943 STAILQ_INIT(&sc->sc_tx_inactive);
1944 STAILQ_INIT(&sc->sc_tx_pending);
1945
1946 for (i = 0; i < UPGT_TX_MAXCOUNT; i++) {
1947 struct upgt_data *data = &sc->sc_tx_data[i];
1948
1949 data->buf = malloc(MCLBYTES, M_USBDEV, M_NOWAIT | M_ZERO);
1950 if (data->buf == NULL) {
1951 device_printf(sc->sc_dev,
1952 "could not allocate TX buffer\n");
1953 return (ENOMEM);
1954 }
1955 STAILQ_INSERT_TAIL(&sc->sc_tx_inactive, data, next);
1956 UPGT_STAT_INC(sc, st_tx_inactive);
1957 }
1958
1959 return (0);
1960}
1961
1962static int
1963upgt_alloc_rx(struct upgt_softc *sc)
1964{
1965 int i;
1966
1967 STAILQ_INIT(&sc->sc_rx_active);
1968 STAILQ_INIT(&sc->sc_rx_inactive);
1969
1970 for (i = 0; i < UPGT_RX_MAXCOUNT; i++) {
1971 struct upgt_data *data = &sc->sc_rx_data[i];
1972
1973 data->buf = malloc(MCLBYTES, M_USBDEV, M_NOWAIT | M_ZERO);
1974 if (data->buf == NULL) {
1975 device_printf(sc->sc_dev,
1976 "could not allocate RX buffer\n");
1977 return (ENOMEM);
1978 }
1979 STAILQ_INSERT_TAIL(&sc->sc_rx_inactive, data, next);
1980 }
1981
1982 return (0);
1983}
1984
1985static int
1986upgt_detach(device_t dev)
1987{
1988 struct upgt_softc *sc = device_get_softc(dev);
1989 struct ifnet *ifp = sc->sc_ifp;
1990 struct ieee80211com *ic = ifp->if_l2com;
1991
1992 if (!device_is_attached(dev))
1993 return 0;
1994
1995 upgt_stop(sc);
1996
1997 callout_drain(&sc->sc_led_ch);
1998 callout_drain(&sc->sc_watchdog_ch);
1999
2000 usbd_transfer_unsetup(sc->sc_xfer, UPGT_N_XFERS);
2001 ieee80211_ifdetach(ic);
2002 upgt_free_rx(sc);
2003 upgt_free_tx(sc);
2004
2005 if_free(ifp);
2006 mtx_destroy(&sc->sc_mtx);
2007
2008 return (0);
2009}
2010
2011static void
2012upgt_free_rx(struct upgt_softc *sc)
2013{
2014 int i;
2015
2016 for (i = 0; i < UPGT_RX_MAXCOUNT; i++) {
2017 struct upgt_data *data = &sc->sc_rx_data[i];
2018
2019 free(data->buf, M_USBDEV);
2020 data->ni = NULL;
2021 }
2022}
2023
2024static void
2025upgt_free_tx(struct upgt_softc *sc)
2026{
2027 int i;
2028
2029 for (i = 0; i < UPGT_TX_MAXCOUNT; i++) {
2030 struct upgt_data *data = &sc->sc_tx_data[i];
2031
2032 free(data->buf, M_USBDEV);
2033 data->ni = NULL;
2034 }
2035}
2036
2037static void
2038upgt_abort_xfers_locked(struct upgt_softc *sc)
2039{
2040 int i;
2041
2042 UPGT_ASSERT_LOCKED(sc);
2043 /* abort any pending transfers */
2044 for (i = 0; i < UPGT_N_XFERS; i++)
2045 usbd_transfer_stop(sc->sc_xfer[i]);
2046}
2047
2048static void
2049upgt_abort_xfers(struct upgt_softc *sc)
2050{
2051
2052 UPGT_LOCK(sc);
2053 upgt_abort_xfers_locked(sc);
2054 UPGT_UNLOCK(sc);
2055}
2056
2057#define UPGT_SYSCTL_STAT_ADD32(c, h, n, p, d) \
2058 SYSCTL_ADD_UINT(c, h, OID_AUTO, n, CTLFLAG_RD, p, 0, d)
2059
2060static void
2061upgt_sysctl_node(struct upgt_softc *sc)
2062{
2063 struct sysctl_ctx_list *ctx;
2064 struct sysctl_oid_list *child;
2065 struct sysctl_oid *tree;
2066 struct upgt_stat *stats;
2067
2068 stats = &sc->sc_stat;
2069 ctx = device_get_sysctl_ctx(sc->sc_dev);
2070 child = SYSCTL_CHILDREN(device_get_sysctl_tree(sc->sc_dev));
2071
2072 tree = SYSCTL_ADD_NODE(ctx, child, OID_AUTO, "stats", CTLFLAG_RD,
2073 NULL, "UPGT statistics");
2074 child = SYSCTL_CHILDREN(tree);
2075 UPGT_SYSCTL_STAT_ADD32(ctx, child, "tx_active",
2076 &stats->st_tx_active, "Active numbers in TX queue");
2077 UPGT_SYSCTL_STAT_ADD32(ctx, child, "tx_inactive",
2078 &stats->st_tx_inactive, "Inactive numbers in TX queue");
2079 UPGT_SYSCTL_STAT_ADD32(ctx, child, "tx_pending",
2080 &stats->st_tx_pending, "Pending numbers in TX queue");
2081}
2082
2083#undef UPGT_SYSCTL_STAT_ADD32
2084
2085static struct upgt_data *
2086_upgt_getbuf(struct upgt_softc *sc)
2087{
2088 struct upgt_data *bf;
2089
2090 bf = STAILQ_FIRST(&sc->sc_tx_inactive);
2091 if (bf != NULL) {
2092 STAILQ_REMOVE_HEAD(&sc->sc_tx_inactive, next);
2093 UPGT_STAT_DEC(sc, st_tx_inactive);
2094 } else
2095 bf = NULL;
2096 if (bf == NULL)
2097 DPRINTF(sc, UPGT_DEBUG_XMIT, "%s: %s\n", __func__,
2098 "out of xmit buffers");
2099 return (bf);
2100}
2101
2102static struct upgt_data *
2103upgt_getbuf(struct upgt_softc *sc)
2104{
2105 struct upgt_data *bf;
2106
2107 UPGT_ASSERT_LOCKED(sc);
2108
2109 bf = _upgt_getbuf(sc);
2110 if (bf == NULL) {
2111 struct ifnet *ifp = sc->sc_ifp;
2112
2113 DPRINTF(sc, UPGT_DEBUG_XMIT, "%s: stop queue\n", __func__);
2114 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
2115 }
2116
2117 return (bf);
2118}
2119
2120static struct upgt_data *
2121upgt_gettxbuf(struct upgt_softc *sc)
2122{
2123 struct upgt_data *bf;
2124
2125 UPGT_ASSERT_LOCKED(sc);
2126
2127 bf = upgt_getbuf(sc);
2128 if (bf == NULL)
2129 return (NULL);
2130
2131 bf->addr = upgt_mem_alloc(sc);
2132 if (bf->addr == 0) {
2133 struct ifnet *ifp = sc->sc_ifp;
2134
2135 DPRINTF(sc, UPGT_DEBUG_XMIT, "%s: no free prism memory!\n",
2136 __func__);
2137 STAILQ_INSERT_HEAD(&sc->sc_tx_inactive, bf, next);
2138 UPGT_STAT_INC(sc, st_tx_inactive);
2139 if (!(ifp->if_drv_flags & IFF_DRV_OACTIVE))
2140 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
2141 return (NULL);
2142 }
2143 return (bf);
2144}
2145
2146static int
2147upgt_tx_start(struct upgt_softc *sc, struct mbuf *m, struct ieee80211_node *ni,
2148 struct upgt_data *data)
2149{
2150 struct ieee80211vap *vap = ni->ni_vap;
2151 int error = 0, len;
2152 struct ieee80211_frame *wh;
2153 struct ieee80211_key *k;
2154 struct ifnet *ifp = sc->sc_ifp;
2155 struct upgt_lmac_mem *mem;
2156 struct upgt_lmac_tx_desc *txdesc;
2157
2158 UPGT_ASSERT_LOCKED(sc);
2159
2160 upgt_set_led(sc, UPGT_LED_BLINK);
2161
2162 /*
2163 * Software crypto.
2164 */
2165 wh = mtod(m, struct ieee80211_frame *);
2166 if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
2167 k = ieee80211_crypto_encap(ni, m);
2168 if (k == NULL) {
2169 device_printf(sc->sc_dev,
2170 "ieee80211_crypto_encap returns NULL.\n");
2171 error = EIO;
2172 goto done;
2173 }
2174
2175 /* in case packet header moved, reset pointer */
2176 wh = mtod(m, struct ieee80211_frame *);
2177 }
2178
2179 /* Transmit the URB containing the TX data. */
2180 bzero(data->buf, MCLBYTES);
2181 mem = (struct upgt_lmac_mem *)data->buf;
2182 mem->addr = htole32(data->addr);
2183 txdesc = (struct upgt_lmac_tx_desc *)(mem + 1);
2184
2185 if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) ==
2186 IEEE80211_FC0_TYPE_MGT) {
2187 /* mgmt frames */
2188 txdesc->header1.flags = UPGT_H1_FLAGS_TX_MGMT;
2189 /* always send mgmt frames at lowest rate (DS1) */
2190 memset(txdesc->rates, 0x10, sizeof(txdesc->rates));
2191 } else {
2192 /* data frames */
2193 txdesc->header1.flags = UPGT_H1_FLAGS_TX_DATA;
2194 bcopy(sc->sc_cur_rateset, txdesc->rates, sizeof(txdesc->rates));
2195 }
2196 txdesc->header1.type = UPGT_H1_TYPE_TX_DATA;
2197 txdesc->header1.len = htole16(m->m_pkthdr.len);
2198 txdesc->header2.reqid = htole32(data->addr);
2199 txdesc->header2.type = htole16(UPGT_H2_TYPE_TX_ACK_YES);
2200 txdesc->header2.flags = htole16(UPGT_H2_FLAGS_TX_ACK_YES);
2201 txdesc->type = htole32(UPGT_TX_DESC_TYPE_DATA);
2202 txdesc->pad3[0] = UPGT_TX_DESC_PAD3_SIZE;
2203
2204 if (ieee80211_radiotap_active_vap(vap)) {
2205 struct upgt_tx_radiotap_header *tap = &sc->sc_txtap;
2206
2207 tap->wt_flags = 0;
2208 tap->wt_rate = 0; /* XXX where to get from? */
2209
2210 ieee80211_radiotap_tx(vap, m);
2211 }
2212
2213 /* copy frame below our TX descriptor header */
2214 m_copydata(m, 0, m->m_pkthdr.len,
2215 data->buf + (sizeof(*mem) + sizeof(*txdesc)));
2216 /* calculate frame size */
2217 len = sizeof(*mem) + sizeof(*txdesc) + m->m_pkthdr.len;
2218 /* we need to align the frame to a 4 byte boundary */
2219 len = (len + 3) & ~3;
2220 /* calculate frame checksum */
2221 mem->chksum = upgt_chksum_le((uint32_t *)txdesc, len - sizeof(*mem));
2222 data->ni = ni;
2223 data->m = m;
2224 data->buflen = len;
2225
2226 DPRINTF(sc, UPGT_DEBUG_XMIT, "%s: TX start data sending (%d bytes)\n",
2227 __func__, len);
2228 KASSERT(len <= MCLBYTES, ("mbuf is small for saving data"));
2229
2230 upgt_bulk_tx(sc, data);
2231done:
2232 /*
2233 * If we don't regulary read the device statistics, the RX queue
2234 * will stall. It's strange, but it works, so we keep reading
2235 * the statistics here. *shrug*
2236 */
2237 if (!(ifp->if_opackets % UPGT_TX_STAT_INTERVAL))
2238 upgt_get_stats(sc);
2239
2240 return (error);
2241}
2242
2243static void
2244upgt_bulk_rx_callback(struct usb_xfer *xfer, usb_error_t error)
2245{
2246 struct upgt_softc *sc = usbd_xfer_softc(xfer);
2247 struct ifnet *ifp = sc->sc_ifp;
2248 struct ieee80211com *ic = ifp->if_l2com;
2249 struct ieee80211_frame *wh;
2250 struct ieee80211_node *ni;
2251 struct mbuf *m = NULL;
2252 struct upgt_data *data;
2253 int8_t nf;
2254 int rssi = -1;
2255
2256 UPGT_ASSERT_LOCKED(sc);
2257
2258 switch (USB_GET_STATE(xfer)) {
2259 case USB_ST_TRANSFERRED:
2260 data = STAILQ_FIRST(&sc->sc_rx_active);
2261 if (data == NULL)
2262 goto setup;
2263 STAILQ_REMOVE_HEAD(&sc->sc_rx_active, next);
2264 m = upgt_rxeof(xfer, data, &rssi);
2265 STAILQ_INSERT_TAIL(&sc->sc_rx_inactive, data, next);
2266 /* FALLTHROUGH */
2267 case USB_ST_SETUP:
2268setup:
2269 data = STAILQ_FIRST(&sc->sc_rx_inactive);
2270 if (data == NULL)
2271 return;
2272 STAILQ_REMOVE_HEAD(&sc->sc_rx_inactive, next);
2273 STAILQ_INSERT_TAIL(&sc->sc_rx_active, data, next);
2274 usbd_xfer_set_frame_data(xfer, 0, data->buf,
2275 usbd_xfer_max_len(xfer));
2276 usbd_transfer_submit(xfer);
2277
2278 /*
2279 * To avoid LOR we should unlock our private mutex here to call
2280 * ieee80211_input() because here is at the end of a USB
2281 * callback and safe to unlock.
2282 */
2283 UPGT_UNLOCK(sc);
2284 if (m != NULL) {
2285 wh = mtod(m, struct ieee80211_frame *);
2286 ni = ieee80211_find_rxnode(ic,
2287 (struct ieee80211_frame_min *)wh);
2288 nf = -95; /* XXX */
2289 if (ni != NULL) {
2290 (void) ieee80211_input(ni, m, rssi, nf);
2291 /* node is no longer needed */
2292 ieee80211_free_node(ni);
2293 } else
2294 (void) ieee80211_input_all(ic, m, rssi, nf);
2295 m = NULL;
2296 }
2297 if ((ifp->if_drv_flags & IFF_DRV_OACTIVE) == 0 &&
2298 !IFQ_IS_EMPTY(&ifp->if_snd))
2299 upgt_start(ifp);
2300 UPGT_LOCK(sc);
2301 break;
2302 default:
2303 /* needs it to the inactive queue due to a error. */
2304 data = STAILQ_FIRST(&sc->sc_rx_active);
2305 if (data != NULL) {
2306 STAILQ_REMOVE_HEAD(&sc->sc_rx_active, next);
2307 STAILQ_INSERT_TAIL(&sc->sc_rx_inactive, data, next);
2308 }
2309 if (error != USB_ERR_CANCELLED) {
2310 usbd_xfer_set_stall(xfer);
2311 ifp->if_ierrors++;
2312 goto setup;
2313 }
2314 break;
2315 }
2316}
2317
2318static void
2319upgt_bulk_tx_callback(struct usb_xfer *xfer, usb_error_t error)
2320{
2321 struct upgt_softc *sc = usbd_xfer_softc(xfer);
2322 struct ifnet *ifp = sc->sc_ifp;
2323 struct upgt_data *data;
2324
2325 UPGT_ASSERT_LOCKED(sc);
2326 switch (USB_GET_STATE(xfer)) {
2327 case USB_ST_TRANSFERRED:
2328 data = STAILQ_FIRST(&sc->sc_tx_active);
2329 if (data == NULL)
2330 goto setup;
2331 STAILQ_REMOVE_HEAD(&sc->sc_tx_active, next);
2332 UPGT_STAT_DEC(sc, st_tx_active);
2333 upgt_txeof(xfer, data);
2334 STAILQ_INSERT_TAIL(&sc->sc_tx_inactive, data, next);
2335 UPGT_STAT_INC(sc, st_tx_inactive);
2336 /* FALLTHROUGH */
2337 case USB_ST_SETUP:
2338setup:
2339 data = STAILQ_FIRST(&sc->sc_tx_pending);
2340 if (data == NULL) {
2341 DPRINTF(sc, UPGT_DEBUG_XMIT, "%s: empty pending queue\n",
2342 __func__);
2343 return;
2344 }
2345 STAILQ_REMOVE_HEAD(&sc->sc_tx_pending, next);
2346 UPGT_STAT_DEC(sc, st_tx_pending);
2347 STAILQ_INSERT_TAIL(&sc->sc_tx_active, data, next);
2348 UPGT_STAT_INC(sc, st_tx_active);
2349
2350 usbd_xfer_set_frame_data(xfer, 0, data->buf, data->buflen);
2351 usbd_transfer_submit(xfer);
2352 UPGT_UNLOCK(sc);
2353 upgt_start(ifp);
2354 UPGT_LOCK(sc);
2355 break;
2356 default:
2357 data = STAILQ_FIRST(&sc->sc_tx_active);
2358 if (data == NULL)
2359 goto setup;
2360 if (data->ni != NULL) {
2361 ieee80211_free_node(data->ni);
2362 data->ni = NULL;
2363 ifp->if_oerrors++;
2364 }
2365 if (error != USB_ERR_CANCELLED) {
2366 usbd_xfer_set_stall(xfer);
2367 goto setup;
2368 }
2369 break;
2370 }
2371}
2372
2373static device_method_t upgt_methods[] = {
2374 /* Device interface */
2375 DEVMETHOD(device_probe, upgt_match),
2376 DEVMETHOD(device_attach, upgt_attach),
2377 DEVMETHOD(device_detach, upgt_detach),
2378
2379 { 0, 0 }
2380};
2381
2382static driver_t upgt_driver = {
2383 "upgt",
2384 upgt_methods,
2385 sizeof(struct upgt_softc)
2386};
2387
2388static devclass_t upgt_devclass;
2389
2390DRIVER_MODULE(if_upgt, uhub, upgt_driver, upgt_devclass, NULL, 0);
2391MODULE_VERSION(if_upgt, 1);
2392MODULE_DEPEND(if_upgt, usb, 1, 1, 1);
2393MODULE_DEPEND(if_upgt, wlan, 1, 1, 1);
2394MODULE_DEPEND(if_upgt, upgtfw_fw, 1, 1, 1);
2/* $FreeBSD: head/sys/dev/usb/wlan/if_upgt.c 212131 2010-09-02 03:50:11Z thompsa $ */
3
4/*
5 * Copyright (c) 2007 Marcus Glocker <mglocker@openbsd.org>
6 *
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 */
19
20#include <sys/param.h>
21#include <sys/systm.h>
22#include <sys/kernel.h>
23#include <sys/endian.h>
24#include <sys/firmware.h>
25#include <sys/linker.h>
26#include <sys/mbuf.h>
27#include <sys/malloc.h>
28#include <sys/module.h>
29#include <sys/socket.h>
30#include <sys/sockio.h>
31#include <sys/sysctl.h>
32
33#include <net/if.h>
34#include <net/if_arp.h>
35#include <net/ethernet.h>
36#include <net/if_dl.h>
37#include <net/if_media.h>
38#include <net/if_types.h>
39
40#include <sys/bus.h>
41#include <machine/bus.h>
42
43#include <net80211/ieee80211_var.h>
44#include <net80211/ieee80211_phy.h>
45#include <net80211/ieee80211_radiotap.h>
46#include <net80211/ieee80211_regdomain.h>
47
48#include <net/bpf.h>
49
50#include <dev/usb/usb.h>
51#include <dev/usb/usbdi.h>
52#include "usbdevs.h"
53
54#include <dev/usb/wlan/if_upgtvar.h>
55
56/*
57 * Driver for the USB PrismGT devices.
58 *
59 * For now just USB 2.0 devices with the GW3887 chipset are supported.
60 * The driver has been written based on the firmware version 2.13.1.0_LM87.
61 *
62 * TODO's:
63 * - MONITOR mode test.
64 * - Add HOSTAP mode.
65 * - Add IBSS mode.
66 * - Support the USB 1.0 devices (NET2280, ISL3880, ISL3886 chipsets).
67 *
68 * Parts of this driver has been influenced by reading the p54u driver
69 * written by Jean-Baptiste Note <jean-baptiste.note@m4x.org> and
70 * Sebastien Bourdeauducq <lekernel@prism54.org>.
71 */
72
73SYSCTL_NODE(_hw, OID_AUTO, upgt, CTLFLAG_RD, 0,
74 "USB PrismGT GW3887 driver parameters");
75
76#ifdef UPGT_DEBUG
77int upgt_debug = 0;
78SYSCTL_INT(_hw_upgt, OID_AUTO, debug, CTLFLAG_RW, &upgt_debug,
79 0, "control debugging printfs");
80TUNABLE_INT("hw.upgt.debug", &upgt_debug);
81enum {
82 UPGT_DEBUG_XMIT = 0x00000001, /* basic xmit operation */
83 UPGT_DEBUG_RECV = 0x00000002, /* basic recv operation */
84 UPGT_DEBUG_RESET = 0x00000004, /* reset processing */
85 UPGT_DEBUG_INTR = 0x00000008, /* INTR */
86 UPGT_DEBUG_TX_PROC = 0x00000010, /* tx ISR proc */
87 UPGT_DEBUG_RX_PROC = 0x00000020, /* rx ISR proc */
88 UPGT_DEBUG_STATE = 0x00000040, /* 802.11 state transitions */
89 UPGT_DEBUG_STAT = 0x00000080, /* statistic */
90 UPGT_DEBUG_FW = 0x00000100, /* firmware */
91 UPGT_DEBUG_ANY = 0xffffffff
92};
93#define DPRINTF(sc, m, fmt, ...) do { \
94 if (sc->sc_debug & (m)) \
95 printf(fmt, __VA_ARGS__); \
96} while (0)
97#else
98#define DPRINTF(sc, m, fmt, ...) do { \
99 (void) sc; \
100} while (0)
101#endif
102
103/*
104 * Prototypes.
105 */
106static device_probe_t upgt_match;
107static device_attach_t upgt_attach;
108static device_detach_t upgt_detach;
109static int upgt_alloc_tx(struct upgt_softc *);
110static int upgt_alloc_rx(struct upgt_softc *);
111static int upgt_device_reset(struct upgt_softc *);
112static void upgt_bulk_tx(struct upgt_softc *, struct upgt_data *);
113static int upgt_fw_verify(struct upgt_softc *);
114static int upgt_mem_init(struct upgt_softc *);
115static int upgt_fw_load(struct upgt_softc *);
116static int upgt_fw_copy(const uint8_t *, char *, int);
117static uint32_t upgt_crc32_le(const void *, size_t);
118static struct mbuf *
119 upgt_rxeof(struct usb_xfer *, struct upgt_data *, int *);
120static struct mbuf *
121 upgt_rx(struct upgt_softc *, uint8_t *, int, int *);
122static void upgt_txeof(struct usb_xfer *, struct upgt_data *);
123static int upgt_eeprom_read(struct upgt_softc *);
124static int upgt_eeprom_parse(struct upgt_softc *);
125static void upgt_eeprom_parse_hwrx(struct upgt_softc *, uint8_t *);
126static void upgt_eeprom_parse_freq3(struct upgt_softc *, uint8_t *, int);
127static void upgt_eeprom_parse_freq4(struct upgt_softc *, uint8_t *, int);
128static void upgt_eeprom_parse_freq6(struct upgt_softc *, uint8_t *, int);
129static uint32_t upgt_chksum_le(const uint32_t *, size_t);
130static void upgt_tx_done(struct upgt_softc *, uint8_t *);
131static void upgt_init(void *);
132static void upgt_init_locked(struct upgt_softc *);
133static int upgt_ioctl(struct ifnet *, u_long, caddr_t);
134static void upgt_start(struct ifnet *);
135static int upgt_raw_xmit(struct ieee80211_node *, struct mbuf *,
136 const struct ieee80211_bpf_params *);
137static void upgt_scan_start(struct ieee80211com *);
138static void upgt_scan_end(struct ieee80211com *);
139static void upgt_set_channel(struct ieee80211com *);
140static struct ieee80211vap *upgt_vap_create(struct ieee80211com *,
141 const char name[IFNAMSIZ], int unit, int opmode,
142 int flags, const uint8_t bssid[IEEE80211_ADDR_LEN],
143 const uint8_t mac[IEEE80211_ADDR_LEN]);
144static void upgt_vap_delete(struct ieee80211vap *);
145static void upgt_update_mcast(struct ifnet *);
146static uint8_t upgt_rx_rate(struct upgt_softc *, const int);
147static void upgt_set_multi(void *);
148static void upgt_stop(struct upgt_softc *);
149static void upgt_setup_rates(struct ieee80211vap *, struct ieee80211com *);
150static int upgt_set_macfilter(struct upgt_softc *, uint8_t);
151static int upgt_newstate(struct ieee80211vap *, enum ieee80211_state, int);
152static void upgt_set_chan(struct upgt_softc *, struct ieee80211_channel *);
153static void upgt_set_led(struct upgt_softc *, int);
154static void upgt_set_led_blink(void *);
155static void upgt_get_stats(struct upgt_softc *);
156static void upgt_mem_free(struct upgt_softc *, uint32_t);
157static uint32_t upgt_mem_alloc(struct upgt_softc *);
158static void upgt_free_tx(struct upgt_softc *);
159static void upgt_free_rx(struct upgt_softc *);
160static void upgt_watchdog(void *);
161static void upgt_abort_xfers(struct upgt_softc *);
162static void upgt_abort_xfers_locked(struct upgt_softc *);
163static void upgt_sysctl_node(struct upgt_softc *);
164static struct upgt_data *
165 upgt_getbuf(struct upgt_softc *);
166static struct upgt_data *
167 upgt_gettxbuf(struct upgt_softc *);
168static int upgt_tx_start(struct upgt_softc *, struct mbuf *,
169 struct ieee80211_node *, struct upgt_data *);
170
171static const char *upgt_fwname = "upgt-gw3887";
172
173static const struct usb_device_id upgt_devs_2[] = {
174#define UPGT_DEV(v,p) { USB_VP(USB_VENDOR_##v, USB_PRODUCT_##v##_##p) }
175 /* version 2 devices */
176 UPGT_DEV(ACCTON, PRISM_GT),
177 UPGT_DEV(BELKIN, F5D7050),
178 UPGT_DEV(CISCOLINKSYS, WUSB54AG),
179 UPGT_DEV(CONCEPTRONIC, PRISM_GT),
180 UPGT_DEV(DELL, PRISM_GT_1),
181 UPGT_DEV(DELL, PRISM_GT_2),
182 UPGT_DEV(FSC, E5400),
183 UPGT_DEV(GLOBESPAN, PRISM_GT_1),
184 UPGT_DEV(GLOBESPAN, PRISM_GT_2),
185 UPGT_DEV(INTERSIL, PRISM_GT),
186 UPGT_DEV(SMC, 2862WG),
187 UPGT_DEV(USR, USR5422),
188 UPGT_DEV(WISTRONNEWEB, UR045G),
189 UPGT_DEV(XYRATEX, PRISM_GT_1),
190 UPGT_DEV(XYRATEX, PRISM_GT_2),
191 UPGT_DEV(ZCOM, XG703A),
192 UPGT_DEV(ZCOM, XM142)
193};
194
195static usb_callback_t upgt_bulk_rx_callback;
196static usb_callback_t upgt_bulk_tx_callback;
197
198static const struct usb_config upgt_config[UPGT_N_XFERS] = {
199 [UPGT_BULK_TX] = {
200 .type = UE_BULK,
201 .endpoint = UE_ADDR_ANY,
202 .direction = UE_DIR_OUT,
203 .bufsize = MCLBYTES,
204 .flags = {
205 .ext_buffer = 1,
206 .force_short_xfer = 1,
207 .pipe_bof = 1
208 },
209 .callback = upgt_bulk_tx_callback,
210 .timeout = UPGT_USB_TIMEOUT, /* ms */
211 },
212 [UPGT_BULK_RX] = {
213 .type = UE_BULK,
214 .endpoint = UE_ADDR_ANY,
215 .direction = UE_DIR_IN,
216 .bufsize = MCLBYTES,
217 .flags = {
218 .ext_buffer = 1,
219 .pipe_bof = 1,
220 .short_xfer_ok = 1
221 },
222 .callback = upgt_bulk_rx_callback,
223 },
224};
225
226static int
227upgt_match(device_t dev)
228{
229 struct usb_attach_arg *uaa = device_get_ivars(dev);
230
231 if (uaa->usb_mode != USB_MODE_HOST)
232 return (ENXIO);
233 if (uaa->info.bConfigIndex != UPGT_CONFIG_INDEX)
234 return (ENXIO);
235 if (uaa->info.bIfaceIndex != UPGT_IFACE_INDEX)
236 return (ENXIO);
237
238 return (usbd_lookup_id_by_uaa(upgt_devs_2, sizeof(upgt_devs_2), uaa));
239}
240
241static int
242upgt_attach(device_t dev)
243{
244 int error;
245 struct ieee80211com *ic;
246 struct ifnet *ifp;
247 struct upgt_softc *sc = device_get_softc(dev);
248 struct usb_attach_arg *uaa = device_get_ivars(dev);
249 uint8_t bands, iface_index = UPGT_IFACE_INDEX;
250
251 sc->sc_dev = dev;
252 sc->sc_udev = uaa->device;
253#ifdef UPGT_DEBUG
254 sc->sc_debug = upgt_debug;
255#endif
256 device_set_usb_desc(dev);
257
258 mtx_init(&sc->sc_mtx, device_get_nameunit(sc->sc_dev), MTX_NETWORK_LOCK,
259 MTX_DEF);
260 callout_init(&sc->sc_led_ch, 0);
261 callout_init(&sc->sc_watchdog_ch, 0);
262
263 /* Allocate TX and RX xfers. */
264 error = upgt_alloc_tx(sc);
265 if (error)
266 goto fail1;
267 error = upgt_alloc_rx(sc);
268 if (error)
269 goto fail2;
270
271 error = usbd_transfer_setup(uaa->device, &iface_index, sc->sc_xfer,
272 upgt_config, UPGT_N_XFERS, sc, &sc->sc_mtx);
273 if (error) {
274 device_printf(dev, "could not allocate USB transfers, "
275 "err=%s\n", usbd_errstr(error));
276 goto fail3;
277 }
278
279 ifp = sc->sc_ifp = if_alloc(IFT_IEEE80211);
280 if (ifp == NULL) {
281 device_printf(dev, "can not if_alloc()\n");
282 goto fail4;
283 }
284
285 /* Initialize the device. */
286 error = upgt_device_reset(sc);
287 if (error)
288 goto fail5;
289 /* Verify the firmware. */
290 error = upgt_fw_verify(sc);
291 if (error)
292 goto fail5;
293 /* Calculate device memory space. */
294 if (sc->sc_memaddr_frame_start == 0 || sc->sc_memaddr_frame_end == 0) {
295 device_printf(dev,
296 "could not find memory space addresses on FW\n");
297 error = EIO;
298 goto fail5;
299 }
300 sc->sc_memaddr_frame_end -= UPGT_MEMSIZE_RX + 1;
301 sc->sc_memaddr_rx_start = sc->sc_memaddr_frame_end + 1;
302
303 DPRINTF(sc, UPGT_DEBUG_FW, "memory address frame start=0x%08x\n",
304 sc->sc_memaddr_frame_start);
305 DPRINTF(sc, UPGT_DEBUG_FW, "memory address frame end=0x%08x\n",
306 sc->sc_memaddr_frame_end);
307 DPRINTF(sc, UPGT_DEBUG_FW, "memory address rx start=0x%08x\n",
308 sc->sc_memaddr_rx_start);
309
310 upgt_mem_init(sc);
311
312 /* Load the firmware. */
313 error = upgt_fw_load(sc);
314 if (error)
315 goto fail5;
316
317 /* Read the whole EEPROM content and parse it. */
318 error = upgt_eeprom_read(sc);
319 if (error)
320 goto fail5;
321 error = upgt_eeprom_parse(sc);
322 if (error)
323 goto fail5;
324
325 /* all works related with the device have done here. */
326 upgt_abort_xfers(sc);
327
328 /* Setup the 802.11 device. */
329 ifp->if_softc = sc;
330 if_initname(ifp, "upgt", device_get_unit(sc->sc_dev));
331 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
332 ifp->if_init = upgt_init;
333 ifp->if_ioctl = upgt_ioctl;
334 ifp->if_start = upgt_start;
335 IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
336 IFQ_SET_READY(&ifp->if_snd);
337
338 ic = ifp->if_l2com;
339 ic->ic_ifp = ifp;
340 ic->ic_phytype = IEEE80211_T_OFDM; /* not only, but not used */
341 ic->ic_opmode = IEEE80211_M_STA;
342 /* set device capabilities */
343 ic->ic_caps =
344 IEEE80211_C_STA /* station mode */
345 | IEEE80211_C_MONITOR /* monitor mode */
346 | IEEE80211_C_SHPREAMBLE /* short preamble supported */
347 | IEEE80211_C_SHSLOT /* short slot time supported */
348 | IEEE80211_C_BGSCAN /* capable of bg scanning */
349 | IEEE80211_C_WPA /* 802.11i */
350 ;
351
352 bands = 0;
353 setbit(&bands, IEEE80211_MODE_11B);
354 setbit(&bands, IEEE80211_MODE_11G);
355 ieee80211_init_channels(ic, NULL, &bands);
356
357 ieee80211_ifattach(ic, sc->sc_myaddr);
358 ic->ic_raw_xmit = upgt_raw_xmit;
359 ic->ic_scan_start = upgt_scan_start;
360 ic->ic_scan_end = upgt_scan_end;
361 ic->ic_set_channel = upgt_set_channel;
362
363 ic->ic_vap_create = upgt_vap_create;
364 ic->ic_vap_delete = upgt_vap_delete;
365 ic->ic_update_mcast = upgt_update_mcast;
366
367 ieee80211_radiotap_attach(ic,
368 &sc->sc_txtap.wt_ihdr, sizeof(sc->sc_txtap),
369 UPGT_TX_RADIOTAP_PRESENT,
370 &sc->sc_rxtap.wr_ihdr, sizeof(sc->sc_rxtap),
371 UPGT_RX_RADIOTAP_PRESENT);
372
373 upgt_sysctl_node(sc);
374
375 if (bootverbose)
376 ieee80211_announce(ic);
377
378 return (0);
379
380fail5: if_free(ifp);
381fail4: usbd_transfer_unsetup(sc->sc_xfer, UPGT_N_XFERS);
382fail3: upgt_free_rx(sc);
383fail2: upgt_free_tx(sc);
384fail1: mtx_destroy(&sc->sc_mtx);
385
386 return (error);
387}
388
389static void
390upgt_txeof(struct usb_xfer *xfer, struct upgt_data *data)
391{
392 struct upgt_softc *sc = usbd_xfer_softc(xfer);
393 struct ifnet *ifp = sc->sc_ifp;
394 struct mbuf *m;
395
396 UPGT_ASSERT_LOCKED(sc);
397
398 /*
399 * Do any tx complete callback. Note this must be done before releasing
400 * the node reference.
401 */
402 if (data->m) {
403 m = data->m;
404 if (m->m_flags & M_TXCB) {
405 /* XXX status? */
406 ieee80211_process_callback(data->ni, m, 0);
407 }
408 m_freem(m);
409 data->m = NULL;
410 }
411 if (data->ni) {
412 ieee80211_free_node(data->ni);
413 data->ni = NULL;
414 }
415 ifp->if_opackets++;
416}
417
418static void
419upgt_get_stats(struct upgt_softc *sc)
420{
421 struct upgt_data *data_cmd;
422 struct upgt_lmac_mem *mem;
423 struct upgt_lmac_stats *stats;
424
425 data_cmd = upgt_getbuf(sc);
426 if (data_cmd == NULL) {
427 device_printf(sc->sc_dev, "%s: out of buffer.\n", __func__);
428 return;
429 }
430
431 /*
432 * Transmit the URB containing the CMD data.
433 */
434 bzero(data_cmd->buf, MCLBYTES);
435
436 mem = (struct upgt_lmac_mem *)data_cmd->buf;
437 mem->addr = htole32(sc->sc_memaddr_frame_start +
438 UPGT_MEMSIZE_FRAME_HEAD);
439
440 stats = (struct upgt_lmac_stats *)(mem + 1);
441
442 stats->header1.flags = 0;
443 stats->header1.type = UPGT_H1_TYPE_CTRL;
444 stats->header1.len = htole16(
445 sizeof(struct upgt_lmac_stats) - sizeof(struct upgt_lmac_header));
446
447 stats->header2.reqid = htole32(sc->sc_memaddr_frame_start);
448 stats->header2.type = htole16(UPGT_H2_TYPE_STATS);
449 stats->header2.flags = 0;
450
451 data_cmd->buflen = sizeof(*mem) + sizeof(*stats);
452
453 mem->chksum = upgt_chksum_le((uint32_t *)stats,
454 data_cmd->buflen - sizeof(*mem));
455
456 upgt_bulk_tx(sc, data_cmd);
457}
458
459static int
460upgt_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
461{
462 struct upgt_softc *sc = ifp->if_softc;
463 struct ieee80211com *ic = ifp->if_l2com;
464 struct ifreq *ifr = (struct ifreq *) data;
465 int error = 0, startall = 0;
466
467 switch (cmd) {
468 case SIOCSIFFLAGS:
469 if (ifp->if_flags & IFF_UP) {
470 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
471 if ((ifp->if_flags ^ sc->sc_if_flags) &
472 (IFF_ALLMULTI | IFF_PROMISC))
473 upgt_set_multi(sc);
474 } else {
475 upgt_init(sc);
476 startall = 1;
477 }
478 } else {
479 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
480 upgt_stop(sc);
481 }
482 sc->sc_if_flags = ifp->if_flags;
483 if (startall)
484 ieee80211_start_all(ic);
485 break;
486 case SIOCGIFMEDIA:
487 error = ifmedia_ioctl(ifp, ifr, &ic->ic_media, cmd);
488 break;
489 case SIOCGIFADDR:
490 error = ether_ioctl(ifp, cmd, data);
491 break;
492 default:
493 error = EINVAL;
494 break;
495 }
496 return error;
497}
498
499static void
500upgt_stop_locked(struct upgt_softc *sc)
501{
502 struct ifnet *ifp = sc->sc_ifp;
503
504 UPGT_ASSERT_LOCKED(sc);
505
506 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
507 upgt_set_macfilter(sc, IEEE80211_S_INIT);
508 upgt_abort_xfers_locked(sc);
509}
510
511static void
512upgt_stop(struct upgt_softc *sc)
513{
514 struct ifnet *ifp = sc->sc_ifp;
515
516 UPGT_LOCK(sc);
517 upgt_stop_locked(sc);
518 UPGT_UNLOCK(sc);
519
520 /* device down */
521 sc->sc_tx_timer = 0;
522 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
523 sc->sc_flags &= ~UPGT_FLAG_INITDONE;
524}
525
526static void
527upgt_set_led(struct upgt_softc *sc, int action)
528{
529 struct upgt_data *data_cmd;
530 struct upgt_lmac_mem *mem;
531 struct upgt_lmac_led *led;
532
533 data_cmd = upgt_getbuf(sc);
534 if (data_cmd == NULL) {
535 device_printf(sc->sc_dev, "%s: out of buffers.\n", __func__);
536 return;
537 }
538
539 /*
540 * Transmit the URB containing the CMD data.
541 */
542 bzero(data_cmd->buf, MCLBYTES);
543
544 mem = (struct upgt_lmac_mem *)data_cmd->buf;
545 mem->addr = htole32(sc->sc_memaddr_frame_start +
546 UPGT_MEMSIZE_FRAME_HEAD);
547
548 led = (struct upgt_lmac_led *)(mem + 1);
549
550 led->header1.flags = UPGT_H1_FLAGS_TX_NO_CALLBACK;
551 led->header1.type = UPGT_H1_TYPE_CTRL;
552 led->header1.len = htole16(
553 sizeof(struct upgt_lmac_led) -
554 sizeof(struct upgt_lmac_header));
555
556 led->header2.reqid = htole32(sc->sc_memaddr_frame_start);
557 led->header2.type = htole16(UPGT_H2_TYPE_LED);
558 led->header2.flags = 0;
559
560 switch (action) {
561 case UPGT_LED_OFF:
562 led->mode = htole16(UPGT_LED_MODE_SET);
563 led->action_fix = 0;
564 led->action_tmp = htole16(UPGT_LED_ACTION_OFF);
565 led->action_tmp_dur = 0;
566 break;
567 case UPGT_LED_ON:
568 led->mode = htole16(UPGT_LED_MODE_SET);
569 led->action_fix = 0;
570 led->action_tmp = htole16(UPGT_LED_ACTION_ON);
571 led->action_tmp_dur = 0;
572 break;
573 case UPGT_LED_BLINK:
574 if (sc->sc_state != IEEE80211_S_RUN) {
575 STAILQ_INSERT_TAIL(&sc->sc_tx_inactive, data_cmd, next);
576 return;
577 }
578 if (sc->sc_led_blink) {
579 /* previous blink was not finished */
580 STAILQ_INSERT_TAIL(&sc->sc_tx_inactive, data_cmd, next);
581 return;
582 }
583 led->mode = htole16(UPGT_LED_MODE_SET);
584 led->action_fix = htole16(UPGT_LED_ACTION_OFF);
585 led->action_tmp = htole16(UPGT_LED_ACTION_ON);
586 led->action_tmp_dur = htole16(UPGT_LED_ACTION_TMP_DUR);
587 /* lock blink */
588 sc->sc_led_blink = 1;
589 callout_reset(&sc->sc_led_ch, hz, upgt_set_led_blink, sc);
590 break;
591 default:
592 STAILQ_INSERT_TAIL(&sc->sc_tx_inactive, data_cmd, next);
593 return;
594 }
595
596 data_cmd->buflen = sizeof(*mem) + sizeof(*led);
597
598 mem->chksum = upgt_chksum_le((uint32_t *)led,
599 data_cmd->buflen - sizeof(*mem));
600
601 upgt_bulk_tx(sc, data_cmd);
602}
603
604static void
605upgt_set_led_blink(void *arg)
606{
607 struct upgt_softc *sc = arg;
608
609 /* blink finished, we are ready for a next one */
610 sc->sc_led_blink = 0;
611}
612
613static void
614upgt_init(void *priv)
615{
616 struct upgt_softc *sc = priv;
617 struct ifnet *ifp = sc->sc_ifp;
618 struct ieee80211com *ic = ifp->if_l2com;
619
620 UPGT_LOCK(sc);
621 upgt_init_locked(sc);
622 UPGT_UNLOCK(sc);
623
624 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
625 ieee80211_start_all(ic); /* start all vap's */
626}
627
628static void
629upgt_init_locked(struct upgt_softc *sc)
630{
631 struct ifnet *ifp = sc->sc_ifp;
632
633 UPGT_ASSERT_LOCKED(sc);
634
635 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
636 upgt_stop_locked(sc);
637
638 usbd_transfer_start(sc->sc_xfer[UPGT_BULK_RX]);
639
640 (void)upgt_set_macfilter(sc, IEEE80211_S_SCAN);
641
642 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
643 ifp->if_drv_flags |= IFF_DRV_RUNNING;
644 sc->sc_flags |= UPGT_FLAG_INITDONE;
645
646 callout_reset(&sc->sc_watchdog_ch, hz, upgt_watchdog, sc);
647}
648
649static int
650upgt_set_macfilter(struct upgt_softc *sc, uint8_t state)
651{
652 struct ifnet *ifp = sc->sc_ifp;
653 struct ieee80211com *ic = ifp->if_l2com;
654 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
655 struct ieee80211_node *ni;
656 struct upgt_data *data_cmd;
657 struct upgt_lmac_mem *mem;
658 struct upgt_lmac_filter *filter;
659 uint8_t broadcast[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
660
661 UPGT_ASSERT_LOCKED(sc);
662
663 data_cmd = upgt_getbuf(sc);
664 if (data_cmd == NULL) {
665 device_printf(sc->sc_dev, "out of TX buffers.\n");
666 return (ENOBUFS);
667 }
668
669 /*
670 * Transmit the URB containing the CMD data.
671 */
672 bzero(data_cmd->buf, MCLBYTES);
673
674 mem = (struct upgt_lmac_mem *)data_cmd->buf;
675 mem->addr = htole32(sc->sc_memaddr_frame_start +
676 UPGT_MEMSIZE_FRAME_HEAD);
677
678 filter = (struct upgt_lmac_filter *)(mem + 1);
679
680 filter->header1.flags = UPGT_H1_FLAGS_TX_NO_CALLBACK;
681 filter->header1.type = UPGT_H1_TYPE_CTRL;
682 filter->header1.len = htole16(
683 sizeof(struct upgt_lmac_filter) -
684 sizeof(struct upgt_lmac_header));
685
686 filter->header2.reqid = htole32(sc->sc_memaddr_frame_start);
687 filter->header2.type = htole16(UPGT_H2_TYPE_MACFILTER);
688 filter->header2.flags = 0;
689
690 switch (state) {
691 case IEEE80211_S_INIT:
692 DPRINTF(sc, UPGT_DEBUG_STATE, "%s: set MAC filter to INIT\n",
693 __func__);
694 filter->type = htole16(UPGT_FILTER_TYPE_RESET);
695 break;
696 case IEEE80211_S_SCAN:
697 DPRINTF(sc, UPGT_DEBUG_STATE,
698 "set MAC filter to SCAN (bssid %s)\n",
699 ether_sprintf(broadcast));
700 filter->type = htole16(UPGT_FILTER_TYPE_NONE);
701 IEEE80211_ADDR_COPY(filter->dst, sc->sc_myaddr);
702 IEEE80211_ADDR_COPY(filter->src, broadcast);
703 filter->unknown1 = htole16(UPGT_FILTER_UNKNOWN1);
704 filter->rxaddr = htole32(sc->sc_memaddr_rx_start);
705 filter->unknown2 = htole16(UPGT_FILTER_UNKNOWN2);
706 filter->rxhw = htole32(sc->sc_eeprom_hwrx);
707 filter->unknown3 = htole16(UPGT_FILTER_UNKNOWN3);
708 break;
709 case IEEE80211_S_RUN:
710 ni = ieee80211_ref_node(vap->iv_bss);
711 /* XXX monitor mode isn't tested yet. */
712 if (vap->iv_opmode == IEEE80211_M_MONITOR) {
713 filter->type = htole16(UPGT_FILTER_TYPE_MONITOR);
714 IEEE80211_ADDR_COPY(filter->dst, sc->sc_myaddr);
715 IEEE80211_ADDR_COPY(filter->src, ni->ni_bssid);
716 filter->unknown1 = htole16(UPGT_FILTER_MONITOR_UNKNOWN1);
717 filter->rxaddr = htole32(sc->sc_memaddr_rx_start);
718 filter->unknown2 = htole16(UPGT_FILTER_MONITOR_UNKNOWN2);
719 filter->rxhw = htole32(sc->sc_eeprom_hwrx);
720 filter->unknown3 = htole16(UPGT_FILTER_MONITOR_UNKNOWN3);
721 } else {
722 DPRINTF(sc, UPGT_DEBUG_STATE,
723 "set MAC filter to RUN (bssid %s)\n",
724 ether_sprintf(ni->ni_bssid));
725 filter->type = htole16(UPGT_FILTER_TYPE_STA);
726 IEEE80211_ADDR_COPY(filter->dst, sc->sc_myaddr);
727 IEEE80211_ADDR_COPY(filter->src, ni->ni_bssid);
728 filter->unknown1 = htole16(UPGT_FILTER_UNKNOWN1);
729 filter->rxaddr = htole32(sc->sc_memaddr_rx_start);
730 filter->unknown2 = htole16(UPGT_FILTER_UNKNOWN2);
731 filter->rxhw = htole32(sc->sc_eeprom_hwrx);
732 filter->unknown3 = htole16(UPGT_FILTER_UNKNOWN3);
733 }
734 ieee80211_free_node(ni);
735 break;
736 default:
737 device_printf(sc->sc_dev,
738 "MAC filter does not know that state\n");
739 break;
740 }
741
742 data_cmd->buflen = sizeof(*mem) + sizeof(*filter);
743
744 mem->chksum = upgt_chksum_le((uint32_t *)filter,
745 data_cmd->buflen - sizeof(*mem));
746
747 upgt_bulk_tx(sc, data_cmd);
748
749 return (0);
750}
751
752static void
753upgt_setup_rates(struct ieee80211vap *vap, struct ieee80211com *ic)
754{
755 struct ifnet *ifp = ic->ic_ifp;
756 struct upgt_softc *sc = ifp->if_softc;
757 const struct ieee80211_txparam *tp;
758
759 /*
760 * 0x01 = OFMD6 0x10 = DS1
761 * 0x04 = OFDM9 0x11 = DS2
762 * 0x06 = OFDM12 0x12 = DS5
763 * 0x07 = OFDM18 0x13 = DS11
764 * 0x08 = OFDM24
765 * 0x09 = OFDM36
766 * 0x0a = OFDM48
767 * 0x0b = OFDM54
768 */
769 const uint8_t rateset_auto_11b[] =
770 { 0x13, 0x13, 0x12, 0x11, 0x11, 0x10, 0x10, 0x10 };
771 const uint8_t rateset_auto_11g[] =
772 { 0x0b, 0x0a, 0x09, 0x08, 0x07, 0x06, 0x04, 0x01 };
773 const uint8_t rateset_fix_11bg[] =
774 { 0x10, 0x11, 0x12, 0x13, 0x01, 0x04, 0x06, 0x07,
775 0x08, 0x09, 0x0a, 0x0b };
776
777 tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
778
779 /* XXX */
780 if (tp->ucastrate == IEEE80211_FIXED_RATE_NONE) {
781 /*
782 * Automatic rate control is done by the device.
783 * We just pass the rateset from which the device
784 * will pickup a rate.
785 */
786 if (ic->ic_curmode == IEEE80211_MODE_11B)
787 bcopy(rateset_auto_11b, sc->sc_cur_rateset,
788 sizeof(sc->sc_cur_rateset));
789 if (ic->ic_curmode == IEEE80211_MODE_11G ||
790 ic->ic_curmode == IEEE80211_MODE_AUTO)
791 bcopy(rateset_auto_11g, sc->sc_cur_rateset,
792 sizeof(sc->sc_cur_rateset));
793 } else {
794 /* set a fixed rate */
795 memset(sc->sc_cur_rateset, rateset_fix_11bg[tp->ucastrate],
796 sizeof(sc->sc_cur_rateset));
797 }
798}
799
800static void
801upgt_set_multi(void *arg)
802{
803 struct upgt_softc *sc = arg;
804 struct ifnet *ifp = sc->sc_ifp;
805
806 if (!(ifp->if_flags & IFF_UP))
807 return;
808
809 /*
810 * XXX don't know how to set a device. Lack of docs. Just try to set
811 * IFF_ALLMULTI flag here.
812 */
813 ifp->if_flags |= IFF_ALLMULTI;
814}
815
816static void
817upgt_start(struct ifnet *ifp)
818{
819 struct upgt_softc *sc = ifp->if_softc;
820 struct upgt_data *data_tx;
821 struct ieee80211_node *ni;
822 struct mbuf *m;
823
824 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
825 return;
826
827 UPGT_LOCK(sc);
828 for (;;) {
829 IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
830 if (m == NULL)
831 break;
832
833 data_tx = upgt_gettxbuf(sc);
834 if (data_tx == NULL) {
835 IFQ_DRV_PREPEND(&ifp->if_snd, m);
836 break;
837 }
838
839 ni = (struct ieee80211_node *)m->m_pkthdr.rcvif;
840 m->m_pkthdr.rcvif = NULL;
841
842 if (upgt_tx_start(sc, m, ni, data_tx) != 0) {
843 STAILQ_INSERT_HEAD(&sc->sc_tx_inactive, data_tx, next);
844 UPGT_STAT_INC(sc, st_tx_inactive);
845 ieee80211_free_node(ni);
846 ifp->if_oerrors++;
847 continue;
848 }
849 sc->sc_tx_timer = 5;
850 }
851 UPGT_UNLOCK(sc);
852}
853
854static int
855upgt_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
856 const struct ieee80211_bpf_params *params)
857{
858 struct ieee80211com *ic = ni->ni_ic;
859 struct ifnet *ifp = ic->ic_ifp;
860 struct upgt_softc *sc = ifp->if_softc;
861 struct upgt_data *data_tx = NULL;
862
863 /* prevent management frames from being sent if we're not ready */
864 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
865 m_freem(m);
866 ieee80211_free_node(ni);
867 return ENETDOWN;
868 }
869
870 UPGT_LOCK(sc);
871 data_tx = upgt_gettxbuf(sc);
872 if (data_tx == NULL) {
873 ieee80211_free_node(ni);
874 m_freem(m);
875 UPGT_UNLOCK(sc);
876 return (ENOBUFS);
877 }
878
879 if (upgt_tx_start(sc, m, ni, data_tx) != 0) {
880 STAILQ_INSERT_HEAD(&sc->sc_tx_inactive, data_tx, next);
881 UPGT_STAT_INC(sc, st_tx_inactive);
882 ieee80211_free_node(ni);
883 ifp->if_oerrors++;
884 UPGT_UNLOCK(sc);
885 return (EIO);
886 }
887 UPGT_UNLOCK(sc);
888
889 sc->sc_tx_timer = 5;
890 return (0);
891}
892
893static void
894upgt_watchdog(void *arg)
895{
896 struct upgt_softc *sc = arg;
897 struct ifnet *ifp = sc->sc_ifp;
898
899 if (sc->sc_tx_timer > 0) {
900 if (--sc->sc_tx_timer == 0) {
901 device_printf(sc->sc_dev, "watchdog timeout\n");
902 /* upgt_init(ifp); XXX needs a process context ? */
903 ifp->if_oerrors++;
904 return;
905 }
906 callout_reset(&sc->sc_watchdog_ch, hz, upgt_watchdog, sc);
907 }
908}
909
910static uint32_t
911upgt_mem_alloc(struct upgt_softc *sc)
912{
913 int i;
914
915 for (i = 0; i < sc->sc_memory.pages; i++) {
916 if (sc->sc_memory.page[i].used == 0) {
917 sc->sc_memory.page[i].used = 1;
918 return (sc->sc_memory.page[i].addr);
919 }
920 }
921
922 return (0);
923}
924
925static void
926upgt_scan_start(struct ieee80211com *ic)
927{
928 /* do nothing. */
929}
930
931static void
932upgt_scan_end(struct ieee80211com *ic)
933{
934 /* do nothing. */
935}
936
937static void
938upgt_set_channel(struct ieee80211com *ic)
939{
940 struct upgt_softc *sc = ic->ic_ifp->if_softc;
941
942 UPGT_LOCK(sc);
943 upgt_set_chan(sc, ic->ic_curchan);
944 UPGT_UNLOCK(sc);
945}
946
947static void
948upgt_set_chan(struct upgt_softc *sc, struct ieee80211_channel *c)
949{
950 struct ifnet *ifp = sc->sc_ifp;
951 struct ieee80211com *ic = ifp->if_l2com;
952 struct upgt_data *data_cmd;
953 struct upgt_lmac_mem *mem;
954 struct upgt_lmac_channel *chan;
955 int channel;
956
957 UPGT_ASSERT_LOCKED(sc);
958
959 channel = ieee80211_chan2ieee(ic, c);
960 if (channel == 0 || channel == IEEE80211_CHAN_ANY) {
961 /* XXX should NEVER happen */
962 device_printf(sc->sc_dev,
963 "%s: invalid channel %x\n", __func__, channel);
964 return;
965 }
966
967 DPRINTF(sc, UPGT_DEBUG_STATE, "%s: channel %d\n", __func__, channel);
968
969 data_cmd = upgt_getbuf(sc);
970 if (data_cmd == NULL) {
971 device_printf(sc->sc_dev, "%s: out of buffers.\n", __func__);
972 return;
973 }
974 /*
975 * Transmit the URB containing the CMD data.
976 */
977 bzero(data_cmd->buf, MCLBYTES);
978
979 mem = (struct upgt_lmac_mem *)data_cmd->buf;
980 mem->addr = htole32(sc->sc_memaddr_frame_start +
981 UPGT_MEMSIZE_FRAME_HEAD);
982
983 chan = (struct upgt_lmac_channel *)(mem + 1);
984
985 chan->header1.flags = UPGT_H1_FLAGS_TX_NO_CALLBACK;
986 chan->header1.type = UPGT_H1_TYPE_CTRL;
987 chan->header1.len = htole16(
988 sizeof(struct upgt_lmac_channel) - sizeof(struct upgt_lmac_header));
989
990 chan->header2.reqid = htole32(sc->sc_memaddr_frame_start);
991 chan->header2.type = htole16(UPGT_H2_TYPE_CHANNEL);
992 chan->header2.flags = 0;
993
994 chan->unknown1 = htole16(UPGT_CHANNEL_UNKNOWN1);
995 chan->unknown2 = htole16(UPGT_CHANNEL_UNKNOWN2);
996 chan->freq6 = sc->sc_eeprom_freq6[channel];
997 chan->settings = sc->sc_eeprom_freq6_settings;
998 chan->unknown3 = UPGT_CHANNEL_UNKNOWN3;
999
1000 bcopy(&sc->sc_eeprom_freq3[channel].data, chan->freq3_1,
1001 sizeof(chan->freq3_1));
1002 bcopy(&sc->sc_eeprom_freq4[channel], chan->freq4,
1003 sizeof(sc->sc_eeprom_freq4[channel]));
1004 bcopy(&sc->sc_eeprom_freq3[channel].data, chan->freq3_2,
1005 sizeof(chan->freq3_2));
1006
1007 data_cmd->buflen = sizeof(*mem) + sizeof(*chan);
1008
1009 mem->chksum = upgt_chksum_le((uint32_t *)chan,
1010 data_cmd->buflen - sizeof(*mem));
1011
1012 upgt_bulk_tx(sc, data_cmd);
1013}
1014
1015static struct ieee80211vap *
1016upgt_vap_create(struct ieee80211com *ic,
1017 const char name[IFNAMSIZ], int unit, int opmode, int flags,
1018 const uint8_t bssid[IEEE80211_ADDR_LEN],
1019 const uint8_t mac[IEEE80211_ADDR_LEN])
1020{
1021 struct upgt_vap *uvp;
1022 struct ieee80211vap *vap;
1023
1024 if (!TAILQ_EMPTY(&ic->ic_vaps)) /* only one at a time */
1025 return NULL;
1026 uvp = (struct upgt_vap *) malloc(sizeof(struct upgt_vap),
1027 M_80211_VAP, M_NOWAIT | M_ZERO);
1028 if (uvp == NULL)
1029 return NULL;
1030 vap = &uvp->vap;
1031 /* enable s/w bmiss handling for sta mode */
1032 ieee80211_vap_setup(ic, vap, name, unit, opmode,
1033 flags | IEEE80211_CLONE_NOBEACONS, bssid, mac);
1034
1035 /* override state transition machine */
1036 uvp->newstate = vap->iv_newstate;
1037 vap->iv_newstate = upgt_newstate;
1038
1039 /* setup device rates */
1040 upgt_setup_rates(vap, ic);
1041
1042 /* complete setup */
1043 ieee80211_vap_attach(vap, ieee80211_media_change,
1044 ieee80211_media_status);
1045 ic->ic_opmode = opmode;
1046 return vap;
1047}
1048
1049static int
1050upgt_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
1051{
1052 struct upgt_vap *uvp = UPGT_VAP(vap);
1053 struct ieee80211com *ic = vap->iv_ic;
1054 struct upgt_softc *sc = ic->ic_ifp->if_softc;
1055
1056 /* do it in a process context */
1057 sc->sc_state = nstate;
1058
1059 IEEE80211_UNLOCK(ic);
1060 UPGT_LOCK(sc);
1061 callout_stop(&sc->sc_led_ch);
1062 callout_stop(&sc->sc_watchdog_ch);
1063
1064 switch (nstate) {
1065 case IEEE80211_S_INIT:
1066 /* do not accept any frames if the device is down */
1067 (void)upgt_set_macfilter(sc, sc->sc_state);
1068 upgt_set_led(sc, UPGT_LED_OFF);
1069 break;
1070 case IEEE80211_S_SCAN:
1071 upgt_set_chan(sc, ic->ic_curchan);
1072 break;
1073 case IEEE80211_S_AUTH:
1074 upgt_set_chan(sc, ic->ic_curchan);
1075 break;
1076 case IEEE80211_S_ASSOC:
1077 break;
1078 case IEEE80211_S_RUN:
1079 upgt_set_macfilter(sc, sc->sc_state);
1080 upgt_set_led(sc, UPGT_LED_ON);
1081 break;
1082 default:
1083 break;
1084 }
1085 UPGT_UNLOCK(sc);
1086 IEEE80211_LOCK(ic);
1087 return (uvp->newstate(vap, nstate, arg));
1088}
1089
1090static void
1091upgt_vap_delete(struct ieee80211vap *vap)
1092{
1093 struct upgt_vap *uvp = UPGT_VAP(vap);
1094
1095 ieee80211_vap_detach(vap);
1096 free(uvp, M_80211_VAP);
1097}
1098
1099static void
1100upgt_update_mcast(struct ifnet *ifp)
1101{
1102 struct upgt_softc *sc = ifp->if_softc;
1103
1104 upgt_set_multi(sc);
1105}
1106
1107static int
1108upgt_eeprom_parse(struct upgt_softc *sc)
1109{
1110 struct upgt_eeprom_header *eeprom_header;
1111 struct upgt_eeprom_option *eeprom_option;
1112 uint16_t option_len;
1113 uint16_t option_type;
1114 uint16_t preamble_len;
1115 int option_end = 0;
1116
1117 /* calculate eeprom options start offset */
1118 eeprom_header = (struct upgt_eeprom_header *)sc->sc_eeprom;
1119 preamble_len = le16toh(eeprom_header->preamble_len);
1120 eeprom_option = (struct upgt_eeprom_option *)(sc->sc_eeprom +
1121 (sizeof(struct upgt_eeprom_header) + preamble_len));
1122
1123 while (!option_end) {
1124 /* the eeprom option length is stored in words */
1125 option_len =
1126 (le16toh(eeprom_option->len) - 1) * sizeof(uint16_t);
1127 option_type =
1128 le16toh(eeprom_option->type);
1129
1130 switch (option_type) {
1131 case UPGT_EEPROM_TYPE_NAME:
1132 DPRINTF(sc, UPGT_DEBUG_FW,
1133 "EEPROM name len=%d\n", option_len);
1134 break;
1135 case UPGT_EEPROM_TYPE_SERIAL:
1136 DPRINTF(sc, UPGT_DEBUG_FW,
1137 "EEPROM serial len=%d\n", option_len);
1138 break;
1139 case UPGT_EEPROM_TYPE_MAC:
1140 DPRINTF(sc, UPGT_DEBUG_FW,
1141 "EEPROM mac len=%d\n", option_len);
1142
1143 IEEE80211_ADDR_COPY(sc->sc_myaddr, eeprom_option->data);
1144 break;
1145 case UPGT_EEPROM_TYPE_HWRX:
1146 DPRINTF(sc, UPGT_DEBUG_FW,
1147 "EEPROM hwrx len=%d\n", option_len);
1148
1149 upgt_eeprom_parse_hwrx(sc, eeprom_option->data);
1150 break;
1151 case UPGT_EEPROM_TYPE_CHIP:
1152 DPRINTF(sc, UPGT_DEBUG_FW,
1153 "EEPROM chip len=%d\n", option_len);
1154 break;
1155 case UPGT_EEPROM_TYPE_FREQ3:
1156 DPRINTF(sc, UPGT_DEBUG_FW,
1157 "EEPROM freq3 len=%d\n", option_len);
1158
1159 upgt_eeprom_parse_freq3(sc, eeprom_option->data,
1160 option_len);
1161 break;
1162 case UPGT_EEPROM_TYPE_FREQ4:
1163 DPRINTF(sc, UPGT_DEBUG_FW,
1164 "EEPROM freq4 len=%d\n", option_len);
1165
1166 upgt_eeprom_parse_freq4(sc, eeprom_option->data,
1167 option_len);
1168 break;
1169 case UPGT_EEPROM_TYPE_FREQ5:
1170 DPRINTF(sc, UPGT_DEBUG_FW,
1171 "EEPROM freq5 len=%d\n", option_len);
1172 break;
1173 case UPGT_EEPROM_TYPE_FREQ6:
1174 DPRINTF(sc, UPGT_DEBUG_FW,
1175 "EEPROM freq6 len=%d\n", option_len);
1176
1177 upgt_eeprom_parse_freq6(sc, eeprom_option->data,
1178 option_len);
1179 break;
1180 case UPGT_EEPROM_TYPE_END:
1181 DPRINTF(sc, UPGT_DEBUG_FW,
1182 "EEPROM end len=%d\n", option_len);
1183 option_end = 1;
1184 break;
1185 case UPGT_EEPROM_TYPE_OFF:
1186 DPRINTF(sc, UPGT_DEBUG_FW,
1187 "%s: EEPROM off without end option\n", __func__);
1188 return (EIO);
1189 default:
1190 DPRINTF(sc, UPGT_DEBUG_FW,
1191 "EEPROM unknown type 0x%04x len=%d\n",
1192 option_type, option_len);
1193 break;
1194 }
1195
1196 /* jump to next EEPROM option */
1197 eeprom_option = (struct upgt_eeprom_option *)
1198 (eeprom_option->data + option_len);
1199 }
1200
1201 return (0);
1202}
1203
1204static void
1205upgt_eeprom_parse_freq3(struct upgt_softc *sc, uint8_t *data, int len)
1206{
1207 struct upgt_eeprom_freq3_header *freq3_header;
1208 struct upgt_lmac_freq3 *freq3;
1209 int i, elements, flags;
1210 unsigned channel;
1211
1212 freq3_header = (struct upgt_eeprom_freq3_header *)data;
1213 freq3 = (struct upgt_lmac_freq3 *)(freq3_header + 1);
1214
1215 flags = freq3_header->flags;
1216 elements = freq3_header->elements;
1217
1218 DPRINTF(sc, UPGT_DEBUG_FW, "flags=0x%02x elements=%d\n",
1219 flags, elements);
1220
1221 for (i = 0; i < elements; i++) {
1222 channel = ieee80211_mhz2ieee(le16toh(freq3[i].freq), 0);
1223 if (!(channel >= 0 && channel < IEEE80211_CHAN_MAX))
1224 continue;
1225
1226 sc->sc_eeprom_freq3[channel] = freq3[i];
1227
1228 DPRINTF(sc, UPGT_DEBUG_FW, "frequence=%d, channel=%d\n",
1229 le16toh(sc->sc_eeprom_freq3[channel].freq), channel);
1230 }
1231}
1232
1233void
1234upgt_eeprom_parse_freq4(struct upgt_softc *sc, uint8_t *data, int len)
1235{
1236 struct upgt_eeprom_freq4_header *freq4_header;
1237 struct upgt_eeprom_freq4_1 *freq4_1;
1238 struct upgt_eeprom_freq4_2 *freq4_2;
1239 int i, j, elements, settings, flags;
1240 unsigned channel;
1241
1242 freq4_header = (struct upgt_eeprom_freq4_header *)data;
1243 freq4_1 = (struct upgt_eeprom_freq4_1 *)(freq4_header + 1);
1244 flags = freq4_header->flags;
1245 elements = freq4_header->elements;
1246 settings = freq4_header->settings;
1247
1248 /* we need this value later */
1249 sc->sc_eeprom_freq6_settings = freq4_header->settings;
1250
1251 DPRINTF(sc, UPGT_DEBUG_FW, "flags=0x%02x elements=%d settings=%d\n",
1252 flags, elements, settings);
1253
1254 for (i = 0; i < elements; i++) {
1255 channel = ieee80211_mhz2ieee(le16toh(freq4_1[i].freq), 0);
1256 if (!(channel >= 0 && channel < IEEE80211_CHAN_MAX))
1257 continue;
1258
1259 freq4_2 = (struct upgt_eeprom_freq4_2 *)freq4_1[i].data;
1260 for (j = 0; j < settings; j++) {
1261 sc->sc_eeprom_freq4[channel][j].cmd = freq4_2[j];
1262 sc->sc_eeprom_freq4[channel][j].pad = 0;
1263 }
1264
1265 DPRINTF(sc, UPGT_DEBUG_FW, "frequence=%d, channel=%d\n",
1266 le16toh(freq4_1[i].freq), channel);
1267 }
1268}
1269
1270void
1271upgt_eeprom_parse_freq6(struct upgt_softc *sc, uint8_t *data, int len)
1272{
1273 struct upgt_lmac_freq6 *freq6;
1274 int i, elements;
1275 unsigned channel;
1276
1277 freq6 = (struct upgt_lmac_freq6 *)data;
1278 elements = len / sizeof(struct upgt_lmac_freq6);
1279
1280 DPRINTF(sc, UPGT_DEBUG_FW, "elements=%d\n", elements);
1281
1282 for (i = 0; i < elements; i++) {
1283 channel = ieee80211_mhz2ieee(le16toh(freq6[i].freq), 0);
1284 if (!(channel >= 0 && channel < IEEE80211_CHAN_MAX))
1285 continue;
1286
1287 sc->sc_eeprom_freq6[channel] = freq6[i];
1288
1289 DPRINTF(sc, UPGT_DEBUG_FW, "frequence=%d, channel=%d\n",
1290 le16toh(sc->sc_eeprom_freq6[channel].freq), channel);
1291 }
1292}
1293
1294static void
1295upgt_eeprom_parse_hwrx(struct upgt_softc *sc, uint8_t *data)
1296{
1297 struct upgt_eeprom_option_hwrx *option_hwrx;
1298
1299 option_hwrx = (struct upgt_eeprom_option_hwrx *)data;
1300
1301 sc->sc_eeprom_hwrx = option_hwrx->rxfilter - UPGT_EEPROM_RX_CONST;
1302
1303 DPRINTF(sc, UPGT_DEBUG_FW, "hwrx option value=0x%04x\n",
1304 sc->sc_eeprom_hwrx);
1305}
1306
1307static int
1308upgt_eeprom_read(struct upgt_softc *sc)
1309{
1310 struct upgt_data *data_cmd;
1311 struct upgt_lmac_mem *mem;
1312 struct upgt_lmac_eeprom *eeprom;
1313 int block, error, offset;
1314
1315 UPGT_LOCK(sc);
1316 usb_pause_mtx(&sc->sc_mtx, 100);
1317
1318 offset = 0;
1319 block = UPGT_EEPROM_BLOCK_SIZE;
1320 while (offset < UPGT_EEPROM_SIZE) {
1321 DPRINTF(sc, UPGT_DEBUG_FW,
1322 "request EEPROM block (offset=%d, len=%d)\n", offset, block);
1323
1324 data_cmd = upgt_getbuf(sc);
1325 if (data_cmd == NULL) {
1326 UPGT_UNLOCK(sc);
1327 return (ENOBUFS);
1328 }
1329
1330 /*
1331 * Transmit the URB containing the CMD data.
1332 */
1333 bzero(data_cmd->buf, MCLBYTES);
1334
1335 mem = (struct upgt_lmac_mem *)data_cmd->buf;
1336 mem->addr = htole32(sc->sc_memaddr_frame_start +
1337 UPGT_MEMSIZE_FRAME_HEAD);
1338
1339 eeprom = (struct upgt_lmac_eeprom *)(mem + 1);
1340 eeprom->header1.flags = 0;
1341 eeprom->header1.type = UPGT_H1_TYPE_CTRL;
1342 eeprom->header1.len = htole16((
1343 sizeof(struct upgt_lmac_eeprom) -
1344 sizeof(struct upgt_lmac_header)) + block);
1345
1346 eeprom->header2.reqid = htole32(sc->sc_memaddr_frame_start);
1347 eeprom->header2.type = htole16(UPGT_H2_TYPE_EEPROM);
1348 eeprom->header2.flags = 0;
1349
1350 eeprom->offset = htole16(offset);
1351 eeprom->len = htole16(block);
1352
1353 data_cmd->buflen = sizeof(*mem) + sizeof(*eeprom) + block;
1354
1355 mem->chksum = upgt_chksum_le((uint32_t *)eeprom,
1356 data_cmd->buflen - sizeof(*mem));
1357 upgt_bulk_tx(sc, data_cmd);
1358
1359 error = mtx_sleep(sc, &sc->sc_mtx, 0, "eeprom_request", hz);
1360 if (error != 0) {
1361 device_printf(sc->sc_dev,
1362 "timeout while waiting for EEPROM data\n");
1363 UPGT_UNLOCK(sc);
1364 return (EIO);
1365 }
1366
1367 offset += block;
1368 if (UPGT_EEPROM_SIZE - offset < block)
1369 block = UPGT_EEPROM_SIZE - offset;
1370 }
1371
1372 UPGT_UNLOCK(sc);
1373 return (0);
1374}
1375
1376/*
1377 * When a rx data came in the function returns a mbuf and a rssi values.
1378 */
1379static struct mbuf *
1380upgt_rxeof(struct usb_xfer *xfer, struct upgt_data *data, int *rssi)
1381{
1382 struct mbuf *m = NULL;
1383 struct upgt_softc *sc = usbd_xfer_softc(xfer);
1384 struct upgt_lmac_header *header;
1385 struct upgt_lmac_eeprom *eeprom;
1386 uint8_t h1_type;
1387 uint16_t h2_type;
1388 int actlen, sumlen;
1389
1390 usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL);
1391
1392 UPGT_ASSERT_LOCKED(sc);
1393
1394 if (actlen < 1)
1395 return (NULL);
1396
1397 /* Check only at the very beginning. */
1398 if (!(sc->sc_flags & UPGT_FLAG_FWLOADED) &&
1399 (memcmp(data->buf, "OK", 2) == 0)) {
1400 sc->sc_flags |= UPGT_FLAG_FWLOADED;
1401 wakeup_one(sc);
1402 return (NULL);
1403 }
1404
1405 if (actlen < UPGT_RX_MINSZ)
1406 return (NULL);
1407
1408 /*
1409 * Check what type of frame came in.
1410 */
1411 header = (struct upgt_lmac_header *)(data->buf + 4);
1412
1413 h1_type = header->header1.type;
1414 h2_type = le16toh(header->header2.type);
1415
1416 if (h1_type == UPGT_H1_TYPE_CTRL && h2_type == UPGT_H2_TYPE_EEPROM) {
1417 eeprom = (struct upgt_lmac_eeprom *)(data->buf + 4);
1418 uint16_t eeprom_offset = le16toh(eeprom->offset);
1419 uint16_t eeprom_len = le16toh(eeprom->len);
1420
1421 DPRINTF(sc, UPGT_DEBUG_FW,
1422 "received EEPROM block (offset=%d, len=%d)\n",
1423 eeprom_offset, eeprom_len);
1424
1425 bcopy(data->buf + sizeof(struct upgt_lmac_eeprom) + 4,
1426 sc->sc_eeprom + eeprom_offset, eeprom_len);
1427
1428 /* EEPROM data has arrived in time, wakeup. */
1429 wakeup(sc);
1430 } else if (h1_type == UPGT_H1_TYPE_CTRL &&
1431 h2_type == UPGT_H2_TYPE_TX_DONE) {
1432 DPRINTF(sc, UPGT_DEBUG_XMIT, "%s: received 802.11 TX done\n",
1433 __func__);
1434 upgt_tx_done(sc, data->buf + 4);
1435 } else if (h1_type == UPGT_H1_TYPE_RX_DATA ||
1436 h1_type == UPGT_H1_TYPE_RX_DATA_MGMT) {
1437 DPRINTF(sc, UPGT_DEBUG_RECV, "%s: received 802.11 RX data\n",
1438 __func__);
1439 m = upgt_rx(sc, data->buf + 4, le16toh(header->header1.len),
1440 rssi);
1441 } else if (h1_type == UPGT_H1_TYPE_CTRL &&
1442 h2_type == UPGT_H2_TYPE_STATS) {
1443 DPRINTF(sc, UPGT_DEBUG_STAT, "%s: received statistic data\n",
1444 __func__);
1445 /* TODO: what could we do with the statistic data? */
1446 } else {
1447 /* ignore unknown frame types */
1448 DPRINTF(sc, UPGT_DEBUG_INTR,
1449 "received unknown frame type 0x%02x\n",
1450 header->header1.type);
1451 }
1452 return (m);
1453}
1454
1455/*
1456 * The firmware awaits a checksum for each frame we send to it.
1457 * The algorithm used therefor is uncommon but somehow similar to CRC32.
1458 */
1459static uint32_t
1460upgt_chksum_le(const uint32_t *buf, size_t size)
1461{
1462 int i;
1463 uint32_t crc = 0;
1464
1465 for (i = 0; i < size; i += sizeof(uint32_t)) {
1466 crc = htole32(crc ^ *buf++);
1467 crc = htole32((crc >> 5) ^ (crc << 3));
1468 }
1469
1470 return (crc);
1471}
1472
1473static struct mbuf *
1474upgt_rx(struct upgt_softc *sc, uint8_t *data, int pkglen, int *rssi)
1475{
1476 struct ifnet *ifp = sc->sc_ifp;
1477 struct ieee80211com *ic = ifp->if_l2com;
1478 struct upgt_lmac_rx_desc *rxdesc;
1479 struct mbuf *m;
1480
1481 /*
1482 * don't pass packets to the ieee80211 framework if the driver isn't
1483 * RUNNING.
1484 */
1485 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
1486 return (NULL);
1487
1488 /* access RX packet descriptor */
1489 rxdesc = (struct upgt_lmac_rx_desc *)data;
1490
1491 /* create mbuf which is suitable for strict alignment archs */
1492 KASSERT((pkglen + ETHER_ALIGN) < MCLBYTES,
1493 ("A current mbuf storage is small (%d)", pkglen + ETHER_ALIGN));
1494 m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
1495 if (m == NULL) {
1496 device_printf(sc->sc_dev, "could not create RX mbuf\n");
1497 return (NULL);
1498 }
1499 m_adj(m, ETHER_ALIGN);
1500 bcopy(rxdesc->data, mtod(m, char *), pkglen);
1501 /* trim FCS */
1502 m->m_len = m->m_pkthdr.len = pkglen - IEEE80211_CRC_LEN;
1503 m->m_pkthdr.rcvif = ifp;
1504
1505 if (ieee80211_radiotap_active(ic)) {
1506 struct upgt_rx_radiotap_header *tap = &sc->sc_rxtap;
1507
1508 tap->wr_flags = 0;
1509 tap->wr_rate = upgt_rx_rate(sc, rxdesc->rate);
1510 tap->wr_antsignal = rxdesc->rssi;
1511 }
1512 ifp->if_ipackets++;
1513
1514 DPRINTF(sc, UPGT_DEBUG_RX_PROC, "%s: RX done\n", __func__);
1515 *rssi = rxdesc->rssi;
1516 return (m);
1517}
1518
1519static uint8_t
1520upgt_rx_rate(struct upgt_softc *sc, const int rate)
1521{
1522 struct ifnet *ifp = sc->sc_ifp;
1523 struct ieee80211com *ic = ifp->if_l2com;
1524 static const uint8_t cck_upgt2rate[4] = { 2, 4, 11, 22 };
1525 static const uint8_t ofdm_upgt2rate[12] =
1526 { 2, 4, 11, 22, 12, 18, 24, 36, 48, 72, 96, 108 };
1527
1528 if (ic->ic_curmode == IEEE80211_MODE_11B &&
1529 !(rate < 0 || rate > 3))
1530 return cck_upgt2rate[rate & 0xf];
1531
1532 if (ic->ic_curmode == IEEE80211_MODE_11G &&
1533 !(rate < 0 || rate > 11))
1534 return ofdm_upgt2rate[rate & 0xf];
1535
1536 return (0);
1537}
1538
1539static void
1540upgt_tx_done(struct upgt_softc *sc, uint8_t *data)
1541{
1542 struct ifnet *ifp = sc->sc_ifp;
1543 struct upgt_lmac_tx_done_desc *desc;
1544 int i, freed = 0;
1545
1546 UPGT_ASSERT_LOCKED(sc);
1547
1548 desc = (struct upgt_lmac_tx_done_desc *)data;
1549
1550 for (i = 0; i < UPGT_TX_MAXCOUNT; i++) {
1551 struct upgt_data *data_tx = &sc->sc_tx_data[i];
1552
1553 if (data_tx->addr == le32toh(desc->header2.reqid)) {
1554 upgt_mem_free(sc, data_tx->addr);
1555 data_tx->ni = NULL;
1556 data_tx->addr = 0;
1557 data_tx->m = NULL;
1558 data_tx->use = 0;
1559
1560 DPRINTF(sc, UPGT_DEBUG_TX_PROC,
1561 "TX done: memaddr=0x%08x, status=0x%04x, rssi=%d, ",
1562 le32toh(desc->header2.reqid),
1563 le16toh(desc->status), le16toh(desc->rssi));
1564 DPRINTF(sc, UPGT_DEBUG_TX_PROC, "seq=%d\n",
1565 le16toh(desc->seq));
1566
1567 freed++;
1568 }
1569 }
1570
1571 if (freed != 0) {
1572 sc->sc_tx_timer = 0;
1573 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1574 UPGT_UNLOCK(sc);
1575 upgt_start(ifp);
1576 UPGT_LOCK(sc);
1577 }
1578}
1579
1580static void
1581upgt_mem_free(struct upgt_softc *sc, uint32_t addr)
1582{
1583 int i;
1584
1585 for (i = 0; i < sc->sc_memory.pages; i++) {
1586 if (sc->sc_memory.page[i].addr == addr) {
1587 sc->sc_memory.page[i].used = 0;
1588 return;
1589 }
1590 }
1591
1592 device_printf(sc->sc_dev,
1593 "could not free memory address 0x%08x\n", addr);
1594}
1595
1596static int
1597upgt_fw_load(struct upgt_softc *sc)
1598{
1599 const struct firmware *fw;
1600 struct upgt_data *data_cmd;
1601 struct upgt_fw_x2_header *x2;
1602 char start_fwload_cmd[] = { 0x3c, 0x0d };
1603 int error = 0, offset, bsize, n;
1604 uint32_t crc32;
1605
1606 fw = firmware_get(upgt_fwname);
1607 if (fw == NULL) {
1608 device_printf(sc->sc_dev, "could not read microcode %s\n",
1609 upgt_fwname);
1610 return (EIO);
1611 }
1612
1613 UPGT_LOCK(sc);
1614
1615 /* send firmware start load command */
1616 data_cmd = upgt_getbuf(sc);
1617 if (data_cmd == NULL) {
1618 error = ENOBUFS;
1619 goto fail;
1620 }
1621 data_cmd->buflen = sizeof(start_fwload_cmd);
1622 bcopy(start_fwload_cmd, data_cmd->buf, data_cmd->buflen);
1623 upgt_bulk_tx(sc, data_cmd);
1624
1625 /* send X2 header */
1626 data_cmd = upgt_getbuf(sc);
1627 if (data_cmd == NULL) {
1628 error = ENOBUFS;
1629 goto fail;
1630 }
1631 data_cmd->buflen = sizeof(struct upgt_fw_x2_header);
1632 x2 = (struct upgt_fw_x2_header *)data_cmd->buf;
1633 bcopy(UPGT_X2_SIGNATURE, x2->signature, UPGT_X2_SIGNATURE_SIZE);
1634 x2->startaddr = htole32(UPGT_MEMADDR_FIRMWARE_START);
1635 x2->len = htole32(fw->datasize);
1636 x2->crc = upgt_crc32_le((uint8_t *)data_cmd->buf +
1637 UPGT_X2_SIGNATURE_SIZE,
1638 sizeof(struct upgt_fw_x2_header) - UPGT_X2_SIGNATURE_SIZE -
1639 sizeof(uint32_t));
1640 upgt_bulk_tx(sc, data_cmd);
1641
1642 /* download firmware */
1643 for (offset = 0; offset < fw->datasize; offset += bsize) {
1644 if (fw->datasize - offset > UPGT_FW_BLOCK_SIZE)
1645 bsize = UPGT_FW_BLOCK_SIZE;
1646 else
1647 bsize = fw->datasize - offset;
1648
1649 data_cmd = upgt_getbuf(sc);
1650 if (data_cmd == NULL) {
1651 error = ENOBUFS;
1652 goto fail;
1653 }
1654 n = upgt_fw_copy((const uint8_t *)fw->data + offset,
1655 data_cmd->buf, bsize);
1656 data_cmd->buflen = bsize;
1657 upgt_bulk_tx(sc, data_cmd);
1658
1659 DPRINTF(sc, UPGT_DEBUG_FW, "FW offset=%d, read=%d, sent=%d\n",
1660 offset, n, bsize);
1661 bsize = n;
1662 }
1663 DPRINTF(sc, UPGT_DEBUG_FW, "%s: firmware downloaded\n", __func__);
1664
1665 /* load firmware */
1666 data_cmd = upgt_getbuf(sc);
1667 if (data_cmd == NULL) {
1668 error = ENOBUFS;
1669 goto fail;
1670 }
1671 crc32 = upgt_crc32_le(fw->data, fw->datasize);
1672 *((uint32_t *)(data_cmd->buf) ) = crc32;
1673 *((uint8_t *)(data_cmd->buf) + 4) = 'g';
1674 *((uint8_t *)(data_cmd->buf) + 5) = '\r';
1675 data_cmd->buflen = 6;
1676 upgt_bulk_tx(sc, data_cmd);
1677
1678 /* waiting 'OK' response. */
1679 usbd_transfer_start(sc->sc_xfer[UPGT_BULK_RX]);
1680 error = mtx_sleep(sc, &sc->sc_mtx, 0, "upgtfw", 2 * hz);
1681 if (error != 0) {
1682 device_printf(sc->sc_dev, "firmware load failed\n");
1683 error = EIO;
1684 }
1685
1686 DPRINTF(sc, UPGT_DEBUG_FW, "%s: firmware loaded\n", __func__);
1687fail:
1688 UPGT_UNLOCK(sc);
1689 firmware_put(fw, FIRMWARE_UNLOAD);
1690 return (error);
1691}
1692
1693static uint32_t
1694upgt_crc32_le(const void *buf, size_t size)
1695{
1696 uint32_t crc;
1697
1698 crc = ether_crc32_le(buf, size);
1699
1700 /* apply final XOR value as common for CRC-32 */
1701 crc = htole32(crc ^ 0xffffffffU);
1702
1703 return (crc);
1704}
1705
1706/*
1707 * While copying the version 2 firmware, we need to replace two characters:
1708 *
1709 * 0x7e -> 0x7d 0x5e
1710 * 0x7d -> 0x7d 0x5d
1711 */
1712static int
1713upgt_fw_copy(const uint8_t *src, char *dst, int size)
1714{
1715 int i, j;
1716
1717 for (i = 0, j = 0; i < size && j < size; i++) {
1718 switch (src[i]) {
1719 case 0x7e:
1720 dst[j] = 0x7d;
1721 j++;
1722 dst[j] = 0x5e;
1723 j++;
1724 break;
1725 case 0x7d:
1726 dst[j] = 0x7d;
1727 j++;
1728 dst[j] = 0x5d;
1729 j++;
1730 break;
1731 default:
1732 dst[j] = src[i];
1733 j++;
1734 break;
1735 }
1736 }
1737
1738 return (i);
1739}
1740
1741static int
1742upgt_mem_init(struct upgt_softc *sc)
1743{
1744 int i;
1745
1746 for (i = 0; i < UPGT_MEMORY_MAX_PAGES; i++) {
1747 sc->sc_memory.page[i].used = 0;
1748
1749 if (i == 0) {
1750 /*
1751 * The first memory page is always reserved for
1752 * command data.
1753 */
1754 sc->sc_memory.page[i].addr =
1755 sc->sc_memaddr_frame_start + MCLBYTES;
1756 } else {
1757 sc->sc_memory.page[i].addr =
1758 sc->sc_memory.page[i - 1].addr + MCLBYTES;
1759 }
1760
1761 if (sc->sc_memory.page[i].addr + MCLBYTES >=
1762 sc->sc_memaddr_frame_end)
1763 break;
1764
1765 DPRINTF(sc, UPGT_DEBUG_FW, "memory address page %d=0x%08x\n",
1766 i, sc->sc_memory.page[i].addr);
1767 }
1768
1769 sc->sc_memory.pages = i;
1770
1771 DPRINTF(sc, UPGT_DEBUG_FW, "memory pages=%d\n", sc->sc_memory.pages);
1772 return (0);
1773}
1774
1775static int
1776upgt_fw_verify(struct upgt_softc *sc)
1777{
1778 const struct firmware *fw;
1779 const struct upgt_fw_bra_option *bra_opt;
1780 const struct upgt_fw_bra_descr *descr;
1781 const uint8_t *p;
1782 const uint32_t *uc;
1783 uint32_t bra_option_type, bra_option_len;
1784 int offset, bra_end = 0, error = 0;
1785
1786 fw = firmware_get(upgt_fwname);
1787 if (fw == NULL) {
1788 device_printf(sc->sc_dev, "could not read microcode %s\n",
1789 upgt_fwname);
1790 return EIO;
1791 }
1792
1793 /*
1794 * Seek to beginning of Boot Record Area (BRA).
1795 */
1796 for (offset = 0; offset < fw->datasize; offset += sizeof(*uc)) {
1797 uc = (const uint32_t *)((const uint8_t *)fw->data + offset);
1798 if (*uc == 0)
1799 break;
1800 }
1801 for (; offset < fw->datasize; offset += sizeof(*uc)) {
1802 uc = (const uint32_t *)((const uint8_t *)fw->data + offset);
1803 if (*uc != 0)
1804 break;
1805 }
1806 if (offset == fw->datasize) {
1807 device_printf(sc->sc_dev,
1808 "firmware Boot Record Area not found\n");
1809 error = EIO;
1810 goto fail;
1811 }
1812
1813 DPRINTF(sc, UPGT_DEBUG_FW,
1814 "firmware Boot Record Area found at offset %d\n", offset);
1815
1816 /*
1817 * Parse Boot Record Area (BRA) options.
1818 */
1819 while (offset < fw->datasize && bra_end == 0) {
1820 /* get current BRA option */
1821 p = (const uint8_t *)fw->data + offset;
1822 bra_opt = (const struct upgt_fw_bra_option *)p;
1823 bra_option_type = le32toh(bra_opt->type);
1824 bra_option_len = le32toh(bra_opt->len) * sizeof(*uc);
1825
1826 switch (bra_option_type) {
1827 case UPGT_BRA_TYPE_FW:
1828 DPRINTF(sc, UPGT_DEBUG_FW, "UPGT_BRA_TYPE_FW len=%d\n",
1829 bra_option_len);
1830
1831 if (bra_option_len != UPGT_BRA_FWTYPE_SIZE) {
1832 device_printf(sc->sc_dev,
1833 "wrong UPGT_BRA_TYPE_FW len\n");
1834 error = EIO;
1835 goto fail;
1836 }
1837 if (memcmp(UPGT_BRA_FWTYPE_LM86, bra_opt->data,
1838 bra_option_len) == 0) {
1839 sc->sc_fw_type = UPGT_FWTYPE_LM86;
1840 break;
1841 }
1842 if (memcmp(UPGT_BRA_FWTYPE_LM87, bra_opt->data,
1843 bra_option_len) == 0) {
1844 sc->sc_fw_type = UPGT_FWTYPE_LM87;
1845 break;
1846 }
1847 device_printf(sc->sc_dev,
1848 "unsupported firmware type\n");
1849 error = EIO;
1850 goto fail;
1851 case UPGT_BRA_TYPE_VERSION:
1852 DPRINTF(sc, UPGT_DEBUG_FW,
1853 "UPGT_BRA_TYPE_VERSION len=%d\n", bra_option_len);
1854 break;
1855 case UPGT_BRA_TYPE_DEPIF:
1856 DPRINTF(sc, UPGT_DEBUG_FW,
1857 "UPGT_BRA_TYPE_DEPIF len=%d\n", bra_option_len);
1858 break;
1859 case UPGT_BRA_TYPE_EXPIF:
1860 DPRINTF(sc, UPGT_DEBUG_FW,
1861 "UPGT_BRA_TYPE_EXPIF len=%d\n", bra_option_len);
1862 break;
1863 case UPGT_BRA_TYPE_DESCR:
1864 DPRINTF(sc, UPGT_DEBUG_FW,
1865 "UPGT_BRA_TYPE_DESCR len=%d\n", bra_option_len);
1866
1867 descr = (const struct upgt_fw_bra_descr *)bra_opt->data;
1868
1869 sc->sc_memaddr_frame_start =
1870 le32toh(descr->memaddr_space_start);
1871 sc->sc_memaddr_frame_end =
1872 le32toh(descr->memaddr_space_end);
1873
1874 DPRINTF(sc, UPGT_DEBUG_FW,
1875 "memory address space start=0x%08x\n",
1876 sc->sc_memaddr_frame_start);
1877 DPRINTF(sc, UPGT_DEBUG_FW,
1878 "memory address space end=0x%08x\n",
1879 sc->sc_memaddr_frame_end);
1880 break;
1881 case UPGT_BRA_TYPE_END:
1882 DPRINTF(sc, UPGT_DEBUG_FW, "UPGT_BRA_TYPE_END len=%d\n",
1883 bra_option_len);
1884 bra_end = 1;
1885 break;
1886 default:
1887 DPRINTF(sc, UPGT_DEBUG_FW, "unknown BRA option len=%d\n",
1888 bra_option_len);
1889 error = EIO;
1890 goto fail;
1891 }
1892
1893 /* jump to next BRA option */
1894 offset += sizeof(struct upgt_fw_bra_option) + bra_option_len;
1895 }
1896
1897 DPRINTF(sc, UPGT_DEBUG_FW, "%s: firmware verified", __func__);
1898fail:
1899 firmware_put(fw, FIRMWARE_UNLOAD);
1900 return (error);
1901}
1902
1903static void
1904upgt_bulk_tx(struct upgt_softc *sc, struct upgt_data *data)
1905{
1906
1907 UPGT_ASSERT_LOCKED(sc);
1908
1909 STAILQ_INSERT_TAIL(&sc->sc_tx_pending, data, next);
1910 UPGT_STAT_INC(sc, st_tx_pending);
1911 usbd_transfer_start(sc->sc_xfer[UPGT_BULK_TX]);
1912}
1913
1914static int
1915upgt_device_reset(struct upgt_softc *sc)
1916{
1917 struct upgt_data *data;
1918 char init_cmd[] = { 0x7e, 0x7e, 0x7e, 0x7e };
1919
1920 UPGT_LOCK(sc);
1921
1922 data = upgt_getbuf(sc);
1923 if (data == NULL) {
1924 UPGT_UNLOCK(sc);
1925 return (ENOBUFS);
1926 }
1927 bcopy(init_cmd, data->buf, sizeof(init_cmd));
1928 data->buflen = sizeof(init_cmd);
1929 upgt_bulk_tx(sc, data);
1930 usb_pause_mtx(&sc->sc_mtx, 100);
1931
1932 UPGT_UNLOCK(sc);
1933 DPRINTF(sc, UPGT_DEBUG_FW, "%s: device initialized\n", __func__);
1934 return (0);
1935}
1936
1937static int
1938upgt_alloc_tx(struct upgt_softc *sc)
1939{
1940 int i;
1941
1942 STAILQ_INIT(&sc->sc_tx_active);
1943 STAILQ_INIT(&sc->sc_tx_inactive);
1944 STAILQ_INIT(&sc->sc_tx_pending);
1945
1946 for (i = 0; i < UPGT_TX_MAXCOUNT; i++) {
1947 struct upgt_data *data = &sc->sc_tx_data[i];
1948
1949 data->buf = malloc(MCLBYTES, M_USBDEV, M_NOWAIT | M_ZERO);
1950 if (data->buf == NULL) {
1951 device_printf(sc->sc_dev,
1952 "could not allocate TX buffer\n");
1953 return (ENOMEM);
1954 }
1955 STAILQ_INSERT_TAIL(&sc->sc_tx_inactive, data, next);
1956 UPGT_STAT_INC(sc, st_tx_inactive);
1957 }
1958
1959 return (0);
1960}
1961
1962static int
1963upgt_alloc_rx(struct upgt_softc *sc)
1964{
1965 int i;
1966
1967 STAILQ_INIT(&sc->sc_rx_active);
1968 STAILQ_INIT(&sc->sc_rx_inactive);
1969
1970 for (i = 0; i < UPGT_RX_MAXCOUNT; i++) {
1971 struct upgt_data *data = &sc->sc_rx_data[i];
1972
1973 data->buf = malloc(MCLBYTES, M_USBDEV, M_NOWAIT | M_ZERO);
1974 if (data->buf == NULL) {
1975 device_printf(sc->sc_dev,
1976 "could not allocate RX buffer\n");
1977 return (ENOMEM);
1978 }
1979 STAILQ_INSERT_TAIL(&sc->sc_rx_inactive, data, next);
1980 }
1981
1982 return (0);
1983}
1984
1985static int
1986upgt_detach(device_t dev)
1987{
1988 struct upgt_softc *sc = device_get_softc(dev);
1989 struct ifnet *ifp = sc->sc_ifp;
1990 struct ieee80211com *ic = ifp->if_l2com;
1991
1992 if (!device_is_attached(dev))
1993 return 0;
1994
1995 upgt_stop(sc);
1996
1997 callout_drain(&sc->sc_led_ch);
1998 callout_drain(&sc->sc_watchdog_ch);
1999
2000 usbd_transfer_unsetup(sc->sc_xfer, UPGT_N_XFERS);
2001 ieee80211_ifdetach(ic);
2002 upgt_free_rx(sc);
2003 upgt_free_tx(sc);
2004
2005 if_free(ifp);
2006 mtx_destroy(&sc->sc_mtx);
2007
2008 return (0);
2009}
2010
2011static void
2012upgt_free_rx(struct upgt_softc *sc)
2013{
2014 int i;
2015
2016 for (i = 0; i < UPGT_RX_MAXCOUNT; i++) {
2017 struct upgt_data *data = &sc->sc_rx_data[i];
2018
2019 free(data->buf, M_USBDEV);
2020 data->ni = NULL;
2021 }
2022}
2023
2024static void
2025upgt_free_tx(struct upgt_softc *sc)
2026{
2027 int i;
2028
2029 for (i = 0; i < UPGT_TX_MAXCOUNT; i++) {
2030 struct upgt_data *data = &sc->sc_tx_data[i];
2031
2032 free(data->buf, M_USBDEV);
2033 data->ni = NULL;
2034 }
2035}
2036
2037static void
2038upgt_abort_xfers_locked(struct upgt_softc *sc)
2039{
2040 int i;
2041
2042 UPGT_ASSERT_LOCKED(sc);
2043 /* abort any pending transfers */
2044 for (i = 0; i < UPGT_N_XFERS; i++)
2045 usbd_transfer_stop(sc->sc_xfer[i]);
2046}
2047
2048static void
2049upgt_abort_xfers(struct upgt_softc *sc)
2050{
2051
2052 UPGT_LOCK(sc);
2053 upgt_abort_xfers_locked(sc);
2054 UPGT_UNLOCK(sc);
2055}
2056
2057#define UPGT_SYSCTL_STAT_ADD32(c, h, n, p, d) \
2058 SYSCTL_ADD_UINT(c, h, OID_AUTO, n, CTLFLAG_RD, p, 0, d)
2059
2060static void
2061upgt_sysctl_node(struct upgt_softc *sc)
2062{
2063 struct sysctl_ctx_list *ctx;
2064 struct sysctl_oid_list *child;
2065 struct sysctl_oid *tree;
2066 struct upgt_stat *stats;
2067
2068 stats = &sc->sc_stat;
2069 ctx = device_get_sysctl_ctx(sc->sc_dev);
2070 child = SYSCTL_CHILDREN(device_get_sysctl_tree(sc->sc_dev));
2071
2072 tree = SYSCTL_ADD_NODE(ctx, child, OID_AUTO, "stats", CTLFLAG_RD,
2073 NULL, "UPGT statistics");
2074 child = SYSCTL_CHILDREN(tree);
2075 UPGT_SYSCTL_STAT_ADD32(ctx, child, "tx_active",
2076 &stats->st_tx_active, "Active numbers in TX queue");
2077 UPGT_SYSCTL_STAT_ADD32(ctx, child, "tx_inactive",
2078 &stats->st_tx_inactive, "Inactive numbers in TX queue");
2079 UPGT_SYSCTL_STAT_ADD32(ctx, child, "tx_pending",
2080 &stats->st_tx_pending, "Pending numbers in TX queue");
2081}
2082
2083#undef UPGT_SYSCTL_STAT_ADD32
2084
2085static struct upgt_data *
2086_upgt_getbuf(struct upgt_softc *sc)
2087{
2088 struct upgt_data *bf;
2089
2090 bf = STAILQ_FIRST(&sc->sc_tx_inactive);
2091 if (bf != NULL) {
2092 STAILQ_REMOVE_HEAD(&sc->sc_tx_inactive, next);
2093 UPGT_STAT_DEC(sc, st_tx_inactive);
2094 } else
2095 bf = NULL;
2096 if (bf == NULL)
2097 DPRINTF(sc, UPGT_DEBUG_XMIT, "%s: %s\n", __func__,
2098 "out of xmit buffers");
2099 return (bf);
2100}
2101
2102static struct upgt_data *
2103upgt_getbuf(struct upgt_softc *sc)
2104{
2105 struct upgt_data *bf;
2106
2107 UPGT_ASSERT_LOCKED(sc);
2108
2109 bf = _upgt_getbuf(sc);
2110 if (bf == NULL) {
2111 struct ifnet *ifp = sc->sc_ifp;
2112
2113 DPRINTF(sc, UPGT_DEBUG_XMIT, "%s: stop queue\n", __func__);
2114 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
2115 }
2116
2117 return (bf);
2118}
2119
2120static struct upgt_data *
2121upgt_gettxbuf(struct upgt_softc *sc)
2122{
2123 struct upgt_data *bf;
2124
2125 UPGT_ASSERT_LOCKED(sc);
2126
2127 bf = upgt_getbuf(sc);
2128 if (bf == NULL)
2129 return (NULL);
2130
2131 bf->addr = upgt_mem_alloc(sc);
2132 if (bf->addr == 0) {
2133 struct ifnet *ifp = sc->sc_ifp;
2134
2135 DPRINTF(sc, UPGT_DEBUG_XMIT, "%s: no free prism memory!\n",
2136 __func__);
2137 STAILQ_INSERT_HEAD(&sc->sc_tx_inactive, bf, next);
2138 UPGT_STAT_INC(sc, st_tx_inactive);
2139 if (!(ifp->if_drv_flags & IFF_DRV_OACTIVE))
2140 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
2141 return (NULL);
2142 }
2143 return (bf);
2144}
2145
2146static int
2147upgt_tx_start(struct upgt_softc *sc, struct mbuf *m, struct ieee80211_node *ni,
2148 struct upgt_data *data)
2149{
2150 struct ieee80211vap *vap = ni->ni_vap;
2151 int error = 0, len;
2152 struct ieee80211_frame *wh;
2153 struct ieee80211_key *k;
2154 struct ifnet *ifp = sc->sc_ifp;
2155 struct upgt_lmac_mem *mem;
2156 struct upgt_lmac_tx_desc *txdesc;
2157
2158 UPGT_ASSERT_LOCKED(sc);
2159
2160 upgt_set_led(sc, UPGT_LED_BLINK);
2161
2162 /*
2163 * Software crypto.
2164 */
2165 wh = mtod(m, struct ieee80211_frame *);
2166 if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
2167 k = ieee80211_crypto_encap(ni, m);
2168 if (k == NULL) {
2169 device_printf(sc->sc_dev,
2170 "ieee80211_crypto_encap returns NULL.\n");
2171 error = EIO;
2172 goto done;
2173 }
2174
2175 /* in case packet header moved, reset pointer */
2176 wh = mtod(m, struct ieee80211_frame *);
2177 }
2178
2179 /* Transmit the URB containing the TX data. */
2180 bzero(data->buf, MCLBYTES);
2181 mem = (struct upgt_lmac_mem *)data->buf;
2182 mem->addr = htole32(data->addr);
2183 txdesc = (struct upgt_lmac_tx_desc *)(mem + 1);
2184
2185 if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) ==
2186 IEEE80211_FC0_TYPE_MGT) {
2187 /* mgmt frames */
2188 txdesc->header1.flags = UPGT_H1_FLAGS_TX_MGMT;
2189 /* always send mgmt frames at lowest rate (DS1) */
2190 memset(txdesc->rates, 0x10, sizeof(txdesc->rates));
2191 } else {
2192 /* data frames */
2193 txdesc->header1.flags = UPGT_H1_FLAGS_TX_DATA;
2194 bcopy(sc->sc_cur_rateset, txdesc->rates, sizeof(txdesc->rates));
2195 }
2196 txdesc->header1.type = UPGT_H1_TYPE_TX_DATA;
2197 txdesc->header1.len = htole16(m->m_pkthdr.len);
2198 txdesc->header2.reqid = htole32(data->addr);
2199 txdesc->header2.type = htole16(UPGT_H2_TYPE_TX_ACK_YES);
2200 txdesc->header2.flags = htole16(UPGT_H2_FLAGS_TX_ACK_YES);
2201 txdesc->type = htole32(UPGT_TX_DESC_TYPE_DATA);
2202 txdesc->pad3[0] = UPGT_TX_DESC_PAD3_SIZE;
2203
2204 if (ieee80211_radiotap_active_vap(vap)) {
2205 struct upgt_tx_radiotap_header *tap = &sc->sc_txtap;
2206
2207 tap->wt_flags = 0;
2208 tap->wt_rate = 0; /* XXX where to get from? */
2209
2210 ieee80211_radiotap_tx(vap, m);
2211 }
2212
2213 /* copy frame below our TX descriptor header */
2214 m_copydata(m, 0, m->m_pkthdr.len,
2215 data->buf + (sizeof(*mem) + sizeof(*txdesc)));
2216 /* calculate frame size */
2217 len = sizeof(*mem) + sizeof(*txdesc) + m->m_pkthdr.len;
2218 /* we need to align the frame to a 4 byte boundary */
2219 len = (len + 3) & ~3;
2220 /* calculate frame checksum */
2221 mem->chksum = upgt_chksum_le((uint32_t *)txdesc, len - sizeof(*mem));
2222 data->ni = ni;
2223 data->m = m;
2224 data->buflen = len;
2225
2226 DPRINTF(sc, UPGT_DEBUG_XMIT, "%s: TX start data sending (%d bytes)\n",
2227 __func__, len);
2228 KASSERT(len <= MCLBYTES, ("mbuf is small for saving data"));
2229
2230 upgt_bulk_tx(sc, data);
2231done:
2232 /*
2233 * If we don't regulary read the device statistics, the RX queue
2234 * will stall. It's strange, but it works, so we keep reading
2235 * the statistics here. *shrug*
2236 */
2237 if (!(ifp->if_opackets % UPGT_TX_STAT_INTERVAL))
2238 upgt_get_stats(sc);
2239
2240 return (error);
2241}
2242
2243static void
2244upgt_bulk_rx_callback(struct usb_xfer *xfer, usb_error_t error)
2245{
2246 struct upgt_softc *sc = usbd_xfer_softc(xfer);
2247 struct ifnet *ifp = sc->sc_ifp;
2248 struct ieee80211com *ic = ifp->if_l2com;
2249 struct ieee80211_frame *wh;
2250 struct ieee80211_node *ni;
2251 struct mbuf *m = NULL;
2252 struct upgt_data *data;
2253 int8_t nf;
2254 int rssi = -1;
2255
2256 UPGT_ASSERT_LOCKED(sc);
2257
2258 switch (USB_GET_STATE(xfer)) {
2259 case USB_ST_TRANSFERRED:
2260 data = STAILQ_FIRST(&sc->sc_rx_active);
2261 if (data == NULL)
2262 goto setup;
2263 STAILQ_REMOVE_HEAD(&sc->sc_rx_active, next);
2264 m = upgt_rxeof(xfer, data, &rssi);
2265 STAILQ_INSERT_TAIL(&sc->sc_rx_inactive, data, next);
2266 /* FALLTHROUGH */
2267 case USB_ST_SETUP:
2268setup:
2269 data = STAILQ_FIRST(&sc->sc_rx_inactive);
2270 if (data == NULL)
2271 return;
2272 STAILQ_REMOVE_HEAD(&sc->sc_rx_inactive, next);
2273 STAILQ_INSERT_TAIL(&sc->sc_rx_active, data, next);
2274 usbd_xfer_set_frame_data(xfer, 0, data->buf,
2275 usbd_xfer_max_len(xfer));
2276 usbd_transfer_submit(xfer);
2277
2278 /*
2279 * To avoid LOR we should unlock our private mutex here to call
2280 * ieee80211_input() because here is at the end of a USB
2281 * callback and safe to unlock.
2282 */
2283 UPGT_UNLOCK(sc);
2284 if (m != NULL) {
2285 wh = mtod(m, struct ieee80211_frame *);
2286 ni = ieee80211_find_rxnode(ic,
2287 (struct ieee80211_frame_min *)wh);
2288 nf = -95; /* XXX */
2289 if (ni != NULL) {
2290 (void) ieee80211_input(ni, m, rssi, nf);
2291 /* node is no longer needed */
2292 ieee80211_free_node(ni);
2293 } else
2294 (void) ieee80211_input_all(ic, m, rssi, nf);
2295 m = NULL;
2296 }
2297 if ((ifp->if_drv_flags & IFF_DRV_OACTIVE) == 0 &&
2298 !IFQ_IS_EMPTY(&ifp->if_snd))
2299 upgt_start(ifp);
2300 UPGT_LOCK(sc);
2301 break;
2302 default:
2303 /* needs it to the inactive queue due to a error. */
2304 data = STAILQ_FIRST(&sc->sc_rx_active);
2305 if (data != NULL) {
2306 STAILQ_REMOVE_HEAD(&sc->sc_rx_active, next);
2307 STAILQ_INSERT_TAIL(&sc->sc_rx_inactive, data, next);
2308 }
2309 if (error != USB_ERR_CANCELLED) {
2310 usbd_xfer_set_stall(xfer);
2311 ifp->if_ierrors++;
2312 goto setup;
2313 }
2314 break;
2315 }
2316}
2317
2318static void
2319upgt_bulk_tx_callback(struct usb_xfer *xfer, usb_error_t error)
2320{
2321 struct upgt_softc *sc = usbd_xfer_softc(xfer);
2322 struct ifnet *ifp = sc->sc_ifp;
2323 struct upgt_data *data;
2324
2325 UPGT_ASSERT_LOCKED(sc);
2326 switch (USB_GET_STATE(xfer)) {
2327 case USB_ST_TRANSFERRED:
2328 data = STAILQ_FIRST(&sc->sc_tx_active);
2329 if (data == NULL)
2330 goto setup;
2331 STAILQ_REMOVE_HEAD(&sc->sc_tx_active, next);
2332 UPGT_STAT_DEC(sc, st_tx_active);
2333 upgt_txeof(xfer, data);
2334 STAILQ_INSERT_TAIL(&sc->sc_tx_inactive, data, next);
2335 UPGT_STAT_INC(sc, st_tx_inactive);
2336 /* FALLTHROUGH */
2337 case USB_ST_SETUP:
2338setup:
2339 data = STAILQ_FIRST(&sc->sc_tx_pending);
2340 if (data == NULL) {
2341 DPRINTF(sc, UPGT_DEBUG_XMIT, "%s: empty pending queue\n",
2342 __func__);
2343 return;
2344 }
2345 STAILQ_REMOVE_HEAD(&sc->sc_tx_pending, next);
2346 UPGT_STAT_DEC(sc, st_tx_pending);
2347 STAILQ_INSERT_TAIL(&sc->sc_tx_active, data, next);
2348 UPGT_STAT_INC(sc, st_tx_active);
2349
2350 usbd_xfer_set_frame_data(xfer, 0, data->buf, data->buflen);
2351 usbd_transfer_submit(xfer);
2352 UPGT_UNLOCK(sc);
2353 upgt_start(ifp);
2354 UPGT_LOCK(sc);
2355 break;
2356 default:
2357 data = STAILQ_FIRST(&sc->sc_tx_active);
2358 if (data == NULL)
2359 goto setup;
2360 if (data->ni != NULL) {
2361 ieee80211_free_node(data->ni);
2362 data->ni = NULL;
2363 ifp->if_oerrors++;
2364 }
2365 if (error != USB_ERR_CANCELLED) {
2366 usbd_xfer_set_stall(xfer);
2367 goto setup;
2368 }
2369 break;
2370 }
2371}
2372
2373static device_method_t upgt_methods[] = {
2374 /* Device interface */
2375 DEVMETHOD(device_probe, upgt_match),
2376 DEVMETHOD(device_attach, upgt_attach),
2377 DEVMETHOD(device_detach, upgt_detach),
2378
2379 { 0, 0 }
2380};
2381
2382static driver_t upgt_driver = {
2383 "upgt",
2384 upgt_methods,
2385 sizeof(struct upgt_softc)
2386};
2387
2388static devclass_t upgt_devclass;
2389
2390DRIVER_MODULE(if_upgt, uhub, upgt_driver, upgt_devclass, NULL, 0);
2391MODULE_VERSION(if_upgt, 1);
2392MODULE_DEPEND(if_upgt, usb, 1, 1, 1);
2393MODULE_DEPEND(if_upgt, wlan, 1, 1, 1);
2394MODULE_DEPEND(if_upgt, upgtfw_fw, 1, 1, 1);