3 * Mappping of generic netlink family IDs to net devices
6 * Copyright (C) 2005-2006 Intel Corporation <linux-wimax@intel.com>
7 * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License version
11 * 2 as published by the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
24 * We assign a single generic netlink family ID to each device (to
27 * We need a way to map family ID to a wimax_dev pointer.
29 * The idea is to use a very simple lookup. Using a netlink attribute
30 * with (for example) the interface name implies a heavier search over
31 * all the network devices; seemed kind of a waste given that we know
32 * we are looking for a WiMAX device and that most systems will have
33 * just a single WiMAX adapter.
35 * We put all the WiMAX devices in the system in a linked list and
36 * match the generic link family ID against the list.
38 * By using a linked list, the case of a single adapter in the system
39 * becomes (almost) no overhead, while still working for many more. If
40 * it ever goes beyond two, I'll be surprised.
42 #include <linux/device.h>
43 #include <net/genetlink.h>
44 #include <linux/netdevice.h>
45 #include <linux/list.h>
46 #include <linux/wimax.h>
47 #include "wimax-internal.h"
50 #define D_SUBMODULE id_table
51 #include "debug-levels.h"
54 static DEFINE_SPINLOCK(wimax_id_table_lock);
55 static struct list_head wimax_id_table = LIST_HEAD_INIT(wimax_id_table);
59 * wimax_id_table_add - add a gennetlink familiy ID / wimax_dev mapping
61 * @wimax_dev: WiMAX device descriptor to associate to the Generic
64 * Look for an empty spot in the ID table; if none found, double the
65 * table's size and get the first spot.
67 void wimax_id_table_add(struct wimax_dev *wimax_dev)
69 d_fnstart(3, NULL, "(wimax_dev %p)\n", wimax_dev);
70 spin_lock(&wimax_id_table_lock);
71 list_add(&wimax_dev->id_table_node, &wimax_id_table);
72 spin_unlock(&wimax_id_table_lock);
73 d_fnend(3, NULL, "(wimax_dev %p)\n", wimax_dev);
78 * wimax_get_netdev_by_info - lookup a wimax_dev from the gennetlink info
80 * The generic netlink family ID has been filled out in the
81 * nlmsghdr->nlmsg_type field, so we pull it from there, look it up in
82 * the mapping table and reference the wimax_dev.
84 * When done, the reference should be dropped with
85 * 'dev_put(wimax_dev->net_dev)'.
87 struct wimax_dev *wimax_dev_get_by_genl_info(
88 struct genl_info *info, int ifindex)
90 struct wimax_dev *wimax_dev = NULL;
92 d_fnstart(3, NULL, "(info %p ifindex %d)\n", info, ifindex);
93 spin_lock(&wimax_id_table_lock);
94 list_for_each_entry(wimax_dev, &wimax_id_table, id_table_node) {
95 if (wimax_dev->net_dev->ifindex == ifindex) {
96 dev_hold(wimax_dev->net_dev);
100 if (wimax_dev == NULL)
101 d_printf(1, NULL, "wimax: no devices found with ifindex %d\n",
103 spin_unlock(&wimax_id_table_lock);
104 d_fnend(3, NULL, "(info %p ifindex %d) = %p\n",
105 info, ifindex, wimax_dev);
111 * wimax_id_table_rm - Remove a gennetlink familiy ID / wimax_dev mapping
113 * @id: family ID to remove from the table
115 void wimax_id_table_rm(struct wimax_dev *wimax_dev)
117 spin_lock(&wimax_id_table_lock);
118 list_del_init(&wimax_dev->id_table_node);
119 spin_unlock(&wimax_id_table_lock);
124 * Release the gennetlink family id / mapping table
126 * On debug, verify that the table is empty upon removal. We want the
127 * code always compiled, to ensure it doesn't bit rot. It will be
128 * compiled out if CONFIG_BUG is disabled.
130 void wimax_id_table_release(void)
132 struct wimax_dev *wimax_dev;
137 spin_lock(&wimax_id_table_lock);
138 list_for_each_entry(wimax_dev, &wimax_id_table, id_table_node) {
139 printk(KERN_ERR "BUG: %s wimax_dev %p ifindex %d not cleared\n",
140 __func__, wimax_dev, wimax_dev->net_dev->ifindex);
143 spin_unlock(&wimax_id_table_lock);