zd_mac.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047
  1. /* ZD1211 USB-WLAN driver for Linux
  2. *
  3. * Copyright (C) 2005-2007 Ulrich Kunitz <kune@deine-taler.de>
  4. * Copyright (C) 2006-2007 Daniel Drake <dsd@gentoo.org>
  5. * Copyright (C) 2006-2007 Michael Wu <flamingice@sourmilk.net>
  6. * Copyright (c) 2007 Luis R. Rodriguez <mcgrof@winlab.rutgers.edu>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. */
  22. #include <linux/netdevice.h>
  23. #include <linux/etherdevice.h>
  24. #include <linux/usb.h>
  25. #include <linux/jiffies.h>
  26. #include <net/ieee80211_radiotap.h>
  27. #include "zd_def.h"
  28. #include "zd_chip.h"
  29. #include "zd_mac.h"
  30. #include "zd_ieee80211.h"
  31. #include "zd_rf.h"
  32. /* This table contains the hardware specific values for the modulation rates. */
  33. static const struct ieee80211_rate zd_rates[] = {
  34. { .bitrate = 10,
  35. .hw_value = ZD_CCK_RATE_1M, },
  36. { .bitrate = 20,
  37. .hw_value = ZD_CCK_RATE_2M,
  38. .hw_value_short = ZD_CCK_RATE_2M | ZD_CCK_PREA_SHORT,
  39. .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  40. { .bitrate = 55,
  41. .hw_value = ZD_CCK_RATE_5_5M,
  42. .hw_value_short = ZD_CCK_RATE_5_5M | ZD_CCK_PREA_SHORT,
  43. .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  44. { .bitrate = 110,
  45. .hw_value = ZD_CCK_RATE_11M,
  46. .hw_value_short = ZD_CCK_RATE_11M | ZD_CCK_PREA_SHORT,
  47. .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  48. { .bitrate = 60,
  49. .hw_value = ZD_OFDM_RATE_6M,
  50. .flags = 0 },
  51. { .bitrate = 90,
  52. .hw_value = ZD_OFDM_RATE_9M,
  53. .flags = 0 },
  54. { .bitrate = 120,
  55. .hw_value = ZD_OFDM_RATE_12M,
  56. .flags = 0 },
  57. { .bitrate = 180,
  58. .hw_value = ZD_OFDM_RATE_18M,
  59. .flags = 0 },
  60. { .bitrate = 240,
  61. .hw_value = ZD_OFDM_RATE_24M,
  62. .flags = 0 },
  63. { .bitrate = 360,
  64. .hw_value = ZD_OFDM_RATE_36M,
  65. .flags = 0 },
  66. { .bitrate = 480,
  67. .hw_value = ZD_OFDM_RATE_48M,
  68. .flags = 0 },
  69. { .bitrate = 540,
  70. .hw_value = ZD_OFDM_RATE_54M,
  71. .flags = 0 },
  72. };
  73. static const struct ieee80211_channel zd_channels[] = {
  74. { .center_freq = 2412, .hw_value = 1 },
  75. { .center_freq = 2417, .hw_value = 2 },
  76. { .center_freq = 2422, .hw_value = 3 },
  77. { .center_freq = 2427, .hw_value = 4 },
  78. { .center_freq = 2432, .hw_value = 5 },
  79. { .center_freq = 2437, .hw_value = 6 },
  80. { .center_freq = 2442, .hw_value = 7 },
  81. { .center_freq = 2447, .hw_value = 8 },
  82. { .center_freq = 2452, .hw_value = 9 },
  83. { .center_freq = 2457, .hw_value = 10 },
  84. { .center_freq = 2462, .hw_value = 11 },
  85. { .center_freq = 2467, .hw_value = 12 },
  86. { .center_freq = 2472, .hw_value = 13 },
  87. { .center_freq = 2484, .hw_value = 14 },
  88. };
  89. static void housekeeping_init(struct zd_mac *mac);
  90. static void housekeeping_enable(struct zd_mac *mac);
  91. static void housekeeping_disable(struct zd_mac *mac);
  92. int zd_mac_preinit_hw(struct ieee80211_hw *hw)
  93. {
  94. int r;
  95. u8 addr[ETH_ALEN];
  96. struct zd_mac *mac = zd_hw_mac(hw);
  97. r = zd_chip_read_mac_addr_fw(&mac->chip, addr);
  98. if (r)
  99. return r;
  100. SET_IEEE80211_PERM_ADDR(hw, addr);
  101. return 0;
  102. }
  103. int zd_mac_init_hw(struct ieee80211_hw *hw)
  104. {
  105. int r;
  106. struct zd_mac *mac = zd_hw_mac(hw);
  107. struct zd_chip *chip = &mac->chip;
  108. u8 default_regdomain;
  109. r = zd_chip_enable_int(chip);
  110. if (r)
  111. goto out;
  112. r = zd_chip_init_hw(chip);
  113. if (r)
  114. goto disable_int;
  115. ZD_ASSERT(!irqs_disabled());
  116. r = zd_read_regdomain(chip, &default_regdomain);
  117. if (r)
  118. goto disable_int;
  119. spin_lock_irq(&mac->lock);
  120. mac->regdomain = mac->default_regdomain = default_regdomain;
  121. spin_unlock_irq(&mac->lock);
  122. /* We must inform the device that we are doing encryption/decryption in
  123. * software at the moment. */
  124. r = zd_set_encryption_type(chip, ENC_SNIFFER);
  125. if (r)
  126. goto disable_int;
  127. zd_geo_init(hw, mac->regdomain);
  128. r = 0;
  129. disable_int:
  130. zd_chip_disable_int(chip);
  131. out:
  132. return r;
  133. }
  134. void zd_mac_clear(struct zd_mac *mac)
  135. {
  136. flush_workqueue(zd_workqueue);
  137. zd_chip_clear(&mac->chip);
  138. ZD_ASSERT(!spin_is_locked(&mac->lock));
  139. ZD_MEMCLEAR(mac, sizeof(struct zd_mac));
  140. }
  141. static int set_rx_filter(struct zd_mac *mac)
  142. {
  143. unsigned long flags;
  144. u32 filter = STA_RX_FILTER;
  145. spin_lock_irqsave(&mac->lock, flags);
  146. if (mac->pass_ctrl)
  147. filter |= RX_FILTER_CTRL;
  148. spin_unlock_irqrestore(&mac->lock, flags);
  149. return zd_iowrite32(&mac->chip, CR_RX_FILTER, filter);
  150. }
  151. static int set_mc_hash(struct zd_mac *mac)
  152. {
  153. struct zd_mc_hash hash;
  154. zd_mc_clear(&hash);
  155. return zd_chip_set_multicast_hash(&mac->chip, &hash);
  156. }
  157. static int zd_op_start(struct ieee80211_hw *hw)
  158. {
  159. struct zd_mac *mac = zd_hw_mac(hw);
  160. struct zd_chip *chip = &mac->chip;
  161. struct zd_usb *usb = &chip->usb;
  162. int r;
  163. if (!usb->initialized) {
  164. r = zd_usb_init_hw(usb);
  165. if (r)
  166. goto out;
  167. }
  168. r = zd_chip_enable_int(chip);
  169. if (r < 0)
  170. goto out;
  171. r = zd_chip_set_basic_rates(chip, CR_RATES_80211B | CR_RATES_80211G);
  172. if (r < 0)
  173. goto disable_int;
  174. r = set_rx_filter(mac);
  175. if (r)
  176. goto disable_int;
  177. r = set_mc_hash(mac);
  178. if (r)
  179. goto disable_int;
  180. r = zd_chip_switch_radio_on(chip);
  181. if (r < 0)
  182. goto disable_int;
  183. r = zd_chip_enable_rxtx(chip);
  184. if (r < 0)
  185. goto disable_radio;
  186. r = zd_chip_enable_hwint(chip);
  187. if (r < 0)
  188. goto disable_rxtx;
  189. housekeeping_enable(mac);
  190. return 0;
  191. disable_rxtx:
  192. zd_chip_disable_rxtx(chip);
  193. disable_radio:
  194. zd_chip_switch_radio_off(chip);
  195. disable_int:
  196. zd_chip_disable_int(chip);
  197. out:
  198. return r;
  199. }
  200. /**
  201. * clear_tx_skb_control_block - clears the control block of tx skbuffs
  202. * @skb: a &struct sk_buff pointer
  203. *
  204. * This clears the control block of skbuff buffers, which were transmitted to
  205. * the device. Notify that the function is not thread-safe, so prevent
  206. * multiple calls.
  207. */
  208. static void clear_tx_skb_control_block(struct sk_buff *skb)
  209. {
  210. struct zd_tx_skb_control_block *cb =
  211. (struct zd_tx_skb_control_block *)skb->cb;
  212. kfree(cb->control);
  213. cb->control = NULL;
  214. }
  215. /**
  216. * kfree_tx_skb - frees a tx skbuff
  217. * @skb: a &struct sk_buff pointer
  218. *
  219. * Frees the tx skbuff. Frees also the allocated control structure in the
  220. * control block if necessary.
  221. */
  222. static void kfree_tx_skb(struct sk_buff *skb)
  223. {
  224. clear_tx_skb_control_block(skb);
  225. dev_kfree_skb_any(skb);
  226. }
  227. static void zd_op_stop(struct ieee80211_hw *hw)
  228. {
  229. struct zd_mac *mac = zd_hw_mac(hw);
  230. struct zd_chip *chip = &mac->chip;
  231. struct sk_buff *skb;
  232. struct sk_buff_head *ack_wait_queue = &mac->ack_wait_queue;
  233. /* The order here deliberately is a little different from the open()
  234. * method, since we need to make sure there is no opportunity for RX
  235. * frames to be processed by mac80211 after we have stopped it.
  236. */
  237. zd_chip_disable_rxtx(chip);
  238. housekeeping_disable(mac);
  239. flush_workqueue(zd_workqueue);
  240. zd_chip_disable_hwint(chip);
  241. zd_chip_switch_radio_off(chip);
  242. zd_chip_disable_int(chip);
  243. while ((skb = skb_dequeue(ack_wait_queue)))
  244. kfree_tx_skb(skb);
  245. }
  246. /**
  247. * init_tx_skb_control_block - initializes skb control block
  248. * @skb: a &sk_buff pointer
  249. * @dev: pointer to the mac80221 device
  250. * @control: mac80211 tx control applying for the frame in @skb
  251. *
  252. * Initializes the control block of the skbuff to be transmitted.
  253. */
  254. static int init_tx_skb_control_block(struct sk_buff *skb,
  255. struct ieee80211_hw *hw,
  256. struct ieee80211_tx_control *control)
  257. {
  258. struct zd_tx_skb_control_block *cb =
  259. (struct zd_tx_skb_control_block *)skb->cb;
  260. ZD_ASSERT(sizeof(*cb) <= sizeof(skb->cb));
  261. memset(cb, 0, sizeof(*cb));
  262. cb->hw= hw;
  263. cb->control = kmalloc(sizeof(*control), GFP_ATOMIC);
  264. if (cb->control == NULL)
  265. return -ENOMEM;
  266. memcpy(cb->control, control, sizeof(*control));
  267. return 0;
  268. }
  269. /**
  270. * tx_status - reports tx status of a packet if required
  271. * @hw - a &struct ieee80211_hw pointer
  272. * @skb - a sk-buffer
  273. * @status - the tx status of the packet without control information
  274. * @success - True for successfull transmission of the frame
  275. *
  276. * This information calls ieee80211_tx_status_irqsafe() if required by the
  277. * control information. It copies the control information into the status
  278. * information.
  279. *
  280. * If no status information has been requested, the skb is freed.
  281. */
  282. static void tx_status(struct ieee80211_hw *hw, struct sk_buff *skb,
  283. struct ieee80211_tx_status *status,
  284. bool success)
  285. {
  286. struct zd_tx_skb_control_block *cb = (struct zd_tx_skb_control_block *)
  287. skb->cb;
  288. ZD_ASSERT(cb->control != NULL);
  289. memcpy(&status->control, cb->control, sizeof(status->control));
  290. if (!success)
  291. status->excessive_retries = 1;
  292. clear_tx_skb_control_block(skb);
  293. ieee80211_tx_status_irqsafe(hw, skb, status);
  294. }
  295. /**
  296. * zd_mac_tx_failed - callback for failed frames
  297. * @dev: the mac80211 wireless device
  298. *
  299. * This function is called if a frame couldn't be succesfully be
  300. * transferred. The first frame from the tx queue, will be selected and
  301. * reported as error to the upper layers.
  302. */
  303. void zd_mac_tx_failed(struct ieee80211_hw *hw)
  304. {
  305. struct sk_buff_head *q = &zd_hw_mac(hw)->ack_wait_queue;
  306. struct sk_buff *skb;
  307. struct ieee80211_tx_status status;
  308. skb = skb_dequeue(q);
  309. if (skb == NULL)
  310. return;
  311. memset(&status, 0, sizeof(status));
  312. tx_status(hw, skb, &status, 0);
  313. }
  314. /**
  315. * zd_mac_tx_to_dev - callback for USB layer
  316. * @skb: a &sk_buff pointer
  317. * @error: error value, 0 if transmission successful
  318. *
  319. * Informs the MAC layer that the frame has successfully transferred to the
  320. * device. If an ACK is required and the transfer to the device has been
  321. * successful, the packets are put on the @ack_wait_queue with
  322. * the control set removed.
  323. */
  324. void zd_mac_tx_to_dev(struct sk_buff *skb, int error)
  325. {
  326. struct zd_tx_skb_control_block *cb =
  327. (struct zd_tx_skb_control_block *)skb->cb;
  328. struct ieee80211_hw *hw = cb->hw;
  329. if (likely(cb->control)) {
  330. skb_pull(skb, sizeof(struct zd_ctrlset));
  331. if (unlikely(error ||
  332. (cb->control->flags & IEEE80211_TXCTL_NO_ACK)))
  333. {
  334. struct ieee80211_tx_status status;
  335. memset(&status, 0, sizeof(status));
  336. tx_status(hw, skb, &status, !error);
  337. } else {
  338. struct sk_buff_head *q =
  339. &zd_hw_mac(hw)->ack_wait_queue;
  340. skb_queue_tail(q, skb);
  341. while (skb_queue_len(q) > ZD_MAC_MAX_ACK_WAITERS)
  342. zd_mac_tx_failed(hw);
  343. }
  344. } else {
  345. kfree_tx_skb(skb);
  346. }
  347. }
  348. static int zd_calc_tx_length_us(u8 *service, u8 zd_rate, u16 tx_length)
  349. {
  350. /* ZD_PURE_RATE() must be used to remove the modulation type flag of
  351. * the zd-rate values.
  352. */
  353. static const u8 rate_divisor[] = {
  354. [ZD_PURE_RATE(ZD_CCK_RATE_1M)] = 1,
  355. [ZD_PURE_RATE(ZD_CCK_RATE_2M)] = 2,
  356. /* Bits must be doubled. */
  357. [ZD_PURE_RATE(ZD_CCK_RATE_5_5M)] = 11,
  358. [ZD_PURE_RATE(ZD_CCK_RATE_11M)] = 11,
  359. [ZD_PURE_RATE(ZD_OFDM_RATE_6M)] = 6,
  360. [ZD_PURE_RATE(ZD_OFDM_RATE_9M)] = 9,
  361. [ZD_PURE_RATE(ZD_OFDM_RATE_12M)] = 12,
  362. [ZD_PURE_RATE(ZD_OFDM_RATE_18M)] = 18,
  363. [ZD_PURE_RATE(ZD_OFDM_RATE_24M)] = 24,
  364. [ZD_PURE_RATE(ZD_OFDM_RATE_36M)] = 36,
  365. [ZD_PURE_RATE(ZD_OFDM_RATE_48M)] = 48,
  366. [ZD_PURE_RATE(ZD_OFDM_RATE_54M)] = 54,
  367. };
  368. u32 bits = (u32)tx_length * 8;
  369. u32 divisor;
  370. divisor = rate_divisor[ZD_PURE_RATE(zd_rate)];
  371. if (divisor == 0)
  372. return -EINVAL;
  373. switch (zd_rate) {
  374. case ZD_CCK_RATE_5_5M:
  375. bits = (2*bits) + 10; /* round up to the next integer */
  376. break;
  377. case ZD_CCK_RATE_11M:
  378. if (service) {
  379. u32 t = bits % 11;
  380. *service &= ~ZD_PLCP_SERVICE_LENGTH_EXTENSION;
  381. if (0 < t && t <= 3) {
  382. *service |= ZD_PLCP_SERVICE_LENGTH_EXTENSION;
  383. }
  384. }
  385. bits += 10; /* round up to the next integer */
  386. break;
  387. }
  388. return bits/divisor;
  389. }
  390. static void cs_set_control(struct zd_mac *mac, struct zd_ctrlset *cs,
  391. struct ieee80211_hdr *header, u32 flags)
  392. {
  393. u16 fctl = le16_to_cpu(header->frame_control);
  394. /*
  395. * CONTROL TODO:
  396. * - if backoff needed, enable bit 0
  397. * - if burst (backoff not needed) disable bit 0
  398. */
  399. cs->control = 0;
  400. /* First fragment */
  401. if (flags & IEEE80211_TXCTL_FIRST_FRAGMENT)
  402. cs->control |= ZD_CS_NEED_RANDOM_BACKOFF;
  403. /* Multicast */
  404. if (is_multicast_ether_addr(header->addr1))
  405. cs->control |= ZD_CS_MULTICAST;
  406. /* PS-POLL */
  407. if ((fctl & (IEEE80211_FCTL_FTYPE|IEEE80211_FCTL_STYPE)) ==
  408. (IEEE80211_FTYPE_CTL|IEEE80211_STYPE_PSPOLL))
  409. cs->control |= ZD_CS_PS_POLL_FRAME;
  410. if (flags & IEEE80211_TXCTL_USE_RTS_CTS)
  411. cs->control |= ZD_CS_RTS;
  412. if (flags & IEEE80211_TXCTL_USE_CTS_PROTECT)
  413. cs->control |= ZD_CS_SELF_CTS;
  414. /* FIXME: Management frame? */
  415. }
  416. void zd_mac_config_beacon(struct ieee80211_hw *hw, struct sk_buff *beacon)
  417. {
  418. struct zd_mac *mac = zd_hw_mac(hw);
  419. u32 tmp, j = 0;
  420. /* 4 more bytes for tail CRC */
  421. u32 full_len = beacon->len + 4;
  422. zd_iowrite32(&mac->chip, CR_BCN_FIFO_SEMAPHORE, 0);
  423. zd_ioread32(&mac->chip, CR_BCN_FIFO_SEMAPHORE, &tmp);
  424. while (tmp & 0x2) {
  425. zd_ioread32(&mac->chip, CR_BCN_FIFO_SEMAPHORE, &tmp);
  426. if ((++j % 100) == 0) {
  427. printk(KERN_ERR "CR_BCN_FIFO_SEMAPHORE not ready\n");
  428. if (j >= 500) {
  429. printk(KERN_ERR "Giving up beacon config.\n");
  430. return;
  431. }
  432. }
  433. msleep(1);
  434. }
  435. zd_iowrite32(&mac->chip, CR_BCN_FIFO, full_len - 1);
  436. if (zd_chip_is_zd1211b(&mac->chip))
  437. zd_iowrite32(&mac->chip, CR_BCN_LENGTH, full_len - 1);
  438. for (j = 0 ; j < beacon->len; j++)
  439. zd_iowrite32(&mac->chip, CR_BCN_FIFO,
  440. *((u8 *)(beacon->data + j)));
  441. for (j = 0; j < 4; j++)
  442. zd_iowrite32(&mac->chip, CR_BCN_FIFO, 0x0);
  443. zd_iowrite32(&mac->chip, CR_BCN_FIFO_SEMAPHORE, 1);
  444. /* 802.11b/g 2.4G CCK 1Mb
  445. * 802.11a, not yet implemented, uses different values (see GPL vendor
  446. * driver)
  447. */
  448. zd_iowrite32(&mac->chip, CR_BCN_PLCP_CFG, 0x00000400 |
  449. (full_len << 19));
  450. }
  451. static int fill_ctrlset(struct zd_mac *mac,
  452. struct sk_buff *skb,
  453. struct ieee80211_tx_control *control)
  454. {
  455. int r;
  456. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  457. unsigned int frag_len = skb->len + FCS_LEN;
  458. unsigned int packet_length;
  459. struct zd_ctrlset *cs = (struct zd_ctrlset *)
  460. skb_push(skb, sizeof(struct zd_ctrlset));
  461. ZD_ASSERT(frag_len <= 0xffff);
  462. cs->modulation = control->tx_rate->hw_value;
  463. if (control->flags & IEEE80211_TXCTL_SHORT_PREAMBLE)
  464. cs->modulation = control->tx_rate->hw_value_short;
  465. cs->tx_length = cpu_to_le16(frag_len);
  466. cs_set_control(mac, cs, hdr, control->flags);
  467. packet_length = frag_len + sizeof(struct zd_ctrlset) + 10;
  468. ZD_ASSERT(packet_length <= 0xffff);
  469. /* ZD1211B: Computing the length difference this way, gives us
  470. * flexibility to compute the packet length.
  471. */
  472. cs->packet_length = cpu_to_le16(zd_chip_is_zd1211b(&mac->chip) ?
  473. packet_length - frag_len : packet_length);
  474. /*
  475. * CURRENT LENGTH:
  476. * - transmit frame length in microseconds
  477. * - seems to be derived from frame length
  478. * - see Cal_Us_Service() in zdinlinef.h
  479. * - if macp->bTxBurstEnable is enabled, then multiply by 4
  480. * - bTxBurstEnable is never set in the vendor driver
  481. *
  482. * SERVICE:
  483. * - "for PLCP configuration"
  484. * - always 0 except in some situations at 802.11b 11M
  485. * - see line 53 of zdinlinef.h
  486. */
  487. cs->service = 0;
  488. r = zd_calc_tx_length_us(&cs->service, ZD_RATE(cs->modulation),
  489. le16_to_cpu(cs->tx_length));
  490. if (r < 0)
  491. return r;
  492. cs->current_length = cpu_to_le16(r);
  493. cs->next_frame_length = 0;
  494. return 0;
  495. }
  496. /**
  497. * zd_op_tx - transmits a network frame to the device
  498. *
  499. * @dev: mac80211 hardware device
  500. * @skb: socket buffer
  501. * @control: the control structure
  502. *
  503. * This function transmit an IEEE 802.11 network frame to the device. The
  504. * control block of the skbuff will be initialized. If necessary the incoming
  505. * mac80211 queues will be stopped.
  506. */
  507. static int zd_op_tx(struct ieee80211_hw *hw, struct sk_buff *skb,
  508. struct ieee80211_tx_control *control)
  509. {
  510. struct zd_mac *mac = zd_hw_mac(hw);
  511. int r;
  512. r = fill_ctrlset(mac, skb, control);
  513. if (r)
  514. return r;
  515. r = init_tx_skb_control_block(skb, hw, control);
  516. if (r)
  517. return r;
  518. r = zd_usb_tx(&mac->chip.usb, skb);
  519. if (r) {
  520. clear_tx_skb_control_block(skb);
  521. return r;
  522. }
  523. return 0;
  524. }
  525. /**
  526. * filter_ack - filters incoming packets for acknowledgements
  527. * @dev: the mac80211 device
  528. * @rx_hdr: received header
  529. * @stats: the status for the received packet
  530. *
  531. * This functions looks for ACK packets and tries to match them with the
  532. * frames in the tx queue. If a match is found the frame will be dequeued and
  533. * the upper layers is informed about the successful transmission. If
  534. * mac80211 queues have been stopped and the number of frames still to be
  535. * transmitted is low the queues will be opened again.
  536. *
  537. * Returns 1 if the frame was an ACK, 0 if it was ignored.
  538. */
  539. static int filter_ack(struct ieee80211_hw *hw, struct ieee80211_hdr *rx_hdr,
  540. struct ieee80211_rx_status *stats)
  541. {
  542. u16 fc = le16_to_cpu(rx_hdr->frame_control);
  543. struct sk_buff *skb;
  544. struct sk_buff_head *q;
  545. unsigned long flags;
  546. if ((fc & (IEEE80211_FCTL_FTYPE | IEEE80211_FCTL_STYPE)) !=
  547. (IEEE80211_FTYPE_CTL | IEEE80211_STYPE_ACK))
  548. return 0;
  549. q = &zd_hw_mac(hw)->ack_wait_queue;
  550. spin_lock_irqsave(&q->lock, flags);
  551. for (skb = q->next; skb != (struct sk_buff *)q; skb = skb->next) {
  552. struct ieee80211_hdr *tx_hdr;
  553. tx_hdr = (struct ieee80211_hdr *)skb->data;
  554. if (likely(!compare_ether_addr(tx_hdr->addr2, rx_hdr->addr1)))
  555. {
  556. struct ieee80211_tx_status status;
  557. memset(&status, 0, sizeof(status));
  558. status.flags = IEEE80211_TX_STATUS_ACK;
  559. status.ack_signal = stats->ssi;
  560. __skb_unlink(skb, q);
  561. tx_status(hw, skb, &status, 1);
  562. goto out;
  563. }
  564. }
  565. out:
  566. spin_unlock_irqrestore(&q->lock, flags);
  567. return 1;
  568. }
  569. int zd_mac_rx(struct ieee80211_hw *hw, const u8 *buffer, unsigned int length)
  570. {
  571. struct zd_mac *mac = zd_hw_mac(hw);
  572. struct ieee80211_rx_status stats;
  573. const struct rx_status *status;
  574. struct sk_buff *skb;
  575. int bad_frame = 0;
  576. u16 fc;
  577. bool is_qos, is_4addr, need_padding;
  578. int i;
  579. u8 rate;
  580. if (length < ZD_PLCP_HEADER_SIZE + 10 /* IEEE80211_1ADDR_LEN */ +
  581. FCS_LEN + sizeof(struct rx_status))
  582. return -EINVAL;
  583. memset(&stats, 0, sizeof(stats));
  584. /* Note about pass_failed_fcs and pass_ctrl access below:
  585. * mac locking intentionally omitted here, as this is the only unlocked
  586. * reader and the only writer is configure_filter. Plus, if there were
  587. * any races accessing these variables, it wouldn't really matter.
  588. * If mac80211 ever provides a way for us to access filter flags
  589. * from outside configure_filter, we could improve on this. Also, this
  590. * situation may change once we implement some kind of DMA-into-skb
  591. * RX path. */
  592. /* Caller has to ensure that length >= sizeof(struct rx_status). */
  593. status = (struct rx_status *)
  594. (buffer + (length - sizeof(struct rx_status)));
  595. if (status->frame_status & ZD_RX_ERROR) {
  596. if (mac->pass_failed_fcs &&
  597. (status->frame_status & ZD_RX_CRC32_ERROR)) {
  598. stats.flag |= RX_FLAG_FAILED_FCS_CRC;
  599. bad_frame = 1;
  600. } else {
  601. return -EINVAL;
  602. }
  603. }
  604. stats.freq = zd_channels[_zd_chip_get_channel(&mac->chip) - 1].center_freq;
  605. stats.band = IEEE80211_BAND_2GHZ;
  606. stats.ssi = status->signal_strength;
  607. stats.signal = zd_rx_qual_percent(buffer,
  608. length - sizeof(struct rx_status),
  609. status);
  610. rate = zd_rx_rate(buffer, status);
  611. /* todo: return index in the big switches in zd_rx_rate instead */
  612. for (i = 0; i < mac->band.n_bitrates; i++)
  613. if (rate == mac->band.bitrates[i].hw_value)
  614. stats.rate_idx = i;
  615. length -= ZD_PLCP_HEADER_SIZE + sizeof(struct rx_status);
  616. buffer += ZD_PLCP_HEADER_SIZE;
  617. /* Except for bad frames, filter each frame to see if it is an ACK, in
  618. * which case our internal TX tracking is updated. Normally we then
  619. * bail here as there's no need to pass ACKs on up to the stack, but
  620. * there is also the case where the stack has requested us to pass
  621. * control frames on up (pass_ctrl) which we must consider. */
  622. if (!bad_frame &&
  623. filter_ack(hw, (struct ieee80211_hdr *)buffer, &stats)
  624. && !mac->pass_ctrl)
  625. return 0;
  626. fc = le16_to_cpu(*((__le16 *) buffer));
  627. is_qos = ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA) &&
  628. (fc & IEEE80211_STYPE_QOS_DATA);
  629. is_4addr = (fc & (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) ==
  630. (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS);
  631. need_padding = is_qos ^ is_4addr;
  632. skb = dev_alloc_skb(length + (need_padding ? 2 : 0));
  633. if (skb == NULL)
  634. return -ENOMEM;
  635. if (need_padding) {
  636. /* Make sure the the payload data is 4 byte aligned. */
  637. skb_reserve(skb, 2);
  638. }
  639. memcpy(skb_put(skb, length), buffer, length);
  640. ieee80211_rx_irqsafe(hw, skb, &stats);
  641. return 0;
  642. }
  643. static int zd_op_add_interface(struct ieee80211_hw *hw,
  644. struct ieee80211_if_init_conf *conf)
  645. {
  646. struct zd_mac *mac = zd_hw_mac(hw);
  647. /* using IEEE80211_IF_TYPE_INVALID to indicate no mode selected */
  648. if (mac->type != IEEE80211_IF_TYPE_INVALID)
  649. return -EOPNOTSUPP;
  650. switch (conf->type) {
  651. case IEEE80211_IF_TYPE_MNTR:
  652. case IEEE80211_IF_TYPE_MESH_POINT:
  653. case IEEE80211_IF_TYPE_STA:
  654. mac->type = conf->type;
  655. break;
  656. default:
  657. return -EOPNOTSUPP;
  658. }
  659. return zd_write_mac_addr(&mac->chip, conf->mac_addr);
  660. }
  661. static void zd_op_remove_interface(struct ieee80211_hw *hw,
  662. struct ieee80211_if_init_conf *conf)
  663. {
  664. struct zd_mac *mac = zd_hw_mac(hw);
  665. mac->type = IEEE80211_IF_TYPE_INVALID;
  666. zd_set_beacon_interval(&mac->chip, 0);
  667. zd_write_mac_addr(&mac->chip, NULL);
  668. }
  669. static int zd_op_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf)
  670. {
  671. struct zd_mac *mac = zd_hw_mac(hw);
  672. return zd_chip_set_channel(&mac->chip, conf->channel->hw_value);
  673. }
  674. static int zd_op_config_interface(struct ieee80211_hw *hw,
  675. struct ieee80211_vif *vif,
  676. struct ieee80211_if_conf *conf)
  677. {
  678. struct zd_mac *mac = zd_hw_mac(hw);
  679. int associated;
  680. if (mac->type == IEEE80211_IF_TYPE_MESH_POINT) {
  681. associated = true;
  682. if (conf->beacon) {
  683. zd_mac_config_beacon(hw, conf->beacon);
  684. kfree_skb(conf->beacon);
  685. zd_set_beacon_interval(&mac->chip, BCN_MODE_IBSS |
  686. hw->conf.beacon_int);
  687. }
  688. } else
  689. associated = is_valid_ether_addr(conf->bssid);
  690. spin_lock_irq(&mac->lock);
  691. mac->associated = associated;
  692. spin_unlock_irq(&mac->lock);
  693. /* TODO: do hardware bssid filtering */
  694. return 0;
  695. }
  696. void zd_process_intr(struct work_struct *work)
  697. {
  698. u16 int_status;
  699. struct zd_mac *mac = container_of(work, struct zd_mac, process_intr);
  700. int_status = le16_to_cpu(*(__le16 *)(mac->intr_buffer+4));
  701. if (int_status & INT_CFG_NEXT_BCN) {
  702. if (net_ratelimit())
  703. dev_dbg_f(zd_mac_dev(mac), "INT_CFG_NEXT_BCN\n");
  704. } else
  705. dev_dbg_f(zd_mac_dev(mac), "Unsupported interrupt\n");
  706. zd_chip_enable_hwint(&mac->chip);
  707. }
  708. static void set_multicast_hash_handler(struct work_struct *work)
  709. {
  710. struct zd_mac *mac =
  711. container_of(work, struct zd_mac, set_multicast_hash_work);
  712. struct zd_mc_hash hash;
  713. spin_lock_irq(&mac->lock);
  714. hash = mac->multicast_hash;
  715. spin_unlock_irq(&mac->lock);
  716. zd_chip_set_multicast_hash(&mac->chip, &hash);
  717. }
  718. static void set_rx_filter_handler(struct work_struct *work)
  719. {
  720. struct zd_mac *mac =
  721. container_of(work, struct zd_mac, set_rx_filter_work);
  722. int r;
  723. dev_dbg_f(zd_mac_dev(mac), "\n");
  724. r = set_rx_filter(mac);
  725. if (r)
  726. dev_err(zd_mac_dev(mac), "set_rx_filter_handler error %d\n", r);
  727. }
  728. #define SUPPORTED_FIF_FLAGS \
  729. (FIF_PROMISC_IN_BSS | FIF_ALLMULTI | FIF_FCSFAIL | FIF_CONTROL | \
  730. FIF_OTHER_BSS | FIF_BCN_PRBRESP_PROMISC)
  731. static void zd_op_configure_filter(struct ieee80211_hw *hw,
  732. unsigned int changed_flags,
  733. unsigned int *new_flags,
  734. int mc_count, struct dev_mc_list *mclist)
  735. {
  736. struct zd_mc_hash hash;
  737. struct zd_mac *mac = zd_hw_mac(hw);
  738. unsigned long flags;
  739. int i;
  740. /* Only deal with supported flags */
  741. changed_flags &= SUPPORTED_FIF_FLAGS;
  742. *new_flags &= SUPPORTED_FIF_FLAGS;
  743. /* changed_flags is always populated but this driver
  744. * doesn't support all FIF flags so its possible we don't
  745. * need to do anything */
  746. if (!changed_flags)
  747. return;
  748. if (*new_flags & (FIF_PROMISC_IN_BSS | FIF_ALLMULTI)) {
  749. zd_mc_add_all(&hash);
  750. } else {
  751. DECLARE_MAC_BUF(macbuf);
  752. zd_mc_clear(&hash);
  753. for (i = 0; i < mc_count; i++) {
  754. if (!mclist)
  755. break;
  756. dev_dbg_f(zd_mac_dev(mac), "mc addr %s\n",
  757. print_mac(macbuf, mclist->dmi_addr));
  758. zd_mc_add_addr(&hash, mclist->dmi_addr);
  759. mclist = mclist->next;
  760. }
  761. }
  762. spin_lock_irqsave(&mac->lock, flags);
  763. mac->pass_failed_fcs = !!(*new_flags & FIF_FCSFAIL);
  764. mac->pass_ctrl = !!(*new_flags & FIF_CONTROL);
  765. mac->multicast_hash = hash;
  766. spin_unlock_irqrestore(&mac->lock, flags);
  767. queue_work(zd_workqueue, &mac->set_multicast_hash_work);
  768. if (changed_flags & FIF_CONTROL)
  769. queue_work(zd_workqueue, &mac->set_rx_filter_work);
  770. /* no handling required for FIF_OTHER_BSS as we don't currently
  771. * do BSSID filtering */
  772. /* FIXME: in future it would be nice to enable the probe response
  773. * filter (so that the driver doesn't see them) until
  774. * FIF_BCN_PRBRESP_PROMISC is set. however due to atomicity here, we'd
  775. * have to schedule work to enable prbresp reception, which might
  776. * happen too late. For now we'll just listen and forward them all the
  777. * time. */
  778. }
  779. static void set_rts_cts_work(struct work_struct *work)
  780. {
  781. struct zd_mac *mac =
  782. container_of(work, struct zd_mac, set_rts_cts_work);
  783. unsigned long flags;
  784. unsigned int short_preamble;
  785. mutex_lock(&mac->chip.mutex);
  786. spin_lock_irqsave(&mac->lock, flags);
  787. mac->updating_rts_rate = 0;
  788. short_preamble = mac->short_preamble;
  789. spin_unlock_irqrestore(&mac->lock, flags);
  790. zd_chip_set_rts_cts_rate_locked(&mac->chip, short_preamble);
  791. mutex_unlock(&mac->chip.mutex);
  792. }
  793. static void zd_op_bss_info_changed(struct ieee80211_hw *hw,
  794. struct ieee80211_vif *vif,
  795. struct ieee80211_bss_conf *bss_conf,
  796. u32 changes)
  797. {
  798. struct zd_mac *mac = zd_hw_mac(hw);
  799. unsigned long flags;
  800. dev_dbg_f(zd_mac_dev(mac), "changes: %x\n", changes);
  801. if (changes & BSS_CHANGED_ERP_PREAMBLE) {
  802. spin_lock_irqsave(&mac->lock, flags);
  803. mac->short_preamble = bss_conf->use_short_preamble;
  804. if (!mac->updating_rts_rate) {
  805. mac->updating_rts_rate = 1;
  806. /* FIXME: should disable TX here, until work has
  807. * completed and RTS_CTS reg is updated */
  808. queue_work(zd_workqueue, &mac->set_rts_cts_work);
  809. }
  810. spin_unlock_irqrestore(&mac->lock, flags);
  811. }
  812. }
  813. static const struct ieee80211_ops zd_ops = {
  814. .tx = zd_op_tx,
  815. .start = zd_op_start,
  816. .stop = zd_op_stop,
  817. .add_interface = zd_op_add_interface,
  818. .remove_interface = zd_op_remove_interface,
  819. .config = zd_op_config,
  820. .config_interface = zd_op_config_interface,
  821. .configure_filter = zd_op_configure_filter,
  822. .bss_info_changed = zd_op_bss_info_changed,
  823. };
  824. struct ieee80211_hw *zd_mac_alloc_hw(struct usb_interface *intf)
  825. {
  826. struct zd_mac *mac;
  827. struct ieee80211_hw *hw;
  828. hw = ieee80211_alloc_hw(sizeof(struct zd_mac), &zd_ops);
  829. if (!hw) {
  830. dev_dbg_f(&intf->dev, "out of memory\n");
  831. return NULL;
  832. }
  833. mac = zd_hw_mac(hw);
  834. memset(mac, 0, sizeof(*mac));
  835. spin_lock_init(&mac->lock);
  836. mac->hw = hw;
  837. mac->type = IEEE80211_IF_TYPE_INVALID;
  838. memcpy(mac->channels, zd_channels, sizeof(zd_channels));
  839. memcpy(mac->rates, zd_rates, sizeof(zd_rates));
  840. mac->band.n_bitrates = ARRAY_SIZE(zd_rates);
  841. mac->band.bitrates = mac->rates;
  842. mac->band.n_channels = ARRAY_SIZE(zd_channels);
  843. mac->band.channels = mac->channels;
  844. hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &mac->band;
  845. hw->flags = IEEE80211_HW_RX_INCLUDES_FCS |
  846. IEEE80211_HW_HOST_GEN_BEACON_TEMPLATE;
  847. hw->max_rssi = 100;
  848. hw->max_signal = 100;
  849. hw->queues = 1;
  850. hw->extra_tx_headroom = sizeof(struct zd_ctrlset);
  851. skb_queue_head_init(&mac->ack_wait_queue);
  852. zd_chip_init(&mac->chip, hw, intf);
  853. housekeeping_init(mac);
  854. INIT_WORK(&mac->set_multicast_hash_work, set_multicast_hash_handler);
  855. INIT_WORK(&mac->set_rts_cts_work, set_rts_cts_work);
  856. INIT_WORK(&mac->set_rx_filter_work, set_rx_filter_handler);
  857. INIT_WORK(&mac->process_intr, zd_process_intr);
  858. SET_IEEE80211_DEV(hw, &intf->dev);
  859. return hw;
  860. }
  861. #define LINK_LED_WORK_DELAY HZ
  862. static void link_led_handler(struct work_struct *work)
  863. {
  864. struct zd_mac *mac =
  865. container_of(work, struct zd_mac, housekeeping.link_led_work.work);
  866. struct zd_chip *chip = &mac->chip;
  867. int is_associated;
  868. int r;
  869. spin_lock_irq(&mac->lock);
  870. is_associated = mac->associated;
  871. spin_unlock_irq(&mac->lock);
  872. r = zd_chip_control_leds(chip,
  873. is_associated ? LED_ASSOCIATED : LED_SCANNING);
  874. if (r)
  875. dev_dbg_f(zd_mac_dev(mac), "zd_chip_control_leds error %d\n", r);
  876. queue_delayed_work(zd_workqueue, &mac->housekeeping.link_led_work,
  877. LINK_LED_WORK_DELAY);
  878. }
  879. static void housekeeping_init(struct zd_mac *mac)
  880. {
  881. INIT_DELAYED_WORK(&mac->housekeeping.link_led_work, link_led_handler);
  882. }
  883. static void housekeeping_enable(struct zd_mac *mac)
  884. {
  885. dev_dbg_f(zd_mac_dev(mac), "\n");
  886. queue_delayed_work(zd_workqueue, &mac->housekeeping.link_led_work,
  887. 0);
  888. }
  889. static void housekeeping_disable(struct zd_mac *mac)
  890. {
  891. dev_dbg_f(zd_mac_dev(mac), "\n");
  892. cancel_rearming_delayed_workqueue(zd_workqueue,
  893. &mac->housekeeping.link_led_work);
  894. zd_chip_control_leds(&mac->chip, LED_OFF);
  895. }