debugfs.c 9.9 KB

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