1 // SPDX-License-Identifier: GPL-2.0
2 // Remote Controller core raw events header
4 // Copyright (C) 2010 by Mauro Carvalho Chehab
10 /* Define the max number of pulse/space transitions to buffer */
11 #define MAX_IR_EVENT_SIZE 512
13 #include <linux/slab.h>
14 #include <media/rc-core.h>
17 * rc_open - Opens a RC device
19 * @rdev: pointer to struct rc_dev.
21 int rc_open(struct rc_dev
*rdev
);
24 * rc_close - Closes a RC device
26 * @rdev: pointer to struct rc_dev.
28 void rc_close(struct rc_dev
*rdev
);
30 struct ir_raw_handler
{
31 struct list_head list
;
33 u64 protocols
; /* which are handled by this handler */
34 int (*decode
)(struct rc_dev
*dev
, struct ir_raw_event event
);
35 int (*encode
)(enum rc_proto protocol
, u32 scancode
,
36 struct ir_raw_event
*events
, unsigned int max
);
39 /* These two should only be used by the mce kbd decoder */
40 int (*raw_register
)(struct rc_dev
*dev
);
41 int (*raw_unregister
)(struct rc_dev
*dev
);
44 struct ir_raw_event_ctrl
{
45 struct list_head list
; /* to keep track of raw clients */
46 struct task_struct
*thread
;
47 /* fifo for the pulse/space durations */
48 DECLARE_KFIFO(kfifo
, struct ir_raw_event
, MAX_IR_EVENT_SIZE
);
49 ktime_t last_event
; /* when last event occurred */
50 struct rc_dev
*dev
; /* pointer to the parent rc_dev */
52 struct timer_list edge_handle
;
54 /* raw decoder state follows */
55 struct ir_raw_event prev_ev
;
56 struct ir_raw_event this_ev
;
100 unsigned int pulse_len
;
103 struct input_dev
*idev
;
104 struct timer_list rx_timeout
;
111 unsigned wanted_bits
;
120 /* macros for IR decoders */
121 static inline bool geq_margin(unsigned d1
, unsigned d2
, unsigned margin
)
123 return d1
> (d2
- margin
);
126 static inline bool eq_margin(unsigned d1
, unsigned d2
, unsigned margin
)
128 return ((d1
> (d2
- margin
)) && (d1
< (d2
+ margin
)));
131 static inline bool is_transition(struct ir_raw_event
*x
, struct ir_raw_event
*y
)
133 return x
->pulse
!= y
->pulse
;
136 static inline void decrease_duration(struct ir_raw_event
*ev
, unsigned duration
)
138 if (duration
> ev
->duration
)
141 ev
->duration
-= duration
;
144 /* Returns true if event is normal pulse/space event */
145 static inline bool is_timing_event(struct ir_raw_event ev
)
147 return !ev
.carrier_report
&& !ev
.reset
;
150 #define TO_US(duration) DIV_ROUND_CLOSEST((duration), 1000)
151 #define TO_STR(is_pulse) ((is_pulse) ? "pulse" : "space")
153 /* functions for IR encoders */
154 bool rc_validate_scancode(enum rc_proto proto
, u32 scancode
);
156 static inline void init_ir_raw_event_duration(struct ir_raw_event
*ev
,
160 init_ir_raw_event(ev
);
161 ev
->duration
= duration
;
166 * struct ir_raw_timings_manchester - Manchester coding timings
167 * @leader: duration of leader pulse (if any) 0 if continuing
168 * existing signal (see @pulse_space_start)
169 * @pulse_space_start: 1 for starting with pulse (0 for starting with space)
170 * @clock: duration of each pulse/space in ns
171 * @invert: if set clock logic is inverted
172 * (0 = space + pulse, 1 = pulse + space)
173 * @trailer_space: duration of trailer space in ns
175 struct ir_raw_timings_manchester
{
177 unsigned int pulse_space_start
:1;
179 unsigned int invert
:1;
180 unsigned int trailer_space
;
183 int ir_raw_gen_manchester(struct ir_raw_event
**ev
, unsigned int max
,
184 const struct ir_raw_timings_manchester
*timings
,
185 unsigned int n
, u64 data
);
188 * ir_raw_gen_pulse_space() - generate pulse and space raw events.
189 * @ev: Pointer to pointer to next free raw event.
190 * Will be incremented for each raw event written.
191 * @max: Pointer to number of raw events available in buffer.
192 * Will be decremented for each raw event written.
193 * @pulse_width: Width of pulse in ns.
194 * @space_width: Width of space in ns.
196 * Returns: 0 on success.
197 * -ENOBUFS if there isn't enough buffer space to write both raw
198 * events. In this case @max events will have been written.
200 static inline int ir_raw_gen_pulse_space(struct ir_raw_event
**ev
,
202 unsigned int pulse_width
,
203 unsigned int space_width
)
207 init_ir_raw_event_duration((*ev
)++, 1, pulse_width
);
210 init_ir_raw_event_duration((*ev
)++, 0, space_width
);
216 * struct ir_raw_timings_pd - pulse-distance modulation timings
217 * @header_pulse: duration of header pulse in ns (0 for none)
218 * @header_space: duration of header space in ns
219 * @bit_pulse: duration of bit pulse in ns
220 * @bit_space: duration of bit space (for logic 0 and 1) in ns
221 * @trailer_pulse: duration of trailer pulse in ns
222 * @trailer_space: duration of trailer space in ns
223 * @msb_first: 1 if most significant bit is sent first
225 struct ir_raw_timings_pd
{
226 unsigned int header_pulse
;
227 unsigned int header_space
;
228 unsigned int bit_pulse
;
229 unsigned int bit_space
[2];
230 unsigned int trailer_pulse
;
231 unsigned int trailer_space
;
232 unsigned int msb_first
:1;
235 int ir_raw_gen_pd(struct ir_raw_event
**ev
, unsigned int max
,
236 const struct ir_raw_timings_pd
*timings
,
237 unsigned int n
, u64 data
);
240 * struct ir_raw_timings_pl - pulse-length modulation timings
241 * @header_pulse: duration of header pulse in ns (0 for none)
242 * @bit_space: duration of bit space in ns
243 * @bit_pulse: duration of bit pulse (for logic 0 and 1) in ns
244 * @trailer_space: duration of trailer space in ns
245 * @msb_first: 1 if most significant bit is sent first
247 struct ir_raw_timings_pl
{
248 unsigned int header_pulse
;
249 unsigned int bit_space
;
250 unsigned int bit_pulse
[2];
251 unsigned int trailer_space
;
252 unsigned int msb_first
:1;
255 int ir_raw_gen_pl(struct ir_raw_event
**ev
, unsigned int max
,
256 const struct ir_raw_timings_pl
*timings
,
257 unsigned int n
, u64 data
);
260 * Routines from rc-raw.c to be used internally and by decoders
262 u64
ir_raw_get_allowed_protocols(void);
263 int ir_raw_event_prepare(struct rc_dev
*dev
);
264 int ir_raw_event_register(struct rc_dev
*dev
);
265 void ir_raw_event_free(struct rc_dev
*dev
);
266 void ir_raw_event_unregister(struct rc_dev
*dev
);
267 int ir_raw_handler_register(struct ir_raw_handler
*ir_raw_handler
);
268 void ir_raw_handler_unregister(struct ir_raw_handler
*ir_raw_handler
);
269 void ir_raw_load_modules(u64
*protocols
);
270 void ir_raw_init(void);
276 int lirc_dev_init(void);
277 void lirc_dev_exit(void);
278 void ir_lirc_raw_event(struct rc_dev
*dev
, struct ir_raw_event ev
);
279 void ir_lirc_scancode_event(struct rc_dev
*dev
, struct lirc_scancode
*lsc
);
280 int ir_lirc_register(struct rc_dev
*dev
);
281 void ir_lirc_unregister(struct rc_dev
*dev
);
283 static inline int lirc_dev_init(void) { return 0; }
284 static inline void lirc_dev_exit(void) {}
285 static inline void ir_lirc_raw_event(struct rc_dev
*dev
,
286 struct ir_raw_event ev
) { }
287 static inline void ir_lirc_scancode_event(struct rc_dev
*dev
,
288 struct lirc_scancode
*lsc
) { }
289 static inline int ir_lirc_register(struct rc_dev
*dev
) { return 0; }
290 static inline void ir_lirc_unregister(struct rc_dev
*dev
) { }
294 * Decoder initialization code
296 * Those load logic are called during ir-core init, and automatically
297 * loads the compiled decoders for their usage with IR raw events
300 #endif /* _RC_CORE_PRIV */