Skip to content

Commit

Permalink
Reuse the ipts_buffer struct to store descriptor and feature reports
Browse files Browse the repository at this point in the history
  • Loading branch information
StollD committed Dec 11, 2022
1 parent 5ca2625 commit f4c781a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 20 deletions.
11 changes: 6 additions & 5 deletions src/context.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,12 @@ struct ipts_context {
struct mutex feature_lock;
struct completion feature_event;

u8 *get_feature_report;
size_t get_feature_size;

u8 *descriptor;
size_t desc_size;
/*
* These are not inside of struct ipts_resources
* because they don't own the memory they point to.
*/
struct ipts_buffer feature_report;
struct ipts_buffer descriptor;

struct hid_device *hid;
struct ipts_device_info info;
Expand Down
4 changes: 2 additions & 2 deletions src/control.c
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ static int ipts_control_get_descriptor(struct ipts_context *ipts)
header = (struct ipts_data_header *)ipts->resources.descriptor.address;

if (header->type == IPTS_DATA_TYPE_DESCRIPTOR) {
ipts->descriptor = &header->data[8];
ipts->desc_size = header->size - 8;
ipts->descriptor.address = &header->data[8];
ipts->descriptor.size = header->size - 8;

return 0;
}
Expand Down
27 changes: 14 additions & 13 deletions src/hid.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ static int ipts_hid_parse(struct hid_device *hid)
u8 *buffer;
size_t size;
struct ipts_context *ipts;
bool has_native_descriptor;

if (!hid)
return -EFAULT;
Expand All @@ -89,18 +90,19 @@ static int ipts_hid_parse(struct hid_device *hid)
return -EFAULT;

size = sizeof(ipts_singletouch_descriptor);
has_native_descriptor = ipts->descriptor.address && ipts->descriptor.size > 0;

if (ipts->descriptor && ipts->desc_size > 0)
size += ipts->desc_size;
if (has_native_descriptor)
size += ipts->descriptor.size;
else
size += sizeof(ipts_fallback_descriptor);

buffer = kzalloc(size, GFP_KERNEL);
memcpy(buffer, ipts_singletouch_descriptor, sizeof(ipts_singletouch_descriptor));

if (ipts->descriptor && ipts->desc_size > 0) {
memcpy(&buffer[sizeof(ipts_singletouch_descriptor)], ipts->descriptor,
ipts->desc_size);
if (has_native_descriptor) {
memcpy(&buffer[sizeof(ipts_singletouch_descriptor)], ipts->descriptor.address,
ipts->descriptor.size);
} else {
memcpy(&buffer[sizeof(ipts_singletouch_descriptor)], ipts_fallback_descriptor,
sizeof(ipts_fallback_descriptor));
Expand All @@ -127,8 +129,7 @@ static int ipts_hid_get_feature(struct ipts_context *ipts, unsigned char reportn
memset(buf, 0, size);
buf[0] = reportnum;

ipts->get_feature_report = NULL;
ipts->get_feature_size = 0;
memset(&ipts->feature_report, 0, sizeof(ipts->feature_report));
reinit_completion(&ipts->feature_event);

ret = ipts_hid_hid2me_feedback(ipts, type, buf, size);
Expand All @@ -144,18 +145,18 @@ static int ipts_hid_get_feature(struct ipts_context *ipts, unsigned char reportn
goto out;
}

if (!ipts->get_feature_report) {
if (!ipts->feature_report.address) {
ret = -EFAULT;
goto out;
}

if (ipts->get_feature_size > size) {
if (ipts->feature_report.size > size) {
ret = -ETOOSMALL;
goto out;
}

ret = ipts->get_feature_size;
memcpy(buf, ipts->get_feature_report, ipts->get_feature_size);
ret = ipts->feature_report.size;
memcpy(buf, ipts->feature_report.address, ipts->feature_report.size);

out:
mutex_unlock(&ipts->feature_lock);
Expand Down Expand Up @@ -268,8 +269,8 @@ int ipts_hid_input_data(struct ipts_context *ipts, int buffer)
return hid_input_report(ipts->hid, HID_INPUT_REPORT, header->data, header->size, 1);

if (header->type == IPTS_DATA_TYPE_GET_FEATURES) {
ipts->get_feature_report = header->data;
ipts->get_feature_size = header->size;
ipts->feature_report.address = header->data;
ipts->feature_report.size = header->size;

complete_all(&ipts->feature_event);
return 0;
Expand Down

0 comments on commit f4c781a

Please sign in to comment.