Skip to content

Commit a0466f1

Browse files
amalonmchehab
authored andcommitted
[media] rc: ir-rc5-decoder: Add encode capability
Add the capability to encode RC-5, RC-5X and RC-5-SZ scancodes as raw events. The protocol is chosen based on the specified protocol mask, and whether all the required bits are set in the scancode mask, and none of the unused bits are set in the scancode data. For example a scancode filter with bit 16 set in both data and mask is unambiguously RC-5X. The Manchester modulation helper is used, and for RC-5X it is used twice with two sets of timings, the first with a short trailer space for the space in the middle, and the second with no leader so that it can continue the space. The encoding in RC-5-SZ first inserts a pulse and then simply utilizes the generic Manchester encoder available in rc-core. Signed-off-by: James Hogan <james@albanarts.com> Signed-off-by: Antti Seppälä <a.seppala@gmail.com> Cc: David Härdeman <david@hardeman.nu> Signed-off-by: Mauro Carvalho Chehab <mchehab@osg.samsung.com>
1 parent 1d971d9 commit a0466f1

File tree

1 file changed

+116
-0
lines changed

1 file changed

+116
-0
lines changed

drivers/media/rc/ir-rc5-decoder.c

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,9 +184,125 @@ static int ir_rc5_decode(struct rc_dev *dev, struct ir_raw_event ev)
184184
return -EINVAL;
185185
}
186186

187+
static struct ir_raw_timings_manchester ir_rc5_timings = {
188+
.leader = RC5_UNIT,
189+
.pulse_space_start = 0,
190+
.clock = RC5_UNIT,
191+
.trailer_space = RC5_UNIT * 10,
192+
};
193+
194+
static struct ir_raw_timings_manchester ir_rc5x_timings[2] = {
195+
{
196+
.leader = RC5_UNIT,
197+
.pulse_space_start = 0,
198+
.clock = RC5_UNIT,
199+
.trailer_space = RC5X_SPACE,
200+
},
201+
{
202+
.clock = RC5_UNIT,
203+
.trailer_space = RC5_UNIT * 10,
204+
},
205+
};
206+
207+
static struct ir_raw_timings_manchester ir_rc5_sz_timings = {
208+
.leader = RC5_UNIT,
209+
.pulse_space_start = 0,
210+
.clock = RC5_UNIT,
211+
.trailer_space = RC5_UNIT * 10,
212+
};
213+
214+
static int ir_rc5_validate_filter(const struct rc_scancode_filter *scancode,
215+
unsigned int important_bits)
216+
{
217+
/* all important bits of scancode should be set in mask */
218+
if (~scancode->mask & important_bits)
219+
return -EINVAL;
220+
/* extra bits in mask should be zero in data */
221+
if (scancode->mask & scancode->data & ~important_bits)
222+
return -EINVAL;
223+
return 0;
224+
}
225+
226+
/**
227+
* ir_rc5_encode() - Encode a scancode as a stream of raw events
228+
*
229+
* @protocols: allowed protocols
230+
* @scancode: scancode filter describing scancode (helps distinguish between
231+
* protocol subtypes when scancode is ambiguous)
232+
* @events: array of raw ir events to write into
233+
* @max: maximum size of @events
234+
*
235+
* Returns: The number of events written.
236+
* -ENOBUFS if there isn't enough space in the array to fit the
237+
* encoding. In this case all @max events will have been written.
238+
* -EINVAL if the scancode is ambiguous or invalid.
239+
*/
240+
static int ir_rc5_encode(u64 protocols,
241+
const struct rc_scancode_filter *scancode,
242+
struct ir_raw_event *events, unsigned int max)
243+
{
244+
int ret;
245+
struct ir_raw_event *e = events;
246+
unsigned int data, xdata, command, commandx, system;
247+
248+
/* Detect protocol and convert scancode to raw data */
249+
if (protocols & RC_BIT_RC5 &&
250+
!ir_rc5_validate_filter(scancode, 0x1f7f)) {
251+
/* decode scancode */
252+
command = (scancode->data & 0x003f) >> 0;
253+
commandx = (scancode->data & 0x0040) >> 6;
254+
system = (scancode->data & 0x1f00) >> 8;
255+
/* encode data */
256+
data = !commandx << 12 | system << 6 | command;
257+
258+
/* Modulate the data */
259+
ret = ir_raw_gen_manchester(&e, max, &ir_rc5_timings, RC5_NBITS,
260+
data);
261+
if (ret < 0)
262+
return ret;
263+
} else if (protocols & RC_BIT_RC5X &&
264+
!ir_rc5_validate_filter(scancode, 0x1f7f3f)) {
265+
/* decode scancode */
266+
xdata = (scancode->data & 0x00003f) >> 0;
267+
command = (scancode->data & 0x003f00) >> 8;
268+
commandx = (scancode->data & 0x004000) >> 14;
269+
system = (scancode->data & 0x1f0000) >> 16;
270+
/* commandx and system overlap, bits must match when encoded */
271+
if (commandx == (system & 0x1))
272+
return -EINVAL;
273+
/* encode data */
274+
data = 1 << 18 | system << 12 | command << 6 | xdata;
275+
276+
/* Modulate the data */
277+
ret = ir_raw_gen_manchester(&e, max, &ir_rc5x_timings[0],
278+
CHECK_RC5X_NBITS,
279+
data >> (RC5X_NBITS-CHECK_RC5X_NBITS));
280+
if (ret < 0)
281+
return ret;
282+
ret = ir_raw_gen_manchester(&e, max - (e - events),
283+
&ir_rc5x_timings[1],
284+
RC5X_NBITS - CHECK_RC5X_NBITS,
285+
data);
286+
if (ret < 0)
287+
return ret;
288+
} else if (protocols & RC_BIT_RC5_SZ &&
289+
!ir_rc5_validate_filter(scancode, 0x2fff)) {
290+
/* RC5-SZ scancode is raw enough for Manchester as it is */
291+
ret = ir_raw_gen_manchester(&e, max, &ir_rc5_sz_timings,
292+
RC5_SZ_NBITS, scancode->data & 0x2fff);
293+
if (ret < 0)
294+
return ret;
295+
} else {
296+
return -EINVAL;
297+
}
298+
299+
return e - events;
300+
}
301+
187302
static struct ir_raw_handler rc5_handler = {
188303
.protocols = RC_BIT_RC5 | RC_BIT_RC5X | RC_BIT_RC5_SZ,
189304
.decode = ir_rc5_decode,
305+
.encode = ir_rc5_encode,
190306
};
191307

192308
static int __init ir_rc5_decode_init(void)

0 commit comments

Comments
 (0)