blob: b1ae2d9c5ddcbfe75a8aa6c21489d8e2b1f14ca3 [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>
28#include <linux/usb.h>
Benjamin Tissoires8cb37462014-09-30 13:18:26 -040029#include <linux/kfifo.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
34#define DJ_MAX_NUMBER_NOTIFICATIONS 8
35#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
77
78/* 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)
Benjamin Tissoires8cb37462014-09-30 13:18:26 -0400103
104struct dj_report {
105 u8 report_id;
106 u8 device_index;
107 u8 report_type;
108 u8 report_params[DJREPORT_SHORT_LENGTH - 3];
109};
110
Benjamin Tissoires7bb56a52019-04-20 13:21:44 +0200111struct hidpp_event {
112 u8 report_id;
113 u8 device_index;
114 u8 sub_id;
115 u8 params[HIDPP_REPORT_LONG_LENGTH - 3U];
116} __packed;
117
Benjamin Tissoires8cb37462014-09-30 13:18:26 -0400118struct dj_receiver_dev {
119 struct hid_device *hdev;
120 struct dj_device *paired_dj_devices[DJ_MAX_PAIRED_DEVICES +
121 DJ_DEVICE_INDEX_MIN];
122 struct work_struct work;
123 struct kfifo notif_fifo;
124 spinlock_t lock;
125 bool querying_devices;
126};
127
128struct dj_device {
129 struct hid_device *hdev;
130 struct dj_receiver_dev *dj_receiver_dev;
131 u32 reports_supported;
132 u8 device_index;
133};
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200134
135/* Keyboard descriptor (1) */
136static const char kbd_descriptor[] = {
137 0x05, 0x01, /* USAGE_PAGE (generic Desktop) */
138 0x09, 0x06, /* USAGE (Keyboard) */
139 0xA1, 0x01, /* COLLECTION (Application) */
140 0x85, 0x01, /* REPORT_ID (1) */
141 0x95, 0x08, /* REPORT_COUNT (8) */
142 0x75, 0x01, /* REPORT_SIZE (1) */
143 0x15, 0x00, /* LOGICAL_MINIMUM (0) */
144 0x25, 0x01, /* LOGICAL_MAXIMUM (1) */
145 0x05, 0x07, /* USAGE_PAGE (Keyboard) */
146 0x19, 0xE0, /* USAGE_MINIMUM (Left Control) */
147 0x29, 0xE7, /* USAGE_MAXIMUM (Right GUI) */
148 0x81, 0x02, /* INPUT (Data,Var,Abs) */
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200149 0x95, 0x06, /* REPORT_COUNT (6) */
150 0x75, 0x08, /* REPORT_SIZE (8) */
151 0x15, 0x00, /* LOGICAL_MINIMUM (0) */
152 0x26, 0xFF, 0x00, /* LOGICAL_MAXIMUM (255) */
153 0x05, 0x07, /* USAGE_PAGE (Keyboard) */
154 0x19, 0x00, /* USAGE_MINIMUM (no event) */
155 0x2A, 0xFF, 0x00, /* USAGE_MAXIMUM (reserved) */
156 0x81, 0x00, /* INPUT (Data,Ary,Abs) */
Benjamin Tissoires0e40d352014-02-05 16:33:17 -0500157 0x85, 0x0e, /* REPORT_ID (14) */
158 0x05, 0x08, /* USAGE PAGE (LED page) */
159 0x95, 0x05, /* REPORT COUNT (5) */
160 0x75, 0x01, /* REPORT SIZE (1) */
161 0x15, 0x00, /* LOGICAL_MINIMUM (0) */
162 0x25, 0x01, /* LOGICAL_MAXIMUM (1) */
163 0x19, 0x01, /* USAGE MINIMUM (1) */
164 0x29, 0x05, /* USAGE MAXIMUM (5) */
165 0x91, 0x02, /* OUTPUT (Data, Variable, Absolute) */
166 0x95, 0x01, /* REPORT COUNT (1) */
167 0x75, 0x03, /* REPORT SIZE (3) */
168 0x91, 0x01, /* OUTPUT (Constant) */
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200169 0xC0
170};
171
172/* Mouse descriptor (2) */
173static const char mse_descriptor[] = {
174 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */
175 0x09, 0x02, /* USAGE (Mouse) */
176 0xA1, 0x01, /* COLLECTION (Application) */
177 0x85, 0x02, /* REPORT_ID = 2 */
178 0x09, 0x01, /* USAGE (pointer) */
179 0xA1, 0x00, /* COLLECTION (physical) */
180 0x05, 0x09, /* USAGE_PAGE (buttons) */
181 0x19, 0x01, /* USAGE_MIN (1) */
182 0x29, 0x10, /* USAGE_MAX (16) */
183 0x15, 0x00, /* LOGICAL_MIN (0) */
184 0x25, 0x01, /* LOGICAL_MAX (1) */
185 0x95, 0x10, /* REPORT_COUNT (16) */
186 0x75, 0x01, /* REPORT_SIZE (1) */
187 0x81, 0x02, /* INPUT (data var abs) */
188 0x05, 0x01, /* USAGE_PAGE (generic desktop) */
189 0x16, 0x01, 0xF8, /* LOGICAL_MIN (-2047) */
190 0x26, 0xFF, 0x07, /* LOGICAL_MAX (2047) */
191 0x75, 0x0C, /* REPORT_SIZE (12) */
192 0x95, 0x02, /* REPORT_COUNT (2) */
193 0x09, 0x30, /* USAGE (X) */
194 0x09, 0x31, /* USAGE (Y) */
195 0x81, 0x06, /* INPUT */
196 0x15, 0x81, /* LOGICAL_MIN (-127) */
197 0x25, 0x7F, /* LOGICAL_MAX (127) */
198 0x75, 0x08, /* REPORT_SIZE (8) */
199 0x95, 0x01, /* REPORT_COUNT (1) */
200 0x09, 0x38, /* USAGE (wheel) */
201 0x81, 0x06, /* INPUT */
202 0x05, 0x0C, /* USAGE_PAGE(consumer) */
203 0x0A, 0x38, 0x02, /* USAGE(AC Pan) */
204 0x95, 0x01, /* REPORT_COUNT (1) */
205 0x81, 0x06, /* INPUT */
206 0xC0, /* END_COLLECTION */
207 0xC0, /* END_COLLECTION */
208};
209
210/* Consumer Control descriptor (3) */
211static const char consumer_descriptor[] = {
212 0x05, 0x0C, /* USAGE_PAGE (Consumer Devices) */
213 0x09, 0x01, /* USAGE (Consumer Control) */
214 0xA1, 0x01, /* COLLECTION (Application) */
215 0x85, 0x03, /* REPORT_ID = 3 */
216 0x75, 0x10, /* REPORT_SIZE (16) */
217 0x95, 0x02, /* REPORT_COUNT (2) */
218 0x15, 0x01, /* LOGICAL_MIN (1) */
219 0x26, 0x8C, 0x02, /* LOGICAL_MAX (652) */
220 0x19, 0x01, /* USAGE_MIN (1) */
221 0x2A, 0x8C, 0x02, /* USAGE_MAX (652) */
222 0x81, 0x00, /* INPUT (Data Ary Abs) */
223 0xC0, /* END_COLLECTION */
224}; /* */
225
226/* System control descriptor (4) */
227static const char syscontrol_descriptor[] = {
228 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */
229 0x09, 0x80, /* USAGE (System Control) */
230 0xA1, 0x01, /* COLLECTION (Application) */
231 0x85, 0x04, /* REPORT_ID = 4 */
232 0x75, 0x02, /* REPORT_SIZE (2) */
233 0x95, 0x01, /* REPORT_COUNT (1) */
234 0x15, 0x01, /* LOGICAL_MIN (1) */
235 0x25, 0x03, /* LOGICAL_MAX (3) */
236 0x09, 0x82, /* USAGE (System Sleep) */
237 0x09, 0x81, /* USAGE (System Power Down) */
238 0x09, 0x83, /* USAGE (System Wake Up) */
239 0x81, 0x60, /* INPUT (Data Ary Abs NPrf Null) */
240 0x75, 0x06, /* REPORT_SIZE (6) */
241 0x81, 0x03, /* INPUT (Cnst Var Abs) */
242 0xC0, /* END_COLLECTION */
243};
244
245/* Media descriptor (8) */
246static const char media_descriptor[] = {
247 0x06, 0xbc, 0xff, /* Usage Page 0xffbc */
248 0x09, 0x88, /* Usage 0x0088 */
249 0xa1, 0x01, /* BeginCollection */
250 0x85, 0x08, /* Report ID 8 */
251 0x19, 0x01, /* Usage Min 0x0001 */
252 0x29, 0xff, /* Usage Max 0x00ff */
253 0x15, 0x01, /* Logical Min 1 */
254 0x26, 0xff, 0x00, /* Logical Max 255 */
255 0x75, 0x08, /* Report Size 8 */
256 0x95, 0x01, /* Report Count 1 */
257 0x81, 0x00, /* Input */
258 0xc0, /* EndCollection */
259}; /* */
260
Benjamin Tissoires925f0f32014-09-30 13:18:29 -0400261/* HIDPP descriptor */
262static const char hidpp_descriptor[] = {
263 0x06, 0x00, 0xff, /* Usage Page (Vendor Defined Page 1) */
264 0x09, 0x01, /* Usage (Vendor Usage 1) */
265 0xa1, 0x01, /* Collection (Application) */
266 0x85, 0x10, /* Report ID (16) */
267 0x75, 0x08, /* Report Size (8) */
268 0x95, 0x06, /* Report Count (6) */
269 0x15, 0x00, /* Logical Minimum (0) */
270 0x26, 0xff, 0x00, /* Logical Maximum (255) */
271 0x09, 0x01, /* Usage (Vendor Usage 1) */
272 0x81, 0x00, /* Input (Data,Arr,Abs) */
273 0x09, 0x01, /* Usage (Vendor Usage 1) */
274 0x91, 0x00, /* Output (Data,Arr,Abs) */
275 0xc0, /* End Collection */
276 0x06, 0x00, 0xff, /* Usage Page (Vendor Defined Page 1) */
277 0x09, 0x02, /* Usage (Vendor Usage 2) */
278 0xa1, 0x01, /* Collection (Application) */
279 0x85, 0x11, /* Report ID (17) */
280 0x75, 0x08, /* Report Size (8) */
281 0x95, 0x13, /* Report Count (19) */
282 0x15, 0x00, /* Logical Minimum (0) */
283 0x26, 0xff, 0x00, /* Logical Maximum (255) */
284 0x09, 0x02, /* Usage (Vendor Usage 2) */
285 0x81, 0x00, /* Input (Data,Arr,Abs) */
286 0x09, 0x02, /* Usage (Vendor Usage 2) */
287 0x91, 0x00, /* Output (Data,Arr,Abs) */
288 0xc0, /* End Collection */
289 0x06, 0x00, 0xff, /* Usage Page (Vendor Defined Page 1) */
290 0x09, 0x04, /* Usage (Vendor Usage 0x04) */
291 0xa1, 0x01, /* Collection (Application) */
292 0x85, 0x20, /* Report ID (32) */
293 0x75, 0x08, /* Report Size (8) */
294 0x95, 0x0e, /* Report Count (14) */
295 0x15, 0x00, /* Logical Minimum (0) */
296 0x26, 0xff, 0x00, /* Logical Maximum (255) */
297 0x09, 0x41, /* Usage (Vendor Usage 0x41) */
298 0x81, 0x00, /* Input (Data,Arr,Abs) */
299 0x09, 0x41, /* Usage (Vendor Usage 0x41) */
300 0x91, 0x00, /* Output (Data,Arr,Abs) */
301 0x85, 0x21, /* Report ID (33) */
302 0x95, 0x1f, /* Report Count (31) */
303 0x15, 0x00, /* Logical Minimum (0) */
304 0x26, 0xff, 0x00, /* Logical Maximum (255) */
305 0x09, 0x42, /* Usage (Vendor Usage 0x42) */
306 0x81, 0x00, /* Input (Data,Arr,Abs) */
307 0x09, 0x42, /* Usage (Vendor Usage 0x42) */
308 0x91, 0x00, /* Output (Data,Arr,Abs) */
309 0xc0, /* End Collection */
310};
311
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200312/* Maximum size of all defined hid reports in bytes (including report id) */
313#define MAX_REPORT_SIZE 8
314
Henrik Rydberg2a039bf2012-04-22 14:21:39 +0200315/* Make sure all descriptors are present here */
316#define MAX_RDESC_SIZE \
317 (sizeof(kbd_descriptor) + \
318 sizeof(mse_descriptor) + \
319 sizeof(consumer_descriptor) + \
320 sizeof(syscontrol_descriptor) + \
Benjamin Tissoires925f0f32014-09-30 13:18:29 -0400321 sizeof(media_descriptor) + \
322 sizeof(hidpp_descriptor))
Henrik Rydberg2a039bf2012-04-22 14:21:39 +0200323
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200324/* Number of possible hid report types that can be created by this driver.
325 *
326 * Right now, RF report types have the same report types (or report id's)
327 * than the hid report created from those RF reports. In the future
328 * this doesnt have to be true.
329 *
330 * For instance, RF report type 0x01 which has a size of 8 bytes, corresponds
331 * to hid report id 0x01, this is standard keyboard. Same thing applies to mice
332 * reports and consumer control, etc. If a new RF report is created, it doesn't
333 * has to have the same report id as its corresponding hid report, so an
334 * translation may have to take place for future report types.
335 */
336#define NUMBER_OF_HID_REPORTS 32
337static const u8 hid_reportid_size_map[NUMBER_OF_HID_REPORTS] = {
338 [1] = 8, /* Standard keyboard */
339 [2] = 8, /* Standard mouse */
340 [3] = 5, /* Consumer control */
341 [4] = 2, /* System control */
342 [8] = 2, /* Media Center */
343};
344
345
346#define LOGITECH_DJ_INTERFACE_NUMBER 0x02
347
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200348static struct hid_ll_driver logi_dj_ll_driver;
349
Nestor Lopez Casadoc63e0e32013-07-18 06:21:30 -0700350static int logi_dj_recv_query_paired_devices(struct dj_receiver_dev *djrcv_dev);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200351
352static void logi_dj_recv_destroy_djhid_device(struct dj_receiver_dev *djrcv_dev,
353 struct dj_report *dj_report)
354{
355 /* Called in delayed work context */
356 struct dj_device *dj_dev;
357 unsigned long flags;
358
359 spin_lock_irqsave(&djrcv_dev->lock, flags);
360 dj_dev = djrcv_dev->paired_dj_devices[dj_report->device_index];
361 djrcv_dev->paired_dj_devices[dj_report->device_index] = NULL;
362 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
363
364 if (dj_dev != NULL) {
365 hid_destroy_device(dj_dev->hdev);
366 kfree(dj_dev);
367 } else {
368 dev_err(&djrcv_dev->hdev->dev, "%s: can't destroy a NULL device\n",
369 __func__);
370 }
371}
372
373static void logi_dj_recv_add_djhid_device(struct dj_receiver_dev *djrcv_dev,
374 struct dj_report *dj_report)
375{
376 /* Called in delayed work context */
377 struct hid_device *djrcv_hdev = djrcv_dev->hdev;
378 struct usb_interface *intf = to_usb_interface(djrcv_hdev->dev.parent);
379 struct usb_device *usbdev = interface_to_usbdev(intf);
380 struct hid_device *dj_hiddev;
381 struct dj_device *dj_dev;
382
383 /* Device index goes from 1 to 6, we need 3 bytes to store the
384 * semicolon, the index, and a null terminator
385 */
386 unsigned char tmpstr[3];
387
388 if (dj_report->report_params[DEVICE_PAIRED_PARAM_SPFUNCTION] &
389 SPFUNCTION_DEVICE_LIST_EMPTY) {
390 dbg_hid("%s: device list is empty\n", __func__);
Nestor Lopez Casadoc63e0e32013-07-18 06:21:30 -0700391 djrcv_dev->querying_devices = false;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200392 return;
393 }
394
Nestor Lopez Casadoc63e0e32013-07-18 06:21:30 -0700395 if (djrcv_dev->paired_dj_devices[dj_report->device_index]) {
396 /* The device is already known. No need to reallocate it. */
397 dbg_hid("%s: device is already known\n", __func__);
398 return;
399 }
400
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200401 dj_hiddev = hid_allocate_device();
402 if (IS_ERR(dj_hiddev)) {
403 dev_err(&djrcv_hdev->dev, "%s: hid_allocate_device failed\n",
404 __func__);
405 return;
406 }
407
408 dj_hiddev->ll_driver = &logi_dj_ll_driver;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200409
410 dj_hiddev->dev.parent = &djrcv_hdev->dev;
411 dj_hiddev->bus = BUS_USB;
412 dj_hiddev->vendor = le16_to_cpu(usbdev->descriptor.idVendor);
Benjamin Tissoiresd6102742014-09-30 13:18:25 -0400413 dj_hiddev->product =
414 (dj_report->report_params[DEVICE_PAIRED_PARAM_EQUAD_ID_MSB]
415 << 8) |
416 dj_report->report_params[DEVICE_PAIRED_PARAM_EQUAD_ID_LSB];
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200417 snprintf(dj_hiddev->name, sizeof(dj_hiddev->name),
Benjamin Tissoiresd6102742014-09-30 13:18:25 -0400418 "Logitech Unifying Device. Wireless PID:%04x",
419 dj_hiddev->product);
420
421 dj_hiddev->group = HID_GROUP_LOGITECH_DJ_DEVICE;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200422
423 usb_make_path(usbdev, dj_hiddev->phys, sizeof(dj_hiddev->phys));
424 snprintf(tmpstr, sizeof(tmpstr), ":%d", dj_report->device_index);
425 strlcat(dj_hiddev->phys, tmpstr, sizeof(dj_hiddev->phys));
426
427 dj_dev = kzalloc(sizeof(struct dj_device), GFP_KERNEL);
428
429 if (!dj_dev) {
430 dev_err(&djrcv_hdev->dev, "%s: failed allocating dj_device\n",
431 __func__);
432 goto dj_device_allocate_fail;
433 }
434
Jonathan Nieder44d27f72012-05-11 16:17:16 +0200435 dj_dev->reports_supported = get_unaligned_le32(
436 dj_report->report_params + DEVICE_PAIRED_RF_REPORT_TYPE);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200437 dj_dev->hdev = dj_hiddev;
438 dj_dev->dj_receiver_dev = djrcv_dev;
439 dj_dev->device_index = dj_report->device_index;
440 dj_hiddev->driver_data = dj_dev;
441
442 djrcv_dev->paired_dj_devices[dj_report->device_index] = dj_dev;
443
444 if (hid_add_device(dj_hiddev)) {
445 dev_err(&djrcv_hdev->dev, "%s: failed adding dj_device\n",
446 __func__);
447 goto hid_add_device_fail;
448 }
449
450 return;
451
452hid_add_device_fail:
453 djrcv_dev->paired_dj_devices[dj_report->device_index] = NULL;
454 kfree(dj_dev);
455dj_device_allocate_fail:
456 hid_destroy_device(dj_hiddev);
457}
458
459static void delayedwork_callback(struct work_struct *work)
460{
461 struct dj_receiver_dev *djrcv_dev =
462 container_of(work, struct dj_receiver_dev, work);
463
464 struct dj_report dj_report;
465 unsigned long flags;
466 int count;
Nestor Lopez Casadoc63e0e32013-07-18 06:21:30 -0700467 int retval;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200468
469 dbg_hid("%s\n", __func__);
470
471 spin_lock_irqsave(&djrcv_dev->lock, flags);
472
473 count = kfifo_out(&djrcv_dev->notif_fifo, &dj_report,
474 sizeof(struct dj_report));
475
476 if (count != sizeof(struct dj_report)) {
477 dev_err(&djrcv_dev->hdev->dev, "%s: workitem triggered without "
478 "notifications available\n", __func__);
479 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
480 return;
481 }
482
483 if (!kfifo_is_empty(&djrcv_dev->notif_fifo)) {
484 if (schedule_work(&djrcv_dev->work) == 0) {
485 dbg_hid("%s: did not schedule the work item, was "
486 "already queued\n", __func__);
487 }
488 }
489
490 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
491
492 switch (dj_report.report_type) {
493 case REPORT_TYPE_NOTIF_DEVICE_PAIRED:
494 logi_dj_recv_add_djhid_device(djrcv_dev, &dj_report);
495 break;
496 case REPORT_TYPE_NOTIF_DEVICE_UNPAIRED:
497 logi_dj_recv_destroy_djhid_device(djrcv_dev, &dj_report);
498 break;
499 default:
Nestor Lopez Casadoc63e0e32013-07-18 06:21:30 -0700500 /* A normal report (i. e. not belonging to a pair/unpair notification)
501 * arriving here, means that the report arrived but we did not have a
502 * paired dj_device associated to the report's device_index, this
503 * means that the original "device paired" notification corresponding
504 * to this dj_device never arrived to this driver. The reason is that
505 * hid-core discards all packets coming from a device while probe() is
506 * executing. */
507 if (!djrcv_dev->paired_dj_devices[dj_report.device_index]) {
508 /* ok, we don't know the device, just re-ask the
509 * receiver for the list of connected devices. */
510 retval = logi_dj_recv_query_paired_devices(djrcv_dev);
511 if (!retval) {
512 /* everything went fine, so just leave */
513 break;
514 }
515 dev_err(&djrcv_dev->hdev->dev,
516 "%s:logi_dj_recv_query_paired_devices "
517 "error:%d\n", __func__, retval);
518 }
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200519 dbg_hid("%s: unexpected report type\n", __func__);
520 }
521}
522
523static void logi_dj_recv_queue_notification(struct dj_receiver_dev *djrcv_dev,
524 struct dj_report *dj_report)
525{
526 /* We are called from atomic context (tasklet && djrcv->lock held) */
527
528 kfifo_in(&djrcv_dev->notif_fifo, dj_report, sizeof(struct dj_report));
529
530 if (schedule_work(&djrcv_dev->work) == 0) {
531 dbg_hid("%s: did not schedule the work item, was already "
532 "queued\n", __func__);
533 }
534}
535
536static void logi_dj_recv_forward_null_report(struct dj_receiver_dev *djrcv_dev,
537 struct dj_report *dj_report)
538{
539 /* We are called from atomic context (tasklet && djrcv->lock held) */
540 unsigned int i;
541 u8 reportbuffer[MAX_REPORT_SIZE];
542 struct dj_device *djdev;
543
544 djdev = djrcv_dev->paired_dj_devices[dj_report->device_index];
545
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200546 memset(reportbuffer, 0, sizeof(reportbuffer));
547
548 for (i = 0; i < NUMBER_OF_HID_REPORTS; i++) {
549 if (djdev->reports_supported & (1 << i)) {
550 reportbuffer[0] = i;
551 if (hid_input_report(djdev->hdev,
552 HID_INPUT_REPORT,
553 reportbuffer,
554 hid_reportid_size_map[i], 1)) {
555 dbg_hid("hid_input_report error sending null "
556 "report\n");
557 }
558 }
559 }
560}
561
Benjamin Tissoires83898232019-04-20 13:21:43 +0200562static void logi_dj_recv_forward_dj(struct dj_receiver_dev *djrcv_dev,
563 struct dj_report *dj_report)
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200564{
565 /* We are called from atomic context (tasklet && djrcv->lock held) */
566 struct dj_device *dj_device;
567
568 dj_device = djrcv_dev->paired_dj_devices[dj_report->device_index];
569
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200570 if ((dj_report->report_type > ARRAY_SIZE(hid_reportid_size_map) - 1) ||
571 (hid_reportid_size_map[dj_report->report_type] == 0)) {
572 dbg_hid("invalid report type:%x\n", dj_report->report_type);
573 return;
574 }
575
576 if (hid_input_report(dj_device->hdev,
577 HID_INPUT_REPORT, &dj_report->report_type,
578 hid_reportid_size_map[dj_report->report_type], 1)) {
579 dbg_hid("hid_input_report error\n");
580 }
581}
582
Benjamin Tissoires83898232019-04-20 13:21:43 +0200583static void logi_dj_recv_forward_report(struct dj_device *dj_dev, u8 *data,
584 int size)
Benjamin Tissoires925f0f32014-09-30 13:18:29 -0400585{
586 /* We are called from atomic context (tasklet && djrcv->lock held) */
587 if (hid_input_report(dj_dev->hdev, HID_INPUT_REPORT, data, size, 1))
588 dbg_hid("hid_input_report error\n");
589}
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200590
591static int logi_dj_recv_send_report(struct dj_receiver_dev *djrcv_dev,
592 struct dj_report *dj_report)
593{
594 struct hid_device *hdev = djrcv_dev->hdev;
Benjamin Tissoiresdcd90062013-03-05 17:09:00 +0100595 struct hid_report *report;
596 struct hid_report_enum *output_report_enum;
597 u8 *data = (u8 *)(&dj_report->device_index);
Kees Cook297502a2013-09-11 21:56:56 +0200598 unsigned int i;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200599
Benjamin Tissoiresdcd90062013-03-05 17:09:00 +0100600 output_report_enum = &hdev->report_enum[HID_OUTPUT_REPORT];
601 report = output_report_enum->report_id_hash[REPORT_ID_DJ_SHORT];
602
603 if (!report) {
604 dev_err(&hdev->dev, "%s: unable to find dj report\n", __func__);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200605 return -ENODEV;
606 }
607
Kees Cook297502a2013-09-11 21:56:56 +0200608 for (i = 0; i < DJREPORT_SHORT_LENGTH - 1; i++)
Benjamin Tissoiresdcd90062013-03-05 17:09:00 +0100609 report->field[0]->value[i] = data[i];
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200610
Jiri Kosina83a44ac2013-03-09 10:58:13 +0100611 hid_hw_request(hdev, report, HID_REQ_SET_REPORT);
Benjamin Tissoiresdcd90062013-03-05 17:09:00 +0100612
613 return 0;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200614}
615
616static int logi_dj_recv_query_paired_devices(struct dj_receiver_dev *djrcv_dev)
617{
Marc Dionned8dc3492012-06-01 18:12:14 -0400618 struct dj_report *dj_report;
619 int retval;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200620
Nestor Lopez Casadoc63e0e32013-07-18 06:21:30 -0700621 /* no need to protect djrcv_dev->querying_devices */
622 if (djrcv_dev->querying_devices)
623 return 0;
624
Alan Cox8a55ade2012-09-04 15:10:08 +0100625 dj_report = kzalloc(sizeof(struct dj_report), GFP_KERNEL);
Marc Dionned8dc3492012-06-01 18:12:14 -0400626 if (!dj_report)
627 return -ENOMEM;
628 dj_report->report_id = REPORT_ID_DJ_SHORT;
629 dj_report->device_index = 0xFF;
630 dj_report->report_type = REPORT_TYPE_CMD_GET_PAIRED_DEVICES;
631 retval = logi_dj_recv_send_report(djrcv_dev, dj_report);
632 kfree(dj_report);
633 return retval;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200634}
635
Nestor Lopez Casadoc63e0e32013-07-18 06:21:30 -0700636
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200637static int logi_dj_recv_switch_to_dj_mode(struct dj_receiver_dev *djrcv_dev,
638 unsigned timeout)
639{
Benjamin Tissoires6a9ddc82014-09-30 13:18:31 -0400640 struct hid_device *hdev = djrcv_dev->hdev;
Marc Dionned8dc3492012-06-01 18:12:14 -0400641 struct dj_report *dj_report;
Benjamin Tissoires6a9ddc82014-09-30 13:18:31 -0400642 u8 *buf;
Marc Dionned8dc3492012-06-01 18:12:14 -0400643 int retval;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200644
Alan Cox8a55ade2012-09-04 15:10:08 +0100645 dj_report = kzalloc(sizeof(struct dj_report), GFP_KERNEL);
Marc Dionned8dc3492012-06-01 18:12:14 -0400646 if (!dj_report)
647 return -ENOMEM;
648 dj_report->report_id = REPORT_ID_DJ_SHORT;
649 dj_report->device_index = 0xFF;
650 dj_report->report_type = REPORT_TYPE_CMD_SWITCH;
651 dj_report->report_params[CMD_SWITCH_PARAM_DEVBITFIELD] = 0x3F;
652 dj_report->report_params[CMD_SWITCH_PARAM_TIMEOUT_SECONDS] = (u8)timeout;
653 retval = logi_dj_recv_send_report(djrcv_dev, dj_report);
Benjamin Tisssoires42c22db2014-01-08 17:18:45 -0500654
655 /*
656 * Ugly sleep to work around a USB 3.0 bug when the receiver is still
657 * processing the "switch-to-dj" command while we send an other command.
658 * 50 msec should gives enough time to the receiver to be ready.
659 */
660 msleep(50);
661
Benjamin Tissoires6a9ddc82014-09-30 13:18:31 -0400662 /*
663 * Magical bits to set up hidpp notifications when the dj devices
664 * are connected/disconnected.
665 *
666 * We can reuse dj_report because HIDPP_REPORT_SHORT_LENGTH is smaller
667 * than DJREPORT_SHORT_LENGTH.
668 */
669 buf = (u8 *)dj_report;
670
671 memset(buf, 0, HIDPP_REPORT_SHORT_LENGTH);
672
673 buf[0] = REPORT_ID_HIDPP_SHORT;
674 buf[1] = 0xFF;
675 buf[2] = 0x80;
676 buf[3] = 0x00;
677 buf[4] = 0x00;
678 buf[5] = 0x09;
679 buf[6] = 0x00;
680
681 hid_hw_raw_request(hdev, REPORT_ID_HIDPP_SHORT, buf,
682 HIDPP_REPORT_SHORT_LENGTH, HID_OUTPUT_REPORT,
683 HID_REQ_SET_REPORT);
684
685 kfree(dj_report);
Marc Dionned8dc3492012-06-01 18:12:14 -0400686 return retval;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200687}
688
689
690static int logi_dj_ll_open(struct hid_device *hid)
691{
692 dbg_hid("%s:%s\n", __func__, hid->phys);
693 return 0;
694
695}
696
697static void logi_dj_ll_close(struct hid_device *hid)
698{
699 dbg_hid("%s:%s\n", __func__, hid->phys);
700}
701
Benjamin Tissoires8dba3022017-03-27 16:59:21 +0200702/*
703 * Register 0xB5 is "pairing information". It is solely intended for the
704 * receiver, so do not overwrite the device index.
705 */
706static u8 unifying_pairing_query[] = {0x10, 0xff, 0x83, 0xb5};
707static u8 unifying_pairing_answer[] = {0x11, 0xff, 0x83, 0xb5};
Benjamin Tissoires33797822014-09-30 13:18:30 -0400708
Benjamin Tissoiresbd27e202014-02-10 12:58:53 -0500709static int logi_dj_ll_raw_request(struct hid_device *hid,
710 unsigned char reportnum, __u8 *buf,
711 size_t count, unsigned char report_type,
712 int reqtype)
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200713{
Benjamin Tissoires0e40d352014-02-05 16:33:17 -0500714 struct dj_device *djdev = hid->driver_data;
715 struct dj_receiver_dev *djrcv_dev = djdev->dj_receiver_dev;
716 u8 *out_buf;
717 int ret;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200718
Benjamin Tissoires925f0f32014-09-30 13:18:29 -0400719 if ((buf[0] == REPORT_ID_HIDPP_SHORT) ||
720 (buf[0] == REPORT_ID_HIDPP_LONG)) {
721 if (count < 2)
722 return -EINVAL;
723
Benjamin Tissoires33797822014-09-30 13:18:30 -0400724 /* special case where we should not overwrite
725 * the device_index */
Benjamin Tissoires8dba3022017-03-27 16:59:21 +0200726 if (count == 7 && !memcmp(buf, unifying_pairing_query,
727 sizeof(unifying_pairing_query)))
728 buf[4] = (buf[4] & 0xf0) | (djdev->device_index - 1);
Benjamin Tissoires33797822014-09-30 13:18:30 -0400729 else
730 buf[1] = djdev->device_index;
Benjamin Tissoires925f0f32014-09-30 13:18:29 -0400731 return hid_hw_raw_request(djrcv_dev->hdev, reportnum, buf,
732 count, report_type, reqtype);
733 }
734
Benjamin Tissoires0e40d352014-02-05 16:33:17 -0500735 if (buf[0] != REPORT_TYPE_LEDS)
736 return -EINVAL;
737
738 out_buf = kzalloc(DJREPORT_SHORT_LENGTH, GFP_ATOMIC);
739 if (!out_buf)
740 return -ENOMEM;
741
Jiri Kosina51217e62014-08-21 09:56:47 -0500742 if (count > DJREPORT_SHORT_LENGTH - 2)
Benjamin Tissoires0e40d352014-02-05 16:33:17 -0500743 count = DJREPORT_SHORT_LENGTH - 2;
744
745 out_buf[0] = REPORT_ID_DJ_SHORT;
746 out_buf[1] = djdev->device_index;
747 memcpy(out_buf + 2, buf, count);
748
Benjamin Tissoires0e40d352014-02-05 16:33:17 -0500749 ret = hid_hw_raw_request(djrcv_dev->hdev, out_buf[0], out_buf,
Benjamin Tissoiresbd27e202014-02-10 12:58:53 -0500750 DJREPORT_SHORT_LENGTH, report_type, reqtype);
Benjamin Tissoires0e40d352014-02-05 16:33:17 -0500751
752 kfree(out_buf);
753 return ret;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200754}
755
Dan Carpenter6d603322013-10-19 12:08:59 +0300756static void rdcat(char *rdesc, unsigned int *rsize, const char *data, unsigned int size)
Henrik Rydberg2a039bf2012-04-22 14:21:39 +0200757{
Dan Carpenter6d603322013-10-19 12:08:59 +0300758 memcpy(rdesc + *rsize, data, size);
Henrik Rydberg2a039bf2012-04-22 14:21:39 +0200759 *rsize += size;
760}
761
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200762static int logi_dj_ll_parse(struct hid_device *hid)
763{
764 struct dj_device *djdev = hid->driver_data;
Henrik Rydberg2a039bf2012-04-22 14:21:39 +0200765 unsigned int rsize = 0;
766 char *rdesc;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200767 int retval;
768
769 dbg_hid("%s\n", __func__);
770
771 djdev->hdev->version = 0x0111;
772 djdev->hdev->country = 0x00;
773
Henrik Rydberg2a039bf2012-04-22 14:21:39 +0200774 rdesc = kmalloc(MAX_RDESC_SIZE, GFP_KERNEL);
775 if (!rdesc)
776 return -ENOMEM;
777
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200778 if (djdev->reports_supported & STD_KEYBOARD) {
779 dbg_hid("%s: sending a kbd descriptor, reports_supported: %x\n",
780 __func__, djdev->reports_supported);
Dan Carpenter6d603322013-10-19 12:08:59 +0300781 rdcat(rdesc, &rsize, kbd_descriptor, sizeof(kbd_descriptor));
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200782 }
783
784 if (djdev->reports_supported & STD_MOUSE) {
785 dbg_hid("%s: sending a mouse descriptor, reports_supported: "
786 "%x\n", __func__, djdev->reports_supported);
Dan Carpenter6d603322013-10-19 12:08:59 +0300787 rdcat(rdesc, &rsize, mse_descriptor, sizeof(mse_descriptor));
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200788 }
789
790 if (djdev->reports_supported & MULTIMEDIA) {
791 dbg_hid("%s: sending a multimedia report descriptor: %x\n",
792 __func__, djdev->reports_supported);
Dan Carpenter6d603322013-10-19 12:08:59 +0300793 rdcat(rdesc, &rsize, consumer_descriptor, sizeof(consumer_descriptor));
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200794 }
795
796 if (djdev->reports_supported & POWER_KEYS) {
797 dbg_hid("%s: sending a power keys report descriptor: %x\n",
798 __func__, djdev->reports_supported);
Dan Carpenter6d603322013-10-19 12:08:59 +0300799 rdcat(rdesc, &rsize, syscontrol_descriptor, sizeof(syscontrol_descriptor));
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200800 }
801
802 if (djdev->reports_supported & MEDIA_CENTER) {
803 dbg_hid("%s: sending a media center report descriptor: %x\n",
804 __func__, djdev->reports_supported);
Dan Carpenter6d603322013-10-19 12:08:59 +0300805 rdcat(rdesc, &rsize, media_descriptor, sizeof(media_descriptor));
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200806 }
807
808 if (djdev->reports_supported & KBD_LEDS) {
809 dbg_hid("%s: need to send kbd leds report descriptor: %x\n",
810 __func__, djdev->reports_supported);
811 }
812
Benjamin Tissoires925f0f32014-09-30 13:18:29 -0400813 rdcat(rdesc, &rsize, hidpp_descriptor, sizeof(hidpp_descriptor));
814
Henrik Rydberg2a039bf2012-04-22 14:21:39 +0200815 retval = hid_parse_report(hid, rdesc, rsize);
816 kfree(rdesc);
817
818 return retval;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200819}
820
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200821static int logi_dj_ll_start(struct hid_device *hid)
822{
823 dbg_hid("%s\n", __func__);
824 return 0;
825}
826
827static void logi_dj_ll_stop(struct hid_device *hid)
828{
829 dbg_hid("%s\n", __func__);
830}
831
832
833static struct hid_ll_driver logi_dj_ll_driver = {
834 .parse = logi_dj_ll_parse,
835 .start = logi_dj_ll_start,
836 .stop = logi_dj_ll_stop,
837 .open = logi_dj_ll_open,
838 .close = logi_dj_ll_close,
Benjamin Tissoiresbd27e202014-02-10 12:58:53 -0500839 .raw_request = logi_dj_ll_raw_request,
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200840};
841
Benjamin Tissoires925f0f32014-09-30 13:18:29 -0400842static int logi_dj_dj_event(struct hid_device *hdev,
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200843 struct hid_report *report, u8 *data,
844 int size)
845{
846 struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
847 struct dj_report *dj_report = (struct dj_report *) data;
848 unsigned long flags;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200849
Benjamin Tissoires925f0f32014-09-30 13:18:29 -0400850 /*
851 * Here we receive all data coming from iface 2, there are 3 cases:
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200852 *
Benjamin Tissoires925f0f32014-09-30 13:18:29 -0400853 * 1) Data is intended for this driver i. e. data contains arrival,
854 * departure, etc notifications, in which case we queue them for delayed
855 * processing by the work queue. We return 1 to hid-core as no further
856 * processing is required from it.
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200857 *
Benjamin Tissoires925f0f32014-09-30 13:18:29 -0400858 * 2) Data informs a connection change, if the change means rf link
859 * loss, then we must send a null report to the upper layer to discard
860 * potentially pressed keys that may be repeated forever by the input
861 * layer. Return 1 to hid-core as no further processing is required.
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200862 *
Benjamin Tissoires925f0f32014-09-30 13:18:29 -0400863 * 3) Data is an actual input event from a paired DJ device in which
864 * case we forward it to the correct hid device (via hid_input_report()
865 * ) and return 1 so hid-core does not anything else with it.
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200866 */
Benjamin Tissoires5abfe852014-08-22 16:16:05 -0400867
Jiri Kosinaad3e14d2014-08-21 09:57:17 -0500868 if ((dj_report->device_index < DJ_DEVICE_INDEX_MIN) ||
869 (dj_report->device_index > DJ_DEVICE_INDEX_MAX)) {
Benjamin Tissoires5abfe852014-08-22 16:16:05 -0400870 /*
871 * Device index is wrong, bail out.
872 * This driver can ignore safely the receiver notifications,
873 * so ignore those reports too.
874 */
875 if (dj_report->device_index != DJ_RECEIVER_INDEX)
876 dev_err(&hdev->dev, "%s: invalid device index:%d\n",
Jiri Kosinaad3e14d2014-08-21 09:57:17 -0500877 __func__, dj_report->device_index);
878 return false;
879 }
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200880
881 spin_lock_irqsave(&djrcv_dev->lock, flags);
Benjamin Tissoires368d4e592014-08-22 16:16:06 -0400882
883 if (!djrcv_dev->paired_dj_devices[dj_report->device_index]) {
884 /* received an event for an unknown device, bail out */
885 logi_dj_recv_queue_notification(djrcv_dev, dj_report);
886 goto out;
887 }
888
Benjamin Tissoires5abfe852014-08-22 16:16:05 -0400889 switch (dj_report->report_type) {
890 case REPORT_TYPE_NOTIF_DEVICE_PAIRED:
Benjamin Tissoires368d4e592014-08-22 16:16:06 -0400891 /* pairing notifications are handled above the switch */
892 break;
Benjamin Tissoires5abfe852014-08-22 16:16:05 -0400893 case REPORT_TYPE_NOTIF_DEVICE_UNPAIRED:
894 logi_dj_recv_queue_notification(djrcv_dev, dj_report);
895 break;
896 case REPORT_TYPE_NOTIF_CONNECTION_STATUS:
897 if (dj_report->report_params[CONNECTION_STATUS_PARAM_STATUS] ==
898 STATUS_LINKLOSS) {
899 logi_dj_recv_forward_null_report(djrcv_dev, dj_report);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200900 }
Benjamin Tissoires5abfe852014-08-22 16:16:05 -0400901 break;
902 default:
Benjamin Tissoires83898232019-04-20 13:21:43 +0200903 logi_dj_recv_forward_dj(djrcv_dev, dj_report);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200904 }
Benjamin Tissoires368d4e592014-08-22 16:16:06 -0400905
906out:
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200907 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
908
Benjamin Tissoires5abfe852014-08-22 16:16:05 -0400909 return true;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200910}
911
Benjamin Tissoires925f0f32014-09-30 13:18:29 -0400912static int logi_dj_hidpp_event(struct hid_device *hdev,
913 struct hid_report *report, u8 *data,
914 int size)
915{
916 struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
Benjamin Tissoires7bb56a52019-04-20 13:21:44 +0200917 struct hidpp_event *hidpp_report = (struct hidpp_event *) data;
Benjamin Tissoires925f0f32014-09-30 13:18:29 -0400918 unsigned long flags;
Benjamin Tissoires7bb56a52019-04-20 13:21:44 +0200919 u8 device_index = hidpp_report->device_index;
Benjamin Tissoires925f0f32014-09-30 13:18:29 -0400920
Benjamin Tissoires33797822014-09-30 13:18:30 -0400921 if (device_index == HIDPP_RECEIVER_INDEX) {
922 /* special case were the device wants to know its unifying
923 * name */
924 if (size == HIDPP_REPORT_LONG_LENGTH &&
Benjamin Tissoires8dba3022017-03-27 16:59:21 +0200925 !memcmp(data, unifying_pairing_answer,
926 sizeof(unifying_pairing_answer)))
Benjamin Tissoires33797822014-09-30 13:18:30 -0400927 device_index = (data[4] & 0x0F) + 1;
928 else
929 return false;
930 }
Benjamin Tissoires925f0f32014-09-30 13:18:29 -0400931
932 /*
933 * Data is from the HID++ collection, in this case, we forward the
934 * data to the corresponding child dj device and return 0 to hid-core
935 * so he data also goes to the hidraw device of the receiver. This
936 * allows a user space application to implement the full HID++ routing
937 * via the receiver.
938 */
939
940 if ((device_index < DJ_DEVICE_INDEX_MIN) ||
941 (device_index > DJ_DEVICE_INDEX_MAX)) {
942 /*
943 * Device index is wrong, bail out.
944 * This driver can ignore safely the receiver notifications,
945 * so ignore those reports too.
946 */
947 dev_err(&hdev->dev, "%s: invalid device index:%d\n",
Benjamin Tissoires7bb56a52019-04-20 13:21:44 +0200948 __func__, hidpp_report->device_index);
Benjamin Tissoires925f0f32014-09-30 13:18:29 -0400949 return false;
950 }
951
952 spin_lock_irqsave(&djrcv_dev->lock, flags);
953
954 if (!djrcv_dev->paired_dj_devices[device_index])
955 /* received an event for an unknown device, bail out */
956 goto out;
957
Benjamin Tissoires83898232019-04-20 13:21:43 +0200958 logi_dj_recv_forward_report(djrcv_dev->paired_dj_devices[device_index],
959 data, size);
Benjamin Tissoires925f0f32014-09-30 13:18:29 -0400960
961out:
962 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
963
964 return false;
965}
966
967static int logi_dj_raw_event(struct hid_device *hdev,
968 struct hid_report *report, u8 *data,
969 int size)
970{
971 dbg_hid("%s, size:%d\n", __func__, size);
972
973 switch (data[0]) {
974 case REPORT_ID_DJ_SHORT:
Peter Wuf254ae92014-12-16 16:55:21 +0100975 if (size != DJREPORT_SHORT_LENGTH) {
976 dev_err(&hdev->dev, "DJ report of bad size (%d)", size);
977 return false;
978 }
Benjamin Tissoires925f0f32014-09-30 13:18:29 -0400979 return logi_dj_dj_event(hdev, report, data, size);
980 case REPORT_ID_HIDPP_SHORT:
Peter Wuf254ae92014-12-16 16:55:21 +0100981 if (size != HIDPP_REPORT_SHORT_LENGTH) {
982 dev_err(&hdev->dev,
983 "Short HID++ report of bad size (%d)", size);
984 return false;
985 }
986 return logi_dj_hidpp_event(hdev, report, data, size);
Benjamin Tissoires925f0f32014-09-30 13:18:29 -0400987 case REPORT_ID_HIDPP_LONG:
Peter Wuf254ae92014-12-16 16:55:21 +0100988 if (size != HIDPP_REPORT_LONG_LENGTH) {
989 dev_err(&hdev->dev,
990 "Long HID++ report of bad size (%d)", size);
991 return false;
992 }
Benjamin Tissoires925f0f32014-09-30 13:18:29 -0400993 return logi_dj_hidpp_event(hdev, report, data, size);
994 }
995
996 return false;
997}
998
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200999static int logi_dj_probe(struct hid_device *hdev,
1000 const struct hid_device_id *id)
1001{
1002 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
1003 struct dj_receiver_dev *djrcv_dev;
1004 int retval;
1005
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001006 dbg_hid("%s called for ifnum %d\n", __func__,
1007 intf->cur_altsetting->desc.bInterfaceNumber);
1008
1009 /* Ignore interfaces 0 and 1, they will not carry any data, dont create
1010 * any hid_device for them */
1011 if (intf->cur_altsetting->desc.bInterfaceNumber !=
1012 LOGITECH_DJ_INTERFACE_NUMBER) {
1013 dbg_hid("%s: ignoring ifnum %d\n", __func__,
1014 intf->cur_altsetting->desc.bInterfaceNumber);
1015 return -ENODEV;
1016 }
1017
1018 /* Treat interface 2 */
1019
1020 djrcv_dev = kzalloc(sizeof(struct dj_receiver_dev), GFP_KERNEL);
1021 if (!djrcv_dev) {
1022 dev_err(&hdev->dev,
1023 "%s:failed allocating dj_receiver_dev\n", __func__);
1024 return -ENOMEM;
1025 }
1026 djrcv_dev->hdev = hdev;
1027 INIT_WORK(&djrcv_dev->work, delayedwork_callback);
1028 spin_lock_init(&djrcv_dev->lock);
1029 if (kfifo_alloc(&djrcv_dev->notif_fifo,
1030 DJ_MAX_NUMBER_NOTIFICATIONS * sizeof(struct dj_report),
1031 GFP_KERNEL)) {
1032 dev_err(&hdev->dev,
1033 "%s:failed allocating notif_fifo\n", __func__);
1034 kfree(djrcv_dev);
1035 return -ENOMEM;
1036 }
1037 hid_set_drvdata(hdev, djrcv_dev);
1038
1039 /* Call to usbhid to fetch the HID descriptors of interface 2 and
1040 * subsequently call to the hid/hid-core to parse the fetched
1041 * descriptors, this will in turn create the hidraw and hiddev nodes
1042 * for interface 2 of the receiver */
1043 retval = hid_parse(hdev);
1044 if (retval) {
1045 dev_err(&hdev->dev,
1046 "%s:parse of interface 2 failed\n", __func__);
1047 goto hid_parse_fail;
1048 }
1049
Kees Cook297502a2013-09-11 21:56:56 +02001050 if (!hid_validate_values(hdev, HID_OUTPUT_REPORT, REPORT_ID_DJ_SHORT,
1051 0, DJREPORT_SHORT_LENGTH - 1)) {
1052 retval = -ENODEV;
1053 goto hid_parse_fail;
1054 }
1055
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001056 /* Starts the usb device and connects to upper interfaces hiddev and
1057 * hidraw */
1058 retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
1059 if (retval) {
1060 dev_err(&hdev->dev,
1061 "%s:hid_hw_start returned error\n", __func__);
1062 goto hid_hw_start_fail;
1063 }
1064
1065 retval = logi_dj_recv_switch_to_dj_mode(djrcv_dev, 0);
1066 if (retval < 0) {
1067 dev_err(&hdev->dev,
1068 "%s:logi_dj_recv_switch_to_dj_mode returned error:%d\n",
1069 __func__, retval);
1070 goto switch_to_dj_mode_fail;
1071 }
1072
1073 /* This is enabling the polling urb on the IN endpoint */
Benjamin Tissoiresddf75402013-07-12 11:01:02 +02001074 retval = hid_hw_open(hdev);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001075 if (retval < 0) {
Benjamin Tissoiresddf75402013-07-12 11:01:02 +02001076 dev_err(&hdev->dev, "%s:hid_hw_open returned error:%d\n",
1077 __func__, retval);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001078 goto llopen_failed;
1079 }
1080
Andrew de los Reyesa9dd22b72013-02-18 09:20:22 -08001081 /* Allow incoming packets to arrive: */
1082 hid_device_io_start(hdev);
1083
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001084 retval = logi_dj_recv_query_paired_devices(djrcv_dev);
1085 if (retval < 0) {
1086 dev_err(&hdev->dev, "%s:logi_dj_recv_query_paired_devices "
1087 "error:%d\n", __func__, retval);
1088 goto logi_dj_recv_query_paired_devices_failed;
1089 }
1090
1091 return retval;
1092
1093logi_dj_recv_query_paired_devices_failed:
Benjamin Tissoiresddf75402013-07-12 11:01:02 +02001094 hid_hw_close(hdev);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001095
1096llopen_failed:
1097switch_to_dj_mode_fail:
1098 hid_hw_stop(hdev);
1099
1100hid_hw_start_fail:
1101hid_parse_fail:
1102 kfifo_free(&djrcv_dev->notif_fifo);
1103 kfree(djrcv_dev);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001104 return retval;
1105
1106}
1107
1108#ifdef CONFIG_PM
1109static int logi_dj_reset_resume(struct hid_device *hdev)
1110{
1111 int retval;
1112 struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
1113
1114 retval = logi_dj_recv_switch_to_dj_mode(djrcv_dev, 0);
1115 if (retval < 0) {
1116 dev_err(&hdev->dev,
1117 "%s:logi_dj_recv_switch_to_dj_mode returned error:%d\n",
1118 __func__, retval);
1119 }
1120
1121 return 0;
1122}
1123#endif
1124
1125static void logi_dj_remove(struct hid_device *hdev)
1126{
1127 struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
1128 struct dj_device *dj_dev;
1129 int i;
1130
1131 dbg_hid("%s\n", __func__);
1132
1133 cancel_work_sync(&djrcv_dev->work);
1134
Benjamin Tissoiresddf75402013-07-12 11:01:02 +02001135 hid_hw_close(hdev);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001136 hid_hw_stop(hdev);
1137
1138 /* I suppose that at this point the only context that can access
1139 * the djrecv_data is this thread as the work item is guaranteed to
1140 * have finished and no more raw_event callbacks should arrive after
1141 * the remove callback was triggered so no locks are put around the
1142 * code below */
Nestor Lopez Casado844580f2011-09-20 15:59:03 +02001143 for (i = 0; i < (DJ_MAX_PAIRED_DEVICES + DJ_DEVICE_INDEX_MIN); i++) {
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001144 dj_dev = djrcv_dev->paired_dj_devices[i];
1145 if (dj_dev != NULL) {
1146 hid_destroy_device(dj_dev->hdev);
1147 kfree(dj_dev);
1148 djrcv_dev->paired_dj_devices[i] = NULL;
1149 }
1150 }
1151
1152 kfifo_free(&djrcv_dev->notif_fifo);
1153 kfree(djrcv_dev);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001154}
1155
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001156static const struct hid_device_id logi_dj_receivers[] = {
1157 {HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
1158 USB_DEVICE_ID_LOGITECH_UNIFYING_RECEIVER)},
1159 {HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
1160 USB_DEVICE_ID_LOGITECH_UNIFYING_RECEIVER_2)},
1161 {}
1162};
1163
1164MODULE_DEVICE_TABLE(hid, logi_dj_receivers);
1165
1166static struct hid_driver logi_djreceiver_driver = {
1167 .name = "logitech-djreceiver",
1168 .id_table = logi_dj_receivers,
1169 .probe = logi_dj_probe,
1170 .remove = logi_dj_remove,
1171 .raw_event = logi_dj_raw_event,
1172#ifdef CONFIG_PM
1173 .reset_resume = logi_dj_reset_resume,
1174#endif
1175};
1176
Benjamin Tissoiresab94e562014-09-30 13:18:28 -04001177module_hid_driver(logi_djreceiver_driver);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001178
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001179MODULE_LICENSE("GPL");
1180MODULE_AUTHOR("Logitech");
1181MODULE_AUTHOR("Nestor Lopez Casado");
1182MODULE_AUTHOR("nlopezcasad@logitech.com");