watchdog: pnx4008_wdt: fix broken email address
[cascardo/linux.git] / drivers / 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 #include <linux/iio/kfifo_buf.h>
9 #include <linux/sched.h>
10 #include <linux/poll.h>
11
12 struct iio_kfifo {
13         struct iio_buffer buffer;
14         struct kfifo kf;
15         struct mutex user_lock;
16         int update_needed;
17 };
18
19 #define iio_to_kfifo(r) container_of(r, struct iio_kfifo, buffer)
20
21 static inline int __iio_allocate_kfifo(struct iio_kfifo *buf,
22                                 int bytes_per_datum, int length)
23 {
24         if ((length == 0) || (bytes_per_datum == 0))
25                 return -EINVAL;
26
27         return __kfifo_alloc((struct __kfifo *)&buf->kf, length,
28                              bytes_per_datum, GFP_KERNEL);
29 }
30
31 static int iio_request_update_kfifo(struct iio_buffer *r)
32 {
33         int ret = 0;
34         struct iio_kfifo *buf = iio_to_kfifo(r);
35
36         mutex_lock(&buf->user_lock);
37         if (buf->update_needed) {
38                 kfifo_free(&buf->kf);
39                 ret = __iio_allocate_kfifo(buf, buf->buffer.bytes_per_datum,
40                                    buf->buffer.length);
41                 buf->update_needed = false;
42         } else {
43                 kfifo_reset_out(&buf->kf);
44         }
45         mutex_unlock(&buf->user_lock);
46
47         return ret;
48 }
49
50 static int iio_mark_update_needed_kfifo(struct iio_buffer *r)
51 {
52         struct iio_kfifo *kf = iio_to_kfifo(r);
53         kf->update_needed = true;
54         return 0;
55 }
56
57 static int iio_set_bytes_per_datum_kfifo(struct iio_buffer *r, size_t bpd)
58 {
59         if (r->bytes_per_datum != bpd) {
60                 r->bytes_per_datum = bpd;
61                 iio_mark_update_needed_kfifo(r);
62         }
63         return 0;
64 }
65
66 static int iio_set_length_kfifo(struct iio_buffer *r, int length)
67 {
68         /* Avoid an invalid state */
69         if (length < 2)
70                 length = 2;
71         if (r->length != length) {
72                 r->length = length;
73                 iio_mark_update_needed_kfifo(r);
74         }
75         return 0;
76 }
77
78 static int iio_store_to_kfifo(struct iio_buffer *r,
79                               const void *data)
80 {
81         int ret;
82         struct iio_kfifo *kf = iio_to_kfifo(r);
83         ret = kfifo_in(&kf->kf, data, 1);
84         if (ret != 1)
85                 return -EBUSY;
86         return 0;
87 }
88
89 static int iio_read_first_n_kfifo(struct iio_buffer *r,
90                            size_t n, char __user *buf)
91 {
92         int ret, copied;
93         struct iio_kfifo *kf = iio_to_kfifo(r);
94
95         if (mutex_lock_interruptible(&kf->user_lock))
96                 return -ERESTARTSYS;
97
98         if (!kfifo_initialized(&kf->kf) || n < kfifo_esize(&kf->kf))
99                 ret = -EINVAL;
100         else
101                 ret = kfifo_to_user(&kf->kf, buf, n, &copied);
102         mutex_unlock(&kf->user_lock);
103         if (ret < 0)
104                 return ret;
105
106         return copied;
107 }
108
109 static size_t iio_kfifo_buf_data_available(struct iio_buffer *r)
110 {
111         struct iio_kfifo *kf = iio_to_kfifo(r);
112         size_t samples;
113
114         mutex_lock(&kf->user_lock);
115         samples = kfifo_len(&kf->kf);
116         mutex_unlock(&kf->user_lock);
117
118         return samples;
119 }
120
121 static void iio_kfifo_buffer_release(struct iio_buffer *buffer)
122 {
123         struct iio_kfifo *kf = iio_to_kfifo(buffer);
124
125         mutex_destroy(&kf->user_lock);
126         kfifo_free(&kf->kf);
127         kfree(kf);
128 }
129
130 static const struct iio_buffer_access_funcs kfifo_access_funcs = {
131         .store_to = &iio_store_to_kfifo,
132         .read_first_n = &iio_read_first_n_kfifo,
133         .data_available = iio_kfifo_buf_data_available,
134         .request_update = &iio_request_update_kfifo,
135         .set_bytes_per_datum = &iio_set_bytes_per_datum_kfifo,
136         .set_length = &iio_set_length_kfifo,
137         .release = &iio_kfifo_buffer_release,
138 };
139
140 struct iio_buffer *iio_kfifo_allocate(void)
141 {
142         struct iio_kfifo *kf;
143
144         kf = kzalloc(sizeof(*kf), GFP_KERNEL);
145         if (!kf)
146                 return NULL;
147
148         kf->update_needed = true;
149         iio_buffer_init(&kf->buffer);
150         kf->buffer.access = &kfifo_access_funcs;
151         kf->buffer.length = 2;
152         mutex_init(&kf->user_lock);
153
154         return &kf->buffer;
155 }
156 EXPORT_SYMBOL(iio_kfifo_allocate);
157
158 void iio_kfifo_free(struct iio_buffer *r)
159 {
160         iio_buffer_put(r);
161 }
162 EXPORT_SYMBOL(iio_kfifo_free);
163
164 static void devm_iio_kfifo_release(struct device *dev, void *res)
165 {
166         iio_kfifo_free(*(struct iio_buffer **)res);
167 }
168
169 static int devm_iio_kfifo_match(struct device *dev, void *res, void *data)
170 {
171         struct iio_buffer **r = res;
172
173         if (WARN_ON(!r || !*r))
174                 return 0;
175
176         return *r == data;
177 }
178
179 /**
180  * devm_iio_fifo_allocate - Resource-managed iio_kfifo_allocate()
181  * @dev:                Device to allocate kfifo buffer for
182  *
183  * RETURNS:
184  * Pointer to allocated iio_buffer on success, NULL on failure.
185  */
186 struct iio_buffer *devm_iio_kfifo_allocate(struct device *dev)
187 {
188         struct iio_buffer **ptr, *r;
189
190         ptr = devres_alloc(devm_iio_kfifo_release, sizeof(*ptr), GFP_KERNEL);
191         if (!ptr)
192                 return NULL;
193
194         r = iio_kfifo_allocate();
195         if (r) {
196                 *ptr = r;
197                 devres_add(dev, ptr);
198         } else {
199                 devres_free(ptr);
200         }
201
202         return r;
203 }
204 EXPORT_SYMBOL(devm_iio_kfifo_allocate);
205
206 /**
207  * devm_iio_fifo_free - Resource-managed iio_kfifo_free()
208  * @dev:                Device the buffer belongs to
209  * @r:                  The buffer associated with the device
210  */
211 void devm_iio_kfifo_free(struct device *dev, struct iio_buffer *r)
212 {
213         WARN_ON(devres_release(dev, devm_iio_kfifo_release,
214                                devm_iio_kfifo_match, r));
215 }
216 EXPORT_SYMBOL(devm_iio_kfifo_free);
217
218 MODULE_LICENSE("GPL");