blob: ce00dc299404814f47467a75415c59845e4fffc0 [file] [log] [blame]
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001/*
2 * HID driver for Logitech Unifying receivers
3 *
4 * Copyright (c) 2011 Logitech
5 */
6
7/*
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 *
22 */
23
24
25#include <linux/device.h>
26#include <linux/hid.h>
27#include <linux/module.h>
Benjamin Tissoires8cb37462014-09-30 13:18:26 -040028#include <linux/kfifo.h>
Benjamin Tissoires82c0beb2019-04-20 13:21:47 +020029#include <linux/delay.h>
Hans de Goededa12b222019-04-20 13:21:59 +020030#include <linux/usb.h> /* For to_usb_interface for kvm extra intf check */
Jonathan Nieder44d27f72012-05-11 16:17:16 +020031#include <asm/unaligned.h>
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +020032#include "hid-ids.h"
Benjamin Tissoires8cb37462014-09-30 13:18:26 -040033
34#define DJ_MAX_PAIRED_DEVICES 6
Benjamin Tissoires4fcad952019-04-20 13:21:48 +020035#define DJ_MAX_NUMBER_NOTIFS 8
Benjamin Tissoires8cb37462014-09-30 13:18:26 -040036#define DJ_RECEIVER_INDEX 0
37#define DJ_DEVICE_INDEX_MIN 1
38#define DJ_DEVICE_INDEX_MAX 6
39
40#define DJREPORT_SHORT_LENGTH 15
41#define DJREPORT_LONG_LENGTH 32
42
43#define REPORT_ID_DJ_SHORT 0x20
44#define REPORT_ID_DJ_LONG 0x21
45
Benjamin Tissoires925f0f32014-09-30 13:18:29 -040046#define REPORT_ID_HIDPP_SHORT 0x10
47#define REPORT_ID_HIDPP_LONG 0x11
48
49#define HIDPP_REPORT_SHORT_LENGTH 7
50#define HIDPP_REPORT_LONG_LENGTH 20
51
52#define HIDPP_RECEIVER_INDEX 0xff
53
54#define REPORT_TYPE_RFREPORT_FIRST 0x01
Benjamin Tissoires8cb37462014-09-30 13:18:26 -040055#define REPORT_TYPE_RFREPORT_LAST 0x1F
56
57/* Command Switch to DJ mode */
58#define REPORT_TYPE_CMD_SWITCH 0x80
59#define CMD_SWITCH_PARAM_DEVBITFIELD 0x00
60#define CMD_SWITCH_PARAM_TIMEOUT_SECONDS 0x01
61#define TIMEOUT_NO_KEEPALIVE 0x00
62
63/* Command to Get the list of Paired devices */
64#define REPORT_TYPE_CMD_GET_PAIRED_DEVICES 0x81
65
66/* Device Paired Notification */
67#define REPORT_TYPE_NOTIF_DEVICE_PAIRED 0x41
68#define SPFUNCTION_MORE_NOTIF_EXPECTED 0x01
69#define SPFUNCTION_DEVICE_LIST_EMPTY 0x02
70#define DEVICE_PAIRED_PARAM_SPFUNCTION 0x00
71#define DEVICE_PAIRED_PARAM_EQUAD_ID_LSB 0x01
72#define DEVICE_PAIRED_PARAM_EQUAD_ID_MSB 0x02
73#define DEVICE_PAIRED_RF_REPORT_TYPE 0x03
74
75/* Device Un-Paired Notification */
76#define REPORT_TYPE_NOTIF_DEVICE_UNPAIRED 0x40
77
Benjamin Tissoires8cb37462014-09-30 13:18:26 -040078/* Connection Status Notification */
79#define REPORT_TYPE_NOTIF_CONNECTION_STATUS 0x42
80#define CONNECTION_STATUS_PARAM_STATUS 0x00
81#define STATUS_LINKLOSS 0x01
82
83/* Error Notification */
84#define REPORT_TYPE_NOTIF_ERROR 0x7F
85#define NOTIF_ERROR_PARAM_ETYPE 0x00
86#define ETYPE_KEEPALIVE_TIMEOUT 0x01
87
88/* supported DJ HID && RF report types */
89#define REPORT_TYPE_KEYBOARD 0x01
90#define REPORT_TYPE_MOUSE 0x02
91#define REPORT_TYPE_CONSUMER_CONTROL 0x03
92#define REPORT_TYPE_SYSTEM_CONTROL 0x04
93#define REPORT_TYPE_MEDIA_CENTER 0x08
94#define REPORT_TYPE_LEDS 0x0E
95
96/* RF Report types bitfield */
Benjamin Tissoiresa17dd1f2019-04-20 13:21:45 +020097#define STD_KEYBOARD BIT(1)
98#define STD_MOUSE BIT(2)
99#define MULTIMEDIA BIT(3)
100#define POWER_KEYS BIT(4)
101#define MEDIA_CENTER BIT(8)
102#define KBD_LEDS BIT(14)
Hans de Goede6d3c3f02019-04-20 13:22:02 +0200103/* Fake (bitnr > NUMBER_OF_HID_REPORTS) bit to track HID++ capability */
104#define HIDPP BIT_ULL(63)
Benjamin Tissoires8cb37462014-09-30 13:18:26 -0400105
Hans de Goede74808f92019-04-20 13:21:54 +0200106/* HID++ Device Connected Notification */
107#define REPORT_TYPE_NOTIF_DEVICE_CONNECTED 0x41
108#define HIDPP_PARAM_PROTO_TYPE 0x00
109#define HIDPP_PARAM_DEVICE_INFO 0x01
110#define HIDPP_PARAM_EQUAD_LSB 0x02
111#define HIDPP_PARAM_EQUAD_MSB 0x03
Hans de Goedec9121cf2019-04-20 13:21:56 +0200112#define HIDPP_PARAM_27MHZ_DEVID 0x03
Hans de Goede74808f92019-04-20 13:21:54 +0200113#define HIDPP_DEVICE_TYPE_MASK GENMASK(3, 0)
114#define HIDPP_LINK_STATUS_MASK BIT(6)
Hans de Goedef2113c32019-04-20 13:22:03 +0200115#define HIDPP_MANUFACTURER_MASK BIT(7)
Hans de Goede74808f92019-04-20 13:21:54 +0200116
Hans de Goedede76b1d2019-04-20 13:22:00 +0200117#define HIDPP_DEVICE_TYPE_KEYBOARD 1
118#define HIDPP_DEVICE_TYPE_MOUSE 2
119
Hans de Goede74808f92019-04-20 13:21:54 +0200120#define HIDPP_SET_REGISTER 0x80
Benjamin Tissoiresc0340412019-04-20 13:21:46 +0200121#define HIDPP_GET_LONG_REGISTER 0x83
Hans de Goede74808f92019-04-20 13:21:54 +0200122#define HIDPP_REG_CONNECTION_STATE 0x02
Benjamin Tissoiresc0340412019-04-20 13:21:46 +0200123#define HIDPP_REG_PAIRING_INFORMATION 0xB5
124#define HIDPP_PAIRING_INFORMATION 0x20
Hans de Goede74808f92019-04-20 13:21:54 +0200125#define HIDPP_FAKE_DEVICE_ARRIVAL 0x02
126
127enum recvr_type {
128 recvr_type_dj,
129 recvr_type_hidpp,
Benjamin Tissoiresf5fb57a2019-04-20 13:21:55 +0200130 recvr_type_gaming_hidpp,
Hans de Goede3ed224e2019-06-05 14:44:08 +0200131 recvr_type_mouse_only,
Hans de Goedec9121cf2019-04-20 13:21:56 +0200132 recvr_type_27mhz,
Hans de Goedef2113c32019-04-20 13:22:03 +0200133 recvr_type_bluetooth,
Hans de Goede74808f92019-04-20 13:21:54 +0200134};
Benjamin Tissoiresc0340412019-04-20 13:21:46 +0200135
Benjamin Tissoires8cb37462014-09-30 13:18:26 -0400136struct dj_report {
137 u8 report_id;
138 u8 device_index;
139 u8 report_type;
140 u8 report_params[DJREPORT_SHORT_LENGTH - 3];
141};
142
Benjamin Tissoires7bb56a52019-04-20 13:21:44 +0200143struct hidpp_event {
144 u8 report_id;
145 u8 device_index;
146 u8 sub_id;
147 u8 params[HIDPP_REPORT_LONG_LENGTH - 3U];
148} __packed;
149
Benjamin Tissoires8cb37462014-09-30 13:18:26 -0400150struct dj_receiver_dev {
Hans de Goedea1d97cc2019-04-20 13:21:52 +0200151 struct hid_device *mouse;
152 struct hid_device *keyboard;
Hans de Goede0ee75542019-04-20 13:21:51 +0200153 struct hid_device *hidpp;
Benjamin Tissoires8cb37462014-09-30 13:18:26 -0400154 struct dj_device *paired_dj_devices[DJ_MAX_PAIRED_DEVICES +
155 DJ_DEVICE_INDEX_MIN];
Hans de Goedea1d97cc2019-04-20 13:21:52 +0200156 struct list_head list;
157 struct kref kref;
Benjamin Tissoires8cb37462014-09-30 13:18:26 -0400158 struct work_struct work;
159 struct kfifo notif_fifo;
Hans de Goedeb6aeedd2019-04-20 13:21:53 +0200160 unsigned long last_query; /* in jiffies */
Hans de Goedea1d97cc2019-04-20 13:21:52 +0200161 bool ready;
Hans de Goede74808f92019-04-20 13:21:54 +0200162 enum recvr_type type;
163 unsigned int unnumbered_application;
Benjamin Tissoires8cb37462014-09-30 13:18:26 -0400164 spinlock_t lock;
Benjamin Tissoires8cb37462014-09-30 13:18:26 -0400165};
166
167struct dj_device {
168 struct hid_device *hdev;
169 struct dj_receiver_dev *dj_receiver_dev;
Hans de Goede6d3c3f02019-04-20 13:22:02 +0200170 u64 reports_supported;
Benjamin Tissoires8cb37462014-09-30 13:18:26 -0400171 u8 device_index;
172};
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200173
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200174#define WORKITEM_TYPE_EMPTY 0
175#define WORKITEM_TYPE_PAIRED 1
176#define WORKITEM_TYPE_UNPAIRED 2
177#define WORKITEM_TYPE_UNKNOWN 255
178
179struct dj_workitem {
180 u8 type; /* WORKITEM_TYPE_* */
181 u8 device_index;
Hans de Goedede76b1d2019-04-20 13:22:00 +0200182 u8 device_type;
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200183 u8 quad_id_msb;
184 u8 quad_id_lsb;
Hans de Goede6d3c3f02019-04-20 13:22:02 +0200185 u64 reports_supported;
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200186};
187
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200188/* Keyboard descriptor (1) */
189static const char kbd_descriptor[] = {
190 0x05, 0x01, /* USAGE_PAGE (generic Desktop) */
191 0x09, 0x06, /* USAGE (Keyboard) */
192 0xA1, 0x01, /* COLLECTION (Application) */
193 0x85, 0x01, /* REPORT_ID (1) */
194 0x95, 0x08, /* REPORT_COUNT (8) */
195 0x75, 0x01, /* REPORT_SIZE (1) */
196 0x15, 0x00, /* LOGICAL_MINIMUM (0) */
197 0x25, 0x01, /* LOGICAL_MAXIMUM (1) */
198 0x05, 0x07, /* USAGE_PAGE (Keyboard) */
199 0x19, 0xE0, /* USAGE_MINIMUM (Left Control) */
200 0x29, 0xE7, /* USAGE_MAXIMUM (Right GUI) */
201 0x81, 0x02, /* INPUT (Data,Var,Abs) */
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200202 0x95, 0x06, /* REPORT_COUNT (6) */
203 0x75, 0x08, /* REPORT_SIZE (8) */
204 0x15, 0x00, /* LOGICAL_MINIMUM (0) */
205 0x26, 0xFF, 0x00, /* LOGICAL_MAXIMUM (255) */
206 0x05, 0x07, /* USAGE_PAGE (Keyboard) */
207 0x19, 0x00, /* USAGE_MINIMUM (no event) */
208 0x2A, 0xFF, 0x00, /* USAGE_MAXIMUM (reserved) */
209 0x81, 0x00, /* INPUT (Data,Ary,Abs) */
Benjamin Tissoires0e40d352014-02-05 16:33:17 -0500210 0x85, 0x0e, /* REPORT_ID (14) */
211 0x05, 0x08, /* USAGE PAGE (LED page) */
212 0x95, 0x05, /* REPORT COUNT (5) */
213 0x75, 0x01, /* REPORT SIZE (1) */
214 0x15, 0x00, /* LOGICAL_MINIMUM (0) */
215 0x25, 0x01, /* LOGICAL_MAXIMUM (1) */
216 0x19, 0x01, /* USAGE MINIMUM (1) */
217 0x29, 0x05, /* USAGE MAXIMUM (5) */
218 0x91, 0x02, /* OUTPUT (Data, Variable, Absolute) */
219 0x95, 0x01, /* REPORT COUNT (1) */
220 0x75, 0x03, /* REPORT SIZE (3) */
221 0x91, 0x01, /* OUTPUT (Constant) */
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200222 0xC0
223};
224
225/* Mouse descriptor (2) */
226static const char mse_descriptor[] = {
227 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */
228 0x09, 0x02, /* USAGE (Mouse) */
229 0xA1, 0x01, /* COLLECTION (Application) */
230 0x85, 0x02, /* REPORT_ID = 2 */
231 0x09, 0x01, /* USAGE (pointer) */
232 0xA1, 0x00, /* COLLECTION (physical) */
233 0x05, 0x09, /* USAGE_PAGE (buttons) */
234 0x19, 0x01, /* USAGE_MIN (1) */
235 0x29, 0x10, /* USAGE_MAX (16) */
236 0x15, 0x00, /* LOGICAL_MIN (0) */
237 0x25, 0x01, /* LOGICAL_MAX (1) */
238 0x95, 0x10, /* REPORT_COUNT (16) */
239 0x75, 0x01, /* REPORT_SIZE (1) */
240 0x81, 0x02, /* INPUT (data var abs) */
241 0x05, 0x01, /* USAGE_PAGE (generic desktop) */
242 0x16, 0x01, 0xF8, /* LOGICAL_MIN (-2047) */
243 0x26, 0xFF, 0x07, /* LOGICAL_MAX (2047) */
244 0x75, 0x0C, /* REPORT_SIZE (12) */
245 0x95, 0x02, /* REPORT_COUNT (2) */
246 0x09, 0x30, /* USAGE (X) */
247 0x09, 0x31, /* USAGE (Y) */
248 0x81, 0x06, /* INPUT */
249 0x15, 0x81, /* LOGICAL_MIN (-127) */
250 0x25, 0x7F, /* LOGICAL_MAX (127) */
251 0x75, 0x08, /* REPORT_SIZE (8) */
252 0x95, 0x01, /* REPORT_COUNT (1) */
253 0x09, 0x38, /* USAGE (wheel) */
254 0x81, 0x06, /* INPUT */
255 0x05, 0x0C, /* USAGE_PAGE(consumer) */
256 0x0A, 0x38, 0x02, /* USAGE(AC Pan) */
257 0x95, 0x01, /* REPORT_COUNT (1) */
258 0x81, 0x06, /* INPUT */
259 0xC0, /* END_COLLECTION */
260 0xC0, /* END_COLLECTION */
261};
262
Hans de Goedec9121cf2019-04-20 13:21:56 +0200263/* Mouse descriptor (2) for 27 MHz receiver, only 8 buttons */
264static const char mse_27mhz_descriptor[] = {
265 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */
266 0x09, 0x02, /* USAGE (Mouse) */
267 0xA1, 0x01, /* COLLECTION (Application) */
268 0x85, 0x02, /* REPORT_ID = 2 */
269 0x09, 0x01, /* USAGE (pointer) */
270 0xA1, 0x00, /* COLLECTION (physical) */
271 0x05, 0x09, /* USAGE_PAGE (buttons) */
272 0x19, 0x01, /* USAGE_MIN (1) */
273 0x29, 0x08, /* USAGE_MAX (8) */
274 0x15, 0x00, /* LOGICAL_MIN (0) */
275 0x25, 0x01, /* LOGICAL_MAX (1) */
276 0x95, 0x08, /* REPORT_COUNT (8) */
277 0x75, 0x01, /* REPORT_SIZE (1) */
278 0x81, 0x02, /* INPUT (data var abs) */
279 0x05, 0x01, /* USAGE_PAGE (generic desktop) */
280 0x16, 0x01, 0xF8, /* LOGICAL_MIN (-2047) */
281 0x26, 0xFF, 0x07, /* LOGICAL_MAX (2047) */
282 0x75, 0x0C, /* REPORT_SIZE (12) */
283 0x95, 0x02, /* REPORT_COUNT (2) */
284 0x09, 0x30, /* USAGE (X) */
285 0x09, 0x31, /* USAGE (Y) */
286 0x81, 0x06, /* INPUT */
287 0x15, 0x81, /* LOGICAL_MIN (-127) */
288 0x25, 0x7F, /* LOGICAL_MAX (127) */
289 0x75, 0x08, /* REPORT_SIZE (8) */
290 0x95, 0x01, /* REPORT_COUNT (1) */
291 0x09, 0x38, /* USAGE (wheel) */
292 0x81, 0x06, /* INPUT */
293 0x05, 0x0C, /* USAGE_PAGE(consumer) */
294 0x0A, 0x38, 0x02, /* USAGE(AC Pan) */
295 0x95, 0x01, /* REPORT_COUNT (1) */
296 0x81, 0x06, /* INPUT */
297 0xC0, /* END_COLLECTION */
298 0xC0, /* END_COLLECTION */
299};
300
Hans de Goedef2113c32019-04-20 13:22:03 +0200301/* Mouse descriptor (2) for Bluetooth receiver, low-res hwheel, 12 buttons */
302static const char mse_bluetooth_descriptor[] = {
303 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */
304 0x09, 0x02, /* USAGE (Mouse) */
305 0xA1, 0x01, /* COLLECTION (Application) */
306 0x85, 0x02, /* REPORT_ID = 2 */
307 0x09, 0x01, /* USAGE (pointer) */
308 0xA1, 0x00, /* COLLECTION (physical) */
309 0x05, 0x09, /* USAGE_PAGE (buttons) */
310 0x19, 0x01, /* USAGE_MIN (1) */
311 0x29, 0x08, /* USAGE_MAX (8) */
312 0x15, 0x00, /* LOGICAL_MIN (0) */
313 0x25, 0x01, /* LOGICAL_MAX (1) */
314 0x95, 0x08, /* REPORT_COUNT (8) */
315 0x75, 0x01, /* REPORT_SIZE (1) */
316 0x81, 0x02, /* INPUT (data var abs) */
317 0x05, 0x01, /* USAGE_PAGE (generic desktop) */
318 0x16, 0x01, 0xF8, /* LOGICAL_MIN (-2047) */
319 0x26, 0xFF, 0x07, /* LOGICAL_MAX (2047) */
320 0x75, 0x0C, /* REPORT_SIZE (12) */
321 0x95, 0x02, /* REPORT_COUNT (2) */
322 0x09, 0x30, /* USAGE (X) */
323 0x09, 0x31, /* USAGE (Y) */
324 0x81, 0x06, /* INPUT */
325 0x15, 0x81, /* LOGICAL_MIN (-127) */
326 0x25, 0x7F, /* LOGICAL_MAX (127) */
327 0x75, 0x08, /* REPORT_SIZE (8) */
328 0x95, 0x01, /* REPORT_COUNT (1) */
329 0x09, 0x38, /* USAGE (wheel) */
330 0x81, 0x06, /* INPUT */
331 0x05, 0x0C, /* USAGE_PAGE(consumer) */
332 0x0A, 0x38, 0x02, /* USAGE(AC Pan) */
333 0x15, 0xF9, /* LOGICAL_MIN (-7) */
334 0x25, 0x07, /* LOGICAL_MAX (7) */
335 0x75, 0x04, /* REPORT_SIZE (4) */
336 0x95, 0x01, /* REPORT_COUNT (1) */
337 0x81, 0x06, /* INPUT */
338 0x05, 0x09, /* USAGE_PAGE (buttons) */
339 0x19, 0x09, /* USAGE_MIN (9) */
340 0x29, 0x0C, /* USAGE_MAX (12) */
341 0x15, 0x00, /* LOGICAL_MIN (0) */
342 0x25, 0x01, /* LOGICAL_MAX (1) */
343 0x75, 0x01, /* REPORT_SIZE (1) */
344 0x95, 0x04, /* REPORT_COUNT (4) */
345 0x81, 0x06, /* INPUT */
346 0xC0, /* END_COLLECTION */
347 0xC0, /* END_COLLECTION */
348};
349
Benjamin Tissoiresf5fb57a2019-04-20 13:21:55 +0200350/* Gaming Mouse descriptor (2) */
351static const char mse_high_res_descriptor[] = {
352 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */
353 0x09, 0x02, /* USAGE (Mouse) */
354 0xA1, 0x01, /* COLLECTION (Application) */
355 0x85, 0x02, /* REPORT_ID = 2 */
356 0x09, 0x01, /* USAGE (pointer) */
357 0xA1, 0x00, /* COLLECTION (physical) */
358 0x05, 0x09, /* USAGE_PAGE (buttons) */
359 0x19, 0x01, /* USAGE_MIN (1) */
360 0x29, 0x10, /* USAGE_MAX (16) */
361 0x15, 0x00, /* LOGICAL_MIN (0) */
362 0x25, 0x01, /* LOGICAL_MAX (1) */
363 0x95, 0x10, /* REPORT_COUNT (16) */
364 0x75, 0x01, /* REPORT_SIZE (1) */
365 0x81, 0x02, /* INPUT (data var abs) */
366 0x05, 0x01, /* USAGE_PAGE (generic desktop) */
367 0x16, 0x01, 0x80, /* LOGICAL_MIN (-32767) */
368 0x26, 0xFF, 0x7F, /* LOGICAL_MAX (32767) */
369 0x75, 0x10, /* REPORT_SIZE (16) */
370 0x95, 0x02, /* REPORT_COUNT (2) */
371 0x09, 0x30, /* USAGE (X) */
372 0x09, 0x31, /* USAGE (Y) */
373 0x81, 0x06, /* INPUT */
374 0x15, 0x81, /* LOGICAL_MIN (-127) */
375 0x25, 0x7F, /* LOGICAL_MAX (127) */
376 0x75, 0x08, /* REPORT_SIZE (8) */
377 0x95, 0x01, /* REPORT_COUNT (1) */
378 0x09, 0x38, /* USAGE (wheel) */
379 0x81, 0x06, /* INPUT */
380 0x05, 0x0C, /* USAGE_PAGE(consumer) */
381 0x0A, 0x38, 0x02, /* USAGE(AC Pan) */
382 0x95, 0x01, /* REPORT_COUNT (1) */
383 0x81, 0x06, /* INPUT */
384 0xC0, /* END_COLLECTION */
385 0xC0, /* END_COLLECTION */
386};
387
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200388/* Consumer Control descriptor (3) */
389static const char consumer_descriptor[] = {
390 0x05, 0x0C, /* USAGE_PAGE (Consumer Devices) */
391 0x09, 0x01, /* USAGE (Consumer Control) */
392 0xA1, 0x01, /* COLLECTION (Application) */
393 0x85, 0x03, /* REPORT_ID = 3 */
394 0x75, 0x10, /* REPORT_SIZE (16) */
395 0x95, 0x02, /* REPORT_COUNT (2) */
396 0x15, 0x01, /* LOGICAL_MIN (1) */
397 0x26, 0x8C, 0x02, /* LOGICAL_MAX (652) */
398 0x19, 0x01, /* USAGE_MIN (1) */
399 0x2A, 0x8C, 0x02, /* USAGE_MAX (652) */
400 0x81, 0x00, /* INPUT (Data Ary Abs) */
401 0xC0, /* END_COLLECTION */
402}; /* */
403
404/* System control descriptor (4) */
405static const char syscontrol_descriptor[] = {
406 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */
407 0x09, 0x80, /* USAGE (System Control) */
408 0xA1, 0x01, /* COLLECTION (Application) */
409 0x85, 0x04, /* REPORT_ID = 4 */
410 0x75, 0x02, /* REPORT_SIZE (2) */
411 0x95, 0x01, /* REPORT_COUNT (1) */
412 0x15, 0x01, /* LOGICAL_MIN (1) */
413 0x25, 0x03, /* LOGICAL_MAX (3) */
414 0x09, 0x82, /* USAGE (System Sleep) */
415 0x09, 0x81, /* USAGE (System Power Down) */
416 0x09, 0x83, /* USAGE (System Wake Up) */
417 0x81, 0x60, /* INPUT (Data Ary Abs NPrf Null) */
418 0x75, 0x06, /* REPORT_SIZE (6) */
419 0x81, 0x03, /* INPUT (Cnst Var Abs) */
420 0xC0, /* END_COLLECTION */
421};
422
423/* Media descriptor (8) */
424static const char media_descriptor[] = {
425 0x06, 0xbc, 0xff, /* Usage Page 0xffbc */
426 0x09, 0x88, /* Usage 0x0088 */
427 0xa1, 0x01, /* BeginCollection */
428 0x85, 0x08, /* Report ID 8 */
429 0x19, 0x01, /* Usage Min 0x0001 */
430 0x29, 0xff, /* Usage Max 0x00ff */
431 0x15, 0x01, /* Logical Min 1 */
432 0x26, 0xff, 0x00, /* Logical Max 255 */
433 0x75, 0x08, /* Report Size 8 */
434 0x95, 0x01, /* Report Count 1 */
435 0x81, 0x00, /* Input */
436 0xc0, /* EndCollection */
437}; /* */
438
Benjamin Tissoires925f0f32014-09-30 13:18:29 -0400439/* HIDPP descriptor */
440static const char hidpp_descriptor[] = {
441 0x06, 0x00, 0xff, /* Usage Page (Vendor Defined Page 1) */
442 0x09, 0x01, /* Usage (Vendor Usage 1) */
443 0xa1, 0x01, /* Collection (Application) */
444 0x85, 0x10, /* Report ID (16) */
445 0x75, 0x08, /* Report Size (8) */
446 0x95, 0x06, /* Report Count (6) */
447 0x15, 0x00, /* Logical Minimum (0) */
448 0x26, 0xff, 0x00, /* Logical Maximum (255) */
449 0x09, 0x01, /* Usage (Vendor Usage 1) */
450 0x81, 0x00, /* Input (Data,Arr,Abs) */
451 0x09, 0x01, /* Usage (Vendor Usage 1) */
452 0x91, 0x00, /* Output (Data,Arr,Abs) */
453 0xc0, /* End Collection */
454 0x06, 0x00, 0xff, /* Usage Page (Vendor Defined Page 1) */
455 0x09, 0x02, /* Usage (Vendor Usage 2) */
456 0xa1, 0x01, /* Collection (Application) */
457 0x85, 0x11, /* Report ID (17) */
458 0x75, 0x08, /* Report Size (8) */
459 0x95, 0x13, /* Report Count (19) */
460 0x15, 0x00, /* Logical Minimum (0) */
461 0x26, 0xff, 0x00, /* Logical Maximum (255) */
462 0x09, 0x02, /* Usage (Vendor Usage 2) */
463 0x81, 0x00, /* Input (Data,Arr,Abs) */
464 0x09, 0x02, /* Usage (Vendor Usage 2) */
465 0x91, 0x00, /* Output (Data,Arr,Abs) */
466 0xc0, /* End Collection */
467 0x06, 0x00, 0xff, /* Usage Page (Vendor Defined Page 1) */
468 0x09, 0x04, /* Usage (Vendor Usage 0x04) */
469 0xa1, 0x01, /* Collection (Application) */
470 0x85, 0x20, /* Report ID (32) */
471 0x75, 0x08, /* Report Size (8) */
472 0x95, 0x0e, /* Report Count (14) */
473 0x15, 0x00, /* Logical Minimum (0) */
474 0x26, 0xff, 0x00, /* Logical Maximum (255) */
475 0x09, 0x41, /* Usage (Vendor Usage 0x41) */
476 0x81, 0x00, /* Input (Data,Arr,Abs) */
477 0x09, 0x41, /* Usage (Vendor Usage 0x41) */
478 0x91, 0x00, /* Output (Data,Arr,Abs) */
479 0x85, 0x21, /* Report ID (33) */
480 0x95, 0x1f, /* Report Count (31) */
481 0x15, 0x00, /* Logical Minimum (0) */
482 0x26, 0xff, 0x00, /* Logical Maximum (255) */
483 0x09, 0x42, /* Usage (Vendor Usage 0x42) */
484 0x81, 0x00, /* Input (Data,Arr,Abs) */
485 0x09, 0x42, /* Usage (Vendor Usage 0x42) */
486 0x91, 0x00, /* Output (Data,Arr,Abs) */
487 0xc0, /* End Collection */
488};
489
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200490/* Maximum size of all defined hid reports in bytes (including report id) */
491#define MAX_REPORT_SIZE 8
492
Henrik Rydberg2a039bf2012-04-22 14:21:39 +0200493/* Make sure all descriptors are present here */
494#define MAX_RDESC_SIZE \
495 (sizeof(kbd_descriptor) + \
Hans de Goedef2113c32019-04-20 13:22:03 +0200496 sizeof(mse_bluetooth_descriptor) + \
Henrik Rydberg2a039bf2012-04-22 14:21:39 +0200497 sizeof(consumer_descriptor) + \
498 sizeof(syscontrol_descriptor) + \
Benjamin Tissoires925f0f32014-09-30 13:18:29 -0400499 sizeof(media_descriptor) + \
500 sizeof(hidpp_descriptor))
Henrik Rydberg2a039bf2012-04-22 14:21:39 +0200501
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200502/* Number of possible hid report types that can be created by this driver.
503 *
504 * Right now, RF report types have the same report types (or report id's)
505 * than the hid report created from those RF reports. In the future
506 * this doesnt have to be true.
507 *
508 * For instance, RF report type 0x01 which has a size of 8 bytes, corresponds
509 * to hid report id 0x01, this is standard keyboard. Same thing applies to mice
510 * reports and consumer control, etc. If a new RF report is created, it doesn't
511 * has to have the same report id as its corresponding hid report, so an
512 * translation may have to take place for future report types.
513 */
514#define NUMBER_OF_HID_REPORTS 32
515static const u8 hid_reportid_size_map[NUMBER_OF_HID_REPORTS] = {
516 [1] = 8, /* Standard keyboard */
517 [2] = 8, /* Standard mouse */
518 [3] = 5, /* Consumer control */
519 [4] = 2, /* System control */
520 [8] = 2, /* Media Center */
521};
522
523
524#define LOGITECH_DJ_INTERFACE_NUMBER 0x02
525
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200526static struct hid_ll_driver logi_dj_ll_driver;
527
Nestor Lopez Casadoc63e0e32013-07-18 06:21:30 -0700528static int logi_dj_recv_query_paired_devices(struct dj_receiver_dev *djrcv_dev);
Hans de Goedea1d97cc2019-04-20 13:21:52 +0200529static void delayedwork_callback(struct work_struct *work);
530
531static LIST_HEAD(dj_hdev_list);
532static DEFINE_MUTEX(dj_hdev_list_lock);
533
534/*
535 * dj/HID++ receivers are really a single logical entity, but for BIOS/Windows
536 * compatibility they have multiple USB interfaces. On HID++ receivers we need
537 * to listen for input reports on both interfaces. The functions below are used
538 * to create a single struct dj_receiver_dev for all interfaces belonging to
539 * a single USB-device / receiver.
540 */
Hans de Goedef2113c32019-04-20 13:22:03 +0200541static struct dj_receiver_dev *dj_find_receiver_dev(struct hid_device *hdev,
542 enum recvr_type type)
Hans de Goedea1d97cc2019-04-20 13:21:52 +0200543{
544 struct dj_receiver_dev *djrcv_dev;
Hans de Goedef2113c32019-04-20 13:22:03 +0200545 char sep;
546
547 /*
548 * The bluetooth receiver contains a built-in hub and has separate
549 * USB-devices for the keyboard and mouse interfaces.
550 */
551 sep = (type == recvr_type_bluetooth) ? '.' : '/';
Hans de Goedea1d97cc2019-04-20 13:21:52 +0200552
553 /* Try to find an already-probed interface from the same device */
554 list_for_each_entry(djrcv_dev, &dj_hdev_list, list) {
555 if (djrcv_dev->mouse &&
Hans de Goedef2113c32019-04-20 13:22:03 +0200556 hid_compare_device_paths(hdev, djrcv_dev->mouse, sep)) {
Hans de Goedea1d97cc2019-04-20 13:21:52 +0200557 kref_get(&djrcv_dev->kref);
558 return djrcv_dev;
559 }
560 if (djrcv_dev->keyboard &&
Hans de Goedef2113c32019-04-20 13:22:03 +0200561 hid_compare_device_paths(hdev, djrcv_dev->keyboard, sep)) {
Hans de Goedea1d97cc2019-04-20 13:21:52 +0200562 kref_get(&djrcv_dev->kref);
563 return djrcv_dev;
564 }
565 if (djrcv_dev->hidpp &&
Hans de Goedef2113c32019-04-20 13:22:03 +0200566 hid_compare_device_paths(hdev, djrcv_dev->hidpp, sep)) {
Hans de Goedea1d97cc2019-04-20 13:21:52 +0200567 kref_get(&djrcv_dev->kref);
568 return djrcv_dev;
569 }
570 }
571
572 return NULL;
573}
574
575static void dj_release_receiver_dev(struct kref *kref)
576{
577 struct dj_receiver_dev *djrcv_dev = container_of(kref, struct dj_receiver_dev, kref);
578
579 list_del(&djrcv_dev->list);
580 kfifo_free(&djrcv_dev->notif_fifo);
581 kfree(djrcv_dev);
582}
583
584static void dj_put_receiver_dev(struct hid_device *hdev)
585{
586 struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
587
588 mutex_lock(&dj_hdev_list_lock);
589
590 if (djrcv_dev->mouse == hdev)
591 djrcv_dev->mouse = NULL;
592 if (djrcv_dev->keyboard == hdev)
593 djrcv_dev->keyboard = NULL;
594 if (djrcv_dev->hidpp == hdev)
595 djrcv_dev->hidpp = NULL;
596
597 kref_put(&djrcv_dev->kref, dj_release_receiver_dev);
598
599 mutex_unlock(&dj_hdev_list_lock);
600}
601
602static struct dj_receiver_dev *dj_get_receiver_dev(struct hid_device *hdev,
Hans de Goede74808f92019-04-20 13:21:54 +0200603 enum recvr_type type,
Hans de Goedea1d97cc2019-04-20 13:21:52 +0200604 unsigned int application,
605 bool is_hidpp)
606{
607 struct dj_receiver_dev *djrcv_dev;
608
609 mutex_lock(&dj_hdev_list_lock);
610
Hans de Goedef2113c32019-04-20 13:22:03 +0200611 djrcv_dev = dj_find_receiver_dev(hdev, type);
Hans de Goedea1d97cc2019-04-20 13:21:52 +0200612 if (!djrcv_dev) {
613 djrcv_dev = kzalloc(sizeof(*djrcv_dev), GFP_KERNEL);
614 if (!djrcv_dev)
615 goto out;
616
617 INIT_WORK(&djrcv_dev->work, delayedwork_callback);
618 spin_lock_init(&djrcv_dev->lock);
619 if (kfifo_alloc(&djrcv_dev->notif_fifo,
620 DJ_MAX_NUMBER_NOTIFS * sizeof(struct dj_workitem),
621 GFP_KERNEL)) {
622 kfree(djrcv_dev);
623 djrcv_dev = NULL;
624 goto out;
625 }
626 kref_init(&djrcv_dev->kref);
627 list_add_tail(&djrcv_dev->list, &dj_hdev_list);
Hans de Goedeb6aeedd2019-04-20 13:21:53 +0200628 djrcv_dev->last_query = jiffies;
Hans de Goede74808f92019-04-20 13:21:54 +0200629 djrcv_dev->type = type;
Hans de Goedea1d97cc2019-04-20 13:21:52 +0200630 }
631
632 if (application == HID_GD_KEYBOARD)
633 djrcv_dev->keyboard = hdev;
634 if (application == HID_GD_MOUSE)
635 djrcv_dev->mouse = hdev;
636 if (is_hidpp)
637 djrcv_dev->hidpp = hdev;
638
639 hid_set_drvdata(hdev, djrcv_dev);
640out:
641 mutex_unlock(&dj_hdev_list_lock);
642 return djrcv_dev;
643}
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200644
645static void logi_dj_recv_destroy_djhid_device(struct dj_receiver_dev *djrcv_dev,
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200646 struct dj_workitem *workitem)
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200647{
648 /* Called in delayed work context */
649 struct dj_device *dj_dev;
650 unsigned long flags;
651
652 spin_lock_irqsave(&djrcv_dev->lock, flags);
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200653 dj_dev = djrcv_dev->paired_dj_devices[workitem->device_index];
654 djrcv_dev->paired_dj_devices[workitem->device_index] = NULL;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200655 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
656
657 if (dj_dev != NULL) {
658 hid_destroy_device(dj_dev->hdev);
659 kfree(dj_dev);
660 } else {
Hans de Goede0ee75542019-04-20 13:21:51 +0200661 hid_err(djrcv_dev->hidpp, "%s: can't destroy a NULL device\n",
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200662 __func__);
663 }
664}
665
666static void logi_dj_recv_add_djhid_device(struct dj_receiver_dev *djrcv_dev,
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200667 struct dj_workitem *workitem)
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200668{
669 /* Called in delayed work context */
Hans de Goede0ee75542019-04-20 13:21:51 +0200670 struct hid_device *djrcv_hdev = djrcv_dev->hidpp;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200671 struct hid_device *dj_hiddev;
672 struct dj_device *dj_dev;
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200673 u8 device_index = workitem->device_index;
Hans de Goedef41d7662019-04-20 13:21:50 +0200674 unsigned long flags;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200675
676 /* Device index goes from 1 to 6, we need 3 bytes to store the
677 * semicolon, the index, and a null terminator
678 */
679 unsigned char tmpstr[3];
680
Hans de Goedef41d7662019-04-20 13:21:50 +0200681 /* We are the only one ever adding a device, no need to lock */
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200682 if (djrcv_dev->paired_dj_devices[device_index]) {
Nestor Lopez Casadoc63e0e32013-07-18 06:21:30 -0700683 /* The device is already known. No need to reallocate it. */
684 dbg_hid("%s: device is already known\n", __func__);
685 return;
686 }
687
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200688 dj_hiddev = hid_allocate_device();
689 if (IS_ERR(dj_hiddev)) {
Hans de Goedeaca22a32019-04-20 13:21:58 +0200690 hid_err(djrcv_hdev, "%s: hid_allocate_dev failed\n", __func__);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200691 return;
692 }
693
694 dj_hiddev->ll_driver = &logi_dj_ll_driver;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200695
696 dj_hiddev->dev.parent = &djrcv_hdev->dev;
697 dj_hiddev->bus = BUS_USB;
Benjamin Tissoires82c0beb2019-04-20 13:21:47 +0200698 dj_hiddev->vendor = djrcv_hdev->vendor;
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200699 dj_hiddev->product = (workitem->quad_id_msb << 8) |
700 workitem->quad_id_lsb;
Hans de Goedede76b1d2019-04-20 13:22:00 +0200701 if (workitem->device_type) {
702 const char *type_str = "Device";
703
704 switch (workitem->device_type) {
705 case 0x01: type_str = "Keyboard"; break;
706 case 0x02: type_str = "Mouse"; break;
707 case 0x03: type_str = "Numpad"; break;
708 case 0x04: type_str = "Presenter"; break;
709 case 0x07: type_str = "Remote Control"; break;
710 case 0x08: type_str = "Trackball"; break;
711 case 0x09: type_str = "Touchpad"; break;
712 }
713 snprintf(dj_hiddev->name, sizeof(dj_hiddev->name),
714 "Logitech Wireless %s PID:%04x",
715 type_str, dj_hiddev->product);
716 } else {
717 snprintf(dj_hiddev->name, sizeof(dj_hiddev->name),
718 "Logitech Unifying Device. Wireless PID:%04x",
719 dj_hiddev->product);
720 }
Benjamin Tissoiresd6102742014-09-30 13:18:25 -0400721
Hans de Goedec9121cf2019-04-20 13:21:56 +0200722 if (djrcv_dev->type == recvr_type_27mhz)
723 dj_hiddev->group = HID_GROUP_LOGITECH_27MHZ_DEVICE;
724 else
725 dj_hiddev->group = HID_GROUP_LOGITECH_DJ_DEVICE;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200726
Benjamin Tissoires82c0beb2019-04-20 13:21:47 +0200727 memcpy(dj_hiddev->phys, djrcv_hdev->phys, sizeof(djrcv_hdev->phys));
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200728 snprintf(tmpstr, sizeof(tmpstr), ":%d", device_index);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200729 strlcat(dj_hiddev->phys, tmpstr, sizeof(dj_hiddev->phys));
730
731 dj_dev = kzalloc(sizeof(struct dj_device), GFP_KERNEL);
732
733 if (!dj_dev) {
Hans de Goedeaca22a32019-04-20 13:21:58 +0200734 hid_err(djrcv_hdev, "%s: failed allocating dj_dev\n", __func__);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200735 goto dj_device_allocate_fail;
736 }
737
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200738 dj_dev->reports_supported = workitem->reports_supported;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200739 dj_dev->hdev = dj_hiddev;
740 dj_dev->dj_receiver_dev = djrcv_dev;
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200741 dj_dev->device_index = device_index;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200742 dj_hiddev->driver_data = dj_dev;
743
Hans de Goedef41d7662019-04-20 13:21:50 +0200744 spin_lock_irqsave(&djrcv_dev->lock, flags);
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200745 djrcv_dev->paired_dj_devices[device_index] = dj_dev;
Hans de Goedef41d7662019-04-20 13:21:50 +0200746 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200747
748 if (hid_add_device(dj_hiddev)) {
Hans de Goedeaca22a32019-04-20 13:21:58 +0200749 hid_err(djrcv_hdev, "%s: failed adding dj_device\n", __func__);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200750 goto hid_add_device_fail;
751 }
752
753 return;
754
755hid_add_device_fail:
Hans de Goedef41d7662019-04-20 13:21:50 +0200756 spin_lock_irqsave(&djrcv_dev->lock, flags);
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200757 djrcv_dev->paired_dj_devices[device_index] = NULL;
Hans de Goedef41d7662019-04-20 13:21:50 +0200758 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200759 kfree(dj_dev);
760dj_device_allocate_fail:
761 hid_destroy_device(dj_hiddev);
762}
763
764static void delayedwork_callback(struct work_struct *work)
765{
766 struct dj_receiver_dev *djrcv_dev =
767 container_of(work, struct dj_receiver_dev, work);
768
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200769 struct dj_workitem workitem;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200770 unsigned long flags;
771 int count;
Nestor Lopez Casadoc63e0e32013-07-18 06:21:30 -0700772 int retval;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200773
774 dbg_hid("%s\n", __func__);
775
776 spin_lock_irqsave(&djrcv_dev->lock, flags);
777
Hans de Goedea1d97cc2019-04-20 13:21:52 +0200778 /*
779 * Since we attach to multiple interfaces, we may get scheduled before
780 * we are bound to the HID++ interface, catch this.
781 */
782 if (!djrcv_dev->ready) {
783 pr_warn("%s: delayedwork queued before hidpp interface was enumerated\n",
784 __func__);
785 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
786 return;
787 }
788
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200789 count = kfifo_out(&djrcv_dev->notif_fifo, &workitem, sizeof(workitem));
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200790
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200791 if (count != sizeof(workitem)) {
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200792 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
793 return;
794 }
795
Hans de Goedee316aa62019-04-20 13:22:01 +0200796 if (!kfifo_is_empty(&djrcv_dev->notif_fifo))
797 schedule_work(&djrcv_dev->work);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200798
799 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
800
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200801 switch (workitem.type) {
802 case WORKITEM_TYPE_PAIRED:
803 logi_dj_recv_add_djhid_device(djrcv_dev, &workitem);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200804 break;
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200805 case WORKITEM_TYPE_UNPAIRED:
806 logi_dj_recv_destroy_djhid_device(djrcv_dev, &workitem);
807 break;
808 case WORKITEM_TYPE_UNKNOWN:
809 retval = logi_dj_recv_query_paired_devices(djrcv_dev);
810 if (retval) {
Hans de Goede0ee75542019-04-20 13:21:51 +0200811 hid_err(djrcv_dev->hidpp, "%s: logi_dj_recv_query_paired_devices error: %d\n",
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200812 __func__, retval);
813 }
814 break;
815 case WORKITEM_TYPE_EMPTY:
816 dbg_hid("%s: device list is empty\n", __func__);
817 break;
818 }
819}
820
Hans de Goedeb6aeedd2019-04-20 13:21:53 +0200821/*
822 * Sometimes we receive reports for which we do not have a paired dj_device
823 * associated with the device_index or report-type to forward the report to.
824 * This means that the original "device paired" notification corresponding
825 * to the dj_device never arrived to this driver. Possible reasons for this are:
826 * 1) hid-core discards all packets coming from a device during probe().
827 * 2) if the receiver is plugged into a KVM switch then the pairing reports
828 * are only forwarded to it if the focus is on this PC.
829 * This function deals with this by re-asking the receiver for the list of
830 * connected devices in the delayed work callback.
831 * This function MUST be called with djrcv->lock held.
832 */
833static void logi_dj_recv_queue_unknown_work(struct dj_receiver_dev *djrcv_dev)
834{
835 struct dj_workitem workitem = { .type = WORKITEM_TYPE_UNKNOWN };
836
837 /* Rate limit queries done because of unhandeled reports to 2/sec */
838 if (time_before(jiffies, djrcv_dev->last_query + HZ / 2))
839 return;
840
841 kfifo_in(&djrcv_dev->notif_fifo, &workitem, sizeof(workitem));
842 schedule_work(&djrcv_dev->work);
843}
844
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200845static void logi_dj_recv_queue_notification(struct dj_receiver_dev *djrcv_dev,
846 struct dj_report *dj_report)
847{
848 /* We are called from atomic context (tasklet && djrcv->lock held) */
849 struct dj_workitem workitem = {
850 .device_index = dj_report->device_index,
851 };
852
853 switch (dj_report->report_type) {
854 case REPORT_TYPE_NOTIF_DEVICE_PAIRED:
855 workitem.type = WORKITEM_TYPE_PAIRED;
856 if (dj_report->report_params[DEVICE_PAIRED_PARAM_SPFUNCTION] &
857 SPFUNCTION_DEVICE_LIST_EMPTY) {
858 workitem.type = WORKITEM_TYPE_EMPTY;
859 break;
860 }
861 /* fall-through */
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200862 case REPORT_TYPE_NOTIF_DEVICE_UNPAIRED:
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200863 workitem.quad_id_msb =
864 dj_report->report_params[DEVICE_PAIRED_PARAM_EQUAD_ID_MSB];
865 workitem.quad_id_lsb =
866 dj_report->report_params[DEVICE_PAIRED_PARAM_EQUAD_ID_LSB];
867 workitem.reports_supported = get_unaligned_le32(
868 dj_report->report_params +
869 DEVICE_PAIRED_RF_REPORT_TYPE);
Hans de Goede6d3c3f02019-04-20 13:22:02 +0200870 workitem.reports_supported |= HIDPP;
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200871 if (dj_report->report_type == REPORT_TYPE_NOTIF_DEVICE_UNPAIRED)
872 workitem.type = WORKITEM_TYPE_UNPAIRED;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200873 break;
874 default:
Hans de Goedeb6aeedd2019-04-20 13:21:53 +0200875 logi_dj_recv_queue_unknown_work(djrcv_dev);
876 return;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200877 }
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200878
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200879 kfifo_in(&djrcv_dev->notif_fifo, &workitem, sizeof(workitem));
Hans de Goedee316aa62019-04-20 13:22:01 +0200880 schedule_work(&djrcv_dev->work);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200881}
882
Hans de Goede3ed224e2019-06-05 14:44:08 +0200883static void logi_hidpp_dev_conn_notif_equad(struct hid_device *hdev,
884 struct hidpp_event *hidpp_report,
Hans de Goede74808f92019-04-20 13:21:54 +0200885 struct dj_workitem *workitem)
886{
Hans de Goede3ed224e2019-06-05 14:44:08 +0200887 struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
888
Hans de Goede74808f92019-04-20 13:21:54 +0200889 workitem->type = WORKITEM_TYPE_PAIRED;
Hans de Goedede76b1d2019-04-20 13:22:00 +0200890 workitem->device_type = hidpp_report->params[HIDPP_PARAM_DEVICE_INFO] &
891 HIDPP_DEVICE_TYPE_MASK;
Hans de Goede74808f92019-04-20 13:21:54 +0200892 workitem->quad_id_msb = hidpp_report->params[HIDPP_PARAM_EQUAD_MSB];
893 workitem->quad_id_lsb = hidpp_report->params[HIDPP_PARAM_EQUAD_LSB];
Hans de Goedede76b1d2019-04-20 13:22:00 +0200894 switch (workitem->device_type) {
Hans de Goede74808f92019-04-20 13:21:54 +0200895 case REPORT_TYPE_KEYBOARD:
896 workitem->reports_supported |= STD_KEYBOARD | MULTIMEDIA |
Hans de Goede6d3c3f02019-04-20 13:22:02 +0200897 POWER_KEYS | MEDIA_CENTER |
898 HIDPP;
Hans de Goede74808f92019-04-20 13:21:54 +0200899 break;
900 case REPORT_TYPE_MOUSE:
Hans de Goede6d3c3f02019-04-20 13:22:02 +0200901 workitem->reports_supported |= STD_MOUSE | HIDPP;
Hans de Goede3ed224e2019-06-05 14:44:08 +0200902 if (djrcv_dev->type == recvr_type_mouse_only)
903 workitem->reports_supported |= MULTIMEDIA;
Hans de Goede74808f92019-04-20 13:21:54 +0200904 break;
905 }
906}
907
Hans de Goedec9121cf2019-04-20 13:21:56 +0200908static void logi_hidpp_dev_conn_notif_27mhz(struct hid_device *hdev,
909 struct hidpp_event *hidpp_report,
910 struct dj_workitem *workitem)
911{
912 workitem->type = WORKITEM_TYPE_PAIRED;
913 workitem->quad_id_lsb = hidpp_report->params[HIDPP_PARAM_27MHZ_DEVID];
914 switch (hidpp_report->device_index) {
915 case 1: /* Index 1 is always a mouse */
916 case 2: /* Index 2 is always a mouse */
Hans de Goedede76b1d2019-04-20 13:22:00 +0200917 workitem->device_type = HIDPP_DEVICE_TYPE_MOUSE;
Hans de Goede6d3c3f02019-04-20 13:22:02 +0200918 workitem->reports_supported |= STD_MOUSE | HIDPP;
Hans de Goedec9121cf2019-04-20 13:21:56 +0200919 break;
920 case 3: /* Index 3 is always the keyboard */
921 case 4: /* Index 4 is used for an optional separate numpad */
Hans de Goedede76b1d2019-04-20 13:22:00 +0200922 workitem->device_type = HIDPP_DEVICE_TYPE_KEYBOARD;
Hans de Goedec9121cf2019-04-20 13:21:56 +0200923 workitem->reports_supported |= STD_KEYBOARD | MULTIMEDIA |
Hans de Goede6d3c3f02019-04-20 13:22:02 +0200924 POWER_KEYS | HIDPP;
Hans de Goedec9121cf2019-04-20 13:21:56 +0200925 break;
926 default:
927 hid_warn(hdev, "%s: unexpected device-index %d", __func__,
928 hidpp_report->device_index);
929 }
930}
931
Hans de Goede74808f92019-04-20 13:21:54 +0200932static void logi_hidpp_recv_queue_notif(struct hid_device *hdev,
933 struct hidpp_event *hidpp_report)
934{
935 /* We are called from atomic context (tasklet && djrcv->lock held) */
936 struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
937 const char *device_type = "UNKNOWN";
938 struct dj_workitem workitem = {
939 .type = WORKITEM_TYPE_EMPTY,
940 .device_index = hidpp_report->device_index,
941 };
942
943 switch (hidpp_report->params[HIDPP_PARAM_PROTO_TYPE]) {
944 case 0x01:
945 device_type = "Bluetooth";
Hans de Goedef2113c32019-04-20 13:22:03 +0200946 /* Bluetooth connect packet contents is the same as (e)QUAD */
Hans de Goede3ed224e2019-06-05 14:44:08 +0200947 logi_hidpp_dev_conn_notif_equad(hdev, hidpp_report, &workitem);
Hans de Goedef2113c32019-04-20 13:22:03 +0200948 if (!(hidpp_report->params[HIDPP_PARAM_DEVICE_INFO] &
949 HIDPP_MANUFACTURER_MASK)) {
950 hid_info(hdev, "Non Logitech device connected on slot %d\n",
951 hidpp_report->device_index);
952 workitem.reports_supported &= ~HIDPP;
953 }
Hans de Goede74808f92019-04-20 13:21:54 +0200954 break;
955 case 0x02:
956 device_type = "27 Mhz";
Hans de Goedec9121cf2019-04-20 13:21:56 +0200957 logi_hidpp_dev_conn_notif_27mhz(hdev, hidpp_report, &workitem);
Hans de Goede74808f92019-04-20 13:21:54 +0200958 break;
959 case 0x03:
960 device_type = "QUAD or eQUAD";
Hans de Goede3ed224e2019-06-05 14:44:08 +0200961 logi_hidpp_dev_conn_notif_equad(hdev, hidpp_report, &workitem);
Hans de Goede74808f92019-04-20 13:21:54 +0200962 break;
963 case 0x04:
964 device_type = "eQUAD step 4 DJ";
Hans de Goede3ed224e2019-06-05 14:44:08 +0200965 logi_hidpp_dev_conn_notif_equad(hdev, hidpp_report, &workitem);
Hans de Goede74808f92019-04-20 13:21:54 +0200966 break;
967 case 0x05:
968 device_type = "DFU Lite";
969 break;
970 case 0x06:
971 device_type = "eQUAD step 4 Lite";
Hans de Goede3ed224e2019-06-05 14:44:08 +0200972 logi_hidpp_dev_conn_notif_equad(hdev, hidpp_report, &workitem);
Hans de Goede74808f92019-04-20 13:21:54 +0200973 break;
974 case 0x07:
975 device_type = "eQUAD step 4 Gaming";
976 break;
977 case 0x08:
978 device_type = "eQUAD step 4 for gamepads";
979 break;
980 case 0x0a:
981 device_type = "eQUAD nano Lite";
Hans de Goede3ed224e2019-06-05 14:44:08 +0200982 logi_hidpp_dev_conn_notif_equad(hdev, hidpp_report, &workitem);
Hans de Goede74808f92019-04-20 13:21:54 +0200983 break;
984 case 0x0c:
985 device_type = "eQUAD Lightspeed";
Hans de Goede3ed224e2019-06-05 14:44:08 +0200986 logi_hidpp_dev_conn_notif_equad(hdev, hidpp_report, &workitem);
Benjamin Tissoiresf5fb57a2019-04-20 13:21:55 +0200987 workitem.reports_supported |= STD_KEYBOARD;
Hans de Goede74808f92019-04-20 13:21:54 +0200988 break;
989 }
990
991 if (workitem.type == WORKITEM_TYPE_EMPTY) {
992 hid_warn(hdev,
993 "unusable device of type %s (0x%02x) connected on slot %d",
994 device_type,
995 hidpp_report->params[HIDPP_PARAM_PROTO_TYPE],
996 hidpp_report->device_index);
997 return;
998 }
999
1000 hid_info(hdev, "device of type %s (0x%02x) connected on slot %d",
1001 device_type, hidpp_report->params[HIDPP_PARAM_PROTO_TYPE],
1002 hidpp_report->device_index);
1003
Hans de Goede74808f92019-04-20 13:21:54 +02001004 kfifo_in(&djrcv_dev->notif_fifo, &workitem, sizeof(workitem));
Hans de Goedee316aa62019-04-20 13:22:01 +02001005 schedule_work(&djrcv_dev->work);
Hans de Goede74808f92019-04-20 13:21:54 +02001006}
1007
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001008static void logi_dj_recv_forward_null_report(struct dj_receiver_dev *djrcv_dev,
1009 struct dj_report *dj_report)
1010{
1011 /* We are called from atomic context (tasklet && djrcv->lock held) */
1012 unsigned int i;
1013 u8 reportbuffer[MAX_REPORT_SIZE];
1014 struct dj_device *djdev;
1015
1016 djdev = djrcv_dev->paired_dj_devices[dj_report->device_index];
1017
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001018 memset(reportbuffer, 0, sizeof(reportbuffer));
1019
1020 for (i = 0; i < NUMBER_OF_HID_REPORTS; i++) {
1021 if (djdev->reports_supported & (1 << i)) {
1022 reportbuffer[0] = i;
1023 if (hid_input_report(djdev->hdev,
1024 HID_INPUT_REPORT,
1025 reportbuffer,
1026 hid_reportid_size_map[i], 1)) {
1027 dbg_hid("hid_input_report error sending null "
1028 "report\n");
1029 }
1030 }
1031 }
1032}
1033
Benjamin Tissoires83898232019-04-20 13:21:43 +02001034static void logi_dj_recv_forward_dj(struct dj_receiver_dev *djrcv_dev,
1035 struct dj_report *dj_report)
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001036{
1037 /* We are called from atomic context (tasklet && djrcv->lock held) */
1038 struct dj_device *dj_device;
1039
1040 dj_device = djrcv_dev->paired_dj_devices[dj_report->device_index];
1041
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001042 if ((dj_report->report_type > ARRAY_SIZE(hid_reportid_size_map) - 1) ||
1043 (hid_reportid_size_map[dj_report->report_type] == 0)) {
1044 dbg_hid("invalid report type:%x\n", dj_report->report_type);
1045 return;
1046 }
1047
1048 if (hid_input_report(dj_device->hdev,
1049 HID_INPUT_REPORT, &dj_report->report_type,
1050 hid_reportid_size_map[dj_report->report_type], 1)) {
1051 dbg_hid("hid_input_report error\n");
1052 }
1053}
1054
Benjamin Tissoires83898232019-04-20 13:21:43 +02001055static void logi_dj_recv_forward_report(struct dj_device *dj_dev, u8 *data,
1056 int size)
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001057{
1058 /* We are called from atomic context (tasklet && djrcv->lock held) */
1059 if (hid_input_report(dj_dev->hdev, HID_INPUT_REPORT, data, size, 1))
1060 dbg_hid("hid_input_report error\n");
1061}
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001062
Hans de Goede74808f92019-04-20 13:21:54 +02001063static void logi_dj_recv_forward_input_report(struct hid_device *hdev,
1064 u8 *data, int size)
1065{
1066 struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
1067 struct dj_device *dj_dev;
1068 unsigned long flags;
1069 u8 report = data[0];
1070 int i;
1071
1072 if (report > REPORT_TYPE_RFREPORT_LAST) {
Colin Ian King640d4ea2019-04-26 14:16:31 +01001073 hid_err(hdev, "Unexpected input report number %d\n", report);
Hans de Goede74808f92019-04-20 13:21:54 +02001074 return;
1075 }
1076
1077 spin_lock_irqsave(&djrcv_dev->lock, flags);
1078 for (i = 0; i < (DJ_MAX_PAIRED_DEVICES + DJ_DEVICE_INDEX_MIN); i++) {
1079 dj_dev = djrcv_dev->paired_dj_devices[i];
1080 if (dj_dev && (dj_dev->reports_supported & BIT(report))) {
1081 logi_dj_recv_forward_report(dj_dev, data, size);
1082 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
1083 return;
1084 }
1085 }
1086
1087 logi_dj_recv_queue_unknown_work(djrcv_dev);
1088 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
1089
1090 dbg_hid("No dj-devs handling input report number %d\n", report);
1091}
1092
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001093static int logi_dj_recv_send_report(struct dj_receiver_dev *djrcv_dev,
1094 struct dj_report *dj_report)
1095{
Hans de Goede0ee75542019-04-20 13:21:51 +02001096 struct hid_device *hdev = djrcv_dev->hidpp;
Benjamin Tissoiresdcd90062013-03-05 17:09:00 +01001097 struct hid_report *report;
1098 struct hid_report_enum *output_report_enum;
1099 u8 *data = (u8 *)(&dj_report->device_index);
Kees Cook297502a2013-09-11 21:56:56 +02001100 unsigned int i;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001101
Benjamin Tissoiresdcd90062013-03-05 17:09:00 +01001102 output_report_enum = &hdev->report_enum[HID_OUTPUT_REPORT];
1103 report = output_report_enum->report_id_hash[REPORT_ID_DJ_SHORT];
1104
1105 if (!report) {
Hans de Goedeaca22a32019-04-20 13:21:58 +02001106 hid_err(hdev, "%s: unable to find dj report\n", __func__);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001107 return -ENODEV;
1108 }
1109
Kees Cook297502a2013-09-11 21:56:56 +02001110 for (i = 0; i < DJREPORT_SHORT_LENGTH - 1; i++)
Benjamin Tissoiresdcd90062013-03-05 17:09:00 +01001111 report->field[0]->value[i] = data[i];
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001112
Jiri Kosina83a44ac2013-03-09 10:58:13 +01001113 hid_hw_request(hdev, report, HID_REQ_SET_REPORT);
Benjamin Tissoiresdcd90062013-03-05 17:09:00 +01001114
1115 return 0;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001116}
1117
Hans de Goede74808f92019-04-20 13:21:54 +02001118static int logi_dj_recv_query_hidpp_devices(struct dj_receiver_dev *djrcv_dev)
1119{
1120 const u8 template[] = {REPORT_ID_HIDPP_SHORT,
1121 HIDPP_RECEIVER_INDEX,
1122 HIDPP_SET_REGISTER,
1123 HIDPP_REG_CONNECTION_STATE,
1124 HIDPP_FAKE_DEVICE_ARRIVAL,
1125 0x00, 0x00};
1126 u8 *hidpp_report;
1127 int retval;
1128
1129 hidpp_report = kmemdup(template, sizeof(template), GFP_KERNEL);
1130 if (!hidpp_report)
1131 return -ENOMEM;
1132
1133 retval = hid_hw_raw_request(djrcv_dev->hidpp,
1134 REPORT_ID_HIDPP_SHORT,
1135 hidpp_report, sizeof(template),
1136 HID_OUTPUT_REPORT,
1137 HID_REQ_SET_REPORT);
1138
1139 kfree(hidpp_report);
1140 return 0;
1141}
1142
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001143static int logi_dj_recv_query_paired_devices(struct dj_receiver_dev *djrcv_dev)
1144{
Marc Dionned8dc3492012-06-01 18:12:14 -04001145 struct dj_report *dj_report;
1146 int retval;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001147
Hans de Goedeb6aeedd2019-04-20 13:21:53 +02001148 djrcv_dev->last_query = jiffies;
1149
Hans de Goede74808f92019-04-20 13:21:54 +02001150 if (djrcv_dev->type != recvr_type_dj)
1151 return logi_dj_recv_query_hidpp_devices(djrcv_dev);
1152
Alan Cox8a55ade2012-09-04 15:10:08 +01001153 dj_report = kzalloc(sizeof(struct dj_report), GFP_KERNEL);
Marc Dionned8dc3492012-06-01 18:12:14 -04001154 if (!dj_report)
1155 return -ENOMEM;
1156 dj_report->report_id = REPORT_ID_DJ_SHORT;
1157 dj_report->device_index = 0xFF;
1158 dj_report->report_type = REPORT_TYPE_CMD_GET_PAIRED_DEVICES;
1159 retval = logi_dj_recv_send_report(djrcv_dev, dj_report);
1160 kfree(dj_report);
1161 return retval;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001162}
1163
Nestor Lopez Casadoc63e0e32013-07-18 06:21:30 -07001164
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001165static int logi_dj_recv_switch_to_dj_mode(struct dj_receiver_dev *djrcv_dev,
1166 unsigned timeout)
1167{
Hans de Goede0ee75542019-04-20 13:21:51 +02001168 struct hid_device *hdev = djrcv_dev->hidpp;
Marc Dionned8dc3492012-06-01 18:12:14 -04001169 struct dj_report *dj_report;
Benjamin Tissoires6a9ddc82014-09-30 13:18:31 -04001170 u8 *buf;
Hans de Goede74808f92019-04-20 13:21:54 +02001171 int retval = 0;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001172
Alan Cox8a55ade2012-09-04 15:10:08 +01001173 dj_report = kzalloc(sizeof(struct dj_report), GFP_KERNEL);
Marc Dionned8dc3492012-06-01 18:12:14 -04001174 if (!dj_report)
1175 return -ENOMEM;
Benjamin Tisssoires42c22db2014-01-08 17:18:45 -05001176
Hans de Goede74808f92019-04-20 13:21:54 +02001177 if (djrcv_dev->type == recvr_type_dj) {
1178 dj_report->report_id = REPORT_ID_DJ_SHORT;
1179 dj_report->device_index = 0xFF;
1180 dj_report->report_type = REPORT_TYPE_CMD_SWITCH;
1181 dj_report->report_params[CMD_SWITCH_PARAM_DEVBITFIELD] = 0x3F;
1182 dj_report->report_params[CMD_SWITCH_PARAM_TIMEOUT_SECONDS] =
1183 (u8)timeout;
1184
1185 retval = logi_dj_recv_send_report(djrcv_dev, dj_report);
1186
1187 /*
1188 * Ugly sleep to work around a USB 3.0 bug when the receiver is
1189 * still processing the "switch-to-dj" command while we send an
1190 * other command.
1191 * 50 msec should gives enough time to the receiver to be ready.
1192 */
1193 msleep(50);
1194 }
Benjamin Tisssoires42c22db2014-01-08 17:18:45 -05001195
Benjamin Tissoires6a9ddc82014-09-30 13:18:31 -04001196 /*
1197 * Magical bits to set up hidpp notifications when the dj devices
1198 * are connected/disconnected.
1199 *
1200 * We can reuse dj_report because HIDPP_REPORT_SHORT_LENGTH is smaller
1201 * than DJREPORT_SHORT_LENGTH.
1202 */
1203 buf = (u8 *)dj_report;
1204
1205 memset(buf, 0, HIDPP_REPORT_SHORT_LENGTH);
1206
1207 buf[0] = REPORT_ID_HIDPP_SHORT;
1208 buf[1] = 0xFF;
1209 buf[2] = 0x80;
1210 buf[3] = 0x00;
1211 buf[4] = 0x00;
1212 buf[5] = 0x09;
1213 buf[6] = 0x00;
1214
1215 hid_hw_raw_request(hdev, REPORT_ID_HIDPP_SHORT, buf,
1216 HIDPP_REPORT_SHORT_LENGTH, HID_OUTPUT_REPORT,
1217 HID_REQ_SET_REPORT);
1218
1219 kfree(dj_report);
Marc Dionned8dc3492012-06-01 18:12:14 -04001220 return retval;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001221}
1222
1223
1224static int logi_dj_ll_open(struct hid_device *hid)
1225{
Hans de Goedeaca22a32019-04-20 13:21:58 +02001226 dbg_hid("%s: %s\n", __func__, hid->phys);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001227 return 0;
1228
1229}
1230
1231static void logi_dj_ll_close(struct hid_device *hid)
1232{
Hans de Goedeaca22a32019-04-20 13:21:58 +02001233 dbg_hid("%s: %s\n", __func__, hid->phys);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001234}
1235
Benjamin Tissoires8dba3022017-03-27 16:59:21 +02001236/*
1237 * Register 0xB5 is "pairing information". It is solely intended for the
1238 * receiver, so do not overwrite the device index.
1239 */
Benjamin Tissoiresc0340412019-04-20 13:21:46 +02001240static u8 unifying_pairing_query[] = { REPORT_ID_HIDPP_SHORT,
1241 HIDPP_RECEIVER_INDEX,
1242 HIDPP_GET_LONG_REGISTER,
1243 HIDPP_REG_PAIRING_INFORMATION };
1244static u8 unifying_pairing_answer[] = { REPORT_ID_HIDPP_LONG,
1245 HIDPP_RECEIVER_INDEX,
1246 HIDPP_GET_LONG_REGISTER,
1247 HIDPP_REG_PAIRING_INFORMATION };
Benjamin Tissoires33797822014-09-30 13:18:30 -04001248
Benjamin Tissoiresbd27e202014-02-10 12:58:53 -05001249static int logi_dj_ll_raw_request(struct hid_device *hid,
1250 unsigned char reportnum, __u8 *buf,
1251 size_t count, unsigned char report_type,
1252 int reqtype)
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001253{
Benjamin Tissoires0e40d352014-02-05 16:33:17 -05001254 struct dj_device *djdev = hid->driver_data;
1255 struct dj_receiver_dev *djrcv_dev = djdev->dj_receiver_dev;
1256 u8 *out_buf;
1257 int ret;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001258
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001259 if ((buf[0] == REPORT_ID_HIDPP_SHORT) ||
1260 (buf[0] == REPORT_ID_HIDPP_LONG)) {
1261 if (count < 2)
1262 return -EINVAL;
1263
Benjamin Tissoires33797822014-09-30 13:18:30 -04001264 /* special case where we should not overwrite
1265 * the device_index */
Benjamin Tissoires8dba3022017-03-27 16:59:21 +02001266 if (count == 7 && !memcmp(buf, unifying_pairing_query,
1267 sizeof(unifying_pairing_query)))
1268 buf[4] = (buf[4] & 0xf0) | (djdev->device_index - 1);
Benjamin Tissoires33797822014-09-30 13:18:30 -04001269 else
1270 buf[1] = djdev->device_index;
Hans de Goede0ee75542019-04-20 13:21:51 +02001271 return hid_hw_raw_request(djrcv_dev->hidpp, reportnum, buf,
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001272 count, report_type, reqtype);
1273 }
1274
Benjamin Tissoires0e40d352014-02-05 16:33:17 -05001275 if (buf[0] != REPORT_TYPE_LEDS)
1276 return -EINVAL;
1277
Hans de Goede74808f92019-04-20 13:21:54 +02001278 if (djrcv_dev->type != recvr_type_dj && count >= 2) {
1279 if (!djrcv_dev->keyboard) {
1280 hid_warn(hid, "Received REPORT_TYPE_LEDS request before the keyboard interface was enumerated\n");
1281 return 0;
1282 }
1283 /* usbhid overrides the report ID and ignores the first byte */
1284 return hid_hw_raw_request(djrcv_dev->keyboard, 0, buf, count,
1285 report_type, reqtype);
1286 }
1287
Benjamin Tissoires0e40d352014-02-05 16:33:17 -05001288 out_buf = kzalloc(DJREPORT_SHORT_LENGTH, GFP_ATOMIC);
1289 if (!out_buf)
1290 return -ENOMEM;
1291
Jiri Kosina51217e62014-08-21 09:56:47 -05001292 if (count > DJREPORT_SHORT_LENGTH - 2)
Benjamin Tissoires0e40d352014-02-05 16:33:17 -05001293 count = DJREPORT_SHORT_LENGTH - 2;
1294
1295 out_buf[0] = REPORT_ID_DJ_SHORT;
1296 out_buf[1] = djdev->device_index;
1297 memcpy(out_buf + 2, buf, count);
1298
Hans de Goede0ee75542019-04-20 13:21:51 +02001299 ret = hid_hw_raw_request(djrcv_dev->hidpp, out_buf[0], out_buf,
Benjamin Tissoiresbd27e202014-02-10 12:58:53 -05001300 DJREPORT_SHORT_LENGTH, report_type, reqtype);
Benjamin Tissoires0e40d352014-02-05 16:33:17 -05001301
1302 kfree(out_buf);
1303 return ret;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001304}
1305
Dan Carpenter6d603322013-10-19 12:08:59 +03001306static void rdcat(char *rdesc, unsigned int *rsize, const char *data, unsigned int size)
Henrik Rydberg2a039bf2012-04-22 14:21:39 +02001307{
Dan Carpenter6d603322013-10-19 12:08:59 +03001308 memcpy(rdesc + *rsize, data, size);
Henrik Rydberg2a039bf2012-04-22 14:21:39 +02001309 *rsize += size;
1310}
1311
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001312static int logi_dj_ll_parse(struct hid_device *hid)
1313{
1314 struct dj_device *djdev = hid->driver_data;
Henrik Rydberg2a039bf2012-04-22 14:21:39 +02001315 unsigned int rsize = 0;
1316 char *rdesc;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001317 int retval;
1318
1319 dbg_hid("%s\n", __func__);
1320
1321 djdev->hdev->version = 0x0111;
1322 djdev->hdev->country = 0x00;
1323
Henrik Rydberg2a039bf2012-04-22 14:21:39 +02001324 rdesc = kmalloc(MAX_RDESC_SIZE, GFP_KERNEL);
1325 if (!rdesc)
1326 return -ENOMEM;
1327
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001328 if (djdev->reports_supported & STD_KEYBOARD) {
Hans de Goede6d3c3f02019-04-20 13:22:02 +02001329 dbg_hid("%s: sending a kbd descriptor, reports_supported: %llx\n",
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001330 __func__, djdev->reports_supported);
Dan Carpenter6d603322013-10-19 12:08:59 +03001331 rdcat(rdesc, &rsize, kbd_descriptor, sizeof(kbd_descriptor));
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001332 }
1333
1334 if (djdev->reports_supported & STD_MOUSE) {
Hans de Goede6d3c3f02019-04-20 13:22:02 +02001335 dbg_hid("%s: sending a mouse descriptor, reports_supported: %llx\n",
1336 __func__, djdev->reports_supported);
Hans de Goede3ed224e2019-06-05 14:44:08 +02001337 if (djdev->dj_receiver_dev->type == recvr_type_gaming_hidpp ||
1338 djdev->dj_receiver_dev->type == recvr_type_mouse_only)
Benjamin Tissoiresf5fb57a2019-04-20 13:21:55 +02001339 rdcat(rdesc, &rsize, mse_high_res_descriptor,
1340 sizeof(mse_high_res_descriptor));
Hans de Goedec9121cf2019-04-20 13:21:56 +02001341 else if (djdev->dj_receiver_dev->type == recvr_type_27mhz)
1342 rdcat(rdesc, &rsize, mse_27mhz_descriptor,
1343 sizeof(mse_27mhz_descriptor));
Hans de Goedef2113c32019-04-20 13:22:03 +02001344 else if (djdev->dj_receiver_dev->type == recvr_type_bluetooth)
1345 rdcat(rdesc, &rsize, mse_bluetooth_descriptor,
1346 sizeof(mse_bluetooth_descriptor));
Benjamin Tissoiresf5fb57a2019-04-20 13:21:55 +02001347 else
1348 rdcat(rdesc, &rsize, mse_descriptor,
1349 sizeof(mse_descriptor));
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001350 }
1351
1352 if (djdev->reports_supported & MULTIMEDIA) {
Hans de Goede6d3c3f02019-04-20 13:22:02 +02001353 dbg_hid("%s: sending a multimedia report descriptor: %llx\n",
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001354 __func__, djdev->reports_supported);
Dan Carpenter6d603322013-10-19 12:08:59 +03001355 rdcat(rdesc, &rsize, consumer_descriptor, sizeof(consumer_descriptor));
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001356 }
1357
1358 if (djdev->reports_supported & POWER_KEYS) {
Hans de Goede6d3c3f02019-04-20 13:22:02 +02001359 dbg_hid("%s: sending a power keys report descriptor: %llx\n",
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001360 __func__, djdev->reports_supported);
Dan Carpenter6d603322013-10-19 12:08:59 +03001361 rdcat(rdesc, &rsize, syscontrol_descriptor, sizeof(syscontrol_descriptor));
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001362 }
1363
1364 if (djdev->reports_supported & MEDIA_CENTER) {
Hans de Goede6d3c3f02019-04-20 13:22:02 +02001365 dbg_hid("%s: sending a media center report descriptor: %llx\n",
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001366 __func__, djdev->reports_supported);
Dan Carpenter6d603322013-10-19 12:08:59 +03001367 rdcat(rdesc, &rsize, media_descriptor, sizeof(media_descriptor));
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001368 }
1369
1370 if (djdev->reports_supported & KBD_LEDS) {
Hans de Goede6d3c3f02019-04-20 13:22:02 +02001371 dbg_hid("%s: need to send kbd leds report descriptor: %llx\n",
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001372 __func__, djdev->reports_supported);
1373 }
1374
Hans de Goede6d3c3f02019-04-20 13:22:02 +02001375 if (djdev->reports_supported & HIDPP) {
1376 rdcat(rdesc, &rsize, hidpp_descriptor,
1377 sizeof(hidpp_descriptor));
1378 }
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001379
Henrik Rydberg2a039bf2012-04-22 14:21:39 +02001380 retval = hid_parse_report(hid, rdesc, rsize);
1381 kfree(rdesc);
1382
1383 return retval;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001384}
1385
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001386static int logi_dj_ll_start(struct hid_device *hid)
1387{
1388 dbg_hid("%s\n", __func__);
1389 return 0;
1390}
1391
1392static void logi_dj_ll_stop(struct hid_device *hid)
1393{
1394 dbg_hid("%s\n", __func__);
1395}
1396
1397
1398static struct hid_ll_driver logi_dj_ll_driver = {
1399 .parse = logi_dj_ll_parse,
1400 .start = logi_dj_ll_start,
1401 .stop = logi_dj_ll_stop,
1402 .open = logi_dj_ll_open,
1403 .close = logi_dj_ll_close,
Benjamin Tissoiresbd27e202014-02-10 12:58:53 -05001404 .raw_request = logi_dj_ll_raw_request,
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001405};
1406
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001407static int logi_dj_dj_event(struct hid_device *hdev,
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001408 struct hid_report *report, u8 *data,
1409 int size)
1410{
1411 struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
1412 struct dj_report *dj_report = (struct dj_report *) data;
1413 unsigned long flags;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001414
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001415 /*
1416 * Here we receive all data coming from iface 2, there are 3 cases:
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001417 *
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001418 * 1) Data is intended for this driver i. e. data contains arrival,
1419 * departure, etc notifications, in which case we queue them for delayed
1420 * processing by the work queue. We return 1 to hid-core as no further
1421 * processing is required from it.
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001422 *
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001423 * 2) Data informs a connection change, if the change means rf link
1424 * loss, then we must send a null report to the upper layer to discard
1425 * potentially pressed keys that may be repeated forever by the input
1426 * layer. Return 1 to hid-core as no further processing is required.
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001427 *
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001428 * 3) Data is an actual input event from a paired DJ device in which
1429 * case we forward it to the correct hid device (via hid_input_report()
1430 * ) and return 1 so hid-core does not anything else with it.
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001431 */
Benjamin Tissoires5abfe852014-08-22 16:16:05 -04001432
Jiri Kosinaad3e14d2014-08-21 09:57:17 -05001433 if ((dj_report->device_index < DJ_DEVICE_INDEX_MIN) ||
1434 (dj_report->device_index > DJ_DEVICE_INDEX_MAX)) {
Benjamin Tissoires5abfe852014-08-22 16:16:05 -04001435 /*
1436 * Device index is wrong, bail out.
1437 * This driver can ignore safely the receiver notifications,
1438 * so ignore those reports too.
1439 */
1440 if (dj_report->device_index != DJ_RECEIVER_INDEX)
Hans de Goedeaca22a32019-04-20 13:21:58 +02001441 hid_err(hdev, "%s: invalid device index:%d\n",
Jiri Kosinaad3e14d2014-08-21 09:57:17 -05001442 __func__, dj_report->device_index);
1443 return false;
1444 }
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001445
1446 spin_lock_irqsave(&djrcv_dev->lock, flags);
Benjamin Tissoires368d4e592014-08-22 16:16:06 -04001447
1448 if (!djrcv_dev->paired_dj_devices[dj_report->device_index]) {
1449 /* received an event for an unknown device, bail out */
1450 logi_dj_recv_queue_notification(djrcv_dev, dj_report);
1451 goto out;
1452 }
1453
Benjamin Tissoires5abfe852014-08-22 16:16:05 -04001454 switch (dj_report->report_type) {
1455 case REPORT_TYPE_NOTIF_DEVICE_PAIRED:
Benjamin Tissoires368d4e592014-08-22 16:16:06 -04001456 /* pairing notifications are handled above the switch */
1457 break;
Benjamin Tissoires5abfe852014-08-22 16:16:05 -04001458 case REPORT_TYPE_NOTIF_DEVICE_UNPAIRED:
1459 logi_dj_recv_queue_notification(djrcv_dev, dj_report);
1460 break;
1461 case REPORT_TYPE_NOTIF_CONNECTION_STATUS:
1462 if (dj_report->report_params[CONNECTION_STATUS_PARAM_STATUS] ==
1463 STATUS_LINKLOSS) {
1464 logi_dj_recv_forward_null_report(djrcv_dev, dj_report);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001465 }
Benjamin Tissoires5abfe852014-08-22 16:16:05 -04001466 break;
1467 default:
Benjamin Tissoires83898232019-04-20 13:21:43 +02001468 logi_dj_recv_forward_dj(djrcv_dev, dj_report);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001469 }
Benjamin Tissoires368d4e592014-08-22 16:16:06 -04001470
1471out:
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001472 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
1473
Benjamin Tissoires5abfe852014-08-22 16:16:05 -04001474 return true;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001475}
1476
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001477static int logi_dj_hidpp_event(struct hid_device *hdev,
1478 struct hid_report *report, u8 *data,
1479 int size)
1480{
1481 struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
Benjamin Tissoires7bb56a52019-04-20 13:21:44 +02001482 struct hidpp_event *hidpp_report = (struct hidpp_event *) data;
Hans de Goede74808f92019-04-20 13:21:54 +02001483 struct dj_device *dj_dev;
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001484 unsigned long flags;
Benjamin Tissoires7bb56a52019-04-20 13:21:44 +02001485 u8 device_index = hidpp_report->device_index;
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001486
Benjamin Tissoires33797822014-09-30 13:18:30 -04001487 if (device_index == HIDPP_RECEIVER_INDEX) {
1488 /* special case were the device wants to know its unifying
1489 * name */
1490 if (size == HIDPP_REPORT_LONG_LENGTH &&
Benjamin Tissoires8dba3022017-03-27 16:59:21 +02001491 !memcmp(data, unifying_pairing_answer,
1492 sizeof(unifying_pairing_answer)))
Benjamin Tissoires33797822014-09-30 13:18:30 -04001493 device_index = (data[4] & 0x0F) + 1;
1494 else
1495 return false;
1496 }
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001497
1498 /*
1499 * Data is from the HID++ collection, in this case, we forward the
1500 * data to the corresponding child dj device and return 0 to hid-core
1501 * so he data also goes to the hidraw device of the receiver. This
1502 * allows a user space application to implement the full HID++ routing
1503 * via the receiver.
1504 */
1505
1506 if ((device_index < DJ_DEVICE_INDEX_MIN) ||
1507 (device_index > DJ_DEVICE_INDEX_MAX)) {
1508 /*
1509 * Device index is wrong, bail out.
1510 * This driver can ignore safely the receiver notifications,
1511 * so ignore those reports too.
1512 */
Hans de Goedeaca22a32019-04-20 13:21:58 +02001513 hid_err(hdev, "%s: invalid device index:%d\n", __func__,
1514 hidpp_report->device_index);
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001515 return false;
1516 }
1517
1518 spin_lock_irqsave(&djrcv_dev->lock, flags);
1519
Hans de Goede74808f92019-04-20 13:21:54 +02001520 dj_dev = djrcv_dev->paired_dj_devices[device_index];
Hans de Goedec9121cf2019-04-20 13:21:56 +02001521
1522 /*
1523 * With 27 MHz receivers, we do not get an explicit unpair event,
1524 * remove the old device if the user has paired a *different* device.
1525 */
1526 if (djrcv_dev->type == recvr_type_27mhz && dj_dev &&
1527 hidpp_report->sub_id == REPORT_TYPE_NOTIF_DEVICE_CONNECTED &&
1528 hidpp_report->params[HIDPP_PARAM_PROTO_TYPE] == 0x02 &&
1529 hidpp_report->params[HIDPP_PARAM_27MHZ_DEVID] !=
1530 dj_dev->hdev->product) {
1531 struct dj_workitem workitem = {
1532 .device_index = hidpp_report->device_index,
1533 .type = WORKITEM_TYPE_UNPAIRED,
1534 };
1535 kfifo_in(&djrcv_dev->notif_fifo, &workitem, sizeof(workitem));
1536 /* logi_hidpp_recv_queue_notif will queue the work */
1537 dj_dev = NULL;
1538 }
1539
Hans de Goede74808f92019-04-20 13:21:54 +02001540 if (dj_dev) {
1541 logi_dj_recv_forward_report(dj_dev, data, size);
1542 } else {
1543 if (hidpp_report->sub_id == REPORT_TYPE_NOTIF_DEVICE_CONNECTED)
1544 logi_hidpp_recv_queue_notif(hdev, hidpp_report);
1545 else
1546 logi_dj_recv_queue_unknown_work(djrcv_dev);
1547 }
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001548
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001549 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
1550
1551 return false;
1552}
1553
1554static int logi_dj_raw_event(struct hid_device *hdev,
1555 struct hid_report *report, u8 *data,
1556 int size)
1557{
Hans de Goede74808f92019-04-20 13:21:54 +02001558 struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001559 dbg_hid("%s, size:%d\n", __func__, size);
1560
Hans de Goededa12b222019-04-20 13:21:59 +02001561 if (!djrcv_dev)
1562 return 0;
1563
Hans de Goede74808f92019-04-20 13:21:54 +02001564 if (!hdev->report_enum[HID_INPUT_REPORT].numbered) {
1565
1566 if (djrcv_dev->unnumbered_application == HID_GD_KEYBOARD) {
1567 /*
1568 * For the keyboard, we can reuse the same report by
1569 * using the second byte which is constant in the USB
1570 * HID report descriptor.
1571 */
1572 data[1] = data[0];
1573 data[0] = REPORT_TYPE_KEYBOARD;
1574
1575 logi_dj_recv_forward_input_report(hdev, data, size);
1576
1577 /* restore previous state */
1578 data[0] = data[1];
1579 data[1] = 0;
1580 }
Hans de Goede3ed224e2019-06-05 14:44:08 +02001581 /*
1582 * Mouse-only receivers send unnumbered mouse data. The 27 MHz
1583 * receiver uses 6 byte packets, the nano receiver 8 bytes.
1584 */
Hans de Goede1f944ac2019-04-20 13:21:57 +02001585 if (djrcv_dev->unnumbered_application == HID_GD_MOUSE &&
Hans de Goede3ed224e2019-06-05 14:44:08 +02001586 size <= 8) {
1587 u8 mouse_report[9];
Hans de Goede1f944ac2019-04-20 13:21:57 +02001588
1589 /* Prepend report id */
1590 mouse_report[0] = REPORT_TYPE_MOUSE;
Hans de Goede3ed224e2019-06-05 14:44:08 +02001591 memcpy(mouse_report + 1, data, size);
1592 logi_dj_recv_forward_input_report(hdev, mouse_report,
1593 size + 1);
Hans de Goede1f944ac2019-04-20 13:21:57 +02001594 }
Hans de Goede74808f92019-04-20 13:21:54 +02001595
1596 return false;
1597 }
1598
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001599 switch (data[0]) {
1600 case REPORT_ID_DJ_SHORT:
Peter Wuf254ae92014-12-16 16:55:21 +01001601 if (size != DJREPORT_SHORT_LENGTH) {
Hans de Goedeaca22a32019-04-20 13:21:58 +02001602 hid_err(hdev, "Short DJ report bad size (%d)", size);
Peter Wuf254ae92014-12-16 16:55:21 +01001603 return false;
1604 }
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001605 return logi_dj_dj_event(hdev, report, data, size);
Benjamin Tissoiresf5fb57a2019-04-20 13:21:55 +02001606 case REPORT_ID_DJ_LONG:
1607 if (size != DJREPORT_LONG_LENGTH) {
Hans de Goedeaca22a32019-04-20 13:21:58 +02001608 hid_err(hdev, "Long DJ report bad size (%d)", size);
Benjamin Tissoiresf5fb57a2019-04-20 13:21:55 +02001609 return false;
1610 }
1611 return logi_dj_dj_event(hdev, report, data, size);
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001612 case REPORT_ID_HIDPP_SHORT:
Peter Wuf254ae92014-12-16 16:55:21 +01001613 if (size != HIDPP_REPORT_SHORT_LENGTH) {
Hans de Goedeaca22a32019-04-20 13:21:58 +02001614 hid_err(hdev, "Short HID++ report bad size (%d)", size);
Peter Wuf254ae92014-12-16 16:55:21 +01001615 return false;
1616 }
1617 return logi_dj_hidpp_event(hdev, report, data, size);
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001618 case REPORT_ID_HIDPP_LONG:
Peter Wuf254ae92014-12-16 16:55:21 +01001619 if (size != HIDPP_REPORT_LONG_LENGTH) {
Hans de Goedeaca22a32019-04-20 13:21:58 +02001620 hid_err(hdev, "Long HID++ report bad size (%d)", size);
Peter Wuf254ae92014-12-16 16:55:21 +01001621 return false;
1622 }
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001623 return logi_dj_hidpp_event(hdev, report, data, size);
1624 }
1625
Hans de Goede74808f92019-04-20 13:21:54 +02001626 logi_dj_recv_forward_input_report(hdev, data, size);
1627
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001628 return false;
1629}
1630
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001631static int logi_dj_probe(struct hid_device *hdev,
1632 const struct hid_device_id *id)
1633{
Benjamin Tissoires82c0beb2019-04-20 13:21:47 +02001634 struct hid_report_enum *rep_enum;
1635 struct hid_report *rep;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001636 struct dj_receiver_dev *djrcv_dev;
Hans de Goededa12b222019-04-20 13:21:59 +02001637 struct usb_interface *intf;
1638 unsigned int no_dj_interfaces = 0;
Benjamin Tissoires82c0beb2019-04-20 13:21:47 +02001639 bool has_hidpp = false;
Hans de Goedea1d97cc2019-04-20 13:21:52 +02001640 unsigned long flags;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001641 int retval;
1642
Benjamin Tissoires82c0beb2019-04-20 13:21:47 +02001643 /*
1644 * Call to usbhid to fetch the HID descriptors of the current
1645 * interface subsequently call to the hid/hid-core to parse the
1646 * fetched descriptors.
1647 */
1648 retval = hid_parse(hdev);
1649 if (retval) {
Hans de Goedeaca22a32019-04-20 13:21:58 +02001650 hid_err(hdev, "%s: parse failed\n", __func__);
Benjamin Tissoires82c0beb2019-04-20 13:21:47 +02001651 return retval;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001652 }
1653
Hans de Goededa12b222019-04-20 13:21:59 +02001654 /*
1655 * Some KVMs add an extra interface for e.g. mouse emulation. If we
1656 * treat these as logitech-dj interfaces then this causes input events
1657 * reported through this extra interface to not be reported correctly.
1658 * To avoid this, we treat these as generic-hid devices.
1659 */
1660 switch (id->driver_data) {
1661 case recvr_type_dj: no_dj_interfaces = 3; break;
1662 case recvr_type_hidpp: no_dj_interfaces = 2; break;
1663 case recvr_type_gaming_hidpp: no_dj_interfaces = 3; break;
Hans de Goede3ed224e2019-06-05 14:44:08 +02001664 case recvr_type_mouse_only: no_dj_interfaces = 2; break;
Hans de Goededa12b222019-04-20 13:21:59 +02001665 case recvr_type_27mhz: no_dj_interfaces = 2; break;
Hans de Goedef2113c32019-04-20 13:22:03 +02001666 case recvr_type_bluetooth: no_dj_interfaces = 2; break;
Hans de Goededa12b222019-04-20 13:21:59 +02001667 }
1668 if (hid_is_using_ll_driver(hdev, &usb_hid_driver)) {
1669 intf = to_usb_interface(hdev->dev.parent);
1670 if (intf && intf->altsetting->desc.bInterfaceNumber >=
1671 no_dj_interfaces) {
1672 hdev->quirks |= HID_QUIRK_INPUT_PER_APP;
1673 return hid_hw_start(hdev, HID_CONNECT_DEFAULT);
1674 }
1675 }
1676
Benjamin Tissoires82c0beb2019-04-20 13:21:47 +02001677 rep_enum = &hdev->report_enum[HID_INPUT_REPORT];
1678
Hans de Goede74808f92019-04-20 13:21:54 +02001679 /* no input reports, bail out */
1680 if (list_empty(&rep_enum->report_list))
1681 return -ENODEV;
1682
Benjamin Tissoires82c0beb2019-04-20 13:21:47 +02001683 /*
1684 * Check for the HID++ application.
1685 * Note: we should theoretically check for HID++ and DJ
1686 * collections, but this will do.
1687 */
1688 list_for_each_entry(rep, &rep_enum->report_list, list) {
1689 if (rep->application == 0xff000001)
1690 has_hidpp = true;
1691 }
1692
1693 /*
1694 * Ignore interfaces without DJ/HID++ collection, they will not carry
1695 * any data, dont create any hid_device for them.
1696 */
Hans de Goede74808f92019-04-20 13:21:54 +02001697 if (!has_hidpp && id->driver_data == recvr_type_dj)
Benjamin Tissoires82c0beb2019-04-20 13:21:47 +02001698 return -ENODEV;
1699
Hans de Goedea1d97cc2019-04-20 13:21:52 +02001700 /* get the current application attached to the node */
1701 rep = list_first_entry(&rep_enum->report_list, struct hid_report, list);
Hans de Goede74808f92019-04-20 13:21:54 +02001702 djrcv_dev = dj_get_receiver_dev(hdev, id->driver_data,
Hans de Goedea1d97cc2019-04-20 13:21:52 +02001703 rep->application, has_hidpp);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001704 if (!djrcv_dev) {
Hans de Goedeaca22a32019-04-20 13:21:58 +02001705 hid_err(hdev, "%s: dj_get_receiver_dev failed\n", __func__);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001706 return -ENOMEM;
1707 }
Kees Cook297502a2013-09-11 21:56:56 +02001708
Hans de Goede74808f92019-04-20 13:21:54 +02001709 if (!rep_enum->numbered)
1710 djrcv_dev->unnumbered_application = rep->application;
1711
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001712 /* Starts the usb device and connects to upper interfaces hiddev and
1713 * hidraw */
Hans de Goede74808f92019-04-20 13:21:54 +02001714 retval = hid_hw_start(hdev, HID_CONNECT_HIDRAW|HID_CONNECT_HIDDEV);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001715 if (retval) {
Hans de Goedeaca22a32019-04-20 13:21:58 +02001716 hid_err(hdev, "%s: hid_hw_start returned error\n", __func__);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001717 goto hid_hw_start_fail;
1718 }
1719
Hans de Goede74808f92019-04-20 13:21:54 +02001720 if (has_hidpp) {
1721 retval = logi_dj_recv_switch_to_dj_mode(djrcv_dev, 0);
1722 if (retval < 0) {
1723 hid_err(hdev, "%s: logi_dj_recv_switch_to_dj_mode returned error:%d\n",
1724 __func__, retval);
1725 goto switch_to_dj_mode_fail;
1726 }
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001727 }
1728
1729 /* This is enabling the polling urb on the IN endpoint */
Benjamin Tissoiresddf75402013-07-12 11:01:02 +02001730 retval = hid_hw_open(hdev);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001731 if (retval < 0) {
Hans de Goedeaca22a32019-04-20 13:21:58 +02001732 hid_err(hdev, "%s: hid_hw_open returned error:%d\n",
Benjamin Tissoiresddf75402013-07-12 11:01:02 +02001733 __func__, retval);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001734 goto llopen_failed;
1735 }
1736
Andrew de los Reyesa9dd22b72013-02-18 09:20:22 -08001737 /* Allow incoming packets to arrive: */
1738 hid_device_io_start(hdev);
1739
Hans de Goede74808f92019-04-20 13:21:54 +02001740 if (has_hidpp) {
1741 spin_lock_irqsave(&djrcv_dev->lock, flags);
1742 djrcv_dev->ready = true;
1743 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
1744 retval = logi_dj_recv_query_paired_devices(djrcv_dev);
1745 if (retval < 0) {
1746 hid_err(hdev, "%s: logi_dj_recv_query_paired_devices error:%d\n",
1747 __func__, retval);
1748 goto logi_dj_recv_query_paired_devices_failed;
1749 }
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001750 }
1751
1752 return retval;
1753
1754logi_dj_recv_query_paired_devices_failed:
Benjamin Tissoiresddf75402013-07-12 11:01:02 +02001755 hid_hw_close(hdev);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001756
1757llopen_failed:
1758switch_to_dj_mode_fail:
1759 hid_hw_stop(hdev);
1760
1761hid_hw_start_fail:
Hans de Goedea1d97cc2019-04-20 13:21:52 +02001762 dj_put_receiver_dev(hdev);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001763 return retval;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001764}
1765
1766#ifdef CONFIG_PM
1767static int logi_dj_reset_resume(struct hid_device *hdev)
1768{
1769 int retval;
1770 struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
1771
Hans de Goededa12b222019-04-20 13:21:59 +02001772 if (!djrcv_dev || djrcv_dev->hidpp != hdev)
Hans de Goede74808f92019-04-20 13:21:54 +02001773 return 0;
1774
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001775 retval = logi_dj_recv_switch_to_dj_mode(djrcv_dev, 0);
1776 if (retval < 0) {
Hans de Goedeaca22a32019-04-20 13:21:58 +02001777 hid_err(hdev, "%s: logi_dj_recv_switch_to_dj_mode returned error:%d\n",
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001778 __func__, retval);
1779 }
1780
1781 return 0;
1782}
1783#endif
1784
1785static void logi_dj_remove(struct hid_device *hdev)
1786{
1787 struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
1788 struct dj_device *dj_dev;
Hans de Goedea1d97cc2019-04-20 13:21:52 +02001789 unsigned long flags;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001790 int i;
1791
1792 dbg_hid("%s\n", __func__);
1793
Hans de Goededa12b222019-04-20 13:21:59 +02001794 if (!djrcv_dev)
1795 return hid_hw_stop(hdev);
1796
Hans de Goedea1d97cc2019-04-20 13:21:52 +02001797 /*
1798 * This ensures that if the work gets requeued from another
1799 * interface of the same receiver it will be a no-op.
1800 */
1801 spin_lock_irqsave(&djrcv_dev->lock, flags);
1802 djrcv_dev->ready = false;
1803 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
1804
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001805 cancel_work_sync(&djrcv_dev->work);
1806
Benjamin Tissoiresddf75402013-07-12 11:01:02 +02001807 hid_hw_close(hdev);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001808 hid_hw_stop(hdev);
1809
Hans de Goedea1d97cc2019-04-20 13:21:52 +02001810 /*
1811 * For proper operation we need access to all interfaces, so we destroy
1812 * the paired devices when we're unbound from any interface.
1813 *
1814 * Note we may still be bound to other interfaces, sharing the same
1815 * djrcv_dev, so we need locking here.
1816 */
Nestor Lopez Casado844580f2011-09-20 15:59:03 +02001817 for (i = 0; i < (DJ_MAX_PAIRED_DEVICES + DJ_DEVICE_INDEX_MIN); i++) {
Hans de Goedea1d97cc2019-04-20 13:21:52 +02001818 spin_lock_irqsave(&djrcv_dev->lock, flags);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001819 dj_dev = djrcv_dev->paired_dj_devices[i];
Hans de Goedea1d97cc2019-04-20 13:21:52 +02001820 djrcv_dev->paired_dj_devices[i] = NULL;
1821 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001822 if (dj_dev != NULL) {
1823 hid_destroy_device(dj_dev->hdev);
1824 kfree(dj_dev);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001825 }
1826 }
1827
Hans de Goedea1d97cc2019-04-20 13:21:52 +02001828 dj_put_receiver_dev(hdev);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001829}
1830
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001831static const struct hid_device_id logi_dj_receivers[] = {
1832 {HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
Hans de Goede74808f92019-04-20 13:21:54 +02001833 USB_DEVICE_ID_LOGITECH_UNIFYING_RECEIVER),
1834 .driver_data = recvr_type_dj},
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001835 {HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
Hans de Goede74808f92019-04-20 13:21:54 +02001836 USB_DEVICE_ID_LOGITECH_UNIFYING_RECEIVER_2),
1837 .driver_data = recvr_type_dj},
Hans de Goede3ed224e2019-06-05 14:44:08 +02001838 { /* Logitech Nano mouse only receiver */
Hans de Goede74808f92019-04-20 13:21:54 +02001839 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
1840 USB_DEVICE_ID_LOGITECH_NANO_RECEIVER),
Hans de Goede3ed224e2019-06-05 14:44:08 +02001841 .driver_data = recvr_type_mouse_only},
Hans de Goede74808f92019-04-20 13:21:54 +02001842 { /* Logitech Nano (non DJ) receiver */
1843 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
1844 USB_DEVICE_ID_LOGITECH_NANO_RECEIVER_2),
1845 .driver_data = recvr_type_hidpp},
Benjamin Tissoiresf5fb57a2019-04-20 13:21:55 +02001846 { /* Logitech gaming receiver (0xc539) */
1847 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
1848 USB_DEVICE_ID_LOGITECH_NANO_RECEIVER_GAMING),
1849 .driver_data = recvr_type_gaming_hidpp},
Hans de Goedec9121cf2019-04-20 13:21:56 +02001850 { /* Logitech 27 MHz HID++ 1.0 receiver (0xc517) */
1851 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
1852 USB_DEVICE_ID_S510_RECEIVER_2),
1853 .driver_data = recvr_type_27mhz},
Hans de Goede1f944ac2019-04-20 13:21:57 +02001854 { /* Logitech 27 MHz HID++ 1.0 mouse-only receiver (0xc51b) */
1855 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
1856 USB_DEVICE_ID_LOGITECH_27MHZ_MOUSE_RECEIVER),
1857 .driver_data = recvr_type_27mhz},
Hans de Goedef2113c32019-04-20 13:22:03 +02001858 { /* Logitech MX5000 HID++ / bluetooth receiver keyboard intf. */
1859 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
1860 0xc70e),
1861 .driver_data = recvr_type_bluetooth},
1862 { /* Logitech MX5000 HID++ / bluetooth receiver mouse intf. */
1863 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
1864 0xc70a),
1865 .driver_data = recvr_type_bluetooth},
Hans de Goedeb9a94fb2019-04-28 21:25:51 +02001866 { /* Logitech MX5500 HID++ / bluetooth receiver keyboard intf. */
1867 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
1868 0xc71b),
1869 .driver_data = recvr_type_bluetooth},
1870 { /* Logitech MX5500 HID++ / bluetooth receiver mouse intf. */
1871 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
1872 0xc71c),
1873 .driver_data = recvr_type_bluetooth},
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001874 {}
1875};
1876
1877MODULE_DEVICE_TABLE(hid, logi_dj_receivers);
1878
1879static struct hid_driver logi_djreceiver_driver = {
1880 .name = "logitech-djreceiver",
1881 .id_table = logi_dj_receivers,
1882 .probe = logi_dj_probe,
1883 .remove = logi_dj_remove,
1884 .raw_event = logi_dj_raw_event,
1885#ifdef CONFIG_PM
1886 .reset_resume = logi_dj_reset_resume,
1887#endif
1888};
1889
Benjamin Tissoiresab94e562014-09-30 13:18:28 -04001890module_hid_driver(logi_djreceiver_driver);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001891
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001892MODULE_LICENSE("GPL");
1893MODULE_AUTHOR("Logitech");
1894MODULE_AUTHOR("Nestor Lopez Casado");
1895MODULE_AUTHOR("nlopezcasad@logitech.com");