qlink_util.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. * Copyright (c) 2015-2016 Quantenna Communications, Inc.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version 2
  7. * of the License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. */
  15. #include <linux/nl80211.h>
  16. #include "qlink_util.h"
  17. u16 qlink_iface_type_to_nl_mask(u16 qlink_type)
  18. {
  19. u16 result = 0;
  20. switch (qlink_type) {
  21. case QLINK_IFTYPE_AP:
  22. result |= BIT(NL80211_IFTYPE_AP);
  23. break;
  24. case QLINK_IFTYPE_STATION:
  25. result |= BIT(NL80211_IFTYPE_STATION);
  26. break;
  27. case QLINK_IFTYPE_ADHOC:
  28. result |= BIT(NL80211_IFTYPE_ADHOC);
  29. break;
  30. case QLINK_IFTYPE_MONITOR:
  31. result |= BIT(NL80211_IFTYPE_MONITOR);
  32. break;
  33. case QLINK_IFTYPE_WDS:
  34. result |= BIT(NL80211_IFTYPE_WDS);
  35. break;
  36. case QLINK_IFTYPE_AP_VLAN:
  37. result |= BIT(NL80211_IFTYPE_AP_VLAN);
  38. break;
  39. }
  40. return result;
  41. }
  42. u8 qlink_chan_width_mask_to_nl(u16 qlink_mask)
  43. {
  44. u8 result = 0;
  45. if (qlink_mask & BIT(QLINK_CHAN_WIDTH_5))
  46. result |= BIT(NL80211_CHAN_WIDTH_5);
  47. if (qlink_mask & BIT(QLINK_CHAN_WIDTH_10))
  48. result |= BIT(NL80211_CHAN_WIDTH_10);
  49. if (qlink_mask & BIT(QLINK_CHAN_WIDTH_20_NOHT))
  50. result |= BIT(NL80211_CHAN_WIDTH_20_NOHT);
  51. if (qlink_mask & BIT(QLINK_CHAN_WIDTH_20))
  52. result |= BIT(NL80211_CHAN_WIDTH_20);
  53. if (qlink_mask & BIT(QLINK_CHAN_WIDTH_40))
  54. result |= BIT(NL80211_CHAN_WIDTH_40);
  55. if (qlink_mask & BIT(QLINK_CHAN_WIDTH_80))
  56. result |= BIT(NL80211_CHAN_WIDTH_80);
  57. if (qlink_mask & BIT(QLINK_CHAN_WIDTH_80P80))
  58. result |= BIT(NL80211_CHAN_WIDTH_80P80);
  59. if (qlink_mask & BIT(QLINK_CHAN_WIDTH_160))
  60. result |= BIT(NL80211_CHAN_WIDTH_160);
  61. return result;
  62. }
  63. static enum nl80211_chan_width qlink_chanwidth_to_nl(u8 qlw)
  64. {
  65. switch (qlw) {
  66. case QLINK_CHAN_WIDTH_20_NOHT:
  67. return NL80211_CHAN_WIDTH_20_NOHT;
  68. case QLINK_CHAN_WIDTH_20:
  69. return NL80211_CHAN_WIDTH_20;
  70. case QLINK_CHAN_WIDTH_40:
  71. return NL80211_CHAN_WIDTH_40;
  72. case QLINK_CHAN_WIDTH_80:
  73. return NL80211_CHAN_WIDTH_80;
  74. case QLINK_CHAN_WIDTH_80P80:
  75. return NL80211_CHAN_WIDTH_80P80;
  76. case QLINK_CHAN_WIDTH_160:
  77. return NL80211_CHAN_WIDTH_160;
  78. case QLINK_CHAN_WIDTH_5:
  79. return NL80211_CHAN_WIDTH_5;
  80. case QLINK_CHAN_WIDTH_10:
  81. return NL80211_CHAN_WIDTH_10;
  82. default:
  83. return -1;
  84. }
  85. }
  86. static u8 qlink_chanwidth_nl_to_qlink(enum nl80211_chan_width nlwidth)
  87. {
  88. switch (nlwidth) {
  89. case NL80211_CHAN_WIDTH_20_NOHT:
  90. return QLINK_CHAN_WIDTH_20_NOHT;
  91. case NL80211_CHAN_WIDTH_20:
  92. return QLINK_CHAN_WIDTH_20;
  93. case NL80211_CHAN_WIDTH_40:
  94. return QLINK_CHAN_WIDTH_40;
  95. case NL80211_CHAN_WIDTH_80:
  96. return QLINK_CHAN_WIDTH_80;
  97. case NL80211_CHAN_WIDTH_80P80:
  98. return QLINK_CHAN_WIDTH_80P80;
  99. case NL80211_CHAN_WIDTH_160:
  100. return QLINK_CHAN_WIDTH_160;
  101. case NL80211_CHAN_WIDTH_5:
  102. return QLINK_CHAN_WIDTH_5;
  103. case NL80211_CHAN_WIDTH_10:
  104. return QLINK_CHAN_WIDTH_10;
  105. default:
  106. return -1;
  107. }
  108. }
  109. void qlink_chandef_q2cfg(struct wiphy *wiphy,
  110. const struct qlink_chandef *qch,
  111. struct cfg80211_chan_def *chdef)
  112. {
  113. struct ieee80211_channel *chan;
  114. chan = ieee80211_get_channel(wiphy, le16_to_cpu(qch->chan.center_freq));
  115. chdef->chan = chan;
  116. chdef->center_freq1 = le16_to_cpu(qch->center_freq1);
  117. chdef->center_freq2 = le16_to_cpu(qch->center_freq2);
  118. chdef->width = qlink_chanwidth_to_nl(qch->width);
  119. }
  120. void qlink_chandef_cfg2q(const struct cfg80211_chan_def *chdef,
  121. struct qlink_chandef *qch)
  122. {
  123. struct ieee80211_channel *chan = chdef->chan;
  124. qch->chan.hw_value = cpu_to_le16(chan->hw_value);
  125. qch->chan.center_freq = cpu_to_le16(chan->center_freq);
  126. qch->chan.flags = cpu_to_le32(chan->flags);
  127. qch->center_freq1 = cpu_to_le16(chdef->center_freq1);
  128. qch->center_freq2 = cpu_to_le16(chdef->center_freq2);
  129. qch->width = qlink_chanwidth_nl_to_qlink(chdef->width);
  130. }
  131. enum qlink_hidden_ssid qlink_hidden_ssid_nl2q(enum nl80211_hidden_ssid nl_val)
  132. {
  133. switch (nl_val) {
  134. case NL80211_HIDDEN_SSID_ZERO_LEN:
  135. return QLINK_HIDDEN_SSID_ZERO_LEN;
  136. case NL80211_HIDDEN_SSID_ZERO_CONTENTS:
  137. return QLINK_HIDDEN_SSID_ZERO_CONTENTS;
  138. case NL80211_HIDDEN_SSID_NOT_IN_USE:
  139. default:
  140. return QLINK_HIDDEN_SSID_NOT_IN_USE;
  141. }
  142. }
  143. bool qtnf_utils_is_bit_set(const u8 *arr, unsigned int bit,
  144. unsigned int arr_max_len)
  145. {
  146. unsigned int idx = bit / BITS_PER_BYTE;
  147. u8 mask = 1 << (bit - (idx * BITS_PER_BYTE));
  148. if (idx >= arr_max_len)
  149. return false;
  150. return arr[idx] & mask;
  151. }
  152. void qlink_acl_data_cfg2q(const struct cfg80211_acl_data *acl,
  153. struct qlink_acl_data *qacl)
  154. {
  155. switch (acl->acl_policy) {
  156. case NL80211_ACL_POLICY_ACCEPT_UNLESS_LISTED:
  157. qacl->policy =
  158. cpu_to_le32(QLINK_ACL_POLICY_ACCEPT_UNLESS_LISTED);
  159. break;
  160. case NL80211_ACL_POLICY_DENY_UNLESS_LISTED:
  161. qacl->policy = cpu_to_le32(QLINK_ACL_POLICY_DENY_UNLESS_LISTED);
  162. break;
  163. }
  164. qacl->num_entries = cpu_to_le32(acl->n_acl_entries);
  165. memcpy(qacl->mac_addrs, acl->mac_addrs,
  166. acl->n_acl_entries * sizeof(*qacl->mac_addrs));
  167. }