zd_mac.c 27 KB

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