forked from projectara/greybus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
raw.c
348 lines (284 loc) · 7.38 KB
/
raw.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
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/*
* Greybus driver for the Raw protocol
*
* Copyright 2015 Google Inc.
* Copyright 2015 Linaro Ltd.
*
* Released under the GPLv2 only.
*/
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/sizes.h>
#include <linux/cdev.h>
#include <linux/fs.h>
#include <linux/idr.h>
#include <linux/uaccess.h>
#include "greybus.h"
struct gb_raw {
struct gb_connection *connection;
struct list_head list;
int list_data;
struct mutex list_lock;
dev_t dev;
struct cdev cdev;
struct device *device;
};
struct raw_data {
struct list_head entry;
u32 len;
u8 data[0];
};
static struct class *raw_class;
static int raw_major;
static const struct file_operations raw_fops;
static DEFINE_IDA(minors);
/* Number of minor devices this driver supports */
#define NUM_MINORS 256
/* Maximum size of any one send data buffer we support */
#define MAX_PACKET_SIZE (PAGE_SIZE * 2)
/*
* Maximum size of the data in the receive buffer we allow before we start to
* drop messages on the floor
*/
#define MAX_DATA_SIZE (MAX_PACKET_SIZE * 8)
/*
* Add the raw data message to the list of received messages.
*/
static int receive_data(struct gb_raw *raw, u32 len, u8 *data)
{
struct raw_data *raw_data;
int retval = 0;
if (len > MAX_PACKET_SIZE) {
dev_err(raw->device, "Too big of a data packet, rejected\n");
return -EINVAL;
}
mutex_lock(&raw->list_lock);
if ((raw->list_data + len) > MAX_DATA_SIZE) {
dev_err(raw->device,
"Too much data in receive buffer, now dropping packets\n");
retval = -EINVAL;
goto exit;
}
raw_data = kmalloc(sizeof(*raw_data) + len, GFP_KERNEL);
if (!raw_data) {
retval = -ENOMEM;
goto exit;
}
raw->list_data += len;
raw_data->len = len;
memcpy(&raw_data->data[0], data, len);
list_add_tail(&raw_data->entry, &raw->list);
exit:
mutex_unlock(&raw->list_lock);
return retval;
}
static int gb_raw_receive(u8 type, struct gb_operation *op)
{
struct gb_connection *connection = op->connection;
struct gb_raw *raw = connection->private;
struct gb_raw_send_request *receive;
u32 len;
if (type != GB_RAW_TYPE_SEND) {
dev_err(raw->device, "unknown request type %d\n", type);
return -EINVAL;
}
/* Verify size of payload */
if (op->request->payload_size < sizeof(*receive)) {
dev_err(raw->device, "raw receive request too small (%zu < %zu)\n",
op->request->payload_size, sizeof(*receive));
return -EINVAL;
}
receive = op->request->payload;
len = le32_to_cpu(receive->len);
if (len != (int)(op->request->payload_size - sizeof(__le32))) {
dev_err(raw->device,
"raw receive request wrong size %d vs %d\n",
len,
(int)(op->request->payload_size - sizeof(__le32)));
return -EINVAL;
}
if (len == 0) {
dev_err(raw->device, "raw receive request of 0 bytes?\n");
return -EINVAL;
}
return receive_data(raw, len, receive->data);
}
static int gb_raw_send(struct gb_raw *raw, u32 len, const char __user *data)
{
struct gb_connection *connection = raw->connection;
struct gb_raw_send_request *request;
int retval;
request = kmalloc(len + sizeof(*request), GFP_KERNEL);
if (!request)
return -ENOMEM;
if (copy_from_user(&request->data[0], data, len)) {
kfree(request);
return -EFAULT;
}
request->len = cpu_to_le32(len);
retval = gb_operation_sync(connection, GB_RAW_TYPE_SEND,
request, len + sizeof(*request),
NULL, 0);
kfree(request);
return retval;
}
static int gb_raw_connection_init(struct gb_connection *connection)
{
struct gb_raw *raw;
int retval;
int minor;
raw = kzalloc(sizeof(*raw), GFP_KERNEL);
if (!raw)
return -ENOMEM;
raw->connection = connection;
connection->private = raw;
INIT_LIST_HEAD(&raw->list);
mutex_init(&raw->list_lock);
minor = ida_simple_get(&minors, 0, 0, GFP_KERNEL);
if (minor < 0) {
retval = minor;
goto error_free;
}
raw->dev = MKDEV(raw_major, minor);
cdev_init(&raw->cdev, &raw_fops);
retval = cdev_add(&raw->cdev, raw->dev, 1);
if (retval)
goto error_cdev;
raw->device = device_create(raw_class, &connection->dev, raw->dev, raw,
"gb!raw%d", minor);
if (IS_ERR(raw->device)) {
retval = PTR_ERR(raw->device);
goto error_device;
}
return 0;
error_device:
cdev_del(&raw->cdev);
error_cdev:
ida_simple_remove(&minors, minor);
error_free:
kfree(raw);
return retval;
}
static void gb_raw_connection_exit(struct gb_connection *connection)
{
struct gb_raw *raw = connection->private;
struct raw_data *raw_data;
struct raw_data *temp;
// FIXME - handle removing a connection when the char device node is open.
cdev_del(&raw->cdev);
ida_simple_remove(&minors, MINOR(raw->dev));
device_del(raw->device);
mutex_lock(&raw->list_lock);
list_for_each_entry_safe(raw_data, temp, &raw->list, entry) {
list_del(&raw_data->entry);
kfree(raw_data);
}
mutex_unlock(&raw->list_lock);
kfree(raw);
}
static struct gb_protocol raw_protocol = {
.name = "raw",
.id = GREYBUS_PROTOCOL_RAW,
.major = GB_RAW_VERSION_MAJOR,
.minor = GB_RAW_VERSION_MINOR,
.connection_init = gb_raw_connection_init,
.connection_exit = gb_raw_connection_exit,
.request_recv = gb_raw_receive,
};
/*
* Character device node interfaces.
*
* Note, we are using read/write to only allow a single read/write per message.
* This means for read(), you have to provide a big enough buffer for the full
* message to be copied into. If the buffer isn't big enough, the read() will
* fail with -ENOSPC.
*/
static int raw_open(struct inode *inode, struct file *file)
{
struct cdev *cdev = inode->i_cdev;
struct gb_raw *raw = container_of(cdev, struct gb_raw, cdev);
file->private_data = raw;
return 0;
}
static ssize_t raw_write(struct file *file, const char __user *buf,
size_t count, loff_t *ppos)
{
struct gb_raw *raw = file->private_data;
int retval;
if (!count)
return 0;
if (count > MAX_PACKET_SIZE)
return -E2BIG;
retval = gb_raw_send(raw, count, buf);
if (retval)
return retval;
return count;
}
static ssize_t raw_read(struct file *file, char __user *buf, size_t count,
loff_t *ppos)
{
struct gb_raw *raw = file->private_data;
int retval = 0;
struct raw_data *raw_data;
mutex_lock(&raw->list_lock);
if (list_empty(&raw->list))
goto exit;
raw_data = list_first_entry(&raw->list, struct raw_data, entry);
if (raw_data->len > count) {
retval = -ENOSPC;
goto exit;
}
if (copy_to_user(buf, &raw_data->data[0], raw_data->len)) {
retval = -EFAULT;
goto exit;
}
list_del(&raw_data->entry);
raw->list_data -= raw_data->len;
retval = raw_data->len;
kfree(raw_data);
exit:
mutex_unlock(&raw->list_lock);
return retval;
}
static const struct file_operations raw_fops = {
.owner = THIS_MODULE,
.write = raw_write,
.read = raw_read,
.open = raw_open,
.llseek = noop_llseek,
};
static int raw_init(void)
{
dev_t dev;
int retval;
raw_class = class_create(THIS_MODULE, "gb_raw");
if (IS_ERR(raw_class)) {
retval = PTR_ERR(raw_class);
goto error_class;
}
retval = alloc_chrdev_region(&dev, 0, NUM_MINORS, "gb_raw");
if (retval < 0)
goto error_chrdev;
raw_major = MAJOR(dev);
retval = gb_protocol_register(&raw_protocol);
if (retval)
goto error_gb;
return 0;
error_gb:
unregister_chrdev_region(dev, NUM_MINORS);
error_chrdev:
class_destroy(raw_class);
error_class:
return retval;
}
module_init(raw_init);
static void __exit raw_exit(void)
{
gb_protocol_deregister(&raw_protocol);
unregister_chrdev_region(MKDEV(raw_major, 0), NUM_MINORS);
class_destroy(raw_class);
ida_destroy(&minors);
}
module_exit(raw_exit);
MODULE_LICENSE("GPL v2");