11aaa1f99211e159993f736c716a15a799f375502Gwendal Grignou/*
21aaa1f99211e159993f736c716a15a799f375502Gwendal Grignou * Copyright (C) 2008-2014 The Android Open Source Project
31aaa1f99211e159993f736c716a15a799f375502Gwendal Grignou *
41aaa1f99211e159993f736c716a15a799f375502Gwendal Grignou * Licensed under the Apache License, Version 2.0 (the "License");
51aaa1f99211e159993f736c716a15a799f375502Gwendal Grignou * you may not use this file except in compliance with the License.
61aaa1f99211e159993f736c716a15a799f375502Gwendal Grignou * You may obtain a copy of the License at
71aaa1f99211e159993f736c716a15a799f375502Gwendal Grignou *
81aaa1f99211e159993f736c716a15a799f375502Gwendal Grignou *      http://www.apache.org/licenses/LICENSE-2.0
91aaa1f99211e159993f736c716a15a799f375502Gwendal Grignou *
101aaa1f99211e159993f736c716a15a799f375502Gwendal Grignou * Unless required by applicable law or agreed to in writing, software
111aaa1f99211e159993f736c716a15a799f375502Gwendal Grignou * distributed under the License is distributed on an "AS IS" BASIS,
121aaa1f99211e159993f736c716a15a799f375502Gwendal Grignou * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
131aaa1f99211e159993f736c716a15a799f375502Gwendal Grignou * See the License for the specific language governing permissions and
141aaa1f99211e159993f736c716a15a799f375502Gwendal Grignou * limitations under the License.
151aaa1f99211e159993f736c716a15a799f375502Gwendal Grignou */
161aaa1f99211e159993f736c716a15a799f375502Gwendal Grignou
171aaa1f99211e159993f736c716a15a799f375502Gwendal Grignou#ifndef CROS_EC_SENSORS_H
181aaa1f99211e159993f736c716a15a799f375502Gwendal Grignou#define CROS_EC_SENSORS_H
191aaa1f99211e159993f736c716a15a799f375502Gwendal Grignou
201aaa1f99211e159993f736c716a15a799f375502Gwendal Grignou#include <errno.h>
211aaa1f99211e159993f736c716a15a799f375502Gwendal Grignou#include <stdint.h>
221aaa1f99211e159993f736c716a15a799f375502Gwendal Grignou#include <sys/cdefs.h>
231aaa1f99211e159993f736c716a15a799f375502Gwendal Grignou#include <sys/types.h>
241aaa1f99211e159993f736c716a15a799f375502Gwendal Grignou#include <utils/BitSet.h>
251aaa1f99211e159993f736c716a15a799f375502Gwendal Grignou
261aaa1f99211e159993f736c716a15a799f375502Gwendal Grignou#include <hardware/sensors.h>
271aaa1f99211e159993f736c716a15a799f375502Gwendal Grignou
281aaa1f99211e159993f736c716a15a799f375502Gwendal Grignou#define IIO_DIR  "/sys/bus/iio/devices/"
291aaa1f99211e159993f736c716a15a799f375502Gwendal Grignou#define IIO_MAX_NAME_LENGTH 30
3099740606f7dddd715dc16d2f0f1cb2f0570d59ffGwendal Grignou#define IIO_MAX_BUFF_SIZE 4096
318941a90a8967dfa9f824434d97b3e6f315c26154Gwendal Grignou#define INT32_CHAR_LEN 12
328941a90a8967dfa9f824434d97b3e6f315c26154Gwendal Grignou
33369babf60a2aeb91e17f4e9429ea8f7bba02c323George Burgess IV/*
34369babf60a2aeb91e17f4e9429ea8f7bba02c323George Burgess IV * Use sizeof(...) - 1 instead of strlen because clang FORTIFY makes strlen
35369babf60a2aeb91e17f4e9429ea8f7bba02c323George Burgess IV * non-constant.
36369babf60a2aeb91e17f4e9429ea8f7bba02c323George Burgess IV */
37369babf60a2aeb91e17f4e9429ea8f7bba02c323George Burgess IV#define IIO_MAX_DEVICE_NAME_LENGTH (sizeof(IIO_DIR) - 1 + IIO_MAX_NAME_LENGTH)
381aaa1f99211e159993f736c716a15a799f375502Gwendal Grignou
3999740606f7dddd715dc16d2f0f1cb2f0570d59ffGwendal Grignou#define CROS_EC_MAX_SAMPLING_PERIOD ((1 << 16) - 2)
4099740606f7dddd715dc16d2f0f1cb2f0570d59ffGwendal Grignou
411aaa1f99211e159993f736c716a15a799f375502Gwendal Grignouenum {X, Y, Z, MAX_AXIS};
421aaa1f99211e159993f736c716a15a799f375502Gwendal Grignou
431aaa1f99211e159993f736c716a15a799f375502Gwendal Grignouextern const char *cros_ec_sensor_names[];
441aaa1f99211e159993f736c716a15a799f375502Gwendal Grignou
45218d7b410aa0ea3933e9ce45ed9e5518fb511cf2Gwendal Grignou#define CROS_EC_EVENT_FLUSH_FLAG 0x1
46218d7b410aa0ea3933e9ce45ed9e5518fb511cf2Gwendal Grignou#define CROS_EC_EVENT_WAKEUP_FLAG 0x2
47218d7b410aa0ea3933e9ce45ed9e5518fb511cf2Gwendal Grignou
48218d7b410aa0ea3933e9ce45ed9e5518fb511cf2Gwendal Grignou#define CROS_EC_MAX_PHYSICAL_SENSOR 256
49218d7b410aa0ea3933e9ce45ed9e5518fb511cf2Gwendal Grignou
50218d7b410aa0ea3933e9ce45ed9e5518fb511cf2Gwendal Grignouenum cros_ec_gesture {
51218d7b410aa0ea3933e9ce45ed9e5518fb511cf2Gwendal Grignou    CROS_EC_SIGMO,
52218d7b410aa0ea3933e9ce45ed9e5518fb511cf2Gwendal Grignou    CROS_EC_MAX_GESTURE,
53218d7b410aa0ea3933e9ce45ed9e5518fb511cf2Gwendal Grignou};
54218d7b410aa0ea3933e9ce45ed9e5518fb511cf2Gwendal Grignou
55218d7b410aa0ea3933e9ce45ed9e5518fb511cf2Gwendal Grignou
56218d7b410aa0ea3933e9ce45ed9e5518fb511cf2Gwendal Grignou/*****************************************************************************/
57218d7b410aa0ea3933e9ce45ed9e5518fb511cf2Gwendal Grignou/* from ec_commands.h */
581aaa1f99211e159993f736c716a15a799f375502Gwendal Grignoustruct cros_ec_event {
591aaa1f99211e159993f736c716a15a799f375502Gwendal Grignou    uint8_t sensor_id;
601aaa1f99211e159993f736c716a15a799f375502Gwendal Grignou    uint8_t flags;
61218d7b410aa0ea3933e9ce45ed9e5518fb511cf2Gwendal Grignou    union {
62218d7b410aa0ea3933e9ce45ed9e5518fb511cf2Gwendal Grignou        int16_t vector[MAX_AXIS];
63218d7b410aa0ea3933e9ce45ed9e5518fb511cf2Gwendal Grignou        struct {
64218d7b410aa0ea3933e9ce45ed9e5518fb511cf2Gwendal Grignou            uint8_t activity;
65218d7b410aa0ea3933e9ce45ed9e5518fb511cf2Gwendal Grignou            uint8_t state;
66218d7b410aa0ea3933e9ce45ed9e5518fb511cf2Gwendal Grignou            uint16_t add_info[2];
67218d7b410aa0ea3933e9ce45ed9e5518fb511cf2Gwendal Grignou        };
68218d7b410aa0ea3933e9ce45ed9e5518fb511cf2Gwendal Grignou    };
691aaa1f99211e159993f736c716a15a799f375502Gwendal Grignou    uint64_t timestamp;
701aaa1f99211e159993f736c716a15a799f375502Gwendal Grignou} __packed;
711aaa1f99211e159993f736c716a15a799f375502Gwendal Grignou
72218d7b410aa0ea3933e9ce45ed9e5518fb511cf2Gwendal Grignouenum motionsensor_activity {
73218d7b410aa0ea3933e9ce45ed9e5518fb511cf2Gwendal Grignou    MOTIONSENSE_ACTIVITY_RESERVED = 0,
74218d7b410aa0ea3933e9ce45ed9e5518fb511cf2Gwendal Grignou    MOTIONSENSE_ACTIVITY_SIG_MOTION = 1,
75218d7b410aa0ea3933e9ce45ed9e5518fb511cf2Gwendal Grignou    MOTIONSENSE_MAX_ACTIVITY,
76218d7b410aa0ea3933e9ce45ed9e5518fb511cf2Gwendal Grignou};
77218d7b410aa0ea3933e9ce45ed9e5518fb511cf2Gwendal Grignou
78218d7b410aa0ea3933e9ce45ed9e5518fb511cf2Gwendal Grignou
79218d7b410aa0ea3933e9ce45ed9e5518fb511cf2Gwendal Grignou/*****************************************************************************/
80218d7b410aa0ea3933e9ce45ed9e5518fb511cf2Gwendal Grignou
81218d7b410aa0ea3933e9ce45ed9e5518fb511cf2Gwendal Grignouenum cros_ec_sensor_device {
82218d7b410aa0ea3933e9ce45ed9e5518fb511cf2Gwendal Grignou    CROS_EC_ACCEL,
83218d7b410aa0ea3933e9ce45ed9e5518fb511cf2Gwendal Grignou    CROS_EC_GYRO,
84218d7b410aa0ea3933e9ce45ed9e5518fb511cf2Gwendal Grignou    CROS_EC_MAG,
85218d7b410aa0ea3933e9ce45ed9e5518fb511cf2Gwendal Grignou    CROS_EC_PROX,
86218d7b410aa0ea3933e9ce45ed9e5518fb511cf2Gwendal Grignou    CROS_EC_LIGHT,
87218d7b410aa0ea3933e9ce45ed9e5518fb511cf2Gwendal Grignou    CROS_EC_ACTIVITY,
88218d7b410aa0ea3933e9ce45ed9e5518fb511cf2Gwendal Grignou    CROS_EC_RING, /* should be the last device */
89218d7b410aa0ea3933e9ce45ed9e5518fb511cf2Gwendal Grignou    CROS_EC_MAX_DEVICE,
90218d7b410aa0ea3933e9ce45ed9e5518fb511cf2Gwendal Grignou};
911aaa1f99211e159993f736c716a15a799f375502Gwendal Grignou
921aaa1f99211e159993f736c716a15a799f375502Gwendal Grignoustruct cros_ec_sensor_info {
931aaa1f99211e159993f736c716a15a799f375502Gwendal Grignou    /* description of the sensor, as reported to sensorservice. */
941aaa1f99211e159993f736c716a15a799f375502Gwendal Grignou    sensor_t sensor_data;
95218d7b410aa0ea3933e9ce45ed9e5518fb511cf2Gwendal Grignou    enum cros_ec_sensor_device type;
961aaa1f99211e159993f736c716a15a799f375502Gwendal Grignou    const char *device_name;
971aaa1f99211e159993f736c716a15a799f375502Gwendal Grignou    int64_t sampling_period_ns;
981aaa1f99211e159993f736c716a15a799f375502Gwendal Grignou    int64_t max_report_latency_ns;
991aaa1f99211e159993f736c716a15a799f375502Gwendal Grignou    bool enabled;
1001aaa1f99211e159993f736c716a15a799f375502Gwendal Grignou};
1011aaa1f99211e159993f736c716a15a799f375502Gwendal Grignou
102218d7b410aa0ea3933e9ce45ed9e5518fb511cf2Gwendal Grignoustruct cros_ec_gesture_info {
103218d7b410aa0ea3933e9ce45ed9e5518fb511cf2Gwendal Grignou    /* For activities managed by the sensor interface */
104218d7b410aa0ea3933e9ce45ed9e5518fb511cf2Gwendal Grignou    sensor_t sensor_data;
105218d7b410aa0ea3933e9ce45ed9e5518fb511cf2Gwendal Grignou    const char *device_name;
106218d7b410aa0ea3933e9ce45ed9e5518fb511cf2Gwendal Grignou    const char *enable_entry;
107218d7b410aa0ea3933e9ce45ed9e5518fb511cf2Gwendal Grignou    bool enabled;
108218d7b410aa0ea3933e9ce45ed9e5518fb511cf2Gwendal Grignou};
109218d7b410aa0ea3933e9ce45ed9e5518fb511cf2Gwendal Grignou
110da6a4f85b1fff0f447be7cc4f9fb6760f076d10eGwendal Grignou/*
111da6a4f85b1fff0f447be7cc4f9fb6760f076d10eGwendal Grignou * To write sysfs parameters: IIO_DIR is appended before path.
112da6a4f85b1fff0f447be7cc4f9fb6760f076d10eGwendal Grignou */
113da6a4f85b1fff0f447be7cc4f9fb6760f076d10eGwendal Grignouint cros_ec_sysfs_set_input_attr(const char *path, const char *attr, const char *value, size_t len);
114da6a4f85b1fff0f447be7cc4f9fb6760f076d10eGwendal Grignouint cros_ec_sysfs_set_input_attr_by_int(const char *path, const char *attr, int value);
115da6a4f85b1fff0f447be7cc4f9fb6760f076d10eGwendal Grignou
116da6a4f85b1fff0f447be7cc4f9fb6760f076d10eGwendal Grignou/*
117da6a4f85b1fff0f447be7cc4f9fb6760f076d10eGwendal Grignou * To read sysfs parameters: IIO_DIR is NOT appended.
118da6a4f85b1fff0f447be7cc4f9fb6760f076d10eGwendal Grignou */
119da6a4f85b1fff0f447be7cc4f9fb6760f076d10eGwendal Grignouint cros_ec_sysfs_get_attr(const char *path, const char *attr, char *output);
120da6a4f85b1fff0f447be7cc4f9fb6760f076d10eGwendal Grignou
1211aaa1f99211e159993f736c716a15a799f375502Gwendal Grignouclass CrosECSensor {
1221aaa1f99211e159993f736c716a15a799f375502Gwendal Grignou    struct cros_ec_sensor_info *mSensorInfo;
1237aac29a768e346e1d2e3c03e917befc48f3009f8Gwendal Grignou    size_t mSensorNb;
124218d7b410aa0ea3933e9ce45ed9e5518fb511cf2Gwendal Grignou    struct cros_ec_gesture_info *mGestureInfo;
1257aac29a768e346e1d2e3c03e917befc48f3009f8Gwendal Grignou    size_t mGestureNb;
1261aaa1f99211e159993f736c716a15a799f375502Gwendal Grignou    char mRingPath[IIO_MAX_DEVICE_NAME_LENGTH];
1278941a90a8967dfa9f824434d97b3e6f315c26154Gwendal Grignou    cros_ec_event mEvents[IIO_MAX_BUFF_SIZE];
1281aaa1f99211e159993f736c716a15a799f375502Gwendal Grignou    int mDataFd;
1291aaa1f99211e159993f736c716a15a799f375502Gwendal Grignou
130a2267a8787b0cb8966e37b4bc73a98a68c3dc032Gwendal Grignou    int processEvent(sensors_event_t* data, const cros_ec_event *event);
1311aaa1f99211e159993f736c716a15a799f375502Gwendal Grignoupublic:
132218d7b410aa0ea3933e9ce45ed9e5518fb511cf2Gwendal Grignou    CrosECSensor(
133218d7b410aa0ea3933e9ce45ed9e5518fb511cf2Gwendal Grignou        struct cros_ec_sensor_info *sensor_info,
1347aac29a768e346e1d2e3c03e917befc48f3009f8Gwendal Grignou        size_t sensor_nb,
135218d7b410aa0ea3933e9ce45ed9e5518fb511cf2Gwendal Grignou        struct cros_ec_gesture_info *gesture_info,
1367aac29a768e346e1d2e3c03e917befc48f3009f8Gwendal Grignou        size_t gesture_nb,
1371aaa1f99211e159993f736c716a15a799f375502Gwendal Grignou        const char *ring_device_name,
1381aaa1f99211e159993f736c716a15a799f375502Gwendal Grignou        const char *trigger_name);
1391aaa1f99211e159993f736c716a15a799f375502Gwendal Grignou    virtual ~CrosECSensor();
1401aaa1f99211e159993f736c716a15a799f375502Gwendal Grignou    virtual int getFd(void);
1411aaa1f99211e159993f736c716a15a799f375502Gwendal Grignou    int readEvents(sensors_event_t* data, int count);
1421aaa1f99211e159993f736c716a15a799f375502Gwendal Grignou
1431aaa1f99211e159993f736c716a15a799f375502Gwendal Grignou    virtual int activate(int handle, int enabled);
1441aaa1f99211e159993f736c716a15a799f375502Gwendal Grignou    virtual int batch(int handle, int64_t period_ns, int64_t timeout);
1451aaa1f99211e159993f736c716a15a799f375502Gwendal Grignou    virtual int flush(int handle);
1461aaa1f99211e159993f736c716a15a799f375502Gwendal Grignou};
1471aaa1f99211e159993f736c716a15a799f375502Gwendal Grignou
1481aaa1f99211e159993f736c716a15a799f375502Gwendal Grignou#endif  // CROS_EC_SENSORS_H
149