linux/drivers/hid/intel-ish-hid/ishtp-hid.c
<<
>>
Prefs
   1/*
   2 * ISHTP-HID glue driver.
   3 *
   4 * Copyright (c) 2012-2016, Intel Corporation.
   5 *
   6 * This program is free software; you can redistribute it and/or modify it
   7 * under the terms and conditions of the GNU General Public License,
   8 * version 2, as published by the Free Software Foundation.
   9 *
  10 * This program is distributed in the hope it will be useful, but WITHOUT
  11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13 * more details.
  14 */
  15
  16#include <linux/hid.h>
  17#include <uapi/linux/input.h>
  18#include <linux/sched.h>
  19#include <linux/wait.h>
  20#include "ishtp/client.h"
  21#include "ishtp-hid.h"
  22
  23/**
  24 * ishtp_hid_parse() - hid-core .parse() callback
  25 * @hid:        hid device instance
  26 *
  27 * This function gets called during call to hid_add_device
  28 *
  29 * Return: 0 on success and non zero on error
  30 */
  31static int ishtp_hid_parse(struct hid_device *hid)
  32{
  33        struct ishtp_hid_data *hid_data =  hid->driver_data;
  34        struct ishtp_cl_data *client_data = hid_data->client_data;
  35        int rv;
  36
  37        rv = hid_parse_report(hid, client_data->report_descr[hid_data->index],
  38                              client_data->report_descr_size[hid_data->index]);
  39        if (rv)
  40                return  rv;
  41
  42        return 0;
  43}
  44
  45/* Empty callbacks with success return code */
  46static int ishtp_hid_start(struct hid_device *hid)
  47{
  48        return 0;
  49}
  50
  51static void ishtp_hid_stop(struct hid_device *hid)
  52{
  53}
  54
  55static int ishtp_hid_open(struct hid_device *hid)
  56{
  57        return 0;
  58}
  59
  60static void ishtp_hid_close(struct hid_device *hid)
  61{
  62}
  63
  64static int ishtp_raw_request(struct hid_device *hdev, unsigned char reportnum,
  65        __u8 *buf, size_t len, unsigned char rtype, int reqtype)
  66{
  67        return 0;
  68}
  69
  70/**
  71 * ishtp_hid_request() - hid-core .request() callback
  72 * @hid:        hid device instance
  73 * @rep:        pointer to hid_report
  74 * @reqtype:    type of req. [GET|SET]_REPORT
  75 *
  76 * This function is used to set/get feaure/input report.
  77 */
  78static void ishtp_hid_request(struct hid_device *hid, struct hid_report *rep,
  79        int reqtype)
  80{
  81        struct ishtp_hid_data *hid_data =  hid->driver_data;
  82        /* the specific report length, just HID part of it */
  83        unsigned int len = ((rep->size - 1) >> 3) + 1 + (rep->id > 0);
  84        char *buf;
  85        unsigned int header_size = sizeof(struct hostif_msg);
  86
  87        len += header_size;
  88
  89        hid_data->request_done = false;
  90        switch (reqtype) {
  91        case HID_REQ_GET_REPORT:
  92                hid_ishtp_get_report(hid, rep->id, rep->type);
  93                break;
  94        case HID_REQ_SET_REPORT:
  95                /*
  96                 * Spare 7 bytes for 64b accesses through
  97                 * get/put_unaligned_le64()
  98                 */
  99                buf = kzalloc(len + 7, GFP_KERNEL);
 100                if (!buf)
 101                        return;
 102
 103                hid_output_report(rep, buf + header_size);
 104                hid_ishtp_set_feature(hid, buf, len, rep->id);
 105                kfree(buf);
 106                break;
 107        }
 108}
 109
 110/**
 111 * ishtp_wait_for_response() - hid-core .wait() callback
 112 * @hid:        hid device instance
 113 *
 114 * This function is used to wait after get feaure/input report.
 115 *
 116 * Return: 0 on success and non zero on error
 117 */
 118static int ishtp_wait_for_response(struct hid_device *hid)
 119{
 120        struct ishtp_hid_data *hid_data =  hid->driver_data;
 121        struct ishtp_cl_data *client_data = hid_data->client_data;
 122        int rv;
 123
 124        hid_ishtp_trace(client_data,  "%s hid %p\n", __func__, hid);
 125
 126        rv = ishtp_hid_link_ready_wait(hid_data->client_data);
 127        if (rv)
 128                return rv;
 129
 130        if (!hid_data->request_done)
 131                wait_event_interruptible_timeout(hid_data->hid_wait,
 132                                        hid_data->request_done, 3 * HZ);
 133
 134        if (!hid_data->request_done) {
 135                hid_err(hid,
 136                        "timeout waiting for response from ISHTP device\n");
 137                return -ETIMEDOUT;
 138        }
 139        hid_ishtp_trace(client_data,  "%s hid %p done\n", __func__, hid);
 140
 141        hid_data->request_done = false;
 142
 143        return 0;
 144}
 145
 146/**
 147 * ishtp_hid_wakeup() - Wakeup caller
 148 * @hid:        hid device instance
 149 *
 150 * This function will wakeup caller waiting for Get/Set feature report
 151 */
 152void ishtp_hid_wakeup(struct hid_device *hid)
 153{
 154        struct ishtp_hid_data *hid_data = hid->driver_data;
 155
 156        hid_data->request_done = true;
 157        wake_up_interruptible(&hid_data->hid_wait);
 158}
 159
 160static struct hid_ll_driver ishtp_hid_ll_driver = {
 161        .parse = ishtp_hid_parse,
 162        .start = ishtp_hid_start,
 163        .stop = ishtp_hid_stop,
 164        .open = ishtp_hid_open,
 165        .close = ishtp_hid_close,
 166        .request = ishtp_hid_request,
 167        .wait = ishtp_wait_for_response,
 168        .raw_request = ishtp_raw_request
 169};
 170
 171/**
 172 * ishtp_hid_probe() - hid register ll driver
 173 * @cur_hid_dev:        Index of hid device calling to register
 174 * @client_data:        Client data pointer
 175 *
 176 * This function is used to allocate and add HID device.
 177 *
 178 * Return: 0 on success, non zero on error
 179 */
 180int ishtp_hid_probe(unsigned int cur_hid_dev,
 181                    struct ishtp_cl_data *client_data)
 182{
 183        int rv;
 184        struct hid_device *hid;
 185        struct ishtp_hid_data *hid_data;
 186
 187        hid = hid_allocate_device();
 188        if (IS_ERR(hid)) {
 189                rv = PTR_ERR(hid);
 190                return  -ENOMEM;
 191        }
 192
 193        hid_data = kzalloc(sizeof(*hid_data), GFP_KERNEL);
 194        if (!hid_data) {
 195                rv = -ENOMEM;
 196                goto err_hid_data;
 197        }
 198
 199        hid_data->index = cur_hid_dev;
 200        hid_data->client_data = client_data;
 201        init_waitqueue_head(&hid_data->hid_wait);
 202
 203        hid->driver_data = hid_data;
 204
 205        client_data->hid_sensor_hubs[cur_hid_dev] = hid;
 206
 207        hid->ll_driver = &ishtp_hid_ll_driver;
 208        hid->bus = BUS_INTEL_ISHTP;
 209        hid->dev.parent = &client_data->cl_device->dev;
 210        hid->version = le16_to_cpu(ISH_HID_VERSION);
 211        hid->vendor = le16_to_cpu(ISH_HID_VENDOR);
 212        hid->product = le16_to_cpu(ISH_HID_PRODUCT);
 213        snprintf(hid->name, sizeof(hid->name), "%s %04X:%04X", "hid-ishtp",
 214                hid->vendor, hid->product);
 215
 216        rv = hid_add_device(hid);
 217        if (rv)
 218                goto err_hid_device;
 219
 220        hid_ishtp_trace(client_data,  "%s allocated hid %p\n", __func__, hid);
 221
 222        return 0;
 223
 224err_hid_device:
 225        kfree(hid_data);
 226err_hid_data:
 227        hid_destroy_device(hid);
 228        return rv;
 229}
 230
 231/**
 232 * ishtp_hid_probe() - Remove registered hid device
 233 * @client_data:        client data pointer
 234 *
 235 * This function is used to destroy allocatd HID device.
 236 */
 237void ishtp_hid_remove(struct ishtp_cl_data *client_data)
 238{
 239        int i;
 240
 241        for (i = 0; i < client_data->num_hid_devices; ++i) {
 242                if (client_data->hid_sensor_hubs[i]) {
 243                        kfree(client_data->hid_sensor_hubs[i]->driver_data);
 244                        hid_destroy_device(client_data->hid_sensor_hubs[i]);
 245                        client_data->hid_sensor_hubs[i] = NULL;
 246                }
 247        }
 248}
 249