platform/x86: hp-wmi: Correctly determine method id in WMI calls
PATCH 1/1 platform/x86: hp-wmi: Correctly determine method id in WMI calls
The WMI queries are performed by evaluating the WMPV() method from ACPI DSDT tables, and it takes three arguments: instance index, method id and input data (buffer).
Currently the method id is hard-coded to 0x3 in hp_wmi_perform_query() which means that it will perform WMI calls that expect an output data of size 0x80 (128). The output size is usually OK for the WMI queries we perform, however it would be better to pick the correct one before evaluating the WMI method.
Which correct method id to choose can be figured out by looking at the following ASL code from WVPI() method:
...
Name (PVSZ, Package (0x05)
{
Zero,
0x04,
0x80,
0x0400,
0x1000
})Store (Zero, Local0)
If (LAnd (LGreaterEqual (Arg1, One), LLessEqual (Arg1, 0x05))) {
Store (DerefOf (Index (PVSZ, Subtract (Arg1, One))), Local0)
}
...
Arg1 is the method id and PVSZ is the package used to index the corresponding output size; 1 -> 0, 2 -> 4, 3 -> 128, 4 -> 1024, 5 -> 4096.
This patch maps the output size passed in hp_wmi_perform_query() to the correct method id before evaluating the WMI method.
--- drivers/platform/x86/hp-wmi.c | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-)
drivers/platform/x86/hp-wmi.c +22 -1
@@ -188,6 +188,22 @@ struct rfkill2_device {static int rfkill2_count;static struct rfkill2_device rfkill2[HPWMI_MAX_RFKILL2_DEVICES ]; +/* map output size to the corresponding WMI method id */ +static inline u32 outsize_to_mid (int outsize) +{ +if (!outsize) +return 1 ; +if (outsize <=0x4 ) +return 2 ; +if (outsize <=0x80 ) +return 3 ; +if (outsize <=0x400 ) +return 4 ; +if (outsize <=0x1000 ) +return 5 ; +return (u32)-1 ; +} +/* * hp_wmi_perform_query *
@@ -211,6 +227,7 @@ static struct rfkill2_device rfkill2[HPWMI_MAX_RFKILL2_DEVICES];static int hp_wmi_perform_query (int query,enum hp_wmi_command command,void *buffer,int insize,int outsize) { +u32 mid;struct bios_return *bios_return;int actual_outsize;union acpi_object *obj;
@@ -225,11 +242,15 @@ static int hp_wmi_perform_query(int query, enum hp_wmi_command command,struct acpi_buffer output = {ACPI_ALLOCATE_BUFFER ,NULL };int ret =0 ; + mid =outsize_to_mid (outsize); +if (WARN_ON (mid == (u32)-1 )) +return -EINVAL ; +if (WARN_ON (insize >sizeof (args.data)))return -EINVAL ;memcpy (&args.data, buffer, insize); -wmi_evaluate_method (HPWMI_BIOS_GUID ,0 ,0x3 , &input, &output); +wmi_evaluate_method (HPWMI_BIOS_GUID ,0 , mid, &input, &output); obj = output.pointer;
Signature · 1 line
2.11.0
Quoted 29 lines · Paulo Alcantara
The WMI queries are performed by evaluating the
WMPV()method from ACPI DSDT tables, and it takes three arguments: instance index, method id and input data (buffer).Currently the method id is hard-coded to
0x3inhp_wmi_perform_query()which means that it will perform WMI calls that expect an output data of size0x80(128). The output size is usually OK for the WMI queries we perform, however it would be better to pick the correct one before evaluating the WMI method.Which correct method id to choose can be figured out by looking at the following ASL code from
WVPI()method:...
Name (PVSZ, Package (0x05){ Zero, 0x04, 0x80, 0x0400, 0x1000 })Store (Zero, Local0)
If (LAnd (LGreaterEqual (Arg1, One), LLessEqual (Arg1,0x05))) {
Store (DerefOf (Index (PVSZ, Subtract (Arg1, One))), Local0)
}
...
Quoted 6 lines
Arg1 is the method id and PVSZ is the package used to index the corresponding output size; 1 -> 0, 2 -> 4, 3 -> 128, 4 -> 1024, 5 -> 4096.
This patch maps the output size passed in
hp_wmi_perform_query()to the correct method id before evaluating the WMI method.
Thanks for the patch, my comments below.
drivers/platform/x86/hp-wmi.c struct rfkill2_device { · line 192
Quoted 2 lines
drivers/platform/x86/hp-wmi.c +2
-0+/* map output size to the corresponding WMI method id */ +static inline u32 outsize_to_mid (int outsize)
I would name it like
encode_outsize_for_pvsz
drivers/platform/x86/hp-wmi.c struct rfkill2_device { · line 204
Quoted 12 lines
drivers/platform/x86/hp-wmi.c +12
-0+{ +if (!outsize) +return 1 ; +if (outsize <=0x4 ) +return 2 ; +if (outsize <=0x80 ) +return 3 ; +if (outsize <=0x400 ) +return 4 ; +if (outsize <=0x1000 ) +return 5 ; +return (u32)-1 ;
I would do other way around, i.e.
if (outsize > 4096)
return U32_MAX;
if (outsize > ...)
return ...;
if (outsize > 0)
return 2;
return 1;
Note two things:
1) use decimal integers for size (slightly better to read);
2) don't trick negative numbers.
drivers/platform/x86/hp-wmi.c static int hp_wmi_perform_query(int query, enum hp_wmi_command command, · line 246
Quoted 4 lines
drivers/platform/x86/hp-wmi.c +3
-0+} + mid =outsize_to_mid (outsize); +if (WARN_ON (mid == (u32)-1 ))
And looking here, why you don't return int from that function directly and use it's negative value as error code?
Quoted 2 lines
drivers/platform/x86/hp-wmi.c +2
-0+return -EINVAL ; +
Signature · 2 lines
With Best Regards, Andy Shevchenko
Hi Andy,
Quoted 73 lines · Andy Shevchenko
Quoted 29 lines · Paulo Alcantara
The WMI queries are performed by evaluating the
WMPV()method from ACPI DSDT tables, and it takes three arguments: instance index, method id and input data (buffer).Currently the method id is hard-coded to
0x3inhp_wmi_perform_query()which means that it will perform WMI calls that expect an output data of size0x80(128). The output size is usually OK for the WMI queries we perform, however it would be better to pick the correct one before evaluating the WMI method.Which correct method id to choose can be figured out by looking at the following ASL code from
WVPI()method:...
Name (PVSZ, Package (0x05){ Zero, 0x04, 0x80, 0x0400, 0x1000 })Store (Zero, Local0)
If (LAnd (LGreaterEqual (Arg1, One), LLessEqual (Arg1,0x05))) {
Store (DerefOf (Index (PVSZ, Subtract (Arg1, One))), Local0)
}
...Quoted 6 lines
Arg1 is the method id and PVSZ is the package used to index the corresponding output size; 1 -> 0, 2 -> 4, 3 -> 128, 4 -> 1024, 5 -> 4096.
This patch maps the output size passed in
hp_wmi_perform_query()to the correct method id before evaluating the WMI method.Thanks for the patch, my comments below.
drivers/platform/x86/hp-wmi.c struct rfkill2_device { · line 192
Quoted 2 lines
drivers/platform/x86/hp-wmi.c +2
-0+/* map output size to the corresponding WMI method id */ +static inline u32 outsize_to_mid (int outsize)I would name it like
encode_outsize_for_pvszdrivers/platform/x86/hp-wmi.c struct rfkill2_device { · line 204
Quoted 12 lines
drivers/platform/x86/hp-wmi.c +12
-0+{ +if (!outsize) +return 1 ; +if (outsize <=0x4 ) +return 2 ; +if (outsize <=0x80 ) +return 3 ; +if (outsize <=0x400 ) +return 4 ; +if (outsize <=0x1000 ) +return 5 ; +return (u32)-1 ;I would do other way around, i.e.
if (outsize > 4096)
returnU32_MAX;
if (outsize > ...)
return ...;
if (outsize > 0)
return 2;
return 1;Note two things:
1) use decimal integers for size (slightly better to read);
2) don't trick negative numbers.
Quoted 10 lines
drivers/platform/x86/hp-wmi.c static int hp_wmi_perform_query(int query, enum hp_wmi_command command, · line 246
Quoted 4 lines
drivers/platform/x86/hp-wmi.c +3
-0+} + mid =outsize_to_mid (outsize); +if (WARN_ON (mid == (u32)-1 ))And looking here, why you don't return int from that function directly and use it's negative value as error code?
Quoted 2 lines
drivers/platform/x86/hp-wmi.c +2
-0+return -EINVAL ; +
Thanks for the quick review. I agree with all you comments and suggestions. I'll send a v2 shortly.
-Paulo
PATCH v2 1/1 platform/x86: hp-wmi: Correctly determine method id in WMI calls
In reply to Paulo AlcantaraThe WMI queries are performed by evaluating the WMPV() method from ACPI DSDT tables, and it takes three arguments: instance index, method id and input data (buffer).
Currently the method id is hard-coded to 0x3 in hp_wmi_perform_query() which means that it will perform WMI calls that expect an output data of size 0x80 (128). The output size is usually OK for the WMI queries we perform, however it would be better to pick the correct one before evaluating the WMI method.
Which correct method id to choose can be figured out by looking at the following ASL code from WVPI() method:
...
Name (PVSZ, Package (0x05)
{
Zero,
0x04,
0x80,
0x0400,
0x1000
})Store (Zero, Local0)
If (LAnd (LGreaterEqual (Arg1, One), LLessEqual (Arg1, 0x05))) {
Store (DerefOf (Index (PVSZ, Subtract (Arg1, One))), Local0)
}
...
Arg1 is the method id and PVSZ is the package used to index the corresponding output size; 1 -> 0, 2 -> 4, 3 -> 128, 4 -> 1024, 5 -> 4096.
This patch maps the output size passed in hp_wmi_perform_query() to the correct method id before evaluating the WMI method.
--- drivers/platform/x86/hp-wmi.c | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-)
drivers/platform/x86/hp-wmi.c +22 -1
@@ -188,6 +188,22 @@ struct rfkill2_device {static int rfkill2_count;static struct rfkill2_device rfkill2[HPWMI_MAX_RFKILL2_DEVICES ]; +/* map output size to the corresponding WMI method id */ +static inline int encode_outsize_for_pvsz (int outsize) +{ +if (outsize >4096 ) +return -EINVAL ; +if (outsize >1024 ) +return 5 ; +if (outsize >128 ) +return 4 ; +if (outsize >4 ) +return 3 ; +if (outsize >0 ) +return 2 ; +return 1 ; +} +/* * hp_wmi_perform_query *
@@ -211,6 +227,7 @@ static struct rfkill2_device rfkill2[HPWMI_MAX_RFKILL2_DEVICES];static int hp_wmi_perform_query (int query,enum hp_wmi_command command,void *buffer,int insize,int outsize) { +int mid;struct bios_return *bios_return;int actual_outsize;union acpi_object *obj;
@@ -225,11 +242,15 @@ static int hp_wmi_perform_query(int query, enum hp_wmi_command command,struct acpi_buffer output = {ACPI_ALLOCATE_BUFFER ,NULL };int ret =0 ; + mid =encode_outsize_for_pvsz (outsize); +if (WARN_ON (mid <0 )) +return mid; +if (WARN_ON (insize >sizeof (args.data)))return -EINVAL ;memcpy (&args.data, buffer, insize); -wmi_evaluate_method (HPWMI_BIOS_GUID ,0 ,0x3 , &input, &output); +wmi_evaluate_method (HPWMI_BIOS_GUID ,0 , mid, &input, &output); obj = output.pointer;
Signature · 1 line
2.11.0