blob: 6b9fc74f2e1d018aabd30a7733ea351a26fcc3c2 [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
41static int debug;
42
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
58 struct task_struct *task;
59 long jiffies_to_wait;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -030060};
61
62static DEFINE_MUTEX(lirc_dev_lock);
63
64static struct irctl *irctls[MAX_IRCTL_DEVICES];
Jarod Wilsonc1cbb702010-10-18 18:39:20 -030065static struct cdev cdevs[MAX_IRCTL_DEVICES];
Jarod Wilson4a62a5a2010-07-03 01:06:57 -030066
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{
81 dev_dbg(ir->d.dev, LOGHEAD "cleaning up\n", ir->d.name, ir->d.minor);
82
83 device_destroy(lirc_class, MKDEV(MAJOR(lirc_base_dev), ir->d.minor));
84
85 if (ir->buf != ir->d.rbuf) {
86 lirc_buffer_free(ir->buf);
87 kfree(ir->buf);
88 }
89 ir->buf = NULL;
90}
91
92/* helper function
93 * reads key codes from driver and puts them into buffer
94 * returns 0 on success
95 */
Jarod Wilson578fcb82010-10-16 21:29:50 -030096static int lirc_add_to_buf(struct irctl *ir)
Jarod Wilson4a62a5a2010-07-03 01:06:57 -030097{
98 if (ir->d.add_to_buf) {
99 int res = -ENODATA;
100 int got_data = 0;
101
102 /*
103 * service the device as long as it is returning
104 * data and we have space
105 */
106get_data:
107 res = ir->d.add_to_buf(ir->d.data, ir->buf);
108 if (res == 0) {
109 got_data++;
110 goto get_data;
111 }
112
113 if (res == -ENODEV)
114 kthread_stop(ir->task);
115
116 return got_data ? 0 : res;
117 }
118
119 return 0;
120}
121
122/* main function of the polling thread
123 */
124static int lirc_thread(void *irctl)
125{
126 struct irctl *ir = irctl;
127
128 dev_dbg(ir->d.dev, LOGHEAD "poll thread started\n",
129 ir->d.name, ir->d.minor);
130
131 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
147 dev_dbg(ir->d.dev, LOGHEAD "poll thread ended\n",
148 ir->d.name, ir->d.minor);
149
150 return 0;
151}
152
153
Jarod Wilson578fcb82010-10-16 21:29:50 -0300154static struct file_operations lirc_dev_fops = {
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300155 .owner = THIS_MODULE,
156 .read = lirc_dev_fop_read,
157 .write = lirc_dev_fop_write,
158 .poll = lirc_dev_fop_poll,
Arnd Bergmann044e5872010-08-02 15:43:35 -0300159 .unlocked_ioctl = lirc_dev_fop_ioctl,
Jarod Wilson8be292c2010-10-09 15:07:06 -0300160#ifdef CONFIG_COMPAT
161 .compat_ioctl = lirc_dev_fop_ioctl,
162#endif
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300163 .open = lirc_dev_fop_open,
164 .release = lirc_dev_fop_close,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200165 .llseek = noop_llseek,
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300166};
167
168static int lirc_cdev_add(struct irctl *ir)
169{
170 int retval;
171 struct lirc_driver *d = &ir->d;
Jarod Wilsonc1cbb702010-10-18 18:39:20 -0300172 struct cdev *cdev = &cdevs[d->minor];
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300173
174 if (d->fops) {
Jarod Wilsonc1cbb702010-10-18 18:39:20 -0300175 cdev_init(cdev, d->fops);
176 cdev->owner = d->owner;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300177 } else {
Jarod Wilsonc1cbb702010-10-18 18:39:20 -0300178 cdev_init(cdev, &lirc_dev_fops);
179 cdev->owner = THIS_MODULE;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300180 }
Jarod Wilsonc1cbb702010-10-18 18:39:20 -0300181 kobject_set_name(&cdev->kobj, "lirc%d", d->minor);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300182
Jarod Wilsonc1cbb702010-10-18 18:39:20 -0300183 retval = cdev_add(cdev, MKDEV(MAJOR(lirc_base_dev), d->minor), 1);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300184 if (retval)
Jarod Wilsonc1cbb702010-10-18 18:39:20 -0300185 kobject_put(&cdev->kobj);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300186
187 return retval;
188}
189
190int lirc_register_driver(struct lirc_driver *d)
191{
192 struct irctl *ir;
193 int minor;
194 int bytes_in_key;
195 unsigned int chunk_size;
196 unsigned int buffer_size;
197 int err;
198
199 if (!d) {
200 printk(KERN_ERR "lirc_dev: lirc_register_driver: "
201 "driver pointer must be not NULL!\n");
202 err = -EBADRQC;
203 goto out;
204 }
205
Jarod Wilson715d29a2010-10-18 12:02:01 -0300206 if (!d->dev) {
207 printk(KERN_ERR "%s: dev pointer not filled in!\n", __func__);
208 err = -EINVAL;
209 goto out;
210 }
211
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300212 if (MAX_IRCTL_DEVICES <= d->minor) {
213 dev_err(d->dev, "lirc_dev: lirc_register_driver: "
214 "\"minor\" must be between 0 and %d (%d)!\n",
215 MAX_IRCTL_DEVICES-1, d->minor);
216 err = -EBADRQC;
217 goto out;
218 }
219
220 if (1 > d->code_length || (BUFLEN * 8) < d->code_length) {
221 dev_err(d->dev, "lirc_dev: lirc_register_driver: "
222 "code length in bits for minor (%d) "
223 "must be less than %d!\n",
224 d->minor, BUFLEN * 8);
225 err = -EBADRQC;
226 goto out;
227 }
228
229 dev_dbg(d->dev, "lirc_dev: lirc_register_driver: sample_rate: %d\n",
230 d->sample_rate);
231 if (d->sample_rate) {
232 if (2 > d->sample_rate || HZ < d->sample_rate) {
233 dev_err(d->dev, "lirc_dev: lirc_register_driver: "
234 "sample_rate must be between 2 and %d!\n", HZ);
235 err = -EBADRQC;
236 goto out;
237 }
238 if (!d->add_to_buf) {
239 dev_err(d->dev, "lirc_dev: lirc_register_driver: "
240 "add_to_buf cannot be NULL when "
241 "sample_rate is set\n");
242 err = -EBADRQC;
243 goto out;
244 }
245 } else if (!(d->fops && d->fops->read) && !d->rbuf) {
246 dev_err(d->dev, "lirc_dev: lirc_register_driver: "
247 "fops->read and rbuf cannot all be NULL!\n");
248 err = -EBADRQC;
249 goto out;
250 } else if (!d->rbuf) {
251 if (!(d->fops && d->fops->read && d->fops->poll &&
Arnd Bergmann044e5872010-08-02 15:43:35 -0300252 d->fops->unlocked_ioctl)) {
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300253 dev_err(d->dev, "lirc_dev: lirc_register_driver: "
Arnd Bergmann044e5872010-08-02 15:43:35 -0300254 "neither read, poll nor unlocked_ioctl can be NULL!\n");
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300255 err = -EBADRQC;
256 goto out;
257 }
258 }
259
260 mutex_lock(&lirc_dev_lock);
261
262 minor = d->minor;
263
264 if (minor < 0) {
265 /* find first free slot for driver */
266 for (minor = 0; minor < MAX_IRCTL_DEVICES; minor++)
267 if (!irctls[minor])
268 break;
269 if (MAX_IRCTL_DEVICES == minor) {
270 dev_err(d->dev, "lirc_dev: lirc_register_driver: "
271 "no free slots for drivers!\n");
272 err = -ENOMEM;
273 goto out_lock;
274 }
275 } else if (irctls[minor]) {
276 dev_err(d->dev, "lirc_dev: lirc_register_driver: "
277 "minor (%d) just registered!\n", minor);
278 err = -EBUSY;
279 goto out_lock;
280 }
281
282 ir = kzalloc(sizeof(struct irctl), GFP_KERNEL);
283 if (!ir) {
284 err = -ENOMEM;
285 goto out_lock;
286 }
Jarod Wilson578fcb82010-10-16 21:29:50 -0300287 lirc_irctl_init(ir);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300288 irctls[minor] = ir;
289 d->minor = minor;
290
291 if (d->sample_rate) {
292 ir->jiffies_to_wait = HZ / d->sample_rate;
293 } else {
294 /* it means - wait for external event in task queue */
295 ir->jiffies_to_wait = 0;
296 }
297
298 /* some safety check 8-) */
299 d->name[sizeof(d->name)-1] = '\0';
300
301 bytes_in_key = BITS_TO_LONGS(d->code_length) +
302 (d->code_length % 8 ? 1 : 0);
303 buffer_size = d->buffer_size ? d->buffer_size : BUFLEN / bytes_in_key;
304 chunk_size = d->chunk_size ? d->chunk_size : bytes_in_key;
305
306 if (d->rbuf) {
307 ir->buf = d->rbuf;
308 } else {
309 ir->buf = kmalloc(sizeof(struct lirc_buffer), GFP_KERNEL);
310 if (!ir->buf) {
311 err = -ENOMEM;
312 goto out_lock;
313 }
314 err = lirc_buffer_init(ir->buf, chunk_size, buffer_size);
315 if (err) {
316 kfree(ir->buf);
317 goto out_lock;
318 }
319 }
320 ir->chunk_size = ir->buf->chunk_size;
321
322 if (d->features == 0)
323 d->features = LIRC_CAN_REC_LIRCCODE;
324
325 ir->d = *d;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300326
327 device_create(lirc_class, ir->d.dev,
328 MKDEV(MAJOR(lirc_base_dev), ir->d.minor), NULL,
329 "lirc%u", ir->d.minor);
330
331 if (d->sample_rate) {
332 /* try to fire up polling thread */
333 ir->task = kthread_run(lirc_thread, (void *)ir, "lirc_dev");
334 if (IS_ERR(ir->task)) {
335 dev_err(d->dev, "lirc_dev: lirc_register_driver: "
336 "cannot run poll thread for minor = %d\n",
337 d->minor);
338 err = -ECHILD;
339 goto out_sysfs;
340 }
341 }
342
343 err = lirc_cdev_add(ir);
344 if (err)
345 goto out_sysfs;
346
347 ir->attached = 1;
348 mutex_unlock(&lirc_dev_lock);
349
350 dev_info(ir->d.dev, "lirc_dev: driver %s registered at minor = %d\n",
351 ir->d.name, ir->d.minor);
352 return minor;
353
354out_sysfs:
355 device_destroy(lirc_class, MKDEV(MAJOR(lirc_base_dev), ir->d.minor));
356out_lock:
357 mutex_unlock(&lirc_dev_lock);
358out:
359 return err;
360}
361EXPORT_SYMBOL(lirc_register_driver);
362
363int lirc_unregister_driver(int minor)
364{
365 struct irctl *ir;
Jarod Wilsonc1cbb702010-10-18 18:39:20 -0300366 struct cdev *cdev;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300367
368 if (minor < 0 || minor >= MAX_IRCTL_DEVICES) {
Jarod Wilson4fc21542010-10-09 15:17:03 -0300369 printk(KERN_ERR "lirc_dev: %s: minor (%d) must be between "
370 "0 and %d!\n", __func__, minor, MAX_IRCTL_DEVICES-1);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300371 return -EBADRQC;
372 }
373
374 ir = irctls[minor];
Jarod Wilsondf1868e2010-09-17 18:12:31 -0300375 if (!ir) {
Jarod Wilson4fc21542010-10-09 15:17:03 -0300376 printk(KERN_ERR "lirc_dev: %s: failed to get irctl struct "
377 "for minor %d!\n", __func__, minor);
Jarod Wilsondf1868e2010-09-17 18:12:31 -0300378 return -ENOENT;
379 }
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300380
Jarod Wilsonc1cbb702010-10-18 18:39:20 -0300381 cdev = &cdevs[minor];
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300382
383 mutex_lock(&lirc_dev_lock);
384
385 if (ir->d.minor != minor) {
Jarod Wilson4fc21542010-10-09 15:17:03 -0300386 printk(KERN_ERR "lirc_dev: %s: minor (%d) device not "
387 "registered!\n", __func__, 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);
405 ir->d.set_use_dec(ir->d.data);
Jarod Wilsonc1cbb702010-10-18 18:39:20 -0300406 module_put(cdev->owner);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300407 mutex_unlock(&ir->irctl_lock);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300408 } else {
Jarod Wilson578fcb82010-10-16 21:29:50 -0300409 lirc_irctl_cleanup(ir);
Jarod Wilsonc1cbb702010-10-18 18:39:20 -0300410 cdev_del(cdev);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300411 kfree(ir);
412 irctls[minor] = NULL;
413 }
414
415 mutex_unlock(&lirc_dev_lock);
416
417 return 0;
418}
419EXPORT_SYMBOL(lirc_unregister_driver);
420
421int lirc_dev_fop_open(struct inode *inode, struct file *file)
422{
423 struct irctl *ir;
Jarod Wilsonc1cbb702010-10-18 18:39:20 -0300424 struct cdev *cdev;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300425 int retval = 0;
426
427 if (iminor(inode) >= MAX_IRCTL_DEVICES) {
428 printk(KERN_WARNING "lirc_dev [%d]: open result = -ENODEV\n",
429 iminor(inode));
430 return -ENODEV;
431 }
432
433 if (mutex_lock_interruptible(&lirc_dev_lock))
434 return -ERESTARTSYS;
435
436 ir = irctls[iminor(inode)];
437 if (!ir) {
438 retval = -ENODEV;
439 goto error;
440 }
441
442 dev_dbg(ir->d.dev, LOGHEAD "open called\n", ir->d.name, ir->d.minor);
443
444 if (ir->d.minor == NOPLUG) {
445 retval = -ENODEV;
446 goto error;
447 }
448
449 if (ir->open) {
450 retval = -EBUSY;
451 goto error;
452 }
453
Jarod Wilsonc1cbb702010-10-18 18:39:20 -0300454 cdev = &cdevs[iminor(inode)];
455 if (try_module_get(cdev->owner)) {
456 ir->open++;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300457 retval = ir->d.set_use_inc(ir->d.data);
458
459 if (retval) {
Jarod Wilsonc1cbb702010-10-18 18:39:20 -0300460 module_put(cdev->owner);
461 ir->open--;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300462 } else {
463 lirc_buffer_clear(ir->buf);
464 }
465 if (ir->task)
466 wake_up_process(ir->task);
467 }
468
469error:
470 if (ir)
471 dev_dbg(ir->d.dev, LOGHEAD "open result = %d\n",
472 ir->d.name, ir->d.minor, retval);
473
474 mutex_unlock(&lirc_dev_lock);
475
Arnd Bergmannd9d2e9d2010-08-15 18:51:56 +0200476 nonseekable_open(inode, file);
477
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300478 return retval;
479}
480EXPORT_SYMBOL(lirc_dev_fop_open);
481
482int lirc_dev_fop_close(struct inode *inode, struct file *file)
483{
484 struct irctl *ir = irctls[iminor(inode)];
Jarod Wilsonc1cbb702010-10-18 18:39:20 -0300485 struct cdev *cdev = &cdevs[iminor(inode)];
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300486
Jarod Wilson715d29a2010-10-18 12:02:01 -0300487 if (!ir) {
488 printk(KERN_ERR "%s: called with invalid irctl\n", __func__);
489 return -EINVAL;
490 }
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300491
492 dev_dbg(ir->d.dev, LOGHEAD "close called\n", ir->d.name, ir->d.minor);
493
494 WARN_ON(mutex_lock_killable(&lirc_dev_lock));
495
Jarod Wilson715d29a2010-10-18 12:02:01 -0300496 ir->open--;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300497 if (ir->attached) {
498 ir->d.set_use_dec(ir->d.data);
Jarod Wilsonc1cbb702010-10-18 18:39:20 -0300499 module_put(cdev->owner);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300500 } else {
Jarod Wilson578fcb82010-10-16 21:29:50 -0300501 lirc_irctl_cleanup(ir);
Jarod Wilsonc1cbb702010-10-18 18:39:20 -0300502 cdev_del(cdev);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300503 irctls[ir->d.minor] = NULL;
504 kfree(ir);
505 }
506
507 mutex_unlock(&lirc_dev_lock);
508
509 return 0;
510}
511EXPORT_SYMBOL(lirc_dev_fop_close);
512
513unsigned int lirc_dev_fop_poll(struct file *file, poll_table *wait)
514{
515 struct irctl *ir = irctls[iminor(file->f_dentry->d_inode)];
516 unsigned int ret;
517
Jarod Wilson715d29a2010-10-18 12:02:01 -0300518 if (!ir) {
519 printk(KERN_ERR "%s: called with invalid irctl\n", __func__);
520 return POLLERR;
521 }
522
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300523 dev_dbg(ir->d.dev, LOGHEAD "poll called\n", ir->d.name, ir->d.minor);
524
Dan Carpenter5c769a62010-11-17 02:12:23 -0300525 if (!ir->attached)
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300526 return POLLERR;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300527
528 poll_wait(file, &ir->buf->wait_poll, wait);
529
530 if (ir->buf)
531 if (lirc_buffer_empty(ir->buf))
532 ret = 0;
533 else
534 ret = POLLIN | POLLRDNORM;
535 else
536 ret = POLLERR;
537
538 dev_dbg(ir->d.dev, LOGHEAD "poll result = %d\n",
539 ir->d.name, ir->d.minor, ret);
540
541 return ret;
542}
543EXPORT_SYMBOL(lirc_dev_fop_poll);
544
Arnd Bergmann044e5872010-08-02 15:43:35 -0300545long lirc_dev_fop_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300546{
Jarod Wilsonbe1f9852010-10-08 17:24:21 -0300547 __u32 mode;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300548 int result = 0;
Jarod Wilsond889a132010-10-16 21:36:43 -0300549 struct irctl *ir = irctls[iminor(file->f_dentry->d_inode)];
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300550
Jarod Wilson8be292c2010-10-09 15:07:06 -0300551 if (!ir) {
552 printk(KERN_ERR "lirc_dev: %s: no irctl found!\n", __func__);
553 return -ENODEV;
554 }
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300555
556 dev_dbg(ir->d.dev, LOGHEAD "ioctl called (0x%x)\n",
557 ir->d.name, ir->d.minor, cmd);
558
559 if (ir->d.minor == NOPLUG || !ir->attached) {
560 dev_dbg(ir->d.dev, LOGHEAD "ioctl result = -ENODEV\n",
561 ir->d.name, ir->d.minor);
562 return -ENODEV;
563 }
564
565 mutex_lock(&ir->irctl_lock);
566
567 switch (cmd) {
568 case LIRC_GET_FEATURES:
Jarod Wilsonbe1f9852010-10-08 17:24:21 -0300569 result = put_user(ir->d.features, (__u32 *)arg);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300570 break;
571 case LIRC_GET_REC_MODE:
572 if (!(ir->d.features & LIRC_CAN_REC_MASK)) {
573 result = -ENOSYS;
574 break;
575 }
576
577 result = put_user(LIRC_REC2MODE
578 (ir->d.features & LIRC_CAN_REC_MASK),
Jarod Wilsonbe1f9852010-10-08 17:24:21 -0300579 (__u32 *)arg);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300580 break;
581 case LIRC_SET_REC_MODE:
582 if (!(ir->d.features & LIRC_CAN_REC_MASK)) {
583 result = -ENOSYS;
584 break;
585 }
586
Jarod Wilsonbe1f9852010-10-08 17:24:21 -0300587 result = get_user(mode, (__u32 *)arg);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300588 if (!result && !(LIRC_MODE2REC(mode) & ir->d.features))
589 result = -EINVAL;
590 /*
591 * FIXME: We should actually set the mode somehow but
592 * for now, lirc_serial doesn't support mode changing either
593 */
594 break;
595 case LIRC_GET_LENGTH:
Jarod Wilsonbe1f9852010-10-08 17:24:21 -0300596 result = put_user(ir->d.code_length, (__u32 *)arg);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300597 break;
598 case LIRC_GET_MIN_TIMEOUT:
599 if (!(ir->d.features & LIRC_CAN_SET_REC_TIMEOUT) ||
600 ir->d.min_timeout == 0) {
601 result = -ENOSYS;
602 break;
603 }
604
Jarod Wilsonbe1f9852010-10-08 17:24:21 -0300605 result = put_user(ir->d.min_timeout, (__u32 *)arg);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300606 break;
607 case LIRC_GET_MAX_TIMEOUT:
608 if (!(ir->d.features & LIRC_CAN_SET_REC_TIMEOUT) ||
609 ir->d.max_timeout == 0) {
610 result = -ENOSYS;
611 break;
612 }
613
Jarod Wilsonbe1f9852010-10-08 17:24:21 -0300614 result = put_user(ir->d.max_timeout, (__u32 *)arg);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300615 break;
616 default:
617 result = -EINVAL;
618 }
619
620 dev_dbg(ir->d.dev, LOGHEAD "ioctl result = %d\n",
621 ir->d.name, ir->d.minor, result);
622
623 mutex_unlock(&ir->irctl_lock);
624
625 return result;
626}
627EXPORT_SYMBOL(lirc_dev_fop_ioctl);
628
629ssize_t lirc_dev_fop_read(struct file *file,
Dan Carpenter0e835082010-11-17 02:13:39 -0300630 char __user *buffer,
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300631 size_t length,
632 loff_t *ppos)
633{
634 struct irctl *ir = irctls[iminor(file->f_dentry->d_inode)];
Jarod Wilson715d29a2010-10-18 12:02:01 -0300635 unsigned char *buf;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300636 int ret = 0, written = 0;
637 DECLARE_WAITQUEUE(wait, current);
638
Jarod Wilson715d29a2010-10-18 12:02:01 -0300639 if (!ir) {
640 printk(KERN_ERR "%s: called with invalid irctl\n", __func__);
641 return -ENODEV;
642 }
643
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300644 dev_dbg(ir->d.dev, LOGHEAD "read called\n", ir->d.name, ir->d.minor);
645
Jarod Wilson715d29a2010-10-18 12:02:01 -0300646 buf = kzalloc(ir->chunk_size, GFP_KERNEL);
647 if (!buf)
648 return -ENOMEM;
649
Dan Carpenter250f7a52010-11-17 02:20:15 -0300650 if (mutex_lock_interruptible(&ir->irctl_lock)) {
651 ret = -ERESTARTSYS;
652 goto out_unlocked;
653 }
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300654 if (!ir->attached) {
Dan Carpenter250f7a52010-11-17 02:20:15 -0300655 ret = -ENODEV;
656 goto out_locked;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300657 }
658
659 if (length % ir->chunk_size) {
Dan Carpenter250f7a52010-11-17 02:20:15 -0300660 ret = -EINVAL;
661 goto out_locked;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300662 }
663
664 /*
665 * we add ourselves to the task queue before buffer check
666 * to avoid losing scan code (in case when queue is awaken somewhere
667 * between while condition checking and scheduling)
668 */
669 add_wait_queue(&ir->buf->wait_poll, &wait);
670 set_current_state(TASK_INTERRUPTIBLE);
671
672 /*
673 * while we didn't provide 'length' bytes, device is opened in blocking
674 * mode and 'copy_to_user' is happy, wait for data.
675 */
676 while (written < length && ret == 0) {
677 if (lirc_buffer_empty(ir->buf)) {
678 /* According to the read(2) man page, 'written' can be
679 * returned as less than 'length', instead of blocking
680 * again, returning -EWOULDBLOCK, or returning
681 * -ERESTARTSYS */
682 if (written)
683 break;
684 if (file->f_flags & O_NONBLOCK) {
685 ret = -EWOULDBLOCK;
686 break;
687 }
688 if (signal_pending(current)) {
689 ret = -ERESTARTSYS;
690 break;
691 }
692
693 mutex_unlock(&ir->irctl_lock);
694 schedule();
695 set_current_state(TASK_INTERRUPTIBLE);
696
697 if (mutex_lock_interruptible(&ir->irctl_lock)) {
698 ret = -ERESTARTSYS;
Jarod Wilson69c271f2010-07-07 11:29:44 -0300699 remove_wait_queue(&ir->buf->wait_poll, &wait);
700 set_current_state(TASK_RUNNING);
701 goto out_unlocked;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300702 }
703
704 if (!ir->attached) {
705 ret = -ENODEV;
706 break;
707 }
708 } else {
709 lirc_buffer_read(ir->buf, buf);
710 ret = copy_to_user((void *)buffer+written, buf,
711 ir->buf->chunk_size);
Dan Carpenter250f7a52010-11-17 02:20:15 -0300712 if (!ret)
713 written += ir->buf->chunk_size;
714 else
715 ret = -EFAULT;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300716 }
717 }
718
719 remove_wait_queue(&ir->buf->wait_poll, &wait);
720 set_current_state(TASK_RUNNING);
Dan Carpenter250f7a52010-11-17 02:20:15 -0300721
722out_locked:
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300723 mutex_unlock(&ir->irctl_lock);
724
Jarod Wilson69c271f2010-07-07 11:29:44 -0300725out_unlocked:
Jarod Wilson715d29a2010-10-18 12:02:01 -0300726 kfree(buf);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300727 dev_dbg(ir->d.dev, LOGHEAD "read result = %s (%d)\n",
Dan Carpenter250f7a52010-11-17 02:20:15 -0300728 ir->d.name, ir->d.minor, ret ? "<fail>" : "<ok>", ret);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300729
730 return ret ? ret : written;
731}
732EXPORT_SYMBOL(lirc_dev_fop_read);
733
734void *lirc_get_pdata(struct file *file)
735{
736 void *data = NULL;
737
738 if (file && file->f_dentry && file->f_dentry->d_inode &&
739 file->f_dentry->d_inode->i_rdev) {
740 struct irctl *ir;
741 ir = irctls[iminor(file->f_dentry->d_inode)];
742 data = ir->d.data;
743 }
744
745 return data;
746}
747EXPORT_SYMBOL(lirc_get_pdata);
748
749
Dan Carpenter0e835082010-11-17 02:13:39 -0300750ssize_t lirc_dev_fop_write(struct file *file, const char __user *buffer,
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300751 size_t length, loff_t *ppos)
752{
753 struct irctl *ir = irctls[iminor(file->f_dentry->d_inode)];
754
Jarod Wilson715d29a2010-10-18 12:02:01 -0300755 if (!ir) {
756 printk(KERN_ERR "%s: called with invalid irctl\n", __func__);
757 return -ENODEV;
758 }
759
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300760 dev_dbg(ir->d.dev, LOGHEAD "write called\n", ir->d.name, ir->d.minor);
761
762 if (!ir->attached)
763 return -ENODEV;
764
765 return -EINVAL;
766}
767EXPORT_SYMBOL(lirc_dev_fop_write);
768
769
770static int __init lirc_dev_init(void)
771{
772 int retval;
773
774 lirc_class = class_create(THIS_MODULE, "lirc");
775 if (IS_ERR(lirc_class)) {
776 retval = PTR_ERR(lirc_class);
777 printk(KERN_ERR "lirc_dev: class_create failed\n");
778 goto error;
779 }
780
781 retval = alloc_chrdev_region(&lirc_base_dev, 0, MAX_IRCTL_DEVICES,
782 IRCTL_DEV_NAME);
783 if (retval) {
784 class_destroy(lirc_class);
785 printk(KERN_ERR "lirc_dev: alloc_chrdev_region failed\n");
786 goto error;
787 }
788
789
790 printk(KERN_INFO "lirc_dev: IR Remote Control driver registered, "
791 "major %d \n", MAJOR(lirc_base_dev));
792
793error:
794 return retval;
795}
796
797
798
799static void __exit lirc_dev_exit(void)
800{
801 class_destroy(lirc_class);
802 unregister_chrdev_region(lirc_base_dev, MAX_IRCTL_DEVICES);
803 printk(KERN_INFO "lirc_dev: module unloaded\n");
804}
805
806module_init(lirc_dev_init);
807module_exit(lirc_dev_exit);
808
809MODULE_DESCRIPTION("LIRC base driver module");
810MODULE_AUTHOR("Artur Lipowski");
811MODULE_LICENSE("GPL");
812
813module_param(debug, bool, S_IRUGO | S_IWUSR);
814MODULE_PARM_DESC(debug, "Enable debugging messages");