[PATCH 20/40] i2400m: debugfs controls

Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]
From: Inaky Perez-Gonzalez
Date: Friday, December 5, 2008 - 11:55 am

Expose knobs to control the device (induce reset, power saving,
querying tx or rx stats, internal debug information and debug level
manipulation).

Signed-off-by: Inaky Perez-Gonzalez <inaky@linux.intel.com>
---
 drivers/net/wimax/i2400m/debugfs.c |  383 ++++++++++++++++++++++++++++++++++++
 1 files changed, 383 insertions(+), 0 deletions(-)
 create mode 100644 drivers/net/wimax/i2400m/debugfs.c

diff --git a/drivers/net/wimax/i2400m/debugfs.c b/drivers/net/wimax/i2400m/debugfs.c
new file mode 100644
index 0000000..9d410fe
--- /dev/null
+++ b/drivers/net/wimax/i2400m/debugfs.c
@@ -0,0 +1,383 @@
+/*
+ * Intel Wireless WiMAX Connection 2400m
+ * Debugfs interfaces to manipulate driver and device information
+ *
+ *
+ * Copyright (C) 2007 Intel Corporation <linux-wimax@intel.com>
+ * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License version
+ * 2 as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA.
+ */
+
+#include <linux/debugfs.h>
+#include <linux/netdevice.h>
+#include <linux/etherdevice.h>
+#include <linux/spinlock.h>
+#include <linux/device.h>
+#include "i2400m.h"
+
+
+#define D_SUBMODULE debugfs
+#include "debug-levels.h"
+
+static
+int debugfs_netdev_queue_stopped_get(void *data, u64 *val)
+{
+	struct i2400m *i2400m = data;
+	*val = netif_queue_stopped(i2400m->wimax_dev.net_dev);
+	return 0;
+}
+DEFINE_SIMPLE_ATTRIBUTE(fops_netdev_queue_stopped,
+			debugfs_netdev_queue_stopped_get,
+			NULL, "%llu\n");
+
+
+static
+struct dentry *debugfs_create_netdev_queue_stopped(
+	const char *name, struct dentry *parent, struct i2400m *i2400m)
+{
+	return debugfs_create_file(name, 0400, parent, i2400m,
+				   &fops_netdev_queue_stopped);
+}
+
+
+/*
+ * inode->i_private has the @data argument to debugfs_create_file()
+ */
+static
+int i2400m_stats_open(struct inode *inode, struct file *filp)
+{
+	filp->private_data = inode->i_private;
+	return 0;
+}
+
+/*
+ * We don't allow partial reads of this file, as then the reader would
+ * get weirdly confused data as it is updated.
+ *
+ * So or you read it all or nothing; if you try to read with an offset
+ * != 0, we consider you are done reading.
+ */
+static
+ssize_t i2400m_rx_stats_read(struct file *filp, char __user *buffer,
+			     size_t count, loff_t *ppos)
+{
+	struct i2400m *i2400m = filp->private_data;
+	char buf[128];
+	unsigned long flags;
+
+	if (*ppos != 0)
+		return 0;
+	if (count < sizeof(buf))
+		return -ENOSPC;
+	spin_lock_irqsave(&i2400m->rx_lock, flags);
+	snprintf(buf, sizeof(buf), "%u %u %u %u %u %u %u\n",
+		 i2400m->rx_pl_num, i2400m->rx_pl_min,
+		 i2400m->rx_pl_max, i2400m->rx_num,
+		 i2400m->rx_size_acc,
+		 i2400m->rx_size_min, i2400m->rx_size_max);
+	spin_unlock_irqrestore(&i2400m->rx_lock, flags);
+	return simple_read_from_buffer(buffer, count, ppos, buf, strlen(buf));
+}
+
+
+/* Any write clears the stats */
+static
+ssize_t i2400m_rx_stats_write(struct file *filp, const char __user *buffer,
+			      size_t count, loff_t *ppos)
+{
+	struct i2400m *i2400m = filp->private_data;
+	unsigned long flags;
+
+	spin_lock_irqsave(&i2400m->rx_lock, flags);
+	i2400m->rx_pl_num = 0;
+	i2400m->rx_pl_max = 0;
+	i2400m->rx_pl_min = UINT_MAX;
+	i2400m->rx_num = 0;
+	i2400m->rx_size_acc = 0;
+	i2400m->rx_size_min = UINT_MAX;
+	i2400m->rx_size_max = 0;
+	spin_unlock_irqrestore(&i2400m->rx_lock, flags);
+	return count;
+}
+
+static
+const struct file_operations i2400m_rx_stats_fops = {
+	.owner =	THIS_MODULE,
+	.open =		i2400m_stats_open,
+	.read =		i2400m_rx_stats_read,
+	.write =	i2400m_rx_stats_write,
+};
+
+
+/* See i2400m_rx_stats_read() */
+static
+ssize_t i2400m_tx_stats_read(struct file *filp, char __user *buffer,
+			     size_t count, loff_t *ppos)
+{
+	struct i2400m *i2400m = filp->private_data;
+	char buf[128];
+	unsigned long flags;
+
+	if (*ppos != 0)
+		return 0;
+	if (count < sizeof(buf))
+		return -ENOSPC;
+	spin_lock_irqsave(&i2400m->tx_lock, flags);
+	snprintf(buf, sizeof(buf), "%u %u %u %u %u %u %u\n",
+		 i2400m->tx_pl_num, i2400m->tx_pl_min,
+		 i2400m->tx_pl_max, i2400m->tx_num,
+		 i2400m->tx_size_acc,
+		 i2400m->tx_size_min, i2400m->tx_size_max);
+	spin_unlock_irqrestore(&i2400m->tx_lock, flags);
+	return simple_read_from_buffer(buffer, count, ppos, buf, strlen(buf));
+}
+
+/* Any write clears the stats */
+static
+ssize_t i2400m_tx_stats_write(struct file *filp, const char __user *buffer,
+			      size_t count, loff_t *ppos)
+{
+	struct i2400m *i2400m = filp->private_data;
+	unsigned long flags;
+
+	spin_lock_irqsave(&i2400m->tx_lock, flags);
+	i2400m->tx_pl_num = 0;
+	i2400m->tx_pl_max = 0;
+	i2400m->tx_pl_min = UINT_MAX;
+	i2400m->tx_num = 0;
+	i2400m->tx_size_acc = 0;
+	i2400m->tx_size_min = UINT_MAX;
+	i2400m->tx_size_max = 0;
+	spin_unlock_irqrestore(&i2400m->tx_lock, flags);
+	return count;
+}
+
+static
+const struct file_operations i2400m_tx_stats_fops = {
+	.owner =	THIS_MODULE,
+	.open =		i2400m_stats_open,
+	.read =		i2400m_tx_stats_read,
+	.write =	i2400m_tx_stats_write,
+};
+
+
+/* Write 1 to ask the device to go into suspend */
+static
+int debugfs_i2400m_suspend_set(void *data, u64 val)
+{
+	int result;
+	struct i2400m *i2400m = data;
+	result = i2400m_cmd_enter_powersave(i2400m);
+	if (result >= 0)
+		result = 0;
+	return result;
+}
+DEFINE_SIMPLE_ATTRIBUTE(fops_i2400m_suspend,
+			NULL, debugfs_i2400m_suspend_set,
+			"%llu\n");
+
+static
+struct dentry *debugfs_create_i2400m_suspend(
+	const char *name, struct dentry *parent, struct i2400m *i2400m)
+{
+	return debugfs_create_file(name, 0200, parent, i2400m,
+				   &fops_i2400m_suspend);
+}
+
+
+/*
+ * Reset the device
+ *
+ * Write 0 to ask the device to soft reset, 1 to cold reset, 2 to bus
+ * reset (as defined by enum i2400m_reset_type).
+ */
+static
+int debugfs_i2400m_reset_set(void *data, u64 val)
+{
+	int result;
+	struct i2400m *i2400m = data;
+	enum i2400m_reset_type rt = val;
+	result = i2400m->bus_reset(i2400m, rt);
+	if (result >= 0)
+		result = 0;
+	return result;
+}
+DEFINE_SIMPLE_ATTRIBUTE(fops_i2400m_reset,
+			NULL, debugfs_i2400m_reset_set,
+			"%llu\n");
+
+static
+struct dentry *debugfs_create_i2400m_reset(
+	const char *name, struct dentry *parent, struct i2400m *i2400m)
+{
+	return debugfs_create_file(name, 0200, parent, i2400m,
+				   &fops_i2400m_reset);
+}
+
+/*
+ * Debug levels control; see debug.h
+ */
+struct d_level D_LEVEL[] = {
+	D_SUBMODULE_DEFINE(control),
+	D_SUBMODULE_DEFINE(driver),
+	D_SUBMODULE_DEFINE(debugfs),
+	D_SUBMODULE_DEFINE(fw),
+	D_SUBMODULE_DEFINE(netdev),
+	D_SUBMODULE_DEFINE(rfkill),
+	D_SUBMODULE_DEFINE(rx),
+	D_SUBMODULE_DEFINE(tx),
+};
+size_t D_LEVEL_SIZE = ARRAY_SIZE(D_LEVEL);
+
+#define __debugfs_register(prefix, name, parent)		\
+	result = d_level_register_debugfs(prefix, name, parent);\
+	if (result < 0)						\
+		goto error;
+
+
+int i2400m_debugfs_add(struct i2400m *i2400m)
+{
+	int result;
+	struct device *dev = i2400m_dev(i2400m);
+	struct dentry *dentry = i2400m->wimax_dev.debugfs_dentry;
+	struct dentry *fd;
+
+	dentry = debugfs_create_dir("i2400m", dentry);
+	result = PTR_ERR(dentry);
+	if (IS_ERR(dentry)) {
+		if (result == -ENODEV)
+			result = 0;	/* No debugfs support */
+		goto error;
+	}
+	i2400m->debugfs_dentry = dentry;
+	__debugfs_register("dl_", control, dentry);
+	__debugfs_register("dl_", driver, dentry);
+	__debugfs_register("dl_", debugfs, dentry);
+	__debugfs_register("dl_", fw, dentry);
+	__debugfs_register("dl_", netdev, dentry);
+	__debugfs_register("dl_", rfkill, dentry);
+	__debugfs_register("dl_", rx, dentry);
+	__debugfs_register("dl_", tx, dentry);
+
+	fd = debugfs_create_size_t("tx_in", 0400, dentry,
+				   &i2400m->tx_in);
+	result = PTR_ERR(fd);
+	if (IS_ERR(fd) && result != -ENODEV) {
+		dev_err(dev, "Can't create debugfs entry "
+			"tx_in: %d\n", result);
+		goto error;
+	}
+
+	fd = debugfs_create_size_t("tx_out", 0400, dentry,
+				   &i2400m->tx_out);
+	result = PTR_ERR(fd);
+	if (IS_ERR(fd) && result != -ENODEV) {
+		dev_err(dev, "Can't create debugfs entry "
+			"tx_out: %d\n", result);
+		goto error;
+	}
+
+	fd = debugfs_create_u32("state", 0600, dentry,
+				&i2400m->state);
+	result = PTR_ERR(fd);
+	if (IS_ERR(fd) && result != -ENODEV) {
+		dev_err(dev, "Can't create debugfs entry "
+			"state: %d\n", result);
+		goto error;
+	}
+
+	/*
+	 * Trace received messages from user space
+	 *
+	 * In order to tap the bidirectional message stream in the
+	 * 'msg' pipe, user space can read from the 'msg' pipe;
+	 * however, due to limitations in libnl, we can't know what
+	 * the different applications are sending down to the kernel.
+	 *
+	 * So we have this hack where the driver will echo any message
+	 * received on the msg pipe from user space [through a call to
+	 * wimax_dev->op_msg_from_user() into
+	 * i2400m_op_msg_from_user()] into the 'trace' pipe that this
+	 * driver creates.
+	 *
+	 * So then, reading from both the 'trace' and 'msg' pipes in
+	 * user space will provide a full dump of the traffic.
+	 *
+	 * Write 1 to activate, 0 to clear.
+	 *
+	 * It is not really very atomic, but it is also not too
+	 * critical.
+	 */
+	fd = debugfs_create_u8("trace_msg_from_user", 0600, dentry,
+			       &i2400m->trace_msg_from_user);
+	result = PTR_ERR(fd);
+	if (IS_ERR(fd) && result != -ENODEV) {
+		dev_err(dev, "Can't create debugfs entry "
+			"trace_msg_from_user: %d\n", result);
+		goto error;
+	}
+
+	fd = debugfs_create_netdev_queue_stopped("netdev_queue_stopped",
+						 dentry, i2400m);
+	result = PTR_ERR(fd);
+	if (IS_ERR(fd) && result != -ENODEV) {
+		dev_err(dev, "Can't create debugfs entry "
+			"netdev_queue_stopped: %d\n", result);
+		goto error;
+	}
+
+	fd = debugfs_create_file("rx_stats", 0600, dentry, i2400m,
+				 &i2400m_rx_stats_fops);
+	result = PTR_ERR(fd);
+	if (IS_ERR(fd) && result != -ENODEV) {
+		dev_err(dev, "Can't create debugfs entry "
+			"rx_stats: %d\n", result);
+		goto error;
+	}
+
+	fd = debugfs_create_file("tx_stats", 0600, dentry, i2400m,
+				 &i2400m_tx_stats_fops);
+	result = PTR_ERR(fd);
+	if (IS_ERR(fd) && result != -ENODEV) {
+		dev_err(dev, "Can't create debugfs entry "
+			"tx_stats: %d\n", result);
+		goto error;
+	}
+
+	fd = debugfs_create_i2400m_suspend("suspend", dentry, i2400m);
+	result = PTR_ERR(fd);
+	if (IS_ERR(fd) && result != -ENODEV) {
+		dev_err(dev, "Can't create debugfs entry suspend: %d\n",
+			result);
+		goto error;
+	}
+
+	fd = debugfs_create_i2400m_reset("reset", dentry, i2400m);
+	result = PTR_ERR(fd);
+	if (IS_ERR(fd) && result != -ENODEV) {
+		dev_err(dev, "Can't create debugfs entry reset: %d\n", result);
+		goto error;
+	}
+
+	result = 0;
+error:
+	return result;
+}
+
+void i2400m_debugfs_rm(struct i2400m *i2400m)
+{
+	debugfs_remove_recursive(i2400m->debugfs_dentry);
+}
-- 
1.5.6.5

