OSDN Git Service

Handle max_freq parameter
[android-x86/hardware-intel-libsensors.git] / common.h
1 /*
2  * Copyright (C) 2014 Intel Corporation.
3  */
4
5 #ifndef __COMMON_H__
6 #define __COMMON_H__
7
8 #define MAX_DEVICES     8       /* Check iio devices 0 to MAX_DEVICES-1 */
9 #define MAX_SENSORS     10      /* We can handle as many sensors */
10 #define MAX_CHANNELS    4       /* We can handle as many channels per sensor */
11 #define MAX_TRIGGERS    8       /* Check for triggers 0 to MAX_TRIGGERS-1 */
12
13 #define DEV_FILE_PATH           "/dev/iio:device%d"
14 #define BASE_PATH               "/sys/bus/iio/devices/iio:device%d/"
15 #define TRIGGER_FILE_PATH       "/sys/bus/iio/devices/trigger%d/name"
16
17 #define CHANNEL_PATH            BASE_PATH "scan_elements/"
18 #define ENABLE_PATH             BASE_PATH "buffer/enable"
19 #define NAME_PATH               BASE_PATH "name"
20 #define TRIGGER_PATH            BASE_PATH "trigger/current_trigger"
21 #define SENSOR_OFFSET_PATH      BASE_PATH "in_%s_offset"
22 #define SENSOR_SCALE_PATH       BASE_PATH "in_%s_scale"
23 #define SENSOR_SAMPLING_PATH    BASE_PATH "in_%s_sampling_frequency"
24 #define DEVICE_SAMPLING_PATH    BASE_PATH "sampling_frequency"
25 #define DEVICE_AVAIL_FREQ_PATH  BASE_PATH "sampling_frequency_available"
26 #define ILLUMINATION_CALIBPATH  BASE_PATH "in_illuminance_calibscale"
27
28 #define PROP_BASE       "ro.iio.%s.%s" /* Note: PROPERTY_KEY_MAX is small */
29
30 #define MAX_TYPE_SPEC_LEN 32    /* Channel type spec len; ex: "le:u10/16>>0" */
31 #define MAX_SENSOR_REPORT_SIZE  32      /* Sensor report buffer size */
32
33 #define MAX_NAME_SIZE           32
34
35 #define MIN_SAMPLES 5
36
37 #define ARRAY_SIZE(x) sizeof(x)/sizeof(x[0])
38 #define REPORTING_MODE(x)       ((x) & 0x06)
39
40 #ifdef __LP64__
41         typedef uint64_t flag_t;
42         typedef int64_t max_delay_t;
43 #else
44         typedef uint32_t flag_t;
45         typedef int32_t max_delay_t;
46 #endif
47
48 struct channel_descriptor_t
49 {
50         const char *name;       /* channel name ; ex: x */
51
52         /* sysfs entries located under scan_elements */
53         const char *en_path;    /* Enabled sysfs file name ; ex: "in_temp_en" */
54         const char *type_path;  /* _type sysfs file name  */
55         const char *index_path; /* _index sysfs file name */
56
57         /* sysfs entries located in /sys/bus/iio/devices/iio:deviceX */
58         const char *raw_path;   /* _raw sysfs file name  */
59         const char *input_path; /* _input sysfs file name */
60         const char *scale_path; /* _scale sysfs file name */
61 };
62
63 struct sensor_catalog_entry_t
64 {
65         const char *tag; /* Prefix such as "accel", "gyro", "temp"... */
66         const int type;  /* Sensor type ; ex: SENSOR_TYPE_ACCELEROMETER */
67         const int num_channels; /* Expected iio channels for this sensor */
68         struct channel_descriptor_t channel[MAX_CHANNELS];
69 };
70
71 struct datum_info_t
72 {
73         char sign;
74         char endianness;
75         short realbits;
76         short storagebits;
77         short shift;
78 };
79
80 struct channel_info_t
81 {
82         int offset; /* Offset in bytes within the iio character device report */
83         int size;                       /* Field size in bytes */
84         float scale;                    /* scale for each channel */
85         char type_spec[MAX_TYPE_SPEC_LEN]; /* From driver; ex: le:u10/16>>0 */
86         struct datum_info_t type_info;     /* Decoded contents of type spec */
87         float opt_scale; /* Optional correction scale read from a property such
88                           * as iio.accel.x.scale, allowing late compensation of
89                           * problems such as misconfigured axes ; set to 1 by
90                           * default. Applied at the end of the scaling process.
91                           */
92 };
93
94 struct sample_ops_t
95 {
96         /* Conversion function called once per channel */
97         float (*transform)(int s, int c, unsigned char* sample_data);
98
99         /* Function called once per sample */
100         int (*finalize)(int s, struct sensors_event_t* data);
101 };
102
103 /*
104  * Whenever we have sensor data recorded for a sensor in the associated
105  * sensor_info cell, its report_pending field is set to a non-zero value
106  * indicating how we got this data.
107  */
108 #define DATA_TRIGGER    1       /* From /dev/iio:device fd              */
109 #define DATA_SYSFS      2       /* Through polling                      */
110 #define DATA_DUPLICATE  3       /* Duplicate of triggered motion sample */
111
112 struct sensor_info_t
113 {
114         char friendly_name[MAX_NAME_SIZE];      /* ex: Accelerometer         */
115         char internal_name[MAX_NAME_SIZE];      /* ex: accel_3d              */
116         char vendor_name[MAX_NAME_SIZE];        /* ex: Intel                 */
117         char init_trigger_name[MAX_NAME_SIZE];  /* ex: accel-name-dev1       */
118         char motion_trigger_name[MAX_NAME_SIZE];/* ex: accel-any-motion-dev1 */
119         float max_range;
120         float resolution;
121         float power;
122
123         /*
124          * Currently active trigger - either a pointer to the initial (default)
125          * trigger name buffer, or a pointer to the motion trigger name buffer,
126          * or something else (typically NULL or a pointer to some static "\n".
127          * This is used to determine if the conditions are met to switch from
128          * the default trigger to the motion trigger for a sensor, or rather for
129          * the interrupt-driven sensors associated to a given iio device.
130          */
131         const char* selected_trigger;
132
133         float offset;   /* (cooked = raw + offset) * scale */
134         float scale;    /*default: 1. when set to 0, use channel specific value*/
135         float illumincalib;     /* to set the calibration for the ALS */
136
137         float sampling_rate;    /* requested events / second */
138
139         int dev_num;    /* Associated iio dev num, ex: 3 for /dev/iio:device3 */
140         int enable_count;
141
142         int catalog_index;/* Associated entry within the sensor_catalog array */
143         int type;         /* Sensor type, such as SENSOR_TYPE_GYROSCOPE       */
144
145         int num_channels; /* Actual channel count ; 0 for poll mode sensors */
146
147         /*
148          * The array below indicates where to gather report data for this
149          * sensor inside the reports that we read from the iio character device.
150          * It is updated whenever channels are enabled or disabled on the same
151          * device. Channel size indicates the size in bytes of fields, and
152          * should be zero for disabled channels. The type field indicates how a
153          * specific channel data item is structured.
154          */
155         struct channel_info_t channel[MAX_CHANNELS];
156
157         /*
158          * This flag is set if we acquired data from the sensor but did not
159          * forward it to upper layers (i.e. Android) yet. If so, report_buffer
160          * contains that data. Valid values are 0: empty, 1: normal, 2: repeat
161          * of last acquired value after timeout.
162          */
163         int report_pending;
164
165         /*
166          * This flag is set if we have a meta data event pending
167          */
168         volatile int meta_data_pending;
169
170         /*
171          * Timestamp closely matching the date of sampling, preferably retrieved
172          * from a iio channel alongside sample data. Value zero indicates that
173          * we couldn't get such a closely correlated timestamp, and that one
174          * has to be generated before the report gets sent up to Android.
175          */
176         int64_t report_ts;
177
178         /* Buffer containing the last generated sensor report for this sensor */
179         unsigned char report_buffer[MAX_SENSOR_REPORT_SIZE];
180
181         /* Whether or not the above buffer contains data from a device report */
182         int report_initialized;
183
184         struct sample_ops_t ops;
185
186         int cal_level; /* 0 means not calibrated */
187         void* cal_data;
188
189         /* Filtering data for noisy sensors */
190         void* filter;
191
192         float prev_val; /* Previously reported value, for on-change sensors */
193
194         /*
195          * Certain sensors expose their readings through sysfs files that have
196          * a long response time (100-200 ms for ALS). Rather than block our
197          * global control loop for several hundred ms each second, offload those
198          * lengthy blocking reads to dedicated threads, which will then report
199          * their data through a fd that we can add to our poll fd set.
200          */
201         int thread_data_fd[2];
202         pthread_t acquisition_thread;
203
204         /* For cal-uncal sensor pairs - index to the pair sensor in sensor_info */
205         int pair_idx;
206
207         uint32_t quirks; /* Bit mask expressing the need for special tweaks */
208
209         /* Note: we may have to explicitely serialize access to some fields */
210
211
212         /*
213          * If the QUIRK_FIELD_ORDERING bit is set in quirks, the contents of
214          * this array are used in the finalization stage to swap sample fields
215          * before transmitting them to Android ; they form a mapping between
216          * the indices of the input and output arrays: ex: 0123 is identity for
217          * a sample containing 4 fields.
218          */
219         unsigned char order[MAX_CHANNELS];
220
221         /* A few variables used for data filtering */
222         float *history;         /* Working buffer containing recorded samples */
223         float *history_sum;     /* The current sum of the history elements    */
224         int history_size;       /* Number of recorded samples                 */
225         int history_entries;    /* How many of these are initialized          */
226         int history_index;      /* Index of sample to evict next time         */
227
228         /*
229          * Event counter - will be used to check if we have a significant sample
230          * for noisy sensors. We want to make sure we do not send any wrong
231          * events before filtering kicks in. We can also use it for statistics.
232          */
233         uint64_t event_count;
234 };
235
236 /* Reference a few commonly used variables... */
237 extern int                              sensor_count;
238 extern struct sensor_t      sensor_desc[MAX_SENSORS];
239 extern struct sensor_info_t             sensor_info[MAX_SENSORS];
240 extern struct sensor_catalog_entry_t    sensor_catalog[];
241
242 /* We are required to be in sync with SystemClock.getNanos */
243 extern int64_t ts_delta;
244
245 #endif