[PATCH v2] HID: Add full support for Logitech Unifying receivers

12927 views
Skip to first unread message

Nestor Lopez Casado

unread,
Aug 11, 2011, 11:30:02 AM8/11/11
to
With this driver, all the devices paired to a single Unifying
receiver are exposed to user processes in separated /input/dev
nodes.

Keyboards with different layouts can be treated differently,
Multiplayer games on single PC (like home theater PC) can
differentiate input coming from different kbds paired to the
same receiver.

Up to now, when Logitech Unifying receivers are connected to a
Linux based system, a single keyboard and a single mouse are
presented to the HID Layer, even if the Unifying receiver can
pair up to six compatible devices. The Unifying receiver by default
multiplexes all incoming events (from multiple keyboards/mice)
into these two.

Signed-off-by: Nestor Lopez Casado <nlope...@logitech.com>
---

Hi everyone,

I took a second look at your comments with the help of Benjamin
Tissoires, who is by the way working with us for a few weeks here
at Logitech.

We finally agreed to remove BUS_DJ, as you suggested, and we are now
using BUS_VIRTUAL. We can not use BUS_USB because that would assign
the hid-logitech-dj created "virtual" devices back to the hid-logitech-dj
module while they should be treated by hid-core.

Concerning the conditional compilation on_id hid_have_special_driver[],
we also agreed to remove them. This implies that if the driver is not included
by a certain distribution, then the receiver and all of the paired devices
will not work at all. Note that today, even if there is no driver installed
this devices work in a basic mode. For this reason we added a 'default m' in
the Kconfig

Overall, the totality of your suggestions have been addressed.

Cheers,
Nestor

drivers/hid/Kconfig | 9 +
drivers/hid/Makefile | 1 +
drivers/hid/hid-core.c | 2 +
drivers/hid/hid-ids.h | 3 +
drivers/hid/hid-logitech-dj.c | 945 +++++++++++++++++++++++++++++++++++++++++
drivers/hid/hid-logitech-dj.h | 116 +++++
6 files changed, 1076 insertions(+), 0 deletions(-)
create mode 100644 drivers/hid/hid-logitech-dj.c
create mode 100644 drivers/hid/hid-logitech-dj.h

diff --git a/drivers/hid/Kconfig b/drivers/hid/Kconfig
index 306b15f..e0f1360 100644
--- a/drivers/hid/Kconfig
+++ b/drivers/hid/Kconfig
@@ -245,6 +245,15 @@ config HID_LOGITECH
---help---
Support for Logitech devices that are not fully compliant with HID standard.

+config HID_LOGITECH_DJ
+ tristate "Logitech Unifying receivers full support"
+ depends on HID_LOGITECH
+ default m
+ ---help---
+ Say Y if you want support for Logitech Unifying receivers and devices.
+ Unifying receivers are capable of pairing up to 6 Logitech compliant
+ devices to the same receiver.
+
config LOGITECH_FF
bool "Logitech force feedback support"
depends on HID_LOGITECH
diff --git a/drivers/hid/Makefile b/drivers/hid/Makefile
index 0a0a38e..ff36e489 100644
--- a/drivers/hid/Makefile
+++ b/drivers/hid/Makefile
@@ -43,6 +43,7 @@ obj-$(CONFIG_HID_KEYTOUCH) += hid-keytouch.o
obj-$(CONFIG_HID_KYE) += hid-kye.o
obj-$(CONFIG_HID_LCPOWER) += hid-lcpower.o
obj-$(CONFIG_HID_LOGITECH) += hid-logitech.o
+obj-$(CONFIG_HID_LOGITECH_DJ) += hid-logitech-dj.o
obj-$(CONFIG_HID_MAGICMOUSE) += hid-magicmouse.o
obj-$(CONFIG_HID_MICROSOFT) += hid-microsoft.o
obj-$(CONFIG_HID_MONTEREY) += hid-monterey.o
diff --git a/drivers/hid/hid-core.c b/drivers/hid/hid-core.c
index 1a5cf0c..ab4ae12 100644
--- a/drivers/hid/hid-core.c
+++ b/drivers/hid/hid-core.c
@@ -1419,6 +1419,8 @@ static const struct hid_device_id hid_have_special_driver[] = {
{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_DFP_WHEEL) },
{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_G25_WHEEL) },
{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_G27_WHEEL) },
+ { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_UNIFYING_RECEIVER) },
+ { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_UNIFYING_RECEIVER_2) },
{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_WII_WHEEL) },
{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_RUMBLEPAD2) },
{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_SPACETRAVELLER) },
diff --git a/drivers/hid/hid-ids.h b/drivers/hid/hid-ids.h
index db63ccf..2081d1a 100644
--- a/drivers/hid/hid-ids.h
+++ b/drivers/hid/hid-ids.h
@@ -443,6 +443,8 @@
#define USB_DEVICE_ID_S510_RECEIVER_2 0xc517
#define USB_DEVICE_ID_LOGITECH_CORDLESS_DESKTOP_LX500 0xc512
#define USB_DEVICE_ID_MX3000_RECEIVER 0xc513
+#define USB_DEVICE_ID_LOGITECH_UNIFYING_RECEIVER 0xc52b
+#define USB_DEVICE_ID_LOGITECH_UNIFYING_RECEIVER_2 0xc532
#define USB_DEVICE_ID_SPACETRAVELLER 0xc623
#define USB_DEVICE_ID_SPACENAVIGATOR 0xc626
#define USB_DEVICE_ID_DINOVO_DESKTOP 0xc704
@@ -450,6 +452,7 @@
#define USB_DEVICE_ID_DINOVO_MINI 0xc71f
#define USB_DEVICE_ID_LOGITECH_MOMO_WHEEL2 0xca03

