util.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * Copyright (c) 2015-2016 Quantenna Communications, Inc.
  3. * All rights reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License
  7. * as published by the Free Software Foundation; either version 2
  8. * of the License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. */
  16. #include "util.h"
  17. void qtnf_sta_list_init(struct qtnf_sta_list *list)
  18. {
  19. if (unlikely(!list))
  20. return;
  21. INIT_LIST_HEAD(&list->head);
  22. atomic_set(&list->size, 0);
  23. }
  24. struct qtnf_sta_node *qtnf_sta_list_lookup(struct qtnf_sta_list *list,
  25. const u8 *mac)
  26. {
  27. struct qtnf_sta_node *node;
  28. if (unlikely(!mac))
  29. return NULL;
  30. list_for_each_entry(node, &list->head, list) {
  31. if (ether_addr_equal(node->mac_addr, mac))
  32. return node;
  33. }
  34. return NULL;
  35. }
  36. struct qtnf_sta_node *qtnf_sta_list_lookup_index(struct qtnf_sta_list *list,
  37. size_t index)
  38. {
  39. struct qtnf_sta_node *node;
  40. if (qtnf_sta_list_size(list) <= index)
  41. return NULL;
  42. list_for_each_entry(node, &list->head, list) {
  43. if (index-- == 0)
  44. return node;
  45. }
  46. return NULL;
  47. }
  48. struct qtnf_sta_node *qtnf_sta_list_add(struct qtnf_sta_list *list,
  49. const u8 *mac)
  50. {
  51. struct qtnf_sta_node *node;
  52. if (unlikely(!mac))
  53. return NULL;
  54. node = qtnf_sta_list_lookup(list, mac);
  55. if (node)
  56. goto done;
  57. node = kzalloc(sizeof(*node), GFP_KERNEL);
  58. if (unlikely(!node))
  59. goto done;
  60. ether_addr_copy(node->mac_addr, mac);
  61. list_add_tail(&node->list, &list->head);
  62. atomic_inc(&list->size);
  63. done:
  64. return node;
  65. }
  66. bool qtnf_sta_list_del(struct qtnf_sta_list *list, const u8 *mac)
  67. {
  68. struct qtnf_sta_node *node;
  69. bool ret = false;
  70. node = qtnf_sta_list_lookup(list, mac);
  71. if (node) {
  72. list_del(&node->list);
  73. atomic_dec(&list->size);
  74. kfree(node);
  75. ret = true;
  76. }
  77. return ret;
  78. }
  79. void qtnf_sta_list_free(struct qtnf_sta_list *list)
  80. {
  81. struct qtnf_sta_node *node, *tmp;
  82. atomic_set(&list->size, 0);
  83. list_for_each_entry_safe(node, tmp, &list->head, list) {
  84. list_del(&node->list);
  85. kfree(node);
  86. }
  87. INIT_LIST_HEAD(&list->head);
  88. }