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