From patchwork Fri Mar 14 23:04:14 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: James Hogan X-Patchwork-Id: 23096 Received: from mail.tu-berlin.de ([130.149.7.33]) by www.linuxtv.org with esmtp (Exim 4.72) (envelope-from ) id 1WObC2-0003KQ-Am; Sat, 15 Mar 2014 00:07:10 +0100 X-tubIT-Incoming-IP: 209.132.180.67 Received: from vger.kernel.org ([209.132.180.67]) by mail.tu-berlin.de (exim-4.72/mailfrontend-6) with esmtp id 1WObC0-0008Ff-3N; Sat, 15 Mar 2014 00:07:10 +0100 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1755885AbaCNXHB (ORCPT + 1 other); Fri, 14 Mar 2014 19:07:01 -0400 Received: from mail-we0-f176.google.com ([74.125.82.176]:54833 "EHLO mail-we0-f176.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1755097AbaCNXHA (ORCPT ); Fri, 14 Mar 2014 19:07:00 -0400 Received: by mail-we0-f176.google.com with SMTP id x48so2644167wes.21 for ; Fri, 14 Mar 2014 16:06:59 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-type:content-transfer-encoding; bh=ol+hxRqEgi3JPj6TuG2c5Clxjg3L/lRMDkyoMkIJCo4=; b=INNgaEFYcsJ8MLQn/vycf7VGFrzc4uh532Gcium4ZXQjgbQ9C0OTbXK6dFa9uIvXOb Vdi+wiofTEOzA+IaJB7iA9lG4DIqydCIUMihE3MthJCf6ME+b17b24zeftQ2Vmx6OON1 wSuxeYsYDhjd06O4F3OhlwJQdR/9SR+jvmVeltZoIlT9Gi3mT4nCxEOwwARxrlgkH7hM sxmLIiJBru7rV0rG+72fnDEnE143G9fkkPOFElGldUHuHOejImtjiu27JEZLKh9nHFiu K4tfLOQApqcQgeSqdBWPm1xygAvXqD1ljxUPVVJSlRhifiCtb6ltFmMSEyCj4/Cbc7Go YM7Q== X-Gm-Message-State: ALoCoQlbqvK45Li/ZN5Ie2MOtvpOLHmDFbHZkmXQ75d4QLHOi6LrQipUc75aBL5l83zR6hnRT9nI X-Received: by 10.194.81.164 with SMTP id b4mr8912610wjy.2.1394838418926; Fri, 14 Mar 2014 16:06:58 -0700 (PDT) Received: from localhost.localdomain (jahogan.plus.com. [212.159.75.221]) by mx.google.com with ESMTPSA id dd3sm17220342wjb.9.2014.03.14.16.06.57 for (version=TLSv1.2 cipher=AES128-GCM-SHA256 bits=128/128); Fri, 14 Mar 2014 16:06:58 -0700 (PDT) From: James Hogan To: Mauro Carvalho Chehab , =?UTF-8?q?Antti=20Sepp=C3=A4l=C3=A4?= Cc: linux-media@vger.kernel.org, James Hogan , =?UTF-8?q?David=20H=C3=A4rdeman?= Subject: [PATCH v2 4/9] rc: ir-nec-decoder: Add encode capability Date: Fri, 14 Mar 2014 23:04:14 +0000 Message-Id: <1394838259-14260-5-git-send-email-james@albanarts.com> X-Mailer: git-send-email 1.8.3.2 In-Reply-To: <1394838259-14260-1-git-send-email-james@albanarts.com> References: <1394838259-14260-1-git-send-email-james@albanarts.com> MIME-Version: 1.0 Sender: linux-media-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-media@vger.kernel.org X-PMX-Version: 6.0.0.2142326, Antispam-Engine: 2.7.2.2107409, Antispam-Data: 2014.3.14.230021 X-PMX-Spam: Gauge=IIIIIIIII, Probability=9%, Report=' HTML_NO_HTTP 0.1, MULTIPLE_RCPTS 0.1, HTML_00_10 0.05, BODY_SIZE_4000_4999 0, BODY_SIZE_5000_LESS 0, BODY_SIZE_7000_LESS 0, CT_TEXT_PLAIN_UTF8_CAPS 0, URI_ENDS_IN_HTML 0, __ANY_URI 0, __CP_URI_IN_BODY 0, __CT 0, __CTE 0, __CT_TEXT_PLAIN 0, __HAS_FROM 0, __HAS_HTML 0, __HAS_MSGID 0, __HAS_X_MAILER 0, __HAS_X_MAILING_LIST 0, __HIGHBITS 0, __IN_REP_TO 0, __MIME_TEXT_ONLY 0, __MIME_VERSION 0, __MULTIPLE_RCPTS_CC_X2 0, __SANE_MSGID 0, __SUBJ_ALPHA_END 0, __TO_MALFORMED_2 0, __URI_NO_WWW 0, __URI_NS , __YOUTUBE_RCVD 0' Add the capability to encode NEC scancodes as raw events. The scancode_to_raw is pretty much taken from the img-ir NEC filter() callback, and modulation uses the pulse distance helper added in a previous commit. Signed-off-by: James Hogan Cc: Mauro Carvalho Chehab Cc: Antti Seppälä Cc: David Härdeman --- Changes in v2: - Update kerneldoc return values to reflect new API with -ENOBUFS return value when buffer is full and only a partial message was encoded. --- drivers/media/rc/ir-nec-decoder.c | 93 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) diff --git a/drivers/media/rc/ir-nec-decoder.c b/drivers/media/rc/ir-nec-decoder.c index 9de1791..813fa6b 100644 --- a/drivers/media/rc/ir-nec-decoder.c +++ b/drivers/media/rc/ir-nec-decoder.c @@ -203,9 +203,102 @@ static int ir_nec_decode(struct rc_dev *dev, struct ir_raw_event ev) return -EINVAL; } +/** + * ir_nec_scancode_to_raw() - encode an NEC scancode ready for modulation. + * @in: scancode filter describing a single NEC scancode. + * @raw: raw data to be modulated. + */ +static int ir_nec_scancode_to_raw(const struct rc_scancode_filter *in, + u32 *raw) +{ + unsigned int addr, addr_inv, data, data_inv; + + data = in->data & 0xff; + + if ((in->data | in->mask) & 0xff000000) { + /* 32-bit NEC (used by Apple and TiVo remotes) */ + /* scan encoding: aaAAddDD */ + if (in->mask != 0xffffffff) + return -EINVAL; + addr_inv = (in->data >> 24) & 0xff; + addr = (in->data >> 16) & 0xff; + data_inv = (in->data >> 8) & 0xff; + } else if ((in->data | in->mask) & 0x00ff0000) { + /* Extended NEC */ + /* scan encoding AAaaDD */ + if (in->mask != 0x00ffffff) + return -EINVAL; + addr = (in->data >> 16) & 0xff; + addr_inv = (in->data >> 8) & 0xff; + data_inv = data ^ 0xff; + } else { + /* Normal NEC */ + /* scan encoding: AADD */ + if (in->mask != 0x0000ffff) + return -EINVAL; + addr = (in->data >> 8) & 0xff; + addr_inv = addr ^ 0xff; + data_inv = data ^ 0xff; + } + + /* raw encoding: ddDDaaAA */ + *raw = data_inv << 24 | + data << 16 | + addr_inv << 8 | + addr; + return 0; +} + +static struct ir_raw_timings_pd ir_nec_timings = { + .header_pulse = NEC_HEADER_PULSE, + .header_space = NEC_HEADER_SPACE, + .bit_pulse = NEC_BIT_PULSE, + .bit_space[0] = NEC_BIT_0_SPACE, + .bit_space[1] = NEC_BIT_1_SPACE, + .trailer_pulse = NEC_TRAILER_PULSE, + .trailer_space = NEC_TRAILER_SPACE, + .msb_first = 0, +}; + +/** + * ir_nec_encode() - Encode a scancode as a stream of raw events + * + * @protocols: allowed protocols + * @scancode: scancode filter describing scancode (helps distinguish between + * protocol subtypes when scancode is ambiguous) + * @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_nec_encode(u64 protocols, + const struct rc_scancode_filter *scancode, + struct ir_raw_event *events, unsigned int max) +{ + struct ir_raw_event *e = events; + int ret; + u32 raw; + + /* Convert a NEC scancode to raw NEC data */ + ret = ir_nec_scancode_to_raw(scancode, &raw); + if (ret < 0) + return ret; + + /* Modulate the raw data using a pulse distance modulation */ + ret = ir_raw_gen_pd(&e, max, &ir_nec_timings, NEC_NBITS, raw); + if (ret < 0) + return ret; + + return e - events; +} + static struct ir_raw_handler nec_handler = { .protocols = RC_BIT_NEC, .decode = ir_nec_decode, + .encode = ir_nec_encode, }; static int __init ir_nec_decode_init(void)