p54common.c 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116
  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. * Copyright 2008, Johannes Berg <johannes@sipsolutions.net>
  7. *
  8. * Based on:
  9. * - the islsm (softmac prism54) driver, which is:
  10. * Copyright 2004-2006 Jean-Baptiste Note <jbnote@gmail.com>, et al.
  11. * - stlc45xx driver
  12. * Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies).
  13. *
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License version 2 as
  16. * published by the Free Software Foundation.
  17. */
  18. #include <linux/init.h>
  19. #include <linux/firmware.h>
  20. #include <linux/etherdevice.h>
  21. #include <net/mac80211.h>
  22. #include "p54.h"
  23. #include "p54common.h"
  24. static int modparam_nohwcrypt;
  25. module_param_named(nohwcrypt, modparam_nohwcrypt, bool, S_IRUGO);
  26. MODULE_PARM_DESC(nohwcrypt, "Disable hardware encryption.");
  27. MODULE_AUTHOR("Michael Wu <flamingice@sourmilk.net>");
  28. MODULE_DESCRIPTION("Softmac Prism54 common code");
  29. MODULE_LICENSE("GPL");
  30. MODULE_ALIAS("prism54common");
  31. static struct ieee80211_rate p54_bgrates[] = {
  32. { .bitrate = 10, .hw_value = 0, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  33. { .bitrate = 20, .hw_value = 1, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  34. { .bitrate = 55, .hw_value = 2, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  35. { .bitrate = 110, .hw_value = 3, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  36. { .bitrate = 60, .hw_value = 4, },
  37. { .bitrate = 90, .hw_value = 5, },
  38. { .bitrate = 120, .hw_value = 6, },
  39. { .bitrate = 180, .hw_value = 7, },
  40. { .bitrate = 240, .hw_value = 8, },
  41. { .bitrate = 360, .hw_value = 9, },
  42. { .bitrate = 480, .hw_value = 10, },
  43. { .bitrate = 540, .hw_value = 11, },
  44. };
  45. static struct ieee80211_channel p54_bgchannels[] = {
  46. { .center_freq = 2412, .hw_value = 1, },
  47. { .center_freq = 2417, .hw_value = 2, },
  48. { .center_freq = 2422, .hw_value = 3, },
  49. { .center_freq = 2427, .hw_value = 4, },
  50. { .center_freq = 2432, .hw_value = 5, },
  51. { .center_freq = 2437, .hw_value = 6, },
  52. { .center_freq = 2442, .hw_value = 7, },
  53. { .center_freq = 2447, .hw_value = 8, },
  54. { .center_freq = 2452, .hw_value = 9, },
  55. { .center_freq = 2457, .hw_value = 10, },
  56. { .center_freq = 2462, .hw_value = 11, },
  57. { .center_freq = 2467, .hw_value = 12, },
  58. { .center_freq = 2472, .hw_value = 13, },
  59. { .center_freq = 2484, .hw_value = 14, },
  60. };
  61. static struct ieee80211_supported_band band_2GHz = {
  62. .channels = p54_bgchannels,
  63. .n_channels = ARRAY_SIZE(p54_bgchannels),
  64. .bitrates = p54_bgrates,
  65. .n_bitrates = ARRAY_SIZE(p54_bgrates),
  66. };
  67. static struct ieee80211_rate p54_arates[] = {
  68. { .bitrate = 60, .hw_value = 4, },
  69. { .bitrate = 90, .hw_value = 5, },
  70. { .bitrate = 120, .hw_value = 6, },
  71. { .bitrate = 180, .hw_value = 7, },
  72. { .bitrate = 240, .hw_value = 8, },
  73. { .bitrate = 360, .hw_value = 9, },
  74. { .bitrate = 480, .hw_value = 10, },
  75. { .bitrate = 540, .hw_value = 11, },
  76. };
  77. static struct ieee80211_channel p54_achannels[] = {
  78. { .center_freq = 4920 },
  79. { .center_freq = 4940 },
  80. { .center_freq = 4960 },
  81. { .center_freq = 4980 },
  82. { .center_freq = 5040 },
  83. { .center_freq = 5060 },
  84. { .center_freq = 5080 },
  85. { .center_freq = 5170 },
  86. { .center_freq = 5180 },
  87. { .center_freq = 5190 },
  88. { .center_freq = 5200 },
  89. { .center_freq = 5210 },
  90. { .center_freq = 5220 },
  91. { .center_freq = 5230 },
  92. { .center_freq = 5240 },
  93. { .center_freq = 5260 },
  94. { .center_freq = 5280 },
  95. { .center_freq = 5300 },
  96. { .center_freq = 5320 },
  97. { .center_freq = 5500 },
  98. { .center_freq = 5520 },
  99. { .center_freq = 5540 },
  100. { .center_freq = 5560 },
  101. { .center_freq = 5580 },
  102. { .center_freq = 5600 },
  103. { .center_freq = 5620 },
  104. { .center_freq = 5640 },
  105. { .center_freq = 5660 },
  106. { .center_freq = 5680 },
  107. { .center_freq = 5700 },
  108. { .center_freq = 5745 },
  109. { .center_freq = 5765 },
  110. { .center_freq = 5785 },
  111. { .center_freq = 5805 },
  112. { .center_freq = 5825 },
  113. };
  114. static struct ieee80211_supported_band band_5GHz = {
  115. .channels = p54_achannels,
  116. .n_channels = ARRAY_SIZE(p54_achannels),
  117. .bitrates = p54_arates,
  118. .n_bitrates = ARRAY_SIZE(p54_arates),
  119. };
  120. int p54_parse_firmware(struct ieee80211_hw *dev, const struct firmware *fw)
  121. {
  122. struct p54_common *priv = dev->priv;
  123. struct bootrec_exp_if *exp_if;
  124. struct bootrec *bootrec;
  125. u32 *data = (u32 *)fw->data;
  126. u32 *end_data = (u32 *)fw->data + (fw->size >> 2);
  127. u8 *fw_version = NULL;
  128. size_t len;
  129. int i;
  130. if (priv->rx_start)
  131. return 0;
  132. while (data < end_data && *data)
  133. data++;
  134. while (data < end_data && !*data)
  135. data++;
  136. bootrec = (struct bootrec *) data;
  137. while (bootrec->data <= end_data &&
  138. (bootrec->data + (len = le32_to_cpu(bootrec->len))) <= end_data) {
  139. u32 code = le32_to_cpu(bootrec->code);
  140. switch (code) {
  141. case BR_CODE_COMPONENT_ID:
  142. priv->fw_interface = be32_to_cpup((__be32 *)
  143. bootrec->data);
  144. switch (priv->fw_interface) {
  145. case FW_LM86:
  146. case FW_LM20:
  147. case FW_LM87: {
  148. char *iftype = (char *)bootrec->data;
  149. printk(KERN_INFO "%s: p54 detected a LM%c%c "
  150. "firmware\n",
  151. wiphy_name(dev->wiphy),
  152. iftype[2], iftype[3]);
  153. break;
  154. }
  155. case FW_FMAC:
  156. default:
  157. printk(KERN_ERR "%s: unsupported firmware\n",
  158. wiphy_name(dev->wiphy));
  159. return -ENODEV;
  160. }
  161. break;
  162. case BR_CODE_COMPONENT_VERSION:
  163. /* 24 bytes should be enough for all firmwares */
  164. if (strnlen((unsigned char*)bootrec->data, 24) < 24)
  165. fw_version = (unsigned char*)bootrec->data;
  166. break;
  167. case BR_CODE_DESCR: {
  168. struct bootrec_desc *desc =
  169. (struct bootrec_desc *)bootrec->data;
  170. priv->rx_start = le32_to_cpu(desc->rx_start);
  171. /* FIXME add sanity checking */
  172. priv->rx_end = le32_to_cpu(desc->rx_end) - 0x3500;
  173. priv->headroom = desc->headroom;
  174. priv->tailroom = desc->tailroom;
  175. priv->privacy_caps = desc->privacy_caps;
  176. priv->rx_keycache_size = desc->rx_keycache_size;
  177. if (le32_to_cpu(bootrec->len) == 11)
  178. priv->rx_mtu = le16_to_cpu(desc->rx_mtu);
  179. else
  180. priv->rx_mtu = (size_t)
  181. 0x620 - priv->tx_hdr_len;
  182. break;
  183. }
  184. case BR_CODE_EXPOSED_IF:
  185. exp_if = (struct bootrec_exp_if *) bootrec->data;
  186. for (i = 0; i < (len * sizeof(*exp_if) / 4); i++)
  187. if (exp_if[i].if_id == cpu_to_le16(0x1a))
  188. priv->fw_var = le16_to_cpu(exp_if[i].variant);
  189. break;
  190. case BR_CODE_DEPENDENT_IF:
  191. break;
  192. case BR_CODE_END_OF_BRA:
  193. case LEGACY_BR_CODE_END_OF_BRA:
  194. end_data = NULL;
  195. break;
  196. default:
  197. break;
  198. }
  199. bootrec = (struct bootrec *)&bootrec->data[len];
  200. }
  201. if (fw_version)
  202. printk(KERN_INFO "%s: FW rev %s - Softmac protocol %x.%x\n",
  203. wiphy_name(dev->wiphy), fw_version,
  204. priv->fw_var >> 8, priv->fw_var & 0xff);
  205. if (priv->fw_var < 0x500)
  206. printk(KERN_INFO "%s: you are using an obsolete firmware. "
  207. "visit http://wireless.kernel.org/en/users/Drivers/p54 "
  208. "and grab one for \"kernel >= 2.6.28\"!\n",
  209. wiphy_name(dev->wiphy));
  210. if (priv->fw_var >= 0x300) {
  211. /* Firmware supports QoS, use it! */
  212. priv->tx_stats[4].limit = 3; /* AC_VO */
  213. priv->tx_stats[5].limit = 4; /* AC_VI */
  214. priv->tx_stats[6].limit = 3; /* AC_BE */
  215. priv->tx_stats[7].limit = 2; /* AC_BK */
  216. dev->queues = 4;
  217. }
  218. if (!modparam_nohwcrypt)
  219. printk(KERN_INFO "%s: cryptographic accelerator "
  220. "WEP:%s, TKIP:%s, CCMP:%s\n",
  221. wiphy_name(dev->wiphy),
  222. (priv->privacy_caps & BR_DESC_PRIV_CAP_WEP) ? "YES" :
  223. "no", (priv->privacy_caps & (BR_DESC_PRIV_CAP_TKIP |
  224. BR_DESC_PRIV_CAP_MICHAEL)) ? "YES" : "no",
  225. (priv->privacy_caps & BR_DESC_PRIV_CAP_AESCCMP) ?
  226. "YES" : "no");
  227. return 0;
  228. }
  229. EXPORT_SYMBOL_GPL(p54_parse_firmware);
  230. static int p54_convert_rev0(struct ieee80211_hw *dev,
  231. struct pda_pa_curve_data *curve_data)
  232. {
  233. struct p54_common *priv = dev->priv;
  234. struct p54_pa_curve_data_sample *dst;
  235. struct pda_pa_curve_data_sample_rev0 *src;
  236. size_t cd_len = sizeof(*curve_data) +
  237. (curve_data->points_per_channel*sizeof(*dst) + 2) *
  238. curve_data->channels;
  239. unsigned int i, j;
  240. void *source, *target;
  241. priv->curve_data = kmalloc(cd_len, GFP_KERNEL);
  242. if (!priv->curve_data)
  243. return -ENOMEM;
  244. memcpy(priv->curve_data, curve_data, sizeof(*curve_data));
  245. source = curve_data->data;
  246. target = priv->curve_data->data;
  247. for (i = 0; i < curve_data->channels; i++) {
  248. __le16 *freq = source;
  249. source += sizeof(__le16);
  250. *((__le16 *)target) = *freq;
  251. target += sizeof(__le16);
  252. for (j = 0; j < curve_data->points_per_channel; j++) {
  253. dst = target;
  254. src = source;
  255. dst->rf_power = src->rf_power;
  256. dst->pa_detector = src->pa_detector;
  257. dst->data_64qam = src->pcv;
  258. /* "invent" the points for the other modulations */
  259. #define SUB(x,y) (u8)((x) - (y)) > (x) ? 0 : (x) - (y)
  260. dst->data_16qam = SUB(src->pcv, 12);
  261. dst->data_qpsk = SUB(dst->data_16qam, 12);
  262. dst->data_bpsk = SUB(dst->data_qpsk, 12);
  263. dst->data_barker = SUB(dst->data_bpsk, 14);
  264. #undef SUB
  265. target += sizeof(*dst);
  266. source += sizeof(*src);
  267. }
  268. }
  269. return 0;
  270. }
  271. static int p54_convert_rev1(struct ieee80211_hw *dev,
  272. struct pda_pa_curve_data *curve_data)
  273. {
  274. struct p54_common *priv = dev->priv;
  275. struct p54_pa_curve_data_sample *dst;
  276. struct pda_pa_curve_data_sample_rev1 *src;
  277. size_t cd_len = sizeof(*curve_data) +
  278. (curve_data->points_per_channel*sizeof(*dst) + 2) *
  279. curve_data->channels;
  280. unsigned int i, j;
  281. void *source, *target;
  282. priv->curve_data = kmalloc(cd_len, GFP_KERNEL);
  283. if (!priv->curve_data)
  284. return -ENOMEM;
  285. memcpy(priv->curve_data, curve_data, sizeof(*curve_data));
  286. source = curve_data->data;
  287. target = priv->curve_data->data;
  288. for (i = 0; i < curve_data->channels; i++) {
  289. __le16 *freq = source;
  290. source += sizeof(__le16);
  291. *((__le16 *)target) = *freq;
  292. target += sizeof(__le16);
  293. for (j = 0; j < curve_data->points_per_channel; j++) {
  294. memcpy(target, source, sizeof(*src));
  295. target += sizeof(*dst);
  296. source += sizeof(*src);
  297. }
  298. source++;
  299. }
  300. return 0;
  301. }
  302. static const char *p54_rf_chips[] = { "NULL", "Duette3", "Duette2",
  303. "Frisbee", "Xbow", "Longbow", "NULL", "NULL" };
  304. static int p54_init_xbow_synth(struct ieee80211_hw *dev);
  305. static int p54_parse_eeprom(struct ieee80211_hw *dev, void *eeprom, int len)
  306. {
  307. struct p54_common *priv = dev->priv;
  308. struct eeprom_pda_wrap *wrap = NULL;
  309. struct pda_entry *entry;
  310. unsigned int data_len, entry_len;
  311. void *tmp;
  312. int err;
  313. u8 *end = (u8 *)eeprom + len;
  314. u16 synth = 0;
  315. wrap = (struct eeprom_pda_wrap *) eeprom;
  316. entry = (void *)wrap->data + le16_to_cpu(wrap->len);
  317. /* verify that at least the entry length/code fits */
  318. while ((u8 *)entry <= end - sizeof(*entry)) {
  319. entry_len = le16_to_cpu(entry->len);
  320. data_len = ((entry_len - 1) << 1);
  321. /* abort if entry exceeds whole structure */
  322. if ((u8 *)entry + sizeof(*entry) + data_len > end)
  323. break;
  324. switch (le16_to_cpu(entry->code)) {
  325. case PDR_MAC_ADDRESS:
  326. SET_IEEE80211_PERM_ADDR(dev, entry->data);
  327. break;
  328. case PDR_PRISM_PA_CAL_OUTPUT_POWER_LIMITS:
  329. if (data_len < 2) {
  330. err = -EINVAL;
  331. goto err;
  332. }
  333. if (2 + entry->data[1]*sizeof(*priv->output_limit) > data_len) {
  334. err = -EINVAL;
  335. goto err;
  336. }
  337. priv->output_limit = kmalloc(entry->data[1] *
  338. sizeof(*priv->output_limit), GFP_KERNEL);
  339. if (!priv->output_limit) {
  340. err = -ENOMEM;
  341. goto err;
  342. }
  343. memcpy(priv->output_limit, &entry->data[2],
  344. entry->data[1]*sizeof(*priv->output_limit));
  345. priv->output_limit_len = entry->data[1];
  346. break;
  347. case PDR_PRISM_PA_CAL_CURVE_DATA: {
  348. struct pda_pa_curve_data *curve_data =
  349. (struct pda_pa_curve_data *)entry->data;
  350. if (data_len < sizeof(*curve_data)) {
  351. err = -EINVAL;
  352. goto err;
  353. }
  354. switch (curve_data->cal_method_rev) {
  355. case 0:
  356. err = p54_convert_rev0(dev, curve_data);
  357. break;
  358. case 1:
  359. err = p54_convert_rev1(dev, curve_data);
  360. break;
  361. default:
  362. printk(KERN_ERR "%s: unknown curve data "
  363. "revision %d\n",
  364. wiphy_name(dev->wiphy),
  365. curve_data->cal_method_rev);
  366. err = -ENODEV;
  367. break;
  368. }
  369. if (err)
  370. goto err;
  371. }
  372. case PDR_PRISM_ZIF_TX_IQ_CALIBRATION:
  373. priv->iq_autocal = kmalloc(data_len, GFP_KERNEL);
  374. if (!priv->iq_autocal) {
  375. err = -ENOMEM;
  376. goto err;
  377. }
  378. memcpy(priv->iq_autocal, entry->data, data_len);
  379. priv->iq_autocal_len = data_len / sizeof(struct pda_iq_autocal_entry);
  380. break;
  381. case PDR_INTERFACE_LIST:
  382. tmp = entry->data;
  383. while ((u8 *)tmp < entry->data + data_len) {
  384. struct bootrec_exp_if *exp_if = tmp;
  385. if (le16_to_cpu(exp_if->if_id) == 0xf)
  386. synth = le16_to_cpu(exp_if->variant);
  387. tmp += sizeof(struct bootrec_exp_if);
  388. }
  389. break;
  390. case PDR_HARDWARE_PLATFORM_COMPONENT_ID:
  391. priv->version = *(u8 *)(entry->data + 1);
  392. break;
  393. case PDR_END:
  394. /* make it overrun */
  395. entry_len = len;
  396. break;
  397. case PDR_MANUFACTURING_PART_NUMBER:
  398. case PDR_PDA_VERSION:
  399. case PDR_NIC_SERIAL_NUMBER:
  400. case PDR_REGULATORY_DOMAIN_LIST:
  401. case PDR_TEMPERATURE_TYPE:
  402. case PDR_PRISM_PCI_IDENTIFIER:
  403. case PDR_COUNTRY_INFORMATION:
  404. case PDR_OEM_NAME:
  405. case PDR_PRODUCT_NAME:
  406. case PDR_UTF8_OEM_NAME:
  407. case PDR_UTF8_PRODUCT_NAME:
  408. case PDR_COUNTRY_LIST:
  409. case PDR_DEFAULT_COUNTRY:
  410. case PDR_ANTENNA_GAIN:
  411. case PDR_PRISM_INDIGO_PA_CALIBRATION_DATA:
  412. case PDR_RSSI_LINEAR_APPROXIMATION:
  413. case PDR_RSSI_LINEAR_APPROXIMATION_DUAL_BAND:
  414. case PDR_REGULATORY_POWER_LIMITS:
  415. case PDR_RSSI_LINEAR_APPROXIMATION_EXTENDED:
  416. case PDR_RADIATED_TRANSMISSION_CORRECTION:
  417. case PDR_PRISM_TX_IQ_CALIBRATION:
  418. case PDR_BASEBAND_REGISTERS:
  419. case PDR_PER_CHANNEL_BASEBAND_REGISTERS:
  420. break;
  421. default:
  422. printk(KERN_INFO "%s: unknown eeprom code : 0x%x\n",
  423. wiphy_name(dev->wiphy),
  424. le16_to_cpu(entry->code));
  425. break;
  426. }
  427. entry = (void *)entry + (entry_len + 1)*2;
  428. }
  429. if (!synth || !priv->iq_autocal || !priv->output_limit ||
  430. !priv->curve_data) {
  431. printk(KERN_ERR "%s: not all required entries found in eeprom!\n",
  432. wiphy_name(dev->wiphy));
  433. err = -EINVAL;
  434. goto err;
  435. }
  436. priv->rxhw = synth & PDR_SYNTH_FRONTEND_MASK;
  437. if (priv->rxhw == 4)
  438. p54_init_xbow_synth(dev);
  439. if (!(synth & PDR_SYNTH_24_GHZ_DISABLED))
  440. dev->wiphy->bands[IEEE80211_BAND_2GHZ] = &band_2GHz;
  441. if (!(synth & PDR_SYNTH_5_GHZ_DISABLED))
  442. dev->wiphy->bands[IEEE80211_BAND_5GHZ] = &band_5GHz;
  443. if (!is_valid_ether_addr(dev->wiphy->perm_addr)) {
  444. u8 perm_addr[ETH_ALEN];
  445. printk(KERN_WARNING "%s: Invalid hwaddr! Using randomly generated MAC addr\n",
  446. wiphy_name(dev->wiphy));
  447. random_ether_addr(perm_addr);
  448. SET_IEEE80211_PERM_ADDR(dev, perm_addr);
  449. }
  450. printk(KERN_INFO "%s: hwaddr %pM, MAC:isl38%02x RF:%s\n",
  451. wiphy_name(dev->wiphy),
  452. dev->wiphy->perm_addr,
  453. priv->version, p54_rf_chips[priv->rxhw]);
  454. return 0;
  455. err:
  456. if (priv->iq_autocal) {
  457. kfree(priv->iq_autocal);
  458. priv->iq_autocal = NULL;
  459. }
  460. if (priv->output_limit) {
  461. kfree(priv->output_limit);
  462. priv->output_limit = NULL;
  463. }
  464. if (priv->curve_data) {
  465. kfree(priv->curve_data);
  466. priv->curve_data = NULL;
  467. }
  468. printk(KERN_ERR "%s: eeprom parse failed!\n",
  469. wiphy_name(dev->wiphy));
  470. return err;
  471. }
  472. static int p54_rssi_to_dbm(struct ieee80211_hw *dev, int rssi)
  473. {
  474. /* TODO: get the rssi_add & rssi_mul data from the eeprom */
  475. return ((rssi * 0x83) / 64 - 400) / 4;
  476. }
  477. static int p54_rx_data(struct ieee80211_hw *dev, struct sk_buff *skb)
  478. {
  479. struct p54_common *priv = dev->priv;
  480. struct p54_rx_data *hdr = (struct p54_rx_data *) skb->data;
  481. struct ieee80211_rx_status rx_status = {0};
  482. u16 freq = le16_to_cpu(hdr->freq);
  483. size_t header_len = sizeof(*hdr);
  484. u32 tsf32;
  485. if (!(hdr->flags & cpu_to_le16(P54_HDR_FLAG_DATA_IN_FCS_GOOD))) {
  486. if (priv->filter_flags & FIF_FCSFAIL)
  487. rx_status.flag |= RX_FLAG_FAILED_FCS_CRC;
  488. else
  489. return 0;
  490. }
  491. if (hdr->decrypt_status == P54_DECRYPT_OK)
  492. rx_status.flag |= RX_FLAG_DECRYPTED;
  493. if ((hdr->decrypt_status == P54_DECRYPT_FAIL_MICHAEL) ||
  494. (hdr->decrypt_status == P54_DECRYPT_FAIL_TKIP))
  495. rx_status.flag |= RX_FLAG_MMIC_ERROR;
  496. rx_status.signal = p54_rssi_to_dbm(dev, hdr->rssi);
  497. rx_status.noise = priv->noise;
  498. /* XX correct? */
  499. rx_status.qual = (100 * hdr->rssi) / 127;
  500. if (hdr->rate & 0x10)
  501. rx_status.flag |= RX_FLAG_SHORTPRE;
  502. rx_status.rate_idx = (dev->conf.channel->band == IEEE80211_BAND_2GHZ ?
  503. hdr->rate : (hdr->rate - 4)) & 0xf;
  504. rx_status.freq = freq;
  505. rx_status.band = dev->conf.channel->band;
  506. rx_status.antenna = hdr->antenna;
  507. tsf32 = le32_to_cpu(hdr->tsf32);
  508. if (tsf32 < priv->tsf_low32)
  509. priv->tsf_high32++;
  510. rx_status.mactime = ((u64)priv->tsf_high32) << 32 | tsf32;
  511. priv->tsf_low32 = tsf32;
  512. rx_status.flag |= RX_FLAG_TSFT;
  513. if (hdr->flags & cpu_to_le16(P54_HDR_FLAG_DATA_ALIGN))
  514. header_len += hdr->align[0];
  515. skb_pull(skb, header_len);
  516. skb_trim(skb, le16_to_cpu(hdr->len));
  517. ieee80211_rx_irqsafe(dev, skb, &rx_status);
  518. return -1;
  519. }
  520. static void inline p54_wake_free_queues(struct ieee80211_hw *dev)
  521. {
  522. struct p54_common *priv = dev->priv;
  523. int i;
  524. if (priv->mode == NL80211_IFTYPE_UNSPECIFIED)
  525. return ;
  526. for (i = 0; i < dev->queues; i++)
  527. if (priv->tx_stats[i + 4].len < priv->tx_stats[i + 4].limit)
  528. ieee80211_wake_queue(dev, i);
  529. }
  530. void p54_free_skb(struct ieee80211_hw *dev, struct sk_buff *skb)
  531. {
  532. struct p54_common *priv = dev->priv;
  533. struct ieee80211_tx_info *info;
  534. struct memrecord *range;
  535. unsigned long flags;
  536. u32 freed = 0, last_addr = priv->rx_start;
  537. if (unlikely(!skb || !dev || !skb_queue_len(&priv->tx_queue)))
  538. return;
  539. spin_lock_irqsave(&priv->tx_queue.lock, flags);
  540. info = IEEE80211_SKB_CB(skb);
  541. range = (void *)info->rate_driver_data;
  542. if (skb->prev != (struct sk_buff *)&priv->tx_queue) {
  543. struct ieee80211_tx_info *ni;
  544. struct memrecord *mr;
  545. ni = IEEE80211_SKB_CB(skb->prev);
  546. mr = (struct memrecord *)ni->rate_driver_data;
  547. last_addr = mr->end_addr;
  548. }
  549. if (skb->next != (struct sk_buff *)&priv->tx_queue) {
  550. struct ieee80211_tx_info *ni;
  551. struct memrecord *mr;
  552. ni = IEEE80211_SKB_CB(skb->next);
  553. mr = (struct memrecord *)ni->rate_driver_data;
  554. freed = mr->start_addr - last_addr;
  555. } else
  556. freed = priv->rx_end - last_addr;
  557. __skb_unlink(skb, &priv->tx_queue);
  558. spin_unlock_irqrestore(&priv->tx_queue.lock, flags);
  559. kfree_skb(skb);
  560. if (freed >= priv->headroom + sizeof(struct p54_hdr) + 48 +
  561. IEEE80211_MAX_RTS_THRESHOLD + priv->tailroom)
  562. p54_wake_free_queues(dev);
  563. }
  564. EXPORT_SYMBOL_GPL(p54_free_skb);
  565. static void p54_rx_frame_sent(struct ieee80211_hw *dev, struct sk_buff *skb)
  566. {
  567. struct p54_common *priv = dev->priv;
  568. struct p54_hdr *hdr = (struct p54_hdr *) skb->data;
  569. struct p54_frame_sent *payload = (struct p54_frame_sent *) hdr->data;
  570. struct sk_buff *entry = (struct sk_buff *) priv->tx_queue.next;
  571. u32 addr = le32_to_cpu(hdr->req_id) - priv->headroom;
  572. struct memrecord *range = NULL;
  573. u32 freed = 0;
  574. u32 last_addr = priv->rx_start;
  575. unsigned long flags;
  576. int count, idx;
  577. spin_lock_irqsave(&priv->tx_queue.lock, flags);
  578. while (entry != (struct sk_buff *)&priv->tx_queue) {
  579. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(entry);
  580. struct p54_hdr *entry_hdr;
  581. struct p54_tx_data *entry_data;
  582. int pad = 0;
  583. range = (void *)info->rate_driver_data;
  584. if (range->start_addr != addr) {
  585. last_addr = range->end_addr;
  586. entry = entry->next;
  587. continue;
  588. }
  589. if (entry->next != (struct sk_buff *)&priv->tx_queue) {
  590. struct ieee80211_tx_info *ni;
  591. struct memrecord *mr;
  592. ni = IEEE80211_SKB_CB(entry->next);
  593. mr = (struct memrecord *)ni->rate_driver_data;
  594. freed = mr->start_addr - last_addr;
  595. } else
  596. freed = priv->rx_end - last_addr;
  597. last_addr = range->end_addr;
  598. __skb_unlink(entry, &priv->tx_queue);
  599. spin_unlock_irqrestore(&priv->tx_queue.lock, flags);
  600. entry_hdr = (struct p54_hdr *) entry->data;
  601. entry_data = (struct p54_tx_data *) entry_hdr->data;
  602. priv->tx_stats[entry_data->hw_queue].len--;
  603. if (unlikely(entry == priv->cached_beacon)) {
  604. kfree_skb(entry);
  605. priv->cached_beacon = NULL;
  606. goto out;
  607. }
  608. /*
  609. * Clear manually, ieee80211_tx_info_clear_status would
  610. * clear the counts too and we need them.
  611. */
  612. memset(&info->status.ampdu_ack_len, 0,
  613. sizeof(struct ieee80211_tx_info) -
  614. offsetof(struct ieee80211_tx_info, status.ampdu_ack_len));
  615. BUILD_BUG_ON(offsetof(struct ieee80211_tx_info,
  616. status.ampdu_ack_len) != 23);
  617. if (entry_hdr->flags & cpu_to_le16(P54_HDR_FLAG_DATA_ALIGN))
  618. pad = entry_data->align[0];
  619. /* walk through the rates array and adjust the counts */
  620. count = payload->tries;
  621. for (idx = 0; idx < 4; idx++) {
  622. if (count >= info->status.rates[idx].count) {
  623. count -= info->status.rates[idx].count;
  624. } else if (count > 0) {
  625. info->status.rates[idx].count = count;
  626. count = 0;
  627. } else {
  628. info->status.rates[idx].idx = -1;
  629. info->status.rates[idx].count = 0;
  630. }
  631. }
  632. if (!(info->flags & IEEE80211_TX_CTL_NO_ACK) &&
  633. (!payload->status))
  634. info->flags |= IEEE80211_TX_STAT_ACK;
  635. if (payload->status & P54_TX_PSM_CANCELLED)
  636. info->flags |= IEEE80211_TX_STAT_TX_FILTERED;
  637. info->status.ack_signal = p54_rssi_to_dbm(dev,
  638. (int)payload->ack_rssi);
  639. skb_pull(entry, sizeof(*hdr) + pad + sizeof(*entry_data));
  640. ieee80211_tx_status_irqsafe(dev, entry);
  641. goto out;
  642. }
  643. spin_unlock_irqrestore(&priv->tx_queue.lock, flags);
  644. out:
  645. if (freed >= priv->headroom + sizeof(struct p54_hdr) + 48 +
  646. IEEE80211_MAX_RTS_THRESHOLD + priv->tailroom)
  647. p54_wake_free_queues(dev);
  648. }
  649. static void p54_rx_eeprom_readback(struct ieee80211_hw *dev,
  650. struct sk_buff *skb)
  651. {
  652. struct p54_hdr *hdr = (struct p54_hdr *) skb->data;
  653. struct p54_eeprom_lm86 *eeprom = (struct p54_eeprom_lm86 *) hdr->data;
  654. struct p54_common *priv = dev->priv;
  655. if (!priv->eeprom)
  656. return ;
  657. if (priv->fw_var >= 0x509) {
  658. memcpy(priv->eeprom, eeprom->v2.data,
  659. le16_to_cpu(eeprom->v2.len));
  660. } else {
  661. memcpy(priv->eeprom, eeprom->v1.data,
  662. le16_to_cpu(eeprom->v1.len));
  663. }
  664. complete(&priv->eeprom_comp);
  665. }
  666. static void p54_rx_stats(struct ieee80211_hw *dev, struct sk_buff *skb)
  667. {
  668. struct p54_common *priv = dev->priv;
  669. struct p54_hdr *hdr = (struct p54_hdr *) skb->data;
  670. struct p54_statistics *stats = (struct p54_statistics *) hdr->data;
  671. u32 tsf32 = le32_to_cpu(stats->tsf32);
  672. if (tsf32 < priv->tsf_low32)
  673. priv->tsf_high32++;
  674. priv->tsf_low32 = tsf32;
  675. priv->stats.dot11RTSFailureCount = le32_to_cpu(stats->rts_fail);
  676. priv->stats.dot11RTSSuccessCount = le32_to_cpu(stats->rts_success);
  677. priv->stats.dot11FCSErrorCount = le32_to_cpu(stats->rx_bad_fcs);
  678. priv->noise = p54_rssi_to_dbm(dev, le32_to_cpu(stats->noise));
  679. complete(&priv->stats_comp);
  680. mod_timer(&priv->stats_timer, jiffies + 5 * HZ);
  681. }
  682. static void p54_rx_trap(struct ieee80211_hw *dev, struct sk_buff *skb)
  683. {
  684. struct p54_hdr *hdr = (struct p54_hdr *) skb->data;
  685. struct p54_trap *trap = (struct p54_trap *) hdr->data;
  686. u16 event = le16_to_cpu(trap->event);
  687. u16 freq = le16_to_cpu(trap->frequency);
  688. switch (event) {
  689. case P54_TRAP_BEACON_TX:
  690. break;
  691. case P54_TRAP_RADAR:
  692. printk(KERN_INFO "%s: radar (freq:%d MHz)\n",
  693. wiphy_name(dev->wiphy), freq);
  694. break;
  695. case P54_TRAP_NO_BEACON:
  696. break;
  697. case P54_TRAP_SCAN:
  698. break;
  699. case P54_TRAP_TBTT:
  700. break;
  701. case P54_TRAP_TIMER:
  702. break;
  703. default:
  704. printk(KERN_INFO "%s: received event:%x freq:%d\n",
  705. wiphy_name(dev->wiphy), event, freq);
  706. break;
  707. }
  708. }
  709. static int p54_rx_control(struct ieee80211_hw *dev, struct sk_buff *skb)
  710. {
  711. struct p54_hdr *hdr = (struct p54_hdr *) skb->data;
  712. switch (le16_to_cpu(hdr->type)) {
  713. case P54_CONTROL_TYPE_TXDONE:
  714. p54_rx_frame_sent(dev, skb);
  715. break;
  716. case P54_CONTROL_TYPE_TRAP:
  717. p54_rx_trap(dev, skb);
  718. break;
  719. case P54_CONTROL_TYPE_BBP:
  720. break;
  721. case P54_CONTROL_TYPE_STAT_READBACK:
  722. p54_rx_stats(dev, skb);
  723. break;
  724. case P54_CONTROL_TYPE_EEPROM_READBACK:
  725. p54_rx_eeprom_readback(dev, skb);
  726. break;
  727. default:
  728. printk(KERN_DEBUG "%s: not handling 0x%02x type control frame\n",
  729. wiphy_name(dev->wiphy), le16_to_cpu(hdr->type));
  730. break;
  731. }
  732. return 0;
  733. }
  734. /* returns zero if skb can be reused */
  735. int p54_rx(struct ieee80211_hw *dev, struct sk_buff *skb)
  736. {
  737. u16 type = le16_to_cpu(*((__le16 *)skb->data));
  738. if (type & P54_HDR_FLAG_CONTROL)
  739. return p54_rx_control(dev, skb);
  740. else
  741. return p54_rx_data(dev, skb);
  742. }
  743. EXPORT_SYMBOL_GPL(p54_rx);
  744. /*
  745. * So, the firmware is somewhat stupid and doesn't know what places in its
  746. * memory incoming data should go to. By poking around in the firmware, we
  747. * can find some unused memory to upload our packets to. However, data that we
  748. * want the card to TX needs to stay intact until the card has told us that
  749. * it is done with it. This function finds empty places we can upload to and
  750. * marks allocated areas as reserved if necessary. p54_rx_frame_sent frees
  751. * allocated areas.
  752. */
  753. static int p54_assign_address(struct ieee80211_hw *dev, struct sk_buff *skb,
  754. struct p54_hdr *data, u32 len)
  755. {
  756. struct p54_common *priv = dev->priv;
  757. struct sk_buff *entry = priv->tx_queue.next;
  758. struct sk_buff *target_skb = NULL;
  759. struct ieee80211_tx_info *info;
  760. struct memrecord *range;
  761. u32 last_addr = priv->rx_start;
  762. u32 largest_hole = 0;
  763. u32 target_addr = priv->rx_start;
  764. unsigned long flags;
  765. unsigned int left;
  766. len = (len + priv->headroom + priv->tailroom + 3) & ~0x3;
  767. if (!skb)
  768. return -EINVAL;
  769. spin_lock_irqsave(&priv->tx_queue.lock, flags);
  770. left = skb_queue_len(&priv->tx_queue);
  771. while (left--) {
  772. u32 hole_size;
  773. info = IEEE80211_SKB_CB(entry);
  774. range = (void *)info->rate_driver_data;
  775. hole_size = range->start_addr - last_addr;
  776. if (!target_skb && hole_size >= len) {
  777. target_skb = entry->prev;
  778. hole_size -= len;
  779. target_addr = last_addr;
  780. }
  781. largest_hole = max(largest_hole, hole_size);
  782. last_addr = range->end_addr;
  783. entry = entry->next;
  784. }
  785. if (!target_skb && priv->rx_end - last_addr >= len) {
  786. target_skb = priv->tx_queue.prev;
  787. largest_hole = max(largest_hole, priv->rx_end - last_addr - len);
  788. if (!skb_queue_empty(&priv->tx_queue)) {
  789. info = IEEE80211_SKB_CB(target_skb);
  790. range = (void *)info->rate_driver_data;
  791. target_addr = range->end_addr;
  792. }
  793. } else
  794. largest_hole = max(largest_hole, priv->rx_end - last_addr);
  795. if (!target_skb) {
  796. spin_unlock_irqrestore(&priv->tx_queue.lock, flags);
  797. ieee80211_stop_queues(dev);
  798. return -ENOMEM;
  799. }
  800. info = IEEE80211_SKB_CB(skb);
  801. range = (void *)info->rate_driver_data;
  802. range->start_addr = target_addr;
  803. range->end_addr = target_addr + len;
  804. __skb_queue_after(&priv->tx_queue, target_skb, skb);
  805. spin_unlock_irqrestore(&priv->tx_queue.lock, flags);
  806. if (largest_hole < priv->headroom + sizeof(struct p54_hdr) +
  807. 48 + IEEE80211_MAX_RTS_THRESHOLD + priv->tailroom)
  808. ieee80211_stop_queues(dev);
  809. data->req_id = cpu_to_le32(target_addr + priv->headroom);
  810. return 0;
  811. }
  812. static struct sk_buff *p54_alloc_skb(struct ieee80211_hw *dev,
  813. u16 hdr_flags, u16 len, u16 type, gfp_t memflags)
  814. {
  815. struct p54_common *priv = dev->priv;
  816. struct p54_hdr *hdr;
  817. struct sk_buff *skb;
  818. skb = __dev_alloc_skb(len + priv->tx_hdr_len, memflags);
  819. if (!skb)
  820. return NULL;
  821. skb_reserve(skb, priv->tx_hdr_len);
  822. hdr = (struct p54_hdr *) skb_put(skb, sizeof(*hdr));
  823. hdr->flags = cpu_to_le16(hdr_flags);
  824. hdr->len = cpu_to_le16(len - sizeof(*hdr));
  825. hdr->type = cpu_to_le16(type);
  826. hdr->tries = hdr->rts_tries = 0;
  827. if (unlikely(p54_assign_address(dev, skb, hdr, len))) {
  828. kfree_skb(skb);
  829. return NULL;
  830. }
  831. return skb;
  832. }
  833. int p54_read_eeprom(struct ieee80211_hw *dev)
  834. {
  835. struct p54_common *priv = dev->priv;
  836. struct p54_hdr *hdr = NULL;
  837. struct p54_eeprom_lm86 *eeprom_hdr;
  838. struct sk_buff *skb;
  839. size_t eeprom_size = 0x2020, offset = 0, blocksize, maxblocksize;
  840. int ret = -ENOMEM;
  841. void *eeprom = NULL;
  842. maxblocksize = EEPROM_READBACK_LEN;
  843. if (priv->fw_var >= 0x509)
  844. maxblocksize -= 0xc;
  845. else
  846. maxblocksize -= 0x4;
  847. skb = p54_alloc_skb(dev, P54_HDR_FLAG_CONTROL, sizeof(*hdr) +
  848. sizeof(*eeprom_hdr) + maxblocksize,
  849. P54_CONTROL_TYPE_EEPROM_READBACK, GFP_KERNEL);
  850. if (!skb)
  851. goto free;
  852. priv->eeprom = kzalloc(EEPROM_READBACK_LEN, GFP_KERNEL);
  853. if (!priv->eeprom)
  854. goto free;
  855. eeprom = kzalloc(eeprom_size, GFP_KERNEL);
  856. if (!eeprom)
  857. goto free;
  858. eeprom_hdr = (struct p54_eeprom_lm86 *) skb_put(skb,
  859. sizeof(*eeprom_hdr) + maxblocksize);
  860. while (eeprom_size) {
  861. blocksize = min(eeprom_size, maxblocksize);
  862. if (priv->fw_var < 0x509) {
  863. eeprom_hdr->v1.offset = cpu_to_le16(offset);
  864. eeprom_hdr->v1.len = cpu_to_le16(blocksize);
  865. } else {
  866. eeprom_hdr->v2.offset = cpu_to_le32(offset);
  867. eeprom_hdr->v2.len = cpu_to_le16(blocksize);
  868. eeprom_hdr->v2.magic2 = 0xf;
  869. memcpy(eeprom_hdr->v2.magic, (const char *)"LOCK", 4);
  870. }
  871. priv->tx(dev, skb, 0);
  872. if (!wait_for_completion_interruptible_timeout(&priv->eeprom_comp, HZ)) {
  873. printk(KERN_ERR "%s: device does not respond!\n",
  874. wiphy_name(dev->wiphy));
  875. ret = -EBUSY;
  876. goto free;
  877. }
  878. memcpy(eeprom + offset, priv->eeprom, blocksize);
  879. offset += blocksize;
  880. eeprom_size -= blocksize;
  881. }
  882. ret = p54_parse_eeprom(dev, eeprom, offset);
  883. free:
  884. kfree(priv->eeprom);
  885. priv->eeprom = NULL;
  886. p54_free_skb(dev, skb);
  887. kfree(eeprom);
  888. return ret;
  889. }
  890. EXPORT_SYMBOL_GPL(p54_read_eeprom);
  891. static int p54_set_tim(struct ieee80211_hw *dev, struct ieee80211_sta *sta,
  892. bool set)
  893. {
  894. struct p54_common *priv = dev->priv;
  895. struct sk_buff *skb;
  896. struct p54_tim *tim;
  897. skb = p54_alloc_skb(dev, P54_HDR_FLAG_CONTROL_OPSET,
  898. sizeof(struct p54_hdr) + sizeof(*tim),
  899. P54_CONTROL_TYPE_TIM, GFP_KERNEL);
  900. if (!skb)
  901. return -ENOMEM;
  902. tim = (struct p54_tim *) skb_put(skb, sizeof(*tim));
  903. tim->count = 1;
  904. tim->entry[0] = cpu_to_le16(set ? (sta->aid | 0x8000) : sta->aid);
  905. priv->tx(dev, skb, 1);
  906. return 0;
  907. }
  908. static int p54_sta_unlock(struct ieee80211_hw *dev, u8 *addr)
  909. {
  910. struct p54_common *priv = dev->priv;
  911. struct sk_buff *skb;
  912. struct p54_sta_unlock *sta;
  913. skb = p54_alloc_skb(dev, P54_HDR_FLAG_CONTROL_OPSET,
  914. sizeof(struct p54_hdr) + sizeof(*sta),
  915. P54_CONTROL_TYPE_PSM_STA_UNLOCK, GFP_ATOMIC);
  916. if (!skb)
  917. return -ENOMEM;
  918. sta = (struct p54_sta_unlock *)skb_put(skb, sizeof(*sta));
  919. memcpy(sta->addr, addr, ETH_ALEN);
  920. priv->tx(dev, skb, 1);
  921. return 0;
  922. }
  923. static void p54_sta_notify_ps(struct ieee80211_hw *dev,
  924. enum sta_notify_ps_cmd notify_cmd,
  925. struct ieee80211_sta *sta)
  926. {
  927. switch (notify_cmd) {
  928. case STA_NOTIFY_AWAKE:
  929. p54_sta_unlock(dev, sta->addr);
  930. break;
  931. default:
  932. break;
  933. }
  934. }
  935. static void p54_sta_notify(struct ieee80211_hw *dev, struct ieee80211_vif *vif,
  936. enum sta_notify_cmd notify_cmd,
  937. struct ieee80211_sta *sta)
  938. {
  939. switch (notify_cmd) {
  940. case STA_NOTIFY_ADD:
  941. case STA_NOTIFY_REMOVE:
  942. /*
  943. * Notify the firmware that we don't want or we don't
  944. * need to buffer frames for this station anymore.
  945. */
  946. p54_sta_unlock(dev, sta->addr);
  947. break;
  948. default:
  949. break;
  950. }
  951. }
  952. static int p54_tx_cancel(struct ieee80211_hw *dev, struct sk_buff *entry)
  953. {
  954. struct p54_common *priv = dev->priv;
  955. struct sk_buff *skb;
  956. struct p54_hdr *hdr;
  957. struct p54_txcancel *cancel;
  958. skb = p54_alloc_skb(dev, P54_HDR_FLAG_CONTROL_OPSET,
  959. sizeof(struct p54_hdr) + sizeof(*cancel),
  960. P54_CONTROL_TYPE_TXCANCEL, GFP_ATOMIC);
  961. if (!skb)
  962. return -ENOMEM;
  963. hdr = (void *)entry->data;
  964. cancel = (struct p54_txcancel *)skb_put(skb, sizeof(*cancel));
  965. cancel->req_id = hdr->req_id;
  966. priv->tx(dev, skb, 1);
  967. return 0;
  968. }
  969. static int p54_tx_fill(struct ieee80211_hw *dev, struct sk_buff *skb,
  970. struct ieee80211_tx_info *info, u8 *queue, size_t *extra_len,
  971. u16 *flags, u16 *aid)
  972. {
  973. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  974. struct p54_common *priv = dev->priv;
  975. int ret = 0;
  976. if (unlikely(ieee80211_is_mgmt(hdr->frame_control))) {
  977. if (ieee80211_is_beacon(hdr->frame_control)) {
  978. *aid = 0;
  979. *queue = 0;
  980. *extra_len = IEEE80211_MAX_TIM_LEN;
  981. *flags = P54_HDR_FLAG_DATA_OUT_TIMESTAMP;
  982. return 0;
  983. } else if (ieee80211_is_probe_resp(hdr->frame_control)) {
  984. *aid = 0;
  985. *queue = 2;
  986. *flags = P54_HDR_FLAG_DATA_OUT_TIMESTAMP |
  987. P54_HDR_FLAG_DATA_OUT_NOCANCEL;
  988. return 0;
  989. } else {
  990. *queue = 2;
  991. ret = 0;
  992. }
  993. } else {
  994. *queue += 4;
  995. ret = 1;
  996. }
  997. switch (priv->mode) {
  998. case NL80211_IFTYPE_STATION:
  999. *aid = 1;
  1000. break;
  1001. case NL80211_IFTYPE_AP:
  1002. case NL80211_IFTYPE_ADHOC:
  1003. case NL80211_IFTYPE_MESH_POINT:
  1004. if (info->flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM) {
  1005. *aid = 0;
  1006. *queue = 3;
  1007. return 0;
  1008. }
  1009. if (info->control.sta)
  1010. *aid = info->control.sta->aid;
  1011. else
  1012. *flags |= P54_HDR_FLAG_DATA_OUT_NOCANCEL;
  1013. }
  1014. return ret;
  1015. }
  1016. static u8 p54_convert_algo(enum ieee80211_key_alg alg)
  1017. {
  1018. switch (alg) {
  1019. case ALG_WEP:
  1020. return P54_CRYPTO_WEP;
  1021. case ALG_TKIP:
  1022. return P54_CRYPTO_TKIPMICHAEL;
  1023. case ALG_CCMP:
  1024. return P54_CRYPTO_AESCCMP;
  1025. default:
  1026. return 0;
  1027. }
  1028. }
  1029. static int p54_tx(struct ieee80211_hw *dev, struct sk_buff *skb)
  1030. {
  1031. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  1032. struct ieee80211_tx_queue_stats *current_queue = NULL;
  1033. struct p54_common *priv = dev->priv;
  1034. struct p54_hdr *hdr;
  1035. struct p54_tx_data *txhdr;
  1036. size_t padding, len, tim_len = 0;
  1037. int i, j, ridx, ret;
  1038. u16 hdr_flags = 0, aid = 0;
  1039. u8 rate, queue, crypt_offset = 0;
  1040. u8 cts_rate = 0x20;
  1041. u8 rc_flags;
  1042. u8 calculated_tries[4];
  1043. u8 nrates = 0, nremaining = 8;
  1044. queue = skb_get_queue_mapping(skb);
  1045. ret = p54_tx_fill(dev, skb, info, &queue, &tim_len, &hdr_flags, &aid);
  1046. current_queue = &priv->tx_stats[queue];
  1047. if (unlikely((current_queue->len > current_queue->limit) && ret))
  1048. return NETDEV_TX_BUSY;
  1049. current_queue->len++;
  1050. current_queue->count++;
  1051. if ((current_queue->len == current_queue->limit) && ret)
  1052. ieee80211_stop_queue(dev, skb_get_queue_mapping(skb));
  1053. padding = (unsigned long)(skb->data - (sizeof(*hdr) + sizeof(*txhdr))) & 3;
  1054. len = skb->len;
  1055. if (info->control.hw_key) {
  1056. crypt_offset = ieee80211_get_hdrlen_from_skb(skb);
  1057. if (info->control.hw_key->alg == ALG_TKIP) {
  1058. u8 *iv = (u8 *)(skb->data + crypt_offset);
  1059. /*
  1060. * The firmware excepts that the IV has to have
  1061. * this special format
  1062. */
  1063. iv[1] = iv[0];
  1064. iv[0] = iv[2];
  1065. iv[2] = 0;
  1066. }
  1067. }
  1068. txhdr = (struct p54_tx_data *) skb_push(skb, sizeof(*txhdr) + padding);
  1069. hdr = (struct p54_hdr *) skb_push(skb, sizeof(*hdr));
  1070. if (padding)
  1071. hdr_flags |= P54_HDR_FLAG_DATA_ALIGN;
  1072. hdr->type = cpu_to_le16(aid);
  1073. hdr->rts_tries = info->control.rates[0].count;
  1074. /*
  1075. * we register the rates in perfect order, and
  1076. * RTS/CTS won't happen on 5 GHz
  1077. */
  1078. cts_rate = info->control.rts_cts_rate_idx;
  1079. memset(&txhdr->rateset, 0, sizeof(txhdr->rateset));
  1080. /* see how many rates got used */
  1081. for (i = 0; i < 4; i++) {
  1082. if (info->control.rates[i].idx < 0)
  1083. break;
  1084. nrates++;
  1085. }
  1086. /* limit tries to 8/nrates per rate */
  1087. for (i = 0; i < nrates; i++) {
  1088. /*
  1089. * The magic expression here is equivalent to 8/nrates for
  1090. * all values that matter, but avoids division and jumps.
  1091. * Note that nrates can only take the values 1 through 4.
  1092. */
  1093. calculated_tries[i] = min_t(int, ((15 >> nrates) | 1) + 1,
  1094. info->control.rates[i].count);
  1095. nremaining -= calculated_tries[i];
  1096. }
  1097. /* if there are tries left, distribute from back to front */
  1098. for (i = nrates - 1; nremaining > 0 && i >= 0; i--) {
  1099. int tmp = info->control.rates[i].count - calculated_tries[i];
  1100. if (tmp <= 0)
  1101. continue;
  1102. /* RC requested more tries at this rate */
  1103. tmp = min_t(int, tmp, nremaining);
  1104. calculated_tries[i] += tmp;
  1105. nremaining -= tmp;
  1106. }
  1107. ridx = 0;
  1108. for (i = 0; i < nrates && ridx < 8; i++) {
  1109. /* we register the rates in perfect order */
  1110. rate = info->control.rates[i].idx;
  1111. if (info->band == IEEE80211_BAND_5GHZ)
  1112. rate += 4;
  1113. /* store the count we actually calculated for TX status */
  1114. info->control.rates[i].count = calculated_tries[i];
  1115. rc_flags = info->control.rates[i].flags;
  1116. if (rc_flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE) {
  1117. rate |= 0x10;
  1118. cts_rate |= 0x10;
  1119. }
  1120. if (rc_flags & IEEE80211_TX_RC_USE_RTS_CTS)
  1121. rate |= 0x40;
  1122. else if (rc_flags & IEEE80211_TX_RC_USE_CTS_PROTECT)
  1123. rate |= 0x20;
  1124. for (j = 0; j < calculated_tries[i] && ridx < 8; j++) {
  1125. txhdr->rateset[ridx] = rate;
  1126. ridx++;
  1127. }
  1128. }
  1129. if (info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ)
  1130. hdr_flags |= P54_HDR_FLAG_DATA_OUT_SEQNR;
  1131. /* TODO: enable bursting */
  1132. hdr->flags = cpu_to_le16(hdr_flags);
  1133. hdr->tries = ridx;
  1134. txhdr->rts_rate_idx = 0;
  1135. if (info->control.hw_key) {
  1136. crypt_offset += info->control.hw_key->iv_len;
  1137. txhdr->key_type = p54_convert_algo(info->control.hw_key->alg);
  1138. txhdr->key_len = min((u8)16, info->control.hw_key->keylen);
  1139. memcpy(txhdr->key, info->control.hw_key->key, txhdr->key_len);
  1140. if (info->control.hw_key->alg == ALG_TKIP) {
  1141. if (unlikely(skb_tailroom(skb) < 12))
  1142. goto err;
  1143. /* reserve space for the MIC key */
  1144. len += 8;
  1145. memcpy(skb_put(skb, 8), &(info->control.hw_key->key
  1146. [NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY]), 8);
  1147. }
  1148. /* reserve some space for ICV */
  1149. len += info->control.hw_key->icv_len;
  1150. } else {
  1151. txhdr->key_type = 0;
  1152. txhdr->key_len = 0;
  1153. }
  1154. txhdr->crypt_offset = crypt_offset;
  1155. txhdr->hw_queue = queue;
  1156. if (current_queue)
  1157. txhdr->backlog = current_queue->len;
  1158. else
  1159. txhdr->backlog = 0;
  1160. memset(txhdr->durations, 0, sizeof(txhdr->durations));
  1161. txhdr->tx_antenna = (info->antenna_sel_tx == 0) ?
  1162. 2 : info->antenna_sel_tx - 1;
  1163. txhdr->output_power = priv->output_power;
  1164. txhdr->cts_rate = cts_rate;
  1165. if (padding)
  1166. txhdr->align[0] = padding;
  1167. hdr->len = cpu_to_le16(len);
  1168. /* modifies skb->cb and with it info, so must be last! */
  1169. if (unlikely(p54_assign_address(dev, skb, hdr, skb->len + tim_len)))
  1170. goto err;
  1171. priv->tx(dev, skb, 0);
  1172. return 0;
  1173. err:
  1174. skb_pull(skb, sizeof(*hdr) + sizeof(*txhdr) + padding);
  1175. if (current_queue) {
  1176. current_queue->len--;
  1177. current_queue->count--;
  1178. }
  1179. return NETDEV_TX_BUSY;
  1180. }
  1181. static int p54_setup_mac(struct ieee80211_hw *dev)
  1182. {
  1183. struct p54_common *priv = dev->priv;
  1184. struct sk_buff *skb;
  1185. struct p54_setup_mac *setup;
  1186. u16 mode;
  1187. skb = p54_alloc_skb(dev, P54_HDR_FLAG_CONTROL_OPSET, sizeof(*setup) +
  1188. sizeof(struct p54_hdr), P54_CONTROL_TYPE_SETUP,
  1189. GFP_ATOMIC);
  1190. if (!skb)
  1191. return -ENOMEM;
  1192. setup = (struct p54_setup_mac *) skb_put(skb, sizeof(*setup));
  1193. if (dev->conf.radio_enabled) {
  1194. switch (priv->mode) {
  1195. case NL80211_IFTYPE_STATION:
  1196. mode = P54_FILTER_TYPE_STATION;
  1197. break;
  1198. case NL80211_IFTYPE_AP:
  1199. mode = P54_FILTER_TYPE_AP;
  1200. break;
  1201. case NL80211_IFTYPE_ADHOC:
  1202. case NL80211_IFTYPE_MESH_POINT:
  1203. mode = P54_FILTER_TYPE_IBSS;
  1204. break;
  1205. default:
  1206. mode = P54_FILTER_TYPE_NONE;
  1207. break;
  1208. }
  1209. if (priv->filter_flags & FIF_PROMISC_IN_BSS)
  1210. mode |= P54_FILTER_TYPE_TRANSPARENT;
  1211. } else
  1212. mode = P54_FILTER_TYPE_RX_DISABLED;
  1213. setup->mac_mode = cpu_to_le16(mode);
  1214. memcpy(setup->mac_addr, priv->mac_addr, ETH_ALEN);
  1215. memcpy(setup->bssid, priv->bssid, ETH_ALEN);
  1216. setup->rx_antenna = 2; /* automatic */
  1217. setup->rx_align = 0;
  1218. if (priv->fw_var < 0x500) {
  1219. setup->v1.basic_rate_mask = cpu_to_le32(priv->basic_rate_mask);
  1220. memset(setup->v1.rts_rates, 0, 8);
  1221. setup->v1.rx_addr = cpu_to_le32(priv->rx_end);
  1222. setup->v1.max_rx = cpu_to_le16(priv->rx_mtu);
  1223. setup->v1.rxhw = cpu_to_le16(priv->rxhw);
  1224. setup->v1.wakeup_timer = cpu_to_le16(priv->wakeup_timer);
  1225. setup->v1.unalloc0 = cpu_to_le16(0);
  1226. } else {
  1227. setup->v2.rx_addr = cpu_to_le32(priv->rx_end);
  1228. setup->v2.max_rx = cpu_to_le16(priv->rx_mtu);
  1229. setup->v2.rxhw = cpu_to_le16(priv->rxhw);
  1230. setup->v2.timer = cpu_to_le16(priv->wakeup_timer);
  1231. setup->v2.truncate = cpu_to_le16(48896);
  1232. setup->v2.basic_rate_mask = cpu_to_le32(priv->basic_rate_mask);
  1233. setup->v2.sbss_offset = 0;
  1234. setup->v2.mcast_window = 0;
  1235. setup->v2.rx_rssi_threshold = 0;
  1236. setup->v2.rx_ed_threshold = 0;
  1237. setup->v2.ref_clock = cpu_to_le32(644245094);
  1238. setup->v2.lpf_bandwidth = cpu_to_le16(65535);
  1239. setup->v2.osc_start_delay = cpu_to_le16(65535);
  1240. }
  1241. priv->tx(dev, skb, 1);
  1242. return 0;
  1243. }
  1244. static int p54_scan(struct ieee80211_hw *dev, u16 mode, u16 dwell,
  1245. u16 frequency)
  1246. {
  1247. struct p54_common *priv = dev->priv;
  1248. struct sk_buff *skb;
  1249. struct p54_scan *chan;
  1250. unsigned int i;
  1251. void *entry;
  1252. __le16 freq = cpu_to_le16(frequency);
  1253. skb = p54_alloc_skb(dev, P54_HDR_FLAG_CONTROL_OPSET, sizeof(*chan) +
  1254. sizeof(struct p54_hdr), P54_CONTROL_TYPE_SCAN,
  1255. GFP_ATOMIC);
  1256. if (!skb)
  1257. return -ENOMEM;
  1258. chan = (struct p54_scan *) skb_put(skb, sizeof(*chan));
  1259. memset(chan->padding1, 0, sizeof(chan->padding1));
  1260. chan->mode = cpu_to_le16(mode);
  1261. chan->dwell = cpu_to_le16(dwell);
  1262. for (i = 0; i < priv->iq_autocal_len; i++) {
  1263. if (priv->iq_autocal[i].freq != freq)
  1264. continue;
  1265. memcpy(&chan->iq_autocal, &priv->iq_autocal[i],
  1266. sizeof(*priv->iq_autocal));
  1267. break;
  1268. }
  1269. if (i == priv->iq_autocal_len)
  1270. goto err;
  1271. for (i = 0; i < priv->output_limit_len; i++) {
  1272. if (priv->output_limit[i].freq != freq)
  1273. continue;
  1274. chan->val_barker = 0x38;
  1275. chan->val_bpsk = chan->dup_bpsk =
  1276. priv->output_limit[i].val_bpsk;
  1277. chan->val_qpsk = chan->dup_qpsk =
  1278. priv->output_limit[i].val_qpsk;
  1279. chan->val_16qam = chan->dup_16qam =
  1280. priv->output_limit[i].val_16qam;
  1281. chan->val_64qam = chan->dup_64qam =
  1282. priv->output_limit[i].val_64qam;
  1283. break;
  1284. }
  1285. if (i == priv->output_limit_len)
  1286. goto err;
  1287. entry = priv->curve_data->data;
  1288. for (i = 0; i < priv->curve_data->channels; i++) {
  1289. if (*((__le16 *)entry) != freq) {
  1290. entry += sizeof(__le16);
  1291. entry += sizeof(struct p54_pa_curve_data_sample) *
  1292. priv->curve_data->points_per_channel;
  1293. continue;
  1294. }
  1295. entry += sizeof(__le16);
  1296. chan->pa_points_per_curve = 8;
  1297. memset(chan->curve_data, 0, sizeof(*chan->curve_data));
  1298. memcpy(chan->curve_data, entry,
  1299. sizeof(struct p54_pa_curve_data_sample) *
  1300. min((u8)8, priv->curve_data->points_per_channel));
  1301. break;
  1302. }
  1303. if (priv->fw_var < 0x500) {
  1304. chan->v1.rssical_mul = cpu_to_le16(130);
  1305. chan->v1.rssical_add = cpu_to_le16(0xfe70);
  1306. } else {
  1307. chan->v2.rssical_mul = cpu_to_le16(130);
  1308. chan->v2.rssical_add = cpu_to_le16(0xfe70);
  1309. chan->v2.basic_rate_mask = cpu_to_le32(priv->basic_rate_mask);
  1310. memset(chan->v2.rts_rates, 0, 8);
  1311. }
  1312. priv->tx(dev, skb, 1);
  1313. return 0;
  1314. err:
  1315. printk(KERN_ERR "%s: frequency change failed\n", wiphy_name(dev->wiphy));
  1316. kfree_skb(skb);
  1317. return -EINVAL;
  1318. }
  1319. static int p54_set_leds(struct ieee80211_hw *dev, int mode, int link, int act)
  1320. {
  1321. struct p54_common *priv = dev->priv;
  1322. struct sk_buff *skb;
  1323. struct p54_led *led;
  1324. skb = p54_alloc_skb(dev, P54_HDR_FLAG_CONTROL_OPSET, sizeof(*led) +
  1325. sizeof(struct p54_hdr), P54_CONTROL_TYPE_LED,
  1326. GFP_ATOMIC);
  1327. if (!skb)
  1328. return -ENOMEM;
  1329. led = (struct p54_led *)skb_put(skb, sizeof(*led));
  1330. led->mode = cpu_to_le16(mode);
  1331. led->led_permanent = cpu_to_le16(link);
  1332. led->led_temporary = cpu_to_le16(act);
  1333. led->duration = cpu_to_le16(1000);
  1334. priv->tx(dev, skb, 1);
  1335. return 0;
  1336. }
  1337. #define P54_SET_QUEUE(queue, ai_fs, cw_min, cw_max, _txop) \
  1338. do { \
  1339. queue.aifs = cpu_to_le16(ai_fs); \
  1340. queue.cwmin = cpu_to_le16(cw_min); \
  1341. queue.cwmax = cpu_to_le16(cw_max); \
  1342. queue.txop = cpu_to_le16(_txop); \
  1343. } while(0)
  1344. static int p54_set_edcf(struct ieee80211_hw *dev)
  1345. {
  1346. struct p54_common *priv = dev->priv;
  1347. struct sk_buff *skb;
  1348. struct p54_edcf *edcf;
  1349. skb = p54_alloc_skb(dev, P54_HDR_FLAG_CONTROL_OPSET, sizeof(*edcf) +
  1350. sizeof(struct p54_hdr), P54_CONTROL_TYPE_DCFINIT,
  1351. GFP_ATOMIC);
  1352. if (!skb)
  1353. return -ENOMEM;
  1354. edcf = (struct p54_edcf *)skb_put(skb, sizeof(*edcf));
  1355. if (priv->use_short_slot) {
  1356. edcf->slottime = 9;
  1357. edcf->sifs = 0x10;
  1358. edcf->eofpad = 0x00;
  1359. } else {
  1360. edcf->slottime = 20;
  1361. edcf->sifs = 0x0a;
  1362. edcf->eofpad = 0x06;
  1363. }
  1364. /* (see prism54/isl_oid.h for further details) */
  1365. edcf->frameburst = cpu_to_le16(0);
  1366. edcf->round_trip_delay = cpu_to_le16(0);
  1367. edcf->flags = 0;
  1368. memset(edcf->mapping, 0, sizeof(edcf->mapping));
  1369. memcpy(edcf->queue, priv->qos_params, sizeof(edcf->queue));
  1370. priv->tx(dev, skb, 1);
  1371. return 0;
  1372. }
  1373. static int p54_init_stats(struct ieee80211_hw *dev)
  1374. {
  1375. struct p54_common *priv = dev->priv;
  1376. priv->cached_stats = p54_alloc_skb(dev, P54_HDR_FLAG_CONTROL,
  1377. sizeof(struct p54_hdr) + sizeof(struct p54_statistics),
  1378. P54_CONTROL_TYPE_STAT_READBACK, GFP_KERNEL);
  1379. if (!priv->cached_stats)
  1380. return -ENOMEM;
  1381. mod_timer(&priv->stats_timer, jiffies + HZ);
  1382. return 0;
  1383. }
  1384. static int p54_beacon_tim(struct sk_buff *skb)
  1385. {
  1386. /*
  1387. * the good excuse for this mess is ... the firmware.
  1388. * The dummy TIM MUST be at the end of the beacon frame,
  1389. * because it'll be overwritten!
  1390. */
  1391. struct ieee80211_mgmt *mgmt = (void *)skb->data;
  1392. u8 *pos, *end;
  1393. if (skb->len <= sizeof(mgmt))
  1394. return -EINVAL;
  1395. pos = (u8 *)mgmt->u.beacon.variable;
  1396. end = skb->data + skb->len;
  1397. while (pos < end) {
  1398. if (pos + 2 + pos[1] > end)
  1399. return -EINVAL;
  1400. if (pos[0] == WLAN_EID_TIM) {
  1401. u8 dtim_len = pos[1];
  1402. u8 dtim_period = pos[3];
  1403. u8 *next = pos + 2 + dtim_len;
  1404. if (dtim_len < 3)
  1405. return -EINVAL;
  1406. memmove(pos, next, end - next);
  1407. if (dtim_len > 3)
  1408. skb_trim(skb, skb->len - (dtim_len - 3));
  1409. pos = end - (dtim_len + 2);
  1410. /* add the dummy at the end */
  1411. pos[0] = WLAN_EID_TIM;
  1412. pos[1] = 3;
  1413. pos[2] = 0;
  1414. pos[3] = dtim_period;
  1415. pos[4] = 0;
  1416. return 0;
  1417. }
  1418. pos += 2 + pos[1];
  1419. }
  1420. return 0;
  1421. }
  1422. static int p54_beacon_update(struct ieee80211_hw *dev,
  1423. struct ieee80211_vif *vif)
  1424. {
  1425. struct p54_common *priv = dev->priv;
  1426. struct sk_buff *beacon;
  1427. int ret;
  1428. if (priv->cached_beacon) {
  1429. p54_tx_cancel(dev, priv->cached_beacon);
  1430. /* wait for the last beacon the be freed */
  1431. msleep(10);
  1432. }
  1433. beacon = ieee80211_beacon_get(dev, vif);
  1434. if (!beacon)
  1435. return -ENOMEM;
  1436. ret = p54_beacon_tim(beacon);
  1437. if (ret)
  1438. return ret;
  1439. ret = p54_tx(dev, beacon);
  1440. if (ret)
  1441. return ret;
  1442. priv->cached_beacon = beacon;
  1443. priv->tsf_high32 = 0;
  1444. priv->tsf_low32 = 0;
  1445. return 0;
  1446. }
  1447. static int p54_start(struct ieee80211_hw *dev)
  1448. {
  1449. struct p54_common *priv = dev->priv;
  1450. int err;
  1451. mutex_lock(&priv->conf_mutex);
  1452. err = priv->open(dev);
  1453. if (err)
  1454. goto out;
  1455. P54_SET_QUEUE(priv->qos_params[0], 0x0002, 0x0003, 0x0007, 47);
  1456. P54_SET_QUEUE(priv->qos_params[1], 0x0002, 0x0007, 0x000f, 94);
  1457. P54_SET_QUEUE(priv->qos_params[2], 0x0003, 0x000f, 0x03ff, 0);
  1458. P54_SET_QUEUE(priv->qos_params[3], 0x0007, 0x000f, 0x03ff, 0);
  1459. err = p54_set_edcf(dev);
  1460. if (err)
  1461. goto out;
  1462. err = p54_init_stats(dev);
  1463. if (err)
  1464. goto out;
  1465. memset(priv->bssid, ~0, ETH_ALEN);
  1466. priv->mode = NL80211_IFTYPE_MONITOR;
  1467. err = p54_setup_mac(dev);
  1468. if (err) {
  1469. priv->mode = NL80211_IFTYPE_UNSPECIFIED;
  1470. goto out;
  1471. }
  1472. out:
  1473. mutex_unlock(&priv->conf_mutex);
  1474. return err;
  1475. }
  1476. static void p54_stop(struct ieee80211_hw *dev)
  1477. {
  1478. struct p54_common *priv = dev->priv;
  1479. struct sk_buff *skb;
  1480. mutex_lock(&priv->conf_mutex);
  1481. del_timer(&priv->stats_timer);
  1482. p54_free_skb(dev, priv->cached_stats);
  1483. priv->cached_stats = NULL;
  1484. if (priv->cached_beacon)
  1485. p54_tx_cancel(dev, priv->cached_beacon);
  1486. while ((skb = skb_dequeue(&priv->tx_queue)))
  1487. kfree_skb(skb);
  1488. priv->cached_beacon = NULL;
  1489. priv->stop(dev);
  1490. priv->tsf_high32 = priv->tsf_low32 = 0;
  1491. priv->mode = NL80211_IFTYPE_UNSPECIFIED;
  1492. mutex_unlock(&priv->conf_mutex);
  1493. }
  1494. static int p54_add_interface(struct ieee80211_hw *dev,
  1495. struct ieee80211_if_init_conf *conf)
  1496. {
  1497. struct p54_common *priv = dev->priv;
  1498. mutex_lock(&priv->conf_mutex);
  1499. if (priv->mode != NL80211_IFTYPE_MONITOR) {
  1500. mutex_unlock(&priv->conf_mutex);
  1501. return -EOPNOTSUPP;
  1502. }
  1503. switch (conf->type) {
  1504. case NL80211_IFTYPE_STATION:
  1505. case NL80211_IFTYPE_ADHOC:
  1506. case NL80211_IFTYPE_AP:
  1507. case NL80211_IFTYPE_MESH_POINT:
  1508. priv->mode = conf->type;
  1509. break;
  1510. default:
  1511. mutex_unlock(&priv->conf_mutex);
  1512. return -EOPNOTSUPP;
  1513. }
  1514. memcpy(priv->mac_addr, conf->mac_addr, ETH_ALEN);
  1515. p54_setup_mac(dev);
  1516. p54_set_leds(dev, 1, 0, 0);
  1517. mutex_unlock(&priv->conf_mutex);
  1518. return 0;
  1519. }
  1520. static void p54_remove_interface(struct ieee80211_hw *dev,
  1521. struct ieee80211_if_init_conf *conf)
  1522. {
  1523. struct p54_common *priv = dev->priv;
  1524. mutex_lock(&priv->conf_mutex);
  1525. if (priv->cached_beacon)
  1526. p54_tx_cancel(dev, priv->cached_beacon);
  1527. priv->mode = NL80211_IFTYPE_MONITOR;
  1528. memset(priv->mac_addr, 0, ETH_ALEN);
  1529. memset(priv->bssid, 0, ETH_ALEN);
  1530. p54_setup_mac(dev);
  1531. mutex_unlock(&priv->conf_mutex);
  1532. }
  1533. static int p54_config(struct ieee80211_hw *dev, u32 changed)
  1534. {
  1535. int ret;
  1536. struct p54_common *priv = dev->priv;
  1537. struct ieee80211_conf *conf = &dev->conf;
  1538. mutex_lock(&priv->conf_mutex);
  1539. if (changed & IEEE80211_CONF_CHANGE_POWER)
  1540. priv->output_power = conf->power_level << 2;
  1541. if (changed & IEEE80211_CONF_CHANGE_RADIO_ENABLED) {
  1542. ret = p54_setup_mac(dev);
  1543. if (ret)
  1544. goto out;
  1545. }
  1546. if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
  1547. ret = p54_scan(dev, P54_SCAN_EXIT, 0,
  1548. conf->channel->center_freq);
  1549. if (ret)
  1550. goto out;
  1551. }
  1552. out:
  1553. mutex_unlock(&priv->conf_mutex);
  1554. return ret;
  1555. }
  1556. static int p54_config_interface(struct ieee80211_hw *dev,
  1557. struct ieee80211_vif *vif,
  1558. struct ieee80211_if_conf *conf)
  1559. {
  1560. struct p54_common *priv = dev->priv;
  1561. int ret = 0;
  1562. mutex_lock(&priv->conf_mutex);
  1563. if (conf->changed & IEEE80211_IFCC_BSSID) {
  1564. memcpy(priv->bssid, conf->bssid, ETH_ALEN);
  1565. ret = p54_setup_mac(dev);
  1566. if (ret)
  1567. goto out;
  1568. }
  1569. if (conf->changed & IEEE80211_IFCC_BEACON) {
  1570. ret = p54_scan(dev, P54_SCAN_EXIT, 0,
  1571. dev->conf.channel->center_freq);
  1572. if (ret)
  1573. goto out;
  1574. ret = p54_setup_mac(dev);
  1575. if (ret)
  1576. goto out;
  1577. ret = p54_beacon_update(dev, vif);
  1578. if (ret)
  1579. goto out;
  1580. ret = p54_set_edcf(dev);
  1581. if (ret)
  1582. goto out;
  1583. }
  1584. ret = p54_set_leds(dev, 1, !is_multicast_ether_addr(priv->bssid), 0);
  1585. out:
  1586. mutex_unlock(&priv->conf_mutex);
  1587. return ret;
  1588. }
  1589. static void p54_configure_filter(struct ieee80211_hw *dev,
  1590. unsigned int changed_flags,
  1591. unsigned int *total_flags,
  1592. int mc_count, struct dev_mc_list *mclist)
  1593. {
  1594. struct p54_common *priv = dev->priv;
  1595. *total_flags &= FIF_PROMISC_IN_BSS |
  1596. (*total_flags & FIF_PROMISC_IN_BSS) ?
  1597. FIF_FCSFAIL : 0;
  1598. priv->filter_flags = *total_flags;
  1599. if (changed_flags & FIF_PROMISC_IN_BSS)
  1600. p54_setup_mac(dev);
  1601. }
  1602. static int p54_conf_tx(struct ieee80211_hw *dev, u16 queue,
  1603. const struct ieee80211_tx_queue_params *params)
  1604. {
  1605. struct p54_common *priv = dev->priv;
  1606. int ret;
  1607. mutex_lock(&priv->conf_mutex);
  1608. if ((params) && !(queue > 4)) {
  1609. P54_SET_QUEUE(priv->qos_params[queue], params->aifs,
  1610. params->cw_min, params->cw_max, params->txop);
  1611. ret = p54_set_edcf(dev);
  1612. } else
  1613. ret = -EINVAL;
  1614. mutex_unlock(&priv->conf_mutex);
  1615. return ret;
  1616. }
  1617. static int p54_init_xbow_synth(struct ieee80211_hw *dev)
  1618. {
  1619. struct p54_common *priv = dev->priv;
  1620. struct sk_buff *skb;
  1621. struct p54_xbow_synth *xbow;
  1622. skb = p54_alloc_skb(dev, P54_HDR_FLAG_CONTROL_OPSET, sizeof(*xbow) +
  1623. sizeof(struct p54_hdr),
  1624. P54_CONTROL_TYPE_XBOW_SYNTH_CFG,
  1625. GFP_KERNEL);
  1626. if (!skb)
  1627. return -ENOMEM;
  1628. xbow = (struct p54_xbow_synth *)skb_put(skb, sizeof(*xbow));
  1629. xbow->magic1 = cpu_to_le16(0x1);
  1630. xbow->magic2 = cpu_to_le16(0x2);
  1631. xbow->freq = cpu_to_le16(5390);
  1632. memset(xbow->padding, 0, sizeof(xbow->padding));
  1633. priv->tx(dev, skb, 1);
  1634. return 0;
  1635. }
  1636. static void p54_statistics_timer(unsigned long data)
  1637. {
  1638. struct ieee80211_hw *dev = (struct ieee80211_hw *) data;
  1639. struct p54_common *priv = dev->priv;
  1640. BUG_ON(!priv->cached_stats);
  1641. priv->tx(dev, priv->cached_stats, 0);
  1642. }
  1643. static int p54_get_stats(struct ieee80211_hw *dev,
  1644. struct ieee80211_low_level_stats *stats)
  1645. {
  1646. struct p54_common *priv = dev->priv;
  1647. del_timer(&priv->stats_timer);
  1648. p54_statistics_timer((unsigned long)dev);
  1649. if (!wait_for_completion_interruptible_timeout(&priv->stats_comp, HZ)) {
  1650. printk(KERN_ERR "%s: device does not respond!\n",
  1651. wiphy_name(dev->wiphy));
  1652. return -EBUSY;
  1653. }
  1654. memcpy(stats, &priv->stats, sizeof(*stats));
  1655. return 0;
  1656. }
  1657. static int p54_get_tx_stats(struct ieee80211_hw *dev,
  1658. struct ieee80211_tx_queue_stats *stats)
  1659. {
  1660. struct p54_common *priv = dev->priv;
  1661. memcpy(stats, &priv->tx_stats[4], sizeof(stats[0]) * dev->queues);
  1662. return 0;
  1663. }
  1664. static void p54_bss_info_changed(struct ieee80211_hw *dev,
  1665. struct ieee80211_vif *vif,
  1666. struct ieee80211_bss_conf *info,
  1667. u32 changed)
  1668. {
  1669. struct p54_common *priv = dev->priv;
  1670. if (changed & BSS_CHANGED_ERP_SLOT) {
  1671. priv->use_short_slot = info->use_short_slot;
  1672. p54_set_edcf(dev);
  1673. }
  1674. if (changed & BSS_CHANGED_BASIC_RATES) {
  1675. if (dev->conf.channel->band == IEEE80211_BAND_5GHZ)
  1676. priv->basic_rate_mask = (info->basic_rates << 4);
  1677. else
  1678. priv->basic_rate_mask = info->basic_rates;
  1679. p54_setup_mac(dev);
  1680. if (priv->fw_var >= 0x500)
  1681. p54_scan(dev, P54_SCAN_EXIT, 0,
  1682. dev->conf.channel->center_freq);
  1683. }
  1684. if (changed & BSS_CHANGED_ASSOC) {
  1685. if (info->assoc) {
  1686. priv->aid = info->aid;
  1687. priv->wakeup_timer = info->beacon_int *
  1688. info->dtim_period * 5;
  1689. p54_setup_mac(dev);
  1690. }
  1691. }
  1692. }
  1693. static int p54_set_key(struct ieee80211_hw *dev, enum set_key_cmd cmd,
  1694. const u8 *local_address, const u8 *address,
  1695. struct ieee80211_key_conf *key)
  1696. {
  1697. struct p54_common *priv = dev->priv;
  1698. struct sk_buff *skb;
  1699. struct p54_keycache *rxkey;
  1700. u8 algo = 0;
  1701. if (modparam_nohwcrypt)
  1702. return -EOPNOTSUPP;
  1703. if (cmd == DISABLE_KEY)
  1704. algo = 0;
  1705. else {
  1706. switch (key->alg) {
  1707. case ALG_TKIP:
  1708. if (!(priv->privacy_caps & (BR_DESC_PRIV_CAP_MICHAEL |
  1709. BR_DESC_PRIV_CAP_TKIP)))
  1710. return -EOPNOTSUPP;
  1711. key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
  1712. algo = P54_CRYPTO_TKIPMICHAEL;
  1713. break;
  1714. case ALG_WEP:
  1715. if (!(priv->privacy_caps & BR_DESC_PRIV_CAP_WEP))
  1716. return -EOPNOTSUPP;
  1717. key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
  1718. algo = P54_CRYPTO_WEP;
  1719. break;
  1720. case ALG_CCMP:
  1721. if (!(priv->privacy_caps & BR_DESC_PRIV_CAP_AESCCMP))
  1722. return -EOPNOTSUPP;
  1723. key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
  1724. algo = P54_CRYPTO_AESCCMP;
  1725. break;
  1726. default:
  1727. return -EINVAL;
  1728. }
  1729. }
  1730. if (key->keyidx > priv->rx_keycache_size) {
  1731. /*
  1732. * The device supports the choosen algorithm, but the firmware
  1733. * does not provide enough key slots to store all of them.
  1734. * So, incoming frames have to be decoded by the mac80211 stack,
  1735. * but we can still offload encryption for outgoing frames.
  1736. */
  1737. return 0;
  1738. }
  1739. mutex_lock(&priv->conf_mutex);
  1740. skb = p54_alloc_skb(dev, P54_HDR_FLAG_CONTROL_OPSET, sizeof(*rxkey) +
  1741. sizeof(struct p54_hdr), P54_CONTROL_TYPE_RX_KEYCACHE,
  1742. GFP_ATOMIC);
  1743. if (!skb) {
  1744. mutex_unlock(&priv->conf_mutex);
  1745. return -ENOMEM;
  1746. }
  1747. /* TODO: some devices have 4 more free slots for rx keys */
  1748. rxkey = (struct p54_keycache *)skb_put(skb, sizeof(*rxkey));
  1749. rxkey->entry = key->keyidx;
  1750. rxkey->key_id = key->keyidx;
  1751. rxkey->key_type = algo;
  1752. if (address)
  1753. memcpy(rxkey->mac, address, ETH_ALEN);
  1754. else
  1755. memset(rxkey->mac, ~0, ETH_ALEN);
  1756. if (key->alg != ALG_TKIP) {
  1757. rxkey->key_len = min((u8)16, key->keylen);
  1758. memcpy(rxkey->key, key->key, rxkey->key_len);
  1759. } else {
  1760. rxkey->key_len = 24;
  1761. memcpy(rxkey->key, key->key, 16);
  1762. memcpy(&(rxkey->key[16]), &(key->key
  1763. [NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY]), 8);
  1764. }
  1765. priv->tx(dev, skb, 1);
  1766. mutex_unlock(&priv->conf_mutex);
  1767. return 0;
  1768. }
  1769. static const struct ieee80211_ops p54_ops = {
  1770. .tx = p54_tx,
  1771. .start = p54_start,
  1772. .stop = p54_stop,
  1773. .add_interface = p54_add_interface,
  1774. .remove_interface = p54_remove_interface,
  1775. .set_tim = p54_set_tim,
  1776. .sta_notify_ps = p54_sta_notify_ps,
  1777. .sta_notify = p54_sta_notify,
  1778. .set_key = p54_set_key,
  1779. .config = p54_config,
  1780. .config_interface = p54_config_interface,
  1781. .bss_info_changed = p54_bss_info_changed,
  1782. .configure_filter = p54_configure_filter,
  1783. .conf_tx = p54_conf_tx,
  1784. .get_stats = p54_get_stats,
  1785. .get_tx_stats = p54_get_tx_stats
  1786. };
  1787. struct ieee80211_hw *p54_init_common(size_t priv_data_len)
  1788. {
  1789. struct ieee80211_hw *dev;
  1790. struct p54_common *priv;
  1791. dev = ieee80211_alloc_hw(priv_data_len, &p54_ops);
  1792. if (!dev)
  1793. return NULL;
  1794. priv = dev->priv;
  1795. priv->mode = NL80211_IFTYPE_UNSPECIFIED;
  1796. priv->basic_rate_mask = 0x15f;
  1797. skb_queue_head_init(&priv->tx_queue);
  1798. dev->flags = IEEE80211_HW_RX_INCLUDES_FCS |
  1799. IEEE80211_HW_SIGNAL_DBM |
  1800. IEEE80211_HW_NOISE_DBM;
  1801. dev->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
  1802. BIT(NL80211_IFTYPE_ADHOC) |
  1803. BIT(NL80211_IFTYPE_AP) |
  1804. BIT(NL80211_IFTYPE_MESH_POINT);
  1805. dev->channel_change_time = 1000; /* TODO: find actual value */
  1806. priv->tx_stats[0].limit = 1; /* Beacon queue */
  1807. priv->tx_stats[1].limit = 1; /* Probe queue for HW scan */
  1808. priv->tx_stats[2].limit = 3; /* queue for MLMEs */
  1809. priv->tx_stats[3].limit = 3; /* Broadcast / MC queue */
  1810. priv->tx_stats[4].limit = 5; /* Data */
  1811. dev->queues = 1;
  1812. priv->noise = -94;
  1813. /*
  1814. * We support at most 8 tries no matter which rate they're at,
  1815. * we cannot support max_rates * max_rate_tries as we set it
  1816. * here, but setting it correctly to 4/2 or so would limit us
  1817. * artificially if the RC algorithm wants just two rates, so
  1818. * let's say 4/7, we'll redistribute it at TX time, see the
  1819. * comments there.
  1820. */
  1821. dev->max_rates = 4;
  1822. dev->max_rate_tries = 7;
  1823. dev->extra_tx_headroom = sizeof(struct p54_hdr) + 4 +
  1824. sizeof(struct p54_tx_data);
  1825. mutex_init(&priv->conf_mutex);
  1826. init_completion(&priv->eeprom_comp);
  1827. init_completion(&priv->stats_comp);
  1828. setup_timer(&priv->stats_timer, p54_statistics_timer,
  1829. (unsigned long)dev);
  1830. return dev;
  1831. }
  1832. EXPORT_SYMBOL_GPL(p54_init_common);
  1833. void p54_free_common(struct ieee80211_hw *dev)
  1834. {
  1835. struct p54_common *priv = dev->priv;
  1836. del_timer(&priv->stats_timer);
  1837. kfree_skb(priv->cached_stats);
  1838. kfree(priv->iq_autocal);
  1839. kfree(priv->output_limit);
  1840. kfree(priv->curve_data);
  1841. }
  1842. EXPORT_SYMBOL_GPL(p54_free_common);
  1843. static int __init p54_init(void)
  1844. {
  1845. return 0;
  1846. }
  1847. static void __exit p54_exit(void)
  1848. {
  1849. }
  1850. module_init(p54_init);
  1851. module_exit(p54_exit);