Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/s390/linux
[deliverable/linux.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 while (i < num_channels) {
51 if (bytes % channels[i].bytes == 0)
52 channels[i].location = bytes;
53 else
54 channels[i].location = bytes - bytes%channels[i].bytes
55 + channels[i].bytes;
56 bytes = channels[i].location + channels[i].bytes;
57 i++;
58 }
59 return bytes;
60 }
61
62 void print2byte(int input, struct iio_channel_info *info)
63 {
64 /* First swap if incorrect endian */
65 if (info->be)
66 input = be16toh((uint16_t)input);
67 else
68 input = le16toh((uint16_t)input);
69
70 /*
71 * Shift before conversion to avoid sign extension
72 * of left aligned data
73 */
74 input = input >> info->shift;
75 if (info->is_signed) {
76 int16_t val = input;
77 val &= (1 << info->bits_used) - 1;
78 val = (int16_t)(val << (16 - info->bits_used)) >>
79 (16 - info->bits_used);
80 printf("%05f ", ((float)val + info->offset)*info->scale);
81 } else {
82 uint16_t val = input;
83 val &= (1 << info->bits_used) - 1;
84 printf("%05f ", ((float)val + info->offset)*info->scale);
85 }
86 }
87 /**
88 * process_scan() - print out the values in SI units
89 * @data: pointer to the start of the scan
90 * @channels: information about the channels. Note
91 * size_from_channelarray must have been called first to fill the
92 * location offsets.
93 * @num_channels: number of channels
94 **/
95 void process_scan(char *data,
96 struct iio_channel_info *channels,
97 int num_channels)
98 {
99 int k;
100 for (k = 0; k < num_channels; k++)
101 switch (channels[k].bytes) {
102 /* only a few cases implemented so far */
103 case 2:
104 print2byte(*(uint16_t *)(data + channels[k].location),
105 &channels[k]);
106 break;
107 case 8:
108 if (channels[k].is_signed) {
109 int64_t val = *(int64_t *)
110 (data +
111 channels[k].location);
112 if ((val >> channels[k].bits_used) & 1)
113 val = (val & channels[k].mask) |
114 ~channels[k].mask;
115 /* special case for timestamp */
116 if (channels[k].scale == 1.0f &&
117 channels[k].offset == 0.0f)
118 printf("%" PRId64 " ", val);
119 else
120 printf("%05f ", ((float)val +
121 channels[k].offset)*
122 channels[k].scale);
123 }
124 break;
125 default:
126 break;
127 }
128 printf("\n");
129 }
130
131 int main(int argc, char **argv)
132 {
133 unsigned long num_loops = 2;
134 unsigned long timedelay = 1000000;
135 unsigned long buf_len = 128;
136
137 int ret, c, i, j, toread;
138 int fp;
139
140 int num_channels;
141 char *trigger_name = NULL, *device_name = NULL;
142 char *dev_dir_name, *buf_dir_name;
143
144 int datardytrigger = 1;
145 char *data;
146 ssize_t read_size;
147 int dev_num, trig_num;
148 char *buffer_access;
149 int scan_size;
150 int noevents = 0;
151 char *dummy;
152
153 struct iio_channel_info *channels;
154
155 while ((c = getopt(argc, argv, "l:w:c:et:n:")) != -1) {
156 switch (c) {
157 case 'n':
158 device_name = optarg;
159 break;
160 case 't':
161 trigger_name = optarg;
162 datardytrigger = 0;
163 break;
164 case 'e':
165 noevents = 1;
166 break;
167 case 'c':
168 num_loops = strtoul(optarg, &dummy, 10);
169 break;
170 case 'w':
171 timedelay = strtoul(optarg, &dummy, 10);
172 break;
173 case 'l':
174 buf_len = strtoul(optarg, &dummy, 10);
175 break;
176 case '?':
177 return -1;
178 }
179 }
180
181 if (device_name == NULL)
182 return -1;
183
184 /* Find the device requested */
185 dev_num = find_type_by_name(device_name, "iio:device");
186 if (dev_num < 0) {
187 printf("Failed to find the %s\n", device_name);
188 ret = -ENODEV;
189 goto error_ret;
190 }
191 printf("iio device number being used is %d\n", dev_num);
192
193 asprintf(&dev_dir_name, "%siio:device%d", iio_dir, dev_num);
194 if (trigger_name == NULL) {
195 /*
196 * Build the trigger name. If it is device associated its
197 * name is <device_name>_dev[n] where n matches the device
198 * number found above
199 */
200 ret = asprintf(&trigger_name,
201 "%s-dev%d", device_name, dev_num);
202 if (ret < 0) {
203 ret = -ENOMEM;
204 goto error_ret;
205 }
206 }
207
208 /* Verify the trigger exists */
209 trig_num = find_type_by_name(trigger_name, "trigger");
210 if (trig_num < 0) {
211 printf("Failed to find the trigger %s\n", trigger_name);
212 ret = -ENODEV;
213 goto error_free_triggername;
214 }
215 printf("iio trigger number being used is %d\n", trig_num);
216
217 /*
218 * Parse the files in scan_elements to identify what channels are
219 * present
220 */
221 ret = build_channel_array(dev_dir_name, &channels, &num_channels);
222 if (ret) {
223 printf("Problem reading scan element information\n");
224 printf("diag %s\n", dev_dir_name);
225 goto error_free_triggername;
226 }
227
228 /*
229 * Construct the directory name for the associated buffer.
230 * As we know that the lis3l02dq has only one buffer this may
231 * be built rather than found.
232 */
233 ret = asprintf(&buf_dir_name,
234 "%siio:device%d/buffer", iio_dir, dev_num);
235 if (ret < 0) {
236 ret = -ENOMEM;
237 goto error_free_triggername;
238 }
239 printf("%s %s\n", dev_dir_name, trigger_name);
240 /* Set the device trigger to be the data ready trigger found above */
241 ret = write_sysfs_string_and_verify("trigger/current_trigger",
242 dev_dir_name,
243 trigger_name);
244 if (ret < 0) {
245 printf("Failed to write current_trigger file\n");
246 goto error_free_buf_dir_name;
247 }
248
249 /* Setup ring buffer parameters */
250 ret = write_sysfs_int("length", buf_dir_name, buf_len);
251 if (ret < 0)
252 goto error_free_buf_dir_name;
253
254 /* Enable the buffer */
255 ret = write_sysfs_int("enable", buf_dir_name, 1);
256 if (ret < 0)
257 goto error_free_buf_dir_name;
258 scan_size = size_from_channelarray(channels, num_channels);
259 data = malloc(scan_size*buf_len);
260 if (!data) {
261 ret = -ENOMEM;
262 goto error_free_buf_dir_name;
263 }
264
265 ret = asprintf(&buffer_access, "/dev/iio:device%d", dev_num);
266 if (ret < 0) {
267 ret = -ENOMEM;
268 goto error_free_data;
269 }
270
271 /* Attempt to open non blocking the access dev */
272 fp = open(buffer_access, O_RDONLY | O_NONBLOCK);
273 if (fp == -1) { /* If it isn't there make the node */
274 printf("Failed to open %s\n", buffer_access);
275 ret = -errno;
276 goto error_free_buffer_access;
277 }
278
279 /* Wait for events 10 times */
280 for (j = 0; j < num_loops; j++) {
281 if (!noevents) {
282 struct pollfd pfd = {
283 .fd = fp,
284 .events = POLLIN,
285 };
286
287 poll(&pfd, 1, -1);
288 toread = buf_len;
289
290 } else {
291 usleep(timedelay);
292 toread = 64;
293 }
294
295 read_size = read(fp,
296 data,
297 toread*scan_size);
298 if (read_size == -EAGAIN) {
299 printf("nothing available\n");
300 continue;
301 }
302 for (i = 0; i < read_size/scan_size; i++)
303 process_scan(data + scan_size*i,
304 channels,
305 num_channels);
306 }
307
308 /* Stop the buffer */
309 ret = write_sysfs_int("enable", buf_dir_name, 0);
310 if (ret < 0)
311 goto error_close_buffer_access;
312
313 /* Disconnect the trigger - just write a dummy name. */
314 write_sysfs_string("trigger/current_trigger",
315 dev_dir_name, "NULL");
316
317 error_close_buffer_access:
318 close(fp);
319 error_free_data:
320 free(data);
321 error_free_buffer_access:
322 free(buffer_access);
323 error_free_buf_dir_name:
324 free(buf_dir_name);
325 error_free_triggername:
326 if (datardytrigger)
327 free(trigger_name);
328 error_ret:
329 return ret;
330 }
This page took 0.040171 seconds and 6 git commands to generate.