blob: 64a17dfe3b6ee9d92873f7f9a00f95eccbc2bf85 [file] [log] [blame]
Greg Kroah-Hartmaneb50fd32017-11-07 14:58:41 +01001// SPDX-License-Identifier: GPL-2.0
Greg Kroah-Hartmane806c7f2015-05-08 15:50:17 +02002/*
3 * Greybus driver for the Raw protocol
4 *
5 * Copyright 2015 Google Inc.
6 * Copyright 2015 Linaro Ltd.
Greg Kroah-Hartmane806c7f2015-05-08 15:50:17 +02007 */
8#include <linux/kernel.h>
9#include <linux/module.h>
10#include <linux/slab.h>
11#include <linux/sizes.h>
12#include <linux/cdev.h>
13#include <linux/fs.h>
14#include <linux/idr.h>
Viresh Kumar4f8ab112015-05-20 16:31:19 +053015#include <linux/uaccess.h>
Greg Kroah-Hartmanec0ad862019-08-25 07:54:27 +020016#include <linux/greybus.h>
Greg Kroah-Hartmane806c7f2015-05-08 15:50:17 +020017
18struct gb_raw {
19 struct gb_connection *connection;
Greg Kroah-Hartmane806c7f2015-05-08 15:50:17 +020020
21 struct list_head list;
22 int list_data;
23 struct mutex list_lock;
24 dev_t dev;
25 struct cdev cdev;
26 struct device *device;
27};
28
Greg Kroah-Hartmane806c7f2015-05-08 15:50:17 +020029struct raw_data {
30 struct list_head entry;
31 u32 len;
32 u8 data[0];
33};
34
35static struct class *raw_class;
36static int raw_major;
37static const struct file_operations raw_fops;
38static DEFINE_IDA(minors);
39
40/* Number of minor devices this driver supports */
41#define NUM_MINORS 256
42
43/* Maximum size of any one send data buffer we support */
44#define MAX_PACKET_SIZE (PAGE_SIZE * 2)
45
46/*
47 * Maximum size of the data in the receive buffer we allow before we start to
48 * drop messages on the floor
49 */
50#define MAX_DATA_SIZE (MAX_PACKET_SIZE * 8)
51
52/*
53 * Add the raw data message to the list of received messages.
54 */
55static int receive_data(struct gb_raw *raw, u32 len, u8 *data)
56{
57 struct raw_data *raw_data;
Viresh Kumar2554eda2016-02-12 16:08:27 +053058 struct device *dev = &raw->connection->bundle->dev;
Greg Kroah-Hartmane806c7f2015-05-08 15:50:17 +020059 int retval = 0;
60
61 if (len > MAX_PACKET_SIZE) {
Viresh Kumar2554eda2016-02-12 16:08:27 +053062 dev_err(dev, "Too big of a data packet, rejected\n");
Greg Kroah-Hartmane806c7f2015-05-08 15:50:17 +020063 return -EINVAL;
64 }
65
66 mutex_lock(&raw->list_lock);
67 if ((raw->list_data + len) > MAX_DATA_SIZE) {
Viresh Kumar2554eda2016-02-12 16:08:27 +053068 dev_err(dev, "Too much data in receive buffer, now dropping packets\n");
Greg Kroah-Hartmane806c7f2015-05-08 15:50:17 +020069 retval = -EINVAL;
70 goto exit;
71 }
72
73 raw_data = kmalloc(sizeof(*raw_data) + len, GFP_KERNEL);
74 if (!raw_data) {
75 retval = -ENOMEM;
76 goto exit;
77 }
78
79 raw->list_data += len;
80 raw_data->len = len;
81 memcpy(&raw_data->data[0], data, len);
82
83 list_add_tail(&raw_data->entry, &raw->list);
84exit:
85 mutex_unlock(&raw->list_lock);
86 return retval;
87}
88
Viresh Kumar512cc322016-02-15 10:47:27 +053089static int gb_raw_request_handler(struct gb_operation *op)
Greg Kroah-Hartmane806c7f2015-05-08 15:50:17 +020090{
91 struct gb_connection *connection = op->connection;
Viresh Kumar2554eda2016-02-12 16:08:27 +053092 struct device *dev = &connection->bundle->dev;
Viresh Kumar512cc322016-02-15 10:47:27 +053093 struct gb_raw *raw = greybus_get_drvdata(connection->bundle);
Greg Kroah-Hartmane806c7f2015-05-08 15:50:17 +020094 struct gb_raw_send_request *receive;
95 u32 len;
96
Viresh Kumar512cc322016-02-15 10:47:27 +053097 if (op->type != GB_RAW_TYPE_SEND) {
Johan Hovoldc7733b62016-03-02 18:00:51 +010098 dev_err(dev, "unknown request type 0x%02x\n", op->type);
Greg Kroah-Hartmane806c7f2015-05-08 15:50:17 +020099 return -EINVAL;
100 }
101
102 /* Verify size of payload */
103 if (op->request->payload_size < sizeof(*receive)) {
Viresh Kumar2554eda2016-02-12 16:08:27 +0530104 dev_err(dev, "raw receive request too small (%zu < %zu)\n",
Viresh Kumar7fea6412015-08-06 12:44:52 +0530105 op->request->payload_size, sizeof(*receive));
Greg Kroah-Hartmane806c7f2015-05-08 15:50:17 +0200106 return -EINVAL;
107 }
108 receive = op->request->payload;
109 len = le32_to_cpu(receive->len);
110 if (len != (int)(op->request->payload_size - sizeof(__le32))) {
Viresh Kumar2554eda2016-02-12 16:08:27 +0530111 dev_err(dev, "raw receive request wrong size %d vs %d\n", len,
Greg Kroah-Hartmane806c7f2015-05-08 15:50:17 +0200112 (int)(op->request->payload_size - sizeof(__le32)));
113 return -EINVAL;
114 }
115 if (len == 0) {
Viresh Kumar2554eda2016-02-12 16:08:27 +0530116 dev_err(dev, "raw receive request of 0 bytes?\n");
Greg Kroah-Hartmane806c7f2015-05-08 15:50:17 +0200117 return -EINVAL;
118 }
119
120 return receive_data(raw, len, receive->data);
121}
122
123static int gb_raw_send(struct gb_raw *raw, u32 len, const char __user *data)
124{
125 struct gb_connection *connection = raw->connection;
126 struct gb_raw_send_request *request;
127 int retval;
128
129 request = kmalloc(len + sizeof(*request), GFP_KERNEL);
130 if (!request)
131 return -ENOMEM;
132
133 if (copy_from_user(&request->data[0], data, len)) {
134 kfree(request);
135 return -EFAULT;
136 }
137
138 request->len = cpu_to_le32(len);
139
140 retval = gb_operation_sync(connection, GB_RAW_TYPE_SEND,
141 request, len + sizeof(*request),
142 NULL, 0);
143
144 kfree(request);
145 return retval;
146}
147
Viresh Kumar512cc322016-02-15 10:47:27 +0530148static int gb_raw_probe(struct gb_bundle *bundle,
149 const struct greybus_bundle_id *id)
Greg Kroah-Hartmane806c7f2015-05-08 15:50:17 +0200150{
Viresh Kumar512cc322016-02-15 10:47:27 +0530151 struct greybus_descriptor_cport *cport_desc;
152 struct gb_connection *connection;
Greg Kroah-Hartmane806c7f2015-05-08 15:50:17 +0200153 struct gb_raw *raw;
154 int retval;
155 int minor;
156
Viresh Kumar512cc322016-02-15 10:47:27 +0530157 if (bundle->num_cports != 1)
158 return -ENODEV;
159
160 cport_desc = &bundle->cport_desc[0];
161 if (cport_desc->protocol_id != GREYBUS_PROTOCOL_RAW)
162 return -ENODEV;
163
Greg Kroah-Hartmane806c7f2015-05-08 15:50:17 +0200164 raw = kzalloc(sizeof(*raw), GFP_KERNEL);
165 if (!raw)
166 return -ENOMEM;
167
Viresh Kumar512cc322016-02-15 10:47:27 +0530168 connection = gb_connection_create(bundle, le16_to_cpu(cport_desc->id),
169 gb_raw_request_handler);
170 if (IS_ERR(connection)) {
171 retval = PTR_ERR(connection);
172 goto error_free;
173 }
Greg Kroah-Hartmane806c7f2015-05-08 15:50:17 +0200174
Greg Kroah-Hartmane806c7f2015-05-08 15:50:17 +0200175 INIT_LIST_HEAD(&raw->list);
176 mutex_init(&raw->list_lock);
177
Viresh Kumar512cc322016-02-15 10:47:27 +0530178 raw->connection = connection;
179 greybus_set_drvdata(bundle, raw);
180
Greg Kroah-Hartmane806c7f2015-05-08 15:50:17 +0200181 minor = ida_simple_get(&minors, 0, 0, GFP_KERNEL);
182 if (minor < 0) {
183 retval = minor;
Viresh Kumar512cc322016-02-15 10:47:27 +0530184 goto error_connection_destroy;
Greg Kroah-Hartmane806c7f2015-05-08 15:50:17 +0200185 }
186
187 raw->dev = MKDEV(raw_major, minor);
188 cdev_init(&raw->cdev, &raw_fops);
Viresh Kumar512cc322016-02-15 10:47:27 +0530189
190 retval = gb_connection_enable(connection);
Greg Kroah-Hartmane806c7f2015-05-08 15:50:17 +0200191 if (retval)
Viresh Kumarc4635932016-02-12 16:08:26 +0530192 goto error_remove_ida;
Greg Kroah-Hartmane806c7f2015-05-08 15:50:17 +0200193
Viresh Kumar512cc322016-02-15 10:47:27 +0530194 retval = cdev_add(&raw->cdev, raw->dev, 1);
195 if (retval)
196 goto error_connection_disable;
197
Greg Kroah-Hartman29a167e2015-10-14 11:31:00 -0700198 raw->device = device_create(raw_class, &connection->bundle->dev,
199 raw->dev, raw, "gb!raw%d", minor);
Greg Kroah-Hartmane806c7f2015-05-08 15:50:17 +0200200 if (IS_ERR(raw->device)) {
201 retval = PTR_ERR(raw->device);
Viresh Kumarc4635932016-02-12 16:08:26 +0530202 goto error_del_cdev;
Greg Kroah-Hartmane806c7f2015-05-08 15:50:17 +0200203 }
204
205 return 0;
206
Viresh Kumarc4635932016-02-12 16:08:26 +0530207error_del_cdev:
Greg Kroah-Hartmane806c7f2015-05-08 15:50:17 +0200208 cdev_del(&raw->cdev);
209
Viresh Kumar512cc322016-02-15 10:47:27 +0530210error_connection_disable:
211 gb_connection_disable(connection);
212
Viresh Kumarc4635932016-02-12 16:08:26 +0530213error_remove_ida:
Greg Kroah-Hartmane806c7f2015-05-08 15:50:17 +0200214 ida_simple_remove(&minors, minor);
215
Viresh Kumar512cc322016-02-15 10:47:27 +0530216error_connection_destroy:
217 gb_connection_destroy(connection);
218
Greg Kroah-Hartmane806c7f2015-05-08 15:50:17 +0200219error_free:
220 kfree(raw);
221 return retval;
222}
223
Viresh Kumar512cc322016-02-15 10:47:27 +0530224static void gb_raw_disconnect(struct gb_bundle *bundle)
Greg Kroah-Hartmane806c7f2015-05-08 15:50:17 +0200225{
Viresh Kumar512cc322016-02-15 10:47:27 +0530226 struct gb_raw *raw = greybus_get_drvdata(bundle);
227 struct gb_connection *connection = raw->connection;
Greg Kroah-Hartmane806c7f2015-05-08 15:50:17 +0200228 struct raw_data *raw_data;
229 struct raw_data *temp;
230
231 // FIXME - handle removing a connection when the char device node is open.
Johan Hovold397d3412016-02-10 11:08:29 +0100232 device_destroy(raw_class, raw->dev);
Greg Kroah-Hartmane806c7f2015-05-08 15:50:17 +0200233 cdev_del(&raw->cdev);
Viresh Kumar512cc322016-02-15 10:47:27 +0530234 gb_connection_disable(connection);
Greg Kroah-Hartmane806c7f2015-05-08 15:50:17 +0200235 ida_simple_remove(&minors, MINOR(raw->dev));
Viresh Kumar512cc322016-02-15 10:47:27 +0530236 gb_connection_destroy(connection);
237
Greg Kroah-Hartmane806c7f2015-05-08 15:50:17 +0200238 mutex_lock(&raw->list_lock);
239 list_for_each_entry_safe(raw_data, temp, &raw->list, entry) {
240 list_del(&raw_data->entry);
241 kfree(raw_data);
242 }
243 mutex_unlock(&raw->list_lock);
244
245 kfree(raw);
246}
247
Greg Kroah-Hartmane806c7f2015-05-08 15:50:17 +0200248/*
249 * Character device node interfaces.
250 *
251 * Note, we are using read/write to only allow a single read/write per message.
252 * This means for read(), you have to provide a big enough buffer for the full
253 * message to be copied into. If the buffer isn't big enough, the read() will
254 * fail with -ENOSPC.
255 */
256
257static int raw_open(struct inode *inode, struct file *file)
258{
259 struct cdev *cdev = inode->i_cdev;
260 struct gb_raw *raw = container_of(cdev, struct gb_raw, cdev);
261
262 file->private_data = raw;
263 return 0;
264}
265
266static ssize_t raw_write(struct file *file, const char __user *buf,
267 size_t count, loff_t *ppos)
268{
269 struct gb_raw *raw = file->private_data;
270 int retval;
271
272 if (!count)
273 return 0;
274
275 if (count > MAX_PACKET_SIZE)
276 return -E2BIG;
277
278 retval = gb_raw_send(raw, count, buf);
279 if (retval)
280 return retval;
281
282 return count;
283}
284
285static ssize_t raw_read(struct file *file, char __user *buf, size_t count,
286 loff_t *ppos)
287{
288 struct gb_raw *raw = file->private_data;
289 int retval = 0;
290 struct raw_data *raw_data;
291
292 mutex_lock(&raw->list_lock);
293 if (list_empty(&raw->list))
294 goto exit;
295
296 raw_data = list_first_entry(&raw->list, struct raw_data, entry);
297 if (raw_data->len > count) {
298 retval = -ENOSPC;
299 goto exit;
300 }
301
302 if (copy_to_user(buf, &raw_data->data[0], raw_data->len)) {
303 retval = -EFAULT;
304 goto exit;
305 }
306
307 list_del(&raw_data->entry);
308 raw->list_data -= raw_data->len;
309 retval = raw_data->len;
310 kfree(raw_data);
311
312exit:
313 mutex_unlock(&raw->list_lock);
314 return retval;
315}
316
317static const struct file_operations raw_fops = {
318 .owner = THIS_MODULE,
319 .write = raw_write,
320 .read = raw_read,
321 .open = raw_open,
322 .llseek = noop_llseek,
323};
324
Viresh Kumar512cc322016-02-15 10:47:27 +0530325static const struct greybus_bundle_id gb_raw_id_table[] = {
326 { GREYBUS_DEVICE_CLASS(GREYBUS_CLASS_RAW) },
327 { }
328};
329MODULE_DEVICE_TABLE(greybus, gb_raw_id_table);
330
331static struct greybus_driver gb_raw_driver = {
332 .name = "raw",
333 .probe = gb_raw_probe,
334 .disconnect = gb_raw_disconnect,
335 .id_table = gb_raw_id_table,
336};
337
Greg Kroah-Hartmane806c7f2015-05-08 15:50:17 +0200338static int raw_init(void)
339{
340 dev_t dev;
341 int retval;
342
343 raw_class = class_create(THIS_MODULE, "gb_raw");
344 if (IS_ERR(raw_class)) {
345 retval = PTR_ERR(raw_class);
346 goto error_class;
347 }
348
349 retval = alloc_chrdev_region(&dev, 0, NUM_MINORS, "gb_raw");
350 if (retval < 0)
351 goto error_chrdev;
352
Greg Kroah-Hartmane806c7f2015-05-08 15:50:17 +0200353 raw_major = MAJOR(dev);
354
Viresh Kumar512cc322016-02-15 10:47:27 +0530355 retval = greybus_register(&gb_raw_driver);
Greg Kroah-Hartmane806c7f2015-05-08 15:50:17 +0200356 if (retval)
357 goto error_gb;
358
359 return 0;
360
361error_gb:
362 unregister_chrdev_region(dev, NUM_MINORS);
363error_chrdev:
364 class_destroy(raw_class);
365error_class:
366 return retval;
367}
Viresh Kumar8ba25222015-05-20 16:54:22 +0530368module_init(raw_init);
Greg Kroah-Hartmane806c7f2015-05-08 15:50:17 +0200369
370static void __exit raw_exit(void)
371{
Viresh Kumar512cc322016-02-15 10:47:27 +0530372 greybus_deregister(&gb_raw_driver);
Greg Kroah-Hartmane806c7f2015-05-08 15:50:17 +0200373 unregister_chrdev_region(MKDEV(raw_major, 0), NUM_MINORS);
374 class_destroy(raw_class);
Greg Kroah-Hartman5c1ac692015-07-08 10:44:09 -0700375 ida_destroy(&minors);
Greg Kroah-Hartmane806c7f2015-05-08 15:50:17 +0200376}
Greg Kroah-Hartmane806c7f2015-05-08 15:50:17 +0200377module_exit(raw_exit);
378
379MODULE_LICENSE("GPL v2");