hid-logitech-dj.c 60.6 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-only
2 3 4 5 6 7 8 9 10 11 12
/*
 *  HID driver for Logitech Unifying receivers
 *
 *  Copyright (c) 2011 Logitech
 */



#include <linux/device.h>
#include <linux/hid.h>
#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 <asm/unaligned.h>
17
#include "hid-ids.h"
18 19

#define DJ_MAX_PAIRED_DEVICES			6
20
#define DJ_MAX_NUMBER_NOTIFS			8
21 22 23 24 25 26 27 28 29 30
#define DJ_RECEIVER_INDEX			0
#define DJ_DEVICE_INDEX_MIN			1
#define DJ_DEVICE_INDEX_MAX			6

#define DJREPORT_SHORT_LENGTH			15
#define DJREPORT_LONG_LENGTH			32

#define REPORT_ID_DJ_SHORT			0x20
#define REPORT_ID_DJ_LONG			0x21

31 32 33 34 35 36 37 38 39
#define REPORT_ID_HIDPP_SHORT			0x10
#define REPORT_ID_HIDPP_LONG			0x11

#define HIDPP_REPORT_SHORT_LENGTH		7
#define HIDPP_REPORT_LONG_LENGTH		20

#define HIDPP_RECEIVER_INDEX			0xff

#define REPORT_TYPE_RFREPORT_FIRST		0x01
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
#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 */
82 83 84 85 86 87
#define STD_KEYBOARD				BIT(1)
#define STD_MOUSE				BIT(2)
#define MULTIMEDIA				BIT(3)
#define POWER_KEYS				BIT(4)
#define MEDIA_CENTER				BIT(8)
#define KBD_LEDS				BIT(14)
88 89
/* Fake (bitnr > NUMBER_OF_HID_REPORTS) bit to track HID++ capability */
#define HIDPP					BIT_ULL(63)
90

91 92 93 94 95 96
/* HID++ Device Connected Notification */
#define REPORT_TYPE_NOTIF_DEVICE_CONNECTED	0x41
#define HIDPP_PARAM_PROTO_TYPE			0x00
#define HIDPP_PARAM_DEVICE_INFO			0x01
#define HIDPP_PARAM_EQUAD_LSB			0x02
#define HIDPP_PARAM_EQUAD_MSB			0x03
97
#define HIDPP_PARAM_27MHZ_DEVID			0x03
98 99
#define HIDPP_DEVICE_TYPE_MASK			GENMASK(3, 0)
#define HIDPP_LINK_STATUS_MASK			BIT(6)
100
#define HIDPP_MANUFACTURER_MASK			BIT(7)
101

102 103 104
#define HIDPP_DEVICE_TYPE_KEYBOARD		1
#define HIDPP_DEVICE_TYPE_MOUSE			2

105
#define HIDPP_SET_REGISTER			0x80
106
#define HIDPP_GET_LONG_REGISTER			0x83
107
#define HIDPP_REG_CONNECTION_STATE		0x02
108 109
#define HIDPP_REG_PAIRING_INFORMATION		0xB5
#define HIDPP_PAIRING_INFORMATION		0x20
110 111 112 113 114
#define HIDPP_FAKE_DEVICE_ARRIVAL		0x02

enum recvr_type {
	recvr_type_dj,
	recvr_type_hidpp,
115
	recvr_type_gaming_hidpp,
116
	recvr_type_mouse_only,
117
	recvr_type_27mhz,
118
	recvr_type_bluetooth,
119
};
120

121 122 123 124 125 126 127
struct dj_report {
	u8 report_id;
	u8 device_index;
	u8 report_type;
	u8 report_params[DJREPORT_SHORT_LENGTH - 3];
};

128 129 130 131 132 133 134
struct hidpp_event {
	u8 report_id;
	u8 device_index;
	u8 sub_id;
	u8 params[HIDPP_REPORT_LONG_LENGTH - 3U];
} __packed;

135
struct dj_receiver_dev {
136 137
	struct hid_device *mouse;
	struct hid_device *keyboard;
138
	struct hid_device *hidpp;
139 140
	struct dj_device *paired_dj_devices[DJ_MAX_PAIRED_DEVICES +
					    DJ_DEVICE_INDEX_MIN];
141 142
	struct list_head list;
	struct kref kref;
143 144
	struct work_struct work;
	struct kfifo notif_fifo;
145
	unsigned long last_query; /* in jiffies */
146
	bool ready;
147 148
	enum recvr_type type;
	unsigned int unnumbered_application;
149 150 151 152 153 154
	spinlock_t lock;
};

struct dj_device {
	struct hid_device *hdev;
	struct dj_receiver_dev *dj_receiver_dev;
155
	u64 reports_supported;
156 157
	u8 device_index;
};
158

159 160 161 162 163 164 165 166
#define WORKITEM_TYPE_EMPTY	0
#define WORKITEM_TYPE_PAIRED	1
#define WORKITEM_TYPE_UNPAIRED	2
#define WORKITEM_TYPE_UNKNOWN	255

struct dj_workitem {
	u8 type;		/* WORKITEM_TYPE_* */
	u8 device_index;
167
	u8 device_type;
168 169
	u8 quad_id_msb;
	u8 quad_id_lsb;
170
	u64 reports_supported;
171 172
};

