]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/staging/most/aim-cdev/cdev.c
Merge branch 'linus' of git://git.kernel.org/pub/scm/linux/kernel/git/herbert/crypto-2.6
[karo-tx-linux.git] / drivers / staging / most / aim-cdev / cdev.c
1 /*
2  * cdev.c - Application interfacing module for character devices
3  *
4  * Copyright (C) 2013-2015 Microchip Technology Germany II GmbH & Co. KG
5  *
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.
10  *
11  * This file is licensed under GPLv2.
12  */
13
14 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15 #include <linux/module.h>
16 #include <linux/sched.h>
17 #include <linux/fs.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>
24 #include "mostcore.h"
25
26 static dev_t aim_devno;
27 static struct class *aim_class;
28 static struct ida minor_id;
29 static unsigned int major;
30
31 struct aim_channel {
32         wait_queue_head_t wq;
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;
40         bool keep_mbo;
41         unsigned int mbo_offs;
42         struct mbo *stacked_mbo;
43         DECLARE_KFIFO_PTR(fifo, typeof(struct mbo *));
44         atomic_t access_ref;
45         struct list_head list;
46 };
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;
50
51
52 static struct aim_channel *get_channel(struct most_interface *iface, int id)
53 {
54         struct aim_channel *channel, *tmp;
55         unsigned long flags;
56         int found_channel = 0;
57
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)) {
61                         found_channel = 1;
62                         break;
63                 }
64         }
65         spin_unlock_irqrestore(&ch_list_lock, flags);
66         if (!found_channel)
67                 return NULL;
68         return channel;
69 }
70
71 /**
72  * aim_open - implements the syscall to open the device
73  * @inode: inode pointer
74  * @filp: file pointer
75  *
76  * This stores the channel pointer in the private data field of
77  * the file structure and activates the channel within the core.
78  */
79 static int aim_open(struct inode *inode, struct file *filp)
80 {
81         struct aim_channel *channel;
82         int ret;
83
84         channel = to_channel(inode->i_cdev);
85         filp->private_data = channel;
86
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");
92                 return -EACCES;
93         }
94         if (!atomic_inc_and_test(&channel->access_ref)) {
95                 pr_info("WARN: Device is busy\n");
96                 atomic_dec(&channel->access_ref);
97                 return -EBUSY;
98         }
99
100         ret = most_start_channel(channel->iface, channel->channel_id);
101         if (ret)
102                 atomic_dec(&channel->access_ref);
103         return ret;
104 }
105
106 /**
107  * aim_close - implements the syscall to close the device
108  * @inode: inode pointer
109  * @filp: file pointer
110  *
111  * This stops the channel within the core.
112  */
113 static int aim_close(struct inode *inode, struct file *filp)
114 {
115         int ret;
116         struct mbo *mbo;
117         struct aim_channel *channel = to_channel(inode->i_cdev);
118
119         mutex_lock(&channel->io_mutex);
120         if (!channel->dev) {
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);
129                 kfree(channel);
130                 return 0;
131         }
132         mutex_unlock(&channel->io_mutex);
133
134         while (0 != kfifo_out((struct kfifo *)&channel->fifo, &mbo, 1))
135                 most_put_mbo(mbo);
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);
141         return ret;
142 }
143
144 /**
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
150  */
151 static ssize_t aim_write(struct file *filp, const char __user *buf,
152                          size_t count, loff_t *offset)
153 {
154         int ret, err;
155         size_t actual_len = 0;
156         size_t max_len = 0;
157         ssize_t retval;
158         struct mbo *mbo;
159         struct aim_channel *channel = filp->private_data;
160
161         mutex_lock(&channel->io_mutex);
162         if (unlikely(!channel->dev)) {
163                 mutex_unlock(&channel->io_mutex);
164                 return -EPIPE;
165         }
166         mutex_unlock(&channel->io_mutex);
167
168         mbo = most_get_mbo(channel->iface, channel->channel_id);
169
170         if (!mbo && channel->dev) {
171                 if ((filp->f_flags & O_NONBLOCK))
172                         return -EAGAIN;
173                 if (wait_event_interruptible(
174                             channel->wq,
175                             (mbo = most_get_mbo(channel->iface,
176                                                 channel->channel_id)) ||
177                             (channel->dev == NULL)))
178                         return -ERESTARTSYS;
179         }
180
181         mutex_lock(&channel->io_mutex);
182         if (unlikely(!channel->dev)) {
183                 mutex_unlock(&channel->io_mutex);
184                 err = -EPIPE;
185                 goto error;
186         }
187         mutex_unlock(&channel->io_mutex);
188
189         max_len = channel->cfg->buffer_size;
190         actual_len = min(count, max_len);
191         mbo->buffer_length = actual_len;
192
193         retval = copy_from_user(mbo->virt_address, buf, mbo->buffer_length);
194         if (retval) {
195                 err = -EIO;
196                 goto error;
197         }
198
199         ret = most_submit_mbo(mbo);
200         if (ret) {
201                 pr_info("submitting MBO to core failed\n");
202                 err = ret;
203                 goto error;
204         }
205         return actual_len - retval;
206 error:
207         if (mbo)
208                 most_put_mbo(mbo);
209         return err;
210 }
211
212 /**
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
218  */
219 static ssize_t
220 aim_read(struct file *filp, char __user *buf, size_t count, loff_t *offset)
221 {
222         ssize_t retval;
223         size_t not_copied, proc_len;
224         struct mbo *mbo;
225         struct aim_channel *channel = filp->private_data;
226
227         if (channel->keep_mbo == true) {
228                 mbo = channel->stacked_mbo;
229                 channel->keep_mbo = false;
230                 goto start_copy;
231         }
232         while ((0 == kfifo_out(&channel->fifo, &mbo, 1))
233                && (channel->dev != NULL)) {
234                 if (filp->f_flags & O_NONBLOCK)
235                         return -EAGAIN;
236                 if (wait_event_interruptible(channel->wq,
237                                              (!kfifo_is_empty(&channel->fifo) ||
238                                               (channel->dev == NULL))))
239                         return -ERESTARTSYS;
240         }
241
242 start_copy:
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);
247                 return -EIO;
248         }
249
250         if (count < mbo->processed_length)
251                 channel->keep_mbo = true;
252
253         proc_len = min((int)count,
254                        (int)(mbo->processed_length - channel->mbo_offs));
255
256         not_copied = copy_to_user(buf,
257                                   mbo->virt_address + channel->mbo_offs,
258                                   proc_len);
259
260         retval = not_copied ? proc_len - not_copied : proc_len;
261
262         if (channel->keep_mbo == true) {
263                 channel->mbo_offs = retval;
264                 channel->stacked_mbo = mbo;
265         } else {
266                 most_put_mbo(mbo);
267                 channel->mbo_offs = 0;
268         }
269         mutex_unlock(&channel->io_mutex);
270         return retval;
271 }
272
273 /**
274  * Initialization of struct file_operations
275  */
276 static const struct file_operations channel_fops = {
277         .owner = THIS_MODULE,
278         .read = aim_read,
279         .write = aim_write,
280         .open = aim_open,
281         .release = aim_close,
282 };
283
284 /**
285  * aim_disconnect_channel - disconnect a channel
286  * @iface: pointer to interface instance
287  * @channel_id: channel index
288  *
289  * This frees allocated memory and removes the cdev that represents this
290  * channel in user space.
291  */
292 static int aim_disconnect_channel(struct most_interface *iface, int channel_id)
293 {
294         struct aim_channel *channel;
295         unsigned long flags;
296
297         if (!iface) {
298                 pr_info("Bad interface pointer\n");
299                 return -EINVAL;
300         }
301
302         channel = get_channel(iface, channel_id);
303         if (channel == NULL)
304                 return -ENXIO;
305
306         mutex_lock(&channel->io_mutex);
307         channel->dev = NULL;
308         mutex_unlock(&channel->io_mutex);
309
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);
318                 kfree(channel);
319         } else {
320                 wake_up_interruptible(&channel->wq);
321         }
322         return 0;
323 }
324
325 /**
326  * aim_rx_completion - completion handler for rx channels
327  * @mbo: pointer to buffer object that has completed
328  *
329  * This searches for the channel linked to this MBO and stores it in the local
330  * fifo buffer.
331  */
332 static int aim_rx_completion(struct mbo *mbo)
333 {
334         struct aim_channel *channel;
335
336         if (!mbo)
337                 return -EINVAL;
338
339         channel = get_channel(mbo->ifp, mbo->hdm_channel_id);
340         if (channel == NULL)
341                 return -ENXIO;
342
343         kfifo_in(&channel->fifo, &mbo, 1);
344 #ifdef DEBUG_MESG
345         if (kfifo_is_full(&channel->fifo))
346                 pr_info("WARN: Fifo is full\n");
347 #endif
348         wake_up_interruptible(&channel->wq);
349         return 0;
350 }
351
352 /**
353  * aim_tx_completion - completion handler for tx channels
354  * @iface: pointer to interface instance
355  * @channel_id: channel index/ID
356  *
357  * This wakes sleeping processes in the wait-queue.
358  */
359 static int aim_tx_completion(struct most_interface *iface, int channel_id)
360 {
361         struct aim_channel *channel;
362
363         if (!iface) {
364                 pr_info("Bad interface pointer\n");
365                 return -EINVAL;
366         }
367         if ((channel_id < 0) || (channel_id >= iface->num_channels)) {
368                 pr_info("Channel ID out of range\n");
369                 return -EINVAL;
370         }
371
372         channel = get_channel(iface, channel_id);
373         if (channel == NULL)
374                 return -ENXIO;
375         wake_up_interruptible(&channel->wq);
376         return 0;
377 }
378
379 static struct most_aim cdev_aim;
380
381 /**
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
388  *
389  * This allocates achannel object and creates the device node in /dev
390  *
391  * Returns 0 on success or error code otherwise.
392  */
393 static int aim_probe(struct most_interface *iface, int channel_id,
394                      struct most_channel_config *cfg,
395                      struct kobject *parent, char *name)
396 {
397         struct aim_channel *channel;
398         unsigned long cl_flags;
399         int retval;
400         int current_minor;
401
402         if ((!iface) || (!cfg) || (!parent) || (!name)) {
403                 pr_info("Probing AIM with bad arguments");
404                 return -EINVAL;
405         }
406         channel = get_channel(iface, channel_id);
407         if (channel)
408                 return -EEXIST;
409
410         current_minor = ida_simple_get(&minor_id, 0, 0, GFP_KERNEL);
411         if (current_minor < 0)
412                 return current_minor;
413
414         channel = kzalloc(sizeof(*channel), GFP_KERNEL);
415         if (!channel) {
416                 pr_info("failed to alloc channel object\n");
417                 retval = -ENOMEM;
418                 goto error_alloc_channel;
419         }
420
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;
426         channel->cfg = cfg;
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);
432         if (retval) {
433                 pr_info("failed to alloc channel kfifo");
434                 goto error_alloc_kfifo;
435         }
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,
442                                      NULL,
443                                      channel->devno,
444                                      NULL,
445                                      "%s", name);
446
447         retval = IS_ERR(channel->dev);
448         if (retval) {
449                 pr_info("failed to create new device node %s\n", name);
450                 goto error_create_device;
451         }
452         kobject_uevent(&channel->dev->kobj, KOBJ_ADD);
453         return 0;
454
455 error_create_device:
456         kfifo_free(&channel->fifo);
457         list_del(&channel->list);
458 error_alloc_kfifo:
459         cdev_del(&channel->cdev);
460         kfree(channel);
461 error_alloc_channel:
462         ida_simple_remove(&minor_id, current_minor);
463         return retval;
464 }
465
466 static struct most_aim cdev_aim = {
467         .name = "cdev",
468         .probe_channel = aim_probe,
469         .disconnect_channel = aim_disconnect_channel,
470         .rx_completion = aim_rx_completion,
471         .tx_completion = aim_tx_completion,
472 };
473
474 static int __init mod_init(void)
475 {
476         pr_info("init()\n");
477
478         INIT_LIST_HEAD(&channel_list);
479         spin_lock_init(&ch_list_lock);
480         ida_init(&minor_id);
481
482         if (alloc_chrdev_region(&aim_devno, 0, 50, "cdev") < 0)
483                 return -EIO;
484         major = MAJOR(aim_devno);
485
486         aim_class = class_create(THIS_MODULE, "most_cdev_aim");
487         if (IS_ERR(aim_class)) {
488                 pr_err("no udev support\n");
489                 goto free_cdev;
490         }
491
492         if (most_register_aim(&cdev_aim))
493                 goto dest_class;
494         return 0;
495
496 dest_class:
497         class_destroy(aim_class);
498 free_cdev:
499         unregister_chrdev_region(aim_devno, 1);
500         return -EIO;
501 }
502
503 static void __exit mod_exit(void)
504 {
505         struct aim_channel *channel, *tmp;
506
507         pr_info("exit module\n");
508
509         most_deregister_aim(&cdev_aim);
510
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));
517                 kfree(channel);
518         }
519         class_destroy(aim_class);
520         unregister_chrdev_region(aim_devno, 1);
521         ida_destroy(&minor_id);
522 }
523
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");