p54common.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043
  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_rates[] = {
  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_channels[] = {
  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_channels,
  56. .n_channels = ARRAY_SIZE(p54_channels),
  57. .bitrates = p54_rates,
  58. .n_bitrates = ARRAY_SIZE(p54_rates),
  59. };
  60. void p54_parse_firmware(struct ieee80211_hw *dev, const struct firmware *fw)
  61. {
  62. struct p54_common *priv = dev->priv;
  63. struct bootrec_exp_if *exp_if;
  64. struct bootrec *bootrec;
  65. u32 *data = (u32 *)fw->data;
  66. u32 *end_data = (u32 *)fw->data + (fw->size >> 2);
  67. u8 *fw_version = NULL;
  68. size_t len;
  69. int i;
  70. if (priv->rx_start)
  71. return;
  72. while (data < end_data && *data)
  73. data++;
  74. while (data < end_data && !*data)
  75. data++;
  76. bootrec = (struct bootrec *) data;
  77. while (bootrec->data <= end_data &&
  78. (bootrec->data + (len = le32_to_cpu(bootrec->len))) <= end_data) {
  79. u32 code = le32_to_cpu(bootrec->code);
  80. switch (code) {
  81. case BR_CODE_COMPONENT_ID:
  82. switch (be32_to_cpu(*(__be32 *)bootrec->data)) {
  83. case FW_FMAC:
  84. printk(KERN_INFO "p54: FreeMAC firmware\n");
  85. break;
  86. case FW_LM20:
  87. printk(KERN_INFO "p54: LM20 firmware\n");
  88. break;
  89. case FW_LM86:
  90. printk(KERN_INFO "p54: LM86 firmware\n");
  91. break;
  92. case FW_LM87:
  93. printk(KERN_INFO "p54: LM87 firmware - not supported yet!\n");
  94. break;
  95. default:
  96. printk(KERN_INFO "p54: unknown firmware\n");
  97. break;
  98. }
  99. break;
  100. case BR_CODE_COMPONENT_VERSION:
  101. /* 24 bytes should be enough for all firmwares */
  102. if (strnlen((unsigned char*)bootrec->data, 24) < 24)
  103. fw_version = (unsigned char*)bootrec->data;
  104. break;
  105. case BR_CODE_DESCR:
  106. priv->rx_start = le32_to_cpu(((__le32 *)bootrec->data)[1]);
  107. /* FIXME add sanity checking */
  108. priv->rx_end = le32_to_cpu(((__le32 *)bootrec->data)[2]) - 0x3500;
  109. break;
  110. case BR_CODE_EXPOSED_IF:
  111. exp_if = (struct bootrec_exp_if *) bootrec->data;
  112. for (i = 0; i < (len * sizeof(*exp_if) / 4); i++)
  113. if (exp_if[i].if_id == cpu_to_le16(0x1a))
  114. priv->fw_var = le16_to_cpu(exp_if[i].variant);
  115. break;
  116. case BR_CODE_DEPENDENT_IF:
  117. break;
  118. case BR_CODE_END_OF_BRA:
  119. case LEGACY_BR_CODE_END_OF_BRA:
  120. end_data = NULL;
  121. break;
  122. default:
  123. break;
  124. }
  125. bootrec = (struct bootrec *)&bootrec->data[len];
  126. }
  127. if (fw_version)
  128. printk(KERN_INFO "p54: FW rev %s - Softmac protocol %x.%x\n",
  129. fw_version, priv->fw_var >> 8, priv->fw_var & 0xff);
  130. if (priv->fw_var >= 0x300) {
  131. /* Firmware supports QoS, use it! */
  132. priv->tx_stats.data[0].limit = 3;
  133. priv->tx_stats.data[1].limit = 4;
  134. priv->tx_stats.data[2].limit = 3;
  135. priv->tx_stats.data[3].limit = 1;
  136. dev->queues = 4;
  137. }
  138. }
  139. EXPORT_SYMBOL_GPL(p54_parse_firmware);
  140. static int p54_convert_rev0_to_rev1(struct ieee80211_hw *dev,
  141. struct pda_pa_curve_data *curve_data)
  142. {
  143. struct p54_common *priv = dev->priv;
  144. struct pda_pa_curve_data_sample_rev1 *rev1;
  145. struct pda_pa_curve_data_sample_rev0 *rev0;
  146. size_t cd_len = sizeof(*curve_data) +
  147. (curve_data->points_per_channel*sizeof(*rev1) + 2) *
  148. curve_data->channels;
  149. unsigned int i, j;
  150. void *source, *target;
  151. priv->curve_data = kmalloc(cd_len, GFP_KERNEL);
  152. if (!priv->curve_data)
  153. return -ENOMEM;
  154. memcpy(priv->curve_data, curve_data, sizeof(*curve_data));
  155. source = curve_data->data;
  156. target = priv->curve_data->data;
  157. for (i = 0; i < curve_data->channels; i++) {
  158. __le16 *freq = source;
  159. source += sizeof(__le16);
  160. *((__le16 *)target) = *freq;
  161. target += sizeof(__le16);
  162. for (j = 0; j < curve_data->points_per_channel; j++) {
  163. rev1 = target;
  164. rev0 = source;
  165. rev1->rf_power = rev0->rf_power;
  166. rev1->pa_detector = rev0->pa_detector;
  167. rev1->data_64qam = rev0->pcv;
  168. /* "invent" the points for the other modulations */
  169. #define SUB(x,y) (u8)((x) - (y)) > (x) ? 0 : (x) - (y)
  170. rev1->data_16qam = SUB(rev0->pcv, 12);
  171. rev1->data_qpsk = SUB(rev1->data_16qam, 12);
  172. rev1->data_bpsk = SUB(rev1->data_qpsk, 12);
  173. rev1->data_barker= SUB(rev1->data_bpsk, 14);
  174. #undef SUB
  175. target += sizeof(*rev1);
  176. source += sizeof(*rev0);
  177. }
  178. }
  179. return 0;
  180. }
  181. int p54_parse_eeprom(struct ieee80211_hw *dev, void *eeprom, int len)
  182. {
  183. struct p54_common *priv = dev->priv;
  184. struct eeprom_pda_wrap *wrap = NULL;
  185. struct pda_entry *entry;
  186. int i = 0;
  187. unsigned int data_len, entry_len;
  188. void *tmp;
  189. int err;
  190. wrap = (struct eeprom_pda_wrap *) eeprom;
  191. entry = (void *)wrap->data + wrap->len;
  192. i += 2;
  193. i += le16_to_cpu(entry->len)*2;
  194. while (i < len) {
  195. entry_len = le16_to_cpu(entry->len);
  196. data_len = ((entry_len - 1) << 1);
  197. switch (le16_to_cpu(entry->code)) {
  198. case PDR_MAC_ADDRESS:
  199. SET_IEEE80211_PERM_ADDR(dev, entry->data);
  200. break;
  201. case PDR_PRISM_PA_CAL_OUTPUT_POWER_LIMITS:
  202. if (data_len < 2) {
  203. err = -EINVAL;
  204. goto err;
  205. }
  206. if (2 + entry->data[1]*sizeof(*priv->output_limit) > data_len) {
  207. err = -EINVAL;
  208. goto err;
  209. }
  210. priv->output_limit = kmalloc(entry->data[1] *
  211. sizeof(*priv->output_limit), GFP_KERNEL);
  212. if (!priv->output_limit) {
  213. err = -ENOMEM;
  214. goto err;
  215. }
  216. memcpy(priv->output_limit, &entry->data[2],
  217. entry->data[1]*sizeof(*priv->output_limit));
  218. priv->output_limit_len = entry->data[1];
  219. break;
  220. case PDR_PRISM_PA_CAL_CURVE_DATA:
  221. if (data_len < sizeof(struct pda_pa_curve_data)) {
  222. err = -EINVAL;
  223. goto err;
  224. }
  225. if (((struct pda_pa_curve_data *)entry->data)->cal_method_rev) {
  226. priv->curve_data = kmalloc(data_len, GFP_KERNEL);
  227. if (!priv->curve_data) {
  228. err = -ENOMEM;
  229. goto err;
  230. }
  231. memcpy(priv->curve_data, entry->data, data_len);
  232. } else {
  233. err = p54_convert_rev0_to_rev1(dev, (struct pda_pa_curve_data *)entry->data);
  234. if (err)
  235. goto err;
  236. }
  237. break;
  238. case PDR_PRISM_ZIF_TX_IQ_CALIBRATION:
  239. priv->iq_autocal = kmalloc(data_len, GFP_KERNEL);
  240. if (!priv->iq_autocal) {
  241. err = -ENOMEM;
  242. goto err;
  243. }
  244. memcpy(priv->iq_autocal, entry->data, data_len);
  245. priv->iq_autocal_len = data_len / sizeof(struct pda_iq_autocal_entry);
  246. break;
  247. case PDR_INTERFACE_LIST:
  248. tmp = entry->data;
  249. while ((u8 *)tmp < entry->data + data_len) {
  250. struct bootrec_exp_if *exp_if = tmp;
  251. if (le16_to_cpu(exp_if->if_id) == 0xF)
  252. priv->rxhw = exp_if->variant & cpu_to_le16(0x07);
  253. tmp += sizeof(struct bootrec_exp_if);
  254. }
  255. break;
  256. case PDR_HARDWARE_PLATFORM_COMPONENT_ID:
  257. priv->version = *(u8 *)(entry->data + 1);
  258. break;
  259. case PDR_END:
  260. i = len;
  261. break;
  262. }
  263. entry = (void *)entry + (entry_len + 1)*2;
  264. i += 2;
  265. i += entry_len*2;
  266. }
  267. if (!priv->iq_autocal || !priv->output_limit || !priv->curve_data) {
  268. printk(KERN_ERR "p54: not all required entries found in eeprom!\n");
  269. err = -EINVAL;
  270. goto err;
  271. }
  272. return 0;
  273. err:
  274. if (priv->iq_autocal) {
  275. kfree(priv->iq_autocal);
  276. priv->iq_autocal = NULL;
  277. }
  278. if (priv->output_limit) {
  279. kfree(priv->output_limit);
  280. priv->output_limit = NULL;
  281. }
  282. if (priv->curve_data) {
  283. kfree(priv->curve_data);
  284. priv->curve_data = NULL;
  285. }
  286. printk(KERN_ERR "p54: eeprom parse failed!\n");
  287. return err;
  288. }
  289. EXPORT_SYMBOL_GPL(p54_parse_eeprom);
  290. void p54_fill_eeprom_readback(struct p54_control_hdr *hdr)
  291. {
  292. struct p54_eeprom_lm86 *eeprom_hdr;
  293. hdr->magic1 = cpu_to_le16(0x8000);
  294. hdr->len = cpu_to_le16(sizeof(*eeprom_hdr) + 0x2000);
  295. hdr->type = cpu_to_le16(P54_CONTROL_TYPE_EEPROM_READBACK);
  296. hdr->retry1 = hdr->retry2 = 0;
  297. eeprom_hdr = (struct p54_eeprom_lm86 *) hdr->data;
  298. eeprom_hdr->offset = 0x0;
  299. eeprom_hdr->len = cpu_to_le16(0x2000);
  300. }
  301. EXPORT_SYMBOL_GPL(p54_fill_eeprom_readback);
  302. static void p54_rx_data(struct ieee80211_hw *dev, struct sk_buff *skb)
  303. {
  304. struct p54_rx_hdr *hdr = (struct p54_rx_hdr *) skb->data;
  305. struct ieee80211_rx_status rx_status = {0};
  306. u16 freq = le16_to_cpu(hdr->freq);
  307. rx_status.ssi = hdr->rssi;
  308. /* XX correct? */
  309. rx_status.rate_idx = hdr->rate & 0xf;
  310. rx_status.freq = freq;
  311. rx_status.band = IEEE80211_BAND_2GHZ;
  312. rx_status.antenna = hdr->antenna;
  313. rx_status.mactime = le64_to_cpu(hdr->timestamp);
  314. rx_status.flag |= RX_FLAG_TSFT;
  315. skb_pull(skb, sizeof(*hdr));
  316. skb_trim(skb, le16_to_cpu(hdr->len));
  317. ieee80211_rx_irqsafe(dev, skb, &rx_status);
  318. }
  319. static void inline p54_wake_free_queues(struct ieee80211_hw *dev)
  320. {
  321. struct p54_common *priv = dev->priv;
  322. int i;
  323. /* ieee80211_start_queues is great if all queues are really empty.
  324. * But, what if some are full? */
  325. for (i = 0; i < dev->queues; i++)
  326. if (priv->tx_stats.data[i].len < priv->tx_stats.data[i].limit)
  327. ieee80211_wake_queue(dev, i);
  328. }
  329. static void p54_rx_frame_sent(struct ieee80211_hw *dev, struct sk_buff *skb)
  330. {
  331. struct p54_common *priv = dev->priv;
  332. struct p54_control_hdr *hdr = (struct p54_control_hdr *) skb->data;
  333. struct p54_frame_sent_hdr *payload = (struct p54_frame_sent_hdr *) hdr->data;
  334. struct sk_buff *entry = (struct sk_buff *) priv->tx_queue.next;
  335. u32 addr = le32_to_cpu(hdr->req_id) - 0x70;
  336. struct memrecord *range = NULL;
  337. u32 freed = 0;
  338. u32 last_addr = priv->rx_start;
  339. while (entry != (struct sk_buff *)&priv->tx_queue) {
  340. range = (struct memrecord *)&entry->cb;
  341. if (range->start_addr == addr) {
  342. struct ieee80211_tx_status status;
  343. struct p54_control_hdr *entry_hdr;
  344. struct p54_tx_control_allocdata *entry_data;
  345. int pad = 0;
  346. if (entry->next != (struct sk_buff *)&priv->tx_queue)
  347. freed = ((struct memrecord *)&entry->next->cb)->start_addr - last_addr;
  348. else
  349. freed = priv->rx_end - last_addr;
  350. last_addr = range->end_addr;
  351. __skb_unlink(entry, &priv->tx_queue);
  352. if (!range->control) {
  353. kfree_skb(entry);
  354. break;
  355. }
  356. memset(&status, 0, sizeof(status));
  357. memcpy(&status.control, range->control,
  358. sizeof(status.control));
  359. kfree(range->control);
  360. priv->tx_stats.data[status.control.queue].len--;
  361. entry_hdr = (struct p54_control_hdr *) entry->data;
  362. entry_data = (struct p54_tx_control_allocdata *) entry_hdr->data;
  363. if ((entry_hdr->magic1 & cpu_to_le16(0x4000)) != 0)
  364. pad = entry_data->align[0];
  365. if (!(status.control.flags & IEEE80211_TXCTL_NO_ACK)) {
  366. if (!(payload->status & 0x01))
  367. status.flags |= IEEE80211_TX_STATUS_ACK;
  368. else
  369. status.excessive_retries = 1;
  370. }
  371. status.retry_count = payload->retries - 1;
  372. status.ack_signal = le16_to_cpu(payload->ack_rssi);
  373. skb_pull(entry, sizeof(*hdr) + pad + sizeof(*entry_data));
  374. ieee80211_tx_status_irqsafe(dev, entry, &status);
  375. break;
  376. } else
  377. last_addr = range->end_addr;
  378. entry = entry->next;
  379. }
  380. if (freed >= IEEE80211_MAX_RTS_THRESHOLD + 0x170 +
  381. sizeof(struct p54_control_hdr))
  382. p54_wake_free_queues(dev);
  383. }
  384. static void p54_rx_control(struct ieee80211_hw *dev, struct sk_buff *skb)
  385. {
  386. struct p54_control_hdr *hdr = (struct p54_control_hdr *) skb->data;
  387. switch (le16_to_cpu(hdr->type)) {
  388. case P54_CONTROL_TYPE_TXDONE:
  389. p54_rx_frame_sent(dev, skb);
  390. break;
  391. case P54_CONTROL_TYPE_BBP:
  392. break;
  393. default:
  394. printk(KERN_DEBUG "%s: not handling 0x%02x type control frame\n",
  395. wiphy_name(dev->wiphy), le16_to_cpu(hdr->type));
  396. break;
  397. }
  398. }
  399. /* returns zero if skb can be reused */
  400. int p54_rx(struct ieee80211_hw *dev, struct sk_buff *skb)
  401. {
  402. u8 type = le16_to_cpu(*((__le16 *)skb->data)) >> 8;
  403. switch (type) {
  404. case 0x00:
  405. case 0x01:
  406. p54_rx_data(dev, skb);
  407. return -1;
  408. case 0x4d:
  409. /* TODO: do something better... but then again, I've never seen this happen */
  410. printk(KERN_ERR "%s: Received fault. Probably need to restart hardware now..\n",
  411. wiphy_name(dev->wiphy));
  412. break;
  413. case 0x80:
  414. p54_rx_control(dev, skb);
  415. break;
  416. default:
  417. printk(KERN_ERR "%s: unknown frame RXed (0x%02x)\n",
  418. wiphy_name(dev->wiphy), type);
  419. break;
  420. }
  421. return 0;
  422. }
  423. EXPORT_SYMBOL_GPL(p54_rx);
  424. /*
  425. * So, the firmware is somewhat stupid and doesn't know what places in its
  426. * memory incoming data should go to. By poking around in the firmware, we
  427. * can find some unused memory to upload our packets to. However, data that we
  428. * want the card to TX needs to stay intact until the card has told us that
  429. * it is done with it. This function finds empty places we can upload to and
  430. * marks allocated areas as reserved if necessary. p54_rx_frame_sent frees
  431. * allocated areas.
  432. */
  433. static void p54_assign_address(struct ieee80211_hw *dev, struct sk_buff *skb,
  434. struct p54_control_hdr *data, u32 len,
  435. struct ieee80211_tx_control *control)
  436. {
  437. struct p54_common *priv = dev->priv;
  438. struct sk_buff *entry = priv->tx_queue.next;
  439. struct sk_buff *target_skb = NULL;
  440. struct memrecord *range;
  441. u32 last_addr = priv->rx_start;
  442. u32 largest_hole = 0;
  443. u32 target_addr = priv->rx_start;
  444. unsigned long flags;
  445. unsigned int left;
  446. len = (len + 0x170 + 3) & ~0x3; /* 0x70 headroom, 0x100 tailroom */
  447. spin_lock_irqsave(&priv->tx_queue.lock, flags);
  448. left = skb_queue_len(&priv->tx_queue);
  449. while (left--) {
  450. u32 hole_size;
  451. range = (struct memrecord *)&entry->cb;
  452. hole_size = range->start_addr - last_addr;
  453. if (!target_skb && hole_size >= len) {
  454. target_skb = entry->prev;
  455. hole_size -= len;
  456. target_addr = last_addr;
  457. }
  458. largest_hole = max(largest_hole, hole_size);
  459. last_addr = range->end_addr;
  460. entry = entry->next;
  461. }
  462. if (!target_skb && priv->rx_end - last_addr >= len) {
  463. target_skb = priv->tx_queue.prev;
  464. largest_hole = max(largest_hole, priv->rx_end - last_addr - len);
  465. if (!skb_queue_empty(&priv->tx_queue)) {
  466. range = (struct memrecord *)&target_skb->cb;
  467. target_addr = range->end_addr;
  468. }
  469. } else
  470. largest_hole = max(largest_hole, priv->rx_end - last_addr);
  471. if (skb) {
  472. range = (struct memrecord *)&skb->cb;
  473. range->start_addr = target_addr;
  474. range->end_addr = target_addr + len;
  475. range->control = control;
  476. __skb_queue_after(&priv->tx_queue, target_skb, skb);
  477. if (largest_hole < IEEE80211_MAX_RTS_THRESHOLD + 0x170 +
  478. sizeof(struct p54_control_hdr))
  479. ieee80211_stop_queues(dev);
  480. }
  481. spin_unlock_irqrestore(&priv->tx_queue.lock, flags);
  482. data->req_id = cpu_to_le32(target_addr + 0x70);
  483. }
  484. static int p54_tx(struct ieee80211_hw *dev, struct sk_buff *skb,
  485. struct ieee80211_tx_control *control)
  486. {
  487. struct ieee80211_tx_queue_stats_data *current_queue;
  488. struct p54_common *priv = dev->priv;
  489. struct p54_control_hdr *hdr;
  490. struct p54_tx_control_allocdata *txhdr;
  491. struct ieee80211_tx_control *control_copy;
  492. size_t padding, len;
  493. u8 rate;
  494. current_queue = &priv->tx_stats.data[control->queue];
  495. if (unlikely(current_queue->len > current_queue->limit))
  496. return NETDEV_TX_BUSY;
  497. current_queue->len++;
  498. current_queue->count++;
  499. if (current_queue->len == current_queue->limit)
  500. ieee80211_stop_queue(dev, control->queue);
  501. padding = (unsigned long)(skb->data - (sizeof(*hdr) + sizeof(*txhdr))) & 3;
  502. len = skb->len;
  503. control_copy = kmalloc(sizeof(*control), GFP_ATOMIC);
  504. if (control_copy)
  505. memcpy(control_copy, control, sizeof(*control));
  506. txhdr = (struct p54_tx_control_allocdata *)
  507. skb_push(skb, sizeof(*txhdr) + padding);
  508. hdr = (struct p54_control_hdr *) skb_push(skb, sizeof(*hdr));
  509. if (padding)
  510. hdr->magic1 = cpu_to_le16(0x4010);
  511. else
  512. hdr->magic1 = cpu_to_le16(0x0010);
  513. hdr->len = cpu_to_le16(len);
  514. hdr->type = (control->flags & IEEE80211_TXCTL_NO_ACK) ? 0 : cpu_to_le16(1);
  515. hdr->retry1 = hdr->retry2 = control->retry_limit;
  516. p54_assign_address(dev, skb, hdr, skb->len, control_copy);
  517. memset(txhdr->wep_key, 0x0, 16);
  518. txhdr->padding = 0;
  519. txhdr->padding2 = 0;
  520. /* TODO: add support for alternate retry TX rates */
  521. rate = control->tx_rate->hw_value;
  522. if (control->flags & IEEE80211_TXCTL_SHORT_PREAMBLE)
  523. rate |= 0x10;
  524. if (control->flags & IEEE80211_TXCTL_USE_RTS_CTS)
  525. rate |= 0x40;
  526. else if (control->flags & IEEE80211_TXCTL_USE_CTS_PROTECT)
  527. rate |= 0x20;
  528. memset(txhdr->rateset, rate, 8);
  529. txhdr->wep_key_present = 0;
  530. txhdr->wep_key_len = 0;
  531. txhdr->frame_type = cpu_to_le32(control->queue + 4);
  532. txhdr->magic4 = 0;
  533. txhdr->antenna = (control->antenna_sel_tx == 0) ?
  534. 2 : control->antenna_sel_tx - 1;
  535. txhdr->output_power = 0x7f; // HW Maximum
  536. txhdr->magic5 = (control->flags & IEEE80211_TXCTL_NO_ACK) ?
  537. 0 : ((rate > 0x3) ? cpu_to_le32(0x33) : cpu_to_le32(0x23));
  538. if (padding)
  539. txhdr->align[0] = padding;
  540. priv->tx(dev, hdr, skb->len, 0);
  541. return 0;
  542. }
  543. static int p54_set_filter(struct ieee80211_hw *dev, u16 filter_type,
  544. const u8 *dst, const u8 *src, u8 antenna,
  545. u32 magic3, u32 magic8, u32 magic9)
  546. {
  547. struct p54_common *priv = dev->priv;
  548. struct p54_control_hdr *hdr;
  549. struct p54_tx_control_filter *filter;
  550. hdr = kzalloc(sizeof(*hdr) + sizeof(*filter) +
  551. priv->tx_hdr_len, GFP_ATOMIC);
  552. if (!hdr)
  553. return -ENOMEM;
  554. hdr = (void *)hdr + priv->tx_hdr_len;
  555. filter = (struct p54_tx_control_filter *) hdr->data;
  556. hdr->magic1 = cpu_to_le16(0x8001);
  557. hdr->len = cpu_to_le16(sizeof(*filter));
  558. p54_assign_address(dev, NULL, hdr, sizeof(*hdr) + sizeof(*filter), NULL);
  559. hdr->type = cpu_to_le16(P54_CONTROL_TYPE_FILTER_SET);
  560. filter->filter_type = cpu_to_le16(filter_type);
  561. memcpy(filter->dst, dst, ETH_ALEN);
  562. if (!src)
  563. memset(filter->src, ~0, ETH_ALEN);
  564. else
  565. memcpy(filter->src, src, ETH_ALEN);
  566. filter->antenna = antenna;
  567. filter->magic3 = cpu_to_le32(magic3);
  568. filter->rx_addr = cpu_to_le32(priv->rx_end);
  569. filter->max_rx = cpu_to_le16(0x0620); /* FIXME: for usb ver 1.. maybe */
  570. filter->rxhw = priv->rxhw;
  571. filter->magic8 = cpu_to_le16(magic8);
  572. filter->magic9 = cpu_to_le16(magic9);
  573. priv->tx(dev, hdr, sizeof(*hdr) + sizeof(*filter), 1);
  574. return 0;
  575. }
  576. static int p54_set_freq(struct ieee80211_hw *dev, __le16 freq)
  577. {
  578. struct p54_common *priv = dev->priv;
  579. struct p54_control_hdr *hdr;
  580. struct p54_tx_control_channel *chan;
  581. unsigned int i;
  582. size_t payload_len = sizeof(*chan) + sizeof(u32)*2 +
  583. sizeof(*chan->curve_data) *
  584. priv->curve_data->points_per_channel;
  585. void *entry;
  586. hdr = kzalloc(sizeof(*hdr) + payload_len +
  587. priv->tx_hdr_len, GFP_KERNEL);
  588. if (!hdr)
  589. return -ENOMEM;
  590. hdr = (void *)hdr + priv->tx_hdr_len;
  591. chan = (struct p54_tx_control_channel *) hdr->data;
  592. hdr->magic1 = cpu_to_le16(0x8001);
  593. hdr->len = cpu_to_le16(sizeof(*chan));
  594. hdr->type = cpu_to_le16(P54_CONTROL_TYPE_CHANNEL_CHANGE);
  595. p54_assign_address(dev, NULL, hdr, sizeof(*hdr) + payload_len, NULL);
  596. chan->magic1 = cpu_to_le16(0x1);
  597. chan->magic2 = cpu_to_le16(0x0);
  598. for (i = 0; i < priv->iq_autocal_len; i++) {
  599. if (priv->iq_autocal[i].freq != freq)
  600. continue;
  601. memcpy(&chan->iq_autocal, &priv->iq_autocal[i],
  602. sizeof(*priv->iq_autocal));
  603. break;
  604. }
  605. if (i == priv->iq_autocal_len)
  606. goto err;
  607. for (i = 0; i < priv->output_limit_len; i++) {
  608. if (priv->output_limit[i].freq != freq)
  609. continue;
  610. chan->val_barker = 0x38;
  611. chan->val_bpsk = priv->output_limit[i].val_bpsk;
  612. chan->val_qpsk = priv->output_limit[i].val_qpsk;
  613. chan->val_16qam = priv->output_limit[i].val_16qam;
  614. chan->val_64qam = priv->output_limit[i].val_64qam;
  615. break;
  616. }
  617. if (i == priv->output_limit_len)
  618. goto err;
  619. chan->pa_points_per_curve = priv->curve_data->points_per_channel;
  620. entry = priv->curve_data->data;
  621. for (i = 0; i < priv->curve_data->channels; i++) {
  622. if (*((__le16 *)entry) != freq) {
  623. entry += sizeof(__le16);
  624. entry += sizeof(struct pda_pa_curve_data_sample_rev1) *
  625. chan->pa_points_per_curve;
  626. continue;
  627. }
  628. entry += sizeof(__le16);
  629. memcpy(chan->curve_data, entry, sizeof(*chan->curve_data) *
  630. chan->pa_points_per_curve);
  631. break;
  632. }
  633. memcpy(hdr->data + payload_len - 4, &chan->val_bpsk, 4);
  634. priv->tx(dev, hdr, sizeof(*hdr) + payload_len, 1);
  635. return 0;
  636. err:
  637. printk(KERN_ERR "%s: frequency change failed\n", wiphy_name(dev->wiphy));
  638. kfree(hdr);
  639. return -EINVAL;
  640. }
  641. static int p54_set_leds(struct ieee80211_hw *dev, int mode, int link, int act)
  642. {
  643. struct p54_common *priv = dev->priv;
  644. struct p54_control_hdr *hdr;
  645. struct p54_tx_control_led *led;
  646. hdr = kzalloc(sizeof(*hdr) + sizeof(*led) +
  647. priv->tx_hdr_len, GFP_KERNEL);
  648. if (!hdr)
  649. return -ENOMEM;
  650. hdr = (void *)hdr + priv->tx_hdr_len;
  651. hdr->magic1 = cpu_to_le16(0x8001);
  652. hdr->len = cpu_to_le16(sizeof(*led));
  653. hdr->type = cpu_to_le16(P54_CONTROL_TYPE_LED);
  654. p54_assign_address(dev, NULL, hdr, sizeof(*hdr) + sizeof(*led), NULL);
  655. led = (struct p54_tx_control_led *) hdr->data;
  656. led->mode = cpu_to_le16(mode);
  657. led->led_permanent = cpu_to_le16(link);
  658. led->led_temporary = cpu_to_le16(act);
  659. led->duration = cpu_to_le16(1000);
  660. priv->tx(dev, hdr, sizeof(*hdr) + sizeof(*led), 1);
  661. return 0;
  662. }
  663. #define P54_SET_QUEUE(queue, ai_fs, cw_min, cw_max, _txop) \
  664. do { \
  665. queue.aifs = cpu_to_le16(ai_fs); \
  666. queue.cwmin = cpu_to_le16(cw_min); \
  667. queue.cwmax = cpu_to_le16(cw_max); \
  668. queue.txop = cpu_to_le16(_txop); \
  669. } while(0)
  670. static void p54_init_vdcf(struct ieee80211_hw *dev)
  671. {
  672. struct p54_common *priv = dev->priv;
  673. struct p54_control_hdr *hdr;
  674. struct p54_tx_control_vdcf *vdcf;
  675. /* all USB V1 adapters need a extra headroom */
  676. hdr = (void *)priv->cached_vdcf + priv->tx_hdr_len;
  677. hdr->magic1 = cpu_to_le16(0x8001);
  678. hdr->len = cpu_to_le16(sizeof(*vdcf));
  679. hdr->type = cpu_to_le16(P54_CONTROL_TYPE_DCFINIT);
  680. hdr->req_id = cpu_to_le32(priv->rx_start);
  681. vdcf = (struct p54_tx_control_vdcf *) hdr->data;
  682. P54_SET_QUEUE(vdcf->queue[0], 0x0002, 0x0003, 0x0007, 47);
  683. P54_SET_QUEUE(vdcf->queue[1], 0x0002, 0x0007, 0x000f, 94);
  684. P54_SET_QUEUE(vdcf->queue[2], 0x0003, 0x000f, 0x03ff, 0);
  685. P54_SET_QUEUE(vdcf->queue[3], 0x0007, 0x000f, 0x03ff, 0);
  686. }
  687. static void p54_set_vdcf(struct ieee80211_hw *dev)
  688. {
  689. struct p54_common *priv = dev->priv;
  690. struct p54_control_hdr *hdr;
  691. struct p54_tx_control_vdcf *vdcf;
  692. hdr = (void *)priv->cached_vdcf + priv->tx_hdr_len;
  693. p54_assign_address(dev, NULL, hdr, sizeof(*hdr) + sizeof(*vdcf), NULL);
  694. vdcf = (struct p54_tx_control_vdcf *) hdr->data;
  695. if (dev->conf.flags & IEEE80211_CONF_SHORT_SLOT_TIME) {
  696. vdcf->slottime = 9;
  697. vdcf->magic1 = 0x00;
  698. vdcf->magic2 = 0x10;
  699. } else {
  700. vdcf->slottime = 20;
  701. vdcf->magic1 = 0x0a;
  702. vdcf->magic2 = 0x06;
  703. }
  704. /* (see prism54/isl_oid.h for further details) */
  705. vdcf->frameburst = cpu_to_le16(0);
  706. priv->tx(dev, hdr, sizeof(*hdr) + sizeof(*vdcf), 0);
  707. }
  708. static int p54_start(struct ieee80211_hw *dev)
  709. {
  710. struct p54_common *priv = dev->priv;
  711. int err;
  712. err = priv->open(dev);
  713. if (!err)
  714. priv->mode = IEEE80211_IF_TYPE_MNTR;
  715. return err;
  716. }
  717. static void p54_stop(struct ieee80211_hw *dev)
  718. {
  719. struct p54_common *priv = dev->priv;
  720. struct sk_buff *skb;
  721. while ((skb = skb_dequeue(&priv->tx_queue))) {
  722. struct memrecord *range = (struct memrecord *)&skb->cb;
  723. if (range->control)
  724. kfree(range->control);
  725. kfree_skb(skb);
  726. }
  727. priv->stop(dev);
  728. priv->mode = IEEE80211_IF_TYPE_INVALID;
  729. }
  730. static int p54_add_interface(struct ieee80211_hw *dev,
  731. struct ieee80211_if_init_conf *conf)
  732. {
  733. struct p54_common *priv = dev->priv;
  734. if (priv->mode != IEEE80211_IF_TYPE_MNTR)
  735. return -EOPNOTSUPP;
  736. switch (conf->type) {
  737. case IEEE80211_IF_TYPE_STA:
  738. priv->mode = conf->type;
  739. break;
  740. default:
  741. return -EOPNOTSUPP;
  742. }
  743. memcpy(priv->mac_addr, conf->mac_addr, ETH_ALEN);
  744. p54_set_filter(dev, 0, priv->mac_addr, NULL, 0, 1, 0, 0xF642);
  745. p54_set_filter(dev, 0, priv->mac_addr, NULL, 1, 0, 0, 0xF642);
  746. switch (conf->type) {
  747. case IEEE80211_IF_TYPE_STA:
  748. p54_set_filter(dev, 1, priv->mac_addr, NULL, 0, 0x15F, 0x1F4, 0);
  749. break;
  750. default:
  751. BUG(); /* impossible */
  752. break;
  753. }
  754. p54_set_leds(dev, 1, 0, 0);
  755. return 0;
  756. }
  757. static void p54_remove_interface(struct ieee80211_hw *dev,
  758. struct ieee80211_if_init_conf *conf)
  759. {
  760. struct p54_common *priv = dev->priv;
  761. priv->mode = IEEE80211_IF_TYPE_MNTR;
  762. memset(priv->mac_addr, 0, ETH_ALEN);
  763. p54_set_filter(dev, 0, priv->mac_addr, NULL, 2, 0, 0, 0);
  764. }
  765. static int p54_config(struct ieee80211_hw *dev, struct ieee80211_conf *conf)
  766. {
  767. int ret;
  768. ret = p54_set_freq(dev, cpu_to_le16(conf->channel->center_freq));
  769. p54_set_vdcf(dev);
  770. return ret;
  771. }
  772. static int p54_config_interface(struct ieee80211_hw *dev,
  773. struct ieee80211_vif *vif,
  774. struct ieee80211_if_conf *conf)
  775. {
  776. struct p54_common *priv = dev->priv;
  777. p54_set_filter(dev, 0, priv->mac_addr, conf->bssid, 0, 1, 0, 0xF642);
  778. p54_set_filter(dev, 0, priv->mac_addr, conf->bssid, 2, 0, 0, 0);
  779. p54_set_leds(dev, 1, !is_multicast_ether_addr(conf->bssid), 0);
  780. memcpy(priv->bssid, conf->bssid, ETH_ALEN);
  781. return 0;
  782. }
  783. static void p54_configure_filter(struct ieee80211_hw *dev,
  784. unsigned int changed_flags,
  785. unsigned int *total_flags,
  786. int mc_count, struct dev_mc_list *mclist)
  787. {
  788. struct p54_common *priv = dev->priv;
  789. *total_flags &= FIF_BCN_PRBRESP_PROMISC;
  790. if (changed_flags & FIF_BCN_PRBRESP_PROMISC) {
  791. if (*total_flags & FIF_BCN_PRBRESP_PROMISC)
  792. p54_set_filter(dev, 0, priv->mac_addr,
  793. NULL, 2, 0, 0, 0);
  794. else
  795. p54_set_filter(dev, 0, priv->mac_addr,
  796. priv->bssid, 2, 0, 0, 0);
  797. }
  798. }
  799. static int p54_conf_tx(struct ieee80211_hw *dev, int queue,
  800. const struct ieee80211_tx_queue_params *params)
  801. {
  802. struct p54_common *priv = dev->priv;
  803. struct p54_tx_control_vdcf *vdcf;
  804. vdcf = (struct p54_tx_control_vdcf *)(((struct p54_control_hdr *)
  805. ((void *)priv->cached_vdcf + priv->tx_hdr_len))->data);
  806. if ((params) && !((queue < 0) || (queue > 4))) {
  807. P54_SET_QUEUE(vdcf->queue[queue], params->aifs,
  808. params->cw_min, params->cw_max, params->txop);
  809. } else
  810. return -EINVAL;
  811. p54_set_vdcf(dev);
  812. return 0;
  813. }
  814. static int p54_get_stats(struct ieee80211_hw *dev,
  815. struct ieee80211_low_level_stats *stats)
  816. {
  817. /* TODO */
  818. return 0;
  819. }
  820. static int p54_get_tx_stats(struct ieee80211_hw *dev,
  821. struct ieee80211_tx_queue_stats *stats)
  822. {
  823. struct p54_common *priv = dev->priv;
  824. unsigned int i;
  825. for (i = 0; i < dev->queues; i++)
  826. memcpy(&stats->data[i], &priv->tx_stats.data[i],
  827. sizeof(stats->data[i]));
  828. return 0;
  829. }
  830. static const struct ieee80211_ops p54_ops = {
  831. .tx = p54_tx,
  832. .start = p54_start,
  833. .stop = p54_stop,
  834. .add_interface = p54_add_interface,
  835. .remove_interface = p54_remove_interface,
  836. .config = p54_config,
  837. .config_interface = p54_config_interface,
  838. .configure_filter = p54_configure_filter,
  839. .conf_tx = p54_conf_tx,
  840. .get_stats = p54_get_stats,
  841. .get_tx_stats = p54_get_tx_stats
  842. };
  843. struct ieee80211_hw *p54_init_common(size_t priv_data_len)
  844. {
  845. struct ieee80211_hw *dev;
  846. struct p54_common *priv;
  847. dev = ieee80211_alloc_hw(priv_data_len, &p54_ops);
  848. if (!dev)
  849. return NULL;
  850. priv = dev->priv;
  851. priv->mode = IEEE80211_IF_TYPE_INVALID;
  852. skb_queue_head_init(&priv->tx_queue);
  853. dev->wiphy->bands[IEEE80211_BAND_2GHZ] = &band_2GHz;
  854. dev->flags = IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING | /* not sure */
  855. IEEE80211_HW_RX_INCLUDES_FCS;
  856. dev->channel_change_time = 1000; /* TODO: find actual value */
  857. dev->max_rssi = 127;
  858. priv->tx_stats.data[0].limit = 5;
  859. dev->queues = 1;
  860. dev->extra_tx_headroom = sizeof(struct p54_control_hdr) + 4 +
  861. sizeof(struct p54_tx_control_allocdata);
  862. priv->cached_vdcf = kzalloc(sizeof(struct p54_tx_control_vdcf) +
  863. priv->tx_hdr_len + sizeof(struct p54_control_hdr), GFP_KERNEL);
  864. if (!priv->cached_vdcf) {
  865. ieee80211_free_hw(dev);
  866. return NULL;
  867. }
  868. p54_init_vdcf(dev);
  869. return dev;
  870. }
  871. EXPORT_SYMBOL_GPL(p54_init_common);
  872. void p54_free_common(struct ieee80211_hw *dev)
  873. {
  874. struct p54_common *priv = dev->priv;
  875. kfree(priv->iq_autocal);
  876. kfree(priv->output_limit);
  877. kfree(priv->curve_data);
  878. kfree(priv->cached_vdcf);
  879. }
  880. EXPORT_SYMBOL_GPL(p54_free_common);
  881. static int __init p54_init(void)
  882. {
  883. return 0;
  884. }
  885. static void __exit p54_exit(void)
  886. {
  887. }
  888. module_init(p54_init);
  889. module_exit(p54_exit);