linux-media.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Sean Young <sean@mess.org>
To: linux-media@vger.kernel.org
Cc: "James Hogan" <james@albanarts.com>,
	"Antti Seppälä" <a.seppala@gmail.com>,
	"David Härdeman" <david@hardeman.nu>
Subject: [PATCH v5 09/18] [media] rc: ir-rc5-decoder: Add encode capability
Date: Mon, 12 Dec 2016 21:13:45 +0000	[thread overview]
Message-ID: <abb46ba342ed0cfd8345223a2c35e93287d03a35.1481575826.git.sean@mess.org> (raw)
In-Reply-To: <1669f6c54c34e5a78ce114c633c98b331e58e8c7.1481575826.git.sean@mess.org>
In-Reply-To: <cover.1481575826.git.sean@mess.org>

From: James Hogan <james@albanarts.com>

Add the capability to encode RC-5, RC-5X and RC-5-SZ scancodes as raw
events.

The Manchester modulation helper is used, and for RC-5X it is used twice
with two sets of timings, the first with a short trailer space for the
space in the middle, and the second with no leader so that it can
continue the space.

The encoding in RC-5-SZ first inserts a pulse and then simply utilizes
the generic Manchester encoder available in rc-core.

Signed-off-by: James Hogan <james@albanarts.com>
Signed-off-by: Antti Seppälä <a.seppala@gmail.com>
Signed-off-by: Sean Young <sean@mess.org>
Cc: David Härdeman <david@hardeman.nu>
---
 drivers/media/rc/ir-rc5-decoder.c | 97 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 97 insertions(+)

diff --git a/drivers/media/rc/ir-rc5-decoder.c b/drivers/media/rc/ir-rc5-decoder.c
index a95477c..92964bd 100644
--- a/drivers/media/rc/ir-rc5-decoder.c
+++ b/drivers/media/rc/ir-rc5-decoder.c
@@ -181,9 +181,106 @@ static int ir_rc5_decode(struct rc_dev *dev, struct ir_raw_event ev)
 	return -EINVAL;
 }
 
+static struct ir_raw_timings_manchester ir_rc5_timings = {
+	.leader			= RC5_UNIT,
+	.pulse_space_start	= 0,
+	.clock			= RC5_UNIT,
+	.trailer_space		= RC5_UNIT * 10,
+};
+
+static struct ir_raw_timings_manchester ir_rc5x_timings[2] = {
+	{
+		.leader			= RC5_UNIT,
+		.pulse_space_start	= 0,
+		.clock			= RC5_UNIT,
+		.trailer_space		= RC5X_SPACE,
+	},
+	{
+		.clock			= RC5_UNIT,
+		.trailer_space		= RC5_UNIT * 10,
+	},
+};
+
+static struct ir_raw_timings_manchester ir_rc5_sz_timings = {
+	.leader				= RC5_UNIT,
+	.pulse_space_start		= 0,
+	.clock				= RC5_UNIT,
+	.trailer_space			= RC5_UNIT * 10,
+};
+
+/**
+ * ir_rc5_encode() - Encode a scancode as a stream of raw events
+ *
+ * @protocol:	protocol variant to encode
+ * @scancode:	scancode to encode
+ * @events:	array of raw ir events to write into
+ * @max:	maximum size of @events
+ *
+ * Returns:	The number of events written.
+ *		-ENOBUFS if there isn't enough space in the array to fit the
+ *		encoding. In this case all @max events will have been written.
+ *		-EINVAL if the scancode is ambiguous or invalid.
+ */
+static int ir_rc5_encode(enum rc_type protocol, u32 scancode,
+			 struct ir_raw_event *events, unsigned int max)
+{
+	int ret;
+	struct ir_raw_event *e = events;
+	unsigned int data, xdata, command, commandx, system, pre_space_data;
+
+	/* Detect protocol and convert scancode to raw data */
+	if (protocol == RC_TYPE_RC5) {
+		/* decode scancode */
+		command  = (scancode & 0x003f) >> 0;
+		commandx = (scancode & 0x0040) >> 6;
+		system   = (scancode & 0x1f00) >> 8;
+		/* encode data */
+		data = !commandx << 12 | system << 6 | command;
+
+		/* Modulate the data */
+		ret = ir_raw_gen_manchester(&e, max, &ir_rc5_timings,
+					    RC5_NBITS, data);
+		if (ret < 0)
+			return ret;
+	} else if (protocol == RC_TYPE_RC5X) {
+		/* decode scancode */
+		xdata    = (scancode & 0x00003f) >> 0;
+		command  = (scancode & 0x003f00) >> 8;
+		commandx = !(scancode & 0x004000);
+		system   = (scancode & 0x1f0000) >> 16;
+
+		/* encode data */
+		data = commandx << 18 | system << 12 | command << 6 | xdata;
+
+		/* Modulate the data */
+		pre_space_data = data >> (RC5X_NBITS - CHECK_RC5X_NBITS);
+		ret = ir_raw_gen_manchester(&e, max, &ir_rc5x_timings[0],
+					    CHECK_RC5X_NBITS, pre_space_data);
+		if (ret < 0)
+			return ret;
+		ret = ir_raw_gen_manchester(&e, max - (e - events),
+					    &ir_rc5x_timings[1],
+					    RC5X_NBITS - CHECK_RC5X_NBITS,
+					    data);
+		if (ret < 0)
+			return ret;
+	} else if (protocol == RC_TYPE_RC5_SZ) {
+		/* RC5-SZ scancode is raw enough for Manchester as it is */
+		ret = ir_raw_gen_manchester(&e, max, &ir_rc5_sz_timings,
+					    RC5_SZ_NBITS, scancode & 0x2fff);
+		if (ret < 0)
+			return ret;
+	} else {
+		return -EINVAL;
+	}
+
+	return e - events;
+}
+
 static struct ir_raw_handler rc5_handler = {
 	.protocols	= RC_BIT_RC5 | RC_BIT_RC5X | RC_BIT_RC5_SZ,
 	.decode		= ir_rc5_decode,
+	.encode		= ir_rc5_encode,
 };
 
 static int __init ir_rc5_decode_init(void)