+
#define USB_VENDOR_ID_LUMIO 0x202e
#define USB_DEVICE_ID_CRYSTALTOUCH 0x0006
#define USB_DEVICE_ID_CRYSTALTOUCH_DUAL 0x0007
diff --git a/drivers/hid/hid-logitech-dj.c b/drivers/hid/hid-logitech-dj.c
new file mode 100644
index 0000000..82091ca
--- /dev/null
+++ b/drivers/hid/hid-logitech-dj.c
@@ -0,0 +1,945 @@
+/*
+ * HID driver for Logitech Unifying receivers
+ *
+ * Copyright (c) 2011 Logitech (c)
+ */
+
+/*
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ *
+ * Should you need to contact me, the author, you can do so by e-mail send
+ * your message to Nestor Lopez Casado <xnlopez at gmail com>
+ *
+ */
+
+
+#include <linux/device.h>
+#include <linux/hid.h>
+#include <linux/module.h>
+#include <linux/usb.h>
+#include "usbhid/usbhid.h"
+#include "hid-ids.h"
+#include "hid-logitech-dj.h"
+
+/* Keyboard descriptor (1) */
+static const char kbd_descriptor[] = {
+ 0x05, 0x01, /* USAGE_PAGE (generic Desktop) */
+ 0x09, 0x06, /* USAGE (Keyboard) */
+ 0xA1, 0x01, /* COLLECTION (Application) */
+ 0x85, 0x01, /* REPORT_ID (1) */
+ 0x95, 0x08, /* REPORT_COUNT (8) */
+ 0x75, 0x01, /* REPORT_SIZE (1) */
+ 0x15, 0x00, /* LOGICAL_MINIMUM (0) */
+ 0x25, 0x01, /* LOGICAL_MAXIMUM (1) */
+ 0x05, 0x07, /* USAGE_PAGE (Keyboard) */
+ 0x19, 0xE0, /* USAGE_MINIMUM (Left Control) */
+ 0x29, 0xE7, /* USAGE_MAXIMUM (Right GUI) */
+ 0x81, 0x02, /* INPUT (Data,Var,Abs) */
+ 0x95, 0x05, /* REPORT COUNT (5) */
+ 0x05, 0x08, /* USAGE PAGE (LED page) */
+ 0x19, 0x01, /* USAGE MINIMUM (1) */
+ 0x29, 0x05, /* USAGE MAXIMUM (5) */
+ 0x91, 0x02, /* OUTPUT (Data, Variable, Absolute) */
+ 0x95, 0x01, /* REPORT COUNT (1) */
+ 0x75, 0x03, /* REPORT SIZE (3) */
+ 0x91, 0x01, /* OUTPUT (Constant) */
+ 0x95, 0x06, /* REPORT_COUNT (6) */
+ 0x75, 0x08, /* REPORT_SIZE (8) */
+ 0x15, 0x00, /* LOGICAL_MINIMUM (0) */
+ 0x26, 0xFF, 0x00, /* LOGICAL_MAXIMUM (255) */
+ 0x05, 0x07, /* USAGE_PAGE (Keyboard) */
+ 0x19, 0x00, /* USAGE_MINIMUM (no event) */
+ 0x2A, 0xFF, 0x00, /* USAGE_MAXIMUM (reserved) */
+ 0x81, 0x00, /* INPUT (Data,Ary,Abs) */
+ 0xC0
+};
+
+/* Mouse descriptor (2) */
+static const char mse_descriptor[] = {
+ 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */
+ 0x09, 0x02, /* USAGE (Mouse) */
+ 0xA1, 0x01, /* COLLECTION (Application) */
+ 0x85, 0x02, /* REPORT_ID = 2 */
+ 0x09, 0x01, /* USAGE (pointer) */
+ 0xA1, 0x00, /* COLLECTION (physical) */
+ 0x05, 0x09, /* USAGE_PAGE (buttons) */
+ 0x19, 0x01, /* USAGE_MIN (1) */
+ 0x29, 0x10, /* USAGE_MAX (16) */
+ 0x15, 0x00, /* LOGICAL_MIN (0) */
+ 0x25, 0x01, /* LOGICAL_MAX (1) */
+ 0x95, 0x10, /* REPORT_COUNT (16) */
+ 0x75, 0x01, /* REPORT_SIZE (1) */
+ 0x81, 0x02, /* INPUT (data var abs) */
+ 0x05, 0x01, /* USAGE_PAGE (generic desktop) */
+ 0x16, 0x01, 0xF8, /* LOGICAL_MIN (-2047) */
+ 0x26, 0xFF, 0x07, /* LOGICAL_MAX (2047) */
+ 0x75, 0x0C, /* REPORT_SIZE (12) */
+ 0x95, 0x02, /* REPORT_COUNT (2) */
+ 0x09, 0x30, /* USAGE (X) */
+ 0x09, 0x31, /* USAGE (Y) */
+ 0x81, 0x06, /* INPUT */
+ 0x15, 0x81, /* LOGICAL_MIN (-127) */
+ 0x25, 0x7F, /* LOGICAL_MAX (127) */
+ 0x75, 0x08, /* REPORT_SIZE (8) */
+ 0x95, 0x01, /* REPORT_COUNT (1) */
+ 0x09, 0x38, /* USAGE (wheel) */
+ 0x81, 0x06, /* INPUT */
+ 0x05, 0x0C, /* USAGE_PAGE(consumer) */
+ 0x0A, 0x38, 0x02, /* USAGE(AC Pan) */
+ 0x95, 0x01, /* REPORT_COUNT (1) */
+ 0x81, 0x06, /* INPUT */
+ 0xC0, /* END_COLLECTION */
+ 0xC0, /* END_COLLECTION */
+};
+
+/* Consumer Control descriptor (3) */
+static const char consumer_descriptor[] = {
+ 0x05, 0x0C, /* USAGE_PAGE (Consumer Devices) */
+ 0x09, 0x01, /* USAGE (Consumer Control) */
+ 0xA1, 0x01, /* COLLECTION (Application) */
+ 0x85, 0x03, /* REPORT_ID = 3 */
+ 0x75, 0x10, /* REPORT_SIZE (16) */
+ 0x95, 0x02, /* REPORT_COUNT (2) */
+ 0x15, 0x01, /* LOGICAL_MIN (1) */
+ 0x26, 0x8C, 0x02, /* LOGICAL_MAX (652) */
+ 0x19, 0x01, /* USAGE_MIN (1) */
+ 0x2A, 0x8C, 0x02, /* USAGE_MAX (652) */
+ 0x81, 0x00, /* INPUT (Data Ary Abs) */
+ 0xC0, /* END_COLLECTION */
+}; /* */
+
+/* System control descriptor (4) */
+static const char syscontrol_descriptor[] = {
+ 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */
+ 0x09, 0x80, /* USAGE (System Control) */
+ 0xA1, 0x01, /* COLLECTION (Application) */
+ 0x85, 0x04, /* REPORT_ID = 4 */
+ 0x75, 0x02, /* REPORT_SIZE (2) */
+ 0x95, 0x01, /* REPORT_COUNT (1) */
+ 0x15, 0x01, /* LOGICAL_MIN (1) */
+ 0x25, 0x03, /* LOGICAL_MAX (3) */
+ 0x09, 0x82, /* USAGE (System Sleep) */
+ 0x09, 0x81, /* USAGE (System Power Down) */
+ 0x09, 0x83, /* USAGE (System Wake Up) */
+ 0x81, 0x60, /* INPUT (Data Ary Abs NPrf Null) */
+ 0x75, 0x06, /* REPORT_SIZE (6) */
+ 0x81, 0x03, /* INPUT (Cnst Var Abs) */
+ 0xC0, /* END_COLLECTION */
+};
+
+/* Media descriptor (8) */
+static const char media_descriptor[] = {
+ 0x06, 0xbc, 0xff, /* Usage Page 0xffbc */
+ 0x09, 0x88, /* Usage 0x0088 */
+ 0xa1, 0x01, /* BeginCollection */
+ 0x85, 0x08, /* Report ID 8 */
+ 0x19, 0x01, /* Usage Min 0x0001 */
+ 0x29, 0xff, /* Usage Max 0x00ff */
+ 0x15, 0x01, /* Logical Min 1 */
+ 0x26, 0xff, 0x00, /* Logical Max 255 */
+ 0x75, 0x08, /* Report Size 8 */
+ 0x95, 0x01, /* Report Count 1 */
+ 0x81, 0x00, /* Input */
+ 0xc0, /* EndCollection */
+}; /* */
+
+/* Maximum size of all defined hid reports in bytes (including report id) */
+#define MAX_REPORT_SIZE 8
+
+/* Number of possible hid report types that can be created by this driver.
+ *
+ * Right now, RF report types have the same report types (or report id's)
+ * than the hid report created from those RF reports. In the future
+ * this doesnt have to be true.
+ *
+ * For instance, RF report type 0x01 which has a size of 8 bytes, corresponds
+ * to hid report id 0x01, this is standard keyboard. Same thing applies to mice
+ * reports and consumer control, etc. If a new RF report is created, it doesn't
+ * has to have the same report id as its corresponding hid report, so an
+ * translation may have to take place for future report types.
+ */
+#define NUMBER_OF_HID_REPORTS 32
+static const u8 hid_reportid_size_map[NUMBER_OF_HID_REPORTS] = {
+ 0, /* Not used */
+ 8, /* Standard keyboard */
+ 8, /* Standard mouse */
+ 5, /* Consumer control */
+ 2, /* System control */
+ 0, /* Not used */
+ 0, /* Not used */
+ 0, /* Not used */
+ 2, /* Media Center */
+ 0, /* Not used */
+ 0, /* Not used */
+ 0, /* Not used */
+ 0, /* Not used */
+ 0, /* Not used */
+ 0, /* Not used */
+ 0, /* Not used */
+ 0, /* Not used */
+ 0, /* Not used */
+ 0, /* Not used */
+ 0, /* Not used */
+ 0, /* Not used */
+ 0, /* Not used */
+ 0, /* Not used */
+ 0, /* Not used */
+ 0, /* Not used */
+ 0, /* Not used */
+ 0, /* Not used */
+ 0, /* Not used */
+ 0, /* Not used */
+ 0, /* Not used */
+ 0, /* Not used */
+ 0 /* Not used */
+};
+
+
+#define LOGITECH_DJ_INTERFACE_NUMBER 0x02
+
+static struct hid_ll_driver logi_dj_ll_driver;
+
+static int logi_dj_output_hidraw_report(struct hid_device *hid, u8 * buf,
+ size_t count,
+ unsigned char report_type);
+
+static void logi_dj_recv_destroy_djhid_device(struct dj_receiver_dev *djrcv_dev,
+ struct dj_report *dj_report)
+{
+ /* Called in delayed work context */
+ struct dj_device *dj_dev;
+ unsigned long flags;
+
+ spin_lock_irqsave(&djrcv_dev->lock, flags);
+ dj_dev = djrcv_dev->paired_dj_devices[dj_report->device_index];
+ djrcv_dev->paired_dj_devices[dj_report->device_index] = NULL;
+ spin_unlock_irqrestore(&djrcv_dev->lock, flags);
+
+ if (dj_dev != NULL) {
+ hid_destroy_device(dj_dev->hdev);
+ kfree(dj_dev);
+ } else {
+ dev_err(&djrcv_dev->hdev->dev, "logi_dj_recv_destroy_djhid_device:"
+ "can't destroy a NULL device\n");
+ }
+}
+
+
+static void logi_dj_recv_add_djhid_device(struct dj_receiver_dev
+ *djrcv_dev,
+ struct dj_report *dj_report)
+{
+ /* Called in delayed work context */
+ struct usb_interface *intf = to_usb_interface(djrcv_dev->hdev->dev.parent);
+ struct usb_device *usbdev = interface_to_usbdev(intf);
+ struct hid_device *dj_hiddev;
+ struct dj_device *dj_dev;
+ unsigned long flags;
+ unsigned char tmpstr[20];
+
+ if (dj_report->report_params[NOTIF_DEVICE_PAIRED_PARAM_SPFUNCTION] &
+ SPFUNCTION_DEVICE_LIST_EMPTY) {
+ dbg_hid("logi_dj_recv_add_djhid_device: device list is empty\n");
+ return;
+ }
+
+ dj_hiddev = hid_allocate_device();
+ if (IS_ERR(dj_hiddev)) {
+ dev_err(&djrcv_dev->hdev->dev, "logi_dj_recv_add_djhid_device:"
+ "hid_allocate_device failed\n");
+ return;
+ }
+
+ dj_hiddev->ll_driver = &logi_dj_ll_driver;
+ dj_hiddev->hid_output_raw_report = logi_dj_output_hidraw_report;
+
+ dj_hiddev->dev.parent = &djrcv_dev->hdev->dev;
+ dj_hiddev->bus = BUS_VIRTUAL;
+ dj_hiddev->vendor = le16_to_cpu(usbdev->descriptor.idVendor);
+ dj_hiddev->product = le16_to_cpu(usbdev->descriptor.idProduct);
+ dj_hiddev->name[0] = 0;
+ strlcpy(dj_hiddev->name, "Logitech Unifying Device. ", sizeof(dj_hiddev->name));
+ snprintf(tmpstr, sizeof(tmpstr), "Wireless PID:%02x%02x",
+ dj_report->report_params[NOTIF_DEVICE_PAIRED_PARAM_EQUAD_ID_MSB],
+ dj_report->report_params[NOTIF_DEVICE_PAIRED_PARAM_EQUAD_ID_LSB]);
+ strlcat(dj_hiddev->name, tmpstr, sizeof(dj_hiddev->name));
+
+ usb_make_path(usbdev, dj_hiddev->phys, sizeof(dj_hiddev->phys));
+ snprintf(tmpstr, sizeof(tmpstr), ":%d", dj_report->device_index);
+ strlcat(dj_hiddev->phys, tmpstr, sizeof(dj_hiddev->phys));
+
+ dj_dev = kzalloc(sizeof(struct dj_device), GFP_KERNEL);
+
+ if (IS_ERR(dj_dev)) {
+ dev_err(&djrcv_dev->hdev->dev,
+ "logi_dj_recv_add_djhid_device:failed allocating dj_device\n");
+ goto dj_device_allocate_fail;
+ }
+
+ dj_dev->reports_supported = le32_to_cpu(
+ dj_report->
+ report_params[NOTIF_DEVICE_PAIRED_RF_REPORT_TYPE_LSB]);
+ dj_dev->hdev = dj_hiddev;
+ dj_dev->device_index = dj_report->device_index;
+ dj_hiddev->driver_data = dj_dev;
+
+ if (hid_add_device(dj_hiddev)) {
+ dev_err(&djrcv_dev->hdev->dev,
+ "logi_dj_recv_add_djhid_device:failed adding dj_device\n");
+ goto hid_add_device_fail;
+ }
+
+ spin_lock_irqsave(&djrcv_dev->lock, flags);
+ djrcv_dev->paired_dj_devices[dj_report->device_index] = dj_dev;
+ spin_unlock_irqrestore(&djrcv_dev->lock, flags);
+
+ return;
+
+hid_add_device_fail:
+ kfree(dj_dev);
+dj_device_allocate_fail:
+ hid_destroy_device(dj_hiddev);
+}
+
+
+static int notif_fifo_push(struct dj_receiver_dev *djrcv_dev,
+ struct dj_report *dj_report)
+{
+ /* We are called from atomic context (tasklet && djrcv->lock held) */
+
+ if (!djrcv_dev->notif_fifo_full) {
+ memcpy(&djrcv_dev->notif_fifo[djrcv_dev->notif_fifo_end &
+ (DJ_MAX_NUMBER_NOTIFICATIONS - 1)],
+ dj_report,
+ sizeof(struct dj_report));
+ djrcv_dev->notif_fifo_end++;
+
+ if (((djrcv_dev->notif_fifo_end ^ djrcv_dev->notif_fifo_start) &
+ (DJ_MAX_NUMBER_NOTIFICATIONS - 1)) == 0)
+ djrcv_dev->notif_fifo_full = 1;
+ return 0;
+ }
+ return -1;
+}
+
+static int notif_fifo_pop(struct dj_receiver_dev *djrcv_dev,
+ struct dj_report *dj_report)
+{
+ /* We are called from atomic context (tasklet && djrcv->lock held) */
+
+ if ((djrcv_dev->notif_fifo_start != djrcv_dev->notif_fifo_end) ||
+ (djrcv_dev->notif_fifo_full == 1)) {
+ memcpy(dj_report,
+ &djrcv_dev->notif_fifo[djrcv_dev->notif_fifo_start &
+ (DJ_MAX_NUMBER_NOTIFICATIONS - 1)],
+ sizeof(struct dj_report));
+ djrcv_dev->notif_fifo_start++;
+ djrcv_dev->notif_fifo_full = 0;
+ return (djrcv_dev->notif_fifo_start !=
+ djrcv_dev->notif_fifo_end) ? 1 : 0;
+ }
+ return -1;
+}
+
+static void delayedwork_callback(struct work_struct *work)
+{
+ struct dj_receiver_dev *djrcv_dev =
+ container_of(work, struct dj_receiver_dev, delayed_work);
+
+ struct dj_report dj_report;
+ unsigned long flags;
+ int process_more;
+
+ dbg_hid("delayedwork_callback\n");
+
+ spin_lock_irqsave(&djrcv_dev->lock, flags);
+ process_more = notif_fifo_pop(djrcv_dev, &dj_report);
+ if (process_more < 0) {
+ dev_err(&djrcv_dev->hdev->dev, "delayedwork_callback:"
+ "workitem triggered without notifications available\n");
+ } else if (process_more > 0) {
+ if (schedule_work(&djrcv_dev->delayed_work) == 0) {
+ dbg_hid("delayedwork_callback:"
+ " did not schedule the work item, was already queued\n");
+ }
+ }
+ spin_unlock_irqrestore(&djrcv_dev->lock, flags);
+
+ if (dj_report.report_type == REPORT_TYPE_NOTIF_DEVICE_PAIRED)
+ logi_dj_recv_add_djhid_device(djrcv_dev, &dj_report);
+ else if (dj_report.report_type == REPORT_TYPE_NOTIF_DEVICE_UNPAIRED)
+ logi_dj_recv_destroy_djhid_device(djrcv_dev, &dj_report);
+ else
+ dbg_hid("delayedwork_callback: unexpected report type\n");
+}
+
+static int logi_dj_recv_queue_notification(struct dj_receiver_dev *djrcv_dev,
+ struct dj_report *dj_report)
+{
+ /* We are called from atomic context (tasklet && djrcv->lock held) */
+
+ if (notif_fifo_push(djrcv_dev, dj_report)) {
+ dev_err(&djrcv_dev->hdev->dev,
+ "logi_dj_recv_queue_notification:"
+ "unexpected number of notifications arrived\n");
+ return 0;
+ }
+
+ if (schedule_work(&djrcv_dev->delayed_work) == 0) {
+ dbg_hid("logi_dj_recv_queue_notification:"
+ " did not schedule the work item, was already queued\n");
+ }
+ return 0;
+}
+
+static void logi_dj_recv_forward_null_report(struct dj_receiver_dev *djrcv_dev,
+ struct dj_report *dj_report)
+{
+ /* We are called from atomic context (tasklet && djrcv->lock held) */
+ unsigned int i;
+ u8 reportbuffer[MAX_REPORT_SIZE];
+ struct dj_device *djdev;
+
+ memset(reportbuffer, 0, sizeof(reportbuffer));
+
+ if (djrcv_dev->paired_dj_devices[dj_report->device_index] == NULL) {
+ dbg_hid("djrcv_dev->paired_dj_devices[dj_report->device_index]"
+ " is NULL, index %d\n", dj_report->device_index);
+ return;
+ }
+
+ djdev = djrcv_dev->paired_dj_devices[dj_report->device_index];
+
+ for (i = 0; i < NUMBER_OF_HID_REPORTS; i++) {
+ if (djdev->reports_supported & (1 << i)) {
+ reportbuffer[0] = i;
+ if (hid_input_report(djdev->hdev,
+ HID_INPUT_REPORT,
+ reportbuffer,
+ hid_reportid_size_map[i], 1)) {
+ dbg_hid("hid_input_report error sending null report\n");
+ }
+ }
+ }
+}
+
+static void logi_dj_recv_forward_report(struct dj_receiver_dev *djrcv_dev,
+ struct dj_report *dj_report)
+{
+ /* We are called from atomic context (tasklet && djrcv->lock held) */
+
+ if (djrcv_dev->paired_dj_devices[dj_report->device_index] == NULL) {
+ dbg_hid("djrcv_dev->paired_dj_devices[dj_report->device_index]"
+ " is NULL, index %d\n", dj_report->device_index);
+ return;
+ }
+
+ if ((dj_report->report_type > (ARRAY_SIZE(hid_reportid_size_map) - 1)) ||
+ (hid_reportid_size_map[dj_report->report_type] == 0)) {
+ dbg_hid("invalid report type:%x\n", dj_report->report_type);
+ return;
+ }
+
+ if (hid_input_report(djrcv_dev->paired_dj_devices[dj_report->device_index]->hdev,
+ HID_INPUT_REPORT, &dj_report->report_type,
+ hid_reportid_size_map[dj_report->report_type], 1)) {
+ dbg_hid("hid_input_report error\n");
+ }
+
+ return;
+
+}
+
+
+static int logi_dj_recv_send_report(struct dj_receiver_dev *djrcv_dev,
+ struct dj_report *dj_report)
+{
+ struct hid_device *hdev = djrcv_dev->hdev;
+ int sent_bytes;
+
+ if (!hdev->hid_output_raw_report) {
+ dev_err(&hdev->dev, "logi_dj_recv_send_report:"
+ "hid_output_raw_report is null\n");
+ return -ENODEV;
+ }
+
+ sent_bytes = hdev->hid_output_raw_report(hdev, (u8 *) dj_report,
+ sizeof(struct dj_report),
+ HID_OUTPUT_REPORT);
+
+ return (sent_bytes < 0) ? sent_bytes : 0;
+}
+
+static int logi_dj_recv_query_paired_devices(struct dj_receiver_dev
+ *djrcv_dev)
+{
+ struct dj_report dj_report;
+
+ memset(&dj_report, 0, sizeof(dj_report));
+ dj_report.report_id = REPORT_ID_DJ_SHORT;
+ dj_report.device_index = 0xFF;
+ dj_report.report_type = REPORT_TYPE_CMD_GET_PAIRED_DEVICES;
+ return logi_dj_recv_send_report(djrcv_dev, &dj_report);
+}
+
+static int logi_dj_recv_switch_to_dj_mode(struct dj_receiver_dev
+ *djrcv_dev, unsigned timeout)
+{
+ struct dj_report dj_report;
+
+ memset(&dj_report, 0, sizeof(dj_report));
+ dj_report.report_id = REPORT_ID_DJ_SHORT;
+ dj_report.device_index = 0xFF;
+ dj_report.report_type = REPORT_TYPE_CMD_SWITCH;
+ dj_report.report_params[CMD_SWITCH_PARAM_DEVBITFIELD] = 0x1F;
+ dj_report.report_params[CMD_SWITCH_PARAM_TIMEOUT_SECONDS] = (u8) timeout;
+ return logi_dj_recv_send_report(djrcv_dev, &dj_report);
+}
+
+
+static int logi_dj_ll_open(struct hid_device *hid)
+{
+ dbg_hid("logi_dj_ll_open:%s\n", hid->phys);
+ return 0;
+
+}
+
+static void logi_dj_ll_close(struct hid_device *hid)
+{
+ dbg_hid("logi_dj_ll_close:%s\n", hid->phys);
+}
+
+
+
+static int logi_dj_output_hidraw_report(struct hid_device *hid, u8 * buf,
+ size_t count,
+ unsigned char report_type)
+{
+ /* Called by hid raw to send data */
+ dbg_hid("logi_dj_output_hidraw_report\n");
+
+ return 0;
+}
+
+static int logi_dj_ll_parse(struct hid_device *hid)
+{
+ struct dj_device *djdev = hid->driver_data;
+ int retval = 0;
+
+ dbg_hid("logi_dj_ll_parse\n");
+
+ djdev->hdev->version = le16_to_cpu(0x0111);
+ djdev->hdev->country = 0x00;
+
+ if (djdev->reports_supported & STD_KEYBOARD) {
+ dbg_hid("logi_dj_ll_parse: "
+ "sending a kbd descriptor, reports_supported: %x\n",
+ djdev->reports_supported);
+ retval = hid_parse_report(hid,
+ (u8 *) kbd_descriptor,
+ sizeof(kbd_descriptor));
+ if (retval) {
+ dbg_hid("logi_dj_ll_parse:"
+ " sending a kbd descriptor, hid_parse failed error: %d\n",
+ retval);
+ return retval;
+ }
+ }
+
+ if (djdev->reports_supported & STD_MOUSE) {
+ dbg_hid("logi_dj_ll_parse:"
+ "sending a mouse descriptor, reports_supported: %x\n",
+ djdev->reports_supported);
+ retval = hid_parse_report(hid,
+ (u8 *) mse_descriptor,
+ sizeof(mse_descriptor));
+ if (retval) {
+ dbg_hid("logi_dj_ll_parse:"
+ " sending a mouse descriptor, hid_parse failed error: %d\n",
+ retval);
+ return retval;
+ }
+ }
+
+ if (djdev->reports_supported & MULTIMEDIA) {
+ dbg_hid("logi_dj_ll_parse:"
+ " sending a multimedia report descriptor: %x\n",
+ djdev->reports_supported);
+ retval = hid_parse_report(hid,
+ (u8 *) consumer_descriptor,
+ sizeof(consumer_descriptor));
+ if (retval) {
+ dbg_hid("logi_dj_ll_parse:"
+ " sending a consumer_descriptor, hid_parse failed error: %d\n",
+ retval);
+ return retval;
+ }
+ }
+
+ if (djdev->reports_supported & POWER_KEYS) {
+ dbg_hid("logi_dj_ll_parse:"
+ " sending a power keys report descriptor: %x\n",
+ djdev->reports_supported);
+ retval = hid_parse_report(hid,
+ (u8 *) syscontrol_descriptor,
+ sizeof(syscontrol_descriptor));
+ if (retval) {
+ dbg_hid("logi_dj_ll_parse:"
+ " sending a syscontrol_descriptor, hid_parse failed error: %d\n",
+ retval);
+ return retval;
+ }
+ }
+
+ if (djdev->reports_supported & MEDIA_CENTER) {
+ dbg_hid("logi_dj_ll_parse:"
+ " sending a media center report descriptor: %x\n",
+ djdev->reports_supported);
+ retval = hid_parse_report(hid,
+ (u8 *) media_descriptor,
+ sizeof(media_descriptor));
+ if (retval) {
+ dbg_hid("logi_dj_ll_parse:"
+ " sending a media_descriptor, hid_parse failed error: %d\n",
+ retval);
+ return retval;
+ }
+ }
+
+ if (djdev->reports_supported & KBD_LEDS) {
+ dbg_hid("logi_dj_ll_parse:"
+ " need to send kbd leds report descriptor: %x\n",
+ djdev->reports_supported);
+ }
+ return 0;
+
+}
+
+static int logi_dj_ll_input_event(struct input_dev *dev, unsigned int type,
+ unsigned int code, int value)
+{
+ /* Sent by the input layer to handle leds and Force Feedback */
+ struct hid_device *dj_hiddev = input_get_drvdata(dev);
+ struct dj_device *dj_dev = dj_hiddev->driver_data;
+
+ struct dj_receiver_dev *djrcv_dev =
+ dev_get_drvdata(dj_hiddev->dev.parent);
+ struct hid_device *dj_rcv_hiddev = djrcv_dev->hdev;
+
+ struct hid_field *field;
+ struct hid_report *report;
+ unsigned char data[8];
+ int offset;
+
+ dbg_hid("logi_dj_ll_input_event: %s, type:%d | code:%d | value:%d\n",
+ dev->phys, type, code, value);
+
+ if (type != EV_LED)
+ return -1;
+
+ offset = hidinput_find_field(dj_hiddev, type, code, &field);
+
+ if (offset == -1) {
+ dev_warn(&dev->dev, "event field not found\n");
+ return -1;
+ }
+ hid_set_field(field, offset, value);
+ hid_output_report(field->report, &data[0]);
+
+ report = dj_rcv_hiddev->
+ report_enum[HID_OUTPUT_REPORT].report_id_hash[REPORT_ID_DJ_SHORT];
+ hid_set_field(report->field[0], 0, dj_dev->device_index);
+ hid_set_field(report->field[0], 1, REPORT_TYPE_LEDS);
+ hid_set_field(report->field[0], 2, data[1]);
+
+ usbhid_submit_report(dj_rcv_hiddev, report, USB_DIR_OUT);
+
+ return 0;
+
+}
+
+static int logi_dj_ll_start(struct hid_device *hid)
+{
+ dbg_hid("logi_dj_ll_start\n");
+ return 0;
+}
+
+static void logi_dj_ll_stop(struct hid_device *hid)
+{
+ dbg_hid("logi_dj_ll_stop\n");
+}
+
+static int logi_dj_ll_power(struct hid_device *hid, int lvl)
+{
+
+ dbg_hid("logi_dj_ll_power\n");
+ return 0;
+}
+
+
+static struct hid_ll_driver logi_dj_ll_driver = {
+ .parse = logi_dj_ll_parse,
+ .start = logi_dj_ll_start,
+ .stop = logi_dj_ll_stop,
+ .open = logi_dj_ll_open,
+ .close = logi_dj_ll_close,
+ .power = logi_dj_ll_power,
+ .hidinput_input_event = logi_dj_ll_input_event,
+};
+
+
+static int logi_dj_raw_event(struct hid_device *hdev,
+ struct hid_report *report, u8 * data,
+ int size)
+{
+ struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
+ struct dj_report *dj_report = (struct dj_report *) data;
+ unsigned long flags;
+ int report_processed = 0;
+
+ dbg_hid("logi_dj_raw_event, size:%d\n", size);
+
+ /* Here we receive all data coming from iface 2, there are 4 cases:
+ *
+ * 1) Data should continue its normal processing i.e. data does not
+ * come from the DJ collection, in which case we do nothing and
+ * return 0, so hid-core can continue normal processing (will forward
+ * to associated hidraw device)
+ *
+ * 2) Data is from DJ collection, and is intended for this driver i. e.
+ * data contains arrival, departure, etc notifications, in which case
+ * we queue them for delayed processing by the work queue. We return 1
+ * to hid-core as no further processing is required from it.
+ *
+ * 3) Data is from DJ collection, and informs a connection change,
+ * if the change means rf link loss, then we must send a null report
+ * to the upper layer to discard potentially pressed keys that may be
+ * repeated forever by the input layer. Return 1 to hid-core as no further
+ * processing is required.
+ *
+ * 4) Data is from DJ collection and is an actual input event from
+ * a paired DJ device in which case we forward it to the correct hid
+ * device (via hid_input_report() ) and return 1 so hid-core does not do
+ * anything else with it.
+ */
+
+ spin_lock_irqsave(&djrcv_dev->lock, flags);
+ if (dj_report->report_id == REPORT_ID_DJ_SHORT) {
+ if ((dj_report->report_type == REPORT_TYPE_NOTIF_DEVICE_PAIRED) ||
+ (dj_report->report_type == REPORT_TYPE_NOTIF_DEVICE_UNPAIRED)) {
+ logi_dj_recv_queue_notification(djrcv_dev, dj_report);
+ } else if (dj_report->report_type == REPORT_TYPE_NOTIF_CONNECTION_STATUS) {
+ if (dj_report->report_params[NOTIF_CONNECTION_STATUS_PARAM_STATUS] ==
+ STATUS_LINKLOSS) {
+ logi_dj_recv_forward_null_report(djrcv_dev, dj_report);
+ }
+ } else {
+ logi_dj_recv_forward_report(djrcv_dev, dj_report);
+ }
+ report_processed = 1;
+ }
+ spin_unlock_irqrestore(&djrcv_dev->lock, flags);
+
+
+ return report_processed;
+}
+
+static int logi_dj_probe(struct hid_device *hdev,
+ const struct hid_device_id *id)
+{
+ struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
+ struct dj_receiver_dev *djrcv_dev;
+ int retval = 0;
+
+ dbg_hid("logi_dj_probe called for ifnum %d\n",
+ intf->cur_altsetting->desc.bInterfaceNumber);
+
+ /* Ignore interfaces 0 and 1, they will not carry any data, dont create
+ * any hid_device for them */
+ if (intf->cur_altsetting->desc.bInterfaceNumber !=
+ LOGITECH_DJ_INTERFACE_NUMBER) {
+ dbg_hid("logi_dj_probe: ignoring ifnum %d\n",
+ intf->cur_altsetting->desc.bInterfaceNumber);
+ return -ENODEV;
+ }
+
+ /* Treat interface 2 */
+
+ djrcv_dev = kzalloc(sizeof(struct dj_receiver_dev), GFP_KERNEL);
+ if (IS_ERR(djrcv_dev)) {
+ dev_err(&hdev->dev,
+ "logi_dj_probe:failed allocating dj_receiver_dev\n");
+ return -ENOMEM;
+ }
+ djrcv_dev->hdev = hdev;
+ INIT_WORK(&djrcv_dev->delayed_work, delayedwork_callback);
+ spin_lock_init(&djrcv_dev->lock);
+ hid_set_drvdata(hdev, djrcv_dev);
+
+ /* Call to usbhid to fetch the HID descriptors of interface 2 and subsequently
+ * call to the hid/hid-core to parse the fetched descriptors, this will in turn
+ * create the hidraw and hiddev nodes for interface 2 of the receiver */
+ retval = hid_parse(hdev);
+ if (retval) {
+ dev_err(&hdev->dev,
+ "logi_dj_probe:parse of interface 2 failed\n");
+ goto hid_parse_fail;
+ }
+
+ /* Starts the usb device and connects to upper interfaces hiddev and hidraw */
+ retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
+ if (retval) {
+ dev_err(&hdev->dev,
+ "logi_dj_probe:hid_hw_start returned error\n");
+ goto hid_hw_start_fail;
+ }
+
+ retval = logi_dj_recv_switch_to_dj_mode(djrcv_dev, 0);
+ if (retval < 0) {
+ dev_err(&hdev->dev,
+ "logi_dj_probe:logi_dj_recv_switch_to_dj_mode returned error:%d\n",
+ retval);
+ goto switch_to_dj_mode_fail;
+ }
+
+ /* This is enabling the polling urb on the IN endpoint */
+ retval = hdev->ll_driver->open(hdev);
+ if (retval < 0) {
+ dev_err(&hdev->dev,
+ "logi_dj_probe:hdev->ll_driver->open returned error:%d\n",
+ retval);
+ goto llopen_failed;
+ }
+
+ retval = logi_dj_recv_query_paired_devices(djrcv_dev);
+ if (retval < 0) {
+ dev_err(&hdev->dev,
+ "logi_dj_probe:logi_dj_recv_query_paired_devices error:%d\n",
+ retval);
+ goto logi_dj_recv_query_paired_devices_failed;
+ }
+
+ return retval;
+
+logi_dj_recv_query_paired_devices_failed:
+ hdev->ll_driver->close(hdev);
+
+llopen_failed:
+switch_to_dj_mode_fail:
+ hid_hw_stop(hdev);
+
+hid_hw_start_fail:
+hid_parse_fail:
+ kfree(djrcv_dev);
+ hid_set_drvdata(hdev, NULL);
+ return retval;
+
+}
+
+static void logi_dj_remove(struct hid_device *hdev)
+{
+ struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
+ struct dj_device *dj_dev;
+ int i;
+
+ dbg_hid("logi_dj_remove\n");
+
+ cancel_work_sync(&djrcv_dev->delayed_work);
+
+ /* I suppose that at this point the only context that can access
+ * the djrecv_data is this thread as the work item is guaranteed to
+ * have finished and no more raw_event callbacks should arrive after
+ * the remove callback was triggered so no locks are put around the
+ * code below */
+ for (i = 0; i < DJ_MAX_PAIRED_DEVICES; i++) {
+ dj_dev = djrcv_dev->paired_dj_devices[i];
+ if (dj_dev != NULL) {
+ hid_destroy_device(dj_dev->hdev);
+ kfree(dj_dev);
+ djrcv_dev->paired_dj_devices[i] = NULL;
+ }
+ }
+
+ hdev->ll_driver->close(hdev);
+ hid_hw_stop(hdev);
+ /* Free the dj_recv_dev struct */
+ kfree(hid_get_drvdata(hdev));
+ hid_set_drvdata(hdev, NULL);
+}
+
+static const struct hid_device_id logi_dj_receivers[] = {
+ {HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xc52b)},
+ {HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xc532)},
+ {}
+};
+
+MODULE_DEVICE_TABLE(hid, logi_dj_receivers);
+
+static struct hid_driver logi_djreceiver_driver = {
+ .name = "logitech-djreceiver",
+ .id_table = logi_dj_receivers,
+ .probe = logi_dj_probe,
+ .remove = logi_dj_remove,
+ .raw_event = logi_dj_raw_event
+};
+
+#define HID_DEVICE(b, ven, prod) \
+ .bus = (b), \
+ .vendor = (ven), .product = (prod)
+
+
+static const struct hid_device_id logi_dj_devices[] = {
+ {HID_DEVICE(BUS_VIRTUAL, HID_ANY_ID, HID_ANY_ID)},
+ {}
+};
+
+static struct hid_driver logi_djdevice_driver = {
+ .name = "generic-dj",
+ .id_table = logi_dj_devices,
+};
+
+
+static int __init logi_dj_init(void)
+{
+ int retval = 0;
+
+ dbg_hid("Logitech-DJ:logi_dj_init\n");
+
+ retval = hid_register_driver(&logi_djreceiver_driver);
+ if (retval)
+ return retval;
+
+ retval = hid_register_driver(&logi_djdevice_driver);
+ if (retval)
+ hid_unregister_driver(&logi_djreceiver_driver);
+
+ return retval;
+
+}
+
+static void __exit logi_dj_exit(void)
+{
+ dbg_hid("Logitech-DJ:logi_dj_exit\n");
+
+ hid_unregister_driver(&logi_djdevice_driver);
+ hid_unregister_driver(&logi_djreceiver_driver);
+
+}
+
+module_init(logi_dj_init);
+module_exit(logi_dj_exit);
+MODULE_LICENSE("GPL");
+MODULE_AUTHOR("Logitech");
+MODULE_AUTHOR("Nestor Lopez Casado");
diff --git a/drivers/hid/hid-logitech-dj.h b/drivers/hid/hid-logitech-dj.h
new file mode 100644
index 0000000..459e1f0
--- /dev/null
+++ b/drivers/hid/hid-logitech-dj.h
@@ -0,0 +1,116 @@
+#ifndef __HID_LOGITECH_DJ_H
+#define __HID_LOGITECH_DJ_H
+
+/*
+ * HID driver for Logitech Unifying receivers
+ *
+ * Copyright (c) 2011 Logitech (c)
+ */
+
+/*
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ *
+ * Should you need to contact me, the author, you can do so by e-mail send
+ * your message to Nestor Lopez Casado <xnlopez at gmail com>
+ *
+ */
+
+#define DJ_MAX_PAIRED_DEVICES 6
+#define DJ_MAX_NUMBER_NOTIFICATIONS 8
+
+#define DJREPORT_SHORT_LENGTH 15
+#define DJREPORT_LONG_LENGTH 32
+
+#define REPORT_ID_HIDPP_SHORT 0x10
+#define REPORT_ID_HIDPP_LONG 0x11
+#define REPORT_ID_DJ_SHORT 0x20
+#define REPORT_ID_DJ_LONG 0x21
+
+#define REPORT_TYPE_RFREPORT_FIRST 0x01
+#define REPORT_TYPE_RFREPORT_LAST 0x1F
+
+/* Command Switch to DJ mode */
+#define REPORT_TYPE_CMD_SWITCH 0x80
+#define CMD_SWITCH_PARAM_DEVBITFIELD 0x00
+#define CMD_SWITCH_PARAM_TIMEOUT_SECONDS 0x01
+#define TIMEOUT_NO_KEEPALIVE 0x00
+
+/* Command to Get the list of Paired devices */
+#define REPORT_TYPE_CMD_GET_PAIRED_DEVICES 0x81
+
+/* Device Paired Notification */
+#define REPORT_TYPE_NOTIF_DEVICE_PAIRED 0x41
+#define NOTIF_DEVICE_PAIRED_PARAM_SPFUNCTION 0x00
+#define SPFUNCTION_MORE_NOTIF_EXPECTED 0x01
+#define SPFUNCTION_DEVICE_LIST_EMPTY 0x02
+#define NOTIF_DEVICE_PAIRED_PARAM_EQUAD_ID_LSB 0x01
+#define NOTIF_DEVICE_PAIRED_PARAM_EQUAD_ID_MSB 0x02
+#define NOTIF_DEVICE_PAIRED_RF_REPORT_TYPE_LSB 0x03
+#define NOTIF_DEVICE_PAIRED_RF_REPORT_TYPE_MSB 0x06
+
+/* Device Un-Paired Notification */
+#define REPORT_TYPE_NOTIF_DEVICE_UNPAIRED 0x40
+
+
+/* Connection Status Notification */
+#define REPORT_TYPE_NOTIF_CONNECTION_STATUS 0x42
+#define NOTIF_CONNECTION_STATUS_PARAM_STATUS 0x00
+#define STATUS_LINKLOSS 0x01
+
+/* Error Notification */
+#define REPORT_TYPE_NOTIF_ERROR 0x7F
+#define NOTIF_ERROR_PARAM_ETYPE 0x00
+#define ETYPE_KEEPALIVE_TIMEOUT 0x01
+
+/* supported DJ HID && RF report types */
+#define REPORT_TYPE_KEYBOARD 0x01
+#define REPORT_TYPE_MOUSE 0x02
+#define REPORT_TYPE_CONSUMER_CONTROL 0x03
+#define REPORT_TYPE_SYSTEM_CONTROL 0x04
+#define REPORT_TYPE_MEDIA_CENTER 0x08
+#define REPORT_TYPE_LEDS 0x0E
+
+/* RF Report types bitfield */
+#define STD_KEYBOARD 0x00000002
+#define STD_MOUSE 0x00000004
+#define MULTIMEDIA 0x00000008
+#define POWER_KEYS 0x00000010
+#define MEDIA_CENTER 0x00000100
+#define KBD_LEDS 0x00004000
+
+struct dj_report {
+ u8 report_id;
+ u8 device_index;
+ u8 report_type;
+ u8 report_params[DJREPORT_SHORT_LENGTH - 3];
+};
+
+struct dj_receiver_dev {
+ struct hid_device *hdev;
+ struct dj_device *paired_dj_devices[DJ_MAX_PAIRED_DEVICES];
+ struct work_struct delayed_work;
+ struct dj_report notif_fifo[DJ_MAX_NUMBER_NOTIFICATIONS];
+ u32 notif_fifo_start;
+ u32 notif_fifo_end;
+ u32 notif_fifo_full;
+ spinlock_t lock;
+};
+
+struct dj_device {
+ struct hid_device *hdev;
+ u32 reports_supported;
+ u32 device_index;
+};
+
+#endif
--
1.7.5.3

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majo...@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/

