All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Antti Seppälä" <a.seppala@gmail.com>
To: Mauro Carvalho Chehab <m.chehab@samsung.com>
Cc: linux-media@vger.kernel.org, "Antti Seppälä" <a.seppala@gmail.com>
Subject: [RFC PATCH 2/4] rc-core: Add support for reading/writing wakeup scancodes via sysfs
Date: Mon, 20 Jan 2014 21:39:45 +0200	[thread overview]
Message-ID: <1390246787-15616-3-git-send-email-a.seppala@gmail.com> (raw)
In-Reply-To: <1390246787-15616-1-git-send-email-a.seppala@gmail.com>

This patch adds support for /sys/class/rc/rc?/wakeup_scancodes file
which can be used to read/write new wakeup scancodes to/from hardware.

The contents of the scancode file are simply white space separated
bytes.

How to read:
 cat /sys/class/rc/rc?/wakeup_scancodes

How to write:
 echo "0x1 0x2 0x3" > /sys/class/rc/rc?/wakeup_scancodes

Signed-off-by: Antti Seppälä <a.seppala@gmail.com>
---
 drivers/media/rc/rc-main.c | 129 +++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 129 insertions(+)

diff --git a/drivers/media/rc/rc-main.c b/drivers/media/rc/rc-main.c
index 02e2f38..a2a68f3 100644
--- a/drivers/media/rc/rc-main.c
+++ b/drivers/media/rc/rc-main.c
@@ -967,6 +967,119 @@ out:
 	return ret;
 }
 
+/**
+ * show_wakeup_scancodes() - shows the current IR wake scancode(s)
+ * @device:	the device descriptor
+ * @mattr:	the device attribute struct (unused)
+ * @buf:	a pointer to the output buffer
+ *
+ * This routine is a callback routine for input read the IR wake scancode(s).
+ * it is trigged by reading /sys/class/rc/rc?/wakeup_scancodes.
+ * It returns the currently active IR wake scancode or empty buffer if wake
+ * scancode is not active.
+ *
+ * dev->lock is taken to guard against races between device
+ * registration, store_wakeup_scancodes and show_wakeup_scancodes.
+ */
+static ssize_t show_wakeup_scancodes(struct device *device,
+				     struct device_attribute *mattr, char *buf)
+{
+	int ret, pos = 0;
+	struct rc_wakeup_scancode *scancode, *next;
+	LIST_HEAD(scancode_list);
+	struct rc_dev *dev = to_rc_dev(device);
+
+	if (!dev || !dev->s_wakeup_scancodes)
+		return -ENODEV;
+
+	mutex_lock(&dev->lock);
+
+	ret = dev->s_wakeup_scancodes(dev, &scancode_list, 0);
+
+	list_for_each_entry_safe_reverse(scancode, next, &scancode_list,
+					 list_item) {
+		pos += scnprintf(buf + pos, PAGE_SIZE - pos, "0x%x ",
+				 scancode->value);
+		list_del(&scancode->list_item);
+		kfree(scancode);
+	}
+	pos += scnprintf(buf + pos, PAGE_SIZE - pos, "\n");
+
+	mutex_unlock(&dev->lock);
+
+	if (ret < 0)
+		return ret;
+
+	return pos;
+}
+
+/**
+ * store_wakeup_scancodes() - changes the current IR wake scancode(s)
+ * @device:	the device descriptor
+ * @mattr:	the device attribute struct (unused)
+ * @buf:	a pointer to the input buffer
+ * @len:	length of the input buffer
+ *
+ * This routine is for changing the IR wake scancode.
+ * It is trigged by writing to /sys/class/rc/rc?/wakeup_scancodes.
+ * Writing bytes separated by white space will pass them to the hardware.
+ * Writing "" (empty) will clear active wake scancode.
+ * Returns -EINVAL if too many values or invalid values were used
+ * otherwise @len.
+ *
+ * dev->lock is taken to guard against races between device
+ * registration, store_wakeup_scancodes and show_wakeup_scancodes.
+ */
+static ssize_t store_wakeup_scancodes(struct device *device,
+				      struct device_attribute *mattr,
+				      const char *data,
+				      size_t len)
+{
+	int ret = 0, error = 0;
+	char *tmp;
+	u8 value;
+	struct rc_wakeup_scancode *scancode, *next;
+	LIST_HEAD(scancode_list);
+	struct rc_dev *dev = to_rc_dev(device);
+
+	if (!dev || !dev->s_wakeup_scancodes)
+		return -ENODEV;
+
+	mutex_lock(&dev->lock);
+
+	while ((tmp = strsep((char **) &data, " ,\t\n")) != NULL) {
+		if (!*tmp)
+			break;
+
+		if (sscanf(tmp, "0x%2hhx", &value) != 1 &&
+		    sscanf(tmp,   "%2hhx", &value) != 1) {
+			error = 1;
+			break;
+		} else {
+			scancode = kmalloc(sizeof(struct rc_wakeup_scancode),
+					   GFP_KERNEL);
+			scancode->value = value;
+			list_add(&scancode->list_item, &scancode_list);
+		}
+	}
+
+	if (error)
+		IR_dprintk(1, "Error parsing value of %s", tmp);
+	else
+		ret = dev->s_wakeup_scancodes(dev, &scancode_list, 1);
+
+	list_for_each_entry_safe(scancode, next, &scancode_list, list_item) {
+		list_del(&scancode->list_item);
+		kfree(scancode);
+	}
+
+	mutex_unlock(&dev->lock);
+	if (ret < 0)
+		return ret;
+
+	return error ? -EINVAL : len;
+}
+
 static void rc_dev_release(struct device *device)
 {
 }
