blob: e564bff865159af9c7bf4631f52cef9a209d536d [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 Goede3ed224e2019-06-05 14:44:08 +0200116 recvr_type_mouse_only,
Hans de Goedec9121cf2019-04-20 13:21:56 +0200117 recvr_type_27mhz,
Hans de Goedef2113c32019-04-20 13:22:03 +0200118 recvr_type_bluetooth,
Hans de Goede74808f92019-04-20 13:21:54 +0200119};
Benjamin Tissoiresc0340412019-04-20 13:21:46 +0200120
Benjamin Tissoires8cb37462014-09-30 13:18:26 -0400121struct dj_report {
122 u8 report_id;
123 u8 device_index;
124 u8 report_type;
125 u8 report_params[DJREPORT_SHORT_LENGTH - 3];
126};
127
Benjamin Tissoires7bb56a52019-04-20 13:21:44 +0200128struct hidpp_event {
129 u8 report_id;
130 u8 device_index;
131 u8 sub_id;
132 u8 params[HIDPP_REPORT_LONG_LENGTH - 3U];
133} __packed;
134
Benjamin Tissoires8cb37462014-09-30 13:18:26 -0400135struct dj_receiver_dev {
Hans de Goedea1d97cc2019-04-20 13:21:52 +0200136 struct hid_device *mouse;
137 struct hid_device *keyboard;
Hans de Goede0ee75542019-04-20 13:21:51 +0200138 struct hid_device *hidpp;
Benjamin Tissoires8cb37462014-09-30 13:18:26 -0400139 struct dj_device *paired_dj_devices[DJ_MAX_PAIRED_DEVICES +
140 DJ_DEVICE_INDEX_MIN];
Hans de Goedea1d97cc2019-04-20 13:21:52 +0200141 struct list_head list;
142 struct kref kref;
Benjamin Tissoires8cb37462014-09-30 13:18:26 -0400143 struct work_struct work;
144 struct kfifo notif_fifo;
Hans de Goedeb6aeedd2019-04-20 13:21:53 +0200145 unsigned long last_query; /* in jiffies */
Hans de Goedea1d97cc2019-04-20 13:21:52 +0200146 bool ready;
Hans de Goede74808f92019-04-20 13:21:54 +0200147 enum recvr_type type;
148 unsigned int unnumbered_application;
Benjamin Tissoires8cb37462014-09-30 13:18:26 -0400149 spinlock_t lock;
Benjamin Tissoires8cb37462014-09-30 13:18:26 -0400150};
151
152struct dj_device {
153 struct hid_device *hdev;
154 struct dj_receiver_dev *dj_receiver_dev;
Hans de Goede6d3c3f02019-04-20 13:22:02 +0200155 u64 reports_supported;
Benjamin Tissoires8cb37462014-09-30 13:18:26 -0400156 u8 device_index;
157};
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200158
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200159#define WORKITEM_TYPE_EMPTY 0
160#define WORKITEM_TYPE_PAIRED 1
161#define WORKITEM_TYPE_UNPAIRED 2
162#define WORKITEM_TYPE_UNKNOWN 255
163
164struct dj_workitem {
165 u8 type; /* WORKITEM_TYPE_* */
166 u8 device_index;
Hans de Goedede76b1d2019-04-20 13:22:00 +0200167 u8 device_type;
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200168 u8 quad_id_msb;
169 u8 quad_id_lsb;
Hans de Goede6d3c3f02019-04-20 13:22:02 +0200170 u64 reports_supported;
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200171};
172
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200173/* Keyboard descriptor (1) */
174static const char kbd_descriptor[] = {
175 0x05, 0x01, /* USAGE_PAGE (generic Desktop) */
176 0x09, 0x06, /* USAGE (Keyboard) */
177 0xA1, 0x01, /* COLLECTION (Application) */
178 0x85, 0x01, /* REPORT_ID (1) */
179 0x95, 0x08, /* REPORT_COUNT (8) */
180 0x75, 0x01, /* REPORT_SIZE (1) */
181 0x15, 0x00, /* LOGICAL_MINIMUM (0) */
182 0x25, 0x01, /* LOGICAL_MAXIMUM (1) */
183 0x05, 0x07, /* USAGE_PAGE (Keyboard) */
184 0x19, 0xE0, /* USAGE_MINIMUM (Left Control) */
185 0x29, 0xE7, /* USAGE_MAXIMUM (Right GUI) */
186 0x81, 0x02, /* INPUT (Data,Var,Abs) */
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200187 0x95, 0x06, /* REPORT_COUNT (6) */
188 0x75, 0x08, /* REPORT_SIZE (8) */
189 0x15, 0x00, /* LOGICAL_MINIMUM (0) */
190 0x26, 0xFF, 0x00, /* LOGICAL_MAXIMUM (255) */
191 0x05, 0x07, /* USAGE_PAGE (Keyboard) */
192 0x19, 0x00, /* USAGE_MINIMUM (no event) */
193 0x2A, 0xFF, 0x00, /* USAGE_MAXIMUM (reserved) */
194 0x81, 0x00, /* INPUT (Data,Ary,Abs) */
Benjamin Tissoires0e40d352014-02-05 16:33:17 -0500195 0x85, 0x0e, /* REPORT_ID (14) */
196 0x05, 0x08, /* USAGE PAGE (LED page) */
197 0x95, 0x05, /* REPORT COUNT (5) */
198 0x75, 0x01, /* REPORT SIZE (1) */
199 0x15, 0x00, /* LOGICAL_MINIMUM (0) */
200 0x25, 0x01, /* LOGICAL_MAXIMUM (1) */
201 0x19, 0x01, /* USAGE MINIMUM (1) */
202 0x29, 0x05, /* USAGE MAXIMUM (5) */
203 0x91, 0x02, /* OUTPUT (Data, Variable, Absolute) */
204 0x95, 0x01, /* REPORT COUNT (1) */
205 0x75, 0x03, /* REPORT SIZE (3) */
206 0x91, 0x01, /* OUTPUT (Constant) */
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200207 0xC0
208};
209
210/* Mouse descriptor (2) */
211static const char mse_descriptor[] = {
212 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */
213 0x09, 0x02, /* USAGE (Mouse) */
214 0xA1, 0x01, /* COLLECTION (Application) */
215 0x85, 0x02, /* REPORT_ID = 2 */
216 0x09, 0x01, /* USAGE (pointer) */
217 0xA1, 0x00, /* COLLECTION (physical) */
218 0x05, 0x09, /* USAGE_PAGE (buttons) */
219 0x19, 0x01, /* USAGE_MIN (1) */
220 0x29, 0x10, /* USAGE_MAX (16) */
221 0x15, 0x00, /* LOGICAL_MIN (0) */
222 0x25, 0x01, /* LOGICAL_MAX (1) */
223 0x95, 0x10, /* REPORT_COUNT (16) */
224 0x75, 0x01, /* REPORT_SIZE (1) */
225 0x81, 0x02, /* INPUT (data var abs) */
226 0x05, 0x01, /* USAGE_PAGE (generic desktop) */
227 0x16, 0x01, 0xF8, /* LOGICAL_MIN (-2047) */
228 0x26, 0xFF, 0x07, /* LOGICAL_MAX (2047) */
229 0x75, 0x0C, /* REPORT_SIZE (12) */
230 0x95, 0x02, /* REPORT_COUNT (2) */
231 0x09, 0x30, /* USAGE (X) */
232 0x09, 0x31, /* USAGE (Y) */
233 0x81, 0x06, /* INPUT */
234 0x15, 0x81, /* LOGICAL_MIN (-127) */
235 0x25, 0x7F, /* LOGICAL_MAX (127) */
236 0x75, 0x08, /* REPORT_SIZE (8) */
237 0x95, 0x01, /* REPORT_COUNT (1) */
238 0x09, 0x38, /* USAGE (wheel) */
239 0x81, 0x06, /* INPUT */
240 0x05, 0x0C, /* USAGE_PAGE(consumer) */
241 0x0A, 0x38, 0x02, /* USAGE(AC Pan) */
242 0x95, 0x01, /* REPORT_COUNT (1) */
243 0x81, 0x06, /* INPUT */
244 0xC0, /* END_COLLECTION */
245 0xC0, /* END_COLLECTION */
246};
247
Hans de Goedec9121cf2019-04-20 13:21:56 +0200248/* Mouse descriptor (2) for 27 MHz receiver, only 8 buttons */
249static const char mse_27mhz_descriptor[] = {
250 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */
251 0x09, 0x02, /* USAGE (Mouse) */
252 0xA1, 0x01, /* COLLECTION (Application) */
253 0x85, 0x02, /* REPORT_ID = 2 */
254 0x09, 0x01, /* USAGE (pointer) */
255 0xA1, 0x00, /* COLLECTION (physical) */
256 0x05, 0x09, /* USAGE_PAGE (buttons) */
257 0x19, 0x01, /* USAGE_MIN (1) */
258 0x29, 0x08, /* USAGE_MAX (8) */
259 0x15, 0x00, /* LOGICAL_MIN (0) */
260 0x25, 0x01, /* LOGICAL_MAX (1) */
261 0x95, 0x08, /* REPORT_COUNT (8) */
262 0x75, 0x01, /* REPORT_SIZE (1) */
263 0x81, 0x02, /* INPUT (data var abs) */
264 0x05, 0x01, /* USAGE_PAGE (generic desktop) */
265 0x16, 0x01, 0xF8, /* LOGICAL_MIN (-2047) */
266 0x26, 0xFF, 0x07, /* LOGICAL_MAX (2047) */
267 0x75, 0x0C, /* REPORT_SIZE (12) */
268 0x95, 0x02, /* REPORT_COUNT (2) */
269 0x09, 0x30, /* USAGE (X) */
270 0x09, 0x31, /* USAGE (Y) */
271 0x81, 0x06, /* INPUT */
272 0x15, 0x81, /* LOGICAL_MIN (-127) */
273 0x25, 0x7F, /* LOGICAL_MAX (127) */
274 0x75, 0x08, /* REPORT_SIZE (8) */
275 0x95, 0x01, /* REPORT_COUNT (1) */
276 0x09, 0x38, /* USAGE (wheel) */
277 0x81, 0x06, /* INPUT */
278 0x05, 0x0C, /* USAGE_PAGE(consumer) */
279 0x0A, 0x38, 0x02, /* USAGE(AC Pan) */
280 0x95, 0x01, /* REPORT_COUNT (1) */
281 0x81, 0x06, /* INPUT */
282 0xC0, /* END_COLLECTION */
283 0xC0, /* END_COLLECTION */
284};
285
Hans de Goedef2113c32019-04-20 13:22:03 +0200286/* Mouse descriptor (2) for Bluetooth receiver, low-res hwheel, 12 buttons */
287static const char mse_bluetooth_descriptor[] = {
288 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */
289 0x09, 0x02, /* USAGE (Mouse) */
290 0xA1, 0x01, /* COLLECTION (Application) */
291 0x85, 0x02, /* REPORT_ID = 2 */
292 0x09, 0x01, /* USAGE (pointer) */
293 0xA1, 0x00, /* COLLECTION (physical) */
294 0x05, 0x09, /* USAGE_PAGE (buttons) */
295 0x19, 0x01, /* USAGE_MIN (1) */
296 0x29, 0x08, /* USAGE_MAX (8) */
297 0x15, 0x00, /* LOGICAL_MIN (0) */
298 0x25, 0x01, /* LOGICAL_MAX (1) */
299 0x95, 0x08, /* REPORT_COUNT (8) */
300 0x75, 0x01, /* REPORT_SIZE (1) */
301 0x81, 0x02, /* INPUT (data var abs) */
302 0x05, 0x01, /* USAGE_PAGE (generic desktop) */
303 0x16, 0x01, 0xF8, /* LOGICAL_MIN (-2047) */
304 0x26, 0xFF, 0x07, /* LOGICAL_MAX (2047) */
305 0x75, 0x0C, /* REPORT_SIZE (12) */
306 0x95, 0x02, /* REPORT_COUNT (2) */
307 0x09, 0x30, /* USAGE (X) */
308 0x09, 0x31, /* USAGE (Y) */
309 0x81, 0x06, /* INPUT */
310 0x15, 0x81, /* LOGICAL_MIN (-127) */
311 0x25, 0x7F, /* LOGICAL_MAX (127) */
312 0x75, 0x08, /* REPORT_SIZE (8) */
313 0x95, 0x01, /* REPORT_COUNT (1) */
314 0x09, 0x38, /* USAGE (wheel) */
315 0x81, 0x06, /* INPUT */
316 0x05, 0x0C, /* USAGE_PAGE(consumer) */
317 0x0A, 0x38, 0x02, /* USAGE(AC Pan) */
318 0x15, 0xF9, /* LOGICAL_MIN (-7) */
319 0x25, 0x07, /* LOGICAL_MAX (7) */
320 0x75, 0x04, /* REPORT_SIZE (4) */
321 0x95, 0x01, /* REPORT_COUNT (1) */
322 0x81, 0x06, /* INPUT */
323 0x05, 0x09, /* USAGE_PAGE (buttons) */
324 0x19, 0x09, /* USAGE_MIN (9) */
325 0x29, 0x0C, /* USAGE_MAX (12) */
326 0x15, 0x00, /* LOGICAL_MIN (0) */
327 0x25, 0x01, /* LOGICAL_MAX (1) */
328 0x75, 0x01, /* REPORT_SIZE (1) */
329 0x95, 0x04, /* REPORT_COUNT (4) */
330 0x81, 0x06, /* INPUT */
331 0xC0, /* END_COLLECTION */
332 0xC0, /* END_COLLECTION */
333};
334
Benjamin Tissoiresf5fb57a2019-04-20 13:21:55 +0200335/* Gaming Mouse descriptor (2) */
336static const char mse_high_res_descriptor[] = {
337 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */
338 0x09, 0x02, /* USAGE (Mouse) */
339 0xA1, 0x01, /* COLLECTION (Application) */
340 0x85, 0x02, /* REPORT_ID = 2 */
341 0x09, 0x01, /* USAGE (pointer) */
342 0xA1, 0x00, /* COLLECTION (physical) */
343 0x05, 0x09, /* USAGE_PAGE (buttons) */
344 0x19, 0x01, /* USAGE_MIN (1) */
345 0x29, 0x10, /* USAGE_MAX (16) */
346 0x15, 0x00, /* LOGICAL_MIN (0) */
347 0x25, 0x01, /* LOGICAL_MAX (1) */
348 0x95, 0x10, /* REPORT_COUNT (16) */
349 0x75, 0x01, /* REPORT_SIZE (1) */
350 0x81, 0x02, /* INPUT (data var abs) */
351 0x05, 0x01, /* USAGE_PAGE (generic desktop) */
352 0x16, 0x01, 0x80, /* LOGICAL_MIN (-32767) */
353 0x26, 0xFF, 0x7F, /* LOGICAL_MAX (32767) */
354 0x75, 0x10, /* REPORT_SIZE (16) */
355 0x95, 0x02, /* REPORT_COUNT (2) */
356 0x09, 0x30, /* USAGE (X) */
357 0x09, 0x31, /* USAGE (Y) */
358 0x81, 0x06, /* INPUT */
359 0x15, 0x81, /* LOGICAL_MIN (-127) */
360 0x25, 0x7F, /* LOGICAL_MAX (127) */
361 0x75, 0x08, /* REPORT_SIZE (8) */
362 0x95, 0x01, /* REPORT_COUNT (1) */
363 0x09, 0x38, /* USAGE (wheel) */
364 0x81, 0x06, /* INPUT */
365 0x05, 0x0C, /* USAGE_PAGE(consumer) */
366 0x0A, 0x38, 0x02, /* USAGE(AC Pan) */
367 0x95, 0x01, /* REPORT_COUNT (1) */
368 0x81, 0x06, /* INPUT */
369 0xC0, /* END_COLLECTION */
370 0xC0, /* END_COLLECTION */
371};
372
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200373/* Consumer Control descriptor (3) */
374static const char consumer_descriptor[] = {
375 0x05, 0x0C, /* USAGE_PAGE (Consumer Devices) */
376 0x09, 0x01, /* USAGE (Consumer Control) */
377 0xA1, 0x01, /* COLLECTION (Application) */
378 0x85, 0x03, /* REPORT_ID = 3 */
379 0x75, 0x10, /* REPORT_SIZE (16) */
380 0x95, 0x02, /* REPORT_COUNT (2) */
381 0x15, 0x01, /* LOGICAL_MIN (1) */
382 0x26, 0x8C, 0x02, /* LOGICAL_MAX (652) */
383 0x19, 0x01, /* USAGE_MIN (1) */
384 0x2A, 0x8C, 0x02, /* USAGE_MAX (652) */
385 0x81, 0x00, /* INPUT (Data Ary Abs) */
386 0xC0, /* END_COLLECTION */
387}; /* */
388
389/* System control descriptor (4) */
390static const char syscontrol_descriptor[] = {
391 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */
392 0x09, 0x80, /* USAGE (System Control) */
393 0xA1, 0x01, /* COLLECTION (Application) */
394 0x85, 0x04, /* REPORT_ID = 4 */
395 0x75, 0x02, /* REPORT_SIZE (2) */
396 0x95, 0x01, /* REPORT_COUNT (1) */
397 0x15, 0x01, /* LOGICAL_MIN (1) */
398 0x25, 0x03, /* LOGICAL_MAX (3) */
399 0x09, 0x82, /* USAGE (System Sleep) */
400 0x09, 0x81, /* USAGE (System Power Down) */
401 0x09, 0x83, /* USAGE (System Wake Up) */
402 0x81, 0x60, /* INPUT (Data Ary Abs NPrf Null) */
403 0x75, 0x06, /* REPORT_SIZE (6) */
404 0x81, 0x03, /* INPUT (Cnst Var Abs) */
405 0xC0, /* END_COLLECTION */
406};
407
408/* Media descriptor (8) */
409static const char media_descriptor[] = {
410 0x06, 0xbc, 0xff, /* Usage Page 0xffbc */
411 0x09, 0x88, /* Usage 0x0088 */
412 0xa1, 0x01, /* BeginCollection */
413 0x85, 0x08, /* Report ID 8 */
414 0x19, 0x01, /* Usage Min 0x0001 */
415 0x29, 0xff, /* Usage Max 0x00ff */
416 0x15, 0x01, /* Logical Min 1 */
417 0x26, 0xff, 0x00, /* Logical Max 255 */
418 0x75, 0x08, /* Report Size 8 */
419 0x95, 0x01, /* Report Count 1 */
420 0x81, 0x00, /* Input */
421 0xc0, /* EndCollection */
422}; /* */
423
Benjamin Tissoires925f0f32014-09-30 13:18:29 -0400424/* HIDPP descriptor */
425static const char hidpp_descriptor[] = {
426 0x06, 0x00, 0xff, /* Usage Page (Vendor Defined Page 1) */
427 0x09, 0x01, /* Usage (Vendor Usage 1) */
428 0xa1, 0x01, /* Collection (Application) */
429 0x85, 0x10, /* Report ID (16) */
430 0x75, 0x08, /* Report Size (8) */
431 0x95, 0x06, /* Report Count (6) */
432 0x15, 0x00, /* Logical Minimum (0) */
433 0x26, 0xff, 0x00, /* Logical Maximum (255) */
434 0x09, 0x01, /* Usage (Vendor Usage 1) */
435 0x81, 0x00, /* Input (Data,Arr,Abs) */
436 0x09, 0x01, /* Usage (Vendor Usage 1) */
437 0x91, 0x00, /* Output (Data,Arr,Abs) */
438 0xc0, /* End Collection */
439 0x06, 0x00, 0xff, /* Usage Page (Vendor Defined Page 1) */
440 0x09, 0x02, /* Usage (Vendor Usage 2) */
441 0xa1, 0x01, /* Collection (Application) */
442 0x85, 0x11, /* Report ID (17) */
443 0x75, 0x08, /* Report Size (8) */
444 0x95, 0x13, /* Report Count (19) */
445 0x15, 0x00, /* Logical Minimum (0) */
446 0x26, 0xff, 0x00, /* Logical Maximum (255) */
447 0x09, 0x02, /* Usage (Vendor Usage 2) */
448 0x81, 0x00, /* Input (Data,Arr,Abs) */
449 0x09, 0x02, /* Usage (Vendor Usage 2) */
450 0x91, 0x00, /* Output (Data,Arr,Abs) */
451 0xc0, /* End Collection */
452 0x06, 0x00, 0xff, /* Usage Page (Vendor Defined Page 1) */
453 0x09, 0x04, /* Usage (Vendor Usage 0x04) */
454 0xa1, 0x01, /* Collection (Application) */
455 0x85, 0x20, /* Report ID (32) */
456 0x75, 0x08, /* Report Size (8) */
457 0x95, 0x0e, /* Report Count (14) */
458 0x15, 0x00, /* Logical Minimum (0) */
459 0x26, 0xff, 0x00, /* Logical Maximum (255) */
460 0x09, 0x41, /* Usage (Vendor Usage 0x41) */
461 0x81, 0x00, /* Input (Data,Arr,Abs) */
462 0x09, 0x41, /* Usage (Vendor Usage 0x41) */
463 0x91, 0x00, /* Output (Data,Arr,Abs) */
464 0x85, 0x21, /* Report ID (33) */
465 0x95, 0x1f, /* Report Count (31) */
466 0x15, 0x00, /* Logical Minimum (0) */
467 0x26, 0xff, 0x00, /* Logical Maximum (255) */
468 0x09, 0x42, /* Usage (Vendor Usage 0x42) */
469 0x81, 0x00, /* Input (Data,Arr,Abs) */
470 0x09, 0x42, /* Usage (Vendor Usage 0x42) */
471 0x91, 0x00, /* Output (Data,Arr,Abs) */
472 0xc0, /* End Collection */
473};
474
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200475/* Maximum size of all defined hid reports in bytes (including report id) */
476#define MAX_REPORT_SIZE 8
477
Henrik Rydberg2a039bf2012-04-22 14:21:39 +0200478/* Make sure all descriptors are present here */
479#define MAX_RDESC_SIZE \
480 (sizeof(kbd_descriptor) + \
Hans de Goedef2113c32019-04-20 13:22:03 +0200481 sizeof(mse_bluetooth_descriptor) + \
Henrik Rydberg2a039bf2012-04-22 14:21:39 +0200482 sizeof(consumer_descriptor) + \
483 sizeof(syscontrol_descriptor) + \
Benjamin Tissoires925f0f32014-09-30 13:18:29 -0400484 sizeof(media_descriptor) + \
485 sizeof(hidpp_descriptor))
Henrik Rydberg2a039bf2012-04-22 14:21:39 +0200486
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200487/* Number of possible hid report types that can be created by this driver.
488 *
489 * Right now, RF report types have the same report types (or report id's)
490 * than the hid report created from those RF reports. In the future
491 * this doesnt have to be true.
492 *
493 * For instance, RF report type 0x01 which has a size of 8 bytes, corresponds
494 * to hid report id 0x01, this is standard keyboard. Same thing applies to mice
495 * reports and consumer control, etc. If a new RF report is created, it doesn't
496 * has to have the same report id as its corresponding hid report, so an
497 * translation may have to take place for future report types.
498 */
499#define NUMBER_OF_HID_REPORTS 32
500static const u8 hid_reportid_size_map[NUMBER_OF_HID_REPORTS] = {
501 [1] = 8, /* Standard keyboard */
502 [2] = 8, /* Standard mouse */
503 [3] = 5, /* Consumer control */
504 [4] = 2, /* System control */
505 [8] = 2, /* Media Center */
506};
507
508
509#define LOGITECH_DJ_INTERFACE_NUMBER 0x02
510
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200511static struct hid_ll_driver logi_dj_ll_driver;
512
Nestor Lopez Casadoc63e0e32013-07-18 06:21:30 -0700513static int logi_dj_recv_query_paired_devices(struct dj_receiver_dev *djrcv_dev);
Hans de Goedea1d97cc2019-04-20 13:21:52 +0200514static void delayedwork_callback(struct work_struct *work);
515
516static LIST_HEAD(dj_hdev_list);
517static DEFINE_MUTEX(dj_hdev_list_lock);
518
519/*
520 * dj/HID++ receivers are really a single logical entity, but for BIOS/Windows
521 * compatibility they have multiple USB interfaces. On HID++ receivers we need
522 * to listen for input reports on both interfaces. The functions below are used
523 * to create a single struct dj_receiver_dev for all interfaces belonging to
524 * a single USB-device / receiver.
525 */
Hans de Goedef2113c32019-04-20 13:22:03 +0200526static struct dj_receiver_dev *dj_find_receiver_dev(struct hid_device *hdev,
527 enum recvr_type type)
Hans de Goedea1d97cc2019-04-20 13:21:52 +0200528{
529 struct dj_receiver_dev *djrcv_dev;
Hans de Goedef2113c32019-04-20 13:22:03 +0200530 char sep;
531
532 /*
533 * The bluetooth receiver contains a built-in hub and has separate
534 * USB-devices for the keyboard and mouse interfaces.
535 */
536 sep = (type == recvr_type_bluetooth) ? '.' : '/';
Hans de Goedea1d97cc2019-04-20 13:21:52 +0200537
538 /* Try to find an already-probed interface from the same device */
539 list_for_each_entry(djrcv_dev, &dj_hdev_list, list) {
540 if (djrcv_dev->mouse &&
Hans de Goedef2113c32019-04-20 13:22:03 +0200541 hid_compare_device_paths(hdev, djrcv_dev->mouse, sep)) {
Hans de Goedea1d97cc2019-04-20 13:21:52 +0200542 kref_get(&djrcv_dev->kref);
543 return djrcv_dev;
544 }
545 if (djrcv_dev->keyboard &&
Hans de Goedef2113c32019-04-20 13:22:03 +0200546 hid_compare_device_paths(hdev, djrcv_dev->keyboard, sep)) {
Hans de Goedea1d97cc2019-04-20 13:21:52 +0200547 kref_get(&djrcv_dev->kref);
548 return djrcv_dev;
549 }
550 if (djrcv_dev->hidpp &&
Hans de Goedef2113c32019-04-20 13:22:03 +0200551 hid_compare_device_paths(hdev, djrcv_dev->hidpp, sep)) {
Hans de Goedea1d97cc2019-04-20 13:21:52 +0200552 kref_get(&djrcv_dev->kref);
553 return djrcv_dev;
554 }
555 }
556
557 return NULL;
558}
559
560static void dj_release_receiver_dev(struct kref *kref)
561{
562 struct dj_receiver_dev *djrcv_dev = container_of(kref, struct dj_receiver_dev, kref);
563
564 list_del(&djrcv_dev->list);
565 kfifo_free(&djrcv_dev->notif_fifo);
566 kfree(djrcv_dev);
567}
568
569static void dj_put_receiver_dev(struct hid_device *hdev)
570{
571 struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
572
573 mutex_lock(&dj_hdev_list_lock);
574
575 if (djrcv_dev->mouse == hdev)
576 djrcv_dev->mouse = NULL;
577 if (djrcv_dev->keyboard == hdev)
578 djrcv_dev->keyboard = NULL;
579 if (djrcv_dev->hidpp == hdev)
580 djrcv_dev->hidpp = NULL;
581
582 kref_put(&djrcv_dev->kref, dj_release_receiver_dev);
583
584 mutex_unlock(&dj_hdev_list_lock);
585}
586
587static struct dj_receiver_dev *dj_get_receiver_dev(struct hid_device *hdev,
Hans de Goede74808f92019-04-20 13:21:54 +0200588 enum recvr_type type,
Hans de Goedea1d97cc2019-04-20 13:21:52 +0200589 unsigned int application,
590 bool is_hidpp)
591{
592 struct dj_receiver_dev *djrcv_dev;
593
594 mutex_lock(&dj_hdev_list_lock);
595
Hans de Goedef2113c32019-04-20 13:22:03 +0200596 djrcv_dev = dj_find_receiver_dev(hdev, type);
Hans de Goedea1d97cc2019-04-20 13:21:52 +0200597 if (!djrcv_dev) {
598 djrcv_dev = kzalloc(sizeof(*djrcv_dev), GFP_KERNEL);
599 if (!djrcv_dev)
600 goto out;
601
602 INIT_WORK(&djrcv_dev->work, delayedwork_callback);
603 spin_lock_init(&djrcv_dev->lock);
604 if (kfifo_alloc(&djrcv_dev->notif_fifo,
605 DJ_MAX_NUMBER_NOTIFS * sizeof(struct dj_workitem),
606 GFP_KERNEL)) {
607 kfree(djrcv_dev);
608 djrcv_dev = NULL;
609 goto out;
610 }
611 kref_init(&djrcv_dev->kref);
612 list_add_tail(&djrcv_dev->list, &dj_hdev_list);
Hans de Goedeb6aeedd2019-04-20 13:21:53 +0200613 djrcv_dev->last_query = jiffies;
Hans de Goede74808f92019-04-20 13:21:54 +0200614 djrcv_dev->type = type;
Hans de Goedea1d97cc2019-04-20 13:21:52 +0200615 }
616
617 if (application == HID_GD_KEYBOARD)
618 djrcv_dev->keyboard = hdev;
619 if (application == HID_GD_MOUSE)
620 djrcv_dev->mouse = hdev;
621 if (is_hidpp)
622 djrcv_dev->hidpp = hdev;
623
624 hid_set_drvdata(hdev, djrcv_dev);
625out:
626 mutex_unlock(&dj_hdev_list_lock);
627 return djrcv_dev;
628}
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200629
630static void logi_dj_recv_destroy_djhid_device(struct dj_receiver_dev *djrcv_dev,
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200631 struct dj_workitem *workitem)
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200632{
633 /* Called in delayed work context */
634 struct dj_device *dj_dev;
635 unsigned long flags;
636
637 spin_lock_irqsave(&djrcv_dev->lock, flags);
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200638 dj_dev = djrcv_dev->paired_dj_devices[workitem->device_index];
639 djrcv_dev->paired_dj_devices[workitem->device_index] = NULL;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200640 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
641
642 if (dj_dev != NULL) {
643 hid_destroy_device(dj_dev->hdev);
644 kfree(dj_dev);
645 } else {
Hans de Goede0ee75542019-04-20 13:21:51 +0200646 hid_err(djrcv_dev->hidpp, "%s: can't destroy a NULL device\n",
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200647 __func__);
648 }
649}
650
651static void logi_dj_recv_add_djhid_device(struct dj_receiver_dev *djrcv_dev,
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200652 struct dj_workitem *workitem)
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200653{
654 /* Called in delayed work context */
Hans de Goede0ee75542019-04-20 13:21:51 +0200655 struct hid_device *djrcv_hdev = djrcv_dev->hidpp;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200656 struct hid_device *dj_hiddev;
657 struct dj_device *dj_dev;
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200658 u8 device_index = workitem->device_index;
Hans de Goedef41d7662019-04-20 13:21:50 +0200659 unsigned long flags;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200660
661 /* Device index goes from 1 to 6, we need 3 bytes to store the
662 * semicolon, the index, and a null terminator
663 */
664 unsigned char tmpstr[3];
665
Hans de Goedef41d7662019-04-20 13:21:50 +0200666 /* We are the only one ever adding a device, no need to lock */
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200667 if (djrcv_dev->paired_dj_devices[device_index]) {
Nestor Lopez Casadoc63e0e32013-07-18 06:21:30 -0700668 /* The device is already known. No need to reallocate it. */
669 dbg_hid("%s: device is already known\n", __func__);
670 return;
671 }
672
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200673 dj_hiddev = hid_allocate_device();
674 if (IS_ERR(dj_hiddev)) {
Hans de Goedeaca22a32019-04-20 13:21:58 +0200675 hid_err(djrcv_hdev, "%s: hid_allocate_dev failed\n", __func__);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200676 return;
677 }
678
679 dj_hiddev->ll_driver = &logi_dj_ll_driver;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200680
681 dj_hiddev->dev.parent = &djrcv_hdev->dev;
682 dj_hiddev->bus = BUS_USB;
Benjamin Tissoires82c0beb2019-04-20 13:21:47 +0200683 dj_hiddev->vendor = djrcv_hdev->vendor;
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200684 dj_hiddev->product = (workitem->quad_id_msb << 8) |
685 workitem->quad_id_lsb;
Hans de Goedede76b1d2019-04-20 13:22:00 +0200686 if (workitem->device_type) {
687 const char *type_str = "Device";
688
689 switch (workitem->device_type) {
690 case 0x01: type_str = "Keyboard"; break;
691 case 0x02: type_str = "Mouse"; break;
692 case 0x03: type_str = "Numpad"; break;
693 case 0x04: type_str = "Presenter"; break;
694 case 0x07: type_str = "Remote Control"; break;
695 case 0x08: type_str = "Trackball"; break;
696 case 0x09: type_str = "Touchpad"; break;
697 }
698 snprintf(dj_hiddev->name, sizeof(dj_hiddev->name),
699 "Logitech Wireless %s PID:%04x",
700 type_str, dj_hiddev->product);
701 } else {
702 snprintf(dj_hiddev->name, sizeof(dj_hiddev->name),
703 "Logitech Unifying Device. Wireless PID:%04x",
704 dj_hiddev->product);
705 }
Benjamin Tissoiresd6102742014-09-30 13:18:25 -0400706
Hans de Goedec9121cf2019-04-20 13:21:56 +0200707 if (djrcv_dev->type == recvr_type_27mhz)
708 dj_hiddev->group = HID_GROUP_LOGITECH_27MHZ_DEVICE;
709 else
710 dj_hiddev->group = HID_GROUP_LOGITECH_DJ_DEVICE;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200711
Benjamin Tissoires82c0beb2019-04-20 13:21:47 +0200712 memcpy(dj_hiddev->phys, djrcv_hdev->phys, sizeof(djrcv_hdev->phys));
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200713 snprintf(tmpstr, sizeof(tmpstr), ":%d", device_index);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200714 strlcat(dj_hiddev->phys, tmpstr, sizeof(dj_hiddev->phys));
715
716 dj_dev = kzalloc(sizeof(struct dj_device), GFP_KERNEL);
717
718 if (!dj_dev) {
Hans de Goedeaca22a32019-04-20 13:21:58 +0200719 hid_err(djrcv_hdev, "%s: failed allocating dj_dev\n", __func__);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200720 goto dj_device_allocate_fail;
721 }
722
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200723 dj_dev->reports_supported = workitem->reports_supported;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200724 dj_dev->hdev = dj_hiddev;
725 dj_dev->dj_receiver_dev = djrcv_dev;
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200726 dj_dev->device_index = device_index;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200727 dj_hiddev->driver_data = dj_dev;
728
Hans de Goedef41d7662019-04-20 13:21:50 +0200729 spin_lock_irqsave(&djrcv_dev->lock, flags);
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200730 djrcv_dev->paired_dj_devices[device_index] = dj_dev;
Hans de Goedef41d7662019-04-20 13:21:50 +0200731 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200732
733 if (hid_add_device(dj_hiddev)) {
Hans de Goedeaca22a32019-04-20 13:21:58 +0200734 hid_err(djrcv_hdev, "%s: failed adding dj_device\n", __func__);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200735 goto hid_add_device_fail;
736 }
737
738 return;
739
740hid_add_device_fail:
Hans de Goedef41d7662019-04-20 13:21:50 +0200741 spin_lock_irqsave(&djrcv_dev->lock, flags);
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200742 djrcv_dev->paired_dj_devices[device_index] = NULL;
Hans de Goedef41d7662019-04-20 13:21:50 +0200743 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200744 kfree(dj_dev);
745dj_device_allocate_fail:
746 hid_destroy_device(dj_hiddev);
747}
748
749static void delayedwork_callback(struct work_struct *work)
750{
751 struct dj_receiver_dev *djrcv_dev =
752 container_of(work, struct dj_receiver_dev, work);
753
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200754 struct dj_workitem workitem;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200755 unsigned long flags;
756 int count;
Nestor Lopez Casadoc63e0e32013-07-18 06:21:30 -0700757 int retval;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200758
759 dbg_hid("%s\n", __func__);
760
761 spin_lock_irqsave(&djrcv_dev->lock, flags);
762
Hans de Goedea1d97cc2019-04-20 13:21:52 +0200763 /*
764 * Since we attach to multiple interfaces, we may get scheduled before
765 * we are bound to the HID++ interface, catch this.
766 */
767 if (!djrcv_dev->ready) {
768 pr_warn("%s: delayedwork queued before hidpp interface was enumerated\n",
769 __func__);
770 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
771 return;
772 }
773
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200774 count = kfifo_out(&djrcv_dev->notif_fifo, &workitem, sizeof(workitem));
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200775
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200776 if (count != sizeof(workitem)) {
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200777 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
778 return;
779 }
780
Hans de Goedee316aa62019-04-20 13:22:01 +0200781 if (!kfifo_is_empty(&djrcv_dev->notif_fifo))
782 schedule_work(&djrcv_dev->work);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200783
784 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
785
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200786 switch (workitem.type) {
787 case WORKITEM_TYPE_PAIRED:
788 logi_dj_recv_add_djhid_device(djrcv_dev, &workitem);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200789 break;
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200790 case WORKITEM_TYPE_UNPAIRED:
791 logi_dj_recv_destroy_djhid_device(djrcv_dev, &workitem);
792 break;
793 case WORKITEM_TYPE_UNKNOWN:
794 retval = logi_dj_recv_query_paired_devices(djrcv_dev);
795 if (retval) {
Hans de Goede0ee75542019-04-20 13:21:51 +0200796 hid_err(djrcv_dev->hidpp, "%s: logi_dj_recv_query_paired_devices error: %d\n",
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200797 __func__, retval);
798 }
799 break;
800 case WORKITEM_TYPE_EMPTY:
801 dbg_hid("%s: device list is empty\n", __func__);
802 break;
803 }
804}
805
Hans de Goedeb6aeedd2019-04-20 13:21:53 +0200806/*
807 * Sometimes we receive reports for which we do not have a paired dj_device
808 * associated with the device_index or report-type to forward the report to.
809 * This means that the original "device paired" notification corresponding
810 * to the dj_device never arrived to this driver. Possible reasons for this are:
811 * 1) hid-core discards all packets coming from a device during probe().
812 * 2) if the receiver is plugged into a KVM switch then the pairing reports
813 * are only forwarded to it if the focus is on this PC.
814 * This function deals with this by re-asking the receiver for the list of
815 * connected devices in the delayed work callback.
816 * This function MUST be called with djrcv->lock held.
817 */
818static void logi_dj_recv_queue_unknown_work(struct dj_receiver_dev *djrcv_dev)
819{
820 struct dj_workitem workitem = { .type = WORKITEM_TYPE_UNKNOWN };
821
822 /* Rate limit queries done because of unhandeled reports to 2/sec */
823 if (time_before(jiffies, djrcv_dev->last_query + HZ / 2))
824 return;
825
826 kfifo_in(&djrcv_dev->notif_fifo, &workitem, sizeof(workitem));
827 schedule_work(&djrcv_dev->work);
828}
829
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200830static void logi_dj_recv_queue_notification(struct dj_receiver_dev *djrcv_dev,
831 struct dj_report *dj_report)
832{
833 /* We are called from atomic context (tasklet && djrcv->lock held) */
834 struct dj_workitem workitem = {
835 .device_index = dj_report->device_index,
836 };
837
838 switch (dj_report->report_type) {
839 case REPORT_TYPE_NOTIF_DEVICE_PAIRED:
840 workitem.type = WORKITEM_TYPE_PAIRED;
841 if (dj_report->report_params[DEVICE_PAIRED_PARAM_SPFUNCTION] &
842 SPFUNCTION_DEVICE_LIST_EMPTY) {
843 workitem.type = WORKITEM_TYPE_EMPTY;
844 break;
845 }
846 /* fall-through */
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200847 case REPORT_TYPE_NOTIF_DEVICE_UNPAIRED:
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200848 workitem.quad_id_msb =
849 dj_report->report_params[DEVICE_PAIRED_PARAM_EQUAD_ID_MSB];
850 workitem.quad_id_lsb =
851 dj_report->report_params[DEVICE_PAIRED_PARAM_EQUAD_ID_LSB];
852 workitem.reports_supported = get_unaligned_le32(
853 dj_report->report_params +
854 DEVICE_PAIRED_RF_REPORT_TYPE);
Hans de Goede6d3c3f02019-04-20 13:22:02 +0200855 workitem.reports_supported |= HIDPP;
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200856 if (dj_report->report_type == REPORT_TYPE_NOTIF_DEVICE_UNPAIRED)
857 workitem.type = WORKITEM_TYPE_UNPAIRED;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200858 break;
859 default:
Hans de Goedeb6aeedd2019-04-20 13:21:53 +0200860 logi_dj_recv_queue_unknown_work(djrcv_dev);
861 return;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200862 }
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200863
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200864 kfifo_in(&djrcv_dev->notif_fifo, &workitem, sizeof(workitem));
Hans de Goedee316aa62019-04-20 13:22:01 +0200865 schedule_work(&djrcv_dev->work);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200866}
867
Hans de Goede3ed224e2019-06-05 14:44:08 +0200868static void logi_hidpp_dev_conn_notif_equad(struct hid_device *hdev,
869 struct hidpp_event *hidpp_report,
Hans de Goede74808f92019-04-20 13:21:54 +0200870 struct dj_workitem *workitem)
871{
Hans de Goede3ed224e2019-06-05 14:44:08 +0200872 struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
873
Hans de Goede74808f92019-04-20 13:21:54 +0200874 workitem->type = WORKITEM_TYPE_PAIRED;
Hans de Goedede76b1d2019-04-20 13:22:00 +0200875 workitem->device_type = hidpp_report->params[HIDPP_PARAM_DEVICE_INFO] &
876 HIDPP_DEVICE_TYPE_MASK;
Hans de Goede74808f92019-04-20 13:21:54 +0200877 workitem->quad_id_msb = hidpp_report->params[HIDPP_PARAM_EQUAD_MSB];
878 workitem->quad_id_lsb = hidpp_report->params[HIDPP_PARAM_EQUAD_LSB];
Hans de Goedede76b1d2019-04-20 13:22:00 +0200879 switch (workitem->device_type) {
Hans de Goede74808f92019-04-20 13:21:54 +0200880 case REPORT_TYPE_KEYBOARD:
881 workitem->reports_supported |= STD_KEYBOARD | MULTIMEDIA |
Hans de Goede6d3c3f02019-04-20 13:22:02 +0200882 POWER_KEYS | MEDIA_CENTER |
883 HIDPP;
Hans de Goede74808f92019-04-20 13:21:54 +0200884 break;
885 case REPORT_TYPE_MOUSE:
Hans de Goede6d3c3f02019-04-20 13:22:02 +0200886 workitem->reports_supported |= STD_MOUSE | HIDPP;
Hans de Goede3ed224e2019-06-05 14:44:08 +0200887 if (djrcv_dev->type == recvr_type_mouse_only)
888 workitem->reports_supported |= MULTIMEDIA;
Hans de Goede74808f92019-04-20 13:21:54 +0200889 break;
890 }
891}
892
Hans de Goedec9121cf2019-04-20 13:21:56 +0200893static void logi_hidpp_dev_conn_notif_27mhz(struct hid_device *hdev,
894 struct hidpp_event *hidpp_report,
895 struct dj_workitem *workitem)
896{
897 workitem->type = WORKITEM_TYPE_PAIRED;
898 workitem->quad_id_lsb = hidpp_report->params[HIDPP_PARAM_27MHZ_DEVID];
899 switch (hidpp_report->device_index) {
900 case 1: /* Index 1 is always a mouse */
901 case 2: /* Index 2 is always a mouse */
Hans de Goedede76b1d2019-04-20 13:22:00 +0200902 workitem->device_type = HIDPP_DEVICE_TYPE_MOUSE;
Hans de Goede6d3c3f02019-04-20 13:22:02 +0200903 workitem->reports_supported |= STD_MOUSE | HIDPP;
Hans de Goedec9121cf2019-04-20 13:21:56 +0200904 break;
905 case 3: /* Index 3 is always the keyboard */
906 case 4: /* Index 4 is used for an optional separate numpad */
Hans de Goedede76b1d2019-04-20 13:22:00 +0200907 workitem->device_type = HIDPP_DEVICE_TYPE_KEYBOARD;
Hans de Goedec9121cf2019-04-20 13:21:56 +0200908 workitem->reports_supported |= STD_KEYBOARD | MULTIMEDIA |
Hans de Goede6d3c3f02019-04-20 13:22:02 +0200909 POWER_KEYS | HIDPP;
Hans de Goedec9121cf2019-04-20 13:21:56 +0200910 break;
911 default:
912 hid_warn(hdev, "%s: unexpected device-index %d", __func__,
913 hidpp_report->device_index);
914 }
915}
916
Hans de Goede74808f92019-04-20 13:21:54 +0200917static void logi_hidpp_recv_queue_notif(struct hid_device *hdev,
918 struct hidpp_event *hidpp_report)
919{
920 /* We are called from atomic context (tasklet && djrcv->lock held) */
921 struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
922 const char *device_type = "UNKNOWN";
923 struct dj_workitem workitem = {
924 .type = WORKITEM_TYPE_EMPTY,
925 .device_index = hidpp_report->device_index,
926 };
927
928 switch (hidpp_report->params[HIDPP_PARAM_PROTO_TYPE]) {
929 case 0x01:
930 device_type = "Bluetooth";
Hans de Goedef2113c32019-04-20 13:22:03 +0200931 /* Bluetooth connect packet contents is the same as (e)QUAD */
Hans de Goede3ed224e2019-06-05 14:44:08 +0200932 logi_hidpp_dev_conn_notif_equad(hdev, hidpp_report, &workitem);
Hans de Goedef2113c32019-04-20 13:22:03 +0200933 if (!(hidpp_report->params[HIDPP_PARAM_DEVICE_INFO] &
934 HIDPP_MANUFACTURER_MASK)) {
935 hid_info(hdev, "Non Logitech device connected on slot %d\n",
936 hidpp_report->device_index);
937 workitem.reports_supported &= ~HIDPP;
938 }
Hans de Goede74808f92019-04-20 13:21:54 +0200939 break;
940 case 0x02:
941 device_type = "27 Mhz";
Hans de Goedec9121cf2019-04-20 13:21:56 +0200942 logi_hidpp_dev_conn_notif_27mhz(hdev, hidpp_report, &workitem);
Hans de Goede74808f92019-04-20 13:21:54 +0200943 break;
944 case 0x03:
945 device_type = "QUAD or eQUAD";
Hans de Goede3ed224e2019-06-05 14:44:08 +0200946 logi_hidpp_dev_conn_notif_equad(hdev, hidpp_report, &workitem);
Hans de Goede74808f92019-04-20 13:21:54 +0200947 break;
948 case 0x04:
949 device_type = "eQUAD step 4 DJ";
Hans de Goede3ed224e2019-06-05 14:44:08 +0200950 logi_hidpp_dev_conn_notif_equad(hdev, hidpp_report, &workitem);
Hans de Goede74808f92019-04-20 13:21:54 +0200951 break;
952 case 0x05:
953 device_type = "DFU Lite";
954 break;
955 case 0x06:
956 device_type = "eQUAD step 4 Lite";
Hans de Goede3ed224e2019-06-05 14:44:08 +0200957 logi_hidpp_dev_conn_notif_equad(hdev, hidpp_report, &workitem);
Hans de Goede74808f92019-04-20 13:21:54 +0200958 break;
959 case 0x07:
960 device_type = "eQUAD step 4 Gaming";
961 break;
962 case 0x08:
963 device_type = "eQUAD step 4 for gamepads";
964 break;
965 case 0x0a:
966 device_type = "eQUAD nano Lite";
Hans de Goede3ed224e2019-06-05 14:44:08 +0200967 logi_hidpp_dev_conn_notif_equad(hdev, hidpp_report, &workitem);
Hans de Goede74808f92019-04-20 13:21:54 +0200968 break;
969 case 0x0c:
970 device_type = "eQUAD Lightspeed";
Hans de Goede3ed224e2019-06-05 14:44:08 +0200971 logi_hidpp_dev_conn_notif_equad(hdev, hidpp_report, &workitem);
Benjamin Tissoiresf5fb57a2019-04-20 13:21:55 +0200972 workitem.reports_supported |= STD_KEYBOARD;
Hans de Goede74808f92019-04-20 13:21:54 +0200973 break;
974 }
975
976 if (workitem.type == WORKITEM_TYPE_EMPTY) {
977 hid_warn(hdev,
978 "unusable device of type %s (0x%02x) connected on slot %d",
979 device_type,
980 hidpp_report->params[HIDPP_PARAM_PROTO_TYPE],
981 hidpp_report->device_index);
982 return;
983 }
984
985 hid_info(hdev, "device of type %s (0x%02x) connected on slot %d",
986 device_type, hidpp_report->params[HIDPP_PARAM_PROTO_TYPE],
987 hidpp_report->device_index);
988
Hans de Goede74808f92019-04-20 13:21:54 +0200989 kfifo_in(&djrcv_dev->notif_fifo, &workitem, sizeof(workitem));
Hans de Goedee316aa62019-04-20 13:22:01 +0200990 schedule_work(&djrcv_dev->work);
Hans de Goede74808f92019-04-20 13:21:54 +0200991}
992
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200993static void logi_dj_recv_forward_null_report(struct dj_receiver_dev *djrcv_dev,
994 struct dj_report *dj_report)
995{
996 /* We are called from atomic context (tasklet && djrcv->lock held) */
997 unsigned int i;
998 u8 reportbuffer[MAX_REPORT_SIZE];
999 struct dj_device *djdev;
1000
1001 djdev = djrcv_dev->paired_dj_devices[dj_report->device_index];
1002
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001003 memset(reportbuffer, 0, sizeof(reportbuffer));
1004
1005 for (i = 0; i < NUMBER_OF_HID_REPORTS; i++) {
1006 if (djdev->reports_supported & (1 << i)) {
1007 reportbuffer[0] = i;
1008 if (hid_input_report(djdev->hdev,
1009 HID_INPUT_REPORT,
1010 reportbuffer,
1011 hid_reportid_size_map[i], 1)) {
1012 dbg_hid("hid_input_report error sending null "
1013 "report\n");
1014 }
1015 }
1016 }
1017}
1018
Benjamin Tissoires83898232019-04-20 13:21:43 +02001019static void logi_dj_recv_forward_dj(struct dj_receiver_dev *djrcv_dev,
1020 struct dj_report *dj_report)
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001021{
1022 /* We are called from atomic context (tasklet && djrcv->lock held) */
1023 struct dj_device *dj_device;
1024
1025 dj_device = djrcv_dev->paired_dj_devices[dj_report->device_index];
1026
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001027 if ((dj_report->report_type > ARRAY_SIZE(hid_reportid_size_map) - 1) ||
1028 (hid_reportid_size_map[dj_report->report_type] == 0)) {
1029 dbg_hid("invalid report type:%x\n", dj_report->report_type);
1030 return;
1031 }
1032
1033 if (hid_input_report(dj_device->hdev,
1034 HID_INPUT_REPORT, &dj_report->report_type,
1035 hid_reportid_size_map[dj_report->report_type], 1)) {
1036 dbg_hid("hid_input_report error\n");
1037 }
1038}
1039
Benjamin Tissoires83898232019-04-20 13:21:43 +02001040static void logi_dj_recv_forward_report(struct dj_device *dj_dev, u8 *data,
1041 int size)
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001042{
1043 /* We are called from atomic context (tasklet && djrcv->lock held) */
1044 if (hid_input_report(dj_dev->hdev, HID_INPUT_REPORT, data, size, 1))
1045 dbg_hid("hid_input_report error\n");
1046}
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001047
Hans de Goede74808f92019-04-20 13:21:54 +02001048static void logi_dj_recv_forward_input_report(struct hid_device *hdev,
1049 u8 *data, int size)
1050{
1051 struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
1052 struct dj_device *dj_dev;
1053 unsigned long flags;
1054 u8 report = data[0];
1055 int i;
1056
1057 if (report > REPORT_TYPE_RFREPORT_LAST) {
Colin Ian King640d4ea2019-04-26 14:16:31 +01001058 hid_err(hdev, "Unexpected input report number %d\n", report);
Hans de Goede74808f92019-04-20 13:21:54 +02001059 return;
1060 }
1061
1062 spin_lock_irqsave(&djrcv_dev->lock, flags);
1063 for (i = 0; i < (DJ_MAX_PAIRED_DEVICES + DJ_DEVICE_INDEX_MIN); i++) {
1064 dj_dev = djrcv_dev->paired_dj_devices[i];
1065 if (dj_dev && (dj_dev->reports_supported & BIT(report))) {
1066 logi_dj_recv_forward_report(dj_dev, data, size);
1067 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
1068 return;
1069 }
1070 }
1071
1072 logi_dj_recv_queue_unknown_work(djrcv_dev);
1073 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
1074
1075 dbg_hid("No dj-devs handling input report number %d\n", report);
1076}
1077
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001078static int logi_dj_recv_send_report(struct dj_receiver_dev *djrcv_dev,
1079 struct dj_report *dj_report)
1080{
Hans de Goede0ee75542019-04-20 13:21:51 +02001081 struct hid_device *hdev = djrcv_dev->hidpp;
Benjamin Tissoiresdcd90062013-03-05 17:09:00 +01001082 struct hid_report *report;
1083 struct hid_report_enum *output_report_enum;
1084 u8 *data = (u8 *)(&dj_report->device_index);
Kees Cook297502a2013-09-11 21:56:56 +02001085 unsigned int i;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001086
Benjamin Tissoiresdcd90062013-03-05 17:09:00 +01001087 output_report_enum = &hdev->report_enum[HID_OUTPUT_REPORT];
1088 report = output_report_enum->report_id_hash[REPORT_ID_DJ_SHORT];
1089
1090 if (!report) {
Hans de Goedeaca22a32019-04-20 13:21:58 +02001091 hid_err(hdev, "%s: unable to find dj report\n", __func__);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001092 return -ENODEV;
1093 }
1094
Kees Cook297502a2013-09-11 21:56:56 +02001095 for (i = 0; i < DJREPORT_SHORT_LENGTH - 1; i++)
Benjamin Tissoiresdcd90062013-03-05 17:09:00 +01001096 report->field[0]->value[i] = data[i];
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001097
Jiri Kosina83a44ac2013-03-09 10:58:13 +01001098 hid_hw_request(hdev, report, HID_REQ_SET_REPORT);
Benjamin Tissoiresdcd90062013-03-05 17:09:00 +01001099
1100 return 0;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001101}
1102
Hans de Goede74808f92019-04-20 13:21:54 +02001103static int logi_dj_recv_query_hidpp_devices(struct dj_receiver_dev *djrcv_dev)
1104{
1105 const u8 template[] = {REPORT_ID_HIDPP_SHORT,
1106 HIDPP_RECEIVER_INDEX,
1107 HIDPP_SET_REGISTER,
1108 HIDPP_REG_CONNECTION_STATE,
1109 HIDPP_FAKE_DEVICE_ARRIVAL,
1110 0x00, 0x00};
1111 u8 *hidpp_report;
1112 int retval;
1113
1114 hidpp_report = kmemdup(template, sizeof(template), GFP_KERNEL);
1115 if (!hidpp_report)
1116 return -ENOMEM;
1117
1118 retval = hid_hw_raw_request(djrcv_dev->hidpp,
1119 REPORT_ID_HIDPP_SHORT,
1120 hidpp_report, sizeof(template),
1121 HID_OUTPUT_REPORT,
1122 HID_REQ_SET_REPORT);
1123
1124 kfree(hidpp_report);
1125 return 0;
1126}
1127
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001128static int logi_dj_recv_query_paired_devices(struct dj_receiver_dev *djrcv_dev)
1129{
Marc Dionned8dc3492012-06-01 18:12:14 -04001130 struct dj_report *dj_report;
1131 int retval;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001132
Hans de Goedeb6aeedd2019-04-20 13:21:53 +02001133 djrcv_dev->last_query = jiffies;
1134
Hans de Goede74808f92019-04-20 13:21:54 +02001135 if (djrcv_dev->type != recvr_type_dj)
1136 return logi_dj_recv_query_hidpp_devices(djrcv_dev);
1137
Alan Cox8a55ade2012-09-04 15:10:08 +01001138 dj_report = kzalloc(sizeof(struct dj_report), GFP_KERNEL);
Marc Dionned8dc3492012-06-01 18:12:14 -04001139 if (!dj_report)
1140 return -ENOMEM;
1141 dj_report->report_id = REPORT_ID_DJ_SHORT;
1142 dj_report->device_index = 0xFF;
1143 dj_report->report_type = REPORT_TYPE_CMD_GET_PAIRED_DEVICES;
1144 retval = logi_dj_recv_send_report(djrcv_dev, dj_report);
1145 kfree(dj_report);
1146 return retval;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001147}
1148
Nestor Lopez Casadoc63e0e32013-07-18 06:21:30 -07001149
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001150static int logi_dj_recv_switch_to_dj_mode(struct dj_receiver_dev *djrcv_dev,
1151 unsigned timeout)
1152{
Hans de Goede0ee75542019-04-20 13:21:51 +02001153 struct hid_device *hdev = djrcv_dev->hidpp;
Marc Dionned8dc3492012-06-01 18:12:14 -04001154 struct dj_report *dj_report;
Benjamin Tissoires6a9ddc82014-09-30 13:18:31 -04001155 u8 *buf;
Hans de Goede74808f92019-04-20 13:21:54 +02001156 int retval = 0;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001157
Alan Cox8a55ade2012-09-04 15:10:08 +01001158 dj_report = kzalloc(sizeof(struct dj_report), GFP_KERNEL);
Marc Dionned8dc3492012-06-01 18:12:14 -04001159 if (!dj_report)
1160 return -ENOMEM;
Benjamin Tisssoires42c22db2014-01-08 17:18:45 -05001161
Hans de Goede74808f92019-04-20 13:21:54 +02001162 if (djrcv_dev->type == recvr_type_dj) {
1163 dj_report->report_id = REPORT_ID_DJ_SHORT;
1164 dj_report->device_index = 0xFF;
1165 dj_report->report_type = REPORT_TYPE_CMD_SWITCH;
1166 dj_report->report_params[CMD_SWITCH_PARAM_DEVBITFIELD] = 0x3F;
1167 dj_report->report_params[CMD_SWITCH_PARAM_TIMEOUT_SECONDS] =
1168 (u8)timeout;
1169
1170 retval = logi_dj_recv_send_report(djrcv_dev, dj_report);
1171
1172 /*
1173 * Ugly sleep to work around a USB 3.0 bug when the receiver is
1174 * still processing the "switch-to-dj" command while we send an
1175 * other command.
1176 * 50 msec should gives enough time to the receiver to be ready.
1177 */
1178 msleep(50);
1179 }
Benjamin Tisssoires42c22db2014-01-08 17:18:45 -05001180
Benjamin Tissoires6a9ddc82014-09-30 13:18:31 -04001181 /*
1182 * Magical bits to set up hidpp notifications when the dj devices
1183 * are connected/disconnected.
1184 *
1185 * We can reuse dj_report because HIDPP_REPORT_SHORT_LENGTH is smaller
1186 * than DJREPORT_SHORT_LENGTH.
1187 */
1188 buf = (u8 *)dj_report;
1189
1190 memset(buf, 0, HIDPP_REPORT_SHORT_LENGTH);
1191
1192 buf[0] = REPORT_ID_HIDPP_SHORT;
1193 buf[1] = 0xFF;
1194 buf[2] = 0x80;
1195 buf[3] = 0x00;
1196 buf[4] = 0x00;
1197 buf[5] = 0x09;
1198 buf[6] = 0x00;
1199
1200 hid_hw_raw_request(hdev, REPORT_ID_HIDPP_SHORT, buf,
1201 HIDPP_REPORT_SHORT_LENGTH, HID_OUTPUT_REPORT,
1202 HID_REQ_SET_REPORT);
1203
1204 kfree(dj_report);
Marc Dionned8dc3492012-06-01 18:12:14 -04001205 return retval;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001206}
1207
1208
1209static int logi_dj_ll_open(struct hid_device *hid)
1210{
Hans de Goedeaca22a32019-04-20 13:21:58 +02001211 dbg_hid("%s: %s\n", __func__, hid->phys);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001212 return 0;
1213
1214}
1215
1216static void logi_dj_ll_close(struct hid_device *hid)
1217{
Hans de Goedeaca22a32019-04-20 13:21:58 +02001218 dbg_hid("%s: %s\n", __func__, hid->phys);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001219}
1220
Benjamin Tissoires8dba3022017-03-27 16:59:21 +02001221/*
1222 * Register 0xB5 is "pairing information". It is solely intended for the
1223 * receiver, so do not overwrite the device index.
1224 */
Benjamin Tissoiresc0340412019-04-20 13:21:46 +02001225static u8 unifying_pairing_query[] = { REPORT_ID_HIDPP_SHORT,
1226 HIDPP_RECEIVER_INDEX,
1227 HIDPP_GET_LONG_REGISTER,
1228 HIDPP_REG_PAIRING_INFORMATION };
1229static u8 unifying_pairing_answer[] = { REPORT_ID_HIDPP_LONG,
1230 HIDPP_RECEIVER_INDEX,
1231 HIDPP_GET_LONG_REGISTER,
1232 HIDPP_REG_PAIRING_INFORMATION };
Benjamin Tissoires33797822014-09-30 13:18:30 -04001233
Benjamin Tissoiresbd27e202014-02-10 12:58:53 -05001234static int logi_dj_ll_raw_request(struct hid_device *hid,
1235 unsigned char reportnum, __u8 *buf,
1236 size_t count, unsigned char report_type,
1237 int reqtype)
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001238{
Benjamin Tissoires0e40d352014-02-05 16:33:17 -05001239 struct dj_device *djdev = hid->driver_data;
1240 struct dj_receiver_dev *djrcv_dev = djdev->dj_receiver_dev;
1241 u8 *out_buf;
1242 int ret;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001243
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001244 if ((buf[0] == REPORT_ID_HIDPP_SHORT) ||
1245 (buf[0] == REPORT_ID_HIDPP_LONG)) {
1246 if (count < 2)
1247 return -EINVAL;
1248
Benjamin Tissoires33797822014-09-30 13:18:30 -04001249 /* special case where we should not overwrite
1250 * the device_index */
Benjamin Tissoires8dba3022017-03-27 16:59:21 +02001251 if (count == 7 && !memcmp(buf, unifying_pairing_query,
1252 sizeof(unifying_pairing_query)))
1253 buf[4] = (buf[4] & 0xf0) | (djdev->device_index - 1);
Benjamin Tissoires33797822014-09-30 13:18:30 -04001254 else
1255 buf[1] = djdev->device_index;
Hans de Goede0ee75542019-04-20 13:21:51 +02001256 return hid_hw_raw_request(djrcv_dev->hidpp, reportnum, buf,
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001257 count, report_type, reqtype);
1258 }
1259
Benjamin Tissoires0e40d352014-02-05 16:33:17 -05001260 if (buf[0] != REPORT_TYPE_LEDS)
1261 return -EINVAL;
1262
Hans de Goede74808f92019-04-20 13:21:54 +02001263 if (djrcv_dev->type != recvr_type_dj && count >= 2) {
1264 if (!djrcv_dev->keyboard) {
1265 hid_warn(hid, "Received REPORT_TYPE_LEDS request before the keyboard interface was enumerated\n");
1266 return 0;
1267 }
1268 /* usbhid overrides the report ID and ignores the first byte */
1269 return hid_hw_raw_request(djrcv_dev->keyboard, 0, buf, count,
1270 report_type, reqtype);
1271 }
1272
Benjamin Tissoires0e40d352014-02-05 16:33:17 -05001273 out_buf = kzalloc(DJREPORT_SHORT_LENGTH, GFP_ATOMIC);
1274 if (!out_buf)
1275 return -ENOMEM;
1276
Jiri Kosina51217e62014-08-21 09:56:47 -05001277 if (count > DJREPORT_SHORT_LENGTH - 2)
Benjamin Tissoires0e40d352014-02-05 16:33:17 -05001278 count = DJREPORT_SHORT_LENGTH - 2;
1279
1280 out_buf[0] = REPORT_ID_DJ_SHORT;
1281 out_buf[1] = djdev->device_index;
1282 memcpy(out_buf + 2, buf, count);
1283
Hans de Goede0ee75542019-04-20 13:21:51 +02001284 ret = hid_hw_raw_request(djrcv_dev->hidpp, out_buf[0], out_buf,
Benjamin Tissoiresbd27e202014-02-10 12:58:53 -05001285 DJREPORT_SHORT_LENGTH, report_type, reqtype);
Benjamin Tissoires0e40d352014-02-05 16:33:17 -05001286
1287 kfree(out_buf);
1288 return ret;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001289}
1290
Dan Carpenter6d603322013-10-19 12:08:59 +03001291static void rdcat(char *rdesc, unsigned int *rsize, const char *data, unsigned int size)
Henrik Rydberg2a039bf2012-04-22 14:21:39 +02001292{
Dan Carpenter6d603322013-10-19 12:08:59 +03001293 memcpy(rdesc + *rsize, data, size);
Henrik Rydberg2a039bf2012-04-22 14:21:39 +02001294 *rsize += size;
1295}
1296
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001297static int logi_dj_ll_parse(struct hid_device *hid)
1298{
1299 struct dj_device *djdev = hid->driver_data;
Henrik Rydberg2a039bf2012-04-22 14:21:39 +02001300 unsigned int rsize = 0;
1301 char *rdesc;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001302 int retval;
1303
1304 dbg_hid("%s\n", __func__);
1305
1306 djdev->hdev->version = 0x0111;
1307 djdev->hdev->country = 0x00;
1308
Henrik Rydberg2a039bf2012-04-22 14:21:39 +02001309 rdesc = kmalloc(MAX_RDESC_SIZE, GFP_KERNEL);
1310 if (!rdesc)
1311 return -ENOMEM;
1312
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001313 if (djdev->reports_supported & STD_KEYBOARD) {
Hans de Goede6d3c3f02019-04-20 13:22:02 +02001314 dbg_hid("%s: sending a kbd descriptor, reports_supported: %llx\n",
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001315 __func__, djdev->reports_supported);
Dan Carpenter6d603322013-10-19 12:08:59 +03001316 rdcat(rdesc, &rsize, kbd_descriptor, sizeof(kbd_descriptor));
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001317 }
1318
1319 if (djdev->reports_supported & STD_MOUSE) {
Hans de Goede6d3c3f02019-04-20 13:22:02 +02001320 dbg_hid("%s: sending a mouse descriptor, reports_supported: %llx\n",
1321 __func__, djdev->reports_supported);
Hans de Goede3ed224e2019-06-05 14:44:08 +02001322 if (djdev->dj_receiver_dev->type == recvr_type_gaming_hidpp ||
1323 djdev->dj_receiver_dev->type == recvr_type_mouse_only)
Benjamin Tissoiresf5fb57a2019-04-20 13:21:55 +02001324 rdcat(rdesc, &rsize, mse_high_res_descriptor,
1325 sizeof(mse_high_res_descriptor));
Hans de Goedec9121cf2019-04-20 13:21:56 +02001326 else if (djdev->dj_receiver_dev->type == recvr_type_27mhz)
1327 rdcat(rdesc, &rsize, mse_27mhz_descriptor,
1328 sizeof(mse_27mhz_descriptor));
Hans de Goedef2113c32019-04-20 13:22:03 +02001329 else if (djdev->dj_receiver_dev->type == recvr_type_bluetooth)
1330 rdcat(rdesc, &rsize, mse_bluetooth_descriptor,
1331 sizeof(mse_bluetooth_descriptor));
Benjamin Tissoiresf5fb57a2019-04-20 13:21:55 +02001332 else
1333 rdcat(rdesc, &rsize, mse_descriptor,
1334 sizeof(mse_descriptor));
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001335 }
1336
1337 if (djdev->reports_supported & MULTIMEDIA) {
Hans de Goede6d3c3f02019-04-20 13:22:02 +02001338 dbg_hid("%s: sending a multimedia report descriptor: %llx\n",
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001339 __func__, djdev->reports_supported);
Dan Carpenter6d603322013-10-19 12:08:59 +03001340 rdcat(rdesc, &rsize, consumer_descriptor, sizeof(consumer_descriptor));
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001341 }
1342
1343 if (djdev->reports_supported & POWER_KEYS) {
Hans de Goede6d3c3f02019-04-20 13:22:02 +02001344 dbg_hid("%s: sending a power keys report descriptor: %llx\n",
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001345 __func__, djdev->reports_supported);
Dan Carpenter6d603322013-10-19 12:08:59 +03001346 rdcat(rdesc, &rsize, syscontrol_descriptor, sizeof(syscontrol_descriptor));
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001347 }
1348
1349 if (djdev->reports_supported & MEDIA_CENTER) {
Hans de Goede6d3c3f02019-04-20 13:22:02 +02001350 dbg_hid("%s: sending a media center report descriptor: %llx\n",
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001351 __func__, djdev->reports_supported);
Dan Carpenter6d603322013-10-19 12:08:59 +03001352 rdcat(rdesc, &rsize, media_descriptor, sizeof(media_descriptor));
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001353 }
1354
1355 if (djdev->reports_supported & KBD_LEDS) {
Hans de Goede6d3c3f02019-04-20 13:22:02 +02001356 dbg_hid("%s: need to send kbd leds report descriptor: %llx\n",
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001357 __func__, djdev->reports_supported);
1358 }
1359
Hans de Goede6d3c3f02019-04-20 13:22:02 +02001360 if (djdev->reports_supported & HIDPP) {
1361 rdcat(rdesc, &rsize, hidpp_descriptor,
1362 sizeof(hidpp_descriptor));
1363 }
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001364
Henrik Rydberg2a039bf2012-04-22 14:21:39 +02001365 retval = hid_parse_report(hid, rdesc, rsize);
1366 kfree(rdesc);
1367
1368 return retval;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001369}
1370
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001371static int logi_dj_ll_start(struct hid_device *hid)
1372{
1373 dbg_hid("%s\n", __func__);
1374 return 0;
1375}
1376
1377static void logi_dj_ll_stop(struct hid_device *hid)
1378{
1379 dbg_hid("%s\n", __func__);
1380}
1381
1382
1383static struct hid_ll_driver logi_dj_ll_driver = {
1384 .parse = logi_dj_ll_parse,
1385 .start = logi_dj_ll_start,
1386 .stop = logi_dj_ll_stop,
1387 .open = logi_dj_ll_open,
1388 .close = logi_dj_ll_close,
Benjamin Tissoiresbd27e202014-02-10 12:58:53 -05001389 .raw_request = logi_dj_ll_raw_request,
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001390};
1391
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001392static int logi_dj_dj_event(struct hid_device *hdev,
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001393 struct hid_report *report, u8 *data,
1394 int size)
1395{
1396 struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
1397 struct dj_report *dj_report = (struct dj_report *) data;
1398 unsigned long flags;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001399
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001400 /*
1401 * Here we receive all data coming from iface 2, there are 3 cases:
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001402 *
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001403 * 1) Data is intended for this driver i. e. data contains arrival,
1404 * departure, etc notifications, in which case we queue them for delayed
1405 * processing by the work queue. We return 1 to hid-core as no further
1406 * processing is required from it.
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001407 *
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001408 * 2) Data informs a connection change, if the change means rf link
1409 * loss, then we must send a null report to the upper layer to discard
1410 * potentially pressed keys that may be repeated forever by the input
1411 * layer. Return 1 to hid-core as no further processing is required.
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001412 *
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001413 * 3) Data is an actual input event from a paired DJ device in which
1414 * case we forward it to the correct hid device (via hid_input_report()
1415 * ) and return 1 so hid-core does not anything else with it.
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001416 */
Benjamin Tissoires5abfe852014-08-22 16:16:05 -04001417
Jiri Kosinaad3e14d2014-08-21 09:57:17 -05001418 if ((dj_report->device_index < DJ_DEVICE_INDEX_MIN) ||
1419 (dj_report->device_index > DJ_DEVICE_INDEX_MAX)) {
Benjamin Tissoires5abfe852014-08-22 16:16:05 -04001420 /*
1421 * Device index is wrong, bail out.
1422 * This driver can ignore safely the receiver notifications,
1423 * so ignore those reports too.
1424 */
1425 if (dj_report->device_index != DJ_RECEIVER_INDEX)
Hans de Goedeaca22a32019-04-20 13:21:58 +02001426 hid_err(hdev, "%s: invalid device index:%d\n",
Jiri Kosinaad3e14d2014-08-21 09:57:17 -05001427 __func__, dj_report->device_index);
1428 return false;
1429 }
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001430
1431 spin_lock_irqsave(&djrcv_dev->lock, flags);
Benjamin Tissoires368d4e592014-08-22 16:16:06 -04001432
1433 if (!djrcv_dev->paired_dj_devices[dj_report->device_index]) {
1434 /* received an event for an unknown device, bail out */
1435 logi_dj_recv_queue_notification(djrcv_dev, dj_report);
1436 goto out;
1437 }
1438
Benjamin Tissoires5abfe852014-08-22 16:16:05 -04001439 switch (dj_report->report_type) {
1440 case REPORT_TYPE_NOTIF_DEVICE_PAIRED:
Benjamin Tissoires368d4e592014-08-22 16:16:06 -04001441 /* pairing notifications are handled above the switch */
1442 break;
Benjamin Tissoires5abfe852014-08-22 16:16:05 -04001443 case REPORT_TYPE_NOTIF_DEVICE_UNPAIRED:
1444 logi_dj_recv_queue_notification(djrcv_dev, dj_report);
1445 break;
1446 case REPORT_TYPE_NOTIF_CONNECTION_STATUS:
1447 if (dj_report->report_params[CONNECTION_STATUS_PARAM_STATUS] ==
1448 STATUS_LINKLOSS) {
1449 logi_dj_recv_forward_null_report(djrcv_dev, dj_report);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001450 }
Benjamin Tissoires5abfe852014-08-22 16:16:05 -04001451 break;
1452 default:
Benjamin Tissoires83898232019-04-20 13:21:43 +02001453 logi_dj_recv_forward_dj(djrcv_dev, dj_report);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001454 }
Benjamin Tissoires368d4e592014-08-22 16:16:06 -04001455
1456out:
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001457 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
1458
Benjamin Tissoires5abfe852014-08-22 16:16:05 -04001459 return true;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001460}
1461
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001462static int logi_dj_hidpp_event(struct hid_device *hdev,
1463 struct hid_report *report, u8 *data,
1464 int size)
1465{
1466 struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
Benjamin Tissoires7bb56a52019-04-20 13:21:44 +02001467 struct hidpp_event *hidpp_report = (struct hidpp_event *) data;
Hans de Goede74808f92019-04-20 13:21:54 +02001468 struct dj_device *dj_dev;
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001469 unsigned long flags;
Benjamin Tissoires7bb56a52019-04-20 13:21:44 +02001470 u8 device_index = hidpp_report->device_index;
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001471
Benjamin Tissoires33797822014-09-30 13:18:30 -04001472 if (device_index == HIDPP_RECEIVER_INDEX) {
1473 /* special case were the device wants to know its unifying
1474 * name */
1475 if (size == HIDPP_REPORT_LONG_LENGTH &&
Benjamin Tissoires8dba3022017-03-27 16:59:21 +02001476 !memcmp(data, unifying_pairing_answer,
1477 sizeof(unifying_pairing_answer)))
Benjamin Tissoires33797822014-09-30 13:18:30 -04001478 device_index = (data[4] & 0x0F) + 1;
1479 else
1480 return false;
1481 }
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001482
1483 /*
1484 * Data is from the HID++ collection, in this case, we forward the
1485 * data to the corresponding child dj device and return 0 to hid-core
1486 * so he data also goes to the hidraw device of the receiver. This
1487 * allows a user space application to implement the full HID++ routing
1488 * via the receiver.
1489 */
1490
1491 if ((device_index < DJ_DEVICE_INDEX_MIN) ||
1492 (device_index > DJ_DEVICE_INDEX_MAX)) {
1493 /*
1494 * Device index is wrong, bail out.
1495 * This driver can ignore safely the receiver notifications,
1496 * so ignore those reports too.
1497 */
Hans de Goedeaca22a32019-04-20 13:21:58 +02001498 hid_err(hdev, "%s: invalid device index:%d\n", __func__,
1499 hidpp_report->device_index);
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001500 return false;
1501 }
1502
1503 spin_lock_irqsave(&djrcv_dev->lock, flags);
1504
Hans de Goede74808f92019-04-20 13:21:54 +02001505 dj_dev = djrcv_dev->paired_dj_devices[device_index];
Hans de Goedec9121cf2019-04-20 13:21:56 +02001506
1507 /*
1508 * With 27 MHz receivers, we do not get an explicit unpair event,
1509 * remove the old device if the user has paired a *different* device.
1510 */
1511 if (djrcv_dev->type == recvr_type_27mhz && dj_dev &&
1512 hidpp_report->sub_id == REPORT_TYPE_NOTIF_DEVICE_CONNECTED &&
1513 hidpp_report->params[HIDPP_PARAM_PROTO_TYPE] == 0x02 &&
1514 hidpp_report->params[HIDPP_PARAM_27MHZ_DEVID] !=
1515 dj_dev->hdev->product) {
1516 struct dj_workitem workitem = {
1517 .device_index = hidpp_report->device_index,
1518 .type = WORKITEM_TYPE_UNPAIRED,
1519 };
1520 kfifo_in(&djrcv_dev->notif_fifo, &workitem, sizeof(workitem));
1521 /* logi_hidpp_recv_queue_notif will queue the work */
1522 dj_dev = NULL;
1523 }
1524
Hans de Goede74808f92019-04-20 13:21:54 +02001525 if (dj_dev) {
1526 logi_dj_recv_forward_report(dj_dev, data, size);
1527 } else {
1528 if (hidpp_report->sub_id == REPORT_TYPE_NOTIF_DEVICE_CONNECTED)
1529 logi_hidpp_recv_queue_notif(hdev, hidpp_report);
1530 else
1531 logi_dj_recv_queue_unknown_work(djrcv_dev);
1532 }
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001533
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001534 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
1535
1536 return false;
1537}
1538
1539static int logi_dj_raw_event(struct hid_device *hdev,
1540 struct hid_report *report, u8 *data,
1541 int size)
1542{
Hans de Goede74808f92019-04-20 13:21:54 +02001543 struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001544 dbg_hid("%s, size:%d\n", __func__, size);
1545
Hans de Goededa12b222019-04-20 13:21:59 +02001546 if (!djrcv_dev)
1547 return 0;
1548
Hans de Goede74808f92019-04-20 13:21:54 +02001549 if (!hdev->report_enum[HID_INPUT_REPORT].numbered) {
1550
1551 if (djrcv_dev->unnumbered_application == HID_GD_KEYBOARD) {
1552 /*
1553 * For the keyboard, we can reuse the same report by
1554 * using the second byte which is constant in the USB
1555 * HID report descriptor.
1556 */
1557 data[1] = data[0];
1558 data[0] = REPORT_TYPE_KEYBOARD;
1559
1560 logi_dj_recv_forward_input_report(hdev, data, size);
1561
1562 /* restore previous state */
1563 data[0] = data[1];
1564 data[1] = 0;
1565 }
Hans de Goede3ed224e2019-06-05 14:44:08 +02001566 /*
1567 * Mouse-only receivers send unnumbered mouse data. The 27 MHz
1568 * receiver uses 6 byte packets, the nano receiver 8 bytes.
1569 */
Hans de Goede1f944ac2019-04-20 13:21:57 +02001570 if (djrcv_dev->unnumbered_application == HID_GD_MOUSE &&
Hans de Goede3ed224e2019-06-05 14:44:08 +02001571 size <= 8) {
1572 u8 mouse_report[9];
Hans de Goede1f944ac2019-04-20 13:21:57 +02001573
1574 /* Prepend report id */
1575 mouse_report[0] = REPORT_TYPE_MOUSE;
Hans de Goede3ed224e2019-06-05 14:44:08 +02001576 memcpy(mouse_report + 1, data, size);
1577 logi_dj_recv_forward_input_report(hdev, mouse_report,
1578 size + 1);
Hans de Goede1f944ac2019-04-20 13:21:57 +02001579 }
Hans de Goede74808f92019-04-20 13:21:54 +02001580
1581 return false;
1582 }
1583
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001584 switch (data[0]) {
1585 case REPORT_ID_DJ_SHORT:
Peter Wuf254ae92014-12-16 16:55:21 +01001586 if (size != DJREPORT_SHORT_LENGTH) {
Hans de Goedeaca22a32019-04-20 13:21:58 +02001587 hid_err(hdev, "Short DJ report bad size (%d)", size);
Peter Wuf254ae92014-12-16 16:55:21 +01001588 return false;
1589 }
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001590 return logi_dj_dj_event(hdev, report, data, size);
Benjamin Tissoiresf5fb57a2019-04-20 13:21:55 +02001591 case REPORT_ID_DJ_LONG:
1592 if (size != DJREPORT_LONG_LENGTH) {
Hans de Goedeaca22a32019-04-20 13:21:58 +02001593 hid_err(hdev, "Long DJ report bad size (%d)", size);
Benjamin Tissoiresf5fb57a2019-04-20 13:21:55 +02001594 return false;
1595 }
1596 return logi_dj_dj_event(hdev, report, data, size);
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001597 case REPORT_ID_HIDPP_SHORT:
Peter Wuf254ae92014-12-16 16:55:21 +01001598 if (size != HIDPP_REPORT_SHORT_LENGTH) {
Hans de Goedeaca22a32019-04-20 13:21:58 +02001599 hid_err(hdev, "Short HID++ report bad size (%d)", size);
Peter Wuf254ae92014-12-16 16:55:21 +01001600 return false;
1601 }
1602 return logi_dj_hidpp_event(hdev, report, data, size);
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001603 case REPORT_ID_HIDPP_LONG:
Peter Wuf254ae92014-12-16 16:55:21 +01001604 if (size != HIDPP_REPORT_LONG_LENGTH) {
Hans de Goedeaca22a32019-04-20 13:21:58 +02001605 hid_err(hdev, "Long HID++ report bad size (%d)", size);
Peter Wuf254ae92014-12-16 16:55:21 +01001606 return false;
1607 }
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001608 return logi_dj_hidpp_event(hdev, report, data, size);
1609 }
1610
Hans de Goede74808f92019-04-20 13:21:54 +02001611 logi_dj_recv_forward_input_report(hdev, data, size);
1612
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001613 return false;
1614}
1615
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001616static int logi_dj_probe(struct hid_device *hdev,
1617 const struct hid_device_id *id)
1618{
Benjamin Tissoires82c0beb2019-04-20 13:21:47 +02001619 struct hid_report_enum *rep_enum;
1620 struct hid_report *rep;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001621 struct dj_receiver_dev *djrcv_dev;
Hans de Goededa12b222019-04-20 13:21:59 +02001622 struct usb_interface *intf;
1623 unsigned int no_dj_interfaces = 0;
Benjamin Tissoires82c0beb2019-04-20 13:21:47 +02001624 bool has_hidpp = false;
Hans de Goedea1d97cc2019-04-20 13:21:52 +02001625 unsigned long flags;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001626 int retval;
1627
Benjamin Tissoires82c0beb2019-04-20 13:21:47 +02001628 /*
1629 * Call to usbhid to fetch the HID descriptors of the current
1630 * interface subsequently call to the hid/hid-core to parse the
1631 * fetched descriptors.
1632 */
1633 retval = hid_parse(hdev);
1634 if (retval) {
Hans de Goedeaca22a32019-04-20 13:21:58 +02001635 hid_err(hdev, "%s: parse failed\n", __func__);
Benjamin Tissoires82c0beb2019-04-20 13:21:47 +02001636 return retval;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001637 }
1638
Hans de Goededa12b222019-04-20 13:21:59 +02001639 /*
1640 * Some KVMs add an extra interface for e.g. mouse emulation. If we
1641 * treat these as logitech-dj interfaces then this causes input events
1642 * reported through this extra interface to not be reported correctly.
1643 * To avoid this, we treat these as generic-hid devices.
1644 */
1645 switch (id->driver_data) {
1646 case recvr_type_dj: no_dj_interfaces = 3; break;
1647 case recvr_type_hidpp: no_dj_interfaces = 2; break;
1648 case recvr_type_gaming_hidpp: no_dj_interfaces = 3; break;
Hans de Goede3ed224e2019-06-05 14:44:08 +02001649 case recvr_type_mouse_only: no_dj_interfaces = 2; break;
Hans de Goededa12b222019-04-20 13:21:59 +02001650 case recvr_type_27mhz: no_dj_interfaces = 2; break;
Hans de Goedef2113c32019-04-20 13:22:03 +02001651 case recvr_type_bluetooth: no_dj_interfaces = 2; break;
Hans de Goededa12b222019-04-20 13:21:59 +02001652 }
1653 if (hid_is_using_ll_driver(hdev, &usb_hid_driver)) {
1654 intf = to_usb_interface(hdev->dev.parent);
1655 if (intf && intf->altsetting->desc.bInterfaceNumber >=
1656 no_dj_interfaces) {
1657 hdev->quirks |= HID_QUIRK_INPUT_PER_APP;
1658 return hid_hw_start(hdev, HID_CONNECT_DEFAULT);
1659 }
1660 }
1661
Benjamin Tissoires82c0beb2019-04-20 13:21:47 +02001662 rep_enum = &hdev->report_enum[HID_INPUT_REPORT];
1663
Hans de Goede74808f92019-04-20 13:21:54 +02001664 /* no input reports, bail out */
1665 if (list_empty(&rep_enum->report_list))
1666 return -ENODEV;
1667
Benjamin Tissoires82c0beb2019-04-20 13:21:47 +02001668 /*
1669 * Check for the HID++ application.
1670 * Note: we should theoretically check for HID++ and DJ
1671 * collections, but this will do.
1672 */
1673 list_for_each_entry(rep, &rep_enum->report_list, list) {
1674 if (rep->application == 0xff000001)
1675 has_hidpp = true;
1676 }
1677
1678 /*
1679 * Ignore interfaces without DJ/HID++ collection, they will not carry
1680 * any data, dont create any hid_device for them.
1681 */
Hans de Goede74808f92019-04-20 13:21:54 +02001682 if (!has_hidpp && id->driver_data == recvr_type_dj)
Benjamin Tissoires82c0beb2019-04-20 13:21:47 +02001683 return -ENODEV;
1684
Hans de Goedea1d97cc2019-04-20 13:21:52 +02001685 /* get the current application attached to the node */
1686 rep = list_first_entry(&rep_enum->report_list, struct hid_report, list);
Hans de Goede74808f92019-04-20 13:21:54 +02001687 djrcv_dev = dj_get_receiver_dev(hdev, id->driver_data,
Hans de Goedea1d97cc2019-04-20 13:21:52 +02001688 rep->application, has_hidpp);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001689 if (!djrcv_dev) {
Hans de Goedeaca22a32019-04-20 13:21:58 +02001690 hid_err(hdev, "%s: dj_get_receiver_dev failed\n", __func__);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001691 return -ENOMEM;
1692 }
Kees Cook297502a2013-09-11 21:56:56 +02001693
Hans de Goede74808f92019-04-20 13:21:54 +02001694 if (!rep_enum->numbered)
1695 djrcv_dev->unnumbered_application = rep->application;
1696
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001697 /* Starts the usb device and connects to upper interfaces hiddev and
1698 * hidraw */
Hans de Goede74808f92019-04-20 13:21:54 +02001699 retval = hid_hw_start(hdev, HID_CONNECT_HIDRAW|HID_CONNECT_HIDDEV);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001700 if (retval) {
Hans de Goedeaca22a32019-04-20 13:21:58 +02001701 hid_err(hdev, "%s: hid_hw_start returned error\n", __func__);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001702 goto hid_hw_start_fail;
1703 }
1704
Hans de Goede74808f92019-04-20 13:21:54 +02001705 if (has_hidpp) {
1706 retval = logi_dj_recv_switch_to_dj_mode(djrcv_dev, 0);
1707 if (retval < 0) {
1708 hid_err(hdev, "%s: logi_dj_recv_switch_to_dj_mode returned error:%d\n",
1709 __func__, retval);
1710 goto switch_to_dj_mode_fail;
1711 }
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001712 }
1713
1714 /* This is enabling the polling urb on the IN endpoint */
Benjamin Tissoiresddf75402013-07-12 11:01:02 +02001715 retval = hid_hw_open(hdev);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001716 if (retval < 0) {
Hans de Goedeaca22a32019-04-20 13:21:58 +02001717 hid_err(hdev, "%s: hid_hw_open returned error:%d\n",
Benjamin Tissoiresddf75402013-07-12 11:01:02 +02001718 __func__, retval);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001719 goto llopen_failed;
1720 }
1721
Andrew de los Reyesa9dd22b72013-02-18 09:20:22 -08001722 /* Allow incoming packets to arrive: */
1723 hid_device_io_start(hdev);
1724
Hans de Goede74808f92019-04-20 13:21:54 +02001725 if (has_hidpp) {
1726 spin_lock_irqsave(&djrcv_dev->lock, flags);
1727 djrcv_dev->ready = true;
1728 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
1729 retval = logi_dj_recv_query_paired_devices(djrcv_dev);
1730 if (retval < 0) {
1731 hid_err(hdev, "%s: logi_dj_recv_query_paired_devices error:%d\n",
1732 __func__, retval);
1733 goto logi_dj_recv_query_paired_devices_failed;
1734 }
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001735 }
1736
1737 return retval;
1738
1739logi_dj_recv_query_paired_devices_failed:
Benjamin Tissoiresddf75402013-07-12 11:01:02 +02001740 hid_hw_close(hdev);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001741
1742llopen_failed:
1743switch_to_dj_mode_fail:
1744 hid_hw_stop(hdev);
1745
1746hid_hw_start_fail:
Hans de Goedea1d97cc2019-04-20 13:21:52 +02001747 dj_put_receiver_dev(hdev);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001748 return retval;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001749}
1750
1751#ifdef CONFIG_PM
1752static int logi_dj_reset_resume(struct hid_device *hdev)
1753{
1754 int retval;
1755 struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
1756
Hans de Goededa12b222019-04-20 13:21:59 +02001757 if (!djrcv_dev || djrcv_dev->hidpp != hdev)
Hans de Goede74808f92019-04-20 13:21:54 +02001758 return 0;
1759
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001760 retval = logi_dj_recv_switch_to_dj_mode(djrcv_dev, 0);
1761 if (retval < 0) {
Hans de Goedeaca22a32019-04-20 13:21:58 +02001762 hid_err(hdev, "%s: logi_dj_recv_switch_to_dj_mode returned error:%d\n",
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001763 __func__, retval);
1764 }
1765
1766 return 0;
1767}
1768#endif
1769
1770static void logi_dj_remove(struct hid_device *hdev)
1771{
1772 struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
1773 struct dj_device *dj_dev;
Hans de Goedea1d97cc2019-04-20 13:21:52 +02001774 unsigned long flags;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001775 int i;
1776
1777 dbg_hid("%s\n", __func__);
1778
Hans de Goededa12b222019-04-20 13:21:59 +02001779 if (!djrcv_dev)
1780 return hid_hw_stop(hdev);
1781
Hans de Goedea1d97cc2019-04-20 13:21:52 +02001782 /*
1783 * This ensures that if the work gets requeued from another
1784 * interface of the same receiver it will be a no-op.
1785 */
1786 spin_lock_irqsave(&djrcv_dev->lock, flags);
1787 djrcv_dev->ready = false;
1788 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
1789
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001790 cancel_work_sync(&djrcv_dev->work);
1791
Benjamin Tissoiresddf75402013-07-12 11:01:02 +02001792 hid_hw_close(hdev);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001793 hid_hw_stop(hdev);
1794
Hans de Goedea1d97cc2019-04-20 13:21:52 +02001795 /*
1796 * For proper operation we need access to all interfaces, so we destroy
1797 * the paired devices when we're unbound from any interface.
1798 *
1799 * Note we may still be bound to other interfaces, sharing the same
1800 * djrcv_dev, so we need locking here.
1801 */
Nestor Lopez Casado844580f2011-09-20 15:59:03 +02001802 for (i = 0; i < (DJ_MAX_PAIRED_DEVICES + DJ_DEVICE_INDEX_MIN); i++) {
Hans de Goedea1d97cc2019-04-20 13:21:52 +02001803 spin_lock_irqsave(&djrcv_dev->lock, flags);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001804 dj_dev = djrcv_dev->paired_dj_devices[i];
Hans de Goedea1d97cc2019-04-20 13:21:52 +02001805 djrcv_dev->paired_dj_devices[i] = NULL;
1806 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001807 if (dj_dev != NULL) {
1808 hid_destroy_device(dj_dev->hdev);
1809 kfree(dj_dev);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001810 }
1811 }
1812
Hans de Goedea1d97cc2019-04-20 13:21:52 +02001813 dj_put_receiver_dev(hdev);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001814}
1815
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001816static const struct hid_device_id logi_dj_receivers[] = {
1817 {HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
Hans de Goede74808f92019-04-20 13:21:54 +02001818 USB_DEVICE_ID_LOGITECH_UNIFYING_RECEIVER),
1819 .driver_data = recvr_type_dj},
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001820 {HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
Hans de Goede74808f92019-04-20 13:21:54 +02001821 USB_DEVICE_ID_LOGITECH_UNIFYING_RECEIVER_2),
1822 .driver_data = recvr_type_dj},
Hans de Goede3ed224e2019-06-05 14:44:08 +02001823 { /* Logitech Nano mouse only receiver */
Hans de Goede74808f92019-04-20 13:21:54 +02001824 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
1825 USB_DEVICE_ID_LOGITECH_NANO_RECEIVER),
Hans de Goede3ed224e2019-06-05 14:44:08 +02001826 .driver_data = recvr_type_mouse_only},
Hans de Goede74808f92019-04-20 13:21:54 +02001827 { /* Logitech Nano (non DJ) receiver */
1828 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
1829 USB_DEVICE_ID_LOGITECH_NANO_RECEIVER_2),
1830 .driver_data = recvr_type_hidpp},
Benjamin Tissoiresf5fb57a2019-04-20 13:21:55 +02001831 { /* Logitech gaming receiver (0xc539) */
1832 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
1833 USB_DEVICE_ID_LOGITECH_NANO_RECEIVER_GAMING),
1834 .driver_data = recvr_type_gaming_hidpp},
Hans de Goedec9121cf2019-04-20 13:21:56 +02001835 { /* Logitech 27 MHz HID++ 1.0 receiver (0xc517) */
1836 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
1837 USB_DEVICE_ID_S510_RECEIVER_2),
1838 .driver_data = recvr_type_27mhz},
Hans de Goede1f944ac2019-04-20 13:21:57 +02001839 { /* Logitech 27 MHz HID++ 1.0 mouse-only receiver (0xc51b) */
1840 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
1841 USB_DEVICE_ID_LOGITECH_27MHZ_MOUSE_RECEIVER),
1842 .driver_data = recvr_type_27mhz},
Hans de Goedef2113c32019-04-20 13:22:03 +02001843 { /* Logitech MX5000 HID++ / bluetooth receiver keyboard intf. */
1844 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
1845 0xc70e),
1846 .driver_data = recvr_type_bluetooth},
1847 { /* Logitech MX5000 HID++ / bluetooth receiver mouse intf. */
1848 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
1849 0xc70a),
1850 .driver_data = recvr_type_bluetooth},
Hans de Goedeb9a94fb2019-04-28 21:25:51 +02001851 { /* Logitech MX5500 HID++ / bluetooth receiver keyboard intf. */
1852 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
1853 0xc71b),
1854 .driver_data = recvr_type_bluetooth},
1855 { /* Logitech MX5500 HID++ / bluetooth receiver mouse intf. */
1856 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
1857 0xc71c),
1858 .driver_data = recvr_type_bluetooth},
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001859 {}
1860};
1861
1862MODULE_DEVICE_TABLE(hid, logi_dj_receivers);
1863
1864static struct hid_driver logi_djreceiver_driver = {
1865 .name = "logitech-djreceiver",
1866 .id_table = logi_dj_receivers,
1867 .probe = logi_dj_probe,
1868 .remove = logi_dj_remove,
1869 .raw_event = logi_dj_raw_event,
1870#ifdef CONFIG_PM
1871 .reset_resume = logi_dj_reset_resume,
1872#endif
1873};
1874
Benjamin Tissoiresab94e562014-09-30 13:18:28 -04001875module_hid_driver(logi_djreceiver_driver);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001876
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001877MODULE_LICENSE("GPL");
1878MODULE_AUTHOR("Logitech");
1879MODULE_AUTHOR("Nestor Lopez Casado");
1880MODULE_AUTHOR("nlopezcasad@logitech.com");