]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/staging/iio/industrialio-ring.c
staging: iio: remove specific chrdev for event reading. Get fd from ioctl on buffer.
[karo-tx-linux.git] / drivers / staging / iio / industrialio-ring.c
1 /* The industrial I/O core
2  *
3  * Copyright (c) 2008 Jonathan Cameron
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 as published by
7  * the Free Software Foundation.
8  *
9  * Handling of ring allocation / resizing.
10  *
11  *
12  * Things to look at here.
13  * - Better memory allocation techniques?
14  * - Alternative access techniques?
15  */
16 #include <linux/kernel.h>
17 #include <linux/device.h>
18 #include <linux/fs.h>
19 #include <linux/cdev.h>
20 #include <linux/slab.h>
21 #include <linux/poll.h>
22
23 #include "iio.h"
24 #include "iio_core.h"
25 #include "ring_generic.h"
26
27 /**
28  * iio_ring_open() - chrdev file open for ring buffer access
29  *
30  * This function relies on all ring buffer implementations having an
31  * iio_ring_buffer as their first element.
32  **/
33 static int iio_ring_open(struct inode *inode, struct file *filp)
34 {
35         struct iio_handler *hand
36                 = container_of(inode->i_cdev, struct iio_handler, chrdev);
37         struct iio_ring_buffer *rb = hand->private;
38
39         filp->private_data = hand->private;
40         if (rb->access->mark_in_use)
41                 rb->access->mark_in_use(rb);
42
43         return 0;
44 }
45
46 /**
47  * iio_ring_release() - chrdev file close ring buffer access
48  *
49  * This function relies on all ring buffer implementations having an
50  * iio_ring_buffer as their first element.
51  **/
52 static int iio_ring_release(struct inode *inode, struct file *filp)
53 {
54         struct cdev *cd = inode->i_cdev;
55         struct iio_handler *hand = iio_cdev_to_handler(cd);
56         struct iio_ring_buffer *rb = hand->private;
57
58         clear_bit(IIO_BUSY_BIT_POS, &rb->access_handler.flags);
59         if (rb->access->unmark_in_use)
60                 rb->access->unmark_in_use(rb);
61
62         return 0;
63 }
64
65 /**
66  * iio_ring_read_first_n_outer() - chrdev read for ring buffer access
67  *
68  * This function relies on all ring buffer implementations having an
69  * iio_ring _bufer as their first element.
70  **/
71 static ssize_t iio_ring_read_first_n_outer(struct file *filp, char __user *buf,
72                                   size_t n, loff_t *f_ps)
73 {
74         struct iio_ring_buffer *rb = filp->private_data;
75
76         if (!rb->access->read_first_n)
77                 return -EINVAL;
78         return rb->access->read_first_n(rb, n, buf);
79 }
80
81 /**
82  * iio_ring_poll() - poll the ring to find out if it has data
83  */
84 static unsigned int iio_ring_poll(struct file *filp,
85                                   struct poll_table_struct *wait)
86 {
87         struct iio_ring_buffer *rb = filp->private_data;
88
89         poll_wait(filp, &rb->pollq, wait);
90         if (rb->stufftoread)
91                 return POLLIN | POLLRDNORM;
92         /* need a way of knowing if there may be enough data... */
93         return 0;
94 }
95
96 /* Somewhat of a cross file organization violation - ioctls here are actually
97  * event related */
98 static long iio_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
99 {
100
101         struct iio_ring_buffer *rb = f->private_data;
102         struct iio_dev *indio_dev = rb->indio_dev;
103         int __user *ip = (int __user *)arg;
104
105         if (cmd == IIO_GET_EVENT_FD_IOCTL) {
106                 int fd;
107                 fd = iio_event_getfd(indio_dev);
108                 if (copy_to_user(ip, &fd, sizeof(fd)))
109                         return -EFAULT;
110                 return 0;
111         }
112         return -EINVAL;
113 }
114
115 static const struct file_operations iio_ring_fileops = {
116         .read = iio_ring_read_first_n_outer,
117         .release = iio_ring_release,
118         .open = iio_ring_open,
119         .poll = iio_ring_poll,
120         .owner = THIS_MODULE,
121         .llseek = noop_llseek,
122         .unlocked_ioctl = iio_ioctl,
123         .compat_ioctl = iio_ioctl,
124 };
125
126 void iio_ring_access_release(struct device *dev)
127 {
128         struct iio_ring_buffer *buf
129                 = container_of(dev, struct iio_ring_buffer, dev);
130         cdev_del(&buf->access_handler.chrdev);
131         iio_device_free_chrdev_minor(MINOR(dev->devt));
132 }
133 EXPORT_SYMBOL(iio_ring_access_release);
134
135 static inline int
136 __iio_request_ring_buffer_chrdev(struct iio_ring_buffer *buf,
137                                  struct module *owner,
138                                  int id)
139 {
140         int ret;
141
142         buf->access_handler.flags = 0;
143         buf->dev.bus = &iio_bus_type;
144         device_initialize(&buf->dev);
145
146         ret = iio_device_get_chrdev_minor();
147         if (ret < 0)
148                 goto error_device_put;
149
150         buf->dev.devt = MKDEV(MAJOR(iio_devt), ret);
151         dev_set_name(&buf->dev, "%s:buffer%d",
152                      dev_name(buf->dev.parent),
153                      id);
154         ret = device_add(&buf->dev);
155         if (ret < 0) {
156                 printk(KERN_ERR "failed to add the ring dev\n");
157                 goto error_device_put;
158         }
159         cdev_init(&buf->access_handler.chrdev, &iio_ring_fileops);
160         buf->access_handler.chrdev.owner = owner;
161         ret = cdev_add(&buf->access_handler.chrdev, buf->dev.devt, 1);
162         if (ret) {
163                 printk(KERN_ERR "failed to allocate ring chrdev\n");
164                 goto error_device_unregister;
165         }
166         return 0;
167
168 error_device_unregister:
169         device_unregister(&buf->dev);
170 error_device_put:
171         put_device(&buf->dev);
172
173         return ret;
174 }
175
176 static void __iio_free_ring_buffer_chrdev(struct iio_ring_buffer *buf)
177 {
178         device_unregister(&buf->dev);
179 }
180
181 void iio_ring_buffer_init(struct iio_ring_buffer *ring,
182                           struct iio_dev *dev_info)
183 {
184         ring->indio_dev = dev_info;
185         ring->access_handler.private = ring;
186         init_waitqueue_head(&ring->pollq);
187 }
188 EXPORT_SYMBOL(iio_ring_buffer_init);
189
190 static ssize_t iio_show_scan_index(struct device *dev,
191                                    struct device_attribute *attr,
192                                    char *buf)
193 {
194         return sprintf(buf, "%u\n", to_iio_dev_attr(attr)->c->scan_index);
195 }
196
197 static ssize_t iio_show_fixed_type(struct device *dev,
198                                    struct device_attribute *attr,
199                                    char *buf)
200 {
201         struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
202         return sprintf(buf, "%c%d/%d>>%u\n",
203                        this_attr->c->scan_type.sign,
204                        this_attr->c->scan_type.realbits,
205                        this_attr->c->scan_type.storagebits,
206                        this_attr->c->scan_type.shift);
207 }
208
209 static ssize_t iio_scan_el_show(struct device *dev,
210                                 struct device_attribute *attr,
211                                 char *buf)
212 {
213         int ret;
214         struct iio_ring_buffer *ring = dev_get_drvdata(dev);
215
216         ret = iio_scan_mask_query(ring, to_iio_dev_attr(attr)->address);
217         if (ret < 0)
218                 return ret;
219         return sprintf(buf, "%d\n", ret);
220 }
221
222 static int iio_scan_mask_clear(struct iio_ring_buffer *ring, int bit)
223 {
224         if (bit > IIO_MAX_SCAN_LENGTH)
225                 return -EINVAL;
226         ring->scan_mask &= ~(1 << bit);
227         ring->scan_count--;
228         return 0;
229 }
230
231 static ssize_t iio_scan_el_store(struct device *dev,
232                                  struct device_attribute *attr,
233                                  const char *buf,
234                                  size_t len)
235 {
236         int ret = 0;
237         bool state;
238         struct iio_ring_buffer *ring = dev_get_drvdata(dev);
239         struct iio_dev *indio_dev = ring->indio_dev;
240         struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
241
242         state = !(buf[0] == '0');
243         mutex_lock(&indio_dev->mlock);
244         if (indio_dev->currentmode == INDIO_RING_TRIGGERED) {
245                 ret = -EBUSY;
246                 goto error_ret;
247         }
248         ret = iio_scan_mask_query(ring, this_attr->address);
249         if (ret < 0)
250                 goto error_ret;
251         if (!state && ret) {
252                 ret = iio_scan_mask_clear(ring, this_attr->address);
253                 if (ret)
254                         goto error_ret;
255         } else if (state && !ret) {
256                 ret = iio_scan_mask_set(ring, this_attr->address);
257                 if (ret)
258                         goto error_ret;
259         }
260
261 error_ret:
262         mutex_unlock(&indio_dev->mlock);
263
264         return ret ? ret : len;
265
266 }
267
268 static ssize_t iio_scan_el_ts_show(struct device *dev,
269                                    struct device_attribute *attr,
270                                    char *buf)
271 {
272         struct iio_ring_buffer *ring = dev_get_drvdata(dev);
273         return sprintf(buf, "%d\n", ring->scan_timestamp);
274 }
275
276 static ssize_t iio_scan_el_ts_store(struct device *dev,
277                                     struct device_attribute *attr,
278                                     const char *buf,
279                                     size_t len)
280 {
281         int ret = 0;
282         struct iio_ring_buffer *ring = dev_get_drvdata(dev);
283         struct iio_dev *indio_dev = ring->indio_dev;
284         bool state;
285         state = !(buf[0] == '0');
286         mutex_lock(&indio_dev->mlock);
287         if (indio_dev->currentmode == INDIO_RING_TRIGGERED) {
288                 ret = -EBUSY;
289                 goto error_ret;
290         }
291         ring->scan_timestamp = state;
292 error_ret:
293         mutex_unlock(&indio_dev->mlock);
294
295         return ret ? ret : len;
296 }
297
298 static int iio_ring_add_channel_sysfs(struct iio_ring_buffer *ring,
299                                       const struct iio_chan_spec *chan)
300 {
301         int ret;
302
303         ret = __iio_add_chan_devattr("index", "scan_elements",
304                                      chan,
305                                      &iio_show_scan_index,
306                                      NULL,
307                                      0,
308                                      0,
309                                      &ring->dev,
310                                      &ring->scan_el_dev_attr_list);
311         if (ret)
312                 goto error_ret;
313
314         ret = __iio_add_chan_devattr("type", "scan_elements",
315                                      chan,
316                                      &iio_show_fixed_type,
317                                      NULL,
318                                      0,
319                                      0,
320                                      &ring->dev,
321                                      &ring->scan_el_dev_attr_list);
322         if (ret)
323                 goto error_ret;
324
325         if (chan->type != IIO_TIMESTAMP)
326                 ret = __iio_add_chan_devattr("en", "scan_elements",
327                                              chan,
328                                              &iio_scan_el_show,
329                                              &iio_scan_el_store,
330                                              chan->scan_index,
331                                              0,
332                                              &ring->dev,
333                                              &ring->scan_el_dev_attr_list);
334         else
335                 ret = __iio_add_chan_devattr("en", "scan_elements",
336                                              chan,
337                                              &iio_scan_el_ts_show,
338                                              &iio_scan_el_ts_store,
339                                              chan->scan_index,
340                                              0,
341                                              &ring->dev,
342                                              &ring->scan_el_dev_attr_list);
343 error_ret:
344         return ret;
345 }
346
347 static void iio_ring_remove_and_free_scan_dev_attr(struct iio_ring_buffer *ring,
348                                                    struct iio_dev_attr *p)
349 {
350         sysfs_remove_file_from_group(&ring->dev.kobj,
351                                      &p->dev_attr.attr, "scan_elements");
352         kfree(p->dev_attr.attr.name);
353         kfree(p);
354 }
355
356 static struct attribute *iio_scan_el_dummy_attrs[] = {
357         NULL
358 };
359
360 static struct attribute_group iio_scan_el_dummy_group = {
361         .name = "scan_elements",
362         .attrs = iio_scan_el_dummy_attrs
363 };
364
365 static void __iio_ring_attr_cleanup(struct iio_ring_buffer *ring)
366 {
367         struct iio_dev_attr *p, *n;
368         int anydynamic = !list_empty(&ring->scan_el_dev_attr_list);
369         list_for_each_entry_safe(p, n,
370                                  &ring->scan_el_dev_attr_list, l)
371                 iio_ring_remove_and_free_scan_dev_attr(ring, p);
372
373         if (ring->scan_el_attrs)
374                 sysfs_remove_group(&ring->dev.kobj,
375                                    ring->scan_el_attrs);
376         else if (anydynamic)
377                 sysfs_remove_group(&ring->dev.kobj,
378                                    &iio_scan_el_dummy_group);
379 }
380
381 int iio_ring_buffer_register_ex(struct iio_ring_buffer *ring, int id,
382                                 const struct iio_chan_spec *channels,
383                                 int num_channels)
384 {
385         int ret, i;
386
387         ret = __iio_request_ring_buffer_chrdev(ring, ring->owner, id);
388         if (ret)
389                 goto error_ret;
390
391         if (ring->scan_el_attrs) {
392                 ret = sysfs_create_group(&ring->dev.kobj,
393                                          ring->scan_el_attrs);
394                 if (ret) {
395                         dev_err(&ring->dev,
396                                 "Failed to add sysfs scan elements\n");
397                         goto error_free_ring_buffer_chrdev;
398                 }
399         } else if (channels) {
400                 ret = sysfs_create_group(&ring->dev.kobj,
401                                          &iio_scan_el_dummy_group);
402                 if (ret)
403                         goto error_free_ring_buffer_chrdev;
404         }
405
406         INIT_LIST_HEAD(&ring->scan_el_dev_attr_list);
407         if (channels) {
408                 /* new magic */
409                 for (i = 0; i < num_channels; i++) {
410                         ret = iio_ring_add_channel_sysfs(ring, &channels[i]);
411                         if (ret < 0)
412                                 goto error_cleanup_dynamic;
413                 }
414         }
415
416         return 0;
417 error_cleanup_dynamic:
418         __iio_ring_attr_cleanup(ring);
419 error_free_ring_buffer_chrdev:
420         __iio_free_ring_buffer_chrdev(ring);
421 error_ret:
422         return ret;
423 }
424 EXPORT_SYMBOL(iio_ring_buffer_register_ex);
425
426 void iio_ring_buffer_unregister(struct iio_ring_buffer *ring)
427 {
428         __iio_ring_attr_cleanup(ring);
429         __iio_free_ring_buffer_chrdev(ring);
430 }
431 EXPORT_SYMBOL(iio_ring_buffer_unregister);
432
433 ssize_t iio_read_ring_length(struct device *dev,
434                              struct device_attribute *attr,
435                              char *buf)
436 {
437         struct iio_ring_buffer *ring = dev_get_drvdata(dev);
438
439         if (ring->access->get_length)
440                 return sprintf(buf, "%d\n",
441                                ring->access->get_length(ring));
442
443         return 0;
444 }
445 EXPORT_SYMBOL(iio_read_ring_length);
446
447 ssize_t iio_write_ring_length(struct device *dev,
448                                struct device_attribute *attr,
449                                const char *buf,
450                                size_t len)
451 {
452         int ret;
453         ulong val;
454         struct iio_ring_buffer *ring = dev_get_drvdata(dev);
455
456         ret = strict_strtoul(buf, 10, &val);
457         if (ret)
458                 return ret;
459
460         if (ring->access->get_length)
461                 if (val == ring->access->get_length(ring))
462                         return len;
463
464         if (ring->access->set_length) {
465                 ring->access->set_length(ring, val);
466                 if (ring->access->mark_param_change)
467                         ring->access->mark_param_change(ring);
468         }
469
470         return len;
471 }
472 EXPORT_SYMBOL(iio_write_ring_length);
473
474 ssize_t iio_read_ring_bytes_per_datum(struct device *dev,
475                           struct device_attribute *attr,
476                           char *buf)
477 {
478         struct iio_ring_buffer *ring = dev_get_drvdata(dev);
479
480         if (ring->access->get_bytes_per_datum)
481                 return sprintf(buf, "%d\n",
482                                ring->access->get_bytes_per_datum(ring));
483
484         return 0;
485 }
486 EXPORT_SYMBOL(iio_read_ring_bytes_per_datum);
487
488 ssize_t iio_store_ring_enable(struct device *dev,
489                               struct device_attribute *attr,
490                               const char *buf,
491                               size_t len)
492 {
493         int ret;
494         bool requested_state, current_state;
495         int previous_mode;
496         struct iio_ring_buffer *ring = dev_get_drvdata(dev);
497         struct iio_dev *dev_info = ring->indio_dev;
498
499         mutex_lock(&dev_info->mlock);
500         previous_mode = dev_info->currentmode;
501         requested_state = !(buf[0] == '0');
502         current_state = !!(previous_mode & INDIO_ALL_RING_MODES);
503         if (current_state == requested_state) {
504                 printk(KERN_INFO "iio-ring, current state requested again\n");
505                 goto done;
506         }
507         if (requested_state) {
508                 if (ring->setup_ops->preenable) {
509                         ret = ring->setup_ops->preenable(dev_info);
510                         if (ret) {
511                                 printk(KERN_ERR
512                                        "Buffer not started:"
513                                        "ring preenable failed\n");
514                                 goto error_ret;
515                         }
516                 }
517                 if (ring->access->request_update) {
518                         ret = ring->access->request_update(ring);
519                         if (ret) {
520                                 printk(KERN_INFO
521                                        "Buffer not started:"
522                                        "ring parameter update failed\n");
523                                 goto error_ret;
524                         }
525                 }
526                 if (ring->access->mark_in_use)
527                         ring->access->mark_in_use(ring);
528                 /* Definitely possible for devices to support both of these.*/
529                 if (dev_info->modes & INDIO_RING_TRIGGERED) {
530                         if (!dev_info->trig) {
531                                 printk(KERN_INFO
532                                        "Buffer not started: no trigger\n");
533                                 ret = -EINVAL;
534                                 if (ring->access->unmark_in_use)
535                                         ring->access->unmark_in_use(ring);
536                                 goto error_ret;
537                         }
538                         dev_info->currentmode = INDIO_RING_TRIGGERED;
539                 } else if (dev_info->modes & INDIO_RING_HARDWARE_BUFFER)
540                         dev_info->currentmode = INDIO_RING_HARDWARE_BUFFER;
541                 else { /* should never be reached */
542                         ret = -EINVAL;
543                         goto error_ret;
544                 }
545
546                 if (ring->setup_ops->postenable) {
547                         ret = ring->setup_ops->postenable(dev_info);
548                         if (ret) {
549                                 printk(KERN_INFO
550                                        "Buffer not started:"
551                                        "postenable failed\n");
552                                 if (ring->access->unmark_in_use)
553                                         ring->access->unmark_in_use(ring);
554                                 dev_info->currentmode = previous_mode;
555                                 if (ring->setup_ops->postdisable)
556                                         ring->setup_ops->postdisable(dev_info);
557                                 goto error_ret;
558                         }
559                 }
560         } else {
561                 if (ring->setup_ops->predisable) {
562                         ret = ring->setup_ops->predisable(dev_info);
563                         if (ret)
564                                 goto error_ret;
565                 }
566                 if (ring->access->unmark_in_use)
567                         ring->access->unmark_in_use(ring);
568                 dev_info->currentmode = INDIO_DIRECT_MODE;
569                 if (ring->setup_ops->postdisable) {
570                         ret = ring->setup_ops->postdisable(dev_info);
571                         if (ret)
572                                 goto error_ret;
573                 }
574         }
575 done:
576         mutex_unlock(&dev_info->mlock);
577         return len;
578
579 error_ret:
580         mutex_unlock(&dev_info->mlock);
581         return ret;
582 }
583 EXPORT_SYMBOL(iio_store_ring_enable);
584
585 ssize_t iio_show_ring_enable(struct device *dev,
586                                     struct device_attribute *attr,
587                                     char *buf)
588 {
589         struct iio_ring_buffer *ring = dev_get_drvdata(dev);
590         return sprintf(buf, "%d\n", !!(ring->indio_dev->currentmode
591                                        & INDIO_ALL_RING_MODES));
592 }
593 EXPORT_SYMBOL(iio_show_ring_enable);
594
595 int iio_sw_ring_preenable(struct iio_dev *indio_dev)
596 {
597         struct iio_ring_buffer *ring = indio_dev->ring;
598         size_t size;
599         dev_dbg(&indio_dev->dev, "%s\n", __func__);
600         /* Check if there are any scan elements enabled, if not fail*/
601         if (!(ring->scan_count || ring->scan_timestamp))
602                 return -EINVAL;
603         if (ring->scan_timestamp)
604                 if (ring->scan_count)
605                         /* Timestamp (aligned to s64) and data */
606                         size = (((ring->scan_count * ring->bpe)
607                                         + sizeof(s64) - 1)
608                                 & ~(sizeof(s64) - 1))
609                                 + sizeof(s64);
610                 else /* Timestamp only  */
611                         size = sizeof(s64);
612         else /* Data only */
613                 size = ring->scan_count * ring->bpe;
614         ring->access->set_bytes_per_datum(ring, size);
615
616         return 0;
617 }
618 EXPORT_SYMBOL(iio_sw_ring_preenable);