Mauro Carvalho Chehab | 4714eda | 2009-12-13 16:00:08 -0300 | [diff] [blame^] | 1 | /* ir-register.c - handle IR scancode->keycode tables |
| 2 | * |
| 3 | * Copyright (C) 2009 by Mauro Carvalho Chehab <mchehab@redhat.com> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation version 2 of the License. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | */ |
| 14 | |
| 15 | #include <linux/input.h> |
| 16 | #include <linux/device.h> |
| 17 | #include <media/ir-core.h> |
| 18 | |
| 19 | #define IRRCV_NUM_DEVICES 256 |
| 20 | |
| 21 | unsigned long ir_core_dev_number; |
| 22 | |
| 23 | static struct class *ir_input_class; |
| 24 | |
| 25 | static DEVICE_ATTR(ir_protocol, S_IRUGO | S_IWUSR, NULL, NULL); |
| 26 | |
| 27 | static struct attribute *ir_dev_attrs[] = { |
| 28 | &dev_attr_ir_protocol.attr, |
| 29 | }; |
| 30 | |
| 31 | int ir_register_class(struct input_dev *input_dev) |
| 32 | { |
| 33 | int rc; |
| 34 | struct kobject *kobj; |
| 35 | |
| 36 | struct ir_input_dev *ir_dev = input_get_drvdata(input_dev); |
| 37 | int devno = find_first_zero_bit(&ir_core_dev_number, |
| 38 | IRRCV_NUM_DEVICES); |
| 39 | |
| 40 | if (unlikely(devno < 0)) |
| 41 | return devno; |
| 42 | |
| 43 | ir_dev->attr.attrs = ir_dev_attrs; |
| 44 | ir_dev->class_dev = device_create(ir_input_class, NULL, |
| 45 | input_dev->dev.devt, ir_dev, |
| 46 | "irrcv%d", devno); |
| 47 | kobj = &ir_dev->class_dev->kobj; |
| 48 | |
| 49 | printk(KERN_WARNING "Creating IR device %s\n", kobject_name(kobj)); |
| 50 | rc = sysfs_create_group(kobj, &ir_dev->attr); |
| 51 | if (unlikely(rc < 0)) { |
| 52 | device_destroy(ir_input_class, input_dev->dev.devt); |
| 53 | return -ENOMEM; |
| 54 | } |
| 55 | |
| 56 | ir_dev->devno = devno; |
| 57 | set_bit(devno, &ir_core_dev_number); |
| 58 | |
| 59 | return 0; |
| 60 | }; |
| 61 | |
| 62 | void ir_unregister_class(struct input_dev *input_dev) |
| 63 | { |
| 64 | struct ir_input_dev *ir_dev = input_get_drvdata(input_dev); |
| 65 | struct kobject *kobj; |
| 66 | |
| 67 | clear_bit(ir_dev->devno, &ir_core_dev_number); |
| 68 | |
| 69 | kobj = &ir_dev->class_dev->kobj; |
| 70 | |
| 71 | sysfs_remove_group(kobj, &ir_dev->attr); |
| 72 | device_destroy(ir_input_class, input_dev->dev.devt); |
| 73 | |
| 74 | kfree(ir_dev->attr.name); |
| 75 | } |
| 76 | |
| 77 | static int __init ir_core_init(void) |
| 78 | { |
| 79 | ir_input_class = class_create(THIS_MODULE, "irrcv"); |
| 80 | if (IS_ERR(ir_input_class)) { |
| 81 | printk(KERN_ERR "ir_core: unable to register irrcv class\n"); |
| 82 | return PTR_ERR(ir_input_class); |
| 83 | } |
| 84 | |
| 85 | return 0; |
| 86 | } |
| 87 | |
| 88 | static void __exit ir_core_exit(void) |
| 89 | { |
| 90 | class_destroy(ir_input_class); |
| 91 | } |
| 92 | |
| 93 | module_init(ir_core_init); |
| 94 | module_exit(ir_core_exit); |