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/kfifo.h>
22 #include <linux/uaccess.h>
23 #include <linux/idr.h>
26 static dev_t aim_devno;
27 static struct class *aim_class;
28 static struct ida minor_id;
29 static unsigned int major;
30 static struct most_aim cdev_aim;
36 struct mutex io_mutex;
37 struct most_interface *iface;
38 struct most_channel_config *cfg;
39 unsigned int channel_id;
42 unsigned int mbo_offs;
43 struct mbo *stacked_mbo;
44 DECLARE_KFIFO_PTR(fifo, typeof(struct mbo *));
46 struct list_head list;
48 #define to_channel(d) container_of(d, struct aim_channel, cdev)
49 static struct list_head channel_list;
50 static spinlock_t ch_list_lock;
52 static struct aim_channel *get_channel(struct most_interface *iface, int id)
54 struct aim_channel *channel, *tmp;
56 int found_channel = 0;
58 spin_lock_irqsave(&ch_list_lock, flags);
59 list_for_each_entry_safe(channel, tmp, &channel_list, list) {
60 if ((channel->iface == iface) && (channel->channel_id == id)) {
65 spin_unlock_irqrestore(&ch_list_lock, flags);
72 * aim_open - implements the syscall to open the device
73 * @inode: inode pointer
76 * This stores the channel pointer in the private data field of
77 * the file structure and activates the channel within the core.
79 static int aim_open(struct inode *inode, struct file *filp)
81 struct aim_channel *channel;
84 channel = to_channel(inode->i_cdev);
85 filp->private_data = channel;
87 if (((channel->cfg->direction == MOST_CH_RX) &&
88 ((filp->f_flags & O_ACCMODE) != O_RDONLY))
89 || ((channel->cfg->direction == MOST_CH_TX) &&
90 ((filp->f_flags & O_ACCMODE) != O_WRONLY))) {
91 pr_info("WARN: Access flags mismatch\n");
94 if (!atomic_inc_and_test(&channel->access_ref)) {
95 pr_info("WARN: Device is busy\n");
96 atomic_dec(&channel->access_ref);
100 ret = most_start_channel(channel->iface, channel->channel_id, &cdev_aim);
102 atomic_dec(&channel->access_ref);
107 * aim_close - implements the syscall to close the device
108 * @inode: inode pointer
109 * @filp: file pointer
111 * This stops the channel within the core.
113 static int aim_close(struct inode *inode, struct file *filp)
117 struct aim_channel *channel = to_channel(inode->i_cdev);
119 mutex_lock(&channel->io_mutex);
121 mutex_unlock(&channel->io_mutex);
122 atomic_dec(&channel->access_ref);
123 device_destroy(aim_class, channel->devno);
124 cdev_del(&channel->cdev);
125 kfifo_free(&channel->fifo);
126 list_del(&channel->list);
127 ida_simple_remove(&minor_id, MINOR(channel->devno));
128 wake_up_interruptible(&channel->wq);
132 mutex_unlock(&channel->io_mutex);
134 while (0 != kfifo_out((struct kfifo *)&channel->fifo, &mbo, 1))
136 if (channel->keep_mbo)
137 most_put_mbo(channel->stacked_mbo);
138 ret = most_stop_channel(channel->iface, channel->channel_id, &cdev_aim);
139 atomic_dec(&channel->access_ref);
140 wake_up_interruptible(&channel->wq);
145 * aim_write - implements the syscall to write to the device
146 * @filp: file pointer
147 * @buf: pointer to user buffer
148 * @count: number of bytes to write
149 * @offset: offset from where to start writing
151 static ssize_t aim_write(struct file *filp, const char __user *buf,
152 size_t count, loff_t *offset)
155 size_t actual_len = 0;
159 struct aim_channel *channel = filp->private_data;
161 mutex_lock(&channel->io_mutex);
162 if (unlikely(!channel->dev)) {
163 mutex_unlock(&channel->io_mutex);
166 mutex_unlock(&channel->io_mutex);
168 mbo = most_get_mbo(channel->iface, channel->channel_id, &cdev_aim);
171 if ((filp->f_flags & O_NONBLOCK))
173 if (wait_event_interruptible(
175 (mbo = most_get_mbo(channel->iface,
182 mutex_lock(&channel->io_mutex);
183 if (unlikely(!channel->dev)) {
184 mutex_unlock(&channel->io_mutex);
188 mutex_unlock(&channel->io_mutex);
190 max_len = channel->cfg->buffer_size;
191 actual_len = min(count, max_len);
192 mbo->buffer_length = actual_len;
194 retval = copy_from_user(mbo->virt_address, buf, mbo->buffer_length);
200 ret = most_submit_mbo(mbo);
202 pr_info("submitting MBO to core failed\n");
206 return actual_len - retval;
214 * aim_read - implements the syscall to read from the device
215 * @filp: file pointer
216 * @buf: pointer to user buffer
217 * @count: number of bytes to read
218 * @offset: offset from where to start reading
221 aim_read(struct file *filp, char __user *buf, size_t count, loff_t *offset)
224 size_t not_copied, proc_len;
226 struct aim_channel *channel = filp->private_data;
228 if (channel->keep_mbo) {
229 mbo = channel->stacked_mbo;
230 channel->keep_mbo = false;
233 while ((0 == kfifo_out(&channel->fifo, &mbo, 1))
235 if (filp->f_flags & O_NONBLOCK)
237 if (wait_event_interruptible(channel->wq,
238 (!kfifo_is_empty(&channel->fifo) ||
244 /* make sure we don't submit to gone devices */
245 mutex_lock(&channel->io_mutex);
246 if (unlikely(!channel->dev)) {
247 mutex_unlock(&channel->io_mutex);
251 if (count < mbo->processed_length)
252 channel->keep_mbo = true;
254 proc_len = min((int)count,
255 (int)(mbo->processed_length - channel->mbo_offs));
257 not_copied = copy_to_user(buf,
258 mbo->virt_address + channel->mbo_offs,
261 retval = not_copied ? proc_len - not_copied : proc_len;
263 if (channel->keep_mbo) {
264 channel->mbo_offs = retval;
265 channel->stacked_mbo = mbo;
268 channel->mbo_offs = 0;
270 mutex_unlock(&channel->io_mutex);
275 * Initialization of struct file_operations
277 static const struct file_operations channel_fops = {
278 .owner = THIS_MODULE,
282 .release = aim_close,
286 * aim_disconnect_channel - disconnect a channel
287 * @iface: pointer to interface instance
288 * @channel_id: channel index
290 * This frees allocated memory and removes the cdev that represents this
291 * channel in user space.
293 static int aim_disconnect_channel(struct most_interface *iface, int channel_id)
295 struct aim_channel *channel;
299 pr_info("Bad interface pointer\n");
303 channel = get_channel(iface, channel_id);
307 mutex_lock(&channel->io_mutex);
309 mutex_unlock(&channel->io_mutex);
311 if (atomic_read(&channel->access_ref)) {
312 device_destroy(aim_class, channel->devno);
313 cdev_del(&channel->cdev);
314 kfifo_free(&channel->fifo);
315 ida_simple_remove(&minor_id, MINOR(channel->devno));
316 spin_lock_irqsave(&ch_list_lock, flags);
317 list_del(&channel->list);
318 spin_unlock_irqrestore(&ch_list_lock, flags);
321 wake_up_interruptible(&channel->wq);
327 * aim_rx_completion - completion handler for rx channels
328 * @mbo: pointer to buffer object that has completed
330 * This searches for the channel linked to this MBO and stores it in the local
333 static int aim_rx_completion(struct mbo *mbo)
335 struct aim_channel *channel;
340 channel = get_channel(mbo->ifp, mbo->hdm_channel_id);
344 kfifo_in(&channel->fifo, &mbo, 1);
346 if (kfifo_is_full(&channel->fifo))
347 pr_info("WARN: Fifo is full\n");
349 wake_up_interruptible(&channel->wq);
354 * aim_tx_completion - completion handler for tx channels
355 * @iface: pointer to interface instance
356 * @channel_id: channel index/ID
358 * This wakes sleeping processes in the wait-queue.
360 static int aim_tx_completion(struct most_interface *iface, int channel_id)
362 struct aim_channel *channel;
365 pr_info("Bad interface pointer\n");
368 if ((channel_id < 0) || (channel_id >= iface->num_channels)) {
369 pr_info("Channel ID out of range\n");
373 channel = get_channel(iface, channel_id);
376 wake_up_interruptible(&channel->wq);
380 static struct most_aim cdev_aim;
383 * aim_probe - probe function of the driver module
384 * @iface: pointer to interface instance
385 * @channel_id: channel index/ID
386 * @cfg: pointer to actual channel configuration
387 * @parent: pointer to kobject (needed for sysfs hook-up)
388 * @name: name of the device to be created
390 * This allocates achannel object and creates the device node in /dev
392 * Returns 0 on success or error code otherwise.
394 static int aim_probe(struct most_interface *iface, int channel_id,
395 struct most_channel_config *cfg,
396 struct kobject *parent, char *name)
398 struct aim_channel *channel;
399 unsigned long cl_flags;
403 if ((!iface) || (!cfg) || (!parent) || (!name)) {
404 pr_info("Probing AIM with bad arguments");
407 channel = get_channel(iface, channel_id);
411 current_minor = ida_simple_get(&minor_id, 0, 0, GFP_KERNEL);
412 if (current_minor < 0)
413 return current_minor;
415 channel = kzalloc(sizeof(*channel), GFP_KERNEL);
418 goto error_alloc_channel;
421 channel->devno = MKDEV(major, current_minor);
422 cdev_init(&channel->cdev, &channel_fops);
423 channel->cdev.owner = THIS_MODULE;
424 cdev_add(&channel->cdev, channel->devno, 1);
425 channel->iface = iface;
427 channel->channel_id = channel_id;
428 channel->mbo_offs = 0;
429 atomic_set(&channel->access_ref, -1);
430 INIT_KFIFO(channel->fifo);
431 retval = kfifo_alloc(&channel->fifo, cfg->num_buffers, GFP_KERNEL);
433 pr_info("failed to alloc channel kfifo");
434 goto error_alloc_kfifo;
436 init_waitqueue_head(&channel->wq);
437 mutex_init(&channel->io_mutex);
438 spin_lock_irqsave(&ch_list_lock, cl_flags);
439 list_add_tail(&channel->list, &channel_list);
440 spin_unlock_irqrestore(&ch_list_lock, cl_flags);
441 channel->dev = device_create(aim_class,
447 retval = IS_ERR(channel->dev);
449 pr_info("failed to create new device node %s\n", name);
450 goto error_create_device;
452 kobject_uevent(&channel->dev->kobj, KOBJ_ADD);
456 kfifo_free(&channel->fifo);
457 list_del(&channel->list);
459 cdev_del(&channel->cdev);
462 ida_simple_remove(&minor_id, current_minor);
466 static struct most_aim cdev_aim = {
468 .probe_channel = aim_probe,
469 .disconnect_channel = aim_disconnect_channel,
470 .rx_completion = aim_rx_completion,
471 .tx_completion = aim_tx_completion,
474 static int __init mod_init(void)
478 INIT_LIST_HEAD(&channel_list);
479 spin_lock_init(&ch_list_lock);
482 if (alloc_chrdev_region(&aim_devno, 0, 50, "cdev") < 0)
484 major = MAJOR(aim_devno);
486 aim_class = class_create(THIS_MODULE, "most_cdev_aim");
487 if (IS_ERR(aim_class)) {
488 pr_err("no udev support\n");
492 if (most_register_aim(&cdev_aim))
497 class_destroy(aim_class);
499 unregister_chrdev_region(aim_devno, 1);
503 static void __exit mod_exit(void)
505 struct aim_channel *channel, *tmp;
507 pr_info("exit module\n");
509 most_deregister_aim(&cdev_aim);
511 list_for_each_entry_safe(channel, tmp, &channel_list, list) {
512 device_destroy(aim_class, channel->devno);
513 cdev_del(&channel->cdev);
514 kfifo_free(&channel->fifo);
515 list_del(&channel->list);
516 ida_simple_remove(&minor_id, MINOR(channel->devno));
519 class_destroy(aim_class);
520 unregister_chrdev_region(aim_devno, 1);
521 ida_destroy(&minor_id);
524 module_init(mod_init);
525 module_exit(mod_exit);
526 MODULE_AUTHOR("Christian Gromm <christian.gromm@microchip.com>");
527 MODULE_LICENSE("GPL");
528 MODULE_DESCRIPTION("character device AIM for mostcore");