Skip to content

Commit 73e784d

Browse files
pzalewski-thegoodpenguinBenjamin Tissoires
authored andcommitted
HID: hid-cypress: clean up usage of 'driver_data'
The module is storing an integer inside the drvdata pointer, which is confusing - furthermore this integer is mutable. When its value is changed it is set again using the 'hid_set_drvdata' API within the 'cp_event' function. Let's fix this, create and allocate the 'cp_device' struct that is then set as the drvdata and then simply use its integer 'quirks' field for storing the quirks, which shall make the code cleaner, type-safe, consistent and more readable. This makes the cast to (void *) during storage unnecessary and the cast to (unsigned long) during retrieval is also removed. Signed-off-by: Pawel Zalewski (The Capable Hub) <pzalewski@thegoodpenguin.co.uk> Signed-off-by: Benjamin Tissoires <bentiss@kernel.org>
1 parent 4de4b8a commit 73e784d

1 file changed

Lines changed: 22 additions & 10 deletions

File tree

drivers/hid/hid-cypress.c

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@
2525

2626
#define VA_INVAL_LOGICAL_BOUNDARY 0x08
2727

28+
struct cp_device {
29+
unsigned long quirks;
30+
};
31+
2832
/*
2933
* Some USB barcode readers from cypress have usage min and usage max in
3034
* the wrong order
@@ -70,7 +74,8 @@ static __u8 *va_logical_boundary_fixup(struct hid_device *hdev, __u8 *rdesc,
7074
static const __u8 *cp_report_fixup(struct hid_device *hdev, __u8 *rdesc,
7175
unsigned int *rsize)
7276
{
73-
unsigned long quirks = (unsigned long)hid_get_drvdata(hdev);
77+
const struct cp_device *cp_device = hid_get_drvdata(hdev);
78+
unsigned long quirks = cp_device->quirks;
7479

7580
if (quirks & CP_RDESC_SWAPPED_MIN_MAX)
7681
rdesc = cp_rdesc_fixup(hdev, rdesc, rsize);
@@ -84,7 +89,8 @@ static int cp_input_mapped(struct hid_device *hdev, struct hid_input *hi,
8489
struct hid_field *field, struct hid_usage *usage,
8590
unsigned long **bit, int *max)
8691
{
87-
unsigned long quirks = (unsigned long)hid_get_drvdata(hdev);
92+
const struct cp_device *cp_device = hid_get_drvdata(hdev);
93+
unsigned long quirks = cp_device->quirks;
8894

8995
if (!(quirks & CP_2WHEEL_MOUSE_HACK))
9096
return 0;
@@ -100,22 +106,21 @@ static int cp_input_mapped(struct hid_device *hdev, struct hid_input *hi,
100106
static int cp_event(struct hid_device *hdev, struct hid_field *field,
101107
struct hid_usage *usage, __s32 value)
102108
{
103-
unsigned long quirks = (unsigned long)hid_get_drvdata(hdev);
109+
struct cp_device *cp_device = hid_get_drvdata(hdev);
104110

105111
if (!(hdev->claimed & HID_CLAIMED_INPUT) || !field->hidinput ||
106-
!usage->type || !(quirks & CP_2WHEEL_MOUSE_HACK))
112+
!usage->type || !(cp_device->quirks & CP_2WHEEL_MOUSE_HACK))
107113
return 0;
108114

109115
if (usage->hid == 0x00090005) {
110116
if (value)
111-
quirks |= CP_2WHEEL_MOUSE_HACK_ON;
117+
cp_device->quirks |= CP_2WHEEL_MOUSE_HACK_ON;
112118
else
113-
quirks &= ~CP_2WHEEL_MOUSE_HACK_ON;
114-
hid_set_drvdata(hdev, (void *)quirks);
119+
cp_device->quirks &= ~CP_2WHEEL_MOUSE_HACK_ON;
115120
return 1;
116121
}
117122

118-
if (usage->code == REL_WHEEL && (quirks & CP_2WHEEL_MOUSE_HACK_ON)) {
123+
if (usage->code == REL_WHEEL && (cp_device->quirks & CP_2WHEEL_MOUSE_HACK_ON)) {
119124
struct input_dev *input = field->hidinput->input;
120125

121126
input_event(input, usage->type, REL_HWHEEL, value);
@@ -127,10 +132,17 @@ static int cp_event(struct hid_device *hdev, struct hid_field *field,
127132

128133
static int cp_probe(struct hid_device *hdev, const struct hid_device_id *id)
129134
{
130-
unsigned long quirks = id->driver_data;
131135
int ret;
136+
struct cp_device *cp_device;
137+
138+
cp_device = devm_kzalloc(&hdev->dev, sizeof(*cp_device), GFP_KERNEL);
139+
140+
if (!cp_device)
141+
return -ENOMEM;
142+
143+
cp_device->quirks = id->driver_data;
132144

133-
hid_set_drvdata(hdev, (void *)quirks);
145+
hid_set_drvdata(hdev, cp_device);
134146

135147
ret = hid_parse(hdev);
136148
if (ret) {

0 commit comments

Comments
 (0)