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>
16 #include <linux/greybus.h>
19 struct gb_connection
*connection
;
21 struct list_head list
;
23 struct mutex list_lock
;
26 struct device
*device
;
30 struct list_head entry
;
35 static struct class *raw_class
;
37 static const struct file_operations raw_fops
;
38 static DEFINE_IDA(minors
);
40 /* Number of minor devices this driver supports */
41 #define NUM_MINORS 256
43 /* Maximum size of any one send data buffer we support */
44 #define MAX_PACKET_SIZE (PAGE_SIZE * 2)
47 * Maximum size of the data in the receive buffer we allow before we start to
48 * drop messages on the floor
50 #define MAX_DATA_SIZE (MAX_PACKET_SIZE * 8)
53 * Add the raw data message to the list of received messages.
55 static int receive_data(struct gb_raw
*raw
, u32 len
, u8
*data
)
57 struct raw_data
*raw_data
;
58 struct device
*dev
= &raw
->connection
->bundle
->dev
;
61 if (len
> MAX_PACKET_SIZE
) {
62 dev_err(dev
, "Too big of a data packet, rejected\n");
66 mutex_lock(&raw
->list_lock
);
67 if ((raw
->list_data
+ len
) > MAX_DATA_SIZE
) {
68 dev_err(dev
, "Too much data in receive buffer, now dropping packets\n");
73 raw_data
= kmalloc(sizeof(*raw_data
) + len
, GFP_KERNEL
);
79 raw
->list_data
+= len
;
81 memcpy(&raw_data
->data
[0], data
, len
);
83 list_add_tail(&raw_data
->entry
, &raw
->list
);
85 mutex_unlock(&raw
->list_lock
);
89 static int gb_raw_request_handler(struct gb_operation
*op
)
91 struct gb_connection
*connection
= op
->connection
;
92 struct device
*dev
= &connection
->bundle
->dev
;
93 struct gb_raw
*raw
= greybus_get_drvdata(connection
->bundle
);
94 struct gb_raw_send_request
*receive
;
97 if (op
->type
!= GB_RAW_TYPE_SEND
) {
98 dev_err(dev
, "unknown request type 0x%02x\n", op
->type
);
102 /* Verify size of payload */
103 if (op
->request
->payload_size
< sizeof(*receive
)) {
104 dev_err(dev
, "raw receive request too small (%zu < %zu)\n",
105 op
->request
->payload_size
, sizeof(*receive
));
108 receive
= op
->request
->payload
;
109 len
= le32_to_cpu(receive
->len
);
110 if (len
!= (int)(op
->request
->payload_size
- sizeof(__le32
))) {
111 dev_err(dev
, "raw receive request wrong size %d vs %d\n", len
,
112 (int)(op
->request
->payload_size
- sizeof(__le32
)));
116 dev_err(dev
, "raw receive request of 0 bytes?\n");
120 return receive_data(raw
, len
, receive
->data
);
123 static int gb_raw_send(struct gb_raw
*raw
, u32 len
, const char __user
*data
)
125 struct gb_connection
*connection
= raw
->connection
;
126 struct gb_raw_send_request
*request
;
129 request
= kmalloc(len
+ sizeof(*request
), GFP_KERNEL
);
133 if (copy_from_user(&request
->data
[0], data
, len
)) {
138 request
->len
= cpu_to_le32(len
);
140 retval
= gb_operation_sync(connection
, GB_RAW_TYPE_SEND
,
141 request
, len
+ sizeof(*request
),
148 static int gb_raw_probe(struct gb_bundle
*bundle
,
149 const struct greybus_bundle_id
*id
)
151 struct greybus_descriptor_cport
*cport_desc
;
152 struct gb_connection
*connection
;
157 if (bundle
->num_cports
!= 1)
160 cport_desc
= &bundle
->cport_desc
[0];
161 if (cport_desc
->protocol_id
!= GREYBUS_PROTOCOL_RAW
)
164 raw
= kzalloc(sizeof(*raw
), GFP_KERNEL
);
168 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
);
175 INIT_LIST_HEAD(&raw
->list
);
176 mutex_init(&raw
->list_lock
);
178 raw
->connection
= connection
;
179 greybus_set_drvdata(bundle
, raw
);
181 minor
= ida_simple_get(&minors
, 0, 0, GFP_KERNEL
);
184 goto error_connection_destroy
;
187 raw
->dev
= MKDEV(raw_major
, minor
);
188 cdev_init(&raw
->cdev
, &raw_fops
);
190 retval
= gb_connection_enable(connection
);
192 goto error_remove_ida
;
194 retval
= cdev_add(&raw
->cdev
, raw
->dev
, 1);
196 goto error_connection_disable
;
198 raw
->device
= device_create(raw_class
, &connection
->bundle
->dev
,
199 raw
->dev
, raw
, "gb!raw%d", minor
);
200 if (IS_ERR(raw
->device
)) {
201 retval
= PTR_ERR(raw
->device
);
208 cdev_del(&raw
->cdev
);
210 error_connection_disable
:
211 gb_connection_disable(connection
);
214 ida_simple_remove(&minors
, minor
);
216 error_connection_destroy
:
217 gb_connection_destroy(connection
);
224 static void gb_raw_disconnect(struct gb_bundle
*bundle
)
226 struct gb_raw
*raw
= greybus_get_drvdata(bundle
);
227 struct gb_connection
*connection
= raw
->connection
;
228 struct raw_data
*raw_data
;
229 struct raw_data
*temp
;
231 // FIXME - handle removing a connection when the char device node is open.
232 device_destroy(raw_class
, raw
->dev
);
233 cdev_del(&raw
->cdev
);
234 gb_connection_disable(connection
);
235 ida_simple_remove(&minors
, MINOR(raw
->dev
));
236 gb_connection_destroy(connection
);
238 mutex_lock(&raw
->list_lock
);
239 list_for_each_entry_safe(raw_data
, temp
, &raw
->list
, entry
) {
240 list_del(&raw_data
->entry
);
243 mutex_unlock(&raw
->list_lock
);
249 * Character device node interfaces.
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
257 static int raw_open(struct inode
*inode
, struct file
*file
)
259 struct cdev
*cdev
= inode
->i_cdev
;
260 struct gb_raw
*raw
= container_of(cdev
, struct gb_raw
, cdev
);
262 file
->private_data
= raw
;
266 static ssize_t
raw_write(struct file
*file
, const char __user
*buf
,
267 size_t count
, loff_t
*ppos
)
269 struct gb_raw
*raw
= file
->private_data
;
275 if (count
> MAX_PACKET_SIZE
)
278 retval
= gb_raw_send(raw
, count
, buf
);
285 static ssize_t
raw_read(struct file
*file
, char __user
*buf
, size_t count
,
288 struct gb_raw
*raw
= file
->private_data
;
290 struct raw_data
*raw_data
;
292 mutex_lock(&raw
->list_lock
);
293 if (list_empty(&raw
->list
))
296 raw_data
= list_first_entry(&raw
->list
, struct raw_data
, entry
);
297 if (raw_data
->len
> count
) {
302 if (copy_to_user(buf
, &raw_data
->data
[0], raw_data
->len
)) {
307 list_del(&raw_data
->entry
);
308 raw
->list_data
-= raw_data
->len
;
309 retval
= raw_data
->len
;
313 mutex_unlock(&raw
->list_lock
);
317 static const struct file_operations raw_fops
= {
318 .owner
= THIS_MODULE
,
322 .llseek
= noop_llseek
,
325 static const struct greybus_bundle_id gb_raw_id_table
[] = {
326 { GREYBUS_DEVICE_CLASS(GREYBUS_CLASS_RAW
) },
329 MODULE_DEVICE_TABLE(greybus
, gb_raw_id_table
);
331 static struct greybus_driver gb_raw_driver
= {
333 .probe
= gb_raw_probe
,
334 .disconnect
= gb_raw_disconnect
,
335 .id_table
= gb_raw_id_table
,
338 static int raw_init(void)
343 raw_class
= class_create(THIS_MODULE
, "gb_raw");
344 if (IS_ERR(raw_class
)) {
345 retval
= PTR_ERR(raw_class
);
349 retval
= alloc_chrdev_region(&dev
, 0, NUM_MINORS
, "gb_raw");
353 raw_major
= MAJOR(dev
);
355 retval
= greybus_register(&gb_raw_driver
);
362 unregister_chrdev_region(dev
, NUM_MINORS
);
364 class_destroy(raw_class
);
368 module_init(raw_init
);
370 static void __exit
raw_exit(void)
372 greybus_deregister(&gb_raw_driver
);
373 unregister_chrdev_region(MKDEV(raw_major
, 0), NUM_MINORS
);
374 class_destroy(raw_class
);
375 ida_destroy(&minors
);
377 module_exit(raw_exit
);
379 MODULE_LICENSE("GPL v2");