rc80211_pid_debugfs.c 5.8 KB

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