p54common.c 68 KB

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