forked from projectara/greybus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vibrator.c
186 lines (156 loc) · 4.21 KB
/
vibrator.c
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
/*
* Greybus Vibrator protocol driver.
*
* Copyright 2014 Google Inc.
* Copyright 2014 Linaro Ltd.
*
* Released under the GPLv2 only.
*/
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/device.h>
#include <linux/kdev_t.h>
#include <linux/idr.h>
#include "greybus.h"
struct gb_vibrator_device {
struct gb_connection *connection;
struct device *dev;
int minor; /* vibrator minor number */
};
/* Version of the Greybus vibrator protocol we support */
#define GB_VIBRATOR_VERSION_MAJOR 0x00
#define GB_VIBRATOR_VERSION_MINOR 0x01
/* Greybus Vibrator operation types */
#define GB_VIBRATOR_TYPE_ON 0x02
#define GB_VIBRATOR_TYPE_OFF 0x03
struct gb_vibrator_on_request {
__le16 timeout_ms;
};
static int turn_on(struct gb_vibrator_device *vib, u16 timeout_ms)
{
struct gb_vibrator_on_request request;
request.timeout_ms = cpu_to_le16(timeout_ms);
return gb_operation_sync(vib->connection, GB_VIBRATOR_TYPE_ON,
&request, sizeof(request), NULL, 0);
}
static int turn_off(struct gb_vibrator_device *vib)
{
return gb_operation_sync(vib->connection, GB_VIBRATOR_TYPE_OFF,
NULL, 0, NULL, 0);
}
static ssize_t timeout_store(struct device *dev, struct device_attribute *attr,
const char *buf, size_t count)
{
struct gb_vibrator_device *vib = dev_get_drvdata(dev);
unsigned long val;
int retval;
retval = kstrtoul(buf, 10, &val);
if (retval < 0) {
dev_err(dev, "could not parse timeout value %d\n", retval);
return retval;
}
if (val)
retval = turn_on(vib, (u16)val);
else
retval = turn_off(vib);
if (retval)
return retval;
return count;
}
static DEVICE_ATTR_WO(timeout);
static struct attribute *vibrator_attrs[] = {
&dev_attr_timeout.attr,
NULL,
};
ATTRIBUTE_GROUPS(vibrator);
static struct class vibrator_class = {
.name = "vibrator",
.owner = THIS_MODULE,
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,11,0)
.dev_groups = vibrator_groups,
#endif
};
static DEFINE_IDA(minors);
static int gb_vibrator_connection_init(struct gb_connection *connection)
{
struct gb_vibrator_device *vib;
struct device *dev;
int retval;
vib = kzalloc(sizeof(*vib), GFP_KERNEL);
if (!vib)
return -ENOMEM;
vib->connection = connection;
connection->private = vib;
/*
* For now we create a device in sysfs for the vibrator, but odds are
* there is a "real" device somewhere in the kernel for this, but I
* can't find it at the moment...
*/
vib->minor = ida_simple_get(&minors, 0, 0, GFP_KERNEL);
if (vib->minor < 0) {
retval = vib->minor;
goto error;
}
dev = device_create(&vibrator_class, &connection->dev, MKDEV(0, 0), vib,
"vibrator%d", vib->minor);
if (IS_ERR(dev)) {
retval = -EINVAL;
goto err_ida_remove;
}
vib->dev = dev;
#if LINUX_VERSION_CODE <= KERNEL_VERSION(3,11,0)
/*
* Newer kernels handle this in a race-free manner, by the dev_groups
* field in the struct class up above. But for older kernels, we need
* to "open code this :(
*/
retval = sysfs_create_group(&dev->kobj, vibrator_groups[0]);
if (retval) {
device_unregister(dev);
goto err_ida_remove;
}
#endif
return 0;
err_ida_remove:
ida_simple_remove(&minors, vib->minor);
error:
kfree(vib);
return retval;
}
static void gb_vibrator_connection_exit(struct gb_connection *connection)
{
struct gb_vibrator_device *vib = connection->private;
#if LINUX_VERSION_CODE <= KERNEL_VERSION(3,11,0)
sysfs_remove_group(&vib->dev->kobj, vibrator_groups[0]);
#endif
device_unregister(vib->dev);
ida_simple_remove(&minors, vib->minor);
kfree(vib);
}
static struct gb_protocol vibrator_protocol = {
.name = "vibrator",
.id = GREYBUS_PROTOCOL_VIBRATOR,
.major = GB_VIBRATOR_VERSION_MAJOR,
.minor = GB_VIBRATOR_VERSION_MINOR,
.connection_init = gb_vibrator_connection_init,
.connection_exit = gb_vibrator_connection_exit,
.request_recv = NULL, /* no incoming requests */
};
static __init int protocol_init(void)
{
int retval;
retval = class_register(&vibrator_class);
if (retval)
return retval;
return gb_protocol_register(&vibrator_protocol);
}
module_init(protocol_init);
static __exit void protocol_exit(void)
{
gb_protocol_deregister(&vibrator_protocol);
class_unregister(&vibrator_class);
ida_destroy(&minors);
}
module_exit(protocol_exit);
MODULE_LICENSE("GPL v2");