]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/hid/hid-logitech-hidpp.c
HID: logitech-dj: enable notifications on connect/disconnect
[mirror_ubuntu-artful-kernel.git] / drivers / hid / hid-logitech-hidpp.c
CommitLineData
2f31c525
BT
1/*
2 * HIDPP protocol for Logitech Unifying receivers
3 *
4 * Copyright (c) 2011 Logitech (c)
5 * Copyright (c) 2012-2013 Google (c)
6 * Copyright (c) 2013-2014 Red Hat Inc.
7 */
8
9/*
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the Free
12 * Software Foundation; version 2 of the License.
13 */
14
15#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
16
17#include <linux/device.h>
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
39/*
40 * There are two hidpp protocols in use, the first version hidpp10 is known
41 * as register access protocol or RAP, the second version hidpp20 is known as
42 * feature access protocol or FAP
43 *
44 * Most older devices (including the Unifying usb receiver) use the RAP protocol
45 * where as most newer devices use the FAP protocol. Both protocols are
46 * compatible with the underlying transport, which could be usb, Unifiying, or
47 * bluetooth. The message lengths are defined by the hid vendor specific report
48 * descriptor for the HIDPP_SHORT report type (total message lenth 7 bytes) and
49 * the HIDPP_LONG report type (total message length 20 bytes)
50 *
51 * The RAP protocol uses both report types, whereas the FAP only uses HIDPP_LONG
52 * messages. The Unifying receiver itself responds to RAP messages (device index
53 * is 0xFF for the receiver), and all messages (short or long) with a device
54 * index between 1 and 6 are passed untouched to the corresponding paired
55 * Unifying device.
56 *
57 * The paired device can be RAP or FAP, it will receive the message untouched
58 * from the Unifiying receiver.
59 */
60
61struct fap {
62 u8 feature_index;
63 u8 funcindex_clientid;
64 u8 params[HIDPP_REPORT_LONG_LENGTH - 4U];
65};
66
67struct rap {
68 u8 sub_id;
69 u8 reg_address;
70 u8 params[HIDPP_REPORT_LONG_LENGTH - 4U];
71};
72
73struct hidpp_report {
74 u8 report_id;
75 u8 device_index;
76 union {
77 struct fap fap;
78 struct rap rap;
79 u8 rawbytes[sizeof(struct fap)];
80 };
81} __packed;
82
83struct hidpp_device {
84 struct hid_device *hid_dev;
85 struct mutex send_mutex;
86 void *send_receive_buf;
87 wait_queue_head_t wait;
88 bool answer_available;
89 u8 protocol_major;
90 u8 protocol_minor;
91
92 void *private_data;
93
94 unsigned long quirks;
95};
96
97
98#define HIDPP_ERROR 0x8f
99#define HIDPP_ERROR_SUCCESS 0x00
100#define HIDPP_ERROR_INVALID_SUBID 0x01
101#define HIDPP_ERROR_INVALID_ADRESS 0x02
102#define HIDPP_ERROR_INVALID_VALUE 0x03
103#define HIDPP_ERROR_CONNECT_FAIL 0x04
104#define HIDPP_ERROR_TOO_MANY_DEVICES 0x05
105#define HIDPP_ERROR_ALREADY_EXISTS 0x06
106#define HIDPP_ERROR_BUSY 0x07
107#define HIDPP_ERROR_UNKNOWN_DEVICE 0x08
108#define HIDPP_ERROR_RESOURCE_ERROR 0x09
109#define HIDPP_ERROR_REQUEST_UNAVAILABLE 0x0a
110#define HIDPP_ERROR_INVALID_PARAM_VALUE 0x0b
111#define HIDPP_ERROR_WRONG_PIN_CODE 0x0c
112
113static int __hidpp_send_report(struct hid_device *hdev,
114 struct hidpp_report *hidpp_report)
115{
116 int fields_count, ret;
117
118 switch (hidpp_report->report_id) {
119 case REPORT_ID_HIDPP_SHORT:
120 fields_count = HIDPP_REPORT_SHORT_LENGTH;
121 break;
122 case REPORT_ID_HIDPP_LONG:
123 fields_count = HIDPP_REPORT_LONG_LENGTH;
124 break;
125 default:
126 return -ENODEV;
127 }
128
129 /*
130 * set the device_index as the receiver, it will be overwritten by
131 * hid_hw_request if needed
132 */
133 hidpp_report->device_index = 0xff;
134
135 ret = hid_hw_raw_request(hdev, hidpp_report->report_id,
136 (u8 *)hidpp_report, fields_count, HID_OUTPUT_REPORT,
137 HID_REQ_SET_REPORT);
138
139 return ret == fields_count ? 0 : -1;
140}
141
142static int hidpp_send_message_sync(struct hidpp_device *hidpp,
143 struct hidpp_report *message,
144 struct hidpp_report *response)
145{
146 int ret;
147
148 mutex_lock(&hidpp->send_mutex);
149
150 hidpp->send_receive_buf = response;
151 hidpp->answer_available = false;
152
153 /*
154 * So that we can later validate the answer when it arrives
155 * in hidpp_raw_event
156 */
157 *response = *message;
158
159 ret = __hidpp_send_report(hidpp->hid_dev, message);
160
161 if (ret) {
162 dbg_hid("__hidpp_send_report returned err: %d\n", ret);
163 memset(response, 0, sizeof(struct hidpp_report));
164 goto exit;
165 }
166
167 if (!wait_event_timeout(hidpp->wait, hidpp->answer_available,
168 5*HZ)) {
169 dbg_hid("%s:timeout waiting for response\n", __func__);
170 memset(response, 0, sizeof(struct hidpp_report));
171 ret = -ETIMEDOUT;
172 }
173
174 if (response->report_id == REPORT_ID_HIDPP_SHORT &&
175 response->fap.feature_index == HIDPP_ERROR) {
176 ret = response->fap.params[1];
177 dbg_hid("__hidpp_send_report got hidpp error %02X\n", ret);
178 goto exit;
179 }
180
181exit:
182 mutex_unlock(&hidpp->send_mutex);
183 return ret;
184
185}
186
187static int hidpp_send_fap_command_sync(struct hidpp_device *hidpp,
188 u8 feat_index, u8 funcindex_clientid, u8 *params, int param_count,
189 struct hidpp_report *response)
190{
191 struct hidpp_report *message = kzalloc(sizeof(struct hidpp_report),
192 GFP_KERNEL);
193 int ret;
194
195 if (param_count > sizeof(message->fap.params))
196 return -EINVAL;
197
198 message->report_id = REPORT_ID_HIDPP_LONG;
199 message->fap.feature_index = feat_index;
200 message->fap.funcindex_clientid = funcindex_clientid;
201 memcpy(&message->fap.params, params, param_count);
202
203 ret = hidpp_send_message_sync(hidpp, message, response);
204 kfree(message);
205 return ret;
206}
207
33797820
BT
208static int hidpp_send_rap_command_sync(struct hidpp_device *hidpp_dev,
209 u8 report_id, u8 sub_id, u8 reg_address, u8 *params, int param_count,
210 struct hidpp_report *response)
211{
212 struct hidpp_report *message = kzalloc(sizeof(struct hidpp_report),
213 GFP_KERNEL);
214 int ret;
215
216 if ((report_id != REPORT_ID_HIDPP_SHORT) &&
217 (report_id != REPORT_ID_HIDPP_LONG))
218 return -EINVAL;
219
220 if (param_count > sizeof(message->rap.params))
221 return -EINVAL;
222
223 message->report_id = report_id;
224 message->rap.sub_id = sub_id;
225 message->rap.reg_address = reg_address;
226 memcpy(&message->rap.params, params, param_count);
227
228 ret = hidpp_send_message_sync(hidpp_dev, message, response);
229 kfree(message);
230 return ret;
231}
232
2f31c525
BT
233static inline bool hidpp_match_answer(struct hidpp_report *question,
234 struct hidpp_report *answer)
235{
236 return (answer->fap.feature_index == question->fap.feature_index) &&
237 (answer->fap.funcindex_clientid == question->fap.funcindex_clientid);
238}
239
240static inline bool hidpp_match_error(struct hidpp_report *question,
241 struct hidpp_report *answer)
242{
243 return (answer->fap.feature_index == HIDPP_ERROR) &&
244 (answer->fap.funcindex_clientid == question->fap.feature_index) &&
245 (answer->fap.params[0] == question->fap.funcindex_clientid);
246}
247
33797820
BT
248/* -------------------------------------------------------------------------- */
249/* HIDP++ 1.0 commands */
250/* -------------------------------------------------------------------------- */
251
252#define HIDPP_SET_REGISTER 0x80
253#define HIDPP_GET_REGISTER 0x81
254#define HIDPP_SET_LONG_REGISTER 0x82
255#define HIDPP_GET_LONG_REGISTER 0x83
256
257#define HIDPP_REG_PAIRING_INFORMATION 0xB5
258#define DEVICE_NAME 0x40
259
260static char *hidpp_get_unifying_name(struct hidpp_device *hidpp_dev)
261{
262 struct hidpp_report response;
263 int ret;
264 /* hid-logitech-dj is in charge of setting the right device index */
265 u8 params[1] = { DEVICE_NAME };
266 char *name;
267 int len;
268
269 ret = hidpp_send_rap_command_sync(hidpp_dev,
270 REPORT_ID_HIDPP_SHORT,
271 HIDPP_GET_LONG_REGISTER,
272 HIDPP_REG_PAIRING_INFORMATION,
273 params, 1, &response);
274 if (ret)
275 return NULL;
276
277 len = response.rap.params[1];
278
279 name = kzalloc(len + 1, GFP_KERNEL);
280 if (!name)
281 return NULL;
282
283 memcpy(name, &response.rap.params[2], len);
284 return name;
285}
286
2f31c525
BT
287/* -------------------------------------------------------------------------- */
288/* 0x0000: Root */
289/* -------------------------------------------------------------------------- */
290
291#define HIDPP_PAGE_ROOT 0x0000
292#define HIDPP_PAGE_ROOT_IDX 0x00
293
294#define CMD_ROOT_GET_FEATURE 0x01
295#define CMD_ROOT_GET_PROTOCOL_VERSION 0x11
296
297static int hidpp_root_get_feature(struct hidpp_device *hidpp, u16 feature,
298 u8 *feature_index, u8 *feature_type)
299{
300 struct hidpp_report response;
301 int ret;
302 u8 params[2] = { feature >> 8, feature & 0x00FF };
303
304 ret = hidpp_send_fap_command_sync(hidpp,
305 HIDPP_PAGE_ROOT_IDX,
306 CMD_ROOT_GET_FEATURE,
307 params, 2, &response);
308 if (ret)
309 return ret;
310
311 *feature_index = response.fap.params[0];
312 *feature_type = response.fap.params[1];
313
314 return ret;
315}
316
317static int hidpp_root_get_protocol_version(struct hidpp_device *hidpp)
318{
319 struct hidpp_report response;
320 int ret;
321
322 ret = hidpp_send_fap_command_sync(hidpp,
323 HIDPP_PAGE_ROOT_IDX,
324 CMD_ROOT_GET_PROTOCOL_VERSION,
325 NULL, 0, &response);
326
327 if (ret == 1) {
328 hidpp->protocol_major = 1;
329 hidpp->protocol_minor = 0;
330 return 0;
331 }
332
333 if (ret)
334 return -ret;
335
336 hidpp->protocol_major = response.fap.params[0];
337 hidpp->protocol_minor = response.fap.params[1];
338
339 return ret;
340}
341
342static bool hidpp_is_connected(struct hidpp_device *hidpp)
343{
344 int ret;
345
346 ret = hidpp_root_get_protocol_version(hidpp);
347 if (!ret)
348 hid_dbg(hidpp->hid_dev, "HID++ %u.%u device connected.\n",
349 hidpp->protocol_major, hidpp->protocol_minor);
350 return ret == 0;
351}
352
353/* -------------------------------------------------------------------------- */
354/* 0x0005: GetDeviceNameType */
355/* -------------------------------------------------------------------------- */
356
357#define HIDPP_PAGE_GET_DEVICE_NAME_TYPE 0x0005
358
359#define CMD_GET_DEVICE_NAME_TYPE_GET_COUNT 0x01
360#define CMD_GET_DEVICE_NAME_TYPE_GET_DEVICE_NAME 0x11
361#define CMD_GET_DEVICE_NAME_TYPE_GET_TYPE 0x21
362
363static int hidpp_devicenametype_get_count(struct hidpp_device *hidpp,
364 u8 feature_index, u8 *nameLength)
365{
366 struct hidpp_report response;
367 int ret;
368
369 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
370 CMD_GET_DEVICE_NAME_TYPE_GET_COUNT, NULL, 0, &response);
371
372 if (ret)
373 return -ret;
374
375 *nameLength = response.fap.params[0];
376
377 return ret;
378}
379
380static int hidpp_devicenametype_get_device_name(struct hidpp_device *hidpp,
381 u8 feature_index, u8 char_index, char *device_name, int len_buf)
382{
383 struct hidpp_report response;
384 int ret, i;
385 int count;
386
387 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
388 CMD_GET_DEVICE_NAME_TYPE_GET_DEVICE_NAME, &char_index, 1,
389 &response);
390
391 if (ret)
392 return -ret;
393
394 if (response.report_id == REPORT_ID_HIDPP_LONG)
395 count = HIDPP_REPORT_LONG_LENGTH - 4;
396 else
397 count = HIDPP_REPORT_SHORT_LENGTH - 4;
398
399 if (len_buf < count)
400 count = len_buf;
401
402 for (i = 0; i < count; i++)
403 device_name[i] = response.fap.params[i];
404
405 return count;
406}
407
408static char *hidpp_get_device_name(struct hidpp_device *hidpp, u8 *name_length)
409{
410 u8 feature_type;
411 u8 feature_index;
412 u8 __name_length;
413 char *name;
414 unsigned index = 0;
415 int ret;
416
417 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_GET_DEVICE_NAME_TYPE,
418 &feature_index, &feature_type);
419 if (ret)
420 goto out_err;
421
422 ret = hidpp_devicenametype_get_count(hidpp, feature_index,
423 &__name_length);
424 if (ret)
425 goto out_err;
426
427 name = kzalloc(__name_length + 1, GFP_KERNEL);
428 if (!name)
429 goto out_err;
430
431 *name_length = __name_length + 1;
432 while (index < __name_length)
433 index += hidpp_devicenametype_get_device_name(hidpp,
434 feature_index, index, name + index,
435 __name_length - index);
436
437 return name;
438
439out_err:
440 *name_length = 0;
441 return NULL;
442}
443
444/* -------------------------------------------------------------------------- */
445/* 0x6100: TouchPadRawXY */
446/* -------------------------------------------------------------------------- */
447
448#define HIDPP_PAGE_TOUCHPAD_RAW_XY 0x6100
449
450#define CMD_TOUCHPAD_GET_RAW_INFO 0x01
451
452#define TOUCHPAD_RAW_XY_ORIGIN_LOWER_LEFT 0x01
453#define TOUCHPAD_RAW_XY_ORIGIN_UPPER_LEFT 0x03
454
455struct hidpp_touchpad_raw_info {
456 u16 x_size;
457 u16 y_size;
458 u8 z_range;
459 u8 area_range;
460 u8 timestamp_unit;
461 u8 maxcontacts;
462 u8 origin;
463 u16 res;
464};
465
466struct hidpp_touchpad_raw_xy_finger {
467 u8 contact_type;
468 u8 contact_status;
469 u16 x;
470 u16 y;
471 u8 z;
472 u8 area;
473 u8 finger_id;
474};
475
476struct hidpp_touchpad_raw_xy {
477 u16 timestamp;
478 struct hidpp_touchpad_raw_xy_finger fingers[2];
479 u8 spurious_flag;
480 u8 end_of_frame;
481 u8 finger_count;
482 u8 button;
483};
484
485static int hidpp_touchpad_get_raw_info(struct hidpp_device *hidpp,
486 u8 feature_index, struct hidpp_touchpad_raw_info *raw_info)
487{
488 struct hidpp_report response;
489 int ret;
490 u8 *params = (u8 *)response.fap.params;
491
492 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
493 CMD_TOUCHPAD_GET_RAW_INFO, NULL, 0, &response);
494
495 if (ret)
496 return -ret;
497
498 raw_info->x_size = get_unaligned_be16(&params[0]);
499 raw_info->y_size = get_unaligned_be16(&params[2]);
500 raw_info->z_range = params[4];
501 raw_info->area_range = params[5];
502 raw_info->maxcontacts = params[7];
503 raw_info->origin = params[8];
504 /* res is given in unit per inch */
505 raw_info->res = get_unaligned_be16(&params[13]) * 2 / 51;
506
507 return ret;
508}
509
510/* ************************************************************************** */
511/* */
512/* Device Support */
513/* */
514/* ************************************************************************** */
515
516/* -------------------------------------------------------------------------- */
517/* Touchpad HID++ devices */
518/* -------------------------------------------------------------------------- */
519
520struct wtp_data {
521 struct input_dev *input;
522 u16 x_size, y_size;
523 u8 finger_count;
524 u8 mt_feature_index;
525 u8 button_feature_index;
526 u8 maxcontacts;
527 bool flip_y;
528 unsigned int resolution;
529};
530
531static int wtp_input_mapping(struct hid_device *hdev, struct hid_input *hi,
532 struct hid_field *field, struct hid_usage *usage,
533 unsigned long **bit, int *max)
534{
535 return -1;
536}
537
538static void wtp_input_configured(struct hid_device *hdev,
539 struct hid_input *hidinput)
540{
541 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
542 struct wtp_data *wd = hidpp->private_data;
543 struct input_dev *input_dev = hidinput->input;
544
545 __set_bit(EV_ABS, input_dev->evbit);
546 __set_bit(EV_KEY, input_dev->evbit);
547 __clear_bit(EV_REL, input_dev->evbit);
548 __clear_bit(EV_LED, input_dev->evbit);
549
550 input_set_abs_params(input_dev, ABS_MT_POSITION_X, 0, wd->x_size, 0, 0);
551 input_abs_set_res(input_dev, ABS_MT_POSITION_X, wd->resolution);
552 input_set_abs_params(input_dev, ABS_MT_POSITION_Y, 0, wd->y_size, 0, 0);
553 input_abs_set_res(input_dev, ABS_MT_POSITION_Y, wd->resolution);
554
555 /* Max pressure is not given by the devices, pick one */
556 input_set_abs_params(input_dev, ABS_MT_PRESSURE, 0, 50, 0, 0);
557
558 input_set_capability(input_dev, EV_KEY, BTN_LEFT);
559
560 __set_bit(INPUT_PROP_BUTTONPAD, input_dev->propbit);
561
562 input_mt_init_slots(input_dev, wd->maxcontacts, INPUT_MT_POINTER |
563 INPUT_MT_DROP_UNUSED);
564
565 wd->input = input_dev;
566}
567
568static void wtp_touch_event(struct wtp_data *wd,
569 struct hidpp_touchpad_raw_xy_finger *touch_report)
570{
571 int slot;
572
573 if (!touch_report->finger_id || touch_report->contact_type)
574 /* no actual data */
575 return;
576
577 slot = input_mt_get_slot_by_key(wd->input, touch_report->finger_id);
578
579 input_mt_slot(wd->input, slot);
580 input_mt_report_slot_state(wd->input, MT_TOOL_FINGER,
581 touch_report->contact_status);
582 if (touch_report->contact_status) {
583 input_event(wd->input, EV_ABS, ABS_MT_POSITION_X,
584 touch_report->x);
585 input_event(wd->input, EV_ABS, ABS_MT_POSITION_Y,
586 wd->flip_y ? wd->y_size - touch_report->y :
587 touch_report->y);
588 input_event(wd->input, EV_ABS, ABS_MT_PRESSURE,
589 touch_report->area);
590 }
591}
592
593static void wtp_send_raw_xy_event(struct hidpp_device *hidpp,
594 struct hidpp_touchpad_raw_xy *raw)
595{
596 struct wtp_data *wd = hidpp->private_data;
597 int i;
598
599 for (i = 0; i < 2; i++)
600 wtp_touch_event(wd, &(raw->fingers[i]));
601
602 if (raw->end_of_frame)
603 input_event(wd->input, EV_KEY, BTN_LEFT, raw->button);
604
605 if (raw->end_of_frame || raw->finger_count <= 2) {
606 input_mt_sync_frame(wd->input);
607 input_sync(wd->input);
608 }
609}
610
611static int wtp_mouse_raw_xy_event(struct hidpp_device *hidpp, u8 *data)
612{
613 struct wtp_data *wd = hidpp->private_data;
614 u8 c1_area = ((data[7] & 0xf) * (data[7] & 0xf) +
615 (data[7] >> 4) * (data[7] >> 4)) / 2;
616 u8 c2_area = ((data[13] & 0xf) * (data[13] & 0xf) +
617 (data[13] >> 4) * (data[13] >> 4)) / 2;
618 struct hidpp_touchpad_raw_xy raw = {
619 .timestamp = data[1],
620 .fingers = {
621 {
622 .contact_type = 0,
623 .contact_status = !!data[7],
624 .x = get_unaligned_le16(&data[3]),
625 .y = get_unaligned_le16(&data[5]),
626 .z = c1_area,
627 .area = c1_area,
628 .finger_id = data[2],
629 }, {
630 .contact_type = 0,
631 .contact_status = !!data[13],
632 .x = get_unaligned_le16(&data[9]),
633 .y = get_unaligned_le16(&data[11]),
634 .z = c2_area,
635 .area = c2_area,
636 .finger_id = data[8],
637 }
638 },
639 .finger_count = wd->maxcontacts,
640 .spurious_flag = 0,
641 .end_of_frame = (data[0] >> 7) == 0,
642 .button = data[0] & 0x01,
643 };
644
645 wtp_send_raw_xy_event(hidpp, &raw);
646
647 return 1;
648}
649
650static int wtp_raw_event(struct hid_device *hdev, u8 *data, int size)
651{
652 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
653 struct wtp_data *wd = hidpp->private_data;
654
655 if (!wd || !wd->input || (data[0] != 0x02) || size < 21)
656 return 1;
657
658 return wtp_mouse_raw_xy_event(hidpp, &data[7]);
659}
660
661static int wtp_get_config(struct hidpp_device *hidpp)
662{
663 struct wtp_data *wd = hidpp->private_data;
664 struct hidpp_touchpad_raw_info raw_info = {0};
665 u8 feature_type;
666 int ret;
667
668 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_TOUCHPAD_RAW_XY,
669 &wd->mt_feature_index, &feature_type);
670 if (ret)
671 /* means that the device is not powered up */
672 return ret;
673
674 ret = hidpp_touchpad_get_raw_info(hidpp, wd->mt_feature_index,
675 &raw_info);
676 if (ret)
677 return ret;
678
679 wd->x_size = raw_info.x_size;
680 wd->y_size = raw_info.y_size;
681 wd->maxcontacts = raw_info.maxcontacts;
682 wd->flip_y = raw_info.origin == TOUCHPAD_RAW_XY_ORIGIN_LOWER_LEFT;
683 wd->resolution = raw_info.res;
684
685 return 0;
686}
687
688static int wtp_allocate(struct hid_device *hdev, const struct hid_device_id *id)
689{
690 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
691 struct wtp_data *wd;
692
693 wd = devm_kzalloc(&hdev->dev, sizeof(struct wtp_data),
694 GFP_KERNEL);
695 if (!wd)
696 return -ENOMEM;
697
698 hidpp->private_data = wd;
699
700 return 0;
701};
702
703/* -------------------------------------------------------------------------- */
704/* Generic HID++ devices */
705/* -------------------------------------------------------------------------- */
706
707static int hidpp_input_mapping(struct hid_device *hdev, struct hid_input *hi,
708 struct hid_field *field, struct hid_usage *usage,
709 unsigned long **bit, int *max)
710{
711 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
712
713 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
714 return wtp_input_mapping(hdev, hi, field, usage, bit, max);
715
716 return 0;
717}
718
719static void hidpp_input_configured(struct hid_device *hdev,
720 struct hid_input *hidinput)
721{
722 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
723
724 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
725 wtp_input_configured(hdev, hidinput);
726}
727
728static int hidpp_raw_hidpp_event(struct hidpp_device *hidpp, u8 *data,
729 int size)
730{
731 struct hidpp_report *question = hidpp->send_receive_buf;
732 struct hidpp_report *answer = hidpp->send_receive_buf;
733 struct hidpp_report *report = (struct hidpp_report *)data;
734
735 /*
736 * If the mutex is locked then we have a pending answer from a
737 * previoulsly sent command
738 */
739 if (unlikely(mutex_is_locked(&hidpp->send_mutex))) {
740 /*
741 * Check for a correct hidpp20 answer or the corresponding
742 * error
743 */
744 if (hidpp_match_answer(question, report) ||
745 hidpp_match_error(question, report)) {
746 *answer = *report;
747 hidpp->answer_available = true;
748 wake_up(&hidpp->wait);
749 /*
750 * This was an answer to a command that this driver sent
751 * We return 1 to hid-core to avoid forwarding the
752 * command upstream as it has been treated by the driver
753 */
754
755 return 1;
756 }
757 }
758
759 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
760 return wtp_raw_event(hidpp->hid_dev, data, size);
761
762 return 0;
763}
764
765static int hidpp_raw_event(struct hid_device *hdev, struct hid_report *report,
766 u8 *data, int size)
767{
768 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
769
770 switch (data[0]) {
771 case REPORT_ID_HIDPP_LONG:
772 if (size != HIDPP_REPORT_LONG_LENGTH) {
773 hid_err(hdev, "received hid++ report of bad size (%d)",
774 size);
775 return 1;
776 }
777 return hidpp_raw_hidpp_event(hidpp, data, size);
778 case REPORT_ID_HIDPP_SHORT:
779 if (size != HIDPP_REPORT_SHORT_LENGTH) {
780 hid_err(hdev, "received hid++ report of bad size (%d)",
781 size);
782 return 1;
783 }
784 return hidpp_raw_hidpp_event(hidpp, data, size);
785 }
786
787 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
788 return wtp_raw_event(hdev, data, size);
789
790 return 0;
791}
792
33797820 793static void hidpp_overwrite_name(struct hid_device *hdev, bool use_unifying)
2f31c525
BT
794{
795 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
796 char *name;
797 u8 name_length;
798
33797820
BT
799 if (use_unifying)
800 /*
801 * the device is connected through an Unifying receiver, and
802 * might not be already connected.
803 * Ask the receiver for its name.
804 */
805 name = hidpp_get_unifying_name(hidpp);
806 else
807 name = hidpp_get_device_name(hidpp, &name_length);
2f31c525
BT
808
809 if (!name)
810 hid_err(hdev, "unable to retrieve the name of the device");
811 else
812 snprintf(hdev->name, sizeof(hdev->name), "%s", name);
813
814 kfree(name);
815}
816
817static int hidpp_probe(struct hid_device *hdev, const struct hid_device_id *id)
818{
819 struct hidpp_device *hidpp;
820 int ret;
821 bool connected;
822
823 hidpp = devm_kzalloc(&hdev->dev, sizeof(struct hidpp_device),
824 GFP_KERNEL);
825 if (!hidpp)
826 return -ENOMEM;
827
828 hidpp->hid_dev = hdev;
829 hid_set_drvdata(hdev, hidpp);
830
831 hidpp->quirks = id->driver_data;
832
833 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP) {
834 ret = wtp_allocate(hdev, id);
835 if (ret)
836 return ret;
837 }
838
839 mutex_init(&hidpp->send_mutex);
840 init_waitqueue_head(&hidpp->wait);
841
842 ret = hid_parse(hdev);
843 if (ret) {
844 hid_err(hdev, "%s:parse failed\n", __func__);
845 goto hid_parse_fail;
846 }
847
848 /* Allow incoming packets */
849 hid_device_io_start(hdev);
850
851 connected = hidpp_is_connected(hidpp);
ab94e562
BT
852 if (id->group != HID_GROUP_LOGITECH_DJ_DEVICE) {
853 if (!connected) {
854 hid_err(hdev, "Device not connected");
855 goto hid_parse_fail;
856 }
2f31c525 857
ab94e562
BT
858 hid_info(hdev, "HID++ %u.%u device connected.\n",
859 hidpp->protocol_major, hidpp->protocol_minor);
ab94e562 860 }
2f31c525 861
33797820
BT
862 hidpp_overwrite_name(hdev, id->group == HID_GROUP_LOGITECH_DJ_DEVICE);
863
2f31c525
BT
864 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP) {
865 ret = wtp_get_config(hidpp);
866 if (ret)
867 goto hid_parse_fail;
868 }
869
870 /* Block incoming packets */
871 hid_device_io_stop(hdev);
872
873 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
874 if (ret) {
875 hid_err(hdev, "%s:hid_hw_start returned error\n", __func__);
876 goto hid_hw_start_fail;
877 }
878
879 return ret;
880
881hid_hw_start_fail:
882hid_parse_fail:
883 mutex_destroy(&hidpp->send_mutex);
884 hid_set_drvdata(hdev, NULL);
885 return ret;
886}
887
888static void hidpp_remove(struct hid_device *hdev)
889{
890 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
891
892 mutex_destroy(&hidpp->send_mutex);
893 hid_hw_stop(hdev);
894}
895
896static const struct hid_device_id hidpp_devices[] = {
897 { /* wireless touchpad T651 */
898 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH,
899 USB_DEVICE_ID_LOGITECH_T651),
900 .driver_data = HIDPP_QUIRK_CLASS_WTP },
ab94e562
BT
901
902 { HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE,
903 USB_VENDOR_ID_LOGITECH, HID_ANY_ID)},
2f31c525
BT
904 {}
905};
906
907MODULE_DEVICE_TABLE(hid, hidpp_devices);
908
909static struct hid_driver hidpp_driver = {
910 .name = "logitech-hidpp-device",
911 .id_table = hidpp_devices,
912 .probe = hidpp_probe,
913 .remove = hidpp_remove,
914 .raw_event = hidpp_raw_event,
915 .input_configured = hidpp_input_configured,
916 .input_mapping = hidpp_input_mapping,
917};
918
919module_hid_driver(hidpp_driver);