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 *mbo = most_get_mbo(c->iface, c->channel_id, &cdev_aim);
64 static struct aim_channel *get_channel(struct most_interface *iface, int id)
66 struct aim_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 aim_channel *c)
87 while (kfifo_out((struct kfifo *)&c->fifo, &mbo, 1))
89 most_stop_channel(c->iface, c->channel_id, &cdev_aim);
92 static void destroy_cdev(struct aim_channel *c)
96 device_destroy(aim_class, c->devno);
99 spin_lock_irqsave(&ch_list_lock, flags);
101 spin_unlock_irqrestore(&ch_list_lock, flags);
102 ida_simple_remove(&minor_id, MINOR(c->devno));
106 * aim_open - implements the syscall to open the device
107 * @inode: inode pointer
108 * @filp: file pointer
110 * This stores the channel pointer in the private data field of
111 * the file structure and activates the channel within the core.
113 static int aim_open(struct inode *inode, struct file *filp)
115 struct aim_channel *c;
118 c = to_channel(inode->i_cdev);
119 filp->private_data = c;
121 if (((c->cfg->direction == MOST_CH_RX) &&
122 ((filp->f_flags & O_ACCMODE) != O_RDONLY)) ||
123 ((c->cfg->direction == MOST_CH_TX) &&
124 ((filp->f_flags & O_ACCMODE) != O_WRONLY))) {
125 pr_info("WARN: Access flags mismatch\n");
129 mutex_lock(&c->io_mutex);
131 pr_info("WARN: Device is destroyed\n");
132 mutex_unlock(&c->io_mutex);
137 pr_info("WARN: Device is busy\n");
138 mutex_unlock(&c->io_mutex);
143 ret = most_start_channel(c->iface, c->channel_id, &cdev_aim);
146 mutex_unlock(&c->io_mutex);
151 * aim_close - implements the syscall to close the device
152 * @inode: inode pointer
153 * @filp: file pointer
155 * This stops the channel within the core.
157 static int aim_close(struct inode *inode, struct file *filp)
159 struct aim_channel *c = to_channel(inode->i_cdev);
161 mutex_lock(&c->io_mutex);
162 spin_lock(&c->unlink);
164 spin_unlock(&c->unlink);
167 mutex_unlock(&c->io_mutex);
170 mutex_unlock(&c->io_mutex);
177 * aim_write - implements the syscall to write to the device
178 * @filp: file pointer
179 * @buf: pointer to user buffer
180 * @count: number of bytes to write
181 * @offset: offset from where to start writing
183 static ssize_t aim_write(struct file *filp, const char __user *buf,
184 size_t count, loff_t *offset)
189 struct mbo *mbo = NULL;
190 struct aim_channel *c = filp->private_data;
192 mutex_lock(&c->io_mutex);
193 while (c->dev && !ch_get_mbo(c, &mbo)) {
194 mutex_unlock(&c->io_mutex);
196 if ((filp->f_flags & O_NONBLOCK))
198 if (wait_event_interruptible(c->wq, ch_has_mbo(c) || !c->dev))
200 mutex_lock(&c->io_mutex);
203 if (unlikely(!c->dev)) {
208 max_len = c->cfg->buffer_size;
209 actual_len = min(count, max_len);
210 mbo->buffer_length = actual_len;
212 if (copy_from_user(mbo->virt_address, buf, mbo->buffer_length)) {
217 ret = most_submit_mbo(mbo);
221 mutex_unlock(&c->io_mutex);
226 mutex_unlock(&c->io_mutex);
231 * aim_read - implements the syscall to read from the device
232 * @filp: file pointer
233 * @buf: pointer to user buffer
234 * @count: number of bytes to read
235 * @offset: offset from where to start reading
238 aim_read(struct file *filp, char __user *buf, size_t count, loff_t *offset)
240 size_t to_copy, not_copied, copied;
242 struct aim_channel *c = filp->private_data;
244 mutex_lock(&c->io_mutex);
245 while (c->dev && !kfifo_peek(&c->fifo, &mbo)) {
246 mutex_unlock(&c->io_mutex);
247 if (filp->f_flags & O_NONBLOCK)
249 if (wait_event_interruptible(c->wq,
250 (!kfifo_is_empty(&c->fifo) ||
253 mutex_lock(&c->io_mutex);
256 /* make sure we don't submit to gone devices */
257 if (unlikely(!c->dev)) {
258 mutex_unlock(&c->io_mutex);
262 to_copy = min_t(size_t,
264 mbo->processed_length - c->mbo_offs);
266 not_copied = copy_to_user(buf,
267 mbo->virt_address + c->mbo_offs,
270 copied = to_copy - not_copied;
272 c->mbo_offs += copied;
273 if (c->mbo_offs >= mbo->processed_length) {
274 kfifo_skip(&c->fifo);
278 mutex_unlock(&c->io_mutex);
282 static unsigned int aim_poll(struct file *filp, poll_table *wait)
284 struct aim_channel *c = filp->private_data;
285 unsigned int mask = 0;
287 poll_wait(filp, &c->wq, wait);
289 if (c->cfg->direction == MOST_CH_RX) {
290 if (!kfifo_is_empty(&c->fifo))
291 mask |= POLLIN | POLLRDNORM;
294 mask |= POLLOUT | POLLWRNORM;
300 * Initialization of struct file_operations
302 static const struct file_operations channel_fops = {
303 .owner = THIS_MODULE,
307 .release = aim_close,
312 * aim_disconnect_channel - disconnect a channel
313 * @iface: pointer to interface instance
314 * @channel_id: channel index
316 * This frees allocated memory and removes the cdev that represents this
317 * channel in user space.
319 static int aim_disconnect_channel(struct most_interface *iface, int channel_id)
321 struct aim_channel *c;
324 pr_info("Bad interface pointer\n");
328 c = get_channel(iface, channel_id);
332 mutex_lock(&c->io_mutex);
333 spin_lock(&c->unlink);
335 spin_unlock(&c->unlink);
338 wake_up_interruptible(&c->wq);
339 mutex_unlock(&c->io_mutex);
342 mutex_unlock(&c->io_mutex);
349 * aim_rx_completion - completion handler for rx channels
350 * @mbo: pointer to buffer object that has completed
352 * This searches for the channel linked to this MBO and stores it in the local
355 static int aim_rx_completion(struct mbo *mbo)
357 struct aim_channel *c;
362 c = get_channel(mbo->ifp, mbo->hdm_channel_id);
366 spin_lock(&c->unlink);
367 if (!c->access_ref || !c->dev) {
368 spin_unlock(&c->unlink);
371 kfifo_in(&c->fifo, &mbo, 1);
372 spin_unlock(&c->unlink);
374 if (kfifo_is_full(&c->fifo))
375 pr_info("WARN: Fifo is full\n");
377 wake_up_interruptible(&c->wq);
382 * aim_tx_completion - completion handler for tx channels
383 * @iface: pointer to interface instance
384 * @channel_id: channel index/ID
386 * This wakes sleeping processes in the wait-queue.
388 static int aim_tx_completion(struct most_interface *iface, int channel_id)
390 struct aim_channel *c;
393 pr_info("Bad interface pointer\n");
396 if ((channel_id < 0) || (channel_id >= iface->num_channels)) {
397 pr_info("Channel ID out of range\n");
401 c = get_channel(iface, channel_id);
404 wake_up_interruptible(&c->wq);
409 * aim_probe - probe function of the driver module
410 * @iface: pointer to interface instance
411 * @channel_id: channel index/ID
412 * @cfg: pointer to actual channel configuration
413 * @parent: pointer to kobject (needed for sysfs hook-up)
414 * @name: name of the device to be created
416 * This allocates achannel object and creates the device node in /dev
418 * Returns 0 on success or error code otherwise.
420 static int aim_probe(struct most_interface *iface, int channel_id,
421 struct most_channel_config *cfg,
422 struct kobject *parent, char *name)
424 struct aim_channel *c;
425 unsigned long cl_flags;
429 if ((!iface) || (!cfg) || (!parent) || (!name)) {
430 pr_info("Probing AIM with bad arguments");
433 c = get_channel(iface, channel_id);
437 current_minor = ida_simple_get(&minor_id, 0, 0, GFP_KERNEL);
438 if (current_minor < 0)
439 return current_minor;
441 c = kzalloc(sizeof(*c), GFP_KERNEL);
444 goto error_alloc_channel;
447 c->devno = MKDEV(major, current_minor);
448 cdev_init(&c->cdev, &channel_fops);
449 c->cdev.owner = THIS_MODULE;
450 cdev_add(&c->cdev, c->devno, 1);
453 c->channel_id = channel_id;
455 spin_lock_init(&c->unlink);
457 retval = kfifo_alloc(&c->fifo, cfg->num_buffers, GFP_KERNEL);
459 pr_info("failed to alloc channel kfifo");
460 goto error_alloc_kfifo;
462 init_waitqueue_head(&c->wq);
463 mutex_init(&c->io_mutex);
464 spin_lock_irqsave(&ch_list_lock, cl_flags);
465 list_add_tail(&c->list, &channel_list);
466 spin_unlock_irqrestore(&ch_list_lock, cl_flags);
467 c->dev = device_create(aim_class,
473 retval = IS_ERR(c->dev);
475 pr_info("failed to create new device node %s\n", name);
476 goto error_create_device;
478 kobject_uevent(&c->dev->kobj, KOBJ_ADD);
482 kfifo_free(&c->fifo);
488 ida_simple_remove(&minor_id, current_minor);
492 static struct most_aim cdev_aim = {
494 .probe_channel = aim_probe,
495 .disconnect_channel = aim_disconnect_channel,
496 .rx_completion = aim_rx_completion,
497 .tx_completion = aim_tx_completion,
500 static int __init mod_init(void)
504 INIT_LIST_HEAD(&channel_list);
505 spin_lock_init(&ch_list_lock);
508 if (alloc_chrdev_region(&aim_devno, 0, 50, "cdev") < 0)
510 major = MAJOR(aim_devno);
512 aim_class = class_create(THIS_MODULE, "most_cdev_aim");
513 if (IS_ERR(aim_class)) {
514 pr_err("no udev support\n");
518 if (most_register_aim(&cdev_aim))
523 class_destroy(aim_class);
525 unregister_chrdev_region(aim_devno, 1);
529 static void __exit mod_exit(void)
531 struct aim_channel *c, *tmp;
533 pr_info("exit module\n");
535 most_deregister_aim(&cdev_aim);
537 list_for_each_entry_safe(c, tmp, &channel_list, list) {
541 class_destroy(aim_class);
542 unregister_chrdev_region(aim_devno, 1);
543 ida_destroy(&minor_id);
546 module_init(mod_init);
547 module_exit(mod_exit);
548 MODULE_AUTHOR("Christian Gromm <christian.gromm@microchip.com>");
549 MODULE_LICENSE("GPL");
550 MODULE_DESCRIPTION("character device AIM for mostcore");