Path: blob/master/drivers/media/rc/ir-sony-decoder.c
15109 views
/* ir-sony-decoder.c - handle Sony IR Pulse/Space protocol1*2* Copyright (C) 2010 by David Härdeman <[email protected]>3*4* This program is free software; you can redistribute it and/or modify5* it under the terms of the GNU General Public License as published by6* the Free Software Foundation version 2 of the License.7*8* This program is distributed in the hope that it will be useful,9* but WITHOUT ANY WARRANTY; without even the implied warranty of10* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the11* GNU General Public License for more details.12*/1314#include <linux/bitrev.h>15#include "rc-core-priv.h"1617#define SONY_UNIT 600000 /* ns */18#define SONY_HEADER_PULSE (4 * SONY_UNIT)19#define SONY_HEADER_SPACE (1 * SONY_UNIT)20#define SONY_BIT_0_PULSE (1 * SONY_UNIT)21#define SONY_BIT_1_PULSE (2 * SONY_UNIT)22#define SONY_BIT_SPACE (1 * SONY_UNIT)23#define SONY_TRAILER_SPACE (10 * SONY_UNIT) /* minimum */2425enum sony_state {26STATE_INACTIVE,27STATE_HEADER_SPACE,28STATE_BIT_PULSE,29STATE_BIT_SPACE,30STATE_FINISHED,31};3233/**34* ir_sony_decode() - Decode one Sony pulse or space35* @dev: the struct rc_dev descriptor of the device36* @ev: the struct ir_raw_event descriptor of the pulse/space37*38* This function returns -EINVAL if the pulse violates the state machine39*/40static int ir_sony_decode(struct rc_dev *dev, struct ir_raw_event ev)41{42struct sony_dec *data = &dev->raw->sony;43u32 scancode;44u8 device, subdevice, function;4546if (!(dev->raw->enabled_protocols & RC_TYPE_SONY))47return 0;4849if (!is_timing_event(ev)) {50if (ev.reset)51data->state = STATE_INACTIVE;52return 0;53}5455if (!geq_margin(ev.duration, SONY_UNIT, SONY_UNIT / 2))56goto out;5758IR_dprintk(2, "Sony decode started at state %d (%uus %s)\n",59data->state, TO_US(ev.duration), TO_STR(ev.pulse));6061switch (data->state) {6263case STATE_INACTIVE:64if (!ev.pulse)65break;6667if (!eq_margin(ev.duration, SONY_HEADER_PULSE, SONY_UNIT / 2))68break;6970data->count = 0;71data->state = STATE_HEADER_SPACE;72return 0;7374case STATE_HEADER_SPACE:75if (ev.pulse)76break;7778if (!eq_margin(ev.duration, SONY_HEADER_SPACE, SONY_UNIT / 2))79break;8081data->state = STATE_BIT_PULSE;82return 0;8384case STATE_BIT_PULSE:85if (!ev.pulse)86break;8788data->bits <<= 1;89if (eq_margin(ev.duration, SONY_BIT_1_PULSE, SONY_UNIT / 2))90data->bits |= 1;91else if (!eq_margin(ev.duration, SONY_BIT_0_PULSE, SONY_UNIT / 2))92break;9394data->count++;95data->state = STATE_BIT_SPACE;96return 0;9798case STATE_BIT_SPACE:99if (ev.pulse)100break;101102if (!geq_margin(ev.duration, SONY_BIT_SPACE, SONY_UNIT / 2))103break;104105decrease_duration(&ev, SONY_BIT_SPACE);106107if (!geq_margin(ev.duration, SONY_UNIT, SONY_UNIT / 2)) {108data->state = STATE_BIT_PULSE;109return 0;110}111112data->state = STATE_FINISHED;113/* Fall through */114115case STATE_FINISHED:116if (ev.pulse)117break;118119if (!geq_margin(ev.duration, SONY_TRAILER_SPACE, SONY_UNIT / 2))120break;121122switch (data->count) {123case 12:124device = bitrev8((data->bits << 3) & 0xF8);125subdevice = 0;126function = bitrev8((data->bits >> 4) & 0xFE);127break;128case 15:129device = bitrev8((data->bits >> 0) & 0xFF);130subdevice = 0;131function = bitrev8((data->bits >> 7) & 0xFD);132break;133case 20:134device = bitrev8((data->bits >> 5) & 0xF8);135subdevice = bitrev8((data->bits >> 0) & 0xFF);136function = bitrev8((data->bits >> 12) & 0xFE);137break;138default:139IR_dprintk(1, "Sony invalid bitcount %u\n", data->count);140goto out;141}142143scancode = device << 16 | subdevice << 8 | function;144IR_dprintk(1, "Sony(%u) scancode 0x%05x\n", data->count, scancode);145rc_keydown(dev, scancode, 0);146data->state = STATE_INACTIVE;147return 0;148}149150out:151IR_dprintk(1, "Sony decode failed at state %d (%uus %s)\n",152data->state, TO_US(ev.duration), TO_STR(ev.pulse));153data->state = STATE_INACTIVE;154return -EINVAL;155}156157static struct ir_raw_handler sony_handler = {158.protocols = RC_TYPE_SONY,159.decode = ir_sony_decode,160};161162static int __init ir_sony_decode_init(void)163{164ir_raw_handler_register(&sony_handler);165166printk(KERN_INFO "IR Sony protocol handler initialized\n");167return 0;168}169170static void __exit ir_sony_decode_exit(void)171{172ir_raw_handler_unregister(&sony_handler);173}174175module_init(ir_sony_decode_init);176module_exit(ir_sony_decode_exit);177178MODULE_LICENSE("GPL");179MODULE_AUTHOR("David Härdeman <[email protected]>");180MODULE_DESCRIPTION("Sony IR protocol decoder");181182183