p54common.c 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191
  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 void p54_parse_rssical(struct ieee80211_hw *dev, void *data, int len,
  306. u16 type)
  307. {
  308. struct p54_common *priv = dev->priv;
  309. int offset = (type == PDR_RSSI_LINEAR_APPROXIMATION_EXTENDED) ? 2 : 0;
  310. int entry_size = sizeof(struct pda_rssi_cal_entry) + offset;
  311. int num_entries = (type == PDR_RSSI_LINEAR_APPROXIMATION) ? 1 : 2;
  312. int i;
  313. if (len != (entry_size * num_entries)) {
  314. printk(KERN_ERR "%s: unknown rssi calibration data packing "
  315. " type:(%x) len:%d.\n",
  316. wiphy_name(dev->wiphy), type, len);
  317. print_hex_dump_bytes("rssical:", DUMP_PREFIX_NONE,
  318. data, len);
  319. printk(KERN_ERR "%s: please report this issue.\n",
  320. wiphy_name(dev->wiphy));
  321. return;
  322. }
  323. for (i = 0; i < num_entries; i++) {
  324. struct pda_rssi_cal_entry *cal = data +
  325. (offset + i * entry_size);
  326. priv->rssical_db[i].mul = (s16) le16_to_cpu(cal->mul);
  327. priv->rssical_db[i].add = (s16) le16_to_cpu(cal->add);
  328. }
  329. }
  330. static int p54_parse_eeprom(struct ieee80211_hw *dev, void *eeprom, int len)
  331. {
  332. struct p54_common *priv = dev->priv;
  333. struct eeprom_pda_wrap *wrap = NULL;
  334. struct pda_entry *entry;
  335. unsigned int data_len, entry_len;
  336. void *tmp;
  337. int err;
  338. u8 *end = (u8 *)eeprom + len;
  339. u16 synth = 0;
  340. wrap = (struct eeprom_pda_wrap *) eeprom;
  341. entry = (void *)wrap->data + le16_to_cpu(wrap->len);
  342. /* verify that at least the entry length/code fits */
  343. while ((u8 *)entry <= end - sizeof(*entry)) {
  344. entry_len = le16_to_cpu(entry->len);
  345. data_len = ((entry_len - 1) << 1);
  346. /* abort if entry exceeds whole structure */
  347. if ((u8 *)entry + sizeof(*entry) + data_len > end)
  348. break;
  349. switch (le16_to_cpu(entry->code)) {
  350. case PDR_MAC_ADDRESS:
  351. SET_IEEE80211_PERM_ADDR(dev, entry->data);
  352. break;
  353. case PDR_PRISM_PA_CAL_OUTPUT_POWER_LIMITS:
  354. if (data_len < 2) {
  355. err = -EINVAL;
  356. goto err;
  357. }
  358. if (2 + entry->data[1]*sizeof(*priv->output_limit) > data_len) {
  359. err = -EINVAL;
  360. goto err;
  361. }
  362. priv->output_limit = kmalloc(entry->data[1] *
  363. sizeof(*priv->output_limit), GFP_KERNEL);
  364. if (!priv->output_limit) {
  365. err = -ENOMEM;
  366. goto err;
  367. }
  368. memcpy(priv->output_limit, &entry->data[2],
  369. entry->data[1]*sizeof(*priv->output_limit));
  370. priv->output_limit_len = entry->data[1];
  371. break;
  372. case PDR_PRISM_PA_CAL_CURVE_DATA: {
  373. struct pda_pa_curve_data *curve_data =
  374. (struct pda_pa_curve_data *)entry->data;
  375. if (data_len < sizeof(*curve_data)) {
  376. err = -EINVAL;
  377. goto err;
  378. }
  379. switch (curve_data->cal_method_rev) {
  380. case 0:
  381. err = p54_convert_rev0(dev, curve_data);
  382. break;
  383. case 1:
  384. err = p54_convert_rev1(dev, curve_data);
  385. break;
  386. default:
  387. printk(KERN_ERR "%s: unknown curve data "
  388. "revision %d\n",
  389. wiphy_name(dev->wiphy),
  390. curve_data->cal_method_rev);
  391. err = -ENODEV;
  392. break;
  393. }
  394. if (err)
  395. goto err;
  396. }
  397. case PDR_PRISM_ZIF_TX_IQ_CALIBRATION:
  398. priv->iq_autocal = kmalloc(data_len, GFP_KERNEL);
  399. if (!priv->iq_autocal) {
  400. err = -ENOMEM;
  401. goto err;
  402. }
  403. memcpy(priv->iq_autocal, entry->data, data_len);
  404. priv->iq_autocal_len = data_len / sizeof(struct pda_iq_autocal_entry);
  405. break;
  406. case PDR_INTERFACE_LIST:
  407. tmp = entry->data;
  408. while ((u8 *)tmp < entry->data + data_len) {
  409. struct bootrec_exp_if *exp_if = tmp;
  410. if (le16_to_cpu(exp_if->if_id) == 0xf)
  411. synth = le16_to_cpu(exp_if->variant);
  412. tmp += sizeof(struct bootrec_exp_if);
  413. }
  414. break;
  415. case PDR_HARDWARE_PLATFORM_COMPONENT_ID:
  416. priv->version = *(u8 *)(entry->data + 1);
  417. break;
  418. case PDR_RSSI_LINEAR_APPROXIMATION:
  419. case PDR_RSSI_LINEAR_APPROXIMATION_DUAL_BAND:
  420. case PDR_RSSI_LINEAR_APPROXIMATION_EXTENDED:
  421. p54_parse_rssical(dev, entry->data, data_len,
  422. le16_to_cpu(entry->code));
  423. break;
  424. case PDR_END:
  425. /* make it overrun */
  426. entry_len = len;
  427. break;
  428. case PDR_MANUFACTURING_PART_NUMBER:
  429. case PDR_PDA_VERSION:
  430. case PDR_NIC_SERIAL_NUMBER:
  431. case PDR_REGULATORY_DOMAIN_LIST:
  432. case PDR_TEMPERATURE_TYPE:
  433. case PDR_PRISM_PCI_IDENTIFIER:
  434. case PDR_COUNTRY_INFORMATION:
  435. case PDR_OEM_NAME:
  436. case PDR_PRODUCT_NAME:
  437. case PDR_UTF8_OEM_NAME:
  438. case PDR_UTF8_PRODUCT_NAME:
  439. case PDR_COUNTRY_LIST:
  440. case PDR_DEFAULT_COUNTRY:
  441. case PDR_ANTENNA_GAIN:
  442. case PDR_PRISM_INDIGO_PA_CALIBRATION_DATA:
  443. case PDR_REGULATORY_POWER_LIMITS:
  444. case PDR_RADIATED_TRANSMISSION_CORRECTION:
  445. case PDR_PRISM_TX_IQ_CALIBRATION:
  446. case PDR_BASEBAND_REGISTERS:
  447. case PDR_PER_CHANNEL_BASEBAND_REGISTERS:
  448. break;
  449. default:
  450. printk(KERN_INFO "%s: unknown eeprom code : 0x%x\n",
  451. wiphy_name(dev->wiphy),
  452. le16_to_cpu(entry->code));
  453. break;
  454. }
  455. entry = (void *)entry + (entry_len + 1)*2;
  456. }
  457. if (!synth || !priv->iq_autocal || !priv->output_limit ||
  458. !priv->curve_data) {
  459. printk(KERN_ERR "%s: not all required entries found in eeprom!\n",
  460. wiphy_name(dev->wiphy));
  461. err = -EINVAL;
  462. goto err;
  463. }
  464. priv->rxhw = synth & PDR_SYNTH_FRONTEND_MASK;
  465. if (priv->rxhw == 4)
  466. p54_init_xbow_synth(dev);
  467. if (!(synth & PDR_SYNTH_24_GHZ_DISABLED))
  468. dev->wiphy->bands[IEEE80211_BAND_2GHZ] = &band_2GHz;
  469. if (!(synth & PDR_SYNTH_5_GHZ_DISABLED))
  470. dev->wiphy->bands[IEEE80211_BAND_5GHZ] = &band_5GHz;
  471. if (!is_valid_ether_addr(dev->wiphy->perm_addr)) {
  472. u8 perm_addr[ETH_ALEN];
  473. printk(KERN_WARNING "%s: Invalid hwaddr! Using randomly generated MAC addr\n",
  474. wiphy_name(dev->wiphy));
  475. random_ether_addr(perm_addr);
  476. SET_IEEE80211_PERM_ADDR(dev, perm_addr);
  477. }
  478. printk(KERN_INFO "%s: hwaddr %pM, MAC:isl38%02x RF:%s\n",
  479. wiphy_name(dev->wiphy),
  480. dev->wiphy->perm_addr,
  481. priv->version, p54_rf_chips[priv->rxhw]);
  482. return 0;
  483. err:
  484. if (priv->iq_autocal) {
  485. kfree(priv->iq_autocal);
  486. priv->iq_autocal = NULL;
  487. }
  488. if (priv->output_limit) {
  489. kfree(priv->output_limit);
  490. priv->output_limit = NULL;
  491. }
  492. if (priv->curve_data) {
  493. kfree(priv->curve_data);
  494. priv->curve_data = NULL;
  495. }
  496. printk(KERN_ERR "%s: eeprom parse failed!\n",
  497. wiphy_name(dev->wiphy));
  498. return err;
  499. }
  500. static int p54_rssi_to_dbm(struct ieee80211_hw *dev, int rssi)
  501. {
  502. struct p54_common *priv = dev->priv;
  503. int band = dev->conf.channel->band;
  504. return ((rssi * priv->rssical_db[band].mul) / 64 +
  505. priv->rssical_db[band].add) / 4;
  506. }
  507. static int p54_rx_data(struct ieee80211_hw *dev, struct sk_buff *skb)
  508. {
  509. struct p54_common *priv = dev->priv;
  510. struct p54_rx_data *hdr = (struct p54_rx_data *) skb->data;
  511. struct ieee80211_rx_status rx_status = {0};
  512. u16 freq = le16_to_cpu(hdr->freq);
  513. size_t header_len = sizeof(*hdr);
  514. u32 tsf32;
  515. /*
  516. * If the device is in a unspecified state we have to
  517. * ignore all data frames. Else we could end up with a
  518. * nasty crash.
  519. */
  520. if (unlikely(priv->mode == NL80211_IFTYPE_UNSPECIFIED))
  521. return 0;
  522. if (!(hdr->flags & cpu_to_le16(P54_HDR_FLAG_DATA_IN_FCS_GOOD))) {
  523. if (priv->filter_flags & FIF_FCSFAIL)
  524. rx_status.flag |= RX_FLAG_FAILED_FCS_CRC;
  525. else
  526. return 0;
  527. }
  528. if (hdr->decrypt_status == P54_DECRYPT_OK)
  529. rx_status.flag |= RX_FLAG_DECRYPTED;
  530. if ((hdr->decrypt_status == P54_DECRYPT_FAIL_MICHAEL) ||
  531. (hdr->decrypt_status == P54_DECRYPT_FAIL_TKIP))
  532. rx_status.flag |= RX_FLAG_MMIC_ERROR;
  533. rx_status.signal = p54_rssi_to_dbm(dev, hdr->rssi);
  534. rx_status.noise = priv->noise;
  535. /* XX correct? */
  536. rx_status.qual = (100 * hdr->rssi) / 127;
  537. if (hdr->rate & 0x10)
  538. rx_status.flag |= RX_FLAG_SHORTPRE;
  539. rx_status.rate_idx = (dev->conf.channel->band == IEEE80211_BAND_2GHZ ?
  540. hdr->rate : (hdr->rate - 4)) & 0xf;
  541. rx_status.freq = freq;
  542. rx_status.band = dev->conf.channel->band;
  543. rx_status.antenna = hdr->antenna;
  544. tsf32 = le32_to_cpu(hdr->tsf32);
  545. if (tsf32 < priv->tsf_low32)
  546. priv->tsf_high32++;
  547. rx_status.mactime = ((u64)priv->tsf_high32) << 32 | tsf32;
  548. priv->tsf_low32 = tsf32;
  549. rx_status.flag |= RX_FLAG_TSFT;
  550. if (hdr->flags & cpu_to_le16(P54_HDR_FLAG_DATA_ALIGN))
  551. header_len += hdr->align[0];
  552. skb_pull(skb, header_len);
  553. skb_trim(skb, le16_to_cpu(hdr->len));
  554. ieee80211_rx_irqsafe(dev, skb, &rx_status);
  555. queue_delayed_work(dev->workqueue, &priv->work,
  556. msecs_to_jiffies(P54_STATISTICS_UPDATE));
  557. return -1;
  558. }
  559. static void inline p54_wake_free_queues(struct ieee80211_hw *dev)
  560. {
  561. struct p54_common *priv = dev->priv;
  562. int i;
  563. if (priv->mode == NL80211_IFTYPE_UNSPECIFIED)
  564. return ;
  565. for (i = 0; i < dev->queues; i++)
  566. if (priv->tx_stats[i + 4].len < priv->tx_stats[i + 4].limit)
  567. ieee80211_wake_queue(dev, i);
  568. }
  569. void p54_free_skb(struct ieee80211_hw *dev, struct sk_buff *skb)
  570. {
  571. struct p54_common *priv = dev->priv;
  572. struct ieee80211_tx_info *info;
  573. struct memrecord *range;
  574. unsigned long flags;
  575. u32 freed = 0, last_addr = priv->rx_start;
  576. if (unlikely(!skb || !dev || !skb_queue_len(&priv->tx_queue)))
  577. return;
  578. /*
  579. * don't try to free an already unlinked skb
  580. */
  581. if (unlikely((!skb->next) || (!skb->prev)))
  582. return;
  583. spin_lock_irqsave(&priv->tx_queue.lock, flags);
  584. info = IEEE80211_SKB_CB(skb);
  585. range = (void *)info->rate_driver_data;
  586. if (skb->prev != (struct sk_buff *)&priv->tx_queue) {
  587. struct ieee80211_tx_info *ni;
  588. struct memrecord *mr;
  589. ni = IEEE80211_SKB_CB(skb->prev);
  590. mr = (struct memrecord *)ni->rate_driver_data;
  591. last_addr = mr->end_addr;
  592. }
  593. if (skb->next != (struct sk_buff *)&priv->tx_queue) {
  594. struct ieee80211_tx_info *ni;
  595. struct memrecord *mr;
  596. ni = IEEE80211_SKB_CB(skb->next);
  597. mr = (struct memrecord *)ni->rate_driver_data;
  598. freed = mr->start_addr - last_addr;
  599. } else
  600. freed = priv->rx_end - last_addr;
  601. __skb_unlink(skb, &priv->tx_queue);
  602. spin_unlock_irqrestore(&priv->tx_queue.lock, flags);
  603. dev_kfree_skb_any(skb);
  604. if (freed >= priv->headroom + sizeof(struct p54_hdr) + 48 +
  605. IEEE80211_MAX_RTS_THRESHOLD + priv->tailroom)
  606. p54_wake_free_queues(dev);
  607. }
  608. EXPORT_SYMBOL_GPL(p54_free_skb);
  609. static struct sk_buff *p54_find_tx_entry(struct ieee80211_hw *dev,
  610. __le32 req_id)
  611. {
  612. struct p54_common *priv = dev->priv;
  613. struct sk_buff *entry = priv->tx_queue.next;
  614. unsigned long flags;
  615. spin_lock_irqsave(&priv->tx_queue.lock, flags);
  616. while (entry != (struct sk_buff *)&priv->tx_queue) {
  617. struct p54_hdr *hdr = (struct p54_hdr *) entry->data;
  618. if (hdr->req_id == req_id) {
  619. spin_unlock_irqrestore(&priv->tx_queue.lock, flags);
  620. return entry;
  621. }
  622. entry = entry->next;
  623. }
  624. spin_unlock_irqrestore(&priv->tx_queue.lock, flags);
  625. return NULL;
  626. }
  627. static void p54_rx_frame_sent(struct ieee80211_hw *dev, struct sk_buff *skb)
  628. {
  629. struct p54_common *priv = dev->priv;
  630. struct p54_hdr *hdr = (struct p54_hdr *) skb->data;
  631. struct p54_frame_sent *payload = (struct p54_frame_sent *) hdr->data;
  632. struct sk_buff *entry = (struct sk_buff *) priv->tx_queue.next;
  633. u32 addr = le32_to_cpu(hdr->req_id) - priv->headroom;
  634. struct memrecord *range = NULL;
  635. u32 freed = 0;
  636. u32 last_addr = priv->rx_start;
  637. unsigned long flags;
  638. int count, idx;
  639. spin_lock_irqsave(&priv->tx_queue.lock, flags);
  640. while (entry != (struct sk_buff *)&priv->tx_queue) {
  641. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(entry);
  642. struct p54_hdr *entry_hdr;
  643. struct p54_tx_data *entry_data;
  644. int pad = 0;
  645. range = (void *)info->rate_driver_data;
  646. if (range->start_addr != addr) {
  647. last_addr = range->end_addr;
  648. entry = entry->next;
  649. continue;
  650. }
  651. if (entry->next != (struct sk_buff *)&priv->tx_queue) {
  652. struct ieee80211_tx_info *ni;
  653. struct memrecord *mr;
  654. ni = IEEE80211_SKB_CB(entry->next);
  655. mr = (struct memrecord *)ni->rate_driver_data;
  656. freed = mr->start_addr - last_addr;
  657. } else
  658. freed = priv->rx_end - last_addr;
  659. last_addr = range->end_addr;
  660. __skb_unlink(entry, &priv->tx_queue);
  661. spin_unlock_irqrestore(&priv->tx_queue.lock, flags);
  662. entry_hdr = (struct p54_hdr *) entry->data;
  663. entry_data = (struct p54_tx_data *) entry_hdr->data;
  664. priv->tx_stats[entry_data->hw_queue].len--;
  665. priv->stats.dot11ACKFailureCount += payload->tries - 1;
  666. if (unlikely(entry == priv->cached_beacon)) {
  667. kfree_skb(entry);
  668. priv->cached_beacon = NULL;
  669. goto out;
  670. }
  671. /*
  672. * Clear manually, ieee80211_tx_info_clear_status would
  673. * clear the counts too and we need them.
  674. */
  675. memset(&info->status.ampdu_ack_len, 0,
  676. sizeof(struct ieee80211_tx_info) -
  677. offsetof(struct ieee80211_tx_info, status.ampdu_ack_len));
  678. BUILD_BUG_ON(offsetof(struct ieee80211_tx_info,
  679. status.ampdu_ack_len) != 23);
  680. if (entry_hdr->flags & cpu_to_le16(P54_HDR_FLAG_DATA_ALIGN))
  681. pad = entry_data->align[0];
  682. /* walk through the rates array and adjust the counts */
  683. count = payload->tries;
  684. for (idx = 0; idx < 4; idx++) {
  685. if (count >= info->status.rates[idx].count) {
  686. count -= info->status.rates[idx].count;
  687. } else if (count > 0) {
  688. info->status.rates[idx].count = count;
  689. count = 0;
  690. } else {
  691. info->status.rates[idx].idx = -1;
  692. info->status.rates[idx].count = 0;
  693. }
  694. }
  695. if (!(info->flags & IEEE80211_TX_CTL_NO_ACK) &&
  696. (!payload->status))
  697. info->flags |= IEEE80211_TX_STAT_ACK;
  698. if (payload->status & P54_TX_PSM_CANCELLED)
  699. info->flags |= IEEE80211_TX_STAT_TX_FILTERED;
  700. info->status.ack_signal = p54_rssi_to_dbm(dev,
  701. (int)payload->ack_rssi);
  702. skb_pull(entry, sizeof(*hdr) + pad + sizeof(*entry_data));
  703. ieee80211_tx_status_irqsafe(dev, entry);
  704. goto out;
  705. }
  706. spin_unlock_irqrestore(&priv->tx_queue.lock, flags);
  707. out:
  708. if (freed >= priv->headroom + sizeof(struct p54_hdr) + 48 +
  709. IEEE80211_MAX_RTS_THRESHOLD + priv->tailroom)
  710. p54_wake_free_queues(dev);
  711. }
  712. static void p54_rx_eeprom_readback(struct ieee80211_hw *dev,
  713. struct sk_buff *skb)
  714. {
  715. struct p54_hdr *hdr = (struct p54_hdr *) skb->data;
  716. struct p54_eeprom_lm86 *eeprom = (struct p54_eeprom_lm86 *) hdr->data;
  717. struct p54_common *priv = dev->priv;
  718. if (!priv->eeprom)
  719. return ;
  720. if (priv->fw_var >= 0x509) {
  721. memcpy(priv->eeprom, eeprom->v2.data,
  722. le16_to_cpu(eeprom->v2.len));
  723. } else {
  724. memcpy(priv->eeprom, eeprom->v1.data,
  725. le16_to_cpu(eeprom->v1.len));
  726. }
  727. complete(&priv->eeprom_comp);
  728. }
  729. static void p54_rx_stats(struct ieee80211_hw *dev, struct sk_buff *skb)
  730. {
  731. struct p54_common *priv = dev->priv;
  732. struct p54_hdr *hdr = (struct p54_hdr *) skb->data;
  733. struct p54_statistics *stats = (struct p54_statistics *) hdr->data;
  734. u32 tsf32;
  735. if (unlikely(priv->mode == NL80211_IFTYPE_UNSPECIFIED))
  736. return ;
  737. tsf32 = le32_to_cpu(stats->tsf32);
  738. if (tsf32 < priv->tsf_low32)
  739. priv->tsf_high32++;
  740. priv->tsf_low32 = tsf32;
  741. priv->stats.dot11RTSFailureCount = le32_to_cpu(stats->rts_fail);
  742. priv->stats.dot11RTSSuccessCount = le32_to_cpu(stats->rts_success);
  743. priv->stats.dot11FCSErrorCount = le32_to_cpu(stats->rx_bad_fcs);
  744. priv->noise = p54_rssi_to_dbm(dev, le32_to_cpu(stats->noise));
  745. p54_free_skb(dev, p54_find_tx_entry(dev, hdr->req_id));
  746. }
  747. static void p54_rx_trap(struct ieee80211_hw *dev, struct sk_buff *skb)
  748. {
  749. struct p54_hdr *hdr = (struct p54_hdr *) skb->data;
  750. struct p54_trap *trap = (struct p54_trap *) hdr->data;
  751. u16 event = le16_to_cpu(trap->event);
  752. u16 freq = le16_to_cpu(trap->frequency);
  753. switch (event) {
  754. case P54_TRAP_BEACON_TX:
  755. break;
  756. case P54_TRAP_RADAR:
  757. printk(KERN_INFO "%s: radar (freq:%d MHz)\n",
  758. wiphy_name(dev->wiphy), freq);
  759. break;
  760. case P54_TRAP_NO_BEACON:
  761. break;
  762. case P54_TRAP_SCAN:
  763. break;
  764. case P54_TRAP_TBTT:
  765. break;
  766. case P54_TRAP_TIMER:
  767. break;
  768. default:
  769. printk(KERN_INFO "%s: received event:%x freq:%d\n",
  770. wiphy_name(dev->wiphy), event, freq);
  771. break;
  772. }
  773. }
  774. static int p54_rx_control(struct ieee80211_hw *dev, struct sk_buff *skb)
  775. {
  776. struct p54_hdr *hdr = (struct p54_hdr *) skb->data;
  777. switch (le16_to_cpu(hdr->type)) {
  778. case P54_CONTROL_TYPE_TXDONE:
  779. p54_rx_frame_sent(dev, skb);
  780. break;
  781. case P54_CONTROL_TYPE_TRAP:
  782. p54_rx_trap(dev, skb);
  783. break;
  784. case P54_CONTROL_TYPE_BBP:
  785. break;
  786. case P54_CONTROL_TYPE_STAT_READBACK:
  787. p54_rx_stats(dev, skb);
  788. break;
  789. case P54_CONTROL_TYPE_EEPROM_READBACK:
  790. p54_rx_eeprom_readback(dev, skb);
  791. break;
  792. default:
  793. printk(KERN_DEBUG "%s: not handling 0x%02x type control frame\n",
  794. wiphy_name(dev->wiphy), le16_to_cpu(hdr->type));
  795. break;
  796. }
  797. return 0;
  798. }
  799. /* returns zero if skb can be reused */
  800. int p54_rx(struct ieee80211_hw *dev, struct sk_buff *skb)
  801. {
  802. u16 type = le16_to_cpu(*((__le16 *)skb->data));
  803. if (type & P54_HDR_FLAG_CONTROL)
  804. return p54_rx_control(dev, skb);
  805. else
  806. return p54_rx_data(dev, skb);
  807. }
  808. EXPORT_SYMBOL_GPL(p54_rx);
  809. /*
  810. * So, the firmware is somewhat stupid and doesn't know what places in its
  811. * memory incoming data should go to. By poking around in the firmware, we
  812. * can find some unused memory to upload our packets to. However, data that we
  813. * want the card to TX needs to stay intact until the card has told us that
  814. * it is done with it. This function finds empty places we can upload to and
  815. * marks allocated areas as reserved if necessary. p54_rx_frame_sent frees
  816. * allocated areas.
  817. */
  818. static int p54_assign_address(struct ieee80211_hw *dev, struct sk_buff *skb,
  819. struct p54_hdr *data, u32 len)
  820. {
  821. struct p54_common *priv = dev->priv;
  822. struct sk_buff *entry = priv->tx_queue.next;
  823. struct sk_buff *target_skb = NULL;
  824. struct ieee80211_tx_info *info;
  825. struct memrecord *range;
  826. u32 last_addr = priv->rx_start;
  827. u32 largest_hole = 0;
  828. u32 target_addr = priv->rx_start;
  829. unsigned long flags;
  830. unsigned int left;
  831. len = (len + priv->headroom + priv->tailroom + 3) & ~0x3;
  832. if (!skb)
  833. return -EINVAL;
  834. spin_lock_irqsave(&priv->tx_queue.lock, flags);
  835. left = skb_queue_len(&priv->tx_queue);
  836. if (unlikely(left >= 28)) {
  837. /*
  838. * The tx_queue is nearly full!
  839. * We have throttle normal data traffic, because we must
  840. * have a few spare slots for control frames left.
  841. */
  842. ieee80211_stop_queues(dev);
  843. queue_delayed_work(dev->workqueue, &priv->work,
  844. msecs_to_jiffies(P54_TX_TIMEOUT));
  845. if (unlikely(left == 32)) {
  846. /*
  847. * The tx_queue is now really full.
  848. *
  849. * TODO: check if the device has crashed and reset it.
  850. */
  851. spin_unlock_irqrestore(&priv->tx_queue.lock, flags);
  852. return -ENOSPC;
  853. }
  854. }
  855. while (left--) {
  856. u32 hole_size;
  857. info = IEEE80211_SKB_CB(entry);
  858. range = (void *)info->rate_driver_data;
  859. hole_size = range->start_addr - last_addr;
  860. if (!target_skb && hole_size >= len) {
  861. target_skb = entry->prev;
  862. hole_size -= len;
  863. target_addr = last_addr;
  864. }
  865. largest_hole = max(largest_hole, hole_size);
  866. last_addr = range->end_addr;
  867. entry = entry->next;
  868. }
  869. if (!target_skb && priv->rx_end - last_addr >= len) {
  870. target_skb = priv->tx_queue.prev;
  871. largest_hole = max(largest_hole, priv->rx_end - last_addr - len);
  872. if (!skb_queue_empty(&priv->tx_queue)) {
  873. info = IEEE80211_SKB_CB(target_skb);
  874. range = (void *)info->rate_driver_data;
  875. target_addr = range->end_addr;
  876. }
  877. } else
  878. largest_hole = max(largest_hole, priv->rx_end - last_addr);
  879. if (!target_skb) {
  880. spin_unlock_irqrestore(&priv->tx_queue.lock, flags);
  881. ieee80211_stop_queues(dev);
  882. return -ENOSPC;
  883. }
  884. info = IEEE80211_SKB_CB(skb);
  885. range = (void *)info->rate_driver_data;
  886. range->start_addr = target_addr;
  887. range->end_addr = target_addr + len;
  888. __skb_queue_after(&priv->tx_queue, target_skb, skb);
  889. spin_unlock_irqrestore(&priv->tx_queue.lock, flags);
  890. if (largest_hole < priv->headroom + sizeof(struct p54_hdr) +
  891. 48 + IEEE80211_MAX_RTS_THRESHOLD + priv->tailroom)
  892. ieee80211_stop_queues(dev);
  893. data->req_id = cpu_to_le32(target_addr + priv->headroom);
  894. return 0;
  895. }
  896. static struct sk_buff *p54_alloc_skb(struct ieee80211_hw *dev,
  897. u16 hdr_flags, u16 len, u16 type, gfp_t memflags)
  898. {
  899. struct p54_common *priv = dev->priv;
  900. struct p54_hdr *hdr;
  901. struct sk_buff *skb;
  902. skb = __dev_alloc_skb(len + priv->tx_hdr_len, memflags);
  903. if (!skb)
  904. return NULL;
  905. skb_reserve(skb, priv->tx_hdr_len);
  906. hdr = (struct p54_hdr *) skb_put(skb, sizeof(*hdr));
  907. hdr->flags = cpu_to_le16(hdr_flags);
  908. hdr->len = cpu_to_le16(len - sizeof(*hdr));
  909. hdr->type = cpu_to_le16(type);
  910. hdr->tries = hdr->rts_tries = 0;
  911. if (unlikely(p54_assign_address(dev, skb, hdr, len))) {
  912. kfree_skb(skb);
  913. return NULL;
  914. }
  915. return skb;
  916. }
  917. int p54_read_eeprom(struct ieee80211_hw *dev)
  918. {
  919. struct p54_common *priv = dev->priv;
  920. struct p54_hdr *hdr = NULL;
  921. struct p54_eeprom_lm86 *eeprom_hdr;
  922. struct sk_buff *skb;
  923. size_t eeprom_size = 0x2020, offset = 0, blocksize, maxblocksize;
  924. int ret = -ENOMEM;
  925. void *eeprom = NULL;
  926. maxblocksize = EEPROM_READBACK_LEN;
  927. if (priv->fw_var >= 0x509)
  928. maxblocksize -= 0xc;
  929. else
  930. maxblocksize -= 0x4;
  931. skb = p54_alloc_skb(dev, P54_HDR_FLAG_CONTROL, sizeof(*hdr) +
  932. sizeof(*eeprom_hdr) + maxblocksize,
  933. P54_CONTROL_TYPE_EEPROM_READBACK, GFP_KERNEL);
  934. if (!skb)
  935. goto free;
  936. priv->eeprom = kzalloc(EEPROM_READBACK_LEN, GFP_KERNEL);
  937. if (!priv->eeprom)
  938. goto free;
  939. eeprom = kzalloc(eeprom_size, GFP_KERNEL);
  940. if (!eeprom)
  941. goto free;
  942. eeprom_hdr = (struct p54_eeprom_lm86 *) skb_put(skb,
  943. sizeof(*eeprom_hdr) + maxblocksize);
  944. while (eeprom_size) {
  945. blocksize = min(eeprom_size, maxblocksize);
  946. if (priv->fw_var < 0x509) {
  947. eeprom_hdr->v1.offset = cpu_to_le16(offset);
  948. eeprom_hdr->v1.len = cpu_to_le16(blocksize);
  949. } else {
  950. eeprom_hdr->v2.offset = cpu_to_le32(offset);
  951. eeprom_hdr->v2.len = cpu_to_le16(blocksize);
  952. eeprom_hdr->v2.magic2 = 0xf;
  953. memcpy(eeprom_hdr->v2.magic, (const char *)"LOCK", 4);
  954. }
  955. priv->tx(dev, skb);
  956. if (!wait_for_completion_interruptible_timeout(&priv->eeprom_comp, HZ)) {
  957. printk(KERN_ERR "%s: device does not respond!\n",
  958. wiphy_name(dev->wiphy));
  959. ret = -EBUSY;
  960. goto free;
  961. }
  962. memcpy(eeprom + offset, priv->eeprom, blocksize);
  963. offset += blocksize;
  964. eeprom_size -= blocksize;
  965. }
  966. ret = p54_parse_eeprom(dev, eeprom, offset);
  967. free:
  968. kfree(priv->eeprom);
  969. priv->eeprom = NULL;
  970. p54_free_skb(dev, skb);
  971. kfree(eeprom);
  972. return ret;
  973. }
  974. EXPORT_SYMBOL_GPL(p54_read_eeprom);
  975. static int p54_set_tim(struct ieee80211_hw *dev, struct ieee80211_sta *sta,
  976. bool set)
  977. {
  978. struct p54_common *priv = dev->priv;
  979. struct sk_buff *skb;
  980. struct p54_tim *tim;
  981. skb = p54_alloc_skb(dev, P54_HDR_FLAG_CONTROL_OPSET,
  982. sizeof(struct p54_hdr) + sizeof(*tim),
  983. P54_CONTROL_TYPE_TIM, GFP_KERNEL);
  984. if (!skb)
  985. return -ENOMEM;
  986. tim = (struct p54_tim *) skb_put(skb, sizeof(*tim));
  987. tim->count = 1;
  988. tim->entry[0] = cpu_to_le16(set ? (sta->aid | 0x8000) : sta->aid);
  989. priv->tx(dev, skb);
  990. return 0;
  991. }
  992. static int p54_sta_unlock(struct ieee80211_hw *dev, u8 *addr)
  993. {
  994. struct p54_common *priv = dev->priv;
  995. struct sk_buff *skb;
  996. struct p54_sta_unlock *sta;
  997. skb = p54_alloc_skb(dev, P54_HDR_FLAG_CONTROL_OPSET,
  998. sizeof(struct p54_hdr) + sizeof(*sta),
  999. P54_CONTROL_TYPE_PSM_STA_UNLOCK, GFP_ATOMIC);
  1000. if (!skb)
  1001. return -ENOMEM;
  1002. sta = (struct p54_sta_unlock *)skb_put(skb, sizeof(*sta));
  1003. memcpy(sta->addr, addr, ETH_ALEN);
  1004. priv->tx(dev, skb);
  1005. return 0;
  1006. }
  1007. static void p54_sta_notify(struct ieee80211_hw *dev, struct ieee80211_vif *vif,
  1008. enum sta_notify_cmd notify_cmd,
  1009. struct ieee80211_sta *sta)
  1010. {
  1011. switch (notify_cmd) {
  1012. case STA_NOTIFY_ADD:
  1013. case STA_NOTIFY_REMOVE:
  1014. /*
  1015. * Notify the firmware that we don't want or we don't
  1016. * need to buffer frames for this station anymore.
  1017. */
  1018. p54_sta_unlock(dev, sta->addr);
  1019. break;
  1020. case STA_NOTIFY_AWAKE:
  1021. /* update the firmware's filter table */
  1022. p54_sta_unlock(dev, sta->addr);
  1023. break;
  1024. default:
  1025. break;
  1026. }
  1027. }
  1028. static int p54_tx_cancel(struct ieee80211_hw *dev, struct sk_buff *entry)
  1029. {
  1030. struct p54_common *priv = dev->priv;
  1031. struct sk_buff *skb;
  1032. struct p54_hdr *hdr;
  1033. struct p54_txcancel *cancel;
  1034. skb = p54_alloc_skb(dev, P54_HDR_FLAG_CONTROL_OPSET,
  1035. sizeof(struct p54_hdr) + sizeof(*cancel),
  1036. P54_CONTROL_TYPE_TXCANCEL, GFP_ATOMIC);
  1037. if (!skb)
  1038. return -ENOMEM;
  1039. hdr = (void *)entry->data;
  1040. cancel = (struct p54_txcancel *)skb_put(skb, sizeof(*cancel));
  1041. cancel->req_id = hdr->req_id;
  1042. priv->tx(dev, skb);
  1043. return 0;
  1044. }
  1045. static int p54_tx_fill(struct ieee80211_hw *dev, struct sk_buff *skb,
  1046. struct ieee80211_tx_info *info, u8 *queue, size_t *extra_len,
  1047. u16 *flags, u16 *aid)
  1048. {
  1049. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  1050. struct p54_common *priv = dev->priv;
  1051. int ret = 0;
  1052. if (unlikely(ieee80211_is_mgmt(hdr->frame_control))) {
  1053. if (ieee80211_is_beacon(hdr->frame_control)) {
  1054. *aid = 0;
  1055. *queue = 0;
  1056. *extra_len = IEEE80211_MAX_TIM_LEN;
  1057. *flags = P54_HDR_FLAG_DATA_OUT_TIMESTAMP;
  1058. return 0;
  1059. } else if (ieee80211_is_probe_resp(hdr->frame_control)) {
  1060. *aid = 0;
  1061. *queue = 2;
  1062. *flags = P54_HDR_FLAG_DATA_OUT_TIMESTAMP |
  1063. P54_HDR_FLAG_DATA_OUT_NOCANCEL;
  1064. return 0;
  1065. } else {
  1066. *queue = 2;
  1067. ret = 0;
  1068. }
  1069. } else {
  1070. *queue += 4;
  1071. ret = 1;
  1072. }
  1073. switch (priv->mode) {
  1074. case NL80211_IFTYPE_STATION:
  1075. *aid = 1;
  1076. break;
  1077. case NL80211_IFTYPE_AP:
  1078. case NL80211_IFTYPE_ADHOC:
  1079. case NL80211_IFTYPE_MESH_POINT:
  1080. if (info->flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM) {
  1081. *aid = 0;
  1082. *queue = 3;
  1083. return 0;
  1084. }
  1085. if (info->control.sta)
  1086. *aid = info->control.sta->aid;
  1087. else
  1088. *flags |= P54_HDR_FLAG_DATA_OUT_NOCANCEL;
  1089. }
  1090. return ret;
  1091. }
  1092. static u8 p54_convert_algo(enum ieee80211_key_alg alg)
  1093. {
  1094. switch (alg) {
  1095. case ALG_WEP:
  1096. return P54_CRYPTO_WEP;
  1097. case ALG_TKIP:
  1098. return P54_CRYPTO_TKIPMICHAEL;
  1099. case ALG_CCMP:
  1100. return P54_CRYPTO_AESCCMP;
  1101. default:
  1102. return 0;
  1103. }
  1104. }
  1105. static int p54_tx(struct ieee80211_hw *dev, struct sk_buff *skb)
  1106. {
  1107. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  1108. struct ieee80211_tx_queue_stats *current_queue = NULL;
  1109. struct p54_common *priv = dev->priv;
  1110. struct p54_hdr *hdr;
  1111. struct p54_tx_data *txhdr;
  1112. size_t padding, len, tim_len = 0;
  1113. int i, j, ridx, ret;
  1114. u16 hdr_flags = 0, aid = 0;
  1115. u8 rate, queue, crypt_offset = 0;
  1116. u8 cts_rate = 0x20;
  1117. u8 rc_flags;
  1118. u8 calculated_tries[4];
  1119. u8 nrates = 0, nremaining = 8;
  1120. queue = skb_get_queue_mapping(skb);
  1121. ret = p54_tx_fill(dev, skb, info, &queue, &tim_len, &hdr_flags, &aid);
  1122. current_queue = &priv->tx_stats[queue];
  1123. if (unlikely((current_queue->len > current_queue->limit) && ret))
  1124. return NETDEV_TX_BUSY;
  1125. current_queue->len++;
  1126. current_queue->count++;
  1127. if ((current_queue->len == current_queue->limit) && ret)
  1128. ieee80211_stop_queue(dev, skb_get_queue_mapping(skb));
  1129. padding = (unsigned long)(skb->data - (sizeof(*hdr) + sizeof(*txhdr))) & 3;
  1130. len = skb->len;
  1131. if (info->control.hw_key) {
  1132. crypt_offset = ieee80211_get_hdrlen_from_skb(skb);
  1133. if (info->control.hw_key->alg == ALG_TKIP) {
  1134. u8 *iv = (u8 *)(skb->data + crypt_offset);
  1135. /*
  1136. * The firmware excepts that the IV has to have
  1137. * this special format
  1138. */
  1139. iv[1] = iv[0];
  1140. iv[0] = iv[2];
  1141. iv[2] = 0;
  1142. }
  1143. }
  1144. txhdr = (struct p54_tx_data *) skb_push(skb, sizeof(*txhdr) + padding);
  1145. hdr = (struct p54_hdr *) skb_push(skb, sizeof(*hdr));
  1146. if (padding)
  1147. hdr_flags |= P54_HDR_FLAG_DATA_ALIGN;
  1148. hdr->type = cpu_to_le16(aid);
  1149. hdr->rts_tries = info->control.rates[0].count;
  1150. /*
  1151. * we register the rates in perfect order, and
  1152. * RTS/CTS won't happen on 5 GHz
  1153. */
  1154. cts_rate = info->control.rts_cts_rate_idx;
  1155. memset(&txhdr->rateset, 0, sizeof(txhdr->rateset));
  1156. /* see how many rates got used */
  1157. for (i = 0; i < 4; i++) {
  1158. if (info->control.rates[i].idx < 0)
  1159. break;
  1160. nrates++;
  1161. }
  1162. /* limit tries to 8/nrates per rate */
  1163. for (i = 0; i < nrates; i++) {
  1164. /*
  1165. * The magic expression here is equivalent to 8/nrates for
  1166. * all values that matter, but avoids division and jumps.
  1167. * Note that nrates can only take the values 1 through 4.
  1168. */
  1169. calculated_tries[i] = min_t(int, ((15 >> nrates) | 1) + 1,
  1170. info->control.rates[i].count);
  1171. nremaining -= calculated_tries[i];
  1172. }
  1173. /* if there are tries left, distribute from back to front */
  1174. for (i = nrates - 1; nremaining > 0 && i >= 0; i--) {
  1175. int tmp = info->control.rates[i].count - calculated_tries[i];
  1176. if (tmp <= 0)
  1177. continue;
  1178. /* RC requested more tries at this rate */
  1179. tmp = min_t(int, tmp, nremaining);
  1180. calculated_tries[i] += tmp;
  1181. nremaining -= tmp;
  1182. }
  1183. ridx = 0;
  1184. for (i = 0; i < nrates && ridx < 8; i++) {
  1185. /* we register the rates in perfect order */
  1186. rate = info->control.rates[i].idx;
  1187. if (info->band == IEEE80211_BAND_5GHZ)
  1188. rate += 4;
  1189. /* store the count we actually calculated for TX status */
  1190. info->control.rates[i].count = calculated_tries[i];
  1191. rc_flags = info->control.rates[i].flags;
  1192. if (rc_flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE) {
  1193. rate |= 0x10;
  1194. cts_rate |= 0x10;
  1195. }
  1196. if (rc_flags & IEEE80211_TX_RC_USE_RTS_CTS)
  1197. rate |= 0x40;
  1198. else if (rc_flags & IEEE80211_TX_RC_USE_CTS_PROTECT)
  1199. rate |= 0x20;
  1200. for (j = 0; j < calculated_tries[i] && ridx < 8; j++) {
  1201. txhdr->rateset[ridx] = rate;
  1202. ridx++;
  1203. }
  1204. }
  1205. if (info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ)
  1206. hdr_flags |= P54_HDR_FLAG_DATA_OUT_SEQNR;
  1207. /* TODO: enable bursting */
  1208. hdr->flags = cpu_to_le16(hdr_flags);
  1209. hdr->tries = ridx;
  1210. txhdr->rts_rate_idx = 0;
  1211. if (info->control.hw_key) {
  1212. crypt_offset += info->control.hw_key->iv_len;
  1213. txhdr->key_type = p54_convert_algo(info->control.hw_key->alg);
  1214. txhdr->key_len = min((u8)16, info->control.hw_key->keylen);
  1215. memcpy(txhdr->key, info->control.hw_key->key, txhdr->key_len);
  1216. if (info->control.hw_key->alg == ALG_TKIP) {
  1217. if (unlikely(skb_tailroom(skb) < 12))
  1218. goto err;
  1219. /* reserve space for the MIC key */
  1220. len += 8;
  1221. memcpy(skb_put(skb, 8), &(info->control.hw_key->key
  1222. [NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY]), 8);
  1223. }
  1224. /* reserve some space for ICV */
  1225. len += info->control.hw_key->icv_len;
  1226. } else {
  1227. txhdr->key_type = 0;
  1228. txhdr->key_len = 0;
  1229. }
  1230. txhdr->crypt_offset = crypt_offset;
  1231. txhdr->hw_queue = queue;
  1232. if (current_queue)
  1233. txhdr->backlog = current_queue->len;
  1234. else
  1235. txhdr->backlog = 0;
  1236. memset(txhdr->durations, 0, sizeof(txhdr->durations));
  1237. txhdr->tx_antenna = (info->antenna_sel_tx == 0) ?
  1238. 2 : info->antenna_sel_tx - 1;
  1239. txhdr->output_power = priv->output_power;
  1240. txhdr->cts_rate = cts_rate;
  1241. if (padding)
  1242. txhdr->align[0] = padding;
  1243. hdr->len = cpu_to_le16(len);
  1244. /* modifies skb->cb and with it info, so must be last! */
  1245. if (unlikely(p54_assign_address(dev, skb, hdr, skb->len + tim_len)))
  1246. goto err;
  1247. priv->tx(dev, skb);
  1248. queue_delayed_work(dev->workqueue, &priv->work,
  1249. msecs_to_jiffies(P54_TX_FRAME_LIFETIME));
  1250. return 0;
  1251. err:
  1252. skb_pull(skb, sizeof(*hdr) + sizeof(*txhdr) + padding);
  1253. if (current_queue) {
  1254. current_queue->len--;
  1255. current_queue->count--;
  1256. }
  1257. return NETDEV_TX_BUSY;
  1258. }
  1259. static int p54_setup_mac(struct ieee80211_hw *dev)
  1260. {
  1261. struct p54_common *priv = dev->priv;
  1262. struct sk_buff *skb;
  1263. struct p54_setup_mac *setup;
  1264. u16 mode;
  1265. skb = p54_alloc_skb(dev, P54_HDR_FLAG_CONTROL_OPSET, sizeof(*setup) +
  1266. sizeof(struct p54_hdr), P54_CONTROL_TYPE_SETUP,
  1267. GFP_ATOMIC);
  1268. if (!skb)
  1269. return -ENOMEM;
  1270. setup = (struct p54_setup_mac *) skb_put(skb, sizeof(*setup));
  1271. if (dev->conf.radio_enabled) {
  1272. switch (priv->mode) {
  1273. case NL80211_IFTYPE_STATION:
  1274. mode = P54_FILTER_TYPE_STATION;
  1275. break;
  1276. case NL80211_IFTYPE_AP:
  1277. mode = P54_FILTER_TYPE_AP;
  1278. break;
  1279. case NL80211_IFTYPE_ADHOC:
  1280. case NL80211_IFTYPE_MESH_POINT:
  1281. mode = P54_FILTER_TYPE_IBSS;
  1282. break;
  1283. default:
  1284. mode = P54_FILTER_TYPE_NONE;
  1285. break;
  1286. }
  1287. if (priv->filter_flags & FIF_PROMISC_IN_BSS)
  1288. mode |= P54_FILTER_TYPE_TRANSPARENT;
  1289. } else
  1290. mode = P54_FILTER_TYPE_RX_DISABLED;
  1291. setup->mac_mode = cpu_to_le16(mode);
  1292. memcpy(setup->mac_addr, priv->mac_addr, ETH_ALEN);
  1293. memcpy(setup->bssid, priv->bssid, ETH_ALEN);
  1294. setup->rx_antenna = 2; /* automatic */
  1295. setup->rx_align = 0;
  1296. if (priv->fw_var < 0x500) {
  1297. setup->v1.basic_rate_mask = cpu_to_le32(priv->basic_rate_mask);
  1298. memset(setup->v1.rts_rates, 0, 8);
  1299. setup->v1.rx_addr = cpu_to_le32(priv->rx_end);
  1300. setup->v1.max_rx = cpu_to_le16(priv->rx_mtu);
  1301. setup->v1.rxhw = cpu_to_le16(priv->rxhw);
  1302. setup->v1.wakeup_timer = cpu_to_le16(priv->wakeup_timer);
  1303. setup->v1.unalloc0 = cpu_to_le16(0);
  1304. } else {
  1305. setup->v2.rx_addr = cpu_to_le32(priv->rx_end);
  1306. setup->v2.max_rx = cpu_to_le16(priv->rx_mtu);
  1307. setup->v2.rxhw = cpu_to_le16(priv->rxhw);
  1308. setup->v2.timer = cpu_to_le16(priv->wakeup_timer);
  1309. setup->v2.truncate = cpu_to_le16(48896);
  1310. setup->v2.basic_rate_mask = cpu_to_le32(priv->basic_rate_mask);
  1311. setup->v2.sbss_offset = 0;
  1312. setup->v2.mcast_window = 0;
  1313. setup->v2.rx_rssi_threshold = 0;
  1314. setup->v2.rx_ed_threshold = 0;
  1315. setup->v2.ref_clock = cpu_to_le32(644245094);
  1316. setup->v2.lpf_bandwidth = cpu_to_le16(65535);
  1317. setup->v2.osc_start_delay = cpu_to_le16(65535);
  1318. }
  1319. priv->tx(dev, skb);
  1320. return 0;
  1321. }
  1322. static int p54_scan(struct ieee80211_hw *dev, u16 mode, u16 dwell)
  1323. {
  1324. struct p54_common *priv = dev->priv;
  1325. struct sk_buff *skb;
  1326. struct p54_scan *chan;
  1327. unsigned int i;
  1328. void *entry;
  1329. __le16 freq = cpu_to_le16(dev->conf.channel->center_freq);
  1330. int band = dev->conf.channel->band;
  1331. skb = p54_alloc_skb(dev, P54_HDR_FLAG_CONTROL_OPSET, sizeof(*chan) +
  1332. sizeof(struct p54_hdr), P54_CONTROL_TYPE_SCAN,
  1333. GFP_ATOMIC);
  1334. if (!skb)
  1335. return -ENOMEM;
  1336. chan = (struct p54_scan *) skb_put(skb, sizeof(*chan));
  1337. memset(chan->padding1, 0, sizeof(chan->padding1));
  1338. chan->mode = cpu_to_le16(mode);
  1339. chan->dwell = cpu_to_le16(dwell);
  1340. for (i = 0; i < priv->iq_autocal_len; i++) {
  1341. if (priv->iq_autocal[i].freq != freq)
  1342. continue;
  1343. memcpy(&chan->iq_autocal, &priv->iq_autocal[i],
  1344. sizeof(*priv->iq_autocal));
  1345. break;
  1346. }
  1347. if (i == priv->iq_autocal_len)
  1348. goto err;
  1349. for (i = 0; i < priv->output_limit_len; i++) {
  1350. if (priv->output_limit[i].freq != freq)
  1351. continue;
  1352. chan->val_barker = 0x38;
  1353. chan->val_bpsk = chan->dup_bpsk =
  1354. priv->output_limit[i].val_bpsk;
  1355. chan->val_qpsk = chan->dup_qpsk =
  1356. priv->output_limit[i].val_qpsk;
  1357. chan->val_16qam = chan->dup_16qam =
  1358. priv->output_limit[i].val_16qam;
  1359. chan->val_64qam = chan->dup_64qam =
  1360. priv->output_limit[i].val_64qam;
  1361. break;
  1362. }
  1363. if (i == priv->output_limit_len)
  1364. goto err;
  1365. entry = priv->curve_data->data;
  1366. for (i = 0; i < priv->curve_data->channels; i++) {
  1367. if (*((__le16 *)entry) != freq) {
  1368. entry += sizeof(__le16);
  1369. entry += sizeof(struct p54_pa_curve_data_sample) *
  1370. priv->curve_data->points_per_channel;
  1371. continue;
  1372. }
  1373. entry += sizeof(__le16);
  1374. chan->pa_points_per_curve = 8;
  1375. memset(chan->curve_data, 0, sizeof(*chan->curve_data));
  1376. memcpy(chan->curve_data, entry,
  1377. sizeof(struct p54_pa_curve_data_sample) *
  1378. min((u8)8, priv->curve_data->points_per_channel));
  1379. break;
  1380. }
  1381. if (priv->fw_var < 0x500) {
  1382. chan->v1_rssi.mul = cpu_to_le16(priv->rssical_db[band].mul);
  1383. chan->v1_rssi.add = cpu_to_le16(priv->rssical_db[band].add);
  1384. } else {
  1385. chan->v2.rssi.mul = cpu_to_le16(priv->rssical_db[band].mul);
  1386. chan->v2.rssi.add = cpu_to_le16(priv->rssical_db[band].add);
  1387. chan->v2.basic_rate_mask = cpu_to_le32(priv->basic_rate_mask);
  1388. memset(chan->v2.rts_rates, 0, 8);
  1389. }
  1390. priv->tx(dev, skb);
  1391. return 0;
  1392. err:
  1393. printk(KERN_ERR "%s: frequency change failed\n", wiphy_name(dev->wiphy));
  1394. kfree_skb(skb);
  1395. return -EINVAL;
  1396. }
  1397. static int p54_set_leds(struct ieee80211_hw *dev, int mode, int link, int act)
  1398. {
  1399. struct p54_common *priv = dev->priv;
  1400. struct sk_buff *skb;
  1401. struct p54_led *led;
  1402. skb = p54_alloc_skb(dev, P54_HDR_FLAG_CONTROL_OPSET, sizeof(*led) +
  1403. sizeof(struct p54_hdr), P54_CONTROL_TYPE_LED,
  1404. GFP_ATOMIC);
  1405. if (!skb)
  1406. return -ENOMEM;
  1407. led = (struct p54_led *)skb_put(skb, sizeof(*led));
  1408. led->mode = cpu_to_le16(mode);
  1409. led->led_permanent = cpu_to_le16(link);
  1410. led->led_temporary = cpu_to_le16(act);
  1411. led->duration = cpu_to_le16(1000);
  1412. priv->tx(dev, skb);
  1413. return 0;
  1414. }
  1415. #define P54_SET_QUEUE(queue, ai_fs, cw_min, cw_max, _txop) \
  1416. do { \
  1417. queue.aifs = cpu_to_le16(ai_fs); \
  1418. queue.cwmin = cpu_to_le16(cw_min); \
  1419. queue.cwmax = cpu_to_le16(cw_max); \
  1420. queue.txop = cpu_to_le16(_txop); \
  1421. } while(0)
  1422. static int p54_set_edcf(struct ieee80211_hw *dev)
  1423. {
  1424. struct p54_common *priv = dev->priv;
  1425. struct sk_buff *skb;
  1426. struct p54_edcf *edcf;
  1427. skb = p54_alloc_skb(dev, P54_HDR_FLAG_CONTROL_OPSET, sizeof(*edcf) +
  1428. sizeof(struct p54_hdr), P54_CONTROL_TYPE_DCFINIT,
  1429. GFP_ATOMIC);
  1430. if (!skb)
  1431. return -ENOMEM;
  1432. edcf = (struct p54_edcf *)skb_put(skb, sizeof(*edcf));
  1433. if (priv->use_short_slot) {
  1434. edcf->slottime = 9;
  1435. edcf->sifs = 0x10;
  1436. edcf->eofpad = 0x00;
  1437. } else {
  1438. edcf->slottime = 20;
  1439. edcf->sifs = 0x0a;
  1440. edcf->eofpad = 0x06;
  1441. }
  1442. /* (see prism54/isl_oid.h for further details) */
  1443. edcf->frameburst = cpu_to_le16(0);
  1444. edcf->round_trip_delay = cpu_to_le16(0);
  1445. edcf->flags = 0;
  1446. memset(edcf->mapping, 0, sizeof(edcf->mapping));
  1447. memcpy(edcf->queue, priv->qos_params, sizeof(edcf->queue));
  1448. priv->tx(dev, skb);
  1449. return 0;
  1450. }
  1451. static int p54_beacon_tim(struct sk_buff *skb)
  1452. {
  1453. /*
  1454. * the good excuse for this mess is ... the firmware.
  1455. * The dummy TIM MUST be at the end of the beacon frame,
  1456. * because it'll be overwritten!
  1457. */
  1458. struct ieee80211_mgmt *mgmt = (void *)skb->data;
  1459. u8 *pos, *end;
  1460. if (skb->len <= sizeof(mgmt))
  1461. return -EINVAL;
  1462. pos = (u8 *)mgmt->u.beacon.variable;
  1463. end = skb->data + skb->len;
  1464. while (pos < end) {
  1465. if (pos + 2 + pos[1] > end)
  1466. return -EINVAL;
  1467. if (pos[0] == WLAN_EID_TIM) {
  1468. u8 dtim_len = pos[1];
  1469. u8 dtim_period = pos[3];
  1470. u8 *next = pos + 2 + dtim_len;
  1471. if (dtim_len < 3)
  1472. return -EINVAL;
  1473. memmove(pos, next, end - next);
  1474. if (dtim_len > 3)
  1475. skb_trim(skb, skb->len - (dtim_len - 3));
  1476. pos = end - (dtim_len + 2);
  1477. /* add the dummy at the end */
  1478. pos[0] = WLAN_EID_TIM;
  1479. pos[1] = 3;
  1480. pos[2] = 0;
  1481. pos[3] = dtim_period;
  1482. pos[4] = 0;
  1483. return 0;
  1484. }
  1485. pos += 2 + pos[1];
  1486. }
  1487. return 0;
  1488. }
  1489. static int p54_beacon_update(struct ieee80211_hw *dev,
  1490. struct ieee80211_vif *vif)
  1491. {
  1492. struct p54_common *priv = dev->priv;
  1493. struct sk_buff *beacon;
  1494. int ret;
  1495. if (priv->cached_beacon) {
  1496. p54_tx_cancel(dev, priv->cached_beacon);
  1497. /* wait for the last beacon the be freed */
  1498. msleep(10);
  1499. }
  1500. beacon = ieee80211_beacon_get(dev, vif);
  1501. if (!beacon)
  1502. return -ENOMEM;
  1503. ret = p54_beacon_tim(beacon);
  1504. if (ret)
  1505. return ret;
  1506. ret = p54_tx(dev, beacon);
  1507. if (ret)
  1508. return ret;
  1509. priv->cached_beacon = beacon;
  1510. priv->tsf_high32 = 0;
  1511. priv->tsf_low32 = 0;
  1512. return 0;
  1513. }
  1514. static int p54_start(struct ieee80211_hw *dev)
  1515. {
  1516. struct p54_common *priv = dev->priv;
  1517. int err;
  1518. mutex_lock(&priv->conf_mutex);
  1519. err = priv->open(dev);
  1520. if (err)
  1521. goto out;
  1522. P54_SET_QUEUE(priv->qos_params[0], 0x0002, 0x0003, 0x0007, 47);
  1523. P54_SET_QUEUE(priv->qos_params[1], 0x0002, 0x0007, 0x000f, 94);
  1524. P54_SET_QUEUE(priv->qos_params[2], 0x0003, 0x000f, 0x03ff, 0);
  1525. P54_SET_QUEUE(priv->qos_params[3], 0x0007, 0x000f, 0x03ff, 0);
  1526. err = p54_set_edcf(dev);
  1527. if (err)
  1528. goto out;
  1529. memset(priv->bssid, ~0, ETH_ALEN);
  1530. priv->mode = NL80211_IFTYPE_MONITOR;
  1531. err = p54_setup_mac(dev);
  1532. if (err) {
  1533. priv->mode = NL80211_IFTYPE_UNSPECIFIED;
  1534. goto out;
  1535. }
  1536. queue_delayed_work(dev->workqueue, &priv->work, 0);
  1537. out:
  1538. mutex_unlock(&priv->conf_mutex);
  1539. return err;
  1540. }
  1541. static void p54_stop(struct ieee80211_hw *dev)
  1542. {
  1543. struct p54_common *priv = dev->priv;
  1544. struct sk_buff *skb;
  1545. mutex_lock(&priv->conf_mutex);
  1546. priv->mode = NL80211_IFTYPE_UNSPECIFIED;
  1547. cancel_delayed_work_sync(&priv->work);
  1548. if (priv->cached_beacon)
  1549. p54_tx_cancel(dev, priv->cached_beacon);
  1550. priv->stop(dev);
  1551. while ((skb = skb_dequeue(&priv->tx_queue)))
  1552. kfree_skb(skb);
  1553. priv->cached_beacon = NULL;
  1554. priv->tsf_high32 = priv->tsf_low32 = 0;
  1555. mutex_unlock(&priv->conf_mutex);
  1556. }
  1557. static int p54_add_interface(struct ieee80211_hw *dev,
  1558. struct ieee80211_if_init_conf *conf)
  1559. {
  1560. struct p54_common *priv = dev->priv;
  1561. mutex_lock(&priv->conf_mutex);
  1562. if (priv->mode != NL80211_IFTYPE_MONITOR) {
  1563. mutex_unlock(&priv->conf_mutex);
  1564. return -EOPNOTSUPP;
  1565. }
  1566. switch (conf->type) {
  1567. case NL80211_IFTYPE_STATION:
  1568. case NL80211_IFTYPE_ADHOC:
  1569. case NL80211_IFTYPE_AP:
  1570. case NL80211_IFTYPE_MESH_POINT:
  1571. priv->mode = conf->type;
  1572. break;
  1573. default:
  1574. mutex_unlock(&priv->conf_mutex);
  1575. return -EOPNOTSUPP;
  1576. }
  1577. memcpy(priv->mac_addr, conf->mac_addr, ETH_ALEN);
  1578. p54_setup_mac(dev);
  1579. p54_set_leds(dev, 1, 0, 0);
  1580. mutex_unlock(&priv->conf_mutex);
  1581. return 0;
  1582. }
  1583. static void p54_remove_interface(struct ieee80211_hw *dev,
  1584. struct ieee80211_if_init_conf *conf)
  1585. {
  1586. struct p54_common *priv = dev->priv;
  1587. mutex_lock(&priv->conf_mutex);
  1588. if (priv->cached_beacon)
  1589. p54_tx_cancel(dev, priv->cached_beacon);
  1590. priv->mode = NL80211_IFTYPE_MONITOR;
  1591. memset(priv->mac_addr, 0, ETH_ALEN);
  1592. memset(priv->bssid, 0, ETH_ALEN);
  1593. p54_setup_mac(dev);
  1594. mutex_unlock(&priv->conf_mutex);
  1595. }
  1596. static int p54_config(struct ieee80211_hw *dev, u32 changed)
  1597. {
  1598. int ret;
  1599. struct p54_common *priv = dev->priv;
  1600. struct ieee80211_conf *conf = &dev->conf;
  1601. mutex_lock(&priv->conf_mutex);
  1602. if (changed & IEEE80211_CONF_CHANGE_POWER)
  1603. priv->output_power = conf->power_level << 2;
  1604. if (changed & IEEE80211_CONF_CHANGE_RADIO_ENABLED) {
  1605. ret = p54_setup_mac(dev);
  1606. if (ret)
  1607. goto out;
  1608. }
  1609. if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
  1610. ret = p54_scan(dev, P54_SCAN_EXIT, 0);
  1611. if (ret)
  1612. goto out;
  1613. }
  1614. out:
  1615. mutex_unlock(&priv->conf_mutex);
  1616. return ret;
  1617. }
  1618. static int p54_config_interface(struct ieee80211_hw *dev,
  1619. struct ieee80211_vif *vif,
  1620. struct ieee80211_if_conf *conf)
  1621. {
  1622. struct p54_common *priv = dev->priv;
  1623. int ret = 0;
  1624. mutex_lock(&priv->conf_mutex);
  1625. if (conf->changed & IEEE80211_IFCC_BSSID) {
  1626. memcpy(priv->bssid, conf->bssid, ETH_ALEN);
  1627. ret = p54_setup_mac(dev);
  1628. if (ret)
  1629. goto out;
  1630. }
  1631. if (conf->changed & IEEE80211_IFCC_BEACON) {
  1632. ret = p54_scan(dev, P54_SCAN_EXIT, 0);
  1633. if (ret)
  1634. goto out;
  1635. ret = p54_setup_mac(dev);
  1636. if (ret)
  1637. goto out;
  1638. ret = p54_beacon_update(dev, vif);
  1639. if (ret)
  1640. goto out;
  1641. ret = p54_set_edcf(dev);
  1642. if (ret)
  1643. goto out;
  1644. }
  1645. ret = p54_set_leds(dev, 1, !is_multicast_ether_addr(priv->bssid), 0);
  1646. out:
  1647. mutex_unlock(&priv->conf_mutex);
  1648. return ret;
  1649. }
  1650. static void p54_configure_filter(struct ieee80211_hw *dev,
  1651. unsigned int changed_flags,
  1652. unsigned int *total_flags,
  1653. int mc_count, struct dev_mc_list *mclist)
  1654. {
  1655. struct p54_common *priv = dev->priv;
  1656. *total_flags &= FIF_PROMISC_IN_BSS |
  1657. (*total_flags & FIF_PROMISC_IN_BSS) ?
  1658. FIF_FCSFAIL : 0;
  1659. priv->filter_flags = *total_flags;
  1660. if (changed_flags & FIF_PROMISC_IN_BSS)
  1661. p54_setup_mac(dev);
  1662. }
  1663. static int p54_conf_tx(struct ieee80211_hw *dev, u16 queue,
  1664. const struct ieee80211_tx_queue_params *params)
  1665. {
  1666. struct p54_common *priv = dev->priv;
  1667. int ret;
  1668. mutex_lock(&priv->conf_mutex);
  1669. if ((params) && !(queue > 4)) {
  1670. P54_SET_QUEUE(priv->qos_params[queue], params->aifs,
  1671. params->cw_min, params->cw_max, params->txop);
  1672. ret = p54_set_edcf(dev);
  1673. } else
  1674. ret = -EINVAL;
  1675. mutex_unlock(&priv->conf_mutex);
  1676. return ret;
  1677. }
  1678. static int p54_init_xbow_synth(struct ieee80211_hw *dev)
  1679. {
  1680. struct p54_common *priv = dev->priv;
  1681. struct sk_buff *skb;
  1682. struct p54_xbow_synth *xbow;
  1683. skb = p54_alloc_skb(dev, P54_HDR_FLAG_CONTROL_OPSET, sizeof(*xbow) +
  1684. sizeof(struct p54_hdr),
  1685. P54_CONTROL_TYPE_XBOW_SYNTH_CFG,
  1686. GFP_KERNEL);
  1687. if (!skb)
  1688. return -ENOMEM;
  1689. xbow = (struct p54_xbow_synth *)skb_put(skb, sizeof(*xbow));
  1690. xbow->magic1 = cpu_to_le16(0x1);
  1691. xbow->magic2 = cpu_to_le16(0x2);
  1692. xbow->freq = cpu_to_le16(5390);
  1693. memset(xbow->padding, 0, sizeof(xbow->padding));
  1694. priv->tx(dev, skb);
  1695. return 0;
  1696. }
  1697. static void p54_work(struct work_struct *work)
  1698. {
  1699. struct p54_common *priv = container_of(work, struct p54_common,
  1700. work.work);
  1701. struct ieee80211_hw *dev = priv->hw;
  1702. struct sk_buff *skb;
  1703. if (unlikely(priv->mode == NL80211_IFTYPE_UNSPECIFIED))
  1704. return ;
  1705. /*
  1706. * TODO: walk through tx_queue and do the following tasks
  1707. * 1. initiate bursts.
  1708. * 2. cancel stuck frames / reset the device if necessary.
  1709. */
  1710. skb = p54_alloc_skb(dev, P54_HDR_FLAG_CONTROL, sizeof(struct p54_hdr) +
  1711. sizeof(struct p54_statistics),
  1712. P54_CONTROL_TYPE_STAT_READBACK, GFP_KERNEL);
  1713. if (!skb)
  1714. return ;
  1715. priv->tx(dev, skb);
  1716. }
  1717. static int p54_get_stats(struct ieee80211_hw *dev,
  1718. struct ieee80211_low_level_stats *stats)
  1719. {
  1720. struct p54_common *priv = dev->priv;
  1721. memcpy(stats, &priv->stats, sizeof(*stats));
  1722. return 0;
  1723. }
  1724. static int p54_get_tx_stats(struct ieee80211_hw *dev,
  1725. struct ieee80211_tx_queue_stats *stats)
  1726. {
  1727. struct p54_common *priv = dev->priv;
  1728. memcpy(stats, &priv->tx_stats[4], sizeof(stats[0]) * dev->queues);
  1729. return 0;
  1730. }
  1731. static void p54_bss_info_changed(struct ieee80211_hw *dev,
  1732. struct ieee80211_vif *vif,
  1733. struct ieee80211_bss_conf *info,
  1734. u32 changed)
  1735. {
  1736. struct p54_common *priv = dev->priv;
  1737. if (changed & BSS_CHANGED_ERP_SLOT) {
  1738. priv->use_short_slot = info->use_short_slot;
  1739. p54_set_edcf(dev);
  1740. }
  1741. if (changed & BSS_CHANGED_BASIC_RATES) {
  1742. if (dev->conf.channel->band == IEEE80211_BAND_5GHZ)
  1743. priv->basic_rate_mask = (info->basic_rates << 4);
  1744. else
  1745. priv->basic_rate_mask = info->basic_rates;
  1746. p54_setup_mac(dev);
  1747. if (priv->fw_var >= 0x500)
  1748. p54_scan(dev, P54_SCAN_EXIT, 0);
  1749. }
  1750. if (changed & BSS_CHANGED_ASSOC) {
  1751. if (info->assoc) {
  1752. priv->aid = info->aid;
  1753. priv->wakeup_timer = info->beacon_int *
  1754. info->dtim_period * 5;
  1755. p54_setup_mac(dev);
  1756. }
  1757. }
  1758. }
  1759. static int p54_set_key(struct ieee80211_hw *dev, enum set_key_cmd cmd,
  1760. const u8 *local_address, const u8 *address,
  1761. struct ieee80211_key_conf *key)
  1762. {
  1763. struct p54_common *priv = dev->priv;
  1764. struct sk_buff *skb;
  1765. struct p54_keycache *rxkey;
  1766. u8 algo = 0;
  1767. if (modparam_nohwcrypt)
  1768. return -EOPNOTSUPP;
  1769. if (cmd == DISABLE_KEY)
  1770. algo = 0;
  1771. else {
  1772. switch (key->alg) {
  1773. case ALG_TKIP:
  1774. if (!(priv->privacy_caps & (BR_DESC_PRIV_CAP_MICHAEL |
  1775. BR_DESC_PRIV_CAP_TKIP)))
  1776. return -EOPNOTSUPP;
  1777. key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
  1778. algo = P54_CRYPTO_TKIPMICHAEL;
  1779. break;
  1780. case ALG_WEP:
  1781. if (!(priv->privacy_caps & BR_DESC_PRIV_CAP_WEP))
  1782. return -EOPNOTSUPP;
  1783. key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
  1784. algo = P54_CRYPTO_WEP;
  1785. break;
  1786. case ALG_CCMP:
  1787. if (!(priv->privacy_caps & BR_DESC_PRIV_CAP_AESCCMP))
  1788. return -EOPNOTSUPP;
  1789. key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
  1790. algo = P54_CRYPTO_AESCCMP;
  1791. break;
  1792. default:
  1793. return -EINVAL;
  1794. }
  1795. }
  1796. if (key->keyidx > priv->rx_keycache_size) {
  1797. /*
  1798. * The device supports the choosen algorithm, but the firmware
  1799. * does not provide enough key slots to store all of them.
  1800. * So, incoming frames have to be decoded by the mac80211 stack,
  1801. * but we can still offload encryption for outgoing frames.
  1802. */
  1803. return 0;
  1804. }
  1805. mutex_lock(&priv->conf_mutex);
  1806. skb = p54_alloc_skb(dev, P54_HDR_FLAG_CONTROL_OPSET, sizeof(*rxkey) +
  1807. sizeof(struct p54_hdr), P54_CONTROL_TYPE_RX_KEYCACHE,
  1808. GFP_ATOMIC);
  1809. if (!skb) {
  1810. mutex_unlock(&priv->conf_mutex);
  1811. return -ENOMEM;
  1812. }
  1813. /* TODO: some devices have 4 more free slots for rx keys */
  1814. rxkey = (struct p54_keycache *)skb_put(skb, sizeof(*rxkey));
  1815. rxkey->entry = key->keyidx;
  1816. rxkey->key_id = key->keyidx;
  1817. rxkey->key_type = algo;
  1818. if (address)
  1819. memcpy(rxkey->mac, address, ETH_ALEN);
  1820. else
  1821. memset(rxkey->mac, ~0, ETH_ALEN);
  1822. if (key->alg != ALG_TKIP) {
  1823. rxkey->key_len = min((u8)16, key->keylen);
  1824. memcpy(rxkey->key, key->key, rxkey->key_len);
  1825. } else {
  1826. rxkey->key_len = 24;
  1827. memcpy(rxkey->key, key->key, 16);
  1828. memcpy(&(rxkey->key[16]), &(key->key
  1829. [NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY]), 8);
  1830. }
  1831. priv->tx(dev, skb);
  1832. mutex_unlock(&priv->conf_mutex);
  1833. return 0;
  1834. }
  1835. static const struct ieee80211_ops p54_ops = {
  1836. .tx = p54_tx,
  1837. .start = p54_start,
  1838. .stop = p54_stop,
  1839. .add_interface = p54_add_interface,
  1840. .remove_interface = p54_remove_interface,
  1841. .set_tim = p54_set_tim,
  1842. .sta_notify = p54_sta_notify,
  1843. .set_key = p54_set_key,
  1844. .config = p54_config,
  1845. .config_interface = p54_config_interface,
  1846. .bss_info_changed = p54_bss_info_changed,
  1847. .configure_filter = p54_configure_filter,
  1848. .conf_tx = p54_conf_tx,
  1849. .get_stats = p54_get_stats,
  1850. .get_tx_stats = p54_get_tx_stats
  1851. };
  1852. struct ieee80211_hw *p54_init_common(size_t priv_data_len)
  1853. {
  1854. struct ieee80211_hw *dev;
  1855. struct p54_common *priv;
  1856. dev = ieee80211_alloc_hw(priv_data_len, &p54_ops);
  1857. if (!dev)
  1858. return NULL;
  1859. priv = dev->priv;
  1860. priv->hw = dev;
  1861. priv->mode = NL80211_IFTYPE_UNSPECIFIED;
  1862. priv->basic_rate_mask = 0x15f;
  1863. skb_queue_head_init(&priv->tx_queue);
  1864. dev->flags = IEEE80211_HW_RX_INCLUDES_FCS |
  1865. IEEE80211_HW_SIGNAL_DBM |
  1866. IEEE80211_HW_NOISE_DBM;
  1867. dev->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
  1868. BIT(NL80211_IFTYPE_ADHOC) |
  1869. BIT(NL80211_IFTYPE_AP) |
  1870. BIT(NL80211_IFTYPE_MESH_POINT);
  1871. dev->channel_change_time = 1000; /* TODO: find actual value */
  1872. priv->tx_stats[0].limit = 1; /* Beacon queue */
  1873. priv->tx_stats[1].limit = 1; /* Probe queue for HW scan */
  1874. priv->tx_stats[2].limit = 3; /* queue for MLMEs */
  1875. priv->tx_stats[3].limit = 3; /* Broadcast / MC queue */
  1876. priv->tx_stats[4].limit = 5; /* Data */
  1877. dev->queues = 1;
  1878. priv->noise = -94;
  1879. /*
  1880. * We support at most 8 tries no matter which rate they're at,
  1881. * we cannot support max_rates * max_rate_tries as we set it
  1882. * here, but setting it correctly to 4/2 or so would limit us
  1883. * artificially if the RC algorithm wants just two rates, so
  1884. * let's say 4/7, we'll redistribute it at TX time, see the
  1885. * comments there.
  1886. */
  1887. dev->max_rates = 4;
  1888. dev->max_rate_tries = 7;
  1889. dev->extra_tx_headroom = sizeof(struct p54_hdr) + 4 +
  1890. sizeof(struct p54_tx_data);
  1891. mutex_init(&priv->conf_mutex);
  1892. init_completion(&priv->eeprom_comp);
  1893. INIT_DELAYED_WORK(&priv->work, p54_work);
  1894. return dev;
  1895. }
  1896. EXPORT_SYMBOL_GPL(p54_init_common);
  1897. void p54_free_common(struct ieee80211_hw *dev)
  1898. {
  1899. struct p54_common *priv = dev->priv;
  1900. kfree(priv->iq_autocal);
  1901. kfree(priv->output_limit);
  1902. kfree(priv->curve_data);
  1903. }
  1904. EXPORT_SYMBOL_GPL(p54_free_common);
  1905. static int __init p54_init(void)
  1906. {
  1907. return 0;
  1908. }
  1909. static void __exit p54_exit(void)
  1910. {
  1911. }
  1912. module_init(p54_init);
  1913. module_exit(p54_exit);