mm owner: fix race between swapoff and exit
[linux-2.6/next.git] / net / mac80211 / rc80211_pid_debugfs.c
blob8121d3bc683585e22443f7621a90697d73edddbe
1 /*
2 * Copyright 2007, Mattias Nissler <mattias.nissler@gmx.de>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 */
9 #include <linux/spinlock.h>
10 #include <linux/poll.h>
11 #include <linux/netdevice.h>
12 #include <linux/types.h>
13 #include <linux/skbuff.h>
15 #include <net/mac80211.h>
16 #include "rate.h"
18 #include "rc80211_pid.h"
20 static void rate_control_pid_event(struct rc_pid_event_buffer *buf,
21 enum rc_pid_event_type type,
22 union rc_pid_event_data *data)
24 struct rc_pid_event *ev;
25 unsigned long status;
27 spin_lock_irqsave(&buf->lock, status);
28 ev = &(buf->ring[buf->next_entry]);
29 buf->next_entry = (buf->next_entry + 1) % RC_PID_EVENT_RING_SIZE;
31 ev->timestamp = jiffies;
32 ev->id = buf->ev_count++;
33 ev->type = type;
34 ev->data = *data;
36 spin_unlock_irqrestore(&buf->lock, status);
38 wake_up_all(&buf->waitqueue);
41 void rate_control_pid_event_tx_status(struct rc_pid_event_buffer *buf,
42 struct ieee80211_tx_info *stat)
44 union rc_pid_event_data evd;
46 memcpy(&evd.tx_status, stat, sizeof(struct ieee80211_tx_info));
47 rate_control_pid_event(buf, RC_PID_EVENT_TYPE_TX_STATUS, &evd);
50 void rate_control_pid_event_rate_change(struct rc_pid_event_buffer *buf,
51 int index, int rate)
53 union rc_pid_event_data evd;
55 evd.index = index;
56 evd.rate = rate;
57 rate_control_pid_event(buf, RC_PID_EVENT_TYPE_RATE_CHANGE, &evd);
60 void rate_control_pid_event_tx_rate(struct rc_pid_event_buffer *buf,
61 int index, int rate)
63 union rc_pid_event_data evd;
65 evd.index = index;
66 evd.rate = rate;
67 rate_control_pid_event(buf, RC_PID_EVENT_TYPE_TX_RATE, &evd);
70 void rate_control_pid_event_pf_sample(struct rc_pid_event_buffer *buf,
71 s32 pf_sample, s32 prop_err,
72 s32 int_err, s32 der_err)
74 union rc_pid_event_data evd;
76 evd.pf_sample = pf_sample;
77 evd.prop_err = prop_err;
78 evd.int_err = int_err;
79 evd.der_err = der_err;
80 rate_control_pid_event(buf, RC_PID_EVENT_TYPE_PF_SAMPLE, &evd);
83 static int rate_control_pid_events_open(struct inode *inode, struct file *file)
85 struct rc_pid_sta_info *sinfo = inode->i_private;
86 struct rc_pid_event_buffer *events = &sinfo->events;
87 struct rc_pid_events_file_info *file_info;
88 unsigned long status;
90 /* Allocate a state struct */
91 file_info = kmalloc(sizeof(*file_info), GFP_KERNEL);
92 if (file_info == NULL)
93 return -ENOMEM;
95 spin_lock_irqsave(&events->lock, status);
97 file_info->next_entry = events->next_entry;
98 file_info->events = events;
100 spin_unlock_irqrestore(&events->lock, status);
102 file->private_data = file_info;
104 return 0;
107 static int rate_control_pid_events_release(struct inode *inode,
108 struct file *file)
110 struct rc_pid_events_file_info *file_info = file->private_data;
112 kfree(file_info);
114 return 0;
117 static unsigned int rate_control_pid_events_poll(struct file *file,
118 poll_table *wait)
120 struct rc_pid_events_file_info *file_info = file->private_data;
122 poll_wait(file, &file_info->events->waitqueue, wait);
124 return POLLIN | POLLRDNORM;
127 #define RC_PID_PRINT_BUF_SIZE 64
129 static ssize_t rate_control_pid_events_read(struct file *file, char __user *buf,
130 size_t length, loff_t *offset)
132 struct rc_pid_events_file_info *file_info = file->private_data;
133 struct rc_pid_event_buffer *events = file_info->events;
134 struct rc_pid_event *ev;
135 char pb[RC_PID_PRINT_BUF_SIZE];
136 int ret;
137 int p;
138 unsigned long status;
140 /* Check if there is something to read. */
141 if (events->next_entry == file_info->next_entry) {
142 if (file->f_flags & O_NONBLOCK)
143 return -EAGAIN;
145 /* Wait */
146 ret = wait_event_interruptible(events->waitqueue,
147 events->next_entry != file_info->next_entry);
149 if (ret)
150 return ret;
153 /* Write out one event per call. I don't care whether it's a little
154 * inefficient, this is debugging code anyway. */
155 spin_lock_irqsave(&events->lock, status);
157 /* Get an event */
158 ev = &(events->ring[file_info->next_entry]);
159 file_info->next_entry = (file_info->next_entry + 1) %
160 RC_PID_EVENT_RING_SIZE;
162 /* Print information about the event. Note that userpace needs to
163 * provide large enough buffers. */
164 length = length < RC_PID_PRINT_BUF_SIZE ?
165 length : RC_PID_PRINT_BUF_SIZE;
166 p = snprintf(pb, length, "%u %lu ", ev->id, ev->timestamp);
167 switch (ev->type) {
168 case RC_PID_EVENT_TYPE_TX_STATUS:
169 p += snprintf(pb + p, length - p, "tx_status %u %u",
170 ev->data.tx_status.status.excessive_retries,
171 ev->data.tx_status.status.retry_count);
172 break;
173 case RC_PID_EVENT_TYPE_RATE_CHANGE:
174 p += snprintf(pb + p, length - p, "rate_change %d %d",
175 ev->data.index, ev->data.rate);
176 break;
177 case RC_PID_EVENT_TYPE_TX_RATE:
178 p += snprintf(pb + p, length - p, "tx_rate %d %d",
179 ev->data.index, ev->data.rate);
180 break;
181 case RC_PID_EVENT_TYPE_PF_SAMPLE:
182 p += snprintf(pb + p, length - p,
183 "pf_sample %d %d %d %d",
184 ev->data.pf_sample, ev->data.prop_err,
185 ev->data.int_err, ev->data.der_err);
186 break;
188 p += snprintf(pb + p, length - p, "\n");
190 spin_unlock_irqrestore(&events->lock, status);
192 if (copy_to_user(buf, pb, p))
193 return -EFAULT;
195 return p;
198 #undef RC_PID_PRINT_BUF_SIZE
200 static struct file_operations rc_pid_fop_events = {
201 .owner = THIS_MODULE,
202 .read = rate_control_pid_events_read,
203 .poll = rate_control_pid_events_poll,
204 .open = rate_control_pid_events_open,
205 .release = rate_control_pid_events_release,
208 void rate_control_pid_add_sta_debugfs(void *priv, void *priv_sta,
209 struct dentry *dir)
211 struct rc_pid_sta_info *spinfo = priv_sta;
213 spinfo->events_entry = debugfs_create_file("rc_pid_events", S_IRUGO,
214 dir, spinfo,
215 &rc_pid_fop_events);
218 void rate_control_pid_remove_sta_debugfs(void *priv, void *priv_sta)
220 struct rc_pid_sta_info *spinfo = priv_sta;
222 debugfs_remove(spinfo->events_entry);