2 * 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 <media/rc-core.h>
19 * rc_open - Opens a RC device
21 * @rdev: pointer to struct rc_dev.
23 int rc_open(struct rc_dev
*rdev
);
26 * rc_close - Closes a RC device
28 * @rdev: pointer to struct rc_dev.
30 void rc_close(struct rc_dev
*rdev
);
32 struct ir_raw_handler
{
33 struct list_head list
;
35 u64 protocols
; /* which are handled by this handler */
36 int (*decode
)(struct rc_dev
*dev
, struct ir_raw_event event
);
37 int (*encode
)(enum rc_proto protocol
, u32 scancode
,
38 struct ir_raw_event
*events
, unsigned int max
);
41 /* These two should only be used by the mce kbd decoder */
42 int (*raw_register
)(struct rc_dev
*dev
);
43 int (*raw_unregister
)(struct rc_dev
*dev
);
46 struct ir_raw_event_ctrl
{
47 struct list_head list
; /* to keep track of raw clients */
48 struct task_struct
*thread
;
49 /* fifo for the pulse/space durations */
50 DECLARE_KFIFO(kfifo
, struct ir_raw_event
, MAX_IR_EVENT_SIZE
);
51 ktime_t last_event
; /* when last event occurred */
52 struct rc_dev
*dev
; /* pointer to the parent rc_dev */
54 struct timer_list edge_handle
;
56 /* raw decoder state follows */
57 struct ir_raw_event prev_ev
;
58 struct ir_raw_event this_ev
;
102 unsigned int pulse_len
;
105 struct input_dev
*idev
;
106 struct timer_list rx_timeout
;
113 unsigned wanted_bits
;
122 /* macros for IR decoders */
123 static inline bool geq_margin(unsigned d1
, unsigned d2
, unsigned margin
)
125 return d1
> (d2
- margin
);
128 static inline bool eq_margin(unsigned d1
, unsigned d2
, unsigned margin
)
130 return ((d1
> (d2
- margin
)) && (d1
< (d2
+ margin
)));
133 static inline bool is_transition(struct ir_raw_event
*x
, struct ir_raw_event
*y
)
135 return x
->pulse
!= y
->pulse
;
138 static inline void decrease_duration(struct ir_raw_event
*ev
, unsigned duration
)
140 if (duration
> ev
->duration
)
143 ev
->duration
-= duration
;
146 /* Returns true if event is normal pulse/space event */
147 static inline bool is_timing_event(struct ir_raw_event ev
)
149 return !ev
.carrier_report
&& !ev
.reset
;
152 #define TO_US(duration) DIV_ROUND_CLOSEST((duration), 1000)
153 #define TO_STR(is_pulse) ((is_pulse) ? "pulse" : "space")
155 /* functions for IR encoders */
156 bool rc_validate_scancode(enum rc_proto proto
, u32 scancode
);
158 static inline void init_ir_raw_event_duration(struct ir_raw_event
*ev
,
162 init_ir_raw_event(ev
);
163 ev
->duration
= duration
;
168 * struct ir_raw_timings_manchester - Manchester coding timings
169 * @leader_pulse: duration of leader pulse (if any) 0 if continuing
171 * @leader_space: duration of leader space (if any)
172 * @clock: duration of each pulse/space in ns
173 * @invert: if set clock logic is inverted
174 * (0 = space + pulse, 1 = pulse + space)
175 * @trailer_space: duration of trailer space in ns
177 struct ir_raw_timings_manchester
{
178 unsigned int leader_pulse
;
179 unsigned int leader_space
;
181 unsigned int invert
:1;
182 unsigned int trailer_space
;
185 int ir_raw_gen_manchester(struct ir_raw_event
**ev
, unsigned int max
,
186 const struct ir_raw_timings_manchester
*timings
,
187 unsigned int n
, u64 data
);
190 * ir_raw_gen_pulse_space() - generate pulse and space raw events.
191 * @ev: Pointer to pointer to next free raw event.
192 * Will be incremented for each raw event written.
193 * @max: Pointer to number of raw events available in buffer.
194 * Will be decremented for each raw event written.
195 * @pulse_width: Width of pulse in ns.
196 * @space_width: Width of space in ns.
198 * Returns: 0 on success.
199 * -ENOBUFS if there isn't enough buffer space to write both raw
200 * events. In this case @max events will have been written.
202 static inline int ir_raw_gen_pulse_space(struct ir_raw_event
**ev
,
204 unsigned int pulse_width
,
205 unsigned int space_width
)
209 init_ir_raw_event_duration((*ev
)++, 1, pulse_width
);
212 init_ir_raw_event_duration((*ev
)++, 0, space_width
);
218 * struct ir_raw_timings_pd - pulse-distance modulation timings
219 * @header_pulse: duration of header pulse in ns (0 for none)
220 * @header_space: duration of header space in ns
221 * @bit_pulse: duration of bit pulse in ns
222 * @bit_space: duration of bit space (for logic 0 and 1) in ns
223 * @trailer_pulse: duration of trailer pulse in ns
224 * @trailer_space: duration of trailer space in ns
225 * @msb_first: 1 if most significant bit is sent first
227 struct ir_raw_timings_pd
{
228 unsigned int header_pulse
;
229 unsigned int header_space
;
230 unsigned int bit_pulse
;
231 unsigned int bit_space
[2];
232 unsigned int trailer_pulse
;
233 unsigned int trailer_space
;
234 unsigned int msb_first
:1;
237 int ir_raw_gen_pd(struct ir_raw_event
**ev
, unsigned int max
,
238 const struct ir_raw_timings_pd
*timings
,
239 unsigned int n
, u64 data
);
242 * struct ir_raw_timings_pl - pulse-length modulation timings
243 * @header_pulse: duration of header pulse in ns (0 for none)
244 * @bit_space: duration of bit space in ns
245 * @bit_pulse: duration of bit pulse (for logic 0 and 1) in ns
246 * @trailer_space: duration of trailer space in ns
247 * @msb_first: 1 if most significant bit is sent first
249 struct ir_raw_timings_pl
{
250 unsigned int header_pulse
;
251 unsigned int bit_space
;
252 unsigned int bit_pulse
[2];
253 unsigned int trailer_space
;
254 unsigned int msb_first
:1;
257 int ir_raw_gen_pl(struct ir_raw_event
**ev
, unsigned int max
,
258 const struct ir_raw_timings_pl
*timings
,
259 unsigned int n
, u64 data
);
262 * Routines from rc-raw.c to be used internally and by decoders
264 u64
ir_raw_get_allowed_protocols(void);
265 int ir_raw_event_prepare(struct rc_dev
*dev
);
266 int ir_raw_event_register(struct rc_dev
*dev
);
267 void ir_raw_event_free(struct rc_dev
*dev
);
268 void ir_raw_event_unregister(struct rc_dev
*dev
);
269 int ir_raw_handler_register(struct ir_raw_handler
*ir_raw_handler
);
270 void ir_raw_handler_unregister(struct ir_raw_handler
*ir_raw_handler
);
271 void ir_raw_load_modules(u64
*protocols
);
272 void ir_raw_init(void);
278 int lirc_dev_init(void);
279 void lirc_dev_exit(void);
280 void ir_lirc_raw_event(struct rc_dev
*dev
, struct ir_raw_event ev
);
281 void ir_lirc_scancode_event(struct rc_dev
*dev
, struct lirc_scancode
*lsc
);
282 int ir_lirc_register(struct rc_dev
*dev
);
283 void ir_lirc_unregister(struct rc_dev
*dev
);
285 static inline int lirc_dev_init(void) { return 0; }
286 static inline void lirc_dev_exit(void) {}
287 static inline void ir_lirc_raw_event(struct rc_dev
*dev
,
288 struct ir_raw_event ev
) { }
289 static inline void ir_lirc_scancode_event(struct rc_dev
*dev
,
290 struct lirc_scancode
*lsc
) { }
291 static inline int ir_lirc_register(struct rc_dev
*dev
) { return 0; }
292 static inline void ir_lirc_unregister(struct rc_dev
*dev
) { }
296 * Decoder initialization code
298 * Those load logic are called during ir-core init, and automatically
299 * loads the compiled decoders for their usage with IR raw events
302 #endif /* _RC_CORE_PRIV */