blob: 7dd9163f7e03cc9dd6cbb399e9e4e5e80e1374ca [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
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
208static inline bool hidpp_match_answer(struct hidpp_report *question,
209 struct hidpp_report *answer)
210{
211 return (answer->fap.feature_index == question->fap.feature_index) &&
212 (answer->fap.funcindex_clientid == question->fap.funcindex_clientid);
213}
214
215static inline bool hidpp_match_error(struct hidpp_report *question,
216 struct hidpp_report *answer)
217{
218 return (answer->fap.feature_index == HIDPP_ERROR) &&
219 (answer->fap.funcindex_clientid == question->fap.feature_index) &&
220 (answer->fap.params[0] == question->fap.funcindex_clientid);
221}
222
223/* -------------------------------------------------------------------------- */
224/* 0x0000: Root */
225/* -------------------------------------------------------------------------- */
226
227#define HIDPP_PAGE_ROOT 0x0000
228#define HIDPP_PAGE_ROOT_IDX 0x00
229
230#define CMD_ROOT_GET_FEATURE 0x01
231#define CMD_ROOT_GET_PROTOCOL_VERSION 0x11
232
233static int hidpp_root_get_feature(struct hidpp_device *hidpp, u16 feature,
234 u8 *feature_index, u8 *feature_type)
235{
236 struct hidpp_report response;
237 int ret;
238 u8 params[2] = { feature >> 8, feature & 0x00FF };
239
240 ret = hidpp_send_fap_command_sync(hidpp,
241 HIDPP_PAGE_ROOT_IDX,
242 CMD_ROOT_GET_FEATURE,
243 params, 2, &response);
244 if (ret)
245 return ret;
246
247 *feature_index = response.fap.params[0];
248 *feature_type = response.fap.params[1];
249
250 return ret;
251}
252
253static int hidpp_root_get_protocol_version(struct hidpp_device *hidpp)
254{
255 struct hidpp_report response;
256 int ret;
257
258 ret = hidpp_send_fap_command_sync(hidpp,
259 HIDPP_PAGE_ROOT_IDX,
260 CMD_ROOT_GET_PROTOCOL_VERSION,
261 NULL, 0, &response);
262
263 if (ret == 1) {
264 hidpp->protocol_major = 1;
265 hidpp->protocol_minor = 0;
266 return 0;
267 }
268
269 if (ret)
270 return -ret;
271
272 hidpp->protocol_major = response.fap.params[0];
273 hidpp->protocol_minor = response.fap.params[1];
274
275 return ret;
276}
277
278static bool hidpp_is_connected(struct hidpp_device *hidpp)
279{
280 int ret;
281
282 ret = hidpp_root_get_protocol_version(hidpp);
283 if (!ret)
284 hid_dbg(hidpp->hid_dev, "HID++ %u.%u device connected.\n",
285 hidpp->protocol_major, hidpp->protocol_minor);
286 return ret == 0;
287}
288
289/* -------------------------------------------------------------------------- */
290/* 0x0005: GetDeviceNameType */
291/* -------------------------------------------------------------------------- */
292
293#define HIDPP_PAGE_GET_DEVICE_NAME_TYPE 0x0005
294
295#define CMD_GET_DEVICE_NAME_TYPE_GET_COUNT 0x01
296#define CMD_GET_DEVICE_NAME_TYPE_GET_DEVICE_NAME 0x11
297#define CMD_GET_DEVICE_NAME_TYPE_GET_TYPE 0x21
298
299static int hidpp_devicenametype_get_count(struct hidpp_device *hidpp,
300 u8 feature_index, u8 *nameLength)
301{
302 struct hidpp_report response;
303 int ret;
304
305 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
306 CMD_GET_DEVICE_NAME_TYPE_GET_COUNT, NULL, 0, &response);
307
308 if (ret)
309 return -ret;
310
311 *nameLength = response.fap.params[0];
312
313 return ret;
314}
315
316static int hidpp_devicenametype_get_device_name(struct hidpp_device *hidpp,
317 u8 feature_index, u8 char_index, char *device_name, int len_buf)
318{
319 struct hidpp_report response;
320 int ret, i;
321 int count;
322
323 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
324 CMD_GET_DEVICE_NAME_TYPE_GET_DEVICE_NAME, &char_index, 1,
325 &response);
326
327 if (ret)
328 return -ret;
329
330 if (response.report_id == REPORT_ID_HIDPP_LONG)
331 count = HIDPP_REPORT_LONG_LENGTH - 4;
332 else
333 count = HIDPP_REPORT_SHORT_LENGTH - 4;
334
335 if (len_buf < count)
336 count = len_buf;
337
338 for (i = 0; i < count; i++)
339 device_name[i] = response.fap.params[i];
340
341 return count;
342}
343
344static char *hidpp_get_device_name(struct hidpp_device *hidpp, u8 *name_length)
345{
346 u8 feature_type;
347 u8 feature_index;
348 u8 __name_length;
349 char *name;
350 unsigned index = 0;
351 int ret;
352
353 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_GET_DEVICE_NAME_TYPE,
354 &feature_index, &feature_type);
355 if (ret)
356 goto out_err;
357
358 ret = hidpp_devicenametype_get_count(hidpp, feature_index,
359 &__name_length);
360 if (ret)
361 goto out_err;
362
363 name = kzalloc(__name_length + 1, GFP_KERNEL);
364 if (!name)
365 goto out_err;
366
367 *name_length = __name_length + 1;
368 while (index < __name_length)
369 index += hidpp_devicenametype_get_device_name(hidpp,
370 feature_index, index, name + index,
371 __name_length - index);
372
373 return name;
374
375out_err:
376 *name_length = 0;
377 return NULL;
378}
379
380/* -------------------------------------------------------------------------- */
381/* 0x6100: TouchPadRawXY */
382/* -------------------------------------------------------------------------- */
383
384#define HIDPP_PAGE_TOUCHPAD_RAW_XY 0x6100
385
386#define CMD_TOUCHPAD_GET_RAW_INFO 0x01
387
388#define TOUCHPAD_RAW_XY_ORIGIN_LOWER_LEFT 0x01
389#define TOUCHPAD_RAW_XY_ORIGIN_UPPER_LEFT 0x03
390
391struct hidpp_touchpad_raw_info {
392 u16 x_size;
393 u16 y_size;
394 u8 z_range;
395 u8 area_range;
396 u8 timestamp_unit;
397 u8 maxcontacts;
398 u8 origin;
399 u16 res;
400};
401
402struct hidpp_touchpad_raw_xy_finger {
403 u8 contact_type;
404 u8 contact_status;
405 u16 x;
406 u16 y;
407 u8 z;
408 u8 area;
409 u8 finger_id;
410};
411
412struct hidpp_touchpad_raw_xy {
413 u16 timestamp;
414 struct hidpp_touchpad_raw_xy_finger fingers[2];
415 u8 spurious_flag;
416 u8 end_of_frame;
417 u8 finger_count;
418 u8 button;
419};
420
421static int hidpp_touchpad_get_raw_info(struct hidpp_device *hidpp,
422 u8 feature_index, struct hidpp_touchpad_raw_info *raw_info)
423{
424 struct hidpp_report response;
425 int ret;
426 u8 *params = (u8 *)response.fap.params;
427
428 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
429 CMD_TOUCHPAD_GET_RAW_INFO, NULL, 0, &response);
430
431 if (ret)
432 return -ret;
433
434 raw_info->x_size = get_unaligned_be16(&params[0]);
435 raw_info->y_size = get_unaligned_be16(&params[2]);
436 raw_info->z_range = params[4];
437 raw_info->area_range = params[5];
438 raw_info->maxcontacts = params[7];
439 raw_info->origin = params[8];
440 /* res is given in unit per inch */
441 raw_info->res = get_unaligned_be16(&params[13]) * 2 / 51;
442
443 return ret;
444}
445
446/* ************************************************************************** */
447/* */
448/* Device Support */
449/* */
450/* ************************************************************************** */
451
452/* -------------------------------------------------------------------------- */
453/* Touchpad HID++ devices */
454/* -------------------------------------------------------------------------- */
455
456struct wtp_data {
457 struct input_dev *input;
458 u16 x_size, y_size;
459 u8 finger_count;
460 u8 mt_feature_index;
461 u8 button_feature_index;
462 u8 maxcontacts;
463 bool flip_y;
464 unsigned int resolution;
465};
466
467static int wtp_input_mapping(struct hid_device *hdev, struct hid_input *hi,
468 struct hid_field *field, struct hid_usage *usage,
469 unsigned long **bit, int *max)
470{
471 return -1;
472}
473
474static void wtp_input_configured(struct hid_device *hdev,
475 struct hid_input *hidinput)
476{
477 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
478 struct wtp_data *wd = hidpp->private_data;
479 struct input_dev *input_dev = hidinput->input;
480
481 __set_bit(EV_ABS, input_dev->evbit);
482 __set_bit(EV_KEY, input_dev->evbit);
483 __clear_bit(EV_REL, input_dev->evbit);
484 __clear_bit(EV_LED, input_dev->evbit);
485
486 input_set_abs_params(input_dev, ABS_MT_POSITION_X, 0, wd->x_size, 0, 0);
487 input_abs_set_res(input_dev, ABS_MT_POSITION_X, wd->resolution);
488 input_set_abs_params(input_dev, ABS_MT_POSITION_Y, 0, wd->y_size, 0, 0);
489 input_abs_set_res(input_dev, ABS_MT_POSITION_Y, wd->resolution);
490
491 /* Max pressure is not given by the devices, pick one */
492 input_set_abs_params(input_dev, ABS_MT_PRESSURE, 0, 50, 0, 0);
493
494 input_set_capability(input_dev, EV_KEY, BTN_LEFT);
495
496 __set_bit(INPUT_PROP_BUTTONPAD, input_dev->propbit);
497
498 input_mt_init_slots(input_dev, wd->maxcontacts, INPUT_MT_POINTER |
499 INPUT_MT_DROP_UNUSED);
500
501 wd->input = input_dev;
502}
503
504static void wtp_touch_event(struct wtp_data *wd,
505 struct hidpp_touchpad_raw_xy_finger *touch_report)
506{
507 int slot;
508
509 if (!touch_report->finger_id || touch_report->contact_type)
510 /* no actual data */
511 return;
512
513 slot = input_mt_get_slot_by_key(wd->input, touch_report->finger_id);
514
515 input_mt_slot(wd->input, slot);
516 input_mt_report_slot_state(wd->input, MT_TOOL_FINGER,
517 touch_report->contact_status);
518 if (touch_report->contact_status) {
519 input_event(wd->input, EV_ABS, ABS_MT_POSITION_X,
520 touch_report->x);
521 input_event(wd->input, EV_ABS, ABS_MT_POSITION_Y,
522 wd->flip_y ? wd->y_size - touch_report->y :
523 touch_report->y);
524 input_event(wd->input, EV_ABS, ABS_MT_PRESSURE,
525 touch_report->area);
526 }
527}
528
529static void wtp_send_raw_xy_event(struct hidpp_device *hidpp,
530 struct hidpp_touchpad_raw_xy *raw)
531{
532 struct wtp_data *wd = hidpp->private_data;
533 int i;
534
535 for (i = 0; i < 2; i++)
536 wtp_touch_event(wd, &(raw->fingers[i]));
537
538 if (raw->end_of_frame)
539 input_event(wd->input, EV_KEY, BTN_LEFT, raw->button);
540
541 if (raw->end_of_frame || raw->finger_count <= 2) {
542 input_mt_sync_frame(wd->input);
543 input_sync(wd->input);
544 }
545}
546
547static int wtp_mouse_raw_xy_event(struct hidpp_device *hidpp, u8 *data)
548{
549 struct wtp_data *wd = hidpp->private_data;
550 u8 c1_area = ((data[7] & 0xf) * (data[7] & 0xf) +
551 (data[7] >> 4) * (data[7] >> 4)) / 2;
552 u8 c2_area = ((data[13] & 0xf) * (data[13] & 0xf) +
553 (data[13] >> 4) * (data[13] >> 4)) / 2;
554 struct hidpp_touchpad_raw_xy raw = {
555 .timestamp = data[1],
556 .fingers = {
557 {
558 .contact_type = 0,
559 .contact_status = !!data[7],
560 .x = get_unaligned_le16(&data[3]),
561 .y = get_unaligned_le16(&data[5]),
562 .z = c1_area,
563 .area = c1_area,
564 .finger_id = data[2],
565 }, {
566 .contact_type = 0,
567 .contact_status = !!data[13],
568 .x = get_unaligned_le16(&data[9]),
569 .y = get_unaligned_le16(&data[11]),
570 .z = c2_area,
571 .area = c2_area,
572 .finger_id = data[8],
573 }
574 },
575 .finger_count = wd->maxcontacts,
576 .spurious_flag = 0,
577 .end_of_frame = (data[0] >> 7) == 0,
578 .button = data[0] & 0x01,
579 };
580
581 wtp_send_raw_xy_event(hidpp, &raw);
582
583 return 1;
584}
585
586static int wtp_raw_event(struct hid_device *hdev, u8 *data, int size)
587{
588 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
589 struct wtp_data *wd = hidpp->private_data;
590
591 if (!wd || !wd->input || (data[0] != 0x02) || size < 21)
592 return 1;
593
594 return wtp_mouse_raw_xy_event(hidpp, &data[7]);
595}
596
597static int wtp_get_config(struct hidpp_device *hidpp)
598{
599 struct wtp_data *wd = hidpp->private_data;
600 struct hidpp_touchpad_raw_info raw_info = {0};
601 u8 feature_type;
602 int ret;
603
604 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_TOUCHPAD_RAW_XY,
605 &wd->mt_feature_index, &feature_type);
606 if (ret)
607 /* means that the device is not powered up */
608 return ret;
609
610 ret = hidpp_touchpad_get_raw_info(hidpp, wd->mt_feature_index,
611 &raw_info);
612 if (ret)
613 return ret;
614
615 wd->x_size = raw_info.x_size;
616 wd->y_size = raw_info.y_size;
617 wd->maxcontacts = raw_info.maxcontacts;
618 wd->flip_y = raw_info.origin == TOUCHPAD_RAW_XY_ORIGIN_LOWER_LEFT;
619 wd->resolution = raw_info.res;
620
621 return 0;
622}
623
624static int wtp_allocate(struct hid_device *hdev, const struct hid_device_id *id)
625{
626 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
627 struct wtp_data *wd;
628
629 wd = devm_kzalloc(&hdev->dev, sizeof(struct wtp_data),
630 GFP_KERNEL);
631 if (!wd)
632 return -ENOMEM;
633
634 hidpp->private_data = wd;
635
636 return 0;
637};
638
639/* -------------------------------------------------------------------------- */
640/* Generic HID++ devices */
641/* -------------------------------------------------------------------------- */
642
643static int hidpp_input_mapping(struct hid_device *hdev, struct hid_input *hi,
644 struct hid_field *field, struct hid_usage *usage,
645 unsigned long **bit, int *max)
646{
647 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
648
649 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
650 return wtp_input_mapping(hdev, hi, field, usage, bit, max);
651
652 return 0;
653}
654
655static void hidpp_input_configured(struct hid_device *hdev,
656 struct hid_input *hidinput)
657{
658 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
659
660 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
661 wtp_input_configured(hdev, hidinput);
662}
663
664static int hidpp_raw_hidpp_event(struct hidpp_device *hidpp, u8 *data,
665 int size)
666{
667 struct hidpp_report *question = hidpp->send_receive_buf;
668 struct hidpp_report *answer = hidpp->send_receive_buf;
669 struct hidpp_report *report = (struct hidpp_report *)data;
670
671 /*
672 * If the mutex is locked then we have a pending answer from a
673 * previoulsly sent command
674 */
675 if (unlikely(mutex_is_locked(&hidpp->send_mutex))) {
676 /*
677 * Check for a correct hidpp20 answer or the corresponding
678 * error
679 */
680 if (hidpp_match_answer(question, report) ||
681 hidpp_match_error(question, report)) {
682 *answer = *report;
683 hidpp->answer_available = true;
684 wake_up(&hidpp->wait);
685 /*
686 * This was an answer to a command that this driver sent
687 * We return 1 to hid-core to avoid forwarding the
688 * command upstream as it has been treated by the driver
689 */
690
691 return 1;
692 }
693 }
694
695 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
696 return wtp_raw_event(hidpp->hid_dev, data, size);
697
698 return 0;
699}
700
701static int hidpp_raw_event(struct hid_device *hdev, struct hid_report *report,
702 u8 *data, int size)
703{
704 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
705
706 switch (data[0]) {
707 case REPORT_ID_HIDPP_LONG:
708 if (size != HIDPP_REPORT_LONG_LENGTH) {
709 hid_err(hdev, "received hid++ report of bad size (%d)",
710 size);
711 return 1;
712 }
713 return hidpp_raw_hidpp_event(hidpp, data, size);
714 case REPORT_ID_HIDPP_SHORT:
715 if (size != HIDPP_REPORT_SHORT_LENGTH) {
716 hid_err(hdev, "received hid++ report of bad size (%d)",
717 size);
718 return 1;
719 }
720 return hidpp_raw_hidpp_event(hidpp, data, size);
721 }
722
723 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
724 return wtp_raw_event(hdev, data, size);
725
726 return 0;
727}
728
729static void hidpp_overwrite_name(struct hid_device *hdev)
730{
731 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
732 char *name;
733 u8 name_length;
734
735 name = hidpp_get_device_name(hidpp, &name_length);
736
737 if (!name)
738 hid_err(hdev, "unable to retrieve the name of the device");
739 else
740 snprintf(hdev->name, sizeof(hdev->name), "%s", name);
741
742 kfree(name);
743}
744
745static int hidpp_probe(struct hid_device *hdev, const struct hid_device_id *id)
746{
747 struct hidpp_device *hidpp;
748 int ret;
749 bool connected;
750
751 hidpp = devm_kzalloc(&hdev->dev, sizeof(struct hidpp_device),
752 GFP_KERNEL);
753 if (!hidpp)
754 return -ENOMEM;
755
756 hidpp->hid_dev = hdev;
757 hid_set_drvdata(hdev, hidpp);
758
759 hidpp->quirks = id->driver_data;
760
761 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP) {
762 ret = wtp_allocate(hdev, id);
763 if (ret)
764 return ret;
765 }
766
767 mutex_init(&hidpp->send_mutex);
768 init_waitqueue_head(&hidpp->wait);
769
770 ret = hid_parse(hdev);
771 if (ret) {
772 hid_err(hdev, "%s:parse failed\n", __func__);
773 goto hid_parse_fail;
774 }
775
776 /* Allow incoming packets */
777 hid_device_io_start(hdev);
778
779 connected = hidpp_is_connected(hidpp);
780 if (!connected) {
781 hid_err(hdev, "Device not connected");
782 goto hid_parse_fail;
783 }
784
785 /* the device is connected, we can ask for its name */
786 hid_info(hdev, "HID++ %u.%u device connected.\n",
787 hidpp->protocol_major, hidpp->protocol_minor);
788 hidpp_overwrite_name(hdev);
789
790 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP) {
791 ret = wtp_get_config(hidpp);
792 if (ret)
793 goto hid_parse_fail;
794 }
795
796 /* Block incoming packets */
797 hid_device_io_stop(hdev);
798
799 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
800 if (ret) {
801 hid_err(hdev, "%s:hid_hw_start returned error\n", __func__);
802 goto hid_hw_start_fail;
803 }
804
805 return ret;
806
807hid_hw_start_fail:
808hid_parse_fail:
809 mutex_destroy(&hidpp->send_mutex);
810 hid_set_drvdata(hdev, NULL);
811 return ret;
812}
813
814static void hidpp_remove(struct hid_device *hdev)
815{
816 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
817
818 mutex_destroy(&hidpp->send_mutex);
819 hid_hw_stop(hdev);
820}
821
822static const struct hid_device_id hidpp_devices[] = {
823 { /* wireless touchpad T651 */
824 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH,
825 USB_DEVICE_ID_LOGITECH_T651),
826 .driver_data = HIDPP_QUIRK_CLASS_WTP },
827 {}
828};
829
830MODULE_DEVICE_TABLE(hid, hidpp_devices);
831
832static struct hid_driver hidpp_driver = {
833 .name = "logitech-hidpp-device",
834 .id_table = hidpp_devices,
835 .probe = hidpp_probe,
836 .remove = hidpp_remove,
837 .raw_event = hidpp_raw_event,
838 .input_configured = hidpp_input_configured,
839 .input_mapping = hidpp_input_mapping,
840};
841
842module_hid_driver(hidpp_driver);