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/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>
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
;
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
++;
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_status
*stat
)
44 union rc_pid_event_data evd
;
46 memcpy(&evd
.tx_status
, stat
, sizeof(struct ieee80211_tx_status
));
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
,
53 union rc_pid_event_data evd
;
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
,
63 union rc_pid_event_data evd
;
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
;
90 /* Allocate a state struct */
91 file_info
= kmalloc(sizeof(*file_info
), GFP_KERNEL
);
92 if (file_info
== NULL
)
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
;
107 static int rate_control_pid_events_release(struct inode
*inode
,
110 struct rc_pid_events_file_info
*file_info
= file
->private_data
;
117 static unsigned int rate_control_pid_events_poll(struct file
*file
,
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
];
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
)
146 ret
= wait_event_interruptible(events
->waitqueue
,
147 events
->next_entry
!= file_info
->next_entry
);
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
);
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
);
168 case RC_PID_EVENT_TYPE_TX_STATUS
:
169 p
+= snprintf(pb
+ p
, length
- p
, "tx_status %u %u",
170 ev
->data
.tx_status
.excessive_retries
,
171 ev
->data
.tx_status
.retry_count
);
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
);
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
);
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
);
188 p
+= snprintf(pb
+ p
, length
- p
, "\n");
190 spin_unlock_irqrestore(&events
->lock
, status
);
192 if (copy_to_user(buf
, pb
, 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
,
211 struct rc_pid_sta_info
*spinfo
= priv_sta
;
213 spinfo
->events_entry
= debugfs_create_file("rc_pid_events", S_IRUGO
,
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
);