blob: 6244fd41b153dcf69468bb4085255c36f44f4eb2 [file] [log] [blame]
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001/*
2 * HID driver for Logitech Unifying receivers
3 *
4 * Copyright (c) 2011 Logitech
5 */
6
7/*
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 *
22 */
23
24
25#include <linux/device.h>
26#include <linux/hid.h>
27#include <linux/module.h>
Benjamin Tissoires8cb37462014-09-30 13:18:26 -040028#include <linux/kfifo.h>
Benjamin Tissoires82c0beb2019-04-20 13:21:47 +020029#include <linux/delay.h>
Jonathan Nieder44d27f72012-05-11 16:17:16 +020030#include <asm/unaligned.h>
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +020031#include "hid-ids.h"
Benjamin Tissoires8cb37462014-09-30 13:18:26 -040032
33#define DJ_MAX_PAIRED_DEVICES 6
Benjamin Tissoires4fcad952019-04-20 13:21:48 +020034#define DJ_MAX_NUMBER_NOTIFS 8
Benjamin Tissoires8cb37462014-09-30 13:18:26 -040035#define DJ_RECEIVER_INDEX 0
36#define DJ_DEVICE_INDEX_MIN 1
37#define DJ_DEVICE_INDEX_MAX 6
38
39#define DJREPORT_SHORT_LENGTH 15
40#define DJREPORT_LONG_LENGTH 32
41
42#define REPORT_ID_DJ_SHORT 0x20
43#define REPORT_ID_DJ_LONG 0x21
44
Benjamin Tissoires925f0f32014-09-30 13:18:29 -040045#define REPORT_ID_HIDPP_SHORT 0x10
46#define REPORT_ID_HIDPP_LONG 0x11
47
48#define HIDPP_REPORT_SHORT_LENGTH 7
49#define HIDPP_REPORT_LONG_LENGTH 20
50
51#define HIDPP_RECEIVER_INDEX 0xff
52
53#define REPORT_TYPE_RFREPORT_FIRST 0x01
Benjamin Tissoires8cb37462014-09-30 13:18:26 -040054#define REPORT_TYPE_RFREPORT_LAST 0x1F
55
56/* Command Switch to DJ mode */
57#define REPORT_TYPE_CMD_SWITCH 0x80
58#define CMD_SWITCH_PARAM_DEVBITFIELD 0x00
59#define CMD_SWITCH_PARAM_TIMEOUT_SECONDS 0x01
60#define TIMEOUT_NO_KEEPALIVE 0x00
61
62/* Command to Get the list of Paired devices */
63#define REPORT_TYPE_CMD_GET_PAIRED_DEVICES 0x81
64
65/* Device Paired Notification */
66#define REPORT_TYPE_NOTIF_DEVICE_PAIRED 0x41
67#define SPFUNCTION_MORE_NOTIF_EXPECTED 0x01
68#define SPFUNCTION_DEVICE_LIST_EMPTY 0x02
69#define DEVICE_PAIRED_PARAM_SPFUNCTION 0x00
70#define DEVICE_PAIRED_PARAM_EQUAD_ID_LSB 0x01
71#define DEVICE_PAIRED_PARAM_EQUAD_ID_MSB 0x02
72#define DEVICE_PAIRED_RF_REPORT_TYPE 0x03
73
74/* Device Un-Paired Notification */
75#define REPORT_TYPE_NOTIF_DEVICE_UNPAIRED 0x40
76
Benjamin Tissoires8cb37462014-09-30 13:18:26 -040077/* Connection Status Notification */
78#define REPORT_TYPE_NOTIF_CONNECTION_STATUS 0x42
79#define CONNECTION_STATUS_PARAM_STATUS 0x00
80#define STATUS_LINKLOSS 0x01
81
82/* Error Notification */
83#define REPORT_TYPE_NOTIF_ERROR 0x7F
84#define NOTIF_ERROR_PARAM_ETYPE 0x00
85#define ETYPE_KEEPALIVE_TIMEOUT 0x01
86
87/* supported DJ HID && RF report types */
88#define REPORT_TYPE_KEYBOARD 0x01
89#define REPORT_TYPE_MOUSE 0x02
90#define REPORT_TYPE_CONSUMER_CONTROL 0x03
91#define REPORT_TYPE_SYSTEM_CONTROL 0x04
92#define REPORT_TYPE_MEDIA_CENTER 0x08
93#define REPORT_TYPE_LEDS 0x0E
94
95/* RF Report types bitfield */
Benjamin Tissoiresa17dd1f2019-04-20 13:21:45 +020096#define STD_KEYBOARD BIT(1)
97#define STD_MOUSE BIT(2)
98#define MULTIMEDIA BIT(3)
99#define POWER_KEYS BIT(4)
100#define MEDIA_CENTER BIT(8)
101#define KBD_LEDS BIT(14)
Benjamin Tissoires8cb37462014-09-30 13:18:26 -0400102
Benjamin Tissoiresc0340412019-04-20 13:21:46 +0200103#define HIDPP_GET_LONG_REGISTER 0x83
104#define HIDPP_REG_PAIRING_INFORMATION 0xB5
105#define HIDPP_PAIRING_INFORMATION 0x20
106
Benjamin Tissoires8cb37462014-09-30 13:18:26 -0400107struct dj_report {
108 u8 report_id;
109 u8 device_index;
110 u8 report_type;
111 u8 report_params[DJREPORT_SHORT_LENGTH - 3];
112};
113
Benjamin Tissoires7bb56a52019-04-20 13:21:44 +0200114struct 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
Benjamin Tissoires8cb37462014-09-30 13:18:26 -0400121struct dj_receiver_dev {
Hans de Goede0ee75542019-04-20 13:21:51 +0200122 struct hid_device *hidpp;
Benjamin Tissoires8cb37462014-09-30 13:18:26 -0400123 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;
Benjamin Tissoires8cb37462014-09-30 13:18:26 -0400128};
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};
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200136
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200137#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
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200150/* 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) */
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200164 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) */
Benjamin Tissoires0e40d352014-02-05 16:33:17 -0500172 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) */
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200184 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
Benjamin Tissoires925f0f32014-09-30 13:18:29 -0400276/* 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
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200327/* Maximum size of all defined hid reports in bytes (including report id) */
328#define MAX_REPORT_SIZE 8
329
Henrik Rydberg2a039bf2012-04-22 14:21:39 +0200330/* 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) + \
Benjamin Tissoires925f0f32014-09-30 13:18:29 -0400336 sizeof(media_descriptor) + \
337 sizeof(hidpp_descriptor))
Henrik Rydberg2a039bf2012-04-22 14:21:39 +0200338
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200339/* 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
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200363static struct hid_ll_driver logi_dj_ll_driver;
364
Nestor Lopez Casadoc63e0e32013-07-18 06:21:30 -0700365static int logi_dj_recv_query_paired_devices(struct dj_receiver_dev *djrcv_dev);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200366
367static void logi_dj_recv_destroy_djhid_device(struct dj_receiver_dev *djrcv_dev,
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200368 struct dj_workitem *workitem)
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200369{
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);
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200375 dj_dev = djrcv_dev->paired_dj_devices[workitem->device_index];
376 djrcv_dev->paired_dj_devices[workitem->device_index] = NULL;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200377 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 {
Hans de Goede0ee75542019-04-20 13:21:51 +0200383 hid_err(djrcv_dev->hidpp, "%s: can't destroy a NULL device\n",
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200384 __func__);
385 }
386}
387
388static void logi_dj_recv_add_djhid_device(struct dj_receiver_dev *djrcv_dev,
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200389 struct dj_workitem *workitem)
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200390{
391 /* Called in delayed work context */
Hans de Goede0ee75542019-04-20 13:21:51 +0200392 struct hid_device *djrcv_hdev = djrcv_dev->hidpp;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200393 struct hid_device *dj_hiddev;
394 struct dj_device *dj_dev;
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200395 u8 device_index = workitem->device_index;
Hans de Goedef41d7662019-04-20 13:21:50 +0200396 unsigned long flags;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200397
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
Hans de Goedef41d7662019-04-20 13:21:50 +0200403 /* We are the only one ever adding a device, no need to lock */
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200404 if (djrcv_dev->paired_dj_devices[device_index]) {
Nestor Lopez Casadoc63e0e32013-07-18 06:21:30 -0700405 /* The device is already known. No need to reallocate it. */
406 dbg_hid("%s: device is already known\n", __func__);
407 return;
408 }
409
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200410 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;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200418
419 dj_hiddev->dev.parent = &djrcv_hdev->dev;
420 dj_hiddev->bus = BUS_USB;
Benjamin Tissoires82c0beb2019-04-20 13:21:47 +0200421 dj_hiddev->vendor = djrcv_hdev->vendor;
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200422 dj_hiddev->product = (workitem->quad_id_msb << 8) |
423 workitem->quad_id_lsb;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200424 snprintf(dj_hiddev->name, sizeof(dj_hiddev->name),
Benjamin Tissoiresd6102742014-09-30 13:18:25 -0400425 "Logitech Unifying Device. Wireless PID:%04x",
426 dj_hiddev->product);
427
428 dj_hiddev->group = HID_GROUP_LOGITECH_DJ_DEVICE;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200429
Benjamin Tissoires82c0beb2019-04-20 13:21:47 +0200430 memcpy(dj_hiddev->phys, djrcv_hdev->phys, sizeof(djrcv_hdev->phys));
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200431 snprintf(tmpstr, sizeof(tmpstr), ":%d", device_index);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200432 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
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200442 dj_dev->reports_supported = workitem->reports_supported;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200443 dj_dev->hdev = dj_hiddev;
444 dj_dev->dj_receiver_dev = djrcv_dev;
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200445 dj_dev->device_index = device_index;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200446 dj_hiddev->driver_data = dj_dev;
447
Hans de Goedef41d7662019-04-20 13:21:50 +0200448 spin_lock_irqsave(&djrcv_dev->lock, flags);
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200449 djrcv_dev->paired_dj_devices[device_index] = dj_dev;
Hans de Goedef41d7662019-04-20 13:21:50 +0200450 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200451
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:
Hans de Goedef41d7662019-04-20 13:21:50 +0200461 spin_lock_irqsave(&djrcv_dev->lock, flags);
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200462 djrcv_dev->paired_dj_devices[device_index] = NULL;
Hans de Goedef41d7662019-04-20 13:21:50 +0200463 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200464 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
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200474 struct dj_workitem workitem;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200475 unsigned long flags;
476 int count;
Nestor Lopez Casadoc63e0e32013-07-18 06:21:30 -0700477 int retval;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200478
479 dbg_hid("%s\n", __func__);
480
481 spin_lock_irqsave(&djrcv_dev->lock, flags);
482
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200483 count = kfifo_out(&djrcv_dev->notif_fifo, &workitem, sizeof(workitem));
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200484
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200485 if (count != sizeof(workitem)) {
Hans de Goede0ee75542019-04-20 13:21:51 +0200486 hid_err(djrcv_dev->hidpp, "delayedwork queued without workitems available\n");
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200487 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
488 return;
489 }
490
491 if (!kfifo_is_empty(&djrcv_dev->notif_fifo)) {
492 if (schedule_work(&djrcv_dev->work) == 0) {
493 dbg_hid("%s: did not schedule the work item, was "
494 "already queued\n", __func__);
495 }
496 }
497
498 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
499
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200500 switch (workitem.type) {
501 case WORKITEM_TYPE_PAIRED:
502 logi_dj_recv_add_djhid_device(djrcv_dev, &workitem);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200503 break;
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200504 case WORKITEM_TYPE_UNPAIRED:
505 logi_dj_recv_destroy_djhid_device(djrcv_dev, &workitem);
506 break;
507 case WORKITEM_TYPE_UNKNOWN:
508 retval = logi_dj_recv_query_paired_devices(djrcv_dev);
509 if (retval) {
Hans de Goede0ee75542019-04-20 13:21:51 +0200510 hid_err(djrcv_dev->hidpp, "%s: logi_dj_recv_query_paired_devices error: %d\n",
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200511 __func__, retval);
512 }
513 break;
514 case WORKITEM_TYPE_EMPTY:
515 dbg_hid("%s: device list is empty\n", __func__);
516 break;
517 }
518}
519
520static void logi_dj_recv_queue_notification(struct dj_receiver_dev *djrcv_dev,
521 struct dj_report *dj_report)
522{
523 /* We are called from atomic context (tasklet && djrcv->lock held) */
524 struct dj_workitem workitem = {
525 .device_index = dj_report->device_index,
526 };
527
528 switch (dj_report->report_type) {
529 case REPORT_TYPE_NOTIF_DEVICE_PAIRED:
530 workitem.type = WORKITEM_TYPE_PAIRED;
531 if (dj_report->report_params[DEVICE_PAIRED_PARAM_SPFUNCTION] &
532 SPFUNCTION_DEVICE_LIST_EMPTY) {
533 workitem.type = WORKITEM_TYPE_EMPTY;
534 break;
535 }
536 /* fall-through */
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200537 case REPORT_TYPE_NOTIF_DEVICE_UNPAIRED:
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200538 workitem.quad_id_msb =
539 dj_report->report_params[DEVICE_PAIRED_PARAM_EQUAD_ID_MSB];
540 workitem.quad_id_lsb =
541 dj_report->report_params[DEVICE_PAIRED_PARAM_EQUAD_ID_LSB];
542 workitem.reports_supported = get_unaligned_le32(
543 dj_report->report_params +
544 DEVICE_PAIRED_RF_REPORT_TYPE);
545 if (dj_report->report_type == REPORT_TYPE_NOTIF_DEVICE_UNPAIRED)
546 workitem.type = WORKITEM_TYPE_UNPAIRED;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200547 break;
548 default:
Nestor Lopez Casadoc63e0e32013-07-18 06:21:30 -0700549 /* A normal report (i. e. not belonging to a pair/unpair notification)
550 * arriving here, means that the report arrived but we did not have a
551 * paired dj_device associated to the report's device_index, this
552 * means that the original "device paired" notification corresponding
553 * to this dj_device never arrived to this driver. The reason is that
554 * hid-core discards all packets coming from a device while probe() is
555 * executing. */
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200556 if (!djrcv_dev->paired_dj_devices[dj_report->device_index]) {
557 /*
558 * ok, we don't know the device, just re-ask the
559 * receiver for the list of connected devices in
560 * the delayed work callback.
561 */
562 workitem.type = WORKITEM_TYPE_UNKNOWN;
Nestor Lopez Casadoc63e0e32013-07-18 06:21:30 -0700563 }
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200564 }
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200565
Benjamin Tissoires4fcad952019-04-20 13:21:48 +0200566 kfifo_in(&djrcv_dev->notif_fifo, &workitem, sizeof(workitem));
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200567
568 if (schedule_work(&djrcv_dev->work) == 0) {
569 dbg_hid("%s: did not schedule the work item, was already "
570 "queued\n", __func__);
571 }
572}
573
574static void logi_dj_recv_forward_null_report(struct dj_receiver_dev *djrcv_dev,
575 struct dj_report *dj_report)
576{
577 /* We are called from atomic context (tasklet && djrcv->lock held) */
578 unsigned int i;
579 u8 reportbuffer[MAX_REPORT_SIZE];
580 struct dj_device *djdev;
581
582 djdev = djrcv_dev->paired_dj_devices[dj_report->device_index];
583
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200584 memset(reportbuffer, 0, sizeof(reportbuffer));
585
586 for (i = 0; i < NUMBER_OF_HID_REPORTS; i++) {
587 if (djdev->reports_supported & (1 << i)) {
588 reportbuffer[0] = i;
589 if (hid_input_report(djdev->hdev,
590 HID_INPUT_REPORT,
591 reportbuffer,
592 hid_reportid_size_map[i], 1)) {
593 dbg_hid("hid_input_report error sending null "
594 "report\n");
595 }
596 }
597 }
598}
599
Benjamin Tissoires83898232019-04-20 13:21:43 +0200600static void logi_dj_recv_forward_dj(struct dj_receiver_dev *djrcv_dev,
601 struct dj_report *dj_report)
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200602{
603 /* We are called from atomic context (tasklet && djrcv->lock held) */
604 struct dj_device *dj_device;
605
606 dj_device = djrcv_dev->paired_dj_devices[dj_report->device_index];
607
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200608 if ((dj_report->report_type > ARRAY_SIZE(hid_reportid_size_map) - 1) ||
609 (hid_reportid_size_map[dj_report->report_type] == 0)) {
610 dbg_hid("invalid report type:%x\n", dj_report->report_type);
611 return;
612 }
613
614 if (hid_input_report(dj_device->hdev,
615 HID_INPUT_REPORT, &dj_report->report_type,
616 hid_reportid_size_map[dj_report->report_type], 1)) {
617 dbg_hid("hid_input_report error\n");
618 }
619}
620
Benjamin Tissoires83898232019-04-20 13:21:43 +0200621static void logi_dj_recv_forward_report(struct dj_device *dj_dev, u8 *data,
622 int size)
Benjamin Tissoires925f0f32014-09-30 13:18:29 -0400623{
624 /* We are called from atomic context (tasklet && djrcv->lock held) */
625 if (hid_input_report(dj_dev->hdev, HID_INPUT_REPORT, data, size, 1))
626 dbg_hid("hid_input_report error\n");
627}
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200628
629static int logi_dj_recv_send_report(struct dj_receiver_dev *djrcv_dev,
630 struct dj_report *dj_report)
631{
Hans de Goede0ee75542019-04-20 13:21:51 +0200632 struct hid_device *hdev = djrcv_dev->hidpp;
Benjamin Tissoiresdcd90062013-03-05 17:09:00 +0100633 struct hid_report *report;
634 struct hid_report_enum *output_report_enum;
635 u8 *data = (u8 *)(&dj_report->device_index);
Kees Cook297502a2013-09-11 21:56:56 +0200636 unsigned int i;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200637
Benjamin Tissoiresdcd90062013-03-05 17:09:00 +0100638 output_report_enum = &hdev->report_enum[HID_OUTPUT_REPORT];
639 report = output_report_enum->report_id_hash[REPORT_ID_DJ_SHORT];
640
641 if (!report) {
642 dev_err(&hdev->dev, "%s: unable to find dj report\n", __func__);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200643 return -ENODEV;
644 }
645
Kees Cook297502a2013-09-11 21:56:56 +0200646 for (i = 0; i < DJREPORT_SHORT_LENGTH - 1; i++)
Benjamin Tissoiresdcd90062013-03-05 17:09:00 +0100647 report->field[0]->value[i] = data[i];
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200648
Jiri Kosina83a44ac2013-03-09 10:58:13 +0100649 hid_hw_request(hdev, report, HID_REQ_SET_REPORT);
Benjamin Tissoiresdcd90062013-03-05 17:09:00 +0100650
651 return 0;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200652}
653
654static int logi_dj_recv_query_paired_devices(struct dj_receiver_dev *djrcv_dev)
655{
Marc Dionned8dc3492012-06-01 18:12:14 -0400656 struct dj_report *dj_report;
657 int retval;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200658
Alan Cox8a55ade2012-09-04 15:10:08 +0100659 dj_report = kzalloc(sizeof(struct dj_report), GFP_KERNEL);
Marc Dionned8dc3492012-06-01 18:12:14 -0400660 if (!dj_report)
661 return -ENOMEM;
662 dj_report->report_id = REPORT_ID_DJ_SHORT;
663 dj_report->device_index = 0xFF;
664 dj_report->report_type = REPORT_TYPE_CMD_GET_PAIRED_DEVICES;
665 retval = logi_dj_recv_send_report(djrcv_dev, dj_report);
666 kfree(dj_report);
667 return retval;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200668}
669
Nestor Lopez Casadoc63e0e32013-07-18 06:21:30 -0700670
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200671static int logi_dj_recv_switch_to_dj_mode(struct dj_receiver_dev *djrcv_dev,
672 unsigned timeout)
673{
Hans de Goede0ee75542019-04-20 13:21:51 +0200674 struct hid_device *hdev = djrcv_dev->hidpp;
Marc Dionned8dc3492012-06-01 18:12:14 -0400675 struct dj_report *dj_report;
Benjamin Tissoires6a9ddc82014-09-30 13:18:31 -0400676 u8 *buf;
Marc Dionned8dc3492012-06-01 18:12:14 -0400677 int retval;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200678
Alan Cox8a55ade2012-09-04 15:10:08 +0100679 dj_report = kzalloc(sizeof(struct dj_report), GFP_KERNEL);
Marc Dionned8dc3492012-06-01 18:12:14 -0400680 if (!dj_report)
681 return -ENOMEM;
682 dj_report->report_id = REPORT_ID_DJ_SHORT;
683 dj_report->device_index = 0xFF;
684 dj_report->report_type = REPORT_TYPE_CMD_SWITCH;
685 dj_report->report_params[CMD_SWITCH_PARAM_DEVBITFIELD] = 0x3F;
686 dj_report->report_params[CMD_SWITCH_PARAM_TIMEOUT_SECONDS] = (u8)timeout;
687 retval = logi_dj_recv_send_report(djrcv_dev, dj_report);
Benjamin Tisssoires42c22db2014-01-08 17:18:45 -0500688
689 /*
690 * Ugly sleep to work around a USB 3.0 bug when the receiver is still
691 * processing the "switch-to-dj" command while we send an other command.
692 * 50 msec should gives enough time to the receiver to be ready.
693 */
694 msleep(50);
695
Benjamin Tissoires6a9ddc82014-09-30 13:18:31 -0400696 /*
697 * Magical bits to set up hidpp notifications when the dj devices
698 * are connected/disconnected.
699 *
700 * We can reuse dj_report because HIDPP_REPORT_SHORT_LENGTH is smaller
701 * than DJREPORT_SHORT_LENGTH.
702 */
703 buf = (u8 *)dj_report;
704
705 memset(buf, 0, HIDPP_REPORT_SHORT_LENGTH);
706
707 buf[0] = REPORT_ID_HIDPP_SHORT;
708 buf[1] = 0xFF;
709 buf[2] = 0x80;
710 buf[3] = 0x00;
711 buf[4] = 0x00;
712 buf[5] = 0x09;
713 buf[6] = 0x00;
714
715 hid_hw_raw_request(hdev, REPORT_ID_HIDPP_SHORT, buf,
716 HIDPP_REPORT_SHORT_LENGTH, HID_OUTPUT_REPORT,
717 HID_REQ_SET_REPORT);
718
719 kfree(dj_report);
Marc Dionned8dc3492012-06-01 18:12:14 -0400720 return retval;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200721}
722
723
724static int logi_dj_ll_open(struct hid_device *hid)
725{
726 dbg_hid("%s:%s\n", __func__, hid->phys);
727 return 0;
728
729}
730
731static void logi_dj_ll_close(struct hid_device *hid)
732{
733 dbg_hid("%s:%s\n", __func__, hid->phys);
734}
735
Benjamin Tissoires8dba3022017-03-27 16:59:21 +0200736/*
737 * Register 0xB5 is "pairing information". It is solely intended for the
738 * receiver, so do not overwrite the device index.
739 */
Benjamin Tissoiresc0340412019-04-20 13:21:46 +0200740static u8 unifying_pairing_query[] = { REPORT_ID_HIDPP_SHORT,
741 HIDPP_RECEIVER_INDEX,
742 HIDPP_GET_LONG_REGISTER,
743 HIDPP_REG_PAIRING_INFORMATION };
744static u8 unifying_pairing_answer[] = { REPORT_ID_HIDPP_LONG,
745 HIDPP_RECEIVER_INDEX,
746 HIDPP_GET_LONG_REGISTER,
747 HIDPP_REG_PAIRING_INFORMATION };
Benjamin Tissoires33797822014-09-30 13:18:30 -0400748
Benjamin Tissoiresbd27e202014-02-10 12:58:53 -0500749static int logi_dj_ll_raw_request(struct hid_device *hid,
750 unsigned char reportnum, __u8 *buf,
751 size_t count, unsigned char report_type,
752 int reqtype)
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200753{
Benjamin Tissoires0e40d352014-02-05 16:33:17 -0500754 struct dj_device *djdev = hid->driver_data;
755 struct dj_receiver_dev *djrcv_dev = djdev->dj_receiver_dev;
756 u8 *out_buf;
757 int ret;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200758
Benjamin Tissoires925f0f32014-09-30 13:18:29 -0400759 if ((buf[0] == REPORT_ID_HIDPP_SHORT) ||
760 (buf[0] == REPORT_ID_HIDPP_LONG)) {
761 if (count < 2)
762 return -EINVAL;
763
Benjamin Tissoires33797822014-09-30 13:18:30 -0400764 /* special case where we should not overwrite
765 * the device_index */
Benjamin Tissoires8dba3022017-03-27 16:59:21 +0200766 if (count == 7 && !memcmp(buf, unifying_pairing_query,
767 sizeof(unifying_pairing_query)))
768 buf[4] = (buf[4] & 0xf0) | (djdev->device_index - 1);
Benjamin Tissoires33797822014-09-30 13:18:30 -0400769 else
770 buf[1] = djdev->device_index;
Hans de Goede0ee75542019-04-20 13:21:51 +0200771 return hid_hw_raw_request(djrcv_dev->hidpp, reportnum, buf,
Benjamin Tissoires925f0f32014-09-30 13:18:29 -0400772 count, report_type, reqtype);
773 }
774
Benjamin Tissoires0e40d352014-02-05 16:33:17 -0500775 if (buf[0] != REPORT_TYPE_LEDS)
776 return -EINVAL;
777
778 out_buf = kzalloc(DJREPORT_SHORT_LENGTH, GFP_ATOMIC);
779 if (!out_buf)
780 return -ENOMEM;
781
Jiri Kosina51217e62014-08-21 09:56:47 -0500782 if (count > DJREPORT_SHORT_LENGTH - 2)
Benjamin Tissoires0e40d352014-02-05 16:33:17 -0500783 count = DJREPORT_SHORT_LENGTH - 2;
784
785 out_buf[0] = REPORT_ID_DJ_SHORT;
786 out_buf[1] = djdev->device_index;
787 memcpy(out_buf + 2, buf, count);
788
Hans de Goede0ee75542019-04-20 13:21:51 +0200789 ret = hid_hw_raw_request(djrcv_dev->hidpp, out_buf[0], out_buf,
Benjamin Tissoiresbd27e202014-02-10 12:58:53 -0500790 DJREPORT_SHORT_LENGTH, report_type, reqtype);
Benjamin Tissoires0e40d352014-02-05 16:33:17 -0500791
792 kfree(out_buf);
793 return ret;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200794}
795
Dan Carpenter6d603322013-10-19 12:08:59 +0300796static void rdcat(char *rdesc, unsigned int *rsize, const char *data, unsigned int size)
Henrik Rydberg2a039bf2012-04-22 14:21:39 +0200797{
Dan Carpenter6d603322013-10-19 12:08:59 +0300798 memcpy(rdesc + *rsize, data, size);
Henrik Rydberg2a039bf2012-04-22 14:21:39 +0200799 *rsize += size;
800}
801
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200802static int logi_dj_ll_parse(struct hid_device *hid)
803{
804 struct dj_device *djdev = hid->driver_data;
Henrik Rydberg2a039bf2012-04-22 14:21:39 +0200805 unsigned int rsize = 0;
806 char *rdesc;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200807 int retval;
808
809 dbg_hid("%s\n", __func__);
810
811 djdev->hdev->version = 0x0111;
812 djdev->hdev->country = 0x00;
813
Henrik Rydberg2a039bf2012-04-22 14:21:39 +0200814 rdesc = kmalloc(MAX_RDESC_SIZE, GFP_KERNEL);
815 if (!rdesc)
816 return -ENOMEM;
817
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200818 if (djdev->reports_supported & STD_KEYBOARD) {
819 dbg_hid("%s: sending a kbd descriptor, reports_supported: %x\n",
820 __func__, djdev->reports_supported);
Dan Carpenter6d603322013-10-19 12:08:59 +0300821 rdcat(rdesc, &rsize, kbd_descriptor, sizeof(kbd_descriptor));
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200822 }
823
824 if (djdev->reports_supported & STD_MOUSE) {
825 dbg_hid("%s: sending a mouse descriptor, reports_supported: "
826 "%x\n", __func__, djdev->reports_supported);
Dan Carpenter6d603322013-10-19 12:08:59 +0300827 rdcat(rdesc, &rsize, mse_descriptor, sizeof(mse_descriptor));
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200828 }
829
830 if (djdev->reports_supported & MULTIMEDIA) {
831 dbg_hid("%s: sending a multimedia report descriptor: %x\n",
832 __func__, djdev->reports_supported);
Dan Carpenter6d603322013-10-19 12:08:59 +0300833 rdcat(rdesc, &rsize, consumer_descriptor, sizeof(consumer_descriptor));
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200834 }
835
836 if (djdev->reports_supported & POWER_KEYS) {
837 dbg_hid("%s: sending a power keys report descriptor: %x\n",
838 __func__, djdev->reports_supported);
Dan Carpenter6d603322013-10-19 12:08:59 +0300839 rdcat(rdesc, &rsize, syscontrol_descriptor, sizeof(syscontrol_descriptor));
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200840 }
841
842 if (djdev->reports_supported & MEDIA_CENTER) {
843 dbg_hid("%s: sending a media center report descriptor: %x\n",
844 __func__, djdev->reports_supported);
Dan Carpenter6d603322013-10-19 12:08:59 +0300845 rdcat(rdesc, &rsize, media_descriptor, sizeof(media_descriptor));
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200846 }
847
848 if (djdev->reports_supported & KBD_LEDS) {
849 dbg_hid("%s: need to send kbd leds report descriptor: %x\n",
850 __func__, djdev->reports_supported);
851 }
852
Benjamin Tissoires925f0f32014-09-30 13:18:29 -0400853 rdcat(rdesc, &rsize, hidpp_descriptor, sizeof(hidpp_descriptor));
854
Henrik Rydberg2a039bf2012-04-22 14:21:39 +0200855 retval = hid_parse_report(hid, rdesc, rsize);
856 kfree(rdesc);
857
858 return retval;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200859}
860
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200861static int logi_dj_ll_start(struct hid_device *hid)
862{
863 dbg_hid("%s\n", __func__);
864 return 0;
865}
866
867static void logi_dj_ll_stop(struct hid_device *hid)
868{
869 dbg_hid("%s\n", __func__);
870}
871
872
873static struct hid_ll_driver logi_dj_ll_driver = {
874 .parse = logi_dj_ll_parse,
875 .start = logi_dj_ll_start,
876 .stop = logi_dj_ll_stop,
877 .open = logi_dj_ll_open,
878 .close = logi_dj_ll_close,
Benjamin Tissoiresbd27e202014-02-10 12:58:53 -0500879 .raw_request = logi_dj_ll_raw_request,
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200880};
881
Benjamin Tissoires925f0f32014-09-30 13:18:29 -0400882static int logi_dj_dj_event(struct hid_device *hdev,
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200883 struct hid_report *report, u8 *data,
884 int size)
885{
886 struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
887 struct dj_report *dj_report = (struct dj_report *) data;
888 unsigned long flags;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200889
Benjamin Tissoires925f0f32014-09-30 13:18:29 -0400890 /*
891 * Here we receive all data coming from iface 2, there are 3 cases:
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200892 *
Benjamin Tissoires925f0f32014-09-30 13:18:29 -0400893 * 1) Data is intended for this driver i. e. data contains arrival,
894 * departure, etc notifications, in which case we queue them for delayed
895 * processing by the work queue. We return 1 to hid-core as no further
896 * processing is required from it.
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200897 *
Benjamin Tissoires925f0f32014-09-30 13:18:29 -0400898 * 2) Data informs a connection change, if the change means rf link
899 * loss, then we must send a null report to the upper layer to discard
900 * potentially pressed keys that may be repeated forever by the input
901 * layer. Return 1 to hid-core as no further processing is required.
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200902 *
Benjamin Tissoires925f0f32014-09-30 13:18:29 -0400903 * 3) Data is an actual input event from a paired DJ device in which
904 * case we forward it to the correct hid device (via hid_input_report()
905 * ) and return 1 so hid-core does not anything else with it.
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200906 */
Benjamin Tissoires5abfe852014-08-22 16:16:05 -0400907
Jiri Kosinaad3e14d2014-08-21 09:57:17 -0500908 if ((dj_report->device_index < DJ_DEVICE_INDEX_MIN) ||
909 (dj_report->device_index > DJ_DEVICE_INDEX_MAX)) {
Benjamin Tissoires5abfe852014-08-22 16:16:05 -0400910 /*
911 * Device index is wrong, bail out.
912 * This driver can ignore safely the receiver notifications,
913 * so ignore those reports too.
914 */
915 if (dj_report->device_index != DJ_RECEIVER_INDEX)
916 dev_err(&hdev->dev, "%s: invalid device index:%d\n",
Jiri Kosinaad3e14d2014-08-21 09:57:17 -0500917 __func__, dj_report->device_index);
918 return false;
919 }
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200920
921 spin_lock_irqsave(&djrcv_dev->lock, flags);
Benjamin Tissoires368d4e592014-08-22 16:16:06 -0400922
923 if (!djrcv_dev->paired_dj_devices[dj_report->device_index]) {
924 /* received an event for an unknown device, bail out */
925 logi_dj_recv_queue_notification(djrcv_dev, dj_report);
926 goto out;
927 }
928
Benjamin Tissoires5abfe852014-08-22 16:16:05 -0400929 switch (dj_report->report_type) {
930 case REPORT_TYPE_NOTIF_DEVICE_PAIRED:
Benjamin Tissoires368d4e592014-08-22 16:16:06 -0400931 /* pairing notifications are handled above the switch */
932 break;
Benjamin Tissoires5abfe852014-08-22 16:16:05 -0400933 case REPORT_TYPE_NOTIF_DEVICE_UNPAIRED:
934 logi_dj_recv_queue_notification(djrcv_dev, dj_report);
935 break;
936 case REPORT_TYPE_NOTIF_CONNECTION_STATUS:
937 if (dj_report->report_params[CONNECTION_STATUS_PARAM_STATUS] ==
938 STATUS_LINKLOSS) {
939 logi_dj_recv_forward_null_report(djrcv_dev, dj_report);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200940 }
Benjamin Tissoires5abfe852014-08-22 16:16:05 -0400941 break;
942 default:
Benjamin Tissoires83898232019-04-20 13:21:43 +0200943 logi_dj_recv_forward_dj(djrcv_dev, dj_report);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200944 }
Benjamin Tissoires368d4e592014-08-22 16:16:06 -0400945
946out:
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200947 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
948
Benjamin Tissoires5abfe852014-08-22 16:16:05 -0400949 return true;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200950}
951
Benjamin Tissoires925f0f32014-09-30 13:18:29 -0400952static int logi_dj_hidpp_event(struct hid_device *hdev,
953 struct hid_report *report, u8 *data,
954 int size)
955{
956 struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
Benjamin Tissoires7bb56a52019-04-20 13:21:44 +0200957 struct hidpp_event *hidpp_report = (struct hidpp_event *) data;
Benjamin Tissoires925f0f32014-09-30 13:18:29 -0400958 unsigned long flags;
Benjamin Tissoires7bb56a52019-04-20 13:21:44 +0200959 u8 device_index = hidpp_report->device_index;
Benjamin Tissoires925f0f32014-09-30 13:18:29 -0400960
Benjamin Tissoires33797822014-09-30 13:18:30 -0400961 if (device_index == HIDPP_RECEIVER_INDEX) {
962 /* special case were the device wants to know its unifying
963 * name */
964 if (size == HIDPP_REPORT_LONG_LENGTH &&
Benjamin Tissoires8dba3022017-03-27 16:59:21 +0200965 !memcmp(data, unifying_pairing_answer,
966 sizeof(unifying_pairing_answer)))
Benjamin Tissoires33797822014-09-30 13:18:30 -0400967 device_index = (data[4] & 0x0F) + 1;
968 else
969 return false;
970 }
Benjamin Tissoires925f0f32014-09-30 13:18:29 -0400971
972 /*
973 * Data is from the HID++ collection, in this case, we forward the
974 * data to the corresponding child dj device and return 0 to hid-core
975 * so he data also goes to the hidraw device of the receiver. This
976 * allows a user space application to implement the full HID++ routing
977 * via the receiver.
978 */
979
980 if ((device_index < DJ_DEVICE_INDEX_MIN) ||
981 (device_index > DJ_DEVICE_INDEX_MAX)) {
982 /*
983 * Device index is wrong, bail out.
984 * This driver can ignore safely the receiver notifications,
985 * so ignore those reports too.
986 */
987 dev_err(&hdev->dev, "%s: invalid device index:%d\n",
Benjamin Tissoires7bb56a52019-04-20 13:21:44 +0200988 __func__, hidpp_report->device_index);
Benjamin Tissoires925f0f32014-09-30 13:18:29 -0400989 return false;
990 }
991
992 spin_lock_irqsave(&djrcv_dev->lock, flags);
993
994 if (!djrcv_dev->paired_dj_devices[device_index])
995 /* received an event for an unknown device, bail out */
996 goto out;
997
Benjamin Tissoires83898232019-04-20 13:21:43 +0200998 logi_dj_recv_forward_report(djrcv_dev->paired_dj_devices[device_index],
999 data, size);
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001000
1001out:
1002 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
1003
1004 return false;
1005}
1006
1007static int logi_dj_raw_event(struct hid_device *hdev,
1008 struct hid_report *report, u8 *data,
1009 int size)
1010{
1011 dbg_hid("%s, size:%d\n", __func__, size);
1012
1013 switch (data[0]) {
1014 case REPORT_ID_DJ_SHORT:
Peter Wuf254ae92014-12-16 16:55:21 +01001015 if (size != DJREPORT_SHORT_LENGTH) {
1016 dev_err(&hdev->dev, "DJ report of bad size (%d)", size);
1017 return false;
1018 }
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001019 return logi_dj_dj_event(hdev, report, data, size);
1020 case REPORT_ID_HIDPP_SHORT:
Peter Wuf254ae92014-12-16 16:55:21 +01001021 if (size != HIDPP_REPORT_SHORT_LENGTH) {
1022 dev_err(&hdev->dev,
1023 "Short HID++ report of bad size (%d)", size);
1024 return false;
1025 }
1026 return logi_dj_hidpp_event(hdev, report, data, size);
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001027 case REPORT_ID_HIDPP_LONG:
Peter Wuf254ae92014-12-16 16:55:21 +01001028 if (size != HIDPP_REPORT_LONG_LENGTH) {
1029 dev_err(&hdev->dev,
1030 "Long HID++ report of bad size (%d)", size);
1031 return false;
1032 }
Benjamin Tissoires925f0f32014-09-30 13:18:29 -04001033 return logi_dj_hidpp_event(hdev, report, data, size);
1034 }
1035
1036 return false;
1037}
1038
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001039static int logi_dj_probe(struct hid_device *hdev,
1040 const struct hid_device_id *id)
1041{
Benjamin Tissoires82c0beb2019-04-20 13:21:47 +02001042 struct hid_report_enum *rep_enum;
1043 struct hid_report *rep;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001044 struct dj_receiver_dev *djrcv_dev;
Benjamin Tissoires82c0beb2019-04-20 13:21:47 +02001045 bool has_hidpp = false;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001046 int retval;
1047
Benjamin Tissoires82c0beb2019-04-20 13:21:47 +02001048 /*
1049 * Call to usbhid to fetch the HID descriptors of the current
1050 * interface subsequently call to the hid/hid-core to parse the
1051 * fetched descriptors.
1052 */
1053 retval = hid_parse(hdev);
1054 if (retval) {
1055 dev_err(&hdev->dev,
1056 "%s:parse failed\n", __func__);
1057 return retval;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001058 }
1059
Benjamin Tissoires82c0beb2019-04-20 13:21:47 +02001060 rep_enum = &hdev->report_enum[HID_INPUT_REPORT];
1061
1062 /*
1063 * Check for the HID++ application.
1064 * Note: we should theoretically check for HID++ and DJ
1065 * collections, but this will do.
1066 */
1067 list_for_each_entry(rep, &rep_enum->report_list, list) {
1068 if (rep->application == 0xff000001)
1069 has_hidpp = true;
1070 }
1071
1072 /*
1073 * Ignore interfaces without DJ/HID++ collection, they will not carry
1074 * any data, dont create any hid_device for them.
1075 */
1076 if (!has_hidpp)
1077 return -ENODEV;
1078
1079 /* Treat DJ/HID++ interface */
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001080
1081 djrcv_dev = kzalloc(sizeof(struct dj_receiver_dev), GFP_KERNEL);
1082 if (!djrcv_dev) {
1083 dev_err(&hdev->dev,
1084 "%s:failed allocating dj_receiver_dev\n", __func__);
1085 return -ENOMEM;
1086 }
Hans de Goede0ee75542019-04-20 13:21:51 +02001087 djrcv_dev->hidpp = hdev;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001088 INIT_WORK(&djrcv_dev->work, delayedwork_callback);
1089 spin_lock_init(&djrcv_dev->lock);
1090 if (kfifo_alloc(&djrcv_dev->notif_fifo,
Benjamin Tissoires4fcad952019-04-20 13:21:48 +02001091 DJ_MAX_NUMBER_NOTIFS * sizeof(struct dj_workitem),
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001092 GFP_KERNEL)) {
1093 dev_err(&hdev->dev,
1094 "%s:failed allocating notif_fifo\n", __func__);
1095 kfree(djrcv_dev);
1096 return -ENOMEM;
1097 }
1098 hid_set_drvdata(hdev, djrcv_dev);
1099
Kees Cook297502a2013-09-11 21:56:56 +02001100
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001101 /* Starts the usb device and connects to upper interfaces hiddev and
1102 * hidraw */
1103 retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
1104 if (retval) {
1105 dev_err(&hdev->dev,
1106 "%s:hid_hw_start returned error\n", __func__);
1107 goto hid_hw_start_fail;
1108 }
1109
1110 retval = logi_dj_recv_switch_to_dj_mode(djrcv_dev, 0);
1111 if (retval < 0) {
1112 dev_err(&hdev->dev,
1113 "%s:logi_dj_recv_switch_to_dj_mode returned error:%d\n",
1114 __func__, retval);
1115 goto switch_to_dj_mode_fail;
1116 }
1117
1118 /* This is enabling the polling urb on the IN endpoint */
Benjamin Tissoiresddf75402013-07-12 11:01:02 +02001119 retval = hid_hw_open(hdev);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001120 if (retval < 0) {
Benjamin Tissoiresddf75402013-07-12 11:01:02 +02001121 dev_err(&hdev->dev, "%s:hid_hw_open returned error:%d\n",
1122 __func__, retval);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001123 goto llopen_failed;
1124 }
1125
Andrew de los Reyesa9dd22b72013-02-18 09:20:22 -08001126 /* Allow incoming packets to arrive: */
1127 hid_device_io_start(hdev);
1128
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001129 retval = logi_dj_recv_query_paired_devices(djrcv_dev);
1130 if (retval < 0) {
1131 dev_err(&hdev->dev, "%s:logi_dj_recv_query_paired_devices "
1132 "error:%d\n", __func__, retval);
1133 goto logi_dj_recv_query_paired_devices_failed;
1134 }
1135
1136 return retval;
1137
1138logi_dj_recv_query_paired_devices_failed:
Benjamin Tissoiresddf75402013-07-12 11:01:02 +02001139 hid_hw_close(hdev);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001140
1141llopen_failed:
1142switch_to_dj_mode_fail:
1143 hid_hw_stop(hdev);
1144
1145hid_hw_start_fail:
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001146 kfifo_free(&djrcv_dev->notif_fifo);
1147 kfree(djrcv_dev);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001148 return retval;
1149
1150}
1151
1152#ifdef CONFIG_PM
1153static int logi_dj_reset_resume(struct hid_device *hdev)
1154{
1155 int retval;
1156 struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
1157
1158 retval = logi_dj_recv_switch_to_dj_mode(djrcv_dev, 0);
1159 if (retval < 0) {
1160 dev_err(&hdev->dev,
1161 "%s:logi_dj_recv_switch_to_dj_mode returned error:%d\n",
1162 __func__, retval);
1163 }
1164
1165 return 0;
1166}
1167#endif
1168
1169static void logi_dj_remove(struct hid_device *hdev)
1170{
1171 struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
1172 struct dj_device *dj_dev;
1173 int i;
1174
1175 dbg_hid("%s\n", __func__);
1176
1177 cancel_work_sync(&djrcv_dev->work);
1178
Benjamin Tissoiresddf75402013-07-12 11:01:02 +02001179 hid_hw_close(hdev);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001180 hid_hw_stop(hdev);
1181
1182 /* I suppose that at this point the only context that can access
1183 * the djrecv_data is this thread as the work item is guaranteed to
1184 * have finished and no more raw_event callbacks should arrive after
1185 * the remove callback was triggered so no locks are put around the
1186 * code below */
Nestor Lopez Casado844580f2011-09-20 15:59:03 +02001187 for (i = 0; i < (DJ_MAX_PAIRED_DEVICES + DJ_DEVICE_INDEX_MIN); i++) {
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001188 dj_dev = djrcv_dev->paired_dj_devices[i];
1189 if (dj_dev != NULL) {
1190 hid_destroy_device(dj_dev->hdev);
1191 kfree(dj_dev);
1192 djrcv_dev->paired_dj_devices[i] = NULL;
1193 }
1194 }
1195
1196 kfifo_free(&djrcv_dev->notif_fifo);
1197 kfree(djrcv_dev);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001198}
1199
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001200static const struct hid_device_id logi_dj_receivers[] = {
1201 {HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
1202 USB_DEVICE_ID_LOGITECH_UNIFYING_RECEIVER)},
1203 {HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
1204 USB_DEVICE_ID_LOGITECH_UNIFYING_RECEIVER_2)},
1205 {}
1206};
1207
1208MODULE_DEVICE_TABLE(hid, logi_dj_receivers);
1209
1210static struct hid_driver logi_djreceiver_driver = {
1211 .name = "logitech-djreceiver",
1212 .id_table = logi_dj_receivers,
1213 .probe = logi_dj_probe,
1214 .remove = logi_dj_remove,
1215 .raw_event = logi_dj_raw_event,
1216#ifdef CONFIG_PM
1217 .reset_resume = logi_dj_reset_resume,
1218#endif
1219};
1220
Benjamin Tissoiresab94e562014-09-30 13:18:28 -04001221module_hid_driver(logi_djreceiver_driver);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001222
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +02001223MODULE_LICENSE("GPL");
1224MODULE_AUTHOR("Logitech");
1225MODULE_AUTHOR("Nestor Lopez Casado");
1226MODULE_AUTHOR("nlopezcasad@logitech.com");