173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
/* 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, 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)       */
195 196 197 198 199 200 201 202 203 204 205 206
	0x85, 0x0e,		/* REPORT_ID (14)               */
	0x05, 0x08,		/*   USAGE PAGE (LED page)      */
	0x95, 0x05,		/*   REPORT COUNT (5)           */
	0x75, 0x01,		/*   REPORT SIZE (1)            */
	0x15, 0x00,		/*   LOGICAL_MINIMUM (0)        */
	0x25, 0x01,		/*   LOGICAL_MAXIMUM (1)        */
	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)          */
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
	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                      */
};

248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
/* Mouse descriptor (2) for 27 MHz receiver, only 8 buttons */
static const char mse_27mhz_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, 0x08,		/*      USAGE_MAX (8)                   */
	0x15, 0x00,		/*      LOGICAL_MIN (0)                 */
	0x25, 0x01,		/*      LOGICAL_MAX (1)                 */
	0x95, 0x08,		/*      REPORT_COUNT (8)                */
	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                      */
};

286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
/* Mouse descriptor (2) for Bluetooth receiver, low-res hwheel, 12 buttons */
static const char mse_bluetooth_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, 0x08,		/*      USAGE_MAX (8)                   */
	0x15, 0x00,		/*      LOGICAL_MIN (0)                 */
	0x25, 0x01,		/*      LOGICAL_MAX (1)                 */
	0x95, 0x08,		/*      REPORT_COUNT (8)                */
	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)                   */
	0x15, 0xF9,		/*      LOGICAL_MIN (-7)                */
	0x25, 0x07,		/*      LOGICAL_MAX (7)                 */
	0x75, 0x04,		/*      REPORT_SIZE (4)                 */
	0x95, 0x01,		/*      REPORT_COUNT (1)                */
	0x81, 0x06,		/*      INPUT                           */
	0x05, 0x09,		/*      USAGE_PAGE (buttons)            */
	0x19, 0x09,		/*      USAGE_MIN (9)                   */
	0x29, 0x0C,		/*      USAGE_MAX (12)                  */
	0x15, 0x00,		/*      LOGICAL_MIN (0)                 */
	0x25, 0x01,		/*      LOGICAL_MAX (1)                 */
	0x75, 0x01,		/*      REPORT_SIZE (1)                 */
	0x95, 0x04,		/*      REPORT_COUNT (4)                */
	0x81, 0x06,		/*      INPUT                           */
	0xC0,			/*    END_COLLECTION                    */
	0xC0,			/*  END_COLLECTION                      */
};

335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
/* Gaming Mouse descriptor (2) */
static const char mse_high_res_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, 0x80,	/*      LOGICAL_MIN (-32767)            */
	0x26, 0xFF, 0x7F,	/*      LOGICAL_MAX (32767)             */
	0x75, 0x10,		/*      REPORT_SIZE (16)                */
	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                      */
};

373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423
/* 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                       */
};				/*                                     */

424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474
/* HIDPP descriptor */
static const char hidpp_descriptor[] = {
	0x06, 0x00, 0xff,	/* Usage Page (Vendor Defined Page 1)  */
	0x09, 0x01,		/* Usage (Vendor Usage 1)              */
	0xa1, 0x01,		/* Collection (Application)            */
	0x85, 0x10,		/*   Report ID (16)                    */
	0x75, 0x08,		/*   Report Size (8)                   */
	0x95, 0x06,		/*   Report Count (6)                  */
	0x15, 0x00,		/*   Logical Minimum (0)               */
	0x26, 0xff, 0x00,	/*   Logical Maximum (255)             */
	0x09, 0x01,		/*   Usage (Vendor Usage 1)            */
	0x81, 0x00,		/*   Input (Data,Arr,Abs)              */
	0x09, 0x01,		/*   Usage (Vendor Usage 1)            */
	0x91, 0x00,		/*   Output (Data,Arr,Abs)             */
	0xc0,			/* End Collection                      */
	0x06, 0x00, 0xff,	/* Usage Page (Vendor Defined Page 1)  */
	0x09, 0x02,		/* Usage (Vendor Usage 2)              */
	0xa1, 0x01,		/* Collection (Application)            */
	0x85, 0x11,		/*   Report ID (17)                    */
	0x75, 0x08,		/*   Report Size (8)                   */
	0x95, 0x13,		/*   Report Count (19)                 */
	0x15, 0x00,		/*   Logical Minimum (0)               */
	0x26, 0xff, 0x00,	/*   Logical Maximum (255)             */
	0x09, 0x02,		/*   Usage (Vendor Usage 2)            */
	0x81, 0x00,		/*   Input (Data,Arr,Abs)              */
	0x09, 0x02,		/*   Usage (Vendor Usage 2)            */
	0x91, 0x00,		/*   Output (Data,Arr,Abs)             */
	0xc0,			/* End Collection                      */
	0x06, 0x00, 0xff,	/* Usage Page (Vendor Defined Page 1)  */
	0x09, 0x04,		/* Usage (Vendor Usage 0x04)           */
	0xa1, 0x01,		/* Collection (Application)            */
	0x85, 0x20,		/*   Report ID (32)                    */
	0x75, 0x08,		/*   Report Size (8)                   */
	0x95, 0x0e,		/*   Report Count (14)                 */
	0x15, 0x00,		/*   Logical Minimum (0)               */
	0x26, 0xff, 0x00,	/*   Logical Maximum (255)             */
	0x09, 0x41,		/*   Usage (Vendor Usage 0x41)         */
	0x81, 0x00,		/*   Input (Data,Arr,Abs)              */
	0x09, 0x41,		/*   Usage (Vendor Usage 0x41)         */
	0x91, 0x00,		/*   Output (Data,Arr,Abs)             */
	0x85, 0x21,		/*   Report ID (33)                    */
	0x95, 0x1f,		/*   Report Count (31)                 */
	0x15, 0x00,		/*   Logical Minimum (0)               */
	0x26, 0xff, 0x00,	/*   Logical Maximum (255)             */
	0x09, 0x42,		/*   Usage (Vendor Usage 0x42)         */
	0x81, 0x00,		/*   Input (Data,Arr,Abs)              */
	0x09, 0x42,		/*   Usage (Vendor Usage 0x42)         */
	0x91, 0x00,		/*   Output (Data,Arr,Abs)             */
	0xc0,			/* End Collection                      */
};

