1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Remote Controller core raw events header
5 * Copyright (C) 2010 by Mauro Carvalho Chehab
11 #define RC_DEV_MAX 256
12 /* Define the max number of pulse/space transitions to buffer */
13 #define MAX_IR_EVENT_SIZE 512
15 #include <linux/slab.h>
16 #include <uapi/linux/bpf.h>
17 #include <media/rc-core.h>
20 * rc_open - Opens a RC device
22 * @rdev: pointer to struct rc_dev.
24 int rc_open(struct rc_dev *rdev);
27 * rc_close - Closes a RC device
29 * @rdev: pointer to struct rc_dev.
31 void rc_close(struct rc_dev *rdev);
33 struct ir_raw_handler {
34 struct list_head list;
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);
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);
48 struct 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;
59 /* raw decoder state follows */
60 struct ir_raw_event prev_ev;
61 struct ir_raw_event this_ev;
63 #ifdef CONFIG_BPF_LIRC_MODE2
65 struct bpf_prog_array __rcu *progs;
67 #if IS_ENABLED(CONFIG_IR_NEC_DECODER)
76 #if IS_ENABLED(CONFIG_IR_RC5_DECODER)
84 #if IS_ENABLED(CONFIG_IR_RC6_DECODER)
94 #if IS_ENABLED(CONFIG_IR_SONY_DECODER)
101 #if IS_ENABLED(CONFIG_IR_JVC_DECODER)
111 #if IS_ENABLED(CONFIG_IR_SANYO_DECODER)
118 #if IS_ENABLED(CONFIG_IR_SHARP_DECODER)
123 unsigned int pulse_len;
126 #if IS_ENABLED(CONFIG_IR_MCE_KBD_DECODER)
128 /* locks key up timer */
130 struct timer_list rx_timeout;
135 unsigned wanted_bits;
138 #if IS_ENABLED(CONFIG_IR_XMP_DECODER)
145 #if IS_ENABLED(CONFIG_IR_IMON_DECODER)
154 #if IS_ENABLED(CONFIG_IR_RCMM_DECODER)
163 /* Mutex for locking raw IR processing and handler change */
164 extern struct mutex ir_raw_handler_lock;
166 /* macros for IR decoders */
167 static inline bool geq_margin(unsigned d1, unsigned d2, unsigned margin)
169 return d1 > (d2 - margin);
172 static inline bool eq_margin(unsigned d1, unsigned d2, unsigned margin)
174 return ((d1 > (d2 - margin)) && (d1 < (d2 + margin)));
177 static inline bool is_transition(struct ir_raw_event *x, struct ir_raw_event *y)
179 return x->pulse != y->pulse;
182 static inline void decrease_duration(struct ir_raw_event *ev, unsigned duration)
184 if (duration > ev->duration)
187 ev->duration -= duration;
190 /* Returns true if event is normal pulse/space event */
191 static inline bool is_timing_event(struct ir_raw_event ev)
193 return !ev.carrier_report && !ev.reset;
196 #define TO_STR(is_pulse) ((is_pulse) ? "pulse" : "space")
198 /* functions for IR encoders */
199 bool rc_validate_scancode(enum rc_proto proto, u32 scancode);
201 static inline void init_ir_raw_event_duration(struct ir_raw_event *ev,
205 *ev = (struct ir_raw_event) {
206 .duration = duration,
212 * struct ir_raw_timings_manchester - Manchester coding timings
213 * @leader_pulse: duration of leader pulse (if any) 0 if continuing
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
221 struct ir_raw_timings_manchester {
222 unsigned int leader_pulse;
223 unsigned int leader_space;
225 unsigned int invert:1;
226 unsigned int trailer_space;
229 int 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);
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.
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.
246 static inline int ir_raw_gen_pulse_space(struct ir_raw_event **ev,
248 unsigned int pulse_width,
249 unsigned int space_width)
253 init_ir_raw_event_duration((*ev)++, 1, pulse_width);
256 init_ir_raw_event_duration((*ev)++, 0, space_width);
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
271 struct 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;
281 int 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);
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
293 struct 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;
301 int 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);
306 * Routines from rc-raw.c to be used internally and by decoders
308 u64 ir_raw_get_allowed_protocols(void);
309 int ir_raw_event_prepare(struct rc_dev *dev);
310 int ir_raw_event_register(struct rc_dev *dev);
311 void ir_raw_event_free(struct rc_dev *dev);
312 void ir_raw_event_unregister(struct rc_dev *dev);
313 int ir_raw_handler_register(struct ir_raw_handler *ir_raw_handler);
314 void ir_raw_handler_unregister(struct ir_raw_handler *ir_raw_handler);
315 void ir_raw_load_modules(u64 *protocols);
316 void ir_raw_init(void);
322 int lirc_dev_init(void);
323 void lirc_dev_exit(void);
324 void lirc_raw_event(struct rc_dev *dev, struct ir_raw_event ev);
325 void lirc_scancode_event(struct rc_dev *dev, struct lirc_scancode *lsc);
326 int lirc_register(struct rc_dev *dev);
327 void lirc_unregister(struct rc_dev *dev);
328 struct rc_dev *rc_dev_get_from_fd(int fd);
330 static inline int lirc_dev_init(void) { return 0; }
331 static inline void lirc_dev_exit(void) {}
332 static inline void lirc_raw_event(struct rc_dev *dev,
333 struct ir_raw_event ev) { }
334 static inline void lirc_scancode_event(struct rc_dev *dev,
335 struct lirc_scancode *lsc) { }
336 static inline int lirc_register(struct rc_dev *dev) { return 0; }
337 static inline void lirc_unregister(struct rc_dev *dev) { }
343 #ifdef CONFIG_BPF_LIRC_MODE2
344 void lirc_bpf_free(struct rc_dev *dev);
345 void lirc_bpf_run(struct rc_dev *dev, u32 sample);
347 static inline void lirc_bpf_free(struct rc_dev *dev) { }
348 static inline void lirc_bpf_run(struct rc_dev *dev, u32 sample) { }
351 #endif /* _RC_CORE_PRIV */