Linux 4.18.10
[linux/fpc-iii.git] / drivers / hid / hid-logitech-dj.c
blob826fa1e1c8d985fb65a2af973c43568c043c6949
1 /*
2 * HID driver for Logitech Unifying receivers
4 * Copyright (c) 2011 Logitech
5 */
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.
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.
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
25 #include <linux/device.h>
26 #include <linux/hid.h>
27 #include <linux/module.h>
28 #include <linux/usb.h>
29 #include <linux/kfifo.h>
30 #include <asm/unaligned.h>
31 #include "hid-ids.h"
33 #define DJ_MAX_PAIRED_DEVICES 6
34 #define DJ_MAX_NUMBER_NOTIFICATIONS 8
35 #define DJ_RECEIVER_INDEX 0
36 #define DJ_DEVICE_INDEX_MIN 1
37 #define DJ_DEVICE_INDEX_MAX 6
39 #define DJREPORT_SHORT_LENGTH 15
40 #define DJREPORT_LONG_LENGTH 32
42 #define REPORT_ID_DJ_SHORT 0x20
43 #define REPORT_ID_DJ_LONG 0x21
45 #define REPORT_ID_HIDPP_SHORT 0x10
46 #define REPORT_ID_HIDPP_LONG 0x11
48 #define HIDPP_REPORT_SHORT_LENGTH 7
49 #define HIDPP_REPORT_LONG_LENGTH 20
51 #define HIDPP_RECEIVER_INDEX 0xff
53 #define REPORT_TYPE_RFREPORT_FIRST 0x01
54 #define REPORT_TYPE_RFREPORT_LAST 0x1F
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
62 /* Command to Get the list of Paired devices */
63 #define REPORT_TYPE_CMD_GET_PAIRED_DEVICES 0x81
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
74 /* Device Un-Paired Notification */
75 #define REPORT_TYPE_NOTIF_DEVICE_UNPAIRED 0x40
78 /* Connection Status Notification */
79 #define REPORT_TYPE_NOTIF_CONNECTION_STATUS 0x42
80 #define CONNECTION_STATUS_PARAM_STATUS 0x00
81 #define STATUS_LINKLOSS 0x01
83 /* Error Notification */
84 #define REPORT_TYPE_NOTIF_ERROR 0x7F
85 #define NOTIF_ERROR_PARAM_ETYPE 0x00
86 #define ETYPE_KEEPALIVE_TIMEOUT 0x01
88 /* supported DJ HID && RF report types */
89 #define REPORT_TYPE_KEYBOARD 0x01
90 #define REPORT_TYPE_MOUSE 0x02
91 #define REPORT_TYPE_CONSUMER_CONTROL 0x03
92 #define REPORT_TYPE_SYSTEM_CONTROL 0x04
93 #define REPORT_TYPE_MEDIA_CENTER 0x08
94 #define REPORT_TYPE_LEDS 0x0E
96 /* RF Report types bitfield */
97 #define STD_KEYBOARD 0x00000002
98 #define STD_MOUSE 0x00000004
99 #define MULTIMEDIA 0x00000008
100 #define POWER_KEYS 0x00000010
101 #define MEDIA_CENTER 0x00000100
102 #define KBD_LEDS 0x00004000
104 struct dj_report {
105 u8 report_id;
106 u8 device_index;
107 u8 report_type;
108 u8 report_params[DJREPORT_SHORT_LENGTH - 3];
111 struct dj_receiver_dev {
112 struct hid_device *hdev;
113 struct dj_device *paired_dj_devices[DJ_MAX_PAIRED_DEVICES +
114 DJ_DEVICE_INDEX_MIN];
115 struct work_struct work;
116 struct kfifo notif_fifo;
117 spinlock_t lock;
118 bool querying_devices;
121 struct dj_device {
122 struct hid_device *hdev;
123 struct dj_receiver_dev *dj_receiver_dev;
124 u32 reports_supported;
125 u8 device_index;
128 /* Keyboard descriptor (1) */
129 static const char kbd_descriptor[] = {
130 0x05, 0x01, /* USAGE_PAGE (generic Desktop) */
131 0x09, 0x06, /* USAGE (Keyboard) */
132 0xA1, 0x01, /* COLLECTION (Application) */
133 0x85, 0x01, /* REPORT_ID (1) */
134 0x95, 0x08, /* REPORT_COUNT (8) */
135 0x75, 0x01, /* REPORT_SIZE (1) */
136 0x15, 0x00, /* LOGICAL_MINIMUM (0) */
137 0x25, 0x01, /* LOGICAL_MAXIMUM (1) */
138 0x05, 0x07, /* USAGE_PAGE (Keyboard) */
139 0x19, 0xE0, /* USAGE_MINIMUM (Left Control) */
140 0x29, 0xE7, /* USAGE_MAXIMUM (Right GUI) */
141 0x81, 0x02, /* INPUT (Data,Var,Abs) */
142 0x95, 0x06, /* REPORT_COUNT (6) */
143 0x75, 0x08, /* REPORT_SIZE (8) */
144 0x15, 0x00, /* LOGICAL_MINIMUM (0) */
145 0x26, 0xFF, 0x00, /* LOGICAL_MAXIMUM (255) */
146 0x05, 0x07, /* USAGE_PAGE (Keyboard) */
147 0x19, 0x00, /* USAGE_MINIMUM (no event) */
148 0x2A, 0xFF, 0x00, /* USAGE_MAXIMUM (reserved) */
149 0x81, 0x00, /* INPUT (Data,Ary,Abs) */
150 0x85, 0x0e, /* REPORT_ID (14) */
151 0x05, 0x08, /* USAGE PAGE (LED page) */
152 0x95, 0x05, /* REPORT COUNT (5) */
153 0x75, 0x01, /* REPORT SIZE (1) */
154 0x15, 0x00, /* LOGICAL_MINIMUM (0) */
155 0x25, 0x01, /* LOGICAL_MAXIMUM (1) */
156 0x19, 0x01, /* USAGE MINIMUM (1) */
157 0x29, 0x05, /* USAGE MAXIMUM (5) */
158 0x91, 0x02, /* OUTPUT (Data, Variable, Absolute) */
159 0x95, 0x01, /* REPORT COUNT (1) */
160 0x75, 0x03, /* REPORT SIZE (3) */
161 0x91, 0x01, /* OUTPUT (Constant) */
162 0xC0
165 /* Mouse descriptor (2) */
166 static const char mse_descriptor[] = {
167 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */
168 0x09, 0x02, /* USAGE (Mouse) */
169 0xA1, 0x01, /* COLLECTION (Application) */
170 0x85, 0x02, /* REPORT_ID = 2 */
171 0x09, 0x01, /* USAGE (pointer) */
172 0xA1, 0x00, /* COLLECTION (physical) */
173 0x05, 0x09, /* USAGE_PAGE (buttons) */
174 0x19, 0x01, /* USAGE_MIN (1) */
175 0x29, 0x10, /* USAGE_MAX (16) */
176 0x15, 0x00, /* LOGICAL_MIN (0) */
177 0x25, 0x01, /* LOGICAL_MAX (1) */
178 0x95, 0x10, /* REPORT_COUNT (16) */
179 0x75, 0x01, /* REPORT_SIZE (1) */
180 0x81, 0x02, /* INPUT (data var abs) */
181 0x05, 0x01, /* USAGE_PAGE (generic desktop) */
182 0x16, 0x01, 0xF8, /* LOGICAL_MIN (-2047) */
183 0x26, 0xFF, 0x07, /* LOGICAL_MAX (2047) */
184 0x75, 0x0C, /* REPORT_SIZE (12) */
185 0x95, 0x02, /* REPORT_COUNT (2) */
186 0x09, 0x30, /* USAGE (X) */
187 0x09, 0x31, /* USAGE (Y) */
188 0x81, 0x06, /* INPUT */
189 0x15, 0x81, /* LOGICAL_MIN (-127) */
190 0x25, 0x7F, /* LOGICAL_MAX (127) */
191 0x75, 0x08, /* REPORT_SIZE (8) */
192 0x95, 0x01, /* REPORT_COUNT (1) */
193 0x09, 0x38, /* USAGE (wheel) */
194 0x81, 0x06, /* INPUT */
195 0x05, 0x0C, /* USAGE_PAGE(consumer) */
196 0x0A, 0x38, 0x02, /* USAGE(AC Pan) */
197 0x95, 0x01, /* REPORT_COUNT (1) */
198 0x81, 0x06, /* INPUT */
199 0xC0, /* END_COLLECTION */
200 0xC0, /* END_COLLECTION */
203 /* Consumer Control descriptor (3) */
204 static const char consumer_descriptor[] = {
205 0x05, 0x0C, /* USAGE_PAGE (Consumer Devices) */
206 0x09, 0x01, /* USAGE (Consumer Control) */
207 0xA1, 0x01, /* COLLECTION (Application) */
208 0x85, 0x03, /* REPORT_ID = 3 */
209 0x75, 0x10, /* REPORT_SIZE (16) */
210 0x95, 0x02, /* REPORT_COUNT (2) */
211 0x15, 0x01, /* LOGICAL_MIN (1) */
212 0x26, 0x8C, 0x02, /* LOGICAL_MAX (652) */
213 0x19, 0x01, /* USAGE_MIN (1) */
214 0x2A, 0x8C, 0x02, /* USAGE_MAX (652) */
215 0x81, 0x00, /* INPUT (Data Ary Abs) */
216 0xC0, /* END_COLLECTION */
217 }; /* */
219 /* System control descriptor (4) */
220 static const char syscontrol_descriptor[] = {
221 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */
222 0x09, 0x80, /* USAGE (System Control) */
223 0xA1, 0x01, /* COLLECTION (Application) */
224 0x85, 0x04, /* REPORT_ID = 4 */
225 0x75, 0x02, /* REPORT_SIZE (2) */
226 0x95, 0x01, /* REPORT_COUNT (1) */
227 0x15, 0x01, /* LOGICAL_MIN (1) */
228 0x25, 0x03, /* LOGICAL_MAX (3) */
229 0x09, 0x82, /* USAGE (System Sleep) */
230 0x09, 0x81, /* USAGE (System Power Down) */
231 0x09, 0x83, /* USAGE (System Wake Up) */
232 0x81, 0x60, /* INPUT (Data Ary Abs NPrf Null) */
233 0x75, 0x06, /* REPORT_SIZE (6) */
234 0x81, 0x03, /* INPUT (Cnst Var Abs) */
235 0xC0, /* END_COLLECTION */
238 /* Media descriptor (8) */
239 static const char media_descriptor[] = {
240 0x06, 0xbc, 0xff, /* Usage Page 0xffbc */
241 0x09, 0x88, /* Usage 0x0088 */
242 0xa1, 0x01, /* BeginCollection */
243 0x85, 0x08, /* Report ID 8 */
244 0x19, 0x01, /* Usage Min 0x0001 */
245 0x29, 0xff, /* Usage Max 0x00ff */
246 0x15, 0x01, /* Logical Min 1 */
247 0x26, 0xff, 0x00, /* Logical Max 255 */
248 0x75, 0x08, /* Report Size 8 */
249 0x95, 0x01, /* Report Count 1 */
250 0x81, 0x00, /* Input */
251 0xc0, /* EndCollection */
252 }; /* */
254 /* HIDPP descriptor */
255 static const char hidpp_descriptor[] = {
256 0x06, 0x00, 0xff, /* Usage Page (Vendor Defined Page 1) */
257 0x09, 0x01, /* Usage (Vendor Usage 1) */
258 0xa1, 0x01, /* Collection (Application) */
259 0x85, 0x10, /* Report ID (16) */
260 0x75, 0x08, /* Report Size (8) */
261 0x95, 0x06, /* Report Count (6) */
262 0x15, 0x00, /* Logical Minimum (0) */
263 0x26, 0xff, 0x00, /* Logical Maximum (255) */
264 0x09, 0x01, /* Usage (Vendor Usage 1) */
265 0x81, 0x00, /* Input (Data,Arr,Abs) */
266 0x09, 0x01, /* Usage (Vendor Usage 1) */
267 0x91, 0x00, /* Output (Data,Arr,Abs) */
268 0xc0, /* End Collection */
269 0x06, 0x00, 0xff, /* Usage Page (Vendor Defined Page 1) */
270 0x09, 0x02, /* Usage (Vendor Usage 2) */
271 0xa1, 0x01, /* Collection (Application) */
272 0x85, 0x11, /* Report ID (17) */
273 0x75, 0x08, /* Report Size (8) */
274 0x95, 0x13, /* Report Count (19) */
275 0x15, 0x00, /* Logical Minimum (0) */
276 0x26, 0xff, 0x00, /* Logical Maximum (255) */
277 0x09, 0x02, /* Usage (Vendor Usage 2) */
278 0x81, 0x00, /* Input (Data,Arr,Abs) */
279 0x09, 0x02, /* Usage (Vendor Usage 2) */
280 0x91, 0x00, /* Output (Data,Arr,Abs) */
281 0xc0, /* End Collection */
282 0x06, 0x00, 0xff, /* Usage Page (Vendor Defined Page 1) */
283 0x09, 0x04, /* Usage (Vendor Usage 0x04) */
284 0xa1, 0x01, /* Collection (Application) */
285 0x85, 0x20, /* Report ID (32) */
286 0x75, 0x08, /* Report Size (8) */
287 0x95, 0x0e, /* Report Count (14) */
288 0x15, 0x00, /* Logical Minimum (0) */
289 0x26, 0xff, 0x00, /* Logical Maximum (255) */
290 0x09, 0x41, /* Usage (Vendor Usage 0x41) */
291 0x81, 0x00, /* Input (Data,Arr,Abs) */
292 0x09, 0x41, /* Usage (Vendor Usage 0x41) */
293 0x91, 0x00, /* Output (Data,Arr,Abs) */
294 0x85, 0x21, /* Report ID (33) */
295 0x95, 0x1f, /* Report Count (31) */
296 0x15, 0x00, /* Logical Minimum (0) */
297 0x26, 0xff, 0x00, /* Logical Maximum (255) */
298 0x09, 0x42, /* Usage (Vendor Usage 0x42) */
299 0x81, 0x00, /* Input (Data,Arr,Abs) */
300 0x09, 0x42, /* Usage (Vendor Usage 0x42) */
301 0x91, 0x00, /* Output (Data,Arr,Abs) */
302 0xc0, /* End Collection */
305 /* Maximum size of all defined hid reports in bytes (including report id) */
306 #define MAX_REPORT_SIZE 8
308 /* Make sure all descriptors are present here */
309 #define MAX_RDESC_SIZE \
310 (sizeof(kbd_descriptor) + \
311 sizeof(mse_descriptor) + \
312 sizeof(consumer_descriptor) + \
313 sizeof(syscontrol_descriptor) + \
314 sizeof(media_descriptor) + \
315 sizeof(hidpp_descriptor))
317 /* Number of possible hid report types that can be created by this driver.
319 * Right now, RF report types have the same report types (or report id's)
320 * than the hid report created from those RF reports. In the future
321 * this doesnt have to be true.
323 * For instance, RF report type 0x01 which has a size of 8 bytes, corresponds
324 * to hid report id 0x01, this is standard keyboard. Same thing applies to mice
325 * reports and consumer control, etc. If a new RF report is created, it doesn't
326 * has to have the same report id as its corresponding hid report, so an
327 * translation may have to take place for future report types.
329 #define NUMBER_OF_HID_REPORTS 32
330 static const u8 hid_reportid_size_map[NUMBER_OF_HID_REPORTS] = {
331 [1] = 8, /* Standard keyboard */
332 [2] = 8, /* Standard mouse */
333 [3] = 5, /* Consumer control */
334 [4] = 2, /* System control */
335 [8] = 2, /* Media Center */
339 #define LOGITECH_DJ_INTERFACE_NUMBER 0x02
341 static struct hid_ll_driver logi_dj_ll_driver;
343 static int logi_dj_recv_query_paired_devices(struct dj_receiver_dev *djrcv_dev);
345 static void logi_dj_recv_destroy_djhid_device(struct dj_receiver_dev *djrcv_dev,
346 struct dj_report *dj_report)
348 /* Called in delayed work context */
349 struct dj_device *dj_dev;
350 unsigned long flags;
352 spin_lock_irqsave(&djrcv_dev->lock, flags);
353 dj_dev = djrcv_dev->paired_dj_devices[dj_report->device_index];
354 djrcv_dev->paired_dj_devices[dj_report->device_index] = NULL;
355 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
357 if (dj_dev != NULL) {
358 hid_destroy_device(dj_dev->hdev);
359 kfree(dj_dev);
360 } else {
361 dev_err(&djrcv_dev->hdev->dev, "%s: can't destroy a NULL device\n",
362 __func__);
366 static void logi_dj_recv_add_djhid_device(struct dj_receiver_dev *djrcv_dev,
367 struct dj_report *dj_report)
369 /* Called in delayed work context */
370 struct hid_device *djrcv_hdev = djrcv_dev->hdev;
371 struct usb_interface *intf = to_usb_interface(djrcv_hdev->dev.parent);
372 struct usb_device *usbdev = interface_to_usbdev(intf);
373 struct hid_device *dj_hiddev;
374 struct dj_device *dj_dev;
376 /* Device index goes from 1 to 6, we need 3 bytes to store the
377 * semicolon, the index, and a null terminator
379 unsigned char tmpstr[3];
381 if (dj_report->report_params[DEVICE_PAIRED_PARAM_SPFUNCTION] &
382 SPFUNCTION_DEVICE_LIST_EMPTY) {
383 dbg_hid("%s: device list is empty\n", __func__);
384 djrcv_dev->querying_devices = false;
385 return;
388 if (djrcv_dev->paired_dj_devices[dj_report->device_index]) {
389 /* The device is already known. No need to reallocate it. */
390 dbg_hid("%s: device is already known\n", __func__);
391 return;
394 dj_hiddev = hid_allocate_device();
395 if (IS_ERR(dj_hiddev)) {
396 dev_err(&djrcv_hdev->dev, "%s: hid_allocate_device failed\n",
397 __func__);
398 return;
401 dj_hiddev->ll_driver = &logi_dj_ll_driver;
403 dj_hiddev->dev.parent = &djrcv_hdev->dev;
404 dj_hiddev->bus = BUS_USB;
405 dj_hiddev->vendor = le16_to_cpu(usbdev->descriptor.idVendor);
406 dj_hiddev->product =
407 (dj_report->report_params[DEVICE_PAIRED_PARAM_EQUAD_ID_MSB]
408 << 8) |
409 dj_report->report_params[DEVICE_PAIRED_PARAM_EQUAD_ID_LSB];
410 snprintf(dj_hiddev->name, sizeof(dj_hiddev->name),
411 "Logitech Unifying Device. Wireless PID:%04x",
412 dj_hiddev->product);
414 dj_hiddev->group = HID_GROUP_LOGITECH_DJ_DEVICE;
416 usb_make_path(usbdev, dj_hiddev->phys, sizeof(dj_hiddev->phys));
417 snprintf(tmpstr, sizeof(tmpstr), ":%d", dj_report->device_index);
418 strlcat(dj_hiddev->phys, tmpstr, sizeof(dj_hiddev->phys));
420 dj_dev = kzalloc(sizeof(struct dj_device), GFP_KERNEL);
422 if (!dj_dev) {
423 dev_err(&djrcv_hdev->dev, "%s: failed allocating dj_device\n",
424 __func__);
425 goto dj_device_allocate_fail;
428 dj_dev->reports_supported = get_unaligned_le32(
429 dj_report->report_params + DEVICE_PAIRED_RF_REPORT_TYPE);
430 dj_dev->hdev = dj_hiddev;
431 dj_dev->dj_receiver_dev = djrcv_dev;
432 dj_dev->device_index = dj_report->device_index;
433 dj_hiddev->driver_data = dj_dev;
435 djrcv_dev->paired_dj_devices[dj_report->device_index] = dj_dev;
437 if (hid_add_device(dj_hiddev)) {
438 dev_err(&djrcv_hdev->dev, "%s: failed adding dj_device\n",
439 __func__);
440 goto hid_add_device_fail;
443 return;
445 hid_add_device_fail:
446 djrcv_dev->paired_dj_devices[dj_report->device_index] = NULL;
447 kfree(dj_dev);
448 dj_device_allocate_fail:
449 hid_destroy_device(dj_hiddev);
452 static void delayedwork_callback(struct work_struct *work)
454 struct dj_receiver_dev *djrcv_dev =
455 container_of(work, struct dj_receiver_dev, work);
457 struct dj_report dj_report;
458 unsigned long flags;
459 int count;
460 int retval;
462 dbg_hid("%s\n", __func__);
464 spin_lock_irqsave(&djrcv_dev->lock, flags);
466 count = kfifo_out(&djrcv_dev->notif_fifo, &dj_report,
467 sizeof(struct dj_report));
469 if (count != sizeof(struct dj_report)) {
470 dev_err(&djrcv_dev->hdev->dev, "%s: workitem triggered without "
471 "notifications available\n", __func__);
472 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
473 return;
476 if (!kfifo_is_empty(&djrcv_dev->notif_fifo)) {
477 if (schedule_work(&djrcv_dev->work) == 0) {
478 dbg_hid("%s: did not schedule the work item, was "
479 "already queued\n", __func__);
483 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
485 switch (dj_report.report_type) {
486 case REPORT_TYPE_NOTIF_DEVICE_PAIRED:
487 logi_dj_recv_add_djhid_device(djrcv_dev, &dj_report);
488 break;
489 case REPORT_TYPE_NOTIF_DEVICE_UNPAIRED:
490 logi_dj_recv_destroy_djhid_device(djrcv_dev, &dj_report);
491 break;
492 default:
493 /* A normal report (i. e. not belonging to a pair/unpair notification)
494 * arriving here, means that the report arrived but we did not have a
495 * paired dj_device associated to the report's device_index, this
496 * means that the original "device paired" notification corresponding
497 * to this dj_device never arrived to this driver. The reason is that
498 * hid-core discards all packets coming from a device while probe() is
499 * executing. */
500 if (!djrcv_dev->paired_dj_devices[dj_report.device_index]) {
501 /* ok, we don't know the device, just re-ask the
502 * receiver for the list of connected devices. */
503 retval = logi_dj_recv_query_paired_devices(djrcv_dev);
504 if (!retval) {
505 /* everything went fine, so just leave */
506 break;
508 dev_err(&djrcv_dev->hdev->dev,
509 "%s:logi_dj_recv_query_paired_devices "
510 "error:%d\n", __func__, retval);
512 dbg_hid("%s: unexpected report type\n", __func__);
516 static void logi_dj_recv_queue_notification(struct dj_receiver_dev *djrcv_dev,
517 struct dj_report *dj_report)
519 /* We are called from atomic context (tasklet && djrcv->lock held) */
521 kfifo_in(&djrcv_dev->notif_fifo, dj_report, sizeof(struct dj_report));
523 if (schedule_work(&djrcv_dev->work) == 0) {
524 dbg_hid("%s: did not schedule the work item, was already "
525 "queued\n", __func__);
529 static void logi_dj_recv_forward_null_report(struct dj_receiver_dev *djrcv_dev,
530 struct dj_report *dj_report)
532 /* We are called from atomic context (tasklet && djrcv->lock held) */
533 unsigned int i;
534 u8 reportbuffer[MAX_REPORT_SIZE];
535 struct dj_device *djdev;
537 djdev = djrcv_dev->paired_dj_devices[dj_report->device_index];
539 memset(reportbuffer, 0, sizeof(reportbuffer));
541 for (i = 0; i < NUMBER_OF_HID_REPORTS; i++) {
542 if (djdev->reports_supported & (1 << i)) {
543 reportbuffer[0] = i;
544 if (hid_input_report(djdev->hdev,
545 HID_INPUT_REPORT,
546 reportbuffer,
547 hid_reportid_size_map[i], 1)) {
548 dbg_hid("hid_input_report error sending null "
549 "report\n");
555 static void logi_dj_recv_forward_report(struct dj_receiver_dev *djrcv_dev,
556 struct dj_report *dj_report)
558 /* We are called from atomic context (tasklet && djrcv->lock held) */
559 struct dj_device *dj_device;
561 dj_device = djrcv_dev->paired_dj_devices[dj_report->device_index];
563 if ((dj_report->report_type > ARRAY_SIZE(hid_reportid_size_map) - 1) ||
564 (hid_reportid_size_map[dj_report->report_type] == 0)) {
565 dbg_hid("invalid report type:%x\n", dj_report->report_type);
566 return;
569 if (hid_input_report(dj_device->hdev,
570 HID_INPUT_REPORT, &dj_report->report_type,
571 hid_reportid_size_map[dj_report->report_type], 1)) {
572 dbg_hid("hid_input_report error\n");
576 static void logi_dj_recv_forward_hidpp(struct dj_device *dj_dev, u8 *data,
577 int size)
579 /* We are called from atomic context (tasklet && djrcv->lock held) */
580 if (hid_input_report(dj_dev->hdev, HID_INPUT_REPORT, data, size, 1))
581 dbg_hid("hid_input_report error\n");
584 static int logi_dj_recv_send_report(struct dj_receiver_dev *djrcv_dev,
585 struct dj_report *dj_report)
587 struct hid_device *hdev = djrcv_dev->hdev;
588 struct hid_report *report;
589 struct hid_report_enum *output_report_enum;
590 u8 *data = (u8 *)(&dj_report->device_index);
591 unsigned int i;
593 output_report_enum = &hdev->report_enum[HID_OUTPUT_REPORT];
594 report = output_report_enum->report_id_hash[REPORT_ID_DJ_SHORT];
596 if (!report) {
597 dev_err(&hdev->dev, "%s: unable to find dj report\n", __func__);
598 return -ENODEV;
601 for (i = 0; i < DJREPORT_SHORT_LENGTH - 1; i++)
602 report->field[0]->value[i] = data[i];
604 hid_hw_request(hdev, report, HID_REQ_SET_REPORT);
606 return 0;
609 static int logi_dj_recv_query_paired_devices(struct dj_receiver_dev *djrcv_dev)
611 struct dj_report *dj_report;
612 int retval;
614 /* no need to protect djrcv_dev->querying_devices */
615 if (djrcv_dev->querying_devices)
616 return 0;
618 dj_report = kzalloc(sizeof(struct dj_report), GFP_KERNEL);
619 if (!dj_report)
620 return -ENOMEM;
621 dj_report->report_id = REPORT_ID_DJ_SHORT;
622 dj_report->device_index = 0xFF;
623 dj_report->report_type = REPORT_TYPE_CMD_GET_PAIRED_DEVICES;
624 retval = logi_dj_recv_send_report(djrcv_dev, dj_report);
625 kfree(dj_report);
626 return retval;
630 static int logi_dj_recv_switch_to_dj_mode(struct dj_receiver_dev *djrcv_dev,
631 unsigned timeout)
633 struct hid_device *hdev = djrcv_dev->hdev;
634 struct dj_report *dj_report;
635 u8 *buf;
636 int retval;
638 dj_report = kzalloc(sizeof(struct dj_report), GFP_KERNEL);
639 if (!dj_report)
640 return -ENOMEM;
641 dj_report->report_id = REPORT_ID_DJ_SHORT;
642 dj_report->device_index = 0xFF;
643 dj_report->report_type = REPORT_TYPE_CMD_SWITCH;
644 dj_report->report_params[CMD_SWITCH_PARAM_DEVBITFIELD] = 0x3F;
645 dj_report->report_params[CMD_SWITCH_PARAM_TIMEOUT_SECONDS] = (u8)timeout;
646 retval = logi_dj_recv_send_report(djrcv_dev, dj_report);
649 * Ugly sleep to work around a USB 3.0 bug when the receiver is still
650 * processing the "switch-to-dj" command while we send an other command.
651 * 50 msec should gives enough time to the receiver to be ready.
653 msleep(50);
656 * Magical bits to set up hidpp notifications when the dj devices
657 * are connected/disconnected.
659 * We can reuse dj_report because HIDPP_REPORT_SHORT_LENGTH is smaller
660 * than DJREPORT_SHORT_LENGTH.
662 buf = (u8 *)dj_report;
664 memset(buf, 0, HIDPP_REPORT_SHORT_LENGTH);
666 buf[0] = REPORT_ID_HIDPP_SHORT;
667 buf[1] = 0xFF;
668 buf[2] = 0x80;
669 buf[3] = 0x00;
670 buf[4] = 0x00;
671 buf[5] = 0x09;
672 buf[6] = 0x00;
674 hid_hw_raw_request(hdev, REPORT_ID_HIDPP_SHORT, buf,
675 HIDPP_REPORT_SHORT_LENGTH, HID_OUTPUT_REPORT,
676 HID_REQ_SET_REPORT);
678 kfree(dj_report);
679 return retval;
683 static int logi_dj_ll_open(struct hid_device *hid)
685 dbg_hid("%s:%s\n", __func__, hid->phys);
686 return 0;
690 static void logi_dj_ll_close(struct hid_device *hid)
692 dbg_hid("%s:%s\n", __func__, hid->phys);
696 * Register 0xB5 is "pairing information". It is solely intended for the
697 * receiver, so do not overwrite the device index.
699 static u8 unifying_pairing_query[] = {0x10, 0xff, 0x83, 0xb5};
700 static u8 unifying_pairing_answer[] = {0x11, 0xff, 0x83, 0xb5};
702 static int logi_dj_ll_raw_request(struct hid_device *hid,
703 unsigned char reportnum, __u8 *buf,
704 size_t count, unsigned char report_type,
705 int reqtype)
707 struct dj_device *djdev = hid->driver_data;
708 struct dj_receiver_dev *djrcv_dev = djdev->dj_receiver_dev;
709 u8 *out_buf;
710 int ret;
712 if ((buf[0] == REPORT_ID_HIDPP_SHORT) ||
713 (buf[0] == REPORT_ID_HIDPP_LONG)) {
714 if (count < 2)
715 return -EINVAL;
717 /* special case where we should not overwrite
718 * the device_index */
719 if (count == 7 && !memcmp(buf, unifying_pairing_query,
720 sizeof(unifying_pairing_query)))
721 buf[4] = (buf[4] & 0xf0) | (djdev->device_index - 1);
722 else
723 buf[1] = djdev->device_index;
724 return hid_hw_raw_request(djrcv_dev->hdev, reportnum, buf,
725 count, report_type, reqtype);
728 if (buf[0] != REPORT_TYPE_LEDS)
729 return -EINVAL;
731 out_buf = kzalloc(DJREPORT_SHORT_LENGTH, GFP_ATOMIC);
732 if (!out_buf)
733 return -ENOMEM;
735 if (count > DJREPORT_SHORT_LENGTH - 2)
736 count = DJREPORT_SHORT_LENGTH - 2;
738 out_buf[0] = REPORT_ID_DJ_SHORT;
739 out_buf[1] = djdev->device_index;
740 memcpy(out_buf + 2, buf, count);
742 ret = hid_hw_raw_request(djrcv_dev->hdev, out_buf[0], out_buf,
743 DJREPORT_SHORT_LENGTH, report_type, reqtype);
745 kfree(out_buf);
746 return ret;
749 static void rdcat(char *rdesc, unsigned int *rsize, const char *data, unsigned int size)
751 memcpy(rdesc + *rsize, data, size);
752 *rsize += size;
755 static int logi_dj_ll_parse(struct hid_device *hid)
757 struct dj_device *djdev = hid->driver_data;
758 unsigned int rsize = 0;
759 char *rdesc;
760 int retval;
762 dbg_hid("%s\n", __func__);
764 djdev->hdev->version = 0x0111;
765 djdev->hdev->country = 0x00;
767 rdesc = kmalloc(MAX_RDESC_SIZE, GFP_KERNEL);
768 if (!rdesc)
769 return -ENOMEM;
771 if (djdev->reports_supported & STD_KEYBOARD) {
772 dbg_hid("%s: sending a kbd descriptor, reports_supported: %x\n",
773 __func__, djdev->reports_supported);
774 rdcat(rdesc, &rsize, kbd_descriptor, sizeof(kbd_descriptor));
777 if (djdev->reports_supported & STD_MOUSE) {
778 dbg_hid("%s: sending a mouse descriptor, reports_supported: "
779 "%x\n", __func__, djdev->reports_supported);
780 rdcat(rdesc, &rsize, mse_descriptor, sizeof(mse_descriptor));
783 if (djdev->reports_supported & MULTIMEDIA) {
784 dbg_hid("%s: sending a multimedia report descriptor: %x\n",
785 __func__, djdev->reports_supported);
786 rdcat(rdesc, &rsize, consumer_descriptor, sizeof(consumer_descriptor));
789 if (djdev->reports_supported & POWER_KEYS) {
790 dbg_hid("%s: sending a power keys report descriptor: %x\n",
791 __func__, djdev->reports_supported);
792 rdcat(rdesc, &rsize, syscontrol_descriptor, sizeof(syscontrol_descriptor));
795 if (djdev->reports_supported & MEDIA_CENTER) {
796 dbg_hid("%s: sending a media center report descriptor: %x\n",
797 __func__, djdev->reports_supported);
798 rdcat(rdesc, &rsize, media_descriptor, sizeof(media_descriptor));
801 if (djdev->reports_supported & KBD_LEDS) {
802 dbg_hid("%s: need to send kbd leds report descriptor: %x\n",
803 __func__, djdev->reports_supported);
806 rdcat(rdesc, &rsize, hidpp_descriptor, sizeof(hidpp_descriptor));
808 retval = hid_parse_report(hid, rdesc, rsize);
809 kfree(rdesc);
811 return retval;
814 static int logi_dj_ll_start(struct hid_device *hid)
816 dbg_hid("%s\n", __func__);
817 return 0;
820 static void logi_dj_ll_stop(struct hid_device *hid)
822 dbg_hid("%s\n", __func__);
826 static struct hid_ll_driver logi_dj_ll_driver = {
827 .parse = logi_dj_ll_parse,
828 .start = logi_dj_ll_start,
829 .stop = logi_dj_ll_stop,
830 .open = logi_dj_ll_open,
831 .close = logi_dj_ll_close,
832 .raw_request = logi_dj_ll_raw_request,
835 static int logi_dj_dj_event(struct hid_device *hdev,
836 struct hid_report *report, u8 *data,
837 int size)
839 struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
840 struct dj_report *dj_report = (struct dj_report *) data;
841 unsigned long flags;
844 * Here we receive all data coming from iface 2, there are 3 cases:
846 * 1) Data is intended for this driver i. e. data contains arrival,
847 * departure, etc notifications, in which case we queue them for delayed
848 * processing by the work queue. We return 1 to hid-core as no further
849 * processing is required from it.
851 * 2) Data informs a connection change, if the change means rf link
852 * loss, then we must send a null report to the upper layer to discard
853 * potentially pressed keys that may be repeated forever by the input
854 * layer. Return 1 to hid-core as no further processing is required.
856 * 3) Data is an actual input event from a paired DJ device in which
857 * case we forward it to the correct hid device (via hid_input_report()
858 * ) and return 1 so hid-core does not anything else with it.
861 if ((dj_report->device_index < DJ_DEVICE_INDEX_MIN) ||
862 (dj_report->device_index > DJ_DEVICE_INDEX_MAX)) {
864 * Device index is wrong, bail out.
865 * This driver can ignore safely the receiver notifications,
866 * so ignore those reports too.
868 if (dj_report->device_index != DJ_RECEIVER_INDEX)
869 dev_err(&hdev->dev, "%s: invalid device index:%d\n",
870 __func__, dj_report->device_index);
871 return false;
874 spin_lock_irqsave(&djrcv_dev->lock, flags);
876 if (!djrcv_dev->paired_dj_devices[dj_report->device_index]) {
877 /* received an event for an unknown device, bail out */
878 logi_dj_recv_queue_notification(djrcv_dev, dj_report);
879 goto out;
882 switch (dj_report->report_type) {
883 case REPORT_TYPE_NOTIF_DEVICE_PAIRED:
884 /* pairing notifications are handled above the switch */
885 break;
886 case REPORT_TYPE_NOTIF_DEVICE_UNPAIRED:
887 logi_dj_recv_queue_notification(djrcv_dev, dj_report);
888 break;
889 case REPORT_TYPE_NOTIF_CONNECTION_STATUS:
890 if (dj_report->report_params[CONNECTION_STATUS_PARAM_STATUS] ==
891 STATUS_LINKLOSS) {
892 logi_dj_recv_forward_null_report(djrcv_dev, dj_report);
894 break;
895 default:
896 logi_dj_recv_forward_report(djrcv_dev, dj_report);
899 out:
900 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
902 return true;
905 static int logi_dj_hidpp_event(struct hid_device *hdev,
906 struct hid_report *report, u8 *data,
907 int size)
909 struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
910 struct dj_report *dj_report = (struct dj_report *) data;
911 unsigned long flags;
912 u8 device_index = dj_report->device_index;
914 if (device_index == HIDPP_RECEIVER_INDEX) {
915 /* special case were the device wants to know its unifying
916 * name */
917 if (size == HIDPP_REPORT_LONG_LENGTH &&
918 !memcmp(data, unifying_pairing_answer,
919 sizeof(unifying_pairing_answer)))
920 device_index = (data[4] & 0x0F) + 1;
921 else
922 return false;
926 * Data is from the HID++ collection, in this case, we forward the
927 * data to the corresponding child dj device and return 0 to hid-core
928 * so he data also goes to the hidraw device of the receiver. This
929 * allows a user space application to implement the full HID++ routing
930 * via the receiver.
933 if ((device_index < DJ_DEVICE_INDEX_MIN) ||
934 (device_index > DJ_DEVICE_INDEX_MAX)) {
936 * Device index is wrong, bail out.
937 * This driver can ignore safely the receiver notifications,
938 * so ignore those reports too.
940 dev_err(&hdev->dev, "%s: invalid device index:%d\n",
941 __func__, dj_report->device_index);
942 return false;
945 spin_lock_irqsave(&djrcv_dev->lock, flags);
947 if (!djrcv_dev->paired_dj_devices[device_index])
948 /* received an event for an unknown device, bail out */
949 goto out;
951 logi_dj_recv_forward_hidpp(djrcv_dev->paired_dj_devices[device_index],
952 data, size);
954 out:
955 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
957 return false;
960 static int logi_dj_raw_event(struct hid_device *hdev,
961 struct hid_report *report, u8 *data,
962 int size)
964 dbg_hid("%s, size:%d\n", __func__, size);
966 switch (data[0]) {
967 case REPORT_ID_DJ_SHORT:
968 if (size != DJREPORT_SHORT_LENGTH) {
969 dev_err(&hdev->dev, "DJ report of bad size (%d)", size);
970 return false;
972 return logi_dj_dj_event(hdev, report, data, size);
973 case REPORT_ID_HIDPP_SHORT:
974 if (size != HIDPP_REPORT_SHORT_LENGTH) {
975 dev_err(&hdev->dev,
976 "Short HID++ report of bad size (%d)", size);
977 return false;
979 return logi_dj_hidpp_event(hdev, report, data, size);
980 case REPORT_ID_HIDPP_LONG:
981 if (size != HIDPP_REPORT_LONG_LENGTH) {
982 dev_err(&hdev->dev,
983 "Long HID++ report of bad size (%d)", size);
984 return false;
986 return logi_dj_hidpp_event(hdev, report, data, size);
989 return false;
992 static int logi_dj_probe(struct hid_device *hdev,
993 const struct hid_device_id *id)
995 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
996 struct dj_receiver_dev *djrcv_dev;
997 int retval;
999 dbg_hid("%s called for ifnum %d\n", __func__,
1000 intf->cur_altsetting->desc.bInterfaceNumber);
1002 /* Ignore interfaces 0 and 1, they will not carry any data, dont create
1003 * any hid_device for them */
1004 if (intf->cur_altsetting->desc.bInterfaceNumber !=
1005 LOGITECH_DJ_INTERFACE_NUMBER) {
1006 dbg_hid("%s: ignoring ifnum %d\n", __func__,
1007 intf->cur_altsetting->desc.bInterfaceNumber);
1008 return -ENODEV;
1011 /* Treat interface 2 */
1013 djrcv_dev = kzalloc(sizeof(struct dj_receiver_dev), GFP_KERNEL);
1014 if (!djrcv_dev) {
1015 dev_err(&hdev->dev,
1016 "%s:failed allocating dj_receiver_dev\n", __func__);
1017 return -ENOMEM;
1019 djrcv_dev->hdev = hdev;
1020 INIT_WORK(&djrcv_dev->work, delayedwork_callback);
1021 spin_lock_init(&djrcv_dev->lock);
1022 if (kfifo_alloc(&djrcv_dev->notif_fifo,
1023 DJ_MAX_NUMBER_NOTIFICATIONS * sizeof(struct dj_report),
1024 GFP_KERNEL)) {
1025 dev_err(&hdev->dev,
1026 "%s:failed allocating notif_fifo\n", __func__);
1027 kfree(djrcv_dev);
1028 return -ENOMEM;
1030 hid_set_drvdata(hdev, djrcv_dev);
1032 /* Call to usbhid to fetch the HID descriptors of interface 2 and
1033 * subsequently call to the hid/hid-core to parse the fetched
1034 * descriptors, this will in turn create the hidraw and hiddev nodes
1035 * for interface 2 of the receiver */
1036 retval = hid_parse(hdev);
1037 if (retval) {
1038 dev_err(&hdev->dev,
1039 "%s:parse of interface 2 failed\n", __func__);
1040 goto hid_parse_fail;
1043 if (!hid_validate_values(hdev, HID_OUTPUT_REPORT, REPORT_ID_DJ_SHORT,
1044 0, DJREPORT_SHORT_LENGTH - 1)) {
1045 retval = -ENODEV;
1046 goto hid_parse_fail;
1049 /* Starts the usb device and connects to upper interfaces hiddev and
1050 * hidraw */
1051 retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
1052 if (retval) {
1053 dev_err(&hdev->dev,
1054 "%s:hid_hw_start returned error\n", __func__);
1055 goto hid_hw_start_fail;
1058 retval = logi_dj_recv_switch_to_dj_mode(djrcv_dev, 0);
1059 if (retval < 0) {
1060 dev_err(&hdev->dev,
1061 "%s:logi_dj_recv_switch_to_dj_mode returned error:%d\n",
1062 __func__, retval);
1063 goto switch_to_dj_mode_fail;
1066 /* This is enabling the polling urb on the IN endpoint */
1067 retval = hid_hw_open(hdev);
1068 if (retval < 0) {
1069 dev_err(&hdev->dev, "%s:hid_hw_open returned error:%d\n",
1070 __func__, retval);
1071 goto llopen_failed;
1074 /* Allow incoming packets to arrive: */
1075 hid_device_io_start(hdev);
1077 retval = logi_dj_recv_query_paired_devices(djrcv_dev);
1078 if (retval < 0) {
1079 dev_err(&hdev->dev, "%s:logi_dj_recv_query_paired_devices "
1080 "error:%d\n", __func__, retval);
1081 goto logi_dj_recv_query_paired_devices_failed;
1084 return retval;
1086 logi_dj_recv_query_paired_devices_failed:
1087 hid_hw_close(hdev);
1089 llopen_failed:
1090 switch_to_dj_mode_fail:
1091 hid_hw_stop(hdev);
1093 hid_hw_start_fail:
1094 hid_parse_fail:
1095 kfifo_free(&djrcv_dev->notif_fifo);
1096 kfree(djrcv_dev);
1097 hid_set_drvdata(hdev, NULL);
1098 return retval;
1102 #ifdef CONFIG_PM
1103 static int logi_dj_reset_resume(struct hid_device *hdev)
1105 int retval;
1106 struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
1108 retval = logi_dj_recv_switch_to_dj_mode(djrcv_dev, 0);
1109 if (retval < 0) {
1110 dev_err(&hdev->dev,
1111 "%s:logi_dj_recv_switch_to_dj_mode returned error:%d\n",
1112 __func__, retval);
1115 return 0;
1117 #endif
1119 static void logi_dj_remove(struct hid_device *hdev)
1121 struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
1122 struct dj_device *dj_dev;
1123 int i;
1125 dbg_hid("%s\n", __func__);
1127 cancel_work_sync(&djrcv_dev->work);
1129 hid_hw_close(hdev);
1130 hid_hw_stop(hdev);
1132 /* I suppose that at this point the only context that can access
1133 * the djrecv_data is this thread as the work item is guaranteed to
1134 * have finished and no more raw_event callbacks should arrive after
1135 * the remove callback was triggered so no locks are put around the
1136 * code below */
1137 for (i = 0; i < (DJ_MAX_PAIRED_DEVICES + DJ_DEVICE_INDEX_MIN); i++) {
1138 dj_dev = djrcv_dev->paired_dj_devices[i];
1139 if (dj_dev != NULL) {
1140 hid_destroy_device(dj_dev->hdev);
1141 kfree(dj_dev);
1142 djrcv_dev->paired_dj_devices[i] = NULL;
1146 kfifo_free(&djrcv_dev->notif_fifo);
1147 kfree(djrcv_dev);
1148 hid_set_drvdata(hdev, NULL);
1151 static const struct hid_device_id logi_dj_receivers[] = {
1152 {HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
1153 USB_DEVICE_ID_LOGITECH_UNIFYING_RECEIVER)},
1154 {HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
1155 USB_DEVICE_ID_LOGITECH_UNIFYING_RECEIVER_2)},
1159 MODULE_DEVICE_TABLE(hid, logi_dj_receivers);
1161 static struct hid_driver logi_djreceiver_driver = {
1162 .name = "logitech-djreceiver",
1163 .id_table = logi_dj_receivers,
1164 .probe = logi_dj_probe,
1165 .remove = logi_dj_remove,
1166 .raw_event = logi_dj_raw_event,
1167 #ifdef CONFIG_PM
1168 .reset_resume = logi_dj_reset_resume,
1169 #endif
1172 module_hid_driver(logi_djreceiver_driver);
1174 MODULE_LICENSE("GPL");
1175 MODULE_AUTHOR("Logitech");
1176 MODULE_AUTHOR("Nestor Lopez Casado");
1177 MODULE_AUTHOR("nlopezcasad@logitech.com");