p54common.c 72 KB

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