p54common.c 71 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682
  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_common *priv = dev->priv;
  917. struct p54_hdr *hdr = (struct p54_hdr *) skb->data;
  918. struct p54_trap *trap = (struct p54_trap *) hdr->data;
  919. u16 event = le16_to_cpu(trap->event);
  920. u16 freq = le16_to_cpu(trap->frequency);
  921. switch (event) {
  922. case P54_TRAP_BEACON_TX:
  923. break;
  924. case P54_TRAP_RADAR:
  925. printk(KERN_INFO "%s: radar (freq:%d MHz)\n",
  926. wiphy_name(dev->wiphy), freq);
  927. break;
  928. case P54_TRAP_NO_BEACON:
  929. if (priv->vif)
  930. ieee80211_beacon_loss(priv->vif);
  931. break;
  932. case P54_TRAP_SCAN:
  933. break;
  934. case P54_TRAP_TBTT:
  935. break;
  936. case P54_TRAP_TIMER:
  937. break;
  938. default:
  939. printk(KERN_INFO "%s: received event:%x freq:%d\n",
  940. wiphy_name(dev->wiphy), event, freq);
  941. break;
  942. }
  943. }
  944. static int p54_rx_control(struct ieee80211_hw *dev, struct sk_buff *skb)
  945. {
  946. struct p54_hdr *hdr = (struct p54_hdr *) skb->data;
  947. switch (le16_to_cpu(hdr->type)) {
  948. case P54_CONTROL_TYPE_TXDONE:
  949. p54_rx_frame_sent(dev, skb);
  950. break;
  951. case P54_CONTROL_TYPE_TRAP:
  952. p54_rx_trap(dev, skb);
  953. break;
  954. case P54_CONTROL_TYPE_BBP:
  955. break;
  956. case P54_CONTROL_TYPE_STAT_READBACK:
  957. p54_rx_stats(dev, skb);
  958. break;
  959. case P54_CONTROL_TYPE_EEPROM_READBACK:
  960. p54_rx_eeprom_readback(dev, skb);
  961. break;
  962. default:
  963. printk(KERN_DEBUG "%s: not handling 0x%02x type control frame\n",
  964. wiphy_name(dev->wiphy), le16_to_cpu(hdr->type));
  965. break;
  966. }
  967. return 0;
  968. }
  969. /* returns zero if skb can be reused */
  970. int p54_rx(struct ieee80211_hw *dev, struct sk_buff *skb)
  971. {
  972. u16 type = le16_to_cpu(*((__le16 *)skb->data));
  973. if (type & P54_HDR_FLAG_CONTROL)
  974. return p54_rx_control(dev, skb);
  975. else
  976. return p54_rx_data(dev, skb);
  977. }
  978. EXPORT_SYMBOL_GPL(p54_rx);
  979. /*
  980. * So, the firmware is somewhat stupid and doesn't know what places in its
  981. * memory incoming data should go to. By poking around in the firmware, we
  982. * can find some unused memory to upload our packets to. However, data that we
  983. * want the card to TX needs to stay intact until the card has told us that
  984. * it is done with it. This function finds empty places we can upload to and
  985. * marks allocated areas as reserved if necessary. p54_rx_frame_sent or
  986. * p54_free_skb frees allocated areas.
  987. */
  988. static int p54_assign_address(struct ieee80211_hw *dev, struct sk_buff *skb,
  989. struct p54_hdr *data, u32 len)
  990. {
  991. struct p54_common *priv = dev->priv;
  992. struct sk_buff *entry;
  993. struct sk_buff *target_skb = NULL;
  994. struct ieee80211_tx_info *info;
  995. struct p54_tx_info *range;
  996. u32 last_addr = priv->rx_start;
  997. u32 largest_hole = 0;
  998. u32 target_addr = priv->rx_start;
  999. unsigned long flags;
  1000. unsigned int left;
  1001. len = (len + priv->headroom + priv->tailroom + 3) & ~0x3;
  1002. if (!skb)
  1003. return -EINVAL;
  1004. spin_lock_irqsave(&priv->tx_queue.lock, flags);
  1005. left = skb_queue_len(&priv->tx_queue);
  1006. if (unlikely(left >= 28)) {
  1007. /*
  1008. * The tx_queue is nearly full!
  1009. * We have throttle normal data traffic, because we must
  1010. * have a few spare slots for control frames left.
  1011. */
  1012. ieee80211_stop_queues(dev);
  1013. queue_delayed_work(dev->workqueue, &priv->work,
  1014. msecs_to_jiffies(P54_TX_TIMEOUT));
  1015. if (unlikely(left == 32)) {
  1016. /*
  1017. * The tx_queue is now really full.
  1018. *
  1019. * TODO: check if the device has crashed and reset it.
  1020. */
  1021. spin_unlock_irqrestore(&priv->tx_queue.lock, flags);
  1022. return -ENOSPC;
  1023. }
  1024. }
  1025. entry = priv->tx_queue.next;
  1026. while (left--) {
  1027. u32 hole_size;
  1028. info = IEEE80211_SKB_CB(entry);
  1029. range = (void *)info->rate_driver_data;
  1030. hole_size = range->start_addr - last_addr;
  1031. if (!target_skb && hole_size >= len) {
  1032. target_skb = entry->prev;
  1033. hole_size -= len;
  1034. target_addr = last_addr;
  1035. }
  1036. largest_hole = max(largest_hole, hole_size);
  1037. last_addr = range->end_addr;
  1038. entry = entry->next;
  1039. }
  1040. if (!target_skb && priv->rx_end - last_addr >= len) {
  1041. target_skb = priv->tx_queue.prev;
  1042. largest_hole = max(largest_hole, priv->rx_end - last_addr - len);
  1043. if (!skb_queue_empty(&priv->tx_queue)) {
  1044. info = IEEE80211_SKB_CB(target_skb);
  1045. range = (void *)info->rate_driver_data;
  1046. target_addr = range->end_addr;
  1047. }
  1048. } else
  1049. largest_hole = max(largest_hole, priv->rx_end - last_addr);
  1050. if (!target_skb) {
  1051. spin_unlock_irqrestore(&priv->tx_queue.lock, flags);
  1052. ieee80211_stop_queues(dev);
  1053. return -ENOSPC;
  1054. }
  1055. info = IEEE80211_SKB_CB(skb);
  1056. range = (void *)info->rate_driver_data;
  1057. range->start_addr = target_addr;
  1058. range->end_addr = target_addr + len;
  1059. __skb_queue_after(&priv->tx_queue, target_skb, skb);
  1060. spin_unlock_irqrestore(&priv->tx_queue.lock, flags);
  1061. if (largest_hole < priv->headroom + sizeof(struct p54_hdr) +
  1062. 48 + IEEE80211_MAX_RTS_THRESHOLD + priv->tailroom)
  1063. ieee80211_stop_queues(dev);
  1064. data->req_id = cpu_to_le32(target_addr + priv->headroom);
  1065. return 0;
  1066. }
  1067. static struct sk_buff *p54_alloc_skb(struct ieee80211_hw *dev, u16 hdr_flags,
  1068. u16 payload_len, u16 type, gfp_t memflags)
  1069. {
  1070. struct p54_common *priv = dev->priv;
  1071. struct p54_hdr *hdr;
  1072. struct sk_buff *skb;
  1073. size_t frame_len = sizeof(*hdr) + payload_len;
  1074. if (frame_len > P54_MAX_CTRL_FRAME_LEN)
  1075. return NULL;
  1076. skb = __dev_alloc_skb(priv->tx_hdr_len + frame_len, memflags);
  1077. if (!skb)
  1078. return NULL;
  1079. skb_reserve(skb, priv->tx_hdr_len);
  1080. hdr = (struct p54_hdr *) skb_put(skb, sizeof(*hdr));
  1081. hdr->flags = cpu_to_le16(hdr_flags);
  1082. hdr->len = cpu_to_le16(payload_len);
  1083. hdr->type = cpu_to_le16(type);
  1084. hdr->tries = hdr->rts_tries = 0;
  1085. if (p54_assign_address(dev, skb, hdr, frame_len)) {
  1086. kfree_skb(skb);
  1087. return NULL;
  1088. }
  1089. return skb;
  1090. }
  1091. int p54_read_eeprom(struct ieee80211_hw *dev)
  1092. {
  1093. struct p54_common *priv = dev->priv;
  1094. struct p54_eeprom_lm86 *eeprom_hdr;
  1095. struct sk_buff *skb;
  1096. size_t eeprom_size = 0x2020, offset = 0, blocksize, maxblocksize;
  1097. int ret = -ENOMEM;
  1098. void *eeprom = NULL;
  1099. maxblocksize = EEPROM_READBACK_LEN;
  1100. if (priv->fw_var >= 0x509)
  1101. maxblocksize -= 0xc;
  1102. else
  1103. maxblocksize -= 0x4;
  1104. skb = p54_alloc_skb(dev, P54_HDR_FLAG_CONTROL, sizeof(*eeprom_hdr) +
  1105. maxblocksize, P54_CONTROL_TYPE_EEPROM_READBACK,
  1106. GFP_KERNEL);
  1107. if (!skb)
  1108. goto free;
  1109. priv->eeprom = kzalloc(EEPROM_READBACK_LEN, GFP_KERNEL);
  1110. if (!priv->eeprom)
  1111. goto free;
  1112. eeprom = kzalloc(eeprom_size, GFP_KERNEL);
  1113. if (!eeprom)
  1114. goto free;
  1115. eeprom_hdr = (struct p54_eeprom_lm86 *) skb_put(skb,
  1116. sizeof(*eeprom_hdr) + maxblocksize);
  1117. while (eeprom_size) {
  1118. blocksize = min(eeprom_size, maxblocksize);
  1119. if (priv->fw_var < 0x509) {
  1120. eeprom_hdr->v1.offset = cpu_to_le16(offset);
  1121. eeprom_hdr->v1.len = cpu_to_le16(blocksize);
  1122. } else {
  1123. eeprom_hdr->v2.offset = cpu_to_le32(offset);
  1124. eeprom_hdr->v2.len = cpu_to_le16(blocksize);
  1125. eeprom_hdr->v2.magic2 = 0xf;
  1126. memcpy(eeprom_hdr->v2.magic, (const char *)"LOCK", 4);
  1127. }
  1128. priv->tx(dev, skb);
  1129. if (!wait_for_completion_interruptible_timeout(&priv->eeprom_comp, HZ)) {
  1130. printk(KERN_ERR "%s: device does not respond!\n",
  1131. wiphy_name(dev->wiphy));
  1132. ret = -EBUSY;
  1133. goto free;
  1134. }
  1135. memcpy(eeprom + offset, priv->eeprom, blocksize);
  1136. offset += blocksize;
  1137. eeprom_size -= blocksize;
  1138. }
  1139. ret = p54_parse_eeprom(dev, eeprom, offset);
  1140. free:
  1141. kfree(priv->eeprom);
  1142. priv->eeprom = NULL;
  1143. p54_free_skb(dev, skb);
  1144. kfree(eeprom);
  1145. return ret;
  1146. }
  1147. EXPORT_SYMBOL_GPL(p54_read_eeprom);
  1148. static int p54_set_tim(struct ieee80211_hw *dev, struct ieee80211_sta *sta,
  1149. bool set)
  1150. {
  1151. struct p54_common *priv = dev->priv;
  1152. struct sk_buff *skb;
  1153. struct p54_tim *tim;
  1154. skb = p54_alloc_skb(dev, P54_HDR_FLAG_CONTROL_OPSET, sizeof(*tim),
  1155. P54_CONTROL_TYPE_TIM, GFP_ATOMIC);
  1156. if (!skb)
  1157. return -ENOMEM;
  1158. tim = (struct p54_tim *) skb_put(skb, sizeof(*tim));
  1159. tim->count = 1;
  1160. tim->entry[0] = cpu_to_le16(set ? (sta->aid | 0x8000) : sta->aid);
  1161. priv->tx(dev, skb);
  1162. return 0;
  1163. }
  1164. static int p54_sta_unlock(struct ieee80211_hw *dev, u8 *addr)
  1165. {
  1166. struct p54_common *priv = dev->priv;
  1167. struct sk_buff *skb;
  1168. struct p54_sta_unlock *sta;
  1169. skb = p54_alloc_skb(dev, P54_HDR_FLAG_CONTROL_OPSET, sizeof(*sta),
  1170. P54_CONTROL_TYPE_PSM_STA_UNLOCK, GFP_ATOMIC);
  1171. if (!skb)
  1172. return -ENOMEM;
  1173. sta = (struct p54_sta_unlock *)skb_put(skb, sizeof(*sta));
  1174. memcpy(sta->addr, addr, ETH_ALEN);
  1175. priv->tx(dev, skb);
  1176. return 0;
  1177. }
  1178. static void p54_sta_notify(struct ieee80211_hw *dev, struct ieee80211_vif *vif,
  1179. enum sta_notify_cmd notify_cmd,
  1180. struct ieee80211_sta *sta)
  1181. {
  1182. switch (notify_cmd) {
  1183. case STA_NOTIFY_ADD:
  1184. case STA_NOTIFY_REMOVE:
  1185. /*
  1186. * Notify the firmware that we don't want or we don't
  1187. * need to buffer frames for this station anymore.
  1188. */
  1189. p54_sta_unlock(dev, sta->addr);
  1190. break;
  1191. case STA_NOTIFY_AWAKE:
  1192. /* update the firmware's filter table */
  1193. p54_sta_unlock(dev, sta->addr);
  1194. break;
  1195. default:
  1196. break;
  1197. }
  1198. }
  1199. static int p54_tx_cancel(struct ieee80211_hw *dev, struct sk_buff *entry)
  1200. {
  1201. struct p54_common *priv = dev->priv;
  1202. struct sk_buff *skb;
  1203. struct p54_hdr *hdr;
  1204. struct p54_txcancel *cancel;
  1205. skb = p54_alloc_skb(dev, P54_HDR_FLAG_CONTROL_OPSET, sizeof(*cancel),
  1206. P54_CONTROL_TYPE_TXCANCEL, GFP_ATOMIC);
  1207. if (!skb)
  1208. return -ENOMEM;
  1209. hdr = (void *)entry->data;
  1210. cancel = (struct p54_txcancel *)skb_put(skb, sizeof(*cancel));
  1211. cancel->req_id = hdr->req_id;
  1212. priv->tx(dev, skb);
  1213. return 0;
  1214. }
  1215. static int p54_tx_fill(struct ieee80211_hw *dev, struct sk_buff *skb,
  1216. struct ieee80211_tx_info *info, u8 *queue, size_t *extra_len,
  1217. u16 *flags, u16 *aid)
  1218. {
  1219. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  1220. struct p54_common *priv = dev->priv;
  1221. int ret = 1;
  1222. switch (priv->mode) {
  1223. case NL80211_IFTYPE_MONITOR:
  1224. /*
  1225. * We have to set P54_HDR_FLAG_DATA_OUT_PROMISC for
  1226. * every frame in promiscuous/monitor mode.
  1227. * see STSW45x0C LMAC API - page 12.
  1228. */
  1229. *aid = 0;
  1230. *flags = P54_HDR_FLAG_DATA_OUT_PROMISC;
  1231. *queue += P54_QUEUE_DATA;
  1232. break;
  1233. case NL80211_IFTYPE_STATION:
  1234. *aid = 1;
  1235. if (unlikely(ieee80211_is_mgmt(hdr->frame_control))) {
  1236. *queue = P54_QUEUE_MGMT;
  1237. ret = 0;
  1238. } else
  1239. *queue += P54_QUEUE_DATA;
  1240. break;
  1241. case NL80211_IFTYPE_AP:
  1242. case NL80211_IFTYPE_ADHOC:
  1243. case NL80211_IFTYPE_MESH_POINT:
  1244. if (info->flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM) {
  1245. *aid = 0;
  1246. *queue = P54_QUEUE_CAB;
  1247. return 0;
  1248. }
  1249. if (unlikely(ieee80211_is_mgmt(hdr->frame_control))) {
  1250. if (ieee80211_is_probe_resp(hdr->frame_control)) {
  1251. *aid = 0;
  1252. *queue = P54_QUEUE_MGMT;
  1253. *flags = P54_HDR_FLAG_DATA_OUT_TIMESTAMP |
  1254. P54_HDR_FLAG_DATA_OUT_NOCANCEL;
  1255. return 0;
  1256. } else if (ieee80211_is_beacon(hdr->frame_control)) {
  1257. *aid = 0;
  1258. if (info->flags & IEEE80211_TX_CTL_INJECTED) {
  1259. /*
  1260. * Injecting beacons on top of a AP is
  1261. * not a good idea... nevertheless,
  1262. * it should be doable.
  1263. */
  1264. *queue += P54_QUEUE_DATA;
  1265. return 1;
  1266. }
  1267. *flags = P54_HDR_FLAG_DATA_OUT_TIMESTAMP;
  1268. *queue = P54_QUEUE_BEACON;
  1269. *extra_len = IEEE80211_MAX_TIM_LEN;
  1270. return 0;
  1271. } else {
  1272. *queue = P54_QUEUE_MGMT;
  1273. ret = 0;
  1274. }
  1275. } else
  1276. *queue += P54_QUEUE_DATA;
  1277. if (info->control.sta)
  1278. *aid = info->control.sta->aid;
  1279. else
  1280. *flags |= P54_HDR_FLAG_DATA_OUT_NOCANCEL;
  1281. break;
  1282. }
  1283. return ret;
  1284. }
  1285. static u8 p54_convert_algo(enum ieee80211_key_alg alg)
  1286. {
  1287. switch (alg) {
  1288. case ALG_WEP:
  1289. return P54_CRYPTO_WEP;
  1290. case ALG_TKIP:
  1291. return P54_CRYPTO_TKIPMICHAEL;
  1292. case ALG_CCMP:
  1293. return P54_CRYPTO_AESCCMP;
  1294. default:
  1295. return 0;
  1296. }
  1297. }
  1298. static int p54_tx(struct ieee80211_hw *dev, struct sk_buff *skb)
  1299. {
  1300. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  1301. struct ieee80211_tx_queue_stats *current_queue;
  1302. struct p54_common *priv = dev->priv;
  1303. struct p54_hdr *hdr;
  1304. struct p54_tx_data *txhdr;
  1305. size_t padding, len, tim_len = 0;
  1306. int i, j, ridx, ret;
  1307. u16 hdr_flags = 0, aid = 0;
  1308. u8 rate, queue, crypt_offset = 0;
  1309. u8 cts_rate = 0x20;
  1310. u8 rc_flags;
  1311. u8 calculated_tries[4];
  1312. u8 nrates = 0, nremaining = 8;
  1313. queue = skb_get_queue_mapping(skb);
  1314. ret = p54_tx_fill(dev, skb, info, &queue, &tim_len, &hdr_flags, &aid);
  1315. current_queue = &priv->tx_stats[queue];
  1316. if (unlikely((current_queue->len > current_queue->limit) && ret))
  1317. return NETDEV_TX_BUSY;
  1318. current_queue->len++;
  1319. current_queue->count++;
  1320. if ((current_queue->len == current_queue->limit) && ret)
  1321. ieee80211_stop_queue(dev, skb_get_queue_mapping(skb));
  1322. padding = (unsigned long)(skb->data - (sizeof(*hdr) + sizeof(*txhdr))) & 3;
  1323. len = skb->len;
  1324. if (info->control.hw_key) {
  1325. crypt_offset = ieee80211_get_hdrlen_from_skb(skb);
  1326. if (info->control.hw_key->alg == ALG_TKIP) {
  1327. u8 *iv = (u8 *)(skb->data + crypt_offset);
  1328. /*
  1329. * The firmware excepts that the IV has to have
  1330. * this special format
  1331. */
  1332. iv[1] = iv[0];
  1333. iv[0] = iv[2];
  1334. iv[2] = 0;
  1335. }
  1336. }
  1337. txhdr = (struct p54_tx_data *) skb_push(skb, sizeof(*txhdr) + padding);
  1338. hdr = (struct p54_hdr *) skb_push(skb, sizeof(*hdr));
  1339. if (padding)
  1340. hdr_flags |= P54_HDR_FLAG_DATA_ALIGN;
  1341. hdr->type = cpu_to_le16(aid);
  1342. hdr->rts_tries = info->control.rates[0].count;
  1343. /*
  1344. * we register the rates in perfect order, and
  1345. * RTS/CTS won't happen on 5 GHz
  1346. */
  1347. cts_rate = info->control.rts_cts_rate_idx;
  1348. memset(&txhdr->rateset, 0, sizeof(txhdr->rateset));
  1349. /* see how many rates got used */
  1350. for (i = 0; i < 4; i++) {
  1351. if (info->control.rates[i].idx < 0)
  1352. break;
  1353. nrates++;
  1354. }
  1355. /* limit tries to 8/nrates per rate */
  1356. for (i = 0; i < nrates; i++) {
  1357. /*
  1358. * The magic expression here is equivalent to 8/nrates for
  1359. * all values that matter, but avoids division and jumps.
  1360. * Note that nrates can only take the values 1 through 4.
  1361. */
  1362. calculated_tries[i] = min_t(int, ((15 >> nrates) | 1) + 1,
  1363. info->control.rates[i].count);
  1364. nremaining -= calculated_tries[i];
  1365. }
  1366. /* if there are tries left, distribute from back to front */
  1367. for (i = nrates - 1; nremaining > 0 && i >= 0; i--) {
  1368. int tmp = info->control.rates[i].count - calculated_tries[i];
  1369. if (tmp <= 0)
  1370. continue;
  1371. /* RC requested more tries at this rate */
  1372. tmp = min_t(int, tmp, nremaining);
  1373. calculated_tries[i] += tmp;
  1374. nremaining -= tmp;
  1375. }
  1376. ridx = 0;
  1377. for (i = 0; i < nrates && ridx < 8; i++) {
  1378. /* we register the rates in perfect order */
  1379. rate = info->control.rates[i].idx;
  1380. if (info->band == IEEE80211_BAND_5GHZ)
  1381. rate += 4;
  1382. /* store the count we actually calculated for TX status */
  1383. info->control.rates[i].count = calculated_tries[i];
  1384. rc_flags = info->control.rates[i].flags;
  1385. if (rc_flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE) {
  1386. rate |= 0x10;
  1387. cts_rate |= 0x10;
  1388. }
  1389. if (rc_flags & IEEE80211_TX_RC_USE_RTS_CTS)
  1390. rate |= 0x40;
  1391. else if (rc_flags & IEEE80211_TX_RC_USE_CTS_PROTECT)
  1392. rate |= 0x20;
  1393. for (j = 0; j < calculated_tries[i] && ridx < 8; j++) {
  1394. txhdr->rateset[ridx] = rate;
  1395. ridx++;
  1396. }
  1397. }
  1398. if (info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ)
  1399. hdr_flags |= P54_HDR_FLAG_DATA_OUT_SEQNR;
  1400. /* TODO: enable bursting */
  1401. hdr->flags = cpu_to_le16(hdr_flags);
  1402. hdr->tries = ridx;
  1403. txhdr->rts_rate_idx = 0;
  1404. if (info->control.hw_key) {
  1405. txhdr->key_type = p54_convert_algo(info->control.hw_key->alg);
  1406. txhdr->key_len = min((u8)16, info->control.hw_key->keylen);
  1407. memcpy(txhdr->key, info->control.hw_key->key, txhdr->key_len);
  1408. if (info->control.hw_key->alg == ALG_TKIP) {
  1409. if (unlikely(skb_tailroom(skb) < 12))
  1410. goto err;
  1411. /* reserve space for the MIC key */
  1412. len += 8;
  1413. memcpy(skb_put(skb, 8), &(info->control.hw_key->key
  1414. [NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY]), 8);
  1415. }
  1416. /* reserve some space for ICV */
  1417. len += info->control.hw_key->icv_len;
  1418. memset(skb_put(skb, info->control.hw_key->icv_len), 0,
  1419. info->control.hw_key->icv_len);
  1420. } else {
  1421. txhdr->key_type = 0;
  1422. txhdr->key_len = 0;
  1423. }
  1424. txhdr->crypt_offset = crypt_offset;
  1425. txhdr->hw_queue = queue;
  1426. txhdr->backlog = current_queue->len;
  1427. memset(txhdr->durations, 0, sizeof(txhdr->durations));
  1428. txhdr->tx_antenna = ((info->antenna_sel_tx == 0) ?
  1429. 2 : info->antenna_sel_tx - 1) & priv->tx_diversity_mask;
  1430. if (priv->rxhw == PDR_SYNTH_FRONTEND_LONGBOW) {
  1431. txhdr->longbow.cts_rate = cts_rate;
  1432. txhdr->longbow.output_power = cpu_to_le16(priv->output_power);
  1433. } else {
  1434. txhdr->normal.output_power = priv->output_power;
  1435. txhdr->normal.cts_rate = cts_rate;
  1436. }
  1437. if (padding)
  1438. txhdr->align[0] = padding;
  1439. hdr->len = cpu_to_le16(len);
  1440. /* modifies skb->cb and with it info, so must be last! */
  1441. if (unlikely(p54_assign_address(dev, skb, hdr, skb->len + tim_len)))
  1442. goto err;
  1443. priv->tx(dev, skb);
  1444. queue_delayed_work(dev->workqueue, &priv->work,
  1445. msecs_to_jiffies(P54_TX_FRAME_LIFETIME));
  1446. return NETDEV_TX_OK;
  1447. err:
  1448. skb_pull(skb, sizeof(*hdr) + sizeof(*txhdr) + padding);
  1449. current_queue->len--;
  1450. current_queue->count--;
  1451. return NETDEV_TX_BUSY;
  1452. }
  1453. static int p54_setup_mac(struct ieee80211_hw *dev)
  1454. {
  1455. struct p54_common *priv = dev->priv;
  1456. struct sk_buff *skb;
  1457. struct p54_setup_mac *setup;
  1458. u16 mode;
  1459. skb = p54_alloc_skb(dev, P54_HDR_FLAG_CONTROL_OPSET, sizeof(*setup),
  1460. P54_CONTROL_TYPE_SETUP, GFP_ATOMIC);
  1461. if (!skb)
  1462. return -ENOMEM;
  1463. setup = (struct p54_setup_mac *) skb_put(skb, sizeof(*setup));
  1464. if (dev->conf.radio_enabled) {
  1465. switch (priv->mode) {
  1466. case NL80211_IFTYPE_STATION:
  1467. mode = P54_FILTER_TYPE_STATION;
  1468. break;
  1469. case NL80211_IFTYPE_AP:
  1470. mode = P54_FILTER_TYPE_AP;
  1471. break;
  1472. case NL80211_IFTYPE_ADHOC:
  1473. case NL80211_IFTYPE_MESH_POINT:
  1474. mode = P54_FILTER_TYPE_IBSS;
  1475. break;
  1476. case NL80211_IFTYPE_MONITOR:
  1477. mode = P54_FILTER_TYPE_PROMISCUOUS;
  1478. break;
  1479. default:
  1480. mode = P54_FILTER_TYPE_HIBERNATE;
  1481. break;
  1482. }
  1483. /*
  1484. * "TRANSPARENT and PROMISCUOUS are mutually exclusive"
  1485. * STSW45X0C LMAC API - page 12
  1486. */
  1487. if (((priv->filter_flags & FIF_PROMISC_IN_BSS) ||
  1488. (priv->filter_flags & FIF_OTHER_BSS)) &&
  1489. (mode != P54_FILTER_TYPE_PROMISCUOUS))
  1490. mode |= P54_FILTER_TYPE_TRANSPARENT;
  1491. } else
  1492. mode = P54_FILTER_TYPE_HIBERNATE;
  1493. setup->mac_mode = cpu_to_le16(mode);
  1494. memcpy(setup->mac_addr, priv->mac_addr, ETH_ALEN);
  1495. memcpy(setup->bssid, priv->bssid, ETH_ALEN);
  1496. setup->rx_antenna = 2 & priv->rx_diversity_mask; /* automatic */
  1497. setup->rx_align = 0;
  1498. if (priv->fw_var < 0x500) {
  1499. setup->v1.basic_rate_mask = cpu_to_le32(priv->basic_rate_mask);
  1500. memset(setup->v1.rts_rates, 0, 8);
  1501. setup->v1.rx_addr = cpu_to_le32(priv->rx_end);
  1502. setup->v1.max_rx = cpu_to_le16(priv->rx_mtu);
  1503. setup->v1.rxhw = cpu_to_le16(priv->rxhw);
  1504. setup->v1.wakeup_timer = cpu_to_le16(priv->wakeup_timer);
  1505. setup->v1.unalloc0 = cpu_to_le16(0);
  1506. } else {
  1507. setup->v2.rx_addr = cpu_to_le32(priv->rx_end);
  1508. setup->v2.max_rx = cpu_to_le16(priv->rx_mtu);
  1509. setup->v2.rxhw = cpu_to_le16(priv->rxhw);
  1510. setup->v2.timer = cpu_to_le16(priv->wakeup_timer);
  1511. setup->v2.truncate = cpu_to_le16(48896);
  1512. setup->v2.basic_rate_mask = cpu_to_le32(priv->basic_rate_mask);
  1513. setup->v2.sbss_offset = 0;
  1514. setup->v2.mcast_window = 0;
  1515. setup->v2.rx_rssi_threshold = 0;
  1516. setup->v2.rx_ed_threshold = 0;
  1517. setup->v2.ref_clock = cpu_to_le32(644245094);
  1518. setup->v2.lpf_bandwidth = cpu_to_le16(65535);
  1519. setup->v2.osc_start_delay = cpu_to_le16(65535);
  1520. }
  1521. priv->tx(dev, skb);
  1522. return 0;
  1523. }
  1524. static int p54_scan(struct ieee80211_hw *dev, u16 mode, u16 dwell)
  1525. {
  1526. struct p54_common *priv = dev->priv;
  1527. struct sk_buff *skb;
  1528. struct p54_hdr *hdr;
  1529. struct p54_scan_head *head;
  1530. struct p54_iq_autocal_entry *iq_autocal;
  1531. union p54_scan_body_union *body;
  1532. struct p54_scan_tail_rate *rate;
  1533. struct pda_rssi_cal_entry *rssi;
  1534. unsigned int i;
  1535. void *entry;
  1536. int band = dev->conf.channel->band;
  1537. __le16 freq = cpu_to_le16(dev->conf.channel->center_freq);
  1538. skb = p54_alloc_skb(dev, P54_HDR_FLAG_CONTROL_OPSET, sizeof(*head) +
  1539. 2 + sizeof(*iq_autocal) + sizeof(*body) +
  1540. sizeof(*rate) + 2 * sizeof(*rssi),
  1541. P54_CONTROL_TYPE_SCAN, GFP_ATOMIC);
  1542. if (!skb)
  1543. return -ENOMEM;
  1544. head = (struct p54_scan_head *) skb_put(skb, sizeof(*head));
  1545. memset(head->scan_params, 0, sizeof(head->scan_params));
  1546. head->mode = cpu_to_le16(mode);
  1547. head->dwell = cpu_to_le16(dwell);
  1548. head->freq = freq;
  1549. if (priv->rxhw == PDR_SYNTH_FRONTEND_LONGBOW) {
  1550. __le16 *pa_power_points = (__le16 *) skb_put(skb, 2);
  1551. *pa_power_points = cpu_to_le16(0x0c);
  1552. }
  1553. iq_autocal = (void *) skb_put(skb, sizeof(*iq_autocal));
  1554. for (i = 0; i < priv->iq_autocal_len; i++) {
  1555. if (priv->iq_autocal[i].freq != freq)
  1556. continue;
  1557. memcpy(iq_autocal, &priv->iq_autocal[i].params,
  1558. sizeof(struct p54_iq_autocal_entry));
  1559. break;
  1560. }
  1561. if (i == priv->iq_autocal_len)
  1562. goto err;
  1563. if (priv->rxhw == PDR_SYNTH_FRONTEND_LONGBOW)
  1564. body = (void *) skb_put(skb, sizeof(body->longbow));
  1565. else
  1566. body = (void *) skb_put(skb, sizeof(body->normal));
  1567. for (i = 0; i < priv->output_limit->entries; i++) {
  1568. __le16 *entry_freq = (void *) (priv->output_limit->data +
  1569. priv->output_limit->entry_size * i);
  1570. if (*entry_freq != freq)
  1571. continue;
  1572. if (priv->rxhw == PDR_SYNTH_FRONTEND_LONGBOW) {
  1573. memcpy(&body->longbow.power_limits,
  1574. (void *) entry_freq + sizeof(__le16),
  1575. priv->output_limit->entry_size);
  1576. } else {
  1577. struct pda_channel_output_limit *limits =
  1578. (void *) entry_freq;
  1579. body->normal.val_barker = 0x38;
  1580. body->normal.val_bpsk = body->normal.dup_bpsk =
  1581. limits->val_bpsk;
  1582. body->normal.val_qpsk = body->normal.dup_qpsk =
  1583. limits->val_qpsk;
  1584. body->normal.val_16qam = body->normal.dup_16qam =
  1585. limits->val_16qam;
  1586. body->normal.val_64qam = body->normal.dup_64qam =
  1587. limits->val_64qam;
  1588. }
  1589. break;
  1590. }
  1591. if (i == priv->output_limit->entries)
  1592. goto err;
  1593. entry = (void *)(priv->curve_data->data + priv->curve_data->offset);
  1594. for (i = 0; i < priv->curve_data->entries; i++) {
  1595. if (*((__le16 *)entry) != freq) {
  1596. entry += priv->curve_data->entry_size;
  1597. continue;
  1598. }
  1599. if (priv->rxhw == PDR_SYNTH_FRONTEND_LONGBOW) {
  1600. memcpy(&body->longbow.curve_data,
  1601. (void *) entry + sizeof(__le16),
  1602. priv->curve_data->entry_size);
  1603. } else {
  1604. struct p54_scan_body *chan = &body->normal;
  1605. struct pda_pa_curve_data *curve_data =
  1606. (void *) priv->curve_data->data;
  1607. entry += sizeof(__le16);
  1608. chan->pa_points_per_curve = 8;
  1609. memset(chan->curve_data, 0, sizeof(*chan->curve_data));
  1610. memcpy(chan->curve_data, entry,
  1611. sizeof(struct p54_pa_curve_data_sample) *
  1612. min((u8)8, curve_data->points_per_channel));
  1613. }
  1614. break;
  1615. }
  1616. if (i == priv->curve_data->entries)
  1617. goto err;
  1618. if ((priv->fw_var >= 0x500) && (priv->fw_var < 0x509)) {
  1619. rate = (void *) skb_put(skb, sizeof(*rate));
  1620. rate->basic_rate_mask = cpu_to_le32(priv->basic_rate_mask);
  1621. for (i = 0; i < sizeof(rate->rts_rates); i++)
  1622. rate->rts_rates[i] = i;
  1623. }
  1624. rssi = (struct pda_rssi_cal_entry *) skb_put(skb, sizeof(*rssi));
  1625. rssi->mul = cpu_to_le16(priv->rssical_db[band].mul);
  1626. rssi->add = cpu_to_le16(priv->rssical_db[band].add);
  1627. if (priv->rxhw == PDR_SYNTH_FRONTEND_LONGBOW) {
  1628. /* Longbow frontend needs ever more */
  1629. rssi = (void *) skb_put(skb, sizeof(*rssi));
  1630. rssi->mul = cpu_to_le16(priv->rssical_db[band].longbow_unkn);
  1631. rssi->add = cpu_to_le16(priv->rssical_db[band].longbow_unk2);
  1632. }
  1633. if (priv->fw_var >= 0x509) {
  1634. rate = (void *) skb_put(skb, sizeof(*rate));
  1635. rate->basic_rate_mask = cpu_to_le32(priv->basic_rate_mask);
  1636. for (i = 0; i < sizeof(rate->rts_rates); i++)
  1637. rate->rts_rates[i] = i;
  1638. }
  1639. hdr = (struct p54_hdr *) skb->data;
  1640. hdr->len = cpu_to_le16(skb->len - sizeof(*hdr));
  1641. priv->tx(dev, skb);
  1642. return 0;
  1643. err:
  1644. printk(KERN_ERR "%s: frequency change failed\n", wiphy_name(dev->wiphy));
  1645. p54_free_skb(dev, skb);
  1646. return -EINVAL;
  1647. }
  1648. static int p54_set_leds(struct ieee80211_hw *dev)
  1649. {
  1650. struct p54_common *priv = dev->priv;
  1651. struct sk_buff *skb;
  1652. struct p54_led *led;
  1653. skb = p54_alloc_skb(dev, P54_HDR_FLAG_CONTROL_OPSET, sizeof(*led),
  1654. P54_CONTROL_TYPE_LED, GFP_ATOMIC);
  1655. if (!skb)
  1656. return -ENOMEM;
  1657. led = (struct p54_led *) skb_put(skb, sizeof(*led));
  1658. led->flags = cpu_to_le16(0x0003);
  1659. led->mask[0] = led->mask[1] = cpu_to_le16(priv->softled_state);
  1660. led->delay[0] = cpu_to_le16(1);
  1661. led->delay[1] = cpu_to_le16(0);
  1662. priv->tx(dev, skb);
  1663. return 0;
  1664. }
  1665. #define P54_SET_QUEUE(queue, ai_fs, cw_min, cw_max, _txop) \
  1666. do { \
  1667. queue.aifs = cpu_to_le16(ai_fs); \
  1668. queue.cwmin = cpu_to_le16(cw_min); \
  1669. queue.cwmax = cpu_to_le16(cw_max); \
  1670. queue.txop = cpu_to_le16(_txop); \
  1671. } while(0)
  1672. static int p54_set_edcf(struct ieee80211_hw *dev)
  1673. {
  1674. struct p54_common *priv = dev->priv;
  1675. struct sk_buff *skb;
  1676. struct p54_edcf *edcf;
  1677. skb = p54_alloc_skb(dev, P54_HDR_FLAG_CONTROL_OPSET, sizeof(*edcf),
  1678. P54_CONTROL_TYPE_DCFINIT, GFP_ATOMIC);
  1679. if (!skb)
  1680. return -ENOMEM;
  1681. edcf = (struct p54_edcf *)skb_put(skb, sizeof(*edcf));
  1682. if (priv->use_short_slot) {
  1683. edcf->slottime = 9;
  1684. edcf->sifs = 0x10;
  1685. edcf->eofpad = 0x00;
  1686. } else {
  1687. edcf->slottime = 20;
  1688. edcf->sifs = 0x0a;
  1689. edcf->eofpad = 0x06;
  1690. }
  1691. /* (see prism54/isl_oid.h for further details) */
  1692. edcf->frameburst = cpu_to_le16(0);
  1693. edcf->round_trip_delay = cpu_to_le16(0);
  1694. edcf->flags = 0;
  1695. memset(edcf->mapping, 0, sizeof(edcf->mapping));
  1696. memcpy(edcf->queue, priv->qos_params, sizeof(edcf->queue));
  1697. priv->tx(dev, skb);
  1698. return 0;
  1699. }
  1700. static int p54_set_ps(struct ieee80211_hw *dev)
  1701. {
  1702. struct p54_common *priv = dev->priv;
  1703. struct sk_buff *skb;
  1704. struct p54_psm *psm;
  1705. u16 mode;
  1706. int i;
  1707. if (dev->conf.flags & IEEE80211_CONF_PS)
  1708. mode = P54_PSM | P54_PSM_BEACON_TIMEOUT | P54_PSM_DTIM |
  1709. P54_PSM_CHECKSUM | P54_PSM_MCBC;
  1710. else
  1711. mode = P54_PSM_CAM;
  1712. skb = p54_alloc_skb(dev, P54_HDR_FLAG_CONTROL_OPSET, sizeof(*psm),
  1713. P54_CONTROL_TYPE_PSM, GFP_ATOMIC);
  1714. if (!skb)
  1715. return -ENOMEM;
  1716. psm = (struct p54_psm *)skb_put(skb, sizeof(*psm));
  1717. psm->mode = cpu_to_le16(mode);
  1718. psm->aid = cpu_to_le16(priv->aid);
  1719. for (i = 0; i < ARRAY_SIZE(psm->intervals); i++) {
  1720. psm->intervals[i].interval =
  1721. cpu_to_le16(dev->conf.listen_interval);
  1722. psm->intervals[i].periods = cpu_to_le16(1);
  1723. }
  1724. psm->beacon_rssi_skip_max = 200;
  1725. psm->rssi_delta_threshold = 0;
  1726. psm->nr = 10;
  1727. psm->exclude[0] = 0;
  1728. priv->tx(dev, skb);
  1729. return 0;
  1730. }
  1731. static int p54_beacon_tim(struct sk_buff *skb)
  1732. {
  1733. /*
  1734. * the good excuse for this mess is ... the firmware.
  1735. * The dummy TIM MUST be at the end of the beacon frame,
  1736. * because it'll be overwritten!
  1737. */
  1738. struct ieee80211_mgmt *mgmt = (void *)skb->data;
  1739. u8 *pos, *end;
  1740. if (skb->len <= sizeof(mgmt))
  1741. return -EINVAL;
  1742. pos = (u8 *)mgmt->u.beacon.variable;
  1743. end = skb->data + skb->len;
  1744. while (pos < end) {
  1745. if (pos + 2 + pos[1] > end)
  1746. return -EINVAL;
  1747. if (pos[0] == WLAN_EID_TIM) {
  1748. u8 dtim_len = pos[1];
  1749. u8 dtim_period = pos[3];
  1750. u8 *next = pos + 2 + dtim_len;
  1751. if (dtim_len < 3)
  1752. return -EINVAL;
  1753. memmove(pos, next, end - next);
  1754. if (dtim_len > 3)
  1755. skb_trim(skb, skb->len - (dtim_len - 3));
  1756. pos = end - (dtim_len + 2);
  1757. /* add the dummy at the end */
  1758. pos[0] = WLAN_EID_TIM;
  1759. pos[1] = 3;
  1760. pos[2] = 0;
  1761. pos[3] = dtim_period;
  1762. pos[4] = 0;
  1763. return 0;
  1764. }
  1765. pos += 2 + pos[1];
  1766. }
  1767. return 0;
  1768. }
  1769. static int p54_beacon_update(struct ieee80211_hw *dev,
  1770. struct ieee80211_vif *vif)
  1771. {
  1772. struct p54_common *priv = dev->priv;
  1773. struct sk_buff *beacon;
  1774. int ret;
  1775. if (priv->cached_beacon) {
  1776. p54_tx_cancel(dev, priv->cached_beacon);
  1777. /* wait for the last beacon the be freed */
  1778. msleep(10);
  1779. }
  1780. beacon = ieee80211_beacon_get(dev, vif);
  1781. if (!beacon)
  1782. return -ENOMEM;
  1783. ret = p54_beacon_tim(beacon);
  1784. if (ret)
  1785. return ret;
  1786. ret = p54_tx(dev, beacon);
  1787. if (ret)
  1788. return ret;
  1789. priv->cached_beacon = beacon;
  1790. priv->tsf_high32 = 0;
  1791. priv->tsf_low32 = 0;
  1792. return 0;
  1793. }
  1794. static int p54_start(struct ieee80211_hw *dev)
  1795. {
  1796. struct p54_common *priv = dev->priv;
  1797. int err;
  1798. mutex_lock(&priv->conf_mutex);
  1799. err = priv->open(dev);
  1800. if (err)
  1801. goto out;
  1802. P54_SET_QUEUE(priv->qos_params[0], 0x0002, 0x0003, 0x0007, 47);
  1803. P54_SET_QUEUE(priv->qos_params[1], 0x0002, 0x0007, 0x000f, 94);
  1804. P54_SET_QUEUE(priv->qos_params[2], 0x0003, 0x000f, 0x03ff, 0);
  1805. P54_SET_QUEUE(priv->qos_params[3], 0x0007, 0x000f, 0x03ff, 0);
  1806. err = p54_set_edcf(dev);
  1807. if (err)
  1808. goto out;
  1809. memset(priv->bssid, ~0, ETH_ALEN);
  1810. priv->mode = NL80211_IFTYPE_MONITOR;
  1811. err = p54_setup_mac(dev);
  1812. if (err) {
  1813. priv->mode = NL80211_IFTYPE_UNSPECIFIED;
  1814. goto out;
  1815. }
  1816. queue_delayed_work(dev->workqueue, &priv->work, 0);
  1817. priv->softled_state = 0;
  1818. err = p54_set_leds(dev);
  1819. out:
  1820. mutex_unlock(&priv->conf_mutex);
  1821. return err;
  1822. }
  1823. static void p54_stop(struct ieee80211_hw *dev)
  1824. {
  1825. struct p54_common *priv = dev->priv;
  1826. struct sk_buff *skb;
  1827. mutex_lock(&priv->conf_mutex);
  1828. priv->mode = NL80211_IFTYPE_UNSPECIFIED;
  1829. priv->softled_state = 0;
  1830. p54_set_leds(dev);
  1831. #ifdef CONFIG_P54_LEDS
  1832. cancel_delayed_work_sync(&priv->led_work);
  1833. #endif /* CONFIG_P54_LEDS */
  1834. cancel_delayed_work_sync(&priv->work);
  1835. if (priv->cached_beacon)
  1836. p54_tx_cancel(dev, priv->cached_beacon);
  1837. priv->stop(dev);
  1838. while ((skb = skb_dequeue(&priv->tx_queue)))
  1839. kfree_skb(skb);
  1840. priv->cached_beacon = NULL;
  1841. priv->tsf_high32 = priv->tsf_low32 = 0;
  1842. mutex_unlock(&priv->conf_mutex);
  1843. }
  1844. static int p54_add_interface(struct ieee80211_hw *dev,
  1845. struct ieee80211_if_init_conf *conf)
  1846. {
  1847. struct p54_common *priv = dev->priv;
  1848. mutex_lock(&priv->conf_mutex);
  1849. if (priv->mode != NL80211_IFTYPE_MONITOR) {
  1850. mutex_unlock(&priv->conf_mutex);
  1851. return -EOPNOTSUPP;
  1852. }
  1853. priv->vif = conf->vif;
  1854. switch (conf->type) {
  1855. case NL80211_IFTYPE_STATION:
  1856. case NL80211_IFTYPE_ADHOC:
  1857. case NL80211_IFTYPE_AP:
  1858. case NL80211_IFTYPE_MESH_POINT:
  1859. priv->mode = conf->type;
  1860. break;
  1861. default:
  1862. mutex_unlock(&priv->conf_mutex);
  1863. return -EOPNOTSUPP;
  1864. }
  1865. memcpy(priv->mac_addr, conf->mac_addr, ETH_ALEN);
  1866. p54_setup_mac(dev);
  1867. mutex_unlock(&priv->conf_mutex);
  1868. return 0;
  1869. }
  1870. static void p54_remove_interface(struct ieee80211_hw *dev,
  1871. struct ieee80211_if_init_conf *conf)
  1872. {
  1873. struct p54_common *priv = dev->priv;
  1874. mutex_lock(&priv->conf_mutex);
  1875. priv->vif = NULL;
  1876. if (priv->cached_beacon)
  1877. p54_tx_cancel(dev, priv->cached_beacon);
  1878. priv->mode = NL80211_IFTYPE_MONITOR;
  1879. memset(priv->mac_addr, 0, ETH_ALEN);
  1880. memset(priv->bssid, 0, ETH_ALEN);
  1881. p54_setup_mac(dev);
  1882. mutex_unlock(&priv->conf_mutex);
  1883. }
  1884. static int p54_config(struct ieee80211_hw *dev, u32 changed)
  1885. {
  1886. int ret = 0;
  1887. struct p54_common *priv = dev->priv;
  1888. struct ieee80211_conf *conf = &dev->conf;
  1889. mutex_lock(&priv->conf_mutex);
  1890. if (changed & IEEE80211_CONF_CHANGE_POWER)
  1891. priv->output_power = conf->power_level << 2;
  1892. if (changed & IEEE80211_CONF_CHANGE_RADIO_ENABLED) {
  1893. ret = p54_setup_mac(dev);
  1894. if (ret)
  1895. goto out;
  1896. }
  1897. if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
  1898. ret = p54_scan(dev, P54_SCAN_EXIT, 0);
  1899. if (ret)
  1900. goto out;
  1901. }
  1902. if (changed & IEEE80211_CONF_CHANGE_PS) {
  1903. ret = p54_set_ps(dev);
  1904. if (ret)
  1905. goto out;
  1906. }
  1907. out:
  1908. mutex_unlock(&priv->conf_mutex);
  1909. return ret;
  1910. }
  1911. static int p54_config_interface(struct ieee80211_hw *dev,
  1912. struct ieee80211_vif *vif,
  1913. struct ieee80211_if_conf *conf)
  1914. {
  1915. struct p54_common *priv = dev->priv;
  1916. int ret = 0;
  1917. mutex_lock(&priv->conf_mutex);
  1918. if (conf->changed & IEEE80211_IFCC_BSSID) {
  1919. memcpy(priv->bssid, conf->bssid, ETH_ALEN);
  1920. ret = p54_setup_mac(dev);
  1921. if (ret)
  1922. goto out;
  1923. }
  1924. if (conf->changed & IEEE80211_IFCC_BEACON) {
  1925. ret = p54_scan(dev, P54_SCAN_EXIT, 0);
  1926. if (ret)
  1927. goto out;
  1928. ret = p54_setup_mac(dev);
  1929. if (ret)
  1930. goto out;
  1931. ret = p54_beacon_update(dev, vif);
  1932. if (ret)
  1933. goto out;
  1934. ret = p54_set_edcf(dev);
  1935. if (ret)
  1936. goto out;
  1937. }
  1938. out:
  1939. mutex_unlock(&priv->conf_mutex);
  1940. return ret;
  1941. }
  1942. static void p54_configure_filter(struct ieee80211_hw *dev,
  1943. unsigned int changed_flags,
  1944. unsigned int *total_flags,
  1945. int mc_count, struct dev_mc_list *mclist)
  1946. {
  1947. struct p54_common *priv = dev->priv;
  1948. *total_flags &= FIF_PROMISC_IN_BSS |
  1949. FIF_OTHER_BSS;
  1950. priv->filter_flags = *total_flags;
  1951. if (changed_flags & (FIF_PROMISC_IN_BSS | FIF_OTHER_BSS))
  1952. p54_setup_mac(dev);
  1953. }
  1954. static int p54_conf_tx(struct ieee80211_hw *dev, u16 queue,
  1955. const struct ieee80211_tx_queue_params *params)
  1956. {
  1957. struct p54_common *priv = dev->priv;
  1958. int ret;
  1959. mutex_lock(&priv->conf_mutex);
  1960. if ((params) && !(queue > 4)) {
  1961. P54_SET_QUEUE(priv->qos_params[queue], params->aifs,
  1962. params->cw_min, params->cw_max, params->txop);
  1963. ret = p54_set_edcf(dev);
  1964. } else
  1965. ret = -EINVAL;
  1966. mutex_unlock(&priv->conf_mutex);
  1967. return ret;
  1968. }
  1969. static int p54_init_xbow_synth(struct ieee80211_hw *dev)
  1970. {
  1971. struct p54_common *priv = dev->priv;
  1972. struct sk_buff *skb;
  1973. struct p54_xbow_synth *xbow;
  1974. skb = p54_alloc_skb(dev, P54_HDR_FLAG_CONTROL_OPSET, sizeof(*xbow),
  1975. P54_CONTROL_TYPE_XBOW_SYNTH_CFG, GFP_KERNEL);
  1976. if (!skb)
  1977. return -ENOMEM;
  1978. xbow = (struct p54_xbow_synth *)skb_put(skb, sizeof(*xbow));
  1979. xbow->magic1 = cpu_to_le16(0x1);
  1980. xbow->magic2 = cpu_to_le16(0x2);
  1981. xbow->freq = cpu_to_le16(5390);
  1982. memset(xbow->padding, 0, sizeof(xbow->padding));
  1983. priv->tx(dev, skb);
  1984. return 0;
  1985. }
  1986. static void p54_work(struct work_struct *work)
  1987. {
  1988. struct p54_common *priv = container_of(work, struct p54_common,
  1989. work.work);
  1990. struct ieee80211_hw *dev = priv->hw;
  1991. struct sk_buff *skb;
  1992. if (unlikely(priv->mode == NL80211_IFTYPE_UNSPECIFIED))
  1993. return ;
  1994. /*
  1995. * TODO: walk through tx_queue and do the following tasks
  1996. * 1. initiate bursts.
  1997. * 2. cancel stuck frames / reset the device if necessary.
  1998. */
  1999. skb = p54_alloc_skb(dev, P54_HDR_FLAG_CONTROL,
  2000. sizeof(struct p54_statistics),
  2001. P54_CONTROL_TYPE_STAT_READBACK, GFP_KERNEL);
  2002. if (!skb)
  2003. return ;
  2004. priv->tx(dev, skb);
  2005. }
  2006. static int p54_get_stats(struct ieee80211_hw *dev,
  2007. struct ieee80211_low_level_stats *stats)
  2008. {
  2009. struct p54_common *priv = dev->priv;
  2010. memcpy(stats, &priv->stats, sizeof(*stats));
  2011. return 0;
  2012. }
  2013. static int p54_get_tx_stats(struct ieee80211_hw *dev,
  2014. struct ieee80211_tx_queue_stats *stats)
  2015. {
  2016. struct p54_common *priv = dev->priv;
  2017. memcpy(stats, &priv->tx_stats[P54_QUEUE_DATA],
  2018. sizeof(stats[0]) * dev->queues);
  2019. return 0;
  2020. }
  2021. static void p54_bss_info_changed(struct ieee80211_hw *dev,
  2022. struct ieee80211_vif *vif,
  2023. struct ieee80211_bss_conf *info,
  2024. u32 changed)
  2025. {
  2026. struct p54_common *priv = dev->priv;
  2027. if (changed & BSS_CHANGED_ERP_SLOT) {
  2028. priv->use_short_slot = info->use_short_slot;
  2029. p54_set_edcf(dev);
  2030. }
  2031. if (changed & BSS_CHANGED_BASIC_RATES) {
  2032. if (dev->conf.channel->band == IEEE80211_BAND_5GHZ)
  2033. priv->basic_rate_mask = (info->basic_rates << 4);
  2034. else
  2035. priv->basic_rate_mask = info->basic_rates;
  2036. p54_setup_mac(dev);
  2037. if (priv->fw_var >= 0x500)
  2038. p54_scan(dev, P54_SCAN_EXIT, 0);
  2039. }
  2040. if (changed & BSS_CHANGED_ASSOC) {
  2041. if (info->assoc) {
  2042. priv->aid = info->aid;
  2043. priv->wakeup_timer = info->beacon_int *
  2044. info->dtim_period * 5;
  2045. p54_setup_mac(dev);
  2046. }
  2047. }
  2048. }
  2049. static int p54_set_key(struct ieee80211_hw *dev, enum set_key_cmd cmd,
  2050. struct ieee80211_vif *vif, struct ieee80211_sta *sta,
  2051. struct ieee80211_key_conf *key)
  2052. {
  2053. struct p54_common *priv = dev->priv;
  2054. struct sk_buff *skb;
  2055. struct p54_keycache *rxkey;
  2056. u8 algo = 0;
  2057. if (modparam_nohwcrypt)
  2058. return -EOPNOTSUPP;
  2059. if (cmd == DISABLE_KEY)
  2060. algo = 0;
  2061. else {
  2062. switch (key->alg) {
  2063. case ALG_TKIP:
  2064. if (!(priv->privacy_caps & (BR_DESC_PRIV_CAP_MICHAEL |
  2065. BR_DESC_PRIV_CAP_TKIP)))
  2066. return -EOPNOTSUPP;
  2067. key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
  2068. algo = P54_CRYPTO_TKIPMICHAEL;
  2069. break;
  2070. case ALG_WEP:
  2071. if (!(priv->privacy_caps & BR_DESC_PRIV_CAP_WEP))
  2072. return -EOPNOTSUPP;
  2073. key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
  2074. algo = P54_CRYPTO_WEP;
  2075. break;
  2076. case ALG_CCMP:
  2077. if (!(priv->privacy_caps & BR_DESC_PRIV_CAP_AESCCMP))
  2078. return -EOPNOTSUPP;
  2079. key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
  2080. algo = P54_CRYPTO_AESCCMP;
  2081. break;
  2082. default:
  2083. return -EOPNOTSUPP;
  2084. }
  2085. }
  2086. if (key->keyidx > priv->rx_keycache_size) {
  2087. /*
  2088. * The device supports the choosen algorithm, but the firmware
  2089. * does not provide enough key slots to store all of them.
  2090. * So, incoming frames have to be decoded by the mac80211 stack,
  2091. * but we can still offload encryption for outgoing frames.
  2092. */
  2093. return 0;
  2094. }
  2095. mutex_lock(&priv->conf_mutex);
  2096. skb = p54_alloc_skb(dev, P54_HDR_FLAG_CONTROL_OPSET, sizeof(*rxkey),
  2097. P54_CONTROL_TYPE_RX_KEYCACHE, GFP_ATOMIC);
  2098. if (!skb) {
  2099. mutex_unlock(&priv->conf_mutex);
  2100. return -ENOMEM;
  2101. }
  2102. /* TODO: some devices have 4 more free slots for rx keys */
  2103. rxkey = (struct p54_keycache *)skb_put(skb, sizeof(*rxkey));
  2104. rxkey->entry = key->keyidx;
  2105. rxkey->key_id = key->keyidx;
  2106. rxkey->key_type = algo;
  2107. if (sta)
  2108. memcpy(rxkey->mac, sta->addr, ETH_ALEN);
  2109. else
  2110. memset(rxkey->mac, ~0, ETH_ALEN);
  2111. if (key->alg != ALG_TKIP) {
  2112. rxkey->key_len = min((u8)16, key->keylen);
  2113. memcpy(rxkey->key, key->key, rxkey->key_len);
  2114. } else {
  2115. rxkey->key_len = 24;
  2116. memcpy(rxkey->key, key->key, 16);
  2117. memcpy(&(rxkey->key[16]), &(key->key
  2118. [NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY]), 8);
  2119. }
  2120. priv->tx(dev, skb);
  2121. mutex_unlock(&priv->conf_mutex);
  2122. return 0;
  2123. }
  2124. #ifdef CONFIG_P54_LEDS
  2125. static void p54_update_leds(struct work_struct *work)
  2126. {
  2127. struct p54_common *priv = container_of(work, struct p54_common,
  2128. led_work.work);
  2129. int err, i, tmp, blink_delay = 400;
  2130. bool rerun = false;
  2131. /* Don't toggle the LED, when the device is down. */
  2132. if (priv->mode == NL80211_IFTYPE_UNSPECIFIED)
  2133. return ;
  2134. for (i = 0; i < ARRAY_SIZE(priv->leds); i++)
  2135. if (priv->leds[i].toggled) {
  2136. priv->softled_state |= BIT(i);
  2137. tmp = 70 + 200 / (priv->leds[i].toggled);
  2138. if (tmp < blink_delay)
  2139. blink_delay = tmp;
  2140. if (priv->leds[i].led_dev.brightness == LED_OFF)
  2141. rerun = true;
  2142. priv->leds[i].toggled =
  2143. !!priv->leds[i].led_dev.brightness;
  2144. } else
  2145. priv->softled_state &= ~BIT(i);
  2146. err = p54_set_leds(priv->hw);
  2147. if (err && net_ratelimit())
  2148. printk(KERN_ERR "%s: failed to update LEDs.\n",
  2149. wiphy_name(priv->hw->wiphy));
  2150. if (rerun)
  2151. queue_delayed_work(priv->hw->workqueue, &priv->led_work,
  2152. msecs_to_jiffies(blink_delay));
  2153. }
  2154. static void p54_led_brightness_set(struct led_classdev *led_dev,
  2155. enum led_brightness brightness)
  2156. {
  2157. struct p54_led_dev *led = container_of(led_dev, struct p54_led_dev,
  2158. led_dev);
  2159. struct ieee80211_hw *dev = led->hw_dev;
  2160. struct p54_common *priv = dev->priv;
  2161. if (priv->mode == NL80211_IFTYPE_UNSPECIFIED)
  2162. return ;
  2163. if (brightness) {
  2164. led->toggled++;
  2165. queue_delayed_work(priv->hw->workqueue, &priv->led_work,
  2166. HZ/10);
  2167. }
  2168. }
  2169. static int p54_register_led(struct ieee80211_hw *dev,
  2170. unsigned int led_index,
  2171. char *name, char *trigger)
  2172. {
  2173. struct p54_common *priv = dev->priv;
  2174. struct p54_led_dev *led = &priv->leds[led_index];
  2175. int err;
  2176. if (led->registered)
  2177. return -EEXIST;
  2178. snprintf(led->name, sizeof(led->name), "p54-%s::%s",
  2179. wiphy_name(dev->wiphy), name);
  2180. led->hw_dev = dev;
  2181. led->index = led_index;
  2182. led->led_dev.name = led->name;
  2183. led->led_dev.default_trigger = trigger;
  2184. led->led_dev.brightness_set = p54_led_brightness_set;
  2185. err = led_classdev_register(wiphy_dev(dev->wiphy), &led->led_dev);
  2186. if (err)
  2187. printk(KERN_ERR "%s: Failed to register %s LED.\n",
  2188. wiphy_name(dev->wiphy), name);
  2189. else
  2190. led->registered = 1;
  2191. return err;
  2192. }
  2193. static int p54_init_leds(struct ieee80211_hw *dev)
  2194. {
  2195. struct p54_common *priv = dev->priv;
  2196. int err;
  2197. /*
  2198. * TODO:
  2199. * Figure out if the EEPROM contains some hints about the number
  2200. * of available/programmable LEDs of the device.
  2201. */
  2202. INIT_DELAYED_WORK(&priv->led_work, p54_update_leds);
  2203. err = p54_register_led(dev, 0, "assoc",
  2204. ieee80211_get_assoc_led_name(dev));
  2205. if (err)
  2206. return err;
  2207. err = p54_register_led(dev, 1, "tx",
  2208. ieee80211_get_tx_led_name(dev));
  2209. if (err)
  2210. return err;
  2211. err = p54_register_led(dev, 2, "rx",
  2212. ieee80211_get_rx_led_name(dev));
  2213. if (err)
  2214. return err;
  2215. err = p54_register_led(dev, 3, "radio",
  2216. ieee80211_get_radio_led_name(dev));
  2217. if (err)
  2218. return err;
  2219. err = p54_set_leds(dev);
  2220. return err;
  2221. }
  2222. static void p54_unregister_leds(struct ieee80211_hw *dev)
  2223. {
  2224. struct p54_common *priv = dev->priv;
  2225. int i;
  2226. for (i = 0; i < ARRAY_SIZE(priv->leds); i++)
  2227. if (priv->leds[i].registered)
  2228. led_classdev_unregister(&priv->leds[i].led_dev);
  2229. }
  2230. #endif /* CONFIG_P54_LEDS */
  2231. static const struct ieee80211_ops p54_ops = {
  2232. .tx = p54_tx,
  2233. .start = p54_start,
  2234. .stop = p54_stop,
  2235. .add_interface = p54_add_interface,
  2236. .remove_interface = p54_remove_interface,
  2237. .set_tim = p54_set_tim,
  2238. .sta_notify = p54_sta_notify,
  2239. .set_key = p54_set_key,
  2240. .config = p54_config,
  2241. .config_interface = p54_config_interface,
  2242. .bss_info_changed = p54_bss_info_changed,
  2243. .configure_filter = p54_configure_filter,
  2244. .conf_tx = p54_conf_tx,
  2245. .get_stats = p54_get_stats,
  2246. .get_tx_stats = p54_get_tx_stats
  2247. };
  2248. struct ieee80211_hw *p54_init_common(size_t priv_data_len)
  2249. {
  2250. struct ieee80211_hw *dev;
  2251. struct p54_common *priv;
  2252. dev = ieee80211_alloc_hw(priv_data_len, &p54_ops);
  2253. if (!dev)
  2254. return NULL;
  2255. priv = dev->priv;
  2256. priv->hw = dev;
  2257. priv->mode = NL80211_IFTYPE_UNSPECIFIED;
  2258. priv->basic_rate_mask = 0x15f;
  2259. skb_queue_head_init(&priv->tx_queue);
  2260. dev->flags = IEEE80211_HW_RX_INCLUDES_FCS |
  2261. IEEE80211_HW_SIGNAL_DBM |
  2262. IEEE80211_HW_NOISE_DBM |
  2263. IEEE80211_HW_BEACON_FILTER;
  2264. dev->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
  2265. BIT(NL80211_IFTYPE_ADHOC) |
  2266. BIT(NL80211_IFTYPE_AP) |
  2267. BIT(NL80211_IFTYPE_MESH_POINT);
  2268. dev->channel_change_time = 1000; /* TODO: find actual value */
  2269. priv->tx_stats[P54_QUEUE_BEACON].limit = 1;
  2270. priv->tx_stats[P54_QUEUE_FWSCAN].limit = 1;
  2271. priv->tx_stats[P54_QUEUE_MGMT].limit = 3;
  2272. priv->tx_stats[P54_QUEUE_CAB].limit = 3;
  2273. priv->tx_stats[P54_QUEUE_DATA].limit = 5;
  2274. dev->queues = 1;
  2275. priv->noise = -94;
  2276. /*
  2277. * We support at most 8 tries no matter which rate they're at,
  2278. * we cannot support max_rates * max_rate_tries as we set it
  2279. * here, but setting it correctly to 4/2 or so would limit us
  2280. * artificially if the RC algorithm wants just two rates, so
  2281. * let's say 4/7, we'll redistribute it at TX time, see the
  2282. * comments there.
  2283. */
  2284. dev->max_rates = 4;
  2285. dev->max_rate_tries = 7;
  2286. dev->extra_tx_headroom = sizeof(struct p54_hdr) + 4 +
  2287. sizeof(struct p54_tx_data);
  2288. mutex_init(&priv->conf_mutex);
  2289. init_completion(&priv->eeprom_comp);
  2290. INIT_DELAYED_WORK(&priv->work, p54_work);
  2291. return dev;
  2292. }
  2293. EXPORT_SYMBOL_GPL(p54_init_common);
  2294. int p54_register_common(struct ieee80211_hw *dev, struct device *pdev)
  2295. {
  2296. int err;
  2297. err = ieee80211_register_hw(dev);
  2298. if (err) {
  2299. dev_err(pdev, "Cannot register device (%d).\n", err);
  2300. return err;
  2301. }
  2302. #ifdef CONFIG_P54_LEDS
  2303. err = p54_init_leds(dev);
  2304. if (err)
  2305. return err;
  2306. #endif /* CONFIG_P54_LEDS */
  2307. dev_info(pdev, "is registered as '%s'\n", wiphy_name(dev->wiphy));
  2308. return 0;
  2309. }
  2310. EXPORT_SYMBOL_GPL(p54_register_common);
  2311. void p54_free_common(struct ieee80211_hw *dev)
  2312. {
  2313. struct p54_common *priv = dev->priv;
  2314. kfree(priv->iq_autocal);
  2315. kfree(priv->output_limit);
  2316. kfree(priv->curve_data);
  2317. #ifdef CONFIG_P54_LEDS
  2318. p54_unregister_leds(dev);
  2319. #endif /* CONFIG_P54_LEDS */
  2320. }
  2321. EXPORT_SYMBOL_GPL(p54_free_common);
  2322. static int __init p54_init(void)
  2323. {
  2324. return 0;
  2325. }
  2326. static void __exit p54_exit(void)
  2327. {
  2328. }
  2329. module_init(p54_init);
  2330. module_exit(p54_exit);