scan.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /* Helpers for managing scan queues
  2. *
  3. * See copyright notice in main.c
  4. */
  5. #include <linux/gfp.h>
  6. #include <linux/kernel.h>
  7. #include <linux/string.h>
  8. #include <linux/ieee80211.h>
  9. #include <net/cfg80211.h>
  10. #include "hermes.h"
  11. #include "orinoco.h"
  12. #include "main.h"
  13. #include "scan.h"
  14. #define ZERO_DBM_OFFSET 0x95
  15. #define MAX_SIGNAL_LEVEL 0x8A
  16. #define MIN_SIGNAL_LEVEL 0x2F
  17. #define SIGNAL_TO_DBM(x) \
  18. (clamp_t(s32, (x), MIN_SIGNAL_LEVEL, MAX_SIGNAL_LEVEL) \
  19. - ZERO_DBM_OFFSET)
  20. #define SIGNAL_TO_MBM(x) (SIGNAL_TO_DBM(x) * 100)
  21. static int symbol_build_supp_rates(u8 *buf, const __le16 *rates)
  22. {
  23. int i;
  24. u8 rate;
  25. buf[0] = WLAN_EID_SUPP_RATES;
  26. for (i = 0; i < 5; i++) {
  27. rate = le16_to_cpu(rates[i]);
  28. /* NULL terminated */
  29. if (rate == 0x0)
  30. break;
  31. buf[i + 2] = rate;
  32. }
  33. buf[1] = i;
  34. return i + 2;
  35. }
  36. static int prism_build_supp_rates(u8 *buf, const u8 *rates)
  37. {
  38. int i;
  39. buf[0] = WLAN_EID_SUPP_RATES;
  40. for (i = 0; i < 8; i++) {
  41. /* NULL terminated */
  42. if (rates[i] == 0x0)
  43. break;
  44. buf[i + 2] = rates[i];
  45. }
  46. buf[1] = i;
  47. /* We might still have another 2 rates, which need to go in
  48. * extended supported rates */
  49. if (i == 8 && rates[i] > 0) {
  50. buf[10] = WLAN_EID_EXT_SUPP_RATES;
  51. for (; i < 10; i++) {
  52. /* NULL terminated */
  53. if (rates[i] == 0x0)
  54. break;
  55. buf[i + 2] = rates[i];
  56. }
  57. buf[11] = i - 8;
  58. }
  59. return (i < 8) ? i + 2 : i + 4;
  60. }
  61. static void orinoco_add_hostscan_result(struct orinoco_private *priv,
  62. const union hermes_scan_info *bss)
  63. {
  64. struct wiphy *wiphy = priv_to_wiphy(priv);
  65. struct ieee80211_channel *channel;
  66. struct cfg80211_bss *cbss;
  67. u8 *ie;
  68. u8 ie_buf[46];
  69. u64 timestamp;
  70. s32 signal;
  71. u16 capability;
  72. u16 beacon_interval;
  73. int ie_len;
  74. int freq;
  75. int len;
  76. len = le16_to_cpu(bss->a.essid_len);
  77. /* Reconstruct SSID and bitrate IEs to pass up */
  78. ie_buf[0] = WLAN_EID_SSID;
  79. ie_buf[1] = len;
  80. memcpy(&ie_buf[2], bss->a.essid, len);
  81. ie = ie_buf + len + 2;
  82. ie_len = ie_buf[1] + 2;
  83. switch (priv->firmware_type) {
  84. case FIRMWARE_TYPE_SYMBOL:
  85. ie_len += symbol_build_supp_rates(ie, bss->s.rates);
  86. break;
  87. case FIRMWARE_TYPE_INTERSIL:
  88. ie_len += prism_build_supp_rates(ie, bss->p.rates);
  89. break;
  90. case FIRMWARE_TYPE_AGERE:
  91. default:
  92. break;
  93. }
  94. freq = ieee80211_dsss_chan_to_freq(le16_to_cpu(bss->a.channel));
  95. channel = ieee80211_get_channel(wiphy, freq);
  96. if (!channel) {
  97. printk(KERN_DEBUG "Invalid channel designation %04X(%04X)",
  98. bss->a.channel, freq);
  99. return; /* Then ignore it for now */
  100. }
  101. timestamp = 0;
  102. capability = le16_to_cpu(bss->a.capabilities);
  103. beacon_interval = le16_to_cpu(bss->a.beacon_interv);
  104. signal = SIGNAL_TO_MBM(le16_to_cpu(bss->a.level));
  105. cbss = cfg80211_inform_bss(wiphy, channel, bss->a.bssid, timestamp,
  106. capability, beacon_interval, ie_buf, ie_len,
  107. signal, GFP_KERNEL);
  108. cfg80211_put_bss(cbss);
  109. }
  110. void orinoco_add_extscan_result(struct orinoco_private *priv,
  111. struct agere_ext_scan_info *bss,
  112. size_t len)
  113. {
  114. struct wiphy *wiphy = priv_to_wiphy(priv);
  115. struct ieee80211_channel *channel;
  116. struct cfg80211_bss *cbss;
  117. const u8 *ie;
  118. u64 timestamp;
  119. s32 signal;
  120. u16 capability;
  121. u16 beacon_interval;
  122. size_t ie_len;
  123. int chan, freq;
  124. ie_len = len - sizeof(*bss);
  125. ie = cfg80211_find_ie(WLAN_EID_DS_PARAMS, bss->data, ie_len);
  126. chan = ie ? ie[2] : 0;
  127. freq = ieee80211_dsss_chan_to_freq(chan);
  128. channel = ieee80211_get_channel(wiphy, freq);
  129. timestamp = le64_to_cpu(bss->timestamp);
  130. capability = le16_to_cpu(bss->capabilities);
  131. beacon_interval = le16_to_cpu(bss->beacon_interval);
  132. ie = bss->data;
  133. signal = SIGNAL_TO_MBM(bss->level);
  134. cbss = cfg80211_inform_bss(wiphy, channel, bss->bssid, timestamp,
  135. capability, beacon_interval, ie, ie_len,
  136. signal, GFP_KERNEL);
  137. cfg80211_put_bss(cbss);
  138. }
  139. void orinoco_add_hostscan_results(struct orinoco_private *priv,
  140. unsigned char *buf,
  141. size_t len)
  142. {
  143. int offset; /* In the scan data */
  144. size_t atom_len;
  145. bool abort = false;
  146. switch (priv->firmware_type) {
  147. case FIRMWARE_TYPE_AGERE:
  148. atom_len = sizeof(struct agere_scan_apinfo);
  149. offset = 0;
  150. break;
  151. case FIRMWARE_TYPE_SYMBOL:
  152. /* Lack of documentation necessitates this hack.
  153. * Different firmwares have 68 or 76 byte long atoms.
  154. * We try modulo first. If the length divides by both,
  155. * we check what would be the channel in the second
  156. * frame for a 68-byte atom. 76-byte atoms have 0 there.
  157. * Valid channel cannot be 0. */
  158. if (len % 76)
  159. atom_len = 68;
  160. else if (len % 68)
  161. atom_len = 76;
  162. else if (len >= 1292 && buf[68] == 0)
  163. atom_len = 76;
  164. else
  165. atom_len = 68;
  166. offset = 0;
  167. break;
  168. case FIRMWARE_TYPE_INTERSIL:
  169. offset = 4;
  170. if (priv->has_hostscan) {
  171. atom_len = le16_to_cpup((__le16 *)buf);
  172. /* Sanity check for atom_len */
  173. if (atom_len < sizeof(struct prism2_scan_apinfo)) {
  174. printk(KERN_ERR "%s: Invalid atom_len in scan "
  175. "data: %zu\n", priv->ndev->name,
  176. atom_len);
  177. abort = true;
  178. goto scan_abort;
  179. }
  180. } else
  181. atom_len = offsetof(struct prism2_scan_apinfo, atim);
  182. break;
  183. default:
  184. abort = true;
  185. goto scan_abort;
  186. }
  187. /* Check that we got an whole number of atoms */
  188. if ((len - offset) % atom_len) {
  189. printk(KERN_ERR "%s: Unexpected scan data length %zu, "
  190. "atom_len %zu, offset %d\n", priv->ndev->name, len,
  191. atom_len, offset);
  192. abort = true;
  193. goto scan_abort;
  194. }
  195. /* Process the entries one by one */
  196. for (; offset + atom_len <= len; offset += atom_len) {
  197. union hermes_scan_info *atom;
  198. atom = (union hermes_scan_info *) (buf + offset);
  199. orinoco_add_hostscan_result(priv, atom);
  200. }
  201. scan_abort:
  202. if (priv->scan_request) {
  203. cfg80211_scan_done(priv->scan_request, abort);
  204. priv->scan_request = NULL;
  205. }
  206. }
  207. void orinoco_scan_done(struct orinoco_private *priv, bool abort)
  208. {
  209. if (priv->scan_request) {
  210. cfg80211_scan_done(priv->scan_request, abort);
  211. priv->scan_request = NULL;
  212. }
  213. }