]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blob - drivers/staging/iio/Documentation/generic_buffer.c
Merge tag 'iio-for-3.19a' of git://git.kernel.org/pub/scm/linux/kernel/git/jic23...
[mirror_ubuntu-zesty-kernel.git] / drivers / staging / iio / Documentation / generic_buffer.c
1 /* Industrialio buffer test code.
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 * This program is primarily intended as an example application.
10 * Reads the current buffer setup from sysfs and starts a short capture
11 * from the specified device, pretty printing the result after appropriate
12 * conversion.
13 *
14 * Command line parameters
15 * generic_buffer -n <device_name> -t <trigger_name>
16 * If trigger name is not specified the program assumes you want a dataready
17 * trigger associated with the device and goes looking for it.
18 *
19 */
20
21 #define _GNU_SOURCE
22
23 #include <unistd.h>
24 #include <dirent.h>
25 #include <fcntl.h>
26 #include <stdio.h>
27 #include <errno.h>
28 #include <sys/stat.h>
29 #include <sys/dir.h>
30 #include <linux/types.h>
31 #include <string.h>
32 #include <poll.h>
33 #include <endian.h>
34 #include <getopt.h>
35 #include <inttypes.h>
36 #include "iio_utils.h"
37
38 /**
39 * size_from_channelarray() - calculate the storage size of a scan
40 * @channels: the channel info array
41 * @num_channels: number of channels
42 *
43 * Has the side effect of filling the channels[i].location values used
44 * in processing the buffer output.
45 **/
46 int size_from_channelarray(struct iio_channel_info *channels, int num_channels)
47 {
48 int bytes = 0;
49 int i = 0;
50
51 while (i < num_channels) {
52 if (bytes % channels[i].bytes == 0)
53 channels[i].location = bytes;
54 else
55 channels[i].location = bytes - bytes%channels[i].bytes
56 + channels[i].bytes;
57 bytes = channels[i].location + channels[i].bytes;
58 i++;
59 }
60 return bytes;
61 }
62
63 void print2byte(int input, struct iio_channel_info *info)
64 {
65 /* First swap if incorrect endian */
66 if (info->be)
67 input = be16toh((uint16_t)input);
68 else
69 input = le16toh((uint16_t)input);
70
71 /*
72 * Shift before conversion to avoid sign extension
73 * of left aligned data
74 */
75 input = input >> info->shift;
76 if (info->is_signed) {
77 int16_t val = input;
78
79 val &= (1 << info->bits_used) - 1;
80 val = (int16_t)(val << (16 - info->bits_used)) >>
81 (16 - info->bits_used);
82 printf("%05f ", ((float)val + info->offset)*info->scale);
83 } else {
84 uint16_t val = input;
85
86 val &= (1 << info->bits_used) - 1;
87 printf("%05f ", ((float)val + info->offset)*info->scale);
88 }
89 }
90 /**
91 * process_scan() - print out the values in SI units
92 * @data: pointer to the start of the scan
93 * @channels: information about the channels. Note
94 * size_from_channelarray must have been called first to fill the
95 * location offsets.
96 * @num_channels: number of channels
97 **/
98 void process_scan(char *data,
99 struct iio_channel_info *channels,
100 int num_channels)
101 {
102 int k;
103
104 for (k = 0; k < num_channels; k++)
105 switch (channels[k].bytes) {
106 /* only a few cases implemented so far */
107 case 2:
108 print2byte(*(uint16_t *)(data + channels[k].location),
109 &channels[k]);
110 break;
111 case 4:
112 if (!channels[k].is_signed) {
113 uint32_t val = *(uint32_t *)
114 (data + channels[k].location);
115 printf("%05f ", ((float)val +
116 channels[k].offset)*
117 channels[k].scale);
118
119 }
120 break;
121 case 8:
122 if (channels[k].is_signed) {
123 int64_t val = *(int64_t *)
124 (data +
125 channels[k].location);
126 if ((val >> channels[k].bits_used) & 1)
127 val = (val & channels[k].mask) |
128 ~channels[k].mask;
129 /* special case for timestamp */
130 if (channels[k].scale == 1.0f &&
131 channels[k].offset == 0.0f)
132 printf("%" PRId64 " ", val);
133 else
134 printf("%05f ", ((float)val +
135 channels[k].offset)*
136 channels[k].scale);
137 }
138 break;
139 default:
140 break;
141 }
142 printf("\n");
143 }
144
145 int main(int argc, char **argv)
146 {
147 unsigned long num_loops = 2;
148 unsigned long timedelay = 1000000;
149 unsigned long buf_len = 128;
150
151 int ret, c, i, j, toread;
152 int fp;
153
154 int num_channels;
155 char *trigger_name = NULL, *device_name = NULL;
156 char *dev_dir_name, *buf_dir_name;
157
158 int datardytrigger = 1;
159 char *data;
160 ssize_t read_size;
161 int dev_num, trig_num;
162 char *buffer_access;
163 int scan_size;
164 int noevents = 0;
165 int notrigger = 0;
166 char *dummy;
167
168 struct iio_channel_info *channels;
169
170 while ((c = getopt(argc, argv, "l:w:c:et:n:g")) != -1) {
171 switch (c) {
172 case 'n':
173 device_name = optarg;
174 break;
175 case 't':
176 trigger_name = optarg;
177 datardytrigger = 0;
178 break;
179 case 'e':
180 noevents = 1;
181 break;
182 case 'c':
183 num_loops = strtoul(optarg, &dummy, 10);
184 break;
185 case 'w':
186 timedelay = strtoul(optarg, &dummy, 10);
187 break;
188 case 'l':
189 buf_len = strtoul(optarg, &dummy, 10);
190 break;
191 case 'g':
192 notrigger = 1;
193 break;
194 case '?':
195 return -1;
196 }
197 }
198
199 if (device_name == NULL)
200 return -1;
201
202 /* Find the device requested */
203 dev_num = find_type_by_name(device_name, "iio:device");
204 if (dev_num < 0) {
205 printf("Failed to find the %s\n", device_name);
206 ret = -ENODEV;
207 goto error_ret;
208 }
209 printf("iio device number being used is %d\n", dev_num);
210
211 asprintf(&dev_dir_name, "%siio:device%d", iio_dir, dev_num);
212
213 if (!notrigger) {
214 if (trigger_name == NULL) {
215 /*
216 * Build the trigger name. If it is device associated
217 * its name is <device_name>_dev[n] where n matches
218 * the device number found above.
219 */
220 ret = asprintf(&trigger_name,
221 "%s-dev%d", device_name, dev_num);
222 if (ret < 0) {
223 ret = -ENOMEM;
224 goto error_ret;
225 }
226 }
227
228 /* Verify the trigger exists */
229 trig_num = find_type_by_name(trigger_name, "trigger");
230 if (trig_num < 0) {
231 printf("Failed to find the trigger %s\n", trigger_name);
232 ret = -ENODEV;
233 goto error_free_triggername;
234 }
235 printf("iio trigger number being used is %d\n", trig_num);
236 } else
237 printf("trigger-less mode selected\n");
238
239 /*
240 * Parse the files in scan_elements to identify what channels are
241 * present
242 */
243 ret = build_channel_array(dev_dir_name, &channels, &num_channels);
244 if (ret) {
245 printf("Problem reading scan element information\n");
246 printf("diag %s\n", dev_dir_name);
247 goto error_free_triggername;
248 }
249
250 /*
251 * Construct the directory name for the associated buffer.
252 * As we know that the lis3l02dq has only one buffer this may
253 * be built rather than found.
254 */
255 ret = asprintf(&buf_dir_name,
256 "%siio:device%d/buffer", iio_dir, dev_num);
257 if (ret < 0) {
258 ret = -ENOMEM;
259 goto error_free_triggername;
260 }
261
262 if (!notrigger) {
263 printf("%s %s\n", dev_dir_name, trigger_name);
264 /* Set the device trigger to be the data ready trigger found
265 * above */
266 ret = write_sysfs_string_and_verify("trigger/current_trigger",
267 dev_dir_name,
268 trigger_name);
269 if (ret < 0) {
270 printf("Failed to write current_trigger file\n");
271 goto error_free_buf_dir_name;
272 }
273 }
274
275 /* Setup ring buffer parameters */
276 ret = write_sysfs_int("length", buf_dir_name, buf_len);
277 if (ret < 0)
278 goto error_free_buf_dir_name;
279
280 /* Enable the buffer */
281 ret = write_sysfs_int("enable", buf_dir_name, 1);
282 if (ret < 0)
283 goto error_free_buf_dir_name;
284 scan_size = size_from_channelarray(channels, num_channels);
285 data = malloc(scan_size*buf_len);
286 if (!data) {
287 ret = -ENOMEM;
288 goto error_free_buf_dir_name;
289 }
290
291 ret = asprintf(&buffer_access, "/dev/iio:device%d", dev_num);
292 if (ret < 0) {
293 ret = -ENOMEM;
294 goto error_free_data;
295 }
296
297 /* Attempt to open non blocking the access dev */
298 fp = open(buffer_access, O_RDONLY | O_NONBLOCK);
299 if (fp == -1) { /* If it isn't there make the node */
300 printf("Failed to open %s\n", buffer_access);
301 ret = -errno;
302 goto error_free_buffer_access;
303 }
304
305 /* Wait for events 10 times */
306 for (j = 0; j < num_loops; j++) {
307 if (!noevents) {
308 struct pollfd pfd = {
309 .fd = fp,
310 .events = POLLIN,
311 };
312
313 poll(&pfd, 1, -1);
314 toread = buf_len;
315
316 } else {
317 usleep(timedelay);
318 toread = 64;
319 }
320
321 read_size = read(fp,
322 data,
323 toread*scan_size);
324 if (read_size < 0) {
325 if (errno == -EAGAIN) {
326 printf("nothing available\n");
327 continue;
328 } else
329 break;
330 }
331 for (i = 0; i < read_size/scan_size; i++)
332 process_scan(data + scan_size*i,
333 channels,
334 num_channels);
335 }
336
337 /* Stop the buffer */
338 ret = write_sysfs_int("enable", buf_dir_name, 0);
339 if (ret < 0)
340 goto error_close_buffer_access;
341
342 if (!notrigger)
343 /* Disconnect the trigger - just write a dummy name. */
344 write_sysfs_string("trigger/current_trigger",
345 dev_dir_name, "NULL");
346
347 error_close_buffer_access:
348 close(fp);
349 error_free_data:
350 free(data);
351 error_free_buffer_access:
352 free(buffer_access);
353 error_free_buf_dir_name:
354 free(buf_dir_name);
355 error_free_triggername:
356 if (datardytrigger)
357 free(trigger_name);
358 error_ret:
359 return ret;
360 }