blob: e77658cd037ca858270f8c238becbf93b7f22e8a [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 Tissoiresa0e625f2014-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 Tissoiresa0e625f2014-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 Tissoiresa0e625f2014-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:
Peter Wu0b3f6562014-12-16 16:55:22 +0100843 if (size < 2) {
844 hid_err(hdev, "Received HID report of bad size (%d)",
845 size);
846 return 1;
847 }
Benjamin Tissoires57ac86c2014-09-30 13:18:34 -0400848 if (hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS) {
849 input_event(wd->input, EV_KEY, BTN_LEFT,
850 !!(data[1] & 0x01));
851 input_event(wd->input, EV_KEY, BTN_RIGHT,
852 !!(data[1] & 0x02));
853 input_sync(wd->input);
Peter Wu8abd8202014-12-16 01:50:16 +0100854 return 0;
Benjamin Tissoires57ac86c2014-09-30 13:18:34 -0400855 } else {
856 if (size < 21)
857 return 1;
858 return wtp_mouse_raw_xy_event(hidpp, &data[7]);
859 }
Benjamin Tissoires586bdc42014-09-30 13:18:33 -0400860 case REPORT_ID_HIDPP_LONG:
Peter Wu0b3f6562014-12-16 16:55:22 +0100861 /* size is already checked in hidpp_raw_event. */
Benjamin Tissoires586bdc42014-09-30 13:18:33 -0400862 if ((report->fap.feature_index != wd->mt_feature_index) ||
863 (report->fap.funcindex_clientid != EVENT_TOUCHPAD_RAW_XY))
864 return 1;
865 hidpp_touchpad_raw_xy_event(hidpp, data + 4, &raw);
866
867 wtp_send_raw_xy_event(hidpp, &raw);
868 return 0;
869 }
870
871 return 0;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400872}
873
874static int wtp_get_config(struct hidpp_device *hidpp)
875{
876 struct wtp_data *wd = hidpp->private_data;
877 struct hidpp_touchpad_raw_info raw_info = {0};
878 u8 feature_type;
879 int ret;
880
881 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_TOUCHPAD_RAW_XY,
882 &wd->mt_feature_index, &feature_type);
883 if (ret)
884 /* means that the device is not powered up */
885 return ret;
886
887 ret = hidpp_touchpad_get_raw_info(hidpp, wd->mt_feature_index,
888 &raw_info);
889 if (ret)
890 return ret;
891
892 wd->x_size = raw_info.x_size;
893 wd->y_size = raw_info.y_size;
894 wd->maxcontacts = raw_info.maxcontacts;
895 wd->flip_y = raw_info.origin == TOUCHPAD_RAW_XY_ORIGIN_LOWER_LEFT;
896 wd->resolution = raw_info.res;
Benjamin Tissoires57ac86c2014-09-30 13:18:34 -0400897 if (!wd->resolution)
898 wd->resolution = WTP_MANUAL_RESOLUTION;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400899
900 return 0;
901}
902
903static int wtp_allocate(struct hid_device *hdev, const struct hid_device_id *id)
904{
905 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
906 struct wtp_data *wd;
907
908 wd = devm_kzalloc(&hdev->dev, sizeof(struct wtp_data),
909 GFP_KERNEL);
910 if (!wd)
911 return -ENOMEM;
912
913 hidpp->private_data = wd;
914
915 return 0;
916};
917
Benjamin Tissoiresbf159442014-12-16 17:06:01 -0500918static int wtp_connect(struct hid_device *hdev, bool connected)
Benjamin Tissoires586bdc42014-09-30 13:18:33 -0400919{
920 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
921 struct wtp_data *wd = hidpp->private_data;
922 int ret;
923
924 if (!connected)
Benjamin Tissoiresbf159442014-12-16 17:06:01 -0500925 return 0;
Benjamin Tissoires586bdc42014-09-30 13:18:33 -0400926
927 if (!wd->x_size) {
928 ret = wtp_get_config(hidpp);
929 if (ret) {
930 hid_err(hdev, "Can not get wtp config: %d\n", ret);
Benjamin Tissoiresbf159442014-12-16 17:06:01 -0500931 return ret;
Benjamin Tissoires586bdc42014-09-30 13:18:33 -0400932 }
933 }
934
Benjamin Tissoiresbf159442014-12-16 17:06:01 -0500935 return hidpp_touchpad_set_raw_report_state(hidpp, wd->mt_feature_index,
Benjamin Tissoires586bdc42014-09-30 13:18:33 -0400936 true, true);
937}
938
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400939/* -------------------------------------------------------------------------- */
940/* Generic HID++ devices */
941/* -------------------------------------------------------------------------- */
942
943static int hidpp_input_mapping(struct hid_device *hdev, struct hid_input *hi,
944 struct hid_field *field, struct hid_usage *usage,
945 unsigned long **bit, int *max)
946{
947 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
948
949 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
950 return wtp_input_mapping(hdev, hi, field, usage, bit, max);
951
952 return 0;
953}
954
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -0400955static void hidpp_populate_input(struct hidpp_device *hidpp,
956 struct input_dev *input, bool origin_is_hid_core)
957{
958 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
959 wtp_populate_input(hidpp, input, origin_is_hid_core);
960}
961
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400962static void hidpp_input_configured(struct hid_device *hdev,
963 struct hid_input *hidinput)
964{
965 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -0400966 struct input_dev *input = hidinput->input;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400967
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -0400968 hidpp_populate_input(hidpp, input, true);
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400969}
970
971static int hidpp_raw_hidpp_event(struct hidpp_device *hidpp, u8 *data,
972 int size)
973{
974 struct hidpp_report *question = hidpp->send_receive_buf;
975 struct hidpp_report *answer = hidpp->send_receive_buf;
976 struct hidpp_report *report = (struct hidpp_report *)data;
977
978 /*
979 * If the mutex is locked then we have a pending answer from a
Peter Wue529fea2014-12-17 00:23:51 +0100980 * previously sent command.
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400981 */
982 if (unlikely(mutex_is_locked(&hidpp->send_mutex))) {
983 /*
984 * Check for a correct hidpp20 answer or the corresponding
985 * error
986 */
987 if (hidpp_match_answer(question, report) ||
988 hidpp_match_error(question, report)) {
989 *answer = *report;
990 hidpp->answer_available = true;
991 wake_up(&hidpp->wait);
992 /*
993 * This was an answer to a command that this driver sent
994 * We return 1 to hid-core to avoid forwarding the
995 * command upstream as it has been treated by the driver
996 */
997
998 return 1;
999 }
1000 }
1001
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04001002 if (unlikely(hidpp_report_is_connect_event(report))) {
1003 atomic_set(&hidpp->connected,
1004 !(report->rap.params[0] & (1 << 6)));
1005 if ((hidpp->quirks & HIDPP_QUIRK_DELAYED_INIT) &&
1006 (schedule_work(&hidpp->work) == 0))
1007 dbg_hid("%s: connect event already queued\n", __func__);
1008 return 1;
1009 }
1010
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001011 return 0;
1012}
1013
1014static int hidpp_raw_event(struct hid_device *hdev, struct hid_report *report,
1015 u8 *data, int size)
1016{
1017 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
Peter Wue529fea2014-12-17 00:23:51 +01001018 int ret = 0;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001019
Peter Wue529fea2014-12-17 00:23:51 +01001020 /* Generic HID++ processing. */
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001021 switch (data[0]) {
1022 case REPORT_ID_HIDPP_LONG:
1023 if (size != HIDPP_REPORT_LONG_LENGTH) {
1024 hid_err(hdev, "received hid++ report of bad size (%d)",
1025 size);
1026 return 1;
1027 }
Peter Wue529fea2014-12-17 00:23:51 +01001028 ret = hidpp_raw_hidpp_event(hidpp, data, size);
1029 break;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001030 case REPORT_ID_HIDPP_SHORT:
1031 if (size != HIDPP_REPORT_SHORT_LENGTH) {
1032 hid_err(hdev, "received hid++ report of bad size (%d)",
1033 size);
1034 return 1;
1035 }
Peter Wue529fea2014-12-17 00:23:51 +01001036 ret = hidpp_raw_hidpp_event(hidpp, data, size);
1037 break;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001038 }
1039
Peter Wue529fea2014-12-17 00:23:51 +01001040 /* If no report is available for further processing, skip calling
1041 * raw_event of subclasses. */
1042 if (ret != 0)
1043 return ret;
1044
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001045 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
1046 return wtp_raw_event(hdev, data, size);
1047
1048 return 0;
1049}
1050
Benjamin Tissoires33797822014-09-30 13:18:30 -04001051static void hidpp_overwrite_name(struct hid_device *hdev, bool use_unifying)
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001052{
1053 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
1054 char *name;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001055
Benjamin Tissoires33797822014-09-30 13:18:30 -04001056 if (use_unifying)
1057 /*
1058 * the device is connected through an Unifying receiver, and
1059 * might not be already connected.
1060 * Ask the receiver for its name.
1061 */
1062 name = hidpp_get_unifying_name(hidpp);
1063 else
Peter Wu02cc0972014-12-11 13:51:17 +01001064 name = hidpp_get_device_name(hidpp);
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001065
1066 if (!name)
1067 hid_err(hdev, "unable to retrieve the name of the device");
1068 else
1069 snprintf(hdev->name, sizeof(hdev->name), "%s", name);
1070
1071 kfree(name);
1072}
1073
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04001074static int hidpp_input_open(struct input_dev *dev)
1075{
1076 struct hid_device *hid = input_get_drvdata(dev);
1077
1078 return hid_hw_open(hid);
1079}
1080
1081static void hidpp_input_close(struct input_dev *dev)
1082{
1083 struct hid_device *hid = input_get_drvdata(dev);
1084
1085 hid_hw_close(hid);
1086}
1087
1088static struct input_dev *hidpp_allocate_input(struct hid_device *hdev)
1089{
1090 struct input_dev *input_dev = devm_input_allocate_device(&hdev->dev);
Benjamin Tissoires005b3f52015-01-08 14:37:12 -05001091 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04001092
1093 if (!input_dev)
1094 return NULL;
1095
1096 input_set_drvdata(input_dev, hdev);
1097 input_dev->open = hidpp_input_open;
1098 input_dev->close = hidpp_input_close;
1099
Benjamin Tissoires005b3f52015-01-08 14:37:12 -05001100 input_dev->name = hidpp->name;
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04001101 input_dev->phys = hdev->phys;
1102 input_dev->uniq = hdev->uniq;
1103 input_dev->id.bustype = hdev->bus;
1104 input_dev->id.vendor = hdev->vendor;
1105 input_dev->id.product = hdev->product;
1106 input_dev->id.version = hdev->version;
1107 input_dev->dev.parent = &hdev->dev;
1108
1109 return input_dev;
1110}
1111
1112static void hidpp_connect_event(struct hidpp_device *hidpp)
1113{
1114 struct hid_device *hdev = hidpp->hid_dev;
1115 int ret = 0;
1116 bool connected = atomic_read(&hidpp->connected);
1117 struct input_dev *input;
1118 char *name, *devm_name;
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04001119
Benjamin Tissoiresbf159442014-12-16 17:06:01 -05001120 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP) {
1121 ret = wtp_connect(hdev, connected);
1122 if (ret)
1123 return;
1124 }
Benjamin Tissoires586bdc42014-09-30 13:18:33 -04001125
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04001126 if (!connected || hidpp->delayed_input)
1127 return;
1128
1129 if (!hidpp->protocol_major) {
1130 ret = !hidpp_is_connected(hidpp);
1131 if (ret) {
1132 hid_err(hdev, "Can not get the protocol version.\n");
1133 return;
1134 }
1135 }
1136
1137 /* the device is already connected, we can ask for its name and
1138 * protocol */
1139 hid_info(hdev, "HID++ %u.%u device connected.\n",
1140 hidpp->protocol_major, hidpp->protocol_minor);
1141
Benjamin Tissoires005b3f52015-01-08 14:37:12 -05001142 if (!hidpp->name || hidpp->name == hdev->name) {
1143 name = hidpp_get_device_name(hidpp);
1144 if (!name) {
1145 hid_err(hdev,
1146 "unable to retrieve the name of the device");
1147 return;
1148 }
1149
1150 devm_name = devm_kasprintf(&hdev->dev, GFP_KERNEL, "%s", name);
1151 kfree(name);
1152 if (!devm_name)
1153 return;
1154
1155 hidpp->name = devm_name;
1156 }
1157
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04001158 input = hidpp_allocate_input(hdev);
1159 if (!input) {
1160 hid_err(hdev, "cannot allocate new input device: %d\n", ret);
1161 return;
1162 }
1163
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04001164 hidpp_populate_input(hidpp, input, false);
1165
1166 ret = input_register_device(input);
1167 if (ret)
1168 input_free_device(input);
1169
1170 hidpp->delayed_input = input;
1171}
1172
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001173static int hidpp_probe(struct hid_device *hdev, const struct hid_device_id *id)
1174{
1175 struct hidpp_device *hidpp;
1176 int ret;
1177 bool connected;
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04001178 unsigned int connect_mask = HID_CONNECT_DEFAULT;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001179
1180 hidpp = devm_kzalloc(&hdev->dev, sizeof(struct hidpp_device),
1181 GFP_KERNEL);
1182 if (!hidpp)
1183 return -ENOMEM;
1184
1185 hidpp->hid_dev = hdev;
Benjamin Tissoires005b3f52015-01-08 14:37:12 -05001186 hidpp->name = hdev->name;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001187 hid_set_drvdata(hdev, hidpp);
1188
1189 hidpp->quirks = id->driver_data;
1190
1191 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP) {
1192 ret = wtp_allocate(hdev, id);
1193 if (ret)
Peter Wuf486d9d2014-12-11 13:51:20 +01001194 goto wtp_allocate_fail;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001195 }
1196
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04001197 INIT_WORK(&hidpp->work, delayed_work_cb);
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001198 mutex_init(&hidpp->send_mutex);
1199 init_waitqueue_head(&hidpp->wait);
1200
1201 ret = hid_parse(hdev);
1202 if (ret) {
1203 hid_err(hdev, "%s:parse failed\n", __func__);
1204 goto hid_parse_fail;
1205 }
1206
1207 /* Allow incoming packets */
1208 hid_device_io_start(hdev);
1209
1210 connected = hidpp_is_connected(hidpp);
Benjamin Tissoiresab94e562014-09-30 13:18:28 -04001211 if (id->group != HID_GROUP_LOGITECH_DJ_DEVICE) {
1212 if (!connected) {
1213 hid_err(hdev, "Device not connected");
Peter Wuf486d9d2014-12-11 13:51:20 +01001214 hid_device_io_stop(hdev);
Benjamin Tissoiresab94e562014-09-30 13:18:28 -04001215 goto hid_parse_fail;
1216 }
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001217
Benjamin Tissoiresab94e562014-09-30 13:18:28 -04001218 hid_info(hdev, "HID++ %u.%u device connected.\n",
1219 hidpp->protocol_major, hidpp->protocol_minor);
Benjamin Tissoiresab94e562014-09-30 13:18:28 -04001220 }
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001221
Benjamin Tissoires33797822014-09-30 13:18:30 -04001222 hidpp_overwrite_name(hdev, id->group == HID_GROUP_LOGITECH_DJ_DEVICE);
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04001223 atomic_set(&hidpp->connected, connected);
Benjamin Tissoires33797822014-09-30 13:18:30 -04001224
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04001225 if (connected && (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)) {
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001226 ret = wtp_get_config(hidpp);
1227 if (ret)
1228 goto hid_parse_fail;
1229 }
1230
1231 /* Block incoming packets */
1232 hid_device_io_stop(hdev);
1233
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04001234 if (hidpp->quirks & HIDPP_QUIRK_DELAYED_INIT)
1235 connect_mask &= ~HID_CONNECT_HIDINPUT;
1236
Benjamin Tissoires3a61e972014-09-30 13:18:35 -04001237 /* Re-enable hidinput for multi-input devices */
1238 if (hidpp->quirks & HIDPP_QUIRK_MULTI_INPUT)
1239 connect_mask |= HID_CONNECT_HIDINPUT;
1240
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04001241 ret = hid_hw_start(hdev, connect_mask);
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001242 if (ret) {
1243 hid_err(hdev, "%s:hid_hw_start returned error\n", __func__);
1244 goto hid_hw_start_fail;
1245 }
1246
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04001247 if (hidpp->quirks & HIDPP_QUIRK_DELAYED_INIT) {
1248 /* Allow incoming packets */
1249 hid_device_io_start(hdev);
1250
1251 hidpp_connect_event(hidpp);
1252 }
1253
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001254 return ret;
1255
1256hid_hw_start_fail:
1257hid_parse_fail:
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04001258 cancel_work_sync(&hidpp->work);
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001259 mutex_destroy(&hidpp->send_mutex);
Peter Wuf486d9d2014-12-11 13:51:20 +01001260wtp_allocate_fail:
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001261 hid_set_drvdata(hdev, NULL);
1262 return ret;
1263}
1264
1265static void hidpp_remove(struct hid_device *hdev)
1266{
1267 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
1268
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);
1271 hid_hw_stop(hdev);
1272}
1273
1274static const struct hid_device_id hidpp_devices[] = {
Benjamin Tissoires57ac86c2014-09-30 13:18:34 -04001275 { /* wireless touchpad */
1276 HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE,
1277 USB_VENDOR_ID_LOGITECH, 0x4011),
1278 .driver_data = HIDPP_QUIRK_CLASS_WTP | HIDPP_QUIRK_DELAYED_INIT |
1279 HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS },
Benjamin Tissoires586bdc42014-09-30 13:18:33 -04001280 { /* wireless touchpad T650 */
1281 HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE,
1282 USB_VENDOR_ID_LOGITECH, 0x4101),
1283 .driver_data = HIDPP_QUIRK_CLASS_WTP | HIDPP_QUIRK_DELAYED_INIT },
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001284 { /* wireless touchpad T651 */
1285 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH,
1286 USB_DEVICE_ID_LOGITECH_T651),
1287 .driver_data = HIDPP_QUIRK_CLASS_WTP },
Benjamin Tissoires3a61e972014-09-30 13:18:35 -04001288 { /* Keyboard TK820 */
1289 HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE,
1290 USB_VENDOR_ID_LOGITECH, 0x4102),
1291 .driver_data = HIDPP_QUIRK_DELAYED_INIT | HIDPP_QUIRK_MULTI_INPUT |
1292 HIDPP_QUIRK_CLASS_WTP },
Benjamin Tissoiresab94e562014-09-30 13:18:28 -04001293
1294 { HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE,
1295 USB_VENDOR_ID_LOGITECH, HID_ANY_ID)},
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001296 {}
1297};
1298
1299MODULE_DEVICE_TABLE(hid, hidpp_devices);
1300
1301static struct hid_driver hidpp_driver = {
1302 .name = "logitech-hidpp-device",
1303 .id_table = hidpp_devices,
1304 .probe = hidpp_probe,
1305 .remove = hidpp_remove,
1306 .raw_event = hidpp_raw_event,
1307 .input_configured = hidpp_input_configured,
1308 .input_mapping = hidpp_input_mapping,
1309};
1310
1311module_hid_driver(hidpp_driver);