rc80211_pid_debugfs.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /*
  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. #include <linux/sched.h>
  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>
  14. #include <linux/slab.h>
  15. #include <linux/export.h>
  16. #include <net/mac80211.h>
  17. #include "rate.h"
  18. #include "rc80211_pid.h"
  19. static void rate_control_pid_event(struct rc_pid_event_buffer *buf,
  20. enum rc_pid_event_type type,
  21. union rc_pid_event_data *data)
  22. {
  23. struct rc_pid_event *ev;
  24. unsigned long status;
  25. spin_lock_irqsave(&buf->lock, status);
  26. ev = &(buf->ring[buf->next_entry]);
  27. buf->next_entry = (buf->next_entry + 1) % RC_PID_EVENT_RING_SIZE;
  28. ev->timestamp = jiffies;
  29. ev->id = buf->ev_count++;
  30. ev->type = type;
  31. ev->data = *data;
  32. spin_unlock_irqrestore(&buf->lock, status);
  33. wake_up_all(&buf->waitqueue);
  34. }
  35. void rate_control_pid_event_tx_status(struct rc_pid_event_buffer *buf,
  36. struct ieee80211_tx_info *stat)
  37. {
  38. union rc_pid_event_data evd;
  39. evd.flags = stat->flags;
  40. memcpy(&evd.tx_status, stat, sizeof(struct ieee80211_tx_info));
  41. rate_control_pid_event(buf, RC_PID_EVENT_TYPE_TX_STATUS, &evd);
  42. }
  43. void rate_control_pid_event_rate_change(struct rc_pid_event_buffer *buf,
  44. int index, int rate)
  45. {
  46. union rc_pid_event_data evd;
  47. evd.index = index;
  48. evd.rate = rate;
  49. rate_control_pid_event(buf, RC_PID_EVENT_TYPE_RATE_CHANGE, &evd);
  50. }
  51. void rate_control_pid_event_tx_rate(struct rc_pid_event_buffer *buf,
  52. int index, int rate)
  53. {
  54. union rc_pid_event_data evd;
  55. evd.index = index;
  56. evd.rate = rate;
  57. rate_control_pid_event(buf, RC_PID_EVENT_TYPE_TX_RATE, &evd);
  58. }
  59. void rate_control_pid_event_pf_sample(struct rc_pid_event_buffer *buf,
  60. s32 pf_sample, s32 prop_err,
  61. s32 int_err, s32 der_err)
  62. {
  63. union rc_pid_event_data evd;
  64. evd.pf_sample = pf_sample;
  65. evd.prop_err = prop_err;
  66. evd.int_err = int_err;
  67. evd.der_err = der_err;
  68. rate_control_pid_event(buf, RC_PID_EVENT_TYPE_PF_SAMPLE, &evd);
  69. }
  70. static int rate_control_pid_events_open(struct inode *inode, struct file *file)
  71. {
  72. struct rc_pid_sta_info *sinfo = inode->i_private;
  73. struct rc_pid_event_buffer *events = &sinfo->events;
  74. struct rc_pid_events_file_info *file_info;
  75. unsigned long status;
  76. /* Allocate a state struct */
  77. file_info = kmalloc(sizeof(*file_info), GFP_KERNEL);
  78. if (file_info == NULL)
  79. return -ENOMEM;
  80. spin_lock_irqsave(&events->lock, status);
  81. file_info->next_entry = events->next_entry;
  82. file_info->events = events;
  83. spin_unlock_irqrestore(&events->lock, status);
  84. file->private_data = file_info;
  85. return 0;
  86. }
  87. static int rate_control_pid_events_release(struct inode *inode,
  88. struct file *file)
  89. {
  90. struct rc_pid_events_file_info *file_info = file->private_data;
  91. kfree(file_info);
  92. return 0;
  93. }
  94. static unsigned int rate_control_pid_events_poll(struct file *file,
  95. poll_table *wait)
  96. {
  97. struct rc_pid_events_file_info *file_info = file->private_data;
  98. poll_wait(file, &file_info->events->waitqueue, wait);
  99. return POLLIN | POLLRDNORM;
  100. }
  101. #define RC_PID_PRINT_BUF_SIZE 64
  102. static ssize_t rate_control_pid_events_read(struct file *file, char __user *buf,
  103. size_t length, loff_t *offset)
  104. {
  105. struct rc_pid_events_file_info *file_info = file->private_data;
  106. struct rc_pid_event_buffer *events = file_info->events;
  107. struct rc_pid_event *ev;
  108. char pb[RC_PID_PRINT_BUF_SIZE];
  109. int ret;
  110. int p;
  111. unsigned long status;
  112. /* Check if there is something to read. */
  113. if (events->next_entry == file_info->next_entry) {
  114. if (file->f_flags & O_NONBLOCK)
  115. return -EAGAIN;
  116. /* Wait */
  117. ret = wait_event_interruptible(events->waitqueue,
  118. events->next_entry != file_info->next_entry);
  119. if (ret)
  120. return ret;
  121. }
  122. /* Write out one event per call. I don't care whether it's a little
  123. * inefficient, this is debugging code anyway. */
  124. spin_lock_irqsave(&events->lock, status);
  125. /* Get an event */
  126. ev = &(events->ring[file_info->next_entry]);
  127. file_info->next_entry = (file_info->next_entry + 1) %
  128. RC_PID_EVENT_RING_SIZE;
  129. /* Print information about the event. Note that userspace needs to
  130. * provide large enough buffers. */
  131. length = length < RC_PID_PRINT_BUF_SIZE ?
  132. length : RC_PID_PRINT_BUF_SIZE;
  133. p = snprintf(pb, length, "%u %lu ", ev->id, ev->timestamp);
  134. switch (ev->type) {
  135. case RC_PID_EVENT_TYPE_TX_STATUS:
  136. p += snprintf(pb + p, length - p, "tx_status %u %u",
  137. !(ev->data.flags & IEEE80211_TX_STAT_ACK),
  138. ev->data.tx_status.status.rates[0].idx);
  139. break;
  140. case RC_PID_EVENT_TYPE_RATE_CHANGE:
  141. p += snprintf(pb + p, length - p, "rate_change %d %d",
  142. ev->data.index, ev->data.rate);
  143. break;
  144. case RC_PID_EVENT_TYPE_TX_RATE:
  145. p += snprintf(pb + p, length - p, "tx_rate %d %d",
  146. ev->data.index, ev->data.rate);
  147. break;
  148. case RC_PID_EVENT_TYPE_PF_SAMPLE:
  149. p += snprintf(pb + p, length - p,
  150. "pf_sample %d %d %d %d",
  151. ev->data.pf_sample, ev->data.prop_err,
  152. ev->data.int_err, ev->data.der_err);
  153. break;
  154. }
  155. p += snprintf(pb + p, length - p, "\n");
  156. spin_unlock_irqrestore(&events->lock, status);
  157. if (copy_to_user(buf, pb, p))
  158. return -EFAULT;
  159. return p;
  160. }
  161. #undef RC_PID_PRINT_BUF_SIZE
  162. static const struct file_operations rc_pid_fop_events = {
  163. .owner = THIS_MODULE,
  164. .read = rate_control_pid_events_read,
  165. .poll = rate_control_pid_events_poll,
  166. .open = rate_control_pid_events_open,
  167. .release = rate_control_pid_events_release,
  168. .llseek = noop_llseek,
  169. };
  170. void rate_control_pid_add_sta_debugfs(void *priv, void *priv_sta,
  171. struct dentry *dir)
  172. {
  173. struct rc_pid_sta_info *spinfo = priv_sta;
  174. spinfo->events_entry = debugfs_create_file("rc_pid_events", S_IRUGO,
  175. dir, spinfo,
  176. &rc_pid_fop_events);
  177. }
  178. void rate_control_pid_remove_sta_debugfs(void *priv, void *priv_sta)
  179. {
  180. struct rc_pid_sta_info *spinfo = priv_sta;
  181. debugfs_remove(spinfo->events_entry);
  182. }