linux/drivers/input/matrix-keymap.c
<<
>>
Prefs
   1/*
   2 * Helpers for matrix keyboard bindings
   3 *
   4 * Copyright (C) 2012 Google, Inc
   5 *
   6 * Author:
   7 *      Olof Johansson <olof@lixom.net>
   8 *
   9 * This software is licensed under the terms of the GNU General Public
  10 * License version 2, as published by the Free Software Foundation, and
  11 * may be copied, distributed, and modified under those terms.
  12 *
  13 * This program is distributed in the hope that it will be useful,
  14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16 * GNU General Public License for more details.
  17 *
  18 */
  19
  20#include <linux/device.h>
  21#include <linux/gfp.h>
  22#include <linux/kernel.h>
  23#include <linux/types.h>
  24#include <linux/input.h>
  25#include <linux/of.h>
  26#include <linux/export.h>
  27#include <linux/module.h>
  28#include <linux/input/matrix_keypad.h>
  29
  30static bool matrix_keypad_map_key(struct input_dev *input_dev,
  31                                  unsigned int rows, unsigned int cols,
  32                                  unsigned int row_shift, unsigned int key)
  33{
  34        unsigned short *keymap = input_dev->keycode;
  35        unsigned int row = KEY_ROW(key);
  36        unsigned int col = KEY_COL(key);
  37        unsigned short code = KEY_VAL(key);
  38
  39        if (row >= rows || col >= cols) {
  40                dev_err(input_dev->dev.parent,
  41                        "%s: invalid keymap entry 0x%x (row: %d, col: %d, rows: %d, cols: %d)\n",
  42                        __func__, key, row, col, rows, cols);
  43                return false;
  44        }
  45
  46        keymap[MATRIX_SCAN_CODE(row, col, row_shift)] = code;
  47        __set_bit(code, input_dev->keybit);
  48
  49        return true;
  50}
  51
  52#ifdef CONFIG_OF
  53static int matrix_keypad_parse_of_keymap(const char *propname,
  54                                         unsigned int rows, unsigned int cols,
  55                                         struct input_dev *input_dev)
  56{
  57        struct device *dev = input_dev->dev.parent;
  58        struct device_node *np = dev->of_node;
  59        unsigned int row_shift = get_count_order(cols);
  60        unsigned int max_keys = rows << row_shift;
  61        unsigned int proplen, i, size;
  62        const __be32 *prop;
  63
  64        if (!np)
  65                return -ENOENT;
  66
  67        if (!propname)
  68                propname = "linux,keymap";
  69
  70        prop = of_get_property(np, propname, &proplen);
  71        if (!prop) {
  72                dev_err(dev, "OF: %s property not defined in %s\n",
  73                        propname, np->full_name);
  74                return -ENOENT;
  75        }
  76
  77        if (proplen % sizeof(u32)) {
  78                dev_err(dev, "OF: Malformed keycode property %s in %s\n",
  79                        propname, np->full_name);
  80                return -EINVAL;
  81        }
  82
  83        size = proplen / sizeof(u32);
  84        if (size > max_keys) {
  85                dev_err(dev, "OF: %s size overflow\n", propname);
  86                return -EINVAL;
  87        }
  88
  89        for (i = 0; i < size; i++) {
  90                unsigned int key = be32_to_cpup(prop + i);
  91
  92                if (!matrix_keypad_map_key(input_dev, rows, cols,
  93                                           row_shift, key))
  94                        return -EINVAL;
  95        }
  96
  97        return 0;
  98}
  99#else
 100static int matrix_keypad_parse_of_keymap(const char *propname,
 101                                         unsigned int rows, unsigned int cols,
 102                                         struct input_dev *input_dev)
 103{
 104        return -ENOSYS;
 105}
 106#endif
 107
 108/**
 109 * matrix_keypad_build_keymap - convert platform keymap into matrix keymap
 110 * @keymap_data: keymap supplied by the platform code
 111 * @keymap_name: name of device tree property containing keymap (if device
 112 *      tree support is enabled).
 113 * @rows: number of rows in target keymap array
 114 * @cols: number of cols in target keymap array
 115 * @keymap: expanded version of keymap that is suitable for use by
 116 * matrix keyboard driver
 117 * @input_dev: input devices for which we are setting up the keymap
 118 *
 119 * This function converts platform keymap (encoded with KEY() macro) into
 120 * an array of keycodes that is suitable for using in a standard matrix
 121 * keyboard driver that uses row and col as indices.
 122 *
 123 * If @keymap_data is not supplied and device tree support is enabled
 124 * it will attempt load the keymap from property specified by @keymap_name
 125 * argument (or "linux,keymap" if @keymap_name is %NULL).
 126 *
 127 * If @keymap is %NULL the function will automatically allocate managed
 128 * block of memory to store the keymap. This memory will be associated with
 129 * the parent device and automatically freed when device unbinds from the
 130 * driver.
 131 *
 132 * Callers are expected to set up input_dev->dev.parent before calling this
 133 * function.
 134 */
 135int matrix_keypad_build_keymap(const struct matrix_keymap_data *keymap_data,
 136                               const char *keymap_name,
 137                               unsigned int rows, unsigned int cols,
 138                               unsigned short *keymap,
 139                               struct input_dev *input_dev)
 140{
 141        unsigned int row_shift = get_count_order(cols);
 142        size_t max_keys = rows << row_shift;
 143        int i;
 144        int error;
 145
 146        if (WARN_ON(!input_dev->dev.parent))
 147                return -EINVAL;
 148
 149        if (!keymap) {
 150                keymap = devm_kzalloc(input_dev->dev.parent,
 151                                      max_keys * sizeof(*keymap),
 152                                      GFP_KERNEL);
 153                if (!keymap) {
 154                        dev_err(input_dev->dev.parent,
 155                                "Unable to allocate memory for keymap");
 156                        return -ENOMEM;
 157                }
 158        }
 159
 160        input_dev->keycode = keymap;
 161        input_dev->keycodesize = sizeof(*keymap);
 162        input_dev->keycodemax = max_keys;
 163
 164        __set_bit(EV_KEY, input_dev->evbit);
 165
 166        if (keymap_data) {
 167                for (i = 0; i < keymap_data->keymap_size; i++) {
 168                        unsigned int key = keymap_data->keymap[i];
 169
 170                        if (!matrix_keypad_map_key(input_dev, rows, cols,
 171                                                   row_shift, key))
 172                                return -EINVAL;
 173                }
 174        } else {
 175                error = matrix_keypad_parse_of_keymap(keymap_name, rows, cols,
 176                                                      input_dev);
 177                if (error)
 178                        return error;
 179        }
 180
 181        __clear_bit(KEY_RESERVED, input_dev->keybit);
 182
 183        return 0;
 184}
 185EXPORT_SYMBOL(matrix_keypad_build_keymap);
 186
 187MODULE_LICENSE("GPL");
 188
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.