debugfs.c 9.8 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. break;
  190. default:
  191. result = -EINVAL;
  192. }
  193. return result;
  194. }
  195. DEFINE_SIMPLE_ATTRIBUTE(fops_i2400m_reset,
  196. NULL, debugfs_i2400m_reset_set,
  197. "%llu\n");
  198. static
  199. struct dentry *debugfs_create_i2400m_reset(
  200. const char *name, struct dentry *parent, struct i2400m *i2400m)
  201. {
  202. return debugfs_create_file(name, 0200, parent, i2400m,
  203. &fops_i2400m_reset);
  204. }
  205. #define __debugfs_register(prefix, name, parent) \
  206. do { \
  207. result = d_level_register_debugfs(prefix, name, parent); \
  208. if (result < 0) \
  209. goto error; \
  210. } while (0)
  211. int i2400m_debugfs_add(struct i2400m *i2400m)
  212. {
  213. int result;
  214. struct device *dev = i2400m_dev(i2400m);
  215. struct dentry *dentry = i2400m->wimax_dev.debugfs_dentry;
  216. struct dentry *fd;
  217. dentry = debugfs_create_dir("i2400m", dentry);
  218. result = PTR_ERR(dentry);
  219. if (IS_ERR(dentry)) {
  220. if (result == -ENODEV)
  221. result = 0; /* No debugfs support */
  222. goto error;
  223. }
  224. i2400m->debugfs_dentry = dentry;
  225. __debugfs_register("dl_", control, dentry);
  226. __debugfs_register("dl_", driver, dentry);
  227. __debugfs_register("dl_", debugfs, dentry);
  228. __debugfs_register("dl_", fw, dentry);
  229. __debugfs_register("dl_", netdev, dentry);
  230. __debugfs_register("dl_", rfkill, dentry);
  231. __debugfs_register("dl_", rx, dentry);
  232. __debugfs_register("dl_", tx, dentry);
  233. fd = debugfs_create_size_t("tx_in", 0400, dentry,
  234. &i2400m->tx_in);
  235. result = PTR_ERR(fd);
  236. if (IS_ERR(fd) && result != -ENODEV) {
  237. dev_err(dev, "Can't create debugfs entry "
  238. "tx_in: %d\n", result);
  239. goto error;
  240. }
  241. fd = debugfs_create_size_t("tx_out", 0400, dentry,
  242. &i2400m->tx_out);
  243. result = PTR_ERR(fd);
  244. if (IS_ERR(fd) && result != -ENODEV) {
  245. dev_err(dev, "Can't create debugfs entry "
  246. "tx_out: %d\n", result);
  247. goto error;
  248. }
  249. fd = debugfs_create_u32("state", 0600, dentry,
  250. &i2400m->state);
  251. result = PTR_ERR(fd);
  252. if (IS_ERR(fd) && result != -ENODEV) {
  253. dev_err(dev, "Can't create debugfs entry "
  254. "state: %d\n", result);
  255. goto error;
  256. }
  257. /*
  258. * Trace received messages from user space
  259. *
  260. * In order to tap the bidirectional message stream in the
  261. * 'msg' pipe, user space can read from the 'msg' pipe;
  262. * however, due to limitations in libnl, we can't know what
  263. * the different applications are sending down to the kernel.
  264. *
  265. * So we have this hack where the driver will echo any message
  266. * received on the msg pipe from user space [through a call to
  267. * wimax_dev->op_msg_from_user() into
  268. * i2400m_op_msg_from_user()] into the 'trace' pipe that this
  269. * driver creates.
  270. *
  271. * So then, reading from both the 'trace' and 'msg' pipes in
  272. * user space will provide a full dump of the traffic.
  273. *
  274. * Write 1 to activate, 0 to clear.
  275. *
  276. * It is not really very atomic, but it is also not too
  277. * critical.
  278. */
  279. fd = debugfs_create_u8("trace_msg_from_user", 0600, dentry,
  280. &i2400m->trace_msg_from_user);
  281. result = PTR_ERR(fd);
  282. if (IS_ERR(fd) && result != -ENODEV) {
  283. dev_err(dev, "Can't create debugfs entry "
  284. "trace_msg_from_user: %d\n", result);
  285. goto error;
  286. }
  287. fd = debugfs_create_netdev_queue_stopped("netdev_queue_stopped",
  288. dentry, i2400m);
  289. result = PTR_ERR(fd);
  290. if (IS_ERR(fd) && result != -ENODEV) {
  291. dev_err(dev, "Can't create debugfs entry "
  292. "netdev_queue_stopped: %d\n", result);
  293. goto error;
  294. }
  295. fd = debugfs_create_file("rx_stats", 0600, dentry, i2400m,
  296. &i2400m_rx_stats_fops);
  297. result = PTR_ERR(fd);
  298. if (IS_ERR(fd) && result != -ENODEV) {
  299. dev_err(dev, "Can't create debugfs entry "
  300. "rx_stats: %d\n", result);
  301. goto error;
  302. }
  303. fd = debugfs_create_file("tx_stats", 0600, dentry, i2400m,
  304. &i2400m_tx_stats_fops);
  305. result = PTR_ERR(fd);
  306. if (IS_ERR(fd) && result != -ENODEV) {
  307. dev_err(dev, "Can't create debugfs entry "
  308. "tx_stats: %d\n", result);
  309. goto error;
  310. }
  311. fd = debugfs_create_i2400m_suspend("suspend", dentry, i2400m);
  312. result = PTR_ERR(fd);
  313. if (IS_ERR(fd) && result != -ENODEV) {
  314. dev_err(dev, "Can't create debugfs entry suspend: %d\n",
  315. result);
  316. goto error;
  317. }
  318. fd = debugfs_create_i2400m_reset("reset", dentry, i2400m);
  319. result = PTR_ERR(fd);
  320. if (IS_ERR(fd) && result != -ENODEV) {
  321. dev_err(dev, "Can't create debugfs entry reset: %d\n", result);
  322. goto error;
  323. }
  324. result = 0;
  325. error:
  326. return result;
  327. }
  328. void i2400m_debugfs_rm(struct i2400m *i2400m)
  329. {
  330. debugfs_remove_recursive(i2400m->debugfs_dentry);
  331. }