Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 1 | /* |
| 2 | * HID over I2C protocol implementation |
| 3 | * |
| 4 | * Copyright (c) 2012 Benjamin Tissoires <benjamin.tissoires@gmail.com> |
| 5 | * Copyright (c) 2012 Ecole Nationale de l'Aviation Civile, France |
| 6 | * Copyright (c) 2012 Red Hat, Inc |
| 7 | * |
| 8 | * This code is partly based on "USB HID support for Linux": |
| 9 | * |
| 10 | * Copyright (c) 1999 Andreas Gal |
| 11 | * Copyright (c) 2000-2005 Vojtech Pavlik <vojtech@suse.cz> |
| 12 | * Copyright (c) 2005 Michael Haboustak <mike-@cinci.rr.com> for Concept2, Inc |
| 13 | * Copyright (c) 2007-2008 Oliver Neukum |
| 14 | * Copyright (c) 2006-2010 Jiri Kosina |
| 15 | * |
| 16 | * This file is subject to the terms and conditions of the GNU General Public |
| 17 | * License. See the file COPYING in the main directory of this archive for |
| 18 | * more details. |
| 19 | */ |
| 20 | |
| 21 | #include <linux/module.h> |
| 22 | #include <linux/i2c.h> |
| 23 | #include <linux/interrupt.h> |
| 24 | #include <linux/input.h> |
| 25 | #include <linux/delay.h> |
| 26 | #include <linux/slab.h> |
| 27 | #include <linux/pm.h> |
Mika Westerberg | 34f439e | 2014-01-29 11:24:36 +0200 | [diff] [blame] | 28 | #include <linux/pm_runtime.h> |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 29 | #include <linux/device.h> |
| 30 | #include <linux/wait.h> |
| 31 | #include <linux/err.h> |
| 32 | #include <linux/string.h> |
| 33 | #include <linux/list.h> |
| 34 | #include <linux/jiffies.h> |
| 35 | #include <linux/kernel.h> |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 36 | #include <linux/hid.h> |
Benjamin Tissoires | 7a7d6d9 | 2012-12-12 18:00:02 +0100 | [diff] [blame] | 37 | #include <linux/mutex.h> |
Mika Westerberg | 92241e6 | 2013-01-09 16:43:08 +0200 | [diff] [blame] | 38 | #include <linux/acpi.h> |
Benjamin Tissoires | 3d7d248 | 2013-06-13 09:50:35 +0200 | [diff] [blame] | 39 | #include <linux/of.h> |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 40 | |
| 41 | #include <linux/i2c/i2c-hid.h> |
| 42 | |
HungNien Chen | 71af01a | 2016-11-10 11:47:13 +0800 | [diff] [blame^] | 43 | #include "../hid-ids.h" |
| 44 | |
| 45 | /* quirks to control the device */ |
| 46 | #define I2C_HID_QUIRK_SET_PWR_WAKEUP_DEV BIT(0) |
| 47 | |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 48 | /* flags */ |
Dan Carpenter | c8fd51d | 2015-05-14 11:33:30 +0300 | [diff] [blame] | 49 | #define I2C_HID_STARTED 0 |
| 50 | #define I2C_HID_RESET_PENDING 1 |
| 51 | #define I2C_HID_READ_PENDING 2 |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 52 | |
| 53 | #define I2C_HID_PWR_ON 0x00 |
| 54 | #define I2C_HID_PWR_SLEEP 0x01 |
| 55 | |
| 56 | /* debug option */ |
Benjamin Tissoires | ee8e880 | 2012-12-04 16:27:45 +0100 | [diff] [blame] | 57 | static bool debug; |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 58 | module_param(debug, bool, 0444); |
| 59 | MODULE_PARM_DESC(debug, "print a lot of debug information"); |
| 60 | |
Benjamin Tissoires | fa738644 | 2012-12-04 16:27:46 +0100 | [diff] [blame] | 61 | #define i2c_hid_dbg(ihid, fmt, arg...) \ |
| 62 | do { \ |
| 63 | if (debug) \ |
| 64 | dev_printk(KERN_DEBUG, &(ihid)->client->dev, fmt, ##arg); \ |
| 65 | } while (0) |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 66 | |
| 67 | struct i2c_hid_desc { |
| 68 | __le16 wHIDDescLength; |
| 69 | __le16 bcdVersion; |
| 70 | __le16 wReportDescLength; |
| 71 | __le16 wReportDescRegister; |
| 72 | __le16 wInputRegister; |
| 73 | __le16 wMaxInputLength; |
| 74 | __le16 wOutputRegister; |
| 75 | __le16 wMaxOutputLength; |
| 76 | __le16 wCommandRegister; |
| 77 | __le16 wDataRegister; |
| 78 | __le16 wVendorID; |
| 79 | __le16 wProductID; |
| 80 | __le16 wVersionID; |
Benjamin Tissoires | 27174cf | 2012-12-05 15:02:53 +0100 | [diff] [blame] | 81 | __le32 reserved; |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 82 | } __packed; |
| 83 | |
| 84 | struct i2c_hid_cmd { |
| 85 | unsigned int registerIndex; |
| 86 | __u8 opcode; |
| 87 | unsigned int length; |
| 88 | bool wait; |
| 89 | }; |
| 90 | |
| 91 | union command { |
| 92 | u8 data[0]; |
| 93 | struct cmd { |
| 94 | __le16 reg; |
| 95 | __u8 reportTypeID; |
| 96 | __u8 opcode; |
| 97 | } __packed c; |
| 98 | }; |
| 99 | |
| 100 | #define I2C_HID_CMD(opcode_) \ |
| 101 | .opcode = opcode_, .length = 4, \ |
| 102 | .registerIndex = offsetof(struct i2c_hid_desc, wCommandRegister) |
| 103 | |
| 104 | /* fetch HID descriptor */ |
| 105 | static const struct i2c_hid_cmd hid_descr_cmd = { .length = 2 }; |
| 106 | /* fetch report descriptors */ |
| 107 | static const struct i2c_hid_cmd hid_report_descr_cmd = { |
| 108 | .registerIndex = offsetof(struct i2c_hid_desc, |
| 109 | wReportDescRegister), |
| 110 | .opcode = 0x00, |
| 111 | .length = 2 }; |
| 112 | /* commands */ |
| 113 | static const struct i2c_hid_cmd hid_reset_cmd = { I2C_HID_CMD(0x01), |
| 114 | .wait = true }; |
| 115 | static const struct i2c_hid_cmd hid_get_report_cmd = { I2C_HID_CMD(0x02) }; |
| 116 | static const struct i2c_hid_cmd hid_set_report_cmd = { I2C_HID_CMD(0x03) }; |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 117 | static const struct i2c_hid_cmd hid_set_power_cmd = { I2C_HID_CMD(0x08) }; |
Andrew Duggan | 811adb9 | 2013-06-17 16:15:06 -0700 | [diff] [blame] | 118 | static const struct i2c_hid_cmd hid_no_cmd = { .length = 0 }; |
Benjamin Tissoires | 6bf6c8b | 2012-12-04 16:27:47 +0100 | [diff] [blame] | 119 | |
| 120 | /* |
| 121 | * These definitions are not used here, but are defined by the spec. |
| 122 | * Keeping them here for documentation purposes. |
| 123 | * |
| 124 | * static const struct i2c_hid_cmd hid_get_idle_cmd = { I2C_HID_CMD(0x04) }; |
| 125 | * static const struct i2c_hid_cmd hid_set_idle_cmd = { I2C_HID_CMD(0x05) }; |
| 126 | * static const struct i2c_hid_cmd hid_get_protocol_cmd = { I2C_HID_CMD(0x06) }; |
| 127 | * static const struct i2c_hid_cmd hid_set_protocol_cmd = { I2C_HID_CMD(0x07) }; |
| 128 | */ |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 129 | |
Benjamin Tissoires | 7a7d6d9 | 2012-12-12 18:00:02 +0100 | [diff] [blame] | 130 | static DEFINE_MUTEX(i2c_hid_open_mut); |
| 131 | |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 132 | /* The main device structure */ |
| 133 | struct i2c_hid { |
| 134 | struct i2c_client *client; /* i2c client */ |
| 135 | struct hid_device *hid; /* pointer to corresponding HID dev */ |
| 136 | union { |
| 137 | __u8 hdesc_buffer[sizeof(struct i2c_hid_desc)]; |
| 138 | struct i2c_hid_desc hdesc; /* the HID Descriptor */ |
| 139 | }; |
| 140 | __le16 wHIDDescRegister; /* location of the i2c |
| 141 | * register of the HID |
| 142 | * descriptor. */ |
| 143 | unsigned int bufsize; /* i2c buffer size */ |
| 144 | char *inbuf; /* Input buffer */ |
Jean-Baptiste Maneyrol | 6296f4a | 2014-11-20 00:46:37 +0800 | [diff] [blame] | 145 | char *rawbuf; /* Raw Input buffer */ |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 146 | char *cmdbuf; /* Command buffer */ |
| 147 | char *argsbuf; /* Command arguments buffer */ |
| 148 | |
| 149 | unsigned long flags; /* device flags */ |
HungNien Chen | 71af01a | 2016-11-10 11:47:13 +0800 | [diff] [blame^] | 150 | unsigned long quirks; /* Various quirks */ |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 151 | |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 152 | wait_queue_head_t wait; /* For waiting the interrupt */ |
Mika Westerberg | 92241e6 | 2013-01-09 16:43:08 +0200 | [diff] [blame] | 153 | |
| 154 | struct i2c_hid_platform_data pdata; |
Andrew Duggan | d1c4803 | 2015-07-30 14:49:00 -0700 | [diff] [blame] | 155 | |
| 156 | bool irq_wake_enabled; |
Mika Westerberg | 9a32740 | 2015-12-21 15:26:31 +0200 | [diff] [blame] | 157 | struct mutex reset_lock; |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 158 | }; |
| 159 | |
HungNien Chen | 71af01a | 2016-11-10 11:47:13 +0800 | [diff] [blame^] | 160 | static const struct i2c_hid_quirks { |
| 161 | __u16 idVendor; |
| 162 | __u16 idProduct; |
| 163 | __u32 quirks; |
| 164 | } i2c_hid_quirks[] = { |
| 165 | { USB_VENDOR_ID_WEIDA, USB_DEVICE_ID_WEIDA_8752, |
| 166 | I2C_HID_QUIRK_SET_PWR_WAKEUP_DEV }, |
| 167 | { USB_VENDOR_ID_WEIDA, USB_DEVICE_ID_WEIDA_8755, |
| 168 | I2C_HID_QUIRK_SET_PWR_WAKEUP_DEV }, |
| 169 | { 0, 0 } |
| 170 | }; |
| 171 | |
| 172 | /* |
| 173 | * i2c_hid_lookup_quirk: return any quirks associated with a I2C HID device |
| 174 | * @idVendor: the 16-bit vendor ID |
| 175 | * @idProduct: the 16-bit product ID |
| 176 | * |
| 177 | * Returns: a u32 quirks value. |
| 178 | */ |
| 179 | static u32 i2c_hid_lookup_quirk(const u16 idVendor, const u16 idProduct) |
| 180 | { |
| 181 | u32 quirks = 0; |
| 182 | int n; |
| 183 | |
| 184 | for (n = 0; i2c_hid_quirks[n].idVendor; n++) |
| 185 | if (i2c_hid_quirks[n].idVendor == idVendor && |
| 186 | (i2c_hid_quirks[n].idProduct == (__u16)HID_ANY_ID || |
| 187 | i2c_hid_quirks[n].idProduct == idProduct)) |
| 188 | quirks = i2c_hid_quirks[n].quirks; |
| 189 | |
| 190 | return quirks; |
| 191 | } |
| 192 | |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 193 | static int __i2c_hid_command(struct i2c_client *client, |
| 194 | const struct i2c_hid_cmd *command, u8 reportID, |
| 195 | u8 reportType, u8 *args, int args_len, |
| 196 | unsigned char *buf_recv, int data_len) |
| 197 | { |
| 198 | struct i2c_hid *ihid = i2c_get_clientdata(client); |
| 199 | union command *cmd = (union command *)ihid->cmdbuf; |
| 200 | int ret; |
| 201 | struct i2c_msg msg[2]; |
| 202 | int msg_num = 1; |
| 203 | |
| 204 | int length = command->length; |
| 205 | bool wait = command->wait; |
| 206 | unsigned int registerIndex = command->registerIndex; |
| 207 | |
| 208 | /* special case for hid_descr_cmd */ |
| 209 | if (command == &hid_descr_cmd) { |
| 210 | cmd->c.reg = ihid->wHIDDescRegister; |
| 211 | } else { |
| 212 | cmd->data[0] = ihid->hdesc_buffer[registerIndex]; |
| 213 | cmd->data[1] = ihid->hdesc_buffer[registerIndex + 1]; |
| 214 | } |
| 215 | |
| 216 | if (length > 2) { |
| 217 | cmd->c.opcode = command->opcode; |
| 218 | cmd->c.reportTypeID = reportID | reportType << 4; |
| 219 | } |
| 220 | |
| 221 | memcpy(cmd->data + length, args, args_len); |
| 222 | length += args_len; |
| 223 | |
| 224 | i2c_hid_dbg(ihid, "%s: cmd=%*ph\n", __func__, length, cmd->data); |
| 225 | |
| 226 | msg[0].addr = client->addr; |
| 227 | msg[0].flags = client->flags & I2C_M_TEN; |
| 228 | msg[0].len = length; |
| 229 | msg[0].buf = cmd->data; |
| 230 | if (data_len > 0) { |
| 231 | msg[1].addr = client->addr; |
| 232 | msg[1].flags = client->flags & I2C_M_TEN; |
| 233 | msg[1].flags |= I2C_M_RD; |
| 234 | msg[1].len = data_len; |
| 235 | msg[1].buf = buf_recv; |
| 236 | msg_num = 2; |
| 237 | set_bit(I2C_HID_READ_PENDING, &ihid->flags); |
| 238 | } |
| 239 | |
| 240 | if (wait) |
| 241 | set_bit(I2C_HID_RESET_PENDING, &ihid->flags); |
| 242 | |
| 243 | ret = i2c_transfer(client->adapter, msg, msg_num); |
| 244 | |
| 245 | if (data_len > 0) |
| 246 | clear_bit(I2C_HID_READ_PENDING, &ihid->flags); |
| 247 | |
| 248 | if (ret != msg_num) |
| 249 | return ret < 0 ? ret : -EIO; |
| 250 | |
| 251 | ret = 0; |
| 252 | |
| 253 | if (wait) { |
| 254 | i2c_hid_dbg(ihid, "%s: waiting...\n", __func__); |
| 255 | if (!wait_event_timeout(ihid->wait, |
| 256 | !test_bit(I2C_HID_RESET_PENDING, &ihid->flags), |
| 257 | msecs_to_jiffies(5000))) |
| 258 | ret = -ENODATA; |
| 259 | i2c_hid_dbg(ihid, "%s: finished.\n", __func__); |
| 260 | } |
| 261 | |
| 262 | return ret; |
| 263 | } |
| 264 | |
| 265 | static int i2c_hid_command(struct i2c_client *client, |
| 266 | const struct i2c_hid_cmd *command, |
| 267 | unsigned char *buf_recv, int data_len) |
| 268 | { |
| 269 | return __i2c_hid_command(client, command, 0, 0, NULL, 0, |
| 270 | buf_recv, data_len); |
| 271 | } |
| 272 | |
| 273 | static int i2c_hid_get_report(struct i2c_client *client, u8 reportType, |
| 274 | u8 reportID, unsigned char *buf_recv, int data_len) |
| 275 | { |
| 276 | struct i2c_hid *ihid = i2c_get_clientdata(client); |
| 277 | u8 args[3]; |
| 278 | int ret; |
| 279 | int args_len = 0; |
| 280 | u16 readRegister = le16_to_cpu(ihid->hdesc.wDataRegister); |
| 281 | |
| 282 | i2c_hid_dbg(ihid, "%s\n", __func__); |
| 283 | |
| 284 | if (reportID >= 0x0F) { |
| 285 | args[args_len++] = reportID; |
| 286 | reportID = 0x0F; |
| 287 | } |
| 288 | |
| 289 | args[args_len++] = readRegister & 0xFF; |
| 290 | args[args_len++] = readRegister >> 8; |
| 291 | |
| 292 | ret = __i2c_hid_command(client, &hid_get_report_cmd, reportID, |
| 293 | reportType, args, args_len, buf_recv, data_len); |
| 294 | if (ret) { |
| 295 | dev_err(&client->dev, |
| 296 | "failed to retrieve report from device.\n"); |
Benjamin Tissoires | 317b204 | 2012-12-04 16:27:48 +0100 | [diff] [blame] | 297 | return ret; |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | return 0; |
| 301 | } |
| 302 | |
Benjamin Tissoires | 9b5a9ae | 2014-02-10 12:58:49 -0500 | [diff] [blame] | 303 | /** |
| 304 | * i2c_hid_set_or_send_report: forward an incoming report to the device |
| 305 | * @client: the i2c_client of the device |
| 306 | * @reportType: 0x03 for HID_FEATURE_REPORT ; 0x02 for HID_OUTPUT_REPORT |
| 307 | * @reportID: the report ID |
| 308 | * @buf: the actual data to transfer, without the report ID |
| 309 | * @len: size of buf |
| 310 | * @use_data: true: use SET_REPORT HID command, false: send plain OUTPUT report |
| 311 | */ |
| 312 | static int i2c_hid_set_or_send_report(struct i2c_client *client, u8 reportType, |
| 313 | u8 reportID, unsigned char *buf, size_t data_len, bool use_data) |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 314 | { |
| 315 | struct i2c_hid *ihid = i2c_get_clientdata(client); |
| 316 | u8 *args = ihid->argsbuf; |
Benjamin Tissoires | 9b5a9ae | 2014-02-10 12:58:49 -0500 | [diff] [blame] | 317 | const struct i2c_hid_cmd *hidcmd; |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 318 | int ret; |
| 319 | u16 dataRegister = le16_to_cpu(ihid->hdesc.wDataRegister); |
Andrew Duggan | 811adb9 | 2013-06-17 16:15:06 -0700 | [diff] [blame] | 320 | u16 outputRegister = le16_to_cpu(ihid->hdesc.wOutputRegister); |
| 321 | u16 maxOutputLength = le16_to_cpu(ihid->hdesc.wMaxOutputLength); |
Dmitry Torokhov | 3b65428 | 2016-03-14 15:21:04 -0700 | [diff] [blame] | 322 | u16 size; |
| 323 | int args_len; |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 324 | int index = 0; |
| 325 | |
| 326 | i2c_hid_dbg(ihid, "%s\n", __func__); |
| 327 | |
Dmitry Torokhov | 3b65428 | 2016-03-14 15:21:04 -0700 | [diff] [blame] | 328 | if (data_len > ihid->bufsize) |
| 329 | return -EINVAL; |
| 330 | |
| 331 | size = 2 /* size */ + |
| 332 | (reportID ? 1 : 0) /* reportID */ + |
| 333 | data_len /* buf */; |
| 334 | args_len = (reportID >= 0x0F ? 1 : 0) /* optional third byte */ + |
| 335 | 2 /* dataRegister */ + |
| 336 | size /* args */; |
| 337 | |
Benjamin Tissoires | 9b5a9ae | 2014-02-10 12:58:49 -0500 | [diff] [blame] | 338 | if (!use_data && maxOutputLength == 0) |
| 339 | return -ENOSYS; |
| 340 | |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 341 | if (reportID >= 0x0F) { |
| 342 | args[index++] = reportID; |
| 343 | reportID = 0x0F; |
| 344 | } |
| 345 | |
Andrew Duggan | 811adb9 | 2013-06-17 16:15:06 -0700 | [diff] [blame] | 346 | /* |
| 347 | * use the data register for feature reports or if the device does not |
| 348 | * support the output register |
| 349 | */ |
Benjamin Tissoires | 9b5a9ae | 2014-02-10 12:58:49 -0500 | [diff] [blame] | 350 | if (use_data) { |
Andrew Duggan | 811adb9 | 2013-06-17 16:15:06 -0700 | [diff] [blame] | 351 | args[index++] = dataRegister & 0xFF; |
| 352 | args[index++] = dataRegister >> 8; |
Benjamin Tissoires | 9b5a9ae | 2014-02-10 12:58:49 -0500 | [diff] [blame] | 353 | hidcmd = &hid_set_report_cmd; |
Andrew Duggan | 811adb9 | 2013-06-17 16:15:06 -0700 | [diff] [blame] | 354 | } else { |
| 355 | args[index++] = outputRegister & 0xFF; |
| 356 | args[index++] = outputRegister >> 8; |
| 357 | hidcmd = &hid_no_cmd; |
| 358 | } |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 359 | |
| 360 | args[index++] = size & 0xFF; |
| 361 | args[index++] = size >> 8; |
| 362 | |
| 363 | if (reportID) |
| 364 | args[index++] = reportID; |
| 365 | |
| 366 | memcpy(&args[index], buf, data_len); |
| 367 | |
Andrew Duggan | 811adb9 | 2013-06-17 16:15:06 -0700 | [diff] [blame] | 368 | ret = __i2c_hid_command(client, hidcmd, reportID, |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 369 | reportType, args, args_len, NULL, 0); |
| 370 | if (ret) { |
| 371 | dev_err(&client->dev, "failed to set a report to device.\n"); |
Benjamin Tissoires | 317b204 | 2012-12-04 16:27:48 +0100 | [diff] [blame] | 372 | return ret; |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | return data_len; |
| 376 | } |
| 377 | |
| 378 | static int i2c_hid_set_power(struct i2c_client *client, int power_state) |
| 379 | { |
| 380 | struct i2c_hid *ihid = i2c_get_clientdata(client); |
| 381 | int ret; |
| 382 | |
| 383 | i2c_hid_dbg(ihid, "%s\n", __func__); |
| 384 | |
HungNien Chen | 71af01a | 2016-11-10 11:47:13 +0800 | [diff] [blame^] | 385 | /* |
| 386 | * Some devices require to send a command to wakeup before power on. |
| 387 | * The call will get a return value (EREMOTEIO) but device will be |
| 388 | * triggered and activated. After that, it goes like a normal device. |
| 389 | */ |
| 390 | if (power_state == I2C_HID_PWR_ON && |
| 391 | ihid->quirks & I2C_HID_QUIRK_SET_PWR_WAKEUP_DEV) { |
| 392 | ret = i2c_hid_command(client, &hid_set_power_cmd, NULL, 0); |
| 393 | |
| 394 | /* Device was already activated */ |
| 395 | if (!ret) |
| 396 | goto set_pwr_exit; |
| 397 | } |
| 398 | |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 399 | ret = __i2c_hid_command(client, &hid_set_power_cmd, power_state, |
| 400 | 0, NULL, 0, NULL, 0); |
HungNien Chen | 71af01a | 2016-11-10 11:47:13 +0800 | [diff] [blame^] | 401 | |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 402 | if (ret) |
| 403 | dev_err(&client->dev, "failed to change power setting.\n"); |
| 404 | |
HungNien Chen | 71af01a | 2016-11-10 11:47:13 +0800 | [diff] [blame^] | 405 | set_pwr_exit: |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 406 | return ret; |
| 407 | } |
| 408 | |
| 409 | static int i2c_hid_hwreset(struct i2c_client *client) |
| 410 | { |
| 411 | struct i2c_hid *ihid = i2c_get_clientdata(client); |
| 412 | int ret; |
| 413 | |
| 414 | i2c_hid_dbg(ihid, "%s\n", __func__); |
| 415 | |
Mika Westerberg | 9a32740 | 2015-12-21 15:26:31 +0200 | [diff] [blame] | 416 | /* |
| 417 | * This prevents sending feature reports while the device is |
| 418 | * being reset. Otherwise we may lose the reset complete |
| 419 | * interrupt. |
| 420 | */ |
| 421 | mutex_lock(&ihid->reset_lock); |
| 422 | |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 423 | ret = i2c_hid_set_power(client, I2C_HID_PWR_ON); |
| 424 | if (ret) |
Mika Westerberg | 9a32740 | 2015-12-21 15:26:31 +0200 | [diff] [blame] | 425 | goto out_unlock; |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 426 | |
| 427 | i2c_hid_dbg(ihid, "resetting...\n"); |
| 428 | |
| 429 | ret = i2c_hid_command(client, &hid_reset_cmd, NULL, 0); |
| 430 | if (ret) { |
| 431 | dev_err(&client->dev, "failed to reset device.\n"); |
| 432 | i2c_hid_set_power(client, I2C_HID_PWR_SLEEP); |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 433 | } |
| 434 | |
Mika Westerberg | 9a32740 | 2015-12-21 15:26:31 +0200 | [diff] [blame] | 435 | out_unlock: |
| 436 | mutex_unlock(&ihid->reset_lock); |
| 437 | return ret; |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 438 | } |
| 439 | |
Benjamin Tissoires | 317b204 | 2012-12-04 16:27:48 +0100 | [diff] [blame] | 440 | static void i2c_hid_get_input(struct i2c_hid *ihid) |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 441 | { |
| 442 | int ret, ret_size; |
Seth Forshee | 6d00f37 | 2015-02-20 11:45:11 -0600 | [diff] [blame] | 443 | int size = le16_to_cpu(ihid->hdesc.wMaxInputLength); |
| 444 | |
| 445 | if (size > ihid->bufsize) |
| 446 | size = ihid->bufsize; |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 447 | |
| 448 | ret = i2c_master_recv(ihid->client, ihid->inbuf, size); |
| 449 | if (ret != size) { |
| 450 | if (ret < 0) |
Benjamin Tissoires | 317b204 | 2012-12-04 16:27:48 +0100 | [diff] [blame] | 451 | return; |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 452 | |
| 453 | dev_err(&ihid->client->dev, "%s: got %d data instead of %d\n", |
| 454 | __func__, ret, size); |
Benjamin Tissoires | 317b204 | 2012-12-04 16:27:48 +0100 | [diff] [blame] | 455 | return; |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | ret_size = ihid->inbuf[0] | ihid->inbuf[1] << 8; |
| 459 | |
| 460 | if (!ret_size) { |
| 461 | /* host or device initiated RESET completed */ |
| 462 | if (test_and_clear_bit(I2C_HID_RESET_PENDING, &ihid->flags)) |
| 463 | wake_up(&ihid->wait); |
Benjamin Tissoires | 317b204 | 2012-12-04 16:27:48 +0100 | [diff] [blame] | 464 | return; |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 465 | } |
| 466 | |
| 467 | if (ret_size > size) { |
| 468 | dev_err(&ihid->client->dev, "%s: incomplete report (%d/%d)\n", |
| 469 | __func__, size, ret_size); |
Benjamin Tissoires | 317b204 | 2012-12-04 16:27:48 +0100 | [diff] [blame] | 470 | return; |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 471 | } |
| 472 | |
| 473 | i2c_hid_dbg(ihid, "input: %*ph\n", ret_size, ihid->inbuf); |
| 474 | |
| 475 | if (test_bit(I2C_HID_STARTED, &ihid->flags)) |
| 476 | hid_input_report(ihid->hid, HID_INPUT_REPORT, ihid->inbuf + 2, |
| 477 | ret_size - 2, 1); |
| 478 | |
Benjamin Tissoires | 317b204 | 2012-12-04 16:27:48 +0100 | [diff] [blame] | 479 | return; |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 480 | } |
| 481 | |
| 482 | static irqreturn_t i2c_hid_irq(int irq, void *dev_id) |
| 483 | { |
| 484 | struct i2c_hid *ihid = dev_id; |
| 485 | |
| 486 | if (test_bit(I2C_HID_READ_PENDING, &ihid->flags)) |
| 487 | return IRQ_HANDLED; |
| 488 | |
| 489 | i2c_hid_get_input(ihid); |
| 490 | |
| 491 | return IRQ_HANDLED; |
| 492 | } |
| 493 | |
| 494 | static int i2c_hid_get_report_length(struct hid_report *report) |
| 495 | { |
| 496 | return ((report->size - 1) >> 3) + 1 + |
| 497 | report->device->report_enum[report->type].numbered + 2; |
| 498 | } |
| 499 | |
| 500 | static void i2c_hid_init_report(struct hid_report *report, u8 *buffer, |
| 501 | size_t bufsize) |
| 502 | { |
| 503 | struct hid_device *hid = report->device; |
| 504 | struct i2c_client *client = hid->driver_data; |
| 505 | struct i2c_hid *ihid = i2c_get_clientdata(client); |
| 506 | unsigned int size, ret_size; |
| 507 | |
| 508 | size = i2c_hid_get_report_length(report); |
Benjamin Tissoires | c737bcf | 2012-12-04 16:27:50 +0100 | [diff] [blame] | 509 | if (i2c_hid_get_report(client, |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 510 | report->type == HID_FEATURE_REPORT ? 0x03 : 0x01, |
Benjamin Tissoires | c737bcf | 2012-12-04 16:27:50 +0100 | [diff] [blame] | 511 | report->id, buffer, size)) |
| 512 | return; |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 513 | |
Jean-Baptiste Maneyrol | 08bb7be | 2014-11-16 22:45:43 +0800 | [diff] [blame] | 514 | i2c_hid_dbg(ihid, "report (len=%d): %*ph\n", size, size, buffer); |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 515 | |
| 516 | ret_size = buffer[0] | (buffer[1] << 8); |
| 517 | |
| 518 | if (ret_size != size) { |
| 519 | dev_err(&client->dev, "error in %s size:%d / ret_size:%d\n", |
| 520 | __func__, size, ret_size); |
| 521 | return; |
| 522 | } |
| 523 | |
| 524 | /* hid->driver_lock is held as we are in probe function, |
| 525 | * we just need to setup the input fields, so using |
| 526 | * hid_report_raw_event is safe. */ |
| 527 | hid_report_raw_event(hid, report->type, buffer + 2, size - 2, 1); |
| 528 | } |
| 529 | |
| 530 | /* |
| 531 | * Initialize all reports |
| 532 | */ |
| 533 | static void i2c_hid_init_reports(struct hid_device *hid) |
| 534 | { |
| 535 | struct hid_report *report; |
| 536 | struct i2c_client *client = hid->driver_data; |
| 537 | struct i2c_hid *ihid = i2c_get_clientdata(client); |
| 538 | u8 *inbuf = kzalloc(ihid->bufsize, GFP_KERNEL); |
| 539 | |
Benjamin Tissoires | 317b204 | 2012-12-04 16:27:48 +0100 | [diff] [blame] | 540 | if (!inbuf) { |
| 541 | dev_err(&client->dev, "can not retrieve initial reports\n"); |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 542 | return; |
Benjamin Tissoires | 317b204 | 2012-12-04 16:27:48 +0100 | [diff] [blame] | 543 | } |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 544 | |
Mika Westerberg | 34f439e | 2014-01-29 11:24:36 +0200 | [diff] [blame] | 545 | /* |
| 546 | * The device must be powered on while we fetch initial reports |
| 547 | * from it. |
| 548 | */ |
| 549 | pm_runtime_get_sync(&client->dev); |
| 550 | |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 551 | list_for_each_entry(report, |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 552 | &hid->report_enum[HID_FEATURE_REPORT].report_list, list) |
| 553 | i2c_hid_init_report(report, inbuf, ihid->bufsize); |
| 554 | |
Mika Westerberg | 34f439e | 2014-01-29 11:24:36 +0200 | [diff] [blame] | 555 | pm_runtime_put(&client->dev); |
| 556 | |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 557 | kfree(inbuf); |
| 558 | } |
| 559 | |
| 560 | /* |
| 561 | * Traverse the supplied list of reports and find the longest |
| 562 | */ |
| 563 | static void i2c_hid_find_max_report(struct hid_device *hid, unsigned int type, |
| 564 | unsigned int *max) |
| 565 | { |
| 566 | struct hid_report *report; |
| 567 | unsigned int size; |
| 568 | |
| 569 | /* We should not rely on wMaxInputLength, as some devices may set it to |
| 570 | * a wrong length. */ |
| 571 | list_for_each_entry(report, &hid->report_enum[type].report_list, list) { |
| 572 | size = i2c_hid_get_report_length(report); |
| 573 | if (*max < size) |
| 574 | *max = size; |
| 575 | } |
| 576 | } |
| 577 | |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 578 | static void i2c_hid_free_buffers(struct i2c_hid *ihid) |
| 579 | { |
| 580 | kfree(ihid->inbuf); |
Jean-Baptiste Maneyrol | 6296f4a | 2014-11-20 00:46:37 +0800 | [diff] [blame] | 581 | kfree(ihid->rawbuf); |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 582 | kfree(ihid->argsbuf); |
| 583 | kfree(ihid->cmdbuf); |
| 584 | ihid->inbuf = NULL; |
Jean-Baptiste Maneyrol | 6296f4a | 2014-11-20 00:46:37 +0800 | [diff] [blame] | 585 | ihid->rawbuf = NULL; |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 586 | ihid->cmdbuf = NULL; |
| 587 | ihid->argsbuf = NULL; |
Benjamin Tissoires | 29b4578 | 2012-12-05 15:02:54 +0100 | [diff] [blame] | 588 | ihid->bufsize = 0; |
| 589 | } |
| 590 | |
| 591 | static int i2c_hid_alloc_buffers(struct i2c_hid *ihid, size_t report_size) |
| 592 | { |
| 593 | /* the worst case is computed from the set_report command with a |
| 594 | * reportID > 15 and the maximum report length */ |
| 595 | int args_len = sizeof(__u8) + /* optional ReportID byte */ |
| 596 | sizeof(__u16) + /* data register */ |
| 597 | sizeof(__u16) + /* size of the report */ |
| 598 | report_size; /* report */ |
| 599 | |
| 600 | ihid->inbuf = kzalloc(report_size, GFP_KERNEL); |
Jean-Baptiste Maneyrol | 6296f4a | 2014-11-20 00:46:37 +0800 | [diff] [blame] | 601 | ihid->rawbuf = kzalloc(report_size, GFP_KERNEL); |
Benjamin Tissoires | 29b4578 | 2012-12-05 15:02:54 +0100 | [diff] [blame] | 602 | ihid->argsbuf = kzalloc(args_len, GFP_KERNEL); |
| 603 | ihid->cmdbuf = kzalloc(sizeof(union command) + args_len, GFP_KERNEL); |
| 604 | |
Jean-Baptiste Maneyrol | 6296f4a | 2014-11-20 00:46:37 +0800 | [diff] [blame] | 605 | if (!ihid->inbuf || !ihid->rawbuf || !ihid->argsbuf || !ihid->cmdbuf) { |
Benjamin Tissoires | 29b4578 | 2012-12-05 15:02:54 +0100 | [diff] [blame] | 606 | i2c_hid_free_buffers(ihid); |
| 607 | return -ENOMEM; |
| 608 | } |
| 609 | |
| 610 | ihid->bufsize = report_size; |
| 611 | |
| 612 | return 0; |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 613 | } |
| 614 | |
| 615 | static int i2c_hid_get_raw_report(struct hid_device *hid, |
| 616 | unsigned char report_number, __u8 *buf, size_t count, |
| 617 | unsigned char report_type) |
| 618 | { |
| 619 | struct i2c_client *client = hid->driver_data; |
| 620 | struct i2c_hid *ihid = i2c_get_clientdata(client); |
Benjamin Tissoires | e5b50fe | 2012-12-05 15:02:56 +0100 | [diff] [blame] | 621 | size_t ret_count, ask_count; |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 622 | int ret; |
| 623 | |
| 624 | if (report_type == HID_OUTPUT_REPORT) |
| 625 | return -EINVAL; |
| 626 | |
Benjamin Tissoires | e5b50fe | 2012-12-05 15:02:56 +0100 | [diff] [blame] | 627 | /* +2 bytes to include the size of the reply in the query buffer */ |
| 628 | ask_count = min(count + 2, (size_t)ihid->bufsize); |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 629 | |
| 630 | ret = i2c_hid_get_report(client, |
| 631 | report_type == HID_FEATURE_REPORT ? 0x03 : 0x01, |
Jean-Baptiste Maneyrol | 6296f4a | 2014-11-20 00:46:37 +0800 | [diff] [blame] | 632 | report_number, ihid->rawbuf, ask_count); |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 633 | |
| 634 | if (ret < 0) |
| 635 | return ret; |
| 636 | |
Jean-Baptiste Maneyrol | 6296f4a | 2014-11-20 00:46:37 +0800 | [diff] [blame] | 637 | ret_count = ihid->rawbuf[0] | (ihid->rawbuf[1] << 8); |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 638 | |
Jiri Kosina | 9afd09a | 2012-12-06 10:59:28 +0100 | [diff] [blame] | 639 | if (ret_count <= 2) |
Benjamin Tissoires | e5b50fe | 2012-12-05 15:02:56 +0100 | [diff] [blame] | 640 | return 0; |
| 641 | |
| 642 | ret_count = min(ret_count, ask_count); |
| 643 | |
| 644 | /* The query buffer contains the size, dropping it in the reply */ |
| 645 | count = min(count, ret_count - 2); |
Jean-Baptiste Maneyrol | 6296f4a | 2014-11-20 00:46:37 +0800 | [diff] [blame] | 646 | memcpy(buf, ihid->rawbuf + 2, count); |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 647 | |
| 648 | return count; |
| 649 | } |
| 650 | |
| 651 | static int i2c_hid_output_raw_report(struct hid_device *hid, __u8 *buf, |
Benjamin Tissoires | 9b5a9ae | 2014-02-10 12:58:49 -0500 | [diff] [blame] | 652 | size_t count, unsigned char report_type, bool use_data) |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 653 | { |
| 654 | struct i2c_client *client = hid->driver_data; |
Mika Westerberg | 9a32740 | 2015-12-21 15:26:31 +0200 | [diff] [blame] | 655 | struct i2c_hid *ihid = i2c_get_clientdata(client); |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 656 | int report_id = buf[0]; |
Benjamin Tissoires | c284979 | 2013-01-31 17:50:02 +0100 | [diff] [blame] | 657 | int ret; |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 658 | |
| 659 | if (report_type == HID_INPUT_REPORT) |
| 660 | return -EINVAL; |
| 661 | |
Mika Westerberg | 9a32740 | 2015-12-21 15:26:31 +0200 | [diff] [blame] | 662 | mutex_lock(&ihid->reset_lock); |
| 663 | |
Benjamin Tissoires | c284979 | 2013-01-31 17:50:02 +0100 | [diff] [blame] | 664 | if (report_id) { |
| 665 | buf++; |
| 666 | count--; |
| 667 | } |
| 668 | |
Benjamin Tissoires | 9b5a9ae | 2014-02-10 12:58:49 -0500 | [diff] [blame] | 669 | ret = i2c_hid_set_or_send_report(client, |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 670 | report_type == HID_FEATURE_REPORT ? 0x03 : 0x02, |
Benjamin Tissoires | 9b5a9ae | 2014-02-10 12:58:49 -0500 | [diff] [blame] | 671 | report_id, buf, count, use_data); |
Benjamin Tissoires | c284979 | 2013-01-31 17:50:02 +0100 | [diff] [blame] | 672 | |
| 673 | if (report_id && ret >= 0) |
| 674 | ret++; /* add report_id to the number of transfered bytes */ |
| 675 | |
Mika Westerberg | 9a32740 | 2015-12-21 15:26:31 +0200 | [diff] [blame] | 676 | mutex_unlock(&ihid->reset_lock); |
| 677 | |
Benjamin Tissoires | c284979 | 2013-01-31 17:50:02 +0100 | [diff] [blame] | 678 | return ret; |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 679 | } |
| 680 | |
Benjamin Tissoires | 9b5a9ae | 2014-02-10 12:58:49 -0500 | [diff] [blame] | 681 | static int i2c_hid_output_report(struct hid_device *hid, __u8 *buf, |
| 682 | size_t count) |
Benjamin Tissoires | 545bef6 | 2013-02-25 11:31:50 +0100 | [diff] [blame] | 683 | { |
Benjamin Tissoires | 9b5a9ae | 2014-02-10 12:58:49 -0500 | [diff] [blame] | 684 | return i2c_hid_output_raw_report(hid, buf, count, HID_OUTPUT_REPORT, |
| 685 | false); |
| 686 | } |
Benjamin Tissoires | 545bef6 | 2013-02-25 11:31:50 +0100 | [diff] [blame] | 687 | |
Benjamin Tissoires | 9b5a9ae | 2014-02-10 12:58:49 -0500 | [diff] [blame] | 688 | static int i2c_hid_raw_request(struct hid_device *hid, unsigned char reportnum, |
| 689 | __u8 *buf, size_t len, unsigned char rtype, |
| 690 | int reqtype) |
| 691 | { |
Benjamin Tissoires | 545bef6 | 2013-02-25 11:31:50 +0100 | [diff] [blame] | 692 | switch (reqtype) { |
| 693 | case HID_REQ_GET_REPORT: |
Benjamin Tissoires | 9b5a9ae | 2014-02-10 12:58:49 -0500 | [diff] [blame] | 694 | return i2c_hid_get_raw_report(hid, reportnum, buf, len, rtype); |
Benjamin Tissoires | 545bef6 | 2013-02-25 11:31:50 +0100 | [diff] [blame] | 695 | case HID_REQ_SET_REPORT: |
Benjamin Tissoires | 9b5a9ae | 2014-02-10 12:58:49 -0500 | [diff] [blame] | 696 | if (buf[0] != reportnum) |
| 697 | return -EINVAL; |
| 698 | return i2c_hid_output_raw_report(hid, buf, len, rtype, true); |
| 699 | default: |
| 700 | return -EIO; |
Benjamin Tissoires | 545bef6 | 2013-02-25 11:31:50 +0100 | [diff] [blame] | 701 | } |
Benjamin Tissoires | 545bef6 | 2013-02-25 11:31:50 +0100 | [diff] [blame] | 702 | } |
| 703 | |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 704 | static int i2c_hid_parse(struct hid_device *hid) |
| 705 | { |
| 706 | struct i2c_client *client = hid->driver_data; |
| 707 | struct i2c_hid *ihid = i2c_get_clientdata(client); |
| 708 | struct i2c_hid_desc *hdesc = &ihid->hdesc; |
| 709 | unsigned int rsize; |
| 710 | char *rdesc; |
| 711 | int ret; |
| 712 | int tries = 3; |
| 713 | |
| 714 | i2c_hid_dbg(ihid, "entering %s\n", __func__); |
| 715 | |
| 716 | rsize = le16_to_cpu(hdesc->wReportDescLength); |
| 717 | if (!rsize || rsize > HID_MAX_DESCRIPTOR_SIZE) { |
| 718 | dbg_hid("weird size of report descriptor (%u)\n", rsize); |
| 719 | return -EINVAL; |
| 720 | } |
| 721 | |
| 722 | do { |
| 723 | ret = i2c_hid_hwreset(client); |
| 724 | if (ret) |
| 725 | msleep(1000); |
| 726 | } while (tries-- > 0 && ret); |
| 727 | |
| 728 | if (ret) |
| 729 | return ret; |
| 730 | |
| 731 | rdesc = kzalloc(rsize, GFP_KERNEL); |
| 732 | |
| 733 | if (!rdesc) { |
| 734 | dbg_hid("couldn't allocate rdesc memory\n"); |
| 735 | return -ENOMEM; |
| 736 | } |
| 737 | |
| 738 | i2c_hid_dbg(ihid, "asking HID report descriptor\n"); |
| 739 | |
| 740 | ret = i2c_hid_command(client, &hid_report_descr_cmd, rdesc, rsize); |
| 741 | if (ret) { |
| 742 | hid_err(hid, "reading report descriptor failed\n"); |
| 743 | kfree(rdesc); |
| 744 | return -EIO; |
| 745 | } |
| 746 | |
| 747 | i2c_hid_dbg(ihid, "Report Descriptor: %*ph\n", rsize, rdesc); |
| 748 | |
| 749 | ret = hid_parse_report(hid, rdesc, rsize); |
| 750 | kfree(rdesc); |
| 751 | if (ret) { |
| 752 | dbg_hid("parsing report descriptor failed\n"); |
| 753 | return ret; |
| 754 | } |
| 755 | |
| 756 | return 0; |
| 757 | } |
| 758 | |
| 759 | static int i2c_hid_start(struct hid_device *hid) |
| 760 | { |
| 761 | struct i2c_client *client = hid->driver_data; |
| 762 | struct i2c_hid *ihid = i2c_get_clientdata(client); |
| 763 | int ret; |
Benjamin Tissoires | 29b4578 | 2012-12-05 15:02:54 +0100 | [diff] [blame] | 764 | unsigned int bufsize = HID_MIN_BUFFER_SIZE; |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 765 | |
Benjamin Tissoires | 29b4578 | 2012-12-05 15:02:54 +0100 | [diff] [blame] | 766 | i2c_hid_find_max_report(hid, HID_INPUT_REPORT, &bufsize); |
| 767 | i2c_hid_find_max_report(hid, HID_OUTPUT_REPORT, &bufsize); |
| 768 | i2c_hid_find_max_report(hid, HID_FEATURE_REPORT, &bufsize); |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 769 | |
Benjamin Tissoires | 29b4578 | 2012-12-05 15:02:54 +0100 | [diff] [blame] | 770 | if (bufsize > ihid->bufsize) { |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 771 | i2c_hid_free_buffers(ihid); |
| 772 | |
Benjamin Tissoires | 29b4578 | 2012-12-05 15:02:54 +0100 | [diff] [blame] | 773 | ret = i2c_hid_alloc_buffers(ihid, bufsize); |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 774 | |
Benjamin Tissoires | 29b4578 | 2012-12-05 15:02:54 +0100 | [diff] [blame] | 775 | if (ret) |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 776 | return ret; |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 777 | } |
| 778 | |
| 779 | if (!(hid->quirks & HID_QUIRK_NO_INIT_REPORTS)) |
| 780 | i2c_hid_init_reports(hid); |
| 781 | |
| 782 | return 0; |
| 783 | } |
| 784 | |
| 785 | static void i2c_hid_stop(struct hid_device *hid) |
| 786 | { |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 787 | hid->claimed = 0; |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 788 | } |
| 789 | |
| 790 | static int i2c_hid_open(struct hid_device *hid) |
| 791 | { |
| 792 | struct i2c_client *client = hid->driver_data; |
| 793 | struct i2c_hid *ihid = i2c_get_clientdata(client); |
Benjamin Tissoires | 7a7d6d9 | 2012-12-12 18:00:02 +0100 | [diff] [blame] | 794 | int ret = 0; |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 795 | |
Benjamin Tissoires | 7a7d6d9 | 2012-12-12 18:00:02 +0100 | [diff] [blame] | 796 | mutex_lock(&i2c_hid_open_mut); |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 797 | if (!hid->open++) { |
Mika Westerberg | 34f439e | 2014-01-29 11:24:36 +0200 | [diff] [blame] | 798 | ret = pm_runtime_get_sync(&client->dev); |
| 799 | if (ret < 0) { |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 800 | hid->open--; |
Benjamin Tissoires | 7a7d6d9 | 2012-12-12 18:00:02 +0100 | [diff] [blame] | 801 | goto done; |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 802 | } |
| 803 | set_bit(I2C_HID_STARTED, &ihid->flags); |
| 804 | } |
Benjamin Tissoires | 7a7d6d9 | 2012-12-12 18:00:02 +0100 | [diff] [blame] | 805 | done: |
| 806 | mutex_unlock(&i2c_hid_open_mut); |
Mika Westerberg | 34f439e | 2014-01-29 11:24:36 +0200 | [diff] [blame] | 807 | return ret < 0 ? ret : 0; |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 808 | } |
| 809 | |
| 810 | static void i2c_hid_close(struct hid_device *hid) |
| 811 | { |
| 812 | struct i2c_client *client = hid->driver_data; |
| 813 | struct i2c_hid *ihid = i2c_get_clientdata(client); |
| 814 | |
| 815 | /* protecting hid->open to make sure we don't restart |
| 816 | * data acquistion due to a resumption we no longer |
| 817 | * care about |
| 818 | */ |
Benjamin Tissoires | 7a7d6d9 | 2012-12-12 18:00:02 +0100 | [diff] [blame] | 819 | mutex_lock(&i2c_hid_open_mut); |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 820 | if (!--hid->open) { |
| 821 | clear_bit(I2C_HID_STARTED, &ihid->flags); |
| 822 | |
| 823 | /* Save some power */ |
Mika Westerberg | 34f439e | 2014-01-29 11:24:36 +0200 | [diff] [blame] | 824 | pm_runtime_put(&client->dev); |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 825 | } |
Benjamin Tissoires | 7a7d6d9 | 2012-12-12 18:00:02 +0100 | [diff] [blame] | 826 | mutex_unlock(&i2c_hid_open_mut); |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 827 | } |
| 828 | |
| 829 | static int i2c_hid_power(struct hid_device *hid, int lvl) |
| 830 | { |
| 831 | struct i2c_client *client = hid->driver_data; |
| 832 | struct i2c_hid *ihid = i2c_get_clientdata(client); |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 833 | |
| 834 | i2c_hid_dbg(ihid, "%s lvl:%d\n", __func__, lvl); |
| 835 | |
| 836 | switch (lvl) { |
| 837 | case PM_HINT_FULLON: |
Mika Westerberg | 34f439e | 2014-01-29 11:24:36 +0200 | [diff] [blame] | 838 | pm_runtime_get_sync(&client->dev); |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 839 | break; |
| 840 | case PM_HINT_NORMAL: |
Mika Westerberg | 34f439e | 2014-01-29 11:24:36 +0200 | [diff] [blame] | 841 | pm_runtime_put(&client->dev); |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 842 | break; |
| 843 | } |
Mika Westerberg | 34f439e | 2014-01-29 11:24:36 +0200 | [diff] [blame] | 844 | return 0; |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 845 | } |
| 846 | |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 847 | static struct hid_ll_driver i2c_hid_ll_driver = { |
| 848 | .parse = i2c_hid_parse, |
| 849 | .start = i2c_hid_start, |
| 850 | .stop = i2c_hid_stop, |
| 851 | .open = i2c_hid_open, |
| 852 | .close = i2c_hid_close, |
| 853 | .power = i2c_hid_power, |
Benjamin Tissoires | 9b5a9ae | 2014-02-10 12:58:49 -0500 | [diff] [blame] | 854 | .output_report = i2c_hid_output_report, |
| 855 | .raw_request = i2c_hid_raw_request, |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 856 | }; |
| 857 | |
Greg Kroah-Hartman | 0fe763c | 2012-12-21 15:14:44 -0800 | [diff] [blame] | 858 | static int i2c_hid_init_irq(struct i2c_client *client) |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 859 | { |
| 860 | struct i2c_hid *ihid = i2c_get_clientdata(client); |
| 861 | int ret; |
| 862 | |
David Arcari | ba18a93 | 2016-10-13 11:30:44 +0200 | [diff] [blame] | 863 | dev_dbg(&client->dev, "Requesting IRQ: %d\n", client->irq); |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 864 | |
David Arcari | ba18a93 | 2016-10-13 11:30:44 +0200 | [diff] [blame] | 865 | ret = request_threaded_irq(client->irq, NULL, i2c_hid_irq, |
Mika Westerberg | f2de746 | 2015-01-26 16:29:32 +0200 | [diff] [blame] | 866 | IRQF_TRIGGER_LOW | IRQF_ONESHOT, |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 867 | client->name, ihid); |
| 868 | if (ret < 0) { |
Benjamin Tissoires | 9972dcc | 2012-12-04 16:27:49 +0100 | [diff] [blame] | 869 | dev_warn(&client->dev, |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 870 | "Could not register for %s interrupt, irq = %d," |
| 871 | " ret = %d\n", |
David Arcari | ba18a93 | 2016-10-13 11:30:44 +0200 | [diff] [blame] | 872 | client->name, client->irq, ret); |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 873 | |
| 874 | return ret; |
| 875 | } |
| 876 | |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 877 | return 0; |
| 878 | } |
| 879 | |
Greg Kroah-Hartman | 0fe763c | 2012-12-21 15:14:44 -0800 | [diff] [blame] | 880 | static int i2c_hid_fetch_hid_descriptor(struct i2c_hid *ihid) |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 881 | { |
| 882 | struct i2c_client *client = ihid->client; |
| 883 | struct i2c_hid_desc *hdesc = &ihid->hdesc; |
| 884 | unsigned int dsize; |
| 885 | int ret; |
| 886 | |
Archana Patni | f58b848 | 2014-05-08 09:26:19 -0400 | [diff] [blame] | 887 | /* i2c hid fetch using a fixed descriptor size (30 bytes) */ |
| 888 | i2c_hid_dbg(ihid, "Fetching the HID descriptor\n"); |
| 889 | ret = i2c_hid_command(client, &hid_descr_cmd, ihid->hdesc_buffer, |
| 890 | sizeof(struct i2c_hid_desc)); |
| 891 | if (ret) { |
| 892 | dev_err(&client->dev, "hid_descr_cmd failed\n"); |
| 893 | return -ENODEV; |
| 894 | } |
| 895 | |
| 896 | /* Validate the length of HID descriptor, the 4 first bytes: |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 897 | * bytes 0-1 -> length |
| 898 | * bytes 2-3 -> bcdVersion (has to be 1.00) */ |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 899 | /* check bcdVersion == 1.0 */ |
| 900 | if (le16_to_cpu(hdesc->bcdVersion) != 0x0100) { |
| 901 | dev_err(&client->dev, |
Benjamin Tissoires | 9972dcc | 2012-12-04 16:27:49 +0100 | [diff] [blame] | 902 | "unexpected HID descriptor bcdVersion (0x%04hx)\n", |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 903 | le16_to_cpu(hdesc->bcdVersion)); |
| 904 | return -ENODEV; |
| 905 | } |
| 906 | |
Archana Patni | f58b848 | 2014-05-08 09:26:19 -0400 | [diff] [blame] | 907 | /* Descriptor length should be 30 bytes as per the specification */ |
| 908 | dsize = le16_to_cpu(hdesc->wHIDDescLength); |
| 909 | if (dsize != sizeof(struct i2c_hid_desc)) { |
| 910 | dev_err(&client->dev, "weird size of HID descriptor (%u)\n", |
| 911 | dsize); |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 912 | return -ENODEV; |
| 913 | } |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 914 | i2c_hid_dbg(ihid, "HID Descriptor: %*ph\n", dsize, ihid->hdesc_buffer); |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 915 | return 0; |
| 916 | } |
| 917 | |
Mika Westerberg | 92241e6 | 2013-01-09 16:43:08 +0200 | [diff] [blame] | 918 | #ifdef CONFIG_ACPI |
| 919 | static int i2c_hid_acpi_pdata(struct i2c_client *client, |
| 920 | struct i2c_hid_platform_data *pdata) |
| 921 | { |
| 922 | static u8 i2c_hid_guid[] = { |
| 923 | 0xF7, 0xF6, 0xDF, 0x3C, 0x67, 0x42, 0x55, 0x45, |
| 924 | 0xAD, 0x05, 0xB3, 0x0A, 0x3D, 0x89, 0x38, 0xDE, |
| 925 | }; |
Jiang Liu | ea547d7 | 2013-12-19 20:38:19 +0800 | [diff] [blame] | 926 | union acpi_object *obj; |
Mika Westerberg | 92241e6 | 2013-01-09 16:43:08 +0200 | [diff] [blame] | 927 | struct acpi_device *adev; |
| 928 | acpi_handle handle; |
| 929 | |
| 930 | handle = ACPI_HANDLE(&client->dev); |
| 931 | if (!handle || acpi_bus_get_device(handle, &adev)) |
| 932 | return -ENODEV; |
| 933 | |
Jiang Liu | ea547d7 | 2013-12-19 20:38:19 +0800 | [diff] [blame] | 934 | obj = acpi_evaluate_dsm_typed(handle, i2c_hid_guid, 1, 1, NULL, |
| 935 | ACPI_TYPE_INTEGER); |
| 936 | if (!obj) { |
Mika Westerberg | 92241e6 | 2013-01-09 16:43:08 +0200 | [diff] [blame] | 937 | dev_err(&client->dev, "device _DSM execution failed\n"); |
| 938 | return -ENODEV; |
| 939 | } |
| 940 | |
Jiang Liu | ea547d7 | 2013-12-19 20:38:19 +0800 | [diff] [blame] | 941 | pdata->hid_descriptor_address = obj->integer.value; |
| 942 | ACPI_FREE(obj); |
Mika Westerberg | 92241e6 | 2013-01-09 16:43:08 +0200 | [diff] [blame] | 943 | |
David Arcari | ba18a93 | 2016-10-13 11:30:44 +0200 | [diff] [blame] | 944 | return 0; |
Mika Westerberg | 92241e6 | 2013-01-09 16:43:08 +0200 | [diff] [blame] | 945 | } |
| 946 | |
| 947 | static const struct acpi_device_id i2c_hid_acpi_match[] = { |
| 948 | {"ACPI0C50", 0 }, |
| 949 | {"PNP0C50", 0 }, |
| 950 | { }, |
| 951 | }; |
| 952 | MODULE_DEVICE_TABLE(acpi, i2c_hid_acpi_match); |
| 953 | #else |
| 954 | static inline int i2c_hid_acpi_pdata(struct i2c_client *client, |
| 955 | struct i2c_hid_platform_data *pdata) |
| 956 | { |
| 957 | return -ENODEV; |
| 958 | } |
| 959 | #endif |
| 960 | |
Benjamin Tissoires | 3d7d248 | 2013-06-13 09:50:35 +0200 | [diff] [blame] | 961 | #ifdef CONFIG_OF |
| 962 | static int i2c_hid_of_probe(struct i2c_client *client, |
| 963 | struct i2c_hid_platform_data *pdata) |
| 964 | { |
| 965 | struct device *dev = &client->dev; |
| 966 | u32 val; |
| 967 | int ret; |
| 968 | |
| 969 | ret = of_property_read_u32(dev->of_node, "hid-descr-addr", &val); |
| 970 | if (ret) { |
| 971 | dev_err(&client->dev, "HID register address not provided\n"); |
| 972 | return -ENODEV; |
| 973 | } |
| 974 | if (val >> 16) { |
| 975 | dev_err(&client->dev, "Bad HID register address: 0x%08x\n", |
| 976 | val); |
| 977 | return -EINVAL; |
| 978 | } |
| 979 | pdata->hid_descriptor_address = val; |
| 980 | |
| 981 | return 0; |
| 982 | } |
| 983 | |
| 984 | static const struct of_device_id i2c_hid_of_match[] = { |
| 985 | { .compatible = "hid-over-i2c" }, |
| 986 | {}, |
| 987 | }; |
| 988 | MODULE_DEVICE_TABLE(of, i2c_hid_of_match); |
| 989 | #else |
| 990 | static inline int i2c_hid_of_probe(struct i2c_client *client, |
| 991 | struct i2c_hid_platform_data *pdata) |
| 992 | { |
| 993 | return -ENODEV; |
| 994 | } |
| 995 | #endif |
| 996 | |
Greg Kroah-Hartman | 0fe763c | 2012-12-21 15:14:44 -0800 | [diff] [blame] | 997 | static int i2c_hid_probe(struct i2c_client *client, |
| 998 | const struct i2c_device_id *dev_id) |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 999 | { |
| 1000 | int ret; |
| 1001 | struct i2c_hid *ihid; |
| 1002 | struct hid_device *hid; |
| 1003 | __u16 hidRegister; |
| 1004 | struct i2c_hid_platform_data *platform_data = client->dev.platform_data; |
| 1005 | |
| 1006 | dbg_hid("HID probe called for i2c 0x%02x\n", client->addr); |
| 1007 | |
David Arcari | ba18a93 | 2016-10-13 11:30:44 +0200 | [diff] [blame] | 1008 | if (!client->irq) { |
| 1009 | dev_err(&client->dev, |
| 1010 | "HID over i2c has not been provided an Int IRQ\n"); |
| 1011 | return -EINVAL; |
| 1012 | } |
| 1013 | |
David Arcari | 93d26ae | 2016-10-13 11:30:45 +0200 | [diff] [blame] | 1014 | if (client->irq < 0) { |
| 1015 | if (client->irq != -EPROBE_DEFER) |
| 1016 | dev_err(&client->dev, |
| 1017 | "HID over i2c doesn't have a valid IRQ\n"); |
| 1018 | return client->irq; |
| 1019 | } |
| 1020 | |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 1021 | ihid = kzalloc(sizeof(struct i2c_hid), GFP_KERNEL); |
| 1022 | if (!ihid) |
| 1023 | return -ENOMEM; |
| 1024 | |
Benjamin Tissoires | 3d7d248 | 2013-06-13 09:50:35 +0200 | [diff] [blame] | 1025 | if (client->dev.of_node) { |
| 1026 | ret = i2c_hid_of_probe(client, &ihid->pdata); |
| 1027 | if (ret) |
| 1028 | goto err; |
| 1029 | } else if (!platform_data) { |
Mika Westerberg | 92241e6 | 2013-01-09 16:43:08 +0200 | [diff] [blame] | 1030 | ret = i2c_hid_acpi_pdata(client, &ihid->pdata); |
| 1031 | if (ret) { |
| 1032 | dev_err(&client->dev, |
| 1033 | "HID register address not provided\n"); |
| 1034 | goto err; |
| 1035 | } |
| 1036 | } else { |
| 1037 | ihid->pdata = *platform_data; |
| 1038 | } |
| 1039 | |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 1040 | i2c_set_clientdata(client, ihid); |
| 1041 | |
| 1042 | ihid->client = client; |
| 1043 | |
Mika Westerberg | 92241e6 | 2013-01-09 16:43:08 +0200 | [diff] [blame] | 1044 | hidRegister = ihid->pdata.hid_descriptor_address; |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 1045 | ihid->wHIDDescRegister = cpu_to_le16(hidRegister); |
| 1046 | |
| 1047 | init_waitqueue_head(&ihid->wait); |
Mika Westerberg | 9a32740 | 2015-12-21 15:26:31 +0200 | [diff] [blame] | 1048 | mutex_init(&ihid->reset_lock); |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 1049 | |
| 1050 | /* we need to allocate the command buffer without knowing the maximum |
| 1051 | * size of the reports. Let's use HID_MIN_BUFFER_SIZE, then we do the |
| 1052 | * real computation later. */ |
Benjamin Tissoires | 29b4578 | 2012-12-05 15:02:54 +0100 | [diff] [blame] | 1053 | ret = i2c_hid_alloc_buffers(ihid, HID_MIN_BUFFER_SIZE); |
| 1054 | if (ret < 0) |
| 1055 | goto err; |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 1056 | |
Mika Westerberg | 34f439e | 2014-01-29 11:24:36 +0200 | [diff] [blame] | 1057 | pm_runtime_get_noresume(&client->dev); |
| 1058 | pm_runtime_set_active(&client->dev); |
| 1059 | pm_runtime_enable(&client->dev); |
Fu, Zhonghui | 982e42d | 2016-05-19 10:46:24 +0800 | [diff] [blame] | 1060 | device_enable_async_suspend(&client->dev); |
Mika Westerberg | 34f439e | 2014-01-29 11:24:36 +0200 | [diff] [blame] | 1061 | |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 1062 | ret = i2c_hid_fetch_hid_descriptor(ihid); |
| 1063 | if (ret < 0) |
Mika Westerberg | 34f439e | 2014-01-29 11:24:36 +0200 | [diff] [blame] | 1064 | goto err_pm; |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 1065 | |
| 1066 | ret = i2c_hid_init_irq(client); |
| 1067 | if (ret < 0) |
Mika Westerberg | 34f439e | 2014-01-29 11:24:36 +0200 | [diff] [blame] | 1068 | goto err_pm; |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 1069 | |
| 1070 | hid = hid_allocate_device(); |
| 1071 | if (IS_ERR(hid)) { |
| 1072 | ret = PTR_ERR(hid); |
Benjamin Tissoires | 8a1bbb5 | 2012-12-05 15:02:55 +0100 | [diff] [blame] | 1073 | goto err_irq; |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 1074 | } |
| 1075 | |
| 1076 | ihid->hid = hid; |
| 1077 | |
| 1078 | hid->driver_data = client; |
| 1079 | hid->ll_driver = &i2c_hid_ll_driver; |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 1080 | hid->dev.parent = &client->dev; |
| 1081 | hid->bus = BUS_I2C; |
| 1082 | hid->version = le16_to_cpu(ihid->hdesc.bcdVersion); |
| 1083 | hid->vendor = le16_to_cpu(ihid->hdesc.wVendorID); |
| 1084 | hid->product = le16_to_cpu(ihid->hdesc.wProductID); |
| 1085 | |
Benjamin Tissoires | 9972dcc | 2012-12-04 16:27:49 +0100 | [diff] [blame] | 1086 | snprintf(hid->name, sizeof(hid->name), "%s %04hX:%04hX", |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 1087 | client->name, hid->vendor, hid->product); |
Mika Westerberg | f3984ed | 2015-09-28 15:15:08 +0300 | [diff] [blame] | 1088 | strlcpy(hid->phys, dev_name(&client->dev), sizeof(hid->phys)); |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 1089 | |
HungNien Chen | 71af01a | 2016-11-10 11:47:13 +0800 | [diff] [blame^] | 1090 | ihid->quirks = i2c_hid_lookup_quirk(hid->vendor, hid->product); |
| 1091 | |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 1092 | ret = hid_add_device(hid); |
| 1093 | if (ret) { |
| 1094 | if (ret != -ENODEV) |
| 1095 | hid_err(client, "can't add hid device: %d\n", ret); |
| 1096 | goto err_mem_free; |
| 1097 | } |
| 1098 | |
Mika Westerberg | 34f439e | 2014-01-29 11:24:36 +0200 | [diff] [blame] | 1099 | pm_runtime_put(&client->dev); |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 1100 | return 0; |
| 1101 | |
| 1102 | err_mem_free: |
| 1103 | hid_destroy_device(hid); |
| 1104 | |
Benjamin Tissoires | 8a1bbb5 | 2012-12-05 15:02:55 +0100 | [diff] [blame] | 1105 | err_irq: |
David Arcari | ba18a93 | 2016-10-13 11:30:44 +0200 | [diff] [blame] | 1106 | free_irq(client->irq, ihid); |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 1107 | |
Mika Westerberg | 34f439e | 2014-01-29 11:24:36 +0200 | [diff] [blame] | 1108 | err_pm: |
| 1109 | pm_runtime_put_noidle(&client->dev); |
| 1110 | pm_runtime_disable(&client->dev); |
| 1111 | |
Benjamin Tissoires | 8a1bbb5 | 2012-12-05 15:02:55 +0100 | [diff] [blame] | 1112 | err: |
Jiri Kosina | 3c62602 | 2012-11-20 17:09:40 +0100 | [diff] [blame] | 1113 | i2c_hid_free_buffers(ihid); |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 1114 | kfree(ihid); |
| 1115 | return ret; |
| 1116 | } |
| 1117 | |
Greg Kroah-Hartman | 0fe763c | 2012-12-21 15:14:44 -0800 | [diff] [blame] | 1118 | static int i2c_hid_remove(struct i2c_client *client) |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 1119 | { |
| 1120 | struct i2c_hid *ihid = i2c_get_clientdata(client); |
| 1121 | struct hid_device *hid; |
| 1122 | |
Mika Westerberg | 34f439e | 2014-01-29 11:24:36 +0200 | [diff] [blame] | 1123 | pm_runtime_get_sync(&client->dev); |
| 1124 | pm_runtime_disable(&client->dev); |
| 1125 | pm_runtime_set_suspended(&client->dev); |
| 1126 | pm_runtime_put_noidle(&client->dev); |
| 1127 | |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 1128 | hid = ihid->hid; |
| 1129 | hid_destroy_device(hid); |
| 1130 | |
David Arcari | ba18a93 | 2016-10-13 11:30:44 +0200 | [diff] [blame] | 1131 | free_irq(client->irq, ihid); |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 1132 | |
Benjamin Tissoires | 134ebfd | 2012-12-04 16:27:54 +0100 | [diff] [blame] | 1133 | if (ihid->bufsize) |
| 1134 | i2c_hid_free_buffers(ihid); |
| 1135 | |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 1136 | kfree(ihid); |
| 1137 | |
| 1138 | return 0; |
| 1139 | } |
| 1140 | |
Guohua Zhong | d9f448e3 | 2016-06-21 18:27:45 +0800 | [diff] [blame] | 1141 | static void i2c_hid_shutdown(struct i2c_client *client) |
| 1142 | { |
| 1143 | struct i2c_hid *ihid = i2c_get_clientdata(client); |
| 1144 | |
| 1145 | i2c_hid_set_power(client, I2C_HID_PWR_SLEEP); |
| 1146 | free_irq(client->irq, ihid); |
| 1147 | } |
| 1148 | |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 1149 | #ifdef CONFIG_PM_SLEEP |
| 1150 | static int i2c_hid_suspend(struct device *dev) |
| 1151 | { |
| 1152 | struct i2c_client *client = to_i2c_client(dev); |
Andrew Duggan | 109571c | 2014-07-11 16:34:18 -0700 | [diff] [blame] | 1153 | struct i2c_hid *ihid = i2c_get_clientdata(client); |
| 1154 | struct hid_device *hid = ihid->hid; |
Doug Anderson | 01714a6 | 2016-03-08 15:03:23 -0800 | [diff] [blame] | 1155 | int ret; |
Andrew Duggan | d1c4803 | 2015-07-30 14:49:00 -0700 | [diff] [blame] | 1156 | int wake_status; |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 1157 | |
Doug Anderson | 01714a6 | 2016-03-08 15:03:23 -0800 | [diff] [blame] | 1158 | if (hid->driver && hid->driver->suspend) { |
| 1159 | /* |
| 1160 | * Wake up the device so that IO issues in |
| 1161 | * HID driver's suspend code can succeed. |
| 1162 | */ |
| 1163 | ret = pm_runtime_resume(dev); |
| 1164 | if (ret < 0) |
| 1165 | return ret; |
Gabriele Mazzotta | af4739c | 2015-07-07 21:58:02 +0200 | [diff] [blame] | 1166 | |
Doug Anderson | 01714a6 | 2016-03-08 15:03:23 -0800 | [diff] [blame] | 1167 | ret = hid->driver->suspend(hid, PMSG_SUSPEND); |
| 1168 | if (ret < 0) |
| 1169 | return ret; |
| 1170 | } |
| 1171 | |
| 1172 | if (!pm_runtime_suspended(dev)) { |
| 1173 | /* Save some power */ |
| 1174 | i2c_hid_set_power(client, I2C_HID_PWR_SLEEP); |
| 1175 | |
David Arcari | ba18a93 | 2016-10-13 11:30:44 +0200 | [diff] [blame] | 1176 | disable_irq(client->irq); |
Doug Anderson | 01714a6 | 2016-03-08 15:03:23 -0800 | [diff] [blame] | 1177 | } |
| 1178 | |
Andrew Duggan | d1c4803 | 2015-07-30 14:49:00 -0700 | [diff] [blame] | 1179 | if (device_may_wakeup(&client->dev)) { |
David Arcari | ba18a93 | 2016-10-13 11:30:44 +0200 | [diff] [blame] | 1180 | wake_status = enable_irq_wake(client->irq); |
Andrew Duggan | d1c4803 | 2015-07-30 14:49:00 -0700 | [diff] [blame] | 1181 | if (!wake_status) |
| 1182 | ihid->irq_wake_enabled = true; |
| 1183 | else |
| 1184 | hid_warn(hid, "Failed to enable irq wake: %d\n", |
| 1185 | wake_status); |
| 1186 | } |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 1187 | |
Doug Anderson | 01714a6 | 2016-03-08 15:03:23 -0800 | [diff] [blame] | 1188 | return 0; |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 1189 | } |
| 1190 | |
| 1191 | static int i2c_hid_resume(struct device *dev) |
| 1192 | { |
| 1193 | int ret; |
| 1194 | struct i2c_client *client = to_i2c_client(dev); |
Andrew Duggan | 109571c | 2014-07-11 16:34:18 -0700 | [diff] [blame] | 1195 | struct i2c_hid *ihid = i2c_get_clientdata(client); |
| 1196 | struct hid_device *hid = ihid->hid; |
Andrew Duggan | d1c4803 | 2015-07-30 14:49:00 -0700 | [diff] [blame] | 1197 | int wake_status; |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 1198 | |
Andrew Duggan | d1c4803 | 2015-07-30 14:49:00 -0700 | [diff] [blame] | 1199 | if (device_may_wakeup(&client->dev) && ihid->irq_wake_enabled) { |
David Arcari | ba18a93 | 2016-10-13 11:30:44 +0200 | [diff] [blame] | 1200 | wake_status = disable_irq_wake(client->irq); |
Andrew Duggan | d1c4803 | 2015-07-30 14:49:00 -0700 | [diff] [blame] | 1201 | if (!wake_status) |
| 1202 | ihid->irq_wake_enabled = false; |
| 1203 | else |
| 1204 | hid_warn(hid, "Failed to disable irq wake: %d\n", |
| 1205 | wake_status); |
| 1206 | } |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 1207 | |
Doug Anderson | 01714a6 | 2016-03-08 15:03:23 -0800 | [diff] [blame] | 1208 | /* We'll resume to full power */ |
| 1209 | pm_runtime_disable(dev); |
| 1210 | pm_runtime_set_active(dev); |
| 1211 | pm_runtime_enable(dev); |
| 1212 | |
David Arcari | ba18a93 | 2016-10-13 11:30:44 +0200 | [diff] [blame] | 1213 | enable_irq(client->irq); |
Doug Anderson | 01714a6 | 2016-03-08 15:03:23 -0800 | [diff] [blame] | 1214 | ret = i2c_hid_hwreset(client); |
| 1215 | if (ret) |
| 1216 | return ret; |
| 1217 | |
Andrew Duggan | 109571c | 2014-07-11 16:34:18 -0700 | [diff] [blame] | 1218 | if (hid->driver && hid->driver->reset_resume) { |
| 1219 | ret = hid->driver->reset_resume(hid); |
| 1220 | return ret; |
| 1221 | } |
| 1222 | |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 1223 | return 0; |
| 1224 | } |
| 1225 | #endif |
| 1226 | |
Rafael J. Wysocki | 721564a | 2014-12-04 01:05:44 +0100 | [diff] [blame] | 1227 | #ifdef CONFIG_PM |
Mika Westerberg | 34f439e | 2014-01-29 11:24:36 +0200 | [diff] [blame] | 1228 | static int i2c_hid_runtime_suspend(struct device *dev) |
| 1229 | { |
| 1230 | struct i2c_client *client = to_i2c_client(dev); |
| 1231 | |
| 1232 | i2c_hid_set_power(client, I2C_HID_PWR_SLEEP); |
David Arcari | ba18a93 | 2016-10-13 11:30:44 +0200 | [diff] [blame] | 1233 | disable_irq(client->irq); |
Mika Westerberg | 34f439e | 2014-01-29 11:24:36 +0200 | [diff] [blame] | 1234 | return 0; |
| 1235 | } |
| 1236 | |
| 1237 | static int i2c_hid_runtime_resume(struct device *dev) |
| 1238 | { |
| 1239 | struct i2c_client *client = to_i2c_client(dev); |
| 1240 | |
David Arcari | ba18a93 | 2016-10-13 11:30:44 +0200 | [diff] [blame] | 1241 | enable_irq(client->irq); |
Mika Westerberg | 34f439e | 2014-01-29 11:24:36 +0200 | [diff] [blame] | 1242 | i2c_hid_set_power(client, I2C_HID_PWR_ON); |
| 1243 | return 0; |
| 1244 | } |
| 1245 | #endif |
| 1246 | |
| 1247 | static const struct dev_pm_ops i2c_hid_pm = { |
| 1248 | SET_SYSTEM_SLEEP_PM_OPS(i2c_hid_suspend, i2c_hid_resume) |
| 1249 | SET_RUNTIME_PM_OPS(i2c_hid_runtime_suspend, i2c_hid_runtime_resume, |
| 1250 | NULL) |
| 1251 | }; |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 1252 | |
| 1253 | static const struct i2c_device_id i2c_hid_id_table[] = { |
Benjamin Tissoires | 24ebb37 | 2012-12-04 16:27:42 +0100 | [diff] [blame] | 1254 | { "hid", 0 }, |
Benson Leung | 1dcdde9 | 2016-03-08 11:25:39 -0800 | [diff] [blame] | 1255 | { "hid-over-i2c", 0 }, |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 1256 | { }, |
| 1257 | }; |
| 1258 | MODULE_DEVICE_TABLE(i2c, i2c_hid_id_table); |
| 1259 | |
| 1260 | |
| 1261 | static struct i2c_driver i2c_hid_driver = { |
| 1262 | .driver = { |
| 1263 | .name = "i2c_hid", |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 1264 | .pm = &i2c_hid_pm, |
Mika Westerberg | 92241e6 | 2013-01-09 16:43:08 +0200 | [diff] [blame] | 1265 | .acpi_match_table = ACPI_PTR(i2c_hid_acpi_match), |
Benjamin Tissoires | 3d7d248 | 2013-06-13 09:50:35 +0200 | [diff] [blame] | 1266 | .of_match_table = of_match_ptr(i2c_hid_of_match), |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 1267 | }, |
| 1268 | |
| 1269 | .probe = i2c_hid_probe, |
Greg Kroah-Hartman | 0fe763c | 2012-12-21 15:14:44 -0800 | [diff] [blame] | 1270 | .remove = i2c_hid_remove, |
Guohua Zhong | d9f448e3 | 2016-06-21 18:27:45 +0800 | [diff] [blame] | 1271 | .shutdown = i2c_hid_shutdown, |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 1272 | .id_table = i2c_hid_id_table, |
| 1273 | }; |
| 1274 | |
| 1275 | module_i2c_driver(i2c_hid_driver); |
| 1276 | |
| 1277 | MODULE_DESCRIPTION("HID over I2C core driver"); |
| 1278 | MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>"); |
| 1279 | MODULE_LICENSE("GPL"); |