475 476 477
/* Maximum size of all defined hid reports in bytes (including report id) */
#define MAX_REPORT_SIZE 8

478 479 480
/* Make sure all descriptors are present here */
#define MAX_RDESC_SIZE				\
	(sizeof(kbd_descriptor) +		\
481
	 sizeof(mse_bluetooth_descriptor) +	\
482 483
	 sizeof(consumer_descriptor) +		\
	 sizeof(syscontrol_descriptor) +	\
484 485
	 sizeof(media_descriptor) +	\
	 sizeof(hidpp_descriptor))
486

487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512
/* 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;

513
static int logi_dj_recv_query_paired_devices(struct dj_receiver_dev *djrcv_dev);
514 515 516 517 518 519 520 521 522 523 524 525
static void delayedwork_callback(struct work_struct *work);

static LIST_HEAD(dj_hdev_list);
static DEFINE_MUTEX(dj_hdev_list_lock);

/*
 * dj/HID++ receivers are really a single logical entity, but for BIOS/Windows
 * compatibility they have multiple USB interfaces. On HID++ receivers we need
 * to listen for input reports on both interfaces. The functions below are used
 * to create a single struct dj_receiver_dev for all interfaces belonging to
 * a single USB-device / receiver.
 */
526 527
static struct dj_receiver_dev *dj_find_receiver_dev(struct hid_device *hdev,
						    enum recvr_type type)
528 529
{
	struct dj_receiver_dev *djrcv_dev;
530 531 532 533 534 535 536
	char sep;

	/*
	 * The bluetooth receiver contains a built-in hub and has separate
	 * USB-devices for the keyboard and mouse interfaces.
	 */
	sep = (type == recvr_type_bluetooth) ? '.' : '/';
537 538 539 540

	/* Try to find an already-probed interface from the same device */
	list_for_each_entry(djrcv_dev, &dj_hdev_list, list) {
		if (djrcv_dev->mouse &&
541
		    hid_compare_device_paths(hdev, djrcv_dev->mouse, sep)) {
542 543 544 545
			kref_get(&djrcv_dev->kref);
			return djrcv_dev;
		}
		if (djrcv_dev->keyboard &&
546
		    hid_compare_device_paths(hdev, djrcv_dev->keyboard, sep)) {
547 548 549 550
			kref_get(&djrcv_dev->kref);
			return djrcv_dev;
		}
		if (djrcv_dev->hidpp &&
551
		    hid_compare_device_paths(hdev, djrcv_dev->hidpp, sep)) {
552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587
			kref_get(&djrcv_dev->kref);
			return djrcv_dev;
		}
	}

	return NULL;
}

static void dj_release_receiver_dev(struct kref *kref)
{
	struct dj_receiver_dev *djrcv_dev = container_of(kref, struct dj_receiver_dev, kref);

	list_del(&djrcv_dev->list);
	kfifo_free(&djrcv_dev->notif_fifo);
	kfree(djrcv_dev);
}

static void dj_put_receiver_dev(struct hid_device *hdev)
{
	struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);

	mutex_lock(&dj_hdev_list_lock);

	if (djrcv_dev->mouse == hdev)
		djrcv_dev->mouse = NULL;
	if (djrcv_dev->keyboard == hdev)
		djrcv_dev->keyboard = NULL;
	if (djrcv_dev->hidpp == hdev)
		djrcv_dev->hidpp = NULL;

	kref_put(&djrcv_dev->kref, dj_release_receiver_dev);

	mutex_unlock(&dj_hdev_list_lock);
}

