Linux Audio

Check our new training course

Loading...
v6.2
   1// SPDX-License-Identifier: GPL-2.0-only
   2/*
   3 *  HIDPP protocol for Logitech receivers
   4 *
   5 *  Copyright (c) 2011 Logitech (c)
   6 *  Copyright (c) 2012-2013 Google (c)
   7 *  Copyright (c) 2013-2014 Red Hat Inc.
   8 */
   9
  10
  11#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  12
  13#include <linux/device.h>
  14#include <linux/input.h>
  15#include <linux/usb.h>
  16#include <linux/hid.h>
  17#include <linux/module.h>
  18#include <linux/slab.h>
  19#include <linux/sched.h>
  20#include <linux/sched/clock.h>
  21#include <linux/kfifo.h>
  22#include <linux/input/mt.h>
  23#include <linux/workqueue.h>
  24#include <linux/atomic.h>
  25#include <linux/fixp-arith.h>
  26#include <asm/unaligned.h>
  27#include "usbhid/usbhid.h"
  28#include "hid-ids.h"
  29
  30MODULE_LICENSE("GPL");
  31MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
  32MODULE_AUTHOR("Nestor Lopez Casado <nlopezcasad@logitech.com>");
  33
  34static bool disable_raw_mode;
  35module_param(disable_raw_mode, bool, 0644);
  36MODULE_PARM_DESC(disable_raw_mode,
  37	"Disable Raw mode reporting for touchpads and keep firmware gestures.");
  38
  39static bool disable_tap_to_click;
  40module_param(disable_tap_to_click, bool, 0644);
  41MODULE_PARM_DESC(disable_tap_to_click,
  42	"Disable Tap-To-Click mode reporting for touchpads (only on the K400 currently).");
  43
  44/* Define a non-zero software ID to identify our own requests */
  45#define LINUX_KERNEL_SW_ID			0x01
  46
  47#define REPORT_ID_HIDPP_SHORT			0x10
  48#define REPORT_ID_HIDPP_LONG			0x11
  49#define REPORT_ID_HIDPP_VERY_LONG		0x12
  50
  51#define HIDPP_REPORT_SHORT_LENGTH		7
  52#define HIDPP_REPORT_LONG_LENGTH		20
  53#define HIDPP_REPORT_VERY_LONG_MAX_LENGTH	64
  54
  55#define HIDPP_REPORT_SHORT_SUPPORTED		BIT(0)
  56#define HIDPP_REPORT_LONG_SUPPORTED		BIT(1)
  57#define HIDPP_REPORT_VERY_LONG_SUPPORTED	BIT(2)
  58
  59#define HIDPP_SUB_ID_CONSUMER_VENDOR_KEYS	0x03
  60#define HIDPP_SUB_ID_ROLLER			0x05
  61#define HIDPP_SUB_ID_MOUSE_EXTRA_BTNS		0x06
  62#define HIDPP_SUB_ID_USER_IFACE_EVENT		0x08
  63#define HIDPP_USER_IFACE_EVENT_ENCRYPTION_KEY_LOST	BIT(5)
  64
  65#define HIDPP_QUIRK_CLASS_WTP			BIT(0)
  66#define HIDPP_QUIRK_CLASS_M560			BIT(1)
  67#define HIDPP_QUIRK_CLASS_K400			BIT(2)
  68#define HIDPP_QUIRK_CLASS_G920			BIT(3)
  69#define HIDPP_QUIRK_CLASS_K750			BIT(4)
  70
  71/* bits 2..20 are reserved for classes */
  72/* #define HIDPP_QUIRK_CONNECT_EVENTS		BIT(21) disabled */
  73#define HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS	BIT(22)
  74#define HIDPP_QUIRK_NO_HIDINPUT			BIT(23)
  75#define HIDPP_QUIRK_FORCE_OUTPUT_REPORTS	BIT(24)
  76#define HIDPP_QUIRK_UNIFYING			BIT(25)
  77#define HIDPP_QUIRK_HIDPP_WHEELS		BIT(26)
  78#define HIDPP_QUIRK_HIDPP_EXTRA_MOUSE_BTNS	BIT(27)
  79#define HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS	BIT(28)
 
  80
  81/* These are just aliases for now */
  82#define HIDPP_QUIRK_KBD_SCROLL_WHEEL HIDPP_QUIRK_HIDPP_WHEELS
  83#define HIDPP_QUIRK_KBD_ZOOM_WHEEL   HIDPP_QUIRK_HIDPP_WHEELS
  84
  85/* Convenience constant to check for any high-res support. */
  86#define HIDPP_CAPABILITY_HI_RES_SCROLL	(HIDPP_CAPABILITY_HIDPP10_FAST_SCROLL | \
  87					 HIDPP_CAPABILITY_HIDPP20_HI_RES_SCROLL | \
  88					 HIDPP_CAPABILITY_HIDPP20_HI_RES_WHEEL)
  89
  90#define HIDPP_QUIRK_DELAYED_INIT		HIDPP_QUIRK_NO_HIDINPUT
  91
  92#define HIDPP_CAPABILITY_HIDPP10_BATTERY	BIT(0)
  93#define HIDPP_CAPABILITY_HIDPP20_BATTERY	BIT(1)
  94#define HIDPP_CAPABILITY_BATTERY_MILEAGE	BIT(2)
  95#define HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS	BIT(3)
  96#define HIDPP_CAPABILITY_BATTERY_VOLTAGE	BIT(4)
  97#define HIDPP_CAPABILITY_BATTERY_PERCENTAGE	BIT(5)
  98#define HIDPP_CAPABILITY_UNIFIED_BATTERY	BIT(6)
  99#define HIDPP_CAPABILITY_HIDPP20_HI_RES_WHEEL	BIT(7)
 100#define HIDPP_CAPABILITY_HIDPP20_HI_RES_SCROLL	BIT(8)
 101#define HIDPP_CAPABILITY_HIDPP10_FAST_SCROLL	BIT(9)
 
 102
 103#define lg_map_key_clear(c)  hid_map_usage_clear(hi, usage, bit, max, EV_KEY, (c))
 104
 105/*
 106 * There are two hidpp protocols in use, the first version hidpp10 is known
 107 * as register access protocol or RAP, the second version hidpp20 is known as
 108 * feature access protocol or FAP
 109 *
 110 * Most older devices (including the Unifying usb receiver) use the RAP protocol
 111 * where as most newer devices use the FAP protocol. Both protocols are
 112 * compatible with the underlying transport, which could be usb, Unifiying, or
 113 * bluetooth. The message lengths are defined by the hid vendor specific report
 114 * descriptor for the HIDPP_SHORT report type (total message lenth 7 bytes) and
 115 * the HIDPP_LONG report type (total message length 20 bytes)
 116 *
 117 * The RAP protocol uses both report types, whereas the FAP only uses HIDPP_LONG
 118 * messages. The Unifying receiver itself responds to RAP messages (device index
 119 * is 0xFF for the receiver), and all messages (short or long) with a device
 120 * index between 1 and 6 are passed untouched to the corresponding paired
 121 * Unifying device.
 122 *
 123 * The paired device can be RAP or FAP, it will receive the message untouched
 124 * from the Unifiying receiver.
 125 */
 126
 127struct fap {
 128	u8 feature_index;
 129	u8 funcindex_clientid;
 130	u8 params[HIDPP_REPORT_VERY_LONG_MAX_LENGTH - 4U];
 131};
 132
 133struct rap {
 134	u8 sub_id;
 135	u8 reg_address;
 136	u8 params[HIDPP_REPORT_VERY_LONG_MAX_LENGTH - 4U];
 137};
 138
 139struct hidpp_report {
 140	u8 report_id;
 141	u8 device_index;
 142	union {
 143		struct fap fap;
 144		struct rap rap;
 145		u8 rawbytes[sizeof(struct fap)];
 146	};
 147} __packed;
 148
 149struct hidpp_battery {
 150	u8 feature_index;
 151	u8 solar_feature_index;
 152	u8 voltage_feature_index;
 
 153	struct power_supply_desc desc;
 154	struct power_supply *ps;
 155	char name[64];
 156	int status;
 157	int capacity;
 158	int level;
 159	int voltage;
 160	int charge_type;
 161	bool online;
 162	u8 supported_levels_1004;
 163};
 164
 165/**
 166 * struct hidpp_scroll_counter - Utility class for processing high-resolution
 167 *                             scroll events.
 168 * @dev: the input device for which events should be reported.
 169 * @wheel_multiplier: the scalar multiplier to be applied to each wheel event
 170 * @remainder: counts the number of high-resolution units moved since the last
 171 *             low-resolution event (REL_WHEEL or REL_HWHEEL) was sent. Should
 172 *             only be used by class methods.
 173 * @direction: direction of last movement (1 or -1)
 174 * @last_time: last event time, used to reset remainder after inactivity
 175 */
 176struct hidpp_scroll_counter {
 177	int wheel_multiplier;
 178	int remainder;
 179	int direction;
 180	unsigned long long last_time;
 181};
 182
 183struct hidpp_device {
 184	struct hid_device *hid_dev;
 185	struct input_dev *input;
 186	struct mutex send_mutex;
 187	void *send_receive_buf;
 188	char *name;		/* will never be NULL and should not be freed */
 189	wait_queue_head_t wait;
 190	int very_long_report_length;
 191	bool answer_available;
 192	u8 protocol_major;
 193	u8 protocol_minor;
 194
 195	void *private_data;
 196
 197	struct work_struct work;
 198	struct kfifo delayed_work_fifo;
 199	atomic_t connected;
 200	struct input_dev *delayed_input;
 201
 202	unsigned long quirks;
 203	unsigned long capabilities;
 204	u8 supported_reports;
 205
 206	struct hidpp_battery battery;
 207	struct hidpp_scroll_counter vertical_wheel_counter;
 208
 209	u8 wireless_feature_index;
 
 
 210};
 211
 212/* HID++ 1.0 error codes */
 213#define HIDPP_ERROR				0x8f
 214#define HIDPP_ERROR_SUCCESS			0x00
 215#define HIDPP_ERROR_INVALID_SUBID		0x01
 216#define HIDPP_ERROR_INVALID_ADRESS		0x02
 217#define HIDPP_ERROR_INVALID_VALUE		0x03
 218#define HIDPP_ERROR_CONNECT_FAIL		0x04
 219#define HIDPP_ERROR_TOO_MANY_DEVICES		0x05
 220#define HIDPP_ERROR_ALREADY_EXISTS		0x06
 221#define HIDPP_ERROR_BUSY			0x07
 222#define HIDPP_ERROR_UNKNOWN_DEVICE		0x08
 223#define HIDPP_ERROR_RESOURCE_ERROR		0x09
 224#define HIDPP_ERROR_REQUEST_UNAVAILABLE		0x0a
 225#define HIDPP_ERROR_INVALID_PARAM_VALUE		0x0b
 226#define HIDPP_ERROR_WRONG_PIN_CODE		0x0c
 227/* HID++ 2.0 error codes */
 
 
 
 
 
 
 
 
 
 
 228#define HIDPP20_ERROR				0xff
 229
 230static void hidpp_connect_event(struct hidpp_device *hidpp_dev);
 231
 232static int __hidpp_send_report(struct hid_device *hdev,
 233				struct hidpp_report *hidpp_report)
 234{
 235	struct hidpp_device *hidpp = hid_get_drvdata(hdev);
 236	int fields_count, ret;
 237
 238	switch (hidpp_report->report_id) {
 239	case REPORT_ID_HIDPP_SHORT:
 240		fields_count = HIDPP_REPORT_SHORT_LENGTH;
 241		break;
 242	case REPORT_ID_HIDPP_LONG:
 243		fields_count = HIDPP_REPORT_LONG_LENGTH;
 244		break;
 245	case REPORT_ID_HIDPP_VERY_LONG:
 246		fields_count = hidpp->very_long_report_length;
 247		break;
 248	default:
 249		return -ENODEV;
 250	}
 251
 252	/*
 253	 * set the device_index as the receiver, it will be overwritten by
 254	 * hid_hw_request if needed
 255	 */
 256	hidpp_report->device_index = 0xff;
 257
 258	if (hidpp->quirks & HIDPP_QUIRK_FORCE_OUTPUT_REPORTS) {
 259		ret = hid_hw_output_report(hdev, (u8 *)hidpp_report, fields_count);
 260	} else {
 261		ret = hid_hw_raw_request(hdev, hidpp_report->report_id,
 262			(u8 *)hidpp_report, fields_count, HID_OUTPUT_REPORT,
 263			HID_REQ_SET_REPORT);
 264	}
 265
 266	return ret == fields_count ? 0 : -1;
 267}
 268
 269/*
 270 * hidpp_send_message_sync() returns 0 in case of success, and something else
 271 * in case of a failure.
 272 * - If ' something else' is positive, that means that an error has been raised
 273 *   by the protocol itself.
 274 * - If ' something else' is negative, that means that we had a classic error
 275 *   (-ENOMEM, -EPIPE, etc...)
 
 
 276 */
 277static int hidpp_send_message_sync(struct hidpp_device *hidpp,
 278	struct hidpp_report *message,
 279	struct hidpp_report *response)
 280{
 281	int ret;
 282
 283	mutex_lock(&hidpp->send_mutex);
 284
 285	hidpp->send_receive_buf = response;
 286	hidpp->answer_available = false;
 287
 288	/*
 289	 * So that we can later validate the answer when it arrives
 290	 * in hidpp_raw_event
 291	 */
 292	*response = *message;
 293
 294	ret = __hidpp_send_report(hidpp->hid_dev, message);
 295
 296	if (ret) {
 297		dbg_hid("__hidpp_send_report returned err: %d\n", ret);
 298		memset(response, 0, sizeof(struct hidpp_report));
 299		goto exit;
 300	}
 301
 302	if (!wait_event_timeout(hidpp->wait, hidpp->answer_available,
 303				5*HZ)) {
 304		dbg_hid("%s:timeout waiting for response\n", __func__);
 305		memset(response, 0, sizeof(struct hidpp_report));
 306		ret = -ETIMEDOUT;
 307	}
 308
 309	if (response->report_id == REPORT_ID_HIDPP_SHORT &&
 310	    response->rap.sub_id == HIDPP_ERROR) {
 311		ret = response->rap.params[1];
 312		dbg_hid("%s:got hidpp error %02X\n", __func__, ret);
 313		goto exit;
 314	}
 315
 316	if ((response->report_id == REPORT_ID_HIDPP_LONG ||
 317			response->report_id == REPORT_ID_HIDPP_VERY_LONG) &&
 318			response->fap.feature_index == HIDPP20_ERROR) {
 319		ret = response->fap.params[1];
 320		dbg_hid("%s:got hidpp 2.0 error %02X\n", __func__, ret);
 321		goto exit;
 322	}
 323
 324exit:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 325	mutex_unlock(&hidpp->send_mutex);
 326	return ret;
 327
 328}
 329
 
 
 
 
 
 
 
 330static int hidpp_send_fap_command_sync(struct hidpp_device *hidpp,
 331	u8 feat_index, u8 funcindex_clientid, u8 *params, int param_count,
 332	struct hidpp_report *response)
 333{
 334	struct hidpp_report *message;
 335	int ret;
 336
 337	if (param_count > sizeof(message->fap.params))
 
 
 
 
 338		return -EINVAL;
 
 339
 340	message = kzalloc(sizeof(struct hidpp_report), GFP_KERNEL);
 341	if (!message)
 342		return -ENOMEM;
 343
 344	if (param_count > (HIDPP_REPORT_LONG_LENGTH - 4))
 345		message->report_id = REPORT_ID_HIDPP_VERY_LONG;
 346	else
 347		message->report_id = REPORT_ID_HIDPP_LONG;
 348	message->fap.feature_index = feat_index;
 349	message->fap.funcindex_clientid = funcindex_clientid | LINUX_KERNEL_SW_ID;
 350	memcpy(&message->fap.params, params, param_count);
 351
 352	ret = hidpp_send_message_sync(hidpp, message, response);
 353	kfree(message);
 354	return ret;
 355}
 356
 
 
 
 
 
 
 
 357static int hidpp_send_rap_command_sync(struct hidpp_device *hidpp_dev,
 358	u8 report_id, u8 sub_id, u8 reg_address, u8 *params, int param_count,
 359	struct hidpp_report *response)
 360{
 361	struct hidpp_report *message;
 362	int ret, max_count;
 363
 364	/* Send as long report if short reports are not supported. */
 365	if (report_id == REPORT_ID_HIDPP_SHORT &&
 366	    !(hidpp_dev->supported_reports & HIDPP_REPORT_SHORT_SUPPORTED))
 367		report_id = REPORT_ID_HIDPP_LONG;
 368
 369	switch (report_id) {
 370	case REPORT_ID_HIDPP_SHORT:
 371		max_count = HIDPP_REPORT_SHORT_LENGTH - 4;
 372		break;
 373	case REPORT_ID_HIDPP_LONG:
 374		max_count = HIDPP_REPORT_LONG_LENGTH - 4;
 375		break;
 376	case REPORT_ID_HIDPP_VERY_LONG:
 377		max_count = hidpp_dev->very_long_report_length - 4;
 378		break;
 379	default:
 380		return -EINVAL;
 381	}
 382
 383	if (param_count > max_count)
 384		return -EINVAL;
 385
 386	message = kzalloc(sizeof(struct hidpp_report), GFP_KERNEL);
 387	if (!message)
 388		return -ENOMEM;
 389	message->report_id = report_id;
 390	message->rap.sub_id = sub_id;
 391	message->rap.reg_address = reg_address;
 392	memcpy(&message->rap.params, params, param_count);
 393
 394	ret = hidpp_send_message_sync(hidpp_dev, message, response);
 395	kfree(message);
 396	return ret;
 397}
 398
 399static void delayed_work_cb(struct work_struct *work)
 400{
 401	struct hidpp_device *hidpp = container_of(work, struct hidpp_device,
 402							work);
 403	hidpp_connect_event(hidpp);
 404}
 405
 406static inline bool hidpp_match_answer(struct hidpp_report *question,
 407		struct hidpp_report *answer)
 408{
 409	return (answer->fap.feature_index == question->fap.feature_index) &&
 410	   (answer->fap.funcindex_clientid == question->fap.funcindex_clientid);
 411}
 412
 413static inline bool hidpp_match_error(struct hidpp_report *question,
 414		struct hidpp_report *answer)
 415{
 416	return ((answer->rap.sub_id == HIDPP_ERROR) ||
 417	    (answer->fap.feature_index == HIDPP20_ERROR)) &&
 418	    (answer->fap.funcindex_clientid == question->fap.feature_index) &&
 419	    (answer->fap.params[0] == question->fap.funcindex_clientid);
 420}
 421
 422static inline bool hidpp_report_is_connect_event(struct hidpp_device *hidpp,
 423		struct hidpp_report *report)
 424{
 425	return (hidpp->wireless_feature_index &&
 426		(report->fap.feature_index == hidpp->wireless_feature_index)) ||
 427		((report->report_id == REPORT_ID_HIDPP_SHORT) &&
 428		(report->rap.sub_id == 0x41));
 429}
 430
 431/*
 432 * hidpp_prefix_name() prefixes the current given name with "Logitech ".
 433 */
 434static void hidpp_prefix_name(char **name, int name_length)
 435{
 436#define PREFIX_LENGTH 9 /* "Logitech " */
 437
 438	int new_length;
 439	char *new_name;
 440
 441	if (name_length > PREFIX_LENGTH &&
 442	    strncmp(*name, "Logitech ", PREFIX_LENGTH) == 0)
 443		/* The prefix has is already in the name */
 444		return;
 445
 446	new_length = PREFIX_LENGTH + name_length;
 447	new_name = kzalloc(new_length, GFP_KERNEL);
 448	if (!new_name)
 449		return;
 450
 451	snprintf(new_name, new_length, "Logitech %s", *name);
 452
 453	kfree(*name);
 454
 455	*name = new_name;
 456}
 457
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 458/**
 459 * hidpp_scroll_counter_handle_scroll() - Send high- and low-resolution scroll
 460 *                                        events given a high-resolution wheel
 461 *                                        movement.
 462 * @input_dev: Pointer to the input device
 463 * @counter: a hid_scroll_counter struct describing the wheel.
 464 * @hi_res_value: the movement of the wheel, in the mouse's high-resolution
 465 *                units.
 466 *
 467 * Given a high-resolution movement, this function converts the movement into
 468 * fractions of 120 and emits high-resolution scroll events for the input
 469 * device. It also uses the multiplier from &struct hid_scroll_counter to
 470 * emit low-resolution scroll events when appropriate for
 471 * backwards-compatibility with userspace input libraries.
 472 */
 473static void hidpp_scroll_counter_handle_scroll(struct input_dev *input_dev,
 474					       struct hidpp_scroll_counter *counter,
 475					       int hi_res_value)
 476{
 477	int low_res_value, remainder, direction;
 478	unsigned long long now, previous;
 479
 480	hi_res_value = hi_res_value * 120/counter->wheel_multiplier;
 481	input_report_rel(input_dev, REL_WHEEL_HI_RES, hi_res_value);
 482
 483	remainder = counter->remainder;
 484	direction = hi_res_value > 0 ? 1 : -1;
 485
 486	now = sched_clock();
 487	previous = counter->last_time;
 488	counter->last_time = now;
 489	/*
 490	 * Reset the remainder after a period of inactivity or when the
 491	 * direction changes. This prevents the REL_WHEEL emulation point
 492	 * from sliding for devices that don't always provide the same
 493	 * number of movements per detent.
 494	 */
 495	if (now - previous > 1000000000 || direction != counter->direction)
 496		remainder = 0;
 497
 498	counter->direction = direction;
 499	remainder += hi_res_value;
 500
 501	/* Some wheels will rest 7/8ths of a detent from the previous detent
 502	 * after slow movement, so we want the threshold for low-res events to
 503	 * be in the middle between two detents (e.g. after 4/8ths) as
 504	 * opposed to on the detents themselves (8/8ths).
 505	 */
 506	if (abs(remainder) >= 60) {
 507		/* Add (or subtract) 1 because we want to trigger when the wheel
 508		 * is half-way to the next detent (i.e. scroll 1 detent after a
 509		 * 1/2 detent movement, 2 detents after a 1 1/2 detent movement,
 510		 * etc.).
 511		 */
 512		low_res_value = remainder / 120;
 513		if (low_res_value == 0)
 514			low_res_value = (hi_res_value > 0 ? 1 : -1);
 515		input_report_rel(input_dev, REL_WHEEL, low_res_value);
 516		remainder -= low_res_value * 120;
 517	}
 518	counter->remainder = remainder;
 519}
 520
 521/* -------------------------------------------------------------------------- */
 522/* HIDP++ 1.0 commands                                                        */
 523/* -------------------------------------------------------------------------- */
 524
 525#define HIDPP_SET_REGISTER				0x80
 526#define HIDPP_GET_REGISTER				0x81
 527#define HIDPP_SET_LONG_REGISTER				0x82
 528#define HIDPP_GET_LONG_REGISTER				0x83
 529
 530/**
 531 * hidpp10_set_register - Modify a HID++ 1.0 register.
 532 * @hidpp_dev: the device to set the register on.
 533 * @register_address: the address of the register to modify.
 534 * @byte: the byte of the register to modify. Should be less than 3.
 535 * @mask: mask of the bits to modify
 536 * @value: new values for the bits in mask
 537 * Return: 0 if successful, otherwise a negative error code.
 538 */
 539static int hidpp10_set_register(struct hidpp_device *hidpp_dev,
 540	u8 register_address, u8 byte, u8 mask, u8 value)
 541{
 542	struct hidpp_report response;
 543	int ret;
 544	u8 params[3] = { 0 };
 545
 546	ret = hidpp_send_rap_command_sync(hidpp_dev,
 547					  REPORT_ID_HIDPP_SHORT,
 548					  HIDPP_GET_REGISTER,
 549					  register_address,
 550					  NULL, 0, &response);
 551	if (ret)
 552		return ret;
 553
 554	memcpy(params, response.rap.params, 3);
 555
 556	params[byte] &= ~mask;
 557	params[byte] |= value & mask;
 558
 559	return hidpp_send_rap_command_sync(hidpp_dev,
 560					   REPORT_ID_HIDPP_SHORT,
 561					   HIDPP_SET_REGISTER,
 562					   register_address,
 563					   params, 3, &response);
 564}
 565
 566#define HIDPP_REG_ENABLE_REPORTS			0x00
 567#define HIDPP_ENABLE_CONSUMER_REPORT			BIT(0)
 568#define HIDPP_ENABLE_WHEEL_REPORT			BIT(2)
 569#define HIDPP_ENABLE_MOUSE_EXTRA_BTN_REPORT		BIT(3)
 570#define HIDPP_ENABLE_BAT_REPORT				BIT(4)
 571#define HIDPP_ENABLE_HWHEEL_REPORT			BIT(5)
 572
 573static int hidpp10_enable_battery_reporting(struct hidpp_device *hidpp_dev)
 574{
 575	return hidpp10_set_register(hidpp_dev, HIDPP_REG_ENABLE_REPORTS, 0,
 576			  HIDPP_ENABLE_BAT_REPORT, HIDPP_ENABLE_BAT_REPORT);
 577}
 578
 579#define HIDPP_REG_FEATURES				0x01
 580#define HIDPP_ENABLE_SPECIAL_BUTTON_FUNC		BIT(1)
 581#define HIDPP_ENABLE_FAST_SCROLL			BIT(6)
 582
 583/* On HID++ 1.0 devices, high-res scroll was called "scrolling acceleration". */
 584static int hidpp10_enable_scrolling_acceleration(struct hidpp_device *hidpp_dev)
 585{
 586	return hidpp10_set_register(hidpp_dev, HIDPP_REG_FEATURES, 0,
 587			  HIDPP_ENABLE_FAST_SCROLL, HIDPP_ENABLE_FAST_SCROLL);
 588}
 589
 590#define HIDPP_REG_BATTERY_STATUS			0x07
 591
 592static int hidpp10_battery_status_map_level(u8 param)
 593{
 594	int level;
 595
 596	switch (param) {
 597	case 1 ... 2:
 598		level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
 599		break;
 600	case 3 ... 4:
 601		level = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
 602		break;
 603	case 5 ... 6:
 604		level = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
 605		break;
 606	case 7:
 607		level = POWER_SUPPLY_CAPACITY_LEVEL_HIGH;
 608		break;
 609	default:
 610		level = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
 611	}
 612
 613	return level;
 614}
 615
 616static int hidpp10_battery_status_map_status(u8 param)
 617{
 618	int status;
 619
 620	switch (param) {
 621	case 0x00:
 622		/* discharging (in use) */
 623		status = POWER_SUPPLY_STATUS_DISCHARGING;
 624		break;
 625	case 0x21: /* (standard) charging */
 626	case 0x24: /* fast charging */
 627	case 0x25: /* slow charging */
 628		status = POWER_SUPPLY_STATUS_CHARGING;
 629		break;
 630	case 0x26: /* topping charge */
 631	case 0x22: /* charge complete */
 632		status = POWER_SUPPLY_STATUS_FULL;
 633		break;
 634	case 0x20: /* unknown */
 635		status = POWER_SUPPLY_STATUS_UNKNOWN;
 636		break;
 637	/*
 638	 * 0x01...0x1F = reserved (not charging)
 639	 * 0x23 = charging error
 640	 * 0x27..0xff = reserved
 641	 */
 642	default:
 643		status = POWER_SUPPLY_STATUS_NOT_CHARGING;
 644		break;
 645	}
 646
 647	return status;
 648}
 649
 650static int hidpp10_query_battery_status(struct hidpp_device *hidpp)
 651{
 652	struct hidpp_report response;
 653	int ret, status;
 654
 655	ret = hidpp_send_rap_command_sync(hidpp,
 656					REPORT_ID_HIDPP_SHORT,
 657					HIDPP_GET_REGISTER,
 658					HIDPP_REG_BATTERY_STATUS,
 659					NULL, 0, &response);
 660	if (ret)
 661		return ret;
 662
 663	hidpp->battery.level =
 664		hidpp10_battery_status_map_level(response.rap.params[0]);
 665	status = hidpp10_battery_status_map_status(response.rap.params[1]);
 666	hidpp->battery.status = status;
 667	/* the capacity is only available when discharging or full */
 668	hidpp->battery.online = status == POWER_SUPPLY_STATUS_DISCHARGING ||
 669				status == POWER_SUPPLY_STATUS_FULL;
 670
 671	return 0;
 672}
 673
 674#define HIDPP_REG_BATTERY_MILEAGE			0x0D
 675
 676static int hidpp10_battery_mileage_map_status(u8 param)
 677{
 678	int status;
 679
 680	switch (param >> 6) {
 681	case 0x00:
 682		/* discharging (in use) */
 683		status = POWER_SUPPLY_STATUS_DISCHARGING;
 684		break;
 685	case 0x01: /* charging */
 686		status = POWER_SUPPLY_STATUS_CHARGING;
 687		break;
 688	case 0x02: /* charge complete */
 689		status = POWER_SUPPLY_STATUS_FULL;
 690		break;
 691	/*
 692	 * 0x03 = charging error
 693	 */
 694	default:
 695		status = POWER_SUPPLY_STATUS_NOT_CHARGING;
 696		break;
 697	}
 698
 699	return status;
 700}
 701
 702static int hidpp10_query_battery_mileage(struct hidpp_device *hidpp)
 703{
 704	struct hidpp_report response;
 705	int ret, status;
 706
 707	ret = hidpp_send_rap_command_sync(hidpp,
 708					REPORT_ID_HIDPP_SHORT,
 709					HIDPP_GET_REGISTER,
 710					HIDPP_REG_BATTERY_MILEAGE,
 711					NULL, 0, &response);
 712	if (ret)
 713		return ret;
 714
 715	hidpp->battery.capacity = response.rap.params[0];
 716	status = hidpp10_battery_mileage_map_status(response.rap.params[2]);
 717	hidpp->battery.status = status;
 718	/* the capacity is only available when discharging or full */
 719	hidpp->battery.online = status == POWER_SUPPLY_STATUS_DISCHARGING ||
 720				status == POWER_SUPPLY_STATUS_FULL;
 721
 722	return 0;
 723}
 724
 725static int hidpp10_battery_event(struct hidpp_device *hidpp, u8 *data, int size)
 726{
 727	struct hidpp_report *report = (struct hidpp_report *)data;
 728	int status, capacity, level;
 729	bool changed;
 730
 731	if (report->report_id != REPORT_ID_HIDPP_SHORT)
 732		return 0;
 733
 734	switch (report->rap.sub_id) {
 735	case HIDPP_REG_BATTERY_STATUS:
 736		capacity = hidpp->battery.capacity;
 737		level = hidpp10_battery_status_map_level(report->rawbytes[1]);
 738		status = hidpp10_battery_status_map_status(report->rawbytes[2]);
 739		break;
 740	case HIDPP_REG_BATTERY_MILEAGE:
 741		capacity = report->rap.params[0];
 742		level = hidpp->battery.level;
 743		status = hidpp10_battery_mileage_map_status(report->rawbytes[3]);
 744		break;
 745	default:
 746		return 0;
 747	}
 748
 749	changed = capacity != hidpp->battery.capacity ||
 750		  level != hidpp->battery.level ||
 751		  status != hidpp->battery.status;
 752
 753	/* the capacity is only available when discharging or full */
 754	hidpp->battery.online = status == POWER_SUPPLY_STATUS_DISCHARGING ||
 755				status == POWER_SUPPLY_STATUS_FULL;
 756
 757	if (changed) {
 758		hidpp->battery.level = level;
 759		hidpp->battery.status = status;
 760		if (hidpp->battery.ps)
 761			power_supply_changed(hidpp->battery.ps);
 762	}
 763
 764	return 0;
 765}
 766
 767#define HIDPP_REG_PAIRING_INFORMATION			0xB5
 768#define HIDPP_EXTENDED_PAIRING				0x30
 769#define HIDPP_DEVICE_NAME				0x40
 770
 771static char *hidpp_unifying_get_name(struct hidpp_device *hidpp_dev)
 772{
 773	struct hidpp_report response;
 774	int ret;
 775	u8 params[1] = { HIDPP_DEVICE_NAME };
 776	char *name;
 777	int len;
 778
 779	ret = hidpp_send_rap_command_sync(hidpp_dev,
 780					REPORT_ID_HIDPP_SHORT,
 781					HIDPP_GET_LONG_REGISTER,
 782					HIDPP_REG_PAIRING_INFORMATION,
 783					params, 1, &response);
 784	if (ret)
 785		return NULL;
 786
 787	len = response.rap.params[1];
 788
 789	if (2 + len > sizeof(response.rap.params))
 790		return NULL;
 791
 792	if (len < 4) /* logitech devices are usually at least Xddd */
 793		return NULL;
 794
 795	name = kzalloc(len + 1, GFP_KERNEL);
 796	if (!name)
 797		return NULL;
 798
 799	memcpy(name, &response.rap.params[2], len);
 800
 801	/* include the terminating '\0' */
 802	hidpp_prefix_name(&name, len + 1);
 803
 804	return name;
 805}
 806
 807static int hidpp_unifying_get_serial(struct hidpp_device *hidpp, u32 *serial)
 808{
 809	struct hidpp_report response;
 810	int ret;
 811	u8 params[1] = { HIDPP_EXTENDED_PAIRING };
 812
 813	ret = hidpp_send_rap_command_sync(hidpp,
 814					REPORT_ID_HIDPP_SHORT,
 815					HIDPP_GET_LONG_REGISTER,
 816					HIDPP_REG_PAIRING_INFORMATION,
 817					params, 1, &response);
 818	if (ret)
 819		return ret;
 820
 821	/*
 822	 * We don't care about LE or BE, we will output it as a string
 823	 * with %4phD, so we need to keep the order.
 824	 */
 825	*serial = *((u32 *)&response.rap.params[1]);
 826	return 0;
 827}
 828
 829static int hidpp_unifying_init(struct hidpp_device *hidpp)
 830{
 831	struct hid_device *hdev = hidpp->hid_dev;
 832	const char *name;
 833	u32 serial;
 834	int ret;
 835
 836	ret = hidpp_unifying_get_serial(hidpp, &serial);
 837	if (ret)
 838		return ret;
 839
 840	snprintf(hdev->uniq, sizeof(hdev->uniq), "%04x-%4phD",
 841		 hdev->product, &serial);
 842	dbg_hid("HID++ Unifying: Got serial: %s\n", hdev->uniq);
 843
 844	name = hidpp_unifying_get_name(hidpp);
 845	if (!name)
 846		return -EIO;
 847
 848	snprintf(hdev->name, sizeof(hdev->name), "%s", name);
 849	dbg_hid("HID++ Unifying: Got name: %s\n", name);
 850
 851	kfree(name);
 852	return 0;
 853}
 854
 855/* -------------------------------------------------------------------------- */
 856/* 0x0000: Root                                                               */
 857/* -------------------------------------------------------------------------- */
 858
 859#define HIDPP_PAGE_ROOT					0x0000
 860#define HIDPP_PAGE_ROOT_IDX				0x00
 861
 862#define CMD_ROOT_GET_FEATURE				0x00
 863#define CMD_ROOT_GET_PROTOCOL_VERSION			0x10
 864
 865static int hidpp_root_get_feature(struct hidpp_device *hidpp, u16 feature,
 866	u8 *feature_index, u8 *feature_type)
 867{
 868	struct hidpp_report response;
 869	int ret;
 870	u8 params[2] = { feature >> 8, feature & 0x00FF };
 871
 872	ret = hidpp_send_fap_command_sync(hidpp,
 873			HIDPP_PAGE_ROOT_IDX,
 874			CMD_ROOT_GET_FEATURE,
 875			params, 2, &response);
 876	if (ret)
 877		return ret;
 878
 879	if (response.fap.params[0] == 0)
 880		return -ENOENT;
 881
 882	*feature_index = response.fap.params[0];
 883	*feature_type = response.fap.params[1];
 884
 885	return ret;
 886}
 887
 888static int hidpp_root_get_protocol_version(struct hidpp_device *hidpp)
 889{
 890	const u8 ping_byte = 0x5a;
 891	u8 ping_data[3] = { 0, 0, ping_byte };
 892	struct hidpp_report response;
 893	int ret;
 894
 895	ret = hidpp_send_rap_command_sync(hidpp,
 896			REPORT_ID_HIDPP_SHORT,
 897			HIDPP_PAGE_ROOT_IDX,
 898			CMD_ROOT_GET_PROTOCOL_VERSION | LINUX_KERNEL_SW_ID,
 899			ping_data, sizeof(ping_data), &response);
 900
 901	if (ret == HIDPP_ERROR_INVALID_SUBID) {
 902		hidpp->protocol_major = 1;
 903		hidpp->protocol_minor = 0;
 904		goto print_version;
 905	}
 906
 907	/* the device might not be connected */
 908	if (ret == HIDPP_ERROR_RESOURCE_ERROR)
 909		return -EIO;
 910
 911	if (ret > 0) {
 912		hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
 913			__func__, ret);
 914		return -EPROTO;
 915	}
 916	if (ret)
 917		return ret;
 918
 919	if (response.rap.params[2] != ping_byte) {
 920		hid_err(hidpp->hid_dev, "%s: ping mismatch 0x%02x != 0x%02x\n",
 921			__func__, response.rap.params[2], ping_byte);
 922		return -EPROTO;
 923	}
 924
 925	hidpp->protocol_major = response.rap.params[0];
 926	hidpp->protocol_minor = response.rap.params[1];
 927
 928print_version:
 929	hid_info(hidpp->hid_dev, "HID++ %u.%u device connected.\n",
 930		 hidpp->protocol_major, hidpp->protocol_minor);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 931	return 0;
 932}
 933
 934/* -------------------------------------------------------------------------- */
 935/* 0x0005: GetDeviceNameType                                                  */
 936/* -------------------------------------------------------------------------- */
 937
 938#define HIDPP_PAGE_GET_DEVICE_NAME_TYPE			0x0005
 939
 940#define CMD_GET_DEVICE_NAME_TYPE_GET_COUNT		0x00
 941#define CMD_GET_DEVICE_NAME_TYPE_GET_DEVICE_NAME	0x10
 942#define CMD_GET_DEVICE_NAME_TYPE_GET_TYPE		0x20
 943
 944static int hidpp_devicenametype_get_count(struct hidpp_device *hidpp,
 945	u8 feature_index, u8 *nameLength)
 946{
 947	struct hidpp_report response;
 948	int ret;
 949
 950	ret = hidpp_send_fap_command_sync(hidpp, feature_index,
 951		CMD_GET_DEVICE_NAME_TYPE_GET_COUNT, NULL, 0, &response);
 952
 953	if (ret > 0) {
 954		hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
 955			__func__, ret);
 956		return -EPROTO;
 957	}
 958	if (ret)
 959		return ret;
 960
 961	*nameLength = response.fap.params[0];
 962
 963	return ret;
 964}
 965
 966static int hidpp_devicenametype_get_device_name(struct hidpp_device *hidpp,
 967	u8 feature_index, u8 char_index, char *device_name, int len_buf)
 968{
 969	struct hidpp_report response;
 970	int ret, i;
 971	int count;
 972
 973	ret = hidpp_send_fap_command_sync(hidpp, feature_index,
 974		CMD_GET_DEVICE_NAME_TYPE_GET_DEVICE_NAME, &char_index, 1,
 975		&response);
 976
 977	if (ret > 0) {
 978		hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
 979			__func__, ret);
 980		return -EPROTO;
 981	}
 982	if (ret)
 983		return ret;
 984
 985	switch (response.report_id) {
 986	case REPORT_ID_HIDPP_VERY_LONG:
 987		count = hidpp->very_long_report_length - 4;
 988		break;
 989	case REPORT_ID_HIDPP_LONG:
 990		count = HIDPP_REPORT_LONG_LENGTH - 4;
 991		break;
 992	case REPORT_ID_HIDPP_SHORT:
 993		count = HIDPP_REPORT_SHORT_LENGTH - 4;
 994		break;
 995	default:
 996		return -EPROTO;
 997	}
 998
 999	if (len_buf < count)
1000		count = len_buf;
1001
1002	for (i = 0; i < count; i++)
1003		device_name[i] = response.fap.params[i];
1004
1005	return count;
1006}
1007
1008static char *hidpp_get_device_name(struct hidpp_device *hidpp)
1009{
1010	u8 feature_type;
1011	u8 feature_index;
1012	u8 __name_length;
1013	char *name;
1014	unsigned index = 0;
1015	int ret;
1016
1017	ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_GET_DEVICE_NAME_TYPE,
1018		&feature_index, &feature_type);
1019	if (ret)
1020		return NULL;
1021
1022	ret = hidpp_devicenametype_get_count(hidpp, feature_index,
1023		&__name_length);
1024	if (ret)
1025		return NULL;
1026
1027	name = kzalloc(__name_length + 1, GFP_KERNEL);
1028	if (!name)
1029		return NULL;
1030
1031	while (index < __name_length) {
1032		ret = hidpp_devicenametype_get_device_name(hidpp,
1033			feature_index, index, name + index,
1034			__name_length - index);
1035		if (ret <= 0) {
1036			kfree(name);
1037			return NULL;
1038		}
1039		index += ret;
1040	}
1041
1042	/* include the terminating '\0' */
1043	hidpp_prefix_name(&name, __name_length + 1);
1044
1045	return name;
1046}
1047
1048/* -------------------------------------------------------------------------- */
1049/* 0x1000: Battery level status                                               */
1050/* -------------------------------------------------------------------------- */
1051
1052#define HIDPP_PAGE_BATTERY_LEVEL_STATUS				0x1000
1053
1054#define CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_LEVEL_STATUS	0x00
1055#define CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_CAPABILITY		0x10
1056
1057#define EVENT_BATTERY_LEVEL_STATUS_BROADCAST			0x00
1058
1059#define FLAG_BATTERY_LEVEL_DISABLE_OSD				BIT(0)
1060#define FLAG_BATTERY_LEVEL_MILEAGE				BIT(1)
1061#define FLAG_BATTERY_LEVEL_RECHARGEABLE				BIT(2)
1062
1063static int hidpp_map_battery_level(int capacity)
1064{
1065	if (capacity < 11)
1066		return POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
1067	/*
1068	 * The spec says this should be < 31 but some devices report 30
1069	 * with brand new batteries and Windows reports 30 as "Good".
1070	 */
1071	else if (capacity < 30)
1072		return POWER_SUPPLY_CAPACITY_LEVEL_LOW;
1073	else if (capacity < 81)
1074		return POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
1075	return POWER_SUPPLY_CAPACITY_LEVEL_FULL;
1076}
1077
1078static int hidpp20_batterylevel_map_status_capacity(u8 data[3], int *capacity,
1079						    int *next_capacity,
1080						    int *level)
1081{
1082	int status;
1083
1084	*capacity = data[0];
1085	*next_capacity = data[1];
1086	*level = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
1087
1088	/* When discharging, we can rely on the device reported capacity.
1089	 * For all other states the device reports 0 (unknown).
1090	 */
1091	switch (data[2]) {
1092		case 0: /* discharging (in use) */
1093			status = POWER_SUPPLY_STATUS_DISCHARGING;
1094			*level = hidpp_map_battery_level(*capacity);
1095			break;
1096		case 1: /* recharging */
1097			status = POWER_SUPPLY_STATUS_CHARGING;
1098			break;
1099		case 2: /* charge in final stage */
1100			status = POWER_SUPPLY_STATUS_CHARGING;
1101			break;
1102		case 3: /* charge complete */
1103			status = POWER_SUPPLY_STATUS_FULL;
1104			*level = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
1105			*capacity = 100;
1106			break;
1107		case 4: /* recharging below optimal speed */
1108			status = POWER_SUPPLY_STATUS_CHARGING;
1109			break;
1110		/* 5 = invalid battery type
1111		   6 = thermal error
1112		   7 = other charging error */
1113		default:
1114			status = POWER_SUPPLY_STATUS_NOT_CHARGING;
1115			break;
1116	}
1117
1118	return status;
1119}
1120
1121static int hidpp20_batterylevel_get_battery_capacity(struct hidpp_device *hidpp,
1122						     u8 feature_index,
1123						     int *status,
1124						     int *capacity,
1125						     int *next_capacity,
1126						     int *level)
1127{
1128	struct hidpp_report response;
1129	int ret;
1130	u8 *params = (u8 *)response.fap.params;
1131
1132	ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1133					  CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_LEVEL_STATUS,
1134					  NULL, 0, &response);
1135	/* Ignore these intermittent errors */
1136	if (ret == HIDPP_ERROR_RESOURCE_ERROR)
1137		return -EIO;
1138	if (ret > 0) {
1139		hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1140			__func__, ret);
1141		return -EPROTO;
1142	}
1143	if (ret)
1144		return ret;
1145
1146	*status = hidpp20_batterylevel_map_status_capacity(params, capacity,
1147							   next_capacity,
1148							   level);
1149
1150	return 0;
1151}
1152
1153static int hidpp20_batterylevel_get_battery_info(struct hidpp_device *hidpp,
1154						  u8 feature_index)
1155{
1156	struct hidpp_report response;
1157	int ret;
1158	u8 *params = (u8 *)response.fap.params;
1159	unsigned int level_count, flags;
1160
1161	ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1162					  CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_CAPABILITY,
1163					  NULL, 0, &response);
1164	if (ret > 0) {
1165		hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1166			__func__, ret);
1167		return -EPROTO;
1168	}
1169	if (ret)
1170		return ret;
1171
1172	level_count = params[0];
1173	flags = params[1];
1174
1175	if (level_count < 10 || !(flags & FLAG_BATTERY_LEVEL_MILEAGE))
1176		hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS;
1177	else
1178		hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_MILEAGE;
1179
1180	return 0;
1181}
1182
1183static int hidpp20_query_battery_info_1000(struct hidpp_device *hidpp)
1184{
1185	u8 feature_type;
1186	int ret;
1187	int status, capacity, next_capacity, level;
1188
1189	if (hidpp->battery.feature_index == 0xff) {
1190		ret = hidpp_root_get_feature(hidpp,
1191					     HIDPP_PAGE_BATTERY_LEVEL_STATUS,
1192					     &hidpp->battery.feature_index,
1193					     &feature_type);
1194		if (ret)
1195			return ret;
1196	}
1197
1198	ret = hidpp20_batterylevel_get_battery_capacity(hidpp,
1199						hidpp->battery.feature_index,
1200						&status, &capacity,
1201						&next_capacity, &level);
1202	if (ret)
1203		return ret;
1204
1205	ret = hidpp20_batterylevel_get_battery_info(hidpp,
1206						hidpp->battery.feature_index);
1207	if (ret)
1208		return ret;
1209
1210	hidpp->battery.status = status;
1211	hidpp->battery.capacity = capacity;
1212	hidpp->battery.level = level;
1213	/* the capacity is only available when discharging or full */
1214	hidpp->battery.online = status == POWER_SUPPLY_STATUS_DISCHARGING ||
1215				status == POWER_SUPPLY_STATUS_FULL;
1216
1217	return 0;
1218}
1219
1220static int hidpp20_battery_event_1000(struct hidpp_device *hidpp,
1221				 u8 *data, int size)
1222{
1223	struct hidpp_report *report = (struct hidpp_report *)data;
1224	int status, capacity, next_capacity, level;
1225	bool changed;
1226
1227	if (report->fap.feature_index != hidpp->battery.feature_index ||
1228	    report->fap.funcindex_clientid != EVENT_BATTERY_LEVEL_STATUS_BROADCAST)
1229		return 0;
1230
1231	status = hidpp20_batterylevel_map_status_capacity(report->fap.params,
1232							  &capacity,
1233							  &next_capacity,
1234							  &level);
1235
1236	/* the capacity is only available when discharging or full */
1237	hidpp->battery.online = status == POWER_SUPPLY_STATUS_DISCHARGING ||
1238				status == POWER_SUPPLY_STATUS_FULL;
1239
1240	changed = capacity != hidpp->battery.capacity ||
1241		  level != hidpp->battery.level ||
1242		  status != hidpp->battery.status;
1243
1244	if (changed) {
1245		hidpp->battery.level = level;
1246		hidpp->battery.capacity = capacity;
1247		hidpp->battery.status = status;
1248		if (hidpp->battery.ps)
1249			power_supply_changed(hidpp->battery.ps);
1250	}
1251
1252	return 0;
1253}
1254
1255/* -------------------------------------------------------------------------- */
1256/* 0x1001: Battery voltage                                                    */
1257/* -------------------------------------------------------------------------- */
1258
1259#define HIDPP_PAGE_BATTERY_VOLTAGE 0x1001
1260
1261#define CMD_BATTERY_VOLTAGE_GET_BATTERY_VOLTAGE 0x00
1262
1263#define EVENT_BATTERY_VOLTAGE_STATUS_BROADCAST 0x00
1264
1265static int hidpp20_battery_map_status_voltage(u8 data[3], int *voltage,
1266						int *level, int *charge_type)
1267{
1268	int status;
1269
1270	long flags = (long) data[2];
1271	*level = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
1272
1273	if (flags & 0x80)
1274		switch (flags & 0x07) {
1275		case 0:
1276			status = POWER_SUPPLY_STATUS_CHARGING;
1277			break;
1278		case 1:
1279			status = POWER_SUPPLY_STATUS_FULL;
1280			*level = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
1281			break;
1282		case 2:
1283			status = POWER_SUPPLY_STATUS_NOT_CHARGING;
1284			break;
1285		default:
1286			status = POWER_SUPPLY_STATUS_UNKNOWN;
1287			break;
1288		}
1289	else
1290		status = POWER_SUPPLY_STATUS_DISCHARGING;
1291
1292	*charge_type = POWER_SUPPLY_CHARGE_TYPE_STANDARD;
1293	if (test_bit(3, &flags)) {
1294		*charge_type = POWER_SUPPLY_CHARGE_TYPE_FAST;
1295	}
1296	if (test_bit(4, &flags)) {
1297		*charge_type = POWER_SUPPLY_CHARGE_TYPE_TRICKLE;
1298	}
1299	if (test_bit(5, &flags)) {
1300		*level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
1301	}
1302
1303	*voltage = get_unaligned_be16(data);
1304
1305	return status;
1306}
1307
1308static int hidpp20_battery_get_battery_voltage(struct hidpp_device *hidpp,
1309						 u8 feature_index,
1310						 int *status, int *voltage,
1311						 int *level, int *charge_type)
1312{
1313	struct hidpp_report response;
1314	int ret;
1315	u8 *params = (u8 *)response.fap.params;
1316
1317	ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1318					  CMD_BATTERY_VOLTAGE_GET_BATTERY_VOLTAGE,
1319					  NULL, 0, &response);
1320
1321	if (ret > 0) {
1322		hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1323			__func__, ret);
1324		return -EPROTO;
1325	}
1326	if (ret)
1327		return ret;
1328
1329	hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_VOLTAGE;
1330
1331	*status = hidpp20_battery_map_status_voltage(params, voltage,
1332						     level, charge_type);
1333
1334	return 0;
1335}
1336
1337static int hidpp20_map_battery_capacity(struct hid_device *hid_dev, int voltage)
1338{
1339	/* NB: This voltage curve doesn't necessarily map perfectly to all
1340	 * devices that implement the BATTERY_VOLTAGE feature. This is because
1341	 * there are a few devices that use different battery technology.
1342	 */
1343
1344	static const int voltages[] = {
1345		4186, 4156, 4143, 4133, 4122, 4113, 4103, 4094, 4086, 4075,
1346		4067, 4059, 4051, 4043, 4035, 4027, 4019, 4011, 4003, 3997,
1347		3989, 3983, 3976, 3969, 3961, 3955, 3949, 3942, 3935, 3929,
1348		3922, 3916, 3909, 3902, 3896, 3890, 3883, 3877, 3870, 3865,
1349		3859, 3853, 3848, 3842, 3837, 3833, 3828, 3824, 3819, 3815,
1350		3811, 3808, 3804, 3800, 3797, 3793, 3790, 3787, 3784, 3781,
1351		3778, 3775, 3772, 3770, 3767, 3764, 3762, 3759, 3757, 3754,
1352		3751, 3748, 3744, 3741, 3737, 3734, 3730, 3726, 3724, 3720,
1353		3717, 3714, 3710, 3706, 3702, 3697, 3693, 3688, 3683, 3677,
1354		3671, 3666, 3662, 3658, 3654, 3646, 3633, 3612, 3579, 3537
1355	};
1356
1357	int i;
1358
1359	BUILD_BUG_ON(ARRAY_SIZE(voltages) != 100);
1360
1361	if (unlikely(voltage < 3500 || voltage >= 5000))
1362		hid_warn_once(hid_dev,
1363			      "%s: possibly using the wrong voltage curve\n",
1364			      __func__);
1365
1366	for (i = 0; i < ARRAY_SIZE(voltages); i++) {
1367		if (voltage >= voltages[i])
1368			return ARRAY_SIZE(voltages) - i;
1369	}
1370
1371	return 0;
1372}
1373
1374static int hidpp20_query_battery_voltage_info(struct hidpp_device *hidpp)
1375{
1376	u8 feature_type;
1377	int ret;
1378	int status, voltage, level, charge_type;
1379
1380	if (hidpp->battery.voltage_feature_index == 0xff) {
1381		ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_BATTERY_VOLTAGE,
1382					     &hidpp->battery.voltage_feature_index,
1383					     &feature_type);
1384		if (ret)
1385			return ret;
1386	}
1387
1388	ret = hidpp20_battery_get_battery_voltage(hidpp,
1389						  hidpp->battery.voltage_feature_index,
1390						  &status, &voltage, &level, &charge_type);
1391
1392	if (ret)
1393		return ret;
1394
1395	hidpp->battery.status = status;
1396	hidpp->battery.voltage = voltage;
1397	hidpp->battery.capacity = hidpp20_map_battery_capacity(hidpp->hid_dev,
1398							       voltage);
1399	hidpp->battery.level = level;
1400	hidpp->battery.charge_type = charge_type;
1401	hidpp->battery.online = status != POWER_SUPPLY_STATUS_NOT_CHARGING;
1402
1403	return 0;
1404}
1405
1406static int hidpp20_battery_voltage_event(struct hidpp_device *hidpp,
1407					    u8 *data, int size)
1408{
1409	struct hidpp_report *report = (struct hidpp_report *)data;
1410	int status, voltage, level, charge_type;
1411
1412	if (report->fap.feature_index != hidpp->battery.voltage_feature_index ||
1413		report->fap.funcindex_clientid != EVENT_BATTERY_VOLTAGE_STATUS_BROADCAST)
1414		return 0;
1415
1416	status = hidpp20_battery_map_status_voltage(report->fap.params, &voltage,
1417						    &level, &charge_type);
1418
1419	hidpp->battery.online = status != POWER_SUPPLY_STATUS_NOT_CHARGING;
1420
1421	if (voltage != hidpp->battery.voltage || status != hidpp->battery.status) {
1422		hidpp->battery.voltage = voltage;
1423		hidpp->battery.capacity = hidpp20_map_battery_capacity(hidpp->hid_dev,
1424								       voltage);
1425		hidpp->battery.status = status;
1426		hidpp->battery.level = level;
1427		hidpp->battery.charge_type = charge_type;
1428		if (hidpp->battery.ps)
1429			power_supply_changed(hidpp->battery.ps);
1430	}
1431	return 0;
1432}
1433
1434/* -------------------------------------------------------------------------- */
1435/* 0x1004: Unified battery                                                    */
1436/* -------------------------------------------------------------------------- */
1437
1438#define HIDPP_PAGE_UNIFIED_BATTERY				0x1004
1439
1440#define CMD_UNIFIED_BATTERY_GET_CAPABILITIES			0x00
1441#define CMD_UNIFIED_BATTERY_GET_STATUS				0x10
1442
1443#define EVENT_UNIFIED_BATTERY_STATUS_EVENT			0x00
1444
1445#define FLAG_UNIFIED_BATTERY_LEVEL_CRITICAL			BIT(0)
1446#define FLAG_UNIFIED_BATTERY_LEVEL_LOW				BIT(1)
1447#define FLAG_UNIFIED_BATTERY_LEVEL_GOOD				BIT(2)
1448#define FLAG_UNIFIED_BATTERY_LEVEL_FULL				BIT(3)
1449
1450#define FLAG_UNIFIED_BATTERY_FLAGS_RECHARGEABLE			BIT(0)
1451#define FLAG_UNIFIED_BATTERY_FLAGS_STATE_OF_CHARGE		BIT(1)
1452
1453static int hidpp20_unifiedbattery_get_capabilities(struct hidpp_device *hidpp,
1454						   u8 feature_index)
1455{
1456	struct hidpp_report response;
1457	int ret;
1458	u8 *params = (u8 *)response.fap.params;
1459
1460	if (hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS ||
1461	    hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_PERCENTAGE) {
1462		/* we have already set the device capabilities, so let's skip */
1463		return 0;
1464	}
1465
1466	ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1467					  CMD_UNIFIED_BATTERY_GET_CAPABILITIES,
1468					  NULL, 0, &response);
1469	/* Ignore these intermittent errors */
1470	if (ret == HIDPP_ERROR_RESOURCE_ERROR)
1471		return -EIO;
1472	if (ret > 0) {
1473		hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1474			__func__, ret);
1475		return -EPROTO;
1476	}
1477	if (ret)
1478		return ret;
1479
1480	/*
1481	 * If the device supports state of charge (battery percentage) we won't
1482	 * export the battery level information. there are 4 possible battery
1483	 * levels and they all are optional, this means that the device might
1484	 * not support any of them, we are just better off with the battery
1485	 * percentage.
1486	 */
1487	if (params[1] & FLAG_UNIFIED_BATTERY_FLAGS_STATE_OF_CHARGE) {
1488		hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_PERCENTAGE;
1489		hidpp->battery.supported_levels_1004 = 0;
1490	} else {
1491		hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS;
1492		hidpp->battery.supported_levels_1004 = params[0];
1493	}
1494
1495	return 0;
1496}
1497
1498static int hidpp20_unifiedbattery_map_status(struct hidpp_device *hidpp,
1499					     u8 charging_status,
1500					     u8 external_power_status)
1501{
1502	int status;
1503
1504	switch (charging_status) {
1505		case 0: /* discharging */
1506			status = POWER_SUPPLY_STATUS_DISCHARGING;
1507			break;
1508		case 1: /* charging */
1509		case 2: /* charging slow */
1510			status = POWER_SUPPLY_STATUS_CHARGING;
1511			break;
1512		case 3: /* complete */
1513			status = POWER_SUPPLY_STATUS_FULL;
1514			break;
1515		case 4: /* error */
1516			status = POWER_SUPPLY_STATUS_NOT_CHARGING;
1517			hid_info(hidpp->hid_dev, "%s: charging error",
1518				 hidpp->name);
1519			break;
1520		default:
1521			status = POWER_SUPPLY_STATUS_NOT_CHARGING;
1522			break;
1523	}
1524
1525	return status;
1526}
1527
1528static int hidpp20_unifiedbattery_map_level(struct hidpp_device *hidpp,
1529					    u8 battery_level)
1530{
1531	/* cler unsupported level bits */
1532	battery_level &= hidpp->battery.supported_levels_1004;
1533
1534	if (battery_level & FLAG_UNIFIED_BATTERY_LEVEL_FULL)
1535		return POWER_SUPPLY_CAPACITY_LEVEL_FULL;
1536	else if (battery_level & FLAG_UNIFIED_BATTERY_LEVEL_GOOD)
1537		return POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
1538	else if (battery_level & FLAG_UNIFIED_BATTERY_LEVEL_LOW)
1539		return POWER_SUPPLY_CAPACITY_LEVEL_LOW;
1540	else if (battery_level & FLAG_UNIFIED_BATTERY_LEVEL_CRITICAL)
1541		return POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
1542
1543	return POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
1544}
1545
1546static int hidpp20_unifiedbattery_get_status(struct hidpp_device *hidpp,
1547					     u8 feature_index,
1548					     u8 *state_of_charge,
1549					     int *status,
1550					     int *level)
1551{
1552	struct hidpp_report response;
1553	int ret;
1554	u8 *params = (u8 *)response.fap.params;
1555
1556	ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1557					  CMD_UNIFIED_BATTERY_GET_STATUS,
1558					  NULL, 0, &response);
1559	/* Ignore these intermittent errors */
1560	if (ret == HIDPP_ERROR_RESOURCE_ERROR)
1561		return -EIO;
1562	if (ret > 0) {
1563		hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1564			__func__, ret);
1565		return -EPROTO;
1566	}
1567	if (ret)
1568		return ret;
1569
1570	*state_of_charge = params[0];
1571	*status = hidpp20_unifiedbattery_map_status(hidpp, params[2], params[3]);
1572	*level = hidpp20_unifiedbattery_map_level(hidpp, params[1]);
1573
1574	return 0;
1575}
1576
1577static int hidpp20_query_battery_info_1004(struct hidpp_device *hidpp)
1578{
1579	u8 feature_type;
1580	int ret;
1581	u8 state_of_charge;
1582	int status, level;
1583
1584	if (hidpp->battery.feature_index == 0xff) {
1585		ret = hidpp_root_get_feature(hidpp,
1586					     HIDPP_PAGE_UNIFIED_BATTERY,
1587					     &hidpp->battery.feature_index,
1588					     &feature_type);
1589		if (ret)
1590			return ret;
1591	}
1592
1593	ret = hidpp20_unifiedbattery_get_capabilities(hidpp,
1594					hidpp->battery.feature_index);
1595	if (ret)
1596		return ret;
1597
1598	ret = hidpp20_unifiedbattery_get_status(hidpp,
1599						hidpp->battery.feature_index,
1600						&state_of_charge,
1601						&status,
1602						&level);
1603	if (ret)
1604		return ret;
1605
1606	hidpp->capabilities |= HIDPP_CAPABILITY_UNIFIED_BATTERY;
1607	hidpp->battery.capacity = state_of_charge;
1608	hidpp->battery.status = status;
1609	hidpp->battery.level = level;
1610	hidpp->battery.online = true;
1611
1612	return 0;
1613}
1614
1615static int hidpp20_battery_event_1004(struct hidpp_device *hidpp,
1616				 u8 *data, int size)
1617{
1618	struct hidpp_report *report = (struct hidpp_report *)data;
1619	u8 *params = (u8 *)report->fap.params;
1620	int state_of_charge, status, level;
1621	bool changed;
1622
1623	if (report->fap.feature_index != hidpp->battery.feature_index ||
1624	    report->fap.funcindex_clientid != EVENT_UNIFIED_BATTERY_STATUS_EVENT)
1625		return 0;
1626
1627	state_of_charge = params[0];
1628	status = hidpp20_unifiedbattery_map_status(hidpp, params[2], params[3]);
1629	level = hidpp20_unifiedbattery_map_level(hidpp, params[1]);
1630
1631	changed = status != hidpp->battery.status ||
1632		  (state_of_charge != hidpp->battery.capacity &&
1633		   hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_PERCENTAGE) ||
1634		  (level != hidpp->battery.level &&
1635		   hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS);
1636
1637	if (changed) {
1638		hidpp->battery.capacity = state_of_charge;
1639		hidpp->battery.status = status;
1640		hidpp->battery.level = level;
1641		if (hidpp->battery.ps)
1642			power_supply_changed(hidpp->battery.ps);
1643	}
1644
1645	return 0;
1646}
1647
1648/* -------------------------------------------------------------------------- */
1649/* Battery feature helpers                                                    */
1650/* -------------------------------------------------------------------------- */
1651
1652static enum power_supply_property hidpp_battery_props[] = {
1653	POWER_SUPPLY_PROP_ONLINE,
1654	POWER_SUPPLY_PROP_STATUS,
1655	POWER_SUPPLY_PROP_SCOPE,
1656	POWER_SUPPLY_PROP_MODEL_NAME,
1657	POWER_SUPPLY_PROP_MANUFACTURER,
1658	POWER_SUPPLY_PROP_SERIAL_NUMBER,
1659	0, /* placeholder for POWER_SUPPLY_PROP_CAPACITY, */
1660	0, /* placeholder for POWER_SUPPLY_PROP_CAPACITY_LEVEL, */
1661	0, /* placeholder for POWER_SUPPLY_PROP_VOLTAGE_NOW, */
1662};
1663
1664static int hidpp_battery_get_property(struct power_supply *psy,
1665				      enum power_supply_property psp,
1666				      union power_supply_propval *val)
1667{
1668	struct hidpp_device *hidpp = power_supply_get_drvdata(psy);
1669	int ret = 0;
1670
1671	switch(psp) {
1672		case POWER_SUPPLY_PROP_STATUS:
1673			val->intval = hidpp->battery.status;
1674			break;
1675		case POWER_SUPPLY_PROP_CAPACITY:
1676			val->intval = hidpp->battery.capacity;
1677			break;
1678		case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
1679			val->intval = hidpp->battery.level;
1680			break;
1681		case POWER_SUPPLY_PROP_SCOPE:
1682			val->intval = POWER_SUPPLY_SCOPE_DEVICE;
1683			break;
1684		case POWER_SUPPLY_PROP_ONLINE:
1685			val->intval = hidpp->battery.online;
1686			break;
1687		case POWER_SUPPLY_PROP_MODEL_NAME:
1688			if (!strncmp(hidpp->name, "Logitech ", 9))
1689				val->strval = hidpp->name + 9;
1690			else
1691				val->strval = hidpp->name;
1692			break;
1693		case POWER_SUPPLY_PROP_MANUFACTURER:
1694			val->strval = "Logitech";
1695			break;
1696		case POWER_SUPPLY_PROP_SERIAL_NUMBER:
1697			val->strval = hidpp->hid_dev->uniq;
1698			break;
1699		case POWER_SUPPLY_PROP_VOLTAGE_NOW:
1700			/* hardware reports voltage in mV. sysfs expects uV */
1701			val->intval = hidpp->battery.voltage * 1000;
1702			break;
1703		case POWER_SUPPLY_PROP_CHARGE_TYPE:
1704			val->intval = hidpp->battery.charge_type;
1705			break;
1706		default:
1707			ret = -EINVAL;
1708			break;
1709	}
1710
1711	return ret;
1712}
1713
1714/* -------------------------------------------------------------------------- */
1715/* 0x1d4b: Wireless device status                                             */
1716/* -------------------------------------------------------------------------- */
1717#define HIDPP_PAGE_WIRELESS_DEVICE_STATUS			0x1d4b
1718
1719static int hidpp_set_wireless_feature_index(struct hidpp_device *hidpp)
1720{
1721	u8 feature_type;
1722	int ret;
1723
1724	ret = hidpp_root_get_feature(hidpp,
1725				     HIDPP_PAGE_WIRELESS_DEVICE_STATUS,
1726				     &hidpp->wireless_feature_index,
1727				     &feature_type);
1728
1729	return ret;
1730}
1731
1732/* -------------------------------------------------------------------------- */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1733/* 0x2120: Hi-resolution scrolling                                            */
1734/* -------------------------------------------------------------------------- */
1735
1736#define HIDPP_PAGE_HI_RESOLUTION_SCROLLING			0x2120
1737
1738#define CMD_HI_RESOLUTION_SCROLLING_SET_HIGHRES_SCROLLING_MODE	0x10
1739
1740static int hidpp_hrs_set_highres_scrolling_mode(struct hidpp_device *hidpp,
1741	bool enabled, u8 *multiplier)
1742{
1743	u8 feature_index;
1744	u8 feature_type;
1745	int ret;
1746	u8 params[1];
1747	struct hidpp_report response;
1748
1749	ret = hidpp_root_get_feature(hidpp,
1750				     HIDPP_PAGE_HI_RESOLUTION_SCROLLING,
1751				     &feature_index,
1752				     &feature_type);
1753	if (ret)
1754		return ret;
1755
1756	params[0] = enabled ? BIT(0) : 0;
1757	ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1758					  CMD_HI_RESOLUTION_SCROLLING_SET_HIGHRES_SCROLLING_MODE,
1759					  params, sizeof(params), &response);
1760	if (ret)
1761		return ret;
1762	*multiplier = response.fap.params[1];
1763	return 0;
1764}
1765
1766/* -------------------------------------------------------------------------- */
1767/* 0x2121: HiRes Wheel                                                        */
1768/* -------------------------------------------------------------------------- */
1769
1770#define HIDPP_PAGE_HIRES_WHEEL		0x2121
1771
1772#define CMD_HIRES_WHEEL_GET_WHEEL_CAPABILITY	0x00
1773#define CMD_HIRES_WHEEL_SET_WHEEL_MODE		0x20
1774
1775static int hidpp_hrw_get_wheel_capability(struct hidpp_device *hidpp,
1776	u8 *multiplier)
1777{
1778	u8 feature_index;
1779	u8 feature_type;
1780	int ret;
1781	struct hidpp_report response;
1782
1783	ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_HIRES_WHEEL,
1784				     &feature_index, &feature_type);
1785	if (ret)
1786		goto return_default;
1787
1788	ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1789					  CMD_HIRES_WHEEL_GET_WHEEL_CAPABILITY,
1790					  NULL, 0, &response);
1791	if (ret)
1792		goto return_default;
1793
1794	*multiplier = response.fap.params[0];
1795	return 0;
1796return_default:
1797	hid_warn(hidpp->hid_dev,
1798		 "Couldn't get wheel multiplier (error %d)\n", ret);
1799	return ret;
1800}
1801
1802static int hidpp_hrw_set_wheel_mode(struct hidpp_device *hidpp, bool invert,
1803	bool high_resolution, bool use_hidpp)
1804{
1805	u8 feature_index;
1806	u8 feature_type;
1807	int ret;
1808	u8 params[1];
1809	struct hidpp_report response;
1810
1811	ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_HIRES_WHEEL,
1812				     &feature_index, &feature_type);
1813	if (ret)
1814		return ret;
1815
1816	params[0] = (invert          ? BIT(2) : 0) |
1817		    (high_resolution ? BIT(1) : 0) |
1818		    (use_hidpp       ? BIT(0) : 0);
1819
1820	return hidpp_send_fap_command_sync(hidpp, feature_index,
1821					   CMD_HIRES_WHEEL_SET_WHEEL_MODE,
1822					   params, sizeof(params), &response);
1823}
1824
1825/* -------------------------------------------------------------------------- */
1826/* 0x4301: Solar Keyboard                                                     */
1827/* -------------------------------------------------------------------------- */
1828
1829#define HIDPP_PAGE_SOLAR_KEYBOARD			0x4301
1830
1831#define CMD_SOLAR_SET_LIGHT_MEASURE			0x00
1832
1833#define EVENT_SOLAR_BATTERY_BROADCAST			0x00
1834#define EVENT_SOLAR_BATTERY_LIGHT_MEASURE		0x10
1835#define EVENT_SOLAR_CHECK_LIGHT_BUTTON			0x20
1836
1837static int hidpp_solar_request_battery_event(struct hidpp_device *hidpp)
1838{
1839	struct hidpp_report response;
1840	u8 params[2] = { 1, 1 };
1841	u8 feature_type;
1842	int ret;
1843
1844	if (hidpp->battery.feature_index == 0xff) {
1845		ret = hidpp_root_get_feature(hidpp,
1846					     HIDPP_PAGE_SOLAR_KEYBOARD,
1847					     &hidpp->battery.solar_feature_index,
1848					     &feature_type);
1849		if (ret)
1850			return ret;
1851	}
1852
1853	ret = hidpp_send_fap_command_sync(hidpp,
1854					  hidpp->battery.solar_feature_index,
1855					  CMD_SOLAR_SET_LIGHT_MEASURE,
1856					  params, 2, &response);
1857	if (ret > 0) {
1858		hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1859			__func__, ret);
1860		return -EPROTO;
1861	}
1862	if (ret)
1863		return ret;
1864
1865	hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_MILEAGE;
1866
1867	return 0;
1868}
1869
1870static int hidpp_solar_battery_event(struct hidpp_device *hidpp,
1871				     u8 *data, int size)
1872{
1873	struct hidpp_report *report = (struct hidpp_report *)data;
1874	int capacity, lux, status;
1875	u8 function;
1876
1877	function = report->fap.funcindex_clientid;
1878
1879
1880	if (report->fap.feature_index != hidpp->battery.solar_feature_index ||
1881	    !(function == EVENT_SOLAR_BATTERY_BROADCAST ||
1882	      function == EVENT_SOLAR_BATTERY_LIGHT_MEASURE ||
1883	      function == EVENT_SOLAR_CHECK_LIGHT_BUTTON))
1884		return 0;
1885
1886	capacity = report->fap.params[0];
1887
1888	switch (function) {
1889	case EVENT_SOLAR_BATTERY_LIGHT_MEASURE:
1890		lux = (report->fap.params[1] << 8) | report->fap.params[2];
1891		if (lux > 200)
1892			status = POWER_SUPPLY_STATUS_CHARGING;
1893		else
1894			status = POWER_SUPPLY_STATUS_DISCHARGING;
1895		break;
1896	case EVENT_SOLAR_CHECK_LIGHT_BUTTON:
1897	default:
1898		if (capacity < hidpp->battery.capacity)
1899			status = POWER_SUPPLY_STATUS_DISCHARGING;
1900		else
1901			status = POWER_SUPPLY_STATUS_CHARGING;
1902
1903	}
1904
1905	if (capacity == 100)
1906		status = POWER_SUPPLY_STATUS_FULL;
1907
1908	hidpp->battery.online = true;
1909	if (capacity != hidpp->battery.capacity ||
1910	    status != hidpp->battery.status) {
1911		hidpp->battery.capacity = capacity;
1912		hidpp->battery.status = status;
1913		if (hidpp->battery.ps)
1914			power_supply_changed(hidpp->battery.ps);
1915	}
1916
1917	return 0;
1918}
1919
1920/* -------------------------------------------------------------------------- */
1921/* 0x6010: Touchpad FW items                                                  */
1922/* -------------------------------------------------------------------------- */
1923
1924#define HIDPP_PAGE_TOUCHPAD_FW_ITEMS			0x6010
1925
1926#define CMD_TOUCHPAD_FW_ITEMS_SET			0x10
1927
1928struct hidpp_touchpad_fw_items {
1929	uint8_t presence;
1930	uint8_t desired_state;
1931	uint8_t state;
1932	uint8_t persistent;
1933};
1934
1935/*
1936 * send a set state command to the device by reading the current items->state
1937 * field. items is then filled with the current state.
1938 */
1939static int hidpp_touchpad_fw_items_set(struct hidpp_device *hidpp,
1940				       u8 feature_index,
1941				       struct hidpp_touchpad_fw_items *items)
1942{
1943	struct hidpp_report response;
1944	int ret;
1945	u8 *params = (u8 *)response.fap.params;
1946
1947	ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1948		CMD_TOUCHPAD_FW_ITEMS_SET, &items->state, 1, &response);
1949
1950	if (ret > 0) {
1951		hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1952			__func__, ret);
1953		return -EPROTO;
1954	}
1955	if (ret)
1956		return ret;
1957
1958	items->presence = params[0];
1959	items->desired_state = params[1];
1960	items->state = params[2];
1961	items->persistent = params[3];
1962
1963	return 0;
1964}
1965
1966/* -------------------------------------------------------------------------- */
1967/* 0x6100: TouchPadRawXY                                                      */
1968/* -------------------------------------------------------------------------- */
1969
1970#define HIDPP_PAGE_TOUCHPAD_RAW_XY			0x6100
1971
1972#define CMD_TOUCHPAD_GET_RAW_INFO			0x00
1973#define CMD_TOUCHPAD_SET_RAW_REPORT_STATE		0x20
1974
1975#define EVENT_TOUCHPAD_RAW_XY				0x00
1976
1977#define TOUCHPAD_RAW_XY_ORIGIN_LOWER_LEFT		0x01
1978#define TOUCHPAD_RAW_XY_ORIGIN_UPPER_LEFT		0x03
1979
1980struct hidpp_touchpad_raw_info {
1981	u16 x_size;
1982	u16 y_size;
1983	u8 z_range;
1984	u8 area_range;
1985	u8 timestamp_unit;
1986	u8 maxcontacts;
1987	u8 origin;
1988	u16 res;
1989};
1990
1991struct hidpp_touchpad_raw_xy_finger {
1992	u8 contact_type;
1993	u8 contact_status;
1994	u16 x;
1995	u16 y;
1996	u8 z;
1997	u8 area;
1998	u8 finger_id;
1999};
2000
2001struct hidpp_touchpad_raw_xy {
2002	u16 timestamp;
2003	struct hidpp_touchpad_raw_xy_finger fingers[2];
2004	u8 spurious_flag;
2005	u8 end_of_frame;
2006	u8 finger_count;
2007	u8 button;
2008};
2009
2010static int hidpp_touchpad_get_raw_info(struct hidpp_device *hidpp,
2011	u8 feature_index, struct hidpp_touchpad_raw_info *raw_info)
2012{
2013	struct hidpp_report response;
2014	int ret;
2015	u8 *params = (u8 *)response.fap.params;
2016
2017	ret = hidpp_send_fap_command_sync(hidpp, feature_index,
2018		CMD_TOUCHPAD_GET_RAW_INFO, NULL, 0, &response);
2019
2020	if (ret > 0) {
2021		hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
2022			__func__, ret);
2023		return -EPROTO;
2024	}
2025	if (ret)
2026		return ret;
2027
2028	raw_info->x_size = get_unaligned_be16(&params[0]);
2029	raw_info->y_size = get_unaligned_be16(&params[2]);
2030	raw_info->z_range = params[4];
2031	raw_info->area_range = params[5];
2032	raw_info->maxcontacts = params[7];
2033	raw_info->origin = params[8];
2034	/* res is given in unit per inch */
2035	raw_info->res = get_unaligned_be16(&params[13]) * 2 / 51;
2036
2037	return ret;
2038}
2039
2040static int hidpp_touchpad_set_raw_report_state(struct hidpp_device *hidpp_dev,
2041		u8 feature_index, bool send_raw_reports,
2042		bool sensor_enhanced_settings)
2043{
2044	struct hidpp_report response;
2045
2046	/*
2047	 * Params:
2048	 *   bit 0 - enable raw
2049	 *   bit 1 - 16bit Z, no area
2050	 *   bit 2 - enhanced sensitivity
2051	 *   bit 3 - width, height (4 bits each) instead of area
2052	 *   bit 4 - send raw + gestures (degrades smoothness)
2053	 *   remaining bits - reserved
2054	 */
2055	u8 params = send_raw_reports | (sensor_enhanced_settings << 2);
2056
2057	return hidpp_send_fap_command_sync(hidpp_dev, feature_index,
2058		CMD_TOUCHPAD_SET_RAW_REPORT_STATE, &params, 1, &response);
2059}
2060
2061static void hidpp_touchpad_touch_event(u8 *data,
2062	struct hidpp_touchpad_raw_xy_finger *finger)
2063{
2064	u8 x_m = data[0] << 2;
2065	u8 y_m = data[2] << 2;
2066
2067	finger->x = x_m << 6 | data[1];
2068	finger->y = y_m << 6 | data[3];
2069
2070	finger->contact_type = data[0] >> 6;
2071	finger->contact_status = data[2] >> 6;
2072
2073	finger->z = data[4];
2074	finger->area = data[5];
2075	finger->finger_id = data[6] >> 4;
2076}
2077
2078static void hidpp_touchpad_raw_xy_event(struct hidpp_device *hidpp_dev,
2079		u8 *data, struct hidpp_touchpad_raw_xy *raw_xy)
2080{
2081	memset(raw_xy, 0, sizeof(struct hidpp_touchpad_raw_xy));
2082	raw_xy->end_of_frame = data[8] & 0x01;
2083	raw_xy->spurious_flag = (data[8] >> 1) & 0x01;
2084	raw_xy->finger_count = data[15] & 0x0f;
2085	raw_xy->button = (data[8] >> 2) & 0x01;
2086
2087	if (raw_xy->finger_count) {
2088		hidpp_touchpad_touch_event(&data[2], &raw_xy->fingers[0]);
2089		hidpp_touchpad_touch_event(&data[9], &raw_xy->fingers[1]);
2090	}
2091}
2092
2093/* -------------------------------------------------------------------------- */
2094/* 0x8123: Force feedback support                                             */
2095/* -------------------------------------------------------------------------- */
2096
2097#define HIDPP_FF_GET_INFO		0x01
2098#define HIDPP_FF_RESET_ALL		0x11
2099#define HIDPP_FF_DOWNLOAD_EFFECT	0x21
2100#define HIDPP_FF_SET_EFFECT_STATE	0x31
2101#define HIDPP_FF_DESTROY_EFFECT		0x41
2102#define HIDPP_FF_GET_APERTURE		0x51
2103#define HIDPP_FF_SET_APERTURE		0x61
2104#define HIDPP_FF_GET_GLOBAL_GAINS	0x71
2105#define HIDPP_FF_SET_GLOBAL_GAINS	0x81
2106
2107#define HIDPP_FF_EFFECT_STATE_GET	0x00
2108#define HIDPP_FF_EFFECT_STATE_STOP	0x01
2109#define HIDPP_FF_EFFECT_STATE_PLAY	0x02
2110#define HIDPP_FF_EFFECT_STATE_PAUSE	0x03
2111
2112#define HIDPP_FF_EFFECT_CONSTANT	0x00
2113#define HIDPP_FF_EFFECT_PERIODIC_SINE		0x01
2114#define HIDPP_FF_EFFECT_PERIODIC_SQUARE		0x02
2115#define HIDPP_FF_EFFECT_PERIODIC_TRIANGLE	0x03
2116#define HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHUP	0x04
2117#define HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHDOWN	0x05
2118#define HIDPP_FF_EFFECT_SPRING		0x06
2119#define HIDPP_FF_EFFECT_DAMPER		0x07
2120#define HIDPP_FF_EFFECT_FRICTION	0x08
2121#define HIDPP_FF_EFFECT_INERTIA		0x09
2122#define HIDPP_FF_EFFECT_RAMP		0x0A
2123
2124#define HIDPP_FF_EFFECT_AUTOSTART	0x80
2125
2126#define HIDPP_FF_EFFECTID_NONE		-1
2127#define HIDPP_FF_EFFECTID_AUTOCENTER	-2
2128#define HIDPP_AUTOCENTER_PARAMS_LENGTH	18
2129
2130#define HIDPP_FF_MAX_PARAMS	20
2131#define HIDPP_FF_RESERVED_SLOTS	1
2132
2133struct hidpp_ff_private_data {
2134	struct hidpp_device *hidpp;
2135	u8 feature_index;
2136	u8 version;
2137	u16 gain;
2138	s16 range;
2139	u8 slot_autocenter;
2140	u8 num_effects;
2141	int *effect_ids;
2142	struct workqueue_struct *wq;
2143	atomic_t workqueue_size;
2144};
2145
2146struct hidpp_ff_work_data {
2147	struct work_struct work;
2148	struct hidpp_ff_private_data *data;
2149	int effect_id;
2150	u8 command;
2151	u8 params[HIDPP_FF_MAX_PARAMS];
2152	u8 size;
2153};
2154
2155static const signed short hidpp_ff_effects[] = {
2156	FF_CONSTANT,
2157	FF_PERIODIC,
2158	FF_SINE,
2159	FF_SQUARE,
2160	FF_SAW_UP,
2161	FF_SAW_DOWN,
2162	FF_TRIANGLE,
2163	FF_SPRING,
2164	FF_DAMPER,
2165	FF_AUTOCENTER,
2166	FF_GAIN,
2167	-1
2168};
2169
2170static const signed short hidpp_ff_effects_v2[] = {
2171	FF_RAMP,
2172	FF_FRICTION,
2173	FF_INERTIA,
2174	-1
2175};
2176
2177static const u8 HIDPP_FF_CONDITION_CMDS[] = {
2178	HIDPP_FF_EFFECT_SPRING,
2179	HIDPP_FF_EFFECT_FRICTION,
2180	HIDPP_FF_EFFECT_DAMPER,
2181	HIDPP_FF_EFFECT_INERTIA
2182};
2183
2184static const char *HIDPP_FF_CONDITION_NAMES[] = {
2185	"spring",
2186	"friction",
2187	"damper",
2188	"inertia"
2189};
2190
2191
2192static u8 hidpp_ff_find_effect(struct hidpp_ff_private_data *data, int effect_id)
2193{
2194	int i;
2195
2196	for (i = 0; i < data->num_effects; i++)
2197		if (data->effect_ids[i] == effect_id)
2198			return i+1;
2199
2200	return 0;
2201}
2202
2203static void hidpp_ff_work_handler(struct work_struct *w)
2204{
2205	struct hidpp_ff_work_data *wd = container_of(w, struct hidpp_ff_work_data, work);
2206	struct hidpp_ff_private_data *data = wd->data;
2207	struct hidpp_report response;
2208	u8 slot;
2209	int ret;
2210
2211	/* add slot number if needed */
2212	switch (wd->effect_id) {
2213	case HIDPP_FF_EFFECTID_AUTOCENTER:
2214		wd->params[0] = data->slot_autocenter;
2215		break;
2216	case HIDPP_FF_EFFECTID_NONE:
2217		/* leave slot as zero */
2218		break;
2219	default:
2220		/* find current slot for effect */
2221		wd->params[0] = hidpp_ff_find_effect(data, wd->effect_id);
2222		break;
2223	}
2224
2225	/* send command and wait for reply */
2226	ret = hidpp_send_fap_command_sync(data->hidpp, data->feature_index,
2227		wd->command, wd->params, wd->size, &response);
2228
2229	if (ret) {
2230		hid_err(data->hidpp->hid_dev, "Failed to send command to device!\n");
2231		goto out;
2232	}
2233
2234	/* parse return data */
2235	switch (wd->command) {
2236	case HIDPP_FF_DOWNLOAD_EFFECT:
2237		slot = response.fap.params[0];
2238		if (slot > 0 && slot <= data->num_effects) {
2239			if (wd->effect_id >= 0)
2240				/* regular effect uploaded */
2241				data->effect_ids[slot-1] = wd->effect_id;
2242			else if (wd->effect_id >= HIDPP_FF_EFFECTID_AUTOCENTER)
2243				/* autocenter spring uploaded */
2244				data->slot_autocenter = slot;
2245		}
2246		break;
2247	case HIDPP_FF_DESTROY_EFFECT:
2248		if (wd->effect_id >= 0)
2249			/* regular effect destroyed */
2250			data->effect_ids[wd->params[0]-1] = -1;
2251		else if (wd->effect_id >= HIDPP_FF_EFFECTID_AUTOCENTER)
2252			/* autocenter spring destoyed */
2253			data->slot_autocenter = 0;
2254		break;
2255	case HIDPP_FF_SET_GLOBAL_GAINS:
2256		data->gain = (wd->params[0] << 8) + wd->params[1];
2257		break;
2258	case HIDPP_FF_SET_APERTURE:
2259		data->range = (wd->params[0] << 8) + wd->params[1];
2260		break;
2261	default:
2262		/* no action needed */
2263		break;
2264	}
2265
2266out:
2267	atomic_dec(&data->workqueue_size);
2268	kfree(wd);
2269}
2270
2271static int hidpp_ff_queue_work(struct hidpp_ff_private_data *data, int effect_id, u8 command, u8 *params, u8 size)
2272{
2273	struct hidpp_ff_work_data *wd = kzalloc(sizeof(*wd), GFP_KERNEL);
2274	int s;
2275
2276	if (!wd)
2277		return -ENOMEM;
2278
2279	INIT_WORK(&wd->work, hidpp_ff_work_handler);
2280
2281	wd->data = data;
2282	wd->effect_id = effect_id;
2283	wd->command = command;
2284	wd->size = size;
2285	memcpy(wd->params, params, size);
2286
2287	s = atomic_inc_return(&data->workqueue_size);
2288	queue_work(data->wq, &wd->work);
2289
2290	/* warn about excessive queue size */
2291	if (s >= 20 && s % 20 == 0)
2292		hid_warn(data->hidpp->hid_dev, "Force feedback command queue contains %d commands, causing substantial delays!", s);
2293
2294	return 0;
2295}
2296
2297static int hidpp_ff_upload_effect(struct input_dev *dev, struct ff_effect *effect, struct ff_effect *old)
2298{
2299	struct hidpp_ff_private_data *data = dev->ff->private;
2300	u8 params[20];
2301	u8 size;
2302	int force;
2303
2304	/* set common parameters */
2305	params[2] = effect->replay.length >> 8;
2306	params[3] = effect->replay.length & 255;
2307	params[4] = effect->replay.delay >> 8;
2308	params[5] = effect->replay.delay & 255;
2309
2310	switch (effect->type) {
2311	case FF_CONSTANT:
2312		force = (effect->u.constant.level * fixp_sin16((effect->direction * 360) >> 16)) >> 15;
2313		params[1] = HIDPP_FF_EFFECT_CONSTANT;
2314		params[6] = force >> 8;
2315		params[7] = force & 255;
2316		params[8] = effect->u.constant.envelope.attack_level >> 7;
2317		params[9] = effect->u.constant.envelope.attack_length >> 8;
2318		params[10] = effect->u.constant.envelope.attack_length & 255;
2319		params[11] = effect->u.constant.envelope.fade_level >> 7;
2320		params[12] = effect->u.constant.envelope.fade_length >> 8;
2321		params[13] = effect->u.constant.envelope.fade_length & 255;
2322		size = 14;
2323		dbg_hid("Uploading constant force level=%d in dir %d = %d\n",
2324				effect->u.constant.level,
2325				effect->direction, force);
2326		dbg_hid("          envelope attack=(%d, %d ms) fade=(%d, %d ms)\n",
2327				effect->u.constant.envelope.attack_level,
2328				effect->u.constant.envelope.attack_length,
2329				effect->u.constant.envelope.fade_level,
2330				effect->u.constant.envelope.fade_length);
2331		break;
2332	case FF_PERIODIC:
2333	{
2334		switch (effect->u.periodic.waveform) {
2335		case FF_SINE:
2336			params[1] = HIDPP_FF_EFFECT_PERIODIC_SINE;
2337			break;
2338		case FF_SQUARE:
2339			params[1] = HIDPP_FF_EFFECT_PERIODIC_SQUARE;
2340			break;
2341		case FF_SAW_UP:
2342			params[1] = HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHUP;
2343			break;
2344		case FF_SAW_DOWN:
2345			params[1] = HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHDOWN;
2346			break;
2347		case FF_TRIANGLE:
2348			params[1] = HIDPP_FF_EFFECT_PERIODIC_TRIANGLE;
2349			break;
2350		default:
2351			hid_err(data->hidpp->hid_dev, "Unexpected periodic waveform type %i!\n", effect->u.periodic.waveform);
2352			return -EINVAL;
2353		}
2354		force = (effect->u.periodic.magnitude * fixp_sin16((effect->direction * 360) >> 16)) >> 15;
2355		params[6] = effect->u.periodic.magnitude >> 8;
2356		params[7] = effect->u.periodic.magnitude & 255;
2357		params[8] = effect->u.periodic.offset >> 8;
2358		params[9] = effect->u.periodic.offset & 255;
2359		params[10] = effect->u.periodic.period >> 8;
2360		params[11] = effect->u.periodic.period & 255;
2361		params[12] = effect->u.periodic.phase >> 8;
2362		params[13] = effect->u.periodic.phase & 255;
2363		params[14] = effect->u.periodic.envelope.attack_level >> 7;
2364		params[15] = effect->u.periodic.envelope.attack_length >> 8;
2365		params[16] = effect->u.periodic.envelope.attack_length & 255;
2366		params[17] = effect->u.periodic.envelope.fade_level >> 7;
2367		params[18] = effect->u.periodic.envelope.fade_length >> 8;
2368		params[19] = effect->u.periodic.envelope.fade_length & 255;
2369		size = 20;
2370		dbg_hid("Uploading periodic force mag=%d/dir=%d, offset=%d, period=%d ms, phase=%d\n",
2371				effect->u.periodic.magnitude, effect->direction,
2372				effect->u.periodic.offset,
2373				effect->u.periodic.period,
2374				effect->u.periodic.phase);
2375		dbg_hid("          envelope attack=(%d, %d ms) fade=(%d, %d ms)\n",
2376				effect->u.periodic.envelope.attack_level,
2377				effect->u.periodic.envelope.attack_length,
2378				effect->u.periodic.envelope.fade_level,
2379				effect->u.periodic.envelope.fade_length);
2380		break;
2381	}
2382	case FF_RAMP:
2383		params[1] = HIDPP_FF_EFFECT_RAMP;
2384		force = (effect->u.ramp.start_level * fixp_sin16((effect->direction * 360) >> 16)) >> 15;
2385		params[6] = force >> 8;
2386		params[7] = force & 255;
2387		force = (effect->u.ramp.end_level * fixp_sin16((effect->direction * 360) >> 16)) >> 15;
2388		params[8] = force >> 8;
2389		params[9] = force & 255;
2390		params[10] = effect->u.ramp.envelope.attack_level >> 7;
2391		params[11] = effect->u.ramp.envelope.attack_length >> 8;
2392		params[12] = effect->u.ramp.envelope.attack_length & 255;
2393		params[13] = effect->u.ramp.envelope.fade_level >> 7;
2394		params[14] = effect->u.ramp.envelope.fade_length >> 8;
2395		params[15] = effect->u.ramp.envelope.fade_length & 255;
2396		size = 16;
2397		dbg_hid("Uploading ramp force level=%d -> %d in dir %d = %d\n",
2398				effect->u.ramp.start_level,
2399				effect->u.ramp.end_level,
2400				effect->direction, force);
2401		dbg_hid("          envelope attack=(%d, %d ms) fade=(%d, %d ms)\n",
2402				effect->u.ramp.envelope.attack_level,
2403				effect->u.ramp.envelope.attack_length,
2404				effect->u.ramp.envelope.fade_level,
2405				effect->u.ramp.envelope.fade_length);
2406		break;
2407	case FF_FRICTION:
2408	case FF_INERTIA:
2409	case FF_SPRING:
2410	case FF_DAMPER:
2411		params[1] = HIDPP_FF_CONDITION_CMDS[effect->type - FF_SPRING];
2412		params[6] = effect->u.condition[0].left_saturation >> 9;
2413		params[7] = (effect->u.condition[0].left_saturation >> 1) & 255;
2414		params[8] = effect->u.condition[0].left_coeff >> 8;
2415		params[9] = effect->u.condition[0].left_coeff & 255;
2416		params[10] = effect->u.condition[0].deadband >> 9;
2417		params[11] = (effect->u.condition[0].deadband >> 1) & 255;
2418		params[12] = effect->u.condition[0].center >> 8;
2419		params[13] = effect->u.condition[0].center & 255;
2420		params[14] = effect->u.condition[0].right_coeff >> 8;
2421		params[15] = effect->u.condition[0].right_coeff & 255;
2422		params[16] = effect->u.condition[0].right_saturation >> 9;
2423		params[17] = (effect->u.condition[0].right_saturation >> 1) & 255;
2424		size = 18;
2425		dbg_hid("Uploading %s force left coeff=%d, left sat=%d, right coeff=%d, right sat=%d\n",
2426				HIDPP_FF_CONDITION_NAMES[effect->type - FF_SPRING],
2427				effect->u.condition[0].left_coeff,
2428				effect->u.condition[0].left_saturation,
2429				effect->u.condition[0].right_coeff,
2430				effect->u.condition[0].right_saturation);
2431		dbg_hid("          deadband=%d, center=%d\n",
2432				effect->u.condition[0].deadband,
2433				effect->u.condition[0].center);
2434		break;
2435	default:
2436		hid_err(data->hidpp->hid_dev, "Unexpected force type %i!\n", effect->type);
2437		return -EINVAL;
2438	}
2439
2440	return hidpp_ff_queue_work(data, effect->id, HIDPP_FF_DOWNLOAD_EFFECT, params, size);
2441}
2442
2443static int hidpp_ff_playback(struct input_dev *dev, int effect_id, int value)
2444{
2445	struct hidpp_ff_private_data *data = dev->ff->private;
2446	u8 params[2];
2447
2448	params[1] = value ? HIDPP_FF_EFFECT_STATE_PLAY : HIDPP_FF_EFFECT_STATE_STOP;
2449
2450	dbg_hid("St%sing playback of effect %d.\n", value?"art":"opp", effect_id);
2451
2452	return hidpp_ff_queue_work(data, effect_id, HIDPP_FF_SET_EFFECT_STATE, params, ARRAY_SIZE(params));
2453}
2454
2455static int hidpp_ff_erase_effect(struct input_dev *dev, int effect_id)
2456{
2457	struct hidpp_ff_private_data *data = dev->ff->private;
2458	u8 slot = 0;
2459
2460	dbg_hid("Erasing effect %d.\n", effect_id);
2461
2462	return hidpp_ff_queue_work(data, effect_id, HIDPP_FF_DESTROY_EFFECT, &slot, 1);
2463}
2464
2465static void hidpp_ff_set_autocenter(struct input_dev *dev, u16 magnitude)
2466{
2467	struct hidpp_ff_private_data *data = dev->ff->private;
2468	u8 params[HIDPP_AUTOCENTER_PARAMS_LENGTH];
2469
2470	dbg_hid("Setting autocenter to %d.\n", magnitude);
2471
2472	/* start a standard spring effect */
2473	params[1] = HIDPP_FF_EFFECT_SPRING | HIDPP_FF_EFFECT_AUTOSTART;
2474	/* zero delay and duration */
2475	params[2] = params[3] = params[4] = params[5] = 0;
2476	/* set coeff to 25% of saturation */
2477	params[8] = params[14] = magnitude >> 11;
2478	params[9] = params[15] = (magnitude >> 3) & 255;
2479	params[6] = params[16] = magnitude >> 9;
2480	params[7] = params[17] = (magnitude >> 1) & 255;
2481	/* zero deadband and center */
2482	params[10] = params[11] = params[12] = params[13] = 0;
2483
2484	hidpp_ff_queue_work(data, HIDPP_FF_EFFECTID_AUTOCENTER, HIDPP_FF_DOWNLOAD_EFFECT, params, ARRAY_SIZE(params));
2485}
2486
2487static void hidpp_ff_set_gain(struct input_dev *dev, u16 gain)
2488{
2489	struct hidpp_ff_private_data *data = dev->ff->private;
2490	u8 params[4];
2491
2492	dbg_hid("Setting gain to %d.\n", gain);
2493
2494	params[0] = gain >> 8;
2495	params[1] = gain & 255;
2496	params[2] = 0; /* no boost */
2497	params[3] = 0;
2498
2499	hidpp_ff_queue_work(data, HIDPP_FF_EFFECTID_NONE, HIDPP_FF_SET_GLOBAL_GAINS, params, ARRAY_SIZE(params));
2500}
2501
2502static ssize_t hidpp_ff_range_show(struct device *dev, struct device_attribute *attr, char *buf)
2503{
2504	struct hid_device *hid = to_hid_device(dev);
2505	struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
2506	struct input_dev *idev = hidinput->input;
2507	struct hidpp_ff_private_data *data = idev->ff->private;
2508
2509	return scnprintf(buf, PAGE_SIZE, "%u\n", data->range);
2510}
2511
2512static ssize_t hidpp_ff_range_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
2513{
2514	struct hid_device *hid = to_hid_device(dev);
2515	struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
2516	struct input_dev *idev = hidinput->input;
2517	struct hidpp_ff_private_data *data = idev->ff->private;
2518	u8 params[2];
2519	int range = simple_strtoul(buf, NULL, 10);
2520
2521	range = clamp(range, 180, 900);
2522
2523	params[0] = range >> 8;
2524	params[1] = range & 0x00FF;
2525
2526	hidpp_ff_queue_work(data, -1, HIDPP_FF_SET_APERTURE, params, ARRAY_SIZE(params));
2527
2528	return count;
2529}
2530
2531static DEVICE_ATTR(range, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH, hidpp_ff_range_show, hidpp_ff_range_store);
2532
2533static void hidpp_ff_destroy(struct ff_device *ff)
2534{
2535	struct hidpp_ff_private_data *data = ff->private;
2536	struct hid_device *hid = data->hidpp->hid_dev;
2537
2538	hid_info(hid, "Unloading HID++ force feedback.\n");
2539
2540	device_remove_file(&hid->dev, &dev_attr_range);
2541	destroy_workqueue(data->wq);
2542	kfree(data->effect_ids);
2543}
2544
2545static int hidpp_ff_init(struct hidpp_device *hidpp,
2546			 struct hidpp_ff_private_data *data)
2547{
2548	struct hid_device *hid = hidpp->hid_dev;
2549	struct hid_input *hidinput;
2550	struct input_dev *dev;
2551	struct usb_device_descriptor *udesc;
2552	u16 bcdDevice;
2553	struct ff_device *ff;
2554	int error, j, num_slots = data->num_effects;
2555	u8 version;
2556
2557	if (!hid_is_usb(hid)) {
2558		hid_err(hid, "device is not USB\n");
2559		return -ENODEV;
2560	}
2561
2562	if (list_empty(&hid->inputs)) {
2563		hid_err(hid, "no inputs found\n");
2564		return -ENODEV;
2565	}
2566	hidinput = list_entry(hid->inputs.next, struct hid_input, list);
2567	dev = hidinput->input;
2568
2569	if (!dev) {
2570		hid_err(hid, "Struct input_dev not set!\n");
2571		return -EINVAL;
2572	}
2573
2574	/* Get firmware release */
2575	udesc = &(hid_to_usb_dev(hid)->descriptor);
2576	bcdDevice = le16_to_cpu(udesc->bcdDevice);
2577	version = bcdDevice & 255;
2578
2579	/* Set supported force feedback capabilities */
2580	for (j = 0; hidpp_ff_effects[j] >= 0; j++)
2581		set_bit(hidpp_ff_effects[j], dev->ffbit);
2582	if (version > 1)
2583		for (j = 0; hidpp_ff_effects_v2[j] >= 0; j++)
2584			set_bit(hidpp_ff_effects_v2[j], dev->ffbit);
2585
2586	error = input_ff_create(dev, num_slots);
2587
2588	if (error) {
2589		hid_err(dev, "Failed to create FF device!\n");
2590		return error;
2591	}
2592	/*
2593	 * Create a copy of passed data, so we can transfer memory
2594	 * ownership to FF core
2595	 */
2596	data = kmemdup(data, sizeof(*data), GFP_KERNEL);
2597	if (!data)
2598		return -ENOMEM;
2599	data->effect_ids = kcalloc(num_slots, sizeof(int), GFP_KERNEL);
2600	if (!data->effect_ids) {
2601		kfree(data);
2602		return -ENOMEM;
2603	}
2604	data->wq = create_singlethread_workqueue("hidpp-ff-sendqueue");
2605	if (!data->wq) {
2606		kfree(data->effect_ids);
2607		kfree(data);
2608		return -ENOMEM;
2609	}
2610
2611	data->hidpp = hidpp;
2612	data->version = version;
2613	for (j = 0; j < num_slots; j++)
2614		data->effect_ids[j] = -1;
2615
2616	ff = dev->ff;
2617	ff->private = data;
2618
2619	ff->upload = hidpp_ff_upload_effect;
2620	ff->erase = hidpp_ff_erase_effect;
2621	ff->playback = hidpp_ff_playback;
2622	ff->set_gain = hidpp_ff_set_gain;
2623	ff->set_autocenter = hidpp_ff_set_autocenter;
2624	ff->destroy = hidpp_ff_destroy;
2625
2626	/* Create sysfs interface */
2627	error = device_create_file(&(hidpp->hid_dev->dev), &dev_attr_range);
2628	if (error)
2629		hid_warn(hidpp->hid_dev, "Unable to create sysfs interface for \"range\", errno %d!\n", error);
2630
2631	/* init the hardware command queue */
2632	atomic_set(&data->workqueue_size, 0);
2633
2634	hid_info(hid, "Force feedback support loaded (firmware release %d).\n",
2635		 version);
2636
2637	return 0;
2638}
2639
2640/* ************************************************************************** */
2641/*                                                                            */
2642/* Device Support                                                             */
2643/*                                                                            */
2644/* ************************************************************************** */
2645
2646/* -------------------------------------------------------------------------- */
2647/* Touchpad HID++ devices                                                     */
2648/* -------------------------------------------------------------------------- */
2649
2650#define WTP_MANUAL_RESOLUTION				39
2651
2652struct wtp_data {
2653	u16 x_size, y_size;
2654	u8 finger_count;
2655	u8 mt_feature_index;
2656	u8 button_feature_index;
2657	u8 maxcontacts;
2658	bool flip_y;
2659	unsigned int resolution;
2660};
2661
2662static int wtp_input_mapping(struct hid_device *hdev, struct hid_input *hi,
2663		struct hid_field *field, struct hid_usage *usage,
2664		unsigned long **bit, int *max)
2665{
2666	return -1;
2667}
2668
2669static void wtp_populate_input(struct hidpp_device *hidpp,
2670			       struct input_dev *input_dev)
2671{
2672	struct wtp_data *wd = hidpp->private_data;
2673
2674	__set_bit(EV_ABS, input_dev->evbit);
2675	__set_bit(EV_KEY, input_dev->evbit);
2676	__clear_bit(EV_REL, input_dev->evbit);
2677	__clear_bit(EV_LED, input_dev->evbit);
2678
2679	input_set_abs_params(input_dev, ABS_MT_POSITION_X, 0, wd->x_size, 0, 0);
2680	input_abs_set_res(input_dev, ABS_MT_POSITION_X, wd->resolution);
2681	input_set_abs_params(input_dev, ABS_MT_POSITION_Y, 0, wd->y_size, 0, 0);
2682	input_abs_set_res(input_dev, ABS_MT_POSITION_Y, wd->resolution);
2683
2684	/* Max pressure is not given by the devices, pick one */
2685	input_set_abs_params(input_dev, ABS_MT_PRESSURE, 0, 50, 0, 0);
2686
2687	input_set_capability(input_dev, EV_KEY, BTN_LEFT);
2688
2689	if (hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS)
2690		input_set_capability(input_dev, EV_KEY, BTN_RIGHT);
2691	else
2692		__set_bit(INPUT_PROP_BUTTONPAD, input_dev->propbit);
2693
2694	input_mt_init_slots(input_dev, wd->maxcontacts, INPUT_MT_POINTER |
2695		INPUT_MT_DROP_UNUSED);
2696}
2697
2698static void wtp_touch_event(struct hidpp_device *hidpp,
2699	struct hidpp_touchpad_raw_xy_finger *touch_report)
2700{
2701	struct wtp_data *wd = hidpp->private_data;
2702	int slot;
2703
2704	if (!touch_report->finger_id || touch_report->contact_type)
2705		/* no actual data */
2706		return;
2707
2708	slot = input_mt_get_slot_by_key(hidpp->input, touch_report->finger_id);
2709
2710	input_mt_slot(hidpp->input, slot);
2711	input_mt_report_slot_state(hidpp->input, MT_TOOL_FINGER,
2712					touch_report->contact_status);
2713	if (touch_report->contact_status) {
2714		input_event(hidpp->input, EV_ABS, ABS_MT_POSITION_X,
2715				touch_report->x);
2716		input_event(hidpp->input, EV_ABS, ABS_MT_POSITION_Y,
2717				wd->flip_y ? wd->y_size - touch_report->y :
2718					     touch_report->y);
2719		input_event(hidpp->input, EV_ABS, ABS_MT_PRESSURE,
2720				touch_report->area);
2721	}
2722}
2723
2724static void wtp_send_raw_xy_event(struct hidpp_device *hidpp,
2725		struct hidpp_touchpad_raw_xy *raw)
2726{
2727	int i;
2728
2729	for (i = 0; i < 2; i++)
2730		wtp_touch_event(hidpp, &(raw->fingers[i]));
2731
2732	if (raw->end_of_frame &&
2733	    !(hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS))
2734		input_event(hidpp->input, EV_KEY, BTN_LEFT, raw->button);
2735
2736	if (raw->end_of_frame || raw->finger_count <= 2) {
2737		input_mt_sync_frame(hidpp->input);
2738		input_sync(hidpp->input);
2739	}
2740}
2741
2742static int wtp_mouse_raw_xy_event(struct hidpp_device *hidpp, u8 *data)
2743{
2744	struct wtp_data *wd = hidpp->private_data;
2745	u8 c1_area = ((data[7] & 0xf) * (data[7] & 0xf) +
2746		      (data[7] >> 4) * (data[7] >> 4)) / 2;
2747	u8 c2_area = ((data[13] & 0xf) * (data[13] & 0xf) +
2748		      (data[13] >> 4) * (data[13] >> 4)) / 2;
2749	struct hidpp_touchpad_raw_xy raw = {
2750		.timestamp = data[1],
2751		.fingers = {
2752			{
2753				.contact_type = 0,
2754				.contact_status = !!data[7],
2755				.x = get_unaligned_le16(&data[3]),
2756				.y = get_unaligned_le16(&data[5]),
2757				.z = c1_area,
2758				.area = c1_area,
2759				.finger_id = data[2],
2760			}, {
2761				.contact_type = 0,
2762				.contact_status = !!data[13],
2763				.x = get_unaligned_le16(&data[9]),
2764				.y = get_unaligned_le16(&data[11]),
2765				.z = c2_area,
2766				.area = c2_area,
2767				.finger_id = data[8],
2768			}
2769		},
2770		.finger_count = wd->maxcontacts,
2771		.spurious_flag = 0,
2772		.end_of_frame = (data[0] >> 7) == 0,
2773		.button = data[0] & 0x01,
2774	};
2775
2776	wtp_send_raw_xy_event(hidpp, &raw);
2777
2778	return 1;
2779}
2780
2781static int wtp_raw_event(struct hid_device *hdev, u8 *data, int size)
2782{
2783	struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2784	struct wtp_data *wd = hidpp->private_data;
2785	struct hidpp_report *report = (struct hidpp_report *)data;
2786	struct hidpp_touchpad_raw_xy raw;
2787
2788	if (!wd || !hidpp->input)
2789		return 1;
2790
2791	switch (data[0]) {
2792	case 0x02:
2793		if (size < 2) {
2794			hid_err(hdev, "Received HID report of bad size (%d)",
2795				size);
2796			return 1;
2797		}
2798		if (hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS) {
2799			input_event(hidpp->input, EV_KEY, BTN_LEFT,
2800					!!(data[1] & 0x01));
2801			input_event(hidpp->input, EV_KEY, BTN_RIGHT,
2802					!!(data[1] & 0x02));
2803			input_sync(hidpp->input);
2804			return 0;
2805		} else {
2806			if (size < 21)
2807				return 1;
2808			return wtp_mouse_raw_xy_event(hidpp, &data[7]);
2809		}
2810	case REPORT_ID_HIDPP_LONG:
2811		/* size is already checked in hidpp_raw_event. */
2812		if ((report->fap.feature_index != wd->mt_feature_index) ||
2813		    (report->fap.funcindex_clientid != EVENT_TOUCHPAD_RAW_XY))
2814			return 1;
2815		hidpp_touchpad_raw_xy_event(hidpp, data + 4, &raw);
2816
2817		wtp_send_raw_xy_event(hidpp, &raw);
2818		return 0;
2819	}
2820
2821	return 0;
2822}
2823
2824static int wtp_get_config(struct hidpp_device *hidpp)
2825{
2826	struct wtp_data *wd = hidpp->private_data;
2827	struct hidpp_touchpad_raw_info raw_info = {0};
2828	u8 feature_type;
2829	int ret;
2830
2831	ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_TOUCHPAD_RAW_XY,
2832		&wd->mt_feature_index, &feature_type);
2833	if (ret)
2834		/* means that the device is not powered up */
2835		return ret;
2836
2837	ret = hidpp_touchpad_get_raw_info(hidpp, wd->mt_feature_index,
2838		&raw_info);
2839	if (ret)
2840		return ret;
2841
2842	wd->x_size = raw_info.x_size;
2843	wd->y_size = raw_info.y_size;
2844	wd->maxcontacts = raw_info.maxcontacts;
2845	wd->flip_y = raw_info.origin == TOUCHPAD_RAW_XY_ORIGIN_LOWER_LEFT;
2846	wd->resolution = raw_info.res;
2847	if (!wd->resolution)
2848		wd->resolution = WTP_MANUAL_RESOLUTION;
2849
2850	return 0;
2851}
2852
2853static int wtp_allocate(struct hid_device *hdev, const struct hid_device_id *id)
2854{
2855	struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2856	struct wtp_data *wd;
2857
2858	wd = devm_kzalloc(&hdev->dev, sizeof(struct wtp_data),
2859			GFP_KERNEL);
2860	if (!wd)
2861		return -ENOMEM;
2862
2863	hidpp->private_data = wd;
2864
2865	return 0;
2866};
2867
2868static int wtp_connect(struct hid_device *hdev, bool connected)
2869{
2870	struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2871	struct wtp_data *wd = hidpp->private_data;
2872	int ret;
2873
2874	if (!wd->x_size) {
2875		ret = wtp_get_config(hidpp);
2876		if (ret) {
2877			hid_err(hdev, "Can not get wtp config: %d\n", ret);
2878			return ret;
2879		}
2880	}
2881
2882	return hidpp_touchpad_set_raw_report_state(hidpp, wd->mt_feature_index,
2883			true, true);
2884}
2885
2886/* ------------------------------------------------------------------------- */
2887/* Logitech M560 devices                                                     */
2888/* ------------------------------------------------------------------------- */
2889
2890/*
2891 * Logitech M560 protocol overview
2892 *
2893 * The Logitech M560 mouse, is designed for windows 8. When the middle and/or
2894 * the sides buttons are pressed, it sends some keyboard keys events
2895 * instead of buttons ones.
2896 * To complicate things further, the middle button keys sequence
2897 * is different from the odd press and the even press.
2898 *
2899 * forward button -> Super_R
2900 * backward button -> Super_L+'d' (press only)
2901 * middle button -> 1st time: Alt_L+SuperL+XF86TouchpadOff (press only)
2902 *                  2nd time: left-click (press only)
2903 * NB: press-only means that when the button is pressed, the
2904 * KeyPress/ButtonPress and KeyRelease/ButtonRelease events are generated
2905 * together sequentially; instead when the button is released, no event is
2906 * generated !
2907 *
2908 * With the command
2909 *	10<xx>0a 3500af03 (where <xx> is the mouse id),
2910 * the mouse reacts differently:
2911 * - it never sends a keyboard key event
2912 * - for the three mouse button it sends:
2913 *	middle button               press   11<xx>0a 3500af00...
2914 *	side 1 button (forward)     press   11<xx>0a 3500b000...
2915 *	side 2 button (backward)    press   11<xx>0a 3500ae00...
2916 *	middle/side1/side2 button   release 11<xx>0a 35000000...
2917 */
2918
2919static const u8 m560_config_parameter[] = {0x00, 0xaf, 0x03};
2920
2921/* how buttons are mapped in the report */
2922#define M560_MOUSE_BTN_LEFT		0x01
2923#define M560_MOUSE_BTN_RIGHT		0x02
2924#define M560_MOUSE_BTN_WHEEL_LEFT	0x08
2925#define M560_MOUSE_BTN_WHEEL_RIGHT	0x10
2926
2927#define M560_SUB_ID			0x0a
2928#define M560_BUTTON_MODE_REGISTER	0x35
2929
2930static int m560_send_config_command(struct hid_device *hdev, bool connected)
2931{
2932	struct hidpp_report response;
2933	struct hidpp_device *hidpp_dev;
2934
2935	hidpp_dev = hid_get_drvdata(hdev);
2936
2937	return hidpp_send_rap_command_sync(
2938		hidpp_dev,
2939		REPORT_ID_HIDPP_SHORT,
2940		M560_SUB_ID,
2941		M560_BUTTON_MODE_REGISTER,
2942		(u8 *)m560_config_parameter,
2943		sizeof(m560_config_parameter),
2944		&response
2945	);
2946}
2947
2948static int m560_raw_event(struct hid_device *hdev, u8 *data, int size)
2949{
2950	struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2951
2952	/* sanity check */
2953	if (!hidpp->input) {
2954		hid_err(hdev, "error in parameter\n");
2955		return -EINVAL;
2956	}
2957
2958	if (size < 7) {
2959		hid_err(hdev, "error in report\n");
2960		return 0;
2961	}
2962
2963	if (data[0] == REPORT_ID_HIDPP_LONG &&
2964	    data[2] == M560_SUB_ID && data[6] == 0x00) {
2965		/*
2966		 * m560 mouse report for middle, forward and backward button
2967		 *
2968		 * data[0] = 0x11
2969		 * data[1] = device-id
2970		 * data[2] = 0x0a
2971		 * data[5] = 0xaf -> middle
2972		 *	     0xb0 -> forward
2973		 *	     0xae -> backward
2974		 *	     0x00 -> release all
2975		 * data[6] = 0x00
2976		 */
2977
2978		switch (data[5]) {
2979		case 0xaf:
2980			input_report_key(hidpp->input, BTN_MIDDLE, 1);
2981			break;
2982		case 0xb0:
2983			input_report_key(hidpp->input, BTN_FORWARD, 1);
2984			break;
2985		case 0xae:
2986			input_report_key(hidpp->input, BTN_BACK, 1);
2987			break;
2988		case 0x00:
2989			input_report_key(hidpp->input, BTN_BACK, 0);
2990			input_report_key(hidpp->input, BTN_FORWARD, 0);
2991			input_report_key(hidpp->input, BTN_MIDDLE, 0);
2992			break;
2993		default:
2994			hid_err(hdev, "error in report\n");
2995			return 0;
2996		}
2997		input_sync(hidpp->input);
2998
2999	} else if (data[0] == 0x02) {
3000		/*
3001		 * Logitech M560 mouse report
3002		 *
3003		 * data[0] = type (0x02)
3004		 * data[1..2] = buttons
3005		 * data[3..5] = xy
3006		 * data[6] = wheel
3007		 */
3008
3009		int v;
3010
3011		input_report_key(hidpp->input, BTN_LEFT,
3012			!!(data[1] & M560_MOUSE_BTN_LEFT));
3013		input_report_key(hidpp->input, BTN_RIGHT,
3014			!!(data[1] & M560_MOUSE_BTN_RIGHT));
3015
3016		if (data[1] & M560_MOUSE_BTN_WHEEL_LEFT) {
3017			input_report_rel(hidpp->input, REL_HWHEEL, -1);
3018			input_report_rel(hidpp->input, REL_HWHEEL_HI_RES,
3019					 -120);
3020		} else if (data[1] & M560_MOUSE_BTN_WHEEL_RIGHT) {
3021			input_report_rel(hidpp->input, REL_HWHEEL, 1);
3022			input_report_rel(hidpp->input, REL_HWHEEL_HI_RES,
3023					 120);
3024		}
3025
3026		v = hid_snto32(hid_field_extract(hdev, data+3, 0, 12), 12);
3027		input_report_rel(hidpp->input, REL_X, v);
3028
3029		v = hid_snto32(hid_field_extract(hdev, data+3, 12, 12), 12);
3030		input_report_rel(hidpp->input, REL_Y, v);
3031
3032		v = hid_snto32(data[6], 8);
3033		if (v != 0)
3034			hidpp_scroll_counter_handle_scroll(hidpp->input,
3035					&hidpp->vertical_wheel_counter, v);
3036
3037		input_sync(hidpp->input);
3038	}
3039
3040	return 1;
3041}
3042
3043static void m560_populate_input(struct hidpp_device *hidpp,
3044				struct input_dev *input_dev)
3045{
3046	__set_bit(EV_KEY, input_dev->evbit);
3047	__set_bit(BTN_MIDDLE, input_dev->keybit);
3048	__set_bit(BTN_RIGHT, input_dev->keybit);
3049	__set_bit(BTN_LEFT, input_dev->keybit);
3050	__set_bit(BTN_BACK, input_dev->keybit);
3051	__set_bit(BTN_FORWARD, input_dev->keybit);
3052
3053	__set_bit(EV_REL, input_dev->evbit);
3054	__set_bit(REL_X, input_dev->relbit);
3055	__set_bit(REL_Y, input_dev->relbit);
3056	__set_bit(REL_WHEEL, input_dev->relbit);
3057	__set_bit(REL_HWHEEL, input_dev->relbit);
3058	__set_bit(REL_WHEEL_HI_RES, input_dev->relbit);
3059	__set_bit(REL_HWHEEL_HI_RES, input_dev->relbit);
3060}
3061
3062static int m560_input_mapping(struct hid_device *hdev, struct hid_input *hi,
3063		struct hid_field *field, struct hid_usage *usage,
3064		unsigned long **bit, int *max)
3065{
3066	return -1;
3067}
3068
3069/* ------------------------------------------------------------------------- */
3070/* Logitech K400 devices                                                     */
3071/* ------------------------------------------------------------------------- */
3072
3073/*
3074 * The Logitech K400 keyboard has an embedded touchpad which is seen
3075 * as a mouse from the OS point of view. There is a hardware shortcut to disable
3076 * tap-to-click but the setting is not remembered accross reset, annoying some
3077 * users.
3078 *
3079 * We can toggle this feature from the host by using the feature 0x6010:
3080 * Touchpad FW items
3081 */
3082
3083struct k400_private_data {
3084	u8 feature_index;
3085};
3086
3087static int k400_disable_tap_to_click(struct hidpp_device *hidpp)
3088{
3089	struct k400_private_data *k400 = hidpp->private_data;
3090	struct hidpp_touchpad_fw_items items = {};
3091	int ret;
3092	u8 feature_type;
3093
3094	if (!k400->feature_index) {
3095		ret = hidpp_root_get_feature(hidpp,
3096			HIDPP_PAGE_TOUCHPAD_FW_ITEMS,
3097			&k400->feature_index, &feature_type);
3098		if (ret)
3099			/* means that the device is not powered up */
3100			return ret;
3101	}
3102
3103	ret = hidpp_touchpad_fw_items_set(hidpp, k400->feature_index, &items);
3104	if (ret)
3105		return ret;
3106
3107	return 0;
3108}
3109
3110static int k400_allocate(struct hid_device *hdev)
3111{
3112	struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3113	struct k400_private_data *k400;
3114
3115	k400 = devm_kzalloc(&hdev->dev, sizeof(struct k400_private_data),
3116			    GFP_KERNEL);
3117	if (!k400)
3118		return -ENOMEM;
3119
3120	hidpp->private_data = k400;
3121
3122	return 0;
3123};
3124
3125static int k400_connect(struct hid_device *hdev, bool connected)
3126{
3127	struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3128
3129	if (!disable_tap_to_click)
3130		return 0;
3131
3132	return k400_disable_tap_to_click(hidpp);
3133}
3134
3135/* ------------------------------------------------------------------------- */
3136/* Logitech G920 Driving Force Racing Wheel for Xbox One                     */
3137/* ------------------------------------------------------------------------- */
3138
3139#define HIDPP_PAGE_G920_FORCE_FEEDBACK			0x8123
3140
3141static int g920_ff_set_autocenter(struct hidpp_device *hidpp,
3142				  struct hidpp_ff_private_data *data)
3143{
3144	struct hidpp_report response;
3145	u8 params[HIDPP_AUTOCENTER_PARAMS_LENGTH] = {
3146		[1] = HIDPP_FF_EFFECT_SPRING | HIDPP_FF_EFFECT_AUTOSTART,
3147	};
3148	int ret;
3149
3150	/* initialize with zero autocenter to get wheel in usable state */
3151
3152	dbg_hid("Setting autocenter to 0.\n");
3153	ret = hidpp_send_fap_command_sync(hidpp, data->feature_index,
3154					  HIDPP_FF_DOWNLOAD_EFFECT,
3155					  params, ARRAY_SIZE(params),
3156					  &response);
3157	if (ret)
3158		hid_warn(hidpp->hid_dev, "Failed to autocenter device!\n");
3159	else
3160		data->slot_autocenter = response.fap.params[0];
3161
3162	return ret;
3163}
3164
3165static int g920_get_config(struct hidpp_device *hidpp,
3166			   struct hidpp_ff_private_data *data)
3167{
3168	struct hidpp_report response;
3169	u8 feature_type;
3170	int ret;
3171
3172	memset(data, 0, sizeof(*data));
3173
3174	/* Find feature and store for later use */
3175	ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_G920_FORCE_FEEDBACK,
3176				     &data->feature_index, &feature_type);
3177	if (ret)
3178		return ret;
3179
3180	/* Read number of slots available in device */
3181	ret = hidpp_send_fap_command_sync(hidpp, data->feature_index,
3182					  HIDPP_FF_GET_INFO,
3183					  NULL, 0,
3184					  &response);
3185	if (ret) {
3186		if (ret < 0)
3187			return ret;
3188		hid_err(hidpp->hid_dev,
3189			"%s: received protocol error 0x%02x\n", __func__, ret);
3190		return -EPROTO;
3191	}
3192
3193	data->num_effects = response.fap.params[0] - HIDPP_FF_RESERVED_SLOTS;
3194
3195	/* reset all forces */
3196	ret = hidpp_send_fap_command_sync(hidpp, data->feature_index,
3197					  HIDPP_FF_RESET_ALL,
3198					  NULL, 0,
3199					  &response);
3200	if (ret)
3201		hid_warn(hidpp->hid_dev, "Failed to reset all forces!\n");
3202
3203	ret = hidpp_send_fap_command_sync(hidpp, data->feature_index,
3204					  HIDPP_FF_GET_APERTURE,
3205					  NULL, 0,
3206					  &response);
3207	if (ret) {
3208		hid_warn(hidpp->hid_dev,
3209			 "Failed to read range from device!\n");
3210	}
3211	data->range = ret ?
3212		900 : get_unaligned_be16(&response.fap.params[0]);
3213
3214	/* Read the current gain values */
3215	ret = hidpp_send_fap_command_sync(hidpp, data->feature_index,
3216					  HIDPP_FF_GET_GLOBAL_GAINS,
3217					  NULL, 0,
3218					  &response);
3219	if (ret)
3220		hid_warn(hidpp->hid_dev,
3221			 "Failed to read gain values from device!\n");
3222	data->gain = ret ?
3223		0xffff : get_unaligned_be16(&response.fap.params[0]);
3224
3225	/* ignore boost value at response.fap.params[2] */
3226
3227	return g920_ff_set_autocenter(hidpp, data);
3228}
3229
3230/* -------------------------------------------------------------------------- */
3231/* Logitech Dinovo Mini keyboard with builtin touchpad                        */
3232/* -------------------------------------------------------------------------- */
3233#define DINOVO_MINI_PRODUCT_ID		0xb30c
3234
3235static int lg_dinovo_input_mapping(struct hid_device *hdev, struct hid_input *hi,
3236		struct hid_field *field, struct hid_usage *usage,
3237		unsigned long **bit, int *max)
3238{
3239	if ((usage->hid & HID_USAGE_PAGE) != HID_UP_LOGIVENDOR)
3240		return 0;
3241
3242	switch (usage->hid & HID_USAGE) {
3243	case 0x00d: lg_map_key_clear(KEY_MEDIA);	break;
3244	default:
3245		return 0;
3246	}
3247	return 1;
3248}
3249
3250/* -------------------------------------------------------------------------- */
3251/* HID++1.0 devices which use HID++ reports for their wheels                  */
3252/* -------------------------------------------------------------------------- */
3253static int hidpp10_wheel_connect(struct hidpp_device *hidpp)
3254{
3255	return hidpp10_set_register(hidpp, HIDPP_REG_ENABLE_REPORTS, 0,
3256			HIDPP_ENABLE_WHEEL_REPORT | HIDPP_ENABLE_HWHEEL_REPORT,
3257			HIDPP_ENABLE_WHEEL_REPORT | HIDPP_ENABLE_HWHEEL_REPORT);
3258}
3259
3260static int hidpp10_wheel_raw_event(struct hidpp_device *hidpp,
3261				   u8 *data, int size)
3262{
3263	s8 value, hvalue;
3264
3265	if (!hidpp->input)
3266		return -EINVAL;
3267
3268	if (size < 7)
3269		return 0;
3270
3271	if (data[0] != REPORT_ID_HIDPP_SHORT || data[2] != HIDPP_SUB_ID_ROLLER)
3272		return 0;
3273
3274	value = data[3];
3275	hvalue = data[4];
3276
3277	input_report_rel(hidpp->input, REL_WHEEL, value);
3278	input_report_rel(hidpp->input, REL_WHEEL_HI_RES, value * 120);
3279	input_report_rel(hidpp->input, REL_HWHEEL, hvalue);
3280	input_report_rel(hidpp->input, REL_HWHEEL_HI_RES, hvalue * 120);
3281	input_sync(hidpp->input);
3282
3283	return 1;
3284}
3285
3286static void hidpp10_wheel_populate_input(struct hidpp_device *hidpp,
3287					 struct input_dev *input_dev)
3288{
3289	__set_bit(EV_REL, input_dev->evbit);
3290	__set_bit(REL_WHEEL, input_dev->relbit);
3291	__set_bit(REL_WHEEL_HI_RES, input_dev->relbit);
3292	__set_bit(REL_HWHEEL, input_dev->relbit);
3293	__set_bit(REL_HWHEEL_HI_RES, input_dev->relbit);
3294}
3295
3296/* -------------------------------------------------------------------------- */
3297/* HID++1.0 mice which use HID++ reports for extra mouse buttons              */
3298/* -------------------------------------------------------------------------- */
3299static int hidpp10_extra_mouse_buttons_connect(struct hidpp_device *hidpp)
3300{
3301	return hidpp10_set_register(hidpp, HIDPP_REG_ENABLE_REPORTS, 0,
3302				    HIDPP_ENABLE_MOUSE_EXTRA_BTN_REPORT,
3303				    HIDPP_ENABLE_MOUSE_EXTRA_BTN_REPORT);
3304}
3305
3306static int hidpp10_extra_mouse_buttons_raw_event(struct hidpp_device *hidpp,
3307				    u8 *data, int size)
3308{
3309	int i;
3310
3311	if (!hidpp->input)
3312		return -EINVAL;
3313
3314	if (size < 7)
3315		return 0;
3316
3317	if (data[0] != REPORT_ID_HIDPP_SHORT ||
3318	    data[2] != HIDPP_SUB_ID_MOUSE_EXTRA_BTNS)
3319		return 0;
3320
3321	/*
3322	 * Buttons are either delivered through the regular mouse report *or*
3323	 * through the extra buttons report. At least for button 6 how it is
3324	 * delivered differs per receiver firmware version. Even receivers with
3325	 * the same usb-id show different behavior, so we handle both cases.
3326	 */
3327	for (i = 0; i < 8; i++)
3328		input_report_key(hidpp->input, BTN_MOUSE + i,
3329				 (data[3] & (1 << i)));
3330
3331	/* Some mice report events on button 9+, use BTN_MISC */
3332	for (i = 0; i < 8; i++)
3333		input_report_key(hidpp->input, BTN_MISC + i,
3334				 (data[4] & (1 << i)));
3335
3336	input_sync(hidpp->input);
3337	return 1;
3338}
3339
3340static void hidpp10_extra_mouse_buttons_populate_input(
3341			struct hidpp_device *hidpp, struct input_dev *input_dev)
3342{
3343	/* BTN_MOUSE - BTN_MOUSE+7 are set already by the descriptor */
3344	__set_bit(BTN_0, input_dev->keybit);
3345	__set_bit(BTN_1, input_dev->keybit);
3346	__set_bit(BTN_2, input_dev->keybit);
3347	__set_bit(BTN_3, input_dev->keybit);
3348	__set_bit(BTN_4, input_dev->keybit);
3349	__set_bit(BTN_5, input_dev->keybit);
3350	__set_bit(BTN_6, input_dev->keybit);
3351	__set_bit(BTN_7, input_dev->keybit);
3352}
3353
3354/* -------------------------------------------------------------------------- */
3355/* HID++1.0 kbds which only report 0x10xx consumer usages through sub-id 0x03 */
3356/* -------------------------------------------------------------------------- */
3357
3358/* Find the consumer-page input report desc and change Maximums to 0x107f */
3359static u8 *hidpp10_consumer_keys_report_fixup(struct hidpp_device *hidpp,
3360					      u8 *_rdesc, unsigned int *rsize)
3361{
3362	/* Note 0 terminated so we can use strnstr to search for this. */
3363	static const char consumer_rdesc_start[] = {
3364		0x05, 0x0C,	/* USAGE_PAGE (Consumer Devices)       */
3365		0x09, 0x01,	/* USAGE (Consumer Control)            */
3366		0xA1, 0x01,	/* COLLECTION (Application)            */
3367		0x85, 0x03,	/* REPORT_ID = 3                       */
3368		0x75, 0x10,	/* REPORT_SIZE (16)                    */
3369		0x95, 0x02,	/* REPORT_COUNT (2)                    */
3370		0x15, 0x01,	/* LOGICAL_MIN (1)                     */
3371		0x26, 0x00	/* LOGICAL_MAX (...                    */
3372	};
3373	char *consumer_rdesc, *rdesc = (char *)_rdesc;
3374	unsigned int size;
3375
3376	consumer_rdesc = strnstr(rdesc, consumer_rdesc_start, *rsize);
3377	size = *rsize - (consumer_rdesc - rdesc);
3378	if (consumer_rdesc && size >= 25) {
3379		consumer_rdesc[15] = 0x7f;
3380		consumer_rdesc[16] = 0x10;
3381		consumer_rdesc[20] = 0x7f;
3382		consumer_rdesc[21] = 0x10;
3383	}
3384	return _rdesc;
3385}
3386
3387static int hidpp10_consumer_keys_connect(struct hidpp_device *hidpp)
3388{
3389	return hidpp10_set_register(hidpp, HIDPP_REG_ENABLE_REPORTS, 0,
3390				    HIDPP_ENABLE_CONSUMER_REPORT,
3391				    HIDPP_ENABLE_CONSUMER_REPORT);
3392}
3393
3394static int hidpp10_consumer_keys_raw_event(struct hidpp_device *hidpp,
3395					   u8 *data, int size)
3396{
3397	u8 consumer_report[5];
3398
3399	if (size < 7)
3400		return 0;
3401
3402	if (data[0] != REPORT_ID_HIDPP_SHORT ||
3403	    data[2] != HIDPP_SUB_ID_CONSUMER_VENDOR_KEYS)
3404		return 0;
3405
3406	/*
3407	 * Build a normal consumer report (3) out of the data, this detour
3408	 * is necessary to get some keyboards to report their 0x10xx usages.
3409	 */
3410	consumer_report[0] = 0x03;
3411	memcpy(&consumer_report[1], &data[3], 4);
3412	/* We are called from atomic context */
3413	hid_report_raw_event(hidpp->hid_dev, HID_INPUT_REPORT,
3414			     consumer_report, 5, 1);
3415
3416	return 1;
3417}
3418
3419/* -------------------------------------------------------------------------- */
3420/* High-resolution scroll wheels                                              */
3421/* -------------------------------------------------------------------------- */
3422
3423static int hi_res_scroll_enable(struct hidpp_device *hidpp)
3424{
3425	int ret;
3426	u8 multiplier = 1;
3427
3428	if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP20_HI_RES_WHEEL) {
3429		ret = hidpp_hrw_set_wheel_mode(hidpp, false, true, false);
3430		if (ret == 0)
3431			ret = hidpp_hrw_get_wheel_capability(hidpp, &multiplier);
3432	} else if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP20_HI_RES_SCROLL) {
3433		ret = hidpp_hrs_set_highres_scrolling_mode(hidpp, true,
3434							   &multiplier);
3435	} else /* if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP10_FAST_SCROLL) */ {
3436		ret = hidpp10_enable_scrolling_acceleration(hidpp);
3437		multiplier = 8;
3438	}
3439	if (ret)
 
 
3440		return ret;
 
3441
3442	if (multiplier == 0)
 
 
3443		multiplier = 1;
 
3444
3445	hidpp->vertical_wheel_counter.wheel_multiplier = multiplier;
3446	hid_dbg(hidpp->hid_dev, "wheel multiplier = %d\n", multiplier);
3447	return 0;
3448}
3449
3450static int hidpp_initialize_hires_scroll(struct hidpp_device *hidpp)
3451{
3452	int ret;
3453	unsigned long capabilities;
3454
3455	capabilities = hidpp->capabilities;
3456
3457	if (hidpp->protocol_major >= 2) {
3458		u8 feature_index;
3459		u8 feature_type;
3460
3461		ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_HIRES_WHEEL,
3462					     &feature_index, &feature_type);
3463		if (!ret) {
3464			hidpp->capabilities |= HIDPP_CAPABILITY_HIDPP20_HI_RES_WHEEL;
3465			hid_dbg(hidpp->hid_dev, "Detected HID++ 2.0 hi-res scroll wheel\n");
3466			return 0;
3467		}
3468		ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_HI_RESOLUTION_SCROLLING,
3469					     &feature_index, &feature_type);
3470		if (!ret) {
3471			hidpp->capabilities |= HIDPP_CAPABILITY_HIDPP20_HI_RES_SCROLL;
3472			hid_dbg(hidpp->hid_dev, "Detected HID++ 2.0 hi-res scrolling\n");
3473		}
3474	} else {
3475		struct hidpp_report response;
3476
3477		ret = hidpp_send_rap_command_sync(hidpp,
3478						  REPORT_ID_HIDPP_SHORT,
3479						  HIDPP_GET_REGISTER,
3480						  HIDPP_ENABLE_FAST_SCROLL,
3481						  NULL, 0, &response);
3482		if (!ret) {
3483			hidpp->capabilities |= HIDPP_CAPABILITY_HIDPP10_FAST_SCROLL;
3484			hid_dbg(hidpp->hid_dev, "Detected HID++ 1.0 fast scroll\n");
3485		}
3486	}
3487
3488	if (hidpp->capabilities == capabilities)
3489		hid_dbg(hidpp->hid_dev, "Did not detect HID++ hi-res scrolling hardware support\n");
3490	return 0;
3491}
3492
3493/* -------------------------------------------------------------------------- */
3494/* Generic HID++ devices                                                      */
3495/* -------------------------------------------------------------------------- */
3496
3497static u8 *hidpp_report_fixup(struct hid_device *hdev, u8 *rdesc,
3498			      unsigned int *rsize)
3499{
3500	struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3501
3502	if (!hidpp)
3503		return rdesc;
3504
3505	/* For 27 MHz keyboards the quirk gets set after hid_parse. */
3506	if (hdev->group == HID_GROUP_LOGITECH_27MHZ_DEVICE ||
3507	    (hidpp->quirks & HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS))
3508		rdesc = hidpp10_consumer_keys_report_fixup(hidpp, rdesc, rsize);
3509
3510	return rdesc;
3511}
3512
3513static int hidpp_input_mapping(struct hid_device *hdev, struct hid_input *hi,
3514		struct hid_field *field, struct hid_usage *usage,
3515		unsigned long **bit, int *max)
3516{
3517	struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3518
3519	if (!hidpp)
3520		return 0;
3521
3522	if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
3523		return wtp_input_mapping(hdev, hi, field, usage, bit, max);
3524	else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560 &&
3525			field->application != HID_GD_MOUSE)
3526		return m560_input_mapping(hdev, hi, field, usage, bit, max);
3527
3528	if (hdev->product == DINOVO_MINI_PRODUCT_ID)
3529		return lg_dinovo_input_mapping(hdev, hi, field, usage, bit, max);
3530
3531	return 0;
3532}
3533
3534static int hidpp_input_mapped(struct hid_device *hdev, struct hid_input *hi,
3535		struct hid_field *field, struct hid_usage *usage,
3536		unsigned long **bit, int *max)
3537{
3538	struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3539
3540	if (!hidpp)
3541		return 0;
3542
3543	/* Ensure that Logitech G920 is not given a default fuzz/flat value */
3544	if (hidpp->quirks & HIDPP_QUIRK_CLASS_G920) {
3545		if (usage->type == EV_ABS && (usage->code == ABS_X ||
3546				usage->code == ABS_Y || usage->code == ABS_Z ||
3547				usage->code == ABS_RZ)) {
3548			field->application = HID_GD_MULTIAXIS;
3549		}
3550	}
3551
3552	return 0;
3553}
3554
3555
3556static void hidpp_populate_input(struct hidpp_device *hidpp,
3557				 struct input_dev *input)
3558{
3559	hidpp->input = input;
3560
3561	if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
3562		wtp_populate_input(hidpp, input);
3563	else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560)
3564		m560_populate_input(hidpp, input);
3565
3566	if (hidpp->quirks & HIDPP_QUIRK_HIDPP_WHEELS)
3567		hidpp10_wheel_populate_input(hidpp, input);
3568
3569	if (hidpp->quirks & HIDPP_QUIRK_HIDPP_EXTRA_MOUSE_BTNS)
3570		hidpp10_extra_mouse_buttons_populate_input(hidpp, input);
3571}
3572
3573static int hidpp_input_configured(struct hid_device *hdev,
3574				struct hid_input *hidinput)
3575{
3576	struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3577	struct input_dev *input = hidinput->input;
3578
3579	if (!hidpp)
3580		return 0;
3581
3582	hidpp_populate_input(hidpp, input);
3583
3584	return 0;
3585}
3586
3587static int hidpp_raw_hidpp_event(struct hidpp_device *hidpp, u8 *data,
3588		int size)
3589{
3590	struct hidpp_report *question = hidpp->send_receive_buf;
3591	struct hidpp_report *answer = hidpp->send_receive_buf;
3592	struct hidpp_report *report = (struct hidpp_report *)data;
3593	int ret;
3594
3595	/*
3596	 * If the mutex is locked then we have a pending answer from a
3597	 * previously sent command.
3598	 */
3599	if (unlikely(mutex_is_locked(&hidpp->send_mutex))) {
3600		/*
3601		 * Check for a correct hidpp20 answer or the corresponding
3602		 * error
3603		 */
3604		if (hidpp_match_answer(question, report) ||
3605				hidpp_match_error(question, report)) {
3606			*answer = *report;
3607			hidpp->answer_available = true;
3608			wake_up(&hidpp->wait);
3609			/*
3610			 * This was an answer to a command that this driver sent
3611			 * We return 1 to hid-core to avoid forwarding the
3612			 * command upstream as it has been treated by the driver
3613			 */
3614
3615			return 1;
3616		}
3617	}
3618
3619	if (unlikely(hidpp_report_is_connect_event(hidpp, report))) {
3620		atomic_set(&hidpp->connected,
3621				!(report->rap.params[0] & (1 << 6)));
3622		if (schedule_work(&hidpp->work) == 0)
3623			dbg_hid("%s: connect event already queued\n", __func__);
3624		return 1;
3625	}
3626
3627	if (hidpp->hid_dev->group == HID_GROUP_LOGITECH_27MHZ_DEVICE &&
3628	    data[0] == REPORT_ID_HIDPP_SHORT &&
3629	    data[2] == HIDPP_SUB_ID_USER_IFACE_EVENT &&
3630	    (data[3] & HIDPP_USER_IFACE_EVENT_ENCRYPTION_KEY_LOST)) {
3631		dev_err_ratelimited(&hidpp->hid_dev->dev,
3632			"Error the keyboard's wireless encryption key has been lost, your keyboard will not work unless you re-configure encryption.\n");
3633		dev_err_ratelimited(&hidpp->hid_dev->dev,
3634			"See: https://gitlab.freedesktop.org/jwrdegoede/logitech-27mhz-keyboard-encryption-setup/\n");
3635	}
3636
3637	if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP20_BATTERY) {
3638		ret = hidpp20_battery_event_1000(hidpp, data, size);
3639		if (ret != 0)
3640			return ret;
3641		ret = hidpp20_battery_event_1004(hidpp, data, size);
3642		if (ret != 0)
3643			return ret;
3644		ret = hidpp_solar_battery_event(hidpp, data, size);
3645		if (ret != 0)
3646			return ret;
3647		ret = hidpp20_battery_voltage_event(hidpp, data, size);
3648		if (ret != 0)
3649			return ret;
 
 
 
3650	}
3651
3652	if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP10_BATTERY) {
3653		ret = hidpp10_battery_event(hidpp, data, size);
3654		if (ret != 0)
3655			return ret;
3656	}
3657
3658	if (hidpp->quirks & HIDPP_QUIRK_HIDPP_WHEELS) {
3659		ret = hidpp10_wheel_raw_event(hidpp, data, size);
3660		if (ret != 0)
3661			return ret;
3662	}
3663
3664	if (hidpp->quirks & HIDPP_QUIRK_HIDPP_EXTRA_MOUSE_BTNS) {
3665		ret = hidpp10_extra_mouse_buttons_raw_event(hidpp, data, size);
3666		if (ret != 0)
3667			return ret;
3668	}
3669
3670	if (hidpp->quirks & HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS) {
3671		ret = hidpp10_consumer_keys_raw_event(hidpp, data, size);
3672		if (ret != 0)
3673			return ret;
3674	}
3675
3676	return 0;
3677}
3678
3679static int hidpp_raw_event(struct hid_device *hdev, struct hid_report *report,
3680		u8 *data, int size)
3681{
3682	struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3683	int ret = 0;
3684
3685	if (!hidpp)
3686		return 0;
3687
3688	/* Generic HID++ processing. */
3689	switch (data[0]) {
3690	case REPORT_ID_HIDPP_VERY_LONG:
3691		if (size != hidpp->very_long_report_length) {
3692			hid_err(hdev, "received hid++ report of bad size (%d)",
3693				size);
3694			return 1;
3695		}
3696		ret = hidpp_raw_hidpp_event(hidpp, data, size);
3697		break;
3698	case REPORT_ID_HIDPP_LONG:
3699		if (size != HIDPP_REPORT_LONG_LENGTH) {
3700			hid_err(hdev, "received hid++ report of bad size (%d)",
3701				size);
3702			return 1;
3703		}
3704		ret = hidpp_raw_hidpp_event(hidpp, data, size);
3705		break;
3706	case REPORT_ID_HIDPP_SHORT:
3707		if (size != HIDPP_REPORT_SHORT_LENGTH) {
3708			hid_err(hdev, "received hid++ report of bad size (%d)",
3709				size);
3710			return 1;
3711		}
3712		ret = hidpp_raw_hidpp_event(hidpp, data, size);
3713		break;
3714	}
3715
3716	/* If no report is available for further processing, skip calling
3717	 * raw_event of subclasses. */
3718	if (ret != 0)
3719		return ret;
3720
3721	if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
3722		return wtp_raw_event(hdev, data, size);
3723	else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560)
3724		return m560_raw_event(hdev, data, size);
3725
3726	return 0;
3727}
3728
3729static int hidpp_event(struct hid_device *hdev, struct hid_field *field,
3730	struct hid_usage *usage, __s32 value)
3731{
3732	/* This function will only be called for scroll events, due to the
3733	 * restriction imposed in hidpp_usages.
3734	 */
3735	struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3736	struct hidpp_scroll_counter *counter;
3737
3738	if (!hidpp)
3739		return 0;
3740
3741	counter = &hidpp->vertical_wheel_counter;
3742	/* A scroll event may occur before the multiplier has been retrieved or
3743	 * the input device set, or high-res scroll enabling may fail. In such
3744	 * cases we must return early (falling back to default behaviour) to
3745	 * avoid a crash in hidpp_scroll_counter_handle_scroll.
3746	 */
3747	if (!(hidpp->capabilities & HIDPP_CAPABILITY_HI_RES_SCROLL)
3748	    || value == 0 || hidpp->input == NULL
3749	    || counter->wheel_multiplier == 0)
3750		return 0;
3751
3752	hidpp_scroll_counter_handle_scroll(hidpp->input, counter, value);
3753	return 1;
3754}
3755
3756static int hidpp_initialize_battery(struct hidpp_device *hidpp)
3757{
3758	static atomic_t battery_no = ATOMIC_INIT(0);
3759	struct power_supply_config cfg = { .drv_data = hidpp };
3760	struct power_supply_desc *desc = &hidpp->battery.desc;
3761	enum power_supply_property *battery_props;
3762	struct hidpp_battery *battery;
3763	unsigned int num_battery_props;
3764	unsigned long n;
3765	int ret;
3766
3767	if (hidpp->battery.ps)
3768		return 0;
3769
3770	hidpp->battery.feature_index = 0xff;
3771	hidpp->battery.solar_feature_index = 0xff;
3772	hidpp->battery.voltage_feature_index = 0xff;
 
3773
3774	if (hidpp->protocol_major >= 2) {
3775		if (hidpp->quirks & HIDPP_QUIRK_CLASS_K750)
3776			ret = hidpp_solar_request_battery_event(hidpp);
3777		else {
3778			/* we only support one battery feature right now, so let's
3779			   first check the ones that support battery level first
3780			   and leave voltage for last */
3781			ret = hidpp20_query_battery_info_1000(hidpp);
3782			if (ret)
3783				ret = hidpp20_query_battery_info_1004(hidpp);
3784			if (ret)
3785				ret = hidpp20_query_battery_voltage_info(hidpp);
 
 
3786		}
3787
3788		if (ret)
3789			return ret;
3790		hidpp->capabilities |= HIDPP_CAPABILITY_HIDPP20_BATTERY;
3791	} else {
3792		ret = hidpp10_query_battery_status(hidpp);
3793		if (ret) {
3794			ret = hidpp10_query_battery_mileage(hidpp);
3795			if (ret)
3796				return -ENOENT;
3797			hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_MILEAGE;
3798		} else {
3799			hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS;
3800		}
3801		hidpp->capabilities |= HIDPP_CAPABILITY_HIDPP10_BATTERY;
3802	}
3803
3804	battery_props = devm_kmemdup(&hidpp->hid_dev->dev,
3805				     hidpp_battery_props,
3806				     sizeof(hidpp_battery_props),
3807				     GFP_KERNEL);
3808	if (!battery_props)
3809		return -ENOMEM;
3810
3811	num_battery_props = ARRAY_SIZE(hidpp_battery_props) - 3;
3812
3813	if (hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_MILEAGE ||
3814	    hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_PERCENTAGE ||
3815	    hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_VOLTAGE)
 
3816		battery_props[num_battery_props++] =
3817				POWER_SUPPLY_PROP_CAPACITY;
3818
3819	if (hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS)
3820		battery_props[num_battery_props++] =
3821				POWER_SUPPLY_PROP_CAPACITY_LEVEL;
3822
3823	if (hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_VOLTAGE)
 
3824		battery_props[num_battery_props++] =
3825			POWER_SUPPLY_PROP_VOLTAGE_NOW;
3826
3827	battery = &hidpp->battery;
3828
3829	n = atomic_inc_return(&battery_no) - 1;
3830	desc->properties = battery_props;
3831	desc->num_properties = num_battery_props;
3832	desc->get_property = hidpp_battery_get_property;
3833	sprintf(battery->name, "hidpp_battery_%ld", n);
3834	desc->name = battery->name;
3835	desc->type = POWER_SUPPLY_TYPE_BATTERY;
3836	desc->use_for_apm = 0;
3837
3838	battery->ps = devm_power_supply_register(&hidpp->hid_dev->dev,
3839						 &battery->desc,
3840						 &cfg);
3841	if (IS_ERR(battery->ps))
3842		return PTR_ERR(battery->ps);
3843
3844	power_supply_powers(battery->ps, &hidpp->hid_dev->dev);
3845
3846	return ret;
3847}
3848
3849static void hidpp_overwrite_name(struct hid_device *hdev)
 
3850{
3851	struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3852	char *name;
3853
3854	if (hidpp->protocol_major < 2)
3855		return;
 
3856
3857	name = hidpp_get_device_name(hidpp);
3858
3859	if (!name) {
3860		hid_err(hdev, "unable to retrieve the name of the device");
3861	} else {
3862		dbg_hid("HID++: Got name: %s\n", name);
3863		snprintf(hdev->name, sizeof(hdev->name), "%s", name);
 
3864	}
3865
3866	kfree(name);
3867}
3868
3869static int hidpp_input_open(struct input_dev *dev)
3870{
3871	struct hid_device *hid = input_get_drvdata(dev);
3872
3873	return hid_hw_open(hid);
3874}
3875
3876static void hidpp_input_close(struct input_dev *dev)
3877{
3878	struct hid_device *hid = input_get_drvdata(dev);
3879
3880	hid_hw_close(hid);
3881}
3882
3883static struct input_dev *hidpp_allocate_input(struct hid_device *hdev)
3884{
3885	struct input_dev *input_dev = devm_input_allocate_device(&hdev->dev);
3886	struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3887
3888	if (!input_dev)
3889		return NULL;
3890
3891	input_set_drvdata(input_dev, hdev);
3892	input_dev->open = hidpp_input_open;
3893	input_dev->close = hidpp_input_close;
3894
3895	input_dev->name = hidpp->name;
3896	input_dev->phys = hdev->phys;
3897	input_dev->uniq = hdev->uniq;
3898	input_dev->id.bustype = hdev->bus;
3899	input_dev->id.vendor  = hdev->vendor;
3900	input_dev->id.product = hdev->product;
3901	input_dev->id.version = hdev->version;
3902	input_dev->dev.parent = &hdev->dev;
3903
3904	return input_dev;
3905}
3906
3907static void hidpp_connect_event(struct hidpp_device *hidpp)
3908{
 
3909	struct hid_device *hdev = hidpp->hid_dev;
3910	int ret = 0;
3911	bool connected = atomic_read(&hidpp->connected);
3912	struct input_dev *input;
3913	char *name, *devm_name;
 
3914
3915	if (!connected) {
 
 
 
3916		if (hidpp->battery.ps) {
3917			hidpp->battery.online = false;
3918			hidpp->battery.status = POWER_SUPPLY_STATUS_UNKNOWN;
3919			hidpp->battery.level = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
3920			power_supply_changed(hidpp->battery.ps);
3921		}
3922		return;
3923	}
3924
3925	if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP) {
3926		ret = wtp_connect(hdev, connected);
3927		if (ret)
3928			return;
3929	} else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560) {
3930		ret = m560_send_config_command(hdev, connected);
3931		if (ret)
3932			return;
3933	} else if (hidpp->quirks & HIDPP_QUIRK_CLASS_K400) {
3934		ret = k400_connect(hdev, connected);
3935		if (ret)
3936			return;
3937	}
3938
3939	if (hidpp->quirks & HIDPP_QUIRK_HIDPP_WHEELS) {
3940		ret = hidpp10_wheel_connect(hidpp);
3941		if (ret)
3942			return;
3943	}
3944
3945	if (hidpp->quirks & HIDPP_QUIRK_HIDPP_EXTRA_MOUSE_BTNS) {
3946		ret = hidpp10_extra_mouse_buttons_connect(hidpp);
3947		if (ret)
3948			return;
3949	}
3950
3951	if (hidpp->quirks & HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS) {
3952		ret = hidpp10_consumer_keys_connect(hidpp);
3953		if (ret)
3954			return;
3955	}
3956
3957	/* the device is already connected, we can ask for its name and
3958	 * protocol */
3959	if (!hidpp->protocol_major) {
3960		ret = hidpp_root_get_protocol_version(hidpp);
3961		if (ret) {
3962			hid_err(hdev, "Can not get the protocol version.\n");
3963			return;
3964		}
3965	}
3966
3967	if (hidpp->name == hdev->name && hidpp->protocol_major >= 2) {
3968		name = hidpp_get_device_name(hidpp);
3969		if (name) {
3970			devm_name = devm_kasprintf(&hdev->dev, GFP_KERNEL,
3971						   "%s", name);
3972			kfree(name);
3973			if (!devm_name)
3974				return;
3975
3976			hidpp->name = devm_name;
3977		}
3978	}
3979
3980	hidpp_initialize_battery(hidpp);
3981	if (!hid_is_usb(hidpp->hid_dev))
3982		hidpp_initialize_hires_scroll(hidpp);
3983
3984	/* forward current battery state */
3985	if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP10_BATTERY) {
3986		hidpp10_enable_battery_reporting(hidpp);
3987		if (hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_MILEAGE)
3988			hidpp10_query_battery_mileage(hidpp);
3989		else
3990			hidpp10_query_battery_status(hidpp);
3991	} else if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP20_BATTERY) {
3992		if (hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_VOLTAGE)
3993			hidpp20_query_battery_voltage_info(hidpp);
3994		else if (hidpp->capabilities & HIDPP_CAPABILITY_UNIFIED_BATTERY)
3995			hidpp20_query_battery_info_1004(hidpp);
 
 
3996		else
3997			hidpp20_query_battery_info_1000(hidpp);
3998	}
3999	if (hidpp->battery.ps)
4000		power_supply_changed(hidpp->battery.ps);
4001
4002	if (hidpp->capabilities & HIDPP_CAPABILITY_HI_RES_SCROLL)
4003		hi_res_scroll_enable(hidpp);
4004
4005	if (!(hidpp->quirks & HIDPP_QUIRK_NO_HIDINPUT) || hidpp->delayed_input)
4006		/* if the input nodes are already created, we can stop now */
4007		return;
4008
4009	input = hidpp_allocate_input(hdev);
4010	if (!input) {
4011		hid_err(hdev, "cannot allocate new input device: %d\n", ret);
4012		return;
4013	}
4014
4015	hidpp_populate_input(hidpp, input);
4016
4017	ret = input_register_device(input);
4018	if (ret) {
4019		input_free_device(input);
4020		return;
4021	}
4022
4023	hidpp->delayed_input = input;
4024}
4025
4026static DEVICE_ATTR(builtin_power_supply, 0000, NULL, NULL);
4027
4028static struct attribute *sysfs_attrs[] = {
4029	&dev_attr_builtin_power_supply.attr,
4030	NULL
4031};
4032
4033static const struct attribute_group ps_attribute_group = {
4034	.attrs = sysfs_attrs
4035};
4036
4037static int hidpp_get_report_length(struct hid_device *hdev, int id)
4038{
4039	struct hid_report_enum *re;
4040	struct hid_report *report;
4041
4042	re = &(hdev->report_enum[HID_OUTPUT_REPORT]);
4043	report = re->report_id_hash[id];
4044	if (!report)
4045		return 0;
4046
4047	return report->field[0]->report_count + 1;
4048}
4049
4050static u8 hidpp_validate_device(struct hid_device *hdev)
4051{
4052	struct hidpp_device *hidpp = hid_get_drvdata(hdev);
4053	int id, report_length;
4054	u8 supported_reports = 0;
4055
4056	id = REPORT_ID_HIDPP_SHORT;
4057	report_length = hidpp_get_report_length(hdev, id);
4058	if (report_length) {
4059		if (report_length < HIDPP_REPORT_SHORT_LENGTH)
4060			goto bad_device;
4061
4062		supported_reports |= HIDPP_REPORT_SHORT_SUPPORTED;
4063	}
4064
4065	id = REPORT_ID_HIDPP_LONG;
4066	report_length = hidpp_get_report_length(hdev, id);
4067	if (report_length) {
4068		if (report_length < HIDPP_REPORT_LONG_LENGTH)
4069			goto bad_device;
4070
4071		supported_reports |= HIDPP_REPORT_LONG_SUPPORTED;
4072	}
4073
4074	id = REPORT_ID_HIDPP_VERY_LONG;
4075	report_length = hidpp_get_report_length(hdev, id);
4076	if (report_length) {
4077		if (report_length < HIDPP_REPORT_LONG_LENGTH ||
4078		    report_length > HIDPP_REPORT_VERY_LONG_MAX_LENGTH)
4079			goto bad_device;
4080
4081		supported_reports |= HIDPP_REPORT_VERY_LONG_SUPPORTED;
4082		hidpp->very_long_report_length = report_length;
4083	}
4084
4085	return supported_reports;
4086
4087bad_device:
4088	hid_warn(hdev, "not enough values in hidpp report %d\n", id);
4089	return false;
4090}
4091
4092static bool hidpp_application_equals(struct hid_device *hdev,
4093				     unsigned int application)
4094{
4095	struct list_head *report_list;
4096	struct hid_report *report;
4097
4098	report_list = &hdev->report_enum[HID_INPUT_REPORT].report_list;
4099	report = list_first_entry_or_null(report_list, struct hid_report, list);
4100	return report && report->application == application;
4101}
4102
4103static int hidpp_probe(struct hid_device *hdev, const struct hid_device_id *id)
4104{
4105	struct hidpp_device *hidpp;
4106	int ret;
4107	bool connected;
4108	unsigned int connect_mask = HID_CONNECT_DEFAULT;
4109	struct hidpp_ff_private_data data;
4110
4111	/* report_fixup needs drvdata to be set before we call hid_parse */
4112	hidpp = devm_kzalloc(&hdev->dev, sizeof(*hidpp), GFP_KERNEL);
4113	if (!hidpp)
4114		return -ENOMEM;
4115
4116	hidpp->hid_dev = hdev;
4117	hidpp->name = hdev->name;
4118	hidpp->quirks = id->driver_data;
4119	hid_set_drvdata(hdev, hidpp);
4120
4121	ret = hid_parse(hdev);
4122	if (ret) {
4123		hid_err(hdev, "%s:parse failed\n", __func__);
4124		return ret;
4125	}
4126
4127	/*
4128	 * Make sure the device is HID++ capable, otherwise treat as generic HID
4129	 */
4130	hidpp->supported_reports = hidpp_validate_device(hdev);
4131
4132	if (!hidpp->supported_reports) {
4133		hid_set_drvdata(hdev, NULL);
4134		devm_kfree(&hdev->dev, hidpp);
4135		return hid_hw_start(hdev, HID_CONNECT_DEFAULT);
4136	}
4137
4138	if (id->group == HID_GROUP_LOGITECH_DJ_DEVICE)
4139		hidpp->quirks |= HIDPP_QUIRK_UNIFYING;
4140
4141	if (id->group == HID_GROUP_LOGITECH_27MHZ_DEVICE &&
4142	    hidpp_application_equals(hdev, HID_GD_MOUSE))
4143		hidpp->quirks |= HIDPP_QUIRK_HIDPP_WHEELS |
4144				 HIDPP_QUIRK_HIDPP_EXTRA_MOUSE_BTNS;
4145
4146	if (id->group == HID_GROUP_LOGITECH_27MHZ_DEVICE &&
4147	    hidpp_application_equals(hdev, HID_GD_KEYBOARD))
4148		hidpp->quirks |= HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS;
4149
4150	if (disable_raw_mode) {
4151		hidpp->quirks &= ~HIDPP_QUIRK_CLASS_WTP;
4152		hidpp->quirks &= ~HIDPP_QUIRK_NO_HIDINPUT;
4153	}
4154
4155	if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP) {
4156		ret = wtp_allocate(hdev, id);
4157		if (ret)
4158			return ret;
4159	} else if (hidpp->quirks & HIDPP_QUIRK_CLASS_K400) {
4160		ret = k400_allocate(hdev);
4161		if (ret)
4162			return ret;
4163	}
4164
4165	INIT_WORK(&hidpp->work, delayed_work_cb);
4166	mutex_init(&hidpp->send_mutex);
4167	init_waitqueue_head(&hidpp->wait);
4168
4169	/* indicates we are handling the battery properties in the kernel */
4170	ret = sysfs_create_group(&hdev->dev.kobj, &ps_attribute_group);
4171	if (ret)
4172		hid_warn(hdev, "Cannot allocate sysfs group for %s\n",
4173			 hdev->name);
4174
4175	/*
4176	 * Plain USB connections need to actually call start and open
4177	 * on the transport driver to allow incoming data.
 
 
4178	 */
4179	ret = hid_hw_start(hdev, 0);
4180	if (ret) {
4181		hid_err(hdev, "hw start failed\n");
4182		goto hid_hw_start_fail;
4183	}
4184
4185	ret = hid_hw_open(hdev);
4186	if (ret < 0) {
4187		dev_err(&hdev->dev, "%s:hid_hw_open returned error:%d\n",
4188			__func__, ret);
4189		goto hid_hw_open_fail;
4190	}
4191
4192	/* Allow incoming packets */
4193	hid_device_io_start(hdev);
4194
4195	if (hidpp->quirks & HIDPP_QUIRK_UNIFYING)
 
4196		hidpp_unifying_init(hidpp);
 
 
4197
4198	connected = hidpp_root_get_protocol_version(hidpp) == 0;
4199	atomic_set(&hidpp->connected, connected);
4200	if (!(hidpp->quirks & HIDPP_QUIRK_UNIFYING)) {
4201		if (!connected) {
4202			ret = -ENODEV;
4203			hid_err(hdev, "Device not connected");
4204			goto hid_hw_init_fail;
4205		}
4206
4207		hidpp_overwrite_name(hdev);
4208	}
4209
4210	if (connected && hidpp->protocol_major >= 2) {
4211		ret = hidpp_set_wireless_feature_index(hidpp);
4212		if (ret == -ENOENT)
4213			hidpp->wireless_feature_index = 0;
4214		else if (ret)
4215			goto hid_hw_init_fail;
4216	}
4217
4218	if (connected && (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)) {
4219		ret = wtp_get_config(hidpp);
4220		if (ret)
4221			goto hid_hw_init_fail;
4222	} else if (connected && (hidpp->quirks & HIDPP_QUIRK_CLASS_G920)) {
4223		ret = g920_get_config(hidpp, &data);
4224		if (ret)
4225			goto hid_hw_init_fail;
4226	}
4227
4228	hidpp_connect_event(hidpp);
4229
4230	/* Reset the HID node state */
4231	hid_device_io_stop(hdev);
4232	hid_hw_close(hdev);
4233	hid_hw_stop(hdev);
4234
4235	if (hidpp->quirks & HIDPP_QUIRK_NO_HIDINPUT)
4236		connect_mask &= ~HID_CONNECT_HIDINPUT;
4237
4238	/* Now export the actual inputs and hidraw nodes to the world */
4239	ret = hid_hw_start(hdev, connect_mask);
 
4240	if (ret) {
4241		hid_err(hdev, "%s:hid_hw_start returned error\n", __func__);
4242		goto hid_hw_start_fail;
4243	}
4244
 
 
 
 
 
4245	if (hidpp->quirks & HIDPP_QUIRK_CLASS_G920) {
4246		ret = hidpp_ff_init(hidpp, &data);
 
 
 
 
 
4247		if (ret)
4248			hid_warn(hidpp->hid_dev,
4249		     "Unable to initialize force feedback support, errno %d\n",
4250				 ret);
4251	}
4252
 
 
 
 
 
4253	return ret;
4254
4255hid_hw_init_fail:
4256	hid_hw_close(hdev);
4257hid_hw_open_fail:
4258	hid_hw_stop(hdev);
4259hid_hw_start_fail:
4260	sysfs_remove_group(&hdev->dev.kobj, &ps_attribute_group);
4261	cancel_work_sync(&hidpp->work);
4262	mutex_destroy(&hidpp->send_mutex);
4263	return ret;
4264}
4265
4266static void hidpp_remove(struct hid_device *hdev)
4267{
4268	struct hidpp_device *hidpp = hid_get_drvdata(hdev);
4269
4270	if (!hidpp)
4271		return hid_hw_stop(hdev);
4272
4273	sysfs_remove_group(&hdev->dev.kobj, &ps_attribute_group);
4274
4275	hid_hw_stop(hdev);
4276	cancel_work_sync(&hidpp->work);
4277	mutex_destroy(&hidpp->send_mutex);
4278}
4279
4280#define LDJ_DEVICE(product) \
4281	HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE, \
4282		   USB_VENDOR_ID_LOGITECH, (product))
4283
4284#define L27MHZ_DEVICE(product) \
4285	HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_27MHZ_DEVICE, \
4286		   USB_VENDOR_ID_LOGITECH, (product))
4287
4288static const struct hid_device_id hidpp_devices[] = {
4289	{ /* wireless touchpad */
4290	  LDJ_DEVICE(0x4011),
4291	  .driver_data = HIDPP_QUIRK_CLASS_WTP | HIDPP_QUIRK_DELAYED_INIT |
4292			 HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS },
4293	{ /* wireless touchpad T650 */
4294	  LDJ_DEVICE(0x4101),
4295	  .driver_data = HIDPP_QUIRK_CLASS_WTP | HIDPP_QUIRK_DELAYED_INIT },
4296	{ /* wireless touchpad T651 */
4297	  HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH,
4298		USB_DEVICE_ID_LOGITECH_T651),
4299	  .driver_data = HIDPP_QUIRK_CLASS_WTP },
 
 
4300	{ /* Mouse logitech M560 */
4301	  LDJ_DEVICE(0x402d),
4302	  .driver_data = HIDPP_QUIRK_DELAYED_INIT | HIDPP_QUIRK_CLASS_M560 },
 
 
 
 
4303	{ /* Keyboard logitech K400 */
4304	  LDJ_DEVICE(0x4024),
4305	  .driver_data = HIDPP_QUIRK_CLASS_K400 },
4306	{ /* Solar Keyboard Logitech K750 */
4307	  LDJ_DEVICE(0x4002),
4308	  .driver_data = HIDPP_QUIRK_CLASS_K750 },
4309	{ /* Keyboard MX5000 (Bluetooth-receiver in HID proxy mode) */
4310	  LDJ_DEVICE(0xb305),
4311	  .driver_data = HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS },
4312	{ /* Dinovo Edge (Bluetooth-receiver in HID proxy mode) */
4313	  LDJ_DEVICE(0xb309),
4314	  .driver_data = HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS },
4315	{ /* Keyboard MX5500 (Bluetooth-receiver in HID proxy mode) */
4316	  LDJ_DEVICE(0xb30b),
4317	  .driver_data = HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS },
4318
4319	{ LDJ_DEVICE(HID_ANY_ID) },
4320
4321	{ /* Keyboard LX501 (Y-RR53) */
4322	  L27MHZ_DEVICE(0x0049),
4323	  .driver_data = HIDPP_QUIRK_KBD_ZOOM_WHEEL },
4324	{ /* Keyboard MX3000 (Y-RAM74) */
4325	  L27MHZ_DEVICE(0x0057),
4326	  .driver_data = HIDPP_QUIRK_KBD_SCROLL_WHEEL },
4327	{ /* Keyboard MX3200 (Y-RAV80) */
4328	  L27MHZ_DEVICE(0x005c),
4329	  .driver_data = HIDPP_QUIRK_KBD_ZOOM_WHEEL },
4330	{ /* S510 Media Remote */
4331	  L27MHZ_DEVICE(0x00fe),
4332	  .driver_data = HIDPP_QUIRK_KBD_SCROLL_WHEEL },
4333
4334	{ L27MHZ_DEVICE(HID_ANY_ID) },
4335
4336	{ /* Logitech G403 Wireless Gaming Mouse over USB */
4337	  HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC082) },
 
 
4338	{ /* Logitech G703 Gaming Mouse over USB */
4339	  HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC087) },
4340	{ /* Logitech G703 Hero Gaming Mouse over USB */
4341	  HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC090) },
4342	{ /* Logitech G900 Gaming Mouse over USB */
4343	  HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC081) },
4344	{ /* Logitech G903 Gaming Mouse over USB */
4345	  HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC086) },
4346	{ /* Logitech G903 Hero Gaming Mouse over USB */
4347	  HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC091) },
 
 
4348	{ /* Logitech G920 Wheel over USB */
4349	  HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_G920_WHEEL),
4350		.driver_data = HIDPP_QUIRK_CLASS_G920 | HIDPP_QUIRK_FORCE_OUTPUT_REPORTS},
 
 
 
4351	{ /* Logitech G Pro Gaming Mouse over USB */
4352	  HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC088) },
 
 
 
 
 
 
 
 
4353
4354	{ /* MX5000 keyboard over Bluetooth */
4355	  HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb305),
4356	  .driver_data = HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS },
4357	{ /* Dinovo Edge keyboard over Bluetooth */
4358	  HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb309),
4359	  .driver_data = HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS },
4360	{ /* MX5500 keyboard over Bluetooth */
4361	  HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb30b),
4362	  .driver_data = HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS },
 
 
4363	{ /* M-RCQ142 V470 Cordless Laser Mouse over Bluetooth */
4364	  HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb008) },
4365	{ /* MX Master mouse over Bluetooth */
4366	  HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb012) },
 
 
4367	{ /* MX Ergo trackball over Bluetooth */
4368	  HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb01d) },
4369	{ HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb01e) },
 
 
4370	{ /* MX Master 3 mouse over Bluetooth */
4371	  HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb023) },
 
 
 
 
4372	{}
4373};
4374
4375MODULE_DEVICE_TABLE(hid, hidpp_devices);
4376
4377static const struct hid_usage_id hidpp_usages[] = {
4378	{ HID_GD_WHEEL, EV_REL, REL_WHEEL_HI_RES },
4379	{ HID_ANY_ID - 1, HID_ANY_ID - 1, HID_ANY_ID - 1}
4380};
4381
4382static struct hid_driver hidpp_driver = {
4383	.name = "logitech-hidpp-device",
4384	.id_table = hidpp_devices,
4385	.report_fixup = hidpp_report_fixup,
4386	.probe = hidpp_probe,
4387	.remove = hidpp_remove,
4388	.raw_event = hidpp_raw_event,
4389	.usage_table = hidpp_usages,
4390	.event = hidpp_event,
4391	.input_configured = hidpp_input_configured,
4392	.input_mapping = hidpp_input_mapping,
4393	.input_mapped = hidpp_input_mapped,
4394};
4395
4396module_hid_driver(hidpp_driver);
v6.8
   1// SPDX-License-Identifier: GPL-2.0-only
   2/*
   3 *  HIDPP protocol for Logitech receivers
   4 *
   5 *  Copyright (c) 2011 Logitech (c)
   6 *  Copyright (c) 2012-2013 Google (c)
   7 *  Copyright (c) 2013-2014 Red Hat Inc.
   8 */
   9
  10
  11#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  12
  13#include <linux/device.h>
  14#include <linux/input.h>
  15#include <linux/usb.h>
  16#include <linux/hid.h>
  17#include <linux/module.h>
  18#include <linux/slab.h>
  19#include <linux/sched.h>
  20#include <linux/sched/clock.h>
  21#include <linux/kfifo.h>
  22#include <linux/input/mt.h>
  23#include <linux/workqueue.h>
  24#include <linux/atomic.h>
  25#include <linux/fixp-arith.h>
  26#include <asm/unaligned.h>
  27#include "usbhid/usbhid.h"
  28#include "hid-ids.h"
  29
  30MODULE_LICENSE("GPL");
  31MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
  32MODULE_AUTHOR("Nestor Lopez Casado <nlopezcasad@logitech.com>");
  33MODULE_AUTHOR("Bastien Nocera <hadess@hadess.net>");
 
 
 
 
  34
  35static bool disable_tap_to_click;
  36module_param(disable_tap_to_click, bool, 0644);
  37MODULE_PARM_DESC(disable_tap_to_click,
  38	"Disable Tap-To-Click mode reporting for touchpads (only on the K400 currently).");
  39
  40/* Define a non-zero software ID to identify our own requests */
  41#define LINUX_KERNEL_SW_ID			0x01
  42
  43#define REPORT_ID_HIDPP_SHORT			0x10
  44#define REPORT_ID_HIDPP_LONG			0x11
  45#define REPORT_ID_HIDPP_VERY_LONG		0x12
  46
  47#define HIDPP_REPORT_SHORT_LENGTH		7
  48#define HIDPP_REPORT_LONG_LENGTH		20
  49#define HIDPP_REPORT_VERY_LONG_MAX_LENGTH	64
  50
  51#define HIDPP_REPORT_SHORT_SUPPORTED		BIT(0)
  52#define HIDPP_REPORT_LONG_SUPPORTED		BIT(1)
  53#define HIDPP_REPORT_VERY_LONG_SUPPORTED	BIT(2)
  54
  55#define HIDPP_SUB_ID_CONSUMER_VENDOR_KEYS	0x03
  56#define HIDPP_SUB_ID_ROLLER			0x05
  57#define HIDPP_SUB_ID_MOUSE_EXTRA_BTNS		0x06
  58#define HIDPP_SUB_ID_USER_IFACE_EVENT		0x08
  59#define HIDPP_USER_IFACE_EVENT_ENCRYPTION_KEY_LOST	BIT(5)
  60
  61#define HIDPP_QUIRK_CLASS_WTP			BIT(0)
  62#define HIDPP_QUIRK_CLASS_M560			BIT(1)
  63#define HIDPP_QUIRK_CLASS_K400			BIT(2)
  64#define HIDPP_QUIRK_CLASS_G920			BIT(3)
  65#define HIDPP_QUIRK_CLASS_K750			BIT(4)
  66
  67/* bits 2..20 are reserved for classes */
  68/* #define HIDPP_QUIRK_CONNECT_EVENTS		BIT(21) disabled */
  69#define HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS	BIT(22)
  70#define HIDPP_QUIRK_DELAYED_INIT		BIT(23)
  71#define HIDPP_QUIRK_FORCE_OUTPUT_REPORTS	BIT(24)
  72#define HIDPP_QUIRK_HIDPP_WHEELS		BIT(25)
  73#define HIDPP_QUIRK_HIDPP_EXTRA_MOUSE_BTNS	BIT(26)
  74#define HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS	BIT(27)
  75#define HIDPP_QUIRK_HI_RES_SCROLL_1P0		BIT(28)
  76#define HIDPP_QUIRK_WIRELESS_STATUS		BIT(29)
  77
  78/* These are just aliases for now */
  79#define HIDPP_QUIRK_KBD_SCROLL_WHEEL HIDPP_QUIRK_HIDPP_WHEELS
  80#define HIDPP_QUIRK_KBD_ZOOM_WHEEL   HIDPP_QUIRK_HIDPP_WHEELS
  81
  82/* Convenience constant to check for any high-res support. */
  83#define HIDPP_CAPABILITY_HI_RES_SCROLL	(HIDPP_CAPABILITY_HIDPP10_FAST_SCROLL | \
  84					 HIDPP_CAPABILITY_HIDPP20_HI_RES_SCROLL | \
  85					 HIDPP_CAPABILITY_HIDPP20_HI_RES_WHEEL)
  86
 
 
  87#define HIDPP_CAPABILITY_HIDPP10_BATTERY	BIT(0)
  88#define HIDPP_CAPABILITY_HIDPP20_BATTERY	BIT(1)
  89#define HIDPP_CAPABILITY_BATTERY_MILEAGE	BIT(2)
  90#define HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS	BIT(3)
  91#define HIDPP_CAPABILITY_BATTERY_VOLTAGE	BIT(4)
  92#define HIDPP_CAPABILITY_BATTERY_PERCENTAGE	BIT(5)
  93#define HIDPP_CAPABILITY_UNIFIED_BATTERY	BIT(6)
  94#define HIDPP_CAPABILITY_HIDPP20_HI_RES_WHEEL	BIT(7)
  95#define HIDPP_CAPABILITY_HIDPP20_HI_RES_SCROLL	BIT(8)
  96#define HIDPP_CAPABILITY_HIDPP10_FAST_SCROLL	BIT(9)
  97#define HIDPP_CAPABILITY_ADC_MEASUREMENT	BIT(10)
  98
  99#define lg_map_key_clear(c)  hid_map_usage_clear(hi, usage, bit, max, EV_KEY, (c))
 100
 101/*
 102 * There are two hidpp protocols in use, the first version hidpp10 is known
 103 * as register access protocol or RAP, the second version hidpp20 is known as
 104 * feature access protocol or FAP
 105 *
 106 * Most older devices (including the Unifying usb receiver) use the RAP protocol
 107 * where as most newer devices use the FAP protocol. Both protocols are
 108 * compatible with the underlying transport, which could be usb, Unifiying, or
 109 * bluetooth. The message lengths are defined by the hid vendor specific report
 110 * descriptor for the HIDPP_SHORT report type (total message lenth 7 bytes) and
 111 * the HIDPP_LONG report type (total message length 20 bytes)
 112 *
 113 * The RAP protocol uses both report types, whereas the FAP only uses HIDPP_LONG
 114 * messages. The Unifying receiver itself responds to RAP messages (device index
 115 * is 0xFF for the receiver), and all messages (short or long) with a device
 116 * index between 1 and 6 are passed untouched to the corresponding paired
 117 * Unifying device.
 118 *
 119 * The paired device can be RAP or FAP, it will receive the message untouched
 120 * from the Unifiying receiver.
 121 */
 122
 123struct fap {
 124	u8 feature_index;
 125	u8 funcindex_clientid;
 126	u8 params[HIDPP_REPORT_VERY_LONG_MAX_LENGTH - 4U];
 127};
 128
 129struct rap {
 130	u8 sub_id;
 131	u8 reg_address;
 132	u8 params[HIDPP_REPORT_VERY_LONG_MAX_LENGTH - 4U];
 133};
 134
 135struct hidpp_report {
 136	u8 report_id;
 137	u8 device_index;
 138	union {
 139		struct fap fap;
 140		struct rap rap;
 141		u8 rawbytes[sizeof(struct fap)];
 142	};
 143} __packed;
 144
 145struct hidpp_battery {
 146	u8 feature_index;
 147	u8 solar_feature_index;
 148	u8 voltage_feature_index;
 149	u8 adc_measurement_feature_index;
 150	struct power_supply_desc desc;
 151	struct power_supply *ps;
 152	char name[64];
 153	int status;
 154	int capacity;
 155	int level;
 156	int voltage;
 157	int charge_type;
 158	bool online;
 159	u8 supported_levels_1004;
 160};
 161
 162/**
 163 * struct hidpp_scroll_counter - Utility class for processing high-resolution
 164 *                             scroll events.
 165 * @dev: the input device for which events should be reported.
 166 * @wheel_multiplier: the scalar multiplier to be applied to each wheel event
 167 * @remainder: counts the number of high-resolution units moved since the last
 168 *             low-resolution event (REL_WHEEL or REL_HWHEEL) was sent. Should
 169 *             only be used by class methods.
 170 * @direction: direction of last movement (1 or -1)
 171 * @last_time: last event time, used to reset remainder after inactivity
 172 */
 173struct hidpp_scroll_counter {
 174	int wheel_multiplier;
 175	int remainder;
 176	int direction;
 177	unsigned long long last_time;
 178};
 179
 180struct hidpp_device {
 181	struct hid_device *hid_dev;
 182	struct input_dev *input;
 183	struct mutex send_mutex;
 184	void *send_receive_buf;
 185	char *name;		/* will never be NULL and should not be freed */
 186	wait_queue_head_t wait;
 187	int very_long_report_length;
 188	bool answer_available;
 189	u8 protocol_major;
 190	u8 protocol_minor;
 191
 192	void *private_data;
 193
 194	struct work_struct work;
 195	struct kfifo delayed_work_fifo;
 
 196	struct input_dev *delayed_input;
 197
 198	unsigned long quirks;
 199	unsigned long capabilities;
 200	u8 supported_reports;
 201
 202	struct hidpp_battery battery;
 203	struct hidpp_scroll_counter vertical_wheel_counter;
 204
 205	u8 wireless_feature_index;
 206
 207	bool connected_once;
 208};
 209
 210/* HID++ 1.0 error codes */
 211#define HIDPP_ERROR				0x8f
 212#define HIDPP_ERROR_SUCCESS			0x00
 213#define HIDPP_ERROR_INVALID_SUBID		0x01
 214#define HIDPP_ERROR_INVALID_ADRESS		0x02
 215#define HIDPP_ERROR_INVALID_VALUE		0x03
 216#define HIDPP_ERROR_CONNECT_FAIL		0x04
 217#define HIDPP_ERROR_TOO_MANY_DEVICES		0x05
 218#define HIDPP_ERROR_ALREADY_EXISTS		0x06
 219#define HIDPP_ERROR_BUSY			0x07
 220#define HIDPP_ERROR_UNKNOWN_DEVICE		0x08
 221#define HIDPP_ERROR_RESOURCE_ERROR		0x09
 222#define HIDPP_ERROR_REQUEST_UNAVAILABLE		0x0a
 223#define HIDPP_ERROR_INVALID_PARAM_VALUE		0x0b
 224#define HIDPP_ERROR_WRONG_PIN_CODE		0x0c
 225/* HID++ 2.0 error codes */
 226#define HIDPP20_ERROR_NO_ERROR			0x00
 227#define HIDPP20_ERROR_UNKNOWN			0x01
 228#define HIDPP20_ERROR_INVALID_ARGS		0x02
 229#define HIDPP20_ERROR_OUT_OF_RANGE		0x03
 230#define HIDPP20_ERROR_HW_ERROR			0x04
 231#define HIDPP20_ERROR_NOT_ALLOWED		0x05
 232#define HIDPP20_ERROR_INVALID_FEATURE_INDEX	0x06
 233#define HIDPP20_ERROR_INVALID_FUNCTION_ID	0x07
 234#define HIDPP20_ERROR_BUSY			0x08
 235#define HIDPP20_ERROR_UNSUPPORTED		0x09
 236#define HIDPP20_ERROR				0xff
 237
 
 
 238static int __hidpp_send_report(struct hid_device *hdev,
 239				struct hidpp_report *hidpp_report)
 240{
 241	struct hidpp_device *hidpp = hid_get_drvdata(hdev);
 242	int fields_count, ret;
 243
 244	switch (hidpp_report->report_id) {
 245	case REPORT_ID_HIDPP_SHORT:
 246		fields_count = HIDPP_REPORT_SHORT_LENGTH;
 247		break;
 248	case REPORT_ID_HIDPP_LONG:
 249		fields_count = HIDPP_REPORT_LONG_LENGTH;
 250		break;
 251	case REPORT_ID_HIDPP_VERY_LONG:
 252		fields_count = hidpp->very_long_report_length;
 253		break;
 254	default:
 255		return -ENODEV;
 256	}
 257
 258	/*
 259	 * set the device_index as the receiver, it will be overwritten by
 260	 * hid_hw_request if needed
 261	 */
 262	hidpp_report->device_index = 0xff;
 263
 264	if (hidpp->quirks & HIDPP_QUIRK_FORCE_OUTPUT_REPORTS) {
 265		ret = hid_hw_output_report(hdev, (u8 *)hidpp_report, fields_count);
 266	} else {
 267		ret = hid_hw_raw_request(hdev, hidpp_report->report_id,
 268			(u8 *)hidpp_report, fields_count, HID_OUTPUT_REPORT,
 269			HID_REQ_SET_REPORT);
 270	}
 271
 272	return ret == fields_count ? 0 : -1;
 273}
 274
 275/*
 276 * Effectively send the message to the device, waiting for its answer.
 277 *
 278 * Must be called with hidpp->send_mutex locked
 279 *
 280 * Same return protocol than hidpp_send_message_sync():
 281 * - success on 0
 282 * - negative error means transport error
 283 * - positive value means protocol error
 284 */
 285static int __do_hidpp_send_message_sync(struct hidpp_device *hidpp,
 286	struct hidpp_report *message,
 287	struct hidpp_report *response)
 288{
 289	int ret;
 290
 291	__must_hold(&hidpp->send_mutex);
 292
 293	hidpp->send_receive_buf = response;
 294	hidpp->answer_available = false;
 295
 296	/*
 297	 * So that we can later validate the answer when it arrives
 298	 * in hidpp_raw_event
 299	 */
 300	*response = *message;
 301
 302	ret = __hidpp_send_report(hidpp->hid_dev, message);
 
 303	if (ret) {
 304		dbg_hid("__hidpp_send_report returned err: %d\n", ret);
 305		memset(response, 0, sizeof(struct hidpp_report));
 306		return ret;
 307	}
 308
 309	if (!wait_event_timeout(hidpp->wait, hidpp->answer_available,
 310				5*HZ)) {
 311		dbg_hid("%s:timeout waiting for response\n", __func__);
 312		memset(response, 0, sizeof(struct hidpp_report));
 313		return -ETIMEDOUT;
 314	}
 315
 316	if (response->report_id == REPORT_ID_HIDPP_SHORT &&
 317	    response->rap.sub_id == HIDPP_ERROR) {
 318		ret = response->rap.params[1];
 319		dbg_hid("%s:got hidpp error %02X\n", __func__, ret);
 320		return ret;
 321	}
 322
 323	if ((response->report_id == REPORT_ID_HIDPP_LONG ||
 324	     response->report_id == REPORT_ID_HIDPP_VERY_LONG) &&
 325	    response->fap.feature_index == HIDPP20_ERROR) {
 326		ret = response->fap.params[1];
 327		dbg_hid("%s:got hidpp 2.0 error %02X\n", __func__, ret);
 328		return ret;
 329	}
 330
 331	return 0;
 332}
 333
 334/*
 335 * hidpp_send_message_sync() returns 0 in case of success, and something else
 336 * in case of a failure.
 337 *
 338 * See __do_hidpp_send_message_sync() for a detailed explanation of the returned
 339 * value.
 340 */
 341static int hidpp_send_message_sync(struct hidpp_device *hidpp,
 342	struct hidpp_report *message,
 343	struct hidpp_report *response)
 344{
 345	int ret;
 346	int max_retries = 3;
 347
 348	mutex_lock(&hidpp->send_mutex);
 349
 350	do {
 351		ret = __do_hidpp_send_message_sync(hidpp, message, response);
 352		if (ret != HIDPP20_ERROR_BUSY)
 353			break;
 354
 355		dbg_hid("%s:got busy hidpp 2.0 error %02X, retrying\n", __func__, ret);
 356	} while (--max_retries);
 357
 358	mutex_unlock(&hidpp->send_mutex);
 359	return ret;
 360
 361}
 362
 363/*
 364 * hidpp_send_fap_command_sync() returns 0 in case of success, and something else
 365 * in case of a failure.
 366 *
 367 * See __do_hidpp_send_message_sync() for a detailed explanation of the returned
 368 * value.
 369 */
 370static int hidpp_send_fap_command_sync(struct hidpp_device *hidpp,
 371	u8 feat_index, u8 funcindex_clientid, u8 *params, int param_count,
 372	struct hidpp_report *response)
 373{
 374	struct hidpp_report *message;
 375	int ret;
 376
 377	if (param_count > sizeof(message->fap.params)) {
 378		hid_dbg(hidpp->hid_dev,
 379			"Invalid number of parameters passed to command (%d != %llu)\n",
 380			param_count,
 381			(unsigned long long) sizeof(message->fap.params));
 382		return -EINVAL;
 383	}
 384
 385	message = kzalloc(sizeof(struct hidpp_report), GFP_KERNEL);
 386	if (!message)
 387		return -ENOMEM;
 388
 389	if (param_count > (HIDPP_REPORT_LONG_LENGTH - 4))
 390		message->report_id = REPORT_ID_HIDPP_VERY_LONG;
 391	else
 392		message->report_id = REPORT_ID_HIDPP_LONG;
 393	message->fap.feature_index = feat_index;
 394	message->fap.funcindex_clientid = funcindex_clientid | LINUX_KERNEL_SW_ID;
 395	memcpy(&message->fap.params, params, param_count);
 396
 397	ret = hidpp_send_message_sync(hidpp, message, response);
 398	kfree(message);
 399	return ret;
 400}
 401
 402/*
 403 * hidpp_send_rap_command_sync() returns 0 in case of success, and something else
 404 * in case of a failure.
 405 *
 406 * See __do_hidpp_send_message_sync() for a detailed explanation of the returned
 407 * value.
 408 */
 409static int hidpp_send_rap_command_sync(struct hidpp_device *hidpp_dev,
 410	u8 report_id, u8 sub_id, u8 reg_address, u8 *params, int param_count,
 411	struct hidpp_report *response)
 412{
 413	struct hidpp_report *message;
 414	int ret, max_count;
 415
 416	/* Send as long report if short reports are not supported. */
 417	if (report_id == REPORT_ID_HIDPP_SHORT &&
 418	    !(hidpp_dev->supported_reports & HIDPP_REPORT_SHORT_SUPPORTED))
 419		report_id = REPORT_ID_HIDPP_LONG;
 420
 421	switch (report_id) {
 422	case REPORT_ID_HIDPP_SHORT:
 423		max_count = HIDPP_REPORT_SHORT_LENGTH - 4;
 424		break;
 425	case REPORT_ID_HIDPP_LONG:
 426		max_count = HIDPP_REPORT_LONG_LENGTH - 4;
 427		break;
 428	case REPORT_ID_HIDPP_VERY_LONG:
 429		max_count = hidpp_dev->very_long_report_length - 4;
 430		break;
 431	default:
 432		return -EINVAL;
 433	}
 434
 435	if (param_count > max_count)
 436		return -EINVAL;
 437
 438	message = kzalloc(sizeof(struct hidpp_report), GFP_KERNEL);
 439	if (!message)
 440		return -ENOMEM;
 441	message->report_id = report_id;
 442	message->rap.sub_id = sub_id;
 443	message->rap.reg_address = reg_address;
 444	memcpy(&message->rap.params, params, param_count);
 445
 446	ret = hidpp_send_message_sync(hidpp_dev, message, response);
 447	kfree(message);
 448	return ret;
 449}
 450
 
 
 
 
 
 
 
 451static inline bool hidpp_match_answer(struct hidpp_report *question,
 452		struct hidpp_report *answer)
 453{
 454	return (answer->fap.feature_index == question->fap.feature_index) &&
 455	   (answer->fap.funcindex_clientid == question->fap.funcindex_clientid);
 456}
 457
 458static inline bool hidpp_match_error(struct hidpp_report *question,
 459		struct hidpp_report *answer)
 460{
 461	return ((answer->rap.sub_id == HIDPP_ERROR) ||
 462	    (answer->fap.feature_index == HIDPP20_ERROR)) &&
 463	    (answer->fap.funcindex_clientid == question->fap.feature_index) &&
 464	    (answer->fap.params[0] == question->fap.funcindex_clientid);
 465}
 466
 467static inline bool hidpp_report_is_connect_event(struct hidpp_device *hidpp,
 468		struct hidpp_report *report)
 469{
 470	return (hidpp->wireless_feature_index &&
 471		(report->fap.feature_index == hidpp->wireless_feature_index)) ||
 472		((report->report_id == REPORT_ID_HIDPP_SHORT) &&
 473		(report->rap.sub_id == 0x41));
 474}
 475
 476/*
 477 * hidpp_prefix_name() prefixes the current given name with "Logitech ".
 478 */
 479static void hidpp_prefix_name(char **name, int name_length)
 480{
 481#define PREFIX_LENGTH 9 /* "Logitech " */
 482
 483	int new_length;
 484	char *new_name;
 485
 486	if (name_length > PREFIX_LENGTH &&
 487	    strncmp(*name, "Logitech ", PREFIX_LENGTH) == 0)
 488		/* The prefix has is already in the name */
 489		return;
 490
 491	new_length = PREFIX_LENGTH + name_length;
 492	new_name = kzalloc(new_length, GFP_KERNEL);
 493	if (!new_name)
 494		return;
 495
 496	snprintf(new_name, new_length, "Logitech %s", *name);
 497
 498	kfree(*name);
 499
 500	*name = new_name;
 501}
 502
 503/*
 504 * Updates the USB wireless_status based on whether the headset
 505 * is turned on and reachable.
 506 */
 507static void hidpp_update_usb_wireless_status(struct hidpp_device *hidpp)
 508{
 509	struct hid_device *hdev = hidpp->hid_dev;
 510	struct usb_interface *intf;
 511
 512	if (!(hidpp->quirks & HIDPP_QUIRK_WIRELESS_STATUS))
 513		return;
 514	if (!hid_is_usb(hdev))
 515		return;
 516
 517	intf = to_usb_interface(hdev->dev.parent);
 518	usb_set_wireless_status(intf, hidpp->battery.online ?
 519				USB_WIRELESS_STATUS_CONNECTED :
 520				USB_WIRELESS_STATUS_DISCONNECTED);
 521}
 522
 523/**
 524 * hidpp_scroll_counter_handle_scroll() - Send high- and low-resolution scroll
 525 *                                        events given a high-resolution wheel
 526 *                                        movement.
 527 * @input_dev: Pointer to the input device
 528 * @counter: a hid_scroll_counter struct describing the wheel.
 529 * @hi_res_value: the movement of the wheel, in the mouse's high-resolution
 530 *                units.
 531 *
 532 * Given a high-resolution movement, this function converts the movement into
 533 * fractions of 120 and emits high-resolution scroll events for the input
 534 * device. It also uses the multiplier from &struct hid_scroll_counter to
 535 * emit low-resolution scroll events when appropriate for
 536 * backwards-compatibility with userspace input libraries.
 537 */
 538static void hidpp_scroll_counter_handle_scroll(struct input_dev *input_dev,
 539					       struct hidpp_scroll_counter *counter,
 540					       int hi_res_value)
 541{
 542	int low_res_value, remainder, direction;
 543	unsigned long long now, previous;
 544
 545	hi_res_value = hi_res_value * 120/counter->wheel_multiplier;
 546	input_report_rel(input_dev, REL_WHEEL_HI_RES, hi_res_value);
 547
 548	remainder = counter->remainder;
 549	direction = hi_res_value > 0 ? 1 : -1;
 550
 551	now = sched_clock();
 552	previous = counter->last_time;
 553	counter->last_time = now;
 554	/*
 555	 * Reset the remainder after a period of inactivity or when the
 556	 * direction changes. This prevents the REL_WHEEL emulation point
 557	 * from sliding for devices that don't always provide the same
 558	 * number of movements per detent.
 559	 */
 560	if (now - previous > 1000000000 || direction != counter->direction)
 561		remainder = 0;
 562
 563	counter->direction = direction;
 564	remainder += hi_res_value;
 565
 566	/* Some wheels will rest 7/8ths of a detent from the previous detent
 567	 * after slow movement, so we want the threshold for low-res events to
 568	 * be in the middle between two detents (e.g. after 4/8ths) as
 569	 * opposed to on the detents themselves (8/8ths).
 570	 */
 571	if (abs(remainder) >= 60) {
 572		/* Add (or subtract) 1 because we want to trigger when the wheel
 573		 * is half-way to the next detent (i.e. scroll 1 detent after a
 574		 * 1/2 detent movement, 2 detents after a 1 1/2 detent movement,
 575		 * etc.).
 576		 */
 577		low_res_value = remainder / 120;
 578		if (low_res_value == 0)
 579			low_res_value = (hi_res_value > 0 ? 1 : -1);
 580		input_report_rel(input_dev, REL_WHEEL, low_res_value);
 581		remainder -= low_res_value * 120;
 582	}
 583	counter->remainder = remainder;
 584}
 585
 586/* -------------------------------------------------------------------------- */
 587/* HIDP++ 1.0 commands                                                        */
 588/* -------------------------------------------------------------------------- */
 589
 590#define HIDPP_SET_REGISTER				0x80
 591#define HIDPP_GET_REGISTER				0x81
 592#define HIDPP_SET_LONG_REGISTER				0x82
 593#define HIDPP_GET_LONG_REGISTER				0x83
 594
 595/**
 596 * hidpp10_set_register - Modify a HID++ 1.0 register.
 597 * @hidpp_dev: the device to set the register on.
 598 * @register_address: the address of the register to modify.
 599 * @byte: the byte of the register to modify. Should be less than 3.
 600 * @mask: mask of the bits to modify
 601 * @value: new values for the bits in mask
 602 * Return: 0 if successful, otherwise a negative error code.
 603 */
 604static int hidpp10_set_register(struct hidpp_device *hidpp_dev,
 605	u8 register_address, u8 byte, u8 mask, u8 value)
 606{
 607	struct hidpp_report response;
 608	int ret;
 609	u8 params[3] = { 0 };
 610
 611	ret = hidpp_send_rap_command_sync(hidpp_dev,
 612					  REPORT_ID_HIDPP_SHORT,
 613					  HIDPP_GET_REGISTER,
 614					  register_address,
 615					  NULL, 0, &response);
 616	if (ret)
 617		return ret;
 618
 619	memcpy(params, response.rap.params, 3);
 620
 621	params[byte] &= ~mask;
 622	params[byte] |= value & mask;
 623
 624	return hidpp_send_rap_command_sync(hidpp_dev,
 625					   REPORT_ID_HIDPP_SHORT,
 626					   HIDPP_SET_REGISTER,
 627					   register_address,
 628					   params, 3, &response);
 629}
 630
 631#define HIDPP_REG_ENABLE_REPORTS			0x00
 632#define HIDPP_ENABLE_CONSUMER_REPORT			BIT(0)
 633#define HIDPP_ENABLE_WHEEL_REPORT			BIT(2)
 634#define HIDPP_ENABLE_MOUSE_EXTRA_BTN_REPORT		BIT(3)
 635#define HIDPP_ENABLE_BAT_REPORT				BIT(4)
 636#define HIDPP_ENABLE_HWHEEL_REPORT			BIT(5)
 637
 638static int hidpp10_enable_battery_reporting(struct hidpp_device *hidpp_dev)
 639{
 640	return hidpp10_set_register(hidpp_dev, HIDPP_REG_ENABLE_REPORTS, 0,
 641			  HIDPP_ENABLE_BAT_REPORT, HIDPP_ENABLE_BAT_REPORT);
 642}
 643
 644#define HIDPP_REG_FEATURES				0x01
 645#define HIDPP_ENABLE_SPECIAL_BUTTON_FUNC		BIT(1)
 646#define HIDPP_ENABLE_FAST_SCROLL			BIT(6)
 647
 648/* On HID++ 1.0 devices, high-res scroll was called "scrolling acceleration". */
 649static int hidpp10_enable_scrolling_acceleration(struct hidpp_device *hidpp_dev)
 650{
 651	return hidpp10_set_register(hidpp_dev, HIDPP_REG_FEATURES, 0,
 652			  HIDPP_ENABLE_FAST_SCROLL, HIDPP_ENABLE_FAST_SCROLL);
 653}
 654
 655#define HIDPP_REG_BATTERY_STATUS			0x07
 656
 657static int hidpp10_battery_status_map_level(u8 param)
 658{
 659	int level;
 660
 661	switch (param) {
 662	case 1 ... 2:
 663		level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
 664		break;
 665	case 3 ... 4:
 666		level = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
 667		break;
 668	case 5 ... 6:
 669		level = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
 670		break;
 671	case 7:
 672		level = POWER_SUPPLY_CAPACITY_LEVEL_HIGH;
 673		break;
 674	default:
 675		level = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
 676	}
 677
 678	return level;
 679}
 680
 681static int hidpp10_battery_status_map_status(u8 param)
 682{
 683	int status;
 684
 685	switch (param) {
 686	case 0x00:
 687		/* discharging (in use) */
 688		status = POWER_SUPPLY_STATUS_DISCHARGING;
 689		break;
 690	case 0x21: /* (standard) charging */
 691	case 0x24: /* fast charging */
 692	case 0x25: /* slow charging */
 693		status = POWER_SUPPLY_STATUS_CHARGING;
 694		break;
 695	case 0x26: /* topping charge */
 696	case 0x22: /* charge complete */
 697		status = POWER_SUPPLY_STATUS_FULL;
 698		break;
 699	case 0x20: /* unknown */
 700		status = POWER_SUPPLY_STATUS_UNKNOWN;
 701		break;
 702	/*
 703	 * 0x01...0x1F = reserved (not charging)
 704	 * 0x23 = charging error
 705	 * 0x27..0xff = reserved
 706	 */
 707	default:
 708		status = POWER_SUPPLY_STATUS_NOT_CHARGING;
 709		break;
 710	}
 711
 712	return status;
 713}
 714
 715static int hidpp10_query_battery_status(struct hidpp_device *hidpp)
 716{
 717	struct hidpp_report response;
 718	int ret, status;
 719
 720	ret = hidpp_send_rap_command_sync(hidpp,
 721					REPORT_ID_HIDPP_SHORT,
 722					HIDPP_GET_REGISTER,
 723					HIDPP_REG_BATTERY_STATUS,
 724					NULL, 0, &response);
 725	if (ret)
 726		return ret;
 727
 728	hidpp->battery.level =
 729		hidpp10_battery_status_map_level(response.rap.params[0]);
 730	status = hidpp10_battery_status_map_status(response.rap.params[1]);
 731	hidpp->battery.status = status;
 732	/* the capacity is only available when discharging or full */
 733	hidpp->battery.online = status == POWER_SUPPLY_STATUS_DISCHARGING ||
 734				status == POWER_SUPPLY_STATUS_FULL;
 735
 736	return 0;
 737}
 738
 739#define HIDPP_REG_BATTERY_MILEAGE			0x0D
 740
 741static int hidpp10_battery_mileage_map_status(u8 param)
 742{
 743	int status;
 744
 745	switch (param >> 6) {
 746	case 0x00:
 747		/* discharging (in use) */
 748		status = POWER_SUPPLY_STATUS_DISCHARGING;
 749		break;
 750	case 0x01: /* charging */
 751		status = POWER_SUPPLY_STATUS_CHARGING;
 752		break;
 753	case 0x02: /* charge complete */
 754		status = POWER_SUPPLY_STATUS_FULL;
 755		break;
 756	/*
 757	 * 0x03 = charging error
 758	 */
 759	default:
 760		status = POWER_SUPPLY_STATUS_NOT_CHARGING;
 761		break;
 762	}
 763
 764	return status;
 765}
 766
 767static int hidpp10_query_battery_mileage(struct hidpp_device *hidpp)
 768{
 769	struct hidpp_report response;
 770	int ret, status;
 771
 772	ret = hidpp_send_rap_command_sync(hidpp,
 773					REPORT_ID_HIDPP_SHORT,
 774					HIDPP_GET_REGISTER,
 775					HIDPP_REG_BATTERY_MILEAGE,
 776					NULL, 0, &response);
 777	if (ret)
 778		return ret;
 779
 780	hidpp->battery.capacity = response.rap.params[0];
 781	status = hidpp10_battery_mileage_map_status(response.rap.params[2]);
 782	hidpp->battery.status = status;
 783	/* the capacity is only available when discharging or full */
 784	hidpp->battery.online = status == POWER_SUPPLY_STATUS_DISCHARGING ||
 785				status == POWER_SUPPLY_STATUS_FULL;
 786
 787	return 0;
 788}
 789
 790static int hidpp10_battery_event(struct hidpp_device *hidpp, u8 *data, int size)
 791{
 792	struct hidpp_report *report = (struct hidpp_report *)data;
 793	int status, capacity, level;
 794	bool changed;
 795
 796	if (report->report_id != REPORT_ID_HIDPP_SHORT)
 797		return 0;
 798
 799	switch (report->rap.sub_id) {
 800	case HIDPP_REG_BATTERY_STATUS:
 801		capacity = hidpp->battery.capacity;
 802		level = hidpp10_battery_status_map_level(report->rawbytes[1]);
 803		status = hidpp10_battery_status_map_status(report->rawbytes[2]);
 804		break;
 805	case HIDPP_REG_BATTERY_MILEAGE:
 806		capacity = report->rap.params[0];
 807		level = hidpp->battery.level;
 808		status = hidpp10_battery_mileage_map_status(report->rawbytes[3]);
 809		break;
 810	default:
 811		return 0;
 812	}
 813
 814	changed = capacity != hidpp->battery.capacity ||
 815		  level != hidpp->battery.level ||
 816		  status != hidpp->battery.status;
 817
 818	/* the capacity is only available when discharging or full */
 819	hidpp->battery.online = status == POWER_SUPPLY_STATUS_DISCHARGING ||
 820				status == POWER_SUPPLY_STATUS_FULL;
 821
 822	if (changed) {
 823		hidpp->battery.level = level;
 824		hidpp->battery.status = status;
 825		if (hidpp->battery.ps)
 826			power_supply_changed(hidpp->battery.ps);
 827	}
 828
 829	return 0;
 830}
 831
 832#define HIDPP_REG_PAIRING_INFORMATION			0xB5
 833#define HIDPP_EXTENDED_PAIRING				0x30
 834#define HIDPP_DEVICE_NAME				0x40
 835
 836static char *hidpp_unifying_get_name(struct hidpp_device *hidpp_dev)
 837{
 838	struct hidpp_report response;
 839	int ret;
 840	u8 params[1] = { HIDPP_DEVICE_NAME };
 841	char *name;
 842	int len;
 843
 844	ret = hidpp_send_rap_command_sync(hidpp_dev,
 845					REPORT_ID_HIDPP_SHORT,
 846					HIDPP_GET_LONG_REGISTER,
 847					HIDPP_REG_PAIRING_INFORMATION,
 848					params, 1, &response);
 849	if (ret)
 850		return NULL;
 851
 852	len = response.rap.params[1];
 853
 854	if (2 + len > sizeof(response.rap.params))
 855		return NULL;
 856
 857	if (len < 4) /* logitech devices are usually at least Xddd */
 858		return NULL;
 859
 860	name = kzalloc(len + 1, GFP_KERNEL);
 861	if (!name)
 862		return NULL;
 863
 864	memcpy(name, &response.rap.params[2], len);
 865
 866	/* include the terminating '\0' */
 867	hidpp_prefix_name(&name, len + 1);
 868
 869	return name;
 870}
 871
 872static int hidpp_unifying_get_serial(struct hidpp_device *hidpp, u32 *serial)
 873{
 874	struct hidpp_report response;
 875	int ret;
 876	u8 params[1] = { HIDPP_EXTENDED_PAIRING };
 877
 878	ret = hidpp_send_rap_command_sync(hidpp,
 879					REPORT_ID_HIDPP_SHORT,
 880					HIDPP_GET_LONG_REGISTER,
 881					HIDPP_REG_PAIRING_INFORMATION,
 882					params, 1, &response);
 883	if (ret)
 884		return ret;
 885
 886	/*
 887	 * We don't care about LE or BE, we will output it as a string
 888	 * with %4phD, so we need to keep the order.
 889	 */
 890	*serial = *((u32 *)&response.rap.params[1]);
 891	return 0;
 892}
 893
 894static int hidpp_unifying_init(struct hidpp_device *hidpp)
 895{
 896	struct hid_device *hdev = hidpp->hid_dev;
 897	const char *name;
 898	u32 serial;
 899	int ret;
 900
 901	ret = hidpp_unifying_get_serial(hidpp, &serial);
 902	if (ret)
 903		return ret;
 904
 905	snprintf(hdev->uniq, sizeof(hdev->uniq), "%4phD", &serial);
 
 906	dbg_hid("HID++ Unifying: Got serial: %s\n", hdev->uniq);
 907
 908	name = hidpp_unifying_get_name(hidpp);
 909	if (!name)
 910		return -EIO;
 911
 912	snprintf(hdev->name, sizeof(hdev->name), "%s", name);
 913	dbg_hid("HID++ Unifying: Got name: %s\n", name);
 914
 915	kfree(name);
 916	return 0;
 917}
 918
 919/* -------------------------------------------------------------------------- */
 920/* 0x0000: Root                                                               */
 921/* -------------------------------------------------------------------------- */
 922
 923#define HIDPP_PAGE_ROOT					0x0000
 924#define HIDPP_PAGE_ROOT_IDX				0x00
 925
 926#define CMD_ROOT_GET_FEATURE				0x00
 927#define CMD_ROOT_GET_PROTOCOL_VERSION			0x10
 928
 929static int hidpp_root_get_feature(struct hidpp_device *hidpp, u16 feature,
 930	u8 *feature_index, u8 *feature_type)
 931{
 932	struct hidpp_report response;
 933	int ret;
 934	u8 params[2] = { feature >> 8, feature & 0x00FF };
 935
 936	ret = hidpp_send_fap_command_sync(hidpp,
 937			HIDPP_PAGE_ROOT_IDX,
 938			CMD_ROOT_GET_FEATURE,
 939			params, 2, &response);
 940	if (ret)
 941		return ret;
 942
 943	if (response.fap.params[0] == 0)
 944		return -ENOENT;
 945
 946	*feature_index = response.fap.params[0];
 947	*feature_type = response.fap.params[1];
 948
 949	return ret;
 950}
 951
 952static int hidpp_root_get_protocol_version(struct hidpp_device *hidpp)
 953{
 954	const u8 ping_byte = 0x5a;
 955	u8 ping_data[3] = { 0, 0, ping_byte };
 956	struct hidpp_report response;
 957	int ret;
 958
 959	ret = hidpp_send_rap_command_sync(hidpp,
 960			REPORT_ID_HIDPP_SHORT,
 961			HIDPP_PAGE_ROOT_IDX,
 962			CMD_ROOT_GET_PROTOCOL_VERSION | LINUX_KERNEL_SW_ID,
 963			ping_data, sizeof(ping_data), &response);
 964
 965	if (ret == HIDPP_ERROR_INVALID_SUBID) {
 966		hidpp->protocol_major = 1;
 967		hidpp->protocol_minor = 0;
 968		goto print_version;
 969	}
 970
 971	/* the device might not be connected */
 972	if (ret == HIDPP_ERROR_RESOURCE_ERROR)
 973		return -EIO;
 974
 975	if (ret > 0) {
 976		hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
 977			__func__, ret);
 978		return -EPROTO;
 979	}
 980	if (ret)
 981		return ret;
 982
 983	if (response.rap.params[2] != ping_byte) {
 984		hid_err(hidpp->hid_dev, "%s: ping mismatch 0x%02x != 0x%02x\n",
 985			__func__, response.rap.params[2], ping_byte);
 986		return -EPROTO;
 987	}
 988
 989	hidpp->protocol_major = response.rap.params[0];
 990	hidpp->protocol_minor = response.rap.params[1];
 991
 992print_version:
 993	if (!hidpp->connected_once) {
 994		hid_info(hidpp->hid_dev, "HID++ %u.%u device connected.\n",
 995			 hidpp->protocol_major, hidpp->protocol_minor);
 996		hidpp->connected_once = true;
 997	} else
 998		hid_dbg(hidpp->hid_dev, "HID++ %u.%u device connected.\n",
 999			 hidpp->protocol_major, hidpp->protocol_minor);
1000	return 0;
1001}
1002
1003/* -------------------------------------------------------------------------- */
1004/* 0x0003: Device Information                                                 */
1005/* -------------------------------------------------------------------------- */
1006
1007#define HIDPP_PAGE_DEVICE_INFORMATION			0x0003
1008
1009#define CMD_GET_DEVICE_INFO				0x00
1010
1011static int hidpp_get_serial(struct hidpp_device *hidpp, u32 *serial)
1012{
1013	struct hidpp_report response;
1014	u8 feature_type;
1015	u8 feature_index;
1016	int ret;
1017
1018	ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_DEVICE_INFORMATION,
1019				     &feature_index,
1020				     &feature_type);
1021	if (ret)
1022		return ret;
1023
1024	ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1025					  CMD_GET_DEVICE_INFO,
1026					  NULL, 0, &response);
1027	if (ret)
1028		return ret;
1029
1030	/* See hidpp_unifying_get_serial() */
1031	*serial = *((u32 *)&response.rap.params[1]);
1032	return 0;
1033}
1034
1035static int hidpp_serial_init(struct hidpp_device *hidpp)
1036{
1037	struct hid_device *hdev = hidpp->hid_dev;
1038	u32 serial;
1039	int ret;
1040
1041	ret = hidpp_get_serial(hidpp, &serial);
1042	if (ret)
1043		return ret;
1044
1045	snprintf(hdev->uniq, sizeof(hdev->uniq), "%4phD", &serial);
1046	dbg_hid("HID++ DeviceInformation: Got serial: %s\n", hdev->uniq);
1047
1048	return 0;
1049}
1050
1051/* -------------------------------------------------------------------------- */
1052/* 0x0005: GetDeviceNameType                                                  */
1053/* -------------------------------------------------------------------------- */
1054
1055#define HIDPP_PAGE_GET_DEVICE_NAME_TYPE			0x0005
1056
1057#define CMD_GET_DEVICE_NAME_TYPE_GET_COUNT		0x00
1058#define CMD_GET_DEVICE_NAME_TYPE_GET_DEVICE_NAME	0x10
1059#define CMD_GET_DEVICE_NAME_TYPE_GET_TYPE		0x20
1060
1061static int hidpp_devicenametype_get_count(struct hidpp_device *hidpp,
1062	u8 feature_index, u8 *nameLength)
1063{
1064	struct hidpp_report response;
1065	int ret;
1066
1067	ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1068		CMD_GET_DEVICE_NAME_TYPE_GET_COUNT, NULL, 0, &response);
1069
1070	if (ret > 0) {
1071		hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1072			__func__, ret);
1073		return -EPROTO;
1074	}
1075	if (ret)
1076		return ret;
1077
1078	*nameLength = response.fap.params[0];
1079
1080	return ret;
1081}
1082
1083static int hidpp_devicenametype_get_device_name(struct hidpp_device *hidpp,
1084	u8 feature_index, u8 char_index, char *device_name, int len_buf)
1085{
1086	struct hidpp_report response;
1087	int ret, i;
1088	int count;
1089
1090	ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1091		CMD_GET_DEVICE_NAME_TYPE_GET_DEVICE_NAME, &char_index, 1,
1092		&response);
1093
1094	if (ret > 0) {
1095		hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1096			__func__, ret);
1097		return -EPROTO;
1098	}
1099	if (ret)
1100		return ret;
1101
1102	switch (response.report_id) {
1103	case REPORT_ID_HIDPP_VERY_LONG:
1104		count = hidpp->very_long_report_length - 4;
1105		break;
1106	case REPORT_ID_HIDPP_LONG:
1107		count = HIDPP_REPORT_LONG_LENGTH - 4;
1108		break;
1109	case REPORT_ID_HIDPP_SHORT:
1110		count = HIDPP_REPORT_SHORT_LENGTH - 4;
1111		break;
1112	default:
1113		return -EPROTO;
1114	}
1115
1116	if (len_buf < count)
1117		count = len_buf;
1118
1119	for (i = 0; i < count; i++)
1120		device_name[i] = response.fap.params[i];
1121
1122	return count;
1123}
1124
1125static char *hidpp_get_device_name(struct hidpp_device *hidpp)
1126{
1127	u8 feature_type;
1128	u8 feature_index;
1129	u8 __name_length;
1130	char *name;
1131	unsigned index = 0;
1132	int ret;
1133
1134	ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_GET_DEVICE_NAME_TYPE,
1135		&feature_index, &feature_type);
1136	if (ret)
1137		return NULL;
1138
1139	ret = hidpp_devicenametype_get_count(hidpp, feature_index,
1140		&__name_length);
1141	if (ret)
1142		return NULL;
1143
1144	name = kzalloc(__name_length + 1, GFP_KERNEL);
1145	if (!name)
1146		return NULL;
1147
1148	while (index < __name_length) {
1149		ret = hidpp_devicenametype_get_device_name(hidpp,
1150			feature_index, index, name + index,
1151			__name_length - index);
1152		if (ret <= 0) {
1153			kfree(name);
1154			return NULL;
1155		}
1156		index += ret;
1157	}
1158
1159	/* include the terminating '\0' */
1160	hidpp_prefix_name(&name, __name_length + 1);
1161
1162	return name;
1163}
1164
1165/* -------------------------------------------------------------------------- */
1166/* 0x1000: Battery level status                                               */
1167/* -------------------------------------------------------------------------- */
1168
1169#define HIDPP_PAGE_BATTERY_LEVEL_STATUS				0x1000
1170
1171#define CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_LEVEL_STATUS	0x00
1172#define CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_CAPABILITY		0x10
1173
1174#define EVENT_BATTERY_LEVEL_STATUS_BROADCAST			0x00
1175
1176#define FLAG_BATTERY_LEVEL_DISABLE_OSD				BIT(0)
1177#define FLAG_BATTERY_LEVEL_MILEAGE				BIT(1)
1178#define FLAG_BATTERY_LEVEL_RECHARGEABLE				BIT(2)
1179
1180static int hidpp_map_battery_level(int capacity)
1181{
1182	if (capacity < 11)
1183		return POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
1184	/*
1185	 * The spec says this should be < 31 but some devices report 30
1186	 * with brand new batteries and Windows reports 30 as "Good".
1187	 */
1188	else if (capacity < 30)
1189		return POWER_SUPPLY_CAPACITY_LEVEL_LOW;
1190	else if (capacity < 81)
1191		return POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
1192	return POWER_SUPPLY_CAPACITY_LEVEL_FULL;
1193}
1194
1195static int hidpp20_batterylevel_map_status_capacity(u8 data[3], int *capacity,
1196						    int *next_capacity,
1197						    int *level)
1198{
1199	int status;
1200
1201	*capacity = data[0];
1202	*next_capacity = data[1];
1203	*level = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
1204
1205	/* When discharging, we can rely on the device reported capacity.
1206	 * For all other states the device reports 0 (unknown).
1207	 */
1208	switch (data[2]) {
1209		case 0: /* discharging (in use) */
1210			status = POWER_SUPPLY_STATUS_DISCHARGING;
1211			*level = hidpp_map_battery_level(*capacity);
1212			break;
1213		case 1: /* recharging */
1214			status = POWER_SUPPLY_STATUS_CHARGING;
1215			break;
1216		case 2: /* charge in final stage */
1217			status = POWER_SUPPLY_STATUS_CHARGING;
1218			break;
1219		case 3: /* charge complete */
1220			status = POWER_SUPPLY_STATUS_FULL;
1221			*level = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
1222			*capacity = 100;
1223			break;
1224		case 4: /* recharging below optimal speed */
1225			status = POWER_SUPPLY_STATUS_CHARGING;
1226			break;
1227		/* 5 = invalid battery type
1228		   6 = thermal error
1229		   7 = other charging error */
1230		default:
1231			status = POWER_SUPPLY_STATUS_NOT_CHARGING;
1232			break;
1233	}
1234
1235	return status;
1236}
1237
1238static int hidpp20_batterylevel_get_battery_capacity(struct hidpp_device *hidpp,
1239						     u8 feature_index,
1240						     int *status,
1241						     int *capacity,
1242						     int *next_capacity,
1243						     int *level)
1244{
1245	struct hidpp_report response;
1246	int ret;
1247	u8 *params = (u8 *)response.fap.params;
1248
1249	ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1250					  CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_LEVEL_STATUS,
1251					  NULL, 0, &response);
1252	/* Ignore these intermittent errors */
1253	if (ret == HIDPP_ERROR_RESOURCE_ERROR)
1254		return -EIO;
1255	if (ret > 0) {
1256		hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1257			__func__, ret);
1258		return -EPROTO;
1259	}
1260	if (ret)
1261		return ret;
1262
1263	*status = hidpp20_batterylevel_map_status_capacity(params, capacity,
1264							   next_capacity,
1265							   level);
1266
1267	return 0;
1268}
1269
1270static int hidpp20_batterylevel_get_battery_info(struct hidpp_device *hidpp,
1271						  u8 feature_index)
1272{
1273	struct hidpp_report response;
1274	int ret;
1275	u8 *params = (u8 *)response.fap.params;
1276	unsigned int level_count, flags;
1277
1278	ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1279					  CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_CAPABILITY,
1280					  NULL, 0, &response);
1281	if (ret > 0) {
1282		hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1283			__func__, ret);
1284		return -EPROTO;
1285	}
1286	if (ret)
1287		return ret;
1288
1289	level_count = params[0];
1290	flags = params[1];
1291
1292	if (level_count < 10 || !(flags & FLAG_BATTERY_LEVEL_MILEAGE))
1293		hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS;
1294	else
1295		hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_MILEAGE;
1296
1297	return 0;
1298}
1299
1300static int hidpp20_query_battery_info_1000(struct hidpp_device *hidpp)
1301{
1302	u8 feature_type;
1303	int ret;
1304	int status, capacity, next_capacity, level;
1305
1306	if (hidpp->battery.feature_index == 0xff) {
1307		ret = hidpp_root_get_feature(hidpp,
1308					     HIDPP_PAGE_BATTERY_LEVEL_STATUS,
1309					     &hidpp->battery.feature_index,
1310					     &feature_type);
1311		if (ret)
1312			return ret;
1313	}
1314
1315	ret = hidpp20_batterylevel_get_battery_capacity(hidpp,
1316						hidpp->battery.feature_index,
1317						&status, &capacity,
1318						&next_capacity, &level);
1319	if (ret)
1320		return ret;
1321
1322	ret = hidpp20_batterylevel_get_battery_info(hidpp,
1323						hidpp->battery.feature_index);
1324	if (ret)
1325		return ret;
1326
1327	hidpp->battery.status = status;
1328	hidpp->battery.capacity = capacity;
1329	hidpp->battery.level = level;
1330	/* the capacity is only available when discharging or full */
1331	hidpp->battery.online = status == POWER_SUPPLY_STATUS_DISCHARGING ||
1332				status == POWER_SUPPLY_STATUS_FULL;
1333
1334	return 0;
1335}
1336
1337static int hidpp20_battery_event_1000(struct hidpp_device *hidpp,
1338				 u8 *data, int size)
1339{
1340	struct hidpp_report *report = (struct hidpp_report *)data;
1341	int status, capacity, next_capacity, level;
1342	bool changed;
1343
1344	if (report->fap.feature_index != hidpp->battery.feature_index ||
1345	    report->fap.funcindex_clientid != EVENT_BATTERY_LEVEL_STATUS_BROADCAST)
1346		return 0;
1347
1348	status = hidpp20_batterylevel_map_status_capacity(report->fap.params,
1349							  &capacity,
1350							  &next_capacity,
1351							  &level);
1352
1353	/* the capacity is only available when discharging or full */
1354	hidpp->battery.online = status == POWER_SUPPLY_STATUS_DISCHARGING ||
1355				status == POWER_SUPPLY_STATUS_FULL;
1356
1357	changed = capacity != hidpp->battery.capacity ||
1358		  level != hidpp->battery.level ||
1359		  status != hidpp->battery.status;
1360
1361	if (changed) {
1362		hidpp->battery.level = level;
1363		hidpp->battery.capacity = capacity;
1364		hidpp->battery.status = status;
1365		if (hidpp->battery.ps)
1366			power_supply_changed(hidpp->battery.ps);
1367	}
1368
1369	return 0;
1370}
1371
1372/* -------------------------------------------------------------------------- */
1373/* 0x1001: Battery voltage                                                    */
1374/* -------------------------------------------------------------------------- */
1375
1376#define HIDPP_PAGE_BATTERY_VOLTAGE 0x1001
1377
1378#define CMD_BATTERY_VOLTAGE_GET_BATTERY_VOLTAGE 0x00
1379
1380#define EVENT_BATTERY_VOLTAGE_STATUS_BROADCAST 0x00
1381
1382static int hidpp20_battery_map_status_voltage(u8 data[3], int *voltage,
1383						int *level, int *charge_type)
1384{
1385	int status;
1386
1387	long flags = (long) data[2];
1388	*level = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
1389
1390	if (flags & 0x80)
1391		switch (flags & 0x07) {
1392		case 0:
1393			status = POWER_SUPPLY_STATUS_CHARGING;
1394			break;
1395		case 1:
1396			status = POWER_SUPPLY_STATUS_FULL;
1397			*level = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
1398			break;
1399		case 2:
1400			status = POWER_SUPPLY_STATUS_NOT_CHARGING;
1401			break;
1402		default:
1403			status = POWER_SUPPLY_STATUS_UNKNOWN;
1404			break;
1405		}
1406	else
1407		status = POWER_SUPPLY_STATUS_DISCHARGING;
1408
1409	*charge_type = POWER_SUPPLY_CHARGE_TYPE_STANDARD;
1410	if (test_bit(3, &flags)) {
1411		*charge_type = POWER_SUPPLY_CHARGE_TYPE_FAST;
1412	}
1413	if (test_bit(4, &flags)) {
1414		*charge_type = POWER_SUPPLY_CHARGE_TYPE_TRICKLE;
1415	}
1416	if (test_bit(5, &flags)) {
1417		*level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
1418	}
1419
1420	*voltage = get_unaligned_be16(data);
1421
1422	return status;
1423}
1424
1425static int hidpp20_battery_get_battery_voltage(struct hidpp_device *hidpp,
1426						 u8 feature_index,
1427						 int *status, int *voltage,
1428						 int *level, int *charge_type)
1429{
1430	struct hidpp_report response;
1431	int ret;
1432	u8 *params = (u8 *)response.fap.params;
1433
1434	ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1435					  CMD_BATTERY_VOLTAGE_GET_BATTERY_VOLTAGE,
1436					  NULL, 0, &response);
1437
1438	if (ret > 0) {
1439		hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1440			__func__, ret);
1441		return -EPROTO;
1442	}
1443	if (ret)
1444		return ret;
1445
1446	hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_VOLTAGE;
1447
1448	*status = hidpp20_battery_map_status_voltage(params, voltage,
1449						     level, charge_type);
1450
1451	return 0;
1452}
1453
1454static int hidpp20_map_battery_capacity(struct hid_device *hid_dev, int voltage)
1455{
1456	/* NB: This voltage curve doesn't necessarily map perfectly to all
1457	 * devices that implement the BATTERY_VOLTAGE feature. This is because
1458	 * there are a few devices that use different battery technology.
1459	 */
1460
1461	static const int voltages[100] = {
1462		4186, 4156, 4143, 4133, 4122, 4113, 4103, 4094, 4086, 4075,
1463		4067, 4059, 4051, 4043, 4035, 4027, 4019, 4011, 4003, 3997,
1464		3989, 3983, 3976, 3969, 3961, 3955, 3949, 3942, 3935, 3929,
1465		3922, 3916, 3909, 3902, 3896, 3890, 3883, 3877, 3870, 3865,
1466		3859, 3853, 3848, 3842, 3837, 3833, 3828, 3824, 3819, 3815,
1467		3811, 3808, 3804, 3800, 3797, 3793, 3790, 3787, 3784, 3781,
1468		3778, 3775, 3772, 3770, 3767, 3764, 3762, 3759, 3757, 3754,
1469		3751, 3748, 3744, 3741, 3737, 3734, 3730, 3726, 3724, 3720,
1470		3717, 3714, 3710, 3706, 3702, 3697, 3693, 3688, 3683, 3677,
1471		3671, 3666, 3662, 3658, 3654, 3646, 3633, 3612, 3579, 3537
1472	};
1473
1474	int i;
1475
 
 
1476	if (unlikely(voltage < 3500 || voltage >= 5000))
1477		hid_warn_once(hid_dev,
1478			      "%s: possibly using the wrong voltage curve\n",
1479			      __func__);
1480
1481	for (i = 0; i < ARRAY_SIZE(voltages); i++) {
1482		if (voltage >= voltages[i])
1483			return ARRAY_SIZE(voltages) - i;
1484	}
1485
1486	return 0;
1487}
1488
1489static int hidpp20_query_battery_voltage_info(struct hidpp_device *hidpp)
1490{
1491	u8 feature_type;
1492	int ret;
1493	int status, voltage, level, charge_type;
1494
1495	if (hidpp->battery.voltage_feature_index == 0xff) {
1496		ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_BATTERY_VOLTAGE,
1497					     &hidpp->battery.voltage_feature_index,
1498					     &feature_type);
1499		if (ret)
1500			return ret;
1501	}
1502
1503	ret = hidpp20_battery_get_battery_voltage(hidpp,
1504						  hidpp->battery.voltage_feature_index,
1505						  &status, &voltage, &level, &charge_type);
1506
1507	if (ret)
1508		return ret;
1509
1510	hidpp->battery.status = status;
1511	hidpp->battery.voltage = voltage;
1512	hidpp->battery.capacity = hidpp20_map_battery_capacity(hidpp->hid_dev,
1513							       voltage);
1514	hidpp->battery.level = level;
1515	hidpp->battery.charge_type = charge_type;
1516	hidpp->battery.online = status != POWER_SUPPLY_STATUS_NOT_CHARGING;
1517
1518	return 0;
1519}
1520
1521static int hidpp20_battery_voltage_event(struct hidpp_device *hidpp,
1522					    u8 *data, int size)
1523{
1524	struct hidpp_report *report = (struct hidpp_report *)data;
1525	int status, voltage, level, charge_type;
1526
1527	if (report->fap.feature_index != hidpp->battery.voltage_feature_index ||
1528		report->fap.funcindex_clientid != EVENT_BATTERY_VOLTAGE_STATUS_BROADCAST)
1529		return 0;
1530
1531	status = hidpp20_battery_map_status_voltage(report->fap.params, &voltage,
1532						    &level, &charge_type);
1533
1534	hidpp->battery.online = status != POWER_SUPPLY_STATUS_NOT_CHARGING;
1535
1536	if (voltage != hidpp->battery.voltage || status != hidpp->battery.status) {
1537		hidpp->battery.voltage = voltage;
1538		hidpp->battery.capacity = hidpp20_map_battery_capacity(hidpp->hid_dev,
1539								       voltage);
1540		hidpp->battery.status = status;
1541		hidpp->battery.level = level;
1542		hidpp->battery.charge_type = charge_type;
1543		if (hidpp->battery.ps)
1544			power_supply_changed(hidpp->battery.ps);
1545	}
1546	return 0;
1547}
1548
1549/* -------------------------------------------------------------------------- */
1550/* 0x1004: Unified battery                                                    */
1551/* -------------------------------------------------------------------------- */
1552
1553#define HIDPP_PAGE_UNIFIED_BATTERY				0x1004
1554
1555#define CMD_UNIFIED_BATTERY_GET_CAPABILITIES			0x00
1556#define CMD_UNIFIED_BATTERY_GET_STATUS				0x10
1557
1558#define EVENT_UNIFIED_BATTERY_STATUS_EVENT			0x00
1559
1560#define FLAG_UNIFIED_BATTERY_LEVEL_CRITICAL			BIT(0)
1561#define FLAG_UNIFIED_BATTERY_LEVEL_LOW				BIT(1)
1562#define FLAG_UNIFIED_BATTERY_LEVEL_GOOD				BIT(2)
1563#define FLAG_UNIFIED_BATTERY_LEVEL_FULL				BIT(3)
1564
1565#define FLAG_UNIFIED_BATTERY_FLAGS_RECHARGEABLE			BIT(0)
1566#define FLAG_UNIFIED_BATTERY_FLAGS_STATE_OF_CHARGE		BIT(1)
1567
1568static int hidpp20_unifiedbattery_get_capabilities(struct hidpp_device *hidpp,
1569						   u8 feature_index)
1570{
1571	struct hidpp_report response;
1572	int ret;
1573	u8 *params = (u8 *)response.fap.params;
1574
1575	if (hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS ||
1576	    hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_PERCENTAGE) {
1577		/* we have already set the device capabilities, so let's skip */
1578		return 0;
1579	}
1580
1581	ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1582					  CMD_UNIFIED_BATTERY_GET_CAPABILITIES,
1583					  NULL, 0, &response);
1584	/* Ignore these intermittent errors */
1585	if (ret == HIDPP_ERROR_RESOURCE_ERROR)
1586		return -EIO;
1587	if (ret > 0) {
1588		hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1589			__func__, ret);
1590		return -EPROTO;
1591	}
1592	if (ret)
1593		return ret;
1594
1595	/*
1596	 * If the device supports state of charge (battery percentage) we won't
1597	 * export the battery level information. there are 4 possible battery
1598	 * levels and they all are optional, this means that the device might
1599	 * not support any of them, we are just better off with the battery
1600	 * percentage.
1601	 */
1602	if (params[1] & FLAG_UNIFIED_BATTERY_FLAGS_STATE_OF_CHARGE) {
1603		hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_PERCENTAGE;
1604		hidpp->battery.supported_levels_1004 = 0;
1605	} else {
1606		hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS;
1607		hidpp->battery.supported_levels_1004 = params[0];
1608	}
1609
1610	return 0;
1611}
1612
1613static int hidpp20_unifiedbattery_map_status(struct hidpp_device *hidpp,
1614					     u8 charging_status,
1615					     u8 external_power_status)
1616{
1617	int status;
1618
1619	switch (charging_status) {
1620		case 0: /* discharging */
1621			status = POWER_SUPPLY_STATUS_DISCHARGING;
1622			break;
1623		case 1: /* charging */
1624		case 2: /* charging slow */
1625			status = POWER_SUPPLY_STATUS_CHARGING;
1626			break;
1627		case 3: /* complete */
1628			status = POWER_SUPPLY_STATUS_FULL;
1629			break;
1630		case 4: /* error */
1631			status = POWER_SUPPLY_STATUS_NOT_CHARGING;
1632			hid_info(hidpp->hid_dev, "%s: charging error",
1633				 hidpp->name);
1634			break;
1635		default:
1636			status = POWER_SUPPLY_STATUS_NOT_CHARGING;
1637			break;
1638	}
1639
1640	return status;
1641}
1642
1643static int hidpp20_unifiedbattery_map_level(struct hidpp_device *hidpp,
1644					    u8 battery_level)
1645{
1646	/* cler unsupported level bits */
1647	battery_level &= hidpp->battery.supported_levels_1004;
1648
1649	if (battery_level & FLAG_UNIFIED_BATTERY_LEVEL_FULL)
1650		return POWER_SUPPLY_CAPACITY_LEVEL_FULL;
1651	else if (battery_level & FLAG_UNIFIED_BATTERY_LEVEL_GOOD)
1652		return POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
1653	else if (battery_level & FLAG_UNIFIED_BATTERY_LEVEL_LOW)
1654		return POWER_SUPPLY_CAPACITY_LEVEL_LOW;
1655	else if (battery_level & FLAG_UNIFIED_BATTERY_LEVEL_CRITICAL)
1656		return POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
1657
1658	return POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
1659}
1660
1661static int hidpp20_unifiedbattery_get_status(struct hidpp_device *hidpp,
1662					     u8 feature_index,
1663					     u8 *state_of_charge,
1664					     int *status,
1665					     int *level)
1666{
1667	struct hidpp_report response;
1668	int ret;
1669	u8 *params = (u8 *)response.fap.params;
1670
1671	ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1672					  CMD_UNIFIED_BATTERY_GET_STATUS,
1673					  NULL, 0, &response);
1674	/* Ignore these intermittent errors */
1675	if (ret == HIDPP_ERROR_RESOURCE_ERROR)
1676		return -EIO;
1677	if (ret > 0) {
1678		hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1679			__func__, ret);
1680		return -EPROTO;
1681	}
1682	if (ret)
1683		return ret;
1684
1685	*state_of_charge = params[0];
1686	*status = hidpp20_unifiedbattery_map_status(hidpp, params[2], params[3]);
1687	*level = hidpp20_unifiedbattery_map_level(hidpp, params[1]);
1688
1689	return 0;
1690}
1691
1692static int hidpp20_query_battery_info_1004(struct hidpp_device *hidpp)
1693{
1694	u8 feature_type;
1695	int ret;
1696	u8 state_of_charge;
1697	int status, level;
1698
1699	if (hidpp->battery.feature_index == 0xff) {
1700		ret = hidpp_root_get_feature(hidpp,
1701					     HIDPP_PAGE_UNIFIED_BATTERY,
1702					     &hidpp->battery.feature_index,
1703					     &feature_type);
1704		if (ret)
1705			return ret;
1706	}
1707
1708	ret = hidpp20_unifiedbattery_get_capabilities(hidpp,
1709					hidpp->battery.feature_index);
1710	if (ret)
1711		return ret;
1712
1713	ret = hidpp20_unifiedbattery_get_status(hidpp,
1714						hidpp->battery.feature_index,
1715						&state_of_charge,
1716						&status,
1717						&level);
1718	if (ret)
1719		return ret;
1720
1721	hidpp->capabilities |= HIDPP_CAPABILITY_UNIFIED_BATTERY;
1722	hidpp->battery.capacity = state_of_charge;
1723	hidpp->battery.status = status;
1724	hidpp->battery.level = level;
1725	hidpp->battery.online = true;
1726
1727	return 0;
1728}
1729
1730static int hidpp20_battery_event_1004(struct hidpp_device *hidpp,
1731				 u8 *data, int size)
1732{
1733	struct hidpp_report *report = (struct hidpp_report *)data;
1734	u8 *params = (u8 *)report->fap.params;
1735	int state_of_charge, status, level;
1736	bool changed;
1737
1738	if (report->fap.feature_index != hidpp->battery.feature_index ||
1739	    report->fap.funcindex_clientid != EVENT_UNIFIED_BATTERY_STATUS_EVENT)
1740		return 0;
1741
1742	state_of_charge = params[0];
1743	status = hidpp20_unifiedbattery_map_status(hidpp, params[2], params[3]);
1744	level = hidpp20_unifiedbattery_map_level(hidpp, params[1]);
1745
1746	changed = status != hidpp->battery.status ||
1747		  (state_of_charge != hidpp->battery.capacity &&
1748		   hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_PERCENTAGE) ||
1749		  (level != hidpp->battery.level &&
1750		   hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS);
1751
1752	if (changed) {
1753		hidpp->battery.capacity = state_of_charge;
1754		hidpp->battery.status = status;
1755		hidpp->battery.level = level;
1756		if (hidpp->battery.ps)
1757			power_supply_changed(hidpp->battery.ps);
1758	}
1759
1760	return 0;
1761}
1762
1763/* -------------------------------------------------------------------------- */
1764/* Battery feature helpers                                                    */
1765/* -------------------------------------------------------------------------- */
1766
1767static enum power_supply_property hidpp_battery_props[] = {
1768	POWER_SUPPLY_PROP_ONLINE,
1769	POWER_SUPPLY_PROP_STATUS,
1770	POWER_SUPPLY_PROP_SCOPE,
1771	POWER_SUPPLY_PROP_MODEL_NAME,
1772	POWER_SUPPLY_PROP_MANUFACTURER,
1773	POWER_SUPPLY_PROP_SERIAL_NUMBER,
1774	0, /* placeholder for POWER_SUPPLY_PROP_CAPACITY, */
1775	0, /* placeholder for POWER_SUPPLY_PROP_CAPACITY_LEVEL, */
1776	0, /* placeholder for POWER_SUPPLY_PROP_VOLTAGE_NOW, */
1777};
1778
1779static int hidpp_battery_get_property(struct power_supply *psy,
1780				      enum power_supply_property psp,
1781				      union power_supply_propval *val)
1782{
1783	struct hidpp_device *hidpp = power_supply_get_drvdata(psy);
1784	int ret = 0;
1785
1786	switch(psp) {
1787		case POWER_SUPPLY_PROP_STATUS:
1788			val->intval = hidpp->battery.status;
1789			break;
1790		case POWER_SUPPLY_PROP_CAPACITY:
1791			val->intval = hidpp->battery.capacity;
1792			break;
1793		case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
1794			val->intval = hidpp->battery.level;
1795			break;
1796		case POWER_SUPPLY_PROP_SCOPE:
1797			val->intval = POWER_SUPPLY_SCOPE_DEVICE;
1798			break;
1799		case POWER_SUPPLY_PROP_ONLINE:
1800			val->intval = hidpp->battery.online;
1801			break;
1802		case POWER_SUPPLY_PROP_MODEL_NAME:
1803			if (!strncmp(hidpp->name, "Logitech ", 9))
1804				val->strval = hidpp->name + 9;
1805			else
1806				val->strval = hidpp->name;
1807			break;
1808		case POWER_SUPPLY_PROP_MANUFACTURER:
1809			val->strval = "Logitech";
1810			break;
1811		case POWER_SUPPLY_PROP_SERIAL_NUMBER:
1812			val->strval = hidpp->hid_dev->uniq;
1813			break;
1814		case POWER_SUPPLY_PROP_VOLTAGE_NOW:
1815			/* hardware reports voltage in mV. sysfs expects uV */
1816			val->intval = hidpp->battery.voltage * 1000;
1817			break;
1818		case POWER_SUPPLY_PROP_CHARGE_TYPE:
1819			val->intval = hidpp->battery.charge_type;
1820			break;
1821		default:
1822			ret = -EINVAL;
1823			break;
1824	}
1825
1826	return ret;
1827}
1828
1829/* -------------------------------------------------------------------------- */
1830/* 0x1d4b: Wireless device status                                             */
1831/* -------------------------------------------------------------------------- */
1832#define HIDPP_PAGE_WIRELESS_DEVICE_STATUS			0x1d4b
1833
1834static int hidpp_get_wireless_feature_index(struct hidpp_device *hidpp, u8 *feature_index)
1835{
1836	u8 feature_type;
1837	int ret;
1838
1839	ret = hidpp_root_get_feature(hidpp,
1840				     HIDPP_PAGE_WIRELESS_DEVICE_STATUS,
1841				     feature_index, &feature_type);
 
1842
1843	return ret;
1844}
1845
1846/* -------------------------------------------------------------------------- */
1847/* 0x1f20: ADC measurement                                                    */
1848/* -------------------------------------------------------------------------- */
1849
1850#define HIDPP_PAGE_ADC_MEASUREMENT 0x1f20
1851
1852#define CMD_ADC_MEASUREMENT_GET_ADC_MEASUREMENT 0x00
1853
1854#define EVENT_ADC_MEASUREMENT_STATUS_BROADCAST 0x00
1855
1856static int hidpp20_map_adc_measurement_1f20_capacity(struct hid_device *hid_dev, int voltage)
1857{
1858	/* NB: This voltage curve doesn't necessarily map perfectly to all
1859	 * devices that implement the ADC_MEASUREMENT feature. This is because
1860	 * there are a few devices that use different battery technology.
1861	 *
1862	 * Adapted from:
1863	 * https://github.com/Sapd/HeadsetControl/blob/acd972be0468e039b93aae81221f20a54d2d60f7/src/devices/logitech_g633_g933_935.c#L44-L52
1864	 */
1865	static const int voltages[100] = {
1866		4030, 4024, 4018, 4011, 4003, 3994, 3985, 3975, 3963, 3951,
1867		3937, 3922, 3907, 3893, 3880, 3868, 3857, 3846, 3837, 3828,
1868		3820, 3812, 3805, 3798, 3791, 3785, 3779, 3773, 3768, 3762,
1869		3757, 3752, 3747, 3742, 3738, 3733, 3729, 3724, 3720, 3716,
1870		3712, 3708, 3704, 3700, 3696, 3692, 3688, 3685, 3681, 3677,
1871		3674, 3670, 3667, 3663, 3660, 3657, 3653, 3650, 3646, 3643,
1872		3640, 3637, 3633, 3630, 3627, 3624, 3620, 3617, 3614, 3611,
1873		3608, 3604, 3601, 3598, 3595, 3592, 3589, 3585, 3582, 3579,
1874		3576, 3573, 3569, 3566, 3563, 3560, 3556, 3553, 3550, 3546,
1875		3543, 3539, 3536, 3532, 3529, 3525, 3499, 3466, 3433, 3399,
1876	};
1877
1878	int i;
1879
1880	if (voltage == 0)
1881		return 0;
1882
1883	if (unlikely(voltage < 3400 || voltage >= 5000))
1884		hid_warn_once(hid_dev,
1885			      "%s: possibly using the wrong voltage curve\n",
1886			      __func__);
1887
1888	for (i = 0; i < ARRAY_SIZE(voltages); i++) {
1889		if (voltage >= voltages[i])
1890			return ARRAY_SIZE(voltages) - i;
1891	}
1892
1893	return 0;
1894}
1895
1896static int hidpp20_map_adc_measurement_1f20(u8 data[3], int *voltage)
1897{
1898	int status;
1899	u8 flags;
1900
1901	flags = data[2];
1902
1903	switch (flags) {
1904	case 0x01:
1905		status = POWER_SUPPLY_STATUS_DISCHARGING;
1906		break;
1907	case 0x03:
1908		status = POWER_SUPPLY_STATUS_CHARGING;
1909		break;
1910	case 0x07:
1911		status = POWER_SUPPLY_STATUS_FULL;
1912		break;
1913	case 0x0F:
1914	default:
1915		status = POWER_SUPPLY_STATUS_UNKNOWN;
1916		break;
1917	}
1918
1919	*voltage = get_unaligned_be16(data);
1920
1921	dbg_hid("Parsed 1f20 data as flag 0x%02x voltage %dmV\n",
1922		flags, *voltage);
1923
1924	return status;
1925}
1926
1927/* Return value is whether the device is online */
1928static bool hidpp20_get_adc_measurement_1f20(struct hidpp_device *hidpp,
1929						 u8 feature_index,
1930						 int *status, int *voltage)
1931{
1932	struct hidpp_report response;
1933	int ret;
1934	u8 *params = (u8 *)response.fap.params;
1935
1936	*status = POWER_SUPPLY_STATUS_UNKNOWN;
1937	*voltage = 0;
1938	ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1939					  CMD_ADC_MEASUREMENT_GET_ADC_MEASUREMENT,
1940					  NULL, 0, &response);
1941
1942	if (ret > 0) {
1943		hid_dbg(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1944			__func__, ret);
1945		return false;
1946	}
1947
1948	*status = hidpp20_map_adc_measurement_1f20(params, voltage);
1949	return true;
1950}
1951
1952static int hidpp20_query_adc_measurement_info_1f20(struct hidpp_device *hidpp)
1953{
1954	u8 feature_type;
1955
1956	if (hidpp->battery.adc_measurement_feature_index == 0xff) {
1957		int ret;
1958
1959		ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_ADC_MEASUREMENT,
1960					     &hidpp->battery.adc_measurement_feature_index,
1961					     &feature_type);
1962		if (ret)
1963			return ret;
1964
1965		hidpp->capabilities |= HIDPP_CAPABILITY_ADC_MEASUREMENT;
1966	}
1967
1968	hidpp->battery.online = hidpp20_get_adc_measurement_1f20(hidpp,
1969								 hidpp->battery.adc_measurement_feature_index,
1970								 &hidpp->battery.status,
1971								 &hidpp->battery.voltage);
1972	hidpp->battery.capacity = hidpp20_map_adc_measurement_1f20_capacity(hidpp->hid_dev,
1973									    hidpp->battery.voltage);
1974	hidpp_update_usb_wireless_status(hidpp);
1975
1976	return 0;
1977}
1978
1979static int hidpp20_adc_measurement_event_1f20(struct hidpp_device *hidpp,
1980					    u8 *data, int size)
1981{
1982	struct hidpp_report *report = (struct hidpp_report *)data;
1983	int status, voltage;
1984
1985	if (report->fap.feature_index != hidpp->battery.adc_measurement_feature_index ||
1986		report->fap.funcindex_clientid != EVENT_ADC_MEASUREMENT_STATUS_BROADCAST)
1987		return 0;
1988
1989	status = hidpp20_map_adc_measurement_1f20(report->fap.params, &voltage);
1990
1991	hidpp->battery.online = status != POWER_SUPPLY_STATUS_UNKNOWN;
1992
1993	if (voltage != hidpp->battery.voltage || status != hidpp->battery.status) {
1994		hidpp->battery.status = status;
1995		hidpp->battery.voltage = voltage;
1996		hidpp->battery.capacity = hidpp20_map_adc_measurement_1f20_capacity(hidpp->hid_dev, voltage);
1997		if (hidpp->battery.ps)
1998			power_supply_changed(hidpp->battery.ps);
1999		hidpp_update_usb_wireless_status(hidpp);
2000	}
2001	return 0;
2002}
2003
2004/* -------------------------------------------------------------------------- */
2005/* 0x2120: Hi-resolution scrolling                                            */
2006/* -------------------------------------------------------------------------- */
2007
2008#define HIDPP_PAGE_HI_RESOLUTION_SCROLLING			0x2120
2009
2010#define CMD_HI_RESOLUTION_SCROLLING_SET_HIGHRES_SCROLLING_MODE	0x10
2011
2012static int hidpp_hrs_set_highres_scrolling_mode(struct hidpp_device *hidpp,
2013	bool enabled, u8 *multiplier)
2014{
2015	u8 feature_index;
2016	u8 feature_type;
2017	int ret;
2018	u8 params[1];
2019	struct hidpp_report response;
2020
2021	ret = hidpp_root_get_feature(hidpp,
2022				     HIDPP_PAGE_HI_RESOLUTION_SCROLLING,
2023				     &feature_index,
2024				     &feature_type);
2025	if (ret)
2026		return ret;
2027
2028	params[0] = enabled ? BIT(0) : 0;
2029	ret = hidpp_send_fap_command_sync(hidpp, feature_index,
2030					  CMD_HI_RESOLUTION_SCROLLING_SET_HIGHRES_SCROLLING_MODE,
2031					  params, sizeof(params), &response);
2032	if (ret)
2033		return ret;
2034	*multiplier = response.fap.params[1];
2035	return 0;
2036}
2037
2038/* -------------------------------------------------------------------------- */
2039/* 0x2121: HiRes Wheel                                                        */
2040/* -------------------------------------------------------------------------- */
2041
2042#define HIDPP_PAGE_HIRES_WHEEL		0x2121
2043
2044#define CMD_HIRES_WHEEL_GET_WHEEL_CAPABILITY	0x00
2045#define CMD_HIRES_WHEEL_SET_WHEEL_MODE		0x20
2046
2047static int hidpp_hrw_get_wheel_capability(struct hidpp_device *hidpp,
2048	u8 *multiplier)
2049{
2050	u8 feature_index;
2051	u8 feature_type;
2052	int ret;
2053	struct hidpp_report response;
2054
2055	ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_HIRES_WHEEL,
2056				     &feature_index, &feature_type);
2057	if (ret)
2058		goto return_default;
2059
2060	ret = hidpp_send_fap_command_sync(hidpp, feature_index,
2061					  CMD_HIRES_WHEEL_GET_WHEEL_CAPABILITY,
2062					  NULL, 0, &response);
2063	if (ret)
2064		goto return_default;
2065
2066	*multiplier = response.fap.params[0];
2067	return 0;
2068return_default:
2069	hid_warn(hidpp->hid_dev,
2070		 "Couldn't get wheel multiplier (error %d)\n", ret);
2071	return ret;
2072}
2073
2074static int hidpp_hrw_set_wheel_mode(struct hidpp_device *hidpp, bool invert,
2075	bool high_resolution, bool use_hidpp)
2076{
2077	u8 feature_index;
2078	u8 feature_type;
2079	int ret;
2080	u8 params[1];
2081	struct hidpp_report response;
2082
2083	ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_HIRES_WHEEL,
2084				     &feature_index, &feature_type);
2085	if (ret)
2086		return ret;
2087
2088	params[0] = (invert          ? BIT(2) : 0) |
2089		    (high_resolution ? BIT(1) : 0) |
2090		    (use_hidpp       ? BIT(0) : 0);
2091
2092	return hidpp_send_fap_command_sync(hidpp, feature_index,
2093					   CMD_HIRES_WHEEL_SET_WHEEL_MODE,
2094					   params, sizeof(params), &response);
2095}
2096
2097/* -------------------------------------------------------------------------- */
2098/* 0x4301: Solar Keyboard                                                     */
2099/* -------------------------------------------------------------------------- */
2100
2101#define HIDPP_PAGE_SOLAR_KEYBOARD			0x4301
2102
2103#define CMD_SOLAR_SET_LIGHT_MEASURE			0x00
2104
2105#define EVENT_SOLAR_BATTERY_BROADCAST			0x00
2106#define EVENT_SOLAR_BATTERY_LIGHT_MEASURE		0x10
2107#define EVENT_SOLAR_CHECK_LIGHT_BUTTON			0x20
2108
2109static int hidpp_solar_request_battery_event(struct hidpp_device *hidpp)
2110{
2111	struct hidpp_report response;
2112	u8 params[2] = { 1, 1 };
2113	u8 feature_type;
2114	int ret;
2115
2116	if (hidpp->battery.feature_index == 0xff) {
2117		ret = hidpp_root_get_feature(hidpp,
2118					     HIDPP_PAGE_SOLAR_KEYBOARD,
2119					     &hidpp->battery.solar_feature_index,
2120					     &feature_type);
2121		if (ret)
2122			return ret;
2123	}
2124
2125	ret = hidpp_send_fap_command_sync(hidpp,
2126					  hidpp->battery.solar_feature_index,
2127					  CMD_SOLAR_SET_LIGHT_MEASURE,
2128					  params, 2, &response);
2129	if (ret > 0) {
2130		hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
2131			__func__, ret);
2132		return -EPROTO;
2133	}
2134	if (ret)
2135		return ret;
2136
2137	hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_MILEAGE;
2138
2139	return 0;
2140}
2141
2142static int hidpp_solar_battery_event(struct hidpp_device *hidpp,
2143				     u8 *data, int size)
2144{
2145	struct hidpp_report *report = (struct hidpp_report *)data;
2146	int capacity, lux, status;
2147	u8 function;
2148
2149	function = report->fap.funcindex_clientid;
2150
2151
2152	if (report->fap.feature_index != hidpp->battery.solar_feature_index ||
2153	    !(function == EVENT_SOLAR_BATTERY_BROADCAST ||
2154	      function == EVENT_SOLAR_BATTERY_LIGHT_MEASURE ||
2155	      function == EVENT_SOLAR_CHECK_LIGHT_BUTTON))
2156		return 0;
2157
2158	capacity = report->fap.params[0];
2159
2160	switch (function) {
2161	case EVENT_SOLAR_BATTERY_LIGHT_MEASURE:
2162		lux = (report->fap.params[1] << 8) | report->fap.params[2];
2163		if (lux > 200)
2164			status = POWER_SUPPLY_STATUS_CHARGING;
2165		else
2166			status = POWER_SUPPLY_STATUS_DISCHARGING;
2167		break;
2168	case EVENT_SOLAR_CHECK_LIGHT_BUTTON:
2169	default:
2170		if (capacity < hidpp->battery.capacity)
2171			status = POWER_SUPPLY_STATUS_DISCHARGING;
2172		else
2173			status = POWER_SUPPLY_STATUS_CHARGING;
2174
2175	}
2176
2177	if (capacity == 100)
2178		status = POWER_SUPPLY_STATUS_FULL;
2179
2180	hidpp->battery.online = true;
2181	if (capacity != hidpp->battery.capacity ||
2182	    status != hidpp->battery.status) {
2183		hidpp->battery.capacity = capacity;
2184		hidpp->battery.status = status;
2185		if (hidpp->battery.ps)
2186			power_supply_changed(hidpp->battery.ps);
2187	}
2188
2189	return 0;
2190}
2191
2192/* -------------------------------------------------------------------------- */
2193/* 0x6010: Touchpad FW items                                                  */
2194/* -------------------------------------------------------------------------- */
2195
2196#define HIDPP_PAGE_TOUCHPAD_FW_ITEMS			0x6010
2197
2198#define CMD_TOUCHPAD_FW_ITEMS_SET			0x10
2199
2200struct hidpp_touchpad_fw_items {
2201	uint8_t presence;
2202	uint8_t desired_state;
2203	uint8_t state;
2204	uint8_t persistent;
2205};
2206
2207/*
2208 * send a set state command to the device by reading the current items->state
2209 * field. items is then filled with the current state.
2210 */
2211static int hidpp_touchpad_fw_items_set(struct hidpp_device *hidpp,
2212				       u8 feature_index,
2213				       struct hidpp_touchpad_fw_items *items)
2214{
2215	struct hidpp_report response;
2216	int ret;
2217	u8 *params = (u8 *)response.fap.params;
2218
2219	ret = hidpp_send_fap_command_sync(hidpp, feature_index,
2220		CMD_TOUCHPAD_FW_ITEMS_SET, &items->state, 1, &response);
2221
2222	if (ret > 0) {
2223		hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
2224			__func__, ret);
2225		return -EPROTO;
2226	}
2227	if (ret)
2228		return ret;
2229
2230	items->presence = params[0];
2231	items->desired_state = params[1];
2232	items->state = params[2];
2233	items->persistent = params[3];
2234
2235	return 0;
2236}
2237
2238/* -------------------------------------------------------------------------- */
2239/* 0x6100: TouchPadRawXY                                                      */
2240/* -------------------------------------------------------------------------- */
2241
2242#define HIDPP_PAGE_TOUCHPAD_RAW_XY			0x6100
2243
2244#define CMD_TOUCHPAD_GET_RAW_INFO			0x00
2245#define CMD_TOUCHPAD_SET_RAW_REPORT_STATE		0x20
2246
2247#define EVENT_TOUCHPAD_RAW_XY				0x00
2248
2249#define TOUCHPAD_RAW_XY_ORIGIN_LOWER_LEFT		0x01
2250#define TOUCHPAD_RAW_XY_ORIGIN_UPPER_LEFT		0x03
2251
2252struct hidpp_touchpad_raw_info {
2253	u16 x_size;
2254	u16 y_size;
2255	u8 z_range;
2256	u8 area_range;
2257	u8 timestamp_unit;
2258	u8 maxcontacts;
2259	u8 origin;
2260	u16 res;
2261};
2262
2263struct hidpp_touchpad_raw_xy_finger {
2264	u8 contact_type;
2265	u8 contact_status;
2266	u16 x;
2267	u16 y;
2268	u8 z;
2269	u8 area;
2270	u8 finger_id;
2271};
2272
2273struct hidpp_touchpad_raw_xy {
2274	u16 timestamp;
2275	struct hidpp_touchpad_raw_xy_finger fingers[2];
2276	u8 spurious_flag;
2277	u8 end_of_frame;
2278	u8 finger_count;
2279	u8 button;
2280};
2281
2282static int hidpp_touchpad_get_raw_info(struct hidpp_device *hidpp,
2283	u8 feature_index, struct hidpp_touchpad_raw_info *raw_info)
2284{
2285	struct hidpp_report response;
2286	int ret;
2287	u8 *params = (u8 *)response.fap.params;
2288
2289	ret = hidpp_send_fap_command_sync(hidpp, feature_index,
2290		CMD_TOUCHPAD_GET_RAW_INFO, NULL, 0, &response);
2291
2292	if (ret > 0) {
2293		hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
2294			__func__, ret);
2295		return -EPROTO;
2296	}
2297	if (ret)
2298		return ret;
2299
2300	raw_info->x_size = get_unaligned_be16(&params[0]);
2301	raw_info->y_size = get_unaligned_be16(&params[2]);
2302	raw_info->z_range = params[4];
2303	raw_info->area_range = params[5];
2304	raw_info->maxcontacts = params[7];
2305	raw_info->origin = params[8];
2306	/* res is given in unit per inch */
2307	raw_info->res = get_unaligned_be16(&params[13]) * 2 / 51;
2308
2309	return ret;
2310}
2311
2312static int hidpp_touchpad_set_raw_report_state(struct hidpp_device *hidpp_dev,
2313		u8 feature_index, bool send_raw_reports,
2314		bool sensor_enhanced_settings)
2315{
2316	struct hidpp_report response;
2317
2318	/*
2319	 * Params:
2320	 *   bit 0 - enable raw
2321	 *   bit 1 - 16bit Z, no area
2322	 *   bit 2 - enhanced sensitivity
2323	 *   bit 3 - width, height (4 bits each) instead of area
2324	 *   bit 4 - send raw + gestures (degrades smoothness)
2325	 *   remaining bits - reserved
2326	 */
2327	u8 params = send_raw_reports | (sensor_enhanced_settings << 2);
2328
2329	return hidpp_send_fap_command_sync(hidpp_dev, feature_index,
2330		CMD_TOUCHPAD_SET_RAW_REPORT_STATE, &params, 1, &response);
2331}
2332
2333static void hidpp_touchpad_touch_event(u8 *data,
2334	struct hidpp_touchpad_raw_xy_finger *finger)
2335{
2336	u8 x_m = data[0] << 2;
2337	u8 y_m = data[2] << 2;
2338
2339	finger->x = x_m << 6 | data[1];
2340	finger->y = y_m << 6 | data[3];
2341
2342	finger->contact_type = data[0] >> 6;
2343	finger->contact_status = data[2] >> 6;
2344
2345	finger->z = data[4];
2346	finger->area = data[5];
2347	finger->finger_id = data[6] >> 4;
2348}
2349
2350static void hidpp_touchpad_raw_xy_event(struct hidpp_device *hidpp_dev,
2351		u8 *data, struct hidpp_touchpad_raw_xy *raw_xy)
2352{
2353	memset(raw_xy, 0, sizeof(struct hidpp_touchpad_raw_xy));
2354	raw_xy->end_of_frame = data[8] & 0x01;
2355	raw_xy->spurious_flag = (data[8] >> 1) & 0x01;
2356	raw_xy->finger_count = data[15] & 0x0f;
2357	raw_xy->button = (data[8] >> 2) & 0x01;
2358
2359	if (raw_xy->finger_count) {
2360		hidpp_touchpad_touch_event(&data[2], &raw_xy->fingers[0]);
2361		hidpp_touchpad_touch_event(&data[9], &raw_xy->fingers[1]);
2362	}
2363}
2364
2365/* -------------------------------------------------------------------------- */
2366/* 0x8123: Force feedback support                                             */
2367/* -------------------------------------------------------------------------- */
2368
2369#define HIDPP_FF_GET_INFO		0x01
2370#define HIDPP_FF_RESET_ALL		0x11
2371#define HIDPP_FF_DOWNLOAD_EFFECT	0x21
2372#define HIDPP_FF_SET_EFFECT_STATE	0x31
2373#define HIDPP_FF_DESTROY_EFFECT		0x41
2374#define HIDPP_FF_GET_APERTURE		0x51
2375#define HIDPP_FF_SET_APERTURE		0x61
2376#define HIDPP_FF_GET_GLOBAL_GAINS	0x71
2377#define HIDPP_FF_SET_GLOBAL_GAINS	0x81
2378
2379#define HIDPP_FF_EFFECT_STATE_GET	0x00
2380#define HIDPP_FF_EFFECT_STATE_STOP	0x01
2381#define HIDPP_FF_EFFECT_STATE_PLAY	0x02
2382#define HIDPP_FF_EFFECT_STATE_PAUSE	0x03
2383
2384#define HIDPP_FF_EFFECT_CONSTANT	0x00
2385#define HIDPP_FF_EFFECT_PERIODIC_SINE		0x01
2386#define HIDPP_FF_EFFECT_PERIODIC_SQUARE		0x02
2387#define HIDPP_FF_EFFECT_PERIODIC_TRIANGLE	0x03
2388#define HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHUP	0x04
2389#define HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHDOWN	0x05
2390#define HIDPP_FF_EFFECT_SPRING		0x06
2391#define HIDPP_FF_EFFECT_DAMPER		0x07
2392#define HIDPP_FF_EFFECT_FRICTION	0x08
2393#define HIDPP_FF_EFFECT_INERTIA		0x09
2394#define HIDPP_FF_EFFECT_RAMP		0x0A
2395
2396#define HIDPP_FF_EFFECT_AUTOSTART	0x80
2397
2398#define HIDPP_FF_EFFECTID_NONE		-1
2399#define HIDPP_FF_EFFECTID_AUTOCENTER	-2
2400#define HIDPP_AUTOCENTER_PARAMS_LENGTH	18
2401
2402#define HIDPP_FF_MAX_PARAMS	20
2403#define HIDPP_FF_RESERVED_SLOTS	1
2404
2405struct hidpp_ff_private_data {
2406	struct hidpp_device *hidpp;
2407	u8 feature_index;
2408	u8 version;
2409	u16 gain;
2410	s16 range;
2411	u8 slot_autocenter;
2412	u8 num_effects;
2413	int *effect_ids;
2414	struct workqueue_struct *wq;
2415	atomic_t workqueue_size;
2416};
2417
2418struct hidpp_ff_work_data {
2419	struct work_struct work;
2420	struct hidpp_ff_private_data *data;
2421	int effect_id;
2422	u8 command;
2423	u8 params[HIDPP_FF_MAX_PARAMS];
2424	u8 size;
2425};
2426
2427static const signed short hidpp_ff_effects[] = {
2428	FF_CONSTANT,
2429	FF_PERIODIC,
2430	FF_SINE,
2431	FF_SQUARE,
2432	FF_SAW_UP,
2433	FF_SAW_DOWN,
2434	FF_TRIANGLE,
2435	FF_SPRING,
2436	FF_DAMPER,
2437	FF_AUTOCENTER,
2438	FF_GAIN,
2439	-1
2440};
2441
2442static const signed short hidpp_ff_effects_v2[] = {
2443	FF_RAMP,
2444	FF_FRICTION,
2445	FF_INERTIA,
2446	-1
2447};
2448
2449static const u8 HIDPP_FF_CONDITION_CMDS[] = {
2450	HIDPP_FF_EFFECT_SPRING,
2451	HIDPP_FF_EFFECT_FRICTION,
2452	HIDPP_FF_EFFECT_DAMPER,
2453	HIDPP_FF_EFFECT_INERTIA
2454};
2455
2456static const char *HIDPP_FF_CONDITION_NAMES[] = {
2457	"spring",
2458	"friction",
2459	"damper",
2460	"inertia"
2461};
2462
2463
2464static u8 hidpp_ff_find_effect(struct hidpp_ff_private_data *data, int effect_id)
2465{
2466	int i;
2467
2468	for (i = 0; i < data->num_effects; i++)
2469		if (data->effect_ids[i] == effect_id)
2470			return i+1;
2471
2472	return 0;
2473}
2474
2475static void hidpp_ff_work_handler(struct work_struct *w)
2476{
2477	struct hidpp_ff_work_data *wd = container_of(w, struct hidpp_ff_work_data, work);
2478	struct hidpp_ff_private_data *data = wd->data;
2479	struct hidpp_report response;
2480	u8 slot;
2481	int ret;
2482
2483	/* add slot number if needed */
2484	switch (wd->effect_id) {
2485	case HIDPP_FF_EFFECTID_AUTOCENTER:
2486		wd->params[0] = data->slot_autocenter;
2487		break;
2488	case HIDPP_FF_EFFECTID_NONE:
2489		/* leave slot as zero */
2490		break;
2491	default:
2492		/* find current slot for effect */
2493		wd->params[0] = hidpp_ff_find_effect(data, wd->effect_id);
2494		break;
2495	}
2496
2497	/* send command and wait for reply */
2498	ret = hidpp_send_fap_command_sync(data->hidpp, data->feature_index,
2499		wd->command, wd->params, wd->size, &response);
2500
2501	if (ret) {
2502		hid_err(data->hidpp->hid_dev, "Failed to send command to device!\n");
2503		goto out;
2504	}
2505
2506	/* parse return data */
2507	switch (wd->command) {
2508	case HIDPP_FF_DOWNLOAD_EFFECT:
2509		slot = response.fap.params[0];
2510		if (slot > 0 && slot <= data->num_effects) {
2511			if (wd->effect_id >= 0)
2512				/* regular effect uploaded */
2513				data->effect_ids[slot-1] = wd->effect_id;
2514			else if (wd->effect_id >= HIDPP_FF_EFFECTID_AUTOCENTER)
2515				/* autocenter spring uploaded */
2516				data->slot_autocenter = slot;
2517		}
2518		break;
2519	case HIDPP_FF_DESTROY_EFFECT:
2520		if (wd->effect_id >= 0)
2521			/* regular effect destroyed */
2522			data->effect_ids[wd->params[0]-1] = -1;
2523		else if (wd->effect_id >= HIDPP_FF_EFFECTID_AUTOCENTER)
2524			/* autocenter spring destoyed */
2525			data->slot_autocenter = 0;
2526		break;
2527	case HIDPP_FF_SET_GLOBAL_GAINS:
2528		data->gain = (wd->params[0] << 8) + wd->params[1];
2529		break;
2530	case HIDPP_FF_SET_APERTURE:
2531		data->range = (wd->params[0] << 8) + wd->params[1];
2532		break;
2533	default:
2534		/* no action needed */
2535		break;
2536	}
2537
2538out:
2539	atomic_dec(&data->workqueue_size);
2540	kfree(wd);
2541}
2542
2543static int hidpp_ff_queue_work(struct hidpp_ff_private_data *data, int effect_id, u8 command, u8 *params, u8 size)
2544{
2545	struct hidpp_ff_work_data *wd = kzalloc(sizeof(*wd), GFP_KERNEL);
2546	int s;
2547
2548	if (!wd)
2549		return -ENOMEM;
2550
2551	INIT_WORK(&wd->work, hidpp_ff_work_handler);
2552
2553	wd->data = data;
2554	wd->effect_id = effect_id;
2555	wd->command = command;
2556	wd->size = size;
2557	memcpy(wd->params, params, size);
2558
2559	s = atomic_inc_return(&data->workqueue_size);
2560	queue_work(data->wq, &wd->work);
2561
2562	/* warn about excessive queue size */
2563	if (s >= 20 && s % 20 == 0)
2564		hid_warn(data->hidpp->hid_dev, "Force feedback command queue contains %d commands, causing substantial delays!", s);
2565
2566	return 0;
2567}
2568
2569static int hidpp_ff_upload_effect(struct input_dev *dev, struct ff_effect *effect, struct ff_effect *old)
2570{
2571	struct hidpp_ff_private_data *data = dev->ff->private;
2572	u8 params[20];
2573	u8 size;
2574	int force;
2575
2576	/* set common parameters */
2577	params[2] = effect->replay.length >> 8;
2578	params[3] = effect->replay.length & 255;
2579	params[4] = effect->replay.delay >> 8;
2580	params[5] = effect->replay.delay & 255;
2581
2582	switch (effect->type) {
2583	case FF_CONSTANT:
2584		force = (effect->u.constant.level * fixp_sin16((effect->direction * 360) >> 16)) >> 15;
2585		params[1] = HIDPP_FF_EFFECT_CONSTANT;
2586		params[6] = force >> 8;
2587		params[7] = force & 255;
2588		params[8] = effect->u.constant.envelope.attack_level >> 7;
2589		params[9] = effect->u.constant.envelope.attack_length >> 8;
2590		params[10] = effect->u.constant.envelope.attack_length & 255;
2591		params[11] = effect->u.constant.envelope.fade_level >> 7;
2592		params[12] = effect->u.constant.envelope.fade_length >> 8;
2593		params[13] = effect->u.constant.envelope.fade_length & 255;
2594		size = 14;
2595		dbg_hid("Uploading constant force level=%d in dir %d = %d\n",
2596				effect->u.constant.level,
2597				effect->direction, force);
2598		dbg_hid("          envelope attack=(%d, %d ms) fade=(%d, %d ms)\n",
2599				effect->u.constant.envelope.attack_level,
2600				effect->u.constant.envelope.attack_length,
2601				effect->u.constant.envelope.fade_level,
2602				effect->u.constant.envelope.fade_length);
2603		break;
2604	case FF_PERIODIC:
2605	{
2606		switch (effect->u.periodic.waveform) {
2607		case FF_SINE:
2608			params[1] = HIDPP_FF_EFFECT_PERIODIC_SINE;
2609			break;
2610		case FF_SQUARE:
2611			params[1] = HIDPP_FF_EFFECT_PERIODIC_SQUARE;
2612			break;
2613		case FF_SAW_UP:
2614			params[1] = HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHUP;
2615			break;
2616		case FF_SAW_DOWN:
2617			params[1] = HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHDOWN;
2618			break;
2619		case FF_TRIANGLE:
2620			params[1] = HIDPP_FF_EFFECT_PERIODIC_TRIANGLE;
2621			break;
2622		default:
2623			hid_err(data->hidpp->hid_dev, "Unexpected periodic waveform type %i!\n", effect->u.periodic.waveform);
2624			return -EINVAL;
2625		}
2626		force = (effect->u.periodic.magnitude * fixp_sin16((effect->direction * 360) >> 16)) >> 15;
2627		params[6] = effect->u.periodic.magnitude >> 8;
2628		params[7] = effect->u.periodic.magnitude & 255;
2629		params[8] = effect->u.periodic.offset >> 8;
2630		params[9] = effect->u.periodic.offset & 255;
2631		params[10] = effect->u.periodic.period >> 8;
2632		params[11] = effect->u.periodic.period & 255;
2633		params[12] = effect->u.periodic.phase >> 8;
2634		params[13] = effect->u.periodic.phase & 255;
2635		params[14] = effect->u.periodic.envelope.attack_level >> 7;
2636		params[15] = effect->u.periodic.envelope.attack_length >> 8;
2637		params[16] = effect->u.periodic.envelope.attack_length & 255;
2638		params[17] = effect->u.periodic.envelope.fade_level >> 7;
2639		params[18] = effect->u.periodic.envelope.fade_length >> 8;
2640		params[19] = effect->u.periodic.envelope.fade_length & 255;
2641		size = 20;
2642		dbg_hid("Uploading periodic force mag=%d/dir=%d, offset=%d, period=%d ms, phase=%d\n",
2643				effect->u.periodic.magnitude, effect->direction,
2644				effect->u.periodic.offset,
2645				effect->u.periodic.period,
2646				effect->u.periodic.phase);
2647		dbg_hid("          envelope attack=(%d, %d ms) fade=(%d, %d ms)\n",
2648				effect->u.periodic.envelope.attack_level,
2649				effect->u.periodic.envelope.attack_length,
2650				effect->u.periodic.envelope.fade_level,
2651				effect->u.periodic.envelope.fade_length);
2652		break;
2653	}
2654	case FF_RAMP:
2655		params[1] = HIDPP_FF_EFFECT_RAMP;
2656		force = (effect->u.ramp.start_level * fixp_sin16((effect->direction * 360) >> 16)) >> 15;
2657		params[6] = force >> 8;
2658		params[7] = force & 255;
2659		force = (effect->u.ramp.end_level * fixp_sin16((effect->direction * 360) >> 16)) >> 15;
2660		params[8] = force >> 8;
2661		params[9] = force & 255;
2662		params[10] = effect->u.ramp.envelope.attack_level >> 7;
2663		params[11] = effect->u.ramp.envelope.attack_length >> 8;
2664		params[12] = effect->u.ramp.envelope.attack_length & 255;
2665		params[13] = effect->u.ramp.envelope.fade_level >> 7;
2666		params[14] = effect->u.ramp.envelope.fade_length >> 8;
2667		params[15] = effect->u.ramp.envelope.fade_length & 255;
2668		size = 16;
2669		dbg_hid("Uploading ramp force level=%d -> %d in dir %d = %d\n",
2670				effect->u.ramp.start_level,
2671				effect->u.ramp.end_level,
2672				effect->direction, force);
2673		dbg_hid("          envelope attack=(%d, %d ms) fade=(%d, %d ms)\n",
2674				effect->u.ramp.envelope.attack_level,
2675				effect->u.ramp.envelope.attack_length,
2676				effect->u.ramp.envelope.fade_level,
2677				effect->u.ramp.envelope.fade_length);
2678		break;
2679	case FF_FRICTION:
2680	case FF_INERTIA:
2681	case FF_SPRING:
2682	case FF_DAMPER:
2683		params[1] = HIDPP_FF_CONDITION_CMDS[effect->type - FF_SPRING];
2684		params[6] = effect->u.condition[0].left_saturation >> 9;
2685		params[7] = (effect->u.condition[0].left_saturation >> 1) & 255;
2686		params[8] = effect->u.condition[0].left_coeff >> 8;
2687		params[9] = effect->u.condition[0].left_coeff & 255;
2688		params[10] = effect->u.condition[0].deadband >> 9;
2689		params[11] = (effect->u.condition[0].deadband >> 1) & 255;
2690		params[12] = effect->u.condition[0].center >> 8;
2691		params[13] = effect->u.condition[0].center & 255;
2692		params[14] = effect->u.condition[0].right_coeff >> 8;
2693		params[15] = effect->u.condition[0].right_coeff & 255;
2694		params[16] = effect->u.condition[0].right_saturation >> 9;
2695		params[17] = (effect->u.condition[0].right_saturation >> 1) & 255;
2696		size = 18;
2697		dbg_hid("Uploading %s force left coeff=%d, left sat=%d, right coeff=%d, right sat=%d\n",
2698				HIDPP_FF_CONDITION_NAMES[effect->type - FF_SPRING],
2699				effect->u.condition[0].left_coeff,
2700				effect->u.condition[0].left_saturation,
2701				effect->u.condition[0].right_coeff,
2702				effect->u.condition[0].right_saturation);
2703		dbg_hid("          deadband=%d, center=%d\n",
2704				effect->u.condition[0].deadband,
2705				effect->u.condition[0].center);
2706		break;
2707	default:
2708		hid_err(data->hidpp->hid_dev, "Unexpected force type %i!\n", effect->type);
2709		return -EINVAL;
2710	}
2711
2712	return hidpp_ff_queue_work(data, effect->id, HIDPP_FF_DOWNLOAD_EFFECT, params, size);
2713}
2714
2715static int hidpp_ff_playback(struct input_dev *dev, int effect_id, int value)
2716{
2717	struct hidpp_ff_private_data *data = dev->ff->private;
2718	u8 params[2];
2719
2720	params[1] = value ? HIDPP_FF_EFFECT_STATE_PLAY : HIDPP_FF_EFFECT_STATE_STOP;
2721
2722	dbg_hid("St%sing playback of effect %d.\n", value?"art":"opp", effect_id);
2723
2724	return hidpp_ff_queue_work(data, effect_id, HIDPP_FF_SET_EFFECT_STATE, params, ARRAY_SIZE(params));
2725}
2726
2727static int hidpp_ff_erase_effect(struct input_dev *dev, int effect_id)
2728{
2729	struct hidpp_ff_private_data *data = dev->ff->private;
2730	u8 slot = 0;
2731
2732	dbg_hid("Erasing effect %d.\n", effect_id);
2733
2734	return hidpp_ff_queue_work(data, effect_id, HIDPP_FF_DESTROY_EFFECT, &slot, 1);
2735}
2736
2737static void hidpp_ff_set_autocenter(struct input_dev *dev, u16 magnitude)
2738{
2739	struct hidpp_ff_private_data *data = dev->ff->private;
2740	u8 params[HIDPP_AUTOCENTER_PARAMS_LENGTH];
2741
2742	dbg_hid("Setting autocenter to %d.\n", magnitude);
2743
2744	/* start a standard spring effect */
2745	params[1] = HIDPP_FF_EFFECT_SPRING | HIDPP_FF_EFFECT_AUTOSTART;
2746	/* zero delay and duration */
2747	params[2] = params[3] = params[4] = params[5] = 0;
2748	/* set coeff to 25% of saturation */
2749	params[8] = params[14] = magnitude >> 11;
2750	params[9] = params[15] = (magnitude >> 3) & 255;
2751	params[6] = params[16] = magnitude >> 9;
2752	params[7] = params[17] = (magnitude >> 1) & 255;
2753	/* zero deadband and center */
2754	params[10] = params[11] = params[12] = params[13] = 0;
2755
2756	hidpp_ff_queue_work(data, HIDPP_FF_EFFECTID_AUTOCENTER, HIDPP_FF_DOWNLOAD_EFFECT, params, ARRAY_SIZE(params));
2757}
2758
2759static void hidpp_ff_set_gain(struct input_dev *dev, u16 gain)
2760{
2761	struct hidpp_ff_private_data *data = dev->ff->private;
2762	u8 params[4];
2763
2764	dbg_hid("Setting gain to %d.\n", gain);
2765
2766	params[0] = gain >> 8;
2767	params[1] = gain & 255;
2768	params[2] = 0; /* no boost */
2769	params[3] = 0;
2770
2771	hidpp_ff_queue_work(data, HIDPP_FF_EFFECTID_NONE, HIDPP_FF_SET_GLOBAL_GAINS, params, ARRAY_SIZE(params));
2772}
2773
2774static ssize_t hidpp_ff_range_show(struct device *dev, struct device_attribute *attr, char *buf)
2775{
2776	struct hid_device *hid = to_hid_device(dev);
2777	struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
2778	struct input_dev *idev = hidinput->input;
2779	struct hidpp_ff_private_data *data = idev->ff->private;
2780
2781	return scnprintf(buf, PAGE_SIZE, "%u\n", data->range);
2782}
2783
2784static ssize_t hidpp_ff_range_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
2785{
2786	struct hid_device *hid = to_hid_device(dev);
2787	struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
2788	struct input_dev *idev = hidinput->input;
2789	struct hidpp_ff_private_data *data = idev->ff->private;
2790	u8 params[2];
2791	int range = simple_strtoul(buf, NULL, 10);
2792
2793	range = clamp(range, 180, 900);
2794
2795	params[0] = range >> 8;
2796	params[1] = range & 0x00FF;
2797
2798	hidpp_ff_queue_work(data, -1, HIDPP_FF_SET_APERTURE, params, ARRAY_SIZE(params));
2799
2800	return count;
2801}
2802
2803static DEVICE_ATTR(range, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH, hidpp_ff_range_show, hidpp_ff_range_store);
2804
2805static void hidpp_ff_destroy(struct ff_device *ff)
2806{
2807	struct hidpp_ff_private_data *data = ff->private;
2808	struct hid_device *hid = data->hidpp->hid_dev;
2809
2810	hid_info(hid, "Unloading HID++ force feedback.\n");
2811
2812	device_remove_file(&hid->dev, &dev_attr_range);
2813	destroy_workqueue(data->wq);
2814	kfree(data->effect_ids);
2815}
2816
2817static int hidpp_ff_init(struct hidpp_device *hidpp,
2818			 struct hidpp_ff_private_data *data)
2819{
2820	struct hid_device *hid = hidpp->hid_dev;
2821	struct hid_input *hidinput;
2822	struct input_dev *dev;
2823	struct usb_device_descriptor *udesc;
2824	u16 bcdDevice;
2825	struct ff_device *ff;
2826	int error, j, num_slots = data->num_effects;
2827	u8 version;
2828
2829	if (!hid_is_usb(hid)) {
2830		hid_err(hid, "device is not USB\n");
2831		return -ENODEV;
2832	}
2833
2834	if (list_empty(&hid->inputs)) {
2835		hid_err(hid, "no inputs found\n");
2836		return -ENODEV;
2837	}
2838	hidinput = list_entry(hid->inputs.next, struct hid_input, list);
2839	dev = hidinput->input;
2840
2841	if (!dev) {
2842		hid_err(hid, "Struct input_dev not set!\n");
2843		return -EINVAL;
2844	}
2845
2846	/* Get firmware release */
2847	udesc = &(hid_to_usb_dev(hid)->descriptor);
2848	bcdDevice = le16_to_cpu(udesc->bcdDevice);
2849	version = bcdDevice & 255;
2850
2851	/* Set supported force feedback capabilities */
2852	for (j = 0; hidpp_ff_effects[j] >= 0; j++)
2853		set_bit(hidpp_ff_effects[j], dev->ffbit);
2854	if (version > 1)
2855		for (j = 0; hidpp_ff_effects_v2[j] >= 0; j++)
2856			set_bit(hidpp_ff_effects_v2[j], dev->ffbit);
2857
2858	error = input_ff_create(dev, num_slots);
2859
2860	if (error) {
2861		hid_err(dev, "Failed to create FF device!\n");
2862		return error;
2863	}
2864	/*
2865	 * Create a copy of passed data, so we can transfer memory
2866	 * ownership to FF core
2867	 */
2868	data = kmemdup(data, sizeof(*data), GFP_KERNEL);
2869	if (!data)
2870		return -ENOMEM;
2871	data->effect_ids = kcalloc(num_slots, sizeof(int), GFP_KERNEL);
2872	if (!data->effect_ids) {
2873		kfree(data);
2874		return -ENOMEM;
2875	}
2876	data->wq = create_singlethread_workqueue("hidpp-ff-sendqueue");
2877	if (!data->wq) {
2878		kfree(data->effect_ids);
2879		kfree(data);
2880		return -ENOMEM;
2881	}
2882
2883	data->hidpp = hidpp;
2884	data->version = version;
2885	for (j = 0; j < num_slots; j++)
2886		data->effect_ids[j] = -1;
2887
2888	ff = dev->ff;
2889	ff->private = data;
2890
2891	ff->upload = hidpp_ff_upload_effect;
2892	ff->erase = hidpp_ff_erase_effect;
2893	ff->playback = hidpp_ff_playback;
2894	ff->set_gain = hidpp_ff_set_gain;
2895	ff->set_autocenter = hidpp_ff_set_autocenter;
2896	ff->destroy = hidpp_ff_destroy;
2897
2898	/* Create sysfs interface */
2899	error = device_create_file(&(hidpp->hid_dev->dev), &dev_attr_range);
2900	if (error)
2901		hid_warn(hidpp->hid_dev, "Unable to create sysfs interface for \"range\", errno %d!\n", error);
2902
2903	/* init the hardware command queue */
2904	atomic_set(&data->workqueue_size, 0);
2905
2906	hid_info(hid, "Force feedback support loaded (firmware release %d).\n",
2907		 version);
2908
2909	return 0;
2910}
2911
2912/* ************************************************************************** */
2913/*                                                                            */
2914/* Device Support                                                             */
2915/*                                                                            */
2916/* ************************************************************************** */
2917
2918/* -------------------------------------------------------------------------- */
2919/* Touchpad HID++ devices                                                     */
2920/* -------------------------------------------------------------------------- */
2921
2922#define WTP_MANUAL_RESOLUTION				39
2923
2924struct wtp_data {
2925	u16 x_size, y_size;
2926	u8 finger_count;
2927	u8 mt_feature_index;
2928	u8 button_feature_index;
2929	u8 maxcontacts;
2930	bool flip_y;
2931	unsigned int resolution;
2932};
2933
2934static int wtp_input_mapping(struct hid_device *hdev, struct hid_input *hi,
2935		struct hid_field *field, struct hid_usage *usage,
2936		unsigned long **bit, int *max)
2937{
2938	return -1;
2939}
2940
2941static void wtp_populate_input(struct hidpp_device *hidpp,
2942			       struct input_dev *input_dev)
2943{
2944	struct wtp_data *wd = hidpp->private_data;
2945
2946	__set_bit(EV_ABS, input_dev->evbit);
2947	__set_bit(EV_KEY, input_dev->evbit);
2948	__clear_bit(EV_REL, input_dev->evbit);
2949	__clear_bit(EV_LED, input_dev->evbit);
2950
2951	input_set_abs_params(input_dev, ABS_MT_POSITION_X, 0, wd->x_size, 0, 0);
2952	input_abs_set_res(input_dev, ABS_MT_POSITION_X, wd->resolution);
2953	input_set_abs_params(input_dev, ABS_MT_POSITION_Y, 0, wd->y_size, 0, 0);
2954	input_abs_set_res(input_dev, ABS_MT_POSITION_Y, wd->resolution);
2955
2956	/* Max pressure is not given by the devices, pick one */
2957	input_set_abs_params(input_dev, ABS_MT_PRESSURE, 0, 50, 0, 0);
2958
2959	input_set_capability(input_dev, EV_KEY, BTN_LEFT);
2960
2961	if (hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS)
2962		input_set_capability(input_dev, EV_KEY, BTN_RIGHT);
2963	else
2964		__set_bit(INPUT_PROP_BUTTONPAD, input_dev->propbit);
2965
2966	input_mt_init_slots(input_dev, wd->maxcontacts, INPUT_MT_POINTER |
2967		INPUT_MT_DROP_UNUSED);
2968}
2969
2970static void wtp_touch_event(struct hidpp_device *hidpp,
2971	struct hidpp_touchpad_raw_xy_finger *touch_report)
2972{
2973	struct wtp_data *wd = hidpp->private_data;
2974	int slot;
2975
2976	if (!touch_report->finger_id || touch_report->contact_type)
2977		/* no actual data */
2978		return;
2979
2980	slot = input_mt_get_slot_by_key(hidpp->input, touch_report->finger_id);
2981
2982	input_mt_slot(hidpp->input, slot);
2983	input_mt_report_slot_state(hidpp->input, MT_TOOL_FINGER,
2984					touch_report->contact_status);
2985	if (touch_report->contact_status) {
2986		input_event(hidpp->input, EV_ABS, ABS_MT_POSITION_X,
2987				touch_report->x);
2988		input_event(hidpp->input, EV_ABS, ABS_MT_POSITION_Y,
2989				wd->flip_y ? wd->y_size - touch_report->y :
2990					     touch_report->y);
2991		input_event(hidpp->input, EV_ABS, ABS_MT_PRESSURE,
2992				touch_report->area);
2993	}
2994}
2995
2996static void wtp_send_raw_xy_event(struct hidpp_device *hidpp,
2997		struct hidpp_touchpad_raw_xy *raw)
2998{
2999	int i;
3000
3001	for (i = 0; i < 2; i++)
3002		wtp_touch_event(hidpp, &(raw->fingers[i]));
3003
3004	if (raw->end_of_frame &&
3005	    !(hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS))
3006		input_event(hidpp->input, EV_KEY, BTN_LEFT, raw->button);
3007
3008	if (raw->end_of_frame || raw->finger_count <= 2) {
3009		input_mt_sync_frame(hidpp->input);
3010		input_sync(hidpp->input);
3011	}
3012}
3013
3014static int wtp_mouse_raw_xy_event(struct hidpp_device *hidpp, u8 *data)
3015{
3016	struct wtp_data *wd = hidpp->private_data;
3017	u8 c1_area = ((data[7] & 0xf) * (data[7] & 0xf) +
3018		      (data[7] >> 4) * (data[7] >> 4)) / 2;
3019	u8 c2_area = ((data[13] & 0xf) * (data[13] & 0xf) +
3020		      (data[13] >> 4) * (data[13] >> 4)) / 2;
3021	struct hidpp_touchpad_raw_xy raw = {
3022		.timestamp = data[1],
3023		.fingers = {
3024			{
3025				.contact_type = 0,
3026				.contact_status = !!data[7],
3027				.x = get_unaligned_le16(&data[3]),
3028				.y = get_unaligned_le16(&data[5]),
3029				.z = c1_area,
3030				.area = c1_area,
3031				.finger_id = data[2],
3032			}, {
3033				.contact_type = 0,
3034				.contact_status = !!data[13],
3035				.x = get_unaligned_le16(&data[9]),
3036				.y = get_unaligned_le16(&data[11]),
3037				.z = c2_area,
3038				.area = c2_area,
3039				.finger_id = data[8],
3040			}
3041		},
3042		.finger_count = wd->maxcontacts,
3043		.spurious_flag = 0,
3044		.end_of_frame = (data[0] >> 7) == 0,
3045		.button = data[0] & 0x01,
3046	};
3047
3048	wtp_send_raw_xy_event(hidpp, &raw);
3049
3050	return 1;
3051}
3052
3053static int wtp_raw_event(struct hid_device *hdev, u8 *data, int size)
3054{
3055	struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3056	struct wtp_data *wd = hidpp->private_data;
3057	struct hidpp_report *report = (struct hidpp_report *)data;
3058	struct hidpp_touchpad_raw_xy raw;
3059
3060	if (!wd || !hidpp->input)
3061		return 1;
3062
3063	switch (data[0]) {
3064	case 0x02:
3065		if (size < 2) {
3066			hid_err(hdev, "Received HID report of bad size (%d)",
3067				size);
3068			return 1;
3069		}
3070		if (hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS) {
3071			input_event(hidpp->input, EV_KEY, BTN_LEFT,
3072					!!(data[1] & 0x01));
3073			input_event(hidpp->input, EV_KEY, BTN_RIGHT,
3074					!!(data[1] & 0x02));
3075			input_sync(hidpp->input);
3076			return 0;
3077		} else {
3078			if (size < 21)
3079				return 1;
3080			return wtp_mouse_raw_xy_event(hidpp, &data[7]);
3081		}
3082	case REPORT_ID_HIDPP_LONG:
3083		/* size is already checked in hidpp_raw_event. */
3084		if ((report->fap.feature_index != wd->mt_feature_index) ||
3085		    (report->fap.funcindex_clientid != EVENT_TOUCHPAD_RAW_XY))
3086			return 1;
3087		hidpp_touchpad_raw_xy_event(hidpp, data + 4, &raw);
3088
3089		wtp_send_raw_xy_event(hidpp, &raw);
3090		return 0;
3091	}
3092
3093	return 0;
3094}
3095
3096static int wtp_get_config(struct hidpp_device *hidpp)
3097{
3098	struct wtp_data *wd = hidpp->private_data;
3099	struct hidpp_touchpad_raw_info raw_info = {0};
3100	u8 feature_type;
3101	int ret;
3102
3103	ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_TOUCHPAD_RAW_XY,
3104		&wd->mt_feature_index, &feature_type);
3105	if (ret)
3106		/* means that the device is not powered up */
3107		return ret;
3108
3109	ret = hidpp_touchpad_get_raw_info(hidpp, wd->mt_feature_index,
3110		&raw_info);
3111	if (ret)
3112		return ret;
3113
3114	wd->x_size = raw_info.x_size;
3115	wd->y_size = raw_info.y_size;
3116	wd->maxcontacts = raw_info.maxcontacts;
3117	wd->flip_y = raw_info.origin == TOUCHPAD_RAW_XY_ORIGIN_LOWER_LEFT;
3118	wd->resolution = raw_info.res;
3119	if (!wd->resolution)
3120		wd->resolution = WTP_MANUAL_RESOLUTION;
3121
3122	return 0;
3123}
3124
3125static int wtp_allocate(struct hid_device *hdev, const struct hid_device_id *id)
3126{
3127	struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3128	struct wtp_data *wd;
3129
3130	wd = devm_kzalloc(&hdev->dev, sizeof(struct wtp_data),
3131			GFP_KERNEL);
3132	if (!wd)
3133		return -ENOMEM;
3134
3135	hidpp->private_data = wd;
3136
3137	return 0;
3138};
3139
3140static int wtp_connect(struct hid_device *hdev)
3141{
3142	struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3143	struct wtp_data *wd = hidpp->private_data;
3144	int ret;
3145
3146	if (!wd->x_size) {
3147		ret = wtp_get_config(hidpp);
3148		if (ret) {
3149			hid_err(hdev, "Can not get wtp config: %d\n", ret);
3150			return ret;
3151		}
3152	}
3153
3154	return hidpp_touchpad_set_raw_report_state(hidpp, wd->mt_feature_index,
3155			true, true);
3156}
3157
3158/* ------------------------------------------------------------------------- */
3159/* Logitech M560 devices                                                     */
3160/* ------------------------------------------------------------------------- */
3161
3162/*
3163 * Logitech M560 protocol overview
3164 *
3165 * The Logitech M560 mouse, is designed for windows 8. When the middle and/or
3166 * the sides buttons are pressed, it sends some keyboard keys events
3167 * instead of buttons ones.
3168 * To complicate things further, the middle button keys sequence
3169 * is different from the odd press and the even press.
3170 *
3171 * forward button -> Super_R
3172 * backward button -> Super_L+'d' (press only)
3173 * middle button -> 1st time: Alt_L+SuperL+XF86TouchpadOff (press only)
3174 *                  2nd time: left-click (press only)
3175 * NB: press-only means that when the button is pressed, the
3176 * KeyPress/ButtonPress and KeyRelease/ButtonRelease events are generated
3177 * together sequentially; instead when the button is released, no event is
3178 * generated !
3179 *
3180 * With the command
3181 *	10<xx>0a 3500af03 (where <xx> is the mouse id),
3182 * the mouse reacts differently:
3183 * - it never sends a keyboard key event
3184 * - for the three mouse button it sends:
3185 *	middle button               press   11<xx>0a 3500af00...
3186 *	side 1 button (forward)     press   11<xx>0a 3500b000...
3187 *	side 2 button (backward)    press   11<xx>0a 3500ae00...
3188 *	middle/side1/side2 button   release 11<xx>0a 35000000...
3189 */
3190
3191static const u8 m560_config_parameter[] = {0x00, 0xaf, 0x03};
3192
3193/* how buttons are mapped in the report */
3194#define M560_MOUSE_BTN_LEFT		0x01
3195#define M560_MOUSE_BTN_RIGHT		0x02
3196#define M560_MOUSE_BTN_WHEEL_LEFT	0x08
3197#define M560_MOUSE_BTN_WHEEL_RIGHT	0x10
3198
3199#define M560_SUB_ID			0x0a
3200#define M560_BUTTON_MODE_REGISTER	0x35
3201
3202static int m560_send_config_command(struct hid_device *hdev)
3203{
3204	struct hidpp_report response;
3205	struct hidpp_device *hidpp_dev;
3206
3207	hidpp_dev = hid_get_drvdata(hdev);
3208
3209	return hidpp_send_rap_command_sync(
3210		hidpp_dev,
3211		REPORT_ID_HIDPP_SHORT,
3212		M560_SUB_ID,
3213		M560_BUTTON_MODE_REGISTER,
3214		(u8 *)m560_config_parameter,
3215		sizeof(m560_config_parameter),
3216		&response
3217	);
3218}
3219
3220static int m560_raw_event(struct hid_device *hdev, u8 *data, int size)
3221{
3222	struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3223
3224	/* sanity check */
3225	if (!hidpp->input) {
3226		hid_err(hdev, "error in parameter\n");
3227		return -EINVAL;
3228	}
3229
3230	if (size < 7) {
3231		hid_err(hdev, "error in report\n");
3232		return 0;
3233	}
3234
3235	if (data[0] == REPORT_ID_HIDPP_LONG &&
3236	    data[2] == M560_SUB_ID && data[6] == 0x00) {
3237		/*
3238		 * m560 mouse report for middle, forward and backward button
3239		 *
3240		 * data[0] = 0x11
3241		 * data[1] = device-id
3242		 * data[2] = 0x0a
3243		 * data[5] = 0xaf -> middle
3244		 *	     0xb0 -> forward
3245		 *	     0xae -> backward
3246		 *	     0x00 -> release all
3247		 * data[6] = 0x00
3248		 */
3249
3250		switch (data[5]) {
3251		case 0xaf:
3252			input_report_key(hidpp->input, BTN_MIDDLE, 1);
3253			break;
3254		case 0xb0:
3255			input_report_key(hidpp->input, BTN_FORWARD, 1);
3256			break;
3257		case 0xae:
3258			input_report_key(hidpp->input, BTN_BACK, 1);
3259			break;
3260		case 0x00:
3261			input_report_key(hidpp->input, BTN_BACK, 0);
3262			input_report_key(hidpp->input, BTN_FORWARD, 0);
3263			input_report_key(hidpp->input, BTN_MIDDLE, 0);
3264			break;
3265		default:
3266			hid_err(hdev, "error in report\n");
3267			return 0;
3268		}
3269		input_sync(hidpp->input);
3270
3271	} else if (data[0] == 0x02) {
3272		/*
3273		 * Logitech M560 mouse report
3274		 *
3275		 * data[0] = type (0x02)
3276		 * data[1..2] = buttons
3277		 * data[3..5] = xy
3278		 * data[6] = wheel
3279		 */
3280
3281		int v;
3282
3283		input_report_key(hidpp->input, BTN_LEFT,
3284			!!(data[1] & M560_MOUSE_BTN_LEFT));
3285		input_report_key(hidpp->input, BTN_RIGHT,
3286			!!(data[1] & M560_MOUSE_BTN_RIGHT));
3287
3288		if (data[1] & M560_MOUSE_BTN_WHEEL_LEFT) {
3289			input_report_rel(hidpp->input, REL_HWHEEL, -1);
3290			input_report_rel(hidpp->input, REL_HWHEEL_HI_RES,
3291					 -120);
3292		} else if (data[1] & M560_MOUSE_BTN_WHEEL_RIGHT) {
3293			input_report_rel(hidpp->input, REL_HWHEEL, 1);
3294			input_report_rel(hidpp->input, REL_HWHEEL_HI_RES,
3295					 120);
3296		}
3297
3298		v = hid_snto32(hid_field_extract(hdev, data+3, 0, 12), 12);
3299		input_report_rel(hidpp->input, REL_X, v);
3300
3301		v = hid_snto32(hid_field_extract(hdev, data+3, 12, 12), 12);
3302		input_report_rel(hidpp->input, REL_Y, v);
3303
3304		v = hid_snto32(data[6], 8);
3305		if (v != 0)
3306			hidpp_scroll_counter_handle_scroll(hidpp->input,
3307					&hidpp->vertical_wheel_counter, v);
3308
3309		input_sync(hidpp->input);
3310	}
3311
3312	return 1;
3313}
3314
3315static void m560_populate_input(struct hidpp_device *hidpp,
3316				struct input_dev *input_dev)
3317{
3318	__set_bit(EV_KEY, input_dev->evbit);
3319	__set_bit(BTN_MIDDLE, input_dev->keybit);
3320	__set_bit(BTN_RIGHT, input_dev->keybit);
3321	__set_bit(BTN_LEFT, input_dev->keybit);
3322	__set_bit(BTN_BACK, input_dev->keybit);
3323	__set_bit(BTN_FORWARD, input_dev->keybit);
3324
3325	__set_bit(EV_REL, input_dev->evbit);
3326	__set_bit(REL_X, input_dev->relbit);
3327	__set_bit(REL_Y, input_dev->relbit);
3328	__set_bit(REL_WHEEL, input_dev->relbit);
3329	__set_bit(REL_HWHEEL, input_dev->relbit);
3330	__set_bit(REL_WHEEL_HI_RES, input_dev->relbit);
3331	__set_bit(REL_HWHEEL_HI_RES, input_dev->relbit);
3332}
3333
3334static int m560_input_mapping(struct hid_device *hdev, struct hid_input *hi,
3335		struct hid_field *field, struct hid_usage *usage,
3336		unsigned long **bit, int *max)
3337{
3338	return -1;
3339}
3340
3341/* ------------------------------------------------------------------------- */
3342/* Logitech K400 devices                                                     */
3343/* ------------------------------------------------------------------------- */
3344
3345/*
3346 * The Logitech K400 keyboard has an embedded touchpad which is seen
3347 * as a mouse from the OS point of view. There is a hardware shortcut to disable
3348 * tap-to-click but the setting is not remembered accross reset, annoying some
3349 * users.
3350 *
3351 * We can toggle this feature from the host by using the feature 0x6010:
3352 * Touchpad FW items
3353 */
3354
3355struct k400_private_data {
3356	u8 feature_index;
3357};
3358
3359static int k400_disable_tap_to_click(struct hidpp_device *hidpp)
3360{
3361	struct k400_private_data *k400 = hidpp->private_data;
3362	struct hidpp_touchpad_fw_items items = {};
3363	int ret;
3364	u8 feature_type;
3365
3366	if (!k400->feature_index) {
3367		ret = hidpp_root_get_feature(hidpp,
3368			HIDPP_PAGE_TOUCHPAD_FW_ITEMS,
3369			&k400->feature_index, &feature_type);
3370		if (ret)
3371			/* means that the device is not powered up */
3372			return ret;
3373	}
3374
3375	ret = hidpp_touchpad_fw_items_set(hidpp, k400->feature_index, &items);
3376	if (ret)
3377		return ret;
3378
3379	return 0;
3380}
3381
3382static int k400_allocate(struct hid_device *hdev)
3383{
3384	struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3385	struct k400_private_data *k400;
3386
3387	k400 = devm_kzalloc(&hdev->dev, sizeof(struct k400_private_data),
3388			    GFP_KERNEL);
3389	if (!k400)
3390		return -ENOMEM;
3391
3392	hidpp->private_data = k400;
3393
3394	return 0;
3395};
3396
3397static int k400_connect(struct hid_device *hdev)
3398{
3399	struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3400
3401	if (!disable_tap_to_click)
3402		return 0;
3403
3404	return k400_disable_tap_to_click(hidpp);
3405}
3406
3407/* ------------------------------------------------------------------------- */
3408/* Logitech G920 Driving Force Racing Wheel for Xbox One                     */
3409/* ------------------------------------------------------------------------- */
3410
3411#define HIDPP_PAGE_G920_FORCE_FEEDBACK			0x8123
3412
3413static int g920_ff_set_autocenter(struct hidpp_device *hidpp,
3414				  struct hidpp_ff_private_data *data)
3415{
3416	struct hidpp_report response;
3417	u8 params[HIDPP_AUTOCENTER_PARAMS_LENGTH] = {
3418		[1] = HIDPP_FF_EFFECT_SPRING | HIDPP_FF_EFFECT_AUTOSTART,
3419	};
3420	int ret;
3421
3422	/* initialize with zero autocenter to get wheel in usable state */
3423
3424	dbg_hid("Setting autocenter to 0.\n");
3425	ret = hidpp_send_fap_command_sync(hidpp, data->feature_index,
3426					  HIDPP_FF_DOWNLOAD_EFFECT,
3427					  params, ARRAY_SIZE(params),
3428					  &response);
3429	if (ret)
3430		hid_warn(hidpp->hid_dev, "Failed to autocenter device!\n");
3431	else
3432		data->slot_autocenter = response.fap.params[0];
3433
3434	return ret;
3435}
3436
3437static int g920_get_config(struct hidpp_device *hidpp,
3438			   struct hidpp_ff_private_data *data)
3439{
3440	struct hidpp_report response;
3441	u8 feature_type;
3442	int ret;
3443
3444	memset(data, 0, sizeof(*data));
3445
3446	/* Find feature and store for later use */
3447	ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_G920_FORCE_FEEDBACK,
3448				     &data->feature_index, &feature_type);
3449	if (ret)
3450		return ret;
3451
3452	/* Read number of slots available in device */
3453	ret = hidpp_send_fap_command_sync(hidpp, data->feature_index,
3454					  HIDPP_FF_GET_INFO,
3455					  NULL, 0,
3456					  &response);
3457	if (ret) {
3458		if (ret < 0)
3459			return ret;
3460		hid_err(hidpp->hid_dev,
3461			"%s: received protocol error 0x%02x\n", __func__, ret);
3462		return -EPROTO;
3463	}
3464
3465	data->num_effects = response.fap.params[0] - HIDPP_FF_RESERVED_SLOTS;
3466
3467	/* reset all forces */
3468	ret = hidpp_send_fap_command_sync(hidpp, data->feature_index,
3469					  HIDPP_FF_RESET_ALL,
3470					  NULL, 0,
3471					  &response);
3472	if (ret)
3473		hid_warn(hidpp->hid_dev, "Failed to reset all forces!\n");
3474
3475	ret = hidpp_send_fap_command_sync(hidpp, data->feature_index,
3476					  HIDPP_FF_GET_APERTURE,
3477					  NULL, 0,
3478					  &response);
3479	if (ret) {
3480		hid_warn(hidpp->hid_dev,
3481			 "Failed to read range from device!\n");
3482	}
3483	data->range = ret ?
3484		900 : get_unaligned_be16(&response.fap.params[0]);
3485
3486	/* Read the current gain values */
3487	ret = hidpp_send_fap_command_sync(hidpp, data->feature_index,
3488					  HIDPP_FF_GET_GLOBAL_GAINS,
3489					  NULL, 0,
3490					  &response);
3491	if (ret)
3492		hid_warn(hidpp->hid_dev,
3493			 "Failed to read gain values from device!\n");
3494	data->gain = ret ?
3495		0xffff : get_unaligned_be16(&response.fap.params[0]);
3496
3497	/* ignore boost value at response.fap.params[2] */
3498
3499	return g920_ff_set_autocenter(hidpp, data);
3500}
3501
3502/* -------------------------------------------------------------------------- */
3503/* Logitech Dinovo Mini keyboard with builtin touchpad                        */
3504/* -------------------------------------------------------------------------- */
3505#define DINOVO_MINI_PRODUCT_ID		0xb30c
3506
3507static int lg_dinovo_input_mapping(struct hid_device *hdev, struct hid_input *hi,
3508		struct hid_field *field, struct hid_usage *usage,
3509		unsigned long **bit, int *max)
3510{
3511	if ((usage->hid & HID_USAGE_PAGE) != HID_UP_LOGIVENDOR)
3512		return 0;
3513
3514	switch (usage->hid & HID_USAGE) {
3515	case 0x00d: lg_map_key_clear(KEY_MEDIA);	break;
3516	default:
3517		return 0;
3518	}
3519	return 1;
3520}
3521
3522/* -------------------------------------------------------------------------- */
3523/* HID++1.0 devices which use HID++ reports for their wheels                  */
3524/* -------------------------------------------------------------------------- */
3525static int hidpp10_wheel_connect(struct hidpp_device *hidpp)
3526{
3527	return hidpp10_set_register(hidpp, HIDPP_REG_ENABLE_REPORTS, 0,
3528			HIDPP_ENABLE_WHEEL_REPORT | HIDPP_ENABLE_HWHEEL_REPORT,
3529			HIDPP_ENABLE_WHEEL_REPORT | HIDPP_ENABLE_HWHEEL_REPORT);
3530}
3531
3532static int hidpp10_wheel_raw_event(struct hidpp_device *hidpp,
3533				   u8 *data, int size)
3534{
3535	s8 value, hvalue;
3536
3537	if (!hidpp->input)
3538		return -EINVAL;
3539
3540	if (size < 7)
3541		return 0;
3542
3543	if (data[0] != REPORT_ID_HIDPP_SHORT || data[2] != HIDPP_SUB_ID_ROLLER)
3544		return 0;
3545
3546	value = data[3];
3547	hvalue = data[4];
3548
3549	input_report_rel(hidpp->input, REL_WHEEL, value);
3550	input_report_rel(hidpp->input, REL_WHEEL_HI_RES, value * 120);
3551	input_report_rel(hidpp->input, REL_HWHEEL, hvalue);
3552	input_report_rel(hidpp->input, REL_HWHEEL_HI_RES, hvalue * 120);
3553	input_sync(hidpp->input);
3554
3555	return 1;
3556}
3557
3558static void hidpp10_wheel_populate_input(struct hidpp_device *hidpp,
3559					 struct input_dev *input_dev)
3560{
3561	__set_bit(EV_REL, input_dev->evbit);
3562	__set_bit(REL_WHEEL, input_dev->relbit);
3563	__set_bit(REL_WHEEL_HI_RES, input_dev->relbit);
3564	__set_bit(REL_HWHEEL, input_dev->relbit);
3565	__set_bit(REL_HWHEEL_HI_RES, input_dev->relbit);
3566}
3567
3568/* -------------------------------------------------------------------------- */
3569/* HID++1.0 mice which use HID++ reports for extra mouse buttons              */
3570/* -------------------------------------------------------------------------- */
3571static int hidpp10_extra_mouse_buttons_connect(struct hidpp_device *hidpp)
3572{
3573	return hidpp10_set_register(hidpp, HIDPP_REG_ENABLE_REPORTS, 0,
3574				    HIDPP_ENABLE_MOUSE_EXTRA_BTN_REPORT,
3575				    HIDPP_ENABLE_MOUSE_EXTRA_BTN_REPORT);
3576}
3577
3578static int hidpp10_extra_mouse_buttons_raw_event(struct hidpp_device *hidpp,
3579				    u8 *data, int size)
3580{
3581	int i;
3582
3583	if (!hidpp->input)
3584		return -EINVAL;
3585
3586	if (size < 7)
3587		return 0;
3588
3589	if (data[0] != REPORT_ID_HIDPP_SHORT ||
3590	    data[2] != HIDPP_SUB_ID_MOUSE_EXTRA_BTNS)
3591		return 0;
3592
3593	/*
3594	 * Buttons are either delivered through the regular mouse report *or*
3595	 * through the extra buttons report. At least for button 6 how it is
3596	 * delivered differs per receiver firmware version. Even receivers with
3597	 * the same usb-id show different behavior, so we handle both cases.
3598	 */
3599	for (i = 0; i < 8; i++)
3600		input_report_key(hidpp->input, BTN_MOUSE + i,
3601				 (data[3] & (1 << i)));
3602
3603	/* Some mice report events on button 9+, use BTN_MISC */
3604	for (i = 0; i < 8; i++)
3605		input_report_key(hidpp->input, BTN_MISC + i,
3606				 (data[4] & (1 << i)));
3607
3608	input_sync(hidpp->input);
3609	return 1;
3610}
3611
3612static void hidpp10_extra_mouse_buttons_populate_input(
3613			struct hidpp_device *hidpp, struct input_dev *input_dev)
3614{
3615	/* BTN_MOUSE - BTN_MOUSE+7 are set already by the descriptor */
3616	__set_bit(BTN_0, input_dev->keybit);
3617	__set_bit(BTN_1, input_dev->keybit);
3618	__set_bit(BTN_2, input_dev->keybit);
3619	__set_bit(BTN_3, input_dev->keybit);
3620	__set_bit(BTN_4, input_dev->keybit);
3621	__set_bit(BTN_5, input_dev->keybit);
3622	__set_bit(BTN_6, input_dev->keybit);
3623	__set_bit(BTN_7, input_dev->keybit);
3624}
3625
3626/* -------------------------------------------------------------------------- */
3627/* HID++1.0 kbds which only report 0x10xx consumer usages through sub-id 0x03 */
3628/* -------------------------------------------------------------------------- */
3629
3630/* Find the consumer-page input report desc and change Maximums to 0x107f */
3631static u8 *hidpp10_consumer_keys_report_fixup(struct hidpp_device *hidpp,
3632					      u8 *_rdesc, unsigned int *rsize)
3633{
3634	/* Note 0 terminated so we can use strnstr to search for this. */
3635	static const char consumer_rdesc_start[] = {
3636		0x05, 0x0C,	/* USAGE_PAGE (Consumer Devices)       */
3637		0x09, 0x01,	/* USAGE (Consumer Control)            */
3638		0xA1, 0x01,	/* COLLECTION (Application)            */
3639		0x85, 0x03,	/* REPORT_ID = 3                       */
3640		0x75, 0x10,	/* REPORT_SIZE (16)                    */
3641		0x95, 0x02,	/* REPORT_COUNT (2)                    */
3642		0x15, 0x01,	/* LOGICAL_MIN (1)                     */
3643		0x26, 0x00	/* LOGICAL_MAX (...                    */
3644	};
3645	char *consumer_rdesc, *rdesc = (char *)_rdesc;
3646	unsigned int size;
3647
3648	consumer_rdesc = strnstr(rdesc, consumer_rdesc_start, *rsize);
3649	size = *rsize - (consumer_rdesc - rdesc);
3650	if (consumer_rdesc && size >= 25) {
3651		consumer_rdesc[15] = 0x7f;
3652		consumer_rdesc[16] = 0x10;
3653		consumer_rdesc[20] = 0x7f;
3654		consumer_rdesc[21] = 0x10;
3655	}
3656	return _rdesc;
3657}
3658
3659static int hidpp10_consumer_keys_connect(struct hidpp_device *hidpp)
3660{
3661	return hidpp10_set_register(hidpp, HIDPP_REG_ENABLE_REPORTS, 0,
3662				    HIDPP_ENABLE_CONSUMER_REPORT,
3663				    HIDPP_ENABLE_CONSUMER_REPORT);
3664}
3665
3666static int hidpp10_consumer_keys_raw_event(struct hidpp_device *hidpp,
3667					   u8 *data, int size)
3668{
3669	u8 consumer_report[5];
3670
3671	if (size < 7)
3672		return 0;
3673
3674	if (data[0] != REPORT_ID_HIDPP_SHORT ||
3675	    data[2] != HIDPP_SUB_ID_CONSUMER_VENDOR_KEYS)
3676		return 0;
3677
3678	/*
3679	 * Build a normal consumer report (3) out of the data, this detour
3680	 * is necessary to get some keyboards to report their 0x10xx usages.
3681	 */
3682	consumer_report[0] = 0x03;
3683	memcpy(&consumer_report[1], &data[3], 4);
3684	/* We are called from atomic context */
3685	hid_report_raw_event(hidpp->hid_dev, HID_INPUT_REPORT,
3686			     consumer_report, 5, 1);
3687
3688	return 1;
3689}
3690
3691/* -------------------------------------------------------------------------- */
3692/* High-resolution scroll wheels                                              */
3693/* -------------------------------------------------------------------------- */
3694
3695static int hi_res_scroll_enable(struct hidpp_device *hidpp)
3696{
3697	int ret;
3698	u8 multiplier = 1;
3699
3700	if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP20_HI_RES_WHEEL) {
3701		ret = hidpp_hrw_set_wheel_mode(hidpp, false, true, false);
3702		if (ret == 0)
3703			ret = hidpp_hrw_get_wheel_capability(hidpp, &multiplier);
3704	} else if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP20_HI_RES_SCROLL) {
3705		ret = hidpp_hrs_set_highres_scrolling_mode(hidpp, true,
3706							   &multiplier);
3707	} else /* if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP10_FAST_SCROLL) */ {
3708		ret = hidpp10_enable_scrolling_acceleration(hidpp);
3709		multiplier = 8;
3710	}
3711	if (ret) {
3712		hid_dbg(hidpp->hid_dev,
3713			"Could not enable hi-res scrolling: %d\n", ret);
3714		return ret;
3715	}
3716
3717	if (multiplier == 0) {
3718		hid_dbg(hidpp->hid_dev,
3719			"Invalid multiplier 0 from device, setting it to 1\n");
3720		multiplier = 1;
3721	}
3722
3723	hidpp->vertical_wheel_counter.wheel_multiplier = multiplier;
3724	hid_dbg(hidpp->hid_dev, "wheel multiplier = %d\n", multiplier);
3725	return 0;
3726}
3727
3728static int hidpp_initialize_hires_scroll(struct hidpp_device *hidpp)
3729{
3730	int ret;
3731	unsigned long capabilities;
3732
3733	capabilities = hidpp->capabilities;
3734
3735	if (hidpp->protocol_major >= 2) {
3736		u8 feature_index;
3737		u8 feature_type;
3738
3739		ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_HIRES_WHEEL,
3740					     &feature_index, &feature_type);
3741		if (!ret) {
3742			hidpp->capabilities |= HIDPP_CAPABILITY_HIDPP20_HI_RES_WHEEL;
3743			hid_dbg(hidpp->hid_dev, "Detected HID++ 2.0 hi-res scroll wheel\n");
3744			return 0;
3745		}
3746		ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_HI_RESOLUTION_SCROLLING,
3747					     &feature_index, &feature_type);
3748		if (!ret) {
3749			hidpp->capabilities |= HIDPP_CAPABILITY_HIDPP20_HI_RES_SCROLL;
3750			hid_dbg(hidpp->hid_dev, "Detected HID++ 2.0 hi-res scrolling\n");
3751		}
3752	} else {
3753		/* We cannot detect fast scrolling support on HID++ 1.0 devices */
3754		if (hidpp->quirks & HIDPP_QUIRK_HI_RES_SCROLL_1P0) {
 
 
 
 
 
 
3755			hidpp->capabilities |= HIDPP_CAPABILITY_HIDPP10_FAST_SCROLL;
3756			hid_dbg(hidpp->hid_dev, "Detected HID++ 1.0 fast scroll\n");
3757		}
3758	}
3759
3760	if (hidpp->capabilities == capabilities)
3761		hid_dbg(hidpp->hid_dev, "Did not detect HID++ hi-res scrolling hardware support\n");
3762	return 0;
3763}
3764
3765/* -------------------------------------------------------------------------- */
3766/* Generic HID++ devices                                                      */
3767/* -------------------------------------------------------------------------- */
3768
3769static u8 *hidpp_report_fixup(struct hid_device *hdev, u8 *rdesc,
3770			      unsigned int *rsize)
3771{
3772	struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3773
3774	if (!hidpp)
3775		return rdesc;
3776
3777	/* For 27 MHz keyboards the quirk gets set after hid_parse. */
3778	if (hdev->group == HID_GROUP_LOGITECH_27MHZ_DEVICE ||
3779	    (hidpp->quirks & HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS))
3780		rdesc = hidpp10_consumer_keys_report_fixup(hidpp, rdesc, rsize);
3781
3782	return rdesc;
3783}
3784
3785static int hidpp_input_mapping(struct hid_device *hdev, struct hid_input *hi,
3786		struct hid_field *field, struct hid_usage *usage,
3787		unsigned long **bit, int *max)
3788{
3789	struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3790
3791	if (!hidpp)
3792		return 0;
3793
3794	if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
3795		return wtp_input_mapping(hdev, hi, field, usage, bit, max);
3796	else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560 &&
3797			field->application != HID_GD_MOUSE)
3798		return m560_input_mapping(hdev, hi, field, usage, bit, max);
3799
3800	if (hdev->product == DINOVO_MINI_PRODUCT_ID)
3801		return lg_dinovo_input_mapping(hdev, hi, field, usage, bit, max);
3802
3803	return 0;
3804}
3805
3806static int hidpp_input_mapped(struct hid_device *hdev, struct hid_input *hi,
3807		struct hid_field *field, struct hid_usage *usage,
3808		unsigned long **bit, int *max)
3809{
3810	struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3811
3812	if (!hidpp)
3813		return 0;
3814
3815	/* Ensure that Logitech G920 is not given a default fuzz/flat value */
3816	if (hidpp->quirks & HIDPP_QUIRK_CLASS_G920) {
3817		if (usage->type == EV_ABS && (usage->code == ABS_X ||
3818				usage->code == ABS_Y || usage->code == ABS_Z ||
3819				usage->code == ABS_RZ)) {
3820			field->application = HID_GD_MULTIAXIS;
3821		}
3822	}
3823
3824	return 0;
3825}
3826
3827
3828static void hidpp_populate_input(struct hidpp_device *hidpp,
3829				 struct input_dev *input)
3830{
3831	hidpp->input = input;
3832
3833	if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
3834		wtp_populate_input(hidpp, input);
3835	else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560)
3836		m560_populate_input(hidpp, input);
3837
3838	if (hidpp->quirks & HIDPP_QUIRK_HIDPP_WHEELS)
3839		hidpp10_wheel_populate_input(hidpp, input);
3840
3841	if (hidpp->quirks & HIDPP_QUIRK_HIDPP_EXTRA_MOUSE_BTNS)
3842		hidpp10_extra_mouse_buttons_populate_input(hidpp, input);
3843}
3844
3845static int hidpp_input_configured(struct hid_device *hdev,
3846				struct hid_input *hidinput)
3847{
3848	struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3849	struct input_dev *input = hidinput->input;
3850
3851	if (!hidpp)
3852		return 0;
3853
3854	hidpp_populate_input(hidpp, input);
3855
3856	return 0;
3857}
3858
3859static int hidpp_raw_hidpp_event(struct hidpp_device *hidpp, u8 *data,
3860		int size)
3861{
3862	struct hidpp_report *question = hidpp->send_receive_buf;
3863	struct hidpp_report *answer = hidpp->send_receive_buf;
3864	struct hidpp_report *report = (struct hidpp_report *)data;
3865	int ret;
3866
3867	/*
3868	 * If the mutex is locked then we have a pending answer from a
3869	 * previously sent command.
3870	 */
3871	if (unlikely(mutex_is_locked(&hidpp->send_mutex))) {
3872		/*
3873		 * Check for a correct hidpp20 answer or the corresponding
3874		 * error
3875		 */
3876		if (hidpp_match_answer(question, report) ||
3877				hidpp_match_error(question, report)) {
3878			*answer = *report;
3879			hidpp->answer_available = true;
3880			wake_up(&hidpp->wait);
3881			/*
3882			 * This was an answer to a command that this driver sent
3883			 * We return 1 to hid-core to avoid forwarding the
3884			 * command upstream as it has been treated by the driver
3885			 */
3886
3887			return 1;
3888		}
3889	}
3890
3891	if (unlikely(hidpp_report_is_connect_event(hidpp, report))) {
 
 
3892		if (schedule_work(&hidpp->work) == 0)
3893			dbg_hid("%s: connect event already queued\n", __func__);
3894		return 1;
3895	}
3896
3897	if (hidpp->hid_dev->group == HID_GROUP_LOGITECH_27MHZ_DEVICE &&
3898	    data[0] == REPORT_ID_HIDPP_SHORT &&
3899	    data[2] == HIDPP_SUB_ID_USER_IFACE_EVENT &&
3900	    (data[3] & HIDPP_USER_IFACE_EVENT_ENCRYPTION_KEY_LOST)) {
3901		dev_err_ratelimited(&hidpp->hid_dev->dev,
3902			"Error the keyboard's wireless encryption key has been lost, your keyboard will not work unless you re-configure encryption.\n");
3903		dev_err_ratelimited(&hidpp->hid_dev->dev,
3904			"See: https://gitlab.freedesktop.org/jwrdegoede/logitech-27mhz-keyboard-encryption-setup/\n");
3905	}
3906
3907	if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP20_BATTERY) {
3908		ret = hidpp20_battery_event_1000(hidpp, data, size);
3909		if (ret != 0)
3910			return ret;
3911		ret = hidpp20_battery_event_1004(hidpp, data, size);
3912		if (ret != 0)
3913			return ret;
3914		ret = hidpp_solar_battery_event(hidpp, data, size);
3915		if (ret != 0)
3916			return ret;
3917		ret = hidpp20_battery_voltage_event(hidpp, data, size);
3918		if (ret != 0)
3919			return ret;
3920		ret = hidpp20_adc_measurement_event_1f20(hidpp, data, size);
3921		if (ret != 0)
3922			return ret;
3923	}
3924
3925	if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP10_BATTERY) {
3926		ret = hidpp10_battery_event(hidpp, data, size);
3927		if (ret != 0)
3928			return ret;
3929	}
3930
3931	if (hidpp->quirks & HIDPP_QUIRK_HIDPP_WHEELS) {
3932		ret = hidpp10_wheel_raw_event(hidpp, data, size);
3933		if (ret != 0)
3934			return ret;
3935	}
3936
3937	if (hidpp->quirks & HIDPP_QUIRK_HIDPP_EXTRA_MOUSE_BTNS) {
3938		ret = hidpp10_extra_mouse_buttons_raw_event(hidpp, data, size);
3939		if (ret != 0)
3940			return ret;
3941	}
3942
3943	if (hidpp->quirks & HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS) {
3944		ret = hidpp10_consumer_keys_raw_event(hidpp, data, size);
3945		if (ret != 0)
3946			return ret;
3947	}
3948
3949	return 0;
3950}
3951
3952static int hidpp_raw_event(struct hid_device *hdev, struct hid_report *report,
3953		u8 *data, int size)
3954{
3955	struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3956	int ret = 0;
3957
3958	if (!hidpp)
3959		return 0;
3960
3961	/* Generic HID++ processing. */
3962	switch (data[0]) {
3963	case REPORT_ID_HIDPP_VERY_LONG:
3964		if (size != hidpp->very_long_report_length) {
3965			hid_err(hdev, "received hid++ report of bad size (%d)",
3966				size);
3967			return 1;
3968		}
3969		ret = hidpp_raw_hidpp_event(hidpp, data, size);
3970		break;
3971	case REPORT_ID_HIDPP_LONG:
3972		if (size != HIDPP_REPORT_LONG_LENGTH) {
3973			hid_err(hdev, "received hid++ report of bad size (%d)",
3974				size);
3975			return 1;
3976		}
3977		ret = hidpp_raw_hidpp_event(hidpp, data, size);
3978		break;
3979	case REPORT_ID_HIDPP_SHORT:
3980		if (size != HIDPP_REPORT_SHORT_LENGTH) {
3981			hid_err(hdev, "received hid++ report of bad size (%d)",
3982				size);
3983			return 1;
3984		}
3985		ret = hidpp_raw_hidpp_event(hidpp, data, size);
3986		break;
3987	}
3988
3989	/* If no report is available for further processing, skip calling
3990	 * raw_event of subclasses. */
3991	if (ret != 0)
3992		return ret;
3993
3994	if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
3995		return wtp_raw_event(hdev, data, size);
3996	else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560)
3997		return m560_raw_event(hdev, data, size);
3998
3999	return 0;
4000}
4001
4002static int hidpp_event(struct hid_device *hdev, struct hid_field *field,
4003	struct hid_usage *usage, __s32 value)
4004{
4005	/* This function will only be called for scroll events, due to the
4006	 * restriction imposed in hidpp_usages.
4007	 */
4008	struct hidpp_device *hidpp = hid_get_drvdata(hdev);
4009	struct hidpp_scroll_counter *counter;
4010
4011	if (!hidpp)
4012		return 0;
4013
4014	counter = &hidpp->vertical_wheel_counter;
4015	/* A scroll event may occur before the multiplier has been retrieved or
4016	 * the input device set, or high-res scroll enabling may fail. In such
4017	 * cases we must return early (falling back to default behaviour) to
4018	 * avoid a crash in hidpp_scroll_counter_handle_scroll.
4019	 */
4020	if (!(hidpp->capabilities & HIDPP_CAPABILITY_HI_RES_SCROLL)
4021	    || value == 0 || hidpp->input == NULL
4022	    || counter->wheel_multiplier == 0)
4023		return 0;
4024
4025	hidpp_scroll_counter_handle_scroll(hidpp->input, counter, value);
4026	return 1;
4027}
4028
4029static int hidpp_initialize_battery(struct hidpp_device *hidpp)
4030{
4031	static atomic_t battery_no = ATOMIC_INIT(0);
4032	struct power_supply_config cfg = { .drv_data = hidpp };
4033	struct power_supply_desc *desc = &hidpp->battery.desc;
4034	enum power_supply_property *battery_props;
4035	struct hidpp_battery *battery;
4036	unsigned int num_battery_props;
4037	unsigned long n;
4038	int ret;
4039
4040	if (hidpp->battery.ps)
4041		return 0;
4042
4043	hidpp->battery.feature_index = 0xff;
4044	hidpp->battery.solar_feature_index = 0xff;
4045	hidpp->battery.voltage_feature_index = 0xff;
4046	hidpp->battery.adc_measurement_feature_index = 0xff;
4047
4048	if (hidpp->protocol_major >= 2) {
4049		if (hidpp->quirks & HIDPP_QUIRK_CLASS_K750)
4050			ret = hidpp_solar_request_battery_event(hidpp);
4051		else {
4052			/* we only support one battery feature right now, so let's
4053			   first check the ones that support battery level first
4054			   and leave voltage for last */
4055			ret = hidpp20_query_battery_info_1000(hidpp);
4056			if (ret)
4057				ret = hidpp20_query_battery_info_1004(hidpp);
4058			if (ret)
4059				ret = hidpp20_query_battery_voltage_info(hidpp);
4060			if (ret)
4061				ret = hidpp20_query_adc_measurement_info_1f20(hidpp);
4062		}
4063
4064		if (ret)
4065			return ret;
4066		hidpp->capabilities |= HIDPP_CAPABILITY_HIDPP20_BATTERY;
4067	} else {
4068		ret = hidpp10_query_battery_status(hidpp);
4069		if (ret) {
4070			ret = hidpp10_query_battery_mileage(hidpp);
4071			if (ret)
4072				return -ENOENT;
4073			hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_MILEAGE;
4074		} else {
4075			hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS;
4076		}
4077		hidpp->capabilities |= HIDPP_CAPABILITY_HIDPP10_BATTERY;
4078	}
4079
4080	battery_props = devm_kmemdup(&hidpp->hid_dev->dev,
4081				     hidpp_battery_props,
4082				     sizeof(hidpp_battery_props),
4083				     GFP_KERNEL);
4084	if (!battery_props)
4085		return -ENOMEM;
4086
4087	num_battery_props = ARRAY_SIZE(hidpp_battery_props) - 3;
4088
4089	if (hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_MILEAGE ||
4090	    hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_PERCENTAGE ||
4091	    hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_VOLTAGE ||
4092	    hidpp->capabilities & HIDPP_CAPABILITY_ADC_MEASUREMENT)
4093		battery_props[num_battery_props++] =
4094				POWER_SUPPLY_PROP_CAPACITY;
4095
4096	if (hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS)
4097		battery_props[num_battery_props++] =
4098				POWER_SUPPLY_PROP_CAPACITY_LEVEL;
4099
4100	if (hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_VOLTAGE ||
4101	    hidpp->capabilities & HIDPP_CAPABILITY_ADC_MEASUREMENT)
4102		battery_props[num_battery_props++] =
4103			POWER_SUPPLY_PROP_VOLTAGE_NOW;
4104
4105	battery = &hidpp->battery;
4106
4107	n = atomic_inc_return(&battery_no) - 1;
4108	desc->properties = battery_props;
4109	desc->num_properties = num_battery_props;
4110	desc->get_property = hidpp_battery_get_property;
4111	sprintf(battery->name, "hidpp_battery_%ld", n);
4112	desc->name = battery->name;
4113	desc->type = POWER_SUPPLY_TYPE_BATTERY;
4114	desc->use_for_apm = 0;
4115
4116	battery->ps = devm_power_supply_register(&hidpp->hid_dev->dev,
4117						 &battery->desc,
4118						 &cfg);
4119	if (IS_ERR(battery->ps))
4120		return PTR_ERR(battery->ps);
4121
4122	power_supply_powers(battery->ps, &hidpp->hid_dev->dev);
4123
4124	return ret;
4125}
4126
4127/* Get name + serial for USB and Bluetooth HID++ devices */
4128static void hidpp_non_unifying_init(struct hidpp_device *hidpp)
4129{
4130	struct hid_device *hdev = hidpp->hid_dev;
4131	char *name;
4132
4133	/* Bluetooth devices already have their serialnr set */
4134	if (hid_is_usb(hdev))
4135		hidpp_serial_init(hidpp);
4136
4137	name = hidpp_get_device_name(hidpp);
4138	if (name) {
 
 
 
4139		dbg_hid("HID++: Got name: %s\n", name);
4140		snprintf(hdev->name, sizeof(hdev->name), "%s", name);
4141		kfree(name);
4142	}
 
 
4143}
4144
4145static int hidpp_input_open(struct input_dev *dev)
4146{
4147	struct hid_device *hid = input_get_drvdata(dev);
4148
4149	return hid_hw_open(hid);
4150}
4151
4152static void hidpp_input_close(struct input_dev *dev)
4153{
4154	struct hid_device *hid = input_get_drvdata(dev);
4155
4156	hid_hw_close(hid);
4157}
4158
4159static struct input_dev *hidpp_allocate_input(struct hid_device *hdev)
4160{
4161	struct input_dev *input_dev = devm_input_allocate_device(&hdev->dev);
4162	struct hidpp_device *hidpp = hid_get_drvdata(hdev);
4163
4164	if (!input_dev)
4165		return NULL;
4166
4167	input_set_drvdata(input_dev, hdev);
4168	input_dev->open = hidpp_input_open;
4169	input_dev->close = hidpp_input_close;
4170
4171	input_dev->name = hidpp->name;
4172	input_dev->phys = hdev->phys;
4173	input_dev->uniq = hdev->uniq;
4174	input_dev->id.bustype = hdev->bus;
4175	input_dev->id.vendor  = hdev->vendor;
4176	input_dev->id.product = hdev->product;
4177	input_dev->id.version = hdev->version;
4178	input_dev->dev.parent = &hdev->dev;
4179
4180	return input_dev;
4181}
4182
4183static void hidpp_connect_event(struct work_struct *work)
4184{
4185	struct hidpp_device *hidpp = container_of(work, struct hidpp_device, work);
4186	struct hid_device *hdev = hidpp->hid_dev;
 
 
4187	struct input_dev *input;
4188	char *name, *devm_name;
4189	int ret;
4190
4191	/* Get device version to check if it is connected */
4192	ret = hidpp_root_get_protocol_version(hidpp);
4193	if (ret) {
4194		hid_dbg(hidpp->hid_dev, "Disconnected\n");
4195		if (hidpp->battery.ps) {
4196			hidpp->battery.online = false;
4197			hidpp->battery.status = POWER_SUPPLY_STATUS_UNKNOWN;
4198			hidpp->battery.level = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
4199			power_supply_changed(hidpp->battery.ps);
4200		}
4201		return;
4202	}
4203
4204	if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP) {
4205		ret = wtp_connect(hdev);
4206		if (ret)
4207			return;
4208	} else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560) {
4209		ret = m560_send_config_command(hdev);
4210		if (ret)
4211			return;
4212	} else if (hidpp->quirks & HIDPP_QUIRK_CLASS_K400) {
4213		ret = k400_connect(hdev);
4214		if (ret)
4215			return;
4216	}
4217
4218	if (hidpp->quirks & HIDPP_QUIRK_HIDPP_WHEELS) {
4219		ret = hidpp10_wheel_connect(hidpp);
4220		if (ret)
4221			return;
4222	}
4223
4224	if (hidpp->quirks & HIDPP_QUIRK_HIDPP_EXTRA_MOUSE_BTNS) {
4225		ret = hidpp10_extra_mouse_buttons_connect(hidpp);
4226		if (ret)
4227			return;
4228	}
4229
4230	if (hidpp->quirks & HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS) {
4231		ret = hidpp10_consumer_keys_connect(hidpp);
4232		if (ret)
4233			return;
4234	}
4235
4236	if (hidpp->protocol_major >= 2) {
4237		u8 feature_index;
4238
4239		if (!hidpp_get_wireless_feature_index(hidpp, &feature_index))
4240			hidpp->wireless_feature_index = feature_index;
 
 
 
4241	}
4242
4243	if (hidpp->name == hdev->name && hidpp->protocol_major >= 2) {
4244		name = hidpp_get_device_name(hidpp);
4245		if (name) {
4246			devm_name = devm_kasprintf(&hdev->dev, GFP_KERNEL,
4247						   "%s", name);
4248			kfree(name);
4249			if (!devm_name)
4250				return;
4251
4252			hidpp->name = devm_name;
4253		}
4254	}
4255
4256	hidpp_initialize_battery(hidpp);
4257	if (!hid_is_usb(hidpp->hid_dev))
4258		hidpp_initialize_hires_scroll(hidpp);
4259
4260	/* forward current battery state */
4261	if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP10_BATTERY) {
4262		hidpp10_enable_battery_reporting(hidpp);
4263		if (hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_MILEAGE)
4264			hidpp10_query_battery_mileage(hidpp);
4265		else
4266			hidpp10_query_battery_status(hidpp);
4267	} else if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP20_BATTERY) {
4268		if (hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_VOLTAGE)
4269			hidpp20_query_battery_voltage_info(hidpp);
4270		else if (hidpp->capabilities & HIDPP_CAPABILITY_UNIFIED_BATTERY)
4271			hidpp20_query_battery_info_1004(hidpp);
4272		else if (hidpp->capabilities & HIDPP_CAPABILITY_ADC_MEASUREMENT)
4273			hidpp20_query_adc_measurement_info_1f20(hidpp);
4274		else
4275			hidpp20_query_battery_info_1000(hidpp);
4276	}
4277	if (hidpp->battery.ps)
4278		power_supply_changed(hidpp->battery.ps);
4279
4280	if (hidpp->capabilities & HIDPP_CAPABILITY_HI_RES_SCROLL)
4281		hi_res_scroll_enable(hidpp);
4282
4283	if (!(hidpp->quirks & HIDPP_QUIRK_DELAYED_INIT) || hidpp->delayed_input)
4284		/* if the input nodes are already created, we can stop now */
4285		return;
4286
4287	input = hidpp_allocate_input(hdev);
4288	if (!input) {
4289		hid_err(hdev, "cannot allocate new input device: %d\n", ret);
4290		return;
4291	}
4292
4293	hidpp_populate_input(hidpp, input);
4294
4295	ret = input_register_device(input);
4296	if (ret) {
4297		input_free_device(input);
4298		return;
4299	}
4300
4301	hidpp->delayed_input = input;
4302}
4303
4304static DEVICE_ATTR(builtin_power_supply, 0000, NULL, NULL);
4305
4306static struct attribute *sysfs_attrs[] = {
4307	&dev_attr_builtin_power_supply.attr,
4308	NULL
4309};
4310
4311static const struct attribute_group ps_attribute_group = {
4312	.attrs = sysfs_attrs
4313};
4314
4315static int hidpp_get_report_length(struct hid_device *hdev, int id)
4316{
4317	struct hid_report_enum *re;
4318	struct hid_report *report;
4319
4320	re = &(hdev->report_enum[HID_OUTPUT_REPORT]);
4321	report = re->report_id_hash[id];
4322	if (!report)
4323		return 0;
4324
4325	return report->field[0]->report_count + 1;
4326}
4327
4328static u8 hidpp_validate_device(struct hid_device *hdev)
4329{
4330	struct hidpp_device *hidpp = hid_get_drvdata(hdev);
4331	int id, report_length;
4332	u8 supported_reports = 0;
4333
4334	id = REPORT_ID_HIDPP_SHORT;
4335	report_length = hidpp_get_report_length(hdev, id);
4336	if (report_length) {
4337		if (report_length < HIDPP_REPORT_SHORT_LENGTH)
4338			goto bad_device;
4339
4340		supported_reports |= HIDPP_REPORT_SHORT_SUPPORTED;
4341	}
4342
4343	id = REPORT_ID_HIDPP_LONG;
4344	report_length = hidpp_get_report_length(hdev, id);
4345	if (report_length) {
4346		if (report_length < HIDPP_REPORT_LONG_LENGTH)
4347			goto bad_device;
4348
4349		supported_reports |= HIDPP_REPORT_LONG_SUPPORTED;
4350	}
4351
4352	id = REPORT_ID_HIDPP_VERY_LONG;
4353	report_length = hidpp_get_report_length(hdev, id);
4354	if (report_length) {
4355		if (report_length < HIDPP_REPORT_LONG_LENGTH ||
4356		    report_length > HIDPP_REPORT_VERY_LONG_MAX_LENGTH)
4357			goto bad_device;
4358
4359		supported_reports |= HIDPP_REPORT_VERY_LONG_SUPPORTED;
4360		hidpp->very_long_report_length = report_length;
4361	}
4362
4363	return supported_reports;
4364
4365bad_device:
4366	hid_warn(hdev, "not enough values in hidpp report %d\n", id);
4367	return false;
4368}
4369
4370static bool hidpp_application_equals(struct hid_device *hdev,
4371				     unsigned int application)
4372{
4373	struct list_head *report_list;
4374	struct hid_report *report;
4375
4376	report_list = &hdev->report_enum[HID_INPUT_REPORT].report_list;
4377	report = list_first_entry_or_null(report_list, struct hid_report, list);
4378	return report && report->application == application;
4379}
4380
4381static int hidpp_probe(struct hid_device *hdev, const struct hid_device_id *id)
4382{
4383	struct hidpp_device *hidpp;
4384	int ret;
 
4385	unsigned int connect_mask = HID_CONNECT_DEFAULT;
 
4386
4387	/* report_fixup needs drvdata to be set before we call hid_parse */
4388	hidpp = devm_kzalloc(&hdev->dev, sizeof(*hidpp), GFP_KERNEL);
4389	if (!hidpp)
4390		return -ENOMEM;
4391
4392	hidpp->hid_dev = hdev;
4393	hidpp->name = hdev->name;
4394	hidpp->quirks = id->driver_data;
4395	hid_set_drvdata(hdev, hidpp);
4396
4397	ret = hid_parse(hdev);
4398	if (ret) {
4399		hid_err(hdev, "%s:parse failed\n", __func__);
4400		return ret;
4401	}
4402
4403	/*
4404	 * Make sure the device is HID++ capable, otherwise treat as generic HID
4405	 */
4406	hidpp->supported_reports = hidpp_validate_device(hdev);
4407
4408	if (!hidpp->supported_reports) {
4409		hid_set_drvdata(hdev, NULL);
4410		devm_kfree(&hdev->dev, hidpp);
4411		return hid_hw_start(hdev, HID_CONNECT_DEFAULT);
4412	}
4413
 
 
 
4414	if (id->group == HID_GROUP_LOGITECH_27MHZ_DEVICE &&
4415	    hidpp_application_equals(hdev, HID_GD_MOUSE))
4416		hidpp->quirks |= HIDPP_QUIRK_HIDPP_WHEELS |
4417				 HIDPP_QUIRK_HIDPP_EXTRA_MOUSE_BTNS;
4418
4419	if (id->group == HID_GROUP_LOGITECH_27MHZ_DEVICE &&
4420	    hidpp_application_equals(hdev, HID_GD_KEYBOARD))
4421		hidpp->quirks |= HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS;
4422
 
 
 
 
 
4423	if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP) {
4424		ret = wtp_allocate(hdev, id);
4425		if (ret)
4426			return ret;
4427	} else if (hidpp->quirks & HIDPP_QUIRK_CLASS_K400) {
4428		ret = k400_allocate(hdev);
4429		if (ret)
4430			return ret;
4431	}
4432
4433	INIT_WORK(&hidpp->work, hidpp_connect_event);
4434	mutex_init(&hidpp->send_mutex);
4435	init_waitqueue_head(&hidpp->wait);
4436
4437	/* indicates we are handling the battery properties in the kernel */
4438	ret = sysfs_create_group(&hdev->dev.kobj, &ps_attribute_group);
4439	if (ret)
4440		hid_warn(hdev, "Cannot allocate sysfs group for %s\n",
4441			 hdev->name);
4442
4443	/*
4444	 * First call hid_hw_start(hdev, 0) to allow IO without connecting any
4445	 * hid subdrivers (hid-input, hidraw). This allows retrieving the dev's
4446	 * name and serial number and store these in hdev->name and hdev->uniq,
4447	 * before the hid-input and hidraw drivers expose these to userspace.
4448	 */
4449	ret = hid_hw_start(hdev, 0);
4450	if (ret) {
4451		hid_err(hdev, "hw start failed\n");
4452		goto hid_hw_start_fail;
4453	}
4454
4455	ret = hid_hw_open(hdev);
4456	if (ret < 0) {
4457		dev_err(&hdev->dev, "%s:hid_hw_open returned error:%d\n",
4458			__func__, ret);
4459		goto hid_hw_open_fail;
4460	}
4461
4462	/* Allow incoming packets */
4463	hid_device_io_start(hdev);
4464
4465	/* Get name + serial, store in hdev->name + hdev->uniq */
4466	if (id->group == HID_GROUP_LOGITECH_DJ_DEVICE)
4467		hidpp_unifying_init(hidpp);
4468	else
4469		hidpp_non_unifying_init(hidpp);
4470
4471	if (hidpp->quirks & HIDPP_QUIRK_DELAYED_INIT)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4472		connect_mask &= ~HID_CONNECT_HIDINPUT;
4473
4474	/* Now export the actual inputs and hidraw nodes to the world */
4475	hid_device_io_stop(hdev);
4476	ret = hid_connect(hdev, connect_mask);
4477	if (ret) {
4478		hid_err(hdev, "%s:hid_connect returned error %d\n", __func__, ret);
4479		goto hid_hw_init_fail;
4480	}
4481
4482	/* Check for connected devices now that incoming packets will not be disabled again */
4483	hid_device_io_start(hdev);
4484	schedule_work(&hidpp->work);
4485	flush_work(&hidpp->work);
4486
4487	if (hidpp->quirks & HIDPP_QUIRK_CLASS_G920) {
4488		struct hidpp_ff_private_data data;
4489
4490		ret = g920_get_config(hidpp, &data);
4491		if (!ret)
4492			ret = hidpp_ff_init(hidpp, &data);
4493
4494		if (ret)
4495			hid_warn(hidpp->hid_dev,
4496		     "Unable to initialize force feedback support, errno %d\n",
4497				 ret);
4498	}
4499
4500	/*
4501	 * This relies on logi_dj_ll_close() being a no-op so that DJ connection
4502	 * events will still be received.
4503	 */
4504	hid_hw_close(hdev);
4505	return ret;
4506
4507hid_hw_init_fail:
4508	hid_hw_close(hdev);
4509hid_hw_open_fail:
4510	hid_hw_stop(hdev);
4511hid_hw_start_fail:
4512	sysfs_remove_group(&hdev->dev.kobj, &ps_attribute_group);
4513	cancel_work_sync(&hidpp->work);
4514	mutex_destroy(&hidpp->send_mutex);
4515	return ret;
4516}
4517
4518static void hidpp_remove(struct hid_device *hdev)
4519{
4520	struct hidpp_device *hidpp = hid_get_drvdata(hdev);
4521
4522	if (!hidpp)
4523		return hid_hw_stop(hdev);
4524
4525	sysfs_remove_group(&hdev->dev.kobj, &ps_attribute_group);
4526
4527	hid_hw_stop(hdev);
4528	cancel_work_sync(&hidpp->work);
4529	mutex_destroy(&hidpp->send_mutex);
4530}
4531
4532#define LDJ_DEVICE(product) \
4533	HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE, \
4534		   USB_VENDOR_ID_LOGITECH, (product))
4535
4536#define L27MHZ_DEVICE(product) \
4537	HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_27MHZ_DEVICE, \
4538		   USB_VENDOR_ID_LOGITECH, (product))
4539
4540static const struct hid_device_id hidpp_devices[] = {
4541	{ /* wireless touchpad */
4542	  LDJ_DEVICE(0x4011),
4543	  .driver_data = HIDPP_QUIRK_CLASS_WTP | HIDPP_QUIRK_DELAYED_INIT |
4544			 HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS },
4545	{ /* wireless touchpad T650 */
4546	  LDJ_DEVICE(0x4101),
4547	  .driver_data = HIDPP_QUIRK_CLASS_WTP | HIDPP_QUIRK_DELAYED_INIT },
4548	{ /* wireless touchpad T651 */
4549	  HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH,
4550		USB_DEVICE_ID_LOGITECH_T651),
4551	  .driver_data = HIDPP_QUIRK_CLASS_WTP | HIDPP_QUIRK_DELAYED_INIT },
4552	{ /* Mouse Logitech Anywhere MX */
4553	  LDJ_DEVICE(0x1017), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_1P0 },
4554	{ /* Mouse logitech M560 */
4555	  LDJ_DEVICE(0x402d),
4556	  .driver_data = HIDPP_QUIRK_DELAYED_INIT | HIDPP_QUIRK_CLASS_M560 },
4557	{ /* Mouse Logitech M705 (firmware RQM17) */
4558	  LDJ_DEVICE(0x101b), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_1P0 },
4559	{ /* Mouse Logitech Performance MX */
4560	  LDJ_DEVICE(0x101a), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_1P0 },
4561	{ /* Keyboard logitech K400 */
4562	  LDJ_DEVICE(0x4024),
4563	  .driver_data = HIDPP_QUIRK_CLASS_K400 },
4564	{ /* Solar Keyboard Logitech K750 */
4565	  LDJ_DEVICE(0x4002),
4566	  .driver_data = HIDPP_QUIRK_CLASS_K750 },
4567	{ /* Keyboard MX5000 (Bluetooth-receiver in HID proxy mode) */
4568	  LDJ_DEVICE(0xb305),
4569	  .driver_data = HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS },
4570	{ /* Dinovo Edge (Bluetooth-receiver in HID proxy mode) */
4571	  LDJ_DEVICE(0xb309),
4572	  .driver_data = HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS },
4573	{ /* Keyboard MX5500 (Bluetooth-receiver in HID proxy mode) */
4574	  LDJ_DEVICE(0xb30b),
4575	  .driver_data = HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS },
4576
4577	{ LDJ_DEVICE(HID_ANY_ID) },
4578
4579	{ /* Keyboard LX501 (Y-RR53) */
4580	  L27MHZ_DEVICE(0x0049),
4581	  .driver_data = HIDPP_QUIRK_KBD_ZOOM_WHEEL },
4582	{ /* Keyboard MX3000 (Y-RAM74) */
4583	  L27MHZ_DEVICE(0x0057),
4584	  .driver_data = HIDPP_QUIRK_KBD_SCROLL_WHEEL },
4585	{ /* Keyboard MX3200 (Y-RAV80) */
4586	  L27MHZ_DEVICE(0x005c),
4587	  .driver_data = HIDPP_QUIRK_KBD_ZOOM_WHEEL },
4588	{ /* S510 Media Remote */
4589	  L27MHZ_DEVICE(0x00fe),
4590	  .driver_data = HIDPP_QUIRK_KBD_SCROLL_WHEEL },
4591
4592	{ L27MHZ_DEVICE(HID_ANY_ID) },
4593
4594	{ /* Logitech G403 Wireless Gaming Mouse over USB */
4595	  HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC082) },
4596	{ /* Logitech G502 Lightspeed Wireless Gaming Mouse over USB */
4597	  HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC08D) },
4598	{ /* Logitech G703 Gaming Mouse over USB */
4599	  HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC087) },
4600	{ /* Logitech G703 Hero Gaming Mouse over USB */
4601	  HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC090) },
4602	{ /* Logitech G900 Gaming Mouse over USB */
4603	  HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC081) },
4604	{ /* Logitech G903 Gaming Mouse over USB */
4605	  HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC086) },
4606	{ /* Logitech G903 Hero Gaming Mouse over USB */
4607	  HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC091) },
4608	{ /* Logitech G915 TKL Keyboard over USB */
4609	  HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC343) },
4610	{ /* Logitech G920 Wheel over USB */
4611	  HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_G920_WHEEL),
4612		.driver_data = HIDPP_QUIRK_CLASS_G920 | HIDPP_QUIRK_FORCE_OUTPUT_REPORTS},
4613	{ /* Logitech G923 Wheel (Xbox version) over USB */
4614	  HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_G923_XBOX_WHEEL),
4615		.driver_data = HIDPP_QUIRK_CLASS_G920 | HIDPP_QUIRK_FORCE_OUTPUT_REPORTS },
4616	{ /* Logitech G Pro Gaming Mouse over USB */
4617	  HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC088) },
4618	{ /* Logitech G Pro X Superlight Gaming Mouse over USB */
4619	  HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC094) },
4620	{ /* Logitech G Pro X Superlight 2 Gaming Mouse over USB */
4621	  HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC09b) },
4622
4623	{ /* G935 Gaming Headset */
4624	  HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0x0a87),
4625		.driver_data = HIDPP_QUIRK_WIRELESS_STATUS },
4626
4627	{ /* MX5000 keyboard over Bluetooth */
4628	  HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb305),
4629	  .driver_data = HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS },
4630	{ /* Dinovo Edge keyboard over Bluetooth */
4631	  HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb309),
4632	  .driver_data = HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS },
4633	{ /* MX5500 keyboard over Bluetooth */
4634	  HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb30b),
4635	  .driver_data = HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS },
4636	{ /* Logitech G915 TKL keyboard over Bluetooth */
4637	  HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb35f) },
4638	{ /* M-RCQ142 V470 Cordless Laser Mouse over Bluetooth */
4639	  HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb008) },
4640	{ /* MX Master mouse over Bluetooth */
4641	  HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb012) },
4642	{ /* M720 Triathlon mouse over Bluetooth */
4643	  HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb015) },
4644	{ /* MX Ergo trackball over Bluetooth */
4645	  HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb01d) },
4646	{ HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb01e) },
4647	{ /* Signature M650 over Bluetooth */
4648	  HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb02a) },
4649	{ /* MX Master 3 mouse over Bluetooth */
4650	  HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb023) },
4651	{ /* MX Anywhere 3 mouse over Bluetooth */
4652	  HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb025) },
4653	{ /* MX Master 3S mouse over Bluetooth */
4654	  HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb034) },
4655	{}
4656};
4657
4658MODULE_DEVICE_TABLE(hid, hidpp_devices);
4659
4660static const struct hid_usage_id hidpp_usages[] = {
4661	{ HID_GD_WHEEL, EV_REL, REL_WHEEL_HI_RES },
4662	{ HID_ANY_ID - 1, HID_ANY_ID - 1, HID_ANY_ID - 1}
4663};
4664
4665static struct hid_driver hidpp_driver = {
4666	.name = "logitech-hidpp-device",
4667	.id_table = hidpp_devices,
4668	.report_fixup = hidpp_report_fixup,
4669	.probe = hidpp_probe,
4670	.remove = hidpp_remove,
4671	.raw_event = hidpp_raw_event,
4672	.usage_table = hidpp_usages,
4673	.event = hidpp_event,
4674	.input_configured = hidpp_input_configured,
4675	.input_mapping = hidpp_input_mapping,
4676	.input_mapped = hidpp_input_mapped,
4677};
4678
4679module_hid_driver(hidpp_driver);