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