blob: fd237ab120bbbd8a3a753b379f3edddb41079f61 [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 }
Vasiliy Kulikovb395cba2010-11-26 14:06:41 -0300181 retval = kobject_set_name(&cdev->kobj, "lirc%d", d->minor);
182 if (retval)
183 return retval;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300184
Jarod Wilsonc1cbb702010-10-18 18:39:20 -0300185 retval = cdev_add(cdev, MKDEV(MAJOR(lirc_base_dev), d->minor), 1);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300186 if (retval)
Jarod Wilsonc1cbb702010-10-18 18:39:20 -0300187 kobject_put(&cdev->kobj);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300188
189 return retval;
190}
191
192int lirc_register_driver(struct lirc_driver *d)
193{
194 struct irctl *ir;
195 int minor;
196 int bytes_in_key;
197 unsigned int chunk_size;
198 unsigned int buffer_size;
199 int err;
200
201 if (!d) {
202 printk(KERN_ERR "lirc_dev: lirc_register_driver: "
203 "driver pointer must be not NULL!\n");
204 err = -EBADRQC;
205 goto out;
206 }
207
Jarod Wilson715d29a2010-10-18 12:02:01 -0300208 if (!d->dev) {
209 printk(KERN_ERR "%s: dev pointer not filled in!\n", __func__);
210 err = -EINVAL;
211 goto out;
212 }
213
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300214 if (MAX_IRCTL_DEVICES <= d->minor) {
215 dev_err(d->dev, "lirc_dev: lirc_register_driver: "
216 "\"minor\" must be between 0 and %d (%d)!\n",
217 MAX_IRCTL_DEVICES-1, d->minor);
218 err = -EBADRQC;
219 goto out;
220 }
221
222 if (1 > d->code_length || (BUFLEN * 8) < d->code_length) {
223 dev_err(d->dev, "lirc_dev: lirc_register_driver: "
224 "code length in bits for minor (%d) "
225 "must be less than %d!\n",
226 d->minor, BUFLEN * 8);
227 err = -EBADRQC;
228 goto out;
229 }
230
231 dev_dbg(d->dev, "lirc_dev: lirc_register_driver: sample_rate: %d\n",
232 d->sample_rate);
233 if (d->sample_rate) {
234 if (2 > d->sample_rate || HZ < d->sample_rate) {
235 dev_err(d->dev, "lirc_dev: lirc_register_driver: "
236 "sample_rate must be between 2 and %d!\n", HZ);
237 err = -EBADRQC;
238 goto out;
239 }
240 if (!d->add_to_buf) {
241 dev_err(d->dev, "lirc_dev: lirc_register_driver: "
242 "add_to_buf cannot be NULL when "
243 "sample_rate is set\n");
244 err = -EBADRQC;
245 goto out;
246 }
247 } else if (!(d->fops && d->fops->read) && !d->rbuf) {
248 dev_err(d->dev, "lirc_dev: lirc_register_driver: "
249 "fops->read and rbuf cannot all be NULL!\n");
250 err = -EBADRQC;
251 goto out;
252 } else if (!d->rbuf) {
253 if (!(d->fops && d->fops->read && d->fops->poll &&
Arnd Bergmann044e5872010-08-02 15:43:35 -0300254 d->fops->unlocked_ioctl)) {
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300255 dev_err(d->dev, "lirc_dev: lirc_register_driver: "
Arnd Bergmann044e5872010-08-02 15:43:35 -0300256 "neither read, poll nor unlocked_ioctl can be NULL!\n");
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300257 err = -EBADRQC;
258 goto out;
259 }
260 }
261
262 mutex_lock(&lirc_dev_lock);
263
264 minor = d->minor;
265
266 if (minor < 0) {
267 /* find first free slot for driver */
268 for (minor = 0; minor < MAX_IRCTL_DEVICES; minor++)
269 if (!irctls[minor])
270 break;
271 if (MAX_IRCTL_DEVICES == minor) {
272 dev_err(d->dev, "lirc_dev: lirc_register_driver: "
273 "no free slots for drivers!\n");
274 err = -ENOMEM;
275 goto out_lock;
276 }
277 } else if (irctls[minor]) {
278 dev_err(d->dev, "lirc_dev: lirc_register_driver: "
279 "minor (%d) just registered!\n", minor);
280 err = -EBUSY;
281 goto out_lock;
282 }
283
284 ir = kzalloc(sizeof(struct irctl), GFP_KERNEL);
285 if (!ir) {
286 err = -ENOMEM;
287 goto out_lock;
288 }
Jarod Wilson578fcb82010-10-16 21:29:50 -0300289 lirc_irctl_init(ir);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300290 irctls[minor] = ir;
291 d->minor = minor;
292
293 if (d->sample_rate) {
294 ir->jiffies_to_wait = HZ / d->sample_rate;
295 } else {
296 /* it means - wait for external event in task queue */
297 ir->jiffies_to_wait = 0;
298 }
299
300 /* some safety check 8-) */
301 d->name[sizeof(d->name)-1] = '\0';
302
303 bytes_in_key = BITS_TO_LONGS(d->code_length) +
304 (d->code_length % 8 ? 1 : 0);
305 buffer_size = d->buffer_size ? d->buffer_size : BUFLEN / bytes_in_key;
306 chunk_size = d->chunk_size ? d->chunk_size : bytes_in_key;
307
308 if (d->rbuf) {
309 ir->buf = d->rbuf;
310 } else {
311 ir->buf = kmalloc(sizeof(struct lirc_buffer), GFP_KERNEL);
312 if (!ir->buf) {
313 err = -ENOMEM;
314 goto out_lock;
315 }
316 err = lirc_buffer_init(ir->buf, chunk_size, buffer_size);
317 if (err) {
318 kfree(ir->buf);
319 goto out_lock;
320 }
321 }
322 ir->chunk_size = ir->buf->chunk_size;
323
324 if (d->features == 0)
325 d->features = LIRC_CAN_REC_LIRCCODE;
326
327 ir->d = *d;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300328
329 device_create(lirc_class, ir->d.dev,
330 MKDEV(MAJOR(lirc_base_dev), ir->d.minor), NULL,
331 "lirc%u", ir->d.minor);
332
333 if (d->sample_rate) {
334 /* try to fire up polling thread */
335 ir->task = kthread_run(lirc_thread, (void *)ir, "lirc_dev");
336 if (IS_ERR(ir->task)) {
337 dev_err(d->dev, "lirc_dev: lirc_register_driver: "
338 "cannot run poll thread for minor = %d\n",
339 d->minor);
340 err = -ECHILD;
341 goto out_sysfs;
342 }
343 }
344
345 err = lirc_cdev_add(ir);
346 if (err)
347 goto out_sysfs;
348
349 ir->attached = 1;
350 mutex_unlock(&lirc_dev_lock);
351
352 dev_info(ir->d.dev, "lirc_dev: driver %s registered at minor = %d\n",
353 ir->d.name, ir->d.minor);
354 return minor;
355
356out_sysfs:
357 device_destroy(lirc_class, MKDEV(MAJOR(lirc_base_dev), ir->d.minor));
358out_lock:
359 mutex_unlock(&lirc_dev_lock);
360out:
361 return err;
362}
363EXPORT_SYMBOL(lirc_register_driver);
364
365int lirc_unregister_driver(int minor)
366{
367 struct irctl *ir;
Jarod Wilsonc1cbb702010-10-18 18:39:20 -0300368 struct cdev *cdev;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300369
370 if (minor < 0 || minor >= MAX_IRCTL_DEVICES) {
Jarod Wilson4fc21542010-10-09 15:17:03 -0300371 printk(KERN_ERR "lirc_dev: %s: minor (%d) must be between "
372 "0 and %d!\n", __func__, minor, MAX_IRCTL_DEVICES-1);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300373 return -EBADRQC;
374 }
375
376 ir = irctls[minor];
Jarod Wilsondf1868e2010-09-17 18:12:31 -0300377 if (!ir) {
Jarod Wilson4fc21542010-10-09 15:17:03 -0300378 printk(KERN_ERR "lirc_dev: %s: failed to get irctl struct "
379 "for minor %d!\n", __func__, minor);
Jarod Wilsondf1868e2010-09-17 18:12:31 -0300380 return -ENOENT;
381 }
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300382
Jarod Wilsonc1cbb702010-10-18 18:39:20 -0300383 cdev = &cdevs[minor];
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300384
385 mutex_lock(&lirc_dev_lock);
386
387 if (ir->d.minor != minor) {
Jarod Wilson4fc21542010-10-09 15:17:03 -0300388 printk(KERN_ERR "lirc_dev: %s: minor (%d) device not "
389 "registered!\n", __func__, minor);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300390 mutex_unlock(&lirc_dev_lock);
391 return -ENOENT;
392 }
393
394 /* end up polling thread */
395 if (ir->task)
396 kthread_stop(ir->task);
397
398 dev_dbg(ir->d.dev, "lirc_dev: driver %s unregistered from minor = %d\n",
399 ir->d.name, ir->d.minor);
400
401 ir->attached = 0;
402 if (ir->open) {
403 dev_dbg(ir->d.dev, LOGHEAD "releasing opened driver\n",
404 ir->d.name, ir->d.minor);
405 wake_up_interruptible(&ir->buf->wait_poll);
406 mutex_lock(&ir->irctl_lock);
407 ir->d.set_use_dec(ir->d.data);
Jarod Wilsonc1cbb702010-10-18 18:39:20 -0300408 module_put(cdev->owner);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300409 mutex_unlock(&ir->irctl_lock);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300410 } else {
Jarod Wilson578fcb82010-10-16 21:29:50 -0300411 lirc_irctl_cleanup(ir);
Jarod Wilsonc1cbb702010-10-18 18:39:20 -0300412 cdev_del(cdev);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300413 kfree(ir);
414 irctls[minor] = NULL;
415 }
416
417 mutex_unlock(&lirc_dev_lock);
418
419 return 0;
420}
421EXPORT_SYMBOL(lirc_unregister_driver);
422
423int lirc_dev_fop_open(struct inode *inode, struct file *file)
424{
425 struct irctl *ir;
Jarod Wilsonc1cbb702010-10-18 18:39:20 -0300426 struct cdev *cdev;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300427 int retval = 0;
428
429 if (iminor(inode) >= MAX_IRCTL_DEVICES) {
430 printk(KERN_WARNING "lirc_dev [%d]: open result = -ENODEV\n",
431 iminor(inode));
432 return -ENODEV;
433 }
434
435 if (mutex_lock_interruptible(&lirc_dev_lock))
436 return -ERESTARTSYS;
437
438 ir = irctls[iminor(inode)];
439 if (!ir) {
440 retval = -ENODEV;
441 goto error;
442 }
443
444 dev_dbg(ir->d.dev, LOGHEAD "open called\n", ir->d.name, ir->d.minor);
445
446 if (ir->d.minor == NOPLUG) {
447 retval = -ENODEV;
448 goto error;
449 }
450
451 if (ir->open) {
452 retval = -EBUSY;
453 goto error;
454 }
455
Jarod Wilsonc1cbb702010-10-18 18:39:20 -0300456 cdev = &cdevs[iminor(inode)];
457 if (try_module_get(cdev->owner)) {
458 ir->open++;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300459 retval = ir->d.set_use_inc(ir->d.data);
460
461 if (retval) {
Jarod Wilsonc1cbb702010-10-18 18:39:20 -0300462 module_put(cdev->owner);
463 ir->open--;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300464 } else {
465 lirc_buffer_clear(ir->buf);
466 }
467 if (ir->task)
468 wake_up_process(ir->task);
469 }
470
471error:
472 if (ir)
473 dev_dbg(ir->d.dev, LOGHEAD "open result = %d\n",
474 ir->d.name, ir->d.minor, retval);
475
476 mutex_unlock(&lirc_dev_lock);
477
Arnd Bergmannd9d2e9d2010-08-15 18:51:56 +0200478 nonseekable_open(inode, file);
479
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300480 return retval;
481}
482EXPORT_SYMBOL(lirc_dev_fop_open);
483
484int lirc_dev_fop_close(struct inode *inode, struct file *file)
485{
486 struct irctl *ir = irctls[iminor(inode)];
Jarod Wilsonc1cbb702010-10-18 18:39:20 -0300487 struct cdev *cdev = &cdevs[iminor(inode)];
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300488
Jarod Wilson715d29a2010-10-18 12:02:01 -0300489 if (!ir) {
490 printk(KERN_ERR "%s: called with invalid irctl\n", __func__);
491 return -EINVAL;
492 }
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300493
494 dev_dbg(ir->d.dev, LOGHEAD "close called\n", ir->d.name, ir->d.minor);
495
496 WARN_ON(mutex_lock_killable(&lirc_dev_lock));
497
Jarod Wilson715d29a2010-10-18 12:02:01 -0300498 ir->open--;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300499 if (ir->attached) {
500 ir->d.set_use_dec(ir->d.data);
Jarod Wilsonc1cbb702010-10-18 18:39:20 -0300501 module_put(cdev->owner);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300502 } else {
Jarod Wilson578fcb82010-10-16 21:29:50 -0300503 lirc_irctl_cleanup(ir);
Jarod Wilsonc1cbb702010-10-18 18:39:20 -0300504 cdev_del(cdev);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300505 irctls[ir->d.minor] = NULL;
506 kfree(ir);
507 }
508
509 mutex_unlock(&lirc_dev_lock);
510
511 return 0;
512}
513EXPORT_SYMBOL(lirc_dev_fop_close);
514
515unsigned int lirc_dev_fop_poll(struct file *file, poll_table *wait)
516{
517 struct irctl *ir = irctls[iminor(file->f_dentry->d_inode)];
518 unsigned int ret;
519
Jarod Wilson715d29a2010-10-18 12:02:01 -0300520 if (!ir) {
521 printk(KERN_ERR "%s: called with invalid irctl\n", __func__);
522 return POLLERR;
523 }
524
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300525 dev_dbg(ir->d.dev, LOGHEAD "poll called\n", ir->d.name, ir->d.minor);
526
Dan Carpenter5c769a62010-11-17 02:12:23 -0300527 if (!ir->attached)
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300528 return POLLERR;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300529
530 poll_wait(file, &ir->buf->wait_poll, wait);
531
532 if (ir->buf)
533 if (lirc_buffer_empty(ir->buf))
534 ret = 0;
535 else
536 ret = POLLIN | POLLRDNORM;
537 else
538 ret = POLLERR;
539
540 dev_dbg(ir->d.dev, LOGHEAD "poll result = %d\n",
541 ir->d.name, ir->d.minor, ret);
542
543 return ret;
544}
545EXPORT_SYMBOL(lirc_dev_fop_poll);
546
Arnd Bergmann044e5872010-08-02 15:43:35 -0300547long lirc_dev_fop_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300548{
Jarod Wilsonbe1f9852010-10-08 17:24:21 -0300549 __u32 mode;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300550 int result = 0;
Jarod Wilsond889a132010-10-16 21:36:43 -0300551 struct irctl *ir = irctls[iminor(file->f_dentry->d_inode)];
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300552
Jarod Wilson8be292c2010-10-09 15:07:06 -0300553 if (!ir) {
554 printk(KERN_ERR "lirc_dev: %s: no irctl found!\n", __func__);
555 return -ENODEV;
556 }
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300557
558 dev_dbg(ir->d.dev, LOGHEAD "ioctl called (0x%x)\n",
559 ir->d.name, ir->d.minor, cmd);
560
561 if (ir->d.minor == NOPLUG || !ir->attached) {
562 dev_dbg(ir->d.dev, LOGHEAD "ioctl result = -ENODEV\n",
563 ir->d.name, ir->d.minor);
564 return -ENODEV;
565 }
566
567 mutex_lock(&ir->irctl_lock);
568
569 switch (cmd) {
570 case LIRC_GET_FEATURES:
Jarod Wilsonbe1f9852010-10-08 17:24:21 -0300571 result = put_user(ir->d.features, (__u32 *)arg);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300572 break;
573 case LIRC_GET_REC_MODE:
574 if (!(ir->d.features & LIRC_CAN_REC_MASK)) {
575 result = -ENOSYS;
576 break;
577 }
578
579 result = put_user(LIRC_REC2MODE
580 (ir->d.features & LIRC_CAN_REC_MASK),
Jarod Wilsonbe1f9852010-10-08 17:24:21 -0300581 (__u32 *)arg);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300582 break;
583 case LIRC_SET_REC_MODE:
584 if (!(ir->d.features & LIRC_CAN_REC_MASK)) {
585 result = -ENOSYS;
586 break;
587 }
588
Jarod Wilsonbe1f9852010-10-08 17:24:21 -0300589 result = get_user(mode, (__u32 *)arg);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300590 if (!result && !(LIRC_MODE2REC(mode) & ir->d.features))
591 result = -EINVAL;
592 /*
593 * FIXME: We should actually set the mode somehow but
594 * for now, lirc_serial doesn't support mode changing either
595 */
596 break;
597 case LIRC_GET_LENGTH:
Jarod Wilsonbe1f9852010-10-08 17:24:21 -0300598 result = put_user(ir->d.code_length, (__u32 *)arg);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300599 break;
600 case LIRC_GET_MIN_TIMEOUT:
601 if (!(ir->d.features & LIRC_CAN_SET_REC_TIMEOUT) ||
602 ir->d.min_timeout == 0) {
603 result = -ENOSYS;
604 break;
605 }
606
Jarod Wilsonbe1f9852010-10-08 17:24:21 -0300607 result = put_user(ir->d.min_timeout, (__u32 *)arg);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300608 break;
609 case LIRC_GET_MAX_TIMEOUT:
610 if (!(ir->d.features & LIRC_CAN_SET_REC_TIMEOUT) ||
611 ir->d.max_timeout == 0) {
612 result = -ENOSYS;
613 break;
614 }
615
Jarod Wilsonbe1f9852010-10-08 17:24:21 -0300616 result = put_user(ir->d.max_timeout, (__u32 *)arg);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300617 break;
618 default:
619 result = -EINVAL;
620 }
621
622 dev_dbg(ir->d.dev, LOGHEAD "ioctl result = %d\n",
623 ir->d.name, ir->d.minor, result);
624
625 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{
636 struct irctl *ir = irctls[iminor(file->f_dentry->d_inode)];
Jarod Wilson715d29a2010-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 Wilson715d29a2010-10-18 12:02:01 -0300641 if (!ir) {
642 printk(KERN_ERR "%s: called with invalid irctl\n", __func__);
643 return -ENODEV;
644 }
645
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300646 dev_dbg(ir->d.dev, LOGHEAD "read called\n", ir->d.name, ir->d.minor);
647
Jarod Wilson715d29a2010-10-18 12:02:01 -0300648 buf = kzalloc(ir->chunk_size, GFP_KERNEL);
649 if (!buf)
650 return -ENOMEM;
651
Dan Carpenter250f7a52010-11-17 02:20:15 -0300652 if (mutex_lock_interruptible(&ir->irctl_lock)) {
653 ret = -ERESTARTSYS;
654 goto out_unlocked;
655 }
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300656 if (!ir->attached) {
Dan Carpenter250f7a52010-11-17 02:20:15 -0300657 ret = -ENODEV;
658 goto out_locked;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300659 }
660
661 if (length % ir->chunk_size) {
Dan Carpenter250f7a52010-11-17 02:20:15 -0300662 ret = -EINVAL;
663 goto out_locked;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300664 }
665
666 /*
667 * we add ourselves to the task queue before buffer check
668 * to avoid losing scan code (in case when queue is awaken somewhere
669 * between while condition checking and scheduling)
670 */
671 add_wait_queue(&ir->buf->wait_poll, &wait);
672 set_current_state(TASK_INTERRUPTIBLE);
673
674 /*
675 * while we didn't provide 'length' bytes, device is opened in blocking
676 * mode and 'copy_to_user' is happy, wait for data.
677 */
678 while (written < length && ret == 0) {
679 if (lirc_buffer_empty(ir->buf)) {
680 /* According to the read(2) man page, 'written' can be
681 * returned as less than 'length', instead of blocking
682 * again, returning -EWOULDBLOCK, or returning
683 * -ERESTARTSYS */
684 if (written)
685 break;
686 if (file->f_flags & O_NONBLOCK) {
687 ret = -EWOULDBLOCK;
688 break;
689 }
690 if (signal_pending(current)) {
691 ret = -ERESTARTSYS;
692 break;
693 }
694
695 mutex_unlock(&ir->irctl_lock);
696 schedule();
697 set_current_state(TASK_INTERRUPTIBLE);
698
699 if (mutex_lock_interruptible(&ir->irctl_lock)) {
700 ret = -ERESTARTSYS;
Jarod Wilson69c271f2010-07-07 11:29:44 -0300701 remove_wait_queue(&ir->buf->wait_poll, &wait);
702 set_current_state(TASK_RUNNING);
703 goto out_unlocked;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300704 }
705
706 if (!ir->attached) {
707 ret = -ENODEV;
708 break;
709 }
710 } else {
711 lirc_buffer_read(ir->buf, buf);
712 ret = copy_to_user((void *)buffer+written, buf,
713 ir->buf->chunk_size);
Dan Carpenter250f7a52010-11-17 02:20:15 -0300714 if (!ret)
715 written += ir->buf->chunk_size;
716 else
717 ret = -EFAULT;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300718 }
719 }
720
721 remove_wait_queue(&ir->buf->wait_poll, &wait);
722 set_current_state(TASK_RUNNING);
Dan Carpenter250f7a52010-11-17 02:20:15 -0300723
724out_locked:
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300725 mutex_unlock(&ir->irctl_lock);
726
Jarod Wilson69c271f2010-07-07 11:29:44 -0300727out_unlocked:
Jarod Wilson715d29a2010-10-18 12:02:01 -0300728 kfree(buf);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300729 dev_dbg(ir->d.dev, LOGHEAD "read result = %s (%d)\n",
Dan Carpenter250f7a52010-11-17 02:20:15 -0300730 ir->d.name, ir->d.minor, ret ? "<fail>" : "<ok>", ret);
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{
738 void *data = NULL;
739
740 if (file && file->f_dentry && file->f_dentry->d_inode &&
741 file->f_dentry->d_inode->i_rdev) {
742 struct irctl *ir;
743 ir = irctls[iminor(file->f_dentry->d_inode)];
744 data = ir->d.data;
745 }
746
747 return data;
748}
749EXPORT_SYMBOL(lirc_get_pdata);
750
751
Dan Carpenter0e835082010-11-17 02:13:39 -0300752ssize_t lirc_dev_fop_write(struct file *file, const char __user *buffer,
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300753 size_t length, loff_t *ppos)
754{
755 struct irctl *ir = irctls[iminor(file->f_dentry->d_inode)];
756
Jarod Wilson715d29a2010-10-18 12:02:01 -0300757 if (!ir) {
758 printk(KERN_ERR "%s: called with invalid irctl\n", __func__);
759 return -ENODEV;
760 }
761
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300762 dev_dbg(ir->d.dev, LOGHEAD "write called\n", ir->d.name, ir->d.minor);
763
764 if (!ir->attached)
765 return -ENODEV;
766
767 return -EINVAL;
768}
769EXPORT_SYMBOL(lirc_dev_fop_write);
770
771
772static int __init lirc_dev_init(void)
773{
774 int retval;
775
776 lirc_class = class_create(THIS_MODULE, "lirc");
777 if (IS_ERR(lirc_class)) {
778 retval = PTR_ERR(lirc_class);
779 printk(KERN_ERR "lirc_dev: class_create failed\n");
780 goto error;
781 }
782
783 retval = alloc_chrdev_region(&lirc_base_dev, 0, MAX_IRCTL_DEVICES,
784 IRCTL_DEV_NAME);
785 if (retval) {
786 class_destroy(lirc_class);
787 printk(KERN_ERR "lirc_dev: alloc_chrdev_region failed\n");
788 goto error;
789 }
790
791
792 printk(KERN_INFO "lirc_dev: IR Remote Control driver registered, "
793 "major %d \n", MAJOR(lirc_base_dev));
794
795error:
796 return retval;
797}
798
799
800
801static void __exit lirc_dev_exit(void)
802{
803 class_destroy(lirc_class);
804 unregister_chrdev_region(lirc_base_dev, MAX_IRCTL_DEVICES);
805 printk(KERN_INFO "lirc_dev: module unloaded\n");
806}
807
808module_init(lirc_dev_init);
809module_exit(lirc_dev_exit);
810
811MODULE_DESCRIPTION("LIRC base driver module");
812MODULE_AUTHOR("Artur Lipowski");
813MODULE_LICENSE("GPL");
814
815module_param(debug, bool, S_IRUGO | S_IWUSR);
816MODULE_PARM_DESC(debug, "Enable debugging messages");