blob: 904a4b0d90f509970045d416c5cb136355e996a9 [file] [log] [blame]
Thomas Gleixner45051532019-05-29 16:57:47 -07001// SPDX-License-Identifier: GPL-2.0-only
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02002/*
3 * HID driver for Logitech Unifying receivers
4 *
5 * Copyright (c) 2011 Logitech
6 */
7
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02008
9
10#include <linux/device.h>
11#include <linux/hid.h>
12#include <linux/module.h>
Benjamin Tissoires8cb37462014-09-30 13:18:26 -040013#include <linux/kfifo.h>
Benjamin Tissoires82c0beb2019-04-20 13:21:47 +020014#include <linux/delay.h>
Hans de Goededa12b222019-04-20 13:21:59 +020015#include <linux/usb.h> /* For to_usb_interface for kvm extra intf check */
Jonathan Nieder44d27f72012-05-11 16:17:16 +020016#include <asm/unaligned.h>
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +020017#include "hid-ids.h"
Benjamin Tissoires8cb37462014-09-30 13:18:26 -040018
19#define DJ_MAX_PAIRED_DEVICES 6
Benjamin Tissoires4fcad952019-04-20 13:21:48 +020020#define DJ_MAX_NUMBER_NOTIFS 8
Benjamin Tissoires8cb37462014-09-30 13:18:26 -040021#define DJ_RECEIVER_INDEX 0
22#define DJ_DEVICE_INDEX_MIN 1
23#define DJ_DEVICE_INDEX_MAX 6
24
25#define DJREPORT_SHORT_LENGTH 15
26#define DJREPORT_LONG_LENGTH 32
27
28#define REPORT_ID_DJ_SHORT 0x20
29#define REPORT_ID_DJ_LONG 0x21
30
Benjamin Tissoires925f0f32014-09-30 13:18:29 -040031#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_RECEIVER_INDEX 0xff
38
39#define REPORT_TYPE_RFREPORT_FIRST 0x01
Benjamin Tissoires8cb37462014-09-30 13:18:26 -040040#define REPORT_TYPE_RFREPORT_LAST 0x1F
41
42/* Command Switch to DJ mode */
43#define REPORT_TYPE_CMD_SWITCH 0x80
44#define CMD_SWITCH_PARAM_DEVBITFIELD 0x00
45#define CMD_SWITCH_PARAM_TIMEOUT_SECONDS 0x01
46#define TIMEOUT_NO_KEEPALIVE 0x00
47
48/* Command to Get the list of Paired devices */
49#define REPORT_TYPE_CMD_GET_PAIRED_DEVICES 0x81
50
51/* Device Paired Notification */
52#define REPORT_TYPE_NOTIF_DEVICE_PAIRED 0x41
53#define SPFUNCTION_MORE_NOTIF_EXPECTED 0x01
54#define SPFUNCTION_DEVICE_LIST_EMPTY 0x02
55#define DEVICE_PAIRED_PARAM_SPFUNCTION 0x00
56#define DEVICE_PAIRED_PARAM_EQUAD_ID_LSB 0x01
57#define DEVICE_PAIRED_PARAM_EQUAD_ID_MSB 0x02
58#define DEVICE_PAIRED_RF_REPORT_TYPE 0x03
59
60/* Device Un-Paired Notification */
61#define REPORT_TYPE_NOTIF_DEVICE_UNPAIRED 0x40
62
Benjamin Tissoires8cb37462014-09-30 13:18:26 -040063/* Connection Status Notification */
64#define REPORT_TYPE_NOTIF_CONNECTION_STATUS 0x42
65#define CONNECTION_STATUS_PARAM_STATUS 0x00
66#define STATUS_LINKLOSS 0x01
67
68/* Error Notification */
69#define REPORT_TYPE_NOTIF_ERROR 0x7F
70#define NOTIF_ERROR_PARAM_ETYPE 0x00
71#define ETYPE_KEEPALIVE_TIMEOUT 0x01
72
73/* supported DJ HID && RF report types */
74#define REPORT_TYPE_KEYBOARD 0x01
75#define REPORT_TYPE_MOUSE 0x02
76#define REPORT_TYPE_CONSUMER_CONTROL 0x03
77#define REPORT_TYPE_SYSTEM_CONTROL 0x04
78#define REPORT_TYPE_MEDIA_CENTER 0x08
79#define REPORT_TYPE_LEDS 0x0E
80
81/* RF Report types bitfield */
Benjamin Tissoiresa17dd1f2019-04-20 13:21:45 +020082#define STD_KEYBOARD BIT(1)
83#define STD_MOUSE BIT(2)
84#define MULTIMEDIA BIT(3)
85#define POWER_KEYS BIT(4)
86#define MEDIA_CENTER BIT(8)
87#define KBD_LEDS BIT(14)
Hans de Goede6d3c3f02019-04-20 13:22:02 +020088/* Fake (bitnr > NUMBER_OF_HID_REPORTS) bit to track HID++ capability */
89#define HIDPP BIT_ULL(63)
Benjamin Tissoires8cb37462014-09-30 13:18:26 -040090
Hans de Goede74808f92019-04-20 13:21:54 +020091/* HID++ Device Connected Notification */
92#define REPORT_TYPE_NOTIF_DEVICE_CONNECTED 0x41
93#define HIDPP_PARAM_PROTO_TYPE 0x00
94#define HIDPP_PARAM_DEVICE_INFO 0x01
95#define HIDPP_PARAM_EQUAD_LSB 0x02
96#define HIDPP_PARAM_EQUAD_MSB 0x03
Hans de Goedec9121cf2019-04-20 13:21:56 +020097#define HIDPP_PARAM_27MHZ_DEVID 0x03
Hans de Goede74808f92019-04-20 13:21:54 +020098#define HIDPP_DEVICE_TYPE_MASK GENMASK(3, 0)
99#define HIDPP_LINK_STATUS_MASK BIT(6)
Hans de Goedef2113c32019-04-20 13:22:03 +0200100#define HIDPP_MANUFACTURER_MASK BIT(7)
Hans de Goede74808f92019-04-20 13:21:54 +0200101
Hans de Goedede76b1d2019-04-20 13:22:00 +0200102#define HIDPP_DEVICE_TYPE_KEYBOARD 1
103#define HIDPP_DEVICE_TYPE_MOUSE 2
104
Hans de Goede74808f92019-04-20 13:21:54 +0200105#define HIDPP_SET_REGISTER 0x80
Benjamin Tissoiresc0340412019-04-20 13:21:46 +0200106#define HIDPP_GET_LONG_REGISTER 0x83
Hans de Goede74808f92019-04-20 13:21:54 +0200107#define HIDPP_REG_CONNECTION_STATE 0x02
Benjamin Tissoiresc0340412019-04-20 13:21:46 +0200108#define HIDPP_REG_PAIRING_INFORMATION 0xB5
109#define HIDPP_PAIRING_INFORMATION 0x20
Hans de Goede74808f92019-04-20 13:21:54 +0200110#define HIDPP_FAKE_DEVICE_ARRIVAL 0x02
111
112enum recvr_type {
113 recvr_type_dj,
114 recvr_type_hidpp,
Benjamin Tissoiresf5fb57a2019-04-20 13:21:55 +0200115 recvr_type_gaming_hidpp,
Hans de Goedec9121cf2019-04-20 13:21:56 +0200116 recvr_type_27mhz,
Hans de Goedef2113c32019-04-20 13:22:03 +0200117 recvr_type_bluetooth,
Hans de Goede74808f92019-04-20 13:21:54 +0200118};
Benjamin Tissoiresc0340412019-04-20 13:21:46 +0200119
Benjamin Tissoires8cb37462014-09-30 13:18:26 -0400120struct dj_report {
121 u8 report_id;
122 u8 device_index;
123 u8 report_type;
124 u8 report_params[DJREPORT_SHORT_LENGTH - 3];
125};
126
Benjamin Tissoires7bb56a52019-04-20 13:21:44 +0200127struct hidpp_event {
128 u8 report_id;
129 u8 device_index;
130 u8 sub_id;
131 u8 params[HIDPP_REPORT_LONG_LENGTH - 3U];
132} __packed;
133
Benjamin Tissoires8cb37462014-09-30 13:18:26 -0400134struct dj_receiver_dev {
Hans de Goedea1d97cc2019-04-20 13:21:52 +0200135 struct hid_device *mouse;
136 struct hid_device *keyboard;
Hans de Goede0ee75542019-04-20 13:21:51 +0200137 struct hid_device *hidpp;
Benjamin Tissoires8cb37462014-09-30 13:18:26 -0400138 struct dj_device *paired_dj_devices[DJ_MAX_PAIRED_DEVICES +
139 DJ_DEVICE_INDEX_MIN];
Hans de Goedea1d97cc2019-04-20 13:21:52 +0200140 struct list_head list;
141 struct kref kref;
Benjamin Tissoires8cb37462014-09-30 13:18:26 -0400142 struct work_struct work;
143 struct kfifo notif_fifo;
Hans de Goedeb6aeedd2019-04-20 13:21:53 +0200144 unsigned long last_query; /* in jiffies */
Hans de Goedea1d97cc2019-04-20 13:21:52 +0200145 bool ready;
Hans de Goede74808f92019-04-20 13:21:54 +0200146 enum recvr_type type;
147 unsigned int unnumbered_application;
Benjamin Tissoires8cb37462014-09-30 13:18:26 -0400148 spinlock_t lock;
Benjamin Tissoires8cb37462014-09-30 13:18:26 -0400149};
150
151struct dj_device {
152 struct hid_device *hdev;
153 struct dj_receiver_dev *dj_receiver_dev;
Hans de Goede6d3c3f02019-04-20 13:22:02 +0200154 u64 reports_supported;
Benjamin Tissoires8cb37462014-09-30 13:18:26 -0400155 u8 device_index;
156};
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200157
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200158#define WORKITEM_TYPE_EMPTY 0
159#define WORKITEM_TYPE_PAIRED 1
160#define WORKITEM_TYPE_UNPAIRED 2
161#define WORKITEM_TYPE_UNKNOWN 255
162
163struct dj_workitem {
164 u8 type; /* WORKITEM_TYPE_* */
165 u8 device_index;
Hans de Goedede76b1d2019-04-20 13:22:00 +0200166 u8 device_type;
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200167 u8 quad_id_msb;
168 u8 quad_id_lsb;
Hans de Goede6d3c3f02019-04-20 13:22:02 +0200169 u64 reports_supported;
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200170};
171
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200172/* Keyboard descriptor (1) */
173static const char kbd_descriptor[] = {
174 0x05, 0x01, /* USAGE_PAGE (generic Desktop) */
175 0x09, 0x06, /* USAGE (Keyboard) */
176 0xA1, 0x01, /* COLLECTION (Application) */
177 0x85, 0x01, /* REPORT_ID (1) */
178 0x95, 0x08, /* REPORT_COUNT (8) */
179 0x75, 0x01, /* REPORT_SIZE (1) */
180 0x15, 0x00, /* LOGICAL_MINIMUM (0) */
181 0x25, 0x01, /* LOGICAL_MAXIMUM (1) */
182 0x05, 0x07, /* USAGE_PAGE (Keyboard) */
183 0x19, 0xE0, /* USAGE_MINIMUM (Left Control) */
184 0x29, 0xE7, /* USAGE_MAXIMUM (Right GUI) */
185 0x81, 0x02, /* INPUT (Data,Var,Abs) */
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200186 0x95, 0x06, /* REPORT_COUNT (6) */
187 0x75, 0x08, /* REPORT_SIZE (8) */
188 0x15, 0x00, /* LOGICAL_MINIMUM (0) */
189 0x26, 0xFF, 0x00, /* LOGICAL_MAXIMUM (255) */
190 0x05, 0x07, /* USAGE_PAGE (Keyboard) */
191 0x19, 0x00, /* USAGE_MINIMUM (no event) */
192 0x2A, 0xFF, 0x00, /* USAGE_MAXIMUM (reserved) */
193 0x81, 0x00, /* INPUT (Data,Ary,Abs) */
Benjamin Tissoires0e40d352014-02-05 16:33:17 -0500194 0x85, 0x0e, /* REPORT_ID (14) */
195 0x05, 0x08, /* USAGE PAGE (LED page) */
196 0x95, 0x05, /* REPORT COUNT (5) */
197 0x75, 0x01, /* REPORT SIZE (1) */
198 0x15, 0x00, /* LOGICAL_MINIMUM (0) */
199 0x25, 0x01, /* LOGICAL_MAXIMUM (1) */
200 0x19, 0x01, /* USAGE MINIMUM (1) */
201 0x29, 0x05, /* USAGE MAXIMUM (5) */
202 0x91, 0x02, /* OUTPUT (Data, Variable, Absolute) */
203 0x95, 0x01, /* REPORT COUNT (1) */
204 0x75, 0x03, /* REPORT SIZE (3) */
205 0x91, 0x01, /* OUTPUT (Constant) */
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200206 0xC0
207};
208
209/* Mouse descriptor (2) */
210static const char mse_descriptor[] = {
211 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */
212 0x09, 0x02, /* USAGE (Mouse) */
213 0xA1, 0x01, /* COLLECTION (Application) */
214 0x85, 0x02, /* REPORT_ID = 2 */
215 0x09, 0x01, /* USAGE (pointer) */
216 0xA1, 0x00, /* COLLECTION (physical) */
217 0x05, 0x09, /* USAGE_PAGE (buttons) */
218 0x19, 0x01, /* USAGE_MIN (1) */
219 0x29, 0x10, /* USAGE_MAX (16) */
220 0x15, 0x00, /* LOGICAL_MIN (0) */
221 0x25, 0x01, /* LOGICAL_MAX (1) */
222 0x95, 0x10, /* REPORT_COUNT (16) */
223 0x75, 0x01, /* REPORT_SIZE (1) */
224 0x81, 0x02, /* INPUT (data var abs) */
225 0x05, 0x01, /* USAGE_PAGE (generic desktop) */
226 0x16, 0x01, 0xF8, /* LOGICAL_MIN (-2047) */
227 0x26, 0xFF, 0x07, /* LOGICAL_MAX (2047) */
228 0x75, 0x0C, /* REPORT_SIZE (12) */
229 0x95, 0x02, /* REPORT_COUNT (2) */
230 0x09, 0x30, /* USAGE (X) */
231 0x09, 0x31, /* USAGE (Y) */
232 0x81, 0x06, /* INPUT */
233 0x15, 0x81, /* LOGICAL_MIN (-127) */
234 0x25, 0x7F, /* LOGICAL_MAX (127) */
235 0x75, 0x08, /* REPORT_SIZE (8) */
236 0x95, 0x01, /* REPORT_COUNT (1) */
237 0x09, 0x38, /* USAGE (wheel) */
238 0x81, 0x06, /* INPUT */
239 0x05, 0x0C, /* USAGE_PAGE(consumer) */
240 0x0A, 0x38, 0x02, /* USAGE(AC Pan) */
241 0x95, 0x01, /* REPORT_COUNT (1) */
242 0x81, 0x06, /* INPUT */
243 0xC0, /* END_COLLECTION */
244 0xC0, /* END_COLLECTION */
245};
246
Hans de Goedec9121cf2019-04-20 13:21:56 +0200247/* Mouse descriptor (2) for 27 MHz receiver, only 8 buttons */
248static const char mse_27mhz_descriptor[] = {
249 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */
250 0x09, 0x02, /* USAGE (Mouse) */
251 0xA1, 0x01, /* COLLECTION (Application) */
252 0x85, 0x02, /* REPORT_ID = 2 */
253 0x09, 0x01, /* USAGE (pointer) */
254 0xA1, 0x00, /* COLLECTION (physical) */
255 0x05, 0x09, /* USAGE_PAGE (buttons) */
256 0x19, 0x01, /* USAGE_MIN (1) */
257 0x29, 0x08, /* USAGE_MAX (8) */
258 0x15, 0x00, /* LOGICAL_MIN (0) */
259 0x25, 0x01, /* LOGICAL_MAX (1) */
260 0x95, 0x08, /* REPORT_COUNT (8) */
261 0x75, 0x01, /* REPORT_SIZE (1) */
262 0x81, 0x02, /* INPUT (data var abs) */
263 0x05, 0x01, /* USAGE_PAGE (generic desktop) */
264 0x16, 0x01, 0xF8, /* LOGICAL_MIN (-2047) */
265 0x26, 0xFF, 0x07, /* LOGICAL_MAX (2047) */
266 0x75, 0x0C, /* REPORT_SIZE (12) */
267 0x95, 0x02, /* REPORT_COUNT (2) */
268 0x09, 0x30, /* USAGE (X) */
269 0x09, 0x31, /* USAGE (Y) */
270 0x81, 0x06, /* INPUT */
271 0x15, 0x81, /* LOGICAL_MIN (-127) */
272 0x25, 0x7F, /* LOGICAL_MAX (127) */
273 0x75, 0x08, /* REPORT_SIZE (8) */
274 0x95, 0x01, /* REPORT_COUNT (1) */
275 0x09, 0x38, /* USAGE (wheel) */
276 0x81, 0x06, /* INPUT */
277 0x05, 0x0C, /* USAGE_PAGE(consumer) */
278 0x0A, 0x38, 0x02, /* USAGE(AC Pan) */
279 0x95, 0x01, /* REPORT_COUNT (1) */
280 0x81, 0x06, /* INPUT */
281 0xC0, /* END_COLLECTION */
282 0xC0, /* END_COLLECTION */
283};
284
Hans de Goedef2113c32019-04-20 13:22:03 +0200285/* Mouse descriptor (2) for Bluetooth receiver, low-res hwheel, 12 buttons */
286static const char mse_bluetooth_descriptor[] = {
287 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */
288 0x09, 0x02, /* USAGE (Mouse) */
289 0xA1, 0x01, /* COLLECTION (Application) */
290 0x85, 0x02, /* REPORT_ID = 2 */
291 0x09, 0x01, /* USAGE (pointer) */
292 0xA1, 0x00, /* COLLECTION (physical) */
293 0x05, 0x09, /* USAGE_PAGE (buttons) */
294 0x19, 0x01, /* USAGE_MIN (1) */
295 0x29, 0x08, /* USAGE_MAX (8) */
296 0x15, 0x00, /* LOGICAL_MIN (0) */
297 0x25, 0x01, /* LOGICAL_MAX (1) */
298 0x95, 0x08, /* REPORT_COUNT (8) */
299 0x75, 0x01, /* REPORT_SIZE (1) */
300 0x81, 0x02, /* INPUT (data var abs) */
301 0x05, 0x01, /* USAGE_PAGE (generic desktop) */
302 0x16, 0x01, 0xF8, /* LOGICAL_MIN (-2047) */
303 0x26, 0xFF, 0x07, /* LOGICAL_MAX (2047) */
304 0x75, 0x0C, /* REPORT_SIZE (12) */
305 0x95, 0x02, /* REPORT_COUNT (2) */
306 0x09, 0x30, /* USAGE (X) */
307 0x09, 0x31, /* USAGE (Y) */
308 0x81, 0x06, /* INPUT */
309 0x15, 0x81, /* LOGICAL_MIN (-127) */
310 0x25, 0x7F, /* LOGICAL_MAX (127) */
311 0x75, 0x08, /* REPORT_SIZE (8) */
312 0x95, 0x01, /* REPORT_COUNT (1) */
313 0x09, 0x38, /* USAGE (wheel) */
314 0x81, 0x06, /* INPUT */
315 0x05, 0x0C, /* USAGE_PAGE(consumer) */
316 0x0A, 0x38, 0x02, /* USAGE(AC Pan) */
317 0x15, 0xF9, /* LOGICAL_MIN (-7) */
318 0x25, 0x07, /* LOGICAL_MAX (7) */
319 0x75, 0x04, /* REPORT_SIZE (4) */
320 0x95, 0x01, /* REPORT_COUNT (1) */
321 0x81, 0x06, /* INPUT */
322 0x05, 0x09, /* USAGE_PAGE (buttons) */
323 0x19, 0x09, /* USAGE_MIN (9) */
324 0x29, 0x0C, /* USAGE_MAX (12) */
325 0x15, 0x00, /* LOGICAL_MIN (0) */
326 0x25, 0x01, /* LOGICAL_MAX (1) */
327 0x75, 0x01, /* REPORT_SIZE (1) */
328 0x95, 0x04, /* REPORT_COUNT (4) */
329 0x81, 0x06, /* INPUT */
330 0xC0, /* END_COLLECTION */
331 0xC0, /* END_COLLECTION */
332};
333
Benjamin Tissoiresf5fb57a2019-04-20 13:21:55 +0200334/* Gaming Mouse descriptor (2) */
335static const char mse_high_res_descriptor[] = {
336 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */
337 0x09, 0x02, /* USAGE (Mouse) */
338 0xA1, 0x01, /* COLLECTION (Application) */
339 0x85, 0x02, /* REPORT_ID = 2 */
340 0x09, 0x01, /* USAGE (pointer) */
341 0xA1, 0x00, /* COLLECTION (physical) */
342 0x05, 0x09, /* USAGE_PAGE (buttons) */
343 0x19, 0x01, /* USAGE_MIN (1) */
344 0x29, 0x10, /* USAGE_MAX (16) */
345 0x15, 0x00, /* LOGICAL_MIN (0) */
346 0x25, 0x01, /* LOGICAL_MAX (1) */
347 0x95, 0x10, /* REPORT_COUNT (16) */
348 0x75, 0x01, /* REPORT_SIZE (1) */
349 0x81, 0x02, /* INPUT (data var abs) */
350 0x05, 0x01, /* USAGE_PAGE (generic desktop) */
351 0x16, 0x01, 0x80, /* LOGICAL_MIN (-32767) */
352 0x26, 0xFF, 0x7F, /* LOGICAL_MAX (32767) */
353 0x75, 0x10, /* REPORT_SIZE (16) */
354 0x95, 0x02, /* REPORT_COUNT (2) */
355 0x09, 0x30, /* USAGE (X) */
356 0x09, 0x31, /* USAGE (Y) */
357 0x81, 0x06, /* INPUT */
358 0x15, 0x81, /* LOGICAL_MIN (-127) */
359 0x25, 0x7F, /* LOGICAL_MAX (127) */
360 0x75, 0x08, /* REPORT_SIZE (8) */
361 0x95, 0x01, /* REPORT_COUNT (1) */
362 0x09, 0x38, /* USAGE (wheel) */
363 0x81, 0x06, /* INPUT */
364 0x05, 0x0C, /* USAGE_PAGE(consumer) */
365 0x0A, 0x38, 0x02, /* USAGE(AC Pan) */
366 0x95, 0x01, /* REPORT_COUNT (1) */
367 0x81, 0x06, /* INPUT */
368 0xC0, /* END_COLLECTION */
369 0xC0, /* END_COLLECTION */
370};
371
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200372/* Consumer Control descriptor (3) */
373static const char consumer_descriptor[] = {
374 0x05, 0x0C, /* USAGE_PAGE (Consumer Devices) */
375 0x09, 0x01, /* USAGE (Consumer Control) */
376 0xA1, 0x01, /* COLLECTION (Application) */
377 0x85, 0x03, /* REPORT_ID = 3 */
378 0x75, 0x10, /* REPORT_SIZE (16) */
379 0x95, 0x02, /* REPORT_COUNT (2) */
380 0x15, 0x01, /* LOGICAL_MIN (1) */
381 0x26, 0x8C, 0x02, /* LOGICAL_MAX (652) */
382 0x19, 0x01, /* USAGE_MIN (1) */
383 0x2A, 0x8C, 0x02, /* USAGE_MAX (652) */
384 0x81, 0x00, /* INPUT (Data Ary Abs) */
385 0xC0, /* END_COLLECTION */
386}; /* */
387
388/* System control descriptor (4) */
389static const char syscontrol_descriptor[] = {
390 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */
391 0x09, 0x80, /* USAGE (System Control) */
392 0xA1, 0x01, /* COLLECTION (Application) */
393 0x85, 0x04, /* REPORT_ID = 4 */
394 0x75, 0x02, /* REPORT_SIZE (2) */
395 0x95, 0x01, /* REPORT_COUNT (1) */
396 0x15, 0x01, /* LOGICAL_MIN (1) */
397 0x25, 0x03, /* LOGICAL_MAX (3) */
398 0x09, 0x82, /* USAGE (System Sleep) */
399 0x09, 0x81, /* USAGE (System Power Down) */
400 0x09, 0x83, /* USAGE (System Wake Up) */
401 0x81, 0x60, /* INPUT (Data Ary Abs NPrf Null) */
402 0x75, 0x06, /* REPORT_SIZE (6) */
403 0x81, 0x03, /* INPUT (Cnst Var Abs) */
404 0xC0, /* END_COLLECTION */
405};
406
407/* Media descriptor (8) */
408static const char media_descriptor[] = {
409 0x06, 0xbc, 0xff, /* Usage Page 0xffbc */
410 0x09, 0x88, /* Usage 0x0088 */
411 0xa1, 0x01, /* BeginCollection */
412 0x85, 0x08, /* Report ID 8 */
413 0x19, 0x01, /* Usage Min 0x0001 */
414 0x29, 0xff, /* Usage Max 0x00ff */
415 0x15, 0x01, /* Logical Min 1 */
416 0x26, 0xff, 0x00, /* Logical Max 255 */
417 0x75, 0x08, /* Report Size 8 */
418 0x95, 0x01, /* Report Count 1 */
419 0x81, 0x00, /* Input */
420 0xc0, /* EndCollection */
421}; /* */
422
Benjamin Tissoires925f0f32014-09-30 13:18:29 -0400423/* HIDPP descriptor */
424static const char hidpp_descriptor[] = {
425 0x06, 0x00, 0xff, /* Usage Page (Vendor Defined Page 1) */
426 0x09, 0x01, /* Usage (Vendor Usage 1) */
427 0xa1, 0x01, /* Collection (Application) */
428 0x85, 0x10, /* Report ID (16) */
429 0x75, 0x08, /* Report Size (8) */
430 0x95, 0x06, /* Report Count (6) */
431 0x15, 0x00, /* Logical Minimum (0) */
432 0x26, 0xff, 0x00, /* Logical Maximum (255) */
433 0x09, 0x01, /* Usage (Vendor Usage 1) */
434 0x81, 0x00, /* Input (Data,Arr,Abs) */
435 0x09, 0x01, /* Usage (Vendor Usage 1) */
436 0x91, 0x00, /* Output (Data,Arr,Abs) */
437 0xc0, /* End Collection */
438 0x06, 0x00, 0xff, /* Usage Page (Vendor Defined Page 1) */
439 0x09, 0x02, /* Usage (Vendor Usage 2) */
440 0xa1, 0x01, /* Collection (Application) */
441 0x85, 0x11, /* Report ID (17) */
442 0x75, 0x08, /* Report Size (8) */
443 0x95, 0x13, /* Report Count (19) */
444 0x15, 0x00, /* Logical Minimum (0) */
445 0x26, 0xff, 0x00, /* Logical Maximum (255) */
446 0x09, 0x02, /* Usage (Vendor Usage 2) */
447 0x81, 0x00, /* Input (Data,Arr,Abs) */
448 0x09, 0x02, /* Usage (Vendor Usage 2) */
449 0x91, 0x00, /* Output (Data,Arr,Abs) */
450 0xc0, /* End Collection */
451 0x06, 0x00, 0xff, /* Usage Page (Vendor Defined Page 1) */
452 0x09, 0x04, /* Usage (Vendor Usage 0x04) */
453 0xa1, 0x01, /* Collection (Application) */
454 0x85, 0x20, /* Report ID (32) */
455 0x75, 0x08, /* Report Size (8) */
456 0x95, 0x0e, /* Report Count (14) */
457 0x15, 0x00, /* Logical Minimum (0) */
458 0x26, 0xff, 0x00, /* Logical Maximum (255) */
459 0x09, 0x41, /* Usage (Vendor Usage 0x41) */
460 0x81, 0x00, /* Input (Data,Arr,Abs) */
461 0x09, 0x41, /* Usage (Vendor Usage 0x41) */
462 0x91, 0x00, /* Output (Data,Arr,Abs) */
463 0x85, 0x21, /* Report ID (33) */
464 0x95, 0x1f, /* Report Count (31) */
465 0x15, 0x00, /* Logical Minimum (0) */
466 0x26, 0xff, 0x00, /* Logical Maximum (255) */
467 0x09, 0x42, /* Usage (Vendor Usage 0x42) */
468 0x81, 0x00, /* Input (Data,Arr,Abs) */
469 0x09, 0x42, /* Usage (Vendor Usage 0x42) */
470 0x91, 0x00, /* Output (Data,Arr,Abs) */
471 0xc0, /* End Collection */
472};
473
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200474/* Maximum size of all defined hid reports in bytes (including report id) */
475#define MAX_REPORT_SIZE 8
476
Henrik Rydberg2a039bf2012-04-22 14:21:39 +0200477/* Make sure all descriptors are present here */
478#define MAX_RDESC_SIZE \
479 (sizeof(kbd_descriptor) + \
Hans de Goedef2113c32019-04-20 13:22:03 +0200480 sizeof(mse_bluetooth_descriptor) + \
Henrik Rydberg2a039bf2012-04-22 14:21:39 +0200481 sizeof(consumer_descriptor) + \
482 sizeof(syscontrol_descriptor) + \
Benjamin Tissoires925f0f32014-09-30 13:18:29 -0400483 sizeof(media_descriptor) + \
484 sizeof(hidpp_descriptor))
Henrik Rydberg2a039bf2012-04-22 14:21:39 +0200485
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200486/* Number of possible hid report types that can be created by this driver.
487 *
488 * Right now, RF report types have the same report types (or report id's)
489 * than the hid report created from those RF reports. In the future
490 * this doesnt have to be true.
491 *
492 * For instance, RF report type 0x01 which has a size of 8 bytes, corresponds
493 * to hid report id 0x01, this is standard keyboard. Same thing applies to mice
494 * reports and consumer control, etc. If a new RF report is created, it doesn't
495 * has to have the same report id as its corresponding hid report, so an
496 * translation may have to take place for future report types.
497 */
498#define NUMBER_OF_HID_REPORTS 32
499static const u8 hid_reportid_size_map[NUMBER_OF_HID_REPORTS] = {
500 [1] = 8, /* Standard keyboard */
501 [2] = 8, /* Standard mouse */
502 [3] = 5, /* Consumer control */
503 [4] = 2, /* System control */
504 [8] = 2, /* Media Center */
505};
506
507
508#define LOGITECH_DJ_INTERFACE_NUMBER 0x02
509
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200510static struct hid_ll_driver logi_dj_ll_driver;
511
Nestor Lopez Casadoc63e0e32013-07-18 06:21:30 -0700512static int logi_dj_recv_query_paired_devices(struct dj_receiver_dev *djrcv_dev);
Hans de Goedea1d97cc2019-04-20 13:21:52 +0200513static void delayedwork_callback(struct work_struct *work);
514
515static LIST_HEAD(dj_hdev_list);
516static DEFINE_MUTEX(dj_hdev_list_lock);
517
518/*
519 * dj/HID++ receivers are really a single logical entity, but for BIOS/Windows
520 * compatibility they have multiple USB interfaces. On HID++ receivers we need
521 * to listen for input reports on both interfaces. The functions below are used
522 * to create a single struct dj_receiver_dev for all interfaces belonging to
523 * a single USB-device / receiver.
524 */
Hans de Goedef2113c32019-04-20 13:22:03 +0200525static struct dj_receiver_dev *dj_find_receiver_dev(struct hid_device *hdev,
526 enum recvr_type type)
Hans de Goedea1d97cc2019-04-20 13:21:52 +0200527{
528 struct dj_receiver_dev *djrcv_dev;
Hans de Goedef2113c32019-04-20 13:22:03 +0200529 char sep;
530
531 /*
532 * The bluetooth receiver contains a built-in hub and has separate
533 * USB-devices for the keyboard and mouse interfaces.
534 */
535 sep = (type == recvr_type_bluetooth) ? '.' : '/';
Hans de Goedea1d97cc2019-04-20 13:21:52 +0200536
537 /* Try to find an already-probed interface from the same device */
538 list_for_each_entry(djrcv_dev, &dj_hdev_list, list) {
539 if (djrcv_dev->mouse &&
Hans de Goedef2113c32019-04-20 13:22:03 +0200540 hid_compare_device_paths(hdev, djrcv_dev->mouse, sep)) {
Hans de Goedea1d97cc2019-04-20 13:21:52 +0200541 kref_get(&djrcv_dev->kref);
542 return djrcv_dev;
543 }
544 if (djrcv_dev->keyboard &&
Hans de Goedef2113c32019-04-20 13:22:03 +0200545 hid_compare_device_paths(hdev, djrcv_dev->keyboard, sep)) {
Hans de Goedea1d97cc2019-04-20 13:21:52 +0200546 kref_get(&djrcv_dev->kref);
547 return djrcv_dev;
548 }
549 if (djrcv_dev->hidpp &&
Hans de Goedef2113c32019-04-20 13:22:03 +0200550 hid_compare_device_paths(hdev, djrcv_dev->hidpp, sep)) {
Hans de Goedea1d97cc2019-04-20 13:21:52 +0200551 kref_get(&djrcv_dev->kref);
552 return djrcv_dev;
553 }
554 }
555
556 return NULL;
557}
558
559static void dj_release_receiver_dev(struct kref *kref)
560{
561 struct dj_receiver_dev *djrcv_dev = container_of(kref, struct dj_receiver_dev, kref);
562
563 list_del(&djrcv_dev->list);
564 kfifo_free(&djrcv_dev->notif_fifo);
565 kfree(djrcv_dev);
566}
567
568static void dj_put_receiver_dev(struct hid_device *hdev)
569{
570 struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
571
572 mutex_lock(&dj_hdev_list_lock);
573
574 if (djrcv_dev->mouse == hdev)
575 djrcv_dev->mouse = NULL;
576 if (djrcv_dev->keyboard == hdev)
577 djrcv_dev->keyboard = NULL;
578 if (djrcv_dev->hidpp == hdev)
579 djrcv_dev->hidpp = NULL;
580
581 kref_put(&djrcv_dev->kref, dj_release_receiver_dev);
582
583 mutex_unlock(&dj_hdev_list_lock);
584}
585
586static struct dj_receiver_dev *dj_get_receiver_dev(struct hid_device *hdev,
Hans de Goede74808f92019-04-20 13:21:54 +0200587 enum recvr_type type,
Hans de Goedea1d97cc2019-04-20 13:21:52 +0200588 unsigned int application,
589 bool is_hidpp)
590{
591 struct dj_receiver_dev *djrcv_dev;
592
593 mutex_lock(&dj_hdev_list_lock);
594
Hans de Goedef2113c32019-04-20 13:22:03 +0200595 djrcv_dev = dj_find_receiver_dev(hdev, type);
Hans de Goedea1d97cc2019-04-20 13:21:52 +0200596 if (!djrcv_dev) {
597 djrcv_dev = kzalloc(sizeof(*djrcv_dev), GFP_KERNEL);
598 if (!djrcv_dev)
599 goto out;
600
601 INIT_WORK(&djrcv_dev->work, delayedwork_callback);
602 spin_lock_init(&djrcv_dev->lock);
603 if (kfifo_alloc(&djrcv_dev->notif_fifo,
604 DJ_MAX_NUMBER_NOTIFS * sizeof(struct dj_workitem),
605 GFP_KERNEL)) {
606 kfree(djrcv_dev);
607 djrcv_dev = NULL;
608 goto out;
609 }
610 kref_init(&djrcv_dev->kref);
611 list_add_tail(&djrcv_dev->list, &dj_hdev_list);
Hans de Goedeb6aeedd2019-04-20 13:21:53 +0200612 djrcv_dev->last_query = jiffies;
Hans de Goede74808f92019-04-20 13:21:54 +0200613 djrcv_dev->type = type;
Hans de Goedea1d97cc2019-04-20 13:21:52 +0200614 }
615
616 if (application == HID_GD_KEYBOARD)
617 djrcv_dev->keyboard = hdev;
618 if (application == HID_GD_MOUSE)
619 djrcv_dev->mouse = hdev;
620 if (is_hidpp)
621 djrcv_dev->hidpp = hdev;
622
623 hid_set_drvdata(hdev, djrcv_dev);
624out:
625 mutex_unlock(&dj_hdev_list_lock);
626 return djrcv_dev;
627}
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200628
629static void logi_dj_recv_destroy_djhid_device(struct dj_receiver_dev *djrcv_dev,
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200630 struct dj_workitem *workitem)
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200631{
632 /* Called in delayed work context */
633 struct dj_device *dj_dev;
634 unsigned long flags;
635
636 spin_lock_irqsave(&djrcv_dev->lock, flags);
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200637 dj_dev = djrcv_dev->paired_dj_devices[workitem->device_index];
638 djrcv_dev->paired_dj_devices[workitem->device_index] = NULL;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200639 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
640
641 if (dj_dev != NULL) {
642 hid_destroy_device(dj_dev->hdev);
643 kfree(dj_dev);
644 } else {
Hans de Goede0ee75542019-04-20 13:21:51 +0200645 hid_err(djrcv_dev->hidpp, "%s: can't destroy a NULL device\n",
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200646 __func__);
647 }
648}
649
650static void logi_dj_recv_add_djhid_device(struct dj_receiver_dev *djrcv_dev,
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200651 struct dj_workitem *workitem)
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200652{
653 /* Called in delayed work context */
Hans de Goede0ee75542019-04-20 13:21:51 +0200654 struct hid_device *djrcv_hdev = djrcv_dev->hidpp;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200655 struct hid_device *dj_hiddev;
656 struct dj_device *dj_dev;
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200657 u8 device_index = workitem->device_index;
Hans de Goedef41d7662019-04-20 13:21:50 +0200658 unsigned long flags;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200659
660 /* Device index goes from 1 to 6, we need 3 bytes to store the
661 * semicolon, the index, and a null terminator
662 */
663 unsigned char tmpstr[3];
664
Hans de Goedef41d7662019-04-20 13:21:50 +0200665 /* We are the only one ever adding a device, no need to lock */
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200666 if (djrcv_dev->paired_dj_devices[device_index]) {
Nestor Lopez Casadoc63e0e32013-07-18 06:21:30 -0700667 /* The device is already known. No need to reallocate it. */
668 dbg_hid("%s: device is already known\n", __func__);
669 return;
670 }
671
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200672 dj_hiddev = hid_allocate_device();
673 if (IS_ERR(dj_hiddev)) {
Hans de Goedeaca22a32019-04-20 13:21:58 +0200674 hid_err(djrcv_hdev, "%s: hid_allocate_dev failed\n", __func__);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200675 return;
676 }
677
678 dj_hiddev->ll_driver = &logi_dj_ll_driver;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200679
680 dj_hiddev->dev.parent = &djrcv_hdev->dev;
681 dj_hiddev->bus = BUS_USB;
Benjamin Tissoires82c0beb2019-04-20 13:21:47 +0200682 dj_hiddev->vendor = djrcv_hdev->vendor;
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200683 dj_hiddev->product = (workitem->quad_id_msb << 8) |
684 workitem->quad_id_lsb;
Hans de Goedede76b1d2019-04-20 13:22:00 +0200685 if (workitem->device_type) {
686 const char *type_str = "Device";
687
688 switch (workitem->device_type) {
689 case 0x01: type_str = "Keyboard"; break;
690 case 0x02: type_str = "Mouse"; break;
691 case 0x03: type_str = "Numpad"; break;
692 case 0x04: type_str = "Presenter"; break;
693 case 0x07: type_str = "Remote Control"; break;
694 case 0x08: type_str = "Trackball"; break;
695 case 0x09: type_str = "Touchpad"; break;
696 }
697 snprintf(dj_hiddev->name, sizeof(dj_hiddev->name),
698 "Logitech Wireless %s PID:%04x",
699 type_str, dj_hiddev->product);
700 } else {
701 snprintf(dj_hiddev->name, sizeof(dj_hiddev->name),
702 "Logitech Unifying Device. Wireless PID:%04x",
703 dj_hiddev->product);
704 }
Benjamin Tissoiresd6102742014-09-30 13:18:25 -0400705
Hans de Goedec9121cf2019-04-20 13:21:56 +0200706 if (djrcv_dev->type == recvr_type_27mhz)
707 dj_hiddev->group = HID_GROUP_LOGITECH_27MHZ_DEVICE;
708 else
709 dj_hiddev->group = HID_GROUP_LOGITECH_DJ_DEVICE;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200710
Benjamin Tissoires82c0beb2019-04-20 13:21:47 +0200711 memcpy(dj_hiddev->phys, djrcv_hdev->phys, sizeof(djrcv_hdev->phys));
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200712 snprintf(tmpstr, sizeof(tmpstr), ":%d", device_index);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200713 strlcat(dj_hiddev->phys, tmpstr, sizeof(dj_hiddev->phys));
714
715 dj_dev = kzalloc(sizeof(struct dj_device), GFP_KERNEL);
716
717 if (!dj_dev) {
Hans de Goedeaca22a32019-04-20 13:21:58 +0200718 hid_err(djrcv_hdev, "%s: failed allocating dj_dev\n", __func__);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200719 goto dj_device_allocate_fail;
720 }
721
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200722 dj_dev->reports_supported = workitem->reports_supported;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200723 dj_dev->hdev = dj_hiddev;
724 dj_dev->dj_receiver_dev = djrcv_dev;
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200725 dj_dev->device_index = device_index;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200726 dj_hiddev->driver_data = dj_dev;
727
Hans de Goedef41d7662019-04-20 13:21:50 +0200728 spin_lock_irqsave(&djrcv_dev->lock, flags);
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200729 djrcv_dev->paired_dj_devices[device_index] = dj_dev;
Hans de Goedef41d7662019-04-20 13:21:50 +0200730 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200731
732 if (hid_add_device(dj_hiddev)) {
Hans de Goedeaca22a32019-04-20 13:21:58 +0200733 hid_err(djrcv_hdev, "%s: failed adding dj_device\n", __func__);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200734 goto hid_add_device_fail;
735 }
736
737 return;
738
739hid_add_device_fail:
Hans de Goedef41d7662019-04-20 13:21:50 +0200740 spin_lock_irqsave(&djrcv_dev->lock, flags);
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200741 djrcv_dev->paired_dj_devices[device_index] = NULL;
Hans de Goedef41d7662019-04-20 13:21:50 +0200742 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200743 kfree(dj_dev);
744dj_device_allocate_fail:
745 hid_destroy_device(dj_hiddev);
746}
747
748static void delayedwork_callback(struct work_struct *work)
749{
750 struct dj_receiver_dev *djrcv_dev =
751 container_of(work, struct dj_receiver_dev, work);
752
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200753 struct dj_workitem workitem;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200754 unsigned long flags;
755 int count;
Nestor Lopez Casadoc63e0e32013-07-18 06:21:30 -0700756 int retval;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200757
758 dbg_hid("%s\n", __func__);
759
760 spin_lock_irqsave(&djrcv_dev->lock, flags);
761
Hans de Goedea1d97cc2019-04-20 13:21:52 +0200762 /*
763 * Since we attach to multiple interfaces, we may get scheduled before
764 * we are bound to the HID++ interface, catch this.
765 */
766 if (!djrcv_dev->ready) {
767 pr_warn("%s: delayedwork queued before hidpp interface was enumerated\n",
768 __func__);
769 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
770 return;
771 }
772
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200773 count = kfifo_out(&djrcv_dev->notif_fifo, &workitem, sizeof(workitem));
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200774
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200775 if (count != sizeof(workitem)) {
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200776 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
777 return;
778 }
779
Hans de Goedee316aa62019-04-20 13:22:01 +0200780 if (!kfifo_is_empty(&djrcv_dev->notif_fifo))
781 schedule_work(&djrcv_dev->work);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200782
783 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
784
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200785 switch (workitem.type) {
786 case WORKITEM_TYPE_PAIRED:
787 logi_dj_recv_add_djhid_device(djrcv_dev, &workitem);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200788 break;
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200789 case WORKITEM_TYPE_UNPAIRED:
790 logi_dj_recv_destroy_djhid_device(djrcv_dev, &workitem);
791 break;
792 case WORKITEM_TYPE_UNKNOWN:
793 retval = logi_dj_recv_query_paired_devices(djrcv_dev);
794 if (retval) {
Hans de Goede0ee75542019-04-20 13:21:51 +0200795 hid_err(djrcv_dev->hidpp, "%s: logi_dj_recv_query_paired_devices error: %d\n",
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200796 __func__, retval);
797 }
798 break;
799 case WORKITEM_TYPE_EMPTY:
800 dbg_hid("%s: device list is empty\n", __func__);
801 break;
802 }
803}
804
Hans de Goedeb6aeedd2019-04-20 13:21:53 +0200805/*
806 * Sometimes we receive reports for which we do not have a paired dj_device
807 * associated with the device_index or report-type to forward the report to.
808 * This means that the original "device paired" notification corresponding
809 * to the dj_device never arrived to this driver. Possible reasons for this are:
810 * 1) hid-core discards all packets coming from a device during probe().
811 * 2) if the receiver is plugged into a KVM switch then the pairing reports
812 * are only forwarded to it if the focus is on this PC.
813 * This function deals with this by re-asking the receiver for the list of
814 * connected devices in the delayed work callback.
815 * This function MUST be called with djrcv->lock held.
816 */
817static void logi_dj_recv_queue_unknown_work(struct dj_receiver_dev *djrcv_dev)
818{
819 struct dj_workitem workitem = { .type = WORKITEM_TYPE_UNKNOWN };
820
821 /* Rate limit queries done because of unhandeled reports to 2/sec */
822 if (time_before(jiffies, djrcv_dev->last_query + HZ / 2))
823 return;
824
825 kfifo_in(&djrcv_dev->notif_fifo, &workitem, sizeof(workitem));
826 schedule_work(&djrcv_dev->work);
827}
828
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200829static void logi_dj_recv_queue_notification(struct dj_receiver_dev *djrcv_dev,
830 struct dj_report *dj_report)
831{
832 /* We are called from atomic context (tasklet && djrcv->lock held) */
833 struct dj_workitem workitem = {
834 .device_index = dj_report->device_index,
835 };
836
837 switch (dj_report->report_type) {
838 case REPORT_TYPE_NOTIF_DEVICE_PAIRED:
839 workitem.type = WORKITEM_TYPE_PAIRED;
840 if (dj_report->report_params[DEVICE_PAIRED_PARAM_SPFUNCTION] &
841 SPFUNCTION_DEVICE_LIST_EMPTY) {
842 workitem.type = WORKITEM_TYPE_EMPTY;
843 break;
844 }
845 /* fall-through */
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200846 case REPORT_TYPE_NOTIF_DEVICE_UNPAIRED:
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200847 workitem.quad_id_msb =
848 dj_report->report_params[DEVICE_PAIRED_PARAM_EQUAD_ID_MSB];
849 workitem.quad_id_lsb =
850 dj_report->report_params[DEVICE_PAIRED_PARAM_EQUAD_ID_LSB];
851 workitem.reports_supported = get_unaligned_le32(
852 dj_report->report_params +
853 DEVICE_PAIRED_RF_REPORT_TYPE);
Hans de Goede6d3c3f02019-04-20 13:22:02 +0200854 workitem.reports_supported |= HIDPP;
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200855 if (dj_report->report_type == REPORT_TYPE_NOTIF_DEVICE_UNPAIRED)
856 workitem.type = WORKITEM_TYPE_UNPAIRED;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200857 break;
858 default:
Hans de Goedeb6aeedd2019-04-20 13:21:53 +0200859 logi_dj_recv_queue_unknown_work(djrcv_dev);
860 return;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200861 }
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200862
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200863 kfifo_in(&djrcv_dev->notif_fifo, &workitem, sizeof(workitem));
Hans de Goedee316aa62019-04-20 13:22:01 +0200864 schedule_work(&djrcv_dev->work);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200865}
866
Hans de Goede74808f92019-04-20 13:21:54 +0200867static void logi_hidpp_dev_conn_notif_equad(struct hidpp_event *hidpp_report,
868 struct dj_workitem *workitem)
869{
870 workitem->type = WORKITEM_TYPE_PAIRED;
Hans de Goedede76b1d2019-04-20 13:22:00 +0200871 workitem->device_type = hidpp_report->params[HIDPP_PARAM_DEVICE_INFO] &
872 HIDPP_DEVICE_TYPE_MASK;
Hans de Goede74808f92019-04-20 13:21:54 +0200873 workitem->quad_id_msb = hidpp_report->params[HIDPP_PARAM_EQUAD_MSB];
874 workitem->quad_id_lsb = hidpp_report->params[HIDPP_PARAM_EQUAD_LSB];
Hans de Goedede76b1d2019-04-20 13:22:00 +0200875 switch (workitem->device_type) {
Hans de Goede74808f92019-04-20 13:21:54 +0200876 case REPORT_TYPE_KEYBOARD:
877 workitem->reports_supported |= STD_KEYBOARD | MULTIMEDIA |
Hans de Goede6d3c3f02019-04-20 13:22:02 +0200878 POWER_KEYS | MEDIA_CENTER |
879 HIDPP;
Hans de Goede74808f92019-04-20 13:21:54 +0200880 break;
881 case REPORT_TYPE_MOUSE:
Hans de Goede6d3c3f02019-04-20 13:22:02 +0200882 workitem->reports_supported |= STD_MOUSE | HIDPP;
Hans de Goede74808f92019-04-20 13:21:54 +0200883 break;
884 }
885}
886
Hans de Goedec9121cf2019-04-20 13:21:56 +0200887static void logi_hidpp_dev_conn_notif_27mhz(struct hid_device *hdev,
888 struct hidpp_event *hidpp_report,
889 struct dj_workitem *workitem)
890{
891 workitem->type = WORKITEM_TYPE_PAIRED;
892 workitem->quad_id_lsb = hidpp_report->params[HIDPP_PARAM_27MHZ_DEVID];
893 switch (hidpp_report->device_index) {
894 case 1: /* Index 1 is always a mouse */
895 case 2: /* Index 2 is always a mouse */
Hans de Goedede76b1d2019-04-20 13:22:00 +0200896 workitem->device_type = HIDPP_DEVICE_TYPE_MOUSE;
Hans de Goede6d3c3f02019-04-20 13:22:02 +0200897 workitem->reports_supported |= STD_MOUSE | HIDPP;
Hans de Goedec9121cf2019-04-20 13:21:56 +0200898 break;
899 case 3: /* Index 3 is always the keyboard */
900 case 4: /* Index 4 is used for an optional separate numpad */
Hans de Goedede76b1d2019-04-20 13:22:00 +0200901 workitem->device_type = HIDPP_DEVICE_TYPE_KEYBOARD;
Hans de Goedec9121cf2019-04-20 13:21:56 +0200902 workitem->reports_supported |= STD_KEYBOARD | MULTIMEDIA |
Hans de Goede6d3c3f02019-04-20 13:22:02 +0200903 POWER_KEYS | HIDPP;
Hans de Goedec9121cf2019-04-20 13:21:56 +0200904 break;
905 default:
906 hid_warn(hdev, "%s: unexpected device-index %d", __func__,
907 hidpp_report->device_index);
908 }
909}
910
Hans de Goede74808f92019-04-20 13:21:54 +0200911static void logi_hidpp_recv_queue_notif(struct hid_device *hdev,
912 struct hidpp_event *hidpp_report)
913{
914 /* We are called from atomic context (tasklet && djrcv->lock held) */
915 struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
916 const char *device_type = "UNKNOWN";
917 struct dj_workitem workitem = {
918 .type = WORKITEM_TYPE_EMPTY,
919 .device_index = hidpp_report->device_index,
920 };
921
922 switch (hidpp_report->params[HIDPP_PARAM_PROTO_TYPE]) {
923 case 0x01:
924 device_type = "Bluetooth";
Hans de Goedef2113c32019-04-20 13:22:03 +0200925 /* Bluetooth connect packet contents is the same as (e)QUAD */
926 logi_hidpp_dev_conn_notif_equad(hidpp_report, &workitem);
927 if (!(hidpp_report->params[HIDPP_PARAM_DEVICE_INFO] &
928 HIDPP_MANUFACTURER_MASK)) {
929 hid_info(hdev, "Non Logitech device connected on slot %d\n",
930 hidpp_report->device_index);
931 workitem.reports_supported &= ~HIDPP;
932 }
Hans de Goede74808f92019-04-20 13:21:54 +0200933 break;
934 case 0x02:
935 device_type = "27 Mhz";
Hans de Goedec9121cf2019-04-20 13:21:56 +0200936 logi_hidpp_dev_conn_notif_27mhz(hdev, hidpp_report, &workitem);
Hans de Goede74808f92019-04-20 13:21:54 +0200937 break;
938 case 0x03:
939 device_type = "QUAD or eQUAD";
940 logi_hidpp_dev_conn_notif_equad(hidpp_report, &workitem);
941 break;
942 case 0x04:
943 device_type = "eQUAD step 4 DJ";
944 logi_hidpp_dev_conn_notif_equad(hidpp_report, &workitem);
945 break;
946 case 0x05:
947 device_type = "DFU Lite";
948 break;
949 case 0x06:
950 device_type = "eQUAD step 4 Lite";
951 logi_hidpp_dev_conn_notif_equad(hidpp_report, &workitem);
952 break;
953 case 0x07:
954 device_type = "eQUAD step 4 Gaming";
955 break;
956 case 0x08:
957 device_type = "eQUAD step 4 for gamepads";
958 break;
959 case 0x0a:
960 device_type = "eQUAD nano Lite";
961 logi_hidpp_dev_conn_notif_equad(hidpp_report, &workitem);
962 break;
963 case 0x0c:
964 device_type = "eQUAD Lightspeed";
Benjamin Tissoiresf5fb57a2019-04-20 13:21:55 +0200965 logi_hidpp_dev_conn_notif_equad(hidpp_report, &workitem);
966 workitem.reports_supported |= STD_KEYBOARD;
Hans de Goede74808f92019-04-20 13:21:54 +0200967 break;
968 }
969
970 if (workitem.type == WORKITEM_TYPE_EMPTY) {
971 hid_warn(hdev,
972 "unusable device of type %s (0x%02x) connected on slot %d",
973 device_type,
974 hidpp_report->params[HIDPP_PARAM_PROTO_TYPE],
975 hidpp_report->device_index);
976 return;
977 }
978
979 hid_info(hdev, "device of type %s (0x%02x) connected on slot %d",
980 device_type, hidpp_report->params[HIDPP_PARAM_PROTO_TYPE],
981 hidpp_report->device_index);
982
Hans de Goede74808f92019-04-20 13:21:54 +0200983 kfifo_in(&djrcv_dev->notif_fifo, &workitem, sizeof(workitem));
Hans de Goedee316aa62019-04-20 13:22:01 +0200984 schedule_work(&djrcv_dev->work);
Hans de Goede74808f92019-04-20 13:21:54 +0200985}
986
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200987static void logi_dj_recv_forward_null_report(struct dj_receiver_dev *djrcv_dev,
988 struct dj_report *dj_report)
989{
990 /* We are called from atomic context (tasklet && djrcv->lock held) */
991 unsigned int i;
992 u8 reportbuffer[MAX_REPORT_SIZE];
993 struct dj_device *djdev;
994
995 djdev = djrcv_dev->paired_dj_devices[dj_report->device_index];
996
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200997 memset(reportbuffer, 0, sizeof(reportbuffer));
998
999 for (i = 0; i < NUMBER_OF_HID_REPORTS; i++) {
1000 if (djdev->reports_supported & (1 << i)) {
1001 reportbuffer[0] = i;
1002 if (hid_input_report(djdev->hdev,
1003 HID_INPUT_REPORT,
1004 reportbuffer,
1005 hid_reportid_size_map[i], 1)) {
1006 dbg_hid("hid_input_report error sending null "
1007 "report\n");
1008 }
1009 }
1010 }
1011}
1012
Benjamin Tissoires83898232019-04-20 13:21:43 +02001013static void logi_dj_recv_forward_dj(struct dj_receiver_dev *djrcv_dev,
1014 struct dj_report *dj_report)
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001015{
1016 /* We are called from atomic context (tasklet && djrcv->lock held) */
1017 struct dj_device *dj_device;
1018
1019 dj_device = djrcv_dev->paired_dj_devices[dj_report->device_index];
1020
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001021 if ((dj_report->report_type > ARRAY_SIZE(hid_reportid_size_map) - 1) ||
1022 (hid_reportid_size_map[dj_report->report_type] == 0)) {
1023 dbg_hid("invalid report type:%x\n", dj_report->report_type);
1024 return;
1025 }
1026
1027 if (hid_input_report(dj_device->hdev,
1028 HID_INPUT_REPORT, &dj_report->report_type,
1029 hid_reportid_size_map[dj_report->report_type], 1)) {
1030 dbg_hid("hid_input_report error\n");
1031 }
1032}
1033
Benjamin Tissoires83898232019-04-20 13:21:43 +02001034static void logi_dj_recv_forward_report(struct dj_device *dj_dev, u8 *data,
1035 int size)
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001036{
1037 /* We are called from atomic context (tasklet && djrcv->lock held) */
1038 if (hid_input_report(dj_dev->hdev, HID_INPUT_REPORT, data, size, 1))
1039 dbg_hid("hid_input_report error\n");
1040}
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001041
Hans de Goede74808f92019-04-20 13:21:54 +02001042static void logi_dj_recv_forward_input_report(struct hid_device *hdev,
1043 u8 *data, int size)
1044{
1045 struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
1046 struct dj_device *dj_dev;
1047 unsigned long flags;
1048 u8 report = data[0];
1049 int i;
1050
1051 if (report > REPORT_TYPE_RFREPORT_LAST) {
Colin Ian King640d4ea2019-04-26 14:16:31 +01001052 hid_err(hdev, "Unexpected input report number %d\n", report);
Hans de Goede74808f92019-04-20 13:21:54 +02001053 return;
1054 }
1055
1056 spin_lock_irqsave(&djrcv_dev->lock, flags);
1057 for (i = 0; i < (DJ_MAX_PAIRED_DEVICES + DJ_DEVICE_INDEX_MIN); i++) {
1058 dj_dev = djrcv_dev->paired_dj_devices[i];
1059 if (dj_dev && (dj_dev->reports_supported & BIT(report))) {
1060 logi_dj_recv_forward_report(dj_dev, data, size);
1061 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
1062 return;
1063 }
1064 }
1065
1066 logi_dj_recv_queue_unknown_work(djrcv_dev);
1067 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
1068
1069 dbg_hid("No dj-devs handling input report number %d\n", report);
1070}
1071
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001072static int logi_dj_recv_send_report(struct dj_receiver_dev *djrcv_dev,
1073 struct dj_report *dj_report)
1074{
Hans de Goede0ee75542019-04-20 13:21:51 +02001075 struct hid_device *hdev = djrcv_dev->hidpp;
Benjamin Tissoiresdcd90062013-03-05 17:09:00 +01001076 struct hid_report *report;
1077 struct hid_report_enum *output_report_enum;
1078 u8 *data = (u8 *)(&dj_report->device_index);
Kees Cook297502a2013-09-11 21:56:56 +02001079 unsigned int i;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001080
Benjamin Tissoiresdcd90062013-03-05 17:09:00 +01001081 output_report_enum = &hdev->report_enum[HID_OUTPUT_REPORT];
1082 report = output_report_enum->report_id_hash[REPORT_ID_DJ_SHORT];
1083
1084 if (!report) {
Hans de Goedeaca22a32019-04-20 13:21:58 +02001085 hid_err(hdev, "%s: unable to find dj report\n", __func__);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001086 return -ENODEV;
1087 }
1088
Kees Cook297502a2013-09-11 21:56:56 +02001089 for (i = 0; i < DJREPORT_SHORT_LENGTH - 1; i++)
Benjamin Tissoiresdcd90062013-03-05 17:09:00 +01001090 report->field[0]->value[i] = data[i];
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001091
Jiri Kosina83a44ac2013-03-09 10:58:13 +01001092 hid_hw_request(hdev, report, HID_REQ_SET_REPORT);
Benjamin Tissoiresdcd90062013-03-05 17:09:00 +01001093
1094 return 0;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001095}
1096
Hans de Goede74808f92019-04-20 13:21:54 +02001097static int logi_dj_recv_query_hidpp_devices(struct dj_receiver_dev *djrcv_dev)
1098{
1099 const u8 template[] = {REPORT_ID_HIDPP_SHORT,
1100 HIDPP_RECEIVER_INDEX,
1101 HIDPP_SET_REGISTER,
1102 HIDPP_REG_CONNECTION_STATE,
1103 HIDPP_FAKE_DEVICE_ARRIVAL,
1104 0x00, 0x00};
1105 u8 *hidpp_report;
1106 int retval;
1107
1108 hidpp_report = kmemdup(template, sizeof(template), GFP_KERNEL);
1109 if (!hidpp_report)
1110 return -ENOMEM;
1111
1112 retval = hid_hw_raw_request(djrcv_dev->hidpp,
1113 REPORT_ID_HIDPP_SHORT,
1114 hidpp_report, sizeof(template),
1115 HID_OUTPUT_REPORT,
1116 HID_REQ_SET_REPORT);
1117
1118 kfree(hidpp_report);
1119 return 0;
1120}
1121
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001122static int logi_dj_recv_query_paired_devices(struct dj_receiver_dev *djrcv_dev)
1123{
Marc Dionned8dc3492012-06-01 18:12:14 -04001124 struct dj_report *dj_report;
1125 int retval;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001126
Hans de Goedeb6aeedd2019-04-20 13:21:53 +02001127 djrcv_dev->last_query = jiffies;
1128
Hans de Goede74808f92019-04-20 13:21:54 +02001129 if (djrcv_dev->type != recvr_type_dj)
1130 return logi_dj_recv_query_hidpp_devices(djrcv_dev);
1131
Alan Cox8a55ade2012-09-04 15:10:08 +01001132 dj_report = kzalloc(sizeof(struct dj_report), GFP_KERNEL);
Marc Dionned8dc3492012-06-01 18:12:14 -04001133 if (!dj_report)
1134 return -ENOMEM;
1135 dj_report->report_id = REPORT_ID_DJ_SHORT;
1136 dj_report->device_index = 0xFF;
1137 dj_report->report_type = REPORT_TYPE_CMD_GET_PAIRED_DEVICES;
1138 retval = logi_dj_recv_send_report(djrcv_dev, dj_report);
1139 kfree(dj_report);
1140 return retval;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001141}
1142
Nestor Lopez Casadoc63e0e32013-07-18 06:21:30 -07001143
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001144static int logi_dj_recv_switch_to_dj_mode(struct dj_receiver_dev *djrcv_dev,
1145 unsigned timeout)
1146{
Hans de Goede0ee75542019-04-20 13:21:51 +02001147 struct hid_device *hdev = djrcv_dev->hidpp;
Marc Dionned8dc3492012-06-01 18:12:14 -04001148 struct dj_report *dj_report;
Benjamin Tissoires6a9ddc82014-09-30 13:18:31 -04001149 u8 *buf;
Hans de Goede74808f92019-04-20 13:21:54 +02001150 int retval = 0;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001151
Alan Cox8a55ade2012-09-04 15:10:08 +01001152 dj_report = kzalloc(sizeof(struct dj_report), GFP_KERNEL);
Marc Dionned8dc3492012-06-01 18:12:14 -04001153 if (!dj_report)
1154 return -ENOMEM;
Benjamin Tisssoires42c22db2014-01-08 17:18:45 -05001155
Hans de Goede74808f92019-04-20 13:21:54 +02001156 if (djrcv_dev->type == recvr_type_dj) {
1157 dj_report->report_id = REPORT_ID_DJ_SHORT;
1158 dj_report->device_index = 0xFF;
1159 dj_report->report_type = REPORT_TYPE_CMD_SWITCH;
1160 dj_report->report_params[CMD_SWITCH_PARAM_DEVBITFIELD] = 0x3F;
1161 dj_report->report_params[CMD_SWITCH_PARAM_TIMEOUT_SECONDS] =
1162 (u8)timeout;
1163
1164 retval = logi_dj_recv_send_report(djrcv_dev, dj_report);
1165
1166 /*
1167 * Ugly sleep to work around a USB 3.0 bug when the receiver is
1168 * still processing the "switch-to-dj" command while we send an
1169 * other command.
1170 * 50 msec should gives enough time to the receiver to be ready.
1171 */
1172 msleep(50);
1173 }
Benjamin Tisssoires42c22db2014-01-08 17:18:45 -05001174
Benjamin Tissoires6a9ddc82014-09-30 13:18:31 -04001175 /*
1176 * Magical bits to set up hidpp notifications when the dj devices
1177 * are connected/disconnected.
1178 *
1179 * We can reuse dj_report because HIDPP_REPORT_SHORT_LENGTH is smaller
1180 * than DJREPORT_SHORT_LENGTH.
1181 */
1182 buf = (u8 *)dj_report;
1183
1184 memset(buf, 0, HIDPP_REPORT_SHORT_LENGTH);
1185
1186 buf[0] = REPORT_ID_HIDPP_SHORT;
1187 buf[1] = 0xFF;
1188 buf[2] = 0x80;
1189 buf[3] = 0x00;
1190 buf[4] = 0x00;
1191 buf[5] = 0x09;
1192 buf[6] = 0x00;
1193
1194 hid_hw_raw_request(hdev, REPORT_ID_HIDPP_SHORT, buf,
1195 HIDPP_REPORT_SHORT_LENGTH, HID_OUTPUT_REPORT,
1196 HID_REQ_SET_REPORT);
1197
1198 kfree(dj_report);
Marc Dionned8dc3492012-06-01 18:12:14 -04001199 return retval;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001200}
1201
1202
1203static int logi_dj_ll_open(struct hid_device *hid)
1204{
Hans de Goedeaca22a32019-04-20 13:21:58 +02001205 dbg_hid("%s: %s\n", __func__, hid->phys);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001206 return 0;
1207
1208}
1209
1210static void logi_dj_ll_close(struct hid_device *hid)
1211{
Hans de Goedeaca22a32019-04-20 13:21:58 +02001212 dbg_hid("%s: %s\n", __func__, hid->phys);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001213}
1214
Benjamin Tissoires8dba3022017-03-27 16:59:21 +02001215/*
1216 * Register 0xB5 is "pairing information". It is solely intended for the
1217 * receiver, so do not overwrite the device index.
1218 */
Benjamin Tissoiresc0340412019-04-20 13:21:46 +02001219static u8 unifying_pairing_query[] = { REPORT_ID_HIDPP_SHORT,
1220 HIDPP_RECEIVER_INDEX,
1221 HIDPP_GET_LONG_REGISTER,
1222 HIDPP_REG_PAIRING_INFORMATION };
1223static u8 unifying_pairing_answer[] = { REPORT_ID_HIDPP_LONG,
1224 HIDPP_RECEIVER_INDEX,
1225 HIDPP_GET_LONG_REGISTER,
1226 HIDPP_REG_PAIRING_INFORMATION };
Benjamin Tissoires33797822014-09-30 13:18:30 -04001227
Benjamin Tissoiresbd27e202014-02-10 12:58:53 -05001228static int logi_dj_ll_raw_request(struct hid_device *hid,
1229 unsigned char reportnum, __u8 *buf,
1230 size_t count, unsigned char report_type,
1231 int reqtype)
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001232{
Benjamin Tissoires0e40d352014-02-05 16:33:17 -05001233 struct dj_device *djdev = hid->driver_data;
1234 struct dj_receiver_dev *djrcv_dev = djdev->dj_receiver_dev;
1235 u8 *out_buf;
1236 int ret;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001237
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001238 if ((buf[0] == REPORT_ID_HIDPP_SHORT) ||
1239 (buf[0] == REPORT_ID_HIDPP_LONG)) {
1240 if (count < 2)
1241 return -EINVAL;
1242
Benjamin Tissoires33797822014-09-30 13:18:30 -04001243 /* special case where we should not overwrite
1244 * the device_index */
Benjamin Tissoires8dba3022017-03-27 16:59:21 +02001245 if (count == 7 && !memcmp(buf, unifying_pairing_query,
1246 sizeof(unifying_pairing_query)))
1247 buf[4] = (buf[4] & 0xf0) | (djdev->device_index - 1);
Benjamin Tissoires33797822014-09-30 13:18:30 -04001248 else
1249 buf[1] = djdev->device_index;
Hans de Goede0ee75542019-04-20 13:21:51 +02001250 return hid_hw_raw_request(djrcv_dev->hidpp, reportnum, buf,
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001251 count, report_type, reqtype);
1252 }
1253
Benjamin Tissoires0e40d352014-02-05 16:33:17 -05001254 if (buf[0] != REPORT_TYPE_LEDS)
1255 return -EINVAL;
1256
Hans de Goede74808f92019-04-20 13:21:54 +02001257 if (djrcv_dev->type != recvr_type_dj && count >= 2) {
1258 if (!djrcv_dev->keyboard) {
1259 hid_warn(hid, "Received REPORT_TYPE_LEDS request before the keyboard interface was enumerated\n");
1260 return 0;
1261 }
1262 /* usbhid overrides the report ID and ignores the first byte */
1263 return hid_hw_raw_request(djrcv_dev->keyboard, 0, buf, count,
1264 report_type, reqtype);
1265 }
1266
Benjamin Tissoires0e40d352014-02-05 16:33:17 -05001267 out_buf = kzalloc(DJREPORT_SHORT_LENGTH, GFP_ATOMIC);
1268 if (!out_buf)
1269 return -ENOMEM;
1270
Jiri Kosina51217e62014-08-21 09:56:47 -05001271 if (count > DJREPORT_SHORT_LENGTH - 2)
Benjamin Tissoires0e40d352014-02-05 16:33:17 -05001272 count = DJREPORT_SHORT_LENGTH - 2;
1273
1274 out_buf[0] = REPORT_ID_DJ_SHORT;
1275 out_buf[1] = djdev->device_index;
1276 memcpy(out_buf + 2, buf, count);
1277
Hans de Goede0ee75542019-04-20 13:21:51 +02001278 ret = hid_hw_raw_request(djrcv_dev->hidpp, out_buf[0], out_buf,
Benjamin Tissoiresbd27e202014-02-10 12:58:53 -05001279 DJREPORT_SHORT_LENGTH, report_type, reqtype);
Benjamin Tissoires0e40d352014-02-05 16:33:17 -05001280
1281 kfree(out_buf);
1282 return ret;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001283}
1284
Dan Carpenter6d603322013-10-19 12:08:59 +03001285static void rdcat(char *rdesc, unsigned int *rsize, const char *data, unsigned int size)
Henrik Rydberg2a039bf2012-04-22 14:21:39 +02001286{
Dan Carpenter6d603322013-10-19 12:08:59 +03001287 memcpy(rdesc + *rsize, data, size);
Henrik Rydberg2a039bf2012-04-22 14:21:39 +02001288 *rsize += size;
1289}
1290
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001291static int logi_dj_ll_parse(struct hid_device *hid)
1292{
1293 struct dj_device *djdev = hid->driver_data;
Henrik Rydberg2a039bf2012-04-22 14:21:39 +02001294 unsigned int rsize = 0;
1295 char *rdesc;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001296 int retval;
1297
1298 dbg_hid("%s\n", __func__);
1299
1300 djdev->hdev->version = 0x0111;
1301 djdev->hdev->country = 0x00;
1302
Henrik Rydberg2a039bf2012-04-22 14:21:39 +02001303 rdesc = kmalloc(MAX_RDESC_SIZE, GFP_KERNEL);
1304 if (!rdesc)
1305 return -ENOMEM;
1306
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001307 if (djdev->reports_supported & STD_KEYBOARD) {
Hans de Goede6d3c3f02019-04-20 13:22:02 +02001308 dbg_hid("%s: sending a kbd descriptor, reports_supported: %llx\n",
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001309 __func__, djdev->reports_supported);
Dan Carpenter6d603322013-10-19 12:08:59 +03001310 rdcat(rdesc, &rsize, kbd_descriptor, sizeof(kbd_descriptor));
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001311 }
1312
1313 if (djdev->reports_supported & STD_MOUSE) {
Hans de Goede6d3c3f02019-04-20 13:22:02 +02001314 dbg_hid("%s: sending a mouse descriptor, reports_supported: %llx\n",
1315 __func__, djdev->reports_supported);
Benjamin Tissoiresf5fb57a2019-04-20 13:21:55 +02001316 if (djdev->dj_receiver_dev->type == recvr_type_gaming_hidpp)
1317 rdcat(rdesc, &rsize, mse_high_res_descriptor,
1318 sizeof(mse_high_res_descriptor));
Hans de Goedec9121cf2019-04-20 13:21:56 +02001319 else if (djdev->dj_receiver_dev->type == recvr_type_27mhz)
1320 rdcat(rdesc, &rsize, mse_27mhz_descriptor,
1321 sizeof(mse_27mhz_descriptor));
Hans de Goedef2113c32019-04-20 13:22:03 +02001322 else if (djdev->dj_receiver_dev->type == recvr_type_bluetooth)
1323 rdcat(rdesc, &rsize, mse_bluetooth_descriptor,
1324 sizeof(mse_bluetooth_descriptor));
Benjamin Tissoiresf5fb57a2019-04-20 13:21:55 +02001325 else
1326 rdcat(rdesc, &rsize, mse_descriptor,
1327 sizeof(mse_descriptor));
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001328 }
1329
1330 if (djdev->reports_supported & MULTIMEDIA) {
Hans de Goede6d3c3f02019-04-20 13:22:02 +02001331 dbg_hid("%s: sending a multimedia report descriptor: %llx\n",
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001332 __func__, djdev->reports_supported);
Dan Carpenter6d603322013-10-19 12:08:59 +03001333 rdcat(rdesc, &rsize, consumer_descriptor, sizeof(consumer_descriptor));
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001334 }
1335
1336 if (djdev->reports_supported & POWER_KEYS) {
Hans de Goede6d3c3f02019-04-20 13:22:02 +02001337 dbg_hid("%s: sending a power keys report descriptor: %llx\n",
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001338 __func__, djdev->reports_supported);
Dan Carpenter6d603322013-10-19 12:08:59 +03001339 rdcat(rdesc, &rsize, syscontrol_descriptor, sizeof(syscontrol_descriptor));
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001340 }
1341
1342 if (djdev->reports_supported & MEDIA_CENTER) {
Hans de Goede6d3c3f02019-04-20 13:22:02 +02001343 dbg_hid("%s: sending a media center report descriptor: %llx\n",
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001344 __func__, djdev->reports_supported);
Dan Carpenter6d603322013-10-19 12:08:59 +03001345 rdcat(rdesc, &rsize, media_descriptor, sizeof(media_descriptor));
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001346 }
1347
1348 if (djdev->reports_supported & KBD_LEDS) {
Hans de Goede6d3c3f02019-04-20 13:22:02 +02001349 dbg_hid("%s: need to send kbd leds report descriptor: %llx\n",
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001350 __func__, djdev->reports_supported);
1351 }
1352
Hans de Goede6d3c3f02019-04-20 13:22:02 +02001353 if (djdev->reports_supported & HIDPP) {
1354 rdcat(rdesc, &rsize, hidpp_descriptor,
1355 sizeof(hidpp_descriptor));
1356 }
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001357
Henrik Rydberg2a039bf2012-04-22 14:21:39 +02001358 retval = hid_parse_report(hid, rdesc, rsize);
1359 kfree(rdesc);
1360
1361 return retval;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001362}
1363
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001364static int logi_dj_ll_start(struct hid_device *hid)
1365{
1366 dbg_hid("%s\n", __func__);
1367 return 0;
1368}
1369
1370static void logi_dj_ll_stop(struct hid_device *hid)
1371{
1372 dbg_hid("%s\n", __func__);
1373}
1374
1375
1376static struct hid_ll_driver logi_dj_ll_driver = {
1377 .parse = logi_dj_ll_parse,
1378 .start = logi_dj_ll_start,
1379 .stop = logi_dj_ll_stop,
1380 .open = logi_dj_ll_open,
1381 .close = logi_dj_ll_close,
Benjamin Tissoiresbd27e202014-02-10 12:58:53 -05001382 .raw_request = logi_dj_ll_raw_request,
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001383};
1384
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001385static int logi_dj_dj_event(struct hid_device *hdev,
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001386 struct hid_report *report, u8 *data,
1387 int size)
1388{
1389 struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
1390 struct dj_report *dj_report = (struct dj_report *) data;
1391 unsigned long flags;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001392
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001393 /*
1394 * Here we receive all data coming from iface 2, there are 3 cases:
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001395 *
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001396 * 1) Data is intended for this driver i. e. data contains arrival,
1397 * departure, etc notifications, in which case we queue them for delayed
1398 * processing by the work queue. We return 1 to hid-core as no further
1399 * processing is required from it.
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001400 *
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001401 * 2) Data informs a connection change, if the change means rf link
1402 * loss, then we must send a null report to the upper layer to discard
1403 * potentially pressed keys that may be repeated forever by the input
1404 * layer. Return 1 to hid-core as no further processing is required.
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001405 *
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001406 * 3) Data is an actual input event from a paired DJ device in which
1407 * case we forward it to the correct hid device (via hid_input_report()
1408 * ) and return 1 so hid-core does not anything else with it.
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001409 */
Benjamin Tissoires5abfe852014-08-22 16:16:05 -04001410
Jiri Kosinaad3e14d2014-08-21 09:57:17 -05001411 if ((dj_report->device_index < DJ_DEVICE_INDEX_MIN) ||
1412 (dj_report->device_index > DJ_DEVICE_INDEX_MAX)) {
Benjamin Tissoires5abfe852014-08-22 16:16:05 -04001413 /*
1414 * Device index is wrong, bail out.
1415 * This driver can ignore safely the receiver notifications,
1416 * so ignore those reports too.
1417 */
1418 if (dj_report->device_index != DJ_RECEIVER_INDEX)
Hans de Goedeaca22a32019-04-20 13:21:58 +02001419 hid_err(hdev, "%s: invalid device index:%d\n",
Jiri Kosinaad3e14d2014-08-21 09:57:17 -05001420 __func__, dj_report->device_index);
1421 return false;
1422 }
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001423
1424 spin_lock_irqsave(&djrcv_dev->lock, flags);
Benjamin Tissoires368d4e592014-08-22 16:16:06 -04001425
1426 if (!djrcv_dev->paired_dj_devices[dj_report->device_index]) {
1427 /* received an event for an unknown device, bail out */
1428 logi_dj_recv_queue_notification(djrcv_dev, dj_report);
1429 goto out;
1430 }
1431
Benjamin Tissoires5abfe852014-08-22 16:16:05 -04001432 switch (dj_report->report_type) {
1433 case REPORT_TYPE_NOTIF_DEVICE_PAIRED:
Benjamin Tissoires368d4e592014-08-22 16:16:06 -04001434 /* pairing notifications are handled above the switch */
1435 break;
Benjamin Tissoires5abfe852014-08-22 16:16:05 -04001436 case REPORT_TYPE_NOTIF_DEVICE_UNPAIRED:
1437 logi_dj_recv_queue_notification(djrcv_dev, dj_report);
1438 break;
1439 case REPORT_TYPE_NOTIF_CONNECTION_STATUS:
1440 if (dj_report->report_params[CONNECTION_STATUS_PARAM_STATUS] ==
1441 STATUS_LINKLOSS) {
1442 logi_dj_recv_forward_null_report(djrcv_dev, dj_report);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001443 }
Benjamin Tissoires5abfe852014-08-22 16:16:05 -04001444 break;
1445 default:
Benjamin Tissoires83898232019-04-20 13:21:43 +02001446 logi_dj_recv_forward_dj(djrcv_dev, dj_report);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001447 }
Benjamin Tissoires368d4e592014-08-22 16:16:06 -04001448
1449out:
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001450 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
1451
Benjamin Tissoires5abfe852014-08-22 16:16:05 -04001452 return true;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001453}
1454
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001455static int logi_dj_hidpp_event(struct hid_device *hdev,
1456 struct hid_report *report, u8 *data,
1457 int size)
1458{
1459 struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
Benjamin Tissoires7bb56a52019-04-20 13:21:44 +02001460 struct hidpp_event *hidpp_report = (struct hidpp_event *) data;
Hans de Goede74808f92019-04-20 13:21:54 +02001461 struct dj_device *dj_dev;
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001462 unsigned long flags;
Benjamin Tissoires7bb56a52019-04-20 13:21:44 +02001463 u8 device_index = hidpp_report->device_index;
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001464
Benjamin Tissoires33797822014-09-30 13:18:30 -04001465 if (device_index == HIDPP_RECEIVER_INDEX) {
1466 /* special case were the device wants to know its unifying
1467 * name */
1468 if (size == HIDPP_REPORT_LONG_LENGTH &&
Benjamin Tissoires8dba3022017-03-27 16:59:21 +02001469 !memcmp(data, unifying_pairing_answer,
1470 sizeof(unifying_pairing_answer)))
Benjamin Tissoires33797822014-09-30 13:18:30 -04001471 device_index = (data[4] & 0x0F) + 1;
1472 else
1473 return false;
1474 }
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001475
1476 /*
1477 * Data is from the HID++ collection, in this case, we forward the
1478 * data to the corresponding child dj device and return 0 to hid-core
1479 * so he data also goes to the hidraw device of the receiver. This
1480 * allows a user space application to implement the full HID++ routing
1481 * via the receiver.
1482 */
1483
1484 if ((device_index < DJ_DEVICE_INDEX_MIN) ||
1485 (device_index > DJ_DEVICE_INDEX_MAX)) {
1486 /*
1487 * Device index is wrong, bail out.
1488 * This driver can ignore safely the receiver notifications,
1489 * so ignore those reports too.
1490 */
Hans de Goedeaca22a32019-04-20 13:21:58 +02001491 hid_err(hdev, "%s: invalid device index:%d\n", __func__,
1492 hidpp_report->device_index);
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001493 return false;
1494 }
1495
1496 spin_lock_irqsave(&djrcv_dev->lock, flags);
1497
Hans de Goede74808f92019-04-20 13:21:54 +02001498 dj_dev = djrcv_dev->paired_dj_devices[device_index];
Hans de Goedec9121cf2019-04-20 13:21:56 +02001499
1500 /*
1501 * With 27 MHz receivers, we do not get an explicit unpair event,
1502 * remove the old device if the user has paired a *different* device.
1503 */
1504 if (djrcv_dev->type == recvr_type_27mhz && dj_dev &&
1505 hidpp_report->sub_id == REPORT_TYPE_NOTIF_DEVICE_CONNECTED &&
1506 hidpp_report->params[HIDPP_PARAM_PROTO_TYPE] == 0x02 &&
1507 hidpp_report->params[HIDPP_PARAM_27MHZ_DEVID] !=
1508 dj_dev->hdev->product) {
1509 struct dj_workitem workitem = {
1510 .device_index = hidpp_report->device_index,
1511 .type = WORKITEM_TYPE_UNPAIRED,
1512 };
1513 kfifo_in(&djrcv_dev->notif_fifo, &workitem, sizeof(workitem));
1514 /* logi_hidpp_recv_queue_notif will queue the work */
1515 dj_dev = NULL;
1516 }
1517
Hans de Goede74808f92019-04-20 13:21:54 +02001518 if (dj_dev) {
1519 logi_dj_recv_forward_report(dj_dev, data, size);
1520 } else {
1521 if (hidpp_report->sub_id == REPORT_TYPE_NOTIF_DEVICE_CONNECTED)
1522 logi_hidpp_recv_queue_notif(hdev, hidpp_report);
1523 else
1524 logi_dj_recv_queue_unknown_work(djrcv_dev);
1525 }
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001526
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001527 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
1528
1529 return false;
1530}
1531
1532static int logi_dj_raw_event(struct hid_device *hdev,
1533 struct hid_report *report, u8 *data,
1534 int size)
1535{
Hans de Goede74808f92019-04-20 13:21:54 +02001536 struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001537 dbg_hid("%s, size:%d\n", __func__, size);
1538
Hans de Goededa12b222019-04-20 13:21:59 +02001539 if (!djrcv_dev)
1540 return 0;
1541
Hans de Goede74808f92019-04-20 13:21:54 +02001542 if (!hdev->report_enum[HID_INPUT_REPORT].numbered) {
1543
1544 if (djrcv_dev->unnumbered_application == HID_GD_KEYBOARD) {
1545 /*
1546 * For the keyboard, we can reuse the same report by
1547 * using the second byte which is constant in the USB
1548 * HID report descriptor.
1549 */
1550 data[1] = data[0];
1551 data[0] = REPORT_TYPE_KEYBOARD;
1552
1553 logi_dj_recv_forward_input_report(hdev, data, size);
1554
1555 /* restore previous state */
1556 data[0] = data[1];
1557 data[1] = 0;
1558 }
Hans de Goede1f944ac2019-04-20 13:21:57 +02001559 /* The 27 MHz mouse-only receiver sends unnumbered mouse data */
1560 if (djrcv_dev->unnumbered_application == HID_GD_MOUSE &&
1561 size == 6) {
1562 u8 mouse_report[7];
1563
1564 /* Prepend report id */
1565 mouse_report[0] = REPORT_TYPE_MOUSE;
1566 memcpy(mouse_report + 1, data, 6);
1567 logi_dj_recv_forward_input_report(hdev, mouse_report, 7);
1568 }
Hans de Goede74808f92019-04-20 13:21:54 +02001569
1570 return false;
1571 }
1572
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001573 switch (data[0]) {
1574 case REPORT_ID_DJ_SHORT:
Peter Wuf254ae92014-12-16 16:55:21 +01001575 if (size != DJREPORT_SHORT_LENGTH) {
Hans de Goedeaca22a32019-04-20 13:21:58 +02001576 hid_err(hdev, "Short DJ report bad size (%d)", size);
Peter Wuf254ae92014-12-16 16:55:21 +01001577 return false;
1578 }
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001579 return logi_dj_dj_event(hdev, report, data, size);
Benjamin Tissoiresf5fb57a2019-04-20 13:21:55 +02001580 case REPORT_ID_DJ_LONG:
1581 if (size != DJREPORT_LONG_LENGTH) {
Hans de Goedeaca22a32019-04-20 13:21:58 +02001582 hid_err(hdev, "Long DJ report bad size (%d)", size);
Benjamin Tissoiresf5fb57a2019-04-20 13:21:55 +02001583 return false;
1584 }
1585 return logi_dj_dj_event(hdev, report, data, size);
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001586 case REPORT_ID_HIDPP_SHORT:
Peter Wuf254ae92014-12-16 16:55:21 +01001587 if (size != HIDPP_REPORT_SHORT_LENGTH) {
Hans de Goedeaca22a32019-04-20 13:21:58 +02001588 hid_err(hdev, "Short HID++ report bad size (%d)", size);
Peter Wuf254ae92014-12-16 16:55:21 +01001589 return false;
1590 }
1591 return logi_dj_hidpp_event(hdev, report, data, size);
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001592 case REPORT_ID_HIDPP_LONG:
Peter Wuf254ae92014-12-16 16:55:21 +01001593 if (size != HIDPP_REPORT_LONG_LENGTH) {
Hans de Goedeaca22a32019-04-20 13:21:58 +02001594 hid_err(hdev, "Long HID++ report bad size (%d)", size);
Peter Wuf254ae92014-12-16 16:55:21 +01001595 return false;
1596 }
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001597 return logi_dj_hidpp_event(hdev, report, data, size);
1598 }
1599
Hans de Goede74808f92019-04-20 13:21:54 +02001600 logi_dj_recv_forward_input_report(hdev, data, size);
1601
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001602 return false;
1603}
1604
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001605static int logi_dj_probe(struct hid_device *hdev,
1606 const struct hid_device_id *id)
1607{
Benjamin Tissoires82c0beb2019-04-20 13:21:47 +02001608 struct hid_report_enum *rep_enum;
1609 struct hid_report *rep;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001610 struct dj_receiver_dev *djrcv_dev;
Hans de Goededa12b222019-04-20 13:21:59 +02001611 struct usb_interface *intf;
1612 unsigned int no_dj_interfaces = 0;
Benjamin Tissoires82c0beb2019-04-20 13:21:47 +02001613 bool has_hidpp = false;
Hans de Goedea1d97cc2019-04-20 13:21:52 +02001614 unsigned long flags;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001615 int retval;
1616
Benjamin Tissoires82c0beb2019-04-20 13:21:47 +02001617 /*
1618 * Call to usbhid to fetch the HID descriptors of the current
1619 * interface subsequently call to the hid/hid-core to parse the
1620 * fetched descriptors.
1621 */
1622 retval = hid_parse(hdev);
1623 if (retval) {
Hans de Goedeaca22a32019-04-20 13:21:58 +02001624 hid_err(hdev, "%s: parse failed\n", __func__);
Benjamin Tissoires82c0beb2019-04-20 13:21:47 +02001625 return retval;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001626 }
1627
Hans de Goededa12b222019-04-20 13:21:59 +02001628 /*
1629 * Some KVMs add an extra interface for e.g. mouse emulation. If we
1630 * treat these as logitech-dj interfaces then this causes input events
1631 * reported through this extra interface to not be reported correctly.
1632 * To avoid this, we treat these as generic-hid devices.
1633 */
1634 switch (id->driver_data) {
1635 case recvr_type_dj: no_dj_interfaces = 3; break;
1636 case recvr_type_hidpp: no_dj_interfaces = 2; break;
1637 case recvr_type_gaming_hidpp: no_dj_interfaces = 3; break;
1638 case recvr_type_27mhz: no_dj_interfaces = 2; break;
Hans de Goedef2113c32019-04-20 13:22:03 +02001639 case recvr_type_bluetooth: no_dj_interfaces = 2; break;
Hans de Goededa12b222019-04-20 13:21:59 +02001640 }
1641 if (hid_is_using_ll_driver(hdev, &usb_hid_driver)) {
1642 intf = to_usb_interface(hdev->dev.parent);
1643 if (intf && intf->altsetting->desc.bInterfaceNumber >=
1644 no_dj_interfaces) {
1645 hdev->quirks |= HID_QUIRK_INPUT_PER_APP;
1646 return hid_hw_start(hdev, HID_CONNECT_DEFAULT);
1647 }
1648 }
1649
Benjamin Tissoires82c0beb2019-04-20 13:21:47 +02001650 rep_enum = &hdev->report_enum[HID_INPUT_REPORT];
1651
Hans de Goede74808f92019-04-20 13:21:54 +02001652 /* no input reports, bail out */
1653 if (list_empty(&rep_enum->report_list))
1654 return -ENODEV;
1655
Benjamin Tissoires82c0beb2019-04-20 13:21:47 +02001656 /*
1657 * Check for the HID++ application.
1658 * Note: we should theoretically check for HID++ and DJ
1659 * collections, but this will do.
1660 */
1661 list_for_each_entry(rep, &rep_enum->report_list, list) {
1662 if (rep->application == 0xff000001)
1663 has_hidpp = true;
1664 }
1665
1666 /*
1667 * Ignore interfaces without DJ/HID++ collection, they will not carry
1668 * any data, dont create any hid_device for them.
1669 */
Hans de Goede74808f92019-04-20 13:21:54 +02001670 if (!has_hidpp && id->driver_data == recvr_type_dj)
Benjamin Tissoires82c0beb2019-04-20 13:21:47 +02001671 return -ENODEV;
1672
Hans de Goedea1d97cc2019-04-20 13:21:52 +02001673 /* get the current application attached to the node */
1674 rep = list_first_entry(&rep_enum->report_list, struct hid_report, list);
Hans de Goede74808f92019-04-20 13:21:54 +02001675 djrcv_dev = dj_get_receiver_dev(hdev, id->driver_data,
Hans de Goedea1d97cc2019-04-20 13:21:52 +02001676 rep->application, has_hidpp);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001677 if (!djrcv_dev) {
Hans de Goedeaca22a32019-04-20 13:21:58 +02001678 hid_err(hdev, "%s: dj_get_receiver_dev failed\n", __func__);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001679 return -ENOMEM;
1680 }
Kees Cook297502a2013-09-11 21:56:56 +02001681
Hans de Goede74808f92019-04-20 13:21:54 +02001682 if (!rep_enum->numbered)
1683 djrcv_dev->unnumbered_application = rep->application;
1684
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001685 /* Starts the usb device and connects to upper interfaces hiddev and
1686 * hidraw */
Hans de Goede74808f92019-04-20 13:21:54 +02001687 retval = hid_hw_start(hdev, HID_CONNECT_HIDRAW|HID_CONNECT_HIDDEV);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001688 if (retval) {
Hans de Goedeaca22a32019-04-20 13:21:58 +02001689 hid_err(hdev, "%s: hid_hw_start returned error\n", __func__);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001690 goto hid_hw_start_fail;
1691 }
1692
Hans de Goede74808f92019-04-20 13:21:54 +02001693 if (has_hidpp) {
1694 retval = logi_dj_recv_switch_to_dj_mode(djrcv_dev, 0);
1695 if (retval < 0) {
1696 hid_err(hdev, "%s: logi_dj_recv_switch_to_dj_mode returned error:%d\n",
1697 __func__, retval);
1698 goto switch_to_dj_mode_fail;
1699 }
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001700 }
1701
1702 /* This is enabling the polling urb on the IN endpoint */
Benjamin Tissoiresddf75402013-07-12 11:01:02 +02001703 retval = hid_hw_open(hdev);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001704 if (retval < 0) {
Hans de Goedeaca22a32019-04-20 13:21:58 +02001705 hid_err(hdev, "%s: hid_hw_open returned error:%d\n",
Benjamin Tissoiresddf75402013-07-12 11:01:02 +02001706 __func__, retval);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001707 goto llopen_failed;
1708 }
1709
Andrew de los Reyesa9dd22b72013-02-18 09:20:22 -08001710 /* Allow incoming packets to arrive: */
1711 hid_device_io_start(hdev);
1712
Hans de Goede74808f92019-04-20 13:21:54 +02001713 if (has_hidpp) {
1714 spin_lock_irqsave(&djrcv_dev->lock, flags);
1715 djrcv_dev->ready = true;
1716 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
1717 retval = logi_dj_recv_query_paired_devices(djrcv_dev);
1718 if (retval < 0) {
1719 hid_err(hdev, "%s: logi_dj_recv_query_paired_devices error:%d\n",
1720 __func__, retval);
1721 goto logi_dj_recv_query_paired_devices_failed;
1722 }
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001723 }
1724
1725 return retval;
1726
1727logi_dj_recv_query_paired_devices_failed:
Benjamin Tissoiresddf75402013-07-12 11:01:02 +02001728 hid_hw_close(hdev);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001729
1730llopen_failed:
1731switch_to_dj_mode_fail:
1732 hid_hw_stop(hdev);
1733
1734hid_hw_start_fail:
Hans de Goedea1d97cc2019-04-20 13:21:52 +02001735 dj_put_receiver_dev(hdev);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001736 return retval;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001737}
1738
1739#ifdef CONFIG_PM
1740static int logi_dj_reset_resume(struct hid_device *hdev)
1741{
1742 int retval;
1743 struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
1744
Hans de Goededa12b222019-04-20 13:21:59 +02001745 if (!djrcv_dev || djrcv_dev->hidpp != hdev)
Hans de Goede74808f92019-04-20 13:21:54 +02001746 return 0;
1747
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001748 retval = logi_dj_recv_switch_to_dj_mode(djrcv_dev, 0);
1749 if (retval < 0) {
Hans de Goedeaca22a32019-04-20 13:21:58 +02001750 hid_err(hdev, "%s: logi_dj_recv_switch_to_dj_mode returned error:%d\n",
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001751 __func__, retval);
1752 }
1753
1754 return 0;
1755}
1756#endif
1757
1758static void logi_dj_remove(struct hid_device *hdev)
1759{
1760 struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
1761 struct dj_device *dj_dev;
Hans de Goedea1d97cc2019-04-20 13:21:52 +02001762 unsigned long flags;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001763 int i;
1764
1765 dbg_hid("%s\n", __func__);
1766
Hans de Goededa12b222019-04-20 13:21:59 +02001767 if (!djrcv_dev)
1768 return hid_hw_stop(hdev);
1769
Hans de Goedea1d97cc2019-04-20 13:21:52 +02001770 /*
1771 * This ensures that if the work gets requeued from another
1772 * interface of the same receiver it will be a no-op.
1773 */
1774 spin_lock_irqsave(&djrcv_dev->lock, flags);
1775 djrcv_dev->ready = false;
1776 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
1777
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001778 cancel_work_sync(&djrcv_dev->work);
1779
Benjamin Tissoiresddf75402013-07-12 11:01:02 +02001780 hid_hw_close(hdev);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001781 hid_hw_stop(hdev);
1782
Hans de Goedea1d97cc2019-04-20 13:21:52 +02001783 /*
1784 * For proper operation we need access to all interfaces, so we destroy
1785 * the paired devices when we're unbound from any interface.
1786 *
1787 * Note we may still be bound to other interfaces, sharing the same
1788 * djrcv_dev, so we need locking here.
1789 */
Nestor Lopez Casado844580f2011-09-20 15:59:03 +02001790 for (i = 0; i < (DJ_MAX_PAIRED_DEVICES + DJ_DEVICE_INDEX_MIN); i++) {
Hans de Goedea1d97cc2019-04-20 13:21:52 +02001791 spin_lock_irqsave(&djrcv_dev->lock, flags);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001792 dj_dev = djrcv_dev->paired_dj_devices[i];
Hans de Goedea1d97cc2019-04-20 13:21:52 +02001793 djrcv_dev->paired_dj_devices[i] = NULL;
1794 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001795 if (dj_dev != NULL) {
1796 hid_destroy_device(dj_dev->hdev);
1797 kfree(dj_dev);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001798 }
1799 }
1800
Hans de Goedea1d97cc2019-04-20 13:21:52 +02001801 dj_put_receiver_dev(hdev);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001802}
1803
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001804static const struct hid_device_id logi_dj_receivers[] = {
1805 {HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
Hans de Goede74808f92019-04-20 13:21:54 +02001806 USB_DEVICE_ID_LOGITECH_UNIFYING_RECEIVER),
1807 .driver_data = recvr_type_dj},
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001808 {HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
Hans de Goede74808f92019-04-20 13:21:54 +02001809 USB_DEVICE_ID_LOGITECH_UNIFYING_RECEIVER_2),
1810 .driver_data = recvr_type_dj},
1811 { /* Logitech Nano (non DJ) receiver */
1812 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
1813 USB_DEVICE_ID_LOGITECH_NANO_RECEIVER),
1814 .driver_data = recvr_type_hidpp},
1815 { /* Logitech Nano (non DJ) receiver */
1816 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
1817 USB_DEVICE_ID_LOGITECH_NANO_RECEIVER_2),
1818 .driver_data = recvr_type_hidpp},
Benjamin Tissoiresf5fb57a2019-04-20 13:21:55 +02001819 { /* Logitech gaming receiver (0xc539) */
1820 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
1821 USB_DEVICE_ID_LOGITECH_NANO_RECEIVER_GAMING),
1822 .driver_data = recvr_type_gaming_hidpp},
Hans de Goedec9121cf2019-04-20 13:21:56 +02001823 { /* Logitech 27 MHz HID++ 1.0 receiver (0xc517) */
1824 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
1825 USB_DEVICE_ID_S510_RECEIVER_2),
1826 .driver_data = recvr_type_27mhz},
Hans de Goede1f944ac2019-04-20 13:21:57 +02001827 { /* Logitech 27 MHz HID++ 1.0 mouse-only receiver (0xc51b) */
1828 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
1829 USB_DEVICE_ID_LOGITECH_27MHZ_MOUSE_RECEIVER),
1830 .driver_data = recvr_type_27mhz},
Hans de Goedef2113c32019-04-20 13:22:03 +02001831 { /* Logitech MX5000 HID++ / bluetooth receiver keyboard intf. */
1832 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
1833 0xc70e),
1834 .driver_data = recvr_type_bluetooth},
1835 { /* Logitech MX5000 HID++ / bluetooth receiver mouse intf. */
1836 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
1837 0xc70a),
1838 .driver_data = recvr_type_bluetooth},
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001839 {}
1840};
1841
1842MODULE_DEVICE_TABLE(hid, logi_dj_receivers);
1843
1844static struct hid_driver logi_djreceiver_driver = {
1845 .name = "logitech-djreceiver",
1846 .id_table = logi_dj_receivers,
1847 .probe = logi_dj_probe,
1848 .remove = logi_dj_remove,
1849 .raw_event = logi_dj_raw_event,
1850#ifdef CONFIG_PM
1851 .reset_resume = logi_dj_reset_resume,
1852#endif
1853};
1854
Benjamin Tissoiresab94e562014-09-30 13:18:28 -04001855module_hid_driver(logi_djreceiver_driver);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001856
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001857MODULE_LICENSE("GPL");
1858MODULE_AUTHOR("Logitech");
1859MODULE_AUTHOR("Nestor Lopez Casado");
1860MODULE_AUTHOR("nlopezcasad@logitech.com");