Dmitry Torokhov

unread,
Aug 12, 2011, 2:00:02 AM8/12/11
to
Hi Nestor,

On Thu, Aug 11, 2011 at 05:22:17PM +0200, Nestor Lopez Casado wrote:
> With this driver, all the devices paired to a single Unifying
> receiver are exposed to user processes in separated /input/dev
> nodes.
>

A few random comments.

> Keyboards with different layouts can be treated differently,
> Multiplayer games on single PC (like home theater PC) can
> differentiate input coming from different kbds paired to the
> same receiver.
>
> Up to now, when Logitech Unifying receivers are connected to a
> Linux based system, a single keyboard and a single mouse are
> presented to the HID Layer, even if the Unifying receiver can
> pair up to six compatible devices. The Unifying receiver by default
> multiplexes all incoming events (from multiple keyboards/mice)
> into these two.
>
> Signed-off-by: Nestor Lopez Casado <nlope...@logitech.com>
> ---
>
> Hi everyone,
>
> I took a second look at your comments with the help of Benjamin
> Tissoires, who is by the way working with us for a few weeks here
> at Logitech.
>
> We finally agreed to remove BUS_DJ, as you suggested, and we are now
> using BUS_VIRTUAL. We can not use BUS_USB because that would assign
> the hid-logitech-dj created "virtual" devices back to the hid-logitech-dj
> module while they should be treated by hid-core.

You do not need BUS_VIRTUAL; you can have logi_dj_probe() detect devices
that you created (looking at the parent for example) and refuse them
with -ENODEV. Then next driver will have chance to bind I believe.

>
> Concerning the conditional compilation on_id hid_have_special_driver[],
> we also agreed to remove them. This implies that if the driver is not included
> by a certain distribution, then the receiver and all of the paired devices
> will not work at all. Note that today, even if there is no driver installed
> this devices work in a basic mode. For this reason we added a 'default m' in
> the Kconfig
>
> Overall, the totality of your suggestions have been addressed.
>
> Cheers,
> Nestor
>

> @@ -450,6 +452,7 @@
> #define USB_DEVICE_ID_DINOVO_MINI 0xc71f
> #define USB_DEVICE_ID_LOGITECH_MOMO_WHEEL2 0xca03
>
> +

This is a stray change.

> +#define NUMBER_OF_HID_REPORTS 32
> +static const u8 hid_reportid_size_map[NUMBER_OF_HID_REPORTS] = {
> + 0, /* Not used */
> + 8, /* Standard keyboard */
> + 8, /* Standard mouse */
> + 5, /* Consumer control */
> + 2, /* System control */
> + 0, /* Not used */
> + 0, /* Not used */
> + 0, /* Not used */
> + 2, /* Media Center */

You could use a bit more compact foprmat:

[1] = 8,
[2] = 8,
[3] = 5,
[4] = 2,
[8] = 2,
};

Here and elsewhere in logging:

dev_err(&djrcv_dev->hdev->dev,
"%s: can't destroy a NULL device\n:, __func__);

so that you do not need to adjust messages if you rename functions.

Why?

> + strlcpy(dj_hiddev->name, "Logitech Unifying Device. ", sizeof(dj_hiddev->name));
> + snprintf(tmpstr, sizeof(tmpstr), "Wireless PID:%02x%02x",
> + dj_report->report_params[NOTIF_DEVICE_PAIRED_PARAM_EQUAD_ID_MSB],
> + dj_report->report_params[NOTIF_DEVICE_PAIRED_PARAM_EQUAD_ID_LSB]);
> + strlcat(dj_hiddev->name, tmpstr, sizeof(dj_hiddev->name));

Why do you need tmpstr? Why not snprintf() directly into
dj_hiddev->name?


> +
> + usb_make_path(usbdev, dj_hiddev->phys, sizeof(dj_hiddev->phys));
> + snprintf(tmpstr, sizeof(tmpstr), ":%d", dj_report->device_index);
> + strlcat(dj_hiddev->phys, tmpstr, sizeof(dj_hiddev->phys));
> +
> + dj_dev = kzalloc(sizeof(struct dj_device), GFP_KERNEL);
> +
> + if (IS_ERR(dj_dev)) {

kzalloc does not return ERR_PTR-encoded errors, it returns address or
NULL.

> + dev_err(&djrcv_dev->hdev->dev,
> + "logi_dj_recv_add_djhid_device:failed allocating dj_device\n");
> + goto dj_device_allocate_fail;
> + }
> +
> + dj_dev->reports_supported = le32_to_cpu(
> + dj_report->
> + report_params[NOTIF_DEVICE_PAIRED_RF_REPORT_TYPE_LSB]);

This formatting is ugly. Maybe shorten define a bit?

> + dj_dev->hdev = dj_hiddev;
> + dj_dev->device_index = dj_report->device_index;
> + dj_hiddev->driver_data = dj_dev;
> +
> + if (hid_add_device(dj_hiddev)) {
> + dev_err(&djrcv_dev->hdev->dev,
> + "logi_dj_recv_add_djhid_device:failed adding dj_device\n");
> + goto hid_add_device_fail;
> + }
> +
> + spin_lock_irqsave(&djrcv_dev->lock, flags);
> + djrcv_dev->paired_dj_devices[dj_report->device_index] = dj_dev;
> + spin_unlock_irqrestore(&djrcv_dev->lock, flags);

Why is this lock needed here?

How about a bit simplier:

if (djrcv_dev->notif_fifo_full)
return -1;

djrcv_dev->notif_fifo[djrcv_dev->notif_fifo_end++] = *dj_report;
djrcv_dev->notif_fifo_end &= DJ_MAX_NUMBER_NOTIFICATIONS - 1;

if (djrcv_dev->notif_fifo_end == djrcv_dev->notif_fifo_start)
djrcv_dev->notif_fifo_full = true;

return 0;

> + return 0;
> + }
> + return -1;
> +}
> +
> +static int notif_fifo_pop(struct dj_receiver_dev *djrcv_dev,
> + struct dj_report *dj_report)
> +{
> + /* We are called from atomic context (tasklet && djrcv->lock held) */
> +
> + if ((djrcv_dev->notif_fifo_start != djrcv_dev->notif_fifo_end) ||
> + (djrcv_dev->notif_fifo_full == 1)) {
> + memcpy(dj_report,
> + &djrcv_dev->notif_fifo[djrcv_dev->notif_fifo_start &
> + (DJ_MAX_NUMBER_NOTIFICATIONS - 1)],
> + sizeof(struct dj_report));
> + djrcv_dev->notif_fifo_start++;
> + djrcv_dev->notif_fifo_full = 0;
> + return (djrcv_dev->notif_fifo_start !=
> + djrcv_dev->notif_fifo_end) ? 1 : 0;

Needs the same degunking as above. Or maybe use kfifo.

Hmm, instead of notif_fifo_pop() returning variety of return codes why
don't you ahve a separate function that tells you if your buffer is
empty or not. Or, again, use kfifo.

> + spin_unlock_irqrestore(&djrcv_dev->lock, flags);
> +
> + if (dj_report.report_type == REPORT_TYPE_NOTIF_DEVICE_PAIRED)
> + logi_dj_recv_add_djhid_device(djrcv_dev, &dj_report);
> + else if (dj_report.report_type == REPORT_TYPE_NOTIF_DEVICE_UNPAIRED)
> + logi_dj_recv_destroy_djhid_device(djrcv_dev, &dj_report);
> + else
> + dbg_hid("delayedwork_callback: unexpected report type\n");

switch() is a nice construct.

Why do you zero buffer here and then do unrelated work? Move it down.

> +
> + if (djrcv_dev->paired_dj_devices[dj_report->device_index] == NULL) {
> + dbg_hid("djrcv_dev->paired_dj_devices[dj_report->device_index]"
> + " is NULL, index %d\n", dj_report->device_index);
> + return;
> + }
> +
> + djdev = djrcv_dev->paired_dj_devices[dj_report->device_index];

if (!djdev) {
...

No "naked" returns at the end please.

Why le16_to_cpu()? You are not getting data from the wire and it will be
wrong on BE arches.

Hmm, this will lead to DMAin on-stack data... or do we copy it again in
usbhid_submit_report()?

> +
> + usbhid_submit_report(dj_rcv_hiddev, report, USB_DIR_OUT);
> +
> + return 0;
> +
> +}
> +
> +static int logi_dj_ll_start(struct hid_device *hid)
> +{
> + dbg_hid("logi_dj_ll_start\n");
> + return 0;
> +}
> +
> +static void logi_dj_ll_stop(struct hid_device *hid)
> +{
> + dbg_hid("logi_dj_ll_stop\n");
> +}
> +
> +static int logi_dj_ll_power(struct hid_device *hid, int lvl)
> +{
> +
> + dbg_hid("logi_dj_ll_power\n");
> + return 0;
> +}

power() is optional method, you can drop stub implementation.

> +
> +
> +static struct hid_ll_driver logi_dj_ll_driver = {
> + .parse = logi_dj_ll_parse,
> + .start = logi_dj_ll_start,
> + .stop = logi_dj_ll_stop,
> + .open = logi_dj_ll_open,
> + .close = logi_dj_ll_close,
> + .power = logi_dj_ll_power,
> + .hidinput_input_event = logi_dj_ll_input_event,
> +};
> +
> +
> +static int logi_dj_raw_event(struct hid_device *hdev,
> + struct hid_report *report, u8 * data,
> + int size)
> +{
> + struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
> + struct dj_report *dj_report = (struct dj_report *) data;
> + unsigned long flags;
> + int report_processed = 0;

bool?


switch (dj_report->report_type) {
...
}

report_processed = true;

> + }
> + spin_unlock_irqrestore(&djrcv_dev->lock, flags);
> +
> +

One of blank lines is extra.

> + return report_processed;
> +}
> +
> +static int logi_dj_probe(struct hid_device *hdev,
> + const struct hid_device_id *id)
> +{
> + struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
> + struct dj_receiver_dev *djrcv_dev;
> + int retval = 0;

Do not initialize to 0 - you risk missing setting proper value in one of
error handling branches and compiler won't be able to warn you.

> +
> + dbg_hid("logi_dj_probe called for ifnum %d\n",
> + intf->cur_altsetting->desc.bInterfaceNumber);
> +
> + /* Ignore interfaces 0 and 1, they will not carry any data, dont create
> + * any hid_device for them */
> + if (intf->cur_altsetting->desc.bInterfaceNumber !=
> + LOGITECH_DJ_INTERFACE_NUMBER) {
> + dbg_hid("logi_dj_probe: ignoring ifnum %d\n",
> + intf->cur_altsetting->desc.bInterfaceNumber);
> + return -ENODEV;
> + }
> +
> + /* Treat interface 2 */
> +
> + djrcv_dev = kzalloc(sizeof(struct dj_receiver_dev), GFP_KERNEL);
> + if (IS_ERR(djrcv_dev)) {

if (!djrcv_dev) {

I'd expect you kill paired devices here, after stopping the receiver.

> + /* Free the dj_recv_dev struct */
> + kfree(hid_get_drvdata(hdev));

Why not kfree(djrcv_dev)? You would not need the comment either.

No need to initialize.

Please rename to "work"... "delayed_work" usually means that schedule is
delayed by so many jiffies.

> + struct dj_report notif_fifo[DJ_MAX_NUMBER_NOTIFICATIONS];
> + u32 notif_fifo_start;
> + u32 notif_fifo_end;
> + u32 notif_fifo_full;

Why u32? Why not bool?

> + spinlock_t lock;
> +};
> +
> +struct dj_device {
> + struct hid_device *hdev;
> + u32 reports_supported;
> + u32 device_index;


You expect to have 4M devices and reports?

Also, why do you need .h file? Nothing from it seems to be used anywhere
but your driver.

Thanks.

--
Dmitry

Jiri Kosina

unread,
Aug 16, 2011, 5:50:01 AM8/16/11
to
On Sat, 13 Aug 2011, Benjamin Tissoires wrote:

> First of all, thank you for this detailed review. Nestor and I agree
> yesterday on the fact that I'll submit the next version of this patch.
> He'll be busy for 2 weeks, and I need to work with this driver for other
> devices we have at Logitech. I'll make the straight forward changes, and
> when it will require more expertise on the receiver, I'll ask to Nestor.

If you could send first the patch with Dmitry's feedback (thanks Dmitry!)
incorporated, and support for other devices as a separate patch on top of
that, that'd be great.

Thanks,

--
Jiri Kosina
SUSE Labs

Benjamin Tissoires

unread,
Aug 16, 2011, 6:00:01 AM8/16/11
to
Hi Jiri,

On Tue, Aug 16, 2011 at 11:46, Jiri Kosina <jko...@suse.cz> wrote:
> On Sat, 13 Aug 2011, Benjamin Tissoires wrote:
>
>> First of all, thank you for this detailed review. Nestor and I agree
>> yesterday on the fact that I'll submit the next version of this patch.
>> He'll be busy for 2 weeks, and I need to work with this driver for other
>> devices we have at Logitech. I'll make the straight forward changes, and
>> when it will require more expertise on the receiver, I'll ask to Nestor.
>
> If you could send first the patch with Dmitry's feedback (thanks Dmitry!)
> incorporated, and support for other devices as a separate patch on top of
> that, that'd be great.
>

That's exactly what we want to do. The corrected patch will come
first, and we'll add features later: I'm currently writing them now
;-)

Cheers,
Benjamin

PS: sorry for the people in the list, my previous reply has not been
accepted by the LKML with my Logitech address.


> Thanks,
>
> --
> Jiri Kosina
> SUSE Labs
> --

> To unsubscribe from this list: send the line "unsubscribe linux-input" in

Benjamin Tissoires

unread,
Aug 25, 2011, 5:00:03 AM8/25/11
to
From: Nestor Lopez Casado <nlope...@logitech.com>

With this driver, all the devices paired to a single Unifying
receiver are exposed to user processes in separated /input/dev
nodes.

Keyboards with different layouts can be treated differently,


Multiplayer games on single PC (like home theater PC) can
differentiate input coming from different kbds paired to the
same receiver.

Up to now, when Logitech Unifying receivers are connected to a
Linux based system, a single keyboard and a single mouse are
presented to the HID Layer, even if the Unifying receiver can
pair up to six compatible devices. The Unifying receiver by default
multiplexes all incoming events (from multiple keyboards/mice)
into these two.

Signed-off-by: Nestor Lopez Casado <nlope...@logitech.com>

Signed-off-by: Benjamin Tissoires <btiss...@logitech.com>
---

Hi guys,

this is the third version of the unifying receiver. I made the changes requested
by Dmitry:
- switch to kfifo
- introduce __func__ in all debugs/errors messages
- in logi_dj_ll_input_event, do not use the report of the device, but a copy
(is it the right way to do it to avoid the DMA problem?)
- switch to BUS_USB for the created devices
- small other cosmetic changes

One side note:
We really thing using BUS_USB is not the right way to do it when we will have
more drivers:
by keeping it, we will have no choice but to provide the VIP/PID of the
receiver to the created devices. We then will have to handle a manual parsing
of the wireless id to know which driver to use. This will result in having
all the Logitech's drivers loaded when someone plug an unifying receiver.
if we switch to a new BUS_DJ, we could then rely on the automatic user-space
loading of the drivers to load only the right drivers.

BTW, as long as Logitech does not provide those drivers (I hope they will come
out soon), there is no point using BUS_DJ. So it's fine not using it right now.

Thanks,
Benjamin

drivers/hid/Kconfig | 9 +
drivers/hid/Makefile | 1 +
drivers/hid/hid-core.c | 2 +

drivers/hid/hid-ids.h | 2 +
drivers/hid/hid-logitech-dj.c | 893 +++++++++++++++++++++++++++++++++++++++++
drivers/hid/hid-logitech-dj.h | 123 ++++++
6 files changed, 1030 insertions(+), 0 deletions(-)


create mode 100644 drivers/hid/hid-logitech-dj.c
create mode 100644 drivers/hid/hid-logitech-dj.h

diff --git a/drivers/hid/Kconfig b/drivers/hid/Kconfig
index 36ca465..0eb7f04 100644
--- a/drivers/hid/Kconfig
+++ b/drivers/hid/Kconfig
@@ -231,6 +231,15 @@ config HID_LOGITECH


---help---
Support for Logitech devices that are not fully compliant with HID standard.

+config HID_LOGITECH_DJ
+ tristate "Logitech Unifying receivers full support"
+ depends on HID_LOGITECH
+ default m
+ ---help---
+ Say Y if you want support for Logitech Unifying receivers and devices.
+ Unifying receivers are capable of pairing up to 6 Logitech compliant
+ devices to the same receiver.
+
config LOGITECH_FF
bool "Logitech force feedback support"
depends on HID_LOGITECH
diff --git a/drivers/hid/Makefile b/drivers/hid/Makefile

index a24cee4..3aba5e7 100644
--- a/drivers/hid/Makefile
+++ b/drivers/hid/Makefile
@@ -44,6 +44,7 @@ obj-$(CONFIG_HID_KEYTOUCH) += hid-keytouch.o


obj-$(CONFIG_HID_KYE) += hid-kye.o
obj-$(CONFIG_HID_LCPOWER) += hid-lcpower.o
obj-$(CONFIG_HID_LOGITECH) += hid-logitech.o
+obj-$(CONFIG_HID_LOGITECH_DJ) += hid-logitech-dj.o
obj-$(CONFIG_HID_MAGICMOUSE) += hid-magicmouse.o
obj-$(CONFIG_HID_MICROSOFT) += hid-microsoft.o
obj-$(CONFIG_HID_MONTEREY) += hid-monterey.o
diff --git a/drivers/hid/hid-core.c b/drivers/hid/hid-core.c

index b0e618b..ebe4fdd 100644
--- a/drivers/hid/hid-core.c
+++ b/drivers/hid/hid-core.c
@@ -1423,6 +1423,8 @@ static const struct hid_device_id hid_have_special_driver[] = {


{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_DFP_WHEEL) },
{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_G25_WHEEL) },
{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_G27_WHEEL) },
+ { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_UNIFYING_RECEIVER) },
+ { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_UNIFYING_RECEIVER_2) },
{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_WII_WHEEL) },
{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_RUMBLEPAD2) },
{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_SPACETRAVELLER) },
diff --git a/drivers/hid/hid-ids.h b/drivers/hid/hid-ids.h

index a756ee6..c42764b 100644
--- a/drivers/hid/hid-ids.h
+++ b/drivers/hid/hid-ids.h
@@ -440,6 +440,8 @@


#define USB_DEVICE_ID_S510_RECEIVER_2 0xc517
#define USB_DEVICE_ID_LOGITECH_CORDLESS_DESKTOP_LX500 0xc512
#define USB_DEVICE_ID_MX3000_RECEIVER 0xc513
+#define USB_DEVICE_ID_LOGITECH_UNIFYING_RECEIVER 0xc52b
+#define USB_DEVICE_ID_LOGITECH_UNIFYING_RECEIVER_2 0xc532
#define USB_DEVICE_ID_SPACETRAVELLER 0xc623
#define USB_DEVICE_ID_SPACENAVIGATOR 0xc626
#define USB_DEVICE_ID_DINOVO_DESKTOP 0xc704

diff --git a/drivers/hid/hid-logitech-dj.c b/drivers/hid/hid-logitech-dj.c
new file mode 100644
index 0000000..896328d
--- /dev/null
+++ b/drivers/hid/hid-logitech-dj.c
@@ -0,0 +1,893 @@

+#define NUMBER_OF_HID_REPORTS 32
+static const u8 hid_reportid_size_map[NUMBER_OF_HID_REPORTS] = {

+ [1] = 8, /* Standard keyboard */
+ [2] = 8, /* Standard mouse */
+ [3] = 5, /* Consumer control */
+ [4] = 2, /* System control */
+ [8] = 2, /* Media Center */
+};

+ dev_err(&djrcv_dev->hdev->dev, "%s: can't destroy a NULL device\n",
+ __func__);
+ }
+}
+
+static void logi_dj_recv_add_djhid_device(struct dj_receiver_dev *djrcv_dev,


+ struct dj_report *dj_report)
+{
+ /* Called in delayed work context */

+ struct hid_device *djrcv_hdev = djrcv_dev->hdev;
+ struct usb_interface *intf = to_usb_interface(djrcv_hdev->dev.parent);


+ struct usb_device *usbdev = interface_to_usbdev(intf);
+ struct hid_device *dj_hiddev;
+ struct dj_device *dj_dev;

+ unsigned char tmpstr[20];
+

+ if (dj_report->report_params[DEVICE_PAIRED_PARAM_SPFUNCTION] &
+ SPFUNCTION_DEVICE_LIST_EMPTY) {
+ dbg_hid("%s: device list is empty\n", __func__);


+ return;
+ }
+
+ dj_hiddev = hid_allocate_device();
+ if (IS_ERR(dj_hiddev)) {

+ dev_err(&djrcv_hdev->dev, "%s: hid_allocate_device failed\n",
+ __func__);


+ return;
+ }
+
+ dj_hiddev->ll_driver = &logi_dj_ll_driver;
+ dj_hiddev->hid_output_raw_report = logi_dj_output_hidraw_report;
+

+ dj_hiddev->dev.parent = &djrcv_hdev->dev;
+ dj_hiddev->bus = BUS_USB;


+ dj_hiddev->vendor = le16_to_cpu(usbdev->descriptor.idVendor);
+ dj_hiddev->product = le16_to_cpu(usbdev->descriptor.idProduct);

+ snprintf(dj_hiddev->name, sizeof(dj_hiddev->name),
+ "Logitech Unifying Device. Wireless PID:%02x%02x",
+ dj_report->report_params[DEVICE_PAIRED_PARAM_EQUAD_ID_MSB],
+ dj_report->report_params[DEVICE_PAIRED_PARAM_EQUAD_ID_LSB]);


+
+ usb_make_path(usbdev, dj_hiddev->phys, sizeof(dj_hiddev->phys));
+ snprintf(tmpstr, sizeof(tmpstr), ":%d", dj_report->device_index);
+ strlcat(dj_hiddev->phys, tmpstr, sizeof(dj_hiddev->phys));
+
+ dj_dev = kzalloc(sizeof(struct dj_device), GFP_KERNEL);
+

+ if (!dj_dev) {
+ dev_err(&djrcv_hdev->dev, "%s: failed allocating dj_device\n",
+ __func__);


+ goto dj_device_allocate_fail;
+ }
+
+ dj_dev->reports_supported = le32_to_cpu(

+ dj_report->report_params[DEVICE_PAIRED_RF_REPORT_TYPE]);


+ dj_dev->hdev = dj_hiddev;

+ dj_dev->dj_receiver_dev = djrcv_dev;


+ dj_dev->device_index = dj_report->device_index;
+ dj_hiddev->driver_data = dj_dev;
+

+ djrcv_dev->paired_dj_devices[dj_report->device_index] = dj_dev;
+

+ if (hid_add_device(dj_hiddev)) {
+ dev_err(&djrcv_hdev->dev, "%s: failed adding dj_device\n",
+ __func__);


+ goto hid_add_device_fail;
+ }
+

+ return;
+
+hid_add_device_fail:


+ djrcv_dev->paired_dj_devices[dj_report->device_index] = NULL;

+ kfree(dj_dev);
+dj_device_allocate_fail:
+ hid_destroy_device(dj_hiddev);
+}
+

+static void delayedwork_callback(struct work_struct *work)
+{
+ struct dj_receiver_dev *djrcv_dev =

+ container_of(work, struct dj_receiver_dev, work);


+
+ struct dj_report dj_report;
+ unsigned long flags;

+ int count;
+
+ dbg_hid("%s\n", __func__);


+
+ spin_lock_irqsave(&djrcv_dev->lock, flags);
+

+ count = kfifo_out(&djrcv_dev->notif_fifo, &dj_report,
+ sizeof(struct dj_report));
+
+ if (count != sizeof(struct dj_report)) {
+ dev_err(&djrcv_dev->hdev->dev, "%s: workitem triggered without "
+ "notifications available\n", __func__);
+ spin_unlock_irqrestore(&djrcv_dev->lock, flags);
+ return;
+ }
+
+ if (!kfifo_is_empty(&djrcv_dev->notif_fifo)) {
+ if (schedule_work(&djrcv_dev->work) == 0) {
+ dbg_hid("%s: did not schedule the work item, was "
+ "already queued\n", __func__);
+ }
+ }


+
+ spin_unlock_irqrestore(&djrcv_dev->lock, flags);
+

+ switch (dj_report.report_type) {
+ case REPORT_TYPE_NOTIF_DEVICE_PAIRED:
+ logi_dj_recv_add_djhid_device(djrcv_dev, &dj_report);
+ break;
+ case REPORT_TYPE_NOTIF_DEVICE_UNPAIRED:
+ logi_dj_recv_destroy_djhid_device(djrcv_dev, &dj_report);
+ break;
+ default:
+ dbg_hid("%s: unexpected report type\n", __func__);
+ }
+}
+
+static void logi_dj_recv_queue_notification(struct dj_receiver_dev *djrcv_dev,


+ struct dj_report *dj_report)
+{
+ /* We are called from atomic context (tasklet && djrcv->lock held) */
+

+ kfifo_in(&djrcv_dev->notif_fifo, dj_report, sizeof(struct dj_report));
+
+ if (schedule_work(&djrcv_dev->work) == 0) {
+ dbg_hid("%s: did not schedule the work item, was already "
+ "queued\n", __func__);
+ }
+}
+


