xref: /openbmc/linux/drivers/media/rc/lirc_dev.c (revision 2d96b44f)
1 /*
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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
23 
24 #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>
32 #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 
40 #include <media/rc-core.h>
41 #include <media/lirc.h>
42 #include <media/lirc_dev.h>
43 
44 static bool debug;
45 
46 #define IRCTL_DEV_NAME	"BaseRemoteCtl"
47 #define NOPLUG		-1
48 #define LOGHEAD		"lirc_dev (%s[%d]): "
49 
50 static dev_t lirc_base_dev;
51 
52 struct 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 
61 	struct cdev *cdev;
62 
63 	struct task_struct *task;
64 	long jiffies_to_wait;
65 };
66 
67 static DEFINE_MUTEX(lirc_dev_lock);
68 
69 static struct irctl *irctls[MAX_IRCTL_DEVICES];
70 
71 /* Only used for sysfs but defined to void otherwise */
72 static struct class *lirc_class;
73 
74 /*  helper function
75  *  initializes the irctl structure
76  */
77 static void lirc_irctl_init(struct irctl *ir)
78 {
79 	mutex_init(&ir->irctl_lock);
80 	ir->d.minor = NOPLUG;
81 }
82 
83 static void lirc_irctl_cleanup(struct irctl *ir)
84 {
85 	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  */
98 static int lirc_add_to_buf(struct irctl *ir)
99 {
100 	int res;
101 	int got_data = -1;
102 
103 	if (!ir->d.add_to_buf)
104 		return 0;
105 
106 	/*
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);
114 
115 	if (res == -ENODEV)
116 		kthread_stop(ir->task);
117 
118 	return got_data ? 0 : res;
119 }
120 
121 /* main function of the polling thread
122  */
123 static int lirc_thread(void *irctl)
124 {
125 	struct irctl *ir = irctl;
126 
127 	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;
135 			if (!lirc_add_to_buf(ir))
136 				wake_up_interruptible(&ir->buf->wait_poll);
137 		} else {
138 			set_current_state(TASK_INTERRUPTIBLE);
139 			schedule();
140 		}
141 	} while (!kthread_should_stop());
142 
143 	return 0;
144 }
145 
146 
147 static const struct file_operations lirc_dev_fops = {
148 	.owner		= THIS_MODULE,
149 	.read		= lirc_dev_fop_read,
150 	.write		= lirc_dev_fop_write,
151 	.poll		= lirc_dev_fop_poll,
152 	.unlocked_ioctl	= lirc_dev_fop_ioctl,
153 #ifdef CONFIG_COMPAT
154 	.compat_ioctl	= lirc_dev_fop_ioctl,
155 #endif
156 	.open		= lirc_dev_fop_open,
157 	.release	= lirc_dev_fop_close,
158 	.llseek		= noop_llseek,
159 };
160 
161 static int lirc_cdev_add(struct irctl *ir)
162 {
163 	int retval = -ENOMEM;
164 	struct lirc_driver *d = &ir->d;
165 	struct cdev *cdev;
166 
167 	cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
168 	if (!cdev)
169 		goto err_out;
170 
171 	if (d->fops) {
172 		cdev_init(cdev, d->fops);
173 		cdev->owner = d->owner;
174 	} else {
175 		cdev_init(cdev, &lirc_dev_fops);
176 		cdev->owner = THIS_MODULE;
177 	}
178 	retval = kobject_set_name(&cdev->kobj, "lirc%d", d->minor);
179 	if (retval)
180 		goto err_out;
181 
182 	retval = cdev_add(cdev, MKDEV(MAJOR(lirc_base_dev), d->minor), 1);
183 	if (retval) {
184 		kobject_put(&cdev->kobj);
185 		goto err_out;
186 	}
187 
188 	ir->cdev = cdev;
189 
190 	return 0;
191 
192 err_out:
193 	kfree(cdev);
194 	return retval;
195 }
196 
197 static int lirc_allocate_buffer(struct irctl *ir)
198 {
199 	int err = 0;
200 	int bytes_in_key;
201 	unsigned int chunk_size;
202 	unsigned int buffer_size;
203 	struct lirc_driver *d = &ir->d;
204 
205 	mutex_lock(&lirc_dev_lock);
206 
207 	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);
216 		if (!ir->buf) {
217 			err = -ENOMEM;
218 			goto out;
219 		}
220 
221 		err = lirc_buffer_init(ir->buf, chunk_size, buffer_size);
222 		if (err) {
223 			kfree(ir->buf);
224 			goto out;
225 		}
226 	}
227 	ir->chunk_size = ir->buf->chunk_size;
228 
229 out:
230 	mutex_unlock(&lirc_dev_lock);
231 
232 	return err;
233 }
234 
235 static int lirc_allocate_driver(struct lirc_driver *d)
236 {
237 	struct irctl *ir;
238 	int minor;
239 	int err;
240 
241 	if (!d) {
242 		pr_err("driver pointer must be not NULL!\n");
243 		return -EBADRQC;
244 	}
245 
246 	if (!d->dev) {
247 		pr_err("dev pointer not filled in!\n");
248 		return -EINVAL;
249 	}
250 
251 	if (d->minor >= MAX_IRCTL_DEVICES) {
252 		dev_err(d->dev, "minor must be between 0 and %d!\n",
253 						MAX_IRCTL_DEVICES - 1);
254 		return -EBADRQC;
255 	}
256 
257 	if (d->code_length < 1 || d->code_length > (BUFLEN * 8)) {
258 		dev_err(d->dev, "code length must be less than %d bits\n",
259 								BUFLEN * 8);
260 		return -EBADRQC;
261 	}
262 
263 	if (d->sample_rate) {
264 		if (2 > d->sample_rate || HZ < d->sample_rate) {
265 			dev_err(d->dev, "invalid %d sample rate\n",
266 							d->sample_rate);
267 			return -EBADRQC;
268 		}
269 		if (!d->add_to_buf) {
270 			dev_err(d->dev, "add_to_buf not set\n");
271 			return -EBADRQC;
272 		}
273 	} 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");
276 		return -EBADRQC;
277 	}
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;
288 		if (minor == MAX_IRCTL_DEVICES) {
289 			dev_err(d->dev, "no free slots for drivers!\n");
290 			err = -ENOMEM;
291 			goto out_lock;
292 		}
293 	} else if (irctls[minor]) {
294 		dev_err(d->dev, "minor (%d) just registered!\n", minor);
295 		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 	}
304 	lirc_irctl_init(ir);
305 	irctls[minor] = ir;
306 	d->minor = minor;
307 
308 	/* some safety check 8-) */
309 	d->name[sizeof(d->name)-1] = '\0';
310 
311 	if (d->features == 0)
312 		d->features = LIRC_CAN_REC_LIRCCODE;
313 
314 	ir->d = *d;
315 
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) {
321 		ir->jiffies_to_wait = HZ / d->sample_rate;
322 
323 		/* try to fire up polling thread */
324 		ir->task = kthread_run(lirc_thread, (void *)ir, "lirc_dev");
325 		if (IS_ERR(ir->task)) {
326 			dev_err(d->dev, "cannot run thread for minor = %d\n",
327 								d->minor);
328 			err = -ECHILD;
329 			goto out_sysfs;
330 		}
331 	} else {
332 		/* it means - wait for external event in task queue */
333 		ir->jiffies_to_wait = 0;
334 	}
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 
347 out_sysfs:
348 	device_destroy(lirc_class, MKDEV(MAJOR(lirc_base_dev), ir->d.minor));
349 out_lock:
350 	mutex_unlock(&lirc_dev_lock);
351 
352 	return err;
353 }
354 
355 int 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 }
371 EXPORT_SYMBOL(lirc_register_driver);
372 
373 int lirc_unregister_driver(int minor)
374 {
375 	struct irctl *ir;
376 	struct cdev *cdev;
377 
378 	if (minor < 0 || minor >= MAX_IRCTL_DEVICES) {
379 		pr_err("minor (%d) must be between 0 and %d!\n",
380 					minor, MAX_IRCTL_DEVICES - 1);
381 		return -EBADRQC;
382 	}
383 
384 	ir = irctls[minor];
385 	if (!ir) {
386 		pr_err("failed to get irctl\n");
387 		return -ENOENT;
388 	}
389 
390 	cdev = ir->cdev;
391 
392 	mutex_lock(&lirc_dev_lock);
393 
394 	if (ir->d.minor != minor) {
395 		dev_err(ir->d.dev, "lirc_dev: minor %d device not registered\n",
396 									minor);
397 		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 
415 		if (ir->d.set_use_dec)
416 			ir->d.set_use_dec(ir->d.data);
417 
418 		module_put(cdev->owner);
419 		mutex_unlock(&ir->irctl_lock);
420 	} else {
421 		lirc_irctl_cleanup(ir);
422 		cdev_del(cdev);
423 		kfree(cdev);
424 		kfree(ir);
425 		irctls[minor] = NULL;
426 	}
427 
428 	mutex_unlock(&lirc_dev_lock);
429 
430 	return 0;
431 }
432 EXPORT_SYMBOL(lirc_unregister_driver);
433 
434 int lirc_dev_fop_open(struct inode *inode, struct file *file)
435 {
436 	struct irctl *ir;
437 	struct cdev *cdev;
438 	int retval = 0;
439 
440 	if (iminor(inode) >= MAX_IRCTL_DEVICES) {
441 		pr_err("open result for %d is -ENODEV\n", iminor(inode));
442 		return -ENODEV;
443 	}
444 
445 	if (mutex_lock_interruptible(&lirc_dev_lock))
446 		return -ERESTARTSYS;
447 
448 	ir = irctls[iminor(inode)];
449 	if (!ir) {
450 		retval = -ENODEV;
451 		goto error;
452 	}
453 
454 	dev_dbg(ir->d.dev, LOGHEAD "open called\n", ir->d.name, ir->d.minor);
455 
456 	if (ir->d.minor == NOPLUG) {
457 		retval = -ENODEV;
458 		goto error;
459 	}
460 
461 	if (ir->open) {
462 		retval = -EBUSY;
463 		goto error;
464 	}
465 
466 	if (ir->d.rdev) {
467 		retval = rc_open(ir->d.rdev);
468 		if (retval)
469 			goto error;
470 	}
471 
472 	cdev = ir->cdev;
473 	if (try_module_get(cdev->owner)) {
474 		ir->open++;
475 		if (ir->d.set_use_inc)
476 			retval = ir->d.set_use_inc(ir->d.data);
477 
478 		if (retval) {
479 			module_put(cdev->owner);
480 			ir->open--;
481 		} else {
482 			lirc_buffer_clear(ir->buf);
483 		}
484 		if (ir->task)
485 			wake_up_process(ir->task);
486 	}
487 
488 error:
489 	mutex_unlock(&lirc_dev_lock);
490 
491 	nonseekable_open(inode, file);
492 
493 	return retval;
494 }
495 EXPORT_SYMBOL(lirc_dev_fop_open);
496 
497 int lirc_dev_fop_close(struct inode *inode, struct file *file)
498 {
499 	struct irctl *ir = irctls[iminor(inode)];
500 	struct cdev *cdev;
501 	int ret;
502 
503 	if (!ir) {
504 		pr_err("called with invalid irctl\n");
505 		return -EINVAL;
506 	}
507 
508 	cdev = ir->cdev;
509 
510 	ret = mutex_lock_killable(&lirc_dev_lock);
511 	WARN_ON(ret);
512 
513 	rc_close(ir->d.rdev);
514 
515 	ir->open--;
516 	if (ir->attached) {
517 		if (ir->d.set_use_dec)
518 			ir->d.set_use_dec(ir->d.data);
519 		module_put(cdev->owner);
520 	} else {
521 		lirc_irctl_cleanup(ir);
522 		cdev_del(cdev);
523 		irctls[ir->d.minor] = NULL;
524 		kfree(cdev);
525 		kfree(ir);
526 	}
527 
528 	if (!ret)
529 		mutex_unlock(&lirc_dev_lock);
530 
531 	return 0;
532 }
533 EXPORT_SYMBOL(lirc_dev_fop_close);
534 
535 unsigned int lirc_dev_fop_poll(struct file *file, poll_table *wait)
536 {
537 	struct irctl *ir = irctls[iminor(file_inode(file))];
538 	unsigned int ret;
539 
540 	if (!ir) {
541 		pr_err("called with invalid irctl\n");
542 		return POLLERR;
543 	}
544 
545 	if (!ir->attached)
546 		return POLLERR;
547 
548 	if (ir->buf) {
549 		poll_wait(file, &ir->buf->wait_poll, wait);
550 
551 		if (lirc_buffer_empty(ir->buf))
552 			ret = 0;
553 		else
554 			ret = POLLIN | POLLRDNORM;
555 	} else
556 		ret = POLLERR;
557 
558 	dev_dbg(ir->d.dev, LOGHEAD "poll result = %d\n",
559 		ir->d.name, ir->d.minor, ret);
560 
561 	return ret;
562 }
563 EXPORT_SYMBOL(lirc_dev_fop_poll);
564 
565 long lirc_dev_fop_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
566 {
567 	__u32 mode;
568 	int result = 0;
569 	struct irctl *ir = irctls[iminor(file_inode(file))];
570 
571 	if (!ir) {
572 		pr_err("no irctl found!\n");
573 		return -ENODEV;
574 	}
575 
576 	dev_dbg(ir->d.dev, LOGHEAD "ioctl called (0x%x)\n",
577 		ir->d.name, ir->d.minor, cmd);
578 
579 	if (ir->d.minor == NOPLUG || !ir->attached) {
580 		dev_err(ir->d.dev, LOGHEAD "ioctl result = -ENODEV\n",
581 			ir->d.name, ir->d.minor);
582 		return -ENODEV;
583 	}
584 
585 	mutex_lock(&ir->irctl_lock);
586 
587 	switch (cmd) {
588 	case LIRC_GET_FEATURES:
589 		result = put_user(ir->d.features, (__u32 __user *)arg);
590 		break;
591 	case LIRC_GET_REC_MODE:
592 		if (LIRC_CAN_REC(ir->d.features)) {
593 			result = -ENOTTY;
594 			break;
595 		}
596 
597 		result = put_user(LIRC_REC2MODE
598 				  (ir->d.features & LIRC_CAN_REC_MASK),
599 				  (__u32 __user *)arg);
600 		break;
601 	case LIRC_SET_REC_MODE:
602 		if (LIRC_CAN_REC(ir->d.features)) {
603 			result = -ENOTTY;
604 			break;
605 		}
606 
607 		result = get_user(mode, (__u32 __user *)arg);
608 		if (!result && !(LIRC_MODE2REC(mode) & ir->d.features))
609 			result = -EINVAL;
610 		/*
611 		 * FIXME: We should actually set the mode somehow but
612 		 * for now, lirc_serial doesn't support mode changing either
613 		 */
614 		break;
615 	case LIRC_GET_LENGTH:
616 		result = put_user(ir->d.code_length, (__u32 __user *)arg);
617 		break;
618 	case LIRC_GET_MIN_TIMEOUT:
619 		if (!(ir->d.features & LIRC_CAN_SET_REC_TIMEOUT) ||
620 		    ir->d.min_timeout == 0) {
621 			result = -ENOTTY;
622 			break;
623 		}
624 
625 		result = put_user(ir->d.min_timeout, (__u32 __user *)arg);
626 		break;
627 	case LIRC_GET_MAX_TIMEOUT:
628 		if (!(ir->d.features & LIRC_CAN_SET_REC_TIMEOUT) ||
629 		    ir->d.max_timeout == 0) {
630 			result = -ENOTTY;
631 			break;
632 		}
633 
634 		result = put_user(ir->d.max_timeout, (__u32 __user *)arg);
635 		break;
636 	default:
637 		result = -EINVAL;
638 	}
639 
640 	mutex_unlock(&ir->irctl_lock);
641 
642 	return result;
643 }
644 EXPORT_SYMBOL(lirc_dev_fop_ioctl);
645 
646 ssize_t lirc_dev_fop_read(struct file *file,
647 			  char __user *buffer,
648 			  size_t length,
649 			  loff_t *ppos)
650 {
651 	struct irctl *ir = irctls[iminor(file_inode(file))];
652 	unsigned char *buf;
653 	int ret = 0, written = 0;
654 	DECLARE_WAITQUEUE(wait, current);
655 
656 	if (!ir) {
657 		pr_err("called with invalid irctl\n");
658 		return -ENODEV;
659 	}
660 
661 	dev_dbg(ir->d.dev, LOGHEAD "read called\n", ir->d.name, ir->d.minor);
662 
663 	buf = kzalloc(ir->chunk_size, GFP_KERNEL);
664 	if (!buf)
665 		return -ENOMEM;
666 
667 	if (mutex_lock_interruptible(&ir->irctl_lock)) {
668 		ret = -ERESTARTSYS;
669 		goto out_unlocked;
670 	}
671 	if (!ir->attached) {
672 		ret = -ENODEV;
673 		goto out_locked;
674 	}
675 
676 	if (length % ir->chunk_size) {
677 		ret = -EINVAL;
678 		goto out_locked;
679 	}
680 
681 	/*
682 	 * we add ourselves to the task queue before buffer check
683 	 * to avoid losing scan code (in case when queue is awaken somewhere
684 	 * between while condition checking and scheduling)
685 	 */
686 	add_wait_queue(&ir->buf->wait_poll, &wait);
687 	set_current_state(TASK_INTERRUPTIBLE);
688 
689 	/*
690 	 * while we didn't provide 'length' bytes, device is opened in blocking
691 	 * mode and 'copy_to_user' is happy, wait for data.
692 	 */
693 	while (written < length && ret == 0) {
694 		if (lirc_buffer_empty(ir->buf)) {
695 			/* According to the read(2) man page, 'written' can be
696 			 * returned as less than 'length', instead of blocking
697 			 * again, returning -EWOULDBLOCK, or returning
698 			 * -ERESTARTSYS
699 			 */
700 			if (written)
701 				break;
702 			if (file->f_flags & O_NONBLOCK) {
703 				ret = -EWOULDBLOCK;
704 				break;
705 			}
706 			if (signal_pending(current)) {
707 				ret = -ERESTARTSYS;
708 				break;
709 			}
710 
711 			mutex_unlock(&ir->irctl_lock);
712 			schedule();
713 			set_current_state(TASK_INTERRUPTIBLE);
714 
715 			if (mutex_lock_interruptible(&ir->irctl_lock)) {
716 				ret = -ERESTARTSYS;
717 				remove_wait_queue(&ir->buf->wait_poll, &wait);
718 				set_current_state(TASK_RUNNING);
719 				goto out_unlocked;
720 			}
721 
722 			if (!ir->attached) {
723 				ret = -ENODEV;
724 				break;
725 			}
726 		} else {
727 			lirc_buffer_read(ir->buf, buf);
728 			ret = copy_to_user((void __user *)buffer+written, buf,
729 					   ir->buf->chunk_size);
730 			if (!ret)
731 				written += ir->buf->chunk_size;
732 			else
733 				ret = -EFAULT;
734 		}
735 	}
736 
737 	remove_wait_queue(&ir->buf->wait_poll, &wait);
738 	set_current_state(TASK_RUNNING);
739 
740 out_locked:
741 	mutex_unlock(&ir->irctl_lock);
742 
743 out_unlocked:
744 	kfree(buf);
745 
746 	return ret ? ret : written;
747 }
748 EXPORT_SYMBOL(lirc_dev_fop_read);
749 
750 void *lirc_get_pdata(struct file *file)
751 {
752 	return irctls[iminor(file_inode(file))]->d.data;
753 }
754 EXPORT_SYMBOL(lirc_get_pdata);
755 
756 
757 ssize_t lirc_dev_fop_write(struct file *file, const char __user *buffer,
758 			   size_t length, loff_t *ppos)
759 {
760 	struct irctl *ir = irctls[iminor(file_inode(file))];
761 
762 	if (!ir) {
763 		pr_err("called with invalid irctl\n");
764 		return -ENODEV;
765 	}
766 
767 	if (!ir->attached)
768 		return -ENODEV;
769 
770 	return -EINVAL;
771 }
772 EXPORT_SYMBOL(lirc_dev_fop_write);
773 
774 
775 static int __init lirc_dev_init(void)
776 {
777 	int retval;
778 
779 	lirc_class = class_create(THIS_MODULE, "lirc");
780 	if (IS_ERR(lirc_class)) {
781 		pr_err("class_create failed\n");
782 		return PTR_ERR(lirc_class);
783 	}
784 
785 	retval = alloc_chrdev_region(&lirc_base_dev, 0, MAX_IRCTL_DEVICES,
786 				     IRCTL_DEV_NAME);
787 	if (retval) {
788 		class_destroy(lirc_class);
789 		pr_err("alloc_chrdev_region failed\n");
790 		return retval;
791 	}
792 
793 
794 	pr_info("IR Remote Control driver registered, major %d\n",
795 						MAJOR(lirc_base_dev));
796 
797 	return 0;
798 }
799 
800 
801 
802 static void __exit lirc_dev_exit(void)
803 {
804 	class_destroy(lirc_class);
805 	unregister_chrdev_region(lirc_base_dev, MAX_IRCTL_DEVICES);
806 	pr_info("module unloaded\n");
807 }
808 
809 module_init(lirc_dev_init);
810 module_exit(lirc_dev_exit);
811 
812 MODULE_DESCRIPTION("LIRC base driver module");
813 MODULE_AUTHOR("Artur Lipowski");
814 MODULE_LICENSE("GPL");
815 
816 module_param(debug, bool, S_IRUGO | S_IWUSR);
817 MODULE_PARM_DESC(debug, "Enable debugging messages");
818