]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - drivers/iio/kfifo_buf.c
Merge tag 'scsi-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jejb...
[mirror_ubuntu-bionic-kernel.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
11 struct iio_kfifo {
12 struct iio_buffer buffer;
13 struct kfifo kf;
14 int update_needed;
15 };
16
17 #define iio_to_kfifo(r) container_of(r, struct iio_kfifo, buffer)
18
19 static inline int __iio_allocate_kfifo(struct iio_kfifo *buf,
20 int bytes_per_datum, int length)
21 {
22 if ((length == 0) || (bytes_per_datum == 0))
23 return -EINVAL;
24
25 return __kfifo_alloc((struct __kfifo *)&buf->kf, length,
26 bytes_per_datum, GFP_KERNEL);
27 }
28
29 static int iio_request_update_kfifo(struct iio_buffer *r)
30 {
31 int ret = 0;
32 struct iio_kfifo *buf = iio_to_kfifo(r);
33
34 if (!buf->update_needed)
35 goto error_ret;
36 kfifo_free(&buf->kf);
37 ret = __iio_allocate_kfifo(buf, buf->buffer.bytes_per_datum,
38 buf->buffer.length);
39 r->stufftoread = false;
40 error_ret:
41 return ret;
42 }
43
44 static int iio_get_length_kfifo(struct iio_buffer *r)
45 {
46 return r->length;
47 }
48
49 static IIO_BUFFER_ENABLE_ATTR;
50 static IIO_BUFFER_LENGTH_ATTR;
51
52 static struct attribute *iio_kfifo_attributes[] = {
53 &dev_attr_length.attr,
54 &dev_attr_enable.attr,
55 NULL,
56 };
57
58 static struct attribute_group iio_kfifo_attribute_group = {
59 .attrs = iio_kfifo_attributes,
60 .name = "buffer",
61 };
62
63 static int iio_get_bytes_per_datum_kfifo(struct iio_buffer *r)
64 {
65 return r->bytes_per_datum;
66 }
67
68 static int iio_mark_update_needed_kfifo(struct iio_buffer *r)
69 {
70 struct iio_kfifo *kf = iio_to_kfifo(r);
71 kf->update_needed = true;
72 return 0;
73 }
74
75 static int iio_set_bytes_per_datum_kfifo(struct iio_buffer *r, size_t bpd)
76 {
77 if (r->bytes_per_datum != bpd) {
78 r->bytes_per_datum = bpd;
79 iio_mark_update_needed_kfifo(r);
80 }
81 return 0;
82 }
83
84 static int iio_set_length_kfifo(struct iio_buffer *r, int length)
85 {
86 /* Avoid an invalid state */
87 if (length < 2)
88 length = 2;
89 if (r->length != length) {
90 r->length = length;
91 iio_mark_update_needed_kfifo(r);
92 }
93 return 0;
94 }
95
96 static int iio_store_to_kfifo(struct iio_buffer *r,
97 u8 *data)
98 {
99 int ret;
100 struct iio_kfifo *kf = iio_to_kfifo(r);
101 ret = kfifo_in(&kf->kf, data, 1);
102 if (ret != 1)
103 return -EBUSY;
104 r->stufftoread = true;
105 wake_up_interruptible(&r->pollq);
106
107 return 0;
108 }
109
110 static int iio_read_first_n_kfifo(struct iio_buffer *r,
111 size_t n, char __user *buf)
112 {
113 int ret, copied;
114 struct iio_kfifo *kf = iio_to_kfifo(r);
115
116 if (n < r->bytes_per_datum || r->bytes_per_datum == 0)
117 return -EINVAL;
118
119 ret = kfifo_to_user(&kf->kf, buf, n, &copied);
120 if (ret < 0)
121 return ret;
122
123 if (kfifo_is_empty(&kf->kf))
124 r->stufftoread = false;
125 /* verify it is still empty to avoid race */
126 if (!kfifo_is_empty(&kf->kf))
127 r->stufftoread = true;
128
129 return copied;
130 }
131
132 static const struct iio_buffer_access_funcs kfifo_access_funcs = {
133 .store_to = &iio_store_to_kfifo,
134 .read_first_n = &iio_read_first_n_kfifo,
135 .request_update = &iio_request_update_kfifo,
136 .get_bytes_per_datum = &iio_get_bytes_per_datum_kfifo,
137 .set_bytes_per_datum = &iio_set_bytes_per_datum_kfifo,
138 .get_length = &iio_get_length_kfifo,
139 .set_length = &iio_set_length_kfifo,
140 };
141
142 struct iio_buffer *iio_kfifo_allocate(struct iio_dev *indio_dev)
143 {
144 struct iio_kfifo *kf;
145
146 kf = kzalloc(sizeof *kf, GFP_KERNEL);
147 if (!kf)
148 return NULL;
149 kf->update_needed = true;
150 iio_buffer_init(&kf->buffer);
151 kf->buffer.attrs = &iio_kfifo_attribute_group;
152 kf->buffer.access = &kfifo_access_funcs;
153 kf->buffer.length = 2;
154 return &kf->buffer;
155 }
156 EXPORT_SYMBOL(iio_kfifo_allocate);
157
158 void iio_kfifo_free(struct iio_buffer *r)
159 {
160 kfree(iio_to_kfifo(r));
161 }
162 EXPORT_SYMBOL(iio_kfifo_free);
163
164 MODULE_LICENSE("GPL");