aboutsummaryrefslogtreecommitdiffstats
path: root/include/linux/timer.h
blob: 38cf093ef62c745d9f06e1038127ef61834796a1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#ifndef _LINUX_TIMER_H
#define _LINUX_TIMER_H

#include <linux/list.h>
#include <linux/ktime.h>
#include <linux/stddef.h>
#include <linux/debugobjects.h>
#include <linux/stringify.h>

struct tvec_base;

struct timer_list {
	/*
	 * All fields that change during normal runtime grouped to the
	 * same cacheline
	 */
	struct list_head entry;
	unsigned long expires;
	struct tvec_base *base;

	void (*function)(unsigned long);
	unsigned long data;

	int slack;

#ifdef CONFIG_TIMER_STATS
	void *start_site;
	char start_comm[16];
	int start_pid;
#endif
#ifdef CONFIG_LOCKDEP
	struct lockdep_map lockdep_map;
#endif
};

extern struct tvec_base boot_tvec_bases;

#ifdef CONFIG_LOCKDEP
/*
 * NB: because we have to copy the lockdep_map, setting the lockdep_map key
 * (second argument) here is required, otherwise it could be initialised to
 * the copy of the lockdep_map later! We use the pointer to and the string
 * "<file>:<line>" as the key resp. the name of the lockdep_map.
 */
#define __TIMER_LOCKDEP_MAP_INITIALIZER(_kn)				\
	.lockdep_map = STATIC_LOCKDEP_MAP_INIT(_kn, &_kn),
#else
#define __TIMER_LOCKDEP_MAP_INITIALIZER(_kn)
#endif

#define TIMER_INITIALIZER(_function, _expires, _data) {		\
		.entry = { .prev = TIMER_ENTRY_STATIC },	\
		.function = (_function),			\
		.expires = (_expires),				\
		.data = (_data),				\
		.base = &boot_tvec_bases,			\
		__TIMER_LOCKDEP_MAP_INITIALIZER(		\
			__FILE__ ":" __stringify(__LINE__))	\
	}

#define DEFINE_TIMER(_name, _function, _expires, _data)		\
	struct timer_list _name =				\
		TIMER_INITIALIZER(_function, _expires, _data)

void init_timer_key(struct timer_list *timer,
		    const char *name,
		    struct lock_class_key *key);
void init_timer_deferrable_key(struct timer_list *timer,
			       const char *name,
			       struct lock_class_key *key);

