p54common.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051
  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.data[0].limit = 3;
  133. priv->tx_stats.data[1].limit = 4;
  134. priv->tx_stats.data[2].limit = 3;
  135. priv->tx_stats.data[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.ssi = 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. /* ieee80211_start_queues is great if all queues are really empty.
  329. * But, what if some are full? */
  330. for (i = 0; i < dev->queues; i++)
  331. if (priv->tx_stats.data[i].len < priv->tx_stats.data[i].limit)
  332. ieee80211_wake_queue(dev, i);
  333. }
  334. static void p54_rx_frame_sent(struct ieee80211_hw *dev, struct sk_buff *skb)
  335. {
  336. struct p54_common *priv = dev->priv;
  337. struct p54_control_hdr *hdr = (struct p54_control_hdr *) skb->data;
  338. struct p54_frame_sent_hdr *payload = (struct p54_frame_sent_hdr *) hdr->data;
  339. struct sk_buff *entry = (struct sk_buff *) priv->tx_queue.next;
  340. u32 addr = le32_to_cpu(hdr->req_id) - 0x70;
  341. struct memrecord *range = NULL;
  342. u32 freed = 0;
  343. u32 last_addr = priv->rx_start;
  344. while (entry != (struct sk_buff *)&priv->tx_queue) {
  345. range = (struct memrecord *)&entry->cb;
  346. if (range->start_addr == addr) {
  347. struct ieee80211_tx_status status;
  348. struct p54_control_hdr *entry_hdr;
  349. struct p54_tx_control_allocdata *entry_data;
  350. int pad = 0;
  351. if (entry->next != (struct sk_buff *)&priv->tx_queue)
  352. freed = ((struct memrecord *)&entry->next->cb)->start_addr - last_addr;
  353. else
  354. freed = priv->rx_end - last_addr;
  355. last_addr = range->end_addr;
  356. __skb_unlink(entry, &priv->tx_queue);
  357. if (!range->control) {
  358. kfree_skb(entry);
  359. break;
  360. }
  361. memset(&status, 0, sizeof(status));
  362. memcpy(&status.control, range->control,
  363. sizeof(status.control));
  364. kfree(range->control);
  365. priv->tx_stats.data[status.control.queue].len--;
  366. entry_hdr = (struct p54_control_hdr *) entry->data;
  367. entry_data = (struct p54_tx_control_allocdata *) entry_hdr->data;
  368. if ((entry_hdr->magic1 & cpu_to_le16(0x4000)) != 0)
  369. pad = entry_data->align[0];
  370. if (!(status.control.flags & IEEE80211_TXCTL_NO_ACK)) {
  371. if (!(payload->status & 0x01))
  372. status.flags |= IEEE80211_TX_STATUS_ACK;
  373. else
  374. status.excessive_retries = 1;
  375. }
  376. status.retry_count = payload->retries - 1;
  377. status.ack_signal = le16_to_cpu(payload->ack_rssi);
  378. skb_pull(entry, sizeof(*hdr) + pad + sizeof(*entry_data));
  379. ieee80211_tx_status_irqsafe(dev, entry, &status);
  380. break;
  381. } else
  382. last_addr = range->end_addr;
  383. entry = entry->next;
  384. }
  385. if (freed >= IEEE80211_MAX_RTS_THRESHOLD + 0x170 +
  386. sizeof(struct p54_control_hdr))
  387. p54_wake_free_queues(dev);
  388. }
  389. static void p54_rx_control(struct ieee80211_hw *dev, struct sk_buff *skb)
  390. {
  391. struct p54_control_hdr *hdr = (struct p54_control_hdr *) skb->data;
  392. switch (le16_to_cpu(hdr->type)) {
  393. case P54_CONTROL_TYPE_TXDONE:
  394. p54_rx_frame_sent(dev, skb);
  395. break;
  396. case P54_CONTROL_TYPE_BBP:
  397. break;
  398. default:
  399. printk(KERN_DEBUG "%s: not handling 0x%02x type control frame\n",
  400. wiphy_name(dev->wiphy), le16_to_cpu(hdr->type));
  401. break;
  402. }
  403. }
  404. /* returns zero if skb can be reused */
  405. int p54_rx(struct ieee80211_hw *dev, struct sk_buff *skb)
  406. {
  407. u8 type = le16_to_cpu(*((__le16 *)skb->data)) >> 8;
  408. switch (type) {
  409. case 0x00:
  410. case 0x01:
  411. p54_rx_data(dev, skb);
  412. return -1;
  413. case 0x4d:
  414. /* TODO: do something better... but then again, I've never seen this happen */
  415. printk(KERN_ERR "%s: Received fault. Probably need to restart hardware now..\n",
  416. wiphy_name(dev->wiphy));
  417. break;
  418. case 0x80:
  419. p54_rx_control(dev, skb);
  420. break;
  421. default:
  422. printk(KERN_ERR "%s: unknown frame RXed (0x%02x)\n",
  423. wiphy_name(dev->wiphy), type);
  424. break;
  425. }
  426. return 0;
  427. }
  428. EXPORT_SYMBOL_GPL(p54_rx);
  429. /*
  430. * So, the firmware is somewhat stupid and doesn't know what places in its
  431. * memory incoming data should go to. By poking around in the firmware, we
  432. * can find some unused memory to upload our packets to. However, data that we
  433. * want the card to TX needs to stay intact until the card has told us that
  434. * it is done with it. This function finds empty places we can upload to and
  435. * marks allocated areas as reserved if necessary. p54_rx_frame_sent frees
  436. * allocated areas.
  437. */
  438. static void p54_assign_address(struct ieee80211_hw *dev, struct sk_buff *skb,
  439. struct p54_control_hdr *data, u32 len,
  440. struct ieee80211_tx_control *control)
  441. {
  442. struct p54_common *priv = dev->priv;
  443. struct sk_buff *entry = priv->tx_queue.next;
  444. struct sk_buff *target_skb = NULL;
  445. struct memrecord *range;
  446. u32 last_addr = priv->rx_start;
  447. u32 largest_hole = 0;
  448. u32 target_addr = priv->rx_start;
  449. unsigned long flags;
  450. unsigned int left;
  451. len = (len + 0x170 + 3) & ~0x3; /* 0x70 headroom, 0x100 tailroom */
  452. spin_lock_irqsave(&priv->tx_queue.lock, flags);
  453. left = skb_queue_len(&priv->tx_queue);
  454. while (left--) {
  455. u32 hole_size;
  456. range = (struct memrecord *)&entry->cb;
  457. hole_size = range->start_addr - last_addr;
  458. if (!target_skb && hole_size >= len) {
  459. target_skb = entry->prev;
  460. hole_size -= len;
  461. target_addr = last_addr;
  462. }
  463. largest_hole = max(largest_hole, hole_size);
  464. last_addr = range->end_addr;
  465. entry = entry->next;
  466. }
  467. if (!target_skb && priv->rx_end - last_addr >= len) {
  468. target_skb = priv->tx_queue.prev;
  469. largest_hole = max(largest_hole, priv->rx_end - last_addr - len);
  470. if (!skb_queue_empty(&priv->tx_queue)) {
  471. range = (struct memrecord *)&target_skb->cb;
  472. target_addr = range->end_addr;
  473. }
  474. } else
  475. largest_hole = max(largest_hole, priv->rx_end - last_addr);
  476. if (skb) {
  477. range = (struct memrecord *)&skb->cb;
  478. range->start_addr = target_addr;
  479. range->end_addr = target_addr + len;
  480. range->control = control;
  481. __skb_queue_after(&priv->tx_queue, target_skb, skb);
  482. if (largest_hole < IEEE80211_MAX_RTS_THRESHOLD + 0x170 +
  483. sizeof(struct p54_control_hdr))
  484. ieee80211_stop_queues(dev);
  485. }
  486. spin_unlock_irqrestore(&priv->tx_queue.lock, flags);
  487. data->req_id = cpu_to_le32(target_addr + 0x70);
  488. }
  489. static int p54_tx(struct ieee80211_hw *dev, struct sk_buff *skb,
  490. struct ieee80211_tx_control *control)
  491. {
  492. struct ieee80211_tx_queue_stats_data *current_queue;
  493. struct p54_common *priv = dev->priv;
  494. struct p54_control_hdr *hdr;
  495. struct p54_tx_control_allocdata *txhdr;
  496. struct ieee80211_tx_control *control_copy;
  497. size_t padding, len;
  498. u8 rate;
  499. current_queue = &priv->tx_stats.data[control->queue];
  500. if (unlikely(current_queue->len > current_queue->limit))
  501. return NETDEV_TX_BUSY;
  502. current_queue->len++;
  503. current_queue->count++;
  504. if (current_queue->len == current_queue->limit)
  505. ieee80211_stop_queue(dev, control->queue);
  506. padding = (unsigned long)(skb->data - (sizeof(*hdr) + sizeof(*txhdr))) & 3;
  507. len = skb->len;
  508. control_copy = kmalloc(sizeof(*control), GFP_ATOMIC);
  509. if (control_copy)
  510. memcpy(control_copy, control, sizeof(*control));
  511. txhdr = (struct p54_tx_control_allocdata *)
  512. skb_push(skb, sizeof(*txhdr) + padding);
  513. hdr = (struct p54_control_hdr *) skb_push(skb, sizeof(*hdr));
  514. if (padding)
  515. hdr->magic1 = cpu_to_le16(0x4010);
  516. else
  517. hdr->magic1 = cpu_to_le16(0x0010);
  518. hdr->len = cpu_to_le16(len);
  519. hdr->type = (control->flags & IEEE80211_TXCTL_NO_ACK) ? 0 : cpu_to_le16(1);
  520. hdr->retry1 = hdr->retry2 = control->retry_limit;
  521. p54_assign_address(dev, skb, hdr, skb->len, control_copy);
  522. memset(txhdr->wep_key, 0x0, 16);
  523. txhdr->padding = 0;
  524. txhdr->padding2 = 0;
  525. /* TODO: add support for alternate retry TX rates */
  526. rate = control->tx_rate->hw_value;
  527. if (control->flags & IEEE80211_TXCTL_SHORT_PREAMBLE)
  528. rate |= 0x10;
  529. if (control->flags & IEEE80211_TXCTL_USE_RTS_CTS)
  530. rate |= 0x40;
  531. else if (control->flags & IEEE80211_TXCTL_USE_CTS_PROTECT)
  532. rate |= 0x20;
  533. memset(txhdr->rateset, rate, 8);
  534. txhdr->wep_key_present = 0;
  535. txhdr->wep_key_len = 0;
  536. txhdr->frame_type = cpu_to_le32(control->queue + 4);
  537. txhdr->magic4 = 0;
  538. txhdr->antenna = (control->antenna_sel_tx == 0) ?
  539. 2 : control->antenna_sel_tx - 1;
  540. txhdr->output_power = 0x7f; // HW Maximum
  541. txhdr->magic5 = (control->flags & IEEE80211_TXCTL_NO_ACK) ?
  542. 0 : ((rate > 0x3) ? cpu_to_le32(0x33) : cpu_to_le32(0x23));
  543. if (padding)
  544. txhdr->align[0] = padding;
  545. priv->tx(dev, hdr, skb->len, 0);
  546. return 0;
  547. }
  548. static int p54_set_filter(struct ieee80211_hw *dev, u16 filter_type,
  549. const u8 *dst, const u8 *src, u8 antenna,
  550. u32 magic3, u32 magic8, u32 magic9)
  551. {
  552. struct p54_common *priv = dev->priv;
  553. struct p54_control_hdr *hdr;
  554. struct p54_tx_control_filter *filter;
  555. hdr = kzalloc(sizeof(*hdr) + sizeof(*filter) +
  556. priv->tx_hdr_len, GFP_ATOMIC);
  557. if (!hdr)
  558. return -ENOMEM;
  559. hdr = (void *)hdr + priv->tx_hdr_len;
  560. filter = (struct p54_tx_control_filter *) hdr->data;
  561. hdr->magic1 = cpu_to_le16(0x8001);
  562. hdr->len = cpu_to_le16(sizeof(*filter));
  563. p54_assign_address(dev, NULL, hdr, sizeof(*hdr) + sizeof(*filter), NULL);
  564. hdr->type = cpu_to_le16(P54_CONTROL_TYPE_FILTER_SET);
  565. filter->filter_type = cpu_to_le16(filter_type);
  566. memcpy(filter->dst, dst, ETH_ALEN);
  567. if (!src)
  568. memset(filter->src, ~0, ETH_ALEN);
  569. else
  570. memcpy(filter->src, src, ETH_ALEN);
  571. filter->antenna = antenna;
  572. filter->magic3 = cpu_to_le32(magic3);
  573. filter->rx_addr = cpu_to_le32(priv->rx_end);
  574. filter->max_rx = cpu_to_le16(0x0620); /* FIXME: for usb ver 1.. maybe */
  575. filter->rxhw = priv->rxhw;
  576. filter->magic8 = cpu_to_le16(magic8);
  577. filter->magic9 = cpu_to_le16(magic9);
  578. priv->tx(dev, hdr, sizeof(*hdr) + sizeof(*filter), 1);
  579. return 0;
  580. }
  581. static int p54_set_freq(struct ieee80211_hw *dev, __le16 freq)
  582. {
  583. struct p54_common *priv = dev->priv;
  584. struct p54_control_hdr *hdr;
  585. struct p54_tx_control_channel *chan;
  586. unsigned int i;
  587. size_t payload_len = sizeof(*chan) + sizeof(u32)*2 +
  588. sizeof(*chan->curve_data) *
  589. priv->curve_data->points_per_channel;
  590. void *entry;
  591. hdr = kzalloc(sizeof(*hdr) + payload_len +
  592. priv->tx_hdr_len, GFP_KERNEL);
  593. if (!hdr)
  594. return -ENOMEM;
  595. hdr = (void *)hdr + priv->tx_hdr_len;
  596. chan = (struct p54_tx_control_channel *) hdr->data;
  597. hdr->magic1 = cpu_to_le16(0x8001);
  598. hdr->len = cpu_to_le16(sizeof(*chan));
  599. hdr->type = cpu_to_le16(P54_CONTROL_TYPE_CHANNEL_CHANGE);
  600. p54_assign_address(dev, NULL, hdr, sizeof(*hdr) + payload_len, NULL);
  601. chan->magic1 = cpu_to_le16(0x1);
  602. chan->magic2 = cpu_to_le16(0x0);
  603. for (i = 0; i < priv->iq_autocal_len; i++) {
  604. if (priv->iq_autocal[i].freq != freq)
  605. continue;
  606. memcpy(&chan->iq_autocal, &priv->iq_autocal[i],
  607. sizeof(*priv->iq_autocal));
  608. break;
  609. }
  610. if (i == priv->iq_autocal_len)
  611. goto err;
  612. for (i = 0; i < priv->output_limit_len; i++) {
  613. if (priv->output_limit[i].freq != freq)
  614. continue;
  615. chan->val_barker = 0x38;
  616. chan->val_bpsk = priv->output_limit[i].val_bpsk;
  617. chan->val_qpsk = priv->output_limit[i].val_qpsk;
  618. chan->val_16qam = priv->output_limit[i].val_16qam;
  619. chan->val_64qam = priv->output_limit[i].val_64qam;
  620. break;
  621. }
  622. if (i == priv->output_limit_len)
  623. goto err;
  624. chan->pa_points_per_curve = priv->curve_data->points_per_channel;
  625. entry = priv->curve_data->data;
  626. for (i = 0; i < priv->curve_data->channels; i++) {
  627. if (*((__le16 *)entry) != freq) {
  628. entry += sizeof(__le16);
  629. entry += sizeof(struct pda_pa_curve_data_sample_rev1) *
  630. chan->pa_points_per_curve;
  631. continue;
  632. }
  633. entry += sizeof(__le16);
  634. memcpy(chan->curve_data, entry, sizeof(*chan->curve_data) *
  635. chan->pa_points_per_curve);
  636. break;
  637. }
  638. memcpy(hdr->data + payload_len - 4, &chan->val_bpsk, 4);
  639. priv->tx(dev, hdr, sizeof(*hdr) + payload_len, 1);
  640. return 0;
  641. err:
  642. printk(KERN_ERR "%s: frequency change failed\n", wiphy_name(dev->wiphy));
  643. kfree(hdr);
  644. return -EINVAL;
  645. }
  646. static int p54_set_leds(struct ieee80211_hw *dev, int mode, int link, int act)
  647. {
  648. struct p54_common *priv = dev->priv;
  649. struct p54_control_hdr *hdr;
  650. struct p54_tx_control_led *led;
  651. hdr = kzalloc(sizeof(*hdr) + sizeof(*led) +
  652. priv->tx_hdr_len, GFP_KERNEL);
  653. if (!hdr)
  654. return -ENOMEM;
  655. hdr = (void *)hdr + priv->tx_hdr_len;
  656. hdr->magic1 = cpu_to_le16(0x8001);
  657. hdr->len = cpu_to_le16(sizeof(*led));
  658. hdr->type = cpu_to_le16(P54_CONTROL_TYPE_LED);
  659. p54_assign_address(dev, NULL, hdr, sizeof(*hdr) + sizeof(*led), NULL);
  660. led = (struct p54_tx_control_led *) hdr->data;
  661. led->mode = cpu_to_le16(mode);
  662. led->led_permanent = cpu_to_le16(link);
  663. led->led_temporary = cpu_to_le16(act);
  664. led->duration = cpu_to_le16(1000);
  665. priv->tx(dev, hdr, sizeof(*hdr) + sizeof(*led), 1);
  666. return 0;
  667. }
  668. #define P54_SET_QUEUE(queue, ai_fs, cw_min, cw_max, _txop) \
  669. do { \
  670. queue.aifs = cpu_to_le16(ai_fs); \
  671. queue.cwmin = cpu_to_le16(cw_min); \
  672. queue.cwmax = cpu_to_le16(cw_max); \
  673. queue.txop = cpu_to_le16(_txop); \
  674. } while(0)
  675. static void p54_init_vdcf(struct ieee80211_hw *dev)
  676. {
  677. struct p54_common *priv = dev->priv;
  678. struct p54_control_hdr *hdr;
  679. struct p54_tx_control_vdcf *vdcf;
  680. /* all USB V1 adapters need a extra headroom */
  681. hdr = (void *)priv->cached_vdcf + priv->tx_hdr_len;
  682. hdr->magic1 = cpu_to_le16(0x8001);
  683. hdr->len = cpu_to_le16(sizeof(*vdcf));
  684. hdr->type = cpu_to_le16(P54_CONTROL_TYPE_DCFINIT);
  685. hdr->req_id = cpu_to_le32(priv->rx_start);
  686. vdcf = (struct p54_tx_control_vdcf *) hdr->data;
  687. P54_SET_QUEUE(vdcf->queue[0], 0x0002, 0x0003, 0x0007, 47);
  688. P54_SET_QUEUE(vdcf->queue[1], 0x0002, 0x0007, 0x000f, 94);
  689. P54_SET_QUEUE(vdcf->queue[2], 0x0003, 0x000f, 0x03ff, 0);
  690. P54_SET_QUEUE(vdcf->queue[3], 0x0007, 0x000f, 0x03ff, 0);
  691. }
  692. static void p54_set_vdcf(struct ieee80211_hw *dev)
  693. {
  694. struct p54_common *priv = dev->priv;
  695. struct p54_control_hdr *hdr;
  696. struct p54_tx_control_vdcf *vdcf;
  697. hdr = (void *)priv->cached_vdcf + priv->tx_hdr_len;
  698. p54_assign_address(dev, NULL, hdr, sizeof(*hdr) + sizeof(*vdcf), NULL);
  699. vdcf = (struct p54_tx_control_vdcf *) hdr->data;
  700. if (dev->conf.flags & IEEE80211_CONF_SHORT_SLOT_TIME) {
  701. vdcf->slottime = 9;
  702. vdcf->magic1 = 0x00;
  703. vdcf->magic2 = 0x10;
  704. } else {
  705. vdcf->slottime = 20;
  706. vdcf->magic1 = 0x0a;
  707. vdcf->magic2 = 0x06;
  708. }
  709. /* (see prism54/isl_oid.h for further details) */
  710. vdcf->frameburst = cpu_to_le16(0);
  711. priv->tx(dev, hdr, sizeof(*hdr) + sizeof(*vdcf), 0);
  712. }
  713. static int p54_start(struct ieee80211_hw *dev)
  714. {
  715. struct p54_common *priv = dev->priv;
  716. int err;
  717. err = priv->open(dev);
  718. if (!err)
  719. priv->mode = IEEE80211_IF_TYPE_MNTR;
  720. return err;
  721. }
  722. static void p54_stop(struct ieee80211_hw *dev)
  723. {
  724. struct p54_common *priv = dev->priv;
  725. struct sk_buff *skb;
  726. while ((skb = skb_dequeue(&priv->tx_queue))) {
  727. struct memrecord *range = (struct memrecord *)&skb->cb;
  728. if (range->control)
  729. kfree(range->control);
  730. kfree_skb(skb);
  731. }
  732. priv->stop(dev);
  733. priv->mode = IEEE80211_IF_TYPE_INVALID;
  734. }
  735. static int p54_add_interface(struct ieee80211_hw *dev,
  736. struct ieee80211_if_init_conf *conf)
  737. {
  738. struct p54_common *priv = dev->priv;
  739. if (priv->mode != IEEE80211_IF_TYPE_MNTR)
  740. return -EOPNOTSUPP;
  741. switch (conf->type) {
  742. case IEEE80211_IF_TYPE_STA:
  743. priv->mode = conf->type;
  744. break;
  745. default:
  746. return -EOPNOTSUPP;
  747. }
  748. memcpy(priv->mac_addr, conf->mac_addr, ETH_ALEN);
  749. p54_set_filter(dev, 0, priv->mac_addr, NULL, 0, 1, 0, 0xF642);
  750. p54_set_filter(dev, 0, priv->mac_addr, NULL, 1, 0, 0, 0xF642);
  751. switch (conf->type) {
  752. case IEEE80211_IF_TYPE_STA:
  753. p54_set_filter(dev, 1, priv->mac_addr, NULL, 0, 0x15F, 0x1F4, 0);
  754. break;
  755. default:
  756. BUG(); /* impossible */
  757. break;
  758. }
  759. p54_set_leds(dev, 1, 0, 0);
  760. return 0;
  761. }
  762. static void p54_remove_interface(struct ieee80211_hw *dev,
  763. struct ieee80211_if_init_conf *conf)
  764. {
  765. struct p54_common *priv = dev->priv;
  766. priv->mode = IEEE80211_IF_TYPE_MNTR;
  767. memset(priv->mac_addr, 0, ETH_ALEN);
  768. p54_set_filter(dev, 0, priv->mac_addr, NULL, 2, 0, 0, 0);
  769. }
  770. static int p54_config(struct ieee80211_hw *dev, struct ieee80211_conf *conf)
  771. {
  772. int ret;
  773. ret = p54_set_freq(dev, cpu_to_le16(conf->channel->center_freq));
  774. p54_set_vdcf(dev);
  775. return ret;
  776. }
  777. static int p54_config_interface(struct ieee80211_hw *dev,
  778. struct ieee80211_vif *vif,
  779. struct ieee80211_if_conf *conf)
  780. {
  781. struct p54_common *priv = dev->priv;
  782. p54_set_filter(dev, 0, priv->mac_addr, conf->bssid, 0, 1, 0, 0xF642);
  783. p54_set_filter(dev, 0, priv->mac_addr, conf->bssid, 2, 0, 0, 0);
  784. p54_set_leds(dev, 1, !is_multicast_ether_addr(conf->bssid), 0);
  785. memcpy(priv->bssid, conf->bssid, ETH_ALEN);
  786. return 0;
  787. }
  788. static void p54_configure_filter(struct ieee80211_hw *dev,
  789. unsigned int changed_flags,
  790. unsigned int *total_flags,
  791. int mc_count, struct dev_mc_list *mclist)
  792. {
  793. struct p54_common *priv = dev->priv;
  794. *total_flags &= FIF_BCN_PRBRESP_PROMISC;
  795. if (changed_flags & FIF_BCN_PRBRESP_PROMISC) {
  796. if (*total_flags & FIF_BCN_PRBRESP_PROMISC)
  797. p54_set_filter(dev, 0, priv->mac_addr,
  798. NULL, 2, 0, 0, 0);
  799. else
  800. p54_set_filter(dev, 0, priv->mac_addr,
  801. priv->bssid, 2, 0, 0, 0);
  802. }
  803. }
  804. static int p54_conf_tx(struct ieee80211_hw *dev, int queue,
  805. const struct ieee80211_tx_queue_params *params)
  806. {
  807. struct p54_common *priv = dev->priv;
  808. struct p54_tx_control_vdcf *vdcf;
  809. vdcf = (struct p54_tx_control_vdcf *)(((struct p54_control_hdr *)
  810. ((void *)priv->cached_vdcf + priv->tx_hdr_len))->data);
  811. if ((params) && !((queue < 0) || (queue > 4))) {
  812. P54_SET_QUEUE(vdcf->queue[queue], params->aifs,
  813. params->cw_min, params->cw_max, params->txop);
  814. } else
  815. return -EINVAL;
  816. p54_set_vdcf(dev);
  817. return 0;
  818. }
  819. static int p54_get_stats(struct ieee80211_hw *dev,
  820. struct ieee80211_low_level_stats *stats)
  821. {
  822. /* TODO */
  823. return 0;
  824. }
  825. static int p54_get_tx_stats(struct ieee80211_hw *dev,
  826. struct ieee80211_tx_queue_stats *stats)
  827. {
  828. struct p54_common *priv = dev->priv;
  829. unsigned int i;
  830. for (i = 0; i < dev->queues; i++)
  831. memcpy(&stats->data[i], &priv->tx_stats.data[i],
  832. sizeof(stats->data[i]));
  833. return 0;
  834. }
  835. static const struct ieee80211_ops p54_ops = {
  836. .tx = p54_tx,
  837. .start = p54_start,
  838. .stop = p54_stop,
  839. .add_interface = p54_add_interface,
  840. .remove_interface = p54_remove_interface,
  841. .config = p54_config,
  842. .config_interface = p54_config_interface,
  843. .configure_filter = p54_configure_filter,
  844. .conf_tx = p54_conf_tx,
  845. .get_stats = p54_get_stats,
  846. .get_tx_stats = p54_get_tx_stats
  847. };
  848. struct ieee80211_hw *p54_init_common(size_t priv_data_len)
  849. {
  850. struct ieee80211_hw *dev;
  851. struct p54_common *priv;
  852. dev = ieee80211_alloc_hw(priv_data_len, &p54_ops);
  853. if (!dev)
  854. return NULL;
  855. priv = dev->priv;
  856. priv->mode = IEEE80211_IF_TYPE_INVALID;
  857. skb_queue_head_init(&priv->tx_queue);
  858. dev->wiphy->bands[IEEE80211_BAND_2GHZ] = &band_2GHz;
  859. dev->flags = IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING | /* not sure */
  860. IEEE80211_HW_RX_INCLUDES_FCS;
  861. dev->channel_change_time = 1000; /* TODO: find actual value */
  862. dev->max_rssi = 127;
  863. priv->tx_stats.data[0].limit = 5;
  864. dev->queues = 1;
  865. dev->extra_tx_headroom = sizeof(struct p54_control_hdr) + 4 +
  866. sizeof(struct p54_tx_control_allocdata);
  867. priv->cached_vdcf = kzalloc(sizeof(struct p54_tx_control_vdcf) +
  868. priv->tx_hdr_len + sizeof(struct p54_control_hdr), GFP_KERNEL);
  869. if (!priv->cached_vdcf) {
  870. ieee80211_free_hw(dev);
  871. return NULL;
  872. }
  873. p54_init_vdcf(dev);
  874. return dev;
  875. }
  876. EXPORT_SYMBOL_GPL(p54_init_common);
  877. void p54_free_common(struct ieee80211_hw *dev)
  878. {
  879. struct p54_common *priv = dev->priv;
  880. kfree(priv->iq_autocal);
  881. kfree(priv->output_limit);
  882. kfree(priv->curve_data);
  883. kfree(priv->cached_vdcf);
  884. }
  885. EXPORT_SYMBOL_GPL(p54_free_common);
  886. static int __init p54_init(void)
  887. {
  888. return 0;
  889. }
  890. static void __exit p54_exit(void)
  891. {
  892. }
  893. module_init(p54_init);
  894. module_exit(p54_exit);