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