Merge branch 'bugfixes' of git://git.linux-nfs.org/projects/trondmy/nfs-2.6
[pandora-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 #include <unistd.h>
22 #include <dirent.h>
23 #include <fcntl.h>
24 #include <stdio.h>
25 #include <errno.h>
26 #include <sys/stat.h>
27 #include <sys/dir.h>
28 #include <linux/types.h>
29 #include "iio_utils.h"
30
31 const int buf_len = 128;
32 const int num_loops = 2;
33
34 /**
35  * size_from_channelarray() - calculate the storage size of a scan
36  * @channels: the channel info array
37  * @num_channels: size of the channel info array
38  *
39  * Has the side effect of filling the channels[i].location values used
40  * in processing the buffer output.
41  **/
42 int size_from_channelarray(struct iio_channel_info *channels, int num_channels)
43 {
44         int bytes = 0;
45         int i = 0;
46         while (i < num_channels) {
47                 if (bytes % channels[i].bytes == 0)
48                         channels[i].location = bytes;
49                 else
50                         channels[i].location = bytes - bytes%channels[i].bytes
51                                 + channels[i].bytes;
52                 bytes = channels[i].location + channels[i].bytes;
53                 i++;
54         }
55         return bytes;
56 }
57
58 /**
59  * process_scan() - print out the values in SI units
60  * @data:               pointer to the start of the scan
61  * @infoarray:          information about the channels. Note
62  *  size_from_channelarray must have been called first to fill the
63  *  location offsets.
64  * @num_channels:       the number of active channels
65  **/
66 void process_scan(char *data,
67                   struct iio_channel_info *infoarray,
68                   int num_channels)
69 {
70         int k;
71         for (k = 0; k < num_channels; k++)
72                 switch (infoarray[k].bytes) {
73                         /* only a few cases implemented so far */
74                 case 2:
75                         if (infoarray[k].is_signed) {
76                                 int16_t val = *(int16_t *)
77                                         (data
78                                          + infoarray[k].location);
79                                 if ((val >> infoarray[k].bits_used) & 1)
80                                         val = (val & infoarray[k].mask) |
81                                                 ~infoarray[k].mask;
82                                 printf("%05f ", ((float)val +
83                                                  infoarray[k].offset)*
84                                        infoarray[k].scale);
85                         } else {
86                                 uint16_t val = *(uint16_t *)
87                                         (data +
88                                          infoarray[k].location);
89                                 val = (val & infoarray[k].mask);
90                                 printf("%05f ", ((float)val +
91                                                  infoarray[k].offset)*
92                                        infoarray[k].scale);
93                         }
94                         break;
95                 case 8:
96                         if (infoarray[k].is_signed) {
97                                 int64_t val = *(int64_t *)
98                                         (data +
99                                          infoarray[k].location);
100                                 if ((val >> infoarray[k].bits_used) & 1)
101                                         val = (val & infoarray[k].mask) |
102                                                 ~infoarray[k].mask;
103                                 /* special case for timestamp */
104                                 if (infoarray[k].scale == 1.0f &&
105                                     infoarray[k].offset == 0.0f)
106                                         printf(" %lld", val);
107                                 else
108                                         printf("%05f ", ((float)val +
109                                                          infoarray[k].offset)*
110                                                infoarray[k].scale);
111                         }
112                         break;
113                 default:
114                         break;
115                 }
116         printf("\n");
117 }
118
119 int main(int argc, char **argv)
120 {
121         int ret, c, i, j, toread;
122
123         FILE *fp_ev;
124         int fp;
125
126         int num_channels;
127         char *trigger_name = NULL, *device_name = NULL;
128         char *dev_dir_name, *buf_dir_name;
129
130         int datardytrigger = 1;
131         char *data;
132         size_t read_size;
133         struct iio_event_data dat;
134         int dev_num, trig_num;
135         char *buffer_access, *buffer_event;
136         int scan_size;
137
138         struct iio_channel_info *infoarray;
139
140         while ((c = getopt(argc, argv, "t:n:")) != -1) {
141                 switch (c) {
142                 case 'n':
143                         device_name = optarg;
144                         break;
145                 case 't':
146                         trigger_name = optarg;
147                         datardytrigger = 0;
148                         break;
149                 case '?':
150                         return -1;
151                 }
152         }
153
154         /* Find the device requested */
155         dev_num = find_type_by_name(device_name, "device");
156         if (dev_num < 0) {
157                 printf("Failed to find the %s\n", device_name);
158                 ret = -ENODEV;
159                 goto error_ret;
160         }
161         printf("iio device number being used is %d\n", dev_num);
162
163         asprintf(&dev_dir_name, "%sdevice%d", iio_dir, dev_num);
164         if (trigger_name == NULL) {
165                 /*
166                  * Build the trigger name. If it is device associated it's
167                  * name is <device_name>_dev[n] where n matches the device
168                  * number found above
169                  */
170                 ret = asprintf(&trigger_name,
171                                "%s-dev%d", device_name, dev_num);
172                 if (ret < 0) {
173                         ret = -ENOMEM;
174                         goto error_ret;
175                 }
176         }
177
178         /* Verify the trigger exists */
179         trig_num = find_type_by_name(trigger_name, "trigger");
180         if (trig_num < 0) {
181                 printf("Failed to find the trigger %s\n", trigger_name);
182                 ret = -ENODEV;
183                 goto error_free_triggername;
184         }
185         printf("iio trigger number being used is %d\n", trig_num);
186
187         /*
188          * Parse the files in scan_elements to identify what channels are
189          * present
190          */
191         ret = build_channel_array(dev_dir_name, &infoarray, &num_channels);
192         if (ret) {
193                 printf("Problem reading scan element information \n");
194                 goto error_free_triggername;
195         }
196
197         /*
198          * Construct the directory name for the associated buffer.
199          * As we know that the lis3l02dq has only one buffer this may
200          * be built rather than found.
201          */
202         ret = asprintf(&buf_dir_name, "%sdevice%d:buffer0", iio_dir, dev_num);
203         if (ret < 0) {
204                 ret = -ENOMEM;
205                 goto error_free_triggername;
206         }
207         printf("%s %s\n", dev_dir_name, trigger_name);
208         /* Set the device trigger to be the data rdy trigger found above */
209         ret = write_sysfs_string_and_verify("trigger/current_trigger",
210                                         dev_dir_name,
211                                         trigger_name);
212         if (ret < 0) {
213                 printf("Failed to write current_trigger file\n");
214                 goto error_free_buf_dir_name;
215         }
216
217         /* Setup ring buffer parameters */
218         ret = write_sysfs_int("length", buf_dir_name, buf_len);
219         if (ret < 0)
220                 goto error_free_buf_dir_name;
221
222         /* Enable the buffer */
223         ret = write_sysfs_int("enable", buf_dir_name, 1);
224         if (ret < 0)
225                 goto error_free_buf_dir_name;
226         scan_size = size_from_channelarray(infoarray, num_channels);
227         data = malloc(scan_size*buf_len);
228         if (!data) {
229                 ret = -ENOMEM;
230                 goto error_free_buf_dir_name;
231         }
232
233         ret = asprintf(&buffer_access,
234                        "/dev/device%d:buffer0:access0",
235                        dev_num);
236         if (ret < 0) {
237                 ret = -ENOMEM;
238                 goto error_free_data;
239         }
240
241         ret = asprintf(&buffer_event, "/dev/device%d:buffer0:event0", dev_num);
242         if (ret < 0) {
243                 ret = -ENOMEM;
244                 goto error_free_buffer_access;
245         }
246         /* Attempt to open non blocking the access dev */
247         fp = open(buffer_access, O_RDONLY | O_NONBLOCK);
248         if (fp == -1) { /*If it isn't there make the node */
249                 printf("Failed to open %s\n", buffer_access);
250                 ret = -errno;
251                 goto error_free_buffer_event;
252         }
253         /* Attempt to open the event access dev (blocking this time) */
254         fp_ev = fopen(buffer_event, "rb");
255         if (fp_ev == NULL) {
256                 printf("Failed to open %s\n", buffer_event);
257                 ret = -errno;
258                 goto error_close_buffer_access;
259         }
260
261         /* Wait for events 10 times */
262         for (j = 0; j < num_loops; j++) {
263                 read_size = fread(&dat, 1, sizeof(struct iio_event_data),
264                                   fp_ev);
265                 switch (dat.id) {
266                 case IIO_EVENT_CODE_RING_100_FULL:
267                         toread = buf_len;
268                         break;
269                 case IIO_EVENT_CODE_RING_75_FULL:
270                         toread = buf_len*3/4;
271                         break;
272                 case IIO_EVENT_CODE_RING_50_FULL:
273                         toread = buf_len/2;
274                         break;
275                 default:
276                         printf("Unexpecteded event code\n");
277                         continue;
278                 }
279                 read_size = read(fp,
280                                  data,
281                                  toread*scan_size);
282                 if (read_size == -EAGAIN) {
283                         printf("nothing available\n");
284                         continue;
285                 }
286                 for (i = 0; i < read_size/scan_size; i++)
287                         process_scan(data + scan_size*i,
288                                      infoarray,
289                                      num_channels);
290         }
291
292         /* Stop the ring buffer */
293         ret = write_sysfs_int("enable", buf_dir_name, 0);
294         if (ret < 0)
295                 goto error_close_buffer_event;
296
297         /* Disconnect from the trigger - just write a dummy name.*/
298         write_sysfs_string("trigger/current_trigger",
299                         dev_dir_name, "NULL");
300
301 error_close_buffer_event:
302         fclose(fp_ev);
303 error_close_buffer_access:
304         close(fp);
305 error_free_data:
306         free(data);
307 error_free_buffer_access:
308         free(buffer_access);
309 error_free_buffer_event:
310         free(buffer_event);
311 error_free_buf_dir_name:
312         free(buf_dir_name);
313 error_free_triggername:
314         if (datardytrigger)
315                 free(trigger_name);
316 error_ret:
317         return ret;
318 }