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.
9 #include <linux/sched.h>
10 #include <linux/spinlock.h>
11 #include <linux/poll.h>
12 #include <linux/netdevice.h>
13 #include <linux/types.h>
14 #include <linux/skbuff.h>
15 #include <linux/slab.h>
16 #include <linux/export.h>
18 #include <net/mac80211.h>
21 #include "rc80211_pid.h"
23 static 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
)
27 struct rc_pid_event
*ev
;
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
;
34 ev
->timestamp
= jiffies
;
35 ev
->id
= buf
->ev_count
++;
39 spin_unlock_irqrestore(&buf
->lock
, status
);
41 wake_up_all(&buf
->waitqueue
);
44 void rate_control_pid_event_tx_status(struct rc_pid_event_buffer
*buf
,
45 struct ieee80211_tx_info
*stat
)
47 union rc_pid_event_data evd
;
49 evd
.flags
= stat
->flags
;
50 memcpy(&evd
.tx_status
, stat
, sizeof(struct ieee80211_tx_info
));
51 rate_control_pid_event(buf
, RC_PID_EVENT_TYPE_TX_STATUS
, &evd
);
54 void rate_control_pid_event_rate_change(struct rc_pid_event_buffer
*buf
,
57 union rc_pid_event_data evd
;
61 rate_control_pid_event(buf
, RC_PID_EVENT_TYPE_RATE_CHANGE
, &evd
);
64 void rate_control_pid_event_tx_rate(struct rc_pid_event_buffer
*buf
,
67 union rc_pid_event_data evd
;
71 rate_control_pid_event(buf
, RC_PID_EVENT_TYPE_TX_RATE
, &evd
);
74 void 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
)
78 union rc_pid_event_data evd
;
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
);
87 static int rate_control_pid_events_open(struct inode
*inode
, struct file
*file
)
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
;
94 /* Allocate a state struct */
95 file_info
= kmalloc(sizeof(*file_info
), GFP_KERNEL
);
96 if (file_info
== NULL
)
99 spin_lock_irqsave(&events
->lock
, status
);
101 file_info
->next_entry
= events
->next_entry
;
102 file_info
->events
= events
;
104 spin_unlock_irqrestore(&events
->lock
, status
);
106 file
->private_data
= file_info
;
111 static int rate_control_pid_events_release(struct inode
*inode
,
114 struct rc_pid_events_file_info
*file_info
= file
->private_data
;
121 static unsigned int rate_control_pid_events_poll(struct file
*file
,
124 struct rc_pid_events_file_info
*file_info
= file
->private_data
;
126 poll_wait(file
, &file_info
->events
->waitqueue
, wait
);
128 return POLLIN
| POLLRDNORM
;
131 #define RC_PID_PRINT_BUF_SIZE 64
133 static ssize_t
rate_control_pid_events_read(struct file
*file
, char __user
*buf
,
134 size_t length
, loff_t
*offset
)
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
];
142 unsigned long status
;
144 /* Check if there is something to read. */
145 if (events
->next_entry
== file_info
->next_entry
) {
146 if (file
->f_flags
& O_NONBLOCK
)
150 ret
= wait_event_interruptible(events
->waitqueue
,
151 events
->next_entry
!= file_info
->next_entry
);
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
);
162 ev
= &(events
->ring
[file_info
->next_entry
]);
163 file_info
->next_entry
= (file_info
->next_entry
+ 1) %
164 RC_PID_EVENT_RING_SIZE
;
166 /* Print information about the event. Note that userspace needs to
167 * provide large enough buffers. */
168 length
= length
< RC_PID_PRINT_BUF_SIZE
?
169 length
: RC_PID_PRINT_BUF_SIZE
;
170 p
= scnprintf(pb
, length
, "%u %lu ", ev
->id
, ev
->timestamp
);
172 case RC_PID_EVENT_TYPE_TX_STATUS
:
173 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
);
177 case RC_PID_EVENT_TYPE_RATE_CHANGE
:
178 p
+= scnprintf(pb
+ p
, length
- p
, "rate_change %d %d",
179 ev
->data
.index
, ev
->data
.rate
);
181 case RC_PID_EVENT_TYPE_TX_RATE
:
182 p
+= scnprintf(pb
+ p
, length
- p
, "tx_rate %d %d",
183 ev
->data
.index
, ev
->data
.rate
);
185 case RC_PID_EVENT_TYPE_PF_SAMPLE
:
186 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
);
192 p
+= scnprintf(pb
+ p
, length
- p
, "\n");
194 spin_unlock_irqrestore(&events
->lock
, status
);
196 if (copy_to_user(buf
, pb
, p
))
202 #undef RC_PID_PRINT_BUF_SIZE
204 static const struct file_operations rc_pid_fop_events
= {
205 .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
,
210 .llseek
= noop_llseek
,
213 void rate_control_pid_add_sta_debugfs(void *priv
, void *priv_sta
,
216 struct rc_pid_sta_info
*spinfo
= priv_sta
;
218 spinfo
->events_entry
= debugfs_create_file("rc_pid_events", S_IRUGO
,
223 void rate_control_pid_remove_sta_debugfs(void *priv
, void *priv_sta
)
225 struct rc_pid_sta_info
*spinfo
= priv_sta
;
227 debugfs_remove(spinfo
->events_entry
);