]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/hid/hid-logitech-dj.c
HID: logitech-dj: rely on hid groups to separate receivers from dj devices
[mirror_ubuntu-artful-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>
28#include <linux/usb.h>
44d27f7d 29#include <asm/unaligned.h>
534a7b8e
NLC
30#include "hid-ids.h"
31#include "hid-logitech-dj.h"
32
33/* Keyboard descriptor (1) */
34static const char kbd_descriptor[] = {
35 0x05, 0x01, /* USAGE_PAGE (generic Desktop) */
36 0x09, 0x06, /* USAGE (Keyboard) */
37 0xA1, 0x01, /* COLLECTION (Application) */
38 0x85, 0x01, /* REPORT_ID (1) */
39 0x95, 0x08, /* REPORT_COUNT (8) */
40 0x75, 0x01, /* REPORT_SIZE (1) */
41 0x15, 0x00, /* LOGICAL_MINIMUM (0) */
42 0x25, 0x01, /* LOGICAL_MAXIMUM (1) */
43 0x05, 0x07, /* USAGE_PAGE (Keyboard) */
44 0x19, 0xE0, /* USAGE_MINIMUM (Left Control) */
45 0x29, 0xE7, /* USAGE_MAXIMUM (Right GUI) */
46 0x81, 0x02, /* INPUT (Data,Var,Abs) */
534a7b8e
NLC
47 0x95, 0x06, /* REPORT_COUNT (6) */
48 0x75, 0x08, /* REPORT_SIZE (8) */
49 0x15, 0x00, /* LOGICAL_MINIMUM (0) */
50 0x26, 0xFF, 0x00, /* LOGICAL_MAXIMUM (255) */
51 0x05, 0x07, /* USAGE_PAGE (Keyboard) */
52 0x19, 0x00, /* USAGE_MINIMUM (no event) */
53 0x2A, 0xFF, 0x00, /* USAGE_MAXIMUM (reserved) */
54 0x81, 0x00, /* INPUT (Data,Ary,Abs) */
0e40d356
BT
55 0x85, 0x0e, /* REPORT_ID (14) */
56 0x05, 0x08, /* USAGE PAGE (LED page) */
57 0x95, 0x05, /* REPORT COUNT (5) */
58 0x75, 0x01, /* REPORT SIZE (1) */
59 0x15, 0x00, /* LOGICAL_MINIMUM (0) */
60 0x25, 0x01, /* LOGICAL_MAXIMUM (1) */
61 0x19, 0x01, /* USAGE MINIMUM (1) */
62 0x29, 0x05, /* USAGE MAXIMUM (5) */
63 0x91, 0x02, /* OUTPUT (Data, Variable, Absolute) */
64 0x95, 0x01, /* REPORT COUNT (1) */
65 0x75, 0x03, /* REPORT SIZE (3) */
66 0x91, 0x01, /* OUTPUT (Constant) */
534a7b8e
NLC
67 0xC0
68};
69
70/* Mouse descriptor (2) */
71static const char mse_descriptor[] = {
72 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */
73 0x09, 0x02, /* USAGE (Mouse) */
74 0xA1, 0x01, /* COLLECTION (Application) */
75 0x85, 0x02, /* REPORT_ID = 2 */
76 0x09, 0x01, /* USAGE (pointer) */
77 0xA1, 0x00, /* COLLECTION (physical) */
78 0x05, 0x09, /* USAGE_PAGE (buttons) */
79 0x19, 0x01, /* USAGE_MIN (1) */
80 0x29, 0x10, /* USAGE_MAX (16) */
81 0x15, 0x00, /* LOGICAL_MIN (0) */
82 0x25, 0x01, /* LOGICAL_MAX (1) */
83 0x95, 0x10, /* REPORT_COUNT (16) */
84 0x75, 0x01, /* REPORT_SIZE (1) */
85 0x81, 0x02, /* INPUT (data var abs) */
86 0x05, 0x01, /* USAGE_PAGE (generic desktop) */
87 0x16, 0x01, 0xF8, /* LOGICAL_MIN (-2047) */
88 0x26, 0xFF, 0x07, /* LOGICAL_MAX (2047) */
89 0x75, 0x0C, /* REPORT_SIZE (12) */
90 0x95, 0x02, /* REPORT_COUNT (2) */
91 0x09, 0x30, /* USAGE (X) */
92 0x09, 0x31, /* USAGE (Y) */
93 0x81, 0x06, /* INPUT */
94 0x15, 0x81, /* LOGICAL_MIN (-127) */
95 0x25, 0x7F, /* LOGICAL_MAX (127) */
96 0x75, 0x08, /* REPORT_SIZE (8) */
97 0x95, 0x01, /* REPORT_COUNT (1) */
98 0x09, 0x38, /* USAGE (wheel) */
99 0x81, 0x06, /* INPUT */
100 0x05, 0x0C, /* USAGE_PAGE(consumer) */
101 0x0A, 0x38, 0x02, /* USAGE(AC Pan) */
102 0x95, 0x01, /* REPORT_COUNT (1) */
103 0x81, 0x06, /* INPUT */
104 0xC0, /* END_COLLECTION */
105 0xC0, /* END_COLLECTION */
106};
107
108/* Consumer Control descriptor (3) */
109static const char consumer_descriptor[] = {
110 0x05, 0x0C, /* USAGE_PAGE (Consumer Devices) */
111 0x09, 0x01, /* USAGE (Consumer Control) */
112 0xA1, 0x01, /* COLLECTION (Application) */
113 0x85, 0x03, /* REPORT_ID = 3 */
114 0x75, 0x10, /* REPORT_SIZE (16) */
115 0x95, 0x02, /* REPORT_COUNT (2) */
116 0x15, 0x01, /* LOGICAL_MIN (1) */
117 0x26, 0x8C, 0x02, /* LOGICAL_MAX (652) */
118 0x19, 0x01, /* USAGE_MIN (1) */
119 0x2A, 0x8C, 0x02, /* USAGE_MAX (652) */
120 0x81, 0x00, /* INPUT (Data Ary Abs) */
121 0xC0, /* END_COLLECTION */
122}; /* */
123
124/* System control descriptor (4) */
125static const char syscontrol_descriptor[] = {
126 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */
127 0x09, 0x80, /* USAGE (System Control) */
128 0xA1, 0x01, /* COLLECTION (Application) */
129 0x85, 0x04, /* REPORT_ID = 4 */
130 0x75, 0x02, /* REPORT_SIZE (2) */
131 0x95, 0x01, /* REPORT_COUNT (1) */
132 0x15, 0x01, /* LOGICAL_MIN (1) */
133 0x25, 0x03, /* LOGICAL_MAX (3) */
134 0x09, 0x82, /* USAGE (System Sleep) */
135 0x09, 0x81, /* USAGE (System Power Down) */
136 0x09, 0x83, /* USAGE (System Wake Up) */
137 0x81, 0x60, /* INPUT (Data Ary Abs NPrf Null) */
138 0x75, 0x06, /* REPORT_SIZE (6) */
139 0x81, 0x03, /* INPUT (Cnst Var Abs) */
140 0xC0, /* END_COLLECTION */
141};
142
143/* Media descriptor (8) */
144static const char media_descriptor[] = {
145 0x06, 0xbc, 0xff, /* Usage Page 0xffbc */
146 0x09, 0x88, /* Usage 0x0088 */
147 0xa1, 0x01, /* BeginCollection */
148 0x85, 0x08, /* Report ID 8 */
149 0x19, 0x01, /* Usage Min 0x0001 */
150 0x29, 0xff, /* Usage Max 0x00ff */
151 0x15, 0x01, /* Logical Min 1 */
152 0x26, 0xff, 0x00, /* Logical Max 255 */
153 0x75, 0x08, /* Report Size 8 */
154 0x95, 0x01, /* Report Count 1 */
155 0x81, 0x00, /* Input */
156 0xc0, /* EndCollection */
157}; /* */
158
159/* Maximum size of all defined hid reports in bytes (including report id) */
160#define MAX_REPORT_SIZE 8
161
2a039bf5
HR
162/* Make sure all descriptors are present here */
163#define MAX_RDESC_SIZE \
164 (sizeof(kbd_descriptor) + \
165 sizeof(mse_descriptor) + \
166 sizeof(consumer_descriptor) + \
167 sizeof(syscontrol_descriptor) + \
168 sizeof(media_descriptor))
169
534a7b8e
NLC
170/* Number of possible hid report types that can be created by this driver.
171 *
172 * Right now, RF report types have the same report types (or report id's)
173 * than the hid report created from those RF reports. In the future
174 * this doesnt have to be true.
175 *
176 * For instance, RF report type 0x01 which has a size of 8 bytes, corresponds
177 * to hid report id 0x01, this is standard keyboard. Same thing applies to mice
178 * reports and consumer control, etc. If a new RF report is created, it doesn't
179 * has to have the same report id as its corresponding hid report, so an
180 * translation may have to take place for future report types.
181 */
182#define NUMBER_OF_HID_REPORTS 32
183static const u8 hid_reportid_size_map[NUMBER_OF_HID_REPORTS] = {
184 [1] = 8, /* Standard keyboard */
185 [2] = 8, /* Standard mouse */
186 [3] = 5, /* Consumer control */
187 [4] = 2, /* System control */
188 [8] = 2, /* Media Center */
189};
190
191
192#define LOGITECH_DJ_INTERFACE_NUMBER 0x02
193
534a7b8e
NLC
194static struct hid_ll_driver logi_dj_ll_driver;
195
c63e0e37 196static int logi_dj_recv_query_paired_devices(struct dj_receiver_dev *djrcv_dev);
534a7b8e
NLC
197
198static void logi_dj_recv_destroy_djhid_device(struct dj_receiver_dev *djrcv_dev,
199 struct dj_report *dj_report)
200{
201 /* Called in delayed work context */
202 struct dj_device *dj_dev;
203 unsigned long flags;
204
205 spin_lock_irqsave(&djrcv_dev->lock, flags);
206 dj_dev = djrcv_dev->paired_dj_devices[dj_report->device_index];
207 djrcv_dev->paired_dj_devices[dj_report->device_index] = NULL;
208 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
209
210 if (dj_dev != NULL) {
211 hid_destroy_device(dj_dev->hdev);
212 kfree(dj_dev);
213 } else {
214 dev_err(&djrcv_dev->hdev->dev, "%s: can't destroy a NULL device\n",
215 __func__);
216 }
217}
218
219static void logi_dj_recv_add_djhid_device(struct dj_receiver_dev *djrcv_dev,
220 struct dj_report *dj_report)
221{
222 /* Called in delayed work context */
223 struct hid_device *djrcv_hdev = djrcv_dev->hdev;
224 struct usb_interface *intf = to_usb_interface(djrcv_hdev->dev.parent);
225 struct usb_device *usbdev = interface_to_usbdev(intf);
226 struct hid_device *dj_hiddev;
227 struct dj_device *dj_dev;
228
229 /* Device index goes from 1 to 6, we need 3 bytes to store the
230 * semicolon, the index, and a null terminator
231 */
232 unsigned char tmpstr[3];
233
234 if (dj_report->report_params[DEVICE_PAIRED_PARAM_SPFUNCTION] &
235 SPFUNCTION_DEVICE_LIST_EMPTY) {
236 dbg_hid("%s: device list is empty\n", __func__);
c63e0e37 237 djrcv_dev->querying_devices = false;
534a7b8e
NLC
238 return;
239 }
240
c63e0e37
NLC
241 if (djrcv_dev->paired_dj_devices[dj_report->device_index]) {
242 /* The device is already known. No need to reallocate it. */
243 dbg_hid("%s: device is already known\n", __func__);
244 return;
245 }
246
534a7b8e
NLC
247 dj_hiddev = hid_allocate_device();
248 if (IS_ERR(dj_hiddev)) {
249 dev_err(&djrcv_hdev->dev, "%s: hid_allocate_device failed\n",
250 __func__);
251 return;
252 }
253
254 dj_hiddev->ll_driver = &logi_dj_ll_driver;
534a7b8e
NLC
255
256 dj_hiddev->dev.parent = &djrcv_hdev->dev;
257 dj_hiddev->bus = BUS_USB;
258 dj_hiddev->vendor = le16_to_cpu(usbdev->descriptor.idVendor);
d610274b
BT
259 dj_hiddev->product =
260 (dj_report->report_params[DEVICE_PAIRED_PARAM_EQUAD_ID_MSB]
261 << 8) |
262 dj_report->report_params[DEVICE_PAIRED_PARAM_EQUAD_ID_LSB];
534a7b8e 263 snprintf(dj_hiddev->name, sizeof(dj_hiddev->name),
d610274b
BT
264 "Logitech Unifying Device. Wireless PID:%04x",
265 dj_hiddev->product);
266
267 dj_hiddev->group = HID_GROUP_LOGITECH_DJ_DEVICE;
534a7b8e
NLC
268
269 usb_make_path(usbdev, dj_hiddev->phys, sizeof(dj_hiddev->phys));
270 snprintf(tmpstr, sizeof(tmpstr), ":%d", dj_report->device_index);
271 strlcat(dj_hiddev->phys, tmpstr, sizeof(dj_hiddev->phys));
272
273 dj_dev = kzalloc(sizeof(struct dj_device), GFP_KERNEL);
274
275 if (!dj_dev) {
276 dev_err(&djrcv_hdev->dev, "%s: failed allocating dj_device\n",
277 __func__);
278 goto dj_device_allocate_fail;
279 }
280
44d27f7d
JN
281 dj_dev->reports_supported = get_unaligned_le32(
282 dj_report->report_params + DEVICE_PAIRED_RF_REPORT_TYPE);
534a7b8e
NLC
283 dj_dev->hdev = dj_hiddev;
284 dj_dev->dj_receiver_dev = djrcv_dev;
285 dj_dev->device_index = dj_report->device_index;
286 dj_hiddev->driver_data = dj_dev;
287
288 djrcv_dev->paired_dj_devices[dj_report->device_index] = dj_dev;
289
290 if (hid_add_device(dj_hiddev)) {
291 dev_err(&djrcv_hdev->dev, "%s: failed adding dj_device\n",
292 __func__);
293 goto hid_add_device_fail;
294 }
295
296 return;
297
298hid_add_device_fail:
299 djrcv_dev->paired_dj_devices[dj_report->device_index] = NULL;
300 kfree(dj_dev);
301dj_device_allocate_fail:
302 hid_destroy_device(dj_hiddev);
303}
304
305static void delayedwork_callback(struct work_struct *work)
306{
307 struct dj_receiver_dev *djrcv_dev =
308 container_of(work, struct dj_receiver_dev, work);
309
310 struct dj_report dj_report;
311 unsigned long flags;
312 int count;
c63e0e37 313 int retval;
534a7b8e
NLC
314
315 dbg_hid("%s\n", __func__);
316
317 spin_lock_irqsave(&djrcv_dev->lock, flags);
318
319 count = kfifo_out(&djrcv_dev->notif_fifo, &dj_report,
320 sizeof(struct dj_report));
321
322 if (count != sizeof(struct dj_report)) {
323 dev_err(&djrcv_dev->hdev->dev, "%s: workitem triggered without "
324 "notifications available\n", __func__);
325 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
326 return;
327 }
328
329 if (!kfifo_is_empty(&djrcv_dev->notif_fifo)) {
330 if (schedule_work(&djrcv_dev->work) == 0) {
331 dbg_hid("%s: did not schedule the work item, was "
332 "already queued\n", __func__);
333 }
334 }
335
336 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
337
338 switch (dj_report.report_type) {
339 case REPORT_TYPE_NOTIF_DEVICE_PAIRED:
340 logi_dj_recv_add_djhid_device(djrcv_dev, &dj_report);
341 break;
342 case REPORT_TYPE_NOTIF_DEVICE_UNPAIRED:
343 logi_dj_recv_destroy_djhid_device(djrcv_dev, &dj_report);
344 break;
345 default:
c63e0e37
NLC
346 /* A normal report (i. e. not belonging to a pair/unpair notification)
347 * arriving here, means that the report arrived but we did not have a
348 * paired dj_device associated to the report's device_index, this
349 * means that the original "device paired" notification corresponding
350 * to this dj_device never arrived to this driver. The reason is that
351 * hid-core discards all packets coming from a device while probe() is
352 * executing. */
353 if (!djrcv_dev->paired_dj_devices[dj_report.device_index]) {
354 /* ok, we don't know the device, just re-ask the
355 * receiver for the list of connected devices. */
356 retval = logi_dj_recv_query_paired_devices(djrcv_dev);
357 if (!retval) {
358 /* everything went fine, so just leave */
359 break;
360 }
361 dev_err(&djrcv_dev->hdev->dev,
362 "%s:logi_dj_recv_query_paired_devices "
363 "error:%d\n", __func__, retval);
364 }
534a7b8e
NLC
365 dbg_hid("%s: unexpected report type\n", __func__);
366 }
367}
368
369static void logi_dj_recv_queue_notification(struct dj_receiver_dev *djrcv_dev,
370 struct dj_report *dj_report)
371{
372 /* We are called from atomic context (tasklet && djrcv->lock held) */
373
374 kfifo_in(&djrcv_dev->notif_fifo, dj_report, sizeof(struct dj_report));
375
376 if (schedule_work(&djrcv_dev->work) == 0) {
377 dbg_hid("%s: did not schedule the work item, was already "
378 "queued\n", __func__);
379 }
380}
381
382static void logi_dj_recv_forward_null_report(struct dj_receiver_dev *djrcv_dev,
383 struct dj_report *dj_report)
384{
385 /* We are called from atomic context (tasklet && djrcv->lock held) */
386 unsigned int i;
387 u8 reportbuffer[MAX_REPORT_SIZE];
388 struct dj_device *djdev;
389
390 djdev = djrcv_dev->paired_dj_devices[dj_report->device_index];
391
534a7b8e
NLC
392 memset(reportbuffer, 0, sizeof(reportbuffer));
393
394 for (i = 0; i < NUMBER_OF_HID_REPORTS; i++) {
395 if (djdev->reports_supported & (1 << i)) {
396 reportbuffer[0] = i;
397 if (hid_input_report(djdev->hdev,
398 HID_INPUT_REPORT,
399 reportbuffer,
400 hid_reportid_size_map[i], 1)) {
401 dbg_hid("hid_input_report error sending null "
402 "report\n");
403 }
404 }
405 }
406}
407
408static void logi_dj_recv_forward_report(struct dj_receiver_dev *djrcv_dev,
409 struct dj_report *dj_report)
410{
411 /* We are called from atomic context (tasklet && djrcv->lock held) */
412 struct dj_device *dj_device;
413
414 dj_device = djrcv_dev->paired_dj_devices[dj_report->device_index];
415
534a7b8e
NLC
416 if ((dj_report->report_type > ARRAY_SIZE(hid_reportid_size_map) - 1) ||
417 (hid_reportid_size_map[dj_report->report_type] == 0)) {
418 dbg_hid("invalid report type:%x\n", dj_report->report_type);
419 return;
420 }
421
422 if (hid_input_report(dj_device->hdev,
423 HID_INPUT_REPORT, &dj_report->report_type,
424 hid_reportid_size_map[dj_report->report_type], 1)) {
425 dbg_hid("hid_input_report error\n");
426 }
427}
428
429
430static int logi_dj_recv_send_report(struct dj_receiver_dev *djrcv_dev,
431 struct dj_report *dj_report)
432{
433 struct hid_device *hdev = djrcv_dev->hdev;
dcd9006b
BT
434 struct hid_report *report;
435 struct hid_report_enum *output_report_enum;
436 u8 *data = (u8 *)(&dj_report->device_index);
297502ab 437 unsigned int i;
534a7b8e 438
dcd9006b
BT
439 output_report_enum = &hdev->report_enum[HID_OUTPUT_REPORT];
440 report = output_report_enum->report_id_hash[REPORT_ID_DJ_SHORT];
441
442 if (!report) {
443 dev_err(&hdev->dev, "%s: unable to find dj report\n", __func__);
534a7b8e
NLC
444 return -ENODEV;
445 }
446
297502ab 447 for (i = 0; i < DJREPORT_SHORT_LENGTH - 1; i++)
dcd9006b
BT
448 report->field[0]->value[i] = data[i];
449
83a44ac8 450 hid_hw_request(hdev, report, HID_REQ_SET_REPORT);
534a7b8e 451
dcd9006b 452 return 0;
534a7b8e
NLC
453}
454
455static int logi_dj_recv_query_paired_devices(struct dj_receiver_dev *djrcv_dev)
456{
d8dc3494
MD
457 struct dj_report *dj_report;
458 int retval;
534a7b8e 459
c63e0e37
NLC
460 /* no need to protect djrcv_dev->querying_devices */
461 if (djrcv_dev->querying_devices)
462 return 0;
463
8a55ade7 464 dj_report = kzalloc(sizeof(struct dj_report), GFP_KERNEL);
d8dc3494
MD
465 if (!dj_report)
466 return -ENOMEM;
467 dj_report->report_id = REPORT_ID_DJ_SHORT;
468 dj_report->device_index = 0xFF;
469 dj_report->report_type = REPORT_TYPE_CMD_GET_PAIRED_DEVICES;
470 retval = logi_dj_recv_send_report(djrcv_dev, dj_report);
471 kfree(dj_report);
472 return retval;
534a7b8e
NLC
473}
474
c63e0e37 475
534a7b8e
NLC
476static int logi_dj_recv_switch_to_dj_mode(struct dj_receiver_dev *djrcv_dev,
477 unsigned timeout)
478{
d8dc3494
MD
479 struct dj_report *dj_report;
480 int retval;
534a7b8e 481
8a55ade7 482 dj_report = kzalloc(sizeof(struct dj_report), GFP_KERNEL);
d8dc3494
MD
483 if (!dj_report)
484 return -ENOMEM;
485 dj_report->report_id = REPORT_ID_DJ_SHORT;
486 dj_report->device_index = 0xFF;
487 dj_report->report_type = REPORT_TYPE_CMD_SWITCH;
488 dj_report->report_params[CMD_SWITCH_PARAM_DEVBITFIELD] = 0x3F;
489 dj_report->report_params[CMD_SWITCH_PARAM_TIMEOUT_SECONDS] = (u8)timeout;
490 retval = logi_dj_recv_send_report(djrcv_dev, dj_report);
491 kfree(dj_report);
42c22dbf
BT
492
493 /*
494 * Ugly sleep to work around a USB 3.0 bug when the receiver is still
495 * processing the "switch-to-dj" command while we send an other command.
496 * 50 msec should gives enough time to the receiver to be ready.
497 */
498 msleep(50);
499
d8dc3494 500 return retval;
534a7b8e
NLC
501}
502
503
504static int logi_dj_ll_open(struct hid_device *hid)
505{
506 dbg_hid("%s:%s\n", __func__, hid->phys);
507 return 0;
508
509}
510
511static void logi_dj_ll_close(struct hid_device *hid)
512{
513 dbg_hid("%s:%s\n", __func__, hid->phys);
514}
515
bd27e202
BT
516static int logi_dj_ll_raw_request(struct hid_device *hid,
517 unsigned char reportnum, __u8 *buf,
518 size_t count, unsigned char report_type,
519 int reqtype)
534a7b8e 520{
0e40d356
BT
521 struct dj_device *djdev = hid->driver_data;
522 struct dj_receiver_dev *djrcv_dev = djdev->dj_receiver_dev;
523 u8 *out_buf;
524 int ret;
534a7b8e 525
0e40d356
BT
526 if (buf[0] != REPORT_TYPE_LEDS)
527 return -EINVAL;
528
529 out_buf = kzalloc(DJREPORT_SHORT_LENGTH, GFP_ATOMIC);
530 if (!out_buf)
531 return -ENOMEM;
532
51217e69 533 if (count > DJREPORT_SHORT_LENGTH - 2)
0e40d356
BT
534 count = DJREPORT_SHORT_LENGTH - 2;
535
536 out_buf[0] = REPORT_ID_DJ_SHORT;
537 out_buf[1] = djdev->device_index;
538 memcpy(out_buf + 2, buf, count);
539
0e40d356 540 ret = hid_hw_raw_request(djrcv_dev->hdev, out_buf[0], out_buf,
bd27e202 541 DJREPORT_SHORT_LENGTH, report_type, reqtype);
0e40d356
BT
542
543 kfree(out_buf);
544 return ret;
534a7b8e
NLC
545}
546
6d603326 547static void rdcat(char *rdesc, unsigned int *rsize, const char *data, unsigned int size)
2a039bf5 548{
6d603326 549 memcpy(rdesc + *rsize, data, size);
2a039bf5
HR
550 *rsize += size;
551}
552
534a7b8e
NLC
553static int logi_dj_ll_parse(struct hid_device *hid)
554{
555 struct dj_device *djdev = hid->driver_data;
2a039bf5
HR
556 unsigned int rsize = 0;
557 char *rdesc;
534a7b8e
NLC
558 int retval;
559
560 dbg_hid("%s\n", __func__);
561
562 djdev->hdev->version = 0x0111;
563 djdev->hdev->country = 0x00;
564
2a039bf5
HR
565 rdesc = kmalloc(MAX_RDESC_SIZE, GFP_KERNEL);
566 if (!rdesc)
567 return -ENOMEM;
568
534a7b8e
NLC
569 if (djdev->reports_supported & STD_KEYBOARD) {
570 dbg_hid("%s: sending a kbd descriptor, reports_supported: %x\n",
571 __func__, djdev->reports_supported);
6d603326 572 rdcat(rdesc, &rsize, kbd_descriptor, sizeof(kbd_descriptor));
534a7b8e
NLC
573 }
574
575 if (djdev->reports_supported & STD_MOUSE) {
576 dbg_hid("%s: sending a mouse descriptor, reports_supported: "
577 "%x\n", __func__, djdev->reports_supported);
6d603326 578 rdcat(rdesc, &rsize, mse_descriptor, sizeof(mse_descriptor));
534a7b8e
NLC
579 }
580
581 if (djdev->reports_supported & MULTIMEDIA) {
582 dbg_hid("%s: sending a multimedia report descriptor: %x\n",
583 __func__, djdev->reports_supported);
6d603326 584 rdcat(rdesc, &rsize, consumer_descriptor, sizeof(consumer_descriptor));
534a7b8e
NLC
585 }
586
587 if (djdev->reports_supported & POWER_KEYS) {
588 dbg_hid("%s: sending a power keys report descriptor: %x\n",
589 __func__, djdev->reports_supported);
6d603326 590 rdcat(rdesc, &rsize, syscontrol_descriptor, sizeof(syscontrol_descriptor));
534a7b8e
NLC
591 }
592
593 if (djdev->reports_supported & MEDIA_CENTER) {
594 dbg_hid("%s: sending a media center report descriptor: %x\n",
595 __func__, djdev->reports_supported);
6d603326 596 rdcat(rdesc, &rsize, media_descriptor, sizeof(media_descriptor));
534a7b8e
NLC
597 }
598
599 if (djdev->reports_supported & KBD_LEDS) {
600 dbg_hid("%s: need to send kbd leds report descriptor: %x\n",
601 __func__, djdev->reports_supported);
602 }
603
2a039bf5
HR
604 retval = hid_parse_report(hid, rdesc, rsize);
605 kfree(rdesc);
606
607 return retval;
534a7b8e
NLC
608}
609
534a7b8e
NLC
610static int logi_dj_ll_start(struct hid_device *hid)
611{
612 dbg_hid("%s\n", __func__);
613 return 0;
614}
615
616static void logi_dj_ll_stop(struct hid_device *hid)
617{
618 dbg_hid("%s\n", __func__);
619}
620
621
622static struct hid_ll_driver logi_dj_ll_driver = {
623 .parse = logi_dj_ll_parse,
624 .start = logi_dj_ll_start,
625 .stop = logi_dj_ll_stop,
626 .open = logi_dj_ll_open,
627 .close = logi_dj_ll_close,
bd27e202 628 .raw_request = logi_dj_ll_raw_request,
534a7b8e
NLC
629};
630
631
632static int logi_dj_raw_event(struct hid_device *hdev,
633 struct hid_report *report, u8 *data,
634 int size)
635{
636 struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
637 struct dj_report *dj_report = (struct dj_report *) data;
638 unsigned long flags;
534a7b8e
NLC
639
640 dbg_hid("%s, size:%d\n", __func__, size);
641
642 /* Here we receive all data coming from iface 2, there are 4 cases:
643 *
644 * 1) Data should continue its normal processing i.e. data does not
645 * come from the DJ collection, in which case we do nothing and
646 * return 0, so hid-core can continue normal processing (will forward
647 * to associated hidraw device)
648 *
649 * 2) Data is from DJ collection, and is intended for this driver i. e.
650 * data contains arrival, departure, etc notifications, in which case
651 * we queue them for delayed processing by the work queue. We return 1
652 * to hid-core as no further processing is required from it.
653 *
654 * 3) Data is from DJ collection, and informs a connection change,
655 * if the change means rf link loss, then we must send a null report
656 * to the upper layer to discard potentially pressed keys that may be
657 * repeated forever by the input layer. Return 1 to hid-core as no
658 * further processing is required.
659 *
660 * 4) Data is from DJ collection and is an actual input event from
661 * a paired DJ device in which case we forward it to the correct hid
662 * device (via hid_input_report() ) and return 1 so hid-core does not do
663 * anything else with it.
664 */
5abfe85c
BT
665
666 /* case 1) */
667 if (data[0] != REPORT_ID_DJ_SHORT)
668 return false;
669
ad3e14d7
JK
670 if ((dj_report->device_index < DJ_DEVICE_INDEX_MIN) ||
671 (dj_report->device_index > DJ_DEVICE_INDEX_MAX)) {
5abfe85c
BT
672 /*
673 * Device index is wrong, bail out.
674 * This driver can ignore safely the receiver notifications,
675 * so ignore those reports too.
676 */
677 if (dj_report->device_index != DJ_RECEIVER_INDEX)
678 dev_err(&hdev->dev, "%s: invalid device index:%d\n",
ad3e14d7
JK
679 __func__, dj_report->device_index);
680 return false;
681 }
534a7b8e
NLC
682
683 spin_lock_irqsave(&djrcv_dev->lock, flags);
368d4e59
BT
684
685 if (!djrcv_dev->paired_dj_devices[dj_report->device_index]) {
686 /* received an event for an unknown device, bail out */
687 logi_dj_recv_queue_notification(djrcv_dev, dj_report);
688 goto out;
689 }
690
5abfe85c
BT
691 switch (dj_report->report_type) {
692 case REPORT_TYPE_NOTIF_DEVICE_PAIRED:
368d4e59
BT
693 /* pairing notifications are handled above the switch */
694 break;
5abfe85c
BT
695 case REPORT_TYPE_NOTIF_DEVICE_UNPAIRED:
696 logi_dj_recv_queue_notification(djrcv_dev, dj_report);
697 break;
698 case REPORT_TYPE_NOTIF_CONNECTION_STATUS:
699 if (dj_report->report_params[CONNECTION_STATUS_PARAM_STATUS] ==
700 STATUS_LINKLOSS) {
701 logi_dj_recv_forward_null_report(djrcv_dev, dj_report);
534a7b8e 702 }
5abfe85c
BT
703 break;
704 default:
705 logi_dj_recv_forward_report(djrcv_dev, dj_report);
534a7b8e 706 }
368d4e59
BT
707
708out:
534a7b8e
NLC
709 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
710
5abfe85c 711 return true;
534a7b8e
NLC
712}
713
714static int logi_dj_probe(struct hid_device *hdev,
715 const struct hid_device_id *id)
716{
717 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
718 struct dj_receiver_dev *djrcv_dev;
719 int retval;
720
534a7b8e
NLC
721 dbg_hid("%s called for ifnum %d\n", __func__,
722 intf->cur_altsetting->desc.bInterfaceNumber);
723
724 /* Ignore interfaces 0 and 1, they will not carry any data, dont create
725 * any hid_device for them */
726 if (intf->cur_altsetting->desc.bInterfaceNumber !=
727 LOGITECH_DJ_INTERFACE_NUMBER) {
728 dbg_hid("%s: ignoring ifnum %d\n", __func__,
729 intf->cur_altsetting->desc.bInterfaceNumber);
730 return -ENODEV;
731 }
732
733 /* Treat interface 2 */
734
735 djrcv_dev = kzalloc(sizeof(struct dj_receiver_dev), GFP_KERNEL);
736 if (!djrcv_dev) {
737 dev_err(&hdev->dev,
738 "%s:failed allocating dj_receiver_dev\n", __func__);
739 return -ENOMEM;
740 }
741 djrcv_dev->hdev = hdev;
742 INIT_WORK(&djrcv_dev->work, delayedwork_callback);
743 spin_lock_init(&djrcv_dev->lock);
744 if (kfifo_alloc(&djrcv_dev->notif_fifo,
745 DJ_MAX_NUMBER_NOTIFICATIONS * sizeof(struct dj_report),
746 GFP_KERNEL)) {
747 dev_err(&hdev->dev,
748 "%s:failed allocating notif_fifo\n", __func__);
749 kfree(djrcv_dev);
750 return -ENOMEM;
751 }
752 hid_set_drvdata(hdev, djrcv_dev);
753
754 /* Call to usbhid to fetch the HID descriptors of interface 2 and
755 * subsequently call to the hid/hid-core to parse the fetched
756 * descriptors, this will in turn create the hidraw and hiddev nodes
757 * for interface 2 of the receiver */
758 retval = hid_parse(hdev);
759 if (retval) {
760 dev_err(&hdev->dev,
761 "%s:parse of interface 2 failed\n", __func__);
762 goto hid_parse_fail;
763 }
764
297502ab
KC
765 if (!hid_validate_values(hdev, HID_OUTPUT_REPORT, REPORT_ID_DJ_SHORT,
766 0, DJREPORT_SHORT_LENGTH - 1)) {
767 retval = -ENODEV;
768 goto hid_parse_fail;
769 }
770
534a7b8e
NLC
771 /* Starts the usb device and connects to upper interfaces hiddev and
772 * hidraw */
773 retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
774 if (retval) {
775 dev_err(&hdev->dev,
776 "%s:hid_hw_start returned error\n", __func__);
777 goto hid_hw_start_fail;
778 }
779
780 retval = logi_dj_recv_switch_to_dj_mode(djrcv_dev, 0);
781 if (retval < 0) {
782 dev_err(&hdev->dev,
783 "%s:logi_dj_recv_switch_to_dj_mode returned error:%d\n",
784 __func__, retval);
785 goto switch_to_dj_mode_fail;
786 }
787
788 /* This is enabling the polling urb on the IN endpoint */
ddf7540e 789 retval = hid_hw_open(hdev);
534a7b8e 790 if (retval < 0) {
ddf7540e
BT
791 dev_err(&hdev->dev, "%s:hid_hw_open returned error:%d\n",
792 __func__, retval);
534a7b8e
NLC
793 goto llopen_failed;
794 }
795
a9dd22b7
AR
796 /* Allow incoming packets to arrive: */
797 hid_device_io_start(hdev);
798
534a7b8e
NLC
799 retval = logi_dj_recv_query_paired_devices(djrcv_dev);
800 if (retval < 0) {
801 dev_err(&hdev->dev, "%s:logi_dj_recv_query_paired_devices "
802 "error:%d\n", __func__, retval);
803 goto logi_dj_recv_query_paired_devices_failed;
804 }
805
806 return retval;
807
808logi_dj_recv_query_paired_devices_failed:
ddf7540e 809 hid_hw_close(hdev);
534a7b8e
NLC
810
811llopen_failed:
812switch_to_dj_mode_fail:
813 hid_hw_stop(hdev);
814
815hid_hw_start_fail:
816hid_parse_fail:
817 kfifo_free(&djrcv_dev->notif_fifo);
818 kfree(djrcv_dev);
819 hid_set_drvdata(hdev, NULL);
820 return retval;
821
822}
823
824#ifdef CONFIG_PM
825static int logi_dj_reset_resume(struct hid_device *hdev)
826{
827 int retval;
828 struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
829
830 retval = logi_dj_recv_switch_to_dj_mode(djrcv_dev, 0);
831 if (retval < 0) {
832 dev_err(&hdev->dev,
833 "%s:logi_dj_recv_switch_to_dj_mode returned error:%d\n",
834 __func__, retval);
835 }
836
837 return 0;
838}
839#endif
840
841static void logi_dj_remove(struct hid_device *hdev)
842{
843 struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
844 struct dj_device *dj_dev;
845 int i;
846
847 dbg_hid("%s\n", __func__);
848
849 cancel_work_sync(&djrcv_dev->work);
850
ddf7540e 851 hid_hw_close(hdev);
534a7b8e
NLC
852 hid_hw_stop(hdev);
853
854 /* I suppose that at this point the only context that can access
855 * the djrecv_data is this thread as the work item is guaranteed to
856 * have finished and no more raw_event callbacks should arrive after
857 * the remove callback was triggered so no locks are put around the
858 * code below */
844580ff 859 for (i = 0; i < (DJ_MAX_PAIRED_DEVICES + DJ_DEVICE_INDEX_MIN); i++) {
534a7b8e
NLC
860 dj_dev = djrcv_dev->paired_dj_devices[i];
861 if (dj_dev != NULL) {
862 hid_destroy_device(dj_dev->hdev);
863 kfree(dj_dev);
864 djrcv_dev->paired_dj_devices[i] = NULL;
865 }
866 }
867
868 kfifo_free(&djrcv_dev->notif_fifo);
869 kfree(djrcv_dev);
870 hid_set_drvdata(hdev, NULL);
871}
872
534a7b8e
NLC
873static const struct hid_device_id logi_dj_receivers[] = {
874 {HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
875 USB_DEVICE_ID_LOGITECH_UNIFYING_RECEIVER)},
876 {HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
877 USB_DEVICE_ID_LOGITECH_UNIFYING_RECEIVER_2)},
878 {}
879};
880
881MODULE_DEVICE_TABLE(hid, logi_dj_receivers);
882
883static struct hid_driver logi_djreceiver_driver = {
884 .name = "logitech-djreceiver",
885 .id_table = logi_dj_receivers,
886 .probe = logi_dj_probe,
887 .remove = logi_dj_remove,
888 .raw_event = logi_dj_raw_event,
889#ifdef CONFIG_PM
890 .reset_resume = logi_dj_reset_resume,
891#endif
892};
893
894
895static const struct hid_device_id logi_dj_devices[] = {
d610274b
BT
896 { HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE,
897 USB_VENDOR_ID_LOGITECH, HID_ANY_ID)},
534a7b8e
NLC
898 {}
899};
900
901static struct hid_driver logi_djdevice_driver = {
902 .name = "logitech-djdevice",
903 .id_table = logi_dj_devices,
534a7b8e
NLC
904};
905
906
907static int __init logi_dj_init(void)
908{
909 int retval;
910
911 dbg_hid("Logitech-DJ:%s\n", __func__);
912
913 retval = hid_register_driver(&logi_djreceiver_driver);
914 if (retval)
915 return retval;
916
917 retval = hid_register_driver(&logi_djdevice_driver);
918 if (retval)
919 hid_unregister_driver(&logi_djreceiver_driver);
920
921 return retval;
922
923}
924
925static void __exit logi_dj_exit(void)
926{
927 dbg_hid("Logitech-DJ:%s\n", __func__);
928
929 hid_unregister_driver(&logi_djdevice_driver);
930 hid_unregister_driver(&logi_djreceiver_driver);
931
932}
933
934module_init(logi_dj_init);
935module_exit(logi_dj_exit);
936MODULE_LICENSE("GPL");
937MODULE_AUTHOR("Logitech");
938MODULE_AUTHOR("Nestor Lopez Casado");
939MODULE_AUTHOR("nlopezcasad@logitech.com");