p54common.c 70 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629
  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. #ifdef CONFIG_P54_LEDS
  23. #include <linux/leds.h>
  24. #endif /* CONFIG_P54_LEDS */
  25. #include "p54.h"
  26. #include "p54common.h"
  27. static int modparam_nohwcrypt;
  28. module_param_named(nohwcrypt, modparam_nohwcrypt, bool, S_IRUGO);
  29. MODULE_PARM_DESC(nohwcrypt, "Disable hardware encryption.");
  30. MODULE_AUTHOR("Michael Wu <flamingice@sourmilk.net>");
  31. MODULE_DESCRIPTION("Softmac Prism54 common code");
  32. MODULE_LICENSE("GPL");
  33. MODULE_ALIAS("prism54common");
  34. static struct ieee80211_rate p54_bgrates[] = {
  35. { .bitrate = 10, .hw_value = 0, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  36. { .bitrate = 20, .hw_value = 1, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  37. { .bitrate = 55, .hw_value = 2, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  38. { .bitrate = 110, .hw_value = 3, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  39. { .bitrate = 60, .hw_value = 4, },
  40. { .bitrate = 90, .hw_value = 5, },
  41. { .bitrate = 120, .hw_value = 6, },
  42. { .bitrate = 180, .hw_value = 7, },
  43. { .bitrate = 240, .hw_value = 8, },
  44. { .bitrate = 360, .hw_value = 9, },
  45. { .bitrate = 480, .hw_value = 10, },
  46. { .bitrate = 540, .hw_value = 11, },
  47. };
  48. static struct ieee80211_channel p54_bgchannels[] = {
  49. { .center_freq = 2412, .hw_value = 1, },
  50. { .center_freq = 2417, .hw_value = 2, },
  51. { .center_freq = 2422, .hw_value = 3, },
  52. { .center_freq = 2427, .hw_value = 4, },
  53. { .center_freq = 2432, .hw_value = 5, },
  54. { .center_freq = 2437, .hw_value = 6, },
  55. { .center_freq = 2442, .hw_value = 7, },
  56. { .center_freq = 2447, .hw_value = 8, },
  57. { .center_freq = 2452, .hw_value = 9, },
  58. { .center_freq = 2457, .hw_value = 10, },
  59. { .center_freq = 2462, .hw_value = 11, },
  60. { .center_freq = 2467, .hw_value = 12, },
  61. { .center_freq = 2472, .hw_value = 13, },
  62. { .center_freq = 2484, .hw_value = 14, },
  63. };
  64. static struct ieee80211_supported_band band_2GHz = {
  65. .channels = p54_bgchannels,
  66. .n_channels = ARRAY_SIZE(p54_bgchannels),
  67. .bitrates = p54_bgrates,
  68. .n_bitrates = ARRAY_SIZE(p54_bgrates),
  69. };
  70. static struct ieee80211_rate p54_arates[] = {
  71. { .bitrate = 60, .hw_value = 4, },
  72. { .bitrate = 90, .hw_value = 5, },
  73. { .bitrate = 120, .hw_value = 6, },
  74. { .bitrate = 180, .hw_value = 7, },
  75. { .bitrate = 240, .hw_value = 8, },
  76. { .bitrate = 360, .hw_value = 9, },
  77. { .bitrate = 480, .hw_value = 10, },
  78. { .bitrate = 540, .hw_value = 11, },
  79. };
  80. static struct ieee80211_channel p54_achannels[] = {
  81. { .center_freq = 4920 },
  82. { .center_freq = 4940 },
  83. { .center_freq = 4960 },
  84. { .center_freq = 4980 },
  85. { .center_freq = 5040 },
  86. { .center_freq = 5060 },
  87. { .center_freq = 5080 },
  88. { .center_freq = 5170 },
  89. { .center_freq = 5180 },
  90. { .center_freq = 5190 },
  91. { .center_freq = 5200 },
  92. { .center_freq = 5210 },
  93. { .center_freq = 5220 },
  94. { .center_freq = 5230 },
  95. { .center_freq = 5240 },
  96. { .center_freq = 5260 },
  97. { .center_freq = 5280 },
  98. { .center_freq = 5300 },
  99. { .center_freq = 5320 },
  100. { .center_freq = 5500 },
  101. { .center_freq = 5520 },
  102. { .center_freq = 5540 },
  103. { .center_freq = 5560 },
  104. { .center_freq = 5580 },
  105. { .center_freq = 5600 },
  106. { .center_freq = 5620 },
  107. { .center_freq = 5640 },
  108. { .center_freq = 5660 },
  109. { .center_freq = 5680 },
  110. { .center_freq = 5700 },
  111. { .center_freq = 5745 },
  112. { .center_freq = 5765 },
  113. { .center_freq = 5785 },
  114. { .center_freq = 5805 },
  115. { .center_freq = 5825 },
  116. };
  117. static struct ieee80211_supported_band band_5GHz = {
  118. .channels = p54_achannels,
  119. .n_channels = ARRAY_SIZE(p54_achannels),
  120. .bitrates = p54_arates,
  121. .n_bitrates = ARRAY_SIZE(p54_arates),
  122. };
  123. int p54_parse_firmware(struct ieee80211_hw *dev, const struct firmware *fw)
  124. {
  125. struct p54_common *priv = dev->priv;
  126. struct bootrec_exp_if *exp_if;
  127. struct bootrec *bootrec;
  128. u32 *data = (u32 *)fw->data;
  129. u32 *end_data = (u32 *)fw->data + (fw->size >> 2);
  130. u8 *fw_version = NULL;
  131. size_t len;
  132. int i;
  133. int maxlen;
  134. if (priv->rx_start)
  135. return 0;
  136. while (data < end_data && *data)
  137. data++;
  138. while (data < end_data && !*data)
  139. data++;
  140. bootrec = (struct bootrec *) data;
  141. while (bootrec->data <= end_data &&
  142. (bootrec->data + (len = le32_to_cpu(bootrec->len))) <= end_data) {
  143. u32 code = le32_to_cpu(bootrec->code);
  144. switch (code) {
  145. case BR_CODE_COMPONENT_ID:
  146. priv->fw_interface = be32_to_cpup((__be32 *)
  147. bootrec->data);
  148. switch (priv->fw_interface) {
  149. case FW_LM86:
  150. case FW_LM20:
  151. case FW_LM87: {
  152. char *iftype = (char *)bootrec->data;
  153. printk(KERN_INFO "%s: p54 detected a LM%c%c "
  154. "firmware\n",
  155. wiphy_name(dev->wiphy),
  156. iftype[2], iftype[3]);
  157. break;
  158. }
  159. case FW_FMAC:
  160. default:
  161. printk(KERN_ERR "%s: unsupported firmware\n",
  162. wiphy_name(dev->wiphy));
  163. return -ENODEV;
  164. }
  165. break;
  166. case BR_CODE_COMPONENT_VERSION:
  167. /* 24 bytes should be enough for all firmwares */
  168. if (strnlen((unsigned char*)bootrec->data, 24) < 24)
  169. fw_version = (unsigned char*)bootrec->data;
  170. break;
  171. case BR_CODE_DESCR: {
  172. struct bootrec_desc *desc =
  173. (struct bootrec_desc *)bootrec->data;
  174. priv->rx_start = le32_to_cpu(desc->rx_start);
  175. /* FIXME add sanity checking */
  176. priv->rx_end = le32_to_cpu(desc->rx_end) - 0x3500;
  177. priv->headroom = desc->headroom;
  178. priv->tailroom = desc->tailroom;
  179. priv->privacy_caps = desc->privacy_caps;
  180. priv->rx_keycache_size = desc->rx_keycache_size;
  181. if (le32_to_cpu(bootrec->len) == 11)
  182. priv->rx_mtu = le16_to_cpu(desc->rx_mtu);
  183. else
  184. priv->rx_mtu = (size_t)
  185. 0x620 - priv->tx_hdr_len;
  186. maxlen = priv->tx_hdr_len + /* USB devices */
  187. sizeof(struct p54_rx_data) +
  188. 4 + /* rx alignment */
  189. IEEE80211_MAX_FRAG_THRESHOLD;
  190. if (priv->rx_mtu > maxlen && PAGE_SIZE == 4096) {
  191. printk(KERN_INFO "p54: rx_mtu reduced from %d "
  192. "to %d\n", priv->rx_mtu,
  193. maxlen);
  194. priv->rx_mtu = maxlen;
  195. }
  196. break;
  197. }
  198. case BR_CODE_EXPOSED_IF:
  199. exp_if = (struct bootrec_exp_if *) bootrec->data;
  200. for (i = 0; i < (len * sizeof(*exp_if) / 4); i++)
  201. if (exp_if[i].if_id == cpu_to_le16(0x1a))
  202. priv->fw_var = le16_to_cpu(exp_if[i].variant);
  203. break;
  204. case BR_CODE_DEPENDENT_IF:
  205. break;
  206. case BR_CODE_END_OF_BRA:
  207. case LEGACY_BR_CODE_END_OF_BRA:
  208. end_data = NULL;
  209. break;
  210. default:
  211. break;
  212. }
  213. bootrec = (struct bootrec *)&bootrec->data[len];
  214. }
  215. if (fw_version)
  216. printk(KERN_INFO "%s: FW rev %s - Softmac protocol %x.%x\n",
  217. wiphy_name(dev->wiphy), fw_version,
  218. priv->fw_var >> 8, priv->fw_var & 0xff);
  219. if (priv->fw_var < 0x500)
  220. printk(KERN_INFO "%s: you are using an obsolete firmware. "
  221. "visit http://wireless.kernel.org/en/users/Drivers/p54 "
  222. "and grab one for \"kernel >= 2.6.28\"!\n",
  223. wiphy_name(dev->wiphy));
  224. if (priv->fw_var >= 0x300) {
  225. /* Firmware supports QoS, use it! */
  226. priv->tx_stats[P54_QUEUE_AC_VO].limit = 3;
  227. priv->tx_stats[P54_QUEUE_AC_VI].limit = 4;
  228. priv->tx_stats[P54_QUEUE_AC_BE].limit = 3;
  229. priv->tx_stats[P54_QUEUE_AC_BK].limit = 2;
  230. dev->queues = P54_QUEUE_AC_NUM;
  231. }
  232. if (!modparam_nohwcrypt)
  233. printk(KERN_INFO "%s: cryptographic accelerator "
  234. "WEP:%s, TKIP:%s, CCMP:%s\n",
  235. wiphy_name(dev->wiphy),
  236. (priv->privacy_caps & BR_DESC_PRIV_CAP_WEP) ? "YES" :
  237. "no", (priv->privacy_caps & (BR_DESC_PRIV_CAP_TKIP |
  238. BR_DESC_PRIV_CAP_MICHAEL)) ? "YES" : "no",
  239. (priv->privacy_caps & BR_DESC_PRIV_CAP_AESCCMP) ?
  240. "YES" : "no");
  241. return 0;
  242. }
  243. EXPORT_SYMBOL_GPL(p54_parse_firmware);
  244. static int p54_convert_rev0(struct ieee80211_hw *dev,
  245. struct pda_pa_curve_data *curve_data)
  246. {
  247. struct p54_common *priv = dev->priv;
  248. struct p54_pa_curve_data_sample *dst;
  249. struct pda_pa_curve_data_sample_rev0 *src;
  250. size_t cd_len = sizeof(*curve_data) +
  251. (curve_data->points_per_channel*sizeof(*dst) + 2) *
  252. curve_data->channels;
  253. unsigned int i, j;
  254. void *source, *target;
  255. priv->curve_data = kmalloc(sizeof(*priv->curve_data) + cd_len,
  256. GFP_KERNEL);
  257. if (!priv->curve_data)
  258. return -ENOMEM;
  259. priv->curve_data->entries = curve_data->channels;
  260. priv->curve_data->entry_size = sizeof(__le16) +
  261. sizeof(*dst) * curve_data->points_per_channel;
  262. priv->curve_data->offset = offsetof(struct pda_pa_curve_data, data);
  263. priv->curve_data->len = cd_len;
  264. memcpy(priv->curve_data->data, curve_data, sizeof(*curve_data));
  265. source = curve_data->data;
  266. target = ((struct pda_pa_curve_data *) priv->curve_data->data)->data;
  267. for (i = 0; i < curve_data->channels; i++) {
  268. __le16 *freq = source;
  269. source += sizeof(__le16);
  270. *((__le16 *)target) = *freq;
  271. target += sizeof(__le16);
  272. for (j = 0; j < curve_data->points_per_channel; j++) {
  273. dst = target;
  274. src = source;
  275. dst->rf_power = src->rf_power;
  276. dst->pa_detector = src->pa_detector;
  277. dst->data_64qam = src->pcv;
  278. /* "invent" the points for the other modulations */
  279. #define SUB(x,y) (u8)((x) - (y)) > (x) ? 0 : (x) - (y)
  280. dst->data_16qam = SUB(src->pcv, 12);
  281. dst->data_qpsk = SUB(dst->data_16qam, 12);
  282. dst->data_bpsk = SUB(dst->data_qpsk, 12);
  283. dst->data_barker = SUB(dst->data_bpsk, 14);
  284. #undef SUB
  285. target += sizeof(*dst);
  286. source += sizeof(*src);
  287. }
  288. }
  289. return 0;
  290. }
  291. static int p54_convert_rev1(struct ieee80211_hw *dev,
  292. struct pda_pa_curve_data *curve_data)
  293. {
  294. struct p54_common *priv = dev->priv;
  295. struct p54_pa_curve_data_sample *dst;
  296. struct pda_pa_curve_data_sample_rev1 *src;
  297. size_t cd_len = sizeof(*curve_data) +
  298. (curve_data->points_per_channel*sizeof(*dst) + 2) *
  299. curve_data->channels;
  300. unsigned int i, j;
  301. void *source, *target;
  302. priv->curve_data = kzalloc(cd_len + sizeof(*priv->curve_data),
  303. GFP_KERNEL);
  304. if (!priv->curve_data)
  305. return -ENOMEM;
  306. priv->curve_data->entries = curve_data->channels;
  307. priv->curve_data->entry_size = sizeof(__le16) +
  308. sizeof(*dst) * curve_data->points_per_channel;
  309. priv->curve_data->offset = offsetof(struct pda_pa_curve_data, data);
  310. priv->curve_data->len = cd_len;
  311. memcpy(priv->curve_data->data, curve_data, sizeof(*curve_data));
  312. source = curve_data->data;
  313. target = ((struct pda_pa_curve_data *) priv->curve_data->data)->data;
  314. for (i = 0; i < curve_data->channels; i++) {
  315. __le16 *freq = source;
  316. source += sizeof(__le16);
  317. *((__le16 *)target) = *freq;
  318. target += sizeof(__le16);
  319. for (j = 0; j < curve_data->points_per_channel; j++) {
  320. memcpy(target, source, sizeof(*src));
  321. target += sizeof(*dst);
  322. source += sizeof(*src);
  323. }
  324. source++;
  325. }
  326. return 0;
  327. }
  328. static const char *p54_rf_chips[] = { "NULL", "Duette3", "Duette2",
  329. "Frisbee", "Xbow", "Longbow", "NULL", "NULL" };
  330. static int p54_init_xbow_synth(struct ieee80211_hw *dev);
  331. static void p54_parse_rssical(struct ieee80211_hw *dev, void *data, int len,
  332. u16 type)
  333. {
  334. struct p54_common *priv = dev->priv;
  335. int offset = (type == PDR_RSSI_LINEAR_APPROXIMATION_EXTENDED) ? 2 : 0;
  336. int entry_size = sizeof(struct pda_rssi_cal_entry) + offset;
  337. int num_entries = (type == PDR_RSSI_LINEAR_APPROXIMATION) ? 1 : 2;
  338. int i;
  339. if (len != (entry_size * num_entries)) {
  340. printk(KERN_ERR "%s: unknown rssi calibration data packing "
  341. " type:(%x) len:%d.\n",
  342. wiphy_name(dev->wiphy), type, len);
  343. print_hex_dump_bytes("rssical:", DUMP_PREFIX_NONE,
  344. data, len);
  345. printk(KERN_ERR "%s: please report this issue.\n",
  346. wiphy_name(dev->wiphy));
  347. return;
  348. }
  349. for (i = 0; i < num_entries; i++) {
  350. struct pda_rssi_cal_entry *cal = data +
  351. (offset + i * entry_size);
  352. priv->rssical_db[i].mul = (s16) le16_to_cpu(cal->mul);
  353. priv->rssical_db[i].add = (s16) le16_to_cpu(cal->add);
  354. }
  355. }
  356. static void p54_parse_default_country(struct ieee80211_hw *dev,
  357. void *data, int len)
  358. {
  359. struct pda_country *country;
  360. if (len != sizeof(*country)) {
  361. printk(KERN_ERR "%s: found possible invalid default country "
  362. "eeprom entry. (entry size: %d)\n",
  363. wiphy_name(dev->wiphy), len);
  364. print_hex_dump_bytes("country:", DUMP_PREFIX_NONE,
  365. data, len);
  366. printk(KERN_ERR "%s: please report this issue.\n",
  367. wiphy_name(dev->wiphy));
  368. return;
  369. }
  370. country = (struct pda_country *) data;
  371. if (country->flags == PDR_COUNTRY_CERT_CODE_PSEUDO)
  372. regulatory_hint(dev->wiphy, country->alpha2);
  373. else {
  374. /* TODO:
  375. * write a shared/common function that converts
  376. * "Regulatory domain codes" (802.11-2007 14.8.2.2)
  377. * into ISO/IEC 3166-1 alpha2 for regulatory_hint.
  378. */
  379. }
  380. }
  381. static int p54_convert_output_limits(struct ieee80211_hw *dev,
  382. u8 *data, size_t len)
  383. {
  384. struct p54_common *priv = dev->priv;
  385. if (len < 2)
  386. return -EINVAL;
  387. if (data[0] != 0) {
  388. printk(KERN_ERR "%s: unknown output power db revision:%x\n",
  389. wiphy_name(dev->wiphy), data[0]);
  390. return -EINVAL;
  391. }
  392. if (2 + data[1] * sizeof(struct pda_channel_output_limit) > len)
  393. return -EINVAL;
  394. priv->output_limit = kmalloc(data[1] *
  395. sizeof(struct pda_channel_output_limit) +
  396. sizeof(*priv->output_limit), GFP_KERNEL);
  397. if (!priv->output_limit)
  398. return -ENOMEM;
  399. priv->output_limit->offset = 0;
  400. priv->output_limit->entries = data[1];
  401. priv->output_limit->entry_size =
  402. sizeof(struct pda_channel_output_limit);
  403. priv->output_limit->len = priv->output_limit->entry_size *
  404. priv->output_limit->entries +
  405. priv->output_limit->offset;
  406. memcpy(priv->output_limit->data, &data[2],
  407. data[1] * sizeof(struct pda_channel_output_limit));
  408. return 0;
  409. }
  410. static struct p54_cal_database *p54_convert_db(struct pda_custom_wrapper *src,
  411. size_t total_len)
  412. {
  413. struct p54_cal_database *dst;
  414. size_t payload_len, entries, entry_size, offset;
  415. payload_len = le16_to_cpu(src->len);
  416. entries = le16_to_cpu(src->entries);
  417. entry_size = le16_to_cpu(src->entry_size);
  418. offset = le16_to_cpu(src->offset);
  419. if (((entries * entry_size + offset) != payload_len) ||
  420. (payload_len + sizeof(*src) != total_len))
  421. return NULL;
  422. dst = kmalloc(sizeof(*dst) + payload_len, GFP_KERNEL);
  423. if (!dst)
  424. return NULL;
  425. dst->entries = entries;
  426. dst->entry_size = entry_size;
  427. dst->offset = offset;
  428. dst->len = payload_len;
  429. memcpy(dst->data, src->data, payload_len);
  430. return dst;
  431. }
  432. int p54_parse_eeprom(struct ieee80211_hw *dev, void *eeprom, int len)
  433. {
  434. struct p54_common *priv = dev->priv;
  435. struct eeprom_pda_wrap *wrap = NULL;
  436. struct pda_entry *entry;
  437. unsigned int data_len, entry_len;
  438. void *tmp;
  439. int err;
  440. u8 *end = (u8 *)eeprom + len;
  441. u16 synth = 0;
  442. wrap = (struct eeprom_pda_wrap *) eeprom;
  443. entry = (void *)wrap->data + le16_to_cpu(wrap->len);
  444. /* verify that at least the entry length/code fits */
  445. while ((u8 *)entry <= end - sizeof(*entry)) {
  446. entry_len = le16_to_cpu(entry->len);
  447. data_len = ((entry_len - 1) << 1);
  448. /* abort if entry exceeds whole structure */
  449. if ((u8 *)entry + sizeof(*entry) + data_len > end)
  450. break;
  451. switch (le16_to_cpu(entry->code)) {
  452. case PDR_MAC_ADDRESS:
  453. if (data_len != ETH_ALEN)
  454. break;
  455. SET_IEEE80211_PERM_ADDR(dev, entry->data);
  456. break;
  457. case PDR_PRISM_PA_CAL_OUTPUT_POWER_LIMITS:
  458. if (priv->output_limit)
  459. break;
  460. err = p54_convert_output_limits(dev, entry->data,
  461. data_len);
  462. if (err)
  463. goto err;
  464. break;
  465. case PDR_PRISM_PA_CAL_CURVE_DATA: {
  466. struct pda_pa_curve_data *curve_data =
  467. (struct pda_pa_curve_data *)entry->data;
  468. if (data_len < sizeof(*curve_data)) {
  469. err = -EINVAL;
  470. goto err;
  471. }
  472. switch (curve_data->cal_method_rev) {
  473. case 0:
  474. err = p54_convert_rev0(dev, curve_data);
  475. break;
  476. case 1:
  477. err = p54_convert_rev1(dev, curve_data);
  478. break;
  479. default:
  480. printk(KERN_ERR "%s: unknown curve data "
  481. "revision %d\n",
  482. wiphy_name(dev->wiphy),
  483. curve_data->cal_method_rev);
  484. err = -ENODEV;
  485. break;
  486. }
  487. if (err)
  488. goto err;
  489. }
  490. break;
  491. case PDR_PRISM_ZIF_TX_IQ_CALIBRATION:
  492. priv->iq_autocal = kmalloc(data_len, GFP_KERNEL);
  493. if (!priv->iq_autocal) {
  494. err = -ENOMEM;
  495. goto err;
  496. }
  497. memcpy(priv->iq_autocal, entry->data, data_len);
  498. priv->iq_autocal_len = data_len / sizeof(struct pda_iq_autocal_entry);
  499. break;
  500. case PDR_DEFAULT_COUNTRY:
  501. p54_parse_default_country(dev, entry->data, data_len);
  502. break;
  503. case PDR_INTERFACE_LIST:
  504. tmp = entry->data;
  505. while ((u8 *)tmp < entry->data + data_len) {
  506. struct bootrec_exp_if *exp_if = tmp;
  507. if (le16_to_cpu(exp_if->if_id) == 0xf)
  508. synth = le16_to_cpu(exp_if->variant);
  509. tmp += sizeof(struct bootrec_exp_if);
  510. }
  511. break;
  512. case PDR_HARDWARE_PLATFORM_COMPONENT_ID:
  513. if (data_len < 2)
  514. break;
  515. priv->version = *(u8 *)(entry->data + 1);
  516. break;
  517. case PDR_RSSI_LINEAR_APPROXIMATION:
  518. case PDR_RSSI_LINEAR_APPROXIMATION_DUAL_BAND:
  519. case PDR_RSSI_LINEAR_APPROXIMATION_EXTENDED:
  520. p54_parse_rssical(dev, entry->data, data_len,
  521. le16_to_cpu(entry->code));
  522. break;
  523. case PDR_RSSI_LINEAR_APPROXIMATION_CUSTOM: {
  524. __le16 *src = (void *) entry->data;
  525. s16 *dst = (void *) &priv->rssical_db;
  526. int i;
  527. if (data_len != sizeof(priv->rssical_db)) {
  528. err = -EINVAL;
  529. goto err;
  530. }
  531. for (i = 0; i < sizeof(priv->rssical_db) /
  532. sizeof(*src); i++)
  533. *(dst++) = (s16) le16_to_cpu(*(src++));
  534. }
  535. break;
  536. case PDR_PRISM_PA_CAL_OUTPUT_POWER_LIMITS_CUSTOM: {
  537. struct pda_custom_wrapper *pda = (void *) entry->data;
  538. if (priv->output_limit || data_len < sizeof(*pda))
  539. break;
  540. priv->output_limit = p54_convert_db(pda, data_len);
  541. }
  542. break;
  543. case PDR_PRISM_PA_CAL_CURVE_DATA_CUSTOM: {
  544. struct pda_custom_wrapper *pda = (void *) entry->data;
  545. if (priv->curve_data || data_len < sizeof(*pda))
  546. break;
  547. priv->curve_data = p54_convert_db(pda, data_len);
  548. }
  549. break;
  550. case PDR_END:
  551. /* make it overrun */
  552. entry_len = len;
  553. break;
  554. case PDR_MANUFACTURING_PART_NUMBER:
  555. case PDR_PDA_VERSION:
  556. case PDR_NIC_SERIAL_NUMBER:
  557. case PDR_REGULATORY_DOMAIN_LIST:
  558. case PDR_TEMPERATURE_TYPE:
  559. case PDR_PRISM_PCI_IDENTIFIER:
  560. case PDR_COUNTRY_INFORMATION:
  561. case PDR_OEM_NAME:
  562. case PDR_PRODUCT_NAME:
  563. case PDR_UTF8_OEM_NAME:
  564. case PDR_UTF8_PRODUCT_NAME:
  565. case PDR_COUNTRY_LIST:
  566. case PDR_ANTENNA_GAIN:
  567. case PDR_PRISM_INDIGO_PA_CALIBRATION_DATA:
  568. case PDR_REGULATORY_POWER_LIMITS:
  569. case PDR_RADIATED_TRANSMISSION_CORRECTION:
  570. case PDR_PRISM_TX_IQ_CALIBRATION:
  571. case PDR_BASEBAND_REGISTERS:
  572. case PDR_PER_CHANNEL_BASEBAND_REGISTERS:
  573. break;
  574. default:
  575. printk(KERN_INFO "%s: unknown eeprom code : 0x%x\n",
  576. wiphy_name(dev->wiphy),
  577. le16_to_cpu(entry->code));
  578. break;
  579. }
  580. entry = (void *)entry + (entry_len + 1)*2;
  581. }
  582. if (!synth || !priv->iq_autocal || !priv->output_limit ||
  583. !priv->curve_data) {
  584. printk(KERN_ERR "%s: not all required entries found in eeprom!\n",
  585. wiphy_name(dev->wiphy));
  586. err = -EINVAL;
  587. goto err;
  588. }
  589. priv->rxhw = synth & PDR_SYNTH_FRONTEND_MASK;
  590. if (priv->rxhw == PDR_SYNTH_FRONTEND_XBOW)
  591. p54_init_xbow_synth(dev);
  592. if (!(synth & PDR_SYNTH_24_GHZ_DISABLED))
  593. dev->wiphy->bands[IEEE80211_BAND_2GHZ] = &band_2GHz;
  594. if (!(synth & PDR_SYNTH_5_GHZ_DISABLED))
  595. dev->wiphy->bands[IEEE80211_BAND_5GHZ] = &band_5GHz;
  596. if ((synth & PDR_SYNTH_RX_DIV_MASK) == PDR_SYNTH_RX_DIV_SUPPORTED)
  597. priv->rx_diversity_mask = 3;
  598. if ((synth & PDR_SYNTH_TX_DIV_MASK) == PDR_SYNTH_TX_DIV_SUPPORTED)
  599. priv->tx_diversity_mask = 3;
  600. if (!is_valid_ether_addr(dev->wiphy->perm_addr)) {
  601. u8 perm_addr[ETH_ALEN];
  602. printk(KERN_WARNING "%s: Invalid hwaddr! Using randomly generated MAC addr\n",
  603. wiphy_name(dev->wiphy));
  604. random_ether_addr(perm_addr);
  605. SET_IEEE80211_PERM_ADDR(dev, perm_addr);
  606. }
  607. printk(KERN_INFO "%s: hwaddr %pM, MAC:isl38%02x RF:%s\n",
  608. wiphy_name(dev->wiphy),
  609. dev->wiphy->perm_addr,
  610. priv->version, p54_rf_chips[priv->rxhw]);
  611. return 0;
  612. err:
  613. if (priv->iq_autocal) {
  614. kfree(priv->iq_autocal);
  615. priv->iq_autocal = NULL;
  616. }
  617. if (priv->output_limit) {
  618. kfree(priv->output_limit);
  619. priv->output_limit = NULL;
  620. }
  621. if (priv->curve_data) {
  622. kfree(priv->curve_data);
  623. priv->curve_data = NULL;
  624. }
  625. printk(KERN_ERR "%s: eeprom parse failed!\n",
  626. wiphy_name(dev->wiphy));
  627. return err;
  628. }
  629. EXPORT_SYMBOL_GPL(p54_parse_eeprom);
  630. static int p54_rssi_to_dbm(struct ieee80211_hw *dev, int rssi)
  631. {
  632. struct p54_common *priv = dev->priv;
  633. int band = dev->conf.channel->band;
  634. if (priv->rxhw != PDR_SYNTH_FRONTEND_LONGBOW)
  635. return ((rssi * priv->rssical_db[band].mul) / 64 +
  636. priv->rssical_db[band].add) / 4;
  637. else
  638. /*
  639. * TODO: find the correct formula
  640. */
  641. return ((rssi * priv->rssical_db[band].mul) / 64 +
  642. priv->rssical_db[band].add) / 4;
  643. }
  644. static int p54_rx_data(struct ieee80211_hw *dev, struct sk_buff *skb)
  645. {
  646. struct p54_common *priv = dev->priv;
  647. struct p54_rx_data *hdr = (struct p54_rx_data *) skb->data;
  648. struct ieee80211_rx_status rx_status = {0};
  649. u16 freq = le16_to_cpu(hdr->freq);
  650. size_t header_len = sizeof(*hdr);
  651. u32 tsf32;
  652. u8 rate = hdr->rate & 0xf;
  653. /*
  654. * If the device is in a unspecified state we have to
  655. * ignore all data frames. Else we could end up with a
  656. * nasty crash.
  657. */
  658. if (unlikely(priv->mode == NL80211_IFTYPE_UNSPECIFIED))
  659. return 0;
  660. if (!(hdr->flags & cpu_to_le16(P54_HDR_FLAG_DATA_IN_FCS_GOOD))) {
  661. return 0;
  662. }
  663. if (hdr->decrypt_status == P54_DECRYPT_OK)
  664. rx_status.flag |= RX_FLAG_DECRYPTED;
  665. if ((hdr->decrypt_status == P54_DECRYPT_FAIL_MICHAEL) ||
  666. (hdr->decrypt_status == P54_DECRYPT_FAIL_TKIP))
  667. rx_status.flag |= RX_FLAG_MMIC_ERROR;
  668. rx_status.signal = p54_rssi_to_dbm(dev, hdr->rssi);
  669. rx_status.noise = priv->noise;
  670. /* XX correct? */
  671. rx_status.qual = (100 * hdr->rssi) / 127;
  672. if (hdr->rate & 0x10)
  673. rx_status.flag |= RX_FLAG_SHORTPRE;
  674. if (dev->conf.channel->band == IEEE80211_BAND_5GHZ)
  675. rx_status.rate_idx = (rate < 4) ? 0 : rate - 4;
  676. else
  677. rx_status.rate_idx = rate;
  678. rx_status.freq = freq;
  679. rx_status.band = dev->conf.channel->band;
  680. rx_status.antenna = hdr->antenna;
  681. tsf32 = le32_to_cpu(hdr->tsf32);
  682. if (tsf32 < priv->tsf_low32)
  683. priv->tsf_high32++;
  684. rx_status.mactime = ((u64)priv->tsf_high32) << 32 | tsf32;
  685. priv->tsf_low32 = tsf32;
  686. rx_status.flag |= RX_FLAG_TSFT;
  687. if (hdr->flags & cpu_to_le16(P54_HDR_FLAG_DATA_ALIGN))
  688. header_len += hdr->align[0];
  689. skb_pull(skb, header_len);
  690. skb_trim(skb, le16_to_cpu(hdr->len));
  691. ieee80211_rx_irqsafe(dev, skb, &rx_status);
  692. queue_delayed_work(dev->workqueue, &priv->work,
  693. msecs_to_jiffies(P54_STATISTICS_UPDATE));
  694. return -1;
  695. }
  696. static void inline p54_wake_free_queues(struct ieee80211_hw *dev)
  697. {
  698. struct p54_common *priv = dev->priv;
  699. int i;
  700. if (priv->mode == NL80211_IFTYPE_UNSPECIFIED)
  701. return ;
  702. for (i = 0; i < dev->queues; i++)
  703. if (priv->tx_stats[i + P54_QUEUE_DATA].len <
  704. priv->tx_stats[i + P54_QUEUE_DATA].limit)
  705. ieee80211_wake_queue(dev, i);
  706. }
  707. void p54_free_skb(struct ieee80211_hw *dev, struct sk_buff *skb)
  708. {
  709. struct p54_common *priv = dev->priv;
  710. struct ieee80211_tx_info *info;
  711. struct p54_tx_info *range;
  712. unsigned long flags;
  713. u32 freed = 0, last_addr = priv->rx_start;
  714. if (unlikely(!skb || !dev || !skb_queue_len(&priv->tx_queue)))
  715. return;
  716. /*
  717. * don't try to free an already unlinked skb
  718. */
  719. if (unlikely((!skb->next) || (!skb->prev)))
  720. return;
  721. spin_lock_irqsave(&priv->tx_queue.lock, flags);
  722. info = IEEE80211_SKB_CB(skb);
  723. range = (void *)info->rate_driver_data;
  724. if (skb->prev != (struct sk_buff *)&priv->tx_queue) {
  725. struct ieee80211_tx_info *ni;
  726. struct p54_tx_info *mr;
  727. ni = IEEE80211_SKB_CB(skb->prev);
  728. mr = (struct p54_tx_info *)ni->rate_driver_data;
  729. last_addr = mr->end_addr;
  730. }
  731. if (skb->next != (struct sk_buff *)&priv->tx_queue) {
  732. struct ieee80211_tx_info *ni;
  733. struct p54_tx_info *mr;
  734. ni = IEEE80211_SKB_CB(skb->next);
  735. mr = (struct p54_tx_info *)ni->rate_driver_data;
  736. freed = mr->start_addr - last_addr;
  737. } else
  738. freed = priv->rx_end - last_addr;
  739. __skb_unlink(skb, &priv->tx_queue);
  740. spin_unlock_irqrestore(&priv->tx_queue.lock, flags);
  741. dev_kfree_skb_any(skb);
  742. if (freed >= priv->headroom + sizeof(struct p54_hdr) + 48 +
  743. IEEE80211_MAX_RTS_THRESHOLD + priv->tailroom)
  744. p54_wake_free_queues(dev);
  745. }
  746. EXPORT_SYMBOL_GPL(p54_free_skb);
  747. static struct sk_buff *p54_find_tx_entry(struct ieee80211_hw *dev,
  748. __le32 req_id)
  749. {
  750. struct p54_common *priv = dev->priv;
  751. struct sk_buff *entry;
  752. unsigned long flags;
  753. spin_lock_irqsave(&priv->tx_queue.lock, flags);
  754. entry = priv->tx_queue.next;
  755. while (entry != (struct sk_buff *)&priv->tx_queue) {
  756. struct p54_hdr *hdr = (struct p54_hdr *) entry->data;
  757. if (hdr->req_id == req_id) {
  758. spin_unlock_irqrestore(&priv->tx_queue.lock, flags);
  759. return entry;
  760. }
  761. entry = entry->next;
  762. }
  763. spin_unlock_irqrestore(&priv->tx_queue.lock, flags);
  764. return NULL;
  765. }
  766. static void p54_rx_frame_sent(struct ieee80211_hw *dev, struct sk_buff *skb)
  767. {
  768. struct p54_common *priv = dev->priv;
  769. struct p54_hdr *hdr = (struct p54_hdr *) skb->data;
  770. struct p54_frame_sent *payload = (struct p54_frame_sent *) hdr->data;
  771. struct sk_buff *entry;
  772. u32 addr = le32_to_cpu(hdr->req_id) - priv->headroom;
  773. struct p54_tx_info *range = NULL;
  774. u32 freed = 0;
  775. u32 last_addr = priv->rx_start;
  776. unsigned long flags;
  777. int count, idx;
  778. spin_lock_irqsave(&priv->tx_queue.lock, flags);
  779. entry = (struct sk_buff *) priv->tx_queue.next;
  780. while (entry != (struct sk_buff *)&priv->tx_queue) {
  781. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(entry);
  782. struct p54_hdr *entry_hdr;
  783. struct p54_tx_data *entry_data;
  784. unsigned int pad = 0, frame_len;
  785. range = (void *)info->rate_driver_data;
  786. if (range->start_addr != addr) {
  787. last_addr = range->end_addr;
  788. entry = entry->next;
  789. continue;
  790. }
  791. if (entry->next != (struct sk_buff *)&priv->tx_queue) {
  792. struct ieee80211_tx_info *ni;
  793. struct p54_tx_info *mr;
  794. ni = IEEE80211_SKB_CB(entry->next);
  795. mr = (struct p54_tx_info *)ni->rate_driver_data;
  796. freed = mr->start_addr - last_addr;
  797. } else
  798. freed = priv->rx_end - last_addr;
  799. last_addr = range->end_addr;
  800. __skb_unlink(entry, &priv->tx_queue);
  801. spin_unlock_irqrestore(&priv->tx_queue.lock, flags);
  802. frame_len = entry->len;
  803. entry_hdr = (struct p54_hdr *) entry->data;
  804. entry_data = (struct p54_tx_data *) entry_hdr->data;
  805. priv->tx_stats[entry_data->hw_queue].len--;
  806. priv->stats.dot11ACKFailureCount += payload->tries - 1;
  807. /*
  808. * Frames in P54_QUEUE_FWSCAN and P54_QUEUE_BEACON are
  809. * generated by the driver. Therefore tx_status is bogus
  810. * and we don't want to confuse the mac80211 stack.
  811. */
  812. if (unlikely(entry_data->hw_queue < P54_QUEUE_FWSCAN)) {
  813. if (entry_data->hw_queue == P54_QUEUE_BEACON)
  814. priv->cached_beacon = NULL;
  815. kfree_skb(entry);
  816. goto out;
  817. }
  818. /*
  819. * Clear manually, ieee80211_tx_info_clear_status would
  820. * clear the counts too and we need them.
  821. */
  822. memset(&info->status.ampdu_ack_len, 0,
  823. sizeof(struct ieee80211_tx_info) -
  824. offsetof(struct ieee80211_tx_info, status.ampdu_ack_len));
  825. BUILD_BUG_ON(offsetof(struct ieee80211_tx_info,
  826. status.ampdu_ack_len) != 23);
  827. if (entry_hdr->flags & cpu_to_le16(P54_HDR_FLAG_DATA_ALIGN))
  828. pad = entry_data->align[0];
  829. /* walk through the rates array and adjust the counts */
  830. count = payload->tries;
  831. for (idx = 0; idx < 4; idx++) {
  832. if (count >= info->status.rates[idx].count) {
  833. count -= info->status.rates[idx].count;
  834. } else if (count > 0) {
  835. info->status.rates[idx].count = count;
  836. count = 0;
  837. } else {
  838. info->status.rates[idx].idx = -1;
  839. info->status.rates[idx].count = 0;
  840. }
  841. }
  842. if (!(info->flags & IEEE80211_TX_CTL_NO_ACK) &&
  843. (!payload->status))
  844. info->flags |= IEEE80211_TX_STAT_ACK;
  845. if (payload->status & P54_TX_PSM_CANCELLED)
  846. info->flags |= IEEE80211_TX_STAT_TX_FILTERED;
  847. info->status.ack_signal = p54_rssi_to_dbm(dev,
  848. (int)payload->ack_rssi);
  849. /* Undo all changes to the frame. */
  850. switch (entry_data->key_type) {
  851. case P54_CRYPTO_TKIPMICHAEL: {
  852. u8 *iv = (u8 *)(entry_data->align + pad +
  853. entry_data->crypt_offset);
  854. /* Restore the original TKIP IV. */
  855. iv[2] = iv[0];
  856. iv[0] = iv[1];
  857. iv[1] = (iv[0] | 0x20) & 0x7f; /* WEPSeed - 8.3.2.2 */
  858. frame_len -= 12; /* remove TKIP_MMIC + TKIP_ICV */
  859. break;
  860. }
  861. case P54_CRYPTO_AESCCMP:
  862. frame_len -= 8; /* remove CCMP_MIC */
  863. break;
  864. case P54_CRYPTO_WEP:
  865. frame_len -= 4; /* remove WEP_ICV */
  866. break;
  867. }
  868. skb_trim(entry, frame_len);
  869. skb_pull(entry, sizeof(*hdr) + pad + sizeof(*entry_data));
  870. ieee80211_tx_status_irqsafe(dev, entry);
  871. goto out;
  872. }
  873. spin_unlock_irqrestore(&priv->tx_queue.lock, flags);
  874. out:
  875. if (freed >= priv->headroom + sizeof(struct p54_hdr) + 48 +
  876. IEEE80211_MAX_RTS_THRESHOLD + priv->tailroom)
  877. p54_wake_free_queues(dev);
  878. }
  879. static void p54_rx_eeprom_readback(struct ieee80211_hw *dev,
  880. struct sk_buff *skb)
  881. {
  882. struct p54_hdr *hdr = (struct p54_hdr *) skb->data;
  883. struct p54_eeprom_lm86 *eeprom = (struct p54_eeprom_lm86 *) hdr->data;
  884. struct p54_common *priv = dev->priv;
  885. if (!priv->eeprom)
  886. return ;
  887. if (priv->fw_var >= 0x509) {
  888. memcpy(priv->eeprom, eeprom->v2.data,
  889. le16_to_cpu(eeprom->v2.len));
  890. } else {
  891. memcpy(priv->eeprom, eeprom->v1.data,
  892. le16_to_cpu(eeprom->v1.len));
  893. }
  894. complete(&priv->eeprom_comp);
  895. }
  896. static void p54_rx_stats(struct ieee80211_hw *dev, struct sk_buff *skb)
  897. {
  898. struct p54_common *priv = dev->priv;
  899. struct p54_hdr *hdr = (struct p54_hdr *) skb->data;
  900. struct p54_statistics *stats = (struct p54_statistics *) hdr->data;
  901. u32 tsf32;
  902. if (unlikely(priv->mode == NL80211_IFTYPE_UNSPECIFIED))
  903. return ;
  904. tsf32 = le32_to_cpu(stats->tsf32);
  905. if (tsf32 < priv->tsf_low32)
  906. priv->tsf_high32++;
  907. priv->tsf_low32 = tsf32;
  908. priv->stats.dot11RTSFailureCount = le32_to_cpu(stats->rts_fail);
  909. priv->stats.dot11RTSSuccessCount = le32_to_cpu(stats->rts_success);
  910. priv->stats.dot11FCSErrorCount = le32_to_cpu(stats->rx_bad_fcs);
  911. priv->noise = p54_rssi_to_dbm(dev, le32_to_cpu(stats->noise));
  912. p54_free_skb(dev, p54_find_tx_entry(dev, hdr->req_id));
  913. }
  914. static void p54_rx_trap(struct ieee80211_hw *dev, struct sk_buff *skb)
  915. {
  916. struct p54_hdr *hdr = (struct p54_hdr *) skb->data;
  917. struct p54_trap *trap = (struct p54_trap *) hdr->data;
  918. u16 event = le16_to_cpu(trap->event);
  919. u16 freq = le16_to_cpu(trap->frequency);
  920. switch (event) {
  921. case P54_TRAP_BEACON_TX:
  922. break;
  923. case P54_TRAP_RADAR:
  924. printk(KERN_INFO "%s: radar (freq:%d MHz)\n",
  925. wiphy_name(dev->wiphy), freq);
  926. break;
  927. case P54_TRAP_NO_BEACON:
  928. break;
  929. case P54_TRAP_SCAN:
  930. break;
  931. case P54_TRAP_TBTT:
  932. break;
  933. case P54_TRAP_TIMER:
  934. break;
  935. default:
  936. printk(KERN_INFO "%s: received event:%x freq:%d\n",
  937. wiphy_name(dev->wiphy), event, freq);
  938. break;
  939. }
  940. }
  941. static int p54_rx_control(struct ieee80211_hw *dev, struct sk_buff *skb)
  942. {
  943. struct p54_hdr *hdr = (struct p54_hdr *) skb->data;
  944. switch (le16_to_cpu(hdr->type)) {
  945. case P54_CONTROL_TYPE_TXDONE:
  946. p54_rx_frame_sent(dev, skb);
  947. break;
  948. case P54_CONTROL_TYPE_TRAP:
  949. p54_rx_trap(dev, skb);
  950. break;
  951. case P54_CONTROL_TYPE_BBP:
  952. break;
  953. case P54_CONTROL_TYPE_STAT_READBACK:
  954. p54_rx_stats(dev, skb);
  955. break;
  956. case P54_CONTROL_TYPE_EEPROM_READBACK:
  957. p54_rx_eeprom_readback(dev, skb);
  958. break;
  959. default:
  960. printk(KERN_DEBUG "%s: not handling 0x%02x type control frame\n",
  961. wiphy_name(dev->wiphy), le16_to_cpu(hdr->type));
  962. break;
  963. }
  964. return 0;
  965. }
  966. /* returns zero if skb can be reused */
  967. int p54_rx(struct ieee80211_hw *dev, struct sk_buff *skb)
  968. {
  969. u16 type = le16_to_cpu(*((__le16 *)skb->data));
  970. if (type & P54_HDR_FLAG_CONTROL)
  971. return p54_rx_control(dev, skb);
  972. else
  973. return p54_rx_data(dev, skb);
  974. }
  975. EXPORT_SYMBOL_GPL(p54_rx);
  976. /*
  977. * So, the firmware is somewhat stupid and doesn't know what places in its
  978. * memory incoming data should go to. By poking around in the firmware, we
  979. * can find some unused memory to upload our packets to. However, data that we
  980. * want the card to TX needs to stay intact until the card has told us that
  981. * it is done with it. This function finds empty places we can upload to and
  982. * marks allocated areas as reserved if necessary. p54_rx_frame_sent or
  983. * p54_free_skb frees allocated areas.
  984. */
  985. static int p54_assign_address(struct ieee80211_hw *dev, struct sk_buff *skb,
  986. struct p54_hdr *data, u32 len)
  987. {
  988. struct p54_common *priv = dev->priv;
  989. struct sk_buff *entry;
  990. struct sk_buff *target_skb = NULL;
  991. struct ieee80211_tx_info *info;
  992. struct p54_tx_info *range;
  993. u32 last_addr = priv->rx_start;
  994. u32 largest_hole = 0;
  995. u32 target_addr = priv->rx_start;
  996. unsigned long flags;
  997. unsigned int left;
  998. len = (len + priv->headroom + priv->tailroom + 3) & ~0x3;
  999. if (!skb)
  1000. return -EINVAL;
  1001. spin_lock_irqsave(&priv->tx_queue.lock, flags);
  1002. left = skb_queue_len(&priv->tx_queue);
  1003. if (unlikely(left >= 28)) {
  1004. /*
  1005. * The tx_queue is nearly full!
  1006. * We have throttle normal data traffic, because we must
  1007. * have a few spare slots for control frames left.
  1008. */
  1009. ieee80211_stop_queues(dev);
  1010. queue_delayed_work(dev->workqueue, &priv->work,
  1011. msecs_to_jiffies(P54_TX_TIMEOUT));
  1012. if (unlikely(left == 32)) {
  1013. /*
  1014. * The tx_queue is now really full.
  1015. *
  1016. * TODO: check if the device has crashed and reset it.
  1017. */
  1018. spin_unlock_irqrestore(&priv->tx_queue.lock, flags);
  1019. return -ENOSPC;
  1020. }
  1021. }
  1022. entry = priv->tx_queue.next;
  1023. while (left--) {
  1024. u32 hole_size;
  1025. info = IEEE80211_SKB_CB(entry);
  1026. range = (void *)info->rate_driver_data;
  1027. hole_size = range->start_addr - last_addr;
  1028. if (!target_skb && hole_size >= len) {
  1029. target_skb = entry->prev;
  1030. hole_size -= len;
  1031. target_addr = last_addr;
  1032. }
  1033. largest_hole = max(largest_hole, hole_size);
  1034. last_addr = range->end_addr;
  1035. entry = entry->next;
  1036. }
  1037. if (!target_skb && priv->rx_end - last_addr >= len) {
  1038. target_skb = priv->tx_queue.prev;
  1039. largest_hole = max(largest_hole, priv->rx_end - last_addr - len);
  1040. if (!skb_queue_empty(&priv->tx_queue)) {
  1041. info = IEEE80211_SKB_CB(target_skb);
  1042. range = (void *)info->rate_driver_data;
  1043. target_addr = range->end_addr;
  1044. }
  1045. } else
  1046. largest_hole = max(largest_hole, priv->rx_end - last_addr);
  1047. if (!target_skb) {
  1048. spin_unlock_irqrestore(&priv->tx_queue.lock, flags);
  1049. ieee80211_stop_queues(dev);
  1050. return -ENOSPC;
  1051. }
  1052. info = IEEE80211_SKB_CB(skb);
  1053. range = (void *)info->rate_driver_data;
  1054. range->start_addr = target_addr;
  1055. range->end_addr = target_addr + len;
  1056. __skb_queue_after(&priv->tx_queue, target_skb, skb);
  1057. spin_unlock_irqrestore(&priv->tx_queue.lock, flags);
  1058. if (largest_hole < priv->headroom + sizeof(struct p54_hdr) +
  1059. 48 + IEEE80211_MAX_RTS_THRESHOLD + priv->tailroom)
  1060. ieee80211_stop_queues(dev);
  1061. data->req_id = cpu_to_le32(target_addr + priv->headroom);
  1062. return 0;
  1063. }
  1064. static struct sk_buff *p54_alloc_skb(struct ieee80211_hw *dev, u16 hdr_flags,
  1065. u16 payload_len, u16 type, gfp_t memflags)
  1066. {
  1067. struct p54_common *priv = dev->priv;
  1068. struct p54_hdr *hdr;
  1069. struct sk_buff *skb;
  1070. size_t frame_len = sizeof(*hdr) + payload_len;
  1071. if (frame_len > P54_MAX_CTRL_FRAME_LEN)
  1072. return NULL;
  1073. skb = __dev_alloc_skb(priv->tx_hdr_len + frame_len, memflags);
  1074. if (!skb)
  1075. return NULL;
  1076. skb_reserve(skb, priv->tx_hdr_len);
  1077. hdr = (struct p54_hdr *) skb_put(skb, sizeof(*hdr));
  1078. hdr->flags = cpu_to_le16(hdr_flags);
  1079. hdr->len = cpu_to_le16(payload_len);
  1080. hdr->type = cpu_to_le16(type);
  1081. hdr->tries = hdr->rts_tries = 0;
  1082. if (p54_assign_address(dev, skb, hdr, frame_len)) {
  1083. kfree_skb(skb);
  1084. return NULL;
  1085. }
  1086. return skb;
  1087. }
  1088. int p54_read_eeprom(struct ieee80211_hw *dev)
  1089. {
  1090. struct p54_common *priv = dev->priv;
  1091. struct p54_eeprom_lm86 *eeprom_hdr;
  1092. struct sk_buff *skb;
  1093. size_t eeprom_size = 0x2020, offset = 0, blocksize, maxblocksize;
  1094. int ret = -ENOMEM;
  1095. void *eeprom = NULL;
  1096. maxblocksize = EEPROM_READBACK_LEN;
  1097. if (priv->fw_var >= 0x509)
  1098. maxblocksize -= 0xc;
  1099. else
  1100. maxblocksize -= 0x4;
  1101. skb = p54_alloc_skb(dev, P54_HDR_FLAG_CONTROL, sizeof(*eeprom_hdr) +
  1102. maxblocksize, P54_CONTROL_TYPE_EEPROM_READBACK,
  1103. GFP_KERNEL);
  1104. if (!skb)
  1105. goto free;
  1106. priv->eeprom = kzalloc(EEPROM_READBACK_LEN, GFP_KERNEL);
  1107. if (!priv->eeprom)
  1108. goto free;
  1109. eeprom = kzalloc(eeprom_size, GFP_KERNEL);
  1110. if (!eeprom)
  1111. goto free;
  1112. eeprom_hdr = (struct p54_eeprom_lm86 *) skb_put(skb,
  1113. sizeof(*eeprom_hdr) + maxblocksize);
  1114. while (eeprom_size) {
  1115. blocksize = min(eeprom_size, maxblocksize);
  1116. if (priv->fw_var < 0x509) {
  1117. eeprom_hdr->v1.offset = cpu_to_le16(offset);
  1118. eeprom_hdr->v1.len = cpu_to_le16(blocksize);
  1119. } else {
  1120. eeprom_hdr->v2.offset = cpu_to_le32(offset);
  1121. eeprom_hdr->v2.len = cpu_to_le16(blocksize);
  1122. eeprom_hdr->v2.magic2 = 0xf;
  1123. memcpy(eeprom_hdr->v2.magic, (const char *)"LOCK", 4);
  1124. }
  1125. priv->tx(dev, skb);
  1126. if (!wait_for_completion_interruptible_timeout(&priv->eeprom_comp, HZ)) {
  1127. printk(KERN_ERR "%s: device does not respond!\n",
  1128. wiphy_name(dev->wiphy));
  1129. ret = -EBUSY;
  1130. goto free;
  1131. }
  1132. memcpy(eeprom + offset, priv->eeprom, blocksize);
  1133. offset += blocksize;
  1134. eeprom_size -= blocksize;
  1135. }
  1136. ret = p54_parse_eeprom(dev, eeprom, offset);
  1137. free:
  1138. kfree(priv->eeprom);
  1139. priv->eeprom = NULL;
  1140. p54_free_skb(dev, skb);
  1141. kfree(eeprom);
  1142. return ret;
  1143. }
  1144. EXPORT_SYMBOL_GPL(p54_read_eeprom);
  1145. static int p54_set_tim(struct ieee80211_hw *dev, struct ieee80211_sta *sta,
  1146. bool set)
  1147. {
  1148. struct p54_common *priv = dev->priv;
  1149. struct sk_buff *skb;
  1150. struct p54_tim *tim;
  1151. skb = p54_alloc_skb(dev, P54_HDR_FLAG_CONTROL_OPSET, sizeof(*tim),
  1152. P54_CONTROL_TYPE_TIM, GFP_ATOMIC);
  1153. if (!skb)
  1154. return -ENOMEM;
  1155. tim = (struct p54_tim *) skb_put(skb, sizeof(*tim));
  1156. tim->count = 1;
  1157. tim->entry[0] = cpu_to_le16(set ? (sta->aid | 0x8000) : sta->aid);
  1158. priv->tx(dev, skb);
  1159. return 0;
  1160. }
  1161. static int p54_sta_unlock(struct ieee80211_hw *dev, u8 *addr)
  1162. {
  1163. struct p54_common *priv = dev->priv;
  1164. struct sk_buff *skb;
  1165. struct p54_sta_unlock *sta;
  1166. skb = p54_alloc_skb(dev, P54_HDR_FLAG_CONTROL_OPSET, sizeof(*sta),
  1167. P54_CONTROL_TYPE_PSM_STA_UNLOCK, GFP_ATOMIC);
  1168. if (!skb)
  1169. return -ENOMEM;
  1170. sta = (struct p54_sta_unlock *)skb_put(skb, sizeof(*sta));
  1171. memcpy(sta->addr, addr, ETH_ALEN);
  1172. priv->tx(dev, skb);
  1173. return 0;
  1174. }
  1175. static void p54_sta_notify(struct ieee80211_hw *dev, struct ieee80211_vif *vif,
  1176. enum sta_notify_cmd notify_cmd,
  1177. struct ieee80211_sta *sta)
  1178. {
  1179. switch (notify_cmd) {
  1180. case STA_NOTIFY_ADD:
  1181. case STA_NOTIFY_REMOVE:
  1182. /*
  1183. * Notify the firmware that we don't want or we don't
  1184. * need to buffer frames for this station anymore.
  1185. */
  1186. p54_sta_unlock(dev, sta->addr);
  1187. break;
  1188. case STA_NOTIFY_AWAKE:
  1189. /* update the firmware's filter table */
  1190. p54_sta_unlock(dev, sta->addr);
  1191. break;
  1192. default:
  1193. break;
  1194. }
  1195. }
  1196. static int p54_tx_cancel(struct ieee80211_hw *dev, struct sk_buff *entry)
  1197. {
  1198. struct p54_common *priv = dev->priv;
  1199. struct sk_buff *skb;
  1200. struct p54_hdr *hdr;
  1201. struct p54_txcancel *cancel;
  1202. skb = p54_alloc_skb(dev, P54_HDR_FLAG_CONTROL_OPSET, sizeof(*cancel),
  1203. P54_CONTROL_TYPE_TXCANCEL, GFP_ATOMIC);
  1204. if (!skb)
  1205. return -ENOMEM;
  1206. hdr = (void *)entry->data;
  1207. cancel = (struct p54_txcancel *)skb_put(skb, sizeof(*cancel));
  1208. cancel->req_id = hdr->req_id;
  1209. priv->tx(dev, skb);
  1210. return 0;
  1211. }
  1212. static int p54_tx_fill(struct ieee80211_hw *dev, struct sk_buff *skb,
  1213. struct ieee80211_tx_info *info, u8 *queue, size_t *extra_len,
  1214. u16 *flags, u16 *aid)
  1215. {
  1216. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  1217. struct p54_common *priv = dev->priv;
  1218. int ret = 1;
  1219. switch (priv->mode) {
  1220. case NL80211_IFTYPE_MONITOR:
  1221. /*
  1222. * We have to set P54_HDR_FLAG_DATA_OUT_PROMISC for
  1223. * every frame in promiscuous/monitor mode.
  1224. * see STSW45x0C LMAC API - page 12.
  1225. */
  1226. *aid = 0;
  1227. *flags = P54_HDR_FLAG_DATA_OUT_PROMISC;
  1228. *queue += P54_QUEUE_DATA;
  1229. break;
  1230. case NL80211_IFTYPE_STATION:
  1231. *aid = 1;
  1232. if (unlikely(ieee80211_is_mgmt(hdr->frame_control))) {
  1233. *queue = P54_QUEUE_MGMT;
  1234. ret = 0;
  1235. } else
  1236. *queue += P54_QUEUE_DATA;
  1237. break;
  1238. case NL80211_IFTYPE_AP:
  1239. case NL80211_IFTYPE_ADHOC:
  1240. case NL80211_IFTYPE_MESH_POINT:
  1241. if (info->flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM) {
  1242. *aid = 0;
  1243. *queue = P54_QUEUE_CAB;
  1244. return 0;
  1245. }
  1246. if (unlikely(ieee80211_is_mgmt(hdr->frame_control))) {
  1247. if (ieee80211_is_probe_resp(hdr->frame_control)) {
  1248. *aid = 0;
  1249. *queue = P54_QUEUE_MGMT;
  1250. *flags = P54_HDR_FLAG_DATA_OUT_TIMESTAMP |
  1251. P54_HDR_FLAG_DATA_OUT_NOCANCEL;
  1252. return 0;
  1253. } else if (ieee80211_is_beacon(hdr->frame_control)) {
  1254. *aid = 0;
  1255. if (info->flags & IEEE80211_TX_CTL_INJECTED) {
  1256. /*
  1257. * Injecting beacons on top of a AP is
  1258. * not a good idea... nevertheless,
  1259. * it should be doable.
  1260. */
  1261. *queue += P54_QUEUE_DATA;
  1262. return 1;
  1263. }
  1264. *flags = P54_HDR_FLAG_DATA_OUT_TIMESTAMP;
  1265. *queue = P54_QUEUE_BEACON;
  1266. *extra_len = IEEE80211_MAX_TIM_LEN;
  1267. return 0;
  1268. } else {
  1269. *queue = P54_QUEUE_MGMT;
  1270. ret = 0;
  1271. }
  1272. } else
  1273. *queue += P54_QUEUE_DATA;
  1274. if (info->control.sta)
  1275. *aid = info->control.sta->aid;
  1276. else
  1277. *flags |= P54_HDR_FLAG_DATA_OUT_NOCANCEL;
  1278. break;
  1279. }
  1280. return ret;
  1281. }
  1282. static u8 p54_convert_algo(enum ieee80211_key_alg alg)
  1283. {
  1284. switch (alg) {
  1285. case ALG_WEP:
  1286. return P54_CRYPTO_WEP;
  1287. case ALG_TKIP:
  1288. return P54_CRYPTO_TKIPMICHAEL;
  1289. case ALG_CCMP:
  1290. return P54_CRYPTO_AESCCMP;
  1291. default:
  1292. return 0;
  1293. }
  1294. }
  1295. static int p54_tx(struct ieee80211_hw *dev, struct sk_buff *skb)
  1296. {
  1297. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  1298. struct ieee80211_tx_queue_stats *current_queue;
  1299. struct p54_common *priv = dev->priv;
  1300. struct p54_hdr *hdr;
  1301. struct p54_tx_data *txhdr;
  1302. size_t padding, len, tim_len = 0;
  1303. int i, j, ridx, ret;
  1304. u16 hdr_flags = 0, aid = 0;
  1305. u8 rate, queue, crypt_offset = 0;
  1306. u8 cts_rate = 0x20;
  1307. u8 rc_flags;
  1308. u8 calculated_tries[4];
  1309. u8 nrates = 0, nremaining = 8;
  1310. queue = skb_get_queue_mapping(skb);
  1311. ret = p54_tx_fill(dev, skb, info, &queue, &tim_len, &hdr_flags, &aid);
  1312. current_queue = &priv->tx_stats[queue];
  1313. if (unlikely((current_queue->len > current_queue->limit) && ret))
  1314. return NETDEV_TX_BUSY;
  1315. current_queue->len++;
  1316. current_queue->count++;
  1317. if ((current_queue->len == current_queue->limit) && ret)
  1318. ieee80211_stop_queue(dev, skb_get_queue_mapping(skb));
  1319. padding = (unsigned long)(skb->data - (sizeof(*hdr) + sizeof(*txhdr))) & 3;
  1320. len = skb->len;
  1321. if (info->control.hw_key) {
  1322. crypt_offset = ieee80211_get_hdrlen_from_skb(skb);
  1323. if (info->control.hw_key->alg == ALG_TKIP) {
  1324. u8 *iv = (u8 *)(skb->data + crypt_offset);
  1325. /*
  1326. * The firmware excepts that the IV has to have
  1327. * this special format
  1328. */
  1329. iv[1] = iv[0];
  1330. iv[0] = iv[2];
  1331. iv[2] = 0;
  1332. }
  1333. }
  1334. txhdr = (struct p54_tx_data *) skb_push(skb, sizeof(*txhdr) + padding);
  1335. hdr = (struct p54_hdr *) skb_push(skb, sizeof(*hdr));
  1336. if (padding)
  1337. hdr_flags |= P54_HDR_FLAG_DATA_ALIGN;
  1338. hdr->type = cpu_to_le16(aid);
  1339. hdr->rts_tries = info->control.rates[0].count;
  1340. /*
  1341. * we register the rates in perfect order, and
  1342. * RTS/CTS won't happen on 5 GHz
  1343. */
  1344. cts_rate = info->control.rts_cts_rate_idx;
  1345. memset(&txhdr->rateset, 0, sizeof(txhdr->rateset));
  1346. /* see how many rates got used */
  1347. for (i = 0; i < 4; i++) {
  1348. if (info->control.rates[i].idx < 0)
  1349. break;
  1350. nrates++;
  1351. }
  1352. /* limit tries to 8/nrates per rate */
  1353. for (i = 0; i < nrates; i++) {
  1354. /*
  1355. * The magic expression here is equivalent to 8/nrates for
  1356. * all values that matter, but avoids division and jumps.
  1357. * Note that nrates can only take the values 1 through 4.
  1358. */
  1359. calculated_tries[i] = min_t(int, ((15 >> nrates) | 1) + 1,
  1360. info->control.rates[i].count);
  1361. nremaining -= calculated_tries[i];
  1362. }
  1363. /* if there are tries left, distribute from back to front */
  1364. for (i = nrates - 1; nremaining > 0 && i >= 0; i--) {
  1365. int tmp = info->control.rates[i].count - calculated_tries[i];
  1366. if (tmp <= 0)
  1367. continue;
  1368. /* RC requested more tries at this rate */
  1369. tmp = min_t(int, tmp, nremaining);
  1370. calculated_tries[i] += tmp;
  1371. nremaining -= tmp;
  1372. }
  1373. ridx = 0;
  1374. for (i = 0; i < nrates && ridx < 8; i++) {
  1375. /* we register the rates in perfect order */
  1376. rate = info->control.rates[i].idx;
  1377. if (info->band == IEEE80211_BAND_5GHZ)
  1378. rate += 4;
  1379. /* store the count we actually calculated for TX status */
  1380. info->control.rates[i].count = calculated_tries[i];
  1381. rc_flags = info->control.rates[i].flags;
  1382. if (rc_flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE) {
  1383. rate |= 0x10;
  1384. cts_rate |= 0x10;
  1385. }
  1386. if (rc_flags & IEEE80211_TX_RC_USE_RTS_CTS)
  1387. rate |= 0x40;
  1388. else if (rc_flags & IEEE80211_TX_RC_USE_CTS_PROTECT)
  1389. rate |= 0x20;
  1390. for (j = 0; j < calculated_tries[i] && ridx < 8; j++) {
  1391. txhdr->rateset[ridx] = rate;
  1392. ridx++;
  1393. }
  1394. }
  1395. if (info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ)
  1396. hdr_flags |= P54_HDR_FLAG_DATA_OUT_SEQNR;
  1397. /* TODO: enable bursting */
  1398. hdr->flags = cpu_to_le16(hdr_flags);
  1399. hdr->tries = ridx;
  1400. txhdr->rts_rate_idx = 0;
  1401. if (info->control.hw_key) {
  1402. txhdr->key_type = p54_convert_algo(info->control.hw_key->alg);
  1403. txhdr->key_len = min((u8)16, info->control.hw_key->keylen);
  1404. memcpy(txhdr->key, info->control.hw_key->key, txhdr->key_len);
  1405. if (info->control.hw_key->alg == ALG_TKIP) {
  1406. if (unlikely(skb_tailroom(skb) < 12))
  1407. goto err;
  1408. /* reserve space for the MIC key */
  1409. len += 8;
  1410. memcpy(skb_put(skb, 8), &(info->control.hw_key->key
  1411. [NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY]), 8);
  1412. }
  1413. /* reserve some space for ICV */
  1414. len += info->control.hw_key->icv_len;
  1415. memset(skb_put(skb, info->control.hw_key->icv_len), 0,
  1416. info->control.hw_key->icv_len);
  1417. } else {
  1418. txhdr->key_type = 0;
  1419. txhdr->key_len = 0;
  1420. }
  1421. txhdr->crypt_offset = crypt_offset;
  1422. txhdr->hw_queue = queue;
  1423. txhdr->backlog = current_queue->len;
  1424. memset(txhdr->durations, 0, sizeof(txhdr->durations));
  1425. txhdr->tx_antenna = ((info->antenna_sel_tx == 0) ?
  1426. 2 : info->antenna_sel_tx - 1) & priv->tx_diversity_mask;
  1427. if (priv->rxhw == PDR_SYNTH_FRONTEND_LONGBOW) {
  1428. txhdr->longbow.cts_rate = cts_rate;
  1429. txhdr->longbow.output_power = cpu_to_le16(priv->output_power);
  1430. } else {
  1431. txhdr->normal.output_power = priv->output_power;
  1432. txhdr->normal.cts_rate = cts_rate;
  1433. }
  1434. if (padding)
  1435. txhdr->align[0] = padding;
  1436. hdr->len = cpu_to_le16(len);
  1437. /* modifies skb->cb and with it info, so must be last! */
  1438. if (unlikely(p54_assign_address(dev, skb, hdr, skb->len + tim_len)))
  1439. goto err;
  1440. priv->tx(dev, skb);
  1441. queue_delayed_work(dev->workqueue, &priv->work,
  1442. msecs_to_jiffies(P54_TX_FRAME_LIFETIME));
  1443. return NETDEV_TX_OK;
  1444. err:
  1445. skb_pull(skb, sizeof(*hdr) + sizeof(*txhdr) + padding);
  1446. current_queue->len--;
  1447. current_queue->count--;
  1448. return NETDEV_TX_BUSY;
  1449. }
  1450. static int p54_setup_mac(struct ieee80211_hw *dev)
  1451. {
  1452. struct p54_common *priv = dev->priv;
  1453. struct sk_buff *skb;
  1454. struct p54_setup_mac *setup;
  1455. u16 mode;
  1456. skb = p54_alloc_skb(dev, P54_HDR_FLAG_CONTROL_OPSET, sizeof(*setup),
  1457. P54_CONTROL_TYPE_SETUP, GFP_ATOMIC);
  1458. if (!skb)
  1459. return -ENOMEM;
  1460. setup = (struct p54_setup_mac *) skb_put(skb, sizeof(*setup));
  1461. if (dev->conf.radio_enabled) {
  1462. switch (priv->mode) {
  1463. case NL80211_IFTYPE_STATION:
  1464. mode = P54_FILTER_TYPE_STATION;
  1465. break;
  1466. case NL80211_IFTYPE_AP:
  1467. mode = P54_FILTER_TYPE_AP;
  1468. break;
  1469. case NL80211_IFTYPE_ADHOC:
  1470. case NL80211_IFTYPE_MESH_POINT:
  1471. mode = P54_FILTER_TYPE_IBSS;
  1472. break;
  1473. case NL80211_IFTYPE_MONITOR:
  1474. mode = P54_FILTER_TYPE_PROMISCUOUS;
  1475. break;
  1476. default:
  1477. mode = P54_FILTER_TYPE_HIBERNATE;
  1478. break;
  1479. }
  1480. /*
  1481. * "TRANSPARENT and PROMISCUOUS are mutually exclusive"
  1482. * STSW45X0C LMAC API - page 12
  1483. */
  1484. if (((priv->filter_flags & FIF_PROMISC_IN_BSS) ||
  1485. (priv->filter_flags & FIF_OTHER_BSS)) &&
  1486. (mode != P54_FILTER_TYPE_PROMISCUOUS))
  1487. mode |= P54_FILTER_TYPE_TRANSPARENT;
  1488. } else
  1489. mode = P54_FILTER_TYPE_HIBERNATE;
  1490. setup->mac_mode = cpu_to_le16(mode);
  1491. memcpy(setup->mac_addr, priv->mac_addr, ETH_ALEN);
  1492. memcpy(setup->bssid, priv->bssid, ETH_ALEN);
  1493. setup->rx_antenna = 2 & priv->rx_diversity_mask; /* automatic */
  1494. setup->rx_align = 0;
  1495. if (priv->fw_var < 0x500) {
  1496. setup->v1.basic_rate_mask = cpu_to_le32(priv->basic_rate_mask);
  1497. memset(setup->v1.rts_rates, 0, 8);
  1498. setup->v1.rx_addr = cpu_to_le32(priv->rx_end);
  1499. setup->v1.max_rx = cpu_to_le16(priv->rx_mtu);
  1500. setup->v1.rxhw = cpu_to_le16(priv->rxhw);
  1501. setup->v1.wakeup_timer = cpu_to_le16(priv->wakeup_timer);
  1502. setup->v1.unalloc0 = cpu_to_le16(0);
  1503. } else {
  1504. setup->v2.rx_addr = cpu_to_le32(priv->rx_end);
  1505. setup->v2.max_rx = cpu_to_le16(priv->rx_mtu);
  1506. setup->v2.rxhw = cpu_to_le16(priv->rxhw);
  1507. setup->v2.timer = cpu_to_le16(priv->wakeup_timer);
  1508. setup->v2.truncate = cpu_to_le16(48896);
  1509. setup->v2.basic_rate_mask = cpu_to_le32(priv->basic_rate_mask);
  1510. setup->v2.sbss_offset = 0;
  1511. setup->v2.mcast_window = 0;
  1512. setup->v2.rx_rssi_threshold = 0;
  1513. setup->v2.rx_ed_threshold = 0;
  1514. setup->v2.ref_clock = cpu_to_le32(644245094);
  1515. setup->v2.lpf_bandwidth = cpu_to_le16(65535);
  1516. setup->v2.osc_start_delay = cpu_to_le16(65535);
  1517. }
  1518. priv->tx(dev, skb);
  1519. return 0;
  1520. }
  1521. static int p54_scan(struct ieee80211_hw *dev, u16 mode, u16 dwell)
  1522. {
  1523. struct p54_common *priv = dev->priv;
  1524. struct sk_buff *skb;
  1525. struct p54_hdr *hdr;
  1526. struct p54_scan_head *head;
  1527. struct p54_iq_autocal_entry *iq_autocal;
  1528. union p54_scan_body_union *body;
  1529. struct p54_scan_tail_rate *rate;
  1530. struct pda_rssi_cal_entry *rssi;
  1531. unsigned int i;
  1532. void *entry;
  1533. int band = dev->conf.channel->band;
  1534. __le16 freq = cpu_to_le16(dev->conf.channel->center_freq);
  1535. skb = p54_alloc_skb(dev, P54_HDR_FLAG_CONTROL_OPSET, sizeof(*head) +
  1536. 2 + sizeof(*iq_autocal) + sizeof(*body) +
  1537. sizeof(*rate) + 2 * sizeof(*rssi),
  1538. P54_CONTROL_TYPE_SCAN, GFP_ATOMIC);
  1539. if (!skb)
  1540. return -ENOMEM;
  1541. head = (struct p54_scan_head *) skb_put(skb, sizeof(*head));
  1542. memset(head->scan_params, 0, sizeof(head->scan_params));
  1543. head->mode = cpu_to_le16(mode);
  1544. head->dwell = cpu_to_le16(dwell);
  1545. head->freq = freq;
  1546. if (priv->rxhw == PDR_SYNTH_FRONTEND_LONGBOW) {
  1547. __le16 *pa_power_points = (__le16 *) skb_put(skb, 2);
  1548. *pa_power_points = cpu_to_le16(0x0c);
  1549. }
  1550. iq_autocal = (void *) skb_put(skb, sizeof(*iq_autocal));
  1551. for (i = 0; i < priv->iq_autocal_len; i++) {
  1552. if (priv->iq_autocal[i].freq != freq)
  1553. continue;
  1554. memcpy(iq_autocal, &priv->iq_autocal[i].params,
  1555. sizeof(struct p54_iq_autocal_entry));
  1556. break;
  1557. }
  1558. if (i == priv->iq_autocal_len)
  1559. goto err;
  1560. if (priv->rxhw == PDR_SYNTH_FRONTEND_LONGBOW)
  1561. body = (void *) skb_put(skb, sizeof(body->longbow));
  1562. else
  1563. body = (void *) skb_put(skb, sizeof(body->normal));
  1564. for (i = 0; i < priv->output_limit->entries; i++) {
  1565. __le16 *entry_freq = (void *) (priv->output_limit->data +
  1566. priv->output_limit->entry_size * i);
  1567. if (*entry_freq != freq)
  1568. continue;
  1569. if (priv->rxhw == PDR_SYNTH_FRONTEND_LONGBOW) {
  1570. memcpy(&body->longbow.power_limits,
  1571. (void *) entry_freq + sizeof(__le16),
  1572. priv->output_limit->entry_size);
  1573. } else {
  1574. struct pda_channel_output_limit *limits =
  1575. (void *) entry_freq;
  1576. body->normal.val_barker = 0x38;
  1577. body->normal.val_bpsk = body->normal.dup_bpsk =
  1578. limits->val_bpsk;
  1579. body->normal.val_qpsk = body->normal.dup_qpsk =
  1580. limits->val_qpsk;
  1581. body->normal.val_16qam = body->normal.dup_16qam =
  1582. limits->val_16qam;
  1583. body->normal.val_64qam = body->normal.dup_64qam =
  1584. limits->val_64qam;
  1585. }
  1586. break;
  1587. }
  1588. if (i == priv->output_limit->entries)
  1589. goto err;
  1590. entry = (void *)(priv->curve_data->data + priv->curve_data->offset);
  1591. for (i = 0; i < priv->curve_data->entries; i++) {
  1592. if (*((__le16 *)entry) != freq) {
  1593. entry += priv->curve_data->entry_size;
  1594. continue;
  1595. }
  1596. if (priv->rxhw == PDR_SYNTH_FRONTEND_LONGBOW) {
  1597. memcpy(&body->longbow.curve_data,
  1598. (void *) entry + sizeof(__le16),
  1599. priv->curve_data->entry_size);
  1600. } else {
  1601. struct p54_scan_body *chan = &body->normal;
  1602. struct pda_pa_curve_data *curve_data =
  1603. (void *) priv->curve_data->data;
  1604. entry += sizeof(__le16);
  1605. chan->pa_points_per_curve = 8;
  1606. memset(chan->curve_data, 0, sizeof(*chan->curve_data));
  1607. memcpy(chan->curve_data, entry,
  1608. sizeof(struct p54_pa_curve_data_sample) *
  1609. min((u8)8, curve_data->points_per_channel));
  1610. }
  1611. break;
  1612. }
  1613. if (i == priv->curve_data->entries)
  1614. goto err;
  1615. if ((priv->fw_var >= 0x500) && (priv->fw_var < 0x509)) {
  1616. rate = (void *) skb_put(skb, sizeof(*rate));
  1617. rate->basic_rate_mask = cpu_to_le32(priv->basic_rate_mask);
  1618. for (i = 0; i < sizeof(rate->rts_rates); i++)
  1619. rate->rts_rates[i] = i;
  1620. }
  1621. rssi = (struct pda_rssi_cal_entry *) skb_put(skb, sizeof(*rssi));
  1622. rssi->mul = cpu_to_le16(priv->rssical_db[band].mul);
  1623. rssi->add = cpu_to_le16(priv->rssical_db[band].add);
  1624. if (priv->rxhw == PDR_SYNTH_FRONTEND_LONGBOW) {
  1625. /* Longbow frontend needs ever more */
  1626. rssi = (void *) skb_put(skb, sizeof(*rssi));
  1627. rssi->mul = cpu_to_le16(priv->rssical_db[band].longbow_unkn);
  1628. rssi->add = cpu_to_le16(priv->rssical_db[band].longbow_unk2);
  1629. }
  1630. if (priv->fw_var >= 0x509) {
  1631. rate = (void *) skb_put(skb, sizeof(*rate));
  1632. rate->basic_rate_mask = cpu_to_le32(priv->basic_rate_mask);
  1633. for (i = 0; i < sizeof(rate->rts_rates); i++)
  1634. rate->rts_rates[i] = i;
  1635. }
  1636. hdr = (struct p54_hdr *) skb->data;
  1637. hdr->len = cpu_to_le16(skb->len - sizeof(*hdr));
  1638. priv->tx(dev, skb);
  1639. return 0;
  1640. err:
  1641. printk(KERN_ERR "%s: frequency change failed\n", wiphy_name(dev->wiphy));
  1642. p54_free_skb(dev, skb);
  1643. return -EINVAL;
  1644. }
  1645. static int p54_set_leds(struct ieee80211_hw *dev)
  1646. {
  1647. struct p54_common *priv = dev->priv;
  1648. struct sk_buff *skb;
  1649. struct p54_led *led;
  1650. skb = p54_alloc_skb(dev, P54_HDR_FLAG_CONTROL_OPSET, sizeof(*led),
  1651. P54_CONTROL_TYPE_LED, GFP_ATOMIC);
  1652. if (!skb)
  1653. return -ENOMEM;
  1654. led = (struct p54_led *) skb_put(skb, sizeof(*led));
  1655. led->flags = cpu_to_le16(0x0003);
  1656. led->mask[0] = led->mask[1] = cpu_to_le16(priv->softled_state);
  1657. led->delay[0] = cpu_to_le16(1);
  1658. led->delay[1] = cpu_to_le16(0);
  1659. priv->tx(dev, skb);
  1660. return 0;
  1661. }
  1662. #define P54_SET_QUEUE(queue, ai_fs, cw_min, cw_max, _txop) \
  1663. do { \
  1664. queue.aifs = cpu_to_le16(ai_fs); \
  1665. queue.cwmin = cpu_to_le16(cw_min); \
  1666. queue.cwmax = cpu_to_le16(cw_max); \
  1667. queue.txop = cpu_to_le16(_txop); \
  1668. } while(0)
  1669. static int p54_set_edcf(struct ieee80211_hw *dev)
  1670. {
  1671. struct p54_common *priv = dev->priv;
  1672. struct sk_buff *skb;
  1673. struct p54_edcf *edcf;
  1674. skb = p54_alloc_skb(dev, P54_HDR_FLAG_CONTROL_OPSET, sizeof(*edcf),
  1675. P54_CONTROL_TYPE_DCFINIT, GFP_ATOMIC);
  1676. if (!skb)
  1677. return -ENOMEM;
  1678. edcf = (struct p54_edcf *)skb_put(skb, sizeof(*edcf));
  1679. if (priv->use_short_slot) {
  1680. edcf->slottime = 9;
  1681. edcf->sifs = 0x10;
  1682. edcf->eofpad = 0x00;
  1683. } else {
  1684. edcf->slottime = 20;
  1685. edcf->sifs = 0x0a;
  1686. edcf->eofpad = 0x06;
  1687. }
  1688. /* (see prism54/isl_oid.h for further details) */
  1689. edcf->frameburst = cpu_to_le16(0);
  1690. edcf->round_trip_delay = cpu_to_le16(0);
  1691. edcf->flags = 0;
  1692. memset(edcf->mapping, 0, sizeof(edcf->mapping));
  1693. memcpy(edcf->queue, priv->qos_params, sizeof(edcf->queue));
  1694. priv->tx(dev, skb);
  1695. return 0;
  1696. }
  1697. static int p54_set_ps(struct ieee80211_hw *dev)
  1698. {
  1699. struct p54_common *priv = dev->priv;
  1700. struct sk_buff *skb;
  1701. struct p54_psm *psm;
  1702. u16 mode;
  1703. int i;
  1704. if (dev->conf.flags & IEEE80211_CONF_PS)
  1705. mode = P54_PSM | P54_PSM_DTIM | P54_PSM_MCBC;
  1706. else
  1707. mode = P54_PSM_CAM;
  1708. skb = p54_alloc_skb(dev, P54_HDR_FLAG_CONTROL_OPSET, sizeof(*psm),
  1709. P54_CONTROL_TYPE_PSM, GFP_ATOMIC);
  1710. if (!skb)
  1711. return -ENOMEM;
  1712. psm = (struct p54_psm *)skb_put(skb, sizeof(*psm));
  1713. psm->mode = cpu_to_le16(mode);
  1714. psm->aid = cpu_to_le16(priv->aid);
  1715. for (i = 0; i < ARRAY_SIZE(psm->intervals); i++) {
  1716. psm->intervals[i].interval =
  1717. cpu_to_le16(dev->conf.listen_interval);
  1718. psm->intervals[i].periods = cpu_to_le16(1);
  1719. }
  1720. psm->beacon_rssi_skip_max = 60;
  1721. psm->rssi_delta_threshold = 0;
  1722. psm->nr = 0;
  1723. priv->tx(dev, skb);
  1724. return 0;
  1725. }
  1726. static int p54_beacon_tim(struct sk_buff *skb)
  1727. {
  1728. /*
  1729. * the good excuse for this mess is ... the firmware.
  1730. * The dummy TIM MUST be at the end of the beacon frame,
  1731. * because it'll be overwritten!
  1732. */
  1733. struct ieee80211_mgmt *mgmt = (void *)skb->data;
  1734. u8 *pos, *end;
  1735. if (skb->len <= sizeof(mgmt))
  1736. return -EINVAL;
  1737. pos = (u8 *)mgmt->u.beacon.variable;
  1738. end = skb->data + skb->len;
  1739. while (pos < end) {
  1740. if (pos + 2 + pos[1] > end)
  1741. return -EINVAL;
  1742. if (pos[0] == WLAN_EID_TIM) {
  1743. u8 dtim_len = pos[1];
  1744. u8 dtim_period = pos[3];
  1745. u8 *next = pos + 2 + dtim_len;
  1746. if (dtim_len < 3)
  1747. return -EINVAL;
  1748. memmove(pos, next, end - next);
  1749. if (dtim_len > 3)
  1750. skb_trim(skb, skb->len - (dtim_len - 3));
  1751. pos = end - (dtim_len + 2);
  1752. /* add the dummy at the end */
  1753. pos[0] = WLAN_EID_TIM;
  1754. pos[1] = 3;
  1755. pos[2] = 0;
  1756. pos[3] = dtim_period;
  1757. pos[4] = 0;
  1758. return 0;
  1759. }
  1760. pos += 2 + pos[1];
  1761. }
  1762. return 0;
  1763. }
  1764. static int p54_beacon_update(struct ieee80211_hw *dev,
  1765. struct ieee80211_vif *vif)
  1766. {
  1767. struct p54_common *priv = dev->priv;
  1768. struct sk_buff *beacon;
  1769. int ret;
  1770. if (priv->cached_beacon) {
  1771. p54_tx_cancel(dev, priv->cached_beacon);
  1772. /* wait for the last beacon the be freed */
  1773. msleep(10);
  1774. }
  1775. beacon = ieee80211_beacon_get(dev, vif);
  1776. if (!beacon)
  1777. return -ENOMEM;
  1778. ret = p54_beacon_tim(beacon);
  1779. if (ret)
  1780. return ret;
  1781. ret = p54_tx(dev, beacon);
  1782. if (ret)
  1783. return ret;
  1784. priv->cached_beacon = beacon;
  1785. priv->tsf_high32 = 0;
  1786. priv->tsf_low32 = 0;
  1787. return 0;
  1788. }
  1789. static int p54_start(struct ieee80211_hw *dev)
  1790. {
  1791. struct p54_common *priv = dev->priv;
  1792. int err;
  1793. mutex_lock(&priv->conf_mutex);
  1794. err = priv->open(dev);
  1795. if (err)
  1796. goto out;
  1797. P54_SET_QUEUE(priv->qos_params[0], 0x0002, 0x0003, 0x0007, 47);
  1798. P54_SET_QUEUE(priv->qos_params[1], 0x0002, 0x0007, 0x000f, 94);
  1799. P54_SET_QUEUE(priv->qos_params[2], 0x0003, 0x000f, 0x03ff, 0);
  1800. P54_SET_QUEUE(priv->qos_params[3], 0x0007, 0x000f, 0x03ff, 0);
  1801. err = p54_set_edcf(dev);
  1802. if (err)
  1803. goto out;
  1804. memset(priv->bssid, ~0, ETH_ALEN);
  1805. priv->mode = NL80211_IFTYPE_MONITOR;
  1806. err = p54_setup_mac(dev);
  1807. if (err) {
  1808. priv->mode = NL80211_IFTYPE_UNSPECIFIED;
  1809. goto out;
  1810. }
  1811. queue_delayed_work(dev->workqueue, &priv->work, 0);
  1812. priv->softled_state = 0;
  1813. err = p54_set_leds(dev);
  1814. out:
  1815. mutex_unlock(&priv->conf_mutex);
  1816. return err;
  1817. }
  1818. static void p54_stop(struct ieee80211_hw *dev)
  1819. {
  1820. struct p54_common *priv = dev->priv;
  1821. struct sk_buff *skb;
  1822. mutex_lock(&priv->conf_mutex);
  1823. priv->mode = NL80211_IFTYPE_UNSPECIFIED;
  1824. priv->softled_state = 0;
  1825. p54_set_leds(dev);
  1826. cancel_delayed_work_sync(&priv->work);
  1827. if (priv->cached_beacon)
  1828. p54_tx_cancel(dev, priv->cached_beacon);
  1829. priv->stop(dev);
  1830. while ((skb = skb_dequeue(&priv->tx_queue)))
  1831. kfree_skb(skb);
  1832. priv->cached_beacon = NULL;
  1833. priv->tsf_high32 = priv->tsf_low32 = 0;
  1834. mutex_unlock(&priv->conf_mutex);
  1835. }
  1836. static int p54_add_interface(struct ieee80211_hw *dev,
  1837. struct ieee80211_if_init_conf *conf)
  1838. {
  1839. struct p54_common *priv = dev->priv;
  1840. mutex_lock(&priv->conf_mutex);
  1841. if (priv->mode != NL80211_IFTYPE_MONITOR) {
  1842. mutex_unlock(&priv->conf_mutex);
  1843. return -EOPNOTSUPP;
  1844. }
  1845. switch (conf->type) {
  1846. case NL80211_IFTYPE_STATION:
  1847. case NL80211_IFTYPE_ADHOC:
  1848. case NL80211_IFTYPE_AP:
  1849. case NL80211_IFTYPE_MESH_POINT:
  1850. priv->mode = conf->type;
  1851. break;
  1852. default:
  1853. mutex_unlock(&priv->conf_mutex);
  1854. return -EOPNOTSUPP;
  1855. }
  1856. memcpy(priv->mac_addr, conf->mac_addr, ETH_ALEN);
  1857. p54_setup_mac(dev);
  1858. mutex_unlock(&priv->conf_mutex);
  1859. return 0;
  1860. }
  1861. static void p54_remove_interface(struct ieee80211_hw *dev,
  1862. struct ieee80211_if_init_conf *conf)
  1863. {
  1864. struct p54_common *priv = dev->priv;
  1865. mutex_lock(&priv->conf_mutex);
  1866. if (priv->cached_beacon)
  1867. p54_tx_cancel(dev, priv->cached_beacon);
  1868. priv->mode = NL80211_IFTYPE_MONITOR;
  1869. memset(priv->mac_addr, 0, ETH_ALEN);
  1870. memset(priv->bssid, 0, ETH_ALEN);
  1871. p54_setup_mac(dev);
  1872. mutex_unlock(&priv->conf_mutex);
  1873. }
  1874. static int p54_config(struct ieee80211_hw *dev, u32 changed)
  1875. {
  1876. int ret = 0;
  1877. struct p54_common *priv = dev->priv;
  1878. struct ieee80211_conf *conf = &dev->conf;
  1879. mutex_lock(&priv->conf_mutex);
  1880. if (changed & IEEE80211_CONF_CHANGE_POWER)
  1881. priv->output_power = conf->power_level << 2;
  1882. if (changed & IEEE80211_CONF_CHANGE_RADIO_ENABLED) {
  1883. ret = p54_setup_mac(dev);
  1884. if (ret)
  1885. goto out;
  1886. }
  1887. if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
  1888. ret = p54_scan(dev, P54_SCAN_EXIT, 0);
  1889. if (ret)
  1890. goto out;
  1891. }
  1892. if (changed & IEEE80211_CONF_CHANGE_PS) {
  1893. ret = p54_set_ps(dev);
  1894. if (ret)
  1895. goto out;
  1896. }
  1897. out:
  1898. mutex_unlock(&priv->conf_mutex);
  1899. return ret;
  1900. }
  1901. static int p54_config_interface(struct ieee80211_hw *dev,
  1902. struct ieee80211_vif *vif,
  1903. struct ieee80211_if_conf *conf)
  1904. {
  1905. struct p54_common *priv = dev->priv;
  1906. int ret = 0;
  1907. mutex_lock(&priv->conf_mutex);
  1908. if (conf->changed & IEEE80211_IFCC_BSSID) {
  1909. memcpy(priv->bssid, conf->bssid, ETH_ALEN);
  1910. ret = p54_setup_mac(dev);
  1911. if (ret)
  1912. goto out;
  1913. }
  1914. if (conf->changed & IEEE80211_IFCC_BEACON) {
  1915. ret = p54_scan(dev, P54_SCAN_EXIT, 0);
  1916. if (ret)
  1917. goto out;
  1918. ret = p54_setup_mac(dev);
  1919. if (ret)
  1920. goto out;
  1921. ret = p54_beacon_update(dev, vif);
  1922. if (ret)
  1923. goto out;
  1924. ret = p54_set_edcf(dev);
  1925. if (ret)
  1926. goto out;
  1927. }
  1928. out:
  1929. mutex_unlock(&priv->conf_mutex);
  1930. return ret;
  1931. }
  1932. static void p54_configure_filter(struct ieee80211_hw *dev,
  1933. unsigned int changed_flags,
  1934. unsigned int *total_flags,
  1935. int mc_count, struct dev_mc_list *mclist)
  1936. {
  1937. struct p54_common *priv = dev->priv;
  1938. *total_flags &= FIF_PROMISC_IN_BSS |
  1939. FIF_OTHER_BSS;
  1940. priv->filter_flags = *total_flags;
  1941. if (changed_flags & (FIF_PROMISC_IN_BSS | FIF_OTHER_BSS))
  1942. p54_setup_mac(dev);
  1943. }
  1944. static int p54_conf_tx(struct ieee80211_hw *dev, u16 queue,
  1945. const struct ieee80211_tx_queue_params *params)
  1946. {
  1947. struct p54_common *priv = dev->priv;
  1948. int ret;
  1949. mutex_lock(&priv->conf_mutex);
  1950. if ((params) && !(queue > 4)) {
  1951. P54_SET_QUEUE(priv->qos_params[queue], params->aifs,
  1952. params->cw_min, params->cw_max, params->txop);
  1953. ret = p54_set_edcf(dev);
  1954. } else
  1955. ret = -EINVAL;
  1956. mutex_unlock(&priv->conf_mutex);
  1957. return ret;
  1958. }
  1959. static int p54_init_xbow_synth(struct ieee80211_hw *dev)
  1960. {
  1961. struct p54_common *priv = dev->priv;
  1962. struct sk_buff *skb;
  1963. struct p54_xbow_synth *xbow;
  1964. skb = p54_alloc_skb(dev, P54_HDR_FLAG_CONTROL_OPSET, sizeof(*xbow),
  1965. P54_CONTROL_TYPE_XBOW_SYNTH_CFG, GFP_KERNEL);
  1966. if (!skb)
  1967. return -ENOMEM;
  1968. xbow = (struct p54_xbow_synth *)skb_put(skb, sizeof(*xbow));
  1969. xbow->magic1 = cpu_to_le16(0x1);
  1970. xbow->magic2 = cpu_to_le16(0x2);
  1971. xbow->freq = cpu_to_le16(5390);
  1972. memset(xbow->padding, 0, sizeof(xbow->padding));
  1973. priv->tx(dev, skb);
  1974. return 0;
  1975. }
  1976. static void p54_work(struct work_struct *work)
  1977. {
  1978. struct p54_common *priv = container_of(work, struct p54_common,
  1979. work.work);
  1980. struct ieee80211_hw *dev = priv->hw;
  1981. struct sk_buff *skb;
  1982. if (unlikely(priv->mode == NL80211_IFTYPE_UNSPECIFIED))
  1983. return ;
  1984. /*
  1985. * TODO: walk through tx_queue and do the following tasks
  1986. * 1. initiate bursts.
  1987. * 2. cancel stuck frames / reset the device if necessary.
  1988. */
  1989. skb = p54_alloc_skb(dev, P54_HDR_FLAG_CONTROL,
  1990. sizeof(struct p54_statistics),
  1991. P54_CONTROL_TYPE_STAT_READBACK, GFP_KERNEL);
  1992. if (!skb)
  1993. return ;
  1994. priv->tx(dev, skb);
  1995. }
  1996. static int p54_get_stats(struct ieee80211_hw *dev,
  1997. struct ieee80211_low_level_stats *stats)
  1998. {
  1999. struct p54_common *priv = dev->priv;
  2000. memcpy(stats, &priv->stats, sizeof(*stats));
  2001. return 0;
  2002. }
  2003. static int p54_get_tx_stats(struct ieee80211_hw *dev,
  2004. struct ieee80211_tx_queue_stats *stats)
  2005. {
  2006. struct p54_common *priv = dev->priv;
  2007. memcpy(stats, &priv->tx_stats[P54_QUEUE_DATA],
  2008. sizeof(stats[0]) * dev->queues);
  2009. return 0;
  2010. }
  2011. static void p54_bss_info_changed(struct ieee80211_hw *dev,
  2012. struct ieee80211_vif *vif,
  2013. struct ieee80211_bss_conf *info,
  2014. u32 changed)
  2015. {
  2016. struct p54_common *priv = dev->priv;
  2017. if (changed & BSS_CHANGED_ERP_SLOT) {
  2018. priv->use_short_slot = info->use_short_slot;
  2019. p54_set_edcf(dev);
  2020. }
  2021. if (changed & BSS_CHANGED_BASIC_RATES) {
  2022. if (dev->conf.channel->band == IEEE80211_BAND_5GHZ)
  2023. priv->basic_rate_mask = (info->basic_rates << 4);
  2024. else
  2025. priv->basic_rate_mask = info->basic_rates;
  2026. p54_setup_mac(dev);
  2027. if (priv->fw_var >= 0x500)
  2028. p54_scan(dev, P54_SCAN_EXIT, 0);
  2029. }
  2030. if (changed & BSS_CHANGED_ASSOC) {
  2031. if (info->assoc) {
  2032. priv->aid = info->aid;
  2033. priv->wakeup_timer = info->beacon_int *
  2034. info->dtim_period * 5;
  2035. p54_setup_mac(dev);
  2036. }
  2037. }
  2038. }
  2039. static int p54_set_key(struct ieee80211_hw *dev, enum set_key_cmd cmd,
  2040. struct ieee80211_vif *vif, struct ieee80211_sta *sta,
  2041. struct ieee80211_key_conf *key)
  2042. {
  2043. struct p54_common *priv = dev->priv;
  2044. struct sk_buff *skb;
  2045. struct p54_keycache *rxkey;
  2046. u8 algo = 0;
  2047. if (modparam_nohwcrypt)
  2048. return -EOPNOTSUPP;
  2049. if (cmd == DISABLE_KEY)
  2050. algo = 0;
  2051. else {
  2052. switch (key->alg) {
  2053. case ALG_TKIP:
  2054. if (!(priv->privacy_caps & (BR_DESC_PRIV_CAP_MICHAEL |
  2055. BR_DESC_PRIV_CAP_TKIP)))
  2056. return -EOPNOTSUPP;
  2057. key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
  2058. algo = P54_CRYPTO_TKIPMICHAEL;
  2059. break;
  2060. case ALG_WEP:
  2061. if (!(priv->privacy_caps & BR_DESC_PRIV_CAP_WEP))
  2062. return -EOPNOTSUPP;
  2063. key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
  2064. algo = P54_CRYPTO_WEP;
  2065. break;
  2066. case ALG_CCMP:
  2067. if (!(priv->privacy_caps & BR_DESC_PRIV_CAP_AESCCMP))
  2068. return -EOPNOTSUPP;
  2069. key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
  2070. algo = P54_CRYPTO_AESCCMP;
  2071. break;
  2072. default:
  2073. return -EOPNOTSUPP;
  2074. }
  2075. }
  2076. if (key->keyidx > priv->rx_keycache_size) {
  2077. /*
  2078. * The device supports the choosen algorithm, but the firmware
  2079. * does not provide enough key slots to store all of them.
  2080. * So, incoming frames have to be decoded by the mac80211 stack,
  2081. * but we can still offload encryption for outgoing frames.
  2082. */
  2083. return 0;
  2084. }
  2085. mutex_lock(&priv->conf_mutex);
  2086. skb = p54_alloc_skb(dev, P54_HDR_FLAG_CONTROL_OPSET, sizeof(*rxkey),
  2087. P54_CONTROL_TYPE_RX_KEYCACHE, GFP_ATOMIC);
  2088. if (!skb) {
  2089. mutex_unlock(&priv->conf_mutex);
  2090. return -ENOMEM;
  2091. }
  2092. /* TODO: some devices have 4 more free slots for rx keys */
  2093. rxkey = (struct p54_keycache *)skb_put(skb, sizeof(*rxkey));
  2094. rxkey->entry = key->keyidx;
  2095. rxkey->key_id = key->keyidx;
  2096. rxkey->key_type = algo;
  2097. if (sta)
  2098. memcpy(rxkey->mac, sta->addr, ETH_ALEN);
  2099. else
  2100. memset(rxkey->mac, ~0, ETH_ALEN);
  2101. if (key->alg != ALG_TKIP) {
  2102. rxkey->key_len = min((u8)16, key->keylen);
  2103. memcpy(rxkey->key, key->key, rxkey->key_len);
  2104. } else {
  2105. rxkey->key_len = 24;
  2106. memcpy(rxkey->key, key->key, 16);
  2107. memcpy(&(rxkey->key[16]), &(key->key
  2108. [NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY]), 8);
  2109. }
  2110. priv->tx(dev, skb);
  2111. mutex_unlock(&priv->conf_mutex);
  2112. return 0;
  2113. }
  2114. #ifdef CONFIG_P54_LEDS
  2115. static void p54_led_brightness_set(struct led_classdev *led_dev,
  2116. enum led_brightness brightness)
  2117. {
  2118. struct p54_led_dev *led = container_of(led_dev, struct p54_led_dev,
  2119. led_dev);
  2120. struct ieee80211_hw *dev = led->hw_dev;
  2121. struct p54_common *priv = dev->priv;
  2122. int err;
  2123. /* Don't toggle the LED, when the device is down. */
  2124. if (priv->mode == NL80211_IFTYPE_UNSPECIFIED)
  2125. return ;
  2126. if (brightness != LED_OFF)
  2127. priv->softled_state |= BIT(led->index);
  2128. else
  2129. priv->softled_state &= ~BIT(led->index);
  2130. err = p54_set_leds(dev);
  2131. if (err && net_ratelimit())
  2132. printk(KERN_ERR "%s: failed to update %s LED.\n",
  2133. wiphy_name(dev->wiphy), led_dev->name);
  2134. }
  2135. static int p54_register_led(struct ieee80211_hw *dev,
  2136. struct p54_led_dev *led,
  2137. unsigned int led_index,
  2138. char *name, char *trigger)
  2139. {
  2140. int err;
  2141. if (led->registered)
  2142. return -EEXIST;
  2143. snprintf(led->name, sizeof(led->name), "p54-%s::%s",
  2144. wiphy_name(dev->wiphy), name);
  2145. led->hw_dev = dev;
  2146. led->index = led_index;
  2147. led->led_dev.name = led->name;
  2148. led->led_dev.default_trigger = trigger;
  2149. led->led_dev.brightness_set = p54_led_brightness_set;
  2150. err = led_classdev_register(wiphy_dev(dev->wiphy), &led->led_dev);
  2151. if (err)
  2152. printk(KERN_ERR "%s: Failed to register %s LED.\n",
  2153. wiphy_name(dev->wiphy), name);
  2154. else
  2155. led->registered = 1;
  2156. return err;
  2157. }
  2158. static int p54_init_leds(struct ieee80211_hw *dev)
  2159. {
  2160. struct p54_common *priv = dev->priv;
  2161. int err;
  2162. /*
  2163. * TODO:
  2164. * Figure out if the EEPROM contains some hints about the number
  2165. * of available/programmable LEDs of the device.
  2166. * But for now, we can assume that we have two programmable LEDs.
  2167. */
  2168. err = p54_register_led(dev, &priv->assoc_led, 0, "assoc",
  2169. ieee80211_get_assoc_led_name(dev));
  2170. if (err)
  2171. return err;
  2172. err = p54_register_led(dev, &priv->tx_led, 1, "tx",
  2173. ieee80211_get_tx_led_name(dev));
  2174. if (err)
  2175. return err;
  2176. err = p54_set_leds(dev);
  2177. return err;
  2178. }
  2179. static void p54_unregister_leds(struct ieee80211_hw *dev)
  2180. {
  2181. struct p54_common *priv = dev->priv;
  2182. if (priv->tx_led.registered)
  2183. led_classdev_unregister(&priv->tx_led.led_dev);
  2184. if (priv->assoc_led.registered)
  2185. led_classdev_unregister(&priv->assoc_led.led_dev);
  2186. }
  2187. #endif /* CONFIG_P54_LEDS */
  2188. static const struct ieee80211_ops p54_ops = {
  2189. .tx = p54_tx,
  2190. .start = p54_start,
  2191. .stop = p54_stop,
  2192. .add_interface = p54_add_interface,
  2193. .remove_interface = p54_remove_interface,
  2194. .set_tim = p54_set_tim,
  2195. .sta_notify = p54_sta_notify,
  2196. .set_key = p54_set_key,
  2197. .config = p54_config,
  2198. .config_interface = p54_config_interface,
  2199. .bss_info_changed = p54_bss_info_changed,
  2200. .configure_filter = p54_configure_filter,
  2201. .conf_tx = p54_conf_tx,
  2202. .get_stats = p54_get_stats,
  2203. .get_tx_stats = p54_get_tx_stats
  2204. };
  2205. struct ieee80211_hw *p54_init_common(size_t priv_data_len)
  2206. {
  2207. struct ieee80211_hw *dev;
  2208. struct p54_common *priv;
  2209. dev = ieee80211_alloc_hw(priv_data_len, &p54_ops);
  2210. if (!dev)
  2211. return NULL;
  2212. priv = dev->priv;
  2213. priv->hw = dev;
  2214. priv->mode = NL80211_IFTYPE_UNSPECIFIED;
  2215. priv->basic_rate_mask = 0x15f;
  2216. skb_queue_head_init(&priv->tx_queue);
  2217. dev->flags = IEEE80211_HW_RX_INCLUDES_FCS |
  2218. IEEE80211_HW_SUPPORTS_PS |
  2219. IEEE80211_HW_PS_NULLFUNC_STACK |
  2220. IEEE80211_HW_SIGNAL_DBM |
  2221. IEEE80211_HW_NOISE_DBM;
  2222. dev->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
  2223. BIT(NL80211_IFTYPE_ADHOC) |
  2224. BIT(NL80211_IFTYPE_AP) |
  2225. BIT(NL80211_IFTYPE_MESH_POINT);
  2226. dev->channel_change_time = 1000; /* TODO: find actual value */
  2227. priv->tx_stats[P54_QUEUE_BEACON].limit = 1;
  2228. priv->tx_stats[P54_QUEUE_FWSCAN].limit = 1;
  2229. priv->tx_stats[P54_QUEUE_MGMT].limit = 3;
  2230. priv->tx_stats[P54_QUEUE_CAB].limit = 3;
  2231. priv->tx_stats[P54_QUEUE_DATA].limit = 5;
  2232. dev->queues = 1;
  2233. priv->noise = -94;
  2234. /*
  2235. * We support at most 8 tries no matter which rate they're at,
  2236. * we cannot support max_rates * max_rate_tries as we set it
  2237. * here, but setting it correctly to 4/2 or so would limit us
  2238. * artificially if the RC algorithm wants just two rates, so
  2239. * let's say 4/7, we'll redistribute it at TX time, see the
  2240. * comments there.
  2241. */
  2242. dev->max_rates = 4;
  2243. dev->max_rate_tries = 7;
  2244. dev->extra_tx_headroom = sizeof(struct p54_hdr) + 4 +
  2245. sizeof(struct p54_tx_data);
  2246. mutex_init(&priv->conf_mutex);
  2247. init_completion(&priv->eeprom_comp);
  2248. INIT_DELAYED_WORK(&priv->work, p54_work);
  2249. return dev;
  2250. }
  2251. EXPORT_SYMBOL_GPL(p54_init_common);
  2252. int p54_register_common(struct ieee80211_hw *dev, struct device *pdev)
  2253. {
  2254. int err;
  2255. err = ieee80211_register_hw(dev);
  2256. if (err) {
  2257. dev_err(pdev, "Cannot register device (%d).\n", err);
  2258. return err;
  2259. }
  2260. #ifdef CONFIG_P54_LEDS
  2261. err = p54_init_leds(dev);
  2262. if (err)
  2263. return err;
  2264. #endif /* CONFIG_P54_LEDS */
  2265. dev_info(pdev, "is registered as '%s'\n", wiphy_name(dev->wiphy));
  2266. return 0;
  2267. }
  2268. EXPORT_SYMBOL_GPL(p54_register_common);
  2269. void p54_free_common(struct ieee80211_hw *dev)
  2270. {
  2271. struct p54_common *priv = dev->priv;
  2272. kfree(priv->iq_autocal);
  2273. kfree(priv->output_limit);
  2274. kfree(priv->curve_data);
  2275. #ifdef CONFIG_P54_LEDS
  2276. p54_unregister_leds(dev);
  2277. #endif /* CONFIG_P54_LEDS */
  2278. }
  2279. EXPORT_SYMBOL_GPL(p54_free_common);
  2280. static int __init p54_init(void)
  2281. {
  2282. return 0;
  2283. }
  2284. static void __exit p54_exit(void)
  2285. {
  2286. }
  2287. module_init(p54_init);
  2288. module_exit(p54_exit);