--
To unsubscribe from this list: send the line "unsubscribe netdev" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]

Messages in current thread:
[PATCH 00/40] merge request for WiMAX kernel stack and i24 ..., Inaky Perez-Gonzalez, (Fri Dec 5, 11:54 am)
[PATCH 01/40] wimax: documentation for the stack, Inaky Perez-Gonzalez, (Fri Dec 5, 11:54 am)
[PATCH 02/40] wimax: declarations for the in-kernel WiMAX API, Inaky Perez-Gonzalez, (Fri Dec 5, 11:54 am)
[PATCH 03/40] wimax: constants and definitions to interact ..., Inaky Perez-Gonzalez, (Fri Dec 5, 11:54 am)
[PATCH 04/40] wimax: internal API for the kernel space WiM ..., Inaky Perez-Gonzalez, (Fri Dec 5, 11:54 am)
[PATCH 05/40] wimax: debug macros and debug settings for t ..., Inaky Perez-Gonzalez, (Fri Dec 5, 11:54 am)
[PATCH 06/40] genetlink: export genl_unregister_mc_group(), Inaky Perez-Gonzalez, (Fri Dec 5, 11:54 am)
[PATCH 07/40] debugfs: add helpers for exporting a size_t ..., Inaky Perez-Gonzalez, (Fri Dec 5, 11:55 am)
[PATCH 08/40] wimax: generic WiMAX device management (regi ..., Inaky Perez-Gonzalez, (Fri Dec 5, 11:55 am)
[PATCH 09/40] wimax: Mappping of generic netlink family ID ..., Inaky Perez-Gonzalez, (Fri Dec 5, 11:55 am)
[PATCH 10/40] wimax: Generic messaging interface between u ..., Inaky Perez-Gonzalez, (Fri Dec 5, 11:55 am)
[PATCH 11/40] wimax: RF-kill framework integration, Inaky Perez-Gonzalez, (Fri Dec 5, 11:55 am)
[PATCH 12/40] wimax: API call to reset a WiMAX device, Inaky Perez-Gonzalez, (Fri Dec 5, 11:55 am)
[PATCH 13/40] wimax: debugfs controls, Inaky Perez-Gonzalez, (Fri Dec 5, 11:55 am)
[PATCH 14/40] wimax: Makefile, Kconfig and docbook linkage ..., Inaky Perez-Gonzalez, (Fri Dec 5, 11:55 am)
[PATCH 15/40] i2400m: documentation and instructions for usage, Inaky Perez-Gonzalez, (Fri Dec 5, 11:55 am)
[PATCH 16/40] i2400m: host-to-device protocol definitions, Inaky Perez-Gonzalez, (Fri Dec 5, 11:55 am)
[PATCH 17/40] i2400m: core driver definitions and API, Inaky Perez-Gonzalez, (Fri Dec 5, 11:55 am)
[PATCH 18/40] i2400m: Generic probe/disconnect, reset and ..., Inaky Perez-Gonzalez, (Fri Dec 5, 11:55 am)
[PATCH 19/40] i2400m: linkage to the networking stack, Inaky Perez-Gonzalez, (Fri Dec 5, 11:55 am)
[PATCH 20/40] i2400m: debugfs controls, Inaky Perez-Gonzalez, (Fri Dec 5, 11:55 am)
[PATCH 21/40] i2400m: rfkill integration with the WiMAX stack, Inaky Perez-Gonzalez, (Fri Dec 5, 11:55 am)
[PATCH 22/40] i2400m: firmware loading and bootrom initial ..., Inaky Perez-Gonzalez, (Fri Dec 5, 11:55 am)
[PATCH 23/40] i2400m: handling of the data/control recepti ..., Inaky Perez-Gonzalez, (Fri Dec 5, 11:55 am)
[PATCH 24/40] i2400m: handling of the data/control transmi ..., Inaky Perez-Gonzalez, (Fri Dec 5, 11:55 am)
[PATCH 25/40] i2400m: various functions for device management, Inaky Perez-Gonzalez, (Fri Dec 5, 11:55 am)
[PATCH 26/40] i2400m/USB: header for the USB bus driver, Inaky Perez-Gonzalez, (Fri Dec 5, 11:55 am)
[PATCH 27/40] i2400m/USB: error density tracking, Inaky Perez-Gonzalez, (Fri Dec 5, 11:55 am)
[PATCH 28/40] i2400m/USB: main probe/disconnect and backen ..., Inaky Perez-Gonzalez, (Fri Dec 5, 11:55 am)
[PATCH 29/40] i2400m/USB: firmware upload backend, Inaky Perez-Gonzalez, (Fri Dec 5, 11:55 am)
[PATCH 30/40] i2400m/USB: handling of notifications from t ..., Inaky Perez-Gonzalez, (Fri Dec 5, 11:55 am)
[PATCH 31/40] i2400m/USB: read transactions from the USB d ..., Inaky Perez-Gonzalez, (Fri Dec 5, 11:55 am)
[PATCH 32/40] i2400m/USB: write transactions to the USB device, Inaky Perez-Gonzalez, (Fri Dec 5, 11:55 am)
[PATCH 33/40] i2400m/SDIO: header for the SDIO subdriver, Inaky Perez-Gonzalez, (Fri Dec 5, 11:55 am)
[PATCH 34/40] i2400m/SDIO: main probe/disconnect and backe ..., Inaky Perez-Gonzalez, (Fri Dec 5, 11:55 am)
[PATCH 35/40] i2400m/SDIO: firmware upload backend, Inaky Perez-Gonzalez, (Fri Dec 5, 11:55 am)
[PATCH 36/40] i2400m/SDIO: read transactions from the SDIO ..., Inaky Perez-Gonzalez, (Fri Dec 5, 11:55 am)
[PATCH 37/40] i2400m/SDIO: write transactions to the SDIO ..., Inaky Perez-Gonzalez, (Fri Dec 5, 11:55 am)
[PATCH 38/40] i2400m: Makefile and Kconfig, Inaky Perez-Gonzalez, (Fri Dec 5, 11:55 am)
[PATCH 39/40] wimax: export linux/wimax.h and linux/wimax/ ..., Inaky Perez-Gonzalez, (Fri Dec 5, 11:55 am)
[PATCH 40/40] wimax/i2400m: add CREDITS and MAINTAINERS en ..., Inaky Perez-Gonzalez, (Fri Dec 5, 11:55 am)
Re: [PATCH 02/40] wimax: declarations for the in-kernel Wi ..., Inaky Perez-Gonzalez, (Sat Dec 6, 6:04 pm)