Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * AMD MP2 Sensors transport driver
0004  *
0005  * Copyright 2020-2021 Advanced Micro Devices, Inc.
0006  * Authors: Nehal Bakulchandra Shah <Nehal-bakulchandra.shah@amd.com>
0007  *      Sandeep Singh <sandeep.singh@amd.com>
0008  *      Basavaraj Natikar <Basavaraj.Natikar@amd.com>
0009  */
0010 #include <linux/hid.h>
0011 #include <linux/wait.h>
0012 #include <linux/sched.h>
0013 
0014 #include "amd_sfh_hid.h"
0015 #include "amd_sfh_pcie.h"
0016 
0017 #define AMD_SFH_RESPONSE_TIMEOUT    1500
0018 
0019 /**
0020  * amdtp_hid_parse() - hid-core .parse() callback
0021  * @hid:    hid device instance
0022  *
0023  * This function gets called during call to hid_add_device
0024  *
0025  * Return: 0 on success and non zero on error
0026  */
0027 static int amdtp_hid_parse(struct hid_device *hid)
0028 {
0029     struct amdtp_hid_data *hid_data = hid->driver_data;
0030     struct amdtp_cl_data *cli_data = hid_data->cli_data;
0031 
0032     return hid_parse_report(hid, cli_data->report_descr[hid_data->index],
0033                   cli_data->report_descr_sz[hid_data->index]);
0034 }
0035 
0036 /* Empty callbacks with success return code */
0037 static int amdtp_hid_start(struct hid_device *hid)
0038 {
0039     return 0;
0040 }
0041 
0042 static void amdtp_hid_stop(struct hid_device *hid)
0043 {
0044 }
0045 
0046 static int amdtp_hid_open(struct hid_device *hid)
0047 {
0048     return 0;
0049 }
0050 
0051 static void amdtp_hid_close(struct hid_device *hid)
0052 {
0053 }
0054 
0055 static int amdtp_raw_request(struct hid_device *hdev, u8 reportnum,
0056                  u8 *buf, size_t len, u8 rtype, int reqtype)
0057 {
0058     return 0;
0059 }
0060 
0061 static void amdtp_hid_request(struct hid_device *hid, struct hid_report *rep, int reqtype)
0062 {
0063     int rc;
0064 
0065     switch (reqtype) {
0066     case HID_REQ_GET_REPORT:
0067         rc = amd_sfh_get_report(hid, rep->id, rep->type);
0068         if (rc)
0069             dev_err(&hid->dev, "AMDSFH  get report error\n");
0070         break;
0071     case HID_REQ_SET_REPORT:
0072         amd_sfh_set_report(hid, rep->id, reqtype);
0073         break;
0074     default:
0075         break;
0076     }
0077 }
0078 
0079 static int amdtp_wait_for_response(struct hid_device *hid)
0080 {
0081     struct amdtp_hid_data *hid_data =  hid->driver_data;
0082     struct amdtp_cl_data *cli_data = hid_data->cli_data;
0083     int i, ret = 0;
0084 
0085     for (i = 0; i < cli_data->num_hid_devices; i++) {
0086         if (cli_data->hid_sensor_hubs[i] == hid)
0087             break;
0088     }
0089 
0090     if (!cli_data->request_done[i])
0091         ret = wait_event_interruptible_timeout(hid_data->hid_wait,
0092                                cli_data->request_done[i],
0093                                msecs_to_jiffies(AMD_SFH_RESPONSE_TIMEOUT));
0094     if (ret == -ERESTARTSYS)
0095         return -ERESTARTSYS;
0096     else if (ret < 0)
0097         return -ETIMEDOUT;
0098     else
0099         return 0;
0100 }
0101 
0102 void amdtp_hid_wakeup(struct hid_device *hid)
0103 {
0104     struct amdtp_hid_data *hid_data;
0105     struct amdtp_cl_data *cli_data;
0106 
0107     if (hid) {
0108         hid_data = hid->driver_data;
0109         cli_data = hid_data->cli_data;
0110         cli_data->request_done[cli_data->cur_hid_dev] = true;
0111         wake_up_interruptible(&hid_data->hid_wait);
0112     }
0113 }
0114 
0115 static struct hid_ll_driver amdtp_hid_ll_driver = {
0116     .parse  =   amdtp_hid_parse,
0117     .start  =   amdtp_hid_start,
0118     .stop   =   amdtp_hid_stop,
0119     .open   =   amdtp_hid_open,
0120     .close  =   amdtp_hid_close,
0121     .request  = amdtp_hid_request,
0122     .wait   =   amdtp_wait_for_response,
0123     .raw_request  = amdtp_raw_request,
0124 };
0125 
0126 int amdtp_hid_probe(u32 cur_hid_dev, struct amdtp_cl_data *cli_data)
0127 {
0128     struct amd_mp2_dev *mp2 = container_of(cli_data->in_data, struct amd_mp2_dev, in_data);
0129     struct device *dev = &mp2->pdev->dev;
0130     struct hid_device *hid;
0131     struct amdtp_hid_data *hid_data;
0132     int rc;
0133 
0134     hid = hid_allocate_device();
0135     if (IS_ERR(hid))
0136         return PTR_ERR(hid);
0137 
0138     hid_data = kzalloc(sizeof(*hid_data), GFP_KERNEL);
0139     if (!hid_data) {
0140         rc = -ENOMEM;
0141         goto err_hid_data;
0142     }
0143 
0144     hid->ll_driver = &amdtp_hid_ll_driver;
0145     hid_data->index = cur_hid_dev;
0146     hid_data->cli_data = cli_data;
0147     init_waitqueue_head(&hid_data->hid_wait);
0148 
0149     hid->driver_data = hid_data;
0150     cli_data->hid_sensor_hubs[cur_hid_dev] = hid;
0151     strscpy(hid->phys, dev->driver ? dev->driver->name : dev_name(dev),
0152         sizeof(hid->phys));
0153     hid->bus = BUS_AMD_SFH;
0154     hid->vendor = AMD_SFH_HID_VENDOR;
0155     hid->product = AMD_SFH_HID_PRODUCT;
0156     snprintf(hid->name, sizeof(hid->name), "%s %04X:%04X", "hid-amdsfh",
0157          hid->vendor, hid->product);
0158 
0159     rc = hid_add_device(hid);
0160     if (rc)
0161         goto err_hid_device;
0162     return 0;
0163 
0164 err_hid_device:
0165     kfree(hid_data);
0166 err_hid_data:
0167     hid_destroy_device(hid);
0168     return rc;
0169 }
0170 
0171 void amdtp_hid_remove(struct amdtp_cl_data *cli_data)
0172 {
0173     int i;
0174 
0175     for (i = 0; i < cli_data->num_hid_devices; ++i) {
0176         if (cli_data->hid_sensor_hubs[i]) {
0177             kfree(cli_data->hid_sensor_hubs[i]->driver_data);
0178             hid_destroy_device(cli_data->hid_sensor_hubs[i]);
0179             cli_data->hid_sensor_hubs[i] = NULL;
0180         }
0181     }
0182 }