blob: f45279c3b11a9f241f6b0f28d1565ef965c04587 [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>
28#include <linux/usb.h>
Jonathan Nieder44d27f72012-05-11 16:17:16 +020029#include <asm/unaligned.h>
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +020030#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) */
47 0x95, 0x05, /* REPORT COUNT (5) */
48 0x05, 0x08, /* USAGE PAGE (LED page) */
49 0x19, 0x01, /* USAGE MINIMUM (1) */
50 0x29, 0x05, /* USAGE MAXIMUM (5) */
51 0x91, 0x02, /* OUTPUT (Data, Variable, Absolute) */
52 0x95, 0x01, /* REPORT COUNT (1) */
53 0x75, 0x03, /* REPORT SIZE (3) */
54 0x91, 0x01, /* OUTPUT (Constant) */
55 0x95, 0x06, /* REPORT_COUNT (6) */
56 0x75, 0x08, /* REPORT_SIZE (8) */
57 0x15, 0x00, /* LOGICAL_MINIMUM (0) */
58 0x26, 0xFF, 0x00, /* LOGICAL_MAXIMUM (255) */
59 0x05, 0x07, /* USAGE_PAGE (Keyboard) */
60 0x19, 0x00, /* USAGE_MINIMUM (no event) */
61 0x2A, 0xFF, 0x00, /* USAGE_MAXIMUM (reserved) */
62 0x81, 0x00, /* INPUT (Data,Ary,Abs) */
63 0xC0
64};
65
66/* Mouse descriptor (2) */
67static const char mse_descriptor[] = {
68 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */
69 0x09, 0x02, /* USAGE (Mouse) */
70 0xA1, 0x01, /* COLLECTION (Application) */
71 0x85, 0x02, /* REPORT_ID = 2 */
72 0x09, 0x01, /* USAGE (pointer) */
73 0xA1, 0x00, /* COLLECTION (physical) */
74 0x05, 0x09, /* USAGE_PAGE (buttons) */
75 0x19, 0x01, /* USAGE_MIN (1) */
76 0x29, 0x10, /* USAGE_MAX (16) */
77 0x15, 0x00, /* LOGICAL_MIN (0) */
78 0x25, 0x01, /* LOGICAL_MAX (1) */
79 0x95, 0x10, /* REPORT_COUNT (16) */
80 0x75, 0x01, /* REPORT_SIZE (1) */
81 0x81, 0x02, /* INPUT (data var abs) */
82 0x05, 0x01, /* USAGE_PAGE (generic desktop) */
83 0x16, 0x01, 0xF8, /* LOGICAL_MIN (-2047) */
84 0x26, 0xFF, 0x07, /* LOGICAL_MAX (2047) */
85 0x75, 0x0C, /* REPORT_SIZE (12) */
86 0x95, 0x02, /* REPORT_COUNT (2) */
87 0x09, 0x30, /* USAGE (X) */
88 0x09, 0x31, /* USAGE (Y) */
89 0x81, 0x06, /* INPUT */
90 0x15, 0x81, /* LOGICAL_MIN (-127) */
91 0x25, 0x7F, /* LOGICAL_MAX (127) */
92 0x75, 0x08, /* REPORT_SIZE (8) */
93 0x95, 0x01, /* REPORT_COUNT (1) */
94 0x09, 0x38, /* USAGE (wheel) */
95 0x81, 0x06, /* INPUT */
96 0x05, 0x0C, /* USAGE_PAGE(consumer) */
97 0x0A, 0x38, 0x02, /* USAGE(AC Pan) */
98 0x95, 0x01, /* REPORT_COUNT (1) */
99 0x81, 0x06, /* INPUT */
100 0xC0, /* END_COLLECTION */
101 0xC0, /* END_COLLECTION */
102};
103
104/* Consumer Control descriptor (3) */
105static const char consumer_descriptor[] = {
106 0x05, 0x0C, /* USAGE_PAGE (Consumer Devices) */
107 0x09, 0x01, /* USAGE (Consumer Control) */
108 0xA1, 0x01, /* COLLECTION (Application) */
109 0x85, 0x03, /* REPORT_ID = 3 */
110 0x75, 0x10, /* REPORT_SIZE (16) */
111 0x95, 0x02, /* REPORT_COUNT (2) */
112 0x15, 0x01, /* LOGICAL_MIN (1) */
113 0x26, 0x8C, 0x02, /* LOGICAL_MAX (652) */
114 0x19, 0x01, /* USAGE_MIN (1) */
115 0x2A, 0x8C, 0x02, /* USAGE_MAX (652) */
116 0x81, 0x00, /* INPUT (Data Ary Abs) */
117 0xC0, /* END_COLLECTION */
118}; /* */
119
120/* System control descriptor (4) */
121static const char syscontrol_descriptor[] = {
122 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */
123 0x09, 0x80, /* USAGE (System Control) */
124 0xA1, 0x01, /* COLLECTION (Application) */
125 0x85, 0x04, /* REPORT_ID = 4 */
126 0x75, 0x02, /* REPORT_SIZE (2) */
127 0x95, 0x01, /* REPORT_COUNT (1) */
128 0x15, 0x01, /* LOGICAL_MIN (1) */
129 0x25, 0x03, /* LOGICAL_MAX (3) */
130 0x09, 0x82, /* USAGE (System Sleep) */
131 0x09, 0x81, /* USAGE (System Power Down) */
132 0x09, 0x83, /* USAGE (System Wake Up) */
133 0x81, 0x60, /* INPUT (Data Ary Abs NPrf Null) */
134 0x75, 0x06, /* REPORT_SIZE (6) */
135 0x81, 0x03, /* INPUT (Cnst Var Abs) */
136 0xC0, /* END_COLLECTION */
137};
138
139/* Media descriptor (8) */
140static const char media_descriptor[] = {
141 0x06, 0xbc, 0xff, /* Usage Page 0xffbc */
142 0x09, 0x88, /* Usage 0x0088 */
143 0xa1, 0x01, /* BeginCollection */
144 0x85, 0x08, /* Report ID 8 */
145 0x19, 0x01, /* Usage Min 0x0001 */
146 0x29, 0xff, /* Usage Max 0x00ff */
147 0x15, 0x01, /* Logical Min 1 */
148 0x26, 0xff, 0x00, /* Logical Max 255 */
149 0x75, 0x08, /* Report Size 8 */
150 0x95, 0x01, /* Report Count 1 */
151 0x81, 0x00, /* Input */
152 0xc0, /* EndCollection */
153}; /* */
154
155/* Maximum size of all defined hid reports in bytes (including report id) */
156#define MAX_REPORT_SIZE 8
157
Henrik Rydberg2a039bf2012-04-22 14:21:39 +0200158/* Make sure all descriptors are present here */
159#define MAX_RDESC_SIZE \
160 (sizeof(kbd_descriptor) + \
161 sizeof(mse_descriptor) + \
162 sizeof(consumer_descriptor) + \
163 sizeof(syscontrol_descriptor) + \
164 sizeof(media_descriptor))
165
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200166/* Number of possible hid report types that can be created by this driver.
167 *
168 * Right now, RF report types have the same report types (or report id's)
169 * than the hid report created from those RF reports. In the future
170 * this doesnt have to be true.
171 *
172 * For instance, RF report type 0x01 which has a size of 8 bytes, corresponds
173 * to hid report id 0x01, this is standard keyboard. Same thing applies to mice
174 * reports and consumer control, etc. If a new RF report is created, it doesn't
175 * has to have the same report id as its corresponding hid report, so an
176 * translation may have to take place for future report types.
177 */
178#define NUMBER_OF_HID_REPORTS 32
179static const u8 hid_reportid_size_map[NUMBER_OF_HID_REPORTS] = {
180 [1] = 8, /* Standard keyboard */
181 [2] = 8, /* Standard mouse */
182 [3] = 5, /* Consumer control */
183 [4] = 2, /* System control */
184 [8] = 2, /* Media Center */
185};
186
187
188#define LOGITECH_DJ_INTERFACE_NUMBER 0x02
189
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200190static struct hid_ll_driver logi_dj_ll_driver;
191
192static int logi_dj_output_hidraw_report(struct hid_device *hid, u8 * buf,
193 size_t count,
194 unsigned char report_type);
Nestor Lopez Casadoc63e0e32013-07-18 06:21:30 -0700195static int logi_dj_recv_query_paired_devices(struct dj_receiver_dev *djrcv_dev);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200196
197static void logi_dj_recv_destroy_djhid_device(struct dj_receiver_dev *djrcv_dev,
198 struct dj_report *dj_report)
199{
200 /* Called in delayed work context */
201 struct dj_device *dj_dev;
202 unsigned long flags;
203
204 spin_lock_irqsave(&djrcv_dev->lock, flags);
205 dj_dev = djrcv_dev->paired_dj_devices[dj_report->device_index];
206 djrcv_dev->paired_dj_devices[dj_report->device_index] = NULL;
207 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
208
209 if (dj_dev != NULL) {
210 hid_destroy_device(dj_dev->hdev);
211 kfree(dj_dev);
212 } else {
213 dev_err(&djrcv_dev->hdev->dev, "%s: can't destroy a NULL device\n",
214 __func__);
215 }
216}
217
218static void logi_dj_recv_add_djhid_device(struct dj_receiver_dev *djrcv_dev,
219 struct dj_report *dj_report)
220{
221 /* Called in delayed work context */
222 struct hid_device *djrcv_hdev = djrcv_dev->hdev;
223 struct usb_interface *intf = to_usb_interface(djrcv_hdev->dev.parent);
224 struct usb_device *usbdev = interface_to_usbdev(intf);
225 struct hid_device *dj_hiddev;
226 struct dj_device *dj_dev;
227
228 /* Device index goes from 1 to 6, we need 3 bytes to store the
229 * semicolon, the index, and a null terminator
230 */
231 unsigned char tmpstr[3];
232
233 if (dj_report->report_params[DEVICE_PAIRED_PARAM_SPFUNCTION] &
234 SPFUNCTION_DEVICE_LIST_EMPTY) {
235 dbg_hid("%s: device list is empty\n", __func__);
Nestor Lopez Casadoc63e0e32013-07-18 06:21:30 -0700236 djrcv_dev->querying_devices = false;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200237 return;
238 }
239
240 if ((dj_report->device_index < DJ_DEVICE_INDEX_MIN) ||
241 (dj_report->device_index > DJ_DEVICE_INDEX_MAX)) {
242 dev_err(&djrcv_hdev->dev, "%s: invalid device index:%d\n",
243 __func__, dj_report->device_index);
244 return;
245 }
246
Nestor Lopez Casadoc63e0e32013-07-18 06:21:30 -0700247 if (djrcv_dev->paired_dj_devices[dj_report->device_index]) {
248 /* The device is already known. No need to reallocate it. */
249 dbg_hid("%s: device is already known\n", __func__);
250 return;
251 }
252
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200253 dj_hiddev = hid_allocate_device();
254 if (IS_ERR(dj_hiddev)) {
255 dev_err(&djrcv_hdev->dev, "%s: hid_allocate_device failed\n",
256 __func__);
257 return;
258 }
259
260 dj_hiddev->ll_driver = &logi_dj_ll_driver;
261 dj_hiddev->hid_output_raw_report = logi_dj_output_hidraw_report;
262
263 dj_hiddev->dev.parent = &djrcv_hdev->dev;
264 dj_hiddev->bus = BUS_USB;
265 dj_hiddev->vendor = le16_to_cpu(usbdev->descriptor.idVendor);
266 dj_hiddev->product = le16_to_cpu(usbdev->descriptor.idProduct);
267 snprintf(dj_hiddev->name, sizeof(dj_hiddev->name),
268 "Logitech Unifying Device. Wireless PID:%02x%02x",
269 dj_report->report_params[DEVICE_PAIRED_PARAM_EQUAD_ID_MSB],
270 dj_report->report_params[DEVICE_PAIRED_PARAM_EQUAD_ID_LSB]);
271
272 usb_make_path(usbdev, dj_hiddev->phys, sizeof(dj_hiddev->phys));
273 snprintf(tmpstr, sizeof(tmpstr), ":%d", dj_report->device_index);
274 strlcat(dj_hiddev->phys, tmpstr, sizeof(dj_hiddev->phys));
275
276 dj_dev = kzalloc(sizeof(struct dj_device), GFP_KERNEL);
277
278 if (!dj_dev) {
279 dev_err(&djrcv_hdev->dev, "%s: failed allocating dj_device\n",
280 __func__);
281 goto dj_device_allocate_fail;
282 }
283
Jonathan Nieder44d27f72012-05-11 16:17:16 +0200284 dj_dev->reports_supported = get_unaligned_le32(
285 dj_report->report_params + DEVICE_PAIRED_RF_REPORT_TYPE);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200286 dj_dev->hdev = dj_hiddev;
287 dj_dev->dj_receiver_dev = djrcv_dev;
288 dj_dev->device_index = dj_report->device_index;
289 dj_hiddev->driver_data = dj_dev;
290
291 djrcv_dev->paired_dj_devices[dj_report->device_index] = dj_dev;
292
293 if (hid_add_device(dj_hiddev)) {
294 dev_err(&djrcv_hdev->dev, "%s: failed adding dj_device\n",
295 __func__);
296 goto hid_add_device_fail;
297 }
298
299 return;
300
301hid_add_device_fail:
302 djrcv_dev->paired_dj_devices[dj_report->device_index] = NULL;
303 kfree(dj_dev);
304dj_device_allocate_fail:
305 hid_destroy_device(dj_hiddev);
306}
307
308static void delayedwork_callback(struct work_struct *work)
309{
310 struct dj_receiver_dev *djrcv_dev =
311 container_of(work, struct dj_receiver_dev, work);
312
313 struct dj_report dj_report;
314 unsigned long flags;
315 int count;
Nestor Lopez Casadoc63e0e32013-07-18 06:21:30 -0700316 int retval;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200317
318 dbg_hid("%s\n", __func__);
319
320 spin_lock_irqsave(&djrcv_dev->lock, flags);
321
322 count = kfifo_out(&djrcv_dev->notif_fifo, &dj_report,
323 sizeof(struct dj_report));
324
325 if (count != sizeof(struct dj_report)) {
326 dev_err(&djrcv_dev->hdev->dev, "%s: workitem triggered without "
327 "notifications available\n", __func__);
328 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
329 return;
330 }
331
332 if (!kfifo_is_empty(&djrcv_dev->notif_fifo)) {
333 if (schedule_work(&djrcv_dev->work) == 0) {
334 dbg_hid("%s: did not schedule the work item, was "
335 "already queued\n", __func__);
336 }
337 }
338
339 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
340
341 switch (dj_report.report_type) {
342 case REPORT_TYPE_NOTIF_DEVICE_PAIRED:
343 logi_dj_recv_add_djhid_device(djrcv_dev, &dj_report);
344 break;
345 case REPORT_TYPE_NOTIF_DEVICE_UNPAIRED:
346 logi_dj_recv_destroy_djhid_device(djrcv_dev, &dj_report);
347 break;
348 default:
Nestor Lopez Casadoc63e0e32013-07-18 06:21:30 -0700349 /* A normal report (i. e. not belonging to a pair/unpair notification)
350 * arriving here, means that the report arrived but we did not have a
351 * paired dj_device associated to the report's device_index, this
352 * means that the original "device paired" notification corresponding
353 * to this dj_device never arrived to this driver. The reason is that
354 * hid-core discards all packets coming from a device while probe() is
355 * executing. */
356 if (!djrcv_dev->paired_dj_devices[dj_report.device_index]) {
357 /* ok, we don't know the device, just re-ask the
358 * receiver for the list of connected devices. */
359 retval = logi_dj_recv_query_paired_devices(djrcv_dev);
360 if (!retval) {
361 /* everything went fine, so just leave */
362 break;
363 }
364 dev_err(&djrcv_dev->hdev->dev,
365 "%s:logi_dj_recv_query_paired_devices "
366 "error:%d\n", __func__, retval);
367 }
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200368 dbg_hid("%s: unexpected report type\n", __func__);
369 }
370}
371
372static void logi_dj_recv_queue_notification(struct dj_receiver_dev *djrcv_dev,
373 struct dj_report *dj_report)
374{
375 /* We are called from atomic context (tasklet && djrcv->lock held) */
376
377 kfifo_in(&djrcv_dev->notif_fifo, dj_report, sizeof(struct dj_report));
378
379 if (schedule_work(&djrcv_dev->work) == 0) {
380 dbg_hid("%s: did not schedule the work item, was already "
381 "queued\n", __func__);
382 }
383}
384
385static void logi_dj_recv_forward_null_report(struct dj_receiver_dev *djrcv_dev,
386 struct dj_report *dj_report)
387{
388 /* We are called from atomic context (tasklet && djrcv->lock held) */
389 unsigned int i;
390 u8 reportbuffer[MAX_REPORT_SIZE];
391 struct dj_device *djdev;
392
393 djdev = djrcv_dev->paired_dj_devices[dj_report->device_index];
394
395 if (!djdev) {
396 dbg_hid("djrcv_dev->paired_dj_devices[dj_report->device_index]"
397 " is NULL, index %d\n", dj_report->device_index);
Nestor Lopez Casadoc63e0e32013-07-18 06:21:30 -0700398 kfifo_in(&djrcv_dev->notif_fifo, dj_report, sizeof(struct dj_report));
399
400 if (schedule_work(&djrcv_dev->work) == 0) {
401 dbg_hid("%s: did not schedule the work item, was already "
402 "queued\n", __func__);
403 }
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200404 return;
405 }
406
407 memset(reportbuffer, 0, sizeof(reportbuffer));
408
409 for (i = 0; i < NUMBER_OF_HID_REPORTS; i++) {
410 if (djdev->reports_supported & (1 << i)) {
411 reportbuffer[0] = i;
412 if (hid_input_report(djdev->hdev,
413 HID_INPUT_REPORT,
414 reportbuffer,
415 hid_reportid_size_map[i], 1)) {
416 dbg_hid("hid_input_report error sending null "
417 "report\n");
418 }
419 }
420 }
421}
422
423static void logi_dj_recv_forward_report(struct dj_receiver_dev *djrcv_dev,
424 struct dj_report *dj_report)
425{
426 /* We are called from atomic context (tasklet && djrcv->lock held) */
427 struct dj_device *dj_device;
428
429 dj_device = djrcv_dev->paired_dj_devices[dj_report->device_index];
430
431 if (dj_device == NULL) {
432 dbg_hid("djrcv_dev->paired_dj_devices[dj_report->device_index]"
433 " is NULL, index %d\n", dj_report->device_index);
Nestor Lopez Casadoc63e0e32013-07-18 06:21:30 -0700434 kfifo_in(&djrcv_dev->notif_fifo, dj_report, sizeof(struct dj_report));
435
436 if (schedule_work(&djrcv_dev->work) == 0) {
437 dbg_hid("%s: did not schedule the work item, was already "
438 "queued\n", __func__);
439 }
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200440 return;
441 }
442
443 if ((dj_report->report_type > ARRAY_SIZE(hid_reportid_size_map) - 1) ||
444 (hid_reportid_size_map[dj_report->report_type] == 0)) {
445 dbg_hid("invalid report type:%x\n", dj_report->report_type);
446 return;
447 }
448
449 if (hid_input_report(dj_device->hdev,
450 HID_INPUT_REPORT, &dj_report->report_type,
451 hid_reportid_size_map[dj_report->report_type], 1)) {
452 dbg_hid("hid_input_report error\n");
453 }
454}
455
456
457static int logi_dj_recv_send_report(struct dj_receiver_dev *djrcv_dev,
458 struct dj_report *dj_report)
459{
460 struct hid_device *hdev = djrcv_dev->hdev;
Benjamin Tissoiresdcd90062013-03-05 17:09:00 +0100461 struct hid_report *report;
462 struct hid_report_enum *output_report_enum;
463 u8 *data = (u8 *)(&dj_report->device_index);
Kees Cook297502a2013-09-11 21:56:56 +0200464 unsigned int i;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200465
Benjamin Tissoiresdcd90062013-03-05 17:09:00 +0100466 output_report_enum = &hdev->report_enum[HID_OUTPUT_REPORT];
467 report = output_report_enum->report_id_hash[REPORT_ID_DJ_SHORT];
468
469 if (!report) {
470 dev_err(&hdev->dev, "%s: unable to find dj report\n", __func__);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200471 return -ENODEV;
472 }
473
Kees Cook297502a2013-09-11 21:56:56 +0200474 for (i = 0; i < DJREPORT_SHORT_LENGTH - 1; i++)
Benjamin Tissoiresdcd90062013-03-05 17:09:00 +0100475 report->field[0]->value[i] = data[i];
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200476
Jiri Kosina83a44ac2013-03-09 10:58:13 +0100477 hid_hw_request(hdev, report, HID_REQ_SET_REPORT);
Benjamin Tissoiresdcd90062013-03-05 17:09:00 +0100478
479 return 0;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200480}
481
482static int logi_dj_recv_query_paired_devices(struct dj_receiver_dev *djrcv_dev)
483{
Marc Dionned8dc3492012-06-01 18:12:14 -0400484 struct dj_report *dj_report;
485 int retval;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200486
Nestor Lopez Casadoc63e0e32013-07-18 06:21:30 -0700487 /* no need to protect djrcv_dev->querying_devices */
488 if (djrcv_dev->querying_devices)
489 return 0;
490
Alan Cox8a55ade2012-09-04 15:10:08 +0100491 dj_report = kzalloc(sizeof(struct dj_report), GFP_KERNEL);
Marc Dionned8dc3492012-06-01 18:12:14 -0400492 if (!dj_report)
493 return -ENOMEM;
494 dj_report->report_id = REPORT_ID_DJ_SHORT;
495 dj_report->device_index = 0xFF;
496 dj_report->report_type = REPORT_TYPE_CMD_GET_PAIRED_DEVICES;
497 retval = logi_dj_recv_send_report(djrcv_dev, dj_report);
498 kfree(dj_report);
499 return retval;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200500}
501
Nestor Lopez Casadoc63e0e32013-07-18 06:21:30 -0700502
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200503static int logi_dj_recv_switch_to_dj_mode(struct dj_receiver_dev *djrcv_dev,
504 unsigned timeout)
505{
Marc Dionned8dc3492012-06-01 18:12:14 -0400506 struct dj_report *dj_report;
507 int retval;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200508
Alan Cox8a55ade2012-09-04 15:10:08 +0100509 dj_report = kzalloc(sizeof(struct dj_report), GFP_KERNEL);
Marc Dionned8dc3492012-06-01 18:12:14 -0400510 if (!dj_report)
511 return -ENOMEM;
512 dj_report->report_id = REPORT_ID_DJ_SHORT;
513 dj_report->device_index = 0xFF;
514 dj_report->report_type = REPORT_TYPE_CMD_SWITCH;
515 dj_report->report_params[CMD_SWITCH_PARAM_DEVBITFIELD] = 0x3F;
516 dj_report->report_params[CMD_SWITCH_PARAM_TIMEOUT_SECONDS] = (u8)timeout;
517 retval = logi_dj_recv_send_report(djrcv_dev, dj_report);
518 kfree(dj_report);
Benjamin Tisssoires42c22db2014-01-08 17:18:45 -0500519
520 /*
521 * Ugly sleep to work around a USB 3.0 bug when the receiver is still
522 * processing the "switch-to-dj" command while we send an other command.
523 * 50 msec should gives enough time to the receiver to be ready.
524 */
525 msleep(50);
526
Marc Dionned8dc3492012-06-01 18:12:14 -0400527 return retval;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200528}
529
530
531static int logi_dj_ll_open(struct hid_device *hid)
532{
533 dbg_hid("%s:%s\n", __func__, hid->phys);
534 return 0;
535
536}
537
538static void logi_dj_ll_close(struct hid_device *hid)
539{
540 dbg_hid("%s:%s\n", __func__, hid->phys);
541}
542
543static int logi_dj_output_hidraw_report(struct hid_device *hid, u8 * buf,
544 size_t count,
545 unsigned char report_type)
546{
547 /* Called by hid raw to send data */
548 dbg_hid("%s\n", __func__);
549
550 return 0;
551}
552
Dan Carpenter6d603322013-10-19 12:08:59 +0300553static void rdcat(char *rdesc, unsigned int *rsize, const char *data, unsigned int size)
Henrik Rydberg2a039bf2012-04-22 14:21:39 +0200554{
Dan Carpenter6d603322013-10-19 12:08:59 +0300555 memcpy(rdesc + *rsize, data, size);
Henrik Rydberg2a039bf2012-04-22 14:21:39 +0200556 *rsize += size;
557}
558
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200559static int logi_dj_ll_parse(struct hid_device *hid)
560{
561 struct dj_device *djdev = hid->driver_data;
Henrik Rydberg2a039bf2012-04-22 14:21:39 +0200562 unsigned int rsize = 0;
563 char *rdesc;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200564 int retval;
565
566 dbg_hid("%s\n", __func__);
567
568 djdev->hdev->version = 0x0111;
569 djdev->hdev->country = 0x00;
570
Henrik Rydberg2a039bf2012-04-22 14:21:39 +0200571 rdesc = kmalloc(MAX_RDESC_SIZE, GFP_KERNEL);
572 if (!rdesc)
573 return -ENOMEM;
574
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200575 if (djdev->reports_supported & STD_KEYBOARD) {
576 dbg_hid("%s: sending a kbd descriptor, reports_supported: %x\n",
577 __func__, djdev->reports_supported);
Dan Carpenter6d603322013-10-19 12:08:59 +0300578 rdcat(rdesc, &rsize, kbd_descriptor, sizeof(kbd_descriptor));
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200579 }
580
581 if (djdev->reports_supported & STD_MOUSE) {
582 dbg_hid("%s: sending a mouse descriptor, reports_supported: "
583 "%x\n", __func__, djdev->reports_supported);
Dan Carpenter6d603322013-10-19 12:08:59 +0300584 rdcat(rdesc, &rsize, mse_descriptor, sizeof(mse_descriptor));
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200585 }
586
587 if (djdev->reports_supported & MULTIMEDIA) {
588 dbg_hid("%s: sending a multimedia report descriptor: %x\n",
589 __func__, djdev->reports_supported);
Dan Carpenter6d603322013-10-19 12:08:59 +0300590 rdcat(rdesc, &rsize, consumer_descriptor, sizeof(consumer_descriptor));
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200591 }
592
593 if (djdev->reports_supported & POWER_KEYS) {
594 dbg_hid("%s: sending a power keys report descriptor: %x\n",
595 __func__, djdev->reports_supported);
Dan Carpenter6d603322013-10-19 12:08:59 +0300596 rdcat(rdesc, &rsize, syscontrol_descriptor, sizeof(syscontrol_descriptor));
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200597 }
598
599 if (djdev->reports_supported & MEDIA_CENTER) {
600 dbg_hid("%s: sending a media center report descriptor: %x\n",
601 __func__, djdev->reports_supported);
Dan Carpenter6d603322013-10-19 12:08:59 +0300602 rdcat(rdesc, &rsize, media_descriptor, sizeof(media_descriptor));
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200603 }
604
605 if (djdev->reports_supported & KBD_LEDS) {
606 dbg_hid("%s: need to send kbd leds report descriptor: %x\n",
607 __func__, djdev->reports_supported);
608 }
609
Henrik Rydberg2a039bf2012-04-22 14:21:39 +0200610 retval = hid_parse_report(hid, rdesc, rsize);
611 kfree(rdesc);
612
613 return retval;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200614}
615
616static int logi_dj_ll_input_event(struct input_dev *dev, unsigned int type,
617 unsigned int code, int value)
618{
619 /* Sent by the input layer to handle leds and Force Feedback */
620 struct hid_device *dj_hiddev = input_get_drvdata(dev);
621 struct dj_device *dj_dev = dj_hiddev->driver_data;
622
623 struct dj_receiver_dev *djrcv_dev =
624 dev_get_drvdata(dj_hiddev->dev.parent);
625 struct hid_device *dj_rcv_hiddev = djrcv_dev->hdev;
626 struct hid_report_enum *output_report_enum;
627
628 struct hid_field *field;
629 struct hid_report *report;
Jiri Kosina27ce4052013-07-10 19:56:27 +0200630 unsigned char *data;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200631 int offset;
632
633 dbg_hid("%s: %s, type:%d | code:%d | value:%d\n",
634 __func__, dev->phys, type, code, value);
635
636 if (type != EV_LED)
637 return -1;
638
639 offset = hidinput_find_field(dj_hiddev, type, code, &field);
640
641 if (offset == -1) {
642 dev_warn(&dev->dev, "event field not found\n");
643 return -1;
644 }
645 hid_set_field(field, offset, value);
Jiri Kosina27ce4052013-07-10 19:56:27 +0200646
Peter Hurleyce7373682013-07-31 17:17:58 -0400647 data = hid_alloc_report_buf(field->report, GFP_ATOMIC);
Jiri Kosina27ce4052013-07-10 19:56:27 +0200648 if (!data) {
649 dev_warn(&dev->dev, "failed to allocate report buf memory\n");
650 return -1;
651 }
652
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200653 hid_output_report(field->report, &data[0]);
654
655 output_report_enum = &dj_rcv_hiddev->report_enum[HID_OUTPUT_REPORT];
656 report = output_report_enum->report_id_hash[REPORT_ID_DJ_SHORT];
657 hid_set_field(report->field[0], 0, dj_dev->device_index);
658 hid_set_field(report->field[0], 1, REPORT_TYPE_LEDS);
659 hid_set_field(report->field[0], 2, data[1]);
660
Benjamin Tissoiresd88142722013-02-25 11:31:46 +0100661 hid_hw_request(dj_rcv_hiddev, report, HID_REQ_SET_REPORT);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200662
Jiri Kosina27ce4052013-07-10 19:56:27 +0200663 kfree(data);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200664
Jiri Kosina27ce4052013-07-10 19:56:27 +0200665 return 0;
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200666}
667
668static int logi_dj_ll_start(struct hid_device *hid)
669{
670 dbg_hid("%s\n", __func__);
671 return 0;
672}
673
674static void logi_dj_ll_stop(struct hid_device *hid)
675{
676 dbg_hid("%s\n", __func__);
677}
678
679
680static struct hid_ll_driver logi_dj_ll_driver = {
681 .parse = logi_dj_ll_parse,
682 .start = logi_dj_ll_start,
683 .stop = logi_dj_ll_stop,
684 .open = logi_dj_ll_open,
685 .close = logi_dj_ll_close,
686 .hidinput_input_event = logi_dj_ll_input_event,
687};
688
689
690static int logi_dj_raw_event(struct hid_device *hdev,
691 struct hid_report *report, u8 *data,
692 int size)
693{
694 struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
695 struct dj_report *dj_report = (struct dj_report *) data;
696 unsigned long flags;
697 bool report_processed = false;
698
699 dbg_hid("%s, size:%d\n", __func__, size);
700
701 /* Here we receive all data coming from iface 2, there are 4 cases:
702 *
703 * 1) Data should continue its normal processing i.e. data does not
704 * come from the DJ collection, in which case we do nothing and
705 * return 0, so hid-core can continue normal processing (will forward
706 * to associated hidraw device)
707 *
708 * 2) Data is from DJ collection, and is intended for this driver i. e.
709 * data contains arrival, departure, etc notifications, in which case
710 * we queue them for delayed processing by the work queue. We return 1
711 * to hid-core as no further processing is required from it.
712 *
713 * 3) Data is from DJ collection, and informs a connection change,
714 * if the change means rf link loss, then we must send a null report
715 * to the upper layer to discard potentially pressed keys that may be
716 * repeated forever by the input layer. Return 1 to hid-core as no
717 * further processing is required.
718 *
719 * 4) Data is from DJ collection and is an actual input event from
720 * a paired DJ device in which case we forward it to the correct hid
721 * device (via hid_input_report() ) and return 1 so hid-core does not do
722 * anything else with it.
723 */
724
725 spin_lock_irqsave(&djrcv_dev->lock, flags);
726 if (dj_report->report_id == REPORT_ID_DJ_SHORT) {
727 switch (dj_report->report_type) {
728 case REPORT_TYPE_NOTIF_DEVICE_PAIRED:
729 case REPORT_TYPE_NOTIF_DEVICE_UNPAIRED:
730 logi_dj_recv_queue_notification(djrcv_dev, dj_report);
731 break;
732 case REPORT_TYPE_NOTIF_CONNECTION_STATUS:
733 if (dj_report->report_params[CONNECTION_STATUS_PARAM_STATUS] ==
734 STATUS_LINKLOSS) {
735 logi_dj_recv_forward_null_report(djrcv_dev, dj_report);
736 }
737 break;
738 default:
739 logi_dj_recv_forward_report(djrcv_dev, dj_report);
740 }
741 report_processed = true;
742 }
743 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
744
745 return report_processed;
746}
747
748static int logi_dj_probe(struct hid_device *hdev,
749 const struct hid_device_id *id)
750{
751 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
752 struct dj_receiver_dev *djrcv_dev;
753 int retval;
754
755 if (is_dj_device((struct dj_device *)hdev->driver_data))
756 return -ENODEV;
757
758 dbg_hid("%s called for ifnum %d\n", __func__,
759 intf->cur_altsetting->desc.bInterfaceNumber);
760
761 /* Ignore interfaces 0 and 1, they will not carry any data, dont create
762 * any hid_device for them */
763 if (intf->cur_altsetting->desc.bInterfaceNumber !=
764 LOGITECH_DJ_INTERFACE_NUMBER) {
765 dbg_hid("%s: ignoring ifnum %d\n", __func__,
766 intf->cur_altsetting->desc.bInterfaceNumber);
767 return -ENODEV;
768 }
769
770 /* Treat interface 2 */
771
772 djrcv_dev = kzalloc(sizeof(struct dj_receiver_dev), GFP_KERNEL);
773 if (!djrcv_dev) {
774 dev_err(&hdev->dev,
775 "%s:failed allocating dj_receiver_dev\n", __func__);
776 return -ENOMEM;
777 }
778 djrcv_dev->hdev = hdev;
779 INIT_WORK(&djrcv_dev->work, delayedwork_callback);
780 spin_lock_init(&djrcv_dev->lock);
781 if (kfifo_alloc(&djrcv_dev->notif_fifo,
782 DJ_MAX_NUMBER_NOTIFICATIONS * sizeof(struct dj_report),
783 GFP_KERNEL)) {
784 dev_err(&hdev->dev,
785 "%s:failed allocating notif_fifo\n", __func__);
786 kfree(djrcv_dev);
787 return -ENOMEM;
788 }
789 hid_set_drvdata(hdev, djrcv_dev);
790
791 /* Call to usbhid to fetch the HID descriptors of interface 2 and
792 * subsequently call to the hid/hid-core to parse the fetched
793 * descriptors, this will in turn create the hidraw and hiddev nodes
794 * for interface 2 of the receiver */
795 retval = hid_parse(hdev);
796 if (retval) {
797 dev_err(&hdev->dev,
798 "%s:parse of interface 2 failed\n", __func__);
799 goto hid_parse_fail;
800 }
801
Kees Cook297502a2013-09-11 21:56:56 +0200802 if (!hid_validate_values(hdev, HID_OUTPUT_REPORT, REPORT_ID_DJ_SHORT,
803 0, DJREPORT_SHORT_LENGTH - 1)) {
804 retval = -ENODEV;
805 goto hid_parse_fail;
806 }
807
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200808 /* Starts the usb device and connects to upper interfaces hiddev and
809 * hidraw */
810 retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
811 if (retval) {
812 dev_err(&hdev->dev,
813 "%s:hid_hw_start returned error\n", __func__);
814 goto hid_hw_start_fail;
815 }
816
817 retval = logi_dj_recv_switch_to_dj_mode(djrcv_dev, 0);
818 if (retval < 0) {
819 dev_err(&hdev->dev,
820 "%s:logi_dj_recv_switch_to_dj_mode returned error:%d\n",
821 __func__, retval);
822 goto switch_to_dj_mode_fail;
823 }
824
825 /* This is enabling the polling urb on the IN endpoint */
Benjamin Tissoiresddf7540e2013-07-12 11:01:02 +0200826 retval = hid_hw_open(hdev);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200827 if (retval < 0) {
Benjamin Tissoiresddf7540e2013-07-12 11:01:02 +0200828 dev_err(&hdev->dev, "%s:hid_hw_open returned error:%d\n",
829 __func__, retval);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200830 goto llopen_failed;
831 }
832
Andrew de los Reyesa9dd22b72013-02-18 09:20:22 -0800833 /* Allow incoming packets to arrive: */
834 hid_device_io_start(hdev);
835
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200836 retval = logi_dj_recv_query_paired_devices(djrcv_dev);
837 if (retval < 0) {
838 dev_err(&hdev->dev, "%s:logi_dj_recv_query_paired_devices "
839 "error:%d\n", __func__, retval);
840 goto logi_dj_recv_query_paired_devices_failed;
841 }
842
843 return retval;
844
845logi_dj_recv_query_paired_devices_failed:
Benjamin Tissoiresddf7540e2013-07-12 11:01:02 +0200846 hid_hw_close(hdev);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200847
848llopen_failed:
849switch_to_dj_mode_fail:
850 hid_hw_stop(hdev);
851
852hid_hw_start_fail:
853hid_parse_fail:
854 kfifo_free(&djrcv_dev->notif_fifo);
855 kfree(djrcv_dev);
856 hid_set_drvdata(hdev, NULL);
857 return retval;
858
859}
860
861#ifdef CONFIG_PM
862static int logi_dj_reset_resume(struct hid_device *hdev)
863{
864 int retval;
865 struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
866
867 retval = logi_dj_recv_switch_to_dj_mode(djrcv_dev, 0);
868 if (retval < 0) {
869 dev_err(&hdev->dev,
870 "%s:logi_dj_recv_switch_to_dj_mode returned error:%d\n",
871 __func__, retval);
872 }
873
874 return 0;
875}
876#endif
877
878static void logi_dj_remove(struct hid_device *hdev)
879{
880 struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
881 struct dj_device *dj_dev;
882 int i;
883
884 dbg_hid("%s\n", __func__);
885
886 cancel_work_sync(&djrcv_dev->work);
887
Benjamin Tissoiresddf7540e2013-07-12 11:01:02 +0200888 hid_hw_close(hdev);
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200889 hid_hw_stop(hdev);
890
891 /* I suppose that at this point the only context that can access
892 * the djrecv_data is this thread as the work item is guaranteed to
893 * have finished and no more raw_event callbacks should arrive after
894 * the remove callback was triggered so no locks are put around the
895 * code below */
Nestor Lopez Casado844580f2011-09-20 15:59:03 +0200896 for (i = 0; i < (DJ_MAX_PAIRED_DEVICES + DJ_DEVICE_INDEX_MIN); i++) {
Nestor Lopez Casado534a7b82011-09-15 11:34:49 +0200897 dj_dev = djrcv_dev->paired_dj_devices[i];
898 if (dj_dev != NULL) {
899 hid_destroy_device(dj_dev->hdev);
900 kfree(dj_dev);
901 djrcv_dev->paired_dj_devices[i] = NULL;
902 }
903 }
904
905 kfifo_free(&djrcv_dev->notif_fifo);
906 kfree(djrcv_dev);
907 hid_set_drvdata(hdev, NULL);
908}
909
910static int logi_djdevice_probe(struct hid_device *hdev,
911 const struct hid_device_id *id)
912{
913 int ret;
914 struct dj_device *dj_dev = hdev->driver_data;
915
916 if (!is_dj_device(dj_dev))
917 return -ENODEV;
918
919 ret = hid_parse(hdev);
920 if (!ret)
921 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
922
923 return ret;
924}
925
926static const struct hid_device_id logi_dj_receivers[] = {
927 {HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
928 USB_DEVICE_ID_LOGITECH_UNIFYING_RECEIVER)},
929 {HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
930 USB_DEVICE_ID_LOGITECH_UNIFYING_RECEIVER_2)},
931 {}
932};
933
934MODULE_DEVICE_TABLE(hid, logi_dj_receivers);
935
936static struct hid_driver logi_djreceiver_driver = {
937 .name = "logitech-djreceiver",
938 .id_table = logi_dj_receivers,
939 .probe = logi_dj_probe,
940 .remove = logi_dj_remove,
941 .raw_event = logi_dj_raw_event,
942#ifdef CONFIG_PM
943 .reset_resume = logi_dj_reset_resume,
944#endif
945};
946
947
948static const struct hid_device_id logi_dj_devices[] = {
949 {HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
950 USB_DEVICE_ID_LOGITECH_UNIFYING_RECEIVER)},
951 {HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
952 USB_DEVICE_ID_LOGITECH_UNIFYING_RECEIVER_2)},
953 {}
954};
955
956static struct hid_driver logi_djdevice_driver = {
957 .name = "logitech-djdevice",
958 .id_table = logi_dj_devices,
959 .probe = logi_djdevice_probe,
960};
961
962
963static int __init logi_dj_init(void)
964{
965 int retval;
966
967 dbg_hid("Logitech-DJ:%s\n", __func__);
968
969 retval = hid_register_driver(&logi_djreceiver_driver);
970 if (retval)
971 return retval;
972
973 retval = hid_register_driver(&logi_djdevice_driver);
974 if (retval)
975 hid_unregister_driver(&logi_djreceiver_driver);
976
977 return retval;
978
979}
980
981static void __exit logi_dj_exit(void)
982{
983 dbg_hid("Logitech-DJ:%s\n", __func__);
984
985 hid_unregister_driver(&logi_djdevice_driver);
986 hid_unregister_driver(&logi_djreceiver_driver);
987
988}
989
990module_init(logi_dj_init);
991module_exit(logi_dj_exit);
992MODULE_LICENSE("GPL");
993MODULE_AUTHOR("Logitech");
994MODULE_AUTHOR("Nestor Lopez Casado");
995MODULE_AUTHOR("nlopezcasad@logitech.com");