]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - drivers/hid/hid-logitech-dj.c
HID: logitech-dj: add support for 27 MHz mouse-only receivers
[mirror_ubuntu-jammy-kernel.git] / drivers / hid / hid-logitech-dj.c
CommitLineData
534a7b8e
NLC
1/*
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>
8cb3746a 28#include <linux/kfifo.h>
82c0beb8 29#include <linux/delay.h>
44d27f7d 30#include <asm/unaligned.h>
534a7b8e 31#include "hid-ids.h"
8cb3746a
BT
32
33#define DJ_MAX_PAIRED_DEVICES 6
4fcad95a 34#define DJ_MAX_NUMBER_NOTIFS 8
8cb3746a
BT
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
925f0f3e
BT
45#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
8cb3746a
BT
54#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
8cb3746a
BT
77/* 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 */
a17dd1f2
BT
96#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)
8cb3746a 102
74808f91
HG
103/* 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
c9121cf6 109#define HIDPP_PARAM_27MHZ_DEVID 0x03
74808f91
HG
110#define HIDPP_DEVICE_TYPE_MASK GENMASK(3, 0)
111#define HIDPP_LINK_STATUS_MASK BIT(6)
112
113#define HIDPP_SET_REGISTER 0x80
c0340412 114#define HIDPP_GET_LONG_REGISTER 0x83
74808f91 115#define HIDPP_REG_CONNECTION_STATE 0x02
c0340412
BT
116#define HIDPP_REG_PAIRING_INFORMATION 0xB5
117#define HIDPP_PAIRING_INFORMATION 0x20
74808f91
HG
118#define HIDPP_FAKE_DEVICE_ARRIVAL 0x02
119
120enum recvr_type {
121 recvr_type_dj,
122 recvr_type_hidpp,
f5fb57a7 123 recvr_type_gaming_hidpp,
c9121cf6 124 recvr_type_27mhz,
74808f91 125};
c0340412 126
8cb3746a
BT
127struct dj_report {
128 u8 report_id;
129 u8 device_index;
130 u8 report_type;
131 u8 report_params[DJREPORT_SHORT_LENGTH - 3];
132};
133
7bb56a5f
BT
134struct hidpp_event {
135 u8 report_id;
136 u8 device_index;
137 u8 sub_id;
138 u8 params[HIDPP_REPORT_LONG_LENGTH - 3U];
139} __packed;
140
8cb3746a 141struct dj_receiver_dev {
a1d97ccb
HG
142 struct hid_device *mouse;
143 struct hid_device *keyboard;
0ee75544 144 struct hid_device *hidpp;
8cb3746a
BT
145 struct dj_device *paired_dj_devices[DJ_MAX_PAIRED_DEVICES +
146 DJ_DEVICE_INDEX_MIN];
a1d97ccb
HG
147 struct list_head list;
148 struct kref kref;
8cb3746a
BT
149 struct work_struct work;
150 struct kfifo notif_fifo;
b6aeedde 151 unsigned long last_query; /* in jiffies */
a1d97ccb 152 bool ready;
74808f91
HG
153 enum recvr_type type;
154 unsigned int unnumbered_application;
8cb3746a 155 spinlock_t lock;
8cb3746a
BT
156};
157
158struct dj_device {
159 struct hid_device *hdev;
160 struct dj_receiver_dev *dj_receiver_dev;
161 u32 reports_supported;
162 u8 device_index;
163};
534a7b8e 164
4fcad95a
BT
165#define WORKITEM_TYPE_EMPTY 0
166#define WORKITEM_TYPE_PAIRED 1
167#define WORKITEM_TYPE_UNPAIRED 2
168#define WORKITEM_TYPE_UNKNOWN 255
169
170struct dj_workitem {
171 u8 type; /* WORKITEM_TYPE_* */
172 u8 device_index;
173 u8 quad_id_msb;
174 u8 quad_id_lsb;
175 u32 reports_supported;
176};
177
534a7b8e
NLC
178/* Keyboard descriptor (1) */
179static const char kbd_descriptor[] = {
180 0x05, 0x01, /* USAGE_PAGE (generic Desktop) */
181 0x09, 0x06, /* USAGE (Keyboard) */
182 0xA1, 0x01, /* COLLECTION (Application) */
183 0x85, 0x01, /* REPORT_ID (1) */
184 0x95, 0x08, /* REPORT_COUNT (8) */
185 0x75, 0x01, /* REPORT_SIZE (1) */
186 0x15, 0x00, /* LOGICAL_MINIMUM (0) */
187 0x25, 0x01, /* LOGICAL_MAXIMUM (1) */
188 0x05, 0x07, /* USAGE_PAGE (Keyboard) */
189 0x19, 0xE0, /* USAGE_MINIMUM (Left Control) */
190 0x29, 0xE7, /* USAGE_MAXIMUM (Right GUI) */
191 0x81, 0x02, /* INPUT (Data,Var,Abs) */
534a7b8e
NLC
192 0x95, 0x06, /* REPORT_COUNT (6) */
193 0x75, 0x08, /* REPORT_SIZE (8) */
194 0x15, 0x00, /* LOGICAL_MINIMUM (0) */
195 0x26, 0xFF, 0x00, /* LOGICAL_MAXIMUM (255) */
196 0x05, 0x07, /* USAGE_PAGE (Keyboard) */
197 0x19, 0x00, /* USAGE_MINIMUM (no event) */
198 0x2A, 0xFF, 0x00, /* USAGE_MAXIMUM (reserved) */
199 0x81, 0x00, /* INPUT (Data,Ary,Abs) */
0e40d356
BT
200 0x85, 0x0e, /* REPORT_ID (14) */
201 0x05, 0x08, /* USAGE PAGE (LED page) */
202 0x95, 0x05, /* REPORT COUNT (5) */
203 0x75, 0x01, /* REPORT SIZE (1) */
204 0x15, 0x00, /* LOGICAL_MINIMUM (0) */
205 0x25, 0x01, /* LOGICAL_MAXIMUM (1) */
206 0x19, 0x01, /* USAGE MINIMUM (1) */
207 0x29, 0x05, /* USAGE MAXIMUM (5) */
208 0x91, 0x02, /* OUTPUT (Data, Variable, Absolute) */
209 0x95, 0x01, /* REPORT COUNT (1) */
210 0x75, 0x03, /* REPORT SIZE (3) */
211 0x91, 0x01, /* OUTPUT (Constant) */
534a7b8e
NLC
212 0xC0
213};
214
215/* Mouse descriptor (2) */
216static const char mse_descriptor[] = {
217 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */
218 0x09, 0x02, /* USAGE (Mouse) */
219 0xA1, 0x01, /* COLLECTION (Application) */
220 0x85, 0x02, /* REPORT_ID = 2 */
221 0x09, 0x01, /* USAGE (pointer) */
222 0xA1, 0x00, /* COLLECTION (physical) */
223 0x05, 0x09, /* USAGE_PAGE (buttons) */
224 0x19, 0x01, /* USAGE_MIN (1) */
225 0x29, 0x10, /* USAGE_MAX (16) */
226 0x15, 0x00, /* LOGICAL_MIN (0) */
227 0x25, 0x01, /* LOGICAL_MAX (1) */
228 0x95, 0x10, /* REPORT_COUNT (16) */
229 0x75, 0x01, /* REPORT_SIZE (1) */
230 0x81, 0x02, /* INPUT (data var abs) */
231 0x05, 0x01, /* USAGE_PAGE (generic desktop) */
232 0x16, 0x01, 0xF8, /* LOGICAL_MIN (-2047) */
233 0x26, 0xFF, 0x07, /* LOGICAL_MAX (2047) */
234 0x75, 0x0C, /* REPORT_SIZE (12) */
235 0x95, 0x02, /* REPORT_COUNT (2) */
236 0x09, 0x30, /* USAGE (X) */
237 0x09, 0x31, /* USAGE (Y) */
238 0x81, 0x06, /* INPUT */
239 0x15, 0x81, /* LOGICAL_MIN (-127) */
240 0x25, 0x7F, /* LOGICAL_MAX (127) */
241 0x75, 0x08, /* REPORT_SIZE (8) */
242 0x95, 0x01, /* REPORT_COUNT (1) */
243 0x09, 0x38, /* USAGE (wheel) */
244 0x81, 0x06, /* INPUT */
245 0x05, 0x0C, /* USAGE_PAGE(consumer) */
246 0x0A, 0x38, 0x02, /* USAGE(AC Pan) */
247 0x95, 0x01, /* REPORT_COUNT (1) */
248 0x81, 0x06, /* INPUT */
249 0xC0, /* END_COLLECTION */
250 0xC0, /* END_COLLECTION */
251};
252
c9121cf6
HG
253/* Mouse descriptor (2) for 27 MHz receiver, only 8 buttons */
254static const char mse_27mhz_descriptor[] = {
255 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */
256 0x09, 0x02, /* USAGE (Mouse) */
257 0xA1, 0x01, /* COLLECTION (Application) */
258 0x85, 0x02, /* REPORT_ID = 2 */
259 0x09, 0x01, /* USAGE (pointer) */
260 0xA1, 0x00, /* COLLECTION (physical) */
261 0x05, 0x09, /* USAGE_PAGE (buttons) */
262 0x19, 0x01, /* USAGE_MIN (1) */
263 0x29, 0x08, /* USAGE_MAX (8) */
264 0x15, 0x00, /* LOGICAL_MIN (0) */
265 0x25, 0x01, /* LOGICAL_MAX (1) */
266 0x95, 0x08, /* REPORT_COUNT (8) */
267 0x75, 0x01, /* REPORT_SIZE (1) */
268 0x81, 0x02, /* INPUT (data var abs) */
269 0x05, 0x01, /* USAGE_PAGE (generic desktop) */
270 0x16, 0x01, 0xF8, /* LOGICAL_MIN (-2047) */
271 0x26, 0xFF, 0x07, /* LOGICAL_MAX (2047) */
272 0x75, 0x0C, /* REPORT_SIZE (12) */
273 0x95, 0x02, /* REPORT_COUNT (2) */
274 0x09, 0x30, /* USAGE (X) */
275 0x09, 0x31, /* USAGE (Y) */
276 0x81, 0x06, /* INPUT */
277 0x15, 0x81, /* LOGICAL_MIN (-127) */
278 0x25, 0x7F, /* LOGICAL_MAX (127) */
279 0x75, 0x08, /* REPORT_SIZE (8) */
280 0x95, 0x01, /* REPORT_COUNT (1) */
281 0x09, 0x38, /* USAGE (wheel) */
282 0x81, 0x06, /* INPUT */
283 0x05, 0x0C, /* USAGE_PAGE(consumer) */
284 0x0A, 0x38, 0x02, /* USAGE(AC Pan) */
285 0x95, 0x01, /* REPORT_COUNT (1) */
286 0x81, 0x06, /* INPUT */
287 0xC0, /* END_COLLECTION */
288 0xC0, /* END_COLLECTION */
289};
290
f5fb57a7
BT
291/* Gaming Mouse descriptor (2) */
292static const char mse_high_res_descriptor[] = {
293 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */
294 0x09, 0x02, /* USAGE (Mouse) */
295 0xA1, 0x01, /* COLLECTION (Application) */
296 0x85, 0x02, /* REPORT_ID = 2 */
297 0x09, 0x01, /* USAGE (pointer) */
298 0xA1, 0x00, /* COLLECTION (physical) */
299 0x05, 0x09, /* USAGE_PAGE (buttons) */
300 0x19, 0x01, /* USAGE_MIN (1) */
301 0x29, 0x10, /* USAGE_MAX (16) */
302 0x15, 0x00, /* LOGICAL_MIN (0) */
303 0x25, 0x01, /* LOGICAL_MAX (1) */
304 0x95, 0x10, /* REPORT_COUNT (16) */
305 0x75, 0x01, /* REPORT_SIZE (1) */
306 0x81, 0x02, /* INPUT (data var abs) */
307 0x05, 0x01, /* USAGE_PAGE (generic desktop) */
308 0x16, 0x01, 0x80, /* LOGICAL_MIN (-32767) */
309 0x26, 0xFF, 0x7F, /* LOGICAL_MAX (32767) */
310 0x75, 0x10, /* REPORT_SIZE (16) */
311 0x95, 0x02, /* REPORT_COUNT (2) */
312 0x09, 0x30, /* USAGE (X) */
313 0x09, 0x31, /* USAGE (Y) */
314 0x81, 0x06, /* INPUT */
315 0x15, 0x81, /* LOGICAL_MIN (-127) */
316 0x25, 0x7F, /* LOGICAL_MAX (127) */
317 0x75, 0x08, /* REPORT_SIZE (8) */
318 0x95, 0x01, /* REPORT_COUNT (1) */
319 0x09, 0x38, /* USAGE (wheel) */
320 0x81, 0x06, /* INPUT */
321 0x05, 0x0C, /* USAGE_PAGE(consumer) */
322 0x0A, 0x38, 0x02, /* USAGE(AC Pan) */
323 0x95, 0x01, /* REPORT_COUNT (1) */
324 0x81, 0x06, /* INPUT */
325 0xC0, /* END_COLLECTION */
326 0xC0, /* END_COLLECTION */
327};
328
534a7b8e
NLC
329/* Consumer Control descriptor (3) */
330static const char consumer_descriptor[] = {
331 0x05, 0x0C, /* USAGE_PAGE (Consumer Devices) */
332 0x09, 0x01, /* USAGE (Consumer Control) */
333 0xA1, 0x01, /* COLLECTION (Application) */
334 0x85, 0x03, /* REPORT_ID = 3 */
335 0x75, 0x10, /* REPORT_SIZE (16) */
336 0x95, 0x02, /* REPORT_COUNT (2) */
337 0x15, 0x01, /* LOGICAL_MIN (1) */
338 0x26, 0x8C, 0x02, /* LOGICAL_MAX (652) */
339 0x19, 0x01, /* USAGE_MIN (1) */
340 0x2A, 0x8C, 0x02, /* USAGE_MAX (652) */
341 0x81, 0x00, /* INPUT (Data Ary Abs) */
342 0xC0, /* END_COLLECTION */
343}; /* */
344
345/* System control descriptor (4) */
346static const char syscontrol_descriptor[] = {
347 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */
348 0x09, 0x80, /* USAGE (System Control) */
349 0xA1, 0x01, /* COLLECTION (Application) */
350 0x85, 0x04, /* REPORT_ID = 4 */
351 0x75, 0x02, /* REPORT_SIZE (2) */
352 0x95, 0x01, /* REPORT_COUNT (1) */
353 0x15, 0x01, /* LOGICAL_MIN (1) */
354 0x25, 0x03, /* LOGICAL_MAX (3) */
355 0x09, 0x82, /* USAGE (System Sleep) */
356 0x09, 0x81, /* USAGE (System Power Down) */
357 0x09, 0x83, /* USAGE (System Wake Up) */
358 0x81, 0x60, /* INPUT (Data Ary Abs NPrf Null) */
359 0x75, 0x06, /* REPORT_SIZE (6) */
360 0x81, 0x03, /* INPUT (Cnst Var Abs) */
361 0xC0, /* END_COLLECTION */
362};
363
364/* Media descriptor (8) */
365static const char media_descriptor[] = {
366 0x06, 0xbc, 0xff, /* Usage Page 0xffbc */
367 0x09, 0x88, /* Usage 0x0088 */
368 0xa1, 0x01, /* BeginCollection */
369 0x85, 0x08, /* Report ID 8 */
370 0x19, 0x01, /* Usage Min 0x0001 */
371 0x29, 0xff, /* Usage Max 0x00ff */
372 0x15, 0x01, /* Logical Min 1 */
373 0x26, 0xff, 0x00, /* Logical Max 255 */
374 0x75, 0x08, /* Report Size 8 */
375 0x95, 0x01, /* Report Count 1 */
376 0x81, 0x00, /* Input */
377 0xc0, /* EndCollection */
378}; /* */
379
925f0f3e
BT
380/* HIDPP descriptor */
381static const char hidpp_descriptor[] = {
382 0x06, 0x00, 0xff, /* Usage Page (Vendor Defined Page 1) */
383 0x09, 0x01, /* Usage (Vendor Usage 1) */
384 0xa1, 0x01, /* Collection (Application) */
385 0x85, 0x10, /* Report ID (16) */
386 0x75, 0x08, /* Report Size (8) */
387 0x95, 0x06, /* Report Count (6) */
388 0x15, 0x00, /* Logical Minimum (0) */
389 0x26, 0xff, 0x00, /* Logical Maximum (255) */
390 0x09, 0x01, /* Usage (Vendor Usage 1) */
391 0x81, 0x00, /* Input (Data,Arr,Abs) */
392 0x09, 0x01, /* Usage (Vendor Usage 1) */
393 0x91, 0x00, /* Output (Data,Arr,Abs) */
394 0xc0, /* End Collection */
395 0x06, 0x00, 0xff, /* Usage Page (Vendor Defined Page 1) */
396 0x09, 0x02, /* Usage (Vendor Usage 2) */
397 0xa1, 0x01, /* Collection (Application) */
398 0x85, 0x11, /* Report ID (17) */
399 0x75, 0x08, /* Report Size (8) */
400 0x95, 0x13, /* Report Count (19) */
401 0x15, 0x00, /* Logical Minimum (0) */
402 0x26, 0xff, 0x00, /* Logical Maximum (255) */
403 0x09, 0x02, /* Usage (Vendor Usage 2) */
404 0x81, 0x00, /* Input (Data,Arr,Abs) */
405 0x09, 0x02, /* Usage (Vendor Usage 2) */
406 0x91, 0x00, /* Output (Data,Arr,Abs) */
407 0xc0, /* End Collection */
408 0x06, 0x00, 0xff, /* Usage Page (Vendor Defined Page 1) */
409 0x09, 0x04, /* Usage (Vendor Usage 0x04) */
410 0xa1, 0x01, /* Collection (Application) */
411 0x85, 0x20, /* Report ID (32) */
412 0x75, 0x08, /* Report Size (8) */
413 0x95, 0x0e, /* Report Count (14) */
414 0x15, 0x00, /* Logical Minimum (0) */
415 0x26, 0xff, 0x00, /* Logical Maximum (255) */
416 0x09, 0x41, /* Usage (Vendor Usage 0x41) */
417 0x81, 0x00, /* Input (Data,Arr,Abs) */
418 0x09, 0x41, /* Usage (Vendor Usage 0x41) */
419 0x91, 0x00, /* Output (Data,Arr,Abs) */
420 0x85, 0x21, /* Report ID (33) */
421 0x95, 0x1f, /* Report Count (31) */
422 0x15, 0x00, /* Logical Minimum (0) */
423 0x26, 0xff, 0x00, /* Logical Maximum (255) */
424 0x09, 0x42, /* Usage (Vendor Usage 0x42) */
425 0x81, 0x00, /* Input (Data,Arr,Abs) */
426 0x09, 0x42, /* Usage (Vendor Usage 0x42) */
427 0x91, 0x00, /* Output (Data,Arr,Abs) */
428 0xc0, /* End Collection */
429};
430
534a7b8e
NLC
431/* Maximum size of all defined hid reports in bytes (including report id) */
432#define MAX_REPORT_SIZE 8
433
2a039bf5
HR
434/* Make sure all descriptors are present here */
435#define MAX_RDESC_SIZE \
436 (sizeof(kbd_descriptor) + \
437 sizeof(mse_descriptor) + \
438 sizeof(consumer_descriptor) + \
439 sizeof(syscontrol_descriptor) + \
925f0f3e
BT
440 sizeof(media_descriptor) + \
441 sizeof(hidpp_descriptor))
2a039bf5 442
534a7b8e
NLC
443/* Number of possible hid report types that can be created by this driver.
444 *
445 * Right now, RF report types have the same report types (or report id's)
446 * than the hid report created from those RF reports. In the future
447 * this doesnt have to be true.
448 *
449 * For instance, RF report type 0x01 which has a size of 8 bytes, corresponds
450 * to hid report id 0x01, this is standard keyboard. Same thing applies to mice
451 * reports and consumer control, etc. If a new RF report is created, it doesn't
452 * has to have the same report id as its corresponding hid report, so an
453 * translation may have to take place for future report types.
454 */
455#define NUMBER_OF_HID_REPORTS 32
456static const u8 hid_reportid_size_map[NUMBER_OF_HID_REPORTS] = {
457 [1] = 8, /* Standard keyboard */
458 [2] = 8, /* Standard mouse */
459 [3] = 5, /* Consumer control */
460 [4] = 2, /* System control */
461 [8] = 2, /* Media Center */
462};
463
464
465#define LOGITECH_DJ_INTERFACE_NUMBER 0x02
466
534a7b8e
NLC
467static struct hid_ll_driver logi_dj_ll_driver;
468
c63e0e37 469static int logi_dj_recv_query_paired_devices(struct dj_receiver_dev *djrcv_dev);
a1d97ccb
HG
470static void delayedwork_callback(struct work_struct *work);
471
472static LIST_HEAD(dj_hdev_list);
473static DEFINE_MUTEX(dj_hdev_list_lock);
474
475/*
476 * dj/HID++ receivers are really a single logical entity, but for BIOS/Windows
477 * compatibility they have multiple USB interfaces. On HID++ receivers we need
478 * to listen for input reports on both interfaces. The functions below are used
479 * to create a single struct dj_receiver_dev for all interfaces belonging to
480 * a single USB-device / receiver.
481 */
482static struct dj_receiver_dev *dj_find_receiver_dev(struct hid_device *hdev)
483{
484 struct dj_receiver_dev *djrcv_dev;
485
486 /* Try to find an already-probed interface from the same device */
487 list_for_each_entry(djrcv_dev, &dj_hdev_list, list) {
488 if (djrcv_dev->mouse &&
489 hid_compare_device_paths(hdev, djrcv_dev->mouse, '/')) {
490 kref_get(&djrcv_dev->kref);
491 return djrcv_dev;
492 }
493 if (djrcv_dev->keyboard &&
494 hid_compare_device_paths(hdev, djrcv_dev->keyboard, '/')) {
495 kref_get(&djrcv_dev->kref);
496 return djrcv_dev;
497 }
498 if (djrcv_dev->hidpp &&
499 hid_compare_device_paths(hdev, djrcv_dev->hidpp, '/')) {
500 kref_get(&djrcv_dev->kref);
501 return djrcv_dev;
502 }
503 }
504
505 return NULL;
506}
507
508static void dj_release_receiver_dev(struct kref *kref)
509{
510 struct dj_receiver_dev *djrcv_dev = container_of(kref, struct dj_receiver_dev, kref);
511
512 list_del(&djrcv_dev->list);
513 kfifo_free(&djrcv_dev->notif_fifo);
514 kfree(djrcv_dev);
515}
516
517static void dj_put_receiver_dev(struct hid_device *hdev)
518{
519 struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
520
521 mutex_lock(&dj_hdev_list_lock);
522
523 if (djrcv_dev->mouse == hdev)
524 djrcv_dev->mouse = NULL;
525 if (djrcv_dev->keyboard == hdev)
526 djrcv_dev->keyboard = NULL;
527 if (djrcv_dev->hidpp == hdev)
528 djrcv_dev->hidpp = NULL;
529
530 kref_put(&djrcv_dev->kref, dj_release_receiver_dev);
531
532 mutex_unlock(&dj_hdev_list_lock);
533}
534
535static struct dj_receiver_dev *dj_get_receiver_dev(struct hid_device *hdev,
74808f91 536 enum recvr_type type,
a1d97ccb
HG
537 unsigned int application,
538 bool is_hidpp)
539{
540 struct dj_receiver_dev *djrcv_dev;
541
542 mutex_lock(&dj_hdev_list_lock);
543
544 djrcv_dev = dj_find_receiver_dev(hdev);
545 if (!djrcv_dev) {
546 djrcv_dev = kzalloc(sizeof(*djrcv_dev), GFP_KERNEL);
547 if (!djrcv_dev)
548 goto out;
549
550 INIT_WORK(&djrcv_dev->work, delayedwork_callback);
551 spin_lock_init(&djrcv_dev->lock);
552 if (kfifo_alloc(&djrcv_dev->notif_fifo,
553 DJ_MAX_NUMBER_NOTIFS * sizeof(struct dj_workitem),
554 GFP_KERNEL)) {
555 kfree(djrcv_dev);
556 djrcv_dev = NULL;
557 goto out;
558 }
559 kref_init(&djrcv_dev->kref);
560 list_add_tail(&djrcv_dev->list, &dj_hdev_list);
b6aeedde 561 djrcv_dev->last_query = jiffies;
74808f91 562 djrcv_dev->type = type;
a1d97ccb
HG
563 }
564
565 if (application == HID_GD_KEYBOARD)
566 djrcv_dev->keyboard = hdev;
567 if (application == HID_GD_MOUSE)
568 djrcv_dev->mouse = hdev;
569 if (is_hidpp)
570 djrcv_dev->hidpp = hdev;
571
572 hid_set_drvdata(hdev, djrcv_dev);
573out:
574 mutex_unlock(&dj_hdev_list_lock);
575 return djrcv_dev;
576}
534a7b8e
NLC
577
578static void logi_dj_recv_destroy_djhid_device(struct dj_receiver_dev *djrcv_dev,
4fcad95a 579 struct dj_workitem *workitem)
534a7b8e
NLC
580{
581 /* Called in delayed work context */
582 struct dj_device *dj_dev;
583 unsigned long flags;
584
585 spin_lock_irqsave(&djrcv_dev->lock, flags);
4fcad95a
BT
586 dj_dev = djrcv_dev->paired_dj_devices[workitem->device_index];
587 djrcv_dev->paired_dj_devices[workitem->device_index] = NULL;
534a7b8e
NLC
588 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
589
590 if (dj_dev != NULL) {
591 hid_destroy_device(dj_dev->hdev);
592 kfree(dj_dev);
593 } else {
0ee75544 594 hid_err(djrcv_dev->hidpp, "%s: can't destroy a NULL device\n",
534a7b8e
NLC
595 __func__);
596 }
597}
598
599static void logi_dj_recv_add_djhid_device(struct dj_receiver_dev *djrcv_dev,
4fcad95a 600 struct dj_workitem *workitem)
534a7b8e
NLC
601{
602 /* Called in delayed work context */
0ee75544 603 struct hid_device *djrcv_hdev = djrcv_dev->hidpp;
534a7b8e
NLC
604 struct hid_device *dj_hiddev;
605 struct dj_device *dj_dev;
4fcad95a 606 u8 device_index = workitem->device_index;
f41d766c 607 unsigned long flags;
534a7b8e
NLC
608
609 /* Device index goes from 1 to 6, we need 3 bytes to store the
610 * semicolon, the index, and a null terminator
611 */
612 unsigned char tmpstr[3];
613
f41d766c 614 /* We are the only one ever adding a device, no need to lock */
4fcad95a 615 if (djrcv_dev->paired_dj_devices[device_index]) {
c63e0e37
NLC
616 /* The device is already known. No need to reallocate it. */
617 dbg_hid("%s: device is already known\n", __func__);
618 return;
619 }
620
534a7b8e
NLC
621 dj_hiddev = hid_allocate_device();
622 if (IS_ERR(dj_hiddev)) {
623 dev_err(&djrcv_hdev->dev, "%s: hid_allocate_device failed\n",
624 __func__);
625 return;
626 }
627
628 dj_hiddev->ll_driver = &logi_dj_ll_driver;
534a7b8e
NLC
629
630 dj_hiddev->dev.parent = &djrcv_hdev->dev;
631 dj_hiddev->bus = BUS_USB;
82c0beb8 632 dj_hiddev->vendor = djrcv_hdev->vendor;
4fcad95a
BT
633 dj_hiddev->product = (workitem->quad_id_msb << 8) |
634 workitem->quad_id_lsb;
534a7b8e 635 snprintf(dj_hiddev->name, sizeof(dj_hiddev->name),
d610274b
BT
636 "Logitech Unifying Device. Wireless PID:%04x",
637 dj_hiddev->product);
638
c9121cf6
HG
639 if (djrcv_dev->type == recvr_type_27mhz)
640 dj_hiddev->group = HID_GROUP_LOGITECH_27MHZ_DEVICE;
641 else
642 dj_hiddev->group = HID_GROUP_LOGITECH_DJ_DEVICE;
534a7b8e 643
82c0beb8 644 memcpy(dj_hiddev->phys, djrcv_hdev->phys, sizeof(djrcv_hdev->phys));
4fcad95a 645 snprintf(tmpstr, sizeof(tmpstr), ":%d", device_index);
534a7b8e
NLC
646 strlcat(dj_hiddev->phys, tmpstr, sizeof(dj_hiddev->phys));
647
648 dj_dev = kzalloc(sizeof(struct dj_device), GFP_KERNEL);
649
650 if (!dj_dev) {
651 dev_err(&djrcv_hdev->dev, "%s: failed allocating dj_device\n",
652 __func__);
653 goto dj_device_allocate_fail;
654 }
655
4fcad95a 656 dj_dev->reports_supported = workitem->reports_supported;
534a7b8e
NLC
657 dj_dev->hdev = dj_hiddev;
658 dj_dev->dj_receiver_dev = djrcv_dev;
4fcad95a 659 dj_dev->device_index = device_index;
534a7b8e
NLC
660 dj_hiddev->driver_data = dj_dev;
661
f41d766c 662 spin_lock_irqsave(&djrcv_dev->lock, flags);
4fcad95a 663 djrcv_dev->paired_dj_devices[device_index] = dj_dev;
f41d766c 664 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
534a7b8e
NLC
665
666 if (hid_add_device(dj_hiddev)) {
667 dev_err(&djrcv_hdev->dev, "%s: failed adding dj_device\n",
668 __func__);
669 goto hid_add_device_fail;
670 }
671
672 return;
673
674hid_add_device_fail:
f41d766c 675 spin_lock_irqsave(&djrcv_dev->lock, flags);
4fcad95a 676 djrcv_dev->paired_dj_devices[device_index] = NULL;
f41d766c 677 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
534a7b8e
NLC
678 kfree(dj_dev);
679dj_device_allocate_fail:
680 hid_destroy_device(dj_hiddev);
681}
682
683static void delayedwork_callback(struct work_struct *work)
684{
685 struct dj_receiver_dev *djrcv_dev =
686 container_of(work, struct dj_receiver_dev, work);
687
4fcad95a 688 struct dj_workitem workitem;
534a7b8e
NLC
689 unsigned long flags;
690 int count;
c63e0e37 691 int retval;
534a7b8e
NLC
692
693 dbg_hid("%s\n", __func__);
694
695 spin_lock_irqsave(&djrcv_dev->lock, flags);
696
a1d97ccb
HG
697 /*
698 * Since we attach to multiple interfaces, we may get scheduled before
699 * we are bound to the HID++ interface, catch this.
700 */
701 if (!djrcv_dev->ready) {
702 pr_warn("%s: delayedwork queued before hidpp interface was enumerated\n",
703 __func__);
704 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
705 return;
706 }
707
4fcad95a 708 count = kfifo_out(&djrcv_dev->notif_fifo, &workitem, sizeof(workitem));
534a7b8e 709
4fcad95a 710 if (count != sizeof(workitem)) {
0ee75544 711 hid_err(djrcv_dev->hidpp, "delayedwork queued without workitems available\n");
534a7b8e
NLC
712 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
713 return;
714 }
715
716 if (!kfifo_is_empty(&djrcv_dev->notif_fifo)) {
717 if (schedule_work(&djrcv_dev->work) == 0) {
718 dbg_hid("%s: did not schedule the work item, was "
719 "already queued\n", __func__);
720 }
721 }
722
723 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
724
4fcad95a
BT
725 switch (workitem.type) {
726 case WORKITEM_TYPE_PAIRED:
727 logi_dj_recv_add_djhid_device(djrcv_dev, &workitem);
534a7b8e 728 break;
4fcad95a
BT
729 case WORKITEM_TYPE_UNPAIRED:
730 logi_dj_recv_destroy_djhid_device(djrcv_dev, &workitem);
731 break;
732 case WORKITEM_TYPE_UNKNOWN:
733 retval = logi_dj_recv_query_paired_devices(djrcv_dev);
734 if (retval) {
0ee75544 735 hid_err(djrcv_dev->hidpp, "%s: logi_dj_recv_query_paired_devices error: %d\n",
4fcad95a
BT
736 __func__, retval);
737 }
738 break;
739 case WORKITEM_TYPE_EMPTY:
740 dbg_hid("%s: device list is empty\n", __func__);
741 break;
742 }
743}
744
b6aeedde
HG
745/*
746 * Sometimes we receive reports for which we do not have a paired dj_device
747 * associated with the device_index or report-type to forward the report to.
748 * This means that the original "device paired" notification corresponding
749 * to the dj_device never arrived to this driver. Possible reasons for this are:
750 * 1) hid-core discards all packets coming from a device during probe().
751 * 2) if the receiver is plugged into a KVM switch then the pairing reports
752 * are only forwarded to it if the focus is on this PC.
753 * This function deals with this by re-asking the receiver for the list of
754 * connected devices in the delayed work callback.
755 * This function MUST be called with djrcv->lock held.
756 */
757static void logi_dj_recv_queue_unknown_work(struct dj_receiver_dev *djrcv_dev)
758{
759 struct dj_workitem workitem = { .type = WORKITEM_TYPE_UNKNOWN };
760
761 /* Rate limit queries done because of unhandeled reports to 2/sec */
762 if (time_before(jiffies, djrcv_dev->last_query + HZ / 2))
763 return;
764
765 kfifo_in(&djrcv_dev->notif_fifo, &workitem, sizeof(workitem));
766 schedule_work(&djrcv_dev->work);
767}
768
4fcad95a
BT
769static void logi_dj_recv_queue_notification(struct dj_receiver_dev *djrcv_dev,
770 struct dj_report *dj_report)
771{
772 /* We are called from atomic context (tasklet && djrcv->lock held) */
773 struct dj_workitem workitem = {
774 .device_index = dj_report->device_index,
775 };
776
777 switch (dj_report->report_type) {
778 case REPORT_TYPE_NOTIF_DEVICE_PAIRED:
779 workitem.type = WORKITEM_TYPE_PAIRED;
780 if (dj_report->report_params[DEVICE_PAIRED_PARAM_SPFUNCTION] &
781 SPFUNCTION_DEVICE_LIST_EMPTY) {
782 workitem.type = WORKITEM_TYPE_EMPTY;
783 break;
784 }
785 /* fall-through */
534a7b8e 786 case REPORT_TYPE_NOTIF_DEVICE_UNPAIRED:
4fcad95a
BT
787 workitem.quad_id_msb =
788 dj_report->report_params[DEVICE_PAIRED_PARAM_EQUAD_ID_MSB];
789 workitem.quad_id_lsb =
790 dj_report->report_params[DEVICE_PAIRED_PARAM_EQUAD_ID_LSB];
791 workitem.reports_supported = get_unaligned_le32(
792 dj_report->report_params +
793 DEVICE_PAIRED_RF_REPORT_TYPE);
794 if (dj_report->report_type == REPORT_TYPE_NOTIF_DEVICE_UNPAIRED)
795 workitem.type = WORKITEM_TYPE_UNPAIRED;
534a7b8e
NLC
796 break;
797 default:
b6aeedde
HG
798 logi_dj_recv_queue_unknown_work(djrcv_dev);
799 return;
534a7b8e 800 }
534a7b8e 801
4fcad95a 802 kfifo_in(&djrcv_dev->notif_fifo, &workitem, sizeof(workitem));
534a7b8e
NLC
803
804 if (schedule_work(&djrcv_dev->work) == 0) {
805 dbg_hid("%s: did not schedule the work item, was already "
806 "queued\n", __func__);
807 }
808}
809
74808f91
HG
810static void logi_hidpp_dev_conn_notif_equad(struct hidpp_event *hidpp_report,
811 struct dj_workitem *workitem)
812{
813 workitem->type = WORKITEM_TYPE_PAIRED;
814 workitem->quad_id_msb = hidpp_report->params[HIDPP_PARAM_EQUAD_MSB];
815 workitem->quad_id_lsb = hidpp_report->params[HIDPP_PARAM_EQUAD_LSB];
816 switch (hidpp_report->params[HIDPP_PARAM_DEVICE_INFO] &
817 HIDPP_DEVICE_TYPE_MASK) {
818 case REPORT_TYPE_KEYBOARD:
819 workitem->reports_supported |= STD_KEYBOARD | MULTIMEDIA |
820 POWER_KEYS | MEDIA_CENTER;
821 break;
822 case REPORT_TYPE_MOUSE:
823 workitem->reports_supported |= STD_MOUSE;
824 break;
825 }
826}
827
c9121cf6
HG
828static void logi_hidpp_dev_conn_notif_27mhz(struct hid_device *hdev,
829 struct hidpp_event *hidpp_report,
830 struct dj_workitem *workitem)
831{
832 workitem->type = WORKITEM_TYPE_PAIRED;
833 workitem->quad_id_lsb = hidpp_report->params[HIDPP_PARAM_27MHZ_DEVID];
834 switch (hidpp_report->device_index) {
835 case 1: /* Index 1 is always a mouse */
836 case 2: /* Index 2 is always a mouse */
837 workitem->reports_supported |= STD_MOUSE;
838 break;
839 case 3: /* Index 3 is always the keyboard */
840 case 4: /* Index 4 is used for an optional separate numpad */
841 workitem->reports_supported |= STD_KEYBOARD | MULTIMEDIA |
842 POWER_KEYS;
843 break;
844 default:
845 hid_warn(hdev, "%s: unexpected device-index %d", __func__,
846 hidpp_report->device_index);
847 }
848}
849
74808f91
HG
850static void logi_hidpp_recv_queue_notif(struct hid_device *hdev,
851 struct hidpp_event *hidpp_report)
852{
853 /* We are called from atomic context (tasklet && djrcv->lock held) */
854 struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
855 const char *device_type = "UNKNOWN";
856 struct dj_workitem workitem = {
857 .type = WORKITEM_TYPE_EMPTY,
858 .device_index = hidpp_report->device_index,
859 };
860
861 switch (hidpp_report->params[HIDPP_PARAM_PROTO_TYPE]) {
862 case 0x01:
863 device_type = "Bluetooth";
864 break;
865 case 0x02:
866 device_type = "27 Mhz";
c9121cf6 867 logi_hidpp_dev_conn_notif_27mhz(hdev, hidpp_report, &workitem);
74808f91
HG
868 break;
869 case 0x03:
870 device_type = "QUAD or eQUAD";
871 logi_hidpp_dev_conn_notif_equad(hidpp_report, &workitem);
872 break;
873 case 0x04:
874 device_type = "eQUAD step 4 DJ";
875 logi_hidpp_dev_conn_notif_equad(hidpp_report, &workitem);
876 break;
877 case 0x05:
878 device_type = "DFU Lite";
879 break;
880 case 0x06:
881 device_type = "eQUAD step 4 Lite";
882 logi_hidpp_dev_conn_notif_equad(hidpp_report, &workitem);
883 break;
884 case 0x07:
885 device_type = "eQUAD step 4 Gaming";
886 break;
887 case 0x08:
888 device_type = "eQUAD step 4 for gamepads";
889 break;
890 case 0x0a:
891 device_type = "eQUAD nano Lite";
892 logi_hidpp_dev_conn_notif_equad(hidpp_report, &workitem);
893 break;
894 case 0x0c:
895 device_type = "eQUAD Lightspeed";
f5fb57a7
BT
896 logi_hidpp_dev_conn_notif_equad(hidpp_report, &workitem);
897 workitem.reports_supported |= STD_KEYBOARD;
74808f91
HG
898 break;
899 }
900
901 if (workitem.type == WORKITEM_TYPE_EMPTY) {
902 hid_warn(hdev,
903 "unusable device of type %s (0x%02x) connected on slot %d",
904 device_type,
905 hidpp_report->params[HIDPP_PARAM_PROTO_TYPE],
906 hidpp_report->device_index);
907 return;
908 }
909
910 hid_info(hdev, "device of type %s (0x%02x) connected on slot %d",
911 device_type, hidpp_report->params[HIDPP_PARAM_PROTO_TYPE],
912 hidpp_report->device_index);
913
914
915 kfifo_in(&djrcv_dev->notif_fifo, &workitem, sizeof(workitem));
916
917 if (schedule_work(&djrcv_dev->work) == 0) {
918 dbg_hid("%s: did not schedule the work item, was already queued\n",
919 __func__);
920 }
921}
922
534a7b8e
NLC
923static void logi_dj_recv_forward_null_report(struct dj_receiver_dev *djrcv_dev,
924 struct dj_report *dj_report)
925{
926 /* We are called from atomic context (tasklet && djrcv->lock held) */
927 unsigned int i;
928 u8 reportbuffer[MAX_REPORT_SIZE];
929 struct dj_device *djdev;
930
931 djdev = djrcv_dev->paired_dj_devices[dj_report->device_index];
932
534a7b8e
NLC
933 memset(reportbuffer, 0, sizeof(reportbuffer));
934
935 for (i = 0; i < NUMBER_OF_HID_REPORTS; i++) {
936 if (djdev->reports_supported & (1 << i)) {
937 reportbuffer[0] = i;
938 if (hid_input_report(djdev->hdev,
939 HID_INPUT_REPORT,
940 reportbuffer,
941 hid_reportid_size_map[i], 1)) {
942 dbg_hid("hid_input_report error sending null "
943 "report\n");
944 }
945 }
946 }
947}
948
83898234
BT
949static void logi_dj_recv_forward_dj(struct dj_receiver_dev *djrcv_dev,
950 struct dj_report *dj_report)
534a7b8e
NLC
951{
952 /* We are called from atomic context (tasklet && djrcv->lock held) */
953 struct dj_device *dj_device;
954
955 dj_device = djrcv_dev->paired_dj_devices[dj_report->device_index];
956
534a7b8e
NLC
957 if ((dj_report->report_type > ARRAY_SIZE(hid_reportid_size_map) - 1) ||
958 (hid_reportid_size_map[dj_report->report_type] == 0)) {
959 dbg_hid("invalid report type:%x\n", dj_report->report_type);
960 return;
961 }
962
963 if (hid_input_report(dj_device->hdev,
964 HID_INPUT_REPORT, &dj_report->report_type,
965 hid_reportid_size_map[dj_report->report_type], 1)) {
966 dbg_hid("hid_input_report error\n");
967 }
968}
969
83898234
BT
970static void logi_dj_recv_forward_report(struct dj_device *dj_dev, u8 *data,
971 int size)
925f0f3e
BT
972{
973 /* We are called from atomic context (tasklet && djrcv->lock held) */
974 if (hid_input_report(dj_dev->hdev, HID_INPUT_REPORT, data, size, 1))
975 dbg_hid("hid_input_report error\n");
976}
534a7b8e 977
74808f91
HG
978static void logi_dj_recv_forward_input_report(struct hid_device *hdev,
979 u8 *data, int size)
980{
981 struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
982 struct dj_device *dj_dev;
983 unsigned long flags;
984 u8 report = data[0];
985 int i;
986
987 if (report > REPORT_TYPE_RFREPORT_LAST) {
988 hid_err(hdev, "Unexpect input report number %d\n", report);
989 return;
990 }
991
992 spin_lock_irqsave(&djrcv_dev->lock, flags);
993 for (i = 0; i < (DJ_MAX_PAIRED_DEVICES + DJ_DEVICE_INDEX_MIN); i++) {
994 dj_dev = djrcv_dev->paired_dj_devices[i];
995 if (dj_dev && (dj_dev->reports_supported & BIT(report))) {
996 logi_dj_recv_forward_report(dj_dev, data, size);
997 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
998 return;
999 }
1000 }
1001
1002 logi_dj_recv_queue_unknown_work(djrcv_dev);
1003 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
1004
1005 dbg_hid("No dj-devs handling input report number %d\n", report);
1006}
1007
534a7b8e
NLC
1008static int logi_dj_recv_send_report(struct dj_receiver_dev *djrcv_dev,
1009 struct dj_report *dj_report)
1010{
0ee75544 1011 struct hid_device *hdev = djrcv_dev->hidpp;
dcd9006b
BT
1012 struct hid_report *report;
1013 struct hid_report_enum *output_report_enum;
1014 u8 *data = (u8 *)(&dj_report->device_index);
297502ab 1015 unsigned int i;
534a7b8e 1016
dcd9006b
BT
1017 output_report_enum = &hdev->report_enum[HID_OUTPUT_REPORT];
1018 report = output_report_enum->report_id_hash[REPORT_ID_DJ_SHORT];
1019
1020 if (!report) {
1021 dev_err(&hdev->dev, "%s: unable to find dj report\n", __func__);
534a7b8e
NLC
1022 return -ENODEV;
1023 }
1024
297502ab 1025 for (i = 0; i < DJREPORT_SHORT_LENGTH - 1; i++)
dcd9006b
BT
1026 report->field[0]->value[i] = data[i];
1027
83a44ac8 1028 hid_hw_request(hdev, report, HID_REQ_SET_REPORT);
534a7b8e 1029
dcd9006b 1030 return 0;
534a7b8e
NLC
1031}
1032
74808f91
HG
1033static int logi_dj_recv_query_hidpp_devices(struct dj_receiver_dev *djrcv_dev)
1034{
1035 const u8 template[] = {REPORT_ID_HIDPP_SHORT,
1036 HIDPP_RECEIVER_INDEX,
1037 HIDPP_SET_REGISTER,
1038 HIDPP_REG_CONNECTION_STATE,
1039 HIDPP_FAKE_DEVICE_ARRIVAL,
1040 0x00, 0x00};
1041 u8 *hidpp_report;
1042 int retval;
1043
1044 hidpp_report = kmemdup(template, sizeof(template), GFP_KERNEL);
1045 if (!hidpp_report)
1046 return -ENOMEM;
1047
1048 retval = hid_hw_raw_request(djrcv_dev->hidpp,
1049 REPORT_ID_HIDPP_SHORT,
1050 hidpp_report, sizeof(template),
1051 HID_OUTPUT_REPORT,
1052 HID_REQ_SET_REPORT);
1053
1054 kfree(hidpp_report);
1055 return 0;
1056}
1057
534a7b8e
NLC
1058static int logi_dj_recv_query_paired_devices(struct dj_receiver_dev *djrcv_dev)
1059{
d8dc3494
MD
1060 struct dj_report *dj_report;
1061 int retval;
534a7b8e 1062
b6aeedde
HG
1063 djrcv_dev->last_query = jiffies;
1064
74808f91
HG
1065 if (djrcv_dev->type != recvr_type_dj)
1066 return logi_dj_recv_query_hidpp_devices(djrcv_dev);
1067
8a55ade7 1068 dj_report = kzalloc(sizeof(struct dj_report), GFP_KERNEL);
d8dc3494
MD
1069 if (!dj_report)
1070 return -ENOMEM;
1071 dj_report->report_id = REPORT_ID_DJ_SHORT;
1072 dj_report->device_index = 0xFF;
1073 dj_report->report_type = REPORT_TYPE_CMD_GET_PAIRED_DEVICES;
1074 retval = logi_dj_recv_send_report(djrcv_dev, dj_report);
1075 kfree(dj_report);
1076 return retval;
534a7b8e
NLC
1077}
1078
c63e0e37 1079
534a7b8e
NLC
1080static int logi_dj_recv_switch_to_dj_mode(struct dj_receiver_dev *djrcv_dev,
1081 unsigned timeout)
1082{
0ee75544 1083 struct hid_device *hdev = djrcv_dev->hidpp;
d8dc3494 1084 struct dj_report *dj_report;
6a9ddc89 1085 u8 *buf;
74808f91 1086 int retval = 0;
534a7b8e 1087
8a55ade7 1088 dj_report = kzalloc(sizeof(struct dj_report), GFP_KERNEL);
d8dc3494
MD
1089 if (!dj_report)
1090 return -ENOMEM;
42c22dbf 1091
74808f91
HG
1092 if (djrcv_dev->type == recvr_type_dj) {
1093 dj_report->report_id = REPORT_ID_DJ_SHORT;
1094 dj_report->device_index = 0xFF;
1095 dj_report->report_type = REPORT_TYPE_CMD_SWITCH;
1096 dj_report->report_params[CMD_SWITCH_PARAM_DEVBITFIELD] = 0x3F;
1097 dj_report->report_params[CMD_SWITCH_PARAM_TIMEOUT_SECONDS] =
1098 (u8)timeout;
1099
1100 retval = logi_dj_recv_send_report(djrcv_dev, dj_report);
1101
1102 /*
1103 * Ugly sleep to work around a USB 3.0 bug when the receiver is
1104 * still processing the "switch-to-dj" command while we send an
1105 * other command.
1106 * 50 msec should gives enough time to the receiver to be ready.
1107 */
1108 msleep(50);
1109 }
42c22dbf 1110
6a9ddc89
BT
1111 /*
1112 * Magical bits to set up hidpp notifications when the dj devices
1113 * are connected/disconnected.
1114 *
1115 * We can reuse dj_report because HIDPP_REPORT_SHORT_LENGTH is smaller
1116 * than DJREPORT_SHORT_LENGTH.
1117 */
1118 buf = (u8 *)dj_report;
1119
1120 memset(buf, 0, HIDPP_REPORT_SHORT_LENGTH);
1121
1122 buf[0] = REPORT_ID_HIDPP_SHORT;
1123 buf[1] = 0xFF;
1124 buf[2] = 0x80;
1125 buf[3] = 0x00;
1126 buf[4] = 0x00;
1127 buf[5] = 0x09;
1128 buf[6] = 0x00;
1129
1130 hid_hw_raw_request(hdev, REPORT_ID_HIDPP_SHORT, buf,
1131 HIDPP_REPORT_SHORT_LENGTH, HID_OUTPUT_REPORT,
1132 HID_REQ_SET_REPORT);
1133
1134 kfree(dj_report);
d8dc3494 1135 return retval;
534a7b8e
NLC
1136}
1137
1138
1139static int logi_dj_ll_open(struct hid_device *hid)
1140{
1141 dbg_hid("%s:%s\n", __func__, hid->phys);
1142 return 0;
1143
1144}
1145
1146static void logi_dj_ll_close(struct hid_device *hid)
1147{
1148 dbg_hid("%s:%s\n", __func__, hid->phys);
1149}
1150
8dba3026
BT
1151/*
1152 * Register 0xB5 is "pairing information". It is solely intended for the
1153 * receiver, so do not overwrite the device index.
1154 */
c0340412
BT
1155static u8 unifying_pairing_query[] = { REPORT_ID_HIDPP_SHORT,
1156 HIDPP_RECEIVER_INDEX,
1157 HIDPP_GET_LONG_REGISTER,
1158 HIDPP_REG_PAIRING_INFORMATION };
1159static u8 unifying_pairing_answer[] = { REPORT_ID_HIDPP_LONG,
1160 HIDPP_RECEIVER_INDEX,
1161 HIDPP_GET_LONG_REGISTER,
1162 HIDPP_REG_PAIRING_INFORMATION };
33797820 1163
bd27e202
BT
1164static int logi_dj_ll_raw_request(struct hid_device *hid,
1165 unsigned char reportnum, __u8 *buf,
1166 size_t count, unsigned char report_type,
1167 int reqtype)
534a7b8e 1168{
0e40d356
BT
1169 struct dj_device *djdev = hid->driver_data;
1170 struct dj_receiver_dev *djrcv_dev = djdev->dj_receiver_dev;
1171 u8 *out_buf;
1172 int ret;
534a7b8e 1173
925f0f3e
BT
1174 if ((buf[0] == REPORT_ID_HIDPP_SHORT) ||
1175 (buf[0] == REPORT_ID_HIDPP_LONG)) {
1176 if (count < 2)
1177 return -EINVAL;
1178
33797820
BT
1179 /* special case where we should not overwrite
1180 * the device_index */
8dba3026
BT
1181 if (count == 7 && !memcmp(buf, unifying_pairing_query,
1182 sizeof(unifying_pairing_query)))
1183 buf[4] = (buf[4] & 0xf0) | (djdev->device_index - 1);
33797820
BT
1184 else
1185 buf[1] = djdev->device_index;
0ee75544 1186 return hid_hw_raw_request(djrcv_dev->hidpp, reportnum, buf,
925f0f3e
BT
1187 count, report_type, reqtype);
1188 }
1189
0e40d356
BT
1190 if (buf[0] != REPORT_TYPE_LEDS)
1191 return -EINVAL;
1192
74808f91
HG
1193 if (djrcv_dev->type != recvr_type_dj && count >= 2) {
1194 if (!djrcv_dev->keyboard) {
1195 hid_warn(hid, "Received REPORT_TYPE_LEDS request before the keyboard interface was enumerated\n");
1196 return 0;
1197 }
1198 /* usbhid overrides the report ID and ignores the first byte */
1199 return hid_hw_raw_request(djrcv_dev->keyboard, 0, buf, count,
1200 report_type, reqtype);
1201 }
1202
0e40d356
BT
1203 out_buf = kzalloc(DJREPORT_SHORT_LENGTH, GFP_ATOMIC);
1204 if (!out_buf)
1205 return -ENOMEM;
1206
51217e69 1207 if (count > DJREPORT_SHORT_LENGTH - 2)
0e40d356
BT
1208 count = DJREPORT_SHORT_LENGTH - 2;
1209
1210 out_buf[0] = REPORT_ID_DJ_SHORT;
1211 out_buf[1] = djdev->device_index;
1212 memcpy(out_buf + 2, buf, count);
1213
0ee75544 1214 ret = hid_hw_raw_request(djrcv_dev->hidpp, out_buf[0], out_buf,
bd27e202 1215 DJREPORT_SHORT_LENGTH, report_type, reqtype);
0e40d356
BT
1216
1217 kfree(out_buf);
1218 return ret;
534a7b8e
NLC
1219}
1220
6d603326 1221static void rdcat(char *rdesc, unsigned int *rsize, const char *data, unsigned int size)
2a039bf5 1222{
6d603326 1223 memcpy(rdesc + *rsize, data, size);
2a039bf5
HR
1224 *rsize += size;
1225}
1226
534a7b8e
NLC
1227static int logi_dj_ll_parse(struct hid_device *hid)
1228{
1229 struct dj_device *djdev = hid->driver_data;
2a039bf5
HR
1230 unsigned int rsize = 0;
1231 char *rdesc;
534a7b8e
NLC
1232 int retval;
1233
1234 dbg_hid("%s\n", __func__);
1235
1236 djdev->hdev->version = 0x0111;
1237 djdev->hdev->country = 0x00;
1238
2a039bf5
HR
1239 rdesc = kmalloc(MAX_RDESC_SIZE, GFP_KERNEL);
1240 if (!rdesc)
1241 return -ENOMEM;
1242
534a7b8e
NLC
1243 if (djdev->reports_supported & STD_KEYBOARD) {
1244 dbg_hid("%s: sending a kbd descriptor, reports_supported: %x\n",
1245 __func__, djdev->reports_supported);
6d603326 1246 rdcat(rdesc, &rsize, kbd_descriptor, sizeof(kbd_descriptor));
534a7b8e
NLC
1247 }
1248
1249 if (djdev->reports_supported & STD_MOUSE) {
1250 dbg_hid("%s: sending a mouse descriptor, reports_supported: "
1251 "%x\n", __func__, djdev->reports_supported);
f5fb57a7
BT
1252 if (djdev->dj_receiver_dev->type == recvr_type_gaming_hidpp)
1253 rdcat(rdesc, &rsize, mse_high_res_descriptor,
1254 sizeof(mse_high_res_descriptor));
c9121cf6
HG
1255 else if (djdev->dj_receiver_dev->type == recvr_type_27mhz)
1256 rdcat(rdesc, &rsize, mse_27mhz_descriptor,
1257 sizeof(mse_27mhz_descriptor));
f5fb57a7
BT
1258 else
1259 rdcat(rdesc, &rsize, mse_descriptor,
1260 sizeof(mse_descriptor));
534a7b8e
NLC
1261 }
1262
1263 if (djdev->reports_supported & MULTIMEDIA) {
1264 dbg_hid("%s: sending a multimedia report descriptor: %x\n",
1265 __func__, djdev->reports_supported);
6d603326 1266 rdcat(rdesc, &rsize, consumer_descriptor, sizeof(consumer_descriptor));
534a7b8e
NLC
1267 }
1268
1269 if (djdev->reports_supported & POWER_KEYS) {
1270 dbg_hid("%s: sending a power keys report descriptor: %x\n",
1271 __func__, djdev->reports_supported);
6d603326 1272 rdcat(rdesc, &rsize, syscontrol_descriptor, sizeof(syscontrol_descriptor));
534a7b8e
NLC
1273 }
1274
1275 if (djdev->reports_supported & MEDIA_CENTER) {
1276 dbg_hid("%s: sending a media center report descriptor: %x\n",
1277 __func__, djdev->reports_supported);
6d603326 1278 rdcat(rdesc, &rsize, media_descriptor, sizeof(media_descriptor));
534a7b8e
NLC
1279 }
1280
1281 if (djdev->reports_supported & KBD_LEDS) {
1282 dbg_hid("%s: need to send kbd leds report descriptor: %x\n",
1283 __func__, djdev->reports_supported);
1284 }
1285
925f0f3e
BT
1286 rdcat(rdesc, &rsize, hidpp_descriptor, sizeof(hidpp_descriptor));
1287
2a039bf5
HR
1288 retval = hid_parse_report(hid, rdesc, rsize);
1289 kfree(rdesc);
1290
1291 return retval;
534a7b8e
NLC
1292}
1293
534a7b8e
NLC
1294static int logi_dj_ll_start(struct hid_device *hid)
1295{
1296 dbg_hid("%s\n", __func__);
1297 return 0;
1298}
1299
1300static void logi_dj_ll_stop(struct hid_device *hid)
1301{
1302 dbg_hid("%s\n", __func__);
1303}
1304
1305
1306static struct hid_ll_driver logi_dj_ll_driver = {
1307 .parse = logi_dj_ll_parse,
1308 .start = logi_dj_ll_start,
1309 .stop = logi_dj_ll_stop,
1310 .open = logi_dj_ll_open,
1311 .close = logi_dj_ll_close,
bd27e202 1312 .raw_request = logi_dj_ll_raw_request,
534a7b8e
NLC
1313};
1314
925f0f3e 1315static int logi_dj_dj_event(struct hid_device *hdev,
534a7b8e
NLC
1316 struct hid_report *report, u8 *data,
1317 int size)
1318{
1319 struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
1320 struct dj_report *dj_report = (struct dj_report *) data;
1321 unsigned long flags;
534a7b8e 1322
925f0f3e
BT
1323 /*
1324 * Here we receive all data coming from iface 2, there are 3 cases:
534a7b8e 1325 *
925f0f3e
BT
1326 * 1) Data is intended for this driver i. e. data contains arrival,
1327 * departure, etc notifications, in which case we queue them for delayed
1328 * processing by the work queue. We return 1 to hid-core as no further
1329 * processing is required from it.
534a7b8e 1330 *
925f0f3e
BT
1331 * 2) Data informs a connection change, if the change means rf link
1332 * loss, then we must send a null report to the upper layer to discard
1333 * potentially pressed keys that may be repeated forever by the input
1334 * layer. Return 1 to hid-core as no further processing is required.
534a7b8e 1335 *
925f0f3e
BT
1336 * 3) Data is an actual input event from a paired DJ device in which
1337 * case we forward it to the correct hid device (via hid_input_report()
1338 * ) and return 1 so hid-core does not anything else with it.
534a7b8e 1339 */
5abfe85c 1340
ad3e14d7
JK
1341 if ((dj_report->device_index < DJ_DEVICE_INDEX_MIN) ||
1342 (dj_report->device_index > DJ_DEVICE_INDEX_MAX)) {
5abfe85c
BT
1343 /*
1344 * Device index is wrong, bail out.
1345 * This driver can ignore safely the receiver notifications,
1346 * so ignore those reports too.
1347 */
1348 if (dj_report->device_index != DJ_RECEIVER_INDEX)
1349 dev_err(&hdev->dev, "%s: invalid device index:%d\n",
ad3e14d7
JK
1350 __func__, dj_report->device_index);
1351 return false;
1352 }
534a7b8e
NLC
1353
1354 spin_lock_irqsave(&djrcv_dev->lock, flags);
368d4e59
BT
1355
1356 if (!djrcv_dev->paired_dj_devices[dj_report->device_index]) {
1357 /* received an event for an unknown device, bail out */
1358 logi_dj_recv_queue_notification(djrcv_dev, dj_report);
1359 goto out;
1360 }
1361
5abfe85c
BT
1362 switch (dj_report->report_type) {
1363 case REPORT_TYPE_NOTIF_DEVICE_PAIRED:
368d4e59
BT
1364 /* pairing notifications are handled above the switch */
1365 break;
5abfe85c
BT
1366 case REPORT_TYPE_NOTIF_DEVICE_UNPAIRED:
1367 logi_dj_recv_queue_notification(djrcv_dev, dj_report);
1368 break;
1369 case REPORT_TYPE_NOTIF_CONNECTION_STATUS:
1370 if (dj_report->report_params[CONNECTION_STATUS_PARAM_STATUS] ==
1371 STATUS_LINKLOSS) {
1372 logi_dj_recv_forward_null_report(djrcv_dev, dj_report);
534a7b8e 1373 }
5abfe85c
BT
1374 break;
1375 default:
83898234 1376 logi_dj_recv_forward_dj(djrcv_dev, dj_report);
534a7b8e 1377 }
368d4e59
BT
1378
1379out:
534a7b8e
NLC
1380 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
1381
5abfe85c 1382 return true;
534a7b8e
NLC
1383}
1384
925f0f3e
BT
1385static int logi_dj_hidpp_event(struct hid_device *hdev,
1386 struct hid_report *report, u8 *data,
1387 int size)
1388{
1389 struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
7bb56a5f 1390 struct hidpp_event *hidpp_report = (struct hidpp_event *) data;
74808f91 1391 struct dj_device *dj_dev;
925f0f3e 1392 unsigned long flags;
7bb56a5f 1393 u8 device_index = hidpp_report->device_index;
925f0f3e 1394
33797820
BT
1395 if (device_index == HIDPP_RECEIVER_INDEX) {
1396 /* special case were the device wants to know its unifying
1397 * name */
1398 if (size == HIDPP_REPORT_LONG_LENGTH &&
8dba3026
BT
1399 !memcmp(data, unifying_pairing_answer,
1400 sizeof(unifying_pairing_answer)))
33797820
BT
1401 device_index = (data[4] & 0x0F) + 1;
1402 else
1403 return false;
1404 }
925f0f3e
BT
1405
1406 /*
1407 * Data is from the HID++ collection, in this case, we forward the
1408 * data to the corresponding child dj device and return 0 to hid-core
1409 * so he data also goes to the hidraw device of the receiver. This
1410 * allows a user space application to implement the full HID++ routing
1411 * via the receiver.
1412 */
1413
1414 if ((device_index < DJ_DEVICE_INDEX_MIN) ||
1415 (device_index > DJ_DEVICE_INDEX_MAX)) {
1416 /*
1417 * Device index is wrong, bail out.
1418 * This driver can ignore safely the receiver notifications,
1419 * so ignore those reports too.
1420 */
1421 dev_err(&hdev->dev, "%s: invalid device index:%d\n",
7bb56a5f 1422 __func__, hidpp_report->device_index);
925f0f3e
BT
1423 return false;
1424 }
1425
1426 spin_lock_irqsave(&djrcv_dev->lock, flags);
1427
74808f91 1428 dj_dev = djrcv_dev->paired_dj_devices[device_index];
c9121cf6
HG
1429
1430 /*
1431 * With 27 MHz receivers, we do not get an explicit unpair event,
1432 * remove the old device if the user has paired a *different* device.
1433 */
1434 if (djrcv_dev->type == recvr_type_27mhz && dj_dev &&
1435 hidpp_report->sub_id == REPORT_TYPE_NOTIF_DEVICE_CONNECTED &&
1436 hidpp_report->params[HIDPP_PARAM_PROTO_TYPE] == 0x02 &&
1437 hidpp_report->params[HIDPP_PARAM_27MHZ_DEVID] !=
1438 dj_dev->hdev->product) {
1439 struct dj_workitem workitem = {
1440 .device_index = hidpp_report->device_index,
1441 .type = WORKITEM_TYPE_UNPAIRED,
1442 };
1443 kfifo_in(&djrcv_dev->notif_fifo, &workitem, sizeof(workitem));
1444 /* logi_hidpp_recv_queue_notif will queue the work */
1445 dj_dev = NULL;
1446 }
1447
74808f91
HG
1448 if (dj_dev) {
1449 logi_dj_recv_forward_report(dj_dev, data, size);
1450 } else {
1451 if (hidpp_report->sub_id == REPORT_TYPE_NOTIF_DEVICE_CONNECTED)
1452 logi_hidpp_recv_queue_notif(hdev, hidpp_report);
1453 else
1454 logi_dj_recv_queue_unknown_work(djrcv_dev);
1455 }
925f0f3e 1456
925f0f3e
BT
1457 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
1458
1459 return false;
1460}
1461
1462static int logi_dj_raw_event(struct hid_device *hdev,
1463 struct hid_report *report, u8 *data,
1464 int size)
1465{
74808f91 1466 struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
925f0f3e
BT
1467 dbg_hid("%s, size:%d\n", __func__, size);
1468
74808f91
HG
1469 if (!hdev->report_enum[HID_INPUT_REPORT].numbered) {
1470
1471 if (djrcv_dev->unnumbered_application == HID_GD_KEYBOARD) {
1472 /*
1473 * For the keyboard, we can reuse the same report by
1474 * using the second byte which is constant in the USB
1475 * HID report descriptor.
1476 */
1477 data[1] = data[0];
1478 data[0] = REPORT_TYPE_KEYBOARD;
1479
1480 logi_dj_recv_forward_input_report(hdev, data, size);
1481
1482 /* restore previous state */
1483 data[0] = data[1];
1484 data[1] = 0;
1485 }
1f944ac6
HG
1486 /* The 27 MHz mouse-only receiver sends unnumbered mouse data */
1487 if (djrcv_dev->unnumbered_application == HID_GD_MOUSE &&
1488 size == 6) {
1489 u8 mouse_report[7];
1490
1491 /* Prepend report id */
1492 mouse_report[0] = REPORT_TYPE_MOUSE;
1493 memcpy(mouse_report + 1, data, 6);
1494 logi_dj_recv_forward_input_report(hdev, mouse_report, 7);
1495 }
74808f91
HG
1496
1497 return false;
1498 }
1499
925f0f3e
BT
1500 switch (data[0]) {
1501 case REPORT_ID_DJ_SHORT:
f254ae93
PW
1502 if (size != DJREPORT_SHORT_LENGTH) {
1503 dev_err(&hdev->dev, "DJ report of bad size (%d)", size);
1504 return false;
1505 }
925f0f3e 1506 return logi_dj_dj_event(hdev, report, data, size);
f5fb57a7
BT
1507 case REPORT_ID_DJ_LONG:
1508 if (size != DJREPORT_LONG_LENGTH) {
1509 dev_err(&hdev->dev, "DJ report of bad size (%d)", size);
1510 return false;
1511 }
1512 return logi_dj_dj_event(hdev, report, data, size);
925f0f3e 1513 case REPORT_ID_HIDPP_SHORT:
f254ae93
PW
1514 if (size != HIDPP_REPORT_SHORT_LENGTH) {
1515 dev_err(&hdev->dev,
1516 "Short HID++ report of bad size (%d)", size);
1517 return false;
1518 }
1519 return logi_dj_hidpp_event(hdev, report, data, size);
925f0f3e 1520 case REPORT_ID_HIDPP_LONG:
f254ae93
PW
1521 if (size != HIDPP_REPORT_LONG_LENGTH) {
1522 dev_err(&hdev->dev,
1523 "Long HID++ report of bad size (%d)", size);
1524 return false;
1525 }
925f0f3e
BT
1526 return logi_dj_hidpp_event(hdev, report, data, size);
1527 }
1528
74808f91
HG
1529 logi_dj_recv_forward_input_report(hdev, data, size);
1530
925f0f3e
BT
1531 return false;
1532}
1533
534a7b8e
NLC
1534static int logi_dj_probe(struct hid_device *hdev,
1535 const struct hid_device_id *id)
1536{
82c0beb8
BT
1537 struct hid_report_enum *rep_enum;
1538 struct hid_report *rep;
534a7b8e 1539 struct dj_receiver_dev *djrcv_dev;
82c0beb8 1540 bool has_hidpp = false;
a1d97ccb 1541 unsigned long flags;
534a7b8e
NLC
1542 int retval;
1543
82c0beb8
BT
1544 /*
1545 * Call to usbhid to fetch the HID descriptors of the current
1546 * interface subsequently call to the hid/hid-core to parse the
1547 * fetched descriptors.
1548 */
1549 retval = hid_parse(hdev);
1550 if (retval) {
1551 dev_err(&hdev->dev,
1552 "%s:parse failed\n", __func__);
1553 return retval;
1554 }
534a7b8e 1555
82c0beb8
BT
1556 rep_enum = &hdev->report_enum[HID_INPUT_REPORT];
1557
74808f91
HG
1558 /* no input reports, bail out */
1559 if (list_empty(&rep_enum->report_list))
1560 return -ENODEV;
1561
82c0beb8
BT
1562 /*
1563 * Check for the HID++ application.
1564 * Note: we should theoretically check for HID++ and DJ
1565 * collections, but this will do.
1566 */
1567 list_for_each_entry(rep, &rep_enum->report_list, list) {
1568 if (rep->application == 0xff000001)
1569 has_hidpp = true;
534a7b8e
NLC
1570 }
1571
82c0beb8
BT
1572 /*
1573 * Ignore interfaces without DJ/HID++ collection, they will not carry
1574 * any data, dont create any hid_device for them.
1575 */
74808f91 1576 if (!has_hidpp && id->driver_data == recvr_type_dj)
82c0beb8
BT
1577 return -ENODEV;
1578
a1d97ccb
HG
1579 /* get the current application attached to the node */
1580 rep = list_first_entry(&rep_enum->report_list, struct hid_report, list);
74808f91 1581 djrcv_dev = dj_get_receiver_dev(hdev, id->driver_data,
a1d97ccb 1582 rep->application, has_hidpp);
534a7b8e
NLC
1583 if (!djrcv_dev) {
1584 dev_err(&hdev->dev,
1585 "%s:failed allocating dj_receiver_dev\n", __func__);
1586 return -ENOMEM;
1587 }
297502ab 1588
74808f91
HG
1589 if (!rep_enum->numbered)
1590 djrcv_dev->unnumbered_application = rep->application;
1591
534a7b8e
NLC
1592 /* Starts the usb device and connects to upper interfaces hiddev and
1593 * hidraw */
74808f91 1594 retval = hid_hw_start(hdev, HID_CONNECT_HIDRAW|HID_CONNECT_HIDDEV);
534a7b8e
NLC
1595 if (retval) {
1596 dev_err(&hdev->dev,
1597 "%s:hid_hw_start returned error\n", __func__);
1598 goto hid_hw_start_fail;
1599 }
1600
74808f91
HG
1601 if (has_hidpp) {
1602 retval = logi_dj_recv_switch_to_dj_mode(djrcv_dev, 0);
1603 if (retval < 0) {
1604 hid_err(hdev, "%s: logi_dj_recv_switch_to_dj_mode returned error:%d\n",
1605 __func__, retval);
1606 goto switch_to_dj_mode_fail;
1607 }
534a7b8e
NLC
1608 }
1609
1610 /* This is enabling the polling urb on the IN endpoint */
ddf7540e 1611 retval = hid_hw_open(hdev);
534a7b8e 1612 if (retval < 0) {
ddf7540e
BT
1613 dev_err(&hdev->dev, "%s:hid_hw_open returned error:%d\n",
1614 __func__, retval);
534a7b8e
NLC
1615 goto llopen_failed;
1616 }
1617
a9dd22b7
AR
1618 /* Allow incoming packets to arrive: */
1619 hid_device_io_start(hdev);
1620
74808f91
HG
1621 if (has_hidpp) {
1622 spin_lock_irqsave(&djrcv_dev->lock, flags);
1623 djrcv_dev->ready = true;
1624 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
1625 retval = logi_dj_recv_query_paired_devices(djrcv_dev);
1626 if (retval < 0) {
1627 hid_err(hdev, "%s: logi_dj_recv_query_paired_devices error:%d\n",
1628 __func__, retval);
1629 goto logi_dj_recv_query_paired_devices_failed;
1630 }
534a7b8e
NLC
1631 }
1632
1633 return retval;
1634
1635logi_dj_recv_query_paired_devices_failed:
ddf7540e 1636 hid_hw_close(hdev);
534a7b8e
NLC
1637
1638llopen_failed:
1639switch_to_dj_mode_fail:
1640 hid_hw_stop(hdev);
1641
1642hid_hw_start_fail:
a1d97ccb 1643 dj_put_receiver_dev(hdev);
534a7b8e 1644 return retval;
534a7b8e
NLC
1645}
1646
1647#ifdef CONFIG_PM
1648static int logi_dj_reset_resume(struct hid_device *hdev)
1649{
1650 int retval;
1651 struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
1652
74808f91
HG
1653 if (djrcv_dev->hidpp != hdev)
1654 return 0;
1655
534a7b8e
NLC
1656 retval = logi_dj_recv_switch_to_dj_mode(djrcv_dev, 0);
1657 if (retval < 0) {
1658 dev_err(&hdev->dev,
1659 "%s:logi_dj_recv_switch_to_dj_mode returned error:%d\n",
1660 __func__, retval);
1661 }
1662
1663 return 0;
1664}
1665#endif
1666
1667static void logi_dj_remove(struct hid_device *hdev)
1668{
1669 struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
1670 struct dj_device *dj_dev;
a1d97ccb 1671 unsigned long flags;
534a7b8e
NLC
1672 int i;
1673
1674 dbg_hid("%s\n", __func__);
1675
a1d97ccb
HG
1676 /*
1677 * This ensures that if the work gets requeued from another
1678 * interface of the same receiver it will be a no-op.
1679 */
1680 spin_lock_irqsave(&djrcv_dev->lock, flags);
1681 djrcv_dev->ready = false;
1682 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
1683
534a7b8e
NLC
1684 cancel_work_sync(&djrcv_dev->work);
1685
ddf7540e 1686 hid_hw_close(hdev);
534a7b8e
NLC
1687 hid_hw_stop(hdev);
1688
a1d97ccb
HG
1689 /*
1690 * For proper operation we need access to all interfaces, so we destroy
1691 * the paired devices when we're unbound from any interface.
1692 *
1693 * Note we may still be bound to other interfaces, sharing the same
1694 * djrcv_dev, so we need locking here.
1695 */
844580ff 1696 for (i = 0; i < (DJ_MAX_PAIRED_DEVICES + DJ_DEVICE_INDEX_MIN); i++) {
a1d97ccb 1697 spin_lock_irqsave(&djrcv_dev->lock, flags);
534a7b8e 1698 dj_dev = djrcv_dev->paired_dj_devices[i];
a1d97ccb
HG
1699 djrcv_dev->paired_dj_devices[i] = NULL;
1700 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
534a7b8e
NLC
1701 if (dj_dev != NULL) {
1702 hid_destroy_device(dj_dev->hdev);
1703 kfree(dj_dev);
534a7b8e
NLC
1704 }
1705 }
1706
a1d97ccb 1707 dj_put_receiver_dev(hdev);
534a7b8e
NLC
1708}
1709
534a7b8e
NLC
1710static const struct hid_device_id logi_dj_receivers[] = {
1711 {HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
74808f91
HG
1712 USB_DEVICE_ID_LOGITECH_UNIFYING_RECEIVER),
1713 .driver_data = recvr_type_dj},
534a7b8e 1714 {HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
74808f91
HG
1715 USB_DEVICE_ID_LOGITECH_UNIFYING_RECEIVER_2),
1716 .driver_data = recvr_type_dj},
1717 { /* Logitech Nano (non DJ) receiver */
1718 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
1719 USB_DEVICE_ID_LOGITECH_NANO_RECEIVER),
1720 .driver_data = recvr_type_hidpp},
1721 { /* Logitech Nano (non DJ) receiver */
1722 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
1723 USB_DEVICE_ID_LOGITECH_NANO_RECEIVER_2),
1724 .driver_data = recvr_type_hidpp},
f5fb57a7
BT
1725 { /* Logitech gaming receiver (0xc539) */
1726 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
1727 USB_DEVICE_ID_LOGITECH_NANO_RECEIVER_GAMING),
1728 .driver_data = recvr_type_gaming_hidpp},
c9121cf6
HG
1729 { /* Logitech 27 MHz HID++ 1.0 receiver (0xc517) */
1730 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
1731 USB_DEVICE_ID_S510_RECEIVER_2),
1732 .driver_data = recvr_type_27mhz},
1f944ac6
HG
1733 { /* Logitech 27 MHz HID++ 1.0 mouse-only receiver (0xc51b) */
1734 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
1735 USB_DEVICE_ID_LOGITECH_27MHZ_MOUSE_RECEIVER),
1736 .driver_data = recvr_type_27mhz},
534a7b8e
NLC
1737 {}
1738};
1739
1740MODULE_DEVICE_TABLE(hid, logi_dj_receivers);
1741
1742static struct hid_driver logi_djreceiver_driver = {
1743 .name = "logitech-djreceiver",
1744 .id_table = logi_dj_receivers,
1745 .probe = logi_dj_probe,
1746 .remove = logi_dj_remove,
1747 .raw_event = logi_dj_raw_event,
1748#ifdef CONFIG_PM
1749 .reset_resume = logi_dj_reset_resume,
1750#endif
1751};
1752
ab94e562 1753module_hid_driver(logi_djreceiver_driver);
534a7b8e 1754
534a7b8e
NLC
1755MODULE_LICENSE("GPL");
1756MODULE_AUTHOR("Logitech");
1757MODULE_AUTHOR("Nestor Lopez Casado");
1758MODULE_AUTHOR("nlopezcasad@logitech.com");