Merge tag 'powerpc-6.3-1' of git://git.kernel.org/pub/scm/linux/kernel/git/powerpc...
[linux-block.git] / drivers / most / most_cdev.c
CommitLineData
1a79f22d 1// SPDX-License-Identifier: GPL-2.0
9bc79bbc 2/*
b7937dc4 3 * cdev.c - Character device component for Mostcore
9bc79bbc
CG
4 *
5 * Copyright (C) 2013-2015 Microchip Technology Germany II GmbH & Co. KG
9bc79bbc
CG
6 */
7
9bc79bbc
CG
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>
aac997df 14#include <linux/poll.h>
9bc79bbc
CG
15#include <linux/kfifo.h>
16#include <linux/uaccess.h>
17#include <linux/idr.h>
b2765275 18#include <linux/most.h>
9bc79bbc 19
aba258b7
CG
20#define CHRDEV_REGION_SIZE 50
21
c73d915d
CG
22static struct cdev_component {
23 dev_t devno;
24 struct ida minor_id;
25 unsigned int major;
26 struct class *class;
45917e79 27 struct most_component cc;
c73d915d 28} comp;
9bc79bbc 29
ef0fbbbb 30struct comp_channel {
9bc79bbc 31 wait_queue_head_t wq;
fa96b8ed 32 spinlock_t unlink; /* synchronization lock to unlink channels */
9bc79bbc
CG
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;
06e7ecf2 40 size_t mbo_offs;
9bc79bbc 41 DECLARE_KFIFO_PTR(fifo, typeof(struct mbo *));
b3c9f3c5 42 int access_ref;
9bc79bbc
CG
43 struct list_head list;
44};
9cbe5aa6 45
ef0fbbbb 46#define to_channel(d) container_of(d, struct comp_channel, cdev)
57515640 47static LIST_HEAD(channel_list);
2c4134e7 48static DEFINE_SPINLOCK(ch_list_lock);
9bc79bbc 49
ef0fbbbb 50static inline bool ch_has_mbo(struct comp_channel *c)
cdc293d5 51{
c73d915d 52 return channel_has_mbo(c->iface, c->channel_id, &comp.cc) > 0;
cdc293d5
CG
53}
54
7d56f62d 55static inline struct mbo *ch_get_mbo(struct comp_channel *c, struct mbo **mbo)
fa96b8ed 56{
da2c0871 57 if (!kfifo_peek(&c->fifo, mbo)) {
c73d915d 58 *mbo = most_get_mbo(c->iface, c->channel_id, &comp.cc);
da2c0871
CG
59 if (*mbo)
60 kfifo_in(&c->fifo, mbo, 1);
61 }
fa96b8ed
CG
62 return *mbo;
63}
64
ef0fbbbb 65static struct comp_channel *get_channel(struct most_interface *iface, int id)
9bc79bbc 66{
ef0fbbbb 67 struct comp_channel *c, *tmp;
9bc79bbc 68 unsigned long flags;
9bc79bbc
CG
69
70 spin_lock_irqsave(&ch_list_lock, flags);
d8b082e6
CG
71 list_for_each_entry_safe(c, tmp, &channel_list, list) {
72 if ((c->iface == iface) && (c->channel_id == id)) {
0ae9e4f2
CG
73 spin_unlock_irqrestore(&ch_list_lock, flags);
74 return c;
9bc79bbc
CG
75 }
76 }
77 spin_unlock_irqrestore(&ch_list_lock, flags);
0ae9e4f2 78 return NULL;
9bc79bbc
CG
79}
80
ef0fbbbb 81static void stop_channel(struct comp_channel *c)
5f858a61
CG
82{
83 struct mbo *mbo;
84
85 while (kfifo_out((struct kfifo *)&c->fifo, &mbo, 1))
86 most_put_mbo(mbo);
c73d915d 87 most_stop_channel(c->iface, c->channel_id, &comp.cc);
5f858a61
CG
88}
89
ef0fbbbb 90static void destroy_cdev(struct comp_channel *c)
5f858a61
CG
91{
92 unsigned long flags;
93
c73d915d 94 device_destroy(comp.class, c->devno);
5f858a61 95 cdev_del(&c->cdev);
5f858a61
CG
96 spin_lock_irqsave(&ch_list_lock, flags);
97 list_del(&c->list);
98 spin_unlock_irqrestore(&ch_list_lock, flags);
2c4aaa1f
AS
99}
100
ef0fbbbb 101static void destroy_channel(struct comp_channel *c)
2c4aaa1f 102{
c73d915d 103 ida_simple_remove(&comp.minor_id, MINOR(c->devno));
2c4aaa1f
AS
104 kfifo_free(&c->fifo);
105 kfree(c);
5f858a61
CG
106}
107
9bc79bbc 108/**
1fd923f3 109 * comp_open - implements the syscall to open the device
9bc79bbc
CG
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 */
1fd923f3 116static int comp_open(struct inode *inode, struct file *filp)
9bc79bbc 117{
ef0fbbbb 118 struct comp_channel *c;
9bc79bbc
CG
119 int ret;
120
d8b082e6
CG
121 c = to_channel(inode->i_cdev);
122 filp->private_data = c;
9bc79bbc 123
d8b082e6 124 if (((c->cfg->direction == MOST_CH_RX) &&
623d8002 125 ((filp->f_flags & O_ACCMODE) != O_RDONLY)) ||
d8b082e6 126 ((c->cfg->direction == MOST_CH_TX) &&
9bc79bbc 127 ((filp->f_flags & O_ACCMODE) != O_WRONLY))) {
9bc79bbc
CG
128 return -EACCES;
129 }
fa96b8ed
CG
130
131 mutex_lock(&c->io_mutex);
132 if (!c->dev) {
fa96b8ed 133 mutex_unlock(&c->io_mutex);
9b28e148 134 return -ENODEV;
fa96b8ed
CG
135 }
136
b3c9f3c5 137 if (c->access_ref) {
fa96b8ed 138 mutex_unlock(&c->io_mutex);
9bc79bbc
CG
139 return -EBUSY;
140 }
141
f45b0fba 142 c->mbo_offs = 0;
c73d915d 143 ret = most_start_channel(c->iface, c->channel_id, &comp.cc);
b3c9f3c5
CG
144 if (!ret)
145 c->access_ref = 1;
fa96b8ed 146 mutex_unlock(&c->io_mutex);
9bc79bbc
CG
147 return ret;
148}
149
150/**
1fd923f3 151 * comp_close - implements the syscall to close the device
9bc79bbc
CG
152 * @inode: inode pointer
153 * @filp: file pointer
154 *
155 * This stops the channel within the core.
156 */
1fd923f3 157static int comp_close(struct inode *inode, struct file *filp)
9bc79bbc 158{
ef0fbbbb 159 struct comp_channel *c = to_channel(inode->i_cdev);
d8b082e6
CG
160
161 mutex_lock(&c->io_mutex);
fa96b8ed 162 spin_lock(&c->unlink);
b3c9f3c5 163 c->access_ref = 0;
fa96b8ed
CG
164 spin_unlock(&c->unlink);
165 if (c->dev) {
166 stop_channel(c);
d8b082e6 167 mutex_unlock(&c->io_mutex);
fa96b8ed 168 } else {
fa96b8ed 169 mutex_unlock(&c->io_mutex);
2c4aaa1f 170 destroy_channel(c);
9bc79bbc 171 }
5f858a61 172 return 0;
9bc79bbc
CG
173}
174
175/**
1fd923f3 176 * comp_write - implements the syscall to write to the device
9bc79bbc
CG
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 */
1fd923f3
CG
182static ssize_t comp_write(struct file *filp, const char __user *buf,
183 size_t count, loff_t *offset)
9bc79bbc 184{
5adf5dc5 185 int ret;
da2c0871 186 size_t to_copy, left;
fa96b8ed 187 struct mbo *mbo = NULL;
ef0fbbbb 188 struct comp_channel *c = filp->private_data;
9bc79bbc 189
d8b082e6 190 mutex_lock(&c->io_mutex);
fa96b8ed 191 while (c->dev && !ch_get_mbo(c, &mbo)) {
d8b082e6 192 mutex_unlock(&c->io_mutex);
9bc79bbc 193
9bc79bbc
CG
194 if ((filp->f_flags & O_NONBLOCK))
195 return -EAGAIN;
fa96b8ed 196 if (wait_event_interruptible(c->wq, ch_has_mbo(c) || !c->dev))
9bc79bbc 197 return -ERESTARTSYS;
fa96b8ed 198 mutex_lock(&c->io_mutex);
9bc79bbc
CG
199 }
200
d8b082e6 201 if (unlikely(!c->dev)) {
9b28e148 202 ret = -ENODEV;
5adf5dc5 203 goto unlock;
9bc79bbc 204 }
9bc79bbc 205
da2c0871
CG
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) {
5adf5dc5 209 ret = -EFAULT;
da2c0871 210 goto unlock;
9bc79bbc
CG
211 }
212
da2c0871
CG
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;
5adf5dc5 224unlock:
fa96b8ed 225 mutex_unlock(&c->io_mutex);
5adf5dc5 226 return ret;
9bc79bbc
CG
227}
228
229/**
1fd923f3 230 * comp_read - implements the syscall to read from the device
9bc79bbc
CG
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 */
236static ssize_t
1fd923f3 237comp_read(struct file *filp, char __user *buf, size_t count, loff_t *offset)
9bc79bbc 238{
06e7ecf2 239 size_t to_copy, not_copied, copied;
8463d9fa 240 struct mbo *mbo = NULL;
ef0fbbbb 241 struct comp_channel *c = filp->private_data;
9bc79bbc 242
fa96b8ed 243 mutex_lock(&c->io_mutex);
f45b0fba 244 while (c->dev && !kfifo_peek(&c->fifo, &mbo)) {
fa96b8ed 245 mutex_unlock(&c->io_mutex);
9bc79bbc
CG
246 if (filp->f_flags & O_NONBLOCK)
247 return -EAGAIN;
d8b082e6
CG
248 if (wait_event_interruptible(c->wq,
249 (!kfifo_is_empty(&c->fifo) ||
250 (!c->dev))))
9bc79bbc 251 return -ERESTARTSYS;
fa96b8ed 252 mutex_lock(&c->io_mutex);
9bc79bbc
CG
253 }
254
9bc79bbc 255 /* make sure we don't submit to gone devices */
d8b082e6
CG
256 if (unlikely(!c->dev)) {
257 mutex_unlock(&c->io_mutex);
9b28e148 258 return -ENODEV;
9bc79bbc
CG
259 }
260
e6d6cbe3
CG
261 to_copy = min_t(size_t,
262 count,
d8b082e6 263 mbo->processed_length - c->mbo_offs);
9bc79bbc
CG
264
265 not_copied = copy_to_user(buf,
d8b082e6 266 mbo->virt_address + c->mbo_offs,
f9f24870 267 to_copy);
9bc79bbc 268
4aa575a9 269 copied = to_copy - not_copied;
9bc79bbc 270
d8b082e6
CG
271 c->mbo_offs += copied;
272 if (c->mbo_offs >= mbo->processed_length) {
f45b0fba 273 kfifo_skip(&c->fifo);
9bc79bbc 274 most_put_mbo(mbo);
d8b082e6 275 c->mbo_offs = 0;
9bc79bbc 276 }
d8b082e6 277 mutex_unlock(&c->io_mutex);
f9f24870 278 return copied;
9bc79bbc
CG
279}
280
5d8515bc 281static __poll_t comp_poll(struct file *filp, poll_table *wait)
aac997df 282{
ef0fbbbb 283 struct comp_channel *c = filp->private_data;
afc9a42b 284 __poll_t mask = 0;
aac997df 285
c27fc351 286 poll_wait(filp, &c->wq, wait);
aac997df 287
993c1637 288 mutex_lock(&c->io_mutex);
aac997df 289 if (c->cfg->direction == MOST_CH_RX) {
993c1637 290 if (!c->dev || !kfifo_is_empty(&c->fifo))
a9a08845 291 mask |= EPOLLIN | EPOLLRDNORM;
aac997df 292 } else {
993c1637 293 if (!c->dev || !kfifo_is_empty(&c->fifo) || ch_has_mbo(c))
a9a08845 294 mask |= EPOLLOUT | EPOLLWRNORM;
aac997df 295 }
993c1637 296 mutex_unlock(&c->io_mutex);
aac997df
CG
297 return mask;
298}
299
fba3993e 300/*
9bc79bbc
CG
301 * Initialization of struct file_operations
302 */
303static const struct file_operations channel_fops = {
304 .owner = THIS_MODULE,
1fd923f3
CG
305 .read = comp_read,
306 .write = comp_write,
307 .open = comp_open,
308 .release = comp_close,
309 .poll = comp_poll,
9bc79bbc
CG
310};
311
312/**
1fd923f3 313 * comp_disconnect_channel - disconnect a channel
9bc79bbc
CG
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 */
1fd923f3 320static int comp_disconnect_channel(struct most_interface *iface, int channel_id)
9bc79bbc 321{
ef0fbbbb 322 struct comp_channel *c;
9bc79bbc 323
d8b082e6
CG
324 c = get_channel(iface, channel_id);
325 if (!c)
78aee651 326 return -EINVAL;
9bc79bbc 327
d8b082e6 328 mutex_lock(&c->io_mutex);
fa96b8ed 329 spin_lock(&c->unlink);
d8b082e6 330 c->dev = NULL;
fa96b8ed 331 spin_unlock(&c->unlink);
2c4aaa1f 332 destroy_cdev(c);
b3c9f3c5 333 if (c->access_ref) {
fa96b8ed
CG
334 stop_channel(c);
335 wake_up_interruptible(&c->wq);
336 mutex_unlock(&c->io_mutex);
337 } else {
fa96b8ed 338 mutex_unlock(&c->io_mutex);
2c4aaa1f 339 destroy_channel(c);
9bc79bbc
CG
340 }
341 return 0;
342}
343
344/**
1fd923f3 345 * comp_rx_completion - completion handler for rx channels
9bc79bbc
CG
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 */
1fd923f3 351static int comp_rx_completion(struct mbo *mbo)
9bc79bbc 352{
ef0fbbbb 353 struct comp_channel *c;
9bc79bbc
CG
354
355 if (!mbo)
356 return -EINVAL;
357
d8b082e6
CG
358 c = get_channel(mbo->ifp, mbo->hdm_channel_id);
359 if (!c)
78aee651 360 return -EINVAL;
9bc79bbc 361
fa96b8ed 362 spin_lock(&c->unlink);
b3c9f3c5 363 if (!c->access_ref || !c->dev) {
fa96b8ed 364 spin_unlock(&c->unlink);
9b28e148 365 return -ENODEV;
fa96b8ed 366 }
d8b082e6 367 kfifo_in(&c->fifo, &mbo, 1);
fa96b8ed 368 spin_unlock(&c->unlink);
9bc79bbc 369#ifdef DEBUG_MESG
d8b082e6 370 if (kfifo_is_full(&c->fifo))
08839388 371 dev_warn(c->dev, "Fifo is full\n");
9bc79bbc 372#endif
d8b082e6 373 wake_up_interruptible(&c->wq);
9bc79bbc
CG
374 return 0;
375}
376
377/**
1fd923f3 378 * comp_tx_completion - completion handler for tx channels
9bc79bbc
CG
379 * @iface: pointer to interface instance
380 * @channel_id: channel index/ID
381 *
382 * This wakes sleeping processes in the wait-queue.
383 */
1fd923f3 384static int comp_tx_completion(struct most_interface *iface, int channel_id)
9bc79bbc 385{
ef0fbbbb 386 struct comp_channel *c;
9bc79bbc 387
08839388
CG
388 c = get_channel(iface, channel_id);
389 if (!c)
78aee651 390 return -EINVAL;
08839388 391
9bc79bbc 392 if ((channel_id < 0) || (channel_id >= iface->num_channels)) {
08839388 393 dev_warn(c->dev, "Channel ID out of range\n");
9bc79bbc
CG
394 return -EINVAL;
395 }
396
d8b082e6 397 wake_up_interruptible(&c->wq);
9bc79bbc
CG
398 return 0;
399}
400
9bc79bbc 401/**
1fd923f3 402 * comp_probe - probe function of the driver module
9bc79bbc
CG
403 * @iface: pointer to interface instance
404 * @channel_id: channel index/ID
405 * @cfg: pointer to actual channel configuration
9bc79bbc 406 * @name: name of the device to be created
fba3993e 407 * @args: pointer to array of component parameters (from configfs)
9bc79bbc 408 *
fba3993e 409 * This allocates a channel object and creates the device node in /dev
9bc79bbc
CG
410 *
411 * Returns 0 on success or error code otherwise.
412 */
1fd923f3 413static int comp_probe(struct most_interface *iface, int channel_id,
dfee92dd 414 struct most_channel_config *cfg, char *name, char *args)
9bc79bbc 415{
ef0fbbbb 416 struct comp_channel *c;
9bc79bbc
CG
417 unsigned long cl_flags;
418 int retval;
419 int current_minor;
420
e8e0f7fd 421 if (!cfg || !name)
9bc79bbc 422 return -EINVAL;
61fd971e 423
d8b082e6
CG
424 c = get_channel(iface, channel_id);
425 if (c)
9bc79bbc
CG
426 return -EEXIST;
427
c73d915d 428 current_minor = ida_simple_get(&comp.minor_id, 0, 0, GFP_KERNEL);
9bc79bbc
CG
429 if (current_minor < 0)
430 return current_minor;
431
d8b082e6
CG
432 c = kzalloc(sizeof(*c), GFP_KERNEL);
433 if (!c) {
9bc79bbc 434 retval = -ENOMEM;
bddd3c25 435 goto err_remove_ida;
9bc79bbc
CG
436 }
437
c73d915d 438 c->devno = MKDEV(comp.major, current_minor);
d8b082e6
CG
439 cdev_init(&c->cdev, &channel_fops);
440 c->cdev.owner = THIS_MODULE;
5ae89078
CIK
441 retval = cdev_add(&c->cdev, c->devno, 1);
442 if (retval < 0)
443 goto err_free_c;
d8b082e6
CG
444 c->iface = iface;
445 c->cfg = cfg;
446 c->channel_id = channel_id;
b3c9f3c5 447 c->access_ref = 0;
fa96b8ed 448 spin_lock_init(&c->unlink);
d8b082e6
CG
449 INIT_KFIFO(c->fifo);
450 retval = kfifo_alloc(&c->fifo, cfg->num_buffers, GFP_KERNEL);
ebf256e3 451 if (retval)
bddd3c25 452 goto err_del_cdev_and_free_channel;
d8b082e6
CG
453 init_waitqueue_head(&c->wq);
454 mutex_init(&c->io_mutex);
9bc79bbc 455 spin_lock_irqsave(&ch_list_lock, cl_flags);
d8b082e6 456 list_add_tail(&c->list, &channel_list);
9bc79bbc 457 spin_unlock_irqrestore(&ch_list_lock, cl_flags);
c73d915d 458 c->dev = device_create(comp.class, NULL, c->devno, NULL, "%s", name);
9bc79bbc 459
ea398547
SM
460 if (IS_ERR(c->dev)) {
461 retval = PTR_ERR(c->dev);
bddd3c25 462 goto err_free_kfifo_and_del_list;
9bc79bbc 463 }
d8b082e6 464 kobject_uevent(&c->dev->kobj, KOBJ_ADD);
9bc79bbc
CG
465 return 0;
466
bddd3c25 467err_free_kfifo_and_del_list:
d8b082e6
CG
468 kfifo_free(&c->fifo);
469 list_del(&c->list);
bddd3c25 470err_del_cdev_and_free_channel:
d8b082e6 471 cdev_del(&c->cdev);
5ae89078 472err_free_c:
d8b082e6 473 kfree(c);
bddd3c25 474err_remove_ida:
c73d915d 475 ida_simple_remove(&comp.minor_id, current_minor);
9bc79bbc
CG
476 return retval;
477}
478
c73d915d
CG
479static struct cdev_component comp = {
480 .cc = {
08283d30 481 .mod = THIS_MODULE,
c73d915d
CG
482 .name = "cdev",
483 .probe_channel = comp_probe,
484 .disconnect_channel = comp_disconnect_channel,
485 .rx_completion = comp_rx_completion,
486 .tx_completion = comp_tx_completion,
487 },
9bc79bbc
CG
488};
489
ddb13810 490static int __init most_cdev_init(void)
9bc79bbc 491{
9b28e148
CG
492 int err;
493
c73d915d 494 comp.class = class_create(THIS_MODULE, "most_cdev");
61fd971e 495 if (IS_ERR(comp.class))
c73d915d 496 return PTR_ERR(comp.class);
c73d915d 497
c73d915d 498 ida_init(&comp.minor_id);
9bc79bbc 499
aba258b7 500 err = alloc_chrdev_region(&comp.devno, 0, CHRDEV_REGION_SIZE, "cdev");
324e87b7 501 if (err < 0)
1d9e3a07 502 goto dest_ida;
c73d915d
CG
503 comp.major = MAJOR(comp.devno);
504 err = most_register_component(&comp.cc);
9b28e148 505 if (err)
c73d915d 506 goto free_cdev;
919c03ae
CG
507 err = most_register_configfs_subsys(&comp.cc);
508 if (err)
509 goto deregister_comp;
9bc79bbc
CG
510 return 0;
511
919c03ae
CG
512deregister_comp:
513 most_deregister_component(&comp.cc);
9bc79bbc 514free_cdev:
aba258b7 515 unregister_chrdev_region(comp.devno, CHRDEV_REGION_SIZE);
1d9e3a07 516dest_ida:
c73d915d
CG
517 ida_destroy(&comp.minor_id);
518 class_destroy(comp.class);
9b28e148 519 return err;
9bc79bbc
CG
520}
521
ddb13810 522static void __exit most_cdev_exit(void)
9bc79bbc 523{
ef0fbbbb 524 struct comp_channel *c, *tmp;
9bc79bbc 525
919c03ae 526 most_deregister_configfs_subsys(&comp.cc);
c73d915d 527 most_deregister_component(&comp.cc);
9bc79bbc 528
d8b082e6
CG
529 list_for_each_entry_safe(c, tmp, &channel_list, list) {
530 destroy_cdev(c);
2c4aaa1f 531 destroy_channel(c);
9bc79bbc 532 }
af708900 533 unregister_chrdev_region(comp.devno, CHRDEV_REGION_SIZE);
c73d915d
CG
534 ida_destroy(&comp.minor_id);
535 class_destroy(comp.class);
9bc79bbc
CG
536}
537
ddb13810
RD
538module_init(most_cdev_init);
539module_exit(most_cdev_exit);
9bc79bbc
CG
540MODULE_AUTHOR("Christian Gromm <christian.gromm@microchip.com>");
541MODULE_LICENSE("GPL");
b7937dc4 542MODULE_DESCRIPTION("character device component for mostcore");