Documentation: rename PCI/PCI-DMA-mapping.txt to DMA-API-HOWTO.txt
[nv-tegra-linux-2.6.git] / drivers / staging / batman-adv / device.c
blobe7f44215b5f383834b865b0791737f55e5838991
1 /*
2 * Copyright (C) 2007-2009 B.A.T.M.A.N. contributors:
4 * Marek Lindner
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of version 2 of the GNU General Public
8 * License as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 * 02110-1301, USA
22 #include <linux/device.h>
23 #include "main.h"
24 #include "device.h"
25 #include "send.h"
26 #include "types.h"
27 #include "hash.h"
28 #include "hard-interface.h"
30 static struct class *batman_class;
32 static int Major; /* Major number assigned to our device driver */
34 static const struct file_operations fops = {
35 .open = bat_device_open,
36 .release = bat_device_release,
37 .read = bat_device_read,
38 .write = bat_device_write,
39 .poll = bat_device_poll,
42 static struct device_client *device_client_hash[256];
44 void bat_device_init(void)
46 int i;
48 for (i = 0; i < 256; i++)
49 device_client_hash[i] = NULL;
52 int bat_device_setup(void)
54 int tmp_major;
56 if (Major)
57 return 1;
59 /* register our device - kernel assigns a free major number */
60 tmp_major = register_chrdev(0, DRIVER_DEVICE, &fops);
61 if (tmp_major < 0) {
62 printk(KERN_ERR "batman-adv:Registering the character device failed with %d\n",
63 tmp_major);
64 return 0;
67 batman_class = class_create(THIS_MODULE, "batman-adv");
69 if (IS_ERR(batman_class)) {
70 printk(KERN_ERR "batman-adv:Could not register class 'batman-adv' \n");
71 return 0;
74 device_create(batman_class, NULL, MKDEV(tmp_major, 0), NULL,
75 "batman-adv");
77 Major = tmp_major;
78 return 1;
81 void bat_device_destroy(void)
83 if (!Major)
84 return;
86 device_destroy(batman_class, MKDEV(Major, 0));
87 class_destroy(batman_class);
89 /* Unregister the device */
90 unregister_chrdev(Major, DRIVER_DEVICE);
92 Major = 0;
95 int bat_device_open(struct inode *inode, struct file *file)
97 unsigned int i;
98 struct device_client *device_client;
100 device_client = kmalloc(sizeof(struct device_client), GFP_KERNEL);
102 if (!device_client)
103 return -ENOMEM;
105 for (i = 0; i < 256; i++) {
106 if (!device_client_hash[i]) {
107 device_client_hash[i] = device_client;
108 break;
112 if (device_client_hash[i] != device_client) {
113 printk(KERN_ERR "batman-adv:Error - can't add another packet client: maximum number of clients reached \n");
114 kfree(device_client);
115 return -EXFULL;
118 INIT_LIST_HEAD(&device_client->queue_list);
119 device_client->queue_len = 0;
120 device_client->index = i;
121 spin_lock_init(&device_client->lock);
122 init_waitqueue_head(&device_client->queue_wait);
124 file->private_data = device_client;
126 inc_module_count();
127 return 0;
130 int bat_device_release(struct inode *inode, struct file *file)
132 struct device_client *device_client =
133 (struct device_client *)file->private_data;
134 struct device_packet *device_packet;
135 struct list_head *list_pos, *list_pos_tmp;
136 unsigned long flags;
138 spin_lock_irqsave(&device_client->lock, flags);
140 /* for all packets in the queue ... */
141 list_for_each_safe(list_pos, list_pos_tmp, &device_client->queue_list) {
142 device_packet = list_entry(list_pos,
143 struct device_packet, list);
145 list_del(list_pos);
146 kfree(device_packet);
149 device_client_hash[device_client->index] = NULL;
150 spin_unlock_irqrestore(&device_client->lock, flags);
152 kfree(device_client);
153 dec_module_count();
155 return 0;
158 ssize_t bat_device_read(struct file *file, char __user *buf, size_t count,
159 loff_t *ppos)
161 struct device_client *device_client =
162 (struct device_client *)file->private_data;
163 struct device_packet *device_packet;
164 int error;
165 unsigned long flags;
167 if ((file->f_flags & O_NONBLOCK) && (device_client->queue_len == 0))
168 return -EAGAIN;
170 if ((!buf) || (count < sizeof(struct icmp_packet)))
171 return -EINVAL;
173 if (!access_ok(VERIFY_WRITE, buf, count))
174 return -EFAULT;
176 error = wait_event_interruptible(device_client->queue_wait,
177 device_client->queue_len);
179 if (error)
180 return error;
182 spin_lock_irqsave(&device_client->lock, flags);
184 device_packet = list_first_entry(&device_client->queue_list,
185 struct device_packet, list);
186 list_del(&device_packet->list);
187 device_client->queue_len--;
189 spin_unlock_irqrestore(&device_client->lock, flags);
191 error = __copy_to_user(buf, &device_packet->icmp_packet,
192 sizeof(struct icmp_packet));
194 kfree(device_packet);
196 if (error)
197 return error;
199 return sizeof(struct icmp_packet);
202 ssize_t bat_device_write(struct file *file, const char __user *buff,
203 size_t len, loff_t *off)
205 struct device_client *device_client =
206 (struct device_client *)file->private_data;
207 struct icmp_packet icmp_packet;
208 struct orig_node *orig_node;
209 struct batman_if *batman_if;
210 uint8_t dstaddr[ETH_ALEN];
211 unsigned long flags;
213 if (len < sizeof(struct icmp_packet)) {
214 bat_dbg(DBG_BATMAN, "batman-adv:Error - can't send packet from char device: invalid packet size\n");
215 return -EINVAL;
218 if (!access_ok(VERIFY_READ, buff, sizeof(struct icmp_packet)))
219 return -EFAULT;
221 if (__copy_from_user(&icmp_packet, buff, sizeof(icmp_packet)))
222 return -EFAULT;
224 if (icmp_packet.packet_type != BAT_ICMP) {
225 bat_dbg(DBG_BATMAN, "batman-adv:Error - can't send packet from char device: got bogus packet type (expected: BAT_ICMP)\n");
226 return -EINVAL;
229 if (icmp_packet.msg_type != ECHO_REQUEST) {
230 bat_dbg(DBG_BATMAN, "batman-adv:Error - can't send packet from char device: got bogus message type (expected: ECHO_REQUEST)\n");
231 return -EINVAL;
234 icmp_packet.uid = device_client->index;
236 if (icmp_packet.version != COMPAT_VERSION) {
237 icmp_packet.msg_type = PARAMETER_PROBLEM;
238 icmp_packet.ttl = COMPAT_VERSION;
239 bat_device_add_packet(device_client, &icmp_packet);
240 goto out;
243 if (atomic_read(&module_state) != MODULE_ACTIVE)
244 goto dst_unreach;
246 spin_lock_irqsave(&orig_hash_lock, flags);
247 orig_node = ((struct orig_node *)hash_find(orig_hash, icmp_packet.dst));
249 if (!orig_node)
250 goto unlock;
252 if (!orig_node->router)
253 goto unlock;
255 batman_if = orig_node->batman_if;
256 memcpy(dstaddr, orig_node->router->addr, ETH_ALEN);
258 spin_unlock_irqrestore(&orig_hash_lock, flags);
260 if (!batman_if)
261 goto dst_unreach;
263 if (batman_if->if_active != IF_ACTIVE)
264 goto dst_unreach;
266 memcpy(icmp_packet.orig,
267 batman_if->net_dev->dev_addr,
268 ETH_ALEN);
270 send_raw_packet((unsigned char *)&icmp_packet,
271 sizeof(struct icmp_packet),
272 batman_if, dstaddr);
274 goto out;
276 unlock:
277 spin_unlock_irqrestore(&orig_hash_lock, flags);
278 dst_unreach:
279 icmp_packet.msg_type = DESTINATION_UNREACHABLE;
280 bat_device_add_packet(device_client, &icmp_packet);
281 out:
282 return len;
285 unsigned int bat_device_poll(struct file *file, poll_table *wait)
287 struct device_client *device_client =
288 (struct device_client *)file->private_data;
290 poll_wait(file, &device_client->queue_wait, wait);
292 if (device_client->queue_len > 0)
293 return POLLIN | POLLRDNORM;
295 return 0;
298 void bat_device_add_packet(struct device_client *device_client,
299 struct icmp_packet *icmp_packet)
301 struct device_packet *device_packet;
302 unsigned long flags;
304 device_packet = kmalloc(sizeof(struct device_packet), GFP_KERNEL);
306 if (!device_packet)
307 return;
309 INIT_LIST_HEAD(&device_packet->list);
310 memcpy(&device_packet->icmp_packet, icmp_packet,
311 sizeof(struct icmp_packet));
313 spin_lock_irqsave(&device_client->lock, flags);
315 /* while waiting for the lock the device_client could have been
316 * deleted */
317 if (!device_client_hash[icmp_packet->uid]) {
318 spin_unlock_irqrestore(&device_client->lock, flags);
319 kfree(device_packet);
320 return;
323 list_add_tail(&device_packet->list, &device_client->queue_list);
324 device_client->queue_len++;
326 if (device_client->queue_len > 100) {
327 device_packet = list_first_entry(&device_client->queue_list,
328 struct device_packet, list);
330 list_del(&device_packet->list);
331 kfree(device_packet);
332 device_client->queue_len--;
335 spin_unlock_irqrestore(&device_client->lock, flags);
337 wake_up(&device_client->queue_wait);
340 void bat_device_receive_packet(struct icmp_packet *icmp_packet)
342 struct device_client *hash = device_client_hash[icmp_packet->uid];
344 if (hash)
345 bat_device_add_packet(hash, icmp_packet);