p54common.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430
  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. *
  7. * Based on the islsm (softmac prism54) driver, which is:
  8. * Copyright 2004-2006 Jean-Baptiste Note <jbnote@gmail.com>, et al.
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  13. */
  14. #include <linux/init.h>
  15. #include <linux/firmware.h>
  16. #include <linux/etherdevice.h>
  17. #include <net/mac80211.h>
  18. #include "p54.h"
  19. #include "p54common.h"
  20. MODULE_AUTHOR("Michael Wu <flamingice@sourmilk.net>");
  21. MODULE_DESCRIPTION("Softmac Prism54 common code");
  22. MODULE_LICENSE("GPL");
  23. MODULE_ALIAS("prism54common");
  24. static struct ieee80211_rate p54_bgrates[] = {
  25. { .bitrate = 10, .hw_value = 0, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  26. { .bitrate = 20, .hw_value = 1, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  27. { .bitrate = 55, .hw_value = 2, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  28. { .bitrate = 110, .hw_value = 3, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  29. { .bitrate = 60, .hw_value = 4, },
  30. { .bitrate = 90, .hw_value = 5, },
  31. { .bitrate = 120, .hw_value = 6, },
  32. { .bitrate = 180, .hw_value = 7, },
  33. { .bitrate = 240, .hw_value = 8, },
  34. { .bitrate = 360, .hw_value = 9, },
  35. { .bitrate = 480, .hw_value = 10, },
  36. { .bitrate = 540, .hw_value = 11, },
  37. };
  38. static struct ieee80211_channel p54_bgchannels[] = {
  39. { .center_freq = 2412, .hw_value = 1, },
  40. { .center_freq = 2417, .hw_value = 2, },
  41. { .center_freq = 2422, .hw_value = 3, },
  42. { .center_freq = 2427, .hw_value = 4, },
  43. { .center_freq = 2432, .hw_value = 5, },
  44. { .center_freq = 2437, .hw_value = 6, },
  45. { .center_freq = 2442, .hw_value = 7, },
  46. { .center_freq = 2447, .hw_value = 8, },
  47. { .center_freq = 2452, .hw_value = 9, },
  48. { .center_freq = 2457, .hw_value = 10, },
  49. { .center_freq = 2462, .hw_value = 11, },
  50. { .center_freq = 2467, .hw_value = 12, },
  51. { .center_freq = 2472, .hw_value = 13, },
  52. { .center_freq = 2484, .hw_value = 14, },
  53. };
  54. static struct ieee80211_supported_band band_2GHz = {
  55. .channels = p54_bgchannels,
  56. .n_channels = ARRAY_SIZE(p54_bgchannels),
  57. .bitrates = p54_bgrates,
  58. .n_bitrates = ARRAY_SIZE(p54_bgrates),
  59. };
  60. static struct ieee80211_rate p54_arates[] = {
  61. { .bitrate = 60, .hw_value = 4, },
  62. { .bitrate = 90, .hw_value = 5, },
  63. { .bitrate = 120, .hw_value = 6, },
  64. { .bitrate = 180, .hw_value = 7, },
  65. { .bitrate = 240, .hw_value = 8, },
  66. { .bitrate = 360, .hw_value = 9, },
  67. { .bitrate = 480, .hw_value = 10, },
  68. { .bitrate = 540, .hw_value = 11, },
  69. };
  70. static struct ieee80211_channel p54_achannels[] = {
  71. { .center_freq = 4920 },
  72. { .center_freq = 4940 },
  73. { .center_freq = 4960 },
  74. { .center_freq = 4980 },
  75. { .center_freq = 5040 },
  76. { .center_freq = 5060 },
  77. { .center_freq = 5080 },
  78. { .center_freq = 5170 },
  79. { .center_freq = 5180 },
  80. { .center_freq = 5190 },
  81. { .center_freq = 5200 },
  82. { .center_freq = 5210 },
  83. { .center_freq = 5220 },
  84. { .center_freq = 5230 },
  85. { .center_freq = 5240 },
  86. { .center_freq = 5260 },
  87. { .center_freq = 5280 },
  88. { .center_freq = 5300 },
  89. { .center_freq = 5320 },
  90. { .center_freq = 5500 },
  91. { .center_freq = 5520 },
  92. { .center_freq = 5540 },
  93. { .center_freq = 5560 },
  94. { .center_freq = 5580 },
  95. { .center_freq = 5600 },
  96. { .center_freq = 5620 },
  97. { .center_freq = 5640 },
  98. { .center_freq = 5660 },
  99. { .center_freq = 5680 },
  100. { .center_freq = 5700 },
  101. { .center_freq = 5745 },
  102. { .center_freq = 5765 },
  103. { .center_freq = 5785 },
  104. { .center_freq = 5805 },
  105. { .center_freq = 5825 },
  106. };
  107. static struct ieee80211_supported_band band_5GHz = {
  108. .channels = p54_achannels,
  109. .n_channels = ARRAY_SIZE(p54_achannels),
  110. .bitrates = p54_arates,
  111. .n_bitrates = ARRAY_SIZE(p54_arates),
  112. };
  113. int p54_parse_firmware(struct ieee80211_hw *dev, const struct firmware *fw)
  114. {
  115. struct p54_common *priv = dev->priv;
  116. struct bootrec_exp_if *exp_if;
  117. struct bootrec *bootrec;
  118. u32 *data = (u32 *)fw->data;
  119. u32 *end_data = (u32 *)fw->data + (fw->size >> 2);
  120. u8 *fw_version = NULL;
  121. size_t len;
  122. int i;
  123. if (priv->rx_start)
  124. return 0;
  125. while (data < end_data && *data)
  126. data++;
  127. while (data < end_data && !*data)
  128. data++;
  129. bootrec = (struct bootrec *) data;
  130. while (bootrec->data <= end_data &&
  131. (bootrec->data + (len = le32_to_cpu(bootrec->len))) <= end_data) {
  132. u32 code = le32_to_cpu(bootrec->code);
  133. switch (code) {
  134. case BR_CODE_COMPONENT_ID:
  135. priv->fw_interface = be32_to_cpup((__be32 *)
  136. bootrec->data);
  137. switch (priv->fw_interface) {
  138. case FW_FMAC:
  139. printk(KERN_INFO "p54: FreeMAC firmware\n");
  140. break;
  141. case FW_LM20:
  142. printk(KERN_INFO "p54: LM20 firmware\n");
  143. break;
  144. case FW_LM86:
  145. printk(KERN_INFO "p54: LM86 firmware\n");
  146. break;
  147. case FW_LM87:
  148. printk(KERN_INFO "p54: LM87 firmware\n");
  149. break;
  150. default:
  151. printk(KERN_INFO "p54: unknown firmware\n");
  152. break;
  153. }
  154. break;
  155. case BR_CODE_COMPONENT_VERSION:
  156. /* 24 bytes should be enough for all firmwares */
  157. if (strnlen((unsigned char*)bootrec->data, 24) < 24)
  158. fw_version = (unsigned char*)bootrec->data;
  159. break;
  160. case BR_CODE_DESCR: {
  161. struct bootrec_desc *desc =
  162. (struct bootrec_desc *)bootrec->data;
  163. priv->rx_start = le32_to_cpu(desc->rx_start);
  164. /* FIXME add sanity checking */
  165. priv->rx_end = le32_to_cpu(desc->rx_end) - 0x3500;
  166. priv->headroom = desc->headroom;
  167. priv->tailroom = desc->tailroom;
  168. if (le32_to_cpu(bootrec->len) == 11)
  169. priv->rx_mtu = le16_to_cpu(bootrec->rx_mtu);
  170. else
  171. priv->rx_mtu = (size_t)
  172. 0x620 - priv->tx_hdr_len;
  173. break;
  174. }
  175. case BR_CODE_EXPOSED_IF:
  176. exp_if = (struct bootrec_exp_if *) bootrec->data;
  177. for (i = 0; i < (len * sizeof(*exp_if) / 4); i++)
  178. if (exp_if[i].if_id == cpu_to_le16(0x1a))
  179. priv->fw_var = le16_to_cpu(exp_if[i].variant);
  180. break;
  181. case BR_CODE_DEPENDENT_IF:
  182. break;
  183. case BR_CODE_END_OF_BRA:
  184. case LEGACY_BR_CODE_END_OF_BRA:
  185. end_data = NULL;
  186. break;
  187. default:
  188. break;
  189. }
  190. bootrec = (struct bootrec *)&bootrec->data[len];
  191. }
  192. if (fw_version)
  193. printk(KERN_INFO "p54: FW rev %s - Softmac protocol %x.%x\n",
  194. fw_version, priv->fw_var >> 8, priv->fw_var & 0xff);
  195. if (priv->fw_var >= 0x300) {
  196. /* Firmware supports QoS, use it! */
  197. priv->tx_stats[4].limit = 3;
  198. priv->tx_stats[5].limit = 4;
  199. priv->tx_stats[6].limit = 3;
  200. priv->tx_stats[7].limit = 1;
  201. dev->queues = 4;
  202. }
  203. return 0;
  204. }
  205. EXPORT_SYMBOL_GPL(p54_parse_firmware);
  206. static int p54_convert_rev0(struct ieee80211_hw *dev,
  207. struct pda_pa_curve_data *curve_data)
  208. {
  209. struct p54_common *priv = dev->priv;
  210. struct p54_pa_curve_data_sample *dst;
  211. struct pda_pa_curve_data_sample_rev0 *src;
  212. size_t cd_len = sizeof(*curve_data) +
  213. (curve_data->points_per_channel*sizeof(*dst) + 2) *
  214. curve_data->channels;
  215. unsigned int i, j;
  216. void *source, *target;
  217. priv->curve_data = kmalloc(cd_len, GFP_KERNEL);
  218. if (!priv->curve_data)
  219. return -ENOMEM;
  220. memcpy(priv->curve_data, curve_data, sizeof(*curve_data));
  221. source = curve_data->data;
  222. target = priv->curve_data->data;
  223. for (i = 0; i < curve_data->channels; i++) {
  224. __le16 *freq = source;
  225. source += sizeof(__le16);
  226. *((__le16 *)target) = *freq;
  227. target += sizeof(__le16);
  228. for (j = 0; j < curve_data->points_per_channel; j++) {
  229. dst = target;
  230. src = source;
  231. dst->rf_power = src->rf_power;
  232. dst->pa_detector = src->pa_detector;
  233. dst->data_64qam = src->pcv;
  234. /* "invent" the points for the other modulations */
  235. #define SUB(x,y) (u8)((x) - (y)) > (x) ? 0 : (x) - (y)
  236. dst->data_16qam = SUB(src->pcv, 12);
  237. dst->data_qpsk = SUB(dst->data_16qam, 12);
  238. dst->data_bpsk = SUB(dst->data_qpsk, 12);
  239. dst->data_barker = SUB(dst->data_bpsk, 14);
  240. #undef SUB
  241. target += sizeof(*dst);
  242. source += sizeof(*src);
  243. }
  244. }
  245. return 0;
  246. }
  247. static int p54_convert_rev1(struct ieee80211_hw *dev,
  248. struct pda_pa_curve_data *curve_data)
  249. {
  250. struct p54_common *priv = dev->priv;
  251. struct p54_pa_curve_data_sample *dst;
  252. struct pda_pa_curve_data_sample_rev1 *src;
  253. size_t cd_len = sizeof(*curve_data) +
  254. (curve_data->points_per_channel*sizeof(*dst) + 2) *
  255. curve_data->channels;
  256. unsigned int i, j;
  257. void *source, *target;
  258. priv->curve_data = kmalloc(cd_len, GFP_KERNEL);
  259. if (!priv->curve_data)
  260. return -ENOMEM;
  261. memcpy(priv->curve_data, curve_data, sizeof(*curve_data));
  262. source = curve_data->data;
  263. target = priv->curve_data->data;
  264. for (i = 0; i < curve_data->channels; i++) {
  265. __le16 *freq = source;
  266. source += sizeof(__le16);
  267. *((__le16 *)target) = *freq;
  268. target += sizeof(__le16);
  269. for (j = 0; j < curve_data->points_per_channel; j++) {
  270. memcpy(target, source, sizeof(*src));
  271. target += sizeof(*dst);
  272. source += sizeof(*src);
  273. }
  274. source++;
  275. }
  276. return 0;
  277. }
  278. static const char *p54_rf_chips[] = { "NULL", "Duette3", "Duette2",
  279. "Frisbee", "Xbow", "Longbow", "NULL", "NULL" };
  280. static int p54_init_xbow_synth(struct ieee80211_hw *dev);
  281. static int p54_parse_eeprom(struct ieee80211_hw *dev, void *eeprom, int len)
  282. {
  283. struct p54_common *priv = dev->priv;
  284. struct eeprom_pda_wrap *wrap = NULL;
  285. struct pda_entry *entry;
  286. unsigned int data_len, entry_len;
  287. void *tmp;
  288. int err;
  289. u8 *end = (u8 *)eeprom + len;
  290. u16 synth;
  291. DECLARE_MAC_BUF(mac);
  292. wrap = (struct eeprom_pda_wrap *) eeprom;
  293. entry = (void *)wrap->data + le16_to_cpu(wrap->len);
  294. /* verify that at least the entry length/code fits */
  295. while ((u8 *)entry <= end - sizeof(*entry)) {
  296. entry_len = le16_to_cpu(entry->len);
  297. data_len = ((entry_len - 1) << 1);
  298. /* abort if entry exceeds whole structure */
  299. if ((u8 *)entry + sizeof(*entry) + data_len > end)
  300. break;
  301. switch (le16_to_cpu(entry->code)) {
  302. case PDR_MAC_ADDRESS:
  303. SET_IEEE80211_PERM_ADDR(dev, entry->data);
  304. break;
  305. case PDR_PRISM_PA_CAL_OUTPUT_POWER_LIMITS:
  306. if (data_len < 2) {
  307. err = -EINVAL;
  308. goto err;
  309. }
  310. if (2 + entry->data[1]*sizeof(*priv->output_limit) > data_len) {
  311. err = -EINVAL;
  312. goto err;
  313. }
  314. priv->output_limit = kmalloc(entry->data[1] *
  315. sizeof(*priv->output_limit), GFP_KERNEL);
  316. if (!priv->output_limit) {
  317. err = -ENOMEM;
  318. goto err;
  319. }
  320. memcpy(priv->output_limit, &entry->data[2],
  321. entry->data[1]*sizeof(*priv->output_limit));
  322. priv->output_limit_len = entry->data[1];
  323. break;
  324. case PDR_PRISM_PA_CAL_CURVE_DATA: {
  325. struct pda_pa_curve_data *curve_data =
  326. (struct pda_pa_curve_data *)entry->data;
  327. if (data_len < sizeof(*curve_data)) {
  328. err = -EINVAL;
  329. goto err;
  330. }
  331. switch (curve_data->cal_method_rev) {
  332. case 0:
  333. err = p54_convert_rev0(dev, curve_data);
  334. break;
  335. case 1:
  336. err = p54_convert_rev1(dev, curve_data);
  337. break;
  338. default:
  339. printk(KERN_ERR "p54: unknown curve data "
  340. "revision %d\n",
  341. curve_data->cal_method_rev);
  342. err = -ENODEV;
  343. break;
  344. }
  345. if (err)
  346. goto err;
  347. }
  348. case PDR_PRISM_ZIF_TX_IQ_CALIBRATION:
  349. priv->iq_autocal = kmalloc(data_len, GFP_KERNEL);
  350. if (!priv->iq_autocal) {
  351. err = -ENOMEM;
  352. goto err;
  353. }
  354. memcpy(priv->iq_autocal, entry->data, data_len);
  355. priv->iq_autocal_len = data_len / sizeof(struct pda_iq_autocal_entry);
  356. break;
  357. case PDR_INTERFACE_LIST:
  358. tmp = entry->data;
  359. while ((u8 *)tmp < entry->data + data_len) {
  360. struct bootrec_exp_if *exp_if = tmp;
  361. if (le16_to_cpu(exp_if->if_id) == 0xf)
  362. synth = le16_to_cpu(exp_if->variant);
  363. tmp += sizeof(struct bootrec_exp_if);
  364. }
  365. break;
  366. case PDR_HARDWARE_PLATFORM_COMPONENT_ID:
  367. priv->version = *(u8 *)(entry->data + 1);
  368. break;
  369. case PDR_END:
  370. /* make it overrun */
  371. entry_len = len;
  372. break;
  373. default:
  374. printk(KERN_INFO "p54: unknown eeprom code : 0x%x\n",
  375. le16_to_cpu(entry->code));
  376. break;
  377. }
  378. entry = (void *)entry + (entry_len + 1)*2;
  379. }
  380. if (!priv->iq_autocal || !priv->output_limit || !priv->curve_data) {
  381. printk(KERN_ERR "p54: not all required entries found in eeprom!\n");
  382. err = -EINVAL;
  383. goto err;
  384. }
  385. priv->rxhw = synth & 0x07;
  386. if (priv->rxhw == 4)
  387. p54_init_xbow_synth(dev);
  388. if (!(synth & 0x40))
  389. dev->wiphy->bands[IEEE80211_BAND_2GHZ] = &band_2GHz;
  390. if (!(synth & 0x80))
  391. dev->wiphy->bands[IEEE80211_BAND_5GHZ] = &band_5GHz;
  392. if (!is_valid_ether_addr(dev->wiphy->perm_addr)) {
  393. u8 perm_addr[ETH_ALEN];
  394. printk(KERN_WARNING "%s: Invalid hwaddr! Using randomly generated MAC addr\n",
  395. wiphy_name(dev->wiphy));
  396. random_ether_addr(perm_addr);
  397. SET_IEEE80211_PERM_ADDR(dev, perm_addr);
  398. }
  399. printk(KERN_INFO "%s: hwaddr %s, MAC:isl38%02x RF:%s\n",
  400. wiphy_name(dev->wiphy),
  401. print_mac(mac, dev->wiphy->perm_addr),
  402. priv->version, p54_rf_chips[priv->rxhw]);
  403. return 0;
  404. err:
  405. if (priv->iq_autocal) {
  406. kfree(priv->iq_autocal);
  407. priv->iq_autocal = NULL;
  408. }
  409. if (priv->output_limit) {
  410. kfree(priv->output_limit);
  411. priv->output_limit = NULL;
  412. }
  413. if (priv->curve_data) {
  414. kfree(priv->curve_data);
  415. priv->curve_data = NULL;
  416. }
  417. printk(KERN_ERR "p54: eeprom parse failed!\n");
  418. return err;
  419. }
  420. static int p54_rssi_to_dbm(struct ieee80211_hw *dev, int rssi)
  421. {
  422. /* TODO: get the rssi_add & rssi_mul data from the eeprom */
  423. return ((rssi * 0x83) / 64 - 400) / 4;
  424. }
  425. static int p54_rx_data(struct ieee80211_hw *dev, struct sk_buff *skb)
  426. {
  427. struct p54_common *priv = dev->priv;
  428. struct p54_rx_hdr *hdr = (struct p54_rx_hdr *) skb->data;
  429. struct ieee80211_rx_status rx_status = {0};
  430. u16 freq = le16_to_cpu(hdr->freq);
  431. size_t header_len = sizeof(*hdr);
  432. u32 tsf32;
  433. if (!(hdr->magic & cpu_to_le16(0x0001))) {
  434. if (priv->filter_flags & FIF_FCSFAIL)
  435. rx_status.flag |= RX_FLAG_FAILED_FCS_CRC;
  436. else
  437. return 0;
  438. }
  439. rx_status.signal = p54_rssi_to_dbm(dev, hdr->rssi);
  440. rx_status.noise = priv->noise;
  441. /* XX correct? */
  442. rx_status.qual = (100 * hdr->rssi) / 127;
  443. rx_status.rate_idx = (dev->conf.channel->band == IEEE80211_BAND_2GHZ ?
  444. hdr->rate : (hdr->rate - 4)) & 0xf;
  445. rx_status.freq = freq;
  446. rx_status.band = dev->conf.channel->band;
  447. rx_status.antenna = hdr->antenna;
  448. tsf32 = le32_to_cpu(hdr->tsf32);
  449. if (tsf32 < priv->tsf_low32)
  450. priv->tsf_high32++;
  451. rx_status.mactime = ((u64)priv->tsf_high32) << 32 | tsf32;
  452. priv->tsf_low32 = tsf32;
  453. rx_status.flag |= RX_FLAG_TSFT;
  454. if (hdr->magic & cpu_to_le16(0x4000))
  455. header_len += hdr->align[0];
  456. skb_pull(skb, header_len);
  457. skb_trim(skb, le16_to_cpu(hdr->len));
  458. ieee80211_rx_irqsafe(dev, skb, &rx_status);
  459. return -1;
  460. }
  461. static void inline p54_wake_free_queues(struct ieee80211_hw *dev)
  462. {
  463. struct p54_common *priv = dev->priv;
  464. int i;
  465. for (i = 0; i < dev->queues; i++)
  466. if (priv->tx_stats[i + 4].len < priv->tx_stats[i + 4].limit)
  467. ieee80211_wake_queue(dev, i);
  468. }
  469. static void p54_rx_frame_sent(struct ieee80211_hw *dev, struct sk_buff *skb)
  470. {
  471. struct p54_common *priv = dev->priv;
  472. struct p54_control_hdr *hdr = (struct p54_control_hdr *) skb->data;
  473. struct p54_frame_sent_hdr *payload = (struct p54_frame_sent_hdr *) hdr->data;
  474. struct sk_buff *entry = (struct sk_buff *) priv->tx_queue.next;
  475. u32 addr = le32_to_cpu(hdr->req_id) - priv->headroom;
  476. struct memrecord *range = NULL;
  477. u32 freed = 0;
  478. u32 last_addr = priv->rx_start;
  479. unsigned long flags;
  480. spin_lock_irqsave(&priv->tx_queue.lock, flags);
  481. while (entry != (struct sk_buff *)&priv->tx_queue) {
  482. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(entry);
  483. range = (void *)info->driver_data;
  484. if (range->start_addr == addr) {
  485. struct p54_control_hdr *entry_hdr;
  486. struct p54_tx_control_allocdata *entry_data;
  487. int pad = 0;
  488. if (entry->next != (struct sk_buff *)&priv->tx_queue) {
  489. struct ieee80211_tx_info *ni;
  490. struct memrecord *mr;
  491. ni = IEEE80211_SKB_CB(entry->next);
  492. mr = (struct memrecord *)ni->driver_data;
  493. freed = mr->start_addr - last_addr;
  494. } else
  495. freed = priv->rx_end - last_addr;
  496. last_addr = range->end_addr;
  497. __skb_unlink(entry, &priv->tx_queue);
  498. spin_unlock_irqrestore(&priv->tx_queue.lock, flags);
  499. memset(&info->status, 0, sizeof(info->status));
  500. entry_hdr = (struct p54_control_hdr *) entry->data;
  501. entry_data = (struct p54_tx_control_allocdata *) entry_hdr->data;
  502. if ((entry_hdr->magic1 & cpu_to_le16(0x4000)) != 0)
  503. pad = entry_data->align[0];
  504. priv->tx_stats[entry_data->hw_queue].len--;
  505. if (!(info->flags & IEEE80211_TX_CTL_NO_ACK)) {
  506. if (!(payload->status & 0x01))
  507. info->flags |= IEEE80211_TX_STAT_ACK;
  508. else
  509. info->status.excessive_retries = 1;
  510. }
  511. info->status.retry_count = payload->retries - 1;
  512. info->status.ack_signal = p54_rssi_to_dbm(dev,
  513. le16_to_cpu(payload->ack_rssi));
  514. skb_pull(entry, sizeof(*hdr) + pad + sizeof(*entry_data));
  515. ieee80211_tx_status_irqsafe(dev, entry);
  516. goto out;
  517. } else
  518. last_addr = range->end_addr;
  519. entry = entry->next;
  520. }
  521. spin_unlock_irqrestore(&priv->tx_queue.lock, flags);
  522. out:
  523. if (freed >= IEEE80211_MAX_RTS_THRESHOLD + 0x170 +
  524. sizeof(struct p54_control_hdr))
  525. p54_wake_free_queues(dev);
  526. }
  527. static void p54_rx_eeprom_readback(struct ieee80211_hw *dev,
  528. struct sk_buff *skb)
  529. {
  530. struct p54_control_hdr *hdr = (struct p54_control_hdr *) skb->data;
  531. struct p54_eeprom_lm86 *eeprom = (struct p54_eeprom_lm86 *) hdr->data;
  532. struct p54_common *priv = dev->priv;
  533. if (!priv->eeprom)
  534. return ;
  535. memcpy(priv->eeprom, eeprom->data, le16_to_cpu(eeprom->len));
  536. complete(&priv->eeprom_comp);
  537. }
  538. static void p54_rx_stats(struct ieee80211_hw *dev, struct sk_buff *skb)
  539. {
  540. struct p54_common *priv = dev->priv;
  541. struct p54_control_hdr *hdr = (struct p54_control_hdr *) skb->data;
  542. struct p54_statistics *stats = (struct p54_statistics *) hdr->data;
  543. u32 tsf32 = le32_to_cpu(stats->tsf32);
  544. if (tsf32 < priv->tsf_low32)
  545. priv->tsf_high32++;
  546. priv->tsf_low32 = tsf32;
  547. priv->stats.dot11RTSFailureCount = le32_to_cpu(stats->rts_fail);
  548. priv->stats.dot11RTSSuccessCount = le32_to_cpu(stats->rts_success);
  549. priv->stats.dot11FCSErrorCount = le32_to_cpu(stats->rx_bad_fcs);
  550. priv->noise = p54_rssi_to_dbm(dev, le32_to_cpu(stats->noise));
  551. complete(&priv->stats_comp);
  552. mod_timer(&priv->stats_timer, jiffies + 5 * HZ);
  553. }
  554. static int p54_rx_control(struct ieee80211_hw *dev, struct sk_buff *skb)
  555. {
  556. struct p54_control_hdr *hdr = (struct p54_control_hdr *) skb->data;
  557. switch (le16_to_cpu(hdr->type)) {
  558. case P54_CONTROL_TYPE_TXDONE:
  559. p54_rx_frame_sent(dev, skb);
  560. break;
  561. case P54_CONTROL_TYPE_BBP:
  562. break;
  563. case P54_CONTROL_TYPE_STAT_READBACK:
  564. p54_rx_stats(dev, skb);
  565. break;
  566. case P54_CONTROL_TYPE_EEPROM_READBACK:
  567. p54_rx_eeprom_readback(dev, skb);
  568. break;
  569. default:
  570. printk(KERN_DEBUG "%s: not handling 0x%02x type control frame\n",
  571. wiphy_name(dev->wiphy), le16_to_cpu(hdr->type));
  572. break;
  573. }
  574. return 0;
  575. }
  576. /* returns zero if skb can be reused */
  577. int p54_rx(struct ieee80211_hw *dev, struct sk_buff *skb)
  578. {
  579. u8 type = le16_to_cpu(*((__le16 *)skb->data)) >> 8;
  580. if (type == 0x80)
  581. return p54_rx_control(dev, skb);
  582. else
  583. return p54_rx_data(dev, skb);
  584. }
  585. EXPORT_SYMBOL_GPL(p54_rx);
  586. /*
  587. * So, the firmware is somewhat stupid and doesn't know what places in its
  588. * memory incoming data should go to. By poking around in the firmware, we
  589. * can find some unused memory to upload our packets to. However, data that we
  590. * want the card to TX needs to stay intact until the card has told us that
  591. * it is done with it. This function finds empty places we can upload to and
  592. * marks allocated areas as reserved if necessary. p54_rx_frame_sent frees
  593. * allocated areas.
  594. */
  595. static void p54_assign_address(struct ieee80211_hw *dev, struct sk_buff *skb,
  596. struct p54_control_hdr *data, u32 len)
  597. {
  598. struct p54_common *priv = dev->priv;
  599. struct sk_buff *entry = priv->tx_queue.next;
  600. struct sk_buff *target_skb = NULL;
  601. u32 last_addr = priv->rx_start;
  602. u32 largest_hole = 0;
  603. u32 target_addr = priv->rx_start;
  604. unsigned long flags;
  605. unsigned int left;
  606. len = (len + priv->headroom + priv->tailroom + 3) & ~0x3;
  607. spin_lock_irqsave(&priv->tx_queue.lock, flags);
  608. left = skb_queue_len(&priv->tx_queue);
  609. while (left--) {
  610. u32 hole_size;
  611. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(entry);
  612. struct memrecord *range = (void *)info->driver_data;
  613. hole_size = range->start_addr - last_addr;
  614. if (!target_skb && hole_size >= len) {
  615. target_skb = entry->prev;
  616. hole_size -= len;
  617. target_addr = last_addr;
  618. }
  619. largest_hole = max(largest_hole, hole_size);
  620. last_addr = range->end_addr;
  621. entry = entry->next;
  622. }
  623. if (!target_skb && priv->rx_end - last_addr >= len) {
  624. target_skb = priv->tx_queue.prev;
  625. largest_hole = max(largest_hole, priv->rx_end - last_addr - len);
  626. if (!skb_queue_empty(&priv->tx_queue)) {
  627. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(target_skb);
  628. struct memrecord *range = (void *)info->driver_data;
  629. target_addr = range->end_addr;
  630. }
  631. } else
  632. largest_hole = max(largest_hole, priv->rx_end - last_addr);
  633. if (skb) {
  634. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  635. struct memrecord *range = (void *)info->driver_data;
  636. range->start_addr = target_addr;
  637. range->end_addr = target_addr + len;
  638. __skb_queue_after(&priv->tx_queue, target_skb, skb);
  639. if (largest_hole < priv->rx_mtu + priv->headroom +
  640. priv->tailroom +
  641. sizeof(struct p54_control_hdr))
  642. ieee80211_stop_queues(dev);
  643. }
  644. spin_unlock_irqrestore(&priv->tx_queue.lock, flags);
  645. data->req_id = cpu_to_le32(target_addr + priv->headroom);
  646. }
  647. int p54_read_eeprom(struct ieee80211_hw *dev)
  648. {
  649. struct p54_common *priv = dev->priv;
  650. struct p54_control_hdr *hdr = NULL;
  651. struct p54_eeprom_lm86 *eeprom_hdr;
  652. size_t eeprom_size = 0x2020, offset = 0, blocksize;
  653. int ret = -ENOMEM;
  654. void *eeprom = NULL;
  655. hdr = (struct p54_control_hdr *)kzalloc(sizeof(*hdr) +
  656. sizeof(*eeprom_hdr) + EEPROM_READBACK_LEN, GFP_KERNEL);
  657. if (!hdr)
  658. goto free;
  659. priv->eeprom = kzalloc(EEPROM_READBACK_LEN, GFP_KERNEL);
  660. if (!priv->eeprom)
  661. goto free;
  662. eeprom = kzalloc(eeprom_size, GFP_KERNEL);
  663. if (!eeprom)
  664. goto free;
  665. hdr->magic1 = cpu_to_le16(0x8000);
  666. hdr->type = cpu_to_le16(P54_CONTROL_TYPE_EEPROM_READBACK);
  667. hdr->retry1 = hdr->retry2 = 0;
  668. eeprom_hdr = (struct p54_eeprom_lm86 *) hdr->data;
  669. while (eeprom_size) {
  670. blocksize = min(eeprom_size, (size_t)EEPROM_READBACK_LEN);
  671. hdr->len = cpu_to_le16(blocksize + sizeof(*eeprom_hdr));
  672. eeprom_hdr->offset = cpu_to_le16(offset);
  673. eeprom_hdr->len = cpu_to_le16(blocksize);
  674. p54_assign_address(dev, NULL, hdr, le16_to_cpu(hdr->len) +
  675. sizeof(*hdr));
  676. priv->tx(dev, hdr, le16_to_cpu(hdr->len) + sizeof(*hdr), 0);
  677. if (!wait_for_completion_interruptible_timeout(&priv->eeprom_comp, HZ)) {
  678. printk(KERN_ERR "%s: device does not respond!\n",
  679. wiphy_name(dev->wiphy));
  680. ret = -EBUSY;
  681. goto free;
  682. }
  683. memcpy(eeprom + offset, priv->eeprom, blocksize);
  684. offset += blocksize;
  685. eeprom_size -= blocksize;
  686. }
  687. ret = p54_parse_eeprom(dev, eeprom, offset);
  688. free:
  689. kfree(priv->eeprom);
  690. priv->eeprom = NULL;
  691. kfree(hdr);
  692. kfree(eeprom);
  693. return ret;
  694. }
  695. EXPORT_SYMBOL_GPL(p54_read_eeprom);
  696. static int p54_tx(struct ieee80211_hw *dev, struct sk_buff *skb)
  697. {
  698. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  699. struct ieee80211_tx_queue_stats *current_queue;
  700. struct p54_common *priv = dev->priv;
  701. struct p54_control_hdr *hdr;
  702. struct ieee80211_hdr *ieee80211hdr = (struct ieee80211_hdr *)skb->data;
  703. struct p54_tx_control_allocdata *txhdr;
  704. size_t padding, len;
  705. u8 rate;
  706. u8 cts_rate = 0x20;
  707. current_queue = &priv->tx_stats[skb_get_queue_mapping(skb) + 4];
  708. if (unlikely(current_queue->len > current_queue->limit))
  709. return NETDEV_TX_BUSY;
  710. current_queue->len++;
  711. current_queue->count++;
  712. if (current_queue->len == current_queue->limit)
  713. ieee80211_stop_queue(dev, skb_get_queue_mapping(skb));
  714. padding = (unsigned long)(skb->data - (sizeof(*hdr) + sizeof(*txhdr))) & 3;
  715. len = skb->len;
  716. txhdr = (struct p54_tx_control_allocdata *)
  717. skb_push(skb, sizeof(*txhdr) + padding);
  718. hdr = (struct p54_control_hdr *) skb_push(skb, sizeof(*hdr));
  719. if (padding)
  720. hdr->magic1 = cpu_to_le16(0x4010);
  721. else
  722. hdr->magic1 = cpu_to_le16(0x0010);
  723. hdr->len = cpu_to_le16(len);
  724. hdr->type = (info->flags & IEEE80211_TX_CTL_NO_ACK) ? 0 : cpu_to_le16(1);
  725. hdr->retry1 = hdr->retry2 = info->control.retry_limit;
  726. /* TODO: add support for alternate retry TX rates */
  727. rate = ieee80211_get_tx_rate(dev, info)->hw_value;
  728. if (info->flags & IEEE80211_TX_CTL_SHORT_PREAMBLE) {
  729. rate |= 0x10;
  730. cts_rate |= 0x10;
  731. }
  732. if (info->flags & IEEE80211_TX_CTL_USE_RTS_CTS) {
  733. rate |= 0x40;
  734. cts_rate |= ieee80211_get_rts_cts_rate(dev, info)->hw_value;
  735. } else if (info->flags & IEEE80211_TX_CTL_USE_CTS_PROTECT) {
  736. rate |= 0x20;
  737. cts_rate |= ieee80211_get_rts_cts_rate(dev, info)->hw_value;
  738. }
  739. memset(txhdr->rateset, rate, 8);
  740. txhdr->key_type = 0;
  741. txhdr->key_len = 0;
  742. txhdr->hw_queue = skb_get_queue_mapping(skb) + 4;
  743. txhdr->tx_antenna = (info->antenna_sel_tx == 0) ?
  744. 2 : info->antenna_sel_tx - 1;
  745. txhdr->output_power = priv->output_power;
  746. txhdr->cts_rate = (info->flags & IEEE80211_TX_CTL_NO_ACK) ?
  747. 0 : cts_rate;
  748. if (padding)
  749. txhdr->align[0] = padding;
  750. /* FIXME: The sequence that follows is needed for this driver to
  751. * work with mac80211 since "mac80211: fix TX sequence numbers".
  752. * As with the temporary code in rt2x00, changes will be needed
  753. * to get proper sequence numbers on beacons. In addition, this
  754. * patch places the sequence number in the hardware state, which
  755. * limits us to a single virtual state.
  756. */
  757. if (info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ) {
  758. if (info->flags & IEEE80211_TX_CTL_FIRST_FRAGMENT)
  759. priv->seqno += 0x10;
  760. ieee80211hdr->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
  761. ieee80211hdr->seq_ctrl |= cpu_to_le16(priv->seqno);
  762. }
  763. /* modifies skb->cb and with it info, so must be last! */
  764. p54_assign_address(dev, skb, hdr, skb->len);
  765. priv->tx(dev, hdr, skb->len, 0);
  766. return 0;
  767. }
  768. static int p54_set_filter(struct ieee80211_hw *dev, u16 filter_type,
  769. const u8 *bssid)
  770. {
  771. struct p54_common *priv = dev->priv;
  772. struct p54_control_hdr *hdr;
  773. struct p54_tx_control_filter *filter;
  774. size_t data_len;
  775. hdr = kzalloc(sizeof(*hdr) + sizeof(*filter) +
  776. priv->tx_hdr_len, GFP_ATOMIC);
  777. if (!hdr)
  778. return -ENOMEM;
  779. hdr = (void *)hdr + priv->tx_hdr_len;
  780. filter = (struct p54_tx_control_filter *) hdr->data;
  781. hdr->magic1 = cpu_to_le16(0x8001);
  782. hdr->type = cpu_to_le16(P54_CONTROL_TYPE_FILTER_SET);
  783. priv->filter_type = filter->filter_type = cpu_to_le16(filter_type);
  784. memcpy(filter->mac_addr, priv->mac_addr, ETH_ALEN);
  785. if (!bssid)
  786. memset(filter->bssid, ~0, ETH_ALEN);
  787. else
  788. memcpy(filter->bssid, bssid, ETH_ALEN);
  789. filter->rx_antenna = priv->rx_antenna;
  790. if (priv->fw_var < 0x500) {
  791. data_len = P54_TX_CONTROL_FILTER_V1_LEN;
  792. filter->v1.basic_rate_mask = cpu_to_le32(0x15F);
  793. filter->v1.rx_addr = cpu_to_le32(priv->rx_end);
  794. filter->v1.max_rx = cpu_to_le16(priv->rx_mtu);
  795. filter->v1.rxhw = cpu_to_le16(priv->rxhw);
  796. filter->v1.wakeup_timer = cpu_to_le16(500);
  797. } else {
  798. data_len = P54_TX_CONTROL_FILTER_V2_LEN;
  799. filter->v2.rx_addr = cpu_to_le32(priv->rx_end);
  800. filter->v2.max_rx = cpu_to_le16(priv->rx_mtu);
  801. filter->v2.rxhw = cpu_to_le16(priv->rxhw);
  802. filter->v2.timer = cpu_to_le16(1000);
  803. }
  804. hdr->len = cpu_to_le16(data_len);
  805. p54_assign_address(dev, NULL, hdr, sizeof(*hdr) + data_len);
  806. priv->tx(dev, hdr, sizeof(*hdr) + data_len, 1);
  807. return 0;
  808. }
  809. static int p54_set_freq(struct ieee80211_hw *dev, __le16 freq)
  810. {
  811. struct p54_common *priv = dev->priv;
  812. struct p54_control_hdr *hdr;
  813. struct p54_tx_control_channel *chan;
  814. unsigned int i;
  815. size_t data_len;
  816. void *entry;
  817. hdr = kzalloc(sizeof(*hdr) + sizeof(*chan) +
  818. priv->tx_hdr_len, GFP_KERNEL);
  819. if (!hdr)
  820. return -ENOMEM;
  821. hdr = (void *)hdr + priv->tx_hdr_len;
  822. chan = (struct p54_tx_control_channel *) hdr->data;
  823. hdr->magic1 = cpu_to_le16(0x8001);
  824. hdr->type = cpu_to_le16(P54_CONTROL_TYPE_CHANNEL_CHANGE);
  825. chan->flags = cpu_to_le16(0x1);
  826. chan->dwell = cpu_to_le16(0x0);
  827. for (i = 0; i < priv->iq_autocal_len; i++) {
  828. if (priv->iq_autocal[i].freq != freq)
  829. continue;
  830. memcpy(&chan->iq_autocal, &priv->iq_autocal[i],
  831. sizeof(*priv->iq_autocal));
  832. break;
  833. }
  834. if (i == priv->iq_autocal_len)
  835. goto err;
  836. for (i = 0; i < priv->output_limit_len; i++) {
  837. if (priv->output_limit[i].freq != freq)
  838. continue;
  839. chan->val_barker = 0x38;
  840. chan->val_bpsk = chan->dup_bpsk =
  841. priv->output_limit[i].val_bpsk;
  842. chan->val_qpsk = chan->dup_qpsk =
  843. priv->output_limit[i].val_qpsk;
  844. chan->val_16qam = chan->dup_16qam =
  845. priv->output_limit[i].val_16qam;
  846. chan->val_64qam = chan->dup_64qam =
  847. priv->output_limit[i].val_64qam;
  848. break;
  849. }
  850. if (i == priv->output_limit_len)
  851. goto err;
  852. entry = priv->curve_data->data;
  853. for (i = 0; i < priv->curve_data->channels; i++) {
  854. if (*((__le16 *)entry) != freq) {
  855. entry += sizeof(__le16);
  856. entry += sizeof(struct p54_pa_curve_data_sample) *
  857. priv->curve_data->points_per_channel;
  858. continue;
  859. }
  860. entry += sizeof(__le16);
  861. chan->pa_points_per_curve =
  862. min(priv->curve_data->points_per_channel, (u8) 8);
  863. memcpy(chan->curve_data, entry, sizeof(*chan->curve_data) *
  864. chan->pa_points_per_curve);
  865. break;
  866. }
  867. if (priv->fw_var < 0x500) {
  868. data_len = P54_TX_CONTROL_CHANNEL_V1_LEN;
  869. chan->v1.rssical_mul = cpu_to_le16(130);
  870. chan->v1.rssical_add = cpu_to_le16(0xfe70);
  871. } else {
  872. data_len = P54_TX_CONTROL_CHANNEL_V2_LEN;
  873. chan->v2.rssical_mul = cpu_to_le16(130);
  874. chan->v2.rssical_add = cpu_to_le16(0xfe70);
  875. chan->v2.basic_rate_mask = cpu_to_le32(0x15f);
  876. }
  877. hdr->len = cpu_to_le16(data_len);
  878. p54_assign_address(dev, NULL, hdr, sizeof(*hdr) + data_len);
  879. priv->tx(dev, hdr, sizeof(*hdr) + data_len, 1);
  880. return 0;
  881. err:
  882. printk(KERN_ERR "%s: frequency change failed\n", wiphy_name(dev->wiphy));
  883. kfree(hdr);
  884. return -EINVAL;
  885. }
  886. static int p54_set_leds(struct ieee80211_hw *dev, int mode, int link, int act)
  887. {
  888. struct p54_common *priv = dev->priv;
  889. struct p54_control_hdr *hdr;
  890. struct p54_tx_control_led *led;
  891. hdr = kzalloc(sizeof(*hdr) + sizeof(*led) +
  892. priv->tx_hdr_len, GFP_KERNEL);
  893. if (!hdr)
  894. return -ENOMEM;
  895. hdr = (void *)hdr + priv->tx_hdr_len;
  896. hdr->magic1 = cpu_to_le16(0x8001);
  897. hdr->len = cpu_to_le16(sizeof(*led));
  898. hdr->type = cpu_to_le16(P54_CONTROL_TYPE_LED);
  899. p54_assign_address(dev, NULL, hdr, sizeof(*hdr) + sizeof(*led));
  900. led = (struct p54_tx_control_led *) hdr->data;
  901. led->mode = cpu_to_le16(mode);
  902. led->led_permanent = cpu_to_le16(link);
  903. led->led_temporary = cpu_to_le16(act);
  904. led->duration = cpu_to_le16(1000);
  905. priv->tx(dev, hdr, sizeof(*hdr) + sizeof(*led), 1);
  906. return 0;
  907. }
  908. #define P54_SET_QUEUE(queue, ai_fs, cw_min, cw_max, _txop) \
  909. do { \
  910. queue.aifs = cpu_to_le16(ai_fs); \
  911. queue.cwmin = cpu_to_le16(cw_min); \
  912. queue.cwmax = cpu_to_le16(cw_max); \
  913. queue.txop = cpu_to_le16(_txop); \
  914. } while(0)
  915. static void p54_init_vdcf(struct ieee80211_hw *dev)
  916. {
  917. struct p54_common *priv = dev->priv;
  918. struct p54_control_hdr *hdr;
  919. struct p54_tx_control_vdcf *vdcf;
  920. /* all USB V1 adapters need a extra headroom */
  921. hdr = (void *)priv->cached_vdcf + priv->tx_hdr_len;
  922. hdr->magic1 = cpu_to_le16(0x8001);
  923. hdr->len = cpu_to_le16(sizeof(*vdcf));
  924. hdr->type = cpu_to_le16(P54_CONTROL_TYPE_DCFINIT);
  925. hdr->req_id = cpu_to_le32(priv->rx_start);
  926. vdcf = (struct p54_tx_control_vdcf *) hdr->data;
  927. P54_SET_QUEUE(vdcf->queue[0], 0x0002, 0x0003, 0x0007, 47);
  928. P54_SET_QUEUE(vdcf->queue[1], 0x0002, 0x0007, 0x000f, 94);
  929. P54_SET_QUEUE(vdcf->queue[2], 0x0003, 0x000f, 0x03ff, 0);
  930. P54_SET_QUEUE(vdcf->queue[3], 0x0007, 0x000f, 0x03ff, 0);
  931. }
  932. static void p54_set_vdcf(struct ieee80211_hw *dev)
  933. {
  934. struct p54_common *priv = dev->priv;
  935. struct p54_control_hdr *hdr;
  936. struct p54_tx_control_vdcf *vdcf;
  937. hdr = (void *)priv->cached_vdcf + priv->tx_hdr_len;
  938. p54_assign_address(dev, NULL, hdr, sizeof(*hdr) + sizeof(*vdcf));
  939. vdcf = (struct p54_tx_control_vdcf *) hdr->data;
  940. if (dev->conf.flags & IEEE80211_CONF_SHORT_SLOT_TIME) {
  941. vdcf->slottime = 9;
  942. vdcf->magic1 = 0x10;
  943. vdcf->magic2 = 0x00;
  944. } else {
  945. vdcf->slottime = 20;
  946. vdcf->magic1 = 0x0a;
  947. vdcf->magic2 = 0x06;
  948. }
  949. /* (see prism54/isl_oid.h for further details) */
  950. vdcf->frameburst = cpu_to_le16(0);
  951. priv->tx(dev, hdr, sizeof(*hdr) + sizeof(*vdcf), 0);
  952. }
  953. static int p54_start(struct ieee80211_hw *dev)
  954. {
  955. struct p54_common *priv = dev->priv;
  956. int err;
  957. if (!priv->cached_vdcf) {
  958. priv->cached_vdcf = kzalloc(sizeof(struct p54_tx_control_vdcf)+
  959. priv->tx_hdr_len + sizeof(struct p54_control_hdr),
  960. GFP_KERNEL);
  961. if (!priv->cached_vdcf)
  962. return -ENOMEM;
  963. }
  964. if (!priv->cached_stats) {
  965. priv->cached_stats = kzalloc(sizeof(struct p54_statistics) +
  966. priv->tx_hdr_len + sizeof(struct p54_control_hdr),
  967. GFP_KERNEL);
  968. if (!priv->cached_stats) {
  969. kfree(priv->cached_vdcf);
  970. priv->cached_vdcf = NULL;
  971. return -ENOMEM;
  972. }
  973. }
  974. err = priv->open(dev);
  975. if (!err)
  976. priv->mode = NL80211_IFTYPE_MONITOR;
  977. p54_init_vdcf(dev);
  978. mod_timer(&priv->stats_timer, jiffies + HZ);
  979. return err;
  980. }
  981. static void p54_stop(struct ieee80211_hw *dev)
  982. {
  983. struct p54_common *priv = dev->priv;
  984. struct sk_buff *skb;
  985. del_timer(&priv->stats_timer);
  986. while ((skb = skb_dequeue(&priv->tx_queue)))
  987. kfree_skb(skb);
  988. priv->stop(dev);
  989. priv->tsf_high32 = priv->tsf_low32 = 0;
  990. priv->mode = NL80211_IFTYPE_UNSPECIFIED;
  991. }
  992. static int p54_add_interface(struct ieee80211_hw *dev,
  993. struct ieee80211_if_init_conf *conf)
  994. {
  995. struct p54_common *priv = dev->priv;
  996. if (priv->mode != NL80211_IFTYPE_MONITOR)
  997. return -EOPNOTSUPP;
  998. switch (conf->type) {
  999. case NL80211_IFTYPE_STATION:
  1000. priv->mode = conf->type;
  1001. break;
  1002. default:
  1003. return -EOPNOTSUPP;
  1004. }
  1005. memcpy(priv->mac_addr, conf->mac_addr, ETH_ALEN);
  1006. p54_set_filter(dev, 0, NULL);
  1007. switch (conf->type) {
  1008. case NL80211_IFTYPE_STATION:
  1009. p54_set_filter(dev, 1, NULL);
  1010. break;
  1011. default:
  1012. BUG(); /* impossible */
  1013. break;
  1014. }
  1015. p54_set_leds(dev, 1, 0, 0);
  1016. return 0;
  1017. }
  1018. static void p54_remove_interface(struct ieee80211_hw *dev,
  1019. struct ieee80211_if_init_conf *conf)
  1020. {
  1021. struct p54_common *priv = dev->priv;
  1022. priv->mode = NL80211_IFTYPE_MONITOR;
  1023. memset(priv->mac_addr, 0, ETH_ALEN);
  1024. p54_set_filter(dev, 0, NULL);
  1025. }
  1026. static int p54_config(struct ieee80211_hw *dev, struct ieee80211_conf *conf)
  1027. {
  1028. int ret;
  1029. struct p54_common *priv = dev->priv;
  1030. mutex_lock(&priv->conf_mutex);
  1031. priv->rx_antenna = (conf->antenna_sel_rx == 0) ?
  1032. 2 : conf->antenna_sel_tx - 1;
  1033. priv->output_power = conf->power_level << 2;
  1034. ret = p54_set_freq(dev, cpu_to_le16(conf->channel->center_freq));
  1035. p54_set_vdcf(dev);
  1036. mutex_unlock(&priv->conf_mutex);
  1037. return ret;
  1038. }
  1039. static int p54_config_interface(struct ieee80211_hw *dev,
  1040. struct ieee80211_vif *vif,
  1041. struct ieee80211_if_conf *conf)
  1042. {
  1043. struct p54_common *priv = dev->priv;
  1044. mutex_lock(&priv->conf_mutex);
  1045. p54_set_filter(dev, 0, conf->bssid);
  1046. p54_set_leds(dev, 1, !is_multicast_ether_addr(conf->bssid), 0);
  1047. memcpy(priv->bssid, conf->bssid, ETH_ALEN);
  1048. mutex_unlock(&priv->conf_mutex);
  1049. return 0;
  1050. }
  1051. static void p54_configure_filter(struct ieee80211_hw *dev,
  1052. unsigned int changed_flags,
  1053. unsigned int *total_flags,
  1054. int mc_count, struct dev_mc_list *mclist)
  1055. {
  1056. struct p54_common *priv = dev->priv;
  1057. *total_flags &= FIF_BCN_PRBRESP_PROMISC |
  1058. FIF_PROMISC_IN_BSS |
  1059. FIF_FCSFAIL;
  1060. priv->filter_flags = *total_flags;
  1061. if (changed_flags & FIF_BCN_PRBRESP_PROMISC) {
  1062. if (*total_flags & FIF_BCN_PRBRESP_PROMISC)
  1063. p54_set_filter(dev, le16_to_cpu(priv->filter_type),
  1064. NULL);
  1065. else
  1066. p54_set_filter(dev, le16_to_cpu(priv->filter_type),
  1067. priv->bssid);
  1068. }
  1069. if (changed_flags & FIF_PROMISC_IN_BSS) {
  1070. if (*total_flags & FIF_PROMISC_IN_BSS)
  1071. p54_set_filter(dev, le16_to_cpu(priv->filter_type) |
  1072. 0x8, NULL);
  1073. else
  1074. p54_set_filter(dev, le16_to_cpu(priv->filter_type) &
  1075. ~0x8, priv->bssid);
  1076. }
  1077. }
  1078. static int p54_conf_tx(struct ieee80211_hw *dev, u16 queue,
  1079. const struct ieee80211_tx_queue_params *params)
  1080. {
  1081. struct p54_common *priv = dev->priv;
  1082. struct p54_tx_control_vdcf *vdcf;
  1083. vdcf = (struct p54_tx_control_vdcf *)(((struct p54_control_hdr *)
  1084. ((void *)priv->cached_vdcf + priv->tx_hdr_len))->data);
  1085. if ((params) && !(queue > 4)) {
  1086. P54_SET_QUEUE(vdcf->queue[queue], params->aifs,
  1087. params->cw_min, params->cw_max, params->txop);
  1088. } else
  1089. return -EINVAL;
  1090. p54_set_vdcf(dev);
  1091. return 0;
  1092. }
  1093. static int p54_init_xbow_synth(struct ieee80211_hw *dev)
  1094. {
  1095. struct p54_common *priv = dev->priv;
  1096. struct p54_control_hdr *hdr;
  1097. struct p54_tx_control_xbow_synth *xbow;
  1098. hdr = kzalloc(sizeof(*hdr) + sizeof(*xbow) +
  1099. priv->tx_hdr_len, GFP_KERNEL);
  1100. if (!hdr)
  1101. return -ENOMEM;
  1102. hdr = (void *)hdr + priv->tx_hdr_len;
  1103. hdr->magic1 = cpu_to_le16(0x8001);
  1104. hdr->len = cpu_to_le16(sizeof(*xbow));
  1105. hdr->type = cpu_to_le16(P54_CONTROL_TYPE_XBOW_SYNTH_CFG);
  1106. p54_assign_address(dev, NULL, hdr, sizeof(*hdr) + sizeof(*xbow));
  1107. xbow = (struct p54_tx_control_xbow_synth *) hdr->data;
  1108. xbow->magic1 = cpu_to_le16(0x1);
  1109. xbow->magic2 = cpu_to_le16(0x2);
  1110. xbow->freq = cpu_to_le16(5390);
  1111. priv->tx(dev, hdr, sizeof(*hdr) + sizeof(*xbow), 1);
  1112. return 0;
  1113. }
  1114. static void p54_statistics_timer(unsigned long data)
  1115. {
  1116. struct ieee80211_hw *dev = (struct ieee80211_hw *) data;
  1117. struct p54_common *priv = dev->priv;
  1118. struct p54_control_hdr *hdr;
  1119. struct p54_statistics *stats;
  1120. BUG_ON(!priv->cached_stats);
  1121. hdr = (void *)priv->cached_stats + priv->tx_hdr_len;
  1122. hdr->magic1 = cpu_to_le16(0x8000);
  1123. hdr->len = cpu_to_le16(sizeof(*stats));
  1124. hdr->type = cpu_to_le16(P54_CONTROL_TYPE_STAT_READBACK);
  1125. p54_assign_address(dev, NULL, hdr, sizeof(*hdr) + sizeof(*stats));
  1126. priv->tx(dev, hdr, sizeof(*hdr) + sizeof(*stats), 0);
  1127. }
  1128. static int p54_get_stats(struct ieee80211_hw *dev,
  1129. struct ieee80211_low_level_stats *stats)
  1130. {
  1131. struct p54_common *priv = dev->priv;
  1132. del_timer(&priv->stats_timer);
  1133. p54_statistics_timer((unsigned long)dev);
  1134. if (!wait_for_completion_interruptible_timeout(&priv->stats_comp, HZ)) {
  1135. printk(KERN_ERR "%s: device does not respond!\n",
  1136. wiphy_name(dev->wiphy));
  1137. return -EBUSY;
  1138. }
  1139. memcpy(stats, &priv->stats, sizeof(*stats));
  1140. return 0;
  1141. }
  1142. static int p54_get_tx_stats(struct ieee80211_hw *dev,
  1143. struct ieee80211_tx_queue_stats *stats)
  1144. {
  1145. struct p54_common *priv = dev->priv;
  1146. memcpy(stats, &priv->tx_stats[4], sizeof(stats[0]) * dev->queues);
  1147. return 0;
  1148. }
  1149. static const struct ieee80211_ops p54_ops = {
  1150. .tx = p54_tx,
  1151. .start = p54_start,
  1152. .stop = p54_stop,
  1153. .add_interface = p54_add_interface,
  1154. .remove_interface = p54_remove_interface,
  1155. .config = p54_config,
  1156. .config_interface = p54_config_interface,
  1157. .configure_filter = p54_configure_filter,
  1158. .conf_tx = p54_conf_tx,
  1159. .get_stats = p54_get_stats,
  1160. .get_tx_stats = p54_get_tx_stats
  1161. };
  1162. struct ieee80211_hw *p54_init_common(size_t priv_data_len)
  1163. {
  1164. struct ieee80211_hw *dev;
  1165. struct p54_common *priv;
  1166. dev = ieee80211_alloc_hw(priv_data_len, &p54_ops);
  1167. if (!dev)
  1168. return NULL;
  1169. priv = dev->priv;
  1170. priv->mode = NL80211_IFTYPE_UNSPECIFIED;
  1171. skb_queue_head_init(&priv->tx_queue);
  1172. dev->flags = IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING | /* not sure */
  1173. IEEE80211_HW_RX_INCLUDES_FCS |
  1174. IEEE80211_HW_SIGNAL_DBM |
  1175. IEEE80211_HW_NOISE_DBM;
  1176. dev->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION);
  1177. dev->channel_change_time = 1000; /* TODO: find actual value */
  1178. priv->tx_stats[0].limit = 1;
  1179. priv->tx_stats[1].limit = 1;
  1180. priv->tx_stats[2].limit = 1;
  1181. priv->tx_stats[3].limit = 1;
  1182. priv->tx_stats[4].limit = 5;
  1183. dev->queues = 1;
  1184. priv->noise = -94;
  1185. dev->extra_tx_headroom = sizeof(struct p54_control_hdr) + 4 +
  1186. sizeof(struct p54_tx_control_allocdata);
  1187. mutex_init(&priv->conf_mutex);
  1188. init_completion(&priv->eeprom_comp);
  1189. init_completion(&priv->stats_comp);
  1190. setup_timer(&priv->stats_timer, p54_statistics_timer,
  1191. (unsigned long)dev);
  1192. return dev;
  1193. }
  1194. EXPORT_SYMBOL_GPL(p54_init_common);
  1195. void p54_free_common(struct ieee80211_hw *dev)
  1196. {
  1197. struct p54_common *priv = dev->priv;
  1198. kfree(priv->cached_stats);
  1199. kfree(priv->iq_autocal);
  1200. kfree(priv->output_limit);
  1201. kfree(priv->curve_data);
  1202. kfree(priv->cached_vdcf);
  1203. }
  1204. EXPORT_SYMBOL_GPL(p54_free_common);
  1205. static int __init p54_init(void)
  1206. {
  1207. return 0;
  1208. }
  1209. static void __exit p54_exit(void)
  1210. {
  1211. }
  1212. module_init(p54_init);
  1213. module_exit(p54_exit);