David Härdeman | 3fe29c8 | 2010-04-15 18:46:10 -0300 | [diff] [blame] | 1 | /* ir-sony-decoder.c - handle Sony IR Pulse/Space protocol |
| 2 | * |
| 3 | * Copyright (C) 2010 by David Härdeman <david@hardeman.nu> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation version 2 of the License. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | */ |
| 14 | |
| 15 | #include <linux/bitrev.h> |
Paul Gortmaker | 7a707b8 | 2011-07-03 14:03:12 -0400 | [diff] [blame] | 16 | #include <linux/module.h> |
Mauro Carvalho Chehab | f62de67 | 2010-11-09 23:09:57 -0300 | [diff] [blame] | 17 | #include "rc-core-priv.h" |
David Härdeman | 3fe29c8 | 2010-04-15 18:46:10 -0300 | [diff] [blame] | 18 | |
| 19 | #define SONY_UNIT 600000 /* ns */ |
| 20 | #define SONY_HEADER_PULSE (4 * SONY_UNIT) |
| 21 | #define SONY_HEADER_SPACE (1 * SONY_UNIT) |
| 22 | #define SONY_BIT_0_PULSE (1 * SONY_UNIT) |
| 23 | #define SONY_BIT_1_PULSE (2 * SONY_UNIT) |
| 24 | #define SONY_BIT_SPACE (1 * SONY_UNIT) |
| 25 | #define SONY_TRAILER_SPACE (10 * SONY_UNIT) /* minimum */ |
| 26 | |
David Härdeman | 3fe29c8 | 2010-04-15 18:46:10 -0300 | [diff] [blame] | 27 | enum sony_state { |
| 28 | STATE_INACTIVE, |
| 29 | STATE_HEADER_SPACE, |
| 30 | STATE_BIT_PULSE, |
| 31 | STATE_BIT_SPACE, |
| 32 | STATE_FINISHED, |
| 33 | }; |
| 34 | |
David Härdeman | 3fe29c8 | 2010-04-15 18:46:10 -0300 | [diff] [blame] | 35 | /** |
| 36 | * ir_sony_decode() - Decode one Sony pulse or space |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 37 | * @dev: the struct rc_dev descriptor of the device |
David Härdeman | 3fe29c8 | 2010-04-15 18:46:10 -0300 | [diff] [blame] | 38 | * @ev: the struct ir_raw_event descriptor of the pulse/space |
| 39 | * |
| 40 | * This function returns -EINVAL if the pulse violates the state machine |
| 41 | */ |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 42 | static int ir_sony_decode(struct rc_dev *dev, struct ir_raw_event ev) |
David Härdeman | 3fe29c8 | 2010-04-15 18:46:10 -0300 | [diff] [blame] | 43 | { |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 44 | struct sony_dec *data = &dev->raw->sony; |
David Härdeman | 3fe29c8 | 2010-04-15 18:46:10 -0300 | [diff] [blame] | 45 | u32 scancode; |
| 46 | u8 device, subdevice, function; |
| 47 | |
Mauro Carvalho Chehab | 52b6614 | 2010-11-17 14:20:52 -0300 | [diff] [blame] | 48 | if (!(dev->raw->enabled_protocols & RC_TYPE_SONY)) |
David Härdeman | 3fe29c8 | 2010-04-15 18:46:10 -0300 | [diff] [blame] | 49 | return 0; |
| 50 | |
Maxim Levitsky | 4651918 | 2010-10-16 19:56:28 -0300 | [diff] [blame] | 51 | if (!is_timing_event(ev)) { |
| 52 | if (ev.reset) |
| 53 | data->state = STATE_INACTIVE; |
David Härdeman | 3fe29c8 | 2010-04-15 18:46:10 -0300 | [diff] [blame] | 54 | return 0; |
| 55 | } |
| 56 | |
| 57 | if (!geq_margin(ev.duration, SONY_UNIT, SONY_UNIT / 2)) |
| 58 | goto out; |
| 59 | |
| 60 | IR_dprintk(2, "Sony decode started at state %d (%uus %s)\n", |
| 61 | data->state, TO_US(ev.duration), TO_STR(ev.pulse)); |
| 62 | |
| 63 | switch (data->state) { |
| 64 | |
| 65 | case STATE_INACTIVE: |
| 66 | if (!ev.pulse) |
| 67 | break; |
| 68 | |
| 69 | if (!eq_margin(ev.duration, SONY_HEADER_PULSE, SONY_UNIT / 2)) |
| 70 | break; |
| 71 | |
| 72 | data->count = 0; |
| 73 | data->state = STATE_HEADER_SPACE; |
| 74 | return 0; |
| 75 | |
| 76 | case STATE_HEADER_SPACE: |
| 77 | if (ev.pulse) |
| 78 | break; |
| 79 | |
| 80 | if (!eq_margin(ev.duration, SONY_HEADER_SPACE, SONY_UNIT / 2)) |
| 81 | break; |
| 82 | |
| 83 | data->state = STATE_BIT_PULSE; |
| 84 | return 0; |
| 85 | |
| 86 | case STATE_BIT_PULSE: |
| 87 | if (!ev.pulse) |
| 88 | break; |
| 89 | |
David Härdeman | c216369 | 2010-06-13 17:29:36 -0300 | [diff] [blame] | 90 | data->bits <<= 1; |
David Härdeman | 3fe29c8 | 2010-04-15 18:46:10 -0300 | [diff] [blame] | 91 | if (eq_margin(ev.duration, SONY_BIT_1_PULSE, SONY_UNIT / 2)) |
David Härdeman | c216369 | 2010-06-13 17:29:36 -0300 | [diff] [blame] | 92 | data->bits |= 1; |
David Härdeman | 3fe29c8 | 2010-04-15 18:46:10 -0300 | [diff] [blame] | 93 | else if (!eq_margin(ev.duration, SONY_BIT_0_PULSE, SONY_UNIT / 2)) |
| 94 | break; |
| 95 | |
| 96 | data->count++; |
| 97 | data->state = STATE_BIT_SPACE; |
| 98 | return 0; |
| 99 | |
| 100 | case STATE_BIT_SPACE: |
| 101 | if (ev.pulse) |
| 102 | break; |
| 103 | |
| 104 | if (!geq_margin(ev.duration, SONY_BIT_SPACE, SONY_UNIT / 2)) |
| 105 | break; |
| 106 | |
| 107 | decrease_duration(&ev, SONY_BIT_SPACE); |
| 108 | |
| 109 | if (!geq_margin(ev.duration, SONY_UNIT, SONY_UNIT / 2)) { |
| 110 | data->state = STATE_BIT_PULSE; |
| 111 | return 0; |
| 112 | } |
| 113 | |
| 114 | data->state = STATE_FINISHED; |
| 115 | /* Fall through */ |
| 116 | |
| 117 | case STATE_FINISHED: |
| 118 | if (ev.pulse) |
| 119 | break; |
| 120 | |
| 121 | if (!geq_margin(ev.duration, SONY_TRAILER_SPACE, SONY_UNIT / 2)) |
| 122 | break; |
| 123 | |
| 124 | switch (data->count) { |
| 125 | case 12: |
David Härdeman | c216369 | 2010-06-13 17:29:36 -0300 | [diff] [blame] | 126 | device = bitrev8((data->bits << 3) & 0xF8); |
David Härdeman | 3fe29c8 | 2010-04-15 18:46:10 -0300 | [diff] [blame] | 127 | subdevice = 0; |
David Härdeman | c216369 | 2010-06-13 17:29:36 -0300 | [diff] [blame] | 128 | function = bitrev8((data->bits >> 4) & 0xFE); |
David Härdeman | 3fe29c8 | 2010-04-15 18:46:10 -0300 | [diff] [blame] | 129 | break; |
| 130 | case 15: |
David Härdeman | c216369 | 2010-06-13 17:29:36 -0300 | [diff] [blame] | 131 | device = bitrev8((data->bits >> 0) & 0xFF); |
David Härdeman | 3fe29c8 | 2010-04-15 18:46:10 -0300 | [diff] [blame] | 132 | subdevice = 0; |
David Härdeman | c216369 | 2010-06-13 17:29:36 -0300 | [diff] [blame] | 133 | function = bitrev8((data->bits >> 7) & 0xFD); |
David Härdeman | 3fe29c8 | 2010-04-15 18:46:10 -0300 | [diff] [blame] | 134 | break; |
| 135 | case 20: |
David Härdeman | c216369 | 2010-06-13 17:29:36 -0300 | [diff] [blame] | 136 | device = bitrev8((data->bits >> 5) & 0xF8); |
| 137 | subdevice = bitrev8((data->bits >> 0) & 0xFF); |
| 138 | function = bitrev8((data->bits >> 12) & 0xFE); |
David Härdeman | 3fe29c8 | 2010-04-15 18:46:10 -0300 | [diff] [blame] | 139 | break; |
| 140 | default: |
| 141 | IR_dprintk(1, "Sony invalid bitcount %u\n", data->count); |
| 142 | goto out; |
| 143 | } |
| 144 | |
| 145 | scancode = device << 16 | subdevice << 8 | function; |
| 146 | IR_dprintk(1, "Sony(%u) scancode 0x%05x\n", data->count, scancode); |
Mauro Carvalho Chehab | ca86674 | 2010-11-17 13:53:11 -0300 | [diff] [blame] | 147 | rc_keydown(dev, scancode, 0); |
David Härdeman | 3fe29c8 | 2010-04-15 18:46:10 -0300 | [diff] [blame] | 148 | data->state = STATE_INACTIVE; |
| 149 | return 0; |
| 150 | } |
| 151 | |
| 152 | out: |
| 153 | IR_dprintk(1, "Sony decode failed at state %d (%uus %s)\n", |
| 154 | data->state, TO_US(ev.duration), TO_STR(ev.pulse)); |
| 155 | data->state = STATE_INACTIVE; |
| 156 | return -EINVAL; |
| 157 | } |
| 158 | |
David Härdeman | 3fe29c8 | 2010-04-15 18:46:10 -0300 | [diff] [blame] | 159 | static struct ir_raw_handler sony_handler = { |
Mauro Carvalho Chehab | 52b6614 | 2010-11-17 14:20:52 -0300 | [diff] [blame] | 160 | .protocols = RC_TYPE_SONY, |
David Härdeman | 3fe29c8 | 2010-04-15 18:46:10 -0300 | [diff] [blame] | 161 | .decode = ir_sony_decode, |
David Härdeman | 3fe29c8 | 2010-04-15 18:46:10 -0300 | [diff] [blame] | 162 | }; |
| 163 | |
| 164 | static int __init ir_sony_decode_init(void) |
| 165 | { |
| 166 | ir_raw_handler_register(&sony_handler); |
| 167 | |
| 168 | printk(KERN_INFO "IR Sony protocol handler initialized\n"); |
| 169 | return 0; |
| 170 | } |
| 171 | |
| 172 | static void __exit ir_sony_decode_exit(void) |
| 173 | { |
| 174 | ir_raw_handler_unregister(&sony_handler); |
| 175 | } |
| 176 | |
| 177 | module_init(ir_sony_decode_init); |
| 178 | module_exit(ir_sony_decode_exit); |
| 179 | |
| 180 | MODULE_LICENSE("GPL"); |
| 181 | MODULE_AUTHOR("David Härdeman <david@hardeman.nu>"); |
| 182 | MODULE_DESCRIPTION("Sony IR protocol decoder"); |