p54common.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036
  1. /*
  2. * Common code for mac80211 Prism54 drivers
  3. *
  4. * Copyright (c) 2006, Michael Wu <flamingice@sourmilk.net>
  5. * Copyright (c) 2007, Christian Lamparter <chunkeey@web.de>
  6. *
  7. * Based on the islsm (softmac prism54) driver, which is:
  8. * Copyright 2004-2006 Jean-Baptiste Note <jbnote@gmail.com>, et al.
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  13. */
  14. #include <linux/init.h>
  15. #include <linux/firmware.h>
  16. #include <linux/etherdevice.h>
  17. #include <net/mac80211.h>
  18. #include "p54.h"
  19. #include "p54common.h"
  20. MODULE_AUTHOR("Michael Wu <flamingice@sourmilk.net>");
  21. MODULE_DESCRIPTION("Softmac Prism54 common code");
  22. MODULE_LICENSE("GPL");
  23. MODULE_ALIAS("prism54common");
  24. static struct ieee80211_rate p54_rates[] = {
  25. { .bitrate = 10, .hw_value = 0, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  26. { .bitrate = 20, .hw_value = 1, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  27. { .bitrate = 55, .hw_value = 2, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  28. { .bitrate = 110, .hw_value = 3, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  29. { .bitrate = 60, .hw_value = 4, },
  30. { .bitrate = 90, .hw_value = 5, },
  31. { .bitrate = 120, .hw_value = 6, },
  32. { .bitrate = 180, .hw_value = 7, },
  33. { .bitrate = 240, .hw_value = 8, },
  34. { .bitrate = 360, .hw_value = 9, },
  35. { .bitrate = 480, .hw_value = 10, },
  36. { .bitrate = 540, .hw_value = 11, },
  37. };
  38. static struct ieee80211_channel p54_channels[] = {
  39. { .center_freq = 2412, .hw_value = 1, },
  40. { .center_freq = 2417, .hw_value = 2, },
  41. { .center_freq = 2422, .hw_value = 3, },
  42. { .center_freq = 2427, .hw_value = 4, },
  43. { .center_freq = 2432, .hw_value = 5, },
  44. { .center_freq = 2437, .hw_value = 6, },
  45. { .center_freq = 2442, .hw_value = 7, },
  46. { .center_freq = 2447, .hw_value = 8, },
  47. { .center_freq = 2452, .hw_value = 9, },
  48. { .center_freq = 2457, .hw_value = 10, },
  49. { .center_freq = 2462, .hw_value = 11, },
  50. { .center_freq = 2467, .hw_value = 12, },
  51. { .center_freq = 2472, .hw_value = 13, },
  52. { .center_freq = 2484, .hw_value = 14, },
  53. };
  54. static struct ieee80211_supported_band band_2GHz = {
  55. .channels = p54_channels,
  56. .n_channels = ARRAY_SIZE(p54_channels),
  57. .bitrates = p54_rates,
  58. .n_bitrates = ARRAY_SIZE(p54_rates),
  59. };
  60. void p54_parse_firmware(struct ieee80211_hw *dev, const struct firmware *fw)
  61. {
  62. struct p54_common *priv = dev->priv;
  63. struct bootrec_exp_if *exp_if;
  64. struct bootrec *bootrec;
  65. u32 *data = (u32 *)fw->data;
  66. u32 *end_data = (u32 *)fw->data + (fw->size >> 2);
  67. u8 *fw_version = NULL;
  68. size_t len;
  69. int i;
  70. if (priv->rx_start)
  71. return;
  72. while (data < end_data && *data)
  73. data++;
  74. while (data < end_data && !*data)
  75. data++;
  76. bootrec = (struct bootrec *) data;
  77. while (bootrec->data <= end_data &&
  78. (bootrec->data + (len = le32_to_cpu(bootrec->len))) <= end_data) {
  79. u32 code = le32_to_cpu(bootrec->code);
  80. switch (code) {
  81. case BR_CODE_COMPONENT_ID:
  82. switch (be32_to_cpu(*(__be32 *)bootrec->data)) {
  83. case FW_FMAC:
  84. printk(KERN_INFO "p54: FreeMAC firmware\n");
  85. break;
  86. case FW_LM20:
  87. printk(KERN_INFO "p54: LM20 firmware\n");
  88. break;
  89. case FW_LM86:
  90. printk(KERN_INFO "p54: LM86 firmware\n");
  91. break;
  92. case FW_LM87:
  93. printk(KERN_INFO "p54: LM87 firmware - not supported yet!\n");
  94. break;
  95. default:
  96. printk(KERN_INFO "p54: unknown firmware\n");
  97. break;
  98. }
  99. break;
  100. case BR_CODE_COMPONENT_VERSION:
  101. /* 24 bytes should be enough for all firmwares */
  102. if (strnlen((unsigned char*)bootrec->data, 24) < 24)
  103. fw_version = (unsigned char*)bootrec->data;
  104. break;
  105. case BR_CODE_DESCR:
  106. priv->rx_start = le32_to_cpu(((__le32 *)bootrec->data)[1]);
  107. /* FIXME add sanity checking */
  108. priv->rx_end = le32_to_cpu(((__le32 *)bootrec->data)[2]) - 0x3500;
  109. break;
  110. case BR_CODE_EXPOSED_IF:
  111. exp_if = (struct bootrec_exp_if *) bootrec->data;
  112. for (i = 0; i < (len * sizeof(*exp_if) / 4); i++)
  113. if (exp_if[i].if_id == cpu_to_le16(0x1a))
  114. priv->fw_var = le16_to_cpu(exp_if[i].variant);
  115. break;
  116. case BR_CODE_DEPENDENT_IF:
  117. break;
  118. case BR_CODE_END_OF_BRA:
  119. case LEGACY_BR_CODE_END_OF_BRA:
  120. end_data = NULL;
  121. break;
  122. default:
  123. break;
  124. }
  125. bootrec = (struct bootrec *)&bootrec->data[len];
  126. }
  127. if (fw_version)
  128. printk(KERN_INFO "p54: FW rev %s - Softmac protocol %x.%x\n",
  129. fw_version, priv->fw_var >> 8, priv->fw_var & 0xff);
  130. if (priv->fw_var >= 0x300) {
  131. /* Firmware supports QoS, use it! */
  132. priv->tx_stats[0].limit = 3;
  133. priv->tx_stats[1].limit = 4;
  134. priv->tx_stats[2].limit = 3;
  135. priv->tx_stats[3].limit = 1;
  136. dev->queues = 4;
  137. }
  138. }
  139. EXPORT_SYMBOL_GPL(p54_parse_firmware);
  140. static int p54_convert_rev0_to_rev1(struct ieee80211_hw *dev,
  141. struct pda_pa_curve_data *curve_data)
  142. {
  143. struct p54_common *priv = dev->priv;
  144. struct pda_pa_curve_data_sample_rev1 *rev1;
  145. struct pda_pa_curve_data_sample_rev0 *rev0;
  146. size_t cd_len = sizeof(*curve_data) +
  147. (curve_data->points_per_channel*sizeof(*rev1) + 2) *
  148. curve_data->channels;
  149. unsigned int i, j;
  150. void *source, *target;
  151. priv->curve_data = kmalloc(cd_len, GFP_KERNEL);
  152. if (!priv->curve_data)
  153. return -ENOMEM;
  154. memcpy(priv->curve_data, curve_data, sizeof(*curve_data));
  155. source = curve_data->data;
  156. target = priv->curve_data->data;
  157. for (i = 0; i < curve_data->channels; i++) {
  158. __le16 *freq = source;
  159. source += sizeof(__le16);
  160. *((__le16 *)target) = *freq;
  161. target += sizeof(__le16);
  162. for (j = 0; j < curve_data->points_per_channel; j++) {
  163. rev1 = target;
  164. rev0 = source;
  165. rev1->rf_power = rev0->rf_power;
  166. rev1->pa_detector = rev0->pa_detector;
  167. rev1->data_64qam = rev0->pcv;
  168. /* "invent" the points for the other modulations */
  169. #define SUB(x,y) (u8)((x) - (y)) > (x) ? 0 : (x) - (y)
  170. rev1->data_16qam = SUB(rev0->pcv, 12);
  171. rev1->data_qpsk = SUB(rev1->data_16qam, 12);
  172. rev1->data_bpsk = SUB(rev1->data_qpsk, 12);
  173. rev1->data_barker= SUB(rev1->data_bpsk, 14);
  174. #undef SUB
  175. target += sizeof(*rev1);
  176. source += sizeof(*rev0);
  177. }
  178. }
  179. return 0;
  180. }
  181. int p54_parse_eeprom(struct ieee80211_hw *dev, void *eeprom, int len)
  182. {
  183. struct p54_common *priv = dev->priv;
  184. struct eeprom_pda_wrap *wrap = NULL;
  185. struct pda_entry *entry;
  186. unsigned int data_len, entry_len;
  187. void *tmp;
  188. int err;
  189. u8 *end = (u8 *)eeprom + len;
  190. wrap = (struct eeprom_pda_wrap *) eeprom;
  191. entry = (void *)wrap->data + le16_to_cpu(wrap->len);
  192. /* verify that at least the entry length/code fits */
  193. while ((u8 *)entry <= end - sizeof(*entry)) {
  194. entry_len = le16_to_cpu(entry->len);
  195. data_len = ((entry_len - 1) << 1);
  196. /* abort if entry exceeds whole structure */
  197. if ((u8 *)entry + sizeof(*entry) + data_len > end)
  198. break;
  199. switch (le16_to_cpu(entry->code)) {
  200. case PDR_MAC_ADDRESS:
  201. SET_IEEE80211_PERM_ADDR(dev, entry->data);
  202. break;
  203. case PDR_PRISM_PA_CAL_OUTPUT_POWER_LIMITS:
  204. if (data_len < 2) {
  205. err = -EINVAL;
  206. goto err;
  207. }
  208. if (2 + entry->data[1]*sizeof(*priv->output_limit) > data_len) {
  209. err = -EINVAL;
  210. goto err;
  211. }
  212. priv->output_limit = kmalloc(entry->data[1] *
  213. sizeof(*priv->output_limit), GFP_KERNEL);
  214. if (!priv->output_limit) {
  215. err = -ENOMEM;
  216. goto err;
  217. }
  218. memcpy(priv->output_limit, &entry->data[2],
  219. entry->data[1]*sizeof(*priv->output_limit));
  220. priv->output_limit_len = entry->data[1];
  221. break;
  222. case PDR_PRISM_PA_CAL_CURVE_DATA:
  223. if (data_len < sizeof(struct pda_pa_curve_data)) {
  224. err = -EINVAL;
  225. goto err;
  226. }
  227. if (((struct pda_pa_curve_data *)entry->data)->cal_method_rev) {
  228. priv->curve_data = kmalloc(data_len, GFP_KERNEL);
  229. if (!priv->curve_data) {
  230. err = -ENOMEM;
  231. goto err;
  232. }
  233. memcpy(priv->curve_data, entry->data, data_len);
  234. } else {
  235. err = p54_convert_rev0_to_rev1(dev, (struct pda_pa_curve_data *)entry->data);
  236. if (err)
  237. goto err;
  238. }
  239. break;
  240. case PDR_PRISM_ZIF_TX_IQ_CALIBRATION:
  241. priv->iq_autocal = kmalloc(data_len, GFP_KERNEL);
  242. if (!priv->iq_autocal) {
  243. err = -ENOMEM;
  244. goto err;
  245. }
  246. memcpy(priv->iq_autocal, entry->data, data_len);
  247. priv->iq_autocal_len = data_len / sizeof(struct pda_iq_autocal_entry);
  248. break;
  249. case PDR_INTERFACE_LIST:
  250. tmp = entry->data;
  251. while ((u8 *)tmp < entry->data + data_len) {
  252. struct bootrec_exp_if *exp_if = tmp;
  253. if (le16_to_cpu(exp_if->if_id) == 0xF)
  254. priv->rxhw = exp_if->variant & cpu_to_le16(0x07);
  255. tmp += sizeof(struct bootrec_exp_if);
  256. }
  257. break;
  258. case PDR_HARDWARE_PLATFORM_COMPONENT_ID:
  259. priv->version = *(u8 *)(entry->data + 1);
  260. break;
  261. case PDR_END:
  262. /* make it overrun */
  263. entry_len = len;
  264. break;
  265. default:
  266. printk(KERN_INFO "p54: unknown eeprom code : 0x%x\n",
  267. le16_to_cpu(entry->code));
  268. break;
  269. }
  270. entry = (void *)entry + (entry_len + 1)*2;
  271. }
  272. if (!priv->iq_autocal || !priv->output_limit || !priv->curve_data) {
  273. printk(KERN_ERR "p54: not all required entries found in eeprom!\n");
  274. err = -EINVAL;
  275. goto err;
  276. }
  277. return 0;
  278. err:
  279. if (priv->iq_autocal) {
  280. kfree(priv->iq_autocal);
  281. priv->iq_autocal = NULL;
  282. }
  283. if (priv->output_limit) {
  284. kfree(priv->output_limit);
  285. priv->output_limit = NULL;
  286. }
  287. if (priv->curve_data) {
  288. kfree(priv->curve_data);
  289. priv->curve_data = NULL;
  290. }
  291. printk(KERN_ERR "p54: eeprom parse failed!\n");
  292. return err;
  293. }
  294. EXPORT_SYMBOL_GPL(p54_parse_eeprom);
  295. void p54_fill_eeprom_readback(struct p54_control_hdr *hdr)
  296. {
  297. struct p54_eeprom_lm86 *eeprom_hdr;
  298. hdr->magic1 = cpu_to_le16(0x8000);
  299. hdr->len = cpu_to_le16(sizeof(*eeprom_hdr) + 0x2000);
  300. hdr->type = cpu_to_le16(P54_CONTROL_TYPE_EEPROM_READBACK);
  301. hdr->retry1 = hdr->retry2 = 0;
  302. eeprom_hdr = (struct p54_eeprom_lm86 *) hdr->data;
  303. eeprom_hdr->offset = 0x0;
  304. eeprom_hdr->len = cpu_to_le16(0x2000);
  305. }
  306. EXPORT_SYMBOL_GPL(p54_fill_eeprom_readback);
  307. static void p54_rx_data(struct ieee80211_hw *dev, struct sk_buff *skb)
  308. {
  309. struct p54_rx_hdr *hdr = (struct p54_rx_hdr *) skb->data;
  310. struct ieee80211_rx_status rx_status = {0};
  311. u16 freq = le16_to_cpu(hdr->freq);
  312. rx_status.signal = hdr->rssi;
  313. /* XX correct? */
  314. rx_status.rate_idx = hdr->rate & 0xf;
  315. rx_status.freq = freq;
  316. rx_status.band = IEEE80211_BAND_2GHZ;
  317. rx_status.antenna = hdr->antenna;
  318. rx_status.mactime = le64_to_cpu(hdr->timestamp);
  319. rx_status.flag |= RX_FLAG_TSFT;
  320. skb_pull(skb, sizeof(*hdr));
  321. skb_trim(skb, le16_to_cpu(hdr->len));
  322. ieee80211_rx_irqsafe(dev, skb, &rx_status);
  323. }
  324. static void inline p54_wake_free_queues(struct ieee80211_hw *dev)
  325. {
  326. struct p54_common *priv = dev->priv;
  327. int i;
  328. for (i = 0; i < dev->queues; i++)
  329. if (priv->tx_stats[i].len < priv->tx_stats[i].limit)
  330. ieee80211_wake_queue(dev, i);
  331. }
  332. static void p54_rx_frame_sent(struct ieee80211_hw *dev, struct sk_buff *skb)
  333. {
  334. struct p54_common *priv = dev->priv;
  335. struct p54_control_hdr *hdr = (struct p54_control_hdr *) skb->data;
  336. struct p54_frame_sent_hdr *payload = (struct p54_frame_sent_hdr *) hdr->data;
  337. struct sk_buff *entry = (struct sk_buff *) priv->tx_queue.next;
  338. u32 addr = le32_to_cpu(hdr->req_id) - 0x70;
  339. struct memrecord *range = NULL;
  340. u32 freed = 0;
  341. u32 last_addr = priv->rx_start;
  342. while (entry != (struct sk_buff *)&priv->tx_queue) {
  343. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(entry);
  344. range = (void *)info->driver_data;
  345. if (range->start_addr == addr) {
  346. struct p54_control_hdr *entry_hdr;
  347. struct p54_tx_control_allocdata *entry_data;
  348. int pad = 0;
  349. if (entry->next != (struct sk_buff *)&priv->tx_queue) {
  350. struct ieee80211_tx_info *ni;
  351. struct memrecord *mr;
  352. ni = IEEE80211_SKB_CB(entry->next);
  353. mr = (struct memrecord *)ni->driver_data;
  354. freed = mr->start_addr - last_addr;
  355. } else
  356. freed = priv->rx_end - last_addr;
  357. last_addr = range->end_addr;
  358. __skb_unlink(entry, &priv->tx_queue);
  359. memset(&info->status, 0, sizeof(info->status));
  360. priv->tx_stats[skb_get_queue_mapping(skb)].len--;
  361. entry_hdr = (struct p54_control_hdr *) entry->data;
  362. entry_data = (struct p54_tx_control_allocdata *) entry_hdr->data;
  363. if ((entry_hdr->magic1 & cpu_to_le16(0x4000)) != 0)
  364. pad = entry_data->align[0];
  365. if (!(info->flags & IEEE80211_TX_CTL_NO_ACK)) {
  366. if (!(payload->status & 0x01))
  367. info->flags |= IEEE80211_TX_STAT_ACK;
  368. else
  369. info->status.excessive_retries = 1;
  370. }
  371. info->status.retry_count = payload->retries - 1;
  372. info->status.ack_signal = le16_to_cpu(payload->ack_rssi);
  373. skb_pull(entry, sizeof(*hdr) + pad + sizeof(*entry_data));
  374. ieee80211_tx_status_irqsafe(dev, entry);
  375. break;
  376. } else
  377. last_addr = range->end_addr;
  378. entry = entry->next;
  379. }
  380. if (freed >= IEEE80211_MAX_RTS_THRESHOLD + 0x170 +
  381. sizeof(struct p54_control_hdr))
  382. p54_wake_free_queues(dev);
  383. }
  384. static void p54_rx_control(struct ieee80211_hw *dev, struct sk_buff *skb)
  385. {
  386. struct p54_control_hdr *hdr = (struct p54_control_hdr *) skb->data;
  387. switch (le16_to_cpu(hdr->type)) {
  388. case P54_CONTROL_TYPE_TXDONE:
  389. p54_rx_frame_sent(dev, skb);
  390. break;
  391. case P54_CONTROL_TYPE_BBP:
  392. break;
  393. default:
  394. printk(KERN_DEBUG "%s: not handling 0x%02x type control frame\n",
  395. wiphy_name(dev->wiphy), le16_to_cpu(hdr->type));
  396. break;
  397. }
  398. }
  399. /* returns zero if skb can be reused */
  400. int p54_rx(struct ieee80211_hw *dev, struct sk_buff *skb)
  401. {
  402. u8 type = le16_to_cpu(*((__le16 *)skb->data)) >> 8;
  403. switch (type) {
  404. case 0x00:
  405. case 0x01:
  406. p54_rx_data(dev, skb);
  407. return -1;
  408. case 0x4d:
  409. /* TODO: do something better... but then again, I've never seen this happen */
  410. printk(KERN_ERR "%s: Received fault. Probably need to restart hardware now..\n",
  411. wiphy_name(dev->wiphy));
  412. break;
  413. case 0x80:
  414. p54_rx_control(dev, skb);
  415. break;
  416. default:
  417. printk(KERN_ERR "%s: unknown frame RXed (0x%02x)\n",
  418. wiphy_name(dev->wiphy), type);
  419. break;
  420. }
  421. return 0;
  422. }
  423. EXPORT_SYMBOL_GPL(p54_rx);
  424. /*
  425. * So, the firmware is somewhat stupid and doesn't know what places in its
  426. * memory incoming data should go to. By poking around in the firmware, we
  427. * can find some unused memory to upload our packets to. However, data that we
  428. * want the card to TX needs to stay intact until the card has told us that
  429. * it is done with it. This function finds empty places we can upload to and
  430. * marks allocated areas as reserved if necessary. p54_rx_frame_sent frees
  431. * allocated areas.
  432. */
  433. static void p54_assign_address(struct ieee80211_hw *dev, struct sk_buff *skb,
  434. struct p54_control_hdr *data, u32 len)
  435. {
  436. struct p54_common *priv = dev->priv;
  437. struct sk_buff *entry = priv->tx_queue.next;
  438. struct sk_buff *target_skb = NULL;
  439. u32 last_addr = priv->rx_start;
  440. u32 largest_hole = 0;
  441. u32 target_addr = priv->rx_start;
  442. unsigned long flags;
  443. unsigned int left;
  444. len = (len + 0x170 + 3) & ~0x3; /* 0x70 headroom, 0x100 tailroom */
  445. spin_lock_irqsave(&priv->tx_queue.lock, flags);
  446. left = skb_queue_len(&priv->tx_queue);
  447. while (left--) {
  448. u32 hole_size;
  449. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(entry);
  450. struct memrecord *range = (void *)info->driver_data;
  451. hole_size = range->start_addr - last_addr;
  452. if (!target_skb && hole_size >= len) {
  453. target_skb = entry->prev;
  454. hole_size -= len;
  455. target_addr = last_addr;
  456. }
  457. largest_hole = max(largest_hole, hole_size);
  458. last_addr = range->end_addr;
  459. entry = entry->next;
  460. }
  461. if (!target_skb && priv->rx_end - last_addr >= len) {
  462. target_skb = priv->tx_queue.prev;
  463. largest_hole = max(largest_hole, priv->rx_end - last_addr - len);
  464. if (!skb_queue_empty(&priv->tx_queue)) {
  465. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(target_skb);
  466. struct memrecord *range = (void *)info->driver_data;
  467. target_addr = range->end_addr;
  468. }
  469. } else
  470. largest_hole = max(largest_hole, priv->rx_end - last_addr);
  471. if (skb) {
  472. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  473. struct memrecord *range = (void *)info->driver_data;
  474. range->start_addr = target_addr;
  475. range->end_addr = target_addr + len;
  476. __skb_queue_after(&priv->tx_queue, target_skb, skb);
  477. if (largest_hole < IEEE80211_MAX_RTS_THRESHOLD + 0x170 +
  478. sizeof(struct p54_control_hdr))
  479. ieee80211_stop_queues(dev);
  480. }
  481. spin_unlock_irqrestore(&priv->tx_queue.lock, flags);
  482. data->req_id = cpu_to_le32(target_addr + 0x70);
  483. }
  484. static int p54_tx(struct ieee80211_hw *dev, struct sk_buff *skb)
  485. {
  486. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  487. struct ieee80211_tx_queue_stats *current_queue;
  488. struct p54_common *priv = dev->priv;
  489. struct p54_control_hdr *hdr;
  490. struct p54_tx_control_allocdata *txhdr;
  491. size_t padding, len;
  492. u8 rate;
  493. current_queue = &priv->tx_stats[skb_get_queue_mapping(skb)];
  494. if (unlikely(current_queue->len > current_queue->limit))
  495. return NETDEV_TX_BUSY;
  496. current_queue->len++;
  497. current_queue->count++;
  498. if (current_queue->len == current_queue->limit)
  499. ieee80211_stop_queue(dev, skb_get_queue_mapping(skb));
  500. padding = (unsigned long)(skb->data - (sizeof(*hdr) + sizeof(*txhdr))) & 3;
  501. len = skb->len;
  502. txhdr = (struct p54_tx_control_allocdata *)
  503. skb_push(skb, sizeof(*txhdr) + padding);
  504. hdr = (struct p54_control_hdr *) skb_push(skb, sizeof(*hdr));
  505. if (padding)
  506. hdr->magic1 = cpu_to_le16(0x4010);
  507. else
  508. hdr->magic1 = cpu_to_le16(0x0010);
  509. hdr->len = cpu_to_le16(len);
  510. hdr->type = (info->flags & IEEE80211_TX_CTL_NO_ACK) ? 0 : cpu_to_le16(1);
  511. hdr->retry1 = hdr->retry2 = info->control.retry_limit;
  512. memset(txhdr->wep_key, 0x0, 16);
  513. txhdr->padding = 0;
  514. txhdr->padding2 = 0;
  515. /* TODO: add support for alternate retry TX rates */
  516. rate = ieee80211_get_tx_rate(dev, info)->hw_value;
  517. if (info->flags & IEEE80211_TX_CTL_SHORT_PREAMBLE)
  518. rate |= 0x10;
  519. if (info->flags & IEEE80211_TX_CTL_USE_RTS_CTS)
  520. rate |= 0x40;
  521. else if (info->flags & IEEE80211_TX_CTL_USE_CTS_PROTECT)
  522. rate |= 0x20;
  523. memset(txhdr->rateset, rate, 8);
  524. txhdr->wep_key_present = 0;
  525. txhdr->wep_key_len = 0;
  526. txhdr->frame_type = cpu_to_le32(skb_get_queue_mapping(skb) + 4);
  527. txhdr->magic4 = 0;
  528. txhdr->antenna = (info->antenna_sel_tx == 0) ?
  529. 2 : info->antenna_sel_tx - 1;
  530. txhdr->output_power = 0x7f; // HW Maximum
  531. txhdr->magic5 = (info->flags & IEEE80211_TX_CTL_NO_ACK) ?
  532. 0 : ((rate > 0x3) ? cpu_to_le32(0x33) : cpu_to_le32(0x23));
  533. if (padding)
  534. txhdr->align[0] = padding;
  535. /* modifies skb->cb and with it info, so must be last! */
  536. p54_assign_address(dev, skb, hdr, skb->len);
  537. priv->tx(dev, hdr, skb->len, 0);
  538. return 0;
  539. }
  540. static int p54_set_filter(struct ieee80211_hw *dev, u16 filter_type,
  541. const u8 *dst, const u8 *src, u8 antenna,
  542. u32 magic3, u32 magic8, u32 magic9)
  543. {
  544. struct p54_common *priv = dev->priv;
  545. struct p54_control_hdr *hdr;
  546. struct p54_tx_control_filter *filter;
  547. hdr = kzalloc(sizeof(*hdr) + sizeof(*filter) +
  548. priv->tx_hdr_len, GFP_ATOMIC);
  549. if (!hdr)
  550. return -ENOMEM;
  551. hdr = (void *)hdr + priv->tx_hdr_len;
  552. filter = (struct p54_tx_control_filter *) hdr->data;
  553. hdr->magic1 = cpu_to_le16(0x8001);
  554. hdr->len = cpu_to_le16(sizeof(*filter));
  555. p54_assign_address(dev, NULL, hdr, sizeof(*hdr) + sizeof(*filter));
  556. hdr->type = cpu_to_le16(P54_CONTROL_TYPE_FILTER_SET);
  557. filter->filter_type = cpu_to_le16(filter_type);
  558. memcpy(filter->dst, dst, ETH_ALEN);
  559. if (!src)
  560. memset(filter->src, ~0, ETH_ALEN);
  561. else
  562. memcpy(filter->src, src, ETH_ALEN);
  563. filter->antenna = antenna;
  564. filter->magic3 = cpu_to_le32(magic3);
  565. filter->rx_addr = cpu_to_le32(priv->rx_end);
  566. filter->max_rx = cpu_to_le16(0x0620); /* FIXME: for usb ver 1.. maybe */
  567. filter->rxhw = priv->rxhw;
  568. filter->magic8 = cpu_to_le16(magic8);
  569. filter->magic9 = cpu_to_le16(magic9);
  570. priv->tx(dev, hdr, sizeof(*hdr) + sizeof(*filter), 1);
  571. return 0;
  572. }
  573. static int p54_set_freq(struct ieee80211_hw *dev, __le16 freq)
  574. {
  575. struct p54_common *priv = dev->priv;
  576. struct p54_control_hdr *hdr;
  577. struct p54_tx_control_channel *chan;
  578. unsigned int i;
  579. size_t payload_len = sizeof(*chan) + sizeof(u32)*2 +
  580. sizeof(*chan->curve_data) *
  581. priv->curve_data->points_per_channel;
  582. void *entry;
  583. hdr = kzalloc(sizeof(*hdr) + payload_len +
  584. priv->tx_hdr_len, GFP_KERNEL);
  585. if (!hdr)
  586. return -ENOMEM;
  587. hdr = (void *)hdr + priv->tx_hdr_len;
  588. chan = (struct p54_tx_control_channel *) hdr->data;
  589. hdr->magic1 = cpu_to_le16(0x8001);
  590. hdr->len = cpu_to_le16(sizeof(*chan));
  591. hdr->type = cpu_to_le16(P54_CONTROL_TYPE_CHANNEL_CHANGE);
  592. p54_assign_address(dev, NULL, hdr, sizeof(*hdr) + payload_len);
  593. chan->magic1 = cpu_to_le16(0x1);
  594. chan->magic2 = cpu_to_le16(0x0);
  595. for (i = 0; i < priv->iq_autocal_len; i++) {
  596. if (priv->iq_autocal[i].freq != freq)
  597. continue;
  598. memcpy(&chan->iq_autocal, &priv->iq_autocal[i],
  599. sizeof(*priv->iq_autocal));
  600. break;
  601. }
  602. if (i == priv->iq_autocal_len)
  603. goto err;
  604. for (i = 0; i < priv->output_limit_len; i++) {
  605. if (priv->output_limit[i].freq != freq)
  606. continue;
  607. chan->val_barker = 0x38;
  608. chan->val_bpsk = priv->output_limit[i].val_bpsk;
  609. chan->val_qpsk = priv->output_limit[i].val_qpsk;
  610. chan->val_16qam = priv->output_limit[i].val_16qam;
  611. chan->val_64qam = priv->output_limit[i].val_64qam;
  612. break;
  613. }
  614. if (i == priv->output_limit_len)
  615. goto err;
  616. chan->pa_points_per_curve = priv->curve_data->points_per_channel;
  617. entry = priv->curve_data->data;
  618. for (i = 0; i < priv->curve_data->channels; i++) {
  619. if (*((__le16 *)entry) != freq) {
  620. entry += sizeof(__le16);
  621. entry += sizeof(struct pda_pa_curve_data_sample_rev1) *
  622. chan->pa_points_per_curve;
  623. continue;
  624. }
  625. entry += sizeof(__le16);
  626. memcpy(chan->curve_data, entry, sizeof(*chan->curve_data) *
  627. chan->pa_points_per_curve);
  628. break;
  629. }
  630. memcpy(hdr->data + payload_len - 4, &chan->val_bpsk, 4);
  631. priv->tx(dev, hdr, sizeof(*hdr) + payload_len, 1);
  632. return 0;
  633. err:
  634. printk(KERN_ERR "%s: frequency change failed\n", wiphy_name(dev->wiphy));
  635. kfree(hdr);
  636. return -EINVAL;
  637. }
  638. static int p54_set_leds(struct ieee80211_hw *dev, int mode, int link, int act)
  639. {
  640. struct p54_common *priv = dev->priv;
  641. struct p54_control_hdr *hdr;
  642. struct p54_tx_control_led *led;
  643. hdr = kzalloc(sizeof(*hdr) + sizeof(*led) +
  644. priv->tx_hdr_len, GFP_KERNEL);
  645. if (!hdr)
  646. return -ENOMEM;
  647. hdr = (void *)hdr + priv->tx_hdr_len;
  648. hdr->magic1 = cpu_to_le16(0x8001);
  649. hdr->len = cpu_to_le16(sizeof(*led));
  650. hdr->type = cpu_to_le16(P54_CONTROL_TYPE_LED);
  651. p54_assign_address(dev, NULL, hdr, sizeof(*hdr) + sizeof(*led));
  652. led = (struct p54_tx_control_led *) hdr->data;
  653. led->mode = cpu_to_le16(mode);
  654. led->led_permanent = cpu_to_le16(link);
  655. led->led_temporary = cpu_to_le16(act);
  656. led->duration = cpu_to_le16(1000);
  657. priv->tx(dev, hdr, sizeof(*hdr) + sizeof(*led), 1);
  658. return 0;
  659. }
  660. #define P54_SET_QUEUE(queue, ai_fs, cw_min, cw_max, _txop) \
  661. do { \
  662. queue.aifs = cpu_to_le16(ai_fs); \
  663. queue.cwmin = cpu_to_le16(cw_min); \
  664. queue.cwmax = cpu_to_le16(cw_max); \
  665. queue.txop = cpu_to_le16(_txop); \
  666. } while(0)
  667. static void p54_init_vdcf(struct ieee80211_hw *dev)
  668. {
  669. struct p54_common *priv = dev->priv;
  670. struct p54_control_hdr *hdr;
  671. struct p54_tx_control_vdcf *vdcf;
  672. /* all USB V1 adapters need a extra headroom */
  673. hdr = (void *)priv->cached_vdcf + priv->tx_hdr_len;
  674. hdr->magic1 = cpu_to_le16(0x8001);
  675. hdr->len = cpu_to_le16(sizeof(*vdcf));
  676. hdr->type = cpu_to_le16(P54_CONTROL_TYPE_DCFINIT);
  677. hdr->req_id = cpu_to_le32(priv->rx_start);
  678. vdcf = (struct p54_tx_control_vdcf *) hdr->data;
  679. P54_SET_QUEUE(vdcf->queue[0], 0x0002, 0x0003, 0x0007, 47);
  680. P54_SET_QUEUE(vdcf->queue[1], 0x0002, 0x0007, 0x000f, 94);
  681. P54_SET_QUEUE(vdcf->queue[2], 0x0003, 0x000f, 0x03ff, 0);
  682. P54_SET_QUEUE(vdcf->queue[3], 0x0007, 0x000f, 0x03ff, 0);
  683. }
  684. static void p54_set_vdcf(struct ieee80211_hw *dev)
  685. {
  686. struct p54_common *priv = dev->priv;
  687. struct p54_control_hdr *hdr;
  688. struct p54_tx_control_vdcf *vdcf;
  689. hdr = (void *)priv->cached_vdcf + priv->tx_hdr_len;
  690. p54_assign_address(dev, NULL, hdr, sizeof(*hdr) + sizeof(*vdcf));
  691. vdcf = (struct p54_tx_control_vdcf *) hdr->data;
  692. if (dev->conf.flags & IEEE80211_CONF_SHORT_SLOT_TIME) {
  693. vdcf->slottime = 9;
  694. vdcf->magic1 = 0x00;
  695. vdcf->magic2 = 0x10;
  696. } else {
  697. vdcf->slottime = 20;
  698. vdcf->magic1 = 0x0a;
  699. vdcf->magic2 = 0x06;
  700. }
  701. /* (see prism54/isl_oid.h for further details) */
  702. vdcf->frameburst = cpu_to_le16(0);
  703. priv->tx(dev, hdr, sizeof(*hdr) + sizeof(*vdcf), 0);
  704. }
  705. static int p54_start(struct ieee80211_hw *dev)
  706. {
  707. struct p54_common *priv = dev->priv;
  708. int err;
  709. err = priv->open(dev);
  710. if (!err)
  711. priv->mode = IEEE80211_IF_TYPE_MNTR;
  712. return err;
  713. }
  714. static void p54_stop(struct ieee80211_hw *dev)
  715. {
  716. struct p54_common *priv = dev->priv;
  717. struct sk_buff *skb;
  718. while ((skb = skb_dequeue(&priv->tx_queue)))
  719. kfree_skb(skb);
  720. priv->stop(dev);
  721. priv->mode = IEEE80211_IF_TYPE_INVALID;
  722. }
  723. static int p54_add_interface(struct ieee80211_hw *dev,
  724. struct ieee80211_if_init_conf *conf)
  725. {
  726. struct p54_common *priv = dev->priv;
  727. if (priv->mode != IEEE80211_IF_TYPE_MNTR)
  728. return -EOPNOTSUPP;
  729. switch (conf->type) {
  730. case IEEE80211_IF_TYPE_STA:
  731. priv->mode = conf->type;
  732. break;
  733. default:
  734. return -EOPNOTSUPP;
  735. }
  736. memcpy(priv->mac_addr, conf->mac_addr, ETH_ALEN);
  737. p54_set_filter(dev, 0, priv->mac_addr, NULL, 0, 1, 0, 0xF642);
  738. p54_set_filter(dev, 0, priv->mac_addr, NULL, 1, 0, 0, 0xF642);
  739. switch (conf->type) {
  740. case IEEE80211_IF_TYPE_STA:
  741. p54_set_filter(dev, 1, priv->mac_addr, NULL, 0, 0x15F, 0x1F4, 0);
  742. break;
  743. default:
  744. BUG(); /* impossible */
  745. break;
  746. }
  747. p54_set_leds(dev, 1, 0, 0);
  748. return 0;
  749. }
  750. static void p54_remove_interface(struct ieee80211_hw *dev,
  751. struct ieee80211_if_init_conf *conf)
  752. {
  753. struct p54_common *priv = dev->priv;
  754. priv->mode = IEEE80211_IF_TYPE_MNTR;
  755. memset(priv->mac_addr, 0, ETH_ALEN);
  756. p54_set_filter(dev, 0, priv->mac_addr, NULL, 2, 0, 0, 0);
  757. }
  758. static int p54_config(struct ieee80211_hw *dev, struct ieee80211_conf *conf)
  759. {
  760. int ret;
  761. ret = p54_set_freq(dev, cpu_to_le16(conf->channel->center_freq));
  762. p54_set_vdcf(dev);
  763. return ret;
  764. }
  765. static int p54_config_interface(struct ieee80211_hw *dev,
  766. struct ieee80211_vif *vif,
  767. struct ieee80211_if_conf *conf)
  768. {
  769. struct p54_common *priv = dev->priv;
  770. p54_set_filter(dev, 0, priv->mac_addr, conf->bssid, 0, 1, 0, 0xF642);
  771. p54_set_filter(dev, 0, priv->mac_addr, conf->bssid, 2, 0, 0, 0);
  772. p54_set_leds(dev, 1, !is_multicast_ether_addr(conf->bssid), 0);
  773. memcpy(priv->bssid, conf->bssid, ETH_ALEN);
  774. return 0;
  775. }
  776. static void p54_configure_filter(struct ieee80211_hw *dev,
  777. unsigned int changed_flags,
  778. unsigned int *total_flags,
  779. int mc_count, struct dev_mc_list *mclist)
  780. {
  781. struct p54_common *priv = dev->priv;
  782. *total_flags &= FIF_BCN_PRBRESP_PROMISC;
  783. if (changed_flags & FIF_BCN_PRBRESP_PROMISC) {
  784. if (*total_flags & FIF_BCN_PRBRESP_PROMISC)
  785. p54_set_filter(dev, 0, priv->mac_addr,
  786. NULL, 2, 0, 0, 0);
  787. else
  788. p54_set_filter(dev, 0, priv->mac_addr,
  789. priv->bssid, 2, 0, 0, 0);
  790. }
  791. }
  792. static int p54_conf_tx(struct ieee80211_hw *dev, u16 queue,
  793. const struct ieee80211_tx_queue_params *params)
  794. {
  795. struct p54_common *priv = dev->priv;
  796. struct p54_tx_control_vdcf *vdcf;
  797. vdcf = (struct p54_tx_control_vdcf *)(((struct p54_control_hdr *)
  798. ((void *)priv->cached_vdcf + priv->tx_hdr_len))->data);
  799. if ((params) && !(queue > 4)) {
  800. P54_SET_QUEUE(vdcf->queue[queue], params->aifs,
  801. params->cw_min, params->cw_max, params->txop);
  802. } else
  803. return -EINVAL;
  804. p54_set_vdcf(dev);
  805. return 0;
  806. }
  807. static int p54_get_stats(struct ieee80211_hw *dev,
  808. struct ieee80211_low_level_stats *stats)
  809. {
  810. /* TODO */
  811. return 0;
  812. }
  813. static int p54_get_tx_stats(struct ieee80211_hw *dev,
  814. struct ieee80211_tx_queue_stats *stats)
  815. {
  816. struct p54_common *priv = dev->priv;
  817. memcpy(stats, &priv->tx_stats, sizeof(stats[0]) * dev->queues);
  818. return 0;
  819. }
  820. static const struct ieee80211_ops p54_ops = {
  821. .tx = p54_tx,
  822. .start = p54_start,
  823. .stop = p54_stop,
  824. .add_interface = p54_add_interface,
  825. .remove_interface = p54_remove_interface,
  826. .config = p54_config,
  827. .config_interface = p54_config_interface,
  828. .configure_filter = p54_configure_filter,
  829. .conf_tx = p54_conf_tx,
  830. .get_stats = p54_get_stats,
  831. .get_tx_stats = p54_get_tx_stats
  832. };
  833. struct ieee80211_hw *p54_init_common(size_t priv_data_len)
  834. {
  835. struct ieee80211_hw *dev;
  836. struct p54_common *priv;
  837. dev = ieee80211_alloc_hw(priv_data_len, &p54_ops);
  838. if (!dev)
  839. return NULL;
  840. priv = dev->priv;
  841. priv->mode = IEEE80211_IF_TYPE_INVALID;
  842. skb_queue_head_init(&priv->tx_queue);
  843. dev->wiphy->bands[IEEE80211_BAND_2GHZ] = &band_2GHz;
  844. dev->flags = IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING | /* not sure */
  845. IEEE80211_HW_RX_INCLUDES_FCS |
  846. IEEE80211_HW_SIGNAL_UNSPEC;
  847. dev->channel_change_time = 1000; /* TODO: find actual value */
  848. dev->max_signal = 127;
  849. priv->tx_stats[0].limit = 5;
  850. dev->queues = 1;
  851. dev->extra_tx_headroom = sizeof(struct p54_control_hdr) + 4 +
  852. sizeof(struct p54_tx_control_allocdata);
  853. priv->cached_vdcf = kzalloc(sizeof(struct p54_tx_control_vdcf) +
  854. priv->tx_hdr_len + sizeof(struct p54_control_hdr), GFP_KERNEL);
  855. if (!priv->cached_vdcf) {
  856. ieee80211_free_hw(dev);
  857. return NULL;
  858. }
  859. p54_init_vdcf(dev);
  860. return dev;
  861. }
  862. EXPORT_SYMBOL_GPL(p54_init_common);
  863. void p54_free_common(struct ieee80211_hw *dev)
  864. {
  865. struct p54_common *priv = dev->priv;
  866. kfree(priv->iq_autocal);
  867. kfree(priv->output_limit);
  868. kfree(priv->curve_data);
  869. kfree(priv->cached_vdcf);
  870. }
  871. EXPORT_SYMBOL_GPL(p54_free_common);
  872. static int __init p54_init(void)
  873. {
  874. return 0;
  875. }
  876. static void __exit p54_exit(void)
  877. {
  878. }
  879. module_init(p54_init);
  880. module_exit(p54_exit);