FreeBSD/Linux Kernel Cross Reference
sys/dev/usb/if_upgt.c
1 /* $OpenBSD: if_upgt.c,v 1.35 2008/04/16 18:32:15 damien Exp $ */
2 /* $FreeBSD: src/sys/dev/usb/if_upgt.c,v 1.2 2008/12/01 10:04:39 kevlo Exp $ */
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 <dev/usb/usbdi_util.h>
53 #include <dev/usb/usbdivar.h>
54 #include "usbdevs.h"
55 #include <dev/usb/usb_ethersubr.h>
56
57 #include <dev/usb/if_upgtvar.h>
58
59 /*
60 * Driver for the USB PrismGT devices.
61 *
62 * For now just USB 2.0 devices with the GW3887 chipset are supported.
63 * The driver has been written based on the firmware version 2.13.1.0_LM87.
64 *
65 * TODO's:
66 * - MONITOR mode test.
67 * - Add HOSTAP mode.
68 * - Add IBSS mode.
69 * - Support the USB 1.0 devices (NET2280, ISL3880, ISL3886 chipsets).
70 *
71 * Parts of this driver has been influenced by reading the p54u driver
72 * written by Jean-Baptiste Note <jean-baptiste.note@m4x.org> and
73 * Sebastien Bourdeauducq <lekernel@prism54.org>.
74 */
75
76 SYSCTL_NODE(_hw, OID_AUTO, upgt, CTLFLAG_RD, 0,
77 "USB PrismGT GW3887 driver parameters");
78
79 /*
80 * NB: normally `upgt_txbuf' value can be increased to maximum 6, mininum 1.
81 * However, we're using just 2 txbufs to protect packet losses in some cases
82 * so the performance was sacrificed that with this value its speed is about
83 * 2.1Mb/s.
84 *
85 * With setting txbuf value as 6, you can get full speed, 3.0Mb/s, of this
86 * device but sometimes you'd meet some packet losses then retransmision.
87 */
88 static int upgt_txbuf = UPGT_TX_COUNT; /* # tx buffers to allocate */
89 SYSCTL_INT(_hw_upgt, OID_AUTO, txbuf, CTLFLAG_RW, &upgt_txbuf,
90 0, "tx buffers allocated");
91 TUNABLE_INT("hw.upgt.txbuf", &upgt_txbuf);
92
93 #ifdef UPGT_DEBUG
94 int upgt_debug = 0;
95 SYSCTL_INT(_hw_upgt, OID_AUTO, debug, CTLFLAG_RW, &upgt_debug,
96 0, "control debugging printfs");
97 TUNABLE_INT("hw.upgt.debug", &upgt_debug);
98 enum {
99 UPGT_DEBUG_XMIT = 0x00000001, /* basic xmit operation */
100 UPGT_DEBUG_RECV = 0x00000002, /* basic recv operation */
101 UPGT_DEBUG_RESET = 0x00000004, /* reset processing */
102 UPGT_DEBUG_INTR = 0x00000008, /* INTR */
103 UPGT_DEBUG_TX_PROC = 0x00000010, /* tx ISR proc */
104 UPGT_DEBUG_RX_PROC = 0x00000020, /* rx ISR proc */
105 UPGT_DEBUG_STATE = 0x00000040, /* 802.11 state transitions */
106 UPGT_DEBUG_STAT = 0x00000080, /* statistic */
107 UPGT_DEBUG_FW = 0x00000100, /* firmware */
108 UPGT_DEBUG_ANY = 0xffffffff
109 };
110 #define DPRINTF(sc, m, fmt, ...) do { \
111 if (sc->sc_debug & (m)) \
112 printf(fmt, __VA_ARGS__); \
113 } while (0)
114 #else
115 #define DPRINTF(sc, m, fmt, ...) do { \
116 (void) sc; \
117 } while (0)
118 #endif
119
120 /*
121 * Prototypes.
122 */
123 static device_probe_t upgt_match;
124 static device_attach_t upgt_attach;
125 static device_detach_t upgt_detach;
126 static int upgt_alloc_tx(struct upgt_softc *);
127 static int upgt_alloc_rx(struct upgt_softc *);
128 static int upgt_alloc_cmd(struct upgt_softc *);
129 static int upgt_attach_hook(device_t);
130 static int upgt_device_reset(struct upgt_softc *);
131 static int upgt_bulk_xmit(struct upgt_softc *, struct upgt_data *,
132 usbd_pipe_handle, uint32_t *, int);
133 static int upgt_fw_verify(struct upgt_softc *);
134 static int upgt_mem_init(struct upgt_softc *);
135 static int upgt_fw_load(struct upgt_softc *);
136 static int upgt_fw_copy(const uint8_t *, char *, int);
137 static uint32_t upgt_crc32_le(const void *, size_t);
138 static void upgt_rxeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
139 static void upgt_txeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
140 static int upgt_eeprom_read(struct upgt_softc *);
141 static int upgt_eeprom_parse(struct upgt_softc *);
142 static void upgt_eeprom_parse_hwrx(struct upgt_softc *, uint8_t *);
143 static void upgt_eeprom_parse_freq3(struct upgt_softc *, uint8_t *, int);
144 static void upgt_eeprom_parse_freq4(struct upgt_softc *, uint8_t *, int);
145 static void upgt_eeprom_parse_freq6(struct upgt_softc *, uint8_t *, int);
146 static uint32_t upgt_chksum_le(const uint32_t *, size_t);
147 static void upgt_tx_done(struct upgt_softc *, uint8_t *);
148 static void upgt_rx(struct upgt_softc *, uint8_t *, int);
149 static void upgt_init(void *);
150 static void upgt_init_locked(struct upgt_softc *);
151 static int upgt_ioctl(struct ifnet *, u_long, caddr_t);
152 static void upgt_start(struct ifnet *);
153 static int upgt_raw_xmit(struct ieee80211_node *, struct mbuf *,
154 const struct ieee80211_bpf_params *);
155 static void upgt_scan_start(struct ieee80211com *);
156 static void upgt_scan_end(struct ieee80211com *);
157 static void upgt_set_channel(struct ieee80211com *);
158 static struct ieee80211vap *upgt_vap_create(struct ieee80211com *,
159 const char name[IFNAMSIZ], int unit, int opmode,
160 int flags, const uint8_t bssid[IEEE80211_ADDR_LEN],
161 const uint8_t mac[IEEE80211_ADDR_LEN]);
162 static void upgt_vap_delete(struct ieee80211vap *);
163 static void upgt_update_mcast(struct ifnet *);
164 static uint8_t upgt_rx_rate(struct upgt_softc *, const int);
165 static void upgt_set_multi(void *);
166 static void upgt_stop(struct upgt_softc *, int);
167 static void upgt_setup_rates(struct ieee80211vap *, struct ieee80211com *);
168 static int upgt_set_macfilter(struct upgt_softc *, uint8_t);
169 static int upgt_newstate(struct ieee80211vap *, enum ieee80211_state, int);
170 static void upgt_task(void *);
171 static void upgt_scantask(void *);
172 static void upgt_set_chan(struct upgt_softc *, struct ieee80211_channel *);
173 static void upgt_set_led(struct upgt_softc *, int);
174 static void upgt_set_led_blink(void *);
175 static void upgt_tx_task(void *);
176 static int upgt_get_stats(struct upgt_softc *);
177 static void upgt_mem_free(struct upgt_softc *, uint32_t);
178 static uint32_t upgt_mem_alloc(struct upgt_softc *);
179 static void upgt_free_tx(struct upgt_softc *);
180 static void upgt_free_rx(struct upgt_softc *);
181 static void upgt_free_cmd(struct upgt_softc *);
182 static void upgt_watchdog(void *);
183
184 static const char *upgt_fwname = "upgt-gw3887";
185
186 static const struct usb_devno upgt_devs_2[] = {
187 /* version 2 devices */
188 { USB_VENDOR_ACCTON, USB_PRODUCT_ACCTON_PRISM_GT },
189 { USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5D7050 },
190 { USB_VENDOR_CONCEPTRONIC, USB_PRODUCT_CONCEPTRONIC_PRISM_GT },
191 { USB_VENDOR_DELL, USB_PRODUCT_DELL_PRISM_GT_1 },
192 { USB_VENDOR_DELL, USB_PRODUCT_DELL_PRISM_GT_2 },
193 { USB_VENDOR_FSC, USB_PRODUCT_FSC_E5400 },
194 { USB_VENDOR_GLOBESPAN, USB_PRODUCT_GLOBESPAN_PRISM_GT_1 },
195 { USB_VENDOR_GLOBESPAN, USB_PRODUCT_GLOBESPAN_PRISM_GT_2 },
196 { USB_VENDOR_INTERSIL, USB_PRODUCT_INTERSIL_PRISM_GT },
197 { USB_VENDOR_SMC, USB_PRODUCT_SMC_2862WG },
198 { USB_VENDOR_WISTRONNEWEB, USB_PRODUCT_WISTRONNEWEB_UR045G },
199 { USB_VENDOR_XYRATEX, USB_PRODUCT_XYRATEX_PRISM_GT_1 },
200 { USB_VENDOR_XYRATEX, USB_PRODUCT_XYRATEX_PRISM_GT_2 },
201 { USB_VENDOR_ZCOM, USB_PRODUCT_ZCOM_XG703A }
202 };
203
204 static int
205 upgt_match(device_t dev)
206 {
207 struct usb_attach_arg *uaa = device_get_ivars(dev);
208
209 if (!uaa->iface)
210 return UMATCH_NONE;
211
212 if (usb_lookup(upgt_devs_2, uaa->vendor, uaa->product) != NULL)
213 return (UMATCH_VENDOR_PRODUCT);
214
215 return (UMATCH_NONE);
216 }
217
218 static int
219 upgt_attach(device_t dev)
220 {
221 int i;
222 struct upgt_softc *sc = device_get_softc(dev);
223 struct usb_attach_arg *uaa = device_get_ivars(dev);
224 usb_endpoint_descriptor_t *ed;
225 usb_interface_descriptor_t *id;
226 usbd_status error;
227
228 sc->sc_dev = dev;
229 sc->sc_udev = uaa->device;
230 #ifdef UPGT_DEBUG
231 sc->sc_debug = upgt_debug;
232 #endif
233
234 /* set configuration number */
235 if (usbd_set_config_no(sc->sc_udev, UPGT_CONFIG_NO, 0) != 0) {
236 device_printf(dev, "could not set configuration no!\n");
237 return ENXIO;
238 }
239
240 /* get the first interface handle */
241 error = usbd_device2interface_handle(sc->sc_udev, UPGT_IFACE_INDEX,
242 &sc->sc_iface);
243 if (error != 0) {
244 device_printf(dev, "could not get interface handle!\n");
245 return ENXIO;
246 }
247
248 /* find endpoints */
249 id = usbd_get_interface_descriptor(sc->sc_iface);
250 sc->sc_rx_no = sc->sc_tx_no = -1;
251 for (i = 0; i < id->bNumEndpoints; i++) {
252 ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
253 if (ed == NULL) {
254 device_printf(dev,
255 "no endpoint descriptor for iface %d!\n", i);
256 return ENXIO;
257 }
258
259 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
260 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK)
261 sc->sc_tx_no = ed->bEndpointAddress;
262 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
263 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK)
264 sc->sc_rx_no = ed->bEndpointAddress;
265
266 /*
267 * 0x01 TX pipe
268 * 0x81 RX pipe
269 *
270 * Deprecated scheme (not used with fw version >2.5.6.x):
271 * 0x02 TX MGMT pipe
272 * 0x82 TX MGMT pipe
273 */
274 if (sc->sc_tx_no != -1 && sc->sc_rx_no != -1)
275 break;
276 }
277 if (sc->sc_rx_no == -1 || sc->sc_tx_no == -1) {
278 device_printf(dev, "missing endpoint!\n");
279 return ENXIO;
280 }
281
282 /*
283 * Open TX and RX USB bulk pipes.
284 */
285 error = usbd_open_pipe(sc->sc_iface, sc->sc_tx_no, USBD_EXCLUSIVE_USE,
286 &sc->sc_tx_pipeh);
287 if (error != 0) {
288 device_printf(dev, "could not open TX pipe: %s!\n",
289 usbd_errstr(error));
290 goto fail;
291 }
292 error = usbd_open_pipe(sc->sc_iface, sc->sc_rx_no, USBD_EXCLUSIVE_USE,
293 &sc->sc_rx_pipeh);
294 if (error != 0) {
295 device_printf(dev, "could not open RX pipe: %s!\n",
296 usbd_errstr(error));
297 goto fail;
298 }
299
300 /* Allocate TX, RX, and CMD xfers. */
301 if (upgt_alloc_tx(sc) != 0)
302 goto fail;
303 if (upgt_alloc_rx(sc) != 0)
304 goto fail;
305 if (upgt_alloc_cmd(sc) != 0)
306 goto fail;
307
308 /* We need the firmware loaded to complete the attach. */
309 return upgt_attach_hook(dev);
310
311 fail:
312 device_printf(dev, "%s failed!\n", __func__);
313 return ENXIO;
314 }
315
316 static int
317 upgt_attach_hook(device_t dev)
318 {
319 struct ieee80211com *ic;
320 struct ifnet *ifp;
321 struct upgt_softc *sc = device_get_softc(dev);
322 struct upgt_data *data_rx = &sc->rx_data;
323 uint8_t bands;
324 usbd_status error;
325
326 ifp = sc->sc_ifp = if_alloc(IFT_IEEE80211);
327 if (ifp == NULL) {
328 device_printf(dev, "can not if_alloc()\n");
329 return ENXIO;
330 }
331
332 mtx_init(&sc->sc_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
333 MTX_DEF | MTX_RECURSE);
334 usb_init_task(&sc->sc_mcasttask, upgt_set_multi, sc);
335 usb_init_task(&sc->sc_scantask, upgt_scantask, sc);
336 usb_init_task(&sc->sc_task, upgt_task, sc);
337 usb_init_task(&sc->sc_task_tx, upgt_tx_task, sc);
338 callout_init(&sc->sc_led_ch, 0);
339 callout_init(&sc->sc_watchdog_ch, 0);
340
341 /* Initialize the device. */
342 if (upgt_device_reset(sc) != 0)
343 goto fail;
344
345 /* Verify the firmware. */
346 if (upgt_fw_verify(sc) != 0)
347 goto fail;
348
349 /* Calculate device memory space. */
350 if (sc->sc_memaddr_frame_start == 0 || sc->sc_memaddr_frame_end == 0) {
351 device_printf(dev,
352 "could not find memory space addresses on FW!\n");
353 goto fail;
354 }
355 sc->sc_memaddr_frame_end -= UPGT_MEMSIZE_RX + 1;
356 sc->sc_memaddr_rx_start = sc->sc_memaddr_frame_end + 1;
357
358 DPRINTF(sc, UPGT_DEBUG_FW, "memory address frame start=0x%08x\n",
359 sc->sc_memaddr_frame_start);
360 DPRINTF(sc, UPGT_DEBUG_FW, "memory address frame end=0x%08x\n",
361 sc->sc_memaddr_frame_end);
362 DPRINTF(sc, UPGT_DEBUG_FW, "memory address rx start=0x%08x\n",
363 sc->sc_memaddr_rx_start);
364
365 upgt_mem_init(sc);
366
367 /* Load the firmware. */
368 if (upgt_fw_load(sc) != 0)
369 goto fail;
370
371 /* Startup the RX pipe. */
372 usbd_setup_xfer(data_rx->xfer, sc->sc_rx_pipeh, data_rx, data_rx->buf,
373 MCLBYTES, USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, upgt_rxeof);
374 error = usbd_transfer(data_rx->xfer);
375 if (error != 0 && error != USBD_IN_PROGRESS) {
376 device_printf(dev, "could not queue RX transfer!\n");
377 goto fail;
378 }
379 usbd_delay_ms(sc->sc_udev, 100);
380
381 /* Read the whole EEPROM content and parse it. */
382 if (upgt_eeprom_read(sc) != 0)
383 goto fail;
384 if (upgt_eeprom_parse(sc) != 0)
385 goto fail;
386
387 /* Setup the 802.11 device. */
388 ifp->if_softc = sc;
389 if_initname(ifp, "upgt", device_get_unit(sc->sc_dev));
390 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST |
391 IFF_NEEDSGIANT; /* USB stack is still under Giant lock */
392 ifp->if_init = upgt_init;
393 ifp->if_ioctl = upgt_ioctl;
394 ifp->if_start = upgt_start;
395 IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN);
396 IFQ_SET_READY(&ifp->if_snd);
397
398 ic = ifp->if_l2com;
399 ic->ic_ifp = ifp;
400 ic->ic_phytype = IEEE80211_T_OFDM; /* not only, but not used */
401 ic->ic_opmode = IEEE80211_M_STA;
402 /* set device capabilities */
403 ic->ic_caps =
404 IEEE80211_C_STA /* station mode */
405 | IEEE80211_C_MONITOR /* monitor mode */
406 | IEEE80211_C_SHPREAMBLE /* short preamble supported */
407 | IEEE80211_C_SHSLOT /* short slot time supported */
408 | IEEE80211_C_BGSCAN /* capable of bg scanning */
409 | IEEE80211_C_WPA /* 802.11i */
410 ;
411
412 bands = 0;
413 setbit(&bands, IEEE80211_MODE_11B);
414 setbit(&bands, IEEE80211_MODE_11G);
415 ieee80211_init_channels(ic, NULL, &bands);
416
417 ieee80211_ifattach(ic);
418 ic->ic_raw_xmit = upgt_raw_xmit;
419 ic->ic_scan_start = upgt_scan_start;
420 ic->ic_scan_end = upgt_scan_end;
421 ic->ic_set_channel = upgt_set_channel;
422
423 ic->ic_vap_create = upgt_vap_create;
424 ic->ic_vap_delete = upgt_vap_delete;
425 ic->ic_update_mcast = upgt_update_mcast;
426
427 bpfattach(ifp, DLT_IEEE802_11_RADIO,
428 sizeof(struct ieee80211_frame) + sizeof(sc->sc_txtap));
429 sc->sc_rxtap_len = sizeof(sc->sc_rxtap);
430 sc->sc_rxtap.wr_ihdr.it_len = htole16(sc->sc_rxtap_len);
431 sc->sc_rxtap.wr_ihdr.it_present = htole32(UPGT_RX_RADIOTAP_PRESENT);
432 sc->sc_txtap_len = sizeof(sc->sc_txtap);
433 sc->sc_txtap.wt_ihdr.it_len = htole16(sc->sc_txtap_len);
434 sc->sc_txtap.wt_ihdr.it_present = htole32(UPGT_TX_RADIOTAP_PRESENT);
435
436 if (bootverbose)
437 ieee80211_announce(ic);
438
439 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, sc->sc_dev);
440 return 0;
441 fail:
442 device_printf(dev, "%s failed!\n", __func__);
443 mtx_destroy(&sc->sc_mtx);
444 if_free(ifp);
445 return ENXIO;
446 }
447
448 static void
449 upgt_tx_task(void *arg)
450 {
451 struct upgt_softc *sc = arg;
452 struct ifnet *ifp = sc->sc_ifp;
453 struct ieee80211com *ic = ifp->if_l2com;
454 struct ieee80211_frame *wh;
455 struct ieee80211_key *k;
456 struct upgt_data *data_tx;
457 struct upgt_lmac_mem *mem;
458 struct upgt_lmac_tx_desc *txdesc;
459 struct mbuf *m;
460 uint32_t addr;
461 int len, i;
462 usbd_status error;
463
464 upgt_set_led(sc, UPGT_LED_BLINK);
465
466 UPGT_LOCK(sc);
467 for (i = 0; i < upgt_txbuf; i++) {
468 data_tx = &sc->tx_data[i];
469 if (data_tx->m == NULL)
470 continue;
471
472 m = data_tx->m;
473 addr = data_tx->addr + UPGT_MEMSIZE_FRAME_HEAD;
474
475 /*
476 * Software crypto.
477 */
478 wh = mtod(m, struct ieee80211_frame *);
479 if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
480 k = ieee80211_crypto_encap(data_tx->ni, m);
481 if (k == NULL) {
482 device_printf(sc->sc_dev,
483 "ieee80211_crypto_encap returns NULL.\n");
484 goto done;
485 }
486
487 /* in case packet header moved, reset pointer */
488 wh = mtod(m, struct ieee80211_frame *);
489 }
490
491 /*
492 * Transmit the URB containing the TX data.
493 */
494 bzero(data_tx->buf, MCLBYTES);
495
496 mem = (struct upgt_lmac_mem *)data_tx->buf;
497 mem->addr = htole32(addr);
498
499 txdesc = (struct upgt_lmac_tx_desc *)(mem + 1);
500
501 if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) ==
502 IEEE80211_FC0_TYPE_MGT) {
503 /* mgmt frames */
504 txdesc->header1.flags = UPGT_H1_FLAGS_TX_MGMT;
505 /* always send mgmt frames at lowest rate (DS1) */
506 memset(txdesc->rates, 0x10, sizeof(txdesc->rates));
507 } else {
508 /* data frames */
509 txdesc->header1.flags = UPGT_H1_FLAGS_TX_DATA;
510 bcopy(sc->sc_cur_rateset, txdesc->rates,
511 sizeof(txdesc->rates));
512 }
513 txdesc->header1.type = UPGT_H1_TYPE_TX_DATA;
514 txdesc->header1.len = htole16(m->m_pkthdr.len);
515 txdesc->header2.reqid = htole32(data_tx->addr);
516 txdesc->header2.type = htole16(UPGT_H2_TYPE_TX_ACK_YES);
517 txdesc->header2.flags = htole16(UPGT_H2_FLAGS_TX_ACK_YES);
518 txdesc->type = htole32(UPGT_TX_DESC_TYPE_DATA);
519 txdesc->pad3[0] = UPGT_TX_DESC_PAD3_SIZE;
520
521 if (bpf_peers_present(ifp->if_bpf)) {
522 struct upgt_tx_radiotap_header *tap = &sc->sc_txtap;
523
524 tap->wt_flags = 0;
525 tap->wt_rate = 0; /* XXX where to get from? */
526 tap->wt_chan_freq = htole16(ic->ic_curchan->ic_freq);
527 tap->wt_chan_flags = htole16(ic->ic_curchan->ic_flags);
528
529 bpf_mtap2(ifp->if_bpf, tap, sc->sc_txtap_len, m);
530 }
531
532 /* copy frame below our TX descriptor header */
533 m_copydata(m, 0, m->m_pkthdr.len,
534 data_tx->buf + (sizeof(*mem) + sizeof(*txdesc)));
535 /* calculate frame size */
536 len = sizeof(*mem) + sizeof(*txdesc) + m->m_pkthdr.len;
537 /* we need to align the frame to a 4 byte boundary */
538 len = (len + 3) & ~3;
539 /* calculate frame checksum */
540 mem->chksum = upgt_chksum_le((uint32_t *)txdesc,
541 len - sizeof(*mem));
542 /* we do not need the mbuf anymore */
543 m_freem(m);
544 data_tx->m = NULL;
545
546 DPRINTF(sc, UPGT_DEBUG_XMIT, "%s: TX start data sending\n",
547 __func__);
548 KASSERT(len <= MCLBYTES, ("mbuf is small for saving data"));
549
550 usbd_setup_xfer(data_tx->xfer, sc->sc_tx_pipeh, data_tx,
551 data_tx->buf, len, USBD_FORCE_SHORT_XFER | USBD_NO_COPY,
552 UPGT_USB_TIMEOUT, upgt_txeof);
553 UPGT_UNLOCK(sc);
554 mtx_lock(&Giant);
555 error = usbd_transfer(data_tx->xfer);
556 mtx_unlock(&Giant);
557 UPGT_LOCK(sc);
558 if (error != 0 && error != USBD_IN_PROGRESS) {
559 device_printf(sc->sc_dev,
560 "could not transmit TX data URB!\n");
561 goto done;
562 }
563
564 DPRINTF(sc, UPGT_DEBUG_XMIT, "TX sent (%d bytes)\n", len);
565 }
566 done:
567 UPGT_UNLOCK(sc);
568 /*
569 * If we don't regulary read the device statistics, the RX queue
570 * will stall. It's strange, but it works, so we keep reading
571 * the statistics here. *shrug*
572 */
573 (void)upgt_get_stats(sc);
574 }
575
576 static void
577 upgt_txeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
578 {
579 struct upgt_data *data_tx = priv;
580 struct upgt_softc *sc = data_tx->sc;
581
582 if (status != USBD_NORMAL_COMPLETION) {
583 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
584 return;
585 if (status == USBD_STALLED) {
586 usbd_clear_endpoint_stall_async(sc->sc_rx_pipeh);
587 return;
588 }
589
590 device_printf(sc->sc_dev, "TX warning(%s)\n",
591 usbd_errstr(status));
592 }
593 }
594
595 static int
596 upgt_get_stats(struct upgt_softc *sc)
597 {
598 struct upgt_data *data_cmd = &sc->cmd_data;
599 struct upgt_lmac_mem *mem;
600 struct upgt_lmac_stats *stats;
601 int len;
602
603 /*
604 * Transmit the URB containing the CMD data.
605 */
606 bzero(data_cmd->buf, MCLBYTES);
607
608 mem = (struct upgt_lmac_mem *)data_cmd->buf;
609 mem->addr = htole32(sc->sc_memaddr_frame_start +
610 UPGT_MEMSIZE_FRAME_HEAD);
611
612 stats = (struct upgt_lmac_stats *)(mem + 1);
613
614 stats->header1.flags = 0;
615 stats->header1.type = UPGT_H1_TYPE_CTRL;
616 stats->header1.len = htole16(
617 sizeof(struct upgt_lmac_stats) - sizeof(struct upgt_lmac_header));
618
619 stats->header2.reqid = htole32(sc->sc_memaddr_frame_start);
620 stats->header2.type = htole16(UPGT_H2_TYPE_STATS);
621 stats->header2.flags = 0;
622
623 len = sizeof(*mem) + sizeof(*stats);
624
625 mem->chksum = upgt_chksum_le((uint32_t *)stats,
626 len - sizeof(*mem));
627
628 if (upgt_bulk_xmit(sc, data_cmd, sc->sc_tx_pipeh, &len, 0) != 0) {
629 device_printf(sc->sc_dev,
630 "could not transmit statistics CMD data URB!\n");
631 return (EIO);
632 }
633
634 return (0);
635 }
636
637 static int
638 upgt_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
639 {
640 struct upgt_softc *sc = ifp->if_softc;
641 struct ieee80211com *ic = ifp->if_l2com;
642 struct ifreq *ifr = (struct ifreq *) data;
643 int error = 0, startall = 0;
644
645 switch (cmd) {
646 case SIOCSIFFLAGS:
647 mtx_lock(&Giant);
648 if (ifp->if_flags & IFF_UP) {
649 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
650 if ((ifp->if_flags ^ sc->sc_if_flags) &
651 (IFF_ALLMULTI | IFF_PROMISC))
652 upgt_set_multi(sc);
653 } else {
654 upgt_init(sc);
655 startall = 1;
656 }
657 } else {
658 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
659 upgt_stop(sc, 1);
660 }
661 sc->sc_if_flags = ifp->if_flags;
662 if (startall)
663 ieee80211_start_all(ic);
664 mtx_unlock(&Giant);
665 break;
666 case SIOCGIFMEDIA:
667 error = ifmedia_ioctl(ifp, ifr, &ic->ic_media, cmd);
668 break;
669 case SIOCGIFADDR:
670 error = ether_ioctl(ifp, cmd, data);
671 break;
672 default:
673 error = EINVAL;
674 break;
675 }
676 return error;
677 }
678
679 static void
680 upgt_stop(struct upgt_softc *sc, int disable)
681 {
682 struct ifnet *ifp = sc->sc_ifp;
683
684 /* abort and close TX / RX pipes */
685 if (sc->sc_tx_pipeh != NULL)
686 usbd_abort_pipe(sc->sc_tx_pipeh);
687 if (sc->sc_rx_pipeh != NULL)
688 usbd_abort_pipe(sc->sc_rx_pipeh);
689
690 /* device down */
691 sc->sc_tx_timer = 0;
692 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
693 }
694
695 static void
696 upgt_task(void *arg)
697 {
698 struct upgt_softc *sc = arg;
699 struct ifnet *ifp = sc->sc_ifp;
700 struct ieee80211com *ic = ifp->if_l2com;
701 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
702 struct upgt_vap *uvp = UPGT_VAP(vap);
703
704 DPRINTF(sc, UPGT_DEBUG_STATE, "%s: %s -> %s\n", __func__,
705 ieee80211_state_name[vap->iv_state],
706 ieee80211_state_name[sc->sc_state]);
707
708 switch (sc->sc_state) {
709 case IEEE80211_S_INIT:
710 /* do not accept any frames if the device is down */
711 UPGT_LOCK(sc);
712 upgt_set_macfilter(sc, sc->sc_state);
713 UPGT_UNLOCK(sc);
714 upgt_set_led(sc, UPGT_LED_OFF);
715 break;
716 case IEEE80211_S_SCAN:
717 upgt_set_chan(sc, ic->ic_curchan);
718 break;
719 case IEEE80211_S_AUTH:
720 upgt_set_chan(sc, ic->ic_curchan);
721 break;
722 case IEEE80211_S_ASSOC:
723 break;
724 case IEEE80211_S_RUN:
725 UPGT_LOCK(sc);
726 upgt_set_macfilter(sc, sc->sc_state);
727 UPGT_UNLOCK(sc);
728 upgt_set_led(sc, UPGT_LED_ON);
729 break;
730 default:
731 break;
732 }
733
734 IEEE80211_LOCK(ic);
735 uvp->newstate(vap, sc->sc_state, sc->sc_arg);
736 if (vap->iv_newstate_cb != NULL)
737 vap->iv_newstate_cb(vap, sc->sc_state, sc->sc_arg);
738 IEEE80211_UNLOCK(ic);
739 }
740
741 static void
742 upgt_set_led(struct upgt_softc *sc, int action)
743 {
744 struct upgt_data *data_cmd = &sc->cmd_data;
745 struct upgt_lmac_mem *mem;
746 struct upgt_lmac_led *led;
747 int len;
748
749 /*
750 * Transmit the URB containing the CMD data.
751 */
752 bzero(data_cmd->buf, MCLBYTES);
753
754 mem = (struct upgt_lmac_mem *)data_cmd->buf;
755 mem->addr = htole32(sc->sc_memaddr_frame_start +
756 UPGT_MEMSIZE_FRAME_HEAD);
757
758 led = (struct upgt_lmac_led *)(mem + 1);
759
760 led->header1.flags = UPGT_H1_FLAGS_TX_NO_CALLBACK;
761 led->header1.type = UPGT_H1_TYPE_CTRL;
762 led->header1.len = htole16(
763 sizeof(struct upgt_lmac_led) -
764 sizeof(struct upgt_lmac_header));
765
766 led->header2.reqid = htole32(sc->sc_memaddr_frame_start);
767 led->header2.type = htole16(UPGT_H2_TYPE_LED);
768 led->header2.flags = 0;
769
770 switch (action) {
771 case UPGT_LED_OFF:
772 led->mode = htole16(UPGT_LED_MODE_SET);
773 led->action_fix = 0;
774 led->action_tmp = htole16(UPGT_LED_ACTION_OFF);
775 led->action_tmp_dur = 0;
776 break;
777 case UPGT_LED_ON:
778 led->mode = htole16(UPGT_LED_MODE_SET);
779 led->action_fix = 0;
780 led->action_tmp = htole16(UPGT_LED_ACTION_ON);
781 led->action_tmp_dur = 0;
782 break;
783 case UPGT_LED_BLINK:
784 if (sc->sc_state != IEEE80211_S_RUN)
785 return;
786 if (sc->sc_led_blink)
787 /* previous blink was not finished */
788 return;
789 led->mode = htole16(UPGT_LED_MODE_SET);
790 led->action_fix = htole16(UPGT_LED_ACTION_OFF);
791 led->action_tmp = htole16(UPGT_LED_ACTION_ON);
792 led->action_tmp_dur = htole16(UPGT_LED_ACTION_TMP_DUR);
793 /* lock blink */
794 sc->sc_led_blink = 1;
795 callout_reset(&sc->sc_led_ch, hz, upgt_set_led_blink, sc);
796 break;
797 default:
798 return;
799 }
800
801 len = sizeof(*mem) + sizeof(*led);
802
803 mem->chksum = upgt_chksum_le((uint32_t *)led,
804 len - sizeof(*mem));
805
806 if (upgt_bulk_xmit(sc, data_cmd, sc->sc_tx_pipeh, &len, 0) != 0)
807 device_printf(sc->sc_dev, "could not transmit led CMD URB!\n");
808 }
809
810 static void
811 upgt_set_led_blink(void *arg)
812 {
813 struct upgt_softc *sc = arg;
814
815 /* blink finished, we are ready for a next one */
816 sc->sc_led_blink = 0;
817 }
818
819 static void
820 upgt_init(void *priv)
821 {
822 struct upgt_softc *sc = priv;
823 struct ifnet *ifp = sc->sc_ifp;
824 struct ieee80211com *ic = ifp->if_l2com;
825
826 UPGT_LOCK(sc);
827 upgt_init_locked(sc);
828 UPGT_UNLOCK(sc);
829
830 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
831 ieee80211_start_all(ic); /* start all vap's */
832 }
833
834 static void
835 upgt_init_locked(struct upgt_softc *sc)
836 {
837 struct ifnet *ifp = sc->sc_ifp;
838 struct ieee80211com *ic = ifp->if_l2com;
839
840 IEEE80211_ADDR_COPY(ic->ic_myaddr, IF_LLADDR(ifp));
841 DPRINTF(sc, UPGT_DEBUG_RESET, "setting MAC address to %s\n",
842 ether_sprintf(ic->ic_myaddr));
843
844 upgt_set_macfilter(sc, IEEE80211_S_SCAN);
845
846 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
847 ifp->if_drv_flags |= IFF_DRV_RUNNING;
848 }
849
850 static int
851 upgt_set_macfilter(struct upgt_softc *sc, uint8_t state)
852 {
853 struct ifnet *ifp = sc->sc_ifp;
854 struct ieee80211com *ic = ifp->if_l2com;
855 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
856 struct ieee80211_node *ni = vap->iv_bss;
857 struct upgt_data *data_cmd = &sc->cmd_data;
858 struct upgt_lmac_mem *mem;
859 struct upgt_lmac_filter *filter;
860 int len;
861 uint8_t broadcast[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
862
863 /*
864 * Transmit the URB containing the CMD data.
865 */
866 bzero(data_cmd->buf, MCLBYTES);
867
868 mem = (struct upgt_lmac_mem *)data_cmd->buf;
869 mem->addr = htole32(sc->sc_memaddr_frame_start +
870 UPGT_MEMSIZE_FRAME_HEAD);
871
872 filter = (struct upgt_lmac_filter *)(mem + 1);
873
874 filter->header1.flags = UPGT_H1_FLAGS_TX_NO_CALLBACK;
875 filter->header1.type = UPGT_H1_TYPE_CTRL;
876 filter->header1.len = htole16(
877 sizeof(struct upgt_lmac_filter) -
878 sizeof(struct upgt_lmac_header));
879
880 filter->header2.reqid = htole32(sc->sc_memaddr_frame_start);
881 filter->header2.type = htole16(UPGT_H2_TYPE_MACFILTER);
882 filter->header2.flags = 0;
883
884 switch (state) {
885 case IEEE80211_S_INIT:
886 DPRINTF(sc, UPGT_DEBUG_STATE, "%s: set MAC filter to INIT\n",
887 __func__);
888 filter->type = htole16(UPGT_FILTER_TYPE_RESET);
889 break;
890 case IEEE80211_S_SCAN:
891 DPRINTF(sc, UPGT_DEBUG_STATE,
892 "set MAC filter to SCAN (bssid %s)\n",
893 ether_sprintf(broadcast));
894 filter->type = htole16(UPGT_FILTER_TYPE_NONE);
895 IEEE80211_ADDR_COPY(filter->dst, ic->ic_myaddr);
896 IEEE80211_ADDR_COPY(filter->src, broadcast);
897 filter->unknown1 = htole16(UPGT_FILTER_UNKNOWN1);
898 filter->rxaddr = htole32(sc->sc_memaddr_rx_start);
899 filter->unknown2 = htole16(UPGT_FILTER_UNKNOWN2);
900 filter->rxhw = htole32(sc->sc_eeprom_hwrx);
901 filter->unknown3 = htole16(UPGT_FILTER_UNKNOWN3);
902 break;
903 case IEEE80211_S_RUN:
904 /* XXX monitor mode isn't tested yet. */
905 if (vap->iv_opmode == IEEE80211_M_MONITOR) {
906 filter->type = htole16(UPGT_FILTER_TYPE_MONITOR);
907 IEEE80211_ADDR_COPY(filter->dst, ic->ic_myaddr);
908 IEEE80211_ADDR_COPY(filter->src, ni->ni_bssid);
909 filter->unknown1 = htole16(UPGT_FILTER_MONITOR_UNKNOWN1);
910 filter->rxaddr = htole32(sc->sc_memaddr_rx_start);
911 filter->unknown2 = htole16(UPGT_FILTER_MONITOR_UNKNOWN2);
912 filter->rxhw = htole32(sc->sc_eeprom_hwrx);
913 filter->unknown3 = htole16(UPGT_FILTER_MONITOR_UNKNOWN3);
914 } else {
915 DPRINTF(sc, UPGT_DE |