OSDN Git Service

Merge remote-tracking branch 'origin/abt/topic/gmin/l-dev/sensors/master' into gmin...
[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 ARRAY_SIZE(x) sizeof(x)/sizeof(x[0])
36
37 #ifdef __LP64__
38         typedef uint64_t flag_t;
39 #else
40         typedef uint32_t flag_t;
41 #endif
42
43 struct channel_descriptor_t
44 {
45         const char *name;       /* channel name ; ex: x */
46
47         /* sysfs entries located under scan_elements */
48         const char *en_path;    /* Enabled sysfs file name ; ex: "in_temp_en" */
49         const char *type_path;  /* _type sysfs file name  */
50         const char *index_path; /* _index sysfs file name */
51
52         /* sysfs entries located in /sys/bus/iio/devices/iio:deviceX */
53         const char *raw_path;   /* _raw sysfs file name  */
54         const char *input_path; /* _input sysfs file name */
55         const char *scale_path; /* _scale sysfs file name */
56 };
57
58 struct sensor_catalog_entry_t
59 {
60         const char *tag; /* Prefix such as "accel", "gyro", "temp"... */
61         const int type;  /* Sensor type ; ex: SENSOR_TYPE_ACCELEROMETER */
62         const int num_channels; /* Expected iio channels for this sensor */
63         struct channel_descriptor_t channel[MAX_CHANNELS];
64 };
65
66 struct datum_info_t
67 {
68         char sign;
69         char endianness;
70         short realbits;
71         short storagebits;
72         short shift;
73 };
74
75 struct channel_info_t
76 {
77         int offset; /* Offset in bytes within the iio character device report */
78         int size;                       /* Field size in bytes */
79         float scale;                    /* scale for each channel */
80         char type_spec[MAX_TYPE_SPEC_LEN]; /* From driver; ex: le:u10/16>>0 */
81         struct datum_info_t type_info;     /* Decoded contents of type spec */
82         float opt_scale; /* Optional correction scale read from a property such
83                           * as iio.accel.x.scale, allowing late compensation of
84                           * problems such as misconfigured axes ; set to 1 by
85                           * default. Applied at the end of the scaling process.
86                           */
87 };
88
89 struct sample_ops_t
90 {
91         /* Conversion function called once per channel */
92         float (*transform)(int s, int c, unsigned char* sample_data);
93
94         /* Function called once per sample */
95         int (*finalize)(int s, struct sensors_event_t* data);
96 };
97
98 struct sensor_info_t
99 {
100         char friendly_name[MAX_NAME_SIZE];      /* ex: Accelerometer */
101         char internal_name[MAX_NAME_SIZE];      /* ex: accel_3d */
102         char vendor_name[MAX_NAME_SIZE];        /* ex: Intel */
103         char trigger_name[MAX_NAME_SIZE];       /* ex: accel-name-dev1 */
104         float max_range;
105         float resolution;
106         float power;
107
108         float offset;   /* (cooked = raw + offset) * scale */
109         float scale;    /*default: 1. when set to 0, use channel specific value*/
110         float illumincalib;     /* to set the calibration for the ALS */
111
112         float sampling_rate;    /* requested events / second */
113
114         int dev_num;    /* Associated iio dev num, ex: 3 for /dev/iio:device3 */
115         int enable_count;
116
117         int catalog_index;/* Associated entry within the sensor_catalog array */
118
119         int num_channels; /* Actual channel count ; 0 for poll mode sensors */
120
121         /*
122          * The array below indicates where to gather report data for this
123          * sensor inside the reports that we read from the iio character device.
124          * It is updated whenever channels are enabled or disabled on the same
125          * device. Channel size indicates the size in bytes of fields, and
126          * should be zero for disabled channels. The type field indicates how a
127          * specific channel data item is structured.
128          */
129         struct channel_info_t channel[MAX_CHANNELS];
130
131         /*
132          * This flag is set if we acquired data from the sensor but did not
133          * forward it to upper layers (i.e. Android) yet. If so, report_buffer
134          * contains that data.
135          */
136         int report_pending;
137
138         /*
139          * Timestamp closely matching the date of sampling, preferably retrieved
140          * from a iio channel alongside sample data. Value zero indicates that
141          * we couldn't get such a closely correlated timestamp, and that one
142          * has to be generated before the report gets sent up to Android.
143          */
144         int64_t report_ts;
145
146         /* Buffer containing the last generated sensor report for this sensor */
147         unsigned char report_buffer[MAX_SENSOR_REPORT_SIZE];
148
149         /* Whether or not the above buffer contains data from a device report */
150         int report_initialized;
151
152         struct sample_ops_t ops;
153
154         int calibrated;
155         void* cal_data;
156
157         float prev_val; /* Previously reported value, for on-change sensors */
158
159         /*
160          * Certain sensors expose their readings through sysfs files that have
161          * a long response time (100-200 ms for ALS). Rather than block our
162          * global control loop for several hundred ms each second, offload those
163          * lengthy blocking reads to dedicated threads, which will then report
164          * their data through a fd that we can add to our poll fd set.
165          */
166         int thread_data_fd[2];
167         pthread_t acquisition_thread;
168
169         /* For cal-uncal sensor pairs - index to the pair sensor in sensor_info */
170         int pair_idx;
171
172         uint32_t quirks; /* Bit mask expressing the need for special tweaks */
173
174         /* Note: we may have to explicitely serialize access to some fields */
175
176
177         /*
178          * If the QUIRK_FIELD_ORDERING bit is set in quirks, the contents of
179          * this array are used in the finalization stage to swap sample fields
180          * before transmitting them to Android ; they form a mapping between
181          * the indices of the input and output arrays: ex: 0123 is identity for
182          * a sample containing 4 fields.
183          */
184         unsigned char order[MAX_CHANNELS];
185 };
186
187 /* Reference a few commonly used variables... */
188 extern int                              sensor_count;
189 extern struct sensor_info_t             sensor_info[MAX_SENSORS];
190 extern struct sensor_catalog_entry_t    sensor_catalog[];
191
192 #endif