+static void logi_dj_recv_forward_null_report(struct dj_receiver_dev *djrcv_dev,
+ struct dj_report *dj_report)
+{
+ /* We are called from atomic context (tasklet && djrcv->lock held) */
+ unsigned int i;
+ u8 reportbuffer[MAX_REPORT_SIZE];
+ struct dj_device *djdev;
+

+ djdev = djrcv_dev->paired_dj_devices[dj_report->device_index];

+
+ if (!djdev) {


+ dbg_hid("djrcv_dev->paired_dj_devices[dj_report->device_index]"
+ " is NULL, index %d\n", dj_report->device_index);
+ return;
+ }
+

+ memset(reportbuffer, 0, sizeof(reportbuffer));


+
+ for (i = 0; i < NUMBER_OF_HID_REPORTS; i++) {
+ if (djdev->reports_supported & (1 << i)) {
+ reportbuffer[0] = i;
+ if (hid_input_report(djdev->hdev,
+ HID_INPUT_REPORT,
+ reportbuffer,
+ hid_reportid_size_map[i], 1)) {
+ dbg_hid("hid_input_report error sending null "

+ "report\n");


+ }
+ }
+ }
+}
+
+static void logi_dj_recv_forward_report(struct dj_receiver_dev *djrcv_dev,
+ struct dj_report *dj_report)
+{
+ /* We are called from atomic context (tasklet && djrcv->lock held) */

+ struct dj_device *dj_device;
+
+ dj_device = djrcv_dev->paired_dj_devices[dj_report->device_index];
+
+ if (dj_device == NULL) {


+ dbg_hid("djrcv_dev->paired_dj_devices[dj_report->device_index]"
+ " is NULL, index %d\n", dj_report->device_index);
+ return;
+ }
+

+ if ((dj_report->report_type > ARRAY_SIZE(hid_reportid_size_map) - 1) ||


+ (hid_reportid_size_map[dj_report->report_type] == 0)) {
+ dbg_hid("invalid report type:%x\n", dj_report->report_type);
+ return;
+ }
+

+ if (hid_input_report(dj_device->hdev,


+ HID_INPUT_REPORT, &dj_report->report_type,
+ hid_reportid_size_map[dj_report->report_type], 1)) {
+ dbg_hid("hid_input_report error\n");
+ }
+}
+

+
+static int logi_dj_recv_send_report(struct dj_receiver_dev *djrcv_dev,
+ struct dj_report *dj_report)
+{
+ struct hid_device *hdev = djrcv_dev->hdev;
+ int sent_bytes;
+
+ if (!hdev->hid_output_raw_report) {

+ dev_err(&hdev->dev, "%s:"
+ "hid_output_raw_report is null\n", __func__);


+ return -ENODEV;
+ }
+
+ sent_bytes = hdev->hid_output_raw_report(hdev, (u8 *) dj_report,
+ sizeof(struct dj_report),
+ HID_OUTPUT_REPORT);
+
+ return (sent_bytes < 0) ? sent_bytes : 0;
+}
+

+static int logi_dj_recv_query_paired_devices(struct dj_receiver_dev *djrcv_dev)


+{
+ struct dj_report dj_report;
+
+ memset(&dj_report, 0, sizeof(dj_report));
+ dj_report.report_id = REPORT_ID_DJ_SHORT;
+ dj_report.device_index = 0xFF;
+ dj_report.report_type = REPORT_TYPE_CMD_GET_PAIRED_DEVICES;
+ return logi_dj_recv_send_report(djrcv_dev, &dj_report);
+}
+

+static int logi_dj_recv_switch_to_dj_mode(struct dj_receiver_dev *djrcv_dev,
+ unsigned timeout)


+{
+ struct dj_report dj_report;
+
+ memset(&dj_report, 0, sizeof(dj_report));
+ dj_report.report_id = REPORT_ID_DJ_SHORT;
+ dj_report.device_index = 0xFF;
+ dj_report.report_type = REPORT_TYPE_CMD_SWITCH;
+ dj_report.report_params[CMD_SWITCH_PARAM_DEVBITFIELD] = 0x1F;
+ dj_report.report_params[CMD_SWITCH_PARAM_TIMEOUT_SECONDS] = (u8)timeout;
+ return logi_dj_recv_send_report(djrcv_dev, &dj_report);
+}
+
+
+static int logi_dj_ll_open(struct hid_device *hid)
+{

+ dbg_hid("%s:%s\n", __func__, hid->phys);


+ return 0;
+
+}
+
+static void logi_dj_ll_close(struct hid_device *hid)
+{

+ dbg_hid("%s:%s\n", __func__, hid->phys);


+}
+
+static int logi_dj_output_hidraw_report(struct hid_device *hid, u8 * buf,
+ size_t count,
+ unsigned char report_type)
+{
+ /* Called by hid raw to send data */

+ dbg_hid("%s\n", __func__);


+
+ return 0;
+}
+
+static int logi_dj_ll_parse(struct hid_device *hid)
+{
+ struct dj_device *djdev = hid->driver_data;

+ int retval;
+
+ dbg_hid("%s\n", __func__);
+
+ djdev->hdev->version = 0x0111;


+ djdev->hdev->country = 0x00;
+
+ if (djdev->reports_supported & STD_KEYBOARD) {

+ dbg_hid("%s: sending a kbd descriptor, reports_supported: %x\n",
+ __func__, djdev->reports_supported);


+ retval = hid_parse_report(hid,
+ (u8 *) kbd_descriptor,
+ sizeof(kbd_descriptor));
+ if (retval) {

+ dbg_hid("%s: sending a kbd descriptor, hid_parse failed"
+ " error: %d\n", __func__, retval);


+ return retval;
+ }
+ }
+
+ if (djdev->reports_supported & STD_MOUSE) {

+ dbg_hid("%s: sending a mouse descriptor, reports_supported: "
+ "%x\n", __func__, djdev->reports_supported);


+ retval = hid_parse_report(hid,
+ (u8 *) mse_descriptor,
+ sizeof(mse_descriptor));
+ if (retval) {

+ dbg_hid("%s: sending a mouse descriptor, hid_parse "
+ "failed error: %d\n", __func__, retval);


+ return retval;
+ }
+ }
+
+ if (djdev->reports_supported & MULTIMEDIA) {

+ dbg_hid("%s: sending a multimedia report descriptor: %x\n",
+ __func__, djdev->reports_supported);


+ retval = hid_parse_report(hid,
+ (u8 *) consumer_descriptor,
+ sizeof(consumer_descriptor));
+ if (retval) {

+ dbg_hid("%s: sending a consumer_descriptor, hid_parse "
+ "failed error: %d\n", __func__, retval);


+ return retval;
+ }
+ }
+
+ if (djdev->reports_supported & POWER_KEYS) {

+ dbg_hid("%s: sending a power keys report descriptor: %x\n",
+ __func__, djdev->reports_supported);


+ retval = hid_parse_report(hid,
+ (u8 *) syscontrol_descriptor,
+ sizeof(syscontrol_descriptor));
+ if (retval) {

+ dbg_hid("%s: sending a syscontrol_descriptor, "
+ "hid_parse failed error: %d\n",
+ __func__, retval);


+ return retval;
+ }
+ }
+
+ if (djdev->reports_supported & MEDIA_CENTER) {

+ dbg_hid("%s: sending a media center report descriptor: %x\n",
+ __func__, djdev->reports_supported);


+ retval = hid_parse_report(hid,
+ (u8 *) media_descriptor,
+ sizeof(media_descriptor));
+ if (retval) {

+ dbg_hid("%s: sending a media_descriptor, hid_parse "
+ "failed error: %d\n", __func__, retval);


+ return retval;
+ }
+ }
+
+ if (djdev->reports_supported & KBD_LEDS) {

+ dbg_hid("%s: need to send kbd leds report descriptor: %x\n",
+ __func__, djdev->reports_supported);
+ }


+
+ return 0;
+}
+

+static int logi_dj_ll_input_event(struct input_dev *dev, unsigned int type,
+ unsigned int code, int value)
+{
+ /* Sent by the input layer to handle leds and Force Feedback */
+ struct hid_device *dj_hiddev = input_get_drvdata(dev);
+ struct dj_device *dj_dev = dj_hiddev->driver_data;
+
+ struct dj_receiver_dev *djrcv_dev =
+ dev_get_drvdata(dj_hiddev->dev.parent);
+ struct hid_device *dj_rcv_hiddev = djrcv_dev->hdev;

+ struct hid_report_enum *output_report_enum;


+
+ struct hid_field *field;

+ struct hid_report report;


+ unsigned char data[8];
+ int offset;
+

+ dbg_hid("%s: %s, type:%d | code:%d | value:%d\n",
+ __func__, dev->phys, type, code, value);


+
+ if (type != EV_LED)
+ return -1;
+
+ offset = hidinput_find_field(dj_hiddev, type, code, &field);
+
+ if (offset == -1) {
+ dev_warn(&dev->dev, "event field not found\n");
+ return -1;
+ }
+ hid_set_field(field, offset, value);
+ hid_output_report(field->report, &data[0]);
+

+ output_report_enum = &dj_rcv_hiddev->report_enum[HID_OUTPUT_REPORT];
+ report = *output_report_enum->report_id_hash[REPORT_ID_DJ_SHORT];
+ hid_set_field(report.field[0], 0, dj_dev->device_index);
+ hid_set_field(report.field[0], 1, REPORT_TYPE_LEDS);
+ hid_set_field(report.field[0], 2, data[1]);
+
+ usbhid_submit_report(dj_rcv_hiddev, &report, USB_DIR_OUT);


+
+ return 0;
+
+}
+
+static int logi_dj_ll_start(struct hid_device *hid)
+{

+ dbg_hid("%s\n", __func__);


+ return 0;
+}
+
+static void logi_dj_ll_stop(struct hid_device *hid)
+{

+ dbg_hid("%s\n", __func__);
+}
+
+


+static struct hid_ll_driver logi_dj_ll_driver = {
+ .parse = logi_dj_ll_parse,
+ .start = logi_dj_ll_start,
+ .stop = logi_dj_ll_stop,
+ .open = logi_dj_ll_open,
+ .close = logi_dj_ll_close,

+ .hidinput_input_event = logi_dj_ll_input_event,
+};
+
+
+static int logi_dj_raw_event(struct hid_device *hdev,
+ struct hid_report *report, u8 *data,
+ int size)
+{
+ struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
+ struct dj_report *dj_report = (struct dj_report *) data;
+ unsigned long flags;

+ bool report_processed = false;
+
+ dbg_hid("%s, size:%d\n", __func__, size);


+
+ /* Here we receive all data coming from iface 2, there are 4 cases:
+ *
+ * 1) Data should continue its normal processing i.e. data does not
+ * come from the DJ collection, in which case we do nothing and
+ * return 0, so hid-core can continue normal processing (will forward
+ * to associated hidraw device)
+ *
+ * 2) Data is from DJ collection, and is intended for this driver i. e.
+ * data contains arrival, departure, etc notifications, in which case
+ * we queue them for delayed processing by the work queue. We return 1
+ * to hid-core as no further processing is required from it.
+ *
+ * 3) Data is from DJ collection, and informs a connection change,
+ * if the change means rf link loss, then we must send a null report
+ * to the upper layer to discard potentially pressed keys that may be
+ * repeated forever by the input layer. Return 1 to hid-core as no

+ * further processing is required.


+ *
+ * 4) Data is from DJ collection and is an actual input event from
+ * a paired DJ device in which case we forward it to the correct hid
+ * device (via hid_input_report() ) and return 1 so hid-core does not do
+ * anything else with it.
+ */
+
+ spin_lock_irqsave(&djrcv_dev->lock, flags);
+ if (dj_report->report_id == REPORT_ID_DJ_SHORT) {

+ switch (dj_report->report_type) {
+ case REPORT_TYPE_NOTIF_DEVICE_PAIRED:
+ case REPORT_TYPE_NOTIF_DEVICE_UNPAIRED:
+ logi_dj_recv_queue_notification(djrcv_dev, dj_report);
+ break;
+ case REPORT_TYPE_NOTIF_CONNECTION_STATUS:
+ if (dj_report->report_params[CONNECTION_STATUS_PARAM_STATUS] ==


+ STATUS_LINKLOSS) {
+ logi_dj_recv_forward_null_report(djrcv_dev, dj_report);
+ }

+ break;
+ default:
+ logi_dj_recv_forward_report(djrcv_dev, dj_report);
+ }
+ report_processed = true;


+ }
+ spin_unlock_irqrestore(&djrcv_dev->lock, flags);
+

+ return report_processed;
+}
+
+static int logi_dj_probe(struct hid_device *hdev,
+ const struct hid_device_id *id)
+{
+ struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
+ struct dj_receiver_dev *djrcv_dev;

+ int retval;
+
+ if (is_dj_device((struct dj_device *)hdev->driver_data))
+ return -ENODEV;
+
+ dbg_hid("%s called for ifnum %d\n", __func__,


+ intf->cur_altsetting->desc.bInterfaceNumber);
+
+ /* Ignore interfaces 0 and 1, they will not carry any data, dont create
+ * any hid_device for them */
+ if (intf->cur_altsetting->desc.bInterfaceNumber !=
+ LOGITECH_DJ_INTERFACE_NUMBER) {

+ dbg_hid("%s: ignoring ifnum %d\n", __func__,


+ intf->cur_altsetting->desc.bInterfaceNumber);
+ return -ENODEV;
+ }
+
+ /* Treat interface 2 */
+
+ djrcv_dev = kzalloc(sizeof(struct dj_receiver_dev), GFP_KERNEL);

+ if (!djrcv_dev) {
+ dev_err(&hdev->dev,
+ "%s:failed allocating dj_receiver_dev\n", __func__);


+ return -ENOMEM;
+ }
+ djrcv_dev->hdev = hdev;

+ INIT_WORK(&djrcv_dev->work, delayedwork_callback);
+ spin_lock_init(&djrcv_dev->lock);
+ if (kfifo_alloc(&djrcv_dev->notif_fifo,
+ DJ_MAX_NUMBER_NOTIFICATIONS * sizeof(struct dj_report),
+ GFP_KERNEL)) {
+ dev_err(&hdev->dev,
+ "%s:failed allocating notif_fifo\n", __func__);
+ kfree(djrcv_dev);
+ return -ENOMEM;
+ }


+ hid_set_drvdata(hdev, djrcv_dev);
+
+ /* Call to usbhid to fetch the HID descriptors of interface 2 and

+ * subsequently call to the hid/hid-core to parse the fetched
+ * descriptors, this will in turn create the hidraw and hiddev nodes
+ * for interface 2 of the receiver */


+ retval = hid_parse(hdev);
+ if (retval) {
+ dev_err(&hdev->dev,

+ "%s:parse of interface 2 failed\n", __func__);


+ goto hid_parse_fail;
+ }
+
+ /* Starts the usb device and connects to upper interfaces hiddev and

+ * hidraw */


+ retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
+ if (retval) {
+ dev_err(&hdev->dev,

+ "%s:hid_hw_start returned error\n", __func__);


+ goto hid_hw_start_fail;
+ }
+
+ retval = logi_dj_recv_switch_to_dj_mode(djrcv_dev, 0);
+ if (retval < 0) {
+ dev_err(&hdev->dev,

+ "%s:logi_dj_recv_switch_to_dj_mode returned error:%d\n",
+ __func__, retval);


+ goto switch_to_dj_mode_fail;
+ }
+
+ /* This is enabling the polling urb on the IN endpoint */
+ retval = hdev->ll_driver->open(hdev);
+ if (retval < 0) {

+ dev_err(&hdev->dev, "%s:hdev->ll_driver->open returned "
+ "error:%d\n", __func__, retval);


+ goto llopen_failed;
+ }
+
+ retval = logi_dj_recv_query_paired_devices(djrcv_dev);
+ if (retval < 0) {

+ dev_err(&hdev->dev, "%s:logi_dj_recv_query_paired_devices "
+ "error:%d\n", __func__, retval);


+ goto logi_dj_recv_query_paired_devices_failed;
+ }
+
+ return retval;
+
+logi_dj_recv_query_paired_devices_failed:
+ hdev->ll_driver->close(hdev);
+
+llopen_failed:
+switch_to_dj_mode_fail:
+ hid_hw_stop(hdev);
+
+hid_hw_start_fail:
+hid_parse_fail:

+ kfifo_free(&djrcv_dev->notif_fifo);


+ kfree(djrcv_dev);
+ hid_set_drvdata(hdev, NULL);
+ return retval;
+
+}
+
+static void logi_dj_remove(struct hid_device *hdev)
+{
+ struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
+ struct dj_device *dj_dev;
+ int i;
+

+ dbg_hid("%s\n", __func__);
+
+ cancel_work_sync(&djrcv_dev->work);


+
+ hdev->ll_driver->close(hdev);
+ hid_hw_stop(hdev);
+
+ /* I suppose that at this point the only context that can access
+ * the djrecv_data is this thread as the work item is guaranteed to
+ * have finished and no more raw_event callbacks should arrive after
+ * the remove callback was triggered so no locks are put around the
+ * code below */
+ for (i = 0; i < DJ_MAX_PAIRED_DEVICES; i++) {
+ dj_dev = djrcv_dev->paired_dj_devices[i];
+ if (dj_dev != NULL) {
+ hid_destroy_device(dj_dev->hdev);
+ kfree(dj_dev);
+ djrcv_dev->paired_dj_devices[i] = NULL;
+ }
+ }
+

+ kfifo_free(&djrcv_dev->notif_fifo);


+ kfree(djrcv_dev);
+ hid_set_drvdata(hdev, NULL);
+}

+
+static int logi_djdevice_probe(struct hid_device *hdev,


+ const struct hid_device_id *id)
+{

+ int ret;
+ struct dj_device *dj_dev = hdev->driver_data;
+
+ if (!is_dj_device(dj_dev))
+ return -ENODEV;
+
+ ret = hid_parse(hdev);
+ if (!ret)
+ ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
+
+ return ret;


+}
+
+static const struct hid_device_id logi_dj_receivers[] = {
+ {HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,

+ USB_DEVICE_ID_LOGITECH_UNIFYING_RECEIVER)},
+ {HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
+ USB_DEVICE_ID_LOGITECH_UNIFYING_RECEIVER_2)},


+ {}
+};
+
+MODULE_DEVICE_TABLE(hid, logi_dj_receivers);
+
+static struct hid_driver logi_djreceiver_driver = {
+ .name = "logitech-djreceiver",
+ .id_table = logi_dj_receivers,
+ .probe = logi_dj_probe,
+ .remove = logi_dj_remove,
+ .raw_event = logi_dj_raw_event
+};
+

+
+static const struct hid_device_id logi_dj_devices[] = {

+ {HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
+ USB_DEVICE_ID_LOGITECH_UNIFYING_RECEIVER)},
+ {HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
+ USB_DEVICE_ID_LOGITECH_UNIFYING_RECEIVER_2)},


+ {}
+};
+
+static struct hid_driver logi_djdevice_driver = {

+ .name = "logitech-djdevice",
+ .id_table = logi_dj_devices,
+ .probe = logi_djdevice_probe,


+};
+
+
+static int __init logi_dj_init(void)
+{

+ int retval;
+
+ dbg_hid("Logitech-DJ:%s\n", __func__);


+
+ retval = hid_register_driver(&logi_djreceiver_driver);
+ if (retval)
+ return retval;
+
+ retval = hid_register_driver(&logi_djdevice_driver);
+ if (retval)
+ hid_unregister_driver(&logi_djreceiver_driver);
+
+ return retval;
+
+}
+
+static void __exit logi_dj_exit(void)
+{

+ dbg_hid("Logitech-DJ:%s\n", __func__);


+
+ hid_unregister_driver(&logi_djdevice_driver);
+ hid_unregister_driver(&logi_djreceiver_driver);
+
+}
+
+module_init(logi_dj_init);
+module_exit(logi_dj_exit);
+MODULE_LICENSE("GPL");
+MODULE_AUTHOR("Logitech");
+MODULE_AUTHOR("Nestor Lopez Casado");
diff --git a/drivers/hid/hid-logitech-dj.h b/drivers/hid/hid-logitech-dj.h
new file mode 100644

index 0000000..8dcdccd
--- /dev/null
+++ b/drivers/hid/hid-logitech-dj.h
@@ -0,0 +1,123 @@

+#include <linux/kfifo.h>


+
+#define DJ_MAX_PAIRED_DEVICES 6
+#define DJ_MAX_NUMBER_NOTIFICATIONS 8
+
+#define DJREPORT_SHORT_LENGTH 15
+#define DJREPORT_LONG_LENGTH 32
+

+#define REPORT_ID_DJ_SHORT 0x20
+#define REPORT_ID_DJ_LONG 0x21
+
+#define REPORT_TYPE_RFREPORT_FIRST 0x01
+#define REPORT_TYPE_RFREPORT_LAST 0x1F
+
+/* Command Switch to DJ mode */
+#define REPORT_TYPE_CMD_SWITCH 0x80
+#define CMD_SWITCH_PARAM_DEVBITFIELD 0x00
+#define CMD_SWITCH_PARAM_TIMEOUT_SECONDS 0x01
+#define TIMEOUT_NO_KEEPALIVE 0x00
+
+/* Command to Get the list of Paired devices */
+#define REPORT_TYPE_CMD_GET_PAIRED_DEVICES 0x81
+
+/* Device Paired Notification */
+#define REPORT_TYPE_NOTIF_DEVICE_PAIRED 0x41

+#define SPFUNCTION_MORE_NOTIF_EXPECTED 0x01
+#define SPFUNCTION_DEVICE_LIST_EMPTY 0x02

+#define DEVICE_PAIRED_PARAM_SPFUNCTION 0x00
+#define DEVICE_PAIRED_PARAM_EQUAD_ID_LSB 0x01
+#define DEVICE_PAIRED_PARAM_EQUAD_ID_MSB 0x02
+#define DEVICE_PAIRED_RF_REPORT_TYPE 0x03


+
+/* Device Un-Paired Notification */
+#define REPORT_TYPE_NOTIF_DEVICE_UNPAIRED 0x40
+
+
+/* Connection Status Notification */
+#define REPORT_TYPE_NOTIF_CONNECTION_STATUS 0x42

+#define CONNECTION_STATUS_PARAM_STATUS 0x00

+ struct work_struct work;
+ struct kfifo notif_fifo;


+ spinlock_t lock;
+};
+
+struct dj_device {
+ struct hid_device *hdev;

+ struct dj_receiver_dev *dj_receiver_dev;
+ u32 reports_supported;
+ u8 device_index;
+};
+
+/**
+ * is_dj_device - know if the given dj_device is not the receiver.
+ * @dj_dev: the dj device to test
+ *
+ * This macro tests if a struct dj_device pointer is a device created
+ * by the bus enumarator.
+ */
+#define is_dj_device(dj_dev) \
+ (&(dj_dev)->dj_receiver_dev->hdev->dev == (dj_dev)->hdev->dev.parent)
+
+#endif
--
1.7.4.4

Benjamin Tissoires

unread,
Sep 1, 2011, 8:50:02 AM9/1/11
to
Hi guys,

just a little up to this patch, and a more technical question:

