diff options
Diffstat (limited to 'drivers/media/common/ir-keytable.c')
| -rw-r--r-- | drivers/media/common/ir-keytable.c | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/drivers/media/common/ir-keytable.c b/drivers/media/common/ir-keytable.c index 65e352389b2d..6e3cc78c33b2 100644 --- a/drivers/media/common/ir-keytable.c +++ b/drivers/media/common/ir-keytable.c | |||
| @@ -7,6 +7,68 @@ | |||
| 7 | 7 | ||
| 8 | #include <media/ir-common.h> | 8 | #include <media/ir-common.h> |
| 9 | 9 | ||
| 10 | #define IR_TAB_MIN_SIZE 32 | ||
| 11 | |||
| 12 | /** | ||
| 13 | * ir_roundup_tablesize() - gets an optimum value for the table size | ||
| 14 | * @n_elems: minimum number of entries to store keycodes | ||
| 15 | * | ||
| 16 | * This routine is used to choose the keycode table size. | ||
| 17 | * | ||
| 18 | * In order to have some empty space for new keycodes, | ||
| 19 | * and knowing in advance that kmalloc allocates only power of two | ||
| 20 | * segments, it optimizes the allocated space to have some spare space | ||
| 21 | * for those new keycodes by using the maximum number of entries that | ||
| 22 | * will be effectively be allocated by kmalloc. | ||
| 23 | * In order to reduce the quantity of table resizes, it has a minimum | ||
| 24 | * table size of IR_TAB_MIN_SIZE. | ||
| 25 | */ | ||
| 26 | int ir_roundup_tablesize(int n_elems) | ||
| 27 | { | ||
| 28 | size_t size; | ||
| 29 | |||
| 30 | if (n_elems < IR_TAB_MIN_SIZE) | ||
| 31 | n_elems = IR_TAB_MIN_SIZE; | ||
| 32 | |||
| 33 | /* | ||
| 34 | * As kmalloc only allocates sizes of power of two, get as | ||
| 35 | * much entries as possible for the allocated memory segment | ||
| 36 | */ | ||
| 37 | size = roundup_pow_of_two(n_elems * sizeof(struct ir_scancode)); | ||
| 38 | n_elems = size / sizeof(struct ir_scancode); | ||
| 39 | |||
| 40 | return n_elems; | ||
| 41 | } | ||
| 42 | |||
| 43 | /** | ||
| 44 | * ir_copy_table() - copies a keytable, discarding the unused entries | ||
| 45 | * @destin: destin table | ||
| 46 | * @origin: origin table | ||
| 47 | * | ||
| 48 | * Copies all entries where the keycode is not KEY_UNKNOWN/KEY_RESERVED | ||
| 49 | */ | ||
| 50 | |||
| 51 | int ir_copy_table(struct ir_scancode_table *destin, | ||
| 52 | const struct ir_scancode_table *origin) | ||
| 53 | { | ||
| 54 | int i, j = 0; | ||
| 55 | |||
| 56 | for (i = 0; i < origin->size; i++) { | ||
| 57 | if (origin->scan[i].keycode != KEY_UNKNOWN && | ||
| 58 | origin->scan[i].keycode != KEY_RESERVED) { | ||
| 59 | memcpy(&destin->scan[j], &origin->scan[i], | ||
| 60 | sizeof(struct ir_scancode)); | ||
| 61 | j++; | ||
| 62 | } | ||
| 63 | } | ||
| 64 | destin->size = j; | ||
| 65 | |||
| 66 | IR_dprintk(1, "Copied %d scancodes to the new keycode table\n", j); | ||
| 67 | |||
| 68 | return 0; | ||
| 69 | } | ||
| 70 | |||
| 71 | |||
| 10 | /** | 72 | /** |
| 11 | * ir_getkeycode() - get a keycode at the evdev scancode ->keycode table | 73 | * ir_getkeycode() - get a keycode at the evdev scancode ->keycode table |
| 12 | * @dev: the struct input_dev device descriptor | 74 | * @dev: the struct input_dev device descriptor |
| @@ -152,3 +214,16 @@ int ir_set_keycode_table(struct input_dev *input_dev, | |||
| 152 | 214 | ||
| 153 | return 0; | 215 | return 0; |
| 154 | } | 216 | } |
| 217 | |||
| 218 | void ir_input_free(struct input_dev *dev) | ||
| 219 | { | ||
| 220 | struct ir_scancode_table *rc_tab = input_get_drvdata(dev); | ||
| 221 | |||
| 222 | IR_dprintk(1, "Freed keycode table\n"); | ||
| 223 | |||
| 224 | rc_tab->size = 0; | ||
| 225 | kfree(rc_tab->scan); | ||
| 226 | rc_tab->scan = NULL; | ||
| 227 | } | ||
| 228 | EXPORT_SYMBOL_GPL(ir_input_free); | ||
| 229 | |||
