blob: acb632db3bb55a3e26893ad5fe3cebe5c1744e06 [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
31#define REPORT_ID_HIDPP_SHORT 0x10
32#define REPORT_ID_HIDPP_LONG 0x11
33
34#define HIDPP_REPORT_SHORT_LENGTH 7
35#define HIDPP_REPORT_LONG_LENGTH 20
36
37#define HIDPP_QUIRK_CLASS_WTP BIT(0)
38
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -040039/* bits 1..20 are reserved for classes */
40#define HIDPP_QUIRK_DELAYED_INIT BIT(21)
Benjamin Tissoires57ac86c2014-09-30 13:18:34 -040041#define HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS BIT(22)
Benjamin Tissoires3a61e972014-09-30 13:18:35 -040042#define HIDPP_QUIRK_MULTI_INPUT BIT(23)
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -040043
Benjamin Tissoires2f31c522014-09-30 13:18:27 -040044/*
45 * There are two hidpp protocols in use, the first version hidpp10 is known
46 * as register access protocol or RAP, the second version hidpp20 is known as
47 * feature access protocol or FAP
48 *
49 * Most older devices (including the Unifying usb receiver) use the RAP protocol
50 * where as most newer devices use the FAP protocol. Both protocols are
51 * compatible with the underlying transport, which could be usb, Unifiying, or
52 * bluetooth. The message lengths are defined by the hid vendor specific report
53 * descriptor for the HIDPP_SHORT report type (total message lenth 7 bytes) and
54 * the HIDPP_LONG report type (total message length 20 bytes)
55 *
56 * The RAP protocol uses both report types, whereas the FAP only uses HIDPP_LONG
57 * messages. The Unifying receiver itself responds to RAP messages (device index
58 * is 0xFF for the receiver), and all messages (short or long) with a device
59 * index between 1 and 6 are passed untouched to the corresponding paired
60 * Unifying device.
61 *
62 * The paired device can be RAP or FAP, it will receive the message untouched
63 * from the Unifiying receiver.
64 */
65
66struct fap {
67 u8 feature_index;
68 u8 funcindex_clientid;
69 u8 params[HIDPP_REPORT_LONG_LENGTH - 4U];
70};
71
72struct rap {
73 u8 sub_id;
74 u8 reg_address;
75 u8 params[HIDPP_REPORT_LONG_LENGTH - 4U];
76};
77
78struct hidpp_report {
79 u8 report_id;
80 u8 device_index;
81 union {
82 struct fap fap;
83 struct rap rap;
84 u8 rawbytes[sizeof(struct fap)];
85 };
86} __packed;
87
88struct hidpp_device {
89 struct hid_device *hid_dev;
90 struct mutex send_mutex;
91 void *send_receive_buf;
Benjamin Tissoires005b3f52015-01-08 14:37:12 -050092 char *name; /* will never be NULL and should not be freed */
Benjamin Tissoires2f31c522014-09-30 13:18:27 -040093 wait_queue_head_t wait;
94 bool answer_available;
95 u8 protocol_major;
96 u8 protocol_minor;
97
98 void *private_data;
99
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -0400100 struct work_struct work;
101 struct kfifo delayed_work_fifo;
102 atomic_t connected;
103 struct input_dev *delayed_input;
104
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400105 unsigned long quirks;
106};
107
108
Peter Wuf677bb12014-12-16 01:50:14 +0100109/* HID++ 1.0 error codes */
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400110#define HIDPP_ERROR 0x8f
111#define HIDPP_ERROR_SUCCESS 0x00
112#define HIDPP_ERROR_INVALID_SUBID 0x01
113#define HIDPP_ERROR_INVALID_ADRESS 0x02
114#define HIDPP_ERROR_INVALID_VALUE 0x03
115#define HIDPP_ERROR_CONNECT_FAIL 0x04
116#define HIDPP_ERROR_TOO_MANY_DEVICES 0x05
117#define HIDPP_ERROR_ALREADY_EXISTS 0x06
118#define HIDPP_ERROR_BUSY 0x07
119#define HIDPP_ERROR_UNKNOWN_DEVICE 0x08
120#define HIDPP_ERROR_RESOURCE_ERROR 0x09
121#define HIDPP_ERROR_REQUEST_UNAVAILABLE 0x0a
122#define HIDPP_ERROR_INVALID_PARAM_VALUE 0x0b
123#define HIDPP_ERROR_WRONG_PIN_CODE 0x0c
Peter Wuf677bb12014-12-16 01:50:14 +0100124/* HID++ 2.0 error codes */
125#define HIDPP20_ERROR 0xff
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400126
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -0400127static void hidpp_connect_event(struct hidpp_device *hidpp_dev);
128
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400129static int __hidpp_send_report(struct hid_device *hdev,
130 struct hidpp_report *hidpp_report)
131{
132 int fields_count, ret;
133
134 switch (hidpp_report->report_id) {
135 case REPORT_ID_HIDPP_SHORT:
136 fields_count = HIDPP_REPORT_SHORT_LENGTH;
137 break;
138 case REPORT_ID_HIDPP_LONG:
139 fields_count = HIDPP_REPORT_LONG_LENGTH;
140 break;
141 default:
142 return -ENODEV;
143 }
144
145 /*
146 * set the device_index as the receiver, it will be overwritten by
147 * hid_hw_request if needed
148 */
149 hidpp_report->device_index = 0xff;
150
151 ret = hid_hw_raw_request(hdev, hidpp_report->report_id,
152 (u8 *)hidpp_report, fields_count, HID_OUTPUT_REPORT,
153 HID_REQ_SET_REPORT);
154
155 return ret == fields_count ? 0 : -1;
156}
157
Benjamin Tissoires8c9952b2014-11-03 16:09:58 -0500158/**
159 * hidpp_send_message_sync() returns 0 in case of success, and something else
160 * in case of a failure.
161 * - If ' something else' is positive, that means that an error has been raised
162 * by the protocol itself.
163 * - If ' something else' is negative, that means that we had a classic error
164 * (-ENOMEM, -EPIPE, etc...)
165 */
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400166static int hidpp_send_message_sync(struct hidpp_device *hidpp,
167 struct hidpp_report *message,
168 struct hidpp_report *response)
169{
170 int ret;
171
172 mutex_lock(&hidpp->send_mutex);
173
174 hidpp->send_receive_buf = response;
175 hidpp->answer_available = false;
176
177 /*
178 * So that we can later validate the answer when it arrives
179 * in hidpp_raw_event
180 */
181 *response = *message;
182
183 ret = __hidpp_send_report(hidpp->hid_dev, message);
184
185 if (ret) {
186 dbg_hid("__hidpp_send_report returned err: %d\n", ret);
187 memset(response, 0, sizeof(struct hidpp_report));
188 goto exit;
189 }
190
191 if (!wait_event_timeout(hidpp->wait, hidpp->answer_available,
192 5*HZ)) {
193 dbg_hid("%s:timeout waiting for response\n", __func__);
194 memset(response, 0, sizeof(struct hidpp_report));
195 ret = -ETIMEDOUT;
196 }
197
198 if (response->report_id == REPORT_ID_HIDPP_SHORT &&
Peter Wuf677bb12014-12-16 01:50:14 +0100199 response->rap.sub_id == HIDPP_ERROR) {
200 ret = response->rap.params[1];
201 dbg_hid("%s:got hidpp error %02X\n", __func__, ret);
202 goto exit;
203 }
204
205 if (response->report_id == REPORT_ID_HIDPP_LONG &&
206 response->fap.feature_index == HIDPP20_ERROR) {
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400207 ret = response->fap.params[1];
Peter Wuf677bb12014-12-16 01:50:14 +0100208 dbg_hid("%s:got hidpp 2.0 error %02X\n", __func__, ret);
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400209 goto exit;
210 }
211
212exit:
213 mutex_unlock(&hidpp->send_mutex);
214 return ret;
215
216}
217
218static int hidpp_send_fap_command_sync(struct hidpp_device *hidpp,
219 u8 feat_index, u8 funcindex_clientid, u8 *params, int param_count,
220 struct hidpp_report *response)
221{
Dan Carpenter3e7830c2014-10-31 12:14:39 +0300222 struct hidpp_report *message;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400223 int ret;
224
225 if (param_count > sizeof(message->fap.params))
226 return -EINVAL;
227
Dan Carpenter3e7830c2014-10-31 12:14:39 +0300228 message = kzalloc(sizeof(struct hidpp_report), GFP_KERNEL);
229 if (!message)
230 return -ENOMEM;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400231 message->report_id = REPORT_ID_HIDPP_LONG;
232 message->fap.feature_index = feat_index;
233 message->fap.funcindex_clientid = funcindex_clientid;
234 memcpy(&message->fap.params, params, param_count);
235
236 ret = hidpp_send_message_sync(hidpp, message, response);
237 kfree(message);
238 return ret;
239}
240
Benjamin Tissoires33797822014-09-30 13:18:30 -0400241static int hidpp_send_rap_command_sync(struct hidpp_device *hidpp_dev,
242 u8 report_id, u8 sub_id, u8 reg_address, u8 *params, int param_count,
243 struct hidpp_report *response)
244{
Dan Carpenter3e7830c2014-10-31 12:14:39 +0300245 struct hidpp_report *message;
Benjamin Tissoires33797822014-09-30 13:18:30 -0400246 int ret;
247
248 if ((report_id != REPORT_ID_HIDPP_SHORT) &&
249 (report_id != REPORT_ID_HIDPP_LONG))
250 return -EINVAL;
251
252 if (param_count > sizeof(message->rap.params))
253 return -EINVAL;
254
Dan Carpenter3e7830c2014-10-31 12:14:39 +0300255 message = kzalloc(sizeof(struct hidpp_report), GFP_KERNEL);
256 if (!message)
257 return -ENOMEM;
Benjamin Tissoires33797822014-09-30 13:18:30 -0400258 message->report_id = report_id;
259 message->rap.sub_id = sub_id;
260 message->rap.reg_address = reg_address;
261 memcpy(&message->rap.params, params, param_count);
262
263 ret = hidpp_send_message_sync(hidpp_dev, message, response);
264 kfree(message);
265 return ret;
266}
267
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -0400268static void delayed_work_cb(struct work_struct *work)
269{
270 struct hidpp_device *hidpp = container_of(work, struct hidpp_device,
271 work);
272 hidpp_connect_event(hidpp);
273}
274
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400275static inline bool hidpp_match_answer(struct hidpp_report *question,
276 struct hidpp_report *answer)
277{
278 return (answer->fap.feature_index == question->fap.feature_index) &&
279 (answer->fap.funcindex_clientid == question->fap.funcindex_clientid);
280}
281
282static inline bool hidpp_match_error(struct hidpp_report *question,
283 struct hidpp_report *answer)
284{
Peter Wuf677bb12014-12-16 01:50:14 +0100285 return ((answer->rap.sub_id == HIDPP_ERROR) ||
286 (answer->fap.feature_index == HIDPP20_ERROR)) &&
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400287 (answer->fap.funcindex_clientid == question->fap.feature_index) &&
288 (answer->fap.params[0] == question->fap.funcindex_clientid);
289}
290
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -0400291static inline bool hidpp_report_is_connect_event(struct hidpp_report *report)
292{
293 return (report->report_id == REPORT_ID_HIDPP_SHORT) &&
294 (report->rap.sub_id == 0x41);
295}
296
Benjamin Tissoiresb1ac9482014-12-11 17:39:59 -0500297/**
298 * hidpp_prefix_name() prefixes the current given name with "Logitech ".
299 */
300static void hidpp_prefix_name(char **name, int name_length)
301{
302#define PREFIX_LENGTH 9 /* "Logitech " */
303
304 int new_length;
305 char *new_name;
306
307 if (name_length > PREFIX_LENGTH &&
308 strncmp(*name, "Logitech ", PREFIX_LENGTH) == 0)
309 /* The prefix has is already in the name */
310 return;
311
312 new_length = PREFIX_LENGTH + name_length;
313 new_name = kzalloc(new_length, GFP_KERNEL);
314 if (!new_name)
315 return;
316
317 snprintf(new_name, new_length, "Logitech %s", *name);
318
319 kfree(*name);
320
321 *name = new_name;
322}
323
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400324/* -------------------------------------------------------------------------- */
Benjamin Tissoires33797822014-09-30 13:18:30 -0400325/* HIDP++ 1.0 commands */
326/* -------------------------------------------------------------------------- */
327
328#define HIDPP_SET_REGISTER 0x80
329#define HIDPP_GET_REGISTER 0x81
330#define HIDPP_SET_LONG_REGISTER 0x82
331#define HIDPP_GET_LONG_REGISTER 0x83
332
333#define HIDPP_REG_PAIRING_INFORMATION 0xB5
334#define DEVICE_NAME 0x40
335
336static char *hidpp_get_unifying_name(struct hidpp_device *hidpp_dev)
337{
338 struct hidpp_report response;
339 int ret;
340 /* hid-logitech-dj is in charge of setting the right device index */
341 u8 params[1] = { DEVICE_NAME };
342 char *name;
343 int len;
344
345 ret = hidpp_send_rap_command_sync(hidpp_dev,
346 REPORT_ID_HIDPP_SHORT,
347 HIDPP_GET_LONG_REGISTER,
348 HIDPP_REG_PAIRING_INFORMATION,
349 params, 1, &response);
350 if (ret)
351 return NULL;
352
353 len = response.rap.params[1];
354
Peter Wu3a034a72014-12-11 13:51:19 +0100355 if (2 + len > sizeof(response.rap.params))
356 return NULL;
357
Benjamin Tissoires33797822014-09-30 13:18:30 -0400358 name = kzalloc(len + 1, GFP_KERNEL);
359 if (!name)
360 return NULL;
361
362 memcpy(name, &response.rap.params[2], len);
Benjamin Tissoiresb1ac9482014-12-11 17:39:59 -0500363
364 /* include the terminating '\0' */
365 hidpp_prefix_name(&name, len + 1);
366
Benjamin Tissoires33797822014-09-30 13:18:30 -0400367 return name;
368}
369
370/* -------------------------------------------------------------------------- */
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400371/* 0x0000: Root */
372/* -------------------------------------------------------------------------- */
373
374#define HIDPP_PAGE_ROOT 0x0000
375#define HIDPP_PAGE_ROOT_IDX 0x00
376
377#define CMD_ROOT_GET_FEATURE 0x01
378#define CMD_ROOT_GET_PROTOCOL_VERSION 0x11
379
380static int hidpp_root_get_feature(struct hidpp_device *hidpp, u16 feature,
381 u8 *feature_index, u8 *feature_type)
382{
383 struct hidpp_report response;
384 int ret;
385 u8 params[2] = { feature >> 8, feature & 0x00FF };
386
387 ret = hidpp_send_fap_command_sync(hidpp,
388 HIDPP_PAGE_ROOT_IDX,
389 CMD_ROOT_GET_FEATURE,
390 params, 2, &response);
391 if (ret)
392 return ret;
393
394 *feature_index = response.fap.params[0];
395 *feature_type = response.fap.params[1];
396
397 return ret;
398}
399
400static int hidpp_root_get_protocol_version(struct hidpp_device *hidpp)
401{
402 struct hidpp_report response;
403 int ret;
404
405 ret = hidpp_send_fap_command_sync(hidpp,
406 HIDPP_PAGE_ROOT_IDX,
407 CMD_ROOT_GET_PROTOCOL_VERSION,
408 NULL, 0, &response);
409
Benjamin Tissoires552f12e2014-11-03 16:09:59 -0500410 if (ret == HIDPP_ERROR_INVALID_SUBID) {
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400411 hidpp->protocol_major = 1;
412 hidpp->protocol_minor = 0;
413 return 0;
414 }
415
Benjamin Tissoires552f12e2014-11-03 16:09:59 -0500416 /* the device might not be connected */
417 if (ret == HIDPP_ERROR_RESOURCE_ERROR)
418 return -EIO;
419
Benjamin Tissoires8c9952b2014-11-03 16:09:58 -0500420 if (ret > 0) {
421 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
422 __func__, ret);
423 return -EPROTO;
424 }
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400425 if (ret)
Benjamin Tissoires8c9952b2014-11-03 16:09:58 -0500426 return ret;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400427
428 hidpp->protocol_major = response.fap.params[0];
429 hidpp->protocol_minor = response.fap.params[1];
430
431 return ret;
432}
433
434static bool hidpp_is_connected(struct hidpp_device *hidpp)
435{
436 int ret;
437
438 ret = hidpp_root_get_protocol_version(hidpp);
439 if (!ret)
440 hid_dbg(hidpp->hid_dev, "HID++ %u.%u device connected.\n",
441 hidpp->protocol_major, hidpp->protocol_minor);
442 return ret == 0;
443}
444
445/* -------------------------------------------------------------------------- */
446/* 0x0005: GetDeviceNameType */
447/* -------------------------------------------------------------------------- */
448
449#define HIDPP_PAGE_GET_DEVICE_NAME_TYPE 0x0005
450
451#define CMD_GET_DEVICE_NAME_TYPE_GET_COUNT 0x01
452#define CMD_GET_DEVICE_NAME_TYPE_GET_DEVICE_NAME 0x11
453#define CMD_GET_DEVICE_NAME_TYPE_GET_TYPE 0x21
454
455static int hidpp_devicenametype_get_count(struct hidpp_device *hidpp,
456 u8 feature_index, u8 *nameLength)
457{
458 struct hidpp_report response;
459 int ret;
460
461 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
462 CMD_GET_DEVICE_NAME_TYPE_GET_COUNT, NULL, 0, &response);
463
Benjamin Tissoires8c9952b2014-11-03 16:09:58 -0500464 if (ret > 0) {
465 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
466 __func__, ret);
467 return -EPROTO;
468 }
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400469 if (ret)
Benjamin Tissoires8c9952b2014-11-03 16:09:58 -0500470 return ret;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400471
472 *nameLength = response.fap.params[0];
473
474 return ret;
475}
476
477static int hidpp_devicenametype_get_device_name(struct hidpp_device *hidpp,
478 u8 feature_index, u8 char_index, char *device_name, int len_buf)
479{
480 struct hidpp_report response;
481 int ret, i;
482 int count;
483
484 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
485 CMD_GET_DEVICE_NAME_TYPE_GET_DEVICE_NAME, &char_index, 1,
486 &response);
487
Benjamin Tissoires8c9952b2014-11-03 16:09:58 -0500488 if (ret > 0) {
489 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
490 __func__, ret);
491 return -EPROTO;
492 }
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400493 if (ret)
Benjamin Tissoires8c9952b2014-11-03 16:09:58 -0500494 return ret;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400495
496 if (response.report_id == REPORT_ID_HIDPP_LONG)
497 count = HIDPP_REPORT_LONG_LENGTH - 4;
498 else
499 count = HIDPP_REPORT_SHORT_LENGTH - 4;
500
501 if (len_buf < count)
502 count = len_buf;
503
504 for (i = 0; i < count; i++)
505 device_name[i] = response.fap.params[i];
506
507 return count;
508}
509
Peter Wu02cc0972014-12-11 13:51:17 +0100510static char *hidpp_get_device_name(struct hidpp_device *hidpp)
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400511{
512 u8 feature_type;
513 u8 feature_index;
514 u8 __name_length;
515 char *name;
516 unsigned index = 0;
517 int ret;
518
519 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_GET_DEVICE_NAME_TYPE,
520 &feature_index, &feature_type);
521 if (ret)
Peter Wu02cc0972014-12-11 13:51:17 +0100522 return NULL;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400523
524 ret = hidpp_devicenametype_get_count(hidpp, feature_index,
525 &__name_length);
526 if (ret)
Peter Wu02cc0972014-12-11 13:51:17 +0100527 return NULL;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400528
529 name = kzalloc(__name_length + 1, GFP_KERNEL);
530 if (!name)
Peter Wu02cc0972014-12-11 13:51:17 +0100531 return NULL;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400532
Peter Wu1430ee72014-12-11 13:51:18 +0100533 while (index < __name_length) {
534 ret = hidpp_devicenametype_get_device_name(hidpp,
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400535 feature_index, index, name + index,
536 __name_length - index);
Peter Wu1430ee72014-12-11 13:51:18 +0100537 if (ret <= 0) {
538 kfree(name);
539 return NULL;
540 }
541 index += ret;
542 }
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400543
Benjamin Tissoiresb1ac9482014-12-11 17:39:59 -0500544 /* include the terminating '\0' */
545 hidpp_prefix_name(&name, __name_length + 1);
546
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400547 return name;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400548}
549
550/* -------------------------------------------------------------------------- */
551/* 0x6100: TouchPadRawXY */
552/* -------------------------------------------------------------------------- */
553
554#define HIDPP_PAGE_TOUCHPAD_RAW_XY 0x6100
555
556#define CMD_TOUCHPAD_GET_RAW_INFO 0x01
Benjamin Tissoires586bdc42014-09-30 13:18:33 -0400557#define CMD_TOUCHPAD_SET_RAW_REPORT_STATE 0x21
558
559#define EVENT_TOUCHPAD_RAW_XY 0x00
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400560
561#define TOUCHPAD_RAW_XY_ORIGIN_LOWER_LEFT 0x01
562#define TOUCHPAD_RAW_XY_ORIGIN_UPPER_LEFT 0x03
563
564struct hidpp_touchpad_raw_info {
565 u16 x_size;
566 u16 y_size;
567 u8 z_range;
568 u8 area_range;
569 u8 timestamp_unit;
570 u8 maxcontacts;
571 u8 origin;
572 u16 res;
573};
574
575struct hidpp_touchpad_raw_xy_finger {
576 u8 contact_type;
577 u8 contact_status;
578 u16 x;
579 u16 y;
580 u8 z;
581 u8 area;
582 u8 finger_id;
583};
584
585struct hidpp_touchpad_raw_xy {
586 u16 timestamp;
587 struct hidpp_touchpad_raw_xy_finger fingers[2];
588 u8 spurious_flag;
589 u8 end_of_frame;
590 u8 finger_count;
591 u8 button;
592};
593
594static int hidpp_touchpad_get_raw_info(struct hidpp_device *hidpp,
595 u8 feature_index, struct hidpp_touchpad_raw_info *raw_info)
596{
597 struct hidpp_report response;
598 int ret;
599 u8 *params = (u8 *)response.fap.params;
600
601 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
602 CMD_TOUCHPAD_GET_RAW_INFO, NULL, 0, &response);
603
Benjamin Tissoires8c9952b2014-11-03 16:09:58 -0500604 if (ret > 0) {
605 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
606 __func__, ret);
607 return -EPROTO;
608 }
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400609 if (ret)
Benjamin Tissoires8c9952b2014-11-03 16:09:58 -0500610 return ret;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400611
612 raw_info->x_size = get_unaligned_be16(&params[0]);
613 raw_info->y_size = get_unaligned_be16(&params[2]);
614 raw_info->z_range = params[4];
615 raw_info->area_range = params[5];
616 raw_info->maxcontacts = params[7];
617 raw_info->origin = params[8];
618 /* res is given in unit per inch */
619 raw_info->res = get_unaligned_be16(&params[13]) * 2 / 51;
620
621 return ret;
622}
623
Benjamin Tissoires586bdc42014-09-30 13:18:33 -0400624static int hidpp_touchpad_set_raw_report_state(struct hidpp_device *hidpp_dev,
625 u8 feature_index, bool send_raw_reports,
626 bool sensor_enhanced_settings)
627{
628 struct hidpp_report response;
629
630 /*
631 * Params:
632 * bit 0 - enable raw
633 * bit 1 - 16bit Z, no area
634 * bit 2 - enhanced sensitivity
635 * bit 3 - width, height (4 bits each) instead of area
636 * bit 4 - send raw + gestures (degrades smoothness)
637 * remaining bits - reserved
638 */
639 u8 params = send_raw_reports | (sensor_enhanced_settings << 2);
640
641 return hidpp_send_fap_command_sync(hidpp_dev, feature_index,
642 CMD_TOUCHPAD_SET_RAW_REPORT_STATE, &params, 1, &response);
643}
644
645static void hidpp_touchpad_touch_event(u8 *data,
646 struct hidpp_touchpad_raw_xy_finger *finger)
647{
648 u8 x_m = data[0] << 2;
649 u8 y_m = data[2] << 2;
650
651 finger->x = x_m << 6 | data[1];
652 finger->y = y_m << 6 | data[3];
653
654 finger->contact_type = data[0] >> 6;
655 finger->contact_status = data[2] >> 6;
656
657 finger->z = data[4];
658 finger->area = data[5];
659 finger->finger_id = data[6] >> 4;
660}
661
662static void hidpp_touchpad_raw_xy_event(struct hidpp_device *hidpp_dev,
663 u8 *data, struct hidpp_touchpad_raw_xy *raw_xy)
664{
665 memset(raw_xy, 0, sizeof(struct hidpp_touchpad_raw_xy));
666 raw_xy->end_of_frame = data[8] & 0x01;
667 raw_xy->spurious_flag = (data[8] >> 1) & 0x01;
668 raw_xy->finger_count = data[15] & 0x0f;
669 raw_xy->button = (data[8] >> 2) & 0x01;
670
671 if (raw_xy->finger_count) {
672 hidpp_touchpad_touch_event(&data[2], &raw_xy->fingers[0]);
673 hidpp_touchpad_touch_event(&data[9], &raw_xy->fingers[1]);
674 }
675}
676
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400677/* ************************************************************************** */
678/* */
679/* Device Support */
680/* */
681/* ************************************************************************** */
682
683/* -------------------------------------------------------------------------- */
684/* Touchpad HID++ devices */
685/* -------------------------------------------------------------------------- */
686
Benjamin Tissoires57ac86c2014-09-30 13:18:34 -0400687#define WTP_MANUAL_RESOLUTION 39
688
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400689struct wtp_data {
690 struct input_dev *input;
691 u16 x_size, y_size;
692 u8 finger_count;
693 u8 mt_feature_index;
694 u8 button_feature_index;
695 u8 maxcontacts;
696 bool flip_y;
697 unsigned int resolution;
698};
699
700static int wtp_input_mapping(struct hid_device *hdev, struct hid_input *hi,
701 struct hid_field *field, struct hid_usage *usage,
702 unsigned long **bit, int *max)
703{
Benjamin Tissoires3a61e972014-09-30 13:18:35 -0400704 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
705
706 if ((hidpp->quirks & HIDPP_QUIRK_MULTI_INPUT) &&
707 (field->application == HID_GD_KEYBOARD))
708 return 0;
709
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400710 return -1;
711}
712
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -0400713static void wtp_populate_input(struct hidpp_device *hidpp,
714 struct input_dev *input_dev, bool origin_is_hid_core)
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400715{
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400716 struct wtp_data *wd = hidpp->private_data;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400717
Benjamin Tissoires3a61e972014-09-30 13:18:35 -0400718 if ((hidpp->quirks & HIDPP_QUIRK_MULTI_INPUT) && origin_is_hid_core)
719 /* this is the generic hid-input call */
720 return;
721
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400722 __set_bit(EV_ABS, input_dev->evbit);
723 __set_bit(EV_KEY, input_dev->evbit);
724 __clear_bit(EV_REL, input_dev->evbit);
725 __clear_bit(EV_LED, input_dev->evbit);
726
727 input_set_abs_params(input_dev, ABS_MT_POSITION_X, 0, wd->x_size, 0, 0);
728 input_abs_set_res(input_dev, ABS_MT_POSITION_X, wd->resolution);
729 input_set_abs_params(input_dev, ABS_MT_POSITION_Y, 0, wd->y_size, 0, 0);
730 input_abs_set_res(input_dev, ABS_MT_POSITION_Y, wd->resolution);
731
732 /* Max pressure is not given by the devices, pick one */
733 input_set_abs_params(input_dev, ABS_MT_PRESSURE, 0, 50, 0, 0);
734
735 input_set_capability(input_dev, EV_KEY, BTN_LEFT);
736
Benjamin Tissoires57ac86c2014-09-30 13:18:34 -0400737 if (hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS)
738 input_set_capability(input_dev, EV_KEY, BTN_RIGHT);
739 else
740 __set_bit(INPUT_PROP_BUTTONPAD, input_dev->propbit);
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400741
742 input_mt_init_slots(input_dev, wd->maxcontacts, INPUT_MT_POINTER |
743 INPUT_MT_DROP_UNUSED);
744
745 wd->input = input_dev;
746}
747
748static void wtp_touch_event(struct wtp_data *wd,
749 struct hidpp_touchpad_raw_xy_finger *touch_report)
750{
751 int slot;
752
753 if (!touch_report->finger_id || touch_report->contact_type)
754 /* no actual data */
755 return;
756
757 slot = input_mt_get_slot_by_key(wd->input, touch_report->finger_id);
758
759 input_mt_slot(wd->input, slot);
760 input_mt_report_slot_state(wd->input, MT_TOOL_FINGER,
761 touch_report->contact_status);
762 if (touch_report->contact_status) {
763 input_event(wd->input, EV_ABS, ABS_MT_POSITION_X,
764 touch_report->x);
765 input_event(wd->input, EV_ABS, ABS_MT_POSITION_Y,
766 wd->flip_y ? wd->y_size - touch_report->y :
767 touch_report->y);
768 input_event(wd->input, EV_ABS, ABS_MT_PRESSURE,
769 touch_report->area);
770 }
771}
772
773static void wtp_send_raw_xy_event(struct hidpp_device *hidpp,
774 struct hidpp_touchpad_raw_xy *raw)
775{
776 struct wtp_data *wd = hidpp->private_data;
777 int i;
778
779 for (i = 0; i < 2; i++)
780 wtp_touch_event(wd, &(raw->fingers[i]));
781
Benjamin Tissoires57ac86c2014-09-30 13:18:34 -0400782 if (raw->end_of_frame &&
783 !(hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS))
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400784 input_event(wd->input, EV_KEY, BTN_LEFT, raw->button);
785
786 if (raw->end_of_frame || raw->finger_count <= 2) {
787 input_mt_sync_frame(wd->input);
788 input_sync(wd->input);
789 }
790}
791
792static int wtp_mouse_raw_xy_event(struct hidpp_device *hidpp, u8 *data)
793{
794 struct wtp_data *wd = hidpp->private_data;
795 u8 c1_area = ((data[7] & 0xf) * (data[7] & 0xf) +
796 (data[7] >> 4) * (data[7] >> 4)) / 2;
797 u8 c2_area = ((data[13] & 0xf) * (data[13] & 0xf) +
798 (data[13] >> 4) * (data[13] >> 4)) / 2;
799 struct hidpp_touchpad_raw_xy raw = {
800 .timestamp = data[1],
801 .fingers = {
802 {
803 .contact_type = 0,
804 .contact_status = !!data[7],
805 .x = get_unaligned_le16(&data[3]),
806 .y = get_unaligned_le16(&data[5]),
807 .z = c1_area,
808 .area = c1_area,
809 .finger_id = data[2],
810 }, {
811 .contact_type = 0,
812 .contact_status = !!data[13],
813 .x = get_unaligned_le16(&data[9]),
814 .y = get_unaligned_le16(&data[11]),
815 .z = c2_area,
816 .area = c2_area,
817 .finger_id = data[8],
818 }
819 },
820 .finger_count = wd->maxcontacts,
821 .spurious_flag = 0,
822 .end_of_frame = (data[0] >> 7) == 0,
823 .button = data[0] & 0x01,
824 };
825
826 wtp_send_raw_xy_event(hidpp, &raw);
827
828 return 1;
829}
830
831static int wtp_raw_event(struct hid_device *hdev, u8 *data, int size)
832{
833 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
834 struct wtp_data *wd = hidpp->private_data;
Benjamin Tissoires586bdc42014-09-30 13:18:33 -0400835 struct hidpp_report *report = (struct hidpp_report *)data;
836 struct hidpp_touchpad_raw_xy raw;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400837
Benjamin Tissoires586bdc42014-09-30 13:18:33 -0400838 if (!wd || !wd->input)
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400839 return 1;
840
Benjamin Tissoires586bdc42014-09-30 13:18:33 -0400841 switch (data[0]) {
842 case 0x02:
Benjamin Tissoires57ac86c2014-09-30 13:18:34 -0400843 if (hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS) {
844 input_event(wd->input, EV_KEY, BTN_LEFT,
845 !!(data[1] & 0x01));
846 input_event(wd->input, EV_KEY, BTN_RIGHT,
847 !!(data[1] & 0x02));
848 input_sync(wd->input);
849 } else {
850 if (size < 21)
851 return 1;
852 return wtp_mouse_raw_xy_event(hidpp, &data[7]);
853 }
Benjamin Tissoires586bdc42014-09-30 13:18:33 -0400854 case REPORT_ID_HIDPP_LONG:
855 if ((report->fap.feature_index != wd->mt_feature_index) ||
856 (report->fap.funcindex_clientid != EVENT_TOUCHPAD_RAW_XY))
857 return 1;
858 hidpp_touchpad_raw_xy_event(hidpp, data + 4, &raw);
859
860 wtp_send_raw_xy_event(hidpp, &raw);
861 return 0;
862 }
863
864 return 0;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400865}
866
867static int wtp_get_config(struct hidpp_device *hidpp)
868{
869 struct wtp_data *wd = hidpp->private_data;
870 struct hidpp_touchpad_raw_info raw_info = {0};
871 u8 feature_type;
872 int ret;
873
874 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_TOUCHPAD_RAW_XY,
875 &wd->mt_feature_index, &feature_type);
876 if (ret)
877 /* means that the device is not powered up */
878 return ret;
879
880 ret = hidpp_touchpad_get_raw_info(hidpp, wd->mt_feature_index,
881 &raw_info);
882 if (ret)
883 return ret;
884
885 wd->x_size = raw_info.x_size;
886 wd->y_size = raw_info.y_size;
887 wd->maxcontacts = raw_info.maxcontacts;
888 wd->flip_y = raw_info.origin == TOUCHPAD_RAW_XY_ORIGIN_LOWER_LEFT;
889 wd->resolution = raw_info.res;
Benjamin Tissoires57ac86c2014-09-30 13:18:34 -0400890 if (!wd->resolution)
891 wd->resolution = WTP_MANUAL_RESOLUTION;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400892
893 return 0;
894}
895
896static int wtp_allocate(struct hid_device *hdev, const struct hid_device_id *id)
897{
898 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
899 struct wtp_data *wd;
900
901 wd = devm_kzalloc(&hdev->dev, sizeof(struct wtp_data),
902 GFP_KERNEL);
903 if (!wd)
904 return -ENOMEM;
905
906 hidpp->private_data = wd;
907
908 return 0;
909};
910
Benjamin Tissoiresbf159442014-12-16 17:06:01 -0500911static int wtp_connect(struct hid_device *hdev, bool connected)
Benjamin Tissoires586bdc42014-09-30 13:18:33 -0400912{
913 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
914 struct wtp_data *wd = hidpp->private_data;
915 int ret;
916
917 if (!connected)
Benjamin Tissoiresbf159442014-12-16 17:06:01 -0500918 return 0;
Benjamin Tissoires586bdc42014-09-30 13:18:33 -0400919
920 if (!wd->x_size) {
921 ret = wtp_get_config(hidpp);
922 if (ret) {
923 hid_err(hdev, "Can not get wtp config: %d\n", ret);
Benjamin Tissoiresbf159442014-12-16 17:06:01 -0500924 return ret;
Benjamin Tissoires586bdc42014-09-30 13:18:33 -0400925 }
926 }
927
Benjamin Tissoiresbf159442014-12-16 17:06:01 -0500928 return hidpp_touchpad_set_raw_report_state(hidpp, wd->mt_feature_index,
Benjamin Tissoires586bdc42014-09-30 13:18:33 -0400929 true, true);
930}
931
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400932/* -------------------------------------------------------------------------- */
933/* Generic HID++ devices */
934/* -------------------------------------------------------------------------- */
935
936static int hidpp_input_mapping(struct hid_device *hdev, struct hid_input *hi,
937 struct hid_field *field, struct hid_usage *usage,
938 unsigned long **bit, int *max)
939{
940 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
941
942 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
943 return wtp_input_mapping(hdev, hi, field, usage, bit, max);
944
945 return 0;
946}
947
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -0400948static void hidpp_populate_input(struct hidpp_device *hidpp,
949 struct input_dev *input, bool origin_is_hid_core)
950{
951 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
952 wtp_populate_input(hidpp, input, origin_is_hid_core);
953}
954
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400955static void hidpp_input_configured(struct hid_device *hdev,
956 struct hid_input *hidinput)
957{
958 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -0400959 struct input_dev *input = hidinput->input;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400960
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -0400961 hidpp_populate_input(hidpp, input, true);
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400962}
963
964static int hidpp_raw_hidpp_event(struct hidpp_device *hidpp, u8 *data,
965 int size)
966{
967 struct hidpp_report *question = hidpp->send_receive_buf;
968 struct hidpp_report *answer = hidpp->send_receive_buf;
969 struct hidpp_report *report = (struct hidpp_report *)data;
970
971 /*
972 * If the mutex is locked then we have a pending answer from a
Peter Wue529fea2014-12-17 00:23:51 +0100973 * previously sent command.
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400974 */
975 if (unlikely(mutex_is_locked(&hidpp->send_mutex))) {
976 /*
977 * Check for a correct hidpp20 answer or the corresponding
978 * error
979 */
980 if (hidpp_match_answer(question, report) ||
981 hidpp_match_error(question, report)) {
982 *answer = *report;
983 hidpp->answer_available = true;
984 wake_up(&hidpp->wait);
985 /*
986 * This was an answer to a command that this driver sent
987 * We return 1 to hid-core to avoid forwarding the
988 * command upstream as it has been treated by the driver
989 */
990
991 return 1;
992 }
993 }
994
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -0400995 if (unlikely(hidpp_report_is_connect_event(report))) {
996 atomic_set(&hidpp->connected,
997 !(report->rap.params[0] & (1 << 6)));
998 if ((hidpp->quirks & HIDPP_QUIRK_DELAYED_INIT) &&
999 (schedule_work(&hidpp->work) == 0))
1000 dbg_hid("%s: connect event already queued\n", __func__);
1001 return 1;
1002 }
1003
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001004 return 0;
1005}
1006
1007static int hidpp_raw_event(struct hid_device *hdev, struct hid_report *report,
1008 u8 *data, int size)
1009{
1010 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
Peter Wue529fea2014-12-17 00:23:51 +01001011 int ret = 0;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001012
Peter Wue529fea2014-12-17 00:23:51 +01001013 /* Generic HID++ processing. */
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001014 switch (data[0]) {
1015 case REPORT_ID_HIDPP_LONG:
1016 if (size != HIDPP_REPORT_LONG_LENGTH) {
1017 hid_err(hdev, "received hid++ report of bad size (%d)",
1018 size);
1019 return 1;
1020 }
Peter Wue529fea2014-12-17 00:23:51 +01001021 ret = hidpp_raw_hidpp_event(hidpp, data, size);
1022 break;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001023 case REPORT_ID_HIDPP_SHORT:
1024 if (size != HIDPP_REPORT_SHORT_LENGTH) {
1025 hid_err(hdev, "received hid++ report of bad size (%d)",
1026 size);
1027 return 1;
1028 }
Peter Wue529fea2014-12-17 00:23:51 +01001029 ret = hidpp_raw_hidpp_event(hidpp, data, size);
1030 break;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001031 }
1032
Peter Wue529fea2014-12-17 00:23:51 +01001033 /* If no report is available for further processing, skip calling
1034 * raw_event of subclasses. */
1035 if (ret != 0)
1036 return ret;
1037
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001038 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
1039 return wtp_raw_event(hdev, data, size);
1040
1041 return 0;
1042}
1043
Benjamin Tissoires33797822014-09-30 13:18:30 -04001044static void hidpp_overwrite_name(struct hid_device *hdev, bool use_unifying)
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001045{
1046 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
1047 char *name;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001048
Benjamin Tissoires33797822014-09-30 13:18:30 -04001049 if (use_unifying)
1050 /*
1051 * the device is connected through an Unifying receiver, and
1052 * might not be already connected.
1053 * Ask the receiver for its name.
1054 */
1055 name = hidpp_get_unifying_name(hidpp);
1056 else
Peter Wu02cc0972014-12-11 13:51:17 +01001057 name = hidpp_get_device_name(hidpp);
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001058
1059 if (!name)
1060 hid_err(hdev, "unable to retrieve the name of the device");
1061 else
1062 snprintf(hdev->name, sizeof(hdev->name), "%s", name);
1063
1064 kfree(name);
1065}
1066
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04001067static int hidpp_input_open(struct input_dev *dev)
1068{
1069 struct hid_device *hid = input_get_drvdata(dev);
1070
1071 return hid_hw_open(hid);
1072}
1073
1074static void hidpp_input_close(struct input_dev *dev)
1075{
1076 struct hid_device *hid = input_get_drvdata(dev);
1077
1078 hid_hw_close(hid);
1079}
1080
1081static struct input_dev *hidpp_allocate_input(struct hid_device *hdev)
1082{
1083 struct input_dev *input_dev = devm_input_allocate_device(&hdev->dev);
Benjamin Tissoires005b3f52015-01-08 14:37:12 -05001084 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04001085
1086 if (!input_dev)
1087 return NULL;
1088
1089 input_set_drvdata(input_dev, hdev);
1090 input_dev->open = hidpp_input_open;
1091 input_dev->close = hidpp_input_close;
1092
Benjamin Tissoires005b3f52015-01-08 14:37:12 -05001093 input_dev->name = hidpp->name;
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04001094 input_dev->phys = hdev->phys;
1095 input_dev->uniq = hdev->uniq;
1096 input_dev->id.bustype = hdev->bus;
1097 input_dev->id.vendor = hdev->vendor;
1098 input_dev->id.product = hdev->product;
1099 input_dev->id.version = hdev->version;
1100 input_dev->dev.parent = &hdev->dev;
1101
1102 return input_dev;
1103}
1104
1105static void hidpp_connect_event(struct hidpp_device *hidpp)
1106{
1107 struct hid_device *hdev = hidpp->hid_dev;
1108 int ret = 0;
1109 bool connected = atomic_read(&hidpp->connected);
1110 struct input_dev *input;
1111 char *name, *devm_name;
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04001112
Benjamin Tissoiresbf159442014-12-16 17:06:01 -05001113 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP) {
1114 ret = wtp_connect(hdev, connected);
1115 if (ret)
1116 return;
1117 }
Benjamin Tissoires586bdc42014-09-30 13:18:33 -04001118
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04001119 if (!connected || hidpp->delayed_input)
1120 return;
1121
1122 if (!hidpp->protocol_major) {
1123 ret = !hidpp_is_connected(hidpp);
1124 if (ret) {
1125 hid_err(hdev, "Can not get the protocol version.\n");
1126 return;
1127 }
1128 }
1129
1130 /* the device is already connected, we can ask for its name and
1131 * protocol */
1132 hid_info(hdev, "HID++ %u.%u device connected.\n",
1133 hidpp->protocol_major, hidpp->protocol_minor);
1134
Benjamin Tissoires005b3f52015-01-08 14:37:12 -05001135 if (!hidpp->name || hidpp->name == hdev->name) {
1136 name = hidpp_get_device_name(hidpp);
1137 if (!name) {
1138 hid_err(hdev,
1139 "unable to retrieve the name of the device");
1140 return;
1141 }
1142
1143 devm_name = devm_kasprintf(&hdev->dev, GFP_KERNEL, "%s", name);
1144 kfree(name);
1145 if (!devm_name)
1146 return;
1147
1148 hidpp->name = devm_name;
1149 }
1150
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04001151 input = hidpp_allocate_input(hdev);
1152 if (!input) {
1153 hid_err(hdev, "cannot allocate new input device: %d\n", ret);
1154 return;
1155 }
1156
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04001157 hidpp_populate_input(hidpp, input, false);
1158
1159 ret = input_register_device(input);
1160 if (ret)
1161 input_free_device(input);
1162
1163 hidpp->delayed_input = input;
1164}
1165
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001166static int hidpp_probe(struct hid_device *hdev, const struct hid_device_id *id)
1167{
1168 struct hidpp_device *hidpp;
1169 int ret;
1170 bool connected;
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04001171 unsigned int connect_mask = HID_CONNECT_DEFAULT;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001172
1173 hidpp = devm_kzalloc(&hdev->dev, sizeof(struct hidpp_device),
1174 GFP_KERNEL);
1175 if (!hidpp)
1176 return -ENOMEM;
1177
1178 hidpp->hid_dev = hdev;
Benjamin Tissoires005b3f52015-01-08 14:37:12 -05001179 hidpp->name = hdev->name;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001180 hid_set_drvdata(hdev, hidpp);
1181
1182 hidpp->quirks = id->driver_data;
1183
1184 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP) {
1185 ret = wtp_allocate(hdev, id);
1186 if (ret)
Peter Wuf486d9d2014-12-11 13:51:20 +01001187 goto wtp_allocate_fail;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001188 }
1189
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04001190 INIT_WORK(&hidpp->work, delayed_work_cb);
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001191 mutex_init(&hidpp->send_mutex);
1192 init_waitqueue_head(&hidpp->wait);
1193
1194 ret = hid_parse(hdev);
1195 if (ret) {
1196 hid_err(hdev, "%s:parse failed\n", __func__);
1197 goto hid_parse_fail;
1198 }
1199
1200 /* Allow incoming packets */
1201 hid_device_io_start(hdev);
1202
1203 connected = hidpp_is_connected(hidpp);
Benjamin Tissoiresab94e562014-09-30 13:18:28 -04001204 if (id->group != HID_GROUP_LOGITECH_DJ_DEVICE) {
1205 if (!connected) {
1206 hid_err(hdev, "Device not connected");
Peter Wuf486d9d2014-12-11 13:51:20 +01001207 hid_device_io_stop(hdev);
Benjamin Tissoiresab94e562014-09-30 13:18:28 -04001208 goto hid_parse_fail;
1209 }
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001210
Benjamin Tissoiresab94e562014-09-30 13:18:28 -04001211 hid_info(hdev, "HID++ %u.%u device connected.\n",
1212 hidpp->protocol_major, hidpp->protocol_minor);
Benjamin Tissoiresab94e562014-09-30 13:18:28 -04001213 }
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001214
Benjamin Tissoires33797822014-09-30 13:18:30 -04001215 hidpp_overwrite_name(hdev, id->group == HID_GROUP_LOGITECH_DJ_DEVICE);
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04001216 atomic_set(&hidpp->connected, connected);
Benjamin Tissoires33797822014-09-30 13:18:30 -04001217
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04001218 if (connected && (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)) {
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001219 ret = wtp_get_config(hidpp);
1220 if (ret)
1221 goto hid_parse_fail;
1222 }
1223
1224 /* Block incoming packets */
1225 hid_device_io_stop(hdev);
1226
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04001227 if (hidpp->quirks & HIDPP_QUIRK_DELAYED_INIT)
1228 connect_mask &= ~HID_CONNECT_HIDINPUT;
1229
Benjamin Tissoires3a61e972014-09-30 13:18:35 -04001230 /* Re-enable hidinput for multi-input devices */
1231 if (hidpp->quirks & HIDPP_QUIRK_MULTI_INPUT)
1232 connect_mask |= HID_CONNECT_HIDINPUT;
1233
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04001234 ret = hid_hw_start(hdev, connect_mask);
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001235 if (ret) {
1236 hid_err(hdev, "%s:hid_hw_start returned error\n", __func__);
1237 goto hid_hw_start_fail;
1238 }
1239
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04001240 if (hidpp->quirks & HIDPP_QUIRK_DELAYED_INIT) {
1241 /* Allow incoming packets */
1242 hid_device_io_start(hdev);
1243
1244 hidpp_connect_event(hidpp);
1245 }
1246
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001247 return ret;
1248
1249hid_hw_start_fail:
1250hid_parse_fail:
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04001251 cancel_work_sync(&hidpp->work);
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001252 mutex_destroy(&hidpp->send_mutex);
Peter Wuf486d9d2014-12-11 13:51:20 +01001253wtp_allocate_fail:
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001254 hid_set_drvdata(hdev, NULL);
1255 return ret;
1256}
1257
1258static void hidpp_remove(struct hid_device *hdev)
1259{
1260 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
1261
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04001262 cancel_work_sync(&hidpp->work);
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001263 mutex_destroy(&hidpp->send_mutex);
1264 hid_hw_stop(hdev);
1265}
1266
1267static const struct hid_device_id hidpp_devices[] = {
Benjamin Tissoires57ac86c2014-09-30 13:18:34 -04001268 { /* wireless touchpad */
1269 HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE,
1270 USB_VENDOR_ID_LOGITECH, 0x4011),
1271 .driver_data = HIDPP_QUIRK_CLASS_WTP | HIDPP_QUIRK_DELAYED_INIT |
1272 HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS },
Benjamin Tissoires586bdc42014-09-30 13:18:33 -04001273 { /* wireless touchpad T650 */
1274 HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE,
1275 USB_VENDOR_ID_LOGITECH, 0x4101),
1276 .driver_data = HIDPP_QUIRK_CLASS_WTP | HIDPP_QUIRK_DELAYED_INIT },
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001277 { /* wireless touchpad T651 */
1278 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH,
1279 USB_DEVICE_ID_LOGITECH_T651),
1280 .driver_data = HIDPP_QUIRK_CLASS_WTP },
Benjamin Tissoires3a61e972014-09-30 13:18:35 -04001281 { /* Keyboard TK820 */
1282 HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE,
1283 USB_VENDOR_ID_LOGITECH, 0x4102),
1284 .driver_data = HIDPP_QUIRK_DELAYED_INIT | HIDPP_QUIRK_MULTI_INPUT |
1285 HIDPP_QUIRK_CLASS_WTP },
Benjamin Tissoiresab94e562014-09-30 13:18:28 -04001286
1287 { HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE,
1288 USB_VENDOR_ID_LOGITECH, HID_ANY_ID)},
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001289 {}
1290};
1291
1292MODULE_DEVICE_TABLE(hid, hidpp_devices);
1293
1294static struct hid_driver hidpp_driver = {
1295 .name = "logitech-hidpp-device",
1296 .id_table = hidpp_devices,
1297 .probe = hidpp_probe,
1298 .remove = hidpp_remove,
1299 .raw_event = hidpp_raw_event,
1300 .input_configured = hidpp_input_configured,
1301 .input_mapping = hidpp_input_mapping,
1302};
1303
1304module_hid_driver(hidpp_driver);