1 // SPDX-License-Identifier: GPL-2.0
3 * Greybus driver for the Raw protocol
5 * Copyright 2015 Google Inc.
6 * Copyright 2015 Linaro Ltd.
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>
14 #include <linux/idr.h>
15 #include <linux/uaccess.h>
20 struct gb_connection
*connection
;
22 struct list_head list
;
24 struct mutex list_lock
;
27 struct device
*device
;
31 struct list_head entry
;
36 static struct class *raw_class
;
38 static const struct file_operations raw_fops
;
39 static DEFINE_IDA(minors
);
41 /* Number of minor devices this driver supports */
42 #define NUM_MINORS 256
44 /* Maximum size of any one send data buffer we support */
45 #define MAX_PACKET_SIZE (PAGE_SIZE * 2)
48 * Maximum size of the data in the receive buffer we allow before we start to
49 * drop messages on the floor
51 #define MAX_DATA_SIZE (MAX_PACKET_SIZE * 8)
54 * Add the raw data message to the list of received messages.
56 static int receive_data(struct gb_raw
*raw
, u32 len
, u8
*data
)
58 struct raw_data
*raw_data
;
59 struct device
*dev
= &raw
->connection
->bundle
->dev
;
62 if (len
> MAX_PACKET_SIZE
) {
63 dev_err(dev
, "Too big of a data packet, rejected\n");
67 mutex_lock(&raw
->list_lock
);
68 if ((raw
->list_data
+ len
) > MAX_DATA_SIZE
) {
69 dev_err(dev
, "Too much data in receive buffer, now dropping packets\n");
74 raw_data
= kmalloc(sizeof(*raw_data
) + len
, GFP_KERNEL
);
80 raw
->list_data
+= len
;
82 memcpy(&raw_data
->data
[0], data
, len
);
84 list_add_tail(&raw_data
->entry
, &raw
->list
);
86 mutex_unlock(&raw
->list_lock
);
90 static int gb_raw_request_handler(struct gb_operation
*op
)
92 struct gb_connection
*connection
= op
->connection
;
93 struct device
*dev
= &connection
->bundle
->dev
;
94 struct gb_raw
*raw
= greybus_get_drvdata(connection
->bundle
);
95 struct gb_raw_send_request
*receive
;
98 if (op
->type
!= GB_RAW_TYPE_SEND
) {
99 dev_err(dev
, "unknown request type 0x%02x\n", op
->type
);
103 /* Verify size of payload */
104 if (op
->request
->payload_size
< sizeof(*receive
)) {
105 dev_err(dev
, "raw receive request too small (%zu < %zu)\n",
106 op
->request
->payload_size
, sizeof(*receive
));
109 receive
= op
->request
->payload
;
110 len
= le32_to_cpu(receive
->len
);
111 if (len
!= (int)(op
->request
->payload_size
- sizeof(__le32
))) {
112 dev_err(dev
, "raw receive request wrong size %d vs %d\n", len
,
113 (int)(op
->request
->payload_size
- sizeof(__le32
)));
117 dev_err(dev
, "raw receive request of 0 bytes?\n");
121 return receive_data(raw
, len
, receive
->data
);
124 static int gb_raw_send(struct gb_raw
*raw
, u32 len
, const char __user
*data
)
126 struct gb_connection
*connection
= raw
->connection
;
127 struct gb_raw_send_request
*request
;
130 request
= kmalloc(len
+ sizeof(*request
), GFP_KERNEL
);
134 if (copy_from_user(&request
->data
[0], data
, len
)) {
139 request
->len
= cpu_to_le32(len
);
141 retval
= gb_operation_sync(connection
, GB_RAW_TYPE_SEND
,
142 request
, len
+ sizeof(*request
),
149 static int gb_raw_probe(struct gb_bundle
*bundle
,
150 const struct greybus_bundle_id
*id
)
152 struct greybus_descriptor_cport
*cport_desc
;
153 struct gb_connection
*connection
;
158 if (bundle
->num_cports
!= 1)
161 cport_desc
= &bundle
->cport_desc
[0];
162 if (cport_desc
->protocol_id
!= GREYBUS_PROTOCOL_RAW
)
165 raw
= kzalloc(sizeof(*raw
), GFP_KERNEL
);
169 connection
= gb_connection_create(bundle
, le16_to_cpu(cport_desc
->id
),
170 gb_raw_request_handler
);
171 if (IS_ERR(connection
)) {
172 retval
= PTR_ERR(connection
);
176 INIT_LIST_HEAD(&raw
->list
);
177 mutex_init(&raw
->list_lock
);
179 raw
->connection
= connection
;
180 greybus_set_drvdata(bundle
, raw
);
182 minor
= ida_simple_get(&minors
, 0, 0, GFP_KERNEL
);
185 goto error_connection_destroy
;
188 raw
->dev
= MKDEV(raw_major
, minor
);
189 cdev_init(&raw
->cdev
, &raw_fops
);
191 retval
= gb_connection_enable(connection
);
193 goto error_remove_ida
;
195 retval
= cdev_add(&raw
->cdev
, raw
->dev
, 1);
197 goto error_connection_disable
;
199 raw
->device
= device_create(raw_class
, &connection
->bundle
->dev
,
200 raw
->dev
, raw
, "gb!raw%d", minor
);
201 if (IS_ERR(raw
->device
)) {
202 retval
= PTR_ERR(raw
->device
);
209 cdev_del(&raw
->cdev
);
211 error_connection_disable
:
212 gb_connection_disable(connection
);
215 ida_simple_remove(&minors
, minor
);
217 error_connection_destroy
:
218 gb_connection_destroy(connection
);
225 static void gb_raw_disconnect(struct gb_bundle
*bundle
)
227 struct gb_raw
*raw
= greybus_get_drvdata(bundle
);
228 struct gb_connection
*connection
= raw
->connection
;
229 struct raw_data
*raw_data
;
230 struct raw_data
*temp
;
232 // FIXME - handle removing a connection when the char device node is open.
233 device_destroy(raw_class
, raw
->dev
);
234 cdev_del(&raw
->cdev
);
235 gb_connection_disable(connection
);
236 ida_simple_remove(&minors
, MINOR(raw
->dev
));
237 gb_connection_destroy(connection
);
239 mutex_lock(&raw
->list_lock
);
240 list_for_each_entry_safe(raw_data
, temp
, &raw
->list
, entry
) {
241 list_del(&raw_data
->entry
);
244 mutex_unlock(&raw
->list_lock
);
250 * Character device node interfaces.
252 * Note, we are using read/write to only allow a single read/write per message.
253 * This means for read(), you have to provide a big enough buffer for the full
254 * message to be copied into. If the buffer isn't big enough, the read() will
258 static int raw_open(struct inode
*inode
, struct file
*file
)
260 struct cdev
*cdev
= inode
->i_cdev
;
261 struct gb_raw
*raw
= container_of(cdev
, struct gb_raw
, cdev
);
263 file
->private_data
= raw
;
267 static ssize_t
raw_write(struct file
*file
, const char __user
*buf
,
268 size_t count
, loff_t
*ppos
)
270 struct gb_raw
*raw
= file
->private_data
;
276 if (count
> MAX_PACKET_SIZE
)
279 retval
= gb_raw_send(raw
, count
, buf
);
286 static ssize_t
raw_read(struct file
*file
, char __user
*buf
, size_t count
,
289 struct gb_raw
*raw
= file
->private_data
;
291 struct raw_data
*raw_data
;
293 mutex_lock(&raw
->list_lock
);
294 if (list_empty(&raw
->list
))
297 raw_data
= list_first_entry(&raw
->list
, struct raw_data
, entry
);
298 if (raw_data
->len
> count
) {
303 if (copy_to_user(buf
, &raw_data
->data
[0], raw_data
->len
)) {
308 list_del(&raw_data
->entry
);
309 raw
->list_data
-= raw_data
->len
;
310 retval
= raw_data
->len
;
314 mutex_unlock(&raw
->list_lock
);
318 static const struct file_operations raw_fops
= {
319 .owner
= THIS_MODULE
,
323 .llseek
= noop_llseek
,
326 static const struct greybus_bundle_id gb_raw_id_table
[] = {
327 { GREYBUS_DEVICE_CLASS(GREYBUS_CLASS_RAW
) },
330 MODULE_DEVICE_TABLE(greybus
, gb_raw_id_table
);
332 static struct greybus_driver gb_raw_driver
= {
334 .probe
= gb_raw_probe
,
335 .disconnect
= gb_raw_disconnect
,
336 .id_table
= gb_raw_id_table
,
339 static int raw_init(void)
344 raw_class
= class_create(THIS_MODULE
, "gb_raw");
345 if (IS_ERR(raw_class
)) {
346 retval
= PTR_ERR(raw_class
);
350 retval
= alloc_chrdev_region(&dev
, 0, NUM_MINORS
, "gb_raw");
354 raw_major
= MAJOR(dev
);
356 retval
= greybus_register(&gb_raw_driver
);
363 unregister_chrdev_region(dev
, NUM_MINORS
);
365 class_destroy(raw_class
);
369 module_init(raw_init
);
371 static void __exit
raw_exit(void)
373 greybus_deregister(&gb_raw_driver
);
374 unregister_chrdev_region(MKDEV(raw_major
, 0), NUM_MINORS
);
375 class_destroy(raw_class
);
376 ida_destroy(&minors
);
378 module_exit(raw_exit
);
380 MODULE_LICENSE("GPL v2");