staging: brcm80211: remove brcms_b_dotxstatus wrapper function
[zen-stable.git] / drivers / staging / iio / industrialio-buffer.c
blob60929ff633e530f2d74e68dbdcf6b5fc739adb4d
1 /* The industrial I/O core
3 * Copyright (c) 2008 Jonathan Cameron
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.
9 * Handling of buffer allocation / resizing.
12 * Things to look at here.
13 * - Better memory allocation techniques?
14 * - Alternative access techniques?
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>
23 #include "iio.h"
24 #include "iio_core.h"
25 #include "sysfs.h"
26 #include "buffer_generic.h"
28 static const char * const iio_endian_prefix[] = {
29 [IIO_BE] = "be",
30 [IIO_LE] = "le",
33 /**
34 * iio_buffer_read_first_n_outer() - chrdev read for buffer access
36 * This function relies on all buffer implementations having an
37 * iio_buffer as their first element.
38 **/
39 ssize_t iio_buffer_read_first_n_outer(struct file *filp, char __user *buf,
40 size_t n, loff_t *f_ps)
42 struct iio_dev *indio_dev = filp->private_data;
43 struct iio_buffer *rb = indio_dev->buffer;
45 if (!rb->access->read_first_n)
46 return -EINVAL;
47 return rb->access->read_first_n(rb, n, buf);
50 /**
51 * iio_buffer_poll() - poll the buffer to find out if it has data
53 unsigned int iio_buffer_poll(struct file *filp,
54 struct poll_table_struct *wait)
56 struct iio_dev *indio_dev = filp->private_data;
57 struct iio_buffer *rb = indio_dev->buffer;
59 poll_wait(filp, &rb->pollq, wait);
60 if (rb->stufftoread)
61 return POLLIN | POLLRDNORM;
62 /* need a way of knowing if there may be enough data... */
63 return 0;
66 int iio_chrdev_buffer_open(struct iio_dev *indio_dev)
68 struct iio_buffer *rb = indio_dev->buffer;
69 if (!rb)
70 return -EINVAL;
71 if (rb->access->mark_in_use)
72 rb->access->mark_in_use(rb);
73 return 0;
76 void iio_chrdev_buffer_release(struct iio_dev *indio_dev)
78 struct iio_buffer *rb = indio_dev->buffer;
80 clear_bit(IIO_BUSY_BIT_POS, &rb->flags);
81 if (rb->access->unmark_in_use)
82 rb->access->unmark_in_use(rb);
85 void iio_buffer_init(struct iio_buffer *buffer, struct iio_dev *dev_info)
87 buffer->indio_dev = dev_info;
88 init_waitqueue_head(&buffer->pollq);
90 EXPORT_SYMBOL(iio_buffer_init);
92 static ssize_t iio_show_scan_index(struct device *dev,
93 struct device_attribute *attr,
94 char *buf)
96 return sprintf(buf, "%u\n", to_iio_dev_attr(attr)->c->scan_index);
99 static ssize_t iio_show_fixed_type(struct device *dev,
100 struct device_attribute *attr,
101 char *buf)
103 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
104 u8 type = this_attr->c->scan_type.endianness;
106 if (type == IIO_CPU) {
107 #ifdef __LITTLE_ENDIAN
108 type = IIO_LE;
109 #else
110 type = IIO_BE;
111 #endif
113 return sprintf(buf, "%s:%c%d/%d>>%u\n",
114 iio_endian_prefix[type],
115 this_attr->c->scan_type.sign,
116 this_attr->c->scan_type.realbits,
117 this_attr->c->scan_type.storagebits,
118 this_attr->c->scan_type.shift);
121 static ssize_t iio_scan_el_show(struct device *dev,
122 struct device_attribute *attr,
123 char *buf)
125 int ret;
126 struct iio_dev *dev_info = dev_get_drvdata(dev);
128 ret = iio_scan_mask_query(dev_info->buffer,
129 to_iio_dev_attr(attr)->address);
130 if (ret < 0)
131 return ret;
132 return sprintf(buf, "%d\n", ret);
135 static int iio_scan_mask_clear(struct iio_buffer *buffer, int bit)
137 clear_bit(bit, buffer->scan_mask);
138 buffer->scan_count--;
139 return 0;
142 static ssize_t iio_scan_el_store(struct device *dev,
143 struct device_attribute *attr,
144 const char *buf,
145 size_t len)
147 int ret = 0;
148 bool state;
149 struct iio_dev *indio_dev = dev_get_drvdata(dev);
150 struct iio_buffer *buffer = indio_dev->buffer;
151 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
153 state = !(buf[0] == '0');
154 mutex_lock(&indio_dev->mlock);
155 if (indio_dev->currentmode == INDIO_BUFFER_TRIGGERED) {
156 ret = -EBUSY;
157 goto error_ret;
159 ret = iio_scan_mask_query(buffer, this_attr->address);
160 if (ret < 0)
161 goto error_ret;
162 if (!state && ret) {
163 ret = iio_scan_mask_clear(buffer, this_attr->address);
164 if (ret)
165 goto error_ret;
166 } else if (state && !ret) {
167 ret = iio_scan_mask_set(buffer, this_attr->address);
168 if (ret)
169 goto error_ret;
172 error_ret:
173 mutex_unlock(&indio_dev->mlock);
175 return ret ? ret : len;
179 static ssize_t iio_scan_el_ts_show(struct device *dev,
180 struct device_attribute *attr,
181 char *buf)
183 struct iio_dev *dev_info = dev_get_drvdata(dev);
184 return sprintf(buf, "%d\n", dev_info->buffer->scan_timestamp);
187 static ssize_t iio_scan_el_ts_store(struct device *dev,
188 struct device_attribute *attr,
189 const char *buf,
190 size_t len)
192 int ret = 0;
193 struct iio_dev *indio_dev = dev_get_drvdata(dev);
194 bool state;
196 state = !(buf[0] == '0');
197 mutex_lock(&indio_dev->mlock);
198 if (indio_dev->currentmode == INDIO_BUFFER_TRIGGERED) {
199 ret = -EBUSY;
200 goto error_ret;
202 indio_dev->buffer->scan_timestamp = state;
203 error_ret:
204 mutex_unlock(&indio_dev->mlock);
206 return ret ? ret : len;
209 static int iio_buffer_add_channel_sysfs(struct iio_dev *indio_dev,
210 const struct iio_chan_spec *chan)
212 int ret, attrcount = 0;
213 struct iio_buffer *buffer = indio_dev->buffer;
215 ret = __iio_add_chan_devattr("index",
216 chan,
217 &iio_show_scan_index,
218 NULL,
221 &indio_dev->dev,
222 &buffer->scan_el_dev_attr_list);
223 if (ret)
224 goto error_ret;
225 attrcount++;
226 ret = __iio_add_chan_devattr("type",
227 chan,
228 &iio_show_fixed_type,
229 NULL,
232 &indio_dev->dev,
233 &buffer->scan_el_dev_attr_list);
234 if (ret)
235 goto error_ret;
236 attrcount++;
237 if (chan->type != IIO_TIMESTAMP)
238 ret = __iio_add_chan_devattr("en",
239 chan,
240 &iio_scan_el_show,
241 &iio_scan_el_store,
242 chan->scan_index,
244 &indio_dev->dev,
245 &buffer->scan_el_dev_attr_list);
246 else
247 ret = __iio_add_chan_devattr("en",
248 chan,
249 &iio_scan_el_ts_show,
250 &iio_scan_el_ts_store,
251 chan->scan_index,
253 &indio_dev->dev,
254 &buffer->scan_el_dev_attr_list);
255 attrcount++;
256 ret = attrcount;
257 error_ret:
258 return ret;
261 static void iio_buffer_remove_and_free_scan_dev_attr(struct iio_dev *indio_dev,
262 struct iio_dev_attr *p)
264 kfree(p->dev_attr.attr.name);
265 kfree(p);
268 static void __iio_buffer_attr_cleanup(struct iio_dev *indio_dev)
270 struct iio_dev_attr *p, *n;
271 struct iio_buffer *buffer = indio_dev->buffer;
273 list_for_each_entry_safe(p, n,
274 &buffer->scan_el_dev_attr_list, l)
275 iio_buffer_remove_and_free_scan_dev_attr(indio_dev, p);
278 static const char * const iio_scan_elements_group_name = "scan_elements";
280 int iio_buffer_register(struct iio_dev *indio_dev,
281 const struct iio_chan_spec *channels,
282 int num_channels)
284 struct iio_dev_attr *p;
285 struct attribute **attr;
286 struct iio_buffer *buffer = indio_dev->buffer;
287 int ret, i, attrn, attrcount, attrcount_orig = 0;
289 if (buffer->attrs)
290 indio_dev->groups[indio_dev->groupcounter++] = buffer->attrs;
292 if (buffer->scan_el_attrs != NULL) {
293 attr = buffer->scan_el_attrs->attrs;
294 while (*attr++ != NULL)
295 attrcount_orig++;
297 attrcount = attrcount_orig;
298 INIT_LIST_HEAD(&buffer->scan_el_dev_attr_list);
299 if (channels) {
300 /* new magic */
301 for (i = 0; i < num_channels; i++) {
302 /* Establish necessary mask length */
303 if (channels[i].scan_index >
304 (int)indio_dev->masklength - 1)
305 indio_dev->masklength
306 = indio_dev->channels[i].scan_index + 1;
308 ret = iio_buffer_add_channel_sysfs(indio_dev,
309 &channels[i]);
310 if (ret < 0)
311 goto error_cleanup_dynamic;
312 attrcount += ret;
314 if (indio_dev->masklength && buffer->scan_mask == NULL) {
315 buffer->scan_mask
316 = kzalloc(sizeof(*buffer->scan_mask)*
317 BITS_TO_LONGS(indio_dev->masklength),
318 GFP_KERNEL);
319 if (buffer->scan_mask == NULL) {
320 ret = -ENOMEM;
321 goto error_cleanup_dynamic;
326 buffer->scan_el_group.name = iio_scan_elements_group_name;
328 buffer->scan_el_group.attrs
329 = kzalloc(sizeof(buffer->scan_el_group.attrs[0])*
330 (attrcount + 1),
331 GFP_KERNEL);
332 if (buffer->scan_el_group.attrs == NULL) {
333 ret = -ENOMEM;
334 goto error_free_scan_mask;
336 if (buffer->scan_el_attrs)
337 memcpy(buffer->scan_el_group.attrs, buffer->scan_el_attrs,
338 sizeof(buffer->scan_el_group.attrs[0])*attrcount_orig);
339 attrn = attrcount_orig;
341 list_for_each_entry(p, &buffer->scan_el_dev_attr_list, l)
342 buffer->scan_el_group.attrs[attrn++] = &p->dev_attr.attr;
343 indio_dev->groups[indio_dev->groupcounter++] = &buffer->scan_el_group;
345 return 0;
347 error_free_scan_mask:
348 kfree(buffer->scan_mask);
349 error_cleanup_dynamic:
350 __iio_buffer_attr_cleanup(indio_dev);
352 return ret;
354 EXPORT_SYMBOL(iio_buffer_register);
356 void iio_buffer_unregister(struct iio_dev *indio_dev)
358 kfree(indio_dev->buffer->scan_mask);
359 kfree(indio_dev->buffer->scan_el_group.attrs);
360 __iio_buffer_attr_cleanup(indio_dev);
362 EXPORT_SYMBOL(iio_buffer_unregister);
364 ssize_t iio_buffer_read_length(struct device *dev,
365 struct device_attribute *attr,
366 char *buf)
368 struct iio_dev *indio_dev = dev_get_drvdata(dev);
369 struct iio_buffer *buffer = indio_dev->buffer;
371 if (buffer->access->get_length)
372 return sprintf(buf, "%d\n",
373 buffer->access->get_length(buffer));
375 return 0;
377 EXPORT_SYMBOL(iio_buffer_read_length);
379 ssize_t iio_buffer_write_length(struct device *dev,
380 struct device_attribute *attr,
381 const char *buf,
382 size_t len)
384 int ret;
385 ulong val;
386 struct iio_dev *indio_dev = dev_get_drvdata(dev);
387 struct iio_buffer *buffer = indio_dev->buffer;
389 ret = strict_strtoul(buf, 10, &val);
390 if (ret)
391 return ret;
393 if (buffer->access->get_length)
394 if (val == buffer->access->get_length(buffer))
395 return len;
397 if (buffer->access->set_length) {
398 buffer->access->set_length(buffer, val);
399 if (buffer->access->mark_param_change)
400 buffer->access->mark_param_change(buffer);
403 return len;
405 EXPORT_SYMBOL(iio_buffer_write_length);
407 ssize_t iio_buffer_read_bytes_per_datum(struct device *dev,
408 struct device_attribute *attr,
409 char *buf)
411 struct iio_dev *indio_dev = dev_get_drvdata(dev);
412 struct iio_buffer *buffer = indio_dev->buffer;
414 if (buffer->access->get_bytes_per_datum)
415 return sprintf(buf, "%d\n",
416 buffer->access->get_bytes_per_datum(buffer));
418 return 0;
420 EXPORT_SYMBOL(iio_buffer_read_bytes_per_datum);
422 ssize_t iio_buffer_store_enable(struct device *dev,
423 struct device_attribute *attr,
424 const char *buf,
425 size_t len)
427 int ret;
428 bool requested_state, current_state;
429 int previous_mode;
430 struct iio_dev *dev_info = dev_get_drvdata(dev);
431 struct iio_buffer *buffer = dev_info->buffer;
433 mutex_lock(&dev_info->mlock);
434 previous_mode = dev_info->currentmode;
435 requested_state = !(buf[0] == '0');
436 current_state = !!(previous_mode & INDIO_ALL_BUFFER_MODES);
437 if (current_state == requested_state) {
438 printk(KERN_INFO "iio-buffer, current state requested again\n");
439 goto done;
441 if (requested_state) {
442 if (buffer->setup_ops->preenable) {
443 ret = buffer->setup_ops->preenable(dev_info);
444 if (ret) {
445 printk(KERN_ERR
446 "Buffer not started:"
447 "buffer preenable failed\n");
448 goto error_ret;
451 if (buffer->access->request_update) {
452 ret = buffer->access->request_update(buffer);
453 if (ret) {
454 printk(KERN_INFO
455 "Buffer not started:"
456 "buffer parameter update failed\n");
457 goto error_ret;
460 if (buffer->access->mark_in_use)
461 buffer->access->mark_in_use(buffer);
462 /* Definitely possible for devices to support both of these.*/
463 if (dev_info->modes & INDIO_BUFFER_TRIGGERED) {
464 if (!dev_info->trig) {
465 printk(KERN_INFO
466 "Buffer not started: no trigger\n");
467 ret = -EINVAL;
468 if (buffer->access->unmark_in_use)
469 buffer->access->unmark_in_use(buffer);
470 goto error_ret;
472 dev_info->currentmode = INDIO_BUFFER_TRIGGERED;
473 } else if (dev_info->modes & INDIO_BUFFER_HARDWARE)
474 dev_info->currentmode = INDIO_BUFFER_HARDWARE;
475 else { /* should never be reached */
476 ret = -EINVAL;
477 goto error_ret;
480 if (buffer->setup_ops->postenable) {
481 ret = buffer->setup_ops->postenable(dev_info);
482 if (ret) {
483 printk(KERN_INFO
484 "Buffer not started:"
485 "postenable failed\n");
486 if (buffer->access->unmark_in_use)
487 buffer->access->unmark_in_use(buffer);
488 dev_info->currentmode = previous_mode;
489 if (buffer->setup_ops->postdisable)
490 buffer->setup_ops->
491 postdisable(dev_info);
492 goto error_ret;
495 } else {
496 if (buffer->setup_ops->predisable) {
497 ret = buffer->setup_ops->predisable(dev_info);
498 if (ret)
499 goto error_ret;
501 if (buffer->access->unmark_in_use)
502 buffer->access->unmark_in_use(buffer);
503 dev_info->currentmode = INDIO_DIRECT_MODE;
504 if (buffer->setup_ops->postdisable) {
505 ret = buffer->setup_ops->postdisable(dev_info);
506 if (ret)
507 goto error_ret;
510 done:
511 mutex_unlock(&dev_info->mlock);
512 return len;
514 error_ret:
515 mutex_unlock(&dev_info->mlock);
516 return ret;
518 EXPORT_SYMBOL(iio_buffer_store_enable);
520 ssize_t iio_buffer_show_enable(struct device *dev,
521 struct device_attribute *attr,
522 char *buf)
524 struct iio_dev *dev_info = dev_get_drvdata(dev);
525 return sprintf(buf, "%d\n", !!(dev_info->currentmode
526 & INDIO_ALL_BUFFER_MODES));
528 EXPORT_SYMBOL(iio_buffer_show_enable);
530 int iio_sw_buffer_preenable(struct iio_dev *indio_dev)
532 struct iio_buffer *buffer = indio_dev->buffer;
533 size_t size;
534 dev_dbg(&indio_dev->dev, "%s\n", __func__);
535 /* Check if there are any scan elements enabled, if not fail*/
536 if (!(buffer->scan_count || buffer->scan_timestamp))
537 return -EINVAL;
538 if (buffer->scan_timestamp)
539 if (buffer->scan_count)
540 /* Timestamp (aligned to s64) and data */
541 size = (((buffer->scan_count * buffer->bpe)
542 + sizeof(s64) - 1)
543 & ~(sizeof(s64) - 1))
544 + sizeof(s64);
545 else /* Timestamp only */
546 size = sizeof(s64);
547 else /* Data only */
548 size = buffer->scan_count * buffer->bpe;
549 buffer->access->set_bytes_per_datum(buffer, size);
551 return 0;
553 EXPORT_SYMBOL(iio_sw_buffer_preenable);
556 /* note NULL used as error indicator as it doesn't make sense. */
557 static unsigned long *iio_scan_mask_match(unsigned long *av_masks,
558 unsigned int masklength,
559 unsigned long *mask)
561 if (bitmap_empty(mask, masklength))
562 return NULL;
563 while (*av_masks) {
564 if (bitmap_subset(mask, av_masks, masklength))
565 return av_masks;
566 av_masks += BITS_TO_LONGS(masklength);
568 return NULL;
572 * iio_scan_mask_set() - set particular bit in the scan mask
573 * @buffer: the buffer whose scan mask we are interested in
574 * @bit: the bit to be set.
576 int iio_scan_mask_set(struct iio_buffer *buffer, int bit)
578 struct iio_dev *dev_info = buffer->indio_dev;
579 unsigned long *mask;
580 unsigned long *trialmask;
582 trialmask = kmalloc(sizeof(*trialmask)*
583 BITS_TO_LONGS(dev_info->masklength),
584 GFP_KERNEL);
586 if (trialmask == NULL)
587 return -ENOMEM;
588 if (!dev_info->masklength) {
589 WARN_ON("trying to set scanmask prior to registering buffer\n");
590 kfree(trialmask);
591 return -EINVAL;
593 bitmap_copy(trialmask, buffer->scan_mask, dev_info->masklength);
594 set_bit(bit, trialmask);
596 if (dev_info->available_scan_masks) {
597 mask = iio_scan_mask_match(dev_info->available_scan_masks,
598 dev_info->masklength,
599 trialmask);
600 if (!mask) {
601 kfree(trialmask);
602 return -EINVAL;
605 bitmap_copy(buffer->scan_mask, trialmask, dev_info->masklength);
606 buffer->scan_count++;
608 kfree(trialmask);
610 return 0;
612 EXPORT_SYMBOL_GPL(iio_scan_mask_set);
614 int iio_scan_mask_query(struct iio_buffer *buffer, int bit)
616 struct iio_dev *dev_info = buffer->indio_dev;
617 long *mask;
619 if (bit > dev_info->masklength)
620 return -EINVAL;
622 if (!buffer->scan_mask)
623 return 0;
624 if (dev_info->available_scan_masks)
625 mask = iio_scan_mask_match(dev_info->available_scan_masks,
626 dev_info->masklength,
627 buffer->scan_mask);
628 else
629 mask = buffer->scan_mask;
630 if (!mask)
631 return 0;
633 return test_bit(bit, mask);
635 EXPORT_SYMBOL_GPL(iio_scan_mask_query);