Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * ISHTP-HID glue driver.
0004  *
0005  * Copyright (c) 2012-2016, Intel Corporation.
0006  */
0007 
0008 #include <linux/hid.h>
0009 #include <linux/intel-ish-client-if.h>
0010 #include <uapi/linux/input.h>
0011 #include "ishtp-hid.h"
0012 
0013 /**
0014  * ishtp_hid_parse() - hid-core .parse() callback
0015  * @hid:    hid device instance
0016  *
0017  * This function gets called during call to hid_add_device
0018  *
0019  * Return: 0 on success and non zero on error
0020  */
0021 static int ishtp_hid_parse(struct hid_device *hid)
0022 {
0023     struct ishtp_hid_data *hid_data =  hid->driver_data;
0024     struct ishtp_cl_data *client_data = hid_data->client_data;
0025     int rv;
0026 
0027     rv = hid_parse_report(hid, client_data->report_descr[hid_data->index],
0028                   client_data->report_descr_size[hid_data->index]);
0029     if (rv)
0030         return  rv;
0031 
0032     return 0;
0033 }
0034 
0035 /* Empty callbacks with success return code */
0036 static int ishtp_hid_start(struct hid_device *hid)
0037 {
0038     return 0;
0039 }
0040 
0041 static void ishtp_hid_stop(struct hid_device *hid)
0042 {
0043 }
0044 
0045 static int ishtp_hid_open(struct hid_device *hid)
0046 {
0047     return 0;
0048 }
0049 
0050 static void ishtp_hid_close(struct hid_device *hid)
0051 {
0052 }
0053 
0054 static int ishtp_raw_request(struct hid_device *hid, unsigned char reportnum,
0055                  __u8 *buf, size_t len, unsigned char rtype,
0056                  int reqtype)
0057 {
0058     struct ishtp_hid_data *hid_data =  hid->driver_data;
0059     char *ishtp_buf = NULL;
0060     size_t ishtp_buf_len;
0061     unsigned int header_size = sizeof(struct hostif_msg);
0062 
0063     if (rtype == HID_OUTPUT_REPORT)
0064         return -EINVAL;
0065 
0066     hid_data->request_done = false;
0067     switch (reqtype) {
0068     case HID_REQ_GET_REPORT:
0069         hid_data->raw_buf = buf;
0070         hid_data->raw_buf_size = len;
0071         hid_data->raw_get_req = true;
0072 
0073         hid_ishtp_get_report(hid, reportnum, rtype);
0074         break;
0075     case HID_REQ_SET_REPORT:
0076         /*
0077          * Spare 7 bytes for 64b accesses through
0078          * get/put_unaligned_le64()
0079          */
0080         ishtp_buf_len = len + header_size;
0081         ishtp_buf = kzalloc(ishtp_buf_len + 7, GFP_KERNEL);
0082         if (!ishtp_buf)
0083             return -ENOMEM;
0084 
0085         memcpy(ishtp_buf + header_size, buf, len);
0086         hid_ishtp_set_feature(hid, ishtp_buf, ishtp_buf_len, reportnum);
0087         kfree(ishtp_buf);
0088         break;
0089     }
0090 
0091     hid_hw_wait(hid);
0092 
0093     return len;
0094 }
0095 
0096 /**
0097  * ishtp_hid_request() - hid-core .request() callback
0098  * @hid:    hid device instance
0099  * @rep:    pointer to hid_report
0100  * @reqtype:    type of req. [GET|SET]_REPORT
0101  *
0102  * This function is used to set/get feaure/input report.
0103  */
0104 static void ishtp_hid_request(struct hid_device *hid, struct hid_report *rep,
0105     int reqtype)
0106 {
0107     struct ishtp_hid_data *hid_data =  hid->driver_data;
0108     /* the specific report length, just HID part of it */
0109     unsigned int len = ((rep->size - 1) >> 3) + 1 + (rep->id > 0);
0110     char *buf;
0111     unsigned int header_size = sizeof(struct hostif_msg);
0112 
0113     len += header_size;
0114 
0115     hid_data->request_done = false;
0116     switch (reqtype) {
0117     case HID_REQ_GET_REPORT:
0118         hid_data->raw_get_req = false;
0119         hid_ishtp_get_report(hid, rep->id, rep->type);
0120         break;
0121     case HID_REQ_SET_REPORT:
0122         /*
0123          * Spare 7 bytes for 64b accesses through
0124          * get/put_unaligned_le64()
0125          */
0126         buf = kzalloc(len + 7, GFP_KERNEL);
0127         if (!buf)
0128             return;
0129 
0130         hid_output_report(rep, buf + header_size);
0131         hid_ishtp_set_feature(hid, buf, len, rep->id);
0132         kfree(buf);
0133         break;
0134     }
0135 }
0136 
0137 /**
0138  * ishtp_wait_for_response() - hid-core .wait() callback
0139  * @hid:    hid device instance
0140  *
0141  * This function is used to wait after get feaure/input report.
0142  *
0143  * Return: 0 on success and non zero on error
0144  */
0145 static int ishtp_wait_for_response(struct hid_device *hid)
0146 {
0147     struct ishtp_hid_data *hid_data =  hid->driver_data;
0148     int rv;
0149 
0150     hid_ishtp_trace(client_data,  "%s hid %p\n", __func__, hid);
0151 
0152     rv = ishtp_hid_link_ready_wait(hid_data->client_data);
0153     if (rv)
0154         return rv;
0155 
0156     if (!hid_data->request_done)
0157         wait_event_interruptible_timeout(hid_data->hid_wait,
0158                     hid_data->request_done, 3 * HZ);
0159 
0160     if (!hid_data->request_done) {
0161         hid_err(hid,
0162             "timeout waiting for response from ISHTP device\n");
0163         return -ETIMEDOUT;
0164     }
0165     hid_ishtp_trace(client_data,  "%s hid %p done\n", __func__, hid);
0166 
0167     hid_data->request_done = false;
0168 
0169     return 0;
0170 }
0171 
0172 /**
0173  * ishtp_hid_wakeup() - Wakeup caller
0174  * @hid:    hid device instance
0175  *
0176  * This function will wakeup caller waiting for Get/Set feature report
0177  */
0178 void ishtp_hid_wakeup(struct hid_device *hid)
0179 {
0180     struct ishtp_hid_data *hid_data = hid->driver_data;
0181 
0182     hid_data->request_done = true;
0183     wake_up_interruptible(&hid_data->hid_wait);
0184 }
0185 
0186 static struct hid_ll_driver ishtp_hid_ll_driver = {
0187     .parse = ishtp_hid_parse,
0188     .start = ishtp_hid_start,
0189     .stop = ishtp_hid_stop,
0190     .open = ishtp_hid_open,
0191     .close = ishtp_hid_close,
0192     .request = ishtp_hid_request,
0193     .wait = ishtp_wait_for_response,
0194     .raw_request = ishtp_raw_request
0195 };
0196 
0197 /**
0198  * ishtp_hid_probe() - hid register ll driver
0199  * @cur_hid_dev:    Index of hid device calling to register
0200  * @client_data:    Client data pointer
0201  *
0202  * This function is used to allocate and add HID device.
0203  *
0204  * Return: 0 on success, non zero on error
0205  */
0206 int ishtp_hid_probe(unsigned int cur_hid_dev,
0207             struct ishtp_cl_data *client_data)
0208 {
0209     int rv;
0210     struct hid_device *hid;
0211     struct ishtp_hid_data *hid_data;
0212 
0213     hid = hid_allocate_device();
0214     if (IS_ERR(hid))
0215         return PTR_ERR(hid);
0216 
0217     hid_data = kzalloc(sizeof(*hid_data), GFP_KERNEL);
0218     if (!hid_data) {
0219         rv = -ENOMEM;
0220         goto err_hid_data;
0221     }
0222 
0223     hid_data->index = cur_hid_dev;
0224     hid_data->client_data = client_data;
0225     init_waitqueue_head(&hid_data->hid_wait);
0226 
0227     hid->driver_data = hid_data;
0228 
0229     client_data->hid_sensor_hubs[cur_hid_dev] = hid;
0230 
0231     hid->ll_driver = &ishtp_hid_ll_driver;
0232     hid->bus = BUS_INTEL_ISHTP;
0233     hid->dev.parent = ishtp_device(client_data->cl_device);
0234 
0235     hid->version = le16_to_cpu(ISH_HID_VERSION);
0236     hid->vendor = le16_to_cpu(client_data->hid_devices[cur_hid_dev].vid);
0237     hid->product = le16_to_cpu(client_data->hid_devices[cur_hid_dev].pid);
0238     snprintf(hid->name, sizeof(hid->name), "%s %04X:%04X", "hid-ishtp",
0239         hid->vendor, hid->product);
0240 
0241     rv = hid_add_device(hid);
0242     if (rv)
0243         goto err_hid_device;
0244 
0245     hid_ishtp_trace(client_data,  "%s allocated hid %p\n", __func__, hid);
0246 
0247     return 0;
0248 
0249 err_hid_device:
0250     kfree(hid_data);
0251 err_hid_data:
0252     hid_destroy_device(hid);
0253     return rv;
0254 }
0255 
0256 /**
0257  * ishtp_hid_remove() - Remove registered hid device
0258  * @client_data:    client data pointer
0259  *
0260  * This function is used to destroy allocatd HID device.
0261  */
0262 void ishtp_hid_remove(struct ishtp_cl_data *client_data)
0263 {
0264     int i;
0265 
0266     for (i = 0; i < client_data->num_hid_devices; ++i) {
0267         if (client_data->hid_sensor_hubs[i]) {
0268             kfree(client_data->hid_sensor_hubs[i]->driver_data);
0269             hid_destroy_device(client_data->hid_sensor_hubs[i]);
0270             client_data->hid_sensor_hubs[i] = NULL;
0271         }
0272     }
0273 }