[media] adv7175: support s_power
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / media / rc / ir-raw.c
CommitLineData
829ba9fe 1/* ir-raw.c - handle IR pulse/space events
a3572c34
MCC
2 *
3 * Copyright (C) 2010 by Mauro Carvalho Chehab <mchehab@redhat.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation version 2 of the License.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 */
14
0d2cb1de 15#include <linux/kthread.h>
45a568fa 16#include <linux/mutex.h>
724e2495 17#include <linux/sched.h>
0d2cb1de 18#include <linux/freezer.h>
f62de675 19#include "rc-core-priv.h"
a3572c34 20
724e2495
DH
21/* Define the max number of pulse/space transitions to buffer */
22#define MAX_IR_EVENT_SIZE 512
a3572c34 23
c216369e
DH
24/* Used to keep track of IR raw clients, protected by ir_raw_handler_lock */
25static LIST_HEAD(ir_raw_client_list);
26
995187be 27/* Used to handle IR raw handler extensions */
45a568fa 28static DEFINE_MUTEX(ir_raw_handler_lock);
667c9ebe
DH
29static LIST_HEAD(ir_raw_handler_list);
30static u64 available_protocols;
93c312ff 31
5fa2989f 32#ifdef MODULE
995187be
MCC
33/* Used to load the decoders */
34static struct work_struct wq_load;
5fa2989f 35#endif
995187be 36
0d2cb1de 37static int ir_raw_event_thread(void *data)
724e2495 38{
e40b1127 39 struct ir_raw_event ev;
c216369e 40 struct ir_raw_handler *handler;
0d2cb1de 41 struct ir_raw_event_ctrl *raw = (struct ir_raw_event_ctrl *)data;
c6ef1e7f 42 int retval;
0d2cb1de
ML
43
44 while (!kthread_should_stop()) {
724e2495 45
c6ef1e7f
ML
46 spin_lock_irq(&raw->lock);
47 retval = kfifo_out(&raw->kfifo, &ev, sizeof(ev));
48
49 if (!retval) {
50 set_current_state(TASK_INTERRUPTIBLE);
0d2cb1de 51
c6ef1e7f
ML
52 if (kthread_should_stop())
53 set_current_state(TASK_RUNNING);
54
55 spin_unlock_irq(&raw->lock);
56 schedule();
57 continue;
0d2cb1de
ML
58 }
59
c6ef1e7f 60 spin_unlock_irq(&raw->lock);
0d2cb1de 61
c6ef1e7f
ML
62
63 BUG_ON(retval != sizeof(ev));
64
65 mutex_lock(&ir_raw_handler_lock);
66 list_for_each_entry(handler, &ir_raw_handler_list, list)
d8b4b582 67 handler->decode(raw->dev, ev);
c6ef1e7f
ML
68 raw->prev_ev = ev;
69 mutex_unlock(&ir_raw_handler_lock);
c216369e 70 }
0d2cb1de
ML
71
72 return 0;
724e2495
DH
73}
74
724e2495
DH
75/**
76 * ir_raw_event_store() - pass a pulse/space duration to the raw ir decoders
d8b4b582 77 * @dev: the struct rc_dev device descriptor
e40b1127 78 * @ev: the struct ir_raw_event descriptor of the pulse/space
724e2495
DH
79 *
80 * This routine (which may be called from an interrupt context) stores a
81 * pulse/space duration for the raw ir decoding state machines. Pulses are
82 * signalled as positive values and spaces as negative values. A zero value
83 * will reset the decoding state machines.
84 */
d8b4b582 85int ir_raw_event_store(struct rc_dev *dev, struct ir_raw_event *ev)
a3572c34 86{
d8b4b582 87 if (!dev->raw)
a3572c34
MCC
88 return -EINVAL;
89
74c4792c 90 IR_dprintk(2, "sample: (%05dus %s)\n",
d8b4b582 91 TO_US(ev->duration), TO_STR(ev->pulse));
510fcb70 92
d8b4b582 93 if (kfifo_in(&dev->raw->kfifo, ev, sizeof(*ev)) != sizeof(*ev))
724e2495 94 return -ENOMEM;
a3572c34 95
724e2495
DH
96 return 0;
97}
98EXPORT_SYMBOL_GPL(ir_raw_event_store);
a3572c34 99
724e2495
DH
100/**
101 * ir_raw_event_store_edge() - notify raw ir decoders of the start of a pulse/space
d8b4b582 102 * @dev: the struct rc_dev device descriptor
724e2495
DH
103 * @type: the type of the event that has occurred
104 *
105 * This routine (which may be called from an interrupt context) is used to
106 * store the beginning of an ir pulse or space (or the start/end of ir
107 * reception) for the raw ir decoding state machines. This is used by
108 * hardware which does not provide durations directly but only interrupts
109 * (or similar events) on state change.
110 */
d8b4b582 111int ir_raw_event_store_edge(struct rc_dev *dev, enum raw_event_type type)
724e2495 112{
724e2495
DH
113 ktime_t now;
114 s64 delta; /* ns */
e40b1127 115 struct ir_raw_event ev;
724e2495 116 int rc = 0;
a3572c34 117
d8b4b582 118 if (!dev->raw)
724e2495 119 return -EINVAL;
a3572c34 120
724e2495 121 now = ktime_get();
d8b4b582 122 delta = ktime_to_ns(ktime_sub(now, dev->raw->last_event));
a3572c34 123
724e2495
DH
124 /* Check for a long duration since last event or if we're
125 * being called for the first time, note that delta can't
126 * possibly be negative.
127 */
e40b1127 128 ev.duration = 0;
d8b4b582 129 if (delta > IR_MAX_DURATION || !dev->raw->last_type)
724e2495 130 type |= IR_START_EVENT;
e40b1127
DH
131 else
132 ev.duration = delta;
724e2495
DH
133
134 if (type & IR_START_EVENT)
d8b4b582
DH
135 ir_raw_event_reset(dev);
136 else if (dev->raw->last_type & IR_SPACE) {
e40b1127 137 ev.pulse = false;
d8b4b582
DH
138 rc = ir_raw_event_store(dev, &ev);
139 } else if (dev->raw->last_type & IR_PULSE) {
e40b1127 140 ev.pulse = true;
d8b4b582 141 rc = ir_raw_event_store(dev, &ev);
e40b1127 142 } else
724e2495 143 return 0;
a3572c34 144
d8b4b582
DH
145 dev->raw->last_event = now;
146 dev->raw->last_type = type;
a3572c34
MCC
147 return rc;
148}
724e2495 149EXPORT_SYMBOL_GPL(ir_raw_event_store_edge);
a3572c34 150
4a702ebf
ML
151/**
152 * ir_raw_event_store_with_filter() - pass next pulse/space to decoders with some processing
d8b4b582 153 * @dev: the struct rc_dev device descriptor
4a702ebf
ML
154 * @type: the type of the event that has occurred
155 *
156 * This routine (which may be called from an interrupt context) works
157 * in similiar manner to ir_raw_event_store_edge.
158 * This routine is intended for devices with limited internal buffer
159 * It automerges samples of same type, and handles timeouts
160 */
d8b4b582 161int ir_raw_event_store_with_filter(struct rc_dev *dev, struct ir_raw_event *ev)
4a702ebf 162{
d8b4b582 163 if (!dev->raw)
4a702ebf
ML
164 return -EINVAL;
165
166 /* Ignore spaces in idle mode */
d8b4b582 167 if (dev->idle && !ev->pulse)
4a702ebf 168 return 0;
d8b4b582
DH
169 else if (dev->idle)
170 ir_raw_event_set_idle(dev, false);
171
172 if (!dev->raw->this_ev.duration)
173 dev->raw->this_ev = *ev;
174 else if (ev->pulse == dev->raw->this_ev.pulse)
175 dev->raw->this_ev.duration += ev->duration;
176 else {
177 ir_raw_event_store(dev, &dev->raw->this_ev);
178 dev->raw->this_ev = *ev;
4a702ebf
ML
179 }
180
181 /* Enter idle mode if nessesary */
d8b4b582
DH
182 if (!ev->pulse && dev->timeout &&
183 dev->raw->this_ev.duration >= dev->timeout)
184 ir_raw_event_set_idle(dev, true);
185
4a702ebf
ML
186 return 0;
187}
188EXPORT_SYMBOL_GPL(ir_raw_event_store_with_filter);
189
4651918a 190/**
d8b4b582
DH
191 * ir_raw_event_set_idle() - provide hint to rc-core when the device is idle or not
192 * @dev: the struct rc_dev device descriptor
193 * @idle: whether the device is idle or not
4651918a 194 */
d8b4b582 195void ir_raw_event_set_idle(struct rc_dev *dev, bool idle)
4a702ebf 196{
d8b4b582 197 if (!dev->raw)
4a702ebf
ML
198 return;
199
4651918a 200 IR_dprintk(2, "%s idle mode\n", idle ? "enter" : "leave");
4a702ebf
ML
201
202 if (idle) {
d8b4b582
DH
203 dev->raw->this_ev.timeout = true;
204 ir_raw_event_store(dev, &dev->raw->this_ev);
205 init_ir_raw_event(&dev->raw->this_ev);
4a702ebf 206 }
4651918a 207
d8b4b582
DH
208 if (dev->s_idle)
209 dev->s_idle(dev, idle);
210
211 dev->idle = idle;
4a702ebf
ML
212}
213EXPORT_SYMBOL_GPL(ir_raw_event_set_idle);
214
724e2495
DH
215/**
216 * ir_raw_event_handle() - schedules the decoding of stored ir data
d8b4b582 217 * @dev: the struct rc_dev device descriptor
724e2495 218 *
d8b4b582 219 * This routine will tell rc-core to start decoding stored ir data.
724e2495 220 */
d8b4b582 221void ir_raw_event_handle(struct rc_dev *dev)
a3572c34 222{
c6ef1e7f 223 unsigned long flags;
a3572c34 224
d8b4b582 225 if (!dev->raw)
724e2495 226 return;
a3572c34 227
d8b4b582
DH
228 spin_lock_irqsave(&dev->raw->lock, flags);
229 wake_up_process(dev->raw->thread);
230 spin_unlock_irqrestore(&dev->raw->lock, flags);
a3572c34
MCC
231}
232EXPORT_SYMBOL_GPL(ir_raw_event_handle);
995187be 233
667c9ebe
DH
234/* used internally by the sysfs interface */
235u64
236ir_raw_get_allowed_protocols()
237{
238 u64 protocols;
45a568fa 239 mutex_lock(&ir_raw_handler_lock);
667c9ebe 240 protocols = available_protocols;
45a568fa 241 mutex_unlock(&ir_raw_handler_lock);
667c9ebe
DH
242 return protocols;
243}
244
245/*
246 * Used to (un)register raw event clients
247 */
d8b4b582 248int ir_raw_event_register(struct rc_dev *dev)
667c9ebe 249{
667c9ebe 250 int rc;
c216369e 251 struct ir_raw_handler *handler;
667c9ebe 252
d8b4b582
DH
253 if (!dev)
254 return -EINVAL;
667c9ebe 255
d8b4b582
DH
256 dev->raw = kzalloc(sizeof(*dev->raw), GFP_KERNEL);
257 if (!dev->raw)
258 return -ENOMEM;
0d2cb1de 259
d8b4b582
DH
260 dev->raw->dev = dev;
261 dev->raw->enabled_protocols = ~0;
262 rc = kfifo_alloc(&dev->raw->kfifo,
263 sizeof(struct ir_raw_event) * MAX_IR_EVENT_SIZE,
667c9ebe 264 GFP_KERNEL);
d8b4b582
DH
265 if (rc < 0)
266 goto out;
667c9ebe 267
d8b4b582
DH
268 spin_lock_init(&dev->raw->lock);
269 dev->raw->thread = kthread_run(ir_raw_event_thread, dev->raw,
270 "rc%ld", dev->devno);
0d2cb1de 271
d8b4b582
DH
272 if (IS_ERR(dev->raw->thread)) {
273 rc = PTR_ERR(dev->raw->thread);
274 goto out;
0d2cb1de
ML
275 }
276
45a568fa 277 mutex_lock(&ir_raw_handler_lock);
d8b4b582 278 list_add_tail(&dev->raw->list, &ir_raw_client_list);
c216369e
DH
279 list_for_each_entry(handler, &ir_raw_handler_list, list)
280 if (handler->raw_register)
d8b4b582 281 handler->raw_register(dev);
45a568fa 282 mutex_unlock(&ir_raw_handler_lock);
667c9ebe 283
c216369e 284 return 0;
d8b4b582
DH
285
286out:
287 kfree(dev->raw);
288 dev->raw = NULL;
289 return rc;
667c9ebe
DH
290}
291
d8b4b582 292void ir_raw_event_unregister(struct rc_dev *dev)
667c9ebe 293{
c216369e 294 struct ir_raw_handler *handler;
667c9ebe 295
d8b4b582 296 if (!dev || !dev->raw)
667c9ebe
DH
297 return;
298
d8b4b582 299 kthread_stop(dev->raw->thread);
c216369e 300
45a568fa 301 mutex_lock(&ir_raw_handler_lock);
d8b4b582 302 list_del(&dev->raw->list);
c216369e
DH
303 list_for_each_entry(handler, &ir_raw_handler_list, list)
304 if (handler->raw_unregister)
d8b4b582 305 handler->raw_unregister(dev);
45a568fa 306 mutex_unlock(&ir_raw_handler_lock);
667c9ebe 307
d8b4b582
DH
308 kfifo_free(&dev->raw->kfifo);
309 kfree(dev->raw);
310 dev->raw = NULL;
667c9ebe
DH
311}
312
995187be
MCC
313/*
314 * Extension interface - used to register the IR decoders
315 */
316
317int ir_raw_handler_register(struct ir_raw_handler *ir_raw_handler)
318{
c216369e
DH
319 struct ir_raw_event_ctrl *raw;
320
45a568fa 321 mutex_lock(&ir_raw_handler_lock);
995187be 322 list_add_tail(&ir_raw_handler->list, &ir_raw_handler_list);
c216369e
DH
323 if (ir_raw_handler->raw_register)
324 list_for_each_entry(raw, &ir_raw_client_list, list)
d8b4b582 325 ir_raw_handler->raw_register(raw->dev);
667c9ebe 326 available_protocols |= ir_raw_handler->protocols;
45a568fa 327 mutex_unlock(&ir_raw_handler_lock);
667c9ebe 328
995187be
MCC
329 return 0;
330}
331EXPORT_SYMBOL(ir_raw_handler_register);
332
333void ir_raw_handler_unregister(struct ir_raw_handler *ir_raw_handler)
334{
c216369e
DH
335 struct ir_raw_event_ctrl *raw;
336
45a568fa 337 mutex_lock(&ir_raw_handler_lock);
995187be 338 list_del(&ir_raw_handler->list);
c216369e
DH
339 if (ir_raw_handler->raw_unregister)
340 list_for_each_entry(raw, &ir_raw_client_list, list)
d8b4b582 341 ir_raw_handler->raw_unregister(raw->dev);
667c9ebe 342 available_protocols &= ~ir_raw_handler->protocols;
45a568fa 343 mutex_unlock(&ir_raw_handler_lock);
995187be
MCC
344}
345EXPORT_SYMBOL(ir_raw_handler_unregister);
346
5fa2989f 347#ifdef MODULE
995187be
MCC
348static void init_decoders(struct work_struct *work)
349{
350 /* Load the decoder modules */
351
352 load_nec_decode();
db1423a6 353 load_rc5_decode();
784a4931 354 load_rc6_decode();
bf670f64 355 load_jvc_decode();
3fe29c89 356 load_sony_decode();
ca414698 357 load_lirc_codec();
995187be
MCC
358
359 /* If needed, we may later add some init code. In this case,
6bda9644 360 it is needed to change the CONFIG_MODULE test at rc-core.h
995187be
MCC
361 */
362}
5fa2989f 363#endif
995187be
MCC
364
365void ir_raw_init(void)
366{
93c312ff 367#ifdef MODULE
995187be
MCC
368 INIT_WORK(&wq_load, init_decoders);
369 schedule_work(&wq_load);
93c312ff
MCC
370#endif
371}