]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blob - drivers/media/rc/rc-core-priv.h
media: rc: move ir-lirc-codec.c contents into lirc_dev.c
[mirror_ubuntu-hirsute-kernel.git] / drivers / media / rc / rc-core-priv.h
1 // SPDX-License-Identifier: GPL-2.0
2 // Remote Controller core raw events header
3 //
4 // Copyright (C) 2010 by Mauro Carvalho Chehab
5
6 #ifndef _RC_CORE_PRIV
7 #define _RC_CORE_PRIV
8
9 #define RC_DEV_MAX 256
10 /* Define the max number of pulse/space transitions to buffer */
11 #define MAX_IR_EVENT_SIZE 512
12
13 #include <linux/slab.h>
14 #include <media/rc-core.h>
15
16 /**
17 * rc_open - Opens a RC device
18 *
19 * @rdev: pointer to struct rc_dev.
20 */
21 int rc_open(struct rc_dev *rdev);
22
23 /**
24 * rc_close - Closes a RC device
25 *
26 * @rdev: pointer to struct rc_dev.
27 */
28 void rc_close(struct rc_dev *rdev);
29
30 struct ir_raw_handler {
31 struct list_head list;
32
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);
37 u32 carrier;
38
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);
42 };
43
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 */
51 /* edge driver */
52 struct timer_list edge_handle;
53
54 /* raw decoder state follows */
55 struct ir_raw_event prev_ev;
56 struct ir_raw_event this_ev;
57 struct nec_dec {
58 int state;
59 unsigned count;
60 u32 bits;
61 bool is_nec_x;
62 bool necx_repeat;
63 } nec;
64 struct rc5_dec {
65 int state;
66 u32 bits;
67 unsigned count;
68 bool is_rc5x;
69 } rc5;
70 struct rc6_dec {
71 int state;
72 u8 header;
73 u32 body;
74 bool toggle;
75 unsigned count;
76 unsigned wanted_bits;
77 } rc6;
78 struct sony_dec {
79 int state;
80 u32 bits;
81 unsigned count;
82 } sony;
83 struct jvc_dec {
84 int state;
85 u16 bits;
86 u16 old_bits;
87 unsigned count;
88 bool first;
89 bool toggle;
90 } jvc;
91 struct sanyo_dec {
92 int state;
93 unsigned count;
94 u64 bits;
95 } sanyo;
96 struct sharp_dec {
97 int state;
98 unsigned count;
99 u32 bits;
100 unsigned int pulse_len;
101 } sharp;
102 struct mce_kbd_dec {
103 struct input_dev *idev;
104 struct timer_list rx_timeout;
105 char name[64];
106 char phys[64];
107 int state;
108 u8 header;
109 u32 body;
110 unsigned count;
111 unsigned wanted_bits;
112 } mce_kbd;
113 struct xmp_dec {
114 int state;
115 unsigned count;
116 u32 durations[16];
117 } xmp;
118 };
119
120 /* macros for IR decoders */
121 static inline bool geq_margin(unsigned d1, unsigned d2, unsigned margin)
122 {
123 return d1 > (d2 - margin);
124 }
125
126 static inline bool eq_margin(unsigned d1, unsigned d2, unsigned margin)
127 {
128 return ((d1 > (d2 - margin)) && (d1 < (d2 + margin)));
129 }
130
131 static inline bool is_transition(struct ir_raw_event *x, struct ir_raw_event *y)
132 {
133 return x->pulse != y->pulse;
134 }
135
136 static inline void decrease_duration(struct ir_raw_event *ev, unsigned duration)
137 {
138 if (duration > ev->duration)
139 ev->duration = 0;
140 else
141 ev->duration -= duration;
142 }
143
144 /* Returns true if event is normal pulse/space event */
145 static inline bool is_timing_event(struct ir_raw_event ev)
146 {
147 return !ev.carrier_report && !ev.reset;
148 }
149
150 #define TO_US(duration) DIV_ROUND_CLOSEST((duration), 1000)
151 #define TO_STR(is_pulse) ((is_pulse) ? "pulse" : "space")
152
153 /* functions for IR encoders */
154 bool rc_validate_scancode(enum rc_proto proto, u32 scancode);
155
156 static inline void init_ir_raw_event_duration(struct ir_raw_event *ev,
157 unsigned int pulse,
158 u32 duration)
159 {
160 init_ir_raw_event(ev);
161 ev->duration = duration;
162 ev->pulse = pulse;
163 }
164
165 /**
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
174 */
175 struct ir_raw_timings_manchester {
176 unsigned int leader;
177 unsigned int pulse_space_start:1;
178 unsigned int clock;
179 unsigned int invert:1;
180 unsigned int trailer_space;
181 };
182
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);
186
187 /**
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.
195 *
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.
199 */
200 static inline int ir_raw_gen_pulse_space(struct ir_raw_event **ev,
201 unsigned int *max,
202 unsigned int pulse_width,
203 unsigned int space_width)
204 {
205 if (!*max)
206 return -ENOBUFS;
207 init_ir_raw_event_duration((*ev)++, 1, pulse_width);
208 if (!--*max)
209 return -ENOBUFS;
210 init_ir_raw_event_duration((*ev)++, 0, space_width);
211 --*max;
212 return 0;
213 }
214
215 /**
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
224 */
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;
233 };
234
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);
238
239 /**
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
246 */
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;
253 };
254
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);
258
259 /*
260 * Routines from rc-raw.c to be used internally and by decoders
261 */
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);
271
272 /*
273 * lirc interface
274 */
275 #ifdef CONFIG_LIRC
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);
282 #else
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) { }
291 #endif
292
293 /*
294 * Decoder initialization code
295 *
296 * Those load logic are called during ir-core init, and automatically
297 * loads the compiled decoders for their usage with IR raw events
298 */
299
300 #endif /* _RC_CORE_PRIV */