GNU Linux-libre 4.14.290-gnu1
[releases.git] / drivers / media / rc / rc-core-priv.h
1 /*
2  * Remote Controller core raw events header
3  *
4  * Copyright (C) 2010 by Mauro Carvalho Chehab
5  *
6  * This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation version 2 of the License.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  */
15
16 #ifndef _RC_CORE_PRIV
17 #define _RC_CORE_PRIV
18
19 /* Define the max number of pulse/space transitions to buffer */
20 #define MAX_IR_EVENT_SIZE       512
21
22 #include <linux/slab.h>
23 #include <media/rc-core.h>
24
25 struct ir_raw_handler {
26         struct list_head list;
27
28         u64 protocols; /* which are handled by this handler */
29         int (*decode)(struct rc_dev *dev, struct ir_raw_event event);
30         int (*encode)(enum rc_proto protocol, u32 scancode,
31                       struct ir_raw_event *events, unsigned int max);
32
33         /* These two should only be used by the lirc decoder */
34         int (*raw_register)(struct rc_dev *dev);
35         int (*raw_unregister)(struct rc_dev *dev);
36 };
37
38 struct ir_raw_event_ctrl {
39         struct list_head                list;           /* to keep track of raw clients */
40         struct task_struct              *thread;
41         /* fifo for the pulse/space durations */
42         DECLARE_KFIFO(kfifo, struct ir_raw_event, MAX_IR_EVENT_SIZE);
43         ktime_t                         last_event;     /* when last event occurred */
44         struct rc_dev                   *dev;           /* pointer to the parent rc_dev */
45         /* edge driver */
46         struct timer_list edge_handle;
47
48         /* raw decoder state follows */
49         struct ir_raw_event prev_ev;
50         struct ir_raw_event this_ev;
51         struct nec_dec {
52                 int state;
53                 unsigned count;
54                 u32 bits;
55                 bool is_nec_x;
56                 bool necx_repeat;
57         } nec;
58         struct rc5_dec {
59                 int state;
60                 u32 bits;
61                 unsigned count;
62                 bool is_rc5x;
63         } rc5;
64         struct rc6_dec {
65                 int state;
66                 u8 header;
67                 u32 body;
68                 bool toggle;
69                 unsigned count;
70                 unsigned wanted_bits;
71         } rc6;
72         struct sony_dec {
73                 int state;
74                 u32 bits;
75                 unsigned count;
76         } sony;
77         struct jvc_dec {
78                 int state;
79                 u16 bits;
80                 u16 old_bits;
81                 unsigned count;
82                 bool first;
83                 bool toggle;
84         } jvc;
85         struct sanyo_dec {
86                 int state;
87                 unsigned count;
88                 u64 bits;
89         } sanyo;
90         struct sharp_dec {
91                 int state;
92                 unsigned count;
93                 u32 bits;
94                 unsigned int pulse_len;
95         } sharp;
96         struct mce_kbd_dec {
97                 struct input_dev *idev;
98                 struct timer_list rx_timeout;
99                 char name[64];
100                 char phys[64];
101                 int state;
102                 u8 header;
103                 u32 body;
104                 unsigned count;
105                 unsigned wanted_bits;
106         } mce_kbd;
107         struct lirc_codec {
108                 struct rc_dev *dev;
109                 struct lirc_driver *drv;
110                 int carrier_low;
111
112                 ktime_t gap_start;
113                 u64 gap_duration;
114                 bool gap;
115                 bool send_timeout_reports;
116
117         } lirc;
118         struct xmp_dec {
119                 int state;
120                 unsigned count;
121                 u32 durations[16];
122         } xmp;
123 };
124
125 /* macros for IR decoders */
126 static inline bool geq_margin(unsigned d1, unsigned d2, unsigned margin)
127 {
128         return d1 > (d2 - margin);
129 }
130
131 static inline bool eq_margin(unsigned d1, unsigned d2, unsigned margin)
132 {
133         return ((d1 > (d2 - margin)) && (d1 < (d2 + margin)));
134 }
135
136 static inline bool is_transition(struct ir_raw_event *x, struct ir_raw_event *y)
137 {
138         return x->pulse != y->pulse;
139 }
140
141 static inline void decrease_duration(struct ir_raw_event *ev, unsigned duration)
142 {
143         if (duration > ev->duration)
144                 ev->duration = 0;
145         else
146                 ev->duration -= duration;
147 }
148
149 /* Returns true if event is normal pulse/space event */
150 static inline bool is_timing_event(struct ir_raw_event ev)
151 {
152         return !ev.carrier_report && !ev.reset;
153 }
154
155 #define TO_US(duration)                 DIV_ROUND_CLOSEST((duration), 1000)
156 #define TO_STR(is_pulse)                ((is_pulse) ? "pulse" : "space")
157
158 /* functions for IR encoders */
159
160 static inline void init_ir_raw_event_duration(struct ir_raw_event *ev,
161                                               unsigned int pulse,
162                                               u32 duration)
163 {
164         init_ir_raw_event(ev);
165         ev->duration = duration;
166         ev->pulse = pulse;
167 }
168
169 /**
170  * struct ir_raw_timings_manchester - Manchester coding timings
171  * @leader:             duration of leader pulse (if any) 0 if continuing
172  *                      existing signal (see @pulse_space_start)
173  * @pulse_space_start:  1 for starting with pulse (0 for starting with space)
174  * @clock:              duration of each pulse/space in ns
175  * @invert:             if set clock logic is inverted
176  *                      (0 = space + pulse, 1 = pulse + space)
177  * @trailer_space:      duration of trailer space in ns
178  */
179 struct ir_raw_timings_manchester {
180         unsigned int leader;
181         unsigned int pulse_space_start:1;
182         unsigned int clock;
183         unsigned int invert:1;
184         unsigned int trailer_space;
185 };
186
187 int ir_raw_gen_manchester(struct ir_raw_event **ev, unsigned int max,
188                           const struct ir_raw_timings_manchester *timings,
189                           unsigned int n, u64 data);
190
191 /**
192  * ir_raw_gen_pulse_space() - generate pulse and space raw events.
193  * @ev:                 Pointer to pointer to next free raw event.
194  *                      Will be incremented for each raw event written.
195  * @max:                Pointer to number of raw events available in buffer.
196  *                      Will be decremented for each raw event written.
197  * @pulse_width:        Width of pulse in ns.
198  * @space_width:        Width of space in ns.
199  *
200  * Returns:     0 on success.
201  *              -ENOBUFS if there isn't enough buffer space to write both raw
202  *              events. In this case @max events will have been written.
203  */
204 static inline int ir_raw_gen_pulse_space(struct ir_raw_event **ev,
205                                          unsigned int *max,
206                                          unsigned int pulse_width,
207                                          unsigned int space_width)
208 {
209         if (!*max)
210                 return -ENOBUFS;
211         init_ir_raw_event_duration((*ev)++, 1, pulse_width);
212         if (!--*max)
213                 return -ENOBUFS;
214         init_ir_raw_event_duration((*ev)++, 0, space_width);
215         --*max;
216         return 0;
217 }
218
219 /**
220  * struct ir_raw_timings_pd - pulse-distance modulation timings
221  * @header_pulse:       duration of header pulse in ns (0 for none)
222  * @header_space:       duration of header space in ns
223  * @bit_pulse:          duration of bit pulse in ns
224  * @bit_space:          duration of bit space (for logic 0 and 1) in ns
225  * @trailer_pulse:      duration of trailer pulse in ns
226  * @trailer_space:      duration of trailer space in ns
227  * @msb_first:          1 if most significant bit is sent first
228  */
229 struct ir_raw_timings_pd {
230         unsigned int header_pulse;
231         unsigned int header_space;
232         unsigned int bit_pulse;
233         unsigned int bit_space[2];
234         unsigned int trailer_pulse;
235         unsigned int trailer_space;
236         unsigned int msb_first:1;
237 };
238
239 int ir_raw_gen_pd(struct ir_raw_event **ev, unsigned int max,
240                   const struct ir_raw_timings_pd *timings,
241                   unsigned int n, u64 data);
242
243 /**
244  * struct ir_raw_timings_pl - pulse-length modulation timings
245  * @header_pulse:       duration of header pulse in ns (0 for none)
246  * @bit_space:          duration of bit space in ns
247  * @bit_pulse:          duration of bit pulse (for logic 0 and 1) in ns
248  * @trailer_space:      duration of trailer space in ns
249  * @msb_first:          1 if most significant bit is sent first
250  */
251 struct ir_raw_timings_pl {
252         unsigned int header_pulse;
253         unsigned int bit_space;
254         unsigned int bit_pulse[2];
255         unsigned int trailer_space;
256         unsigned int msb_first:1;
257 };
258
259 int ir_raw_gen_pl(struct ir_raw_event **ev, unsigned int max,
260                   const struct ir_raw_timings_pl *timings,
261                   unsigned int n, u64 data);
262
263 /*
264  * Routines from rc-raw.c to be used internally and by decoders
265  */
266 u64 ir_raw_get_allowed_protocols(void);
267 int ir_raw_event_prepare(struct rc_dev *dev);
268 int ir_raw_event_register(struct rc_dev *dev);
269 void ir_raw_event_free(struct rc_dev *dev);
270 void ir_raw_event_unregister(struct rc_dev *dev);
271 int ir_raw_handler_register(struct ir_raw_handler *ir_raw_handler);
272 void ir_raw_handler_unregister(struct ir_raw_handler *ir_raw_handler);
273 void ir_raw_init(void);
274
275 /*
276  * Decoder initialization code
277  *
278  * Those load logic are called during ir-core init, and automatically
279  * loads the compiled decoders for their usage with IR raw events
280  */
281
282 #endif /* _RC_CORE_PRIV */