]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/hid/hid-logitech-hidpp.c
HID: quirks: Fix keyboard + touchpad on Lenovo Miix 630
[mirror_ubuntu-bionic-kernel.git] / drivers / hid / hid-logitech-hidpp.c
CommitLineData
2f31c525
BT
1/*
2 * HIDPP protocol for Logitech Unifying receivers
3 *
4 * Copyright (c) 2011 Logitech (c)
5 * Copyright (c) 2012-2013 Google (c)
6 * Copyright (c) 2013-2014 Red Hat Inc.
7 */
8
9/*
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the Free
12 * Software Foundation; version 2 of the License.
13 */
14
15#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
16
17#include <linux/device.h>
ff21a635
EV
18#include <linux/input.h>
19#include <linux/usb.h>
2f31c525
BT
20#include <linux/hid.h>
21#include <linux/module.h>
22#include <linux/slab.h>
23#include <linux/sched.h>
24#include <linux/kfifo.h>
25#include <linux/input/mt.h>
ff21a635
EV
26#include <linux/workqueue.h>
27#include <linux/atomic.h>
28#include <linux/fixp-arith.h>
2f31c525 29#include <asm/unaligned.h>
ff21a635 30#include "usbhid/usbhid.h"
2f31c525
BT
31#include "hid-ids.h"
32
33MODULE_LICENSE("GPL");
34MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
35MODULE_AUTHOR("Nestor Lopez Casado <nlopezcasad@logitech.com>");
36
9188dbae
BT
37static bool disable_raw_mode;
38module_param(disable_raw_mode, bool, 0644);
39MODULE_PARM_DESC(disable_raw_mode,
40 "Disable Raw mode reporting for touchpads and keep firmware gestures.");
41
90cdd986
BT
42static bool disable_tap_to_click;
43module_param(disable_tap_to_click, bool, 0644);
44MODULE_PARM_DESC(disable_tap_to_click,
45 "Disable Tap-To-Click mode reporting for touchpads (only on the K400 currently).");
46
2f31c525
BT
47#define REPORT_ID_HIDPP_SHORT 0x10
48#define REPORT_ID_HIDPP_LONG 0x11
a5ce8f5b 49#define REPORT_ID_HIDPP_VERY_LONG 0x12
2f31c525
BT
50
51#define HIDPP_REPORT_SHORT_LENGTH 7
52#define HIDPP_REPORT_LONG_LENGTH 20
a5ce8f5b 53#define HIDPP_REPORT_VERY_LONG_LENGTH 64
2f31c525
BT
54
55#define HIDPP_QUIRK_CLASS_WTP BIT(0)
8a09b4fa 56#define HIDPP_QUIRK_CLASS_M560 BIT(1)
90cdd986 57#define HIDPP_QUIRK_CLASS_K400 BIT(2)
7bfd2927 58#define HIDPP_QUIRK_CLASS_G920 BIT(3)
696ecef9 59#define HIDPP_QUIRK_CLASS_K750 BIT(4)
2f31c525 60
8a09b4fa 61/* bits 2..20 are reserved for classes */
6bd4e65d 62/* #define HIDPP_QUIRK_CONNECT_EVENTS BIT(21) disabled */
57ac86cf 63#define HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS BIT(22)
580a7e82 64#define HIDPP_QUIRK_NO_HIDINPUT BIT(23)
7bfd2927 65#define HIDPP_QUIRK_FORCE_OUTPUT_REPORTS BIT(24)
843c624e 66#define HIDPP_QUIRK_UNIFYING BIT(25)
580a7e82 67
6bd4e65d 68#define HIDPP_QUIRK_DELAYED_INIT HIDPP_QUIRK_NO_HIDINPUT
c39e3d5f 69
206d7c68
BT
70#define HIDPP_CAPABILITY_HIDPP10_BATTERY BIT(0)
71#define HIDPP_CAPABILITY_HIDPP20_BATTERY BIT(1)
5b036ea1
BT
72#define HIDPP_CAPABILITY_BATTERY_MILEAGE BIT(2)
73#define HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS BIT(3)
206d7c68 74
2f31c525
BT
75/*
76 * There are two hidpp protocols in use, the first version hidpp10 is known
77 * as register access protocol or RAP, the second version hidpp20 is known as
78 * feature access protocol or FAP
79 *
80 * Most older devices (including the Unifying usb receiver) use the RAP protocol
81 * where as most newer devices use the FAP protocol. Both protocols are
82 * compatible with the underlying transport, which could be usb, Unifiying, or
83 * bluetooth. The message lengths are defined by the hid vendor specific report
84 * descriptor for the HIDPP_SHORT report type (total message lenth 7 bytes) and
85 * the HIDPP_LONG report type (total message length 20 bytes)
86 *
87 * The RAP protocol uses both report types, whereas the FAP only uses HIDPP_LONG
88 * messages. The Unifying receiver itself responds to RAP messages (device index
89 * is 0xFF for the receiver), and all messages (short or long) with a device
90 * index between 1 and 6 are passed untouched to the corresponding paired
91 * Unifying device.
92 *
93 * The paired device can be RAP or FAP, it will receive the message untouched
94 * from the Unifiying receiver.
95 */
96
97struct fap {
98 u8 feature_index;
99 u8 funcindex_clientid;
a5ce8f5b 100 u8 params[HIDPP_REPORT_VERY_LONG_LENGTH - 4U];
2f31c525
BT
101};
102
103struct rap {
104 u8 sub_id;
105 u8 reg_address;
a5ce8f5b 106 u8 params[HIDPP_REPORT_VERY_LONG_LENGTH - 4U];
2f31c525
BT
107};
108
109struct hidpp_report {
110 u8 report_id;
111 u8 device_index;
112 union {
113 struct fap fap;
114 struct rap rap;
115 u8 rawbytes[sizeof(struct fap)];
116 };
117} __packed;
118
5a2b190c
PH
119struct hidpp_battery {
120 u8 feature_index;
696ecef9 121 u8 solar_feature_index;
5a2b190c
PH
122 struct power_supply_desc desc;
123 struct power_supply *ps;
124 char name[64];
125 int status;
14f437a1 126 int capacity;
5b036ea1 127 int level;
284f8d75 128 bool online;
5a2b190c
PH
129};
130
2f31c525
BT
131struct hidpp_device {
132 struct hid_device *hid_dev;
133 struct mutex send_mutex;
134 void *send_receive_buf;
005b3f57 135 char *name; /* will never be NULL and should not be freed */
2f31c525
BT
136 wait_queue_head_t wait;
137 bool answer_available;
138 u8 protocol_major;
139 u8 protocol_minor;
140
141 void *private_data;
142
c39e3d5f
BT
143 struct work_struct work;
144 struct kfifo delayed_work_fifo;
145 atomic_t connected;
146 struct input_dev *delayed_input;
147
2f31c525 148 unsigned long quirks;
206d7c68 149 unsigned long capabilities;
2f31c525 150
5a2b190c
PH
151 struct hidpp_battery battery;
152};
2f31c525 153
f677bb15 154/* HID++ 1.0 error codes */
2f31c525
BT
155#define HIDPP_ERROR 0x8f
156#define HIDPP_ERROR_SUCCESS 0x00
157#define HIDPP_ERROR_INVALID_SUBID 0x01
158#define HIDPP_ERROR_INVALID_ADRESS 0x02
159#define HIDPP_ERROR_INVALID_VALUE 0x03
160#define HIDPP_ERROR_CONNECT_FAIL 0x04
161#define HIDPP_ERROR_TOO_MANY_DEVICES 0x05
162#define HIDPP_ERROR_ALREADY_EXISTS 0x06
163#define HIDPP_ERROR_BUSY 0x07
164#define HIDPP_ERROR_UNKNOWN_DEVICE 0x08
165#define HIDPP_ERROR_RESOURCE_ERROR 0x09
166#define HIDPP_ERROR_REQUEST_UNAVAILABLE 0x0a
167#define HIDPP_ERROR_INVALID_PARAM_VALUE 0x0b
168#define HIDPP_ERROR_WRONG_PIN_CODE 0x0c
f677bb15
PW
169/* HID++ 2.0 error codes */
170#define HIDPP20_ERROR 0xff
2f31c525 171
c39e3d5f
BT
172static void hidpp_connect_event(struct hidpp_device *hidpp_dev);
173
2f31c525
BT
174static int __hidpp_send_report(struct hid_device *hdev,
175 struct hidpp_report *hidpp_report)
176{
7bfd2927 177 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2f31c525
BT
178 int fields_count, ret;
179
7bfd2927
SW
180 hidpp = hid_get_drvdata(hdev);
181
2f31c525
BT
182 switch (hidpp_report->report_id) {
183 case REPORT_ID_HIDPP_SHORT:
184 fields_count = HIDPP_REPORT_SHORT_LENGTH;
185 break;
186 case REPORT_ID_HIDPP_LONG:
187 fields_count = HIDPP_REPORT_LONG_LENGTH;
188 break;
a5ce8f5b
SW
189 case REPORT_ID_HIDPP_VERY_LONG:
190 fields_count = HIDPP_REPORT_VERY_LONG_LENGTH;
191 break;
2f31c525
BT
192 default:
193 return -ENODEV;
194 }
195
196 /*
197 * set the device_index as the receiver, it will be overwritten by
198 * hid_hw_request if needed
199 */
200 hidpp_report->device_index = 0xff;
201
7bfd2927
SW
202 if (hidpp->quirks & HIDPP_QUIRK_FORCE_OUTPUT_REPORTS) {
203 ret = hid_hw_output_report(hdev, (u8 *)hidpp_report, fields_count);
204 } else {
205 ret = hid_hw_raw_request(hdev, hidpp_report->report_id,
206 (u8 *)hidpp_report, fields_count, HID_OUTPUT_REPORT,
207 HID_REQ_SET_REPORT);
208 }
2f31c525
BT
209
210 return ret == fields_count ? 0 : -1;
211}
212
8c9952b2
BT
213/**
214 * hidpp_send_message_sync() returns 0 in case of success, and something else
215 * in case of a failure.
216 * - If ' something else' is positive, that means that an error has been raised
217 * by the protocol itself.
218 * - If ' something else' is negative, that means that we had a classic error
219 * (-ENOMEM, -EPIPE, etc...)
220 */
2f31c525
BT
221static int hidpp_send_message_sync(struct hidpp_device *hidpp,
222 struct hidpp_report *message,
223 struct hidpp_report *response)
224{
225 int ret;
226
227 mutex_lock(&hidpp->send_mutex);
228
229 hidpp->send_receive_buf = response;
230 hidpp->answer_available = false;
231
232 /*
233 * So that we can later validate the answer when it arrives
234 * in hidpp_raw_event
235 */
236 *response = *message;
237
238 ret = __hidpp_send_report(hidpp->hid_dev, message);
239
240 if (ret) {
241 dbg_hid("__hidpp_send_report returned err: %d\n", ret);
242 memset(response, 0, sizeof(struct hidpp_report));
243 goto exit;
244 }
245
246 if (!wait_event_timeout(hidpp->wait, hidpp->answer_available,
247 5*HZ)) {
248 dbg_hid("%s:timeout waiting for response\n", __func__);
249 memset(response, 0, sizeof(struct hidpp_report));
250 ret = -ETIMEDOUT;
251 }
252
253 if (response->report_id == REPORT_ID_HIDPP_SHORT &&
f677bb15
PW
254 response->rap.sub_id == HIDPP_ERROR) {
255 ret = response->rap.params[1];
256 dbg_hid("%s:got hidpp error %02X\n", __func__, ret);
257 goto exit;
258 }
259
a5ce8f5b
SW
260 if ((response->report_id == REPORT_ID_HIDPP_LONG ||
261 response->report_id == REPORT_ID_HIDPP_VERY_LONG) &&
262 response->fap.feature_index == HIDPP20_ERROR) {
2f31c525 263 ret = response->fap.params[1];
f677bb15 264 dbg_hid("%s:got hidpp 2.0 error %02X\n", __func__, ret);
2f31c525
BT
265 goto exit;
266 }
267
268exit:
269 mutex_unlock(&hidpp->send_mutex);
270 return ret;
271
272}
273
274static int hidpp_send_fap_command_sync(struct hidpp_device *hidpp,
275 u8 feat_index, u8 funcindex_clientid, u8 *params, int param_count,
276 struct hidpp_report *response)
277{
3e7830ce 278 struct hidpp_report *message;
2f31c525
BT
279 int ret;
280
281 if (param_count > sizeof(message->fap.params))
282 return -EINVAL;
283
3e7830ce
DC
284 message = kzalloc(sizeof(struct hidpp_report), GFP_KERNEL);
285 if (!message)
286 return -ENOMEM;
a5ce8f5b
SW
287
288 if (param_count > (HIDPP_REPORT_LONG_LENGTH - 4))
289 message->report_id = REPORT_ID_HIDPP_VERY_LONG;
290 else
291 message->report_id = REPORT_ID_HIDPP_LONG;
2f31c525
BT
292 message->fap.feature_index = feat_index;
293 message->fap.funcindex_clientid = funcindex_clientid;
294 memcpy(&message->fap.params, params, param_count);
295
296 ret = hidpp_send_message_sync(hidpp, message, response);
297 kfree(message);
298 return ret;
299}
300
33797820
BT
301static int hidpp_send_rap_command_sync(struct hidpp_device *hidpp_dev,
302 u8 report_id, u8 sub_id, u8 reg_address, u8 *params, int param_count,
303 struct hidpp_report *response)
304{
3e7830ce 305 struct hidpp_report *message;
a5ce8f5b 306 int ret, max_count;
33797820 307
a5ce8f5b
SW
308 switch (report_id) {
309 case REPORT_ID_HIDPP_SHORT:
310 max_count = HIDPP_REPORT_SHORT_LENGTH - 4;
311 break;
312 case REPORT_ID_HIDPP_LONG:
313 max_count = HIDPP_REPORT_LONG_LENGTH - 4;
314 break;
315 case REPORT_ID_HIDPP_VERY_LONG:
316 max_count = HIDPP_REPORT_VERY_LONG_LENGTH - 4;
317 break;
318 default:
33797820 319 return -EINVAL;
a5ce8f5b 320 }
33797820 321
a5ce8f5b 322 if (param_count > max_count)
33797820
BT
323 return -EINVAL;
324
3e7830ce
DC
325 message = kzalloc(sizeof(struct hidpp_report), GFP_KERNEL);
326 if (!message)
327 return -ENOMEM;
33797820
BT
328 message->report_id = report_id;
329 message->rap.sub_id = sub_id;
330 message->rap.reg_address = reg_address;
331 memcpy(&message->rap.params, params, param_count);
332
333 ret = hidpp_send_message_sync(hidpp_dev, message, response);
334 kfree(message);
335 return ret;
336}
337
c39e3d5f
BT
338static void delayed_work_cb(struct work_struct *work)
339{
340 struct hidpp_device *hidpp = container_of(work, struct hidpp_device,
341 work);
342 hidpp_connect_event(hidpp);
343}
344
2f31c525
BT
345static inline bool hidpp_match_answer(struct hidpp_report *question,
346 struct hidpp_report *answer)
347{
348 return (answer->fap.feature_index == question->fap.feature_index) &&
349 (answer->fap.funcindex_clientid == question->fap.funcindex_clientid);
350}
351
352static inline bool hidpp_match_error(struct hidpp_report *question,
353 struct hidpp_report *answer)
354{
f677bb15
PW
355 return ((answer->rap.sub_id == HIDPP_ERROR) ||
356 (answer->fap.feature_index == HIDPP20_ERROR)) &&
2f31c525
BT
357 (answer->fap.funcindex_clientid == question->fap.feature_index) &&
358 (answer->fap.params[0] == question->fap.funcindex_clientid);
359}
360
c39e3d5f
BT
361static inline bool hidpp_report_is_connect_event(struct hidpp_report *report)
362{
363 return (report->report_id == REPORT_ID_HIDPP_SHORT) &&
364 (report->rap.sub_id == 0x41);
365}
366
a0e625f8
BT
367/**
368 * hidpp_prefix_name() prefixes the current given name with "Logitech ".
369 */
370static void hidpp_prefix_name(char **name, int name_length)
371{
372#define PREFIX_LENGTH 9 /* "Logitech " */
373
374 int new_length;
375 char *new_name;
376
377 if (name_length > PREFIX_LENGTH &&
378 strncmp(*name, "Logitech ", PREFIX_LENGTH) == 0)
379 /* The prefix has is already in the name */
380 return;
381
382 new_length = PREFIX_LENGTH + name_length;
383 new_name = kzalloc(new_length, GFP_KERNEL);
384 if (!new_name)
385 return;
386
387 snprintf(new_name, new_length, "Logitech %s", *name);
388
389 kfree(*name);
390
391 *name = new_name;
392}
393
33797820
BT
394/* -------------------------------------------------------------------------- */
395/* HIDP++ 1.0 commands */
396/* -------------------------------------------------------------------------- */
397
398#define HIDPP_SET_REGISTER 0x80
399#define HIDPP_GET_REGISTER 0x81
400#define HIDPP_SET_LONG_REGISTER 0x82
401#define HIDPP_GET_LONG_REGISTER 0x83
402
7f7ce2a2
BT
403#define HIDPP_REG_GENERAL 0x00
404
405static int hidpp10_enable_battery_reporting(struct hidpp_device *hidpp_dev)
406{
407 struct hidpp_report response;
408 int ret;
409 u8 params[3] = { 0 };
410
411 ret = hidpp_send_rap_command_sync(hidpp_dev,
412 REPORT_ID_HIDPP_SHORT,
413 HIDPP_GET_REGISTER,
414 HIDPP_REG_GENERAL,
415 NULL, 0, &response);
416 if (ret)
417 return ret;
418
419 memcpy(params, response.rap.params, 3);
420
421 /* Set the battery bit */
422 params[0] |= BIT(4);
423
424 return hidpp_send_rap_command_sync(hidpp_dev,
425 REPORT_ID_HIDPP_SHORT,
426 HIDPP_SET_REGISTER,
427 HIDPP_REG_GENERAL,
428 params, 3, &response);
429}
430
431#define HIDPP_REG_BATTERY_STATUS 0x07
432
433static int hidpp10_battery_status_map_level(u8 param)
434{
435 int level;
436
437 switch (param) {
438 case 1 ... 2:
439 level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
440 break;
441 case 3 ... 4:
442 level = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
443 break;
444 case 5 ... 6:
445 level = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
446 break;
447 case 7:
448 level = POWER_SUPPLY_CAPACITY_LEVEL_HIGH;
449 break;
450 default:
451 level = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
452 }
453
454 return level;
455}
456
457static int hidpp10_battery_status_map_status(u8 param)
458{
459 int status;
460
461 switch (param) {
462 case 0x00:
463 /* discharging (in use) */
464 status = POWER_SUPPLY_STATUS_DISCHARGING;
465 break;
466 case 0x21: /* (standard) charging */
467 case 0x24: /* fast charging */
468 case 0x25: /* slow charging */
469 status = POWER_SUPPLY_STATUS_CHARGING;
470 break;
471 case 0x26: /* topping charge */
472 case 0x22: /* charge complete */
473 status = POWER_SUPPLY_STATUS_FULL;
474 break;
475 case 0x20: /* unknown */
476 status = POWER_SUPPLY_STATUS_UNKNOWN;
477 break;
478 /*
479 * 0x01...0x1F = reserved (not charging)
480 * 0x23 = charging error
481 * 0x27..0xff = reserved
482 */
483 default:
484 status = POWER_SUPPLY_STATUS_NOT_CHARGING;
485 break;
486 }
487
488 return status;
489}
490
491static int hidpp10_query_battery_status(struct hidpp_device *hidpp)
492{
493 struct hidpp_report response;
494 int ret, status;
495
496 ret = hidpp_send_rap_command_sync(hidpp,
497 REPORT_ID_HIDPP_SHORT,
498 HIDPP_GET_REGISTER,
499 HIDPP_REG_BATTERY_STATUS,
500 NULL, 0, &response);
501 if (ret)
502 return ret;
503
504 hidpp->battery.level =
505 hidpp10_battery_status_map_level(response.rap.params[0]);
506 status = hidpp10_battery_status_map_status(response.rap.params[1]);
507 hidpp->battery.status = status;
508 /* the capacity is only available when discharging or full */
509 hidpp->battery.online = status == POWER_SUPPLY_STATUS_DISCHARGING ||
510 status == POWER_SUPPLY_STATUS_FULL;
511
512 return 0;
513}
514
515#define HIDPP_REG_BATTERY_MILEAGE 0x0D
516
517static int hidpp10_battery_mileage_map_status(u8 param)
518{
519 int status;
520
521 switch (param >> 6) {
522 case 0x00:
523 /* discharging (in use) */
524 status = POWER_SUPPLY_STATUS_DISCHARGING;
525 break;
526 case 0x01: /* charging */
527 status = POWER_SUPPLY_STATUS_CHARGING;
528 break;
529 case 0x02: /* charge complete */
530 status = POWER_SUPPLY_STATUS_FULL;
531 break;
532 /*
533 * 0x03 = charging error
534 */
535 default:
536 status = POWER_SUPPLY_STATUS_NOT_CHARGING;
537 break;
538 }
539
540 return status;
541}
542
543static int hidpp10_query_battery_mileage(struct hidpp_device *hidpp)
544{
545 struct hidpp_report response;
546 int ret, status;
547
548 ret = hidpp_send_rap_command_sync(hidpp,
549 REPORT_ID_HIDPP_SHORT,
550 HIDPP_GET_REGISTER,
551 HIDPP_REG_BATTERY_MILEAGE,
552 NULL, 0, &response);
553 if (ret)
554 return ret;
555
556 hidpp->battery.capacity = response.rap.params[0];
557 status = hidpp10_battery_mileage_map_status(response.rap.params[2]);
558 hidpp->battery.status = status;
559 /* the capacity is only available when discharging or full */
560 hidpp->battery.online = status == POWER_SUPPLY_STATUS_DISCHARGING ||
561 status == POWER_SUPPLY_STATUS_FULL;
562
563 return 0;
564}
565
566static int hidpp10_battery_event(struct hidpp_device *hidpp, u8 *data, int size)
567{
568 struct hidpp_report *report = (struct hidpp_report *)data;
569 int status, capacity, level;
570 bool changed;
571
572 if (report->report_id != REPORT_ID_HIDPP_SHORT)
573 return 0;
574
575 switch (report->rap.sub_id) {
576 case HIDPP_REG_BATTERY_STATUS:
577 capacity = hidpp->battery.capacity;
578 level = hidpp10_battery_status_map_level(report->rawbytes[1]);
579 status = hidpp10_battery_status_map_status(report->rawbytes[2]);
580 break;
581 case HIDPP_REG_BATTERY_MILEAGE:
582 capacity = report->rap.params[0];
583 level = hidpp->battery.level;
584 status = hidpp10_battery_mileage_map_status(report->rawbytes[3]);
585 break;
586 default:
587 return 0;
588 }
589
590 changed = capacity != hidpp->battery.capacity ||
591 level != hidpp->battery.level ||
592 status != hidpp->battery.status;
593
594 /* the capacity is only available when discharging or full */
595 hidpp->battery.online = status == POWER_SUPPLY_STATUS_DISCHARGING ||
596 status == POWER_SUPPLY_STATUS_FULL;
597
598 if (changed) {
599 hidpp->battery.level = level;
600 hidpp->battery.status = status;
601 if (hidpp->battery.ps)
602 power_supply_changed(hidpp->battery.ps);
603 }
604
605 return 0;
606}
607
33797820 608#define HIDPP_REG_PAIRING_INFORMATION 0xB5
843c624e
BT
609#define HIDPP_EXTENDED_PAIRING 0x30
610#define HIDPP_DEVICE_NAME 0x40
33797820 611
843c624e 612static char *hidpp_unifying_get_name(struct hidpp_device *hidpp_dev)
33797820
BT
613{
614 struct hidpp_report response;
615 int ret;
843c624e 616 u8 params[1] = { HIDPP_DEVICE_NAME };
33797820
BT
617 char *name;
618 int len;
619
620 ret = hidpp_send_rap_command_sync(hidpp_dev,
621 REPORT_ID_HIDPP_SHORT,
622 HIDPP_GET_LONG_REGISTER,
623 HIDPP_REG_PAIRING_INFORMATION,
624 params, 1, &response);
625 if (ret)
626 return NULL;
627
628 len = response.rap.params[1];
629
3a034a7a
PW
630 if (2 + len > sizeof(response.rap.params))
631 return NULL;
632
33797820
BT
633 name = kzalloc(len + 1, GFP_KERNEL);
634 if (!name)
635 return NULL;
636
637 memcpy(name, &response.rap.params[2], len);
a0e625f8
BT
638
639 /* include the terminating '\0' */
640 hidpp_prefix_name(&name, len + 1);
641
33797820
BT
642 return name;
643}
644
843c624e
BT
645static int hidpp_unifying_get_serial(struct hidpp_device *hidpp, u32 *serial)
646{
647 struct hidpp_report response;
648 int ret;
649 u8 params[1] = { HIDPP_EXTENDED_PAIRING };
650
651 ret = hidpp_send_rap_command_sync(hidpp,
652 REPORT_ID_HIDPP_SHORT,
653 HIDPP_GET_LONG_REGISTER,
654 HIDPP_REG_PAIRING_INFORMATION,
655 params, 1, &response);
656 if (ret)
657 return ret;
658
659 /*
660 * We don't care about LE or BE, we will output it as a string
661 * with %4phD, so we need to keep the order.
662 */
663 *serial = *((u32 *)&response.rap.params[1]);
664 return 0;
665}
666
667static int hidpp_unifying_init(struct hidpp_device *hidpp)
668{
669 struct hid_device *hdev = hidpp->hid_dev;
670 const char *name;
671 u32 serial;
672 int ret;
673
674 ret = hidpp_unifying_get_serial(hidpp, &serial);
675 if (ret)
676 return ret;
677
678 snprintf(hdev->uniq, sizeof(hdev->uniq), "%04x-%4phD",
679 hdev->product, &serial);
680 dbg_hid("HID++ Unifying: Got serial: %s\n", hdev->uniq);
681
682 name = hidpp_unifying_get_name(hidpp);
683 if (!name)
684 return -EIO;
685
686 snprintf(hdev->name, sizeof(hdev->name), "%s", name);
687 dbg_hid("HID++ Unifying: Got name: %s\n", name);
688
689 kfree(name);
690 return 0;
691}
692
2f31c525
BT
693/* -------------------------------------------------------------------------- */
694/* 0x0000: Root */
695/* -------------------------------------------------------------------------- */
696
697#define HIDPP_PAGE_ROOT 0x0000
698#define HIDPP_PAGE_ROOT_IDX 0x00
699
700#define CMD_ROOT_GET_FEATURE 0x01
701#define CMD_ROOT_GET_PROTOCOL_VERSION 0x11
702
703static int hidpp_root_get_feature(struct hidpp_device *hidpp, u16 feature,
704 u8 *feature_index, u8 *feature_type)
705{
706 struct hidpp_report response;
707 int ret;
708 u8 params[2] = { feature >> 8, feature & 0x00FF };
709
710 ret = hidpp_send_fap_command_sync(hidpp,
711 HIDPP_PAGE_ROOT_IDX,
712 CMD_ROOT_GET_FEATURE,
713 params, 2, &response);
714 if (ret)
715 return ret;
716
a9525b80
BT
717 if (response.fap.params[0] == 0)
718 return -ENOENT;
719
2f31c525
BT
720 *feature_index = response.fap.params[0];
721 *feature_type = response.fap.params[1];
722
723 return ret;
724}
725
726static int hidpp_root_get_protocol_version(struct hidpp_device *hidpp)
727{
88ca3195
HG
728 const u8 ping_byte = 0x5a;
729 u8 ping_data[3] = { 0, 0, ping_byte };
2f31c525
BT
730 struct hidpp_report response;
731 int ret;
732
88ca3195
HG
733 ret = hidpp_send_rap_command_sync(hidpp,
734 REPORT_ID_HIDPP_SHORT,
2f31c525
BT
735 HIDPP_PAGE_ROOT_IDX,
736 CMD_ROOT_GET_PROTOCOL_VERSION,
88ca3195 737 ping_data, sizeof(ping_data), &response);
2f31c525 738
552f12eb 739 if (ret == HIDPP_ERROR_INVALID_SUBID) {
2f31c525
BT
740 hidpp->protocol_major = 1;
741 hidpp->protocol_minor = 0;
742 return 0;
743 }
744
552f12eb
BT
745 /* the device might not be connected */
746 if (ret == HIDPP_ERROR_RESOURCE_ERROR)
747 return -EIO;
748
8c9952b2
BT
749 if (ret > 0) {
750 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
751 __func__, ret);
752 return -EPROTO;
753 }
2f31c525 754 if (ret)
8c9952b2 755 return ret;
2f31c525 756
88ca3195
HG
757 if (response.rap.params[2] != ping_byte) {
758 hid_err(hidpp->hid_dev, "%s: ping mismatch 0x%02x != 0x%02x\n",
759 __func__, response.rap.params[2], ping_byte);
760 return -EPROTO;
761 }
762
763 hidpp->protocol_major = response.rap.params[0];
764 hidpp->protocol_minor = response.rap.params[1];
2f31c525
BT
765
766 return ret;
767}
768
769static bool hidpp_is_connected(struct hidpp_device *hidpp)
770{
771 int ret;
772
773 ret = hidpp_root_get_protocol_version(hidpp);
774 if (!ret)
775 hid_dbg(hidpp->hid_dev, "HID++ %u.%u device connected.\n",
776 hidpp->protocol_major, hidpp->protocol_minor);
777 return ret == 0;
778}
779
780/* -------------------------------------------------------------------------- */
781/* 0x0005: GetDeviceNameType */
782/* -------------------------------------------------------------------------- */
783
784#define HIDPP_PAGE_GET_DEVICE_NAME_TYPE 0x0005
785
786#define CMD_GET_DEVICE_NAME_TYPE_GET_COUNT 0x01
787#define CMD_GET_DEVICE_NAME_TYPE_GET_DEVICE_NAME 0x11
788#define CMD_GET_DEVICE_NAME_TYPE_GET_TYPE 0x21
789
790static int hidpp_devicenametype_get_count(struct hidpp_device *hidpp,
791 u8 feature_index, u8 *nameLength)
792{
793 struct hidpp_report response;
794 int ret;
795
796 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
797 CMD_GET_DEVICE_NAME_TYPE_GET_COUNT, NULL, 0, &response);
798
8c9952b2
BT
799 if (ret > 0) {
800 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
801 __func__, ret);
802 return -EPROTO;
803 }
2f31c525 804 if (ret)
8c9952b2 805 return ret;
2f31c525
BT
806
807 *nameLength = response.fap.params[0];
808
809 return ret;
810}
811
812static int hidpp_devicenametype_get_device_name(struct hidpp_device *hidpp,
813 u8 feature_index, u8 char_index, char *device_name, int len_buf)
814{
815 struct hidpp_report response;
816 int ret, i;
817 int count;
818
819 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
820 CMD_GET_DEVICE_NAME_TYPE_GET_DEVICE_NAME, &char_index, 1,
821 &response);
822
8c9952b2
BT
823 if (ret > 0) {
824 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
825 __func__, ret);
826 return -EPROTO;
827 }
2f31c525 828 if (ret)
8c9952b2 829 return ret;
2f31c525 830
a5ce8f5b
SW
831 switch (response.report_id) {
832 case REPORT_ID_HIDPP_VERY_LONG:
833 count = HIDPP_REPORT_VERY_LONG_LENGTH - 4;
834 break;
835 case REPORT_ID_HIDPP_LONG:
2f31c525 836 count = HIDPP_REPORT_LONG_LENGTH - 4;
a5ce8f5b
SW
837 break;
838 case REPORT_ID_HIDPP_SHORT:
2f31c525 839 count = HIDPP_REPORT_SHORT_LENGTH - 4;
a5ce8f5b
SW
840 break;
841 default:
842 return -EPROTO;
843 }
2f31c525
BT
844
845 if (len_buf < count)
846 count = len_buf;
847
848 for (i = 0; i < count; i++)
849 device_name[i] = response.fap.params[i];
850
851 return count;
852}
853
02cc097e 854static char *hidpp_get_device_name(struct hidpp_device *hidpp)
2f31c525
BT
855{
856 u8 feature_type;
857 u8 feature_index;
858 u8 __name_length;
859 char *name;
860 unsigned index = 0;
861 int ret;
862
863 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_GET_DEVICE_NAME_TYPE,
864 &feature_index, &feature_type);
865 if (ret)
02cc097e 866 return NULL;
2f31c525
BT
867
868 ret = hidpp_devicenametype_get_count(hidpp, feature_index,
869 &__name_length);
870 if (ret)
02cc097e 871 return NULL;
2f31c525
BT
872
873 name = kzalloc(__name_length + 1, GFP_KERNEL);
874 if (!name)
02cc097e 875 return NULL;
2f31c525 876
1430ee73
PW
877 while (index < __name_length) {
878 ret = hidpp_devicenametype_get_device_name(hidpp,
2f31c525
BT
879 feature_index, index, name + index,
880 __name_length - index);
1430ee73
PW
881 if (ret <= 0) {
882 kfree(name);
883 return NULL;
884 }
885 index += ret;
886 }
2f31c525 887
a0e625f8
BT
888 /* include the terminating '\0' */
889 hidpp_prefix_name(&name, __name_length + 1);
890
2f31c525 891 return name;
2f31c525
BT
892}
893
5a2b190c
PH
894/* -------------------------------------------------------------------------- */
895/* 0x1000: Battery level status */
896/* -------------------------------------------------------------------------- */
897
898#define HIDPP_PAGE_BATTERY_LEVEL_STATUS 0x1000
899
900#define CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_LEVEL_STATUS 0x00
901#define CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_CAPABILITY 0x10
902
903#define EVENT_BATTERY_LEVEL_STATUS_BROADCAST 0x00
904
5b036ea1
BT
905#define FLAG_BATTERY_LEVEL_DISABLE_OSD BIT(0)
906#define FLAG_BATTERY_LEVEL_MILEAGE BIT(1)
907#define FLAG_BATTERY_LEVEL_RECHARGEABLE BIT(2)
908
909static int hidpp_map_battery_level(int capacity)
910{
911 if (capacity < 11)
912 return POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
906cf06e
HG
913 /*
914 * The spec says this should be < 31 but some devices report 30
915 * with brand new batteries and Windows reports 30 as "Good".
916 */
917 else if (capacity < 30)
5b036ea1
BT
918 return POWER_SUPPLY_CAPACITY_LEVEL_LOW;
919 else if (capacity < 81)
920 return POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
921 return POWER_SUPPLY_CAPACITY_LEVEL_FULL;
922}
923
14f437a1 924static int hidpp20_batterylevel_map_status_capacity(u8 data[3], int *capacity,
5b036ea1
BT
925 int *next_capacity,
926 int *level)
5a2b190c
PH
927{
928 int status;
5a2b190c 929
14f437a1
BT
930 *capacity = data[0];
931 *next_capacity = data[1];
5b036ea1 932 *level = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
5a2b190c 933
14f437a1
BT
934 /* When discharging, we can rely on the device reported capacity.
935 * For all other states the device reports 0 (unknown).
5a2b190c
PH
936 */
937 switch (data[2]) {
938 case 0: /* discharging (in use) */
939 status = POWER_SUPPLY_STATUS_DISCHARGING;
5b036ea1 940 *level = hidpp_map_battery_level(*capacity);
5a2b190c
PH
941 break;
942 case 1: /* recharging */
943 status = POWER_SUPPLY_STATUS_CHARGING;
5a2b190c
PH
944 break;
945 case 2: /* charge in final stage */
946 status = POWER_SUPPLY_STATUS_CHARGING;
5a2b190c
PH
947 break;
948 case 3: /* charge complete */
949 status = POWER_SUPPLY_STATUS_FULL;
5b036ea1 950 *level = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
14f437a1 951 *capacity = 100;
5a2b190c
PH
952 break;
953 case 4: /* recharging below optimal speed */
954 status = POWER_SUPPLY_STATUS_CHARGING;
5a2b190c
PH
955 break;
956 /* 5 = invalid battery type
957 6 = thermal error
958 7 = other charging error */
959 default:
960 status = POWER_SUPPLY_STATUS_NOT_CHARGING;
5a2b190c
PH
961 break;
962 }
963
5a2b190c
PH
964 return status;
965}
966
14f437a1
BT
967static int hidpp20_batterylevel_get_battery_capacity(struct hidpp_device *hidpp,
968 u8 feature_index,
969 int *status,
970 int *capacity,
5b036ea1
BT
971 int *next_capacity,
972 int *level)
5a2b190c
PH
973{
974 struct hidpp_report response;
975 int ret;
976 u8 *params = (u8 *)response.fap.params;
977
978 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
979 CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_LEVEL_STATUS,
980 NULL, 0, &response);
981 if (ret > 0) {
982 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
983 __func__, ret);
984 return -EPROTO;
985 }
986 if (ret)
987 return ret;
988
14f437a1 989 *status = hidpp20_batterylevel_map_status_capacity(params, capacity,
5b036ea1
BT
990 next_capacity,
991 level);
992
993 return 0;
994}
995
996static int hidpp20_batterylevel_get_battery_info(struct hidpp_device *hidpp,
997 u8 feature_index)
998{
999 struct hidpp_report response;
1000 int ret;
1001 u8 *params = (u8 *)response.fap.params;
1002 unsigned int level_count, flags;
1003
1004 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1005 CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_CAPABILITY,
1006 NULL, 0, &response);
1007 if (ret > 0) {
1008 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1009 __func__, ret);
1010 return -EPROTO;
1011 }
1012 if (ret)
1013 return ret;
1014
1015 level_count = params[0];
1016 flags = params[1];
1017
1018 if (level_count < 10 || !(flags & FLAG_BATTERY_LEVEL_MILEAGE))
1019 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS;
1020 else
1021 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_MILEAGE;
5a2b190c
PH
1022
1023 return 0;
1024}
1025
1026static int hidpp20_query_battery_info(struct hidpp_device *hidpp)
1027{
1028 u8 feature_type;
1029 int ret;
5b036ea1 1030 int status, capacity, next_capacity, level;
5a2b190c 1031
696ecef9 1032 if (hidpp->battery.feature_index == 0xff) {
5a2b190c
PH
1033 ret = hidpp_root_get_feature(hidpp,
1034 HIDPP_PAGE_BATTERY_LEVEL_STATUS,
1035 &hidpp->battery.feature_index,
1036 &feature_type);
1037 if (ret)
1038 return ret;
1039 }
1040
14f437a1
BT
1041 ret = hidpp20_batterylevel_get_battery_capacity(hidpp,
1042 hidpp->battery.feature_index,
1043 &status, &capacity,
5b036ea1
BT
1044 &next_capacity, &level);
1045 if (ret)
1046 return ret;
1047
1048 ret = hidpp20_batterylevel_get_battery_info(hidpp,
1049 hidpp->battery.feature_index);
5a2b190c
PH
1050 if (ret)
1051 return ret;
1052
1053 hidpp->battery.status = status;
14f437a1 1054 hidpp->battery.capacity = capacity;
5b036ea1 1055 hidpp->battery.level = level;
284f8d75
BT
1056 /* the capacity is only available when discharging or full */
1057 hidpp->battery.online = status == POWER_SUPPLY_STATUS_DISCHARGING ||
1058 status == POWER_SUPPLY_STATUS_FULL;
5a2b190c
PH
1059
1060 return 0;
1061}
1062
1063static int hidpp20_battery_event(struct hidpp_device *hidpp,
1064 u8 *data, int size)
1065{
1066 struct hidpp_report *report = (struct hidpp_report *)data;
5b036ea1 1067 int status, capacity, next_capacity, level;
5a2b190c
PH
1068 bool changed;
1069
1070 if (report->fap.feature_index != hidpp->battery.feature_index ||
1071 report->fap.funcindex_clientid != EVENT_BATTERY_LEVEL_STATUS_BROADCAST)
1072 return 0;
1073
14f437a1
BT
1074 status = hidpp20_batterylevel_map_status_capacity(report->fap.params,
1075 &capacity,
5b036ea1
BT
1076 &next_capacity,
1077 &level);
5a2b190c 1078
284f8d75
BT
1079 /* the capacity is only available when discharging or full */
1080 hidpp->battery.online = status == POWER_SUPPLY_STATUS_DISCHARGING ||
1081 status == POWER_SUPPLY_STATUS_FULL;
1082
14f437a1 1083 changed = capacity != hidpp->battery.capacity ||
5b036ea1 1084 level != hidpp->battery.level ||
5a2b190c
PH
1085 status != hidpp->battery.status;
1086
1087 if (changed) {
5b036ea1 1088 hidpp->battery.level = level;
14f437a1 1089 hidpp->battery.capacity = capacity;
5a2b190c
PH
1090 hidpp->battery.status = status;
1091 if (hidpp->battery.ps)
1092 power_supply_changed(hidpp->battery.ps);
1093 }
1094
1095 return 0;
1096}
1097
1098static enum power_supply_property hidpp_battery_props[] = {
284f8d75 1099 POWER_SUPPLY_PROP_ONLINE,
5a2b190c 1100 POWER_SUPPLY_PROP_STATUS,
3861e6ca 1101 POWER_SUPPLY_PROP_SCOPE,
32043d0f
BT
1102 POWER_SUPPLY_PROP_MODEL_NAME,
1103 POWER_SUPPLY_PROP_MANUFACTURER,
1104 POWER_SUPPLY_PROP_SERIAL_NUMBER,
5b036ea1
BT
1105 0, /* placeholder for POWER_SUPPLY_PROP_CAPACITY, */
1106 0, /* placeholder for POWER_SUPPLY_PROP_CAPACITY_LEVEL, */
5a2b190c
PH
1107};
1108
1109static int hidpp_battery_get_property(struct power_supply *psy,
1110 enum power_supply_property psp,
1111 union power_supply_propval *val)
1112{
1113 struct hidpp_device *hidpp = power_supply_get_drvdata(psy);
1114 int ret = 0;
1115
1116 switch(psp) {
1117 case POWER_SUPPLY_PROP_STATUS:
1118 val->intval = hidpp->battery.status;
1119 break;
1120 case POWER_SUPPLY_PROP_CAPACITY:
14f437a1 1121 val->intval = hidpp->battery.capacity;
5a2b190c 1122 break;
5b036ea1
BT
1123 case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
1124 val->intval = hidpp->battery.level;
1125 break;
3861e6ca
BN
1126 case POWER_SUPPLY_PROP_SCOPE:
1127 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
1128 break;
284f8d75
BT
1129 case POWER_SUPPLY_PROP_ONLINE:
1130 val->intval = hidpp->battery.online;
1131 break;
32043d0f
BT
1132 case POWER_SUPPLY_PROP_MODEL_NAME:
1133 if (!strncmp(hidpp->name, "Logitech ", 9))
1134 val->strval = hidpp->name + 9;
1135 else
1136 val->strval = hidpp->name;
1137 break;
1138 case POWER_SUPPLY_PROP_MANUFACTURER:
1139 val->strval = "Logitech";
1140 break;
1141 case POWER_SUPPLY_PROP_SERIAL_NUMBER:
1142 val->strval = hidpp->hid_dev->uniq;
1143 break;
5a2b190c
PH
1144 default:
1145 ret = -EINVAL;
1146 break;
1147 }
1148
1149 return ret;
1150}
1151
696ecef9
BT
1152/* -------------------------------------------------------------------------- */
1153/* 0x4301: Solar Keyboard */
1154/* -------------------------------------------------------------------------- */
1155
1156#define HIDPP_PAGE_SOLAR_KEYBOARD 0x4301
1157
1158#define CMD_SOLAR_SET_LIGHT_MEASURE 0x00
1159
1160#define EVENT_SOLAR_BATTERY_BROADCAST 0x00
1161#define EVENT_SOLAR_BATTERY_LIGHT_MEASURE 0x10
1162#define EVENT_SOLAR_CHECK_LIGHT_BUTTON 0x20
1163
1164static int hidpp_solar_request_battery_event(struct hidpp_device *hidpp)
1165{
1166 struct hidpp_report response;
1167 u8 params[2] = { 1, 1 };
1168 u8 feature_type;
1169 int ret;
1170
1171 if (hidpp->battery.feature_index == 0xff) {
1172 ret = hidpp_root_get_feature(hidpp,
1173 HIDPP_PAGE_SOLAR_KEYBOARD,
1174 &hidpp->battery.solar_feature_index,
1175 &feature_type);
1176 if (ret)
1177 return ret;
1178 }
1179
1180 ret = hidpp_send_fap_command_sync(hidpp,
1181 hidpp->battery.solar_feature_index,
1182 CMD_SOLAR_SET_LIGHT_MEASURE,
1183 params, 2, &response);
1184 if (ret > 0) {
1185 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1186 __func__, ret);
1187 return -EPROTO;
1188 }
1189 if (ret)
1190 return ret;
1191
1192 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_MILEAGE;
1193
1194 return 0;
1195}
1196
1197static int hidpp_solar_battery_event(struct hidpp_device *hidpp,
1198 u8 *data, int size)
1199{
1200 struct hidpp_report *report = (struct hidpp_report *)data;
1201 int capacity, lux, status;
1202 u8 function;
1203
1204 function = report->fap.funcindex_clientid;
1205
1206
1207 if (report->fap.feature_index != hidpp->battery.solar_feature_index ||
1208 !(function == EVENT_SOLAR_BATTERY_BROADCAST ||
1209 function == EVENT_SOLAR_BATTERY_LIGHT_MEASURE ||
1210 function == EVENT_SOLAR_CHECK_LIGHT_BUTTON))
1211 return 0;
1212
1213 capacity = report->fap.params[0];
1214
1215 switch (function) {
1216 case EVENT_SOLAR_BATTERY_LIGHT_MEASURE:
1217 lux = (report->fap.params[1] << 8) | report->fap.params[2];
1218 if (lux > 200)
1219 status = POWER_SUPPLY_STATUS_CHARGING;
1220 else
1221 status = POWER_SUPPLY_STATUS_DISCHARGING;
1222 break;
1223 case EVENT_SOLAR_CHECK_LIGHT_BUTTON:
1224 default:
1225 if (capacity < hidpp->battery.capacity)
1226 status = POWER_SUPPLY_STATUS_DISCHARGING;
1227 else
1228 status = POWER_SUPPLY_STATUS_CHARGING;
1229
1230 }
1231
1232 if (capacity == 100)
1233 status = POWER_SUPPLY_STATUS_FULL;
1234
1235 hidpp->battery.online = true;
1236 if (capacity != hidpp->battery.capacity ||
1237 status != hidpp->battery.status) {
1238 hidpp->battery.capacity = capacity;
1239 hidpp->battery.status = status;
1240 if (hidpp->battery.ps)
1241 power_supply_changed(hidpp->battery.ps);
1242 }
1243
1244 return 0;
1245}
1246
90cdd986
BT
1247/* -------------------------------------------------------------------------- */
1248/* 0x6010: Touchpad FW items */
1249/* -------------------------------------------------------------------------- */
1250
1251#define HIDPP_PAGE_TOUCHPAD_FW_ITEMS 0x6010
1252
1253#define CMD_TOUCHPAD_FW_ITEMS_SET 0x10
1254
1255struct hidpp_touchpad_fw_items {
1256 uint8_t presence;
1257 uint8_t desired_state;
1258 uint8_t state;
1259 uint8_t persistent;
1260};
1261
1262/**
1263 * send a set state command to the device by reading the current items->state
1264 * field. items is then filled with the current state.
1265 */
1266static int hidpp_touchpad_fw_items_set(struct hidpp_device *hidpp,
1267 u8 feature_index,
1268 struct hidpp_touchpad_fw_items *items)
1269{
1270 struct hidpp_report response;
1271 int ret;
1272 u8 *params = (u8 *)response.fap.params;
1273
1274 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1275 CMD_TOUCHPAD_FW_ITEMS_SET, &items->state, 1, &response);
1276
1277 if (ret > 0) {
1278 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1279 __func__, ret);
1280 return -EPROTO;
1281 }
1282 if (ret)
1283 return ret;
1284
1285 items->presence = params[0];
1286 items->desired_state = params[1];
1287 items->state = params[2];
1288 items->persistent = params[3];
1289
1290 return 0;
1291}
1292
2f31c525
BT
1293/* -------------------------------------------------------------------------- */
1294/* 0x6100: TouchPadRawXY */
1295/* -------------------------------------------------------------------------- */
1296
1297#define HIDPP_PAGE_TOUCHPAD_RAW_XY 0x6100
1298
1299#define CMD_TOUCHPAD_GET_RAW_INFO 0x01
586bdc4e
BT
1300#define CMD_TOUCHPAD_SET_RAW_REPORT_STATE 0x21
1301
1302#define EVENT_TOUCHPAD_RAW_XY 0x00
2f31c525
BT
1303
1304#define TOUCHPAD_RAW_XY_ORIGIN_LOWER_LEFT 0x01
1305#define TOUCHPAD_RAW_XY_ORIGIN_UPPER_LEFT 0x03
1306
1307struct hidpp_touchpad_raw_info {
1308 u16 x_size;
1309 u16 y_size;
1310 u8 z_range;
1311 u8 area_range;
1312 u8 timestamp_unit;
1313 u8 maxcontacts;
1314 u8 origin;
1315 u16 res;
1316};
1317
1318struct hidpp_touchpad_raw_xy_finger {
1319 u8 contact_type;
1320 u8 contact_status;
1321 u16 x;
1322 u16 y;
1323 u8 z;
1324 u8 area;
1325 u8 finger_id;
1326};
1327
1328struct hidpp_touchpad_raw_xy {
1329 u16 timestamp;
1330 struct hidpp_touchpad_raw_xy_finger fingers[2];
1331 u8 spurious_flag;
1332 u8 end_of_frame;
1333 u8 finger_count;
1334 u8 button;
1335};
1336
1337static int hidpp_touchpad_get_raw_info(struct hidpp_device *hidpp,
1338 u8 feature_index, struct hidpp_touchpad_raw_info *raw_info)
1339{
1340 struct hidpp_report response;
1341 int ret;
1342 u8 *params = (u8 *)response.fap.params;
1343
1344 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1345 CMD_TOUCHPAD_GET_RAW_INFO, NULL, 0, &response);
1346
8c9952b2
BT
1347 if (ret > 0) {
1348 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1349 __func__, ret);
1350 return -EPROTO;
1351 }
2f31c525 1352 if (ret)
8c9952b2 1353 return ret;
2f31c525
BT
1354
1355 raw_info->x_size = get_unaligned_be16(&params[0]);
1356 raw_info->y_size = get_unaligned_be16(&params[2]);
1357 raw_info->z_range = params[4];
1358 raw_info->area_range = params[5];
1359 raw_info->maxcontacts = params[7];
1360 raw_info->origin = params[8];
1361 /* res is given in unit per inch */
1362 raw_info->res = get_unaligned_be16(&params[13]) * 2 / 51;
1363
1364 return ret;
1365}
1366
586bdc4e
BT
1367static int hidpp_touchpad_set_raw_report_state(struct hidpp_device *hidpp_dev,
1368 u8 feature_index, bool send_raw_reports,
1369 bool sensor_enhanced_settings)
1370{
1371 struct hidpp_report response;
1372
1373 /*
1374 * Params:
1375 * bit 0 - enable raw
1376 * bit 1 - 16bit Z, no area
1377 * bit 2 - enhanced sensitivity
1378 * bit 3 - width, height (4 bits each) instead of area
1379 * bit 4 - send raw + gestures (degrades smoothness)
1380 * remaining bits - reserved
1381 */
1382 u8 params = send_raw_reports | (sensor_enhanced_settings << 2);
1383
1384 return hidpp_send_fap_command_sync(hidpp_dev, feature_index,
1385 CMD_TOUCHPAD_SET_RAW_REPORT_STATE, &params, 1, &response);
1386}
1387
1388static void hidpp_touchpad_touch_event(u8 *data,
1389 struct hidpp_touchpad_raw_xy_finger *finger)
1390{
1391 u8 x_m = data[0] << 2;
1392 u8 y_m = data[2] << 2;
1393
1394 finger->x = x_m << 6 | data[1];
1395 finger->y = y_m << 6 | data[3];
1396
1397 finger->contact_type = data[0] >> 6;
1398 finger->contact_status = data[2] >> 6;
1399
1400 finger->z = data[4];
1401 finger->area = data[5];
1402 finger->finger_id = data[6] >> 4;
1403}
1404
1405static void hidpp_touchpad_raw_xy_event(struct hidpp_device *hidpp_dev,
1406 u8 *data, struct hidpp_touchpad_raw_xy *raw_xy)
1407{
1408 memset(raw_xy, 0, sizeof(struct hidpp_touchpad_raw_xy));
1409 raw_xy->end_of_frame = data[8] & 0x01;
1410 raw_xy->spurious_flag = (data[8] >> 1) & 0x01;
1411 raw_xy->finger_count = data[15] & 0x0f;
1412 raw_xy->button = (data[8] >> 2) & 0x01;
1413
1414 if (raw_xy->finger_count) {
1415 hidpp_touchpad_touch_event(&data[2], &raw_xy->fingers[0]);
1416 hidpp_touchpad_touch_event(&data[9], &raw_xy->fingers[1]);
1417 }
1418}
1419
ff21a635
EV
1420/* -------------------------------------------------------------------------- */
1421/* 0x8123: Force feedback support */
1422/* -------------------------------------------------------------------------- */
1423
1424#define HIDPP_FF_GET_INFO 0x01
1425#define HIDPP_FF_RESET_ALL 0x11
1426#define HIDPP_FF_DOWNLOAD_EFFECT 0x21
1427#define HIDPP_FF_SET_EFFECT_STATE 0x31
1428#define HIDPP_FF_DESTROY_EFFECT 0x41
1429#define HIDPP_FF_GET_APERTURE 0x51
1430#define HIDPP_FF_SET_APERTURE 0x61
1431#define HIDPP_FF_GET_GLOBAL_GAINS 0x71
1432#define HIDPP_FF_SET_GLOBAL_GAINS 0x81
1433
1434#define HIDPP_FF_EFFECT_STATE_GET 0x00
1435#define HIDPP_FF_EFFECT_STATE_STOP 0x01
1436#define HIDPP_FF_EFFECT_STATE_PLAY 0x02
1437#define HIDPP_FF_EFFECT_STATE_PAUSE 0x03
1438
1439#define HIDPP_FF_EFFECT_CONSTANT 0x00
1440#define HIDPP_FF_EFFECT_PERIODIC_SINE 0x01
1441#define HIDPP_FF_EFFECT_PERIODIC_SQUARE 0x02
1442#define HIDPP_FF_EFFECT_PERIODIC_TRIANGLE 0x03
1443#define HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHUP 0x04
1444#define HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHDOWN 0x05
1445#define HIDPP_FF_EFFECT_SPRING 0x06
1446#define HIDPP_FF_EFFECT_DAMPER 0x07
1447#define HIDPP_FF_EFFECT_FRICTION 0x08
1448#define HIDPP_FF_EFFECT_INERTIA 0x09
1449#define HIDPP_FF_EFFECT_RAMP 0x0A
1450
1451#define HIDPP_FF_EFFECT_AUTOSTART 0x80
1452
1453#define HIDPP_FF_EFFECTID_NONE -1
1454#define HIDPP_FF_EFFECTID_AUTOCENTER -2
1455
1456#define HIDPP_FF_MAX_PARAMS 20
1457#define HIDPP_FF_RESERVED_SLOTS 1
1458
1459struct hidpp_ff_private_data {
1460 struct hidpp_device *hidpp;
1461 u8 feature_index;
1462 u8 version;
1463 u16 gain;
1464 s16 range;
1465 u8 slot_autocenter;
1466 u8 num_effects;
1467 int *effect_ids;
1468 struct workqueue_struct *wq;
1469 atomic_t workqueue_size;
1470};
1471
1472struct hidpp_ff_work_data {
1473 struct work_struct work;
1474 struct hidpp_ff_private_data *data;
1475 int effect_id;
1476 u8 command;
1477 u8 params[HIDPP_FF_MAX_PARAMS];
1478 u8 size;
1479};
1480
1481static const signed short hiddpp_ff_effects[] = {
1482 FF_CONSTANT,
1483 FF_PERIODIC,
1484 FF_SINE,
1485 FF_SQUARE,
1486 FF_SAW_UP,
1487 FF_SAW_DOWN,
1488 FF_TRIANGLE,
1489 FF_SPRING,
1490 FF_DAMPER,
1491 FF_AUTOCENTER,
1492 FF_GAIN,
1493 -1
1494};
1495
1496static const signed short hiddpp_ff_effects_v2[] = {
1497 FF_RAMP,
1498 FF_FRICTION,
1499 FF_INERTIA,
1500 -1
1501};
1502
1503static const u8 HIDPP_FF_CONDITION_CMDS[] = {
1504 HIDPP_FF_EFFECT_SPRING,
1505 HIDPP_FF_EFFECT_FRICTION,
1506 HIDPP_FF_EFFECT_DAMPER,
1507 HIDPP_FF_EFFECT_INERTIA
1508};
1509
1510static const char *HIDPP_FF_CONDITION_NAMES[] = {
1511 "spring",
1512 "friction",
1513 "damper",
1514 "inertia"
1515};
1516
1517
1518static u8 hidpp_ff_find_effect(struct hidpp_ff_private_data *data, int effect_id)
1519{
1520 int i;
1521
1522 for (i = 0; i < data->num_effects; i++)
1523 if (data->effect_ids[i] == effect_id)
1524 return i+1;
1525
1526 return 0;
1527}
1528
1529static void hidpp_ff_work_handler(struct work_struct *w)
1530{
1531 struct hidpp_ff_work_data *wd = container_of(w, struct hidpp_ff_work_data, work);
1532 struct hidpp_ff_private_data *data = wd->data;
1533 struct hidpp_report response;
1534 u8 slot;
1535 int ret;
1536
1537 /* add slot number if needed */
1538 switch (wd->effect_id) {
1539 case HIDPP_FF_EFFECTID_AUTOCENTER:
1540 wd->params[0] = data->slot_autocenter;
1541 break;
1542 case HIDPP_FF_EFFECTID_NONE:
1543 /* leave slot as zero */
1544 break;
1545 default:
1546 /* find current slot for effect */
1547 wd->params[0] = hidpp_ff_find_effect(data, wd->effect_id);
1548 break;
1549 }
1550
1551 /* send command and wait for reply */
1552 ret = hidpp_send_fap_command_sync(data->hidpp, data->feature_index,
1553 wd->command, wd->params, wd->size, &response);
1554
1555 if (ret) {
1556 hid_err(data->hidpp->hid_dev, "Failed to send command to device!\n");
1557 goto out;
1558 }
1559
1560 /* parse return data */
1561 switch (wd->command) {
1562 case HIDPP_FF_DOWNLOAD_EFFECT:
1563 slot = response.fap.params[0];
1564 if (slot > 0 && slot <= data->num_effects) {
1565 if (wd->effect_id >= 0)
1566 /* regular effect uploaded */
1567 data->effect_ids[slot-1] = wd->effect_id;
1568 else if (wd->effect_id >= HIDPP_FF_EFFECTID_AUTOCENTER)
1569 /* autocenter spring uploaded */
1570 data->slot_autocenter = slot;
1571 }
1572 break;
1573 case HIDPP_FF_DESTROY_EFFECT:
1574 if (wd->effect_id >= 0)
1575 /* regular effect destroyed */
1576 data->effect_ids[wd->params[0]-1] = -1;
1577 else if (wd->effect_id >= HIDPP_FF_EFFECTID_AUTOCENTER)
1578 /* autocenter spring destoyed */
1579 data->slot_autocenter = 0;
1580 break;
1581 case HIDPP_FF_SET_GLOBAL_GAINS:
1582 data->gain = (wd->params[0] << 8) + wd->params[1];
1583 break;
1584 case HIDPP_FF_SET_APERTURE:
1585 data->range = (wd->params[0] << 8) + wd->params[1];
1586 break;
1587 default:
1588 /* no action needed */
1589 break;
1590 }
1591
1592out:
1593 atomic_dec(&data->workqueue_size);
1594 kfree(wd);
1595}
1596
1597static int hidpp_ff_queue_work(struct hidpp_ff_private_data *data, int effect_id, u8 command, u8 *params, u8 size)
1598{
1599 struct hidpp_ff_work_data *wd = kzalloc(sizeof(*wd), GFP_KERNEL);
1600 int s;
1601
1602 if (!wd)
1603 return -ENOMEM;
1604
1605 INIT_WORK(&wd->work, hidpp_ff_work_handler);
1606
1607 wd->data = data;
1608 wd->effect_id = effect_id;
1609 wd->command = command;
1610 wd->size = size;
1611 memcpy(wd->params, params, size);
1612
1613 atomic_inc(&data->workqueue_size);
1614 queue_work(data->wq, &wd->work);
1615
1616 /* warn about excessive queue size */
1617 s = atomic_read(&data->workqueue_size);
1618 if (s >= 20 && s % 20 == 0)
1619 hid_warn(data->hidpp->hid_dev, "Force feedback command queue contains %d commands, causing substantial delays!", s);
1620
1621 return 0;
1622}
1623
1624static int hidpp_ff_upload_effect(struct input_dev *dev, struct ff_effect *effect, struct ff_effect *old)
1625{
1626 struct hidpp_ff_private_data *data = dev->ff->private;
1627 u8 params[20];
1628 u8 size;
1629 int force;
1630
1631 /* set common parameters */
1632 params[2] = effect->replay.length >> 8;
1633 params[3] = effect->replay.length & 255;
1634 params[4] = effect->replay.delay >> 8;
1635 params[5] = effect->replay.delay & 255;
1636
1637 switch (effect->type) {
1638 case FF_CONSTANT:
1639 force = (effect->u.constant.level * fixp_sin16((effect->direction * 360) >> 16)) >> 15;
1640 params[1] = HIDPP_FF_EFFECT_CONSTANT;
1641 params[6] = force >> 8;
1642 params[7] = force & 255;
1643 params[8] = effect->u.constant.envelope.attack_level >> 7;
1644 params[9] = effect->u.constant.envelope.attack_length >> 8;
1645 params[10] = effect->u.constant.envelope.attack_length & 255;
1646 params[11] = effect->u.constant.envelope.fade_level >> 7;
1647 params[12] = effect->u.constant.envelope.fade_length >> 8;
1648 params[13] = effect->u.constant.envelope.fade_length & 255;
1649 size = 14;
1650 dbg_hid("Uploading constant force level=%d in dir %d = %d\n",
1651 effect->u.constant.level,
1652 effect->direction, force);
1653 dbg_hid(" envelope attack=(%d, %d ms) fade=(%d, %d ms)\n",
1654 effect->u.constant.envelope.attack_level,
1655 effect->u.constant.envelope.attack_length,
1656 effect->u.constant.envelope.fade_level,
1657 effect->u.constant.envelope.fade_length);
1658 break;
1659 case FF_PERIODIC:
1660 {
1661 switch (effect->u.periodic.waveform) {
1662 case FF_SINE:
1663 params[1] = HIDPP_FF_EFFECT_PERIODIC_SINE;
1664 break;
1665 case FF_SQUARE:
1666 params[1] = HIDPP_FF_EFFECT_PERIODIC_SQUARE;
1667 break;
1668 case FF_SAW_UP:
1669 params[1] = HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHUP;
1670 break;
1671 case FF_SAW_DOWN:
1672 params[1] = HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHDOWN;
1673 break;
1674 case FF_TRIANGLE:
1675 params[1] = HIDPP_FF_EFFECT_PERIODIC_TRIANGLE;
1676 break;
1677 default:
1678 hid_err(data->hidpp->hid_dev, "Unexpected periodic waveform type %i!\n", effect->u.periodic.waveform);
1679 return -EINVAL;
1680 }
1681 force = (effect->u.periodic.magnitude * fixp_sin16((effect->direction * 360) >> 16)) >> 15;
1682 params[6] = effect->u.periodic.magnitude >> 8;
1683 params[7] = effect->u.periodic.magnitude & 255;
1684 params[8] = effect->u.periodic.offset >> 8;
1685 params[9] = effect->u.periodic.offset & 255;
1686 params[10] = effect->u.periodic.period >> 8;
1687 params[11] = effect->u.periodic.period & 255;
1688 params[12] = effect->u.periodic.phase >> 8;
1689 params[13] = effect->u.periodic.phase & 255;
1690 params[14] = effect->u.periodic.envelope.attack_level >> 7;
1691 params[15] = effect->u.periodic.envelope.attack_length >> 8;
1692 params[16] = effect->u.periodic.envelope.attack_length & 255;
1693 params[17] = effect->u.periodic.envelope.fade_level >> 7;
1694 params[18] = effect->u.periodic.envelope.fade_length >> 8;
1695 params[19] = effect->u.periodic.envelope.fade_length & 255;
1696 size = 20;
1697 dbg_hid("Uploading periodic force mag=%d/dir=%d, offset=%d, period=%d ms, phase=%d\n",
1698 effect->u.periodic.magnitude, effect->direction,
1699 effect->u.periodic.offset,
1700 effect->u.periodic.period,
1701 effect->u.periodic.phase);
1702 dbg_hid(" envelope attack=(%d, %d ms) fade=(%d, %d ms)\n",
1703 effect->u.periodic.envelope.attack_level,
1704 effect->u.periodic.envelope.attack_length,
1705 effect->u.periodic.envelope.fade_level,
1706 effect->u.periodic.envelope.fade_length);
1707 break;
1708 }
1709 case FF_RAMP:
1710 params[1] = HIDPP_FF_EFFECT_RAMP;
1711 force = (effect->u.ramp.start_level * fixp_sin16((effect->direction * 360) >> 16)) >> 15;
1712 params[6] = force >> 8;
1713 params[7] = force & 255;
1714 force = (effect->u.ramp.end_level * fixp_sin16((effect->direction * 360) >> 16)) >> 15;
1715 params[8] = force >> 8;
1716 params[9] = force & 255;
1717 params[10] = effect->u.ramp.envelope.attack_level >> 7;
1718 params[11] = effect->u.ramp.envelope.attack_length >> 8;
1719 params[12] = effect->u.ramp.envelope.attack_length & 255;
1720 params[13] = effect->u.ramp.envelope.fade_level >> 7;
1721 params[14] = effect->u.ramp.envelope.fade_length >> 8;
1722 params[15] = effect->u.ramp.envelope.fade_length & 255;
1723 size = 16;
1724 dbg_hid("Uploading ramp force level=%d -> %d in dir %d = %d\n",
1725 effect->u.ramp.start_level,
1726 effect->u.ramp.end_level,
1727 effect->direction, force);
1728 dbg_hid(" envelope attack=(%d, %d ms) fade=(%d, %d ms)\n",
1729 effect->u.ramp.envelope.attack_level,
1730 effect->u.ramp.envelope.attack_length,
1731 effect->u.ramp.envelope.fade_level,
1732 effect->u.ramp.envelope.fade_length);
1733 break;
1734 case FF_FRICTION:
1735 case FF_INERTIA:
1736 case FF_SPRING:
1737 case FF_DAMPER:
1738 params[1] = HIDPP_FF_CONDITION_CMDS[effect->type - FF_SPRING];
1739 params[6] = effect->u.condition[0].left_saturation >> 9;
1740 params[7] = (effect->u.condition[0].left_saturation >> 1) & 255;
1741 params[8] = effect->u.condition[0].left_coeff >> 8;
1742 params[9] = effect->u.condition[0].left_coeff & 255;
1743 params[10] = effect->u.condition[0].deadband >> 9;
1744 params[11] = (effect->u.condition[0].deadband >> 1) & 255;
1745 params[12] = effect->u.condition[0].center >> 8;
1746 params[13] = effect->u.condition[0].center & 255;
1747 params[14] = effect->u.condition[0].right_coeff >> 8;
1748 params[15] = effect->u.condition[0].right_coeff & 255;
1749 params[16] = effect->u.condition[0].right_saturation >> 9;
1750 params[17] = (effect->u.condition[0].right_saturation >> 1) & 255;
1751 size = 18;
1752 dbg_hid("Uploading %s force left coeff=%d, left sat=%d, right coeff=%d, right sat=%d\n",
1753 HIDPP_FF_CONDITION_NAMES[effect->type - FF_SPRING],
1754 effect->u.condition[0].left_coeff,
1755 effect->u.condition[0].left_saturation,
1756 effect->u.condition[0].right_coeff,
1757 effect->u.condition[0].right_saturation);
1758 dbg_hid(" deadband=%d, center=%d\n",
1759 effect->u.condition[0].deadband,
1760 effect->u.condition[0].center);
1761 break;
1762 default:
1763 hid_err(data->hidpp->hid_dev, "Unexpected force type %i!\n", effect->type);
1764 return -EINVAL;
1765 }
1766
1767 return hidpp_ff_queue_work(data, effect->id, HIDPP_FF_DOWNLOAD_EFFECT, params, size);
1768}
1769
1770static int hidpp_ff_playback(struct input_dev *dev, int effect_id, int value)
1771{
1772 struct hidpp_ff_private_data *data = dev->ff->private;
1773 u8 params[2];
1774
1775 params[1] = value ? HIDPP_FF_EFFECT_STATE_PLAY : HIDPP_FF_EFFECT_STATE_STOP;
1776
1777 dbg_hid("St%sing playback of effect %d.\n", value?"art":"opp", effect_id);
1778
1779 return hidpp_ff_queue_work(data, effect_id, HIDPP_FF_SET_EFFECT_STATE, params, ARRAY_SIZE(params));
1780}
1781
1782static int hidpp_ff_erase_effect(struct input_dev *dev, int effect_id)
1783{
1784 struct hidpp_ff_private_data *data = dev->ff->private;
1785 u8 slot = 0;
1786
1787 dbg_hid("Erasing effect %d.\n", effect_id);
1788
1789 return hidpp_ff_queue_work(data, effect_id, HIDPP_FF_DESTROY_EFFECT, &slot, 1);
1790}
1791
1792static void hidpp_ff_set_autocenter(struct input_dev *dev, u16 magnitude)
1793{
1794 struct hidpp_ff_private_data *data = dev->ff->private;
1795 u8 params[18];
1796
1797 dbg_hid("Setting autocenter to %d.\n", magnitude);
1798
1799 /* start a standard spring effect */
1800 params[1] = HIDPP_FF_EFFECT_SPRING | HIDPP_FF_EFFECT_AUTOSTART;
1801 /* zero delay and duration */
1802 params[2] = params[3] = params[4] = params[5] = 0;
1803 /* set coeff to 25% of saturation */
1804 params[8] = params[14] = magnitude >> 11;
1805 params[9] = params[15] = (magnitude >> 3) & 255;
1806 params[6] = params[16] = magnitude >> 9;
1807 params[7] = params[17] = (magnitude >> 1) & 255;
1808 /* zero deadband and center */
1809 params[10] = params[11] = params[12] = params[13] = 0;
1810
1811 hidpp_ff_queue_work(data, HIDPP_FF_EFFECTID_AUTOCENTER, HIDPP_FF_DOWNLOAD_EFFECT, params, ARRAY_SIZE(params));
1812}
1813
1814static void hidpp_ff_set_gain(struct input_dev *dev, u16 gain)
1815{
1816 struct hidpp_ff_private_data *data = dev->ff->private;
1817 u8 params[4];
1818
1819 dbg_hid("Setting gain to %d.\n", gain);
1820
1821 params[0] = gain >> 8;
1822 params[1] = gain & 255;
1823 params[2] = 0; /* no boost */
1824 params[3] = 0;
1825
1826 hidpp_ff_queue_work(data, HIDPP_FF_EFFECTID_NONE, HIDPP_FF_SET_GLOBAL_GAINS, params, ARRAY_SIZE(params));
1827}
1828
1829static ssize_t hidpp_ff_range_show(struct device *dev, struct device_attribute *attr, char *buf)
1830{
1831 struct hid_device *hid = to_hid_device(dev);
1832 struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
1833 struct input_dev *idev = hidinput->input;
1834 struct hidpp_ff_private_data *data = idev->ff->private;
1835
1836 return scnprintf(buf, PAGE_SIZE, "%u\n", data->range);
1837}
1838
1839static ssize_t hidpp_ff_range_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
1840{
1841 struct hid_device *hid = to_hid_device(dev);
1842 struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
1843 struct input_dev *idev = hidinput->input;
1844 struct hidpp_ff_private_data *data = idev->ff->private;
1845 u8 params[2];
1846 int range = simple_strtoul(buf, NULL, 10);
1847
1848 range = clamp(range, 180, 900);
1849
1850 params[0] = range >> 8;
1851 params[1] = range & 0x00FF;
1852
1853 hidpp_ff_queue_work(data, -1, HIDPP_FF_SET_APERTURE, params, ARRAY_SIZE(params));
1854
1855 return count;
1856}
1857
1858static DEVICE_ATTR(range, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH, hidpp_ff_range_show, hidpp_ff_range_store);
1859
1860static void hidpp_ff_destroy(struct ff_device *ff)
1861{
1862 struct hidpp_ff_private_data *data = ff->private;
1863
1864 kfree(data->effect_ids);
1865}
1866
af2e628d 1867static int hidpp_ff_init(struct hidpp_device *hidpp, u8 feature_index)
ff21a635
EV
1868{
1869 struct hid_device *hid = hidpp->hid_dev;
29d944cd
AS
1870 struct hid_input *hidinput;
1871 struct input_dev *dev;
ff21a635
EV
1872 const struct usb_device_descriptor *udesc = &(hid_to_usb_dev(hid)->descriptor);
1873 const u16 bcdDevice = le16_to_cpu(udesc->bcdDevice);
1874 struct ff_device *ff;
1875 struct hidpp_report response;
1876 struct hidpp_ff_private_data *data;
1877 int error, j, num_slots;
1878 u8 version;
1879
29d944cd
AS
1880 if (list_empty(&hid->inputs)) {
1881 hid_err(hid, "no inputs found\n");
1882 return -ENODEV;
1883 }
1884 hidinput = list_entry(hid->inputs.next, struct hid_input, list);
1885 dev = hidinput->input;
1886
ff21a635
EV
1887 if (!dev) {
1888 hid_err(hid, "Struct input_dev not set!\n");
1889 return -EINVAL;
1890 }
1891
1892 /* Get firmware release */
1893 version = bcdDevice & 255;
1894
1895 /* Set supported force feedback capabilities */
1896 for (j = 0; hiddpp_ff_effects[j] >= 0; j++)
1897 set_bit(hiddpp_ff_effects[j], dev->ffbit);
1898 if (version > 1)
1899 for (j = 0; hiddpp_ff_effects_v2[j] >= 0; j++)
1900 set_bit(hiddpp_ff_effects_v2[j], dev->ffbit);
1901
1902 /* Read number of slots available in device */
1903 error = hidpp_send_fap_command_sync(hidpp, feature_index,
1904 HIDPP_FF_GET_INFO, NULL, 0, &response);
1905 if (error) {
1906 if (error < 0)
1907 return error;
1908 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1909 __func__, error);
1910 return -EPROTO;
1911 }
1912
1913 num_slots = response.fap.params[0] - HIDPP_FF_RESERVED_SLOTS;
1914
1915 error = input_ff_create(dev, num_slots);
1916
1917 if (error) {
1918 hid_err(dev, "Failed to create FF device!\n");
1919 return error;
1920 }
1921
1922 data = kzalloc(sizeof(*data), GFP_KERNEL);
1923 if (!data)
1924 return -ENOMEM;
1925 data->effect_ids = kcalloc(num_slots, sizeof(int), GFP_KERNEL);
1926 if (!data->effect_ids) {
1927 kfree(data);
1928 return -ENOMEM;
1929 }
fb267095
KL
1930 data->wq = create_singlethread_workqueue("hidpp-ff-sendqueue");
1931 if (!data->wq) {
1932 kfree(data->effect_ids);
1933 kfree(data);
1934 return -ENOMEM;
1935 }
1936
ff21a635
EV
1937 data->hidpp = hidpp;
1938 data->feature_index = feature_index;
1939 data->version = version;
1940 data->slot_autocenter = 0;
1941 data->num_effects = num_slots;
1942 for (j = 0; j < num_slots; j++)
1943 data->effect_ids[j] = -1;
1944
1945 ff = dev->ff;
1946 ff->private = data;
1947
1948 ff->upload = hidpp_ff_upload_effect;
1949 ff->erase = hidpp_ff_erase_effect;
1950 ff->playback = hidpp_ff_playback;
1951 ff->set_gain = hidpp_ff_set_gain;
1952 ff->set_autocenter = hidpp_ff_set_autocenter;
1953 ff->destroy = hidpp_ff_destroy;
1954
1955
1956 /* reset all forces */
1957 error = hidpp_send_fap_command_sync(hidpp, feature_index,
1958 HIDPP_FF_RESET_ALL, NULL, 0, &response);
1959
1960 /* Read current Range */
1961 error = hidpp_send_fap_command_sync(hidpp, feature_index,
1962 HIDPP_FF_GET_APERTURE, NULL, 0, &response);
1963 if (error)
1964 hid_warn(hidpp->hid_dev, "Failed to read range from device!\n");
1965 data->range = error ? 900 : get_unaligned_be16(&response.fap.params[0]);
1966
1967 /* Create sysfs interface */
1968 error = device_create_file(&(hidpp->hid_dev->dev), &dev_attr_range);
1969 if (error)
1970 hid_warn(hidpp->hid_dev, "Unable to create sysfs interface for \"range\", errno %d!\n", error);
1971
1972 /* Read the current gain values */
1973 error = hidpp_send_fap_command_sync(hidpp, feature_index,
1974 HIDPP_FF_GET_GLOBAL_GAINS, NULL, 0, &response);
1975 if (error)
1976 hid_warn(hidpp->hid_dev, "Failed to read gain values from device!\n");
1977 data->gain = error ? 0xffff : get_unaligned_be16(&response.fap.params[0]);
1978 /* ignore boost value at response.fap.params[2] */
1979
1980 /* init the hardware command queue */
ff21a635
EV
1981 atomic_set(&data->workqueue_size, 0);
1982
1983 /* initialize with zero autocenter to get wheel in usable state */
1984 hidpp_ff_set_autocenter(dev, 0);
1985
df47b246
CIK
1986 hid_info(hid, "Force feedback support loaded (firmware release %d).\n",
1987 version);
ff21a635
EV
1988
1989 return 0;
1990}
1991
af2e628d 1992static int hidpp_ff_deinit(struct hid_device *hid)
ff21a635
EV
1993{
1994 struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
1995 struct input_dev *dev = hidinput->input;
1996 struct hidpp_ff_private_data *data;
1997
1998 if (!dev) {
1999 hid_err(hid, "Struct input_dev not found!\n");
2000 return -EINVAL;
2001 }
2002
2003 hid_info(hid, "Unloading HID++ force feedback.\n");
2004 data = dev->ff->private;
2005 if (!data) {
2006 hid_err(hid, "Private data not found!\n");
2007 return -EINVAL;
2008 }
2009
2010 destroy_workqueue(data->wq);
2011 device_remove_file(&hid->dev, &dev_attr_range);
2012
2013 return 0;
2014}
2015
2016
2f31c525
BT
2017/* ************************************************************************** */
2018/* */
2019/* Device Support */
2020/* */
2021/* ************************************************************************** */
2022
2023/* -------------------------------------------------------------------------- */
2024/* Touchpad HID++ devices */
2025/* -------------------------------------------------------------------------- */
2026
57ac86cf
BT
2027#define WTP_MANUAL_RESOLUTION 39
2028
2f31c525
BT
2029struct wtp_data {
2030 struct input_dev *input;
2031 u16 x_size, y_size;
2032 u8 finger_count;
2033 u8 mt_feature_index;
2034 u8 button_feature_index;
2035 u8 maxcontacts;
2036 bool flip_y;
2037 unsigned int resolution;
2038};
2039
2040static int wtp_input_mapping(struct hid_device *hdev, struct hid_input *hi,
2041 struct hid_field *field, struct hid_usage *usage,
2042 unsigned long **bit, int *max)
2043{
2044 return -1;
2045}
2046
c39e3d5f
BT
2047static void wtp_populate_input(struct hidpp_device *hidpp,
2048 struct input_dev *input_dev, bool origin_is_hid_core)
2f31c525 2049{
2f31c525 2050 struct wtp_data *wd = hidpp->private_data;
2f31c525
BT
2051
2052 __set_bit(EV_ABS, input_dev->evbit);
2053 __set_bit(EV_KEY, input_dev->evbit);
2054 __clear_bit(EV_REL, input_dev->evbit);
2055 __clear_bit(EV_LED, input_dev->evbit);
2056
2057 input_set_abs_params(input_dev, ABS_MT_POSITION_X, 0, wd->x_size, 0, 0);
2058 input_abs_set_res(input_dev, ABS_MT_POSITION_X, wd->resolution);
2059 input_set_abs_params(input_dev, ABS_MT_POSITION_Y, 0, wd->y_size, 0, 0);
2060 input_abs_set_res(input_dev, ABS_MT_POSITION_Y, wd->resolution);
2061
2062 /* Max pressure is not given by the devices, pick one */
2063 input_set_abs_params(input_dev, ABS_MT_PRESSURE, 0, 50, 0, 0);
2064
2065 input_set_capability(input_dev, EV_KEY, BTN_LEFT);
2066
57ac86cf
BT
2067 if (hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS)
2068 input_set_capability(input_dev, EV_KEY, BTN_RIGHT);
2069 else
2070 __set_bit(INPUT_PROP_BUTTONPAD, input_dev->propbit);
2f31c525
BT
2071
2072 input_mt_init_slots(input_dev, wd->maxcontacts, INPUT_MT_POINTER |
2073 INPUT_MT_DROP_UNUSED);
2074
2075 wd->input = input_dev;
2076}
2077
2078static void wtp_touch_event(struct wtp_data *wd,
2079 struct hidpp_touchpad_raw_xy_finger *touch_report)
2080{
2081 int slot;
2082
2083 if (!touch_report->finger_id || touch_report->contact_type)
2084 /* no actual data */
2085 return;
2086
2087 slot = input_mt_get_slot_by_key(wd->input, touch_report->finger_id);
2088
2089 input_mt_slot(wd->input, slot);
2090 input_mt_report_slot_state(wd->input, MT_TOOL_FINGER,
2091 touch_report->contact_status);
2092 if (touch_report->contact_status) {
2093 input_event(wd->input, EV_ABS, ABS_MT_POSITION_X,
2094 touch_report->x);
2095 input_event(wd->input, EV_ABS, ABS_MT_POSITION_Y,
2096 wd->flip_y ? wd->y_size - touch_report->y :
2097 touch_report->y);
2098 input_event(wd->input, EV_ABS, ABS_MT_PRESSURE,
2099 touch_report->area);
2100 }
2101}
2102
2103static void wtp_send_raw_xy_event(struct hidpp_device *hidpp,
2104 struct hidpp_touchpad_raw_xy *raw)
2105{
2106 struct wtp_data *wd = hidpp->private_data;
2107 int i;
2108
2109 for (i = 0; i < 2; i++)
2110 wtp_touch_event(wd, &(raw->fingers[i]));
2111
57ac86cf
BT
2112 if (raw->end_of_frame &&
2113 !(hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS))
2f31c525
BT
2114 input_event(wd->input, EV_KEY, BTN_LEFT, raw->button);
2115
2116 if (raw->end_of_frame || raw->finger_count <= 2) {
2117 input_mt_sync_frame(wd->input);
2118 input_sync(wd->input);
2119 }
2120}
2121
2122static int wtp_mouse_raw_xy_event(struct hidpp_device *hidpp, u8 *data)
2123{
2124 struct wtp_data *wd = hidpp->private_data;
2125 u8 c1_area = ((data[7] & 0xf) * (data[7] & 0xf) +
2126 (data[7] >> 4) * (data[7] >> 4)) / 2;
2127 u8 c2_area = ((data[13] & 0xf) * (data[13] & 0xf) +
2128 (data[13] >> 4) * (data[13] >> 4)) / 2;
2129 struct hidpp_touchpad_raw_xy raw = {
2130 .timestamp = data[1],
2131 .fingers = {
2132 {
2133 .contact_type = 0,
2134 .contact_status = !!data[7],
2135 .x = get_unaligned_le16(&data[3]),
2136 .y = get_unaligned_le16(&data[5]),
2137 .z = c1_area,
2138 .area = c1_area,
2139 .finger_id = data[2],
2140 }, {
2141 .contact_type = 0,
2142 .contact_status = !!data[13],
2143 .x = get_unaligned_le16(&data[9]),
2144 .y = get_unaligned_le16(&data[11]),
2145 .z = c2_area,
2146 .area = c2_area,
2147 .finger_id = data[8],
2148 }
2149 },
2150 .finger_count = wd->maxcontacts,
2151 .spurious_flag = 0,
2152 .end_of_frame = (data[0] >> 7) == 0,
2153 .button = data[0] & 0x01,
2154 };
2155
2156 wtp_send_raw_xy_event(hidpp, &raw);
2157
2158 return 1;
2159}
2160
2161static int wtp_raw_event(struct hid_device *hdev, u8 *data, int size)
2162{
2163 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2164 struct wtp_data *wd = hidpp->private_data;
586bdc4e
BT
2165 struct hidpp_report *report = (struct hidpp_report *)data;
2166 struct hidpp_touchpad_raw_xy raw;
2f31c525 2167
586bdc4e 2168 if (!wd || !wd->input)
2f31c525
BT
2169 return 1;
2170
586bdc4e
BT
2171 switch (data[0]) {
2172 case 0x02:
0b3f6569
PW
2173 if (size < 2) {
2174 hid_err(hdev, "Received HID report of bad size (%d)",
2175 size);
2176 return 1;
2177 }
57ac86cf
BT
2178 if (hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS) {
2179 input_event(wd->input, EV_KEY, BTN_LEFT,
2180 !!(data[1] & 0x01));
2181 input_event(wd->input, EV_KEY, BTN_RIGHT,
2182 !!(data[1] & 0x02));
2183 input_sync(wd->input);
8abd8205 2184 return 0;
57ac86cf
BT
2185 } else {
2186 if (size < 21)
2187 return 1;
2188 return wtp_mouse_raw_xy_event(hidpp, &data[7]);
2189 }
586bdc4e 2190 case REPORT_ID_HIDPP_LONG:
0b3f6569 2191 /* size is already checked in hidpp_raw_event. */
586bdc4e
BT
2192 if ((report->fap.feature_index != wd->mt_feature_index) ||
2193 (report->fap.funcindex_clientid != EVENT_TOUCHPAD_RAW_XY))
2194 return 1;
2195 hidpp_touchpad_raw_xy_event(hidpp, data + 4, &raw);
2196
2197 wtp_send_raw_xy_event(hidpp, &raw);
2198 return 0;
2199 }
2200
2201 return 0;
2f31c525
BT
2202}
2203
2204static int wtp_get_config(struct hidpp_device *hidpp)
2205{
2206 struct wtp_data *wd = hidpp->private_data;
2207 struct hidpp_touchpad_raw_info raw_info = {0};
2208 u8 feature_type;
2209 int ret;
2210
2211 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_TOUCHPAD_RAW_XY,
2212 &wd->mt_feature_index, &feature_type);
2213 if (ret)
2214 /* means that the device is not powered up */
2215 return ret;
2216
2217 ret = hidpp_touchpad_get_raw_info(hidpp, wd->mt_feature_index,
2218 &raw_info);
2219 if (ret)
2220 return ret;
2221
2222 wd->x_size = raw_info.x_size;
2223 wd->y_size = raw_info.y_size;
2224 wd->maxcontacts = raw_info.maxcontacts;
2225 wd->flip_y = raw_info.origin == TOUCHPAD_RAW_XY_ORIGIN_LOWER_LEFT;
2226 wd->resolution = raw_info.res;
57ac86cf
BT
2227 if (!wd->resolution)
2228 wd->resolution = WTP_MANUAL_RESOLUTION;
2f31c525
BT
2229
2230 return 0;
2231}
2232
2233static int wtp_allocate(struct hid_device *hdev, const struct hid_device_id *id)
2234{
2235 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2236 struct wtp_data *wd;
2237
2238 wd = devm_kzalloc(&hdev->dev, sizeof(struct wtp_data),
2239 GFP_KERNEL);
2240 if (!wd)
2241 return -ENOMEM;
2242
2243 hidpp->private_data = wd;
2244
2245 return 0;
2246};
2247
bf159447 2248static int wtp_connect(struct hid_device *hdev, bool connected)
586bdc4e
BT
2249{
2250 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2251 struct wtp_data *wd = hidpp->private_data;
2252 int ret;
2253
586bdc4e
BT
2254 if (!wd->x_size) {
2255 ret = wtp_get_config(hidpp);
2256 if (ret) {
2257 hid_err(hdev, "Can not get wtp config: %d\n", ret);
bf159447 2258 return ret;
586bdc4e
BT
2259 }
2260 }
2261
bf159447 2262 return hidpp_touchpad_set_raw_report_state(hidpp, wd->mt_feature_index,
586bdc4e
BT
2263 true, true);
2264}
2265
8a09b4fa
GB
2266/* ------------------------------------------------------------------------- */
2267/* Logitech M560 devices */
2268/* ------------------------------------------------------------------------- */
2269
2270/*
2271 * Logitech M560 protocol overview
2272 *
2273 * The Logitech M560 mouse, is designed for windows 8. When the middle and/or
2274 * the sides buttons are pressed, it sends some keyboard keys events
2275 * instead of buttons ones.
2276 * To complicate things further, the middle button keys sequence
2277 * is different from the odd press and the even press.
2278 *
2279 * forward button -> Super_R
2280 * backward button -> Super_L+'d' (press only)
2281 * middle button -> 1st time: Alt_L+SuperL+XF86TouchpadOff (press only)
2282 * 2nd time: left-click (press only)
2283 * NB: press-only means that when the button is pressed, the
2284 * KeyPress/ButtonPress and KeyRelease/ButtonRelease events are generated
2285 * together sequentially; instead when the button is released, no event is
2286 * generated !
2287 *
2288 * With the command
2289 * 10<xx>0a 3500af03 (where <xx> is the mouse id),
2290 * the mouse reacts differently:
2291 * - it never sends a keyboard key event
2292 * - for the three mouse button it sends:
2293 * middle button press 11<xx>0a 3500af00...
2294 * side 1 button (forward) press 11<xx>0a 3500b000...
2295 * side 2 button (backward) press 11<xx>0a 3500ae00...
2296 * middle/side1/side2 button release 11<xx>0a 35000000...
2297 */
2298
2299static const u8 m560_config_parameter[] = {0x00, 0xaf, 0x03};
2300
2301struct m560_private_data {
2302 struct input_dev *input;
2303};
2304
2305/* how buttons are mapped in the report */
2306#define M560_MOUSE_BTN_LEFT 0x01
2307#define M560_MOUSE_BTN_RIGHT 0x02
2308#define M560_MOUSE_BTN_WHEEL_LEFT 0x08
2309#define M560_MOUSE_BTN_WHEEL_RIGHT 0x10
2310
2311#define M560_SUB_ID 0x0a
2312#define M560_BUTTON_MODE_REGISTER 0x35
2313
2314static int m560_send_config_command(struct hid_device *hdev, bool connected)
2315{
2316 struct hidpp_report response;
2317 struct hidpp_device *hidpp_dev;
2318
2319 hidpp_dev = hid_get_drvdata(hdev);
2320
8a09b4fa
GB
2321 return hidpp_send_rap_command_sync(
2322 hidpp_dev,
2323 REPORT_ID_HIDPP_SHORT,
2324 M560_SUB_ID,
2325 M560_BUTTON_MODE_REGISTER,
2326 (u8 *)m560_config_parameter,
2327 sizeof(m560_config_parameter),
2328 &response
2329 );
2330}
2331
2332static int m560_allocate(struct hid_device *hdev)
2333{
2334 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2335 struct m560_private_data *d;
2336
2337 d = devm_kzalloc(&hdev->dev, sizeof(struct m560_private_data),
2338 GFP_KERNEL);
2339 if (!d)
2340 return -ENOMEM;
2341
2342 hidpp->private_data = d;
2343
2344 return 0;
2345};
2346
2347static int m560_raw_event(struct hid_device *hdev, u8 *data, int size)
2348{
2349 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2350 struct m560_private_data *mydata = hidpp->private_data;
2351
2352 /* sanity check */
2353 if (!mydata || !mydata->input) {
2354 hid_err(hdev, "error in parameter\n");
2355 return -EINVAL;
2356 }
2357
2358 if (size < 7) {
2359 hid_err(hdev, "error in report\n");
2360 return 0;
2361 }
2362
2363 if (data[0] == REPORT_ID_HIDPP_LONG &&
2364 data[2] == M560_SUB_ID && data[6] == 0x00) {
2365 /*
2366 * m560 mouse report for middle, forward and backward button
2367 *
2368 * data[0] = 0x11
2369 * data[1] = device-id
2370 * data[2] = 0x0a
2371 * data[5] = 0xaf -> middle
2372 * 0xb0 -> forward
2373 * 0xae -> backward
2374 * 0x00 -> release all
2375 * data[6] = 0x00
2376 */
2377
2378 switch (data[5]) {
2379 case 0xaf:
2380 input_report_key(mydata->input, BTN_MIDDLE, 1);
2381 break;
2382 case 0xb0:
2383 input_report_key(mydata->input, BTN_FORWARD, 1);
2384 break;
2385 case 0xae:
2386 input_report_key(mydata->input, BTN_BACK, 1);
2387 break;
2388 case 0x00:
2389 input_report_key(mydata->input, BTN_BACK, 0);
2390 input_report_key(mydata->input, BTN_FORWARD, 0);
2391 input_report_key(mydata->input, BTN_MIDDLE, 0);
2392 break;
2393 default:
2394 hid_err(hdev, "error in report\n");
2395 return 0;
2396 }
2397 input_sync(mydata->input);
2398
2399 } else if (data[0] == 0x02) {
2400 /*
2401 * Logitech M560 mouse report
2402 *
2403 * data[0] = type (0x02)
2404 * data[1..2] = buttons
2405 * data[3..5] = xy
2406 * data[6] = wheel
2407 */
2408
2409 int v;
2410
2411 input_report_key(mydata->input, BTN_LEFT,
2412 !!(data[1] & M560_MOUSE_BTN_LEFT));
2413 input_report_key(mydata->input, BTN_RIGHT,
2414 !!(data[1] & M560_MOUSE_BTN_RIGHT));
2415
2416 if (data[1] & M560_MOUSE_BTN_WHEEL_LEFT)
2417 input_report_rel(mydata->input, REL_HWHEEL, -1);
2418 else if (data[1] & M560_MOUSE_BTN_WHEEL_RIGHT)
2419 input_report_rel(mydata->input, REL_HWHEEL, 1);
2420
2421 v = hid_snto32(hid_field_extract(hdev, data+3, 0, 12), 12);
2422 input_report_rel(mydata->input, REL_X, v);
2423
2424 v = hid_snto32(hid_field_extract(hdev, data+3, 12, 12), 12);
2425 input_report_rel(mydata->input, REL_Y, v);
2426
2427 v = hid_snto32(data[6], 8);
2428 input_report_rel(mydata->input, REL_WHEEL, v);
2429
2430 input_sync(mydata->input);
2431 }
2432
2433 return 1;
2434}
2435
2436static void m560_populate_input(struct hidpp_device *hidpp,
2437 struct input_dev *input_dev, bool origin_is_hid_core)
2438{
2439 struct m560_private_data *mydata = hidpp->private_data;
2440
2441 mydata->input = input_dev;
2442
2443 __set_bit(EV_KEY, mydata->input->evbit);
2444 __set_bit(BTN_MIDDLE, mydata->input->keybit);
2445 __set_bit(BTN_RIGHT, mydata->input->keybit);
2446 __set_bit(BTN_LEFT, mydata->input->keybit);
2447 __set_bit(BTN_BACK, mydata->input->keybit);
2448 __set_bit(BTN_FORWARD, mydata->input->keybit);
2449
2450 __set_bit(EV_REL, mydata->input->evbit);
2451 __set_bit(REL_X, mydata->input->relbit);
2452 __set_bit(REL_Y, mydata->input->relbit);
2453 __set_bit(REL_WHEEL, mydata->input->relbit);
2454 __set_bit(REL_HWHEEL, mydata->input->relbit);
2455}
2456
2457static int m560_input_mapping(struct hid_device *hdev, struct hid_input *hi,
2458 struct hid_field *field, struct hid_usage *usage,
2459 unsigned long **bit, int *max)
2460{
2461 return -1;
2462}
2463
90cdd986
BT
2464/* ------------------------------------------------------------------------- */
2465/* Logitech K400 devices */
2466/* ------------------------------------------------------------------------- */
2467
2468/*
2469 * The Logitech K400 keyboard has an embedded touchpad which is seen
2470 * as a mouse from the OS point of view. There is a hardware shortcut to disable
2471 * tap-to-click but the setting is not remembered accross reset, annoying some
2472 * users.
2473 *
2474 * We can toggle this feature from the host by using the feature 0x6010:
2475 * Touchpad FW items
2476 */
2477
2478struct k400_private_data {
2479 u8 feature_index;
2480};
2481
2482static int k400_disable_tap_to_click(struct hidpp_device *hidpp)
2483{
2484 struct k400_private_data *k400 = hidpp->private_data;
2485 struct hidpp_touchpad_fw_items items = {};
2486 int ret;
2487 u8 feature_type;
2488
2489 if (!k400->feature_index) {
2490 ret = hidpp_root_get_feature(hidpp,
2491 HIDPP_PAGE_TOUCHPAD_FW_ITEMS,
2492 &k400->feature_index, &feature_type);
2493 if (ret)
2494 /* means that the device is not powered up */
2495 return ret;
2496 }
2497
2498 ret = hidpp_touchpad_fw_items_set(hidpp, k400->feature_index, &items);
2499 if (ret)
2500 return ret;
2501
2502 return 0;
2503}
2504
2505static int k400_allocate(struct hid_device *hdev)
2506{
2507 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2508 struct k400_private_data *k400;
2509
2510 k400 = devm_kzalloc(&hdev->dev, sizeof(struct k400_private_data),
2511 GFP_KERNEL);
2512 if (!k400)
2513 return -ENOMEM;
2514
2515 hidpp->private_data = k400;
2516
2517 return 0;
2518};
2519
2520static int k400_connect(struct hid_device *hdev, bool connected)
2521{
2522 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2523
90cdd986
BT
2524 if (!disable_tap_to_click)
2525 return 0;
2526
2527 return k400_disable_tap_to_click(hidpp);
2528}
2529
7f4b49fe
SW
2530/* ------------------------------------------------------------------------- */
2531/* Logitech G920 Driving Force Racing Wheel for Xbox One */
2532/* ------------------------------------------------------------------------- */
2533
2534#define HIDPP_PAGE_G920_FORCE_FEEDBACK 0x8123
2535
7f4b49fe
SW
2536static int g920_get_config(struct hidpp_device *hidpp)
2537{
7f4b49fe
SW
2538 u8 feature_type;
2539 u8 feature_index;
2540 int ret;
2541
7f4b49fe
SW
2542 /* Find feature and store for later use */
2543 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_G920_FORCE_FEEDBACK,
2544 &feature_index, &feature_type);
2545 if (ret)
2546 return ret;
2547
ff21a635 2548 ret = hidpp_ff_init(hidpp, feature_index);
7f4b49fe 2549 if (ret)
ff21a635
EV
2550 hid_warn(hidpp->hid_dev, "Unable to initialize force feedback support, errno %d\n",
2551 ret);
7f4b49fe
SW
2552
2553 return 0;
2554}
2555
2f31c525
BT
2556/* -------------------------------------------------------------------------- */
2557/* Generic HID++ devices */
2558/* -------------------------------------------------------------------------- */
2559
2560static int hidpp_input_mapping(struct hid_device *hdev, struct hid_input *hi,
2561 struct hid_field *field, struct hid_usage *usage,
2562 unsigned long **bit, int *max)
2563{
2564 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2565
2566 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
2567 return wtp_input_mapping(hdev, hi, field, usage, bit, max);
8a09b4fa
GB
2568 else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560 &&
2569 field->application != HID_GD_MOUSE)
2570 return m560_input_mapping(hdev, hi, field, usage, bit, max);
2f31c525
BT
2571
2572 return 0;
2573}
2574
0b1804e3
SW
2575static int hidpp_input_mapped(struct hid_device *hdev, struct hid_input *hi,
2576 struct hid_field *field, struct hid_usage *usage,
2577 unsigned long **bit, int *max)
2578{
2579 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2580
2581 /* Ensure that Logitech G920 is not given a default fuzz/flat value */
2582 if (hidpp->quirks & HIDPP_QUIRK_CLASS_G920) {
2583 if (usage->type == EV_ABS && (usage->code == ABS_X ||
2584 usage->code == ABS_Y || usage->code == ABS_Z ||
2585 usage->code == ABS_RZ)) {
2586 field->application = HID_GD_MULTIAXIS;
2587 }
2588 }
2589
2590 return 0;
2591}
2592
2593
c39e3d5f
BT
2594static void hidpp_populate_input(struct hidpp_device *hidpp,
2595 struct input_dev *input, bool origin_is_hid_core)
2596{
2597 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
2598 wtp_populate_input(hidpp, input, origin_is_hid_core);
8a09b4fa
GB
2599 else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560)
2600 m560_populate_input(hidpp, input, origin_is_hid_core);
c39e3d5f
BT
2601}
2602
b2c68a2f 2603static int hidpp_input_configured(struct hid_device *hdev,
2f31c525
BT
2604 struct hid_input *hidinput)
2605{
2606 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
c39e3d5f 2607 struct input_dev *input = hidinput->input;
2f31c525 2608
c39e3d5f 2609 hidpp_populate_input(hidpp, input, true);
b2c68a2f
DT
2610
2611 return 0;
2f31c525
BT
2612}
2613
2614static int hidpp_raw_hidpp_event(struct hidpp_device *hidpp, u8 *data,
2615 int size)
2616{
2617 struct hidpp_report *question = hidpp->send_receive_buf;
2618 struct hidpp_report *answer = hidpp->send_receive_buf;
2619 struct hidpp_report *report = (struct hidpp_report *)data;
eb626c57 2620 int ret;
2f31c525
BT
2621
2622 /*
2623 * If the mutex is locked then we have a pending answer from a
e529fea9 2624 * previously sent command.
2f31c525
BT
2625 */
2626 if (unlikely(mutex_is_locked(&hidpp->send_mutex))) {
2627 /*
2628 * Check for a correct hidpp20 answer or the corresponding
2629 * error
2630 */
2631 if (hidpp_match_answer(question, report) ||
2632 hidpp_match_error(question, report)) {
2633 *answer = *report;
2634 hidpp->answer_available = true;
2635 wake_up(&hidpp->wait);
2636 /*
2637 * This was an answer to a command that this driver sent
2638 * We return 1 to hid-core to avoid forwarding the
2639 * command upstream as it has been treated by the driver
2640 */
2641
2642 return 1;
2643 }
2644 }
2645
c39e3d5f
BT
2646 if (unlikely(hidpp_report_is_connect_event(report))) {
2647 atomic_set(&hidpp->connected,
2648 !(report->rap.params[0] & (1 << 6)));
6bd4e65d 2649 if (schedule_work(&hidpp->work) == 0)
c39e3d5f
BT
2650 dbg_hid("%s: connect event already queued\n", __func__);
2651 return 1;
2652 }
2653
eb626c57
BT
2654 if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP20_BATTERY) {
2655 ret = hidpp20_battery_event(hidpp, data, size);
2656 if (ret != 0)
2657 return ret;
696ecef9
BT
2658 ret = hidpp_solar_battery_event(hidpp, data, size);
2659 if (ret != 0)
2660 return ret;
eb626c57
BT
2661 }
2662
7f7ce2a2
BT
2663 if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP10_BATTERY) {
2664 ret = hidpp10_battery_event(hidpp, data, size);
2665 if (ret != 0)
2666 return ret;
2667 }
2668
2f31c525
BT
2669 return 0;
2670}
2671
2672static int hidpp_raw_event(struct hid_device *hdev, struct hid_report *report,
2673 u8 *data, int size)
2674{
2675 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
e529fea9 2676 int ret = 0;
2f31c525 2677
e529fea9 2678 /* Generic HID++ processing. */
2f31c525 2679 switch (data[0]) {
a5ce8f5b
SW
2680 case REPORT_ID_HIDPP_VERY_LONG:
2681 if (size != HIDPP_REPORT_VERY_LONG_LENGTH) {
2682 hid_err(hdev, "received hid++ report of bad size (%d)",
2683 size);
2684 return 1;
2685 }
2686 ret = hidpp_raw_hidpp_event(hidpp, data, size);
2687 break;
2f31c525
BT
2688 case REPORT_ID_HIDPP_LONG:
2689 if (size != HIDPP_REPORT_LONG_LENGTH) {
2690 hid_err(hdev, "received hid++ report of bad size (%d)",
2691 size);
2692 return 1;
2693 }
e529fea9
PW
2694 ret = hidpp_raw_hidpp_event(hidpp, data, size);
2695 break;
2f31c525
BT
2696 case REPORT_ID_HIDPP_SHORT:
2697 if (size != HIDPP_REPORT_SHORT_LENGTH) {
2698 hid_err(hdev, "received hid++ report of bad size (%d)",
2699 size);
2700 return 1;
2701 }
e529fea9
PW
2702 ret = hidpp_raw_hidpp_event(hidpp, data, size);
2703 break;
2f31c525
BT
2704 }
2705
e529fea9
PW
2706 /* If no report is available for further processing, skip calling
2707 * raw_event of subclasses. */
2708 if (ret != 0)
2709 return ret;
2710
2f31c525
BT
2711 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
2712 return wtp_raw_event(hdev, data, size);
8a09b4fa
GB
2713 else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560)
2714 return m560_raw_event(hdev, data, size);
2f31c525
BT
2715
2716 return 0;
2717}
2718
a52ec107
BT
2719static int hidpp_initialize_battery(struct hidpp_device *hidpp)
2720{
2721 static atomic_t battery_no = ATOMIC_INIT(0);
2722 struct power_supply_config cfg = { .drv_data = hidpp };
2723 struct power_supply_desc *desc = &hidpp->battery.desc;
5b036ea1 2724 enum power_supply_property *battery_props;
a52ec107 2725 struct hidpp_battery *battery;
5b036ea1 2726 unsigned int num_battery_props;
a52ec107
BT
2727 unsigned long n;
2728 int ret;
2729
2730 if (hidpp->battery.ps)
2731 return 0;
2732
696ecef9
BT
2733 hidpp->battery.feature_index = 0xff;
2734 hidpp->battery.solar_feature_index = 0xff;
2735
a52ec107 2736 if (hidpp->protocol_major >= 2) {
696ecef9
BT
2737 if (hidpp->quirks & HIDPP_QUIRK_CLASS_K750)
2738 ret = hidpp_solar_request_battery_event(hidpp);
2739 else
2740 ret = hidpp20_query_battery_info(hidpp);
2741
a52ec107
BT
2742 if (ret)
2743 return ret;
2744 hidpp->capabilities |= HIDPP_CAPABILITY_HIDPP20_BATTERY;
2745 } else {
7f7ce2a2
BT
2746 ret = hidpp10_query_battery_status(hidpp);
2747 if (ret) {
2748 ret = hidpp10_query_battery_mileage(hidpp);
2749 if (ret)
2750 return -ENOENT;
2751 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_MILEAGE;
2752 } else {
2753 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS;
2754 }
2755 hidpp->capabilities |= HIDPP_CAPABILITY_HIDPP10_BATTERY;
a52ec107
BT
2756 }
2757
5b036ea1
BT
2758 battery_props = devm_kmemdup(&hidpp->hid_dev->dev,
2759 hidpp_battery_props,
2760 sizeof(hidpp_battery_props),
2761 GFP_KERNEL);
929b60a8
GS
2762 if (!battery_props)
2763 return -ENOMEM;
2764
5b036ea1
BT
2765 num_battery_props = ARRAY_SIZE(hidpp_battery_props) - 2;
2766
2767 if (hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_MILEAGE)
2768 battery_props[num_battery_props++] =
2769 POWER_SUPPLY_PROP_CAPACITY;
2770
2771 if (hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS)
2772 battery_props[num_battery_props++] =
2773 POWER_SUPPLY_PROP_CAPACITY_LEVEL;
2774
a52ec107
BT
2775 battery = &hidpp->battery;
2776
2777 n = atomic_inc_return(&battery_no) - 1;
5b036ea1
BT
2778 desc->properties = battery_props;
2779 desc->num_properties = num_battery_props;
a52ec107
BT
2780 desc->get_property = hidpp_battery_get_property;
2781 sprintf(battery->name, "hidpp_battery_%ld", n);
2782 desc->name = battery->name;
2783 desc->type = POWER_SUPPLY_TYPE_BATTERY;
2784 desc->use_for_apm = 0;
2785
2786 battery->ps = devm_power_supply_register(&hidpp->hid_dev->dev,
2787 &battery->desc,
2788 &cfg);
2789 if (IS_ERR(battery->ps))
2790 return PTR_ERR(battery->ps);
2791
2792 power_supply_powers(battery->ps, &hidpp->hid_dev->dev);
2793
2794 return ret;
2795}
2796
843c624e 2797static void hidpp_overwrite_name(struct hid_device *hdev)
2f31c525
BT
2798{
2799 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2800 char *name;
2f31c525 2801
843c624e 2802 if (hidpp->protocol_major < 2)
b4f8ce07 2803 return;
843c624e
BT
2804
2805 name = hidpp_get_device_name(hidpp);
2f31c525 2806
7bfd2927 2807 if (!name) {
2f31c525 2808 hid_err(hdev, "unable to retrieve the name of the device");
7bfd2927
SW
2809 } else {
2810 dbg_hid("HID++: Got name: %s\n", name);
2f31c525 2811 snprintf(hdev->name, sizeof(hdev->name), "%s", name);
7bfd2927 2812 }
2f31c525
BT
2813
2814 kfree(name);
2815}
2816
c39e3d5f
BT
2817static int hidpp_input_open(struct input_dev *dev)
2818{
2819 struct hid_device *hid = input_get_drvdata(dev);
2820
2821 return hid_hw_open(hid);
2822}
2823
2824static void hidpp_input_close(struct input_dev *dev)
2825{
2826 struct hid_device *hid = input_get_drvdata(dev);
2827
2828 hid_hw_close(hid);
2829}
2830
2831static struct input_dev *hidpp_allocate_input(struct hid_device *hdev)
2832{
2833 struct input_dev *input_dev = devm_input_allocate_device(&hdev->dev);
005b3f57 2834 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
c39e3d5f
BT
2835
2836 if (!input_dev)
2837 return NULL;
2838
2839 input_set_drvdata(input_dev, hdev);
2840 input_dev->open = hidpp_input_open;
2841 input_dev->close = hidpp_input_close;
2842
005b3f57 2843 input_dev->name = hidpp->name;
c39e3d5f
BT
2844 input_dev->phys = hdev->phys;
2845 input_dev->uniq = hdev->uniq;
2846 input_dev->id.bustype = hdev->bus;
2847 input_dev->id.vendor = hdev->vendor;
2848 input_dev->id.product = hdev->product;
2849 input_dev->id.version = hdev->version;
2850 input_dev->dev.parent = &hdev->dev;
2851
2852 return input_dev;
2853}
2854
2855static void hidpp_connect_event(struct hidpp_device *hidpp)
2856{
2857 struct hid_device *hdev = hidpp->hid_dev;
2858 int ret = 0;
2859 bool connected = atomic_read(&hidpp->connected);
2860 struct input_dev *input;
2861 char *name, *devm_name;
c39e3d5f 2862
284f8d75
BT
2863 if (!connected) {
2864 if (hidpp->battery.ps) {
2865 hidpp->battery.online = false;
2866 hidpp->battery.status = POWER_SUPPLY_STATUS_UNKNOWN;
5b036ea1 2867 hidpp->battery.level = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
284f8d75
BT
2868 power_supply_changed(hidpp->battery.ps);
2869 }
2936836f 2870 return;
284f8d75 2871 }
2936836f 2872
bf159447
BT
2873 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP) {
2874 ret = wtp_connect(hdev, connected);
2875 if (ret)
2876 return;
8a09b4fa
GB
2877 } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560) {
2878 ret = m560_send_config_command(hdev, connected);
2879 if (ret)
2880 return;
90cdd986
BT
2881 } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_K400) {
2882 ret = k400_connect(hdev, connected);
2883 if (ret)
2884 return;
bf159447 2885 }
586bdc4e 2886
580a7e82
BT
2887 /* the device is already connected, we can ask for its name and
2888 * protocol */
c39e3d5f
BT
2889 if (!hidpp->protocol_major) {
2890 ret = !hidpp_is_connected(hidpp);
2891 if (ret) {
2892 hid_err(hdev, "Can not get the protocol version.\n");
2893 return;
2894 }
580a7e82
BT
2895 hid_info(hdev, "HID++ %u.%u device connected.\n",
2896 hidpp->protocol_major, hidpp->protocol_minor);
c39e3d5f
BT
2897 }
2898
187f2bba 2899 if (hidpp->name == hdev->name && hidpp->protocol_major >= 2) {
005b3f57
BT
2900 name = hidpp_get_device_name(hidpp);
2901 if (!name) {
2902 hid_err(hdev,
2903 "unable to retrieve the name of the device");
2904 return;
2905 }
2906
2907 devm_name = devm_kasprintf(&hdev->dev, GFP_KERNEL, "%s", name);
2908 kfree(name);
2909 if (!devm_name)
2910 return;
2911
2912 hidpp->name = devm_name;
2913 }
2914
187f2bba
BT
2915 hidpp_initialize_battery(hidpp);
2916
9b9c519f 2917 /* forward current battery state */
7f7ce2a2
BT
2918 if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP10_BATTERY) {
2919 hidpp10_enable_battery_reporting(hidpp);
2920 if (hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_MILEAGE)
2921 hidpp10_query_battery_mileage(hidpp);
2922 else
2923 hidpp10_query_battery_status(hidpp);
2924 } else if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP20_BATTERY) {
9b9c519f 2925 hidpp20_query_battery_info(hidpp);
9b9c519f 2926 }
7f7ce2a2
BT
2927 if (hidpp->battery.ps)
2928 power_supply_changed(hidpp->battery.ps);
9b9c519f 2929
2936836f
BT
2930 if (!(hidpp->quirks & HIDPP_QUIRK_NO_HIDINPUT) || hidpp->delayed_input)
2931 /* if the input nodes are already created, we can stop now */
187f2bba
BT
2932 return;
2933
c39e3d5f
BT
2934 input = hidpp_allocate_input(hdev);
2935 if (!input) {
2936 hid_err(hdev, "cannot allocate new input device: %d\n", ret);
2937 return;
2938 }
2939
c39e3d5f
BT
2940 hidpp_populate_input(hidpp, input, false);
2941
2942 ret = input_register_device(input);
2943 if (ret)
2944 input_free_device(input);
2945
2946 hidpp->delayed_input = input;
2947}
2948
a4bf6153
BT
2949static DEVICE_ATTR(builtin_power_supply, 0000, NULL, NULL);
2950
2951static struct attribute *sysfs_attrs[] = {
2952 &dev_attr_builtin_power_supply.attr,
2953 NULL
2954};
2955
35a33cb5 2956static const struct attribute_group ps_attribute_group = {
a4bf6153
BT
2957 .attrs = sysfs_attrs
2958};
2959
2f31c525
BT
2960static int hidpp_probe(struct hid_device *hdev, const struct hid_device_id *id)
2961{
2962 struct hidpp_device *hidpp;
2963 int ret;
2964 bool connected;
c39e3d5f 2965 unsigned int connect_mask = HID_CONNECT_DEFAULT;
2f31c525
BT
2966
2967 hidpp = devm_kzalloc(&hdev->dev, sizeof(struct hidpp_device),
2968 GFP_KERNEL);
2969 if (!hidpp)
2970 return -ENOMEM;
2971
2972 hidpp->hid_dev = hdev;
005b3f57 2973 hidpp->name = hdev->name;
2f31c525
BT
2974 hid_set_drvdata(hdev, hidpp);
2975
2976 hidpp->quirks = id->driver_data;
2977
843c624e
BT
2978 if (id->group == HID_GROUP_LOGITECH_DJ_DEVICE)
2979 hidpp->quirks |= HIDPP_QUIRK_UNIFYING;
2980
9188dbae
BT
2981 if (disable_raw_mode) {
2982 hidpp->quirks &= ~HIDPP_QUIRK_CLASS_WTP;
580a7e82 2983 hidpp->quirks &= ~HIDPP_QUIRK_NO_HIDINPUT;
9188dbae
BT
2984 }
2985
2f31c525
BT
2986 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP) {
2987 ret = wtp_allocate(hdev, id);
2988 if (ret)
8a09b4fa
GB
2989 goto allocate_fail;
2990 } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560) {
2991 ret = m560_allocate(hdev);
2992 if (ret)
2993 goto allocate_fail;
90cdd986
BT
2994 } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_K400) {
2995 ret = k400_allocate(hdev);
2996 if (ret)
2997 goto allocate_fail;
2f31c525
BT
2998 }
2999
c39e3d5f 3000 INIT_WORK(&hidpp->work, delayed_work_cb);
2f31c525
BT
3001 mutex_init(&hidpp->send_mutex);
3002 init_waitqueue_head(&hidpp->wait);
3003
a4bf6153
BT
3004 /* indicates we are handling the battery properties in the kernel */
3005 ret = sysfs_create_group(&hdev->dev.kobj, &ps_attribute_group);
3006 if (ret)
3007 hid_warn(hdev, "Cannot allocate sysfs group for %s\n",
3008 hdev->name);
3009
2f31c525
BT
3010 ret = hid_parse(hdev);
3011 if (ret) {
3012 hid_err(hdev, "%s:parse failed\n", __func__);
3013 goto hid_parse_fail;
3014 }
3015
7bfd2927
SW
3016 if (hidpp->quirks & HIDPP_QUIRK_NO_HIDINPUT)
3017 connect_mask &= ~HID_CONNECT_HIDINPUT;
3018
3019 if (hidpp->quirks & HIDPP_QUIRK_CLASS_G920) {
3020 ret = hid_hw_start(hdev, connect_mask);
3021 if (ret) {
3022 hid_err(hdev, "hw start failed\n");
3023 goto hid_hw_start_fail;
3024 }
3025 ret = hid_hw_open(hdev);
3026 if (ret < 0) {
3027 dev_err(&hdev->dev, "%s:hid_hw_open returned error:%d\n",
3028 __func__, ret);
3029 hid_hw_stop(hdev);
3030 goto hid_hw_start_fail;
3031 }
3032 }
3033
3034
2f31c525
BT
3035 /* Allow incoming packets */
3036 hid_device_io_start(hdev);
3037
843c624e
BT
3038 if (hidpp->quirks & HIDPP_QUIRK_UNIFYING)
3039 hidpp_unifying_init(hidpp);
3040
2f31c525 3041 connected = hidpp_is_connected(hidpp);
843c624e
BT
3042 atomic_set(&hidpp->connected, connected);
3043 if (!(hidpp->quirks & HIDPP_QUIRK_UNIFYING)) {
ab94e562 3044 if (!connected) {
b832da56 3045 ret = -ENODEV;
ab94e562 3046 hid_err(hdev, "Device not connected");
7bfd2927 3047 goto hid_hw_open_failed;
ab94e562 3048 }
2f31c525 3049
ab94e562
BT
3050 hid_info(hdev, "HID++ %u.%u device connected.\n",
3051 hidpp->protocol_major, hidpp->protocol_minor);
2f31c525 3052
843c624e
BT
3053 hidpp_overwrite_name(hdev);
3054 }
33797820 3055
c39e3d5f 3056 if (connected && (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)) {
2f31c525
BT
3057 ret = wtp_get_config(hidpp);
3058 if (ret)
7bfd2927 3059 goto hid_hw_open_failed;
7f4b49fe
SW
3060 } else if (connected && (hidpp->quirks & HIDPP_QUIRK_CLASS_G920)) {
3061 ret = g920_get_config(hidpp);
3062 if (ret)
3063 goto hid_hw_open_failed;
2f31c525
BT
3064 }
3065
3066 /* Block incoming packets */
3067 hid_device_io_stop(hdev);
3068
7bfd2927
SW
3069 if (!(hidpp->quirks & HIDPP_QUIRK_CLASS_G920)) {
3070 ret = hid_hw_start(hdev, connect_mask);
3071 if (ret) {
3072 hid_err(hdev, "%s:hid_hw_start returned error\n", __func__);
3073 goto hid_hw_start_fail;
3074 }
2f31c525
BT
3075 }
3076
6bd4e65d
BT
3077 /* Allow incoming packets */
3078 hid_device_io_start(hdev);
c39e3d5f 3079
6bd4e65d 3080 hidpp_connect_event(hidpp);
c39e3d5f 3081
2f31c525
BT
3082 return ret;
3083
7bfd2927
SW
3084hid_hw_open_failed:
3085 hid_device_io_stop(hdev);
3086 if (hidpp->quirks & HIDPP_QUIRK_CLASS_G920) {
3087 hid_hw_close(hdev);
3088 hid_hw_stop(hdev);
3089 }
2f31c525
BT
3090hid_hw_start_fail:
3091hid_parse_fail:
a4bf6153 3092 sysfs_remove_group(&hdev->dev.kobj, &ps_attribute_group);
c39e3d5f 3093 cancel_work_sync(&hidpp->work);
2f31c525 3094 mutex_destroy(&hidpp->send_mutex);
8a09b4fa 3095allocate_fail:
2f31c525
BT
3096 hid_set_drvdata(hdev, NULL);
3097 return ret;
3098}
3099
3100static void hidpp_remove(struct hid_device *hdev)
3101{
3102 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3103
a4bf6153
BT
3104 sysfs_remove_group(&hdev->dev.kobj, &ps_attribute_group);
3105
7f4b49fe 3106 if (hidpp->quirks & HIDPP_QUIRK_CLASS_G920) {
ff21a635 3107 hidpp_ff_deinit(hdev);
7bfd2927 3108 hid_hw_close(hdev);
7f4b49fe 3109 }
7bfd2927 3110 hid_hw_stop(hdev);
c39e3d5f 3111 cancel_work_sync(&hidpp->work);
2f31c525 3112 mutex_destroy(&hidpp->send_mutex);
2f31c525
BT
3113}
3114
3115static const struct hid_device_id hidpp_devices[] = {
57ac86cf
BT
3116 { /* wireless touchpad */
3117 HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE,
3118 USB_VENDOR_ID_LOGITECH, 0x4011),
3119 .driver_data = HIDPP_QUIRK_CLASS_WTP | HIDPP_QUIRK_DELAYED_INIT |
3120 HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS },
586bdc4e
BT
3121 { /* wireless touchpad T650 */
3122 HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE,
3123 USB_VENDOR_ID_LOGITECH, 0x4101),
3124 .driver_data = HIDPP_QUIRK_CLASS_WTP | HIDPP_QUIRK_DELAYED_INIT },
2f31c525
BT
3125 { /* wireless touchpad T651 */
3126 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH,
3127 USB_DEVICE_ID_LOGITECH_T651),
3128 .driver_data = HIDPP_QUIRK_CLASS_WTP },
8a09b4fa 3129 { /* Mouse logitech M560 */
3a61e975 3130 HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE,
8a09b4fa
GB
3131 USB_VENDOR_ID_LOGITECH, 0x402d),
3132 .driver_data = HIDPP_QUIRK_DELAYED_INIT | HIDPP_QUIRK_CLASS_M560 },
90cdd986
BT
3133 { /* Keyboard logitech K400 */
3134 HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE,
3135 USB_VENDOR_ID_LOGITECH, 0x4024),
6bd4e65d 3136 .driver_data = HIDPP_QUIRK_CLASS_K400 },
696ecef9
BT
3137 { /* Solar Keyboard Logitech K750 */
3138 HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE,
3139 USB_VENDOR_ID_LOGITECH, 0x4002),
3140 .driver_data = HIDPP_QUIRK_CLASS_K750 },
ab94e562
BT
3141
3142 { HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE,
3143 USB_VENDOR_ID_LOGITECH, HID_ANY_ID)},
7bfd2927
SW
3144
3145 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_G920_WHEEL),
3146 .driver_data = HIDPP_QUIRK_CLASS_G920 | HIDPP_QUIRK_FORCE_OUTPUT_REPORTS},
2f31c525
BT
3147 {}
3148};
3149
3150MODULE_DEVICE_TABLE(hid, hidpp_devices);
3151
3152static struct hid_driver hidpp_driver = {
3153 .name = "logitech-hidpp-device",
3154 .id_table = hidpp_devices,
3155 .probe = hidpp_probe,
3156 .remove = hidpp_remove,
3157 .raw_event = hidpp_raw_event,
3158 .input_configured = hidpp_input_configured,
3159 .input_mapping = hidpp_input_mapping,
0b1804e3 3160 .input_mapped = hidpp_input_mapped,
2f31c525
BT
3161};
3162
3163module_hid_driver(hidpp_driver);