Loading...
1/* SPDX-License-Identifier: GPL-2.0 */
2/*
3 * Remote Controller core raw events header
4 *
5 * Copyright (C) 2010 by Mauro Carvalho Chehab
6 */
7
8#ifndef _RC_CORE_PRIV
9#define _RC_CORE_PRIV
10
11#define RC_DEV_MAX 256
12/* Define the max number of pulse/space transitions to buffer */
13#define MAX_IR_EVENT_SIZE 512
14
15#include <linux/slab.h>
16#include <uapi/linux/bpf.h>
17#include <media/rc-core.h>
18
19/**
20 * rc_open - Opens a RC device
21 *
22 * @rdev: pointer to struct rc_dev.
23 */
24int rc_open(struct rc_dev *rdev);
25
26/**
27 * rc_close - Closes a RC device
28 *
29 * @rdev: pointer to struct rc_dev.
30 */
31void rc_close(struct rc_dev *rdev);
32
33struct ir_raw_handler {
34 struct list_head list;
35
36 u64 protocols; /* which are handled by this handler */
37 int (*decode)(struct rc_dev *dev, struct ir_raw_event event);
38 int (*encode)(enum rc_proto protocol, u32 scancode,
39 struct ir_raw_event *events, unsigned int max);
40 u32 carrier;
41 u32 min_timeout;
42
43 /* These two should only be used by the mce kbd decoder */
44 int (*raw_register)(struct rc_dev *dev);
45 int (*raw_unregister)(struct rc_dev *dev);
46};
47
48struct ir_raw_event_ctrl {
49 struct list_head list; /* to keep track of raw clients */
50 struct task_struct *thread;
51 /* fifo for the pulse/space durations */
52 DECLARE_KFIFO(kfifo, struct ir_raw_event, MAX_IR_EVENT_SIZE);
53 ktime_t last_event; /* when last event occurred */
54 struct rc_dev *dev; /* pointer to the parent rc_dev */
55 /* handle delayed ir_raw_event_store_edge processing */
56 spinlock_t edge_spinlock;
57 struct timer_list edge_handle;
58
59 /* raw decoder state follows */
60 struct ir_raw_event prev_ev;
61 struct ir_raw_event this_ev;
62
63#ifdef CONFIG_BPF_LIRC_MODE2
64 u32 bpf_sample;
65 struct bpf_prog_array __rcu *progs;
66#endif
67#if IS_ENABLED(CONFIG_IR_NEC_DECODER)
68 struct nec_dec {
69 int state;
70 unsigned count;
71 u32 bits;
72 bool is_nec_x;
73 bool necx_repeat;
74 } nec;
75#endif
76#if IS_ENABLED(CONFIG_IR_RC5_DECODER)
77 struct rc5_dec {
78 int state;
79 u32 bits;
80 unsigned count;
81 bool is_rc5x;
82 } rc5;
83#endif
84#if IS_ENABLED(CONFIG_IR_RC6_DECODER)
85 struct rc6_dec {
86 int state;
87 u8 header;
88 u32 body;
89 bool toggle;
90 unsigned count;
91 unsigned wanted_bits;
92 } rc6;
93#endif
94#if IS_ENABLED(CONFIG_IR_SONY_DECODER)
95 struct sony_dec {
96 int state;
97 u32 bits;
98 unsigned count;
99 } sony;
100#endif
101#if IS_ENABLED(CONFIG_IR_JVC_DECODER)
102 struct jvc_dec {
103 int state;
104 u16 bits;
105 u16 old_bits;
106 unsigned count;
107 bool first;
108 bool toggle;
109 } jvc;
110#endif
111#if IS_ENABLED(CONFIG_IR_SANYO_DECODER)
112 struct sanyo_dec {
113 int state;
114 unsigned count;
115 u64 bits;
116 } sanyo;
117#endif
118#if IS_ENABLED(CONFIG_IR_SHARP_DECODER)
119 struct sharp_dec {
120 int state;
121 unsigned count;
122 u32 bits;
123 unsigned int pulse_len;
124 } sharp;
125#endif
126#if IS_ENABLED(CONFIG_IR_MCE_KBD_DECODER)
127 struct mce_kbd_dec {
128 /* locks key up timer */
129 spinlock_t keylock;
130 struct timer_list rx_timeout;
131 int state;
132 u8 header;
133 u32 body;
134 unsigned count;
135 unsigned wanted_bits;
136 } mce_kbd;
137#endif
138#if IS_ENABLED(CONFIG_IR_XMP_DECODER)
139 struct xmp_dec {
140 int state;
141 unsigned count;
142 u32 durations[16];
143 } xmp;
144#endif
145#if IS_ENABLED(CONFIG_IR_IMON_DECODER)
146 struct imon_dec {
147 int state;
148 int count;
149 int last_chk;
150 unsigned int bits;
151 bool stick_keyboard;
152 } imon;
153#endif
154#if IS_ENABLED(CONFIG_IR_RCMM_DECODER)
155 struct rcmm_dec {
156 int state;
157 unsigned int count;
158 u32 bits;
159 } rcmm;
160#endif
161};
162
163/* Mutex for locking raw IR processing and handler change */
164extern struct mutex ir_raw_handler_lock;
165
166/* macros for IR decoders */
167static inline bool geq_margin(unsigned d1, unsigned d2, unsigned margin)
168{
169 return d1 > (d2 - margin);
170}
171
172static inline bool eq_margin(unsigned d1, unsigned d2, unsigned margin)
173{
174 return ((d1 > (d2 - margin)) && (d1 < (d2 + margin)));
175}
176
177static inline bool is_transition(struct ir_raw_event *x, struct ir_raw_event *y)
178{
179 return x->pulse != y->pulse;
180}
181
182static inline void decrease_duration(struct ir_raw_event *ev, unsigned duration)
183{
184 if (duration > ev->duration)
185 ev->duration = 0;
186 else
187 ev->duration -= duration;
188}
189
190/* Returns true if event is normal pulse/space event */
191static inline bool is_timing_event(struct ir_raw_event ev)
192{
193 return !ev.carrier_report && !ev.overflow;
194}
195
196#define TO_STR(is_pulse) ((is_pulse) ? "pulse" : "space")
197
198/* functions for IR encoders */
199bool rc_validate_scancode(enum rc_proto proto, u32 scancode);
200
201static inline void init_ir_raw_event_duration(struct ir_raw_event *ev,
202 unsigned int pulse,
203 u32 duration)
204{
205 *ev = (struct ir_raw_event) {
206 .duration = duration,
207 .pulse = pulse
208 };
209}
210
211/**
212 * struct ir_raw_timings_manchester - Manchester coding timings
213 * @leader_pulse: duration of leader pulse (if any) 0 if continuing
214 * existing signal
215 * @leader_space: duration of leader space (if any)
216 * @clock: duration of each pulse/space in ns
217 * @invert: if set clock logic is inverted
218 * (0 = space + pulse, 1 = pulse + space)
219 * @trailer_space: duration of trailer space in ns
220 */
221struct ir_raw_timings_manchester {
222 unsigned int leader_pulse;
223 unsigned int leader_space;
224 unsigned int clock;
225 unsigned int invert:1;
226 unsigned int trailer_space;
227};
228
229int ir_raw_gen_manchester(struct ir_raw_event **ev, unsigned int max,
230 const struct ir_raw_timings_manchester *timings,
231 unsigned int n, u64 data);
232
233/**
234 * ir_raw_gen_pulse_space() - generate pulse and space raw events.
235 * @ev: Pointer to pointer to next free raw event.
236 * Will be incremented for each raw event written.
237 * @max: Pointer to number of raw events available in buffer.
238 * Will be decremented for each raw event written.
239 * @pulse_width: Width of pulse in ns.
240 * @space_width: Width of space in ns.
241 *
242 * Returns: 0 on success.
243 * -ENOBUFS if there isn't enough buffer space to write both raw
244 * events. In this case @max events will have been written.
245 */
246static inline int ir_raw_gen_pulse_space(struct ir_raw_event **ev,
247 unsigned int *max,
248 unsigned int pulse_width,
249 unsigned int space_width)
250{
251 if (!*max)
252 return -ENOBUFS;
253 init_ir_raw_event_duration((*ev)++, 1, pulse_width);
254 if (!--*max)
255 return -ENOBUFS;
256 init_ir_raw_event_duration((*ev)++, 0, space_width);
257 --*max;
258 return 0;
259}
260
261/**
262 * struct ir_raw_timings_pd - pulse-distance modulation timings
263 * @header_pulse: duration of header pulse in ns (0 for none)
264 * @header_space: duration of header space in ns
265 * @bit_pulse: duration of bit pulse in ns
266 * @bit_space: duration of bit space (for logic 0 and 1) in ns
267 * @trailer_pulse: duration of trailer pulse in ns
268 * @trailer_space: duration of trailer space in ns
269 * @msb_first: 1 if most significant bit is sent first
270 */
271struct ir_raw_timings_pd {
272 unsigned int header_pulse;
273 unsigned int header_space;
274 unsigned int bit_pulse;
275 unsigned int bit_space[2];
276 unsigned int trailer_pulse;
277 unsigned int trailer_space;
278 unsigned int msb_first:1;
279};
280
281int ir_raw_gen_pd(struct ir_raw_event **ev, unsigned int max,
282 const struct ir_raw_timings_pd *timings,
283 unsigned int n, u64 data);
284
285/**
286 * struct ir_raw_timings_pl - pulse-length modulation timings
287 * @header_pulse: duration of header pulse in ns (0 for none)
288 * @bit_space: duration of bit space in ns
289 * @bit_pulse: duration of bit pulse (for logic 0 and 1) in ns
290 * @trailer_space: duration of trailer space in ns
291 * @msb_first: 1 if most significant bit is sent first
292 */
293struct ir_raw_timings_pl {
294 unsigned int header_pulse;
295 unsigned int bit_space;
296 unsigned int bit_pulse[2];
297 unsigned int trailer_space;
298 unsigned int msb_first:1;
299};
300
301int ir_raw_gen_pl(struct ir_raw_event **ev, unsigned int max,
302 const struct ir_raw_timings_pl *timings,
303 unsigned int n, u64 data);
304
305/*
306 * Routines from rc-raw.c to be used internally and by decoders
307 */
308u64 ir_raw_get_allowed_protocols(void);
309int ir_raw_event_prepare(struct rc_dev *dev);
310int ir_raw_event_register(struct rc_dev *dev);
311void ir_raw_event_free(struct rc_dev *dev);
312void ir_raw_event_unregister(struct rc_dev *dev);
313int ir_raw_handler_register(struct ir_raw_handler *ir_raw_handler);
314void ir_raw_handler_unregister(struct ir_raw_handler *ir_raw_handler);
315void ir_raw_load_modules(u64 *protocols);
316void ir_raw_init(void);
317
318/*
319 * lirc interface
320 */
321#ifdef CONFIG_LIRC
322int lirc_dev_init(void);
323void lirc_dev_exit(void);
324void lirc_raw_event(struct rc_dev *dev, struct ir_raw_event ev);
325void lirc_scancode_event(struct rc_dev *dev, struct lirc_scancode *lsc);
326int lirc_register(struct rc_dev *dev);
327void lirc_unregister(struct rc_dev *dev);
328struct rc_dev *rc_dev_get_from_fd(int fd);
329#else
330static inline int lirc_dev_init(void) { return 0; }
331static inline void lirc_dev_exit(void) {}
332static inline void lirc_raw_event(struct rc_dev *dev,
333 struct ir_raw_event ev) { }
334static inline void lirc_scancode_event(struct rc_dev *dev,
335 struct lirc_scancode *lsc) { }
336static inline int lirc_register(struct rc_dev *dev) { return 0; }
337static inline void lirc_unregister(struct rc_dev *dev) { }
338#endif
339
340/*
341 * bpf interface
342 */
343#ifdef CONFIG_BPF_LIRC_MODE2
344void lirc_bpf_free(struct rc_dev *dev);
345void lirc_bpf_run(struct rc_dev *dev, u32 sample);
346#else
347static inline void lirc_bpf_free(struct rc_dev *dev) { }
348static inline void lirc_bpf_run(struct rc_dev *dev, u32 sample) { }
349#endif
350
351#endif /* _RC_CORE_PRIV */
1/*
2 * Remote Controller core raw events header
3 *
4 * Copyright (C) 2010 by Mauro Carvalho Chehab <mchehab@redhat.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 */
15
16#ifndef _RC_CORE_PRIV
17#define _RC_CORE_PRIV
18
19#include <linux/slab.h>
20#include <linux/spinlock.h>
21#include <media/rc-core.h>
22
23struct ir_raw_handler {
24 struct list_head list;
25
26 u64 protocols; /* which are handled by this handler */
27 int (*decode)(struct rc_dev *dev, struct ir_raw_event event);
28
29 /* These two should only be used by the lirc decoder */
30 int (*raw_register)(struct rc_dev *dev);
31 int (*raw_unregister)(struct rc_dev *dev);
32};
33
34struct ir_raw_event_ctrl {
35 struct list_head list; /* to keep track of raw clients */
36 struct task_struct *thread;
37 spinlock_t lock;
38 struct kfifo_rec_ptr_1 kfifo; /* fifo for the pulse/space durations */
39 ktime_t last_event; /* when last event occurred */
40 enum raw_event_type last_type; /* last event type */
41 struct rc_dev *dev; /* pointer to the parent rc_dev */
42 u64 enabled_protocols; /* enabled raw protocol decoders */
43
44 /* raw decoder state follows */
45 struct ir_raw_event prev_ev;
46 struct ir_raw_event this_ev;
47 struct nec_dec {
48 int state;
49 unsigned count;
50 u32 bits;
51 bool is_nec_x;
52 bool necx_repeat;
53 } nec;
54 struct rc5_dec {
55 int state;
56 u32 bits;
57 unsigned count;
58 unsigned wanted_bits;
59 } rc5;
60 struct rc6_dec {
61 int state;
62 u8 header;
63 u32 body;
64 bool toggle;
65 unsigned count;
66 unsigned wanted_bits;
67 } rc6;
68 struct sony_dec {
69 int state;
70 u32 bits;
71 unsigned count;
72 } sony;
73 struct jvc_dec {
74 int state;
75 u16 bits;
76 u16 old_bits;
77 unsigned count;
78 bool first;
79 bool toggle;
80 } jvc;
81 struct rc5_sz_dec {
82 int state;
83 u32 bits;
84 unsigned count;
85 unsigned wanted_bits;
86 } rc5_sz;
87 struct sanyo_dec {
88 int state;
89 unsigned count;
90 u64 bits;
91 } sanyo;
92 struct mce_kbd_dec {
93 struct input_dev *idev;
94 struct timer_list rx_timeout;
95 char name[64];
96 char phys[64];
97 int state;
98 u8 header;
99 u32 body;
100 unsigned count;
101 unsigned wanted_bits;
102 } mce_kbd;
103 struct lirc_codec {
104 struct rc_dev *dev;
105 struct lirc_driver *drv;
106 int carrier_low;
107
108 ktime_t gap_start;
109 u64 gap_duration;
110 bool gap;
111 bool send_timeout_reports;
112
113 } lirc;
114};
115
116/* macros for IR decoders */
117static inline bool geq_margin(unsigned d1, unsigned d2, unsigned margin)
118{
119 return d1 > (d2 - margin);
120}
121
122static inline bool eq_margin(unsigned d1, unsigned d2, unsigned margin)
123{
124 return ((d1 > (d2 - margin)) && (d1 < (d2 + margin)));
125}
126
127static inline bool is_transition(struct ir_raw_event *x, struct ir_raw_event *y)
128{
129 return x->pulse != y->pulse;
130}
131
132static inline void decrease_duration(struct ir_raw_event *ev, unsigned duration)
133{
134 if (duration > ev->duration)
135 ev->duration = 0;
136 else
137 ev->duration -= duration;
138}
139
140/* Returns true if event is normal pulse/space event */
141static inline bool is_timing_event(struct ir_raw_event ev)
142{
143 return !ev.carrier_report && !ev.reset;
144}
145
146#define TO_US(duration) DIV_ROUND_CLOSEST((duration), 1000)
147#define TO_STR(is_pulse) ((is_pulse) ? "pulse" : "space")
148
149/*
150 * Routines from rc-raw.c to be used internally and by decoders
151 */
152u64 ir_raw_get_allowed_protocols(void);
153int ir_raw_event_register(struct rc_dev *dev);
154void ir_raw_event_unregister(struct rc_dev *dev);
155int ir_raw_handler_register(struct ir_raw_handler *ir_raw_handler);
156void ir_raw_handler_unregister(struct ir_raw_handler *ir_raw_handler);
157void ir_raw_init(void);
158
159/*
160 * Decoder initialization code
161 *
162 * Those load logic are called during ir-core init, and automatically
163 * loads the compiled decoders for their usage with IR raw events
164 */
165
166/* from ir-nec-decoder.c */
167#ifdef CONFIG_IR_NEC_DECODER_MODULE
168#define load_nec_decode() request_module("ir-nec-decoder")
169#else
170static inline void load_nec_decode(void) { }
171#endif
172
173/* from ir-rc5-decoder.c */
174#ifdef CONFIG_IR_RC5_DECODER_MODULE
175#define load_rc5_decode() request_module("ir-rc5-decoder")
176#else
177static inline void load_rc5_decode(void) { }
178#endif
179
180/* from ir-rc6-decoder.c */
181#ifdef CONFIG_IR_RC6_DECODER_MODULE
182#define load_rc6_decode() request_module("ir-rc6-decoder")
183#else
184static inline void load_rc6_decode(void) { }
185#endif
186
187/* from ir-jvc-decoder.c */
188#ifdef CONFIG_IR_JVC_DECODER_MODULE
189#define load_jvc_decode() request_module("ir-jvc-decoder")
190#else
191static inline void load_jvc_decode(void) { }
192#endif
193
194/* from ir-sony-decoder.c */
195#ifdef CONFIG_IR_SONY_DECODER_MODULE
196#define load_sony_decode() request_module("ir-sony-decoder")
197#else
198static inline void load_sony_decode(void) { }
199#endif
200
201/* from ir-sanyo-decoder.c */
202#ifdef CONFIG_IR_SANYO_DECODER_MODULE
203#define load_sanyo_decode() request_module("ir-sanyo-decoder")
204#else
205static inline void load_sanyo_decode(void) { }
206#endif
207
208/* from ir-mce_kbd-decoder.c */
209#ifdef CONFIG_IR_MCE_KBD_DECODER_MODULE
210#define load_mce_kbd_decode() request_module("ir-mce_kbd-decoder")
211#else
212static inline void load_mce_kbd_decode(void) { }
213#endif
214
215/* from ir-lirc-codec.c */
216#ifdef CONFIG_IR_LIRC_CODEC_MODULE
217#define load_lirc_codec() request_module("ir-lirc-codec")
218#else
219static inline void load_lirc_codec(void) { }
220#endif
221
222
223#endif /* _RC_CORE_PRIV */