On Thu, Aug 25, 2011 at 10:11, Benjamin Tissoires
<benjamin....@gmail.com> wrote:
> From: Nestor Lopez Casado <nlope...@logitech.com>
>
> With this driver, all the devices paired to a single Unifying
> receiver are exposed to user processes in separated /input/dev
> nodes.
>
> Keyboards with different layouts can be treated differently,
> Multiplayer games on single PC (like home theater PC) can
> differentiate input coming from different kbds paired to the
> same receiver.
>
> Up to now, when Logitech Unifying receivers are connected to a
> Linux based system, a single keyboard and a single mouse are
> presented to the HID Layer, even if the Unifying receiver can
> pair up to six compatible devices. The Unifying receiver by default
> multiplexes all incoming events (from multiple keyboards/mice)
> into these two.
>
> Signed-off-by: Nestor Lopez Casado <nlope...@logitech.com>
> Signed-off-by: Benjamin Tissoires <btiss...@logitech.com>
> ---
>

> áHi guys,
>
> áthis is the third version of the unifying receiver. I made the changes requested
> áby Dmitry:
> á- switch to kfifo
> á- introduce __func__ in all debugs/errors messages
> á- in logi_dj_ll_input_event, do not use the report of the device, but a copy
> á(is it the right way to do it to avoid the DMA problem?)
> á- switch to BUS_USB for the created devices
> á- small other cosmetic changes
>
> áOne side note:
> áWe really thing using BUS_USB is not the right way to do it when we will have
> ámore drivers:
> á by keeping it, we will have no choice but to provide the VIP/PID of the
> áreceiver to the created devices. We then will have to handle a manual parsing
> áof the wireless id to know which driver to use. This will result in having
> áall the Logitech's drivers loaded when someone plug an unifying receiver.
> á if we switch to a new BUS_DJ, we could then rely on the automatic user-space
> áloading of the drivers to load only the right drivers.
>
> áBTW, as long as Logitech does not provide those drivers (I hope they will come
> áout soon), there is no point using BUS_DJ. So it's fine not using it right now.
>
> áThanks,
> áBenjamin
>
> ádrivers/hid/Kconfig á á á á á | á á9 +
> ádrivers/hid/Makefile á á á á á| á á1 +
> ádrivers/hid/hid-core.c á á á á| á á2 +
> ádrivers/hid/hid-ids.h á á á á | á á2 +
> ádrivers/hid/hid-logitech-dj.c | á893 +++++++++++++++++++++++++++++++++++++++++
> ádrivers/hid/hid-logitech-dj.h | á123 ++++++
> á6 files changed, 1030 insertions(+), 0 deletions(-)
> ácreate mode 100644 drivers/hid/hid-logitech-dj.c
> ácreate mode 100644 drivers/hid/hid-logitech-dj.h


>
> diff --git a/drivers/hid/Kconfig b/drivers/hid/Kconfig
> index 36ca465..0eb7f04 100644
> --- a/drivers/hid/Kconfig
> +++ b/drivers/hid/Kconfig
> @@ -231,6 +231,15 @@ config HID_LOGITECH

> á á á á---help---
> á á á áSupport for Logitech devices that are not fully compliant with HID standard.
>
> +config HID_LOGITECH_DJ
> + á á á tristate "Logitech Unifying receivers full support"
> + á á á depends on HID_LOGITECH
> + á á á default m
> + á á á ---help---
> + á á á Say Y if you want support for Logitech Unifying receivers and devices.
> + á á á Unifying receivers are capable of pairing up to 6 Logitech compliant
> + á á á devices to the same receiver.
> +
> áconfig LOGITECH_FF
> á á á ábool "Logitech force feedback support"
> á á á ádepends on HID_LOGITECH


> diff --git a/drivers/hid/Makefile b/drivers/hid/Makefile
> index a24cee4..3aba5e7 100644
> --- a/drivers/hid/Makefile
> +++ b/drivers/hid/Makefile

> @@ -44,6 +44,7 @@ obj-$(CONFIG_HID_KEYTOUCH) á á+= hid-keytouch.o
> áobj-$(CONFIG_HID_KYE) á á á á á+= hid-kye.o
> áobj-$(CONFIG_HID_LCPOWER) á á á += hid-lcpower.o
> áobj-$(CONFIG_HID_LOGITECH) á á += hid-logitech.o
> +obj-$(CONFIG_HID_LOGITECH_DJ) á+= hid-logitech-dj.o
> áobj-$(CONFIG_HID_MAGICMOUSE) á á+= hid-magicmouse.o
> áobj-$(CONFIG_HID_MICROSOFT) á á+= hid-microsoft.o
> áobj-$(CONFIG_HID_MONTEREY) á á += hid-monterey.o


> diff --git a/drivers/hid/hid-core.c b/drivers/hid/hid-core.c
> index b0e618b..ebe4fdd 100644
> --- a/drivers/hid/hid-core.c
> +++ b/drivers/hid/hid-core.c
> @@ -1423,6 +1423,8 @@ static const struct hid_device_id hid_have_special_driver[] = {

> á á á á{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_DFP_WHEEL) },
> á á á á{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_G25_WHEEL) },
> á á á á{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_G27_WHEEL) },
> + á á á { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_UNIFYING_RECEIVER) },
> + á á á { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_UNIFYING_RECEIVER_2) },
> á á á á{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_WII_WHEEL) },
> á á á á{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_RUMBLEPAD2) },
> á á á á{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_SPACETRAVELLER) },


> diff --git a/drivers/hid/hid-ids.h b/drivers/hid/hid-ids.h
> index a756ee6..c42764b 100644
> --- a/drivers/hid/hid-ids.h
> +++ b/drivers/hid/hid-ids.h
> @@ -440,6 +440,8 @@

> á#define USB_DEVICE_ID_S510_RECEIVER_2 á0xc517
> á#define USB_DEVICE_ID_LOGITECH_CORDLESS_DESKTOP_LX500 á0xc512
> á#define USB_DEVICE_ID_MX3000_RECEIVER á0xc513
> +#define USB_DEVICE_ID_LOGITECH_UNIFYING_RECEIVER á á á 0xc52b
> +#define USB_DEVICE_ID_LOGITECH_UNIFYING_RECEIVER_2 á á 0xc532
> á#define USB_DEVICE_ID_SPACETRAVELLER á 0xc623
> á#define USB_DEVICE_ID_SPACENAVIGATOR á 0xc626
> á#define USB_DEVICE_ID_DINOVO_DESKTOP á 0xc704


> diff --git a/drivers/hid/hid-logitech-dj.c b/drivers/hid/hid-logitech-dj.c
> new file mode 100644
> index 0000000..896328d
> --- /dev/null
> +++ b/drivers/hid/hid-logitech-dj.c
> @@ -0,0 +1,893 @@
> +/*

> + * áHID driver for Logitech Unifying receivers
> + *
> + * áCopyright (c) 2011 Logitech (c)


> + */
> +
> +/*
> + * This program is free software; you can redistribute it and/or modify
> + * it under the terms of the GNU General Public License version 2 as
> + * published by the Free Software Foundation.
> +
> + *
> + * This program is distributed in the hope that it will be useful,
> + * but WITHOUT ANY WARRANTY; without even the implied warranty of

> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. áSee the


> + * GNU General Public License for more details.
> + *
> + * You should have received a copy of the GNU General Public License
> + * along with this program; if not, write to the Free Software
> + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
> + *
> + * Should you need to contact me, the author, you can do so by e-mail send
> + * your message to Nestor Lopez Casado <xnlopez at gmail com>
> + *
> + */
> +
> +
> +#include <linux/device.h>
> +#include <linux/hid.h>
> +#include <linux/module.h>
> +#include <linux/usb.h>
> +#include "usbhid/usbhid.h"
> +#include "hid-ids.h"
> +#include "hid-logitech-dj.h"
> +
> +/* Keyboard descriptor (1) */
> +static const char kbd_descriptor[] = {

> + á á á 0x05, 0x01, á á á á á á /* USAGE_PAGE (generic Desktop) á á */
> + á á á 0x09, 0x06, á á á á á á /* USAGE (Keyboard) á á á á */
> + á á á 0xA1, 0x01, á á á á á á /* COLLECTION (Application) á á */
> + á á á 0x85, 0x01, á á á á á á /* REPORT_ID (1) á á á á á á*/
> + á á á 0x95, 0x08, á á á á á á /* á REPORT_COUNT (8) á á á á á */
> + á á á 0x75, 0x01, á á á á á á /* á REPORT_SIZE (1) á á á á á á*/
> + á á á 0x15, 0x00, á á á á á á /* á LOGICAL_MINIMUM (0) á á á á*/
> + á á á 0x25, 0x01, á á á á á á /* á LOGICAL_MAXIMUM (1) á á á á*/
> + á á á 0x05, 0x07, á á á á á á /* á USAGE_PAGE (Keyboard) á á á*/
> + á á á 0x19, 0xE0, á á á á á á /* á USAGE_MINIMUM (Left Control) á */
> + á á á 0x29, 0xE7, á á á á á á /* á USAGE_MAXIMUM (Right GUI) á á á*/
> + á á á 0x81, 0x02, á á á á á á /* á INPUT (Data,Var,Abs) á á á */
> + á á á 0x95, 0x05, á á á á á á /* á REPORT COUNT (5) á á á á á */
> + á á á 0x05, 0x08, á á á á á á /* á USAGE PAGE (LED page) á á á*/
> + á á á 0x19, 0x01, á á á á á á /* á USAGE MINIMUM (1) á á á á á*/
> + á á á 0x29, 0x05, á á á á á á /* á USAGE MAXIMUM (5) á á á á á*/
> + á á á 0x91, 0x02, á á á á á á /* á OUTPUT (Data, Variable, Absolute) á*/
> + á á á 0x95, 0x01, á á á á á á /* á REPORT COUNT (1) á á á á á */
> + á á á 0x75, 0x03, á á á á á á /* á REPORT SIZE (3) á á á á á á*/
> + á á á 0x91, 0x01, á á á á á á /* á OUTPUT (Constant) á á á á á*/
> + á á á 0x95, 0x06, á á á á á á /* á REPORT_COUNT (6) á á á á á */
> + á á á 0x75, 0x08, á á á á á á /* á REPORT_SIZE (8) á á á á á á*/
> + á á á 0x15, 0x00, á á á á á á /* á LOGICAL_MINIMUM (0) á á á á*/
> + á á á 0x26, 0xFF, 0x00, á á á /* á LOGICAL_MAXIMUM (255) á á á*/
> + á á á 0x05, 0x07, á á á á á á /* á USAGE_PAGE (Keyboard) á á á*/
> + á á á 0x19, 0x00, á á á á á á /* á USAGE_MINIMUM (no event) á á á */
> + á á á 0x2A, 0xFF, 0x00, á á á /* á USAGE_MAXIMUM (reserved) á á á */
> + á á á 0x81, 0x00, á á á á á á /* á INPUT (Data,Ary,Abs) á á á */
> + á á á 0xC0
> +};
> +
> +/* Mouse descriptor (2) á á */


> +static const char mse_descriptor[] = {

> + á á á 0x05, 0x01, á á á á á á /* áUSAGE_PAGE (Generic Desktop) á á á á*/
> + á á á 0x09, 0x02, á á á á á á /* áUSAGE (Mouse) á á á á á á á á á á á */
> + á á á 0xA1, 0x01, á á á á á á /* áCOLLECTION (Application) á á á á á á*/
> + á á á 0x85, 0x02, á á á á á á /* á áREPORT_ID = 2 á á á á á á á á á á */
> + á á á 0x09, 0x01, á á á á á á /* á áUSAGE (pointer) á á á á á á á á á */
> + á á á 0xA1, 0x00, á á á á á á /* á áCOLLECTION (physical) á á á á á á */
> + á á á 0x05, 0x09, á á á á á á /* á á áUSAGE_PAGE (buttons) á á á á á á*/
> + á á á 0x19, 0x01, á á á á á á /* á á áUSAGE_MIN (1) á á á á á á á á á */
> + á á á 0x29, 0x10, á á á á á á /* á á áUSAGE_MAX (16) á á á á á á á á á*/
> + á á á 0x15, 0x00, á á á á á á /* á á áLOGICAL_MIN (0) á á á á á á á á */
> + á á á 0x25, 0x01, á á á á á á /* á á áLOGICAL_MAX (1) á á á á á á á á */
> + á á á 0x95, 0x10, á á á á á á /* á á áREPORT_COUNT (16) á á á á á á á */
> + á á á 0x75, 0x01, á á á á á á /* á á áREPORT_SIZE (1) á á á á á á á á */
> + á á á 0x81, 0x02, á á á á á á /* á á áINPUT (data var abs) á á á á á á*/
> + á á á 0x05, 0x01, á á á á á á /* á á áUSAGE_PAGE (generic desktop) á á*/
> + á á á 0x16, 0x01, 0xF8, á á á /* á á áLOGICAL_MIN (-2047) á á á á á á */
> + á á á 0x26, 0xFF, 0x07, á á á /* á á áLOGICAL_MAX (2047) á á á á á á á*/
> + á á á 0x75, 0x0C, á á á á á á /* á á áREPORT_SIZE (12) á á á á á á á á*/
> + á á á 0x95, 0x02, á á á á á á /* á á áREPORT_COUNT (2) á á á á á á á á*/
> + á á á 0x09, 0x30, á á á á á á /* á á áUSAGE (X) á á á á á á á á á á á */
> + á á á 0x09, 0x31, á á á á á á /* á á áUSAGE (Y) á á á á á á á á á á á */
> + á á á 0x81, 0x06, á á á á á á /* á á áINPUT á á á á á á á á á á á á á */
> + á á á 0x15, 0x81, á á á á á á /* á á áLOGICAL_MIN (-127) á á á á á á á*/
> + á á á 0x25, 0x7F, á á á á á á /* á á áLOGICAL_MAX (127) á á á á á á á */
> + á á á 0x75, 0x08, á á á á á á /* á á áREPORT_SIZE (8) á á á á á á á á */
> + á á á 0x95, 0x01, á á á á á á /* á á áREPORT_COUNT (1) á á á á á á á á*/
> + á á á 0x09, 0x38, á á á á á á /* á á áUSAGE (wheel) á á á á á á á á á */
> + á á á 0x81, 0x06, á á á á á á /* á á áINPUT á á á á á á á á á á á á á */
> + á á á 0x05, 0x0C, á á á á á á /* á á áUSAGE_PAGE(consumer) á á á á á á*/
> + á á á 0x0A, 0x38, 0x02, á á á /* á á áUSAGE(AC Pan) á á á á á á á á á */
> + á á á 0x95, 0x01, á á á á á á /* á á áREPORT_COUNT (1) á á á á á á á á*/
> + á á á 0x81, 0x06, á á á á á á /* á á áINPUT á á á á á á á á á á á á á */
> + á á á 0xC0, á á á á á á á á á /* á áEND_COLLECTION á á á á á á á á á á*/
> + á á á 0xC0, á á á á á á á á á /* áEND_COLLECTION á á á á á á á á á á á*/


> +};
> +
> +/* Consumer Control descriptor (3) */
> +static const char consumer_descriptor[] = {

> + á á á 0x05, 0x0C, á á á á á á /* USAGE_PAGE (Consumer Devices) á á á */
> + á á á 0x09, 0x01, á á á á á á /* USAGE (Consumer Control) á á á á á á*/
> + á á á 0xA1, 0x01, á á á á á á /* COLLECTION (Application) á á á á á á*/
> + á á á 0x85, 0x03, á á á á á á /* REPORT_ID = 3 á á á á á á á á á á á */
> + á á á 0x75, 0x10, á á á á á á /* REPORT_SIZE (16) á á á á á á á á á á*/
> + á á á 0x95, 0x02, á á á á á á /* REPORT_COUNT (2) á á á á á á á á á á*/
> + á á á 0x15, 0x01, á á á á á á /* LOGICAL_MIN (1) á á á á á á á á á á */
> + á á á 0x26, 0x8C, 0x02, á á á /* LOGICAL_MAX (652) á á á á á á á á á */
> + á á á 0x19, 0x01, á á á á á á /* USAGE_MIN (1) á á á á á á á á á á á */
> + á á á 0x2A, 0x8C, 0x02, á á á /* USAGE_MAX (652) á á á á á á á á á á */
> + á á á 0x81, 0x00, á á á á á á /* INPUT (Data Ary Abs) á á á á á á á á*/
> + á á á 0xC0, á á á á á á á á á /* END_COLLECTION á á á á á á á á á á á*/
> +}; á á á á á á á á á á á á á á /* á á á á á á á á á á á á á á á á á á */


> +
> +/* System control descriptor (4) */
> +static const char syscontrol_descriptor[] = {

> + á á á 0x05, 0x01, á á á á á á /* á USAGE_PAGE (Generic Desktop) á á á*/
> + á á á 0x09, 0x80, á á á á á á /* á USAGE (System Control) á á á á á á*/
> + á á á 0xA1, 0x01, á á á á á á /* á COLLECTION (Application) á á á á á*/
> + á á á 0x85, 0x04, á á á á á á /* á REPORT_ID = 4 á á á á á á á á á á */
> + á á á 0x75, 0x02, á á á á á á /* á REPORT_SIZE (2) á á á á á á á á á */
> + á á á 0x95, 0x01, á á á á á á /* á REPORT_COUNT (1) á á á á á á á á á*/
> + á á á 0x15, 0x01, á á á á á á /* á LOGICAL_MIN (1) á á á á á á á á á */
> + á á á 0x25, 0x03, á á á á á á /* á LOGICAL_MAX (3) á á á á á á á á á */
> + á á á 0x09, 0x82, á á á á á á /* á USAGE (System Sleep) á á á á á á á*/
> + á á á 0x09, 0x81, á á á á á á /* á USAGE (System Power Down) á á á á */
> + á á á 0x09, 0x83, á á á á á á /* á USAGE (System Wake Up) á á á á á á*/
> + á á á 0x81, 0x60, á á á á á á /* á INPUT (Data Ary Abs NPrf Null) á á*/
> + á á á 0x75, 0x06, á á á á á á /* á REPORT_SIZE (6) á á á á á á á á á */
> + á á á 0x81, 0x03, á á á á á á /* á INPUT (Cnst Var Abs) á á á á á á á*/
> + á á á 0xC0, á á á á á á á á á /* á END_COLLECTION á á á á á á á á á á*/


> +};
> +
> +/* Media descriptor (8) */
> +static const char media_descriptor[] = {

> + á á á 0x06, 0xbc, 0xff, á á á /* Usage Page 0xffbc á á á á á á á á á */
> + á á á 0x09, 0x88, á á á á á á /* Usage 0x0088 á á á á á á á á á á á á*/
> + á á á 0xa1, 0x01, á á á á á á /* BeginCollection á á á á á á á á á á */
> + á á á 0x85, 0x08, á á á á á á /* á Report ID 8 á á á á á á á á á á á */
> + á á á 0x19, 0x01, á á á á á á /* á Usage Min 0x0001 á á á á á á á á á*/
> + á á á 0x29, 0xff, á á á á á á /* á Usage Max 0x00ff á á á á á á á á á*/
> + á á á 0x15, 0x01, á á á á á á /* á Logical Min 1 á á á á á á á á á á */
> + á á á 0x26, 0xff, 0x00, á á á /* á Logical Max 255 á á á á á á á á á */
> + á á á 0x75, 0x08, á á á á á á /* á Report Size 8 á á á á á á á á á á */
> + á á á 0x95, 0x01, á á á á á á /* á Report Count 1 á á á á á á á á á á*/
> + á á á 0x81, 0x00, á á á á á á /* á Input á á á á á á á á á á á á á á */
> + á á á 0xc0, á á á á á á á á á /* EndCollection á á á á á á á á á á á */
> +}; á á á á á á á á á á á á á á /* á á á á á á á á á á á á á á á á á á */


> +
> +/* Maximum size of all defined hid reports in bytes (including report id) */
> +#define MAX_REPORT_SIZE 8
> +
> +/* Number of possible hid report types that can be created by this driver.
> + *
> + * Right now, RF report types have the same report types (or report id's)
> + * than the hid report created from those RF reports. In the future
> + * this doesnt have to be true.
> + *
> + * For instance, RF report type 0x01 which has a size of 8 bytes, corresponds
> + * to hid report id 0x01, this is standard keyboard. Same thing applies to mice
> + * reports and consumer control, etc. If a new RF report is created, it doesn't
> + * has to have the same report id as its corresponding hid report, so an
> + * translation may have to take place for future report types.
> + */
> +#define NUMBER_OF_HID_REPORTS 32
> +static const u8 hid_reportid_size_map[NUMBER_OF_HID_REPORTS] = {

> + á á á [1] = 8, á á á á á á á á/* Standard keyboard */
> + á á á [2] = 8, á á á á á á á á/* Standard mouse */
> + á á á [3] = 5, á á á á á á á á/* Consumer control */
> + á á á [4] = 2, á á á á á á á á/* System control */
> + á á á [8] = 2, á á á á á á á á/* Media Center */


> +};
> +
> +
> +#define LOGITECH_DJ_INTERFACE_NUMBER 0x02
> +
> +static struct hid_ll_driver logi_dj_ll_driver;
> +
> +static int logi_dj_output_hidraw_report(struct hid_device *hid, u8 * buf,

> + á á á á á á á á á á á á á á á á á á á size_t count,
> + á á á á á á á á á á á á á á á á á á á unsigned char report_type);


