2 * HIDPP protocol for Logitech Unifying receivers
4 * Copyright (c) 2011 Logitech (c)
5 * Copyright (c) 2012-2013 Google (c)
6 * Copyright (c) 2013-2014 Red Hat Inc.
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.
15 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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>
27 MODULE_LICENSE("GPL");
28 MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
29 MODULE_AUTHOR("Nestor Lopez Casado <nlopezcasad@logitech.com>");
31 #define REPORT_ID_HIDPP_SHORT 0x10
32 #define REPORT_ID_HIDPP_LONG 0x11
34 #define HIDPP_REPORT_SHORT_LENGTH 7
35 #define HIDPP_REPORT_LONG_LENGTH 20
37 #define HIDPP_QUIRK_CLASS_WTP BIT(0)
39 /* bits 1..20 are reserved for classes */
40 #define HIDPP_QUIRK_DELAYED_INIT BIT(21)
43 * There are two hidpp protocols in use, the first version hidpp10 is known
44 * as register access protocol or RAP, the second version hidpp20 is known as
45 * feature access protocol or FAP
47 * Most older devices (including the Unifying usb receiver) use the RAP protocol
48 * where as most newer devices use the FAP protocol. Both protocols are
49 * compatible with the underlying transport, which could be usb, Unifiying, or
50 * bluetooth. The message lengths are defined by the hid vendor specific report
51 * descriptor for the HIDPP_SHORT report type (total message lenth 7 bytes) and
52 * the HIDPP_LONG report type (total message length 20 bytes)
54 * The RAP protocol uses both report types, whereas the FAP only uses HIDPP_LONG
55 * messages. The Unifying receiver itself responds to RAP messages (device index
56 * is 0xFF for the receiver), and all messages (short or long) with a device
57 * index between 1 and 6 are passed untouched to the corresponding paired
60 * The paired device can be RAP or FAP, it will receive the message untouched
61 * from the Unifiying receiver.
66 u8 funcindex_clientid;
67 u8 params[HIDPP_REPORT_LONG_LENGTH - 4U];
73 u8 params[HIDPP_REPORT_LONG_LENGTH - 4U];
82 u8 rawbytes[sizeof(struct fap)];
87 struct hid_device *hid_dev;
88 struct mutex send_mutex;
89 void *send_receive_buf;
90 wait_queue_head_t wait;
91 bool answer_available;
97 struct work_struct work;
98 struct kfifo delayed_work_fifo;
100 struct input_dev *delayed_input;
102 unsigned long quirks;
106 #define HIDPP_ERROR 0x8f
107 #define HIDPP_ERROR_SUCCESS 0x00
108 #define HIDPP_ERROR_INVALID_SUBID 0x01
109 #define HIDPP_ERROR_INVALID_ADRESS 0x02
110 #define HIDPP_ERROR_INVALID_VALUE 0x03
111 #define HIDPP_ERROR_CONNECT_FAIL 0x04
112 #define HIDPP_ERROR_TOO_MANY_DEVICES 0x05
113 #define HIDPP_ERROR_ALREADY_EXISTS 0x06
114 #define HIDPP_ERROR_BUSY 0x07
115 #define HIDPP_ERROR_UNKNOWN_DEVICE 0x08
116 #define HIDPP_ERROR_RESOURCE_ERROR 0x09
117 #define HIDPP_ERROR_REQUEST_UNAVAILABLE 0x0a
118 #define HIDPP_ERROR_INVALID_PARAM_VALUE 0x0b
119 #define HIDPP_ERROR_WRONG_PIN_CODE 0x0c
121 static void hidpp_connect_event(struct hidpp_device *hidpp_dev);
123 static int __hidpp_send_report(struct hid_device *hdev,
124 struct hidpp_report *hidpp_report)
126 int fields_count, ret;
128 switch (hidpp_report->report_id) {
129 case REPORT_ID_HIDPP_SHORT:
130 fields_count = HIDPP_REPORT_SHORT_LENGTH;
132 case REPORT_ID_HIDPP_LONG:
133 fields_count = HIDPP_REPORT_LONG_LENGTH;
140 * set the device_index as the receiver, it will be overwritten by
141 * hid_hw_request if needed
143 hidpp_report->device_index = 0xff;
145 ret = hid_hw_raw_request(hdev, hidpp_report->report_id,
146 (u8 *)hidpp_report, fields_count, HID_OUTPUT_REPORT,
149 return ret == fields_count ? 0 : -1;
152 static int hidpp_send_message_sync(struct hidpp_device *hidpp,
153 struct hidpp_report *message,
154 struct hidpp_report *response)
158 mutex_lock(&hidpp->send_mutex);
160 hidpp->send_receive_buf = response;
161 hidpp->answer_available = false;
164 * So that we can later validate the answer when it arrives
167 *response = *message;
169 ret = __hidpp_send_report(hidpp->hid_dev, message);
172 dbg_hid("__hidpp_send_report returned err: %d\n", ret);
173 memset(response, 0, sizeof(struct hidpp_report));
177 if (!wait_event_timeout(hidpp->wait, hidpp->answer_available,
179 dbg_hid("%s:timeout waiting for response\n", __func__);
180 memset(response, 0, sizeof(struct hidpp_report));
184 if (response->report_id == REPORT_ID_HIDPP_SHORT &&
185 response->fap.feature_index == HIDPP_ERROR) {
186 ret = response->fap.params[1];
187 dbg_hid("__hidpp_send_report got hidpp error %02X\n", ret);
192 mutex_unlock(&hidpp->send_mutex);
197 static int hidpp_send_fap_command_sync(struct hidpp_device *hidpp,
198 u8 feat_index, u8 funcindex_clientid, u8 *params, int param_count,
199 struct hidpp_report *response)
201 struct hidpp_report *message = kzalloc(sizeof(struct hidpp_report),
205 if (param_count > sizeof(message->fap.params))
208 message->report_id = REPORT_ID_HIDPP_LONG;
209 message->fap.feature_index = feat_index;
210 message->fap.funcindex_clientid = funcindex_clientid;
211 memcpy(&message->fap.params, params, param_count);
213 ret = hidpp_send_message_sync(hidpp, message, response);
218 static int hidpp_send_rap_command_sync(struct hidpp_device *hidpp_dev,
219 u8 report_id, u8 sub_id, u8 reg_address, u8 *params, int param_count,
220 struct hidpp_report *response)
222 struct hidpp_report *message = kzalloc(sizeof(struct hidpp_report),
226 if ((report_id != REPORT_ID_HIDPP_SHORT) &&
227 (report_id != REPORT_ID_HIDPP_LONG))
230 if (param_count > sizeof(message->rap.params))
233 message->report_id = report_id;
234 message->rap.sub_id = sub_id;
235 message->rap.reg_address = reg_address;
236 memcpy(&message->rap.params, params, param_count);
238 ret = hidpp_send_message_sync(hidpp_dev, message, response);
243 static void delayed_work_cb(struct work_struct *work)
245 struct hidpp_device *hidpp = container_of(work, struct hidpp_device,
247 hidpp_connect_event(hidpp);
250 static inline bool hidpp_match_answer(struct hidpp_report *question,
251 struct hidpp_report *answer)
253 return (answer->fap.feature_index == question->fap.feature_index) &&
254 (answer->fap.funcindex_clientid == question->fap.funcindex_clientid);
257 static inline bool hidpp_match_error(struct hidpp_report *question,
258 struct hidpp_report *answer)
260 return (answer->fap.feature_index == HIDPP_ERROR) &&
261 (answer->fap.funcindex_clientid == question->fap.feature_index) &&
262 (answer->fap.params[0] == question->fap.funcindex_clientid);
265 static inline bool hidpp_report_is_connect_event(struct hidpp_report *report)
267 return (report->report_id == REPORT_ID_HIDPP_SHORT) &&
268 (report->rap.sub_id == 0x41);
271 /* -------------------------------------------------------------------------- */
272 /* HIDP++ 1.0 commands */
273 /* -------------------------------------------------------------------------- */
275 #define HIDPP_SET_REGISTER 0x80
276 #define HIDPP_GET_REGISTER 0x81
277 #define HIDPP_SET_LONG_REGISTER 0x82
278 #define HIDPP_GET_LONG_REGISTER 0x83
280 #define HIDPP_REG_PAIRING_INFORMATION 0xB5
281 #define DEVICE_NAME 0x40
283 static char *hidpp_get_unifying_name(struct hidpp_device *hidpp_dev)
285 struct hidpp_report response;
287 /* hid-logitech-dj is in charge of setting the right device index */
288 u8 params[1] = { DEVICE_NAME };
292 ret = hidpp_send_rap_command_sync(hidpp_dev,
293 REPORT_ID_HIDPP_SHORT,
294 HIDPP_GET_LONG_REGISTER,
295 HIDPP_REG_PAIRING_INFORMATION,
296 params, 1, &response);
300 len = response.rap.params[1];
302 name = kzalloc(len + 1, GFP_KERNEL);
306 memcpy(name, &response.rap.params[2], len);
310 /* -------------------------------------------------------------------------- */
312 /* -------------------------------------------------------------------------- */
314 #define HIDPP_PAGE_ROOT 0x0000
315 #define HIDPP_PAGE_ROOT_IDX 0x00
317 #define CMD_ROOT_GET_FEATURE 0x01
318 #define CMD_ROOT_GET_PROTOCOL_VERSION 0x11
320 static int hidpp_root_get_feature(struct hidpp_device *hidpp, u16 feature,
321 u8 *feature_index, u8 *feature_type)
323 struct hidpp_report response;
325 u8 params[2] = { feature >> 8, feature & 0x00FF };
327 ret = hidpp_send_fap_command_sync(hidpp,
329 CMD_ROOT_GET_FEATURE,
330 params, 2, &response);
334 *feature_index = response.fap.params[0];
335 *feature_type = response.fap.params[1];
340 static int hidpp_root_get_protocol_version(struct hidpp_device *hidpp)
342 struct hidpp_report response;
345 ret = hidpp_send_fap_command_sync(hidpp,
347 CMD_ROOT_GET_PROTOCOL_VERSION,
351 hidpp->protocol_major = 1;
352 hidpp->protocol_minor = 0;
359 hidpp->protocol_major = response.fap.params[0];
360 hidpp->protocol_minor = response.fap.params[1];
365 static bool hidpp_is_connected(struct hidpp_device *hidpp)
369 ret = hidpp_root_get_protocol_version(hidpp);
371 hid_dbg(hidpp->hid_dev, "HID++ %u.%u device connected.\n",
372 hidpp->protocol_major, hidpp->protocol_minor);
376 /* -------------------------------------------------------------------------- */
377 /* 0x0005: GetDeviceNameType */
378 /* -------------------------------------------------------------------------- */
380 #define HIDPP_PAGE_GET_DEVICE_NAME_TYPE 0x0005
382 #define CMD_GET_DEVICE_NAME_TYPE_GET_COUNT 0x01
383 #define CMD_GET_DEVICE_NAME_TYPE_GET_DEVICE_NAME 0x11
384 #define CMD_GET_DEVICE_NAME_TYPE_GET_TYPE 0x21
386 static int hidpp_devicenametype_get_count(struct hidpp_device *hidpp,
387 u8 feature_index, u8 *nameLength)
389 struct hidpp_report response;
392 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
393 CMD_GET_DEVICE_NAME_TYPE_GET_COUNT, NULL, 0, &response);
398 *nameLength = response.fap.params[0];
403 static int hidpp_devicenametype_get_device_name(struct hidpp_device *hidpp,
404 u8 feature_index, u8 char_index, char *device_name, int len_buf)
406 struct hidpp_report response;
410 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
411 CMD_GET_DEVICE_NAME_TYPE_GET_DEVICE_NAME, &char_index, 1,
417 if (response.report_id == REPORT_ID_HIDPP_LONG)
418 count = HIDPP_REPORT_LONG_LENGTH - 4;
420 count = HIDPP_REPORT_SHORT_LENGTH - 4;
425 for (i = 0; i < count; i++)
426 device_name[i] = response.fap.params[i];
431 static char *hidpp_get_device_name(struct hidpp_device *hidpp, u8 *name_length)
440 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_GET_DEVICE_NAME_TYPE,
441 &feature_index, &feature_type);
445 ret = hidpp_devicenametype_get_count(hidpp, feature_index,
450 name = kzalloc(__name_length + 1, GFP_KERNEL);
454 *name_length = __name_length + 1;
455 while (index < __name_length)
456 index += hidpp_devicenametype_get_device_name(hidpp,
457 feature_index, index, name + index,
458 __name_length - index);
467 /* -------------------------------------------------------------------------- */
468 /* 0x6100: TouchPadRawXY */
469 /* -------------------------------------------------------------------------- */
471 #define HIDPP_PAGE_TOUCHPAD_RAW_XY 0x6100
473 #define CMD_TOUCHPAD_GET_RAW_INFO 0x01
475 #define TOUCHPAD_RAW_XY_ORIGIN_LOWER_LEFT 0x01
476 #define TOUCHPAD_RAW_XY_ORIGIN_UPPER_LEFT 0x03
478 struct hidpp_touchpad_raw_info {
489 struct hidpp_touchpad_raw_xy_finger {
499 struct hidpp_touchpad_raw_xy {
501 struct hidpp_touchpad_raw_xy_finger fingers[2];
508 static int hidpp_touchpad_get_raw_info(struct hidpp_device *hidpp,
509 u8 feature_index, struct hidpp_touchpad_raw_info *raw_info)
511 struct hidpp_report response;
513 u8 *params = (u8 *)response.fap.params;
515 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
516 CMD_TOUCHPAD_GET_RAW_INFO, NULL, 0, &response);
521 raw_info->x_size = get_unaligned_be16(¶ms[0]);
522 raw_info->y_size = get_unaligned_be16(¶ms[2]);
523 raw_info->z_range = params[4];
524 raw_info->area_range = params[5];
525 raw_info->maxcontacts = params[7];
526 raw_info->origin = params[8];
527 /* res is given in unit per inch */
528 raw_info->res = get_unaligned_be16(¶ms[13]) * 2 / 51;
533 /* ************************************************************************** */
537 /* ************************************************************************** */
539 /* -------------------------------------------------------------------------- */
540 /* Touchpad HID++ devices */
541 /* -------------------------------------------------------------------------- */
544 struct input_dev *input;
548 u8 button_feature_index;
551 unsigned int resolution;
554 static int wtp_input_mapping(struct hid_device *hdev, struct hid_input *hi,
555 struct hid_field *field, struct hid_usage *usage,
556 unsigned long **bit, int *max)
561 static void wtp_populate_input(struct hidpp_device *hidpp,
562 struct input_dev *input_dev, bool origin_is_hid_core)
564 struct wtp_data *wd = hidpp->private_data;
566 __set_bit(EV_ABS, input_dev->evbit);
567 __set_bit(EV_KEY, input_dev->evbit);
568 __clear_bit(EV_REL, input_dev->evbit);
569 __clear_bit(EV_LED, input_dev->evbit);
571 input_set_abs_params(input_dev, ABS_MT_POSITION_X, 0, wd->x_size, 0, 0);
572 input_abs_set_res(input_dev, ABS_MT_POSITION_X, wd->resolution);
573 input_set_abs_params(input_dev, ABS_MT_POSITION_Y, 0, wd->y_size, 0, 0);
574 input_abs_set_res(input_dev, ABS_MT_POSITION_Y, wd->resolution);
576 /* Max pressure is not given by the devices, pick one */
577 input_set_abs_params(input_dev, ABS_MT_PRESSURE, 0, 50, 0, 0);
579 input_set_capability(input_dev, EV_KEY, BTN_LEFT);
581 __set_bit(INPUT_PROP_BUTTONPAD, input_dev->propbit);
583 input_mt_init_slots(input_dev, wd->maxcontacts, INPUT_MT_POINTER |
584 INPUT_MT_DROP_UNUSED);
586 wd->input = input_dev;
589 static void wtp_touch_event(struct wtp_data *wd,
590 struct hidpp_touchpad_raw_xy_finger *touch_report)
594 if (!touch_report->finger_id || touch_report->contact_type)
598 slot = input_mt_get_slot_by_key(wd->input, touch_report->finger_id);
600 input_mt_slot(wd->input, slot);
601 input_mt_report_slot_state(wd->input, MT_TOOL_FINGER,
602 touch_report->contact_status);
603 if (touch_report->contact_status) {
604 input_event(wd->input, EV_ABS, ABS_MT_POSITION_X,
606 input_event(wd->input, EV_ABS, ABS_MT_POSITION_Y,
607 wd->flip_y ? wd->y_size - touch_report->y :
609 input_event(wd->input, EV_ABS, ABS_MT_PRESSURE,
614 static void wtp_send_raw_xy_event(struct hidpp_device *hidpp,
615 struct hidpp_touchpad_raw_xy *raw)
617 struct wtp_data *wd = hidpp->private_data;
620 for (i = 0; i < 2; i++)
621 wtp_touch_event(wd, &(raw->fingers[i]));
623 if (raw->end_of_frame)
624 input_event(wd->input, EV_KEY, BTN_LEFT, raw->button);
626 if (raw->end_of_frame || raw->finger_count <= 2) {
627 input_mt_sync_frame(wd->input);
628 input_sync(wd->input);
632 static int wtp_mouse_raw_xy_event(struct hidpp_device *hidpp, u8 *data)
634 struct wtp_data *wd = hidpp->private_data;
635 u8 c1_area = ((data[7] & 0xf) * (data[7] & 0xf) +
636 (data[7] >> 4) * (data[7] >> 4)) / 2;
637 u8 c2_area = ((data[13] & 0xf) * (data[13] & 0xf) +
638 (data[13] >> 4) * (data[13] >> 4)) / 2;
639 struct hidpp_touchpad_raw_xy raw = {
640 .timestamp = data[1],
644 .contact_status = !!data[7],
645 .x = get_unaligned_le16(&data[3]),
646 .y = get_unaligned_le16(&data[5]),
649 .finger_id = data[2],
652 .contact_status = !!data[13],
653 .x = get_unaligned_le16(&data[9]),
654 .y = get_unaligned_le16(&data[11]),
657 .finger_id = data[8],
660 .finger_count = wd->maxcontacts,
662 .end_of_frame = (data[0] >> 7) == 0,
663 .button = data[0] & 0x01,
666 wtp_send_raw_xy_event(hidpp, &raw);
671 static int wtp_raw_event(struct hid_device *hdev, u8 *data, int size)
673 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
674 struct wtp_data *wd = hidpp->private_data;
676 if (!wd || !wd->input || (data[0] != 0x02) || size < 21)
679 return wtp_mouse_raw_xy_event(hidpp, &data[7]);
682 static int wtp_get_config(struct hidpp_device *hidpp)
684 struct wtp_data *wd = hidpp->private_data;
685 struct hidpp_touchpad_raw_info raw_info = {0};
689 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_TOUCHPAD_RAW_XY,
690 &wd->mt_feature_index, &feature_type);
692 /* means that the device is not powered up */
695 ret = hidpp_touchpad_get_raw_info(hidpp, wd->mt_feature_index,
700 wd->x_size = raw_info.x_size;
701 wd->y_size = raw_info.y_size;
702 wd->maxcontacts = raw_info.maxcontacts;
703 wd->flip_y = raw_info.origin == TOUCHPAD_RAW_XY_ORIGIN_LOWER_LEFT;
704 wd->resolution = raw_info.res;
709 static int wtp_allocate(struct hid_device *hdev, const struct hid_device_id *id)
711 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
714 wd = devm_kzalloc(&hdev->dev, sizeof(struct wtp_data),
719 hidpp->private_data = wd;
724 /* -------------------------------------------------------------------------- */
725 /* Generic HID++ devices */
726 /* -------------------------------------------------------------------------- */
728 static int hidpp_input_mapping(struct hid_device *hdev, struct hid_input *hi,
729 struct hid_field *field, struct hid_usage *usage,
730 unsigned long **bit, int *max)
732 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
734 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
735 return wtp_input_mapping(hdev, hi, field, usage, bit, max);
740 static void hidpp_populate_input(struct hidpp_device *hidpp,
741 struct input_dev *input, bool origin_is_hid_core)
743 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
744 wtp_populate_input(hidpp, input, origin_is_hid_core);
747 static void hidpp_input_configured(struct hid_device *hdev,
748 struct hid_input *hidinput)
750 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
751 struct input_dev *input = hidinput->input;
753 hidpp_populate_input(hidpp, input, true);
756 static int hidpp_raw_hidpp_event(struct hidpp_device *hidpp, u8 *data,
759 struct hidpp_report *question = hidpp->send_receive_buf;
760 struct hidpp_report *answer = hidpp->send_receive_buf;
761 struct hidpp_report *report = (struct hidpp_report *)data;
764 * If the mutex is locked then we have a pending answer from a
765 * previoulsly sent command
767 if (unlikely(mutex_is_locked(&hidpp->send_mutex))) {
769 * Check for a correct hidpp20 answer or the corresponding
772 if (hidpp_match_answer(question, report) ||
773 hidpp_match_error(question, report)) {
775 hidpp->answer_available = true;
776 wake_up(&hidpp->wait);
778 * This was an answer to a command that this driver sent
779 * We return 1 to hid-core to avoid forwarding the
780 * command upstream as it has been treated by the driver
787 if (unlikely(hidpp_report_is_connect_event(report))) {
788 atomic_set(&hidpp->connected,
789 !(report->rap.params[0] & (1 << 6)));
790 if ((hidpp->quirks & HIDPP_QUIRK_DELAYED_INIT) &&
791 (schedule_work(&hidpp->work) == 0))
792 dbg_hid("%s: connect event already queued\n", __func__);
796 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
797 return wtp_raw_event(hidpp->hid_dev, data, size);
802 static int hidpp_raw_event(struct hid_device *hdev, struct hid_report *report,
805 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
808 case REPORT_ID_HIDPP_LONG:
809 if (size != HIDPP_REPORT_LONG_LENGTH) {
810 hid_err(hdev, "received hid++ report of bad size (%d)",
814 return hidpp_raw_hidpp_event(hidpp, data, size);
815 case REPORT_ID_HIDPP_SHORT:
816 if (size != HIDPP_REPORT_SHORT_LENGTH) {
817 hid_err(hdev, "received hid++ report of bad size (%d)",
821 return hidpp_raw_hidpp_event(hidpp, data, size);
824 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
825 return wtp_raw_event(hdev, data, size);
830 static void hidpp_overwrite_name(struct hid_device *hdev, bool use_unifying)
832 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
838 * the device is connected through an Unifying receiver, and
839 * might not be already connected.
840 * Ask the receiver for its name.
842 name = hidpp_get_unifying_name(hidpp);
844 name = hidpp_get_device_name(hidpp, &name_length);
847 hid_err(hdev, "unable to retrieve the name of the device");
849 snprintf(hdev->name, sizeof(hdev->name), "%s", name);
854 static int hidpp_input_open(struct input_dev *dev)
856 struct hid_device *hid = input_get_drvdata(dev);
858 return hid_hw_open(hid);
861 static void hidpp_input_close(struct input_dev *dev)
863 struct hid_device *hid = input_get_drvdata(dev);
868 static struct input_dev *hidpp_allocate_input(struct hid_device *hdev)
870 struct input_dev *input_dev = devm_input_allocate_device(&hdev->dev);
875 input_set_drvdata(input_dev, hdev);
876 input_dev->open = hidpp_input_open;
877 input_dev->close = hidpp_input_close;
879 input_dev->name = hdev->name;
880 input_dev->phys = hdev->phys;
881 input_dev->uniq = hdev->uniq;
882 input_dev->id.bustype = hdev->bus;
883 input_dev->id.vendor = hdev->vendor;
884 input_dev->id.product = hdev->product;
885 input_dev->id.version = hdev->version;
886 input_dev->dev.parent = &hdev->dev;
891 static void hidpp_connect_event(struct hidpp_device *hidpp)
893 struct hid_device *hdev = hidpp->hid_dev;
895 bool connected = atomic_read(&hidpp->connected);
896 struct input_dev *input;
897 char *name, *devm_name;
900 if (!connected || hidpp->delayed_input)
903 if (!hidpp->protocol_major) {
904 ret = !hidpp_is_connected(hidpp);
906 hid_err(hdev, "Can not get the protocol version.\n");
911 /* the device is already connected, we can ask for its name and
913 hid_info(hdev, "HID++ %u.%u device connected.\n",
914 hidpp->protocol_major, hidpp->protocol_minor);
916 input = hidpp_allocate_input(hdev);
918 hid_err(hdev, "cannot allocate new input device: %d\n", ret);
922 name = hidpp_get_device_name(hidpp, &name_length);
924 hid_err(hdev, "unable to retrieve the name of the device");
926 devm_name = devm_kasprintf(&hdev->dev, GFP_KERNEL, "%s", name);
928 input->name = devm_name;
932 hidpp_populate_input(hidpp, input, false);
934 ret = input_register_device(input);
936 input_free_device(input);
938 hidpp->delayed_input = input;
941 static int hidpp_probe(struct hid_device *hdev, const struct hid_device_id *id)
943 struct hidpp_device *hidpp;
946 unsigned int connect_mask = HID_CONNECT_DEFAULT;
948 hidpp = devm_kzalloc(&hdev->dev, sizeof(struct hidpp_device),
953 hidpp->hid_dev = hdev;
954 hid_set_drvdata(hdev, hidpp);
956 hidpp->quirks = id->driver_data;
958 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP) {
959 ret = wtp_allocate(hdev, id);
964 INIT_WORK(&hidpp->work, delayed_work_cb);
965 mutex_init(&hidpp->send_mutex);
966 init_waitqueue_head(&hidpp->wait);
968 ret = hid_parse(hdev);
970 hid_err(hdev, "%s:parse failed\n", __func__);
974 /* Allow incoming packets */
975 hid_device_io_start(hdev);
977 connected = hidpp_is_connected(hidpp);
978 if (id->group != HID_GROUP_LOGITECH_DJ_DEVICE) {
980 hid_err(hdev, "Device not connected");
984 hid_info(hdev, "HID++ %u.%u device connected.\n",
985 hidpp->protocol_major, hidpp->protocol_minor);
988 hidpp_overwrite_name(hdev, id->group == HID_GROUP_LOGITECH_DJ_DEVICE);
989 atomic_set(&hidpp->connected, connected);
991 if (connected && (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)) {
992 ret = wtp_get_config(hidpp);
997 /* Block incoming packets */
998 hid_device_io_stop(hdev);
1000 if (hidpp->quirks & HIDPP_QUIRK_DELAYED_INIT)
1001 connect_mask &= ~HID_CONNECT_HIDINPUT;
1003 ret = hid_hw_start(hdev, connect_mask);
1005 hid_err(hdev, "%s:hid_hw_start returned error\n", __func__);
1006 goto hid_hw_start_fail;
1009 if (hidpp->quirks & HIDPP_QUIRK_DELAYED_INIT) {
1010 /* Allow incoming packets */
1011 hid_device_io_start(hdev);
1013 hidpp_connect_event(hidpp);
1020 cancel_work_sync(&hidpp->work);
1021 mutex_destroy(&hidpp->send_mutex);
1022 hid_set_drvdata(hdev, NULL);
1026 static void hidpp_remove(struct hid_device *hdev)
1028 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
1030 cancel_work_sync(&hidpp->work);
1031 mutex_destroy(&hidpp->send_mutex);
1035 static const struct hid_device_id hidpp_devices[] = {
1036 { /* wireless touchpad T651 */
1037 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH,
1038 USB_DEVICE_ID_LOGITECH_T651),
1039 .driver_data = HIDPP_QUIRK_CLASS_WTP },
1041 { HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE,
1042 USB_VENDOR_ID_LOGITECH, HID_ANY_ID)},
1046 MODULE_DEVICE_TABLE(hid, hidpp_devices);
1048 static struct hid_driver hidpp_driver = {
1049 .name = "logitech-hidpp-device",
1050 .id_table = hidpp_devices,
1051 .probe = hidpp_probe,
1052 .remove = hidpp_remove,
1053 .raw_event = hidpp_raw_event,
1054 .input_configured = hidpp_input_configured,
1055 .input_mapping = hidpp_input_mapping,
1058 module_hid_driver(hidpp_driver);