blob: e0e2297f13d1a0e6f33e944690804c3dbda5fddf [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 *
Jarod Wilson4a62a5a2010-07-03 01:06:57 -030016 */
17
Andi Shyti3fac0312016-07-06 06:01:16 -030018#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
19
Jarod Wilson4a62a5a2010-07-03 01:06:57 -030020#include <linux/module.h>
21#include <linux/kernel.h>
22#include <linux/sched.h>
23#include <linux/errno.h>
24#include <linux/ioctl.h>
25#include <linux/fs.h>
26#include <linux/poll.h>
27#include <linux/completion.h>
Jarod Wilson4a62a5a2010-07-03 01:06:57 -030028#include <linux/mutex.h>
29#include <linux/wait.h>
30#include <linux/unistd.h>
31#include <linux/kthread.h>
32#include <linux/bitops.h>
33#include <linux/device.h>
34#include <linux/cdev.h>
35
Srinivas Kandagatlaca7a7222013-07-22 04:23:07 -030036#include <media/rc-core.h>
Jarod Wilson4a62a5a2010-07-03 01:06:57 -030037#include <media/lirc.h>
Jarod Wilson56900852010-07-16 14:25:33 -030038#include <media/lirc_dev.h>
Jarod Wilson4a62a5a2010-07-03 01:06:57 -030039
Rusty Russell90ab5ee2012-01-13 09:32:20 +103040static bool debug;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -030041
42#define IRCTL_DEV_NAME "BaseRemoteCtl"
43#define NOPLUG -1
44#define LOGHEAD "lirc_dev (%s[%d]): "
45
46static dev_t lirc_base_dev;
47
48struct irctl {
49 struct lirc_driver d;
50 int attached;
51 int open;
52
53 struct mutex irctl_lock;
54 struct lirc_buffer *buf;
55 unsigned int chunk_size;
56
Jarod Wilson8de111e22011-05-27 16:56:50 -030057 struct cdev *cdev;
58
Jarod Wilson4a62a5a2010-07-03 01:06:57 -030059 struct task_struct *task;
60 long jiffies_to_wait;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -030061};
62
63static DEFINE_MUTEX(lirc_dev_lock);
64
65static struct irctl *irctls[MAX_IRCTL_DEVICES];
66
67/* Only used for sysfs but defined to void otherwise */
68static struct class *lirc_class;
69
70/* helper function
71 * initializes the irctl structure
72 */
Jarod Wilson578fcb82010-10-16 21:29:50 -030073static void lirc_irctl_init(struct irctl *ir)
Jarod Wilson4a62a5a2010-07-03 01:06:57 -030074{
Jarod Wilson4a62a5a2010-07-03 01:06:57 -030075 mutex_init(&ir->irctl_lock);
76 ir->d.minor = NOPLUG;
77}
78
Jarod Wilson578fcb82010-10-16 21:29:50 -030079static void lirc_irctl_cleanup(struct irctl *ir)
Jarod Wilson4a62a5a2010-07-03 01:06:57 -030080{
Jarod Wilson4a62a5a2010-07-03 01:06:57 -030081 device_destroy(lirc_class, MKDEV(MAJOR(lirc_base_dev), ir->d.minor));
82
83 if (ir->buf != ir->d.rbuf) {
84 lirc_buffer_free(ir->buf);
85 kfree(ir->buf);
86 }
87 ir->buf = NULL;
88}
89
90/* helper function
91 * reads key codes from driver and puts them into buffer
92 * returns 0 on success
93 */
Jarod Wilson578fcb82010-10-16 21:29:50 -030094static int lirc_add_to_buf(struct irctl *ir)
Jarod Wilson4a62a5a2010-07-03 01:06:57 -030095{
Andi Shyti19e56532016-07-06 06:01:19 -030096 int res;
97 int got_data = -1;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -030098
Andi Shyti19e56532016-07-06 06:01:19 -030099 if (!ir->d.add_to_buf)
100 return 0;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300101
Andi Shyti19e56532016-07-06 06:01:19 -0300102 /*
103 * service the device as long as it is returning
104 * data and we have space
105 */
106 do {
107 got_data++;
108 res = ir->d.add_to_buf(ir->d.data, ir->buf);
109 } while (!res);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300110
Andi Shyti19e56532016-07-06 06:01:19 -0300111 if (res == -ENODEV)
112 kthread_stop(ir->task);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300113
Andi Shyti19e56532016-07-06 06:01:19 -0300114 return got_data ? 0 : res;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300115}
116
117/* main function of the polling thread
118 */
119static int lirc_thread(void *irctl)
120{
121 struct irctl *ir = irctl;
122
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300123 do {
124 if (ir->open) {
125 if (ir->jiffies_to_wait) {
126 set_current_state(TASK_INTERRUPTIBLE);
127 schedule_timeout(ir->jiffies_to_wait);
128 }
129 if (kthread_should_stop())
130 break;
Jarod Wilson578fcb82010-10-16 21:29:50 -0300131 if (!lirc_add_to_buf(ir))
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300132 wake_up_interruptible(&ir->buf->wait_poll);
133 } else {
134 set_current_state(TASK_INTERRUPTIBLE);
135 schedule();
136 }
137 } while (!kthread_should_stop());
138
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300139 return 0;
140}
141
142
Al Viro75ef9de2013-04-04 19:09:41 -0400143static const struct file_operations lirc_dev_fops = {
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300144 .owner = THIS_MODULE,
145 .read = lirc_dev_fop_read,
146 .write = lirc_dev_fop_write,
147 .poll = lirc_dev_fop_poll,
Arnd Bergmann044e5872010-08-02 15:43:35 -0300148 .unlocked_ioctl = lirc_dev_fop_ioctl,
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300149 .open = lirc_dev_fop_open,
150 .release = lirc_dev_fop_close,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200151 .llseek = noop_llseek,
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300152};
153
154static int lirc_cdev_add(struct irctl *ir)
155{
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300156 struct lirc_driver *d = &ir->d;
Jarod Wilson8de111e22011-05-27 16:56:50 -0300157 struct cdev *cdev;
Sean Youngb40769e2016-11-26 19:31:24 -0200158 int retval;
Jarod Wilson8de111e22011-05-27 16:56:50 -0300159
Sean Youngafbb1102016-10-31 15:52:26 -0200160 cdev = cdev_alloc();
Jarod Wilson8de111e22011-05-27 16:56:50 -0300161 if (!cdev)
Sean Youngb40769e2016-11-26 19:31:24 -0200162 return -ENOMEM;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300163
164 if (d->fops) {
Sean Youngafbb1102016-10-31 15:52:26 -0200165 cdev->ops = d->fops;
Jarod Wilsonc1cbb702010-10-18 18:39:20 -0300166 cdev->owner = d->owner;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300167 } else {
Sean Youngafbb1102016-10-31 15:52:26 -0200168 cdev->ops = &lirc_dev_fops;
Jarod Wilsonc1cbb702010-10-18 18:39:20 -0300169 cdev->owner = THIS_MODULE;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300170 }
Vasiliy Kulikovb395cba2010-11-26 14:06:41 -0300171 retval = kobject_set_name(&cdev->kobj, "lirc%d", d->minor);
172 if (retval)
Jarod Wilson8de111e22011-05-27 16:56:50 -0300173 goto err_out;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300174
Jarod Wilsonc1cbb702010-10-18 18:39:20 -0300175 retval = cdev_add(cdev, MKDEV(MAJOR(lirc_base_dev), d->minor), 1);
Sean Youngb40769e2016-11-26 19:31:24 -0200176 if (retval)
Jarod Wilson8de111e22011-05-27 16:56:50 -0300177 goto err_out;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300178
Jarod Wilson8de111e22011-05-27 16:56:50 -0300179 ir->cdev = cdev;
180
181 return 0;
182
183err_out:
Sean Youngafbb1102016-10-31 15:52:26 -0200184 cdev_del(cdev);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300185 return retval;
186}
187
Andi Shyti6fa99e12016-07-06 06:01:13 -0300188static int lirc_allocate_buffer(struct irctl *ir)
189{
Andi Shyti70143982016-07-06 06:01:14 -0300190 int err = 0;
Andi Shyti6fa99e12016-07-06 06:01:13 -0300191 int bytes_in_key;
192 unsigned int chunk_size;
193 unsigned int buffer_size;
194 struct lirc_driver *d = &ir->d;
195
Andi Shyti70143982016-07-06 06:01:14 -0300196 mutex_lock(&lirc_dev_lock);
197
Andi Shyti6fa99e12016-07-06 06:01:13 -0300198 bytes_in_key = BITS_TO_LONGS(d->code_length) +
199 (d->code_length % 8 ? 1 : 0);
200 buffer_size = d->buffer_size ? d->buffer_size : BUFLEN / bytes_in_key;
201 chunk_size = d->chunk_size ? d->chunk_size : bytes_in_key;
202
203 if (d->rbuf) {
204 ir->buf = d->rbuf;
205 } else {
206 ir->buf = kmalloc(sizeof(struct lirc_buffer), GFP_KERNEL);
Andi Shyti70143982016-07-06 06:01:14 -0300207 if (!ir->buf) {
208 err = -ENOMEM;
209 goto out;
210 }
Andi Shyti6fa99e12016-07-06 06:01:13 -0300211
212 err = lirc_buffer_init(ir->buf, chunk_size, buffer_size);
213 if (err) {
214 kfree(ir->buf);
Andi Shyti70143982016-07-06 06:01:14 -0300215 goto out;
Andi Shyti6fa99e12016-07-06 06:01:13 -0300216 }
217 }
218 ir->chunk_size = ir->buf->chunk_size;
219
Andi Shyti70143982016-07-06 06:01:14 -0300220out:
221 mutex_unlock(&lirc_dev_lock);
222
223 return err;
Andi Shyti6fa99e12016-07-06 06:01:13 -0300224}
225
Andi Shyti70143982016-07-06 06:01:14 -0300226static int lirc_allocate_driver(struct lirc_driver *d)
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300227{
228 struct irctl *ir;
229 int minor;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300230 int err;
231
232 if (!d) {
Andi Shyti3fac0312016-07-06 06:01:16 -0300233 pr_err("driver pointer must be not NULL!\n");
Andi Shyti54fceca2016-07-06 06:01:17 -0300234 return -EBADRQC;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300235 }
236
Jarod Wilson715d29a72010-10-18 12:02:01 -0300237 if (!d->dev) {
Andi Shyti3fac0312016-07-06 06:01:16 -0300238 pr_err("dev pointer not filled in!\n");
Andi Shyti54fceca2016-07-06 06:01:17 -0300239 return -EINVAL;
Jarod Wilson715d29a72010-10-18 12:02:01 -0300240 }
241
Andi Shyti9675ee52016-07-06 06:01:23 -0300242 if (d->minor >= MAX_IRCTL_DEVICES) {
Andi Shyti3fac0312016-07-06 06:01:16 -0300243 dev_err(d->dev, "minor must be between 0 and %d!\n",
244 MAX_IRCTL_DEVICES - 1);
Andi Shyti54fceca2016-07-06 06:01:17 -0300245 return -EBADRQC;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300246 }
247
Andi Shyti9675ee52016-07-06 06:01:23 -0300248 if (d->code_length < 1 || d->code_length > (BUFLEN * 8)) {
Andi Shyti3fac0312016-07-06 06:01:16 -0300249 dev_err(d->dev, "code length must be less than %d bits\n",
250 BUFLEN * 8);
Andi Shyti54fceca2016-07-06 06:01:17 -0300251 return -EBADRQC;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300252 }
253
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300254 if (d->sample_rate) {
255 if (2 > d->sample_rate || HZ < d->sample_rate) {
Andi Shyti3fac0312016-07-06 06:01:16 -0300256 dev_err(d->dev, "invalid %d sample rate\n",
257 d->sample_rate);
Andi Shyti54fceca2016-07-06 06:01:17 -0300258 return -EBADRQC;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300259 }
260 if (!d->add_to_buf) {
Andi Shyti3fac0312016-07-06 06:01:16 -0300261 dev_err(d->dev, "add_to_buf not set\n");
Andi Shyti54fceca2016-07-06 06:01:17 -0300262 return -EBADRQC;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300263 }
Andi Shyti14db9fc2016-07-06 06:01:21 -0300264 } else if (!d->rbuf && !(d->fops && d->fops->read &&
265 d->fops->poll && d->fops->unlocked_ioctl)) {
266 dev_err(d->dev, "undefined read, poll, ioctl\n");
Andi Shyti54fceca2016-07-06 06:01:17 -0300267 return -EBADRQC;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300268 }
269
270 mutex_lock(&lirc_dev_lock);
271
272 minor = d->minor;
273
274 if (minor < 0) {
275 /* find first free slot for driver */
276 for (minor = 0; minor < MAX_IRCTL_DEVICES; minor++)
277 if (!irctls[minor])
278 break;
Andi Shyti9675ee52016-07-06 06:01:23 -0300279 if (minor == MAX_IRCTL_DEVICES) {
Andi Shyti3fac0312016-07-06 06:01:16 -0300280 dev_err(d->dev, "no free slots for drivers!\n");
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300281 err = -ENOMEM;
282 goto out_lock;
283 }
284 } else if (irctls[minor]) {
Andi Shyti3fac0312016-07-06 06:01:16 -0300285 dev_err(d->dev, "minor (%d) just registered!\n", minor);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300286 err = -EBUSY;
287 goto out_lock;
288 }
289
290 ir = kzalloc(sizeof(struct irctl), GFP_KERNEL);
291 if (!ir) {
292 err = -ENOMEM;
293 goto out_lock;
294 }
Jarod Wilson578fcb82010-10-16 21:29:50 -0300295 lirc_irctl_init(ir);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300296 irctls[minor] = ir;
297 d->minor = minor;
298
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300299 /* some safety check 8-) */
300 d->name[sizeof(d->name)-1] = '\0';
301
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300302 if (d->features == 0)
303 d->features = LIRC_CAN_REC_LIRCCODE;
304
305 ir->d = *d;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300306
307 device_create(lirc_class, ir->d.dev,
308 MKDEV(MAJOR(lirc_base_dev), ir->d.minor), NULL,
309 "lirc%u", ir->d.minor);
310
311 if (d->sample_rate) {
Andi Shyti6ab86d22016-07-06 06:01:20 -0300312 ir->jiffies_to_wait = HZ / d->sample_rate;
313
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300314 /* try to fire up polling thread */
315 ir->task = kthread_run(lirc_thread, (void *)ir, "lirc_dev");
316 if (IS_ERR(ir->task)) {
Andi Shyti3fac0312016-07-06 06:01:16 -0300317 dev_err(d->dev, "cannot run thread for minor = %d\n",
318 d->minor);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300319 err = -ECHILD;
320 goto out_sysfs;
321 }
Andi Shyti6ab86d22016-07-06 06:01:20 -0300322 } else {
323 /* it means - wait for external event in task queue */
324 ir->jiffies_to_wait = 0;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300325 }
326
327 err = lirc_cdev_add(ir);
328 if (err)
329 goto out_sysfs;
330
331 ir->attached = 1;
332 mutex_unlock(&lirc_dev_lock);
333
334 dev_info(ir->d.dev, "lirc_dev: driver %s registered at minor = %d\n",
335 ir->d.name, ir->d.minor);
336 return minor;
337
338out_sysfs:
339 device_destroy(lirc_class, MKDEV(MAJOR(lirc_base_dev), ir->d.minor));
340out_lock:
341 mutex_unlock(&lirc_dev_lock);
Andi Shyti54fceca2016-07-06 06:01:17 -0300342
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300343 return err;
344}
Andi Shyti70143982016-07-06 06:01:14 -0300345
346int lirc_register_driver(struct lirc_driver *d)
347{
348 int minor, err = 0;
349
350 minor = lirc_allocate_driver(d);
351 if (minor < 0)
352 return minor;
353
354 if (LIRC_CAN_REC(d->features)) {
355 err = lirc_allocate_buffer(irctls[minor]);
356 if (err)
357 lirc_unregister_driver(minor);
358 }
359
360 return err ? err : minor;
361}
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300362EXPORT_SYMBOL(lirc_register_driver);
363
364int lirc_unregister_driver(int minor)
365{
366 struct irctl *ir;
Jarod Wilsonc1cbb702010-10-18 18:39:20 -0300367 struct cdev *cdev;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300368
369 if (minor < 0 || minor >= MAX_IRCTL_DEVICES) {
Andi Shyti3fac0312016-07-06 06:01:16 -0300370 pr_err("minor (%d) must be between 0 and %d!\n",
371 minor, MAX_IRCTL_DEVICES - 1);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300372 return -EBADRQC;
373 }
374
375 ir = irctls[minor];
Jarod Wilsondf1868e2010-09-17 18:12:31 -0300376 if (!ir) {
Andi Shyti3fac0312016-07-06 06:01:16 -0300377 pr_err("failed to get irctl\n");
Jarod Wilsondf1868e2010-09-17 18:12:31 -0300378 return -ENOENT;
379 }
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300380
Jarod Wilson8de111e22011-05-27 16:56:50 -0300381 cdev = ir->cdev;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300382
383 mutex_lock(&lirc_dev_lock);
384
385 if (ir->d.minor != minor) {
Andi Shyti3fac0312016-07-06 06:01:16 -0300386 dev_err(ir->d.dev, "lirc_dev: minor %d device not registered\n",
387 minor);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300388 mutex_unlock(&lirc_dev_lock);
389 return -ENOENT;
390 }
391
392 /* end up polling thread */
393 if (ir->task)
394 kthread_stop(ir->task);
395
396 dev_dbg(ir->d.dev, "lirc_dev: driver %s unregistered from minor = %d\n",
397 ir->d.name, ir->d.minor);
398
399 ir->attached = 0;
400 if (ir->open) {
401 dev_dbg(ir->d.dev, LOGHEAD "releasing opened driver\n",
402 ir->d.name, ir->d.minor);
403 wake_up_interruptible(&ir->buf->wait_poll);
404 mutex_lock(&ir->irctl_lock);
Andi Shyti8e5fa4c2016-07-06 06:01:26 -0300405
406 if (ir->d.set_use_dec)
407 ir->d.set_use_dec(ir->d.data);
408
Jarod Wilsonc1cbb702010-10-18 18:39:20 -0300409 module_put(cdev->owner);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300410 mutex_unlock(&ir->irctl_lock);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300411 } else {
Jarod Wilson578fcb82010-10-16 21:29:50 -0300412 lirc_irctl_cleanup(ir);
Jarod Wilsonc1cbb702010-10-18 18:39:20 -0300413 cdev_del(cdev);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300414 kfree(ir);
415 irctls[minor] = NULL;
416 }
417
418 mutex_unlock(&lirc_dev_lock);
419
420 return 0;
421}
422EXPORT_SYMBOL(lirc_unregister_driver);
423
424int lirc_dev_fop_open(struct inode *inode, struct file *file)
425{
426 struct irctl *ir;
Jarod Wilsonc1cbb702010-10-18 18:39:20 -0300427 struct cdev *cdev;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300428 int retval = 0;
429
430 if (iminor(inode) >= MAX_IRCTL_DEVICES) {
Andi Shyti3fac0312016-07-06 06:01:16 -0300431 pr_err("open result for %d is -ENODEV\n", iminor(inode));
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300432 return -ENODEV;
433 }
434
435 if (mutex_lock_interruptible(&lirc_dev_lock))
436 return -ERESTARTSYS;
437
438 ir = irctls[iminor(inode)];
439 if (!ir) {
440 retval = -ENODEV;
441 goto error;
442 }
443
444 dev_dbg(ir->d.dev, LOGHEAD "open called\n", ir->d.name, ir->d.minor);
445
446 if (ir->d.minor == NOPLUG) {
447 retval = -ENODEV;
448 goto error;
449 }
450
451 if (ir->open) {
452 retval = -EBUSY;
453 goto error;
454 }
455
Srinivas Kandagatlaca7a7222013-07-22 04:23:07 -0300456 if (ir->d.rdev) {
457 retval = rc_open(ir->d.rdev);
458 if (retval)
459 goto error;
460 }
461
Jarod Wilson8de111e22011-05-27 16:56:50 -0300462 cdev = ir->cdev;
Jarod Wilsonc1cbb702010-10-18 18:39:20 -0300463 if (try_module_get(cdev->owner)) {
464 ir->open++;
Andi Shyti8e5fa4c2016-07-06 06:01:26 -0300465 if (ir->d.set_use_inc)
466 retval = ir->d.set_use_inc(ir->d.data);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300467
468 if (retval) {
Jarod Wilsonc1cbb702010-10-18 18:39:20 -0300469 module_put(cdev->owner);
470 ir->open--;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300471 } else {
472 lirc_buffer_clear(ir->buf);
473 }
474 if (ir->task)
475 wake_up_process(ir->task);
476 }
477
478error:
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300479 mutex_unlock(&lirc_dev_lock);
480
Arnd Bergmannd9d2e9d2010-08-15 18:51:56 +0200481 nonseekable_open(inode, file);
482
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300483 return retval;
484}
485EXPORT_SYMBOL(lirc_dev_fop_open);
486
487int lirc_dev_fop_close(struct inode *inode, struct file *file)
488{
489 struct irctl *ir = irctls[iminor(inode)];
Jarod Wilson8de111e22011-05-27 16:56:50 -0300490 struct cdev *cdev;
Mauro Carvalho Chehabb64e10f2016-02-27 07:51:13 -0300491 int ret;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300492
Jarod Wilson715d29a72010-10-18 12:02:01 -0300493 if (!ir) {
Andi Shyti3fac0312016-07-06 06:01:16 -0300494 pr_err("called with invalid irctl\n");
Jarod Wilson715d29a72010-10-18 12:02:01 -0300495 return -EINVAL;
496 }
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300497
Jarod Wilson8de111e22011-05-27 16:56:50 -0300498 cdev = ir->cdev;
499
Mauro Carvalho Chehabb64e10f2016-02-27 07:51:13 -0300500 ret = mutex_lock_killable(&lirc_dev_lock);
501 WARN_ON(ret);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300502
Markus Elfring3dd94f02014-11-20 09:01:32 -0300503 rc_close(ir->d.rdev);
Srinivas Kandagatlaca7a7222013-07-22 04:23:07 -0300504
Jarod Wilson715d29a72010-10-18 12:02:01 -0300505 ir->open--;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300506 if (ir->attached) {
Andi Shyti8e5fa4c2016-07-06 06:01:26 -0300507 if (ir->d.set_use_dec)
508 ir->d.set_use_dec(ir->d.data);
Jarod Wilsonc1cbb702010-10-18 18:39:20 -0300509 module_put(cdev->owner);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300510 } else {
Jarod Wilson578fcb82010-10-16 21:29:50 -0300511 lirc_irctl_cleanup(ir);
Jarod Wilsonc1cbb702010-10-18 18:39:20 -0300512 cdev_del(cdev);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300513 irctls[ir->d.minor] = NULL;
514 kfree(ir);
515 }
516
Mauro Carvalho Chehabb64e10f2016-02-27 07:51:13 -0300517 if (!ret)
518 mutex_unlock(&lirc_dev_lock);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300519
520 return 0;
521}
522EXPORT_SYMBOL(lirc_dev_fop_close);
523
524unsigned int lirc_dev_fop_poll(struct file *file, poll_table *wait)
525{
Al Viro496ad9a2013-01-23 17:07:38 -0500526 struct irctl *ir = irctls[iminor(file_inode(file))];
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300527 unsigned int ret;
528
Jarod Wilson715d29a72010-10-18 12:02:01 -0300529 if (!ir) {
Andi Shyti3fac0312016-07-06 06:01:16 -0300530 pr_err("called with invalid irctl\n");
Jarod Wilson715d29a72010-10-18 12:02:01 -0300531 return POLLERR;
532 }
533
Dan Carpenter5c769a62010-11-17 02:12:23 -0300534 if (!ir->attached)
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300535 return POLLERR;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300536
Andy Shevchenko3656cdd2015-01-06 22:53:37 -0300537 if (ir->buf) {
538 poll_wait(file, &ir->buf->wait_poll, wait);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300539
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300540 if (lirc_buffer_empty(ir->buf))
541 ret = 0;
542 else
543 ret = POLLIN | POLLRDNORM;
Andy Shevchenko3656cdd2015-01-06 22:53:37 -0300544 } else
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300545 ret = POLLERR;
546
547 dev_dbg(ir->d.dev, LOGHEAD "poll result = %d\n",
548 ir->d.name, ir->d.minor, ret);
549
550 return ret;
551}
552EXPORT_SYMBOL(lirc_dev_fop_poll);
553
Arnd Bergmann044e5872010-08-02 15:43:35 -0300554long lirc_dev_fop_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300555{
Jarod Wilsonbe1f9852010-10-08 17:24:21 -0300556 __u32 mode;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300557 int result = 0;
Al Viro496ad9a2013-01-23 17:07:38 -0500558 struct irctl *ir = irctls[iminor(file_inode(file))];
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300559
Jarod Wilson8be292c2010-10-09 15:07:06 -0300560 if (!ir) {
Andi Shyti3fac0312016-07-06 06:01:16 -0300561 pr_err("no irctl found!\n");
Jarod Wilson8be292c2010-10-09 15:07:06 -0300562 return -ENODEV;
563 }
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300564
565 dev_dbg(ir->d.dev, LOGHEAD "ioctl called (0x%x)\n",
566 ir->d.name, ir->d.minor, cmd);
567
568 if (ir->d.minor == NOPLUG || !ir->attached) {
Andi Shyti3fac0312016-07-06 06:01:16 -0300569 dev_err(ir->d.dev, LOGHEAD "ioctl result = -ENODEV\n",
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300570 ir->d.name, ir->d.minor);
571 return -ENODEV;
572 }
573
574 mutex_lock(&ir->irctl_lock);
575
576 switch (cmd) {
577 case LIRC_GET_FEATURES:
Hans Verkuil60519af2014-08-20 19:41:03 -0300578 result = put_user(ir->d.features, (__u32 __user *)arg);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300579 break;
580 case LIRC_GET_REC_MODE:
Sean Youngbd291202016-12-02 15:16:08 -0200581 if (!LIRC_CAN_REC(ir->d.features)) {
Andi Shytib4088092016-07-06 06:01:24 -0300582 result = -ENOTTY;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300583 break;
584 }
585
586 result = put_user(LIRC_REC2MODE
587 (ir->d.features & LIRC_CAN_REC_MASK),
Hans Verkuil60519af2014-08-20 19:41:03 -0300588 (__u32 __user *)arg);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300589 break;
590 case LIRC_SET_REC_MODE:
Sean Youngbd291202016-12-02 15:16:08 -0200591 if (!LIRC_CAN_REC(ir->d.features)) {
Andi Shytib4088092016-07-06 06:01:24 -0300592 result = -ENOTTY;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300593 break;
594 }
595
Hans Verkuil60519af2014-08-20 19:41:03 -0300596 result = get_user(mode, (__u32 __user *)arg);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300597 if (!result && !(LIRC_MODE2REC(mode) & ir->d.features))
598 result = -EINVAL;
599 /*
600 * FIXME: We should actually set the mode somehow but
601 * for now, lirc_serial doesn't support mode changing either
602 */
603 break;
604 case LIRC_GET_LENGTH:
Hans Verkuil60519af2014-08-20 19:41:03 -0300605 result = put_user(ir->d.code_length, (__u32 __user *)arg);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300606 break;
607 case LIRC_GET_MIN_TIMEOUT:
608 if (!(ir->d.features & LIRC_CAN_SET_REC_TIMEOUT) ||
609 ir->d.min_timeout == 0) {
Andi Shytib4088092016-07-06 06:01:24 -0300610 result = -ENOTTY;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300611 break;
612 }
613
Hans Verkuil60519af2014-08-20 19:41:03 -0300614 result = put_user(ir->d.min_timeout, (__u32 __user *)arg);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300615 break;
616 case LIRC_GET_MAX_TIMEOUT:
617 if (!(ir->d.features & LIRC_CAN_SET_REC_TIMEOUT) ||
618 ir->d.max_timeout == 0) {
Andi Shytib4088092016-07-06 06:01:24 -0300619 result = -ENOTTY;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300620 break;
621 }
622
Hans Verkuil60519af2014-08-20 19:41:03 -0300623 result = put_user(ir->d.max_timeout, (__u32 __user *)arg);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300624 break;
625 default:
626 result = -EINVAL;
627 }
628
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300629 mutex_unlock(&ir->irctl_lock);
630
631 return result;
632}
633EXPORT_SYMBOL(lirc_dev_fop_ioctl);
634
635ssize_t lirc_dev_fop_read(struct file *file,
Dan Carpenter0e835082010-11-17 02:13:39 -0300636 char __user *buffer,
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300637 size_t length,
638 loff_t *ppos)
639{
Al Viro496ad9a2013-01-23 17:07:38 -0500640 struct irctl *ir = irctls[iminor(file_inode(file))];
Jarod Wilson715d29a72010-10-18 12:02:01 -0300641 unsigned char *buf;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300642 int ret = 0, written = 0;
643 DECLARE_WAITQUEUE(wait, current);
644
Jarod Wilson715d29a72010-10-18 12:02:01 -0300645 if (!ir) {
Andi Shyti3fac0312016-07-06 06:01:16 -0300646 pr_err("called with invalid irctl\n");
Jarod Wilson715d29a72010-10-18 12:02:01 -0300647 return -ENODEV;
648 }
649
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300650 dev_dbg(ir->d.dev, LOGHEAD "read called\n", ir->d.name, ir->d.minor);
651
Jarod Wilson715d29a72010-10-18 12:02:01 -0300652 buf = kzalloc(ir->chunk_size, GFP_KERNEL);
653 if (!buf)
654 return -ENOMEM;
655
Dan Carpenter250f7a52010-11-17 02:20:15 -0300656 if (mutex_lock_interruptible(&ir->irctl_lock)) {
657 ret = -ERESTARTSYS;
658 goto out_unlocked;
659 }
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300660 if (!ir->attached) {
Dan Carpenter250f7a52010-11-17 02:20:15 -0300661 ret = -ENODEV;
662 goto out_locked;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300663 }
664
665 if (length % ir->chunk_size) {
Dan Carpenter250f7a52010-11-17 02:20:15 -0300666 ret = -EINVAL;
667 goto out_locked;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300668 }
669
670 /*
671 * we add ourselves to the task queue before buffer check
672 * to avoid losing scan code (in case when queue is awaken somewhere
673 * between while condition checking and scheduling)
674 */
675 add_wait_queue(&ir->buf->wait_poll, &wait);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300676
677 /*
678 * while we didn't provide 'length' bytes, device is opened in blocking
679 * mode and 'copy_to_user' is happy, wait for data.
680 */
681 while (written < length && ret == 0) {
682 if (lirc_buffer_empty(ir->buf)) {
683 /* According to the read(2) man page, 'written' can be
684 * returned as less than 'length', instead of blocking
685 * again, returning -EWOULDBLOCK, or returning
Andi Shyti62e92682016-07-06 06:01:25 -0300686 * -ERESTARTSYS
687 */
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300688 if (written)
689 break;
690 if (file->f_flags & O_NONBLOCK) {
691 ret = -EWOULDBLOCK;
692 break;
693 }
694 if (signal_pending(current)) {
695 ret = -ERESTARTSYS;
696 break;
697 }
698
699 mutex_unlock(&ir->irctl_lock);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300700 set_current_state(TASK_INTERRUPTIBLE);
Sean Young12accdc2016-10-31 15:52:25 -0200701 schedule();
702 set_current_state(TASK_RUNNING);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300703
704 if (mutex_lock_interruptible(&ir->irctl_lock)) {
705 ret = -ERESTARTSYS;
Jarod Wilson69c271f2010-07-07 11:29:44 -0300706 remove_wait_queue(&ir->buf->wait_poll, &wait);
Jarod Wilson69c271f2010-07-07 11:29:44 -0300707 goto out_unlocked;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300708 }
709
710 if (!ir->attached) {
711 ret = -ENODEV;
Sean Youngc77d17c02016-10-31 15:52:27 -0200712 goto out_locked;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300713 }
714 } else {
715 lirc_buffer_read(ir->buf, buf);
Hans Verkuil60519af2014-08-20 19:41:03 -0300716 ret = copy_to_user((void __user *)buffer+written, buf,
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300717 ir->buf->chunk_size);
Dan Carpenter250f7a52010-11-17 02:20:15 -0300718 if (!ret)
719 written += ir->buf->chunk_size;
720 else
721 ret = -EFAULT;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300722 }
723 }
724
725 remove_wait_queue(&ir->buf->wait_poll, &wait);
Dan Carpenter250f7a52010-11-17 02:20:15 -0300726
727out_locked:
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300728 mutex_unlock(&ir->irctl_lock);
729
Jarod Wilson69c271f2010-07-07 11:29:44 -0300730out_unlocked:
Jarod Wilson715d29a72010-10-18 12:02:01 -0300731 kfree(buf);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300732
733 return ret ? ret : written;
734}
735EXPORT_SYMBOL(lirc_dev_fop_read);
736
737void *lirc_get_pdata(struct file *file)
738{
Al Viro0990a972013-01-24 19:00:58 -0500739 return irctls[iminor(file_inode(file))]->d.data;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300740}
741EXPORT_SYMBOL(lirc_get_pdata);
742
743
Dan Carpenter0e835082010-11-17 02:13:39 -0300744ssize_t lirc_dev_fop_write(struct file *file, const char __user *buffer,
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300745 size_t length, loff_t *ppos)
746{
Al Viro496ad9a2013-01-23 17:07:38 -0500747 struct irctl *ir = irctls[iminor(file_inode(file))];
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300748
Jarod Wilson715d29a72010-10-18 12:02:01 -0300749 if (!ir) {
Andi Shyti3fac0312016-07-06 06:01:16 -0300750 pr_err("called with invalid irctl\n");
Jarod Wilson715d29a72010-10-18 12:02:01 -0300751 return -ENODEV;
752 }
753
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300754 if (!ir->attached)
755 return -ENODEV;
756
757 return -EINVAL;
758}
759EXPORT_SYMBOL(lirc_dev_fop_write);
760
761
762static int __init lirc_dev_init(void)
763{
764 int retval;
765
766 lirc_class = class_create(THIS_MODULE, "lirc");
767 if (IS_ERR(lirc_class)) {
Andi Shyti3fac0312016-07-06 06:01:16 -0300768 pr_err("class_create failed\n");
Andi Shyti54fceca2016-07-06 06:01:17 -0300769 return PTR_ERR(lirc_class);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300770 }
771
772 retval = alloc_chrdev_region(&lirc_base_dev, 0, MAX_IRCTL_DEVICES,
773 IRCTL_DEV_NAME);
774 if (retval) {
775 class_destroy(lirc_class);
Andi Shyti3fac0312016-07-06 06:01:16 -0300776 pr_err("alloc_chrdev_region failed\n");
Andi Shyti54fceca2016-07-06 06:01:17 -0300777 return retval;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300778 }
779
780
Andi Shyti3fac0312016-07-06 06:01:16 -0300781 pr_info("IR Remote Control driver registered, major %d\n",
782 MAJOR(lirc_base_dev));
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300783
Andi Shyti54fceca2016-07-06 06:01:17 -0300784 return 0;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300785}
786
787
788
789static void __exit lirc_dev_exit(void)
790{
791 class_destroy(lirc_class);
792 unregister_chrdev_region(lirc_base_dev, MAX_IRCTL_DEVICES);
Andi Shyti3fac0312016-07-06 06:01:16 -0300793 pr_info("module unloaded\n");
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300794}
795
796module_init(lirc_dev_init);
797module_exit(lirc_dev_exit);
798
799MODULE_DESCRIPTION("LIRC base driver module");
800MODULE_AUTHOR("Artur Lipowski");
801MODULE_LICENSE("GPL");
802
803module_param(debug, bool, S_IRUGO | S_IWUSR);
804MODULE_PARM_DESC(debug, "Enable debugging messages");