Merge branch 'drm-intel-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/keith...
[pandora-kernel.git] / drivers / staging / iio / kfifo_buf.c
1 #include <linux/slab.h>
2 #include <linux/kernel.h>
3 #include <linux/module.h>
4 #include <linux/device.h>
5 #include <linux/workqueue.h>
6 #include <linux/kfifo.h>
7 #include <linux/mutex.h>
8
9 #include "kfifo_buf.h"
10
11 static inline int __iio_allocate_kfifo(struct iio_kfifo *buf,
12                                 int bytes_per_datum, int length)
13 {
14         if ((length == 0) || (bytes_per_datum == 0))
15                 return -EINVAL;
16
17         __iio_update_ring_buffer(&buf->ring, bytes_per_datum, length);
18         return kfifo_alloc(&buf->kf, bytes_per_datum*length, GFP_KERNEL);
19 }
20
21 int iio_request_update_kfifo(struct iio_ring_buffer *r)
22 {
23         int ret = 0;
24         struct iio_kfifo *buf = iio_to_kfifo(r);
25
26         mutex_lock(&buf->use_lock);
27         if (!buf->update_needed)
28                 goto error_ret;
29         if (buf->use_count) {
30                 ret = -EAGAIN;
31                 goto error_ret;
32         }
33         kfifo_free(&buf->kf);
34         ret = __iio_allocate_kfifo(buf, buf->ring.bytes_per_datum,
35                                 buf->ring.length);
36 error_ret:
37         mutex_unlock(&buf->use_lock);
38         return ret;
39 }
40 EXPORT_SYMBOL(iio_request_update_kfifo);
41
42 void iio_mark_kfifo_in_use(struct iio_ring_buffer *r)
43 {
44         struct iio_kfifo *buf = iio_to_kfifo(r);
45         mutex_lock(&buf->use_lock);
46         buf->use_count++;
47         mutex_unlock(&buf->use_lock);
48 }
49 EXPORT_SYMBOL(iio_mark_kfifo_in_use);
50
51 void iio_unmark_kfifo_in_use(struct iio_ring_buffer *r)
52 {
53         struct iio_kfifo *buf = iio_to_kfifo(r);
54         mutex_lock(&buf->use_lock);
55         buf->use_count--;
56         mutex_unlock(&buf->use_lock);
57 }
58 EXPORT_SYMBOL(iio_unmark_kfifo_in_use);
59
60 int iio_get_length_kfifo(struct iio_ring_buffer *r)
61 {
62         return r->length;
63 }
64 EXPORT_SYMBOL(iio_get_length_kfifo);
65
66 static inline void __iio_init_kfifo(struct iio_kfifo *kf)
67 {
68         mutex_init(&kf->use_lock);
69 }
70
71 static IIO_RING_ENABLE_ATTR;
72 static IIO_RING_BYTES_PER_DATUM_ATTR;
73 static IIO_RING_LENGTH_ATTR;
74
75 static struct attribute *iio_kfifo_attributes[] = {
76         &dev_attr_length.attr,
77         &dev_attr_bytes_per_datum.attr,
78         &dev_attr_enable.attr,
79         NULL,
80 };
81
82 static struct attribute_group iio_kfifo_attribute_group = {
83         .attrs = iio_kfifo_attributes,
84 };
85
86 static const struct attribute_group *iio_kfifo_attribute_groups[] = {
87         &iio_kfifo_attribute_group,
88         NULL
89 };
90
91 static void iio_kfifo_release(struct device *dev)
92 {
93         struct iio_ring_buffer *r = to_iio_ring_buffer(dev);
94         struct iio_kfifo *kf = iio_to_kfifo(r);
95         kfifo_free(&kf->kf);
96         kfree(kf);
97 }
98
99 static struct device_type iio_kfifo_type = {
100         .release = iio_kfifo_release,
101         .groups = iio_kfifo_attribute_groups,
102 };
103
104 struct iio_ring_buffer *iio_kfifo_allocate(struct iio_dev *indio_dev)
105 {
106         struct iio_kfifo *kf;
107
108         kf = kzalloc(sizeof *kf, GFP_KERNEL);
109         if (!kf)
110                 return NULL;
111         iio_ring_buffer_init(&kf->ring, indio_dev);
112         __iio_init_kfifo(kf);
113         kf->ring.dev.type = &iio_kfifo_type;
114         device_initialize(&kf->ring.dev);
115         kf->ring.dev.parent = &indio_dev->dev;
116         kf->ring.dev.bus = &iio_bus_type;
117         dev_set_drvdata(&kf->ring.dev, (void *)&(kf->ring));
118
119         return &kf->ring;
120 }
121 EXPORT_SYMBOL(iio_kfifo_allocate);
122
123 int iio_get_bytes_per_datum_kfifo(struct iio_ring_buffer *r)
124 {
125         return r->bytes_per_datum;
126 }
127 EXPORT_SYMBOL(iio_get_bytes_per_datum_kfifo);
128
129 int iio_set_bytes_per_datum_kfifo(struct iio_ring_buffer *r, size_t bpd)
130 {
131         if (r->bytes_per_datum != bpd) {
132                 r->bytes_per_datum = bpd;
133                 if (r->access.mark_param_change)
134                         r->access.mark_param_change(r);
135         }
136         return 0;
137 }
138 EXPORT_SYMBOL(iio_set_bytes_per_datum_kfifo);
139
140 int iio_mark_update_needed_kfifo(struct iio_ring_buffer *r)
141 {
142         struct iio_kfifo *kf = iio_to_kfifo(r);
143         kf->update_needed = true;
144         return 0;
145 }
146 EXPORT_SYMBOL(iio_mark_update_needed_kfifo);
147
148 int iio_set_length_kfifo(struct iio_ring_buffer *r, int length)
149 {
150         if (r->length != length) {
151                 r->length = length;
152                 if (r->access.mark_param_change)
153                         r->access.mark_param_change(r);
154         }
155         return 0;
156 }
157 EXPORT_SYMBOL(iio_set_length_kfifo);
158
159 void iio_kfifo_free(struct iio_ring_buffer *r)
160 {
161         if (r)
162                 iio_put_ring_buffer(r);
163 }
164 EXPORT_SYMBOL(iio_kfifo_free);
165
166 int iio_store_to_kfifo(struct iio_ring_buffer *r, u8 *data, s64 timestamp)
167 {
168         int ret;
169         struct iio_kfifo *kf = iio_to_kfifo(r);
170         u8 *datal = kmalloc(r->bytes_per_datum, GFP_KERNEL);
171         memcpy(datal, data, r->bytes_per_datum - sizeof(timestamp));
172         memcpy(datal + r->bytes_per_datum - sizeof(timestamp),
173                 &timestamp, sizeof(timestamp));
174         ret = kfifo_in(&kf->kf, data, r->bytes_per_datum);
175         if (ret != r->bytes_per_datum) {
176                 kfree(datal);
177                 return -EBUSY;
178         }
179         kfree(datal);
180         return 0;
181 }
182 EXPORT_SYMBOL(iio_store_to_kfifo);
183
184 int iio_rip_kfifo(struct iio_ring_buffer *r,
185                 size_t count, char __user *buf, int *deadoffset)
186 {
187         int ret, copied;
188         struct iio_kfifo *kf = iio_to_kfifo(r);
189
190         *deadoffset = 0;
191         ret = kfifo_to_user(&kf->kf, buf, r->bytes_per_datum*count, &copied);
192
193         return copied;
194 }
195 EXPORT_SYMBOL(iio_rip_kfifo);
196 MODULE_LICENSE("GPL");