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; |
Sean Young | 6d741bf | 2017-08-07 16:20:58 -0400 | [diff] [blame] | 45 | enum rc_proto protocol; |
David Härdeman | 3fe29c8 | 2010-04-15 18:46:10 -0300 | [diff] [blame] | 46 | u32 scancode; |
| 47 | u8 device, subdevice, function; |
| 48 | |
Maxim Levitsky | 4651918 | 2010-10-16 19:56:28 -0300 | [diff] [blame] | 49 | if (!is_timing_event(ev)) { |
| 50 | if (ev.reset) |
| 51 | data->state = STATE_INACTIVE; |
David Härdeman | 3fe29c8 | 2010-04-15 18:46:10 -0300 | [diff] [blame] | 52 | return 0; |
| 53 | } |
| 54 | |
| 55 | if (!geq_margin(ev.duration, SONY_UNIT, SONY_UNIT / 2)) |
| 56 | goto out; |
| 57 | |
| 58 | IR_dprintk(2, "Sony decode started at state %d (%uus %s)\n", |
| 59 | data->state, TO_US(ev.duration), TO_STR(ev.pulse)); |
| 60 | |
| 61 | switch (data->state) { |
| 62 | |
| 63 | case STATE_INACTIVE: |
| 64 | if (!ev.pulse) |
| 65 | break; |
| 66 | |
| 67 | if (!eq_margin(ev.duration, SONY_HEADER_PULSE, SONY_UNIT / 2)) |
| 68 | break; |
| 69 | |
| 70 | data->count = 0; |
| 71 | data->state = STATE_HEADER_SPACE; |
| 72 | return 0; |
| 73 | |
| 74 | case STATE_HEADER_SPACE: |
| 75 | if (ev.pulse) |
| 76 | break; |
| 77 | |
| 78 | if (!eq_margin(ev.duration, SONY_HEADER_SPACE, SONY_UNIT / 2)) |
| 79 | break; |
| 80 | |
| 81 | data->state = STATE_BIT_PULSE; |
| 82 | return 0; |
| 83 | |
| 84 | case STATE_BIT_PULSE: |
| 85 | if (!ev.pulse) |
| 86 | break; |
| 87 | |
David Härdeman | c216369 | 2010-06-13 17:29:36 -0300 | [diff] [blame] | 88 | data->bits <<= 1; |
David Härdeman | 3fe29c8 | 2010-04-15 18:46:10 -0300 | [diff] [blame] | 89 | 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] | 90 | data->bits |= 1; |
David Härdeman | 3fe29c8 | 2010-04-15 18:46:10 -0300 | [diff] [blame] | 91 | else if (!eq_margin(ev.duration, SONY_BIT_0_PULSE, SONY_UNIT / 2)) |
| 92 | break; |
| 93 | |
| 94 | data->count++; |
| 95 | data->state = STATE_BIT_SPACE; |
| 96 | return 0; |
| 97 | |
| 98 | case STATE_BIT_SPACE: |
| 99 | if (ev.pulse) |
| 100 | break; |
| 101 | |
| 102 | if (!geq_margin(ev.duration, SONY_BIT_SPACE, SONY_UNIT / 2)) |
| 103 | break; |
| 104 | |
| 105 | decrease_duration(&ev, SONY_BIT_SPACE); |
| 106 | |
| 107 | if (!geq_margin(ev.duration, SONY_UNIT, SONY_UNIT / 2)) { |
| 108 | data->state = STATE_BIT_PULSE; |
| 109 | return 0; |
| 110 | } |
| 111 | |
| 112 | data->state = STATE_FINISHED; |
| 113 | /* Fall through */ |
| 114 | |
| 115 | case STATE_FINISHED: |
| 116 | if (ev.pulse) |
| 117 | break; |
| 118 | |
| 119 | if (!geq_margin(ev.duration, SONY_TRAILER_SPACE, SONY_UNIT / 2)) |
| 120 | break; |
| 121 | |
| 122 | switch (data->count) { |
| 123 | case 12: |
Sean Young | 6d741bf | 2017-08-07 16:20:58 -0400 | [diff] [blame] | 124 | if (!(dev->enabled_protocols & RC_PROTO_BIT_SONY12)) |
Mauro Carvalho Chehab | abefe12 | 2015-04-28 12:23:56 -0300 | [diff] [blame] | 125 | goto finish_state_machine; |
| 126 | |
David Härdeman | c216369 | 2010-06-13 17:29:36 -0300 | [diff] [blame] | 127 | device = bitrev8((data->bits << 3) & 0xF8); |
David Härdeman | 3fe29c8 | 2010-04-15 18:46:10 -0300 | [diff] [blame] | 128 | subdevice = 0; |
David Härdeman | c216369 | 2010-06-13 17:29:36 -0300 | [diff] [blame] | 129 | function = bitrev8((data->bits >> 4) & 0xFE); |
Sean Young | 6d741bf | 2017-08-07 16:20:58 -0400 | [diff] [blame] | 130 | protocol = RC_PROTO_SONY12; |
David Härdeman | 3fe29c8 | 2010-04-15 18:46:10 -0300 | [diff] [blame] | 131 | break; |
| 132 | case 15: |
Sean Young | 6d741bf | 2017-08-07 16:20:58 -0400 | [diff] [blame] | 133 | if (!(dev->enabled_protocols & RC_PROTO_BIT_SONY15)) |
Mauro Carvalho Chehab | abefe12 | 2015-04-28 12:23:56 -0300 | [diff] [blame] | 134 | goto finish_state_machine; |
| 135 | |
David Härdeman | c216369 | 2010-06-13 17:29:36 -0300 | [diff] [blame] | 136 | device = bitrev8((data->bits >> 0) & 0xFF); |
David Härdeman | 3fe29c8 | 2010-04-15 18:46:10 -0300 | [diff] [blame] | 137 | subdevice = 0; |
James Hogan | ec9ee8e | 2012-02-20 06:13:54 -0300 | [diff] [blame] | 138 | function = bitrev8((data->bits >> 7) & 0xFE); |
Sean Young | 6d741bf | 2017-08-07 16:20:58 -0400 | [diff] [blame] | 139 | protocol = RC_PROTO_SONY15; |
David Härdeman | 3fe29c8 | 2010-04-15 18:46:10 -0300 | [diff] [blame] | 140 | break; |
| 141 | case 20: |
Sean Young | 6d741bf | 2017-08-07 16:20:58 -0400 | [diff] [blame] | 142 | if (!(dev->enabled_protocols & RC_PROTO_BIT_SONY20)) |
Mauro Carvalho Chehab | abefe12 | 2015-04-28 12:23:56 -0300 | [diff] [blame] | 143 | goto finish_state_machine; |
| 144 | |
David Härdeman | c216369 | 2010-06-13 17:29:36 -0300 | [diff] [blame] | 145 | device = bitrev8((data->bits >> 5) & 0xF8); |
| 146 | subdevice = bitrev8((data->bits >> 0) & 0xFF); |
| 147 | function = bitrev8((data->bits >> 12) & 0xFE); |
Sean Young | 6d741bf | 2017-08-07 16:20:58 -0400 | [diff] [blame] | 148 | protocol = RC_PROTO_SONY20; |
David Härdeman | 3fe29c8 | 2010-04-15 18:46:10 -0300 | [diff] [blame] | 149 | break; |
| 150 | default: |
| 151 | IR_dprintk(1, "Sony invalid bitcount %u\n", data->count); |
| 152 | goto out; |
| 153 | } |
| 154 | |
| 155 | scancode = device << 16 | subdevice << 8 | function; |
| 156 | IR_dprintk(1, "Sony(%u) scancode 0x%05x\n", data->count, scancode); |
David Härdeman | 120703f | 2014-04-03 20:31:30 -0300 | [diff] [blame] | 157 | rc_keydown(dev, protocol, scancode, 0); |
Mauro Carvalho Chehab | abefe12 | 2015-04-28 12:23:56 -0300 | [diff] [blame] | 158 | goto finish_state_machine; |
David Härdeman | 3fe29c8 | 2010-04-15 18:46:10 -0300 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | out: |
| 162 | IR_dprintk(1, "Sony decode failed at state %d (%uus %s)\n", |
| 163 | data->state, TO_US(ev.duration), TO_STR(ev.pulse)); |
| 164 | data->state = STATE_INACTIVE; |
| 165 | return -EINVAL; |
Mauro Carvalho Chehab | abefe12 | 2015-04-28 12:23:56 -0300 | [diff] [blame] | 166 | |
| 167 | finish_state_machine: |
| 168 | data->state = STATE_INACTIVE; |
| 169 | return 0; |
David Härdeman | 3fe29c8 | 2010-04-15 18:46:10 -0300 | [diff] [blame] | 170 | } |
| 171 | |
Sean Young | 103293b | 2016-12-06 18:33:57 -0200 | [diff] [blame] | 172 | static const struct ir_raw_timings_pl ir_sony_timings = { |
| 173 | .header_pulse = SONY_HEADER_PULSE, |
| 174 | .bit_space = SONY_BIT_SPACE, |
| 175 | .bit_pulse[0] = SONY_BIT_0_PULSE, |
| 176 | .bit_pulse[1] = SONY_BIT_1_PULSE, |
| 177 | .trailer_space = SONY_TRAILER_SPACE + SONY_BIT_SPACE, |
| 178 | .msb_first = 0, |
| 179 | }; |
| 180 | |
| 181 | /** |
| 182 | * ir_sony_encode() - Encode a scancode as a stream of raw events |
| 183 | * |
| 184 | * @protocol: protocol to encode |
| 185 | * @scancode: scancode to encode |
| 186 | * @events: array of raw ir events to write into |
| 187 | * @max: maximum size of @events |
| 188 | * |
| 189 | * Returns: The number of events written. |
| 190 | * -ENOBUFS if there isn't enough space in the array to fit the |
| 191 | * encoding. In this case all @max events will have been written. |
| 192 | */ |
Sean Young | 6d741bf | 2017-08-07 16:20:58 -0400 | [diff] [blame] | 193 | static int ir_sony_encode(enum rc_proto protocol, u32 scancode, |
Sean Young | 103293b | 2016-12-06 18:33:57 -0200 | [diff] [blame] | 194 | struct ir_raw_event *events, unsigned int max) |
| 195 | { |
| 196 | struct ir_raw_event *e = events; |
| 197 | u32 raw, len; |
| 198 | int ret; |
| 199 | |
Sean Young | 6d741bf | 2017-08-07 16:20:58 -0400 | [diff] [blame] | 200 | if (protocol == RC_PROTO_SONY12) { |
Sean Young | 103293b | 2016-12-06 18:33:57 -0200 | [diff] [blame] | 201 | raw = (scancode & 0x7f) | ((scancode & 0x1f0000) >> 9); |
| 202 | len = 12; |
Sean Young | 6d741bf | 2017-08-07 16:20:58 -0400 | [diff] [blame] | 203 | } else if (protocol == RC_PROTO_SONY15) { |
Sean Young | 103293b | 2016-12-06 18:33:57 -0200 | [diff] [blame] | 204 | raw = (scancode & 0x7f) | ((scancode & 0xff0000) >> 9); |
| 205 | len = 15; |
| 206 | } else { |
| 207 | raw = (scancode & 0x7f) | ((scancode & 0x1f0000) >> 9) | |
| 208 | ((scancode & 0xff00) << 4); |
| 209 | len = 20; |
| 210 | } |
| 211 | |
| 212 | ret = ir_raw_gen_pl(&e, max, &ir_sony_timings, len, raw); |
| 213 | if (ret < 0) |
| 214 | return ret; |
| 215 | |
| 216 | return e - events; |
| 217 | } |
| 218 | |
David Härdeman | 3fe29c8 | 2010-04-15 18:46:10 -0300 | [diff] [blame] | 219 | static struct ir_raw_handler sony_handler = { |
Sean Young | 6d741bf | 2017-08-07 16:20:58 -0400 | [diff] [blame] | 220 | .protocols = RC_PROTO_BIT_SONY12 | RC_PROTO_BIT_SONY15 | |
| 221 | RC_PROTO_BIT_SONY20, |
David Härdeman | 3fe29c8 | 2010-04-15 18:46:10 -0300 | [diff] [blame] | 222 | .decode = ir_sony_decode, |
Sean Young | 103293b | 2016-12-06 18:33:57 -0200 | [diff] [blame] | 223 | .encode = ir_sony_encode, |
Sean Young | cdfaa01 | 2017-02-25 06:51:30 -0500 | [diff] [blame^] | 224 | .carrier = 40000, |
David Härdeman | 3fe29c8 | 2010-04-15 18:46:10 -0300 | [diff] [blame] | 225 | }; |
| 226 | |
| 227 | static int __init ir_sony_decode_init(void) |
| 228 | { |
| 229 | ir_raw_handler_register(&sony_handler); |
| 230 | |
| 231 | printk(KERN_INFO "IR Sony protocol handler initialized\n"); |
| 232 | return 0; |
| 233 | } |
| 234 | |
| 235 | static void __exit ir_sony_decode_exit(void) |
| 236 | { |
| 237 | ir_raw_handler_unregister(&sony_handler); |
| 238 | } |
| 239 | |
| 240 | module_init(ir_sony_decode_init); |
| 241 | module_exit(ir_sony_decode_exit); |
| 242 | |
| 243 | MODULE_LICENSE("GPL"); |
| 244 | MODULE_AUTHOR("David Härdeman <david@hardeman.nu>"); |
| 245 | MODULE_DESCRIPTION("Sony IR protocol decoder"); |