static struct dj_receiver_dev *dj_get_receiver_dev(struct hid_device *hdev,
588
						   enum recvr_type type,
589 590 591 592 593 594 595
						   unsigned int application,
						   bool is_hidpp)
{
	struct dj_receiver_dev *djrcv_dev;

	mutex_lock(&dj_hdev_list_lock);

596
	djrcv_dev = dj_find_receiver_dev(hdev, type);
597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612
	if (!djrcv_dev) {
		djrcv_dev = kzalloc(sizeof(*djrcv_dev), GFP_KERNEL);
		if (!djrcv_dev)
			goto out;

		INIT_WORK(&djrcv_dev->work, delayedwork_callback);
		spin_lock_init(&djrcv_dev->lock);
		if (kfifo_alloc(&djrcv_dev->notif_fifo,
			    DJ_MAX_NUMBER_NOTIFS * sizeof(struct dj_workitem),
			    GFP_KERNEL)) {
			kfree(djrcv_dev);
			djrcv_dev = NULL;
			goto out;
		}
		kref_init(&djrcv_dev->kref);
		list_add_tail(&djrcv_dev->list, &dj_hdev_list);
613
		djrcv_dev->last_query = jiffies;
614
		djrcv_dev->type = type;
615 616 617 618 619 620 621 622 623 624 625 626 627 628
	}

	if (application == HID_GD_KEYBOARD)
		djrcv_dev->keyboard = hdev;
	if (application == HID_GD_MOUSE)
		djrcv_dev->mouse = hdev;
	if (is_hidpp)
		djrcv_dev->hidpp = hdev;

	hid_set_drvdata(hdev, djrcv_dev);
out:
	mutex_unlock(&dj_hdev_list_lock);
	return djrcv_dev;
}
629 630

static void logi_dj_recv_destroy_djhid_device(struct dj_receiver_dev *djrcv_dev,
631
					      struct dj_workitem *workitem)
632 633 634 635 636 637
{
	/* Called in delayed work context */
	struct dj_device *dj_dev;
	unsigned long flags;

	spin_lock_irqsave(&djrcv_dev->lock, flags);
638 639
	dj_dev = djrcv_dev->paired_dj_devices[workitem->device_index];
	djrcv_dev->paired_dj_devices[workitem->device_index] = NULL;
640 641 642 643 644 645
	spin_unlock_irqrestore(&djrcv_dev->lock, flags);

	if (dj_dev != NULL) {
		hid_destroy_device(dj_dev->hdev);
		kfree(dj_dev);
	} else {
646
		hid_err(djrcv_dev->hidpp, "%s: can't destroy a NULL device\n",
647 648 649 650 651
			__func__);
	}
}

static void logi_dj_recv_add_djhid_device(struct dj_receiver_dev *djrcv_dev,
652
					  struct dj_workitem *workitem)
653 654
{
	/* Called in delayed work context */
655
	struct hid_device *djrcv_hdev = djrcv_dev->hidpp;
656 657
	struct hid_device *dj_hiddev;
	struct dj_device *dj_dev;
658
	u8 device_index = workitem->device_index;
659
	unsigned long flags;
660 661 662 663 664 665

	/* Device index goes from 1 to 6, we need 3 bytes to store the
	 * semicolon, the index, and a null terminator
	 */
	unsigned char tmpstr[3];

666
	/* We are the only one ever adding a device, no need to lock */
667
	if (djrcv_dev->paired_dj_devices[device_index]) {
668 669 670 671 672
		/* The device is already known. No need to reallocate it. */
		dbg_hid("%s: device is already known\n", __func__);
		return;
	}

673 674
	dj_hiddev = hid_allocate_device();
	if (IS_ERR(dj_hiddev)) {
675
		hid_err(djrcv_hdev, "%s: hid_allocate_dev failed\n", __func__);
676 677 678 679 680 681 682
		return;
	}

	dj_hiddev->ll_driver = &logi_dj_ll_driver;

	dj_hiddev->dev.parent = &djrcv_hdev->dev;
	dj_hiddev->bus = BUS_USB;
683
	dj_hiddev->vendor = djrcv_hdev->vendor;
684 685
	dj_hiddev->product = (workitem->quad_id_msb << 8) |
			      workitem->quad_id_lsb;
686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705
	if (workitem->device_type) {
		const char *type_str = "Device";

		switch (workitem->device_type) {
		case 0x01: type_str = "Keyboard";	break;
		case 0x02: type_str = "Mouse";		break;
		case 0x03: type_str = "Numpad";		break;
		case 0x04: type_str = "Presenter";	break;
		case 0x07: type_str = "Remote Control";	break;
		case 0x08: type_str = "Trackball";	break;
		case 0x09: type_str = "Touchpad";	break;
		}
		snprintf(dj_hiddev->name, sizeof(dj_hiddev->name),
			"Logitech Wireless %s PID:%04x",
			type_str, dj_hiddev->product);
	} else {
		snprintf(dj_hiddev->name, sizeof(dj_hiddev->name),
			"Logitech Unifying Device. Wireless PID:%04x",
			dj_hiddev->product);
	}
706

707 708 709 710
	if (djrcv_dev->type == recvr_type_27mhz)
		dj_hiddev->group = HID_GROUP_LOGITECH_27MHZ_DEVICE;
	else
		dj_hiddev->group = HID_GROUP_LOGITECH_DJ_DEVICE;
711

712
	memcpy(dj_hiddev->phys, djrcv_hdev->phys, sizeof(djrcv_hdev->phys));
713
	snprintf(tmpstr, sizeof(tmpstr), ":%d", device_index);
714 715 716 717 718
	strlcat(dj_hiddev->phys, tmpstr, sizeof(dj_hiddev->phys));

	dj_dev = kzalloc(sizeof(struct dj_device), GFP_KERNEL);

	if (!dj_dev) {
719
		hid_err(djrcv_hdev, "%s: failed allocating dj_dev\n", __func__);
720 721 722
		goto dj_device_allocate_fail;
	}

723
	dj_dev->reports_supported = workitem->reports_supported;
724 725
	dj_dev->hdev = dj_hiddev;
	dj_dev->dj_receiver_dev = djrcv_dev;
726
	dj_dev->device_index = device_index;
727 728
	dj_hiddev->driver_data = dj_dev;

729
	spin_lock_irqsave(&djrcv_dev->lock, flags);
730
	djrcv_dev->paired_dj_devices[device_index] = dj_dev;
731
	spin_unlock_irqrestore(&djrcv_dev->lock, flags);
732 733

	if (hid_add_device(dj_hiddev)) {
734
		hid_err(djrcv_hdev, "%s: failed adding dj_device\n", __func__);
735 736 737 738 739 740
		goto hid_add_device_fail;
	}

	return;

hid_add_device_fail:
741
	spin_lock_irqsave(&djrcv_dev->lock, flags);
742
	djrcv_dev->paired_dj_devices[device_index] = NULL;
743
	spin_unlock_irqrestore(&djrcv_dev->lock, flags);
744 745 746 747 748 749 750 751 752 753
	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);

