debugfs.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. /*
  2. * Intel Wireless WiMAX Connection 2400m
  3. * Debugfs interfaces to manipulate driver and device information
  4. *
  5. *
  6. * Copyright (C) 2007 Intel Corporation <linux-wimax@intel.com>
  7. * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License version
  11. * 2 as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  21. * 02110-1301, USA.
  22. */
  23. #include <linux/debugfs.h>
  24. #include <linux/netdevice.h>
  25. #include <linux/etherdevice.h>
  26. #include <linux/spinlock.h>
  27. #include <linux/device.h>
  28. #include <linux/export.h>
  29. #include "i2400m.h"
  30. #define D_SUBMODULE debugfs
  31. #include "debug-levels.h"
  32. static
  33. int debugfs_netdev_queue_stopped_get(void *data, u64 *val)
  34. {
  35. struct i2400m *i2400m = data;
  36. *val = netif_queue_stopped(i2400m->wimax_dev.net_dev);
  37. return 0;
  38. }
  39. DEFINE_SIMPLE_ATTRIBUTE(fops_netdev_queue_stopped,
  40. debugfs_netdev_queue_stopped_get,
  41. NULL, "%llu\n");
  42. static
  43. struct dentry *debugfs_create_netdev_queue_stopped(
  44. const char *name, struct dentry *parent, struct i2400m *i2400m)
  45. {
  46. return debugfs_create_file(name, 0400, parent, i2400m,
  47. &fops_netdev_queue_stopped);
  48. }
  49. /*
  50. * inode->i_private has the @data argument to debugfs_create_file()
  51. */
  52. static
  53. int i2400m_stats_open(struct inode *inode, struct file *filp)
  54. {
  55. filp->private_data = inode->i_private;
  56. return 0;
  57. }
  58. /*
  59. * We don't allow partial reads of this file, as then the reader would
  60. * get weirdly confused data as it is updated.
  61. *
  62. * So or you read it all or nothing; if you try to read with an offset
  63. * != 0, we consider you are done reading.
  64. */
  65. static
  66. ssize_t i2400m_rx_stats_read(struct file *filp, char __user *buffer,
  67. size_t count, loff_t *ppos)
  68. {
  69. struct i2400m *i2400m = filp->private_data;
  70. char buf[128];
  71. unsigned long flags;
  72. if (*ppos != 0)
  73. return 0;
  74. if (count < sizeof(buf))
  75. return -ENOSPC;
  76. spin_lock_irqsave(&i2400m->rx_lock, flags);
  77. snprintf(buf, sizeof(buf), "%u %u %u %u %u %u %u\n",
  78. i2400m->rx_pl_num, i2400m->rx_pl_min,
  79. i2400m->rx_pl_max, i2400m->rx_num,
  80. i2400m->rx_size_acc,
  81. i2400m->rx_size_min, i2400m->rx_size_max);
  82. spin_unlock_irqrestore(&i2400m->rx_lock, flags);
  83. return simple_read_from_buffer(buffer, count, ppos, buf, strlen(buf));
  84. }
  85. /* Any write clears the stats */
  86. static
  87. ssize_t i2400m_rx_stats_write(struct file *filp, const char __user *buffer,
  88. size_t count, loff_t *ppos)
  89. {
  90. struct i2400m *i2400m = filp->private_data;
  91. unsigned long flags;
  92. spin_lock_irqsave(&i2400m->rx_lock, flags);
  93. i2400m->rx_pl_num = 0;
  94. i2400m->rx_pl_max = 0;
  95. i2400m->rx_pl_min = UINT_MAX;
  96. i2400m->rx_num = 0;
  97. i2400m->rx_size_acc = 0;
  98. i2400m->rx_size_min = UINT_MAX;
  99. i2400m->rx_size_max = 0;
  100. spin_unlock_irqrestore(&i2400m->rx_lock, flags);
  101. return count;
  102. }
  103. static
  104. const struct file_operations i2400m_rx_stats_fops = {
  105. .owner = THIS_MODULE,
  106. .open = i2400m_stats_open,
  107. .read = i2400m_rx_stats_read,
  108. .write = i2400m_rx_stats_write,
  109. .llseek = default_llseek,
  110. };
  111. /* See i2400m_rx_stats_read() */
  112. static
  113. ssize_t i2400m_tx_stats_read(struct file *filp, char __user *buffer,
  114. size_t count, loff_t *ppos)
  115. {
  116. struct i2400m *i2400m = filp->private_data;
  117. char buf[128];
  118. unsigned long flags;
  119. if (*ppos != 0)
  120. return 0;
  121. if (count < sizeof(buf))
  122. return -ENOSPC;
  123. spin_lock_irqsave(&i2400m->tx_lock, flags);
  124. snprintf(buf, sizeof(buf), "%u %u %u %u %u %u %u\n",
  125. i2400m->tx_pl_num, i2400m->tx_pl_min,
  126. i2400m->tx_pl_max, i2400m->tx_num,
  127. i2400m->tx_size_acc,
  128. i2400m->tx_size_min, i2400m->tx_size_max);
  129. spin_unlock_irqrestore(&i2400m->tx_lock, flags);
  130. return simple_read_from_buffer(buffer, count, ppos, buf, strlen(buf));
  131. }
  132. /* Any write clears the stats */
  133. static
  134. ssize_t i2400m_tx_stats_write(struct file *filp, const char __user *buffer,
  135. size_t count, loff_t *ppos)
  136. {
  137. struct i2400m *i2400m = filp->private_data;
  138. unsigned long flags;
  139. spin_lock_irqsave(&i2400m->tx_lock, flags);
  140. i2400m->tx_pl_num = 0;
  141. i2400m->tx_pl_max = 0;
  142. i2400m->tx_pl_min = UINT_MAX;
  143. i2400m->tx_num = 0;
  144. i2400m->tx_size_acc = 0;
  145. i2400m->tx_size_min = UINT_MAX;
  146. i2400m->tx_size_max = 0;
  147. spin_unlock_irqrestore(&i2400m->tx_lock, flags);
  148. return count;
  149. }
  150. static
  151. const struct file_operations i2400m_tx_stats_fops = {
  152. .owner = THIS_MODULE,
  153. .open = i2400m_stats_open,
  154. .read = i2400m_tx_stats_read,
  155. .write = i2400m_tx_stats_write,
  156. .llseek = default_llseek,
  157. };
  158. /* Write 1 to ask the device to go into suspend */
  159. static
  160. int debugfs_i2400m_suspend_set(void *data, u64 val)
  161. {
  162. int result;
  163. struct i2400m *i2400m = data;
  164. result = i2400m_cmd_enter_powersave(i2400m);
  165. if (result >= 0)
  166. result = 0;
  167. return result;
  168. }
  169. DEFINE_SIMPLE_ATTRIBUTE(fops_i2400m_suspend,
  170. NULL, debugfs_i2400m_suspend_set,
  171. "%llu\n");
  172. static
  173. struct dentry *debugfs_create_i2400m_suspend(
  174. const char *name, struct dentry *parent, struct i2400m *i2400m)
  175. {
  176. return debugfs_create_file(name, 0200, parent, i2400m,
  177. &fops_i2400m_suspend);
  178. }
  179. /*
  180. * Reset the device
  181. *
  182. * Write 0 to ask the device to soft reset, 1 to cold reset, 2 to bus
  183. * reset (as defined by enum i2400m_reset_type).
  184. */
  185. static
  186. int debugfs_i2400m_reset_set(void *data, u64 val)
  187. {
  188. int result;
  189. struct i2400m *i2400m = data;
  190. enum i2400m_reset_type rt = val;
  191. switch(rt) {
  192. case I2400M_RT_WARM:
  193. case I2400M_RT_COLD:
  194. case I2400M_RT_BUS:
  195. result = i2400m_reset(i2400m, rt);
  196. if (result >= 0)
  197. result = 0;
  198. default:
  199. result = -EINVAL;
  200. }
  201. return result;
  202. }
  203. DEFINE_SIMPLE_ATTRIBUTE(fops_i2400m_reset,
  204. NULL, debugfs_i2400m_reset_set,
  205. "%llu\n");
  206. static
  207. struct dentry *debugfs_create_i2400m_reset(
  208. const char *name, struct dentry *parent, struct i2400m *i2400m)
  209. {
  210. return debugfs_create_file(name, 0200, parent, i2400m,
  211. &fops_i2400m_reset);
  212. }
  213. #define __debugfs_register(prefix, name, parent) \
  214. do { \
  215. result = d_level_register_debugfs(prefix, name, parent); \
  216. if (result < 0) \
  217. goto error; \
  218. } while (0)
  219. int i2400m_debugfs_add(struct i2400m *i2400m)
  220. {
  221. int result;
  222. struct device *dev = i2400m_dev(i2400m);
  223. struct dentry *dentry = i2400m->wimax_dev.debugfs_dentry;
  224. struct dentry *fd;
  225. dentry = debugfs_create_dir("i2400m", dentry);
  226. result = PTR_ERR(dentry);
  227. if (IS_ERR(dentry)) {
  228. if (result == -ENODEV)
  229. result = 0; /* No debugfs support */
  230. goto error;
  231. }
  232. i2400m->debugfs_dentry = dentry;
  233. __debugfs_register("dl_", control, dentry);
  234. __debugfs_register("dl_", driver, dentry);
  235. __debugfs_register("dl_", debugfs, dentry);
  236. __debugfs_register("dl_", fw, dentry);
  237. __debugfs_register("dl_", netdev, dentry);
  238. __debugfs_register("dl_", rfkill, dentry);
  239. __debugfs_register("dl_", rx, dentry);
  240. __debugfs_register("dl_", tx, dentry);
  241. fd = debugfs_create_size_t("tx_in", 0400, dentry,
  242. &i2400m->tx_in);
  243. result = PTR_ERR(fd);
  244. if (IS_ERR(fd) && result != -ENODEV) {
  245. dev_err(dev, "Can't create debugfs entry "
  246. "tx_in: %d\n", result);
  247. goto error;
  248. }
  249. fd = debugfs_create_size_t("tx_out", 0400, dentry,
  250. &i2400m->tx_out);
  251. result = PTR_ERR(fd);
  252. if (IS_ERR(fd) && result != -ENODEV) {
  253. dev_err(dev, "Can't create debugfs entry "
  254. "tx_out: %d\n", result);
  255. goto error;
  256. }
  257. fd = debugfs_create_u32("state", 0600, dentry,
  258. &i2400m->state);
  259. result = PTR_ERR(fd);
  260. if (IS_ERR(fd) && result != -ENODEV) {
  261. dev_err(dev, "Can't create debugfs entry "
  262. "state: %d\n", result);
  263. goto error;
  264. }
  265. /*
  266. * Trace received messages from user space
  267. *
  268. * In order to tap the bidirectional message stream in the
  269. * 'msg' pipe, user space can read from the 'msg' pipe;
  270. * however, due to limitations in libnl, we can't know what
  271. * the different applications are sending down to the kernel.
  272. *
  273. * So we have this hack where the driver will echo any message
  274. * received on the msg pipe from user space [through a call to
  275. * wimax_dev->op_msg_from_user() into
  276. * i2400m_op_msg_from_user()] into the 'trace' pipe that this
  277. * driver creates.
  278. *
  279. * So then, reading from both the 'trace' and 'msg' pipes in
  280. * user space will provide a full dump of the traffic.
  281. *
  282. * Write 1 to activate, 0 to clear.
  283. *
  284. * It is not really very atomic, but it is also not too
  285. * critical.
  286. */
  287. fd = debugfs_create_u8("trace_msg_from_user", 0600, dentry,
  288. &i2400m->trace_msg_from_user);
  289. result = PTR_ERR(fd);
  290. if (IS_ERR(fd) && result != -ENODEV) {
  291. dev_err(dev, "Can't create debugfs entry "
  292. "trace_msg_from_user: %d\n", result);
  293. goto error;
  294. }
  295. fd = debugfs_create_netdev_queue_stopped("netdev_queue_stopped",
  296. dentry, i2400m);
  297. result = PTR_ERR(fd);
  298. if (IS_ERR(fd) && result != -ENODEV) {
  299. dev_err(dev, "Can't create debugfs entry "
  300. "netdev_queue_stopped: %d\n", result);
  301. goto error;
  302. }
  303. fd = debugfs_create_file("rx_stats", 0600, dentry, i2400m,
  304. &i2400m_rx_stats_fops);
  305. result = PTR_ERR(fd);
  306. if (IS_ERR(fd) && result != -ENODEV) {
  307. dev_err(dev, "Can't create debugfs entry "
  308. "rx_stats: %d\n", result);
  309. goto error;
  310. }
  311. fd = debugfs_create_file("tx_stats", 0600, dentry, i2400m,
  312. &i2400m_tx_stats_fops);
  313. result = PTR_ERR(fd);
  314. if (IS_ERR(fd) && result != -ENODEV) {
  315. dev_err(dev, "Can't create debugfs entry "
  316. "tx_stats: %d\n", result);
  317. goto error;
  318. }
  319. fd = debugfs_create_i2400m_suspend("suspend", dentry, i2400m);
  320. result = PTR_ERR(fd);
  321. if (IS_ERR(fd) && result != -ENODEV) {
  322. dev_err(dev, "Can't create debugfs entry suspend: %d\n",
  323. result);
  324. goto error;
  325. }
  326. fd = debugfs_create_i2400m_reset("reset", dentry, i2400m);
  327. result = PTR_ERR(fd);
  328. if (IS_ERR(fd) && result != -ENODEV) {
  329. dev_err(dev, "Can't create debugfs entry reset: %d\n", result);
  330. goto error;
  331. }
  332. result = 0;
  333. error:
  334. return result;
  335. }
  336. void i2400m_debugfs_rm(struct i2400m *i2400m)
  337. {
  338. debugfs_remove_recursive(i2400m->debugfs_dentry);
  339. }