xref: /linux/drivers/media/rc/rc-core-priv.h (revision 13a370b9d275959ac75e92dc14e43eeae75804f8)
1 /*
2  * SPDX-License-Identifier: GPL-2.0
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  */
24 int rc_open(struct rc_dev *rdev);
25 
26 /**
27  * rc_close - Closes a RC device
28  *
29  * @rdev: pointer to struct rc_dev.
30  */
31 void rc_close(struct rc_dev *rdev);
32 
33 struct 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 
42 	/* These two should only be used by the mce kbd decoder */
43 	int (*raw_register)(struct rc_dev *dev);
44 	int (*raw_unregister)(struct rc_dev *dev);
45 };
46 
47 struct ir_raw_event_ctrl {
48 	struct list_head		list;		/* to keep track of raw clients */
49 	struct task_struct		*thread;
50 	/* fifo for the pulse/space durations */
51 	DECLARE_KFIFO(kfifo, struct ir_raw_event, MAX_IR_EVENT_SIZE);
52 	ktime_t				last_event;	/* when last event occurred */
53 	struct rc_dev			*dev;		/* pointer to the parent rc_dev */
54 	/* handle delayed ir_raw_event_store_edge processing */
55 	spinlock_t			edge_spinlock;
56 	struct timer_list		edge_handle;
57 
58 	/* raw decoder state follows */
59 	struct ir_raw_event prev_ev;
60 	struct ir_raw_event this_ev;
61 
62 #ifdef CONFIG_BPF_LIRC_MODE2
63 	u32				bpf_sample;
64 	struct bpf_prog_array __rcu	*progs;
65 #endif
66 	struct nec_dec {
67 		int state;
68 		unsigned count;
69 		u32 bits;
70 		bool is_nec_x;
71 		bool necx_repeat;
72 	} nec;
73 	struct rc5_dec {
74 		int state;
75 		u32 bits;
76 		unsigned count;
77 		bool is_rc5x;
78 	} rc5;
79 	struct rc6_dec {
80 		int state;
81 		u8 header;
82 		u32 body;
83 		bool toggle;
84 		unsigned count;
85 		unsigned wanted_bits;
86 	} rc6;
87 	struct sony_dec {
88 		int state;
89 		u32 bits;
90 		unsigned count;
91 	} sony;
92 	struct jvc_dec {
93 		int state;
94 		u16 bits;
95 		u16 old_bits;
96 		unsigned count;
97 		bool first;
98 		bool toggle;
99 	} jvc;
100 	struct sanyo_dec {
101 		int state;
102 		unsigned count;
103 		u64 bits;
104 	} sanyo;
105 	struct sharp_dec {
106 		int state;
107 		unsigned count;
108 		u32 bits;
109 		unsigned int pulse_len;
110 	} sharp;
111 	struct mce_kbd_dec {
112 		struct input_dev *idev;
113 		struct timer_list rx_timeout;
114 		char name[64];
115 		char phys[64];
116 		int state;
117 		u8 header;
118 		u32 body;
119 		unsigned count;
120 		unsigned wanted_bits;
121 	} mce_kbd;
122 	struct xmp_dec {
123 		int state;
124 		unsigned count;
125 		u32 durations[16];
126 	} xmp;
127 	struct imon_dec {
128 		int state;
129 		int count;
130 		int last_chk;
131 		unsigned int bits;
132 	} imon;
133 };
134 
135 /* Mutex for locking raw IR processing and handler change */
136 extern struct mutex ir_raw_handler_lock;
137 
138 /* macros for IR decoders */
139 static inline bool geq_margin(unsigned d1, unsigned d2, unsigned margin)
140 {
141 	return d1 > (d2 - margin);
142 }
143 
144 static inline bool eq_margin(unsigned d1, unsigned d2, unsigned margin)
145 {
146 	return ((d1 > (d2 - margin)) && (d1 < (d2 + margin)));
147 }
148 
149 static inline bool is_transition(struct ir_raw_event *x, struct ir_raw_event *y)
150 {
151 	return x->pulse != y->pulse;
152 }
153 
154 static inline void decrease_duration(struct ir_raw_event *ev, unsigned duration)
155 {
156 	if (duration > ev->duration)
157 		ev->duration = 0;
158 	else
159 		ev->duration -= duration;
160 }
161 
162 /* Returns true if event is normal pulse/space event */
163 static inline bool is_timing_event(struct ir_raw_event ev)
164 {
165 	return !ev.carrier_report && !ev.reset;
166 }
167 
168 #define TO_US(duration)			DIV_ROUND_CLOSEST((duration), 1000)
169 #define TO_STR(is_pulse)		((is_pulse) ? "pulse" : "space")
170 
171 /* functions for IR encoders */
172 bool rc_validate_scancode(enum rc_proto proto, u32 scancode);
173 
174 static inline void init_ir_raw_event_duration(struct ir_raw_event *ev,
175 					      unsigned int pulse,
176 					      u32 duration)
177 {
178 	init_ir_raw_event(ev);
179 	ev->duration = duration;
180 	ev->pulse = pulse;
181 }
182 
183 /**
184  * struct ir_raw_timings_manchester - Manchester coding timings
185  * @leader_pulse:	duration of leader pulse (if any) 0 if continuing
186  *			existing signal
187  * @leader_space:	duration of leader space (if any)
188  * @clock:		duration of each pulse/space in ns
189  * @invert:		if set clock logic is inverted
190  *			(0 = space + pulse, 1 = pulse + space)
191  * @trailer_space:	duration of trailer space in ns
192  */
193 struct ir_raw_timings_manchester {
194 	unsigned int leader_pulse;
195 	unsigned int leader_space;
196 	unsigned int clock;
197 	unsigned int invert:1;
198 	unsigned int trailer_space;
199 };
200 
201 int ir_raw_gen_manchester(struct ir_raw_event **ev, unsigned int max,
202 			  const struct ir_raw_timings_manchester *timings,
203 			  unsigned int n, u64 data);
204 
205 /**
206  * ir_raw_gen_pulse_space() - generate pulse and space raw events.
207  * @ev:			Pointer to pointer to next free raw event.
208  *			Will be incremented for each raw event written.
209  * @max:		Pointer to number of raw events available in buffer.
210  *			Will be decremented for each raw event written.
211  * @pulse_width:	Width of pulse in ns.
212  * @space_width:	Width of space in ns.
213  *
214  * Returns:	0 on success.
215  *		-ENOBUFS if there isn't enough buffer space to write both raw
216  *		events. In this case @max events will have been written.
217  */
218 static inline int ir_raw_gen_pulse_space(struct ir_raw_event **ev,
219 					 unsigned int *max,
220 					 unsigned int pulse_width,
221 					 unsigned int space_width)
222 {
223 	if (!*max)
224 		return -ENOBUFS;
225 	init_ir_raw_event_duration((*ev)++, 1, pulse_width);
226 	if (!--*max)
227 		return -ENOBUFS;
228 	init_ir_raw_event_duration((*ev)++, 0, space_width);
229 	--*max;
230 	return 0;
231 }
232 
233 /**
234  * struct ir_raw_timings_pd - pulse-distance modulation timings
235  * @header_pulse:	duration of header pulse in ns (0 for none)
236  * @header_space:	duration of header space in ns
237  * @bit_pulse:		duration of bit pulse in ns
238  * @bit_space:		duration of bit space (for logic 0 and 1) in ns
239  * @trailer_pulse:	duration of trailer pulse in ns
240  * @trailer_space:	duration of trailer space in ns
241  * @msb_first:		1 if most significant bit is sent first
242  */
243 struct ir_raw_timings_pd {
244 	unsigned int header_pulse;
245 	unsigned int header_space;
246 	unsigned int bit_pulse;
247 	unsigned int bit_space[2];
248 	unsigned int trailer_pulse;
249 	unsigned int trailer_space;
250 	unsigned int msb_first:1;
251 };
252 
253 int ir_raw_gen_pd(struct ir_raw_event **ev, unsigned int max,
254 		  const struct ir_raw_timings_pd *timings,
255 		  unsigned int n, u64 data);
256 
257 /**
258  * struct ir_raw_timings_pl - pulse-length modulation timings
259  * @header_pulse:	duration of header pulse in ns (0 for none)
260  * @bit_space:		duration of bit space in ns
261  * @bit_pulse:		duration of bit pulse (for logic 0 and 1) in ns
262  * @trailer_space:	duration of trailer space in ns
263  * @msb_first:		1 if most significant bit is sent first
264  */
265 struct ir_raw_timings_pl {
266 	unsigned int header_pulse;
267 	unsigned int bit_space;
268 	unsigned int bit_pulse[2];
269 	unsigned int trailer_space;
270 	unsigned int msb_first:1;
271 };
272 
273 int ir_raw_gen_pl(struct ir_raw_event **ev, unsigned int max,
274 		  const struct ir_raw_timings_pl *timings,
275 		  unsigned int n, u64 data);
276 
277 /*
278  * Routines from rc-raw.c to be used internally and by decoders
279  */
280 u64 ir_raw_get_allowed_protocols(void);
281 int ir_raw_event_prepare(struct rc_dev *dev);
282 int ir_raw_event_register(struct rc_dev *dev);
283 void ir_raw_event_free(struct rc_dev *dev);
284 void ir_raw_event_unregister(struct rc_dev *dev);
285 int ir_raw_handler_register(struct ir_raw_handler *ir_raw_handler);
286 void ir_raw_handler_unregister(struct ir_raw_handler *ir_raw_handler);
287 void ir_raw_load_modules(u64 *protocols);
288 void ir_raw_init(void);
289 
290 /*
291  * lirc interface
292  */
293 #ifdef CONFIG_LIRC
294 int lirc_dev_init(void);
295 void lirc_dev_exit(void);
296 void ir_lirc_raw_event(struct rc_dev *dev, struct ir_raw_event ev);
297 void ir_lirc_scancode_event(struct rc_dev *dev, struct lirc_scancode *lsc);
298 int ir_lirc_register(struct rc_dev *dev);
299 void ir_lirc_unregister(struct rc_dev *dev);
300 struct rc_dev *rc_dev_get_from_fd(int fd);
301 #else
302 static inline int lirc_dev_init(void) { return 0; }
303 static inline void lirc_dev_exit(void) {}
304 static inline void ir_lirc_raw_event(struct rc_dev *dev,
305 				     struct ir_raw_event ev) { }
306 static inline void ir_lirc_scancode_event(struct rc_dev *dev,
307 					  struct lirc_scancode *lsc) { }
308 static inline int ir_lirc_register(struct rc_dev *dev) { return 0; }
309 static inline void ir_lirc_unregister(struct rc_dev *dev) { }
310 #endif
311 
312 /*
313  * bpf interface
314  */
315 #ifdef CONFIG_BPF_LIRC_MODE2
316 void lirc_bpf_free(struct rc_dev *dev);
317 void lirc_bpf_run(struct rc_dev *dev, u32 sample);
318 #else
319 static inline void lirc_bpf_free(struct rc_dev *dev) { }
320 static inline void lirc_bpf_run(struct rc_dev *dev, u32 sample) { }
321 #endif
322 
323 #endif /* _RC_CORE_PRIV */
324