blob: b3cf6fd4be96473ba62ebbbf92d1522c024a509e [file] [log] [blame]
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001/*
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>
18#include <linux/hid.h>
19#include <linux/module.h>
20#include <linux/slab.h>
21#include <linux/sched.h>
22#include <linux/kfifo.h>
23#include <linux/input/mt.h>
24#include <asm/unaligned.h>
25#include "hid-ids.h"
26
27MODULE_LICENSE("GPL");
28MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
29MODULE_AUTHOR("Nestor Lopez Casado <nlopezcasad@logitech.com>");
30
Benjamin Tissoires9188dba2015-03-26 12:41:57 -040031static bool disable_raw_mode;
32module_param(disable_raw_mode, bool, 0644);
33MODULE_PARM_DESC(disable_raw_mode,
34 "Disable Raw mode reporting for touchpads and keep firmware gestures.");
35
Benjamin Tissoires2f31c522014-09-30 13:18:27 -040036#define REPORT_ID_HIDPP_SHORT 0x10
37#define REPORT_ID_HIDPP_LONG 0x11
38
39#define HIDPP_REPORT_SHORT_LENGTH 7
40#define HIDPP_REPORT_LONG_LENGTH 20
41
42#define HIDPP_QUIRK_CLASS_WTP BIT(0)
43
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -040044/* bits 1..20 are reserved for classes */
45#define HIDPP_QUIRK_DELAYED_INIT BIT(21)
Benjamin Tissoires57ac86c2014-09-30 13:18:34 -040046#define HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS BIT(22)
Benjamin Tissoires3a61e972014-09-30 13:18:35 -040047#define HIDPP_QUIRK_MULTI_INPUT BIT(23)
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -040048
Benjamin Tissoires2f31c522014-09-30 13:18:27 -040049/*
50 * There are two hidpp protocols in use, the first version hidpp10 is known
51 * as register access protocol or RAP, the second version hidpp20 is known as
52 * feature access protocol or FAP
53 *
54 * Most older devices (including the Unifying usb receiver) use the RAP protocol
55 * where as most newer devices use the FAP protocol. Both protocols are
56 * compatible with the underlying transport, which could be usb, Unifiying, or
57 * bluetooth. The message lengths are defined by the hid vendor specific report
58 * descriptor for the HIDPP_SHORT report type (total message lenth 7 bytes) and
59 * the HIDPP_LONG report type (total message length 20 bytes)
60 *
61 * The RAP protocol uses both report types, whereas the FAP only uses HIDPP_LONG
62 * messages. The Unifying receiver itself responds to RAP messages (device index
63 * is 0xFF for the receiver), and all messages (short or long) with a device
64 * index between 1 and 6 are passed untouched to the corresponding paired
65 * Unifying device.
66 *
67 * The paired device can be RAP or FAP, it will receive the message untouched
68 * from the Unifiying receiver.
69 */
70
71struct fap {
72 u8 feature_index;
73 u8 funcindex_clientid;
74 u8 params[HIDPP_REPORT_LONG_LENGTH - 4U];
75};
76
77struct rap {
78 u8 sub_id;
79 u8 reg_address;
80 u8 params[HIDPP_REPORT_LONG_LENGTH - 4U];
81};
82
83struct hidpp_report {
84 u8 report_id;
85 u8 device_index;
86 union {
87 struct fap fap;
88 struct rap rap;
89 u8 rawbytes[sizeof(struct fap)];
90 };
91} __packed;
92
93struct hidpp_device {
94 struct hid_device *hid_dev;
95 struct mutex send_mutex;
96 void *send_receive_buf;
Benjamin Tissoires005b3f52015-01-08 14:37:12 -050097 char *name; /* will never be NULL and should not be freed */
Benjamin Tissoires2f31c522014-09-30 13:18:27 -040098 wait_queue_head_t wait;
99 bool answer_available;
100 u8 protocol_major;
101 u8 protocol_minor;
102
103 void *private_data;
104
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -0400105 struct work_struct work;
106 struct kfifo delayed_work_fifo;
107 atomic_t connected;
108 struct input_dev *delayed_input;
109
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400110 unsigned long quirks;
111};
112
113
Peter Wuf677bb12014-12-16 01:50:14 +0100114/* HID++ 1.0 error codes */
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400115#define HIDPP_ERROR 0x8f
116#define HIDPP_ERROR_SUCCESS 0x00
117#define HIDPP_ERROR_INVALID_SUBID 0x01
118#define HIDPP_ERROR_INVALID_ADRESS 0x02
119#define HIDPP_ERROR_INVALID_VALUE 0x03
120#define HIDPP_ERROR_CONNECT_FAIL 0x04
121#define HIDPP_ERROR_TOO_MANY_DEVICES 0x05
122#define HIDPP_ERROR_ALREADY_EXISTS 0x06
123#define HIDPP_ERROR_BUSY 0x07
124#define HIDPP_ERROR_UNKNOWN_DEVICE 0x08
125#define HIDPP_ERROR_RESOURCE_ERROR 0x09
126#define HIDPP_ERROR_REQUEST_UNAVAILABLE 0x0a
127#define HIDPP_ERROR_INVALID_PARAM_VALUE 0x0b
128#define HIDPP_ERROR_WRONG_PIN_CODE 0x0c
Peter Wuf677bb12014-12-16 01:50:14 +0100129/* HID++ 2.0 error codes */
130#define HIDPP20_ERROR 0xff
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400131
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -0400132static void hidpp_connect_event(struct hidpp_device *hidpp_dev);
133
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400134static int __hidpp_send_report(struct hid_device *hdev,
135 struct hidpp_report *hidpp_report)
136{
137 int fields_count, ret;
138
139 switch (hidpp_report->report_id) {
140 case REPORT_ID_HIDPP_SHORT:
141 fields_count = HIDPP_REPORT_SHORT_LENGTH;
142 break;
143 case REPORT_ID_HIDPP_LONG:
144 fields_count = HIDPP_REPORT_LONG_LENGTH;
145 break;
146 default:
147 return -ENODEV;
148 }
149
150 /*
151 * set the device_index as the receiver, it will be overwritten by
152 * hid_hw_request if needed
153 */
154 hidpp_report->device_index = 0xff;
155
156 ret = hid_hw_raw_request(hdev, hidpp_report->report_id,
157 (u8 *)hidpp_report, fields_count, HID_OUTPUT_REPORT,
158 HID_REQ_SET_REPORT);
159
160 return ret == fields_count ? 0 : -1;
161}
162
Benjamin Tissoires8c9952b2014-11-03 16:09:58 -0500163/**
164 * hidpp_send_message_sync() returns 0 in case of success, and something else
165 * in case of a failure.
166 * - If ' something else' is positive, that means that an error has been raised
167 * by the protocol itself.
168 * - If ' something else' is negative, that means that we had a classic error
169 * (-ENOMEM, -EPIPE, etc...)
170 */
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400171static int hidpp_send_message_sync(struct hidpp_device *hidpp,
172 struct hidpp_report *message,
173 struct hidpp_report *response)
174{
175 int ret;
176
177 mutex_lock(&hidpp->send_mutex);
178
179 hidpp->send_receive_buf = response;
180 hidpp->answer_available = false;
181
182 /*
183 * So that we can later validate the answer when it arrives
184 * in hidpp_raw_event
185 */
186 *response = *message;
187
188 ret = __hidpp_send_report(hidpp->hid_dev, message);
189
190 if (ret) {
191 dbg_hid("__hidpp_send_report returned err: %d\n", ret);
192 memset(response, 0, sizeof(struct hidpp_report));
193 goto exit;
194 }
195
196 if (!wait_event_timeout(hidpp->wait, hidpp->answer_available,
197 5*HZ)) {
198 dbg_hid("%s:timeout waiting for response\n", __func__);
199 memset(response, 0, sizeof(struct hidpp_report));
200 ret = -ETIMEDOUT;
201 }
202
203 if (response->report_id == REPORT_ID_HIDPP_SHORT &&
Peter Wuf677bb12014-12-16 01:50:14 +0100204 response->rap.sub_id == HIDPP_ERROR) {
205 ret = response->rap.params[1];
206 dbg_hid("%s:got hidpp error %02X\n", __func__, ret);
207 goto exit;
208 }
209
210 if (response->report_id == REPORT_ID_HIDPP_LONG &&
211 response->fap.feature_index == HIDPP20_ERROR) {
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400212 ret = response->fap.params[1];
Peter Wuf677bb12014-12-16 01:50:14 +0100213 dbg_hid("%s:got hidpp 2.0 error %02X\n", __func__, ret);
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400214 goto exit;
215 }
216
217exit:
218 mutex_unlock(&hidpp->send_mutex);
219 return ret;
220
221}
222
223static int hidpp_send_fap_command_sync(struct hidpp_device *hidpp,
224 u8 feat_index, u8 funcindex_clientid, u8 *params, int param_count,
225 struct hidpp_report *response)
226{
Dan Carpenter3e7830c2014-10-31 12:14:39 +0300227 struct hidpp_report *message;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400228 int ret;
229
230 if (param_count > sizeof(message->fap.params))
231 return -EINVAL;
232
Dan Carpenter3e7830c2014-10-31 12:14:39 +0300233 message = kzalloc(sizeof(struct hidpp_report), GFP_KERNEL);
234 if (!message)
235 return -ENOMEM;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400236 message->report_id = REPORT_ID_HIDPP_LONG;
237 message->fap.feature_index = feat_index;
238 message->fap.funcindex_clientid = funcindex_clientid;
239 memcpy(&message->fap.params, params, param_count);
240
241 ret = hidpp_send_message_sync(hidpp, message, response);
242 kfree(message);
243 return ret;
244}
245
Benjamin Tissoires33797822014-09-30 13:18:30 -0400246static int hidpp_send_rap_command_sync(struct hidpp_device *hidpp_dev,
247 u8 report_id, u8 sub_id, u8 reg_address, u8 *params, int param_count,
248 struct hidpp_report *response)
249{
Dan Carpenter3e7830c2014-10-31 12:14:39 +0300250 struct hidpp_report *message;
Benjamin Tissoires33797822014-09-30 13:18:30 -0400251 int ret;
252
253 if ((report_id != REPORT_ID_HIDPP_SHORT) &&
254 (report_id != REPORT_ID_HIDPP_LONG))
255 return -EINVAL;
256
257 if (param_count > sizeof(message->rap.params))
258 return -EINVAL;
259
Dan Carpenter3e7830c2014-10-31 12:14:39 +0300260 message = kzalloc(sizeof(struct hidpp_report), GFP_KERNEL);
261 if (!message)
262 return -ENOMEM;
Benjamin Tissoires33797822014-09-30 13:18:30 -0400263 message->report_id = report_id;
264 message->rap.sub_id = sub_id;
265 message->rap.reg_address = reg_address;
266 memcpy(&message->rap.params, params, param_count);
267
268 ret = hidpp_send_message_sync(hidpp_dev, message, response);
269 kfree(message);
270 return ret;
271}
272
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -0400273static void delayed_work_cb(struct work_struct *work)
274{
275 struct hidpp_device *hidpp = container_of(work, struct hidpp_device,
276 work);
277 hidpp_connect_event(hidpp);
278}
279
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400280static inline bool hidpp_match_answer(struct hidpp_report *question,
281 struct hidpp_report *answer)
282{
283 return (answer->fap.feature_index == question->fap.feature_index) &&
284 (answer->fap.funcindex_clientid == question->fap.funcindex_clientid);
285}
286
287static inline bool hidpp_match_error(struct hidpp_report *question,
288 struct hidpp_report *answer)
289{
Peter Wuf677bb12014-12-16 01:50:14 +0100290 return ((answer->rap.sub_id == HIDPP_ERROR) ||
291 (answer->fap.feature_index == HIDPP20_ERROR)) &&
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400292 (answer->fap.funcindex_clientid == question->fap.feature_index) &&
293 (answer->fap.params[0] == question->fap.funcindex_clientid);
294}
295
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -0400296static inline bool hidpp_report_is_connect_event(struct hidpp_report *report)
297{
298 return (report->report_id == REPORT_ID_HIDPP_SHORT) &&
299 (report->rap.sub_id == 0x41);
300}
301
Benjamin Tissoiresa0e625f2014-12-11 17:39:59 -0500302/**
303 * hidpp_prefix_name() prefixes the current given name with "Logitech ".
304 */
305static void hidpp_prefix_name(char **name, int name_length)
306{
307#define PREFIX_LENGTH 9 /* "Logitech " */
308
309 int new_length;
310 char *new_name;
311
312 if (name_length > PREFIX_LENGTH &&
313 strncmp(*name, "Logitech ", PREFIX_LENGTH) == 0)
314 /* The prefix has is already in the name */
315 return;
316
317 new_length = PREFIX_LENGTH + name_length;
318 new_name = kzalloc(new_length, GFP_KERNEL);
319 if (!new_name)
320 return;
321
322 snprintf(new_name, new_length, "Logitech %s", *name);
323
324 kfree(*name);
325
326 *name = new_name;
327}
328
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400329/* -------------------------------------------------------------------------- */
Benjamin Tissoires33797822014-09-30 13:18:30 -0400330/* HIDP++ 1.0 commands */
331/* -------------------------------------------------------------------------- */
332
333#define HIDPP_SET_REGISTER 0x80
334#define HIDPP_GET_REGISTER 0x81
335#define HIDPP_SET_LONG_REGISTER 0x82
336#define HIDPP_GET_LONG_REGISTER 0x83
337
338#define HIDPP_REG_PAIRING_INFORMATION 0xB5
339#define DEVICE_NAME 0x40
340
341static char *hidpp_get_unifying_name(struct hidpp_device *hidpp_dev)
342{
343 struct hidpp_report response;
344 int ret;
345 /* hid-logitech-dj is in charge of setting the right device index */
346 u8 params[1] = { DEVICE_NAME };
347 char *name;
348 int len;
349
350 ret = hidpp_send_rap_command_sync(hidpp_dev,
351 REPORT_ID_HIDPP_SHORT,
352 HIDPP_GET_LONG_REGISTER,
353 HIDPP_REG_PAIRING_INFORMATION,
354 params, 1, &response);
355 if (ret)
356 return NULL;
357
358 len = response.rap.params[1];
359
Peter Wu3a034a72014-12-11 13:51:19 +0100360 if (2 + len > sizeof(response.rap.params))
361 return NULL;
362
Benjamin Tissoires33797822014-09-30 13:18:30 -0400363 name = kzalloc(len + 1, GFP_KERNEL);
364 if (!name)
365 return NULL;
366
367 memcpy(name, &response.rap.params[2], len);
Benjamin Tissoiresa0e625f2014-12-11 17:39:59 -0500368
369 /* include the terminating '\0' */
370 hidpp_prefix_name(&name, len + 1);
371
Benjamin Tissoires33797822014-09-30 13:18:30 -0400372 return name;
373}
374
375/* -------------------------------------------------------------------------- */
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400376/* 0x0000: Root */
377/* -------------------------------------------------------------------------- */
378
379#define HIDPP_PAGE_ROOT 0x0000
380#define HIDPP_PAGE_ROOT_IDX 0x00
381
382#define CMD_ROOT_GET_FEATURE 0x01
383#define CMD_ROOT_GET_PROTOCOL_VERSION 0x11
384
385static int hidpp_root_get_feature(struct hidpp_device *hidpp, u16 feature,
386 u8 *feature_index, u8 *feature_type)
387{
388 struct hidpp_report response;
389 int ret;
390 u8 params[2] = { feature >> 8, feature & 0x00FF };
391
392 ret = hidpp_send_fap_command_sync(hidpp,
393 HIDPP_PAGE_ROOT_IDX,
394 CMD_ROOT_GET_FEATURE,
395 params, 2, &response);
396 if (ret)
397 return ret;
398
399 *feature_index = response.fap.params[0];
400 *feature_type = response.fap.params[1];
401
402 return ret;
403}
404
405static int hidpp_root_get_protocol_version(struct hidpp_device *hidpp)
406{
407 struct hidpp_report response;
408 int ret;
409
410 ret = hidpp_send_fap_command_sync(hidpp,
411 HIDPP_PAGE_ROOT_IDX,
412 CMD_ROOT_GET_PROTOCOL_VERSION,
413 NULL, 0, &response);
414
Benjamin Tissoires552f12e2014-11-03 16:09:59 -0500415 if (ret == HIDPP_ERROR_INVALID_SUBID) {
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400416 hidpp->protocol_major = 1;
417 hidpp->protocol_minor = 0;
418 return 0;
419 }
420
Benjamin Tissoires552f12e2014-11-03 16:09:59 -0500421 /* the device might not be connected */
422 if (ret == HIDPP_ERROR_RESOURCE_ERROR)
423 return -EIO;
424
Benjamin Tissoires8c9952b2014-11-03 16:09:58 -0500425 if (ret > 0) {
426 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
427 __func__, ret);
428 return -EPROTO;
429 }
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400430 if (ret)
Benjamin Tissoires8c9952b2014-11-03 16:09:58 -0500431 return ret;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400432
433 hidpp->protocol_major = response.fap.params[0];
434 hidpp->protocol_minor = response.fap.params[1];
435
436 return ret;
437}
438
439static bool hidpp_is_connected(struct hidpp_device *hidpp)
440{
441 int ret;
442
443 ret = hidpp_root_get_protocol_version(hidpp);
444 if (!ret)
445 hid_dbg(hidpp->hid_dev, "HID++ %u.%u device connected.\n",
446 hidpp->protocol_major, hidpp->protocol_minor);
447 return ret == 0;
448}
449
450/* -------------------------------------------------------------------------- */
451/* 0x0005: GetDeviceNameType */
452/* -------------------------------------------------------------------------- */
453
454#define HIDPP_PAGE_GET_DEVICE_NAME_TYPE 0x0005
455
456#define CMD_GET_DEVICE_NAME_TYPE_GET_COUNT 0x01
457#define CMD_GET_DEVICE_NAME_TYPE_GET_DEVICE_NAME 0x11
458#define CMD_GET_DEVICE_NAME_TYPE_GET_TYPE 0x21
459
460static int hidpp_devicenametype_get_count(struct hidpp_device *hidpp,
461 u8 feature_index, u8 *nameLength)
462{
463 struct hidpp_report response;
464 int ret;
465
466 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
467 CMD_GET_DEVICE_NAME_TYPE_GET_COUNT, NULL, 0, &response);
468
Benjamin Tissoires8c9952b2014-11-03 16:09:58 -0500469 if (ret > 0) {
470 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
471 __func__, ret);
472 return -EPROTO;
473 }
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400474 if (ret)
Benjamin Tissoires8c9952b2014-11-03 16:09:58 -0500475 return ret;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400476
477 *nameLength = response.fap.params[0];
478
479 return ret;
480}
481
482static int hidpp_devicenametype_get_device_name(struct hidpp_device *hidpp,
483 u8 feature_index, u8 char_index, char *device_name, int len_buf)
484{
485 struct hidpp_report response;
486 int ret, i;
487 int count;
488
489 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
490 CMD_GET_DEVICE_NAME_TYPE_GET_DEVICE_NAME, &char_index, 1,
491 &response);
492
Benjamin Tissoires8c9952b2014-11-03 16:09:58 -0500493 if (ret > 0) {
494 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
495 __func__, ret);
496 return -EPROTO;
497 }
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400498 if (ret)
Benjamin Tissoires8c9952b2014-11-03 16:09:58 -0500499 return ret;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400500
501 if (response.report_id == REPORT_ID_HIDPP_LONG)
502 count = HIDPP_REPORT_LONG_LENGTH - 4;
503 else
504 count = HIDPP_REPORT_SHORT_LENGTH - 4;
505
506 if (len_buf < count)
507 count = len_buf;
508
509 for (i = 0; i < count; i++)
510 device_name[i] = response.fap.params[i];
511
512 return count;
513}
514
Peter Wu02cc0972014-12-11 13:51:17 +0100515static char *hidpp_get_device_name(struct hidpp_device *hidpp)
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400516{
517 u8 feature_type;
518 u8 feature_index;
519 u8 __name_length;
520 char *name;
521 unsigned index = 0;
522 int ret;
523
524 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_GET_DEVICE_NAME_TYPE,
525 &feature_index, &feature_type);
526 if (ret)
Peter Wu02cc0972014-12-11 13:51:17 +0100527 return NULL;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400528
529 ret = hidpp_devicenametype_get_count(hidpp, feature_index,
530 &__name_length);
531 if (ret)
Peter Wu02cc0972014-12-11 13:51:17 +0100532 return NULL;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400533
534 name = kzalloc(__name_length + 1, GFP_KERNEL);
535 if (!name)
Peter Wu02cc0972014-12-11 13:51:17 +0100536 return NULL;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400537
Peter Wu1430ee72014-12-11 13:51:18 +0100538 while (index < __name_length) {
539 ret = hidpp_devicenametype_get_device_name(hidpp,
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400540 feature_index, index, name + index,
541 __name_length - index);
Peter Wu1430ee72014-12-11 13:51:18 +0100542 if (ret <= 0) {
543 kfree(name);
544 return NULL;
545 }
546 index += ret;
547 }
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400548
Benjamin Tissoiresa0e625f2014-12-11 17:39:59 -0500549 /* include the terminating '\0' */
550 hidpp_prefix_name(&name, __name_length + 1);
551
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400552 return name;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400553}
554
555/* -------------------------------------------------------------------------- */
556/* 0x6100: TouchPadRawXY */
557/* -------------------------------------------------------------------------- */
558
559#define HIDPP_PAGE_TOUCHPAD_RAW_XY 0x6100
560
561#define CMD_TOUCHPAD_GET_RAW_INFO 0x01
Benjamin Tissoires586bdc42014-09-30 13:18:33 -0400562#define CMD_TOUCHPAD_SET_RAW_REPORT_STATE 0x21
563
564#define EVENT_TOUCHPAD_RAW_XY 0x00
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400565
566#define TOUCHPAD_RAW_XY_ORIGIN_LOWER_LEFT 0x01
567#define TOUCHPAD_RAW_XY_ORIGIN_UPPER_LEFT 0x03
568
569struct hidpp_touchpad_raw_info {
570 u16 x_size;
571 u16 y_size;
572 u8 z_range;
573 u8 area_range;
574 u8 timestamp_unit;
575 u8 maxcontacts;
576 u8 origin;
577 u16 res;
578};
579
580struct hidpp_touchpad_raw_xy_finger {
581 u8 contact_type;
582 u8 contact_status;
583 u16 x;
584 u16 y;
585 u8 z;
586 u8 area;
587 u8 finger_id;
588};
589
590struct hidpp_touchpad_raw_xy {
591 u16 timestamp;
592 struct hidpp_touchpad_raw_xy_finger fingers[2];
593 u8 spurious_flag;
594 u8 end_of_frame;
595 u8 finger_count;
596 u8 button;
597};
598
599static int hidpp_touchpad_get_raw_info(struct hidpp_device *hidpp,
600 u8 feature_index, struct hidpp_touchpad_raw_info *raw_info)
601{
602 struct hidpp_report response;
603 int ret;
604 u8 *params = (u8 *)response.fap.params;
605
606 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
607 CMD_TOUCHPAD_GET_RAW_INFO, NULL, 0, &response);
608
Benjamin Tissoires8c9952b2014-11-03 16:09:58 -0500609 if (ret > 0) {
610 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
611 __func__, ret);
612 return -EPROTO;
613 }
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400614 if (ret)
Benjamin Tissoires8c9952b2014-11-03 16:09:58 -0500615 return ret;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400616
617 raw_info->x_size = get_unaligned_be16(&params[0]);
618 raw_info->y_size = get_unaligned_be16(&params[2]);
619 raw_info->z_range = params[4];
620 raw_info->area_range = params[5];
621 raw_info->maxcontacts = params[7];
622 raw_info->origin = params[8];
623 /* res is given in unit per inch */
624 raw_info->res = get_unaligned_be16(&params[13]) * 2 / 51;
625
626 return ret;
627}
628
Benjamin Tissoires586bdc42014-09-30 13:18:33 -0400629static int hidpp_touchpad_set_raw_report_state(struct hidpp_device *hidpp_dev,
630 u8 feature_index, bool send_raw_reports,
631 bool sensor_enhanced_settings)
632{
633 struct hidpp_report response;
634
635 /*
636 * Params:
637 * bit 0 - enable raw
638 * bit 1 - 16bit Z, no area
639 * bit 2 - enhanced sensitivity
640 * bit 3 - width, height (4 bits each) instead of area
641 * bit 4 - send raw + gestures (degrades smoothness)
642 * remaining bits - reserved
643 */
644 u8 params = send_raw_reports | (sensor_enhanced_settings << 2);
645
646 return hidpp_send_fap_command_sync(hidpp_dev, feature_index,
647 CMD_TOUCHPAD_SET_RAW_REPORT_STATE, &params, 1, &response);
648}
649
650static void hidpp_touchpad_touch_event(u8 *data,
651 struct hidpp_touchpad_raw_xy_finger *finger)
652{
653 u8 x_m = data[0] << 2;
654 u8 y_m = data[2] << 2;
655
656 finger->x = x_m << 6 | data[1];
657 finger->y = y_m << 6 | data[3];
658
659 finger->contact_type = data[0] >> 6;
660 finger->contact_status = data[2] >> 6;
661
662 finger->z = data[4];
663 finger->area = data[5];
664 finger->finger_id = data[6] >> 4;
665}
666
667static void hidpp_touchpad_raw_xy_event(struct hidpp_device *hidpp_dev,
668 u8 *data, struct hidpp_touchpad_raw_xy *raw_xy)
669{
670 memset(raw_xy, 0, sizeof(struct hidpp_touchpad_raw_xy));
671 raw_xy->end_of_frame = data[8] & 0x01;
672 raw_xy->spurious_flag = (data[8] >> 1) & 0x01;
673 raw_xy->finger_count = data[15] & 0x0f;
674 raw_xy->button = (data[8] >> 2) & 0x01;
675
676 if (raw_xy->finger_count) {
677 hidpp_touchpad_touch_event(&data[2], &raw_xy->fingers[0]);
678 hidpp_touchpad_touch_event(&data[9], &raw_xy->fingers[1]);
679 }
680}
681
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400682/* ************************************************************************** */
683/* */
684/* Device Support */
685/* */
686/* ************************************************************************** */
687
688/* -------------------------------------------------------------------------- */
689/* Touchpad HID++ devices */
690/* -------------------------------------------------------------------------- */
691
Benjamin Tissoires57ac86c2014-09-30 13:18:34 -0400692#define WTP_MANUAL_RESOLUTION 39
693
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400694struct wtp_data {
695 struct input_dev *input;
696 u16 x_size, y_size;
697 u8 finger_count;
698 u8 mt_feature_index;
699 u8 button_feature_index;
700 u8 maxcontacts;
701 bool flip_y;
702 unsigned int resolution;
703};
704
705static int wtp_input_mapping(struct hid_device *hdev, struct hid_input *hi,
706 struct hid_field *field, struct hid_usage *usage,
707 unsigned long **bit, int *max)
708{
Benjamin Tissoires3a61e972014-09-30 13:18:35 -0400709 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
710
711 if ((hidpp->quirks & HIDPP_QUIRK_MULTI_INPUT) &&
712 (field->application == HID_GD_KEYBOARD))
713 return 0;
714
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400715 return -1;
716}
717
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -0400718static void wtp_populate_input(struct hidpp_device *hidpp,
719 struct input_dev *input_dev, bool origin_is_hid_core)
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400720{
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400721 struct wtp_data *wd = hidpp->private_data;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400722
Benjamin Tissoires3a61e972014-09-30 13:18:35 -0400723 if ((hidpp->quirks & HIDPP_QUIRK_MULTI_INPUT) && origin_is_hid_core)
724 /* this is the generic hid-input call */
725 return;
726
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400727 __set_bit(EV_ABS, input_dev->evbit);
728 __set_bit(EV_KEY, input_dev->evbit);
729 __clear_bit(EV_REL, input_dev->evbit);
730 __clear_bit(EV_LED, input_dev->evbit);
731
732 input_set_abs_params(input_dev, ABS_MT_POSITION_X, 0, wd->x_size, 0, 0);
733 input_abs_set_res(input_dev, ABS_MT_POSITION_X, wd->resolution);
734 input_set_abs_params(input_dev, ABS_MT_POSITION_Y, 0, wd->y_size, 0, 0);
735 input_abs_set_res(input_dev, ABS_MT_POSITION_Y, wd->resolution);
736
737 /* Max pressure is not given by the devices, pick one */
738 input_set_abs_params(input_dev, ABS_MT_PRESSURE, 0, 50, 0, 0);
739
740 input_set_capability(input_dev, EV_KEY, BTN_LEFT);
741
Benjamin Tissoires57ac86c2014-09-30 13:18:34 -0400742 if (hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS)
743 input_set_capability(input_dev, EV_KEY, BTN_RIGHT);
744 else
745 __set_bit(INPUT_PROP_BUTTONPAD, input_dev->propbit);
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400746
747 input_mt_init_slots(input_dev, wd->maxcontacts, INPUT_MT_POINTER |
748 INPUT_MT_DROP_UNUSED);
749
750 wd->input = input_dev;
751}
752
753static void wtp_touch_event(struct wtp_data *wd,
754 struct hidpp_touchpad_raw_xy_finger *touch_report)
755{
756 int slot;
757
758 if (!touch_report->finger_id || touch_report->contact_type)
759 /* no actual data */
760 return;
761
762 slot = input_mt_get_slot_by_key(wd->input, touch_report->finger_id);
763
764 input_mt_slot(wd->input, slot);
765 input_mt_report_slot_state(wd->input, MT_TOOL_FINGER,
766 touch_report->contact_status);
767 if (touch_report->contact_status) {
768 input_event(wd->input, EV_ABS, ABS_MT_POSITION_X,
769 touch_report->x);
770 input_event(wd->input, EV_ABS, ABS_MT_POSITION_Y,
771 wd->flip_y ? wd->y_size - touch_report->y :
772 touch_report->y);
773 input_event(wd->input, EV_ABS, ABS_MT_PRESSURE,
774 touch_report->area);
775 }
776}
777
778static void wtp_send_raw_xy_event(struct hidpp_device *hidpp,
779 struct hidpp_touchpad_raw_xy *raw)
780{
781 struct wtp_data *wd = hidpp->private_data;
782 int i;
783
784 for (i = 0; i < 2; i++)
785 wtp_touch_event(wd, &(raw->fingers[i]));
786
Benjamin Tissoires57ac86c2014-09-30 13:18:34 -0400787 if (raw->end_of_frame &&
788 !(hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS))
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400789 input_event(wd->input, EV_KEY, BTN_LEFT, raw->button);
790
791 if (raw->end_of_frame || raw->finger_count <= 2) {
792 input_mt_sync_frame(wd->input);
793 input_sync(wd->input);
794 }
795}
796
797static int wtp_mouse_raw_xy_event(struct hidpp_device *hidpp, u8 *data)
798{
799 struct wtp_data *wd = hidpp->private_data;
800 u8 c1_area = ((data[7] & 0xf) * (data[7] & 0xf) +
801 (data[7] >> 4) * (data[7] >> 4)) / 2;
802 u8 c2_area = ((data[13] & 0xf) * (data[13] & 0xf) +
803 (data[13] >> 4) * (data[13] >> 4)) / 2;
804 struct hidpp_touchpad_raw_xy raw = {
805 .timestamp = data[1],
806 .fingers = {
807 {
808 .contact_type = 0,
809 .contact_status = !!data[7],
810 .x = get_unaligned_le16(&data[3]),
811 .y = get_unaligned_le16(&data[5]),
812 .z = c1_area,
813 .area = c1_area,
814 .finger_id = data[2],
815 }, {
816 .contact_type = 0,
817 .contact_status = !!data[13],
818 .x = get_unaligned_le16(&data[9]),
819 .y = get_unaligned_le16(&data[11]),
820 .z = c2_area,
821 .area = c2_area,
822 .finger_id = data[8],
823 }
824 },
825 .finger_count = wd->maxcontacts,
826 .spurious_flag = 0,
827 .end_of_frame = (data[0] >> 7) == 0,
828 .button = data[0] & 0x01,
829 };
830
831 wtp_send_raw_xy_event(hidpp, &raw);
832
833 return 1;
834}
835
836static int wtp_raw_event(struct hid_device *hdev, u8 *data, int size)
837{
838 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
839 struct wtp_data *wd = hidpp->private_data;
Benjamin Tissoires586bdc42014-09-30 13:18:33 -0400840 struct hidpp_report *report = (struct hidpp_report *)data;
841 struct hidpp_touchpad_raw_xy raw;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400842
Benjamin Tissoires586bdc42014-09-30 13:18:33 -0400843 if (!wd || !wd->input)
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400844 return 1;
845
Benjamin Tissoires586bdc42014-09-30 13:18:33 -0400846 switch (data[0]) {
847 case 0x02:
Peter Wu0b3f6562014-12-16 16:55:22 +0100848 if (size < 2) {
849 hid_err(hdev, "Received HID report of bad size (%d)",
850 size);
851 return 1;
852 }
Benjamin Tissoires57ac86c2014-09-30 13:18:34 -0400853 if (hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS) {
854 input_event(wd->input, EV_KEY, BTN_LEFT,
855 !!(data[1] & 0x01));
856 input_event(wd->input, EV_KEY, BTN_RIGHT,
857 !!(data[1] & 0x02));
858 input_sync(wd->input);
Peter Wu8abd8202014-12-16 01:50:16 +0100859 return 0;
Benjamin Tissoires57ac86c2014-09-30 13:18:34 -0400860 } else {
861 if (size < 21)
862 return 1;
863 return wtp_mouse_raw_xy_event(hidpp, &data[7]);
864 }
Benjamin Tissoires586bdc42014-09-30 13:18:33 -0400865 case REPORT_ID_HIDPP_LONG:
Peter Wu0b3f6562014-12-16 16:55:22 +0100866 /* size is already checked in hidpp_raw_event. */
Benjamin Tissoires586bdc42014-09-30 13:18:33 -0400867 if ((report->fap.feature_index != wd->mt_feature_index) ||
868 (report->fap.funcindex_clientid != EVENT_TOUCHPAD_RAW_XY))
869 return 1;
870 hidpp_touchpad_raw_xy_event(hidpp, data + 4, &raw);
871
872 wtp_send_raw_xy_event(hidpp, &raw);
873 return 0;
874 }
875
876 return 0;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400877}
878
879static int wtp_get_config(struct hidpp_device *hidpp)
880{
881 struct wtp_data *wd = hidpp->private_data;
882 struct hidpp_touchpad_raw_info raw_info = {0};
883 u8 feature_type;
884 int ret;
885
886 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_TOUCHPAD_RAW_XY,
887 &wd->mt_feature_index, &feature_type);
888 if (ret)
889 /* means that the device is not powered up */
890 return ret;
891
892 ret = hidpp_touchpad_get_raw_info(hidpp, wd->mt_feature_index,
893 &raw_info);
894 if (ret)
895 return ret;
896
897 wd->x_size = raw_info.x_size;
898 wd->y_size = raw_info.y_size;
899 wd->maxcontacts = raw_info.maxcontacts;
900 wd->flip_y = raw_info.origin == TOUCHPAD_RAW_XY_ORIGIN_LOWER_LEFT;
901 wd->resolution = raw_info.res;
Benjamin Tissoires57ac86c2014-09-30 13:18:34 -0400902 if (!wd->resolution)
903 wd->resolution = WTP_MANUAL_RESOLUTION;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400904
905 return 0;
906}
907
908static int wtp_allocate(struct hid_device *hdev, const struct hid_device_id *id)
909{
910 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
911 struct wtp_data *wd;
912
913 wd = devm_kzalloc(&hdev->dev, sizeof(struct wtp_data),
914 GFP_KERNEL);
915 if (!wd)
916 return -ENOMEM;
917
918 hidpp->private_data = wd;
919
920 return 0;
921};
922
Benjamin Tissoiresbf159442014-12-16 17:06:01 -0500923static int wtp_connect(struct hid_device *hdev, bool connected)
Benjamin Tissoires586bdc42014-09-30 13:18:33 -0400924{
925 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
926 struct wtp_data *wd = hidpp->private_data;
927 int ret;
928
929 if (!connected)
Benjamin Tissoiresbf159442014-12-16 17:06:01 -0500930 return 0;
Benjamin Tissoires586bdc42014-09-30 13:18:33 -0400931
932 if (!wd->x_size) {
933 ret = wtp_get_config(hidpp);
934 if (ret) {
935 hid_err(hdev, "Can not get wtp config: %d\n", ret);
Benjamin Tissoiresbf159442014-12-16 17:06:01 -0500936 return ret;
Benjamin Tissoires586bdc42014-09-30 13:18:33 -0400937 }
938 }
939
Benjamin Tissoiresbf159442014-12-16 17:06:01 -0500940 return hidpp_touchpad_set_raw_report_state(hidpp, wd->mt_feature_index,
Benjamin Tissoires586bdc42014-09-30 13:18:33 -0400941 true, true);
942}
943
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400944/* -------------------------------------------------------------------------- */
945/* Generic HID++ devices */
946/* -------------------------------------------------------------------------- */
947
948static int hidpp_input_mapping(struct hid_device *hdev, struct hid_input *hi,
949 struct hid_field *field, struct hid_usage *usage,
950 unsigned long **bit, int *max)
951{
952 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
953
954 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
955 return wtp_input_mapping(hdev, hi, field, usage, bit, max);
956
957 return 0;
958}
959
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -0400960static void hidpp_populate_input(struct hidpp_device *hidpp,
961 struct input_dev *input, bool origin_is_hid_core)
962{
963 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
964 wtp_populate_input(hidpp, input, origin_is_hid_core);
965}
966
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400967static void hidpp_input_configured(struct hid_device *hdev,
968 struct hid_input *hidinput)
969{
970 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -0400971 struct input_dev *input = hidinput->input;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400972
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -0400973 hidpp_populate_input(hidpp, input, true);
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400974}
975
976static int hidpp_raw_hidpp_event(struct hidpp_device *hidpp, u8 *data,
977 int size)
978{
979 struct hidpp_report *question = hidpp->send_receive_buf;
980 struct hidpp_report *answer = hidpp->send_receive_buf;
981 struct hidpp_report *report = (struct hidpp_report *)data;
982
983 /*
984 * If the mutex is locked then we have a pending answer from a
Peter Wue529fea2014-12-17 00:23:51 +0100985 * previously sent command.
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400986 */
987 if (unlikely(mutex_is_locked(&hidpp->send_mutex))) {
988 /*
989 * Check for a correct hidpp20 answer or the corresponding
990 * error
991 */
992 if (hidpp_match_answer(question, report) ||
993 hidpp_match_error(question, report)) {
994 *answer = *report;
995 hidpp->answer_available = true;
996 wake_up(&hidpp->wait);
997 /*
998 * This was an answer to a command that this driver sent
999 * We return 1 to hid-core to avoid forwarding the
1000 * command upstream as it has been treated by the driver
1001 */
1002
1003 return 1;
1004 }
1005 }
1006
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04001007 if (unlikely(hidpp_report_is_connect_event(report))) {
1008 atomic_set(&hidpp->connected,
1009 !(report->rap.params[0] & (1 << 6)));
1010 if ((hidpp->quirks & HIDPP_QUIRK_DELAYED_INIT) &&
1011 (schedule_work(&hidpp->work) == 0))
1012 dbg_hid("%s: connect event already queued\n", __func__);
1013 return 1;
1014 }
1015
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001016 return 0;
1017}
1018
1019static int hidpp_raw_event(struct hid_device *hdev, struct hid_report *report,
1020 u8 *data, int size)
1021{
1022 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
Peter Wue529fea2014-12-17 00:23:51 +01001023 int ret = 0;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001024
Peter Wue529fea2014-12-17 00:23:51 +01001025 /* Generic HID++ processing. */
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001026 switch (data[0]) {
1027 case REPORT_ID_HIDPP_LONG:
1028 if (size != HIDPP_REPORT_LONG_LENGTH) {
1029 hid_err(hdev, "received hid++ report of bad size (%d)",
1030 size);
1031 return 1;
1032 }
Peter Wue529fea2014-12-17 00:23:51 +01001033 ret = hidpp_raw_hidpp_event(hidpp, data, size);
1034 break;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001035 case REPORT_ID_HIDPP_SHORT:
1036 if (size != HIDPP_REPORT_SHORT_LENGTH) {
1037 hid_err(hdev, "received hid++ report of bad size (%d)",
1038 size);
1039 return 1;
1040 }
Peter Wue529fea2014-12-17 00:23:51 +01001041 ret = hidpp_raw_hidpp_event(hidpp, data, size);
1042 break;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001043 }
1044
Peter Wue529fea2014-12-17 00:23:51 +01001045 /* If no report is available for further processing, skip calling
1046 * raw_event of subclasses. */
1047 if (ret != 0)
1048 return ret;
1049
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001050 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
1051 return wtp_raw_event(hdev, data, size);
1052
1053 return 0;
1054}
1055
Benjamin Tissoires33797822014-09-30 13:18:30 -04001056static void hidpp_overwrite_name(struct hid_device *hdev, bool use_unifying)
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001057{
1058 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
1059 char *name;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001060
Benjamin Tissoires33797822014-09-30 13:18:30 -04001061 if (use_unifying)
1062 /*
1063 * the device is connected through an Unifying receiver, and
1064 * might not be already connected.
1065 * Ask the receiver for its name.
1066 */
1067 name = hidpp_get_unifying_name(hidpp);
1068 else
Peter Wu02cc0972014-12-11 13:51:17 +01001069 name = hidpp_get_device_name(hidpp);
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001070
1071 if (!name)
1072 hid_err(hdev, "unable to retrieve the name of the device");
1073 else
1074 snprintf(hdev->name, sizeof(hdev->name), "%s", name);
1075
1076 kfree(name);
1077}
1078
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04001079static int hidpp_input_open(struct input_dev *dev)
1080{
1081 struct hid_device *hid = input_get_drvdata(dev);
1082
1083 return hid_hw_open(hid);
1084}
1085
1086static void hidpp_input_close(struct input_dev *dev)
1087{
1088 struct hid_device *hid = input_get_drvdata(dev);
1089
1090 hid_hw_close(hid);
1091}
1092
1093static struct input_dev *hidpp_allocate_input(struct hid_device *hdev)
1094{
1095 struct input_dev *input_dev = devm_input_allocate_device(&hdev->dev);
Benjamin Tissoires005b3f52015-01-08 14:37:12 -05001096 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04001097
1098 if (!input_dev)
1099 return NULL;
1100
1101 input_set_drvdata(input_dev, hdev);
1102 input_dev->open = hidpp_input_open;
1103 input_dev->close = hidpp_input_close;
1104
Benjamin Tissoires005b3f52015-01-08 14:37:12 -05001105 input_dev->name = hidpp->name;
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04001106 input_dev->phys = hdev->phys;
1107 input_dev->uniq = hdev->uniq;
1108 input_dev->id.bustype = hdev->bus;
1109 input_dev->id.vendor = hdev->vendor;
1110 input_dev->id.product = hdev->product;
1111 input_dev->id.version = hdev->version;
1112 input_dev->dev.parent = &hdev->dev;
1113
1114 return input_dev;
1115}
1116
1117static void hidpp_connect_event(struct hidpp_device *hidpp)
1118{
1119 struct hid_device *hdev = hidpp->hid_dev;
1120 int ret = 0;
1121 bool connected = atomic_read(&hidpp->connected);
1122 struct input_dev *input;
1123 char *name, *devm_name;
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04001124
Benjamin Tissoiresbf159442014-12-16 17:06:01 -05001125 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP) {
1126 ret = wtp_connect(hdev, connected);
1127 if (ret)
1128 return;
1129 }
Benjamin Tissoires586bdc42014-09-30 13:18:33 -04001130
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04001131 if (!connected || hidpp->delayed_input)
1132 return;
1133
1134 if (!hidpp->protocol_major) {
1135 ret = !hidpp_is_connected(hidpp);
1136 if (ret) {
1137 hid_err(hdev, "Can not get the protocol version.\n");
1138 return;
1139 }
1140 }
1141
1142 /* the device is already connected, we can ask for its name and
1143 * protocol */
1144 hid_info(hdev, "HID++ %u.%u device connected.\n",
1145 hidpp->protocol_major, hidpp->protocol_minor);
1146
Benjamin Tissoires005b3f52015-01-08 14:37:12 -05001147 if (!hidpp->name || hidpp->name == hdev->name) {
1148 name = hidpp_get_device_name(hidpp);
1149 if (!name) {
1150 hid_err(hdev,
1151 "unable to retrieve the name of the device");
1152 return;
1153 }
1154
1155 devm_name = devm_kasprintf(&hdev->dev, GFP_KERNEL, "%s", name);
1156 kfree(name);
1157 if (!devm_name)
1158 return;
1159
1160 hidpp->name = devm_name;
1161 }
1162
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04001163 input = hidpp_allocate_input(hdev);
1164 if (!input) {
1165 hid_err(hdev, "cannot allocate new input device: %d\n", ret);
1166 return;
1167 }
1168
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04001169 hidpp_populate_input(hidpp, input, false);
1170
1171 ret = input_register_device(input);
1172 if (ret)
1173 input_free_device(input);
1174
1175 hidpp->delayed_input = input;
1176}
1177
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001178static int hidpp_probe(struct hid_device *hdev, const struct hid_device_id *id)
1179{
1180 struct hidpp_device *hidpp;
1181 int ret;
1182 bool connected;
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04001183 unsigned int connect_mask = HID_CONNECT_DEFAULT;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001184
1185 hidpp = devm_kzalloc(&hdev->dev, sizeof(struct hidpp_device),
1186 GFP_KERNEL);
1187 if (!hidpp)
1188 return -ENOMEM;
1189
1190 hidpp->hid_dev = hdev;
Benjamin Tissoires005b3f52015-01-08 14:37:12 -05001191 hidpp->name = hdev->name;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001192 hid_set_drvdata(hdev, hidpp);
1193
1194 hidpp->quirks = id->driver_data;
1195
Benjamin Tissoires9188dba2015-03-26 12:41:57 -04001196 if (disable_raw_mode) {
1197 hidpp->quirks &= ~HIDPP_QUIRK_CLASS_WTP;
1198 hidpp->quirks &= ~HIDPP_QUIRK_DELAYED_INIT;
1199 }
1200
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001201 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP) {
1202 ret = wtp_allocate(hdev, id);
1203 if (ret)
Peter Wuf486d9d2014-12-11 13:51:20 +01001204 goto wtp_allocate_fail;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001205 }
1206
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04001207 INIT_WORK(&hidpp->work, delayed_work_cb);
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001208 mutex_init(&hidpp->send_mutex);
1209 init_waitqueue_head(&hidpp->wait);
1210
1211 ret = hid_parse(hdev);
1212 if (ret) {
1213 hid_err(hdev, "%s:parse failed\n", __func__);
1214 goto hid_parse_fail;
1215 }
1216
1217 /* Allow incoming packets */
1218 hid_device_io_start(hdev);
1219
1220 connected = hidpp_is_connected(hidpp);
Benjamin Tissoiresab94e562014-09-30 13:18:28 -04001221 if (id->group != HID_GROUP_LOGITECH_DJ_DEVICE) {
1222 if (!connected) {
Julia Lawallb832da52015-04-05 14:06:29 +02001223 ret = -ENODEV;
Benjamin Tissoiresab94e562014-09-30 13:18:28 -04001224 hid_err(hdev, "Device not connected");
Peter Wuf486d9d2014-12-11 13:51:20 +01001225 hid_device_io_stop(hdev);
Benjamin Tissoiresab94e562014-09-30 13:18:28 -04001226 goto hid_parse_fail;
1227 }
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001228
Benjamin Tissoiresab94e562014-09-30 13:18:28 -04001229 hid_info(hdev, "HID++ %u.%u device connected.\n",
1230 hidpp->protocol_major, hidpp->protocol_minor);
Benjamin Tissoiresab94e562014-09-30 13:18:28 -04001231 }
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001232
Benjamin Tissoires33797822014-09-30 13:18:30 -04001233 hidpp_overwrite_name(hdev, id->group == HID_GROUP_LOGITECH_DJ_DEVICE);
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04001234 atomic_set(&hidpp->connected, connected);
Benjamin Tissoires33797822014-09-30 13:18:30 -04001235
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04001236 if (connected && (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)) {
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001237 ret = wtp_get_config(hidpp);
1238 if (ret)
1239 goto hid_parse_fail;
1240 }
1241
1242 /* Block incoming packets */
1243 hid_device_io_stop(hdev);
1244
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04001245 if (hidpp->quirks & HIDPP_QUIRK_DELAYED_INIT)
1246 connect_mask &= ~HID_CONNECT_HIDINPUT;
1247
Benjamin Tissoires3a61e972014-09-30 13:18:35 -04001248 /* Re-enable hidinput for multi-input devices */
1249 if (hidpp->quirks & HIDPP_QUIRK_MULTI_INPUT)
1250 connect_mask |= HID_CONNECT_HIDINPUT;
1251
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04001252 ret = hid_hw_start(hdev, connect_mask);
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001253 if (ret) {
1254 hid_err(hdev, "%s:hid_hw_start returned error\n", __func__);
1255 goto hid_hw_start_fail;
1256 }
1257
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04001258 if (hidpp->quirks & HIDPP_QUIRK_DELAYED_INIT) {
1259 /* Allow incoming packets */
1260 hid_device_io_start(hdev);
1261
1262 hidpp_connect_event(hidpp);
1263 }
1264
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001265 return ret;
1266
1267hid_hw_start_fail:
1268hid_parse_fail:
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04001269 cancel_work_sync(&hidpp->work);
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001270 mutex_destroy(&hidpp->send_mutex);
Peter Wuf486d9d2014-12-11 13:51:20 +01001271wtp_allocate_fail:
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001272 hid_set_drvdata(hdev, NULL);
1273 return ret;
1274}
1275
1276static void hidpp_remove(struct hid_device *hdev)
1277{
1278 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
1279
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04001280 cancel_work_sync(&hidpp->work);
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001281 mutex_destroy(&hidpp->send_mutex);
1282 hid_hw_stop(hdev);
1283}
1284
1285static const struct hid_device_id hidpp_devices[] = {
Benjamin Tissoires57ac86c2014-09-30 13:18:34 -04001286 { /* wireless touchpad */
1287 HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE,
1288 USB_VENDOR_ID_LOGITECH, 0x4011),
1289 .driver_data = HIDPP_QUIRK_CLASS_WTP | HIDPP_QUIRK_DELAYED_INIT |
1290 HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS },
Benjamin Tissoires586bdc42014-09-30 13:18:33 -04001291 { /* wireless touchpad T650 */
1292 HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE,
1293 USB_VENDOR_ID_LOGITECH, 0x4101),
1294 .driver_data = HIDPP_QUIRK_CLASS_WTP | HIDPP_QUIRK_DELAYED_INIT },
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001295 { /* wireless touchpad T651 */
1296 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH,
1297 USB_DEVICE_ID_LOGITECH_T651),
1298 .driver_data = HIDPP_QUIRK_CLASS_WTP },
Benjamin Tissoires3a61e972014-09-30 13:18:35 -04001299 { /* Keyboard TK820 */
1300 HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE,
1301 USB_VENDOR_ID_LOGITECH, 0x4102),
1302 .driver_data = HIDPP_QUIRK_DELAYED_INIT | HIDPP_QUIRK_MULTI_INPUT |
1303 HIDPP_QUIRK_CLASS_WTP },
Benjamin Tissoiresab94e562014-09-30 13:18:28 -04001304
1305 { HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE,
1306 USB_VENDOR_ID_LOGITECH, HID_ANY_ID)},
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001307 {}
1308};
1309
1310MODULE_DEVICE_TABLE(hid, hidpp_devices);
1311
1312static struct hid_driver hidpp_driver = {
1313 .name = "logitech-hidpp-device",
1314 .id_table = hidpp_devices,
1315 .probe = hidpp_probe,
1316 .remove = hidpp_remove,
1317 .raw_event = hidpp_raw_event,
1318 .input_configured = hidpp_input_configured,
1319 .input_mapping = hidpp_input_mapping,
1320};
1321
1322module_hid_driver(hidpp_driver);