754
	struct dj_workitem workitem;
755 756
	unsigned long flags;
	int count;
757
	int retval;
758 759 760 761 762

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

	spin_lock_irqsave(&djrcv_dev->lock, flags);

763 764 765 766 767 768 769 770 771 772 773
	/*
	 * Since we attach to multiple interfaces, we may get scheduled before
	 * we are bound to the HID++ interface, catch this.
	 */
	if (!djrcv_dev->ready) {
		pr_warn("%s: delayedwork queued before hidpp interface was enumerated\n",
			__func__);
		spin_unlock_irqrestore(&djrcv_dev->lock, flags);
		return;
	}

774
	count = kfifo_out(&djrcv_dev->notif_fifo, &workitem, sizeof(workitem));
775

776
	if (count != sizeof(workitem)) {
777 778 779 780
		spin_unlock_irqrestore(&djrcv_dev->lock, flags);
		return;
	}

781 782
	if (!kfifo_is_empty(&djrcv_dev->notif_fifo))
		schedule_work(&djrcv_dev->work);
783 784 785

	spin_unlock_irqrestore(&djrcv_dev->lock, flags);

786 787 788
	switch (workitem.type) {
	case WORKITEM_TYPE_PAIRED:
		logi_dj_recv_add_djhid_device(djrcv_dev, &workitem);
789
		break;
790 791 792 793 794 795
	case WORKITEM_TYPE_UNPAIRED:
		logi_dj_recv_destroy_djhid_device(djrcv_dev, &workitem);
		break;
	case WORKITEM_TYPE_UNKNOWN:
		retval = logi_dj_recv_query_paired_devices(djrcv_dev);
		if (retval) {
796
			hid_err(djrcv_dev->hidpp, "%s: logi_dj_recv_query_paired_devices error: %d\n",
797 798 799 800 801 802 803 804 805
				__func__, retval);
		}
		break;
	case WORKITEM_TYPE_EMPTY:
		dbg_hid("%s: device list is empty\n", __func__);
		break;
	}
}

806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829
/*
 * Sometimes we receive reports for which we do not have a paired dj_device
 * associated with the device_index or report-type to forward the report to.
 * This means that the original "device paired" notification corresponding
 * to the dj_device never arrived to this driver. Possible reasons for this are:
 * 1) hid-core discards all packets coming from a device during probe().
 * 2) if the receiver is plugged into a KVM switch then the pairing reports
 * are only forwarded to it if the focus is on this PC.
 * This function deals with this by re-asking the receiver for the list of
 * connected devices in the delayed work callback.
 * This function MUST be called with djrcv->lock held.
 */
static void logi_dj_recv_queue_unknown_work(struct dj_receiver_dev *djrcv_dev)
{
	struct dj_workitem workitem = { .type = WORKITEM_TYPE_UNKNOWN };

	/* Rate limit queries done because of unhandeled reports to 2/sec */
	if (time_before(jiffies, djrcv_dev->last_query + HZ / 2))
		return;

	kfifo_in(&djrcv_dev->notif_fifo, &workitem, sizeof(workitem));
	schedule_work(&djrcv_dev->work);
}

830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846
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) */
	struct dj_workitem workitem = {
		.device_index = dj_report->device_index,
	};

	switch (dj_report->report_type) {
	case REPORT_TYPE_NOTIF_DEVICE_PAIRED:
		workitem.type = WORKITEM_TYPE_PAIRED;
		if (dj_report->report_params[DEVICE_PAIRED_PARAM_SPFUNCTION] &
		    SPFUNCTION_DEVICE_LIST_EMPTY) {
			workitem.type = WORKITEM_TYPE_EMPTY;
			break;
		}
		/* fall-through */
847
	case REPORT_TYPE_NOTIF_DEVICE_UNPAIRED:
848 849 850 851 852 853 854
		workitem.quad_id_msb =
			dj_report->report_params[DEVICE_PAIRED_PARAM_EQUAD_ID_MSB];
		workitem.quad_id_lsb =
			dj_report->report_params[DEVICE_PAIRED_PARAM_EQUAD_ID_LSB];
		workitem.reports_supported = get_unaligned_le32(
						dj_report->report_params +
						DEVICE_PAIRED_RF_REPORT_TYPE);
855
		workitem.reports_supported |= HIDPP;
856 857
		if (dj_report->report_type == REPORT_TYPE_NOTIF_DEVICE_UNPAIRED)
			workitem.type = WORKITEM_TYPE_UNPAIRED;
858 859
		break;
	default:
860 861
		logi_dj_recv_queue_unknown_work(djrcv_dev);
		return;
862 863
	}

