blob: 42704552b005e213056411eeb0c39128f328ee5d [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>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010022#include <linux/sched/signal.h>
Jarod Wilson4a62a5a2010-07-03 01:06:57 -030023#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;
David Härdeman0f7c4062017-05-01 10:32:34 -030055 bool buf_internal;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -030056 unsigned int chunk_size;
57
Sean Young74c839b2017-01-30 13:49:58 -020058 struct device dev;
59 struct cdev cdev;
Jarod Wilson8de111e22011-05-27 16:56:50 -030060
Jarod Wilson4a62a5a2010-07-03 01:06:57 -030061 struct task_struct *task;
62 long jiffies_to_wait;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -030063};
64
65static DEFINE_MUTEX(lirc_dev_lock);
66
67static struct irctl *irctls[MAX_IRCTL_DEVICES];
68
69/* Only used for sysfs but defined to void otherwise */
70static struct class *lirc_class;
71
72/* helper function
73 * initializes the irctl structure
74 */
Jarod Wilson578fcb82010-10-16 21:29:50 -030075static void lirc_irctl_init(struct irctl *ir)
Jarod Wilson4a62a5a2010-07-03 01:06:57 -030076{
Jarod Wilson4a62a5a2010-07-03 01:06:57 -030077 mutex_init(&ir->irctl_lock);
78 ir->d.minor = NOPLUG;
79}
80
Sean Young74c839b2017-01-30 13:49:58 -020081static void lirc_release(struct device *ld)
Jarod Wilson4a62a5a2010-07-03 01:06:57 -030082{
Sean Young74c839b2017-01-30 13:49:58 -020083 struct irctl *ir = container_of(ld, struct irctl, dev);
84
85 put_device(ir->dev.parent);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -030086
David Härdeman0f7c4062017-05-01 10:32:34 -030087 if (ir->buf_internal) {
Jarod Wilson4a62a5a2010-07-03 01:06:57 -030088 lirc_buffer_free(ir->buf);
89 kfree(ir->buf);
90 }
Sean Young74c839b2017-01-30 13:49:58 -020091
92 mutex_lock(&lirc_dev_lock);
93 irctls[ir->d.minor] = NULL;
94 mutex_unlock(&lirc_dev_lock);
95 kfree(ir);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -030096}
97
98/* helper function
99 * reads key codes from driver and puts them into buffer
100 * returns 0 on success
101 */
Jarod Wilson578fcb82010-10-16 21:29:50 -0300102static int lirc_add_to_buf(struct irctl *ir)
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300103{
Andi Shyti19e56532016-07-06 06:01:19 -0300104 int res;
105 int got_data = -1;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300106
Andi Shyti19e56532016-07-06 06:01:19 -0300107 if (!ir->d.add_to_buf)
108 return 0;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300109
Andi Shyti19e56532016-07-06 06:01:19 -0300110 /*
111 * service the device as long as it is returning
112 * data and we have space
113 */
114 do {
115 got_data++;
116 res = ir->d.add_to_buf(ir->d.data, ir->buf);
117 } while (!res);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300118
Andi Shyti19e56532016-07-06 06:01:19 -0300119 if (res == -ENODEV)
120 kthread_stop(ir->task);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300121
Andi Shyti19e56532016-07-06 06:01:19 -0300122 return got_data ? 0 : res;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300123}
124
125/* main function of the polling thread
126 */
127static int lirc_thread(void *irctl)
128{
129 struct irctl *ir = irctl;
130
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300131 do {
132 if (ir->open) {
133 if (ir->jiffies_to_wait) {
134 set_current_state(TASK_INTERRUPTIBLE);
135 schedule_timeout(ir->jiffies_to_wait);
136 }
137 if (kthread_should_stop())
138 break;
Jarod Wilson578fcb82010-10-16 21:29:50 -0300139 if (!lirc_add_to_buf(ir))
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300140 wake_up_interruptible(&ir->buf->wait_poll);
141 } else {
142 set_current_state(TASK_INTERRUPTIBLE);
143 schedule();
144 }
145 } while (!kthread_should_stop());
146
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300147 return 0;
148}
149
150
Al Viro75ef9de2013-04-04 19:09:41 -0400151static const struct file_operations lirc_dev_fops = {
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300152 .owner = THIS_MODULE,
153 .read = lirc_dev_fop_read,
154 .write = lirc_dev_fop_write,
155 .poll = lirc_dev_fop_poll,
Arnd Bergmann044e5872010-08-02 15:43:35 -0300156 .unlocked_ioctl = lirc_dev_fop_ioctl,
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300157 .open = lirc_dev_fop_open,
158 .release = lirc_dev_fop_close,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200159 .llseek = noop_llseek,
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300160};
161
162static int lirc_cdev_add(struct irctl *ir)
163{
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;
Sean Youngb40769e2016-11-26 19:31:24 -0200166 int retval;
Jarod Wilson8de111e22011-05-27 16:56:50 -0300167
Sean Young74c839b2017-01-30 13:49:58 -0200168 cdev = &ir->cdev;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300169
170 if (d->fops) {
Sean Young74c839b2017-01-30 13:49:58 -0200171 cdev_init(cdev, d->fops);
Jarod Wilsonc1cbb702010-10-18 18:39:20 -0300172 cdev->owner = d->owner;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300173 } else {
Sean Young74c839b2017-01-30 13:49:58 -0200174 cdev_init(cdev, &lirc_dev_fops);
Jarod Wilsonc1cbb702010-10-18 18:39:20 -0300175 cdev->owner = THIS_MODULE;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300176 }
Vasiliy Kulikovb395cba2010-11-26 14:06:41 -0300177 retval = kobject_set_name(&cdev->kobj, "lirc%d", d->minor);
178 if (retval)
Sean Young74c839b2017-01-30 13:49:58 -0200179 return retval;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300180
Sean Young74c839b2017-01-30 13:49:58 -0200181 cdev->kobj.parent = &ir->dev.kobj;
182 return cdev_add(cdev, ir->dev.devt, 1);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300183}
184
Andi Shyti6fa99e12016-07-06 06:01:13 -0300185static int lirc_allocate_buffer(struct irctl *ir)
186{
Andi Shyti70143982016-07-06 06:01:14 -0300187 int err = 0;
Andi Shyti6fa99e12016-07-06 06:01:13 -0300188 int bytes_in_key;
189 unsigned int chunk_size;
190 unsigned int buffer_size;
191 struct lirc_driver *d = &ir->d;
192
Andi Shyti70143982016-07-06 06:01:14 -0300193 mutex_lock(&lirc_dev_lock);
194
Andi Shyti6fa99e12016-07-06 06:01:13 -0300195 bytes_in_key = BITS_TO_LONGS(d->code_length) +
196 (d->code_length % 8 ? 1 : 0);
197 buffer_size = d->buffer_size ? d->buffer_size : BUFLEN / bytes_in_key;
198 chunk_size = d->chunk_size ? d->chunk_size : bytes_in_key;
199
200 if (d->rbuf) {
201 ir->buf = d->rbuf;
David Härdeman0f7c4062017-05-01 10:32:34 -0300202 ir->buf_internal = false;
Andi Shyti6fa99e12016-07-06 06:01:13 -0300203 } else {
204 ir->buf = kmalloc(sizeof(struct lirc_buffer), GFP_KERNEL);
Andi Shyti70143982016-07-06 06:01:14 -0300205 if (!ir->buf) {
206 err = -ENOMEM;
207 goto out;
208 }
Andi Shyti6fa99e12016-07-06 06:01:13 -0300209
210 err = lirc_buffer_init(ir->buf, chunk_size, buffer_size);
211 if (err) {
212 kfree(ir->buf);
David Härdeman0f7c4062017-05-01 10:32:34 -0300213 ir->buf = NULL;
Andi Shyti70143982016-07-06 06:01:14 -0300214 goto out;
Andi Shyti6fa99e12016-07-06 06:01:13 -0300215 }
David Härdeman0f7c4062017-05-01 10:32:34 -0300216
217 ir->buf_internal = true;
Andi Shyti6fa99e12016-07-06 06:01:13 -0300218 }
219 ir->chunk_size = ir->buf->chunk_size;
220
Andi Shyti70143982016-07-06 06:01:14 -0300221out:
222 mutex_unlock(&lirc_dev_lock);
223
224 return err;
Andi Shyti6fa99e12016-07-06 06:01:13 -0300225}
226
Andi Shyti70143982016-07-06 06:01:14 -0300227static int lirc_allocate_driver(struct lirc_driver *d)
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300228{
229 struct irctl *ir;
230 int minor;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300231 int err;
232
233 if (!d) {
Andi Shyti3fac0312016-07-06 06:01:16 -0300234 pr_err("driver pointer must be not NULL!\n");
Andi Shyti54fceca2016-07-06 06:01:17 -0300235 return -EBADRQC;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300236 }
237
Jarod Wilson715d29a72010-10-18 12:02:01 -0300238 if (!d->dev) {
Andi Shyti3fac0312016-07-06 06:01:16 -0300239 pr_err("dev pointer not filled in!\n");
Andi Shyti54fceca2016-07-06 06:01:17 -0300240 return -EINVAL;
Jarod Wilson715d29a72010-10-18 12:02:01 -0300241 }
242
Andi Shyti9675ee52016-07-06 06:01:23 -0300243 if (d->minor >= MAX_IRCTL_DEVICES) {
Andi Shyti3fac0312016-07-06 06:01:16 -0300244 dev_err(d->dev, "minor must be between 0 and %d!\n",
245 MAX_IRCTL_DEVICES - 1);
Andi Shyti54fceca2016-07-06 06:01:17 -0300246 return -EBADRQC;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300247 }
248
Andi Shyti9675ee52016-07-06 06:01:23 -0300249 if (d->code_length < 1 || d->code_length > (BUFLEN * 8)) {
Andi Shyti3fac0312016-07-06 06:01:16 -0300250 dev_err(d->dev, "code length must be less than %d bits\n",
251 BUFLEN * 8);
Andi Shyti54fceca2016-07-06 06:01:17 -0300252 return -EBADRQC;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300253 }
254
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300255 if (d->sample_rate) {
256 if (2 > d->sample_rate || HZ < d->sample_rate) {
Andi Shyti3fac0312016-07-06 06:01:16 -0300257 dev_err(d->dev, "invalid %d sample rate\n",
258 d->sample_rate);
Andi Shyti54fceca2016-07-06 06:01:17 -0300259 return -EBADRQC;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300260 }
261 if (!d->add_to_buf) {
Andi Shyti3fac0312016-07-06 06:01:16 -0300262 dev_err(d->dev, "add_to_buf not set\n");
Andi Shyti54fceca2016-07-06 06:01:17 -0300263 return -EBADRQC;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300264 }
Andi Shyti14db9fc2016-07-06 06:01:21 -0300265 } else if (!d->rbuf && !(d->fops && d->fops->read &&
266 d->fops->poll && d->fops->unlocked_ioctl)) {
267 dev_err(d->dev, "undefined read, poll, ioctl\n");
Andi Shyti54fceca2016-07-06 06:01:17 -0300268 return -EBADRQC;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300269 }
270
271 mutex_lock(&lirc_dev_lock);
272
273 minor = d->minor;
274
275 if (minor < 0) {
276 /* find first free slot for driver */
277 for (minor = 0; minor < MAX_IRCTL_DEVICES; minor++)
278 if (!irctls[minor])
279 break;
Andi Shyti9675ee52016-07-06 06:01:23 -0300280 if (minor == MAX_IRCTL_DEVICES) {
Andi Shyti3fac0312016-07-06 06:01:16 -0300281 dev_err(d->dev, "no free slots for drivers!\n");
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300282 err = -ENOMEM;
283 goto out_lock;
284 }
285 } else if (irctls[minor]) {
Andi Shyti3fac0312016-07-06 06:01:16 -0300286 dev_err(d->dev, "minor (%d) just registered!\n", minor);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300287 err = -EBUSY;
288 goto out_lock;
289 }
290
291 ir = kzalloc(sizeof(struct irctl), GFP_KERNEL);
292 if (!ir) {
293 err = -ENOMEM;
294 goto out_lock;
295 }
Jarod Wilson578fcb82010-10-16 21:29:50 -0300296 lirc_irctl_init(ir);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300297 irctls[minor] = ir;
298 d->minor = minor;
299
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300300 /* some safety check 8-) */
301 d->name[sizeof(d->name)-1] = '\0';
302
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300303 if (d->features == 0)
304 d->features = LIRC_CAN_REC_LIRCCODE;
305
306 ir->d = *d;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300307
Sean Young74c839b2017-01-30 13:49:58 -0200308 ir->dev.devt = MKDEV(MAJOR(lirc_base_dev), ir->d.minor);
309 ir->dev.class = lirc_class;
310 ir->dev.parent = d->dev;
311 ir->dev.release = lirc_release;
312 dev_set_name(&ir->dev, "lirc%d", ir->d.minor);
313 device_initialize(&ir->dev);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300314
315 if (d->sample_rate) {
Andi Shyti6ab86d22016-07-06 06:01:20 -0300316 ir->jiffies_to_wait = HZ / d->sample_rate;
317
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300318 /* try to fire up polling thread */
319 ir->task = kthread_run(lirc_thread, (void *)ir, "lirc_dev");
320 if (IS_ERR(ir->task)) {
Andi Shyti3fac0312016-07-06 06:01:16 -0300321 dev_err(d->dev, "cannot run thread for minor = %d\n",
322 d->minor);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300323 err = -ECHILD;
324 goto out_sysfs;
325 }
Andi Shyti6ab86d22016-07-06 06:01:20 -0300326 } else {
327 /* it means - wait for external event in task queue */
328 ir->jiffies_to_wait = 0;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300329 }
330
331 err = lirc_cdev_add(ir);
332 if (err)
333 goto out_sysfs;
334
335 ir->attached = 1;
Sean Young74c839b2017-01-30 13:49:58 -0200336
337 err = device_add(&ir->dev);
338 if (err)
339 goto out_cdev;
340
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300341 mutex_unlock(&lirc_dev_lock);
342
Sean Young74c839b2017-01-30 13:49:58 -0200343 get_device(ir->dev.parent);
344
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300345 dev_info(ir->d.dev, "lirc_dev: driver %s registered at minor = %d\n",
346 ir->d.name, ir->d.minor);
347 return minor;
Sean Young74c839b2017-01-30 13:49:58 -0200348out_cdev:
349 cdev_del(&ir->cdev);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300350out_sysfs:
Sean Young74c839b2017-01-30 13:49:58 -0200351 put_device(&ir->dev);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300352out_lock:
353 mutex_unlock(&lirc_dev_lock);
Andi Shyti54fceca2016-07-06 06:01:17 -0300354
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300355 return err;
356}
Andi Shyti70143982016-07-06 06:01:14 -0300357
358int lirc_register_driver(struct lirc_driver *d)
359{
360 int minor, err = 0;
361
362 minor = lirc_allocate_driver(d);
363 if (minor < 0)
364 return minor;
365
366 if (LIRC_CAN_REC(d->features)) {
367 err = lirc_allocate_buffer(irctls[minor]);
368 if (err)
369 lirc_unregister_driver(minor);
David Härdeman0f7c4062017-05-01 10:32:34 -0300370 else
371 /*
372 * This is kind of a hack but ir-lirc-codec needs
373 * access to the buffer that lirc_dev allocated.
374 */
375 d->rbuf = irctls[minor]->buf;
Andi Shyti70143982016-07-06 06:01:14 -0300376 }
377
378 return err ? err : minor;
379}
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300380EXPORT_SYMBOL(lirc_register_driver);
381
382int lirc_unregister_driver(int minor)
383{
384 struct irctl *ir;
385
386 if (minor < 0 || minor >= MAX_IRCTL_DEVICES) {
Andi Shyti3fac0312016-07-06 06:01:16 -0300387 pr_err("minor (%d) must be between 0 and %d!\n",
388 minor, MAX_IRCTL_DEVICES - 1);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300389 return -EBADRQC;
390 }
391
392 ir = irctls[minor];
Jarod Wilsondf1868e2010-09-17 18:12:31 -0300393 if (!ir) {
Andi Shyti3fac0312016-07-06 06:01:16 -0300394 pr_err("failed to get irctl\n");
Jarod Wilsondf1868e2010-09-17 18:12:31 -0300395 return -ENOENT;
396 }
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300397
398 mutex_lock(&lirc_dev_lock);
399
400 if (ir->d.minor != minor) {
Andi Shyti3fac0312016-07-06 06:01:16 -0300401 dev_err(ir->d.dev, "lirc_dev: minor %d device not registered\n",
402 minor);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300403 mutex_unlock(&lirc_dev_lock);
404 return -ENOENT;
405 }
406
407 /* end up polling thread */
408 if (ir->task)
409 kthread_stop(ir->task);
410
411 dev_dbg(ir->d.dev, "lirc_dev: driver %s unregistered from minor = %d\n",
412 ir->d.name, ir->d.minor);
413
414 ir->attached = 0;
415 if (ir->open) {
416 dev_dbg(ir->d.dev, LOGHEAD "releasing opened driver\n",
417 ir->d.name, ir->d.minor);
418 wake_up_interruptible(&ir->buf->wait_poll);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300419 }
420
Sean Young74c839b2017-01-30 13:49:58 -0200421 mutex_lock(&ir->irctl_lock);
422
423 if (ir->d.set_use_dec)
424 ir->d.set_use_dec(ir->d.data);
425
426 mutex_unlock(&ir->irctl_lock);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300427 mutex_unlock(&lirc_dev_lock);
428
Sean Young74c839b2017-01-30 13:49:58 -0200429 device_del(&ir->dev);
430 cdev_del(&ir->cdev);
431 put_device(&ir->dev);
432
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300433 return 0;
434}
435EXPORT_SYMBOL(lirc_unregister_driver);
436
437int lirc_dev_fop_open(struct inode *inode, struct file *file)
438{
439 struct irctl *ir;
440 int retval = 0;
441
442 if (iminor(inode) >= MAX_IRCTL_DEVICES) {
Andi Shyti3fac0312016-07-06 06:01:16 -0300443 pr_err("open result for %d is -ENODEV\n", iminor(inode));
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300444 return -ENODEV;
445 }
446
447 if (mutex_lock_interruptible(&lirc_dev_lock))
448 return -ERESTARTSYS;
449
450 ir = irctls[iminor(inode)];
Sean Youngdb5b15b2017-02-13 10:35:44 -0200451 mutex_unlock(&lirc_dev_lock);
452
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300453 if (!ir) {
454 retval = -ENODEV;
455 goto error;
456 }
457
458 dev_dbg(ir->d.dev, LOGHEAD "open called\n", ir->d.name, ir->d.minor);
459
460 if (ir->d.minor == NOPLUG) {
461 retval = -ENODEV;
462 goto error;
463 }
464
465 if (ir->open) {
466 retval = -EBUSY;
467 goto error;
468 }
469
Srinivas Kandagatlaca7a7222013-07-22 04:23:07 -0300470 if (ir->d.rdev) {
471 retval = rc_open(ir->d.rdev);
472 if (retval)
473 goto error;
474 }
475
Sean Young74c839b2017-01-30 13:49:58 -0200476 ir->open++;
477 if (ir->d.set_use_inc)
478 retval = ir->d.set_use_inc(ir->d.data);
479 if (retval) {
480 ir->open--;
481 } else {
482 if (ir->buf)
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300483 lirc_buffer_clear(ir->buf);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300484 if (ir->task)
485 wake_up_process(ir->task);
486 }
487
488error:
Arnd Bergmannd9d2e9d2010-08-15 18:51:56 +0200489 nonseekable_open(inode, file);
490
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300491 return retval;
492}
493EXPORT_SYMBOL(lirc_dev_fop_open);
494
495int lirc_dev_fop_close(struct inode *inode, struct file *file)
496{
497 struct irctl *ir = irctls[iminor(inode)];
Mauro Carvalho Chehabb64e10f2016-02-27 07:51:13 -0300498 int ret;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300499
Jarod Wilson715d29a72010-10-18 12:02:01 -0300500 if (!ir) {
Andi Shyti3fac0312016-07-06 06:01:16 -0300501 pr_err("called with invalid irctl\n");
Jarod Wilson715d29a72010-10-18 12:02:01 -0300502 return -EINVAL;
503 }
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300504
Mauro Carvalho Chehabb64e10f2016-02-27 07:51:13 -0300505 ret = mutex_lock_killable(&lirc_dev_lock);
506 WARN_ON(ret);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300507
Markus Elfring3dd94f02014-11-20 09:01:32 -0300508 rc_close(ir->d.rdev);
Srinivas Kandagatlaca7a7222013-07-22 04:23:07 -0300509
Jarod Wilson715d29a72010-10-18 12:02:01 -0300510 ir->open--;
Sean Young74c839b2017-01-30 13:49:58 -0200511 if (ir->d.set_use_dec)
512 ir->d.set_use_dec(ir->d.data);
Mauro Carvalho Chehabb64e10f2016-02-27 07:51:13 -0300513 if (!ret)
514 mutex_unlock(&lirc_dev_lock);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300515
516 return 0;
517}
518EXPORT_SYMBOL(lirc_dev_fop_close);
519
520unsigned int lirc_dev_fop_poll(struct file *file, poll_table *wait)
521{
Al Viro496ad9a2013-01-23 17:07:38 -0500522 struct irctl *ir = irctls[iminor(file_inode(file))];
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300523 unsigned int ret;
524
Jarod Wilson715d29a72010-10-18 12:02:01 -0300525 if (!ir) {
Andi Shyti3fac0312016-07-06 06:01:16 -0300526 pr_err("called with invalid irctl\n");
Jarod Wilson715d29a72010-10-18 12:02:01 -0300527 return POLLERR;
528 }
529
Dan Carpenter5c769a62010-11-17 02:12:23 -0300530 if (!ir->attached)
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300531 return POLLERR;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300532
Andy Shevchenko3656cdd2015-01-06 22:53:37 -0300533 if (ir->buf) {
534 poll_wait(file, &ir->buf->wait_poll, wait);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300535
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300536 if (lirc_buffer_empty(ir->buf))
537 ret = 0;
538 else
539 ret = POLLIN | POLLRDNORM;
Andy Shevchenko3656cdd2015-01-06 22:53:37 -0300540 } else
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300541 ret = POLLERR;
542
543 dev_dbg(ir->d.dev, LOGHEAD "poll result = %d\n",
544 ir->d.name, ir->d.minor, ret);
545
546 return ret;
547}
548EXPORT_SYMBOL(lirc_dev_fop_poll);
549
Arnd Bergmann044e5872010-08-02 15:43:35 -0300550long lirc_dev_fop_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300551{
Jarod Wilsonbe1f9852010-10-08 17:24:21 -0300552 __u32 mode;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300553 int result = 0;
Al Viro496ad9a2013-01-23 17:07:38 -0500554 struct irctl *ir = irctls[iminor(file_inode(file))];
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300555
Jarod Wilson8be292c2010-10-09 15:07:06 -0300556 if (!ir) {
Andi Shyti3fac0312016-07-06 06:01:16 -0300557 pr_err("no irctl found!\n");
Jarod Wilson8be292c2010-10-09 15:07:06 -0300558 return -ENODEV;
559 }
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300560
561 dev_dbg(ir->d.dev, LOGHEAD "ioctl called (0x%x)\n",
562 ir->d.name, ir->d.minor, cmd);
563
564 if (ir->d.minor == NOPLUG || !ir->attached) {
Andi Shyti3fac0312016-07-06 06:01:16 -0300565 dev_err(ir->d.dev, LOGHEAD "ioctl result = -ENODEV\n",
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300566 ir->d.name, ir->d.minor);
567 return -ENODEV;
568 }
569
570 mutex_lock(&ir->irctl_lock);
571
572 switch (cmd) {
573 case LIRC_GET_FEATURES:
Hans Verkuil60519af2014-08-20 19:41:03 -0300574 result = put_user(ir->d.features, (__u32 __user *)arg);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300575 break;
576 case LIRC_GET_REC_MODE:
Sean Youngbd291202016-12-02 15:16:08 -0200577 if (!LIRC_CAN_REC(ir->d.features)) {
Andi Shytib4088092016-07-06 06:01:24 -0300578 result = -ENOTTY;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300579 break;
580 }
581
582 result = put_user(LIRC_REC2MODE
583 (ir->d.features & LIRC_CAN_REC_MASK),
Hans Verkuil60519af2014-08-20 19:41:03 -0300584 (__u32 __user *)arg);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300585 break;
586 case LIRC_SET_REC_MODE:
Sean Youngbd291202016-12-02 15:16:08 -0200587 if (!LIRC_CAN_REC(ir->d.features)) {
Andi Shytib4088092016-07-06 06:01:24 -0300588 result = -ENOTTY;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300589 break;
590 }
591
Hans Verkuil60519af2014-08-20 19:41:03 -0300592 result = get_user(mode, (__u32 __user *)arg);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300593 if (!result && !(LIRC_MODE2REC(mode) & ir->d.features))
594 result = -EINVAL;
595 /*
596 * FIXME: We should actually set the mode somehow but
597 * for now, lirc_serial doesn't support mode changing either
598 */
599 break;
600 case LIRC_GET_LENGTH:
Hans Verkuil60519af2014-08-20 19:41:03 -0300601 result = put_user(ir->d.code_length, (__u32 __user *)arg);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300602 break;
603 case LIRC_GET_MIN_TIMEOUT:
604 if (!(ir->d.features & LIRC_CAN_SET_REC_TIMEOUT) ||
605 ir->d.min_timeout == 0) {
Andi Shytib4088092016-07-06 06:01:24 -0300606 result = -ENOTTY;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300607 break;
608 }
609
Hans Verkuil60519af2014-08-20 19:41:03 -0300610 result = put_user(ir->d.min_timeout, (__u32 __user *)arg);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300611 break;
612 case LIRC_GET_MAX_TIMEOUT:
613 if (!(ir->d.features & LIRC_CAN_SET_REC_TIMEOUT) ||
614 ir->d.max_timeout == 0) {
Andi Shytib4088092016-07-06 06:01:24 -0300615 result = -ENOTTY;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300616 break;
617 }
618
Hans Verkuil60519af2014-08-20 19:41:03 -0300619 result = put_user(ir->d.max_timeout, (__u32 __user *)arg);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300620 break;
621 default:
Sean Young5c8627582017-01-26 15:19:33 -0200622 result = -ENOTTY;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300623 }
624
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300625 mutex_unlock(&ir->irctl_lock);
626
627 return result;
628}
629EXPORT_SYMBOL(lirc_dev_fop_ioctl);
630
631ssize_t lirc_dev_fop_read(struct file *file,
Dan Carpenter0e835082010-11-17 02:13:39 -0300632 char __user *buffer,
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300633 size_t length,
634 loff_t *ppos)
635{
Al Viro496ad9a2013-01-23 17:07:38 -0500636 struct irctl *ir = irctls[iminor(file_inode(file))];
Jarod Wilson715d29a72010-10-18 12:02:01 -0300637 unsigned char *buf;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300638 int ret = 0, written = 0;
639 DECLARE_WAITQUEUE(wait, current);
640
Jarod Wilson715d29a72010-10-18 12:02:01 -0300641 if (!ir) {
Andi Shyti3fac0312016-07-06 06:01:16 -0300642 pr_err("called with invalid irctl\n");
Jarod Wilson715d29a72010-10-18 12:02:01 -0300643 return -ENODEV;
644 }
645
Sean Young32002f72017-02-01 20:08:56 -0200646 if (!LIRC_CAN_REC(ir->d.features))
647 return -EINVAL;
648
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300649 dev_dbg(ir->d.dev, LOGHEAD "read called\n", ir->d.name, ir->d.minor);
650
Jarod Wilson715d29a72010-10-18 12:02:01 -0300651 buf = kzalloc(ir->chunk_size, GFP_KERNEL);
652 if (!buf)
653 return -ENOMEM;
654
Dan Carpenter250f7a52010-11-17 02:20:15 -0300655 if (mutex_lock_interruptible(&ir->irctl_lock)) {
656 ret = -ERESTARTSYS;
657 goto out_unlocked;
658 }
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300659 if (!ir->attached) {
Dan Carpenter250f7a52010-11-17 02:20:15 -0300660 ret = -ENODEV;
661 goto out_locked;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300662 }
663
664 if (length % ir->chunk_size) {
Dan Carpenter250f7a52010-11-17 02:20:15 -0300665 ret = -EINVAL;
666 goto out_locked;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300667 }
668
669 /*
670 * we add ourselves to the task queue before buffer check
671 * to avoid losing scan code (in case when queue is awaken somewhere
672 * between while condition checking and scheduling)
673 */
674 add_wait_queue(&ir->buf->wait_poll, &wait);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300675
676 /*
677 * while we didn't provide 'length' bytes, device is opened in blocking
678 * mode and 'copy_to_user' is happy, wait for data.
679 */
680 while (written < length && ret == 0) {
681 if (lirc_buffer_empty(ir->buf)) {
682 /* According to the read(2) man page, 'written' can be
683 * returned as less than 'length', instead of blocking
684 * again, returning -EWOULDBLOCK, or returning
Andi Shyti62e92682016-07-06 06:01:25 -0300685 * -ERESTARTSYS
686 */
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300687 if (written)
688 break;
689 if (file->f_flags & O_NONBLOCK) {
690 ret = -EWOULDBLOCK;
691 break;
692 }
693 if (signal_pending(current)) {
694 ret = -ERESTARTSYS;
695 break;
696 }
697
698 mutex_unlock(&ir->irctl_lock);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300699 set_current_state(TASK_INTERRUPTIBLE);
Sean Young12accdc2016-10-31 15:52:25 -0200700 schedule();
701 set_current_state(TASK_RUNNING);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300702
703 if (mutex_lock_interruptible(&ir->irctl_lock)) {
704 ret = -ERESTARTSYS;
Jarod Wilson69c271f2010-07-07 11:29:44 -0300705 remove_wait_queue(&ir->buf->wait_poll, &wait);
Jarod Wilson69c271f2010-07-07 11:29:44 -0300706 goto out_unlocked;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300707 }
708
709 if (!ir->attached) {
710 ret = -ENODEV;
Sean Youngc77d17c02016-10-31 15:52:27 -0200711 goto out_locked;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300712 }
713 } else {
714 lirc_buffer_read(ir->buf, buf);
Hans Verkuil60519af2014-08-20 19:41:03 -0300715 ret = copy_to_user((void __user *)buffer+written, buf,
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300716 ir->buf->chunk_size);
Dan Carpenter250f7a52010-11-17 02:20:15 -0300717 if (!ret)
718 written += ir->buf->chunk_size;
719 else
720 ret = -EFAULT;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300721 }
722 }
723
724 remove_wait_queue(&ir->buf->wait_poll, &wait);
Dan Carpenter250f7a52010-11-17 02:20:15 -0300725
726out_locked:
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300727 mutex_unlock(&ir->irctl_lock);
728
Jarod Wilson69c271f2010-07-07 11:29:44 -0300729out_unlocked:
Jarod Wilson715d29a72010-10-18 12:02:01 -0300730 kfree(buf);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300731
732 return ret ? ret : written;
733}
734EXPORT_SYMBOL(lirc_dev_fop_read);
735
736void *lirc_get_pdata(struct file *file)
737{
Al Viro0990a972013-01-24 19:00:58 -0500738 return irctls[iminor(file_inode(file))]->d.data;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300739}
740EXPORT_SYMBOL(lirc_get_pdata);
741
742
Dan Carpenter0e835082010-11-17 02:13:39 -0300743ssize_t lirc_dev_fop_write(struct file *file, const char __user *buffer,
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300744 size_t length, loff_t *ppos)
745{
Al Viro496ad9a2013-01-23 17:07:38 -0500746 struct irctl *ir = irctls[iminor(file_inode(file))];
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300747
Jarod Wilson715d29a72010-10-18 12:02:01 -0300748 if (!ir) {
Andi Shyti3fac0312016-07-06 06:01:16 -0300749 pr_err("called with invalid irctl\n");
Jarod Wilson715d29a72010-10-18 12:02:01 -0300750 return -ENODEV;
751 }
752
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300753 if (!ir->attached)
754 return -ENODEV;
755
756 return -EINVAL;
757}
758EXPORT_SYMBOL(lirc_dev_fop_write);
759
760
761static int __init lirc_dev_init(void)
762{
763 int retval;
764
765 lirc_class = class_create(THIS_MODULE, "lirc");
766 if (IS_ERR(lirc_class)) {
Andi Shyti3fac0312016-07-06 06:01:16 -0300767 pr_err("class_create failed\n");
Andi Shyti54fceca2016-07-06 06:01:17 -0300768 return PTR_ERR(lirc_class);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300769 }
770
771 retval = alloc_chrdev_region(&lirc_base_dev, 0, MAX_IRCTL_DEVICES,
772 IRCTL_DEV_NAME);
773 if (retval) {
774 class_destroy(lirc_class);
Andi Shyti3fac0312016-07-06 06:01:16 -0300775 pr_err("alloc_chrdev_region failed\n");
Andi Shyti54fceca2016-07-06 06:01:17 -0300776 return retval;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300777 }
778
Andi Shyti3fac0312016-07-06 06:01:16 -0300779 pr_info("IR Remote Control driver registered, major %d\n",
780 MAJOR(lirc_base_dev));
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300781
Andi Shyti54fceca2016-07-06 06:01:17 -0300782 return 0;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300783}
784
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300785static void __exit lirc_dev_exit(void)
786{
787 class_destroy(lirc_class);
788 unregister_chrdev_region(lirc_base_dev, MAX_IRCTL_DEVICES);
Andi Shyti3fac0312016-07-06 06:01:16 -0300789 pr_info("module unloaded\n");
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300790}
791
792module_init(lirc_dev_init);
793module_exit(lirc_dev_exit);
794
795MODULE_DESCRIPTION("LIRC base driver module");
796MODULE_AUTHOR("Artur Lipowski");
797MODULE_LICENSE("GPL");
798
799module_param(debug, bool, S_IRUGO | S_IWUSR);
800MODULE_PARM_DESC(debug, "Enable debugging messages");