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