blob: 6ff134650a84c195e033c62c76e9c954ac7dc245 [file] [log] [blame]
Mattias Nissler12446c62007-12-19 01:27:18 +01001/*
2 * Copyright 2007, Mattias Nissler <mattias.nissler@gmx.de>
3 *
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 */
8
Alexey Dobriyand43c36d2009-10-07 17:09:06 +04009#include <linux/sched.h>
Mattias Nissler12446c62007-12-19 01:27:18 +010010#include <linux/spinlock.h>
11#include <linux/poll.h>
12#include <linux/netdevice.h>
13#include <linux/types.h>
14#include <linux/skbuff.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090015#include <linux/slab.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040016#include <linux/export.h>
Mattias Nissler12446c62007-12-19 01:27:18 +010017
18#include <net/mac80211.h>
Johannes Berg2c8dccc2008-04-08 15:14:40 -040019#include "rate.h"
Mattias Nissler12446c62007-12-19 01:27:18 +010020
21#include "rc80211_pid.h"
22
23static void rate_control_pid_event(struct rc_pid_event_buffer *buf,
24 enum rc_pid_event_type type,
25 union rc_pid_event_data *data)
26{
27 struct rc_pid_event *ev;
28 unsigned long status;
29
30 spin_lock_irqsave(&buf->lock, status);
31 ev = &(buf->ring[buf->next_entry]);
32 buf->next_entry = (buf->next_entry + 1) % RC_PID_EVENT_RING_SIZE;
33
34 ev->timestamp = jiffies;
35 ev->id = buf->ev_count++;
36 ev->type = type;
37 ev->data = *data;
38
39 spin_unlock_irqrestore(&buf->lock, status);
40
41 wake_up_all(&buf->waitqueue);
42}
43
44void rate_control_pid_event_tx_status(struct rc_pid_event_buffer *buf,
Johannes Berge039fa42008-05-15 12:55:29 +020045 struct ieee80211_tx_info *stat)
Mattias Nissler12446c62007-12-19 01:27:18 +010046{
47 union rc_pid_event_data evd;
48
Johannes Berge6a98542008-10-21 12:40:02 +020049 evd.flags = stat->flags;
Johannes Berge039fa42008-05-15 12:55:29 +020050 memcpy(&evd.tx_status, stat, sizeof(struct ieee80211_tx_info));
Mattias Nissler12446c62007-12-19 01:27:18 +010051 rate_control_pid_event(buf, RC_PID_EVENT_TYPE_TX_STATUS, &evd);
52}
53
54void rate_control_pid_event_rate_change(struct rc_pid_event_buffer *buf,
55 int index, int rate)
56{
57 union rc_pid_event_data evd;
58
59 evd.index = index;
60 evd.rate = rate;
61 rate_control_pid_event(buf, RC_PID_EVENT_TYPE_RATE_CHANGE, &evd);
62}
63
64void rate_control_pid_event_tx_rate(struct rc_pid_event_buffer *buf,
65 int index, int rate)
66{
67 union rc_pid_event_data evd;
68
69 evd.index = index;
70 evd.rate = rate;
71 rate_control_pid_event(buf, RC_PID_EVENT_TYPE_TX_RATE, &evd);
72}
73
74void rate_control_pid_event_pf_sample(struct rc_pid_event_buffer *buf,
75 s32 pf_sample, s32 prop_err,
76 s32 int_err, s32 der_err)
77{
78 union rc_pid_event_data evd;
79
80 evd.pf_sample = pf_sample;
81 evd.prop_err = prop_err;
82 evd.int_err = int_err;
83 evd.der_err = der_err;
84 rate_control_pid_event(buf, RC_PID_EVENT_TYPE_PF_SAMPLE, &evd);
85}
86
87static int rate_control_pid_events_open(struct inode *inode, struct file *file)
88{
89 struct rc_pid_sta_info *sinfo = inode->i_private;
90 struct rc_pid_event_buffer *events = &sinfo->events;
91 struct rc_pid_events_file_info *file_info;
Steven Rostedta1f2aa12008-05-06 20:42:28 -070092 unsigned long status;
Mattias Nissler12446c62007-12-19 01:27:18 +010093
94 /* Allocate a state struct */
95 file_info = kmalloc(sizeof(*file_info), GFP_KERNEL);
96 if (file_info == NULL)
97 return -ENOMEM;
98
99 spin_lock_irqsave(&events->lock, status);
100
101 file_info->next_entry = events->next_entry;
102 file_info->events = events;
103
104 spin_unlock_irqrestore(&events->lock, status);
105
106 file->private_data = file_info;
107
108 return 0;
109}
110
111static int rate_control_pid_events_release(struct inode *inode,
112 struct file *file)
113{
114 struct rc_pid_events_file_info *file_info = file->private_data;
115
116 kfree(file_info);
117
118 return 0;
119}
120
121static unsigned int rate_control_pid_events_poll(struct file *file,
122 poll_table *wait)
123{
124 struct rc_pid_events_file_info *file_info = file->private_data;
125
126 poll_wait(file, &file_info->events->waitqueue, wait);
127
128 return POLLIN | POLLRDNORM;
129}
130
131#define RC_PID_PRINT_BUF_SIZE 64
132
133static ssize_t rate_control_pid_events_read(struct file *file, char __user *buf,
134 size_t length, loff_t *offset)
135{
136 struct rc_pid_events_file_info *file_info = file->private_data;
137 struct rc_pid_event_buffer *events = file_info->events;
138 struct rc_pid_event *ev;
139 char pb[RC_PID_PRINT_BUF_SIZE];
140 int ret;
141 int p;
Steven Rostedta1f2aa12008-05-06 20:42:28 -0700142 unsigned long status;
Mattias Nissler12446c62007-12-19 01:27:18 +0100143
144 /* Check if there is something to read. */
145 if (events->next_entry == file_info->next_entry) {
146 if (file->f_flags & O_NONBLOCK)
147 return -EAGAIN;
148
149 /* Wait */
150 ret = wait_event_interruptible(events->waitqueue,
151 events->next_entry != file_info->next_entry);
152
153 if (ret)
154 return ret;
155 }
156
157 /* Write out one event per call. I don't care whether it's a little
158 * inefficient, this is debugging code anyway. */
159 spin_lock_irqsave(&events->lock, status);
160
161 /* Get an event */
162 ev = &(events->ring[file_info->next_entry]);
163 file_info->next_entry = (file_info->next_entry + 1) %
164 RC_PID_EVENT_RING_SIZE;
165
Andrea Gelmini1fdaa462010-08-05 15:51:35 +0200166 /* Print information about the event. Note that userspace needs to
Mattias Nissler12446c62007-12-19 01:27:18 +0100167 * provide large enough buffers. */
168 length = length < RC_PID_PRINT_BUF_SIZE ?
169 length : RC_PID_PRINT_BUF_SIZE;
Eliad Pellerf364ef92013-08-27 12:40:15 +0300170 p = scnprintf(pb, length, "%u %lu ", ev->id, ev->timestamp);
Mattias Nissler12446c62007-12-19 01:27:18 +0100171 switch (ev->type) {
172 case RC_PID_EVENT_TYPE_TX_STATUS:
Eliad Pellerf364ef92013-08-27 12:40:15 +0300173 p += scnprintf(pb + p, length - p, "tx_status %u %u",
174 !(ev->data.flags & IEEE80211_TX_STAT_ACK),
175 ev->data.tx_status.status.rates[0].idx);
Mattias Nissler12446c62007-12-19 01:27:18 +0100176 break;
177 case RC_PID_EVENT_TYPE_RATE_CHANGE:
Eliad Pellerf364ef92013-08-27 12:40:15 +0300178 p += scnprintf(pb + p, length - p, "rate_change %d %d",
179 ev->data.index, ev->data.rate);
Mattias Nissler12446c62007-12-19 01:27:18 +0100180 break;
181 case RC_PID_EVENT_TYPE_TX_RATE:
Eliad Pellerf364ef92013-08-27 12:40:15 +0300182 p += scnprintf(pb + p, length - p, "tx_rate %d %d",
183 ev->data.index, ev->data.rate);
Mattias Nissler12446c62007-12-19 01:27:18 +0100184 break;
185 case RC_PID_EVENT_TYPE_PF_SAMPLE:
Eliad Pellerf364ef92013-08-27 12:40:15 +0300186 p += scnprintf(pb + p, length - p,
187 "pf_sample %d %d %d %d",
188 ev->data.pf_sample, ev->data.prop_err,
189 ev->data.int_err, ev->data.der_err);
Mattias Nissler12446c62007-12-19 01:27:18 +0100190 break;
191 }
Eliad Pellerf364ef92013-08-27 12:40:15 +0300192 p += scnprintf(pb + p, length - p, "\n");
Mattias Nissler12446c62007-12-19 01:27:18 +0100193
194 spin_unlock_irqrestore(&events->lock, status);
195
196 if (copy_to_user(buf, pb, p))
197 return -EFAULT;
198
199 return p;
200}
201
202#undef RC_PID_PRINT_BUF_SIZE
203
Stephen Hemminger5ca1b992009-09-01 19:25:05 +0000204static const struct file_operations rc_pid_fop_events = {
Mattias Nissler12446c62007-12-19 01:27:18 +0100205 .owner = THIS_MODULE,
206 .read = rate_control_pid_events_read,
207 .poll = rate_control_pid_events_poll,
208 .open = rate_control_pid_events_open,
209 .release = rate_control_pid_events_release,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200210 .llseek = noop_llseek,
Mattias Nissler12446c62007-12-19 01:27:18 +0100211};
212
213void rate_control_pid_add_sta_debugfs(void *priv, void *priv_sta,
214 struct dentry *dir)
215{
216 struct rc_pid_sta_info *spinfo = priv_sta;
217
218 spinfo->events_entry = debugfs_create_file("rc_pid_events", S_IRUGO,
219 dir, spinfo,
220 &rc_pid_fop_events);
221}
222
223void rate_control_pid_remove_sta_debugfs(void *priv, void *priv_sta)
224{
225 struct rc_pid_sta_info *spinfo = priv_sta;
226
227 debugfs_remove(spinfo->events_entry);
228}