blob: ca12d3289bfe90a886da3e25ef5c40c9a2a6452f [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
22#include <linux/module.h>
23#include <linux/kernel.h>
24#include <linux/sched.h>
25#include <linux/errno.h>
26#include <linux/ioctl.h>
27#include <linux/fs.h>
28#include <linux/poll.h>
29#include <linux/completion.h>
Jarod Wilson4a62a5a2010-07-03 01:06:57 -030030#include <linux/mutex.h>
31#include <linux/wait.h>
32#include <linux/unistd.h>
33#include <linux/kthread.h>
34#include <linux/bitops.h>
35#include <linux/device.h>
36#include <linux/cdev.h>
37
38#include <media/lirc.h>
Jarod Wilson56900852010-07-16 14:25:33 -030039#include <media/lirc_dev.h>
Jarod Wilson4a62a5a2010-07-03 01:06:57 -030040
Rusty Russell90ab5ee2012-01-13 09:32:20 +103041static bool debug;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -030042
43#define IRCTL_DEV_NAME "BaseRemoteCtl"
44#define NOPLUG -1
45#define LOGHEAD "lirc_dev (%s[%d]): "
46
47static dev_t lirc_base_dev;
48
49struct irctl {
50 struct lirc_driver d;
51 int attached;
52 int open;
53
54 struct mutex irctl_lock;
55 struct lirc_buffer *buf;
56 unsigned int chunk_size;
57
Jarod Wilson8de111e2011-05-27 16:56:50 -030058 struct cdev *cdev;
59
Jarod Wilson4a62a5a2010-07-03 01:06:57 -030060 struct task_struct *task;
61 long jiffies_to_wait;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -030062};
63
64static DEFINE_MUTEX(lirc_dev_lock);
65
66static struct irctl *irctls[MAX_IRCTL_DEVICES];
67
68/* Only used for sysfs but defined to void otherwise */
69static struct class *lirc_class;
70
71/* helper function
72 * initializes the irctl structure
73 */
Jarod Wilson578fcb82010-10-16 21:29:50 -030074static void lirc_irctl_init(struct irctl *ir)
Jarod Wilson4a62a5a2010-07-03 01:06:57 -030075{
Jarod Wilson4a62a5a2010-07-03 01:06:57 -030076 mutex_init(&ir->irctl_lock);
77 ir->d.minor = NOPLUG;
78}
79
Jarod Wilson578fcb82010-10-16 21:29:50 -030080static void lirc_irctl_cleanup(struct irctl *ir)
Jarod Wilson4a62a5a2010-07-03 01:06:57 -030081{
82 dev_dbg(ir->d.dev, LOGHEAD "cleaning up\n", ir->d.name, ir->d.minor);
83
84 device_destroy(lirc_class, MKDEV(MAJOR(lirc_base_dev), ir->d.minor));
85
86 if (ir->buf != ir->d.rbuf) {
87 lirc_buffer_free(ir->buf);
88 kfree(ir->buf);
89 }
90 ir->buf = NULL;
91}
92
93/* helper function
94 * reads key codes from driver and puts them into buffer
95 * returns 0 on success
96 */
Jarod Wilson578fcb82010-10-16 21:29:50 -030097static int lirc_add_to_buf(struct irctl *ir)
Jarod Wilson4a62a5a2010-07-03 01:06:57 -030098{
99 if (ir->d.add_to_buf) {
100 int res = -ENODATA;
101 int got_data = 0;
102
103 /*
104 * service the device as long as it is returning
105 * data and we have space
106 */
107get_data:
108 res = ir->d.add_to_buf(ir->d.data, ir->buf);
109 if (res == 0) {
110 got_data++;
111 goto get_data;
112 }
113
114 if (res == -ENODEV)
115 kthread_stop(ir->task);
116
117 return got_data ? 0 : res;
118 }
119
120 return 0;
121}
122
123/* main function of the polling thread
124 */
125static int lirc_thread(void *irctl)
126{
127 struct irctl *ir = irctl;
128
129 dev_dbg(ir->d.dev, LOGHEAD "poll thread started\n",
130 ir->d.name, ir->d.minor);
131
132 do {
133 if (ir->open) {
134 if (ir->jiffies_to_wait) {
135 set_current_state(TASK_INTERRUPTIBLE);
136 schedule_timeout(ir->jiffies_to_wait);
137 }
138 if (kthread_should_stop())
139 break;
Jarod Wilson578fcb82010-10-16 21:29:50 -0300140 if (!lirc_add_to_buf(ir))
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300141 wake_up_interruptible(&ir->buf->wait_poll);
142 } else {
143 set_current_state(TASK_INTERRUPTIBLE);
144 schedule();
145 }
146 } while (!kthread_should_stop());
147
148 dev_dbg(ir->d.dev, LOGHEAD "poll thread ended\n",
149 ir->d.name, ir->d.minor);
150
151 return 0;
152}
153
154
Jarod Wilson578fcb82010-10-16 21:29:50 -0300155static struct file_operations lirc_dev_fops = {
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300156 .owner = THIS_MODULE,
157 .read = lirc_dev_fop_read,
158 .write = lirc_dev_fop_write,
159 .poll = lirc_dev_fop_poll,
Arnd Bergmann044e5872010-08-02 15:43:35 -0300160 .unlocked_ioctl = lirc_dev_fop_ioctl,
Jarod Wilson8be292c2010-10-09 15:07:06 -0300161#ifdef CONFIG_COMPAT
162 .compat_ioctl = lirc_dev_fop_ioctl,
163#endif
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300164 .open = lirc_dev_fop_open,
165 .release = lirc_dev_fop_close,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200166 .llseek = noop_llseek,
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300167};
168
169static int lirc_cdev_add(struct irctl *ir)
170{
Jarod Wilson8de111e2011-05-27 16:56:50 -0300171 int retval = -ENOMEM;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300172 struct lirc_driver *d = &ir->d;
Jarod Wilson8de111e2011-05-27 16:56:50 -0300173 struct cdev *cdev;
174
175 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
176 if (!cdev)
177 goto err_out;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300178
179 if (d->fops) {
Jarod Wilsonc1cbb702010-10-18 18:39:20 -0300180 cdev_init(cdev, d->fops);
181 cdev->owner = d->owner;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300182 } else {
Jarod Wilsonc1cbb702010-10-18 18:39:20 -0300183 cdev_init(cdev, &lirc_dev_fops);
184 cdev->owner = THIS_MODULE;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300185 }
Vasiliy Kulikovb395cba2010-11-26 14:06:41 -0300186 retval = kobject_set_name(&cdev->kobj, "lirc%d", d->minor);
187 if (retval)
Jarod Wilson8de111e2011-05-27 16:56:50 -0300188 goto err_out;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300189
Jarod Wilsonc1cbb702010-10-18 18:39:20 -0300190 retval = cdev_add(cdev, MKDEV(MAJOR(lirc_base_dev), d->minor), 1);
Jarod Wilson8de111e2011-05-27 16:56:50 -0300191 if (retval) {
Jarod Wilsonc1cbb702010-10-18 18:39:20 -0300192 kobject_put(&cdev->kobj);
Jarod Wilson8de111e2011-05-27 16:56:50 -0300193 goto err_out;
194 }
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300195
Jarod Wilson8de111e2011-05-27 16:56:50 -0300196 ir->cdev = cdev;
197
198 return 0;
199
200err_out:
201 kfree(cdev);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300202 return retval;
203}
204
205int lirc_register_driver(struct lirc_driver *d)
206{
207 struct irctl *ir;
208 int minor;
209 int bytes_in_key;
210 unsigned int chunk_size;
211 unsigned int buffer_size;
212 int err;
213
214 if (!d) {
215 printk(KERN_ERR "lirc_dev: lirc_register_driver: "
216 "driver pointer must be not NULL!\n");
217 err = -EBADRQC;
218 goto out;
219 }
220
Jarod Wilson715d29a2010-10-18 12:02:01 -0300221 if (!d->dev) {
222 printk(KERN_ERR "%s: dev pointer not filled in!\n", __func__);
223 err = -EINVAL;
224 goto out;
225 }
226
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300227 if (MAX_IRCTL_DEVICES <= d->minor) {
228 dev_err(d->dev, "lirc_dev: lirc_register_driver: "
229 "\"minor\" must be between 0 and %d (%d)!\n",
Jarod Wilson8de111e2011-05-27 16:56:50 -0300230 MAX_IRCTL_DEVICES - 1, d->minor);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300231 err = -EBADRQC;
232 goto out;
233 }
234
235 if (1 > d->code_length || (BUFLEN * 8) < d->code_length) {
236 dev_err(d->dev, "lirc_dev: lirc_register_driver: "
237 "code length in bits for minor (%d) "
238 "must be less than %d!\n",
239 d->minor, BUFLEN * 8);
240 err = -EBADRQC;
241 goto out;
242 }
243
244 dev_dbg(d->dev, "lirc_dev: lirc_register_driver: sample_rate: %d\n",
245 d->sample_rate);
246 if (d->sample_rate) {
247 if (2 > d->sample_rate || HZ < d->sample_rate) {
248 dev_err(d->dev, "lirc_dev: lirc_register_driver: "
249 "sample_rate must be between 2 and %d!\n", HZ);
250 err = -EBADRQC;
251 goto out;
252 }
253 if (!d->add_to_buf) {
254 dev_err(d->dev, "lirc_dev: lirc_register_driver: "
255 "add_to_buf cannot be NULL when "
256 "sample_rate is set\n");
257 err = -EBADRQC;
258 goto out;
259 }
260 } else if (!(d->fops && d->fops->read) && !d->rbuf) {
261 dev_err(d->dev, "lirc_dev: lirc_register_driver: "
262 "fops->read and rbuf cannot all be NULL!\n");
263 err = -EBADRQC;
264 goto out;
265 } else if (!d->rbuf) {
266 if (!(d->fops && d->fops->read && d->fops->poll &&
Arnd Bergmann044e5872010-08-02 15:43:35 -0300267 d->fops->unlocked_ioctl)) {
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300268 dev_err(d->dev, "lirc_dev: lirc_register_driver: "
Arnd Bergmann044e5872010-08-02 15:43:35 -0300269 "neither read, poll nor unlocked_ioctl can be NULL!\n");
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300270 err = -EBADRQC;
271 goto out;
272 }
273 }
274
275 mutex_lock(&lirc_dev_lock);
276
277 minor = d->minor;
278
279 if (minor < 0) {
280 /* find first free slot for driver */
281 for (minor = 0; minor < MAX_IRCTL_DEVICES; minor++)
282 if (!irctls[minor])
283 break;
284 if (MAX_IRCTL_DEVICES == minor) {
285 dev_err(d->dev, "lirc_dev: lirc_register_driver: "
286 "no free slots for drivers!\n");
287 err = -ENOMEM;
288 goto out_lock;
289 }
290 } else if (irctls[minor]) {
291 dev_err(d->dev, "lirc_dev: lirc_register_driver: "
292 "minor (%d) just registered!\n", minor);
293 err = -EBUSY;
294 goto out_lock;
295 }
296
297 ir = kzalloc(sizeof(struct irctl), GFP_KERNEL);
298 if (!ir) {
299 err = -ENOMEM;
300 goto out_lock;
301 }
Jarod Wilson578fcb82010-10-16 21:29:50 -0300302 lirc_irctl_init(ir);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300303 irctls[minor] = ir;
304 d->minor = minor;
305
306 if (d->sample_rate) {
307 ir->jiffies_to_wait = HZ / d->sample_rate;
308 } else {
309 /* it means - wait for external event in task queue */
310 ir->jiffies_to_wait = 0;
311 }
312
313 /* some safety check 8-) */
314 d->name[sizeof(d->name)-1] = '\0';
315
316 bytes_in_key = BITS_TO_LONGS(d->code_length) +
317 (d->code_length % 8 ? 1 : 0);
318 buffer_size = d->buffer_size ? d->buffer_size : BUFLEN / bytes_in_key;
319 chunk_size = d->chunk_size ? d->chunk_size : bytes_in_key;
320
321 if (d->rbuf) {
322 ir->buf = d->rbuf;
323 } else {
324 ir->buf = kmalloc(sizeof(struct lirc_buffer), GFP_KERNEL);
325 if (!ir->buf) {
326 err = -ENOMEM;
327 goto out_lock;
328 }
329 err = lirc_buffer_init(ir->buf, chunk_size, buffer_size);
330 if (err) {
331 kfree(ir->buf);
332 goto out_lock;
333 }
334 }
335 ir->chunk_size = ir->buf->chunk_size;
336
337 if (d->features == 0)
338 d->features = LIRC_CAN_REC_LIRCCODE;
339
340 ir->d = *d;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300341
342 device_create(lirc_class, ir->d.dev,
343 MKDEV(MAJOR(lirc_base_dev), ir->d.minor), NULL,
344 "lirc%u", ir->d.minor);
345
346 if (d->sample_rate) {
347 /* try to fire up polling thread */
348 ir->task = kthread_run(lirc_thread, (void *)ir, "lirc_dev");
349 if (IS_ERR(ir->task)) {
350 dev_err(d->dev, "lirc_dev: lirc_register_driver: "
351 "cannot run poll thread for minor = %d\n",
352 d->minor);
353 err = -ECHILD;
354 goto out_sysfs;
355 }
356 }
357
358 err = lirc_cdev_add(ir);
359 if (err)
360 goto out_sysfs;
361
362 ir->attached = 1;
363 mutex_unlock(&lirc_dev_lock);
364
365 dev_info(ir->d.dev, "lirc_dev: driver %s registered at minor = %d\n",
366 ir->d.name, ir->d.minor);
367 return minor;
368
369out_sysfs:
370 device_destroy(lirc_class, MKDEV(MAJOR(lirc_base_dev), ir->d.minor));
371out_lock:
372 mutex_unlock(&lirc_dev_lock);
373out:
374 return err;
375}
376EXPORT_SYMBOL(lirc_register_driver);
377
378int lirc_unregister_driver(int minor)
379{
380 struct irctl *ir;
Jarod Wilsonc1cbb702010-10-18 18:39:20 -0300381 struct cdev *cdev;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300382
383 if (minor < 0 || minor >= MAX_IRCTL_DEVICES) {
Jarod Wilson4fc21542010-10-09 15:17:03 -0300384 printk(KERN_ERR "lirc_dev: %s: minor (%d) must be between "
Jarod Wilson8de111e2011-05-27 16:56:50 -0300385 "0 and %d!\n", __func__, minor, MAX_IRCTL_DEVICES - 1);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300386 return -EBADRQC;
387 }
388
389 ir = irctls[minor];
Jarod Wilsondf1868e2010-09-17 18:12:31 -0300390 if (!ir) {
Jarod Wilson4fc21542010-10-09 15:17:03 -0300391 printk(KERN_ERR "lirc_dev: %s: failed to get irctl struct "
392 "for minor %d!\n", __func__, minor);
Jarod Wilsondf1868e2010-09-17 18:12:31 -0300393 return -ENOENT;
394 }
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300395
Jarod Wilson8de111e2011-05-27 16:56:50 -0300396 cdev = ir->cdev;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300397
398 mutex_lock(&lirc_dev_lock);
399
400 if (ir->d.minor != minor) {
Jarod Wilson4fc21542010-10-09 15:17:03 -0300401 printk(KERN_ERR "lirc_dev: %s: minor (%d) device not "
402 "registered!\n", __func__, 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);
419 mutex_lock(&ir->irctl_lock);
420 ir->d.set_use_dec(ir->d.data);
Jarod Wilsonc1cbb702010-10-18 18:39:20 -0300421 module_put(cdev->owner);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300422 mutex_unlock(&ir->irctl_lock);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300423 } else {
Jarod Wilson578fcb82010-10-16 21:29:50 -0300424 lirc_irctl_cleanup(ir);
Jarod Wilsonc1cbb702010-10-18 18:39:20 -0300425 cdev_del(cdev);
Jarod Wilson8de111e2011-05-27 16:56:50 -0300426 kfree(cdev);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300427 kfree(ir);
428 irctls[minor] = NULL;
429 }
430
431 mutex_unlock(&lirc_dev_lock);
432
433 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;
Jarod Wilsonc1cbb702010-10-18 18:39:20 -0300440 struct cdev *cdev;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300441 int retval = 0;
442
443 if (iminor(inode) >= MAX_IRCTL_DEVICES) {
444 printk(KERN_WARNING "lirc_dev [%d]: open result = -ENODEV\n",
445 iminor(inode));
446 return -ENODEV;
447 }
448
449 if (mutex_lock_interruptible(&lirc_dev_lock))
450 return -ERESTARTSYS;
451
452 ir = irctls[iminor(inode)];
453 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
Jarod Wilson8de111e2011-05-27 16:56:50 -0300470 cdev = ir->cdev;
Jarod Wilsonc1cbb702010-10-18 18:39:20 -0300471 if (try_module_get(cdev->owner)) {
472 ir->open++;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300473 retval = ir->d.set_use_inc(ir->d.data);
474
475 if (retval) {
Jarod Wilsonc1cbb702010-10-18 18:39:20 -0300476 module_put(cdev->owner);
477 ir->open--;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300478 } else {
479 lirc_buffer_clear(ir->buf);
480 }
481 if (ir->task)
482 wake_up_process(ir->task);
483 }
484
485error:
486 if (ir)
487 dev_dbg(ir->d.dev, LOGHEAD "open result = %d\n",
488 ir->d.name, ir->d.minor, retval);
489
490 mutex_unlock(&lirc_dev_lock);
491
Arnd Bergmannd9d2e9d2010-08-15 18:51:56 +0200492 nonseekable_open(inode, file);
493
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300494 return retval;
495}
496EXPORT_SYMBOL(lirc_dev_fop_open);
497
498int lirc_dev_fop_close(struct inode *inode, struct file *file)
499{
500 struct irctl *ir = irctls[iminor(inode)];
Jarod Wilson8de111e2011-05-27 16:56:50 -0300501 struct cdev *cdev;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300502
Jarod Wilson715d29a2010-10-18 12:02:01 -0300503 if (!ir) {
504 printk(KERN_ERR "%s: called with invalid irctl\n", __func__);
505 return -EINVAL;
506 }
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300507
Jarod Wilson8de111e2011-05-27 16:56:50 -0300508 cdev = ir->cdev;
509
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300510 dev_dbg(ir->d.dev, LOGHEAD "close called\n", ir->d.name, ir->d.minor);
511
512 WARN_ON(mutex_lock_killable(&lirc_dev_lock));
513
Jarod Wilson715d29a2010-10-18 12:02:01 -0300514 ir->open--;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300515 if (ir->attached) {
516 ir->d.set_use_dec(ir->d.data);
Jarod Wilsonc1cbb702010-10-18 18:39:20 -0300517 module_put(cdev->owner);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300518 } else {
Jarod Wilson578fcb82010-10-16 21:29:50 -0300519 lirc_irctl_cleanup(ir);
Jarod Wilsonc1cbb702010-10-18 18:39:20 -0300520 cdev_del(cdev);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300521 irctls[ir->d.minor] = NULL;
Jarod Wilson8de111e2011-05-27 16:56:50 -0300522 kfree(cdev);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300523 kfree(ir);
524 }
525
526 mutex_unlock(&lirc_dev_lock);
527
528 return 0;
529}
530EXPORT_SYMBOL(lirc_dev_fop_close);
531
532unsigned int lirc_dev_fop_poll(struct file *file, poll_table *wait)
533{
534 struct irctl *ir = irctls[iminor(file->f_dentry->d_inode)];
535 unsigned int ret;
536
Jarod Wilson715d29a2010-10-18 12:02:01 -0300537 if (!ir) {
538 printk(KERN_ERR "%s: called with invalid irctl\n", __func__);
539 return POLLERR;
540 }
541
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300542 dev_dbg(ir->d.dev, LOGHEAD "poll called\n", ir->d.name, ir->d.minor);
543
Dan Carpenter5c769a62010-11-17 02:12:23 -0300544 if (!ir->attached)
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300545 return POLLERR;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300546
547 poll_wait(file, &ir->buf->wait_poll, wait);
548
549 if (ir->buf)
550 if (lirc_buffer_empty(ir->buf))
551 ret = 0;
552 else
553 ret = POLLIN | POLLRDNORM;
554 else
555 ret = POLLERR;
556
557 dev_dbg(ir->d.dev, LOGHEAD "poll result = %d\n",
558 ir->d.name, ir->d.minor, ret);
559
560 return ret;
561}
562EXPORT_SYMBOL(lirc_dev_fop_poll);
563
Arnd Bergmann044e5872010-08-02 15:43:35 -0300564long lirc_dev_fop_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300565{
Jarod Wilsonbe1f9852010-10-08 17:24:21 -0300566 __u32 mode;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300567 int result = 0;
Jarod Wilsond889a132010-10-16 21:36:43 -0300568 struct irctl *ir = irctls[iminor(file->f_dentry->d_inode)];
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300569
Jarod Wilson8be292c2010-10-09 15:07:06 -0300570 if (!ir) {
571 printk(KERN_ERR "lirc_dev: %s: no irctl found!\n", __func__);
572 return -ENODEV;
573 }
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300574
575 dev_dbg(ir->d.dev, LOGHEAD "ioctl called (0x%x)\n",
576 ir->d.name, ir->d.minor, cmd);
577
578 if (ir->d.minor == NOPLUG || !ir->attached) {
579 dev_dbg(ir->d.dev, LOGHEAD "ioctl result = -ENODEV\n",
580 ir->d.name, ir->d.minor);
581 return -ENODEV;
582 }
583
584 mutex_lock(&ir->irctl_lock);
585
586 switch (cmd) {
587 case LIRC_GET_FEATURES:
Jarod Wilsonbe1f9852010-10-08 17:24:21 -0300588 result = put_user(ir->d.features, (__u32 *)arg);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300589 break;
590 case LIRC_GET_REC_MODE:
591 if (!(ir->d.features & LIRC_CAN_REC_MASK)) {
592 result = -ENOSYS;
593 break;
594 }
595
596 result = put_user(LIRC_REC2MODE
597 (ir->d.features & LIRC_CAN_REC_MASK),
Jarod Wilsonbe1f9852010-10-08 17:24:21 -0300598 (__u32 *)arg);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300599 break;
600 case LIRC_SET_REC_MODE:
601 if (!(ir->d.features & LIRC_CAN_REC_MASK)) {
602 result = -ENOSYS;
603 break;
604 }
605
Jarod Wilsonbe1f9852010-10-08 17:24:21 -0300606 result = get_user(mode, (__u32 *)arg);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300607 if (!result && !(LIRC_MODE2REC(mode) & ir->d.features))
608 result = -EINVAL;
609 /*
610 * FIXME: We should actually set the mode somehow but
611 * for now, lirc_serial doesn't support mode changing either
612 */
613 break;
614 case LIRC_GET_LENGTH:
Jarod Wilsonbe1f9852010-10-08 17:24:21 -0300615 result = put_user(ir->d.code_length, (__u32 *)arg);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300616 break;
617 case LIRC_GET_MIN_TIMEOUT:
618 if (!(ir->d.features & LIRC_CAN_SET_REC_TIMEOUT) ||
619 ir->d.min_timeout == 0) {
620 result = -ENOSYS;
621 break;
622 }
623
Jarod Wilsonbe1f9852010-10-08 17:24:21 -0300624 result = put_user(ir->d.min_timeout, (__u32 *)arg);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300625 break;
626 case LIRC_GET_MAX_TIMEOUT:
627 if (!(ir->d.features & LIRC_CAN_SET_REC_TIMEOUT) ||
628 ir->d.max_timeout == 0) {
629 result = -ENOSYS;
630 break;
631 }
632
Jarod Wilsonbe1f9852010-10-08 17:24:21 -0300633 result = put_user(ir->d.max_timeout, (__u32 *)arg);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300634 break;
635 default:
636 result = -EINVAL;
637 }
638
639 dev_dbg(ir->d.dev, LOGHEAD "ioctl result = %d\n",
640 ir->d.name, ir->d.minor, result);
641
642 mutex_unlock(&ir->irctl_lock);
643
644 return result;
645}
646EXPORT_SYMBOL(lirc_dev_fop_ioctl);
647
648ssize_t lirc_dev_fop_read(struct file *file,
Dan Carpenter0e835082010-11-17 02:13:39 -0300649 char __user *buffer,
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300650 size_t length,
651 loff_t *ppos)
652{
653 struct irctl *ir = irctls[iminor(file->f_dentry->d_inode)];
Jarod Wilson715d29a2010-10-18 12:02:01 -0300654 unsigned char *buf;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300655 int ret = 0, written = 0;
656 DECLARE_WAITQUEUE(wait, current);
657
Jarod Wilson715d29a2010-10-18 12:02:01 -0300658 if (!ir) {
659 printk(KERN_ERR "%s: called with invalid irctl\n", __func__);
660 return -ENODEV;
661 }
662
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300663 dev_dbg(ir->d.dev, LOGHEAD "read called\n", ir->d.name, ir->d.minor);
664
Jarod Wilson715d29a2010-10-18 12:02:01 -0300665 buf = kzalloc(ir->chunk_size, GFP_KERNEL);
666 if (!buf)
667 return -ENOMEM;
668
Dan Carpenter250f7a52010-11-17 02:20:15 -0300669 if (mutex_lock_interruptible(&ir->irctl_lock)) {
670 ret = -ERESTARTSYS;
671 goto out_unlocked;
672 }
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300673 if (!ir->attached) {
Dan Carpenter250f7a52010-11-17 02:20:15 -0300674 ret = -ENODEV;
675 goto out_locked;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300676 }
677
678 if (length % ir->chunk_size) {
Dan Carpenter250f7a52010-11-17 02:20:15 -0300679 ret = -EINVAL;
680 goto out_locked;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300681 }
682
683 /*
684 * we add ourselves to the task queue before buffer check
685 * to avoid losing scan code (in case when queue is awaken somewhere
686 * between while condition checking and scheduling)
687 */
688 add_wait_queue(&ir->buf->wait_poll, &wait);
689 set_current_state(TASK_INTERRUPTIBLE);
690
691 /*
692 * while we didn't provide 'length' bytes, device is opened in blocking
693 * mode and 'copy_to_user' is happy, wait for data.
694 */
695 while (written < length && ret == 0) {
696 if (lirc_buffer_empty(ir->buf)) {
697 /* According to the read(2) man page, 'written' can be
698 * returned as less than 'length', instead of blocking
699 * again, returning -EWOULDBLOCK, or returning
700 * -ERESTARTSYS */
701 if (written)
702 break;
703 if (file->f_flags & O_NONBLOCK) {
704 ret = -EWOULDBLOCK;
705 break;
706 }
707 if (signal_pending(current)) {
708 ret = -ERESTARTSYS;
709 break;
710 }
711
712 mutex_unlock(&ir->irctl_lock);
713 schedule();
714 set_current_state(TASK_INTERRUPTIBLE);
715
716 if (mutex_lock_interruptible(&ir->irctl_lock)) {
717 ret = -ERESTARTSYS;
Jarod Wilson69c271f2010-07-07 11:29:44 -0300718 remove_wait_queue(&ir->buf->wait_poll, &wait);
719 set_current_state(TASK_RUNNING);
720 goto out_unlocked;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300721 }
722
723 if (!ir->attached) {
724 ret = -ENODEV;
725 break;
726 }
727 } else {
728 lirc_buffer_read(ir->buf, buf);
729 ret = copy_to_user((void *)buffer+written, buf,
730 ir->buf->chunk_size);
Dan Carpenter250f7a52010-11-17 02:20:15 -0300731 if (!ret)
732 written += ir->buf->chunk_size;
733 else
734 ret = -EFAULT;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300735 }
736 }
737
738 remove_wait_queue(&ir->buf->wait_poll, &wait);
739 set_current_state(TASK_RUNNING);
Dan Carpenter250f7a52010-11-17 02:20:15 -0300740
741out_locked:
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300742 mutex_unlock(&ir->irctl_lock);
743
Jarod Wilson69c271f2010-07-07 11:29:44 -0300744out_unlocked:
Jarod Wilson715d29a2010-10-18 12:02:01 -0300745 kfree(buf);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300746 dev_dbg(ir->d.dev, LOGHEAD "read result = %s (%d)\n",
Dan Carpenter250f7a52010-11-17 02:20:15 -0300747 ir->d.name, ir->d.minor, ret ? "<fail>" : "<ok>", ret);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300748
749 return ret ? ret : written;
750}
751EXPORT_SYMBOL(lirc_dev_fop_read);
752
753void *lirc_get_pdata(struct file *file)
754{
755 void *data = NULL;
756
757 if (file && file->f_dentry && file->f_dentry->d_inode &&
758 file->f_dentry->d_inode->i_rdev) {
759 struct irctl *ir;
760 ir = irctls[iminor(file->f_dentry->d_inode)];
761 data = ir->d.data;
762 }
763
764 return data;
765}
766EXPORT_SYMBOL(lirc_get_pdata);
767
768
Dan Carpenter0e835082010-11-17 02:13:39 -0300769ssize_t lirc_dev_fop_write(struct file *file, const char __user *buffer,
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300770 size_t length, loff_t *ppos)
771{
772 struct irctl *ir = irctls[iminor(file->f_dentry->d_inode)];
773
Jarod Wilson715d29a2010-10-18 12:02:01 -0300774 if (!ir) {
775 printk(KERN_ERR "%s: called with invalid irctl\n", __func__);
776 return -ENODEV;
777 }
778
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300779 dev_dbg(ir->d.dev, LOGHEAD "write called\n", ir->d.name, ir->d.minor);
780
781 if (!ir->attached)
782 return -ENODEV;
783
784 return -EINVAL;
785}
786EXPORT_SYMBOL(lirc_dev_fop_write);
787
788
789static int __init lirc_dev_init(void)
790{
791 int retval;
792
793 lirc_class = class_create(THIS_MODULE, "lirc");
794 if (IS_ERR(lirc_class)) {
795 retval = PTR_ERR(lirc_class);
796 printk(KERN_ERR "lirc_dev: class_create failed\n");
797 goto error;
798 }
799
800 retval = alloc_chrdev_region(&lirc_base_dev, 0, MAX_IRCTL_DEVICES,
801 IRCTL_DEV_NAME);
802 if (retval) {
803 class_destroy(lirc_class);
804 printk(KERN_ERR "lirc_dev: alloc_chrdev_region failed\n");
805 goto error;
806 }
807
808
809 printk(KERN_INFO "lirc_dev: IR Remote Control driver registered, "
810 "major %d \n", MAJOR(lirc_base_dev));
811
812error:
813 return retval;
814}
815
816
817
818static void __exit lirc_dev_exit(void)
819{
820 class_destroy(lirc_class);
821 unregister_chrdev_region(lirc_base_dev, MAX_IRCTL_DEVICES);
822 printk(KERN_INFO "lirc_dev: module unloaded\n");
823}
824
825module_init(lirc_dev_init);
826module_exit(lirc_dev_exit);
827
828MODULE_DESCRIPTION("LIRC base driver module");
829MODULE_AUTHOR("Artur Lipowski");
830MODULE_LICENSE("GPL");
831
832module_param(debug, bool, S_IRUGO | S_IWUSR);
833MODULE_PARM_DESC(debug, "Enable debugging messages");