blob: 2bdd2c0a9268854f4f00babe12842cb6670ba6c3 [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 Wilson715d29a72010-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 Wilson715d29a72010-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);
414 ir->d.set_use_dec(ir->d.data);
Jarod Wilsonc1cbb702010-10-18 18:39:20 -0300415 module_put(cdev->owner);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300416 mutex_unlock(&ir->irctl_lock);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300417 } else {
Jarod Wilson578fcb82010-10-16 21:29:50 -0300418 lirc_irctl_cleanup(ir);
Jarod Wilsonc1cbb702010-10-18 18:39:20 -0300419 cdev_del(cdev);
Jarod Wilson8de111e22011-05-27 16:56:50 -0300420 kfree(cdev);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300421 kfree(ir);
422 irctls[minor] = NULL;
423 }
424
425 mutex_unlock(&lirc_dev_lock);
426
427 return 0;
428}
429EXPORT_SYMBOL(lirc_unregister_driver);
430
431int lirc_dev_fop_open(struct inode *inode, struct file *file)
432{
433 struct irctl *ir;
Jarod Wilsonc1cbb702010-10-18 18:39:20 -0300434 struct cdev *cdev;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300435 int retval = 0;
436
437 if (iminor(inode) >= MAX_IRCTL_DEVICES) {
Andi Shyti3fac0312016-07-06 06:01:16 -0300438 pr_err("open result for %d is -ENODEV\n", iminor(inode));
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300439 return -ENODEV;
440 }
441
442 if (mutex_lock_interruptible(&lirc_dev_lock))
443 return -ERESTARTSYS;
444
445 ir = irctls[iminor(inode)];
446 if (!ir) {
447 retval = -ENODEV;
448 goto error;
449 }
450
451 dev_dbg(ir->d.dev, LOGHEAD "open called\n", ir->d.name, ir->d.minor);
452
453 if (ir->d.minor == NOPLUG) {
454 retval = -ENODEV;
455 goto error;
456 }
457
458 if (ir->open) {
459 retval = -EBUSY;
460 goto error;
461 }
462
Srinivas Kandagatlaca7a7222013-07-22 04:23:07 -0300463 if (ir->d.rdev) {
464 retval = rc_open(ir->d.rdev);
465 if (retval)
466 goto error;
467 }
468
Jarod Wilson8de111e22011-05-27 16:56:50 -0300469 cdev = ir->cdev;
Jarod Wilsonc1cbb702010-10-18 18:39:20 -0300470 if (try_module_get(cdev->owner)) {
471 ir->open++;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300472 retval = ir->d.set_use_inc(ir->d.data);
473
474 if (retval) {
Jarod Wilsonc1cbb702010-10-18 18:39:20 -0300475 module_put(cdev->owner);
476 ir->open--;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300477 } else {
478 lirc_buffer_clear(ir->buf);
479 }
480 if (ir->task)
481 wake_up_process(ir->task);
482 }
483
484error:
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300485 mutex_unlock(&lirc_dev_lock);
486
Arnd Bergmannd9d2e9d2010-08-15 18:51:56 +0200487 nonseekable_open(inode, file);
488
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300489 return retval;
490}
491EXPORT_SYMBOL(lirc_dev_fop_open);
492
493int lirc_dev_fop_close(struct inode *inode, struct file *file)
494{
495 struct irctl *ir = irctls[iminor(inode)];
Jarod Wilson8de111e22011-05-27 16:56:50 -0300496 struct cdev *cdev;
Mauro Carvalho Chehabb64e10f2016-02-27 07:51:13 -0300497 int ret;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300498
Jarod Wilson715d29a72010-10-18 12:02:01 -0300499 if (!ir) {
Andi Shyti3fac0312016-07-06 06:01:16 -0300500 pr_err("called with invalid irctl\n");
Jarod Wilson715d29a72010-10-18 12:02:01 -0300501 return -EINVAL;
502 }
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300503
Jarod Wilson8de111e22011-05-27 16:56:50 -0300504 cdev = ir->cdev;
505
Mauro Carvalho Chehabb64e10f2016-02-27 07:51:13 -0300506 ret = mutex_lock_killable(&lirc_dev_lock);
507 WARN_ON(ret);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300508
Markus Elfring3dd94f02014-11-20 09:01:32 -0300509 rc_close(ir->d.rdev);
Srinivas Kandagatlaca7a7222013-07-22 04:23:07 -0300510
Jarod Wilson715d29a72010-10-18 12:02:01 -0300511 ir->open--;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300512 if (ir->attached) {
513 ir->d.set_use_dec(ir->d.data);
Jarod Wilsonc1cbb702010-10-18 18:39:20 -0300514 module_put(cdev->owner);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300515 } else {
Jarod Wilson578fcb82010-10-16 21:29:50 -0300516 lirc_irctl_cleanup(ir);
Jarod Wilsonc1cbb702010-10-18 18:39:20 -0300517 cdev_del(cdev);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300518 irctls[ir->d.minor] = NULL;
Jarod Wilson8de111e22011-05-27 16:56:50 -0300519 kfree(cdev);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300520 kfree(ir);
521 }
522
Mauro Carvalho Chehabb64e10f2016-02-27 07:51:13 -0300523 if (!ret)
524 mutex_unlock(&lirc_dev_lock);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300525
526 return 0;
527}
528EXPORT_SYMBOL(lirc_dev_fop_close);
529
530unsigned int lirc_dev_fop_poll(struct file *file, poll_table *wait)
531{
Al Viro496ad9a2013-01-23 17:07:38 -0500532 struct irctl *ir = irctls[iminor(file_inode(file))];
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300533 unsigned int ret;
534
Jarod Wilson715d29a72010-10-18 12:02:01 -0300535 if (!ir) {
Andi Shyti3fac0312016-07-06 06:01:16 -0300536 pr_err("called with invalid irctl\n");
Jarod Wilson715d29a72010-10-18 12:02:01 -0300537 return POLLERR;
538 }
539
Dan Carpenter5c769a62010-11-17 02:12:23 -0300540 if (!ir->attached)
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300541 return POLLERR;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300542
Andy Shevchenko3656cdd2015-01-06 22:53:37 -0300543 if (ir->buf) {
544 poll_wait(file, &ir->buf->wait_poll, wait);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300545
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300546 if (lirc_buffer_empty(ir->buf))
547 ret = 0;
548 else
549 ret = POLLIN | POLLRDNORM;
Andy Shevchenko3656cdd2015-01-06 22:53:37 -0300550 } else
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300551 ret = POLLERR;
552
553 dev_dbg(ir->d.dev, LOGHEAD "poll result = %d\n",
554 ir->d.name, ir->d.minor, ret);
555
556 return ret;
557}
558EXPORT_SYMBOL(lirc_dev_fop_poll);
559
Arnd Bergmann044e5872010-08-02 15:43:35 -0300560long lirc_dev_fop_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300561{
Jarod Wilsonbe1f9852010-10-08 17:24:21 -0300562 __u32 mode;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300563 int result = 0;
Al Viro496ad9a2013-01-23 17:07:38 -0500564 struct irctl *ir = irctls[iminor(file_inode(file))];
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300565
Jarod Wilson8be292c2010-10-09 15:07:06 -0300566 if (!ir) {
Andi Shyti3fac0312016-07-06 06:01:16 -0300567 pr_err("no irctl found!\n");
Jarod Wilson8be292c2010-10-09 15:07:06 -0300568 return -ENODEV;
569 }
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300570
571 dev_dbg(ir->d.dev, LOGHEAD "ioctl called (0x%x)\n",
572 ir->d.name, ir->d.minor, cmd);
573
574 if (ir->d.minor == NOPLUG || !ir->attached) {
Andi Shyti3fac0312016-07-06 06:01:16 -0300575 dev_err(ir->d.dev, LOGHEAD "ioctl result = -ENODEV\n",
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300576 ir->d.name, ir->d.minor);
577 return -ENODEV;
578 }
579
580 mutex_lock(&ir->irctl_lock);
581
582 switch (cmd) {
583 case LIRC_GET_FEATURES:
Hans Verkuil60519af2014-08-20 19:41:03 -0300584 result = put_user(ir->d.features, (__u32 __user *)arg);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300585 break;
586 case LIRC_GET_REC_MODE:
587 if (!(ir->d.features & LIRC_CAN_REC_MASK)) {
588 result = -ENOSYS;
589 break;
590 }
591
592 result = put_user(LIRC_REC2MODE
593 (ir->d.features & LIRC_CAN_REC_MASK),
Hans Verkuil60519af2014-08-20 19:41:03 -0300594 (__u32 __user *)arg);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300595 break;
596 case LIRC_SET_REC_MODE:
597 if (!(ir->d.features & LIRC_CAN_REC_MASK)) {
598 result = -ENOSYS;
599 break;
600 }
601
Hans Verkuil60519af2014-08-20 19:41:03 -0300602 result = get_user(mode, (__u32 __user *)arg);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300603 if (!result && !(LIRC_MODE2REC(mode) & ir->d.features))
604 result = -EINVAL;
605 /*
606 * FIXME: We should actually set the mode somehow but
607 * for now, lirc_serial doesn't support mode changing either
608 */
609 break;
610 case LIRC_GET_LENGTH:
Hans Verkuil60519af2014-08-20 19:41:03 -0300611 result = put_user(ir->d.code_length, (__u32 __user *)arg);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300612 break;
613 case LIRC_GET_MIN_TIMEOUT:
614 if (!(ir->d.features & LIRC_CAN_SET_REC_TIMEOUT) ||
615 ir->d.min_timeout == 0) {
616 result = -ENOSYS;
617 break;
618 }
619
Hans Verkuil60519af2014-08-20 19:41:03 -0300620 result = put_user(ir->d.min_timeout, (__u32 __user *)arg);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300621 break;
622 case LIRC_GET_MAX_TIMEOUT:
623 if (!(ir->d.features & LIRC_CAN_SET_REC_TIMEOUT) ||
624 ir->d.max_timeout == 0) {
625 result = -ENOSYS;
626 break;
627 }
628
Hans Verkuil60519af2014-08-20 19:41:03 -0300629 result = put_user(ir->d.max_timeout, (__u32 __user *)arg);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300630 break;
631 default:
632 result = -EINVAL;
633 }
634
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300635 mutex_unlock(&ir->irctl_lock);
636
637 return result;
638}
639EXPORT_SYMBOL(lirc_dev_fop_ioctl);
640
641ssize_t lirc_dev_fop_read(struct file *file,
Dan Carpenter0e835082010-11-17 02:13:39 -0300642 char __user *buffer,
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300643 size_t length,
644 loff_t *ppos)
645{
Al Viro496ad9a2013-01-23 17:07:38 -0500646 struct irctl *ir = irctls[iminor(file_inode(file))];
Jarod Wilson715d29a72010-10-18 12:02:01 -0300647 unsigned char *buf;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300648 int ret = 0, written = 0;
649 DECLARE_WAITQUEUE(wait, current);
650
Jarod Wilson715d29a72010-10-18 12:02:01 -0300651 if (!ir) {
Andi Shyti3fac0312016-07-06 06:01:16 -0300652 pr_err("called with invalid irctl\n");
Jarod Wilson715d29a72010-10-18 12:02:01 -0300653 return -ENODEV;
654 }
655
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300656 dev_dbg(ir->d.dev, LOGHEAD "read called\n", ir->d.name, ir->d.minor);
657
Jarod Wilson715d29a72010-10-18 12:02:01 -0300658 buf = kzalloc(ir->chunk_size, GFP_KERNEL);
659 if (!buf)
660 return -ENOMEM;
661
Dan Carpenter250f7a52010-11-17 02:20:15 -0300662 if (mutex_lock_interruptible(&ir->irctl_lock)) {
663 ret = -ERESTARTSYS;
664 goto out_unlocked;
665 }
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300666 if (!ir->attached) {
Dan Carpenter250f7a52010-11-17 02:20:15 -0300667 ret = -ENODEV;
668 goto out_locked;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300669 }
670
671 if (length % ir->chunk_size) {
Dan Carpenter250f7a52010-11-17 02:20:15 -0300672 ret = -EINVAL;
673 goto out_locked;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300674 }
675
676 /*
677 * we add ourselves to the task queue before buffer check
678 * to avoid losing scan code (in case when queue is awaken somewhere
679 * between while condition checking and scheduling)
680 */
681 add_wait_queue(&ir->buf->wait_poll, &wait);
682 set_current_state(TASK_INTERRUPTIBLE);
683
684 /*
685 * while we didn't provide 'length' bytes, device is opened in blocking
686 * mode and 'copy_to_user' is happy, wait for data.
687 */
688 while (written < length && ret == 0) {
689 if (lirc_buffer_empty(ir->buf)) {
690 /* According to the read(2) man page, 'written' can be
691 * returned as less than 'length', instead of blocking
692 * again, returning -EWOULDBLOCK, or returning
693 * -ERESTARTSYS */
694 if (written)
695 break;
696 if (file->f_flags & O_NONBLOCK) {
697 ret = -EWOULDBLOCK;
698 break;
699 }
700 if (signal_pending(current)) {
701 ret = -ERESTARTSYS;
702 break;
703 }
704
705 mutex_unlock(&ir->irctl_lock);
706 schedule();
707 set_current_state(TASK_INTERRUPTIBLE);
708
709 if (mutex_lock_interruptible(&ir->irctl_lock)) {
710 ret = -ERESTARTSYS;
Jarod Wilson69c271f2010-07-07 11:29:44 -0300711 remove_wait_queue(&ir->buf->wait_poll, &wait);
712 set_current_state(TASK_RUNNING);
713 goto out_unlocked;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300714 }
715
716 if (!ir->attached) {
717 ret = -ENODEV;
718 break;
719 }
720 } else {
721 lirc_buffer_read(ir->buf, buf);
Hans Verkuil60519af2014-08-20 19:41:03 -0300722 ret = copy_to_user((void __user *)buffer+written, buf,
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300723 ir->buf->chunk_size);
Dan Carpenter250f7a52010-11-17 02:20:15 -0300724 if (!ret)
725 written += ir->buf->chunk_size;
726 else
727 ret = -EFAULT;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300728 }
729 }
730
731 remove_wait_queue(&ir->buf->wait_poll, &wait);
732 set_current_state(TASK_RUNNING);
Dan Carpenter250f7a52010-11-17 02:20:15 -0300733
734out_locked:
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300735 mutex_unlock(&ir->irctl_lock);
736
Jarod Wilson69c271f2010-07-07 11:29:44 -0300737out_unlocked:
Jarod Wilson715d29a72010-10-18 12:02:01 -0300738 kfree(buf);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300739
740 return ret ? ret : written;
741}
742EXPORT_SYMBOL(lirc_dev_fop_read);
743
744void *lirc_get_pdata(struct file *file)
745{
Al Viro0990a972013-01-24 19:00:58 -0500746 return irctls[iminor(file_inode(file))]->d.data;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300747}
748EXPORT_SYMBOL(lirc_get_pdata);
749
750
Dan Carpenter0e835082010-11-17 02:13:39 -0300751ssize_t lirc_dev_fop_write(struct file *file, const char __user *buffer,
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300752 size_t length, loff_t *ppos)
753{
Al Viro496ad9a2013-01-23 17:07:38 -0500754 struct irctl *ir = irctls[iminor(file_inode(file))];
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300755
Jarod Wilson715d29a72010-10-18 12:02:01 -0300756 if (!ir) {
Andi Shyti3fac0312016-07-06 06:01:16 -0300757 pr_err("called with invalid irctl\n");
Jarod Wilson715d29a72010-10-18 12:02:01 -0300758 return -ENODEV;
759 }
760
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300761 if (!ir->attached)
762 return -ENODEV;
763
764 return -EINVAL;
765}
766EXPORT_SYMBOL(lirc_dev_fop_write);
767
768
769static int __init lirc_dev_init(void)
770{
771 int retval;
772
773 lirc_class = class_create(THIS_MODULE, "lirc");
774 if (IS_ERR(lirc_class)) {
Andi Shyti3fac0312016-07-06 06:01:16 -0300775 pr_err("class_create failed\n");
Andi Shyti54fceca2016-07-06 06:01:17 -0300776 return PTR_ERR(lirc_class);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300777 }
778
779 retval = alloc_chrdev_region(&lirc_base_dev, 0, MAX_IRCTL_DEVICES,
780 IRCTL_DEV_NAME);
781 if (retval) {
782 class_destroy(lirc_class);
Andi Shyti3fac0312016-07-06 06:01:16 -0300783 pr_err("alloc_chrdev_region failed\n");
Andi Shyti54fceca2016-07-06 06:01:17 -0300784 return retval;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300785 }
786
787
Andi Shyti3fac0312016-07-06 06:01:16 -0300788 pr_info("IR Remote Control driver registered, major %d\n",
789 MAJOR(lirc_base_dev));
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300790
Andi Shyti54fceca2016-07-06 06:01:17 -0300791 return 0;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300792}
793
794
795
796static void __exit lirc_dev_exit(void)
797{
798 class_destroy(lirc_class);
799 unregister_chrdev_region(lirc_base_dev, MAX_IRCTL_DEVICES);
Andi Shyti3fac0312016-07-06 06:01:16 -0300800 pr_info("module unloaded\n");
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300801}
802
803module_init(lirc_dev_init);
804module_exit(lirc_dev_exit);
805
806MODULE_DESCRIPTION("LIRC base driver module");
807MODULE_AUTHOR("Artur Lipowski");
808MODULE_LICENSE("GPL");
809
810module_param(debug, bool, S_IRUGO | S_IWUSR);
811MODULE_PARM_DESC(debug, "Enable debugging messages");