864
	kfifo_in(&djrcv_dev->notif_fifo, &workitem, sizeof(workitem));
865
	schedule_work(&djrcv_dev->work);
866 867
}

868 869
static void logi_hidpp_dev_conn_notif_equad(struct hid_device *hdev,
					    struct hidpp_event *hidpp_report,
870 871
					    struct dj_workitem *workitem)
{
872 873
	struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);

874
	workitem->type = WORKITEM_TYPE_PAIRED;
875 876
	workitem->device_type = hidpp_report->params[HIDPP_PARAM_DEVICE_INFO] &
				HIDPP_DEVICE_TYPE_MASK;
877 878
	workitem->quad_id_msb = hidpp_report->params[HIDPP_PARAM_EQUAD_MSB];
	workitem->quad_id_lsb = hidpp_report->params[HIDPP_PARAM_EQUAD_LSB];
879
	switch (workitem->device_type) {
880 881
	case REPORT_TYPE_KEYBOARD:
		workitem->reports_supported |= STD_KEYBOARD | MULTIMEDIA |
882 883
					       POWER_KEYS | MEDIA_CENTER |
					       HIDPP;
884 885
		break;
	case REPORT_TYPE_MOUSE:
886
		workitem->reports_supported |= STD_MOUSE | HIDPP;
887 888
		if (djrcv_dev->type == recvr_type_mouse_only)
			workitem->reports_supported |= MULTIMEDIA;
889 890 891 892
		break;
	}
}

893 894 895 896 897 898 899 900 901
static void logi_hidpp_dev_conn_notif_27mhz(struct hid_device *hdev,
					    struct hidpp_event *hidpp_report,
					    struct dj_workitem *workitem)
{
	workitem->type = WORKITEM_TYPE_PAIRED;
	workitem->quad_id_lsb = hidpp_report->params[HIDPP_PARAM_27MHZ_DEVID];
	switch (hidpp_report->device_index) {
	case 1: /* Index 1 is always a mouse */
	case 2: /* Index 2 is always a mouse */
902
		workitem->device_type = HIDPP_DEVICE_TYPE_MOUSE;
903
		workitem->reports_supported |= STD_MOUSE | HIDPP;
904 905 906
		break;
	case 3: /* Index 3 is always the keyboard */
	case 4: /* Index 4 is used for an optional separate numpad */
907
		workitem->device_type = HIDPP_DEVICE_TYPE_KEYBOARD;
908
		workitem->reports_supported |= STD_KEYBOARD | MULTIMEDIA |
909
					       POWER_KEYS | HIDPP;
910 911 912 913 914 915 916
		break;
	default:
		hid_warn(hdev, "%s: unexpected device-index %d", __func__,
			 hidpp_report->device_index);
	}
}

917 918 919 920 921 922 923 924 925 926 927 928 929 930
static void logi_hidpp_recv_queue_notif(struct hid_device *hdev,
					struct hidpp_event *hidpp_report)
{
	/* We are called from atomic context (tasklet && djrcv->lock held) */
	struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
	const char *device_type = "UNKNOWN";
	struct dj_workitem workitem = {
		.type = WORKITEM_TYPE_EMPTY,
		.device_index = hidpp_report->device_index,
	};

	switch (hidpp_report->params[HIDPP_PARAM_PROTO_TYPE]) {
	case 0x01:
		device_type = "Bluetooth";
931
		/* Bluetooth connect packet contents is the same as (e)QUAD */
932
		logi_hidpp_dev_conn_notif_equad(hdev, hidpp_report, &workitem);
933 934 935 936 937 938
		if (!(hidpp_report->params[HIDPP_PARAM_DEVICE_INFO] &
						HIDPP_MANUFACTURER_MASK)) {
			hid_info(hdev, "Non Logitech device connected on slot %d\n",
				 hidpp_report->device_index);
			workitem.reports_supported &= ~HIDPP;
		}
939 940 941
		break;
	case 0x02:
		device_type = "27 Mhz";
942
		logi_hidpp_dev_conn_notif_27mhz(hdev, hidpp_report, &workitem);
943 944 945
		break;
	case 0x03:
		device_type = "QUAD or eQUAD";
946
		logi_hidpp_dev_conn_notif_equad(hdev, hidpp_report, &workitem);
947 948 949
		break;
	case 0x04:
		device_type = "eQUAD step 4 DJ";
950
		logi_hidpp_dev_conn_notif_equad(hdev, hidpp_report, &workitem);
951 952 953 954 955 956
		break;
	case 0x05:
		device_type = "DFU Lite";
		break;
	case 0x06:
		device_type = "eQUAD step 4 Lite";
957
		logi_hidpp_dev_conn_notif_equad(hdev, hidpp_report, &workitem);
958 959 960 961 962 963 964 965 966
		break;
	case 0x07:
		device_type = "eQUAD step 4 Gaming";
		break;
	case 0x08:
		device_type = "eQUAD step 4 for gamepads";
		break;
	case 0x0a:
		device_type = "eQUAD nano Lite";
967
		logi_hidpp_dev_conn_notif_equad(hdev, hidpp_report, &workitem);
968 969 970
		break;
	case 0x0c:
		device_type = "eQUAD Lightspeed";
971
		logi_hidpp_dev_conn_notif_equad(hdev, hidpp_report, &workitem);
972
		workitem.reports_supported |= STD_KEYBOARD;
973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989
		break;
	}

