blob: 91f9bb87ce688d7470f80f6145b2b2bad191ebe4 [file] [log] [blame]
Jarod Wilson4a62a5a2010-07-03 01:06:57 -03001/*
2 * LIRC base driver
3 *
4 * by Artur Lipowski <alipowski@interia.pl>
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; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
21
Andi Shyti3fac0312016-07-06 06:01:16 -030022#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
23
Jarod Wilson4a62a5a2010-07-03 01:06:57 -030024#include <linux/module.h>
25#include <linux/kernel.h>
26#include <linux/sched.h>
27#include <linux/errno.h>
28#include <linux/ioctl.h>
29#include <linux/fs.h>
30#include <linux/poll.h>
31#include <linux/completion.h>
Jarod Wilson4a62a5a2010-07-03 01:06:57 -030032#include <linux/mutex.h>
33#include <linux/wait.h>
34#include <linux/unistd.h>
35#include <linux/kthread.h>
36#include <linux/bitops.h>
37#include <linux/device.h>
38#include <linux/cdev.h>
39
Srinivas Kandagatlaca7a7222013-07-22 04:23:07 -030040#include <media/rc-core.h>
Jarod Wilson4a62a5a2010-07-03 01:06:57 -030041#include <media/lirc.h>
Jarod Wilson56900852010-07-16 14:25:33 -030042#include <media/lirc_dev.h>
Jarod Wilson4a62a5a2010-07-03 01:06:57 -030043
Rusty Russell90ab5ee2012-01-13 09:32:20 +103044static bool debug;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -030045
46#define IRCTL_DEV_NAME "BaseRemoteCtl"
47#define NOPLUG -1
48#define LOGHEAD "lirc_dev (%s[%d]): "
49
50static dev_t lirc_base_dev;
51
52struct irctl {
53 struct lirc_driver d;
54 int attached;
55 int open;
56
57 struct mutex irctl_lock;
58 struct lirc_buffer *buf;
59 unsigned int chunk_size;
60
Jarod Wilson8de111e22011-05-27 16:56:50 -030061 struct cdev *cdev;
62
Jarod Wilson4a62a5a2010-07-03 01:06:57 -030063 struct task_struct *task;
64 long jiffies_to_wait;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -030065};
66
67static DEFINE_MUTEX(lirc_dev_lock);
68
69static struct irctl *irctls[MAX_IRCTL_DEVICES];
70
71/* Only used for sysfs but defined to void otherwise */
72static struct class *lirc_class;
73
74/* helper function
75 * initializes the irctl structure
76 */
Jarod Wilson578fcb82010-10-16 21:29:50 -030077static void lirc_irctl_init(struct irctl *ir)
Jarod Wilson4a62a5a2010-07-03 01:06:57 -030078{
Jarod Wilson4a62a5a2010-07-03 01:06:57 -030079 mutex_init(&ir->irctl_lock);
80 ir->d.minor = NOPLUG;
81}
82
Jarod Wilson578fcb82010-10-16 21:29:50 -030083static void lirc_irctl_cleanup(struct irctl *ir)
Jarod Wilson4a62a5a2010-07-03 01:06:57 -030084{
Jarod Wilson4a62a5a2010-07-03 01:06:57 -030085 device_destroy(lirc_class, MKDEV(MAJOR(lirc_base_dev), ir->d.minor));
86
87 if (ir->buf != ir->d.rbuf) {
88 lirc_buffer_free(ir->buf);
89 kfree(ir->buf);
90 }
91 ir->buf = NULL;
92}
93
94/* helper function
95 * reads key codes from driver and puts them into buffer
96 * returns 0 on success
97 */
Jarod Wilson578fcb82010-10-16 21:29:50 -030098static int lirc_add_to_buf(struct irctl *ir)
Jarod Wilson4a62a5a2010-07-03 01:06:57 -030099{
Andi Shyti19e56532016-07-06 06:01:19 -0300100 int res;
101 int got_data = -1;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300102
Andi Shyti19e56532016-07-06 06:01:19 -0300103 if (!ir->d.add_to_buf)
104 return 0;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300105
Andi Shyti19e56532016-07-06 06:01:19 -0300106 /*
107 * service the device as long as it is returning
108 * data and we have space
109 */
110 do {
111 got_data++;
112 res = ir->d.add_to_buf(ir->d.data, ir->buf);
113 } while (!res);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300114
Andi Shyti19e56532016-07-06 06:01:19 -0300115 if (res == -ENODEV)
116 kthread_stop(ir->task);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300117
Andi Shyti19e56532016-07-06 06:01:19 -0300118 return got_data ? 0 : res;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300119}
120
121/* main function of the polling thread
122 */
123static int lirc_thread(void *irctl)
124{
125 struct irctl *ir = irctl;
126
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300127 do {
128 if (ir->open) {
129 if (ir->jiffies_to_wait) {
130 set_current_state(TASK_INTERRUPTIBLE);
131 schedule_timeout(ir->jiffies_to_wait);
132 }
133 if (kthread_should_stop())
134 break;
Jarod Wilson578fcb82010-10-16 21:29:50 -0300135 if (!lirc_add_to_buf(ir))
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300136 wake_up_interruptible(&ir->buf->wait_poll);
137 } else {
138 set_current_state(TASK_INTERRUPTIBLE);
139 schedule();
140 }
141 } while (!kthread_should_stop());
142
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300143 return 0;
144}
145
146
Al Viro75ef9de2013-04-04 19:09:41 -0400147static const struct file_operations lirc_dev_fops = {
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300148 .owner = THIS_MODULE,
149 .read = lirc_dev_fop_read,
150 .write = lirc_dev_fop_write,
151 .poll = lirc_dev_fop_poll,
Arnd Bergmann044e5872010-08-02 15:43:35 -0300152 .unlocked_ioctl = lirc_dev_fop_ioctl,
Jarod Wilson8be292c2010-10-09 15:07:06 -0300153#ifdef CONFIG_COMPAT
154 .compat_ioctl = lirc_dev_fop_ioctl,
155#endif
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300156 .open = lirc_dev_fop_open,
157 .release = lirc_dev_fop_close,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200158 .llseek = noop_llseek,
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300159};
160
161static int lirc_cdev_add(struct irctl *ir)
162{
Jarod Wilson8de111e22011-05-27 16:56:50 -0300163 int retval = -ENOMEM;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300164 struct lirc_driver *d = &ir->d;
Jarod Wilson8de111e22011-05-27 16:56:50 -0300165 struct cdev *cdev;
166
167 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
168 if (!cdev)
169 goto err_out;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300170
171 if (d->fops) {
Jarod Wilsonc1cbb702010-10-18 18:39:20 -0300172 cdev_init(cdev, d->fops);
173 cdev->owner = d->owner;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300174 } else {
Jarod Wilsonc1cbb702010-10-18 18:39:20 -0300175 cdev_init(cdev, &lirc_dev_fops);
176 cdev->owner = THIS_MODULE;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300177 }
Vasiliy Kulikovb395cba2010-11-26 14:06:41 -0300178 retval = kobject_set_name(&cdev->kobj, "lirc%d", d->minor);
179 if (retval)
Jarod Wilson8de111e22011-05-27 16:56:50 -0300180 goto err_out;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300181
Jarod Wilsonc1cbb702010-10-18 18:39:20 -0300182 retval = cdev_add(cdev, MKDEV(MAJOR(lirc_base_dev), d->minor), 1);
Jarod Wilson8de111e22011-05-27 16:56:50 -0300183 if (retval) {
Jarod Wilsonc1cbb702010-10-18 18:39:20 -0300184 kobject_put(&cdev->kobj);
Jarod Wilson8de111e22011-05-27 16:56:50 -0300185 goto err_out;
186 }
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300187
Jarod Wilson8de111e22011-05-27 16:56:50 -0300188 ir->cdev = cdev;
189
190 return 0;
191
192err_out:
193 kfree(cdev);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300194 return retval;
195}
196
Andi Shyti6fa99e12016-07-06 06:01:13 -0300197static int lirc_allocate_buffer(struct irctl *ir)
198{
Andi Shyti70143982016-07-06 06:01:14 -0300199 int err = 0;
Andi Shyti6fa99e12016-07-06 06:01:13 -0300200 int bytes_in_key;
201 unsigned int chunk_size;
202 unsigned int buffer_size;
203 struct lirc_driver *d = &ir->d;
204
Andi Shyti70143982016-07-06 06:01:14 -0300205 mutex_lock(&lirc_dev_lock);
206
Andi Shyti6fa99e12016-07-06 06:01:13 -0300207 bytes_in_key = BITS_TO_LONGS(d->code_length) +
208 (d->code_length % 8 ? 1 : 0);
209 buffer_size = d->buffer_size ? d->buffer_size : BUFLEN / bytes_in_key;
210 chunk_size = d->chunk_size ? d->chunk_size : bytes_in_key;
211
212 if (d->rbuf) {
213 ir->buf = d->rbuf;
214 } else {
215 ir->buf = kmalloc(sizeof(struct lirc_buffer), GFP_KERNEL);
Andi Shyti70143982016-07-06 06:01:14 -0300216 if (!ir->buf) {
217 err = -ENOMEM;
218 goto out;
219 }
Andi Shyti6fa99e12016-07-06 06:01:13 -0300220
221 err = lirc_buffer_init(ir->buf, chunk_size, buffer_size);
222 if (err) {
223 kfree(ir->buf);
Andi Shyti70143982016-07-06 06:01:14 -0300224 goto out;
Andi Shyti6fa99e12016-07-06 06:01:13 -0300225 }
226 }
227 ir->chunk_size = ir->buf->chunk_size;
228
Andi Shyti70143982016-07-06 06:01:14 -0300229out:
230 mutex_unlock(&lirc_dev_lock);
231
232 return err;
Andi Shyti6fa99e12016-07-06 06:01:13 -0300233}
234
Andi Shyti70143982016-07-06 06:01:14 -0300235static int lirc_allocate_driver(struct lirc_driver *d)
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300236{
237 struct irctl *ir;
238 int minor;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300239 int err;
240
241 if (!d) {
Andi Shyti3fac0312016-07-06 06:01:16 -0300242 pr_err("driver pointer must be not NULL!\n");
Andi Shyti54fceca2016-07-06 06:01:17 -0300243 return -EBADRQC;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300244 }
245
Jarod Wilson715d29a2010-10-18 12:02:01 -0300246 if (!d->dev) {
Andi Shyti3fac0312016-07-06 06:01:16 -0300247 pr_err("dev pointer not filled in!\n");
Andi Shyti54fceca2016-07-06 06:01:17 -0300248 return -EINVAL;
Jarod Wilson715d29a2010-10-18 12:02:01 -0300249 }
250
Andi Shyti9675ee52016-07-06 06:01:23 -0300251 if (d->minor >= MAX_IRCTL_DEVICES) {
Andi Shyti3fac0312016-07-06 06:01:16 -0300252 dev_err(d->dev, "minor must be between 0 and %d!\n",
253 MAX_IRCTL_DEVICES - 1);
Andi Shyti54fceca2016-07-06 06:01:17 -0300254 return -EBADRQC;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300255 }
256
Andi Shyti9675ee52016-07-06 06:01:23 -0300257 if (d->code_length < 1 || d->code_length > (BUFLEN * 8)) {
Andi Shyti3fac0312016-07-06 06:01:16 -0300258 dev_err(d->dev, "code length must be less than %d bits\n",
259 BUFLEN * 8);
Andi Shyti54fceca2016-07-06 06:01:17 -0300260 return -EBADRQC;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300261 }
262
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300263 if (d->sample_rate) {
264 if (2 > d->sample_rate || HZ < d->sample_rate) {
Andi Shyti3fac0312016-07-06 06:01:16 -0300265 dev_err(d->dev, "invalid %d sample rate\n",
266 d->sample_rate);
Andi Shyti54fceca2016-07-06 06:01:17 -0300267 return -EBADRQC;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300268 }
269 if (!d->add_to_buf) {
Andi Shyti3fac0312016-07-06 06:01:16 -0300270 dev_err(d->dev, "add_to_buf not set\n");
Andi Shyti54fceca2016-07-06 06:01:17 -0300271 return -EBADRQC;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300272 }
Andi Shyti14db9fc2016-07-06 06:01:21 -0300273 } else if (!d->rbuf && !(d->fops && d->fops->read &&
274 d->fops->poll && d->fops->unlocked_ioctl)) {
275 dev_err(d->dev, "undefined read, poll, ioctl\n");
Andi Shyti54fceca2016-07-06 06:01:17 -0300276 return -EBADRQC;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300277 }
278
279 mutex_lock(&lirc_dev_lock);
280
281 minor = d->minor;
282
283 if (minor < 0) {
284 /* find first free slot for driver */
285 for (minor = 0; minor < MAX_IRCTL_DEVICES; minor++)
286 if (!irctls[minor])
287 break;
Andi Shyti9675ee52016-07-06 06:01:23 -0300288 if (minor == MAX_IRCTL_DEVICES) {
Andi Shyti3fac0312016-07-06 06:01:16 -0300289 dev_err(d->dev, "no free slots for drivers!\n");
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300290 err = -ENOMEM;
291 goto out_lock;
292 }
293 } else if (irctls[minor]) {
Andi Shyti3fac0312016-07-06 06:01:16 -0300294 dev_err(d->dev, "minor (%d) just registered!\n", minor);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300295 err = -EBUSY;
296 goto out_lock;
297 }
298
299 ir = kzalloc(sizeof(struct irctl), GFP_KERNEL);
300 if (!ir) {
301 err = -ENOMEM;
302 goto out_lock;
303 }
Jarod Wilson578fcb82010-10-16 21:29:50 -0300304 lirc_irctl_init(ir);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300305 irctls[minor] = ir;
306 d->minor = minor;
307
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300308 /* some safety check 8-) */
309 d->name[sizeof(d->name)-1] = '\0';
310
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300311 if (d->features == 0)
312 d->features = LIRC_CAN_REC_LIRCCODE;
313
314 ir->d = *d;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300315
316 device_create(lirc_class, ir->d.dev,
317 MKDEV(MAJOR(lirc_base_dev), ir->d.minor), NULL,
318 "lirc%u", ir->d.minor);
319
320 if (d->sample_rate) {
Andi Shyti6ab86d22016-07-06 06:01:20 -0300321 ir->jiffies_to_wait = HZ / d->sample_rate;
322
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300323 /* try to fire up polling thread */
324 ir->task = kthread_run(lirc_thread, (void *)ir, "lirc_dev");
325 if (IS_ERR(ir->task)) {
Andi Shyti3fac0312016-07-06 06:01:16 -0300326 dev_err(d->dev, "cannot run thread for minor = %d\n",
327 d->minor);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300328 err = -ECHILD;
329 goto out_sysfs;
330 }
Andi Shyti6ab86d22016-07-06 06:01:20 -0300331 } else {
332 /* it means - wait for external event in task queue */
333 ir->jiffies_to_wait = 0;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300334 }
335
336 err = lirc_cdev_add(ir);
337 if (err)
338 goto out_sysfs;
339
340 ir->attached = 1;
341 mutex_unlock(&lirc_dev_lock);
342
343 dev_info(ir->d.dev, "lirc_dev: driver %s registered at minor = %d\n",
344 ir->d.name, ir->d.minor);
345 return minor;
346
347out_sysfs:
348 device_destroy(lirc_class, MKDEV(MAJOR(lirc_base_dev), ir->d.minor));
349out_lock:
350 mutex_unlock(&lirc_dev_lock);
Andi Shyti54fceca2016-07-06 06:01:17 -0300351
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300352 return err;
353}
Andi Shyti70143982016-07-06 06:01:14 -0300354
355int lirc_register_driver(struct lirc_driver *d)
356{
357 int minor, err = 0;
358
359 minor = lirc_allocate_driver(d);
360 if (minor < 0)
361 return minor;
362
363 if (LIRC_CAN_REC(d->features)) {
364 err = lirc_allocate_buffer(irctls[minor]);
365 if (err)
366 lirc_unregister_driver(minor);
367 }
368
369 return err ? err : minor;
370}
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300371EXPORT_SYMBOL(lirc_register_driver);
372
373int lirc_unregister_driver(int minor)
374{
375 struct irctl *ir;
Jarod Wilsonc1cbb702010-10-18 18:39:20 -0300376 struct cdev *cdev;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300377
378 if (minor < 0 || minor >= MAX_IRCTL_DEVICES) {
Andi Shyti3fac0312016-07-06 06:01:16 -0300379 pr_err("minor (%d) must be between 0 and %d!\n",
380 minor, MAX_IRCTL_DEVICES - 1);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300381 return -EBADRQC;
382 }
383
384 ir = irctls[minor];
Jarod Wilsondf1868e2010-09-17 18:12:31 -0300385 if (!ir) {
Andi Shyti3fac0312016-07-06 06:01:16 -0300386 pr_err("failed to get irctl\n");
Jarod Wilsondf1868e2010-09-17 18:12:31 -0300387 return -ENOENT;
388 }
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300389
Jarod Wilson8de111e22011-05-27 16:56:50 -0300390 cdev = ir->cdev;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300391
392 mutex_lock(&lirc_dev_lock);
393
394 if (ir->d.minor != minor) {
Andi Shyti3fac0312016-07-06 06:01:16 -0300395 dev_err(ir->d.dev, "lirc_dev: minor %d device not registered\n",
396 minor);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300397 mutex_unlock(&lirc_dev_lock);
398 return -ENOENT;
399 }
400
401 /* end up polling thread */
402 if (ir->task)
403 kthread_stop(ir->task);
404
405 dev_dbg(ir->d.dev, "lirc_dev: driver %s unregistered from minor = %d\n",
406 ir->d.name, ir->d.minor);
407
408 ir->attached = 0;
409 if (ir->open) {
410 dev_dbg(ir->d.dev, LOGHEAD "releasing opened driver\n",
411 ir->d.name, ir->d.minor);
412 wake_up_interruptible(&ir->buf->wait_poll);
413 mutex_lock(&ir->irctl_lock);
Andi Shyti8e5fa4c2016-07-06 06:01:26 -0300414
415 if (ir->d.set_use_dec)
416 ir->d.set_use_dec(ir->d.data);
417
Jarod Wilsonc1cbb702010-10-18 18:39:20 -0300418 module_put(cdev->owner);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300419 mutex_unlock(&ir->irctl_lock);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300420 } else {
Jarod Wilson578fcb82010-10-16 21:29:50 -0300421 lirc_irctl_cleanup(ir);
Jarod Wilsonc1cbb702010-10-18 18:39:20 -0300422 cdev_del(cdev);
Jarod Wilson8de111e22011-05-27 16:56:50 -0300423 kfree(cdev);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300424 kfree(ir);
425 irctls[minor] = NULL;
426 }
427
428 mutex_unlock(&lirc_dev_lock);
429
430 return 0;
431}
432EXPORT_SYMBOL(lirc_unregister_driver);
433
434int lirc_dev_fop_open(struct inode *inode, struct file *file)
435{
436 struct irctl *ir;
Jarod Wilsonc1cbb702010-10-18 18:39:20 -0300437 struct cdev *cdev;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300438 int retval = 0;
439
440 if (iminor(inode) >= MAX_IRCTL_DEVICES) {
Andi Shyti3fac0312016-07-06 06:01:16 -0300441 pr_err("open result for %d is -ENODEV\n", iminor(inode));
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300442 return -ENODEV;
443 }
444
445 if (mutex_lock_interruptible(&lirc_dev_lock))
446 return -ERESTARTSYS;
447
448 ir = irctls[iminor(inode)];
449 if (!ir) {
450 retval = -ENODEV;
451 goto error;
452 }
453
454 dev_dbg(ir->d.dev, LOGHEAD "open called\n", ir->d.name, ir->d.minor);
455
456 if (ir->d.minor == NOPLUG) {
457 retval = -ENODEV;
458 goto error;
459 }
460
461 if (ir->open) {
462 retval = -EBUSY;
463 goto error;
464 }
465
Srinivas Kandagatlaca7a7222013-07-22 04:23:07 -0300466 if (ir->d.rdev) {
467 retval = rc_open(ir->d.rdev);
468 if (retval)
469 goto error;
470 }
471
Jarod Wilson8de111e22011-05-27 16:56:50 -0300472 cdev = ir->cdev;
Jarod Wilsonc1cbb702010-10-18 18:39:20 -0300473 if (try_module_get(cdev->owner)) {
474 ir->open++;
Andi Shyti8e5fa4c2016-07-06 06:01:26 -0300475 if (ir->d.set_use_inc)
476 retval = ir->d.set_use_inc(ir->d.data);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300477
478 if (retval) {
Jarod Wilsonc1cbb702010-10-18 18:39:20 -0300479 module_put(cdev->owner);
480 ir->open--;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300481 } else {
482 lirc_buffer_clear(ir->buf);
483 }
484 if (ir->task)
485 wake_up_process(ir->task);
486 }
487
488error:
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300489 mutex_unlock(&lirc_dev_lock);
490
Arnd Bergmannd9d2e9d2010-08-15 18:51:56 +0200491 nonseekable_open(inode, file);
492
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300493 return retval;
494}
495EXPORT_SYMBOL(lirc_dev_fop_open);
496
497int lirc_dev_fop_close(struct inode *inode, struct file *file)
498{
499 struct irctl *ir = irctls[iminor(inode)];
Jarod Wilson8de111e22011-05-27 16:56:50 -0300500 struct cdev *cdev;
Mauro Carvalho Chehabb64e10f2016-02-27 07:51:13 -0300501 int ret;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300502
Jarod Wilson715d29a2010-10-18 12:02:01 -0300503 if (!ir) {
Andi Shyti3fac0312016-07-06 06:01:16 -0300504 pr_err("called with invalid irctl\n");
Jarod Wilson715d29a2010-10-18 12:02:01 -0300505 return -EINVAL;
506 }
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300507
Jarod Wilson8de111e22011-05-27 16:56:50 -0300508 cdev = ir->cdev;
509
Mauro Carvalho Chehabb64e10f2016-02-27 07:51:13 -0300510 ret = mutex_lock_killable(&lirc_dev_lock);
511 WARN_ON(ret);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300512
Markus Elfring3dd94f02014-11-20 09:01:32 -0300513 rc_close(ir->d.rdev);
Srinivas Kandagatlaca7a7222013-07-22 04:23:07 -0300514
Jarod Wilson715d29a2010-10-18 12:02:01 -0300515 ir->open--;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300516 if (ir->attached) {
Andi Shyti8e5fa4c2016-07-06 06:01:26 -0300517 if (ir->d.set_use_dec)
518 ir->d.set_use_dec(ir->d.data);
Jarod Wilsonc1cbb702010-10-18 18:39:20 -0300519 module_put(cdev->owner);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300520 } else {
Jarod Wilson578fcb82010-10-16 21:29:50 -0300521 lirc_irctl_cleanup(ir);
Jarod Wilsonc1cbb702010-10-18 18:39:20 -0300522 cdev_del(cdev);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300523 irctls[ir->d.minor] = NULL;
Jarod Wilson8de111e22011-05-27 16:56:50 -0300524 kfree(cdev);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300525 kfree(ir);
526 }
527
Mauro Carvalho Chehabb64e10f2016-02-27 07:51:13 -0300528 if (!ret)
529 mutex_unlock(&lirc_dev_lock);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300530
531 return 0;
532}
533EXPORT_SYMBOL(lirc_dev_fop_close);
534
535unsigned int lirc_dev_fop_poll(struct file *file, poll_table *wait)
536{
Al Viro496ad9a2013-01-23 17:07:38 -0500537 struct irctl *ir = irctls[iminor(file_inode(file))];
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300538 unsigned int ret;
539
Jarod Wilson715d29a2010-10-18 12:02:01 -0300540 if (!ir) {
Andi Shyti3fac0312016-07-06 06:01:16 -0300541 pr_err("called with invalid irctl\n");
Jarod Wilson715d29a2010-10-18 12:02:01 -0300542 return POLLERR;
543 }
544
Dan Carpenter5c769a62010-11-17 02:12:23 -0300545 if (!ir->attached)
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300546 return POLLERR;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300547
Andy Shevchenko3656cdd2015-01-06 22:53:37 -0300548 if (ir->buf) {
549 poll_wait(file, &ir->buf->wait_poll, wait);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300550
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300551 if (lirc_buffer_empty(ir->buf))
552 ret = 0;
553 else
554 ret = POLLIN | POLLRDNORM;
Andy Shevchenko3656cdd2015-01-06 22:53:37 -0300555 } else
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300556 ret = POLLERR;
557
558 dev_dbg(ir->d.dev, LOGHEAD "poll result = %d\n",
559 ir->d.name, ir->d.minor, ret);
560
561 return ret;
562}
563EXPORT_SYMBOL(lirc_dev_fop_poll);
564
Arnd Bergmann044e5872010-08-02 15:43:35 -0300565long lirc_dev_fop_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300566{
Jarod Wilsonbe1f9852010-10-08 17:24:21 -0300567 __u32 mode;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300568 int result = 0;
Al Viro496ad9a2013-01-23 17:07:38 -0500569 struct irctl *ir = irctls[iminor(file_inode(file))];
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300570
Jarod Wilson8be292c2010-10-09 15:07:06 -0300571 if (!ir) {
Andi Shyti3fac0312016-07-06 06:01:16 -0300572 pr_err("no irctl found!\n");
Jarod Wilson8be292c2010-10-09 15:07:06 -0300573 return -ENODEV;
574 }
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300575
576 dev_dbg(ir->d.dev, LOGHEAD "ioctl called (0x%x)\n",
577 ir->d.name, ir->d.minor, cmd);
578
579 if (ir->d.minor == NOPLUG || !ir->attached) {
Andi Shyti3fac0312016-07-06 06:01:16 -0300580 dev_err(ir->d.dev, LOGHEAD "ioctl result = -ENODEV\n",
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300581 ir->d.name, ir->d.minor);
582 return -ENODEV;
583 }
584
585 mutex_lock(&ir->irctl_lock);
586
587 switch (cmd) {
588 case LIRC_GET_FEATURES:
Hans Verkuil60519af2014-08-20 19:41:03 -0300589 result = put_user(ir->d.features, (__u32 __user *)arg);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300590 break;
591 case LIRC_GET_REC_MODE:
Andi Shyti273b9022016-07-06 06:01:27 -0300592 if (LIRC_CAN_REC(ir->d.features)) {
Andi Shytib4088092016-07-06 06:01:24 -0300593 result = -ENOTTY;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300594 break;
595 }
596
597 result = put_user(LIRC_REC2MODE
598 (ir->d.features & LIRC_CAN_REC_MASK),
Hans Verkuil60519af2014-08-20 19:41:03 -0300599 (__u32 __user *)arg);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300600 break;
601 case LIRC_SET_REC_MODE:
Andi Shyti273b9022016-07-06 06:01:27 -0300602 if (LIRC_CAN_REC(ir->d.features)) {
Andi Shytib4088092016-07-06 06:01:24 -0300603 result = -ENOTTY;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300604 break;
605 }
606
Hans Verkuil60519af2014-08-20 19:41:03 -0300607 result = get_user(mode, (__u32 __user *)arg);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300608 if (!result && !(LIRC_MODE2REC(mode) & ir->d.features))
609 result = -EINVAL;
610 /*
611 * FIXME: We should actually set the mode somehow but
612 * for now, lirc_serial doesn't support mode changing either
613 */
614 break;
615 case LIRC_GET_LENGTH:
Hans Verkuil60519af2014-08-20 19:41:03 -0300616 result = put_user(ir->d.code_length, (__u32 __user *)arg);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300617 break;
618 case LIRC_GET_MIN_TIMEOUT:
619 if (!(ir->d.features & LIRC_CAN_SET_REC_TIMEOUT) ||
620 ir->d.min_timeout == 0) {
Andi Shytib4088092016-07-06 06:01:24 -0300621 result = -ENOTTY;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300622 break;
623 }
624
Hans Verkuil60519af2014-08-20 19:41:03 -0300625 result = put_user(ir->d.min_timeout, (__u32 __user *)arg);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300626 break;
627 case LIRC_GET_MAX_TIMEOUT:
628 if (!(ir->d.features & LIRC_CAN_SET_REC_TIMEOUT) ||
629 ir->d.max_timeout == 0) {
Andi Shytib4088092016-07-06 06:01:24 -0300630 result = -ENOTTY;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300631 break;
632 }
633
Hans Verkuil60519af2014-08-20 19:41:03 -0300634 result = put_user(ir->d.max_timeout, (__u32 __user *)arg);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300635 break;
636 default:
637 result = -EINVAL;
638 }
639
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300640 mutex_unlock(&ir->irctl_lock);
641
642 return result;
643}
644EXPORT_SYMBOL(lirc_dev_fop_ioctl);
645
646ssize_t lirc_dev_fop_read(struct file *file,
Dan Carpenter0e835082010-11-17 02:13:39 -0300647 char __user *buffer,
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300648 size_t length,
649 loff_t *ppos)
650{
Al Viro496ad9a2013-01-23 17:07:38 -0500651 struct irctl *ir = irctls[iminor(file_inode(file))];
Jarod Wilson715d29a2010-10-18 12:02:01 -0300652 unsigned char *buf;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300653 int ret = 0, written = 0;
654 DECLARE_WAITQUEUE(wait, current);
655
Jarod Wilson715d29a2010-10-18 12:02:01 -0300656 if (!ir) {
Andi Shyti3fac0312016-07-06 06:01:16 -0300657 pr_err("called with invalid irctl\n");
Jarod Wilson715d29a2010-10-18 12:02:01 -0300658 return -ENODEV;
659 }
660
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300661 dev_dbg(ir->d.dev, LOGHEAD "read called\n", ir->d.name, ir->d.minor);
662
Jarod Wilson715d29a2010-10-18 12:02:01 -0300663 buf = kzalloc(ir->chunk_size, GFP_KERNEL);
664 if (!buf)
665 return -ENOMEM;
666
Dan Carpenter250f7a52010-11-17 02:20:15 -0300667 if (mutex_lock_interruptible(&ir->irctl_lock)) {
668 ret = -ERESTARTSYS;
669 goto out_unlocked;
670 }
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300671 if (!ir->attached) {
Dan Carpenter250f7a52010-11-17 02:20:15 -0300672 ret = -ENODEV;
673 goto out_locked;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300674 }
675
676 if (length % ir->chunk_size) {
Dan Carpenter250f7a52010-11-17 02:20:15 -0300677 ret = -EINVAL;
678 goto out_locked;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300679 }
680
681 /*
682 * we add ourselves to the task queue before buffer check
683 * to avoid losing scan code (in case when queue is awaken somewhere
684 * between while condition checking and scheduling)
685 */
686 add_wait_queue(&ir->buf->wait_poll, &wait);
687 set_current_state(TASK_INTERRUPTIBLE);
688
689 /*
690 * while we didn't provide 'length' bytes, device is opened in blocking
691 * mode and 'copy_to_user' is happy, wait for data.
692 */
693 while (written < length && ret == 0) {
694 if (lirc_buffer_empty(ir->buf)) {
695 /* According to the read(2) man page, 'written' can be
696 * returned as less than 'length', instead of blocking
697 * again, returning -EWOULDBLOCK, or returning
Andi Shyti62e92682016-07-06 06:01:25 -0300698 * -ERESTARTSYS
699 */
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300700 if (written)
701 break;
702 if (file->f_flags & O_NONBLOCK) {
703 ret = -EWOULDBLOCK;
704 break;
705 }
706 if (signal_pending(current)) {
707 ret = -ERESTARTSYS;
708 break;
709 }
710
711 mutex_unlock(&ir->irctl_lock);
712 schedule();
713 set_current_state(TASK_INTERRUPTIBLE);
714
715 if (mutex_lock_interruptible(&ir->irctl_lock)) {
716 ret = -ERESTARTSYS;
Jarod Wilson69c271f2010-07-07 11:29:44 -0300717 remove_wait_queue(&ir->buf->wait_poll, &wait);
718 set_current_state(TASK_RUNNING);
719 goto out_unlocked;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300720 }
721
722 if (!ir->attached) {
723 ret = -ENODEV;
724 break;
725 }
726 } else {
727 lirc_buffer_read(ir->buf, buf);
Hans Verkuil60519af2014-08-20 19:41:03 -0300728 ret = copy_to_user((void __user *)buffer+written, buf,
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300729 ir->buf->chunk_size);
Dan Carpenter250f7a52010-11-17 02:20:15 -0300730 if (!ret)
731 written += ir->buf->chunk_size;
732 else
733 ret = -EFAULT;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300734 }
735 }
736
737 remove_wait_queue(&ir->buf->wait_poll, &wait);
738 set_current_state(TASK_RUNNING);
Dan Carpenter250f7a52010-11-17 02:20:15 -0300739
740out_locked:
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300741 mutex_unlock(&ir->irctl_lock);
742
Jarod Wilson69c271f2010-07-07 11:29:44 -0300743out_unlocked:
Jarod Wilson715d29a2010-10-18 12:02:01 -0300744 kfree(buf);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300745
746 return ret ? ret : written;
747}
748EXPORT_SYMBOL(lirc_dev_fop_read);
749
750void *lirc_get_pdata(struct file *file)
751{
Al Viro0990a972013-01-24 19:00:58 -0500752 return irctls[iminor(file_inode(file))]->d.data;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300753}
754EXPORT_SYMBOL(lirc_get_pdata);
755
756
Dan Carpenter0e835082010-11-17 02:13:39 -0300757ssize_t lirc_dev_fop_write(struct file *file, const char __user *buffer,
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300758 size_t length, loff_t *ppos)
759{
Al Viro496ad9a2013-01-23 17:07:38 -0500760 struct irctl *ir = irctls[iminor(file_inode(file))];
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300761
Jarod Wilson715d29a2010-10-18 12:02:01 -0300762 if (!ir) {
Andi Shyti3fac0312016-07-06 06:01:16 -0300763 pr_err("called with invalid irctl\n");
Jarod Wilson715d29a2010-10-18 12:02:01 -0300764 return -ENODEV;
765 }
766
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300767 if (!ir->attached)
768 return -ENODEV;
769
770 return -EINVAL;
771}
772EXPORT_SYMBOL(lirc_dev_fop_write);
773
774
775static int __init lirc_dev_init(void)
776{
777 int retval;
778
779 lirc_class = class_create(THIS_MODULE, "lirc");
780 if (IS_ERR(lirc_class)) {
Andi Shyti3fac0312016-07-06 06:01:16 -0300781 pr_err("class_create failed\n");
Andi Shyti54fceca2016-07-06 06:01:17 -0300782 return PTR_ERR(lirc_class);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300783 }
784
785 retval = alloc_chrdev_region(&lirc_base_dev, 0, MAX_IRCTL_DEVICES,
786 IRCTL_DEV_NAME);
787 if (retval) {
788 class_destroy(lirc_class);
Andi Shyti3fac0312016-07-06 06:01:16 -0300789 pr_err("alloc_chrdev_region failed\n");
Andi Shyti54fceca2016-07-06 06:01:17 -0300790 return retval;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300791 }
792
793
Andi Shyti3fac0312016-07-06 06:01:16 -0300794 pr_info("IR Remote Control driver registered, major %d\n",
795 MAJOR(lirc_base_dev));
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300796
Andi Shyti54fceca2016-07-06 06:01:17 -0300797 return 0;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300798}
799
800
801
802static void __exit lirc_dev_exit(void)
803{
804 class_destroy(lirc_class);
805 unregister_chrdev_region(lirc_base_dev, MAX_IRCTL_DEVICES);
Andi Shyti3fac0312016-07-06 06:01:16 -0300806 pr_info("module unloaded\n");
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300807}
808
809module_init(lirc_dev_init);
810module_exit(lirc_dev_exit);
811
812MODULE_DESCRIPTION("LIRC base driver module");
813MODULE_AUTHOR("Artur Lipowski");
814MODULE_LICENSE("GPL");
815
816module_param(debug, bool, S_IRUGO | S_IWUSR);
817MODULE_PARM_DESC(debug, "Enable debugging messages");