blob: fa835d5659825b9f8c3c40b5b3e917ffa475b6e8 [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/*
Filipe Laínsc08ce252020-01-12 23:50:09 +00003 * HID driver for Logitech receivers
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02004 *
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
Filipe Laíns8c9d7342020-01-15 20:18:11 +000019#define DJ_MAX_PAIRED_DEVICES 7
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
Filipe Laíns8c9d7342020-01-15 20:18:11 +000023#define DJ_DEVICE_INDEX_MAX 7
Benjamin Tissoires8cb37462014-09-30 13:18:26 -040024
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
Hans de Goede3a9a2c82019-06-14 10:20:27 +020033#define REPORT_ID_HIDPP_VERY_LONG 0x12
Benjamin Tissoires925f0f32014-09-30 13:18:29 -040034
35#define HIDPP_REPORT_SHORT_LENGTH 7
36#define HIDPP_REPORT_LONG_LENGTH 20
37
38#define HIDPP_RECEIVER_INDEX 0xff
39
40#define REPORT_TYPE_RFREPORT_FIRST 0x01
Benjamin Tissoires8cb37462014-09-30 13:18:26 -040041#define REPORT_TYPE_RFREPORT_LAST 0x1F
42
43/* Command Switch to DJ mode */
44#define REPORT_TYPE_CMD_SWITCH 0x80
45#define CMD_SWITCH_PARAM_DEVBITFIELD 0x00
46#define CMD_SWITCH_PARAM_TIMEOUT_SECONDS 0x01
47#define TIMEOUT_NO_KEEPALIVE 0x00
48
49/* Command to Get the list of Paired devices */
50#define REPORT_TYPE_CMD_GET_PAIRED_DEVICES 0x81
51
52/* Device Paired Notification */
53#define REPORT_TYPE_NOTIF_DEVICE_PAIRED 0x41
54#define SPFUNCTION_MORE_NOTIF_EXPECTED 0x01
55#define SPFUNCTION_DEVICE_LIST_EMPTY 0x02
56#define DEVICE_PAIRED_PARAM_SPFUNCTION 0x00
57#define DEVICE_PAIRED_PARAM_EQUAD_ID_LSB 0x01
58#define DEVICE_PAIRED_PARAM_EQUAD_ID_MSB 0x02
59#define DEVICE_PAIRED_RF_REPORT_TYPE 0x03
60
61/* Device Un-Paired Notification */
62#define REPORT_TYPE_NOTIF_DEVICE_UNPAIRED 0x40
63
Benjamin Tissoires8cb37462014-09-30 13:18:26 -040064/* Connection Status Notification */
65#define REPORT_TYPE_NOTIF_CONNECTION_STATUS 0x42
66#define CONNECTION_STATUS_PARAM_STATUS 0x00
67#define STATUS_LINKLOSS 0x01
68
69/* Error Notification */
70#define REPORT_TYPE_NOTIF_ERROR 0x7F
71#define NOTIF_ERROR_PARAM_ETYPE 0x00
72#define ETYPE_KEEPALIVE_TIMEOUT 0x01
73
74/* supported DJ HID && RF report types */
75#define REPORT_TYPE_KEYBOARD 0x01
76#define REPORT_TYPE_MOUSE 0x02
77#define REPORT_TYPE_CONSUMER_CONTROL 0x03
78#define REPORT_TYPE_SYSTEM_CONTROL 0x04
79#define REPORT_TYPE_MEDIA_CENTER 0x08
80#define REPORT_TYPE_LEDS 0x0E
81
82/* RF Report types bitfield */
Benjamin Tissoiresa17dd1f2019-04-20 13:21:45 +020083#define STD_KEYBOARD BIT(1)
84#define STD_MOUSE BIT(2)
85#define MULTIMEDIA BIT(3)
86#define POWER_KEYS BIT(4)
Hans de Goede434f7702021-02-04 21:56:17 +010087#define KBD_MOUSE BIT(5)
Benjamin Tissoiresa17dd1f2019-04-20 13:21:45 +020088#define MEDIA_CENTER BIT(8)
89#define KBD_LEDS BIT(14)
Hans de Goede6d3c3f02019-04-20 13:22:02 +020090/* Fake (bitnr > NUMBER_OF_HID_REPORTS) bit to track HID++ capability */
91#define HIDPP BIT_ULL(63)
Benjamin Tissoires8cb37462014-09-30 13:18:26 -040092
Hans de Goede74808f92019-04-20 13:21:54 +020093/* HID++ Device Connected Notification */
94#define REPORT_TYPE_NOTIF_DEVICE_CONNECTED 0x41
95#define HIDPP_PARAM_PROTO_TYPE 0x00
96#define HIDPP_PARAM_DEVICE_INFO 0x01
97#define HIDPP_PARAM_EQUAD_LSB 0x02
98#define HIDPP_PARAM_EQUAD_MSB 0x03
Hans de Goedec9121cf2019-04-20 13:21:56 +020099#define HIDPP_PARAM_27MHZ_DEVID 0x03
Hans de Goede74808f92019-04-20 13:21:54 +0200100#define HIDPP_DEVICE_TYPE_MASK GENMASK(3, 0)
101#define HIDPP_LINK_STATUS_MASK BIT(6)
Hans de Goedef2113c32019-04-20 13:22:03 +0200102#define HIDPP_MANUFACTURER_MASK BIT(7)
Hans de Goede74808f92019-04-20 13:21:54 +0200103
Hans de Goedede76b1d2019-04-20 13:22:00 +0200104#define HIDPP_DEVICE_TYPE_KEYBOARD 1
105#define HIDPP_DEVICE_TYPE_MOUSE 2
106
Hans de Goede74808f92019-04-20 13:21:54 +0200107#define HIDPP_SET_REGISTER 0x80
Benjamin Tissoiresc0340412019-04-20 13:21:46 +0200108#define HIDPP_GET_LONG_REGISTER 0x83
Hans de Goede74808f92019-04-20 13:21:54 +0200109#define HIDPP_REG_CONNECTION_STATE 0x02
Benjamin Tissoiresc0340412019-04-20 13:21:46 +0200110#define HIDPP_REG_PAIRING_INFORMATION 0xB5
111#define HIDPP_PAIRING_INFORMATION 0x20
Hans de Goede74808f92019-04-20 13:21:54 +0200112#define HIDPP_FAKE_DEVICE_ARRIVAL 0x02
113
114enum recvr_type {
115 recvr_type_dj,
116 recvr_type_hidpp,
Benjamin Tissoiresf5fb57a2019-04-20 13:21:55 +0200117 recvr_type_gaming_hidpp,
Hans de Goede3ed224e2019-06-05 14:44:08 +0200118 recvr_type_mouse_only,
Hans de Goedec9121cf2019-04-20 13:21:56 +0200119 recvr_type_27mhz,
Hans de Goedef2113c32019-04-20 13:22:03 +0200120 recvr_type_bluetooth,
Hans de Goede434f7702021-02-04 21:56:17 +0100121 recvr_type_dinovo,
Hans de Goede74808f92019-04-20 13:21:54 +0200122};
Benjamin Tissoiresc0340412019-04-20 13:21:46 +0200123
Benjamin Tissoires8cb37462014-09-30 13:18:26 -0400124struct dj_report {
125 u8 report_id;
126 u8 device_index;
127 u8 report_type;
128 u8 report_params[DJREPORT_SHORT_LENGTH - 3];
129};
130
Benjamin Tissoires7bb56a52019-04-20 13:21:44 +0200131struct hidpp_event {
132 u8 report_id;
133 u8 device_index;
134 u8 sub_id;
135 u8 params[HIDPP_REPORT_LONG_LENGTH - 3U];
136} __packed;
137
Benjamin Tissoires8cb37462014-09-30 13:18:26 -0400138struct dj_receiver_dev {
Hans de Goedea1d97cc2019-04-20 13:21:52 +0200139 struct hid_device *mouse;
140 struct hid_device *keyboard;
Hans de Goede0ee75542019-04-20 13:21:51 +0200141 struct hid_device *hidpp;
Benjamin Tissoires8cb37462014-09-30 13:18:26 -0400142 struct dj_device *paired_dj_devices[DJ_MAX_PAIRED_DEVICES +
143 DJ_DEVICE_INDEX_MIN];
Hans de Goedea1d97cc2019-04-20 13:21:52 +0200144 struct list_head list;
145 struct kref kref;
Benjamin Tissoires8cb37462014-09-30 13:18:26 -0400146 struct work_struct work;
147 struct kfifo notif_fifo;
Hans de Goedeb6aeedd2019-04-20 13:21:53 +0200148 unsigned long last_query; /* in jiffies */
Hans de Goedea1d97cc2019-04-20 13:21:52 +0200149 bool ready;
Hans de Goede74808f92019-04-20 13:21:54 +0200150 enum recvr_type type;
151 unsigned int unnumbered_application;
Benjamin Tissoires8cb37462014-09-30 13:18:26 -0400152 spinlock_t lock;
Benjamin Tissoires8cb37462014-09-30 13:18:26 -0400153};
154
155struct dj_device {
156 struct hid_device *hdev;
157 struct dj_receiver_dev *dj_receiver_dev;
Hans de Goede6d3c3f02019-04-20 13:22:02 +0200158 u64 reports_supported;
Benjamin Tissoires8cb37462014-09-30 13:18:26 -0400159 u8 device_index;
160};
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200161
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200162#define WORKITEM_TYPE_EMPTY 0
163#define WORKITEM_TYPE_PAIRED 1
164#define WORKITEM_TYPE_UNPAIRED 2
165#define WORKITEM_TYPE_UNKNOWN 255
166
167struct dj_workitem {
168 u8 type; /* WORKITEM_TYPE_* */
169 u8 device_index;
Hans de Goedede76b1d2019-04-20 13:22:00 +0200170 u8 device_type;
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200171 u8 quad_id_msb;
172 u8 quad_id_lsb;
Hans de Goede6d3c3f02019-04-20 13:22:02 +0200173 u64 reports_supported;
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200174};
175
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200176/* Keyboard descriptor (1) */
177static const char kbd_descriptor[] = {
178 0x05, 0x01, /* USAGE_PAGE (generic Desktop) */
179 0x09, 0x06, /* USAGE (Keyboard) */
180 0xA1, 0x01, /* COLLECTION (Application) */
181 0x85, 0x01, /* REPORT_ID (1) */
182 0x95, 0x08, /* REPORT_COUNT (8) */
183 0x75, 0x01, /* REPORT_SIZE (1) */
184 0x15, 0x00, /* LOGICAL_MINIMUM (0) */
185 0x25, 0x01, /* LOGICAL_MAXIMUM (1) */
186 0x05, 0x07, /* USAGE_PAGE (Keyboard) */
187 0x19, 0xE0, /* USAGE_MINIMUM (Left Control) */
188 0x29, 0xE7, /* USAGE_MAXIMUM (Right GUI) */
189 0x81, 0x02, /* INPUT (Data,Var,Abs) */
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200190 0x95, 0x06, /* REPORT_COUNT (6) */
191 0x75, 0x08, /* REPORT_SIZE (8) */
192 0x15, 0x00, /* LOGICAL_MINIMUM (0) */
193 0x26, 0xFF, 0x00, /* LOGICAL_MAXIMUM (255) */
194 0x05, 0x07, /* USAGE_PAGE (Keyboard) */
195 0x19, 0x00, /* USAGE_MINIMUM (no event) */
196 0x2A, 0xFF, 0x00, /* USAGE_MAXIMUM (reserved) */
197 0x81, 0x00, /* INPUT (Data,Ary,Abs) */
Benjamin Tissoires0e40d352014-02-05 16:33:17 -0500198 0x85, 0x0e, /* REPORT_ID (14) */
199 0x05, 0x08, /* USAGE PAGE (LED page) */
200 0x95, 0x05, /* REPORT COUNT (5) */
201 0x75, 0x01, /* REPORT SIZE (1) */
202 0x15, 0x00, /* LOGICAL_MINIMUM (0) */
203 0x25, 0x01, /* LOGICAL_MAXIMUM (1) */
204 0x19, 0x01, /* USAGE MINIMUM (1) */
205 0x29, 0x05, /* USAGE MAXIMUM (5) */
206 0x91, 0x02, /* OUTPUT (Data, Variable, Absolute) */
207 0x95, 0x01, /* REPORT COUNT (1) */
208 0x75, 0x03, /* REPORT SIZE (3) */
209 0x91, 0x01, /* OUTPUT (Constant) */
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200210 0xC0
211};
212
213/* Mouse descriptor (2) */
214static const char mse_descriptor[] = {
215 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */
216 0x09, 0x02, /* USAGE (Mouse) */
217 0xA1, 0x01, /* COLLECTION (Application) */
218 0x85, 0x02, /* REPORT_ID = 2 */
219 0x09, 0x01, /* USAGE (pointer) */
220 0xA1, 0x00, /* COLLECTION (physical) */
221 0x05, 0x09, /* USAGE_PAGE (buttons) */
222 0x19, 0x01, /* USAGE_MIN (1) */
223 0x29, 0x10, /* USAGE_MAX (16) */
224 0x15, 0x00, /* LOGICAL_MIN (0) */
225 0x25, 0x01, /* LOGICAL_MAX (1) */
226 0x95, 0x10, /* REPORT_COUNT (16) */
227 0x75, 0x01, /* REPORT_SIZE (1) */
228 0x81, 0x02, /* INPUT (data var abs) */
229 0x05, 0x01, /* USAGE_PAGE (generic desktop) */
230 0x16, 0x01, 0xF8, /* LOGICAL_MIN (-2047) */
231 0x26, 0xFF, 0x07, /* LOGICAL_MAX (2047) */
232 0x75, 0x0C, /* REPORT_SIZE (12) */
233 0x95, 0x02, /* REPORT_COUNT (2) */
234 0x09, 0x30, /* USAGE (X) */
235 0x09, 0x31, /* USAGE (Y) */
236 0x81, 0x06, /* INPUT */
237 0x15, 0x81, /* LOGICAL_MIN (-127) */
238 0x25, 0x7F, /* LOGICAL_MAX (127) */
239 0x75, 0x08, /* REPORT_SIZE (8) */
240 0x95, 0x01, /* REPORT_COUNT (1) */
241 0x09, 0x38, /* USAGE (wheel) */
242 0x81, 0x06, /* INPUT */
243 0x05, 0x0C, /* USAGE_PAGE(consumer) */
244 0x0A, 0x38, 0x02, /* USAGE(AC Pan) */
245 0x95, 0x01, /* REPORT_COUNT (1) */
246 0x81, 0x06, /* INPUT */
247 0xC0, /* END_COLLECTION */
248 0xC0, /* END_COLLECTION */
249};
250
Hans de Goedec9121cf2019-04-20 13:21:56 +0200251/* Mouse descriptor (2) for 27 MHz receiver, only 8 buttons */
252static const char mse_27mhz_descriptor[] = {
253 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */
254 0x09, 0x02, /* USAGE (Mouse) */
255 0xA1, 0x01, /* COLLECTION (Application) */
256 0x85, 0x02, /* REPORT_ID = 2 */
257 0x09, 0x01, /* USAGE (pointer) */
258 0xA1, 0x00, /* COLLECTION (physical) */
259 0x05, 0x09, /* USAGE_PAGE (buttons) */
260 0x19, 0x01, /* USAGE_MIN (1) */
261 0x29, 0x08, /* USAGE_MAX (8) */
262 0x15, 0x00, /* LOGICAL_MIN (0) */
263 0x25, 0x01, /* LOGICAL_MAX (1) */
264 0x95, 0x08, /* REPORT_COUNT (8) */
265 0x75, 0x01, /* REPORT_SIZE (1) */
266 0x81, 0x02, /* INPUT (data var abs) */
267 0x05, 0x01, /* USAGE_PAGE (generic desktop) */
268 0x16, 0x01, 0xF8, /* LOGICAL_MIN (-2047) */
269 0x26, 0xFF, 0x07, /* LOGICAL_MAX (2047) */
270 0x75, 0x0C, /* REPORT_SIZE (12) */
271 0x95, 0x02, /* REPORT_COUNT (2) */
272 0x09, 0x30, /* USAGE (X) */
273 0x09, 0x31, /* USAGE (Y) */
274 0x81, 0x06, /* INPUT */
275 0x15, 0x81, /* LOGICAL_MIN (-127) */
276 0x25, 0x7F, /* LOGICAL_MAX (127) */
277 0x75, 0x08, /* REPORT_SIZE (8) */
278 0x95, 0x01, /* REPORT_COUNT (1) */
279 0x09, 0x38, /* USAGE (wheel) */
280 0x81, 0x06, /* INPUT */
281 0x05, 0x0C, /* USAGE_PAGE(consumer) */
282 0x0A, 0x38, 0x02, /* USAGE(AC Pan) */
283 0x95, 0x01, /* REPORT_COUNT (1) */
284 0x81, 0x06, /* INPUT */
285 0xC0, /* END_COLLECTION */
286 0xC0, /* END_COLLECTION */
287};
288
Hans de Goedef2113c32019-04-20 13:22:03 +0200289/* Mouse descriptor (2) for Bluetooth receiver, low-res hwheel, 12 buttons */
290static const char mse_bluetooth_descriptor[] = {
291 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */
292 0x09, 0x02, /* USAGE (Mouse) */
293 0xA1, 0x01, /* COLLECTION (Application) */
294 0x85, 0x02, /* REPORT_ID = 2 */
295 0x09, 0x01, /* USAGE (pointer) */
296 0xA1, 0x00, /* COLLECTION (physical) */
297 0x05, 0x09, /* USAGE_PAGE (buttons) */
298 0x19, 0x01, /* USAGE_MIN (1) */
299 0x29, 0x08, /* USAGE_MAX (8) */
300 0x15, 0x00, /* LOGICAL_MIN (0) */
301 0x25, 0x01, /* LOGICAL_MAX (1) */
302 0x95, 0x08, /* REPORT_COUNT (8) */
303 0x75, 0x01, /* REPORT_SIZE (1) */
304 0x81, 0x02, /* INPUT (data var abs) */
305 0x05, 0x01, /* USAGE_PAGE (generic desktop) */
306 0x16, 0x01, 0xF8, /* LOGICAL_MIN (-2047) */
307 0x26, 0xFF, 0x07, /* LOGICAL_MAX (2047) */
308 0x75, 0x0C, /* REPORT_SIZE (12) */
309 0x95, 0x02, /* REPORT_COUNT (2) */
310 0x09, 0x30, /* USAGE (X) */
311 0x09, 0x31, /* USAGE (Y) */
312 0x81, 0x06, /* INPUT */
313 0x15, 0x81, /* LOGICAL_MIN (-127) */
314 0x25, 0x7F, /* LOGICAL_MAX (127) */
315 0x75, 0x08, /* REPORT_SIZE (8) */
316 0x95, 0x01, /* REPORT_COUNT (1) */
317 0x09, 0x38, /* USAGE (wheel) */
318 0x81, 0x06, /* INPUT */
319 0x05, 0x0C, /* USAGE_PAGE(consumer) */
320 0x0A, 0x38, 0x02, /* USAGE(AC Pan) */
321 0x15, 0xF9, /* LOGICAL_MIN (-7) */
322 0x25, 0x07, /* LOGICAL_MAX (7) */
323 0x75, 0x04, /* REPORT_SIZE (4) */
324 0x95, 0x01, /* REPORT_COUNT (1) */
325 0x81, 0x06, /* INPUT */
326 0x05, 0x09, /* USAGE_PAGE (buttons) */
327 0x19, 0x09, /* USAGE_MIN (9) */
328 0x29, 0x0C, /* USAGE_MAX (12) */
329 0x15, 0x00, /* LOGICAL_MIN (0) */
330 0x25, 0x01, /* LOGICAL_MAX (1) */
331 0x75, 0x01, /* REPORT_SIZE (1) */
332 0x95, 0x04, /* REPORT_COUNT (4) */
Hans de Goedeeec231e2020-11-14 10:45:31 +0100333 0x81, 0x02, /* INPUT (Data,Var,Abs) */
Hans de Goedef2113c32019-04-20 13:22:03 +0200334 0xC0, /* END_COLLECTION */
335 0xC0, /* END_COLLECTION */
336};
337
Hans de Goede434f7702021-02-04 21:56:17 +0100338/* Mouse descriptor (5) for Bluetooth receiver, normal-res hwheel, 8 buttons */
339static const char mse5_bluetooth_descriptor[] = {
340 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */
341 0x09, 0x02, /* Usage (Mouse) */
342 0xa1, 0x01, /* Collection (Application) */
343 0x85, 0x05, /* Report ID (5) */
344 0x09, 0x01, /* Usage (Pointer) */
345 0xa1, 0x00, /* Collection (Physical) */
346 0x05, 0x09, /* Usage Page (Button) */
347 0x19, 0x01, /* Usage Minimum (1) */
348 0x29, 0x08, /* Usage Maximum (8) */
349 0x15, 0x00, /* Logical Minimum (0) */
350 0x25, 0x01, /* Logical Maximum (1) */
351 0x95, 0x08, /* Report Count (8) */
352 0x75, 0x01, /* Report Size (1) */
353 0x81, 0x02, /* Input (Data,Var,Abs) */
354 0x05, 0x01, /* Usage Page (Generic Desktop) */
355 0x16, 0x01, 0xf8, /* Logical Minimum (-2047) */
356 0x26, 0xff, 0x07, /* Logical Maximum (2047) */
357 0x75, 0x0c, /* Report Size (12) */
358 0x95, 0x02, /* Report Count (2) */
359 0x09, 0x30, /* Usage (X) */
360 0x09, 0x31, /* Usage (Y) */
361 0x81, 0x06, /* Input (Data,Var,Rel) */
362 0x15, 0x81, /* Logical Minimum (-127) */
363 0x25, 0x7f, /* Logical Maximum (127) */
364 0x75, 0x08, /* Report Size (8) */
365 0x95, 0x01, /* Report Count (1) */
366 0x09, 0x38, /* Usage (Wheel) */
367 0x81, 0x06, /* Input (Data,Var,Rel) */
368 0x05, 0x0c, /* Usage Page (Consumer Devices) */
369 0x0a, 0x38, 0x02, /* Usage (AC Pan) */
370 0x15, 0x81, /* Logical Minimum (-127) */
371 0x25, 0x7f, /* Logical Maximum (127) */
372 0x75, 0x08, /* Report Size (8) */
373 0x95, 0x01, /* Report Count (1) */
374 0x81, 0x06, /* Input (Data,Var,Rel) */
375 0xc0, /* End Collection */
376 0xc0, /* End Collection */
377};
378
Benjamin Tissoiresf5fb57a2019-04-20 13:21:55 +0200379/* Gaming Mouse descriptor (2) */
380static const char mse_high_res_descriptor[] = {
381 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */
382 0x09, 0x02, /* USAGE (Mouse) */
383 0xA1, 0x01, /* COLLECTION (Application) */
384 0x85, 0x02, /* REPORT_ID = 2 */
385 0x09, 0x01, /* USAGE (pointer) */
386 0xA1, 0x00, /* COLLECTION (physical) */
387 0x05, 0x09, /* USAGE_PAGE (buttons) */
388 0x19, 0x01, /* USAGE_MIN (1) */
389 0x29, 0x10, /* USAGE_MAX (16) */
390 0x15, 0x00, /* LOGICAL_MIN (0) */
391 0x25, 0x01, /* LOGICAL_MAX (1) */
392 0x95, 0x10, /* REPORT_COUNT (16) */
393 0x75, 0x01, /* REPORT_SIZE (1) */
394 0x81, 0x02, /* INPUT (data var abs) */
395 0x05, 0x01, /* USAGE_PAGE (generic desktop) */
396 0x16, 0x01, 0x80, /* LOGICAL_MIN (-32767) */
397 0x26, 0xFF, 0x7F, /* LOGICAL_MAX (32767) */
398 0x75, 0x10, /* REPORT_SIZE (16) */
399 0x95, 0x02, /* REPORT_COUNT (2) */
400 0x09, 0x30, /* USAGE (X) */
401 0x09, 0x31, /* USAGE (Y) */
402 0x81, 0x06, /* INPUT */
403 0x15, 0x81, /* LOGICAL_MIN (-127) */
404 0x25, 0x7F, /* LOGICAL_MAX (127) */
405 0x75, 0x08, /* REPORT_SIZE (8) */
406 0x95, 0x01, /* REPORT_COUNT (1) */
407 0x09, 0x38, /* USAGE (wheel) */
408 0x81, 0x06, /* INPUT */
409 0x05, 0x0C, /* USAGE_PAGE(consumer) */
410 0x0A, 0x38, 0x02, /* USAGE(AC Pan) */
411 0x95, 0x01, /* REPORT_COUNT (1) */
412 0x81, 0x06, /* INPUT */
413 0xC0, /* END_COLLECTION */
414 0xC0, /* END_COLLECTION */
415};
416
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200417/* Consumer Control descriptor (3) */
418static const char consumer_descriptor[] = {
419 0x05, 0x0C, /* USAGE_PAGE (Consumer Devices) */
420 0x09, 0x01, /* USAGE (Consumer Control) */
421 0xA1, 0x01, /* COLLECTION (Application) */
422 0x85, 0x03, /* REPORT_ID = 3 */
423 0x75, 0x10, /* REPORT_SIZE (16) */
424 0x95, 0x02, /* REPORT_COUNT (2) */
425 0x15, 0x01, /* LOGICAL_MIN (1) */
Olivier Gay5b6cc122019-07-29 19:21:52 +0200426 0x26, 0xFF, 0x02, /* LOGICAL_MAX (767) */
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200427 0x19, 0x01, /* USAGE_MIN (1) */
Olivier Gay5b6cc122019-07-29 19:21:52 +0200428 0x2A, 0xFF, 0x02, /* USAGE_MAX (767) */
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200429 0x81, 0x00, /* INPUT (Data Ary Abs) */
430 0xC0, /* END_COLLECTION */
431}; /* */
432
433/* System control descriptor (4) */
434static const char syscontrol_descriptor[] = {
435 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */
436 0x09, 0x80, /* USAGE (System Control) */
437 0xA1, 0x01, /* COLLECTION (Application) */
438 0x85, 0x04, /* REPORT_ID = 4 */
439 0x75, 0x02, /* REPORT_SIZE (2) */
440 0x95, 0x01, /* REPORT_COUNT (1) */
441 0x15, 0x01, /* LOGICAL_MIN (1) */
442 0x25, 0x03, /* LOGICAL_MAX (3) */
443 0x09, 0x82, /* USAGE (System Sleep) */
444 0x09, 0x81, /* USAGE (System Power Down) */
445 0x09, 0x83, /* USAGE (System Wake Up) */
446 0x81, 0x60, /* INPUT (Data Ary Abs NPrf Null) */
447 0x75, 0x06, /* REPORT_SIZE (6) */
448 0x81, 0x03, /* INPUT (Cnst Var Abs) */
449 0xC0, /* END_COLLECTION */
450};
451
452/* Media descriptor (8) */
453static const char media_descriptor[] = {
454 0x06, 0xbc, 0xff, /* Usage Page 0xffbc */
455 0x09, 0x88, /* Usage 0x0088 */
456 0xa1, 0x01, /* BeginCollection */
457 0x85, 0x08, /* Report ID 8 */
458 0x19, 0x01, /* Usage Min 0x0001 */
459 0x29, 0xff, /* Usage Max 0x00ff */
460 0x15, 0x01, /* Logical Min 1 */
461 0x26, 0xff, 0x00, /* Logical Max 255 */
462 0x75, 0x08, /* Report Size 8 */
463 0x95, 0x01, /* Report Count 1 */
464 0x81, 0x00, /* Input */
465 0xc0, /* EndCollection */
466}; /* */
467
Benjamin Tissoires925f0f32014-09-30 13:18:29 -0400468/* HIDPP descriptor */
469static const char hidpp_descriptor[] = {
470 0x06, 0x00, 0xff, /* Usage Page (Vendor Defined Page 1) */
471 0x09, 0x01, /* Usage (Vendor Usage 1) */
472 0xa1, 0x01, /* Collection (Application) */
473 0x85, 0x10, /* Report ID (16) */
474 0x75, 0x08, /* Report Size (8) */
475 0x95, 0x06, /* Report Count (6) */
476 0x15, 0x00, /* Logical Minimum (0) */
477 0x26, 0xff, 0x00, /* Logical Maximum (255) */
478 0x09, 0x01, /* Usage (Vendor Usage 1) */
479 0x81, 0x00, /* Input (Data,Arr,Abs) */
480 0x09, 0x01, /* Usage (Vendor Usage 1) */
481 0x91, 0x00, /* Output (Data,Arr,Abs) */
482 0xc0, /* End Collection */
483 0x06, 0x00, 0xff, /* Usage Page (Vendor Defined Page 1) */
484 0x09, 0x02, /* Usage (Vendor Usage 2) */
485 0xa1, 0x01, /* Collection (Application) */
486 0x85, 0x11, /* Report ID (17) */
487 0x75, 0x08, /* Report Size (8) */
488 0x95, 0x13, /* Report Count (19) */
489 0x15, 0x00, /* Logical Minimum (0) */
490 0x26, 0xff, 0x00, /* Logical Maximum (255) */
491 0x09, 0x02, /* Usage (Vendor Usage 2) */
492 0x81, 0x00, /* Input (Data,Arr,Abs) */
493 0x09, 0x02, /* Usage (Vendor Usage 2) */
494 0x91, 0x00, /* Output (Data,Arr,Abs) */
495 0xc0, /* End Collection */
496 0x06, 0x00, 0xff, /* Usage Page (Vendor Defined Page 1) */
497 0x09, 0x04, /* Usage (Vendor Usage 0x04) */
498 0xa1, 0x01, /* Collection (Application) */
499 0x85, 0x20, /* Report ID (32) */
500 0x75, 0x08, /* Report Size (8) */
501 0x95, 0x0e, /* Report Count (14) */
502 0x15, 0x00, /* Logical Minimum (0) */
503 0x26, 0xff, 0x00, /* Logical Maximum (255) */
504 0x09, 0x41, /* Usage (Vendor Usage 0x41) */
505 0x81, 0x00, /* Input (Data,Arr,Abs) */
506 0x09, 0x41, /* Usage (Vendor Usage 0x41) */
507 0x91, 0x00, /* Output (Data,Arr,Abs) */
508 0x85, 0x21, /* Report ID (33) */
509 0x95, 0x1f, /* Report Count (31) */
510 0x15, 0x00, /* Logical Minimum (0) */
511 0x26, 0xff, 0x00, /* Logical Maximum (255) */
512 0x09, 0x42, /* Usage (Vendor Usage 0x42) */
513 0x81, 0x00, /* Input (Data,Arr,Abs) */
514 0x09, 0x42, /* Usage (Vendor Usage 0x42) */
515 0x91, 0x00, /* Output (Data,Arr,Abs) */
516 0xc0, /* End Collection */
517};
518
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200519/* Maximum size of all defined hid reports in bytes (including report id) */
520#define MAX_REPORT_SIZE 8
521
Henrik Rydberg2a039bf2012-04-22 14:21:39 +0200522/* Make sure all descriptors are present here */
523#define MAX_RDESC_SIZE \
524 (sizeof(kbd_descriptor) + \
Hans de Goedef2113c32019-04-20 13:22:03 +0200525 sizeof(mse_bluetooth_descriptor) + \
Hans de Goede434f7702021-02-04 21:56:17 +0100526 sizeof(mse5_bluetooth_descriptor) + \
Henrik Rydberg2a039bf2012-04-22 14:21:39 +0200527 sizeof(consumer_descriptor) + \
528 sizeof(syscontrol_descriptor) + \
Benjamin Tissoires925f0f32014-09-30 13:18:29 -0400529 sizeof(media_descriptor) + \
530 sizeof(hidpp_descriptor))
Henrik Rydberg2a039bf2012-04-22 14:21:39 +0200531
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200532/* Number of possible hid report types that can be created by this driver.
533 *
534 * Right now, RF report types have the same report types (or report id's)
535 * than the hid report created from those RF reports. In the future
536 * this doesnt have to be true.
537 *
538 * For instance, RF report type 0x01 which has a size of 8 bytes, corresponds
539 * to hid report id 0x01, this is standard keyboard. Same thing applies to mice
540 * reports and consumer control, etc. If a new RF report is created, it doesn't
541 * has to have the same report id as its corresponding hid report, so an
542 * translation may have to take place for future report types.
543 */
544#define NUMBER_OF_HID_REPORTS 32
545static const u8 hid_reportid_size_map[NUMBER_OF_HID_REPORTS] = {
546 [1] = 8, /* Standard keyboard */
547 [2] = 8, /* Standard mouse */
548 [3] = 5, /* Consumer control */
549 [4] = 2, /* System control */
550 [8] = 2, /* Media Center */
551};
552
553
554#define LOGITECH_DJ_INTERFACE_NUMBER 0x02
555
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200556static struct hid_ll_driver logi_dj_ll_driver;
557
Nestor Lopez Casadoc63e0e32013-07-18 06:21:30 -0700558static int logi_dj_recv_query_paired_devices(struct dj_receiver_dev *djrcv_dev);
Hans de Goedea1d97cc2019-04-20 13:21:52 +0200559static void delayedwork_callback(struct work_struct *work);
560
561static LIST_HEAD(dj_hdev_list);
562static DEFINE_MUTEX(dj_hdev_list_lock);
563
Hans de Goede434f7702021-02-04 21:56:17 +0100564static bool recvr_type_is_bluetooth(enum recvr_type type)
565{
566 return type == recvr_type_bluetooth || type == recvr_type_dinovo;
567}
568
Hans de Goedea1d97cc2019-04-20 13:21:52 +0200569/*
570 * dj/HID++ receivers are really a single logical entity, but for BIOS/Windows
571 * compatibility they have multiple USB interfaces. On HID++ receivers we need
572 * to listen for input reports on both interfaces. The functions below are used
573 * to create a single struct dj_receiver_dev for all interfaces belonging to
574 * a single USB-device / receiver.
575 */
Hans de Goedef2113c32019-04-20 13:22:03 +0200576static struct dj_receiver_dev *dj_find_receiver_dev(struct hid_device *hdev,
577 enum recvr_type type)
Hans de Goedea1d97cc2019-04-20 13:21:52 +0200578{
579 struct dj_receiver_dev *djrcv_dev;
Hans de Goedef2113c32019-04-20 13:22:03 +0200580 char sep;
581
582 /*
583 * The bluetooth receiver contains a built-in hub and has separate
584 * USB-devices for the keyboard and mouse interfaces.
585 */
Hans de Goede434f7702021-02-04 21:56:17 +0100586 sep = recvr_type_is_bluetooth(type) ? '.' : '/';
Hans de Goedea1d97cc2019-04-20 13:21:52 +0200587
588 /* Try to find an already-probed interface from the same device */
589 list_for_each_entry(djrcv_dev, &dj_hdev_list, list) {
590 if (djrcv_dev->mouse &&
Hans de Goedef2113c32019-04-20 13:22:03 +0200591 hid_compare_device_paths(hdev, djrcv_dev->mouse, sep)) {
Hans de Goedea1d97cc2019-04-20 13:21:52 +0200592 kref_get(&djrcv_dev->kref);
593 return djrcv_dev;
594 }
595 if (djrcv_dev->keyboard &&
Hans de Goedef2113c32019-04-20 13:22:03 +0200596 hid_compare_device_paths(hdev, djrcv_dev->keyboard, sep)) {
Hans de Goedea1d97cc2019-04-20 13:21:52 +0200597 kref_get(&djrcv_dev->kref);
598 return djrcv_dev;
599 }
600 if (djrcv_dev->hidpp &&
Hans de Goedef2113c32019-04-20 13:22:03 +0200601 hid_compare_device_paths(hdev, djrcv_dev->hidpp, sep)) {
Hans de Goedea1d97cc2019-04-20 13:21:52 +0200602 kref_get(&djrcv_dev->kref);
603 return djrcv_dev;
604 }
605 }
606
607 return NULL;
608}
609
610static void dj_release_receiver_dev(struct kref *kref)
611{
612 struct dj_receiver_dev *djrcv_dev = container_of(kref, struct dj_receiver_dev, kref);
613
614 list_del(&djrcv_dev->list);
615 kfifo_free(&djrcv_dev->notif_fifo);
616 kfree(djrcv_dev);
617}
618
619static void dj_put_receiver_dev(struct hid_device *hdev)
620{
621 struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
622
623 mutex_lock(&dj_hdev_list_lock);
624
625 if (djrcv_dev->mouse == hdev)
626 djrcv_dev->mouse = NULL;
627 if (djrcv_dev->keyboard == hdev)
628 djrcv_dev->keyboard = NULL;
629 if (djrcv_dev->hidpp == hdev)
630 djrcv_dev->hidpp = NULL;
631
632 kref_put(&djrcv_dev->kref, dj_release_receiver_dev);
633
634 mutex_unlock(&dj_hdev_list_lock);
635}
636
637static struct dj_receiver_dev *dj_get_receiver_dev(struct hid_device *hdev,
Hans de Goede74808f92019-04-20 13:21:54 +0200638 enum recvr_type type,
Hans de Goedea1d97cc2019-04-20 13:21:52 +0200639 unsigned int application,
640 bool is_hidpp)
641{
642 struct dj_receiver_dev *djrcv_dev;
643
644 mutex_lock(&dj_hdev_list_lock);
645
Hans de Goedef2113c32019-04-20 13:22:03 +0200646 djrcv_dev = dj_find_receiver_dev(hdev, type);
Hans de Goedea1d97cc2019-04-20 13:21:52 +0200647 if (!djrcv_dev) {
648 djrcv_dev = kzalloc(sizeof(*djrcv_dev), GFP_KERNEL);
649 if (!djrcv_dev)
650 goto out;
651
652 INIT_WORK(&djrcv_dev->work, delayedwork_callback);
653 spin_lock_init(&djrcv_dev->lock);
654 if (kfifo_alloc(&djrcv_dev->notif_fifo,
655 DJ_MAX_NUMBER_NOTIFS * sizeof(struct dj_workitem),
656 GFP_KERNEL)) {
657 kfree(djrcv_dev);
658 djrcv_dev = NULL;
659 goto out;
660 }
661 kref_init(&djrcv_dev->kref);
662 list_add_tail(&djrcv_dev->list, &dj_hdev_list);
Hans de Goedeb6aeedd2019-04-20 13:21:53 +0200663 djrcv_dev->last_query = jiffies;
Hans de Goede74808f92019-04-20 13:21:54 +0200664 djrcv_dev->type = type;
Hans de Goedea1d97cc2019-04-20 13:21:52 +0200665 }
666
667 if (application == HID_GD_KEYBOARD)
668 djrcv_dev->keyboard = hdev;
669 if (application == HID_GD_MOUSE)
670 djrcv_dev->mouse = hdev;
671 if (is_hidpp)
672 djrcv_dev->hidpp = hdev;
673
674 hid_set_drvdata(hdev, djrcv_dev);
675out:
676 mutex_unlock(&dj_hdev_list_lock);
677 return djrcv_dev;
678}
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200679
680static void logi_dj_recv_destroy_djhid_device(struct dj_receiver_dev *djrcv_dev,
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200681 struct dj_workitem *workitem)
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200682{
683 /* Called in delayed work context */
684 struct dj_device *dj_dev;
685 unsigned long flags;
686
687 spin_lock_irqsave(&djrcv_dev->lock, flags);
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200688 dj_dev = djrcv_dev->paired_dj_devices[workitem->device_index];
689 djrcv_dev->paired_dj_devices[workitem->device_index] = NULL;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200690 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
691
692 if (dj_dev != NULL) {
693 hid_destroy_device(dj_dev->hdev);
694 kfree(dj_dev);
695 } else {
Hans de Goede0ee75542019-04-20 13:21:51 +0200696 hid_err(djrcv_dev->hidpp, "%s: can't destroy a NULL device\n",
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200697 __func__);
698 }
699}
700
701static void logi_dj_recv_add_djhid_device(struct dj_receiver_dev *djrcv_dev,
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200702 struct dj_workitem *workitem)
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200703{
704 /* Called in delayed work context */
Hans de Goede0ee75542019-04-20 13:21:51 +0200705 struct hid_device *djrcv_hdev = djrcv_dev->hidpp;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200706 struct hid_device *dj_hiddev;
707 struct dj_device *dj_dev;
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200708 u8 device_index = workitem->device_index;
Hans de Goedef41d7662019-04-20 13:21:50 +0200709 unsigned long flags;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200710
711 /* Device index goes from 1 to 6, we need 3 bytes to store the
712 * semicolon, the index, and a null terminator
713 */
714 unsigned char tmpstr[3];
715
Hans de Goedef41d7662019-04-20 13:21:50 +0200716 /* We are the only one ever adding a device, no need to lock */
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200717 if (djrcv_dev->paired_dj_devices[device_index]) {
Nestor Lopez Casadoc63e0e32013-07-18 06:21:30 -0700718 /* The device is already known. No need to reallocate it. */
719 dbg_hid("%s: device is already known\n", __func__);
720 return;
721 }
722
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200723 dj_hiddev = hid_allocate_device();
724 if (IS_ERR(dj_hiddev)) {
Hans de Goedeaca22a32019-04-20 13:21:58 +0200725 hid_err(djrcv_hdev, "%s: hid_allocate_dev failed\n", __func__);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200726 return;
727 }
728
729 dj_hiddev->ll_driver = &logi_dj_ll_driver;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200730
731 dj_hiddev->dev.parent = &djrcv_hdev->dev;
732 dj_hiddev->bus = BUS_USB;
Benjamin Tissoires82c0beb2019-04-20 13:21:47 +0200733 dj_hiddev->vendor = djrcv_hdev->vendor;
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200734 dj_hiddev->product = (workitem->quad_id_msb << 8) |
735 workitem->quad_id_lsb;
Hans de Goedede76b1d2019-04-20 13:22:00 +0200736 if (workitem->device_type) {
737 const char *type_str = "Device";
738
739 switch (workitem->device_type) {
740 case 0x01: type_str = "Keyboard"; break;
741 case 0x02: type_str = "Mouse"; break;
742 case 0x03: type_str = "Numpad"; break;
743 case 0x04: type_str = "Presenter"; break;
744 case 0x07: type_str = "Remote Control"; break;
745 case 0x08: type_str = "Trackball"; break;
746 case 0x09: type_str = "Touchpad"; break;
747 }
748 snprintf(dj_hiddev->name, sizeof(dj_hiddev->name),
749 "Logitech Wireless %s PID:%04x",
750 type_str, dj_hiddev->product);
751 } else {
752 snprintf(dj_hiddev->name, sizeof(dj_hiddev->name),
Filipe Laínsc08ce252020-01-12 23:50:09 +0000753 "Logitech Wireless Device PID:%04x",
Hans de Goedede76b1d2019-04-20 13:22:00 +0200754 dj_hiddev->product);
755 }
Benjamin Tissoiresd6102742014-09-30 13:18:25 -0400756
Hans de Goedec9121cf2019-04-20 13:21:56 +0200757 if (djrcv_dev->type == recvr_type_27mhz)
758 dj_hiddev->group = HID_GROUP_LOGITECH_27MHZ_DEVICE;
759 else
760 dj_hiddev->group = HID_GROUP_LOGITECH_DJ_DEVICE;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200761
Benjamin Tissoires82c0beb2019-04-20 13:21:47 +0200762 memcpy(dj_hiddev->phys, djrcv_hdev->phys, sizeof(djrcv_hdev->phys));
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200763 snprintf(tmpstr, sizeof(tmpstr), ":%d", device_index);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200764 strlcat(dj_hiddev->phys, tmpstr, sizeof(dj_hiddev->phys));
765
766 dj_dev = kzalloc(sizeof(struct dj_device), GFP_KERNEL);
767
768 if (!dj_dev) {
Hans de Goedeaca22a32019-04-20 13:21:58 +0200769 hid_err(djrcv_hdev, "%s: failed allocating dj_dev\n", __func__);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200770 goto dj_device_allocate_fail;
771 }
772
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200773 dj_dev->reports_supported = workitem->reports_supported;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200774 dj_dev->hdev = dj_hiddev;
775 dj_dev->dj_receiver_dev = djrcv_dev;
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200776 dj_dev->device_index = device_index;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200777 dj_hiddev->driver_data = dj_dev;
778
Hans de Goedef41d7662019-04-20 13:21:50 +0200779 spin_lock_irqsave(&djrcv_dev->lock, flags);
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200780 djrcv_dev->paired_dj_devices[device_index] = dj_dev;
Hans de Goedef41d7662019-04-20 13:21:50 +0200781 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200782
783 if (hid_add_device(dj_hiddev)) {
Hans de Goedeaca22a32019-04-20 13:21:58 +0200784 hid_err(djrcv_hdev, "%s: failed adding dj_device\n", __func__);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200785 goto hid_add_device_fail;
786 }
787
788 return;
789
790hid_add_device_fail:
Hans de Goedef41d7662019-04-20 13:21:50 +0200791 spin_lock_irqsave(&djrcv_dev->lock, flags);
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200792 djrcv_dev->paired_dj_devices[device_index] = NULL;
Hans de Goedef41d7662019-04-20 13:21:50 +0200793 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200794 kfree(dj_dev);
795dj_device_allocate_fail:
796 hid_destroy_device(dj_hiddev);
797}
798
799static void delayedwork_callback(struct work_struct *work)
800{
801 struct dj_receiver_dev *djrcv_dev =
802 container_of(work, struct dj_receiver_dev, work);
803
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200804 struct dj_workitem workitem;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200805 unsigned long flags;
806 int count;
Nestor Lopez Casadoc63e0e32013-07-18 06:21:30 -0700807 int retval;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200808
809 dbg_hid("%s\n", __func__);
810
811 spin_lock_irqsave(&djrcv_dev->lock, flags);
812
Hans de Goedea1d97cc2019-04-20 13:21:52 +0200813 /*
814 * Since we attach to multiple interfaces, we may get scheduled before
815 * we are bound to the HID++ interface, catch this.
816 */
817 if (!djrcv_dev->ready) {
818 pr_warn("%s: delayedwork queued before hidpp interface was enumerated\n",
819 __func__);
820 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
821 return;
822 }
823
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200824 count = kfifo_out(&djrcv_dev->notif_fifo, &workitem, sizeof(workitem));
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200825
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200826 if (count != sizeof(workitem)) {
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200827 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
828 return;
829 }
830
Hans de Goedee316aa62019-04-20 13:22:01 +0200831 if (!kfifo_is_empty(&djrcv_dev->notif_fifo))
832 schedule_work(&djrcv_dev->work);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200833
834 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
835
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200836 switch (workitem.type) {
837 case WORKITEM_TYPE_PAIRED:
838 logi_dj_recv_add_djhid_device(djrcv_dev, &workitem);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200839 break;
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200840 case WORKITEM_TYPE_UNPAIRED:
841 logi_dj_recv_destroy_djhid_device(djrcv_dev, &workitem);
842 break;
843 case WORKITEM_TYPE_UNKNOWN:
844 retval = logi_dj_recv_query_paired_devices(djrcv_dev);
845 if (retval) {
Hans de Goede0ee75542019-04-20 13:21:51 +0200846 hid_err(djrcv_dev->hidpp, "%s: logi_dj_recv_query_paired_devices error: %d\n",
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200847 __func__, retval);
848 }
849 break;
850 case WORKITEM_TYPE_EMPTY:
851 dbg_hid("%s: device list is empty\n", __func__);
852 break;
853 }
854}
855
Hans de Goedeb6aeedd2019-04-20 13:21:53 +0200856/*
857 * Sometimes we receive reports for which we do not have a paired dj_device
858 * associated with the device_index or report-type to forward the report to.
859 * This means that the original "device paired" notification corresponding
860 * to the dj_device never arrived to this driver. Possible reasons for this are:
861 * 1) hid-core discards all packets coming from a device during probe().
862 * 2) if the receiver is plugged into a KVM switch then the pairing reports
863 * are only forwarded to it if the focus is on this PC.
864 * This function deals with this by re-asking the receiver for the list of
865 * connected devices in the delayed work callback.
866 * This function MUST be called with djrcv->lock held.
867 */
868static void logi_dj_recv_queue_unknown_work(struct dj_receiver_dev *djrcv_dev)
869{
870 struct dj_workitem workitem = { .type = WORKITEM_TYPE_UNKNOWN };
871
Chris Packham53ed4cf2020-05-26 11:02:01 +1200872 /* Rate limit queries done because of unhandled reports to 2/sec */
Hans de Goedeb6aeedd2019-04-20 13:21:53 +0200873 if (time_before(jiffies, djrcv_dev->last_query + HZ / 2))
874 return;
875
876 kfifo_in(&djrcv_dev->notif_fifo, &workitem, sizeof(workitem));
877 schedule_work(&djrcv_dev->work);
878}
879
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200880static void logi_dj_recv_queue_notification(struct dj_receiver_dev *djrcv_dev,
881 struct dj_report *dj_report)
882{
883 /* We are called from atomic context (tasklet && djrcv->lock held) */
884 struct dj_workitem workitem = {
885 .device_index = dj_report->device_index,
886 };
887
888 switch (dj_report->report_type) {
889 case REPORT_TYPE_NOTIF_DEVICE_PAIRED:
890 workitem.type = WORKITEM_TYPE_PAIRED;
891 if (dj_report->report_params[DEVICE_PAIRED_PARAM_SPFUNCTION] &
892 SPFUNCTION_DEVICE_LIST_EMPTY) {
893 workitem.type = WORKITEM_TYPE_EMPTY;
894 break;
895 }
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500896 fallthrough;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200897 case REPORT_TYPE_NOTIF_DEVICE_UNPAIRED:
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200898 workitem.quad_id_msb =
899 dj_report->report_params[DEVICE_PAIRED_PARAM_EQUAD_ID_MSB];
900 workitem.quad_id_lsb =
901 dj_report->report_params[DEVICE_PAIRED_PARAM_EQUAD_ID_LSB];
902 workitem.reports_supported = get_unaligned_le32(
903 dj_report->report_params +
904 DEVICE_PAIRED_RF_REPORT_TYPE);
Hans de Goede6d3c3f02019-04-20 13:22:02 +0200905 workitem.reports_supported |= HIDPP;
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200906 if (dj_report->report_type == REPORT_TYPE_NOTIF_DEVICE_UNPAIRED)
907 workitem.type = WORKITEM_TYPE_UNPAIRED;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200908 break;
909 default:
Hans de Goedeb6aeedd2019-04-20 13:21:53 +0200910 logi_dj_recv_queue_unknown_work(djrcv_dev);
911 return;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200912 }
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200913
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200914 kfifo_in(&djrcv_dev->notif_fifo, &workitem, sizeof(workitem));
Hans de Goedee316aa62019-04-20 13:22:01 +0200915 schedule_work(&djrcv_dev->work);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200916}
917
Hans de Goedeee5e5842020-11-02 14:36:56 +0100918/*
919 * Some quad/bluetooth keyboards have a builtin touchpad in this case we see
920 * only 1 paired device with a device_type of REPORT_TYPE_KEYBOARD. For the
921 * touchpad to work we must also forward mouse input reports to the dj_hiddev
922 * created for the keyboard (instead of forwarding them to a second paired
923 * device with a device_type of REPORT_TYPE_MOUSE as we normally would).
Hans de Goede434f7702021-02-04 21:56:17 +0100924 *
925 * On Dinovo receivers the keyboard's touchpad and an optional paired actual
926 * mouse send separate input reports, INPUT(2) aka STD_MOUSE for the mouse
927 * and INPUT(5) aka KBD_MOUSE for the keyboard's touchpad.
928 *
929 * On MX5x00 receivers (which can also be paired with a Dinovo keyboard)
930 * INPUT(2) is used for both an optional paired actual mouse and for the
931 * keyboard's touchpad.
Hans de Goedeee5e5842020-11-02 14:36:56 +0100932 */
933static const u16 kbd_builtin_touchpad_ids[] = {
934 0xb309, /* Dinovo Edge */
Hans de Goedeb4c00e72020-11-14 22:20:56 +0100935 0xb30c, /* Dinovo Mini */
Hans de Goedeee5e5842020-11-02 14:36:56 +0100936};
937
Hans de Goede3ed224e2019-06-05 14:44:08 +0200938static void logi_hidpp_dev_conn_notif_equad(struct hid_device *hdev,
939 struct hidpp_event *hidpp_report,
Hans de Goede74808f92019-04-20 13:21:54 +0200940 struct dj_workitem *workitem)
941{
Hans de Goede3ed224e2019-06-05 14:44:08 +0200942 struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
Hans de Goedeee5e5842020-11-02 14:36:56 +0100943 int i, id;
Hans de Goede3ed224e2019-06-05 14:44:08 +0200944
Hans de Goede74808f92019-04-20 13:21:54 +0200945 workitem->type = WORKITEM_TYPE_PAIRED;
Hans de Goedede76b1d2019-04-20 13:22:00 +0200946 workitem->device_type = hidpp_report->params[HIDPP_PARAM_DEVICE_INFO] &
947 HIDPP_DEVICE_TYPE_MASK;
Hans de Goede74808f92019-04-20 13:21:54 +0200948 workitem->quad_id_msb = hidpp_report->params[HIDPP_PARAM_EQUAD_MSB];
949 workitem->quad_id_lsb = hidpp_report->params[HIDPP_PARAM_EQUAD_LSB];
Hans de Goedede76b1d2019-04-20 13:22:00 +0200950 switch (workitem->device_type) {
Hans de Goede74808f92019-04-20 13:21:54 +0200951 case REPORT_TYPE_KEYBOARD:
952 workitem->reports_supported |= STD_KEYBOARD | MULTIMEDIA |
Hans de Goede6d3c3f02019-04-20 13:22:02 +0200953 POWER_KEYS | MEDIA_CENTER |
954 HIDPP;
Hans de Goedeee5e5842020-11-02 14:36:56 +0100955 id = (workitem->quad_id_msb << 8) | workitem->quad_id_lsb;
956 for (i = 0; i < ARRAY_SIZE(kbd_builtin_touchpad_ids); i++) {
957 if (id == kbd_builtin_touchpad_ids[i]) {
Hans de Goede434f7702021-02-04 21:56:17 +0100958 if (djrcv_dev->type == recvr_type_dinovo)
959 workitem->reports_supported |= KBD_MOUSE;
960 else
961 workitem->reports_supported |= STD_MOUSE;
Hans de Goedeee5e5842020-11-02 14:36:56 +0100962 break;
963 }
964 }
Hans de Goede74808f92019-04-20 13:21:54 +0200965 break;
966 case REPORT_TYPE_MOUSE:
Hans de Goede6d3c3f02019-04-20 13:22:02 +0200967 workitem->reports_supported |= STD_MOUSE | HIDPP;
Hans de Goede3ed224e2019-06-05 14:44:08 +0200968 if (djrcv_dev->type == recvr_type_mouse_only)
969 workitem->reports_supported |= MULTIMEDIA;
Hans de Goede74808f92019-04-20 13:21:54 +0200970 break;
971 }
972}
973
Hans de Goedec9121cf2019-04-20 13:21:56 +0200974static void logi_hidpp_dev_conn_notif_27mhz(struct hid_device *hdev,
975 struct hidpp_event *hidpp_report,
976 struct dj_workitem *workitem)
977{
978 workitem->type = WORKITEM_TYPE_PAIRED;
979 workitem->quad_id_lsb = hidpp_report->params[HIDPP_PARAM_27MHZ_DEVID];
980 switch (hidpp_report->device_index) {
981 case 1: /* Index 1 is always a mouse */
982 case 2: /* Index 2 is always a mouse */
Hans de Goedede76b1d2019-04-20 13:22:00 +0200983 workitem->device_type = HIDPP_DEVICE_TYPE_MOUSE;
Hans de Goede6d3c3f02019-04-20 13:22:02 +0200984 workitem->reports_supported |= STD_MOUSE | HIDPP;
Hans de Goedec9121cf2019-04-20 13:21:56 +0200985 break;
986 case 3: /* Index 3 is always the keyboard */
987 case 4: /* Index 4 is used for an optional separate numpad */
Hans de Goedede76b1d2019-04-20 13:22:00 +0200988 workitem->device_type = HIDPP_DEVICE_TYPE_KEYBOARD;
Hans de Goedec9121cf2019-04-20 13:21:56 +0200989 workitem->reports_supported |= STD_KEYBOARD | MULTIMEDIA |
Hans de Goede6d3c3f02019-04-20 13:22:02 +0200990 POWER_KEYS | HIDPP;
Hans de Goedec9121cf2019-04-20 13:21:56 +0200991 break;
992 default:
993 hid_warn(hdev, "%s: unexpected device-index %d", __func__,
994 hidpp_report->device_index);
995 }
996}
997
Hans de Goede74808f92019-04-20 13:21:54 +0200998static void logi_hidpp_recv_queue_notif(struct hid_device *hdev,
999 struct hidpp_event *hidpp_report)
1000{
1001 /* We are called from atomic context (tasklet && djrcv->lock held) */
1002 struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
1003 const char *device_type = "UNKNOWN";
1004 struct dj_workitem workitem = {
1005 .type = WORKITEM_TYPE_EMPTY,
1006 .device_index = hidpp_report->device_index,
1007 };
1008
1009 switch (hidpp_report->params[HIDPP_PARAM_PROTO_TYPE]) {
1010 case 0x01:
1011 device_type = "Bluetooth";
Hans de Goedef2113c32019-04-20 13:22:03 +02001012 /* Bluetooth connect packet contents is the same as (e)QUAD */
Hans de Goede3ed224e2019-06-05 14:44:08 +02001013 logi_hidpp_dev_conn_notif_equad(hdev, hidpp_report, &workitem);
Hans de Goedef2113c32019-04-20 13:22:03 +02001014 if (!(hidpp_report->params[HIDPP_PARAM_DEVICE_INFO] &
1015 HIDPP_MANUFACTURER_MASK)) {
1016 hid_info(hdev, "Non Logitech device connected on slot %d\n",
1017 hidpp_report->device_index);
1018 workitem.reports_supported &= ~HIDPP;
1019 }
Hans de Goede74808f92019-04-20 13:21:54 +02001020 break;
1021 case 0x02:
1022 device_type = "27 Mhz";
Hans de Goedec9121cf2019-04-20 13:21:56 +02001023 logi_hidpp_dev_conn_notif_27mhz(hdev, hidpp_report, &workitem);
Hans de Goede74808f92019-04-20 13:21:54 +02001024 break;
1025 case 0x03:
1026 device_type = "QUAD or eQUAD";
Hans de Goede3ed224e2019-06-05 14:44:08 +02001027 logi_hidpp_dev_conn_notif_equad(hdev, hidpp_report, &workitem);
Hans de Goede74808f92019-04-20 13:21:54 +02001028 break;
1029 case 0x04:
1030 device_type = "eQUAD step 4 DJ";
Hans de Goede3ed224e2019-06-05 14:44:08 +02001031 logi_hidpp_dev_conn_notif_equad(hdev, hidpp_report, &workitem);
Hans de Goede74808f92019-04-20 13:21:54 +02001032 break;
1033 case 0x05:
1034 device_type = "DFU Lite";
1035 break;
1036 case 0x06:
1037 device_type = "eQUAD step 4 Lite";
Hans de Goede3ed224e2019-06-05 14:44:08 +02001038 logi_hidpp_dev_conn_notif_equad(hdev, hidpp_report, &workitem);
Hans de Goede74808f92019-04-20 13:21:54 +02001039 break;
1040 case 0x07:
1041 device_type = "eQUAD step 4 Gaming";
Benjamin Tissoires992ff2c2019-08-12 18:08:04 +02001042 logi_hidpp_dev_conn_notif_equad(hdev, hidpp_report, &workitem);
Filipe Laínsef07c112021-02-05 14:34:44 +00001043 workitem.reports_supported |= STD_KEYBOARD;
Hans de Goede74808f92019-04-20 13:21:54 +02001044 break;
1045 case 0x08:
1046 device_type = "eQUAD step 4 for gamepads";
1047 break;
1048 case 0x0a:
1049 device_type = "eQUAD nano Lite";
Hans de Goede3ed224e2019-06-05 14:44:08 +02001050 logi_hidpp_dev_conn_notif_equad(hdev, hidpp_report, &workitem);
Hans de Goede74808f92019-04-20 13:21:54 +02001051 break;
1052 case 0x0c:
Filipe Laíns5722f332019-07-30 13:24:57 +01001053 device_type = "eQUAD Lightspeed 1";
1054 logi_hidpp_dev_conn_notif_equad(hdev, hidpp_report, &workitem);
1055 workitem.reports_supported |= STD_KEYBOARD;
1056 break;
1057 case 0x0d:
Filipe Laínsfab3a952021-01-23 18:02:20 +00001058 device_type = "eQUAD Lightspeed 1.1";
1059 logi_hidpp_dev_conn_notif_equad(hdev, hidpp_report, &workitem);
1060 workitem.reports_supported |= STD_KEYBOARD;
1061 break;
1062 case 0x0f:
1063 device_type = "eQUAD Lightspeed 1.2";
Hans de Goede3ed224e2019-06-05 14:44:08 +02001064 logi_hidpp_dev_conn_notif_equad(hdev, hidpp_report, &workitem);
Benjamin Tissoiresf5fb57a2019-04-20 13:21:55 +02001065 workitem.reports_supported |= STD_KEYBOARD;
Hans de Goede74808f92019-04-20 13:21:54 +02001066 break;
1067 }
1068
Filipe Laíns8c9d7342020-01-15 20:18:11 +00001069 /* custom receiver device (eg. powerplay) */
1070 if (hidpp_report->device_index == 7) {
1071 workitem.reports_supported |= HIDPP;
1072 }
1073
Hans de Goede74808f92019-04-20 13:21:54 +02001074 if (workitem.type == WORKITEM_TYPE_EMPTY) {
1075 hid_warn(hdev,
1076 "unusable device of type %s (0x%02x) connected on slot %d",
1077 device_type,
1078 hidpp_report->params[HIDPP_PARAM_PROTO_TYPE],
1079 hidpp_report->device_index);
1080 return;
1081 }
1082
1083 hid_info(hdev, "device of type %s (0x%02x) connected on slot %d",
1084 device_type, hidpp_report->params[HIDPP_PARAM_PROTO_TYPE],
1085 hidpp_report->device_index);
1086
Hans de Goede74808f92019-04-20 13:21:54 +02001087 kfifo_in(&djrcv_dev->notif_fifo, &workitem, sizeof(workitem));
Hans de Goedee316aa62019-04-20 13:22:01 +02001088 schedule_work(&djrcv_dev->work);
Hans de Goede74808f92019-04-20 13:21:54 +02001089}
1090
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001091static void logi_dj_recv_forward_null_report(struct dj_receiver_dev *djrcv_dev,
1092 struct dj_report *dj_report)
1093{
1094 /* We are called from atomic context (tasklet && djrcv->lock held) */
1095 unsigned int i;
1096 u8 reportbuffer[MAX_REPORT_SIZE];
1097 struct dj_device *djdev;
1098
1099 djdev = djrcv_dev->paired_dj_devices[dj_report->device_index];
1100
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001101 memset(reportbuffer, 0, sizeof(reportbuffer));
1102
1103 for (i = 0; i < NUMBER_OF_HID_REPORTS; i++) {
1104 if (djdev->reports_supported & (1 << i)) {
1105 reportbuffer[0] = i;
1106 if (hid_input_report(djdev->hdev,
1107 HID_INPUT_REPORT,
1108 reportbuffer,
1109 hid_reportid_size_map[i], 1)) {
1110 dbg_hid("hid_input_report error sending null "
1111 "report\n");
1112 }
1113 }
1114 }
1115}
1116
Benjamin Tissoires83898232019-04-20 13:21:43 +02001117static void logi_dj_recv_forward_dj(struct dj_receiver_dev *djrcv_dev,
1118 struct dj_report *dj_report)
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001119{
1120 /* We are called from atomic context (tasklet && djrcv->lock held) */
1121 struct dj_device *dj_device;
1122
1123 dj_device = djrcv_dev->paired_dj_devices[dj_report->device_index];
1124
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001125 if ((dj_report->report_type > ARRAY_SIZE(hid_reportid_size_map) - 1) ||
1126 (hid_reportid_size_map[dj_report->report_type] == 0)) {
1127 dbg_hid("invalid report type:%x\n", dj_report->report_type);
1128 return;
1129 }
1130
1131 if (hid_input_report(dj_device->hdev,
1132 HID_INPUT_REPORT, &dj_report->report_type,
1133 hid_reportid_size_map[dj_report->report_type], 1)) {
1134 dbg_hid("hid_input_report error\n");
1135 }
1136}
1137
Benjamin Tissoires83898232019-04-20 13:21:43 +02001138static void logi_dj_recv_forward_report(struct dj_device *dj_dev, u8 *data,
1139 int size)
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001140{
1141 /* We are called from atomic context (tasklet && djrcv->lock held) */
1142 if (hid_input_report(dj_dev->hdev, HID_INPUT_REPORT, data, size, 1))
1143 dbg_hid("hid_input_report error\n");
1144}
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001145
Hans de Goede74808f92019-04-20 13:21:54 +02001146static void logi_dj_recv_forward_input_report(struct hid_device *hdev,
1147 u8 *data, int size)
1148{
1149 struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
1150 struct dj_device *dj_dev;
1151 unsigned long flags;
1152 u8 report = data[0];
1153 int i;
1154
1155 if (report > REPORT_TYPE_RFREPORT_LAST) {
Colin Ian King640d4ea2019-04-26 14:16:31 +01001156 hid_err(hdev, "Unexpected input report number %d\n", report);
Hans de Goede74808f92019-04-20 13:21:54 +02001157 return;
1158 }
1159
1160 spin_lock_irqsave(&djrcv_dev->lock, flags);
1161 for (i = 0; i < (DJ_MAX_PAIRED_DEVICES + DJ_DEVICE_INDEX_MIN); i++) {
1162 dj_dev = djrcv_dev->paired_dj_devices[i];
1163 if (dj_dev && (dj_dev->reports_supported & BIT(report))) {
1164 logi_dj_recv_forward_report(dj_dev, data, size);
1165 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
1166 return;
1167 }
1168 }
1169
1170 logi_dj_recv_queue_unknown_work(djrcv_dev);
1171 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
1172
1173 dbg_hid("No dj-devs handling input report number %d\n", report);
1174}
1175
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001176static int logi_dj_recv_send_report(struct dj_receiver_dev *djrcv_dev,
1177 struct dj_report *dj_report)
1178{
Hans de Goede0ee75542019-04-20 13:21:51 +02001179 struct hid_device *hdev = djrcv_dev->hidpp;
Benjamin Tissoiresdcd90062013-03-05 17:09:00 +01001180 struct hid_report *report;
1181 struct hid_report_enum *output_report_enum;
1182 u8 *data = (u8 *)(&dj_report->device_index);
Kees Cook297502a2013-09-11 21:56:56 +02001183 unsigned int i;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001184
Benjamin Tissoiresdcd90062013-03-05 17:09:00 +01001185 output_report_enum = &hdev->report_enum[HID_OUTPUT_REPORT];
1186 report = output_report_enum->report_id_hash[REPORT_ID_DJ_SHORT];
1187
1188 if (!report) {
Hans de Goedeaca22a32019-04-20 13:21:58 +02001189 hid_err(hdev, "%s: unable to find dj report\n", __func__);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001190 return -ENODEV;
1191 }
1192
Kees Cook297502a2013-09-11 21:56:56 +02001193 for (i = 0; i < DJREPORT_SHORT_LENGTH - 1; i++)
Benjamin Tissoiresdcd90062013-03-05 17:09:00 +01001194 report->field[0]->value[i] = data[i];
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001195
Jiri Kosina83a44ac2013-03-09 10:58:13 +01001196 hid_hw_request(hdev, report, HID_REQ_SET_REPORT);
Benjamin Tissoiresdcd90062013-03-05 17:09:00 +01001197
1198 return 0;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001199}
1200
Hans de Goede74808f92019-04-20 13:21:54 +02001201static int logi_dj_recv_query_hidpp_devices(struct dj_receiver_dev *djrcv_dev)
1202{
Colin Ian King39d21e72019-05-10 14:10:39 +01001203 static const u8 template[] = {
1204 REPORT_ID_HIDPP_SHORT,
1205 HIDPP_RECEIVER_INDEX,
1206 HIDPP_SET_REGISTER,
1207 HIDPP_REG_CONNECTION_STATE,
1208 HIDPP_FAKE_DEVICE_ARRIVAL,
1209 0x00, 0x00
1210 };
Hans de Goede74808f92019-04-20 13:21:54 +02001211 u8 *hidpp_report;
1212 int retval;
1213
1214 hidpp_report = kmemdup(template, sizeof(template), GFP_KERNEL);
1215 if (!hidpp_report)
1216 return -ENOMEM;
1217
1218 retval = hid_hw_raw_request(djrcv_dev->hidpp,
1219 REPORT_ID_HIDPP_SHORT,
1220 hidpp_report, sizeof(template),
1221 HID_OUTPUT_REPORT,
1222 HID_REQ_SET_REPORT);
1223
1224 kfree(hidpp_report);
Hans de Goede6fb08f12019-07-29 17:50:36 +02001225 return (retval < 0) ? retval : 0;
Hans de Goede74808f92019-04-20 13:21:54 +02001226}
1227
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001228static int logi_dj_recv_query_paired_devices(struct dj_receiver_dev *djrcv_dev)
1229{
Marc Dionned8dc3492012-06-01 18:12:14 -04001230 struct dj_report *dj_report;
1231 int retval;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001232
Hans de Goedeb6aeedd2019-04-20 13:21:53 +02001233 djrcv_dev->last_query = jiffies;
1234
Hans de Goede74808f92019-04-20 13:21:54 +02001235 if (djrcv_dev->type != recvr_type_dj)
1236 return logi_dj_recv_query_hidpp_devices(djrcv_dev);
1237
Alan Cox8a55ade2012-09-04 15:10:08 +01001238 dj_report = kzalloc(sizeof(struct dj_report), GFP_KERNEL);
Marc Dionned8dc3492012-06-01 18:12:14 -04001239 if (!dj_report)
1240 return -ENOMEM;
1241 dj_report->report_id = REPORT_ID_DJ_SHORT;
Mazin Rezk7f02bb62020-07-05 02:48:17 +00001242 dj_report->device_index = HIDPP_RECEIVER_INDEX;
Marc Dionned8dc3492012-06-01 18:12:14 -04001243 dj_report->report_type = REPORT_TYPE_CMD_GET_PAIRED_DEVICES;
1244 retval = logi_dj_recv_send_report(djrcv_dev, dj_report);
1245 kfree(dj_report);
1246 return retval;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001247}
1248
Nestor Lopez Casadoc63e0e32013-07-18 06:21:30 -07001249
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001250static int logi_dj_recv_switch_to_dj_mode(struct dj_receiver_dev *djrcv_dev,
1251 unsigned timeout)
1252{
Hans de Goede0ee75542019-04-20 13:21:51 +02001253 struct hid_device *hdev = djrcv_dev->hidpp;
Marc Dionned8dc3492012-06-01 18:12:14 -04001254 struct dj_report *dj_report;
Benjamin Tissoires6a9ddc82014-09-30 13:18:31 -04001255 u8 *buf;
Hans de Goede74808f92019-04-20 13:21:54 +02001256 int retval = 0;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001257
Alan Cox8a55ade2012-09-04 15:10:08 +01001258 dj_report = kzalloc(sizeof(struct dj_report), GFP_KERNEL);
Marc Dionned8dc3492012-06-01 18:12:14 -04001259 if (!dj_report)
1260 return -ENOMEM;
Benjamin Tisssoires42c22db2014-01-08 17:18:45 -05001261
Hans de Goede74808f92019-04-20 13:21:54 +02001262 if (djrcv_dev->type == recvr_type_dj) {
1263 dj_report->report_id = REPORT_ID_DJ_SHORT;
Mazin Rezk7f02bb62020-07-05 02:48:17 +00001264 dj_report->device_index = HIDPP_RECEIVER_INDEX;
Hans de Goede74808f92019-04-20 13:21:54 +02001265 dj_report->report_type = REPORT_TYPE_CMD_SWITCH;
1266 dj_report->report_params[CMD_SWITCH_PARAM_DEVBITFIELD] = 0x3F;
1267 dj_report->report_params[CMD_SWITCH_PARAM_TIMEOUT_SECONDS] =
1268 (u8)timeout;
1269
1270 retval = logi_dj_recv_send_report(djrcv_dev, dj_report);
1271
1272 /*
1273 * Ugly sleep to work around a USB 3.0 bug when the receiver is
1274 * still processing the "switch-to-dj" command while we send an
1275 * other command.
1276 * 50 msec should gives enough time to the receiver to be ready.
1277 */
1278 msleep(50);
1279 }
Benjamin Tisssoires42c22db2014-01-08 17:18:45 -05001280
Benjamin Tissoires6a9ddc82014-09-30 13:18:31 -04001281 /*
1282 * Magical bits to set up hidpp notifications when the dj devices
1283 * are connected/disconnected.
1284 *
1285 * We can reuse dj_report because HIDPP_REPORT_SHORT_LENGTH is smaller
1286 * than DJREPORT_SHORT_LENGTH.
1287 */
1288 buf = (u8 *)dj_report;
1289
1290 memset(buf, 0, HIDPP_REPORT_SHORT_LENGTH);
1291
1292 buf[0] = REPORT_ID_HIDPP_SHORT;
Mazin Rezk7f02bb62020-07-05 02:48:17 +00001293 buf[1] = HIDPP_RECEIVER_INDEX;
Benjamin Tissoires6a9ddc82014-09-30 13:18:31 -04001294 buf[2] = 0x80;
1295 buf[3] = 0x00;
1296 buf[4] = 0x00;
1297 buf[5] = 0x09;
1298 buf[6] = 0x00;
1299
1300 hid_hw_raw_request(hdev, REPORT_ID_HIDPP_SHORT, buf,
1301 HIDPP_REPORT_SHORT_LENGTH, HID_OUTPUT_REPORT,
1302 HID_REQ_SET_REPORT);
1303
1304 kfree(dj_report);
Marc Dionned8dc3492012-06-01 18:12:14 -04001305 return retval;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001306}
1307
1308
1309static int logi_dj_ll_open(struct hid_device *hid)
1310{
Hans de Goedeaca22a32019-04-20 13:21:58 +02001311 dbg_hid("%s: %s\n", __func__, hid->phys);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001312 return 0;
1313
1314}
1315
1316static void logi_dj_ll_close(struct hid_device *hid)
1317{
Hans de Goedeaca22a32019-04-20 13:21:58 +02001318 dbg_hid("%s: %s\n", __func__, hid->phys);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001319}
1320
Benjamin Tissoires8dba3022017-03-27 16:59:21 +02001321/*
1322 * Register 0xB5 is "pairing information". It is solely intended for the
1323 * receiver, so do not overwrite the device index.
1324 */
Benjamin Tissoiresc0340412019-04-20 13:21:46 +02001325static u8 unifying_pairing_query[] = { REPORT_ID_HIDPP_SHORT,
1326 HIDPP_RECEIVER_INDEX,
1327 HIDPP_GET_LONG_REGISTER,
1328 HIDPP_REG_PAIRING_INFORMATION };
1329static u8 unifying_pairing_answer[] = { REPORT_ID_HIDPP_LONG,
1330 HIDPP_RECEIVER_INDEX,
1331 HIDPP_GET_LONG_REGISTER,
1332 HIDPP_REG_PAIRING_INFORMATION };
Benjamin Tissoires33797822014-09-30 13:18:30 -04001333
Benjamin Tissoiresbd27e202014-02-10 12:58:53 -05001334static int logi_dj_ll_raw_request(struct hid_device *hid,
1335 unsigned char reportnum, __u8 *buf,
1336 size_t count, unsigned char report_type,
1337 int reqtype)
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001338{
Benjamin Tissoires0e40d352014-02-05 16:33:17 -05001339 struct dj_device *djdev = hid->driver_data;
1340 struct dj_receiver_dev *djrcv_dev = djdev->dj_receiver_dev;
1341 u8 *out_buf;
1342 int ret;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001343
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001344 if ((buf[0] == REPORT_ID_HIDPP_SHORT) ||
Hans de Goede3a9a2c82019-06-14 10:20:27 +02001345 (buf[0] == REPORT_ID_HIDPP_LONG) ||
1346 (buf[0] == REPORT_ID_HIDPP_VERY_LONG)) {
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001347 if (count < 2)
1348 return -EINVAL;
1349
Benjamin Tissoires33797822014-09-30 13:18:30 -04001350 /* special case where we should not overwrite
1351 * the device_index */
Benjamin Tissoires8dba3022017-03-27 16:59:21 +02001352 if (count == 7 && !memcmp(buf, unifying_pairing_query,
1353 sizeof(unifying_pairing_query)))
1354 buf[4] = (buf[4] & 0xf0) | (djdev->device_index - 1);
Benjamin Tissoires33797822014-09-30 13:18:30 -04001355 else
1356 buf[1] = djdev->device_index;
Hans de Goede0ee75542019-04-20 13:21:51 +02001357 return hid_hw_raw_request(djrcv_dev->hidpp, reportnum, buf,
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001358 count, report_type, reqtype);
1359 }
1360
Benjamin Tissoires0e40d352014-02-05 16:33:17 -05001361 if (buf[0] != REPORT_TYPE_LEDS)
1362 return -EINVAL;
1363
Hans de Goede74808f92019-04-20 13:21:54 +02001364 if (djrcv_dev->type != recvr_type_dj && count >= 2) {
1365 if (!djrcv_dev->keyboard) {
1366 hid_warn(hid, "Received REPORT_TYPE_LEDS request before the keyboard interface was enumerated\n");
1367 return 0;
1368 }
1369 /* usbhid overrides the report ID and ignores the first byte */
1370 return hid_hw_raw_request(djrcv_dev->keyboard, 0, buf, count,
1371 report_type, reqtype);
1372 }
1373
Benjamin Tissoires0e40d352014-02-05 16:33:17 -05001374 out_buf = kzalloc(DJREPORT_SHORT_LENGTH, GFP_ATOMIC);
1375 if (!out_buf)
1376 return -ENOMEM;
1377
Jiri Kosina51217e62014-08-21 09:56:47 -05001378 if (count > DJREPORT_SHORT_LENGTH - 2)
Benjamin Tissoires0e40d352014-02-05 16:33:17 -05001379 count = DJREPORT_SHORT_LENGTH - 2;
1380
1381 out_buf[0] = REPORT_ID_DJ_SHORT;
1382 out_buf[1] = djdev->device_index;
1383 memcpy(out_buf + 2, buf, count);
1384
Hans de Goede0ee75542019-04-20 13:21:51 +02001385 ret = hid_hw_raw_request(djrcv_dev->hidpp, out_buf[0], out_buf,
Benjamin Tissoiresbd27e202014-02-10 12:58:53 -05001386 DJREPORT_SHORT_LENGTH, report_type, reqtype);
Benjamin Tissoires0e40d352014-02-05 16:33:17 -05001387
1388 kfree(out_buf);
1389 return ret;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001390}
1391
Dan Carpenter6d603322013-10-19 12:08:59 +03001392static void rdcat(char *rdesc, unsigned int *rsize, const char *data, unsigned int size)
Henrik Rydberg2a039bf2012-04-22 14:21:39 +02001393{
Dan Carpenter6d603322013-10-19 12:08:59 +03001394 memcpy(rdesc + *rsize, data, size);
Henrik Rydberg2a039bf2012-04-22 14:21:39 +02001395 *rsize += size;
1396}
1397
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001398static int logi_dj_ll_parse(struct hid_device *hid)
1399{
1400 struct dj_device *djdev = hid->driver_data;
Henrik Rydberg2a039bf2012-04-22 14:21:39 +02001401 unsigned int rsize = 0;
1402 char *rdesc;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001403 int retval;
1404
1405 dbg_hid("%s\n", __func__);
1406
1407 djdev->hdev->version = 0x0111;
1408 djdev->hdev->country = 0x00;
1409
Henrik Rydberg2a039bf2012-04-22 14:21:39 +02001410 rdesc = kmalloc(MAX_RDESC_SIZE, GFP_KERNEL);
1411 if (!rdesc)
1412 return -ENOMEM;
1413
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001414 if (djdev->reports_supported & STD_KEYBOARD) {
Hans de Goede6d3c3f02019-04-20 13:22:02 +02001415 dbg_hid("%s: sending a kbd descriptor, reports_supported: %llx\n",
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001416 __func__, djdev->reports_supported);
Dan Carpenter6d603322013-10-19 12:08:59 +03001417 rdcat(rdesc, &rsize, kbd_descriptor, sizeof(kbd_descriptor));
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001418 }
1419
1420 if (djdev->reports_supported & STD_MOUSE) {
Hans de Goede6d3c3f02019-04-20 13:22:02 +02001421 dbg_hid("%s: sending a mouse descriptor, reports_supported: %llx\n",
1422 __func__, djdev->reports_supported);
Hans de Goede3ed224e2019-06-05 14:44:08 +02001423 if (djdev->dj_receiver_dev->type == recvr_type_gaming_hidpp ||
1424 djdev->dj_receiver_dev->type == recvr_type_mouse_only)
Benjamin Tissoiresf5fb57a2019-04-20 13:21:55 +02001425 rdcat(rdesc, &rsize, mse_high_res_descriptor,
1426 sizeof(mse_high_res_descriptor));
Hans de Goedec9121cf2019-04-20 13:21:56 +02001427 else if (djdev->dj_receiver_dev->type == recvr_type_27mhz)
1428 rdcat(rdesc, &rsize, mse_27mhz_descriptor,
1429 sizeof(mse_27mhz_descriptor));
Hans de Goede434f7702021-02-04 21:56:17 +01001430 else if (recvr_type_is_bluetooth(djdev->dj_receiver_dev->type))
Hans de Goedef2113c32019-04-20 13:22:03 +02001431 rdcat(rdesc, &rsize, mse_bluetooth_descriptor,
1432 sizeof(mse_bluetooth_descriptor));
Benjamin Tissoiresf5fb57a2019-04-20 13:21:55 +02001433 else
1434 rdcat(rdesc, &rsize, mse_descriptor,
1435 sizeof(mse_descriptor));
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001436 }
1437
Hans de Goede434f7702021-02-04 21:56:17 +01001438 if (djdev->reports_supported & KBD_MOUSE) {
1439 dbg_hid("%s: sending a kbd-mouse descriptor, reports_supported: %llx\n",
1440 __func__, djdev->reports_supported);
1441 rdcat(rdesc, &rsize, mse5_bluetooth_descriptor,
1442 sizeof(mse5_bluetooth_descriptor));
1443 }
1444
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001445 if (djdev->reports_supported & MULTIMEDIA) {
Hans de Goede6d3c3f02019-04-20 13:22:02 +02001446 dbg_hid("%s: sending a multimedia report descriptor: %llx\n",
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001447 __func__, djdev->reports_supported);
Dan Carpenter6d603322013-10-19 12:08:59 +03001448 rdcat(rdesc, &rsize, consumer_descriptor, sizeof(consumer_descriptor));
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001449 }
1450
1451 if (djdev->reports_supported & POWER_KEYS) {
Hans de Goede6d3c3f02019-04-20 13:22:02 +02001452 dbg_hid("%s: sending a power keys report descriptor: %llx\n",
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001453 __func__, djdev->reports_supported);
Dan Carpenter6d603322013-10-19 12:08:59 +03001454 rdcat(rdesc, &rsize, syscontrol_descriptor, sizeof(syscontrol_descriptor));
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001455 }
1456
1457 if (djdev->reports_supported & MEDIA_CENTER) {
Hans de Goede6d3c3f02019-04-20 13:22:02 +02001458 dbg_hid("%s: sending a media center report descriptor: %llx\n",
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001459 __func__, djdev->reports_supported);
Dan Carpenter6d603322013-10-19 12:08:59 +03001460 rdcat(rdesc, &rsize, media_descriptor, sizeof(media_descriptor));
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001461 }
1462
1463 if (djdev->reports_supported & KBD_LEDS) {
Hans de Goede6d3c3f02019-04-20 13:22:02 +02001464 dbg_hid("%s: need to send kbd leds report descriptor: %llx\n",
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001465 __func__, djdev->reports_supported);
1466 }
1467
Hans de Goede6d3c3f02019-04-20 13:22:02 +02001468 if (djdev->reports_supported & HIDPP) {
Filipe Laíns630dd6e2020-01-13 19:23:00 +00001469 dbg_hid("%s: sending a HID++ descriptor, reports_supported: %llx\n",
1470 __func__, djdev->reports_supported);
Hans de Goede6d3c3f02019-04-20 13:22:02 +02001471 rdcat(rdesc, &rsize, hidpp_descriptor,
1472 sizeof(hidpp_descriptor));
1473 }
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001474
Henrik Rydberg2a039bf2012-04-22 14:21:39 +02001475 retval = hid_parse_report(hid, rdesc, rsize);
1476 kfree(rdesc);
1477
1478 return retval;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001479}
1480
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001481static int logi_dj_ll_start(struct hid_device *hid)
1482{
1483 dbg_hid("%s\n", __func__);
1484 return 0;
1485}
1486
1487static void logi_dj_ll_stop(struct hid_device *hid)
1488{
1489 dbg_hid("%s\n", __func__);
1490}
1491
1492
1493static struct hid_ll_driver logi_dj_ll_driver = {
1494 .parse = logi_dj_ll_parse,
1495 .start = logi_dj_ll_start,
1496 .stop = logi_dj_ll_stop,
1497 .open = logi_dj_ll_open,
1498 .close = logi_dj_ll_close,
Benjamin Tissoiresbd27e202014-02-10 12:58:53 -05001499 .raw_request = logi_dj_ll_raw_request,
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001500};
1501
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001502static int logi_dj_dj_event(struct hid_device *hdev,
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001503 struct hid_report *report, u8 *data,
1504 int size)
1505{
1506 struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
1507 struct dj_report *dj_report = (struct dj_report *) data;
1508 unsigned long flags;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001509
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001510 /*
1511 * Here we receive all data coming from iface 2, there are 3 cases:
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001512 *
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001513 * 1) Data is intended for this driver i. e. data contains arrival,
1514 * departure, etc notifications, in which case we queue them for delayed
1515 * processing by the work queue. We return 1 to hid-core as no further
1516 * processing is required from it.
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001517 *
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001518 * 2) Data informs a connection change, if the change means rf link
1519 * loss, then we must send a null report to the upper layer to discard
1520 * potentially pressed keys that may be repeated forever by the input
1521 * layer. Return 1 to hid-core as no further processing is required.
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001522 *
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001523 * 3) Data is an actual input event from a paired DJ device in which
1524 * case we forward it to the correct hid device (via hid_input_report()
1525 * ) and return 1 so hid-core does not anything else with it.
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001526 */
Benjamin Tissoires5abfe852014-08-22 16:16:05 -04001527
Jiri Kosinaad3e14d2014-08-21 09:57:17 -05001528 if ((dj_report->device_index < DJ_DEVICE_INDEX_MIN) ||
1529 (dj_report->device_index > DJ_DEVICE_INDEX_MAX)) {
Benjamin Tissoires5abfe852014-08-22 16:16:05 -04001530 /*
1531 * Device index is wrong, bail out.
1532 * This driver can ignore safely the receiver notifications,
1533 * so ignore those reports too.
1534 */
1535 if (dj_report->device_index != DJ_RECEIVER_INDEX)
Hans de Goedeaca22a32019-04-20 13:21:58 +02001536 hid_err(hdev, "%s: invalid device index:%d\n",
Jiri Kosinaad3e14d2014-08-21 09:57:17 -05001537 __func__, dj_report->device_index);
1538 return false;
1539 }
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001540
1541 spin_lock_irqsave(&djrcv_dev->lock, flags);
Benjamin Tissoires368d4e592014-08-22 16:16:06 -04001542
1543 if (!djrcv_dev->paired_dj_devices[dj_report->device_index]) {
1544 /* received an event for an unknown device, bail out */
1545 logi_dj_recv_queue_notification(djrcv_dev, dj_report);
1546 goto out;
1547 }
1548
Benjamin Tissoires5abfe852014-08-22 16:16:05 -04001549 switch (dj_report->report_type) {
1550 case REPORT_TYPE_NOTIF_DEVICE_PAIRED:
Benjamin Tissoires368d4e592014-08-22 16:16:06 -04001551 /* pairing notifications are handled above the switch */
1552 break;
Benjamin Tissoires5abfe852014-08-22 16:16:05 -04001553 case REPORT_TYPE_NOTIF_DEVICE_UNPAIRED:
1554 logi_dj_recv_queue_notification(djrcv_dev, dj_report);
1555 break;
1556 case REPORT_TYPE_NOTIF_CONNECTION_STATUS:
1557 if (dj_report->report_params[CONNECTION_STATUS_PARAM_STATUS] ==
1558 STATUS_LINKLOSS) {
1559 logi_dj_recv_forward_null_report(djrcv_dev, dj_report);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001560 }
Benjamin Tissoires5abfe852014-08-22 16:16:05 -04001561 break;
1562 default:
Benjamin Tissoires83898232019-04-20 13:21:43 +02001563 logi_dj_recv_forward_dj(djrcv_dev, dj_report);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001564 }
Benjamin Tissoires368d4e592014-08-22 16:16:06 -04001565
1566out:
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001567 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
1568
Benjamin Tissoires5abfe852014-08-22 16:16:05 -04001569 return true;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001570}
1571
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001572static int logi_dj_hidpp_event(struct hid_device *hdev,
1573 struct hid_report *report, u8 *data,
1574 int size)
1575{
1576 struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
Benjamin Tissoires7bb56a52019-04-20 13:21:44 +02001577 struct hidpp_event *hidpp_report = (struct hidpp_event *) data;
Hans de Goede74808f92019-04-20 13:21:54 +02001578 struct dj_device *dj_dev;
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001579 unsigned long flags;
Benjamin Tissoires7bb56a52019-04-20 13:21:44 +02001580 u8 device_index = hidpp_report->device_index;
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001581
Benjamin Tissoires33797822014-09-30 13:18:30 -04001582 if (device_index == HIDPP_RECEIVER_INDEX) {
1583 /* special case were the device wants to know its unifying
1584 * name */
1585 if (size == HIDPP_REPORT_LONG_LENGTH &&
Benjamin Tissoires8dba3022017-03-27 16:59:21 +02001586 !memcmp(data, unifying_pairing_answer,
1587 sizeof(unifying_pairing_answer)))
Benjamin Tissoires33797822014-09-30 13:18:30 -04001588 device_index = (data[4] & 0x0F) + 1;
1589 else
1590 return false;
1591 }
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001592
1593 /*
1594 * Data is from the HID++ collection, in this case, we forward the
1595 * data to the corresponding child dj device and return 0 to hid-core
1596 * so he data also goes to the hidraw device of the receiver. This
1597 * allows a user space application to implement the full HID++ routing
1598 * via the receiver.
1599 */
1600
1601 if ((device_index < DJ_DEVICE_INDEX_MIN) ||
1602 (device_index > DJ_DEVICE_INDEX_MAX)) {
1603 /*
1604 * Device index is wrong, bail out.
1605 * This driver can ignore safely the receiver notifications,
1606 * so ignore those reports too.
1607 */
Hans de Goedeaca22a32019-04-20 13:21:58 +02001608 hid_err(hdev, "%s: invalid device index:%d\n", __func__,
1609 hidpp_report->device_index);
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001610 return false;
1611 }
1612
1613 spin_lock_irqsave(&djrcv_dev->lock, flags);
1614
Hans de Goede74808f92019-04-20 13:21:54 +02001615 dj_dev = djrcv_dev->paired_dj_devices[device_index];
Hans de Goedec9121cf2019-04-20 13:21:56 +02001616
1617 /*
1618 * With 27 MHz receivers, we do not get an explicit unpair event,
1619 * remove the old device if the user has paired a *different* device.
1620 */
1621 if (djrcv_dev->type == recvr_type_27mhz && dj_dev &&
1622 hidpp_report->sub_id == REPORT_TYPE_NOTIF_DEVICE_CONNECTED &&
1623 hidpp_report->params[HIDPP_PARAM_PROTO_TYPE] == 0x02 &&
1624 hidpp_report->params[HIDPP_PARAM_27MHZ_DEVID] !=
1625 dj_dev->hdev->product) {
1626 struct dj_workitem workitem = {
1627 .device_index = hidpp_report->device_index,
1628 .type = WORKITEM_TYPE_UNPAIRED,
1629 };
1630 kfifo_in(&djrcv_dev->notif_fifo, &workitem, sizeof(workitem));
1631 /* logi_hidpp_recv_queue_notif will queue the work */
1632 dj_dev = NULL;
1633 }
1634
Hans de Goede74808f92019-04-20 13:21:54 +02001635 if (dj_dev) {
1636 logi_dj_recv_forward_report(dj_dev, data, size);
1637 } else {
1638 if (hidpp_report->sub_id == REPORT_TYPE_NOTIF_DEVICE_CONNECTED)
1639 logi_hidpp_recv_queue_notif(hdev, hidpp_report);
1640 else
1641 logi_dj_recv_queue_unknown_work(djrcv_dev);
1642 }
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001643
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001644 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
1645
1646 return false;
1647}
1648
1649static int logi_dj_raw_event(struct hid_device *hdev,
1650 struct hid_report *report, u8 *data,
1651 int size)
1652{
Hans de Goede74808f92019-04-20 13:21:54 +02001653 struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001654 dbg_hid("%s, size:%d\n", __func__, size);
1655
Hans de Goededa12b222019-04-20 13:21:59 +02001656 if (!djrcv_dev)
1657 return 0;
1658
Hans de Goede74808f92019-04-20 13:21:54 +02001659 if (!hdev->report_enum[HID_INPUT_REPORT].numbered) {
1660
1661 if (djrcv_dev->unnumbered_application == HID_GD_KEYBOARD) {
1662 /*
1663 * For the keyboard, we can reuse the same report by
1664 * using the second byte which is constant in the USB
1665 * HID report descriptor.
1666 */
1667 data[1] = data[0];
1668 data[0] = REPORT_TYPE_KEYBOARD;
1669
1670 logi_dj_recv_forward_input_report(hdev, data, size);
1671
1672 /* restore previous state */
1673 data[0] = data[1];
1674 data[1] = 0;
1675 }
Hans de Goede3ed224e2019-06-05 14:44:08 +02001676 /*
1677 * Mouse-only receivers send unnumbered mouse data. The 27 MHz
1678 * receiver uses 6 byte packets, the nano receiver 8 bytes.
1679 */
Hans de Goede1f944ac2019-04-20 13:21:57 +02001680 if (djrcv_dev->unnumbered_application == HID_GD_MOUSE &&
Hans de Goede3ed224e2019-06-05 14:44:08 +02001681 size <= 8) {
1682 u8 mouse_report[9];
Hans de Goede1f944ac2019-04-20 13:21:57 +02001683
1684 /* Prepend report id */
1685 mouse_report[0] = REPORT_TYPE_MOUSE;
Hans de Goede3ed224e2019-06-05 14:44:08 +02001686 memcpy(mouse_report + 1, data, size);
1687 logi_dj_recv_forward_input_report(hdev, mouse_report,
1688 size + 1);
Hans de Goede1f944ac2019-04-20 13:21:57 +02001689 }
Hans de Goede74808f92019-04-20 13:21:54 +02001690
1691 return false;
1692 }
1693
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001694 switch (data[0]) {
1695 case REPORT_ID_DJ_SHORT:
Peter Wuf254ae92014-12-16 16:55:21 +01001696 if (size != DJREPORT_SHORT_LENGTH) {
Hans de Goedeaca22a32019-04-20 13:21:58 +02001697 hid_err(hdev, "Short DJ report bad size (%d)", size);
Peter Wuf254ae92014-12-16 16:55:21 +01001698 return false;
1699 }
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001700 return logi_dj_dj_event(hdev, report, data, size);
Benjamin Tissoiresf5fb57a2019-04-20 13:21:55 +02001701 case REPORT_ID_DJ_LONG:
1702 if (size != DJREPORT_LONG_LENGTH) {
Hans de Goedeaca22a32019-04-20 13:21:58 +02001703 hid_err(hdev, "Long DJ report bad size (%d)", size);
Benjamin Tissoiresf5fb57a2019-04-20 13:21:55 +02001704 return false;
1705 }
1706 return logi_dj_dj_event(hdev, report, data, size);
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001707 case REPORT_ID_HIDPP_SHORT:
Peter Wuf254ae92014-12-16 16:55:21 +01001708 if (size != HIDPP_REPORT_SHORT_LENGTH) {
Hans de Goedeaca22a32019-04-20 13:21:58 +02001709 hid_err(hdev, "Short HID++ report bad size (%d)", size);
Peter Wuf254ae92014-12-16 16:55:21 +01001710 return false;
1711 }
1712 return logi_dj_hidpp_event(hdev, report, data, size);
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001713 case REPORT_ID_HIDPP_LONG:
Peter Wuf254ae92014-12-16 16:55:21 +01001714 if (size != HIDPP_REPORT_LONG_LENGTH) {
Hans de Goedeaca22a32019-04-20 13:21:58 +02001715 hid_err(hdev, "Long HID++ report bad size (%d)", size);
Peter Wuf254ae92014-12-16 16:55:21 +01001716 return false;
1717 }
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001718 return logi_dj_hidpp_event(hdev, report, data, size);
1719 }
1720
Hans de Goede74808f92019-04-20 13:21:54 +02001721 logi_dj_recv_forward_input_report(hdev, data, size);
1722
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001723 return false;
1724}
1725
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001726static int logi_dj_probe(struct hid_device *hdev,
1727 const struct hid_device_id *id)
1728{
Benjamin Tissoires82c0beb2019-04-20 13:21:47 +02001729 struct hid_report_enum *rep_enum;
1730 struct hid_report *rep;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001731 struct dj_receiver_dev *djrcv_dev;
Hans de Goededa12b222019-04-20 13:21:59 +02001732 struct usb_interface *intf;
1733 unsigned int no_dj_interfaces = 0;
Benjamin Tissoires82c0beb2019-04-20 13:21:47 +02001734 bool has_hidpp = false;
Hans de Goedea1d97cc2019-04-20 13:21:52 +02001735 unsigned long flags;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001736 int retval;
1737
Benjamin Tissoires82c0beb2019-04-20 13:21:47 +02001738 /*
1739 * Call to usbhid to fetch the HID descriptors of the current
1740 * interface subsequently call to the hid/hid-core to parse the
1741 * fetched descriptors.
1742 */
1743 retval = hid_parse(hdev);
1744 if (retval) {
Hans de Goedeaca22a32019-04-20 13:21:58 +02001745 hid_err(hdev, "%s: parse failed\n", __func__);
Benjamin Tissoires82c0beb2019-04-20 13:21:47 +02001746 return retval;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001747 }
1748
Hans de Goededa12b222019-04-20 13:21:59 +02001749 /*
1750 * Some KVMs add an extra interface for e.g. mouse emulation. If we
1751 * treat these as logitech-dj interfaces then this causes input events
1752 * reported through this extra interface to not be reported correctly.
1753 * To avoid this, we treat these as generic-hid devices.
1754 */
1755 switch (id->driver_data) {
1756 case recvr_type_dj: no_dj_interfaces = 3; break;
1757 case recvr_type_hidpp: no_dj_interfaces = 2; break;
1758 case recvr_type_gaming_hidpp: no_dj_interfaces = 3; break;
Hans de Goede3ed224e2019-06-05 14:44:08 +02001759 case recvr_type_mouse_only: no_dj_interfaces = 2; break;
Hans de Goededa12b222019-04-20 13:21:59 +02001760 case recvr_type_27mhz: no_dj_interfaces = 2; break;
Hans de Goedef2113c32019-04-20 13:22:03 +02001761 case recvr_type_bluetooth: no_dj_interfaces = 2; break;
Hans de Goede434f7702021-02-04 21:56:17 +01001762 case recvr_type_dinovo: no_dj_interfaces = 2; break;
Hans de Goededa12b222019-04-20 13:21:59 +02001763 }
1764 if (hid_is_using_ll_driver(hdev, &usb_hid_driver)) {
1765 intf = to_usb_interface(hdev->dev.parent);
1766 if (intf && intf->altsetting->desc.bInterfaceNumber >=
1767 no_dj_interfaces) {
1768 hdev->quirks |= HID_QUIRK_INPUT_PER_APP;
1769 return hid_hw_start(hdev, HID_CONNECT_DEFAULT);
1770 }
1771 }
1772
Benjamin Tissoires82c0beb2019-04-20 13:21:47 +02001773 rep_enum = &hdev->report_enum[HID_INPUT_REPORT];
1774
Hans de Goede74808f92019-04-20 13:21:54 +02001775 /* no input reports, bail out */
1776 if (list_empty(&rep_enum->report_list))
1777 return -ENODEV;
1778
Benjamin Tissoires82c0beb2019-04-20 13:21:47 +02001779 /*
1780 * Check for the HID++ application.
1781 * Note: we should theoretically check for HID++ and DJ
1782 * collections, but this will do.
1783 */
1784 list_for_each_entry(rep, &rep_enum->report_list, list) {
1785 if (rep->application == 0xff000001)
1786 has_hidpp = true;
1787 }
1788
1789 /*
1790 * Ignore interfaces without DJ/HID++ collection, they will not carry
1791 * any data, dont create any hid_device for them.
1792 */
Hans de Goede74808f92019-04-20 13:21:54 +02001793 if (!has_hidpp && id->driver_data == recvr_type_dj)
Benjamin Tissoires82c0beb2019-04-20 13:21:47 +02001794 return -ENODEV;
1795
Hans de Goedea1d97cc2019-04-20 13:21:52 +02001796 /* get the current application attached to the node */
1797 rep = list_first_entry(&rep_enum->report_list, struct hid_report, list);
Hans de Goede74808f92019-04-20 13:21:54 +02001798 djrcv_dev = dj_get_receiver_dev(hdev, id->driver_data,
Hans de Goedea1d97cc2019-04-20 13:21:52 +02001799 rep->application, has_hidpp);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001800 if (!djrcv_dev) {
Hans de Goedeaca22a32019-04-20 13:21:58 +02001801 hid_err(hdev, "%s: dj_get_receiver_dev failed\n", __func__);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001802 return -ENOMEM;
1803 }
Kees Cook297502a2013-09-11 21:56:56 +02001804
Hans de Goede74808f92019-04-20 13:21:54 +02001805 if (!rep_enum->numbered)
1806 djrcv_dev->unnumbered_application = rep->application;
1807
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001808 /* Starts the usb device and connects to upper interfaces hiddev and
1809 * hidraw */
Hans de Goede74808f92019-04-20 13:21:54 +02001810 retval = hid_hw_start(hdev, HID_CONNECT_HIDRAW|HID_CONNECT_HIDDEV);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001811 if (retval) {
Hans de Goedeaca22a32019-04-20 13:21:58 +02001812 hid_err(hdev, "%s: hid_hw_start returned error\n", __func__);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001813 goto hid_hw_start_fail;
1814 }
1815
Hans de Goede74808f92019-04-20 13:21:54 +02001816 if (has_hidpp) {
1817 retval = logi_dj_recv_switch_to_dj_mode(djrcv_dev, 0);
1818 if (retval < 0) {
1819 hid_err(hdev, "%s: logi_dj_recv_switch_to_dj_mode returned error:%d\n",
1820 __func__, retval);
1821 goto switch_to_dj_mode_fail;
1822 }
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001823 }
1824
1825 /* This is enabling the polling urb on the IN endpoint */
Benjamin Tissoiresddf75402013-07-12 11:01:02 +02001826 retval = hid_hw_open(hdev);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001827 if (retval < 0) {
Hans de Goedeaca22a32019-04-20 13:21:58 +02001828 hid_err(hdev, "%s: hid_hw_open returned error:%d\n",
Benjamin Tissoiresddf75402013-07-12 11:01:02 +02001829 __func__, retval);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001830 goto llopen_failed;
1831 }
1832
Andrew de los Reyesa9dd22b72013-02-18 09:20:22 -08001833 /* Allow incoming packets to arrive: */
1834 hid_device_io_start(hdev);
1835
Hans de Goede74808f92019-04-20 13:21:54 +02001836 if (has_hidpp) {
1837 spin_lock_irqsave(&djrcv_dev->lock, flags);
1838 djrcv_dev->ready = true;
1839 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
1840 retval = logi_dj_recv_query_paired_devices(djrcv_dev);
1841 if (retval < 0) {
1842 hid_err(hdev, "%s: logi_dj_recv_query_paired_devices error:%d\n",
1843 __func__, retval);
Hans de Goede8ccff282019-08-25 17:35:42 +02001844 /*
1845 * This can happen with a KVM, let the probe succeed,
1846 * logi_dj_recv_queue_unknown_work will retry later.
1847 */
Hans de Goede74808f92019-04-20 13:21:54 +02001848 }
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001849 }
1850
Hans de Goede8ccff282019-08-25 17:35:42 +02001851 return 0;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001852
1853llopen_failed:
1854switch_to_dj_mode_fail:
1855 hid_hw_stop(hdev);
1856
1857hid_hw_start_fail:
Hans de Goedea1d97cc2019-04-20 13:21:52 +02001858 dj_put_receiver_dev(hdev);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001859 return retval;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001860}
1861
1862#ifdef CONFIG_PM
1863static int logi_dj_reset_resume(struct hid_device *hdev)
1864{
1865 int retval;
1866 struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
1867
Hans de Goededa12b222019-04-20 13:21:59 +02001868 if (!djrcv_dev || djrcv_dev->hidpp != hdev)
Hans de Goede74808f92019-04-20 13:21:54 +02001869 return 0;
1870
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001871 retval = logi_dj_recv_switch_to_dj_mode(djrcv_dev, 0);
1872 if (retval < 0) {
Hans de Goedeaca22a32019-04-20 13:21:58 +02001873 hid_err(hdev, "%s: logi_dj_recv_switch_to_dj_mode returned error:%d\n",
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001874 __func__, retval);
1875 }
1876
1877 return 0;
1878}
1879#endif
1880
1881static void logi_dj_remove(struct hid_device *hdev)
1882{
1883 struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
1884 struct dj_device *dj_dev;
Hans de Goedea1d97cc2019-04-20 13:21:52 +02001885 unsigned long flags;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001886 int i;
1887
1888 dbg_hid("%s\n", __func__);
1889
Hans de Goededa12b222019-04-20 13:21:59 +02001890 if (!djrcv_dev)
1891 return hid_hw_stop(hdev);
1892
Hans de Goedea1d97cc2019-04-20 13:21:52 +02001893 /*
1894 * This ensures that if the work gets requeued from another
1895 * interface of the same receiver it will be a no-op.
1896 */
1897 spin_lock_irqsave(&djrcv_dev->lock, flags);
1898 djrcv_dev->ready = false;
1899 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
1900
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001901 cancel_work_sync(&djrcv_dev->work);
1902
Benjamin Tissoiresddf75402013-07-12 11:01:02 +02001903 hid_hw_close(hdev);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001904 hid_hw_stop(hdev);
1905
Hans de Goedea1d97cc2019-04-20 13:21:52 +02001906 /*
1907 * For proper operation we need access to all interfaces, so we destroy
1908 * the paired devices when we're unbound from any interface.
1909 *
1910 * Note we may still be bound to other interfaces, sharing the same
1911 * djrcv_dev, so we need locking here.
1912 */
Nestor Lopez Casado844580f2011-09-20 15:59:03 +02001913 for (i = 0; i < (DJ_MAX_PAIRED_DEVICES + DJ_DEVICE_INDEX_MIN); i++) {
Hans de Goedea1d97cc2019-04-20 13:21:52 +02001914 spin_lock_irqsave(&djrcv_dev->lock, flags);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001915 dj_dev = djrcv_dev->paired_dj_devices[i];
Hans de Goedea1d97cc2019-04-20 13:21:52 +02001916 djrcv_dev->paired_dj_devices[i] = NULL;
1917 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001918 if (dj_dev != NULL) {
1919 hid_destroy_device(dj_dev->hdev);
1920 kfree(dj_dev);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001921 }
1922 }
1923
Hans de Goedea1d97cc2019-04-20 13:21:52 +02001924 dj_put_receiver_dev(hdev);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001925}
1926
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001927static const struct hid_device_id logi_dj_receivers[] = {
Hans de Goede751cb652021-02-04 21:56:16 +01001928 { /* Logitech unifying receiver (0xc52b) */
1929 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
Hans de Goede74808f92019-04-20 13:21:54 +02001930 USB_DEVICE_ID_LOGITECH_UNIFYING_RECEIVER),
1931 .driver_data = recvr_type_dj},
Hans de Goede751cb652021-02-04 21:56:16 +01001932 { /* Logitech unifying receiver (0xc532) */
1933 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
Hans de Goede74808f92019-04-20 13:21:54 +02001934 USB_DEVICE_ID_LOGITECH_UNIFYING_RECEIVER_2),
1935 .driver_data = recvr_type_dj},
Hans de Goede751cb652021-02-04 21:56:16 +01001936
1937 { /* Logitech Nano mouse only receiver (0xc52f) */
Hans de Goede74808f92019-04-20 13:21:54 +02001938 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
1939 USB_DEVICE_ID_LOGITECH_NANO_RECEIVER),
Hans de Goede3ed224e2019-06-05 14:44:08 +02001940 .driver_data = recvr_type_mouse_only},
Hans de Goede751cb652021-02-04 21:56:16 +01001941 { /* Logitech Nano (non DJ) receiver (0xc534) */
Hans de Goede74808f92019-04-20 13:21:54 +02001942 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
1943 USB_DEVICE_ID_LOGITECH_NANO_RECEIVER_2),
1944 .driver_data = recvr_type_hidpp},
Hans de Goede751cb652021-02-04 21:56:16 +01001945
Benjamin Tissoires992ff2c2019-08-12 18:08:04 +02001946 { /* Logitech G700(s) receiver (0xc531) */
1947 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
Hans de Goede751cb652021-02-04 21:56:16 +01001948 USB_DEVICE_ID_LOGITECH_G700_RECEIVER),
Benjamin Tissoires992ff2c2019-08-12 18:08:04 +02001949 .driver_data = recvr_type_gaming_hidpp},
Filipe Laínse4000712021-01-04 20:47:17 +00001950 { /* Logitech G602 receiver (0xc537) */
1951 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
1952 0xc537),
1953 .driver_data = recvr_type_gaming_hidpp},
Filipe Laíns600ea542019-07-16 08:36:21 +01001954 { /* Logitech lightspeed receiver (0xc539) */
Benjamin Tissoiresf5fb57a2019-04-20 13:21:55 +02001955 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
Filipe Laíns5722f332019-07-30 13:24:57 +01001956 USB_DEVICE_ID_LOGITECH_NANO_RECEIVER_LIGHTSPEED_1),
1957 .driver_data = recvr_type_gaming_hidpp},
Filipe Laínsd79f7ba2019-07-16 08:37:47 +01001958 { /* Logitech powerplay receiver (0xc53a) */
1959 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
1960 USB_DEVICE_ID_LOGITECH_NANO_RECEIVER_POWERPLAY),
1961 .driver_data = recvr_type_gaming_hidpp},
Hans de Goede751cb652021-02-04 21:56:16 +01001962 { /* Logitech lightspeed receiver (0xc53f) */
1963 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
1964 USB_DEVICE_ID_LOGITECH_NANO_RECEIVER_LIGHTSPEED_1_1),
1965 .driver_data = recvr_type_gaming_hidpp},
1966
1967 { /* Logitech 27 MHz HID++ 1.0 receiver (0xc513) */
1968 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_MX3000_RECEIVER),
1969 .driver_data = recvr_type_27mhz},
Hans de Goedec9121cf2019-04-20 13:21:56 +02001970 { /* Logitech 27 MHz HID++ 1.0 receiver (0xc517) */
1971 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
1972 USB_DEVICE_ID_S510_RECEIVER_2),
1973 .driver_data = recvr_type_27mhz},
Hans de Goede1f944ac2019-04-20 13:21:57 +02001974 { /* Logitech 27 MHz HID++ 1.0 mouse-only receiver (0xc51b) */
1975 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
1976 USB_DEVICE_ID_LOGITECH_27MHZ_MOUSE_RECEIVER),
1977 .driver_data = recvr_type_27mhz},
Hans de Goede751cb652021-02-04 21:56:16 +01001978
1979 { /* Logitech MX5000 HID++ / bluetooth receiver keyboard intf. (0xc70e) */
Hans de Goedef2113c32019-04-20 13:22:03 +02001980 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
Hans de Goede751cb652021-02-04 21:56:16 +01001981 USB_DEVICE_ID_MX5000_RECEIVER_KBD_DEV),
Hans de Goedef2113c32019-04-20 13:22:03 +02001982 .driver_data = recvr_type_bluetooth},
Hans de Goede751cb652021-02-04 21:56:16 +01001983 { /* Logitech MX5000 HID++ / bluetooth receiver mouse intf. (0xc70a) */
Hans de Goedef2113c32019-04-20 13:22:03 +02001984 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
Hans de Goede751cb652021-02-04 21:56:16 +01001985 USB_DEVICE_ID_MX5000_RECEIVER_MOUSE_DEV),
Hans de Goedef2113c32019-04-20 13:22:03 +02001986 .driver_data = recvr_type_bluetooth},
Hans de Goede751cb652021-02-04 21:56:16 +01001987 { /* Logitech MX5500 HID++ / bluetooth receiver keyboard intf. (0xc71b) */
Hans de Goedeb9a94fb2019-04-28 21:25:51 +02001988 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
Hans de Goede751cb652021-02-04 21:56:16 +01001989 USB_DEVICE_ID_MX5500_RECEIVER_KBD_DEV),
Hans de Goedeb9a94fb2019-04-28 21:25:51 +02001990 .driver_data = recvr_type_bluetooth},
Hans de Goede751cb652021-02-04 21:56:16 +01001991 { /* Logitech MX5500 HID++ / bluetooth receiver mouse intf. (0xc71c) */
Hans de Goedeb9a94fb2019-04-28 21:25:51 +02001992 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
Hans de Goede751cb652021-02-04 21:56:16 +01001993 USB_DEVICE_ID_MX5500_RECEIVER_MOUSE_DEV),
Hans de Goedeb9a94fb2019-04-28 21:25:51 +02001994 .driver_data = recvr_type_bluetooth},
Hans de Goede434f7702021-02-04 21:56:17 +01001995
1996 { /* Logitech Dinovo Edge HID++ / bluetooth receiver keyboard intf. (0xc713) */
1997 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
1998 USB_DEVICE_ID_DINOVO_EDGE_RECEIVER_KBD_DEV),
1999 .driver_data = recvr_type_dinovo},
2000 { /* Logitech Dinovo Edge HID++ / bluetooth receiver mouse intf. (0xc714) */
2001 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
2002 USB_DEVICE_ID_DINOVO_EDGE_RECEIVER_MOUSE_DEV),
2003 .driver_data = recvr_type_dinovo},
2004 { /* Logitech DiNovo Mini HID++ / bluetooth receiver mouse intf. (0xc71e) */
2005 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
2006 USB_DEVICE_ID_DINOVO_MINI_RECEIVER_KBD_DEV),
2007 .driver_data = recvr_type_dinovo},
2008 { /* Logitech DiNovo Mini HID++ / bluetooth receiver keyboard intf. (0xc71f) */
2009 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
2010 USB_DEVICE_ID_DINOVO_MINI_RECEIVER_MOUSE_DEV),
2011 .driver_data = recvr_type_dinovo},
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02002012 {}
2013};
2014
2015MODULE_DEVICE_TABLE(hid, logi_dj_receivers);
2016
2017static struct hid_driver logi_djreceiver_driver = {
2018 .name = "logitech-djreceiver",
2019 .id_table = logi_dj_receivers,
2020 .probe = logi_dj_probe,
2021 .remove = logi_dj_remove,
2022 .raw_event = logi_dj_raw_event,
2023#ifdef CONFIG_PM
2024 .reset_resume = logi_dj_reset_resume,
2025#endif
2026};
2027
Benjamin Tissoiresab94e562014-09-30 13:18:28 -04002028module_hid_driver(logi_djreceiver_driver);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02002029
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02002030MODULE_LICENSE("GPL");
2031MODULE_AUTHOR("Logitech");
2032MODULE_AUTHOR("Nestor Lopez Casado");
2033MODULE_AUTHOR("nlopezcasad@logitech.com");