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;
35 struct mutex io_mutex;
36 struct most_interface *iface;
37 struct most_channel_config *cfg;
38 unsigned int channel_id;
41 unsigned int mbo_offs;
42 struct mbo *stacked_mbo;
43 DECLARE_KFIFO_PTR(fifo, typeof(struct mbo *));
45 struct list_head list;
47 #define to_channel(d) container_of(d, struct aim_channel, cdev)
48 static struct list_head channel_list;
49 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);
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 == true)
137 most_put_mbo(channel->stacked_mbo);
138 ret = most_stop_channel(channel->iface, channel->channel_id);
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);
170 if (!mbo && channel->dev) {
171 if ((filp->f_flags & O_NONBLOCK))
173 if (wait_event_interruptible(
175 (mbo = most_get_mbo(channel->iface,
176 channel->channel_id)) ||
177 (channel->dev == NULL)))
181 mutex_lock(&channel->io_mutex);
182 if (unlikely(!channel->dev)) {
183 mutex_unlock(&channel->io_mutex);
187 mutex_unlock(&channel->io_mutex);
189 max_len = channel->cfg->buffer_size;
190 actual_len = min(count, max_len);
191 mbo->buffer_length = actual_len;
193 retval = copy_from_user(mbo->virt_address, buf, mbo->buffer_length);
199 ret = most_submit_mbo(mbo);
201 pr_info("submitting MBO to core failed\n");
205 return actual_len - retval;
213 * aim_read - implements the syscall to read from the device
214 * @filp: file pointer
215 * @buf: pointer to user buffer
216 * @count: number of bytes to read
217 * @offset: offset from where to start reading
220 aim_read(struct file *filp, char __user *buf, size_t count, loff_t *offset)
223 size_t not_copied, proc_len;
225 struct aim_channel *channel = filp->private_data;
227 if (channel->keep_mbo == true) {
228 mbo = channel->stacked_mbo;
229 channel->keep_mbo = false;
232 while ((0 == kfifo_out(&channel->fifo, &mbo, 1))
233 && (channel->dev != NULL)) {
234 if (filp->f_flags & O_NONBLOCK)
236 if (wait_event_interruptible(channel->wq,
237 (!kfifo_is_empty(&channel->fifo) ||
238 (channel->dev == NULL))))
243 /* make sure we don't submit to gone devices */
244 mutex_lock(&channel->io_mutex);
245 if (unlikely(!channel->dev)) {
246 mutex_unlock(&channel->io_mutex);
250 if (count < mbo->processed_length)
251 channel->keep_mbo = true;
253 proc_len = min((int)count,
254 (int)(mbo->processed_length - channel->mbo_offs));
256 not_copied = copy_to_user(buf,
257 mbo->virt_address + channel->mbo_offs,
260 retval = not_copied ? proc_len - not_copied : proc_len;
262 if (channel->keep_mbo == true) {
263 channel->mbo_offs = retval;
264 channel->stacked_mbo = mbo;
267 channel->mbo_offs = 0;
269 mutex_unlock(&channel->io_mutex);
274 * Initialization of struct file_operations
276 static const struct file_operations channel_fops = {
277 .owner = THIS_MODULE,
281 .release = aim_close,
285 * aim_disconnect_channel - disconnect a channel
286 * @iface: pointer to interface instance
287 * @channel_id: channel index
289 * This frees allocated memory and removes the cdev that represents this
290 * channel in user space.
292 static int aim_disconnect_channel(struct most_interface *iface, int channel_id)
294 struct aim_channel *channel;
298 pr_info("Bad interface pointer\n");
302 channel = get_channel(iface, channel_id);
306 mutex_lock(&channel->io_mutex);
308 mutex_unlock(&channel->io_mutex);
310 if (atomic_read(&channel->access_ref)) {
311 device_destroy(aim_class, channel->devno);
312 cdev_del(&channel->cdev);
313 kfifo_free(&channel->fifo);
314 ida_simple_remove(&minor_id, MINOR(channel->devno));
315 spin_lock_irqsave(&ch_list_lock, flags);
316 list_del(&channel->list);
317 spin_unlock_irqrestore(&ch_list_lock, flags);
320 wake_up_interruptible(&channel->wq);
326 * aim_rx_completion - completion handler for rx channels
327 * @mbo: pointer to buffer object that has completed
329 * This searches for the channel linked to this MBO and stores it in the local
332 static int aim_rx_completion(struct mbo *mbo)
334 struct aim_channel *channel;
339 channel = get_channel(mbo->ifp, mbo->hdm_channel_id);
343 kfifo_in(&channel->fifo, &mbo, 1);
345 if (kfifo_is_full(&channel->fifo))
346 pr_info("WARN: Fifo is full\n");
348 wake_up_interruptible(&channel->wq);
353 * aim_tx_completion - completion handler for tx channels
354 * @iface: pointer to interface instance
355 * @channel_id: channel index/ID
357 * This wakes sleeping processes in the wait-queue.
359 static int aim_tx_completion(struct most_interface *iface, int channel_id)
361 struct aim_channel *channel;
364 pr_info("Bad interface pointer\n");
367 if ((channel_id < 0) || (channel_id >= iface->num_channels)) {
368 pr_info("Channel ID out of range\n");
372 channel = get_channel(iface, channel_id);
375 wake_up_interruptible(&channel->wq);
379 static struct most_aim cdev_aim;
382 * aim_probe - probe function of the driver module
383 * @iface: pointer to interface instance
384 * @channel_id: channel index/ID
385 * @cfg: pointer to actual channel configuration
386 * @parent: pointer to kobject (needed for sysfs hook-up)
387 * @name: name of the device to be created
389 * This allocates achannel object and creates the device node in /dev
391 * Returns 0 on success or error code otherwise.
393 static int aim_probe(struct most_interface *iface, int channel_id,
394 struct most_channel_config *cfg,
395 struct kobject *parent, char *name)
397 struct aim_channel *channel;
398 unsigned long cl_flags;
402 if ((!iface) || (!cfg) || (!parent) || (!name)) {
403 pr_info("Probing AIM with bad arguments");
406 channel = get_channel(iface, channel_id);
410 current_minor = ida_simple_get(&minor_id, 0, 0, GFP_KERNEL);
411 if (current_minor < 0)
412 return current_minor;
414 channel = kzalloc(sizeof(*channel), GFP_KERNEL);
416 pr_info("failed to alloc channel object\n");
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");