blob: 57990a3379221a11a2a30c689b50b28b9ec71190 [file] [log] [blame]
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -03001/* ir-raw-event.c - handle IR Pulse/Space event
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
15#include <media/ir-core.h>
Mauro Carvalho Chehab995187b2010-03-24 20:47:53 -030016#include <linux/workqueue.h>
Mauro Carvalho Chehab93c312f2010-03-25 21:13:43 -030017#include <linux/spinlock.h>
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -030018
19/* Define the max number of bit transitions per IR keycode */
20#define MAX_IR_EVENT_SIZE 256
21
Mauro Carvalho Chehab995187b2010-03-24 20:47:53 -030022/* Used to handle IR raw handler extensions */
23static LIST_HEAD(ir_raw_handler_list);
Mauro Carvalho Chehab93c312f2010-03-25 21:13:43 -030024static spinlock_t ir_raw_handler_lock;
25
26/**
27 * RUN_DECODER() - runs an operation on all IR decoders
28 * @ops: IR raw handler operation to be called
29 * @arg: arguments to be passed to the callback
30 *
31 * Calls ir_raw_handler::ops for all registered IR handlers. It prevents
32 * new decode addition/removal while running, by locking ir_raw_handler_lock
33 * mutex. If an error occurs, it stops the ops. Otherwise, it returns a sum
34 * of the return codes.
35 */
36#define RUN_DECODER(ops, ...) ({ \
37 struct ir_raw_handler *_ir_raw_handler; \
38 int _sumrc = 0, _rc; \
39 spin_lock(&ir_raw_handler_lock); \
40 list_for_each_entry(_ir_raw_handler, &ir_raw_handler_list, list) { \
41 if (_ir_raw_handler->ops) { \
42 _rc = _ir_raw_handler->ops(__VA_ARGS__); \
43 if (_rc < 0) \
44 break; \
45 _sumrc += _rc; \
46 } \
47 } \
48 spin_unlock(&ir_raw_handler_lock); \
49 _sumrc; \
50})
51
Mauro Carvalho Chehab995187b2010-03-24 20:47:53 -030052
53/* Used to load the decoders */
54static struct work_struct wq_load;
55
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -030056int ir_raw_event_register(struct input_dev *input_dev)
57{
58 struct ir_input_dev *ir = input_get_drvdata(input_dev);
59 int rc, size;
60
61 ir->raw = kzalloc(sizeof(*ir->raw), GFP_KERNEL);
Mauro Carvalho Chehab93c312f2010-03-25 21:13:43 -030062 if (!ir->raw)
63 return -ENOMEM;
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -030064
65 size = sizeof(struct ir_raw_event) * MAX_IR_EVENT_SIZE * 2;
66 size = roundup_pow_of_two(size);
67
68 rc = kfifo_alloc(&ir->raw->kfifo, size, GFP_KERNEL);
Mauro Carvalho Chehab93c312f2010-03-25 21:13:43 -030069 if (rc < 0) {
70 kfree(ir->raw);
71 ir->raw = NULL;
72 return rc;
73 }
74
75 rc = RUN_DECODER(raw_register, input_dev);
76 if (rc < 0) {
77 kfifo_free(&ir->raw->kfifo);
78 kfree(ir->raw);
79 ir->raw = NULL;
80 return rc;
81 }
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -030082
83 return rc;
84}
85EXPORT_SYMBOL_GPL(ir_raw_event_register);
86
87void ir_raw_event_unregister(struct input_dev *input_dev)
88{
89 struct ir_input_dev *ir = input_get_drvdata(input_dev);
90
91 if (!ir->raw)
92 return;
93
Mauro Carvalho Chehab93c312f2010-03-25 21:13:43 -030094 RUN_DECODER(raw_unregister, input_dev);
95
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -030096 kfifo_free(&ir->raw->kfifo);
97 kfree(ir->raw);
98 ir->raw = NULL;
99}
100EXPORT_SYMBOL_GPL(ir_raw_event_unregister);
101
102int ir_raw_event_store(struct input_dev *input_dev, enum raw_event_type type)
103{
104 struct ir_input_dev *ir = input_get_drvdata(input_dev);
105 struct timespec ts;
106 struct ir_raw_event event;
107 int rc;
108
109 if (!ir->raw)
110 return -EINVAL;
111
112 event.type = type;
113 event.delta.tv_sec = 0;
114 event.delta.tv_nsec = 0;
115
116 ktime_get_ts(&ts);
117
118 if (timespec_equal(&ir->raw->last_event, &event.delta))
119 event.type |= IR_START_EVENT;
120 else
121 event.delta = timespec_sub(ts, ir->raw->last_event);
122
123 memcpy(&ir->raw->last_event, &ts, sizeof(ts));
124
125 if (event.delta.tv_sec) {
126 event.type |= IR_START_EVENT;
127 event.delta.tv_sec = 0;
128 event.delta.tv_nsec = 0;
129 }
130
131 kfifo_in(&ir->raw->kfifo, &event, sizeof(event));
132
133 return rc;
134}
135EXPORT_SYMBOL_GPL(ir_raw_event_store);
136
137int ir_raw_event_handle(struct input_dev *input_dev)
138{
139 struct ir_input_dev *ir = input_get_drvdata(input_dev);
140 int rc;
Mauro Carvalho Chehab587835a2010-04-04 10:44:51 -0300141 struct ir_raw_event ev;
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -0300142 int len, i;
143
144 /*
145 * Store the events into a temporary buffer. This allows calling more than
146 * one decoder to deal with the received data
147 */
Mauro Carvalho Chehab587835a2010-04-04 10:44:51 -0300148 len = kfifo_len(&ir->raw->kfifo) / sizeof(ev);
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -0300149 if (!len)
150 return 0;
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -0300151
152 for (i = 0; i < len; i++) {
Mauro Carvalho Chehab587835a2010-04-04 10:44:51 -0300153 rc = kfifo_out(&ir->raw->kfifo, &ev, sizeof(ev));
154 if (rc != sizeof(ev)) {
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -0300155 IR_dprintk(1, "overflow error: received %d instead of %zd\n",
Mauro Carvalho Chehab587835a2010-04-04 10:44:51 -0300156 rc, sizeof(ev));
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -0300157 return -EINVAL;
158 }
159 IR_dprintk(2, "event type %d, time before event: %07luus\n",
Mauro Carvalho Chehab587835a2010-04-04 10:44:51 -0300160 ev.type, (ev.delta.tv_nsec + 500) / 1000);
161 rc = RUN_DECODER(decode, input_dev, &ev);
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -0300162 }
163
Mauro Carvalho Chehab995187b2010-03-24 20:47:53 -0300164 /*
165 * Call all ir decoders. This allows decoding the same event with
Mauro Carvalho Chehab587835a2010-04-04 10:44:51 -0300166 * more than one protocol handler.
Mauro Carvalho Chehab995187b2010-03-24 20:47:53 -0300167 */
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -0300168
169 return rc;
170}
171EXPORT_SYMBOL_GPL(ir_raw_event_handle);
Mauro Carvalho Chehab995187b2010-03-24 20:47:53 -0300172
173/*
174 * Extension interface - used to register the IR decoders
175 */
176
177int ir_raw_handler_register(struct ir_raw_handler *ir_raw_handler)
178{
Mauro Carvalho Chehab93c312f2010-03-25 21:13:43 -0300179 spin_lock(&ir_raw_handler_lock);
Mauro Carvalho Chehab995187b2010-03-24 20:47:53 -0300180 list_add_tail(&ir_raw_handler->list, &ir_raw_handler_list);
Mauro Carvalho Chehab93c312f2010-03-25 21:13:43 -0300181 spin_unlock(&ir_raw_handler_lock);
Mauro Carvalho Chehab995187b2010-03-24 20:47:53 -0300182 return 0;
183}
184EXPORT_SYMBOL(ir_raw_handler_register);
185
186void ir_raw_handler_unregister(struct ir_raw_handler *ir_raw_handler)
187{
Mauro Carvalho Chehab93c312f2010-03-25 21:13:43 -0300188 spin_lock(&ir_raw_handler_lock);
Mauro Carvalho Chehab995187b2010-03-24 20:47:53 -0300189 list_del(&ir_raw_handler->list);
Mauro Carvalho Chehab93c312f2010-03-25 21:13:43 -0300190 spin_unlock(&ir_raw_handler_lock);
Mauro Carvalho Chehab995187b2010-03-24 20:47:53 -0300191}
192EXPORT_SYMBOL(ir_raw_handler_unregister);
193
194static void init_decoders(struct work_struct *work)
195{
196 /* Load the decoder modules */
197
198 load_nec_decode();
Mauro Carvalho Chehabdb1423a2010-04-04 10:27:20 -0300199 load_rc5_decode();
Mauro Carvalho Chehab995187b2010-03-24 20:47:53 -0300200
201 /* If needed, we may later add some init code. In this case,
202 it is needed to change the CONFIG_MODULE test at ir-core.h
203 */
204}
205
206void ir_raw_init(void)
207{
Mauro Carvalho Chehab93c312f2010-03-25 21:13:43 -0300208 spin_lock_init(&ir_raw_handler_lock);
Mauro Carvalho Chehab20d5f112010-03-25 23:49:46 -0300209
Mauro Carvalho Chehab93c312f2010-03-25 21:13:43 -0300210#ifdef MODULE
Mauro Carvalho Chehab995187b2010-03-24 20:47:53 -0300211 INIT_WORK(&wq_load, init_decoders);
212 schedule_work(&wq_load);
Mauro Carvalho Chehab93c312f2010-03-25 21:13:43 -0300213#endif
214}