22 #include <linux/module.h>
23 #include <linux/kernel.h>
24 #include <linux/sched.h>
25 #include <linux/errno.h>
28 #include <linux/poll.h>
31 #include <linux/wait.h>
34 #include <linux/bitops.h>
35 #include <linux/device.h>
43 #define IRCTL_DEV_NAME "BaseRemoteCtl"
45 #define LOGHEAD "lirc_dev (%s[%d]): "
47 static dev_t lirc_base_dev;
69 static struct class *lirc_class;
74 static void lirc_irctl_init(
struct irctl *
ir)
80 static void lirc_irctl_cleanup(
struct irctl *
ir)
86 if (ir->
buf != ir->
d.rbuf) {
87 lirc_buffer_free(ir->
buf);
97 static int lirc_add_to_buf(
struct irctl *ir)
99 if (ir->
d.add_to_buf) {
108 res = ir->
d.add_to_buf(ir->
d.data, ir->
buf);
117 return got_data ? 0 :
res;
125 static int lirc_thread(
void *
irctl)
127 struct irctl *ir = irctl;
130 ir->
d.name, ir->
d.minor);
140 if (!lirc_add_to_buf(ir))
149 ir->
d.name, ir->
d.minor);
169 static int lirc_cdev_add(
struct irctl *ir)
216 "driver pointer must be not NULL!\n");
227 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",
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",
244 dev_dbg(d->
dev,
"lirc_dev: lirc_register_driver: sample_rate: %d\n",
248 dev_err(d->
dev,
"lirc_dev: lirc_register_driver: "
249 "sample_rate must be between 2 and %d!\n",
HZ);
254 dev_err(d->
dev,
"lirc_dev: lirc_register_driver: "
255 "add_to_buf cannot be NULL when "
256 "sample_rate is set\n");
261 dev_err(d->
dev,
"lirc_dev: lirc_register_driver: "
262 "fops->read and rbuf cannot all be NULL!\n");
265 }
else if (!d->
rbuf) {
267 d->
fops->unlocked_ioctl)) {
268 dev_err(d->
dev,
"lirc_dev: lirc_register_driver: "
269 "neither read, poll nor unlocked_ioctl can be NULL!\n");
284 if (MAX_IRCTL_DEVICES == minor) {
285 dev_err(d->
dev,
"lirc_dev: lirc_register_driver: "
286 "no free slots for drivers!\n");
290 }
else if (irctls[minor]) {
291 dev_err(d->
dev,
"lirc_dev: lirc_register_driver: "
292 "minor (%d) just registered!\n", minor);
297 ir = kzalloc(
sizeof(
struct irctl),
GFP_KERNEL);
329 err = lirc_buffer_init(ir->
buf, chunk_size, buffer_size);
344 "lirc%u", ir->
d.minor);
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",
358 err = lirc_cdev_add(ir);
365 dev_info(ir->
d.dev,
"lirc_dev: driver %s registered at minor = %d\n",
366 ir->
d.name, ir->
d.minor);
392 "for minor %d!\n", __func__, minor);
400 if (ir->
d.minor != minor) {
402 "registered!\n", __func__, minor);
411 dev_dbg(ir->
d.dev,
"lirc_dev: driver %s unregistered from minor = %d\n",
412 ir->
d.name, ir->
d.minor);
417 ir->
d.name, ir->
d.minor);
420 ir->
d.set_use_dec(ir->
d.data);
421 module_put(cdev->
owner);
424 lirc_irctl_cleanup(ir);
428 irctls[minor] =
NULL;
452 ir = irctls[iminor(inode)];
471 if (try_module_get(cdev->
owner)) {
473 retval = ir->
d.set_use_inc(ir->
d.data);
476 module_put(cdev->
owner);
479 lirc_buffer_clear(ir->
buf);
488 ir->
d.name, ir->
d.minor, retval);
500 struct irctl *ir = irctls[iminor(inode)];
516 ir->
d.set_use_dec(ir->
d.data);
517 module_put(cdev->
owner);
519 lirc_irctl_cleanup(ir);
521 irctls[ir->
d.minor] =
NULL;
534 struct irctl *ir = irctls[iminor(file->f_dentry->d_inode)];
547 poll_wait(file, &ir->
buf->wait_poll, wait);
550 if (lirc_buffer_empty(ir->
buf))
558 ir->
d.name, ir->
d.minor, ret);
568 struct irctl *ir = irctls[iminor(file->f_dentry->d_inode)];
576 ir->
d.name, ir->
d.minor, cmd);
580 ir->
d.name, ir->
d.minor);
619 ir->
d.min_timeout == 0) {
628 ir->
d.max_timeout == 0) {
640 ir->
d.name, ir->
d.minor, result);
653 struct irctl *ir = irctls[iminor(file->f_dentry->d_inode)];
655 int ret = 0, written = 0;
695 while (written < length && ret == 0) {
696 if (lirc_buffer_empty(ir->
buf)) {
728 lirc_buffer_read(ir->
buf, buf);
730 ir->
buf->chunk_size);
732 written += ir->
buf->chunk_size;
747 ir->
d.name, ir->
d.minor, ret ?
"<fail>" :
"<ok>", ret);
749 return ret ? ret : written;
757 if (file && file->f_dentry && file->f_dentry->d_inode &&
758 file->f_dentry->d_inode->i_rdev) {
760 ir = irctls[iminor(file->f_dentry->d_inode)];
770 size_t length, loff_t *ppos)
772 struct irctl *ir = irctls[iminor(file->f_dentry->d_inode)];
789 static int __init lirc_dev_init(
void)
794 if (IS_ERR(lirc_class)) {
795 retval = PTR_ERR(lirc_class);
810 "major %d \n",
MAJOR(lirc_base_dev));
818 static void __exit lirc_dev_exit(
void)