2012-11-12 14:42:59 +00:00
|
|
|
/*
|
|
|
|
* HID over I2C protocol implementation
|
|
|
|
*
|
|
|
|
* Copyright (c) 2012 Benjamin Tissoires <benjamin.tissoires@gmail.com>
|
|
|
|
* Copyright (c) 2012 Ecole Nationale de l'Aviation Civile, France
|
|
|
|
* Copyright (c) 2012 Red Hat, Inc
|
|
|
|
*
|
|
|
|
* This code is partly based on "USB HID support for Linux":
|
|
|
|
*
|
|
|
|
* Copyright (c) 1999 Andreas Gal
|
|
|
|
* Copyright (c) 2000-2005 Vojtech Pavlik <vojtech@suse.cz>
|
|
|
|
* Copyright (c) 2005 Michael Haboustak <mike-@cinci.rr.com> for Concept2, Inc
|
|
|
|
* Copyright (c) 2007-2008 Oliver Neukum
|
|
|
|
* Copyright (c) 2006-2010 Jiri Kosina
|
|
|
|
*
|
|
|
|
* This file is subject to the terms and conditions of the GNU General Public
|
|
|
|
* License. See the file COPYING in the main directory of this archive for
|
|
|
|
* more details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/i2c.h>
|
|
|
|
#include <linux/interrupt.h>
|
|
|
|
#include <linux/input.h>
|
2016-11-21 14:21:27 +00:00
|
|
|
#include <linux/irq.h>
|
2012-11-12 14:42:59 +00:00
|
|
|
#include <linux/delay.h>
|
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <linux/pm.h>
|
2022-11-22 21:39:06 +00:00
|
|
|
#include <linux/pm_wakeirq.h>
|
2012-11-12 14:42:59 +00:00
|
|
|
#include <linux/device.h>
|
|
|
|
#include <linux/wait.h>
|
|
|
|
#include <linux/err.h>
|
|
|
|
#include <linux/string.h>
|
|
|
|
#include <linux/list.h>
|
|
|
|
#include <linux/jiffies.h>
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/hid.h>
|
2012-12-12 17:00:02 +00:00
|
|
|
#include <linux/mutex.h>
|
2022-01-18 07:26:21 +00:00
|
|
|
#include <asm/unaligned.h>
|
2012-11-12 14:42:59 +00:00
|
|
|
|
2016-11-10 03:47:13 +00:00
|
|
|
#include "../hid-ids.h"
|
2018-09-19 09:46:23 +00:00
|
|
|
#include "i2c-hid.h"
|
2016-11-10 03:47:13 +00:00
|
|
|
|
|
|
|
/* quirks to control the device */
|
|
|
|
#define I2C_HID_QUIRK_SET_PWR_WAKEUP_DEV BIT(0)
|
2017-11-07 12:28:00 +00:00
|
|
|
#define I2C_HID_QUIRK_NO_IRQ_AFTER_RESET BIT(1)
|
2019-01-07 07:24:29 +00:00
|
|
|
#define I2C_HID_QUIRK_BOGUS_IRQ BIT(4)
|
2019-11-07 14:28:11 +00:00
|
|
|
#define I2C_HID_QUIRK_RESET_ON_RESUME BIT(5)
|
2019-11-27 03:23:29 +00:00
|
|
|
#define I2C_HID_QUIRK_BAD_INPUT_SIZE BIT(6)
|
2021-04-13 01:20:50 +00:00
|
|
|
#define I2C_HID_QUIRK_NO_WAKEUP_AFTER_RESET BIT(7)
|
2019-11-27 03:23:29 +00:00
|
|
|
|
2022-01-18 07:26:21 +00:00
|
|
|
/* Command opcodes */
|
|
|
|
#define I2C_HID_OPCODE_RESET 0x01
|
|
|
|
#define I2C_HID_OPCODE_GET_REPORT 0x02
|
|
|
|
#define I2C_HID_OPCODE_SET_REPORT 0x03
|
|
|
|
#define I2C_HID_OPCODE_GET_IDLE 0x04
|
|
|
|
#define I2C_HID_OPCODE_SET_IDLE 0x05
|
|
|
|
#define I2C_HID_OPCODE_GET_PROTOCOL 0x06
|
|
|
|
#define I2C_HID_OPCODE_SET_PROTOCOL 0x07
|
|
|
|
#define I2C_HID_OPCODE_SET_POWER 0x08
|
2016-11-10 03:47:13 +00:00
|
|
|
|
2012-11-12 14:42:59 +00:00
|
|
|
/* flags */
|
2015-05-14 08:33:30 +00:00
|
|
|
#define I2C_HID_STARTED 0
|
|
|
|
#define I2C_HID_RESET_PENDING 1
|
|
|
|
#define I2C_HID_READ_PENDING 2
|
2012-11-12 14:42:59 +00:00
|
|
|
|
|
|
|
#define I2C_HID_PWR_ON 0x00
|
|
|
|
#define I2C_HID_PWR_SLEEP 0x01
|
|
|
|
|
2022-12-23 02:53:34 +00:00
|
|
|
#define i2c_hid_dbg(ihid, ...) dev_dbg(&(ihid)->client->dev, __VA_ARGS__)
|
2012-11-12 14:42:59 +00:00
|
|
|
|
|
|
|
struct i2c_hid_desc {
|
|
|
|
__le16 wHIDDescLength;
|
|
|
|
__le16 bcdVersion;
|
|
|
|
__le16 wReportDescLength;
|
|
|
|
__le16 wReportDescRegister;
|
|
|
|
__le16 wInputRegister;
|
|
|
|
__le16 wMaxInputLength;
|
|
|
|
__le16 wOutputRegister;
|
|
|
|
__le16 wMaxOutputLength;
|
|
|
|
__le16 wCommandRegister;
|
|
|
|
__le16 wDataRegister;
|
|
|
|
__le16 wVendorID;
|
|
|
|
__le16 wProductID;
|
|
|
|
__le16 wVersionID;
|
2012-12-05 14:02:53 +00:00
|
|
|
__le32 reserved;
|
2012-11-12 14:42:59 +00:00
|
|
|
} __packed;
|
|
|
|
|
|
|
|
/* The main device structure */
|
|
|
|
struct i2c_hid {
|
|
|
|
struct i2c_client *client; /* i2c client */
|
|
|
|
struct hid_device *hid; /* pointer to corresponding HID dev */
|
2022-01-18 07:26:27 +00:00
|
|
|
struct i2c_hid_desc hdesc; /* the HID Descriptor */
|
2012-11-12 14:42:59 +00:00
|
|
|
__le16 wHIDDescRegister; /* location of the i2c
|
|
|
|
* register of the HID
|
|
|
|
* descriptor. */
|
|
|
|
unsigned int bufsize; /* i2c buffer size */
|
2018-01-08 02:41:40 +00:00
|
|
|
u8 *inbuf; /* Input buffer */
|
|
|
|
u8 *rawbuf; /* Raw Input buffer */
|
|
|
|
u8 *cmdbuf; /* Command buffer */
|
2012-11-12 14:42:59 +00:00
|
|
|
|
|
|
|
unsigned long flags; /* device flags */
|
2016-11-10 03:47:13 +00:00
|
|
|
unsigned long quirks; /* Various quirks */
|
2012-11-12 14:42:59 +00:00
|
|
|
|
|
|
|
wait_queue_head_t wait; /* For waiting the interrupt */
|
2013-01-09 14:43:08 +00:00
|
|
|
|
2015-12-21 13:26:31 +00:00
|
|
|
struct mutex reset_lock;
|
2021-01-15 17:06:37 +00:00
|
|
|
|
|
|
|
struct i2chid_ops *ops;
|
2012-11-12 14:42:59 +00:00
|
|
|
};
|
|
|
|
|
2016-11-10 03:47:13 +00:00
|
|
|
static const struct i2c_hid_quirks {
|
|
|
|
__u16 idVendor;
|
|
|
|
__u16 idProduct;
|
|
|
|
__u32 quirks;
|
|
|
|
} i2c_hid_quirks[] = {
|
2019-08-30 07:58:30 +00:00
|
|
|
{ USB_VENDOR_ID_WEIDA, HID_ANY_ID,
|
2016-11-10 03:47:13 +00:00
|
|
|
I2C_HID_QUIRK_SET_PWR_WAKEUP_DEV },
|
2017-11-07 12:28:00 +00:00
|
|
|
{ I2C_VENDOR_ID_HANTICK, I2C_PRODUCT_ID_HANTICK_5288,
|
2019-10-16 15:12:24 +00:00
|
|
|
I2C_HID_QUIRK_NO_IRQ_AFTER_RESET },
|
2021-01-30 20:33:23 +00:00
|
|
|
{ I2C_VENDOR_ID_ITE, I2C_DEVICE_ID_ITE_VOYO_WINPAD_A15,
|
|
|
|
I2C_HID_QUIRK_NO_IRQ_AFTER_RESET },
|
2019-11-13 19:12:47 +00:00
|
|
|
{ I2C_VENDOR_ID_RAYDIUM, I2C_PRODUCT_ID_RAYDIUM_3118,
|
|
|
|
I2C_HID_QUIRK_NO_IRQ_AFTER_RESET },
|
2019-11-07 14:28:11 +00:00
|
|
|
{ USB_VENDOR_ID_ALPS_JP, HID_ANY_ID,
|
|
|
|
I2C_HID_QUIRK_RESET_ON_RESUME },
|
2020-04-25 10:58:17 +00:00
|
|
|
{ I2C_VENDOR_ID_SYNAPTICS, I2C_PRODUCT_ID_SYNAPTICS_SYNA2393,
|
|
|
|
I2C_HID_QUIRK_RESET_ON_RESUME },
|
2019-11-27 03:23:29 +00:00
|
|
|
{ USB_VENDOR_ID_ITE, I2C_DEVICE_ID_ITE_LENOVO_LEGION_Y720,
|
|
|
|
I2C_HID_QUIRK_BAD_INPUT_SIZE },
|
2021-04-13 01:20:50 +00:00
|
|
|
/*
|
|
|
|
* Sending the wakeup after reset actually break ELAN touchscreen controller
|
|
|
|
*/
|
|
|
|
{ USB_VENDOR_ID_ELAN, HID_ANY_ID,
|
2021-08-09 06:55:05 +00:00
|
|
|
I2C_HID_QUIRK_NO_WAKEUP_AFTER_RESET |
|
|
|
|
I2C_HID_QUIRK_BOGUS_IRQ },
|
2016-11-10 03:47:13 +00:00
|
|
|
{ 0, 0 }
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* i2c_hid_lookup_quirk: return any quirks associated with a I2C HID device
|
|
|
|
* @idVendor: the 16-bit vendor ID
|
|
|
|
* @idProduct: the 16-bit product ID
|
|
|
|
*
|
|
|
|
* Returns: a u32 quirks value.
|
|
|
|
*/
|
|
|
|
static u32 i2c_hid_lookup_quirk(const u16 idVendor, const u16 idProduct)
|
|
|
|
{
|
|
|
|
u32 quirks = 0;
|
|
|
|
int n;
|
|
|
|
|
|
|
|
for (n = 0; i2c_hid_quirks[n].idVendor; n++)
|
|
|
|
if (i2c_hid_quirks[n].idVendor == idVendor &&
|
|
|
|
(i2c_hid_quirks[n].idProduct == (__u16)HID_ANY_ID ||
|
|
|
|
i2c_hid_quirks[n].idProduct == idProduct))
|
|
|
|
quirks = i2c_hid_quirks[n].quirks;
|
|
|
|
|
|
|
|
return quirks;
|
|
|
|
}
|
|
|
|
|
2022-01-18 07:26:21 +00:00
|
|
|
static int i2c_hid_xfer(struct i2c_hid *ihid,
|
|
|
|
u8 *send_buf, int send_len, u8 *recv_buf, int recv_len)
|
|
|
|
{
|
|
|
|
struct i2c_client *client = ihid->client;
|
|
|
|
struct i2c_msg msgs[2] = { 0 };
|
|
|
|
int n = 0;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (send_len) {
|
|
|
|
i2c_hid_dbg(ihid, "%s: cmd=%*ph\n",
|
|
|
|
__func__, send_len, send_buf);
|
|
|
|
|
|
|
|
msgs[n].addr = client->addr;
|
2022-01-18 07:26:28 +00:00
|
|
|
msgs[n].flags = (client->flags & I2C_M_TEN) | I2C_M_DMA_SAFE;
|
2022-01-18 07:26:21 +00:00
|
|
|
msgs[n].len = send_len;
|
|
|
|
msgs[n].buf = send_buf;
|
|
|
|
n++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (recv_len) {
|
|
|
|
msgs[n].addr = client->addr;
|
2022-01-18 07:26:28 +00:00
|
|
|
msgs[n].flags = (client->flags & I2C_M_TEN) |
|
|
|
|
I2C_M_RD | I2C_M_DMA_SAFE;
|
2022-01-18 07:26:21 +00:00
|
|
|
msgs[n].len = recv_len;
|
|
|
|
msgs[n].buf = recv_buf;
|
|
|
|
n++;
|
|
|
|
|
|
|
|
set_bit(I2C_HID_READ_PENDING, &ihid->flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = i2c_transfer(client->adapter, msgs, n);
|
|
|
|
|
|
|
|
if (recv_len)
|
|
|
|
clear_bit(I2C_HID_READ_PENDING, &ihid->flags);
|
|
|
|
|
|
|
|
if (ret != n)
|
|
|
|
return ret < 0 ? ret : -EIO;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-01-18 07:26:22 +00:00
|
|
|
static int i2c_hid_read_register(struct i2c_hid *ihid, __le16 reg,
|
|
|
|
void *buf, size_t len)
|
|
|
|
{
|
|
|
|
*(__le16 *)ihid->cmdbuf = reg;
|
|
|
|
|
|
|
|
return i2c_hid_xfer(ihid, ihid->cmdbuf, sizeof(__le16), buf, len);
|
|
|
|
}
|
|
|
|
|
2022-01-18 07:26:21 +00:00
|
|
|
static size_t i2c_hid_encode_command(u8 *buf, u8 opcode,
|
|
|
|
int report_type, int report_id)
|
|
|
|
{
|
|
|
|
size_t length = 0;
|
|
|
|
|
|
|
|
if (report_id < 0x0F) {
|
|
|
|
buf[length++] = report_type << 4 | report_id;
|
|
|
|
buf[length++] = opcode;
|
|
|
|
} else {
|
|
|
|
buf[length++] = report_type << 4 | 0x0F;
|
|
|
|
buf[length++] = opcode;
|
|
|
|
buf[length++] = report_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
2022-01-18 07:26:25 +00:00
|
|
|
static int i2c_hid_get_report(struct i2c_hid *ihid,
|
|
|
|
u8 report_type, u8 report_id,
|
|
|
|
u8 *recv_buf, size_t recv_len)
|
2012-11-12 14:42:59 +00:00
|
|
|
{
|
2022-01-18 07:26:25 +00:00
|
|
|
size_t length = 0;
|
|
|
|
size_t ret_count;
|
|
|
|
int error;
|
2012-11-12 14:42:59 +00:00
|
|
|
|
2022-01-18 07:26:25 +00:00
|
|
|
i2c_hid_dbg(ihid, "%s\n", __func__);
|
2012-11-12 14:42:59 +00:00
|
|
|
|
2022-01-18 07:26:25 +00:00
|
|
|
/* Command register goes first */
|
|
|
|
*(__le16 *)ihid->cmdbuf = ihid->hdesc.wCommandRegister;
|
|
|
|
length += sizeof(__le16);
|
|
|
|
/* Next is GET_REPORT command */
|
|
|
|
length += i2c_hid_encode_command(ihid->cmdbuf + length,
|
|
|
|
I2C_HID_OPCODE_GET_REPORT,
|
|
|
|
report_type, report_id);
|
|
|
|
/*
|
|
|
|
* Device will send report data through data register. Because
|
|
|
|
* command can be either 2 or 3 bytes destination for the data
|
|
|
|
* register may be not aligned.
|
|
|
|
*/
|
|
|
|
put_unaligned_le16(le16_to_cpu(ihid->hdesc.wDataRegister),
|
|
|
|
ihid->cmdbuf + length);
|
|
|
|
length += sizeof(__le16);
|
2012-11-12 14:42:59 +00:00
|
|
|
|
2022-01-18 07:26:25 +00:00
|
|
|
/*
|
|
|
|
* In addition to report data device will supply data length
|
|
|
|
* in the first 2 bytes of the response, so adjust .
|
|
|
|
*/
|
|
|
|
error = i2c_hid_xfer(ihid, ihid->cmdbuf, length,
|
|
|
|
ihid->rawbuf, recv_len + sizeof(__le16));
|
|
|
|
if (error) {
|
|
|
|
dev_err(&ihid->client->dev,
|
|
|
|
"failed to set a report to device: %d\n", error);
|
|
|
|
return error;
|
|
|
|
}
|
2012-11-12 14:42:59 +00:00
|
|
|
|
2022-01-18 07:26:25 +00:00
|
|
|
/* The buffer is sufficiently aligned */
|
|
|
|
ret_count = le16_to_cpup((__le16 *)ihid->rawbuf);
|
2012-11-12 14:42:59 +00:00
|
|
|
|
2022-01-18 07:26:25 +00:00
|
|
|
/* Check for empty report response */
|
|
|
|
if (ret_count <= sizeof(__le16))
|
|
|
|
return 0;
|
2012-11-12 14:42:59 +00:00
|
|
|
|
2022-01-18 07:26:25 +00:00
|
|
|
recv_len = min(recv_len, ret_count - sizeof(__le16));
|
|
|
|
memcpy(recv_buf, ihid->rawbuf + sizeof(__le16), recv_len);
|
2012-11-12 14:42:59 +00:00
|
|
|
|
2022-01-18 07:26:25 +00:00
|
|
|
if (report_id && recv_len != 0 && recv_buf[0] != report_id) {
|
2022-01-18 07:26:19 +00:00
|
|
|
dev_err(&ihid->client->dev,
|
2022-01-18 07:26:25 +00:00
|
|
|
"device returned incorrect report (%d vs %d expected)\n",
|
|
|
|
recv_buf[0], report_id);
|
|
|
|
return -EINVAL;
|
2012-11-12 14:42:59 +00:00
|
|
|
}
|
|
|
|
|
2022-01-18 07:26:25 +00:00
|
|
|
return recv_len;
|
2012-11-12 14:42:59 +00:00
|
|
|
}
|
|
|
|
|
2022-01-18 07:26:21 +00:00
|
|
|
static size_t i2c_hid_format_report(u8 *buf, int report_id,
|
|
|
|
const u8 *data, size_t size)
|
|
|
|
{
|
|
|
|
size_t length = sizeof(__le16); /* reserve space to store size */
|
|
|
|
|
|
|
|
if (report_id)
|
|
|
|
buf[length++] = report_id;
|
|
|
|
|
|
|
|
memcpy(buf + length, data, size);
|
|
|
|
length += size;
|
|
|
|
|
|
|
|
/* Store overall size in the beginning of the buffer */
|
|
|
|
put_unaligned_le16(length, buf);
|
|
|
|
|
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
2014-02-10 17:58:49 +00:00
|
|
|
/**
|
|
|
|
* i2c_hid_set_or_send_report: forward an incoming report to the device
|
2022-01-18 07:26:19 +00:00
|
|
|
* @ihid: the i2c hid device
|
2022-01-18 07:26:21 +00:00
|
|
|
* @report_type: 0x03 for HID_FEATURE_REPORT ; 0x02 for HID_OUTPUT_REPORT
|
|
|
|
* @report_id: the report ID
|
2014-02-10 17:58:49 +00:00
|
|
|
* @buf: the actual data to transfer, without the report ID
|
2020-09-22 12:08:04 +00:00
|
|
|
* @data_len: size of buf
|
2022-01-18 07:26:21 +00:00
|
|
|
* @do_set: true: use SET_REPORT HID command, false: send plain OUTPUT report
|
2014-02-10 17:58:49 +00:00
|
|
|
*/
|
2022-01-18 07:26:21 +00:00
|
|
|
static int i2c_hid_set_or_send_report(struct i2c_hid *ihid,
|
|
|
|
u8 report_type, u8 report_id,
|
|
|
|
const u8 *buf, size_t data_len,
|
|
|
|
bool do_set)
|
2012-11-12 14:42:59 +00:00
|
|
|
{
|
2022-01-18 07:26:21 +00:00
|
|
|
size_t length = 0;
|
|
|
|
int error;
|
2016-03-14 22:21:04 +00:00
|
|
|
|
|
|
|
i2c_hid_dbg(ihid, "%s\n", __func__);
|
|
|
|
|
|
|
|
if (data_len > ihid->bufsize)
|
|
|
|
return -EINVAL;
|
2012-11-12 14:42:59 +00:00
|
|
|
|
2022-01-18 07:26:21 +00:00
|
|
|
if (!do_set && le16_to_cpu(ihid->hdesc.wMaxOutputLength) == 0)
|
2014-02-10 17:58:49 +00:00
|
|
|
return -ENOSYS;
|
|
|
|
|
2022-01-18 07:26:21 +00:00
|
|
|
if (do_set) {
|
|
|
|
/* Command register goes first */
|
|
|
|
*(__le16 *)ihid->cmdbuf = ihid->hdesc.wCommandRegister;
|
|
|
|
length += sizeof(__le16);
|
|
|
|
/* Next is SET_REPORT command */
|
|
|
|
length += i2c_hid_encode_command(ihid->cmdbuf + length,
|
|
|
|
I2C_HID_OPCODE_SET_REPORT,
|
|
|
|
report_type, report_id);
|
|
|
|
/*
|
|
|
|
* Report data will go into the data register. Because
|
|
|
|
* command can be either 2 or 3 bytes destination for
|
|
|
|
* the data register may be not aligned.
|
|
|
|
*/
|
|
|
|
put_unaligned_le16(le16_to_cpu(ihid->hdesc.wDataRegister),
|
|
|
|
ihid->cmdbuf + length);
|
|
|
|
length += sizeof(__le16);
|
2013-06-17 23:15:06 +00:00
|
|
|
} else {
|
2022-01-18 07:26:21 +00:00
|
|
|
/*
|
|
|
|
* With simple "send report" all data goes into the output
|
|
|
|
* register.
|
|
|
|
*/
|
2022-02-16 01:50:42 +00:00
|
|
|
*(__le16 *)ihid->cmdbuf = ihid->hdesc.wOutputRegister;
|
2022-01-18 07:26:21 +00:00
|
|
|
length += sizeof(__le16);
|
2013-06-17 23:15:06 +00:00
|
|
|
}
|
2012-11-12 14:42:59 +00:00
|
|
|
|
2022-01-18 07:26:21 +00:00
|
|
|
length += i2c_hid_format_report(ihid->cmdbuf + length,
|
|
|
|
report_id, buf, data_len);
|
2012-11-12 14:42:59 +00:00
|
|
|
|
2022-01-18 07:26:21 +00:00
|
|
|
error = i2c_hid_xfer(ihid, ihid->cmdbuf, length, NULL, 0);
|
|
|
|
if (error) {
|
2022-01-18 07:26:19 +00:00
|
|
|
dev_err(&ihid->client->dev,
|
2022-01-18 07:26:21 +00:00
|
|
|
"failed to set a report to device: %d\n", error);
|
|
|
|
return error;
|
2012-11-12 14:42:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return data_len;
|
|
|
|
}
|
|
|
|
|
2022-01-18 07:26:23 +00:00
|
|
|
static int i2c_hid_set_power_command(struct i2c_hid *ihid, int power_state)
|
|
|
|
{
|
|
|
|
size_t length;
|
|
|
|
|
|
|
|
/* SET_POWER uses command register */
|
|
|
|
*(__le16 *)ihid->cmdbuf = ihid->hdesc.wCommandRegister;
|
|
|
|
length = sizeof(__le16);
|
|
|
|
|
|
|
|
/* Now the command itself */
|
|
|
|
length += i2c_hid_encode_command(ihid->cmdbuf + length,
|
|
|
|
I2C_HID_OPCODE_SET_POWER,
|
|
|
|
0, power_state);
|
|
|
|
|
|
|
|
return i2c_hid_xfer(ihid, ihid->cmdbuf, length, NULL, 0);
|
|
|
|
}
|
|
|
|
|
2022-01-18 07:26:19 +00:00
|
|
|
static int i2c_hid_set_power(struct i2c_hid *ihid, int power_state)
|
2012-11-12 14:42:59 +00:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
i2c_hid_dbg(ihid, "%s\n", __func__);
|
|
|
|
|
2016-11-10 03:47:13 +00:00
|
|
|
/*
|
|
|
|
* Some devices require to send a command to wakeup before power on.
|
|
|
|
* The call will get a return value (EREMOTEIO) but device will be
|
|
|
|
* triggered and activated. After that, it goes like a normal device.
|
|
|
|
*/
|
|
|
|
if (power_state == I2C_HID_PWR_ON &&
|
|
|
|
ihid->quirks & I2C_HID_QUIRK_SET_PWR_WAKEUP_DEV) {
|
2022-01-18 07:26:23 +00:00
|
|
|
ret = i2c_hid_set_power_command(ihid, I2C_HID_PWR_ON);
|
2016-11-10 03:47:13 +00:00
|
|
|
|
|
|
|
/* Device was already activated */
|
|
|
|
if (!ret)
|
|
|
|
goto set_pwr_exit;
|
|
|
|
}
|
|
|
|
|
2022-01-18 07:26:23 +00:00
|
|
|
ret = i2c_hid_set_power_command(ihid, power_state);
|
2012-11-12 14:42:59 +00:00
|
|
|
if (ret)
|
2022-01-18 07:26:19 +00:00
|
|
|
dev_err(&ihid->client->dev,
|
|
|
|
"failed to change power setting.\n");
|
2012-11-12 14:42:59 +00:00
|
|
|
|
2016-11-10 03:47:13 +00:00
|
|
|
set_pwr_exit:
|
HID: i2c-hid: Always sleep 60ms after I2C_HID_PWR_ON commands
Before this commit i2c_hid_parse() consists of the following steps:
1. Send power on cmd
2. usleep_range(1000, 5000)
3. Send reset cmd
4. Wait for reset to complete (device interrupt, or msleep(100))
5. Send power on cmd
6. Try to read HID descriptor
Notice how there is an usleep_range(1000, 5000) after the first power-on
command, but not after the second power-on command.
Testing has shown that at least on the BMAX Y13 laptop's i2c-hid touchpad,
not having a delay after the second power-on command causes the HID
descriptor to read as all zeros.
In case we hit this on other devices too, the descriptor being all zeros
can be recognized by the following message being logged many, many times:
hid-generic 0018:0911:5288.0002: unknown main item tag 0x0
At the same time as the BMAX Y13's touchpad issue was debugged,
Kai-Heng was working on debugging some issues with Goodix i2c-hid
touchpads. It turns out that these need a delay after a PWR_ON command
too, otherwise they stop working after a suspend/resume cycle.
According to Goodix a delay of minimal 60ms is needed.
Having multiple cases where we need a delay after sending the power-on
command, seems to indicate that we should always sleep after the power-on
command.
This commit fixes the mentioned issues by moving the existing 1ms sleep to
the i2c_hid_set_power() function and changing it to a 60ms sleep.
Cc: stable@vger.kernel.org
BugLink: https://bugzilla.kernel.org/show_bug.cgi?id=208247
Reported-by: Kai-Heng Feng <kai.heng.feng@canonical.com>
Reported-and-tested-by: Andrea Borgia <andrea@borgia.bo.it>
Signed-off-by: Hans de Goede <hdegoede@redhat.com>
Signed-off-by: Jiri Kosina <jkosina@suse.cz>
2020-08-11 13:39:58 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* The HID over I2C specification states that if a DEVICE needs time
|
|
|
|
* after the PWR_ON request, it should utilise CLOCK stretching.
|
|
|
|
* However, it has been observered that the Windows driver provides a
|
|
|
|
* 1ms sleep between the PWR_ON and RESET requests.
|
|
|
|
* According to Goodix Windows even waits 60 ms after (other?)
|
|
|
|
* PWR_ON requests. Testing has confirmed that several devices
|
|
|
|
* will not work properly without a delay after a PWR_ON request.
|
|
|
|
*/
|
|
|
|
if (!ret && power_state == I2C_HID_PWR_ON)
|
|
|
|
msleep(60);
|
|
|
|
|
2012-11-12 14:42:59 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-01-18 07:26:20 +00:00
|
|
|
static int i2c_hid_execute_reset(struct i2c_hid *ihid)
|
|
|
|
{
|
2022-01-18 07:26:24 +00:00
|
|
|
size_t length = 0;
|
2022-01-18 07:26:20 +00:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
i2c_hid_dbg(ihid, "resetting...\n");
|
|
|
|
|
2022-01-18 07:26:24 +00:00
|
|
|
/* Prepare reset command. Command register goes first. */
|
|
|
|
*(__le16 *)ihid->cmdbuf = ihid->hdesc.wCommandRegister;
|
|
|
|
length += sizeof(__le16);
|
|
|
|
/* Next is RESET command itself */
|
|
|
|
length += i2c_hid_encode_command(ihid->cmdbuf + length,
|
|
|
|
I2C_HID_OPCODE_RESET, 0, 0);
|
|
|
|
|
2022-01-18 07:26:20 +00:00
|
|
|
set_bit(I2C_HID_RESET_PENDING, &ihid->flags);
|
|
|
|
|
2022-01-18 07:26:24 +00:00
|
|
|
ret = i2c_hid_xfer(ihid, ihid->cmdbuf, length, NULL, 0);
|
2022-01-18 07:26:20 +00:00
|
|
|
if (ret) {
|
|
|
|
dev_err(&ihid->client->dev, "failed to reset device.\n");
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ihid->quirks & I2C_HID_QUIRK_NO_IRQ_AFTER_RESET) {
|
|
|
|
msleep(100);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
i2c_hid_dbg(ihid, "%s: waiting...\n", __func__);
|
|
|
|
if (!wait_event_timeout(ihid->wait,
|
|
|
|
!test_bit(I2C_HID_RESET_PENDING, &ihid->flags),
|
|
|
|
msecs_to_jiffies(5000))) {
|
|
|
|
ret = -ENODATA;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
i2c_hid_dbg(ihid, "%s: finished.\n", __func__);
|
|
|
|
|
|
|
|
out:
|
|
|
|
clear_bit(I2C_HID_RESET_PENDING, &ihid->flags);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-01-18 07:26:19 +00:00
|
|
|
static int i2c_hid_hwreset(struct i2c_hid *ihid)
|
2012-11-12 14:42:59 +00:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
i2c_hid_dbg(ihid, "%s\n", __func__);
|
|
|
|
|
2015-12-21 13:26:31 +00:00
|
|
|
/*
|
|
|
|
* This prevents sending feature reports while the device is
|
|
|
|
* being reset. Otherwise we may lose the reset complete
|
|
|
|
* interrupt.
|
|
|
|
*/
|
|
|
|
mutex_lock(&ihid->reset_lock);
|
|
|
|
|
2022-01-18 07:26:19 +00:00
|
|
|
ret = i2c_hid_set_power(ihid, I2C_HID_PWR_ON);
|
2012-11-12 14:42:59 +00:00
|
|
|
if (ret)
|
2015-12-21 13:26:31 +00:00
|
|
|
goto out_unlock;
|
2012-11-12 14:42:59 +00:00
|
|
|
|
2022-01-18 07:26:20 +00:00
|
|
|
ret = i2c_hid_execute_reset(ihid);
|
2012-11-12 14:42:59 +00:00
|
|
|
if (ret) {
|
2022-01-18 07:26:20 +00:00
|
|
|
dev_err(&ihid->client->dev,
|
|
|
|
"failed to reset device: %d\n", ret);
|
2022-01-18 07:26:19 +00:00
|
|
|
i2c_hid_set_power(ihid, I2C_HID_PWR_SLEEP);
|
2019-10-20 21:47:18 +00:00
|
|
|
goto out_unlock;
|
2012-11-12 14:42:59 +00:00
|
|
|
}
|
|
|
|
|
2019-10-20 21:47:18 +00:00
|
|
|
/* At least some SIS devices need this after reset */
|
2021-04-13 01:20:50 +00:00
|
|
|
if (!(ihid->quirks & I2C_HID_QUIRK_NO_WAKEUP_AFTER_RESET))
|
2022-01-18 07:26:19 +00:00
|
|
|
ret = i2c_hid_set_power(ihid, I2C_HID_PWR_ON);
|
2019-10-20 21:47:18 +00:00
|
|
|
|
2015-12-21 13:26:31 +00:00
|
|
|
out_unlock:
|
|
|
|
mutex_unlock(&ihid->reset_lock);
|
|
|
|
return ret;
|
2012-11-12 14:42:59 +00:00
|
|
|
}
|
|
|
|
|
2012-12-04 15:27:48 +00:00
|
|
|
static void i2c_hid_get_input(struct i2c_hid *ihid)
|
2012-11-12 14:42:59 +00:00
|
|
|
{
|
2022-01-18 07:26:26 +00:00
|
|
|
u16 size = le16_to_cpu(ihid->hdesc.wMaxInputLength);
|
|
|
|
u16 ret_size;
|
2018-01-08 02:41:40 +00:00
|
|
|
int ret;
|
2015-02-20 17:45:11 +00:00
|
|
|
|
|
|
|
if (size > ihid->bufsize)
|
|
|
|
size = ihid->bufsize;
|
2012-11-12 14:42:59 +00:00
|
|
|
|
|
|
|
ret = i2c_master_recv(ihid->client, ihid->inbuf, size);
|
|
|
|
if (ret != size) {
|
|
|
|
if (ret < 0)
|
2012-12-04 15:27:48 +00:00
|
|
|
return;
|
2012-11-12 14:42:59 +00:00
|
|
|
|
|
|
|
dev_err(&ihid->client->dev, "%s: got %d data instead of %d\n",
|
|
|
|
__func__, ret, size);
|
2012-12-04 15:27:48 +00:00
|
|
|
return;
|
2012-11-12 14:42:59 +00:00
|
|
|
}
|
|
|
|
|
2022-01-18 07:26:26 +00:00
|
|
|
/* Receiving buffer is properly aligned */
|
|
|
|
ret_size = le16_to_cpup((__le16 *)ihid->inbuf);
|
2012-11-12 14:42:59 +00:00
|
|
|
if (!ret_size) {
|
|
|
|
/* host or device initiated RESET completed */
|
|
|
|
if (test_and_clear_bit(I2C_HID_RESET_PENDING, &ihid->flags))
|
|
|
|
wake_up(&ihid->wait);
|
2012-12-04 15:27:48 +00:00
|
|
|
return;
|
2012-11-12 14:42:59 +00:00
|
|
|
}
|
|
|
|
|
2022-01-18 07:26:26 +00:00
|
|
|
if ((ihid->quirks & I2C_HID_QUIRK_BOGUS_IRQ) && ret_size == 0xffff) {
|
|
|
|
dev_warn_once(&ihid->client->dev,
|
|
|
|
"%s: IRQ triggered but there's no data\n",
|
|
|
|
__func__);
|
2019-01-07 07:24:29 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-01-18 07:26:26 +00:00
|
|
|
if (ret_size > size || ret_size < sizeof(__le16)) {
|
2019-11-27 03:23:29 +00:00
|
|
|
if (ihid->quirks & I2C_HID_QUIRK_BAD_INPUT_SIZE) {
|
2022-01-18 07:26:26 +00:00
|
|
|
*(__le16 *)ihid->inbuf = cpu_to_le16(size);
|
2019-11-27 03:23:29 +00:00
|
|
|
ret_size = size;
|
|
|
|
} else {
|
2022-01-18 07:26:26 +00:00
|
|
|
dev_err(&ihid->client->dev,
|
|
|
|
"%s: incomplete report (%d/%d)\n",
|
2019-11-27 03:23:29 +00:00
|
|
|
__func__, size, ret_size);
|
|
|
|
return;
|
|
|
|
}
|
2012-11-12 14:42:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
i2c_hid_dbg(ihid, "input: %*ph\n", ret_size, ihid->inbuf);
|
|
|
|
|
2021-11-02 18:40:25 +00:00
|
|
|
if (test_bit(I2C_HID_STARTED, &ihid->flags)) {
|
2022-11-18 17:02:45 +00:00
|
|
|
if (ihid->hid->group != HID_GROUP_RMI)
|
|
|
|
pm_wakeup_event(&ihid->client->dev, 0);
|
2021-11-02 18:40:25 +00:00
|
|
|
|
2022-01-18 07:26:26 +00:00
|
|
|
hid_input_report(ihid->hid, HID_INPUT_REPORT,
|
|
|
|
ihid->inbuf + sizeof(__le16),
|
|
|
|
ret_size - sizeof(__le16), 1);
|
2021-11-02 18:40:25 +00:00
|
|
|
}
|
2012-11-12 14:42:59 +00:00
|
|
|
|
2012-12-04 15:27:48 +00:00
|
|
|
return;
|
2012-11-12 14:42:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static irqreturn_t i2c_hid_irq(int irq, void *dev_id)
|
|
|
|
{
|
|
|
|
struct i2c_hid *ihid = dev_id;
|
|
|
|
|
|
|
|
if (test_bit(I2C_HID_READ_PENDING, &ihid->flags))
|
|
|
|
return IRQ_HANDLED;
|
|
|
|
|
|
|
|
i2c_hid_get_input(ihid);
|
|
|
|
|
|
|
|
return IRQ_HANDLED;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int i2c_hid_get_report_length(struct hid_report *report)
|
|
|
|
{
|
|
|
|
return ((report->size - 1) >> 3) + 1 +
|
|
|
|
report->device->report_enum[report->type].numbered + 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Traverse the supplied list of reports and find the longest
|
|
|
|
*/
|
|
|
|
static void i2c_hid_find_max_report(struct hid_device *hid, unsigned int type,
|
|
|
|
unsigned int *max)
|
|
|
|
{
|
|
|
|
struct hid_report *report;
|
|
|
|
unsigned int size;
|
|
|
|
|
|
|
|
/* We should not rely on wMaxInputLength, as some devices may set it to
|
|
|
|
* a wrong length. */
|
|
|
|
list_for_each_entry(report, &hid->report_enum[type].report_list, list) {
|
|
|
|
size = i2c_hid_get_report_length(report);
|
|
|
|
if (*max < size)
|
|
|
|
*max = size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-05 14:02:54 +00:00
|
|
|
static void i2c_hid_free_buffers(struct i2c_hid *ihid)
|
|
|
|
{
|
|
|
|
kfree(ihid->inbuf);
|
2014-11-19 16:46:37 +00:00
|
|
|
kfree(ihid->rawbuf);
|
2012-12-05 14:02:54 +00:00
|
|
|
kfree(ihid->cmdbuf);
|
|
|
|
ihid->inbuf = NULL;
|
2014-11-19 16:46:37 +00:00
|
|
|
ihid->rawbuf = NULL;
|
2012-12-05 14:02:54 +00:00
|
|
|
ihid->cmdbuf = NULL;
|
|
|
|
ihid->bufsize = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int i2c_hid_alloc_buffers(struct i2c_hid *ihid, size_t report_size)
|
2012-11-12 14:42:59 +00:00
|
|
|
{
|
2022-01-18 07:26:21 +00:00
|
|
|
/*
|
|
|
|
* The worst case is computed from the set_report command with a
|
|
|
|
* reportID > 15 and the maximum report length.
|
|
|
|
*/
|
|
|
|
int cmd_len = sizeof(__le16) + /* command register */
|
|
|
|
sizeof(u8) + /* encoded report type/ID */
|
|
|
|
sizeof(u8) + /* opcode */
|
|
|
|
sizeof(u8) + /* optional 3rd byte report ID */
|
|
|
|
sizeof(__le16) + /* data register */
|
|
|
|
sizeof(__le16) + /* report data size */
|
|
|
|
sizeof(u8) + /* report ID if numbered report */
|
|
|
|
report_size;
|
2012-11-12 14:42:59 +00:00
|
|
|
|
2012-12-05 14:02:54 +00:00
|
|
|
ihid->inbuf = kzalloc(report_size, GFP_KERNEL);
|
2014-11-19 16:46:37 +00:00
|
|
|
ihid->rawbuf = kzalloc(report_size, GFP_KERNEL);
|
2022-01-18 07:26:21 +00:00
|
|
|
ihid->cmdbuf = kzalloc(cmd_len, GFP_KERNEL);
|
2012-11-12 14:42:59 +00:00
|
|
|
|
2022-01-18 07:26:21 +00:00
|
|
|
if (!ihid->inbuf || !ihid->rawbuf || !ihid->cmdbuf) {
|
2012-12-05 14:02:54 +00:00
|
|
|
i2c_hid_free_buffers(ihid);
|
2012-11-12 14:42:59 +00:00
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
2012-12-05 14:02:54 +00:00
|
|
|
ihid->bufsize = report_size;
|
2012-11-12 14:42:59 +00:00
|
|
|
|
2012-12-05 14:02:54 +00:00
|
|
|
return 0;
|
2012-11-12 14:42:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int i2c_hid_get_raw_report(struct hid_device *hid,
|
2022-01-18 07:26:25 +00:00
|
|
|
u8 report_type, u8 report_id,
|
|
|
|
u8 *buf, size_t count)
|
2012-11-12 14:42:59 +00:00
|
|
|
{
|
|
|
|
struct i2c_client *client = hid->driver_data;
|
|
|
|
struct i2c_hid *ihid = i2c_get_clientdata(client);
|
2022-01-18 07:26:25 +00:00
|
|
|
int ret_count;
|
2012-11-12 14:42:59 +00:00
|
|
|
|
|
|
|
if (report_type == HID_OUTPUT_REPORT)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2022-01-18 07:26:18 +00:00
|
|
|
/*
|
|
|
|
* In case of unnumbered reports the response from the device will
|
|
|
|
* not have the report ID that the upper layers expect, so we need
|
|
|
|
* to stash it the buffer ourselves and adjust the data size.
|
|
|
|
*/
|
2022-01-18 07:26:25 +00:00
|
|
|
if (!report_id) {
|
2022-01-18 07:26:18 +00:00
|
|
|
buf[0] = 0;
|
|
|
|
buf++;
|
|
|
|
count--;
|
|
|
|
}
|
|
|
|
|
2022-01-18 07:26:25 +00:00
|
|
|
ret_count = i2c_hid_get_report(ihid,
|
2012-11-12 14:42:59 +00:00
|
|
|
report_type == HID_FEATURE_REPORT ? 0x03 : 0x01,
|
2022-01-18 07:26:25 +00:00
|
|
|
report_id, buf, count);
|
2012-11-12 14:42:59 +00:00
|
|
|
|
2022-01-18 07:26:25 +00:00
|
|
|
if (ret_count > 0 && !report_id)
|
|
|
|
ret_count++;
|
2022-01-18 07:26:18 +00:00
|
|
|
|
2022-01-18 07:26:25 +00:00
|
|
|
return ret_count;
|
2012-11-12 14:42:59 +00:00
|
|
|
}
|
|
|
|
|
2022-01-18 07:26:25 +00:00
|
|
|
static int i2c_hid_output_raw_report(struct hid_device *hid, u8 report_type,
|
|
|
|
const u8 *buf, size_t count, bool do_set)
|
2012-11-12 14:42:59 +00:00
|
|
|
{
|
|
|
|
struct i2c_client *client = hid->driver_data;
|
2015-12-21 13:26:31 +00:00
|
|
|
struct i2c_hid *ihid = i2c_get_clientdata(client);
|
2012-11-12 14:42:59 +00:00
|
|
|
int report_id = buf[0];
|
2013-01-31 16:50:02 +00:00
|
|
|
int ret;
|
2012-11-12 14:42:59 +00:00
|
|
|
|
|
|
|
if (report_type == HID_INPUT_REPORT)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2015-12-21 13:26:31 +00:00
|
|
|
mutex_lock(&ihid->reset_lock);
|
|
|
|
|
2022-01-18 07:26:18 +00:00
|
|
|
/*
|
|
|
|
* Note that both numbered and unnumbered reports passed here
|
|
|
|
* are supposed to have report ID stored in the 1st byte of the
|
|
|
|
* buffer, so we strip it off unconditionally before passing payload
|
|
|
|
* to i2c_hid_set_or_send_report which takes care of encoding
|
|
|
|
* everything properly.
|
|
|
|
*/
|
2022-01-18 07:26:19 +00:00
|
|
|
ret = i2c_hid_set_or_send_report(ihid,
|
2012-11-12 14:42:59 +00:00
|
|
|
report_type == HID_FEATURE_REPORT ? 0x03 : 0x02,
|
2022-01-18 07:26:21 +00:00
|
|
|
report_id, buf + 1, count - 1, do_set);
|
2013-01-31 16:50:02 +00:00
|
|
|
|
2022-01-18 07:26:18 +00:00
|
|
|
if (ret >= 0)
|
|
|
|
ret++; /* add report_id to the number of transferred bytes */
|
2013-01-31 16:50:02 +00:00
|
|
|
|
2015-12-21 13:26:31 +00:00
|
|
|
mutex_unlock(&ihid->reset_lock);
|
|
|
|
|
2013-01-31 16:50:02 +00:00
|
|
|
return ret;
|
2012-11-12 14:42:59 +00:00
|
|
|
}
|
|
|
|
|
2022-01-18 07:26:21 +00:00
|
|
|
static int i2c_hid_output_report(struct hid_device *hid, u8 *buf, size_t count)
|
2013-02-25 10:31:50 +00:00
|
|
|
{
|
2022-01-18 07:26:25 +00:00
|
|
|
return i2c_hid_output_raw_report(hid, HID_OUTPUT_REPORT, buf, count,
|
2022-01-18 07:26:21 +00:00
|
|
|
false);
|
2014-02-10 17:58:49 +00:00
|
|
|
}
|
2013-02-25 10:31:50 +00:00
|
|
|
|
2014-02-10 17:58:49 +00:00
|
|
|
static int i2c_hid_raw_request(struct hid_device *hid, unsigned char reportnum,
|
|
|
|
__u8 *buf, size_t len, unsigned char rtype,
|
|
|
|
int reqtype)
|
|
|
|
{
|
2013-02-25 10:31:50 +00:00
|
|
|
switch (reqtype) {
|
|
|
|
case HID_REQ_GET_REPORT:
|
2022-01-18 07:26:25 +00:00
|
|
|
return i2c_hid_get_raw_report(hid, rtype, reportnum, buf, len);
|
2013-02-25 10:31:50 +00:00
|
|
|
case HID_REQ_SET_REPORT:
|
2014-02-10 17:58:49 +00:00
|
|
|
if (buf[0] != reportnum)
|
|
|
|
return -EINVAL;
|
2022-01-18 07:26:25 +00:00
|
|
|
return i2c_hid_output_raw_report(hid, rtype, buf, len, true);
|
2014-02-10 17:58:49 +00:00
|
|
|
default:
|
|
|
|
return -EIO;
|
2013-02-25 10:31:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-12 14:42:59 +00:00
|
|
|
static int i2c_hid_parse(struct hid_device *hid)
|
|
|
|
{
|
|
|
|
struct i2c_client *client = hid->driver_data;
|
|
|
|
struct i2c_hid *ihid = i2c_get_clientdata(client);
|
|
|
|
struct i2c_hid_desc *hdesc = &ihid->hdesc;
|
|
|
|
unsigned int rsize;
|
|
|
|
char *rdesc;
|
|
|
|
int ret;
|
|
|
|
int tries = 3;
|
2018-09-19 09:46:23 +00:00
|
|
|
char *use_override;
|
2012-11-12 14:42:59 +00:00
|
|
|
|
|
|
|
i2c_hid_dbg(ihid, "entering %s\n", __func__);
|
|
|
|
|
|
|
|
rsize = le16_to_cpu(hdesc->wReportDescLength);
|
|
|
|
if (!rsize || rsize > HID_MAX_DESCRIPTOR_SIZE) {
|
|
|
|
dbg_hid("weird size of report descriptor (%u)\n", rsize);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
2022-01-18 07:26:19 +00:00
|
|
|
ret = i2c_hid_hwreset(ihid);
|
2012-11-12 14:42:59 +00:00
|
|
|
if (ret)
|
|
|
|
msleep(1000);
|
|
|
|
} while (tries-- > 0 && ret);
|
|
|
|
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
2018-09-19 09:46:23 +00:00
|
|
|
use_override = i2c_hid_get_dmi_hid_report_desc_override(client->name,
|
|
|
|
&rsize);
|
2012-11-12 14:42:59 +00:00
|
|
|
|
2018-09-19 09:46:23 +00:00
|
|
|
if (use_override) {
|
|
|
|
rdesc = use_override;
|
|
|
|
i2c_hid_dbg(ihid, "Using a HID report descriptor override\n");
|
|
|
|
} else {
|
|
|
|
rdesc = kzalloc(rsize, GFP_KERNEL);
|
|
|
|
|
|
|
|
if (!rdesc) {
|
|
|
|
dbg_hid("couldn't allocate rdesc memory\n");
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
i2c_hid_dbg(ihid, "asking HID report descriptor\n");
|
|
|
|
|
2022-01-18 07:26:22 +00:00
|
|
|
ret = i2c_hid_read_register(ihid,
|
|
|
|
ihid->hdesc.wReportDescRegister,
|
|
|
|
rdesc, rsize);
|
2018-09-19 09:46:23 +00:00
|
|
|
if (ret) {
|
|
|
|
hid_err(hid, "reading report descriptor failed\n");
|
|
|
|
kfree(rdesc);
|
|
|
|
return -EIO;
|
|
|
|
}
|
2012-11-12 14:42:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
i2c_hid_dbg(ihid, "Report Descriptor: %*ph\n", rsize, rdesc);
|
|
|
|
|
|
|
|
ret = hid_parse_report(hid, rdesc, rsize);
|
2018-09-19 09:46:23 +00:00
|
|
|
if (!use_override)
|
|
|
|
kfree(rdesc);
|
|
|
|
|
2012-11-12 14:42:59 +00:00
|
|
|
if (ret) {
|
|
|
|
dbg_hid("parsing report descriptor failed\n");
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int i2c_hid_start(struct hid_device *hid)
|
|
|
|
{
|
|
|
|
struct i2c_client *client = hid->driver_data;
|
|
|
|
struct i2c_hid *ihid = i2c_get_clientdata(client);
|
|
|
|
int ret;
|
2012-12-05 14:02:54 +00:00
|
|
|
unsigned int bufsize = HID_MIN_BUFFER_SIZE;
|
2012-11-12 14:42:59 +00:00
|
|
|
|
2012-12-05 14:02:54 +00:00
|
|
|
i2c_hid_find_max_report(hid, HID_INPUT_REPORT, &bufsize);
|
|
|
|
i2c_hid_find_max_report(hid, HID_OUTPUT_REPORT, &bufsize);
|
|
|
|
i2c_hid_find_max_report(hid, HID_FEATURE_REPORT, &bufsize);
|
2012-11-12 14:42:59 +00:00
|
|
|
|
2012-12-05 14:02:54 +00:00
|
|
|
if (bufsize > ihid->bufsize) {
|
2016-12-10 20:58:55 +00:00
|
|
|
disable_irq(client->irq);
|
2012-11-12 14:42:59 +00:00
|
|
|
i2c_hid_free_buffers(ihid);
|
|
|
|
|
2012-12-05 14:02:54 +00:00
|
|
|
ret = i2c_hid_alloc_buffers(ihid, bufsize);
|
2016-12-10 20:58:55 +00:00
|
|
|
enable_irq(client->irq);
|
2012-11-12 14:42:59 +00:00
|
|
|
|
2012-12-05 14:02:54 +00:00
|
|
|
if (ret)
|
2012-11-12 14:42:59 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void i2c_hid_stop(struct hid_device *hid)
|
|
|
|
{
|
|
|
|
hid->claimed = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int i2c_hid_open(struct hid_device *hid)
|
|
|
|
{
|
|
|
|
struct i2c_client *client = hid->driver_data;
|
|
|
|
struct i2c_hid *ihid = i2c_get_clientdata(client);
|
2017-06-07 06:59:35 +00:00
|
|
|
|
|
|
|
set_bit(I2C_HID_STARTED, &ihid->flags);
|
|
|
|
return 0;
|
2012-11-12 14:42:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void i2c_hid_close(struct hid_device *hid)
|
|
|
|
{
|
|
|
|
struct i2c_client *client = hid->driver_data;
|
|
|
|
struct i2c_hid *ihid = i2c_get_clientdata(client);
|
|
|
|
|
2017-06-07 06:59:35 +00:00
|
|
|
clear_bit(I2C_HID_STARTED, &ihid->flags);
|
2012-11-12 14:42:59 +00:00
|
|
|
}
|
|
|
|
|
2022-12-22 05:10:51 +00:00
|
|
|
static const struct hid_ll_driver i2c_hid_ll_driver = {
|
2012-11-12 14:42:59 +00:00
|
|
|
.parse = i2c_hid_parse,
|
|
|
|
.start = i2c_hid_start,
|
|
|
|
.stop = i2c_hid_stop,
|
|
|
|
.open = i2c_hid_open,
|
|
|
|
.close = i2c_hid_close,
|
2014-02-10 17:58:49 +00:00
|
|
|
.output_report = i2c_hid_output_report,
|
|
|
|
.raw_request = i2c_hid_raw_request,
|
2012-11-12 14:42:59 +00:00
|
|
|
};
|
|
|
|
|
2012-12-21 23:14:44 +00:00
|
|
|
static int i2c_hid_init_irq(struct i2c_client *client)
|
2012-11-12 14:42:59 +00:00
|
|
|
{
|
|
|
|
struct i2c_hid *ihid = i2c_get_clientdata(client);
|
2016-11-21 14:21:27 +00:00
|
|
|
unsigned long irqflags = 0;
|
2012-11-12 14:42:59 +00:00
|
|
|
int ret;
|
|
|
|
|
2022-12-23 02:53:35 +00:00
|
|
|
i2c_hid_dbg(ihid, "Requesting IRQ: %d\n", client->irq);
|
2012-11-12 14:42:59 +00:00
|
|
|
|
2016-11-21 14:21:27 +00:00
|
|
|
if (!irq_get_trigger_type(client->irq))
|
|
|
|
irqflags = IRQF_TRIGGER_LOW;
|
|
|
|
|
2016-10-13 09:30:44 +00:00
|
|
|
ret = request_threaded_irq(client->irq, NULL, i2c_hid_irq,
|
2016-11-21 14:21:27 +00:00
|
|
|
irqflags | IRQF_ONESHOT, client->name, ihid);
|
2012-11-12 14:42:59 +00:00
|
|
|
if (ret < 0) {
|
2012-12-04 15:27:49 +00:00
|
|
|
dev_warn(&client->dev,
|
2012-11-12 14:42:59 +00:00
|
|
|
"Could not register for %s interrupt, irq = %d,"
|
|
|
|
" ret = %d\n",
|
2016-10-13 09:30:44 +00:00
|
|
|
client->name, client->irq, ret);
|
2012-11-12 14:42:59 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-12-21 23:14:44 +00:00
|
|
|
static int i2c_hid_fetch_hid_descriptor(struct i2c_hid *ihid)
|
2012-11-12 14:42:59 +00:00
|
|
|
{
|
|
|
|
struct i2c_client *client = ihid->client;
|
|
|
|
struct i2c_hid_desc *hdesc = &ihid->hdesc;
|
|
|
|
unsigned int dsize;
|
2022-01-18 07:26:22 +00:00
|
|
|
int error;
|
2012-11-12 14:42:59 +00:00
|
|
|
|
2014-05-08 13:26:19 +00:00
|
|
|
/* i2c hid fetch using a fixed descriptor size (30 bytes) */
|
2018-09-19 09:46:23 +00:00
|
|
|
if (i2c_hid_get_dmi_i2c_hid_desc_override(client->name)) {
|
|
|
|
i2c_hid_dbg(ihid, "Using a HID descriptor override\n");
|
|
|
|
ihid->hdesc =
|
|
|
|
*i2c_hid_get_dmi_i2c_hid_desc_override(client->name);
|
|
|
|
} else {
|
|
|
|
i2c_hid_dbg(ihid, "Fetching the HID descriptor\n");
|
2022-01-18 07:26:22 +00:00
|
|
|
error = i2c_hid_read_register(ihid,
|
|
|
|
ihid->wHIDDescRegister,
|
|
|
|
&ihid->hdesc,
|
|
|
|
sizeof(ihid->hdesc));
|
|
|
|
if (error) {
|
|
|
|
dev_err(&ihid->client->dev,
|
|
|
|
"failed to fetch HID descriptor: %d\n",
|
|
|
|
error);
|
2018-09-19 09:46:23 +00:00
|
|
|
return -ENODEV;
|
|
|
|
}
|
2012-11-12 14:42:59 +00:00
|
|
|
}
|
|
|
|
|
2014-05-08 13:26:19 +00:00
|
|
|
/* Validate the length of HID descriptor, the 4 first bytes:
|
|
|
|
* bytes 0-1 -> length
|
|
|
|
* bytes 2-3 -> bcdVersion (has to be 1.00) */
|
2012-11-12 14:42:59 +00:00
|
|
|
/* check bcdVersion == 1.0 */
|
|
|
|
if (le16_to_cpu(hdesc->bcdVersion) != 0x0100) {
|
2022-01-18 07:26:19 +00:00
|
|
|
dev_err(&ihid->client->dev,
|
2012-12-04 15:27:49 +00:00
|
|
|
"unexpected HID descriptor bcdVersion (0x%04hx)\n",
|
2012-11-12 14:42:59 +00:00
|
|
|
le16_to_cpu(hdesc->bcdVersion));
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
|
2014-05-08 13:26:19 +00:00
|
|
|
/* Descriptor length should be 30 bytes as per the specification */
|
|
|
|
dsize = le16_to_cpu(hdesc->wHIDDescLength);
|
|
|
|
if (dsize != sizeof(struct i2c_hid_desc)) {
|
2022-01-18 07:26:19 +00:00
|
|
|
dev_err(&ihid->client->dev,
|
|
|
|
"weird size of HID descriptor (%u)\n", dsize);
|
2012-11-12 14:42:59 +00:00
|
|
|
return -ENODEV;
|
|
|
|
}
|
2022-01-18 07:26:27 +00:00
|
|
|
i2c_hid_dbg(ihid, "HID Descriptor: %*ph\n", dsize, &ihid->hdesc);
|
2012-11-12 14:42:59 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-01-15 17:06:37 +00:00
|
|
|
static int i2c_hid_core_power_up(struct i2c_hid *ihid)
|
2020-10-26 15:46:06 +00:00
|
|
|
{
|
2021-01-15 17:06:37 +00:00
|
|
|
if (!ihid->ops->power_up)
|
|
|
|
return 0;
|
2020-10-26 15:46:06 +00:00
|
|
|
|
2021-01-15 17:06:37 +00:00
|
|
|
return ihid->ops->power_up(ihid->ops);
|
2013-01-09 14:43:08 +00:00
|
|
|
}
|
2017-05-09 08:04:36 +00:00
|
|
|
|
2021-01-15 17:06:37 +00:00
|
|
|
static void i2c_hid_core_power_down(struct i2c_hid *ihid)
|
2013-06-13 07:50:35 +00:00
|
|
|
{
|
2021-01-15 17:06:37 +00:00
|
|
|
if (!ihid->ops->power_down)
|
|
|
|
return;
|
2013-06-13 07:50:35 +00:00
|
|
|
|
2021-01-15 17:06:37 +00:00
|
|
|
ihid->ops->power_down(ihid->ops);
|
2013-06-13 07:50:35 +00:00
|
|
|
}
|
|
|
|
|
2021-01-15 17:06:37 +00:00
|
|
|
static void i2c_hid_core_shutdown_tail(struct i2c_hid *ihid)
|
2017-10-03 18:19:21 +00:00
|
|
|
{
|
2021-01-15 17:06:37 +00:00
|
|
|
if (!ihid->ops->shutdown_tail)
|
|
|
|
return;
|
2017-10-03 18:19:21 +00:00
|
|
|
|
2021-01-15 17:06:37 +00:00
|
|
|
ihid->ops->shutdown_tail(ihid->ops);
|
2017-10-03 18:19:21 +00:00
|
|
|
}
|
|
|
|
|
2021-01-15 17:06:37 +00:00
|
|
|
int i2c_hid_core_probe(struct i2c_client *client, struct i2chid_ops *ops,
|
2021-12-08 12:40:44 +00:00
|
|
|
u16 hid_descriptor_address, u32 quirks)
|
2012-11-12 14:42:59 +00:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
struct i2c_hid *ihid;
|
|
|
|
struct hid_device *hid;
|
|
|
|
|
|
|
|
dbg_hid("HID probe called for i2c 0x%02x\n", client->addr);
|
|
|
|
|
2016-10-13 09:30:44 +00:00
|
|
|
if (!client->irq) {
|
|
|
|
dev_err(&client->dev,
|
|
|
|
"HID over i2c has not been provided an Int IRQ\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2016-10-13 09:30:45 +00:00
|
|
|
if (client->irq < 0) {
|
|
|
|
if (client->irq != -EPROBE_DEFER)
|
|
|
|
dev_err(&client->dev,
|
|
|
|
"HID over i2c doesn't have a valid IRQ\n");
|
|
|
|
return client->irq;
|
|
|
|
}
|
|
|
|
|
2018-06-22 02:27:16 +00:00
|
|
|
ihid = devm_kzalloc(&client->dev, sizeof(*ihid), GFP_KERNEL);
|
2012-11-12 14:42:59 +00:00
|
|
|
if (!ihid)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2021-01-15 17:06:37 +00:00
|
|
|
ihid->ops = ops;
|
2018-06-22 02:27:17 +00:00
|
|
|
|
2021-01-15 17:06:37 +00:00
|
|
|
ret = i2c_hid_core_power_up(ihid);
|
2018-06-22 02:27:17 +00:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
2012-11-12 14:42:59 +00:00
|
|
|
i2c_set_clientdata(client, ihid);
|
|
|
|
|
|
|
|
ihid->client = client;
|
|
|
|
|
2021-01-15 17:06:37 +00:00
|
|
|
ihid->wHIDDescRegister = cpu_to_le16(hid_descriptor_address);
|
2012-11-12 14:42:59 +00:00
|
|
|
|
|
|
|
init_waitqueue_head(&ihid->wait);
|
2015-12-21 13:26:31 +00:00
|
|
|
mutex_init(&ihid->reset_lock);
|
2012-11-12 14:42:59 +00:00
|
|
|
|
|
|
|
/* we need to allocate the command buffer without knowing the maximum
|
|
|
|
* size of the reports. Let's use HID_MIN_BUFFER_SIZE, then we do the
|
|
|
|
* real computation later. */
|
2012-12-05 14:02:54 +00:00
|
|
|
ret = i2c_hid_alloc_buffers(ihid, HID_MIN_BUFFER_SIZE);
|
|
|
|
if (ret < 0)
|
2021-01-15 17:06:37 +00:00
|
|
|
goto err_powered;
|
2020-07-09 07:57:29 +00:00
|
|
|
|
2016-05-19 02:46:24 +00:00
|
|
|
device_enable_async_suspend(&client->dev);
|
2014-01-29 09:24:36 +00:00
|
|
|
|
2018-05-09 19:12:15 +00:00
|
|
|
/* Make sure there is something at this address */
|
|
|
|
ret = i2c_smbus_read_byte(client);
|
|
|
|
if (ret < 0) {
|
2022-12-23 02:53:35 +00:00
|
|
|
i2c_hid_dbg(ihid, "nothing at this address: %d\n", ret);
|
2018-05-09 19:12:15 +00:00
|
|
|
ret = -ENXIO;
|
2021-01-15 17:06:37 +00:00
|
|
|
goto err_powered;
|
2018-05-09 19:12:15 +00:00
|
|
|
}
|
|
|
|
|
2012-11-12 14:42:59 +00:00
|
|
|
ret = i2c_hid_fetch_hid_descriptor(ihid);
|
2020-10-21 14:33:05 +00:00
|
|
|
if (ret < 0) {
|
|
|
|
dev_err(&client->dev,
|
|
|
|
"Failed to fetch the HID Descriptor\n");
|
2021-01-15 17:06:37 +00:00
|
|
|
goto err_powered;
|
2020-10-21 14:33:05 +00:00
|
|
|
}
|
2012-11-12 14:42:59 +00:00
|
|
|
|
|
|
|
ret = i2c_hid_init_irq(client);
|
|
|
|
if (ret < 0)
|
2021-01-15 17:06:37 +00:00
|
|
|
goto err_powered;
|
2012-11-12 14:42:59 +00:00
|
|
|
|
|
|
|
hid = hid_allocate_device();
|
|
|
|
if (IS_ERR(hid)) {
|
|
|
|
ret = PTR_ERR(hid);
|
2012-12-05 14:02:55 +00:00
|
|
|
goto err_irq;
|
2012-11-12 14:42:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ihid->hid = hid;
|
|
|
|
|
|
|
|
hid->driver_data = client;
|
|
|
|
hid->ll_driver = &i2c_hid_ll_driver;
|
|
|
|
hid->dev.parent = &client->dev;
|
|
|
|
hid->bus = BUS_I2C;
|
|
|
|
hid->version = le16_to_cpu(ihid->hdesc.bcdVersion);
|
|
|
|
hid->vendor = le16_to_cpu(ihid->hdesc.wVendorID);
|
|
|
|
hid->product = le16_to_cpu(ihid->hdesc.wProductID);
|
|
|
|
|
2023-02-07 23:03:30 +00:00
|
|
|
hid->initial_quirks = quirks;
|
|
|
|
hid->initial_quirks |= i2c_hid_get_dmi_quirks(hid->vendor,
|
|
|
|
hid->product);
|
|
|
|
|
2021-05-14 13:58:50 +00:00
|
|
|
snprintf(hid->name, sizeof(hid->name), "%s %04X:%04X",
|
|
|
|
client->name, (u16)hid->vendor, (u16)hid->product);
|
2022-08-18 21:00:09 +00:00
|
|
|
strscpy(hid->phys, dev_name(&client->dev), sizeof(hid->phys));
|
2012-11-12 14:42:59 +00:00
|
|
|
|
2016-11-10 03:47:13 +00:00
|
|
|
ihid->quirks = i2c_hid_lookup_quirk(hid->vendor, hid->product);
|
|
|
|
|
2012-11-12 14:42:59 +00:00
|
|
|
ret = hid_add_device(hid);
|
|
|
|
if (ret) {
|
|
|
|
if (ret != -ENODEV)
|
|
|
|
hid_err(client, "can't add hid device: %d\n", ret);
|
|
|
|
goto err_mem_free;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
err_mem_free:
|
|
|
|
hid_destroy_device(hid);
|
|
|
|
|
2012-12-05 14:02:55 +00:00
|
|
|
err_irq:
|
2016-10-13 09:30:44 +00:00
|
|
|
free_irq(client->irq, ihid);
|
2012-11-12 14:42:59 +00:00
|
|
|
|
2021-01-15 17:06:37 +00:00
|
|
|
err_powered:
|
|
|
|
i2c_hid_core_power_down(ihid);
|
2012-11-20 16:09:40 +00:00
|
|
|
i2c_hid_free_buffers(ihid);
|
2012-11-12 14:42:59 +00:00
|
|
|
return ret;
|
|
|
|
}
|
2021-01-15 17:06:37 +00:00
|
|
|
EXPORT_SYMBOL_GPL(i2c_hid_core_probe);
|
2012-11-12 14:42:59 +00:00
|
|
|
|
2022-08-15 08:02:30 +00:00
|
|
|
void i2c_hid_core_remove(struct i2c_client *client)
|
2012-11-12 14:42:59 +00:00
|
|
|
{
|
|
|
|
struct i2c_hid *ihid = i2c_get_clientdata(client);
|
|
|
|
struct hid_device *hid;
|
|
|
|
|
|
|
|
hid = ihid->hid;
|
|
|
|
hid_destroy_device(hid);
|
|
|
|
|
2016-10-13 09:30:44 +00:00
|
|
|
free_irq(client->irq, ihid);
|
2012-11-12 14:42:59 +00:00
|
|
|
|
2012-12-04 15:27:54 +00:00
|
|
|
if (ihid->bufsize)
|
|
|
|
i2c_hid_free_buffers(ihid);
|
|
|
|
|
2021-01-15 17:06:37 +00:00
|
|
|
i2c_hid_core_power_down(ihid);
|
2012-11-12 14:42:59 +00:00
|
|
|
}
|
2021-01-15 17:06:37 +00:00
|
|
|
EXPORT_SYMBOL_GPL(i2c_hid_core_remove);
|
2012-11-12 14:42:59 +00:00
|
|
|
|
2021-01-15 17:06:37 +00:00
|
|
|
void i2c_hid_core_shutdown(struct i2c_client *client)
|
2016-06-21 10:27:45 +00:00
|
|
|
{
|
|
|
|
struct i2c_hid *ihid = i2c_get_clientdata(client);
|
|
|
|
|
2022-01-18 07:26:19 +00:00
|
|
|
i2c_hid_set_power(ihid, I2C_HID_PWR_SLEEP);
|
2016-06-21 10:27:45 +00:00
|
|
|
free_irq(client->irq, ihid);
|
2020-10-26 15:46:06 +00:00
|
|
|
|
2021-01-15 17:06:37 +00:00
|
|
|
i2c_hid_core_shutdown_tail(ihid);
|
2016-06-21 10:27:45 +00:00
|
|
|
}
|
2021-01-15 17:06:37 +00:00
|
|
|
EXPORT_SYMBOL_GPL(i2c_hid_core_shutdown);
|
2016-06-21 10:27:45 +00:00
|
|
|
|
2012-11-12 14:42:59 +00:00
|
|
|
#ifdef CONFIG_PM_SLEEP
|
2021-01-15 17:06:37 +00:00
|
|
|
static int i2c_hid_core_suspend(struct device *dev)
|
2012-11-12 14:42:59 +00:00
|
|
|
{
|
|
|
|
struct i2c_client *client = to_i2c_client(dev);
|
2014-07-11 23:34:18 +00:00
|
|
|
struct i2c_hid *ihid = i2c_get_clientdata(client);
|
|
|
|
struct hid_device *hid = ihid->hid;
|
2016-03-08 23:03:23 +00:00
|
|
|
int ret;
|
2012-11-12 14:42:59 +00:00
|
|
|
|
2021-12-02 09:53:33 +00:00
|
|
|
ret = hid_driver_suspend(hid, PMSG_SUSPEND);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2016-03-08 23:03:23 +00:00
|
|
|
|
2019-10-16 15:12:24 +00:00
|
|
|
/* Save some power */
|
2022-01-18 07:26:19 +00:00
|
|
|
i2c_hid_set_power(ihid, I2C_HID_PWR_SLEEP);
|
2016-03-08 23:03:23 +00:00
|
|
|
|
2019-10-16 15:12:24 +00:00
|
|
|
disable_irq(client->irq);
|
2015-07-07 19:58:02 +00:00
|
|
|
|
2022-11-22 21:39:06 +00:00
|
|
|
if (!device_may_wakeup(&client->dev))
|
2021-01-15 17:06:37 +00:00
|
|
|
i2c_hid_core_power_down(ihid);
|
2012-11-12 14:42:59 +00:00
|
|
|
|
2016-03-08 23:03:23 +00:00
|
|
|
return 0;
|
2012-11-12 14:42:59 +00:00
|
|
|
}
|
|
|
|
|
2021-01-15 17:06:37 +00:00
|
|
|
static int i2c_hid_core_resume(struct device *dev)
|
2012-11-12 14:42:59 +00:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
struct i2c_client *client = to_i2c_client(dev);
|
2014-07-11 23:34:18 +00:00
|
|
|
struct i2c_hid *ihid = i2c_get_clientdata(client);
|
|
|
|
struct hid_device *hid = ihid->hid;
|
2012-11-12 14:42:59 +00:00
|
|
|
|
2022-11-22 21:39:06 +00:00
|
|
|
if (!device_may_wakeup(&client->dev))
|
2021-01-15 17:06:37 +00:00
|
|
|
i2c_hid_core_power_up(ihid);
|
2012-11-12 14:42:59 +00:00
|
|
|
|
2016-10-13 09:30:44 +00:00
|
|
|
enable_irq(client->irq);
|
2018-09-06 02:55:18 +00:00
|
|
|
|
|
|
|
/* Instead of resetting device, simply powers the device on. This
|
|
|
|
* solves "incomplete reports" on Raydium devices 2386:3118 and
|
2018-09-19 18:52:45 +00:00
|
|
|
* 2386:4B33 and fixes various SIS touchscreens no longer sending
|
|
|
|
* data after a suspend/resume.
|
2019-11-07 14:28:11 +00:00
|
|
|
*
|
|
|
|
* However some ALPS touchpads generate IRQ storm without reset, so
|
|
|
|
* let's still reset them here.
|
2018-09-06 02:55:18 +00:00
|
|
|
*/
|
2019-11-07 14:28:11 +00:00
|
|
|
if (ihid->quirks & I2C_HID_QUIRK_RESET_ON_RESUME)
|
2022-01-18 07:26:19 +00:00
|
|
|
ret = i2c_hid_hwreset(ihid);
|
2019-11-07 14:28:11 +00:00
|
|
|
else
|
2022-01-18 07:26:19 +00:00
|
|
|
ret = i2c_hid_set_power(ihid, I2C_HID_PWR_ON);
|
2019-11-07 14:28:11 +00:00
|
|
|
|
2016-03-08 23:03:23 +00:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
2021-12-02 09:53:33 +00:00
|
|
|
return hid_driver_reset_resume(hid);
|
2012-11-12 14:42:59 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-01-15 17:06:37 +00:00
|
|
|
const struct dev_pm_ops i2c_hid_core_pm = {
|
|
|
|
SET_SYSTEM_SLEEP_PM_OPS(i2c_hid_core_suspend, i2c_hid_core_resume)
|
2014-01-29 09:24:36 +00:00
|
|
|
};
|
2021-01-15 17:06:37 +00:00
|
|
|
EXPORT_SYMBOL_GPL(i2c_hid_core_pm);
|
2012-11-12 14:42:59 +00:00
|
|
|
|
|
|
|
MODULE_DESCRIPTION("HID over I2C core driver");
|
|
|
|
MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
|
|
|
|
MODULE_LICENSE("GPL");
|