#ifdef CONFIG_LOCKDEP
#define init_timer(timer)						\
	do {								\
		static struct lock_class_key __key;			\
		init_timer_key((timer), #timer, &__key);		\
	} while (0)

#define init_timer_deferrable(timer)					\
	do {								\
		static struct lock_class_key __key;			\
		init_timer_deferrable_key((timer), #timer, &__key);	\
	} while (0)

#define init_timer_on_stack(timer)					\
	do {								\
		static struct lock_class_key __key;			\
		init_timer_on_stack_key((timer), #timer, &__key);	\
	} while (0)

#define setup_timer(timer, fn, data)					\
	do {								\
		static struct lock_class_key __key;			\
		setup_timer_key((timer), #timer, &__key, (fn), (data));\
	} while (0)

#define setup_timer_on_stack(timer, fn, data)				\
	do {								\
		static struct lock_class_key __key;			\
		setup_timer_on_stack_key((timer), #timer, &__key,	\
					 (fn), (data));			\
	} while (0)
#define setup_deferrable_timer_on_stack(timer, fn, data)		\
	do {								\
		static struct lock_class_key __key;			\
		setup_deferrable_timer_on_stack_key((timer), #timer,	\
						    &__key, (fn),	\
						    (data));		\
	} while (0)
#else
#define init_timer(timer)\
	init_timer_key((timer), NULL, NULL)
#define init_timer_deferrable(timer)\
	init_timer_deferrable_key((timer), NULL, NULL)
#define init_timer_on_stack(timer)\
	init_timer_on_stack_key((timer), NULL, NULL)
#define setup_timer(timer, fn, data)\
	setup_timer_key((timer), NULL, NULL, (fn), (data))
#define setup_timer_on_stack(timer, fn, data)\
	setup_timer_on_stack_key((timer), NULL, NULL, (fn), (data))
#define setup_deferrable_timer_on_stack(timer, fn, data)\
	setup_deferrable_timer_on_stack_key((timer), NULL, NULL, (fn), (data))
#endif

#ifdef CONFIG_DEBUG_OBJECTS_TIMERS
extern void init_timer_on_stack_key(struct timer_list *timer,
				    const char *name,
				    struct lock_class_key *key);
extern void destroy_timer_on_stack(struct timer_list *timer);
#else
static inline void destroy_timer_on_stack(struct timer_list *timer) { }
static inline void init_timer_on_stack_key(struct timer_list *timer,
					   const char *name,
					   struct lock_class_key *key)
{
	init_timer_key(timer, name, key);
}
#endif

static inline void setup_timer_key(struct timer_list * timer,
				const char *name,
				struct lock_class_key *key,
				void (*function)(unsigned long),
				unsigned long data)
{
	timer->function = function;
	timer->data = data;
	init_timer_key(timer, name, key);
}

static inline void setup_timer_on_stack_key(struct timer_list *timer,
					const char *name,
					struct lock_class_key *key,
					void (*function)(unsigned long),
					unsigned long data)
{
	timer->function = function;
	timer->data = data;
	init_timer_on_stack_key(timer, name, key);
}

extern void setup_deferrable_timer_on_stack_key(struct timer_list *timer,
						const char *name,
						struct lock_class_key *key,
						void (*function)(unsigned long),
						unsigned long data);

/**
 * timer_pending - is a timer pending?
 * @timer: the timer in question
 *
 * timer_pending will tell whether a given timer is currently pending,
 * or not. Callers must ensure serialization wrt. other operations done
 * to this timer, eg. interrupt contexts, or other CPUs on SMP.
 *
 * return value: 1 if the timer is pending, 0 if not.
 */
static inline int timer_pending(const struct timer_list * timer)
{
	return timer->entry.next != NULL;
}

extern void add_timer_on(struct timer_list *timer, int cpu);
extern int del_timer(struct timer_list * timer);
extern int mod_timer(struct timer_list *timer, unsigned long expires);
extern int mod_timer_pending(struct timer_list *timer, unsigned long expires);
extern int mod_timer_pinned(struct timer_list *timer, unsigned long expires);

extern void set_timer_slack(struct timer_list *time, int slack_hz);

#define TIMER_NOT_PINNED	0
#define TIMER_PINNED		1
/*
 * The jiffies value which is added to now, when there is no timer
 * in the timer wheel:
 */
#define NEXT_TIMER_MAX_DELTA	((1UL << 30) - 1)

/*
 * Return when the next timer-wheel timeout occurs (in absolute jiffies),
 * locks the timer base and does the comparison against the given
 * jiffie.
 */
extern unsigned long get_next_timer_interrupt(unsigned long now);

/*
 * Timer-statistics info:
 */
#ifdef CONFIG_TIMER_STATS

extern int timer_stats_active;

#define TIMER_STATS_FLAG_DEFERRABLE	0x1

extern void init_timer_stats(void);

extern void timer_stats_update_stats(void *timer, pid_t pid, void *startf,
				     void *timerf, char *comm,
				     unsigned int timer_flag);

extern void __timer_stats_timer_set_start_info(struct timer_list *timer,
					       void *addr);

static inline void timer_stats_timer_set_start_info(struct timer_list *timer)
{
	if (likely(!timer_stats_active))
		return;
	__timer_stats_timer_set_start_info(timer, __builtin_return_address(0));
}

static inline void timer_stats_timer_clear_start_info(struct timer_list *timer)
{
	timer->start_site = NULL;
}
#else
static inline void init_timer_stats(void)
{
}

static inline void timer_stats_timer_set_start_info(struct timer_list *timer)
{
}

static inline void timer_stats_timer_clear_start_info(struct timer_list *timer)
{
}
#endif

extern void add_timer(struct timer_list *timer);

#ifdef CONFIG_SMP
  extern int try_to_del_timer_sync(struct timer_list *timer);
  extern int del_timer_sync(struct timer_list *timer);
#else
# define try_to_del_timer_sync(t)	del_timer(t)
# define del_timer_sync(t)		del_timer(t)
#endif

#define del_singleshot_timer_sync(t) del_timer_sync(t)

extern void init_timers(void);
extern void run_local_timers(void);
struct hrtimer;
extern enum hrtimer_restart it_real_fn(struct hrtimer *);

unsigned long __round_jiffies(unsigned long j, int cpu);
unsigned long __round_jiffies_relative(unsigned long j, int cpu);
unsigned long round_jiffies(unsigned long j);
unsigned long round_jiffies_relative(unsigned long j);

unsigned long __round_jiffies_up(unsigned long j, int cpu);
unsigned long __round_jiffies_up_relative(unsigned long j, int cpu);
unsigned long round_jiffies_up(unsigned long j);
unsigned long round_jiffies_up_relative(unsigned long j);

#endif
*hdev; }; #define RMI_PAGE(addr) (((addr) >> 8) & 0xff) static int rmi_write_report(struct hid_device *hdev, u8 *report, int len); /** * rmi_set_page - Set RMI page * @hdev: The pointer to the hid_device struct * @page: The new page address. * * RMI devices have 16-bit addressing, but some of the physical * implementations (like SMBus) only have 8-bit addressing. So RMI implements * a page address at 0xff of every page so we can reliable page addresses * every 256 registers. * * The page_mutex lock must be held when this function is entered. * * Returns zero on success, non-zero on failure. */ static int rmi_set_page(struct hid_device *hdev, u8 page) { struct rmi_data *data = hid_get_drvdata(hdev); int retval; data->writeReport[0] = RMI_WRITE_REPORT_ID; data->writeReport[1] = 1; data->writeReport[2] = 0xFF; data->writeReport[4] = page; retval = rmi_write_report(hdev, data->writeReport, data->output_report_size); if (retval != data->output_report_size) { dev_err(&hdev->dev, "%s: set page failed: %d.", __func__, retval); return retval; } data->page = page; return 0; } static int rmi_set_mode(struct hid_device *hdev, u8 mode) { int ret; u8 txbuf[2] = {RMI_SET_RMI_MODE_REPORT_ID, mode}; ret = hid_hw_raw_request(hdev, RMI_SET_RMI_MODE_REPORT_ID, txbuf, sizeof(txbuf), HID_FEATURE_REPORT, HID_REQ_SET_REPORT); if (ret < 0) { dev_err(&hdev->dev, "unable to set rmi mode to %d (%d)\n", mode, ret); return ret; } return 0; } static int rmi_write_report(struct hid_device *hdev, u8 *report, int len) { int ret; ret = hid_hw_output_report(hdev, (void *)report, len); if (ret < 0) { dev_err(&hdev->dev, "failed to write hid report (%d)\n", ret); return ret; } return ret; } static int rmi_read_block(struct hid_device *hdev, u16 addr, void *buf, const int len) { struct rmi_data *data = hid_get_drvdata(hdev); int ret; int bytes_read; int bytes_needed; int retries; int read_input_count; mutex_lock(&data->page_mutex); if (RMI_PAGE(addr) != data->page) { ret = rmi_set_page(hdev, RMI_PAGE(addr)); if (ret < 0) goto exit; } for (retries = 5; retries > 0; retries--) { data->writeReport[0] = RMI_READ_ADDR_REPORT_ID; data->writeReport[1] = 0; /* old 1 byte read count */ data->writeReport[2] = addr & 0xFF; data->writeReport[3] = (addr >> 8) & 0xFF; data->writeReport[4] = len & 0xFF; data->writeReport[5] = (len >> 8) & 0xFF; set_bit(RMI_READ_REQUEST_PENDING, &data->flags); ret = rmi_write_report(hdev, data->writeReport, data->output_report_size); if (ret != data->output_report_size) { clear_bit(RMI_READ_REQUEST_PENDING, &data->flags); dev_err(&hdev->dev, "failed to write request output report (%d)\n", ret); goto exit; } bytes_read = 0; bytes_needed = len; while (bytes_read < len) { if (!wait_event_timeout(data->wait, test_bit(RMI_READ_DATA_PENDING, &data->flags), msecs_to_jiffies(1000))) { hid_warn(hdev, "%s: timeout elapsed\n", __func__); ret = -EAGAIN; break; } read_input_count = data->readReport[1]; memcpy(buf + bytes_read, &data->readReport[2], read_input_count < bytes_needed ? read_input_count : bytes_needed); bytes_read += read_input_count; bytes_needed -= read_input_count; clear_bit(RMI_READ_DATA_PENDING, &data->flags); } if (ret >= 0) { ret = 0; break; } } exit: clear_bit(RMI_READ_REQUEST_PENDING, &data->flags); mutex_unlock(&data->page_mutex); return ret; } static inline int rmi_read(struct hid_device *hdev, u16 addr, void *buf) { return rmi_read_block(hdev, addr, buf, 1); } static void rmi_f11_process_touch(struct rmi_data *hdata, int slot, u8 finger_state, u8 *touch_data) { int x, y, wx, wy; int wide, major, minor; int z; input_mt_slot(hdata->input, slot); input_mt_report_slot_state(hdata->input, MT_TOOL_FINGER, finger_state == 0x01); if (finger_state == 0x01) { x = (touch_data[0] << 4) | (touch_data[2] & 0x0F); y = (touch_data[1] << 4) | (touch_data[2] >> 4); wx = touch_data[3] & 0x0F; wy = touch_data[3] >> 4; wide = (wx > wy); major = max(wx, wy); minor = min(wx, wy); z = touch_data[4]; /* y is inverted */ y = hdata->max_y - y; input_event(hdata->input, EV_ABS, ABS_MT_POSITION_X, x); input_event(hdata->input, EV_ABS, ABS_MT_POSITION_Y, y); input_event(hdata->input, EV_ABS, ABS_MT_ORIENTATION, wide); input_event(hdata->input, EV_ABS, ABS_MT_PRESSURE, z); input_event(hdata->input, EV_ABS, ABS_MT_TOUCH_MAJOR, major); input_event(hdata->input, EV_ABS, ABS_MT_TOUCH_MINOR, minor); } } static void rmi_reset_work(struct work_struct *work) { struct rmi_data *hdata = container_of(work, struct rmi_data, reset_work); /* switch the device to RMI if we receive a generic mouse report */ rmi_set_mode(hdata->hdev, RMI_MODE_ATTN_REPORTS); } static inline int rmi_schedule_reset(struct hid_device *hdev) { struct rmi_data *hdata = hid_get_drvdata(hdev); return schedule_work(&hdata->reset_work); } static int rmi_f11_input_event(struct hid_device *hdev, u8 irq, u8 *data, int size) { struct rmi_data *hdata = hid_get_drvdata(hdev); int offset; int i; if (size < hdata->f11.report_size) return 0; if (!(irq & hdata->f11.irq_mask)) return 0; offset = (hdata->max_fingers >> 2) + 1; for (i = 0; i < hdata->max_fingers; i++) { int fs_byte_position = i >> 2; int fs_bit_position = (i & 0x3) << 1; int finger_state = (data[fs_byte_position] >> fs_bit_position) & 0x03; rmi_f11_process_touch(hdata, i, finger_state, &data[offset + 5 * i]); } input_mt_sync_frame(hdata->input); input_sync(hdata->input); return hdata->f11.report_size; } static int rmi_f30_input_event(struct hid_device *hdev, u8 irq, u8 *data, int size) { struct rmi_data *hdata = hid_get_drvdata(hdev); int i; int button = 0; bool value; if (!(irq & hdata->f30.irq_mask)) return 0; for (i = 0; i < hdata->gpio_led_count; i++) { if (test_bit(i, &hdata->button_mask)) { value = (data[i / 8] >> (i & 0x07)) & BIT(0); if (test_bit(i, &hdata->button_state_mask)) value = !value; input_event(hdata->input, EV_KEY, BTN_LEFT + button++, value); } } return hdata->f30.report_size; } static int rmi_input_event(struct hid_device *hdev, u8 *data, int size) { struct rmi_data *hdata = hid_get_drvdata(hdev); unsigned long irq_mask = 0; unsigned index = 2; if (!(test_bit(RMI_STARTED, &hdata->flags))) return 0; irq_mask |= hdata->f11.irq_mask; irq_mask |= hdata->f30.irq_mask; if (data[1] & ~irq_mask) hid_warn(hdev, "unknown intr source:%02lx %s:%d\n", data[1] & ~irq_mask, __FILE__, __LINE__); if (hdata->f11.interrupt_base < hdata->f30.interrupt_base) { index += rmi_f11_input_event(hdev, data[1], &data[index], size - index); index += rmi_f30_input_event(hdev, data[1], &data[index], size - index); } else { index += rmi_f30_input_event(hdev, data[1], &data[index], size - index); index += rmi_f11_input_event(hdev, data[1], &data[index], size - index); } return 1; } static int rmi_read_data_event(struct hid_device *hdev, u8 *data, int size) { struct rmi_data *hdata = hid_get_drvdata(hdev); if (!test_bit(RMI_READ_REQUEST_PENDING, &hdata->flags)) { hid_err(hdev, "no read request pending\n"); return 0; } memcpy(hdata->readReport, data, size < hdata->input_report_size ? size : hdata->input_report_size); set_bit(RMI_READ_DATA_PENDING, &hdata->flags); wake_up(&hdata->wait); return 1; } static int rmi_raw_event(struct hid_device *hdev, struct hid_report *report, u8 *data, int size) { switch (data[0]) { case RMI_READ_DATA_REPORT_ID: return rmi_read_data_event(hdev, data, size); case RMI_ATTN_REPORT_ID: return rmi_input_event(hdev, data, size); case RMI_MOUSE_REPORT_ID: rmi_schedule_reset(hdev); break; } return 0; } static int rmi_post_reset(struct hid_device *hdev) { return rmi_set_mode(hdev, RMI_MODE_ATTN_REPORTS); } static int rmi_post_resume(struct hid_device *hdev) { return rmi_set_mode(hdev, RMI_MODE_ATTN_REPORTS); } #define RMI4_MAX_PAGE 0xff #define RMI4_PAGE_SIZE 0x0100 #define PDT_START_SCAN_LOCATION 0x00e9 #define PDT_END_SCAN_LOCATION 0x0005 #define RMI4_END_OF_PDT(id) ((id) == 0x00 || (id) == 0xff) struct pdt_entry { u8 query_base_addr:8; u8 command_base_addr:8; u8 control_base_addr:8; u8 data_base_addr:8; u8 interrupt_source_count:3; u8 bits3and4:2; u8 function_version:2; u8 bit7:1; u8 function_number:8; } __attribute__((__packed__)); static inline unsigned long rmi_gen_mask(unsigned irq_base, unsigned irq_count) { return GENMASK(irq_count + irq_base - 1, irq_base); } static void rmi_register_function(struct rmi_data *data, struct pdt_entry *pdt_entry, int page, unsigned interrupt_count) { struct rmi_function *f = NULL; u16 page_base = page << 8; switch (pdt_entry->function_number) { case 0x11: f = &data->f11; break; case 0x30: f = &data->f30; break; } if (f) { f->page = page; f->query_base_addr = page_base | pdt_entry->query_base_addr; f->command_base_addr = page_base | pdt_entry->command_base_addr; f->control_base_addr = page_base | pdt_entry->control_base_addr; f->data_base_addr = page_base | pdt_entry->data_base_addr; f->interrupt_base = interrupt_count; f->interrupt_count = pdt_entry->interrupt_source_count; f->irq_mask = rmi_gen_mask(f->interrupt_base, f->interrupt_count); } } static int rmi_scan_pdt(struct hid_device *hdev) { struct rmi_data *data = hid_get_drvdata(hdev); struct pdt_entry entry; int page; bool page_has_function; int i; int retval; int interrupt = 0; u16 page_start, pdt_start , pdt_end; hid_info(hdev, "Scanning PDT...\n"); for (page = 0; (page <= RMI4_MAX_PAGE); page++) { page_start = RMI4_PAGE_SIZE * page; pdt_start = page_start + PDT_START_SCAN_LOCATION; pdt_end = page_start + PDT_END_SCAN_LOCATION; page_has_function = false; for (i = pdt_start; i >= pdt_end; i -= sizeof(entry)) { retval = rmi_read_block(hdev, i, &entry, sizeof(entry)); if (retval) { hid_err(hdev, "Read of PDT entry at %#06x failed.\n", i); goto error_exit; } if (RMI4_END_OF_PDT(entry.function_number)) break; page_has_function = true; hid_info(hdev, "Found F%02X on page %#04x\n", entry.function_number, page); rmi_register_function(data, &entry, page, interrupt); interrupt += entry.interrupt_source_count; } if (!page_has_function) break; } hid_info(hdev, "%s: Done with PDT scan.\n", __func__); retval = 0; error_exit: return retval; } static int rmi_populate_f11(struct hid_device *hdev) { struct rmi_data *data = hid_get_drvdata(hdev); u8 buf[20]; int ret; bool has_query9; bool has_query10; bool has_query11; bool has_query12; bool has_physical_props; unsigned x_size, y_size; u16 query12_offset; if (!data->f11.query_base_addr) { hid_err(hdev, "No 2D sensor found, giving up.\n"); return -ENODEV; } /* query 0 contains some useful information */ ret = rmi_read(hdev, data->f11.query_base_addr, buf); if (ret) { hid_err(hdev, "can not get query 0: %d.\n", ret); return ret; } has_query9 = !!(buf[0] & BIT(3)); has_query11 = !!(buf[0] & BIT(4)); has_query12 = !!(buf[0] & BIT(5)); /* query 1 to get the max number of fingers */ ret = rmi_read(hdev, data->f11.query_base_addr + 1, buf); if (ret) { hid_err(hdev, "can not get NumberOfFingers: %d.\n", ret); return ret; } data->max_fingers = (buf[0] & 0x07) + 1; if (data->max_fingers > 5) data->max_fingers = 10; data->f11.report_size = data->max_fingers * 5 + DIV_ROUND_UP(data->max_fingers, 4); if (!(buf[0] & BIT(4))) { hid_err(hdev, "No absolute events, giving up.\n"); return -ENODEV; } /* query 8 to find out if query 10 exists */ ret = rmi_read(hdev, data->f11.query_base_addr + 8, buf); if (ret) { hid_err(hdev, "can not read gesture information: %d.\n", ret); return ret; } has_query10 = !!(buf[0] & BIT(2)); /* * At least 8 queries are guaranteed to be present in F11 * +1 for query12. */ query12_offset = 9; if (has_query9) ++query12_offset; if (has_query10) ++query12_offset; if (has_query11) ++query12_offset; /* query 12 to know if the physical properties are reported */ if (has_query12) { ret = rmi_read(hdev, data->f11.query_base_addr + query12_offset, buf); if (ret) { hid_err(hdev, "can not get query 12: %d.\n", ret); return ret; } has_physical_props = !!(buf[0] & BIT(5)); if (has_physical_props) { ret = rmi_read_block(hdev, data->f11.query_base_addr + query12_offset + 1, buf, 4); if (ret) { hid_err(hdev, "can not read query 15-18: %d.\n", ret); return ret; } x_size = buf[0] | (buf[1] << 8); y_size = buf[2] | (buf[3] << 8); data->x_size_mm = DIV_ROUND_CLOSEST(x_size, 10); data->y_size_mm = DIV_ROUND_CLOSEST(y_size, 10); hid_info(hdev, "%s: size in mm: %d x %d\n", __func__, data->x_size_mm, data->y_size_mm); } } /* * retrieve the ctrl registers * the ctrl register has a size of 20 but a fw bug split it into 16 + 4, * and there is no way to know if the first 20 bytes are here or not. * We use only the first 10 bytes, so get only them. */ ret = rmi_read_block(hdev, data->f11.control_base_addr, buf, 10); if (ret) { hid_err(hdev, "can not read ctrl block of size 10: %d.\n", ret); return ret; } data->max_x = buf[6] | (buf[7] << 8); data->max_y = buf[8] | (buf[9] << 8); return 0; } static int rmi_populate_f30(struct hid_device *hdev) { struct rmi_data *data = hid_get_drvdata(hdev); u8 buf[20]; int ret; bool has_gpio, has_led; unsigned bytes_per_ctrl; u8 ctrl2_addr; int ctrl2_3_length; int i; /* function F30 is for physical buttons */ if (!data->f30.query_base_addr) { hid_err(hdev, "No GPIO/LEDs found, giving up.\n"); return -ENODEV; } ret = rmi_read_block(hdev, data->f30.query_base_addr, buf, 2); if (ret) { hid_err(hdev, "can not get F30 query registers: %d.\n", ret); return ret; } has_gpio = !!(buf[0] & BIT(3)); has_led = !!(buf[0] & BIT(2)); data->gpio_led_count = buf[1] & 0x1f; /* retrieve ctrl 2 & 3 registers */ bytes_per_ctrl = (data->gpio_led_count + 7) / 8; /* Ctrl0 is present only if both has_gpio and has_led are set*/ ctrl2_addr = (has_gpio && has_led) ? bytes_per_ctrl : 0; /* Ctrl1 is always be present */ ctrl2_addr += bytes_per_ctrl; ctrl2_3_length = 2 * bytes_per_ctrl; data->f30.report_size = bytes_per_ctrl; ret = rmi_read_block(hdev, data->f30.control_base_addr + ctrl2_addr, buf, ctrl2_3_length); if (ret) { hid_err(hdev, "can not read ctrl 2&3 block of size %d: %d.\n", ctrl2_3_length, ret); return ret; } for (i = 0; i < data->gpio_led_count; i++) { int byte_position = i >> 3; int bit_position = i & 0x07; u8 dir_byte = buf[byte_position]; u8 data_byte = buf[byte_position + bytes_per_ctrl]; bool dir = (dir_byte >> bit_position) & BIT(0); bool dat = (data_byte >> bit_position) & BIT(0);