scan.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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_channel_to_frequency(
  95. le16_to_cpu(bss->a.channel), IEEE80211_BAND_2GHZ);
  96. channel = ieee80211_get_channel(wiphy, freq);
  97. if (!channel) {
  98. printk(KERN_DEBUG "Invalid channel designation %04X(%04X)",
  99. bss->a.channel, freq);
  100. return; /* Then ignore it for now */
  101. }
  102. timestamp = 0;
  103. capability = le16_to_cpu(bss->a.capabilities);
  104. beacon_interval = le16_to_cpu(bss->a.beacon_interv);
  105. signal = SIGNAL_TO_MBM(le16_to_cpu(bss->a.level));
  106. cbss = cfg80211_inform_bss(wiphy, channel, CFG80211_BSS_FTYPE_UNKNOWN,
  107. bss->a.bssid, timestamp, capability,
  108. beacon_interval, ie_buf, ie_len, signal,
  109. GFP_KERNEL);
  110. cfg80211_put_bss(wiphy, cbss);
  111. }
  112. void orinoco_add_extscan_result(struct orinoco_private *priv,
  113. struct agere_ext_scan_info *bss,
  114. size_t len)
  115. {
  116. struct wiphy *wiphy = priv_to_wiphy(priv);
  117. struct ieee80211_channel *channel;
  118. struct cfg80211_bss *cbss;
  119. const u8 *ie;
  120. u64 timestamp;
  121. s32 signal;
  122. u16 capability;
  123. u16 beacon_interval;
  124. size_t ie_len;
  125. int chan, freq;
  126. ie_len = len - sizeof(*bss);
  127. ie = cfg80211_find_ie(WLAN_EID_DS_PARAMS, bss->data, ie_len);
  128. chan = ie ? ie[2] : 0;
  129. freq = ieee80211_channel_to_frequency(chan, IEEE80211_BAND_2GHZ);
  130. channel = ieee80211_get_channel(wiphy, freq);
  131. timestamp = le64_to_cpu(bss->timestamp);
  132. capability = le16_to_cpu(bss->capabilities);
  133. beacon_interval = le16_to_cpu(bss->beacon_interval);
  134. ie = bss->data;
  135. signal = SIGNAL_TO_MBM(bss->level);
  136. cbss = cfg80211_inform_bss(wiphy, channel, CFG80211_BSS_FTYPE_UNKNOWN,
  137. bss->bssid, timestamp, capability,
  138. beacon_interval, ie, ie_len, signal,
  139. GFP_KERNEL);
  140. cfg80211_put_bss(wiphy, cbss);
  141. }
  142. void orinoco_add_hostscan_results(struct orinoco_private *priv,
  143. unsigned char *buf,
  144. size_t len)
  145. {
  146. int offset; /* In the scan data */
  147. size_t atom_len;
  148. bool abort = false;
  149. switch (priv->firmware_type) {
  150. case FIRMWARE_TYPE_AGERE:
  151. atom_len = sizeof(struct agere_scan_apinfo);
  152. offset = 0;
  153. break;
  154. case FIRMWARE_TYPE_SYMBOL:
  155. /* Lack of documentation necessitates this hack.
  156. * Different firmwares have 68 or 76 byte long atoms.
  157. * We try modulo first. If the length divides by both,
  158. * we check what would be the channel in the second
  159. * frame for a 68-byte atom. 76-byte atoms have 0 there.
  160. * Valid channel cannot be 0. */
  161. if (len % 76)
  162. atom_len = 68;
  163. else if (len % 68)
  164. atom_len = 76;
  165. else if (len >= 1292 && buf[68] == 0)
  166. atom_len = 76;
  167. else
  168. atom_len = 68;
  169. offset = 0;
  170. break;
  171. case FIRMWARE_TYPE_INTERSIL:
  172. offset = 4;
  173. if (priv->has_hostscan) {
  174. atom_len = le16_to_cpup((__le16 *)buf);
  175. /* Sanity check for atom_len */
  176. if (atom_len < sizeof(struct prism2_scan_apinfo)) {
  177. printk(KERN_ERR "%s: Invalid atom_len in scan "
  178. "data: %zu\n", priv->ndev->name,
  179. atom_len);
  180. abort = true;
  181. goto scan_abort;
  182. }
  183. } else
  184. atom_len = offsetof(struct prism2_scan_apinfo, atim);
  185. break;
  186. default:
  187. abort = true;
  188. goto scan_abort;
  189. }
  190. /* Check that we got an whole number of atoms */
  191. if ((len - offset) % atom_len) {
  192. printk(KERN_ERR "%s: Unexpected scan data length %zu, "
  193. "atom_len %zu, offset %d\n", priv->ndev->name, len,
  194. atom_len, offset);
  195. abort = true;
  196. goto scan_abort;
  197. }
  198. /* Process the entries one by one */
  199. for (; offset + atom_len <= len; offset += atom_len) {
  200. union hermes_scan_info *atom;
  201. atom = (union hermes_scan_info *) (buf + offset);
  202. orinoco_add_hostscan_result(priv, atom);
  203. }
  204. scan_abort:
  205. if (priv->scan_request) {
  206. cfg80211_scan_done(priv->scan_request, abort);
  207. priv->scan_request = NULL;
  208. }
  209. }
  210. void orinoco_scan_done(struct orinoco_private *priv, bool abort)
  211. {
  212. if (priv->scan_request) {
  213. cfg80211_scan_done(priv->scan_request, abort);
  214. priv->scan_request = NULL;
  215. }
  216. }