Mauro Carvalho Chehab | ef53a11 | 2009-11-27 22:01:23 -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 | |
| 6 | #include <linux/usb/input.h> |
| 7 | |
| 8 | #include <media/ir-common.h> |
| 9 | |
Mauro Carvalho Chehab | f6fc504 | 2009-11-29 11:08:02 -0300 | [diff] [blame^] | 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 | |
Mauro Carvalho Chehab | ef53a11 | 2009-11-27 22:01:23 -0300 | [diff] [blame] | 72 | /** |
| 73 | * ir_getkeycode() - get a keycode at the evdev scancode ->keycode table |
| 74 | * @dev: the struct input_dev device descriptor |
| 75 | * @scancode: the desired scancode |
| 76 | * @keycode: the keycode to be retorned. |
| 77 | * |
| 78 | * This routine is used to handle evdev EVIOCGKEY ioctl. |
| 79 | * If the key is not found, returns -EINVAL, otherwise, returns 0. |
| 80 | */ |
| 81 | static int ir_getkeycode(struct input_dev *dev, |
| 82 | int scancode, int *keycode) |
| 83 | { |
| 84 | int i; |
| 85 | struct ir_scancode_table *rc_tab = input_get_drvdata(dev); |
| 86 | struct ir_scancode *keymap = rc_tab->scan; |
| 87 | |
| 88 | /* See if we can match the raw key code. */ |
| 89 | for (i = 0; i < rc_tab->size; i++) |
| 90 | if (keymap[i].scancode == scancode) { |
| 91 | *keycode = keymap[i].keycode; |
| 92 | return 0; |
| 93 | } |
| 94 | |
| 95 | /* |
| 96 | * If is there extra space, returns KEY_RESERVED, |
| 97 | * otherwise, input core won't let ir_setkeycode |
| 98 | * to work |
| 99 | */ |
| 100 | for (i = 0; i < rc_tab->size; i++) |
| 101 | if (keymap[i].keycode == KEY_RESERVED || |
| 102 | keymap[i].keycode == KEY_UNKNOWN) { |
| 103 | *keycode = KEY_RESERVED; |
| 104 | return 0; |
| 105 | } |
| 106 | |
| 107 | return -EINVAL; |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * ir_setkeycode() - set a keycode at the evdev scancode ->keycode table |
| 112 | * @dev: the struct input_dev device descriptor |
| 113 | * @scancode: the desired scancode |
| 114 | * @keycode: the keycode to be retorned. |
| 115 | * |
| 116 | * This routine is used to handle evdev EVIOCSKEY ioctl. |
| 117 | * There's one caveat here: how can we increase the size of the table? |
| 118 | * If the key is not found, returns -EINVAL, otherwise, returns 0. |
| 119 | */ |
| 120 | static int ir_setkeycode(struct input_dev *dev, |
| 121 | int scancode, int keycode) |
| 122 | { |
| 123 | int i; |
| 124 | struct ir_scancode_table *rc_tab = input_get_drvdata(dev); |
| 125 | struct ir_scancode *keymap = rc_tab->scan; |
| 126 | |
| 127 | /* Search if it is replacing an existing keycode */ |
| 128 | for (i = 0; i < rc_tab->size; i++) |
| 129 | if (keymap[i].scancode == scancode) { |
| 130 | keymap[i].keycode = keycode; |
| 131 | return 0; |
| 132 | } |
| 133 | |
| 134 | /* Search if is there a clean entry. If so, use it */ |
| 135 | for (i = 0; i < rc_tab->size; i++) |
| 136 | if (keymap[i].keycode == KEY_RESERVED || |
| 137 | keymap[i].keycode == KEY_UNKNOWN) { |
| 138 | keymap[i].scancode = scancode; |
| 139 | keymap[i].keycode = keycode; |
| 140 | return 0; |
| 141 | } |
| 142 | |
| 143 | /* |
| 144 | * FIXME: Currently, it is not possible to increase the size of |
| 145 | * scancode table. For it to happen, one possibility |
| 146 | * would be to allocate a table with key_map_size + 1, |
| 147 | * copying data, appending the new key on it, and freeing |
| 148 | * the old one - or maybe just allocating some spare space |
| 149 | */ |
| 150 | |
| 151 | return -EINVAL; |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * ir_g_keycode_from_table() - gets the keycode that corresponds to a scancode |
| 156 | * @rc_tab: the ir_scancode_table with the keymap to be used |
| 157 | * @scancode: the scancode that we're seeking |
| 158 | * |
| 159 | * This routine is used by the input routines when a key is pressed at the |
| 160 | * IR. The scancode is received and needs to be converted into a keycode. |
| 161 | * If the key is not found, it returns KEY_UNKNOWN. Otherwise, returns the |
| 162 | * corresponding keycode from the table. |
| 163 | */ |
| 164 | u32 ir_g_keycode_from_table(struct input_dev *dev, u32 scancode) |
| 165 | { |
| 166 | int i; |
| 167 | struct ir_scancode_table *rc_tab = input_get_drvdata(dev); |
| 168 | struct ir_scancode *keymap = rc_tab->scan; |
| 169 | |
| 170 | for (i = 0; i < rc_tab->size; i++) |
| 171 | if (keymap[i].scancode == scancode) { |
| 172 | IR_dprintk(1, "%s: scancode 0x%04x keycode 0x%02x\n", |
| 173 | dev->name, scancode, keymap[i].keycode); |
| 174 | |
| 175 | return keymap[i].keycode; |
| 176 | } |
| 177 | |
| 178 | printk(KERN_INFO "%s: unknown key for scancode 0x%04x\n", |
| 179 | dev->name, scancode); |
| 180 | |
| 181 | return KEY_UNKNOWN; |
| 182 | } |
Mauro Carvalho Chehab | ef53a11 | 2009-11-27 22:01:23 -0300 | [diff] [blame] | 183 | |
| 184 | /** |
| 185 | * ir_set_keycode_table() - sets the IR keycode table and add the handlers |
| 186 | * for keymap table get/set |
| 187 | * @input_dev: the struct input_dev descriptor of the device |
| 188 | * @rc_tab: the struct ir_scancode_table table of scancode/keymap |
| 189 | * |
| 190 | * This routine is used to initialize the input infrastructure to work with |
| 191 | * an IR. It requires that the caller initializes the input_dev struct with |
| 192 | * some fields: name, |
| 193 | */ |
| 194 | int ir_set_keycode_table(struct input_dev *input_dev, |
| 195 | struct ir_scancode_table *rc_tab) |
| 196 | { |
| 197 | struct ir_scancode *keymap = rc_tab->scan; |
| 198 | int i; |
| 199 | |
| 200 | if (rc_tab->scan == NULL || !rc_tab->size) |
| 201 | return -EINVAL; |
| 202 | |
| 203 | /* set the bits for the keys */ |
| 204 | IR_dprintk(1, "key map size: %d\n", rc_tab->size); |
| 205 | for (i = 0; i < rc_tab->size; i++) { |
| 206 | IR_dprintk(1, "#%d: setting bit for keycode 0x%04x\n", |
| 207 | i, keymap[i].keycode); |
| 208 | set_bit(keymap[i].keycode, input_dev->keybit); |
| 209 | } |
| 210 | |
| 211 | input_dev->getkeycode = ir_getkeycode; |
| 212 | input_dev->setkeycode = ir_setkeycode; |
| 213 | input_set_drvdata(input_dev, rc_tab); |
| 214 | |
| 215 | return 0; |
| 216 | } |
Mauro Carvalho Chehab | f6fc504 | 2009-11-29 11:08:02 -0300 | [diff] [blame^] | 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 | |