debugfs.c 9.7 KB

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