HID: logitech-hidpp: do not return the name length
[deliverable/linux.git] / drivers / hid / hid-logitech-hidpp.c
CommitLineData
2f31c525
BT
1/*
2 * HIDPP protocol for Logitech Unifying receivers
3 *
4 * Copyright (c) 2011 Logitech (c)
5 * Copyright (c) 2012-2013 Google (c)
6 * Copyright (c) 2013-2014 Red Hat Inc.
7 */
8
9/*
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the Free
12 * Software Foundation; version 2 of the License.
13 */
14
15#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
16
17#include <linux/device.h>
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
c39e3d5f
BT
39/* bits 1..20 are reserved for classes */
40#define HIDPP_QUIRK_DELAYED_INIT BIT(21)
57ac86cf 41#define HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS BIT(22)
3a61e975 42#define HIDPP_QUIRK_MULTI_INPUT BIT(23)
c39e3d5f 43
2f31c525
BT
44/*
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;
92 wait_queue_head_t wait;
93 bool answer_available;
94 u8 protocol_major;
95 u8 protocol_minor;
96
97 void *private_data;
98
c39e3d5f
BT
99 struct work_struct work;
100 struct kfifo delayed_work_fifo;
101 atomic_t connected;
102 struct input_dev *delayed_input;
103
2f31c525
BT
104 unsigned long quirks;
105};
106
107
108#define HIDPP_ERROR 0x8f
109#define HIDPP_ERROR_SUCCESS 0x00
110#define HIDPP_ERROR_INVALID_SUBID 0x01
111#define HIDPP_ERROR_INVALID_ADRESS 0x02
112#define HIDPP_ERROR_INVALID_VALUE 0x03
113#define HIDPP_ERROR_CONNECT_FAIL 0x04
114#define HIDPP_ERROR_TOO_MANY_DEVICES 0x05
115#define HIDPP_ERROR_ALREADY_EXISTS 0x06
116#define HIDPP_ERROR_BUSY 0x07
117#define HIDPP_ERROR_UNKNOWN_DEVICE 0x08
118#define HIDPP_ERROR_RESOURCE_ERROR 0x09
119#define HIDPP_ERROR_REQUEST_UNAVAILABLE 0x0a
120#define HIDPP_ERROR_INVALID_PARAM_VALUE 0x0b
121#define HIDPP_ERROR_WRONG_PIN_CODE 0x0c
122
c39e3d5f
BT
123static void hidpp_connect_event(struct hidpp_device *hidpp_dev);
124
2f31c525
BT
125static int __hidpp_send_report(struct hid_device *hdev,
126 struct hidpp_report *hidpp_report)
127{
128 int fields_count, ret;
129
130 switch (hidpp_report->report_id) {
131 case REPORT_ID_HIDPP_SHORT:
132 fields_count = HIDPP_REPORT_SHORT_LENGTH;
133 break;
134 case REPORT_ID_HIDPP_LONG:
135 fields_count = HIDPP_REPORT_LONG_LENGTH;
136 break;
137 default:
138 return -ENODEV;
139 }
140
141 /*
142 * set the device_index as the receiver, it will be overwritten by
143 * hid_hw_request if needed
144 */
145 hidpp_report->device_index = 0xff;
146
147 ret = hid_hw_raw_request(hdev, hidpp_report->report_id,
148 (u8 *)hidpp_report, fields_count, HID_OUTPUT_REPORT,
149 HID_REQ_SET_REPORT);
150
151 return ret == fields_count ? 0 : -1;
152}
153
8c9952b2
BT
154/**
155 * hidpp_send_message_sync() returns 0 in case of success, and something else
156 * in case of a failure.
157 * - If ' something else' is positive, that means that an error has been raised
158 * by the protocol itself.
159 * - If ' something else' is negative, that means that we had a classic error
160 * (-ENOMEM, -EPIPE, etc...)
161 */
2f31c525
BT
162static int hidpp_send_message_sync(struct hidpp_device *hidpp,
163 struct hidpp_report *message,
164 struct hidpp_report *response)
165{
166 int ret;
167
168 mutex_lock(&hidpp->send_mutex);
169
170 hidpp->send_receive_buf = response;
171 hidpp->answer_available = false;
172
173 /*
174 * So that we can later validate the answer when it arrives
175 * in hidpp_raw_event
176 */
177 *response = *message;
178
179 ret = __hidpp_send_report(hidpp->hid_dev, message);
180
181 if (ret) {
182 dbg_hid("__hidpp_send_report returned err: %d\n", ret);
183 memset(response, 0, sizeof(struct hidpp_report));
184 goto exit;
185 }
186
187 if (!wait_event_timeout(hidpp->wait, hidpp->answer_available,
188 5*HZ)) {
189 dbg_hid("%s:timeout waiting for response\n", __func__);
190 memset(response, 0, sizeof(struct hidpp_report));
191 ret = -ETIMEDOUT;
192 }
193
194 if (response->report_id == REPORT_ID_HIDPP_SHORT &&
195 response->fap.feature_index == HIDPP_ERROR) {
196 ret = response->fap.params[1];
197 dbg_hid("__hidpp_send_report got hidpp error %02X\n", ret);
198 goto exit;
199 }
200
201exit:
202 mutex_unlock(&hidpp->send_mutex);
203 return ret;
204
205}
206
207static int hidpp_send_fap_command_sync(struct hidpp_device *hidpp,
208 u8 feat_index, u8 funcindex_clientid, u8 *params, int param_count,
209 struct hidpp_report *response)
210{
3e7830ce 211 struct hidpp_report *message;
2f31c525
BT
212 int ret;
213
214 if (param_count > sizeof(message->fap.params))
215 return -EINVAL;
216
3e7830ce
DC
217 message = kzalloc(sizeof(struct hidpp_report), GFP_KERNEL);
218 if (!message)
219 return -ENOMEM;
2f31c525
BT
220 message->report_id = REPORT_ID_HIDPP_LONG;
221 message->fap.feature_index = feat_index;
222 message->fap.funcindex_clientid = funcindex_clientid;
223 memcpy(&message->fap.params, params, param_count);
224
225 ret = hidpp_send_message_sync(hidpp, message, response);
226 kfree(message);
227 return ret;
228}
229
33797820
BT
230static int hidpp_send_rap_command_sync(struct hidpp_device *hidpp_dev,
231 u8 report_id, u8 sub_id, u8 reg_address, u8 *params, int param_count,
232 struct hidpp_report *response)
233{
3e7830ce 234 struct hidpp_report *message;
33797820
BT
235 int ret;
236
237 if ((report_id != REPORT_ID_HIDPP_SHORT) &&
238 (report_id != REPORT_ID_HIDPP_LONG))
239 return -EINVAL;
240
241 if (param_count > sizeof(message->rap.params))
242 return -EINVAL;
243
3e7830ce
DC
244 message = kzalloc(sizeof(struct hidpp_report), GFP_KERNEL);
245 if (!message)
246 return -ENOMEM;
33797820
BT
247 message->report_id = report_id;
248 message->rap.sub_id = sub_id;
249 message->rap.reg_address = reg_address;
250 memcpy(&message->rap.params, params, param_count);
251
252 ret = hidpp_send_message_sync(hidpp_dev, message, response);
253 kfree(message);
254 return ret;
255}
256
c39e3d5f
BT
257static void delayed_work_cb(struct work_struct *work)
258{
259 struct hidpp_device *hidpp = container_of(work, struct hidpp_device,
260 work);
261 hidpp_connect_event(hidpp);
262}
263
2f31c525
BT
264static inline bool hidpp_match_answer(struct hidpp_report *question,
265 struct hidpp_report *answer)
266{
267 return (answer->fap.feature_index == question->fap.feature_index) &&
268 (answer->fap.funcindex_clientid == question->fap.funcindex_clientid);
269}
270
271static inline bool hidpp_match_error(struct hidpp_report *question,
272 struct hidpp_report *answer)
273{
274 return (answer->fap.feature_index == HIDPP_ERROR) &&
275 (answer->fap.funcindex_clientid == question->fap.feature_index) &&
276 (answer->fap.params[0] == question->fap.funcindex_clientid);
277}
278
c39e3d5f
BT
279static inline bool hidpp_report_is_connect_event(struct hidpp_report *report)
280{
281 return (report->report_id == REPORT_ID_HIDPP_SHORT) &&
282 (report->rap.sub_id == 0x41);
283}
284
33797820
BT
285/* -------------------------------------------------------------------------- */
286/* HIDP++ 1.0 commands */
287/* -------------------------------------------------------------------------- */
288
289#define HIDPP_SET_REGISTER 0x80
290#define HIDPP_GET_REGISTER 0x81
291#define HIDPP_SET_LONG_REGISTER 0x82
292#define HIDPP_GET_LONG_REGISTER 0x83
293
294#define HIDPP_REG_PAIRING_INFORMATION 0xB5
295#define DEVICE_NAME 0x40
296
297static char *hidpp_get_unifying_name(struct hidpp_device *hidpp_dev)
298{
299 struct hidpp_report response;
300 int ret;
301 /* hid-logitech-dj is in charge of setting the right device index */
302 u8 params[1] = { DEVICE_NAME };
303 char *name;
304 int len;
305
306 ret = hidpp_send_rap_command_sync(hidpp_dev,
307 REPORT_ID_HIDPP_SHORT,
308 HIDPP_GET_LONG_REGISTER,
309 HIDPP_REG_PAIRING_INFORMATION,
310 params, 1, &response);
311 if (ret)
312 return NULL;
313
314 len = response.rap.params[1];
315
316 name = kzalloc(len + 1, GFP_KERNEL);
317 if (!name)
318 return NULL;
319
320 memcpy(name, &response.rap.params[2], len);
321 return name;
322}
323
2f31c525
BT
324/* -------------------------------------------------------------------------- */
325/* 0x0000: Root */
326/* -------------------------------------------------------------------------- */
327
328#define HIDPP_PAGE_ROOT 0x0000
329#define HIDPP_PAGE_ROOT_IDX 0x00
330
331#define CMD_ROOT_GET_FEATURE 0x01
332#define CMD_ROOT_GET_PROTOCOL_VERSION 0x11
333
334static int hidpp_root_get_feature(struct hidpp_device *hidpp, u16 feature,
335 u8 *feature_index, u8 *feature_type)
336{
337 struct hidpp_report response;
338 int ret;
339 u8 params[2] = { feature >> 8, feature & 0x00FF };
340
341 ret = hidpp_send_fap_command_sync(hidpp,
342 HIDPP_PAGE_ROOT_IDX,
343 CMD_ROOT_GET_FEATURE,
344 params, 2, &response);
345 if (ret)
346 return ret;
347
348 *feature_index = response.fap.params[0];
349 *feature_type = response.fap.params[1];
350
351 return ret;
352}
353
354static int hidpp_root_get_protocol_version(struct hidpp_device *hidpp)
355{
356 struct hidpp_report response;
357 int ret;
358
359 ret = hidpp_send_fap_command_sync(hidpp,
360 HIDPP_PAGE_ROOT_IDX,
361 CMD_ROOT_GET_PROTOCOL_VERSION,
362 NULL, 0, &response);
363
552f12eb 364 if (ret == HIDPP_ERROR_INVALID_SUBID) {
2f31c525
BT
365 hidpp->protocol_major = 1;
366 hidpp->protocol_minor = 0;
367 return 0;
368 }
369
552f12eb
BT
370 /* the device might not be connected */
371 if (ret == HIDPP_ERROR_RESOURCE_ERROR)
372 return -EIO;
373
8c9952b2
BT
374 if (ret > 0) {
375 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
376 __func__, ret);
377 return -EPROTO;
378 }
2f31c525 379 if (ret)
8c9952b2 380 return ret;
2f31c525
BT
381
382 hidpp->protocol_major = response.fap.params[0];
383 hidpp->protocol_minor = response.fap.params[1];
384
385 return ret;
386}
387
388static bool hidpp_is_connected(struct hidpp_device *hidpp)
389{
390 int ret;
391
392 ret = hidpp_root_get_protocol_version(hidpp);
393 if (!ret)
394 hid_dbg(hidpp->hid_dev, "HID++ %u.%u device connected.\n",
395 hidpp->protocol_major, hidpp->protocol_minor);
396 return ret == 0;
397}
398
399/* -------------------------------------------------------------------------- */
400/* 0x0005: GetDeviceNameType */
401/* -------------------------------------------------------------------------- */
402
403#define HIDPP_PAGE_GET_DEVICE_NAME_TYPE 0x0005
404
405#define CMD_GET_DEVICE_NAME_TYPE_GET_COUNT 0x01
406#define CMD_GET_DEVICE_NAME_TYPE_GET_DEVICE_NAME 0x11
407#define CMD_GET_DEVICE_NAME_TYPE_GET_TYPE 0x21
408
409static int hidpp_devicenametype_get_count(struct hidpp_device *hidpp,
410 u8 feature_index, u8 *nameLength)
411{
412 struct hidpp_report response;
413 int ret;
414
415 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
416 CMD_GET_DEVICE_NAME_TYPE_GET_COUNT, NULL, 0, &response);
417
8c9952b2
BT
418 if (ret > 0) {
419 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
420 __func__, ret);
421 return -EPROTO;
422 }
2f31c525 423 if (ret)
8c9952b2 424 return ret;
2f31c525
BT
425
426 *nameLength = response.fap.params[0];
427
428 return ret;
429}
430
431static int hidpp_devicenametype_get_device_name(struct hidpp_device *hidpp,
432 u8 feature_index, u8 char_index, char *device_name, int len_buf)
433{
434 struct hidpp_report response;
435 int ret, i;
436 int count;
437
438 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
439 CMD_GET_DEVICE_NAME_TYPE_GET_DEVICE_NAME, &char_index, 1,
440 &response);
441
8c9952b2
BT
442 if (ret > 0) {
443 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
444 __func__, ret);
445 return -EPROTO;
446 }
2f31c525 447 if (ret)
8c9952b2 448 return ret;
2f31c525
BT
449
450 if (response.report_id == REPORT_ID_HIDPP_LONG)
451 count = HIDPP_REPORT_LONG_LENGTH - 4;
452 else
453 count = HIDPP_REPORT_SHORT_LENGTH - 4;
454
455 if (len_buf < count)
456 count = len_buf;
457
458 for (i = 0; i < count; i++)
459 device_name[i] = response.fap.params[i];
460
461 return count;
462}
463
02cc097e 464static char *hidpp_get_device_name(struct hidpp_device *hidpp)
2f31c525
BT
465{
466 u8 feature_type;
467 u8 feature_index;
468 u8 __name_length;
469 char *name;
470 unsigned index = 0;
471 int ret;
472
473 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_GET_DEVICE_NAME_TYPE,
474 &feature_index, &feature_type);
475 if (ret)
02cc097e 476 return NULL;
2f31c525
BT
477
478 ret = hidpp_devicenametype_get_count(hidpp, feature_index,
479 &__name_length);
480 if (ret)
02cc097e 481 return NULL;
2f31c525
BT
482
483 name = kzalloc(__name_length + 1, GFP_KERNEL);
484 if (!name)
02cc097e 485 return NULL;
2f31c525 486
2f31c525
BT
487 while (index < __name_length)
488 index += hidpp_devicenametype_get_device_name(hidpp,
489 feature_index, index, name + index,
490 __name_length - index);
491
492 return name;
2f31c525
BT
493}
494
495/* -------------------------------------------------------------------------- */
496/* 0x6100: TouchPadRawXY */
497/* -------------------------------------------------------------------------- */
498
499#define HIDPP_PAGE_TOUCHPAD_RAW_XY 0x6100
500
501#define CMD_TOUCHPAD_GET_RAW_INFO 0x01
586bdc4e
BT
502#define CMD_TOUCHPAD_SET_RAW_REPORT_STATE 0x21
503
504#define EVENT_TOUCHPAD_RAW_XY 0x00
2f31c525
BT
505
506#define TOUCHPAD_RAW_XY_ORIGIN_LOWER_LEFT 0x01
507#define TOUCHPAD_RAW_XY_ORIGIN_UPPER_LEFT 0x03
508
509struct hidpp_touchpad_raw_info {
510 u16 x_size;
511 u16 y_size;
512 u8 z_range;
513 u8 area_range;
514 u8 timestamp_unit;
515 u8 maxcontacts;
516 u8 origin;
517 u16 res;
518};
519
520struct hidpp_touchpad_raw_xy_finger {
521 u8 contact_type;
522 u8 contact_status;
523 u16 x;
524 u16 y;
525 u8 z;
526 u8 area;
527 u8 finger_id;
528};
529
530struct hidpp_touchpad_raw_xy {
531 u16 timestamp;
532 struct hidpp_touchpad_raw_xy_finger fingers[2];
533 u8 spurious_flag;
534 u8 end_of_frame;
535 u8 finger_count;
536 u8 button;
537};
538
539static int hidpp_touchpad_get_raw_info(struct hidpp_device *hidpp,
540 u8 feature_index, struct hidpp_touchpad_raw_info *raw_info)
541{
542 struct hidpp_report response;
543 int ret;
544 u8 *params = (u8 *)response.fap.params;
545
546 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
547 CMD_TOUCHPAD_GET_RAW_INFO, NULL, 0, &response);
548
8c9952b2
BT
549 if (ret > 0) {
550 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
551 __func__, ret);
552 return -EPROTO;
553 }
2f31c525 554 if (ret)
8c9952b2 555 return ret;
2f31c525
BT
556
557 raw_info->x_size = get_unaligned_be16(&params[0]);
558 raw_info->y_size = get_unaligned_be16(&params[2]);
559 raw_info->z_range = params[4];
560 raw_info->area_range = params[5];
561 raw_info->maxcontacts = params[7];
562 raw_info->origin = params[8];
563 /* res is given in unit per inch */
564 raw_info->res = get_unaligned_be16(&params[13]) * 2 / 51;
565
566 return ret;
567}
568
586bdc4e
BT
569static int hidpp_touchpad_set_raw_report_state(struct hidpp_device *hidpp_dev,
570 u8 feature_index, bool send_raw_reports,
571 bool sensor_enhanced_settings)
572{
573 struct hidpp_report response;
574
575 /*
576 * Params:
577 * bit 0 - enable raw
578 * bit 1 - 16bit Z, no area
579 * bit 2 - enhanced sensitivity
580 * bit 3 - width, height (4 bits each) instead of area
581 * bit 4 - send raw + gestures (degrades smoothness)
582 * remaining bits - reserved
583 */
584 u8 params = send_raw_reports | (sensor_enhanced_settings << 2);
585
586 return hidpp_send_fap_command_sync(hidpp_dev, feature_index,
587 CMD_TOUCHPAD_SET_RAW_REPORT_STATE, &params, 1, &response);
588}
589
590static void hidpp_touchpad_touch_event(u8 *data,
591 struct hidpp_touchpad_raw_xy_finger *finger)
592{
593 u8 x_m = data[0] << 2;
594 u8 y_m = data[2] << 2;
595
596 finger->x = x_m << 6 | data[1];
597 finger->y = y_m << 6 | data[3];
598
599 finger->contact_type = data[0] >> 6;
600 finger->contact_status = data[2] >> 6;
601
602 finger->z = data[4];
603 finger->area = data[5];
604 finger->finger_id = data[6] >> 4;
605}
606
607static void hidpp_touchpad_raw_xy_event(struct hidpp_device *hidpp_dev,
608 u8 *data, struct hidpp_touchpad_raw_xy *raw_xy)
609{
610 memset(raw_xy, 0, sizeof(struct hidpp_touchpad_raw_xy));
611 raw_xy->end_of_frame = data[8] & 0x01;
612 raw_xy->spurious_flag = (data[8] >> 1) & 0x01;
613 raw_xy->finger_count = data[15] & 0x0f;
614 raw_xy->button = (data[8] >> 2) & 0x01;
615
616 if (raw_xy->finger_count) {
617 hidpp_touchpad_touch_event(&data[2], &raw_xy->fingers[0]);
618 hidpp_touchpad_touch_event(&data[9], &raw_xy->fingers[1]);
619 }
620}
621
2f31c525
BT
622/* ************************************************************************** */
623/* */
624/* Device Support */
625/* */
626/* ************************************************************************** */
627
628/* -------------------------------------------------------------------------- */
629/* Touchpad HID++ devices */
630/* -------------------------------------------------------------------------- */
631
57ac86cf
BT
632#define WTP_MANUAL_RESOLUTION 39
633
2f31c525
BT
634struct wtp_data {
635 struct input_dev *input;
636 u16 x_size, y_size;
637 u8 finger_count;
638 u8 mt_feature_index;
639 u8 button_feature_index;
640 u8 maxcontacts;
641 bool flip_y;
642 unsigned int resolution;
643};
644
645static int wtp_input_mapping(struct hid_device *hdev, struct hid_input *hi,
646 struct hid_field *field, struct hid_usage *usage,
647 unsigned long **bit, int *max)
648{
3a61e975
BT
649 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
650
651 if ((hidpp->quirks & HIDPP_QUIRK_MULTI_INPUT) &&
652 (field->application == HID_GD_KEYBOARD))
653 return 0;
654
2f31c525
BT
655 return -1;
656}
657
c39e3d5f
BT
658static void wtp_populate_input(struct hidpp_device *hidpp,
659 struct input_dev *input_dev, bool origin_is_hid_core)
2f31c525 660{
2f31c525 661 struct wtp_data *wd = hidpp->private_data;
2f31c525 662
3a61e975
BT
663 if ((hidpp->quirks & HIDPP_QUIRK_MULTI_INPUT) && origin_is_hid_core)
664 /* this is the generic hid-input call */
665 return;
666
2f31c525
BT
667 __set_bit(EV_ABS, input_dev->evbit);
668 __set_bit(EV_KEY, input_dev->evbit);
669 __clear_bit(EV_REL, input_dev->evbit);
670 __clear_bit(EV_LED, input_dev->evbit);
671
672 input_set_abs_params(input_dev, ABS_MT_POSITION_X, 0, wd->x_size, 0, 0);
673 input_abs_set_res(input_dev, ABS_MT_POSITION_X, wd->resolution);
674 input_set_abs_params(input_dev, ABS_MT_POSITION_Y, 0, wd->y_size, 0, 0);
675 input_abs_set_res(input_dev, ABS_MT_POSITION_Y, wd->resolution);
676
677 /* Max pressure is not given by the devices, pick one */
678 input_set_abs_params(input_dev, ABS_MT_PRESSURE, 0, 50, 0, 0);
679
680 input_set_capability(input_dev, EV_KEY, BTN_LEFT);
681
57ac86cf
BT
682 if (hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS)
683 input_set_capability(input_dev, EV_KEY, BTN_RIGHT);
684 else
685 __set_bit(INPUT_PROP_BUTTONPAD, input_dev->propbit);
2f31c525
BT
686
687 input_mt_init_slots(input_dev, wd->maxcontacts, INPUT_MT_POINTER |
688 INPUT_MT_DROP_UNUSED);
689
690 wd->input = input_dev;
691}
692
693static void wtp_touch_event(struct wtp_data *wd,
694 struct hidpp_touchpad_raw_xy_finger *touch_report)
695{
696 int slot;
697
698 if (!touch_report->finger_id || touch_report->contact_type)
699 /* no actual data */
700 return;
701
702 slot = input_mt_get_slot_by_key(wd->input, touch_report->finger_id);
703
704 input_mt_slot(wd->input, slot);
705 input_mt_report_slot_state(wd->input, MT_TOOL_FINGER,
706 touch_report->contact_status);
707 if (touch_report->contact_status) {
708 input_event(wd->input, EV_ABS, ABS_MT_POSITION_X,
709 touch_report->x);
710 input_event(wd->input, EV_ABS, ABS_MT_POSITION_Y,
711 wd->flip_y ? wd->y_size - touch_report->y :
712 touch_report->y);
713 input_event(wd->input, EV_ABS, ABS_MT_PRESSURE,
714 touch_report->area);
715 }
716}
717
718static void wtp_send_raw_xy_event(struct hidpp_device *hidpp,
719 struct hidpp_touchpad_raw_xy *raw)
720{
721 struct wtp_data *wd = hidpp->private_data;
722 int i;
723
724 for (i = 0; i < 2; i++)
725 wtp_touch_event(wd, &(raw->fingers[i]));
726
57ac86cf
BT
727 if (raw->end_of_frame &&
728 !(hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS))
2f31c525
BT
729 input_event(wd->input, EV_KEY, BTN_LEFT, raw->button);
730
731 if (raw->end_of_frame || raw->finger_count <= 2) {
732 input_mt_sync_frame(wd->input);
733 input_sync(wd->input);
734 }
735}
736
737static int wtp_mouse_raw_xy_event(struct hidpp_device *hidpp, u8 *data)
738{
739 struct wtp_data *wd = hidpp->private_data;
740 u8 c1_area = ((data[7] & 0xf) * (data[7] & 0xf) +
741 (data[7] >> 4) * (data[7] >> 4)) / 2;
742 u8 c2_area = ((data[13] & 0xf) * (data[13] & 0xf) +
743 (data[13] >> 4) * (data[13] >> 4)) / 2;
744 struct hidpp_touchpad_raw_xy raw = {
745 .timestamp = data[1],
746 .fingers = {
747 {
748 .contact_type = 0,
749 .contact_status = !!data[7],
750 .x = get_unaligned_le16(&data[3]),
751 .y = get_unaligned_le16(&data[5]),
752 .z = c1_area,
753 .area = c1_area,
754 .finger_id = data[2],
755 }, {
756 .contact_type = 0,
757 .contact_status = !!data[13],
758 .x = get_unaligned_le16(&data[9]),
759 .y = get_unaligned_le16(&data[11]),
760 .z = c2_area,
761 .area = c2_area,
762 .finger_id = data[8],
763 }
764 },
765 .finger_count = wd->maxcontacts,
766 .spurious_flag = 0,
767 .end_of_frame = (data[0] >> 7) == 0,
768 .button = data[0] & 0x01,
769 };
770
771 wtp_send_raw_xy_event(hidpp, &raw);
772
773 return 1;
774}
775
776static int wtp_raw_event(struct hid_device *hdev, u8 *data, int size)
777{
778 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
779 struct wtp_data *wd = hidpp->private_data;
586bdc4e
BT
780 struct hidpp_report *report = (struct hidpp_report *)data;
781 struct hidpp_touchpad_raw_xy raw;
2f31c525 782
586bdc4e 783 if (!wd || !wd->input)
2f31c525
BT
784 return 1;
785
586bdc4e
BT
786 switch (data[0]) {
787 case 0x02:
57ac86cf
BT
788 if (hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS) {
789 input_event(wd->input, EV_KEY, BTN_LEFT,
790 !!(data[1] & 0x01));
791 input_event(wd->input, EV_KEY, BTN_RIGHT,
792 !!(data[1] & 0x02));
793 input_sync(wd->input);
794 } else {
795 if (size < 21)
796 return 1;
797 return wtp_mouse_raw_xy_event(hidpp, &data[7]);
798 }
586bdc4e
BT
799 case REPORT_ID_HIDPP_LONG:
800 if ((report->fap.feature_index != wd->mt_feature_index) ||
801 (report->fap.funcindex_clientid != EVENT_TOUCHPAD_RAW_XY))
802 return 1;
803 hidpp_touchpad_raw_xy_event(hidpp, data + 4, &raw);
804
805 wtp_send_raw_xy_event(hidpp, &raw);
806 return 0;
807 }
808
809 return 0;
2f31c525
BT
810}
811
812static int wtp_get_config(struct hidpp_device *hidpp)
813{
814 struct wtp_data *wd = hidpp->private_data;
815 struct hidpp_touchpad_raw_info raw_info = {0};
816 u8 feature_type;
817 int ret;
818
819 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_TOUCHPAD_RAW_XY,
820 &wd->mt_feature_index, &feature_type);
821 if (ret)
822 /* means that the device is not powered up */
823 return ret;
824
825 ret = hidpp_touchpad_get_raw_info(hidpp, wd->mt_feature_index,
826 &raw_info);
827 if (ret)
828 return ret;
829
830 wd->x_size = raw_info.x_size;
831 wd->y_size = raw_info.y_size;
832 wd->maxcontacts = raw_info.maxcontacts;
833 wd->flip_y = raw_info.origin == TOUCHPAD_RAW_XY_ORIGIN_LOWER_LEFT;
834 wd->resolution = raw_info.res;
57ac86cf
BT
835 if (!wd->resolution)
836 wd->resolution = WTP_MANUAL_RESOLUTION;
2f31c525
BT
837
838 return 0;
839}
840
841static int wtp_allocate(struct hid_device *hdev, const struct hid_device_id *id)
842{
843 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
844 struct wtp_data *wd;
845
846 wd = devm_kzalloc(&hdev->dev, sizeof(struct wtp_data),
847 GFP_KERNEL);
848 if (!wd)
849 return -ENOMEM;
850
851 hidpp->private_data = wd;
852
853 return 0;
854};
855
586bdc4e
BT
856static void wtp_connect(struct hid_device *hdev, bool connected)
857{
858 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
859 struct wtp_data *wd = hidpp->private_data;
860 int ret;
861
862 if (!connected)
863 return;
864
865 if (!wd->x_size) {
866 ret = wtp_get_config(hidpp);
867 if (ret) {
868 hid_err(hdev, "Can not get wtp config: %d\n", ret);
869 return;
870 }
871 }
872
873 hidpp_touchpad_set_raw_report_state(hidpp, wd->mt_feature_index,
874 true, true);
875}
876
2f31c525
BT
877/* -------------------------------------------------------------------------- */
878/* Generic HID++ devices */
879/* -------------------------------------------------------------------------- */
880
881static int hidpp_input_mapping(struct hid_device *hdev, struct hid_input *hi,
882 struct hid_field *field, struct hid_usage *usage,
883 unsigned long **bit, int *max)
884{
885 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
886
887 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
888 return wtp_input_mapping(hdev, hi, field, usage, bit, max);
889
890 return 0;
891}
892
c39e3d5f
BT
893static void hidpp_populate_input(struct hidpp_device *hidpp,
894 struct input_dev *input, bool origin_is_hid_core)
895{
896 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
897 wtp_populate_input(hidpp, input, origin_is_hid_core);
898}
899
2f31c525
BT
900static void hidpp_input_configured(struct hid_device *hdev,
901 struct hid_input *hidinput)
902{
903 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
c39e3d5f 904 struct input_dev *input = hidinput->input;
2f31c525 905
c39e3d5f 906 hidpp_populate_input(hidpp, input, true);
2f31c525
BT
907}
908
909static int hidpp_raw_hidpp_event(struct hidpp_device *hidpp, u8 *data,
910 int size)
911{
912 struct hidpp_report *question = hidpp->send_receive_buf;
913 struct hidpp_report *answer = hidpp->send_receive_buf;
914 struct hidpp_report *report = (struct hidpp_report *)data;
915
916 /*
917 * If the mutex is locked then we have a pending answer from a
918 * previoulsly sent command
919 */
920 if (unlikely(mutex_is_locked(&hidpp->send_mutex))) {
921 /*
922 * Check for a correct hidpp20 answer or the corresponding
923 * error
924 */
925 if (hidpp_match_answer(question, report) ||
926 hidpp_match_error(question, report)) {
927 *answer = *report;
928 hidpp->answer_available = true;
929 wake_up(&hidpp->wait);
930 /*
931 * This was an answer to a command that this driver sent
932 * We return 1 to hid-core to avoid forwarding the
933 * command upstream as it has been treated by the driver
934 */
935
936 return 1;
937 }
938 }
939
c39e3d5f
BT
940 if (unlikely(hidpp_report_is_connect_event(report))) {
941 atomic_set(&hidpp->connected,
942 !(report->rap.params[0] & (1 << 6)));
943 if ((hidpp->quirks & HIDPP_QUIRK_DELAYED_INIT) &&
944 (schedule_work(&hidpp->work) == 0))
945 dbg_hid("%s: connect event already queued\n", __func__);
946 return 1;
947 }
948
2f31c525
BT
949 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
950 return wtp_raw_event(hidpp->hid_dev, data, size);
951
952 return 0;
953}
954
955static int hidpp_raw_event(struct hid_device *hdev, struct hid_report *report,
956 u8 *data, int size)
957{
958 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
959
960 switch (data[0]) {
961 case REPORT_ID_HIDPP_LONG:
962 if (size != HIDPP_REPORT_LONG_LENGTH) {
963 hid_err(hdev, "received hid++ report of bad size (%d)",
964 size);
965 return 1;
966 }
967 return hidpp_raw_hidpp_event(hidpp, data, size);
968 case REPORT_ID_HIDPP_SHORT:
969 if (size != HIDPP_REPORT_SHORT_LENGTH) {
970 hid_err(hdev, "received hid++ report of bad size (%d)",
971 size);
972 return 1;
973 }
974 return hidpp_raw_hidpp_event(hidpp, data, size);
975 }
976
977 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
978 return wtp_raw_event(hdev, data, size);
979
980 return 0;
981}
982
33797820 983static void hidpp_overwrite_name(struct hid_device *hdev, bool use_unifying)
2f31c525
BT
984{
985 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
986 char *name;
2f31c525 987
33797820
BT
988 if (use_unifying)
989 /*
990 * the device is connected through an Unifying receiver, and
991 * might not be already connected.
992 * Ask the receiver for its name.
993 */
994 name = hidpp_get_unifying_name(hidpp);
995 else
02cc097e 996 name = hidpp_get_device_name(hidpp);
2f31c525
BT
997
998 if (!name)
999 hid_err(hdev, "unable to retrieve the name of the device");
1000 else
1001 snprintf(hdev->name, sizeof(hdev->name), "%s", name);
1002
1003 kfree(name);
1004}
1005
c39e3d5f
BT
1006static int hidpp_input_open(struct input_dev *dev)
1007{
1008 struct hid_device *hid = input_get_drvdata(dev);
1009
1010 return hid_hw_open(hid);
1011}
1012
1013static void hidpp_input_close(struct input_dev *dev)
1014{
1015 struct hid_device *hid = input_get_drvdata(dev);
1016
1017 hid_hw_close(hid);
1018}
1019
1020static struct input_dev *hidpp_allocate_input(struct hid_device *hdev)
1021{
1022 struct input_dev *input_dev = devm_input_allocate_device(&hdev->dev);
1023
1024 if (!input_dev)
1025 return NULL;
1026
1027 input_set_drvdata(input_dev, hdev);
1028 input_dev->open = hidpp_input_open;
1029 input_dev->close = hidpp_input_close;
1030
1031 input_dev->name = hdev->name;
1032 input_dev->phys = hdev->phys;
1033 input_dev->uniq = hdev->uniq;
1034 input_dev->id.bustype = hdev->bus;
1035 input_dev->id.vendor = hdev->vendor;
1036 input_dev->id.product = hdev->product;
1037 input_dev->id.version = hdev->version;
1038 input_dev->dev.parent = &hdev->dev;
1039
1040 return input_dev;
1041}
1042
1043static void hidpp_connect_event(struct hidpp_device *hidpp)
1044{
1045 struct hid_device *hdev = hidpp->hid_dev;
1046 int ret = 0;
1047 bool connected = atomic_read(&hidpp->connected);
1048 struct input_dev *input;
1049 char *name, *devm_name;
c39e3d5f 1050
586bdc4e
BT
1051 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
1052 wtp_connect(hdev, connected);
1053
c39e3d5f
BT
1054 if (!connected || hidpp->delayed_input)
1055 return;
1056
1057 if (!hidpp->protocol_major) {
1058 ret = !hidpp_is_connected(hidpp);
1059 if (ret) {
1060 hid_err(hdev, "Can not get the protocol version.\n");
1061 return;
1062 }
1063 }
1064
1065 /* the device is already connected, we can ask for its name and
1066 * protocol */
1067 hid_info(hdev, "HID++ %u.%u device connected.\n",
1068 hidpp->protocol_major, hidpp->protocol_minor);
1069
1070 input = hidpp_allocate_input(hdev);
1071 if (!input) {
1072 hid_err(hdev, "cannot allocate new input device: %d\n", ret);
1073 return;
1074 }
1075
02cc097e 1076 name = hidpp_get_device_name(hidpp);
c39e3d5f
BT
1077 if (!name) {
1078 hid_err(hdev, "unable to retrieve the name of the device");
1079 } else {
1080 devm_name = devm_kasprintf(&hdev->dev, GFP_KERNEL, "%s", name);
1081 if (devm_name)
1082 input->name = devm_name;
1083 kfree(name);
1084 }
1085
1086 hidpp_populate_input(hidpp, input, false);
1087
1088 ret = input_register_device(input);
1089 if (ret)
1090 input_free_device(input);
1091
1092 hidpp->delayed_input = input;
1093}
1094
2f31c525
BT
1095static int hidpp_probe(struct hid_device *hdev, const struct hid_device_id *id)
1096{
1097 struct hidpp_device *hidpp;
1098 int ret;
1099 bool connected;
c39e3d5f 1100 unsigned int connect_mask = HID_CONNECT_DEFAULT;
2f31c525
BT
1101
1102 hidpp = devm_kzalloc(&hdev->dev, sizeof(struct hidpp_device),
1103 GFP_KERNEL);
1104 if (!hidpp)
1105 return -ENOMEM;
1106
1107 hidpp->hid_dev = hdev;
1108 hid_set_drvdata(hdev, hidpp);
1109
1110 hidpp->quirks = id->driver_data;
1111
1112 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP) {
1113 ret = wtp_allocate(hdev, id);
1114 if (ret)
1115 return ret;
1116 }
1117
c39e3d5f 1118 INIT_WORK(&hidpp->work, delayed_work_cb);
2f31c525
BT
1119 mutex_init(&hidpp->send_mutex);
1120 init_waitqueue_head(&hidpp->wait);
1121
1122 ret = hid_parse(hdev);
1123 if (ret) {
1124 hid_err(hdev, "%s:parse failed\n", __func__);
1125 goto hid_parse_fail;
1126 }
1127
1128 /* Allow incoming packets */
1129 hid_device_io_start(hdev);
1130
1131 connected = hidpp_is_connected(hidpp);
ab94e562
BT
1132 if (id->group != HID_GROUP_LOGITECH_DJ_DEVICE) {
1133 if (!connected) {
1134 hid_err(hdev, "Device not connected");
1135 goto hid_parse_fail;
1136 }
2f31c525 1137
ab94e562
BT
1138 hid_info(hdev, "HID++ %u.%u device connected.\n",
1139 hidpp->protocol_major, hidpp->protocol_minor);
ab94e562 1140 }
2f31c525 1141
33797820 1142 hidpp_overwrite_name(hdev, id->group == HID_GROUP_LOGITECH_DJ_DEVICE);
c39e3d5f 1143 atomic_set(&hidpp->connected, connected);
33797820 1144
c39e3d5f 1145 if (connected && (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)) {
2f31c525
BT
1146 ret = wtp_get_config(hidpp);
1147 if (ret)
1148 goto hid_parse_fail;
1149 }
1150
1151 /* Block incoming packets */
1152 hid_device_io_stop(hdev);
1153
c39e3d5f
BT
1154 if (hidpp->quirks & HIDPP_QUIRK_DELAYED_INIT)
1155 connect_mask &= ~HID_CONNECT_HIDINPUT;
1156
3a61e975
BT
1157 /* Re-enable hidinput for multi-input devices */
1158 if (hidpp->quirks & HIDPP_QUIRK_MULTI_INPUT)
1159 connect_mask |= HID_CONNECT_HIDINPUT;
1160
c39e3d5f 1161 ret = hid_hw_start(hdev, connect_mask);
2f31c525
BT
1162 if (ret) {
1163 hid_err(hdev, "%s:hid_hw_start returned error\n", __func__);
1164 goto hid_hw_start_fail;
1165 }
1166
c39e3d5f
BT
1167 if (hidpp->quirks & HIDPP_QUIRK_DELAYED_INIT) {
1168 /* Allow incoming packets */
1169 hid_device_io_start(hdev);
1170
1171 hidpp_connect_event(hidpp);
1172 }
1173
2f31c525
BT
1174 return ret;
1175
1176hid_hw_start_fail:
1177hid_parse_fail:
c39e3d5f 1178 cancel_work_sync(&hidpp->work);
2f31c525
BT
1179 mutex_destroy(&hidpp->send_mutex);
1180 hid_set_drvdata(hdev, NULL);
1181 return ret;
1182}
1183
1184static void hidpp_remove(struct hid_device *hdev)
1185{
1186 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
1187
c39e3d5f 1188 cancel_work_sync(&hidpp->work);
2f31c525
BT
1189 mutex_destroy(&hidpp->send_mutex);
1190 hid_hw_stop(hdev);
1191}
1192
1193static const struct hid_device_id hidpp_devices[] = {
57ac86cf
BT
1194 { /* wireless touchpad */
1195 HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE,
1196 USB_VENDOR_ID_LOGITECH, 0x4011),
1197 .driver_data = HIDPP_QUIRK_CLASS_WTP | HIDPP_QUIRK_DELAYED_INIT |
1198 HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS },
586bdc4e
BT
1199 { /* wireless touchpad T650 */
1200 HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE,
1201 USB_VENDOR_ID_LOGITECH, 0x4101),
1202 .driver_data = HIDPP_QUIRK_CLASS_WTP | HIDPP_QUIRK_DELAYED_INIT },
2f31c525
BT
1203 { /* wireless touchpad T651 */
1204 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH,
1205 USB_DEVICE_ID_LOGITECH_T651),
1206 .driver_data = HIDPP_QUIRK_CLASS_WTP },
3a61e975
BT
1207 { /* Keyboard TK820 */
1208 HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE,
1209 USB_VENDOR_ID_LOGITECH, 0x4102),
1210 .driver_data = HIDPP_QUIRK_DELAYED_INIT | HIDPP_QUIRK_MULTI_INPUT |
1211 HIDPP_QUIRK_CLASS_WTP },
ab94e562
BT
1212
1213 { HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE,
1214 USB_VENDOR_ID_LOGITECH, HID_ANY_ID)},
2f31c525
BT
1215 {}
1216};
1217
1218MODULE_DEVICE_TABLE(hid, hidpp_devices);
1219
1220static struct hid_driver hidpp_driver = {
1221 .name = "logitech-hidpp-device",
1222 .id_table = hidpp_devices,
1223 .probe = hidpp_probe,
1224 .remove = hidpp_remove,
1225 .raw_event = hidpp_raw_event,
1226 .input_configured = hidpp_input_configured,
1227 .input_mapping = hidpp_input_mapping,
1228};
1229
1230module_hid_driver(hidpp_driver);
This page took 0.0756 seconds and 5 git commands to generate.