iwl-phy-db.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /******************************************************************************
  2. *
  3. * This file is provided under a dual BSD/GPLv2 license. When using or
  4. * redistributing this file, you may do so under either license.
  5. *
  6. * GPL LICENSE SUMMARY
  7. *
  8. * Copyright(c) 2007 - 2012 Intel Corporation. All rights reserved.
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of version 2 of the GNU General Public License as
  12. * published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
  22. * USA
  23. *
  24. * The full GNU General Public License is included in this distribution
  25. * in the file called LICENSE.GPL.
  26. *
  27. * Contact Information:
  28. * Intel Linux Wireless <ilw@linux.intel.com>
  29. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  30. *
  31. * BSD LICENSE
  32. *
  33. * Copyright(c) 2005 - 2012 Intel Corporation. All rights reserved.
  34. * All rights reserved.
  35. *
  36. * Redistribution and use in source and binary forms, with or without
  37. * modification, are permitted provided that the following conditions
  38. * are met:
  39. *
  40. * * Redistributions of source code must retain the above copyright
  41. * notice, this list of conditions and the following disclaimer.
  42. * * Redistributions in binary form must reproduce the above copyright
  43. * notice, this list of conditions and the following disclaimer in
  44. * the documentation and/or other materials provided with the
  45. * distribution.
  46. * * Neither the name Intel Corporation nor the names of its
  47. * contributors may be used to endorse or promote products derived
  48. * from this software without specific prior written permission.
  49. *
  50. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  51. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  52. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  53. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  54. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  55. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  56. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  57. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  58. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  59. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  60. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  61. *
  62. *****************************************************************************/
  63. #include <linux/slab.h>
  64. #include <linux/string.h>
  65. #include "iwl-debug.h"
  66. #include "iwl-dev.h"
  67. #include "iwl-phy-db.h"
  68. #define CHANNEL_NUM_SIZE 4 /* num of channels in calib_ch size */
  69. struct iwl_phy_db *iwl_phy_db_init(struct device *dev)
  70. {
  71. struct iwl_phy_db *phy_db = kzalloc(sizeof(struct iwl_phy_db),
  72. GFP_KERNEL);
  73. if (!phy_db)
  74. return phy_db;
  75. phy_db->dev = dev;
  76. /* TODO: add default values of the phy db. */
  77. return phy_db;
  78. }
  79. /*
  80. * get phy db section: returns a pointer to a phy db section specified by
  81. * type and channel group id.
  82. */
  83. static struct iwl_phy_db_entry *
  84. iwl_phy_db_get_section(struct iwl_phy_db *phy_db,
  85. enum iwl_phy_db_section_type type,
  86. u16 chg_id)
  87. {
  88. if (!phy_db || type < 0 || type >= IWL_PHY_DB_MAX)
  89. return NULL;
  90. switch (type) {
  91. case IWL_PHY_DB_CFG:
  92. return &phy_db->cfg;
  93. case IWL_PHY_DB_CALIB_NCH:
  94. return &phy_db->calib_nch;
  95. case IWL_PHY_DB_CALIB_CH:
  96. return &phy_db->calib_ch;
  97. case IWL_PHY_DB_CALIB_CHG_PAPD:
  98. if (chg_id < 0 || chg_id >= IWL_NUM_PAPD_CH_GROUPS)
  99. return NULL;
  100. return &phy_db->calib_ch_group_papd[chg_id];
  101. case IWL_PHY_DB_CALIB_CHG_TXP:
  102. if (chg_id < 0 || chg_id >= IWL_NUM_TXP_CH_GROUPS)
  103. return NULL;
  104. return &phy_db->calib_ch_group_txp[chg_id];
  105. default:
  106. return NULL;
  107. }
  108. return NULL;
  109. }
  110. static void iwl_phy_db_free_section(struct iwl_phy_db *phy_db,
  111. enum iwl_phy_db_section_type type,
  112. u16 chg_id)
  113. {
  114. struct iwl_phy_db_entry *entry =
  115. iwl_phy_db_get_section(phy_db, type, chg_id);
  116. if (!entry)
  117. return;
  118. kfree(entry->data);
  119. entry->data = NULL;
  120. entry->size = 0;
  121. }
  122. void iwl_phy_db_free(struct iwl_phy_db *phy_db)
  123. {
  124. int i;
  125. if (!phy_db)
  126. return;
  127. iwl_phy_db_free_section(phy_db, IWL_PHY_DB_CFG, 0);
  128. iwl_phy_db_free_section(phy_db, IWL_PHY_DB_CALIB_NCH, 0);
  129. iwl_phy_db_free_section(phy_db, IWL_PHY_DB_CALIB_CH, 0);
  130. for (i = 0; i < IWL_NUM_PAPD_CH_GROUPS; i++)
  131. iwl_phy_db_free_section(phy_db, IWL_PHY_DB_CALIB_CHG_PAPD, i);
  132. for (i = 0; i < IWL_NUM_TXP_CH_GROUPS; i++)
  133. iwl_phy_db_free_section(phy_db, IWL_PHY_DB_CALIB_CHG_TXP, i);
  134. kfree(phy_db);
  135. }
  136. int iwl_phy_db_set_section(struct iwl_phy_db *phy_db,
  137. enum iwl_phy_db_section_type type, u8 *data,
  138. u16 size, gfp_t alloc_ctx)
  139. {
  140. struct iwl_phy_db_entry *entry;
  141. u16 chg_id = 0;
  142. if (!phy_db)
  143. return -EINVAL;
  144. if (type == IWL_PHY_DB_CALIB_CHG_PAPD ||
  145. type == IWL_PHY_DB_CALIB_CHG_TXP)
  146. chg_id = le16_to_cpup((__le16 *)data);
  147. entry = iwl_phy_db_get_section(phy_db, type, chg_id);
  148. if (!entry)
  149. return -EINVAL;
  150. kfree(entry->data);
  151. entry->data = kmemdup(data, size, alloc_ctx);
  152. if (!entry->data) {
  153. entry->size = 0;
  154. return -ENOMEM;
  155. }
  156. entry->size = size;
  157. if (type == IWL_PHY_DB_CALIB_CH) {
  158. phy_db->channel_num = le32_to_cpup((__le32 *)data);
  159. phy_db->channel_size =
  160. (size - CHANNEL_NUM_SIZE) / phy_db->channel_num;
  161. }
  162. return 0;
  163. }
  164. static int is_valid_channel(u16 ch_id)
  165. {
  166. if (ch_id <= 14 ||
  167. (36 <= ch_id && ch_id <= 64 && ch_id % 4 == 0) ||
  168. (100 <= ch_id && ch_id <= 140 && ch_id % 4 == 0) ||
  169. (145 <= ch_id && ch_id <= 165 && ch_id % 4 == 1))
  170. return 1;
  171. return 0;
  172. }
  173. static u8 ch_id_to_ch_index(u16 ch_id)
  174. {
  175. if (WARN_ON(!is_valid_channel(ch_id)))
  176. return 0xff;
  177. if (ch_id <= 14)
  178. return ch_id - 1;
  179. if (ch_id <= 64)
  180. return (ch_id + 20) / 4;
  181. if (ch_id <= 140)
  182. return (ch_id - 12) / 4;
  183. return (ch_id - 13) / 4;
  184. }
  185. static u16 channel_id_to_papd(u16 ch_id)
  186. {
  187. if (WARN_ON(!is_valid_channel(ch_id)))
  188. return 0xff;
  189. if (1 <= ch_id && ch_id <= 14)
  190. return 0;
  191. if (36 <= ch_id && ch_id <= 64)
  192. return 1;
  193. if (100 <= ch_id && ch_id <= 140)
  194. return 2;
  195. return 3;
  196. }
  197. static u16 channel_id_to_txp(struct iwl_phy_db *phy_db, u16 ch_id)
  198. {
  199. struct iwl_phy_db_chg_txp *txp_chg;
  200. int i;
  201. u8 ch_index = ch_id_to_ch_index(ch_id);
  202. if (ch_index == 0xff)
  203. return 0xff;
  204. for (i = 0; i < IWL_NUM_TXP_CH_GROUPS; i++) {
  205. txp_chg = (void *)phy_db->calib_ch_group_txp[i].data;
  206. if (!txp_chg)
  207. return 0xff;
  208. /*
  209. * Looking for the first channel group that its max channel is
  210. * higher then wanted channel.
  211. */
  212. if (le16_to_cpu(txp_chg->max_channel_idx) >= ch_index)
  213. return i;
  214. }
  215. return 0xff;
  216. }
  217. int iwl_phy_db_get_section_data(struct iwl_phy_db *phy_db,
  218. enum iwl_phy_db_section_type type, u8 **data,
  219. u16 *size, u16 ch_id)
  220. {
  221. struct iwl_phy_db_entry *entry;
  222. u32 channel_num;
  223. u32 channel_size;
  224. u16 ch_group_id = 0;
  225. u16 index;
  226. if (!phy_db)
  227. return -EINVAL;
  228. /* find wanted channel group */
  229. if (type == IWL_PHY_DB_CALIB_CHG_PAPD)
  230. ch_group_id = channel_id_to_papd(ch_id);
  231. else if (type == IWL_PHY_DB_CALIB_CHG_TXP)
  232. ch_group_id = channel_id_to_txp(phy_db, ch_id);
  233. entry = iwl_phy_db_get_section(phy_db, type, ch_group_id);
  234. if (!entry)
  235. return -EINVAL;
  236. if (type == IWL_PHY_DB_CALIB_CH) {
  237. index = ch_id_to_ch_index(ch_id);
  238. channel_num = phy_db->channel_num;
  239. channel_size = phy_db->channel_size;
  240. if (index >= channel_num) {
  241. IWL_ERR(phy_db, "Wrong channel number %d", ch_id);
  242. return -EINVAL;
  243. }
  244. *data = entry->data + CHANNEL_NUM_SIZE + index * channel_size;
  245. *size = channel_size;
  246. } else {
  247. *data = entry->data;
  248. *size = entry->size;
  249. }
  250. return 0;
  251. }