1 // SPDX-License-Identifier: GPL-2.0
3 * cdev.c - Character device component for Mostcore
5 * Copyright (C) 2013-2015 Microchip Technology Germany II GmbH & Co. KG
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9 #include <linux/module.h>
10 #include <linux/sched.h>
12 #include <linux/slab.h>
13 #include <linux/device.h>
14 #include <linux/cdev.h>
15 #include <linux/poll.h>
16 #include <linux/kfifo.h>
17 #include <linux/uaccess.h>
18 #include <linux/idr.h>
19 #include "most/core.h"
21 static struct cdev_component {
26 struct core_component cc;
31 spinlock_t unlink; /* synchronization lock to unlink channels */
34 struct mutex io_mutex;
35 struct most_interface *iface;
36 struct most_channel_config *cfg;
37 unsigned int channel_id;
40 DECLARE_KFIFO_PTR(fifo, typeof(struct mbo *));
42 struct list_head list;
45 #define to_channel(d) container_of(d, struct comp_channel, cdev)
46 static struct list_head channel_list;
47 static spinlock_t ch_list_lock;
49 static inline bool ch_has_mbo(struct comp_channel *c)
51 return channel_has_mbo(c->iface, c->channel_id, &comp.cc) > 0;
54 static inline bool ch_get_mbo(struct comp_channel *c, struct mbo **mbo)
56 if (!kfifo_peek(&c->fifo, mbo)) {
57 *mbo = most_get_mbo(c->iface, c->channel_id, &comp.cc);
59 kfifo_in(&c->fifo, mbo, 1);
64 static struct comp_channel *get_channel(struct most_interface *iface, int id)
66 struct comp_channel *c, *tmp;
68 int found_channel = 0;
70 spin_lock_irqsave(&ch_list_lock, flags);
71 list_for_each_entry_safe(c, tmp, &channel_list, list) {
72 if ((c->iface == iface) && (c->channel_id == id)) {
77 spin_unlock_irqrestore(&ch_list_lock, flags);
83 static void stop_channel(struct comp_channel *c)
87 while (kfifo_out((struct kfifo *)&c->fifo, &mbo, 1))
89 most_stop_channel(c->iface, c->channel_id, &comp.cc);
92 static void destroy_cdev(struct comp_channel *c)
96 device_destroy(comp.class, c->devno);
98 spin_lock_irqsave(&ch_list_lock, flags);
100 spin_unlock_irqrestore(&ch_list_lock, flags);
103 static void destroy_channel(struct comp_channel *c)
105 ida_simple_remove(&comp.minor_id, MINOR(c->devno));
106 kfifo_free(&c->fifo);
111 * comp_open - implements the syscall to open the device
112 * @inode: inode pointer
113 * @filp: file pointer
115 * This stores the channel pointer in the private data field of
116 * the file structure and activates the channel within the core.
118 static int comp_open(struct inode *inode, struct file *filp)
120 struct comp_channel *c;
123 c = to_channel(inode->i_cdev);
124 filp->private_data = c;
126 if (((c->cfg->direction == MOST_CH_RX) &&
127 ((filp->f_flags & O_ACCMODE) != O_RDONLY)) ||
128 ((c->cfg->direction == MOST_CH_TX) &&
129 ((filp->f_flags & O_ACCMODE) != O_WRONLY))) {
130 pr_info("WARN: Access flags mismatch\n");
134 mutex_lock(&c->io_mutex);
136 pr_info("WARN: Device is destroyed\n");
137 mutex_unlock(&c->io_mutex);
142 pr_info("WARN: Device is busy\n");
143 mutex_unlock(&c->io_mutex);
148 ret = most_start_channel(c->iface, c->channel_id, &comp.cc);
151 mutex_unlock(&c->io_mutex);
156 * comp_close - implements the syscall to close the device
157 * @inode: inode pointer
158 * @filp: file pointer
160 * This stops the channel within the core.
162 static int comp_close(struct inode *inode, struct file *filp)
164 struct comp_channel *c = to_channel(inode->i_cdev);
166 mutex_lock(&c->io_mutex);
167 spin_lock(&c->unlink);
169 spin_unlock(&c->unlink);
172 mutex_unlock(&c->io_mutex);
174 mutex_unlock(&c->io_mutex);
181 * comp_write - implements the syscall to write to the device
182 * @filp: file pointer
183 * @buf: pointer to user buffer
184 * @count: number of bytes to write
185 * @offset: offset from where to start writing
187 static ssize_t comp_write(struct file *filp, const char __user *buf,
188 size_t count, loff_t *offset)
191 size_t to_copy, left;
192 struct mbo *mbo = NULL;
193 struct comp_channel *c = filp->private_data;
195 mutex_lock(&c->io_mutex);
196 while (c->dev && !ch_get_mbo(c, &mbo)) {
197 mutex_unlock(&c->io_mutex);
199 if ((filp->f_flags & O_NONBLOCK))
201 if (wait_event_interruptible(c->wq, ch_has_mbo(c) || !c->dev))
203 mutex_lock(&c->io_mutex);
206 if (unlikely(!c->dev)) {
211 to_copy = min(count, c->cfg->buffer_size - c->mbo_offs);
212 left = copy_from_user(mbo->virt_address + c->mbo_offs, buf, to_copy);
213 if (left == to_copy) {
218 c->mbo_offs += to_copy - left;
219 if (c->mbo_offs >= c->cfg->buffer_size ||
220 c->cfg->data_type == MOST_CH_CONTROL ||
221 c->cfg->data_type == MOST_CH_ASYNC) {
222 kfifo_skip(&c->fifo);
223 mbo->buffer_length = c->mbo_offs;
225 most_submit_mbo(mbo);
228 ret = to_copy - left;
230 mutex_unlock(&c->io_mutex);
235 * comp_read - implements the syscall to read from the device
236 * @filp: file pointer
237 * @buf: pointer to user buffer
238 * @count: number of bytes to read
239 * @offset: offset from where to start reading
242 comp_read(struct file *filp, char __user *buf, size_t count, loff_t *offset)
244 size_t to_copy, not_copied, copied;
246 struct comp_channel *c = filp->private_data;
248 mutex_lock(&c->io_mutex);
249 while (c->dev && !kfifo_peek(&c->fifo, &mbo)) {
250 mutex_unlock(&c->io_mutex);
251 if (filp->f_flags & O_NONBLOCK)
253 if (wait_event_interruptible(c->wq,
254 (!kfifo_is_empty(&c->fifo) ||
257 mutex_lock(&c->io_mutex);
260 /* make sure we don't submit to gone devices */
261 if (unlikely(!c->dev)) {
262 mutex_unlock(&c->io_mutex);
266 to_copy = min_t(size_t,
268 mbo->processed_length - c->mbo_offs);
270 not_copied = copy_to_user(buf,
271 mbo->virt_address + c->mbo_offs,
274 copied = to_copy - not_copied;
276 c->mbo_offs += copied;
277 if (c->mbo_offs >= mbo->processed_length) {
278 kfifo_skip(&c->fifo);
282 mutex_unlock(&c->io_mutex);
286 static __poll_t comp_poll(struct file *filp, poll_table *wait)
288 struct comp_channel *c = filp->private_data;
291 poll_wait(filp, &c->wq, wait);
293 if (c->cfg->direction == MOST_CH_RX) {
294 if (!kfifo_is_empty(&c->fifo))
295 mask |= POLLIN | POLLRDNORM;
297 if (!kfifo_is_empty(&c->fifo) || ch_has_mbo(c))
298 mask |= POLLOUT | POLLWRNORM;
304 * Initialization of struct file_operations
306 static const struct file_operations channel_fops = {
307 .owner = THIS_MODULE,
311 .release = comp_close,
316 * comp_disconnect_channel - disconnect a channel
317 * @iface: pointer to interface instance
318 * @channel_id: channel index
320 * This frees allocated memory and removes the cdev that represents this
321 * channel in user space.
323 static int comp_disconnect_channel(struct most_interface *iface, int channel_id)
325 struct comp_channel *c;
328 pr_info("Bad interface pointer\n");
332 c = get_channel(iface, channel_id);
336 mutex_lock(&c->io_mutex);
337 spin_lock(&c->unlink);
339 spin_unlock(&c->unlink);
343 wake_up_interruptible(&c->wq);
344 mutex_unlock(&c->io_mutex);
346 mutex_unlock(&c->io_mutex);
353 * comp_rx_completion - completion handler for rx channels
354 * @mbo: pointer to buffer object that has completed
356 * This searches for the channel linked to this MBO and stores it in the local
359 static int comp_rx_completion(struct mbo *mbo)
361 struct comp_channel *c;
366 c = get_channel(mbo->ifp, mbo->hdm_channel_id);
370 spin_lock(&c->unlink);
371 if (!c->access_ref || !c->dev) {
372 spin_unlock(&c->unlink);
375 kfifo_in(&c->fifo, &mbo, 1);
376 spin_unlock(&c->unlink);
378 if (kfifo_is_full(&c->fifo))
379 pr_info("WARN: Fifo is full\n");
381 wake_up_interruptible(&c->wq);
386 * comp_tx_completion - completion handler for tx channels
387 * @iface: pointer to interface instance
388 * @channel_id: channel index/ID
390 * This wakes sleeping processes in the wait-queue.
392 static int comp_tx_completion(struct most_interface *iface, int channel_id)
394 struct comp_channel *c;
397 pr_info("Bad interface pointer\n");
400 if ((channel_id < 0) || (channel_id >= iface->num_channels)) {
401 pr_info("Channel ID out of range\n");
405 c = get_channel(iface, channel_id);
408 wake_up_interruptible(&c->wq);
413 * comp_probe - probe function of the driver module
414 * @iface: pointer to interface instance
415 * @channel_id: channel index/ID
416 * @cfg: pointer to actual channel configuration
417 * @name: name of the device to be created
419 * This allocates achannel object and creates the device node in /dev
421 * Returns 0 on success or error code otherwise.
423 static int comp_probe(struct most_interface *iface, int channel_id,
424 struct most_channel_config *cfg, char *name)
426 struct comp_channel *c;
427 unsigned long cl_flags;
431 if ((!iface) || (!cfg) || (!name)) {
432 pr_info("Probing component with bad arguments");
435 c = get_channel(iface, channel_id);
439 current_minor = ida_simple_get(&comp.minor_id, 0, 0, GFP_KERNEL);
440 if (current_minor < 0)
441 return current_minor;
443 c = kzalloc(sizeof(*c), GFP_KERNEL);
446 goto error_alloc_channel;
449 c->devno = MKDEV(comp.major, current_minor);
450 cdev_init(&c->cdev, &channel_fops);
451 c->cdev.owner = THIS_MODULE;
452 cdev_add(&c->cdev, c->devno, 1);
455 c->channel_id = channel_id;
457 spin_lock_init(&c->unlink);
459 retval = kfifo_alloc(&c->fifo, cfg->num_buffers, GFP_KERNEL);
461 pr_info("failed to alloc channel kfifo");
462 goto error_alloc_kfifo;
464 init_waitqueue_head(&c->wq);
465 mutex_init(&c->io_mutex);
466 spin_lock_irqsave(&ch_list_lock, cl_flags);
467 list_add_tail(&c->list, &channel_list);
468 spin_unlock_irqrestore(&ch_list_lock, cl_flags);
469 c->dev = device_create(comp.class, NULL, c->devno, NULL, "%s", name);
471 if (IS_ERR(c->dev)) {
472 retval = PTR_ERR(c->dev);
473 pr_info("failed to create new device node %s\n", name);
474 goto error_create_device;
476 kobject_uevent(&c->dev->kobj, KOBJ_ADD);
480 kfifo_free(&c->fifo);
486 ida_simple_remove(&comp.minor_id, current_minor);
490 static struct cdev_component comp = {
493 .probe_channel = comp_probe,
494 .disconnect_channel = comp_disconnect_channel,
495 .rx_completion = comp_rx_completion,
496 .tx_completion = comp_tx_completion,
500 static int __init mod_init(void)
506 comp.class = class_create(THIS_MODULE, "most_cdev");
507 if (IS_ERR(comp.class)) {
508 pr_info("No udev support.\n");
509 return PTR_ERR(comp.class);
512 INIT_LIST_HEAD(&channel_list);
513 spin_lock_init(&ch_list_lock);
514 ida_init(&comp.minor_id);
516 err = alloc_chrdev_region(&comp.devno, 0, 50, "cdev");
519 comp.major = MAJOR(comp.devno);
520 err = most_register_component(&comp.cc);
526 unregister_chrdev_region(comp.devno, 1);
528 ida_destroy(&comp.minor_id);
529 class_destroy(comp.class);
533 static void __exit mod_exit(void)
535 struct comp_channel *c, *tmp;
537 pr_info("exit module\n");
539 most_deregister_component(&comp.cc);
541 list_for_each_entry_safe(c, tmp, &channel_list, list) {
545 unregister_chrdev_region(comp.devno, 1);
546 ida_destroy(&comp.minor_id);
547 class_destroy(comp.class);
550 module_init(mod_init);
551 module_exit(mod_exit);
552 MODULE_AUTHOR("Christian Gromm <christian.gromm@microchip.com>");
553 MODULE_LICENSE("GPL");
554 MODULE_DESCRIPTION("character device component for mostcore");