p54common.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438
  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_bgrates[] = {
  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_bgchannels[] = {
  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_bgchannels,
  56. .n_channels = ARRAY_SIZE(p54_bgchannels),
  57. .bitrates = p54_bgrates,
  58. .n_bitrates = ARRAY_SIZE(p54_bgrates),
  59. };
  60. static struct ieee80211_rate p54_arates[] = {
  61. { .bitrate = 60, .hw_value = 4, },
  62. { .bitrate = 90, .hw_value = 5, },
  63. { .bitrate = 120, .hw_value = 6, },
  64. { .bitrate = 180, .hw_value = 7, },
  65. { .bitrate = 240, .hw_value = 8, },
  66. { .bitrate = 360, .hw_value = 9, },
  67. { .bitrate = 480, .hw_value = 10, },
  68. { .bitrate = 540, .hw_value = 11, },
  69. };
  70. static struct ieee80211_channel p54_achannels[] = {
  71. { .center_freq = 4920 },
  72. { .center_freq = 4940 },
  73. { .center_freq = 4960 },
  74. { .center_freq = 4980 },
  75. { .center_freq = 5040 },
  76. { .center_freq = 5060 },
  77. { .center_freq = 5080 },
  78. { .center_freq = 5170 },
  79. { .center_freq = 5180 },
  80. { .center_freq = 5190 },
  81. { .center_freq = 5200 },
  82. { .center_freq = 5210 },
  83. { .center_freq = 5220 },
  84. { .center_freq = 5230 },
  85. { .center_freq = 5240 },
  86. { .center_freq = 5260 },
  87. { .center_freq = 5280 },
  88. { .center_freq = 5300 },
  89. { .center_freq = 5320 },
  90. { .center_freq = 5500 },
  91. { .center_freq = 5520 },
  92. { .center_freq = 5540 },
  93. { .center_freq = 5560 },
  94. { .center_freq = 5580 },
  95. { .center_freq = 5600 },
  96. { .center_freq = 5620 },
  97. { .center_freq = 5640 },
  98. { .center_freq = 5660 },
  99. { .center_freq = 5680 },
  100. { .center_freq = 5700 },
  101. { .center_freq = 5745 },
  102. { .center_freq = 5765 },
  103. { .center_freq = 5785 },
  104. { .center_freq = 5805 },
  105. { .center_freq = 5825 },
  106. };
  107. static struct ieee80211_supported_band band_5GHz = {
  108. .channels = p54_achannels,
  109. .n_channels = ARRAY_SIZE(p54_achannels),
  110. .bitrates = p54_arates,
  111. .n_bitrates = ARRAY_SIZE(p54_arates),
  112. };
  113. int p54_parse_firmware(struct ieee80211_hw *dev, const struct firmware *fw)
  114. {
  115. struct p54_common *priv = dev->priv;
  116. struct bootrec_exp_if *exp_if;
  117. struct bootrec *bootrec;
  118. u32 *data = (u32 *)fw->data;
  119. u32 *end_data = (u32 *)fw->data + (fw->size >> 2);
  120. u8 *fw_version = NULL;
  121. size_t len;
  122. int i;
  123. if (priv->rx_start)
  124. return 0;
  125. while (data < end_data && *data)
  126. data++;
  127. while (data < end_data && !*data)
  128. data++;
  129. bootrec = (struct bootrec *) data;
  130. while (bootrec->data <= end_data &&
  131. (bootrec->data + (len = le32_to_cpu(bootrec->len))) <= end_data) {
  132. u32 code = le32_to_cpu(bootrec->code);
  133. switch (code) {
  134. case BR_CODE_COMPONENT_ID:
  135. priv->fw_interface = be32_to_cpup((__be32 *)
  136. bootrec->data);
  137. switch (priv->fw_interface) {
  138. case FW_FMAC:
  139. printk(KERN_INFO "p54: FreeMAC firmware\n");
  140. break;
  141. case FW_LM20:
  142. printk(KERN_INFO "p54: LM20 firmware\n");
  143. break;
  144. case FW_LM86:
  145. printk(KERN_INFO "p54: LM86 firmware\n");
  146. break;
  147. case FW_LM87:
  148. printk(KERN_INFO "p54: LM87 firmware\n");
  149. break;
  150. default:
  151. printk(KERN_INFO "p54: unknown firmware\n");
  152. break;
  153. }
  154. break;
  155. case BR_CODE_COMPONENT_VERSION:
  156. /* 24 bytes should be enough for all firmwares */
  157. if (strnlen((unsigned char*)bootrec->data, 24) < 24)
  158. fw_version = (unsigned char*)bootrec->data;
  159. break;
  160. case BR_CODE_DESCR: {
  161. struct bootrec_desc *desc =
  162. (struct bootrec_desc *)bootrec->data;
  163. priv->rx_start = le32_to_cpu(desc->rx_start);
  164. /* FIXME add sanity checking */
  165. priv->rx_end = le32_to_cpu(desc->rx_end) - 0x3500;
  166. priv->headroom = desc->headroom;
  167. priv->tailroom = desc->tailroom;
  168. if (le32_to_cpu(bootrec->len) == 11)
  169. priv->rx_mtu = le16_to_cpu(bootrec->rx_mtu);
  170. else
  171. priv->rx_mtu = (size_t)
  172. 0x620 - priv->tx_hdr_len;
  173. break;
  174. }
  175. case BR_CODE_EXPOSED_IF:
  176. exp_if = (struct bootrec_exp_if *) bootrec->data;
  177. for (i = 0; i < (len * sizeof(*exp_if) / 4); i++)
  178. if (exp_if[i].if_id == cpu_to_le16(0x1a))
  179. priv->fw_var = le16_to_cpu(exp_if[i].variant);
  180. break;
  181. case BR_CODE_DEPENDENT_IF:
  182. break;
  183. case BR_CODE_END_OF_BRA:
  184. case LEGACY_BR_CODE_END_OF_BRA:
  185. end_data = NULL;
  186. break;
  187. default:
  188. break;
  189. }
  190. bootrec = (struct bootrec *)&bootrec->data[len];
  191. }
  192. if (fw_version)
  193. printk(KERN_INFO "p54: FW rev %s - Softmac protocol %x.%x\n",
  194. fw_version, priv->fw_var >> 8, priv->fw_var & 0xff);
  195. if (priv->fw_var >= 0x300) {
  196. /* Firmware supports QoS, use it! */
  197. priv->tx_stats[4].limit = 3;
  198. priv->tx_stats[5].limit = 4;
  199. priv->tx_stats[6].limit = 3;
  200. priv->tx_stats[7].limit = 1;
  201. dev->queues = 4;
  202. }
  203. return 0;
  204. }
  205. EXPORT_SYMBOL_GPL(p54_parse_firmware);
  206. static int p54_convert_rev0(struct ieee80211_hw *dev,
  207. struct pda_pa_curve_data *curve_data)
  208. {
  209. struct p54_common *priv = dev->priv;
  210. struct p54_pa_curve_data_sample *dst;
  211. struct pda_pa_curve_data_sample_rev0 *src;
  212. size_t cd_len = sizeof(*curve_data) +
  213. (curve_data->points_per_channel*sizeof(*dst) + 2) *
  214. curve_data->channels;
  215. unsigned int i, j;
  216. void *source, *target;
  217. priv->curve_data = kmalloc(cd_len, GFP_KERNEL);
  218. if (!priv->curve_data)
  219. return -ENOMEM;
  220. memcpy(priv->curve_data, curve_data, sizeof(*curve_data));
  221. source = curve_data->data;
  222. target = priv->curve_data->data;
  223. for (i = 0; i < curve_data->channels; i++) {
  224. __le16 *freq = source;
  225. source += sizeof(__le16);
  226. *((__le16 *)target) = *freq;
  227. target += sizeof(__le16);
  228. for (j = 0; j < curve_data->points_per_channel; j++) {
  229. dst = target;
  230. src = source;
  231. dst->rf_power = src->rf_power;
  232. dst->pa_detector = src->pa_detector;
  233. dst->data_64qam = src->pcv;
  234. /* "invent" the points for the other modulations */
  235. #define SUB(x,y) (u8)((x) - (y)) > (x) ? 0 : (x) - (y)
  236. dst->data_16qam = SUB(src->pcv, 12);
  237. dst->data_qpsk = SUB(dst->data_16qam, 12);
  238. dst->data_bpsk = SUB(dst->data_qpsk, 12);
  239. dst->data_barker = SUB(dst->data_bpsk, 14);
  240. #undef SUB
  241. target += sizeof(*dst);
  242. source += sizeof(*src);
  243. }
  244. }
  245. return 0;
  246. }
  247. static int p54_convert_rev1(struct ieee80211_hw *dev,
  248. struct pda_pa_curve_data *curve_data)
  249. {
  250. struct p54_common *priv = dev->priv;
  251. struct p54_pa_curve_data_sample *dst;
  252. struct pda_pa_curve_data_sample_rev1 *src;
  253. size_t cd_len = sizeof(*curve_data) +
  254. (curve_data->points_per_channel*sizeof(*dst) + 2) *
  255. curve_data->channels;
  256. unsigned int i, j;
  257. void *source, *target;
  258. priv->curve_data = kmalloc(cd_len, GFP_KERNEL);
  259. if (!priv->curve_data)
  260. return -ENOMEM;
  261. memcpy(priv->curve_data, curve_data, sizeof(*curve_data));
  262. source = curve_data->data;
  263. target = priv->curve_data->data;
  264. for (i = 0; i < curve_data->channels; i++) {
  265. __le16 *freq = source;
  266. source += sizeof(__le16);
  267. *((__le16 *)target) = *freq;
  268. target += sizeof(__le16);
  269. for (j = 0; j < curve_data->points_per_channel; j++) {
  270. memcpy(target, source, sizeof(*src));
  271. target += sizeof(*dst);
  272. source += sizeof(*src);
  273. }
  274. source++;
  275. }
  276. return 0;
  277. }
  278. static const char *p54_rf_chips[] = { "NULL", "Indigo?", "Duette",
  279. "Frisbee", "Xbow", "Longbow" };
  280. static int p54_init_xbow_synth(struct ieee80211_hw *dev);
  281. static int p54_parse_eeprom(struct ieee80211_hw *dev, void *eeprom, int len)
  282. {
  283. struct p54_common *priv = dev->priv;
  284. struct eeprom_pda_wrap *wrap = NULL;
  285. struct pda_entry *entry;
  286. unsigned int data_len, entry_len;
  287. void *tmp;
  288. int err;
  289. u8 *end = (u8 *)eeprom + len;
  290. DECLARE_MAC_BUF(mac);
  291. wrap = (struct eeprom_pda_wrap *) eeprom;
  292. entry = (void *)wrap->data + le16_to_cpu(wrap->len);
  293. /* verify that at least the entry length/code fits */
  294. while ((u8 *)entry <= end - sizeof(*entry)) {
  295. entry_len = le16_to_cpu(entry->len);
  296. data_len = ((entry_len - 1) << 1);
  297. /* abort if entry exceeds whole structure */
  298. if ((u8 *)entry + sizeof(*entry) + data_len > end)
  299. break;
  300. switch (le16_to_cpu(entry->code)) {
  301. case PDR_MAC_ADDRESS:
  302. SET_IEEE80211_PERM_ADDR(dev, entry->data);
  303. break;
  304. case PDR_PRISM_PA_CAL_OUTPUT_POWER_LIMITS:
  305. if (data_len < 2) {
  306. err = -EINVAL;
  307. goto err;
  308. }
  309. if (2 + entry->data[1]*sizeof(*priv->output_limit) > data_len) {
  310. err = -EINVAL;
  311. goto err;
  312. }
  313. priv->output_limit = kmalloc(entry->data[1] *
  314. sizeof(*priv->output_limit), GFP_KERNEL);
  315. if (!priv->output_limit) {
  316. err = -ENOMEM;
  317. goto err;
  318. }
  319. memcpy(priv->output_limit, &entry->data[2],
  320. entry->data[1]*sizeof(*priv->output_limit));
  321. priv->output_limit_len = entry->data[1];
  322. break;
  323. case PDR_PRISM_PA_CAL_CURVE_DATA: {
  324. struct pda_pa_curve_data *curve_data =
  325. (struct pda_pa_curve_data *)entry->data;
  326. if (data_len < sizeof(*curve_data)) {
  327. err = -EINVAL;
  328. goto err;
  329. }
  330. switch (curve_data->cal_method_rev) {
  331. case 0:
  332. err = p54_convert_rev0(dev, curve_data);
  333. break;
  334. case 1:
  335. err = p54_convert_rev1(dev, curve_data);
  336. break;
  337. default:
  338. printk(KERN_ERR "p54: unknown curve data "
  339. "revision %d\n",
  340. curve_data->cal_method_rev);
  341. err = -ENODEV;
  342. break;
  343. }
  344. if (err)
  345. goto err;
  346. }
  347. case PDR_PRISM_ZIF_TX_IQ_CALIBRATION:
  348. priv->iq_autocal = kmalloc(data_len, GFP_KERNEL);
  349. if (!priv->iq_autocal) {
  350. err = -ENOMEM;
  351. goto err;
  352. }
  353. memcpy(priv->iq_autocal, entry->data, data_len);
  354. priv->iq_autocal_len = data_len / sizeof(struct pda_iq_autocal_entry);
  355. break;
  356. case PDR_INTERFACE_LIST:
  357. tmp = entry->data;
  358. while ((u8 *)tmp < entry->data + data_len) {
  359. struct bootrec_exp_if *exp_if = tmp;
  360. if (le16_to_cpu(exp_if->if_id) == 0xF)
  361. priv->rxhw = le16_to_cpu(exp_if->variant) & 0x07;
  362. tmp += sizeof(struct bootrec_exp_if);
  363. }
  364. break;
  365. case PDR_HARDWARE_PLATFORM_COMPONENT_ID:
  366. priv->version = *(u8 *)(entry->data + 1);
  367. break;
  368. case PDR_END:
  369. /* make it overrun */
  370. entry_len = len;
  371. break;
  372. default:
  373. printk(KERN_INFO "p54: unknown eeprom code : 0x%x\n",
  374. le16_to_cpu(entry->code));
  375. break;
  376. }
  377. entry = (void *)entry + (entry_len + 1)*2;
  378. }
  379. if (!priv->iq_autocal || !priv->output_limit || !priv->curve_data) {
  380. printk(KERN_ERR "p54: not all required entries found in eeprom!\n");
  381. err = -EINVAL;
  382. goto err;
  383. }
  384. switch (priv->rxhw) {
  385. case 4: /* XBow */
  386. p54_init_xbow_synth(dev);
  387. case 1: /* Indigo? */
  388. case 2: /* Duette */
  389. dev->wiphy->bands[IEEE80211_BAND_5GHZ] = &band_5GHz;
  390. case 3: /* Frisbee */
  391. case 5: /* Longbow */
  392. dev->wiphy->bands[IEEE80211_BAND_2GHZ] = &band_2GHz;
  393. break;
  394. default:
  395. printk(KERN_ERR "%s: unsupported RF-Chip\n",
  396. wiphy_name(dev->wiphy));
  397. err = -EINVAL;
  398. goto err;
  399. }
  400. if (!is_valid_ether_addr(dev->wiphy->perm_addr)) {
  401. u8 perm_addr[ETH_ALEN];
  402. printk(KERN_WARNING "%s: Invalid hwaddr! Using randomly generated MAC addr\n",
  403. wiphy_name(dev->wiphy));
  404. random_ether_addr(perm_addr);
  405. SET_IEEE80211_PERM_ADDR(dev, perm_addr);
  406. }
  407. printk(KERN_INFO "%s: hwaddr %s, MAC:isl38%02x RF:%s\n",
  408. wiphy_name(dev->wiphy),
  409. print_mac(mac, dev->wiphy->perm_addr),
  410. priv->version, p54_rf_chips[priv->rxhw]);
  411. return 0;
  412. err:
  413. if (priv->iq_autocal) {
  414. kfree(priv->iq_autocal);
  415. priv->iq_autocal = NULL;
  416. }
  417. if (priv->output_limit) {
  418. kfree(priv->output_limit);
  419. priv->output_limit = NULL;
  420. }
  421. if (priv->curve_data) {
  422. kfree(priv->curve_data);
  423. priv->curve_data = NULL;
  424. }
  425. printk(KERN_ERR "p54: eeprom parse failed!\n");
  426. return err;
  427. }
  428. static int p54_rssi_to_dbm(struct ieee80211_hw *dev, int rssi)
  429. {
  430. /* TODO: get the rssi_add & rssi_mul data from the eeprom */
  431. return ((rssi * 0x83) / 64 - 400) / 4;
  432. }
  433. static int p54_rx_data(struct ieee80211_hw *dev, struct sk_buff *skb)
  434. {
  435. struct p54_common *priv = dev->priv;
  436. struct p54_rx_hdr *hdr = (struct p54_rx_hdr *) skb->data;
  437. struct ieee80211_rx_status rx_status = {0};
  438. u16 freq = le16_to_cpu(hdr->freq);
  439. size_t header_len = sizeof(*hdr);
  440. u32 tsf32;
  441. if (!(hdr->magic & cpu_to_le16(0x0001))) {
  442. if (priv->filter_flags & FIF_FCSFAIL)
  443. rx_status.flag |= RX_FLAG_FAILED_FCS_CRC;
  444. else
  445. return 0;
  446. }
  447. rx_status.signal = p54_rssi_to_dbm(dev, hdr->rssi);
  448. rx_status.noise = priv->noise;
  449. /* XX correct? */
  450. rx_status.qual = (100 * hdr->rssi) / 127;
  451. rx_status.rate_idx = (dev->conf.channel->band == IEEE80211_BAND_2GHZ ?
  452. hdr->rate : (hdr->rate - 4)) & 0xf;
  453. rx_status.freq = freq;
  454. rx_status.band = dev->conf.channel->band;
  455. rx_status.antenna = hdr->antenna;
  456. tsf32 = le32_to_cpu(hdr->tsf32);
  457. if (tsf32 < priv->tsf_low32)
  458. priv->tsf_high32++;
  459. rx_status.mactime = ((u64)priv->tsf_high32) << 32 | tsf32;
  460. priv->tsf_low32 = tsf32;
  461. rx_status.flag |= RX_FLAG_TSFT;
  462. if (hdr->magic & cpu_to_le16(0x4000))
  463. header_len += hdr->align[0];
  464. skb_pull(skb, header_len);
  465. skb_trim(skb, le16_to_cpu(hdr->len));
  466. ieee80211_rx_irqsafe(dev, skb, &rx_status);
  467. return -1;
  468. }
  469. static void inline p54_wake_free_queues(struct ieee80211_hw *dev)
  470. {
  471. struct p54_common *priv = dev->priv;
  472. int i;
  473. for (i = 0; i < dev->queues; i++)
  474. if (priv->tx_stats[i + 4].len < priv->tx_stats[i + 4].limit)
  475. ieee80211_wake_queue(dev, i);
  476. }
  477. static void p54_rx_frame_sent(struct ieee80211_hw *dev, struct sk_buff *skb)
  478. {
  479. struct p54_common *priv = dev->priv;
  480. struct p54_control_hdr *hdr = (struct p54_control_hdr *) skb->data;
  481. struct p54_frame_sent_hdr *payload = (struct p54_frame_sent_hdr *) hdr->data;
  482. struct sk_buff *entry = (struct sk_buff *) priv->tx_queue.next;
  483. u32 addr = le32_to_cpu(hdr->req_id) - priv->headroom;
  484. struct memrecord *range = NULL;
  485. u32 freed = 0;
  486. u32 last_addr = priv->rx_start;
  487. unsigned long flags;
  488. spin_lock_irqsave(&priv->tx_queue.lock, flags);
  489. while (entry != (struct sk_buff *)&priv->tx_queue) {
  490. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(entry);
  491. range = (void *)info->driver_data;
  492. if (range->start_addr == addr) {
  493. struct p54_control_hdr *entry_hdr;
  494. struct p54_tx_control_allocdata *entry_data;
  495. int pad = 0;
  496. if (entry->next != (struct sk_buff *)&priv->tx_queue) {
  497. struct ieee80211_tx_info *ni;
  498. struct memrecord *mr;
  499. ni = IEEE80211_SKB_CB(entry->next);
  500. mr = (struct memrecord *)ni->driver_data;
  501. freed = mr->start_addr - last_addr;
  502. } else
  503. freed = priv->rx_end - last_addr;
  504. last_addr = range->end_addr;
  505. __skb_unlink(entry, &priv->tx_queue);
  506. spin_unlock_irqrestore(&priv->tx_queue.lock, flags);
  507. memset(&info->status, 0, sizeof(info->status));
  508. entry_hdr = (struct p54_control_hdr *) entry->data;
  509. entry_data = (struct p54_tx_control_allocdata *) entry_hdr->data;
  510. if ((entry_hdr->magic1 & cpu_to_le16(0x4000)) != 0)
  511. pad = entry_data->align[0];
  512. priv->tx_stats[entry_data->hw_queue].len--;
  513. if (!(info->flags & IEEE80211_TX_CTL_NO_ACK)) {
  514. if (!(payload->status & 0x01))
  515. info->flags |= IEEE80211_TX_STAT_ACK;
  516. else
  517. info->status.excessive_retries = 1;
  518. }
  519. info->status.retry_count = payload->retries - 1;
  520. info->status.ack_signal = p54_rssi_to_dbm(dev,
  521. le16_to_cpu(payload->ack_rssi));
  522. skb_pull(entry, sizeof(*hdr) + pad + sizeof(*entry_data));
  523. ieee80211_tx_status_irqsafe(dev, entry);
  524. goto out;
  525. } else
  526. last_addr = range->end_addr;
  527. entry = entry->next;
  528. }
  529. spin_unlock_irqrestore(&priv->tx_queue.lock, flags);
  530. out:
  531. if (freed >= IEEE80211_MAX_RTS_THRESHOLD + 0x170 +
  532. sizeof(struct p54_control_hdr))
  533. p54_wake_free_queues(dev);
  534. }
  535. static void p54_rx_eeprom_readback(struct ieee80211_hw *dev,
  536. struct sk_buff *skb)
  537. {
  538. struct p54_control_hdr *hdr = (struct p54_control_hdr *) skb->data;
  539. struct p54_eeprom_lm86 *eeprom = (struct p54_eeprom_lm86 *) hdr->data;
  540. struct p54_common *priv = dev->priv;
  541. if (!priv->eeprom)
  542. return ;
  543. memcpy(priv->eeprom, eeprom->data, le16_to_cpu(eeprom->len));
  544. complete(&priv->eeprom_comp);
  545. }
  546. static void p54_rx_stats(struct ieee80211_hw *dev, struct sk_buff *skb)
  547. {
  548. struct p54_common *priv = dev->priv;
  549. struct p54_control_hdr *hdr = (struct p54_control_hdr *) skb->data;
  550. struct p54_statistics *stats = (struct p54_statistics *) hdr->data;
  551. u32 tsf32 = le32_to_cpu(stats->tsf32);
  552. if (tsf32 < priv->tsf_low32)
  553. priv->tsf_high32++;
  554. priv->tsf_low32 = tsf32;
  555. priv->stats.dot11RTSFailureCount = le32_to_cpu(stats->rts_fail);
  556. priv->stats.dot11RTSSuccessCount = le32_to_cpu(stats->rts_success);
  557. priv->stats.dot11FCSErrorCount = le32_to_cpu(stats->rx_bad_fcs);
  558. priv->noise = p54_rssi_to_dbm(dev, le32_to_cpu(stats->noise));
  559. complete(&priv->stats_comp);
  560. mod_timer(&priv->stats_timer, jiffies + 5 * HZ);
  561. }
  562. static int p54_rx_control(struct ieee80211_hw *dev, struct sk_buff *skb)
  563. {
  564. struct p54_control_hdr *hdr = (struct p54_control_hdr *) skb->data;
  565. switch (le16_to_cpu(hdr->type)) {
  566. case P54_CONTROL_TYPE_TXDONE:
  567. p54_rx_frame_sent(dev, skb);
  568. break;
  569. case P54_CONTROL_TYPE_BBP:
  570. break;
  571. case P54_CONTROL_TYPE_STAT_READBACK:
  572. p54_rx_stats(dev, skb);
  573. break;
  574. case P54_CONTROL_TYPE_EEPROM_READBACK:
  575. p54_rx_eeprom_readback(dev, skb);
  576. break;
  577. default:
  578. printk(KERN_DEBUG "%s: not handling 0x%02x type control frame\n",
  579. wiphy_name(dev->wiphy), le16_to_cpu(hdr->type));
  580. break;
  581. }
  582. return 0;
  583. }
  584. /* returns zero if skb can be reused */
  585. int p54_rx(struct ieee80211_hw *dev, struct sk_buff *skb)
  586. {
  587. u8 type = le16_to_cpu(*((__le16 *)skb->data)) >> 8;
  588. if (type == 0x80)
  589. return p54_rx_control(dev, skb);
  590. else
  591. return p54_rx_data(dev, skb);
  592. }
  593. EXPORT_SYMBOL_GPL(p54_rx);
  594. /*
  595. * So, the firmware is somewhat stupid and doesn't know what places in its
  596. * memory incoming data should go to. By poking around in the firmware, we
  597. * can find some unused memory to upload our packets to. However, data that we
  598. * want the card to TX needs to stay intact until the card has told us that
  599. * it is done with it. This function finds empty places we can upload to and
  600. * marks allocated areas as reserved if necessary. p54_rx_frame_sent frees
  601. * allocated areas.
  602. */
  603. static void p54_assign_address(struct ieee80211_hw *dev, struct sk_buff *skb,
  604. struct p54_control_hdr *data, u32 len)
  605. {
  606. struct p54_common *priv = dev->priv;
  607. struct sk_buff *entry = priv->tx_queue.next;
  608. struct sk_buff *target_skb = NULL;
  609. u32 last_addr = priv->rx_start;
  610. u32 largest_hole = 0;
  611. u32 target_addr = priv->rx_start;
  612. unsigned long flags;
  613. unsigned int left;
  614. len = (len + priv->headroom + priv->tailroom + 3) & ~0x3;
  615. spin_lock_irqsave(&priv->tx_queue.lock, flags);
  616. left = skb_queue_len(&priv->tx_queue);
  617. while (left--) {
  618. u32 hole_size;
  619. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(entry);
  620. struct memrecord *range = (void *)info->driver_data;
  621. hole_size = range->start_addr - last_addr;
  622. if (!target_skb && hole_size >= len) {
  623. target_skb = entry->prev;
  624. hole_size -= len;
  625. target_addr = last_addr;
  626. }
  627. largest_hole = max(largest_hole, hole_size);
  628. last_addr = range->end_addr;
  629. entry = entry->next;
  630. }
  631. if (!target_skb && priv->rx_end - last_addr >= len) {
  632. target_skb = priv->tx_queue.prev;
  633. largest_hole = max(largest_hole, priv->rx_end - last_addr - len);
  634. if (!skb_queue_empty(&priv->tx_queue)) {
  635. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(target_skb);
  636. struct memrecord *range = (void *)info->driver_data;
  637. target_addr = range->end_addr;
  638. }
  639. } else
  640. largest_hole = max(largest_hole, priv->rx_end - last_addr);
  641. if (skb) {
  642. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  643. struct memrecord *range = (void *)info->driver_data;
  644. range->start_addr = target_addr;
  645. range->end_addr = target_addr + len;
  646. __skb_queue_after(&priv->tx_queue, target_skb, skb);
  647. if (largest_hole < priv->rx_mtu + priv->headroom +
  648. priv->tailroom +
  649. sizeof(struct p54_control_hdr))
  650. ieee80211_stop_queues(dev);
  651. }
  652. spin_unlock_irqrestore(&priv->tx_queue.lock, flags);
  653. data->req_id = cpu_to_le32(target_addr + priv->headroom);
  654. }
  655. int p54_read_eeprom(struct ieee80211_hw *dev)
  656. {
  657. struct p54_common *priv = dev->priv;
  658. struct p54_control_hdr *hdr = NULL;
  659. struct p54_eeprom_lm86 *eeprom_hdr;
  660. size_t eeprom_size = 0x2020, offset = 0, blocksize;
  661. int ret = -ENOMEM;
  662. void *eeprom = NULL;
  663. hdr = (struct p54_control_hdr *)kzalloc(sizeof(*hdr) +
  664. sizeof(*eeprom_hdr) + EEPROM_READBACK_LEN, GFP_KERNEL);
  665. if (!hdr)
  666. goto free;
  667. priv->eeprom = kzalloc(EEPROM_READBACK_LEN, GFP_KERNEL);
  668. if (!priv->eeprom)
  669. goto free;
  670. eeprom = kzalloc(eeprom_size, GFP_KERNEL);
  671. if (!eeprom)
  672. goto free;
  673. hdr->magic1 = cpu_to_le16(0x8000);
  674. hdr->type = cpu_to_le16(P54_CONTROL_TYPE_EEPROM_READBACK);
  675. hdr->retry1 = hdr->retry2 = 0;
  676. eeprom_hdr = (struct p54_eeprom_lm86 *) hdr->data;
  677. while (eeprom_size) {
  678. blocksize = min(eeprom_size, (size_t)EEPROM_READBACK_LEN);
  679. hdr->len = cpu_to_le16(blocksize + sizeof(*eeprom_hdr));
  680. eeprom_hdr->offset = cpu_to_le16(offset);
  681. eeprom_hdr->len = cpu_to_le16(blocksize);
  682. p54_assign_address(dev, NULL, hdr, le16_to_cpu(hdr->len) +
  683. sizeof(*hdr));
  684. priv->tx(dev, hdr, le16_to_cpu(hdr->len) + sizeof(*hdr), 0);
  685. if (!wait_for_completion_interruptible_timeout(&priv->eeprom_comp, HZ)) {
  686. printk(KERN_ERR "%s: device does not respond!\n",
  687. wiphy_name(dev->wiphy));
  688. ret = -EBUSY;
  689. goto free;
  690. }
  691. memcpy(eeprom + offset, priv->eeprom, blocksize);
  692. offset += blocksize;
  693. eeprom_size -= blocksize;
  694. }
  695. ret = p54_parse_eeprom(dev, eeprom, offset);
  696. free:
  697. kfree(priv->eeprom);
  698. priv->eeprom = NULL;
  699. kfree(hdr);
  700. kfree(eeprom);
  701. return ret;
  702. }
  703. EXPORT_SYMBOL_GPL(p54_read_eeprom);
  704. static int p54_tx(struct ieee80211_hw *dev, struct sk_buff *skb)
  705. {
  706. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  707. struct ieee80211_tx_queue_stats *current_queue;
  708. struct p54_common *priv = dev->priv;
  709. struct p54_control_hdr *hdr;
  710. struct ieee80211_hdr *ieee80211hdr = (struct ieee80211_hdr *)skb->data;
  711. struct p54_tx_control_allocdata *txhdr;
  712. size_t padding, len;
  713. u8 rate;
  714. u8 cts_rate = 0x20;
  715. current_queue = &priv->tx_stats[skb_get_queue_mapping(skb) + 4];
  716. if (unlikely(current_queue->len > current_queue->limit))
  717. return NETDEV_TX_BUSY;
  718. current_queue->len++;
  719. current_queue->count++;
  720. if (current_queue->len == current_queue->limit)
  721. ieee80211_stop_queue(dev, skb_get_queue_mapping(skb));
  722. padding = (unsigned long)(skb->data - (sizeof(*hdr) + sizeof(*txhdr))) & 3;
  723. len = skb->len;
  724. txhdr = (struct p54_tx_control_allocdata *)
  725. skb_push(skb, sizeof(*txhdr) + padding);
  726. hdr = (struct p54_control_hdr *) skb_push(skb, sizeof(*hdr));
  727. if (padding)
  728. hdr->magic1 = cpu_to_le16(0x4010);
  729. else
  730. hdr->magic1 = cpu_to_le16(0x0010);
  731. hdr->len = cpu_to_le16(len);
  732. hdr->type = (info->flags & IEEE80211_TX_CTL_NO_ACK) ? 0 : cpu_to_le16(1);
  733. hdr->retry1 = hdr->retry2 = info->control.retry_limit;
  734. /* TODO: add support for alternate retry TX rates */
  735. rate = ieee80211_get_tx_rate(dev, info)->hw_value;
  736. if (info->flags & IEEE80211_TX_CTL_SHORT_PREAMBLE) {
  737. rate |= 0x10;
  738. cts_rate |= 0x10;
  739. }
  740. if (info->flags & IEEE80211_TX_CTL_USE_RTS_CTS) {
  741. rate |= 0x40;
  742. cts_rate |= ieee80211_get_rts_cts_rate(dev, info)->hw_value;
  743. } else if (info->flags & IEEE80211_TX_CTL_USE_CTS_PROTECT) {
  744. rate |= 0x20;
  745. cts_rate |= ieee80211_get_rts_cts_rate(dev, info)->hw_value;
  746. }
  747. memset(txhdr->rateset, rate, 8);
  748. txhdr->key_type = 0;
  749. txhdr->key_len = 0;
  750. txhdr->hw_queue = skb_get_queue_mapping(skb) + 4;
  751. txhdr->tx_antenna = (info->antenna_sel_tx == 0) ?
  752. 2 : info->antenna_sel_tx - 1;
  753. txhdr->output_power = priv->output_power;
  754. txhdr->cts_rate = (info->flags & IEEE80211_TX_CTL_NO_ACK) ?
  755. 0 : cts_rate;
  756. if (padding)
  757. txhdr->align[0] = padding;
  758. /* FIXME: The sequence that follows is needed for this driver to
  759. * work with mac80211 since "mac80211: fix TX sequence numbers".
  760. * As with the temporary code in rt2x00, changes will be needed
  761. * to get proper sequence numbers on beacons. In addition, this
  762. * patch places the sequence number in the hardware state, which
  763. * limits us to a single virtual state.
  764. */
  765. if (info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ) {
  766. if (info->flags & IEEE80211_TX_CTL_FIRST_FRAGMENT)
  767. priv->seqno += 0x10;
  768. ieee80211hdr->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
  769. ieee80211hdr->seq_ctrl |= cpu_to_le16(priv->seqno);
  770. }
  771. /* modifies skb->cb and with it info, so must be last! */
  772. p54_assign_address(dev, skb, hdr, skb->len);
  773. priv->tx(dev, hdr, skb->len, 0);
  774. return 0;
  775. }
  776. static int p54_set_filter(struct ieee80211_hw *dev, u16 filter_type,
  777. const u8 *bssid)
  778. {
  779. struct p54_common *priv = dev->priv;
  780. struct p54_control_hdr *hdr;
  781. struct p54_tx_control_filter *filter;
  782. size_t data_len;
  783. hdr = kzalloc(sizeof(*hdr) + sizeof(*filter) +
  784. priv->tx_hdr_len, GFP_ATOMIC);
  785. if (!hdr)
  786. return -ENOMEM;
  787. hdr = (void *)hdr + priv->tx_hdr_len;
  788. filter = (struct p54_tx_control_filter *) hdr->data;
  789. hdr->magic1 = cpu_to_le16(0x8001);
  790. hdr->type = cpu_to_le16(P54_CONTROL_TYPE_FILTER_SET);
  791. priv->filter_type = filter->filter_type = cpu_to_le16(filter_type);
  792. memcpy(filter->mac_addr, priv->mac_addr, ETH_ALEN);
  793. if (!bssid)
  794. memset(filter->bssid, ~0, ETH_ALEN);
  795. else
  796. memcpy(filter->bssid, bssid, ETH_ALEN);
  797. filter->rx_antenna = priv->rx_antenna;
  798. if (priv->fw_var < 0x500) {
  799. data_len = P54_TX_CONTROL_FILTER_V1_LEN;
  800. filter->v1.basic_rate_mask = cpu_to_le32(0x15F);
  801. filter->v1.rx_addr = cpu_to_le32(priv->rx_end);
  802. filter->v1.max_rx = cpu_to_le16(priv->rx_mtu);
  803. filter->v1.rxhw = cpu_to_le16(priv->rxhw);
  804. filter->v1.wakeup_timer = cpu_to_le16(500);
  805. } else {
  806. data_len = P54_TX_CONTROL_FILTER_V2_LEN;
  807. filter->v2.rx_addr = cpu_to_le32(priv->rx_end);
  808. filter->v2.max_rx = cpu_to_le16(priv->rx_mtu);
  809. filter->v2.rxhw = cpu_to_le16(priv->rxhw);
  810. filter->v2.timer = cpu_to_le16(1000);
  811. }
  812. hdr->len = cpu_to_le16(data_len);
  813. p54_assign_address(dev, NULL, hdr, sizeof(*hdr) + data_len);
  814. priv->tx(dev, hdr, sizeof(*hdr) + data_len, 1);
  815. return 0;
  816. }
  817. static int p54_set_freq(struct ieee80211_hw *dev, __le16 freq)
  818. {
  819. struct p54_common *priv = dev->priv;
  820. struct p54_control_hdr *hdr;
  821. struct p54_tx_control_channel *chan;
  822. unsigned int i;
  823. size_t data_len;
  824. void *entry;
  825. hdr = kzalloc(sizeof(*hdr) + sizeof(*chan) +
  826. priv->tx_hdr_len, GFP_KERNEL);
  827. if (!hdr)
  828. return -ENOMEM;
  829. hdr = (void *)hdr + priv->tx_hdr_len;
  830. chan = (struct p54_tx_control_channel *) hdr->data;
  831. hdr->magic1 = cpu_to_le16(0x8001);
  832. hdr->type = cpu_to_le16(P54_CONTROL_TYPE_CHANNEL_CHANGE);
  833. chan->flags = cpu_to_le16(0x1);
  834. chan->dwell = cpu_to_le16(0x0);
  835. for (i = 0; i < priv->iq_autocal_len; i++) {
  836. if (priv->iq_autocal[i].freq != freq)
  837. continue;
  838. memcpy(&chan->iq_autocal, &priv->iq_autocal[i],
  839. sizeof(*priv->iq_autocal));
  840. break;
  841. }
  842. if (i == priv->iq_autocal_len)
  843. goto err;
  844. for (i = 0; i < priv->output_limit_len; i++) {
  845. if (priv->output_limit[i].freq != freq)
  846. continue;
  847. chan->val_barker = 0x38;
  848. chan->val_bpsk = chan->dup_bpsk =
  849. priv->output_limit[i].val_bpsk;
  850. chan->val_qpsk = chan->dup_qpsk =
  851. priv->output_limit[i].val_qpsk;
  852. chan->val_16qam = chan->dup_16qam =
  853. priv->output_limit[i].val_16qam;
  854. chan->val_64qam = chan->dup_64qam =
  855. priv->output_limit[i].val_64qam;
  856. break;
  857. }
  858. if (i == priv->output_limit_len)
  859. goto err;
  860. entry = priv->curve_data->data;
  861. for (i = 0; i < priv->curve_data->channels; i++) {
  862. if (*((__le16 *)entry) != freq) {
  863. entry += sizeof(__le16);
  864. entry += sizeof(struct p54_pa_curve_data_sample) *
  865. priv->curve_data->points_per_channel;
  866. continue;
  867. }
  868. entry += sizeof(__le16);
  869. chan->pa_points_per_curve =
  870. min(priv->curve_data->points_per_channel, (u8) 8);
  871. memcpy(chan->curve_data, entry, sizeof(*chan->curve_data) *
  872. chan->pa_points_per_curve);
  873. break;
  874. }
  875. if (priv->fw_var < 0x500) {
  876. data_len = P54_TX_CONTROL_CHANNEL_V1_LEN;
  877. chan->v1.rssical_mul = cpu_to_le16(130);
  878. chan->v1.rssical_add = cpu_to_le16(0xfe70);
  879. } else {
  880. data_len = P54_TX_CONTROL_CHANNEL_V2_LEN;
  881. chan->v2.rssical_mul = cpu_to_le16(130);
  882. chan->v2.rssical_add = cpu_to_le16(0xfe70);
  883. chan->v2.basic_rate_mask = cpu_to_le32(0x15f);
  884. }
  885. hdr->len = cpu_to_le16(data_len);
  886. p54_assign_address(dev, NULL, hdr, sizeof(*hdr) + data_len);
  887. priv->tx(dev, hdr, sizeof(*hdr) + data_len, 1);
  888. return 0;
  889. err:
  890. printk(KERN_ERR "%s: frequency change failed\n", wiphy_name(dev->wiphy));
  891. kfree(hdr);
  892. return -EINVAL;
  893. }
  894. static int p54_set_leds(struct ieee80211_hw *dev, int mode, int link, int act)
  895. {
  896. struct p54_common *priv = dev->priv;
  897. struct p54_control_hdr *hdr;
  898. struct p54_tx_control_led *led;
  899. hdr = kzalloc(sizeof(*hdr) + sizeof(*led) +
  900. priv->tx_hdr_len, GFP_KERNEL);
  901. if (!hdr)
  902. return -ENOMEM;
  903. hdr = (void *)hdr + priv->tx_hdr_len;
  904. hdr->magic1 = cpu_to_le16(0x8001);
  905. hdr->len = cpu_to_le16(sizeof(*led));
  906. hdr->type = cpu_to_le16(P54_CONTROL_TYPE_LED);
  907. p54_assign_address(dev, NULL, hdr, sizeof(*hdr) + sizeof(*led));
  908. led = (struct p54_tx_control_led *) hdr->data;
  909. led->mode = cpu_to_le16(mode);
  910. led->led_permanent = cpu_to_le16(link);
  911. led->led_temporary = cpu_to_le16(act);
  912. led->duration = cpu_to_le16(1000);
  913. priv->tx(dev, hdr, sizeof(*hdr) + sizeof(*led), 1);
  914. return 0;
  915. }
  916. #define P54_SET_QUEUE(queue, ai_fs, cw_min, cw_max, _txop) \
  917. do { \
  918. queue.aifs = cpu_to_le16(ai_fs); \
  919. queue.cwmin = cpu_to_le16(cw_min); \
  920. queue.cwmax = cpu_to_le16(cw_max); \
  921. queue.txop = cpu_to_le16(_txop); \
  922. } while(0)
  923. static void p54_init_vdcf(struct ieee80211_hw *dev)
  924. {
  925. struct p54_common *priv = dev->priv;
  926. struct p54_control_hdr *hdr;
  927. struct p54_tx_control_vdcf *vdcf;
  928. /* all USB V1 adapters need a extra headroom */
  929. hdr = (void *)priv->cached_vdcf + priv->tx_hdr_len;
  930. hdr->magic1 = cpu_to_le16(0x8001);
  931. hdr->len = cpu_to_le16(sizeof(*vdcf));
  932. hdr->type = cpu_to_le16(P54_CONTROL_TYPE_DCFINIT);
  933. hdr->req_id = cpu_to_le32(priv->rx_start);
  934. vdcf = (struct p54_tx_control_vdcf *) hdr->data;
  935. P54_SET_QUEUE(vdcf->queue[0], 0x0002, 0x0003, 0x0007, 47);
  936. P54_SET_QUEUE(vdcf->queue[1], 0x0002, 0x0007, 0x000f, 94);
  937. P54_SET_QUEUE(vdcf->queue[2], 0x0003, 0x000f, 0x03ff, 0);
  938. P54_SET_QUEUE(vdcf->queue[3], 0x0007, 0x000f, 0x03ff, 0);
  939. }
  940. static void p54_set_vdcf(struct ieee80211_hw *dev)
  941. {
  942. struct p54_common *priv = dev->priv;
  943. struct p54_control_hdr *hdr;
  944. struct p54_tx_control_vdcf *vdcf;
  945. hdr = (void *)priv->cached_vdcf + priv->tx_hdr_len;
  946. p54_assign_address(dev, NULL, hdr, sizeof(*hdr) + sizeof(*vdcf));
  947. vdcf = (struct p54_tx_control_vdcf *) hdr->data;
  948. if (dev->conf.flags & IEEE80211_CONF_SHORT_SLOT_TIME) {
  949. vdcf->slottime = 9;
  950. vdcf->magic1 = 0x10;
  951. vdcf->magic2 = 0x00;
  952. } else {
  953. vdcf->slottime = 20;
  954. vdcf->magic1 = 0x0a;
  955. vdcf->magic2 = 0x06;
  956. }
  957. /* (see prism54/isl_oid.h for further details) */
  958. vdcf->frameburst = cpu_to_le16(0);
  959. priv->tx(dev, hdr, sizeof(*hdr) + sizeof(*vdcf), 0);
  960. }
  961. static int p54_start(struct ieee80211_hw *dev)
  962. {
  963. struct p54_common *priv = dev->priv;
  964. int err;
  965. if (!priv->cached_vdcf) {
  966. priv->cached_vdcf = kzalloc(sizeof(struct p54_tx_control_vdcf)+
  967. priv->tx_hdr_len + sizeof(struct p54_control_hdr),
  968. GFP_KERNEL);
  969. if (!priv->cached_vdcf)
  970. return -ENOMEM;
  971. }
  972. if (!priv->cached_stats) {
  973. priv->cached_stats = kzalloc(sizeof(struct p54_statistics) +
  974. priv->tx_hdr_len + sizeof(struct p54_control_hdr),
  975. GFP_KERNEL);
  976. if (!priv->cached_stats) {
  977. kfree(priv->cached_vdcf);
  978. priv->cached_vdcf = NULL;
  979. return -ENOMEM;
  980. }
  981. }
  982. err = priv->open(dev);
  983. if (!err)
  984. priv->mode = NL80211_IFTYPE_MONITOR;
  985. p54_init_vdcf(dev);
  986. mod_timer(&priv->stats_timer, jiffies + HZ);
  987. return err;
  988. }
  989. static void p54_stop(struct ieee80211_hw *dev)
  990. {
  991. struct p54_common *priv = dev->priv;
  992. struct sk_buff *skb;
  993. del_timer(&priv->stats_timer);
  994. while ((skb = skb_dequeue(&priv->tx_queue)))
  995. kfree_skb(skb);
  996. priv->stop(dev);
  997. priv->tsf_high32 = priv->tsf_low32 = 0;
  998. priv->mode = NL80211_IFTYPE_UNSPECIFIED;
  999. }
  1000. static int p54_add_interface(struct ieee80211_hw *dev,
  1001. struct ieee80211_if_init_conf *conf)
  1002. {
  1003. struct p54_common *priv = dev->priv;
  1004. if (priv->mode != NL80211_IFTYPE_MONITOR)
  1005. return -EOPNOTSUPP;
  1006. switch (conf->type) {
  1007. case NL80211_IFTYPE_STATION:
  1008. priv->mode = conf->type;
  1009. break;
  1010. default:
  1011. return -EOPNOTSUPP;
  1012. }
  1013. memcpy(priv->mac_addr, conf->mac_addr, ETH_ALEN);
  1014. p54_set_filter(dev, 0, NULL);
  1015. switch (conf->type) {
  1016. case NL80211_IFTYPE_STATION:
  1017. p54_set_filter(dev, 1, NULL);
  1018. break;
  1019. default:
  1020. BUG(); /* impossible */
  1021. break;
  1022. }
  1023. p54_set_leds(dev, 1, 0, 0);
  1024. return 0;
  1025. }
  1026. static void p54_remove_interface(struct ieee80211_hw *dev,
  1027. struct ieee80211_if_init_conf *conf)
  1028. {
  1029. struct p54_common *priv = dev->priv;
  1030. priv->mode = NL80211_IFTYPE_MONITOR;
  1031. memset(priv->mac_addr, 0, ETH_ALEN);
  1032. p54_set_filter(dev, 0, NULL);
  1033. }
  1034. static int p54_config(struct ieee80211_hw *dev, struct ieee80211_conf *conf)
  1035. {
  1036. int ret;
  1037. struct p54_common *priv = dev->priv;
  1038. mutex_lock(&priv->conf_mutex);
  1039. priv->rx_antenna = (conf->antenna_sel_rx == 0) ?
  1040. 2 : conf->antenna_sel_tx - 1;
  1041. priv->output_power = conf->power_level << 2;
  1042. ret = p54_set_freq(dev, cpu_to_le16(conf->channel->center_freq));
  1043. p54_set_vdcf(dev);
  1044. mutex_unlock(&priv->conf_mutex);
  1045. return ret;
  1046. }
  1047. static int p54_config_interface(struct ieee80211_hw *dev,
  1048. struct ieee80211_vif *vif,
  1049. struct ieee80211_if_conf *conf)
  1050. {
  1051. struct p54_common *priv = dev->priv;
  1052. mutex_lock(&priv->conf_mutex);
  1053. p54_set_filter(dev, 0, conf->bssid);
  1054. p54_set_leds(dev, 1, !is_multicast_ether_addr(conf->bssid), 0);
  1055. memcpy(priv->bssid, conf->bssid, ETH_ALEN);
  1056. mutex_unlock(&priv->conf_mutex);
  1057. return 0;
  1058. }
  1059. static void p54_configure_filter(struct ieee80211_hw *dev,
  1060. unsigned int changed_flags,
  1061. unsigned int *total_flags,
  1062. int mc_count, struct dev_mc_list *mclist)
  1063. {
  1064. struct p54_common *priv = dev->priv;
  1065. *total_flags &= FIF_BCN_PRBRESP_PROMISC |
  1066. FIF_PROMISC_IN_BSS |
  1067. FIF_FCSFAIL;
  1068. priv->filter_flags = *total_flags;
  1069. if (changed_flags & FIF_BCN_PRBRESP_PROMISC) {
  1070. if (*total_flags & FIF_BCN_PRBRESP_PROMISC)
  1071. p54_set_filter(dev, le16_to_cpu(priv->filter_type),
  1072. NULL);
  1073. else
  1074. p54_set_filter(dev, le16_to_cpu(priv->filter_type),
  1075. priv->bssid);
  1076. }
  1077. if (changed_flags & FIF_PROMISC_IN_BSS) {
  1078. if (*total_flags & FIF_PROMISC_IN_BSS)
  1079. p54_set_filter(dev, le16_to_cpu(priv->filter_type) |
  1080. 0x8, NULL);
  1081. else
  1082. p54_set_filter(dev, le16_to_cpu(priv->filter_type) &
  1083. ~0x8, priv->bssid);
  1084. }
  1085. }
  1086. static int p54_conf_tx(struct ieee80211_hw *dev, u16 queue,
  1087. const struct ieee80211_tx_queue_params *params)
  1088. {
  1089. struct p54_common *priv = dev->priv;
  1090. struct p54_tx_control_vdcf *vdcf;
  1091. vdcf = (struct p54_tx_control_vdcf *)(((struct p54_control_hdr *)
  1092. ((void *)priv->cached_vdcf + priv->tx_hdr_len))->data);
  1093. if ((params) && !(queue > 4)) {
  1094. P54_SET_QUEUE(vdcf->queue[queue], params->aifs,
  1095. params->cw_min, params->cw_max, params->txop);
  1096. } else
  1097. return -EINVAL;
  1098. p54_set_vdcf(dev);
  1099. return 0;
  1100. }
  1101. static int p54_init_xbow_synth(struct ieee80211_hw *dev)
  1102. {
  1103. struct p54_common *priv = dev->priv;
  1104. struct p54_control_hdr *hdr;
  1105. struct p54_tx_control_xbow_synth *xbow;
  1106. hdr = kzalloc(sizeof(*hdr) + sizeof(*xbow) +
  1107. priv->tx_hdr_len, GFP_KERNEL);
  1108. if (!hdr)
  1109. return -ENOMEM;
  1110. hdr = (void *)hdr + priv->tx_hdr_len;
  1111. hdr->magic1 = cpu_to_le16(0x8001);
  1112. hdr->len = cpu_to_le16(sizeof(*xbow));
  1113. hdr->type = cpu_to_le16(P54_CONTROL_TYPE_XBOW_SYNTH_CFG);
  1114. p54_assign_address(dev, NULL, hdr, sizeof(*hdr) + sizeof(*xbow));
  1115. xbow = (struct p54_tx_control_xbow_synth *) hdr->data;
  1116. xbow->magic1 = cpu_to_le16(0x1);
  1117. xbow->magic2 = cpu_to_le16(0x2);
  1118. xbow->freq = cpu_to_le16(5390);
  1119. priv->tx(dev, hdr, sizeof(*hdr) + sizeof(*xbow), 1);
  1120. return 0;
  1121. }
  1122. static void p54_statistics_timer(unsigned long data)
  1123. {
  1124. struct ieee80211_hw *dev = (struct ieee80211_hw *) data;
  1125. struct p54_common *priv = dev->priv;
  1126. struct p54_control_hdr *hdr;
  1127. struct p54_statistics *stats;
  1128. BUG_ON(!priv->cached_stats);
  1129. hdr = (void *)priv->cached_stats + priv->tx_hdr_len;
  1130. hdr->magic1 = cpu_to_le16(0x8000);
  1131. hdr->len = cpu_to_le16(sizeof(*stats));
  1132. hdr->type = cpu_to_le16(P54_CONTROL_TYPE_STAT_READBACK);
  1133. p54_assign_address(dev, NULL, hdr, sizeof(*hdr) + sizeof(*stats));
  1134. priv->tx(dev, hdr, sizeof(*hdr) + sizeof(*stats), 0);
  1135. }
  1136. static int p54_get_stats(struct ieee80211_hw *dev,
  1137. struct ieee80211_low_level_stats *stats)
  1138. {
  1139. struct p54_common *priv = dev->priv;
  1140. del_timer(&priv->stats_timer);
  1141. p54_statistics_timer((unsigned long)dev);
  1142. if (!wait_for_completion_interruptible_timeout(&priv->stats_comp, HZ)) {
  1143. printk(KERN_ERR "%s: device does not respond!\n",
  1144. wiphy_name(dev->wiphy));
  1145. return -EBUSY;
  1146. }
  1147. memcpy(stats, &priv->stats, sizeof(*stats));
  1148. return 0;
  1149. }
  1150. static int p54_get_tx_stats(struct ieee80211_hw *dev,
  1151. struct ieee80211_tx_queue_stats *stats)
  1152. {
  1153. struct p54_common *priv = dev->priv;
  1154. memcpy(stats, &priv->tx_stats[4], sizeof(stats[0]) * dev->queues);
  1155. return 0;
  1156. }
  1157. static const struct ieee80211_ops p54_ops = {
  1158. .tx = p54_tx,
  1159. .start = p54_start,
  1160. .stop = p54_stop,
  1161. .add_interface = p54_add_interface,
  1162. .remove_interface = p54_remove_interface,
  1163. .config = p54_config,
  1164. .config_interface = p54_config_interface,
  1165. .configure_filter = p54_configure_filter,
  1166. .conf_tx = p54_conf_tx,
  1167. .get_stats = p54_get_stats,
  1168. .get_tx_stats = p54_get_tx_stats
  1169. };
  1170. struct ieee80211_hw *p54_init_common(size_t priv_data_len)
  1171. {
  1172. struct ieee80211_hw *dev;
  1173. struct p54_common *priv;
  1174. dev = ieee80211_alloc_hw(priv_data_len, &p54_ops);
  1175. if (!dev)
  1176. return NULL;
  1177. priv = dev->priv;
  1178. priv->mode = NL80211_IFTYPE_UNSPECIFIED;
  1179. skb_queue_head_init(&priv->tx_queue);
  1180. dev->flags = IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING | /* not sure */
  1181. IEEE80211_HW_RX_INCLUDES_FCS |
  1182. IEEE80211_HW_SIGNAL_DBM |
  1183. IEEE80211_HW_NOISE_DBM;
  1184. dev->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION);
  1185. dev->channel_change_time = 1000; /* TODO: find actual value */
  1186. priv->tx_stats[0].limit = 1;
  1187. priv->tx_stats[1].limit = 1;
  1188. priv->tx_stats[2].limit = 1;
  1189. priv->tx_stats[3].limit = 1;
  1190. priv->tx_stats[4].limit = 5;
  1191. dev->queues = 1;
  1192. priv->noise = -94;
  1193. dev->extra_tx_headroom = sizeof(struct p54_control_hdr) + 4 +
  1194. sizeof(struct p54_tx_control_allocdata);
  1195. mutex_init(&priv->conf_mutex);
  1196. init_completion(&priv->eeprom_comp);
  1197. init_completion(&priv->stats_comp);
  1198. setup_timer(&priv->stats_timer, p54_statistics_timer,
  1199. (unsigned long)dev);
  1200. return dev;
  1201. }
  1202. EXPORT_SYMBOL_GPL(p54_init_common);
  1203. void p54_free_common(struct ieee80211_hw *dev)
  1204. {
  1205. struct p54_common *priv = dev->priv;
  1206. kfree(priv->cached_stats);
  1207. kfree(priv->iq_autocal);
  1208. kfree(priv->output_limit);
  1209. kfree(priv->curve_data);
  1210. kfree(priv->cached_vdcf);
  1211. }
  1212. EXPORT_SYMBOL_GPL(p54_free_common);
  1213. static int __init p54_init(void)
  1214. {
  1215. return 0;
  1216. }
  1217. static void __exit p54_exit(void)
  1218. {
  1219. }
  1220. module_init(p54_init);
  1221. module_exit(p54_exit);