blob: d880ce6413457049094a0b6a4476479681da05ff [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>
Jonathan Nieder44d27f72012-05-11 16:17:16 +020030#include <asm/unaligned.h>
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +020031#include "hid-ids.h"
Benjamin Tissoires8cb37462014-09-30 13:18:26 -040032
33#define DJ_MAX_PAIRED_DEVICES 6
Benjamin Tissoires4fcad952019-04-20 13:21:48 +020034#define DJ_MAX_NUMBER_NOTIFS 8
Benjamin Tissoires8cb37462014-09-30 13:18:26 -040035#define DJ_RECEIVER_INDEX 0
36#define DJ_DEVICE_INDEX_MIN 1
37#define DJ_DEVICE_INDEX_MAX 6
38
39#define DJREPORT_SHORT_LENGTH 15
40#define DJREPORT_LONG_LENGTH 32
41
42#define REPORT_ID_DJ_SHORT 0x20
43#define REPORT_ID_DJ_LONG 0x21
44
Benjamin Tissoires925f0f32014-09-30 13:18:29 -040045#define REPORT_ID_HIDPP_SHORT 0x10
46#define REPORT_ID_HIDPP_LONG 0x11
47
48#define HIDPP_REPORT_SHORT_LENGTH 7
49#define HIDPP_REPORT_LONG_LENGTH 20
50
51#define HIDPP_RECEIVER_INDEX 0xff
52
53#define REPORT_TYPE_RFREPORT_FIRST 0x01
Benjamin Tissoires8cb37462014-09-30 13:18:26 -040054#define REPORT_TYPE_RFREPORT_LAST 0x1F
55
56/* Command Switch to DJ mode */
57#define REPORT_TYPE_CMD_SWITCH 0x80
58#define CMD_SWITCH_PARAM_DEVBITFIELD 0x00
59#define CMD_SWITCH_PARAM_TIMEOUT_SECONDS 0x01
60#define TIMEOUT_NO_KEEPALIVE 0x00
61
62/* Command to Get the list of Paired devices */
63#define REPORT_TYPE_CMD_GET_PAIRED_DEVICES 0x81
64
65/* Device Paired Notification */
66#define REPORT_TYPE_NOTIF_DEVICE_PAIRED 0x41
67#define SPFUNCTION_MORE_NOTIF_EXPECTED 0x01
68#define SPFUNCTION_DEVICE_LIST_EMPTY 0x02
69#define DEVICE_PAIRED_PARAM_SPFUNCTION 0x00
70#define DEVICE_PAIRED_PARAM_EQUAD_ID_LSB 0x01
71#define DEVICE_PAIRED_PARAM_EQUAD_ID_MSB 0x02
72#define DEVICE_PAIRED_RF_REPORT_TYPE 0x03
73
74/* Device Un-Paired Notification */
75#define REPORT_TYPE_NOTIF_DEVICE_UNPAIRED 0x40
76
Benjamin Tissoires8cb37462014-09-30 13:18:26 -040077/* Connection Status Notification */
78#define REPORT_TYPE_NOTIF_CONNECTION_STATUS 0x42
79#define CONNECTION_STATUS_PARAM_STATUS 0x00
80#define STATUS_LINKLOSS 0x01
81
82/* Error Notification */
83#define REPORT_TYPE_NOTIF_ERROR 0x7F
84#define NOTIF_ERROR_PARAM_ETYPE 0x00
85#define ETYPE_KEEPALIVE_TIMEOUT 0x01
86
87/* supported DJ HID && RF report types */
88#define REPORT_TYPE_KEYBOARD 0x01
89#define REPORT_TYPE_MOUSE 0x02
90#define REPORT_TYPE_CONSUMER_CONTROL 0x03
91#define REPORT_TYPE_SYSTEM_CONTROL 0x04
92#define REPORT_TYPE_MEDIA_CENTER 0x08
93#define REPORT_TYPE_LEDS 0x0E
94
95/* RF Report types bitfield */
Benjamin Tissoiresa17dd1f2019-04-20 13:21:45 +020096#define STD_KEYBOARD BIT(1)
97#define STD_MOUSE BIT(2)
98#define MULTIMEDIA BIT(3)
99#define POWER_KEYS BIT(4)
100#define MEDIA_CENTER BIT(8)
101#define KBD_LEDS BIT(14)
Benjamin Tissoires8cb37462014-09-30 13:18:26 -0400102
Hans de Goede74808f92019-04-20 13:21:54 +0200103/* HID++ Device Connected Notification */
104#define REPORT_TYPE_NOTIF_DEVICE_CONNECTED 0x41
105#define HIDPP_PARAM_PROTO_TYPE 0x00
106#define HIDPP_PARAM_DEVICE_INFO 0x01
107#define HIDPP_PARAM_EQUAD_LSB 0x02
108#define HIDPP_PARAM_EQUAD_MSB 0x03
109#define HIDPP_DEVICE_TYPE_MASK GENMASK(3, 0)
110#define HIDPP_LINK_STATUS_MASK BIT(6)
111
112#define HIDPP_SET_REGISTER 0x80
Benjamin Tissoiresc0340412019-04-20 13:21:46 +0200113#define HIDPP_GET_LONG_REGISTER 0x83
Hans de Goede74808f92019-04-20 13:21:54 +0200114#define HIDPP_REG_CONNECTION_STATE 0x02
Benjamin Tissoiresc0340412019-04-20 13:21:46 +0200115#define HIDPP_REG_PAIRING_INFORMATION 0xB5
116#define HIDPP_PAIRING_INFORMATION 0x20
Hans de Goede74808f92019-04-20 13:21:54 +0200117#define HIDPP_FAKE_DEVICE_ARRIVAL 0x02
118
119enum recvr_type {
120 recvr_type_dj,
121 recvr_type_hidpp,
122};
Benjamin Tissoiresc0340412019-04-20 13:21:46 +0200123
Benjamin Tissoires8cb37462014-09-30 13:18:26 -0400124struct dj_report {
125 u8 report_id;
126 u8 device_index;
127 u8 report_type;
128 u8 report_params[DJREPORT_SHORT_LENGTH - 3];
129};
130
Benjamin Tissoires7bb56a52019-04-20 13:21:44 +0200131struct hidpp_event {
132 u8 report_id;
133 u8 device_index;
134 u8 sub_id;
135 u8 params[HIDPP_REPORT_LONG_LENGTH - 3U];
136} __packed;
137
Benjamin Tissoires8cb37462014-09-30 13:18:26 -0400138struct dj_receiver_dev {
Hans de Goedea1d97cc2019-04-20 13:21:52 +0200139 struct hid_device *mouse;
140 struct hid_device *keyboard;
Hans de Goede0ee75542019-04-20 13:21:51 +0200141 struct hid_device *hidpp;
Benjamin Tissoires8cb37462014-09-30 13:18:26 -0400142 struct dj_device *paired_dj_devices[DJ_MAX_PAIRED_DEVICES +
143 DJ_DEVICE_INDEX_MIN];
Hans de Goedea1d97cc2019-04-20 13:21:52 +0200144 struct list_head list;
145 struct kref kref;
Benjamin Tissoires8cb37462014-09-30 13:18:26 -0400146 struct work_struct work;
147 struct kfifo notif_fifo;
Hans de Goedeb6aeedd2019-04-20 13:21:53 +0200148 unsigned long last_query; /* in jiffies */
Hans de Goedea1d97cc2019-04-20 13:21:52 +0200149 bool ready;
Hans de Goede74808f92019-04-20 13:21:54 +0200150 enum recvr_type type;
151 unsigned int unnumbered_application;
Benjamin Tissoires8cb37462014-09-30 13:18:26 -0400152 spinlock_t lock;
Benjamin Tissoires8cb37462014-09-30 13:18:26 -0400153};
154
155struct dj_device {
156 struct hid_device *hdev;
157 struct dj_receiver_dev *dj_receiver_dev;
158 u32 reports_supported;
159 u8 device_index;
160};
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200161
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200162#define WORKITEM_TYPE_EMPTY 0
163#define WORKITEM_TYPE_PAIRED 1
164#define WORKITEM_TYPE_UNPAIRED 2
165#define WORKITEM_TYPE_UNKNOWN 255
166
167struct dj_workitem {
168 u8 type; /* WORKITEM_TYPE_* */
169 u8 device_index;
170 u8 quad_id_msb;
171 u8 quad_id_lsb;
172 u32 reports_supported;
173};
174
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200175/* Keyboard descriptor (1) */
176static const char kbd_descriptor[] = {
177 0x05, 0x01, /* USAGE_PAGE (generic Desktop) */
178 0x09, 0x06, /* USAGE (Keyboard) */
179 0xA1, 0x01, /* COLLECTION (Application) */
180 0x85, 0x01, /* REPORT_ID (1) */
181 0x95, 0x08, /* REPORT_COUNT (8) */
182 0x75, 0x01, /* REPORT_SIZE (1) */
183 0x15, 0x00, /* LOGICAL_MINIMUM (0) */
184 0x25, 0x01, /* LOGICAL_MAXIMUM (1) */
185 0x05, 0x07, /* USAGE_PAGE (Keyboard) */
186 0x19, 0xE0, /* USAGE_MINIMUM (Left Control) */
187 0x29, 0xE7, /* USAGE_MAXIMUM (Right GUI) */
188 0x81, 0x02, /* INPUT (Data,Var,Abs) */
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200189 0x95, 0x06, /* REPORT_COUNT (6) */
190 0x75, 0x08, /* REPORT_SIZE (8) */
191 0x15, 0x00, /* LOGICAL_MINIMUM (0) */
192 0x26, 0xFF, 0x00, /* LOGICAL_MAXIMUM (255) */
193 0x05, 0x07, /* USAGE_PAGE (Keyboard) */
194 0x19, 0x00, /* USAGE_MINIMUM (no event) */
195 0x2A, 0xFF, 0x00, /* USAGE_MAXIMUM (reserved) */
196 0x81, 0x00, /* INPUT (Data,Ary,Abs) */
Benjamin Tissoires0e40d352014-02-05 16:33:17 -0500197 0x85, 0x0e, /* REPORT_ID (14) */
198 0x05, 0x08, /* USAGE PAGE (LED page) */
199 0x95, 0x05, /* REPORT COUNT (5) */
200 0x75, 0x01, /* REPORT SIZE (1) */
201 0x15, 0x00, /* LOGICAL_MINIMUM (0) */
202 0x25, 0x01, /* LOGICAL_MAXIMUM (1) */
203 0x19, 0x01, /* USAGE MINIMUM (1) */
204 0x29, 0x05, /* USAGE MAXIMUM (5) */
205 0x91, 0x02, /* OUTPUT (Data, Variable, Absolute) */
206 0x95, 0x01, /* REPORT COUNT (1) */
207 0x75, 0x03, /* REPORT SIZE (3) */
208 0x91, 0x01, /* OUTPUT (Constant) */
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200209 0xC0
210};
211
212/* Mouse descriptor (2) */
213static const char mse_descriptor[] = {
214 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */
215 0x09, 0x02, /* USAGE (Mouse) */
216 0xA1, 0x01, /* COLLECTION (Application) */
217 0x85, 0x02, /* REPORT_ID = 2 */
218 0x09, 0x01, /* USAGE (pointer) */
219 0xA1, 0x00, /* COLLECTION (physical) */
220 0x05, 0x09, /* USAGE_PAGE (buttons) */
221 0x19, 0x01, /* USAGE_MIN (1) */
222 0x29, 0x10, /* USAGE_MAX (16) */
223 0x15, 0x00, /* LOGICAL_MIN (0) */
224 0x25, 0x01, /* LOGICAL_MAX (1) */
225 0x95, 0x10, /* REPORT_COUNT (16) */
226 0x75, 0x01, /* REPORT_SIZE (1) */
227 0x81, 0x02, /* INPUT (data var abs) */
228 0x05, 0x01, /* USAGE_PAGE (generic desktop) */
229 0x16, 0x01, 0xF8, /* LOGICAL_MIN (-2047) */
230 0x26, 0xFF, 0x07, /* LOGICAL_MAX (2047) */
231 0x75, 0x0C, /* REPORT_SIZE (12) */
232 0x95, 0x02, /* REPORT_COUNT (2) */
233 0x09, 0x30, /* USAGE (X) */
234 0x09, 0x31, /* USAGE (Y) */
235 0x81, 0x06, /* INPUT */
236 0x15, 0x81, /* LOGICAL_MIN (-127) */
237 0x25, 0x7F, /* LOGICAL_MAX (127) */
238 0x75, 0x08, /* REPORT_SIZE (8) */
239 0x95, 0x01, /* REPORT_COUNT (1) */
240 0x09, 0x38, /* USAGE (wheel) */
241 0x81, 0x06, /* INPUT */
242 0x05, 0x0C, /* USAGE_PAGE(consumer) */
243 0x0A, 0x38, 0x02, /* USAGE(AC Pan) */
244 0x95, 0x01, /* REPORT_COUNT (1) */
245 0x81, 0x06, /* INPUT */
246 0xC0, /* END_COLLECTION */
247 0xC0, /* END_COLLECTION */
248};
249
250/* Consumer Control descriptor (3) */
251static const char consumer_descriptor[] = {
252 0x05, 0x0C, /* USAGE_PAGE (Consumer Devices) */
253 0x09, 0x01, /* USAGE (Consumer Control) */
254 0xA1, 0x01, /* COLLECTION (Application) */
255 0x85, 0x03, /* REPORT_ID = 3 */
256 0x75, 0x10, /* REPORT_SIZE (16) */
257 0x95, 0x02, /* REPORT_COUNT (2) */
258 0x15, 0x01, /* LOGICAL_MIN (1) */
259 0x26, 0x8C, 0x02, /* LOGICAL_MAX (652) */
260 0x19, 0x01, /* USAGE_MIN (1) */
261 0x2A, 0x8C, 0x02, /* USAGE_MAX (652) */
262 0x81, 0x00, /* INPUT (Data Ary Abs) */
263 0xC0, /* END_COLLECTION */
264}; /* */
265
266/* System control descriptor (4) */
267static const char syscontrol_descriptor[] = {
268 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */
269 0x09, 0x80, /* USAGE (System Control) */
270 0xA1, 0x01, /* COLLECTION (Application) */
271 0x85, 0x04, /* REPORT_ID = 4 */
272 0x75, 0x02, /* REPORT_SIZE (2) */
273 0x95, 0x01, /* REPORT_COUNT (1) */
274 0x15, 0x01, /* LOGICAL_MIN (1) */
275 0x25, 0x03, /* LOGICAL_MAX (3) */
276 0x09, 0x82, /* USAGE (System Sleep) */
277 0x09, 0x81, /* USAGE (System Power Down) */
278 0x09, 0x83, /* USAGE (System Wake Up) */
279 0x81, 0x60, /* INPUT (Data Ary Abs NPrf Null) */
280 0x75, 0x06, /* REPORT_SIZE (6) */
281 0x81, 0x03, /* INPUT (Cnst Var Abs) */
282 0xC0, /* END_COLLECTION */
283};
284
285/* Media descriptor (8) */
286static const char media_descriptor[] = {
287 0x06, 0xbc, 0xff, /* Usage Page 0xffbc */
288 0x09, 0x88, /* Usage 0x0088 */
289 0xa1, 0x01, /* BeginCollection */
290 0x85, 0x08, /* Report ID 8 */
291 0x19, 0x01, /* Usage Min 0x0001 */
292 0x29, 0xff, /* Usage Max 0x00ff */
293 0x15, 0x01, /* Logical Min 1 */
294 0x26, 0xff, 0x00, /* Logical Max 255 */
295 0x75, 0x08, /* Report Size 8 */
296 0x95, 0x01, /* Report Count 1 */
297 0x81, 0x00, /* Input */
298 0xc0, /* EndCollection */
299}; /* */
300
Benjamin Tissoires925f0f32014-09-30 13:18:29 -0400301/* HIDPP descriptor */
302static const char hidpp_descriptor[] = {
303 0x06, 0x00, 0xff, /* Usage Page (Vendor Defined Page 1) */
304 0x09, 0x01, /* Usage (Vendor Usage 1) */
305 0xa1, 0x01, /* Collection (Application) */
306 0x85, 0x10, /* Report ID (16) */
307 0x75, 0x08, /* Report Size (8) */
308 0x95, 0x06, /* Report Count (6) */
309 0x15, 0x00, /* Logical Minimum (0) */
310 0x26, 0xff, 0x00, /* Logical Maximum (255) */
311 0x09, 0x01, /* Usage (Vendor Usage 1) */
312 0x81, 0x00, /* Input (Data,Arr,Abs) */
313 0x09, 0x01, /* Usage (Vendor Usage 1) */
314 0x91, 0x00, /* Output (Data,Arr,Abs) */
315 0xc0, /* End Collection */
316 0x06, 0x00, 0xff, /* Usage Page (Vendor Defined Page 1) */
317 0x09, 0x02, /* Usage (Vendor Usage 2) */
318 0xa1, 0x01, /* Collection (Application) */
319 0x85, 0x11, /* Report ID (17) */
320 0x75, 0x08, /* Report Size (8) */
321 0x95, 0x13, /* Report Count (19) */
322 0x15, 0x00, /* Logical Minimum (0) */
323 0x26, 0xff, 0x00, /* Logical Maximum (255) */
324 0x09, 0x02, /* Usage (Vendor Usage 2) */
325 0x81, 0x00, /* Input (Data,Arr,Abs) */
326 0x09, 0x02, /* Usage (Vendor Usage 2) */
327 0x91, 0x00, /* Output (Data,Arr,Abs) */
328 0xc0, /* End Collection */
329 0x06, 0x00, 0xff, /* Usage Page (Vendor Defined Page 1) */
330 0x09, 0x04, /* Usage (Vendor Usage 0x04) */
331 0xa1, 0x01, /* Collection (Application) */
332 0x85, 0x20, /* Report ID (32) */
333 0x75, 0x08, /* Report Size (8) */
334 0x95, 0x0e, /* Report Count (14) */
335 0x15, 0x00, /* Logical Minimum (0) */
336 0x26, 0xff, 0x00, /* Logical Maximum (255) */
337 0x09, 0x41, /* Usage (Vendor Usage 0x41) */
338 0x81, 0x00, /* Input (Data,Arr,Abs) */
339 0x09, 0x41, /* Usage (Vendor Usage 0x41) */
340 0x91, 0x00, /* Output (Data,Arr,Abs) */
341 0x85, 0x21, /* Report ID (33) */
342 0x95, 0x1f, /* Report Count (31) */
343 0x15, 0x00, /* Logical Minimum (0) */
344 0x26, 0xff, 0x00, /* Logical Maximum (255) */
345 0x09, 0x42, /* Usage (Vendor Usage 0x42) */
346 0x81, 0x00, /* Input (Data,Arr,Abs) */
347 0x09, 0x42, /* Usage (Vendor Usage 0x42) */
348 0x91, 0x00, /* Output (Data,Arr,Abs) */
349 0xc0, /* End Collection */
350};
351
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200352/* Maximum size of all defined hid reports in bytes (including report id) */
353#define MAX_REPORT_SIZE 8
354
Henrik Rydberg2a039bf2012-04-22 14:21:39 +0200355/* Make sure all descriptors are present here */
356#define MAX_RDESC_SIZE \
357 (sizeof(kbd_descriptor) + \
358 sizeof(mse_descriptor) + \
359 sizeof(consumer_descriptor) + \
360 sizeof(syscontrol_descriptor) + \
Benjamin Tissoires925f0f32014-09-30 13:18:29 -0400361 sizeof(media_descriptor) + \
362 sizeof(hidpp_descriptor))
Henrik Rydberg2a039bf2012-04-22 14:21:39 +0200363
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200364/* Number of possible hid report types that can be created by this driver.
365 *
366 * Right now, RF report types have the same report types (or report id's)
367 * than the hid report created from those RF reports. In the future
368 * this doesnt have to be true.
369 *
370 * For instance, RF report type 0x01 which has a size of 8 bytes, corresponds
371 * to hid report id 0x01, this is standard keyboard. Same thing applies to mice
372 * reports and consumer control, etc. If a new RF report is created, it doesn't
373 * has to have the same report id as its corresponding hid report, so an
374 * translation may have to take place for future report types.
375 */
376#define NUMBER_OF_HID_REPORTS 32
377static const u8 hid_reportid_size_map[NUMBER_OF_HID_REPORTS] = {
378 [1] = 8, /* Standard keyboard */
379 [2] = 8, /* Standard mouse */
380 [3] = 5, /* Consumer control */
381 [4] = 2, /* System control */
382 [8] = 2, /* Media Center */
383};
384
385
386#define LOGITECH_DJ_INTERFACE_NUMBER 0x02
387
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200388static struct hid_ll_driver logi_dj_ll_driver;
389
Nestor Lopez Casadoc63e0e32013-07-18 06:21:30 -0700390static int logi_dj_recv_query_paired_devices(struct dj_receiver_dev *djrcv_dev);
Hans de Goedea1d97cc2019-04-20 13:21:52 +0200391static void delayedwork_callback(struct work_struct *work);
392
393static LIST_HEAD(dj_hdev_list);
394static DEFINE_MUTEX(dj_hdev_list_lock);
395
396/*
397 * dj/HID++ receivers are really a single logical entity, but for BIOS/Windows
398 * compatibility they have multiple USB interfaces. On HID++ receivers we need
399 * to listen for input reports on both interfaces. The functions below are used
400 * to create a single struct dj_receiver_dev for all interfaces belonging to
401 * a single USB-device / receiver.
402 */
403static struct dj_receiver_dev *dj_find_receiver_dev(struct hid_device *hdev)
404{
405 struct dj_receiver_dev *djrcv_dev;
406
407 /* Try to find an already-probed interface from the same device */
408 list_for_each_entry(djrcv_dev, &dj_hdev_list, list) {
409 if (djrcv_dev->mouse &&
410 hid_compare_device_paths(hdev, djrcv_dev->mouse, '/')) {
411 kref_get(&djrcv_dev->kref);
412 return djrcv_dev;
413 }
414 if (djrcv_dev->keyboard &&
415 hid_compare_device_paths(hdev, djrcv_dev->keyboard, '/')) {
416 kref_get(&djrcv_dev->kref);
417 return djrcv_dev;
418 }
419 if (djrcv_dev->hidpp &&
420 hid_compare_device_paths(hdev, djrcv_dev->hidpp, '/')) {
421 kref_get(&djrcv_dev->kref);
422 return djrcv_dev;
423 }
424 }
425
426 return NULL;
427}
428
429static void dj_release_receiver_dev(struct kref *kref)
430{
431 struct dj_receiver_dev *djrcv_dev = container_of(kref, struct dj_receiver_dev, kref);
432
433 list_del(&djrcv_dev->list);
434 kfifo_free(&djrcv_dev->notif_fifo);
435 kfree(djrcv_dev);
436}
437
438static void dj_put_receiver_dev(struct hid_device *hdev)
439{
440 struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
441
442 mutex_lock(&dj_hdev_list_lock);
443
444 if (djrcv_dev->mouse == hdev)
445 djrcv_dev->mouse = NULL;
446 if (djrcv_dev->keyboard == hdev)
447 djrcv_dev->keyboard = NULL;
448 if (djrcv_dev->hidpp == hdev)
449 djrcv_dev->hidpp = NULL;
450
451 kref_put(&djrcv_dev->kref, dj_release_receiver_dev);
452
453 mutex_unlock(&dj_hdev_list_lock);
454}
455
456static struct dj_receiver_dev *dj_get_receiver_dev(struct hid_device *hdev,
Hans de Goede74808f92019-04-20 13:21:54 +0200457 enum recvr_type type,
Hans de Goedea1d97cc2019-04-20 13:21:52 +0200458 unsigned int application,
459 bool is_hidpp)
460{
461 struct dj_receiver_dev *djrcv_dev;
462
463 mutex_lock(&dj_hdev_list_lock);
464
465 djrcv_dev = dj_find_receiver_dev(hdev);
466 if (!djrcv_dev) {
467 djrcv_dev = kzalloc(sizeof(*djrcv_dev), GFP_KERNEL);
468 if (!djrcv_dev)
469 goto out;
470
471 INIT_WORK(&djrcv_dev->work, delayedwork_callback);
472 spin_lock_init(&djrcv_dev->lock);
473 if (kfifo_alloc(&djrcv_dev->notif_fifo,
474 DJ_MAX_NUMBER_NOTIFS * sizeof(struct dj_workitem),
475 GFP_KERNEL)) {
476 kfree(djrcv_dev);
477 djrcv_dev = NULL;
478 goto out;
479 }
480 kref_init(&djrcv_dev->kref);
481 list_add_tail(&djrcv_dev->list, &dj_hdev_list);
Hans de Goedeb6aeedd2019-04-20 13:21:53 +0200482 djrcv_dev->last_query = jiffies;
Hans de Goede74808f92019-04-20 13:21:54 +0200483 djrcv_dev->type = type;
Hans de Goedea1d97cc2019-04-20 13:21:52 +0200484 }
485
486 if (application == HID_GD_KEYBOARD)
487 djrcv_dev->keyboard = hdev;
488 if (application == HID_GD_MOUSE)
489 djrcv_dev->mouse = hdev;
490 if (is_hidpp)
491 djrcv_dev->hidpp = hdev;
492
493 hid_set_drvdata(hdev, djrcv_dev);
494out:
495 mutex_unlock(&dj_hdev_list_lock);
496 return djrcv_dev;
497}
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200498
499static void logi_dj_recv_destroy_djhid_device(struct dj_receiver_dev *djrcv_dev,
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200500 struct dj_workitem *workitem)
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200501{
502 /* Called in delayed work context */
503 struct dj_device *dj_dev;
504 unsigned long flags;
505
506 spin_lock_irqsave(&djrcv_dev->lock, flags);
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200507 dj_dev = djrcv_dev->paired_dj_devices[workitem->device_index];
508 djrcv_dev->paired_dj_devices[workitem->device_index] = NULL;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200509 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
510
511 if (dj_dev != NULL) {
512 hid_destroy_device(dj_dev->hdev);
513 kfree(dj_dev);
514 } else {
Hans de Goede0ee75542019-04-20 13:21:51 +0200515 hid_err(djrcv_dev->hidpp, "%s: can't destroy a NULL device\n",
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200516 __func__);
517 }
518}
519
520static void logi_dj_recv_add_djhid_device(struct dj_receiver_dev *djrcv_dev,
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200521 struct dj_workitem *workitem)
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200522{
523 /* Called in delayed work context */
Hans de Goede0ee75542019-04-20 13:21:51 +0200524 struct hid_device *djrcv_hdev = djrcv_dev->hidpp;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200525 struct hid_device *dj_hiddev;
526 struct dj_device *dj_dev;
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200527 u8 device_index = workitem->device_index;
Hans de Goedef41d7662019-04-20 13:21:50 +0200528 unsigned long flags;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200529
530 /* Device index goes from 1 to 6, we need 3 bytes to store the
531 * semicolon, the index, and a null terminator
532 */
533 unsigned char tmpstr[3];
534
Hans de Goedef41d7662019-04-20 13:21:50 +0200535 /* We are the only one ever adding a device, no need to lock */
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200536 if (djrcv_dev->paired_dj_devices[device_index]) {
Nestor Lopez Casadoc63e0e32013-07-18 06:21:30 -0700537 /* The device is already known. No need to reallocate it. */
538 dbg_hid("%s: device is already known\n", __func__);
539 return;
540 }
541
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200542 dj_hiddev = hid_allocate_device();
543 if (IS_ERR(dj_hiddev)) {
544 dev_err(&djrcv_hdev->dev, "%s: hid_allocate_device failed\n",
545 __func__);
546 return;
547 }
548
549 dj_hiddev->ll_driver = &logi_dj_ll_driver;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200550
551 dj_hiddev->dev.parent = &djrcv_hdev->dev;
552 dj_hiddev->bus = BUS_USB;
Benjamin Tissoires82c0beb2019-04-20 13:21:47 +0200553 dj_hiddev->vendor = djrcv_hdev->vendor;
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200554 dj_hiddev->product = (workitem->quad_id_msb << 8) |
555 workitem->quad_id_lsb;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200556 snprintf(dj_hiddev->name, sizeof(dj_hiddev->name),
Benjamin Tissoiresd6102742014-09-30 13:18:25 -0400557 "Logitech Unifying Device. Wireless PID:%04x",
558 dj_hiddev->product);
559
560 dj_hiddev->group = HID_GROUP_LOGITECH_DJ_DEVICE;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200561
Benjamin Tissoires82c0beb2019-04-20 13:21:47 +0200562 memcpy(dj_hiddev->phys, djrcv_hdev->phys, sizeof(djrcv_hdev->phys));
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200563 snprintf(tmpstr, sizeof(tmpstr), ":%d", device_index);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200564 strlcat(dj_hiddev->phys, tmpstr, sizeof(dj_hiddev->phys));
565
566 dj_dev = kzalloc(sizeof(struct dj_device), GFP_KERNEL);
567
568 if (!dj_dev) {
569 dev_err(&djrcv_hdev->dev, "%s: failed allocating dj_device\n",
570 __func__);
571 goto dj_device_allocate_fail;
572 }
573
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200574 dj_dev->reports_supported = workitem->reports_supported;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200575 dj_dev->hdev = dj_hiddev;
576 dj_dev->dj_receiver_dev = djrcv_dev;
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200577 dj_dev->device_index = device_index;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200578 dj_hiddev->driver_data = dj_dev;
579
Hans de Goedef41d7662019-04-20 13:21:50 +0200580 spin_lock_irqsave(&djrcv_dev->lock, flags);
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200581 djrcv_dev->paired_dj_devices[device_index] = dj_dev;
Hans de Goedef41d7662019-04-20 13:21:50 +0200582 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200583
584 if (hid_add_device(dj_hiddev)) {
585 dev_err(&djrcv_hdev->dev, "%s: failed adding dj_device\n",
586 __func__);
587 goto hid_add_device_fail;
588 }
589
590 return;
591
592hid_add_device_fail:
Hans de Goedef41d7662019-04-20 13:21:50 +0200593 spin_lock_irqsave(&djrcv_dev->lock, flags);
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200594 djrcv_dev->paired_dj_devices[device_index] = NULL;
Hans de Goedef41d7662019-04-20 13:21:50 +0200595 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200596 kfree(dj_dev);
597dj_device_allocate_fail:
598 hid_destroy_device(dj_hiddev);
599}
600
601static void delayedwork_callback(struct work_struct *work)
602{
603 struct dj_receiver_dev *djrcv_dev =
604 container_of(work, struct dj_receiver_dev, work);
605
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200606 struct dj_workitem workitem;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200607 unsigned long flags;
608 int count;
Nestor Lopez Casadoc63e0e32013-07-18 06:21:30 -0700609 int retval;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200610
611 dbg_hid("%s\n", __func__);
612
613 spin_lock_irqsave(&djrcv_dev->lock, flags);
614
Hans de Goedea1d97cc2019-04-20 13:21:52 +0200615 /*
616 * Since we attach to multiple interfaces, we may get scheduled before
617 * we are bound to the HID++ interface, catch this.
618 */
619 if (!djrcv_dev->ready) {
620 pr_warn("%s: delayedwork queued before hidpp interface was enumerated\n",
621 __func__);
622 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
623 return;
624 }
625
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200626 count = kfifo_out(&djrcv_dev->notif_fifo, &workitem, sizeof(workitem));
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200627
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200628 if (count != sizeof(workitem)) {
Hans de Goede0ee75542019-04-20 13:21:51 +0200629 hid_err(djrcv_dev->hidpp, "delayedwork queued without workitems available\n");
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200630 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
631 return;
632 }
633
634 if (!kfifo_is_empty(&djrcv_dev->notif_fifo)) {
635 if (schedule_work(&djrcv_dev->work) == 0) {
636 dbg_hid("%s: did not schedule the work item, was "
637 "already queued\n", __func__);
638 }
639 }
640
641 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
642
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200643 switch (workitem.type) {
644 case WORKITEM_TYPE_PAIRED:
645 logi_dj_recv_add_djhid_device(djrcv_dev, &workitem);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200646 break;
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200647 case WORKITEM_TYPE_UNPAIRED:
648 logi_dj_recv_destroy_djhid_device(djrcv_dev, &workitem);
649 break;
650 case WORKITEM_TYPE_UNKNOWN:
651 retval = logi_dj_recv_query_paired_devices(djrcv_dev);
652 if (retval) {
Hans de Goede0ee75542019-04-20 13:21:51 +0200653 hid_err(djrcv_dev->hidpp, "%s: logi_dj_recv_query_paired_devices error: %d\n",
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200654 __func__, retval);
655 }
656 break;
657 case WORKITEM_TYPE_EMPTY:
658 dbg_hid("%s: device list is empty\n", __func__);
659 break;
660 }
661}
662
Hans de Goedeb6aeedd2019-04-20 13:21:53 +0200663/*
664 * Sometimes we receive reports for which we do not have a paired dj_device
665 * associated with the device_index or report-type to forward the report to.
666 * This means that the original "device paired" notification corresponding
667 * to the dj_device never arrived to this driver. Possible reasons for this are:
668 * 1) hid-core discards all packets coming from a device during probe().
669 * 2) if the receiver is plugged into a KVM switch then the pairing reports
670 * are only forwarded to it if the focus is on this PC.
671 * This function deals with this by re-asking the receiver for the list of
672 * connected devices in the delayed work callback.
673 * This function MUST be called with djrcv->lock held.
674 */
675static void logi_dj_recv_queue_unknown_work(struct dj_receiver_dev *djrcv_dev)
676{
677 struct dj_workitem workitem = { .type = WORKITEM_TYPE_UNKNOWN };
678
679 /* Rate limit queries done because of unhandeled reports to 2/sec */
680 if (time_before(jiffies, djrcv_dev->last_query + HZ / 2))
681 return;
682
683 kfifo_in(&djrcv_dev->notif_fifo, &workitem, sizeof(workitem));
684 schedule_work(&djrcv_dev->work);
685}
686
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200687static void logi_dj_recv_queue_notification(struct dj_receiver_dev *djrcv_dev,
688 struct dj_report *dj_report)
689{
690 /* We are called from atomic context (tasklet && djrcv->lock held) */
691 struct dj_workitem workitem = {
692 .device_index = dj_report->device_index,
693 };
694
695 switch (dj_report->report_type) {
696 case REPORT_TYPE_NOTIF_DEVICE_PAIRED:
697 workitem.type = WORKITEM_TYPE_PAIRED;
698 if (dj_report->report_params[DEVICE_PAIRED_PARAM_SPFUNCTION] &
699 SPFUNCTION_DEVICE_LIST_EMPTY) {
700 workitem.type = WORKITEM_TYPE_EMPTY;
701 break;
702 }
703 /* fall-through */
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200704 case REPORT_TYPE_NOTIF_DEVICE_UNPAIRED:
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200705 workitem.quad_id_msb =
706 dj_report->report_params[DEVICE_PAIRED_PARAM_EQUAD_ID_MSB];
707 workitem.quad_id_lsb =
708 dj_report->report_params[DEVICE_PAIRED_PARAM_EQUAD_ID_LSB];
709 workitem.reports_supported = get_unaligned_le32(
710 dj_report->report_params +
711 DEVICE_PAIRED_RF_REPORT_TYPE);
712 if (dj_report->report_type == REPORT_TYPE_NOTIF_DEVICE_UNPAIRED)
713 workitem.type = WORKITEM_TYPE_UNPAIRED;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200714 break;
715 default:
Hans de Goedeb6aeedd2019-04-20 13:21:53 +0200716 logi_dj_recv_queue_unknown_work(djrcv_dev);
717 return;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200718 }
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200719
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200720 kfifo_in(&djrcv_dev->notif_fifo, &workitem, sizeof(workitem));
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200721
722 if (schedule_work(&djrcv_dev->work) == 0) {
723 dbg_hid("%s: did not schedule the work item, was already "
724 "queued\n", __func__);
725 }
726}
727
Hans de Goede74808f92019-04-20 13:21:54 +0200728static void logi_hidpp_dev_conn_notif_equad(struct hidpp_event *hidpp_report,
729 struct dj_workitem *workitem)
730{
731 workitem->type = WORKITEM_TYPE_PAIRED;
732 workitem->quad_id_msb = hidpp_report->params[HIDPP_PARAM_EQUAD_MSB];
733 workitem->quad_id_lsb = hidpp_report->params[HIDPP_PARAM_EQUAD_LSB];
734 switch (hidpp_report->params[HIDPP_PARAM_DEVICE_INFO] &
735 HIDPP_DEVICE_TYPE_MASK) {
736 case REPORT_TYPE_KEYBOARD:
737 workitem->reports_supported |= STD_KEYBOARD | MULTIMEDIA |
738 POWER_KEYS | MEDIA_CENTER;
739 break;
740 case REPORT_TYPE_MOUSE:
741 workitem->reports_supported |= STD_MOUSE;
742 break;
743 }
744}
745
746static void logi_hidpp_recv_queue_notif(struct hid_device *hdev,
747 struct hidpp_event *hidpp_report)
748{
749 /* We are called from atomic context (tasklet && djrcv->lock held) */
750 struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
751 const char *device_type = "UNKNOWN";
752 struct dj_workitem workitem = {
753 .type = WORKITEM_TYPE_EMPTY,
754 .device_index = hidpp_report->device_index,
755 };
756
757 switch (hidpp_report->params[HIDPP_PARAM_PROTO_TYPE]) {
758 case 0x01:
759 device_type = "Bluetooth";
760 break;
761 case 0x02:
762 device_type = "27 Mhz";
763 break;
764 case 0x03:
765 device_type = "QUAD or eQUAD";
766 logi_hidpp_dev_conn_notif_equad(hidpp_report, &workitem);
767 break;
768 case 0x04:
769 device_type = "eQUAD step 4 DJ";
770 logi_hidpp_dev_conn_notif_equad(hidpp_report, &workitem);
771 break;
772 case 0x05:
773 device_type = "DFU Lite";
774 break;
775 case 0x06:
776 device_type = "eQUAD step 4 Lite";
777 logi_hidpp_dev_conn_notif_equad(hidpp_report, &workitem);
778 break;
779 case 0x07:
780 device_type = "eQUAD step 4 Gaming";
781 break;
782 case 0x08:
783 device_type = "eQUAD step 4 for gamepads";
784 break;
785 case 0x0a:
786 device_type = "eQUAD nano Lite";
787 logi_hidpp_dev_conn_notif_equad(hidpp_report, &workitem);
788 break;
789 case 0x0c:
790 device_type = "eQUAD Lightspeed";
791 break;
792 }
793
794 if (workitem.type == WORKITEM_TYPE_EMPTY) {
795 hid_warn(hdev,
796 "unusable device of type %s (0x%02x) connected on slot %d",
797 device_type,
798 hidpp_report->params[HIDPP_PARAM_PROTO_TYPE],
799 hidpp_report->device_index);
800 return;
801 }
802
803 hid_info(hdev, "device of type %s (0x%02x) connected on slot %d",
804 device_type, hidpp_report->params[HIDPP_PARAM_PROTO_TYPE],
805 hidpp_report->device_index);
806
807
808 kfifo_in(&djrcv_dev->notif_fifo, &workitem, sizeof(workitem));
809
810 if (schedule_work(&djrcv_dev->work) == 0) {
811 dbg_hid("%s: did not schedule the work item, was already queued\n",
812 __func__);
813 }
814}
815
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200816static void logi_dj_recv_forward_null_report(struct dj_receiver_dev *djrcv_dev,
817 struct dj_report *dj_report)
818{
819 /* We are called from atomic context (tasklet && djrcv->lock held) */
820 unsigned int i;
821 u8 reportbuffer[MAX_REPORT_SIZE];
822 struct dj_device *djdev;
823
824 djdev = djrcv_dev->paired_dj_devices[dj_report->device_index];
825
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200826 memset(reportbuffer, 0, sizeof(reportbuffer));
827
828 for (i = 0; i < NUMBER_OF_HID_REPORTS; i++) {
829 if (djdev->reports_supported & (1 << i)) {
830 reportbuffer[0] = i;
831 if (hid_input_report(djdev->hdev,
832 HID_INPUT_REPORT,
833 reportbuffer,
834 hid_reportid_size_map[i], 1)) {
835 dbg_hid("hid_input_report error sending null "
836 "report\n");
837 }
838 }
839 }
840}
841
Benjamin Tissoires83898232019-04-20 13:21:43 +0200842static void logi_dj_recv_forward_dj(struct dj_receiver_dev *djrcv_dev,
843 struct dj_report *dj_report)
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200844{
845 /* We are called from atomic context (tasklet && djrcv->lock held) */
846 struct dj_device *dj_device;
847
848 dj_device = djrcv_dev->paired_dj_devices[dj_report->device_index];
849
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200850 if ((dj_report->report_type > ARRAY_SIZE(hid_reportid_size_map) - 1) ||
851 (hid_reportid_size_map[dj_report->report_type] == 0)) {
852 dbg_hid("invalid report type:%x\n", dj_report->report_type);
853 return;
854 }
855
856 if (hid_input_report(dj_device->hdev,
857 HID_INPUT_REPORT, &dj_report->report_type,
858 hid_reportid_size_map[dj_report->report_type], 1)) {
859 dbg_hid("hid_input_report error\n");
860 }
861}
862
Benjamin Tissoires83898232019-04-20 13:21:43 +0200863static void logi_dj_recv_forward_report(struct dj_device *dj_dev, u8 *data,
864 int size)
Benjamin Tissoires925f0f32014-09-30 13:18:29 -0400865{
866 /* We are called from atomic context (tasklet && djrcv->lock held) */
867 if (hid_input_report(dj_dev->hdev, HID_INPUT_REPORT, data, size, 1))
868 dbg_hid("hid_input_report error\n");
869}
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200870
Hans de Goede74808f92019-04-20 13:21:54 +0200871static void logi_dj_recv_forward_input_report(struct hid_device *hdev,
872 u8 *data, int size)
873{
874 struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
875 struct dj_device *dj_dev;
876 unsigned long flags;
877 u8 report = data[0];
878 int i;
879
880 if (report > REPORT_TYPE_RFREPORT_LAST) {
881 hid_err(hdev, "Unexpect input report number %d\n", report);
882 return;
883 }
884
885 spin_lock_irqsave(&djrcv_dev->lock, flags);
886 for (i = 0; i < (DJ_MAX_PAIRED_DEVICES + DJ_DEVICE_INDEX_MIN); i++) {
887 dj_dev = djrcv_dev->paired_dj_devices[i];
888 if (dj_dev && (dj_dev->reports_supported & BIT(report))) {
889 logi_dj_recv_forward_report(dj_dev, data, size);
890 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
891 return;
892 }
893 }
894
895 logi_dj_recv_queue_unknown_work(djrcv_dev);
896 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
897
898 dbg_hid("No dj-devs handling input report number %d\n", report);
899}
900
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200901static int logi_dj_recv_send_report(struct dj_receiver_dev *djrcv_dev,
902 struct dj_report *dj_report)
903{
Hans de Goede0ee75542019-04-20 13:21:51 +0200904 struct hid_device *hdev = djrcv_dev->hidpp;
Benjamin Tissoiresdcd90062013-03-05 17:09:00 +0100905 struct hid_report *report;
906 struct hid_report_enum *output_report_enum;
907 u8 *data = (u8 *)(&dj_report->device_index);
Kees Cook297502a2013-09-11 21:56:56 +0200908 unsigned int i;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200909
Benjamin Tissoiresdcd90062013-03-05 17:09:00 +0100910 output_report_enum = &hdev->report_enum[HID_OUTPUT_REPORT];
911 report = output_report_enum->report_id_hash[REPORT_ID_DJ_SHORT];
912
913 if (!report) {
914 dev_err(&hdev->dev, "%s: unable to find dj report\n", __func__);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200915 return -ENODEV;
916 }
917
Kees Cook297502a2013-09-11 21:56:56 +0200918 for (i = 0; i < DJREPORT_SHORT_LENGTH - 1; i++)
Benjamin Tissoiresdcd90062013-03-05 17:09:00 +0100919 report->field[0]->value[i] = data[i];
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200920
Jiri Kosina83a44ac2013-03-09 10:58:13 +0100921 hid_hw_request(hdev, report, HID_REQ_SET_REPORT);
Benjamin Tissoiresdcd90062013-03-05 17:09:00 +0100922
923 return 0;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200924}
925
Hans de Goede74808f92019-04-20 13:21:54 +0200926static int logi_dj_recv_query_hidpp_devices(struct dj_receiver_dev *djrcv_dev)
927{
928 const u8 template[] = {REPORT_ID_HIDPP_SHORT,
929 HIDPP_RECEIVER_INDEX,
930 HIDPP_SET_REGISTER,
931 HIDPP_REG_CONNECTION_STATE,
932 HIDPP_FAKE_DEVICE_ARRIVAL,
933 0x00, 0x00};
934 u8 *hidpp_report;
935 int retval;
936
937 hidpp_report = kmemdup(template, sizeof(template), GFP_KERNEL);
938 if (!hidpp_report)
939 return -ENOMEM;
940
941 retval = hid_hw_raw_request(djrcv_dev->hidpp,
942 REPORT_ID_HIDPP_SHORT,
943 hidpp_report, sizeof(template),
944 HID_OUTPUT_REPORT,
945 HID_REQ_SET_REPORT);
946
947 kfree(hidpp_report);
948 return 0;
949}
950
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200951static int logi_dj_recv_query_paired_devices(struct dj_receiver_dev *djrcv_dev)
952{
Marc Dionned8dc3492012-06-01 18:12:14 -0400953 struct dj_report *dj_report;
954 int retval;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200955
Hans de Goedeb6aeedd2019-04-20 13:21:53 +0200956 djrcv_dev->last_query = jiffies;
957
Hans de Goede74808f92019-04-20 13:21:54 +0200958 if (djrcv_dev->type != recvr_type_dj)
959 return logi_dj_recv_query_hidpp_devices(djrcv_dev);
960
Alan Cox8a55ade2012-09-04 15:10:08 +0100961 dj_report = kzalloc(sizeof(struct dj_report), GFP_KERNEL);
Marc Dionned8dc3492012-06-01 18:12:14 -0400962 if (!dj_report)
963 return -ENOMEM;
964 dj_report->report_id = REPORT_ID_DJ_SHORT;
965 dj_report->device_index = 0xFF;
966 dj_report->report_type = REPORT_TYPE_CMD_GET_PAIRED_DEVICES;
967 retval = logi_dj_recv_send_report(djrcv_dev, dj_report);
968 kfree(dj_report);
969 return retval;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200970}
971
Nestor Lopez Casadoc63e0e32013-07-18 06:21:30 -0700972
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200973static int logi_dj_recv_switch_to_dj_mode(struct dj_receiver_dev *djrcv_dev,
974 unsigned timeout)
975{
Hans de Goede0ee75542019-04-20 13:21:51 +0200976 struct hid_device *hdev = djrcv_dev->hidpp;
Marc Dionned8dc3492012-06-01 18:12:14 -0400977 struct dj_report *dj_report;
Benjamin Tissoires6a9ddc82014-09-30 13:18:31 -0400978 u8 *buf;
Hans de Goede74808f92019-04-20 13:21:54 +0200979 int retval = 0;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200980
Alan Cox8a55ade2012-09-04 15:10:08 +0100981 dj_report = kzalloc(sizeof(struct dj_report), GFP_KERNEL);
Marc Dionned8dc3492012-06-01 18:12:14 -0400982 if (!dj_report)
983 return -ENOMEM;
Benjamin Tisssoires42c22db2014-01-08 17:18:45 -0500984
Hans de Goede74808f92019-04-20 13:21:54 +0200985 if (djrcv_dev->type == recvr_type_dj) {
986 dj_report->report_id = REPORT_ID_DJ_SHORT;
987 dj_report->device_index = 0xFF;
988 dj_report->report_type = REPORT_TYPE_CMD_SWITCH;
989 dj_report->report_params[CMD_SWITCH_PARAM_DEVBITFIELD] = 0x3F;
990 dj_report->report_params[CMD_SWITCH_PARAM_TIMEOUT_SECONDS] =
991 (u8)timeout;
992
993 retval = logi_dj_recv_send_report(djrcv_dev, dj_report);
994
995 /*
996 * Ugly sleep to work around a USB 3.0 bug when the receiver is
997 * still processing the "switch-to-dj" command while we send an
998 * other command.
999 * 50 msec should gives enough time to the receiver to be ready.
1000 */
1001 msleep(50);
1002 }
Benjamin Tisssoires42c22db2014-01-08 17:18:45 -05001003
Benjamin Tissoires6a9ddc82014-09-30 13:18:31 -04001004 /*
1005 * Magical bits to set up hidpp notifications when the dj devices
1006 * are connected/disconnected.
1007 *
1008 * We can reuse dj_report because HIDPP_REPORT_SHORT_LENGTH is smaller
1009 * than DJREPORT_SHORT_LENGTH.
1010 */
1011 buf = (u8 *)dj_report;
1012
1013 memset(buf, 0, HIDPP_REPORT_SHORT_LENGTH);
1014
1015 buf[0] = REPORT_ID_HIDPP_SHORT;
1016 buf[1] = 0xFF;
1017 buf[2] = 0x80;
1018 buf[3] = 0x00;
1019 buf[4] = 0x00;
1020 buf[5] = 0x09;
1021 buf[6] = 0x00;
1022
1023 hid_hw_raw_request(hdev, REPORT_ID_HIDPP_SHORT, buf,
1024 HIDPP_REPORT_SHORT_LENGTH, HID_OUTPUT_REPORT,
1025 HID_REQ_SET_REPORT);
1026
1027 kfree(dj_report);
Marc Dionned8dc3492012-06-01 18:12:14 -04001028 return retval;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001029}
1030
1031
1032static int logi_dj_ll_open(struct hid_device *hid)
1033{
1034 dbg_hid("%s:%s\n", __func__, hid->phys);
1035 return 0;
1036
1037}
1038
1039static void logi_dj_ll_close(struct hid_device *hid)
1040{
1041 dbg_hid("%s:%s\n", __func__, hid->phys);
1042}
1043
Benjamin Tissoires8dba3022017-03-27 16:59:21 +02001044/*
1045 * Register 0xB5 is "pairing information". It is solely intended for the
1046 * receiver, so do not overwrite the device index.
1047 */
Benjamin Tissoiresc0340412019-04-20 13:21:46 +02001048static u8 unifying_pairing_query[] = { REPORT_ID_HIDPP_SHORT,
1049 HIDPP_RECEIVER_INDEX,
1050 HIDPP_GET_LONG_REGISTER,
1051 HIDPP_REG_PAIRING_INFORMATION };
1052static u8 unifying_pairing_answer[] = { REPORT_ID_HIDPP_LONG,
1053 HIDPP_RECEIVER_INDEX,
1054 HIDPP_GET_LONG_REGISTER,
1055 HIDPP_REG_PAIRING_INFORMATION };
Benjamin Tissoires33797822014-09-30 13:18:30 -04001056
Benjamin Tissoiresbd27e202014-02-10 12:58:53 -05001057static int logi_dj_ll_raw_request(struct hid_device *hid,
1058 unsigned char reportnum, __u8 *buf,
1059 size_t count, unsigned char report_type,
1060 int reqtype)
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001061{
Benjamin Tissoires0e40d352014-02-05 16:33:17 -05001062 struct dj_device *djdev = hid->driver_data;
1063 struct dj_receiver_dev *djrcv_dev = djdev->dj_receiver_dev;
1064 u8 *out_buf;
1065 int ret;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001066
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001067 if ((buf[0] == REPORT_ID_HIDPP_SHORT) ||
1068 (buf[0] == REPORT_ID_HIDPP_LONG)) {
1069 if (count < 2)
1070 return -EINVAL;
1071
Benjamin Tissoires33797822014-09-30 13:18:30 -04001072 /* special case where we should not overwrite
1073 * the device_index */
Benjamin Tissoires8dba3022017-03-27 16:59:21 +02001074 if (count == 7 && !memcmp(buf, unifying_pairing_query,
1075 sizeof(unifying_pairing_query)))
1076 buf[4] = (buf[4] & 0xf0) | (djdev->device_index - 1);
Benjamin Tissoires33797822014-09-30 13:18:30 -04001077 else
1078 buf[1] = djdev->device_index;
Hans de Goede0ee75542019-04-20 13:21:51 +02001079 return hid_hw_raw_request(djrcv_dev->hidpp, reportnum, buf,
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001080 count, report_type, reqtype);
1081 }
1082
Benjamin Tissoires0e40d352014-02-05 16:33:17 -05001083 if (buf[0] != REPORT_TYPE_LEDS)
1084 return -EINVAL;
1085
Hans de Goede74808f92019-04-20 13:21:54 +02001086 if (djrcv_dev->type != recvr_type_dj && count >= 2) {
1087 if (!djrcv_dev->keyboard) {
1088 hid_warn(hid, "Received REPORT_TYPE_LEDS request before the keyboard interface was enumerated\n");
1089 return 0;
1090 }
1091 /* usbhid overrides the report ID and ignores the first byte */
1092 return hid_hw_raw_request(djrcv_dev->keyboard, 0, buf, count,
1093 report_type, reqtype);
1094 }
1095
Benjamin Tissoires0e40d352014-02-05 16:33:17 -05001096 out_buf = kzalloc(DJREPORT_SHORT_LENGTH, GFP_ATOMIC);
1097 if (!out_buf)
1098 return -ENOMEM;
1099
Jiri Kosina51217e62014-08-21 09:56:47 -05001100 if (count > DJREPORT_SHORT_LENGTH - 2)
Benjamin Tissoires0e40d352014-02-05 16:33:17 -05001101 count = DJREPORT_SHORT_LENGTH - 2;
1102
1103 out_buf[0] = REPORT_ID_DJ_SHORT;
1104 out_buf[1] = djdev->device_index;
1105 memcpy(out_buf + 2, buf, count);
1106
Hans de Goede0ee75542019-04-20 13:21:51 +02001107 ret = hid_hw_raw_request(djrcv_dev->hidpp, out_buf[0], out_buf,
Benjamin Tissoiresbd27e202014-02-10 12:58:53 -05001108 DJREPORT_SHORT_LENGTH, report_type, reqtype);
Benjamin Tissoires0e40d352014-02-05 16:33:17 -05001109
1110 kfree(out_buf);
1111 return ret;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001112}
1113
Dan Carpenter6d603322013-10-19 12:08:59 +03001114static void rdcat(char *rdesc, unsigned int *rsize, const char *data, unsigned int size)
Henrik Rydberg2a039bf2012-04-22 14:21:39 +02001115{
Dan Carpenter6d603322013-10-19 12:08:59 +03001116 memcpy(rdesc + *rsize, data, size);
Henrik Rydberg2a039bf2012-04-22 14:21:39 +02001117 *rsize += size;
1118}
1119
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001120static int logi_dj_ll_parse(struct hid_device *hid)
1121{
1122 struct dj_device *djdev = hid->driver_data;
Henrik Rydberg2a039bf2012-04-22 14:21:39 +02001123 unsigned int rsize = 0;
1124 char *rdesc;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001125 int retval;
1126
1127 dbg_hid("%s\n", __func__);
1128
1129 djdev->hdev->version = 0x0111;
1130 djdev->hdev->country = 0x00;
1131
Henrik Rydberg2a039bf2012-04-22 14:21:39 +02001132 rdesc = kmalloc(MAX_RDESC_SIZE, GFP_KERNEL);
1133 if (!rdesc)
1134 return -ENOMEM;
1135
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001136 if (djdev->reports_supported & STD_KEYBOARD) {
1137 dbg_hid("%s: sending a kbd descriptor, reports_supported: %x\n",
1138 __func__, djdev->reports_supported);
Dan Carpenter6d603322013-10-19 12:08:59 +03001139 rdcat(rdesc, &rsize, kbd_descriptor, sizeof(kbd_descriptor));
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001140 }
1141
1142 if (djdev->reports_supported & STD_MOUSE) {
1143 dbg_hid("%s: sending a mouse descriptor, reports_supported: "
1144 "%x\n", __func__, djdev->reports_supported);
Dan Carpenter6d603322013-10-19 12:08:59 +03001145 rdcat(rdesc, &rsize, mse_descriptor, sizeof(mse_descriptor));
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001146 }
1147
1148 if (djdev->reports_supported & MULTIMEDIA) {
1149 dbg_hid("%s: sending a multimedia report descriptor: %x\n",
1150 __func__, djdev->reports_supported);
Dan Carpenter6d603322013-10-19 12:08:59 +03001151 rdcat(rdesc, &rsize, consumer_descriptor, sizeof(consumer_descriptor));
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001152 }
1153
1154 if (djdev->reports_supported & POWER_KEYS) {
1155 dbg_hid("%s: sending a power keys report descriptor: %x\n",
1156 __func__, djdev->reports_supported);
Dan Carpenter6d603322013-10-19 12:08:59 +03001157 rdcat(rdesc, &rsize, syscontrol_descriptor, sizeof(syscontrol_descriptor));
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001158 }
1159
1160 if (djdev->reports_supported & MEDIA_CENTER) {
1161 dbg_hid("%s: sending a media center report descriptor: %x\n",
1162 __func__, djdev->reports_supported);
Dan Carpenter6d603322013-10-19 12:08:59 +03001163 rdcat(rdesc, &rsize, media_descriptor, sizeof(media_descriptor));
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001164 }
1165
1166 if (djdev->reports_supported & KBD_LEDS) {
1167 dbg_hid("%s: need to send kbd leds report descriptor: %x\n",
1168 __func__, djdev->reports_supported);
1169 }
1170
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001171 rdcat(rdesc, &rsize, hidpp_descriptor, sizeof(hidpp_descriptor));
1172
Henrik Rydberg2a039bf2012-04-22 14:21:39 +02001173 retval = hid_parse_report(hid, rdesc, rsize);
1174 kfree(rdesc);
1175
1176 return retval;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001177}
1178
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001179static int logi_dj_ll_start(struct hid_device *hid)
1180{
1181 dbg_hid("%s\n", __func__);
1182 return 0;
1183}
1184
1185static void logi_dj_ll_stop(struct hid_device *hid)
1186{
1187 dbg_hid("%s\n", __func__);
1188}
1189
1190
1191static struct hid_ll_driver logi_dj_ll_driver = {
1192 .parse = logi_dj_ll_parse,
1193 .start = logi_dj_ll_start,
1194 .stop = logi_dj_ll_stop,
1195 .open = logi_dj_ll_open,
1196 .close = logi_dj_ll_close,
Benjamin Tissoiresbd27e202014-02-10 12:58:53 -05001197 .raw_request = logi_dj_ll_raw_request,
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001198};
1199
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001200static int logi_dj_dj_event(struct hid_device *hdev,
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001201 struct hid_report *report, u8 *data,
1202 int size)
1203{
1204 struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
1205 struct dj_report *dj_report = (struct dj_report *) data;
1206 unsigned long flags;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001207
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001208 /*
1209 * Here we receive all data coming from iface 2, there are 3 cases:
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001210 *
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001211 * 1) Data is intended for this driver i. e. data contains arrival,
1212 * departure, etc notifications, in which case we queue them for delayed
1213 * processing by the work queue. We return 1 to hid-core as no further
1214 * processing is required from it.
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001215 *
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001216 * 2) Data informs a connection change, if the change means rf link
1217 * loss, then we must send a null report to the upper layer to discard
1218 * potentially pressed keys that may be repeated forever by the input
1219 * layer. Return 1 to hid-core as no further processing is required.
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001220 *
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001221 * 3) Data is an actual input event from a paired DJ device in which
1222 * case we forward it to the correct hid device (via hid_input_report()
1223 * ) and return 1 so hid-core does not anything else with it.
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001224 */
Benjamin Tissoires5abfe852014-08-22 16:16:05 -04001225
Jiri Kosinaad3e14d2014-08-21 09:57:17 -05001226 if ((dj_report->device_index < DJ_DEVICE_INDEX_MIN) ||
1227 (dj_report->device_index > DJ_DEVICE_INDEX_MAX)) {
Benjamin Tissoires5abfe852014-08-22 16:16:05 -04001228 /*
1229 * Device index is wrong, bail out.
1230 * This driver can ignore safely the receiver notifications,
1231 * so ignore those reports too.
1232 */
1233 if (dj_report->device_index != DJ_RECEIVER_INDEX)
1234 dev_err(&hdev->dev, "%s: invalid device index:%d\n",
Jiri Kosinaad3e14d2014-08-21 09:57:17 -05001235 __func__, dj_report->device_index);
1236 return false;
1237 }
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001238
1239 spin_lock_irqsave(&djrcv_dev->lock, flags);
Benjamin Tissoires368d4e592014-08-22 16:16:06 -04001240
1241 if (!djrcv_dev->paired_dj_devices[dj_report->device_index]) {
1242 /* received an event for an unknown device, bail out */
1243 logi_dj_recv_queue_notification(djrcv_dev, dj_report);
1244 goto out;
1245 }
1246
Benjamin Tissoires5abfe852014-08-22 16:16:05 -04001247 switch (dj_report->report_type) {
1248 case REPORT_TYPE_NOTIF_DEVICE_PAIRED:
Benjamin Tissoires368d4e592014-08-22 16:16:06 -04001249 /* pairing notifications are handled above the switch */
1250 break;
Benjamin Tissoires5abfe852014-08-22 16:16:05 -04001251 case REPORT_TYPE_NOTIF_DEVICE_UNPAIRED:
1252 logi_dj_recv_queue_notification(djrcv_dev, dj_report);
1253 break;
1254 case REPORT_TYPE_NOTIF_CONNECTION_STATUS:
1255 if (dj_report->report_params[CONNECTION_STATUS_PARAM_STATUS] ==
1256 STATUS_LINKLOSS) {
1257 logi_dj_recv_forward_null_report(djrcv_dev, dj_report);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001258 }
Benjamin Tissoires5abfe852014-08-22 16:16:05 -04001259 break;
1260 default:
Benjamin Tissoires83898232019-04-20 13:21:43 +02001261 logi_dj_recv_forward_dj(djrcv_dev, dj_report);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001262 }
Benjamin Tissoires368d4e592014-08-22 16:16:06 -04001263
1264out:
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001265 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
1266
Benjamin Tissoires5abfe852014-08-22 16:16:05 -04001267 return true;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001268}
1269
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001270static int logi_dj_hidpp_event(struct hid_device *hdev,
1271 struct hid_report *report, u8 *data,
1272 int size)
1273{
1274 struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
Benjamin Tissoires7bb56a52019-04-20 13:21:44 +02001275 struct hidpp_event *hidpp_report = (struct hidpp_event *) data;
Hans de Goede74808f92019-04-20 13:21:54 +02001276 struct dj_device *dj_dev;
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001277 unsigned long flags;
Benjamin Tissoires7bb56a52019-04-20 13:21:44 +02001278 u8 device_index = hidpp_report->device_index;
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001279
Benjamin Tissoires33797822014-09-30 13:18:30 -04001280 if (device_index == HIDPP_RECEIVER_INDEX) {
1281 /* special case were the device wants to know its unifying
1282 * name */
1283 if (size == HIDPP_REPORT_LONG_LENGTH &&
Benjamin Tissoires8dba3022017-03-27 16:59:21 +02001284 !memcmp(data, unifying_pairing_answer,
1285 sizeof(unifying_pairing_answer)))
Benjamin Tissoires33797822014-09-30 13:18:30 -04001286 device_index = (data[4] & 0x0F) + 1;
1287 else
1288 return false;
1289 }
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001290
1291 /*
1292 * Data is from the HID++ collection, in this case, we forward the
1293 * data to the corresponding child dj device and return 0 to hid-core
1294 * so he data also goes to the hidraw device of the receiver. This
1295 * allows a user space application to implement the full HID++ routing
1296 * via the receiver.
1297 */
1298
1299 if ((device_index < DJ_DEVICE_INDEX_MIN) ||
1300 (device_index > DJ_DEVICE_INDEX_MAX)) {
1301 /*
1302 * Device index is wrong, bail out.
1303 * This driver can ignore safely the receiver notifications,
1304 * so ignore those reports too.
1305 */
1306 dev_err(&hdev->dev, "%s: invalid device index:%d\n",
Benjamin Tissoires7bb56a52019-04-20 13:21:44 +02001307 __func__, hidpp_report->device_index);
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001308 return false;
1309 }
1310
1311 spin_lock_irqsave(&djrcv_dev->lock, flags);
1312
Hans de Goede74808f92019-04-20 13:21:54 +02001313 dj_dev = djrcv_dev->paired_dj_devices[device_index];
1314 if (dj_dev) {
1315 logi_dj_recv_forward_report(dj_dev, data, size);
1316 } else {
1317 if (hidpp_report->sub_id == REPORT_TYPE_NOTIF_DEVICE_CONNECTED)
1318 logi_hidpp_recv_queue_notif(hdev, hidpp_report);
1319 else
1320 logi_dj_recv_queue_unknown_work(djrcv_dev);
1321 }
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001322
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001323 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
1324
1325 return false;
1326}
1327
1328static int logi_dj_raw_event(struct hid_device *hdev,
1329 struct hid_report *report, u8 *data,
1330 int size)
1331{
Hans de Goede74808f92019-04-20 13:21:54 +02001332 struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001333 dbg_hid("%s, size:%d\n", __func__, size);
1334
Hans de Goede74808f92019-04-20 13:21:54 +02001335 if (!hdev->report_enum[HID_INPUT_REPORT].numbered) {
1336
1337 if (djrcv_dev->unnumbered_application == HID_GD_KEYBOARD) {
1338 /*
1339 * For the keyboard, we can reuse the same report by
1340 * using the second byte which is constant in the USB
1341 * HID report descriptor.
1342 */
1343 data[1] = data[0];
1344 data[0] = REPORT_TYPE_KEYBOARD;
1345
1346 logi_dj_recv_forward_input_report(hdev, data, size);
1347
1348 /* restore previous state */
1349 data[0] = data[1];
1350 data[1] = 0;
1351 }
1352
1353 return false;
1354 }
1355
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001356 switch (data[0]) {
1357 case REPORT_ID_DJ_SHORT:
Peter Wuf254ae92014-12-16 16:55:21 +01001358 if (size != DJREPORT_SHORT_LENGTH) {
1359 dev_err(&hdev->dev, "DJ report of bad size (%d)", size);
1360 return false;
1361 }
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001362 return logi_dj_dj_event(hdev, report, data, size);
1363 case REPORT_ID_HIDPP_SHORT:
Peter Wuf254ae92014-12-16 16:55:21 +01001364 if (size != HIDPP_REPORT_SHORT_LENGTH) {
1365 dev_err(&hdev->dev,
1366 "Short HID++ report of bad size (%d)", size);
1367 return false;
1368 }
1369 return logi_dj_hidpp_event(hdev, report, data, size);
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001370 case REPORT_ID_HIDPP_LONG:
Peter Wuf254ae92014-12-16 16:55:21 +01001371 if (size != HIDPP_REPORT_LONG_LENGTH) {
1372 dev_err(&hdev->dev,
1373 "Long HID++ report of bad size (%d)", size);
1374 return false;
1375 }
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001376 return logi_dj_hidpp_event(hdev, report, data, size);
1377 }
1378
Hans de Goede74808f92019-04-20 13:21:54 +02001379 logi_dj_recv_forward_input_report(hdev, data, size);
1380
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001381 return false;
1382}
1383
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001384static int logi_dj_probe(struct hid_device *hdev,
1385 const struct hid_device_id *id)
1386{
Benjamin Tissoires82c0beb2019-04-20 13:21:47 +02001387 struct hid_report_enum *rep_enum;
1388 struct hid_report *rep;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001389 struct dj_receiver_dev *djrcv_dev;
Benjamin Tissoires82c0beb2019-04-20 13:21:47 +02001390 bool has_hidpp = false;
Hans de Goedea1d97cc2019-04-20 13:21:52 +02001391 unsigned long flags;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001392 int retval;
1393
Benjamin Tissoires82c0beb2019-04-20 13:21:47 +02001394 /*
1395 * Call to usbhid to fetch the HID descriptors of the current
1396 * interface subsequently call to the hid/hid-core to parse the
1397 * fetched descriptors.
1398 */
1399 retval = hid_parse(hdev);
1400 if (retval) {
1401 dev_err(&hdev->dev,
1402 "%s:parse failed\n", __func__);
1403 return retval;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001404 }
1405
Benjamin Tissoires82c0beb2019-04-20 13:21:47 +02001406 rep_enum = &hdev->report_enum[HID_INPUT_REPORT];
1407
Hans de Goede74808f92019-04-20 13:21:54 +02001408 /* no input reports, bail out */
1409 if (list_empty(&rep_enum->report_list))
1410 return -ENODEV;
1411
Benjamin Tissoires82c0beb2019-04-20 13:21:47 +02001412 /*
1413 * Check for the HID++ application.
1414 * Note: we should theoretically check for HID++ and DJ
1415 * collections, but this will do.
1416 */
1417 list_for_each_entry(rep, &rep_enum->report_list, list) {
1418 if (rep->application == 0xff000001)
1419 has_hidpp = true;
1420 }
1421
1422 /*
1423 * Ignore interfaces without DJ/HID++ collection, they will not carry
1424 * any data, dont create any hid_device for them.
1425 */
Hans de Goede74808f92019-04-20 13:21:54 +02001426 if (!has_hidpp && id->driver_data == recvr_type_dj)
Benjamin Tissoires82c0beb2019-04-20 13:21:47 +02001427 return -ENODEV;
1428
Hans de Goedea1d97cc2019-04-20 13:21:52 +02001429 /* get the current application attached to the node */
1430 rep = list_first_entry(&rep_enum->report_list, struct hid_report, list);
Hans de Goede74808f92019-04-20 13:21:54 +02001431 djrcv_dev = dj_get_receiver_dev(hdev, id->driver_data,
Hans de Goedea1d97cc2019-04-20 13:21:52 +02001432 rep->application, has_hidpp);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001433 if (!djrcv_dev) {
1434 dev_err(&hdev->dev,
1435 "%s:failed allocating dj_receiver_dev\n", __func__);
1436 return -ENOMEM;
1437 }
Kees Cook297502a2013-09-11 21:56:56 +02001438
Hans de Goede74808f92019-04-20 13:21:54 +02001439 if (!rep_enum->numbered)
1440 djrcv_dev->unnumbered_application = rep->application;
1441
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001442 /* Starts the usb device and connects to upper interfaces hiddev and
1443 * hidraw */
Hans de Goede74808f92019-04-20 13:21:54 +02001444 retval = hid_hw_start(hdev, HID_CONNECT_HIDRAW|HID_CONNECT_HIDDEV);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001445 if (retval) {
1446 dev_err(&hdev->dev,
1447 "%s:hid_hw_start returned error\n", __func__);
1448 goto hid_hw_start_fail;
1449 }
1450
Hans de Goede74808f92019-04-20 13:21:54 +02001451 if (has_hidpp) {
1452 retval = logi_dj_recv_switch_to_dj_mode(djrcv_dev, 0);
1453 if (retval < 0) {
1454 hid_err(hdev, "%s: logi_dj_recv_switch_to_dj_mode returned error:%d\n",
1455 __func__, retval);
1456 goto switch_to_dj_mode_fail;
1457 }
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001458 }
1459
1460 /* This is enabling the polling urb on the IN endpoint */
Benjamin Tissoiresddf75402013-07-12 11:01:02 +02001461 retval = hid_hw_open(hdev);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001462 if (retval < 0) {
Benjamin Tissoiresddf75402013-07-12 11:01:02 +02001463 dev_err(&hdev->dev, "%s:hid_hw_open returned error:%d\n",
1464 __func__, retval);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001465 goto llopen_failed;
1466 }
1467
Andrew de los Reyesa9dd22b72013-02-18 09:20:22 -08001468 /* Allow incoming packets to arrive: */
1469 hid_device_io_start(hdev);
1470
Hans de Goede74808f92019-04-20 13:21:54 +02001471 if (has_hidpp) {
1472 spin_lock_irqsave(&djrcv_dev->lock, flags);
1473 djrcv_dev->ready = true;
1474 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
1475 retval = logi_dj_recv_query_paired_devices(djrcv_dev);
1476 if (retval < 0) {
1477 hid_err(hdev, "%s: logi_dj_recv_query_paired_devices error:%d\n",
1478 __func__, retval);
1479 goto logi_dj_recv_query_paired_devices_failed;
1480 }
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001481 }
1482
1483 return retval;
1484
1485logi_dj_recv_query_paired_devices_failed:
Benjamin Tissoiresddf75402013-07-12 11:01:02 +02001486 hid_hw_close(hdev);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001487
1488llopen_failed:
1489switch_to_dj_mode_fail:
1490 hid_hw_stop(hdev);
1491
1492hid_hw_start_fail:
Hans de Goedea1d97cc2019-04-20 13:21:52 +02001493 dj_put_receiver_dev(hdev);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001494 return retval;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001495}
1496
1497#ifdef CONFIG_PM
1498static int logi_dj_reset_resume(struct hid_device *hdev)
1499{
1500 int retval;
1501 struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
1502
Hans de Goede74808f92019-04-20 13:21:54 +02001503 if (djrcv_dev->hidpp != hdev)
1504 return 0;
1505
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001506 retval = logi_dj_recv_switch_to_dj_mode(djrcv_dev, 0);
1507 if (retval < 0) {
1508 dev_err(&hdev->dev,
1509 "%s:logi_dj_recv_switch_to_dj_mode returned error:%d\n",
1510 __func__, retval);
1511 }
1512
1513 return 0;
1514}
1515#endif
1516
1517static void logi_dj_remove(struct hid_device *hdev)
1518{
1519 struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
1520 struct dj_device *dj_dev;
Hans de Goedea1d97cc2019-04-20 13:21:52 +02001521 unsigned long flags;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001522 int i;
1523
1524 dbg_hid("%s\n", __func__);
1525
Hans de Goedea1d97cc2019-04-20 13:21:52 +02001526 /*
1527 * This ensures that if the work gets requeued from another
1528 * interface of the same receiver it will be a no-op.
1529 */
1530 spin_lock_irqsave(&djrcv_dev->lock, flags);
1531 djrcv_dev->ready = false;
1532 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
1533
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001534 cancel_work_sync(&djrcv_dev->work);
1535
Benjamin Tissoiresddf75402013-07-12 11:01:02 +02001536 hid_hw_close(hdev);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001537 hid_hw_stop(hdev);
1538
Hans de Goedea1d97cc2019-04-20 13:21:52 +02001539 /*
1540 * For proper operation we need access to all interfaces, so we destroy
1541 * the paired devices when we're unbound from any interface.
1542 *
1543 * Note we may still be bound to other interfaces, sharing the same
1544 * djrcv_dev, so we need locking here.
1545 */
Nestor Lopez Casado844580f2011-09-20 15:59:03 +02001546 for (i = 0; i < (DJ_MAX_PAIRED_DEVICES + DJ_DEVICE_INDEX_MIN); i++) {
Hans de Goedea1d97cc2019-04-20 13:21:52 +02001547 spin_lock_irqsave(&djrcv_dev->lock, flags);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001548 dj_dev = djrcv_dev->paired_dj_devices[i];
Hans de Goedea1d97cc2019-04-20 13:21:52 +02001549 djrcv_dev->paired_dj_devices[i] = NULL;
1550 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001551 if (dj_dev != NULL) {
1552 hid_destroy_device(dj_dev->hdev);
1553 kfree(dj_dev);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001554 }
1555 }
1556
Hans de Goedea1d97cc2019-04-20 13:21:52 +02001557 dj_put_receiver_dev(hdev);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001558}
1559
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001560static const struct hid_device_id logi_dj_receivers[] = {
1561 {HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
Hans de Goede74808f92019-04-20 13:21:54 +02001562 USB_DEVICE_ID_LOGITECH_UNIFYING_RECEIVER),
1563 .driver_data = recvr_type_dj},
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001564 {HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
Hans de Goede74808f92019-04-20 13:21:54 +02001565 USB_DEVICE_ID_LOGITECH_UNIFYING_RECEIVER_2),
1566 .driver_data = recvr_type_dj},
1567 { /* Logitech Nano (non DJ) receiver */
1568 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
1569 USB_DEVICE_ID_LOGITECH_NANO_RECEIVER),
1570 .driver_data = recvr_type_hidpp},
1571 { /* Logitech Nano (non DJ) receiver */
1572 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
1573 USB_DEVICE_ID_LOGITECH_NANO_RECEIVER_2),
1574 .driver_data = recvr_type_hidpp},
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001575 {}
1576};
1577
1578MODULE_DEVICE_TABLE(hid, logi_dj_receivers);
1579
1580static struct hid_driver logi_djreceiver_driver = {
1581 .name = "logitech-djreceiver",
1582 .id_table = logi_dj_receivers,
1583 .probe = logi_dj_probe,
1584 .remove = logi_dj_remove,
1585 .raw_event = logi_dj_raw_event,
1586#ifdef CONFIG_PM
1587 .reset_resume = logi_dj_reset_resume,
1588#endif
1589};
1590
Benjamin Tissoiresab94e562014-09-30 13:18:28 -04001591module_hid_driver(logi_djreceiver_driver);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001592
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001593MODULE_LICENSE("GPL");
1594MODULE_AUTHOR("Logitech");
1595MODULE_AUTHOR("Nestor Lopez Casado");
1596MODULE_AUTHOR("nlopezcasad@logitech.com");