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