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