lkdtm: split usercopy tests to separate file
[deliverable/linux.git] / tools / iio / generic_buffer.c
CommitLineData
e58537cc
JC
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#include <unistd.h>
bdcb31d0 22#include <stdlib.h>
e58537cc
JC
23#include <dirent.h>
24#include <fcntl.h>
25#include <stdio.h>
26#include <errno.h>
27#include <sys/stat.h>
28#include <sys/dir.h>
29#include <linux/types.h>
30268a3d 30#include <string.h>
52615d47 31#include <poll.h>
117cf8b7 32#include <endian.h>
bb23378c 33#include <getopt.h>
1bcdfbcf 34#include <inttypes.h>
e58537cc
JC
35#include "iio_utils.h"
36
5f991a92
LW
37/**
38 * enum autochan - state for the automatic channel enabling mechanism
39 */
40enum autochan {
41 AUTOCHANNELS_DISABLED,
42 AUTOCHANNELS_ENABLED,
43 AUTOCHANNELS_ACTIVE,
44};
45
e58537cc
JC
46/**
47 * size_from_channelarray() - calculate the storage size of a scan
d8da0eee
PM
48 * @channels: the channel info array
49 * @num_channels: number of channels
e58537cc
JC
50 *
51 * Has the side effect of filling the channels[i].location values used
52 * in processing the buffer output.
53 **/
54int size_from_channelarray(struct iio_channel_info *channels, int num_channels)
55{
56 int bytes = 0;
57 int i = 0;
ff39a252 58
e58537cc
JC
59 while (i < num_channels) {
60 if (bytes % channels[i].bytes == 0)
61 channels[i].location = bytes;
62 else
7663a4aa
HK
63 channels[i].location = bytes - bytes % channels[i].bytes
64 + channels[i].bytes;
65
e58537cc
JC
66 bytes = channels[i].location + channels[i].bytes;
67 i++;
68 }
7663a4aa 69
e58537cc
JC
70 return bytes;
71}
72
e8d0927a
TB
73void print1byte(uint8_t input, struct iio_channel_info *info)
74{
75 /*
76 * Shift before conversion to avoid sign extension
77 * of left aligned data
78 */
79 input >>= info->shift;
80 input &= info->mask;
81 if (info->is_signed) {
82 int8_t val = (int8_t)(input << (8 - info->bits_used)) >>
83 (8 - info->bits_used);
84 printf("%05f ", ((float)val + info->offset) * info->scale);
85 } else {
86 printf("%05f ", ((float)input + info->offset) * info->scale);
87 }
88}
89
8e926134 90void print2byte(uint16_t input, struct iio_channel_info *info)
52615d47 91{
117cf8b7 92 /* First swap if incorrect endian */
117cf8b7 93 if (info->be)
8e926134 94 input = be16toh(input);
117cf8b7 95 else
8e926134 96 input = le16toh(input);
117cf8b7 97
d8da0eee
PM
98 /*
99 * Shift before conversion to avoid sign extension
100 * of left aligned data
101 */
1525ecfc 102 input >>= info->shift;
8e926134 103 input &= info->mask;
52615d47 104 if (info->is_signed) {
8e926134
HK
105 int16_t val = (int16_t)(input << (16 - info->bits_used)) >>
106 (16 - info->bits_used);
107 printf("%05f ", ((float)val + info->offset) * info->scale);
108 } else {
109 printf("%05f ", ((float)input + info->offset) * info->scale);
110 }
111}
ff39a252 112
8e926134
HK
113void print4byte(uint32_t input, struct iio_channel_info *info)
114{
115 /* First swap if incorrect endian */
116 if (info->be)
117 input = be32toh(input);
118 else
119 input = le32toh(input);
120
121 /*
122 * Shift before conversion to avoid sign extension
123 * of left aligned data
124 */
125 input >>= info->shift;
126 input &= info->mask;
127 if (info->is_signed) {
128 int32_t val = (int32_t)(input << (32 - info->bits_used)) >>
129 (32 - info->bits_used);
130 printf("%05f ", ((float)val + info->offset) * info->scale);
52615d47 131 } else {
8e926134
HK
132 printf("%05f ", ((float)input + info->offset) * info->scale);
133 }
134}
ff39a252 135
8e926134
HK
136void print8byte(uint64_t input, struct iio_channel_info *info)
137{
138 /* First swap if incorrect endian */
139 if (info->be)
140 input = be64toh(input);
141 else
142 input = le64toh(input);
143
144 /*
145 * Shift before conversion to avoid sign extension
146 * of left aligned data
147 */
148 input >>= info->shift;
149 input &= info->mask;
150 if (info->is_signed) {
151 int64_t val = (int64_t)(input << (64 - info->bits_used)) >>
152 (64 - info->bits_used);
153 /* special case for timestamp */
154 if (info->scale == 1.0f && info->offset == 0.0f)
155 printf("%" PRId64 " ", val);
156 else
157 printf("%05f ",
158 ((float)val + info->offset) * info->scale);
159 } else {
160 printf("%05f ", ((float)input + info->offset) * info->scale);
52615d47
JC
161 }
162}
8e926134 163
e58537cc
JC
164/**
165 * process_scan() - print out the values in SI units
166 * @data: pointer to the start of the scan
7663a4aa
HK
167 * @channels: information about the channels.
168 * Note: size_from_channelarray must have been called first
169 * to fill the location offsets.
d8da0eee 170 * @num_channels: number of channels
e58537cc
JC
171 **/
172void process_scan(char *data,
d8da0eee 173 struct iio_channel_info *channels,
e58537cc
JC
174 int num_channels)
175{
176 int k;
ff39a252 177
e58537cc 178 for (k = 0; k < num_channels; k++)
d8da0eee 179 switch (channels[k].bytes) {
e58537cc 180 /* only a few cases implemented so far */
e8d0927a
TB
181 case 1:
182 print1byte(*(uint8_t *)(data + channels[k].location),
183 &channels[k]);
184 break;
e58537cc 185 case 2:
d8da0eee
PM
186 print2byte(*(uint16_t *)(data + channels[k].location),
187 &channels[k]);
e58537cc 188 break;
6cffc1f8 189 case 4:
8e926134
HK
190 print4byte(*(uint32_t *)(data + channels[k].location),
191 &channels[k]);
6cffc1f8 192 break;
e58537cc 193 case 8:
8e926134
HK
194 print8byte(*(uint64_t *)(data + channels[k].location),
195 &channels[k]);
e58537cc
JC
196 break;
197 default:
198 break;
199 }
200 printf("\n");
201}
202
5f991a92
LW
203static int enable_disable_all_channels(char *dev_dir_name, int enable)
204{
205 const struct dirent *ent;
206 char scanelemdir[256];
207 DIR *dp;
208 int ret;
209
210 snprintf(scanelemdir, sizeof(scanelemdir),
211 FORMAT_SCAN_ELEMENTS_DIR, dev_dir_name);
212 scanelemdir[sizeof(scanelemdir)-1] = '\0';
213
214 dp = opendir(scanelemdir);
215 if (!dp) {
216 fprintf(stderr, "Enabling/disabling channels: can't open %s\n",
217 scanelemdir);
218 return -EIO;
219 }
220
221 ret = -ENOENT;
222 while (ent = readdir(dp), ent) {
223 if (iioutils_check_suffix(ent->d_name, "_en")) {
224 printf("%sabling: %s\n",
225 enable ? "En" : "Dis",
226 ent->d_name);
227 ret = write_sysfs_int(ent->d_name, scanelemdir,
228 enable);
229 if (ret < 0)
230 fprintf(stderr, "Failed to enable/disable %s\n",
231 ent->d_name);
232 }
233 }
234
235 if (closedir(dp) == -1) {
236 perror("Enabling/disabling channels: "
237 "Failed to close directory");
238 return -errno;
239 }
240 return 0;
241}
242
e06e3d71
HK
243void print_usage(void)
244{
d9abc615
CO
245 fprintf(stderr, "Usage: generic_buffer [options]...\n"
246 "Capture, convert and output data from IIO device buffer\n"
5f991a92 247 " -a Auto-activate all available channels\n"
d9abc615
CO
248 " -c <n> Do n conversions\n"
249 " -e Disable wait for event (new data)\n"
250 " -g Use trigger-less mode\n"
251 " -l <n> Set buffer length to n samples\n"
252 " -n <name> Set device name (mandatory)\n"
253 " -t <name> Set trigger name\n"
254 " -w <n> Set delay between reads in us (event-less mode)\n");
e06e3d71
HK
255}
256
e58537cc
JC
257int main(int argc, char **argv)
258{
96df9799
JC
259 unsigned long num_loops = 2;
260 unsigned long timedelay = 1000000;
261 unsigned long buf_len = 128;
262
e58537cc 263 int ret, c, i, j, toread;
e58537cc
JC
264 int fp;
265
266 int num_channels;
267 char *trigger_name = NULL, *device_name = NULL;
268 char *dev_dir_name, *buf_dir_name;
269
270 int datardytrigger = 1;
271 char *data;
c77b3810 272 ssize_t read_size;
e58537cc 273 int dev_num, trig_num;
52615d47 274 char *buffer_access;
e58537cc 275 int scan_size;
30268a3d 276 int noevents = 0;
b6d5be57 277 int notrigger = 0;
5f991a92 278 enum autochan autochannels = AUTOCHANNELS_DISABLED;
96df9799 279 char *dummy;
e58537cc 280
d8da0eee 281 struct iio_channel_info *channels;
e58537cc 282
5f991a92 283 while ((c = getopt(argc, argv, "ac:egl:n:t:w:")) != -1) {
e58537cc 284 switch (c) {
5f991a92
LW
285 case 'a':
286 autochannels = AUTOCHANNELS_ENABLED;
287 break;
96df9799 288 case 'c':
c8ce9903 289 errno = 0;
96df9799 290 num_loops = strtoul(optarg, &dummy, 10);
c8ce9903
HK
291 if (errno)
292 return -errno;
7663a4aa 293
96df9799 294 break;
e06e3d71
HK
295 case 'e':
296 noevents = 1;
297 break;
298 case 'g':
299 notrigger = 1;
96df9799
JC
300 break;
301 case 'l':
c8ce9903 302 errno = 0;
96df9799 303 buf_len = strtoul(optarg, &dummy, 10);
c8ce9903
HK
304 if (errno)
305 return -errno;
7663a4aa 306
96df9799 307 break;
e06e3d71
HK
308 case 'n':
309 device_name = optarg;
310 break;
311 case 't':
312 trigger_name = optarg;
313 datardytrigger = 0;
314 break;
315 case 'w':
316 errno = 0;
317 timedelay = strtoul(optarg, &dummy, 10);
318 if (errno)
319 return -errno;
b6d5be57 320 break;
e58537cc 321 case '?':
e06e3d71 322 print_usage();
e58537cc
JC
323 return -1;
324 }
325 }
326
ff1ac639 327 if (!device_name) {
d9abc615 328 fprintf(stderr, "Device name not set\n");
e06e3d71 329 print_usage();
065896e9 330 return -1;
e06e3d71 331 }
065896e9 332
e58537cc 333 /* Find the device requested */
1aa04278 334 dev_num = find_type_by_name(device_name, "iio:device");
e58537cc 335 if (dev_num < 0) {
d9abc615 336 fprintf(stderr, "Failed to find the %s\n", device_name);
0e799878 337 return dev_num;
e58537cc 338 }
7663a4aa 339
e58537cc
JC
340 printf("iio device number being used is %d\n", dev_num);
341
e9e45b43
HK
342 ret = asprintf(&dev_dir_name, "%siio:device%d", iio_dir, dev_num);
343 if (ret < 0)
344 return -ENOMEM;
b6d5be57
KW
345
346 if (!notrigger) {
ff1ac639 347 if (!trigger_name) {
b6d5be57
KW
348 /*
349 * Build the trigger name. If it is device associated
350 * its name is <device_name>_dev[n] where n matches
351 * the device number found above.
352 */
353 ret = asprintf(&trigger_name,
354 "%s-dev%d", device_name, dev_num);
355 if (ret < 0) {
356 ret = -ENOMEM;
d3ccfc41 357 goto error_free_dev_dir_name;
b6d5be57 358 }
e58537cc 359 }
e58537cc 360
793d6b5e
LW
361 /* Look for this "-devN" trigger */
362 trig_num = find_type_by_name(trigger_name, "trigger");
363 if (trig_num < 0) {
364 /* OK try the simpler "-trigger" suffix instead */
365 free(trigger_name);
366 ret = asprintf(&trigger_name,
367 "%s-trigger", device_name);
368 if (ret < 0) {
369 ret = -ENOMEM;
370 goto error_free_dev_dir_name;
371 }
372 }
373
b6d5be57
KW
374 trig_num = find_type_by_name(trigger_name, "trigger");
375 if (trig_num < 0) {
d9abc615
CO
376 fprintf(stderr, "Failed to find the trigger %s\n",
377 trigger_name);
e83a47cf 378 ret = trig_num;
b6d5be57
KW
379 goto error_free_triggername;
380 }
7663a4aa 381
b6d5be57 382 printf("iio trigger number being used is %d\n", trig_num);
7663a4aa 383 } else {
b6d5be57 384 printf("trigger-less mode selected\n");
7663a4aa 385 }
e58537cc
JC
386
387 /*
388 * Parse the files in scan_elements to identify what channels are
389 * present
390 */
d8da0eee 391 ret = build_channel_array(dev_dir_name, &channels, &num_channels);
e58537cc 392 if (ret) {
d9abc615
CO
393 fprintf(stderr, "Problem reading scan element information\n"
394 "diag %s\n", dev_dir_name);
e58537cc
JC
395 goto error_free_triggername;
396 }
5f991a92
LW
397 if (num_channels && autochannels == AUTOCHANNELS_ENABLED) {
398 fprintf(stderr, "Auto-channels selected but some channels "
399 "are already activated in sysfs\n");
400 fprintf(stderr, "Proceeding without activating any channels\n");
401 }
402
403 if (!num_channels && autochannels == AUTOCHANNELS_ENABLED) {
404 fprintf(stderr,
405 "No channels are enabled, enabling all channels\n");
406
407 ret = enable_disable_all_channels(dev_dir_name, 1);
408 if (ret) {
409 fprintf(stderr, "Failed to enable all channels\n");
410 goto error_free_triggername;
411 }
412
413 /* This flags that we need to disable the channels again */
414 autochannels = AUTOCHANNELS_ACTIVE;
415
416 ret = build_channel_array(dev_dir_name, &channels,
417 &num_channels);
418 if (ret) {
419 fprintf(stderr, "Problem reading scan element "
420 "information\n"
421 "diag %s\n", dev_dir_name);
422 goto error_disable_channels;
423 }
424 if (!num_channels) {
425 fprintf(stderr, "Still no channels after "
426 "auto-enabling, giving up\n");
427 goto error_disable_channels;
428 }
429 }
430
431 if (!num_channels && autochannels == AUTOCHANNELS_DISABLED) {
53dabafe
LW
432 fprintf(stderr,
433 "No channels are enabled, we have nothing to scan.\n");
434 fprintf(stderr, "Enable channels manually in "
435 FORMAT_SCAN_ELEMENTS_DIR
5f991a92
LW
436 "/*_en or pass -a to autoenable channels and "
437 "try again.\n", dev_dir_name);
53dabafe
LW
438 ret = -ENOENT;
439 goto error_free_triggername;
440 }
e58537cc
JC
441
442 /*
443 * Construct the directory name for the associated buffer.
444 * As we know that the lis3l02dq has only one buffer this may
445 * be built rather than found.
446 */
1aa04278
JC
447 ret = asprintf(&buf_dir_name,
448 "%siio:device%d/buffer", iio_dir, dev_num);
e58537cc
JC
449 if (ret < 0) {
450 ret = -ENOMEM;
63f05c85 451 goto error_free_channels;
e58537cc 452 }
b6d5be57
KW
453
454 if (!notrigger) {
455 printf("%s %s\n", dev_dir_name, trigger_name);
7663a4aa
HK
456 /*
457 * Set the device trigger to be the data ready trigger found
458 * above
459 */
b6d5be57
KW
460 ret = write_sysfs_string_and_verify("trigger/current_trigger",
461 dev_dir_name,
462 trigger_name);
463 if (ret < 0) {
d9abc615
CO
464 fprintf(stderr,
465 "Failed to write current_trigger file\n");
b6d5be57
KW
466 goto error_free_buf_dir_name;
467 }
e58537cc
JC
468 }
469
470 /* Setup ring buffer parameters */
471 ret = write_sysfs_int("length", buf_dir_name, buf_len);
472 if (ret < 0)
473 goto error_free_buf_dir_name;
474
475 /* Enable the buffer */
476 ret = write_sysfs_int("enable", buf_dir_name, 1);
e7231491
IT
477 if (ret < 0) {
478 fprintf(stderr,
479 "Failed to enable buffer: %s\n", strerror(-ret));
e58537cc 480 goto error_free_buf_dir_name;
e7231491 481 }
7663a4aa 482
d8da0eee 483 scan_size = size_from_channelarray(channels, num_channels);
7663a4aa 484 data = malloc(scan_size * buf_len);
e58537cc
JC
485 if (!data) {
486 ret = -ENOMEM;
487 goto error_free_buf_dir_name;
488 }
489
1aa04278 490 ret = asprintf(&buffer_access, "/dev/iio:device%d", dev_num);
e58537cc
JC
491 if (ret < 0) {
492 ret = -ENOMEM;
493 goto error_free_data;
494 }
495
e58537cc
JC
496 /* Attempt to open non blocking the access dev */
497 fp = open(buffer_access, O_RDONLY | O_NONBLOCK);
7663a4aa 498 if (fp == -1) { /* TODO: If it isn't there make the node */
e58537cc 499 ret = -errno;
d9abc615 500 fprintf(stderr, "Failed to open %s\n", buffer_access);
52615d47 501 goto error_free_buffer_access;
e58537cc
JC
502 }
503
e58537cc 504 for (j = 0; j < num_loops; j++) {
30268a3d 505 if (!noevents) {
52615d47
JC
506 struct pollfd pfd = {
507 .fd = fp,
508 .events = POLLIN,
509 };
510
6bb7cac8
HK
511 ret = poll(&pfd, 1, -1);
512 if (ret < 0) {
513 ret = -errno;
514 goto error_close_buffer_access;
515 } else if (ret == 0) {
516 continue;
517 }
518
52615d47 519 toread = buf_len;
30268a3d 520 } else {
96df9799 521 usleep(timedelay);
30268a3d 522 toread = 64;
e58537cc 523 }
30268a3d 524
7663a4aa 525 read_size = read(fp, data, toread * scan_size);
97b603a4 526 if (read_size < 0) {
8749948a 527 if (errno == EAGAIN) {
d9abc615 528 fprintf(stderr, "nothing available\n");
97b603a4 529 continue;
7663a4aa 530 } else {
97b603a4 531 break;
7663a4aa 532 }
e58537cc 533 }
7663a4aa
HK
534 for (i = 0; i < read_size / scan_size; i++)
535 process_scan(data + scan_size * i, channels,
e58537cc
JC
536 num_channels);
537 }
538
d8da0eee 539 /* Stop the buffer */
e58537cc
JC
540 ret = write_sysfs_int("enable", buf_dir_name, 0);
541 if (ret < 0)
52615d47 542 goto error_close_buffer_access;
e58537cc 543
b6d5be57
KW
544 if (!notrigger)
545 /* Disconnect the trigger - just write a dummy name. */
6bb7cac8
HK
546 ret = write_sysfs_string("trigger/current_trigger",
547 dev_dir_name, "NULL");
548 if (ret < 0)
d9abc615
CO
549 fprintf(stderr, "Failed to write to %s\n",
550 dev_dir_name);
e58537cc 551
e58537cc 552error_close_buffer_access:
6bb7cac8
HK
553 if (close(fp) == -1)
554 perror("Failed to close buffer");
7663a4aa 555
e58537cc
JC
556error_free_buffer_access:
557 free(buffer_access);
a71bfb4a
HK
558error_free_data:
559 free(data);
e58537cc
JC
560error_free_buf_dir_name:
561 free(buf_dir_name);
63f05c85
HK
562error_free_channels:
563 for (i = num_channels - 1; i >= 0; i--) {
564 free(channels[i].name);
565 free(channels[i].generic_name);
566 }
567 free(channels);
e58537cc
JC
568error_free_triggername:
569 if (datardytrigger)
570 free(trigger_name);
5f991a92
LW
571error_disable_channels:
572 if (autochannels == AUTOCHANNELS_ACTIVE) {
573 ret = enable_disable_all_channels(dev_dir_name, 0);
574 if (ret)
575 fprintf(stderr, "Failed to disable all channels\n");
576 }
d3ccfc41
HK
577error_free_dev_dir_name:
578 free(dev_dir_name);
0e799878 579
e58537cc
JC
580 return ret;
581}
This page took 0.524846 seconds and 5 git commands to generate.