zd_mac.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055
  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. goto disable_int;
  157. r = regulatory_hint(hw->wiphy, alpha2);
  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. goto fail;
  503. info->rate_driver_data[0] = hw;
  504. r = zd_usb_tx(&mac->chip.usb, skb);
  505. if (r)
  506. goto fail;
  507. return 0;
  508. fail:
  509. dev_kfree_skb(skb);
  510. return 0;
  511. }
  512. /**
  513. * filter_ack - filters incoming packets for acknowledgements
  514. * @dev: the mac80211 device
  515. * @rx_hdr: received header
  516. * @stats: the status for the received packet
  517. *
  518. * This functions looks for ACK packets and tries to match them with the
  519. * frames in the tx queue. If a match is found the frame will be dequeued and
  520. * the upper layers is informed about the successful transmission. If
  521. * mac80211 queues have been stopped and the number of frames still to be
  522. * transmitted is low the queues will be opened again.
  523. *
  524. * Returns 1 if the frame was an ACK, 0 if it was ignored.
  525. */
  526. static int filter_ack(struct ieee80211_hw *hw, struct ieee80211_hdr *rx_hdr,
  527. struct ieee80211_rx_status *stats)
  528. {
  529. struct sk_buff *skb;
  530. struct sk_buff_head *q;
  531. unsigned long flags;
  532. if (!ieee80211_is_ack(rx_hdr->frame_control))
  533. return 0;
  534. q = &zd_hw_mac(hw)->ack_wait_queue;
  535. spin_lock_irqsave(&q->lock, flags);
  536. skb_queue_walk(q, skb) {
  537. struct ieee80211_hdr *tx_hdr;
  538. tx_hdr = (struct ieee80211_hdr *)skb->data;
  539. if (likely(!memcmp(tx_hdr->addr2, rx_hdr->addr1, ETH_ALEN)))
  540. {
  541. __skb_unlink(skb, q);
  542. tx_status(hw, skb, stats->signal, 1);
  543. goto out;
  544. }
  545. }
  546. out:
  547. spin_unlock_irqrestore(&q->lock, flags);
  548. return 1;
  549. }
  550. int zd_mac_rx(struct ieee80211_hw *hw, const u8 *buffer, unsigned int length)
  551. {
  552. struct zd_mac *mac = zd_hw_mac(hw);
  553. struct ieee80211_rx_status stats;
  554. const struct rx_status *status;
  555. struct sk_buff *skb;
  556. int bad_frame = 0;
  557. __le16 fc;
  558. int need_padding;
  559. int i;
  560. u8 rate;
  561. if (length < ZD_PLCP_HEADER_SIZE + 10 /* IEEE80211_1ADDR_LEN */ +
  562. FCS_LEN + sizeof(struct rx_status))
  563. return -EINVAL;
  564. memset(&stats, 0, sizeof(stats));
  565. /* Note about pass_failed_fcs and pass_ctrl access below:
  566. * mac locking intentionally omitted here, as this is the only unlocked
  567. * reader and the only writer is configure_filter. Plus, if there were
  568. * any races accessing these variables, it wouldn't really matter.
  569. * If mac80211 ever provides a way for us to access filter flags
  570. * from outside configure_filter, we could improve on this. Also, this
  571. * situation may change once we implement some kind of DMA-into-skb
  572. * RX path. */
  573. /* Caller has to ensure that length >= sizeof(struct rx_status). */
  574. status = (struct rx_status *)
  575. (buffer + (length - sizeof(struct rx_status)));
  576. if (status->frame_status & ZD_RX_ERROR) {
  577. if (mac->pass_failed_fcs &&
  578. (status->frame_status & ZD_RX_CRC32_ERROR)) {
  579. stats.flag |= RX_FLAG_FAILED_FCS_CRC;
  580. bad_frame = 1;
  581. } else {
  582. return -EINVAL;
  583. }
  584. }
  585. stats.freq = zd_channels[_zd_chip_get_channel(&mac->chip) - 1].center_freq;
  586. stats.band = IEEE80211_BAND_2GHZ;
  587. stats.signal = status->signal_strength;
  588. stats.qual = zd_rx_qual_percent(buffer,
  589. length - sizeof(struct rx_status),
  590. status);
  591. rate = zd_rx_rate(buffer, status);
  592. /* todo: return index in the big switches in zd_rx_rate instead */
  593. for (i = 0; i < mac->band.n_bitrates; i++)
  594. if (rate == mac->band.bitrates[i].hw_value)
  595. stats.rate_idx = i;
  596. length -= ZD_PLCP_HEADER_SIZE + sizeof(struct rx_status);
  597. buffer += ZD_PLCP_HEADER_SIZE;
  598. /* Except for bad frames, filter each frame to see if it is an ACK, in
  599. * which case our internal TX tracking is updated. Normally we then
  600. * bail here as there's no need to pass ACKs on up to the stack, but
  601. * there is also the case where the stack has requested us to pass
  602. * control frames on up (pass_ctrl) which we must consider. */
  603. if (!bad_frame &&
  604. filter_ack(hw, (struct ieee80211_hdr *)buffer, &stats)
  605. && !mac->pass_ctrl)
  606. return 0;
  607. fc = *(__le16 *)buffer;
  608. need_padding = ieee80211_is_data_qos(fc) ^ ieee80211_has_a4(fc);
  609. skb = dev_alloc_skb(length + (need_padding ? 2 : 0));
  610. if (skb == NULL)
  611. return -ENOMEM;
  612. if (need_padding) {
  613. /* Make sure the the payload data is 4 byte aligned. */
  614. skb_reserve(skb, 2);
  615. }
  616. memcpy(skb_put(skb, length), buffer, length);
  617. ieee80211_rx_irqsafe(hw, skb, &stats);
  618. return 0;
  619. }
  620. static int zd_op_add_interface(struct ieee80211_hw *hw,
  621. struct ieee80211_if_init_conf *conf)
  622. {
  623. struct zd_mac *mac = zd_hw_mac(hw);
  624. /* using NL80211_IFTYPE_UNSPECIFIED to indicate no mode selected */
  625. if (mac->type != NL80211_IFTYPE_UNSPECIFIED)
  626. return -EOPNOTSUPP;
  627. switch (conf->type) {
  628. case NL80211_IFTYPE_MONITOR:
  629. case NL80211_IFTYPE_MESH_POINT:
  630. case NL80211_IFTYPE_STATION:
  631. case NL80211_IFTYPE_ADHOC:
  632. mac->type = conf->type;
  633. break;
  634. default:
  635. return -EOPNOTSUPP;
  636. }
  637. return zd_write_mac_addr(&mac->chip, conf->mac_addr);
  638. }
  639. static void zd_op_remove_interface(struct ieee80211_hw *hw,
  640. struct ieee80211_if_init_conf *conf)
  641. {
  642. struct zd_mac *mac = zd_hw_mac(hw);
  643. mac->type = NL80211_IFTYPE_UNSPECIFIED;
  644. zd_set_beacon_interval(&mac->chip, 0);
  645. zd_write_mac_addr(&mac->chip, NULL);
  646. }
  647. static int zd_op_config(struct ieee80211_hw *hw, u32 changed)
  648. {
  649. struct zd_mac *mac = zd_hw_mac(hw);
  650. struct ieee80211_conf *conf = &hw->conf;
  651. return zd_chip_set_channel(&mac->chip, conf->channel->hw_value);
  652. }
  653. static int zd_op_config_interface(struct ieee80211_hw *hw,
  654. struct ieee80211_vif *vif,
  655. struct ieee80211_if_conf *conf)
  656. {
  657. struct zd_mac *mac = zd_hw_mac(hw);
  658. int associated;
  659. int r;
  660. if (mac->type == NL80211_IFTYPE_MESH_POINT ||
  661. mac->type == NL80211_IFTYPE_ADHOC) {
  662. associated = true;
  663. if (conf->changed & IEEE80211_IFCC_BEACON) {
  664. struct sk_buff *beacon = ieee80211_beacon_get(hw, vif);
  665. if (!beacon)
  666. return -ENOMEM;
  667. r = zd_mac_config_beacon(hw, beacon);
  668. kfree_skb(beacon);
  669. if (r < 0)
  670. return r;
  671. }
  672. if (conf->changed & IEEE80211_IFCC_BEACON_ENABLED) {
  673. u32 interval;
  674. if (conf->enable_beacon)
  675. interval = BCN_MODE_IBSS | hw->conf.beacon_int;
  676. else
  677. interval = 0;
  678. r = zd_set_beacon_interval(&mac->chip, interval);
  679. if (r < 0)
  680. return r;
  681. }
  682. } else
  683. associated = is_valid_ether_addr(conf->bssid);
  684. spin_lock_irq(&mac->lock);
  685. mac->associated = associated;
  686. spin_unlock_irq(&mac->lock);
  687. /* TODO: do hardware bssid filtering */
  688. return 0;
  689. }
  690. static void zd_process_intr(struct work_struct *work)
  691. {
  692. u16 int_status;
  693. struct zd_mac *mac = container_of(work, struct zd_mac, process_intr);
  694. int_status = le16_to_cpu(*(__le16 *)(mac->intr_buffer+4));
  695. if (int_status & INT_CFG_NEXT_BCN)
  696. dev_dbg_f_limit(zd_mac_dev(mac), "INT_CFG_NEXT_BCN\n");
  697. else
  698. dev_dbg_f(zd_mac_dev(mac), "Unsupported interrupt\n");
  699. zd_chip_enable_hwint(&mac->chip);
  700. }
  701. static void set_multicast_hash_handler(struct work_struct *work)
  702. {
  703. struct zd_mac *mac =
  704. container_of(work, struct zd_mac, set_multicast_hash_work);
  705. struct zd_mc_hash hash;
  706. spin_lock_irq(&mac->lock);
  707. hash = mac->multicast_hash;
  708. spin_unlock_irq(&mac->lock);
  709. zd_chip_set_multicast_hash(&mac->chip, &hash);
  710. }
  711. static void set_rx_filter_handler(struct work_struct *work)
  712. {
  713. struct zd_mac *mac =
  714. container_of(work, struct zd_mac, set_rx_filter_work);
  715. int r;
  716. dev_dbg_f(zd_mac_dev(mac), "\n");
  717. r = set_rx_filter(mac);
  718. if (r)
  719. dev_err(zd_mac_dev(mac), "set_rx_filter_handler error %d\n", r);
  720. }
  721. #define SUPPORTED_FIF_FLAGS \
  722. (FIF_PROMISC_IN_BSS | FIF_ALLMULTI | FIF_FCSFAIL | FIF_CONTROL | \
  723. FIF_OTHER_BSS | FIF_BCN_PRBRESP_PROMISC)
  724. static void zd_op_configure_filter(struct ieee80211_hw *hw,
  725. unsigned int changed_flags,
  726. unsigned int *new_flags,
  727. int mc_count, struct dev_mc_list *mclist)
  728. {
  729. struct zd_mc_hash hash;
  730. struct zd_mac *mac = zd_hw_mac(hw);
  731. unsigned long flags;
  732. int i;
  733. /* Only deal with supported flags */
  734. changed_flags &= SUPPORTED_FIF_FLAGS;
  735. *new_flags &= SUPPORTED_FIF_FLAGS;
  736. /* changed_flags is always populated but this driver
  737. * doesn't support all FIF flags so its possible we don't
  738. * need to do anything */
  739. if (!changed_flags)
  740. return;
  741. if (*new_flags & (FIF_PROMISC_IN_BSS | FIF_ALLMULTI)) {
  742. zd_mc_add_all(&hash);
  743. } else {
  744. zd_mc_clear(&hash);
  745. for (i = 0; i < mc_count; i++) {
  746. if (!mclist)
  747. break;
  748. dev_dbg_f(zd_mac_dev(mac), "mc addr %pM\n",
  749. mclist->dmi_addr);
  750. zd_mc_add_addr(&hash, mclist->dmi_addr);
  751. mclist = mclist->next;
  752. }
  753. }
  754. spin_lock_irqsave(&mac->lock, flags);
  755. mac->pass_failed_fcs = !!(*new_flags & FIF_FCSFAIL);
  756. mac->pass_ctrl = !!(*new_flags & FIF_CONTROL);
  757. mac->multicast_hash = hash;
  758. spin_unlock_irqrestore(&mac->lock, flags);
  759. queue_work(zd_workqueue, &mac->set_multicast_hash_work);
  760. if (changed_flags & FIF_CONTROL)
  761. queue_work(zd_workqueue, &mac->set_rx_filter_work);
  762. /* no handling required for FIF_OTHER_BSS as we don't currently
  763. * do BSSID filtering */
  764. /* FIXME: in future it would be nice to enable the probe response
  765. * filter (so that the driver doesn't see them) until
  766. * FIF_BCN_PRBRESP_PROMISC is set. however due to atomicity here, we'd
  767. * have to schedule work to enable prbresp reception, which might
  768. * happen too late. For now we'll just listen and forward them all the
  769. * time. */
  770. }
  771. static void set_rts_cts_work(struct work_struct *work)
  772. {
  773. struct zd_mac *mac =
  774. container_of(work, struct zd_mac, set_rts_cts_work);
  775. unsigned long flags;
  776. unsigned int short_preamble;
  777. mutex_lock(&mac->chip.mutex);
  778. spin_lock_irqsave(&mac->lock, flags);
  779. mac->updating_rts_rate = 0;
  780. short_preamble = mac->short_preamble;
  781. spin_unlock_irqrestore(&mac->lock, flags);
  782. zd_chip_set_rts_cts_rate_locked(&mac->chip, short_preamble);
  783. mutex_unlock(&mac->chip.mutex);
  784. }
  785. static void zd_op_bss_info_changed(struct ieee80211_hw *hw,
  786. struct ieee80211_vif *vif,
  787. struct ieee80211_bss_conf *bss_conf,
  788. u32 changes)
  789. {
  790. struct zd_mac *mac = zd_hw_mac(hw);
  791. unsigned long flags;
  792. dev_dbg_f(zd_mac_dev(mac), "changes: %x\n", changes);
  793. if (changes & BSS_CHANGED_ERP_PREAMBLE) {
  794. spin_lock_irqsave(&mac->lock, flags);
  795. mac->short_preamble = bss_conf->use_short_preamble;
  796. if (!mac->updating_rts_rate) {
  797. mac->updating_rts_rate = 1;
  798. /* FIXME: should disable TX here, until work has
  799. * completed and RTS_CTS reg is updated */
  800. queue_work(zd_workqueue, &mac->set_rts_cts_work);
  801. }
  802. spin_unlock_irqrestore(&mac->lock, flags);
  803. }
  804. }
  805. static u64 zd_op_get_tsf(struct ieee80211_hw *hw)
  806. {
  807. struct zd_mac *mac = zd_hw_mac(hw);
  808. return zd_chip_get_tsf(&mac->chip);
  809. }
  810. static const struct ieee80211_ops zd_ops = {
  811. .tx = zd_op_tx,
  812. .start = zd_op_start,
  813. .stop = zd_op_stop,
  814. .add_interface = zd_op_add_interface,
  815. .remove_interface = zd_op_remove_interface,
  816. .config = zd_op_config,
  817. .config_interface = zd_op_config_interface,
  818. .configure_filter = zd_op_configure_filter,
  819. .bss_info_changed = zd_op_bss_info_changed,
  820. .get_tsf = zd_op_get_tsf,
  821. };
  822. struct ieee80211_hw *zd_mac_alloc_hw(struct usb_interface *intf)
  823. {
  824. struct zd_mac *mac;
  825. struct ieee80211_hw *hw;
  826. hw = ieee80211_alloc_hw(sizeof(struct zd_mac), &zd_ops);
  827. if (!hw) {
  828. dev_dbg_f(&intf->dev, "out of memory\n");
  829. return NULL;
  830. }
  831. mac = zd_hw_mac(hw);
  832. memset(mac, 0, sizeof(*mac));
  833. spin_lock_init(&mac->lock);
  834. mac->hw = hw;
  835. mac->type = NL80211_IFTYPE_UNSPECIFIED;
  836. memcpy(mac->channels, zd_channels, sizeof(zd_channels));
  837. memcpy(mac->rates, zd_rates, sizeof(zd_rates));
  838. mac->band.n_bitrates = ARRAY_SIZE(zd_rates);
  839. mac->band.bitrates = mac->rates;
  840. mac->band.n_channels = ARRAY_SIZE(zd_channels);
  841. mac->band.channels = mac->channels;
  842. hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &mac->band;
  843. hw->flags = IEEE80211_HW_RX_INCLUDES_FCS |
  844. IEEE80211_HW_SIGNAL_UNSPEC;
  845. hw->wiphy->interface_modes =
  846. BIT(NL80211_IFTYPE_MESH_POINT) |
  847. BIT(NL80211_IFTYPE_STATION) |
  848. BIT(NL80211_IFTYPE_ADHOC);
  849. hw->max_signal = 100;
  850. hw->queues = 1;
  851. hw->extra_tx_headroom = sizeof(struct zd_ctrlset);
  852. skb_queue_head_init(&mac->ack_wait_queue);
  853. zd_chip_init(&mac->chip, hw, intf);
  854. housekeeping_init(mac);
  855. INIT_WORK(&mac->set_multicast_hash_work, set_multicast_hash_handler);
  856. INIT_WORK(&mac->set_rts_cts_work, set_rts_cts_work);
  857. INIT_WORK(&mac->set_rx_filter_work, set_rx_filter_handler);
  858. INIT_WORK(&mac->process_intr, zd_process_intr);
  859. SET_IEEE80211_DEV(hw, &intf->dev);
  860. return hw;
  861. }
  862. #define LINK_LED_WORK_DELAY HZ
  863. static void link_led_handler(struct work_struct *work)
  864. {
  865. struct zd_mac *mac =
  866. container_of(work, struct zd_mac, housekeeping.link_led_work.work);
  867. struct zd_chip *chip = &mac->chip;
  868. int is_associated;
  869. int r;
  870. spin_lock_irq(&mac->lock);
  871. is_associated = mac->associated;
  872. spin_unlock_irq(&mac->lock);
  873. r = zd_chip_control_leds(chip,
  874. is_associated ? LED_ASSOCIATED : LED_SCANNING);
  875. if (r)
  876. dev_dbg_f(zd_mac_dev(mac), "zd_chip_control_leds error %d\n", r);
  877. queue_delayed_work(zd_workqueue, &mac->housekeeping.link_led_work,
  878. LINK_LED_WORK_DELAY);
  879. }
  880. static void housekeeping_init(struct zd_mac *mac)
  881. {
  882. INIT_DELAYED_WORK(&mac->housekeeping.link_led_work, link_led_handler);
  883. }
  884. static void housekeeping_enable(struct zd_mac *mac)
  885. {
  886. dev_dbg_f(zd_mac_dev(mac), "\n");
  887. queue_delayed_work(zd_workqueue, &mac->housekeeping.link_led_work,
  888. 0);
  889. }
  890. static void housekeeping_disable(struct zd_mac *mac)
  891. {
  892. dev_dbg_f(zd_mac_dev(mac), "\n");
  893. cancel_rearming_delayed_workqueue(zd_workqueue,
  894. &mac->housekeeping.link_led_work);
  895. zd_chip_control_leds(&mac->chip, LED_OFF);
  896. }