Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 1 | /* |
| 2 | * HID driver for Logitech Unifying receivers |
| 3 | * |
| 4 | * Copyright (c) 2011 Logitech |
| 5 | */ |
| 6 | |
| 7 | /* |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License version 2 as |
| 10 | * published by the Free Software Foundation. |
| 11 | |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 21 | * |
| 22 | */ |
| 23 | |
| 24 | |
| 25 | #include <linux/device.h> |
| 26 | #include <linux/hid.h> |
| 27 | #include <linux/module.h> |
| 28 | #include <linux/usb.h> |
Benjamin Tissoires | 8cb3746 | 2014-09-30 13:18:26 -0400 | [diff] [blame] | 29 | #include <linux/kfifo.h> |
Jonathan Nieder | 44d27f7 | 2012-05-11 16:17:16 +0200 | [diff] [blame] | 30 | #include <asm/unaligned.h> |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 31 | #include "hid-ids.h" |
Benjamin Tissoires | 8cb3746 | 2014-09-30 13:18:26 -0400 | [diff] [blame] | 32 | |
| 33 | #define DJ_MAX_PAIRED_DEVICES 6 |
| 34 | #define DJ_MAX_NUMBER_NOTIFICATIONS 8 |
| 35 | #define DJ_RECEIVER_INDEX 0 |
| 36 | #define DJ_DEVICE_INDEX_MIN 1 |
| 37 | #define DJ_DEVICE_INDEX_MAX 6 |
| 38 | |
| 39 | #define DJREPORT_SHORT_LENGTH 15 |
| 40 | #define DJREPORT_LONG_LENGTH 32 |
| 41 | |
| 42 | #define REPORT_ID_DJ_SHORT 0x20 |
| 43 | #define REPORT_ID_DJ_LONG 0x21 |
| 44 | |
| 45 | #define REPORT_TYPE_RFREPORT_LAST 0x1F |
| 46 | |
| 47 | /* Command Switch to DJ mode */ |
| 48 | #define REPORT_TYPE_CMD_SWITCH 0x80 |
| 49 | #define CMD_SWITCH_PARAM_DEVBITFIELD 0x00 |
| 50 | #define CMD_SWITCH_PARAM_TIMEOUT_SECONDS 0x01 |
| 51 | #define TIMEOUT_NO_KEEPALIVE 0x00 |
| 52 | |
| 53 | /* Command to Get the list of Paired devices */ |
| 54 | #define REPORT_TYPE_CMD_GET_PAIRED_DEVICES 0x81 |
| 55 | |
| 56 | /* Device Paired Notification */ |
| 57 | #define REPORT_TYPE_NOTIF_DEVICE_PAIRED 0x41 |
| 58 | #define SPFUNCTION_MORE_NOTIF_EXPECTED 0x01 |
| 59 | #define SPFUNCTION_DEVICE_LIST_EMPTY 0x02 |
| 60 | #define DEVICE_PAIRED_PARAM_SPFUNCTION 0x00 |
| 61 | #define DEVICE_PAIRED_PARAM_EQUAD_ID_LSB 0x01 |
| 62 | #define DEVICE_PAIRED_PARAM_EQUAD_ID_MSB 0x02 |
| 63 | #define DEVICE_PAIRED_RF_REPORT_TYPE 0x03 |
| 64 | |
| 65 | /* Device Un-Paired Notification */ |
| 66 | #define REPORT_TYPE_NOTIF_DEVICE_UNPAIRED 0x40 |
| 67 | |
| 68 | |
| 69 | /* Connection Status Notification */ |
| 70 | #define REPORT_TYPE_NOTIF_CONNECTION_STATUS 0x42 |
| 71 | #define CONNECTION_STATUS_PARAM_STATUS 0x00 |
| 72 | #define STATUS_LINKLOSS 0x01 |
| 73 | |
| 74 | /* Error Notification */ |
| 75 | #define REPORT_TYPE_NOTIF_ERROR 0x7F |
| 76 | #define NOTIF_ERROR_PARAM_ETYPE 0x00 |
| 77 | #define ETYPE_KEEPALIVE_TIMEOUT 0x01 |
| 78 | |
| 79 | /* supported DJ HID && RF report types */ |
| 80 | #define REPORT_TYPE_KEYBOARD 0x01 |
| 81 | #define REPORT_TYPE_MOUSE 0x02 |
| 82 | #define REPORT_TYPE_CONSUMER_CONTROL 0x03 |
| 83 | #define REPORT_TYPE_SYSTEM_CONTROL 0x04 |
| 84 | #define REPORT_TYPE_MEDIA_CENTER 0x08 |
| 85 | #define REPORT_TYPE_LEDS 0x0E |
| 86 | |
| 87 | /* RF Report types bitfield */ |
| 88 | #define STD_KEYBOARD 0x00000002 |
| 89 | #define STD_MOUSE 0x00000004 |
| 90 | #define MULTIMEDIA 0x00000008 |
| 91 | #define POWER_KEYS 0x00000010 |
| 92 | #define MEDIA_CENTER 0x00000100 |
| 93 | #define KBD_LEDS 0x00004000 |
| 94 | |
| 95 | struct dj_report { |
| 96 | u8 report_id; |
| 97 | u8 device_index; |
| 98 | u8 report_type; |
| 99 | u8 report_params[DJREPORT_SHORT_LENGTH - 3]; |
| 100 | }; |
| 101 | |
| 102 | struct dj_receiver_dev { |
| 103 | struct hid_device *hdev; |
| 104 | struct dj_device *paired_dj_devices[DJ_MAX_PAIRED_DEVICES + |
| 105 | DJ_DEVICE_INDEX_MIN]; |
| 106 | struct work_struct work; |
| 107 | struct kfifo notif_fifo; |
| 108 | spinlock_t lock; |
| 109 | bool querying_devices; |
| 110 | }; |
| 111 | |
| 112 | struct dj_device { |
| 113 | struct hid_device *hdev; |
| 114 | struct dj_receiver_dev *dj_receiver_dev; |
| 115 | u32 reports_supported; |
| 116 | u8 device_index; |
| 117 | }; |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 118 | |
| 119 | /* Keyboard descriptor (1) */ |
| 120 | static const char kbd_descriptor[] = { |
| 121 | 0x05, 0x01, /* USAGE_PAGE (generic Desktop) */ |
| 122 | 0x09, 0x06, /* USAGE (Keyboard) */ |
| 123 | 0xA1, 0x01, /* COLLECTION (Application) */ |
| 124 | 0x85, 0x01, /* REPORT_ID (1) */ |
| 125 | 0x95, 0x08, /* REPORT_COUNT (8) */ |
| 126 | 0x75, 0x01, /* REPORT_SIZE (1) */ |
| 127 | 0x15, 0x00, /* LOGICAL_MINIMUM (0) */ |
| 128 | 0x25, 0x01, /* LOGICAL_MAXIMUM (1) */ |
| 129 | 0x05, 0x07, /* USAGE_PAGE (Keyboard) */ |
| 130 | 0x19, 0xE0, /* USAGE_MINIMUM (Left Control) */ |
| 131 | 0x29, 0xE7, /* USAGE_MAXIMUM (Right GUI) */ |
| 132 | 0x81, 0x02, /* INPUT (Data,Var,Abs) */ |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 133 | 0x95, 0x06, /* REPORT_COUNT (6) */ |
| 134 | 0x75, 0x08, /* REPORT_SIZE (8) */ |
| 135 | 0x15, 0x00, /* LOGICAL_MINIMUM (0) */ |
| 136 | 0x26, 0xFF, 0x00, /* LOGICAL_MAXIMUM (255) */ |
| 137 | 0x05, 0x07, /* USAGE_PAGE (Keyboard) */ |
| 138 | 0x19, 0x00, /* USAGE_MINIMUM (no event) */ |
| 139 | 0x2A, 0xFF, 0x00, /* USAGE_MAXIMUM (reserved) */ |
| 140 | 0x81, 0x00, /* INPUT (Data,Ary,Abs) */ |
Benjamin Tissoires | 0e40d35 | 2014-02-05 16:33:17 -0500 | [diff] [blame] | 141 | 0x85, 0x0e, /* REPORT_ID (14) */ |
| 142 | 0x05, 0x08, /* USAGE PAGE (LED page) */ |
| 143 | 0x95, 0x05, /* REPORT COUNT (5) */ |
| 144 | 0x75, 0x01, /* REPORT SIZE (1) */ |
| 145 | 0x15, 0x00, /* LOGICAL_MINIMUM (0) */ |
| 146 | 0x25, 0x01, /* LOGICAL_MAXIMUM (1) */ |
| 147 | 0x19, 0x01, /* USAGE MINIMUM (1) */ |
| 148 | 0x29, 0x05, /* USAGE MAXIMUM (5) */ |
| 149 | 0x91, 0x02, /* OUTPUT (Data, Variable, Absolute) */ |
| 150 | 0x95, 0x01, /* REPORT COUNT (1) */ |
| 151 | 0x75, 0x03, /* REPORT SIZE (3) */ |
| 152 | 0x91, 0x01, /* OUTPUT (Constant) */ |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 153 | 0xC0 |
| 154 | }; |
| 155 | |
| 156 | /* Mouse descriptor (2) */ |
| 157 | static const char mse_descriptor[] = { |
| 158 | 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */ |
| 159 | 0x09, 0x02, /* USAGE (Mouse) */ |
| 160 | 0xA1, 0x01, /* COLLECTION (Application) */ |
| 161 | 0x85, 0x02, /* REPORT_ID = 2 */ |
| 162 | 0x09, 0x01, /* USAGE (pointer) */ |
| 163 | 0xA1, 0x00, /* COLLECTION (physical) */ |
| 164 | 0x05, 0x09, /* USAGE_PAGE (buttons) */ |
| 165 | 0x19, 0x01, /* USAGE_MIN (1) */ |
| 166 | 0x29, 0x10, /* USAGE_MAX (16) */ |
| 167 | 0x15, 0x00, /* LOGICAL_MIN (0) */ |
| 168 | 0x25, 0x01, /* LOGICAL_MAX (1) */ |
| 169 | 0x95, 0x10, /* REPORT_COUNT (16) */ |
| 170 | 0x75, 0x01, /* REPORT_SIZE (1) */ |
| 171 | 0x81, 0x02, /* INPUT (data var abs) */ |
| 172 | 0x05, 0x01, /* USAGE_PAGE (generic desktop) */ |
| 173 | 0x16, 0x01, 0xF8, /* LOGICAL_MIN (-2047) */ |
| 174 | 0x26, 0xFF, 0x07, /* LOGICAL_MAX (2047) */ |
| 175 | 0x75, 0x0C, /* REPORT_SIZE (12) */ |
| 176 | 0x95, 0x02, /* REPORT_COUNT (2) */ |
| 177 | 0x09, 0x30, /* USAGE (X) */ |
| 178 | 0x09, 0x31, /* USAGE (Y) */ |
| 179 | 0x81, 0x06, /* INPUT */ |
| 180 | 0x15, 0x81, /* LOGICAL_MIN (-127) */ |
| 181 | 0x25, 0x7F, /* LOGICAL_MAX (127) */ |
| 182 | 0x75, 0x08, /* REPORT_SIZE (8) */ |
| 183 | 0x95, 0x01, /* REPORT_COUNT (1) */ |
| 184 | 0x09, 0x38, /* USAGE (wheel) */ |
| 185 | 0x81, 0x06, /* INPUT */ |
| 186 | 0x05, 0x0C, /* USAGE_PAGE(consumer) */ |
| 187 | 0x0A, 0x38, 0x02, /* USAGE(AC Pan) */ |
| 188 | 0x95, 0x01, /* REPORT_COUNT (1) */ |
| 189 | 0x81, 0x06, /* INPUT */ |
| 190 | 0xC0, /* END_COLLECTION */ |
| 191 | 0xC0, /* END_COLLECTION */ |
| 192 | }; |
| 193 | |
| 194 | /* Consumer Control descriptor (3) */ |
| 195 | static const char consumer_descriptor[] = { |
| 196 | 0x05, 0x0C, /* USAGE_PAGE (Consumer Devices) */ |
| 197 | 0x09, 0x01, /* USAGE (Consumer Control) */ |
| 198 | 0xA1, 0x01, /* COLLECTION (Application) */ |
| 199 | 0x85, 0x03, /* REPORT_ID = 3 */ |
| 200 | 0x75, 0x10, /* REPORT_SIZE (16) */ |
| 201 | 0x95, 0x02, /* REPORT_COUNT (2) */ |
| 202 | 0x15, 0x01, /* LOGICAL_MIN (1) */ |
| 203 | 0x26, 0x8C, 0x02, /* LOGICAL_MAX (652) */ |
| 204 | 0x19, 0x01, /* USAGE_MIN (1) */ |
| 205 | 0x2A, 0x8C, 0x02, /* USAGE_MAX (652) */ |
| 206 | 0x81, 0x00, /* INPUT (Data Ary Abs) */ |
| 207 | 0xC0, /* END_COLLECTION */ |
| 208 | }; /* */ |
| 209 | |
| 210 | /* System control descriptor (4) */ |
| 211 | static const char syscontrol_descriptor[] = { |
| 212 | 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */ |
| 213 | 0x09, 0x80, /* USAGE (System Control) */ |
| 214 | 0xA1, 0x01, /* COLLECTION (Application) */ |
| 215 | 0x85, 0x04, /* REPORT_ID = 4 */ |
| 216 | 0x75, 0x02, /* REPORT_SIZE (2) */ |
| 217 | 0x95, 0x01, /* REPORT_COUNT (1) */ |
| 218 | 0x15, 0x01, /* LOGICAL_MIN (1) */ |
| 219 | 0x25, 0x03, /* LOGICAL_MAX (3) */ |
| 220 | 0x09, 0x82, /* USAGE (System Sleep) */ |
| 221 | 0x09, 0x81, /* USAGE (System Power Down) */ |
| 222 | 0x09, 0x83, /* USAGE (System Wake Up) */ |
| 223 | 0x81, 0x60, /* INPUT (Data Ary Abs NPrf Null) */ |
| 224 | 0x75, 0x06, /* REPORT_SIZE (6) */ |
| 225 | 0x81, 0x03, /* INPUT (Cnst Var Abs) */ |
| 226 | 0xC0, /* END_COLLECTION */ |
| 227 | }; |
| 228 | |
| 229 | /* Media descriptor (8) */ |
| 230 | static const char media_descriptor[] = { |
| 231 | 0x06, 0xbc, 0xff, /* Usage Page 0xffbc */ |
| 232 | 0x09, 0x88, /* Usage 0x0088 */ |
| 233 | 0xa1, 0x01, /* BeginCollection */ |
| 234 | 0x85, 0x08, /* Report ID 8 */ |
| 235 | 0x19, 0x01, /* Usage Min 0x0001 */ |
| 236 | 0x29, 0xff, /* Usage Max 0x00ff */ |
| 237 | 0x15, 0x01, /* Logical Min 1 */ |
| 238 | 0x26, 0xff, 0x00, /* Logical Max 255 */ |
| 239 | 0x75, 0x08, /* Report Size 8 */ |
| 240 | 0x95, 0x01, /* Report Count 1 */ |
| 241 | 0x81, 0x00, /* Input */ |
| 242 | 0xc0, /* EndCollection */ |
| 243 | }; /* */ |
| 244 | |
| 245 | /* Maximum size of all defined hid reports in bytes (including report id) */ |
| 246 | #define MAX_REPORT_SIZE 8 |
| 247 | |
Henrik Rydberg | 2a039bf | 2012-04-22 14:21:39 +0200 | [diff] [blame] | 248 | /* Make sure all descriptors are present here */ |
| 249 | #define MAX_RDESC_SIZE \ |
| 250 | (sizeof(kbd_descriptor) + \ |
| 251 | sizeof(mse_descriptor) + \ |
| 252 | sizeof(consumer_descriptor) + \ |
| 253 | sizeof(syscontrol_descriptor) + \ |
| 254 | sizeof(media_descriptor)) |
| 255 | |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 256 | /* Number of possible hid report types that can be created by this driver. |
| 257 | * |
| 258 | * Right now, RF report types have the same report types (or report id's) |
| 259 | * than the hid report created from those RF reports. In the future |
| 260 | * this doesnt have to be true. |
| 261 | * |
| 262 | * For instance, RF report type 0x01 which has a size of 8 bytes, corresponds |
| 263 | * to hid report id 0x01, this is standard keyboard. Same thing applies to mice |
| 264 | * reports and consumer control, etc. If a new RF report is created, it doesn't |
| 265 | * has to have the same report id as its corresponding hid report, so an |
| 266 | * translation may have to take place for future report types. |
| 267 | */ |
| 268 | #define NUMBER_OF_HID_REPORTS 32 |
| 269 | static const u8 hid_reportid_size_map[NUMBER_OF_HID_REPORTS] = { |
| 270 | [1] = 8, /* Standard keyboard */ |
| 271 | [2] = 8, /* Standard mouse */ |
| 272 | [3] = 5, /* Consumer control */ |
| 273 | [4] = 2, /* System control */ |
| 274 | [8] = 2, /* Media Center */ |
| 275 | }; |
| 276 | |
| 277 | |
| 278 | #define LOGITECH_DJ_INTERFACE_NUMBER 0x02 |
| 279 | |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 280 | static struct hid_ll_driver logi_dj_ll_driver; |
| 281 | |
Nestor Lopez Casado | c63e0e3 | 2013-07-18 06:21:30 -0700 | [diff] [blame] | 282 | static int logi_dj_recv_query_paired_devices(struct dj_receiver_dev *djrcv_dev); |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 283 | |
| 284 | static void logi_dj_recv_destroy_djhid_device(struct dj_receiver_dev *djrcv_dev, |
| 285 | struct dj_report *dj_report) |
| 286 | { |
| 287 | /* Called in delayed work context */ |
| 288 | struct dj_device *dj_dev; |
| 289 | unsigned long flags; |
| 290 | |
| 291 | spin_lock_irqsave(&djrcv_dev->lock, flags); |
| 292 | dj_dev = djrcv_dev->paired_dj_devices[dj_report->device_index]; |
| 293 | djrcv_dev->paired_dj_devices[dj_report->device_index] = NULL; |
| 294 | spin_unlock_irqrestore(&djrcv_dev->lock, flags); |
| 295 | |
| 296 | if (dj_dev != NULL) { |
| 297 | hid_destroy_device(dj_dev->hdev); |
| 298 | kfree(dj_dev); |
| 299 | } else { |
| 300 | dev_err(&djrcv_dev->hdev->dev, "%s: can't destroy a NULL device\n", |
| 301 | __func__); |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | static void logi_dj_recv_add_djhid_device(struct dj_receiver_dev *djrcv_dev, |
| 306 | struct dj_report *dj_report) |
| 307 | { |
| 308 | /* Called in delayed work context */ |
| 309 | struct hid_device *djrcv_hdev = djrcv_dev->hdev; |
| 310 | struct usb_interface *intf = to_usb_interface(djrcv_hdev->dev.parent); |
| 311 | struct usb_device *usbdev = interface_to_usbdev(intf); |
| 312 | struct hid_device *dj_hiddev; |
| 313 | struct dj_device *dj_dev; |
| 314 | |
| 315 | /* Device index goes from 1 to 6, we need 3 bytes to store the |
| 316 | * semicolon, the index, and a null terminator |
| 317 | */ |
| 318 | unsigned char tmpstr[3]; |
| 319 | |
| 320 | if (dj_report->report_params[DEVICE_PAIRED_PARAM_SPFUNCTION] & |
| 321 | SPFUNCTION_DEVICE_LIST_EMPTY) { |
| 322 | dbg_hid("%s: device list is empty\n", __func__); |
Nestor Lopez Casado | c63e0e3 | 2013-07-18 06:21:30 -0700 | [diff] [blame] | 323 | djrcv_dev->querying_devices = false; |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 324 | return; |
| 325 | } |
| 326 | |
Nestor Lopez Casado | c63e0e3 | 2013-07-18 06:21:30 -0700 | [diff] [blame] | 327 | if (djrcv_dev->paired_dj_devices[dj_report->device_index]) { |
| 328 | /* The device is already known. No need to reallocate it. */ |
| 329 | dbg_hid("%s: device is already known\n", __func__); |
| 330 | return; |
| 331 | } |
| 332 | |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 333 | dj_hiddev = hid_allocate_device(); |
| 334 | if (IS_ERR(dj_hiddev)) { |
| 335 | dev_err(&djrcv_hdev->dev, "%s: hid_allocate_device failed\n", |
| 336 | __func__); |
| 337 | return; |
| 338 | } |
| 339 | |
| 340 | dj_hiddev->ll_driver = &logi_dj_ll_driver; |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 341 | |
| 342 | dj_hiddev->dev.parent = &djrcv_hdev->dev; |
| 343 | dj_hiddev->bus = BUS_USB; |
| 344 | dj_hiddev->vendor = le16_to_cpu(usbdev->descriptor.idVendor); |
Benjamin Tissoires | d610274 | 2014-09-30 13:18:25 -0400 | [diff] [blame] | 345 | dj_hiddev->product = |
| 346 | (dj_report->report_params[DEVICE_PAIRED_PARAM_EQUAD_ID_MSB] |
| 347 | << 8) | |
| 348 | dj_report->report_params[DEVICE_PAIRED_PARAM_EQUAD_ID_LSB]; |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 349 | snprintf(dj_hiddev->name, sizeof(dj_hiddev->name), |
Benjamin Tissoires | d610274 | 2014-09-30 13:18:25 -0400 | [diff] [blame] | 350 | "Logitech Unifying Device. Wireless PID:%04x", |
| 351 | dj_hiddev->product); |
| 352 | |
| 353 | dj_hiddev->group = HID_GROUP_LOGITECH_DJ_DEVICE; |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 354 | |
| 355 | usb_make_path(usbdev, dj_hiddev->phys, sizeof(dj_hiddev->phys)); |
| 356 | snprintf(tmpstr, sizeof(tmpstr), ":%d", dj_report->device_index); |
| 357 | strlcat(dj_hiddev->phys, tmpstr, sizeof(dj_hiddev->phys)); |
| 358 | |
| 359 | dj_dev = kzalloc(sizeof(struct dj_device), GFP_KERNEL); |
| 360 | |
| 361 | if (!dj_dev) { |
| 362 | dev_err(&djrcv_hdev->dev, "%s: failed allocating dj_device\n", |
| 363 | __func__); |
| 364 | goto dj_device_allocate_fail; |
| 365 | } |
| 366 | |
Jonathan Nieder | 44d27f7 | 2012-05-11 16:17:16 +0200 | [diff] [blame] | 367 | dj_dev->reports_supported = get_unaligned_le32( |
| 368 | dj_report->report_params + DEVICE_PAIRED_RF_REPORT_TYPE); |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 369 | dj_dev->hdev = dj_hiddev; |
| 370 | dj_dev->dj_receiver_dev = djrcv_dev; |
| 371 | dj_dev->device_index = dj_report->device_index; |
| 372 | dj_hiddev->driver_data = dj_dev; |
| 373 | |
| 374 | djrcv_dev->paired_dj_devices[dj_report->device_index] = dj_dev; |
| 375 | |
| 376 | if (hid_add_device(dj_hiddev)) { |
| 377 | dev_err(&djrcv_hdev->dev, "%s: failed adding dj_device\n", |
| 378 | __func__); |
| 379 | goto hid_add_device_fail; |
| 380 | } |
| 381 | |
| 382 | return; |
| 383 | |
| 384 | hid_add_device_fail: |
| 385 | djrcv_dev->paired_dj_devices[dj_report->device_index] = NULL; |
| 386 | kfree(dj_dev); |
| 387 | dj_device_allocate_fail: |
| 388 | hid_destroy_device(dj_hiddev); |
| 389 | } |
| 390 | |
| 391 | static void delayedwork_callback(struct work_struct *work) |
| 392 | { |
| 393 | struct dj_receiver_dev *djrcv_dev = |
| 394 | container_of(work, struct dj_receiver_dev, work); |
| 395 | |
| 396 | struct dj_report dj_report; |
| 397 | unsigned long flags; |
| 398 | int count; |
Nestor Lopez Casado | c63e0e3 | 2013-07-18 06:21:30 -0700 | [diff] [blame] | 399 | int retval; |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 400 | |
| 401 | dbg_hid("%s\n", __func__); |
| 402 | |
| 403 | spin_lock_irqsave(&djrcv_dev->lock, flags); |
| 404 | |
| 405 | count = kfifo_out(&djrcv_dev->notif_fifo, &dj_report, |
| 406 | sizeof(struct dj_report)); |
| 407 | |
| 408 | if (count != sizeof(struct dj_report)) { |
| 409 | dev_err(&djrcv_dev->hdev->dev, "%s: workitem triggered without " |
| 410 | "notifications available\n", __func__); |
| 411 | spin_unlock_irqrestore(&djrcv_dev->lock, flags); |
| 412 | return; |
| 413 | } |
| 414 | |
| 415 | if (!kfifo_is_empty(&djrcv_dev->notif_fifo)) { |
| 416 | if (schedule_work(&djrcv_dev->work) == 0) { |
| 417 | dbg_hid("%s: did not schedule the work item, was " |
| 418 | "already queued\n", __func__); |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | spin_unlock_irqrestore(&djrcv_dev->lock, flags); |
| 423 | |
| 424 | switch (dj_report.report_type) { |
| 425 | case REPORT_TYPE_NOTIF_DEVICE_PAIRED: |
| 426 | logi_dj_recv_add_djhid_device(djrcv_dev, &dj_report); |
| 427 | break; |
| 428 | case REPORT_TYPE_NOTIF_DEVICE_UNPAIRED: |
| 429 | logi_dj_recv_destroy_djhid_device(djrcv_dev, &dj_report); |
| 430 | break; |
| 431 | default: |
Nestor Lopez Casado | c63e0e3 | 2013-07-18 06:21:30 -0700 | [diff] [blame] | 432 | /* A normal report (i. e. not belonging to a pair/unpair notification) |
| 433 | * arriving here, means that the report arrived but we did not have a |
| 434 | * paired dj_device associated to the report's device_index, this |
| 435 | * means that the original "device paired" notification corresponding |
| 436 | * to this dj_device never arrived to this driver. The reason is that |
| 437 | * hid-core discards all packets coming from a device while probe() is |
| 438 | * executing. */ |
| 439 | if (!djrcv_dev->paired_dj_devices[dj_report.device_index]) { |
| 440 | /* ok, we don't know the device, just re-ask the |
| 441 | * receiver for the list of connected devices. */ |
| 442 | retval = logi_dj_recv_query_paired_devices(djrcv_dev); |
| 443 | if (!retval) { |
| 444 | /* everything went fine, so just leave */ |
| 445 | break; |
| 446 | } |
| 447 | dev_err(&djrcv_dev->hdev->dev, |
| 448 | "%s:logi_dj_recv_query_paired_devices " |
| 449 | "error:%d\n", __func__, retval); |
| 450 | } |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 451 | dbg_hid("%s: unexpected report type\n", __func__); |
| 452 | } |
| 453 | } |
| 454 | |
| 455 | static void logi_dj_recv_queue_notification(struct dj_receiver_dev *djrcv_dev, |
| 456 | struct dj_report *dj_report) |
| 457 | { |
| 458 | /* We are called from atomic context (tasklet && djrcv->lock held) */ |
| 459 | |
| 460 | kfifo_in(&djrcv_dev->notif_fifo, dj_report, sizeof(struct dj_report)); |
| 461 | |
| 462 | if (schedule_work(&djrcv_dev->work) == 0) { |
| 463 | dbg_hid("%s: did not schedule the work item, was already " |
| 464 | "queued\n", __func__); |
| 465 | } |
| 466 | } |
| 467 | |
| 468 | static void logi_dj_recv_forward_null_report(struct dj_receiver_dev *djrcv_dev, |
| 469 | struct dj_report *dj_report) |
| 470 | { |
| 471 | /* We are called from atomic context (tasklet && djrcv->lock held) */ |
| 472 | unsigned int i; |
| 473 | u8 reportbuffer[MAX_REPORT_SIZE]; |
| 474 | struct dj_device *djdev; |
| 475 | |
| 476 | djdev = djrcv_dev->paired_dj_devices[dj_report->device_index]; |
| 477 | |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 478 | memset(reportbuffer, 0, sizeof(reportbuffer)); |
| 479 | |
| 480 | for (i = 0; i < NUMBER_OF_HID_REPORTS; i++) { |
| 481 | if (djdev->reports_supported & (1 << i)) { |
| 482 | reportbuffer[0] = i; |
| 483 | if (hid_input_report(djdev->hdev, |
| 484 | HID_INPUT_REPORT, |
| 485 | reportbuffer, |
| 486 | hid_reportid_size_map[i], 1)) { |
| 487 | dbg_hid("hid_input_report error sending null " |
| 488 | "report\n"); |
| 489 | } |
| 490 | } |
| 491 | } |
| 492 | } |
| 493 | |
| 494 | static void logi_dj_recv_forward_report(struct dj_receiver_dev *djrcv_dev, |
| 495 | struct dj_report *dj_report) |
| 496 | { |
| 497 | /* We are called from atomic context (tasklet && djrcv->lock held) */ |
| 498 | struct dj_device *dj_device; |
| 499 | |
| 500 | dj_device = djrcv_dev->paired_dj_devices[dj_report->device_index]; |
| 501 | |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 502 | if ((dj_report->report_type > ARRAY_SIZE(hid_reportid_size_map) - 1) || |
| 503 | (hid_reportid_size_map[dj_report->report_type] == 0)) { |
| 504 | dbg_hid("invalid report type:%x\n", dj_report->report_type); |
| 505 | return; |
| 506 | } |
| 507 | |
| 508 | if (hid_input_report(dj_device->hdev, |
| 509 | HID_INPUT_REPORT, &dj_report->report_type, |
| 510 | hid_reportid_size_map[dj_report->report_type], 1)) { |
| 511 | dbg_hid("hid_input_report error\n"); |
| 512 | } |
| 513 | } |
| 514 | |
| 515 | |
| 516 | static int logi_dj_recv_send_report(struct dj_receiver_dev *djrcv_dev, |
| 517 | struct dj_report *dj_report) |
| 518 | { |
| 519 | struct hid_device *hdev = djrcv_dev->hdev; |
Benjamin Tissoires | dcd9006 | 2013-03-05 17:09:00 +0100 | [diff] [blame] | 520 | struct hid_report *report; |
| 521 | struct hid_report_enum *output_report_enum; |
| 522 | u8 *data = (u8 *)(&dj_report->device_index); |
Kees Cook | 297502a | 2013-09-11 21:56:56 +0200 | [diff] [blame] | 523 | unsigned int i; |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 524 | |
Benjamin Tissoires | dcd9006 | 2013-03-05 17:09:00 +0100 | [diff] [blame] | 525 | output_report_enum = &hdev->report_enum[HID_OUTPUT_REPORT]; |
| 526 | report = output_report_enum->report_id_hash[REPORT_ID_DJ_SHORT]; |
| 527 | |
| 528 | if (!report) { |
| 529 | dev_err(&hdev->dev, "%s: unable to find dj report\n", __func__); |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 530 | return -ENODEV; |
| 531 | } |
| 532 | |
Kees Cook | 297502a | 2013-09-11 21:56:56 +0200 | [diff] [blame] | 533 | for (i = 0; i < DJREPORT_SHORT_LENGTH - 1; i++) |
Benjamin Tissoires | dcd9006 | 2013-03-05 17:09:00 +0100 | [diff] [blame] | 534 | report->field[0]->value[i] = data[i]; |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 535 | |
Jiri Kosina | 83a44ac | 2013-03-09 10:58:13 +0100 | [diff] [blame] | 536 | hid_hw_request(hdev, report, HID_REQ_SET_REPORT); |
Benjamin Tissoires | dcd9006 | 2013-03-05 17:09:00 +0100 | [diff] [blame] | 537 | |
| 538 | return 0; |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 539 | } |
| 540 | |
| 541 | static int logi_dj_recv_query_paired_devices(struct dj_receiver_dev *djrcv_dev) |
| 542 | { |
Marc Dionne | d8dc349 | 2012-06-01 18:12:14 -0400 | [diff] [blame] | 543 | struct dj_report *dj_report; |
| 544 | int retval; |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 545 | |
Nestor Lopez Casado | c63e0e3 | 2013-07-18 06:21:30 -0700 | [diff] [blame] | 546 | /* no need to protect djrcv_dev->querying_devices */ |
| 547 | if (djrcv_dev->querying_devices) |
| 548 | return 0; |
| 549 | |
Alan Cox | 8a55ade | 2012-09-04 15:10:08 +0100 | [diff] [blame] | 550 | dj_report = kzalloc(sizeof(struct dj_report), GFP_KERNEL); |
Marc Dionne | d8dc349 | 2012-06-01 18:12:14 -0400 | [diff] [blame] | 551 | if (!dj_report) |
| 552 | return -ENOMEM; |
| 553 | dj_report->report_id = REPORT_ID_DJ_SHORT; |
| 554 | dj_report->device_index = 0xFF; |
| 555 | dj_report->report_type = REPORT_TYPE_CMD_GET_PAIRED_DEVICES; |
| 556 | retval = logi_dj_recv_send_report(djrcv_dev, dj_report); |
| 557 | kfree(dj_report); |
| 558 | return retval; |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 559 | } |
| 560 | |
Nestor Lopez Casado | c63e0e3 | 2013-07-18 06:21:30 -0700 | [diff] [blame] | 561 | |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 562 | static int logi_dj_recv_switch_to_dj_mode(struct dj_receiver_dev *djrcv_dev, |
| 563 | unsigned timeout) |
| 564 | { |
Marc Dionne | d8dc349 | 2012-06-01 18:12:14 -0400 | [diff] [blame] | 565 | struct dj_report *dj_report; |
| 566 | int retval; |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 567 | |
Alan Cox | 8a55ade | 2012-09-04 15:10:08 +0100 | [diff] [blame] | 568 | dj_report = kzalloc(sizeof(struct dj_report), GFP_KERNEL); |
Marc Dionne | d8dc349 | 2012-06-01 18:12:14 -0400 | [diff] [blame] | 569 | if (!dj_report) |
| 570 | return -ENOMEM; |
| 571 | dj_report->report_id = REPORT_ID_DJ_SHORT; |
| 572 | dj_report->device_index = 0xFF; |
| 573 | dj_report->report_type = REPORT_TYPE_CMD_SWITCH; |
| 574 | dj_report->report_params[CMD_SWITCH_PARAM_DEVBITFIELD] = 0x3F; |
| 575 | dj_report->report_params[CMD_SWITCH_PARAM_TIMEOUT_SECONDS] = (u8)timeout; |
| 576 | retval = logi_dj_recv_send_report(djrcv_dev, dj_report); |
| 577 | kfree(dj_report); |
Benjamin Tisssoires | 42c22db | 2014-01-08 17:18:45 -0500 | [diff] [blame] | 578 | |
| 579 | /* |
| 580 | * Ugly sleep to work around a USB 3.0 bug when the receiver is still |
| 581 | * processing the "switch-to-dj" command while we send an other command. |
| 582 | * 50 msec should gives enough time to the receiver to be ready. |
| 583 | */ |
| 584 | msleep(50); |
| 585 | |
Marc Dionne | d8dc349 | 2012-06-01 18:12:14 -0400 | [diff] [blame] | 586 | return retval; |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 587 | } |
| 588 | |
| 589 | |
| 590 | static int logi_dj_ll_open(struct hid_device *hid) |
| 591 | { |
| 592 | dbg_hid("%s:%s\n", __func__, hid->phys); |
| 593 | return 0; |
| 594 | |
| 595 | } |
| 596 | |
| 597 | static void logi_dj_ll_close(struct hid_device *hid) |
| 598 | { |
| 599 | dbg_hid("%s:%s\n", __func__, hid->phys); |
| 600 | } |
| 601 | |
Benjamin Tissoires | bd27e20 | 2014-02-10 12:58:53 -0500 | [diff] [blame] | 602 | static int logi_dj_ll_raw_request(struct hid_device *hid, |
| 603 | unsigned char reportnum, __u8 *buf, |
| 604 | size_t count, unsigned char report_type, |
| 605 | int reqtype) |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 606 | { |
Benjamin Tissoires | 0e40d35 | 2014-02-05 16:33:17 -0500 | [diff] [blame] | 607 | struct dj_device *djdev = hid->driver_data; |
| 608 | struct dj_receiver_dev *djrcv_dev = djdev->dj_receiver_dev; |
| 609 | u8 *out_buf; |
| 610 | int ret; |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 611 | |
Benjamin Tissoires | 0e40d35 | 2014-02-05 16:33:17 -0500 | [diff] [blame] | 612 | if (buf[0] != REPORT_TYPE_LEDS) |
| 613 | return -EINVAL; |
| 614 | |
| 615 | out_buf = kzalloc(DJREPORT_SHORT_LENGTH, GFP_ATOMIC); |
| 616 | if (!out_buf) |
| 617 | return -ENOMEM; |
| 618 | |
Jiri Kosina | 51217e6 | 2014-08-21 09:56:47 -0500 | [diff] [blame] | 619 | if (count > DJREPORT_SHORT_LENGTH - 2) |
Benjamin Tissoires | 0e40d35 | 2014-02-05 16:33:17 -0500 | [diff] [blame] | 620 | count = DJREPORT_SHORT_LENGTH - 2; |
| 621 | |
| 622 | out_buf[0] = REPORT_ID_DJ_SHORT; |
| 623 | out_buf[1] = djdev->device_index; |
| 624 | memcpy(out_buf + 2, buf, count); |
| 625 | |
Benjamin Tissoires | 0e40d35 | 2014-02-05 16:33:17 -0500 | [diff] [blame] | 626 | ret = hid_hw_raw_request(djrcv_dev->hdev, out_buf[0], out_buf, |
Benjamin Tissoires | bd27e20 | 2014-02-10 12:58:53 -0500 | [diff] [blame] | 627 | DJREPORT_SHORT_LENGTH, report_type, reqtype); |
Benjamin Tissoires | 0e40d35 | 2014-02-05 16:33:17 -0500 | [diff] [blame] | 628 | |
| 629 | kfree(out_buf); |
| 630 | return ret; |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 631 | } |
| 632 | |
Dan Carpenter | 6d60332 | 2013-10-19 12:08:59 +0300 | [diff] [blame] | 633 | static void rdcat(char *rdesc, unsigned int *rsize, const char *data, unsigned int size) |
Henrik Rydberg | 2a039bf | 2012-04-22 14:21:39 +0200 | [diff] [blame] | 634 | { |
Dan Carpenter | 6d60332 | 2013-10-19 12:08:59 +0300 | [diff] [blame] | 635 | memcpy(rdesc + *rsize, data, size); |
Henrik Rydberg | 2a039bf | 2012-04-22 14:21:39 +0200 | [diff] [blame] | 636 | *rsize += size; |
| 637 | } |
| 638 | |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 639 | static int logi_dj_ll_parse(struct hid_device *hid) |
| 640 | { |
| 641 | struct dj_device *djdev = hid->driver_data; |
Henrik Rydberg | 2a039bf | 2012-04-22 14:21:39 +0200 | [diff] [blame] | 642 | unsigned int rsize = 0; |
| 643 | char *rdesc; |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 644 | int retval; |
| 645 | |
| 646 | dbg_hid("%s\n", __func__); |
| 647 | |
| 648 | djdev->hdev->version = 0x0111; |
| 649 | djdev->hdev->country = 0x00; |
| 650 | |
Henrik Rydberg | 2a039bf | 2012-04-22 14:21:39 +0200 | [diff] [blame] | 651 | rdesc = kmalloc(MAX_RDESC_SIZE, GFP_KERNEL); |
| 652 | if (!rdesc) |
| 653 | return -ENOMEM; |
| 654 | |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 655 | if (djdev->reports_supported & STD_KEYBOARD) { |
| 656 | dbg_hid("%s: sending a kbd descriptor, reports_supported: %x\n", |
| 657 | __func__, djdev->reports_supported); |
Dan Carpenter | 6d60332 | 2013-10-19 12:08:59 +0300 | [diff] [blame] | 658 | rdcat(rdesc, &rsize, kbd_descriptor, sizeof(kbd_descriptor)); |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 659 | } |
| 660 | |
| 661 | if (djdev->reports_supported & STD_MOUSE) { |
| 662 | dbg_hid("%s: sending a mouse descriptor, reports_supported: " |
| 663 | "%x\n", __func__, djdev->reports_supported); |
Dan Carpenter | 6d60332 | 2013-10-19 12:08:59 +0300 | [diff] [blame] | 664 | rdcat(rdesc, &rsize, mse_descriptor, sizeof(mse_descriptor)); |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 665 | } |
| 666 | |
| 667 | if (djdev->reports_supported & MULTIMEDIA) { |
| 668 | dbg_hid("%s: sending a multimedia report descriptor: %x\n", |
| 669 | __func__, djdev->reports_supported); |
Dan Carpenter | 6d60332 | 2013-10-19 12:08:59 +0300 | [diff] [blame] | 670 | rdcat(rdesc, &rsize, consumer_descriptor, sizeof(consumer_descriptor)); |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 671 | } |
| 672 | |
| 673 | if (djdev->reports_supported & POWER_KEYS) { |
| 674 | dbg_hid("%s: sending a power keys report descriptor: %x\n", |
| 675 | __func__, djdev->reports_supported); |
Dan Carpenter | 6d60332 | 2013-10-19 12:08:59 +0300 | [diff] [blame] | 676 | rdcat(rdesc, &rsize, syscontrol_descriptor, sizeof(syscontrol_descriptor)); |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 677 | } |
| 678 | |
| 679 | if (djdev->reports_supported & MEDIA_CENTER) { |
| 680 | dbg_hid("%s: sending a media center report descriptor: %x\n", |
| 681 | __func__, djdev->reports_supported); |
Dan Carpenter | 6d60332 | 2013-10-19 12:08:59 +0300 | [diff] [blame] | 682 | rdcat(rdesc, &rsize, media_descriptor, sizeof(media_descriptor)); |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 683 | } |
| 684 | |
| 685 | if (djdev->reports_supported & KBD_LEDS) { |
| 686 | dbg_hid("%s: need to send kbd leds report descriptor: %x\n", |
| 687 | __func__, djdev->reports_supported); |
| 688 | } |
| 689 | |
Henrik Rydberg | 2a039bf | 2012-04-22 14:21:39 +0200 | [diff] [blame] | 690 | retval = hid_parse_report(hid, rdesc, rsize); |
| 691 | kfree(rdesc); |
| 692 | |
| 693 | return retval; |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 694 | } |
| 695 | |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 696 | static int logi_dj_ll_start(struct hid_device *hid) |
| 697 | { |
| 698 | dbg_hid("%s\n", __func__); |
| 699 | return 0; |
| 700 | } |
| 701 | |
| 702 | static void logi_dj_ll_stop(struct hid_device *hid) |
| 703 | { |
| 704 | dbg_hid("%s\n", __func__); |
| 705 | } |
| 706 | |
| 707 | |
| 708 | static struct hid_ll_driver logi_dj_ll_driver = { |
| 709 | .parse = logi_dj_ll_parse, |
| 710 | .start = logi_dj_ll_start, |
| 711 | .stop = logi_dj_ll_stop, |
| 712 | .open = logi_dj_ll_open, |
| 713 | .close = logi_dj_ll_close, |
Benjamin Tissoires | bd27e20 | 2014-02-10 12:58:53 -0500 | [diff] [blame] | 714 | .raw_request = logi_dj_ll_raw_request, |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 715 | }; |
| 716 | |
| 717 | |
| 718 | static int logi_dj_raw_event(struct hid_device *hdev, |
| 719 | struct hid_report *report, u8 *data, |
| 720 | int size) |
| 721 | { |
| 722 | struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev); |
| 723 | struct dj_report *dj_report = (struct dj_report *) data; |
| 724 | unsigned long flags; |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 725 | |
| 726 | dbg_hid("%s, size:%d\n", __func__, size); |
| 727 | |
| 728 | /* Here we receive all data coming from iface 2, there are 4 cases: |
| 729 | * |
| 730 | * 1) Data should continue its normal processing i.e. data does not |
| 731 | * come from the DJ collection, in which case we do nothing and |
| 732 | * return 0, so hid-core can continue normal processing (will forward |
| 733 | * to associated hidraw device) |
| 734 | * |
| 735 | * 2) Data is from DJ collection, and is intended for this driver i. e. |
| 736 | * data contains arrival, departure, etc notifications, in which case |
| 737 | * we queue them for delayed processing by the work queue. We return 1 |
| 738 | * to hid-core as no further processing is required from it. |
| 739 | * |
| 740 | * 3) Data is from DJ collection, and informs a connection change, |
| 741 | * if the change means rf link loss, then we must send a null report |
| 742 | * to the upper layer to discard potentially pressed keys that may be |
| 743 | * repeated forever by the input layer. Return 1 to hid-core as no |
| 744 | * further processing is required. |
| 745 | * |
| 746 | * 4) Data is from DJ collection and is an actual input event from |
| 747 | * a paired DJ device in which case we forward it to the correct hid |
| 748 | * device (via hid_input_report() ) and return 1 so hid-core does not do |
| 749 | * anything else with it. |
| 750 | */ |
Benjamin Tissoires | 5abfe85 | 2014-08-22 16:16:05 -0400 | [diff] [blame] | 751 | |
| 752 | /* case 1) */ |
| 753 | if (data[0] != REPORT_ID_DJ_SHORT) |
| 754 | return false; |
| 755 | |
Jiri Kosina | ad3e14d | 2014-08-21 09:57:17 -0500 | [diff] [blame] | 756 | if ((dj_report->device_index < DJ_DEVICE_INDEX_MIN) || |
| 757 | (dj_report->device_index > DJ_DEVICE_INDEX_MAX)) { |
Benjamin Tissoires | 5abfe85 | 2014-08-22 16:16:05 -0400 | [diff] [blame] | 758 | /* |
| 759 | * Device index is wrong, bail out. |
| 760 | * This driver can ignore safely the receiver notifications, |
| 761 | * so ignore those reports too. |
| 762 | */ |
| 763 | if (dj_report->device_index != DJ_RECEIVER_INDEX) |
| 764 | dev_err(&hdev->dev, "%s: invalid device index:%d\n", |
Jiri Kosina | ad3e14d | 2014-08-21 09:57:17 -0500 | [diff] [blame] | 765 | __func__, dj_report->device_index); |
| 766 | return false; |
| 767 | } |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 768 | |
| 769 | spin_lock_irqsave(&djrcv_dev->lock, flags); |
Benjamin Tissoires | 368d4e5 | 2014-08-22 16:16:06 -0400 | [diff] [blame] | 770 | |
| 771 | if (!djrcv_dev->paired_dj_devices[dj_report->device_index]) { |
| 772 | /* received an event for an unknown device, bail out */ |
| 773 | logi_dj_recv_queue_notification(djrcv_dev, dj_report); |
| 774 | goto out; |
| 775 | } |
| 776 | |
Benjamin Tissoires | 5abfe85 | 2014-08-22 16:16:05 -0400 | [diff] [blame] | 777 | switch (dj_report->report_type) { |
| 778 | case REPORT_TYPE_NOTIF_DEVICE_PAIRED: |
Benjamin Tissoires | 368d4e5 | 2014-08-22 16:16:06 -0400 | [diff] [blame] | 779 | /* pairing notifications are handled above the switch */ |
| 780 | break; |
Benjamin Tissoires | 5abfe85 | 2014-08-22 16:16:05 -0400 | [diff] [blame] | 781 | case REPORT_TYPE_NOTIF_DEVICE_UNPAIRED: |
| 782 | logi_dj_recv_queue_notification(djrcv_dev, dj_report); |
| 783 | break; |
| 784 | case REPORT_TYPE_NOTIF_CONNECTION_STATUS: |
| 785 | if (dj_report->report_params[CONNECTION_STATUS_PARAM_STATUS] == |
| 786 | STATUS_LINKLOSS) { |
| 787 | logi_dj_recv_forward_null_report(djrcv_dev, dj_report); |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 788 | } |
Benjamin Tissoires | 5abfe85 | 2014-08-22 16:16:05 -0400 | [diff] [blame] | 789 | break; |
| 790 | default: |
| 791 | logi_dj_recv_forward_report(djrcv_dev, dj_report); |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 792 | } |
Benjamin Tissoires | 368d4e5 | 2014-08-22 16:16:06 -0400 | [diff] [blame] | 793 | |
| 794 | out: |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 795 | spin_unlock_irqrestore(&djrcv_dev->lock, flags); |
| 796 | |
Benjamin Tissoires | 5abfe85 | 2014-08-22 16:16:05 -0400 | [diff] [blame] | 797 | return true; |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 798 | } |
| 799 | |
| 800 | static int logi_dj_probe(struct hid_device *hdev, |
| 801 | const struct hid_device_id *id) |
| 802 | { |
| 803 | struct usb_interface *intf = to_usb_interface(hdev->dev.parent); |
| 804 | struct dj_receiver_dev *djrcv_dev; |
| 805 | int retval; |
| 806 | |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 807 | dbg_hid("%s called for ifnum %d\n", __func__, |
| 808 | intf->cur_altsetting->desc.bInterfaceNumber); |
| 809 | |
| 810 | /* Ignore interfaces 0 and 1, they will not carry any data, dont create |
| 811 | * any hid_device for them */ |
| 812 | if (intf->cur_altsetting->desc.bInterfaceNumber != |
| 813 | LOGITECH_DJ_INTERFACE_NUMBER) { |
| 814 | dbg_hid("%s: ignoring ifnum %d\n", __func__, |
| 815 | intf->cur_altsetting->desc.bInterfaceNumber); |
| 816 | return -ENODEV; |
| 817 | } |
| 818 | |
| 819 | /* Treat interface 2 */ |
| 820 | |
| 821 | djrcv_dev = kzalloc(sizeof(struct dj_receiver_dev), GFP_KERNEL); |
| 822 | if (!djrcv_dev) { |
| 823 | dev_err(&hdev->dev, |
| 824 | "%s:failed allocating dj_receiver_dev\n", __func__); |
| 825 | return -ENOMEM; |
| 826 | } |
| 827 | djrcv_dev->hdev = hdev; |
| 828 | INIT_WORK(&djrcv_dev->work, delayedwork_callback); |
| 829 | spin_lock_init(&djrcv_dev->lock); |
| 830 | if (kfifo_alloc(&djrcv_dev->notif_fifo, |
| 831 | DJ_MAX_NUMBER_NOTIFICATIONS * sizeof(struct dj_report), |
| 832 | GFP_KERNEL)) { |
| 833 | dev_err(&hdev->dev, |
| 834 | "%s:failed allocating notif_fifo\n", __func__); |
| 835 | kfree(djrcv_dev); |
| 836 | return -ENOMEM; |
| 837 | } |
| 838 | hid_set_drvdata(hdev, djrcv_dev); |
| 839 | |
| 840 | /* Call to usbhid to fetch the HID descriptors of interface 2 and |
| 841 | * subsequently call to the hid/hid-core to parse the fetched |
| 842 | * descriptors, this will in turn create the hidraw and hiddev nodes |
| 843 | * for interface 2 of the receiver */ |
| 844 | retval = hid_parse(hdev); |
| 845 | if (retval) { |
| 846 | dev_err(&hdev->dev, |
| 847 | "%s:parse of interface 2 failed\n", __func__); |
| 848 | goto hid_parse_fail; |
| 849 | } |
| 850 | |
Kees Cook | 297502a | 2013-09-11 21:56:56 +0200 | [diff] [blame] | 851 | if (!hid_validate_values(hdev, HID_OUTPUT_REPORT, REPORT_ID_DJ_SHORT, |
| 852 | 0, DJREPORT_SHORT_LENGTH - 1)) { |
| 853 | retval = -ENODEV; |
| 854 | goto hid_parse_fail; |
| 855 | } |
| 856 | |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 857 | /* Starts the usb device and connects to upper interfaces hiddev and |
| 858 | * hidraw */ |
| 859 | retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT); |
| 860 | if (retval) { |
| 861 | dev_err(&hdev->dev, |
| 862 | "%s:hid_hw_start returned error\n", __func__); |
| 863 | goto hid_hw_start_fail; |
| 864 | } |
| 865 | |
| 866 | retval = logi_dj_recv_switch_to_dj_mode(djrcv_dev, 0); |
| 867 | if (retval < 0) { |
| 868 | dev_err(&hdev->dev, |
| 869 | "%s:logi_dj_recv_switch_to_dj_mode returned error:%d\n", |
| 870 | __func__, retval); |
| 871 | goto switch_to_dj_mode_fail; |
| 872 | } |
| 873 | |
| 874 | /* This is enabling the polling urb on the IN endpoint */ |
Benjamin Tissoires | ddf7540e | 2013-07-12 11:01:02 +0200 | [diff] [blame] | 875 | retval = hid_hw_open(hdev); |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 876 | if (retval < 0) { |
Benjamin Tissoires | ddf7540e | 2013-07-12 11:01:02 +0200 | [diff] [blame] | 877 | dev_err(&hdev->dev, "%s:hid_hw_open returned error:%d\n", |
| 878 | __func__, retval); |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 879 | goto llopen_failed; |
| 880 | } |
| 881 | |
Andrew de los Reyes | a9dd22b7 | 2013-02-18 09:20:22 -0800 | [diff] [blame] | 882 | /* Allow incoming packets to arrive: */ |
| 883 | hid_device_io_start(hdev); |
| 884 | |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 885 | retval = logi_dj_recv_query_paired_devices(djrcv_dev); |
| 886 | if (retval < 0) { |
| 887 | dev_err(&hdev->dev, "%s:logi_dj_recv_query_paired_devices " |
| 888 | "error:%d\n", __func__, retval); |
| 889 | goto logi_dj_recv_query_paired_devices_failed; |
| 890 | } |
| 891 | |
| 892 | return retval; |
| 893 | |
| 894 | logi_dj_recv_query_paired_devices_failed: |
Benjamin Tissoires | ddf7540e | 2013-07-12 11:01:02 +0200 | [diff] [blame] | 895 | hid_hw_close(hdev); |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 896 | |
| 897 | llopen_failed: |
| 898 | switch_to_dj_mode_fail: |
| 899 | hid_hw_stop(hdev); |
| 900 | |
| 901 | hid_hw_start_fail: |
| 902 | hid_parse_fail: |
| 903 | kfifo_free(&djrcv_dev->notif_fifo); |
| 904 | kfree(djrcv_dev); |
| 905 | hid_set_drvdata(hdev, NULL); |
| 906 | return retval; |
| 907 | |
| 908 | } |
| 909 | |
| 910 | #ifdef CONFIG_PM |
| 911 | static int logi_dj_reset_resume(struct hid_device *hdev) |
| 912 | { |
| 913 | int retval; |
| 914 | struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev); |
| 915 | |
| 916 | retval = logi_dj_recv_switch_to_dj_mode(djrcv_dev, 0); |
| 917 | if (retval < 0) { |
| 918 | dev_err(&hdev->dev, |
| 919 | "%s:logi_dj_recv_switch_to_dj_mode returned error:%d\n", |
| 920 | __func__, retval); |
| 921 | } |
| 922 | |
| 923 | return 0; |
| 924 | } |
| 925 | #endif |
| 926 | |
| 927 | static void logi_dj_remove(struct hid_device *hdev) |
| 928 | { |
| 929 | struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev); |
| 930 | struct dj_device *dj_dev; |
| 931 | int i; |
| 932 | |
| 933 | dbg_hid("%s\n", __func__); |
| 934 | |
| 935 | cancel_work_sync(&djrcv_dev->work); |
| 936 | |
Benjamin Tissoires | ddf7540e | 2013-07-12 11:01:02 +0200 | [diff] [blame] | 937 | hid_hw_close(hdev); |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 938 | hid_hw_stop(hdev); |
| 939 | |
| 940 | /* I suppose that at this point the only context that can access |
| 941 | * the djrecv_data is this thread as the work item is guaranteed to |
| 942 | * have finished and no more raw_event callbacks should arrive after |
| 943 | * the remove callback was triggered so no locks are put around the |
| 944 | * code below */ |
Nestor Lopez Casado | 844580f | 2011-09-20 15:59:03 +0200 | [diff] [blame] | 945 | for (i = 0; i < (DJ_MAX_PAIRED_DEVICES + DJ_DEVICE_INDEX_MIN); i++) { |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 946 | dj_dev = djrcv_dev->paired_dj_devices[i]; |
| 947 | if (dj_dev != NULL) { |
| 948 | hid_destroy_device(dj_dev->hdev); |
| 949 | kfree(dj_dev); |
| 950 | djrcv_dev->paired_dj_devices[i] = NULL; |
| 951 | } |
| 952 | } |
| 953 | |
| 954 | kfifo_free(&djrcv_dev->notif_fifo); |
| 955 | kfree(djrcv_dev); |
| 956 | hid_set_drvdata(hdev, NULL); |
| 957 | } |
| 958 | |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 959 | static const struct hid_device_id logi_dj_receivers[] = { |
| 960 | {HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, |
| 961 | USB_DEVICE_ID_LOGITECH_UNIFYING_RECEIVER)}, |
| 962 | {HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, |
| 963 | USB_DEVICE_ID_LOGITECH_UNIFYING_RECEIVER_2)}, |
| 964 | {} |
| 965 | }; |
| 966 | |
| 967 | MODULE_DEVICE_TABLE(hid, logi_dj_receivers); |
| 968 | |
| 969 | static struct hid_driver logi_djreceiver_driver = { |
| 970 | .name = "logitech-djreceiver", |
| 971 | .id_table = logi_dj_receivers, |
| 972 | .probe = logi_dj_probe, |
| 973 | .remove = logi_dj_remove, |
| 974 | .raw_event = logi_dj_raw_event, |
| 975 | #ifdef CONFIG_PM |
| 976 | .reset_resume = logi_dj_reset_resume, |
| 977 | #endif |
| 978 | }; |
| 979 | |
Benjamin Tissoires | ab94e56 | 2014-09-30 13:18:28 -0400 | [diff] [blame^] | 980 | module_hid_driver(logi_djreceiver_driver); |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 981 | |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 982 | MODULE_LICENSE("GPL"); |
| 983 | MODULE_AUTHOR("Logitech"); |
| 984 | MODULE_AUTHOR("Nestor Lopez Casado"); |
| 985 | MODULE_AUTHOR("nlopezcasad@logitech.com"); |