Greg Kroah-Hartman | 1a79f22 | 2017-11-07 14:58:50 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Christian Gromm | 9bc79bb | 2015-07-24 16:11:49 +0200 | [diff] [blame] | 2 | /* |
Christian Gromm | b7937dc | 2017-11-21 15:05:12 +0100 | [diff] [blame] | 3 | * cdev.c - Character device component for Mostcore |
Christian Gromm | 9bc79bb | 2015-07-24 16:11:49 +0200 | [diff] [blame] | 4 | * |
| 5 | * Copyright (C) 2013-2015 Microchip Technology Germany II GmbH & Co. KG |
Christian Gromm | 9bc79bb | 2015-07-24 16:11:49 +0200 | [diff] [blame] | 6 | */ |
| 7 | |
Christian Gromm | 9bc79bb | 2015-07-24 16:11:49 +0200 | [diff] [blame] | 8 | #include <linux/module.h> |
| 9 | #include <linux/sched.h> |
| 10 | #include <linux/fs.h> |
| 11 | #include <linux/slab.h> |
| 12 | #include <linux/device.h> |
| 13 | #include <linux/cdev.h> |
Christian Gromm | aac997d | 2015-09-28 17:18:57 +0200 | [diff] [blame] | 14 | #include <linux/poll.h> |
Christian Gromm | 9bc79bb | 2015-07-24 16:11:49 +0200 | [diff] [blame] | 15 | #include <linux/kfifo.h> |
| 16 | #include <linux/uaccess.h> |
| 17 | #include <linux/idr.h> |
Christian Gromm | b276527 | 2020-03-10 14:02:40 +0100 | [diff] [blame] | 18 | #include <linux/most.h> |
Christian Gromm | 9bc79bb | 2015-07-24 16:11:49 +0200 | [diff] [blame] | 19 | |
Christian Gromm | aba258b | 2018-05-08 11:45:06 +0200 | [diff] [blame] | 20 | #define CHRDEV_REGION_SIZE 50 |
| 21 | |
Christian Gromm | c73d915 | 2017-11-21 15:05:20 +0100 | [diff] [blame] | 22 | static struct cdev_component { |
| 23 | dev_t devno; |
| 24 | struct ida minor_id; |
| 25 | unsigned int major; |
| 26 | struct class *class; |
Christian Gromm | 45917e7 | 2019-12-13 13:04:15 +0100 | [diff] [blame] | 27 | struct most_component cc; |
Christian Gromm | c73d915 | 2017-11-21 15:05:20 +0100 | [diff] [blame] | 28 | } comp; |
Christian Gromm | 9bc79bb | 2015-07-24 16:11:49 +0200 | [diff] [blame] | 29 | |
Christian Gromm | ef0fbbb | 2017-11-21 15:05:08 +0100 | [diff] [blame] | 30 | struct comp_channel { |
Christian Gromm | 9bc79bb | 2015-07-24 16:11:49 +0200 | [diff] [blame] | 31 | wait_queue_head_t wq; |
Christian Gromm | fa96b8e | 2015-12-22 10:53:04 +0100 | [diff] [blame] | 32 | spinlock_t unlink; /* synchronization lock to unlink channels */ |
Christian Gromm | 9bc79bb | 2015-07-24 16:11:49 +0200 | [diff] [blame] | 33 | struct cdev cdev; |
| 34 | struct device *dev; |
| 35 | struct mutex io_mutex; |
| 36 | struct most_interface *iface; |
| 37 | struct most_channel_config *cfg; |
| 38 | unsigned int channel_id; |
| 39 | dev_t devno; |
Christian Gromm | 06e7ecf | 2015-12-22 10:52:45 +0100 | [diff] [blame] | 40 | size_t mbo_offs; |
Christian Gromm | 9bc79bb | 2015-07-24 16:11:49 +0200 | [diff] [blame] | 41 | DECLARE_KFIFO_PTR(fifo, typeof(struct mbo *)); |
Christian Gromm | b3c9f3c | 2015-12-22 10:53:05 +0100 | [diff] [blame] | 42 | int access_ref; |
Christian Gromm | 9bc79bb | 2015-07-24 16:11:49 +0200 | [diff] [blame] | 43 | struct list_head list; |
| 44 | }; |
Christian Gromm | 9cbe5aa | 2015-10-21 17:50:45 +0200 | [diff] [blame] | 45 | |
Christian Gromm | ef0fbbb | 2017-11-21 15:05:08 +0100 | [diff] [blame] | 46 | #define to_channel(d) container_of(d, struct comp_channel, cdev) |
Shixin Liu | 5751564 | 2021-03-29 17:40:15 +0800 | [diff] [blame] | 47 | static LIST_HEAD(channel_list); |
Shixin Liu | 2c4134e | 2021-03-29 17:40:14 +0800 | [diff] [blame] | 48 | static DEFINE_SPINLOCK(ch_list_lock); |
Christian Gromm | 9bc79bb | 2015-07-24 16:11:49 +0200 | [diff] [blame] | 49 | |
Christian Gromm | ef0fbbb | 2017-11-21 15:05:08 +0100 | [diff] [blame] | 50 | static inline bool ch_has_mbo(struct comp_channel *c) |
Christian Gromm | cdc293d | 2016-01-12 14:00:03 +0100 | [diff] [blame] | 51 | { |
Christian Gromm | c73d915 | 2017-11-21 15:05:20 +0100 | [diff] [blame] | 52 | return channel_has_mbo(c->iface, c->channel_id, &comp.cc) > 0; |
Christian Gromm | cdc293d | 2016-01-12 14:00:03 +0100 | [diff] [blame] | 53 | } |
| 54 | |
Christian Gromm | 7d56f62 | 2018-05-08 11:45:08 +0200 | [diff] [blame] | 55 | static inline struct mbo *ch_get_mbo(struct comp_channel *c, struct mbo **mbo) |
Christian Gromm | fa96b8e | 2015-12-22 10:53:04 +0100 | [diff] [blame] | 56 | { |
Christian Gromm | da2c087 | 2016-09-23 15:20:02 +0200 | [diff] [blame] | 57 | if (!kfifo_peek(&c->fifo, mbo)) { |
Christian Gromm | c73d915 | 2017-11-21 15:05:20 +0100 | [diff] [blame] | 58 | *mbo = most_get_mbo(c->iface, c->channel_id, &comp.cc); |
Christian Gromm | da2c087 | 2016-09-23 15:20:02 +0200 | [diff] [blame] | 59 | if (*mbo) |
| 60 | kfifo_in(&c->fifo, mbo, 1); |
| 61 | } |
Christian Gromm | fa96b8e | 2015-12-22 10:53:04 +0100 | [diff] [blame] | 62 | return *mbo; |
| 63 | } |
| 64 | |
Christian Gromm | ef0fbbb | 2017-11-21 15:05:08 +0100 | [diff] [blame] | 65 | static struct comp_channel *get_channel(struct most_interface *iface, int id) |
Christian Gromm | 9bc79bb | 2015-07-24 16:11:49 +0200 | [diff] [blame] | 66 | { |
Christian Gromm | ef0fbbb | 2017-11-21 15:05:08 +0100 | [diff] [blame] | 67 | struct comp_channel *c, *tmp; |
Christian Gromm | 9bc79bb | 2015-07-24 16:11:49 +0200 | [diff] [blame] | 68 | unsigned long flags; |
Christian Gromm | 9bc79bb | 2015-07-24 16:11:49 +0200 | [diff] [blame] | 69 | |
| 70 | spin_lock_irqsave(&ch_list_lock, flags); |
Christian Gromm | d8b082e | 2015-12-22 10:53:03 +0100 | [diff] [blame] | 71 | list_for_each_entry_safe(c, tmp, &channel_list, list) { |
| 72 | if ((c->iface == iface) && (c->channel_id == id)) { |
Christian Gromm | 0ae9e4f | 2020-06-22 10:48:15 +0200 | [diff] [blame] | 73 | spin_unlock_irqrestore(&ch_list_lock, flags); |
| 74 | return c; |
Christian Gromm | 9bc79bb | 2015-07-24 16:11:49 +0200 | [diff] [blame] | 75 | } |
| 76 | } |
| 77 | spin_unlock_irqrestore(&ch_list_lock, flags); |
Christian Gromm | 0ae9e4f | 2020-06-22 10:48:15 +0200 | [diff] [blame] | 78 | return NULL; |
Christian Gromm | 9bc79bb | 2015-07-24 16:11:49 +0200 | [diff] [blame] | 79 | } |
| 80 | |
Christian Gromm | ef0fbbb | 2017-11-21 15:05:08 +0100 | [diff] [blame] | 81 | static void stop_channel(struct comp_channel *c) |
Christian Gromm | 5f858a6 | 2015-12-22 10:53:01 +0100 | [diff] [blame] | 82 | { |
| 83 | struct mbo *mbo; |
| 84 | |
| 85 | while (kfifo_out((struct kfifo *)&c->fifo, &mbo, 1)) |
| 86 | most_put_mbo(mbo); |
Christian Gromm | c73d915 | 2017-11-21 15:05:20 +0100 | [diff] [blame] | 87 | most_stop_channel(c->iface, c->channel_id, &comp.cc); |
Christian Gromm | 5f858a6 | 2015-12-22 10:53:01 +0100 | [diff] [blame] | 88 | } |
| 89 | |
Christian Gromm | ef0fbbb | 2017-11-21 15:05:08 +0100 | [diff] [blame] | 90 | static void destroy_cdev(struct comp_channel *c) |
Christian Gromm | 5f858a6 | 2015-12-22 10:53:01 +0100 | [diff] [blame] | 91 | { |
| 92 | unsigned long flags; |
| 93 | |
Christian Gromm | c73d915 | 2017-11-21 15:05:20 +0100 | [diff] [blame] | 94 | device_destroy(comp.class, c->devno); |
Christian Gromm | 5f858a6 | 2015-12-22 10:53:01 +0100 | [diff] [blame] | 95 | cdev_del(&c->cdev); |
Christian Gromm | 5f858a6 | 2015-12-22 10:53:01 +0100 | [diff] [blame] | 96 | spin_lock_irqsave(&ch_list_lock, flags); |
| 97 | list_del(&c->list); |
| 98 | spin_unlock_irqrestore(&ch_list_lock, flags); |
Andrey Shvetsov | 2c4aaa1 | 2017-04-07 15:38:37 +0200 | [diff] [blame] | 99 | } |
| 100 | |
Christian Gromm | ef0fbbb | 2017-11-21 15:05:08 +0100 | [diff] [blame] | 101 | static void destroy_channel(struct comp_channel *c) |
Andrey Shvetsov | 2c4aaa1 | 2017-04-07 15:38:37 +0200 | [diff] [blame] | 102 | { |
Christian Gromm | c73d915 | 2017-11-21 15:05:20 +0100 | [diff] [blame] | 103 | ida_simple_remove(&comp.minor_id, MINOR(c->devno)); |
Andrey Shvetsov | 2c4aaa1 | 2017-04-07 15:38:37 +0200 | [diff] [blame] | 104 | kfifo_free(&c->fifo); |
| 105 | kfree(c); |
Christian Gromm | 5f858a6 | 2015-12-22 10:53:01 +0100 | [diff] [blame] | 106 | } |
| 107 | |
Christian Gromm | 9bc79bb | 2015-07-24 16:11:49 +0200 | [diff] [blame] | 108 | /** |
Christian Gromm | 1fd923f | 2017-11-21 15:05:19 +0100 | [diff] [blame] | 109 | * comp_open - implements the syscall to open the device |
Christian Gromm | 9bc79bb | 2015-07-24 16:11:49 +0200 | [diff] [blame] | 110 | * @inode: inode pointer |
| 111 | * @filp: file pointer |
| 112 | * |
| 113 | * This stores the channel pointer in the private data field of |
| 114 | * the file structure and activates the channel within the core. |
| 115 | */ |
Christian Gromm | 1fd923f | 2017-11-21 15:05:19 +0100 | [diff] [blame] | 116 | static int comp_open(struct inode *inode, struct file *filp) |
Christian Gromm | 9bc79bb | 2015-07-24 16:11:49 +0200 | [diff] [blame] | 117 | { |
Christian Gromm | ef0fbbb | 2017-11-21 15:05:08 +0100 | [diff] [blame] | 118 | struct comp_channel *c; |
Christian Gromm | 9bc79bb | 2015-07-24 16:11:49 +0200 | [diff] [blame] | 119 | int ret; |
| 120 | |
Christian Gromm | d8b082e | 2015-12-22 10:53:03 +0100 | [diff] [blame] | 121 | c = to_channel(inode->i_cdev); |
| 122 | filp->private_data = c; |
Christian Gromm | 9bc79bb | 2015-07-24 16:11:49 +0200 | [diff] [blame] | 123 | |
Christian Gromm | d8b082e | 2015-12-22 10:53:03 +0100 | [diff] [blame] | 124 | if (((c->cfg->direction == MOST_CH_RX) && |
Christian Gromm | 623d800 | 2015-10-21 17:50:48 +0200 | [diff] [blame] | 125 | ((filp->f_flags & O_ACCMODE) != O_RDONLY)) || |
Christian Gromm | d8b082e | 2015-12-22 10:53:03 +0100 | [diff] [blame] | 126 | ((c->cfg->direction == MOST_CH_TX) && |
Christian Gromm | 9bc79bb | 2015-07-24 16:11:49 +0200 | [diff] [blame] | 127 | ((filp->f_flags & O_ACCMODE) != O_WRONLY))) { |
Christian Gromm | 9bc79bb | 2015-07-24 16:11:49 +0200 | [diff] [blame] | 128 | return -EACCES; |
| 129 | } |
Christian Gromm | fa96b8e | 2015-12-22 10:53:04 +0100 | [diff] [blame] | 130 | |
| 131 | mutex_lock(&c->io_mutex); |
| 132 | if (!c->dev) { |
Christian Gromm | fa96b8e | 2015-12-22 10:53:04 +0100 | [diff] [blame] | 133 | mutex_unlock(&c->io_mutex); |
Christian Gromm | 9b28e14 | 2016-08-19 13:09:33 +0200 | [diff] [blame] | 134 | return -ENODEV; |
Christian Gromm | 9bc79bb | 2015-07-24 16:11:49 +0200 | [diff] [blame] | 135 | } |
| 136 | |
Christian Gromm | b3c9f3c | 2015-12-22 10:53:05 +0100 | [diff] [blame] | 137 | if (c->access_ref) { |
Christian Gromm | fa96b8e | 2015-12-22 10:53:04 +0100 | [diff] [blame] | 138 | mutex_unlock(&c->io_mutex); |
| 139 | return -EBUSY; |
| 140 | } |
| 141 | |
Christian Gromm | f45b0fb | 2015-12-22 10:53:06 +0100 | [diff] [blame] | 142 | c->mbo_offs = 0; |
Christian Gromm | c73d915 | 2017-11-21 15:05:20 +0100 | [diff] [blame] | 143 | ret = most_start_channel(c->iface, c->channel_id, &comp.cc); |
Christian Gromm | b3c9f3c | 2015-12-22 10:53:05 +0100 | [diff] [blame] | 144 | if (!ret) |
| 145 | c->access_ref = 1; |
Christian Gromm | fa96b8e | 2015-12-22 10:53:04 +0100 | [diff] [blame] | 146 | mutex_unlock(&c->io_mutex); |
Christian Gromm | 9bc79bb | 2015-07-24 16:11:49 +0200 | [diff] [blame] | 147 | return ret; |
| 148 | } |
| 149 | |
| 150 | /** |
Christian Gromm | 1fd923f | 2017-11-21 15:05:19 +0100 | [diff] [blame] | 151 | * comp_close - implements the syscall to close the device |
Christian Gromm | 9bc79bb | 2015-07-24 16:11:49 +0200 | [diff] [blame] | 152 | * @inode: inode pointer |
| 153 | * @filp: file pointer |
| 154 | * |
| 155 | * This stops the channel within the core. |
| 156 | */ |
Christian Gromm | 1fd923f | 2017-11-21 15:05:19 +0100 | [diff] [blame] | 157 | static int comp_close(struct inode *inode, struct file *filp) |
Christian Gromm | 9bc79bb | 2015-07-24 16:11:49 +0200 | [diff] [blame] | 158 | { |
Christian Gromm | ef0fbbb | 2017-11-21 15:05:08 +0100 | [diff] [blame] | 159 | struct comp_channel *c = to_channel(inode->i_cdev); |
Christian Gromm | 9bc79bb | 2015-07-24 16:11:49 +0200 | [diff] [blame] | 160 | |
Christian Gromm | d8b082e | 2015-12-22 10:53:03 +0100 | [diff] [blame] | 161 | mutex_lock(&c->io_mutex); |
Christian Gromm | fa96b8e | 2015-12-22 10:53:04 +0100 | [diff] [blame] | 162 | spin_lock(&c->unlink); |
Christian Gromm | b3c9f3c | 2015-12-22 10:53:05 +0100 | [diff] [blame] | 163 | c->access_ref = 0; |
Christian Gromm | fa96b8e | 2015-12-22 10:53:04 +0100 | [diff] [blame] | 164 | spin_unlock(&c->unlink); |
| 165 | if (c->dev) { |
| 166 | stop_channel(c); |
| 167 | mutex_unlock(&c->io_mutex); |
| 168 | } else { |
Christian Gromm | fa96b8e | 2015-12-22 10:53:04 +0100 | [diff] [blame] | 169 | mutex_unlock(&c->io_mutex); |
Andrey Shvetsov | 2c4aaa1 | 2017-04-07 15:38:37 +0200 | [diff] [blame] | 170 | destroy_channel(c); |
Christian Gromm | fa96b8e | 2015-12-22 10:53:04 +0100 | [diff] [blame] | 171 | } |
Christian Gromm | 5f858a6 | 2015-12-22 10:53:01 +0100 | [diff] [blame] | 172 | return 0; |
Christian Gromm | 9bc79bb | 2015-07-24 16:11:49 +0200 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | /** |
Christian Gromm | 1fd923f | 2017-11-21 15:05:19 +0100 | [diff] [blame] | 176 | * comp_write - implements the syscall to write to the device |
Christian Gromm | 9bc79bb | 2015-07-24 16:11:49 +0200 | [diff] [blame] | 177 | * @filp: file pointer |
| 178 | * @buf: pointer to user buffer |
| 179 | * @count: number of bytes to write |
| 180 | * @offset: offset from where to start writing |
| 181 | */ |
Christian Gromm | 1fd923f | 2017-11-21 15:05:19 +0100 | [diff] [blame] | 182 | static ssize_t comp_write(struct file *filp, const char __user *buf, |
| 183 | size_t count, loff_t *offset) |
Christian Gromm | 9bc79bb | 2015-07-24 16:11:49 +0200 | [diff] [blame] | 184 | { |
Christian Gromm | 5adf5dc | 2015-12-22 10:53:07 +0100 | [diff] [blame] | 185 | int ret; |
Christian Gromm | da2c087 | 2016-09-23 15:20:02 +0200 | [diff] [blame] | 186 | size_t to_copy, left; |
Christian Gromm | fa96b8e | 2015-12-22 10:53:04 +0100 | [diff] [blame] | 187 | struct mbo *mbo = NULL; |
Christian Gromm | ef0fbbb | 2017-11-21 15:05:08 +0100 | [diff] [blame] | 188 | struct comp_channel *c = filp->private_data; |
Christian Gromm | 9bc79bb | 2015-07-24 16:11:49 +0200 | [diff] [blame] | 189 | |
Christian Gromm | d8b082e | 2015-12-22 10:53:03 +0100 | [diff] [blame] | 190 | mutex_lock(&c->io_mutex); |
Christian Gromm | fa96b8e | 2015-12-22 10:53:04 +0100 | [diff] [blame] | 191 | while (c->dev && !ch_get_mbo(c, &mbo)) { |
Christian Gromm | d8b082e | 2015-12-22 10:53:03 +0100 | [diff] [blame] | 192 | mutex_unlock(&c->io_mutex); |
Christian Gromm | 9bc79bb | 2015-07-24 16:11:49 +0200 | [diff] [blame] | 193 | |
Christian Gromm | 9bc79bb | 2015-07-24 16:11:49 +0200 | [diff] [blame] | 194 | if ((filp->f_flags & O_NONBLOCK)) |
| 195 | return -EAGAIN; |
Christian Gromm | fa96b8e | 2015-12-22 10:53:04 +0100 | [diff] [blame] | 196 | if (wait_event_interruptible(c->wq, ch_has_mbo(c) || !c->dev)) |
Christian Gromm | 9bc79bb | 2015-07-24 16:11:49 +0200 | [diff] [blame] | 197 | return -ERESTARTSYS; |
Christian Gromm | fa96b8e | 2015-12-22 10:53:04 +0100 | [diff] [blame] | 198 | mutex_lock(&c->io_mutex); |
Christian Gromm | 9bc79bb | 2015-07-24 16:11:49 +0200 | [diff] [blame] | 199 | } |
| 200 | |
Christian Gromm | d8b082e | 2015-12-22 10:53:03 +0100 | [diff] [blame] | 201 | if (unlikely(!c->dev)) { |
Christian Gromm | 9b28e14 | 2016-08-19 13:09:33 +0200 | [diff] [blame] | 202 | ret = -ENODEV; |
Christian Gromm | 5adf5dc | 2015-12-22 10:53:07 +0100 | [diff] [blame] | 203 | goto unlock; |
Christian Gromm | 9bc79bb | 2015-07-24 16:11:49 +0200 | [diff] [blame] | 204 | } |
Christian Gromm | 9bc79bb | 2015-07-24 16:11:49 +0200 | [diff] [blame] | 205 | |
Christian Gromm | da2c087 | 2016-09-23 15:20:02 +0200 | [diff] [blame] | 206 | to_copy = min(count, c->cfg->buffer_size - c->mbo_offs); |
| 207 | left = copy_from_user(mbo->virt_address + c->mbo_offs, buf, to_copy); |
| 208 | if (left == to_copy) { |
Christian Gromm | 5adf5dc | 2015-12-22 10:53:07 +0100 | [diff] [blame] | 209 | ret = -EFAULT; |
Christian Gromm | da2c087 | 2016-09-23 15:20:02 +0200 | [diff] [blame] | 210 | goto unlock; |
Christian Gromm | 9bc79bb | 2015-07-24 16:11:49 +0200 | [diff] [blame] | 211 | } |
| 212 | |
Christian Gromm | da2c087 | 2016-09-23 15:20:02 +0200 | [diff] [blame] | 213 | c->mbo_offs += to_copy - left; |
| 214 | if (c->mbo_offs >= c->cfg->buffer_size || |
| 215 | c->cfg->data_type == MOST_CH_CONTROL || |
| 216 | c->cfg->data_type == MOST_CH_ASYNC) { |
| 217 | kfifo_skip(&c->fifo); |
| 218 | mbo->buffer_length = c->mbo_offs; |
| 219 | c->mbo_offs = 0; |
| 220 | most_submit_mbo(mbo); |
| 221 | } |
| 222 | |
| 223 | ret = to_copy - left; |
Christian Gromm | 5adf5dc | 2015-12-22 10:53:07 +0100 | [diff] [blame] | 224 | unlock: |
Christian Gromm | fa96b8e | 2015-12-22 10:53:04 +0100 | [diff] [blame] | 225 | mutex_unlock(&c->io_mutex); |
Christian Gromm | 5adf5dc | 2015-12-22 10:53:07 +0100 | [diff] [blame] | 226 | return ret; |
Christian Gromm | 9bc79bb | 2015-07-24 16:11:49 +0200 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | /** |
Christian Gromm | 1fd923f | 2017-11-21 15:05:19 +0100 | [diff] [blame] | 230 | * comp_read - implements the syscall to read from the device |
Christian Gromm | 9bc79bb | 2015-07-24 16:11:49 +0200 | [diff] [blame] | 231 | * @filp: file pointer |
| 232 | * @buf: pointer to user buffer |
| 233 | * @count: number of bytes to read |
| 234 | * @offset: offset from where to start reading |
| 235 | */ |
| 236 | static ssize_t |
Christian Gromm | 1fd923f | 2017-11-21 15:05:19 +0100 | [diff] [blame] | 237 | comp_read(struct file *filp, char __user *buf, size_t count, loff_t *offset) |
Christian Gromm | 9bc79bb | 2015-07-24 16:11:49 +0200 | [diff] [blame] | 238 | { |
Christian Gromm | 06e7ecf | 2015-12-22 10:52:45 +0100 | [diff] [blame] | 239 | size_t to_copy, not_copied, copied; |
Christian Gromm | 8463d9f | 2018-05-08 11:45:05 +0200 | [diff] [blame] | 240 | struct mbo *mbo = NULL; |
Christian Gromm | ef0fbbb | 2017-11-21 15:05:08 +0100 | [diff] [blame] | 241 | struct comp_channel *c = filp->private_data; |
Christian Gromm | 9bc79bb | 2015-07-24 16:11:49 +0200 | [diff] [blame] | 242 | |
Christian Gromm | fa96b8e | 2015-12-22 10:53:04 +0100 | [diff] [blame] | 243 | mutex_lock(&c->io_mutex); |
Christian Gromm | f45b0fb | 2015-12-22 10:53:06 +0100 | [diff] [blame] | 244 | while (c->dev && !kfifo_peek(&c->fifo, &mbo)) { |
Christian Gromm | fa96b8e | 2015-12-22 10:53:04 +0100 | [diff] [blame] | 245 | mutex_unlock(&c->io_mutex); |
Christian Gromm | 9bc79bb | 2015-07-24 16:11:49 +0200 | [diff] [blame] | 246 | if (filp->f_flags & O_NONBLOCK) |
| 247 | return -EAGAIN; |
Christian Gromm | d8b082e | 2015-12-22 10:53:03 +0100 | [diff] [blame] | 248 | if (wait_event_interruptible(c->wq, |
| 249 | (!kfifo_is_empty(&c->fifo) || |
| 250 | (!c->dev)))) |
Christian Gromm | 9bc79bb | 2015-07-24 16:11:49 +0200 | [diff] [blame] | 251 | return -ERESTARTSYS; |
Christian Gromm | fa96b8e | 2015-12-22 10:53:04 +0100 | [diff] [blame] | 252 | mutex_lock(&c->io_mutex); |
Christian Gromm | 9bc79bb | 2015-07-24 16:11:49 +0200 | [diff] [blame] | 253 | } |
| 254 | |
Christian Gromm | 9bc79bb | 2015-07-24 16:11:49 +0200 | [diff] [blame] | 255 | /* make sure we don't submit to gone devices */ |
Christian Gromm | d8b082e | 2015-12-22 10:53:03 +0100 | [diff] [blame] | 256 | if (unlikely(!c->dev)) { |
| 257 | mutex_unlock(&c->io_mutex); |
Christian Gromm | 9b28e14 | 2016-08-19 13:09:33 +0200 | [diff] [blame] | 258 | return -ENODEV; |
Christian Gromm | 9bc79bb | 2015-07-24 16:11:49 +0200 | [diff] [blame] | 259 | } |
| 260 | |
Christian Gromm | e6d6cbe | 2015-12-22 10:52:46 +0100 | [diff] [blame] | 261 | to_copy = min_t(size_t, |
| 262 | count, |
Christian Gromm | d8b082e | 2015-12-22 10:53:03 +0100 | [diff] [blame] | 263 | mbo->processed_length - c->mbo_offs); |
Christian Gromm | 9bc79bb | 2015-07-24 16:11:49 +0200 | [diff] [blame] | 264 | |
| 265 | not_copied = copy_to_user(buf, |
Christian Gromm | d8b082e | 2015-12-22 10:53:03 +0100 | [diff] [blame] | 266 | mbo->virt_address + c->mbo_offs, |
Christian Gromm | f9f2487 | 2015-12-22 10:52:43 +0100 | [diff] [blame] | 267 | to_copy); |
Christian Gromm | 9bc79bb | 2015-07-24 16:11:49 +0200 | [diff] [blame] | 268 | |
Christian Gromm | 4aa575a | 2015-12-22 10:52:44 +0100 | [diff] [blame] | 269 | copied = to_copy - not_copied; |
Christian Gromm | 9bc79bb | 2015-07-24 16:11:49 +0200 | [diff] [blame] | 270 | |
Christian Gromm | d8b082e | 2015-12-22 10:53:03 +0100 | [diff] [blame] | 271 | c->mbo_offs += copied; |
| 272 | if (c->mbo_offs >= mbo->processed_length) { |
Christian Gromm | f45b0fb | 2015-12-22 10:53:06 +0100 | [diff] [blame] | 273 | kfifo_skip(&c->fifo); |
Christian Gromm | 9bc79bb | 2015-07-24 16:11:49 +0200 | [diff] [blame] | 274 | most_put_mbo(mbo); |
Christian Gromm | d8b082e | 2015-12-22 10:53:03 +0100 | [diff] [blame] | 275 | c->mbo_offs = 0; |
Christian Gromm | 9bc79bb | 2015-07-24 16:11:49 +0200 | [diff] [blame] | 276 | } |
Christian Gromm | d8b082e | 2015-12-22 10:53:03 +0100 | [diff] [blame] | 277 | mutex_unlock(&c->io_mutex); |
Christian Gromm | f9f2487 | 2015-12-22 10:52:43 +0100 | [diff] [blame] | 278 | return copied; |
Christian Gromm | 9bc79bb | 2015-07-24 16:11:49 +0200 | [diff] [blame] | 279 | } |
| 280 | |
Linus Torvalds | 5d8515b | 2018-02-01 09:51:57 -0800 | [diff] [blame] | 281 | static __poll_t comp_poll(struct file *filp, poll_table *wait) |
Christian Gromm | aac997d | 2015-09-28 17:18:57 +0200 | [diff] [blame] | 282 | { |
Christian Gromm | ef0fbbb | 2017-11-21 15:05:08 +0100 | [diff] [blame] | 283 | struct comp_channel *c = filp->private_data; |
Al Viro | afc9a42 | 2017-07-03 06:39:46 -0400 | [diff] [blame] | 284 | __poll_t mask = 0; |
Christian Gromm | aac997d | 2015-09-28 17:18:57 +0200 | [diff] [blame] | 285 | |
Christian Gromm | c27fc35 | 2015-10-28 15:14:35 +0100 | [diff] [blame] | 286 | poll_wait(filp, &c->wq, wait); |
Christian Gromm | aac997d | 2015-09-28 17:18:57 +0200 | [diff] [blame] | 287 | |
Christian Gromm | 993c163 | 2018-05-08 11:45:10 +0200 | [diff] [blame] | 288 | mutex_lock(&c->io_mutex); |
Christian Gromm | aac997d | 2015-09-28 17:18:57 +0200 | [diff] [blame] | 289 | if (c->cfg->direction == MOST_CH_RX) { |
Christian Gromm | 993c163 | 2018-05-08 11:45:10 +0200 | [diff] [blame] | 290 | if (!c->dev || !kfifo_is_empty(&c->fifo)) |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 291 | mask |= EPOLLIN | EPOLLRDNORM; |
Christian Gromm | aac997d | 2015-09-28 17:18:57 +0200 | [diff] [blame] | 292 | } else { |
Christian Gromm | 993c163 | 2018-05-08 11:45:10 +0200 | [diff] [blame] | 293 | if (!c->dev || !kfifo_is_empty(&c->fifo) || ch_has_mbo(c)) |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 294 | mask |= EPOLLOUT | EPOLLWRNORM; |
Christian Gromm | aac997d | 2015-09-28 17:18:57 +0200 | [diff] [blame] | 295 | } |
Christian Gromm | 993c163 | 2018-05-08 11:45:10 +0200 | [diff] [blame] | 296 | mutex_unlock(&c->io_mutex); |
Christian Gromm | aac997d | 2015-09-28 17:18:57 +0200 | [diff] [blame] | 297 | return mask; |
| 298 | } |
| 299 | |
Christian Gromm | 9bc79bb | 2015-07-24 16:11:49 +0200 | [diff] [blame] | 300 | /** |
| 301 | * Initialization of struct file_operations |
| 302 | */ |
| 303 | static const struct file_operations channel_fops = { |
| 304 | .owner = THIS_MODULE, |
Christian Gromm | 1fd923f | 2017-11-21 15:05:19 +0100 | [diff] [blame] | 305 | .read = comp_read, |
| 306 | .write = comp_write, |
| 307 | .open = comp_open, |
| 308 | .release = comp_close, |
| 309 | .poll = comp_poll, |
Christian Gromm | 9bc79bb | 2015-07-24 16:11:49 +0200 | [diff] [blame] | 310 | }; |
| 311 | |
| 312 | /** |
Christian Gromm | 1fd923f | 2017-11-21 15:05:19 +0100 | [diff] [blame] | 313 | * comp_disconnect_channel - disconnect a channel |
Christian Gromm | 9bc79bb | 2015-07-24 16:11:49 +0200 | [diff] [blame] | 314 | * @iface: pointer to interface instance |
| 315 | * @channel_id: channel index |
| 316 | * |
| 317 | * This frees allocated memory and removes the cdev that represents this |
| 318 | * channel in user space. |
| 319 | */ |
Christian Gromm | 1fd923f | 2017-11-21 15:05:19 +0100 | [diff] [blame] | 320 | static int comp_disconnect_channel(struct most_interface *iface, int channel_id) |
Christian Gromm | 9bc79bb | 2015-07-24 16:11:49 +0200 | [diff] [blame] | 321 | { |
Christian Gromm | ef0fbbb | 2017-11-21 15:05:08 +0100 | [diff] [blame] | 322 | struct comp_channel *c; |
Christian Gromm | 9bc79bb | 2015-07-24 16:11:49 +0200 | [diff] [blame] | 323 | |
Christian Gromm | d8b082e | 2015-12-22 10:53:03 +0100 | [diff] [blame] | 324 | c = get_channel(iface, channel_id); |
| 325 | if (!c) |
Christian Gromm | 78aee65 | 2020-06-22 10:48:14 +0200 | [diff] [blame] | 326 | return -EINVAL; |
Christian Gromm | 9bc79bb | 2015-07-24 16:11:49 +0200 | [diff] [blame] | 327 | |
Christian Gromm | d8b082e | 2015-12-22 10:53:03 +0100 | [diff] [blame] | 328 | mutex_lock(&c->io_mutex); |
Christian Gromm | fa96b8e | 2015-12-22 10:53:04 +0100 | [diff] [blame] | 329 | spin_lock(&c->unlink); |
Christian Gromm | d8b082e | 2015-12-22 10:53:03 +0100 | [diff] [blame] | 330 | c->dev = NULL; |
Christian Gromm | fa96b8e | 2015-12-22 10:53:04 +0100 | [diff] [blame] | 331 | spin_unlock(&c->unlink); |
Andrey Shvetsov | 2c4aaa1 | 2017-04-07 15:38:37 +0200 | [diff] [blame] | 332 | destroy_cdev(c); |
Christian Gromm | b3c9f3c | 2015-12-22 10:53:05 +0100 | [diff] [blame] | 333 | if (c->access_ref) { |
Christian Gromm | fa96b8e | 2015-12-22 10:53:04 +0100 | [diff] [blame] | 334 | stop_channel(c); |
Christian Gromm | d8b082e | 2015-12-22 10:53:03 +0100 | [diff] [blame] | 335 | wake_up_interruptible(&c->wq); |
Christian Gromm | fa96b8e | 2015-12-22 10:53:04 +0100 | [diff] [blame] | 336 | mutex_unlock(&c->io_mutex); |
| 337 | } else { |
Christian Gromm | fa96b8e | 2015-12-22 10:53:04 +0100 | [diff] [blame] | 338 | mutex_unlock(&c->io_mutex); |
Andrey Shvetsov | 2c4aaa1 | 2017-04-07 15:38:37 +0200 | [diff] [blame] | 339 | destroy_channel(c); |
Christian Gromm | 9bc79bb | 2015-07-24 16:11:49 +0200 | [diff] [blame] | 340 | } |
| 341 | return 0; |
| 342 | } |
| 343 | |
| 344 | /** |
Christian Gromm | 1fd923f | 2017-11-21 15:05:19 +0100 | [diff] [blame] | 345 | * comp_rx_completion - completion handler for rx channels |
Christian Gromm | 9bc79bb | 2015-07-24 16:11:49 +0200 | [diff] [blame] | 346 | * @mbo: pointer to buffer object that has completed |
| 347 | * |
| 348 | * This searches for the channel linked to this MBO and stores it in the local |
| 349 | * fifo buffer. |
| 350 | */ |
Christian Gromm | 1fd923f | 2017-11-21 15:05:19 +0100 | [diff] [blame] | 351 | static int comp_rx_completion(struct mbo *mbo) |
Christian Gromm | 9bc79bb | 2015-07-24 16:11:49 +0200 | [diff] [blame] | 352 | { |
Christian Gromm | ef0fbbb | 2017-11-21 15:05:08 +0100 | [diff] [blame] | 353 | struct comp_channel *c; |
Christian Gromm | 9bc79bb | 2015-07-24 16:11:49 +0200 | [diff] [blame] | 354 | |
| 355 | if (!mbo) |
| 356 | return -EINVAL; |
| 357 | |
Christian Gromm | d8b082e | 2015-12-22 10:53:03 +0100 | [diff] [blame] | 358 | c = get_channel(mbo->ifp, mbo->hdm_channel_id); |
| 359 | if (!c) |
Christian Gromm | 78aee65 | 2020-06-22 10:48:14 +0200 | [diff] [blame] | 360 | return -EINVAL; |
Christian Gromm | 9bc79bb | 2015-07-24 16:11:49 +0200 | [diff] [blame] | 361 | |
Christian Gromm | fa96b8e | 2015-12-22 10:53:04 +0100 | [diff] [blame] | 362 | spin_lock(&c->unlink); |
Christian Gromm | b3c9f3c | 2015-12-22 10:53:05 +0100 | [diff] [blame] | 363 | if (!c->access_ref || !c->dev) { |
Christian Gromm | fa96b8e | 2015-12-22 10:53:04 +0100 | [diff] [blame] | 364 | spin_unlock(&c->unlink); |
Christian Gromm | 9b28e14 | 2016-08-19 13:09:33 +0200 | [diff] [blame] | 365 | return -ENODEV; |
Christian Gromm | fa96b8e | 2015-12-22 10:53:04 +0100 | [diff] [blame] | 366 | } |
Christian Gromm | d8b082e | 2015-12-22 10:53:03 +0100 | [diff] [blame] | 367 | kfifo_in(&c->fifo, &mbo, 1); |
Christian Gromm | fa96b8e | 2015-12-22 10:53:04 +0100 | [diff] [blame] | 368 | spin_unlock(&c->unlink); |
Christian Gromm | 9bc79bb | 2015-07-24 16:11:49 +0200 | [diff] [blame] | 369 | #ifdef DEBUG_MESG |
Christian Gromm | d8b082e | 2015-12-22 10:53:03 +0100 | [diff] [blame] | 370 | if (kfifo_is_full(&c->fifo)) |
Christian Gromm | 0883938 | 2020-06-22 10:48:11 +0200 | [diff] [blame] | 371 | dev_warn(c->dev, "Fifo is full\n"); |
Christian Gromm | 9bc79bb | 2015-07-24 16:11:49 +0200 | [diff] [blame] | 372 | #endif |
Christian Gromm | d8b082e | 2015-12-22 10:53:03 +0100 | [diff] [blame] | 373 | wake_up_interruptible(&c->wq); |
Christian Gromm | 9bc79bb | 2015-07-24 16:11:49 +0200 | [diff] [blame] | 374 | return 0; |
| 375 | } |
| 376 | |
| 377 | /** |
Christian Gromm | 1fd923f | 2017-11-21 15:05:19 +0100 | [diff] [blame] | 378 | * comp_tx_completion - completion handler for tx channels |
Christian Gromm | 9bc79bb | 2015-07-24 16:11:49 +0200 | [diff] [blame] | 379 | * @iface: pointer to interface instance |
| 380 | * @channel_id: channel index/ID |
| 381 | * |
| 382 | * This wakes sleeping processes in the wait-queue. |
| 383 | */ |
Christian Gromm | 1fd923f | 2017-11-21 15:05:19 +0100 | [diff] [blame] | 384 | static int comp_tx_completion(struct most_interface *iface, int channel_id) |
Christian Gromm | 9bc79bb | 2015-07-24 16:11:49 +0200 | [diff] [blame] | 385 | { |
Christian Gromm | ef0fbbb | 2017-11-21 15:05:08 +0100 | [diff] [blame] | 386 | struct comp_channel *c; |
Christian Gromm | 9bc79bb | 2015-07-24 16:11:49 +0200 | [diff] [blame] | 387 | |
Christian Gromm | d8b082e | 2015-12-22 10:53:03 +0100 | [diff] [blame] | 388 | c = get_channel(iface, channel_id); |
| 389 | if (!c) |
Christian Gromm | 78aee65 | 2020-06-22 10:48:14 +0200 | [diff] [blame] | 390 | return -EINVAL; |
Christian Gromm | 0883938 | 2020-06-22 10:48:11 +0200 | [diff] [blame] | 391 | |
| 392 | if ((channel_id < 0) || (channel_id >= iface->num_channels)) { |
| 393 | dev_warn(c->dev, "Channel ID out of range\n"); |
| 394 | return -EINVAL; |
| 395 | } |
| 396 | |
Christian Gromm | d8b082e | 2015-12-22 10:53:03 +0100 | [diff] [blame] | 397 | wake_up_interruptible(&c->wq); |
Christian Gromm | 9bc79bb | 2015-07-24 16:11:49 +0200 | [diff] [blame] | 398 | return 0; |
| 399 | } |
| 400 | |
Christian Gromm | 9bc79bb | 2015-07-24 16:11:49 +0200 | [diff] [blame] | 401 | /** |
Christian Gromm | 1fd923f | 2017-11-21 15:05:19 +0100 | [diff] [blame] | 402 | * comp_probe - probe function of the driver module |
Christian Gromm | 9bc79bb | 2015-07-24 16:11:49 +0200 | [diff] [blame] | 403 | * @iface: pointer to interface instance |
| 404 | * @channel_id: channel index/ID |
| 405 | * @cfg: pointer to actual channel configuration |
Christian Gromm | 9bc79bb | 2015-07-24 16:11:49 +0200 | [diff] [blame] | 406 | * @name: name of the device to be created |
| 407 | * |
| 408 | * This allocates achannel object and creates the device node in /dev |
| 409 | * |
| 410 | * Returns 0 on success or error code otherwise. |
| 411 | */ |
Christian Gromm | 1fd923f | 2017-11-21 15:05:19 +0100 | [diff] [blame] | 412 | static int comp_probe(struct most_interface *iface, int channel_id, |
Christian Gromm | dfee92d | 2019-04-03 15:19:45 +0200 | [diff] [blame] | 413 | struct most_channel_config *cfg, char *name, char *args) |
Christian Gromm | 9bc79bb | 2015-07-24 16:11:49 +0200 | [diff] [blame] | 414 | { |
Christian Gromm | ef0fbbb | 2017-11-21 15:05:08 +0100 | [diff] [blame] | 415 | struct comp_channel *c; |
Christian Gromm | 9bc79bb | 2015-07-24 16:11:49 +0200 | [diff] [blame] | 416 | unsigned long cl_flags; |
| 417 | int retval; |
| 418 | int current_minor; |
| 419 | |
Christian Gromm | e8e0f7f | 2020-06-22 10:48:13 +0200 | [diff] [blame] | 420 | if (!cfg || !name) |
Christian Gromm | 9bc79bb | 2015-07-24 16:11:49 +0200 | [diff] [blame] | 421 | return -EINVAL; |
Christian Gromm | 61fd971 | 2020-06-22 10:48:10 +0200 | [diff] [blame] | 422 | |
Christian Gromm | d8b082e | 2015-12-22 10:53:03 +0100 | [diff] [blame] | 423 | c = get_channel(iface, channel_id); |
| 424 | if (c) |
Christian Gromm | 9bc79bb | 2015-07-24 16:11:49 +0200 | [diff] [blame] | 425 | return -EEXIST; |
| 426 | |
Christian Gromm | c73d915 | 2017-11-21 15:05:20 +0100 | [diff] [blame] | 427 | current_minor = ida_simple_get(&comp.minor_id, 0, 0, GFP_KERNEL); |
Christian Gromm | 9bc79bb | 2015-07-24 16:11:49 +0200 | [diff] [blame] | 428 | if (current_minor < 0) |
| 429 | return current_minor; |
| 430 | |
Christian Gromm | d8b082e | 2015-12-22 10:53:03 +0100 | [diff] [blame] | 431 | c = kzalloc(sizeof(*c), GFP_KERNEL); |
| 432 | if (!c) { |
Christian Gromm | 9bc79bb | 2015-07-24 16:11:49 +0200 | [diff] [blame] | 433 | retval = -ENOMEM; |
Christian Gromm | bddd3c2 | 2018-09-21 11:28:51 +0200 | [diff] [blame] | 434 | goto err_remove_ida; |
Christian Gromm | 9bc79bb | 2015-07-24 16:11:49 +0200 | [diff] [blame] | 435 | } |
| 436 | |
Christian Gromm | c73d915 | 2017-11-21 15:05:20 +0100 | [diff] [blame] | 437 | c->devno = MKDEV(comp.major, current_minor); |
Christian Gromm | d8b082e | 2015-12-22 10:53:03 +0100 | [diff] [blame] | 438 | cdev_init(&c->cdev, &channel_fops); |
| 439 | c->cdev.owner = THIS_MODULE; |
Colin Ian King | 5ae8907 | 2019-02-02 22:34:49 +0000 | [diff] [blame] | 440 | retval = cdev_add(&c->cdev, c->devno, 1); |
| 441 | if (retval < 0) |
| 442 | goto err_free_c; |
Christian Gromm | d8b082e | 2015-12-22 10:53:03 +0100 | [diff] [blame] | 443 | c->iface = iface; |
| 444 | c->cfg = cfg; |
| 445 | c->channel_id = channel_id; |
Christian Gromm | b3c9f3c | 2015-12-22 10:53:05 +0100 | [diff] [blame] | 446 | c->access_ref = 0; |
Christian Gromm | fa96b8e | 2015-12-22 10:53:04 +0100 | [diff] [blame] | 447 | spin_lock_init(&c->unlink); |
Christian Gromm | d8b082e | 2015-12-22 10:53:03 +0100 | [diff] [blame] | 448 | INIT_KFIFO(c->fifo); |
| 449 | retval = kfifo_alloc(&c->fifo, cfg->num_buffers, GFP_KERNEL); |
Keyur Patel | ebf256e | 2019-07-14 13:27:06 -0400 | [diff] [blame] | 450 | if (retval) |
Christian Gromm | bddd3c2 | 2018-09-21 11:28:51 +0200 | [diff] [blame] | 451 | goto err_del_cdev_and_free_channel; |
Christian Gromm | d8b082e | 2015-12-22 10:53:03 +0100 | [diff] [blame] | 452 | init_waitqueue_head(&c->wq); |
| 453 | mutex_init(&c->io_mutex); |
Christian Gromm | 9bc79bb | 2015-07-24 16:11:49 +0200 | [diff] [blame] | 454 | spin_lock_irqsave(&ch_list_lock, cl_flags); |
Christian Gromm | d8b082e | 2015-12-22 10:53:03 +0100 | [diff] [blame] | 455 | list_add_tail(&c->list, &channel_list); |
Christian Gromm | 9bc79bb | 2015-07-24 16:11:49 +0200 | [diff] [blame] | 456 | spin_unlock_irqrestore(&ch_list_lock, cl_flags); |
Christian Gromm | c73d915 | 2017-11-21 15:05:20 +0100 | [diff] [blame] | 457 | c->dev = device_create(comp.class, NULL, c->devno, NULL, "%s", name); |
Christian Gromm | 9bc79bb | 2015-07-24 16:11:49 +0200 | [diff] [blame] | 458 | |
Sudip Mukherjee | ea39854 | 2016-02-08 22:55:34 +0530 | [diff] [blame] | 459 | if (IS_ERR(c->dev)) { |
| 460 | retval = PTR_ERR(c->dev); |
Christian Gromm | bddd3c2 | 2018-09-21 11:28:51 +0200 | [diff] [blame] | 461 | goto err_free_kfifo_and_del_list; |
Christian Gromm | 9bc79bb | 2015-07-24 16:11:49 +0200 | [diff] [blame] | 462 | } |
Christian Gromm | d8b082e | 2015-12-22 10:53:03 +0100 | [diff] [blame] | 463 | kobject_uevent(&c->dev->kobj, KOBJ_ADD); |
Christian Gromm | 9bc79bb | 2015-07-24 16:11:49 +0200 | [diff] [blame] | 464 | return 0; |
| 465 | |
Christian Gromm | bddd3c2 | 2018-09-21 11:28:51 +0200 | [diff] [blame] | 466 | err_free_kfifo_and_del_list: |
Christian Gromm | d8b082e | 2015-12-22 10:53:03 +0100 | [diff] [blame] | 467 | kfifo_free(&c->fifo); |
| 468 | list_del(&c->list); |
Christian Gromm | bddd3c2 | 2018-09-21 11:28:51 +0200 | [diff] [blame] | 469 | err_del_cdev_and_free_channel: |
Christian Gromm | d8b082e | 2015-12-22 10:53:03 +0100 | [diff] [blame] | 470 | cdev_del(&c->cdev); |
Colin Ian King | 5ae8907 | 2019-02-02 22:34:49 +0000 | [diff] [blame] | 471 | err_free_c: |
Christian Gromm | d8b082e | 2015-12-22 10:53:03 +0100 | [diff] [blame] | 472 | kfree(c); |
Christian Gromm | bddd3c2 | 2018-09-21 11:28:51 +0200 | [diff] [blame] | 473 | err_remove_ida: |
Christian Gromm | c73d915 | 2017-11-21 15:05:20 +0100 | [diff] [blame] | 474 | ida_simple_remove(&comp.minor_id, current_minor); |
Christian Gromm | 9bc79bb | 2015-07-24 16:11:49 +0200 | [diff] [blame] | 475 | return retval; |
| 476 | } |
| 477 | |
Christian Gromm | c73d915 | 2017-11-21 15:05:20 +0100 | [diff] [blame] | 478 | static struct cdev_component comp = { |
| 479 | .cc = { |
Christian Gromm | 08283d3 | 2019-11-08 17:21:08 +0100 | [diff] [blame] | 480 | .mod = THIS_MODULE, |
Christian Gromm | c73d915 | 2017-11-21 15:05:20 +0100 | [diff] [blame] | 481 | .name = "cdev", |
| 482 | .probe_channel = comp_probe, |
| 483 | .disconnect_channel = comp_disconnect_channel, |
| 484 | .rx_completion = comp_rx_completion, |
| 485 | .tx_completion = comp_tx_completion, |
| 486 | }, |
Christian Gromm | 9bc79bb | 2015-07-24 16:11:49 +0200 | [diff] [blame] | 487 | }; |
| 488 | |
Randy Dunlap | ddb1381 | 2021-07-11 15:31:46 -0700 | [diff] [blame] | 489 | static int __init most_cdev_init(void) |
Christian Gromm | 9bc79bb | 2015-07-24 16:11:49 +0200 | [diff] [blame] | 490 | { |
Christian Gromm | 9b28e14 | 2016-08-19 13:09:33 +0200 | [diff] [blame] | 491 | int err; |
| 492 | |
Christian Gromm | c73d915 | 2017-11-21 15:05:20 +0100 | [diff] [blame] | 493 | comp.class = class_create(THIS_MODULE, "most_cdev"); |
Christian Gromm | 61fd971 | 2020-06-22 10:48:10 +0200 | [diff] [blame] | 494 | if (IS_ERR(comp.class)) |
Christian Gromm | c73d915 | 2017-11-21 15:05:20 +0100 | [diff] [blame] | 495 | return PTR_ERR(comp.class); |
Christian Gromm | c73d915 | 2017-11-21 15:05:20 +0100 | [diff] [blame] | 496 | |
Christian Gromm | c73d915 | 2017-11-21 15:05:20 +0100 | [diff] [blame] | 497 | ida_init(&comp.minor_id); |
Christian Gromm | 9bc79bb | 2015-07-24 16:11:49 +0200 | [diff] [blame] | 498 | |
Christian Gromm | aba258b | 2018-05-08 11:45:06 +0200 | [diff] [blame] | 499 | err = alloc_chrdev_region(&comp.devno, 0, CHRDEV_REGION_SIZE, "cdev"); |
Christian Gromm | 324e87b | 2016-08-19 13:09:34 +0200 | [diff] [blame] | 500 | if (err < 0) |
Christian Gromm | 1d9e3a0 | 2016-08-22 16:46:24 +0200 | [diff] [blame] | 501 | goto dest_ida; |
Christian Gromm | c73d915 | 2017-11-21 15:05:20 +0100 | [diff] [blame] | 502 | comp.major = MAJOR(comp.devno); |
| 503 | err = most_register_component(&comp.cc); |
Christian Gromm | 9b28e14 | 2016-08-19 13:09:33 +0200 | [diff] [blame] | 504 | if (err) |
Christian Gromm | c73d915 | 2017-11-21 15:05:20 +0100 | [diff] [blame] | 505 | goto free_cdev; |
Christian Gromm | 919c03a | 2019-04-03 15:19:48 +0200 | [diff] [blame] | 506 | err = most_register_configfs_subsys(&comp.cc); |
| 507 | if (err) |
| 508 | goto deregister_comp; |
Christian Gromm | 9bc79bb | 2015-07-24 16:11:49 +0200 | [diff] [blame] | 509 | return 0; |
| 510 | |
Christian Gromm | 919c03a | 2019-04-03 15:19:48 +0200 | [diff] [blame] | 511 | deregister_comp: |
| 512 | most_deregister_component(&comp.cc); |
Christian Gromm | 9bc79bb | 2015-07-24 16:11:49 +0200 | [diff] [blame] | 513 | free_cdev: |
Christian Gromm | aba258b | 2018-05-08 11:45:06 +0200 | [diff] [blame] | 514 | unregister_chrdev_region(comp.devno, CHRDEV_REGION_SIZE); |
Christian Gromm | 1d9e3a0 | 2016-08-22 16:46:24 +0200 | [diff] [blame] | 515 | dest_ida: |
Christian Gromm | c73d915 | 2017-11-21 15:05:20 +0100 | [diff] [blame] | 516 | ida_destroy(&comp.minor_id); |
| 517 | class_destroy(comp.class); |
Christian Gromm | 9b28e14 | 2016-08-19 13:09:33 +0200 | [diff] [blame] | 518 | return err; |
Christian Gromm | 9bc79bb | 2015-07-24 16:11:49 +0200 | [diff] [blame] | 519 | } |
| 520 | |
Randy Dunlap | ddb1381 | 2021-07-11 15:31:46 -0700 | [diff] [blame] | 521 | static void __exit most_cdev_exit(void) |
Christian Gromm | 9bc79bb | 2015-07-24 16:11:49 +0200 | [diff] [blame] | 522 | { |
Christian Gromm | ef0fbbb | 2017-11-21 15:05:08 +0100 | [diff] [blame] | 523 | struct comp_channel *c, *tmp; |
Christian Gromm | 9bc79bb | 2015-07-24 16:11:49 +0200 | [diff] [blame] | 524 | |
Christian Gromm | 919c03a | 2019-04-03 15:19:48 +0200 | [diff] [blame] | 525 | most_deregister_configfs_subsys(&comp.cc); |
Christian Gromm | c73d915 | 2017-11-21 15:05:20 +0100 | [diff] [blame] | 526 | most_deregister_component(&comp.cc); |
Christian Gromm | 9bc79bb | 2015-07-24 16:11:49 +0200 | [diff] [blame] | 527 | |
Christian Gromm | d8b082e | 2015-12-22 10:53:03 +0100 | [diff] [blame] | 528 | list_for_each_entry_safe(c, tmp, &channel_list, list) { |
| 529 | destroy_cdev(c); |
Andrey Shvetsov | 2c4aaa1 | 2017-04-07 15:38:37 +0200 | [diff] [blame] | 530 | destroy_channel(c); |
Christian Gromm | 9bc79bb | 2015-07-24 16:11:49 +0200 | [diff] [blame] | 531 | } |
Suresh Udipi | af70890 | 2019-04-24 21:23:43 +0200 | [diff] [blame] | 532 | unregister_chrdev_region(comp.devno, CHRDEV_REGION_SIZE); |
Christian Gromm | c73d915 | 2017-11-21 15:05:20 +0100 | [diff] [blame] | 533 | ida_destroy(&comp.minor_id); |
| 534 | class_destroy(comp.class); |
Christian Gromm | 9bc79bb | 2015-07-24 16:11:49 +0200 | [diff] [blame] | 535 | } |
| 536 | |
Randy Dunlap | ddb1381 | 2021-07-11 15:31:46 -0700 | [diff] [blame] | 537 | module_init(most_cdev_init); |
| 538 | module_exit(most_cdev_exit); |
Christian Gromm | 9bc79bb | 2015-07-24 16:11:49 +0200 | [diff] [blame] | 539 | MODULE_AUTHOR("Christian Gromm <christian.gromm@microchip.com>"); |
| 540 | MODULE_LICENSE("GPL"); |
Christian Gromm | b7937dc | 2017-11-21 15:05:12 +0100 | [diff] [blame] | 541 | MODULE_DESCRIPTION("character device component for mostcore"); |