@@ -1019,6 +1132,15 @@ static struct device_type rc_dev_type = {
 	.uevent		= rc_dev_uevent,
 };
 
+static struct device_attribute dev_attr_wakeup_scancodes = {
+	.attr	= {
+		.name = "wakeup_scancodes",
+		.mode = S_IRUGO | S_IWUSR,
+	},
+	.show = show_wakeup_scancodes,
+	.store = store_wakeup_scancodes,
+};
+
 struct rc_dev *rc_allocate_device(void)
 {
 	struct rc_dev *dev;
@@ -1175,6 +1297,13 @@ int rc_register_device(struct rc_dev *dev)
 		dev->enabled_protocols = rc_type;
 	}
 
+	/* Create sysfs entry only if device has wake scancode support */
+	if (dev->s_wakeup_scancodes) {
+		rc = device_create_file(&dev->dev, &dev_attr_wakeup_scancodes);
+		if (rc < 0)
+			goto out_raw;
+	}
+
 	mutex_unlock(&dev->lock);
 
 	IR_dprintk(1, "Registered rc%ld (driver: %s, remote: %s, mode %s)\n",
-- 
1.8.3.2


  parent reply	other threads:[~2014-01-20 19:40 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-01-03 14:18 [PATCH] nuvoton-cir: Add support for user configurable wake-up codes Antti Seppälä
2014-01-15 19:35 ` Mauro Carvalho Chehab
2014-01-20 19:39   ` [RFC PATCH 0/4] rc: Adding support for sysfs wakeup scancodes Antti Seppälä
2014-01-20 19:39     ` [RFC PATCH 1/4] rc-core: Add defintions needed for sysfs callback Antti Seppälä
2014-01-20 19:39     ` Antti Seppälä [this message]
2014-01-20 19:39     ` [RFC PATCH 3/4] rc-loopback: Add support for reading/writing wakeup scancodes via sysfs Antti Seppälä
2014-01-20 19:39     ` [RFC PATCH 4/4] nuvoton-cir: " Antti Seppälä
2014-01-21 12:28     ` [RFC PATCH 0/4] rc: Adding support for sysfs wakeup scancodes Sean Young
2014-01-22 15:46       ` Antti Seppälä
2014-01-22 16:29         ` Sean Young
2014-01-22 19:10           ` Antti Seppälä
2014-01-22 19:21             ` Antti Palosaari
2014-01-22 21:00             ` Sean Young
2014-01-22 22:01               ` Mauro Carvalho Chehab
2014-01-23 19:11                 ` Antti Seppälä
2014-02-04 17:54                   ` Mauro Carvalho Chehab
2014-02-05  7:03                     ` Antti Seppälä
2014-02-05  9:36                       ` Mauro Carvalho Chehab
2014-02-05  9:39                       ` James Hogan
2014-02-05  9:42                         ` James Hogan
2014-02-05 18:16                           ` Antti Seppälä
2014-02-05 21:21                             ` Mauro Carvalho Chehab
2014-02-06 10:46                               ` James Hogan
2014-02-06 14:55                                 ` Mauro Carvalho Chehab

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1390246787-15616-3-git-send-email-a.seppala@gmail.com \
    --to=a.seppala@gmail.com \
    --cc=linux-media@vger.kernel.org \
    --cc=m.chehab@samsung.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.