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