p54common.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435
  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(bootrec->data);
  136. switch (priv->fw_interface) {
  137. case FW_FMAC:
  138. printk(KERN_INFO "p54: FreeMAC firmware\n");
  139. break;
  140. case FW_LM20:
  141. printk(KERN_INFO "p54: LM20 firmware\n");
  142. break;
  143. case FW_LM86:
  144. printk(KERN_INFO "p54: LM86 firmware\n");
  145. break;
  146. case FW_LM87:
  147. printk(KERN_INFO "p54: LM87 firmware\n");
  148. break;
  149. default:
  150. printk(KERN_INFO "p54: unknown firmware\n");
  151. break;
  152. }
  153. break;
  154. case BR_CODE_COMPONENT_VERSION:
  155. /* 24 bytes should be enough for all firmwares */
  156. if (strnlen((unsigned char*)bootrec->data, 24) < 24)
  157. fw_version = (unsigned char*)bootrec->data;
  158. break;
  159. case BR_CODE_DESCR: {
  160. struct bootrec_desc *desc =
  161. (struct bootrec_desc *)bootrec->data;
  162. priv->rx_start = le32_to_cpu(desc->rx_start);
  163. /* FIXME add sanity checking */
  164. priv->rx_end = le32_to_cpu(desc->rx_end) - 0x3500;
  165. priv->headroom = desc->headroom;
  166. priv->tailroom = desc->tailroom;
  167. if (bootrec->len == 11)
  168. priv->rx_mtu = (size_t) le16_to_cpu(
  169. (__le16)bootrec->data[10]);
  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. const char* p54_rf_chips[] = { "NULL", "Indigo?", "Duette",
  279. "Frisbee", "Xbow", "Longbow" };
  280. static int p54_init_xbow_synth(struct ieee80211_hw *dev);
  281. 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. EXPORT_SYMBOL_GPL(p54_parse_eeprom);
  429. static int p54_rssi_to_dbm(struct ieee80211_hw *dev, int rssi)
  430. {
  431. /* TODO: get the rssi_add & rssi_mul data from the eeprom */
  432. return ((rssi * 0x83) / 64 - 400) / 4;
  433. }
  434. static int p54_rx_data(struct ieee80211_hw *dev, struct sk_buff *skb)
  435. {
  436. struct p54_common *priv = dev->priv;
  437. struct p54_rx_hdr *hdr = (struct p54_rx_hdr *) skb->data;
  438. struct ieee80211_rx_status rx_status = {0};
  439. u16 freq = le16_to_cpu(hdr->freq);
  440. size_t header_len = sizeof(*hdr);
  441. u32 tsf32;
  442. if (!(hdr->magic & cpu_to_le16(0x0001))) {
  443. if (priv->filter_flags & FIF_FCSFAIL)
  444. rx_status.flag |= RX_FLAG_FAILED_FCS_CRC;
  445. else
  446. return 0;
  447. }
  448. rx_status.signal = p54_rssi_to_dbm(dev, hdr->rssi);
  449. rx_status.noise = priv->noise;
  450. /* XX correct? */
  451. rx_status.qual = (100 * hdr->rssi) / 127;
  452. rx_status.rate_idx = hdr->rate & 0xf;
  453. rx_status.freq = freq;
  454. rx_status.band = IEEE80211_BAND_2GHZ;
  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, 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, hdr->len + sizeof(*hdr));
  683. priv->tx(dev, hdr, hdr->len + sizeof(*hdr), 0);
  684. if (!wait_for_completion_interruptible_timeout(&priv->eeprom_comp, HZ)) {
  685. printk(KERN_ERR "%s: device does not respond!\n",
  686. wiphy_name(dev->wiphy));
  687. ret = -EBUSY;
  688. goto free;
  689. }
  690. memcpy(eeprom + offset, priv->eeprom, blocksize);
  691. offset += blocksize;
  692. eeprom_size -= blocksize;
  693. }
  694. ret = p54_parse_eeprom(dev, eeprom, offset);
  695. free:
  696. kfree(priv->eeprom);
  697. priv->eeprom = NULL;
  698. kfree(hdr);
  699. kfree(eeprom);
  700. return ret;
  701. }
  702. EXPORT_SYMBOL_GPL(p54_read_eeprom);
  703. static int p54_tx(struct ieee80211_hw *dev, struct sk_buff *skb)
  704. {
  705. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  706. struct ieee80211_tx_queue_stats *current_queue;
  707. struct p54_common *priv = dev->priv;
  708. struct p54_control_hdr *hdr;
  709. struct ieee80211_hdr *ieee80211hdr = (struct ieee80211_hdr *)skb->data;
  710. struct p54_tx_control_allocdata *txhdr;
  711. size_t padding, len;
  712. u8 rate;
  713. u8 cts_rate = 0x20;
  714. current_queue = &priv->tx_stats[skb_get_queue_mapping(skb) + 4];
  715. if (unlikely(current_queue->len > current_queue->limit))
  716. return NETDEV_TX_BUSY;
  717. current_queue->len++;
  718. current_queue->count++;
  719. if (current_queue->len == current_queue->limit)
  720. ieee80211_stop_queue(dev, skb_get_queue_mapping(skb));
  721. padding = (unsigned long)(skb->data - (sizeof(*hdr) + sizeof(*txhdr))) & 3;
  722. len = skb->len;
  723. txhdr = (struct p54_tx_control_allocdata *)
  724. skb_push(skb, sizeof(*txhdr) + padding);
  725. hdr = (struct p54_control_hdr *) skb_push(skb, sizeof(*hdr));
  726. if (padding)
  727. hdr->magic1 = cpu_to_le16(0x4010);
  728. else
  729. hdr->magic1 = cpu_to_le16(0x0010);
  730. hdr->len = cpu_to_le16(len);
  731. hdr->type = (info->flags & IEEE80211_TX_CTL_NO_ACK) ? 0 : cpu_to_le16(1);
  732. hdr->retry1 = hdr->retry2 = info->control.retry_limit;
  733. /* TODO: add support for alternate retry TX rates */
  734. rate = ieee80211_get_tx_rate(dev, info)->hw_value;
  735. if (info->flags & IEEE80211_TX_CTL_SHORT_PREAMBLE) {
  736. rate |= 0x10;
  737. cts_rate |= 0x10;
  738. }
  739. if (info->flags & IEEE80211_TX_CTL_USE_RTS_CTS) {
  740. rate |= 0x40;
  741. cts_rate |= ieee80211_get_rts_cts_rate(dev, info)->hw_value;
  742. } else if (info->flags & IEEE80211_TX_CTL_USE_CTS_PROTECT) {
  743. rate |= 0x20;
  744. cts_rate |= ieee80211_get_rts_cts_rate(dev, info)->hw_value;
  745. }
  746. memset(txhdr->rateset, rate, 8);
  747. txhdr->key_type = 0;
  748. txhdr->key_len = 0;
  749. txhdr->hw_queue = skb_get_queue_mapping(skb) + 4;
  750. txhdr->tx_antenna = (info->antenna_sel_tx == 0) ?
  751. 2 : info->antenna_sel_tx - 1;
  752. txhdr->output_power = priv->output_power;
  753. txhdr->cts_rate = (info->flags & IEEE80211_TX_CTL_NO_ACK) ?
  754. 0 : cts_rate;
  755. if (padding)
  756. txhdr->align[0] = padding;
  757. /* FIXME: The sequence that follows is needed for this driver to
  758. * work with mac80211 since "mac80211: fix TX sequence numbers".
  759. * As with the temporary code in rt2x00, changes will be needed
  760. * to get proper sequence numbers on beacons. In addition, this
  761. * patch places the sequence number in the hardware state, which
  762. * limits us to a single virtual state.
  763. */
  764. if (info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ) {
  765. if (info->flags & IEEE80211_TX_CTL_FIRST_FRAGMENT)
  766. priv->seqno += 0x10;
  767. ieee80211hdr->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
  768. ieee80211hdr->seq_ctrl |= cpu_to_le16(priv->seqno);
  769. }
  770. /* modifies skb->cb and with it info, so must be last! */
  771. p54_assign_address(dev, skb, hdr, skb->len);
  772. priv->tx(dev, hdr, skb->len, 0);
  773. return 0;
  774. }
  775. static int p54_set_filter(struct ieee80211_hw *dev, u16 filter_type,
  776. const u8 *bssid)
  777. {
  778. struct p54_common *priv = dev->priv;
  779. struct p54_control_hdr *hdr;
  780. struct p54_tx_control_filter *filter;
  781. size_t data_len;
  782. hdr = kzalloc(sizeof(*hdr) + sizeof(*filter) +
  783. priv->tx_hdr_len, GFP_ATOMIC);
  784. if (!hdr)
  785. return -ENOMEM;
  786. hdr = (void *)hdr + priv->tx_hdr_len;
  787. filter = (struct p54_tx_control_filter *) hdr->data;
  788. hdr->magic1 = cpu_to_le16(0x8001);
  789. hdr->type = cpu_to_le16(P54_CONTROL_TYPE_FILTER_SET);
  790. priv->filter_type = filter->filter_type = cpu_to_le16(filter_type);
  791. memcpy(filter->mac_addr, priv->mac_addr, ETH_ALEN);
  792. if (!bssid)
  793. memset(filter->bssid, ~0, ETH_ALEN);
  794. else
  795. memcpy(filter->bssid, bssid, ETH_ALEN);
  796. filter->rx_antenna = priv->rx_antenna;
  797. if (priv->fw_var < 0x500) {
  798. data_len = P54_TX_CONTROL_FILTER_V1_LEN;
  799. filter->v1.basic_rate_mask = cpu_to_le32(0x15F);
  800. filter->v1.rx_addr = cpu_to_le32(priv->rx_end);
  801. filter->v1.max_rx = cpu_to_le16(priv->rx_mtu);
  802. filter->v1.rxhw = cpu_to_le16(priv->rxhw);
  803. filter->v1.wakeup_timer = cpu_to_le16(500);
  804. } else {
  805. data_len = P54_TX_CONTROL_FILTER_V2_LEN;
  806. filter->v2.rx_addr = cpu_to_le32(priv->rx_end);
  807. filter->v2.max_rx = cpu_to_le16(priv->rx_mtu);
  808. filter->v2.rxhw = cpu_to_le16(priv->rxhw);
  809. filter->v2.timer = cpu_to_le16(1000);
  810. }
  811. hdr->len = cpu_to_le16(data_len);
  812. p54_assign_address(dev, NULL, hdr, sizeof(*hdr) + data_len);
  813. priv->tx(dev, hdr, sizeof(*hdr) + data_len, 1);
  814. return 0;
  815. }
  816. static int p54_set_freq(struct ieee80211_hw *dev, __le16 freq)
  817. {
  818. struct p54_common *priv = dev->priv;
  819. struct p54_control_hdr *hdr;
  820. struct p54_tx_control_channel *chan;
  821. unsigned int i;
  822. size_t data_len;
  823. void *entry;
  824. hdr = kzalloc(sizeof(*hdr) + sizeof(*chan) +
  825. priv->tx_hdr_len, GFP_KERNEL);
  826. if (!hdr)
  827. return -ENOMEM;
  828. hdr = (void *)hdr + priv->tx_hdr_len;
  829. chan = (struct p54_tx_control_channel *) hdr->data;
  830. hdr->magic1 = cpu_to_le16(0x8001);
  831. hdr->type = cpu_to_le16(P54_CONTROL_TYPE_CHANNEL_CHANGE);
  832. chan->flags = cpu_to_le16(0x1);
  833. chan->dwell = cpu_to_le16(0x0);
  834. for (i = 0; i < priv->iq_autocal_len; i++) {
  835. if (priv->iq_autocal[i].freq != freq)
  836. continue;
  837. memcpy(&chan->iq_autocal, &priv->iq_autocal[i],
  838. sizeof(*priv->iq_autocal));
  839. break;
  840. }
  841. if (i == priv->iq_autocal_len)
  842. goto err;
  843. for (i = 0; i < priv->output_limit_len; i++) {
  844. if (priv->output_limit[i].freq != freq)
  845. continue;
  846. chan->val_barker = 0x38;
  847. chan->val_bpsk = chan->dup_bpsk =
  848. priv->output_limit[i].val_bpsk;
  849. chan->val_qpsk = chan->dup_qpsk =
  850. priv->output_limit[i].val_qpsk;
  851. chan->val_16qam = chan->dup_16qam =
  852. priv->output_limit[i].val_16qam;
  853. chan->val_64qam = chan->dup_64qam =
  854. priv->output_limit[i].val_64qam;
  855. break;
  856. }
  857. if (i == priv->output_limit_len)
  858. goto err;
  859. entry = priv->curve_data->data;
  860. for (i = 0; i < priv->curve_data->channels; i++) {
  861. if (*((__le16 *)entry) != freq) {
  862. entry += sizeof(__le16);
  863. entry += sizeof(struct p54_pa_curve_data_sample) *
  864. priv->curve_data->points_per_channel;
  865. continue;
  866. }
  867. entry += sizeof(__le16);
  868. chan->pa_points_per_curve =
  869. min(priv->curve_data->points_per_channel, (u8) 8);
  870. memcpy(chan->curve_data, entry, sizeof(*chan->curve_data) *
  871. chan->pa_points_per_curve);
  872. break;
  873. }
  874. if (priv->fw_var < 0x500) {
  875. data_len = P54_TX_CONTROL_CHANNEL_V1_LEN;
  876. chan->v1.rssical_mul = cpu_to_le16(130);
  877. chan->v1.rssical_add = cpu_to_le16(0xfe70);
  878. } else {
  879. data_len = P54_TX_CONTROL_CHANNEL_V2_LEN;
  880. chan->v2.rssical_mul = cpu_to_le16(130);
  881. chan->v2.rssical_add = cpu_to_le16(0xfe70);
  882. chan->v2.basic_rate_mask = cpu_to_le32(0x15f);
  883. }
  884. hdr->len = cpu_to_le16(data_len);
  885. p54_assign_address(dev, NULL, hdr, sizeof(*hdr) + data_len);
  886. priv->tx(dev, hdr, sizeof(*hdr) + data_len, 1);
  887. return 0;
  888. err:
  889. printk(KERN_ERR "%s: frequency change failed\n", wiphy_name(dev->wiphy));
  890. kfree(hdr);
  891. return -EINVAL;
  892. }
  893. static int p54_set_leds(struct ieee80211_hw *dev, int mode, int link, int act)
  894. {
  895. struct p54_common *priv = dev->priv;
  896. struct p54_control_hdr *hdr;
  897. struct p54_tx_control_led *led;
  898. hdr = kzalloc(sizeof(*hdr) + sizeof(*led) +
  899. priv->tx_hdr_len, GFP_KERNEL);
  900. if (!hdr)
  901. return -ENOMEM;
  902. hdr = (void *)hdr + priv->tx_hdr_len;
  903. hdr->magic1 = cpu_to_le16(0x8001);
  904. hdr->len = cpu_to_le16(sizeof(*led));
  905. hdr->type = cpu_to_le16(P54_CONTROL_TYPE_LED);
  906. p54_assign_address(dev, NULL, hdr, sizeof(*hdr) + sizeof(*led));
  907. led = (struct p54_tx_control_led *) hdr->data;
  908. led->mode = cpu_to_le16(mode);
  909. led->led_permanent = cpu_to_le16(link);
  910. led->led_temporary = cpu_to_le16(act);
  911. led->duration = cpu_to_le16(1000);
  912. priv->tx(dev, hdr, sizeof(*hdr) + sizeof(*led), 1);
  913. return 0;
  914. }
  915. #define P54_SET_QUEUE(queue, ai_fs, cw_min, cw_max, _txop) \
  916. do { \
  917. queue.aifs = cpu_to_le16(ai_fs); \
  918. queue.cwmin = cpu_to_le16(cw_min); \
  919. queue.cwmax = cpu_to_le16(cw_max); \
  920. queue.txop = cpu_to_le16(_txop); \
  921. } while(0)
  922. static void p54_init_vdcf(struct ieee80211_hw *dev)
  923. {
  924. struct p54_common *priv = dev->priv;
  925. struct p54_control_hdr *hdr;
  926. struct p54_tx_control_vdcf *vdcf;
  927. /* all USB V1 adapters need a extra headroom */
  928. hdr = (void *)priv->cached_vdcf + priv->tx_hdr_len;
  929. hdr->magic1 = cpu_to_le16(0x8001);
  930. hdr->len = cpu_to_le16(sizeof(*vdcf));
  931. hdr->type = cpu_to_le16(P54_CONTROL_TYPE_DCFINIT);
  932. hdr->req_id = cpu_to_le32(priv->rx_start);
  933. vdcf = (struct p54_tx_control_vdcf *) hdr->data;
  934. P54_SET_QUEUE(vdcf->queue[0], 0x0002, 0x0003, 0x0007, 47);
  935. P54_SET_QUEUE(vdcf->queue[1], 0x0002, 0x0007, 0x000f, 94);
  936. P54_SET_QUEUE(vdcf->queue[2], 0x0003, 0x000f, 0x03ff, 0);
  937. P54_SET_QUEUE(vdcf->queue[3], 0x0007, 0x000f, 0x03ff, 0);
  938. }
  939. static void p54_set_vdcf(struct ieee80211_hw *dev)
  940. {
  941. struct p54_common *priv = dev->priv;
  942. struct p54_control_hdr *hdr;
  943. struct p54_tx_control_vdcf *vdcf;
  944. hdr = (void *)priv->cached_vdcf + priv->tx_hdr_len;
  945. p54_assign_address(dev, NULL, hdr, sizeof(*hdr) + sizeof(*vdcf));
  946. vdcf = (struct p54_tx_control_vdcf *) hdr->data;
  947. if (dev->conf.flags & IEEE80211_CONF_SHORT_SLOT_TIME) {
  948. vdcf->slottime = 9;
  949. vdcf->magic1 = 0x10;
  950. vdcf->magic2 = 0x00;
  951. } else {
  952. vdcf->slottime = 20;
  953. vdcf->magic1 = 0x0a;
  954. vdcf->magic2 = 0x06;
  955. }
  956. /* (see prism54/isl_oid.h for further details) */
  957. vdcf->frameburst = cpu_to_le16(0);
  958. priv->tx(dev, hdr, sizeof(*hdr) + sizeof(*vdcf), 0);
  959. }
  960. static int p54_start(struct ieee80211_hw *dev)
  961. {
  962. struct p54_common *priv = dev->priv;
  963. int err;
  964. if (!priv->cached_vdcf) {
  965. priv->cached_vdcf = kzalloc(sizeof(struct p54_tx_control_vdcf)+
  966. priv->tx_hdr_len + sizeof(struct p54_control_hdr),
  967. GFP_KERNEL);
  968. if (!priv->cached_vdcf)
  969. return -ENOMEM;
  970. }
  971. if (!priv->cached_stats) {
  972. priv->cached_stats = kzalloc(sizeof(struct p54_statistics) +
  973. priv->tx_hdr_len + sizeof(struct p54_control_hdr),
  974. GFP_KERNEL);
  975. if (!priv->cached_stats) {
  976. kfree(priv->cached_vdcf);
  977. priv->cached_vdcf = NULL;
  978. return -ENOMEM;
  979. }
  980. }
  981. err = priv->open(dev);
  982. if (!err)
  983. priv->mode = NL80211_IFTYPE_MONITOR;
  984. p54_init_vdcf(dev);
  985. mod_timer(&priv->stats_timer, jiffies + HZ);
  986. return err;
  987. }
  988. static void p54_stop(struct ieee80211_hw *dev)
  989. {
  990. struct p54_common *priv = dev->priv;
  991. struct sk_buff *skb;
  992. del_timer(&priv->stats_timer);
  993. while ((skb = skb_dequeue(&priv->tx_queue)))
  994. kfree_skb(skb);
  995. priv->stop(dev);
  996. priv->tsf_high32 = priv->tsf_low32 = 0;
  997. priv->mode = NL80211_IFTYPE_UNSPECIFIED;
  998. }
  999. static int p54_add_interface(struct ieee80211_hw *dev,
  1000. struct ieee80211_if_init_conf *conf)
  1001. {
  1002. struct p54_common *priv = dev->priv;
  1003. if (priv->mode != NL80211_IFTYPE_MONITOR)
  1004. return -EOPNOTSUPP;
  1005. switch (conf->type) {
  1006. case NL80211_IFTYPE_STATION:
  1007. priv->mode = conf->type;
  1008. break;
  1009. default:
  1010. return -EOPNOTSUPP;
  1011. }
  1012. memcpy(priv->mac_addr, conf->mac_addr, ETH_ALEN);
  1013. p54_set_filter(dev, 0, NULL);
  1014. switch (conf->type) {
  1015. case NL80211_IFTYPE_STATION:
  1016. p54_set_filter(dev, 1, NULL);
  1017. break;
  1018. default:
  1019. BUG(); /* impossible */
  1020. break;
  1021. }
  1022. p54_set_leds(dev, 1, 0, 0);
  1023. return 0;
  1024. }
  1025. static void p54_remove_interface(struct ieee80211_hw *dev,
  1026. struct ieee80211_if_init_conf *conf)
  1027. {
  1028. struct p54_common *priv = dev->priv;
  1029. priv->mode = NL80211_IFTYPE_MONITOR;
  1030. memset(priv->mac_addr, 0, ETH_ALEN);
  1031. p54_set_filter(dev, 0, NULL);
  1032. }
  1033. static int p54_config(struct ieee80211_hw *dev, struct ieee80211_conf *conf)
  1034. {
  1035. int ret;
  1036. struct p54_common *priv = dev->priv;
  1037. mutex_lock(&priv->conf_mutex);
  1038. priv->rx_antenna = (conf->antenna_sel_rx == 0) ?
  1039. 2 : conf->antenna_sel_tx - 1;
  1040. priv->output_power = conf->power_level << 2;
  1041. ret = p54_set_freq(dev, cpu_to_le16(conf->channel->center_freq));
  1042. p54_set_vdcf(dev);
  1043. mutex_unlock(&priv->conf_mutex);
  1044. return ret;
  1045. }
  1046. static int p54_config_interface(struct ieee80211_hw *dev,
  1047. struct ieee80211_vif *vif,
  1048. struct ieee80211_if_conf *conf)
  1049. {
  1050. struct p54_common *priv = dev->priv;
  1051. mutex_lock(&priv->conf_mutex);
  1052. p54_set_filter(dev, 0, conf->bssid);
  1053. p54_set_leds(dev, 1, !is_multicast_ether_addr(conf->bssid), 0);
  1054. memcpy(priv->bssid, conf->bssid, ETH_ALEN);
  1055. mutex_unlock(&priv->conf_mutex);
  1056. return 0;
  1057. }
  1058. static void p54_configure_filter(struct ieee80211_hw *dev,
  1059. unsigned int changed_flags,
  1060. unsigned int *total_flags,
  1061. int mc_count, struct dev_mc_list *mclist)
  1062. {
  1063. struct p54_common *priv = dev->priv;
  1064. *total_flags &= FIF_BCN_PRBRESP_PROMISC |
  1065. FIF_PROMISC_IN_BSS |
  1066. FIF_FCSFAIL;
  1067. priv->filter_flags = *total_flags;
  1068. if (changed_flags & FIF_BCN_PRBRESP_PROMISC) {
  1069. if (*total_flags & FIF_BCN_PRBRESP_PROMISC)
  1070. p54_set_filter(dev, priv->filter_type, NULL);
  1071. else
  1072. p54_set_filter(dev, priv->filter_type, priv->bssid);
  1073. }
  1074. if (changed_flags & FIF_PROMISC_IN_BSS) {
  1075. if (*total_flags & FIF_PROMISC_IN_BSS)
  1076. p54_set_filter(dev, priv->filter_type |
  1077. cpu_to_le16(0x8), NULL);
  1078. else
  1079. p54_set_filter(dev, priv->filter_type &
  1080. ~cpu_to_le16(0x8), priv->bssid);
  1081. }
  1082. }
  1083. static int p54_conf_tx(struct ieee80211_hw *dev, u16 queue,
  1084. const struct ieee80211_tx_queue_params *params)
  1085. {
  1086. struct p54_common *priv = dev->priv;
  1087. struct p54_tx_control_vdcf *vdcf;
  1088. vdcf = (struct p54_tx_control_vdcf *)(((struct p54_control_hdr *)
  1089. ((void *)priv->cached_vdcf + priv->tx_hdr_len))->data);
  1090. if ((params) && !(queue > 4)) {
  1091. P54_SET_QUEUE(vdcf->queue[queue], params->aifs,
  1092. params->cw_min, params->cw_max, params->txop);
  1093. } else
  1094. return -EINVAL;
  1095. p54_set_vdcf(dev);
  1096. return 0;
  1097. }
  1098. static int p54_init_xbow_synth(struct ieee80211_hw *dev)
  1099. {
  1100. struct p54_common *priv = dev->priv;
  1101. struct p54_control_hdr *hdr;
  1102. struct p54_tx_control_xbow_synth *xbow;
  1103. hdr = kzalloc(sizeof(*hdr) + sizeof(*xbow) +
  1104. priv->tx_hdr_len, GFP_KERNEL);
  1105. if (!hdr)
  1106. return -ENOMEM;
  1107. hdr = (void *)hdr + priv->tx_hdr_len;
  1108. hdr->magic1 = cpu_to_le16(0x8001);
  1109. hdr->len = cpu_to_le16(sizeof(*xbow));
  1110. hdr->type = cpu_to_le16(P54_CONTROL_TYPE_XBOW_SYNTH_CFG);
  1111. p54_assign_address(dev, NULL, hdr, sizeof(*hdr) + sizeof(*xbow));
  1112. xbow = (struct p54_tx_control_xbow_synth *) hdr->data;
  1113. xbow->magic1 = cpu_to_le16(0x1);
  1114. xbow->magic2 = cpu_to_le16(0x2);
  1115. xbow->freq = cpu_to_le16(5390);
  1116. priv->tx(dev, hdr, sizeof(*hdr) + sizeof(*xbow), 1);
  1117. return 0;
  1118. }
  1119. static void p54_statistics_timer(unsigned long data)
  1120. {
  1121. struct ieee80211_hw *dev = (struct ieee80211_hw *) data;
  1122. struct p54_common *priv = dev->priv;
  1123. struct p54_control_hdr *hdr;
  1124. struct p54_statistics *stats;
  1125. BUG_ON(!priv->cached_stats);
  1126. hdr = (void *)priv->cached_stats + priv->tx_hdr_len;
  1127. hdr->magic1 = cpu_to_le16(0x8000);
  1128. hdr->len = cpu_to_le16(sizeof(*stats));
  1129. hdr->type = cpu_to_le16(P54_CONTROL_TYPE_STAT_READBACK);
  1130. p54_assign_address(dev, NULL, hdr, sizeof(*hdr) + sizeof(*stats));
  1131. priv->tx(dev, hdr, sizeof(*hdr) + sizeof(*stats), 0);
  1132. }
  1133. static int p54_get_stats(struct ieee80211_hw *dev,
  1134. struct ieee80211_low_level_stats *stats)
  1135. {
  1136. struct p54_common *priv = dev->priv;
  1137. del_timer(&priv->stats_timer);
  1138. p54_statistics_timer((unsigned long)dev);
  1139. if (!wait_for_completion_interruptible_timeout(&priv->stats_comp, HZ)) {
  1140. printk(KERN_ERR "%s: device does not respond!\n",
  1141. wiphy_name(dev->wiphy));
  1142. return -EBUSY;
  1143. }
  1144. memcpy(stats, &priv->stats, sizeof(*stats));
  1145. return 0;
  1146. }
  1147. static int p54_get_tx_stats(struct ieee80211_hw *dev,
  1148. struct ieee80211_tx_queue_stats *stats)
  1149. {
  1150. struct p54_common *priv = dev->priv;
  1151. memcpy(stats, &priv->tx_stats[4], sizeof(stats[0]) * dev->queues);
  1152. return 0;
  1153. }
  1154. static const struct ieee80211_ops p54_ops = {
  1155. .tx = p54_tx,
  1156. .start = p54_start,
  1157. .stop = p54_stop,
  1158. .add_interface = p54_add_interface,
  1159. .remove_interface = p54_remove_interface,
  1160. .config = p54_config,
  1161. .config_interface = p54_config_interface,
  1162. .configure_filter = p54_configure_filter,
  1163. .conf_tx = p54_conf_tx,
  1164. .get_stats = p54_get_stats,
  1165. .get_tx_stats = p54_get_tx_stats
  1166. };
  1167. struct ieee80211_hw *p54_init_common(size_t priv_data_len)
  1168. {
  1169. struct ieee80211_hw *dev;
  1170. struct p54_common *priv;
  1171. dev = ieee80211_alloc_hw(priv_data_len, &p54_ops);
  1172. if (!dev)
  1173. return NULL;
  1174. priv = dev->priv;
  1175. priv->mode = NL80211_IFTYPE_UNSPECIFIED;
  1176. skb_queue_head_init(&priv->tx_queue);
  1177. dev->flags = IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING | /* not sure */
  1178. IEEE80211_HW_RX_INCLUDES_FCS |
  1179. IEEE80211_HW_SIGNAL_DBM |
  1180. IEEE80211_HW_NOISE_DBM;
  1181. dev->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION);
  1182. dev->channel_change_time = 1000; /* TODO: find actual value */
  1183. priv->tx_stats[0].limit = 1;
  1184. priv->tx_stats[1].limit = 1;
  1185. priv->tx_stats[2].limit = 1;
  1186. priv->tx_stats[3].limit = 1;
  1187. priv->tx_stats[4].limit = 5;
  1188. dev->queues = 1;
  1189. priv->noise = -94;
  1190. dev->extra_tx_headroom = sizeof(struct p54_control_hdr) + 4 +
  1191. sizeof(struct p54_tx_control_allocdata);
  1192. mutex_init(&priv->conf_mutex);
  1193. init_completion(&priv->eeprom_comp);
  1194. init_completion(&priv->stats_comp);
  1195. setup_timer(&priv->stats_timer, p54_statistics_timer,
  1196. (unsigned long)dev);
  1197. return dev;
  1198. }
  1199. EXPORT_SYMBOL_GPL(p54_init_common);
  1200. void p54_free_common(struct ieee80211_hw *dev)
  1201. {
  1202. struct p54_common *priv = dev->priv;
  1203. kfree(priv->cached_stats);
  1204. kfree(priv->iq_autocal);
  1205. kfree(priv->output_limit);
  1206. kfree(priv->curve_data);
  1207. kfree(priv->cached_vdcf);
  1208. }
  1209. EXPORT_SYMBOL_GPL(p54_free_common);
  1210. static int __init p54_init(void)
  1211. {
  1212. return 0;
  1213. }
  1214. static void __exit p54_exit(void)
  1215. {
  1216. }
  1217. module_init(p54_init);
  1218. module_exit(p54_exit);