1 /* SPDX-License-Identifier: GPL-2.0 */ 2 /* Copyright (c) 2024 Intel Corporation */ 3 4 #include <linux/hid.h> 5 #include <linux/input.h> 6 #include <linux/pm_runtime.h> 7 8 #include "quicki2c-dev.h" 9 #include "quicki2c-hid.h" 10 #include "quicki2c-protocol.h" 11 12 /** 13 * quicki2c_hid_parse() - HID core parse() callback 14 * 15 * @hid: HID device instance 16 * 17 * This function gets called during call to hid_add_device 18 * 19 * Return: 0 on success and non zero on error. 20 */ 21 static int quicki2c_hid_parse(struct hid_device *hid) 22 { 23 struct quicki2c_device *qcdev = hid->driver_data; 24 25 if (qcdev->report_descriptor) 26 return hid_parse_report(hid, qcdev->report_descriptor, 27 le16_to_cpu(qcdev->dev_desc.report_desc_len)); 28 29 dev_err_once(qcdev->dev, "invalid report descriptor\n"); 30 return -EINVAL; 31 } 32 33 static int quicki2c_hid_start(struct hid_device *hid) 34 { 35 return 0; 36 } 37 38 static void quicki2c_hid_stop(struct hid_device *hid) 39 { 40 } 41 42 static int quicki2c_hid_open(struct hid_device *hid) 43 { 44 return 0; 45 } 46 47 static void quicki2c_hid_close(struct hid_device *hid) 48 { 49 } 50 51 static int quicki2c_hid_raw_request(struct hid_device *hid, 52 unsigned char reportnum, 53 __u8 *buf, size_t len, 54 unsigned char rtype, int reqtype) 55 { 56 struct quicki2c_device *qcdev = hid->driver_data; 57 int ret = 0; 58 59 ret = pm_runtime_resume_and_get(qcdev->dev); 60 if (ret) 61 return ret; 62 63 switch (reqtype) { 64 case HID_REQ_GET_REPORT: 65 ret = quicki2c_get_report(qcdev, rtype, reportnum, buf, len); 66 break; 67 case HID_REQ_SET_REPORT: 68 ret = quicki2c_set_report(qcdev, rtype, reportnum, buf, len); 69 break; 70 default: 71 dev_err(qcdev->dev, "Not supported request type %d\n", reqtype); 72 break; 73 } 74 75 pm_runtime_mark_last_busy(qcdev->dev); 76 pm_runtime_put_autosuspend(qcdev->dev); 77 78 return ret; 79 } 80 81 static int quicki2c_hid_power(struct hid_device *hid, int lvl) 82 { 83 return 0; 84 } 85 86 static struct hid_ll_driver quicki2c_hid_ll_driver = { 87 .parse = quicki2c_hid_parse, 88 .start = quicki2c_hid_start, 89 .stop = quicki2c_hid_stop, 90 .open = quicki2c_hid_open, 91 .close = quicki2c_hid_close, 92 .power = quicki2c_hid_power, 93 .raw_request = quicki2c_hid_raw_request, 94 }; 95 96 /** 97 * quicki2c_hid_probe() - Register HID low level driver 98 * 99 * @qcdev: point to quicki2c device 100 * 101 * This function is used to allocate and add HID device. 102 * 103 * Return: 0 on success, non zero on error. 104 */ 105 int quicki2c_hid_probe(struct quicki2c_device *qcdev) 106 { 107 struct hid_device *hid; 108 int ret; 109 110 hid = hid_allocate_device(); 111 if (IS_ERR(hid)) 112 return PTR_ERR(hid); 113 114 hid->ll_driver = &quicki2c_hid_ll_driver; 115 hid->bus = BUS_PCI; 116 hid->dev.parent = qcdev->dev; 117 hid->driver_data = qcdev; 118 hid->version = le16_to_cpu(qcdev->dev_desc.version_id); 119 hid->vendor = le16_to_cpu(qcdev->dev_desc.vendor_id); 120 hid->product = le16_to_cpu(qcdev->dev_desc.product_id); 121 snprintf(hid->name, sizeof(hid->name), "%s %04X:%04X", "quicki2c-hid", 122 hid->vendor, hid->product); 123 124 ret = hid_add_device(hid); 125 if (ret) { 126 hid_destroy_device(hid); 127 return ret; 128 } 129 130 qcdev->hid_dev = hid; 131 132 return 0; 133 } 134 135 /** 136 * quicki2c_hid_remove() - Destroy HID device 137 * 138 * @qcdev: point to quicki2c device 139 * 140 * Return: 0 on success, non zero on error. 141 */ 142 void quicki2c_hid_remove(struct quicki2c_device *qcdev) 143 { 144 hid_destroy_device(qcdev->hid_dev); 145 } 146 147 /** 148 * quicki2c_hid_send_report() - Send HID input report data to HID core 149 * 150 * @qcdev: point to quicki2c device 151 * @data: point to input report data buffer 152 * @data_len: the length of input report data 153 * 154 * Return: 0 on success, non zero on error. 155 */ 156 int quicki2c_hid_send_report(struct quicki2c_device *qcdev, 157 void *data, size_t data_len) 158 { 159 int ret; 160 161 ret = hid_input_report(qcdev->hid_dev, HID_INPUT_REPORT, data, data_len, 1); 162 if (ret) 163 dev_err(qcdev->dev, "Failed to send HID input report, ret = %d.\n", ret); 164 165 return ret; 166 } 167