blob: 1cbd898bbdc3b2aecc1e4289534de11241c5aca8 [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 Goedec9121cf2019-04-20 13:21:56 +0200131 recvr_type_27mhz,
Hans de Goedef2113c32019-04-20 13:22:03 +0200132 recvr_type_bluetooth,
Hans de Goede74808f92019-04-20 13:21:54 +0200133};
Benjamin Tissoiresc0340412019-04-20 13:21:46 +0200134
Benjamin Tissoires8cb37462014-09-30 13:18:26 -0400135struct dj_report {
136 u8 report_id;
137 u8 device_index;
138 u8 report_type;
139 u8 report_params[DJREPORT_SHORT_LENGTH - 3];
140};
141
Benjamin Tissoires7bb56a52019-04-20 13:21:44 +0200142struct hidpp_event {
143 u8 report_id;
144 u8 device_index;
145 u8 sub_id;
146 u8 params[HIDPP_REPORT_LONG_LENGTH - 3U];
147} __packed;
148
Benjamin Tissoires8cb37462014-09-30 13:18:26 -0400149struct dj_receiver_dev {
Hans de Goedea1d97cc2019-04-20 13:21:52 +0200150 struct hid_device *mouse;
151 struct hid_device *keyboard;
Hans de Goede0ee75542019-04-20 13:21:51 +0200152 struct hid_device *hidpp;
Benjamin Tissoires8cb37462014-09-30 13:18:26 -0400153 struct dj_device *paired_dj_devices[DJ_MAX_PAIRED_DEVICES +
154 DJ_DEVICE_INDEX_MIN];
Hans de Goedea1d97cc2019-04-20 13:21:52 +0200155 struct list_head list;
156 struct kref kref;
Benjamin Tissoires8cb37462014-09-30 13:18:26 -0400157 struct work_struct work;
158 struct kfifo notif_fifo;
Hans de Goedeb6aeedd2019-04-20 13:21:53 +0200159 unsigned long last_query; /* in jiffies */
Hans de Goedea1d97cc2019-04-20 13:21:52 +0200160 bool ready;
Hans de Goede74808f92019-04-20 13:21:54 +0200161 enum recvr_type type;
162 unsigned int unnumbered_application;
Benjamin Tissoires8cb37462014-09-30 13:18:26 -0400163 spinlock_t lock;
Benjamin Tissoires8cb37462014-09-30 13:18:26 -0400164};
165
166struct dj_device {
167 struct hid_device *hdev;
168 struct dj_receiver_dev *dj_receiver_dev;
Hans de Goede6d3c3f02019-04-20 13:22:02 +0200169 u64 reports_supported;
Benjamin Tissoires8cb37462014-09-30 13:18:26 -0400170 u8 device_index;
171};
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200172
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200173#define WORKITEM_TYPE_EMPTY 0
174#define WORKITEM_TYPE_PAIRED 1
175#define WORKITEM_TYPE_UNPAIRED 2
176#define WORKITEM_TYPE_UNKNOWN 255
177
178struct dj_workitem {
179 u8 type; /* WORKITEM_TYPE_* */
180 u8 device_index;
Hans de Goedede76b1d2019-04-20 13:22:00 +0200181 u8 device_type;
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200182 u8 quad_id_msb;
183 u8 quad_id_lsb;
Hans de Goede6d3c3f02019-04-20 13:22:02 +0200184 u64 reports_supported;
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200185};
186
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200187/* Keyboard descriptor (1) */
188static const char kbd_descriptor[] = {
189 0x05, 0x01, /* USAGE_PAGE (generic Desktop) */
190 0x09, 0x06, /* USAGE (Keyboard) */
191 0xA1, 0x01, /* COLLECTION (Application) */
192 0x85, 0x01, /* REPORT_ID (1) */
193 0x95, 0x08, /* REPORT_COUNT (8) */
194 0x75, 0x01, /* REPORT_SIZE (1) */
195 0x15, 0x00, /* LOGICAL_MINIMUM (0) */
196 0x25, 0x01, /* LOGICAL_MAXIMUM (1) */
197 0x05, 0x07, /* USAGE_PAGE (Keyboard) */
198 0x19, 0xE0, /* USAGE_MINIMUM (Left Control) */
199 0x29, 0xE7, /* USAGE_MAXIMUM (Right GUI) */
200 0x81, 0x02, /* INPUT (Data,Var,Abs) */
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200201 0x95, 0x06, /* REPORT_COUNT (6) */
202 0x75, 0x08, /* REPORT_SIZE (8) */
203 0x15, 0x00, /* LOGICAL_MINIMUM (0) */
204 0x26, 0xFF, 0x00, /* LOGICAL_MAXIMUM (255) */
205 0x05, 0x07, /* USAGE_PAGE (Keyboard) */
206 0x19, 0x00, /* USAGE_MINIMUM (no event) */
207 0x2A, 0xFF, 0x00, /* USAGE_MAXIMUM (reserved) */
208 0x81, 0x00, /* INPUT (Data,Ary,Abs) */
Benjamin Tissoires0e40d352014-02-05 16:33:17 -0500209 0x85, 0x0e, /* REPORT_ID (14) */
210 0x05, 0x08, /* USAGE PAGE (LED page) */
211 0x95, 0x05, /* REPORT COUNT (5) */
212 0x75, 0x01, /* REPORT SIZE (1) */
213 0x15, 0x00, /* LOGICAL_MINIMUM (0) */
214 0x25, 0x01, /* LOGICAL_MAXIMUM (1) */
215 0x19, 0x01, /* USAGE MINIMUM (1) */
216 0x29, 0x05, /* USAGE MAXIMUM (5) */
217 0x91, 0x02, /* OUTPUT (Data, Variable, Absolute) */
218 0x95, 0x01, /* REPORT COUNT (1) */
219 0x75, 0x03, /* REPORT SIZE (3) */
220 0x91, 0x01, /* OUTPUT (Constant) */
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200221 0xC0
222};
223
224/* Mouse descriptor (2) */
225static const char mse_descriptor[] = {
226 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */
227 0x09, 0x02, /* USAGE (Mouse) */
228 0xA1, 0x01, /* COLLECTION (Application) */
229 0x85, 0x02, /* REPORT_ID = 2 */
230 0x09, 0x01, /* USAGE (pointer) */
231 0xA1, 0x00, /* COLLECTION (physical) */
232 0x05, 0x09, /* USAGE_PAGE (buttons) */
233 0x19, 0x01, /* USAGE_MIN (1) */
234 0x29, 0x10, /* USAGE_MAX (16) */
235 0x15, 0x00, /* LOGICAL_MIN (0) */
236 0x25, 0x01, /* LOGICAL_MAX (1) */
237 0x95, 0x10, /* REPORT_COUNT (16) */
238 0x75, 0x01, /* REPORT_SIZE (1) */
239 0x81, 0x02, /* INPUT (data var abs) */
240 0x05, 0x01, /* USAGE_PAGE (generic desktop) */
241 0x16, 0x01, 0xF8, /* LOGICAL_MIN (-2047) */
242 0x26, 0xFF, 0x07, /* LOGICAL_MAX (2047) */
243 0x75, 0x0C, /* REPORT_SIZE (12) */
244 0x95, 0x02, /* REPORT_COUNT (2) */
245 0x09, 0x30, /* USAGE (X) */
246 0x09, 0x31, /* USAGE (Y) */
247 0x81, 0x06, /* INPUT */
248 0x15, 0x81, /* LOGICAL_MIN (-127) */
249 0x25, 0x7F, /* LOGICAL_MAX (127) */
250 0x75, 0x08, /* REPORT_SIZE (8) */
251 0x95, 0x01, /* REPORT_COUNT (1) */
252 0x09, 0x38, /* USAGE (wheel) */
253 0x81, 0x06, /* INPUT */
254 0x05, 0x0C, /* USAGE_PAGE(consumer) */
255 0x0A, 0x38, 0x02, /* USAGE(AC Pan) */
256 0x95, 0x01, /* REPORT_COUNT (1) */
257 0x81, 0x06, /* INPUT */
258 0xC0, /* END_COLLECTION */
259 0xC0, /* END_COLLECTION */
260};
261
Hans de Goedec9121cf2019-04-20 13:21:56 +0200262/* Mouse descriptor (2) for 27 MHz receiver, only 8 buttons */
263static const char mse_27mhz_descriptor[] = {
264 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */
265 0x09, 0x02, /* USAGE (Mouse) */
266 0xA1, 0x01, /* COLLECTION (Application) */
267 0x85, 0x02, /* REPORT_ID = 2 */
268 0x09, 0x01, /* USAGE (pointer) */
269 0xA1, 0x00, /* COLLECTION (physical) */
270 0x05, 0x09, /* USAGE_PAGE (buttons) */
271 0x19, 0x01, /* USAGE_MIN (1) */
272 0x29, 0x08, /* USAGE_MAX (8) */
273 0x15, 0x00, /* LOGICAL_MIN (0) */
274 0x25, 0x01, /* LOGICAL_MAX (1) */
275 0x95, 0x08, /* REPORT_COUNT (8) */
276 0x75, 0x01, /* REPORT_SIZE (1) */
277 0x81, 0x02, /* INPUT (data var abs) */
278 0x05, 0x01, /* USAGE_PAGE (generic desktop) */
279 0x16, 0x01, 0xF8, /* LOGICAL_MIN (-2047) */
280 0x26, 0xFF, 0x07, /* LOGICAL_MAX (2047) */
281 0x75, 0x0C, /* REPORT_SIZE (12) */
282 0x95, 0x02, /* REPORT_COUNT (2) */
283 0x09, 0x30, /* USAGE (X) */
284 0x09, 0x31, /* USAGE (Y) */
285 0x81, 0x06, /* INPUT */
286 0x15, 0x81, /* LOGICAL_MIN (-127) */
287 0x25, 0x7F, /* LOGICAL_MAX (127) */
288 0x75, 0x08, /* REPORT_SIZE (8) */
289 0x95, 0x01, /* REPORT_COUNT (1) */
290 0x09, 0x38, /* USAGE (wheel) */
291 0x81, 0x06, /* INPUT */
292 0x05, 0x0C, /* USAGE_PAGE(consumer) */
293 0x0A, 0x38, 0x02, /* USAGE(AC Pan) */
294 0x95, 0x01, /* REPORT_COUNT (1) */
295 0x81, 0x06, /* INPUT */
296 0xC0, /* END_COLLECTION */
297 0xC0, /* END_COLLECTION */
298};
299
Hans de Goedef2113c32019-04-20 13:22:03 +0200300/* Mouse descriptor (2) for Bluetooth receiver, low-res hwheel, 12 buttons */
301static const char mse_bluetooth_descriptor[] = {
302 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */
303 0x09, 0x02, /* USAGE (Mouse) */
304 0xA1, 0x01, /* COLLECTION (Application) */
305 0x85, 0x02, /* REPORT_ID = 2 */
306 0x09, 0x01, /* USAGE (pointer) */
307 0xA1, 0x00, /* COLLECTION (physical) */
308 0x05, 0x09, /* USAGE_PAGE (buttons) */
309 0x19, 0x01, /* USAGE_MIN (1) */
310 0x29, 0x08, /* USAGE_MAX (8) */
311 0x15, 0x00, /* LOGICAL_MIN (0) */
312 0x25, 0x01, /* LOGICAL_MAX (1) */
313 0x95, 0x08, /* REPORT_COUNT (8) */
314 0x75, 0x01, /* REPORT_SIZE (1) */
315 0x81, 0x02, /* INPUT (data var abs) */
316 0x05, 0x01, /* USAGE_PAGE (generic desktop) */
317 0x16, 0x01, 0xF8, /* LOGICAL_MIN (-2047) */
318 0x26, 0xFF, 0x07, /* LOGICAL_MAX (2047) */
319 0x75, 0x0C, /* REPORT_SIZE (12) */
320 0x95, 0x02, /* REPORT_COUNT (2) */
321 0x09, 0x30, /* USAGE (X) */
322 0x09, 0x31, /* USAGE (Y) */
323 0x81, 0x06, /* INPUT */
324 0x15, 0x81, /* LOGICAL_MIN (-127) */
325 0x25, 0x7F, /* LOGICAL_MAX (127) */
326 0x75, 0x08, /* REPORT_SIZE (8) */
327 0x95, 0x01, /* REPORT_COUNT (1) */
328 0x09, 0x38, /* USAGE (wheel) */
329 0x81, 0x06, /* INPUT */
330 0x05, 0x0C, /* USAGE_PAGE(consumer) */
331 0x0A, 0x38, 0x02, /* USAGE(AC Pan) */
332 0x15, 0xF9, /* LOGICAL_MIN (-7) */
333 0x25, 0x07, /* LOGICAL_MAX (7) */
334 0x75, 0x04, /* REPORT_SIZE (4) */
335 0x95, 0x01, /* REPORT_COUNT (1) */
336 0x81, 0x06, /* INPUT */
337 0x05, 0x09, /* USAGE_PAGE (buttons) */
338 0x19, 0x09, /* USAGE_MIN (9) */
339 0x29, 0x0C, /* USAGE_MAX (12) */
340 0x15, 0x00, /* LOGICAL_MIN (0) */
341 0x25, 0x01, /* LOGICAL_MAX (1) */
342 0x75, 0x01, /* REPORT_SIZE (1) */
343 0x95, 0x04, /* REPORT_COUNT (4) */
344 0x81, 0x06, /* INPUT */
345 0xC0, /* END_COLLECTION */
346 0xC0, /* END_COLLECTION */
347};
348
Benjamin Tissoiresf5fb57a2019-04-20 13:21:55 +0200349/* Gaming Mouse descriptor (2) */
350static const char mse_high_res_descriptor[] = {
351 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */
352 0x09, 0x02, /* USAGE (Mouse) */
353 0xA1, 0x01, /* COLLECTION (Application) */
354 0x85, 0x02, /* REPORT_ID = 2 */
355 0x09, 0x01, /* USAGE (pointer) */
356 0xA1, 0x00, /* COLLECTION (physical) */
357 0x05, 0x09, /* USAGE_PAGE (buttons) */
358 0x19, 0x01, /* USAGE_MIN (1) */
359 0x29, 0x10, /* USAGE_MAX (16) */
360 0x15, 0x00, /* LOGICAL_MIN (0) */
361 0x25, 0x01, /* LOGICAL_MAX (1) */
362 0x95, 0x10, /* REPORT_COUNT (16) */
363 0x75, 0x01, /* REPORT_SIZE (1) */
364 0x81, 0x02, /* INPUT (data var abs) */
365 0x05, 0x01, /* USAGE_PAGE (generic desktop) */
366 0x16, 0x01, 0x80, /* LOGICAL_MIN (-32767) */
367 0x26, 0xFF, 0x7F, /* LOGICAL_MAX (32767) */
368 0x75, 0x10, /* REPORT_SIZE (16) */
369 0x95, 0x02, /* REPORT_COUNT (2) */
370 0x09, 0x30, /* USAGE (X) */
371 0x09, 0x31, /* USAGE (Y) */
372 0x81, 0x06, /* INPUT */
373 0x15, 0x81, /* LOGICAL_MIN (-127) */
374 0x25, 0x7F, /* LOGICAL_MAX (127) */
375 0x75, 0x08, /* REPORT_SIZE (8) */
376 0x95, 0x01, /* REPORT_COUNT (1) */
377 0x09, 0x38, /* USAGE (wheel) */
378 0x81, 0x06, /* INPUT */
379 0x05, 0x0C, /* USAGE_PAGE(consumer) */
380 0x0A, 0x38, 0x02, /* USAGE(AC Pan) */
381 0x95, 0x01, /* REPORT_COUNT (1) */
382 0x81, 0x06, /* INPUT */
383 0xC0, /* END_COLLECTION */
384 0xC0, /* END_COLLECTION */
385};
386
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200387/* Consumer Control descriptor (3) */
388static const char consumer_descriptor[] = {
389 0x05, 0x0C, /* USAGE_PAGE (Consumer Devices) */
390 0x09, 0x01, /* USAGE (Consumer Control) */
391 0xA1, 0x01, /* COLLECTION (Application) */
392 0x85, 0x03, /* REPORT_ID = 3 */
393 0x75, 0x10, /* REPORT_SIZE (16) */
394 0x95, 0x02, /* REPORT_COUNT (2) */
395 0x15, 0x01, /* LOGICAL_MIN (1) */
396 0x26, 0x8C, 0x02, /* LOGICAL_MAX (652) */
397 0x19, 0x01, /* USAGE_MIN (1) */
398 0x2A, 0x8C, 0x02, /* USAGE_MAX (652) */
399 0x81, 0x00, /* INPUT (Data Ary Abs) */
400 0xC0, /* END_COLLECTION */
401}; /* */
402
403/* System control descriptor (4) */
404static const char syscontrol_descriptor[] = {
405 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */
406 0x09, 0x80, /* USAGE (System Control) */
407 0xA1, 0x01, /* COLLECTION (Application) */
408 0x85, 0x04, /* REPORT_ID = 4 */
409 0x75, 0x02, /* REPORT_SIZE (2) */
410 0x95, 0x01, /* REPORT_COUNT (1) */
411 0x15, 0x01, /* LOGICAL_MIN (1) */
412 0x25, 0x03, /* LOGICAL_MAX (3) */
413 0x09, 0x82, /* USAGE (System Sleep) */
414 0x09, 0x81, /* USAGE (System Power Down) */
415 0x09, 0x83, /* USAGE (System Wake Up) */
416 0x81, 0x60, /* INPUT (Data Ary Abs NPrf Null) */
417 0x75, 0x06, /* REPORT_SIZE (6) */
418 0x81, 0x03, /* INPUT (Cnst Var Abs) */
419 0xC0, /* END_COLLECTION */
420};
421
422/* Media descriptor (8) */
423static const char media_descriptor[] = {
424 0x06, 0xbc, 0xff, /* Usage Page 0xffbc */
425 0x09, 0x88, /* Usage 0x0088 */
426 0xa1, 0x01, /* BeginCollection */
427 0x85, 0x08, /* Report ID 8 */
428 0x19, 0x01, /* Usage Min 0x0001 */
429 0x29, 0xff, /* Usage Max 0x00ff */
430 0x15, 0x01, /* Logical Min 1 */
431 0x26, 0xff, 0x00, /* Logical Max 255 */
432 0x75, 0x08, /* Report Size 8 */
433 0x95, 0x01, /* Report Count 1 */
434 0x81, 0x00, /* Input */
435 0xc0, /* EndCollection */
436}; /* */
437
Benjamin Tissoires925f0f32014-09-30 13:18:29 -0400438/* HIDPP descriptor */
439static const char hidpp_descriptor[] = {
440 0x06, 0x00, 0xff, /* Usage Page (Vendor Defined Page 1) */
441 0x09, 0x01, /* Usage (Vendor Usage 1) */
442 0xa1, 0x01, /* Collection (Application) */
443 0x85, 0x10, /* Report ID (16) */
444 0x75, 0x08, /* Report Size (8) */
445 0x95, 0x06, /* Report Count (6) */
446 0x15, 0x00, /* Logical Minimum (0) */
447 0x26, 0xff, 0x00, /* Logical Maximum (255) */
448 0x09, 0x01, /* Usage (Vendor Usage 1) */
449 0x81, 0x00, /* Input (Data,Arr,Abs) */
450 0x09, 0x01, /* Usage (Vendor Usage 1) */
451 0x91, 0x00, /* Output (Data,Arr,Abs) */
452 0xc0, /* End Collection */
453 0x06, 0x00, 0xff, /* Usage Page (Vendor Defined Page 1) */
454 0x09, 0x02, /* Usage (Vendor Usage 2) */
455 0xa1, 0x01, /* Collection (Application) */
456 0x85, 0x11, /* Report ID (17) */
457 0x75, 0x08, /* Report Size (8) */
458 0x95, 0x13, /* Report Count (19) */
459 0x15, 0x00, /* Logical Minimum (0) */
460 0x26, 0xff, 0x00, /* Logical Maximum (255) */
461 0x09, 0x02, /* Usage (Vendor Usage 2) */
462 0x81, 0x00, /* Input (Data,Arr,Abs) */
463 0x09, 0x02, /* Usage (Vendor Usage 2) */
464 0x91, 0x00, /* Output (Data,Arr,Abs) */
465 0xc0, /* End Collection */
466 0x06, 0x00, 0xff, /* Usage Page (Vendor Defined Page 1) */
467 0x09, 0x04, /* Usage (Vendor Usage 0x04) */
468 0xa1, 0x01, /* Collection (Application) */
469 0x85, 0x20, /* Report ID (32) */
470 0x75, 0x08, /* Report Size (8) */
471 0x95, 0x0e, /* Report Count (14) */
472 0x15, 0x00, /* Logical Minimum (0) */
473 0x26, 0xff, 0x00, /* Logical Maximum (255) */
474 0x09, 0x41, /* Usage (Vendor Usage 0x41) */
475 0x81, 0x00, /* Input (Data,Arr,Abs) */
476 0x09, 0x41, /* Usage (Vendor Usage 0x41) */
477 0x91, 0x00, /* Output (Data,Arr,Abs) */
478 0x85, 0x21, /* Report ID (33) */
479 0x95, 0x1f, /* Report Count (31) */
480 0x15, 0x00, /* Logical Minimum (0) */
481 0x26, 0xff, 0x00, /* Logical Maximum (255) */
482 0x09, 0x42, /* Usage (Vendor Usage 0x42) */
483 0x81, 0x00, /* Input (Data,Arr,Abs) */
484 0x09, 0x42, /* Usage (Vendor Usage 0x42) */
485 0x91, 0x00, /* Output (Data,Arr,Abs) */
486 0xc0, /* End Collection */
487};
488
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200489/* Maximum size of all defined hid reports in bytes (including report id) */
490#define MAX_REPORT_SIZE 8
491
Henrik Rydberg2a039bf2012-04-22 14:21:39 +0200492/* Make sure all descriptors are present here */
493#define MAX_RDESC_SIZE \
494 (sizeof(kbd_descriptor) + \
Hans de Goedef2113c32019-04-20 13:22:03 +0200495 sizeof(mse_bluetooth_descriptor) + \
Henrik Rydberg2a039bf2012-04-22 14:21:39 +0200496 sizeof(consumer_descriptor) + \
497 sizeof(syscontrol_descriptor) + \
Benjamin Tissoires925f0f32014-09-30 13:18:29 -0400498 sizeof(media_descriptor) + \
499 sizeof(hidpp_descriptor))
Henrik Rydberg2a039bf2012-04-22 14:21:39 +0200500
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200501/* Number of possible hid report types that can be created by this driver.
502 *
503 * Right now, RF report types have the same report types (or report id's)
504 * than the hid report created from those RF reports. In the future
505 * this doesnt have to be true.
506 *
507 * For instance, RF report type 0x01 which has a size of 8 bytes, corresponds
508 * to hid report id 0x01, this is standard keyboard. Same thing applies to mice
509 * reports and consumer control, etc. If a new RF report is created, it doesn't
510 * has to have the same report id as its corresponding hid report, so an
511 * translation may have to take place for future report types.
512 */
513#define NUMBER_OF_HID_REPORTS 32
514static const u8 hid_reportid_size_map[NUMBER_OF_HID_REPORTS] = {
515 [1] = 8, /* Standard keyboard */
516 [2] = 8, /* Standard mouse */
517 [3] = 5, /* Consumer control */
518 [4] = 2, /* System control */
519 [8] = 2, /* Media Center */
520};
521
522
523#define LOGITECH_DJ_INTERFACE_NUMBER 0x02
524
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200525static struct hid_ll_driver logi_dj_ll_driver;
526
Nestor Lopez Casadoc63e0e32013-07-18 06:21:30 -0700527static int logi_dj_recv_query_paired_devices(struct dj_receiver_dev *djrcv_dev);
Hans de Goedea1d97cc2019-04-20 13:21:52 +0200528static void delayedwork_callback(struct work_struct *work);
529
530static LIST_HEAD(dj_hdev_list);
531static DEFINE_MUTEX(dj_hdev_list_lock);
532
533/*
534 * dj/HID++ receivers are really a single logical entity, but for BIOS/Windows
535 * compatibility they have multiple USB interfaces. On HID++ receivers we need
536 * to listen for input reports on both interfaces. The functions below are used
537 * to create a single struct dj_receiver_dev for all interfaces belonging to
538 * a single USB-device / receiver.
539 */
Hans de Goedef2113c32019-04-20 13:22:03 +0200540static struct dj_receiver_dev *dj_find_receiver_dev(struct hid_device *hdev,
541 enum recvr_type type)
Hans de Goedea1d97cc2019-04-20 13:21:52 +0200542{
543 struct dj_receiver_dev *djrcv_dev;
Hans de Goedef2113c32019-04-20 13:22:03 +0200544 char sep;
545
546 /*
547 * The bluetooth receiver contains a built-in hub and has separate
548 * USB-devices for the keyboard and mouse interfaces.
549 */
550 sep = (type == recvr_type_bluetooth) ? '.' : '/';
Hans de Goedea1d97cc2019-04-20 13:21:52 +0200551
552 /* Try to find an already-probed interface from the same device */
553 list_for_each_entry(djrcv_dev, &dj_hdev_list, list) {
554 if (djrcv_dev->mouse &&
Hans de Goedef2113c32019-04-20 13:22:03 +0200555 hid_compare_device_paths(hdev, djrcv_dev->mouse, sep)) {
Hans de Goedea1d97cc2019-04-20 13:21:52 +0200556 kref_get(&djrcv_dev->kref);
557 return djrcv_dev;
558 }
559 if (djrcv_dev->keyboard &&
Hans de Goedef2113c32019-04-20 13:22:03 +0200560 hid_compare_device_paths(hdev, djrcv_dev->keyboard, sep)) {
Hans de Goedea1d97cc2019-04-20 13:21:52 +0200561 kref_get(&djrcv_dev->kref);
562 return djrcv_dev;
563 }
564 if (djrcv_dev->hidpp &&
Hans de Goedef2113c32019-04-20 13:22:03 +0200565 hid_compare_device_paths(hdev, djrcv_dev->hidpp, sep)) {
Hans de Goedea1d97cc2019-04-20 13:21:52 +0200566 kref_get(&djrcv_dev->kref);
567 return djrcv_dev;
568 }
569 }
570
571 return NULL;
572}
573
574static void dj_release_receiver_dev(struct kref *kref)
575{
576 struct dj_receiver_dev *djrcv_dev = container_of(kref, struct dj_receiver_dev, kref);
577
578 list_del(&djrcv_dev->list);
579 kfifo_free(&djrcv_dev->notif_fifo);
580 kfree(djrcv_dev);
581}
582
583static void dj_put_receiver_dev(struct hid_device *hdev)
584{
585 struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
586
587 mutex_lock(&dj_hdev_list_lock);
588
589 if (djrcv_dev->mouse == hdev)
590 djrcv_dev->mouse = NULL;
591 if (djrcv_dev->keyboard == hdev)
592 djrcv_dev->keyboard = NULL;
593 if (djrcv_dev->hidpp == hdev)
594 djrcv_dev->hidpp = NULL;
595
596 kref_put(&djrcv_dev->kref, dj_release_receiver_dev);
597
598 mutex_unlock(&dj_hdev_list_lock);
599}
600
601static struct dj_receiver_dev *dj_get_receiver_dev(struct hid_device *hdev,
Hans de Goede74808f92019-04-20 13:21:54 +0200602 enum recvr_type type,
Hans de Goedea1d97cc2019-04-20 13:21:52 +0200603 unsigned int application,
604 bool is_hidpp)
605{
606 struct dj_receiver_dev *djrcv_dev;
607
608 mutex_lock(&dj_hdev_list_lock);
609
Hans de Goedef2113c32019-04-20 13:22:03 +0200610 djrcv_dev = dj_find_receiver_dev(hdev, type);
Hans de Goedea1d97cc2019-04-20 13:21:52 +0200611 if (!djrcv_dev) {
612 djrcv_dev = kzalloc(sizeof(*djrcv_dev), GFP_KERNEL);
613 if (!djrcv_dev)
614 goto out;
615
616 INIT_WORK(&djrcv_dev->work, delayedwork_callback);
617 spin_lock_init(&djrcv_dev->lock);
618 if (kfifo_alloc(&djrcv_dev->notif_fifo,
619 DJ_MAX_NUMBER_NOTIFS * sizeof(struct dj_workitem),
620 GFP_KERNEL)) {
621 kfree(djrcv_dev);
622 djrcv_dev = NULL;
623 goto out;
624 }
625 kref_init(&djrcv_dev->kref);
626 list_add_tail(&djrcv_dev->list, &dj_hdev_list);
Hans de Goedeb6aeedd2019-04-20 13:21:53 +0200627 djrcv_dev->last_query = jiffies;
Hans de Goede74808f92019-04-20 13:21:54 +0200628 djrcv_dev->type = type;
Hans de Goedea1d97cc2019-04-20 13:21:52 +0200629 }
630
631 if (application == HID_GD_KEYBOARD)
632 djrcv_dev->keyboard = hdev;
633 if (application == HID_GD_MOUSE)
634 djrcv_dev->mouse = hdev;
635 if (is_hidpp)
636 djrcv_dev->hidpp = hdev;
637
638 hid_set_drvdata(hdev, djrcv_dev);
639out:
640 mutex_unlock(&dj_hdev_list_lock);
641 return djrcv_dev;
642}
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200643
644static void logi_dj_recv_destroy_djhid_device(struct dj_receiver_dev *djrcv_dev,
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200645 struct dj_workitem *workitem)
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200646{
647 /* Called in delayed work context */
648 struct dj_device *dj_dev;
649 unsigned long flags;
650
651 spin_lock_irqsave(&djrcv_dev->lock, flags);
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200652 dj_dev = djrcv_dev->paired_dj_devices[workitem->device_index];
653 djrcv_dev->paired_dj_devices[workitem->device_index] = NULL;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200654 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
655
656 if (dj_dev != NULL) {
657 hid_destroy_device(dj_dev->hdev);
658 kfree(dj_dev);
659 } else {
Hans de Goede0ee75542019-04-20 13:21:51 +0200660 hid_err(djrcv_dev->hidpp, "%s: can't destroy a NULL device\n",
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200661 __func__);
662 }
663}
664
665static void logi_dj_recv_add_djhid_device(struct dj_receiver_dev *djrcv_dev,
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200666 struct dj_workitem *workitem)
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200667{
668 /* Called in delayed work context */
Hans de Goede0ee75542019-04-20 13:21:51 +0200669 struct hid_device *djrcv_hdev = djrcv_dev->hidpp;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200670 struct hid_device *dj_hiddev;
671 struct dj_device *dj_dev;
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200672 u8 device_index = workitem->device_index;
Hans de Goedef41d7662019-04-20 13:21:50 +0200673 unsigned long flags;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200674
675 /* Device index goes from 1 to 6, we need 3 bytes to store the
676 * semicolon, the index, and a null terminator
677 */
678 unsigned char tmpstr[3];
679
Hans de Goedef41d7662019-04-20 13:21:50 +0200680 /* We are the only one ever adding a device, no need to lock */
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200681 if (djrcv_dev->paired_dj_devices[device_index]) {
Nestor Lopez Casadoc63e0e32013-07-18 06:21:30 -0700682 /* The device is already known. No need to reallocate it. */
683 dbg_hid("%s: device is already known\n", __func__);
684 return;
685 }
686
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200687 dj_hiddev = hid_allocate_device();
688 if (IS_ERR(dj_hiddev)) {
Hans de Goedeaca22a32019-04-20 13:21:58 +0200689 hid_err(djrcv_hdev, "%s: hid_allocate_dev failed\n", __func__);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200690 return;
691 }
692
693 dj_hiddev->ll_driver = &logi_dj_ll_driver;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200694
695 dj_hiddev->dev.parent = &djrcv_hdev->dev;
696 dj_hiddev->bus = BUS_USB;
Benjamin Tissoires82c0beb2019-04-20 13:21:47 +0200697 dj_hiddev->vendor = djrcv_hdev->vendor;
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200698 dj_hiddev->product = (workitem->quad_id_msb << 8) |
699 workitem->quad_id_lsb;
Hans de Goedede76b1d2019-04-20 13:22:00 +0200700 if (workitem->device_type) {
701 const char *type_str = "Device";
702
703 switch (workitem->device_type) {
704 case 0x01: type_str = "Keyboard"; break;
705 case 0x02: type_str = "Mouse"; break;
706 case 0x03: type_str = "Numpad"; break;
707 case 0x04: type_str = "Presenter"; break;
708 case 0x07: type_str = "Remote Control"; break;
709 case 0x08: type_str = "Trackball"; break;
710 case 0x09: type_str = "Touchpad"; break;
711 }
712 snprintf(dj_hiddev->name, sizeof(dj_hiddev->name),
713 "Logitech Wireless %s PID:%04x",
714 type_str, dj_hiddev->product);
715 } else {
716 snprintf(dj_hiddev->name, sizeof(dj_hiddev->name),
717 "Logitech Unifying Device. Wireless PID:%04x",
718 dj_hiddev->product);
719 }
Benjamin Tissoiresd6102742014-09-30 13:18:25 -0400720
Hans de Goedec9121cf2019-04-20 13:21:56 +0200721 if (djrcv_dev->type == recvr_type_27mhz)
722 dj_hiddev->group = HID_GROUP_LOGITECH_27MHZ_DEVICE;
723 else
724 dj_hiddev->group = HID_GROUP_LOGITECH_DJ_DEVICE;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200725
Benjamin Tissoires82c0beb2019-04-20 13:21:47 +0200726 memcpy(dj_hiddev->phys, djrcv_hdev->phys, sizeof(djrcv_hdev->phys));
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200727 snprintf(tmpstr, sizeof(tmpstr), ":%d", device_index);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200728 strlcat(dj_hiddev->phys, tmpstr, sizeof(dj_hiddev->phys));
729
730 dj_dev = kzalloc(sizeof(struct dj_device), GFP_KERNEL);
731
732 if (!dj_dev) {
Hans de Goedeaca22a32019-04-20 13:21:58 +0200733 hid_err(djrcv_hdev, "%s: failed allocating dj_dev\n", __func__);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200734 goto dj_device_allocate_fail;
735 }
736
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200737 dj_dev->reports_supported = workitem->reports_supported;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200738 dj_dev->hdev = dj_hiddev;
739 dj_dev->dj_receiver_dev = djrcv_dev;
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200740 dj_dev->device_index = device_index;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200741 dj_hiddev->driver_data = dj_dev;
742
Hans de Goedef41d7662019-04-20 13:21:50 +0200743 spin_lock_irqsave(&djrcv_dev->lock, flags);
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200744 djrcv_dev->paired_dj_devices[device_index] = dj_dev;
Hans de Goedef41d7662019-04-20 13:21:50 +0200745 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200746
747 if (hid_add_device(dj_hiddev)) {
Hans de Goedeaca22a32019-04-20 13:21:58 +0200748 hid_err(djrcv_hdev, "%s: failed adding dj_device\n", __func__);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200749 goto hid_add_device_fail;
750 }
751
752 return;
753
754hid_add_device_fail:
Hans de Goedef41d7662019-04-20 13:21:50 +0200755 spin_lock_irqsave(&djrcv_dev->lock, flags);
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200756 djrcv_dev->paired_dj_devices[device_index] = NULL;
Hans de Goedef41d7662019-04-20 13:21:50 +0200757 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200758 kfree(dj_dev);
759dj_device_allocate_fail:
760 hid_destroy_device(dj_hiddev);
761}
762
763static void delayedwork_callback(struct work_struct *work)
764{
765 struct dj_receiver_dev *djrcv_dev =
766 container_of(work, struct dj_receiver_dev, work);
767
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200768 struct dj_workitem workitem;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200769 unsigned long flags;
770 int count;
Nestor Lopez Casadoc63e0e32013-07-18 06:21:30 -0700771 int retval;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200772
773 dbg_hid("%s\n", __func__);
774
775 spin_lock_irqsave(&djrcv_dev->lock, flags);
776
Hans de Goedea1d97cc2019-04-20 13:21:52 +0200777 /*
778 * Since we attach to multiple interfaces, we may get scheduled before
779 * we are bound to the HID++ interface, catch this.
780 */
781 if (!djrcv_dev->ready) {
782 pr_warn("%s: delayedwork queued before hidpp interface was enumerated\n",
783 __func__);
784 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
785 return;
786 }
787
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200788 count = kfifo_out(&djrcv_dev->notif_fifo, &workitem, sizeof(workitem));
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200789
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200790 if (count != sizeof(workitem)) {
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200791 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
792 return;
793 }
794
Hans de Goedee316aa62019-04-20 13:22:01 +0200795 if (!kfifo_is_empty(&djrcv_dev->notif_fifo))
796 schedule_work(&djrcv_dev->work);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200797
798 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
799
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200800 switch (workitem.type) {
801 case WORKITEM_TYPE_PAIRED:
802 logi_dj_recv_add_djhid_device(djrcv_dev, &workitem);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200803 break;
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200804 case WORKITEM_TYPE_UNPAIRED:
805 logi_dj_recv_destroy_djhid_device(djrcv_dev, &workitem);
806 break;
807 case WORKITEM_TYPE_UNKNOWN:
808 retval = logi_dj_recv_query_paired_devices(djrcv_dev);
809 if (retval) {
Hans de Goede0ee75542019-04-20 13:21:51 +0200810 hid_err(djrcv_dev->hidpp, "%s: logi_dj_recv_query_paired_devices error: %d\n",
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200811 __func__, retval);
812 }
813 break;
814 case WORKITEM_TYPE_EMPTY:
815 dbg_hid("%s: device list is empty\n", __func__);
816 break;
817 }
818}
819
Hans de Goedeb6aeedd2019-04-20 13:21:53 +0200820/*
821 * Sometimes we receive reports for which we do not have a paired dj_device
822 * associated with the device_index or report-type to forward the report to.
823 * This means that the original "device paired" notification corresponding
824 * to the dj_device never arrived to this driver. Possible reasons for this are:
825 * 1) hid-core discards all packets coming from a device during probe().
826 * 2) if the receiver is plugged into a KVM switch then the pairing reports
827 * are only forwarded to it if the focus is on this PC.
828 * This function deals with this by re-asking the receiver for the list of
829 * connected devices in the delayed work callback.
830 * This function MUST be called with djrcv->lock held.
831 */
832static void logi_dj_recv_queue_unknown_work(struct dj_receiver_dev *djrcv_dev)
833{
834 struct dj_workitem workitem = { .type = WORKITEM_TYPE_UNKNOWN };
835
836 /* Rate limit queries done because of unhandeled reports to 2/sec */
837 if (time_before(jiffies, djrcv_dev->last_query + HZ / 2))
838 return;
839
840 kfifo_in(&djrcv_dev->notif_fifo, &workitem, sizeof(workitem));
841 schedule_work(&djrcv_dev->work);
842}
843
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200844static void logi_dj_recv_queue_notification(struct dj_receiver_dev *djrcv_dev,
845 struct dj_report *dj_report)
846{
847 /* We are called from atomic context (tasklet && djrcv->lock held) */
848 struct dj_workitem workitem = {
849 .device_index = dj_report->device_index,
850 };
851
852 switch (dj_report->report_type) {
853 case REPORT_TYPE_NOTIF_DEVICE_PAIRED:
854 workitem.type = WORKITEM_TYPE_PAIRED;
855 if (dj_report->report_params[DEVICE_PAIRED_PARAM_SPFUNCTION] &
856 SPFUNCTION_DEVICE_LIST_EMPTY) {
857 workitem.type = WORKITEM_TYPE_EMPTY;
858 break;
859 }
860 /* fall-through */
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200861 case REPORT_TYPE_NOTIF_DEVICE_UNPAIRED:
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200862 workitem.quad_id_msb =
863 dj_report->report_params[DEVICE_PAIRED_PARAM_EQUAD_ID_MSB];
864 workitem.quad_id_lsb =
865 dj_report->report_params[DEVICE_PAIRED_PARAM_EQUAD_ID_LSB];
866 workitem.reports_supported = get_unaligned_le32(
867 dj_report->report_params +
868 DEVICE_PAIRED_RF_REPORT_TYPE);
Hans de Goede6d3c3f02019-04-20 13:22:02 +0200869 workitem.reports_supported |= HIDPP;
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200870 if (dj_report->report_type == REPORT_TYPE_NOTIF_DEVICE_UNPAIRED)
871 workitem.type = WORKITEM_TYPE_UNPAIRED;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200872 break;
873 default:
Hans de Goedeb6aeedd2019-04-20 13:21:53 +0200874 logi_dj_recv_queue_unknown_work(djrcv_dev);
875 return;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200876 }
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200877
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200878 kfifo_in(&djrcv_dev->notif_fifo, &workitem, sizeof(workitem));
Hans de Goedee316aa62019-04-20 13:22:01 +0200879 schedule_work(&djrcv_dev->work);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200880}
881
Hans de Goede74808f92019-04-20 13:21:54 +0200882static void logi_hidpp_dev_conn_notif_equad(struct hidpp_event *hidpp_report,
883 struct dj_workitem *workitem)
884{
885 workitem->type = WORKITEM_TYPE_PAIRED;
Hans de Goedede76b1d2019-04-20 13:22:00 +0200886 workitem->device_type = hidpp_report->params[HIDPP_PARAM_DEVICE_INFO] &
887 HIDPP_DEVICE_TYPE_MASK;
Hans de Goede74808f92019-04-20 13:21:54 +0200888 workitem->quad_id_msb = hidpp_report->params[HIDPP_PARAM_EQUAD_MSB];
889 workitem->quad_id_lsb = hidpp_report->params[HIDPP_PARAM_EQUAD_LSB];
Hans de Goedede76b1d2019-04-20 13:22:00 +0200890 switch (workitem->device_type) {
Hans de Goede74808f92019-04-20 13:21:54 +0200891 case REPORT_TYPE_KEYBOARD:
892 workitem->reports_supported |= STD_KEYBOARD | MULTIMEDIA |
Hans de Goede6d3c3f02019-04-20 13:22:02 +0200893 POWER_KEYS | MEDIA_CENTER |
894 HIDPP;
Hans de Goede74808f92019-04-20 13:21:54 +0200895 break;
896 case REPORT_TYPE_MOUSE:
Hans de Goede6d3c3f02019-04-20 13:22:02 +0200897 workitem->reports_supported |= STD_MOUSE | HIDPP;
Hans de Goede74808f92019-04-20 13:21:54 +0200898 break;
899 }
900}
901
Hans de Goedec9121cf2019-04-20 13:21:56 +0200902static void logi_hidpp_dev_conn_notif_27mhz(struct hid_device *hdev,
903 struct hidpp_event *hidpp_report,
904 struct dj_workitem *workitem)
905{
906 workitem->type = WORKITEM_TYPE_PAIRED;
907 workitem->quad_id_lsb = hidpp_report->params[HIDPP_PARAM_27MHZ_DEVID];
908 switch (hidpp_report->device_index) {
909 case 1: /* Index 1 is always a mouse */
910 case 2: /* Index 2 is always a mouse */
Hans de Goedede76b1d2019-04-20 13:22:00 +0200911 workitem->device_type = HIDPP_DEVICE_TYPE_MOUSE;
Hans de Goede6d3c3f02019-04-20 13:22:02 +0200912 workitem->reports_supported |= STD_MOUSE | HIDPP;
Hans de Goedec9121cf2019-04-20 13:21:56 +0200913 break;
914 case 3: /* Index 3 is always the keyboard */
915 case 4: /* Index 4 is used for an optional separate numpad */
Hans de Goedede76b1d2019-04-20 13:22:00 +0200916 workitem->device_type = HIDPP_DEVICE_TYPE_KEYBOARD;
Hans de Goedec9121cf2019-04-20 13:21:56 +0200917 workitem->reports_supported |= STD_KEYBOARD | MULTIMEDIA |
Hans de Goede6d3c3f02019-04-20 13:22:02 +0200918 POWER_KEYS | HIDPP;
Hans de Goedec9121cf2019-04-20 13:21:56 +0200919 break;
920 default:
921 hid_warn(hdev, "%s: unexpected device-index %d", __func__,
922 hidpp_report->device_index);
923 }
924}
925
Hans de Goede74808f92019-04-20 13:21:54 +0200926static void logi_hidpp_recv_queue_notif(struct hid_device *hdev,
927 struct hidpp_event *hidpp_report)
928{
929 /* We are called from atomic context (tasklet && djrcv->lock held) */
930 struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
931 const char *device_type = "UNKNOWN";
932 struct dj_workitem workitem = {
933 .type = WORKITEM_TYPE_EMPTY,
934 .device_index = hidpp_report->device_index,
935 };
936
937 switch (hidpp_report->params[HIDPP_PARAM_PROTO_TYPE]) {
938 case 0x01:
939 device_type = "Bluetooth";
Hans de Goedef2113c32019-04-20 13:22:03 +0200940 /* Bluetooth connect packet contents is the same as (e)QUAD */
941 logi_hidpp_dev_conn_notif_equad(hidpp_report, &workitem);
942 if (!(hidpp_report->params[HIDPP_PARAM_DEVICE_INFO] &
943 HIDPP_MANUFACTURER_MASK)) {
944 hid_info(hdev, "Non Logitech device connected on slot %d\n",
945 hidpp_report->device_index);
946 workitem.reports_supported &= ~HIDPP;
947 }
Hans de Goede74808f92019-04-20 13:21:54 +0200948 break;
949 case 0x02:
950 device_type = "27 Mhz";
Hans de Goedec9121cf2019-04-20 13:21:56 +0200951 logi_hidpp_dev_conn_notif_27mhz(hdev, hidpp_report, &workitem);
Hans de Goede74808f92019-04-20 13:21:54 +0200952 break;
953 case 0x03:
954 device_type = "QUAD or eQUAD";
955 logi_hidpp_dev_conn_notif_equad(hidpp_report, &workitem);
956 break;
957 case 0x04:
958 device_type = "eQUAD step 4 DJ";
959 logi_hidpp_dev_conn_notif_equad(hidpp_report, &workitem);
960 break;
961 case 0x05:
962 device_type = "DFU Lite";
963 break;
964 case 0x06:
965 device_type = "eQUAD step 4 Lite";
966 logi_hidpp_dev_conn_notif_equad(hidpp_report, &workitem);
967 break;
968 case 0x07:
969 device_type = "eQUAD step 4 Gaming";
970 break;
971 case 0x08:
972 device_type = "eQUAD step 4 for gamepads";
973 break;
974 case 0x0a:
975 device_type = "eQUAD nano Lite";
976 logi_hidpp_dev_conn_notif_equad(hidpp_report, &workitem);
977 break;
978 case 0x0c:
979 device_type = "eQUAD Lightspeed";
Benjamin Tissoiresf5fb57a2019-04-20 13:21:55 +0200980 logi_hidpp_dev_conn_notif_equad(hidpp_report, &workitem);
981 workitem.reports_supported |= STD_KEYBOARD;
Hans de Goede74808f92019-04-20 13:21:54 +0200982 break;
983 }
984
985 if (workitem.type == WORKITEM_TYPE_EMPTY) {
986 hid_warn(hdev,
987 "unusable device of type %s (0x%02x) connected on slot %d",
988 device_type,
989 hidpp_report->params[HIDPP_PARAM_PROTO_TYPE],
990 hidpp_report->device_index);
991 return;
992 }
993
994 hid_info(hdev, "device of type %s (0x%02x) connected on slot %d",
995 device_type, hidpp_report->params[HIDPP_PARAM_PROTO_TYPE],
996 hidpp_report->device_index);
997
Hans de Goede74808f92019-04-20 13:21:54 +0200998 kfifo_in(&djrcv_dev->notif_fifo, &workitem, sizeof(workitem));
Hans de Goedee316aa62019-04-20 13:22:01 +0200999 schedule_work(&djrcv_dev->work);
Hans de Goede74808f92019-04-20 13:21:54 +02001000}
1001
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001002static void logi_dj_recv_forward_null_report(struct dj_receiver_dev *djrcv_dev,
1003 struct dj_report *dj_report)
1004{
1005 /* We are called from atomic context (tasklet && djrcv->lock held) */
1006 unsigned int i;
1007 u8 reportbuffer[MAX_REPORT_SIZE];
1008 struct dj_device *djdev;
1009
1010 djdev = djrcv_dev->paired_dj_devices[dj_report->device_index];
1011
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001012 memset(reportbuffer, 0, sizeof(reportbuffer));
1013
1014 for (i = 0; i < NUMBER_OF_HID_REPORTS; i++) {
1015 if (djdev->reports_supported & (1 << i)) {
1016 reportbuffer[0] = i;
1017 if (hid_input_report(djdev->hdev,
1018 HID_INPUT_REPORT,
1019 reportbuffer,
1020 hid_reportid_size_map[i], 1)) {
1021 dbg_hid("hid_input_report error sending null "
1022 "report\n");
1023 }
1024 }
1025 }
1026}
1027
Benjamin Tissoires83898232019-04-20 13:21:43 +02001028static void logi_dj_recv_forward_dj(struct dj_receiver_dev *djrcv_dev,
1029 struct dj_report *dj_report)
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001030{
1031 /* We are called from atomic context (tasklet && djrcv->lock held) */
1032 struct dj_device *dj_device;
1033
1034 dj_device = djrcv_dev->paired_dj_devices[dj_report->device_index];
1035
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001036 if ((dj_report->report_type > ARRAY_SIZE(hid_reportid_size_map) - 1) ||
1037 (hid_reportid_size_map[dj_report->report_type] == 0)) {
1038 dbg_hid("invalid report type:%x\n", dj_report->report_type);
1039 return;
1040 }
1041
1042 if (hid_input_report(dj_device->hdev,
1043 HID_INPUT_REPORT, &dj_report->report_type,
1044 hid_reportid_size_map[dj_report->report_type], 1)) {
1045 dbg_hid("hid_input_report error\n");
1046 }
1047}
1048
Benjamin Tissoires83898232019-04-20 13:21:43 +02001049static void logi_dj_recv_forward_report(struct dj_device *dj_dev, u8 *data,
1050 int size)
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001051{
1052 /* We are called from atomic context (tasklet && djrcv->lock held) */
1053 if (hid_input_report(dj_dev->hdev, HID_INPUT_REPORT, data, size, 1))
1054 dbg_hid("hid_input_report error\n");
1055}
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001056
Hans de Goede74808f92019-04-20 13:21:54 +02001057static void logi_dj_recv_forward_input_report(struct hid_device *hdev,
1058 u8 *data, int size)
1059{
1060 struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
1061 struct dj_device *dj_dev;
1062 unsigned long flags;
1063 u8 report = data[0];
1064 int i;
1065
1066 if (report > REPORT_TYPE_RFREPORT_LAST) {
Colin Ian King640d4ea2019-04-26 14:16:31 +01001067 hid_err(hdev, "Unexpected input report number %d\n", report);
Hans de Goede74808f92019-04-20 13:21:54 +02001068 return;
1069 }
1070
1071 spin_lock_irqsave(&djrcv_dev->lock, flags);
1072 for (i = 0; i < (DJ_MAX_PAIRED_DEVICES + DJ_DEVICE_INDEX_MIN); i++) {
1073 dj_dev = djrcv_dev->paired_dj_devices[i];
1074 if (dj_dev && (dj_dev->reports_supported & BIT(report))) {
1075 logi_dj_recv_forward_report(dj_dev, data, size);
1076 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
1077 return;
1078 }
1079 }
1080
1081 logi_dj_recv_queue_unknown_work(djrcv_dev);
1082 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
1083
1084 dbg_hid("No dj-devs handling input report number %d\n", report);
1085}
1086
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001087static int logi_dj_recv_send_report(struct dj_receiver_dev *djrcv_dev,
1088 struct dj_report *dj_report)
1089{
Hans de Goede0ee75542019-04-20 13:21:51 +02001090 struct hid_device *hdev = djrcv_dev->hidpp;
Benjamin Tissoiresdcd90062013-03-05 17:09:00 +01001091 struct hid_report *report;
1092 struct hid_report_enum *output_report_enum;
1093 u8 *data = (u8 *)(&dj_report->device_index);
Kees Cook297502a2013-09-11 21:56:56 +02001094 unsigned int i;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001095
Benjamin Tissoiresdcd90062013-03-05 17:09:00 +01001096 output_report_enum = &hdev->report_enum[HID_OUTPUT_REPORT];
1097 report = output_report_enum->report_id_hash[REPORT_ID_DJ_SHORT];
1098
1099 if (!report) {
Hans de Goedeaca22a32019-04-20 13:21:58 +02001100 hid_err(hdev, "%s: unable to find dj report\n", __func__);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001101 return -ENODEV;
1102 }
1103
Kees Cook297502a2013-09-11 21:56:56 +02001104 for (i = 0; i < DJREPORT_SHORT_LENGTH - 1; i++)
Benjamin Tissoiresdcd90062013-03-05 17:09:00 +01001105 report->field[0]->value[i] = data[i];
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001106
Jiri Kosina83a44ac2013-03-09 10:58:13 +01001107 hid_hw_request(hdev, report, HID_REQ_SET_REPORT);
Benjamin Tissoiresdcd90062013-03-05 17:09:00 +01001108
1109 return 0;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001110}
1111
Hans de Goede74808f92019-04-20 13:21:54 +02001112static int logi_dj_recv_query_hidpp_devices(struct dj_receiver_dev *djrcv_dev)
1113{
Colin Ian King39d21e72019-05-10 14:10:39 +01001114 static const u8 template[] = {
1115 REPORT_ID_HIDPP_SHORT,
1116 HIDPP_RECEIVER_INDEX,
1117 HIDPP_SET_REGISTER,
1118 HIDPP_REG_CONNECTION_STATE,
1119 HIDPP_FAKE_DEVICE_ARRIVAL,
1120 0x00, 0x00
1121 };
Hans de Goede74808f92019-04-20 13:21:54 +02001122 u8 *hidpp_report;
1123 int retval;
1124
1125 hidpp_report = kmemdup(template, sizeof(template), GFP_KERNEL);
1126 if (!hidpp_report)
1127 return -ENOMEM;
1128
1129 retval = hid_hw_raw_request(djrcv_dev->hidpp,
1130 REPORT_ID_HIDPP_SHORT,
1131 hidpp_report, sizeof(template),
1132 HID_OUTPUT_REPORT,
1133 HID_REQ_SET_REPORT);
1134
1135 kfree(hidpp_report);
1136 return 0;
1137}
1138
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001139static int logi_dj_recv_query_paired_devices(struct dj_receiver_dev *djrcv_dev)
1140{
Marc Dionned8dc3492012-06-01 18:12:14 -04001141 struct dj_report *dj_report;
1142 int retval;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001143
Hans de Goedeb6aeedd2019-04-20 13:21:53 +02001144 djrcv_dev->last_query = jiffies;
1145
Hans de Goede74808f92019-04-20 13:21:54 +02001146 if (djrcv_dev->type != recvr_type_dj)
1147 return logi_dj_recv_query_hidpp_devices(djrcv_dev);
1148
Alan Cox8a55ade2012-09-04 15:10:08 +01001149 dj_report = kzalloc(sizeof(struct dj_report), GFP_KERNEL);
Marc Dionned8dc3492012-06-01 18:12:14 -04001150 if (!dj_report)
1151 return -ENOMEM;
1152 dj_report->report_id = REPORT_ID_DJ_SHORT;
1153 dj_report->device_index = 0xFF;
1154 dj_report->report_type = REPORT_TYPE_CMD_GET_PAIRED_DEVICES;
1155 retval = logi_dj_recv_send_report(djrcv_dev, dj_report);
1156 kfree(dj_report);
1157 return retval;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001158}
1159
Nestor Lopez Casadoc63e0e32013-07-18 06:21:30 -07001160
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001161static int logi_dj_recv_switch_to_dj_mode(struct dj_receiver_dev *djrcv_dev,
1162 unsigned timeout)
1163{
Hans de Goede0ee75542019-04-20 13:21:51 +02001164 struct hid_device *hdev = djrcv_dev->hidpp;
Marc Dionned8dc3492012-06-01 18:12:14 -04001165 struct dj_report *dj_report;
Benjamin Tissoires6a9ddc82014-09-30 13:18:31 -04001166 u8 *buf;
Hans de Goede74808f92019-04-20 13:21:54 +02001167 int retval = 0;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001168
Alan Cox8a55ade2012-09-04 15:10:08 +01001169 dj_report = kzalloc(sizeof(struct dj_report), GFP_KERNEL);
Marc Dionned8dc3492012-06-01 18:12:14 -04001170 if (!dj_report)
1171 return -ENOMEM;
Benjamin Tisssoires42c22db2014-01-08 17:18:45 -05001172
Hans de Goede74808f92019-04-20 13:21:54 +02001173 if (djrcv_dev->type == recvr_type_dj) {
1174 dj_report->report_id = REPORT_ID_DJ_SHORT;
1175 dj_report->device_index = 0xFF;
1176 dj_report->report_type = REPORT_TYPE_CMD_SWITCH;
1177 dj_report->report_params[CMD_SWITCH_PARAM_DEVBITFIELD] = 0x3F;
1178 dj_report->report_params[CMD_SWITCH_PARAM_TIMEOUT_SECONDS] =
1179 (u8)timeout;
1180
1181 retval = logi_dj_recv_send_report(djrcv_dev, dj_report);
1182
1183 /*
1184 * Ugly sleep to work around a USB 3.0 bug when the receiver is
1185 * still processing the "switch-to-dj" command while we send an
1186 * other command.
1187 * 50 msec should gives enough time to the receiver to be ready.
1188 */
1189 msleep(50);
1190 }
Benjamin Tisssoires42c22db2014-01-08 17:18:45 -05001191
Benjamin Tissoires6a9ddc82014-09-30 13:18:31 -04001192 /*
1193 * Magical bits to set up hidpp notifications when the dj devices
1194 * are connected/disconnected.
1195 *
1196 * We can reuse dj_report because HIDPP_REPORT_SHORT_LENGTH is smaller
1197 * than DJREPORT_SHORT_LENGTH.
1198 */
1199 buf = (u8 *)dj_report;
1200
1201 memset(buf, 0, HIDPP_REPORT_SHORT_LENGTH);
1202
1203 buf[0] = REPORT_ID_HIDPP_SHORT;
1204 buf[1] = 0xFF;
1205 buf[2] = 0x80;
1206 buf[3] = 0x00;
1207 buf[4] = 0x00;
1208 buf[5] = 0x09;
1209 buf[6] = 0x00;
1210
1211 hid_hw_raw_request(hdev, REPORT_ID_HIDPP_SHORT, buf,
1212 HIDPP_REPORT_SHORT_LENGTH, HID_OUTPUT_REPORT,
1213 HID_REQ_SET_REPORT);
1214
1215 kfree(dj_report);
Marc Dionned8dc3492012-06-01 18:12:14 -04001216 return retval;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001217}
1218
1219
1220static int logi_dj_ll_open(struct hid_device *hid)
1221{
Hans de Goedeaca22a32019-04-20 13:21:58 +02001222 dbg_hid("%s: %s\n", __func__, hid->phys);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001223 return 0;
1224
1225}
1226
1227static void logi_dj_ll_close(struct hid_device *hid)
1228{
Hans de Goedeaca22a32019-04-20 13:21:58 +02001229 dbg_hid("%s: %s\n", __func__, hid->phys);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001230}
1231
Benjamin Tissoires8dba3022017-03-27 16:59:21 +02001232/*
1233 * Register 0xB5 is "pairing information". It is solely intended for the
1234 * receiver, so do not overwrite the device index.
1235 */
Benjamin Tissoiresc0340412019-04-20 13:21:46 +02001236static u8 unifying_pairing_query[] = { REPORT_ID_HIDPP_SHORT,
1237 HIDPP_RECEIVER_INDEX,
1238 HIDPP_GET_LONG_REGISTER,
1239 HIDPP_REG_PAIRING_INFORMATION };
1240static u8 unifying_pairing_answer[] = { REPORT_ID_HIDPP_LONG,
1241 HIDPP_RECEIVER_INDEX,
1242 HIDPP_GET_LONG_REGISTER,
1243 HIDPP_REG_PAIRING_INFORMATION };
Benjamin Tissoires33797822014-09-30 13:18:30 -04001244
Benjamin Tissoiresbd27e202014-02-10 12:58:53 -05001245static int logi_dj_ll_raw_request(struct hid_device *hid,
1246 unsigned char reportnum, __u8 *buf,
1247 size_t count, unsigned char report_type,
1248 int reqtype)
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001249{
Benjamin Tissoires0e40d352014-02-05 16:33:17 -05001250 struct dj_device *djdev = hid->driver_data;
1251 struct dj_receiver_dev *djrcv_dev = djdev->dj_receiver_dev;
1252 u8 *out_buf;
1253 int ret;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001254
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001255 if ((buf[0] == REPORT_ID_HIDPP_SHORT) ||
1256 (buf[0] == REPORT_ID_HIDPP_LONG)) {
1257 if (count < 2)
1258 return -EINVAL;
1259
Benjamin Tissoires33797822014-09-30 13:18:30 -04001260 /* special case where we should not overwrite
1261 * the device_index */
Benjamin Tissoires8dba3022017-03-27 16:59:21 +02001262 if (count == 7 && !memcmp(buf, unifying_pairing_query,
1263 sizeof(unifying_pairing_query)))
1264 buf[4] = (buf[4] & 0xf0) | (djdev->device_index - 1);
Benjamin Tissoires33797822014-09-30 13:18:30 -04001265 else
1266 buf[1] = djdev->device_index;
Hans de Goede0ee75542019-04-20 13:21:51 +02001267 return hid_hw_raw_request(djrcv_dev->hidpp, reportnum, buf,
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001268 count, report_type, reqtype);
1269 }
1270
Benjamin Tissoires0e40d352014-02-05 16:33:17 -05001271 if (buf[0] != REPORT_TYPE_LEDS)
1272 return -EINVAL;
1273
Hans de Goede74808f92019-04-20 13:21:54 +02001274 if (djrcv_dev->type != recvr_type_dj && count >= 2) {
1275 if (!djrcv_dev->keyboard) {
1276 hid_warn(hid, "Received REPORT_TYPE_LEDS request before the keyboard interface was enumerated\n");
1277 return 0;
1278 }
1279 /* usbhid overrides the report ID and ignores the first byte */
1280 return hid_hw_raw_request(djrcv_dev->keyboard, 0, buf, count,
1281 report_type, reqtype);
1282 }
1283
Benjamin Tissoires0e40d352014-02-05 16:33:17 -05001284 out_buf = kzalloc(DJREPORT_SHORT_LENGTH, GFP_ATOMIC);
1285 if (!out_buf)
1286 return -ENOMEM;
1287
Jiri Kosina51217e62014-08-21 09:56:47 -05001288 if (count > DJREPORT_SHORT_LENGTH - 2)
Benjamin Tissoires0e40d352014-02-05 16:33:17 -05001289 count = DJREPORT_SHORT_LENGTH - 2;
1290
1291 out_buf[0] = REPORT_ID_DJ_SHORT;
1292 out_buf[1] = djdev->device_index;
1293 memcpy(out_buf + 2, buf, count);
1294
Hans de Goede0ee75542019-04-20 13:21:51 +02001295 ret = hid_hw_raw_request(djrcv_dev->hidpp, out_buf[0], out_buf,
Benjamin Tissoiresbd27e202014-02-10 12:58:53 -05001296 DJREPORT_SHORT_LENGTH, report_type, reqtype);
Benjamin Tissoires0e40d352014-02-05 16:33:17 -05001297
1298 kfree(out_buf);
1299 return ret;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001300}
1301
Dan Carpenter6d603322013-10-19 12:08:59 +03001302static void rdcat(char *rdesc, unsigned int *rsize, const char *data, unsigned int size)
Henrik Rydberg2a039bf2012-04-22 14:21:39 +02001303{
Dan Carpenter6d603322013-10-19 12:08:59 +03001304 memcpy(rdesc + *rsize, data, size);
Henrik Rydberg2a039bf2012-04-22 14:21:39 +02001305 *rsize += size;
1306}
1307
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001308static int logi_dj_ll_parse(struct hid_device *hid)
1309{
1310 struct dj_device *djdev = hid->driver_data;
Henrik Rydberg2a039bf2012-04-22 14:21:39 +02001311 unsigned int rsize = 0;
1312 char *rdesc;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001313 int retval;
1314
1315 dbg_hid("%s\n", __func__);
1316
1317 djdev->hdev->version = 0x0111;
1318 djdev->hdev->country = 0x00;
1319
Henrik Rydberg2a039bf2012-04-22 14:21:39 +02001320 rdesc = kmalloc(MAX_RDESC_SIZE, GFP_KERNEL);
1321 if (!rdesc)
1322 return -ENOMEM;
1323
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001324 if (djdev->reports_supported & STD_KEYBOARD) {
Hans de Goede6d3c3f02019-04-20 13:22:02 +02001325 dbg_hid("%s: sending a kbd descriptor, reports_supported: %llx\n",
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001326 __func__, djdev->reports_supported);
Dan Carpenter6d603322013-10-19 12:08:59 +03001327 rdcat(rdesc, &rsize, kbd_descriptor, sizeof(kbd_descriptor));
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001328 }
1329
1330 if (djdev->reports_supported & STD_MOUSE) {
Hans de Goede6d3c3f02019-04-20 13:22:02 +02001331 dbg_hid("%s: sending a mouse descriptor, reports_supported: %llx\n",
1332 __func__, djdev->reports_supported);
Benjamin Tissoiresf5fb57a2019-04-20 13:21:55 +02001333 if (djdev->dj_receiver_dev->type == recvr_type_gaming_hidpp)
1334 rdcat(rdesc, &rsize, mse_high_res_descriptor,
1335 sizeof(mse_high_res_descriptor));
Hans de Goedec9121cf2019-04-20 13:21:56 +02001336 else if (djdev->dj_receiver_dev->type == recvr_type_27mhz)
1337 rdcat(rdesc, &rsize, mse_27mhz_descriptor,
1338 sizeof(mse_27mhz_descriptor));
Hans de Goedef2113c32019-04-20 13:22:03 +02001339 else if (djdev->dj_receiver_dev->type == recvr_type_bluetooth)
1340 rdcat(rdesc, &rsize, mse_bluetooth_descriptor,
1341 sizeof(mse_bluetooth_descriptor));
Benjamin Tissoiresf5fb57a2019-04-20 13:21:55 +02001342 else
1343 rdcat(rdesc, &rsize, mse_descriptor,
1344 sizeof(mse_descriptor));
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001345 }
1346
1347 if (djdev->reports_supported & MULTIMEDIA) {
Hans de Goede6d3c3f02019-04-20 13:22:02 +02001348 dbg_hid("%s: sending a multimedia report descriptor: %llx\n",
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001349 __func__, djdev->reports_supported);
Dan Carpenter6d603322013-10-19 12:08:59 +03001350 rdcat(rdesc, &rsize, consumer_descriptor, sizeof(consumer_descriptor));
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001351 }
1352
1353 if (djdev->reports_supported & POWER_KEYS) {
Hans de Goede6d3c3f02019-04-20 13:22:02 +02001354 dbg_hid("%s: sending a power keys report descriptor: %llx\n",
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001355 __func__, djdev->reports_supported);
Dan Carpenter6d603322013-10-19 12:08:59 +03001356 rdcat(rdesc, &rsize, syscontrol_descriptor, sizeof(syscontrol_descriptor));
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001357 }
1358
1359 if (djdev->reports_supported & MEDIA_CENTER) {
Hans de Goede6d3c3f02019-04-20 13:22:02 +02001360 dbg_hid("%s: sending a media center report descriptor: %llx\n",
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001361 __func__, djdev->reports_supported);
Dan Carpenter6d603322013-10-19 12:08:59 +03001362 rdcat(rdesc, &rsize, media_descriptor, sizeof(media_descriptor));
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001363 }
1364
1365 if (djdev->reports_supported & KBD_LEDS) {
Hans de Goede6d3c3f02019-04-20 13:22:02 +02001366 dbg_hid("%s: need to send kbd leds report descriptor: %llx\n",
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001367 __func__, djdev->reports_supported);
1368 }
1369
Hans de Goede6d3c3f02019-04-20 13:22:02 +02001370 if (djdev->reports_supported & HIDPP) {
1371 rdcat(rdesc, &rsize, hidpp_descriptor,
1372 sizeof(hidpp_descriptor));
1373 }
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001374
Henrik Rydberg2a039bf2012-04-22 14:21:39 +02001375 retval = hid_parse_report(hid, rdesc, rsize);
1376 kfree(rdesc);
1377
1378 return retval;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001379}
1380
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001381static int logi_dj_ll_start(struct hid_device *hid)
1382{
1383 dbg_hid("%s\n", __func__);
1384 return 0;
1385}
1386
1387static void logi_dj_ll_stop(struct hid_device *hid)
1388{
1389 dbg_hid("%s\n", __func__);
1390}
1391
1392
1393static struct hid_ll_driver logi_dj_ll_driver = {
1394 .parse = logi_dj_ll_parse,
1395 .start = logi_dj_ll_start,
1396 .stop = logi_dj_ll_stop,
1397 .open = logi_dj_ll_open,
1398 .close = logi_dj_ll_close,
Benjamin Tissoiresbd27e202014-02-10 12:58:53 -05001399 .raw_request = logi_dj_ll_raw_request,
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001400};
1401
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001402static int logi_dj_dj_event(struct hid_device *hdev,
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001403 struct hid_report *report, u8 *data,
1404 int size)
1405{
1406 struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
1407 struct dj_report *dj_report = (struct dj_report *) data;
1408 unsigned long flags;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001409
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001410 /*
1411 * Here we receive all data coming from iface 2, there are 3 cases:
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001412 *
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001413 * 1) Data is intended for this driver i. e. data contains arrival,
1414 * departure, etc notifications, in which case we queue them for delayed
1415 * processing by the work queue. We return 1 to hid-core as no further
1416 * processing is required from it.
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001417 *
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001418 * 2) Data informs a connection change, if the change means rf link
1419 * loss, then we must send a null report to the upper layer to discard
1420 * potentially pressed keys that may be repeated forever by the input
1421 * layer. Return 1 to hid-core as no further processing is required.
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001422 *
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001423 * 3) Data is an actual input event from a paired DJ device in which
1424 * case we forward it to the correct hid device (via hid_input_report()
1425 * ) and return 1 so hid-core does not anything else with it.
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001426 */
Benjamin Tissoires5abfe852014-08-22 16:16:05 -04001427
Jiri Kosinaad3e14d2014-08-21 09:57:17 -05001428 if ((dj_report->device_index < DJ_DEVICE_INDEX_MIN) ||
1429 (dj_report->device_index > DJ_DEVICE_INDEX_MAX)) {
Benjamin Tissoires5abfe852014-08-22 16:16:05 -04001430 /*
1431 * Device index is wrong, bail out.
1432 * This driver can ignore safely the receiver notifications,
1433 * so ignore those reports too.
1434 */
1435 if (dj_report->device_index != DJ_RECEIVER_INDEX)
Hans de Goedeaca22a32019-04-20 13:21:58 +02001436 hid_err(hdev, "%s: invalid device index:%d\n",
Jiri Kosinaad3e14d2014-08-21 09:57:17 -05001437 __func__, dj_report->device_index);
1438 return false;
1439 }
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001440
1441 spin_lock_irqsave(&djrcv_dev->lock, flags);
Benjamin Tissoires368d4e592014-08-22 16:16:06 -04001442
1443 if (!djrcv_dev->paired_dj_devices[dj_report->device_index]) {
1444 /* received an event for an unknown device, bail out */
1445 logi_dj_recv_queue_notification(djrcv_dev, dj_report);
1446 goto out;
1447 }
1448
Benjamin Tissoires5abfe852014-08-22 16:16:05 -04001449 switch (dj_report->report_type) {
1450 case REPORT_TYPE_NOTIF_DEVICE_PAIRED:
Benjamin Tissoires368d4e592014-08-22 16:16:06 -04001451 /* pairing notifications are handled above the switch */
1452 break;
Benjamin Tissoires5abfe852014-08-22 16:16:05 -04001453 case REPORT_TYPE_NOTIF_DEVICE_UNPAIRED:
1454 logi_dj_recv_queue_notification(djrcv_dev, dj_report);
1455 break;
1456 case REPORT_TYPE_NOTIF_CONNECTION_STATUS:
1457 if (dj_report->report_params[CONNECTION_STATUS_PARAM_STATUS] ==
1458 STATUS_LINKLOSS) {
1459 logi_dj_recv_forward_null_report(djrcv_dev, dj_report);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001460 }
Benjamin Tissoires5abfe852014-08-22 16:16:05 -04001461 break;
1462 default:
Benjamin Tissoires83898232019-04-20 13:21:43 +02001463 logi_dj_recv_forward_dj(djrcv_dev, dj_report);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001464 }
Benjamin Tissoires368d4e592014-08-22 16:16:06 -04001465
1466out:
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001467 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
1468
Benjamin Tissoires5abfe852014-08-22 16:16:05 -04001469 return true;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001470}
1471
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001472static int logi_dj_hidpp_event(struct hid_device *hdev,
1473 struct hid_report *report, u8 *data,
1474 int size)
1475{
1476 struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
Benjamin Tissoires7bb56a52019-04-20 13:21:44 +02001477 struct hidpp_event *hidpp_report = (struct hidpp_event *) data;
Hans de Goede74808f92019-04-20 13:21:54 +02001478 struct dj_device *dj_dev;
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001479 unsigned long flags;
Benjamin Tissoires7bb56a52019-04-20 13:21:44 +02001480 u8 device_index = hidpp_report->device_index;
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001481
Benjamin Tissoires33797822014-09-30 13:18:30 -04001482 if (device_index == HIDPP_RECEIVER_INDEX) {
1483 /* special case were the device wants to know its unifying
1484 * name */
1485 if (size == HIDPP_REPORT_LONG_LENGTH &&
Benjamin Tissoires8dba3022017-03-27 16:59:21 +02001486 !memcmp(data, unifying_pairing_answer,
1487 sizeof(unifying_pairing_answer)))
Benjamin Tissoires33797822014-09-30 13:18:30 -04001488 device_index = (data[4] & 0x0F) + 1;
1489 else
1490 return false;
1491 }
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001492
1493 /*
1494 * Data is from the HID++ collection, in this case, we forward the
1495 * data to the corresponding child dj device and return 0 to hid-core
1496 * so he data also goes to the hidraw device of the receiver. This
1497 * allows a user space application to implement the full HID++ routing
1498 * via the receiver.
1499 */
1500
1501 if ((device_index < DJ_DEVICE_INDEX_MIN) ||
1502 (device_index > DJ_DEVICE_INDEX_MAX)) {
1503 /*
1504 * Device index is wrong, bail out.
1505 * This driver can ignore safely the receiver notifications,
1506 * so ignore those reports too.
1507 */
Hans de Goedeaca22a32019-04-20 13:21:58 +02001508 hid_err(hdev, "%s: invalid device index:%d\n", __func__,
1509 hidpp_report->device_index);
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001510 return false;
1511 }
1512
1513 spin_lock_irqsave(&djrcv_dev->lock, flags);
1514
Hans de Goede74808f92019-04-20 13:21:54 +02001515 dj_dev = djrcv_dev->paired_dj_devices[device_index];
Hans de Goedec9121cf2019-04-20 13:21:56 +02001516
1517 /*
1518 * With 27 MHz receivers, we do not get an explicit unpair event,
1519 * remove the old device if the user has paired a *different* device.
1520 */
1521 if (djrcv_dev->type == recvr_type_27mhz && dj_dev &&
1522 hidpp_report->sub_id == REPORT_TYPE_NOTIF_DEVICE_CONNECTED &&
1523 hidpp_report->params[HIDPP_PARAM_PROTO_TYPE] == 0x02 &&
1524 hidpp_report->params[HIDPP_PARAM_27MHZ_DEVID] !=
1525 dj_dev->hdev->product) {
1526 struct dj_workitem workitem = {
1527 .device_index = hidpp_report->device_index,
1528 .type = WORKITEM_TYPE_UNPAIRED,
1529 };
1530 kfifo_in(&djrcv_dev->notif_fifo, &workitem, sizeof(workitem));
1531 /* logi_hidpp_recv_queue_notif will queue the work */
1532 dj_dev = NULL;
1533 }
1534
Hans de Goede74808f92019-04-20 13:21:54 +02001535 if (dj_dev) {
1536 logi_dj_recv_forward_report(dj_dev, data, size);
1537 } else {
1538 if (hidpp_report->sub_id == REPORT_TYPE_NOTIF_DEVICE_CONNECTED)
1539 logi_hidpp_recv_queue_notif(hdev, hidpp_report);
1540 else
1541 logi_dj_recv_queue_unknown_work(djrcv_dev);
1542 }
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001543
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001544 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
1545
1546 return false;
1547}
1548
1549static int logi_dj_raw_event(struct hid_device *hdev,
1550 struct hid_report *report, u8 *data,
1551 int size)
1552{
Hans de Goede74808f92019-04-20 13:21:54 +02001553 struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001554 dbg_hid("%s, size:%d\n", __func__, size);
1555
Hans de Goededa12b222019-04-20 13:21:59 +02001556 if (!djrcv_dev)
1557 return 0;
1558
Hans de Goede74808f92019-04-20 13:21:54 +02001559 if (!hdev->report_enum[HID_INPUT_REPORT].numbered) {
1560
1561 if (djrcv_dev->unnumbered_application == HID_GD_KEYBOARD) {
1562 /*
1563 * For the keyboard, we can reuse the same report by
1564 * using the second byte which is constant in the USB
1565 * HID report descriptor.
1566 */
1567 data[1] = data[0];
1568 data[0] = REPORT_TYPE_KEYBOARD;
1569
1570 logi_dj_recv_forward_input_report(hdev, data, size);
1571
1572 /* restore previous state */
1573 data[0] = data[1];
1574 data[1] = 0;
1575 }
Hans de Goede1f944ac2019-04-20 13:21:57 +02001576 /* The 27 MHz mouse-only receiver sends unnumbered mouse data */
1577 if (djrcv_dev->unnumbered_application == HID_GD_MOUSE &&
1578 size == 6) {
1579 u8 mouse_report[7];
1580
1581 /* Prepend report id */
1582 mouse_report[0] = REPORT_TYPE_MOUSE;
1583 memcpy(mouse_report + 1, data, 6);
1584 logi_dj_recv_forward_input_report(hdev, mouse_report, 7);
1585 }
Hans de Goede74808f92019-04-20 13:21:54 +02001586
1587 return false;
1588 }
1589
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001590 switch (data[0]) {
1591 case REPORT_ID_DJ_SHORT:
Peter Wuf254ae92014-12-16 16:55:21 +01001592 if (size != DJREPORT_SHORT_LENGTH) {
Hans de Goedeaca22a32019-04-20 13:21:58 +02001593 hid_err(hdev, "Short DJ report bad size (%d)", size);
Peter Wuf254ae92014-12-16 16:55:21 +01001594 return false;
1595 }
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001596 return logi_dj_dj_event(hdev, report, data, size);
Benjamin Tissoiresf5fb57a2019-04-20 13:21:55 +02001597 case REPORT_ID_DJ_LONG:
1598 if (size != DJREPORT_LONG_LENGTH) {
Hans de Goedeaca22a32019-04-20 13:21:58 +02001599 hid_err(hdev, "Long DJ report bad size (%d)", size);
Benjamin Tissoiresf5fb57a2019-04-20 13:21:55 +02001600 return false;
1601 }
1602 return logi_dj_dj_event(hdev, report, data, size);
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001603 case REPORT_ID_HIDPP_SHORT:
Peter Wuf254ae92014-12-16 16:55:21 +01001604 if (size != HIDPP_REPORT_SHORT_LENGTH) {
Hans de Goedeaca22a32019-04-20 13:21:58 +02001605 hid_err(hdev, "Short HID++ report bad size (%d)", size);
Peter Wuf254ae92014-12-16 16:55:21 +01001606 return false;
1607 }
1608 return logi_dj_hidpp_event(hdev, report, data, size);
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001609 case REPORT_ID_HIDPP_LONG:
Peter Wuf254ae92014-12-16 16:55:21 +01001610 if (size != HIDPP_REPORT_LONG_LENGTH) {
Hans de Goedeaca22a32019-04-20 13:21:58 +02001611 hid_err(hdev, "Long HID++ report bad size (%d)", size);
Peter Wuf254ae92014-12-16 16:55:21 +01001612 return false;
1613 }
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001614 return logi_dj_hidpp_event(hdev, report, data, size);
1615 }
1616
Hans de Goede74808f92019-04-20 13:21:54 +02001617 logi_dj_recv_forward_input_report(hdev, data, size);
1618
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001619 return false;
1620}
1621
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001622static int logi_dj_probe(struct hid_device *hdev,
1623 const struct hid_device_id *id)
1624{
Benjamin Tissoires82c0beb2019-04-20 13:21:47 +02001625 struct hid_report_enum *rep_enum;
1626 struct hid_report *rep;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001627 struct dj_receiver_dev *djrcv_dev;
Hans de Goededa12b222019-04-20 13:21:59 +02001628 struct usb_interface *intf;
1629 unsigned int no_dj_interfaces = 0;
Benjamin Tissoires82c0beb2019-04-20 13:21:47 +02001630 bool has_hidpp = false;
Hans de Goedea1d97cc2019-04-20 13:21:52 +02001631 unsigned long flags;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001632 int retval;
1633
Benjamin Tissoires82c0beb2019-04-20 13:21:47 +02001634 /*
1635 * Call to usbhid to fetch the HID descriptors of the current
1636 * interface subsequently call to the hid/hid-core to parse the
1637 * fetched descriptors.
1638 */
1639 retval = hid_parse(hdev);
1640 if (retval) {
Hans de Goedeaca22a32019-04-20 13:21:58 +02001641 hid_err(hdev, "%s: parse failed\n", __func__);
Benjamin Tissoires82c0beb2019-04-20 13:21:47 +02001642 return retval;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001643 }
1644
Hans de Goededa12b222019-04-20 13:21:59 +02001645 /*
1646 * Some KVMs add an extra interface for e.g. mouse emulation. If we
1647 * treat these as logitech-dj interfaces then this causes input events
1648 * reported through this extra interface to not be reported correctly.
1649 * To avoid this, we treat these as generic-hid devices.
1650 */
1651 switch (id->driver_data) {
1652 case recvr_type_dj: no_dj_interfaces = 3; break;
1653 case recvr_type_hidpp: no_dj_interfaces = 2; break;
1654 case recvr_type_gaming_hidpp: no_dj_interfaces = 3; break;
1655 case recvr_type_27mhz: no_dj_interfaces = 2; break;
Hans de Goedef2113c32019-04-20 13:22:03 +02001656 case recvr_type_bluetooth: no_dj_interfaces = 2; break;
Hans de Goededa12b222019-04-20 13:21:59 +02001657 }
1658 if (hid_is_using_ll_driver(hdev, &usb_hid_driver)) {
1659 intf = to_usb_interface(hdev->dev.parent);
1660 if (intf && intf->altsetting->desc.bInterfaceNumber >=
1661 no_dj_interfaces) {
1662 hdev->quirks |= HID_QUIRK_INPUT_PER_APP;
1663 return hid_hw_start(hdev, HID_CONNECT_DEFAULT);
1664 }
1665 }
1666
Benjamin Tissoires82c0beb2019-04-20 13:21:47 +02001667 rep_enum = &hdev->report_enum[HID_INPUT_REPORT];
1668
Hans de Goede74808f92019-04-20 13:21:54 +02001669 /* no input reports, bail out */
1670 if (list_empty(&rep_enum->report_list))
1671 return -ENODEV;
1672
Benjamin Tissoires82c0beb2019-04-20 13:21:47 +02001673 /*
1674 * Check for the HID++ application.
1675 * Note: we should theoretically check for HID++ and DJ
1676 * collections, but this will do.
1677 */
1678 list_for_each_entry(rep, &rep_enum->report_list, list) {
1679 if (rep->application == 0xff000001)
1680 has_hidpp = true;
1681 }
1682
1683 /*
1684 * Ignore interfaces without DJ/HID++ collection, they will not carry
1685 * any data, dont create any hid_device for them.
1686 */
Hans de Goede74808f92019-04-20 13:21:54 +02001687 if (!has_hidpp && id->driver_data == recvr_type_dj)
Benjamin Tissoires82c0beb2019-04-20 13:21:47 +02001688 return -ENODEV;
1689
Hans de Goedea1d97cc2019-04-20 13:21:52 +02001690 /* get the current application attached to the node */
1691 rep = list_first_entry(&rep_enum->report_list, struct hid_report, list);
Hans de Goede74808f92019-04-20 13:21:54 +02001692 djrcv_dev = dj_get_receiver_dev(hdev, id->driver_data,
Hans de Goedea1d97cc2019-04-20 13:21:52 +02001693 rep->application, has_hidpp);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001694 if (!djrcv_dev) {
Hans de Goedeaca22a32019-04-20 13:21:58 +02001695 hid_err(hdev, "%s: dj_get_receiver_dev failed\n", __func__);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001696 return -ENOMEM;
1697 }
Kees Cook297502a2013-09-11 21:56:56 +02001698
Hans de Goede74808f92019-04-20 13:21:54 +02001699 if (!rep_enum->numbered)
1700 djrcv_dev->unnumbered_application = rep->application;
1701
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001702 /* Starts the usb device and connects to upper interfaces hiddev and
1703 * hidraw */
Hans de Goede74808f92019-04-20 13:21:54 +02001704 retval = hid_hw_start(hdev, HID_CONNECT_HIDRAW|HID_CONNECT_HIDDEV);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001705 if (retval) {
Hans de Goedeaca22a32019-04-20 13:21:58 +02001706 hid_err(hdev, "%s: hid_hw_start returned error\n", __func__);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001707 goto hid_hw_start_fail;
1708 }
1709
Hans de Goede74808f92019-04-20 13:21:54 +02001710 if (has_hidpp) {
1711 retval = logi_dj_recv_switch_to_dj_mode(djrcv_dev, 0);
1712 if (retval < 0) {
1713 hid_err(hdev, "%s: logi_dj_recv_switch_to_dj_mode returned error:%d\n",
1714 __func__, retval);
1715 goto switch_to_dj_mode_fail;
1716 }
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001717 }
1718
1719 /* This is enabling the polling urb on the IN endpoint */
Benjamin Tissoiresddf75402013-07-12 11:01:02 +02001720 retval = hid_hw_open(hdev);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001721 if (retval < 0) {
Hans de Goedeaca22a32019-04-20 13:21:58 +02001722 hid_err(hdev, "%s: hid_hw_open returned error:%d\n",
Benjamin Tissoiresddf75402013-07-12 11:01:02 +02001723 __func__, retval);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001724 goto llopen_failed;
1725 }
1726
Andrew de los Reyesa9dd22b72013-02-18 09:20:22 -08001727 /* Allow incoming packets to arrive: */
1728 hid_device_io_start(hdev);
1729
Hans de Goede74808f92019-04-20 13:21:54 +02001730 if (has_hidpp) {
1731 spin_lock_irqsave(&djrcv_dev->lock, flags);
1732 djrcv_dev->ready = true;
1733 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
1734 retval = logi_dj_recv_query_paired_devices(djrcv_dev);
1735 if (retval < 0) {
1736 hid_err(hdev, "%s: logi_dj_recv_query_paired_devices error:%d\n",
1737 __func__, retval);
1738 goto logi_dj_recv_query_paired_devices_failed;
1739 }
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001740 }
1741
1742 return retval;
1743
1744logi_dj_recv_query_paired_devices_failed:
Benjamin Tissoiresddf75402013-07-12 11:01:02 +02001745 hid_hw_close(hdev);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001746
1747llopen_failed:
1748switch_to_dj_mode_fail:
1749 hid_hw_stop(hdev);
1750
1751hid_hw_start_fail:
Hans de Goedea1d97cc2019-04-20 13:21:52 +02001752 dj_put_receiver_dev(hdev);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001753 return retval;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001754}
1755
1756#ifdef CONFIG_PM
1757static int logi_dj_reset_resume(struct hid_device *hdev)
1758{
1759 int retval;
1760 struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
1761
Hans de Goededa12b222019-04-20 13:21:59 +02001762 if (!djrcv_dev || djrcv_dev->hidpp != hdev)
Hans de Goede74808f92019-04-20 13:21:54 +02001763 return 0;
1764
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001765 retval = logi_dj_recv_switch_to_dj_mode(djrcv_dev, 0);
1766 if (retval < 0) {
Hans de Goedeaca22a32019-04-20 13:21:58 +02001767 hid_err(hdev, "%s: logi_dj_recv_switch_to_dj_mode returned error:%d\n",
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001768 __func__, retval);
1769 }
1770
1771 return 0;
1772}
1773#endif
1774
1775static void logi_dj_remove(struct hid_device *hdev)
1776{
1777 struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
1778 struct dj_device *dj_dev;
Hans de Goedea1d97cc2019-04-20 13:21:52 +02001779 unsigned long flags;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001780 int i;
1781
1782 dbg_hid("%s\n", __func__);
1783
Hans de Goededa12b222019-04-20 13:21:59 +02001784 if (!djrcv_dev)
1785 return hid_hw_stop(hdev);
1786
Hans de Goedea1d97cc2019-04-20 13:21:52 +02001787 /*
1788 * This ensures that if the work gets requeued from another
1789 * interface of the same receiver it will be a no-op.
1790 */
1791 spin_lock_irqsave(&djrcv_dev->lock, flags);
1792 djrcv_dev->ready = false;
1793 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
1794
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001795 cancel_work_sync(&djrcv_dev->work);
1796
Benjamin Tissoiresddf75402013-07-12 11:01:02 +02001797 hid_hw_close(hdev);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001798 hid_hw_stop(hdev);
1799
Hans de Goedea1d97cc2019-04-20 13:21:52 +02001800 /*
1801 * For proper operation we need access to all interfaces, so we destroy
1802 * the paired devices when we're unbound from any interface.
1803 *
1804 * Note we may still be bound to other interfaces, sharing the same
1805 * djrcv_dev, so we need locking here.
1806 */
Nestor Lopez Casado844580f2011-09-20 15:59:03 +02001807 for (i = 0; i < (DJ_MAX_PAIRED_DEVICES + DJ_DEVICE_INDEX_MIN); i++) {
Hans de Goedea1d97cc2019-04-20 13:21:52 +02001808 spin_lock_irqsave(&djrcv_dev->lock, flags);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001809 dj_dev = djrcv_dev->paired_dj_devices[i];
Hans de Goedea1d97cc2019-04-20 13:21:52 +02001810 djrcv_dev->paired_dj_devices[i] = NULL;
1811 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001812 if (dj_dev != NULL) {
1813 hid_destroy_device(dj_dev->hdev);
1814 kfree(dj_dev);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001815 }
1816 }
1817
Hans de Goedea1d97cc2019-04-20 13:21:52 +02001818 dj_put_receiver_dev(hdev);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001819}
1820
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001821static const struct hid_device_id logi_dj_receivers[] = {
1822 {HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
Hans de Goede74808f92019-04-20 13:21:54 +02001823 USB_DEVICE_ID_LOGITECH_UNIFYING_RECEIVER),
1824 .driver_data = recvr_type_dj},
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001825 {HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
Hans de Goede74808f92019-04-20 13:21:54 +02001826 USB_DEVICE_ID_LOGITECH_UNIFYING_RECEIVER_2),
1827 .driver_data = recvr_type_dj},
1828 { /* Logitech Nano (non DJ) receiver */
1829 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
1830 USB_DEVICE_ID_LOGITECH_NANO_RECEIVER),
1831 .driver_data = recvr_type_hidpp},
1832 { /* Logitech Nano (non DJ) receiver */
1833 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
1834 USB_DEVICE_ID_LOGITECH_NANO_RECEIVER_2),
1835 .driver_data = recvr_type_hidpp},
Benjamin Tissoiresf5fb57a2019-04-20 13:21:55 +02001836 { /* Logitech gaming receiver (0xc539) */
1837 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
1838 USB_DEVICE_ID_LOGITECH_NANO_RECEIVER_GAMING),
1839 .driver_data = recvr_type_gaming_hidpp},
Hans de Goede423dfbc2019-05-09 13:47:04 +02001840 { /* Logitech 27 MHz HID++ 1.0 receiver (0xc513) */
1841 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_MX3000_RECEIVER),
1842 .driver_data = recvr_type_27mhz},
Hans de Goedec9121cf2019-04-20 13:21:56 +02001843 { /* Logitech 27 MHz HID++ 1.0 receiver (0xc517) */
1844 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
1845 USB_DEVICE_ID_S510_RECEIVER_2),
1846 .driver_data = recvr_type_27mhz},
Hans de Goede1f944ac2019-04-20 13:21:57 +02001847 { /* Logitech 27 MHz HID++ 1.0 mouse-only receiver (0xc51b) */
1848 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
1849 USB_DEVICE_ID_LOGITECH_27MHZ_MOUSE_RECEIVER),
1850 .driver_data = recvr_type_27mhz},
Hans de Goedef2113c32019-04-20 13:22:03 +02001851 { /* Logitech MX5000 HID++ / bluetooth receiver keyboard intf. */
1852 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
1853 0xc70e),
1854 .driver_data = recvr_type_bluetooth},
1855 { /* Logitech MX5000 HID++ / bluetooth receiver mouse intf. */
1856 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
1857 0xc70a),
1858 .driver_data = recvr_type_bluetooth},
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001859 {}
1860};
1861
1862MODULE_DEVICE_TABLE(hid, logi_dj_receivers);
1863
1864static struct hid_driver logi_djreceiver_driver = {
1865 .name = "logitech-djreceiver",
1866 .id_table = logi_dj_receivers,
1867 .probe = logi_dj_probe,
1868 .remove = logi_dj_remove,
1869 .raw_event = logi_dj_raw_event,
1870#ifdef CONFIG_PM
1871 .reset_resume = logi_dj_reset_resume,
1872#endif
1873};
1874
Benjamin Tissoiresab94e562014-09-30 13:18:28 -04001875module_hid_driver(logi_djreceiver_driver);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001876
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001877MODULE_LICENSE("GPL");
1878MODULE_AUTHOR("Logitech");
1879MODULE_AUTHOR("Nestor Lopez Casado");
1880MODULE_AUTHOR("nlopezcasad@logitech.com");