> +
> +static void logi_dj_recv_destroy_djhid_device(struct dj_receiver_dev *djrcv_dev,

> + á á á á á á á á á á á á á á á á á á á á á á á struct dj_report *dj_report)
> +{
> + á á á /* Called in delayed work context */
> + á á á struct dj_device *dj_dev;
> + á á á unsigned long flags;
> +
> + á á á spin_lock_irqsave(&djrcv_dev->lock, flags);
> + á á á dj_dev = djrcv_dev->paired_dj_devices[dj_report->device_index];
> + á á á djrcv_dev->paired_dj_devices[dj_report->device_index] = NULL;
> + á á á spin_unlock_irqrestore(&djrcv_dev->lock, flags);
> +
> + á á á if (dj_dev != NULL) {
> + á á á á á á á hid_destroy_device(dj_dev->hdev);
> + á á á á á á á kfree(dj_dev);
> + á á á } else {
> + á á á á á á á dev_err(&djrcv_dev->hdev->dev, "%s: can't destroy a NULL device\n",
> + á á á á á á á á á á á __func__);
> + á á á }


> +}
> +
> +static void logi_dj_recv_add_djhid_device(struct dj_receiver_dev *djrcv_dev,

> + á á á á á á á á á á á á á á á á á á á á struct dj_report *dj_report)
> +{
> + á á á /* Called in delayed work context */
> + á á á struct hid_device *djrcv_hdev = djrcv_dev->hdev;
> + á á á struct usb_interface *intf = to_usb_interface(djrcv_hdev->dev.parent);
> + á á á struct usb_device *usbdev = interface_to_usbdev(intf);
> + á á á struct hid_device *dj_hiddev;
> + á á á struct dj_device *dj_dev;
> + á á á unsigned char tmpstr[20];
> +
> + á á á if (dj_report->report_params[DEVICE_PAIRED_PARAM_SPFUNCTION] &
> + á á á á á SPFUNCTION_DEVICE_LIST_EMPTY) {
> + á á á á á á á dbg_hid("%s: device list is empty\n", __func__);
> + á á á á á á á return;
> + á á á }
> +
> + á á á dj_hiddev = hid_allocate_device();
> + á á á if (IS_ERR(dj_hiddev)) {
> + á á á á á á á dev_err(&djrcv_hdev->dev, "%s: hid_allocate_device failed\n",
> + á á á á á á á á á á á __func__);
> + á á á á á á á return;
> + á á á }
> +
> + á á á dj_hiddev->ll_driver = &logi_dj_ll_driver;
> + á á á dj_hiddev->hid_output_raw_report = logi_dj_output_hidraw_report;
> +
> + á á á dj_hiddev->dev.parent = &djrcv_hdev->dev;
> + á á á dj_hiddev->bus = BUS_USB;
> + á á á dj_hiddev->vendor = le16_to_cpu(usbdev->descriptor.idVendor);
> + á á á dj_hiddev->product = le16_to_cpu(usbdev->descriptor.idProduct);
> + á á á snprintf(dj_hiddev->name, sizeof(dj_hiddev->name),
> + á á á á á á á "Logitech Unifying Device. Wireless PID:%02x%02x",
> + á á á á á á á dj_report->report_params[DEVICE_PAIRED_PARAM_EQUAD_ID_MSB],
> + á á á á á á á dj_report->report_params[DEVICE_PAIRED_PARAM_EQUAD_ID_LSB]);
> +
> + á á á usb_make_path(usbdev, dj_hiddev->phys, sizeof(dj_hiddev->phys));
> + á á á snprintf(tmpstr, sizeof(tmpstr), ":%d", dj_report->device_index);
> + á á á strlcat(dj_hiddev->phys, tmpstr, sizeof(dj_hiddev->phys));
> +
> + á á á dj_dev = kzalloc(sizeof(struct dj_device), GFP_KERNEL);
> +
> + á á á if (!dj_dev) {
> + á á á á á á á dev_err(&djrcv_hdev->dev, "%s: failed allocating dj_device\n",
> + á á á á á á á á á á á __func__);
> + á á á á á á á goto dj_device_allocate_fail;
> + á á á }
> +
> + á á á dj_dev->reports_supported = le32_to_cpu(
> + á á á á á á á dj_report->report_params[DEVICE_PAIRED_RF_REPORT_TYPE]);
> + á á á dj_dev->hdev = dj_hiddev;
> + á á á dj_dev->dj_receiver_dev = djrcv_dev;
> + á á á dj_dev->device_index = dj_report->device_index;
> + á á á dj_hiddev->driver_data = dj_dev;
> +
> + á á á djrcv_dev->paired_dj_devices[dj_report->device_index] = dj_dev;
> +
> + á á á if (hid_add_device(dj_hiddev)) {
> + á á á á á á á dev_err(&djrcv_hdev->dev, "%s: failed adding dj_device\n",
> + á á á á á á á á á á á __func__);
> + á á á á á á á goto hid_add_device_fail;
> + á á á }
> +
> + á á á return;
> +
> +hid_add_device_fail:
> + á á á djrcv_dev->paired_dj_devices[dj_report->device_index] = NULL;
> + á á á kfree(dj_dev);
> +dj_device_allocate_fail:
> + á á á hid_destroy_device(dj_hiddev);


> +}
> +
> +static void delayedwork_callback(struct work_struct *work)
> +{

> + á á á struct dj_receiver_dev *djrcv_dev =
> + á á á á á á á container_of(work, struct dj_receiver_dev, work);
> +
> + á á á struct dj_report dj_report;
> + á á á unsigned long flags;
> + á á á int count;
> +
> + á á á dbg_hid("%s\n", __func__);
> +
> + á á á spin_lock_irqsave(&djrcv_dev->lock, flags);
> +
> + á á á count = kfifo_out(&djrcv_dev->notif_fifo, &dj_report,
> + á á á á á á á á á á á á á á á sizeof(struct dj_report));
> +
> + á á á if (count != sizeof(struct dj_report)) {
> + á á á á á á á dev_err(&djrcv_dev->hdev->dev, "%s: workitem triggered without "
> + á á á á á á á á á á á "notifications available\n", __func__);
> + á á á á á á á spin_unlock_irqrestore(&djrcv_dev->lock, flags);
> + á á á á á á á return;
> + á á á }
> +
> + á á á if (!kfifo_is_empty(&djrcv_dev->notif_fifo)) {
> + á á á á á á á if (schedule_work(&djrcv_dev->work) == 0) {
> + á á á á á á á á á á á dbg_hid("%s: did not schedule the work item, was "
> + á á á á á á á á á á á á á á á "already queued\n", __func__);
> + á á á á á á á }
> + á á á }
> +
> + á á á spin_unlock_irqrestore(&djrcv_dev->lock, flags);
> +
> + á á á switch (dj_report.report_type) {
> + á á á case REPORT_TYPE_NOTIF_DEVICE_PAIRED:
> + á á á á á á á logi_dj_recv_add_djhid_device(djrcv_dev, &dj_report);
> + á á á á á á á break;
> + á á á case REPORT_TYPE_NOTIF_DEVICE_UNPAIRED:
> + á á á á á á á logi_dj_recv_destroy_djhid_device(djrcv_dev, &dj_report);
> + á á á á á á á break;
> + á á á default:
> + á á á á á á á dbg_hid("%s: unexpected report type\n", __func__);
> + á á á }


> +}
> +
> +static void logi_dj_recv_queue_notification(struct dj_receiver_dev *djrcv_dev,

> + á á á á á á á á á á á á á á á á á á á á ástruct dj_report *dj_report)
> +{
> + á á á /* We are called from atomic context (tasklet && djrcv->lock held) */
> +
> + á á á kfifo_in(&djrcv_dev->notif_fifo, dj_report, sizeof(struct dj_report));
> +
> + á á á if (schedule_work(&djrcv_dev->work) == 0) {
> + á á á á á á á dbg_hid("%s: did not schedule the work item, was already "
> + á á á á á á á á á á á "queued\n", __func__);
> + á á á }


> +}
> +
> +static void logi_dj_recv_forward_null_report(struct dj_receiver_dev *djrcv_dev,

> + á á á á á á á á á á á á á á á á á á á á á ástruct dj_report *dj_report)
> +{
> + á á á /* We are called from atomic context (tasklet && djrcv->lock held) */
> + á á á unsigned int i;
> + á á á u8 reportbuffer[MAX_REPORT_SIZE];
> + á á á struct dj_device *djdev;
> +
> + á á á djdev = djrcv_dev->paired_dj_devices[dj_report->device_index];
> +
> + á á á if (!djdev) {
> + á á á á á á á dbg_hid("djrcv_dev->paired_dj_devices[dj_report->device_index]"
> + á á á á á á á á á á á " is NULL, index %d\n", dj_report->device_index);
> + á á á á á á á return;
> + á á á }
> +
> + á á á memset(reportbuffer, 0, sizeof(reportbuffer));
> +
> + á á á for (i = 0; i < NUMBER_OF_HID_REPORTS; i++) {
> + á á á á á á á if (djdev->reports_supported & (1 << i)) {
> + á á á á á á á á á á á reportbuffer[0] = i;
> + á á á á á á á á á á á if (hid_input_report(djdev->hdev,
> + á á á á á á á á á á á á á á á á á á á á á áHID_INPUT_REPORT,
> + á á á á á á á á á á á á á á á á á á á á á áreportbuffer,
> + á á á á á á á á á á á á á á á á á á á á á áhid_reportid_size_map[i], 1)) {
> + á á á á á á á á á á á á á á á dbg_hid("hid_input_report error sending null "
> + á á á á á á á á á á á á á á á á á á á "report\n");
> + á á á á á á á á á á á }
> + á á á á á á á }
> + á á á }


> +}
> +
> +static void logi_dj_recv_forward_report(struct dj_receiver_dev *djrcv_dev,

> + á á á á á á á á á á á á á á á á á á á struct dj_report *dj_report)
> +{
> + á á á /* We are called from atomic context (tasklet && djrcv->lock held) */
> + á á á struct dj_device *dj_device;
> +
> + á á á dj_device = djrcv_dev->paired_dj_devices[dj_report->device_index];
> +
> + á á á if (dj_device == NULL) {
> + á á á á á á á dbg_hid("djrcv_dev->paired_dj_devices[dj_report->device_index]"
> + á á á á á á á á á á á " is NULL, index %d\n", dj_report->device_index);
> + á á á á á á á return;
> + á á á }
> +
> + á á á if ((dj_report->report_type > ARRAY_SIZE(hid_reportid_size_map) - 1) ||
> + á á á á á (hid_reportid_size_map[dj_report->report_type] == 0)) {
> + á á á á á á á dbg_hid("invalid report type:%x\n", dj_report->report_type);
> + á á á á á á á return;
> + á á á }
> +
> + á á á if (hid_input_report(dj_device->hdev,
> + á á á á á á á á á á á HID_INPUT_REPORT, &dj_report->report_type,
> + á á á á á á á á á á á hid_reportid_size_map[dj_report->report_type], 1)) {
> + á á á á á á á dbg_hid("hid_input_report error\n");
> + á á á }


> +}
> +
> +
> +static int logi_dj_recv_send_report(struct dj_receiver_dev *djrcv_dev,

> + á á á á á á á á á á á á á á á á á struct dj_report *dj_report)
> +{
> + á á á struct hid_device *hdev = djrcv_dev->hdev;
> + á á á int sent_bytes;
> +
> + á á á if (!hdev->hid_output_raw_report) {
> + á á á á á á á dev_err(&hdev->dev, "%s:"
> + á á á á á á á á á á á "hid_output_raw_report is null\n", __func__);
> + á á á á á á á return -ENODEV;
> + á á á }
> +
> + á á á sent_bytes = hdev->hid_output_raw_report(hdev, (u8 *) dj_report,
> + á á á á á á á á á á á á á á á á á á á á á á á ásizeof(struct dj_report),
> + á á á á á á á á á á á á á á á á á á á á á á á áHID_OUTPUT_REPORT);
> +
> + á á á return (sent_bytes < 0) ? sent_bytes : 0;


> +}
> +
> +static int logi_dj_recv_query_paired_devices(struct dj_receiver_dev *djrcv_dev)
> +{

> + á á á struct dj_report dj_report;
> +
> + á á á memset(&dj_report, 0, sizeof(dj_report));
> + á á á dj_report.report_id = REPORT_ID_DJ_SHORT;
> + á á á dj_report.device_index = 0xFF;
> + á á á dj_report.report_type = REPORT_TYPE_CMD_GET_PAIRED_DEVICES;
> + á á á return logi_dj_recv_send_report(djrcv_dev, &dj_report);


> +}
> +
> +static int logi_dj_recv_switch_to_dj_mode(struct dj_receiver_dev *djrcv_dev,

> + á á á á á á á á á á á á á á á á á á á á unsigned timeout)
> +{
> + á á á struct dj_report dj_report;
> +
> + á á á memset(&dj_report, 0, sizeof(dj_report));
> + á á á dj_report.report_id = REPORT_ID_DJ_SHORT;
> + á á á dj_report.device_index = 0xFF;
> + á á á dj_report.report_type = REPORT_TYPE_CMD_SWITCH;
> + á á á dj_report.report_params[CMD_SWITCH_PARAM_DEVBITFIELD] = 0x1F;
> + á á á dj_report.report_params[CMD_SWITCH_PARAM_TIMEOUT_SECONDS] = (u8)timeout;
> + á á á return logi_dj_recv_send_report(djrcv_dev, &dj_report);


> +}
> +
> +
> +static int logi_dj_ll_open(struct hid_device *hid)
> +{

> + á á á dbg_hid("%s:%s\n", __func__, hid->phys);
> + á á á return 0;


> +
> +}
> +
> +static void logi_dj_ll_close(struct hid_device *hid)
> +{

> + á á á dbg_hid("%s:%s\n", __func__, hid->phys);


> +}
> +
> +static int logi_dj_output_hidraw_report(struct hid_device *hid, u8 * buf,

> + á á á á á á á á á á á á á á á á á á á size_t count,
> + á á á á á á á á á á á á á á á á á á á unsigned char report_type)
> +{
> + á á á /* Called by hid raw to send data */
> + á á á dbg_hid("%s\n", __func__);
> +
> + á á á return 0;


> +}
> +
> +static int logi_dj_ll_parse(struct hid_device *hid)
> +{

> + á á á struct dj_device *djdev = hid->driver_data;
> + á á á int retval;
> +
> + á á á dbg_hid("%s\n", __func__);
> +
> + á á á djdev->hdev->version = 0x0111;
> + á á á djdev->hdev->country = 0x00;
> +
> + á á á if (djdev->reports_supported & STD_KEYBOARD) {
> + á á á á á á á dbg_hid("%s: sending a kbd descriptor, reports_supported: %x\n",
> + á á á á á á á á á á á __func__, djdev->reports_supported);
> + á á á á á á á retval = hid_parse_report(hid,
> + á á á á á á á á á á á á á á á á á á á á (u8 *) kbd_descriptor,
> + á á á á á á á á á á á á á á á á á á á á sizeof(kbd_descriptor));
> + á á á á á á á if (retval) {
> + á á á á á á á á á á á dbg_hid("%s: sending a kbd descriptor, hid_parse failed"
> + á á á á á á á á á á á á á á á " error: %d\n", __func__, retval);
> + á á á á á á á á á á á return retval;
> + á á á á á á á }
> + á á á }
> +
> + á á á if (djdev->reports_supported & STD_MOUSE) {
> + á á á á á á á dbg_hid("%s: sending a mouse descriptor, reports_supported: "
> + á á á á á á á á á á á "%x\n", __func__, djdev->reports_supported);
> + á á á á á á á retval = hid_parse_report(hid,
> + á á á á á á á á á á á á á á á á á á á á (u8 *) mse_descriptor,
> + á á á á á á á á á á á á á á á á á á á á sizeof(mse_descriptor));
> + á á á á á á á if (retval) {
> + á á á á á á á á á á á dbg_hid("%s: sending a mouse descriptor, hid_parse "
> + á á á á á á á á á á á á á á á "failed error: %d\n", __func__, retval);
> + á á á á á á á á á á á return retval;
> + á á á á á á á }
> + á á á }
> +
> + á á á if (djdev->reports_supported & MULTIMEDIA) {
> + á á á á á á á dbg_hid("%s: sending a multimedia report descriptor: %x\n",
> + á á á á á á á á á á á __func__, djdev->reports_supported);
> + á á á á á á á retval = hid_parse_report(hid,
> + á á á á á á á á á á á á á á á á á á á á (u8 *) consumer_descriptor,
> + á á á á á á á á á á á á á á á á á á á á sizeof(consumer_descriptor));
> + á á á á á á á if (retval) {
> + á á á á á á á á á á á dbg_hid("%s: sending a consumer_descriptor, hid_parse "
> + á á á á á á á á á á á á á á á "failed error: %d\n", __func__, retval);
> + á á á á á á á á á á á return retval;
> + á á á á á á á }
> + á á á }
> +
> + á á á if (djdev->reports_supported & POWER_KEYS) {
> + á á á á á á á dbg_hid("%s: sending a power keys report descriptor: %x\n",
> + á á á á á á á á á á á __func__, djdev->reports_supported);
> + á á á á á á á retval = hid_parse_report(hid,
> + á á á á á á á á á á á á á á á á á á á á (u8 *) syscontrol_descriptor,
> + á á á á á á á á á á á á á á á á á á á á sizeof(syscontrol_descriptor));
> + á á á á á á á if (retval) {
> + á á á á á á á á á á á dbg_hid("%s: sending a syscontrol_descriptor, "
> + á á á á á á á á á á á á á á á "hid_parse failed error: %d\n",
> + á á á á á á á á á á á á á á á __func__, retval);
> + á á á á á á á á á á á return retval;
> + á á á á á á á }
> + á á á }
> +
> + á á á if (djdev->reports_supported & MEDIA_CENTER) {
> + á á á á á á á dbg_hid("%s: sending a media center report descriptor: %x\n",
> + á á á á á á á á á á á __func__, djdev->reports_supported);
> + á á á á á á á retval = hid_parse_report(hid,
> + á á á á á á á á á á á á á á á á á á á á (u8 *) media_descriptor,
> + á á á á á á á á á á á á á á á á á á á á sizeof(media_descriptor));
> + á á á á á á á if (retval) {
> + á á á á á á á á á á á dbg_hid("%s: sending a media_descriptor, hid_parse "
> + á á á á á á á á á á á á á á á "failed error: %d\n", __func__, retval);
> + á á á á á á á á á á á return retval;
> + á á á á á á á }
> + á á á }
> +
> + á á á if (djdev->reports_supported & KBD_LEDS) {
> + á á á á á á á dbg_hid("%s: need to send kbd leds report descriptor: %x\n",
> + á á á á á á á á á á á __func__, djdev->reports_supported);
> + á á á }
> +
> + á á á return 0;


> +}
> +
> +static int logi_dj_ll_input_event(struct input_dev *dev, unsigned int type,

> + á á á á á á á á á á á á á á á á unsigned int code, int value)
> +{
> + á á á /* Sent by the input layer to handle leds and Force Feedback */
> + á á á struct hid_device *dj_hiddev = input_get_drvdata(dev);
> + á á á struct dj_device *dj_dev = dj_hiddev->driver_data;
> +
> + á á á struct dj_receiver_dev *djrcv_dev =
> + á á á á á dev_get_drvdata(dj_hiddev->dev.parent);
> + á á á struct hid_device *dj_rcv_hiddev = djrcv_dev->hdev;
> + á á á struct hid_report_enum *output_report_enum;
> +
> + á á á struct hid_field *field;
> + á á á struct hid_report report;
> + á á á unsigned char data[8];
> + á á á int offset;
> +
> + á á á dbg_hid("%s: %s, type:%d | code:%d | value:%d\n",
> + á á á á á á á __func__, dev->phys, type, code, value);
> +
> + á á á if (type != EV_LED)
> + á á á á á á á return -1;
> +
> + á á á offset = hidinput_find_field(dj_hiddev, type, code, &field);
> +
> + á á á if (offset == -1) {
> + á á á á á á á dev_warn(&dev->dev, "event field not found\n");
> + á á á á á á á return -1;
> + á á á }
> + á á á hid_set_field(field, offset, value);
> + á á á hid_output_report(field->report, &data[0]);
> +
> + á á á output_report_enum = &dj_rcv_hiddev->report_enum[HID_OUTPUT_REPORT];
> + á á á report = *output_report_enum->report_id_hash[REPORT_ID_DJ_SHORT];
> + á á á hid_set_field(report.field[0], 0, dj_dev->device_index);
> + á á á hid_set_field(report.field[0], 1, REPORT_TYPE_LEDS);
> + á á á hid_set_field(report.field[0], 2, data[1]);
> +
> + á á á usbhid_submit_report(dj_rcv_hiddev, &report, USB_DIR_OUT);

This is not working when the receiver is paired to a keyboard. When
the session launches, Gnome try to send leds commands to the keyboard
and this results in an oops.

Using a pointer to the struct hid_report solves the crash.

However, Dmitry, you told us about a DMA stack problem. Can you give
us a better way to handle that?

Thanks,
Benjamin


> +
> + á á á return 0;


> +
> +}
> +
> +static int logi_dj_ll_start(struct hid_device *hid)
> +{

> + á á á dbg_hid("%s\n", __func__);
> + á á á return 0;


> +}
> +
> +static void logi_dj_ll_stop(struct hid_device *hid)
> +{

> + á á á dbg_hid("%s\n", __func__);


> +}
> +
> +
> +static struct hid_ll_driver logi_dj_ll_driver = {

> + á á á .parse = logi_dj_ll_parse,
> + á á á .start = logi_dj_ll_start,
> + á á á .stop = logi_dj_ll_stop,
> + á á á .open = logi_dj_ll_open,
> + á á á .close = logi_dj_ll_close,
> + á á á .hidinput_input_event = logi_dj_ll_input_event,


> +};
> +
> +
> +static int logi_dj_raw_event(struct hid_device *hdev,

> + á á á á á á á á á á á á á ástruct hid_report *report, u8 *data,
> + á á á á á á á á á á á á á áint size)
> +{
> + á á á struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
> + á á á struct dj_report *dj_report = (struct dj_report *) data;
> + á á á unsigned long flags;
> + á á á bool report_processed = false;
> +
> + á á á dbg_hid("%s, size:%d\n", __func__, size);
> +
> + á á á /* Here we receive all data coming from iface 2, there are 4 cases:
> + á á á á*
> + á á á á* 1) Data should continue its normal processing i.e. data does not
> + á á á á* come from the DJ collection, in which case we do nothing and
> + á á á á* return 0, so hid-core can continue normal processing (will forward
> + á á á á* to associated hidraw device)
> + á á á á*
> + á á á á* 2) Data is from DJ collection, and is intended for this driver i. e.
> + á á á á* data contains arrival, departure, etc notifications, in which case
> + á á á á* we queue them for delayed processing by the work queue. We return 1
> + á á á á* to hid-core as no further processing is required from it.
> + á á á á*
> + á á á á* 3) Data is from DJ collection, and informs a connection change,
> + á á á á* if the change means rf link loss, then we must send a null report
> + á á á á* to the upper layer to discard potentially pressed keys that may be
> + á á á á* repeated forever by the input layer. Return 1 to hid-core as no
> + á á á á* further processing is required.
> + á á á á*
> + á á á á* 4) Data is from DJ collection and is an actual input event from
> + á á á á* a paired DJ device in which case we forward it to the correct hid
> + á á á á* device (via hid_input_report() ) and return 1 so hid-core does not do
> + á á á á* anything else with it.
> + á á á á*/
> +
> + á á á spin_lock_irqsave(&djrcv_dev->lock, flags);
> + á á á if (dj_report->report_id == REPORT_ID_DJ_SHORT) {
> + á á á á á á á switch (dj_report->report_type) {
> + á á á á á á á case REPORT_TYPE_NOTIF_DEVICE_PAIRED:
> + á á á á á á á case REPORT_TYPE_NOTIF_DEVICE_UNPAIRED:
> + á á á á á á á á á á á logi_dj_recv_queue_notification(djrcv_dev, dj_report);
> + á á á á á á á á á á á break;
> + á á á á á á á case REPORT_TYPE_NOTIF_CONNECTION_STATUS:
> + á á á á á á á á á á á if (dj_report->report_params[CONNECTION_STATUS_PARAM_STATUS] ==
> + á á á á á á á á á á á á á STATUS_LINKLOSS) {
> + á á á á á á á á á á á á á á á logi_dj_recv_forward_null_report(djrcv_dev, dj_report);
> + á á á á á á á á á á á }
> + á á á á á á á á á á á break;
> + á á á á á á á default:
> + á á á á á á á á á á á logi_dj_recv_forward_report(djrcv_dev, dj_report);
> + á á á á á á á }
> + á á á á á á á report_processed = true;
> + á á á }
> + á á á spin_unlock_irqrestore(&djrcv_dev->lock, flags);
> +
> + á á á return report_processed;


> +}
> +
> +static int logi_dj_probe(struct hid_device *hdev,

> + á á á á á á á á á á á áconst struct hid_device_id *id)
> +{
> + á á á struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
> + á á á struct dj_receiver_dev *djrcv_dev;
> + á á á int retval;
> +
> + á á á if (is_dj_device((struct dj_device *)hdev->driver_data))
> + á á á á á á á return -ENODEV;
> +
> + á á á dbg_hid("%s called for ifnum %d\n", __func__,
> + á á á á á á á intf->cur_altsetting->desc.bInterfaceNumber);
> +
> + á á á /* Ignore interfaces 0 and 1, they will not carry any data, dont create
> + á á á á* any hid_device for them */
> + á á á if (intf->cur_altsetting->desc.bInterfaceNumber !=
> + á á á á á LOGITECH_DJ_INTERFACE_NUMBER) {
> + á á á á á á á dbg_hid("%s: ignoring ifnum %d\n", __func__,
> + á á á á á á á á á á á intf->cur_altsetting->desc.bInterfaceNumber);
> + á á á á á á á return -ENODEV;
> + á á á }
> +
> + á á á /* Treat interface 2 */
> +
> + á á á djrcv_dev = kzalloc(sizeof(struct dj_receiver_dev), GFP_KERNEL);
> + á á á if (!djrcv_dev) {
> + á á á á á á á dev_err(&hdev->dev,
> + á á á á á á á á á á á "%s:failed allocating dj_receiver_dev\n", __func__);
> + á á á á á á á return -ENOMEM;
> + á á á }
> + á á á djrcv_dev->hdev = hdev;
> + á á á INIT_WORK(&djrcv_dev->work, delayedwork_callback);
> + á á á spin_lock_init(&djrcv_dev->lock);
> + á á á if (kfifo_alloc(&djrcv_dev->notif_fifo,
> + á á á á á á á á á á á DJ_MAX_NUMBER_NOTIFICATIONS * sizeof(struct dj_report),
> + á á á á á á á á á á á GFP_KERNEL)) {
> + á á á á á á á dev_err(&hdev->dev,
> + á á á á á á á á á á á "%s:failed allocating notif_fifo\n", __func__);
> + á á á á á á á kfree(djrcv_dev);
> + á á á á á á á return -ENOMEM;
> + á á á }
> + á á á hid_set_drvdata(hdev, djrcv_dev);
> +
> + á á á /* Call áto usbhid to fetch the HID descriptors of interface 2 and
> + á á á á* subsequently call to the hid/hid-core to parse the fetched
> + á á á á* descriptors, this will in turn create the hidraw and hiddev nodes
> + á á á á* for interface 2 of the receiver */
> + á á á retval = hid_parse(hdev);
> + á á á if (retval) {
> + á á á á á á á dev_err(&hdev->dev,
> + á á á á á á á á á á á "%s:parse of interface 2 failed\n", __func__);
> + á á á á á á á goto hid_parse_fail;
> + á á á }
> +
> + á á á /* Starts the usb device and connects to upper interfaces hiddev and
> + á á á á* hidraw */
> + á á á retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
> + á á á if (retval) {
> + á á á á á á á dev_err(&hdev->dev,
> + á á á á á á á á á á á "%s:hid_hw_start returned error\n", __func__);
> + á á á á á á á goto hid_hw_start_fail;
> + á á á }
> +
> + á á á retval = logi_dj_recv_switch_to_dj_mode(djrcv_dev, 0);
> + á á á if (retval < 0) {
> + á á á á á á á dev_err(&hdev->dev,
> + á á á á á á á á á á á "%s:logi_dj_recv_switch_to_dj_mode returned error:%d\n",
> + á á á á á á á á á á á __func__, retval);
> + á á á á á á á goto switch_to_dj_mode_fail;
> + á á á }
> +
> + á á á /* This is enabling the polling urb on the IN endpoint */
> + á á á retval = hdev->ll_driver->open(hdev);
> + á á á if (retval < 0) {
> + á á á á á á á dev_err(&hdev->dev, "%s:hdev->ll_driver->open returned "
> + á á á á á á á á á á á "error:%d\n", __func__, retval);
> + á á á á á á á goto llopen_failed;
> + á á á }
> +
> + á á á retval = logi_dj_recv_query_paired_devices(djrcv_dev);
> + á á á if (retval < 0) {
> + á á á á á á á dev_err(&hdev->dev, "%s:logi_dj_recv_query_paired_devices "
> + á á á á á á á á á á á "error:%d\n", __func__, retval);
> + á á á á á á á goto logi_dj_recv_query_paired_devices_failed;
> + á á á }
> +
> + á á á return retval;
> +
> +logi_dj_recv_query_paired_devices_failed:
> + á á á hdev->ll_driver->close(hdev);
> +
> +llopen_failed:
> +switch_to_dj_mode_fail:
> + á á á hid_hw_stop(hdev);
> +
> +hid_hw_start_fail:
> +hid_parse_fail:
> + á á á kfifo_free(&djrcv_dev->notif_fifo);
> + á á á kfree(djrcv_dev);
> + á á á hid_set_drvdata(hdev, NULL);
> + á á á return retval;


> +
> +}
> +
> +static void logi_dj_remove(struct hid_device *hdev)
> +{

> + á á á struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
> + á á á struct dj_device *dj_dev;
> + á á á int i;
> +
> + á á á dbg_hid("%s\n", __func__);
> +
> + á á á cancel_work_sync(&djrcv_dev->work);
> +
> + á á á hdev->ll_driver->close(hdev);
> + á á á hid_hw_stop(hdev);
> +
> + á á á /* I suppose that at this point the only context that can access
> + á á á á* the djrecv_data is this thread as the work item is guaranteed to
> + á á á á* have finished and no more raw_event callbacks should arrive after
> + á á á á* the remove callback was triggered so no locks are put around the
> + á á á á* code below */
> + á á á for (i = 0; i < DJ_MAX_PAIRED_DEVICES; i++) {
> + á á á á á á á dj_dev = djrcv_dev->paired_dj_devices[i];
> + á á á á á á á if (dj_dev != NULL) {
> + á á á á á á á á á á á hid_destroy_device(dj_dev->hdev);
> + á á á á á á á á á á á kfree(dj_dev);
> + á á á á á á á á á á á djrcv_dev->paired_dj_devices[i] = NULL;
> + á á á á á á á }
> + á á á }
> +
> + á á á kfifo_free(&djrcv_dev->notif_fifo);
> + á á á kfree(djrcv_dev);
> + á á á hid_set_drvdata(hdev, NULL);


> +}
> +
> +static int logi_djdevice_probe(struct hid_device *hdev,

> + á á á á á á á á á á á áconst struct hid_device_id *id)
> +{
> + á á á int ret;
> + á á á struct dj_device *dj_dev = hdev->driver_data;
> +
> + á á á if (!is_dj_device(dj_dev))
> + á á á á á á á return -ENODEV;
> +
> + á á á ret = hid_parse(hdev);
> + á á á if (!ret)
> + á á á á á á á ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
> +
> + á á á return ret;


> +}
> +
> +static const struct hid_device_id logi_dj_receivers[] = {

> + á á á {HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
> + á á á á á á á USB_DEVICE_ID_LOGITECH_UNIFYING_RECEIVER)},
> + á á á {HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
> + á á á á á á á USB_DEVICE_ID_LOGITECH_UNIFYING_RECEIVER_2)},
> + á á á {}


