2 * cdev.c - Application interfacing module for character devices
4 * Copyright (C) 2013-2015 Microchip Technology Germany II GmbH & Co. KG
6 * This program is distributed in the hope that it will be useful,
7 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 * GNU General Public License for more details.
11 * This file is licensed under GPLv2.
14 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15 #include <linux/module.h>
16 #include <linux/sched.h>
18 #include <linux/slab.h>
19 #include <linux/device.h>
20 #include <linux/cdev.h>
21 #include <linux/poll.h>
22 #include <linux/kfifo.h>
23 #include <linux/uaccess.h>
24 #include <linux/idr.h>
27 static dev_t aim_devno;
28 static struct class *aim_class;
29 static struct ida minor_id;
30 static unsigned int major;
31 static struct most_aim cdev_aim;
35 spinlock_t unlink; /* synchronization lock to unlink channels */
38 struct mutex io_mutex;
39 struct most_interface *iface;
40 struct most_channel_config *cfg;
41 unsigned int channel_id;
44 DECLARE_KFIFO_PTR(fifo, typeof(struct mbo *));
46 struct list_head list;
49 #define to_channel(d) container_of(d, struct aim_channel, cdev)
50 static struct list_head channel_list;
51 static spinlock_t ch_list_lock;
53 static inline bool ch_has_mbo(struct aim_channel *c)
55 return channel_has_mbo(c->iface, c->channel_id, &cdev_aim) > 0;
58 static inline bool ch_get_mbo(struct aim_channel *c, struct mbo **mbo)
60 if (!kfifo_peek(&c->fifo, mbo)) {
61 *mbo = most_get_mbo(c->iface, c->channel_id, &cdev_aim);
63 kfifo_in(&c->fifo, mbo, 1);
68 static struct aim_channel *get_channel(struct most_interface *iface, int id)
70 struct aim_channel *c, *tmp;
72 int found_channel = 0;
74 spin_lock_irqsave(&ch_list_lock, flags);
75 list_for_each_entry_safe(c, tmp, &channel_list, list) {
76 if ((c->iface == iface) && (c->channel_id == id)) {
81 spin_unlock_irqrestore(&ch_list_lock, flags);
87 static void stop_channel(struct aim_channel *c)
91 while (kfifo_out((struct kfifo *)&c->fifo, &mbo, 1))
93 most_stop_channel(c->iface, c->channel_id, &cdev_aim);
96 static void destroy_cdev(struct aim_channel *c)
100 device_destroy(aim_class, c->devno);
102 kfifo_free(&c->fifo);
103 spin_lock_irqsave(&ch_list_lock, flags);
105 spin_unlock_irqrestore(&ch_list_lock, flags);
106 ida_simple_remove(&minor_id, MINOR(c->devno));
110 * aim_open - implements the syscall to open the device
111 * @inode: inode pointer
112 * @filp: file pointer
114 * This stores the channel pointer in the private data field of
115 * the file structure and activates the channel within the core.
117 static int aim_open(struct inode *inode, struct file *filp)
119 struct aim_channel *c;
122 c = to_channel(inode->i_cdev);
123 filp->private_data = c;
125 if (((c->cfg->direction == MOST_CH_RX) &&
126 ((filp->f_flags & O_ACCMODE) != O_RDONLY)) ||
127 ((c->cfg->direction == MOST_CH_TX) &&
128 ((filp->f_flags & O_ACCMODE) != O_WRONLY))) {
129 pr_info("WARN: Access flags mismatch\n");
133 mutex_lock(&c->io_mutex);
135 pr_info("WARN: Device is destroyed\n");
136 mutex_unlock(&c->io_mutex);
141 pr_info("WARN: Device is busy\n");
142 mutex_unlock(&c->io_mutex);
147 ret = most_start_channel(c->iface, c->channel_id, &cdev_aim);
150 mutex_unlock(&c->io_mutex);
155 * aim_close - implements the syscall to close the device
156 * @inode: inode pointer
157 * @filp: file pointer
159 * This stops the channel within the core.
161 static int aim_close(struct inode *inode, struct file *filp)
163 struct aim_channel *c = to_channel(inode->i_cdev);
165 mutex_lock(&c->io_mutex);
166 spin_lock(&c->unlink);
168 spin_unlock(&c->unlink);
171 mutex_unlock(&c->io_mutex);
174 mutex_unlock(&c->io_mutex);
181 * aim_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 aim_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 aim_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 * aim_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 aim_read(struct file *filp, char __user *buf, size_t count, loff_t *offset)
244 size_t to_copy, not_copied, copied;
246 struct aim_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 unsigned int aim_poll(struct file *filp, poll_table *wait)
288 struct aim_channel *c = filp->private_data;
289 unsigned int mask = 0;
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 = aim_close,
316 * aim_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 aim_disconnect_channel(struct most_interface *iface, int channel_id)
325 struct aim_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);
342 wake_up_interruptible(&c->wq);
343 mutex_unlock(&c->io_mutex);
346 mutex_unlock(&c->io_mutex);
353 * aim_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 aim_rx_completion(struct mbo *mbo)
361 struct aim_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 * aim_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 aim_tx_completion(struct most_interface *iface, int channel_id)
394 struct aim_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 * aim_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 * @parent: pointer to kobject (needed for sysfs hook-up)
418 * @name: name of the device to be created
420 * This allocates achannel object and creates the device node in /dev
422 * Returns 0 on success or error code otherwise.
424 static int aim_probe(struct most_interface *iface, int channel_id,
425 struct most_channel_config *cfg,
426 struct kobject *parent, char *name)
428 struct aim_channel *c;
429 unsigned long cl_flags;
433 if ((!iface) || (!cfg) || (!parent) || (!name)) {
434 pr_info("Probing AIM with bad arguments");
437 c = get_channel(iface, channel_id);
441 current_minor = ida_simple_get(&minor_id, 0, 0, GFP_KERNEL);
442 if (current_minor < 0)
443 return current_minor;
445 c = kzalloc(sizeof(*c), GFP_KERNEL);
448 goto error_alloc_channel;
451 c->devno = MKDEV(major, current_minor);
452 cdev_init(&c->cdev, &channel_fops);
453 c->cdev.owner = THIS_MODULE;
454 retval = cdev_add(&c->cdev, c->devno, 1);
459 c->channel_id = channel_id;
461 spin_lock_init(&c->unlink);
463 retval = kfifo_alloc(&c->fifo, cfg->num_buffers, GFP_KERNEL);
465 pr_info("failed to alloc channel kfifo");
466 goto error_alloc_kfifo;
468 init_waitqueue_head(&c->wq);
469 mutex_init(&c->io_mutex);
470 spin_lock_irqsave(&ch_list_lock, cl_flags);
471 list_add_tail(&c->list, &channel_list);
472 spin_unlock_irqrestore(&ch_list_lock, cl_flags);
473 c->dev = device_create(aim_class,
479 if (IS_ERR(c->dev)) {
480 retval = PTR_ERR(c->dev);
481 pr_info("failed to create new device node %s\n", name);
482 goto error_create_device;
484 kobject_uevent(&c->dev->kobj, KOBJ_ADD);
488 kfifo_free(&c->fifo);
495 ida_simple_remove(&minor_id, current_minor);
499 static struct most_aim cdev_aim = {
501 .probe_channel = aim_probe,
502 .disconnect_channel = aim_disconnect_channel,
503 .rx_completion = aim_rx_completion,
504 .tx_completion = aim_tx_completion,
507 static int __init mod_init(void)
513 INIT_LIST_HEAD(&channel_list);
514 spin_lock_init(&ch_list_lock);
517 err = alloc_chrdev_region(&aim_devno, 0, 50, "cdev");
520 major = MAJOR(aim_devno);
522 aim_class = class_create(THIS_MODULE, "most_cdev_aim");
523 if (IS_ERR(aim_class)) {
524 pr_err("no udev support\n");
525 err = PTR_ERR(aim_class);
528 err = most_register_aim(&cdev_aim);
534 class_destroy(aim_class);
536 unregister_chrdev_region(aim_devno, 1);
538 ida_destroy(&minor_id);
542 static void __exit mod_exit(void)
544 struct aim_channel *c, *tmp;
546 pr_info("exit module\n");
548 most_deregister_aim(&cdev_aim);
550 list_for_each_entry_safe(c, tmp, &channel_list, list) {
554 class_destroy(aim_class);
555 unregister_chrdev_region(aim_devno, 1);
556 ida_destroy(&minor_id);
559 module_init(mod_init);
560 module_exit(mod_exit);
561 MODULE_AUTHOR("Christian Gromm <christian.gromm@microchip.com>");
562 MODULE_LICENSE("GPL");
563 MODULE_DESCRIPTION("character device AIM for mostcore");