File Index
Files and directories
Sign in
FILE

hid-logitech-dj.c

File preview with syntax highlighting.

linux-6.19.14/drivers/hid/hid-logitech-dj.c C
   1// SPDX-License-Identifier: GPL-2.0-only
   2/*
   3 *  HID driver for Logitech receivers
   4 *
   5 *  Copyright (c) 2011 Logitech
   6 */
   7
   8
   9
  10#include <linux/device.h>
  11#include <linux/hid.h>
  12#include <linux/module.h>
  13#include <linux/kfifo.h>
  14#include <linux/delay.h>
  15#include <linux/usb.h> /* For to_usb_interface for kvm extra intf check */
  16#include <linux/unaligned.h>
  17#include "hid-ids.h"
  18
  19#define DJ_MAX_PAIRED_DEVICES			7
  20#define DJ_MAX_NUMBER_NOTIFS			8
  21#define DJ_RECEIVER_INDEX			0
  22#define DJ_DEVICE_INDEX_MIN			1
  23#define DJ_DEVICE_INDEX_MAX			7
  24
  25#define DJREPORT_SHORT_LENGTH			15
  26#define DJREPORT_LONG_LENGTH			32
  27
  28#define REPORT_ID_DJ_SHORT			0x20
  29#define REPORT_ID_DJ_LONG			0x21
  30
  31#define REPORT_ID_HIDPP_SHORT			0x10
  32#define REPORT_ID_HIDPP_LONG			0x11
  33#define REPORT_ID_HIDPP_VERY_LONG		0x12
  34
  35#define HIDPP_REPORT_SHORT_LENGTH		7
  36#define HIDPP_REPORT_LONG_LENGTH		20
  37
  38#define HIDPP_RECEIVER_INDEX			0xff
  39
  40#define REPORT_TYPE_RFREPORT_FIRST		0x01
  41#define REPORT_TYPE_RFREPORT_LAST		0x1F
  42
  43/* Command Switch to DJ mode */
  44#define REPORT_TYPE_CMD_SWITCH			0x80
  45#define CMD_SWITCH_PARAM_DEVBITFIELD		0x00
  46#define CMD_SWITCH_PARAM_TIMEOUT_SECONDS	0x01
  47#define TIMEOUT_NO_KEEPALIVE			0x00
  48
  49/* Command to Get the list of Paired devices */
  50#define REPORT_TYPE_CMD_GET_PAIRED_DEVICES	0x81
  51
  52/* Device Paired Notification */
  53#define REPORT_TYPE_NOTIF_DEVICE_PAIRED		0x41
  54#define SPFUNCTION_MORE_NOTIF_EXPECTED		0x01
  55#define SPFUNCTION_DEVICE_LIST_EMPTY		0x02
  56#define DEVICE_PAIRED_PARAM_SPFUNCTION		0x00
  57#define DEVICE_PAIRED_PARAM_EQUAD_ID_LSB	0x01
  58#define DEVICE_PAIRED_PARAM_EQUAD_ID_MSB	0x02
  59#define DEVICE_PAIRED_RF_REPORT_TYPE		0x03
  60
  61/* Device Un-Paired Notification */
  62#define REPORT_TYPE_NOTIF_DEVICE_UNPAIRED	0x40
  63
  64/* Connection Status Notification */
  65#define REPORT_TYPE_NOTIF_CONNECTION_STATUS	0x42
  66#define CONNECTION_STATUS_PARAM_STATUS		0x00
  67#define STATUS_LINKLOSS				0x01
  68
  69/* Error Notification */
  70#define REPORT_TYPE_NOTIF_ERROR			0x7F
  71#define NOTIF_ERROR_PARAM_ETYPE			0x00
  72#define ETYPE_KEEPALIVE_TIMEOUT			0x01
  73
  74/* supported DJ HID && RF report types */
  75#define REPORT_TYPE_KEYBOARD			0x01
  76#define REPORT_TYPE_MOUSE			0x02
  77#define REPORT_TYPE_CONSUMER_CONTROL		0x03
  78#define REPORT_TYPE_SYSTEM_CONTROL		0x04
  79#define REPORT_TYPE_MEDIA_CENTER		0x08
  80#define REPORT_TYPE_LEDS			0x0E
  81
  82/* RF Report types bitfield */
  83#define STD_KEYBOARD				BIT(1)
  84#define STD_MOUSE				BIT(2)
  85#define MULTIMEDIA				BIT(3)
  86#define POWER_KEYS				BIT(4)
  87#define KBD_MOUSE				BIT(5)
  88#define MEDIA_CENTER				BIT(8)
  89#define KBD_LEDS				BIT(14)
  90/* Fake (bitnr > NUMBER_OF_HID_REPORTS) bit to track HID++ capability */
  91#define HIDPP					BIT_ULL(63)
  92
  93/* HID++ Device Connected Notification */
  94#define REPORT_TYPE_NOTIF_DEVICE_CONNECTED	0x41
  95#define HIDPP_PARAM_PROTO_TYPE			0x00
  96#define HIDPP_PARAM_DEVICE_INFO			0x01
  97#define HIDPP_PARAM_EQUAD_LSB			0x02
  98#define HIDPP_PARAM_EQUAD_MSB			0x03
  99#define HIDPP_PARAM_27MHZ_DEVID			0x03
 100#define HIDPP_DEVICE_TYPE_MASK			GENMASK(3, 0)
 101#define HIDPP_LINK_STATUS_MASK			BIT(6)
 102#define HIDPP_MANUFACTURER_MASK			BIT(7)
 103#define HIDPP_27MHZ_SECURE_MASK			BIT(7)
 104
 105#define HIDPP_DEVICE_TYPE_KEYBOARD		1
 106#define HIDPP_DEVICE_TYPE_MOUSE			2
 107
 108#define HIDPP_SET_REGISTER			0x80
 109#define HIDPP_GET_LONG_REGISTER			0x83
 110#define HIDPP_REG_CONNECTION_STATE		0x02
 111#define HIDPP_REG_PAIRING_INFORMATION		0xB5
 112#define HIDPP_PAIRING_INFORMATION		0x20
 113#define HIDPP_FAKE_DEVICE_ARRIVAL		0x02
 114
 115enum recvr_type {
 116	recvr_type_dj,
 117	recvr_type_hidpp,
 118	recvr_type_gaming_hidpp,
 119	recvr_type_gaming_hidpp_ls_1_3,
 120	recvr_type_mouse_only,
 121	recvr_type_27mhz,
 122	recvr_type_bluetooth,
 123	recvr_type_dinovo,
 124};
 125
 126struct dj_report {
 127	u8 report_id;
 128	u8 device_index;
 129	u8 report_type;
 130	u8 report_params[DJREPORT_SHORT_LENGTH - 3];
 131};
 132
 133struct hidpp_event {
 134	u8 report_id;
 135	u8 device_index;
 136	u8 sub_id;
 137	u8 params[HIDPP_REPORT_LONG_LENGTH - 3U];
 138} __packed;
 139
 140struct dj_receiver_dev {
 141	struct hid_device *mouse;
 142	struct hid_device *keyboard;
 143	struct hid_device *hidpp;
 144	struct dj_device *paired_dj_devices[DJ_MAX_PAIRED_DEVICES +
 145					    DJ_DEVICE_INDEX_MIN];
 146	struct list_head list;
 147	struct kref kref;
 148	struct work_struct work;
 149	struct kfifo notif_fifo;
 150	unsigned long last_query; /* in jiffies */
 151	bool ready;
 152	bool dj_mode;
 153	enum recvr_type type;
 154	unsigned int unnumbered_application;
 155	spinlock_t lock;
 156};
 157
 158struct dj_device {
 159	struct hid_device *hdev;
 160	struct dj_receiver_dev *dj_receiver_dev;
 161	u64 reports_supported;
 162	u8 device_index;
 163};
 164
 165#define WORKITEM_TYPE_EMPTY	0
 166#define WORKITEM_TYPE_PAIRED	1
 167#define WORKITEM_TYPE_UNPAIRED	2
 168#define WORKITEM_TYPE_UNKNOWN	255
 169
 170struct dj_workitem {
 171	u8 type;		/* WORKITEM_TYPE_* */
 172	u8 device_index;
 173	u8 device_type;
 174	u8 quad_id_msb;
 175	u8 quad_id_lsb;
 176	u64 reports_supported;
 177};
 178
 179/* Keyboard descriptor (1) */
 180static const char kbd_descriptor[] = {
 181	0x05, 0x01,		/* USAGE_PAGE (generic Desktop)     */
 182	0x09, 0x06,		/* USAGE (Keyboard)         */
 183	0xA1, 0x01,		/* COLLECTION (Application)     */
 184	0x85, 0x01,		/* REPORT_ID (1)            */
 185	0x95, 0x08,		/*   REPORT_COUNT (8)           */
 186	0x75, 0x01,		/*   REPORT_SIZE (1)            */
 187	0x15, 0x00,		/*   LOGICAL_MINIMUM (0)        */
 188	0x25, 0x01,		/*   LOGICAL_MAXIMUM (1)        */
 189	0x05, 0x07,		/*   USAGE_PAGE (Keyboard)      */
 190	0x19, 0xE0,		/*   USAGE_MINIMUM (Left Control)   */
 191	0x29, 0xE7,		/*   USAGE_MAXIMUM (Right GUI)      */
 192	0x81, 0x02,		/*   INPUT (Data,Var,Abs)       */
 193	0x95, 0x06,		/*   REPORT_COUNT (6)           */
 194	0x75, 0x08,		/*   REPORT_SIZE (8)            */
 195	0x15, 0x00,		/*   LOGICAL_MINIMUM (0)        */
 196	0x26, 0xFF, 0x00,	/*   LOGICAL_MAXIMUM (255)      */
 197	0x05, 0x07,		/*   USAGE_PAGE (Keyboard)      */
 198	0x19, 0x00,		/*   USAGE_MINIMUM (no event)       */
 199	0x2A, 0xFF, 0x00,	/*   USAGE_MAXIMUM (reserved)       */
 200	0x81, 0x00,		/*   INPUT (Data,Ary,Abs)       */
 201	0x85, 0x0e,		/* REPORT_ID (14)               */
 202	0x05, 0x08,		/*   USAGE PAGE (LED page)      */
 203	0x95, 0x05,		/*   REPORT COUNT (5)           */
 204	0x75, 0x01,		/*   REPORT SIZE (1)            */
 205	0x15, 0x00,		/*   LOGICAL_MINIMUM (0)        */
 206	0x25, 0x01,		/*   LOGICAL_MAXIMUM (1)        */
 207	0x19, 0x01,		/*   USAGE MINIMUM (1)          */
 208	0x29, 0x05,		/*   USAGE MAXIMUM (5)          */
 209	0x91, 0x02,		/*   OUTPUT (Data, Variable, Absolute)  */
 210	0x95, 0x01,		/*   REPORT COUNT (1)           */
 211	0x75, 0x03,		/*   REPORT SIZE (3)            */
 212	0x91, 0x01,		/*   OUTPUT (Constant)          */
 213	0xC0
 214};
 215
 216/* Gaming Keyboard descriptor (1) */
 217static const char kbd_lightspeed_1_3_descriptor[] = {
 218	0x05, 0x01,		/* Usage Page (Generic Desktop) */
 219	0x09, 0x06,		/* Usage (Keyboard)             */
 220	0xA1, 0x01,		/* Collection (Application)     */
 221	0x85, 0x01,		/*   Report ID (1)              */
 222	0x05, 0x07,		/*   Usage Page (Kbrd/Keypad)   */
 223	0x19, 0xE0,		/*   Usage Minimum (0xE0)       */
 224	0x29, 0xE7,		/*   Usage Maximum (0xE7)       */
 225	0x15, 0x00,		/*   Logical Minimum (0)        */
 226	0x25, 0x01,		/*   Logical Maximum (1)        */
 227	0x75, 0x01,		/*   Report Size (1)            */
 228	0x95, 0x08,		/*   Report Count (8)           */
 229	0x81, 0x02,		/*   Input (Data,Var)           */
 230	0x95, 0x70,		/*   Report Count (112)         */
 231	0x19, 0x04,		/*   Usage Minimum (0x04)       */
 232	0x29, 0x73,		/*   Usage Maximum (0x73)       */
 233	0x81, 0x02,		/*   Input (Data,Var,Abs)       */
 234	0x95, 0x05,		/*   Report Count (5)           */
 235	0x19, 0x87,		/*   Usage Minimum (0x87)       */
 236	0x29, 0x8B,		/*   Usage Maximum (0x8B)       */
 237	0x81, 0x02,		/*   Input (Data,Var,Abs)       */
 238	0x95, 0x03,		/*   Report Count (3)           */
 239	0x19, 0x90,		/*   Usage Minimum (0x90)       */
 240	0x29, 0x92,		/*   Usage Maximum (0x92)       */
 241	0x81, 0x02,		/*   Input (Data,Var,Abs)       */
 242	0x95, 0x05,		/*   Report Count (5)           */
 243	0x85, 0x0E,		/*   Report ID (14)             */
 244	0x05, 0x08,		/*   Usage Page (LEDs)          */
 245	0x19, 0x01,		/*   Usage Minimum (Num Lock)   */
 246	0x29, 0x05,		/*   Usage Maximum (Kana)       */
 247	0x91, 0x02,		/*   Output (Data,Var,Abs)      */
 248	0x95, 0x01,		/*   Report Count (1)           */
 249	0x75, 0x03,		/*   Report Size (3)            */
 250	0x91, 0x03,		/*   Output (Const,Var,Abs)     */
 251	0xC0,			/* End Collection               */
 252};
 253
 254/* Mouse descriptor (2)     */
 255static const char mse_descriptor[] = {
 256	0x05, 0x01,		/*  USAGE_PAGE (Generic Desktop)        */
 257	0x09, 0x02,		/*  USAGE (Mouse)                       */
 258	0xA1, 0x01,		/*  COLLECTION (Application)            */
 259	0x85, 0x02,		/*    REPORT_ID = 2                     */
 260	0x09, 0x01,		/*    USAGE (pointer)                   */
 261	0xA1, 0x00,		/*    COLLECTION (physical)             */
 262	0x05, 0x09,		/*      USAGE_PAGE (buttons)            */
 263	0x19, 0x01,		/*      USAGE_MIN (1)                   */
 264	0x29, 0x10,		/*      USAGE_MAX (16)                  */
 265	0x15, 0x00,		/*      LOGICAL_MIN (0)                 */
 266	0x25, 0x01,		/*      LOGICAL_MAX (1)                 */
 267	0x95, 0x10,		/*      REPORT_COUNT (16)               */
 268	0x75, 0x01,		/*      REPORT_SIZE (1)                 */
 269	0x81, 0x02,		/*      INPUT (data var abs)            */
 270	0x05, 0x01,		/*      USAGE_PAGE (generic desktop)    */
 271	0x16, 0x01, 0xF8,	/*      LOGICAL_MIN (-2047)             */
 272	0x26, 0xFF, 0x07,	/*      LOGICAL_MAX (2047)              */
 273	0x75, 0x0C,		/*      REPORT_SIZE (12)                */
 274	0x95, 0x02,		/*      REPORT_COUNT (2)                */
 275	0x09, 0x30,		/*      USAGE (X)                       */
 276	0x09, 0x31,		/*      USAGE (Y)                       */
 277	0x81, 0x06,		/*      INPUT                           */
 278	0x15, 0x81,		/*      LOGICAL_MIN (-127)              */
 279	0x25, 0x7F,		/*      LOGICAL_MAX (127)               */
 280	0x75, 0x08,		/*      REPORT_SIZE (8)                 */
 281	0x95, 0x01,		/*      REPORT_COUNT (1)                */
 282	0x09, 0x38,		/*      USAGE (wheel)                   */
 283	0x81, 0x06,		/*      INPUT                           */
 284	0x05, 0x0C,		/*      USAGE_PAGE(consumer)            */
 285	0x0A, 0x38, 0x02,	/*      USAGE(AC Pan)                   */
 286	0x95, 0x01,		/*      REPORT_COUNT (1)                */
 287	0x81, 0x06,		/*      INPUT                           */
 288	0xC0,			/*    END_COLLECTION                    */
 289	0xC0,			/*  END_COLLECTION                      */
 290};
 291
 292/* Mouse descriptor (2) for 27 MHz receiver, only 8 buttons */
 293static const char mse_27mhz_descriptor[] = {
 294	0x05, 0x01,		/*  USAGE_PAGE (Generic Desktop)        */
 295	0x09, 0x02,		/*  USAGE (Mouse)                       */
 296	0xA1, 0x01,		/*  COLLECTION (Application)            */
 297	0x85, 0x02,		/*    REPORT_ID = 2                     */
 298	0x09, 0x01,		/*    USAGE (pointer)                   */
 299	0xA1, 0x00,		/*    COLLECTION (physical)             */
 300	0x05, 0x09,		/*      USAGE_PAGE (buttons)            */
 301	0x19, 0x01,		/*      USAGE_MIN (1)                   */
 302	0x29, 0x08,		/*      USAGE_MAX (8)                   */
 303	0x15, 0x00,		/*      LOGICAL_MIN (0)                 */
 304	0x25, 0x01,		/*      LOGICAL_MAX (1)                 */
 305	0x95, 0x08,		/*      REPORT_COUNT (8)                */
 306	0x75, 0x01,		/*      REPORT_SIZE (1)                 */
 307	0x81, 0x02,		/*      INPUT (data var abs)            */
 308	0x05, 0x01,		/*      USAGE_PAGE (generic desktop)    */
 309	0x16, 0x01, 0xF8,	/*      LOGICAL_MIN (-2047)             */
 310	0x26, 0xFF, 0x07,	/*      LOGICAL_MAX (2047)              */
 311	0x75, 0x0C,		/*      REPORT_SIZE (12)                */
 312	0x95, 0x02,		/*      REPORT_COUNT (2)                */
 313	0x09, 0x30,		/*      USAGE (X)                       */
 314	0x09, 0x31,		/*      USAGE (Y)                       */
 315	0x81, 0x06,		/*      INPUT                           */
 316	0x15, 0x81,		/*      LOGICAL_MIN (-127)              */
 317	0x25, 0x7F,		/*      LOGICAL_MAX (127)               */
 318	0x75, 0x08,		/*      REPORT_SIZE (8)                 */
 319	0x95, 0x01,		/*      REPORT_COUNT (1)                */
 320	0x09, 0x38,		/*      USAGE (wheel)                   */
 321	0x81, 0x06,		/*      INPUT                           */
 322	0x05, 0x0C,		/*      USAGE_PAGE(consumer)            */
 323	0x0A, 0x38, 0x02,	/*      USAGE(AC Pan)                   */
 324	0x95, 0x01,		/*      REPORT_COUNT (1)                */
 325	0x81, 0x06,		/*      INPUT                           */
 326	0xC0,			/*    END_COLLECTION                    */
 327	0xC0,			/*  END_COLLECTION                      */
 328};
 329
 330/* Mouse descriptor (2) for Bluetooth receiver, low-res hwheel, 12 buttons */
 331static const char mse_bluetooth_descriptor[] = {
 332	0x05, 0x01,		/*  USAGE_PAGE (Generic Desktop)        */
 333	0x09, 0x02,		/*  USAGE (Mouse)                       */
 334	0xA1, 0x01,		/*  COLLECTION (Application)            */
 335	0x85, 0x02,		/*    REPORT_ID = 2                     */
 336	0x09, 0x01,		/*    USAGE (pointer)                   */
 337	0xA1, 0x00,		/*    COLLECTION (physical)             */
 338	0x05, 0x09,		/*      USAGE_PAGE (buttons)            */
 339	0x19, 0x01,		/*      USAGE_MIN (1)                   */
 340	0x29, 0x08,		/*      USAGE_MAX (8)                   */
 341	0x15, 0x00,		/*      LOGICAL_MIN (0)                 */
 342	0x25, 0x01,		/*      LOGICAL_MAX (1)                 */
 343	0x95, 0x08,		/*      REPORT_COUNT (8)                */
 344	0x75, 0x01,		/*      REPORT_SIZE (1)                 */
 345	0x81, 0x02,		/*      INPUT (data var abs)            */
 346	0x05, 0x01,		/*      USAGE_PAGE (generic desktop)    */
 347	0x16, 0x01, 0xF8,	/*      LOGICAL_MIN (-2047)             */
 348	0x26, 0xFF, 0x07,	/*      LOGICAL_MAX (2047)              */
 349	0x75, 0x0C,		/*      REPORT_SIZE (12)                */
 350	0x95, 0x02,		/*      REPORT_COUNT (2)                */
 351	0x09, 0x30,		/*      USAGE (X)                       */
 352	0x09, 0x31,		/*      USAGE (Y)                       */
 353	0x81, 0x06,		/*      INPUT                           */
 354	0x15, 0x81,		/*      LOGICAL_MIN (-127)              */
 355	0x25, 0x7F,		/*      LOGICAL_MAX (127)               */
 356	0x75, 0x08,		/*      REPORT_SIZE (8)                 */
 357	0x95, 0x01,		/*      REPORT_COUNT (1)                */
 358	0x09, 0x38,		/*      USAGE (wheel)                   */
 359	0x81, 0x06,		/*      INPUT                           */
 360	0x05, 0x0C,		/*      USAGE_PAGE(consumer)            */
 361	0x0A, 0x38, 0x02,	/*      USAGE(AC Pan)                   */
 362	0x15, 0xF9,		/*      LOGICAL_MIN (-7)                */
 363	0x25, 0x07,		/*      LOGICAL_MAX (7)                 */
 364	0x75, 0x04,		/*      REPORT_SIZE (4)                 */
 365	0x95, 0x01,		/*      REPORT_COUNT (1)                */
 366	0x81, 0x06,		/*      INPUT                           */
 367	0x05, 0x09,		/*      USAGE_PAGE (buttons)            */
 368	0x19, 0x09,		/*      USAGE_MIN (9)                   */
 369	0x29, 0x0C,		/*      USAGE_MAX (12)                  */
 370	0x15, 0x00,		/*      LOGICAL_MIN (0)                 */
 371	0x25, 0x01,		/*      LOGICAL_MAX (1)                 */
 372	0x75, 0x01,		/*      REPORT_SIZE (1)                 */
 373	0x95, 0x04,		/*      REPORT_COUNT (4)                */
 374	0x81, 0x02,		/*      INPUT (Data,Var,Abs)            */
 375	0xC0,			/*    END_COLLECTION                    */
 376	0xC0,			/*  END_COLLECTION                      */
 377};
 378
 379/* Mouse descriptor (5) for Bluetooth receiver, normal-res hwheel, 8 buttons */
 380static const char mse5_bluetooth_descriptor[] = {
 381	0x05, 0x01,		/*  USAGE_PAGE (Generic Desktop)        */
 382	0x09, 0x02,		/*  Usage (Mouse)                       */
 383	0xa1, 0x01,		/*  Collection (Application)            */
 384	0x85, 0x05,		/*   Report ID (5)                      */
 385	0x09, 0x01,		/*   Usage (Pointer)                    */
 386	0xa1, 0x00,		/*   Collection (Physical)              */
 387	0x05, 0x09,		/*    Usage Page (Button)               */
 388	0x19, 0x01,		/*    Usage Minimum (1)                 */
 389	0x29, 0x08,		/*    Usage Maximum (8)                 */
 390	0x15, 0x00,		/*    Logical Minimum (0)               */
 391	0x25, 0x01,		/*    Logical Maximum (1)               */
 392	0x95, 0x08,		/*    Report Count (8)                  */
 393	0x75, 0x01,		/*    Report Size (1)                   */
 394	0x81, 0x02,		/*    Input (Data,Var,Abs)              */
 395	0x05, 0x01,		/*    Usage Page (Generic Desktop)      */
 396	0x16, 0x01, 0xf8,	/*    Logical Minimum (-2047)           */
 397	0x26, 0xff, 0x07,	/*    Logical Maximum (2047)            */
 398	0x75, 0x0c,		/*    Report Size (12)                  */
 399	0x95, 0x02,		/*    Report Count (2)                  */
 400	0x09, 0x30,		/*    Usage (X)                         */
 401	0x09, 0x31,		/*    Usage (Y)                         */
 402	0x81, 0x06,		/*    Input (Data,Var,Rel)              */
 403	0x15, 0x81,		/*    Logical Minimum (-127)            */
 404	0x25, 0x7f,		/*    Logical Maximum (127)             */
 405	0x75, 0x08,		/*    Report Size (8)                   */
 406	0x95, 0x01,		/*    Report Count (1)                  */
 407	0x09, 0x38,		/*    Usage (Wheel)                     */
 408	0x81, 0x06,		/*    Input (Data,Var,Rel)              */
 409	0x05, 0x0c,		/*    Usage Page (Consumer Devices)     */
 410	0x0a, 0x38, 0x02,	/*    Usage (AC Pan)                    */
 411	0x15, 0x81,		/*    Logical Minimum (-127)            */
 412	0x25, 0x7f,		/*    Logical Maximum (127)             */
 413	0x75, 0x08,		/*    Report Size (8)                   */
 414	0x95, 0x01,		/*    Report Count (1)                  */
 415	0x81, 0x06,		/*    Input (Data,Var,Rel)              */
 416	0xc0,			/*   End Collection                     */
 417	0xc0,			/*  End Collection                      */
 418};
 419
 420/* Gaming Mouse descriptor (2) */
 421static const char mse_high_res_descriptor[] = {
 422	0x05, 0x01,		/*  USAGE_PAGE (Generic Desktop)        */
 423	0x09, 0x02,		/*  USAGE (Mouse)                       */
 424	0xA1, 0x01,		/*  COLLECTION (Application)            */
 425	0x85, 0x02,		/*    REPORT_ID = 2                     */
 426	0x09, 0x01,		/*    USAGE (pointer)                   */
 427	0xA1, 0x00,		/*    COLLECTION (physical)             */
 428	0x05, 0x09,		/*      USAGE_PAGE (buttons)            */
 429	0x19, 0x01,		/*      USAGE_MIN (1)                   */
 430	0x29, 0x10,		/*      USAGE_MAX (16)                  */
 431	0x15, 0x00,		/*      LOGICAL_MIN (0)                 */
 432	0x25, 0x01,		/*      LOGICAL_MAX (1)                 */
 433	0x95, 0x10,		/*      REPORT_COUNT (16)               */
 434	0x75, 0x01,		/*      REPORT_SIZE (1)                 */
 435	0x81, 0x02,		/*      INPUT (data var abs)            */
 436	0x05, 0x01,		/*      USAGE_PAGE (generic desktop)    */
 437	0x16, 0x01, 0x80,	/*      LOGICAL_MIN (-32767)            */
 438	0x26, 0xFF, 0x7F,	/*      LOGICAL_MAX (32767)             */
 439	0x75, 0x10,		/*      REPORT_SIZE (16)                */
 440	0x95, 0x02,		/*      REPORT_COUNT (2)                */
 441	0x09, 0x30,		/*      USAGE (X)                       */
 442	0x09, 0x31,		/*      USAGE (Y)                       */
 443	0x81, 0x06,		/*      INPUT                           */
 444	0x15, 0x81,		/*      LOGICAL_MIN (-127)              */
 445	0x25, 0x7F,		/*      LOGICAL_MAX (127)               */
 446	0x75, 0x08,		/*      REPORT_SIZE (8)                 */
 447	0x95, 0x01,		/*      REPORT_COUNT (1)                */
 448	0x09, 0x38,		/*      USAGE (wheel)                   */
 449	0x81, 0x06,		/*      INPUT                           */
 450	0x05, 0x0C,		/*      USAGE_PAGE(consumer)            */
 451	0x0A, 0x38, 0x02,	/*      USAGE(AC Pan)                   */
 452	0x95, 0x01,		/*      REPORT_COUNT (1)                */
 453	0x81, 0x06,		/*      INPUT                           */
 454	0xC0,			/*    END_COLLECTION                    */
 455	0xC0,			/*  END_COLLECTION                      */
 456};
 457
 458/* Gaming Mouse descriptor with vendor data (2) */
 459static const char mse_high_res_ls_1_3_descriptor[] = {
 460	0x05, 0x01,		/* Usage Page (Generic Desktop)         */
 461	0x09, 0x02,		/* Usage (Mouse)                        */
 462	0xA1, 0x01,		/* Collection (Application)             */
 463	0x85, 0x02,		/*   Report ID (2)                      */
 464	0x09, 0x01,		/*   Usage (Pointer)                    */
 465	0xA1, 0x00,		/*   Collection (Physical)              */
 466	0x95, 0x10,		/*     Report Count (16)                */
 467	0x75, 0x01,		/*     Report Size (1)                  */
 468	0x15, 0x00,		/*     Logical Minimum (0)              */
 469	0x25, 0x01,		/*     Logical Maximum (1)              */
 470	0x05, 0x09,		/*     Usage Page (Button)              */
 471	0x19, 0x01,		/*     Usage Minimum (0x01)             */
 472	0x29, 0x10,		/*     Usage Maximum (0x10)             */
 473	0x81, 0x02,		/*     Input (Data,Var,Abs)             */
 474	0x95, 0x02,		/*     Report Count (2)                 */
 475	0x75, 0x10,		/*     Report Size (16)                 */
 476	0x16, 0x01, 0x80,	/*     Logical Minimum (-32767)         */
 477	0x26, 0xFF, 0x7F,	/*     Logical Maximum (32767)          */
 478	0x05, 0x01,		/*     Usage Page (Generic Desktop)     */
 479	0x09, 0x30,		/*     Usage (X)                        */
 480	0x09, 0x31,		/*     Usage (Y)                        */
 481	0x81, 0x06,		/*     Input (Data,Var,Rel)             */
 482	0x95, 0x01,		/*     Report Count (1)                 */
 483	0x75, 0x08,		/*     Report Size (8)                  */
 484	0x15, 0x81,		/*     Logical Minimum (-127)           */
 485	0x25, 0x7F,		/*     Logical Maximum (127)            */
 486	0x09, 0x38,		/*     Usage (Wheel)                    */
 487	0x81, 0x06,		/*     Input (Data,Var,Rel)             */
 488	0x95, 0x01,		/*     Report Count (1)                 */
 489	0x05, 0x0C,		/*     Usage Page (Consumer)            */
 490	0x0A, 0x38, 0x02,	/*     Usage (AC Pan)                   */
 491	0x81, 0x06,		/*     Input (Data,Var,Rel)             */
 492	0xC0,			/*   End Collection                     */
 493	0x06, 0x00, 0xFF,	/*   Usage Page (Vendor Defined 0xFF00) */
 494	0x09, 0xF1,		/*   Usage (0xF1)                       */
 495	0x75, 0x08,		/*   Report Size (8)                    */
 496	0x95, 0x05,		/*   Report Count (5)                   */
 497	0x15, 0x00,		/*   Logical Minimum (0)                */
 498	0x26, 0xFF, 0x00,	/*   Logical Maximum (255)              */
 499	0x81, 0x00,		/*   Input (Data,Array,Abs)             */
 500	0xC0,			/* End Collection                       */
 501};
 502
 503/* Consumer Control descriptor (3) */
 504static const char consumer_descriptor[] = {
 505	0x05, 0x0C,		/* USAGE_PAGE (Consumer Devices)       */
 506	0x09, 0x01,		/* USAGE (Consumer Control)            */
 507	0xA1, 0x01,		/* COLLECTION (Application)            */
 508	0x85, 0x03,		/* REPORT_ID = 3                       */
 509	0x75, 0x10,		/* REPORT_SIZE (16)                    */
 510	0x95, 0x02,		/* REPORT_COUNT (2)                    */
 511	0x15, 0x01,		/* LOGICAL_MIN (1)                     */
 512	0x26, 0xFF, 0x02,	/* LOGICAL_MAX (767)                   */
 513	0x19, 0x01,		/* USAGE_MIN (1)                       */
 514	0x2A, 0xFF, 0x02,	/* USAGE_MAX (767)                     */
 515	0x81, 0x00,		/* INPUT (Data Ary Abs)                */
 516	0xC0,			/* END_COLLECTION                      */
 517};				/*                                     */
 518
 519/* System control descriptor (4) */
 520static const char syscontrol_descriptor[] = {
 521	0x05, 0x01,		/*   USAGE_PAGE (Generic Desktop)      */
 522	0x09, 0x80,		/*   USAGE (System Control)            */
 523	0xA1, 0x01,		/*   COLLECTION (Application)          */
 524	0x85, 0x04,		/*   REPORT_ID = 4                     */
 525	0x75, 0x02,		/*   REPORT_SIZE (2)                   */
 526	0x95, 0x01,		/*   REPORT_COUNT (1)                  */
 527	0x15, 0x01,		/*   LOGICAL_MIN (1)                   */
 528	0x25, 0x03,		/*   LOGICAL_MAX (3)                   */
 529	0x09, 0x82,		/*   USAGE (System Sleep)              */
 530	0x09, 0x81,		/*   USAGE (System Power Down)         */
 531	0x09, 0x83,		/*   USAGE (System Wake Up)            */
 532	0x81, 0x60,		/*   INPUT (Data Ary Abs NPrf Null)    */
 533	0x75, 0x06,		/*   REPORT_SIZE (6)                   */
 534	0x81, 0x03,		/*   INPUT (Cnst Var Abs)              */
 535	0xC0,			/*   END_COLLECTION                    */
 536};
 537
 538/* Media descriptor (8) */
 539static const char media_descriptor[] = {
 540	0x06, 0xbc, 0xff,	/* Usage Page 0xffbc                   */
 541	0x09, 0x88,		/* Usage 0x0088                        */
 542	0xa1, 0x01,		/* BeginCollection                     */
 543	0x85, 0x08,		/*   Report ID 8                       */
 544	0x19, 0x01,		/*   Usage Min 0x0001                  */
 545	0x29, 0xff,		/*   Usage Max 0x00ff                  */
 546	0x15, 0x01,		/*   Logical Min 1                     */
 547	0x26, 0xff, 0x00,	/*   Logical Max 255                   */
 548	0x75, 0x08,		/*   Report Size 8                     */
 549	0x95, 0x01,		/*   Report Count 1                    */
 550	0x81, 0x00,		/*   Input                             */
 551	0xc0,			/* EndCollection                       */
 552};				/*                                     */
 553
 554/* HIDPP descriptor */
 555static const char hidpp_descriptor[] = {
 556	0x06, 0x00, 0xff,	/* Usage Page (Vendor Defined Page 1)  */
 557	0x09, 0x01,		/* Usage (Vendor Usage 1)              */
 558	0xa1, 0x01,		/* Collection (Application)            */
 559	0x85, 0x10,		/*   Report ID (16)                    */
 560	0x75, 0x08,		/*   Report Size (8)                   */
 561	0x95, 0x06,		/*   Report Count (6)                  */
 562	0x15, 0x00,		/*   Logical Minimum (0)               */
 563	0x26, 0xff, 0x00,	/*   Logical Maximum (255)             */
 564	0x09, 0x01,		/*   Usage (Vendor Usage 1)            */
 565	0x81, 0x00,		/*   Input (Data,Arr,Abs)              */
 566	0x09, 0x01,		/*   Usage (Vendor Usage 1)            */
 567	0x91, 0x00,		/*   Output (Data,Arr,Abs)             */
 568	0xc0,			/* End Collection                      */
 569	0x06, 0x00, 0xff,	/* Usage Page (Vendor Defined Page 1)  */
 570	0x09, 0x02,		/* Usage (Vendor Usage 2)              */
 571	0xa1, 0x01,		/* Collection (Application)            */
 572	0x85, 0x11,		/*   Report ID (17)                    */
 573	0x75, 0x08,		/*   Report Size (8)                   */
 574	0x95, 0x13,		/*   Report Count (19)                 */
 575	0x15, 0x00,		/*   Logical Minimum (0)               */
 576	0x26, 0xff, 0x00,	/*   Logical Maximum (255)             */
 577	0x09, 0x02,		/*   Usage (Vendor Usage 2)            */
 578	0x81, 0x00,		/*   Input (Data,Arr,Abs)              */
 579	0x09, 0x02,		/*   Usage (Vendor Usage 2)            */
 580	0x91, 0x00,		/*   Output (Data,Arr,Abs)             */
 581	0xc0,			/* End Collection                      */
 582	0x06, 0x00, 0xff,	/* Usage Page (Vendor Defined Page 1)  */
 583	0x09, 0x04,		/* Usage (Vendor Usage 0x04)           */
 584	0xa1, 0x01,		/* Collection (Application)            */
 585	0x85, 0x20,		/*   Report ID (32)                    */
 586	0x75, 0x08,		/*   Report Size (8)                   */
 587	0x95, 0x0e,		/*   Report Count (14)                 */
 588	0x15, 0x00,		/*   Logical Minimum (0)               */
 589	0x26, 0xff, 0x00,	/*   Logical Maximum (255)             */
 590	0x09, 0x41,		/*   Usage (Vendor Usage 0x41)         */
 591	0x81, 0x00,		/*   Input (Data,Arr,Abs)              */
 592	0x09, 0x41,		/*   Usage (Vendor Usage 0x41)         */
 593	0x91, 0x00,		/*   Output (Data,Arr,Abs)             */
 594	0x85, 0x21,		/*   Report ID (33)                    */
 595	0x95, 0x1f,		/*   Report Count (31)                 */
 596	0x15, 0x00,		/*   Logical Minimum (0)               */
 597	0x26, 0xff, 0x00,	/*   Logical Maximum (255)             */
 598	0x09, 0x42,		/*   Usage (Vendor Usage 0x42)         */
 599	0x81, 0x00,		/*   Input (Data,Arr,Abs)              */
 600	0x09, 0x42,		/*   Usage (Vendor Usage 0x42)         */
 601	0x91, 0x00,		/*   Output (Data,Arr,Abs)             */
 602	0xc0,			/* End Collection                      */
 603};
 604
 605/* Maximum size of all defined hid reports in bytes (including report id) */
 606#define MAX_REPORT_SIZE 8
 607
 608/* Make sure the largest of each descriptor type is present here */
 609#define MAX_RDESC_SIZE				\
 610	(sizeof(kbd_lightspeed_1_3_descriptor) +\
 611	 sizeof(mse_bluetooth_descriptor) +	\
 612	 sizeof(mse5_bluetooth_descriptor) +	\
 613	 sizeof(consumer_descriptor) +		\
 614	 sizeof(syscontrol_descriptor) +	\
 615	 sizeof(media_descriptor) +	\
 616	 sizeof(hidpp_descriptor))
 617
 618/* Number of possible hid report types that can be created by this driver.
 619 *
 620 * Right now, RF report types have the same report types (or report id's)
 621 * than the hid report created from those RF reports. In the future
 622 * this doesnt have to be true.
 623 *
 624 * For instance, RF report type 0x01 which has a size of 8 bytes, corresponds
 625 * to hid report id 0x01, this is standard keyboard. Same thing applies to mice
 626 * reports and consumer control, etc. If a new RF report is created, it doesn't
 627 * has to have the same report id as its corresponding hid report, so an
 628 * translation may have to take place for future report types.
 629 */
 630#define NUMBER_OF_HID_REPORTS 32
 631static const u8 hid_reportid_size_map[NUMBER_OF_HID_REPORTS] = {
 632	[1] = 8,		/* Standard keyboard */
 633	[2] = 8,		/* Standard mouse */
 634	[3] = 5,		/* Consumer control */
 635	[4] = 2,		/* System control */
 636	[8] = 2,		/* Media Center */
 637};
 638
 639
 640#define LOGITECH_DJ_INTERFACE_NUMBER 0x02
 641
 642static const struct hid_ll_driver logi_dj_ll_driver;
 643
 644static int logi_dj_recv_query_paired_devices(struct dj_receiver_dev *djrcv_dev);
 645static int logi_dj_recv_switch_to_dj_mode(struct dj_receiver_dev *djrcv_dev,
 646					  unsigned int timeout);
 647static void delayedwork_callback(struct work_struct *work);
 648
 649static LIST_HEAD(dj_hdev_list);
 650static DEFINE_MUTEX(dj_hdev_list_lock);
 651
 652static bool recvr_type_is_bluetooth(enum recvr_type type)
 653{
 654	return type == recvr_type_bluetooth || type == recvr_type_dinovo;
 655}
 656
 657/*
 658 * dj/HID++ receivers are really a single logical entity, but for BIOS/Windows
 659 * compatibility they have multiple USB interfaces. On HID++ receivers we need
 660 * to listen for input reports on both interfaces. The functions below are used
 661 * to create a single struct dj_receiver_dev for all interfaces belonging to
 662 * a single USB-device / receiver.
 663 */
 664static struct dj_receiver_dev *dj_find_receiver_dev(struct hid_device *hdev,
 665						    enum recvr_type type)
 666{
 667	struct dj_receiver_dev *djrcv_dev;
 668	char sep;
 669
 670	/*
 671	 * The bluetooth receiver contains a built-in hub and has separate
 672	 * USB-devices for the keyboard and mouse interfaces.
 673	 */
 674	sep = recvr_type_is_bluetooth(type) ? '.' : '/';
 675
 676	/* Try to find an already-probed interface from the same device */
 677	list_for_each_entry(djrcv_dev, &dj_hdev_list, list) {
 678		if (djrcv_dev->mouse &&
 679		    hid_compare_device_paths(hdev, djrcv_dev->mouse, sep)) {
 680			kref_get(&djrcv_dev->kref);
 681			return djrcv_dev;
 682		}
 683		if (djrcv_dev->keyboard &&
 684		    hid_compare_device_paths(hdev, djrcv_dev->keyboard, sep)) {
 685			kref_get(&djrcv_dev->kref);
 686			return djrcv_dev;
 687		}
 688		if (djrcv_dev->hidpp &&
 689		    hid_compare_device_paths(hdev, djrcv_dev->hidpp, sep)) {
 690			kref_get(&djrcv_dev->kref);
 691			return djrcv_dev;
 692		}
 693	}
 694
 695	return NULL;
 696}
 697
 698static void dj_release_receiver_dev(struct kref *kref)
 699{
 700	struct dj_receiver_dev *djrcv_dev = container_of(kref, struct dj_receiver_dev, kref);
 701
 702	list_del(&djrcv_dev->list);
 703	kfifo_free(&djrcv_dev->notif_fifo);
 704	kfree(djrcv_dev);
 705}
 706
 707static void dj_put_receiver_dev(struct hid_device *hdev)
 708{
 709	struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
 710
 711	mutex_lock(&dj_hdev_list_lock);
 712
 713	if (djrcv_dev->mouse == hdev)
 714		djrcv_dev->mouse = NULL;
 715	if (djrcv_dev->keyboard == hdev)
 716		djrcv_dev->keyboard = NULL;
 717	if (djrcv_dev->hidpp == hdev)
 718		djrcv_dev->hidpp = NULL;
 719
 720	kref_put(&djrcv_dev->kref, dj_release_receiver_dev);
 721
 722	mutex_unlock(&dj_hdev_list_lock);
 723}
 724
 725static struct dj_receiver_dev *dj_get_receiver_dev(struct hid_device *hdev,
 726						   enum recvr_type type,
 727						   unsigned int application,
 728						   bool is_hidpp)
 729{
 730	struct dj_receiver_dev *djrcv_dev;
 731
 732	mutex_lock(&dj_hdev_list_lock);
 733
 734	djrcv_dev = dj_find_receiver_dev(hdev, type);
 735	if (!djrcv_dev) {
 736		djrcv_dev = kzalloc(sizeof(*djrcv_dev), GFP_KERNEL);
 737		if (!djrcv_dev)
 738			goto out;
 739
 740		INIT_WORK(&djrcv_dev->work, delayedwork_callback);
 741		spin_lock_init(&djrcv_dev->lock);
 742		if (kfifo_alloc(&djrcv_dev->notif_fifo,
 743			    DJ_MAX_NUMBER_NOTIFS * sizeof(struct dj_workitem),
 744			    GFP_KERNEL)) {
 745			kfree(djrcv_dev);
 746			djrcv_dev = NULL;
 747			goto out;
 748		}
 749		kref_init(&djrcv_dev->kref);
 750		list_add_tail(&djrcv_dev->list, &dj_hdev_list);
 751		djrcv_dev->last_query = jiffies;
 752		djrcv_dev->type = type;
 753	}
 754
 755	if (application == HID_GD_KEYBOARD)
 756		djrcv_dev->keyboard = hdev;
 757	if (application == HID_GD_MOUSE)
 758		djrcv_dev->mouse = hdev;
 759	if (is_hidpp)
 760		djrcv_dev->hidpp = hdev;
 761
 762	hid_set_drvdata(hdev, djrcv_dev);
 763out:
 764	mutex_unlock(&dj_hdev_list_lock);
 765	return djrcv_dev;
 766}
 767
 768static void logi_dj_recv_destroy_djhid_device(struct dj_receiver_dev *djrcv_dev,
 769					      struct dj_workitem *workitem)
 770{
 771	/* Called in delayed work context */
 772	struct dj_device *dj_dev;
 773	unsigned long flags;
 774
 775	spin_lock_irqsave(&djrcv_dev->lock, flags);
 776	dj_dev = djrcv_dev->paired_dj_devices[workitem->device_index];
 777	djrcv_dev->paired_dj_devices[workitem->device_index] = NULL;
 778	spin_unlock_irqrestore(&djrcv_dev->lock, flags);
 779
 780	if (dj_dev != NULL) {
 781		hid_destroy_device(dj_dev->hdev);
 782		kfree(dj_dev);
 783	} else {
 784		hid_err(djrcv_dev->hidpp, "%s: can't destroy a NULL device\n",
 785			__func__);
 786	}
 787}
 788
 789static void logi_dj_recv_add_djhid_device(struct dj_receiver_dev *djrcv_dev,
 790					  struct dj_workitem *workitem)
 791{
 792	/* Called in delayed work context */
 793	struct hid_device *djrcv_hdev = djrcv_dev->hidpp;
 794	struct hid_device *dj_hiddev;
 795	struct dj_device *dj_dev;
 796	u8 device_index = workitem->device_index;
 797	unsigned long flags;
 798
 799	/* Device index goes from 1 to 6, we need 3 bytes to store the
 800	 * semicolon, the index, and a null terminator
 801	 */
 802	unsigned char tmpstr[3];
 803
 804	/* We are the only one ever adding a device, no need to lock */
 805	if (djrcv_dev->paired_dj_devices[device_index]) {
 806		/* The device is already known. No need to reallocate it. */
 807		dbg_hid("%s: device is already known\n", __func__);
 808		return;
 809	}
 810
 811	dj_hiddev = hid_allocate_device();
 812	if (IS_ERR(dj_hiddev)) {
 813		hid_err(djrcv_hdev, "%s: hid_allocate_dev failed\n", __func__);
 814		return;
 815	}
 816
 817	dj_hiddev->ll_driver = &logi_dj_ll_driver;
 818
 819	dj_hiddev->dev.parent = &djrcv_hdev->dev;
 820	dj_hiddev->bus = BUS_USB;
 821	dj_hiddev->vendor = djrcv_hdev->vendor;
 822	dj_hiddev->product = (workitem->quad_id_msb << 8) |
 823			      workitem->quad_id_lsb;
 824	if (workitem->device_type) {
 825		const char *type_str = "Device";
 826
 827		switch (workitem->device_type) {
 828		case 0x01: type_str = "Keyboard";	break;
 829		case 0x02: type_str = "Mouse";		break;
 830		case 0x03: type_str = "Numpad";		break;
 831		case 0x04: type_str = "Presenter";	break;
 832		case 0x07: type_str = "Remote Control";	break;
 833		case 0x08: type_str = "Trackball";	break;
 834		case 0x09: type_str = "Touchpad";	break;
 835		}
 836		snprintf(dj_hiddev->name, sizeof(dj_hiddev->name),
 837			"Logitech Wireless %s PID:%04x",
 838			type_str, dj_hiddev->product);
 839	} else {
 840		snprintf(dj_hiddev->name, sizeof(dj_hiddev->name),
 841			"Logitech Wireless Device PID:%04x",
 842			dj_hiddev->product);
 843	}
 844
 845	if (djrcv_dev->type == recvr_type_27mhz)
 846		dj_hiddev->group = HID_GROUP_LOGITECH_27MHZ_DEVICE;
 847	else
 848		dj_hiddev->group = HID_GROUP_LOGITECH_DJ_DEVICE;
 849
 850	memcpy(dj_hiddev->phys, djrcv_hdev->phys, sizeof(djrcv_hdev->phys));
 851	snprintf(tmpstr, sizeof(tmpstr), ":%d", device_index);
 852	strlcat(dj_hiddev->phys, tmpstr, sizeof(dj_hiddev->phys));
 853
 854	dj_dev = kzalloc(sizeof(struct dj_device), GFP_KERNEL);
 855
 856	if (!dj_dev) {
 857		hid_err(djrcv_hdev, "%s: failed allocating dj_dev\n", __func__);
 858		goto dj_device_allocate_fail;
 859	}
 860
 861	dj_dev->reports_supported = workitem->reports_supported;
 862	dj_dev->hdev = dj_hiddev;
 863	dj_dev->dj_receiver_dev = djrcv_dev;
 864	dj_dev->device_index = device_index;
 865	dj_hiddev->driver_data = dj_dev;
 866
 867	spin_lock_irqsave(&djrcv_dev->lock, flags);
 868	djrcv_dev->paired_dj_devices[device_index] = dj_dev;
 869	spin_unlock_irqrestore(&djrcv_dev->lock, flags);
 870
 871	if (hid_add_device(dj_hiddev)) {
 872		hid_err(djrcv_hdev, "%s: failed adding dj_device\n", __func__);
 873		goto hid_add_device_fail;
 874	}
 875
 876	return;
 877
 878hid_add_device_fail:
 879	spin_lock_irqsave(&djrcv_dev->lock, flags);
 880	djrcv_dev->paired_dj_devices[device_index] = NULL;
 881	spin_unlock_irqrestore(&djrcv_dev->lock, flags);
 882	kfree(dj_dev);
 883dj_device_allocate_fail:
 884	hid_destroy_device(dj_hiddev);
 885}
 886
 887static void delayedwork_callback(struct work_struct *work)
 888{
 889	struct dj_receiver_dev *djrcv_dev =
 890		container_of(work, struct dj_receiver_dev, work);
 891
 892	struct dj_workitem workitem;
 893	unsigned long flags;
 894	int count;
 895
 896	dbg_hid("%s\n", __func__);
 897
 898	spin_lock_irqsave(&djrcv_dev->lock, flags);
 899
 900	/*
 901	 * Since we attach to multiple interfaces, we may get scheduled before
 902	 * we are bound to the HID++ interface, catch this.
 903	 */
 904	if (!djrcv_dev->ready) {
 905		pr_warn("%s: delayedwork queued before hidpp interface was enumerated\n",
 906			__func__);
 907		spin_unlock_irqrestore(&djrcv_dev->lock, flags);
 908		return;
 909	}
 910
 911	count = kfifo_out(&djrcv_dev->notif_fifo, &workitem, sizeof(workitem));
 912
 913	if (count != sizeof(workitem)) {
 914		spin_unlock_irqrestore(&djrcv_dev->lock, flags);
 915		return;
 916	}
 917
 918	if (!kfifo_is_empty(&djrcv_dev->notif_fifo))
 919		schedule_work(&djrcv_dev->work);
 920
 921	spin_unlock_irqrestore(&djrcv_dev->lock, flags);
 922
 923	switch (workitem.type) {
 924	case WORKITEM_TYPE_PAIRED:
 925		logi_dj_recv_add_djhid_device(djrcv_dev, &workitem);
 926		break;
 927	case WORKITEM_TYPE_UNPAIRED:
 928		logi_dj_recv_destroy_djhid_device(djrcv_dev, &workitem);
 929		break;
 930	case WORKITEM_TYPE_UNKNOWN:
 931		if (!djrcv_dev->dj_mode)
 932			logi_dj_recv_switch_to_dj_mode(djrcv_dev, 0);
 933
 934		logi_dj_recv_query_paired_devices(djrcv_dev);
 935		break;
 936	case WORKITEM_TYPE_EMPTY:
 937		dbg_hid("%s: device list is empty\n", __func__);
 938		break;
 939	}
 940}
 941
 942/*
 943 * Sometimes we receive reports for which we do not have a paired dj_device
 944 * associated with the device_index or report-type to forward the report to.
 945 * This means that the original "device paired" notification corresponding
 946 * to the dj_device never arrived to this driver. Possible reasons for this are:
 947 * 1) hid-core discards all packets coming from a device during probe().
 948 * 2) if the receiver is plugged into a KVM switch then the pairing reports
 949 * are only forwarded to it if the focus is on this PC.
 950 * This function deals with this by re-asking the receiver for the list of
 951 * connected devices in the delayed work callback.
 952 * This function MUST be called with djrcv->lock held.
 953 */
 954static void logi_dj_recv_queue_unknown_work(struct dj_receiver_dev *djrcv_dev)
 955{
 956	struct dj_workitem workitem = { .type = WORKITEM_TYPE_UNKNOWN };
 957
 958	/* Rate limit queries done because of unhandled reports to 2/sec */
 959	if (time_before(jiffies, djrcv_dev->last_query + HZ / 2))
 960		return;
 961
 962	kfifo_in(&djrcv_dev->notif_fifo, &workitem, sizeof(workitem));
 963	schedule_work(&djrcv_dev->work);
 964}
 965
 966static void logi_dj_recv_queue_notification(struct dj_receiver_dev *djrcv_dev,
 967					   struct dj_report *dj_report)
 968{
 969	/* We are called from atomic context (tasklet && djrcv->lock held) */
 970	struct dj_workitem workitem = {
 971		.device_index = dj_report->device_index,
 972	};
 973
 974	switch (dj_report->report_type) {
 975	case REPORT_TYPE_NOTIF_DEVICE_PAIRED:
 976		workitem.type = WORKITEM_TYPE_PAIRED;
 977		if (dj_report->report_params[DEVICE_PAIRED_PARAM_SPFUNCTION] &
 978		    SPFUNCTION_DEVICE_LIST_EMPTY) {
 979			workitem.type = WORKITEM_TYPE_EMPTY;
 980			break;
 981		}
 982		fallthrough;
 983	case REPORT_TYPE_NOTIF_DEVICE_UNPAIRED:
 984		workitem.quad_id_msb =
 985			dj_report->report_params[DEVICE_PAIRED_PARAM_EQUAD_ID_MSB];
 986		workitem.quad_id_lsb =
 987			dj_report->report_params[DEVICE_PAIRED_PARAM_EQUAD_ID_LSB];
 988		workitem.reports_supported = get_unaligned_le32(
 989						dj_report->report_params +
 990						DEVICE_PAIRED_RF_REPORT_TYPE);
 991		workitem.reports_supported |= HIDPP;
 992		if (dj_report->report_type == REPORT_TYPE_NOTIF_DEVICE_UNPAIRED)
 993			workitem.type = WORKITEM_TYPE_UNPAIRED;
 994		break;
 995	default:
 996		logi_dj_recv_queue_unknown_work(djrcv_dev);
 997		return;
 998	}
 999
1000	kfifo_in(&djrcv_dev->notif_fifo, &workitem, sizeof(workitem));
1001	schedule_work(&djrcv_dev->work);
1002}
1003
1004/*
1005 * Some quad/bluetooth keyboards have a builtin touchpad in this case we see
1006 * only 1 paired device with a device_type of REPORT_TYPE_KEYBOARD. For the
1007 * touchpad to work we must also forward mouse input reports to the dj_hiddev
1008 * created for the keyboard (instead of forwarding them to a second paired
1009 * device with a device_type of REPORT_TYPE_MOUSE as we normally would).
1010 *
1011 * On Dinovo receivers the keyboard's touchpad and an optional paired actual
1012 * mouse send separate input reports, INPUT(2) aka STD_MOUSE for the mouse
1013 * and INPUT(5) aka KBD_MOUSE for the keyboard's touchpad.
1014 *
1015 * On MX5x00 receivers (which can also be paired with a Dinovo keyboard)
1016 * INPUT(2) is used for both an optional paired actual mouse and for the
1017 * keyboard's touchpad.
1018 */
1019static const u16 kbd_builtin_touchpad_ids[] = {
1020	0xb309, /* Dinovo Edge */
1021	0xb30c, /* Dinovo Mini */
1022};
1023
1024static void logi_hidpp_dev_conn_notif_equad(struct hid_device *hdev,
1025					    struct hidpp_event *hidpp_report,
1026					    struct dj_workitem *workitem)
1027{
1028	struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
1029	int i, id;
1030
1031	workitem->type = WORKITEM_TYPE_PAIRED;
1032	workitem->device_type = hidpp_report->params[HIDPP_PARAM_DEVICE_INFO] &
1033				HIDPP_DEVICE_TYPE_MASK;
1034	workitem->quad_id_msb = hidpp_report->params[HIDPP_PARAM_EQUAD_MSB];
1035	workitem->quad_id_lsb = hidpp_report->params[HIDPP_PARAM_EQUAD_LSB];
1036	switch (workitem->device_type) {
1037	case REPORT_TYPE_KEYBOARD:
1038		workitem->reports_supported |= STD_KEYBOARD | MULTIMEDIA |
1039					       POWER_KEYS | MEDIA_CENTER |
1040					       HIDPP;
1041		id = (workitem->quad_id_msb << 8) | workitem->quad_id_lsb;
1042		for (i = 0; i < ARRAY_SIZE(kbd_builtin_touchpad_ids); i++) {
1043			if (id == kbd_builtin_touchpad_ids[i]) {
1044				if (djrcv_dev->type == recvr_type_dinovo)
1045					workitem->reports_supported |= KBD_MOUSE;
1046				else
1047					workitem->reports_supported |= STD_MOUSE;
1048				break;
1049			}
1050		}
1051		break;
1052	case REPORT_TYPE_MOUSE:
1053		workitem->reports_supported |= STD_MOUSE | HIDPP | MULTIMEDIA;
1054		break;
1055	}
1056}
1057
1058static void logi_hidpp_dev_conn_notif_27mhz(struct hid_device *hdev,
1059					    struct hidpp_event *hidpp_report,
1060					    struct dj_workitem *workitem)
1061{
1062	workitem->type = WORKITEM_TYPE_PAIRED;
1063	workitem->quad_id_lsb = hidpp_report->params[HIDPP_PARAM_27MHZ_DEVID];
1064	switch (hidpp_report->device_index) {
1065	case 1: /* Index 1 is always a mouse */
1066	case 2: /* Index 2 is always a mouse */
1067		workitem->device_type = HIDPP_DEVICE_TYPE_MOUSE;
1068		workitem->reports_supported |= STD_MOUSE | HIDPP;
1069		break;
1070	case 3: /* Index 3 is always the keyboard */
1071		if (hidpp_report->params[HIDPP_PARAM_DEVICE_INFO] & HIDPP_27MHZ_SECURE_MASK) {
1072			hid_info(hdev, "Keyboard connection is encrypted\n");
1073		} else {
1074			hid_warn(hdev, "Keyboard events are send over the air in plain-text / unencrypted\n");
1075			hid_warn(hdev, "See: https://gitlab.freedesktop.org/jwrdegoede/logitech-27mhz-keyboard-encryption-setup/\n");
1076		}
1077		fallthrough;
1078	case 4: /* Index 4 is used for an optional separate numpad */
1079		workitem->device_type = HIDPP_DEVICE_TYPE_KEYBOARD;
1080		workitem->reports_supported |= STD_KEYBOARD | MULTIMEDIA |
1081					       POWER_KEYS | HIDPP;
1082		break;
1083	default:
1084		hid_warn(hdev, "%s: unexpected device-index %d", __func__,
1085			 hidpp_report->device_index);
1086	}
1087}
1088
1089static void logi_hidpp_recv_queue_notif(struct hid_device *hdev,
1090					struct hidpp_event *hidpp_report)
1091{
1092	/* We are called from atomic context (tasklet && djrcv->lock held) */
1093	struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
1094	const char *device_type = "UNKNOWN";
1095	struct dj_workitem workitem = {
1096		.type = WORKITEM_TYPE_EMPTY,
1097		.device_index = hidpp_report->device_index,
1098	};
1099
1100	switch (hidpp_report->params[HIDPP_PARAM_PROTO_TYPE]) {
1101	case 0x01:
1102		device_type = "Bluetooth";
1103		/* Bluetooth connect packet contents is the same as (e)QUAD */
1104		logi_hidpp_dev_conn_notif_equad(hdev, hidpp_report, &workitem);
1105		if (!(hidpp_report->params[HIDPP_PARAM_DEVICE_INFO] &
1106						HIDPP_MANUFACTURER_MASK)) {
1107			hid_info(hdev, "Non Logitech device connected on slot %d\n",
1108				 hidpp_report->device_index);
1109			workitem.reports_supported &= ~HIDPP;
1110		}
1111		break;
1112	case 0x02:
1113		device_type = "27 Mhz";
1114		logi_hidpp_dev_conn_notif_27mhz(hdev, hidpp_report, &workitem);
1115		break;
1116	case 0x03:
1117		device_type = "QUAD or eQUAD";
1118		logi_hidpp_dev_conn_notif_equad(hdev, hidpp_report, &workitem);
1119		break;
1120	case 0x04:
1121		device_type = "eQUAD step 4 DJ";
1122		logi_hidpp_dev_conn_notif_equad(hdev, hidpp_report, &workitem);
1123		break;
1124	case 0x05:
1125		device_type = "DFU Lite";
1126		break;
1127	case 0x06:
1128		device_type = "eQUAD step 4 Lite";
1129		logi_hidpp_dev_conn_notif_equad(hdev, hidpp_report, &workitem);
1130		break;
1131	case 0x07:
1132		device_type = "eQUAD step 4 Gaming";
1133		logi_hidpp_dev_conn_notif_equad(hdev, hidpp_report, &workitem);
1134		workitem.reports_supported |= STD_KEYBOARD;
1135		break;
1136	case 0x08:
1137		device_type = "eQUAD step 4 for gamepads";
1138		break;
1139	case 0x0a:
1140		device_type = "eQUAD nano Lite";
1141		logi_hidpp_dev_conn_notif_equad(hdev, hidpp_report, &workitem);
1142		break;
1143	case 0x0c:
1144		device_type = "eQUAD Lightspeed 1";
1145		logi_hidpp_dev_conn_notif_equad(hdev, hidpp_report, &workitem);
1146		workitem.reports_supported |= STD_KEYBOARD;
1147		break;
1148	case 0x0d:
1149		device_type = "eQUAD Lightspeed 1.1";
1150		logi_hidpp_dev_conn_notif_equad(hdev, hidpp_report, &workitem);
1151		workitem.reports_supported |= STD_KEYBOARD;
1152		break;
1153	case 0x0f:
1154	case 0x11:
1155		device_type = "eQUAD Lightspeed 1.2";
1156		logi_hidpp_dev_conn_notif_equad(hdev, hidpp_report, &workitem);
1157		workitem.reports_supported |= STD_KEYBOARD;
1158		break;
1159	}
1160
1161	/* custom receiver device (eg. powerplay) */
1162	if (hidpp_report->device_index == 7) {
1163		workitem.reports_supported |= HIDPP;
1164	}
1165
1166	if (workitem.type == WORKITEM_TYPE_EMPTY) {
1167		hid_warn(hdev,
1168			 "unusable device of type %s (0x%02x) connected on slot %d",
1169			 device_type,
1170			 hidpp_report->params[HIDPP_PARAM_PROTO_TYPE],
1171			 hidpp_report->device_index);
1172		return;
1173	}
1174
1175	hid_info(hdev, "device of type %s (0x%02x) connected on slot %d",
1176		 device_type, hidpp_report->params[HIDPP_PARAM_PROTO_TYPE],
1177		 hidpp_report->device_index);
1178
1179	kfifo_in(&djrcv_dev->notif_fifo, &workitem, sizeof(workitem));
1180	schedule_work(&djrcv_dev->work);
1181}
1182
1183static void logi_dj_recv_forward_null_report(struct dj_receiver_dev *djrcv_dev,
1184					     struct dj_report *dj_report)
1185{
1186	/* We are called from atomic context (tasklet && djrcv->lock held) */
1187	unsigned int i;
1188	u8 reportbuffer[MAX_REPORT_SIZE];
1189	struct dj_device *djdev;
1190
1191	djdev = djrcv_dev->paired_dj_devices[dj_report->device_index];
1192
1193	memset(reportbuffer, 0, sizeof(reportbuffer));
1194
1195	for (i = 0; i < NUMBER_OF_HID_REPORTS; i++) {
1196		if (djdev->reports_supported & (1 << i)) {
1197			reportbuffer[0] = i;
1198			if (hid_input_report(djdev->hdev,
1199					     HID_INPUT_REPORT,
1200					     reportbuffer,
1201					     hid_reportid_size_map[i], 1)) {
1202				dbg_hid("hid_input_report error sending null "
1203					"report\n");
1204			}
1205		}
1206	}
1207}
1208
1209static void logi_dj_recv_forward_dj(struct dj_receiver_dev *djrcv_dev,
1210				    struct dj_report *dj_report)
1211{
1212	/* We are called from atomic context (tasklet && djrcv->lock held) */
1213	struct dj_device *dj_device;
1214
1215	dj_device = djrcv_dev->paired_dj_devices[dj_report->device_index];
1216
1217	if ((dj_report->report_type > ARRAY_SIZE(hid_reportid_size_map) - 1) ||
1218	    (hid_reportid_size_map[dj_report->report_type] == 0)) {
1219		dbg_hid("invalid report type:%x\n", dj_report->report_type);
1220		return;
1221	}
1222
1223	if (hid_input_report(dj_device->hdev,
1224			HID_INPUT_REPORT, &dj_report->report_type,
1225			hid_reportid_size_map[dj_report->report_type], 1)) {
1226		dbg_hid("hid_input_report error\n");
1227	}
1228}
1229
1230static void logi_dj_recv_forward_report(struct dj_device *dj_dev, u8 *data,
1231					int size)
1232{
1233	/* We are called from atomic context (tasklet && djrcv->lock held) */
1234	if (hid_input_report(dj_dev->hdev, HID_INPUT_REPORT, data, size, 1))
1235		dbg_hid("hid_input_report error\n");
1236}
1237
1238static void logi_dj_recv_forward_input_report(struct hid_device *hdev,
1239					      u8 *data, int size)
1240{
1241	struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
1242	struct dj_device *dj_dev;
1243	unsigned long flags;
1244	u8 report = data[0];
1245	int i;
1246
1247	if (report > REPORT_TYPE_RFREPORT_LAST) {
1248		hid_err(hdev, "Unexpected input report number %d\n", report);
1249		return;
1250	}
1251
1252	spin_lock_irqsave(&djrcv_dev->lock, flags);
1253	for (i = 0; i < (DJ_MAX_PAIRED_DEVICES + DJ_DEVICE_INDEX_MIN); i++) {
1254		dj_dev = djrcv_dev->paired_dj_devices[i];
1255		if (dj_dev && (dj_dev->reports_supported & BIT(report))) {
1256			logi_dj_recv_forward_report(dj_dev, data, size);
1257			spin_unlock_irqrestore(&djrcv_dev->lock, flags);
1258			return;
1259		}
1260	}
1261
1262	logi_dj_recv_queue_unknown_work(djrcv_dev);
1263	spin_unlock_irqrestore(&djrcv_dev->lock, flags);
1264
1265	dbg_hid("No dj-devs handling input report number %d\n", report);
1266}
1267
1268static int logi_dj_recv_send_report(struct dj_receiver_dev *djrcv_dev,
1269				    struct dj_report *dj_report)
1270{
1271	struct hid_device *hdev = djrcv_dev->hidpp;
1272	struct hid_report *report;
1273	struct hid_report_enum *output_report_enum;
1274	u8 *data = (u8 *)(&dj_report->device_index);
1275	unsigned int i;
1276
1277	output_report_enum = &hdev->report_enum[HID_OUTPUT_REPORT];
1278	report = output_report_enum->report_id_hash[REPORT_ID_DJ_SHORT];
1279
1280	if (!report) {
1281		hid_err(hdev, "%s: unable to find dj report\n", __func__);
1282		return -ENODEV;
1283	}
1284
1285	for (i = 0; i < DJREPORT_SHORT_LENGTH - 1; i++)
1286		report->field[0]->value[i] = data[i];
1287
1288	hid_hw_request(hdev, report, HID_REQ_SET_REPORT);
1289
1290	return 0;
1291}
1292
1293static int logi_dj_recv_query_hidpp_devices(struct dj_receiver_dev *djrcv_dev)
1294{
1295	static const u8 template[] = {
1296		REPORT_ID_HIDPP_SHORT,
1297		HIDPP_RECEIVER_INDEX,
1298		HIDPP_SET_REGISTER,
1299		HIDPP_REG_CONNECTION_STATE,
1300		HIDPP_FAKE_DEVICE_ARRIVAL,
1301		0x00, 0x00
1302	};
1303	u8 *hidpp_report;
1304	int retval;
1305
1306	hidpp_report = kmemdup(template, sizeof(template), GFP_KERNEL);
1307	if (!hidpp_report)
1308		return -ENOMEM;
1309
1310	retval = hid_hw_raw_request(djrcv_dev->hidpp,
1311				    REPORT_ID_HIDPP_SHORT,
1312				    hidpp_report, sizeof(template),
1313				    HID_OUTPUT_REPORT,
1314				    HID_REQ_SET_REPORT);
1315
1316	kfree(hidpp_report);
1317	return (retval < 0) ? retval : 0;
1318}
1319
1320static int logi_dj_recv_query_paired_devices(struct dj_receiver_dev *djrcv_dev)
1321{
1322	struct dj_report *dj_report;
1323	int retval;
1324
1325	djrcv_dev->last_query = jiffies;
1326
1327	if (!djrcv_dev->dj_mode)
1328		return 0;
1329
1330	if (djrcv_dev->type != recvr_type_dj) {
1331		retval = logi_dj_recv_query_hidpp_devices(djrcv_dev);
1332		goto out;
1333	}
1334
1335	dj_report = kzalloc(sizeof(struct dj_report), GFP_KERNEL);
1336	if (!dj_report)
1337		return -ENOMEM;
1338	dj_report->report_id = REPORT_ID_DJ_SHORT;
1339	dj_report->device_index = HIDPP_RECEIVER_INDEX;
1340	dj_report->report_type = REPORT_TYPE_CMD_GET_PAIRED_DEVICES;
1341	retval = logi_dj_recv_send_report(djrcv_dev, dj_report);
1342	kfree(dj_report);
1343out:
1344	if (retval < 0)
1345		hid_err(djrcv_dev->hidpp, "%s error:%d\n", __func__, retval);
1346
1347	return retval;
1348}
1349
1350
1351static int logi_dj_recv_switch_to_dj_mode(struct dj_receiver_dev *djrcv_dev,
1352					  unsigned timeout)
1353{
1354	struct hid_device *hdev = djrcv_dev->hidpp;
1355	struct dj_report *dj_report;
1356	u8 *buf;
1357	int retval = 0;
1358
1359	dj_report = kzalloc(sizeof(struct dj_report), GFP_KERNEL);
1360	if (!dj_report)
1361		return -ENOMEM;
1362
1363	if (djrcv_dev->type == recvr_type_dj) {
1364		dj_report->report_id = REPORT_ID_DJ_SHORT;
1365		dj_report->device_index = HIDPP_RECEIVER_INDEX;
1366		dj_report->report_type = REPORT_TYPE_CMD_SWITCH;
1367		dj_report->report_params[CMD_SWITCH_PARAM_DEVBITFIELD] = 0x3F;
1368		dj_report->report_params[CMD_SWITCH_PARAM_TIMEOUT_SECONDS] =
1369								(u8)timeout;
1370
1371		retval = logi_dj_recv_send_report(djrcv_dev, dj_report);
1372		if (retval)
1373			goto out;
1374
1375		/*
1376		 * Ugly sleep to work around a USB 3.0 bug when the receiver is
1377		 * still processing the "switch-to-dj" command while we send an
1378		 * other command.
1379		 * 50 msec should gives enough time to the receiver to be ready.
1380		 */
1381		msleep(50);
1382	}
1383
1384	/*
1385	 * Magical bits to set up hidpp notifications when the dj devices
1386	 * are connected/disconnected.
1387	 *
1388	 * We can reuse dj_report because HIDPP_REPORT_SHORT_LENGTH is smaller
1389	 * than DJREPORT_SHORT_LENGTH.
1390	 */
1391	buf = (u8 *)dj_report;
1392
1393	memset(buf, 0, HIDPP_REPORT_SHORT_LENGTH);
1394
1395	buf[0] = REPORT_ID_HIDPP_SHORT;
1396	buf[1] = HIDPP_RECEIVER_INDEX;
1397	buf[2] = 0x80;
1398	buf[3] = 0x00;
1399	buf[4] = 0x00;
1400	buf[5] = 0x09;
1401	buf[6] = 0x00;
1402
1403	retval = hid_hw_raw_request(hdev, REPORT_ID_HIDPP_SHORT, buf,
1404			HIDPP_REPORT_SHORT_LENGTH, HID_OUTPUT_REPORT,
1405			HID_REQ_SET_REPORT);
1406
1407out:
1408	kfree(dj_report);
1409
1410	if (retval < 0)
1411		hid_err(hdev, "%s error:%d\n", __func__, retval);
1412
1413	djrcv_dev->dj_mode = retval >= 0;
1414	return retval;
1415}
1416
1417
1418static int logi_dj_ll_open(struct hid_device *hid)
1419{
1420	dbg_hid("%s: %s\n", __func__, hid->phys);
1421	return 0;
1422
1423}
1424
1425static void logi_dj_ll_close(struct hid_device *hid)
1426{
1427	dbg_hid("%s: %s\n", __func__, hid->phys);
1428}
1429
1430/*
1431 * Register 0xB5 is "pairing information". It is solely intended for the
1432 * receiver, so do not overwrite the device index.
1433 */
1434static u8 unifying_pairing_query[]  = { REPORT_ID_HIDPP_SHORT,
1435					HIDPP_RECEIVER_INDEX,
1436					HIDPP_GET_LONG_REGISTER,
1437					HIDPP_REG_PAIRING_INFORMATION };
1438static u8 unifying_pairing_answer[] = { REPORT_ID_HIDPP_LONG,
1439					HIDPP_RECEIVER_INDEX,
1440					HIDPP_GET_LONG_REGISTER,
1441					HIDPP_REG_PAIRING_INFORMATION };
1442
1443static int logi_dj_ll_raw_request(struct hid_device *hid,
1444				  unsigned char reportnum, __u8 *buf,
1445				  size_t count, unsigned char report_type,
1446				  int reqtype)
1447{
1448	struct dj_device *djdev = hid->driver_data;
1449	struct dj_receiver_dev *djrcv_dev = djdev->dj_receiver_dev;
1450	u8 *out_buf;
1451	int ret;
1452
1453	if ((buf[0] == REPORT_ID_HIDPP_SHORT) ||
1454	    (buf[0] == REPORT_ID_HIDPP_LONG) ||
1455	    (buf[0] == REPORT_ID_HIDPP_VERY_LONG)) {
1456		if (count < 2)
1457			return -EINVAL;
1458
1459		/* special case where we should not overwrite
1460		 * the device_index */
1461		if (count == 7 && !memcmp(buf, unifying_pairing_query,
1462					  sizeof(unifying_pairing_query)))
1463			buf[4] = (buf[4] & 0xf0) | (djdev->device_index - 1);
1464		else
1465			buf[1] = djdev->device_index;
1466		return hid_hw_raw_request(djrcv_dev->hidpp, reportnum, buf,
1467				count, report_type, reqtype);
1468	}
1469
1470	if (buf[0] != REPORT_TYPE_LEDS)
1471		return -EINVAL;
1472
1473	if (djrcv_dev->type != recvr_type_dj && count >= 2) {
1474		unsigned char led_report_id = 0;
1475
1476		if (!djrcv_dev->keyboard) {
1477			hid_warn(hid, "Received REPORT_TYPE_LEDS request before the keyboard interface was enumerated\n");
1478			return 0;
1479		}
1480
1481		/* This Lightspeed receiver expects LED reports with report ID 1 */
1482		if (djrcv_dev->type == recvr_type_gaming_hidpp_ls_1_3)
1483			led_report_id = 1;
1484
1485		/* usbhid overrides the report ID and ignores the first byte */
1486		return hid_hw_raw_request(djrcv_dev->keyboard, led_report_id, buf, count,
1487					  report_type, reqtype);
1488	}
1489
1490	out_buf = kzalloc(DJREPORT_SHORT_LENGTH, GFP_ATOMIC);
1491	if (!out_buf)
1492		return -ENOMEM;
1493
1494	if (count > DJREPORT_SHORT_LENGTH - 2)
1495		count = DJREPORT_SHORT_LENGTH - 2;
1496
1497	out_buf[0] = REPORT_ID_DJ_SHORT;
1498	out_buf[1] = djdev->device_index;
1499	memcpy(out_buf + 2, buf, count);
1500
1501	ret = hid_hw_raw_request(djrcv_dev->hidpp, out_buf[0], out_buf,
1502		DJREPORT_SHORT_LENGTH, report_type, reqtype);
1503
1504	kfree(out_buf);
1505	return ret;
1506}
1507
1508static void rdcat(char *rdesc, unsigned int *rsize, const char *data, unsigned int size)
1509{
1510	memcpy(rdesc + *rsize, data, size);
1511	*rsize += size;
1512}
1513
1514static int logi_dj_ll_parse(struct hid_device *hid)
1515{
1516	struct dj_device *djdev = hid->driver_data;
1517	unsigned int rsize = 0;
1518	char *rdesc;
1519	int retval;
1520
1521	dbg_hid("%s\n", __func__);
1522
1523	djdev->hdev->version = 0x0111;
1524	djdev->hdev->country = 0x00;
1525
1526	rdesc = kmalloc(MAX_RDESC_SIZE, GFP_KERNEL);
1527	if (!rdesc)
1528		return -ENOMEM;
1529
1530	if (djdev->reports_supported & STD_KEYBOARD) {
1531		dbg_hid("%s: sending a kbd descriptor, reports_supported: %llx\n",
1532			__func__, djdev->reports_supported);
1533		if (djdev->dj_receiver_dev->type == recvr_type_gaming_hidpp_ls_1_3)
1534			rdcat(rdesc, &rsize, kbd_lightspeed_1_3_descriptor,
1535			      sizeof(kbd_lightspeed_1_3_descriptor));
1536		else
1537			rdcat(rdesc, &rsize, kbd_descriptor, sizeof(kbd_descriptor));
1538	}
1539
1540	if (djdev->reports_supported & STD_MOUSE) {
1541		dbg_hid("%s: sending a mouse descriptor, reports_supported: %llx\n",
1542			__func__, djdev->reports_supported);
1543		if (djdev->dj_receiver_dev->type == recvr_type_gaming_hidpp ||
1544		    djdev->dj_receiver_dev->type == recvr_type_mouse_only)
1545			rdcat(rdesc, &rsize, mse_high_res_descriptor,
1546			      sizeof(mse_high_res_descriptor));
1547		else if (djdev->dj_receiver_dev->type == recvr_type_gaming_hidpp_ls_1_3)
1548			rdcat(rdesc, &rsize, mse_high_res_ls_1_3_descriptor,
1549			      sizeof(mse_high_res_ls_1_3_descriptor));
1550		else if (djdev->dj_receiver_dev->type == recvr_type_27mhz)
1551			rdcat(rdesc, &rsize, mse_27mhz_descriptor,
1552			      sizeof(mse_27mhz_descriptor));
1553		else if (recvr_type_is_bluetooth(djdev->dj_receiver_dev->type))
1554			rdcat(rdesc, &rsize, mse_bluetooth_descriptor,
1555			      sizeof(mse_bluetooth_descriptor));
1556		else
1557			rdcat(rdesc, &rsize, mse_descriptor,
1558			      sizeof(mse_descriptor));
1559	}
1560
1561	if (djdev->reports_supported & KBD_MOUSE) {
1562		dbg_hid("%s: sending a kbd-mouse descriptor, reports_supported: %llx\n",
1563			__func__, djdev->reports_supported);
1564		rdcat(rdesc, &rsize, mse5_bluetooth_descriptor,
1565		      sizeof(mse5_bluetooth_descriptor));
1566	}
1567
1568	if (djdev->reports_supported & MULTIMEDIA) {
1569		dbg_hid("%s: sending a multimedia report descriptor: %llx\n",
1570			__func__, djdev->reports_supported);
1571		rdcat(rdesc, &rsize, consumer_descriptor, sizeof(consumer_descriptor));
1572	}
1573
1574	if (djdev->reports_supported & POWER_KEYS) {
1575		dbg_hid("%s: sending a power keys report descriptor: %llx\n",
1576			__func__, djdev->reports_supported);
1577		rdcat(rdesc, &rsize, syscontrol_descriptor, sizeof(syscontrol_descriptor));
1578	}
1579
1580	if (djdev->reports_supported & MEDIA_CENTER) {
1581		dbg_hid("%s: sending a media center report descriptor: %llx\n",
1582			__func__, djdev->reports_supported);
1583		rdcat(rdesc, &rsize, media_descriptor, sizeof(media_descriptor));
1584	}
1585
1586	if (djdev->reports_supported & KBD_LEDS) {
1587		dbg_hid("%s: need to send kbd leds report descriptor: %llx\n",
1588			__func__, djdev->reports_supported);
1589	}
1590
1591	if (djdev->reports_supported & HIDPP) {
1592		dbg_hid("%s: sending a HID++ descriptor, reports_supported: %llx\n",
1593			__func__, djdev->reports_supported);
1594		rdcat(rdesc, &rsize, hidpp_descriptor,
1595		      sizeof(hidpp_descriptor));
1596	}
1597
1598	retval = hid_parse_report(hid, rdesc, rsize);
1599	kfree(rdesc);
1600
1601	return retval;
1602}
1603
1604static int logi_dj_ll_start(struct hid_device *hid)
1605{
1606	dbg_hid("%s\n", __func__);
1607	return 0;
1608}
1609
1610static void logi_dj_ll_stop(struct hid_device *hid)
1611{
1612	dbg_hid("%s\n", __func__);
1613}
1614
1615static bool logi_dj_ll_may_wakeup(struct hid_device *hid)
1616{
1617	struct dj_device *djdev = hid->driver_data;
1618	struct dj_receiver_dev *djrcv_dev = djdev->dj_receiver_dev;
1619
1620	return hid_hw_may_wakeup(djrcv_dev->hidpp);
1621}
1622
1623static const struct hid_ll_driver logi_dj_ll_driver = {
1624	.parse = logi_dj_ll_parse,
1625	.start = logi_dj_ll_start,
1626	.stop = logi_dj_ll_stop,
1627	.open = logi_dj_ll_open,
1628	.close = logi_dj_ll_close,
1629	.raw_request = logi_dj_ll_raw_request,
1630	.may_wakeup = logi_dj_ll_may_wakeup,
1631};
1632
1633static int logi_dj_dj_event(struct hid_device *hdev,
1634			     struct hid_report *report, u8 *data,
1635			     int size)
1636{
1637	struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
1638	struct dj_report *dj_report = (struct dj_report *) data;
1639	unsigned long flags;
1640
1641	/*
1642	 * Here we receive all data coming from iface 2, there are 3 cases:
1643	 *
1644	 * 1) Data is intended for this driver i. e. data contains arrival,
1645	 * departure, etc notifications, in which case we queue them for delayed
1646	 * processing by the work queue. We return 1 to hid-core as no further
1647	 * processing is required from it.
1648	 *
1649	 * 2) Data informs a connection change, if the change means rf link
1650	 * loss, then we must send a null report to the upper layer to discard
1651	 * potentially pressed keys that may be repeated forever by the input
1652	 * layer. Return 1 to hid-core as no further processing is required.
1653	 *
1654	 * 3) Data is an actual input event from a paired DJ device in which
1655	 * case we forward it to the correct hid device (via hid_input_report()
1656	 * ) and return 1 so hid-core does not anything else with it.
1657	 */
1658
1659	if ((dj_report->device_index < DJ_DEVICE_INDEX_MIN) ||
1660	    (dj_report->device_index > DJ_DEVICE_INDEX_MAX)) {
1661		/*
1662		 * Device index is wrong, bail out.
1663		 * This driver can ignore safely the receiver notifications,
1664		 * so ignore those reports too.
1665		 */
1666		if (dj_report->device_index != DJ_RECEIVER_INDEX)
1667			hid_err(hdev, "%s: invalid device index:%d\n",
1668				__func__, dj_report->device_index);
1669		return false;
1670	}
1671
1672	spin_lock_irqsave(&djrcv_dev->lock, flags);
1673
1674	if (!djrcv_dev->paired_dj_devices[dj_report->device_index]) {
1675		/* received an event for an unknown device, bail out */
1676		logi_dj_recv_queue_notification(djrcv_dev, dj_report);
1677		goto out;
1678	}
1679
1680	switch (dj_report->report_type) {
1681	case REPORT_TYPE_NOTIF_DEVICE_PAIRED:
1682		/* pairing notifications are handled above the switch */
1683		break;
1684	case REPORT_TYPE_NOTIF_DEVICE_UNPAIRED:
1685		logi_dj_recv_queue_notification(djrcv_dev, dj_report);
1686		break;
1687	case REPORT_TYPE_NOTIF_CONNECTION_STATUS:
1688		if (dj_report->report_params[CONNECTION_STATUS_PARAM_STATUS] ==
1689		    STATUS_LINKLOSS) {
1690			logi_dj_recv_forward_null_report(djrcv_dev, dj_report);
1691		}
1692		break;
1693	default:
1694		logi_dj_recv_forward_dj(djrcv_dev, dj_report);
1695	}
1696
1697out:
1698	spin_unlock_irqrestore(&djrcv_dev->lock, flags);
1699
1700	return true;
1701}
1702
1703static int logi_dj_hidpp_event(struct hid_device *hdev,
1704			     struct hid_report *report, u8 *data,
1705			     int size)
1706{
1707	struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
1708	struct hidpp_event *hidpp_report = (struct hidpp_event *) data;
1709	struct dj_device *dj_dev;
1710	unsigned long flags;
1711	u8 device_index = hidpp_report->device_index;
1712
1713	if (device_index == HIDPP_RECEIVER_INDEX) {
1714		/* special case were the device wants to know its unifying
1715		 * name */
1716		if (size == HIDPP_REPORT_LONG_LENGTH &&
1717		    !memcmp(data, unifying_pairing_answer,
1718			    sizeof(unifying_pairing_answer)))
1719			device_index = (data[4] & 0x0F) + 1;
1720		else
1721			return false;
1722	}
1723
1724	/*
1725	 * Data is from the HID++ collection, in this case, we forward the
1726	 * data to the corresponding child dj device and return 0 to hid-core
1727	 * so he data also goes to the hidraw device of the receiver. This
1728	 * allows a user space application to implement the full HID++ routing
1729	 * via the receiver.
1730	 */
1731
1732	if ((device_index < DJ_DEVICE_INDEX_MIN) ||
1733	    (device_index > DJ_DEVICE_INDEX_MAX)) {
1734		/*
1735		 * Device index is wrong, bail out.
1736		 * This driver can ignore safely the receiver notifications,
1737		 * so ignore those reports too.
1738		 */
1739		hid_err(hdev, "%s: invalid device index:%d\n", __func__,
1740			hidpp_report->device_index);
1741		return false;
1742	}
1743
1744	spin_lock_irqsave(&djrcv_dev->lock, flags);
1745
1746	dj_dev = djrcv_dev->paired_dj_devices[device_index];
1747
1748	/*
1749	 * With 27 MHz receivers, we do not get an explicit unpair event,
1750	 * remove the old device if the user has paired a *different* device.
1751	 */
1752	if (djrcv_dev->type == recvr_type_27mhz && dj_dev &&
1753	    hidpp_report->sub_id == REPORT_TYPE_NOTIF_DEVICE_CONNECTED &&
1754	    hidpp_report->params[HIDPP_PARAM_PROTO_TYPE] == 0x02 &&
1755	    hidpp_report->params[HIDPP_PARAM_27MHZ_DEVID] !=
1756						dj_dev->hdev->product) {
1757		struct dj_workitem workitem = {
1758			.device_index = hidpp_report->device_index,
1759			.type = WORKITEM_TYPE_UNPAIRED,
1760		};
1761		kfifo_in(&djrcv_dev->notif_fifo, &workitem, sizeof(workitem));
1762		/* logi_hidpp_recv_queue_notif will queue the work */
1763		dj_dev = NULL;
1764	}
1765
1766	if (dj_dev) {
1767		logi_dj_recv_forward_report(dj_dev, data, size);
1768	} else {
1769		if (hidpp_report->sub_id == REPORT_TYPE_NOTIF_DEVICE_CONNECTED)
1770			logi_hidpp_recv_queue_notif(hdev, hidpp_report);
1771		else
1772			logi_dj_recv_queue_unknown_work(djrcv_dev);
1773	}
1774
1775	spin_unlock_irqrestore(&djrcv_dev->lock, flags);
1776
1777	return false;
1778}
1779
1780static int logi_dj_raw_event(struct hid_device *hdev,
1781			     struct hid_report *report, u8 *data,
1782			     int size)
1783{
1784	struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
1785	dbg_hid("%s, size:%d\n", __func__, size);
1786
1787	if (!djrcv_dev)
1788		return 0;
1789
1790	if (!hdev->report_enum[HID_INPUT_REPORT].numbered) {
1791
1792		if (djrcv_dev->unnumbered_application == HID_GD_KEYBOARD) {
1793			/*
1794			 * For the keyboard, we can reuse the same report by
1795			 * using the second byte which is constant in the USB
1796			 * HID report descriptor.
1797			 */
1798			data[1] = data[0];
1799			data[0] = REPORT_TYPE_KEYBOARD;
1800
1801			logi_dj_recv_forward_input_report(hdev, data, size);
1802
1803			/* restore previous state */
1804			data[0] = data[1];
1805			data[1] = 0;
1806		}
1807		/*
1808		 * Mouse-only receivers send unnumbered mouse data. The 27 MHz
1809		 * receiver uses 6 byte packets, the nano receiver 8 bytes,
1810		 * the lightspeed receiver (Pro X Superlight) 13 bytes.
1811		 */
1812		if (djrcv_dev->unnumbered_application == HID_GD_MOUSE &&
1813		    size <= 13){
1814			u8 mouse_report[14];
1815
1816			/* Prepend report id */
1817			mouse_report[0] = REPORT_TYPE_MOUSE;
1818			memcpy(mouse_report + 1, data, size);
1819			logi_dj_recv_forward_input_report(hdev, mouse_report,
1820							  size + 1);
1821		}
1822
1823		return false;
1824	}
1825
1826	switch (data[0]) {
1827	case REPORT_ID_DJ_SHORT:
1828		if (size != DJREPORT_SHORT_LENGTH) {
1829			hid_err(hdev, "Short DJ report bad size (%d)", size);
1830			return false;
1831		}
1832		return logi_dj_dj_event(hdev, report, data, size);
1833	case REPORT_ID_DJ_LONG:
1834		if (size != DJREPORT_LONG_LENGTH) {
1835			hid_err(hdev, "Long DJ report bad size (%d)", size);
1836			return false;
1837		}
1838		return logi_dj_dj_event(hdev, report, data, size);
1839	case REPORT_ID_HIDPP_SHORT:
1840		if (size != HIDPP_REPORT_SHORT_LENGTH) {
1841			hid_err(hdev, "Short HID++ report bad size (%d)", size);
1842			return false;
1843		}
1844		return logi_dj_hidpp_event(hdev, report, data, size);
1845	case REPORT_ID_HIDPP_LONG:
1846		if (size != HIDPP_REPORT_LONG_LENGTH) {
1847			hid_err(hdev, "Long HID++ report bad size (%d)", size);
1848			return false;
1849		}
1850		return logi_dj_hidpp_event(hdev, report, data, size);
1851	}
1852
1853	logi_dj_recv_forward_input_report(hdev, data, size);
1854
1855	return false;
1856}
1857
1858static int logi_dj_probe(struct hid_device *hdev,
1859			 const struct hid_device_id *id)
1860{
1861	struct hid_report_enum *rep_enum;
1862	struct hid_report *rep;
1863	struct dj_receiver_dev *djrcv_dev;
1864	struct usb_interface *intf;
1865	unsigned int no_dj_interfaces = 0;
1866	bool has_hidpp = false;
1867	unsigned long flags;
1868	int retval;
1869
1870	/*
1871	 * Call to usbhid to fetch the HID descriptors of the current
1872	 * interface subsequently call to the hid/hid-core to parse the
1873	 * fetched descriptors.
1874	 */
1875	retval = hid_parse(hdev);
1876	if (retval) {
1877		hid_err(hdev, "%s: parse failed\n", __func__);
1878		return retval;
1879	}
1880
1881	/*
1882	 * Some KVMs add an extra interface for e.g. mouse emulation. If we
1883	 * treat these as logitech-dj interfaces then this causes input events
1884	 * reported through this extra interface to not be reported correctly.
1885	 * To avoid this, we treat these as generic-hid devices.
1886	 */
1887	switch (id->driver_data) {
1888	case recvr_type_dj:		no_dj_interfaces = 3; break;
1889	case recvr_type_hidpp:		no_dj_interfaces = 2; break;
1890	case recvr_type_gaming_hidpp:	no_dj_interfaces = 3; break;
1891	case recvr_type_gaming_hidpp_ls_1_3: no_dj_interfaces = 3; break;
1892	case recvr_type_mouse_only:	no_dj_interfaces = 2; break;
1893	case recvr_type_27mhz:		no_dj_interfaces = 2; break;
1894	case recvr_type_bluetooth:	no_dj_interfaces = 2; break;
1895	case recvr_type_dinovo:		no_dj_interfaces = 2; break;
1896	}
1897	if (hid_is_usb(hdev)) {
1898		intf = to_usb_interface(hdev->dev.parent);
1899		if (intf && intf->altsetting->desc.bInterfaceNumber >=
1900							no_dj_interfaces) {
1901			hdev->quirks |= HID_QUIRK_INPUT_PER_APP;
1902			return hid_hw_start(hdev, HID_CONNECT_DEFAULT);
1903		}
1904	}
1905
1906	rep_enum = &hdev->report_enum[HID_INPUT_REPORT];
1907
1908	/* no input reports, bail out */
1909	if (list_empty(&rep_enum->report_list))
1910		return -ENODEV;
1911
1912	/*
1913	 * Check for the HID++ application.
1914	 * Note: we should theoretically check for HID++ and DJ
1915	 * collections, but this will do.
1916	 */
1917	list_for_each_entry(rep, &rep_enum->report_list, list) {
1918		if (rep->application == 0xff000001)
1919			has_hidpp = true;
1920	}
1921
1922	/*
1923	 * Ignore interfaces without DJ/HID++ collection, they will not carry
1924	 * any data, dont create any hid_device for them.
1925	 */
1926	if (!has_hidpp && id->driver_data == recvr_type_dj)
1927		return -ENODEV;
1928
1929	/* get the current application attached to the node */
1930	rep = list_first_entry(&rep_enum->report_list, struct hid_report, list);
1931	djrcv_dev = dj_get_receiver_dev(hdev, id->driver_data,
1932					rep->application, has_hidpp);
1933	if (!djrcv_dev) {
1934		hid_err(hdev, "%s: dj_get_receiver_dev failed\n", __func__);
1935		return -ENOMEM;
1936	}
1937
1938	if (!rep_enum->numbered)
1939		djrcv_dev->unnumbered_application = rep->application;
1940
1941	/* Starts the usb device and connects to upper interfaces hiddev and
1942	 * hidraw */
1943	retval = hid_hw_start(hdev, HID_CONNECT_HIDRAW|HID_CONNECT_HIDDEV);
1944	if (retval) {
1945		hid_err(hdev, "%s: hid_hw_start returned error\n", __func__);
1946		goto hid_hw_start_fail;
1947	}
1948
1949	if (has_hidpp) {
1950		/*
1951		 * This can fail with a KVM. Ignore errors to let the probe
1952		 * succeed, logi_dj_recv_queue_unknown_work will retry later.
1953		 */
1954		logi_dj_recv_switch_to_dj_mode(djrcv_dev, 0);
1955	}
1956
1957	/* This is enabling the polling urb on the IN endpoint */
1958	retval = hid_hw_open(hdev);
1959	if (retval < 0) {
1960		hid_err(hdev, "%s: hid_hw_open returned error:%d\n",
1961			__func__, retval);
1962		goto llopen_failed;
1963	}
1964
1965	/* Allow incoming packets to arrive: */
1966	hid_device_io_start(hdev);
1967
1968	if (has_hidpp) {
1969		spin_lock_irqsave(&djrcv_dev->lock, flags);
1970		djrcv_dev->ready = true;
1971		spin_unlock_irqrestore(&djrcv_dev->lock, flags);
1972		/* This too can fail with a KVM, ignore errors. */
1973		logi_dj_recv_query_paired_devices(djrcv_dev);
1974	}
1975
1976	return 0;
1977
1978llopen_failed:
1979	hid_hw_stop(hdev);
1980
1981hid_hw_start_fail:
1982	dj_put_receiver_dev(hdev);
1983	return retval;
1984}
1985
1986#ifdef CONFIG_PM
1987static int logi_dj_reset_resume(struct hid_device *hdev)
1988{
1989	struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
1990
1991	if (!djrcv_dev || djrcv_dev->hidpp != hdev)
1992		return 0;
1993
1994	logi_dj_recv_switch_to_dj_mode(djrcv_dev, 0);
1995	return 0;
1996}
1997#endif
1998
1999static void logi_dj_remove(struct hid_device *hdev)
2000{
2001	struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
2002	struct dj_device *dj_dev;
2003	unsigned long flags;
2004	int i;
2005
2006	dbg_hid("%s\n", __func__);
2007
2008	if (!djrcv_dev)
2009		return hid_hw_stop(hdev);
2010
2011	/*
2012	 * This ensures that if the work gets requeued from another
2013	 * interface of the same receiver it will be a no-op.
2014	 */
2015	spin_lock_irqsave(&djrcv_dev->lock, flags);
2016	djrcv_dev->ready = false;
2017	spin_unlock_irqrestore(&djrcv_dev->lock, flags);
2018
2019	cancel_work_sync(&djrcv_dev->work);
2020
2021	hid_hw_close(hdev);
2022	hid_hw_stop(hdev);
2023
2024	/*
2025	 * For proper operation we need access to all interfaces, so we destroy
2026	 * the paired devices when we're unbound from any interface.
2027	 *
2028	 * Note we may still be bound to other interfaces, sharing the same
2029	 * djrcv_dev, so we need locking here.
2030	 */
2031	for (i = 0; i < (DJ_MAX_PAIRED_DEVICES + DJ_DEVICE_INDEX_MIN); i++) {
2032		spin_lock_irqsave(&djrcv_dev->lock, flags);
2033		dj_dev = djrcv_dev->paired_dj_devices[i];
2034		djrcv_dev->paired_dj_devices[i] = NULL;
2035		spin_unlock_irqrestore(&djrcv_dev->lock, flags);
2036		if (dj_dev != NULL) {
2037			hid_destroy_device(dj_dev->hdev);
2038			kfree(dj_dev);
2039		}
2040	}
2041
2042	dj_put_receiver_dev(hdev);
2043}
2044
2045static const struct hid_device_id logi_dj_receivers[] = {
2046	{ /* Logitech unifying receiver (0xc52b) */
2047	  HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
2048		USB_DEVICE_ID_LOGITECH_UNIFYING_RECEIVER),
2049	 .driver_data = recvr_type_dj},
2050	{ /* Logitech unifying receiver (0xc532) */
2051	  HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
2052		USB_DEVICE_ID_LOGITECH_UNIFYING_RECEIVER_2),
2053	 .driver_data = recvr_type_dj},
2054
2055	{ /* Logitech Nano mouse only receiver (0xc52f) */
2056	  HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
2057			 USB_DEVICE_ID_LOGITECH_NANO_RECEIVER),
2058	 .driver_data = recvr_type_mouse_only},
2059	{ /* Logitech Nano (non DJ) receiver (0xc534) */
2060	  HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
2061			 USB_DEVICE_ID_LOGITECH_NANO_RECEIVER_2),
2062	 .driver_data = recvr_type_hidpp},
2063
2064	{ /* Logitech G700(s) receiver (0xc531) */
2065	  HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
2066			 USB_DEVICE_ID_LOGITECH_G700_RECEIVER),
2067	 .driver_data = recvr_type_gaming_hidpp},
2068	{ /* Logitech G602 receiver (0xc537) */
2069	  HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
2070		0xc537),
2071	 .driver_data = recvr_type_gaming_hidpp},
2072	{ /* Logitech lightspeed receiver (0xc539) */
2073	  HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
2074		USB_DEVICE_ID_LOGITECH_NANO_RECEIVER_LIGHTSPEED_1),
2075	 .driver_data = recvr_type_gaming_hidpp},
2076	{ /* Logitech powerplay receiver (0xc53a) */
2077	  HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
2078		USB_DEVICE_ID_LOGITECH_NANO_RECEIVER_POWERPLAY),
2079	 .driver_data = recvr_type_gaming_hidpp},
2080	{ /* Logitech lightspeed receiver (0xc53f) */
2081	  HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
2082		USB_DEVICE_ID_LOGITECH_NANO_RECEIVER_LIGHTSPEED_1_1),
2083	 .driver_data = recvr_type_gaming_hidpp},
2084	{ /* Logitech lightspeed receiver (0xc543) */
2085	  HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
2086		USB_DEVICE_ID_LOGITECH_NANO_RECEIVER_LIGHTSPEED_1_2),
2087	 .driver_data = recvr_type_gaming_hidpp},
2088	{ /* Logitech lightspeed receiver (0xc547) */
2089	  HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
2090		USB_DEVICE_ID_LOGITECH_NANO_RECEIVER_LIGHTSPEED_1_3),
2091	 .driver_data = recvr_type_gaming_hidpp_ls_1_3},
2092	{ /* Logitech lightspeed receiver (0xc54d) */
2093	  HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
2094		USB_DEVICE_ID_LOGITECH_NANO_RECEIVER_LIGHTSPEED_1_4),
2095	 .driver_data = recvr_type_gaming_hidpp_ls_1_3},
2096
2097	{ /* Logitech 27 MHz HID++ 1.0 receiver (0xc513) */
2098	  HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_MX3000_RECEIVER),
2099	 .driver_data = recvr_type_27mhz},
2100	{ /* Logitech 27 MHz HID++ 1.0 receiver (0xc517) */
2101	  HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
2102		USB_DEVICE_ID_S510_RECEIVER_2),
2103	 .driver_data = recvr_type_27mhz},
2104	{ /* Logitech 27 MHz HID++ 1.0 mouse-only receiver (0xc51b) */
2105	  HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
2106		USB_DEVICE_ID_LOGITECH_27MHZ_MOUSE_RECEIVER),
2107	 .driver_data = recvr_type_27mhz},
2108
2109	{ /* Logitech MX5000 HID++ / bluetooth receiver keyboard intf. (0xc70e) */
2110	  HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
2111		USB_DEVICE_ID_MX5000_RECEIVER_KBD_DEV),
2112	 .driver_data = recvr_type_bluetooth},
2113	{ /* Logitech MX5000 HID++ / bluetooth receiver mouse intf. (0xc70a) */
2114	  HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
2115		USB_DEVICE_ID_MX5000_RECEIVER_MOUSE_DEV),
2116	 .driver_data = recvr_type_bluetooth},
2117	{ /* Logitech MX5500 HID++ / bluetooth receiver keyboard intf. (0xc71b) */
2118	  HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
2119		USB_DEVICE_ID_MX5500_RECEIVER_KBD_DEV),
2120	 .driver_data = recvr_type_bluetooth},
2121	{ /* Logitech MX5500 HID++ / bluetooth receiver mouse intf. (0xc71c) */
2122	  HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
2123		USB_DEVICE_ID_MX5500_RECEIVER_MOUSE_DEV),
2124	 .driver_data = recvr_type_bluetooth},
2125
2126	{ /* Logitech Dinovo Edge HID++ / bluetooth receiver keyboard intf. (0xc713) */
2127	  HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
2128		USB_DEVICE_ID_DINOVO_EDGE_RECEIVER_KBD_DEV),
2129	 .driver_data = recvr_type_dinovo},
2130	{ /* Logitech Dinovo Edge HID++ / bluetooth receiver mouse intf. (0xc714) */
2131	  HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
2132		USB_DEVICE_ID_DINOVO_EDGE_RECEIVER_MOUSE_DEV),
2133	 .driver_data = recvr_type_dinovo},
2134	{ /* Logitech DiNovo Mini HID++ / bluetooth receiver mouse intf. (0xc71e) */
2135	  HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
2136		USB_DEVICE_ID_DINOVO_MINI_RECEIVER_KBD_DEV),
2137	 .driver_data = recvr_type_dinovo},
2138	{ /* Logitech DiNovo Mini HID++ / bluetooth receiver keyboard intf. (0xc71f) */
2139	  HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
2140		USB_DEVICE_ID_DINOVO_MINI_RECEIVER_MOUSE_DEV),
2141	 .driver_data = recvr_type_dinovo},
2142	{}
2143};
2144
2145MODULE_DEVICE_TABLE(hid, logi_dj_receivers);
2146
2147static struct hid_driver logi_djreceiver_driver = {
2148	.name = "logitech-djreceiver",
2149	.id_table = logi_dj_receivers,
2150	.probe = logi_dj_probe,
2151	.remove = logi_dj_remove,
2152	.raw_event = logi_dj_raw_event,
2153#ifdef CONFIG_PM
2154	.reset_resume = logi_dj_reset_resume,
2155#endif
2156};
2157
2158module_hid_driver(logi_djreceiver_driver);
2159
2160MODULE_DESCRIPTION("HID driver for Logitech receivers");
2161MODULE_LICENSE("GPL");
2162MODULE_AUTHOR("Logitech");
2163MODULE_AUTHOR("Nestor Lopez Casado");
2164MODULE_AUTHOR("nlopezcasad@logitech.com");
~/ Read-only file preview.