Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 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> |
Roberta Dobrescu | bdcb31d | 2015-02-26 10:49:24 +0200 | [diff] [blame] | 22 | #include <stdlib.h> |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 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> |
Jonathan Cameron | 30268a3 | 2011-02-11 13:09:12 +0000 | [diff] [blame] | 30 | #include <string.h> |
Jonathan Cameron | 52615d4 | 2011-05-18 14:41:19 +0100 | [diff] [blame] | 31 | #include <poll.h> |
Jonathan Cameron | 117cf8b | 2011-12-04 19:10:59 +0000 | [diff] [blame] | 32 | #include <endian.h> |
Peter Meerwald | bb23378 | 2012-06-25 23:12:17 +0200 | [diff] [blame] | 33 | #include <getopt.h> |
Peter Meerwald | 1bcdfbc | 2012-06-25 23:12:16 +0200 | [diff] [blame] | 34 | #include <inttypes.h> |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 35 | #include "iio_utils.h" |
| 36 | |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 37 | /** |
| 38 | * size_from_channelarray() - calculate the storage size of a scan |
Peter Meerwald | d8da0ee | 2012-06-25 23:12:14 +0200 | [diff] [blame] | 39 | * @channels: the channel info array |
| 40 | * @num_channels: number of channels |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 41 | * |
| 42 | * Has the side effect of filling the channels[i].location values used |
| 43 | * in processing the buffer output. |
| 44 | **/ |
| 45 | int size_from_channelarray(struct iio_channel_info *channels, int num_channels) |
| 46 | { |
| 47 | int bytes = 0; |
| 48 | int i = 0; |
Melike Yurtoglu | ff39a25 | 2014-10-03 23:34:50 +0300 | [diff] [blame] | 49 | |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 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 | |
Hartmut Knaack | 8e92613 | 2015-05-31 14:39:58 +0200 | [diff] [blame] | 62 | void print2byte(uint16_t input, struct iio_channel_info *info) |
Jonathan Cameron | 52615d4 | 2011-05-18 14:41:19 +0100 | [diff] [blame] | 63 | { |
Jonathan Cameron | 117cf8b | 2011-12-04 19:10:59 +0000 | [diff] [blame] | 64 | /* First swap if incorrect endian */ |
Jonathan Cameron | 117cf8b | 2011-12-04 19:10:59 +0000 | [diff] [blame] | 65 | if (info->be) |
Hartmut Knaack | 8e92613 | 2015-05-31 14:39:58 +0200 | [diff] [blame] | 66 | input = be16toh(input); |
Jonathan Cameron | 117cf8b | 2011-12-04 19:10:59 +0000 | [diff] [blame] | 67 | else |
Hartmut Knaack | 8e92613 | 2015-05-31 14:39:58 +0200 | [diff] [blame] | 68 | input = le16toh(input); |
Jonathan Cameron | 117cf8b | 2011-12-04 19:10:59 +0000 | [diff] [blame] | 69 | |
Peter Meerwald | d8da0ee | 2012-06-25 23:12:14 +0200 | [diff] [blame] | 70 | /* |
| 71 | * Shift before conversion to avoid sign extension |
| 72 | * of left aligned data |
| 73 | */ |
Aya Mahfouz | 1525ecf | 2015-02-26 11:45:26 +0200 | [diff] [blame] | 74 | input >>= info->shift; |
Hartmut Knaack | 8e92613 | 2015-05-31 14:39:58 +0200 | [diff] [blame] | 75 | input &= info->mask; |
Jonathan Cameron | 52615d4 | 2011-05-18 14:41:19 +0100 | [diff] [blame] | 76 | if (info->is_signed) { |
Hartmut Knaack | 8e92613 | 2015-05-31 14:39:58 +0200 | [diff] [blame] | 77 | int16_t val = (int16_t)(input << (16 - info->bits_used)) >> |
| 78 | (16 - info->bits_used); |
| 79 | printf("%05f ", ((float)val + info->offset) * info->scale); |
Jonathan Cameron | 52615d4 | 2011-05-18 14:41:19 +0100 | [diff] [blame] | 80 | } else { |
Hartmut Knaack | 8e92613 | 2015-05-31 14:39:58 +0200 | [diff] [blame] | 81 | printf("%05f ", ((float)input + info->offset) * info->scale); |
Jonathan Cameron | 52615d4 | 2011-05-18 14:41:19 +0100 | [diff] [blame] | 82 | } |
| 83 | } |
Hartmut Knaack | 8e92613 | 2015-05-31 14:39:58 +0200 | [diff] [blame] | 84 | |
| 85 | void print4byte(uint32_t input, struct iio_channel_info *info) |
| 86 | { |
| 87 | /* First swap if incorrect endian */ |
| 88 | if (info->be) |
| 89 | input = be32toh(input); |
| 90 | else |
| 91 | input = le32toh(input); |
| 92 | |
| 93 | /* |
| 94 | * Shift before conversion to avoid sign extension |
| 95 | * of left aligned data |
| 96 | */ |
| 97 | input >>= info->shift; |
| 98 | input &= info->mask; |
| 99 | if (info->is_signed) { |
| 100 | int32_t val = (int32_t)(input << (32 - info->bits_used)) >> |
| 101 | (32 - info->bits_used); |
| 102 | printf("%05f ", ((float)val + info->offset) * info->scale); |
| 103 | } else { |
| 104 | printf("%05f ", ((float)input + info->offset) * info->scale); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | void print8byte(uint64_t input, struct iio_channel_info *info) |
| 109 | { |
| 110 | /* First swap if incorrect endian */ |
| 111 | if (info->be) |
| 112 | input = be64toh(input); |
| 113 | else |
| 114 | input = le64toh(input); |
| 115 | |
| 116 | /* |
| 117 | * Shift before conversion to avoid sign extension |
| 118 | * of left aligned data |
| 119 | */ |
| 120 | input >>= info->shift; |
| 121 | input &= info->mask; |
| 122 | if (info->is_signed) { |
| 123 | int64_t val = (int64_t)(input << (64 - info->bits_used)) >> |
| 124 | (64 - info->bits_used); |
| 125 | /* special case for timestamp */ |
| 126 | if (info->scale == 1.0f && info->offset == 0.0f) |
| 127 | printf("%" PRId64 " ", val); |
| 128 | else |
| 129 | printf("%05f ", |
| 130 | ((float)val + info->offset) * info->scale); |
| 131 | } else { |
| 132 | printf("%05f ", ((float)input + info->offset) * info->scale); |
| 133 | } |
| 134 | } |
| 135 | |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 136 | /** |
| 137 | * process_scan() - print out the values in SI units |
| 138 | * @data: pointer to the start of the scan |
Peter Meerwald | d8da0ee | 2012-06-25 23:12:14 +0200 | [diff] [blame] | 139 | * @channels: information about the channels. Note |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 140 | * size_from_channelarray must have been called first to fill the |
| 141 | * location offsets. |
Peter Meerwald | d8da0ee | 2012-06-25 23:12:14 +0200 | [diff] [blame] | 142 | * @num_channels: number of channels |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 143 | **/ |
| 144 | void process_scan(char *data, |
Peter Meerwald | d8da0ee | 2012-06-25 23:12:14 +0200 | [diff] [blame] | 145 | struct iio_channel_info *channels, |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 146 | int num_channels) |
| 147 | { |
| 148 | int k; |
Melike Yurtoglu | ff39a25 | 2014-10-03 23:34:50 +0300 | [diff] [blame] | 149 | |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 150 | for (k = 0; k < num_channels; k++) |
Peter Meerwald | d8da0ee | 2012-06-25 23:12:14 +0200 | [diff] [blame] | 151 | switch (channels[k].bytes) { |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 152 | /* only a few cases implemented so far */ |
| 153 | case 2: |
Peter Meerwald | d8da0ee | 2012-06-25 23:12:14 +0200 | [diff] [blame] | 154 | print2byte(*(uint16_t *)(data + channels[k].location), |
| 155 | &channels[k]); |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 156 | break; |
Marek Vasut | 6cffc1f | 2012-08-12 16:21:00 +0100 | [diff] [blame] | 157 | case 4: |
Hartmut Knaack | 8e92613 | 2015-05-31 14:39:58 +0200 | [diff] [blame] | 158 | print4byte(*(uint32_t *)(data + channels[k].location), |
| 159 | &channels[k]); |
Marek Vasut | 6cffc1f | 2012-08-12 16:21:00 +0100 | [diff] [blame] | 160 | break; |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 161 | case 8: |
Hartmut Knaack | 8e92613 | 2015-05-31 14:39:58 +0200 | [diff] [blame] | 162 | print8byte(*(uint64_t *)(data + channels[k].location), |
| 163 | &channels[k]); |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 164 | break; |
| 165 | default: |
| 166 | break; |
| 167 | } |
| 168 | printf("\n"); |
| 169 | } |
| 170 | |
| 171 | int main(int argc, char **argv) |
| 172 | { |
Jonathan Cameron | 96df979 | 2011-02-11 13:09:13 +0000 | [diff] [blame] | 173 | unsigned long num_loops = 2; |
| 174 | unsigned long timedelay = 1000000; |
| 175 | unsigned long buf_len = 128; |
| 176 | |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 177 | int ret, c, i, j, toread; |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 178 | int fp; |
| 179 | |
| 180 | int num_channels; |
| 181 | char *trigger_name = NULL, *device_name = NULL; |
| 182 | char *dev_dir_name, *buf_dir_name; |
| 183 | |
| 184 | int datardytrigger = 1; |
| 185 | char *data; |
Jonathan Cameron | c77b381 | 2011-04-15 18:56:00 +0100 | [diff] [blame] | 186 | ssize_t read_size; |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 187 | int dev_num, trig_num; |
Jonathan Cameron | 52615d4 | 2011-05-18 14:41:19 +0100 | [diff] [blame] | 188 | char *buffer_access; |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 189 | int scan_size; |
Jonathan Cameron | 30268a3 | 2011-02-11 13:09:12 +0000 | [diff] [blame] | 190 | int noevents = 0; |
Karol Wrona | b6d5be57 | 2014-11-04 15:29:39 +0100 | [diff] [blame] | 191 | int notrigger = 0; |
Jonathan Cameron | 96df979 | 2011-02-11 13:09:13 +0000 | [diff] [blame] | 192 | char *dummy; |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 193 | |
Peter Meerwald | d8da0ee | 2012-06-25 23:12:14 +0200 | [diff] [blame] | 194 | struct iio_channel_info *channels; |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 195 | |
Karol Wrona | b6d5be57 | 2014-11-04 15:29:39 +0100 | [diff] [blame] | 196 | while ((c = getopt(argc, argv, "l:w:c:et:n:g")) != -1) { |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 197 | switch (c) { |
| 198 | case 'n': |
| 199 | device_name = optarg; |
| 200 | break; |
| 201 | case 't': |
| 202 | trigger_name = optarg; |
| 203 | datardytrigger = 0; |
| 204 | break; |
Jonathan Cameron | 30268a3 | 2011-02-11 13:09:12 +0000 | [diff] [blame] | 205 | case 'e': |
| 206 | noevents = 1; |
| 207 | break; |
Jonathan Cameron | 96df979 | 2011-02-11 13:09:13 +0000 | [diff] [blame] | 208 | case 'c': |
| 209 | num_loops = strtoul(optarg, &dummy, 10); |
| 210 | break; |
| 211 | case 'w': |
| 212 | timedelay = strtoul(optarg, &dummy, 10); |
| 213 | break; |
| 214 | case 'l': |
| 215 | buf_len = strtoul(optarg, &dummy, 10); |
| 216 | break; |
Karol Wrona | b6d5be57 | 2014-11-04 15:29:39 +0100 | [diff] [blame] | 217 | case 'g': |
| 218 | notrigger = 1; |
| 219 | break; |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 220 | case '?': |
| 221 | return -1; |
| 222 | } |
| 223 | } |
| 224 | |
Michael Hennerich | 065896e | 2011-02-24 16:34:50 +0100 | [diff] [blame] | 225 | if (device_name == NULL) |
| 226 | return -1; |
| 227 | |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 228 | /* Find the device requested */ |
Jonathan Cameron | 1aa0427 | 2011-08-30 12:32:47 +0100 | [diff] [blame] | 229 | dev_num = find_type_by_name(device_name, "iio:device"); |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 230 | if (dev_num < 0) { |
| 231 | printf("Failed to find the %s\n", device_name); |
Hartmut Knaack | e83a47c | 2015-05-31 14:39:57 +0200 | [diff] [blame] | 232 | ret = dev_num; |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 233 | goto error_ret; |
| 234 | } |
| 235 | printf("iio device number being used is %d\n", dev_num); |
| 236 | |
Hartmut Knaack | e9e45b4 | 2015-05-31 14:40:02 +0200 | [diff] [blame^] | 237 | ret = asprintf(&dev_dir_name, "%siio:device%d", iio_dir, dev_num); |
| 238 | if (ret < 0) |
| 239 | return -ENOMEM; |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 240 | |
Karol Wrona | b6d5be57 | 2014-11-04 15:29:39 +0100 | [diff] [blame] | 241 | if (!notrigger) { |
| 242 | if (trigger_name == NULL) { |
| 243 | /* |
| 244 | * Build the trigger name. If it is device associated |
| 245 | * its name is <device_name>_dev[n] where n matches |
| 246 | * the device number found above. |
| 247 | */ |
| 248 | ret = asprintf(&trigger_name, |
| 249 | "%s-dev%d", device_name, dev_num); |
| 250 | if (ret < 0) { |
| 251 | ret = -ENOMEM; |
Hartmut Knaack | d3ccfc4 | 2015-05-31 14:39:42 +0200 | [diff] [blame] | 252 | goto error_free_dev_dir_name; |
Karol Wrona | b6d5be57 | 2014-11-04 15:29:39 +0100 | [diff] [blame] | 253 | } |
| 254 | } |
| 255 | |
| 256 | /* Verify the trigger exists */ |
| 257 | trig_num = find_type_by_name(trigger_name, "trigger"); |
| 258 | if (trig_num < 0) { |
| 259 | printf("Failed to find the trigger %s\n", trigger_name); |
Hartmut Knaack | e83a47c | 2015-05-31 14:39:57 +0200 | [diff] [blame] | 260 | ret = trig_num; |
Karol Wrona | b6d5be57 | 2014-11-04 15:29:39 +0100 | [diff] [blame] | 261 | goto error_free_triggername; |
| 262 | } |
| 263 | printf("iio trigger number being used is %d\n", trig_num); |
| 264 | } else |
| 265 | printf("trigger-less mode selected\n"); |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 266 | |
| 267 | /* |
| 268 | * Parse the files in scan_elements to identify what channels are |
| 269 | * present |
| 270 | */ |
Peter Meerwald | d8da0ee | 2012-06-25 23:12:14 +0200 | [diff] [blame] | 271 | ret = build_channel_array(dev_dir_name, &channels, &num_channels); |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 272 | if (ret) { |
Jonathan Cameron | 52615d4 | 2011-05-18 14:41:19 +0100 | [diff] [blame] | 273 | printf("Problem reading scan element information\n"); |
Jonathan Cameron | 1aa0427 | 2011-08-30 12:32:47 +0100 | [diff] [blame] | 274 | printf("diag %s\n", dev_dir_name); |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 275 | goto error_free_triggername; |
| 276 | } |
| 277 | |
| 278 | /* |
| 279 | * Construct the directory name for the associated buffer. |
| 280 | * As we know that the lis3l02dq has only one buffer this may |
| 281 | * be built rather than found. |
| 282 | */ |
Jonathan Cameron | 1aa0427 | 2011-08-30 12:32:47 +0100 | [diff] [blame] | 283 | ret = asprintf(&buf_dir_name, |
| 284 | "%siio:device%d/buffer", iio_dir, dev_num); |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 285 | if (ret < 0) { |
| 286 | ret = -ENOMEM; |
Hartmut Knaack | 63f05c8 | 2015-05-31 14:39:44 +0200 | [diff] [blame] | 287 | goto error_free_channels; |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 288 | } |
Karol Wrona | b6d5be57 | 2014-11-04 15:29:39 +0100 | [diff] [blame] | 289 | |
| 290 | if (!notrigger) { |
| 291 | printf("%s %s\n", dev_dir_name, trigger_name); |
| 292 | /* Set the device trigger to be the data ready trigger found |
| 293 | * above */ |
| 294 | ret = write_sysfs_string_and_verify("trigger/current_trigger", |
| 295 | dev_dir_name, |
| 296 | trigger_name); |
| 297 | if (ret < 0) { |
| 298 | printf("Failed to write current_trigger file\n"); |
| 299 | goto error_free_buf_dir_name; |
| 300 | } |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | /* Setup ring buffer parameters */ |
| 304 | ret = write_sysfs_int("length", buf_dir_name, buf_len); |
| 305 | if (ret < 0) |
| 306 | goto error_free_buf_dir_name; |
| 307 | |
| 308 | /* Enable the buffer */ |
| 309 | ret = write_sysfs_int("enable", buf_dir_name, 1); |
| 310 | if (ret < 0) |
| 311 | goto error_free_buf_dir_name; |
Peter Meerwald | d8da0ee | 2012-06-25 23:12:14 +0200 | [diff] [blame] | 312 | scan_size = size_from_channelarray(channels, num_channels); |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 313 | data = malloc(scan_size*buf_len); |
| 314 | if (!data) { |
| 315 | ret = -ENOMEM; |
| 316 | goto error_free_buf_dir_name; |
| 317 | } |
| 318 | |
Jonathan Cameron | 1aa0427 | 2011-08-30 12:32:47 +0100 | [diff] [blame] | 319 | ret = asprintf(&buffer_access, "/dev/iio:device%d", dev_num); |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 320 | if (ret < 0) { |
| 321 | ret = -ENOMEM; |
| 322 | goto error_free_data; |
| 323 | } |
| 324 | |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 325 | /* Attempt to open non blocking the access dev */ |
| 326 | fp = open(buffer_access, O_RDONLY | O_NONBLOCK); |
Peter Meerwald | d8da0ee | 2012-06-25 23:12:14 +0200 | [diff] [blame] | 327 | if (fp == -1) { /* If it isn't there make the node */ |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 328 | ret = -errno; |
Hartmut Knaack | 2b6a6e6 | 2015-05-31 14:39:48 +0200 | [diff] [blame] | 329 | printf("Failed to open %s\n", buffer_access); |
Jonathan Cameron | 52615d4 | 2011-05-18 14:41:19 +0100 | [diff] [blame] | 330 | goto error_free_buffer_access; |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | /* Wait for events 10 times */ |
| 334 | for (j = 0; j < num_loops; j++) { |
Jonathan Cameron | 30268a3 | 2011-02-11 13:09:12 +0000 | [diff] [blame] | 335 | if (!noevents) { |
Jonathan Cameron | 52615d4 | 2011-05-18 14:41:19 +0100 | [diff] [blame] | 336 | struct pollfd pfd = { |
| 337 | .fd = fp, |
| 338 | .events = POLLIN, |
| 339 | }; |
| 340 | |
| 341 | poll(&pfd, 1, -1); |
| 342 | toread = buf_len; |
| 343 | |
Jonathan Cameron | 30268a3 | 2011-02-11 13:09:12 +0000 | [diff] [blame] | 344 | } else { |
Jonathan Cameron | 96df979 | 2011-02-11 13:09:13 +0000 | [diff] [blame] | 345 | usleep(timedelay); |
Jonathan Cameron | 30268a3 | 2011-02-11 13:09:12 +0000 | [diff] [blame] | 346 | toread = 64; |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 347 | } |
Jonathan Cameron | 30268a3 | 2011-02-11 13:09:12 +0000 | [diff] [blame] | 348 | |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 349 | read_size = read(fp, |
| 350 | data, |
| 351 | toread*scan_size); |
Peter Meerwald | 97b603a | 2014-12-06 06:00:00 +0000 | [diff] [blame] | 352 | if (read_size < 0) { |
Hartmut Knaack | 8749948 | 2015-05-31 14:39:56 +0200 | [diff] [blame] | 353 | if (errno == EAGAIN) { |
Peter Meerwald | 97b603a | 2014-12-06 06:00:00 +0000 | [diff] [blame] | 354 | printf("nothing available\n"); |
| 355 | continue; |
| 356 | } else |
| 357 | break; |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 358 | } |
| 359 | for (i = 0; i < read_size/scan_size; i++) |
| 360 | process_scan(data + scan_size*i, |
Peter Meerwald | d8da0ee | 2012-06-25 23:12:14 +0200 | [diff] [blame] | 361 | channels, |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 362 | num_channels); |
| 363 | } |
| 364 | |
Peter Meerwald | d8da0ee | 2012-06-25 23:12:14 +0200 | [diff] [blame] | 365 | /* Stop the buffer */ |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 366 | ret = write_sysfs_int("enable", buf_dir_name, 0); |
| 367 | if (ret < 0) |
Jonathan Cameron | 52615d4 | 2011-05-18 14:41:19 +0100 | [diff] [blame] | 368 | goto error_close_buffer_access; |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 369 | |
Karol Wrona | b6d5be57 | 2014-11-04 15:29:39 +0100 | [diff] [blame] | 370 | if (!notrigger) |
| 371 | /* Disconnect the trigger - just write a dummy name. */ |
| 372 | write_sysfs_string("trigger/current_trigger", |
| 373 | dev_dir_name, "NULL"); |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 374 | |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 375 | error_close_buffer_access: |
| 376 | close(fp); |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 377 | error_free_buffer_access: |
| 378 | free(buffer_access); |
Hartmut Knaack | a71bfb4 | 2015-05-31 14:39:41 +0200 | [diff] [blame] | 379 | error_free_data: |
| 380 | free(data); |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 381 | error_free_buf_dir_name: |
| 382 | free(buf_dir_name); |
Hartmut Knaack | 63f05c8 | 2015-05-31 14:39:44 +0200 | [diff] [blame] | 383 | error_free_channels: |
| 384 | for (i = num_channels - 1; i >= 0; i--) { |
| 385 | free(channels[i].name); |
| 386 | free(channels[i].generic_name); |
| 387 | } |
| 388 | free(channels); |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 389 | error_free_triggername: |
| 390 | if (datardytrigger) |
| 391 | free(trigger_name); |
Hartmut Knaack | d3ccfc4 | 2015-05-31 14:39:42 +0200 | [diff] [blame] | 392 | error_free_dev_dir_name: |
| 393 | free(dev_dir_name); |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 394 | error_ret: |
| 395 | return ret; |
| 396 | } |