p54common.c 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885
  1. /*
  2. * Common code for mac80211 Prism54 drivers
  3. *
  4. * Copyright (c) 2006, Michael Wu <flamingice@sourmilk.net>
  5. * Copyright (c) 2007, Christian Lamparter <chunkeey@web.de>
  6. * Copyright 2008, Johannes Berg <johannes@sipsolutions.net>
  7. *
  8. * Based on:
  9. * - the islsm (softmac prism54) driver, which is:
  10. * Copyright 2004-2006 Jean-Baptiste Note <jbnote@gmail.com>, et al.
  11. * - stlc45xx driver
  12. * Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies).
  13. *
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License version 2 as
  16. * published by the Free Software Foundation.
  17. */
  18. #include <linux/init.h>
  19. #include <linux/firmware.h>
  20. #include <linux/etherdevice.h>
  21. #include <net/mac80211.h>
  22. #include "p54.h"
  23. #include "p54common.h"
  24. MODULE_AUTHOR("Michael Wu <flamingice@sourmilk.net>");
  25. MODULE_DESCRIPTION("Softmac Prism54 common code");
  26. MODULE_LICENSE("GPL");
  27. MODULE_ALIAS("prism54common");
  28. static struct ieee80211_rate p54_bgrates[] = {
  29. { .bitrate = 10, .hw_value = 0, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  30. { .bitrate = 20, .hw_value = 1, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  31. { .bitrate = 55, .hw_value = 2, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  32. { .bitrate = 110, .hw_value = 3, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  33. { .bitrate = 60, .hw_value = 4, },
  34. { .bitrate = 90, .hw_value = 5, },
  35. { .bitrate = 120, .hw_value = 6, },
  36. { .bitrate = 180, .hw_value = 7, },
  37. { .bitrate = 240, .hw_value = 8, },
  38. { .bitrate = 360, .hw_value = 9, },
  39. { .bitrate = 480, .hw_value = 10, },
  40. { .bitrate = 540, .hw_value = 11, },
  41. };
  42. static struct ieee80211_channel p54_bgchannels[] = {
  43. { .center_freq = 2412, .hw_value = 1, },
  44. { .center_freq = 2417, .hw_value = 2, },
  45. { .center_freq = 2422, .hw_value = 3, },
  46. { .center_freq = 2427, .hw_value = 4, },
  47. { .center_freq = 2432, .hw_value = 5, },
  48. { .center_freq = 2437, .hw_value = 6, },
  49. { .center_freq = 2442, .hw_value = 7, },
  50. { .center_freq = 2447, .hw_value = 8, },
  51. { .center_freq = 2452, .hw_value = 9, },
  52. { .center_freq = 2457, .hw_value = 10, },
  53. { .center_freq = 2462, .hw_value = 11, },
  54. { .center_freq = 2467, .hw_value = 12, },
  55. { .center_freq = 2472, .hw_value = 13, },
  56. { .center_freq = 2484, .hw_value = 14, },
  57. };
  58. static struct ieee80211_supported_band band_2GHz = {
  59. .channels = p54_bgchannels,
  60. .n_channels = ARRAY_SIZE(p54_bgchannels),
  61. .bitrates = p54_bgrates,
  62. .n_bitrates = ARRAY_SIZE(p54_bgrates),
  63. };
  64. static struct ieee80211_rate p54_arates[] = {
  65. { .bitrate = 60, .hw_value = 4, },
  66. { .bitrate = 90, .hw_value = 5, },
  67. { .bitrate = 120, .hw_value = 6, },
  68. { .bitrate = 180, .hw_value = 7, },
  69. { .bitrate = 240, .hw_value = 8, },
  70. { .bitrate = 360, .hw_value = 9, },
  71. { .bitrate = 480, .hw_value = 10, },
  72. { .bitrate = 540, .hw_value = 11, },
  73. };
  74. static struct ieee80211_channel p54_achannels[] = {
  75. { .center_freq = 4920 },
  76. { .center_freq = 4940 },
  77. { .center_freq = 4960 },
  78. { .center_freq = 4980 },
  79. { .center_freq = 5040 },
  80. { .center_freq = 5060 },
  81. { .center_freq = 5080 },
  82. { .center_freq = 5170 },
  83. { .center_freq = 5180 },
  84. { .center_freq = 5190 },
  85. { .center_freq = 5200 },
  86. { .center_freq = 5210 },
  87. { .center_freq = 5220 },
  88. { .center_freq = 5230 },
  89. { .center_freq = 5240 },
  90. { .center_freq = 5260 },
  91. { .center_freq = 5280 },
  92. { .center_freq = 5300 },
  93. { .center_freq = 5320 },
  94. { .center_freq = 5500 },
  95. { .center_freq = 5520 },
  96. { .center_freq = 5540 },
  97. { .center_freq = 5560 },
  98. { .center_freq = 5580 },
  99. { .center_freq = 5600 },
  100. { .center_freq = 5620 },
  101. { .center_freq = 5640 },
  102. { .center_freq = 5660 },
  103. { .center_freq = 5680 },
  104. { .center_freq = 5700 },
  105. { .center_freq = 5745 },
  106. { .center_freq = 5765 },
  107. { .center_freq = 5785 },
  108. { .center_freq = 5805 },
  109. { .center_freq = 5825 },
  110. };
  111. static struct ieee80211_supported_band band_5GHz = {
  112. .channels = p54_achannels,
  113. .n_channels = ARRAY_SIZE(p54_achannels),
  114. .bitrates = p54_arates,
  115. .n_bitrates = ARRAY_SIZE(p54_arates),
  116. };
  117. int p54_parse_firmware(struct ieee80211_hw *dev, const struct firmware *fw)
  118. {
  119. struct p54_common *priv = dev->priv;
  120. struct bootrec_exp_if *exp_if;
  121. struct bootrec *bootrec;
  122. u32 *data = (u32 *)fw->data;
  123. u32 *end_data = (u32 *)fw->data + (fw->size >> 2);
  124. u8 *fw_version = NULL;
  125. size_t len;
  126. int i;
  127. if (priv->rx_start)
  128. return 0;
  129. while (data < end_data && *data)
  130. data++;
  131. while (data < end_data && !*data)
  132. data++;
  133. bootrec = (struct bootrec *) data;
  134. while (bootrec->data <= end_data &&
  135. (bootrec->data + (len = le32_to_cpu(bootrec->len))) <= end_data) {
  136. u32 code = le32_to_cpu(bootrec->code);
  137. switch (code) {
  138. case BR_CODE_COMPONENT_ID:
  139. priv->fw_interface = be32_to_cpup((__be32 *)
  140. bootrec->data);
  141. switch (priv->fw_interface) {
  142. case FW_FMAC:
  143. printk(KERN_INFO "p54: FreeMAC firmware\n");
  144. break;
  145. case FW_LM20:
  146. printk(KERN_INFO "p54: LM20 firmware\n");
  147. break;
  148. case FW_LM86:
  149. printk(KERN_INFO "p54: LM86 firmware\n");
  150. break;
  151. case FW_LM87:
  152. printk(KERN_INFO "p54: LM87 firmware\n");
  153. break;
  154. default:
  155. printk(KERN_INFO "p54: unknown firmware\n");
  156. break;
  157. }
  158. break;
  159. case BR_CODE_COMPONENT_VERSION:
  160. /* 24 bytes should be enough for all firmwares */
  161. if (strnlen((unsigned char*)bootrec->data, 24) < 24)
  162. fw_version = (unsigned char*)bootrec->data;
  163. break;
  164. case BR_CODE_DESCR: {
  165. struct bootrec_desc *desc =
  166. (struct bootrec_desc *)bootrec->data;
  167. priv->rx_start = le32_to_cpu(desc->rx_start);
  168. /* FIXME add sanity checking */
  169. priv->rx_end = le32_to_cpu(desc->rx_end) - 0x3500;
  170. priv->headroom = desc->headroom;
  171. priv->tailroom = desc->tailroom;
  172. if (le32_to_cpu(bootrec->len) == 11)
  173. priv->rx_mtu = le16_to_cpu(desc->rx_mtu);
  174. else
  175. priv->rx_mtu = (size_t)
  176. 0x620 - priv->tx_hdr_len;
  177. break;
  178. }
  179. case BR_CODE_EXPOSED_IF:
  180. exp_if = (struct bootrec_exp_if *) bootrec->data;
  181. for (i = 0; i < (len * sizeof(*exp_if) / 4); i++)
  182. if (exp_if[i].if_id == cpu_to_le16(0x1a))
  183. priv->fw_var = le16_to_cpu(exp_if[i].variant);
  184. break;
  185. case BR_CODE_DEPENDENT_IF:
  186. break;
  187. case BR_CODE_END_OF_BRA:
  188. case LEGACY_BR_CODE_END_OF_BRA:
  189. end_data = NULL;
  190. break;
  191. default:
  192. break;
  193. }
  194. bootrec = (struct bootrec *)&bootrec->data[len];
  195. }
  196. if (fw_version)
  197. printk(KERN_INFO "p54: FW rev %s - Softmac protocol %x.%x\n",
  198. fw_version, priv->fw_var >> 8, priv->fw_var & 0xff);
  199. if (priv->fw_var < 0x500)
  200. printk(KERN_INFO "p54: you are using an obsolete firmware. "
  201. "visit http://wireless.kernel.org/en/users/Drivers/p54 "
  202. "and grab one for \"kernel >= 2.6.28\"!\n");
  203. if (priv->fw_var >= 0x300) {
  204. /* Firmware supports QoS, use it! */
  205. priv->tx_stats[4].limit = 3; /* AC_VO */
  206. priv->tx_stats[5].limit = 4; /* AC_VI */
  207. priv->tx_stats[6].limit = 3; /* AC_BE */
  208. priv->tx_stats[7].limit = 2; /* AC_BK */
  209. dev->queues = 4;
  210. }
  211. return 0;
  212. }
  213. EXPORT_SYMBOL_GPL(p54_parse_firmware);
  214. static int p54_convert_rev0(struct ieee80211_hw *dev,
  215. struct pda_pa_curve_data *curve_data)
  216. {
  217. struct p54_common *priv = dev->priv;
  218. struct p54_pa_curve_data_sample *dst;
  219. struct pda_pa_curve_data_sample_rev0 *src;
  220. size_t cd_len = sizeof(*curve_data) +
  221. (curve_data->points_per_channel*sizeof(*dst) + 2) *
  222. curve_data->channels;
  223. unsigned int i, j;
  224. void *source, *target;
  225. priv->curve_data = kmalloc(cd_len, GFP_KERNEL);
  226. if (!priv->curve_data)
  227. return -ENOMEM;
  228. memcpy(priv->curve_data, curve_data, sizeof(*curve_data));
  229. source = curve_data->data;
  230. target = priv->curve_data->data;
  231. for (i = 0; i < curve_data->channels; i++) {
  232. __le16 *freq = source;
  233. source += sizeof(__le16);
  234. *((__le16 *)target) = *freq;
  235. target += sizeof(__le16);
  236. for (j = 0; j < curve_data->points_per_channel; j++) {
  237. dst = target;
  238. src = source;
  239. dst->rf_power = src->rf_power;
  240. dst->pa_detector = src->pa_detector;
  241. dst->data_64qam = src->pcv;
  242. /* "invent" the points for the other modulations */
  243. #define SUB(x,y) (u8)((x) - (y)) > (x) ? 0 : (x) - (y)
  244. dst->data_16qam = SUB(src->pcv, 12);
  245. dst->data_qpsk = SUB(dst->data_16qam, 12);
  246. dst->data_bpsk = SUB(dst->data_qpsk, 12);
  247. dst->data_barker = SUB(dst->data_bpsk, 14);
  248. #undef SUB
  249. target += sizeof(*dst);
  250. source += sizeof(*src);
  251. }
  252. }
  253. return 0;
  254. }
  255. static int p54_convert_rev1(struct ieee80211_hw *dev,
  256. struct pda_pa_curve_data *curve_data)
  257. {
  258. struct p54_common *priv = dev->priv;
  259. struct p54_pa_curve_data_sample *dst;
  260. struct pda_pa_curve_data_sample_rev1 *src;
  261. size_t cd_len = sizeof(*curve_data) +
  262. (curve_data->points_per_channel*sizeof(*dst) + 2) *
  263. curve_data->channels;
  264. unsigned int i, j;
  265. void *source, *target;
  266. priv->curve_data = kmalloc(cd_len, GFP_KERNEL);
  267. if (!priv->curve_data)
  268. return -ENOMEM;
  269. memcpy(priv->curve_data, curve_data, sizeof(*curve_data));
  270. source = curve_data->data;
  271. target = priv->curve_data->data;
  272. for (i = 0; i < curve_data->channels; i++) {
  273. __le16 *freq = source;
  274. source += sizeof(__le16);
  275. *((__le16 *)target) = *freq;
  276. target += sizeof(__le16);
  277. for (j = 0; j < curve_data->points_per_channel; j++) {
  278. memcpy(target, source, sizeof(*src));
  279. target += sizeof(*dst);
  280. source += sizeof(*src);
  281. }
  282. source++;
  283. }
  284. return 0;
  285. }
  286. static const char *p54_rf_chips[] = { "NULL", "Duette3", "Duette2",
  287. "Frisbee", "Xbow", "Longbow", "NULL", "NULL" };
  288. static int p54_init_xbow_synth(struct ieee80211_hw *dev);
  289. static int p54_parse_eeprom(struct ieee80211_hw *dev, void *eeprom, int len)
  290. {
  291. struct p54_common *priv = dev->priv;
  292. struct eeprom_pda_wrap *wrap = NULL;
  293. struct pda_entry *entry;
  294. unsigned int data_len, entry_len;
  295. void *tmp;
  296. int err;
  297. u8 *end = (u8 *)eeprom + len;
  298. u16 synth = 0;
  299. wrap = (struct eeprom_pda_wrap *) eeprom;
  300. entry = (void *)wrap->data + le16_to_cpu(wrap->len);
  301. /* verify that at least the entry length/code fits */
  302. while ((u8 *)entry <= end - sizeof(*entry)) {
  303. entry_len = le16_to_cpu(entry->len);
  304. data_len = ((entry_len - 1) << 1);
  305. /* abort if entry exceeds whole structure */
  306. if ((u8 *)entry + sizeof(*entry) + data_len > end)
  307. break;
  308. switch (le16_to_cpu(entry->code)) {
  309. case PDR_MAC_ADDRESS:
  310. SET_IEEE80211_PERM_ADDR(dev, entry->data);
  311. break;
  312. case PDR_PRISM_PA_CAL_OUTPUT_POWER_LIMITS:
  313. if (data_len < 2) {
  314. err = -EINVAL;
  315. goto err;
  316. }
  317. if (2 + entry->data[1]*sizeof(*priv->output_limit) > data_len) {
  318. err = -EINVAL;
  319. goto err;
  320. }
  321. priv->output_limit = kmalloc(entry->data[1] *
  322. sizeof(*priv->output_limit), GFP_KERNEL);
  323. if (!priv->output_limit) {
  324. err = -ENOMEM;
  325. goto err;
  326. }
  327. memcpy(priv->output_limit, &entry->data[2],
  328. entry->data[1]*sizeof(*priv->output_limit));
  329. priv->output_limit_len = entry->data[1];
  330. break;
  331. case PDR_PRISM_PA_CAL_CURVE_DATA: {
  332. struct pda_pa_curve_data *curve_data =
  333. (struct pda_pa_curve_data *)entry->data;
  334. if (data_len < sizeof(*curve_data)) {
  335. err = -EINVAL;
  336. goto err;
  337. }
  338. switch (curve_data->cal_method_rev) {
  339. case 0:
  340. err = p54_convert_rev0(dev, curve_data);
  341. break;
  342. case 1:
  343. err = p54_convert_rev1(dev, curve_data);
  344. break;
  345. default:
  346. printk(KERN_ERR "p54: unknown curve data "
  347. "revision %d\n",
  348. curve_data->cal_method_rev);
  349. err = -ENODEV;
  350. break;
  351. }
  352. if (err)
  353. goto err;
  354. }
  355. case PDR_PRISM_ZIF_TX_IQ_CALIBRATION:
  356. priv->iq_autocal = kmalloc(data_len, GFP_KERNEL);
  357. if (!priv->iq_autocal) {
  358. err = -ENOMEM;
  359. goto err;
  360. }
  361. memcpy(priv->iq_autocal, entry->data, data_len);
  362. priv->iq_autocal_len = data_len / sizeof(struct pda_iq_autocal_entry);
  363. break;
  364. case PDR_INTERFACE_LIST:
  365. tmp = entry->data;
  366. while ((u8 *)tmp < entry->data + data_len) {
  367. struct bootrec_exp_if *exp_if = tmp;
  368. if (le16_to_cpu(exp_if->if_id) == 0xf)
  369. synth = le16_to_cpu(exp_if->variant);
  370. tmp += sizeof(struct bootrec_exp_if);
  371. }
  372. break;
  373. case PDR_HARDWARE_PLATFORM_COMPONENT_ID:
  374. priv->version = *(u8 *)(entry->data + 1);
  375. break;
  376. case PDR_END:
  377. /* make it overrun */
  378. entry_len = len;
  379. break;
  380. case PDR_MANUFACTURING_PART_NUMBER:
  381. case PDR_PDA_VERSION:
  382. case PDR_NIC_SERIAL_NUMBER:
  383. case PDR_REGULATORY_DOMAIN_LIST:
  384. case PDR_TEMPERATURE_TYPE:
  385. case PDR_PRISM_PCI_IDENTIFIER:
  386. case PDR_COUNTRY_INFORMATION:
  387. case PDR_OEM_NAME:
  388. case PDR_PRODUCT_NAME:
  389. case PDR_UTF8_OEM_NAME:
  390. case PDR_UTF8_PRODUCT_NAME:
  391. case PDR_COUNTRY_LIST:
  392. case PDR_DEFAULT_COUNTRY:
  393. case PDR_ANTENNA_GAIN:
  394. case PDR_PRISM_INDIGO_PA_CALIBRATION_DATA:
  395. case PDR_RSSI_LINEAR_APPROXIMATION:
  396. case PDR_RSSI_LINEAR_APPROXIMATION_DUAL_BAND:
  397. case PDR_REGULATORY_POWER_LIMITS:
  398. case PDR_RSSI_LINEAR_APPROXIMATION_EXTENDED:
  399. case PDR_RADIATED_TRANSMISSION_CORRECTION:
  400. case PDR_PRISM_TX_IQ_CALIBRATION:
  401. case PDR_BASEBAND_REGISTERS:
  402. case PDR_PER_CHANNEL_BASEBAND_REGISTERS:
  403. break;
  404. default:
  405. printk(KERN_INFO "p54: unknown eeprom code : 0x%x\n",
  406. le16_to_cpu(entry->code));
  407. break;
  408. }
  409. entry = (void *)entry + (entry_len + 1)*2;
  410. }
  411. if (!synth || !priv->iq_autocal || !priv->output_limit ||
  412. !priv->curve_data) {
  413. printk(KERN_ERR "p54: not all required entries found in eeprom!\n");
  414. err = -EINVAL;
  415. goto err;
  416. }
  417. priv->rxhw = synth & PDR_SYNTH_FRONTEND_MASK;
  418. if (priv->rxhw == 4)
  419. p54_init_xbow_synth(dev);
  420. if (!(synth & PDR_SYNTH_24_GHZ_DISABLED))
  421. dev->wiphy->bands[IEEE80211_BAND_2GHZ] = &band_2GHz;
  422. if (!(synth & PDR_SYNTH_5_GHZ_DISABLED))
  423. dev->wiphy->bands[IEEE80211_BAND_5GHZ] = &band_5GHz;
  424. if (!is_valid_ether_addr(dev->wiphy->perm_addr)) {
  425. u8 perm_addr[ETH_ALEN];
  426. printk(KERN_WARNING "%s: Invalid hwaddr! Using randomly generated MAC addr\n",
  427. wiphy_name(dev->wiphy));
  428. random_ether_addr(perm_addr);
  429. SET_IEEE80211_PERM_ADDR(dev, perm_addr);
  430. }
  431. printk(KERN_INFO "%s: hwaddr %pM, MAC:isl38%02x RF:%s\n",
  432. wiphy_name(dev->wiphy),
  433. dev->wiphy->perm_addr,
  434. priv->version, p54_rf_chips[priv->rxhw]);
  435. return 0;
  436. err:
  437. if (priv->iq_autocal) {
  438. kfree(priv->iq_autocal);
  439. priv->iq_autocal = NULL;
  440. }
  441. if (priv->output_limit) {
  442. kfree(priv->output_limit);
  443. priv->output_limit = NULL;
  444. }
  445. if (priv->curve_data) {
  446. kfree(priv->curve_data);
  447. priv->curve_data = NULL;
  448. }
  449. printk(KERN_ERR "p54: eeprom parse failed!\n");
  450. return err;
  451. }
  452. static int p54_rssi_to_dbm(struct ieee80211_hw *dev, int rssi)
  453. {
  454. /* TODO: get the rssi_add & rssi_mul data from the eeprom */
  455. return ((rssi * 0x83) / 64 - 400) / 4;
  456. }
  457. static int p54_rx_data(struct ieee80211_hw *dev, struct sk_buff *skb)
  458. {
  459. struct p54_common *priv = dev->priv;
  460. struct p54_rx_data *hdr = (struct p54_rx_data *) skb->data;
  461. struct ieee80211_rx_status rx_status = {0};
  462. u16 freq = le16_to_cpu(hdr->freq);
  463. size_t header_len = sizeof(*hdr);
  464. u32 tsf32;
  465. if (!(hdr->flags & cpu_to_le16(P54_HDR_FLAG_DATA_IN_FCS_GOOD))) {
  466. if (priv->filter_flags & FIF_FCSFAIL)
  467. rx_status.flag |= RX_FLAG_FAILED_FCS_CRC;
  468. else
  469. return 0;
  470. }
  471. rx_status.signal = p54_rssi_to_dbm(dev, hdr->rssi);
  472. rx_status.noise = priv->noise;
  473. /* XX correct? */
  474. rx_status.qual = (100 * hdr->rssi) / 127;
  475. rx_status.rate_idx = (dev->conf.channel->band == IEEE80211_BAND_2GHZ ?
  476. hdr->rate : (hdr->rate - 4)) & 0xf;
  477. rx_status.freq = freq;
  478. rx_status.band = dev->conf.channel->band;
  479. rx_status.antenna = hdr->antenna;
  480. tsf32 = le32_to_cpu(hdr->tsf32);
  481. if (tsf32 < priv->tsf_low32)
  482. priv->tsf_high32++;
  483. rx_status.mactime = ((u64)priv->tsf_high32) << 32 | tsf32;
  484. priv->tsf_low32 = tsf32;
  485. rx_status.flag |= RX_FLAG_TSFT;
  486. if (hdr->flags & cpu_to_le16(P54_HDR_FLAG_DATA_ALIGN))
  487. header_len += hdr->align[0];
  488. skb_pull(skb, header_len);
  489. skb_trim(skb, le16_to_cpu(hdr->len));
  490. ieee80211_rx_irqsafe(dev, skb, &rx_status);
  491. return -1;
  492. }
  493. static void inline p54_wake_free_queues(struct ieee80211_hw *dev)
  494. {
  495. struct p54_common *priv = dev->priv;
  496. int i;
  497. if (priv->mode == NL80211_IFTYPE_UNSPECIFIED)
  498. return ;
  499. for (i = 0; i < dev->queues; i++)
  500. if (priv->tx_stats[i + 4].len < priv->tx_stats[i + 4].limit)
  501. ieee80211_wake_queue(dev, i);
  502. }
  503. void p54_free_skb(struct ieee80211_hw *dev, struct sk_buff *skb)
  504. {
  505. struct p54_common *priv = dev->priv;
  506. struct ieee80211_tx_info *info;
  507. struct memrecord *range;
  508. unsigned long flags;
  509. u32 freed = 0, last_addr = priv->rx_start;
  510. if (!skb || !dev)
  511. return;
  512. spin_lock_irqsave(&priv->tx_queue.lock, flags);
  513. info = IEEE80211_SKB_CB(skb);
  514. range = (void *)info->rate_driver_data;
  515. if (skb->prev != (struct sk_buff *)&priv->tx_queue) {
  516. struct ieee80211_tx_info *ni;
  517. struct memrecord *mr;
  518. ni = IEEE80211_SKB_CB(skb->prev);
  519. mr = (struct memrecord *)ni->rate_driver_data;
  520. last_addr = mr->end_addr;
  521. }
  522. if (skb->next != (struct sk_buff *)&priv->tx_queue) {
  523. struct ieee80211_tx_info *ni;
  524. struct memrecord *mr;
  525. ni = IEEE80211_SKB_CB(skb->next);
  526. mr = (struct memrecord *)ni->rate_driver_data;
  527. freed = mr->start_addr - last_addr;
  528. } else
  529. freed = priv->rx_end - last_addr;
  530. __skb_unlink(skb, &priv->tx_queue);
  531. spin_unlock_irqrestore(&priv->tx_queue.lock, flags);
  532. kfree_skb(skb);
  533. if (freed >= priv->headroom + sizeof(struct p54_hdr) + 48 +
  534. IEEE80211_MAX_RTS_THRESHOLD + priv->tailroom)
  535. p54_wake_free_queues(dev);
  536. }
  537. EXPORT_SYMBOL_GPL(p54_free_skb);
  538. static void p54_rx_frame_sent(struct ieee80211_hw *dev, struct sk_buff *skb)
  539. {
  540. struct p54_common *priv = dev->priv;
  541. struct p54_hdr *hdr = (struct p54_hdr *) skb->data;
  542. struct p54_frame_sent *payload = (struct p54_frame_sent *) hdr->data;
  543. struct sk_buff *entry = (struct sk_buff *) priv->tx_queue.next;
  544. u32 addr = le32_to_cpu(hdr->req_id) - priv->headroom;
  545. struct memrecord *range = NULL;
  546. u32 freed = 0;
  547. u32 last_addr = priv->rx_start;
  548. unsigned long flags;
  549. int count, idx;
  550. spin_lock_irqsave(&priv->tx_queue.lock, flags);
  551. while (entry != (struct sk_buff *)&priv->tx_queue) {
  552. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(entry);
  553. struct p54_hdr *entry_hdr;
  554. struct p54_tx_data *entry_data;
  555. int pad = 0;
  556. range = (void *)info->rate_driver_data;
  557. if (range->start_addr != addr) {
  558. last_addr = range->end_addr;
  559. entry = entry->next;
  560. continue;
  561. }
  562. if (entry->next != (struct sk_buff *)&priv->tx_queue) {
  563. struct ieee80211_tx_info *ni;
  564. struct memrecord *mr;
  565. ni = IEEE80211_SKB_CB(entry->next);
  566. mr = (struct memrecord *)ni->rate_driver_data;
  567. freed = mr->start_addr - last_addr;
  568. } else
  569. freed = priv->rx_end - last_addr;
  570. last_addr = range->end_addr;
  571. __skb_unlink(entry, &priv->tx_queue);
  572. spin_unlock_irqrestore(&priv->tx_queue.lock, flags);
  573. if (unlikely(entry == priv->cached_beacon)) {
  574. kfree_skb(entry);
  575. priv->cached_beacon = NULL;
  576. goto out;
  577. }
  578. /*
  579. * Clear manually, ieee80211_tx_info_clear_status would
  580. * clear the counts too and we need them.
  581. */
  582. memset(&info->status.ampdu_ack_len, 0,
  583. sizeof(struct ieee80211_tx_info) -
  584. offsetof(struct ieee80211_tx_info, status.ampdu_ack_len));
  585. BUILD_BUG_ON(offsetof(struct ieee80211_tx_info,
  586. status.ampdu_ack_len) != 23);
  587. entry_hdr = (struct p54_hdr *) entry->data;
  588. entry_data = (struct p54_tx_data *) entry_hdr->data;
  589. if (entry_hdr->flags & cpu_to_le16(P54_HDR_FLAG_DATA_ALIGN))
  590. pad = entry_data->align[0];
  591. /* walk through the rates array and adjust the counts */
  592. count = payload->tries;
  593. for (idx = 0; idx < 4; idx++) {
  594. if (count >= info->status.rates[idx].count) {
  595. count -= info->status.rates[idx].count;
  596. } else if (count > 0) {
  597. info->status.rates[idx].count = count;
  598. count = 0;
  599. } else {
  600. info->status.rates[idx].idx = -1;
  601. info->status.rates[idx].count = 0;
  602. }
  603. }
  604. priv->tx_stats[entry_data->hw_queue].len--;
  605. if (!(info->flags & IEEE80211_TX_CTL_NO_ACK) &&
  606. (!payload->status))
  607. info->flags |= IEEE80211_TX_STAT_ACK;
  608. if (payload->status & P54_TX_PSM_CANCELLED)
  609. info->flags |= IEEE80211_TX_STAT_TX_FILTERED;
  610. info->status.ack_signal = p54_rssi_to_dbm(dev,
  611. (int)payload->ack_rssi);
  612. skb_pull(entry, sizeof(*hdr) + pad + sizeof(*entry_data));
  613. ieee80211_tx_status_irqsafe(dev, entry);
  614. goto out;
  615. }
  616. spin_unlock_irqrestore(&priv->tx_queue.lock, flags);
  617. out:
  618. if (freed >= priv->headroom + sizeof(struct p54_hdr) + 48 +
  619. IEEE80211_MAX_RTS_THRESHOLD + priv->tailroom)
  620. p54_wake_free_queues(dev);
  621. }
  622. static void p54_rx_eeprom_readback(struct ieee80211_hw *dev,
  623. struct sk_buff *skb)
  624. {
  625. struct p54_hdr *hdr = (struct p54_hdr *) skb->data;
  626. struct p54_eeprom_lm86 *eeprom = (struct p54_eeprom_lm86 *) hdr->data;
  627. struct p54_common *priv = dev->priv;
  628. if (!priv->eeprom)
  629. return ;
  630. memcpy(priv->eeprom, eeprom->data, le16_to_cpu(eeprom->len));
  631. complete(&priv->eeprom_comp);
  632. }
  633. static void p54_rx_stats(struct ieee80211_hw *dev, struct sk_buff *skb)
  634. {
  635. struct p54_common *priv = dev->priv;
  636. struct p54_hdr *hdr = (struct p54_hdr *) skb->data;
  637. struct p54_statistics *stats = (struct p54_statistics *) hdr->data;
  638. u32 tsf32 = le32_to_cpu(stats->tsf32);
  639. if (tsf32 < priv->tsf_low32)
  640. priv->tsf_high32++;
  641. priv->tsf_low32 = tsf32;
  642. priv->stats.dot11RTSFailureCount = le32_to_cpu(stats->rts_fail);
  643. priv->stats.dot11RTSSuccessCount = le32_to_cpu(stats->rts_success);
  644. priv->stats.dot11FCSErrorCount = le32_to_cpu(stats->rx_bad_fcs);
  645. priv->noise = p54_rssi_to_dbm(dev, le32_to_cpu(stats->noise));
  646. complete(&priv->stats_comp);
  647. mod_timer(&priv->stats_timer, jiffies + 5 * HZ);
  648. }
  649. static void p54_rx_trap(struct ieee80211_hw *dev, struct sk_buff *skb)
  650. {
  651. struct p54_hdr *hdr = (struct p54_hdr *) skb->data;
  652. struct p54_trap *trap = (struct p54_trap *) hdr->data;
  653. u16 event = le16_to_cpu(trap->event);
  654. u16 freq = le16_to_cpu(trap->frequency);
  655. switch (event) {
  656. case P54_TRAP_BEACON_TX:
  657. break;
  658. case P54_TRAP_RADAR:
  659. printk(KERN_INFO "%s: radar (freq:%d MHz)\n",
  660. wiphy_name(dev->wiphy), freq);
  661. break;
  662. case P54_TRAP_NO_BEACON:
  663. break;
  664. case P54_TRAP_SCAN:
  665. break;
  666. case P54_TRAP_TBTT:
  667. break;
  668. case P54_TRAP_TIMER:
  669. break;
  670. default:
  671. printk(KERN_INFO "%s: received event:%x freq:%d\n",
  672. wiphy_name(dev->wiphy), event, freq);
  673. break;
  674. }
  675. }
  676. static int p54_rx_control(struct ieee80211_hw *dev, struct sk_buff *skb)
  677. {
  678. struct p54_hdr *hdr = (struct p54_hdr *) skb->data;
  679. switch (le16_to_cpu(hdr->type)) {
  680. case P54_CONTROL_TYPE_TXDONE:
  681. p54_rx_frame_sent(dev, skb);
  682. break;
  683. case P54_CONTROL_TYPE_TRAP:
  684. p54_rx_trap(dev, skb);
  685. break;
  686. case P54_CONTROL_TYPE_BBP:
  687. break;
  688. case P54_CONTROL_TYPE_STAT_READBACK:
  689. p54_rx_stats(dev, skb);
  690. break;
  691. case P54_CONTROL_TYPE_EEPROM_READBACK:
  692. p54_rx_eeprom_readback(dev, skb);
  693. break;
  694. default:
  695. printk(KERN_DEBUG "%s: not handling 0x%02x type control frame\n",
  696. wiphy_name(dev->wiphy), le16_to_cpu(hdr->type));
  697. break;
  698. }
  699. return 0;
  700. }
  701. /* returns zero if skb can be reused */
  702. int p54_rx(struct ieee80211_hw *dev, struct sk_buff *skb)
  703. {
  704. u16 type = le16_to_cpu(*((__le16 *)skb->data));
  705. if (type & P54_HDR_FLAG_CONTROL)
  706. return p54_rx_control(dev, skb);
  707. else
  708. return p54_rx_data(dev, skb);
  709. }
  710. EXPORT_SYMBOL_GPL(p54_rx);
  711. /*
  712. * So, the firmware is somewhat stupid and doesn't know what places in its
  713. * memory incoming data should go to. By poking around in the firmware, we
  714. * can find some unused memory to upload our packets to. However, data that we
  715. * want the card to TX needs to stay intact until the card has told us that
  716. * it is done with it. This function finds empty places we can upload to and
  717. * marks allocated areas as reserved if necessary. p54_rx_frame_sent frees
  718. * allocated areas.
  719. */
  720. static int p54_assign_address(struct ieee80211_hw *dev, struct sk_buff *skb,
  721. struct p54_hdr *data, u32 len)
  722. {
  723. struct p54_common *priv = dev->priv;
  724. struct sk_buff *entry = priv->tx_queue.next;
  725. struct sk_buff *target_skb = NULL;
  726. struct ieee80211_tx_info *info;
  727. struct memrecord *range;
  728. u32 last_addr = priv->rx_start;
  729. u32 largest_hole = 0;
  730. u32 target_addr = priv->rx_start;
  731. unsigned long flags;
  732. unsigned int left;
  733. len = (len + priv->headroom + priv->tailroom + 3) & ~0x3;
  734. if (!skb)
  735. return -EINVAL;
  736. spin_lock_irqsave(&priv->tx_queue.lock, flags);
  737. left = skb_queue_len(&priv->tx_queue);
  738. while (left--) {
  739. u32 hole_size;
  740. info = IEEE80211_SKB_CB(entry);
  741. range = (void *)info->rate_driver_data;
  742. hole_size = range->start_addr - last_addr;
  743. if (!target_skb && hole_size >= len) {
  744. target_skb = entry->prev;
  745. hole_size -= len;
  746. target_addr = last_addr;
  747. }
  748. largest_hole = max(largest_hole, hole_size);
  749. last_addr = range->end_addr;
  750. entry = entry->next;
  751. }
  752. if (!target_skb && priv->rx_end - last_addr >= len) {
  753. target_skb = priv->tx_queue.prev;
  754. largest_hole = max(largest_hole, priv->rx_end - last_addr - len);
  755. if (!skb_queue_empty(&priv->tx_queue)) {
  756. info = IEEE80211_SKB_CB(target_skb);
  757. range = (void *)info->rate_driver_data;
  758. target_addr = range->end_addr;
  759. }
  760. } else
  761. largest_hole = max(largest_hole, priv->rx_end - last_addr);
  762. if (!target_skb) {
  763. spin_unlock_irqrestore(&priv->tx_queue.lock, flags);
  764. ieee80211_stop_queues(dev);
  765. return -ENOMEM;
  766. }
  767. info = IEEE80211_SKB_CB(skb);
  768. range = (void *)info->rate_driver_data;
  769. range->start_addr = target_addr;
  770. range->end_addr = target_addr + len;
  771. __skb_queue_after(&priv->tx_queue, target_skb, skb);
  772. spin_unlock_irqrestore(&priv->tx_queue.lock, flags);
  773. if (largest_hole < priv->headroom + sizeof(struct p54_hdr) +
  774. 48 + IEEE80211_MAX_RTS_THRESHOLD + priv->tailroom)
  775. ieee80211_stop_queues(dev);
  776. data->req_id = cpu_to_le32(target_addr + priv->headroom);
  777. return 0;
  778. }
  779. static struct sk_buff *p54_alloc_skb(struct ieee80211_hw *dev,
  780. u16 hdr_flags, u16 len, u16 type, gfp_t memflags)
  781. {
  782. struct p54_common *priv = dev->priv;
  783. struct p54_hdr *hdr;
  784. struct sk_buff *skb;
  785. skb = __dev_alloc_skb(len + priv->tx_hdr_len, memflags);
  786. if (!skb)
  787. return NULL;
  788. skb_reserve(skb, priv->tx_hdr_len);
  789. hdr = (struct p54_hdr *) skb_put(skb, sizeof(*hdr));
  790. hdr->flags = cpu_to_le16(hdr_flags);
  791. hdr->len = cpu_to_le16(len - sizeof(*hdr));
  792. hdr->type = cpu_to_le16(type);
  793. hdr->tries = hdr->rts_tries = 0;
  794. if (unlikely(p54_assign_address(dev, skb, hdr, len))) {
  795. kfree_skb(skb);
  796. return NULL;
  797. }
  798. return skb;
  799. }
  800. int p54_read_eeprom(struct ieee80211_hw *dev)
  801. {
  802. struct p54_common *priv = dev->priv;
  803. struct p54_hdr *hdr = NULL;
  804. struct p54_eeprom_lm86 *eeprom_hdr;
  805. struct sk_buff *skb;
  806. size_t eeprom_size = 0x2020, offset = 0, blocksize;
  807. int ret = -ENOMEM;
  808. void *eeprom = NULL;
  809. skb = p54_alloc_skb(dev, 0x8000, sizeof(*hdr) + sizeof(*eeprom_hdr) +
  810. EEPROM_READBACK_LEN,
  811. P54_CONTROL_TYPE_EEPROM_READBACK, GFP_KERNEL);
  812. if (!skb)
  813. goto free;
  814. priv->eeprom = kzalloc(EEPROM_READBACK_LEN, GFP_KERNEL);
  815. if (!priv->eeprom)
  816. goto free;
  817. eeprom = kzalloc(eeprom_size, GFP_KERNEL);
  818. if (!eeprom)
  819. goto free;
  820. eeprom_hdr = (struct p54_eeprom_lm86 *) skb_put(skb,
  821. sizeof(*eeprom_hdr) + EEPROM_READBACK_LEN);
  822. while (eeprom_size) {
  823. blocksize = min(eeprom_size, (size_t)EEPROM_READBACK_LEN);
  824. eeprom_hdr->offset = cpu_to_le16(offset);
  825. eeprom_hdr->len = cpu_to_le16(blocksize);
  826. priv->tx(dev, skb, 0);
  827. if (!wait_for_completion_interruptible_timeout(&priv->eeprom_comp, HZ)) {
  828. printk(KERN_ERR "%s: device does not respond!\n",
  829. wiphy_name(dev->wiphy));
  830. ret = -EBUSY;
  831. goto free;
  832. }
  833. memcpy(eeprom + offset, priv->eeprom, blocksize);
  834. offset += blocksize;
  835. eeprom_size -= blocksize;
  836. }
  837. ret = p54_parse_eeprom(dev, eeprom, offset);
  838. free:
  839. kfree(priv->eeprom);
  840. priv->eeprom = NULL;
  841. p54_free_skb(dev, skb);
  842. kfree(eeprom);
  843. return ret;
  844. }
  845. EXPORT_SYMBOL_GPL(p54_read_eeprom);
  846. static int p54_set_tim(struct ieee80211_hw *dev, struct ieee80211_sta *sta,
  847. bool set)
  848. {
  849. struct p54_common *priv = dev->priv;
  850. struct sk_buff *skb;
  851. struct p54_tim *tim;
  852. skb = p54_alloc_skb(dev, P54_HDR_FLAG_CONTROL_OPSET,
  853. sizeof(struct p54_hdr) + sizeof(*tim),
  854. P54_CONTROL_TYPE_TIM, GFP_KERNEL);
  855. if (!skb)
  856. return -ENOMEM;
  857. tim = (struct p54_tim *) skb_put(skb, sizeof(*tim));
  858. tim->count = 1;
  859. tim->entry[0] = cpu_to_le16(set ? (sta->aid | 0x8000) : sta->aid);
  860. priv->tx(dev, skb, 1);
  861. return 0;
  862. }
  863. static int p54_sta_unlock(struct ieee80211_hw *dev, u8 *addr)
  864. {
  865. struct p54_common *priv = dev->priv;
  866. struct sk_buff *skb;
  867. struct p54_sta_unlock *sta;
  868. skb = p54_alloc_skb(dev, P54_HDR_FLAG_CONTROL_OPSET,
  869. sizeof(struct p54_hdr) + sizeof(*sta),
  870. P54_CONTROL_TYPE_PSM_STA_UNLOCK, GFP_ATOMIC);
  871. if (!skb)
  872. return -ENOMEM;
  873. sta = (struct p54_sta_unlock *)skb_put(skb, sizeof(*sta));
  874. memcpy(sta->addr, addr, ETH_ALEN);
  875. priv->tx(dev, skb, 1);
  876. return 0;
  877. }
  878. static int p54_tx_cancel(struct ieee80211_hw *dev, struct sk_buff *entry)
  879. {
  880. struct p54_common *priv = dev->priv;
  881. struct sk_buff *skb;
  882. struct p54_hdr *hdr;
  883. struct p54_txcancel *cancel;
  884. skb = p54_alloc_skb(dev, P54_HDR_FLAG_CONTROL_OPSET,
  885. sizeof(struct p54_hdr) + sizeof(*cancel),
  886. P54_CONTROL_TYPE_TXCANCEL, GFP_ATOMIC);
  887. if (!skb)
  888. return -ENOMEM;
  889. hdr = (void *)entry->data;
  890. cancel = (struct p54_txcancel *)skb_put(skb, sizeof(*cancel));
  891. cancel->req_id = hdr->req_id;
  892. priv->tx(dev, skb, 1);
  893. return 0;
  894. }
  895. static int p54_tx_fill(struct ieee80211_hw *dev, struct sk_buff *skb,
  896. struct ieee80211_tx_info *info, u8 *queue, size_t *extra_len,
  897. u16 *flags, u16 *aid)
  898. {
  899. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  900. struct p54_common *priv = dev->priv;
  901. int ret = 0;
  902. if (unlikely(ieee80211_is_mgmt(hdr->frame_control))) {
  903. if (ieee80211_is_beacon(hdr->frame_control)) {
  904. *aid = 0;
  905. *queue = 0;
  906. *extra_len = IEEE80211_MAX_TIM_LEN;
  907. *flags = P54_HDR_FLAG_DATA_OUT_TIMESTAMP;
  908. return 0;
  909. } else if (ieee80211_is_probe_resp(hdr->frame_control)) {
  910. *aid = 0;
  911. *queue = 2;
  912. *flags = P54_HDR_FLAG_DATA_OUT_TIMESTAMP |
  913. P54_HDR_FLAG_DATA_OUT_NOCANCEL;
  914. return 0;
  915. } else {
  916. *queue = 2;
  917. ret = 0;
  918. }
  919. } else {
  920. *queue += 4;
  921. ret = 1;
  922. }
  923. switch (priv->mode) {
  924. case NL80211_IFTYPE_STATION:
  925. *aid = 1;
  926. break;
  927. case NL80211_IFTYPE_AP:
  928. case NL80211_IFTYPE_ADHOC:
  929. if (info->flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM) {
  930. *aid = 0;
  931. *queue = 3;
  932. return 0;
  933. }
  934. if (info->control.sta)
  935. *aid = info->control.sta->aid;
  936. else
  937. *flags = P54_HDR_FLAG_DATA_OUT_NOCANCEL;
  938. }
  939. return ret;
  940. }
  941. static int p54_tx(struct ieee80211_hw *dev, struct sk_buff *skb)
  942. {
  943. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  944. struct ieee80211_tx_queue_stats *current_queue = NULL;
  945. struct p54_common *priv = dev->priv;
  946. struct p54_hdr *hdr;
  947. struct p54_tx_data *txhdr;
  948. size_t padding, len, tim_len = 0;
  949. int i, j, ridx;
  950. u16 hdr_flags = 0, aid = 0;
  951. u8 rate, queue;
  952. u8 cts_rate = 0x20;
  953. u8 rc_flags;
  954. u8 calculated_tries[4];
  955. u8 nrates = 0, nremaining = 8;
  956. queue = skb_get_queue_mapping(skb);
  957. if (p54_tx_fill(dev, skb, info, &queue, &tim_len, &hdr_flags, &aid)) {
  958. current_queue = &priv->tx_stats[queue];
  959. if (unlikely(current_queue->len > current_queue->limit))
  960. return NETDEV_TX_BUSY;
  961. current_queue->len++;
  962. current_queue->count++;
  963. if (current_queue->len == current_queue->limit)
  964. ieee80211_stop_queue(dev, skb_get_queue_mapping(skb));
  965. }
  966. padding = (unsigned long)(skb->data - (sizeof(*hdr) + sizeof(*txhdr))) & 3;
  967. len = skb->len;
  968. if (info->flags & IEEE80211_TX_CTL_CLEAR_PS_FILT) {
  969. if (info->control.sta)
  970. if (p54_sta_unlock(dev, info->control.sta->addr)) {
  971. if (current_queue) {
  972. current_queue->len--;
  973. current_queue->count--;
  974. }
  975. return NETDEV_TX_BUSY;
  976. }
  977. }
  978. txhdr = (struct p54_tx_data *) skb_push(skb, sizeof(*txhdr) + padding);
  979. hdr = (struct p54_hdr *) skb_push(skb, sizeof(*hdr));
  980. if (padding)
  981. hdr_flags |= P54_HDR_FLAG_DATA_ALIGN;
  982. hdr->len = cpu_to_le16(len);
  983. hdr->type = cpu_to_le16(aid);
  984. hdr->rts_tries = info->control.rates[0].count;
  985. /*
  986. * we register the rates in perfect order, and
  987. * RTS/CTS won't happen on 5 GHz
  988. */
  989. cts_rate = info->control.rts_cts_rate_idx;
  990. memset(&txhdr->rateset, 0, sizeof(txhdr->rateset));
  991. /* see how many rates got used */
  992. for (i = 0; i < 4; i++) {
  993. if (info->control.rates[i].idx < 0)
  994. break;
  995. nrates++;
  996. }
  997. /* limit tries to 8/nrates per rate */
  998. for (i = 0; i < nrates; i++) {
  999. /*
  1000. * The magic expression here is equivalent to 8/nrates for
  1001. * all values that matter, but avoids division and jumps.
  1002. * Note that nrates can only take the values 1 through 4.
  1003. */
  1004. calculated_tries[i] = min_t(int, ((15 >> nrates) | 1) + 1,
  1005. info->control.rates[i].count);
  1006. nremaining -= calculated_tries[i];
  1007. }
  1008. /* if there are tries left, distribute from back to front */
  1009. for (i = nrates - 1; nremaining > 0 && i >= 0; i--) {
  1010. int tmp = info->control.rates[i].count - calculated_tries[i];
  1011. if (tmp <= 0)
  1012. continue;
  1013. /* RC requested more tries at this rate */
  1014. tmp = min_t(int, tmp, nremaining);
  1015. calculated_tries[i] += tmp;
  1016. nremaining -= tmp;
  1017. }
  1018. ridx = 0;
  1019. for (i = 0; i < nrates && ridx < 8; i++) {
  1020. /* we register the rates in perfect order */
  1021. rate = info->control.rates[i].idx;
  1022. if (info->band == IEEE80211_BAND_5GHZ)
  1023. rate += 4;
  1024. /* store the count we actually calculated for TX status */
  1025. info->control.rates[i].count = calculated_tries[i];
  1026. rc_flags = info->control.rates[i].flags;
  1027. if (rc_flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE) {
  1028. rate |= 0x10;
  1029. cts_rate |= 0x10;
  1030. }
  1031. if (rc_flags & IEEE80211_TX_RC_USE_RTS_CTS)
  1032. rate |= 0x40;
  1033. else if (rc_flags & IEEE80211_TX_RC_USE_CTS_PROTECT)
  1034. rate |= 0x20;
  1035. for (j = 0; j < calculated_tries[i] && ridx < 8; j++) {
  1036. txhdr->rateset[ridx] = rate;
  1037. ridx++;
  1038. }
  1039. }
  1040. if (info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ)
  1041. hdr_flags |= P54_HDR_FLAG_DATA_OUT_SEQNR;
  1042. /* TODO: enable bursting */
  1043. hdr->flags = cpu_to_le16(hdr_flags);
  1044. hdr->tries = ridx;
  1045. txhdr->crypt_offset = 0;
  1046. txhdr->rts_rate_idx = 0;
  1047. txhdr->key_type = 0;
  1048. txhdr->key_len = 0;
  1049. txhdr->hw_queue = queue;
  1050. txhdr->backlog = 32;
  1051. memset(txhdr->durations, 0, sizeof(txhdr->durations));
  1052. txhdr->tx_antenna = (info->antenna_sel_tx == 0) ?
  1053. 2 : info->antenna_sel_tx - 1;
  1054. txhdr->output_power = priv->output_power;
  1055. txhdr->cts_rate = cts_rate;
  1056. if (padding)
  1057. txhdr->align[0] = padding;
  1058. /* modifies skb->cb and with it info, so must be last! */
  1059. if (unlikely(p54_assign_address(dev, skb, hdr, skb->len + tim_len))) {
  1060. skb_pull(skb, sizeof(*hdr) + sizeof(*txhdr) + padding);
  1061. if (current_queue) {
  1062. current_queue->len--;
  1063. current_queue->count--;
  1064. }
  1065. return NETDEV_TX_BUSY;
  1066. }
  1067. priv->tx(dev, skb, 0);
  1068. return 0;
  1069. }
  1070. static int p54_setup_mac(struct ieee80211_hw *dev, u16 mode, const u8 *bssid)
  1071. {
  1072. struct p54_common *priv = dev->priv;
  1073. struct sk_buff *skb;
  1074. struct p54_setup_mac *setup;
  1075. skb = p54_alloc_skb(dev, P54_HDR_FLAG_CONTROL_OPSET, sizeof(*setup) +
  1076. sizeof(struct p54_hdr), P54_CONTROL_TYPE_SETUP,
  1077. GFP_ATOMIC);
  1078. if (!skb)
  1079. return -ENOMEM;
  1080. setup = (struct p54_setup_mac *) skb_put(skb, sizeof(*setup));
  1081. priv->mac_mode = mode;
  1082. setup->mac_mode = cpu_to_le16(mode);
  1083. memcpy(setup->mac_addr, priv->mac_addr, ETH_ALEN);
  1084. if (!bssid)
  1085. memset(setup->bssid, ~0, ETH_ALEN);
  1086. else
  1087. memcpy(setup->bssid, bssid, ETH_ALEN);
  1088. setup->rx_antenna = priv->rx_antenna;
  1089. setup->rx_align = 0;
  1090. if (priv->fw_var < 0x500) {
  1091. setup->v1.basic_rate_mask = cpu_to_le32(0x15f);
  1092. memset(setup->v1.rts_rates, 0, 8);
  1093. setup->v1.rx_addr = cpu_to_le32(priv->rx_end);
  1094. setup->v1.max_rx = cpu_to_le16(priv->rx_mtu);
  1095. setup->v1.rxhw = cpu_to_le16(priv->rxhw);
  1096. setup->v1.wakeup_timer = cpu_to_le16(500);
  1097. setup->v1.unalloc0 = cpu_to_le16(0);
  1098. } else {
  1099. setup->v2.rx_addr = cpu_to_le32(priv->rx_end);
  1100. setup->v2.max_rx = cpu_to_le16(priv->rx_mtu);
  1101. setup->v2.rxhw = cpu_to_le16(priv->rxhw);
  1102. setup->v2.timer = cpu_to_le16(1000);
  1103. setup->v2.truncate = cpu_to_le16(48896);
  1104. setup->v2.basic_rate_mask = cpu_to_le32(0x15f);
  1105. setup->v2.sbss_offset = 0;
  1106. setup->v2.mcast_window = 0;
  1107. setup->v2.rx_rssi_threshold = 0;
  1108. setup->v2.rx_ed_threshold = 0;
  1109. setup->v2.ref_clock = cpu_to_le32(644245094);
  1110. setup->v2.lpf_bandwidth = cpu_to_le16(65535);
  1111. setup->v2.osc_start_delay = cpu_to_le16(65535);
  1112. }
  1113. priv->tx(dev, skb, 1);
  1114. return 0;
  1115. }
  1116. static int p54_set_freq(struct ieee80211_hw *dev, u16 frequency)
  1117. {
  1118. struct p54_common *priv = dev->priv;
  1119. struct sk_buff *skb;
  1120. struct p54_scan *chan;
  1121. unsigned int i;
  1122. void *entry;
  1123. __le16 freq = cpu_to_le16(frequency);
  1124. skb = p54_alloc_skb(dev, P54_HDR_FLAG_CONTROL_OPSET, sizeof(*chan) +
  1125. sizeof(struct p54_hdr), P54_CONTROL_TYPE_SCAN,
  1126. GFP_ATOMIC);
  1127. if (!skb)
  1128. return -ENOMEM;
  1129. chan = (struct p54_scan *) skb_put(skb, sizeof(*chan));
  1130. memset(chan->padding1, 0, sizeof(chan->padding1));
  1131. chan->mode = cpu_to_le16(P54_SCAN_EXIT);
  1132. chan->dwell = cpu_to_le16(0x0);
  1133. for (i = 0; i < priv->iq_autocal_len; i++) {
  1134. if (priv->iq_autocal[i].freq != freq)
  1135. continue;
  1136. memcpy(&chan->iq_autocal, &priv->iq_autocal[i],
  1137. sizeof(*priv->iq_autocal));
  1138. break;
  1139. }
  1140. if (i == priv->iq_autocal_len)
  1141. goto err;
  1142. for (i = 0; i < priv->output_limit_len; i++) {
  1143. if (priv->output_limit[i].freq != freq)
  1144. continue;
  1145. chan->val_barker = 0x38;
  1146. chan->val_bpsk = chan->dup_bpsk =
  1147. priv->output_limit[i].val_bpsk;
  1148. chan->val_qpsk = chan->dup_qpsk =
  1149. priv->output_limit[i].val_qpsk;
  1150. chan->val_16qam = chan->dup_16qam =
  1151. priv->output_limit[i].val_16qam;
  1152. chan->val_64qam = chan->dup_64qam =
  1153. priv->output_limit[i].val_64qam;
  1154. break;
  1155. }
  1156. if (i == priv->output_limit_len)
  1157. goto err;
  1158. entry = priv->curve_data->data;
  1159. for (i = 0; i < priv->curve_data->channels; i++) {
  1160. if (*((__le16 *)entry) != freq) {
  1161. entry += sizeof(__le16);
  1162. entry += sizeof(struct p54_pa_curve_data_sample) *
  1163. priv->curve_data->points_per_channel;
  1164. continue;
  1165. }
  1166. entry += sizeof(__le16);
  1167. chan->pa_points_per_curve = 8;
  1168. memset(chan->curve_data, 0, sizeof(*chan->curve_data));
  1169. memcpy(chan->curve_data, entry,
  1170. sizeof(struct p54_pa_curve_data_sample) *
  1171. min((u8)8, priv->curve_data->points_per_channel));
  1172. break;
  1173. }
  1174. if (priv->fw_var < 0x500) {
  1175. chan->v1.rssical_mul = cpu_to_le16(130);
  1176. chan->v1.rssical_add = cpu_to_le16(0xfe70);
  1177. } else {
  1178. chan->v2.rssical_mul = cpu_to_le16(130);
  1179. chan->v2.rssical_add = cpu_to_le16(0xfe70);
  1180. chan->v2.basic_rate_mask = cpu_to_le32(0x15f);
  1181. memset(chan->v2.rts_rates, 0, 8);
  1182. }
  1183. priv->tx(dev, skb, 1);
  1184. return 0;
  1185. err:
  1186. printk(KERN_ERR "%s: frequency change failed\n", wiphy_name(dev->wiphy));
  1187. kfree_skb(skb);
  1188. return -EINVAL;
  1189. }
  1190. static int p54_set_leds(struct ieee80211_hw *dev, int mode, int link, int act)
  1191. {
  1192. struct p54_common *priv = dev->priv;
  1193. struct sk_buff *skb;
  1194. struct p54_led *led;
  1195. skb = p54_alloc_skb(dev, P54_HDR_FLAG_CONTROL_OPSET, sizeof(*led) +
  1196. sizeof(struct p54_hdr), P54_CONTROL_TYPE_LED,
  1197. GFP_ATOMIC);
  1198. if (!skb)
  1199. return -ENOMEM;
  1200. led = (struct p54_led *)skb_put(skb, sizeof(*led));
  1201. led->mode = cpu_to_le16(mode);
  1202. led->led_permanent = cpu_to_le16(link);
  1203. led->led_temporary = cpu_to_le16(act);
  1204. led->duration = cpu_to_le16(1000);
  1205. priv->tx(dev, skb, 1);
  1206. return 0;
  1207. }
  1208. #define P54_SET_QUEUE(queue, ai_fs, cw_min, cw_max, _txop) \
  1209. do { \
  1210. queue.aifs = cpu_to_le16(ai_fs); \
  1211. queue.cwmin = cpu_to_le16(cw_min); \
  1212. queue.cwmax = cpu_to_le16(cw_max); \
  1213. queue.txop = cpu_to_le16(_txop); \
  1214. } while(0)
  1215. static int p54_set_edcf(struct ieee80211_hw *dev)
  1216. {
  1217. struct p54_common *priv = dev->priv;
  1218. struct sk_buff *skb;
  1219. struct p54_edcf *edcf;
  1220. skb = p54_alloc_skb(dev, P54_HDR_FLAG_CONTROL_OPSET, sizeof(*edcf) +
  1221. sizeof(struct p54_hdr), P54_CONTROL_TYPE_DCFINIT,
  1222. GFP_ATOMIC);
  1223. if (!skb)
  1224. return -ENOMEM;
  1225. edcf = (struct p54_edcf *)skb_put(skb, sizeof(*edcf));
  1226. if (priv->use_short_slot) {
  1227. edcf->slottime = 9;
  1228. edcf->sifs = 0x10;
  1229. edcf->eofpad = 0x00;
  1230. } else {
  1231. edcf->slottime = 20;
  1232. edcf->sifs = 0x0a;
  1233. edcf->eofpad = 0x06;
  1234. }
  1235. /* (see prism54/isl_oid.h for further details) */
  1236. edcf->frameburst = cpu_to_le16(0);
  1237. edcf->round_trip_delay = cpu_to_le16(0);
  1238. edcf->flags = 0;
  1239. memset(edcf->mapping, 0, sizeof(edcf->mapping));
  1240. memcpy(edcf->queue, priv->qos_params, sizeof(edcf->queue));
  1241. priv->tx(dev, skb, 1);
  1242. return 0;
  1243. }
  1244. static int p54_init_stats(struct ieee80211_hw *dev)
  1245. {
  1246. struct p54_common *priv = dev->priv;
  1247. priv->cached_stats = p54_alloc_skb(dev, P54_HDR_FLAG_CONTROL,
  1248. sizeof(struct p54_hdr) + sizeof(struct p54_statistics),
  1249. P54_CONTROL_TYPE_STAT_READBACK, GFP_KERNEL);
  1250. if (!priv->cached_stats)
  1251. return -ENOMEM;
  1252. mod_timer(&priv->stats_timer, jiffies + HZ);
  1253. return 0;
  1254. }
  1255. static int p54_beacon_tim(struct sk_buff *skb)
  1256. {
  1257. /*
  1258. * the good excuse for this mess is ... the firmware.
  1259. * The dummy TIM MUST be at the end of the beacon frame,
  1260. * because it'll be overwritten!
  1261. */
  1262. struct ieee80211_mgmt *mgmt = (void *)skb->data;
  1263. u8 *pos, *end;
  1264. if (skb->len <= sizeof(mgmt)) {
  1265. printk(KERN_ERR "p54: beacon is too short!\n");
  1266. return -EINVAL;
  1267. }
  1268. pos = (u8 *)mgmt->u.beacon.variable;
  1269. end = skb->data + skb->len;
  1270. while (pos < end) {
  1271. if (pos + 2 + pos[1] > end) {
  1272. printk(KERN_ERR "p54: parsing beacon failed\n");
  1273. return -EINVAL;
  1274. }
  1275. if (pos[0] == WLAN_EID_TIM) {
  1276. u8 dtim_len = pos[1];
  1277. u8 dtim_period = pos[3];
  1278. u8 *next = pos + 2 + dtim_len;
  1279. if (dtim_len < 3) {
  1280. printk(KERN_ERR "p54: invalid dtim len!\n");
  1281. return -EINVAL;
  1282. }
  1283. memmove(pos, next, end - next);
  1284. if (dtim_len > 3)
  1285. skb_trim(skb, skb->len - (dtim_len - 3));
  1286. pos = end - (dtim_len + 2);
  1287. /* add the dummy at the end */
  1288. pos[0] = WLAN_EID_TIM;
  1289. pos[1] = 3;
  1290. pos[2] = 0;
  1291. pos[3] = dtim_period;
  1292. pos[4] = 0;
  1293. return 0;
  1294. }
  1295. pos += 2 + pos[1];
  1296. }
  1297. return 0;
  1298. }
  1299. static int p54_beacon_update(struct ieee80211_hw *dev,
  1300. struct ieee80211_vif *vif)
  1301. {
  1302. struct p54_common *priv = dev->priv;
  1303. struct sk_buff *beacon;
  1304. int ret;
  1305. if (priv->cached_beacon) {
  1306. p54_tx_cancel(dev, priv->cached_beacon);
  1307. /* wait for the last beacon the be freed */
  1308. msleep(10);
  1309. }
  1310. beacon = ieee80211_beacon_get(dev, vif);
  1311. if (!beacon)
  1312. return -ENOMEM;
  1313. ret = p54_beacon_tim(beacon);
  1314. if (ret)
  1315. return ret;
  1316. ret = p54_tx(dev, beacon);
  1317. if (ret)
  1318. return ret;
  1319. priv->cached_beacon = beacon;
  1320. priv->tsf_high32 = 0;
  1321. priv->tsf_low32 = 0;
  1322. return 0;
  1323. }
  1324. static int p54_start(struct ieee80211_hw *dev)
  1325. {
  1326. struct p54_common *priv = dev->priv;
  1327. int err;
  1328. mutex_lock(&priv->conf_mutex);
  1329. err = priv->open(dev);
  1330. if (!err)
  1331. priv->mode = NL80211_IFTYPE_MONITOR;
  1332. P54_SET_QUEUE(priv->qos_params[0], 0x0002, 0x0003, 0x0007, 47);
  1333. P54_SET_QUEUE(priv->qos_params[1], 0x0002, 0x0007, 0x000f, 94);
  1334. P54_SET_QUEUE(priv->qos_params[2], 0x0003, 0x000f, 0x03ff, 0);
  1335. P54_SET_QUEUE(priv->qos_params[3], 0x0007, 0x000f, 0x03ff, 0);
  1336. err = p54_set_edcf(dev);
  1337. if (!err)
  1338. err = p54_init_stats(dev);
  1339. mutex_unlock(&priv->conf_mutex);
  1340. return err;
  1341. }
  1342. static void p54_stop(struct ieee80211_hw *dev)
  1343. {
  1344. struct p54_common *priv = dev->priv;
  1345. struct sk_buff *skb;
  1346. mutex_lock(&priv->conf_mutex);
  1347. del_timer(&priv->stats_timer);
  1348. p54_free_skb(dev, priv->cached_stats);
  1349. priv->cached_stats = NULL;
  1350. if (priv->cached_beacon)
  1351. p54_tx_cancel(dev, priv->cached_beacon);
  1352. while ((skb = skb_dequeue(&priv->tx_queue)))
  1353. kfree_skb(skb);
  1354. kfree(priv->cached_beacon);
  1355. priv->cached_beacon = NULL;
  1356. priv->stop(dev);
  1357. priv->tsf_high32 = priv->tsf_low32 = 0;
  1358. priv->mode = NL80211_IFTYPE_UNSPECIFIED;
  1359. mutex_unlock(&priv->conf_mutex);
  1360. }
  1361. static int p54_add_interface(struct ieee80211_hw *dev,
  1362. struct ieee80211_if_init_conf *conf)
  1363. {
  1364. struct p54_common *priv = dev->priv;
  1365. mutex_lock(&priv->conf_mutex);
  1366. if (priv->mode != NL80211_IFTYPE_MONITOR) {
  1367. mutex_unlock(&priv->conf_mutex);
  1368. return -EOPNOTSUPP;
  1369. }
  1370. switch (conf->type) {
  1371. case NL80211_IFTYPE_STATION:
  1372. case NL80211_IFTYPE_ADHOC:
  1373. case NL80211_IFTYPE_AP:
  1374. priv->mode = conf->type;
  1375. break;
  1376. default:
  1377. mutex_unlock(&priv->conf_mutex);
  1378. return -EOPNOTSUPP;
  1379. }
  1380. memcpy(priv->mac_addr, conf->mac_addr, ETH_ALEN);
  1381. p54_setup_mac(dev, P54_FILTER_TYPE_NONE, NULL);
  1382. switch (conf->type) {
  1383. case NL80211_IFTYPE_STATION:
  1384. p54_setup_mac(dev, P54_FILTER_TYPE_STATION, NULL);
  1385. break;
  1386. case NL80211_IFTYPE_AP:
  1387. p54_setup_mac(dev, P54_FILTER_TYPE_AP, priv->mac_addr);
  1388. break;
  1389. case NL80211_IFTYPE_ADHOC:
  1390. p54_setup_mac(dev, P54_FILTER_TYPE_IBSS, NULL);
  1391. break;
  1392. default:
  1393. BUG(); /* impossible */
  1394. break;
  1395. }
  1396. p54_set_leds(dev, 1, 0, 0);
  1397. mutex_unlock(&priv->conf_mutex);
  1398. return 0;
  1399. }
  1400. static void p54_remove_interface(struct ieee80211_hw *dev,
  1401. struct ieee80211_if_init_conf *conf)
  1402. {
  1403. struct p54_common *priv = dev->priv;
  1404. mutex_lock(&priv->conf_mutex);
  1405. if (priv->cached_beacon)
  1406. p54_tx_cancel(dev, priv->cached_beacon);
  1407. p54_setup_mac(dev, P54_FILTER_TYPE_NONE, NULL);
  1408. priv->mode = NL80211_IFTYPE_MONITOR;
  1409. memset(priv->mac_addr, 0, ETH_ALEN);
  1410. mutex_unlock(&priv->conf_mutex);
  1411. }
  1412. static int p54_config(struct ieee80211_hw *dev, u32 changed)
  1413. {
  1414. int ret;
  1415. struct p54_common *priv = dev->priv;
  1416. struct ieee80211_conf *conf = &dev->conf;
  1417. mutex_lock(&priv->conf_mutex);
  1418. priv->rx_antenna = 2; /* automatic */
  1419. priv->output_power = conf->power_level << 2;
  1420. ret = p54_set_freq(dev, conf->channel->center_freq);
  1421. if (!ret)
  1422. ret = p54_set_edcf(dev);
  1423. mutex_unlock(&priv->conf_mutex);
  1424. return ret;
  1425. }
  1426. static int p54_config_interface(struct ieee80211_hw *dev,
  1427. struct ieee80211_vif *vif,
  1428. struct ieee80211_if_conf *conf)
  1429. {
  1430. struct p54_common *priv = dev->priv;
  1431. int ret = 0;
  1432. mutex_lock(&priv->conf_mutex);
  1433. switch (priv->mode) {
  1434. case NL80211_IFTYPE_STATION:
  1435. ret = p54_setup_mac(dev, P54_FILTER_TYPE_STATION, conf->bssid);
  1436. if (ret)
  1437. goto out;
  1438. ret = p54_set_leds(dev, 1,
  1439. !is_multicast_ether_addr(conf->bssid), 0);
  1440. if (ret)
  1441. goto out;
  1442. memcpy(priv->bssid, conf->bssid, ETH_ALEN);
  1443. break;
  1444. case NL80211_IFTYPE_AP:
  1445. case NL80211_IFTYPE_ADHOC:
  1446. memcpy(priv->bssid, conf->bssid, ETH_ALEN);
  1447. ret = p54_set_freq(dev, dev->conf.channel->center_freq);
  1448. if (ret)
  1449. goto out;
  1450. ret = p54_setup_mac(dev, priv->mac_mode, priv->bssid);
  1451. if (ret)
  1452. goto out;
  1453. if (conf->changed & IEEE80211_IFCC_BEACON) {
  1454. ret = p54_beacon_update(dev, vif);
  1455. if (ret)
  1456. goto out;
  1457. ret = p54_set_edcf(dev);
  1458. if (ret)
  1459. goto out;
  1460. }
  1461. }
  1462. out:
  1463. mutex_unlock(&priv->conf_mutex);
  1464. return ret;
  1465. }
  1466. static void p54_configure_filter(struct ieee80211_hw *dev,
  1467. unsigned int changed_flags,
  1468. unsigned int *total_flags,
  1469. int mc_count, struct dev_mc_list *mclist)
  1470. {
  1471. struct p54_common *priv = dev->priv;
  1472. *total_flags &= FIF_BCN_PRBRESP_PROMISC |
  1473. FIF_PROMISC_IN_BSS |
  1474. FIF_FCSFAIL;
  1475. priv->filter_flags = *total_flags;
  1476. if (changed_flags & FIF_BCN_PRBRESP_PROMISC) {
  1477. if (*total_flags & FIF_BCN_PRBRESP_PROMISC)
  1478. p54_setup_mac(dev, priv->mac_mode, NULL);
  1479. else
  1480. p54_setup_mac(dev, priv->mac_mode, priv->bssid);
  1481. }
  1482. if (changed_flags & FIF_PROMISC_IN_BSS) {
  1483. if (*total_flags & FIF_PROMISC_IN_BSS)
  1484. p54_setup_mac(dev, priv->mac_mode | 0x8, NULL);
  1485. else
  1486. p54_setup_mac(dev, priv->mac_mode & ~0x8, priv->bssid);
  1487. }
  1488. }
  1489. static int p54_conf_tx(struct ieee80211_hw *dev, u16 queue,
  1490. const struct ieee80211_tx_queue_params *params)
  1491. {
  1492. struct p54_common *priv = dev->priv;
  1493. int ret;
  1494. mutex_lock(&priv->conf_mutex);
  1495. if ((params) && !(queue > 4)) {
  1496. P54_SET_QUEUE(priv->qos_params[queue], params->aifs,
  1497. params->cw_min, params->cw_max, params->txop);
  1498. } else
  1499. ret = -EINVAL;
  1500. if (!ret)
  1501. ret = p54_set_edcf(dev);
  1502. mutex_unlock(&priv->conf_mutex);
  1503. return ret;
  1504. }
  1505. static int p54_init_xbow_synth(struct ieee80211_hw *dev)
  1506. {
  1507. struct p54_common *priv = dev->priv;
  1508. struct sk_buff *skb;
  1509. struct p54_xbow_synth *xbow;
  1510. skb = p54_alloc_skb(dev, P54_HDR_FLAG_CONTROL_OPSET, sizeof(*xbow) +
  1511. sizeof(struct p54_hdr),
  1512. P54_CONTROL_TYPE_XBOW_SYNTH_CFG,
  1513. GFP_KERNEL);
  1514. if (!skb)
  1515. return -ENOMEM;
  1516. xbow = (struct p54_xbow_synth *)skb_put(skb, sizeof(*xbow));
  1517. xbow->magic1 = cpu_to_le16(0x1);
  1518. xbow->magic2 = cpu_to_le16(0x2);
  1519. xbow->freq = cpu_to_le16(5390);
  1520. memset(xbow->padding, 0, sizeof(xbow->padding));
  1521. priv->tx(dev, skb, 1);
  1522. return 0;
  1523. }
  1524. static void p54_statistics_timer(unsigned long data)
  1525. {
  1526. struct ieee80211_hw *dev = (struct ieee80211_hw *) data;
  1527. struct p54_common *priv = dev->priv;
  1528. BUG_ON(!priv->cached_stats);
  1529. priv->tx(dev, priv->cached_stats, 0);
  1530. }
  1531. static int p54_get_stats(struct ieee80211_hw *dev,
  1532. struct ieee80211_low_level_stats *stats)
  1533. {
  1534. struct p54_common *priv = dev->priv;
  1535. del_timer(&priv->stats_timer);
  1536. p54_statistics_timer((unsigned long)dev);
  1537. if (!wait_for_completion_interruptible_timeout(&priv->stats_comp, HZ)) {
  1538. printk(KERN_ERR "%s: device does not respond!\n",
  1539. wiphy_name(dev->wiphy));
  1540. return -EBUSY;
  1541. }
  1542. memcpy(stats, &priv->stats, sizeof(*stats));
  1543. return 0;
  1544. }
  1545. static int p54_get_tx_stats(struct ieee80211_hw *dev,
  1546. struct ieee80211_tx_queue_stats *stats)
  1547. {
  1548. struct p54_common *priv = dev->priv;
  1549. memcpy(stats, &priv->tx_stats[4], sizeof(stats[0]) * dev->queues);
  1550. return 0;
  1551. }
  1552. static void p54_bss_info_changed(struct ieee80211_hw *dev,
  1553. struct ieee80211_vif *vif,
  1554. struct ieee80211_bss_conf *info,
  1555. u32 changed)
  1556. {
  1557. struct p54_common *priv = dev->priv;
  1558. if (changed & BSS_CHANGED_ERP_SLOT) {
  1559. priv->use_short_slot = info->use_short_slot;
  1560. p54_set_edcf(dev);
  1561. }
  1562. }
  1563. static const struct ieee80211_ops p54_ops = {
  1564. .tx = p54_tx,
  1565. .start = p54_start,
  1566. .stop = p54_stop,
  1567. .add_interface = p54_add_interface,
  1568. .remove_interface = p54_remove_interface,
  1569. .set_tim = p54_set_tim,
  1570. .config = p54_config,
  1571. .config_interface = p54_config_interface,
  1572. .bss_info_changed = p54_bss_info_changed,
  1573. .configure_filter = p54_configure_filter,
  1574. .conf_tx = p54_conf_tx,
  1575. .get_stats = p54_get_stats,
  1576. .get_tx_stats = p54_get_tx_stats
  1577. };
  1578. struct ieee80211_hw *p54_init_common(size_t priv_data_len)
  1579. {
  1580. struct ieee80211_hw *dev;
  1581. struct p54_common *priv;
  1582. dev = ieee80211_alloc_hw(priv_data_len, &p54_ops);
  1583. if (!dev)
  1584. return NULL;
  1585. priv = dev->priv;
  1586. priv->mode = NL80211_IFTYPE_UNSPECIFIED;
  1587. skb_queue_head_init(&priv->tx_queue);
  1588. dev->flags = IEEE80211_HW_RX_INCLUDES_FCS |
  1589. IEEE80211_HW_SIGNAL_DBM |
  1590. IEEE80211_HW_NOISE_DBM;
  1591. dev->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION |
  1592. NL80211_IFTYPE_ADHOC |
  1593. NL80211_IFTYPE_AP);
  1594. dev->channel_change_time = 1000; /* TODO: find actual value */
  1595. priv->tx_stats[0].limit = 1; /* Beacon queue */
  1596. priv->tx_stats[1].limit = 1; /* Probe queue for HW scan */
  1597. priv->tx_stats[2].limit = 3; /* queue for MLMEs */
  1598. priv->tx_stats[3].limit = 3; /* Broadcast / MC queue */
  1599. priv->tx_stats[4].limit = 5; /* Data */
  1600. dev->queues = 1;
  1601. priv->noise = -94;
  1602. /*
  1603. * We support at most 8 tries no matter which rate they're at,
  1604. * we cannot support max_rates * max_rate_tries as we set it
  1605. * here, but setting it correctly to 4/2 or so would limit us
  1606. * artificially if the RC algorithm wants just two rates, so
  1607. * let's say 4/7, we'll redistribute it at TX time, see the
  1608. * comments there.
  1609. */
  1610. dev->max_rates = 4;
  1611. dev->max_rate_tries = 7;
  1612. dev->extra_tx_headroom = sizeof(struct p54_hdr) + 4 +
  1613. sizeof(struct p54_tx_data);
  1614. mutex_init(&priv->conf_mutex);
  1615. init_completion(&priv->eeprom_comp);
  1616. init_completion(&priv->stats_comp);
  1617. setup_timer(&priv->stats_timer, p54_statistics_timer,
  1618. (unsigned long)dev);
  1619. return dev;
  1620. }
  1621. EXPORT_SYMBOL_GPL(p54_init_common);
  1622. void p54_free_common(struct ieee80211_hw *dev)
  1623. {
  1624. struct p54_common *priv = dev->priv;
  1625. del_timer(&priv->stats_timer);
  1626. kfree_skb(priv->cached_stats);
  1627. kfree(priv->iq_autocal);
  1628. kfree(priv->output_limit);
  1629. kfree(priv->curve_data);
  1630. }
  1631. EXPORT_SYMBOL_GPL(p54_free_common);
  1632. static int __init p54_init(void)
  1633. {
  1634. return 0;
  1635. }
  1636. static void __exit p54_exit(void)
  1637. {
  1638. }
  1639. module_init(p54_init);
  1640. module_exit(p54_exit);