> +};
> +
> +MODULE_DEVICE_TABLE(hid, logi_dj_receivers);
> +
> +static struct hid_driver logi_djreceiver_driver = {

> + á á á .name = "logitech-djreceiver",
> + á á á .id_table = logi_dj_receivers,
> + á á á .probe = logi_dj_probe,
> + á á á .remove = logi_dj_remove,
> + á á á .raw_event = logi_dj_raw_event


> +};
> +
> +
> +static const struct hid_device_id logi_dj_devices[] = {

> + á á á {HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
> + á á á á á á á USB_DEVICE_ID_LOGITECH_UNIFYING_RECEIVER)},
> + á á á {HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
> + á á á á á á á USB_DEVICE_ID_LOGITECH_UNIFYING_RECEIVER_2)},
> + á á á {}


> +};
> +
> +static struct hid_driver logi_djdevice_driver = {

> + á á á .name = "logitech-djdevice",
> + á á á .id_table = logi_dj_devices,
> + á á á .probe = logi_djdevice_probe,


> +};
> +
> +
> +static int __init logi_dj_init(void)
> +{

> + á á á int retval;
> +
> + á á á dbg_hid("Logitech-DJ:%s\n", __func__);
> +
> + á á á retval = hid_register_driver(&logi_djreceiver_driver);
> + á á á if (retval)
> + á á á á á á á return retval;
> +
> + á á á retval = hid_register_driver(&logi_djdevice_driver);
> + á á á if (retval)
> + á á á á á á á hid_unregister_driver(&logi_djreceiver_driver);
> +
> + á á á return retval;


> +
> +}
> +
> +static void __exit logi_dj_exit(void)
> +{

> + á á á dbg_hid("Logitech-DJ:%s\n", __func__);
> +
> + á á á hid_unregister_driver(&logi_djdevice_driver);
> + á á á hid_unregister_driver(&logi_djreceiver_driver);


> +
> +}
> +
> +module_init(logi_dj_init);
> +module_exit(logi_dj_exit);
> +MODULE_LICENSE("GPL");
> +MODULE_AUTHOR("Logitech");
> +MODULE_AUTHOR("Nestor Lopez Casado");
> diff --git a/drivers/hid/hid-logitech-dj.h b/drivers/hid/hid-logitech-dj.h
> new file mode 100644
> index 0000000..8dcdccd
> --- /dev/null
> +++ b/drivers/hid/hid-logitech-dj.h
> @@ -0,0 +1,123 @@
> +#ifndef __HID_LOGITECH_DJ_H
> +#define __HID_LOGITECH_DJ_H
> +
> +/*

> + * áHID driver for Logitech Unifying receivers
> + *
> + * áCopyright (c) 2011 Logitech (c)


> + */
> +
> +/*
> + * This program is free software; you can redistribute it and/or modify
> + * it under the terms of the GNU General Public License version 2 as
> + * published by the Free Software Foundation.
> + *
> + * This program is distributed in the hope that it will be useful,
> + * but WITHOUT ANY WARRANTY; without even the implied warranty of

> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. áSee the


> + * GNU General Public License for more details.
> + *
> + * You should have received a copy of the GNU General Public License
> + * along with this program; if not, write to the Free Software
> + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
> + *
> + * Should you need to contact me, the author, you can do so by e-mail send
> + * your message to Nestor Lopez Casado <xnlopez at gmail com>
> + *
> + */
> +
> +#include <linux/kfifo.h>
> +

> +#define DJ_MAX_PAIRED_DEVICES á á á á á á á á á6
> +#define DJ_MAX_NUMBER_NOTIFICATIONS á á á á á á8
> +
> +#define DJREPORT_SHORT_LENGTH á á á á á á á á á15
> +#define DJREPORT_LONG_LENGTH á á á á á á á á á 32
> +
> +#define REPORT_ID_DJ_SHORT á á á á á á á á á á 0x20
> +#define REPORT_ID_DJ_LONG á á á á á á á á á á á0x21
> +
> +#define REPORT_TYPE_RFREPORT_FIRST á á á á á á 0x01
> +#define REPORT_TYPE_RFREPORT_LAST á á á á á á á0x1F


> +
> +/* Command Switch to DJ mode */

> +#define REPORT_TYPE_CMD_SWITCH á á á á á á á á 0x80
> +#define CMD_SWITCH_PARAM_DEVBITFIELD á á á á á 0x00
> +#define CMD_SWITCH_PARAM_TIMEOUT_SECONDS á á á 0x01
> +#define TIMEOUT_NO_KEEPALIVE á á á á á á á á á 0x00


> +
> +/* Command to Get the list of Paired devices */

> +#define REPORT_TYPE_CMD_GET_PAIRED_DEVICES á á 0x81


> +
> +/* Device Paired Notification */

> +#define REPORT_TYPE_NOTIF_DEVICE_PAIRED á á á á á á á á0x41
> +#define SPFUNCTION_MORE_NOTIF_EXPECTED á á á á 0x01
> +#define SPFUNCTION_DEVICE_LIST_EMPTY á á á á á 0x02
> +#define DEVICE_PAIRED_PARAM_SPFUNCTION á á á á 0x00
> +#define DEVICE_PAIRED_PARAM_EQUAD_ID_LSB á á á 0x01
> +#define DEVICE_PAIRED_PARAM_EQUAD_ID_MSB á á á 0x02
> +#define DEVICE_PAIRED_RF_REPORT_TYPE á á á á á 0x03


> +
> +/* Device Un-Paired Notification */

> +#define REPORT_TYPE_NOTIF_DEVICE_UNPAIRED á á á0x40


> +
> +
> +/* Connection Status Notification */

> +#define REPORT_TYPE_NOTIF_CONNECTION_STATUS á á0x42
> +#define CONNECTION_STATUS_PARAM_STATUS á á á á 0x00
> +#define STATUS_LINKLOSS á á á á á á á á á á á á á á á á0x01


> +
> +/* Error Notification */

> +#define REPORT_TYPE_NOTIF_ERROR á á á á á á á á á á á á0x7F
> +#define NOTIF_ERROR_PARAM_ETYPE á á á á á á á á á á á á0x00
> +#define ETYPE_KEEPALIVE_TIMEOUT á á á á á á á á á á á á0x01


> +
> +/* supported DJ HID && RF report types */

> +#define REPORT_TYPE_KEYBOARD á á á á á á á á á 0x01
> +#define REPORT_TYPE_MOUSE á á á á á á á á á á á0x02
> +#define REPORT_TYPE_CONSUMER_CONTROL á á á á á 0x03
> +#define REPORT_TYPE_SYSTEM_CONTROL á á á á á á 0x04
> +#define REPORT_TYPE_MEDIA_CENTER á á á á á á á 0x08
> +#define REPORT_TYPE_LEDS á á á á á á á á á á á 0x0E


> +
> +/* RF Report types bitfield */

> +#define STD_KEYBOARD á á á á á á á á á á á á á 0x00000002
> +#define STD_MOUSE á á á á á á á á á á á á á á á0x00000004
> +#define MULTIMEDIA á á á á á á á á á á á á á á 0x00000008
> +#define POWER_KEYS á á á á á á á á á á á á á á 0x00000010
> +#define MEDIA_CENTER á á á á á á á á á á á á á 0x00000100
> +#define KBD_LEDS á á á á á á á á á á á á á á á 0x00004000
> +
> +struct dj_report {
> + á á á u8 report_id;
> + á á á u8 device_index;
> + á á á u8 report_type;
> + á á á u8 report_params[DJREPORT_SHORT_LENGTH - 3];
> +};
> +
> +struct dj_receiver_dev {
> + á á á struct hid_device *hdev;
> + á á á struct dj_device *paired_dj_devices[DJ_MAX_PAIRED_DEVICES];
> + á á á struct work_struct work;
> + á á á struct kfifo notif_fifo;
> + á á á spinlock_t lock;
> +};
> +
> +struct dj_device {
> + á á á struct hid_device *hdev;
> + á á á struct dj_receiver_dev *dj_receiver_dev;
> + á á á u32 reports_supported;
> + á á á u8 device_index;


> +};
> +
> +/**
> + * is_dj_device - know if the given dj_device is not the receiver.
> + * @dj_dev: the dj device to test
> + *
> + * This macro tests if a struct dj_device pointer is a device created
> + * by the bus enumarator.
> + */
> +#define is_dj_device(dj_dev) \

> + á á á (&(dj_dev)->dj_receiver_dev->hdev->dev == (dj_dev)->hdev->dev.parent)

Dmitry Torokhov

unread,
Sep 9, 2011, 2:30:02 PM9/9/11
to
On Thu, Sep 01, 2011 at 02:43:32PM +0200, Benjamin Tissoires wrote:
> > +
> > +       usbhid_submit_report(dj_rcv_hiddev, &report, USB_DIR_OUT);

>
> This is not working when the receiver is paired to a keyboard. When
> the session launches, Gnome try to send leds commands to the keyboard
> and this results in an oops.
>
> Using a pointer to the struct hid_report solves the crash.
>
> However, Dmitry, you told us about a DMA stack problem. Can you give
> us a better way to handle that?
>

Actually it is OK, usbhid_submit_report internally allocates DMA_safe
memory for raw report and copies the data for you.

Thanks.

--
Dmitry

Benjamin Tissoires

unread,
Sep 9, 2011, 3:20:02 PM9/9/11
to
Hi Dmitry,

On Fri, Sep 9, 2011 at 20:28, Dmitry Torokhov <dmitry....@gmail.com> wrote:
> On Thu, Sep 01, 2011 at 02:43:32PM +0200, Benjamin Tissoires wrote:
>> > +
>> > +       usbhid_submit_report(dj_rcv_hiddev, &report, USB_DIR_OUT);
>>
>> This is not working when the receiver is paired to a keyboard. When
>> the session launches, Gnome try to send leds commands to the keyboard
>> and this results in an oops.
>>
>> Using a  pointer to the struct hid_report solves the crash.
>>
>> However, Dmitry, you told us about a DMA stack problem. Can you give
>> us a better way to handle that?
>>
>
> Actually it is OK, usbhid_submit_report internally allocates DMA_safe
> memory for raw report and copies the data for you.

Ok, thanks. I'll resubmit the patch ASAP.

Cheers,
Benjamin

Benjamin Tissoires

unread,
Sep 10, 2011, 3:20:01 AM9/10/11
to
From: Nestor Lopez Casado <nlope...@logitech.com>

From: Nestor Lopez Casado <nlope...@logitech.com>

With this driver, all the devices paired to a single Unifying
receiver are exposed to user processes in separated /input/dev
nodes.

Keyboards with different layouts can be treated differently,
Multiplayer games on single PC (like home theater PC) can
differentiate input coming from different kbds paired to the
same receiver.

Up to now, when Logitech Unifying receivers are connected to a
Linux based system, a single keyboard and a single mouse are
presented to the HID Layer, even if the Unifying receiver can
pair up to six compatible devices. The Unifying receiver by default
multiplexes all incoming events (from multiple keyboards/mice)
into these two.

Signed-off-by: Nestor Lopez Casado <nlope...@logitech.com>

Signed-off-by: Benjamin Tissoires <benjamin....@gmail.com>
---

Hi guys,

this is the v4 of the bus enumerator patch.
Since the v3:
- it corrects a kernel oops when starting gnome-panel (due to
a hid_report not given as a pointer in logi_dj_ll_input_event).
- I also added the function logi_dj_reset_resume to re-enable dj mode after
coming from suspend to disk in case usb has been shut off.

Cheers,
Benjamin

drivers/hid/Kconfig | 9 +
drivers/hid/Makefile | 1 +
drivers/hid/hid-core.c | 2 +
drivers/hid/hid-ids.h | 2 +

drivers/hid/hid-logitech-dj.c | 913 +++++++++++++++++++++++++++++++++++++++++
drivers/hid/hid-logitech-dj.h | 123 ++++++
6 files changed, 1050 insertions(+), 0 deletions(-)
create mode 100644 drivers/hid/hid-logitech-dj.c
create mode 100644 drivers/hid/hid-logitech-dj.h

diff --git a/drivers/hid/Kconfig b/drivers/hid/Kconfig
index 36ca465..0eb7f04 100644
--- a/drivers/hid/Kconfig
+++ b/drivers/hid/Kconfig
@@ -231,6 +231,15 @@ config HID_LOGITECH

---help---


Support for Logitech devices that are not fully compliant with HID standard.

+config HID_LOGITECH_DJ

+ tristate "Logitech Unifying receivers full support"
+ depends on HID_LOGITECH
+ default m
+ ---help---

+ Say Y if you want support for Logitech Unifying receivers and devices.
+ Unifying receivers are capable of pairing up to 6 Logitech compliant
+ devices to the same receiver.
+


config LOGITECH_FF
bool "Logitech force feedback support"

depends on HID_LOGITECH
diff --git a/drivers/hid/Makefile b/drivers/hid/Makefile
index a24cee4..3aba5e7 100644
--- a/drivers/hid/Makefile
+++ b/drivers/hid/Makefile

@@ -44,6 +44,7 @@ obj-$(CONFIG_HID_KEYTOUCH) += hid-keytouch.o
obj-$(CONFIG_HID_KYE) += hid-kye.o
obj-$(CONFIG_HID_LCPOWER) += hid-lcpower.o
obj-$(CONFIG_HID_LOGITECH) += hid-logitech.o
+obj-$(CONFIG_HID_LOGITECH_DJ) += hid-logitech-dj.o
obj-$(CONFIG_HID_MAGICMOUSE) += hid-magicmouse.o
obj-$(CONFIG_HID_MICROSOFT) += hid-microsoft.o

obj-$(CONFIG_HID_MONTEREY) += hid-monterey.o


diff --git a/drivers/hid/hid-core.c b/drivers/hid/hid-core.c
index b0e618b..ebe4fdd 100644
--- a/drivers/hid/hid-core.c
+++ b/drivers/hid/hid-core.c
@@ -1423,6 +1423,8 @@ static const struct hid_device_id hid_have_special_driver[] = {

{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_DFP_WHEEL) },
{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_G25_WHEEL) },
{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_G27_WHEEL) },
+ { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_UNIFYING_RECEIVER) },
+ { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_UNIFYING_RECEIVER_2) },
{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_WII_WHEEL) },
{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_RUMBLEPAD2) },

{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_SPACETRAVELLER) },
diff --git a/drivers/hid/hid-ids.h b/drivers/hid/hid-ids.h
index a756ee6..c42764b 100644
--- a/drivers/hid/hid-ids.h
+++ b/drivers/hid/hid-ids.h
@@ -440,6 +440,8 @@

#define USB_DEVICE_ID_S510_RECEIVER_2 0xc517
#define USB_DEVICE_ID_LOGITECH_CORDLESS_DESKTOP_LX500 0xc512
#define USB_DEVICE_ID_MX3000_RECEIVER 0xc513
+#define USB_DEVICE_ID_LOGITECH_UNIFYING_RECEIVER 0xc52b
+#define USB_DEVICE_ID_LOGITECH_UNIFYING_RECEIVER_2 0xc532
#define USB_DEVICE_ID_SPACETRAVELLER 0xc623
#define USB_DEVICE_ID_SPACENAVIGATOR 0xc626

#define USB_DEVICE_ID_DINOVO_DESKTOP 0xc704


diff --git a/drivers/hid/hid-logitech-dj.c b/drivers/hid/hid-logitech-dj.c
new file mode 100644

index 0000000..3963a99
--- /dev/null
+++ b/drivers/hid/hid-logitech-dj.c
@@ -0,0 +1,913 @@
+/*
+ * HID driver for Logitech Unifying receivers
+ *
+ * Copyright (c) 2011 Logitech (c)


+ */
+
+/*
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of

+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the


+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ *
+ * Should you need to contact me, the author, you can do so by e-mail send
+ * your message to Nestor Lopez Casado <xnlopez at gmail com>
+ *
+ */
+
+
+#include <linux/device.h>
+#include <linux/hid.h>
+#include <linux/module.h>
+#include <linux/usb.h>
+#include "usbhid/usbhid.h"
+#include "hid-ids.h"
+#include "hid-logitech-dj.h"
+
+/* Keyboard descriptor (1) */
+static const char kbd_descriptor[] = {

+ 0xC0
+};
+
+/* Mouse descriptor (2) */


+static const char mse_descriptor[] = {

+};
+
+/* Consumer Control descriptor (3) */
+static const char consumer_descriptor[] = {

+ 0x05, 0x0C, /* USAGE_PAGE (Consumer Devices) */
+ 0x09, 0x01, /* USAGE (Consumer Control) */
+ 0xA1, 0x01, /* COLLECTION (Application) */
+ 0x85, 0x03, /* REPORT_ID = 3 */
+ 0x75, 0x10, /* REPORT_SIZE (16) */
+ 0x95, 0x02, /* REPORT_COUNT (2) */
+ 0x15, 0x01, /* LOGICAL_MIN (1) */
+ 0x26, 0x8C, 0x02, /* LOGICAL_MAX (652) */
+ 0x19, 0x01, /* USAGE_MIN (1) */
+ 0x2A, 0x8C, 0x02, /* USAGE_MAX (652) */
+ 0x81, 0x00, /* INPUT (Data Ary Abs) */
+ 0xC0, /* END_COLLECTION */

+}; /* */


+
+/* System control descriptor (4) */
+static const char syscontrol_descriptor[] = {

+ 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */
+ 0x09, 0x80, /* USAGE (System Control) */
+ 0xA1, 0x01, /* COLLECTION (Application) */
+ 0x85, 0x04, /* REPORT_ID = 4 */
+ 0x75, 0x02, /* REPORT_SIZE (2) */
+ 0x95, 0x01, /* REPORT_COUNT (1) */
+ 0x15, 0x01, /* LOGICAL_MIN (1) */
+ 0x25, 0x03, /* LOGICAL_MAX (3) */
+ 0x09, 0x82, /* USAGE (System Sleep) */
+ 0x09, 0x81, /* USAGE (System Power Down) */
+ 0x09, 0x83, /* USAGE (System Wake Up) */
+ 0x81, 0x60, /* INPUT (Data Ary Abs NPrf Null) */
+ 0x75, 0x06, /* REPORT_SIZE (6) */
+ 0x81, 0x03, /* INPUT (Cnst Var Abs) */
+ 0xC0, /* END_COLLECTION */

+};
+
+/* Media descriptor (8) */
+static const char media_descriptor[] = {

+ 0x06, 0xbc, 0xff, /* Usage Page 0xffbc */
+ 0x09, 0x88, /* Usage 0x0088 */
+ 0xa1, 0x01, /* BeginCollection */
+ 0x85, 0x08, /* Report ID 8 */
+ 0x19, 0x01, /* Usage Min 0x0001 */
+ 0x29, 0xff, /* Usage Max 0x00ff */
+ 0x15, 0x01, /* Logical Min 1 */
+ 0x26, 0xff, 0x00, /* Logical Max 255 */
+ 0x75, 0x08, /* Report Size 8 */
+ 0x95, 0x01, /* Report Count 1 */
+ 0x81, 0x00, /* Input */
+ 0xc0, /* EndCollection */

+}; /* */


+
+/* Maximum size of all defined hid reports in bytes (including report id) */
+#define MAX_REPORT_SIZE 8
+
+/* Number of possible hid report types that can be created by this driver.
+ *
+ * Right now, RF report types have the same report types (or report id's)
+ * than the hid report created from those RF reports. In the future
+ * this doesnt have to be true.
+ *
+ * For instance, RF report type 0x01 which has a size of 8 bytes, corresponds
+ * to hid report id 0x01, this is standard keyboard. Same thing applies to mice
+ * reports and consumer control, etc. If a new RF report is created, it doesn't
+ * has to have the same report id as its corresponding hid report, so an
+ * translation may have to take place for future report types.
+ */
+#define NUMBER_OF_HID_REPORTS 32
+static const u8 hid_reportid_size_map[NUMBER_OF_HID_REPORTS] = {

+ [1] = 8, /* Standard keyboard */
+ [2] = 8, /* Standard mouse */
+ [3] = 5, /* Consumer control */
+ [4] = 2, /* System control */

+ [8] = 2, /* Media Center */


+};
+
+
+#define LOGITECH_DJ_INTERFACE_NUMBER 0x02
+
+static struct hid_ll_driver logi_dj_ll_driver;
+
+static int logi_dj_output_hidraw_report(struct hid_device *hid, u8 * buf,

+ size_t count,
+ unsigned char report_type);


