hyperv: Remove recv_pkt_list and lock
[linux/fpc-iii.git] / drivers / hid / hid-logitech-dj.c
blob486dbde2ba2d90d3802e59163870b57c4274fb28
1 /*
2 * HID driver for Logitech Unifying receivers
4 * Copyright (c) 2011 Logitech
5 */
7 /*
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include <linux/device.h>
26 #include <linux/hid.h>
27 #include <linux/module.h>
28 #include <linux/usb.h>
29 #include <asm/unaligned.h>
30 #include "hid-ids.h"
31 #include "hid-logitech-dj.h"
33 /* Keyboard descriptor (1) */
34 static 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, 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) */
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) */
67 0xC0
70 /* Mouse descriptor (2) */
71 static 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 */
108 /* Consumer Control descriptor (3) */
109 static 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 }; /* */
124 /* System control descriptor (4) */
125 static 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 */
143 /* Media descriptor (8) */
144 static 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 }; /* */
159 /* Maximum size of all defined hid reports in bytes (including report id) */
160 #define MAX_REPORT_SIZE 8
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))
170 /* Number of possible hid report types that can be created by this driver.
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.
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.
182 #define NUMBER_OF_HID_REPORTS 32
183 static 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 */
192 #define LOGITECH_DJ_INTERFACE_NUMBER 0x02
194 static struct hid_ll_driver logi_dj_ll_driver;
196 static int logi_dj_recv_query_paired_devices(struct dj_receiver_dev *djrcv_dev);
198 static void logi_dj_recv_destroy_djhid_device(struct dj_receiver_dev *djrcv_dev,
199 struct dj_report *dj_report)
201 /* Called in delayed work context */
202 struct dj_device *dj_dev;
203 unsigned long flags;
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);
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__);
219 static void logi_dj_recv_add_djhid_device(struct dj_receiver_dev *djrcv_dev,
220 struct dj_report *dj_report)
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;
229 /* Device index goes from 1 to 6, we need 3 bytes to store the
230 * semicolon, the index, and a null terminator
232 unsigned char tmpstr[3];
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__);
237 djrcv_dev->querying_devices = false;
238 return;
241 if ((dj_report->device_index < DJ_DEVICE_INDEX_MIN) ||
242 (dj_report->device_index > DJ_DEVICE_INDEX_MAX)) {
243 dev_err(&djrcv_hdev->dev, "%s: invalid device index:%d\n",
244 __func__, dj_report->device_index);
245 return;
248 if (djrcv_dev->paired_dj_devices[dj_report->device_index]) {
249 /* The device is already known. No need to reallocate it. */
250 dbg_hid("%s: device is already known\n", __func__);
251 return;
254 dj_hiddev = hid_allocate_device();
255 if (IS_ERR(dj_hiddev)) {
256 dev_err(&djrcv_hdev->dev, "%s: hid_allocate_device failed\n",
257 __func__);
258 return;
261 dj_hiddev->ll_driver = &logi_dj_ll_driver;
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]);
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));
276 dj_dev = kzalloc(sizeof(struct dj_device), GFP_KERNEL);
278 if (!dj_dev) {
279 dev_err(&djrcv_hdev->dev, "%s: failed allocating dj_device\n",
280 __func__);
281 goto dj_device_allocate_fail;
284 dj_dev->reports_supported = get_unaligned_le32(
285 dj_report->report_params + DEVICE_PAIRED_RF_REPORT_TYPE);
286 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;
291 djrcv_dev->paired_dj_devices[dj_report->device_index] = dj_dev;
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;
299 return;
301 hid_add_device_fail:
302 djrcv_dev->paired_dj_devices[dj_report->device_index] = NULL;
303 kfree(dj_dev);
304 dj_device_allocate_fail:
305 hid_destroy_device(dj_hiddev);
308 static void delayedwork_callback(struct work_struct *work)
310 struct dj_receiver_dev *djrcv_dev =
311 container_of(work, struct dj_receiver_dev, work);
313 struct dj_report dj_report;
314 unsigned long flags;
315 int count;
316 int retval;
318 dbg_hid("%s\n", __func__);
320 spin_lock_irqsave(&djrcv_dev->lock, flags);
322 count = kfifo_out(&djrcv_dev->notif_fifo, &dj_report,
323 sizeof(struct dj_report));
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;
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__);
339 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
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:
349 /* 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;
364 dev_err(&djrcv_dev->hdev->dev,
365 "%s:logi_dj_recv_query_paired_devices "
366 "error:%d\n", __func__, retval);
368 dbg_hid("%s: unexpected report type\n", __func__);
372 static void logi_dj_recv_queue_notification(struct dj_receiver_dev *djrcv_dev,
373 struct dj_report *dj_report)
375 /* We are called from atomic context (tasklet && djrcv->lock held) */
377 kfifo_in(&djrcv_dev->notif_fifo, dj_report, sizeof(struct dj_report));
379 if (schedule_work(&djrcv_dev->work) == 0) {
380 dbg_hid("%s: did not schedule the work item, was already "
381 "queued\n", __func__);
385 static void logi_dj_recv_forward_null_report(struct dj_receiver_dev *djrcv_dev,
386 struct dj_report *dj_report)
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;
393 djdev = djrcv_dev->paired_dj_devices[dj_report->device_index];
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);
398 kfifo_in(&djrcv_dev->notif_fifo, dj_report, sizeof(struct dj_report));
400 if (schedule_work(&djrcv_dev->work) == 0) {
401 dbg_hid("%s: did not schedule the work item, was already "
402 "queued\n", __func__);
404 return;
407 memset(reportbuffer, 0, sizeof(reportbuffer));
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");
423 static void logi_dj_recv_forward_report(struct dj_receiver_dev *djrcv_dev,
424 struct dj_report *dj_report)
426 /* We are called from atomic context (tasklet && djrcv->lock held) */
427 struct dj_device *dj_device;
429 dj_device = djrcv_dev->paired_dj_devices[dj_report->device_index];
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);
434 kfifo_in(&djrcv_dev->notif_fifo, dj_report, sizeof(struct dj_report));
436 if (schedule_work(&djrcv_dev->work) == 0) {
437 dbg_hid("%s: did not schedule the work item, was already "
438 "queued\n", __func__);
440 return;
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;
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");
457 static int logi_dj_recv_send_report(struct dj_receiver_dev *djrcv_dev,
458 struct dj_report *dj_report)
460 struct hid_device *hdev = djrcv_dev->hdev;
461 struct hid_report *report;
462 struct hid_report_enum *output_report_enum;
463 u8 *data = (u8 *)(&dj_report->device_index);
464 unsigned int i;
466 output_report_enum = &hdev->report_enum[HID_OUTPUT_REPORT];
467 report = output_report_enum->report_id_hash[REPORT_ID_DJ_SHORT];
469 if (!report) {
470 dev_err(&hdev->dev, "%s: unable to find dj report\n", __func__);
471 return -ENODEV;
474 for (i = 0; i < DJREPORT_SHORT_LENGTH - 1; i++)
475 report->field[0]->value[i] = data[i];
477 hid_hw_request(hdev, report, HID_REQ_SET_REPORT);
479 return 0;
482 static int logi_dj_recv_query_paired_devices(struct dj_receiver_dev *djrcv_dev)
484 struct dj_report *dj_report;
485 int retval;
487 /* no need to protect djrcv_dev->querying_devices */
488 if (djrcv_dev->querying_devices)
489 return 0;
491 dj_report = kzalloc(sizeof(struct dj_report), GFP_KERNEL);
492 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;
503 static int logi_dj_recv_switch_to_dj_mode(struct dj_receiver_dev *djrcv_dev,
504 unsigned timeout)
506 struct dj_report *dj_report;
507 int retval;
509 dj_report = kzalloc(sizeof(struct dj_report), GFP_KERNEL);
510 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);
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.
525 msleep(50);
527 return retval;
531 static int logi_dj_ll_open(struct hid_device *hid)
533 dbg_hid("%s:%s\n", __func__, hid->phys);
534 return 0;
538 static void logi_dj_ll_close(struct hid_device *hid)
540 dbg_hid("%s:%s\n", __func__, hid->phys);
543 static int logi_dj_ll_raw_request(struct hid_device *hid,
544 unsigned char reportnum, __u8 *buf,
545 size_t count, unsigned char report_type,
546 int reqtype)
548 struct dj_device *djdev = hid->driver_data;
549 struct dj_receiver_dev *djrcv_dev = djdev->dj_receiver_dev;
550 u8 *out_buf;
551 int ret;
553 if (buf[0] != REPORT_TYPE_LEDS)
554 return -EINVAL;
556 out_buf = kzalloc(DJREPORT_SHORT_LENGTH, GFP_ATOMIC);
557 if (!out_buf)
558 return -ENOMEM;
560 if (count < DJREPORT_SHORT_LENGTH - 2)
561 count = DJREPORT_SHORT_LENGTH - 2;
563 out_buf[0] = REPORT_ID_DJ_SHORT;
564 out_buf[1] = djdev->device_index;
565 memcpy(out_buf + 2, buf, count);
567 ret = hid_hw_raw_request(djrcv_dev->hdev, out_buf[0], out_buf,
568 DJREPORT_SHORT_LENGTH, report_type, reqtype);
570 kfree(out_buf);
571 return ret;
574 static void rdcat(char *rdesc, unsigned int *rsize, const char *data, unsigned int size)
576 memcpy(rdesc + *rsize, data, size);
577 *rsize += size;
580 static int logi_dj_ll_parse(struct hid_device *hid)
582 struct dj_device *djdev = hid->driver_data;
583 unsigned int rsize = 0;
584 char *rdesc;
585 int retval;
587 dbg_hid("%s\n", __func__);
589 djdev->hdev->version = 0x0111;
590 djdev->hdev->country = 0x00;
592 rdesc = kmalloc(MAX_RDESC_SIZE, GFP_KERNEL);
593 if (!rdesc)
594 return -ENOMEM;
596 if (djdev->reports_supported & STD_KEYBOARD) {
597 dbg_hid("%s: sending a kbd descriptor, reports_supported: %x\n",
598 __func__, djdev->reports_supported);
599 rdcat(rdesc, &rsize, kbd_descriptor, sizeof(kbd_descriptor));
602 if (djdev->reports_supported & STD_MOUSE) {
603 dbg_hid("%s: sending a mouse descriptor, reports_supported: "
604 "%x\n", __func__, djdev->reports_supported);
605 rdcat(rdesc, &rsize, mse_descriptor, sizeof(mse_descriptor));
608 if (djdev->reports_supported & MULTIMEDIA) {
609 dbg_hid("%s: sending a multimedia report descriptor: %x\n",
610 __func__, djdev->reports_supported);
611 rdcat(rdesc, &rsize, consumer_descriptor, sizeof(consumer_descriptor));
614 if (djdev->reports_supported & POWER_KEYS) {
615 dbg_hid("%s: sending a power keys report descriptor: %x\n",
616 __func__, djdev->reports_supported);
617 rdcat(rdesc, &rsize, syscontrol_descriptor, sizeof(syscontrol_descriptor));
620 if (djdev->reports_supported & MEDIA_CENTER) {
621 dbg_hid("%s: sending a media center report descriptor: %x\n",
622 __func__, djdev->reports_supported);
623 rdcat(rdesc, &rsize, media_descriptor, sizeof(media_descriptor));
626 if (djdev->reports_supported & KBD_LEDS) {
627 dbg_hid("%s: need to send kbd leds report descriptor: %x\n",
628 __func__, djdev->reports_supported);
631 retval = hid_parse_report(hid, rdesc, rsize);
632 kfree(rdesc);
634 return retval;
637 static int logi_dj_ll_start(struct hid_device *hid)
639 dbg_hid("%s\n", __func__);
640 return 0;
643 static void logi_dj_ll_stop(struct hid_device *hid)
645 dbg_hid("%s\n", __func__);
649 static struct hid_ll_driver logi_dj_ll_driver = {
650 .parse = logi_dj_ll_parse,
651 .start = logi_dj_ll_start,
652 .stop = logi_dj_ll_stop,
653 .open = logi_dj_ll_open,
654 .close = logi_dj_ll_close,
655 .raw_request = logi_dj_ll_raw_request,
659 static int logi_dj_raw_event(struct hid_device *hdev,
660 struct hid_report *report, u8 *data,
661 int size)
663 struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
664 struct dj_report *dj_report = (struct dj_report *) data;
665 unsigned long flags;
666 bool report_processed = false;
668 dbg_hid("%s, size:%d\n", __func__, size);
670 /* Here we receive all data coming from iface 2, there are 4 cases:
672 * 1) Data should continue its normal processing i.e. data does not
673 * come from the DJ collection, in which case we do nothing and
674 * return 0, so hid-core can continue normal processing (will forward
675 * to associated hidraw device)
677 * 2) Data is from DJ collection, and is intended for this driver i. e.
678 * data contains arrival, departure, etc notifications, in which case
679 * we queue them for delayed processing by the work queue. We return 1
680 * to hid-core as no further processing is required from it.
682 * 3) Data is from DJ collection, and informs a connection change,
683 * if the change means rf link loss, then we must send a null report
684 * to the upper layer to discard potentially pressed keys that may be
685 * repeated forever by the input layer. Return 1 to hid-core as no
686 * further processing is required.
688 * 4) Data is from DJ collection and is an actual input event from
689 * a paired DJ device in which case we forward it to the correct hid
690 * device (via hid_input_report() ) and return 1 so hid-core does not do
691 * anything else with it.
694 spin_lock_irqsave(&djrcv_dev->lock, flags);
695 if (dj_report->report_id == REPORT_ID_DJ_SHORT) {
696 switch (dj_report->report_type) {
697 case REPORT_TYPE_NOTIF_DEVICE_PAIRED:
698 case REPORT_TYPE_NOTIF_DEVICE_UNPAIRED:
699 logi_dj_recv_queue_notification(djrcv_dev, dj_report);
700 break;
701 case REPORT_TYPE_NOTIF_CONNECTION_STATUS:
702 if (dj_report->report_params[CONNECTION_STATUS_PARAM_STATUS] ==
703 STATUS_LINKLOSS) {
704 logi_dj_recv_forward_null_report(djrcv_dev, dj_report);
706 break;
707 default:
708 logi_dj_recv_forward_report(djrcv_dev, dj_report);
710 report_processed = true;
712 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
714 return report_processed;
717 static int logi_dj_probe(struct hid_device *hdev,
718 const struct hid_device_id *id)
720 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
721 struct dj_receiver_dev *djrcv_dev;
722 int retval;
724 if (is_dj_device((struct dj_device *)hdev->driver_data))
725 return -ENODEV;
727 dbg_hid("%s called for ifnum %d\n", __func__,
728 intf->cur_altsetting->desc.bInterfaceNumber);
730 /* Ignore interfaces 0 and 1, they will not carry any data, dont create
731 * any hid_device for them */
732 if (intf->cur_altsetting->desc.bInterfaceNumber !=
733 LOGITECH_DJ_INTERFACE_NUMBER) {
734 dbg_hid("%s: ignoring ifnum %d\n", __func__,
735 intf->cur_altsetting->desc.bInterfaceNumber);
736 return -ENODEV;
739 /* Treat interface 2 */
741 djrcv_dev = kzalloc(sizeof(struct dj_receiver_dev), GFP_KERNEL);
742 if (!djrcv_dev) {
743 dev_err(&hdev->dev,
744 "%s:failed allocating dj_receiver_dev\n", __func__);
745 return -ENOMEM;
747 djrcv_dev->hdev = hdev;
748 INIT_WORK(&djrcv_dev->work, delayedwork_callback);
749 spin_lock_init(&djrcv_dev->lock);
750 if (kfifo_alloc(&djrcv_dev->notif_fifo,
751 DJ_MAX_NUMBER_NOTIFICATIONS * sizeof(struct dj_report),
752 GFP_KERNEL)) {
753 dev_err(&hdev->dev,
754 "%s:failed allocating notif_fifo\n", __func__);
755 kfree(djrcv_dev);
756 return -ENOMEM;
758 hid_set_drvdata(hdev, djrcv_dev);
760 /* Call to usbhid to fetch the HID descriptors of interface 2 and
761 * subsequently call to the hid/hid-core to parse the fetched
762 * descriptors, this will in turn create the hidraw and hiddev nodes
763 * for interface 2 of the receiver */
764 retval = hid_parse(hdev);
765 if (retval) {
766 dev_err(&hdev->dev,
767 "%s:parse of interface 2 failed\n", __func__);
768 goto hid_parse_fail;
771 if (!hid_validate_values(hdev, HID_OUTPUT_REPORT, REPORT_ID_DJ_SHORT,
772 0, DJREPORT_SHORT_LENGTH - 1)) {
773 retval = -ENODEV;
774 goto hid_parse_fail;
777 /* Starts the usb device and connects to upper interfaces hiddev and
778 * hidraw */
779 retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
780 if (retval) {
781 dev_err(&hdev->dev,
782 "%s:hid_hw_start returned error\n", __func__);
783 goto hid_hw_start_fail;
786 retval = logi_dj_recv_switch_to_dj_mode(djrcv_dev, 0);
787 if (retval < 0) {
788 dev_err(&hdev->dev,
789 "%s:logi_dj_recv_switch_to_dj_mode returned error:%d\n",
790 __func__, retval);
791 goto switch_to_dj_mode_fail;
794 /* This is enabling the polling urb on the IN endpoint */
795 retval = hid_hw_open(hdev);
796 if (retval < 0) {
797 dev_err(&hdev->dev, "%s:hid_hw_open returned error:%d\n",
798 __func__, retval);
799 goto llopen_failed;
802 /* Allow incoming packets to arrive: */
803 hid_device_io_start(hdev);
805 retval = logi_dj_recv_query_paired_devices(djrcv_dev);
806 if (retval < 0) {
807 dev_err(&hdev->dev, "%s:logi_dj_recv_query_paired_devices "
808 "error:%d\n", __func__, retval);
809 goto logi_dj_recv_query_paired_devices_failed;
812 return retval;
814 logi_dj_recv_query_paired_devices_failed:
815 hid_hw_close(hdev);
817 llopen_failed:
818 switch_to_dj_mode_fail:
819 hid_hw_stop(hdev);
821 hid_hw_start_fail:
822 hid_parse_fail:
823 kfifo_free(&djrcv_dev->notif_fifo);
824 kfree(djrcv_dev);
825 hid_set_drvdata(hdev, NULL);
826 return retval;
830 #ifdef CONFIG_PM
831 static int logi_dj_reset_resume(struct hid_device *hdev)
833 int retval;
834 struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
836 retval = logi_dj_recv_switch_to_dj_mode(djrcv_dev, 0);
837 if (retval < 0) {
838 dev_err(&hdev->dev,
839 "%s:logi_dj_recv_switch_to_dj_mode returned error:%d\n",
840 __func__, retval);
843 return 0;
845 #endif
847 static void logi_dj_remove(struct hid_device *hdev)
849 struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
850 struct dj_device *dj_dev;
851 int i;
853 dbg_hid("%s\n", __func__);
855 cancel_work_sync(&djrcv_dev->work);
857 hid_hw_close(hdev);
858 hid_hw_stop(hdev);
860 /* I suppose that at this point the only context that can access
861 * the djrecv_data is this thread as the work item is guaranteed to
862 * have finished and no more raw_event callbacks should arrive after
863 * the remove callback was triggered so no locks are put around the
864 * code below */
865 for (i = 0; i < (DJ_MAX_PAIRED_DEVICES + DJ_DEVICE_INDEX_MIN); i++) {
866 dj_dev = djrcv_dev->paired_dj_devices[i];
867 if (dj_dev != NULL) {
868 hid_destroy_device(dj_dev->hdev);
869 kfree(dj_dev);
870 djrcv_dev->paired_dj_devices[i] = NULL;
874 kfifo_free(&djrcv_dev->notif_fifo);
875 kfree(djrcv_dev);
876 hid_set_drvdata(hdev, NULL);
879 static int logi_djdevice_probe(struct hid_device *hdev,
880 const struct hid_device_id *id)
882 int ret;
883 struct dj_device *dj_dev = hdev->driver_data;
885 if (!is_dj_device(dj_dev))
886 return -ENODEV;
888 ret = hid_parse(hdev);
889 if (!ret)
890 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
892 return ret;
895 static const struct hid_device_id logi_dj_receivers[] = {
896 {HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
897 USB_DEVICE_ID_LOGITECH_UNIFYING_RECEIVER)},
898 {HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
899 USB_DEVICE_ID_LOGITECH_UNIFYING_RECEIVER_2)},
903 MODULE_DEVICE_TABLE(hid, logi_dj_receivers);
905 static struct hid_driver logi_djreceiver_driver = {
906 .name = "logitech-djreceiver",
907 .id_table = logi_dj_receivers,
908 .probe = logi_dj_probe,
909 .remove = logi_dj_remove,
910 .raw_event = logi_dj_raw_event,
911 #ifdef CONFIG_PM
912 .reset_resume = logi_dj_reset_resume,
913 #endif
917 static const struct hid_device_id logi_dj_devices[] = {
918 {HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
919 USB_DEVICE_ID_LOGITECH_UNIFYING_RECEIVER)},
920 {HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
921 USB_DEVICE_ID_LOGITECH_UNIFYING_RECEIVER_2)},
925 static struct hid_driver logi_djdevice_driver = {
926 .name = "logitech-djdevice",
927 .id_table = logi_dj_devices,
928 .probe = logi_djdevice_probe,
932 static int __init logi_dj_init(void)
934 int retval;
936 dbg_hid("Logitech-DJ:%s\n", __func__);
938 retval = hid_register_driver(&logi_djreceiver_driver);
939 if (retval)
940 return retval;
942 retval = hid_register_driver(&logi_djdevice_driver);
943 if (retval)
944 hid_unregister_driver(&logi_djreceiver_driver);
946 return retval;
950 static void __exit logi_dj_exit(void)
952 dbg_hid("Logitech-DJ:%s\n", __func__);
954 hid_unregister_driver(&logi_djdevice_driver);
955 hid_unregister_driver(&logi_djreceiver_driver);
959 module_init(logi_dj_init);
960 module_exit(logi_dj_exit);
961 MODULE_LICENSE("GPL");
962 MODULE_AUTHOR("Logitech");
963 MODULE_AUTHOR("Nestor Lopez Casado");
964 MODULE_AUTHOR("nlopezcasad@logitech.com");