1/*
2 * Copyright 2020, J��r��me Duval, jerome.duval@gmail.com.
3 * Copyright 2008-2011, Michael Lotz <mmlr@mlotz.ch>
4 * Copyright 2023 Vladimir Serbinenko <phcoder@gmail.com>
5 * Distributed under the terms of the MIT license.
6 */
7#ifndef I2C_ELAN_DEVICE_H
8#define I2C_ELAN_DEVICE_H
9
10
11#include <condition_variable.h>
12#include <i2c.h>
13#include <keyboard_mouse_driver.h>
14#include "I2CHIDProtocol.h"
15
16#define TRANSFER_BUFFER_SIZE 48
17
18class ELANDevice {
19public:
20								ELANDevice(device_node* parent, i2c_device_interface* i2c,
21									i2c_device i2cCookie);
22								~ELANDevice();
23
24			status_t			InitCheck() const { return fStatus; }
25
26			bool				IsOpen() const { return fOpenCount > 0; }
27			status_t			Open(uint32 flags);
28			status_t			Close();
29			int32				OpenCount() const { return fOpenCount; }
30
31			void				Removed();
32			bool				IsRemoved() const { return fRemoved; }
33
34			void				SetPublishPath(char *publishPath);
35			const char *			PublishPath() { return fPublishPath; }
36			status_t			Control(uint32 op, void *buffer, size_t length);
37			device_node*			Parent() { return fParent; }
38private:
39	static	void				_TransferCallback(void *cookie,
40									status_t status, void *data,
41									size_t actualLength);
42	static	void				_UnstallCallback(void *cookie,
43									status_t status, void *data,
44									size_t actualLength);
45
46			status_t			_MaybeScheduleTransfer(int type, int id, int reportSize);
47			status_t			_Reset();
48			status_t			_SetPower(uint8 power);
49			status_t			_FetchBuffer(uint8* cmd, size_t cmdLength,
50									void* buffer, size_t bufferLength);
51			status_t			_FetchReport(uint8 type, uint8 id,
52									size_t reportSize);
53			status_t			_ExecCommand(i2c_op op, uint8* cmd,
54									size_t cmdLength, void* buffer,
55									size_t bufferLength);
56			void				_SetReport(status_t status, uint8 *report, size_t length);
57			status_t			_ReadAndParseReport(touchpad_movement *buffer,
58									bigtime_t timeout, int& zero_report_count);
59			status_t			_WaitForReport(bigtime_t timeout);
60			status_t			_ReadRegister(uint16_t reg, size_t len, void *val);
61			status_t			_WriteRegister(uint16_t reg, uint16_t val);
62			status_t			_SetAbsoluteMode(bool enable);
63private:
64			status_t			fStatus;
65
66			bigtime_t			fTransferLastschedule;
67			int32				fTransferScheduled;
68			uint8				fTransferBuffer[TRANSFER_BUFFER_SIZE];
69
70			int32				fOpenCount;
71			bool				fRemoved;
72
73			char *				fPublishPath;
74
75			uint8				fReportID;
76			bool				fHighPrecision;
77
78			i2c_hid_descriptor		fDescriptor;
79
80			device_node*			fParent;
81
82			i2c_device_interface*		fI2C;
83			i2c_device			fI2CCookie;
84
85			uint32				fLastButtons;
86			uint32				fClickCount;
87			bigtime_t			fLastClickTime;
88			bigtime_t			fClickSpeed;
89
90			status_t			fReportStatus;
91			uint8				fCurrentReport[TRANSFER_BUFFER_SIZE];
92			int				fCurrentReportLength;
93			int32				fBusyCount;
94
95			ConditionVariable		fConditionVariable;
96
97			touchpad_specs			fHardwareSpecs;
98};
99
100
101#endif // I2C_ELAN_DEVICE_H
102