+
+static void logi_dj_recv_destroy_djhid_device(struct dj_receiver_dev *djrcv_dev,

+ struct dj_report *dj_report)
+{
+ /* Called in delayed work context */
+ struct dj_device *dj_dev;
+ unsigned long flags;
+
+ spin_lock_irqsave(&djrcv_dev->lock, flags);
+ dj_dev = djrcv_dev->paired_dj_devices[dj_report->device_index];
+ djrcv_dev->paired_dj_devices[dj_report->device_index] = NULL;
+ spin_unlock_irqrestore(&djrcv_dev->lock, flags);
+


+ if (dj_dev != NULL) {
+ hid_destroy_device(dj_dev->hdev);
+ kfree(dj_dev);
+ } else {

+ dev_err(&djrcv_dev->hdev->dev, "%s: can't destroy a NULL device\n",
+ __func__);
+ }
+}
+


+static void logi_dj_recv_add_djhid_device(struct dj_receiver_dev *djrcv_dev,

+ struct dj_report *dj_report)
+{


+ /* Called in delayed work context */
+ struct hid_device *djrcv_hdev = djrcv_dev->hdev;
+ struct usb_interface *intf = to_usb_interface(djrcv_hdev->dev.parent);
+ struct usb_device *usbdev = interface_to_usbdev(intf);
+ struct hid_device *dj_hiddev;
+ struct dj_device *dj_dev;

+ unsigned char tmpstr[20];
+


+ if (dj_report->report_params[DEVICE_PAIRED_PARAM_SPFUNCTION] &
+ SPFUNCTION_DEVICE_LIST_EMPTY) {
+ dbg_hid("%s: device list is empty\n", __func__);
+ return;
+ }
+
+ dj_hiddev = hid_allocate_device();
+ if (IS_ERR(dj_hiddev)) {

+ dev_err(&djrcv_hdev->dev, "%s: hid_allocate_device failed\n",
+ __func__);
+ return;
+ }
+
+ dj_hiddev->ll_driver = &logi_dj_ll_driver;
+ dj_hiddev->hid_output_raw_report = logi_dj_output_hidraw_report;
+
+ dj_hiddev->dev.parent = &djrcv_hdev->dev;
+ dj_hiddev->bus = BUS_USB;
+ dj_hiddev->vendor = le16_to_cpu(usbdev->descriptor.idVendor);
+ dj_hiddev->product = le16_to_cpu(usbdev->descriptor.idProduct);
+ snprintf(dj_hiddev->name, sizeof(dj_hiddev->name),
+ "Logitech Unifying Device. Wireless PID:%02x%02x",
+ dj_report->report_params[DEVICE_PAIRED_PARAM_EQUAD_ID_MSB],
+ dj_report->report_params[DEVICE_PAIRED_PARAM_EQUAD_ID_LSB]);
+
+ usb_make_path(usbdev, dj_hiddev->phys, sizeof(dj_hiddev->phys));
+ snprintf(tmpstr, sizeof(tmpstr), ":%d", dj_report->device_index);
+ strlcat(dj_hiddev->phys, tmpstr, sizeof(dj_hiddev->phys));
+
+ dj_dev = kzalloc(sizeof(struct dj_device), GFP_KERNEL);
+


+ if (!dj_dev) {
+ dev_err(&djrcv_hdev->dev, "%s: failed allocating dj_device\n",
+ __func__);
+ goto dj_device_allocate_fail;
+ }
+

+ dj_dev->reports_supported = le32_to_cpu(
+ dj_report->report_params[DEVICE_PAIRED_RF_REPORT_TYPE]);
+ dj_dev->hdev = dj_hiddev;
+ dj_dev->dj_receiver_dev = djrcv_dev;
+ dj_dev->device_index = dj_report->device_index;
+ dj_hiddev->driver_data = dj_dev;
+
+ djrcv_dev->paired_dj_devices[dj_report->device_index] = dj_dev;
+


+ if (hid_add_device(dj_hiddev)) {
+ dev_err(&djrcv_hdev->dev, "%s: failed adding dj_device\n",
+ __func__);
+ goto hid_add_device_fail;
+ }
+
+ return;
+
+hid_add_device_fail:

+ djrcv_dev->paired_dj_devices[dj_report->device_index] = NULL;
+ kfree(dj_dev);
+dj_device_allocate_fail:
+ hid_destroy_device(dj_hiddev);


+}
+
+static void delayedwork_callback(struct work_struct *work)
+{

+ struct dj_receiver_dev *djrcv_dev =
+ container_of(work, struct dj_receiver_dev, work);
+


+ struct dj_report dj_report;
+ unsigned long flags;
+ int count;
+

+ dbg_hid("%s\n", __func__);
+
+ spin_lock_irqsave(&djrcv_dev->lock, flags);
+


+ count = kfifo_out(&djrcv_dev->notif_fifo, &dj_report,
+ sizeof(struct dj_report));
+
+ if (count != sizeof(struct dj_report)) {
+ dev_err(&djrcv_dev->hdev->dev, "%s: workitem triggered without "
+ "notifications available\n", __func__);
+ spin_unlock_irqrestore(&djrcv_dev->lock, flags);
+ return;
+ }
+
+ if (!kfifo_is_empty(&djrcv_dev->notif_fifo)) {

+ if (schedule_work(&djrcv_dev->work) == 0) {
+ dbg_hid("%s: did not schedule the work item, was "
+ "already queued\n", __func__);
+ }
+ }
+
+ spin_unlock_irqrestore(&djrcv_dev->lock, flags);
+


+ switch (dj_report.report_type) {
+ case REPORT_TYPE_NOTIF_DEVICE_PAIRED:
+ logi_dj_recv_add_djhid_device(djrcv_dev, &dj_report);
+ break;
+ case REPORT_TYPE_NOTIF_DEVICE_UNPAIRED:
+ logi_dj_recv_destroy_djhid_device(djrcv_dev, &dj_report);
+ break;
+ default:

+ dbg_hid("%s: unexpected report type\n", __func__);
+ }


+}
+
+static void logi_dj_recv_queue_notification(struct dj_receiver_dev *djrcv_dev,

+ struct dj_report *dj_report)
+{
+ /* We are called from atomic context (tasklet && djrcv->lock held) */
+
+ kfifo_in(&djrcv_dev->notif_fifo, dj_report, sizeof(struct dj_report));
+
+ if (schedule_work(&djrcv_dev->work) == 0) {
+ dbg_hid("%s: did not schedule the work item, was already "
+ "queued\n", __func__);
+ }


+}
+
+static void logi_dj_recv_forward_null_report(struct dj_receiver_dev *djrcv_dev,

+ struct dj_report *dj_report)
+{


+ /* We are called from atomic context (tasklet && djrcv->lock held) */
+ unsigned int i;
+ u8 reportbuffer[MAX_REPORT_SIZE];

+ struct dj_device *djdev;
+
+ djdev = djrcv_dev->paired_dj_devices[dj_report->device_index];
+
+ if (!djdev) {
+ dbg_hid("djrcv_dev->paired_dj_devices[dj_report->device_index]"
+ " is NULL, index %d\n", dj_report->device_index);
+ return;
+ }
+
+ memset(reportbuffer, 0, sizeof(reportbuffer));
+


+ for (i = 0; i < NUMBER_OF_HID_REPORTS; i++) {
+ if (djdev->reports_supported & (1 << i)) {
+ reportbuffer[0] = i;
+ if (hid_input_report(djdev->hdev,
+ HID_INPUT_REPORT,
+ reportbuffer,
+ hid_reportid_size_map[i], 1)) {
+ dbg_hid("hid_input_report error sending null "
+ "report\n");
+ }
+ }

+ }
+}
+
+static void logi_dj_recv_forward_report(struct dj_receiver_dev *djrcv_dev,
+ struct dj_report *dj_report)
+{
+ /* We are called from atomic context (tasklet && djrcv->lock held) */
+ struct dj_device *dj_device;
+
+ dj_device = djrcv_dev->paired_dj_devices[dj_report->device_index];
+


+ if (dj_device == NULL) {
+ dbg_hid("djrcv_dev->paired_dj_devices[dj_report->device_index]"
+ " is NULL, index %d\n", dj_report->device_index);
+ return;
+ }
+
+ if ((dj_report->report_type > ARRAY_SIZE(hid_reportid_size_map) - 1) ||
+ (hid_reportid_size_map[dj_report->report_type] == 0)) {
+ dbg_hid("invalid report type:%x\n", dj_report->report_type);
+ return;
+ }
+
+ if (hid_input_report(dj_device->hdev,
+ HID_INPUT_REPORT, &dj_report->report_type,

+ hid_reportid_size_map[dj_report->report_type], 1)) {
+ dbg_hid("hid_input_report error\n");
+ }


+}
+
+
+static int logi_dj_recv_send_report(struct dj_receiver_dev *djrcv_dev,

+ struct dj_report *dj_report)
+{


+ struct hid_device *hdev = djrcv_dev->hdev;
+ int sent_bytes;
+
+ if (!hdev->hid_output_raw_report) {
+ dev_err(&hdev->dev, "%s:"
+ "hid_output_raw_report is null\n", __func__);
+ return -ENODEV;
+ }
+
+ sent_bytes = hdev->hid_output_raw_report(hdev, (u8 *) dj_report,
+ sizeof(struct dj_report),
+ HID_OUTPUT_REPORT);
+

+ return (sent_bytes < 0) ? sent_bytes : 0;


+}
+
+static int logi_dj_recv_query_paired_devices(struct dj_receiver_dev *djrcv_dev)
+{

+ struct dj_report dj_report;
+


+ memset(&dj_report, 0, sizeof(dj_report));
+ dj_report.report_id = REPORT_ID_DJ_SHORT;
+ dj_report.device_index = 0xFF;
+ dj_report.report_type = REPORT_TYPE_CMD_GET_PAIRED_DEVICES;

+ return logi_dj_recv_send_report(djrcv_dev, &dj_report);


+}
+
+static int logi_dj_recv_switch_to_dj_mode(struct dj_receiver_dev *djrcv_dev,

+ unsigned timeout)
+{
+ struct dj_report dj_report;
+


+ memset(&dj_report, 0, sizeof(dj_report));
+ dj_report.report_id = REPORT_ID_DJ_SHORT;
+ dj_report.device_index = 0xFF;
+ dj_report.report_type = REPORT_TYPE_CMD_SWITCH;

+ dj_report.report_params[CMD_SWITCH_PARAM_DEVBITFIELD] = 0x1F;
+ dj_report.report_params[CMD_SWITCH_PARAM_TIMEOUT_SECONDS] = (u8)timeout;
+ return logi_dj_recv_send_report(djrcv_dev, &dj_report);


+}
+
+
+static int logi_dj_ll_open(struct hid_device *hid)
+{

+ dbg_hid("%s:%s\n", __func__, hid->phys);
+ return 0;


+
+}
+
+static void logi_dj_ll_close(struct hid_device *hid)
+{

+ dbg_hid("%s:%s\n", __func__, hid->phys);


+}
+
+static int logi_dj_output_hidraw_report(struct hid_device *hid, u8 * buf,

+ size_t count,
+ unsigned char report_type)
+{
+ /* Called by hid raw to send data */
+ dbg_hid("%s\n", __func__);
+
+ return 0;


+}
+
+static int logi_dj_ll_parse(struct hid_device *hid)
+{

+ struct dj_device *djdev = hid->driver_data;
+ int retval;
+
+ dbg_hid("%s\n", __func__);
+
+ djdev->hdev->version = 0x0111;
+ djdev->hdev->country = 0x00;
+


+ if (djdev->reports_supported & STD_KEYBOARD) {
+ dbg_hid("%s: sending a kbd descriptor, reports_supported: %x\n",
+ __func__, djdev->reports_supported);
+ retval = hid_parse_report(hid,
+ (u8 *) kbd_descriptor,
+ sizeof(kbd_descriptor));
+ if (retval) {

+ dbg_hid("%s: sending a kbd descriptor, hid_parse failed"
+ " error: %d\n", __func__, retval);
+ return retval;
+ }
+ }
+


+ if (djdev->reports_supported & STD_MOUSE) {
+ dbg_hid("%s: sending a mouse descriptor, reports_supported: "
+ "%x\n", __func__, djdev->reports_supported);
+ retval = hid_parse_report(hid,
+ (u8 *) mse_descriptor,
+ sizeof(mse_descriptor));
+ if (retval) {

+ dbg_hid("%s: sending a mouse descriptor, hid_parse "
+ "failed error: %d\n", __func__, retval);
+ return retval;
+ }
+ }
+


+ if (djdev->reports_supported & MULTIMEDIA) {
+ dbg_hid("%s: sending a multimedia report descriptor: %x\n",
+ __func__, djdev->reports_supported);
+ retval = hid_parse_report(hid,
+ (u8 *) consumer_descriptor,
+ sizeof(consumer_descriptor));
+ if (retval) {

+ dbg_hid("%s: sending a consumer_descriptor, hid_parse "
+ "failed error: %d\n", __func__, retval);
+ return retval;
+ }
+ }
+


+ if (djdev->reports_supported & POWER_KEYS) {
+ dbg_hid("%s: sending a power keys report descriptor: %x\n",
+ __func__, djdev->reports_supported);
+ retval = hid_parse_report(hid,
+ (u8 *) syscontrol_descriptor,
+ sizeof(syscontrol_descriptor));
+ if (retval) {

+ dbg_hid("%s: sending a syscontrol_descriptor, "
+ "hid_parse failed error: %d\n",
+ __func__, retval);
+ return retval;
+ }
+ }
+


+ if (djdev->reports_supported & MEDIA_CENTER) {
+ dbg_hid("%s: sending a media center report descriptor: %x\n",
+ __func__, djdev->reports_supported);
+ retval = hid_parse_report(hid,
+ (u8 *) media_descriptor,
+ sizeof(media_descriptor));
+ if (retval) {

+ dbg_hid("%s: sending a media_descriptor, hid_parse "
+ "failed error: %d\n", __func__, retval);
+ return retval;
+ }
+ }
+


+ if (djdev->reports_supported & KBD_LEDS) {
+ dbg_hid("%s: need to send kbd leds report descriptor: %x\n",
+ __func__, djdev->reports_supported);
+ }
+

+ return 0;


+}
+
+static int logi_dj_ll_input_event(struct input_dev *dev, unsigned int type,

+ unsigned int code, int value)
+{
+ /* Sent by the input layer to handle leds and Force Feedback */
+ struct hid_device *dj_hiddev = input_get_drvdata(dev);
+ struct dj_device *dj_dev = dj_hiddev->driver_data;
+
+ struct dj_receiver_dev *djrcv_dev =
+ dev_get_drvdata(dj_hiddev->dev.parent);
+ struct hid_device *dj_rcv_hiddev = djrcv_dev->hdev;
+ struct hid_report_enum *output_report_enum;
+
+ struct hid_field *field;
+ struct hid_report *report;
+ unsigned char data[8];
+ int offset;
+
+ dbg_hid("%s: %s, type:%d | code:%d | value:%d\n",
+ __func__, dev->phys, type, code, value);
+
+ if (type != EV_LED)
+ return -1;
+
+ offset = hidinput_find_field(dj_hiddev, type, code, &field);
+


+ if (offset == -1) {
+ dev_warn(&dev->dev, "event field not found\n");
+ return -1;
+ }
+ hid_set_field(field, offset, value);

+ hid_output_report(field->report, &data[0]);
+
+ output_report_enum = &dj_rcv_hiddev->report_enum[HID_OUTPUT_REPORT];
+ report = output_report_enum->report_id_hash[REPORT_ID_DJ_SHORT];
+ hid_set_field(report->field[0], 0, dj_dev->device_index);
+ hid_set_field(report->field[0], 1, REPORT_TYPE_LEDS);
+ hid_set_field(report->field[0], 2, data[1]);
+


+ usbhid_submit_report(dj_rcv_hiddev, report, USB_DIR_OUT);
+

+ return 0;


+
+}
+
+static int logi_dj_ll_start(struct hid_device *hid)
+{

+ dbg_hid("%s\n", __func__);
+ return 0;


+}
+
+static void logi_dj_ll_stop(struct hid_device *hid)
+{

+ dbg_hid("%s\n", __func__);


+}
+
+
+static struct hid_ll_driver logi_dj_ll_driver = {

+ .parse = logi_dj_ll_parse,
+ .start = logi_dj_ll_start,
+ .stop = logi_dj_ll_stop,
+ .open = logi_dj_ll_open,
+ .close = logi_dj_ll_close,

+ .hidinput_input_event = logi_dj_ll_input_event,


+};
+
+
+static int logi_dj_raw_event(struct hid_device *hdev,

+ struct hid_report *report, u8 *data,
+ int size)
+{
+ struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
+ struct dj_report *dj_report = (struct dj_report *) data;
+ unsigned long flags;
+ bool report_processed = false;
+
+ dbg_hid("%s, size:%d\n", __func__, size);
+

+ spin_unlock_irqrestore(&djrcv_dev->lock, flags);
+
+ return report_processed;


+}
+
+static int logi_dj_probe(struct hid_device *hdev,

+ const struct hid_device_id *id)
+{


+ struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
+ struct dj_receiver_dev *djrcv_dev;
+ int retval;
+

+ if (is_dj_device((struct dj_device *)hdev->driver_data))
+ return -ENODEV;
+
+ dbg_hid("%s called for ifnum %d\n", __func__,
+ intf->cur_altsetting->desc.bInterfaceNumber);
+


+ /* Ignore interfaces 0 and 1, they will not carry any data, dont create
+ * any hid_device for them */
+ if (intf->cur_altsetting->desc.bInterfaceNumber !=
+ LOGITECH_DJ_INTERFACE_NUMBER) {
+ dbg_hid("%s: ignoring ifnum %d\n", __func__,
+ intf->cur_altsetting->desc.bInterfaceNumber);
+ return -ENODEV;
+ }
+

+ /* Treat interface 2 */
+

+ /* Starts the usb device and connects to upper interfaces hiddev and
+ * hidraw */
+ retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT);


+ if (retval) {
+ dev_err(&hdev->dev,

+ return retval;
+
+logi_dj_recv_query_paired_devices_failed:
+ hdev->ll_driver->close(hdev);
+
+llopen_failed:
+switch_to_dj_mode_fail:
+ hid_hw_stop(hdev);
+
+hid_hw_start_fail:
+hid_parse_fail:


+ kfifo_free(&djrcv_dev->notif_fifo);
+ kfree(djrcv_dev);
+ hid_set_drvdata(hdev, NULL);

+ return retval;
+
+}
+
+#ifdef CONFIG_PM
+static int logi_dj_reset_resume(struct hid_device *hdev)
+{
+ int retval;
+ struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
+


+ retval = logi_dj_recv_switch_to_dj_mode(djrcv_dev, 0);
+ if (retval < 0) {
+ dev_err(&hdev->dev,

+ "%s:logi_dj_recv_switch_to_dj_mode returned error:%d\n",
+ __func__, retval);


+ }
+
+ return 0;
+}

+#endif


+
+static void logi_dj_remove(struct hid_device *hdev)
+{

+ struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
+ struct dj_device *dj_dev;
+ int i;
+

+ dbg_hid("%s\n", __func__);
+


+ cancel_work_sync(&djrcv_dev->work);
+
+ hdev->ll_driver->close(hdev);
+ hid_hw_stop(hdev);
+
+ /* I suppose that at this point the only context that can access
+ * the djrecv_data is this thread as the work item is guaranteed to
+ * have finished and no more raw_event callbacks should arrive after
+ * the remove callback was triggered so no locks are put around the
+ * code below */

+ for (i = 0; i < DJ_MAX_PAIRED_DEVICES; i++) {
+ dj_dev = djrcv_dev->paired_dj_devices[i];


+ if (dj_dev != NULL) {
+ hid_destroy_device(dj_dev->hdev);
+ kfree(dj_dev);

+ djrcv_dev->paired_dj_devices[i] = NULL;
+ }


+ }
+
+ kfifo_free(&djrcv_dev->notif_fifo);
+ kfree(djrcv_dev);

+ hid_set_drvdata(hdev, NULL);


+}
+
+static int logi_djdevice_probe(struct hid_device *hdev,

+ const struct hid_device_id *id)
+{
+ int ret;
+ struct dj_device *dj_dev = hdev->driver_data;
+


+ if (!is_dj_device(dj_dev))
+ return -ENODEV;
+
+ ret = hid_parse(hdev);
+ if (!ret)

+ ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
+
+ return ret;


+}
+
+static const struct hid_device_id logi_dj_receivers[] = {

+ {HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
+ USB_DEVICE_ID_LOGITECH_UNIFYING_RECEIVER)},
+ {HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
+ USB_DEVICE_ID_LOGITECH_UNIFYING_RECEIVER_2)},
+ {}

+};
+
+MODULE_DEVICE_TABLE(hid, logi_dj_receivers);
+
+static struct hid_driver logi_djreceiver_driver = {

+ .name = "logitech-djreceiver",
+ .id_table = logi_dj_receivers,
+ .probe = logi_dj_probe,
+ .remove = logi_dj_remove,

+ .raw_event = logi_dj_raw_event,
+#ifdef CONFIG_PM
+ .reset_resume = logi_dj_reset_resume,
+#endif


+};
+
+
+static const struct hid_device_id logi_dj_devices[] = {

+ {HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
+ USB_DEVICE_ID_LOGITECH_UNIFYING_RECEIVER)},
+ {HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
+ USB_DEVICE_ID_LOGITECH_UNIFYING_RECEIVER_2)},

+ {}
+};
+
+static struct hid_driver logi_djdevice_driver = {
+ .name = "logitech-djdevice",
+ .id_table = logi_dj_devices,
+ .probe = logi_djdevice_probe,


+};
+
+
+static int __init logi_dj_init(void)
+{

+ int retval;
+
+ dbg_hid("Logitech-DJ:%s\n", __func__);
+


+ retval = hid_register_driver(&logi_djreceiver_driver);
+ if (retval)
+ return retval;
+
+ retval = hid_register_driver(&logi_djdevice_driver);
+ if (retval)
+ hid_unregister_driver(&logi_djreceiver_driver);
+

+ return retval;


+
+}
+
+static void __exit logi_dj_exit(void)
+{

+ dbg_hid("Logitech-DJ:%s\n", __func__);
+
+ hid_unregister_driver(&logi_djdevice_driver);
+ hid_unregister_driver(&logi_djreceiver_driver);


+
+}
+
+module_init(logi_dj_init);
+module_exit(logi_dj_exit);
+MODULE_LICENSE("GPL");
+MODULE_AUTHOR("Logitech");
+MODULE_AUTHOR("Nestor Lopez Casado");
diff --git a/drivers/hid/hid-logitech-dj.h b/drivers/hid/hid-logitech-dj.h
new file mode 100644
index 0000000..8dcdccd
--- /dev/null
+++ b/drivers/hid/hid-logitech-dj.h
@@ -0,0 +1,123 @@
+#ifndef __HID_LOGITECH_DJ_H
+#define __HID_LOGITECH_DJ_H
+
+/*

+ * HID driver for Logitech Unifying receivers
+ *
+ * Copyright (c) 2011 Logitech (c)


+ */
+
+/*
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of

+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the


+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ *
+ * Should you need to contact me, the author, you can do so by e-mail send
+ * your message to Nestor Lopez Casado <xnlopez at gmail com>
+ *
+ */
+
+#include <linux/kfifo.h>
+

+#define DJ_MAX_PAIRED_DEVICES 6
+#define DJ_MAX_NUMBER_NOTIFICATIONS 8
+
+#define DJREPORT_SHORT_LENGTH 15
+#define DJREPORT_LONG_LENGTH 32
+
+#define REPORT_ID_DJ_SHORT 0x20
+#define REPORT_ID_DJ_LONG 0x21
+
+#define REPORT_TYPE_RFREPORT_FIRST 0x01
+#define REPORT_TYPE_RFREPORT_LAST 0x1F

+
+/* Command Switch to DJ mode */

+#define REPORT_TYPE_CMD_SWITCH 0x80
+#define CMD_SWITCH_PARAM_DEVBITFIELD 0x00