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