1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
|
/* Copyright (c) 2013, The Linux Foundation. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 and
* only version 2 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/of.h>
#include <linux/input.h>
#include <linux/input/gen_vkeys.h>
#define MAX_BUF_SIZE 256
#define VKEY_VER_CODE "0x01"
#define HEIGHT_SCALE_NUM 8
#define HEIGHT_SCALE_DENOM 10
#define VKEY_Y_OFFSET_DEFAULT 0
/* numerator and denomenator for border equations */
#define BORDER_ADJUST_NUM 3
#define BORDER_ADJUST_DENOM 4
static struct kobject *vkey_obj;
static char *vkey_buf;
static ssize_t vkey_show(struct kobject *obj,
struct kobj_attribute *attr, char *buf)
{
strlcpy(buf, vkey_buf, MAX_BUF_SIZE);
return strnlen(buf, MAX_BUF_SIZE);
}
static struct kobj_attribute vkey_obj_attr = {
.attr = {
.mode = S_IRUGO,
},
.show = vkey_show,
};
static struct attribute *vkey_attr[] = {
&vkey_obj_attr.attr,
NULL,
};
static struct attribute_group vkey_grp = {
.attrs = vkey_attr,
};
static int vkey_parse_dt(struct device *dev,
struct vkeys_platform_data *pdata)
{
struct device_node *np = dev->of_node;
struct property *prop;
int rc, val;
rc = of_property_read_string(np, "label", &pdata->name);
if (rc) {
dev_err(dev, "Failed to read label\n");
return -EINVAL;
}
rc = of_property_read_u32(np, "qcom,disp-maxx", &pdata->disp_maxx);
if (rc) {
dev_err(dev, "Failed to read display max x\n");
return -EINVAL;
}
rc = of_property_read_u32(np, "qcom,disp-maxy", &pdata->disp_maxy);
if (rc) {
dev_err(dev, "Failed to read display max y\n");
return -EINVAL;
}
rc = of_property_read_u32(np, "qcom,panel-maxx", &pdata->panel_maxx);
if (rc) {
dev_err(dev, "Failed to read panel max x\n");
return -EINVAL;
}
rc = of_property_read_u32(np, "qcom,panel-maxy", &pdata->panel_maxy);
if (rc) {
dev_err(dev, "Failed to read panel max y\n");
return -EINVAL;
}
prop = of_find_property(np, "qcom,key-codes", NULL);
if (prop) {
pdata->num_keys = prop->length / sizeof(u32);
pdata->keycodes = devm_kzalloc(dev,
sizeof(u32) * pdata->num_keys, GFP_KERNEL);
if (!pdata->keycodes)
return -ENOMEM;
rc = of_property_read_u32_array(np, "qcom,key-codes",
pdata->keycodes, pdata->num_keys);
if (rc) {
dev_err(dev, "Failed to read key codes\n");
return -EINVAL;
}
}
pdata->y_offset = VKEY_Y_OFFSET_DEFAULT;
rc = of_property_read_u32(np, "qcom,y-offset", &val);
if (!rc)
pdata->y_offset = val;
else if (rc != -EINVAL) {
dev_err(dev, "Failed to read y position offset\n");
return rc;
}
return 0;
}
static int vkeys_probe(struct platform_device *pdev)
{
struct vkeys_platform_data *pdata;
int width, height, center_x, center_y;
int x1 = 0, x2 = 0, i, c = 0, ret, border;
char *name;
vkey_buf = devm_kzalloc(&pdev->dev, MAX_BUF_SIZE, GFP_KERNEL);
if (!vkey_buf) {
dev_err(&pdev->dev, "Failed to allocate memory\n");
return -ENOMEM;
}
if (pdev->dev.of_node) {
pdata = devm_kzalloc(&pdev->dev,
sizeof(struct vkeys_platform_data), GFP_KERNEL);
if (!pdata) {
dev_err(&pdev->dev, "Failed to allocate memory\n");
return -ENOMEM;
}
ret = vkey_parse_dt(&pdev->dev, pdata);
if (ret) {
dev_err(&pdev->dev, "Parsing DT failed(%d)", ret);
return ret;
}
} else
pdata = pdev->dev.platform_data;
if (!pdata || !pdata->name || !pdata->keycodes || !pdata->num_keys ||
!pdata->disp_maxx || !pdata->disp_maxy || !pdata->panel_maxy) {
dev_err(&pdev->dev, "pdata is invalid\n");
return -EINVAL;
}
border = (pdata->panel_maxx - pdata->disp_maxx) * 2;
width = ((pdata->disp_maxx - (border * (pdata->num_keys - 1)))
/ pdata->num_keys);
height = (pdata->panel_maxy - pdata->disp_maxy);
center_y = pdata->disp_maxy + (height / 2) + pdata->y_offset;
height = height * HEIGHT_SCALE_NUM / HEIGHT_SCALE_DENOM;
x2 -= border * BORDER_ADJUST_NUM / BORDER_ADJUST_DENOM;
for (i = 0; i < pdata->num_keys; i++) {
x1 = x2 + border;
x2 = x2 + border + width;
center_x = x1 + (x2 - x1) / 2;
c += snprintf(vkey_buf + c, MAX_BUF_SIZE - c,
"%s:%d:%d:%d:%d:%d\n",
VKEY_VER_CODE, pdata->keycodes[i],
center_x, center_y, width, height);
}
vkey_buf[c] = '\0';
name = devm_kzalloc(&pdev->dev, sizeof(*name) * MAX_BUF_SIZE,
GFP_KERNEL);
if (!name)
return -ENOMEM;
snprintf(name, MAX_BUF_SIZE,
"virtualkeys.%s", pdata->name);
vkey_obj_attr.attr.name = name;
vkey_obj = kobject_create_and_add("board_properties", NULL);
if (!vkey_obj) {
dev_err(&pdev->dev, "unable to create kobject\n");
return -ENOMEM;
}
ret = sysfs_create_group(vkey_obj, &vkey_grp);
if (ret) {
dev_err(&pdev->dev, "failed to create attributes\n");
goto destroy_kobj;
}
return 0;
destroy_kobj:
kobject_put(vkey_obj);
return ret;
}
static int vkeys_remove(struct platform_device *pdev)
{
sysfs_remove_group(vkey_obj, &vkey_grp);
kobject_put(vkey_obj);
return 0;
}
static struct of_device_id vkey_match_table[] = {
{ .compatible = "qcom,gen-vkeys",},
{ },
};
static struct platform_driver vkeys_driver = {
.probe = vkeys_probe,
.remove = vkeys_remove,
.driver = {
.owner = THIS_MODULE,
.name = "gen_vkeys",
.of_match_table = vkey_match_table,
},
};
module_platform_driver(vkeys_driver);
MODULE_LICENSE("GPL v2");
|