-- 
2.9.3


  parent reply	other threads:[~2016-12-12 21:13 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-12-12 21:13 [PATCH v5 00/18] Use sysfs filter for winbond & nuvoton wakeup Sean Young
2016-12-12 21:13 ` [PATCH v5 01/18] [media] rc: change wakeup_protocols to list all protocol variants Sean Young
2016-12-12 21:13 ` [PATCH v5 06/18] [media] rc: rc-ir-raw: Add scancode encoder callback Sean Young
2016-12-12 21:13 ` [PATCH v5 07/18] [media] rc: rc-ir-raw: Add Manchester encoder (phase encoder) helper Sean Young
2016-12-12 21:13 ` [PATCH v5 02/18] [media] img-ir: use new wakeup_protocols sysfs mechanism Sean Young
2016-12-12 22:31   ` James Hogan
2016-12-13  7:54     ` Sean Young
2016-12-13 11:31       ` James Hogan
2016-12-12 21:13 ` [PATCH v5 03/18] [media] rc: Add scancode validation Sean Young
2016-12-12 21:13 ` [PATCH v5 08/18] [media] rc: rc-ir-raw: Add pulse-distance modulation helper Sean Young
2016-12-12 21:13 ` [PATCH v5 04/18] [media] winbond-cir: use sysfs wakeup filter Sean Young
2016-12-12 21:13 ` Sean Young [this message]
2016-12-12 21:13 ` [PATCH v5 10/18] [media] rc: ir-rc6-decoder: Add encode capability Sean Young
2016-12-12 21:13 ` [PATCH v5 11/18] [media] rc: ir-nec-decoder: " Sean Young
2016-12-12 21:13 ` [PATCH v5 12/18] [media] rc: ir-jvc-decoder: " Sean Young
2016-12-12 21:13 ` [PATCH v5 13/18] [media] rc: ir-sanyo-decoder: " Sean Young
2016-12-12 21:13 ` [PATCH v5 14/18] [media] rc: ir-sharp-decoder: " Sean Young
2016-12-12 21:13 ` [PATCH v5 05/18] [media] rc: raw IR drivers cannot handle cec, unknown or other Sean Young
2016-12-12 21:13 ` [PATCH v5 15/18] [media] rc: ir-sony-decoder: Add encode capability Sean Young
2016-12-12 21:13 ` [PATCH v5 16/18] [media] rc: rc-core: Add support for encode_wakeup drivers Sean Young
2016-12-12 21:13 ` [PATCH v5 17/18] [media] rc: rc-loopback: Add loopback of filter scancodes Sean Young
2016-12-12 21:13 ` [PATCH v5 18/18] [media] rc: nuvoton-cir: Add support wakeup via sysfs filter callback Sean Young

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=abb46ba342ed0cfd8345223a2c35e93287d03a35.1481575826.git.sean@mess.org \
    --to=sean@mess.org \
    --cc=a.seppala@gmail.com \
    --cc=david@hardeman.nu \
    --cc=james@albanarts.com \
    --cc=linux-media@vger.kernel.org \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).