zd_mac.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046
  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_FCTL_STYPE) == 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_write_mac_addr(&mac->chip, NULL);
  667. }
  668. static int zd_op_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf)
  669. {
  670. struct zd_mac *mac = zd_hw_mac(hw);
  671. return zd_chip_set_channel(&mac->chip, conf->channel->hw_value);
  672. }
  673. static int zd_op_config_interface(struct ieee80211_hw *hw,
  674. struct ieee80211_vif *vif,
  675. struct ieee80211_if_conf *conf)
  676. {
  677. struct zd_mac *mac = zd_hw_mac(hw);
  678. int associated;
  679. if (mac->type == IEEE80211_IF_TYPE_MESH_POINT) {
  680. associated = true;
  681. if (conf->beacon) {
  682. zd_mac_config_beacon(hw, conf->beacon);
  683. kfree_skb(conf->beacon);
  684. zd_set_beacon_interval(&mac->chip, BCN_MODE_IBSS |
  685. hw->conf.beacon_int);
  686. }
  687. } else
  688. associated = is_valid_ether_addr(conf->bssid);
  689. spin_lock_irq(&mac->lock);
  690. mac->associated = associated;
  691. spin_unlock_irq(&mac->lock);
  692. /* TODO: do hardware bssid filtering */
  693. return 0;
  694. }
  695. void zd_process_intr(struct work_struct *work)
  696. {
  697. u16 int_status;
  698. struct zd_mac *mac = container_of(work, struct zd_mac, process_intr);
  699. int_status = le16_to_cpu(*(u16 *)(mac->intr_buffer+4));
  700. if (int_status & INT_CFG_NEXT_BCN) {
  701. if (net_ratelimit())
  702. dev_dbg_f(zd_mac_dev(mac), "INT_CFG_NEXT_BCN\n");
  703. } else
  704. dev_dbg_f(zd_mac_dev(mac), "Unsupported interrupt\n");
  705. zd_chip_enable_hwint(&mac->chip);
  706. }
  707. static void set_multicast_hash_handler(struct work_struct *work)
  708. {
  709. struct zd_mac *mac =
  710. container_of(work, struct zd_mac, set_multicast_hash_work);
  711. struct zd_mc_hash hash;
  712. spin_lock_irq(&mac->lock);
  713. hash = mac->multicast_hash;
  714. spin_unlock_irq(&mac->lock);
  715. zd_chip_set_multicast_hash(&mac->chip, &hash);
  716. }
  717. static void set_rx_filter_handler(struct work_struct *work)
  718. {
  719. struct zd_mac *mac =
  720. container_of(work, struct zd_mac, set_rx_filter_work);
  721. int r;
  722. dev_dbg_f(zd_mac_dev(mac), "\n");
  723. r = set_rx_filter(mac);
  724. if (r)
  725. dev_err(zd_mac_dev(mac), "set_rx_filter_handler error %d\n", r);
  726. }
  727. #define SUPPORTED_FIF_FLAGS \
  728. (FIF_PROMISC_IN_BSS | FIF_ALLMULTI | FIF_FCSFAIL | FIF_CONTROL | \
  729. FIF_OTHER_BSS | FIF_BCN_PRBRESP_PROMISC)
  730. static void zd_op_configure_filter(struct ieee80211_hw *hw,
  731. unsigned int changed_flags,
  732. unsigned int *new_flags,
  733. int mc_count, struct dev_mc_list *mclist)
  734. {
  735. struct zd_mc_hash hash;
  736. struct zd_mac *mac = zd_hw_mac(hw);
  737. unsigned long flags;
  738. int i;
  739. /* Only deal with supported flags */
  740. changed_flags &= SUPPORTED_FIF_FLAGS;
  741. *new_flags &= SUPPORTED_FIF_FLAGS;
  742. /* changed_flags is always populated but this driver
  743. * doesn't support all FIF flags so its possible we don't
  744. * need to do anything */
  745. if (!changed_flags)
  746. return;
  747. if (*new_flags & (FIF_PROMISC_IN_BSS | FIF_ALLMULTI)) {
  748. zd_mc_add_all(&hash);
  749. } else {
  750. DECLARE_MAC_BUF(macbuf);
  751. zd_mc_clear(&hash);
  752. for (i = 0; i < mc_count; i++) {
  753. if (!mclist)
  754. break;
  755. dev_dbg_f(zd_mac_dev(mac), "mc addr %s\n",
  756. print_mac(macbuf, mclist->dmi_addr));
  757. zd_mc_add_addr(&hash, mclist->dmi_addr);
  758. mclist = mclist->next;
  759. }
  760. }
  761. spin_lock_irqsave(&mac->lock, flags);
  762. mac->pass_failed_fcs = !!(*new_flags & FIF_FCSFAIL);
  763. mac->pass_ctrl = !!(*new_flags & FIF_CONTROL);
  764. mac->multicast_hash = hash;
  765. spin_unlock_irqrestore(&mac->lock, flags);
  766. queue_work(zd_workqueue, &mac->set_multicast_hash_work);
  767. if (changed_flags & FIF_CONTROL)
  768. queue_work(zd_workqueue, &mac->set_rx_filter_work);
  769. /* no handling required for FIF_OTHER_BSS as we don't currently
  770. * do BSSID filtering */
  771. /* FIXME: in future it would be nice to enable the probe response
  772. * filter (so that the driver doesn't see them) until
  773. * FIF_BCN_PRBRESP_PROMISC is set. however due to atomicity here, we'd
  774. * have to schedule work to enable prbresp reception, which might
  775. * happen too late. For now we'll just listen and forward them all the
  776. * time. */
  777. }
  778. static void set_rts_cts_work(struct work_struct *work)
  779. {
  780. struct zd_mac *mac =
  781. container_of(work, struct zd_mac, set_rts_cts_work);
  782. unsigned long flags;
  783. unsigned int short_preamble;
  784. mutex_lock(&mac->chip.mutex);
  785. spin_lock_irqsave(&mac->lock, flags);
  786. mac->updating_rts_rate = 0;
  787. short_preamble = mac->short_preamble;
  788. spin_unlock_irqrestore(&mac->lock, flags);
  789. zd_chip_set_rts_cts_rate_locked(&mac->chip, short_preamble);
  790. mutex_unlock(&mac->chip.mutex);
  791. }
  792. static void zd_op_bss_info_changed(struct ieee80211_hw *hw,
  793. struct ieee80211_vif *vif,
  794. struct ieee80211_bss_conf *bss_conf,
  795. u32 changes)
  796. {
  797. struct zd_mac *mac = zd_hw_mac(hw);
  798. unsigned long flags;
  799. dev_dbg_f(zd_mac_dev(mac), "changes: %x\n", changes);
  800. if (changes & BSS_CHANGED_ERP_PREAMBLE) {
  801. spin_lock_irqsave(&mac->lock, flags);
  802. mac->short_preamble = bss_conf->use_short_preamble;
  803. if (!mac->updating_rts_rate) {
  804. mac->updating_rts_rate = 1;
  805. /* FIXME: should disable TX here, until work has
  806. * completed and RTS_CTS reg is updated */
  807. queue_work(zd_workqueue, &mac->set_rts_cts_work);
  808. }
  809. spin_unlock_irqrestore(&mac->lock, flags);
  810. }
  811. }
  812. static const struct ieee80211_ops zd_ops = {
  813. .tx = zd_op_tx,
  814. .start = zd_op_start,
  815. .stop = zd_op_stop,
  816. .add_interface = zd_op_add_interface,
  817. .remove_interface = zd_op_remove_interface,
  818. .config = zd_op_config,
  819. .config_interface = zd_op_config_interface,
  820. .configure_filter = zd_op_configure_filter,
  821. .bss_info_changed = zd_op_bss_info_changed,
  822. };
  823. struct ieee80211_hw *zd_mac_alloc_hw(struct usb_interface *intf)
  824. {
  825. struct zd_mac *mac;
  826. struct ieee80211_hw *hw;
  827. hw = ieee80211_alloc_hw(sizeof(struct zd_mac), &zd_ops);
  828. if (!hw) {
  829. dev_dbg_f(&intf->dev, "out of memory\n");
  830. return NULL;
  831. }
  832. mac = zd_hw_mac(hw);
  833. memset(mac, 0, sizeof(*mac));
  834. spin_lock_init(&mac->lock);
  835. mac->hw = hw;
  836. mac->type = IEEE80211_IF_TYPE_INVALID;
  837. memcpy(mac->channels, zd_channels, sizeof(zd_channels));
  838. memcpy(mac->rates, zd_rates, sizeof(zd_rates));
  839. mac->band.n_bitrates = ARRAY_SIZE(zd_rates);
  840. mac->band.bitrates = mac->rates;
  841. mac->band.n_channels = ARRAY_SIZE(zd_channels);
  842. mac->band.channels = mac->channels;
  843. hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &mac->band;
  844. hw->flags = IEEE80211_HW_RX_INCLUDES_FCS |
  845. IEEE80211_HW_HOST_GEN_BEACON_TEMPLATE;
  846. hw->max_rssi = 100;
  847. hw->max_signal = 100;
  848. hw->queues = 1;
  849. hw->extra_tx_headroom = sizeof(struct zd_ctrlset);
  850. skb_queue_head_init(&mac->ack_wait_queue);
  851. zd_chip_init(&mac->chip, hw, intf);
  852. housekeeping_init(mac);
  853. INIT_WORK(&mac->set_multicast_hash_work, set_multicast_hash_handler);
  854. INIT_WORK(&mac->set_rts_cts_work, set_rts_cts_work);
  855. INIT_WORK(&mac->set_rx_filter_work, set_rx_filter_handler);
  856. INIT_WORK(&mac->process_intr, zd_process_intr);
  857. SET_IEEE80211_DEV(hw, &intf->dev);
  858. return hw;
  859. }
  860. #define LINK_LED_WORK_DELAY HZ
  861. static void link_led_handler(struct work_struct *work)
  862. {
  863. struct zd_mac *mac =
  864. container_of(work, struct zd_mac, housekeeping.link_led_work.work);
  865. struct zd_chip *chip = &mac->chip;
  866. int is_associated;
  867. int r;
  868. spin_lock_irq(&mac->lock);
  869. is_associated = mac->associated;
  870. spin_unlock_irq(&mac->lock);
  871. r = zd_chip_control_leds(chip,
  872. is_associated ? LED_ASSOCIATED : LED_SCANNING);
  873. if (r)
  874. dev_dbg_f(zd_mac_dev(mac), "zd_chip_control_leds error %d\n", r);
  875. queue_delayed_work(zd_workqueue, &mac->housekeeping.link_led_work,
  876. LINK_LED_WORK_DELAY);
  877. }
  878. static void housekeeping_init(struct zd_mac *mac)
  879. {
  880. INIT_DELAYED_WORK(&mac->housekeeping.link_led_work, link_led_handler);
  881. }
  882. static void housekeeping_enable(struct zd_mac *mac)
  883. {
  884. dev_dbg_f(zd_mac_dev(mac), "\n");
  885. queue_delayed_work(zd_workqueue, &mac->housekeeping.link_led_work,
  886. 0);
  887. }
  888. static void housekeeping_disable(struct zd_mac *mac)
  889. {
  890. dev_dbg_f(zd_mac_dev(mac), "\n");
  891. cancel_rearming_delayed_workqueue(zd_workqueue,
  892. &mac->housekeeping.link_led_work);
  893. zd_chip_control_leds(&mac->chip, LED_OFF);
  894. }