HID: logitech-hidpp: late bind the input device on wireless connection
[cascardo/linux.git] / drivers / hid / hid-logitech-hidpp.c
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
27 MODULE_LICENSE("GPL");
28 MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
29 MODULE_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 /* bits 1..20 are reserved for classes */
40 #define HIDPP_QUIRK_DELAYED_INIT                BIT(21)
41
42 /*
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
46  *
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)
53  *
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
58  * Unifying device.
59  *
60  * The paired device can be RAP or FAP, it will receive the message untouched
61  * from the Unifiying receiver.
62  */
63
64 struct fap {
65         u8 feature_index;
66         u8 funcindex_clientid;
67         u8 params[HIDPP_REPORT_LONG_LENGTH - 4U];
68 };
69
70 struct rap {
71         u8 sub_id;
72         u8 reg_address;
73         u8 params[HIDPP_REPORT_LONG_LENGTH - 4U];
74 };
75
76 struct hidpp_report {
77         u8 report_id;
78         u8 device_index;
79         union {
80                 struct fap fap;
81                 struct rap rap;
82                 u8 rawbytes[sizeof(struct fap)];
83         };
84 } __packed;
85
86 struct hidpp_device {
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;
92         u8 protocol_major;
93         u8 protocol_minor;
94
95         void *private_data;
96
97         struct work_struct work;
98         struct kfifo delayed_work_fifo;
99         atomic_t connected;
100         struct input_dev *delayed_input;
101
102         unsigned long quirks;
103 };
104
105
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
120
121 static void hidpp_connect_event(struct hidpp_device *hidpp_dev);
122
123 static int __hidpp_send_report(struct hid_device *hdev,
124                                 struct hidpp_report *hidpp_report)
125 {
126         int fields_count, ret;
127
128         switch (hidpp_report->report_id) {
129         case REPORT_ID_HIDPP_SHORT:
130                 fields_count = HIDPP_REPORT_SHORT_LENGTH;
131                 break;
132         case REPORT_ID_HIDPP_LONG:
133                 fields_count = HIDPP_REPORT_LONG_LENGTH;
134                 break;
135         default:
136                 return -ENODEV;
137         }
138
139         /*
140          * set the device_index as the receiver, it will be overwritten by
141          * hid_hw_request if needed
142          */
143         hidpp_report->device_index = 0xff;
144
145         ret = hid_hw_raw_request(hdev, hidpp_report->report_id,
146                 (u8 *)hidpp_report, fields_count, HID_OUTPUT_REPORT,
147                 HID_REQ_SET_REPORT);
148
149         return ret == fields_count ? 0 : -1;
150 }
151
152 static int hidpp_send_message_sync(struct hidpp_device *hidpp,
153         struct hidpp_report *message,
154         struct hidpp_report *response)
155 {
156         int ret;
157
158         mutex_lock(&hidpp->send_mutex);
159
160         hidpp->send_receive_buf = response;
161         hidpp->answer_available = false;
162
163         /*
164          * So that we can later validate the answer when it arrives
165          * in hidpp_raw_event
166          */
167         *response = *message;
168
169         ret = __hidpp_send_report(hidpp->hid_dev, message);
170
171         if (ret) {
172                 dbg_hid("__hidpp_send_report returned err: %d\n", ret);
173                 memset(response, 0, sizeof(struct hidpp_report));
174                 goto exit;
175         }
176
177         if (!wait_event_timeout(hidpp->wait, hidpp->answer_available,
178                                 5*HZ)) {
179                 dbg_hid("%s:timeout waiting for response\n", __func__);
180                 memset(response, 0, sizeof(struct hidpp_report));
181                 ret = -ETIMEDOUT;
182         }
183
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);
188                 goto exit;
189         }
190
191 exit:
192         mutex_unlock(&hidpp->send_mutex);
193         return ret;
194
195 }
196
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)
200 {
201         struct hidpp_report *message = kzalloc(sizeof(struct hidpp_report),
202                         GFP_KERNEL);
203         int ret;
204
205         if (param_count > sizeof(message->fap.params))
206                 return -EINVAL;
207
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);
212
213         ret = hidpp_send_message_sync(hidpp, message, response);
214         kfree(message);
215         return ret;
216 }
217
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)
221 {
222         struct hidpp_report *message = kzalloc(sizeof(struct hidpp_report),
223                         GFP_KERNEL);
224         int ret;
225
226         if ((report_id != REPORT_ID_HIDPP_SHORT) &&
227             (report_id != REPORT_ID_HIDPP_LONG))
228                 return -EINVAL;
229
230         if (param_count > sizeof(message->rap.params))
231                 return -EINVAL;
232
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);
237
238         ret = hidpp_send_message_sync(hidpp_dev, message, response);
239         kfree(message);
240         return ret;
241 }
242
243 static void delayed_work_cb(struct work_struct *work)
244 {
245         struct hidpp_device *hidpp = container_of(work, struct hidpp_device,
246                                                         work);
247         hidpp_connect_event(hidpp);
248 }
249
250 static inline bool hidpp_match_answer(struct hidpp_report *question,
251                 struct hidpp_report *answer)
252 {
253         return (answer->fap.feature_index == question->fap.feature_index) &&
254            (answer->fap.funcindex_clientid == question->fap.funcindex_clientid);
255 }
256
257 static inline bool hidpp_match_error(struct hidpp_report *question,
258                 struct hidpp_report *answer)
259 {
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);
263 }
264
265 static inline bool hidpp_report_is_connect_event(struct hidpp_report *report)
266 {
267         return (report->report_id == REPORT_ID_HIDPP_SHORT) &&
268                 (report->rap.sub_id == 0x41);
269 }
270
271 /* -------------------------------------------------------------------------- */
272 /* HIDP++ 1.0 commands                                                        */
273 /* -------------------------------------------------------------------------- */
274
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
279
280 #define HIDPP_REG_PAIRING_INFORMATION                   0xB5
281 #define DEVICE_NAME                                     0x40
282
283 static char *hidpp_get_unifying_name(struct hidpp_device *hidpp_dev)
284 {
285         struct hidpp_report response;
286         int ret;
287         /* hid-logitech-dj is in charge of setting the right device index */
288         u8 params[1] = { DEVICE_NAME };
289         char *name;
290         int len;
291
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);
297         if (ret)
298                 return NULL;
299
300         len = response.rap.params[1];
301
302         name = kzalloc(len + 1, GFP_KERNEL);
303         if (!name)
304                 return NULL;
305
306         memcpy(name, &response.rap.params[2], len);
307         return name;
308 }
309
310 /* -------------------------------------------------------------------------- */
311 /* 0x0000: Root                                                               */
312 /* -------------------------------------------------------------------------- */
313
314 #define HIDPP_PAGE_ROOT                                 0x0000
315 #define HIDPP_PAGE_ROOT_IDX                             0x00
316
317 #define CMD_ROOT_GET_FEATURE                            0x01
318 #define CMD_ROOT_GET_PROTOCOL_VERSION                   0x11
319
320 static int hidpp_root_get_feature(struct hidpp_device *hidpp, u16 feature,
321         u8 *feature_index, u8 *feature_type)
322 {
323         struct hidpp_report response;
324         int ret;
325         u8 params[2] = { feature >> 8, feature & 0x00FF };
326
327         ret = hidpp_send_fap_command_sync(hidpp,
328                         HIDPP_PAGE_ROOT_IDX,
329                         CMD_ROOT_GET_FEATURE,
330                         params, 2, &response);
331         if (ret)
332                 return ret;
333
334         *feature_index = response.fap.params[0];
335         *feature_type = response.fap.params[1];
336
337         return ret;
338 }
339
340 static int hidpp_root_get_protocol_version(struct hidpp_device *hidpp)
341 {
342         struct hidpp_report response;
343         int ret;
344
345         ret = hidpp_send_fap_command_sync(hidpp,
346                         HIDPP_PAGE_ROOT_IDX,
347                         CMD_ROOT_GET_PROTOCOL_VERSION,
348                         NULL, 0, &response);
349
350         if (ret == 1) {
351                 hidpp->protocol_major = 1;
352                 hidpp->protocol_minor = 0;
353                 return 0;
354         }
355
356         if (ret)
357                 return -ret;
358
359         hidpp->protocol_major = response.fap.params[0];
360         hidpp->protocol_minor = response.fap.params[1];
361
362         return ret;
363 }
364
365 static bool hidpp_is_connected(struct hidpp_device *hidpp)
366 {
367         int ret;
368
369         ret = hidpp_root_get_protocol_version(hidpp);
370         if (!ret)
371                 hid_dbg(hidpp->hid_dev, "HID++ %u.%u device connected.\n",
372                         hidpp->protocol_major, hidpp->protocol_minor);
373         return ret == 0;
374 }
375
376 /* -------------------------------------------------------------------------- */
377 /* 0x0005: GetDeviceNameType                                                  */
378 /* -------------------------------------------------------------------------- */
379
380 #define HIDPP_PAGE_GET_DEVICE_NAME_TYPE                 0x0005
381
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
385
386 static int hidpp_devicenametype_get_count(struct hidpp_device *hidpp,
387         u8 feature_index, u8 *nameLength)
388 {
389         struct hidpp_report response;
390         int ret;
391
392         ret = hidpp_send_fap_command_sync(hidpp, feature_index,
393                 CMD_GET_DEVICE_NAME_TYPE_GET_COUNT, NULL, 0, &response);
394
395         if (ret)
396                 return -ret;
397
398         *nameLength = response.fap.params[0];
399
400         return ret;
401 }
402
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)
405 {
406         struct hidpp_report response;
407         int ret, i;
408         int count;
409
410         ret = hidpp_send_fap_command_sync(hidpp, feature_index,
411                 CMD_GET_DEVICE_NAME_TYPE_GET_DEVICE_NAME, &char_index, 1,
412                 &response);
413
414         if (ret)
415                 return -ret;
416
417         if (response.report_id == REPORT_ID_HIDPP_LONG)
418                 count = HIDPP_REPORT_LONG_LENGTH - 4;
419         else
420                 count = HIDPP_REPORT_SHORT_LENGTH - 4;
421
422         if (len_buf < count)
423                 count = len_buf;
424
425         for (i = 0; i < count; i++)
426                 device_name[i] = response.fap.params[i];
427
428         return count;
429 }
430
431 static char *hidpp_get_device_name(struct hidpp_device *hidpp, u8 *name_length)
432 {
433         u8 feature_type;
434         u8 feature_index;
435         u8 __name_length;
436         char *name;
437         unsigned index = 0;
438         int ret;
439
440         ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_GET_DEVICE_NAME_TYPE,
441                 &feature_index, &feature_type);
442         if (ret)
443                 goto out_err;
444
445         ret = hidpp_devicenametype_get_count(hidpp, feature_index,
446                 &__name_length);
447         if (ret)
448                 goto out_err;
449
450         name = kzalloc(__name_length + 1, GFP_KERNEL);
451         if (!name)
452                 goto out_err;
453
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);
459
460         return name;
461
462 out_err:
463         *name_length = 0;
464         return NULL;
465 }
466
467 /* -------------------------------------------------------------------------- */
468 /* 0x6100: TouchPadRawXY                                                      */
469 /* -------------------------------------------------------------------------- */
470
471 #define HIDPP_PAGE_TOUCHPAD_RAW_XY                      0x6100
472
473 #define CMD_TOUCHPAD_GET_RAW_INFO                       0x01
474
475 #define TOUCHPAD_RAW_XY_ORIGIN_LOWER_LEFT               0x01
476 #define TOUCHPAD_RAW_XY_ORIGIN_UPPER_LEFT               0x03
477
478 struct hidpp_touchpad_raw_info {
479         u16 x_size;
480         u16 y_size;
481         u8 z_range;
482         u8 area_range;
483         u8 timestamp_unit;
484         u8 maxcontacts;
485         u8 origin;
486         u16 res;
487 };
488
489 struct hidpp_touchpad_raw_xy_finger {
490         u8 contact_type;
491         u8 contact_status;
492         u16 x;
493         u16 y;
494         u8 z;
495         u8 area;
496         u8 finger_id;
497 };
498
499 struct hidpp_touchpad_raw_xy {
500         u16 timestamp;
501         struct hidpp_touchpad_raw_xy_finger fingers[2];
502         u8 spurious_flag;
503         u8 end_of_frame;
504         u8 finger_count;
505         u8 button;
506 };
507
508 static int hidpp_touchpad_get_raw_info(struct hidpp_device *hidpp,
509         u8 feature_index, struct hidpp_touchpad_raw_info *raw_info)
510 {
511         struct hidpp_report response;
512         int ret;
513         u8 *params = (u8 *)response.fap.params;
514
515         ret = hidpp_send_fap_command_sync(hidpp, feature_index,
516                 CMD_TOUCHPAD_GET_RAW_INFO, NULL, 0, &response);
517
518         if (ret)
519                 return -ret;
520
521         raw_info->x_size = get_unaligned_be16(&params[0]);
522         raw_info->y_size = get_unaligned_be16(&params[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(&params[13]) * 2 / 51;
529
530         return ret;
531 }
532
533 /* ************************************************************************** */
534 /*                                                                            */
535 /* Device Support                                                             */
536 /*                                                                            */
537 /* ************************************************************************** */
538
539 /* -------------------------------------------------------------------------- */
540 /* Touchpad HID++ devices                                                     */
541 /* -------------------------------------------------------------------------- */
542
543 struct wtp_data {
544         struct input_dev *input;
545         u16 x_size, y_size;
546         u8 finger_count;
547         u8 mt_feature_index;
548         u8 button_feature_index;
549         u8 maxcontacts;
550         bool flip_y;
551         unsigned int resolution;
552 };
553
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)
557 {
558         return -1;
559 }
560
561 static void wtp_populate_input(struct hidpp_device *hidpp,
562                 struct input_dev *input_dev, bool origin_is_hid_core)
563 {
564         struct wtp_data *wd = hidpp->private_data;
565
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);
570
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);
575
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);
578
579         input_set_capability(input_dev, EV_KEY, BTN_LEFT);
580
581         __set_bit(INPUT_PROP_BUTTONPAD, input_dev->propbit);
582
583         input_mt_init_slots(input_dev, wd->maxcontacts, INPUT_MT_POINTER |
584                 INPUT_MT_DROP_UNUSED);
585
586         wd->input = input_dev;
587 }
588
589 static void wtp_touch_event(struct wtp_data *wd,
590         struct hidpp_touchpad_raw_xy_finger *touch_report)
591 {
592         int slot;
593
594         if (!touch_report->finger_id || touch_report->contact_type)
595                 /* no actual data */
596                 return;
597
598         slot = input_mt_get_slot_by_key(wd->input, touch_report->finger_id);
599
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,
605                                 touch_report->x);
606                 input_event(wd->input, EV_ABS, ABS_MT_POSITION_Y,
607                                 wd->flip_y ? wd->y_size - touch_report->y :
608                                              touch_report->y);
609                 input_event(wd->input, EV_ABS, ABS_MT_PRESSURE,
610                                 touch_report->area);
611         }
612 }
613
614 static void wtp_send_raw_xy_event(struct hidpp_device *hidpp,
615                 struct hidpp_touchpad_raw_xy *raw)
616 {
617         struct wtp_data *wd = hidpp->private_data;
618         int i;
619
620         for (i = 0; i < 2; i++)
621                 wtp_touch_event(wd, &(raw->fingers[i]));
622
623         if (raw->end_of_frame)
624                 input_event(wd->input, EV_KEY, BTN_LEFT, raw->button);
625
626         if (raw->end_of_frame || raw->finger_count <= 2) {
627                 input_mt_sync_frame(wd->input);
628                 input_sync(wd->input);
629         }
630 }
631
632 static int wtp_mouse_raw_xy_event(struct hidpp_device *hidpp, u8 *data)
633 {
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],
641                 .fingers = {
642                         {
643                                 .contact_type = 0,
644                                 .contact_status = !!data[7],
645                                 .x = get_unaligned_le16(&data[3]),
646                                 .y = get_unaligned_le16(&data[5]),
647                                 .z = c1_area,
648                                 .area = c1_area,
649                                 .finger_id = data[2],
650                         }, {
651                                 .contact_type = 0,
652                                 .contact_status = !!data[13],
653                                 .x = get_unaligned_le16(&data[9]),
654                                 .y = get_unaligned_le16(&data[11]),
655                                 .z = c2_area,
656                                 .area = c2_area,
657                                 .finger_id = data[8],
658                         }
659                 },
660                 .finger_count = wd->maxcontacts,
661                 .spurious_flag = 0,
662                 .end_of_frame = (data[0] >> 7) == 0,
663                 .button = data[0] & 0x01,
664         };
665
666         wtp_send_raw_xy_event(hidpp, &raw);
667
668         return 1;
669 }
670
671 static int wtp_raw_event(struct hid_device *hdev, u8 *data, int size)
672 {
673         struct hidpp_device *hidpp = hid_get_drvdata(hdev);
674         struct wtp_data *wd = hidpp->private_data;
675
676         if (!wd || !wd->input || (data[0] != 0x02) || size < 21)
677                 return 1;
678
679         return wtp_mouse_raw_xy_event(hidpp, &data[7]);
680 }
681
682 static int wtp_get_config(struct hidpp_device *hidpp)
683 {
684         struct wtp_data *wd = hidpp->private_data;
685         struct hidpp_touchpad_raw_info raw_info = {0};
686         u8 feature_type;
687         int ret;
688
689         ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_TOUCHPAD_RAW_XY,
690                 &wd->mt_feature_index, &feature_type);
691         if (ret)
692                 /* means that the device is not powered up */
693                 return ret;
694
695         ret = hidpp_touchpad_get_raw_info(hidpp, wd->mt_feature_index,
696                 &raw_info);
697         if (ret)
698                 return ret;
699
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;
705
706         return 0;
707 }
708
709 static int wtp_allocate(struct hid_device *hdev, const struct hid_device_id *id)
710 {
711         struct hidpp_device *hidpp = hid_get_drvdata(hdev);
712         struct wtp_data *wd;
713
714         wd = devm_kzalloc(&hdev->dev, sizeof(struct wtp_data),
715                         GFP_KERNEL);
716         if (!wd)
717                 return -ENOMEM;
718
719         hidpp->private_data = wd;
720
721         return 0;
722 };
723
724 /* -------------------------------------------------------------------------- */
725 /* Generic HID++ devices                                                      */
726 /* -------------------------------------------------------------------------- */
727
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)
731 {
732         struct hidpp_device *hidpp = hid_get_drvdata(hdev);
733
734         if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
735                 return wtp_input_mapping(hdev, hi, field, usage, bit, max);
736
737         return 0;
738 }
739
740 static void hidpp_populate_input(struct hidpp_device *hidpp,
741                 struct input_dev *input, bool origin_is_hid_core)
742 {
743         if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
744                 wtp_populate_input(hidpp, input, origin_is_hid_core);
745 }
746
747 static void hidpp_input_configured(struct hid_device *hdev,
748                                 struct hid_input *hidinput)
749 {
750         struct hidpp_device *hidpp = hid_get_drvdata(hdev);
751         struct input_dev *input = hidinput->input;
752
753         hidpp_populate_input(hidpp, input, true);
754 }
755
756 static int hidpp_raw_hidpp_event(struct hidpp_device *hidpp, u8 *data,
757                 int size)
758 {
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;
762
763         /*
764          * If the mutex is locked then we have a pending answer from a
765          * previoulsly sent command
766          */
767         if (unlikely(mutex_is_locked(&hidpp->send_mutex))) {
768                 /*
769                  * Check for a correct hidpp20 answer or the corresponding
770                  * error
771                  */
772                 if (hidpp_match_answer(question, report) ||
773                                 hidpp_match_error(question, report)) {
774                         *answer = *report;
775                         hidpp->answer_available = true;
776                         wake_up(&hidpp->wait);
777                         /*
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
781                          */
782
783                         return 1;
784                 }
785         }
786
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__);
793                 return 1;
794         }
795
796         if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
797                 return wtp_raw_event(hidpp->hid_dev, data, size);
798
799         return 0;
800 }
801
802 static int hidpp_raw_event(struct hid_device *hdev, struct hid_report *report,
803                 u8 *data, int size)
804 {
805         struct hidpp_device *hidpp = hid_get_drvdata(hdev);
806
807         switch (data[0]) {
808         case REPORT_ID_HIDPP_LONG:
809                 if (size != HIDPP_REPORT_LONG_LENGTH) {
810                         hid_err(hdev, "received hid++ report of bad size (%d)",
811                                 size);
812                         return 1;
813                 }
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)",
818                                 size);
819                         return 1;
820                 }
821                 return hidpp_raw_hidpp_event(hidpp, data, size);
822         }
823
824         if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
825                 return wtp_raw_event(hdev, data, size);
826
827         return 0;
828 }
829
830 static void hidpp_overwrite_name(struct hid_device *hdev, bool use_unifying)
831 {
832         struct hidpp_device *hidpp = hid_get_drvdata(hdev);
833         char *name;
834         u8 name_length;
835
836         if (use_unifying)
837                 /*
838                  * the device is connected through an Unifying receiver, and
839                  * might not be already connected.
840                  * Ask the receiver for its name.
841                  */
842                 name = hidpp_get_unifying_name(hidpp);
843         else
844                 name = hidpp_get_device_name(hidpp, &name_length);
845
846         if (!name)
847                 hid_err(hdev, "unable to retrieve the name of the device");
848         else
849                 snprintf(hdev->name, sizeof(hdev->name), "%s", name);
850
851         kfree(name);
852 }
853
854 static int hidpp_input_open(struct input_dev *dev)
855 {
856         struct hid_device *hid = input_get_drvdata(dev);
857
858         return hid_hw_open(hid);
859 }
860
861 static void hidpp_input_close(struct input_dev *dev)
862 {
863         struct hid_device *hid = input_get_drvdata(dev);
864
865         hid_hw_close(hid);
866 }
867
868 static struct input_dev *hidpp_allocate_input(struct hid_device *hdev)
869 {
870         struct input_dev *input_dev = devm_input_allocate_device(&hdev->dev);
871
872         if (!input_dev)
873                 return NULL;
874
875         input_set_drvdata(input_dev, hdev);
876         input_dev->open = hidpp_input_open;
877         input_dev->close = hidpp_input_close;
878
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;
887
888         return input_dev;
889 }
890
891 static void hidpp_connect_event(struct hidpp_device *hidpp)
892 {
893         struct hid_device *hdev = hidpp->hid_dev;
894         int ret = 0;
895         bool connected = atomic_read(&hidpp->connected);
896         struct input_dev *input;
897         char *name, *devm_name;
898         u8 name_length;
899
900         if (!connected || hidpp->delayed_input)
901                 return;
902
903         if (!hidpp->protocol_major) {
904                 ret = !hidpp_is_connected(hidpp);
905                 if (ret) {
906                         hid_err(hdev, "Can not get the protocol version.\n");
907                         return;
908                 }
909         }
910
911         /* the device is already connected, we can ask for its name and
912          * protocol */
913         hid_info(hdev, "HID++ %u.%u device connected.\n",
914                  hidpp->protocol_major, hidpp->protocol_minor);
915
916         input = hidpp_allocate_input(hdev);
917         if (!input) {
918                 hid_err(hdev, "cannot allocate new input device: %d\n", ret);
919                 return;
920         }
921
922         name = hidpp_get_device_name(hidpp, &name_length);
923         if (!name) {
924                 hid_err(hdev, "unable to retrieve the name of the device");
925         } else {
926                 devm_name = devm_kasprintf(&hdev->dev, GFP_KERNEL, "%s", name);
927                 if (devm_name)
928                         input->name = devm_name;
929                 kfree(name);
930         }
931
932         hidpp_populate_input(hidpp, input, false);
933
934         ret = input_register_device(input);
935         if (ret)
936                 input_free_device(input);
937
938         hidpp->delayed_input = input;
939 }
940
941 static int hidpp_probe(struct hid_device *hdev, const struct hid_device_id *id)
942 {
943         struct hidpp_device *hidpp;
944         int ret;
945         bool connected;
946         unsigned int connect_mask = HID_CONNECT_DEFAULT;
947
948         hidpp = devm_kzalloc(&hdev->dev, sizeof(struct hidpp_device),
949                         GFP_KERNEL);
950         if (!hidpp)
951                 return -ENOMEM;
952
953         hidpp->hid_dev = hdev;
954         hid_set_drvdata(hdev, hidpp);
955
956         hidpp->quirks = id->driver_data;
957
958         if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP) {
959                 ret = wtp_allocate(hdev, id);
960                 if (ret)
961                         return ret;
962         }
963
964         INIT_WORK(&hidpp->work, delayed_work_cb);
965         mutex_init(&hidpp->send_mutex);
966         init_waitqueue_head(&hidpp->wait);
967
968         ret = hid_parse(hdev);
969         if (ret) {
970                 hid_err(hdev, "%s:parse failed\n", __func__);
971                 goto hid_parse_fail;
972         }
973
974         /* Allow incoming packets */
975         hid_device_io_start(hdev);
976
977         connected = hidpp_is_connected(hidpp);
978         if (id->group != HID_GROUP_LOGITECH_DJ_DEVICE) {
979                 if (!connected) {
980                         hid_err(hdev, "Device not connected");
981                         goto hid_parse_fail;
982                 }
983
984                 hid_info(hdev, "HID++ %u.%u device connected.\n",
985                          hidpp->protocol_major, hidpp->protocol_minor);
986         }
987
988         hidpp_overwrite_name(hdev, id->group == HID_GROUP_LOGITECH_DJ_DEVICE);
989         atomic_set(&hidpp->connected, connected);
990
991         if (connected && (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)) {
992                 ret = wtp_get_config(hidpp);
993                 if (ret)
994                         goto hid_parse_fail;
995         }
996
997         /* Block incoming packets */
998         hid_device_io_stop(hdev);
999
1000         if (hidpp->quirks & HIDPP_QUIRK_DELAYED_INIT)
1001                 connect_mask &= ~HID_CONNECT_HIDINPUT;
1002
1003         ret = hid_hw_start(hdev, connect_mask);
1004         if (ret) {
1005                 hid_err(hdev, "%s:hid_hw_start returned error\n", __func__);
1006                 goto hid_hw_start_fail;
1007         }
1008
1009         if (hidpp->quirks & HIDPP_QUIRK_DELAYED_INIT) {
1010                 /* Allow incoming packets */
1011                 hid_device_io_start(hdev);
1012
1013                 hidpp_connect_event(hidpp);
1014         }
1015
1016         return ret;
1017
1018 hid_hw_start_fail:
1019 hid_parse_fail:
1020         cancel_work_sync(&hidpp->work);
1021         mutex_destroy(&hidpp->send_mutex);
1022         hid_set_drvdata(hdev, NULL);
1023         return ret;
1024 }
1025
1026 static void hidpp_remove(struct hid_device *hdev)
1027 {
1028         struct hidpp_device *hidpp = hid_get_drvdata(hdev);
1029
1030         cancel_work_sync(&hidpp->work);
1031         mutex_destroy(&hidpp->send_mutex);
1032         hid_hw_stop(hdev);
1033 }
1034
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 },
1040
1041         { HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE,
1042                 USB_VENDOR_ID_LOGITECH, HID_ANY_ID)},
1043         {}
1044 };
1045
1046 MODULE_DEVICE_TABLE(hid, hidpp_devices);
1047
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,
1056 };
1057
1058 module_hid_driver(hidpp_driver);