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