	if (workitem.type == WORKITEM_TYPE_EMPTY) {
		hid_warn(hdev,
			 "unusable device of type %s (0x%02x) connected on slot %d",
			 device_type,
			 hidpp_report->params[HIDPP_PARAM_PROTO_TYPE],
			 hidpp_report->device_index);
		return;
	}

	hid_info(hdev, "device of type %s (0x%02x) connected on slot %d",
		 device_type, hidpp_report->params[HIDPP_PARAM_PROTO_TYPE],
		 hidpp_report->device_index);

	kfifo_in(&djrcv_dev->notif_fifo, &workitem, sizeof(workitem));
990
	schedule_work(&djrcv_dev->work);
991 992
}

993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018
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];

	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");
			}
		}
	}
}

1019 1020
static void logi_dj_recv_forward_dj(struct dj_receiver_dev *djrcv_dev,
				    struct dj_report *dj_report)
1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039
{
	/* 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_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");
	}
}

1040 1041
static void logi_dj_recv_forward_report(struct dj_device *dj_dev, u8 *data,
					int size)
1042 1043 1044 1045 1046
{
	/* We are called from atomic context (tasklet && djrcv->lock held) */
	if (hid_input_report(dj_dev->hdev, HID_INPUT_REPORT, data, size, 1))
		dbg_hid("hid_input_report error\n");
}
1047

1048 1049 1050 1051 1052 1053 1054 1055 1056 1057
static void logi_dj_recv_forward_input_report(struct hid_device *hdev,
					      u8 *data, int size)
{
	struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
	struct dj_device *dj_dev;
	unsigned long flags;
	u8 report = data[0];
	int i;

	if (report > REPORT_TYPE_RFREPORT_LAST) {
1058
		hid_err(hdev, "Unexpected input report number %d\n", report);
1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077
		return;
	}

	spin_lock_irqsave(&djrcv_dev->lock, flags);
	for (i = 0; i < (DJ_MAX_PAIRED_DEVICES + DJ_DEVICE_INDEX_MIN); i++) {
		dj_dev = djrcv_dev->paired_dj_devices[i];
		if (dj_dev && (dj_dev->reports_supported & BIT(report))) {
			logi_dj_recv_forward_report(dj_dev, data, size);
			spin_unlock_irqrestore(&djrcv_dev->lock, flags);
			return;
		}
	}

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

	dbg_hid("No dj-devs handling input report number %d\n", report);
}

1078 1079 1080
static int logi_dj_recv_send_report(struct dj_receiver_dev *djrcv_dev,
				    struct dj_report *dj_report)
{
1081
	struct hid_device *hdev = djrcv_dev->hidpp;
1082 1083 1084
	struct hid_report *report;
	struct hid_report_enum *output_report_enum;
	u8 *data = (u8 *)(&dj_report->device_index);
1085
	unsigned int i;
1086

1087 1088 1089 1090
	output_report_enum = &hdev->report_enum[HID_OUTPUT_REPORT];
	report = output_report_enum->report_id_hash[REPORT_ID_DJ_SHORT];

	if (!report) {
1091
		hid_err(hdev, "%s: unable to find dj report\n", __func__);
1092 1093 1094
		return -ENODEV;
	}

1095
	for (i = 0; i < DJREPORT_SHORT_LENGTH - 1; i++)
1096 1097
		report->field[0]->value[i] = data[i];

1098
	hid_hw_request(hdev, report, HID_REQ_SET_REPORT);
1099

1100
	return 0;
1101 1102
}

1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127
static int logi_dj_recv_query_hidpp_devices(struct dj_receiver_dev *djrcv_dev)
{
	const u8 template[] = {REPORT_ID_HIDPP_SHORT,
			       HIDPP_RECEIVER_INDEX,
			       HIDPP_SET_REGISTER,
			       HIDPP_REG_CONNECTION_STATE,
			       HIDPP_FAKE_DEVICE_ARRIVAL,
			       0x00, 0x00};
	u8 *hidpp_report;
	int retval;

	hidpp_report = kmemdup(template, sizeof(template), GFP_KERNEL);
	if (!hidpp_report)
		return -ENOMEM;

	retval = hid_hw_raw_request(djrcv_dev->hidpp,
				    REPORT_ID_HIDPP_SHORT,
				    hidpp_report, sizeof(template),
				    HID_OUTPUT_REPORT,
				    HID_REQ_SET_REPORT);

	kfree(hidpp_report);
	return 0;
}

