debugfs.c 10 KB

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