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 17 #include "util.h" 18 19 void qtnf_sta_list_init(struct qtnf_sta_list *list) 20 { 21 if (unlikely(!list)) 22 return; 23 24 INIT_LIST_HEAD(&list->head); 25 atomic_set(&list->size, 0); 26 } 27 28 struct qtnf_sta_node *qtnf_sta_list_lookup(struct qtnf_sta_list *list, 29 const u8 *mac) 30 { 31 struct qtnf_sta_node *node; 32 33 if (unlikely(!mac)) 34 return NULL; 35 36 list_for_each_entry(node, &list->head, list) { 37 if (ether_addr_equal(node->mac_addr, mac)) 38 return node; 39 } 40 41 return NULL; 42 } 43 44 struct qtnf_sta_node *qtnf_sta_list_lookup_index(struct qtnf_sta_list *list, 45 size_t index) 46 { 47 struct qtnf_sta_node *node; 48 49 if (qtnf_sta_list_size(list) <= index) 50 return NULL; 51 52 list_for_each_entry(node, &list->head, list) { 53 if (index-- == 0) 54 return node; 55 } 56 57 return NULL; 58 } 59 60 struct qtnf_sta_node *qtnf_sta_list_add(struct qtnf_sta_list *list, 61 const u8 *mac) 62 { 63 struct qtnf_sta_node *node; 64 65 if (unlikely(!mac)) 66 return NULL; 67 68 node = qtnf_sta_list_lookup(list, mac); 69 70 if (node) 71 goto done; 72 73 node = kzalloc(sizeof(*node), GFP_KERNEL); 74 if (unlikely(!node)) 75 goto done; 76 77 ether_addr_copy(node->mac_addr, mac); 78 list_add_tail(&node->list, &list->head); 79 atomic_inc(&list->size); 80 81 done: 82 return node; 83 } 84 85 bool qtnf_sta_list_del(struct qtnf_sta_list *list, const u8 *mac) 86 { 87 struct qtnf_sta_node *node; 88 bool ret = false; 89 90 node = qtnf_sta_list_lookup(list, mac); 91 92 if (node) { 93 list_del(&node->list); 94 atomic_dec(&list->size); 95 kfree(node); 96 ret = true; 97 } 98 99 return ret; 100 } 101 102 void qtnf_sta_list_free(struct qtnf_sta_list *list) 103 { 104 struct qtnf_sta_node *node, *tmp; 105 106 atomic_set(&list->size, 0); 107 108 list_for_each_entry_safe(node, tmp, &list->head, list) { 109 list_del(&node->list); 110 kfree(node); 111 } 112 113 INIT_LIST_HEAD(&list->head); 114 } 115