1128 1129
static int logi_dj_recv_query_paired_devices(struct dj_receiver_dev *djrcv_dev)
{
1130 1131
	struct dj_report *dj_report;
	int retval;
1132

1133 1134
	djrcv_dev->last_query = jiffies;

1135 1136 1137
	if (djrcv_dev->type != recvr_type_dj)
		return logi_dj_recv_query_hidpp_devices(djrcv_dev);

Alan Cox's avatar
Alan Cox committed
1138
	dj_report = kzalloc(sizeof(struct dj_report), GFP_KERNEL);
1139 1140 1141 1142 1143 1144 1145 1146
	if (!dj_report)
		return -ENOMEM;
	dj_report->report_id = REPORT_ID_DJ_SHORT;
	dj_report->device_index = 0xFF;
	dj_report->report_type = REPORT_TYPE_CMD_GET_PAIRED_DEVICES;
	retval = logi_dj_recv_send_report(djrcv_dev, dj_report);
	kfree(dj_report);
	return retval;
1147 1148
}

1149

1150 1151 1152
static int logi_dj_recv_switch_to_dj_mode(struct dj_receiver_dev *djrcv_dev,
					  unsigned timeout)
{
1153
	struct hid_device *hdev = djrcv_dev->hidpp;
1154
	struct dj_report *dj_report;
1155
	u8 *buf;
1156
	int retval = 0;
1157

Alan Cox's avatar
Alan Cox committed
1158
	dj_report = kzalloc(sizeof(struct dj_report), GFP_KERNEL);
1159 1160
	if (!dj_report)
		return -ENOMEM;
1161

1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179
	if (djrcv_dev->type == recvr_type_dj) {
		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] = 0x3F;
		dj_report->report_params[CMD_SWITCH_PARAM_TIMEOUT_SECONDS] =
								(u8)timeout;

		retval = logi_dj_recv_send_report(djrcv_dev, dj_report);

		/*
		 * Ugly sleep to work around a USB 3.0 bug when the receiver is
		 * still processing the "switch-to-dj" command while we send an
		 * other command.
		 * 50 msec should gives enough time to the receiver to be ready.
		 */
		msleep(50);
	}
1180

1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204
	/*
	 * Magical bits to set up hidpp notifications when the dj devices
	 * are connected/disconnected.
	 *
	 * We can reuse dj_report because HIDPP_REPORT_SHORT_LENGTH is smaller
	 * than DJREPORT_SHORT_LENGTH.
	 */
	buf = (u8 *)dj_report;

	memset(buf, 0, HIDPP_REPORT_SHORT_LENGTH);

	buf[0] = REPORT_ID_HIDPP_SHORT;
	buf[1] = 0xFF;
	buf[2] = 0x80;
	buf[3] = 0x00;
	buf[4] = 0x00;
	buf[5] = 0x09;
	buf[6] = 0x00;

	hid_hw_raw_request(hdev, REPORT_ID_HIDPP_SHORT, buf,
			HIDPP_REPORT_SHORT_LENGTH, HID_OUTPUT_REPORT,
			HID_REQ_SET_REPORT);

	kfree(dj_report);
1205
	return retval;
1206 1207 1208 1209 1210
}


static int logi_dj_ll_open(struct hid_device *hid)
{
1211
	dbg_hid("%s: %s\n", __func__, hid->phys);
1212 1213 1214 1215 1216 1217
	return 0;

}

static void logi_dj_ll_close(struct hid_device *hid)
{
1218
	dbg_hid("%s: %s\n", __func__, hid->phys);
1219 1220
}

1221 1222 1223 1224
/*
 * Register 0xB5 is "pairing information". It is solely intended for the
 * receiver, so do not overwrite the device index.
 */
1225 1226 1227 1228 1229 1230 1231 1232
static u8 unifying_pairing_query[]  = { REPORT_ID_HIDPP_SHORT,
					HIDPP_RECEIVER_INDEX,
					HIDPP_GET_LONG_REGISTER,
					HIDPP_REG_PAIRING_INFORMATION };
static u8 unifying_pairing_answer[] = { REPORT_ID_HIDPP_LONG,
					HIDPP_RECEIVER_INDEX,
					HIDPP_GET_LONG_REGISTER,
					HIDPP_REG_PAIRING_INFORMATION };
1233

1234 1235 1236 1237
static int logi_dj_ll_raw_request(struct hid_device *hid,
				  unsigned char reportnum, __u8 *buf,
				  size_t count, unsigned char report_type,
				  int reqtype)
1238
{
1239 1240 1241 1242
	struct dj_device *djdev = hid->driver_data;
	struct dj_receiver_dev *djrcv_dev = djdev->dj_receiver_dev;
	u8 *out_buf;
	int ret;
1243

1244 1245 1246 1247 1248
	if ((buf[0] == REPORT_ID_HIDPP_SHORT) ||
	    (buf[0] == REPORT_ID_HIDPP_LONG)) {
		if (count < 2)
			return -EINVAL;

1249 1250
		/* special case where we should not overwrite
		 * the device_index */
1251 1252 1253
		if (count == 7 && !memcmp(buf, unifying_pairing_query,
					  sizeof(unifying_pairing_query)))
			buf[4] = (buf[4] & 0xf0) | (djdev->device_index - 1);
Benjamin Tissoires's avatar