p54common.c 72 KB

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