zd_mac.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346
  1. /* zd_mac.c
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program; if not, write to the Free Software
  15. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. */
  17. #include <linux/netdevice.h>
  18. #include <linux/etherdevice.h>
  19. #include <linux/wireless.h>
  20. #include <linux/usb.h>
  21. #include <linux/jiffies.h>
  22. #include <net/ieee80211_radiotap.h>
  23. #include "zd_def.h"
  24. #include "zd_chip.h"
  25. #include "zd_mac.h"
  26. #include "zd_ieee80211.h"
  27. #include "zd_netdev.h"
  28. #include "zd_rf.h"
  29. static void ieee_init(struct ieee80211_device *ieee);
  30. static void softmac_init(struct ieee80211softmac_device *sm);
  31. static void set_rts_cts_work(struct work_struct *work);
  32. static void set_basic_rates_work(struct work_struct *work);
  33. static void housekeeping_init(struct zd_mac *mac);
  34. static void housekeeping_enable(struct zd_mac *mac);
  35. static void housekeeping_disable(struct zd_mac *mac);
  36. static void set_multicast_hash_handler(struct work_struct *work);
  37. static void do_rx(unsigned long mac_ptr);
  38. int zd_mac_init(struct zd_mac *mac,
  39. struct net_device *netdev,
  40. struct usb_interface *intf)
  41. {
  42. struct ieee80211_device *ieee = zd_netdev_ieee80211(netdev);
  43. memset(mac, 0, sizeof(*mac));
  44. spin_lock_init(&mac->lock);
  45. mac->netdev = netdev;
  46. INIT_DELAYED_WORK(&mac->set_rts_cts_work, set_rts_cts_work);
  47. INIT_DELAYED_WORK(&mac->set_basic_rates_work, set_basic_rates_work);
  48. skb_queue_head_init(&mac->rx_queue);
  49. tasklet_init(&mac->rx_tasklet, do_rx, (unsigned long)mac);
  50. tasklet_disable(&mac->rx_tasklet);
  51. ieee_init(ieee);
  52. softmac_init(ieee80211_priv(netdev));
  53. zd_chip_init(&mac->chip, netdev, intf);
  54. housekeeping_init(mac);
  55. INIT_WORK(&mac->set_multicast_hash_work, set_multicast_hash_handler);
  56. return 0;
  57. }
  58. static int reset_channel(struct zd_mac *mac)
  59. {
  60. int r;
  61. unsigned long flags;
  62. const struct channel_range *range;
  63. spin_lock_irqsave(&mac->lock, flags);
  64. range = zd_channel_range(mac->regdomain);
  65. if (!range->start) {
  66. r = -EINVAL;
  67. goto out;
  68. }
  69. mac->requested_channel = range->start;
  70. r = 0;
  71. out:
  72. spin_unlock_irqrestore(&mac->lock, flags);
  73. return r;
  74. }
  75. int zd_mac_preinit_hw(struct zd_mac *mac)
  76. {
  77. int r;
  78. u8 addr[ETH_ALEN];
  79. r = zd_chip_read_mac_addr_fw(&mac->chip, addr);
  80. if (r)
  81. return r;
  82. memcpy(mac->netdev->dev_addr, addr, ETH_ALEN);
  83. return 0;
  84. }
  85. int zd_mac_init_hw(struct zd_mac *mac)
  86. {
  87. int r;
  88. struct zd_chip *chip = &mac->chip;
  89. u8 default_regdomain;
  90. r = zd_chip_enable_int(chip);
  91. if (r)
  92. goto out;
  93. r = zd_chip_init_hw(chip);
  94. if (r)
  95. goto disable_int;
  96. ZD_ASSERT(!irqs_disabled());
  97. r = zd_read_regdomain(chip, &default_regdomain);
  98. if (r)
  99. goto disable_int;
  100. if (!zd_regdomain_supported(default_regdomain)) {
  101. /* The vendor driver overrides the regulatory domain and
  102. * allowed channel registers and unconditionally restricts
  103. * available channels to 1-11 everywhere. Match their
  104. * questionable behaviour only for regdomains which we don't
  105. * recognise. */
  106. dev_warn(zd_mac_dev(mac), "Unrecognised regulatory domain: "
  107. "%#04x. Defaulting to FCC.\n", default_regdomain);
  108. default_regdomain = ZD_REGDOMAIN_FCC;
  109. }
  110. spin_lock_irq(&mac->lock);
  111. mac->regdomain = mac->default_regdomain = default_regdomain;
  112. spin_unlock_irq(&mac->lock);
  113. r = reset_channel(mac);
  114. if (r)
  115. goto disable_int;
  116. /* We must inform the device that we are doing encryption/decryption in
  117. * software at the moment. */
  118. r = zd_set_encryption_type(chip, ENC_SNIFFER);
  119. if (r)
  120. goto disable_int;
  121. r = zd_geo_init(zd_mac_to_ieee80211(mac), mac->regdomain);
  122. if (r)
  123. goto disable_int;
  124. r = 0;
  125. disable_int:
  126. zd_chip_disable_int(chip);
  127. out:
  128. return r;
  129. }
  130. void zd_mac_clear(struct zd_mac *mac)
  131. {
  132. flush_workqueue(zd_workqueue);
  133. skb_queue_purge(&mac->rx_queue);
  134. tasklet_kill(&mac->rx_tasklet);
  135. zd_chip_clear(&mac->chip);
  136. ZD_ASSERT(!spin_is_locked(&mac->lock));
  137. ZD_MEMCLEAR(mac, sizeof(struct zd_mac));
  138. }
  139. static int set_rx_filter(struct zd_mac *mac)
  140. {
  141. struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
  142. u32 filter = (ieee->iw_mode == IW_MODE_MONITOR) ? ~0 : STA_RX_FILTER;
  143. return zd_iowrite32(&mac->chip, CR_RX_FILTER, filter);
  144. }
  145. static int set_sniffer(struct zd_mac *mac)
  146. {
  147. struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
  148. return zd_iowrite32(&mac->chip, CR_SNIFFER_ON,
  149. ieee->iw_mode == IW_MODE_MONITOR ? 1 : 0);
  150. return 0;
  151. }
  152. static int set_mc_hash(struct zd_mac *mac)
  153. {
  154. struct zd_mc_hash hash;
  155. struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
  156. zd_mc_clear(&hash);
  157. if (ieee->iw_mode == IW_MODE_MONITOR)
  158. zd_mc_add_all(&hash);
  159. return zd_chip_set_multicast_hash(&mac->chip, &hash);
  160. }
  161. int zd_mac_open(struct net_device *netdev)
  162. {
  163. struct zd_mac *mac = zd_netdev_mac(netdev);
  164. struct zd_chip *chip = &mac->chip;
  165. struct zd_usb *usb = &chip->usb;
  166. int r;
  167. if (!usb->initialized) {
  168. r = zd_usb_init_hw(usb);
  169. if (r)
  170. goto out;
  171. }
  172. tasklet_enable(&mac->rx_tasklet);
  173. r = zd_chip_enable_int(chip);
  174. if (r < 0)
  175. goto out;
  176. r = zd_write_mac_addr(chip, netdev->dev_addr);
  177. if (r)
  178. goto disable_int;
  179. r = zd_chip_set_basic_rates(chip, CR_RATES_80211B | CR_RATES_80211G);
  180. if (r < 0)
  181. goto disable_int;
  182. r = set_rx_filter(mac);
  183. if (r)
  184. goto disable_int;
  185. r = set_sniffer(mac);
  186. if (r)
  187. goto disable_int;
  188. r = set_mc_hash(mac);
  189. if (r)
  190. goto disable_int;
  191. r = zd_chip_switch_radio_on(chip);
  192. if (r < 0)
  193. goto disable_int;
  194. r = zd_chip_set_channel(chip, mac->requested_channel);
  195. if (r < 0)
  196. goto disable_radio;
  197. r = zd_chip_enable_rx(chip);
  198. if (r < 0)
  199. goto disable_radio;
  200. r = zd_chip_enable_hwint(chip);
  201. if (r < 0)
  202. goto disable_rx;
  203. housekeeping_enable(mac);
  204. ieee80211softmac_start(netdev);
  205. return 0;
  206. disable_rx:
  207. zd_chip_disable_rx(chip);
  208. disable_radio:
  209. zd_chip_switch_radio_off(chip);
  210. disable_int:
  211. zd_chip_disable_int(chip);
  212. out:
  213. return r;
  214. }
  215. int zd_mac_stop(struct net_device *netdev)
  216. {
  217. struct zd_mac *mac = zd_netdev_mac(netdev);
  218. struct zd_chip *chip = &mac->chip;
  219. netif_stop_queue(netdev);
  220. /*
  221. * The order here deliberately is a little different from the open()
  222. * method, since we need to make sure there is no opportunity for RX
  223. * frames to be processed by softmac after we have stopped it.
  224. */
  225. zd_chip_disable_rx(chip);
  226. skb_queue_purge(&mac->rx_queue);
  227. tasklet_disable(&mac->rx_tasklet);
  228. housekeeping_disable(mac);
  229. ieee80211softmac_stop(netdev);
  230. /* Ensure no work items are running or queued from this point */
  231. cancel_delayed_work(&mac->set_rts_cts_work);
  232. cancel_delayed_work(&mac->set_basic_rates_work);
  233. flush_workqueue(zd_workqueue);
  234. mac->updating_rts_rate = 0;
  235. mac->updating_basic_rates = 0;
  236. zd_chip_disable_hwint(chip);
  237. zd_chip_switch_radio_off(chip);
  238. zd_chip_disable_int(chip);
  239. return 0;
  240. }
  241. int zd_mac_set_mac_address(struct net_device *netdev, void *p)
  242. {
  243. int r;
  244. unsigned long flags;
  245. struct sockaddr *addr = p;
  246. struct zd_mac *mac = zd_netdev_mac(netdev);
  247. struct zd_chip *chip = &mac->chip;
  248. DECLARE_MAC_BUF(mac2);
  249. if (!is_valid_ether_addr(addr->sa_data))
  250. return -EADDRNOTAVAIL;
  251. dev_dbg_f(zd_mac_dev(mac),
  252. "Setting MAC to %s\n", print_mac(mac2, addr->sa_data));
  253. if (netdev->flags & IFF_UP) {
  254. r = zd_write_mac_addr(chip, addr->sa_data);
  255. if (r)
  256. return r;
  257. }
  258. spin_lock_irqsave(&mac->lock, flags);
  259. memcpy(netdev->dev_addr, addr->sa_data, ETH_ALEN);
  260. spin_unlock_irqrestore(&mac->lock, flags);
  261. return 0;
  262. }
  263. static void set_multicast_hash_handler(struct work_struct *work)
  264. {
  265. struct zd_mac *mac = container_of(work, struct zd_mac,
  266. set_multicast_hash_work);
  267. struct zd_mc_hash hash;
  268. spin_lock_irq(&mac->lock);
  269. hash = mac->multicast_hash;
  270. spin_unlock_irq(&mac->lock);
  271. zd_chip_set_multicast_hash(&mac->chip, &hash);
  272. }
  273. void zd_mac_set_multicast_list(struct net_device *dev)
  274. {
  275. struct zd_mac *mac = zd_netdev_mac(dev);
  276. struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
  277. struct zd_mc_hash hash;
  278. struct dev_mc_list *mc;
  279. unsigned long flags;
  280. DECLARE_MAC_BUF(mac2);
  281. if (dev->flags & (IFF_PROMISC|IFF_ALLMULTI) ||
  282. ieee->iw_mode == IW_MODE_MONITOR) {
  283. zd_mc_add_all(&hash);
  284. } else {
  285. zd_mc_clear(&hash);
  286. for (mc = dev->mc_list; mc; mc = mc->next) {
  287. dev_dbg_f(zd_mac_dev(mac), "mc addr %s\n",
  288. print_mac(mac2, mc->dmi_addr));
  289. zd_mc_add_addr(&hash, mc->dmi_addr);
  290. }
  291. }
  292. spin_lock_irqsave(&mac->lock, flags);
  293. mac->multicast_hash = hash;
  294. spin_unlock_irqrestore(&mac->lock, flags);
  295. queue_work(zd_workqueue, &mac->set_multicast_hash_work);
  296. }
  297. int zd_mac_set_regdomain(struct zd_mac *mac, u8 regdomain)
  298. {
  299. int r;
  300. u8 channel;
  301. ZD_ASSERT(!irqs_disabled());
  302. spin_lock_irq(&mac->lock);
  303. if (regdomain == 0) {
  304. regdomain = mac->default_regdomain;
  305. }
  306. if (!zd_regdomain_supported(regdomain)) {
  307. spin_unlock_irq(&mac->lock);
  308. return -EINVAL;
  309. }
  310. mac->regdomain = regdomain;
  311. channel = mac->requested_channel;
  312. spin_unlock_irq(&mac->lock);
  313. r = zd_geo_init(zd_mac_to_ieee80211(mac), regdomain);
  314. if (r)
  315. return r;
  316. if (!zd_regdomain_supports_channel(regdomain, channel)) {
  317. r = reset_channel(mac);
  318. if (r)
  319. return r;
  320. }
  321. return 0;
  322. }
  323. u8 zd_mac_get_regdomain(struct zd_mac *mac)
  324. {
  325. unsigned long flags;
  326. u8 regdomain;
  327. spin_lock_irqsave(&mac->lock, flags);
  328. regdomain = mac->regdomain;
  329. spin_unlock_irqrestore(&mac->lock, flags);
  330. return regdomain;
  331. }
  332. /* Fallback to lowest rate, if rate is unknown. */
  333. static u8 rate_to_zd_rate(u8 rate)
  334. {
  335. switch (rate) {
  336. case IEEE80211_CCK_RATE_2MB:
  337. return ZD_CCK_RATE_2M;
  338. case IEEE80211_CCK_RATE_5MB:
  339. return ZD_CCK_RATE_5_5M;
  340. case IEEE80211_CCK_RATE_11MB:
  341. return ZD_CCK_RATE_11M;
  342. case IEEE80211_OFDM_RATE_6MB:
  343. return ZD_OFDM_RATE_6M;
  344. case IEEE80211_OFDM_RATE_9MB:
  345. return ZD_OFDM_RATE_9M;
  346. case IEEE80211_OFDM_RATE_12MB:
  347. return ZD_OFDM_RATE_12M;
  348. case IEEE80211_OFDM_RATE_18MB:
  349. return ZD_OFDM_RATE_18M;
  350. case IEEE80211_OFDM_RATE_24MB:
  351. return ZD_OFDM_RATE_24M;
  352. case IEEE80211_OFDM_RATE_36MB:
  353. return ZD_OFDM_RATE_36M;
  354. case IEEE80211_OFDM_RATE_48MB:
  355. return ZD_OFDM_RATE_48M;
  356. case IEEE80211_OFDM_RATE_54MB:
  357. return ZD_OFDM_RATE_54M;
  358. }
  359. return ZD_CCK_RATE_1M;
  360. }
  361. static u16 rate_to_cr_rate(u8 rate)
  362. {
  363. switch (rate) {
  364. case IEEE80211_CCK_RATE_2MB:
  365. return CR_RATE_1M;
  366. case IEEE80211_CCK_RATE_5MB:
  367. return CR_RATE_5_5M;
  368. case IEEE80211_CCK_RATE_11MB:
  369. return CR_RATE_11M;
  370. case IEEE80211_OFDM_RATE_6MB:
  371. return CR_RATE_6M;
  372. case IEEE80211_OFDM_RATE_9MB:
  373. return CR_RATE_9M;
  374. case IEEE80211_OFDM_RATE_12MB:
  375. return CR_RATE_12M;
  376. case IEEE80211_OFDM_RATE_18MB:
  377. return CR_RATE_18M;
  378. case IEEE80211_OFDM_RATE_24MB:
  379. return CR_RATE_24M;
  380. case IEEE80211_OFDM_RATE_36MB:
  381. return CR_RATE_36M;
  382. case IEEE80211_OFDM_RATE_48MB:
  383. return CR_RATE_48M;
  384. case IEEE80211_OFDM_RATE_54MB:
  385. return CR_RATE_54M;
  386. }
  387. return CR_RATE_1M;
  388. }
  389. static void try_enable_tx(struct zd_mac *mac)
  390. {
  391. unsigned long flags;
  392. spin_lock_irqsave(&mac->lock, flags);
  393. if (mac->updating_rts_rate == 0 && mac->updating_basic_rates == 0)
  394. netif_wake_queue(mac->netdev);
  395. spin_unlock_irqrestore(&mac->lock, flags);
  396. }
  397. static void set_rts_cts_work(struct work_struct *work)
  398. {
  399. struct zd_mac *mac =
  400. container_of(work, struct zd_mac, set_rts_cts_work.work);
  401. unsigned long flags;
  402. u8 rts_rate;
  403. unsigned int short_preamble;
  404. mutex_lock(&mac->chip.mutex);
  405. spin_lock_irqsave(&mac->lock, flags);
  406. mac->updating_rts_rate = 0;
  407. rts_rate = mac->rts_rate;
  408. short_preamble = mac->short_preamble;
  409. spin_unlock_irqrestore(&mac->lock, flags);
  410. zd_chip_set_rts_cts_rate_locked(&mac->chip, rts_rate, short_preamble);
  411. mutex_unlock(&mac->chip.mutex);
  412. try_enable_tx(mac);
  413. }
  414. static void set_basic_rates_work(struct work_struct *work)
  415. {
  416. struct zd_mac *mac =
  417. container_of(work, struct zd_mac, set_basic_rates_work.work);
  418. unsigned long flags;
  419. u16 basic_rates;
  420. mutex_lock(&mac->chip.mutex);
  421. spin_lock_irqsave(&mac->lock, flags);
  422. mac->updating_basic_rates = 0;
  423. basic_rates = mac->basic_rates;
  424. spin_unlock_irqrestore(&mac->lock, flags);
  425. zd_chip_set_basic_rates_locked(&mac->chip, basic_rates);
  426. mutex_unlock(&mac->chip.mutex);
  427. try_enable_tx(mac);
  428. }
  429. static void bssinfo_change(struct net_device *netdev, u32 changes)
  430. {
  431. struct zd_mac *mac = zd_netdev_mac(netdev);
  432. struct ieee80211softmac_device *softmac = ieee80211_priv(netdev);
  433. struct ieee80211softmac_bss_info *bssinfo = &softmac->bssinfo;
  434. int need_set_rts_cts = 0;
  435. int need_set_rates = 0;
  436. u16 basic_rates;
  437. unsigned long flags;
  438. dev_dbg_f(zd_mac_dev(mac), "changes: %x\n", changes);
  439. if (changes & IEEE80211SOFTMAC_BSSINFOCHG_SHORT_PREAMBLE) {
  440. spin_lock_irqsave(&mac->lock, flags);
  441. mac->short_preamble = bssinfo->short_preamble;
  442. spin_unlock_irqrestore(&mac->lock, flags);
  443. need_set_rts_cts = 1;
  444. }
  445. if (changes & IEEE80211SOFTMAC_BSSINFOCHG_RATES) {
  446. /* Set RTS rate to highest available basic rate */
  447. u8 hi_rate = ieee80211softmac_highest_supported_rate(softmac,
  448. &bssinfo->supported_rates, 1);
  449. hi_rate = rate_to_zd_rate(hi_rate);
  450. spin_lock_irqsave(&mac->lock, flags);
  451. if (hi_rate != mac->rts_rate) {
  452. mac->rts_rate = hi_rate;
  453. need_set_rts_cts = 1;
  454. }
  455. spin_unlock_irqrestore(&mac->lock, flags);
  456. /* Set basic rates */
  457. need_set_rates = 1;
  458. if (bssinfo->supported_rates.count == 0) {
  459. /* Allow the device to be flexible */
  460. basic_rates = CR_RATES_80211B | CR_RATES_80211G;
  461. } else {
  462. int i = 0;
  463. basic_rates = 0;
  464. for (i = 0; i < bssinfo->supported_rates.count; i++) {
  465. u16 rate = bssinfo->supported_rates.rates[i];
  466. if ((rate & IEEE80211_BASIC_RATE_MASK) == 0)
  467. continue;
  468. rate &= ~IEEE80211_BASIC_RATE_MASK;
  469. basic_rates |= rate_to_cr_rate(rate);
  470. }
  471. }
  472. spin_lock_irqsave(&mac->lock, flags);
  473. mac->basic_rates = basic_rates;
  474. spin_unlock_irqrestore(&mac->lock, flags);
  475. }
  476. /* Schedule any changes we made above */
  477. spin_lock_irqsave(&mac->lock, flags);
  478. if (need_set_rts_cts && !mac->updating_rts_rate) {
  479. mac->updating_rts_rate = 1;
  480. netif_stop_queue(mac->netdev);
  481. queue_delayed_work(zd_workqueue, &mac->set_rts_cts_work, 0);
  482. }
  483. if (need_set_rates && !mac->updating_basic_rates) {
  484. mac->updating_basic_rates = 1;
  485. netif_stop_queue(mac->netdev);
  486. queue_delayed_work(zd_workqueue, &mac->set_basic_rates_work,
  487. 0);
  488. }
  489. spin_unlock_irqrestore(&mac->lock, flags);
  490. }
  491. static void set_channel(struct net_device *netdev, u8 channel)
  492. {
  493. struct zd_mac *mac = zd_netdev_mac(netdev);
  494. dev_dbg_f(zd_mac_dev(mac), "channel %d\n", channel);
  495. zd_chip_set_channel(&mac->chip, channel);
  496. }
  497. int zd_mac_request_channel(struct zd_mac *mac, u8 channel)
  498. {
  499. unsigned long lock_flags;
  500. struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
  501. if (ieee->iw_mode == IW_MODE_INFRA)
  502. return -EPERM;
  503. spin_lock_irqsave(&mac->lock, lock_flags);
  504. if (!zd_regdomain_supports_channel(mac->regdomain, channel)) {
  505. spin_unlock_irqrestore(&mac->lock, lock_flags);
  506. return -EINVAL;
  507. }
  508. mac->requested_channel = channel;
  509. spin_unlock_irqrestore(&mac->lock, lock_flags);
  510. if (netif_running(mac->netdev))
  511. return zd_chip_set_channel(&mac->chip, channel);
  512. else
  513. return 0;
  514. }
  515. u8 zd_mac_get_channel(struct zd_mac *mac)
  516. {
  517. u8 channel = zd_chip_get_channel(&mac->chip);
  518. dev_dbg_f(zd_mac_dev(mac), "channel %u\n", channel);
  519. return channel;
  520. }
  521. int zd_mac_set_mode(struct zd_mac *mac, u32 mode)
  522. {
  523. struct ieee80211_device *ieee;
  524. switch (mode) {
  525. case IW_MODE_AUTO:
  526. case IW_MODE_ADHOC:
  527. case IW_MODE_INFRA:
  528. mac->netdev->type = ARPHRD_ETHER;
  529. break;
  530. case IW_MODE_MONITOR:
  531. mac->netdev->type = ARPHRD_IEEE80211_RADIOTAP;
  532. break;
  533. default:
  534. dev_dbg_f(zd_mac_dev(mac), "wrong mode %u\n", mode);
  535. return -EINVAL;
  536. }
  537. ieee = zd_mac_to_ieee80211(mac);
  538. ZD_ASSERT(!irqs_disabled());
  539. spin_lock_irq(&ieee->lock);
  540. ieee->iw_mode = mode;
  541. spin_unlock_irq(&ieee->lock);
  542. if (netif_running(mac->netdev)) {
  543. int r = set_rx_filter(mac);
  544. if (r)
  545. return r;
  546. return set_sniffer(mac);
  547. }
  548. return 0;
  549. }
  550. int zd_mac_get_mode(struct zd_mac *mac, u32 *mode)
  551. {
  552. unsigned long flags;
  553. struct ieee80211_device *ieee;
  554. ieee = zd_mac_to_ieee80211(mac);
  555. spin_lock_irqsave(&ieee->lock, flags);
  556. *mode = ieee->iw_mode;
  557. spin_unlock_irqrestore(&ieee->lock, flags);
  558. return 0;
  559. }
  560. int zd_mac_get_range(struct zd_mac *mac, struct iw_range *range)
  561. {
  562. int i;
  563. const struct channel_range *channel_range;
  564. u8 regdomain;
  565. memset(range, 0, sizeof(*range));
  566. /* FIXME: Not so important and depends on the mode. For 802.11g
  567. * usually this value is used. It seems to be that Bit/s number is
  568. * given here.
  569. */
  570. range->throughput = 27 * 1000 * 1000;
  571. range->max_qual.qual = 100;
  572. range->max_qual.level = 100;
  573. /* FIXME: Needs still to be tuned. */
  574. range->avg_qual.qual = 71;
  575. range->avg_qual.level = 80;
  576. /* FIXME: depends on standard? */
  577. range->min_rts = 256;
  578. range->max_rts = 2346;
  579. range->min_frag = MIN_FRAG_THRESHOLD;
  580. range->max_frag = MAX_FRAG_THRESHOLD;
  581. range->max_encoding_tokens = WEP_KEYS;
  582. range->num_encoding_sizes = 2;
  583. range->encoding_size[0] = 5;
  584. range->encoding_size[1] = WEP_KEY_LEN;
  585. range->we_version_compiled = WIRELESS_EXT;
  586. range->we_version_source = 20;
  587. range->enc_capa = IW_ENC_CAPA_WPA | IW_ENC_CAPA_WPA2 |
  588. IW_ENC_CAPA_CIPHER_TKIP | IW_ENC_CAPA_CIPHER_CCMP;
  589. ZD_ASSERT(!irqs_disabled());
  590. spin_lock_irq(&mac->lock);
  591. regdomain = mac->regdomain;
  592. spin_unlock_irq(&mac->lock);
  593. channel_range = zd_channel_range(regdomain);
  594. range->num_channels = channel_range->end - channel_range->start;
  595. range->old_num_channels = range->num_channels;
  596. range->num_frequency = range->num_channels;
  597. range->old_num_frequency = range->num_frequency;
  598. for (i = 0; i < range->num_frequency; i++) {
  599. struct iw_freq *freq = &range->freq[i];
  600. freq->i = channel_range->start + i;
  601. zd_channel_to_freq(freq, freq->i);
  602. }
  603. return 0;
  604. }
  605. static int zd_calc_tx_length_us(u8 *service, u8 zd_rate, u16 tx_length)
  606. {
  607. /* ZD_PURE_RATE() must be used to remove the modulation type flag of
  608. * the zd-rate values. */
  609. static const u8 rate_divisor[] = {
  610. [ZD_PURE_RATE(ZD_CCK_RATE_1M)] = 1,
  611. [ZD_PURE_RATE(ZD_CCK_RATE_2M)] = 2,
  612. /* bits must be doubled */
  613. [ZD_PURE_RATE(ZD_CCK_RATE_5_5M)] = 11,
  614. [ZD_PURE_RATE(ZD_CCK_RATE_11M)] = 11,
  615. [ZD_PURE_RATE(ZD_OFDM_RATE_6M)] = 6,
  616. [ZD_PURE_RATE(ZD_OFDM_RATE_9M)] = 9,
  617. [ZD_PURE_RATE(ZD_OFDM_RATE_12M)] = 12,
  618. [ZD_PURE_RATE(ZD_OFDM_RATE_18M)] = 18,
  619. [ZD_PURE_RATE(ZD_OFDM_RATE_24M)] = 24,
  620. [ZD_PURE_RATE(ZD_OFDM_RATE_36M)] = 36,
  621. [ZD_PURE_RATE(ZD_OFDM_RATE_48M)] = 48,
  622. [ZD_PURE_RATE(ZD_OFDM_RATE_54M)] = 54,
  623. };
  624. u32 bits = (u32)tx_length * 8;
  625. u32 divisor;
  626. divisor = rate_divisor[ZD_PURE_RATE(zd_rate)];
  627. if (divisor == 0)
  628. return -EINVAL;
  629. switch (zd_rate) {
  630. case ZD_CCK_RATE_5_5M:
  631. bits = (2*bits) + 10; /* round up to the next integer */
  632. break;
  633. case ZD_CCK_RATE_11M:
  634. if (service) {
  635. u32 t = bits % 11;
  636. *service &= ~ZD_PLCP_SERVICE_LENGTH_EXTENSION;
  637. if (0 < t && t <= 3) {
  638. *service |= ZD_PLCP_SERVICE_LENGTH_EXTENSION;
  639. }
  640. }
  641. bits += 10; /* round up to the next integer */
  642. break;
  643. }
  644. return bits/divisor;
  645. }
  646. static void cs_set_modulation(struct zd_mac *mac, struct zd_ctrlset *cs,
  647. struct ieee80211_hdr_4addr *hdr)
  648. {
  649. struct ieee80211softmac_device *softmac = ieee80211_priv(mac->netdev);
  650. u16 ftype = WLAN_FC_GET_TYPE(le16_to_cpu(hdr->frame_ctl));
  651. u8 rate;
  652. int is_mgt = (ftype == IEEE80211_FTYPE_MGMT) != 0;
  653. int is_multicast = is_multicast_ether_addr(hdr->addr1);
  654. int short_preamble = ieee80211softmac_short_preamble_ok(softmac,
  655. is_multicast, is_mgt);
  656. rate = ieee80211softmac_suggest_txrate(softmac, is_multicast, is_mgt);
  657. cs->modulation = rate_to_zd_rate(rate);
  658. /* Set short preamble bit when appropriate */
  659. if (short_preamble && ZD_MODULATION_TYPE(cs->modulation) == ZD_CCK
  660. && cs->modulation != ZD_CCK_RATE_1M)
  661. cs->modulation |= ZD_CCK_PREA_SHORT;
  662. }
  663. static void cs_set_control(struct zd_mac *mac, struct zd_ctrlset *cs,
  664. struct ieee80211_hdr_4addr *header)
  665. {
  666. struct ieee80211softmac_device *softmac = ieee80211_priv(mac->netdev);
  667. unsigned int tx_length = le16_to_cpu(cs->tx_length);
  668. u16 fctl = le16_to_cpu(header->frame_ctl);
  669. u16 ftype = WLAN_FC_GET_TYPE(fctl);
  670. u16 stype = WLAN_FC_GET_STYPE(fctl);
  671. /*
  672. * CONTROL TODO:
  673. * - if backoff needed, enable bit 0
  674. * - if burst (backoff not needed) disable bit 0
  675. */
  676. cs->control = 0;
  677. /* First fragment */
  678. if (WLAN_GET_SEQ_FRAG(le16_to_cpu(header->seq_ctl)) == 0)
  679. cs->control |= ZD_CS_NEED_RANDOM_BACKOFF;
  680. /* Multicast */
  681. if (is_multicast_ether_addr(header->addr1))
  682. cs->control |= ZD_CS_MULTICAST;
  683. /* PS-POLL */
  684. if (ftype == IEEE80211_FTYPE_CTL && stype == IEEE80211_STYPE_PSPOLL)
  685. cs->control |= ZD_CS_PS_POLL_FRAME;
  686. /* Unicast data frames over the threshold should have RTS */
  687. if (!is_multicast_ether_addr(header->addr1) &&
  688. ftype != IEEE80211_FTYPE_MGMT &&
  689. tx_length > zd_netdev_ieee80211(mac->netdev)->rts)
  690. cs->control |= ZD_CS_RTS;
  691. /* Use CTS-to-self protection if required */
  692. if (ZD_MODULATION_TYPE(cs->modulation) == ZD_OFDM &&
  693. ieee80211softmac_protection_needed(softmac)) {
  694. /* FIXME: avoid sending RTS *and* self-CTS, is that correct? */
  695. cs->control &= ~ZD_CS_RTS;
  696. cs->control |= ZD_CS_SELF_CTS;
  697. }
  698. /* FIXME: Management frame? */
  699. }
  700. static int fill_ctrlset(struct zd_mac *mac,
  701. struct ieee80211_txb *txb,
  702. int frag_num)
  703. {
  704. int r;
  705. struct sk_buff *skb = txb->fragments[frag_num];
  706. struct ieee80211_hdr_4addr *hdr =
  707. (struct ieee80211_hdr_4addr *) skb->data;
  708. unsigned int frag_len = skb->len + IEEE80211_FCS_LEN;
  709. unsigned int next_frag_len;
  710. unsigned int packet_length;
  711. struct zd_ctrlset *cs = (struct zd_ctrlset *)
  712. skb_push(skb, sizeof(struct zd_ctrlset));
  713. if (frag_num+1 < txb->nr_frags) {
  714. next_frag_len = txb->fragments[frag_num+1]->len +
  715. IEEE80211_FCS_LEN;
  716. } else {
  717. next_frag_len = 0;
  718. }
  719. ZD_ASSERT(frag_len <= 0xffff);
  720. ZD_ASSERT(next_frag_len <= 0xffff);
  721. cs_set_modulation(mac, cs, hdr);
  722. cs->tx_length = cpu_to_le16(frag_len);
  723. cs_set_control(mac, cs, hdr);
  724. packet_length = frag_len + sizeof(struct zd_ctrlset) + 10;
  725. ZD_ASSERT(packet_length <= 0xffff);
  726. /* ZD1211B: Computing the length difference this way, gives us
  727. * flexibility to compute the packet length.
  728. */
  729. cs->packet_length = cpu_to_le16(zd_chip_is_zd1211b(&mac->chip) ?
  730. packet_length - frag_len : packet_length);
  731. /*
  732. * CURRENT LENGTH:
  733. * - transmit frame length in microseconds
  734. * - seems to be derived from frame length
  735. * - see Cal_Us_Service() in zdinlinef.h
  736. * - if macp->bTxBurstEnable is enabled, then multiply by 4
  737. * - bTxBurstEnable is never set in the vendor driver
  738. *
  739. * SERVICE:
  740. * - "for PLCP configuration"
  741. * - always 0 except in some situations at 802.11b 11M
  742. * - see line 53 of zdinlinef.h
  743. */
  744. cs->service = 0;
  745. r = zd_calc_tx_length_us(&cs->service, ZD_RATE(cs->modulation),
  746. le16_to_cpu(cs->tx_length));
  747. if (r < 0)
  748. return r;
  749. cs->current_length = cpu_to_le16(r);
  750. if (next_frag_len == 0) {
  751. cs->next_frame_length = 0;
  752. } else {
  753. r = zd_calc_tx_length_us(NULL, ZD_RATE(cs->modulation),
  754. next_frag_len);
  755. if (r < 0)
  756. return r;
  757. cs->next_frame_length = cpu_to_le16(r);
  758. }
  759. return 0;
  760. }
  761. static int zd_mac_tx(struct zd_mac *mac, struct ieee80211_txb *txb, int pri)
  762. {
  763. int i, r;
  764. struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
  765. for (i = 0; i < txb->nr_frags; i++) {
  766. struct sk_buff *skb = txb->fragments[i];
  767. r = fill_ctrlset(mac, txb, i);
  768. if (r) {
  769. ieee->stats.tx_dropped++;
  770. return r;
  771. }
  772. r = zd_usb_tx(&mac->chip.usb, skb->data, skb->len);
  773. if (r) {
  774. ieee->stats.tx_dropped++;
  775. return r;
  776. }
  777. }
  778. /* FIXME: shouldn't this be handled by the upper layers? */
  779. mac->netdev->trans_start = jiffies;
  780. ieee80211_txb_free(txb);
  781. return 0;
  782. }
  783. struct zd_rt_hdr {
  784. struct ieee80211_radiotap_header rt_hdr;
  785. u8 rt_flags;
  786. u8 rt_rate;
  787. u16 rt_channel;
  788. u16 rt_chbitmask;
  789. } __attribute__((packed));
  790. static void fill_rt_header(void *buffer, struct zd_mac *mac,
  791. const struct ieee80211_rx_stats *stats,
  792. const struct rx_status *status)
  793. {
  794. struct zd_rt_hdr *hdr = buffer;
  795. hdr->rt_hdr.it_version = PKTHDR_RADIOTAP_VERSION;
  796. hdr->rt_hdr.it_pad = 0;
  797. hdr->rt_hdr.it_len = cpu_to_le16(sizeof(struct zd_rt_hdr));
  798. hdr->rt_hdr.it_present = cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) |
  799. (1 << IEEE80211_RADIOTAP_CHANNEL) |
  800. (1 << IEEE80211_RADIOTAP_RATE));
  801. hdr->rt_flags = 0;
  802. if (status->decryption_type & (ZD_RX_WEP64|ZD_RX_WEP128|ZD_RX_WEP256))
  803. hdr->rt_flags |= IEEE80211_RADIOTAP_F_WEP;
  804. hdr->rt_rate = stats->rate / 5;
  805. /* FIXME: 802.11a */
  806. hdr->rt_channel = cpu_to_le16(ieee80211chan2mhz(
  807. _zd_chip_get_channel(&mac->chip)));
  808. hdr->rt_chbitmask = cpu_to_le16(IEEE80211_CHAN_2GHZ |
  809. ((status->frame_status & ZD_RX_FRAME_MODULATION_MASK) ==
  810. ZD_RX_OFDM ? IEEE80211_CHAN_OFDM : IEEE80211_CHAN_CCK));
  811. }
  812. /* Returns 1 if the data packet is for us and 0 otherwise. */
  813. static int is_data_packet_for_us(struct ieee80211_device *ieee,
  814. struct ieee80211_hdr_4addr *hdr)
  815. {
  816. struct net_device *netdev = ieee->dev;
  817. u16 fc = le16_to_cpu(hdr->frame_ctl);
  818. ZD_ASSERT(WLAN_FC_GET_TYPE(fc) == IEEE80211_FTYPE_DATA);
  819. switch (ieee->iw_mode) {
  820. case IW_MODE_ADHOC:
  821. if ((fc & (IEEE80211_FCTL_TODS|IEEE80211_FCTL_FROMDS)) != 0 ||
  822. compare_ether_addr(hdr->addr3, ieee->bssid) != 0)
  823. return 0;
  824. break;
  825. case IW_MODE_AUTO:
  826. case IW_MODE_INFRA:
  827. if ((fc & (IEEE80211_FCTL_TODS|IEEE80211_FCTL_FROMDS)) !=
  828. IEEE80211_FCTL_FROMDS ||
  829. compare_ether_addr(hdr->addr2, ieee->bssid) != 0)
  830. return 0;
  831. break;
  832. default:
  833. ZD_ASSERT(ieee->iw_mode != IW_MODE_MONITOR);
  834. return 0;
  835. }
  836. return compare_ether_addr(hdr->addr1, netdev->dev_addr) == 0 ||
  837. (is_multicast_ether_addr(hdr->addr1) &&
  838. compare_ether_addr(hdr->addr3, netdev->dev_addr) != 0) ||
  839. (netdev->flags & IFF_PROMISC);
  840. }
  841. /* Filters received packets. The function returns 1 if the packet should be
  842. * forwarded to ieee80211_rx(). If the packet should be ignored the function
  843. * returns 0. If an invalid packet is found the function returns -EINVAL.
  844. *
  845. * The function calls ieee80211_rx_mgt() directly.
  846. *
  847. * It has been based on ieee80211_rx_any.
  848. */
  849. static int filter_rx(struct ieee80211_device *ieee,
  850. const u8 *buffer, unsigned int length,
  851. struct ieee80211_rx_stats *stats)
  852. {
  853. struct ieee80211_hdr_4addr *hdr;
  854. u16 fc;
  855. if (ieee->iw_mode == IW_MODE_MONITOR)
  856. return 1;
  857. hdr = (struct ieee80211_hdr_4addr *)buffer;
  858. fc = le16_to_cpu(hdr->frame_ctl);
  859. if ((fc & IEEE80211_FCTL_VERS) != 0)
  860. return -EINVAL;
  861. switch (WLAN_FC_GET_TYPE(fc)) {
  862. case IEEE80211_FTYPE_MGMT:
  863. if (length < sizeof(struct ieee80211_hdr_3addr))
  864. return -EINVAL;
  865. ieee80211_rx_mgt(ieee, hdr, stats);
  866. return 0;
  867. case IEEE80211_FTYPE_CTL:
  868. return 0;
  869. case IEEE80211_FTYPE_DATA:
  870. /* Ignore invalid short buffers */
  871. if (length < sizeof(struct ieee80211_hdr_3addr))
  872. return -EINVAL;
  873. return is_data_packet_for_us(ieee, hdr);
  874. }
  875. return -EINVAL;
  876. }
  877. static void update_qual_rssi(struct zd_mac *mac,
  878. const u8 *buffer, unsigned int length,
  879. u8 qual_percent, u8 rssi_percent)
  880. {
  881. unsigned long flags;
  882. struct ieee80211_hdr_3addr *hdr;
  883. int i;
  884. hdr = (struct ieee80211_hdr_3addr *)buffer;
  885. if (length < offsetof(struct ieee80211_hdr_3addr, addr3))
  886. return;
  887. if (compare_ether_addr(hdr->addr2, zd_mac_to_ieee80211(mac)->bssid) != 0)
  888. return;
  889. spin_lock_irqsave(&mac->lock, flags);
  890. i = mac->stats_count % ZD_MAC_STATS_BUFFER_SIZE;
  891. mac->qual_buffer[i] = qual_percent;
  892. mac->rssi_buffer[i] = rssi_percent;
  893. mac->stats_count++;
  894. spin_unlock_irqrestore(&mac->lock, flags);
  895. }
  896. static int fill_rx_stats(struct ieee80211_rx_stats *stats,
  897. const struct rx_status **pstatus,
  898. struct zd_mac *mac,
  899. const u8 *buffer, unsigned int length)
  900. {
  901. const struct rx_status *status;
  902. *pstatus = status = (struct rx_status *)
  903. (buffer + (length - sizeof(struct rx_status)));
  904. if (status->frame_status & ZD_RX_ERROR) {
  905. struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
  906. ieee->stats.rx_errors++;
  907. if (status->frame_status & ZD_RX_TIMEOUT_ERROR)
  908. ieee->stats.rx_missed_errors++;
  909. else if (status->frame_status & ZD_RX_FIFO_OVERRUN_ERROR)
  910. ieee->stats.rx_fifo_errors++;
  911. else if (status->frame_status & ZD_RX_DECRYPTION_ERROR)
  912. ieee->ieee_stats.rx_discards_undecryptable++;
  913. else if (status->frame_status & ZD_RX_CRC32_ERROR) {
  914. ieee->stats.rx_crc_errors++;
  915. ieee->ieee_stats.rx_fcs_errors++;
  916. }
  917. else if (status->frame_status & ZD_RX_CRC16_ERROR)
  918. ieee->stats.rx_crc_errors++;
  919. return -EINVAL;
  920. }
  921. memset(stats, 0, sizeof(struct ieee80211_rx_stats));
  922. stats->len = length - (ZD_PLCP_HEADER_SIZE + IEEE80211_FCS_LEN +
  923. + sizeof(struct rx_status));
  924. /* FIXME: 802.11a */
  925. stats->freq = IEEE80211_24GHZ_BAND;
  926. stats->received_channel = _zd_chip_get_channel(&mac->chip);
  927. stats->rssi = zd_rx_strength_percent(status->signal_strength);
  928. stats->signal = zd_rx_qual_percent(buffer,
  929. length - sizeof(struct rx_status),
  930. status);
  931. stats->mask = IEEE80211_STATMASK_RSSI | IEEE80211_STATMASK_SIGNAL;
  932. stats->rate = zd_rx_rate(buffer, status);
  933. if (stats->rate)
  934. stats->mask |= IEEE80211_STATMASK_RATE;
  935. return 0;
  936. }
  937. static void zd_mac_rx(struct zd_mac *mac, struct sk_buff *skb)
  938. {
  939. int r;
  940. struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
  941. struct ieee80211_rx_stats stats;
  942. const struct rx_status *status;
  943. if (skb->len < ZD_PLCP_HEADER_SIZE + IEEE80211_1ADDR_LEN +
  944. IEEE80211_FCS_LEN + sizeof(struct rx_status))
  945. {
  946. ieee->stats.rx_errors++;
  947. ieee->stats.rx_length_errors++;
  948. goto free_skb;
  949. }
  950. r = fill_rx_stats(&stats, &status, mac, skb->data, skb->len);
  951. if (r) {
  952. /* Only packets with rx errors are included here.
  953. * The error stats have already been set in fill_rx_stats.
  954. */
  955. goto free_skb;
  956. }
  957. __skb_pull(skb, ZD_PLCP_HEADER_SIZE);
  958. __skb_trim(skb, skb->len -
  959. (IEEE80211_FCS_LEN + sizeof(struct rx_status)));
  960. ZD_ASSERT(IS_ALIGNED((unsigned long)skb->data, 4));
  961. update_qual_rssi(mac, skb->data, skb->len, stats.signal,
  962. status->signal_strength);
  963. r = filter_rx(ieee, skb->data, skb->len, &stats);
  964. if (r <= 0) {
  965. if (r < 0) {
  966. ieee->stats.rx_errors++;
  967. dev_dbg_f(zd_mac_dev(mac), "Error in packet.\n");
  968. }
  969. goto free_skb;
  970. }
  971. if (ieee->iw_mode == IW_MODE_MONITOR)
  972. fill_rt_header(skb_push(skb, sizeof(struct zd_rt_hdr)), mac,
  973. &stats, status);
  974. r = ieee80211_rx(ieee, skb, &stats);
  975. if (r)
  976. return;
  977. free_skb:
  978. /* We are always in a soft irq. */
  979. dev_kfree_skb(skb);
  980. }
  981. static void do_rx(unsigned long mac_ptr)
  982. {
  983. struct zd_mac *mac = (struct zd_mac *)mac_ptr;
  984. struct sk_buff *skb;
  985. while ((skb = skb_dequeue(&mac->rx_queue)) != NULL)
  986. zd_mac_rx(mac, skb);
  987. }
  988. int zd_mac_rx_irq(struct zd_mac *mac, const u8 *buffer, unsigned int length)
  989. {
  990. struct sk_buff *skb;
  991. unsigned int reserved =
  992. ALIGN(max_t(unsigned int,
  993. sizeof(struct zd_rt_hdr), ZD_PLCP_HEADER_SIZE), 4) -
  994. ZD_PLCP_HEADER_SIZE;
  995. skb = dev_alloc_skb(reserved + length);
  996. if (!skb) {
  997. struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
  998. dev_warn(zd_mac_dev(mac), "Could not allocate skb.\n");
  999. ieee->stats.rx_dropped++;
  1000. return -ENOMEM;
  1001. }
  1002. skb_reserve(skb, reserved);
  1003. memcpy(__skb_put(skb, length), buffer, length);
  1004. skb_queue_tail(&mac->rx_queue, skb);
  1005. tasklet_schedule(&mac->rx_tasklet);
  1006. return 0;
  1007. }
  1008. static int netdev_tx(struct ieee80211_txb *txb, struct net_device *netdev,
  1009. int pri)
  1010. {
  1011. return zd_mac_tx(zd_netdev_mac(netdev), txb, pri);
  1012. }
  1013. static void set_security(struct net_device *netdev,
  1014. struct ieee80211_security *sec)
  1015. {
  1016. struct ieee80211_device *ieee = zd_netdev_ieee80211(netdev);
  1017. struct ieee80211_security *secinfo = &ieee->sec;
  1018. int keyidx;
  1019. dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)), "\n");
  1020. for (keyidx = 0; keyidx<WEP_KEYS; keyidx++)
  1021. if (sec->flags & (1<<keyidx)) {
  1022. secinfo->encode_alg[keyidx] = sec->encode_alg[keyidx];
  1023. secinfo->key_sizes[keyidx] = sec->key_sizes[keyidx];
  1024. memcpy(secinfo->keys[keyidx], sec->keys[keyidx],
  1025. SCM_KEY_LEN);
  1026. }
  1027. if (sec->flags & SEC_ACTIVE_KEY) {
  1028. secinfo->active_key = sec->active_key;
  1029. dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
  1030. " .active_key = %d\n", sec->active_key);
  1031. }
  1032. if (sec->flags & SEC_UNICAST_GROUP) {
  1033. secinfo->unicast_uses_group = sec->unicast_uses_group;
  1034. dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
  1035. " .unicast_uses_group = %d\n",
  1036. sec->unicast_uses_group);
  1037. }
  1038. if (sec->flags & SEC_LEVEL) {
  1039. secinfo->level = sec->level;
  1040. dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
  1041. " .level = %d\n", sec->level);
  1042. }
  1043. if (sec->flags & SEC_ENABLED) {
  1044. secinfo->enabled = sec->enabled;
  1045. dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
  1046. " .enabled = %d\n", sec->enabled);
  1047. }
  1048. if (sec->flags & SEC_ENCRYPT) {
  1049. secinfo->encrypt = sec->encrypt;
  1050. dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
  1051. " .encrypt = %d\n", sec->encrypt);
  1052. }
  1053. if (sec->flags & SEC_AUTH_MODE) {
  1054. secinfo->auth_mode = sec->auth_mode;
  1055. dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
  1056. " .auth_mode = %d\n", sec->auth_mode);
  1057. }
  1058. }
  1059. static void ieee_init(struct ieee80211_device *ieee)
  1060. {
  1061. ieee->mode = IEEE_B | IEEE_G;
  1062. ieee->freq_band = IEEE80211_24GHZ_BAND;
  1063. ieee->modulation = IEEE80211_OFDM_MODULATION | IEEE80211_CCK_MODULATION;
  1064. ieee->tx_headroom = sizeof(struct zd_ctrlset);
  1065. ieee->set_security = set_security;
  1066. ieee->hard_start_xmit = netdev_tx;
  1067. /* Software encryption/decryption for now */
  1068. ieee->host_build_iv = 0;
  1069. ieee->host_encrypt = 1;
  1070. ieee->host_decrypt = 1;
  1071. /* FIXME: default to managed mode, until ieee80211 and zd1211rw can
  1072. * correctly support AUTO */
  1073. ieee->iw_mode = IW_MODE_INFRA;
  1074. }
  1075. static void softmac_init(struct ieee80211softmac_device *sm)
  1076. {
  1077. sm->set_channel = set_channel;
  1078. sm->bssinfo_change = bssinfo_change;
  1079. }
  1080. struct iw_statistics *zd_mac_get_wireless_stats(struct net_device *ndev)
  1081. {
  1082. struct zd_mac *mac = zd_netdev_mac(ndev);
  1083. struct iw_statistics *iw_stats = &mac->iw_stats;
  1084. unsigned int i, count, qual_total, rssi_total;
  1085. memset(iw_stats, 0, sizeof(struct iw_statistics));
  1086. /* We are not setting the status, because ieee->state is not updated
  1087. * at all and this driver doesn't track authentication state.
  1088. */
  1089. spin_lock_irq(&mac->lock);
  1090. count = mac->stats_count < ZD_MAC_STATS_BUFFER_SIZE ?
  1091. mac->stats_count : ZD_MAC_STATS_BUFFER_SIZE;
  1092. qual_total = rssi_total = 0;
  1093. for (i = 0; i < count; i++) {
  1094. qual_total += mac->qual_buffer[i];
  1095. rssi_total += mac->rssi_buffer[i];
  1096. }
  1097. spin_unlock_irq(&mac->lock);
  1098. iw_stats->qual.updated = IW_QUAL_NOISE_INVALID;
  1099. if (count > 0) {
  1100. iw_stats->qual.qual = qual_total / count;
  1101. iw_stats->qual.level = rssi_total / count;
  1102. iw_stats->qual.updated |=
  1103. IW_QUAL_QUAL_UPDATED|IW_QUAL_LEVEL_UPDATED;
  1104. } else {
  1105. iw_stats->qual.updated |=
  1106. IW_QUAL_QUAL_INVALID|IW_QUAL_LEVEL_INVALID;
  1107. }
  1108. /* TODO: update counter */
  1109. return iw_stats;
  1110. }
  1111. #define LINK_LED_WORK_DELAY HZ
  1112. static void link_led_handler(struct work_struct *work)
  1113. {
  1114. struct zd_mac *mac =
  1115. container_of(work, struct zd_mac, housekeeping.link_led_work.work);
  1116. struct zd_chip *chip = &mac->chip;
  1117. struct ieee80211softmac_device *sm = ieee80211_priv(mac->netdev);
  1118. int is_associated;
  1119. int r;
  1120. spin_lock_irq(&mac->lock);
  1121. is_associated = sm->associnfo.associated != 0;
  1122. spin_unlock_irq(&mac->lock);
  1123. r = zd_chip_control_leds(chip,
  1124. is_associated ? LED_ASSOCIATED : LED_SCANNING);
  1125. if (r)
  1126. dev_err(zd_mac_dev(mac), "zd_chip_control_leds error %d\n", r);
  1127. queue_delayed_work(zd_workqueue, &mac->housekeeping.link_led_work,
  1128. LINK_LED_WORK_DELAY);
  1129. }
  1130. static void housekeeping_init(struct zd_mac *mac)
  1131. {
  1132. INIT_DELAYED_WORK(&mac->housekeeping.link_led_work, link_led_handler);
  1133. }
  1134. static void housekeeping_enable(struct zd_mac *mac)
  1135. {
  1136. dev_dbg_f(zd_mac_dev(mac), "\n");
  1137. queue_delayed_work(zd_workqueue, &mac->housekeeping.link_led_work,
  1138. 0);
  1139. }
  1140. static void housekeeping_disable(struct zd_mac *mac)
  1141. {
  1142. dev_dbg_f(zd_mac_dev(mac), "\n");
  1143. cancel_rearming_delayed_workqueue(zd_workqueue,
  1144. &mac->housekeeping.link_led_work);
  1145. zd_chip_control_leds(&mac->chip, LED_OFF);
  1146. }