]> git.proxmox.com Git - mirror_ubuntu-eoan-kernel.git/blame - drivers/hid/hid-logitech-dj.c
HID: logitech-dj: protect the paired_dj_devices access in add_djhid_dev with the...
[mirror_ubuntu-eoan-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
c0340412
BT
103#define HIDPP_GET_LONG_REGISTER 0x83
104#define HIDPP_REG_PAIRING_INFORMATION 0xB5
105#define HIDPP_PAIRING_INFORMATION 0x20
106
8cb3746a
BT
107struct dj_report {
108 u8 report_id;
109 u8 device_index;
110 u8 report_type;
111 u8 report_params[DJREPORT_SHORT_LENGTH - 3];
112};
113
7bb56a5f
BT
114struct hidpp_event {
115 u8 report_id;
116 u8 device_index;
117 u8 sub_id;
118 u8 params[HIDPP_REPORT_LONG_LENGTH - 3U];
119} __packed;
120
8cb3746a
BT
121struct dj_receiver_dev {
122 struct hid_device *hdev;
123 struct dj_device *paired_dj_devices[DJ_MAX_PAIRED_DEVICES +
124 DJ_DEVICE_INDEX_MIN];
125 struct work_struct work;
126 struct kfifo notif_fifo;
127 spinlock_t lock;
8cb3746a
BT
128};
129
130struct dj_device {
131 struct hid_device *hdev;
132 struct dj_receiver_dev *dj_receiver_dev;
133 u32 reports_supported;
134 u8 device_index;
135};
534a7b8e 136
4fcad95a
BT
137#define WORKITEM_TYPE_EMPTY 0
138#define WORKITEM_TYPE_PAIRED 1
139#define WORKITEM_TYPE_UNPAIRED 2
140#define WORKITEM_TYPE_UNKNOWN 255
141
142struct dj_workitem {
143 u8 type; /* WORKITEM_TYPE_* */
144 u8 device_index;
145 u8 quad_id_msb;
146 u8 quad_id_lsb;
147 u32 reports_supported;
148};
149
534a7b8e
NLC
150/* Keyboard descriptor (1) */
151static const char kbd_descriptor[] = {
152 0x05, 0x01, /* USAGE_PAGE (generic Desktop) */
153 0x09, 0x06, /* USAGE (Keyboard) */
154 0xA1, 0x01, /* COLLECTION (Application) */
155 0x85, 0x01, /* REPORT_ID (1) */
156 0x95, 0x08, /* REPORT_COUNT (8) */
157 0x75, 0x01, /* REPORT_SIZE (1) */
158 0x15, 0x00, /* LOGICAL_MINIMUM (0) */
159 0x25, 0x01, /* LOGICAL_MAXIMUM (1) */
160 0x05, 0x07, /* USAGE_PAGE (Keyboard) */
161 0x19, 0xE0, /* USAGE_MINIMUM (Left Control) */
162 0x29, 0xE7, /* USAGE_MAXIMUM (Right GUI) */
163 0x81, 0x02, /* INPUT (Data,Var,Abs) */
534a7b8e
NLC
164 0x95, 0x06, /* REPORT_COUNT (6) */
165 0x75, 0x08, /* REPORT_SIZE (8) */
166 0x15, 0x00, /* LOGICAL_MINIMUM (0) */
167 0x26, 0xFF, 0x00, /* LOGICAL_MAXIMUM (255) */
168 0x05, 0x07, /* USAGE_PAGE (Keyboard) */
169 0x19, 0x00, /* USAGE_MINIMUM (no event) */
170 0x2A, 0xFF, 0x00, /* USAGE_MAXIMUM (reserved) */
171 0x81, 0x00, /* INPUT (Data,Ary,Abs) */
0e40d356
BT
172 0x85, 0x0e, /* REPORT_ID (14) */
173 0x05, 0x08, /* USAGE PAGE (LED page) */
174 0x95, 0x05, /* REPORT COUNT (5) */
175 0x75, 0x01, /* REPORT SIZE (1) */
176 0x15, 0x00, /* LOGICAL_MINIMUM (0) */
177 0x25, 0x01, /* LOGICAL_MAXIMUM (1) */
178 0x19, 0x01, /* USAGE MINIMUM (1) */
179 0x29, 0x05, /* USAGE MAXIMUM (5) */
180 0x91, 0x02, /* OUTPUT (Data, Variable, Absolute) */
181 0x95, 0x01, /* REPORT COUNT (1) */
182 0x75, 0x03, /* REPORT SIZE (3) */
183 0x91, 0x01, /* OUTPUT (Constant) */
534a7b8e
NLC
184 0xC0
185};
186
187/* Mouse descriptor (2) */
188static const char mse_descriptor[] = {
189 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */
190 0x09, 0x02, /* USAGE (Mouse) */
191 0xA1, 0x01, /* COLLECTION (Application) */
192 0x85, 0x02, /* REPORT_ID = 2 */
193 0x09, 0x01, /* USAGE (pointer) */
194 0xA1, 0x00, /* COLLECTION (physical) */
195 0x05, 0x09, /* USAGE_PAGE (buttons) */
196 0x19, 0x01, /* USAGE_MIN (1) */
197 0x29, 0x10, /* USAGE_MAX (16) */
198 0x15, 0x00, /* LOGICAL_MIN (0) */
199 0x25, 0x01, /* LOGICAL_MAX (1) */
200 0x95, 0x10, /* REPORT_COUNT (16) */
201 0x75, 0x01, /* REPORT_SIZE (1) */
202 0x81, 0x02, /* INPUT (data var abs) */
203 0x05, 0x01, /* USAGE_PAGE (generic desktop) */
204 0x16, 0x01, 0xF8, /* LOGICAL_MIN (-2047) */
205 0x26, 0xFF, 0x07, /* LOGICAL_MAX (2047) */
206 0x75, 0x0C, /* REPORT_SIZE (12) */
207 0x95, 0x02, /* REPORT_COUNT (2) */
208 0x09, 0x30, /* USAGE (X) */
209 0x09, 0x31, /* USAGE (Y) */
210 0x81, 0x06, /* INPUT */
211 0x15, 0x81, /* LOGICAL_MIN (-127) */
212 0x25, 0x7F, /* LOGICAL_MAX (127) */
213 0x75, 0x08, /* REPORT_SIZE (8) */
214 0x95, 0x01, /* REPORT_COUNT (1) */
215 0x09, 0x38, /* USAGE (wheel) */
216 0x81, 0x06, /* INPUT */
217 0x05, 0x0C, /* USAGE_PAGE(consumer) */
218 0x0A, 0x38, 0x02, /* USAGE(AC Pan) */
219 0x95, 0x01, /* REPORT_COUNT (1) */
220 0x81, 0x06, /* INPUT */
221 0xC0, /* END_COLLECTION */
222 0xC0, /* END_COLLECTION */
223};
224
225/* Consumer Control descriptor (3) */
226static const char consumer_descriptor[] = {
227 0x05, 0x0C, /* USAGE_PAGE (Consumer Devices) */
228 0x09, 0x01, /* USAGE (Consumer Control) */
229 0xA1, 0x01, /* COLLECTION (Application) */
230 0x85, 0x03, /* REPORT_ID = 3 */
231 0x75, 0x10, /* REPORT_SIZE (16) */
232 0x95, 0x02, /* REPORT_COUNT (2) */
233 0x15, 0x01, /* LOGICAL_MIN (1) */
234 0x26, 0x8C, 0x02, /* LOGICAL_MAX (652) */
235 0x19, 0x01, /* USAGE_MIN (1) */
236 0x2A, 0x8C, 0x02, /* USAGE_MAX (652) */
237 0x81, 0x00, /* INPUT (Data Ary Abs) */
238 0xC0, /* END_COLLECTION */
239}; /* */
240
241/* System control descriptor (4) */
242static const char syscontrol_descriptor[] = {
243 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */
244 0x09, 0x80, /* USAGE (System Control) */
245 0xA1, 0x01, /* COLLECTION (Application) */
246 0x85, 0x04, /* REPORT_ID = 4 */
247 0x75, 0x02, /* REPORT_SIZE (2) */
248 0x95, 0x01, /* REPORT_COUNT (1) */
249 0x15, 0x01, /* LOGICAL_MIN (1) */
250 0x25, 0x03, /* LOGICAL_MAX (3) */
251 0x09, 0x82, /* USAGE (System Sleep) */
252 0x09, 0x81, /* USAGE (System Power Down) */
253 0x09, 0x83, /* USAGE (System Wake Up) */
254 0x81, 0x60, /* INPUT (Data Ary Abs NPrf Null) */
255 0x75, 0x06, /* REPORT_SIZE (6) */
256 0x81, 0x03, /* INPUT (Cnst Var Abs) */
257 0xC0, /* END_COLLECTION */
258};
259
260/* Media descriptor (8) */
261static const char media_descriptor[] = {
262 0x06, 0xbc, 0xff, /* Usage Page 0xffbc */
263 0x09, 0x88, /* Usage 0x0088 */
264 0xa1, 0x01, /* BeginCollection */
265 0x85, 0x08, /* Report ID 8 */
266 0x19, 0x01, /* Usage Min 0x0001 */
267 0x29, 0xff, /* Usage Max 0x00ff */
268 0x15, 0x01, /* Logical Min 1 */
269 0x26, 0xff, 0x00, /* Logical Max 255 */
270 0x75, 0x08, /* Report Size 8 */
271 0x95, 0x01, /* Report Count 1 */
272 0x81, 0x00, /* Input */
273 0xc0, /* EndCollection */
274}; /* */
275
925f0f3e
BT
276/* HIDPP descriptor */
277static const char hidpp_descriptor[] = {
278 0x06, 0x00, 0xff, /* Usage Page (Vendor Defined Page 1) */
279 0x09, 0x01, /* Usage (Vendor Usage 1) */
280 0xa1, 0x01, /* Collection (Application) */
281 0x85, 0x10, /* Report ID (16) */
282 0x75, 0x08, /* Report Size (8) */
283 0x95, 0x06, /* Report Count (6) */
284 0x15, 0x00, /* Logical Minimum (0) */
285 0x26, 0xff, 0x00, /* Logical Maximum (255) */
286 0x09, 0x01, /* Usage (Vendor Usage 1) */
287 0x81, 0x00, /* Input (Data,Arr,Abs) */
288 0x09, 0x01, /* Usage (Vendor Usage 1) */
289 0x91, 0x00, /* Output (Data,Arr,Abs) */
290 0xc0, /* End Collection */
291 0x06, 0x00, 0xff, /* Usage Page (Vendor Defined Page 1) */
292 0x09, 0x02, /* Usage (Vendor Usage 2) */
293 0xa1, 0x01, /* Collection (Application) */
294 0x85, 0x11, /* Report ID (17) */
295 0x75, 0x08, /* Report Size (8) */
296 0x95, 0x13, /* Report Count (19) */
297 0x15, 0x00, /* Logical Minimum (0) */
298 0x26, 0xff, 0x00, /* Logical Maximum (255) */
299 0x09, 0x02, /* Usage (Vendor Usage 2) */
300 0x81, 0x00, /* Input (Data,Arr,Abs) */
301 0x09, 0x02, /* Usage (Vendor Usage 2) */
302 0x91, 0x00, /* Output (Data,Arr,Abs) */
303 0xc0, /* End Collection */
304 0x06, 0x00, 0xff, /* Usage Page (Vendor Defined Page 1) */
305 0x09, 0x04, /* Usage (Vendor Usage 0x04) */
306 0xa1, 0x01, /* Collection (Application) */
307 0x85, 0x20, /* Report ID (32) */
308 0x75, 0x08, /* Report Size (8) */
309 0x95, 0x0e, /* Report Count (14) */
310 0x15, 0x00, /* Logical Minimum (0) */
311 0x26, 0xff, 0x00, /* Logical Maximum (255) */
312 0x09, 0x41, /* Usage (Vendor Usage 0x41) */
313 0x81, 0x00, /* Input (Data,Arr,Abs) */
314 0x09, 0x41, /* Usage (Vendor Usage 0x41) */
315 0x91, 0x00, /* Output (Data,Arr,Abs) */
316 0x85, 0x21, /* Report ID (33) */
317 0x95, 0x1f, /* Report Count (31) */
318 0x15, 0x00, /* Logical Minimum (0) */
319 0x26, 0xff, 0x00, /* Logical Maximum (255) */
320 0x09, 0x42, /* Usage (Vendor Usage 0x42) */
321 0x81, 0x00, /* Input (Data,Arr,Abs) */
322 0x09, 0x42, /* Usage (Vendor Usage 0x42) */
323 0x91, 0x00, /* Output (Data,Arr,Abs) */
324 0xc0, /* End Collection */
325};
326
534a7b8e
NLC
327/* Maximum size of all defined hid reports in bytes (including report id) */
328#define MAX_REPORT_SIZE 8
329
2a039bf5
HR
330/* Make sure all descriptors are present here */
331#define MAX_RDESC_SIZE \
332 (sizeof(kbd_descriptor) + \
333 sizeof(mse_descriptor) + \
334 sizeof(consumer_descriptor) + \
335 sizeof(syscontrol_descriptor) + \
925f0f3e
BT
336 sizeof(media_descriptor) + \
337 sizeof(hidpp_descriptor))
2a039bf5 338
534a7b8e
NLC
339/* Number of possible hid report types that can be created by this driver.
340 *
341 * Right now, RF report types have the same report types (or report id's)
342 * than the hid report created from those RF reports. In the future
343 * this doesnt have to be true.
344 *
345 * For instance, RF report type 0x01 which has a size of 8 bytes, corresponds
346 * to hid report id 0x01, this is standard keyboard. Same thing applies to mice
347 * reports and consumer control, etc. If a new RF report is created, it doesn't
348 * has to have the same report id as its corresponding hid report, so an
349 * translation may have to take place for future report types.
350 */
351#define NUMBER_OF_HID_REPORTS 32
352static const u8 hid_reportid_size_map[NUMBER_OF_HID_REPORTS] = {
353 [1] = 8, /* Standard keyboard */
354 [2] = 8, /* Standard mouse */
355 [3] = 5, /* Consumer control */
356 [4] = 2, /* System control */
357 [8] = 2, /* Media Center */
358};
359
360
361#define LOGITECH_DJ_INTERFACE_NUMBER 0x02
362
534a7b8e
NLC
363static struct hid_ll_driver logi_dj_ll_driver;
364
c63e0e37 365static int logi_dj_recv_query_paired_devices(struct dj_receiver_dev *djrcv_dev);
534a7b8e
NLC
366
367static void logi_dj_recv_destroy_djhid_device(struct dj_receiver_dev *djrcv_dev,
4fcad95a 368 struct dj_workitem *workitem)
534a7b8e
NLC
369{
370 /* Called in delayed work context */
371 struct dj_device *dj_dev;
372 unsigned long flags;
373
374 spin_lock_irqsave(&djrcv_dev->lock, flags);
4fcad95a
BT
375 dj_dev = djrcv_dev->paired_dj_devices[workitem->device_index];
376 djrcv_dev->paired_dj_devices[workitem->device_index] = NULL;
534a7b8e
NLC
377 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
378
379 if (dj_dev != NULL) {
380 hid_destroy_device(dj_dev->hdev);
381 kfree(dj_dev);
382 } else {
383 dev_err(&djrcv_dev->hdev->dev, "%s: can't destroy a NULL device\n",
384 __func__);
385 }
386}
387
388static void logi_dj_recv_add_djhid_device(struct dj_receiver_dev *djrcv_dev,
4fcad95a 389 struct dj_workitem *workitem)
534a7b8e
NLC
390{
391 /* Called in delayed work context */
392 struct hid_device *djrcv_hdev = djrcv_dev->hdev;
534a7b8e
NLC
393 struct hid_device *dj_hiddev;
394 struct dj_device *dj_dev;
4fcad95a 395 u8 device_index = workitem->device_index;
f41d766c 396 unsigned long flags;
534a7b8e
NLC
397
398 /* Device index goes from 1 to 6, we need 3 bytes to store the
399 * semicolon, the index, and a null terminator
400 */
401 unsigned char tmpstr[3];
402
f41d766c 403 /* We are the only one ever adding a device, no need to lock */
4fcad95a 404 if (djrcv_dev->paired_dj_devices[device_index]) {
c63e0e37
NLC
405 /* The device is already known. No need to reallocate it. */
406 dbg_hid("%s: device is already known\n", __func__);
407 return;
408 }
409
534a7b8e
NLC
410 dj_hiddev = hid_allocate_device();
411 if (IS_ERR(dj_hiddev)) {
412 dev_err(&djrcv_hdev->dev, "%s: hid_allocate_device failed\n",
413 __func__);
414 return;
415 }
416
417 dj_hiddev->ll_driver = &logi_dj_ll_driver;
534a7b8e
NLC
418
419 dj_hiddev->dev.parent = &djrcv_hdev->dev;
420 dj_hiddev->bus = BUS_USB;
82c0beb8 421 dj_hiddev->vendor = djrcv_hdev->vendor;
4fcad95a
BT
422 dj_hiddev->product = (workitem->quad_id_msb << 8) |
423 workitem->quad_id_lsb;
534a7b8e 424 snprintf(dj_hiddev->name, sizeof(dj_hiddev->name),
d610274b
BT
425 "Logitech Unifying Device. Wireless PID:%04x",
426 dj_hiddev->product);
427
428 dj_hiddev->group = HID_GROUP_LOGITECH_DJ_DEVICE;
534a7b8e 429
82c0beb8 430 memcpy(dj_hiddev->phys, djrcv_hdev->phys, sizeof(djrcv_hdev->phys));
4fcad95a 431 snprintf(tmpstr, sizeof(tmpstr), ":%d", device_index);
534a7b8e
NLC
432 strlcat(dj_hiddev->phys, tmpstr, sizeof(dj_hiddev->phys));
433
434 dj_dev = kzalloc(sizeof(struct dj_device), GFP_KERNEL);
435
436 if (!dj_dev) {
437 dev_err(&djrcv_hdev->dev, "%s: failed allocating dj_device\n",
438 __func__);
439 goto dj_device_allocate_fail;
440 }
441
4fcad95a 442 dj_dev->reports_supported = workitem->reports_supported;
534a7b8e
NLC
443 dj_dev->hdev = dj_hiddev;
444 dj_dev->dj_receiver_dev = djrcv_dev;
4fcad95a 445 dj_dev->device_index = device_index;
534a7b8e
NLC
446 dj_hiddev->driver_data = dj_dev;
447
f41d766c 448 spin_lock_irqsave(&djrcv_dev->lock, flags);
4fcad95a 449 djrcv_dev->paired_dj_devices[device_index] = dj_dev;
f41d766c 450 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
534a7b8e
NLC
451
452 if (hid_add_device(dj_hiddev)) {
453 dev_err(&djrcv_hdev->dev, "%s: failed adding dj_device\n",
454 __func__);
455 goto hid_add_device_fail;
456 }
457
458 return;
459
460hid_add_device_fail:
f41d766c 461 spin_lock_irqsave(&djrcv_dev->lock, flags);
4fcad95a 462 djrcv_dev->paired_dj_devices[device_index] = NULL;
f41d766c 463 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
534a7b8e
NLC
464 kfree(dj_dev);
465dj_device_allocate_fail:
466 hid_destroy_device(dj_hiddev);
467}
468
469static void delayedwork_callback(struct work_struct *work)
470{
471 struct dj_receiver_dev *djrcv_dev =
472 container_of(work, struct dj_receiver_dev, work);
473
4fcad95a 474 struct dj_workitem workitem;
534a7b8e
NLC
475 unsigned long flags;
476 int count;
c63e0e37 477 int retval;
534a7b8e
NLC
478
479 dbg_hid("%s\n", __func__);
480
481 spin_lock_irqsave(&djrcv_dev->lock, flags);
482
4fcad95a 483 count = kfifo_out(&djrcv_dev->notif_fifo, &workitem, sizeof(workitem));
534a7b8e 484
4fcad95a 485 if (count != sizeof(workitem)) {
534a7b8e
NLC
486 dev_err(&djrcv_dev->hdev->dev, "%s: workitem triggered without "
487 "notifications available\n", __func__);
488 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
489 return;
490 }
491
492 if (!kfifo_is_empty(&djrcv_dev->notif_fifo)) {
493 if (schedule_work(&djrcv_dev->work) == 0) {
494 dbg_hid("%s: did not schedule the work item, was "
495 "already queued\n", __func__);
496 }
497 }
498
499 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
500
4fcad95a
BT
501 switch (workitem.type) {
502 case WORKITEM_TYPE_PAIRED:
503 logi_dj_recv_add_djhid_device(djrcv_dev, &workitem);
534a7b8e 504 break;
4fcad95a
BT
505 case WORKITEM_TYPE_UNPAIRED:
506 logi_dj_recv_destroy_djhid_device(djrcv_dev, &workitem);
507 break;
508 case WORKITEM_TYPE_UNKNOWN:
509 retval = logi_dj_recv_query_paired_devices(djrcv_dev);
510 if (retval) {
511 dev_err(&djrcv_dev->hdev->dev,
512 "%s: logi_dj_recv_query_paired_devices error: %d\n",
513 __func__, retval);
514 }
515 break;
516 case WORKITEM_TYPE_EMPTY:
517 dbg_hid("%s: device list is empty\n", __func__);
518 break;
519 }
520}
521
522static void logi_dj_recv_queue_notification(struct dj_receiver_dev *djrcv_dev,
523 struct dj_report *dj_report)
524{
525 /* We are called from atomic context (tasklet && djrcv->lock held) */
526 struct dj_workitem workitem = {
527 .device_index = dj_report->device_index,
528 };
529
530 switch (dj_report->report_type) {
531 case REPORT_TYPE_NOTIF_DEVICE_PAIRED:
532 workitem.type = WORKITEM_TYPE_PAIRED;
533 if (dj_report->report_params[DEVICE_PAIRED_PARAM_SPFUNCTION] &
534 SPFUNCTION_DEVICE_LIST_EMPTY) {
535 workitem.type = WORKITEM_TYPE_EMPTY;
536 break;
537 }
538 /* fall-through */
534a7b8e 539 case REPORT_TYPE_NOTIF_DEVICE_UNPAIRED:
4fcad95a
BT
540 workitem.quad_id_msb =
541 dj_report->report_params[DEVICE_PAIRED_PARAM_EQUAD_ID_MSB];
542 workitem.quad_id_lsb =
543 dj_report->report_params[DEVICE_PAIRED_PARAM_EQUAD_ID_LSB];
544 workitem.reports_supported = get_unaligned_le32(
545 dj_report->report_params +
546 DEVICE_PAIRED_RF_REPORT_TYPE);
547 if (dj_report->report_type == REPORT_TYPE_NOTIF_DEVICE_UNPAIRED)
548 workitem.type = WORKITEM_TYPE_UNPAIRED;
534a7b8e
NLC
549 break;
550 default:
c63e0e37
NLC
551 /* A normal report (i. e. not belonging to a pair/unpair notification)
552 * arriving here, means that the report arrived but we did not have a
553 * paired dj_device associated to the report's device_index, this
554 * means that the original "device paired" notification corresponding
555 * to this dj_device never arrived to this driver. The reason is that
556 * hid-core discards all packets coming from a device while probe() is
557 * executing. */
4fcad95a
BT
558 if (!djrcv_dev->paired_dj_devices[dj_report->device_index]) {
559 /*
560 * ok, we don't know the device, just re-ask the
561 * receiver for the list of connected devices in
562 * the delayed work callback.
563 */
564 workitem.type = WORKITEM_TYPE_UNKNOWN;
c63e0e37 565 }
534a7b8e 566 }
534a7b8e 567
4fcad95a 568 kfifo_in(&djrcv_dev->notif_fifo, &workitem, sizeof(workitem));
534a7b8e
NLC
569
570 if (schedule_work(&djrcv_dev->work) == 0) {
571 dbg_hid("%s: did not schedule the work item, was already "
572 "queued\n", __func__);
573 }
574}
575
576static void logi_dj_recv_forward_null_report(struct dj_receiver_dev *djrcv_dev,
577 struct dj_report *dj_report)
578{
579 /* We are called from atomic context (tasklet && djrcv->lock held) */
580 unsigned int i;
581 u8 reportbuffer[MAX_REPORT_SIZE];
582 struct dj_device *djdev;
583
584 djdev = djrcv_dev->paired_dj_devices[dj_report->device_index];
585
534a7b8e
NLC
586 memset(reportbuffer, 0, sizeof(reportbuffer));
587
588 for (i = 0; i < NUMBER_OF_HID_REPORTS; i++) {
589 if (djdev->reports_supported & (1 << i)) {
590 reportbuffer[0] = i;
591 if (hid_input_report(djdev->hdev,
592 HID_INPUT_REPORT,
593 reportbuffer,
594 hid_reportid_size_map[i], 1)) {
595 dbg_hid("hid_input_report error sending null "
596 "report\n");
597 }
598 }
599 }
600}
601
83898234
BT
602static void logi_dj_recv_forward_dj(struct dj_receiver_dev *djrcv_dev,
603 struct dj_report *dj_report)
534a7b8e
NLC
604{
605 /* We are called from atomic context (tasklet && djrcv->lock held) */
606 struct dj_device *dj_device;
607
608 dj_device = djrcv_dev->paired_dj_devices[dj_report->device_index];
609
534a7b8e
NLC
610 if ((dj_report->report_type > ARRAY_SIZE(hid_reportid_size_map) - 1) ||
611 (hid_reportid_size_map[dj_report->report_type] == 0)) {
612 dbg_hid("invalid report type:%x\n", dj_report->report_type);
613 return;
614 }
615
616 if (hid_input_report(dj_device->hdev,
617 HID_INPUT_REPORT, &dj_report->report_type,
618 hid_reportid_size_map[dj_report->report_type], 1)) {
619 dbg_hid("hid_input_report error\n");
620 }
621}
622
83898234
BT
623static void logi_dj_recv_forward_report(struct dj_device *dj_dev, u8 *data,
624 int size)
925f0f3e
BT
625{
626 /* We are called from atomic context (tasklet && djrcv->lock held) */
627 if (hid_input_report(dj_dev->hdev, HID_INPUT_REPORT, data, size, 1))
628 dbg_hid("hid_input_report error\n");
629}
534a7b8e
NLC
630
631static int logi_dj_recv_send_report(struct dj_receiver_dev *djrcv_dev,
632 struct dj_report *dj_report)
633{
634 struct hid_device *hdev = djrcv_dev->hdev;
dcd9006b
BT
635 struct hid_report *report;
636 struct hid_report_enum *output_report_enum;
637 u8 *data = (u8 *)(&dj_report->device_index);
297502ab 638 unsigned int i;
534a7b8e 639
dcd9006b
BT
640 output_report_enum = &hdev->report_enum[HID_OUTPUT_REPORT];
641 report = output_report_enum->report_id_hash[REPORT_ID_DJ_SHORT];
642
643 if (!report) {
644 dev_err(&hdev->dev, "%s: unable to find dj report\n", __func__);
534a7b8e
NLC
645 return -ENODEV;
646 }
647
297502ab 648 for (i = 0; i < DJREPORT_SHORT_LENGTH - 1; i++)
dcd9006b
BT
649 report->field[0]->value[i] = data[i];
650
83a44ac8 651 hid_hw_request(hdev, report, HID_REQ_SET_REPORT);
534a7b8e 652
dcd9006b 653 return 0;
534a7b8e
NLC
654}
655
656static int logi_dj_recv_query_paired_devices(struct dj_receiver_dev *djrcv_dev)
657{
d8dc3494
MD
658 struct dj_report *dj_report;
659 int retval;
534a7b8e 660
8a55ade7 661 dj_report = kzalloc(sizeof(struct dj_report), GFP_KERNEL);
d8dc3494
MD
662 if (!dj_report)
663 return -ENOMEM;
664 dj_report->report_id = REPORT_ID_DJ_SHORT;
665 dj_report->device_index = 0xFF;
666 dj_report->report_type = REPORT_TYPE_CMD_GET_PAIRED_DEVICES;
667 retval = logi_dj_recv_send_report(djrcv_dev, dj_report);
668 kfree(dj_report);
669 return retval;
534a7b8e
NLC
670}
671
c63e0e37 672
534a7b8e
NLC
673static int logi_dj_recv_switch_to_dj_mode(struct dj_receiver_dev *djrcv_dev,
674 unsigned timeout)
675{
6a9ddc89 676 struct hid_device *hdev = djrcv_dev->hdev;
d8dc3494 677 struct dj_report *dj_report;
6a9ddc89 678 u8 *buf;
d8dc3494 679 int retval;
534a7b8e 680
8a55ade7 681 dj_report = kzalloc(sizeof(struct dj_report), GFP_KERNEL);
d8dc3494
MD
682 if (!dj_report)
683 return -ENOMEM;
684 dj_report->report_id = REPORT_ID_DJ_SHORT;
685 dj_report->device_index = 0xFF;
686 dj_report->report_type = REPORT_TYPE_CMD_SWITCH;
687 dj_report->report_params[CMD_SWITCH_PARAM_DEVBITFIELD] = 0x3F;
688 dj_report->report_params[CMD_SWITCH_PARAM_TIMEOUT_SECONDS] = (u8)timeout;
689 retval = logi_dj_recv_send_report(djrcv_dev, dj_report);
42c22dbf
BT
690
691 /*
692 * Ugly sleep to work around a USB 3.0 bug when the receiver is still
693 * processing the "switch-to-dj" command while we send an other command.
694 * 50 msec should gives enough time to the receiver to be ready.
695 */
696 msleep(50);
697
6a9ddc89
BT
698 /*
699 * Magical bits to set up hidpp notifications when the dj devices
700 * are connected/disconnected.
701 *
702 * We can reuse dj_report because HIDPP_REPORT_SHORT_LENGTH is smaller
703 * than DJREPORT_SHORT_LENGTH.
704 */
705 buf = (u8 *)dj_report;
706
707 memset(buf, 0, HIDPP_REPORT_SHORT_LENGTH);
708
709 buf[0] = REPORT_ID_HIDPP_SHORT;
710 buf[1] = 0xFF;
711 buf[2] = 0x80;
712 buf[3] = 0x00;
713 buf[4] = 0x00;
714 buf[5] = 0x09;
715 buf[6] = 0x00;
716
717 hid_hw_raw_request(hdev, REPORT_ID_HIDPP_SHORT, buf,
718 HIDPP_REPORT_SHORT_LENGTH, HID_OUTPUT_REPORT,
719 HID_REQ_SET_REPORT);
720
721 kfree(dj_report);
d8dc3494 722 return retval;
534a7b8e
NLC
723}
724
725
726static int logi_dj_ll_open(struct hid_device *hid)
727{
728 dbg_hid("%s:%s\n", __func__, hid->phys);
729 return 0;
730
731}
732
733static void logi_dj_ll_close(struct hid_device *hid)
734{
735 dbg_hid("%s:%s\n", __func__, hid->phys);
736}
737
8dba3026
BT
738/*
739 * Register 0xB5 is "pairing information". It is solely intended for the
740 * receiver, so do not overwrite the device index.
741 */
c0340412
BT
742static u8 unifying_pairing_query[] = { REPORT_ID_HIDPP_SHORT,
743 HIDPP_RECEIVER_INDEX,
744 HIDPP_GET_LONG_REGISTER,
745 HIDPP_REG_PAIRING_INFORMATION };
746static u8 unifying_pairing_answer[] = { REPORT_ID_HIDPP_LONG,
747 HIDPP_RECEIVER_INDEX,
748 HIDPP_GET_LONG_REGISTER,
749 HIDPP_REG_PAIRING_INFORMATION };
33797820 750
bd27e202
BT
751static int logi_dj_ll_raw_request(struct hid_device *hid,
752 unsigned char reportnum, __u8 *buf,
753 size_t count, unsigned char report_type,
754 int reqtype)
534a7b8e 755{
0e40d356
BT
756 struct dj_device *djdev = hid->driver_data;
757 struct dj_receiver_dev *djrcv_dev = djdev->dj_receiver_dev;
758 u8 *out_buf;
759 int ret;
534a7b8e 760
925f0f3e
BT
761 if ((buf[0] == REPORT_ID_HIDPP_SHORT) ||
762 (buf[0] == REPORT_ID_HIDPP_LONG)) {
763 if (count < 2)
764 return -EINVAL;
765
33797820
BT
766 /* special case where we should not overwrite
767 * the device_index */
8dba3026
BT
768 if (count == 7 && !memcmp(buf, unifying_pairing_query,
769 sizeof(unifying_pairing_query)))
770 buf[4] = (buf[4] & 0xf0) | (djdev->device_index - 1);
33797820
BT
771 else
772 buf[1] = djdev->device_index;
925f0f3e
BT
773 return hid_hw_raw_request(djrcv_dev->hdev, reportnum, buf,
774 count, report_type, reqtype);
775 }
776
0e40d356
BT
777 if (buf[0] != REPORT_TYPE_LEDS)
778 return -EINVAL;
779
780 out_buf = kzalloc(DJREPORT_SHORT_LENGTH, GFP_ATOMIC);
781 if (!out_buf)
782 return -ENOMEM;
783
51217e69 784 if (count > DJREPORT_SHORT_LENGTH - 2)
0e40d356
BT
785 count = DJREPORT_SHORT_LENGTH - 2;
786
787 out_buf[0] = REPORT_ID_DJ_SHORT;
788 out_buf[1] = djdev->device_index;
789 memcpy(out_buf + 2, buf, count);
790
0e40d356 791 ret = hid_hw_raw_request(djrcv_dev->hdev, out_buf[0], out_buf,
bd27e202 792 DJREPORT_SHORT_LENGTH, report_type, reqtype);
0e40d356
BT
793
794 kfree(out_buf);
795 return ret;
534a7b8e
NLC
796}
797
6d603326 798static void rdcat(char *rdesc, unsigned int *rsize, const char *data, unsigned int size)
2a039bf5 799{
6d603326 800 memcpy(rdesc + *rsize, data, size);
2a039bf5
HR
801 *rsize += size;
802}
803
534a7b8e
NLC
804static int logi_dj_ll_parse(struct hid_device *hid)
805{
806 struct dj_device *djdev = hid->driver_data;
2a039bf5
HR
807 unsigned int rsize = 0;
808 char *rdesc;
534a7b8e
NLC
809 int retval;
810
811 dbg_hid("%s\n", __func__);
812
813 djdev->hdev->version = 0x0111;
814 djdev->hdev->country = 0x00;
815
2a039bf5
HR
816 rdesc = kmalloc(MAX_RDESC_SIZE, GFP_KERNEL);
817 if (!rdesc)
818 return -ENOMEM;
819
534a7b8e
NLC
820 if (djdev->reports_supported & STD_KEYBOARD) {
821 dbg_hid("%s: sending a kbd descriptor, reports_supported: %x\n",
822 __func__, djdev->reports_supported);
6d603326 823 rdcat(rdesc, &rsize, kbd_descriptor, sizeof(kbd_descriptor));
534a7b8e
NLC
824 }
825
826 if (djdev->reports_supported & STD_MOUSE) {
827 dbg_hid("%s: sending a mouse descriptor, reports_supported: "
828 "%x\n", __func__, djdev->reports_supported);
6d603326 829 rdcat(rdesc, &rsize, mse_descriptor, sizeof(mse_descriptor));
534a7b8e
NLC
830 }
831
832 if (djdev->reports_supported & MULTIMEDIA) {
833 dbg_hid("%s: sending a multimedia report descriptor: %x\n",
834 __func__, djdev->reports_supported);
6d603326 835 rdcat(rdesc, &rsize, consumer_descriptor, sizeof(consumer_descriptor));
534a7b8e
NLC
836 }
837
838 if (djdev->reports_supported & POWER_KEYS) {
839 dbg_hid("%s: sending a power keys report descriptor: %x\n",
840 __func__, djdev->reports_supported);
6d603326 841 rdcat(rdesc, &rsize, syscontrol_descriptor, sizeof(syscontrol_descriptor));
534a7b8e
NLC
842 }
843
844 if (djdev->reports_supported & MEDIA_CENTER) {
845 dbg_hid("%s: sending a media center report descriptor: %x\n",
846 __func__, djdev->reports_supported);
6d603326 847 rdcat(rdesc, &rsize, media_descriptor, sizeof(media_descriptor));
534a7b8e
NLC
848 }
849
850 if (djdev->reports_supported & KBD_LEDS) {
851 dbg_hid("%s: need to send kbd leds report descriptor: %x\n",
852 __func__, djdev->reports_supported);
853 }
854
925f0f3e
BT
855 rdcat(rdesc, &rsize, hidpp_descriptor, sizeof(hidpp_descriptor));
856
2a039bf5
HR
857 retval = hid_parse_report(hid, rdesc, rsize);
858 kfree(rdesc);
859
860 return retval;
534a7b8e
NLC
861}
862
534a7b8e
NLC
863static int logi_dj_ll_start(struct hid_device *hid)
864{
865 dbg_hid("%s\n", __func__);
866 return 0;
867}
868
869static void logi_dj_ll_stop(struct hid_device *hid)
870{
871 dbg_hid("%s\n", __func__);
872}
873
874
875static struct hid_ll_driver logi_dj_ll_driver = {
876 .parse = logi_dj_ll_parse,
877 .start = logi_dj_ll_start,
878 .stop = logi_dj_ll_stop,
879 .open = logi_dj_ll_open,
880 .close = logi_dj_ll_close,
bd27e202 881 .raw_request = logi_dj_ll_raw_request,
534a7b8e
NLC
882};
883
925f0f3e 884static int logi_dj_dj_event(struct hid_device *hdev,
534a7b8e
NLC
885 struct hid_report *report, u8 *data,
886 int size)
887{
888 struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
889 struct dj_report *dj_report = (struct dj_report *) data;
890 unsigned long flags;
534a7b8e 891
925f0f3e
BT
892 /*
893 * Here we receive all data coming from iface 2, there are 3 cases:
534a7b8e 894 *
925f0f3e
BT
895 * 1) Data is intended for this driver i. e. data contains arrival,
896 * departure, etc notifications, in which case we queue them for delayed
897 * processing by the work queue. We return 1 to hid-core as no further
898 * processing is required from it.
534a7b8e 899 *
925f0f3e
BT
900 * 2) Data informs a connection change, if the change means rf link
901 * loss, then we must send a null report to the upper layer to discard
902 * potentially pressed keys that may be repeated forever by the input
903 * layer. Return 1 to hid-core as no further processing is required.
534a7b8e 904 *
925f0f3e
BT
905 * 3) Data is an actual input event from a paired DJ device in which
906 * case we forward it to the correct hid device (via hid_input_report()
907 * ) and return 1 so hid-core does not anything else with it.
534a7b8e 908 */
5abfe85c 909
ad3e14d7
JK
910 if ((dj_report->device_index < DJ_DEVICE_INDEX_MIN) ||
911 (dj_report->device_index > DJ_DEVICE_INDEX_MAX)) {
5abfe85c
BT
912 /*
913 * Device index is wrong, bail out.
914 * This driver can ignore safely the receiver notifications,
915 * so ignore those reports too.
916 */
917 if (dj_report->device_index != DJ_RECEIVER_INDEX)
918 dev_err(&hdev->dev, "%s: invalid device index:%d\n",
ad3e14d7
JK
919 __func__, dj_report->device_index);
920 return false;
921 }
534a7b8e
NLC
922
923 spin_lock_irqsave(&djrcv_dev->lock, flags);
368d4e59
BT
924
925 if (!djrcv_dev->paired_dj_devices[dj_report->device_index]) {
926 /* received an event for an unknown device, bail out */
927 logi_dj_recv_queue_notification(djrcv_dev, dj_report);
928 goto out;
929 }
930
5abfe85c
BT
931 switch (dj_report->report_type) {
932 case REPORT_TYPE_NOTIF_DEVICE_PAIRED:
368d4e59
BT
933 /* pairing notifications are handled above the switch */
934 break;
5abfe85c
BT
935 case REPORT_TYPE_NOTIF_DEVICE_UNPAIRED:
936 logi_dj_recv_queue_notification(djrcv_dev, dj_report);
937 break;
938 case REPORT_TYPE_NOTIF_CONNECTION_STATUS:
939 if (dj_report->report_params[CONNECTION_STATUS_PARAM_STATUS] ==
940 STATUS_LINKLOSS) {
941 logi_dj_recv_forward_null_report(djrcv_dev, dj_report);
534a7b8e 942 }
5abfe85c
BT
943 break;
944 default:
83898234 945 logi_dj_recv_forward_dj(djrcv_dev, dj_report);
534a7b8e 946 }
368d4e59
BT
947
948out:
534a7b8e
NLC
949 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
950
5abfe85c 951 return true;
534a7b8e
NLC
952}
953
925f0f3e
BT
954static int logi_dj_hidpp_event(struct hid_device *hdev,
955 struct hid_report *report, u8 *data,
956 int size)
957{
958 struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
7bb56a5f 959 struct hidpp_event *hidpp_report = (struct hidpp_event *) data;
925f0f3e 960 unsigned long flags;
7bb56a5f 961 u8 device_index = hidpp_report->device_index;
925f0f3e 962
33797820
BT
963 if (device_index == HIDPP_RECEIVER_INDEX) {
964 /* special case were the device wants to know its unifying
965 * name */
966 if (size == HIDPP_REPORT_LONG_LENGTH &&
8dba3026
BT
967 !memcmp(data, unifying_pairing_answer,
968 sizeof(unifying_pairing_answer)))
33797820
BT
969 device_index = (data[4] & 0x0F) + 1;
970 else
971 return false;
972 }
925f0f3e
BT
973
974 /*
975 * Data is from the HID++ collection, in this case, we forward the
976 * data to the corresponding child dj device and return 0 to hid-core
977 * so he data also goes to the hidraw device of the receiver. This
978 * allows a user space application to implement the full HID++ routing
979 * via the receiver.
980 */
981
982 if ((device_index < DJ_DEVICE_INDEX_MIN) ||
983 (device_index > DJ_DEVICE_INDEX_MAX)) {
984 /*
985 * Device index is wrong, bail out.
986 * This driver can ignore safely the receiver notifications,
987 * so ignore those reports too.
988 */
989 dev_err(&hdev->dev, "%s: invalid device index:%d\n",
7bb56a5f 990 __func__, hidpp_report->device_index);
925f0f3e
BT
991 return false;
992 }
993
994 spin_lock_irqsave(&djrcv_dev->lock, flags);
995
996 if (!djrcv_dev->paired_dj_devices[device_index])
997 /* received an event for an unknown device, bail out */
998 goto out;
999
83898234
BT
1000 logi_dj_recv_forward_report(djrcv_dev->paired_dj_devices[device_index],
1001 data, size);
925f0f3e
BT
1002
1003out:
1004 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
1005
1006 return false;
1007}
1008
1009static int logi_dj_raw_event(struct hid_device *hdev,
1010 struct hid_report *report, u8 *data,
1011 int size)
1012{
1013 dbg_hid("%s, size:%d\n", __func__, size);
1014
1015 switch (data[0]) {
1016 case REPORT_ID_DJ_SHORT:
f254ae93
PW
1017 if (size != DJREPORT_SHORT_LENGTH) {
1018 dev_err(&hdev->dev, "DJ report of bad size (%d)", size);
1019 return false;
1020 }
925f0f3e
BT
1021 return logi_dj_dj_event(hdev, report, data, size);
1022 case REPORT_ID_HIDPP_SHORT:
f254ae93
PW
1023 if (size != HIDPP_REPORT_SHORT_LENGTH) {
1024 dev_err(&hdev->dev,
1025 "Short HID++ report of bad size (%d)", size);
1026 return false;
1027 }
1028 return logi_dj_hidpp_event(hdev, report, data, size);
925f0f3e 1029 case REPORT_ID_HIDPP_LONG:
f254ae93
PW
1030 if (size != HIDPP_REPORT_LONG_LENGTH) {
1031 dev_err(&hdev->dev,
1032 "Long HID++ report of bad size (%d)", size);
1033 return false;
1034 }
925f0f3e
BT
1035 return logi_dj_hidpp_event(hdev, report, data, size);
1036 }
1037
1038 return false;
1039}
1040
534a7b8e
NLC
1041static int logi_dj_probe(struct hid_device *hdev,
1042 const struct hid_device_id *id)
1043{
82c0beb8
BT
1044 struct hid_report_enum *rep_enum;
1045 struct hid_report *rep;
534a7b8e 1046 struct dj_receiver_dev *djrcv_dev;
82c0beb8 1047 bool has_hidpp = false;
534a7b8e
NLC
1048 int retval;
1049
82c0beb8
BT
1050 /*
1051 * Call to usbhid to fetch the HID descriptors of the current
1052 * interface subsequently call to the hid/hid-core to parse the
1053 * fetched descriptors.
1054 */
1055 retval = hid_parse(hdev);
1056 if (retval) {
1057 dev_err(&hdev->dev,
1058 "%s:parse failed\n", __func__);
1059 return retval;
1060 }
534a7b8e 1061
82c0beb8
BT
1062 rep_enum = &hdev->report_enum[HID_INPUT_REPORT];
1063
1064 /*
1065 * Check for the HID++ application.
1066 * Note: we should theoretically check for HID++ and DJ
1067 * collections, but this will do.
1068 */
1069 list_for_each_entry(rep, &rep_enum->report_list, list) {
1070 if (rep->application == 0xff000001)
1071 has_hidpp = true;
534a7b8e
NLC
1072 }
1073
82c0beb8
BT
1074 /*
1075 * Ignore interfaces without DJ/HID++ collection, they will not carry
1076 * any data, dont create any hid_device for them.
1077 */
1078 if (!has_hidpp)
1079 return -ENODEV;
1080
1081 /* Treat DJ/HID++ interface */
534a7b8e
NLC
1082
1083 djrcv_dev = kzalloc(sizeof(struct dj_receiver_dev), GFP_KERNEL);
1084 if (!djrcv_dev) {
1085 dev_err(&hdev->dev,
1086 "%s:failed allocating dj_receiver_dev\n", __func__);
1087 return -ENOMEM;
1088 }
1089 djrcv_dev->hdev = hdev;
1090 INIT_WORK(&djrcv_dev->work, delayedwork_callback);
1091 spin_lock_init(&djrcv_dev->lock);
1092 if (kfifo_alloc(&djrcv_dev->notif_fifo,
4fcad95a 1093 DJ_MAX_NUMBER_NOTIFS * sizeof(struct dj_workitem),
534a7b8e
NLC
1094 GFP_KERNEL)) {
1095 dev_err(&hdev->dev,
1096 "%s:failed allocating notif_fifo\n", __func__);
1097 kfree(djrcv_dev);
1098 return -ENOMEM;
1099 }
1100 hid_set_drvdata(hdev, djrcv_dev);
1101
297502ab 1102
534a7b8e
NLC
1103 /* Starts the usb device and connects to upper interfaces hiddev and
1104 * hidraw */
1105 retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
1106 if (retval) {
1107 dev_err(&hdev->dev,
1108 "%s:hid_hw_start returned error\n", __func__);
1109 goto hid_hw_start_fail;
1110 }
1111
1112 retval = logi_dj_recv_switch_to_dj_mode(djrcv_dev, 0);
1113 if (retval < 0) {
1114 dev_err(&hdev->dev,
1115 "%s:logi_dj_recv_switch_to_dj_mode returned error:%d\n",
1116 __func__, retval);
1117 goto switch_to_dj_mode_fail;
1118 }
1119
1120 /* This is enabling the polling urb on the IN endpoint */
ddf7540e 1121 retval = hid_hw_open(hdev);
534a7b8e 1122 if (retval < 0) {
ddf7540e
BT
1123 dev_err(&hdev->dev, "%s:hid_hw_open returned error:%d\n",
1124 __func__, retval);
534a7b8e
NLC
1125 goto llopen_failed;
1126 }
1127
a9dd22b7
AR
1128 /* Allow incoming packets to arrive: */
1129 hid_device_io_start(hdev);
1130
534a7b8e
NLC
1131 retval = logi_dj_recv_query_paired_devices(djrcv_dev);
1132 if (retval < 0) {
1133 dev_err(&hdev->dev, "%s:logi_dj_recv_query_paired_devices "
1134 "error:%d\n", __func__, retval);
1135 goto logi_dj_recv_query_paired_devices_failed;
1136 }
1137
1138 return retval;
1139
1140logi_dj_recv_query_paired_devices_failed:
ddf7540e 1141 hid_hw_close(hdev);
534a7b8e
NLC
1142
1143llopen_failed:
1144switch_to_dj_mode_fail:
1145 hid_hw_stop(hdev);
1146
1147hid_hw_start_fail:
534a7b8e
NLC
1148 kfifo_free(&djrcv_dev->notif_fifo);
1149 kfree(djrcv_dev);
534a7b8e
NLC
1150 return retval;
1151
1152}
1153
1154#ifdef CONFIG_PM
1155static int logi_dj_reset_resume(struct hid_device *hdev)
1156{
1157 int retval;
1158 struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
1159
1160 retval = logi_dj_recv_switch_to_dj_mode(djrcv_dev, 0);
1161 if (retval < 0) {
1162 dev_err(&hdev->dev,
1163 "%s:logi_dj_recv_switch_to_dj_mode returned error:%d\n",
1164 __func__, retval);
1165 }
1166
1167 return 0;
1168}
1169#endif
1170
1171static void logi_dj_remove(struct hid_device *hdev)
1172{
1173 struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
1174 struct dj_device *dj_dev;
1175 int i;
1176
1177 dbg_hid("%s\n", __func__);
1178
1179 cancel_work_sync(&djrcv_dev->work);
1180
ddf7540e 1181 hid_hw_close(hdev);
534a7b8e
NLC
1182 hid_hw_stop(hdev);
1183
1184 /* I suppose that at this point the only context that can access
1185 * the djrecv_data is this thread as the work item is guaranteed to
1186 * have finished and no more raw_event callbacks should arrive after
1187 * the remove callback was triggered so no locks are put around the
1188 * code below */
844580ff 1189 for (i = 0; i < (DJ_MAX_PAIRED_DEVICES + DJ_DEVICE_INDEX_MIN); i++) {
534a7b8e
NLC
1190 dj_dev = djrcv_dev->paired_dj_devices[i];
1191 if (dj_dev != NULL) {
1192 hid_destroy_device(dj_dev->hdev);
1193 kfree(dj_dev);
1194 djrcv_dev->paired_dj_devices[i] = NULL;
1195 }
1196 }
1197
1198 kfifo_free(&djrcv_dev->notif_fifo);
1199 kfree(djrcv_dev);
534a7b8e
NLC
1200}
1201
534a7b8e
NLC
1202static const struct hid_device_id logi_dj_receivers[] = {
1203 {HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
1204 USB_DEVICE_ID_LOGITECH_UNIFYING_RECEIVER)},
1205 {HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
1206 USB_DEVICE_ID_LOGITECH_UNIFYING_RECEIVER_2)},
1207 {}
1208};
1209
1210MODULE_DEVICE_TABLE(hid, logi_dj_receivers);
1211
1212static struct hid_driver logi_djreceiver_driver = {
1213 .name = "logitech-djreceiver",
1214 .id_table = logi_dj_receivers,
1215 .probe = logi_dj_probe,
1216 .remove = logi_dj_remove,
1217 .raw_event = logi_dj_raw_event,
1218#ifdef CONFIG_PM
1219 .reset_resume = logi_dj_reset_resume,
1220#endif
1221};
1222
ab94e562 1223module_hid_driver(logi_djreceiver_driver);
534a7b8e 1224
534a7b8e
NLC
1225MODULE_LICENSE("GPL");
1226MODULE_AUTHOR("Logitech");
1227MODULE_AUTHOR("Nestor Lopez Casado");
1228MODULE_AUTHOR("nlopezcasad@logitech.com");