platform: chrome: lightbar: Report number of segments
Subsystems (2)
PATCH v2 1/2 platform: chrome: lightbar: Report number of segments
Add attribue num_segments to return the number of exposed LED segments in the lightbar. It can be smaller than the number of physical leds in the lightbar.
Test: Check the attribute is present and returns a value when read.
---
Changes in v2:
- Put local variables at beginning of num_segments_show()
- Cleanup return logic.
drivers/platform/chrome/cros_ec_lightbar.c | 43 +++++++++++++++++++ .../linux/platform_data/cros_ec_commands.h | 11 +++++ 2 files changed, 54 insertions(+)
drivers/platform/chrome/cros_ec_lightbar.c +43 -0
@@ -173,6 +173,47 @@ static ssize_t version_show(struct device *dev,return sysfs_emit (buf,"%d %d\n" , version, flags); } +static ssize_t num_segments_show (struct device *dev, +struct device_attribute *attr,char *buf) +{ +struct ec_params_lightbar *param; +struct ec_response_lightbar *resp; +struct cros_ec_command *msg; +struct cros_ec_dev *ec =to_cros_ec_dev (dev); +uint32_t num =0 ; +int ret; + + ret =lb_throttle (); +if (ret) +return ret; + + msg =alloc_lightbar_cmd_msg (ec); +if (!msg) +return -ENOMEM ; + + param = (struct ec_params_lightbar *)msg->data; + param->cmd =LIGHTBAR_CMD_GET_PARAMS_V3 ; + msg->outsize =sizeof (param->cmd); + msg->result =sizeof (resp->get_params_v3); + ret =cros_ec_cmd_xfer_status (ec->ec_dev, msg); +if (ret <0 && ret != -EINVAL ) + goto exit; + +if (msg->result ==EC_RES_SUCCESS ) { + resp = (struct ec_response_lightbar *)msg->data; + num = resp->get_params_v3.reported_led_num; + } + +/* +* Anything else (ie, EC_RES_INVALID_COMMAND) - no direct control over +* LEDs, return that no leds are supported. +*/ + ret =sysfs_emit (buf,"%u\n" , num); +exit: +kfree (msg); +return ret; +} +static ssize_t brightness_store (struct device *dev,struct device_attribute *attr,const char *buf,size_t count)
@@ -505,6 +546,7 @@ static ssize_t userspace_control_store(struct device *dev,/* Module initialization */ static DEVICE_ATTR_RW (interval_msec); +static DEVICE_ATTR_RO (num_segments);static DEVICE_ATTR_RO (version);static DEVICE_ATTR_WO (brightness);static DEVICE_ATTR_WO (led_rgb);
@@ -514,6 +556,7 @@ static DEVICE_ATTR_RW(userspace_control);static struct attribute *__lb_cmds_attrs[] = { &dev_attr_interval_msec.attr, + &dev_attr_num_segments.attr, &dev_attr_version.attr, &dev_attr_brightness.attr, &dev_attr_led_rgb.attr,
include/linux/platform_data/cros_ec_commands.h +11 -0
@@ -2005,6 +2005,14 @@ struct lightbar_params_v2_colors {struct rgb_s color[8 ];/* 0-3 are Google colors */ } __ec_todo_packed; +struct lightbar_params_v3 { +/* +* Number of LEDs reported by the EC. +* May be less than the actual number of LEDs in the lightbar. +*/ +uint8_t reported_led_num; +} __ec_todo_packed; +/* Lightbar program. */ #define EC_LB_PROG_LEN 192struct lightbar_program {
@@ -2086,6 +2094,8 @@ struct ec_response_lightbar {struct lightbar_params_v2_thresholds get_params_v2_thlds;struct lightbar_params_v2_colors get_params_v2_colors; +struct lightbar_params_v3 get_params_v3; +struct __ec_todo_unpacked {uint32_t num;uint32_t flags;
@@ -2143,6 +2153,7 @@ enum lightbar_command {LIGHTBAR_CMD_SET_PARAMS_V2_THRESHOLDS =31 ,LIGHTBAR_CMD_GET_PARAMS_V2_COLORS =32 ,LIGHTBAR_CMD_SET_PARAMS_V2_COLORS =33 , +LIGHTBAR_CMD_GET_PARAMS_V3 =34 , LIGHTBAR_NUM_CMDS };
Signature · 1 line
2.52.0.457.g6b5491de43-goog
PATCH v2 2/2 platform: chrome: lightbar: Add support for large sequence
Current sequences are limited to 192 bytes. Increase support to whatever the EC support. If the sequence is too long, the EC will return an OVERFLOW error.
Test: Check sending a large sequence is received by the EC.
---
Changes in v2:
- Default initilization for lb_version.
- Minimal changes in local variables definitions in program_store().
- Fix errors in cros_ec_commands.h: use a variable length array for payload.
drivers/platform/chrome/cros_ec_lightbar.c | 78 +++++++++++++------ .../linux/platform_data/cros_ec_commands.h | 13 ++++ 2 files changed, 68 insertions(+), 23 deletions(-)
drivers/platform/chrome/cros_ec_lightbar.c +55 -23
@@ -30,6 +30,11 @@ static unsigned long lb_interval_jiffies = 50 * HZ / 1000;*/ static bool userspace_control; +/* +* Lightbar version +*/ +static int lb_version; +static ssize_t interval_msec_show (struct device *dev,struct device_attribute *attr,char *buf) {
@@ -464,10 +469,11 @@ static ssize_t sequence_store(struct device *dev, struct device_attribute *attr,static ssize_t program_store (struct device *dev,struct device_attribute *attr,const char *buf,size_t count) { -int extra_bytes, max_size, ret; +size_t extra_bytes, max_size;struct ec_params_lightbar *param;struct cros_ec_command *msg;struct cros_ec_dev *ec =to_cros_ec_dev (dev); +int ret;/* * We might need to reject the program for size reasons. The EC
@@ -475,14 +481,22 @@ static ssize_t program_store(struct device *dev, struct device_attribute *attr,* and send a program that is too big for the protocol. In order * to ensure the latter, we also need to ensure we have extra bytes * to represent the rest of the packet. +* With V3, larger program can be sent, limited only by the EC. +* Only the protocol limit the payload size. */ - extra_bytes =sizeof (*param) -sizeof (param->set_program.data); - max_size =min (EC_LB_PROG_LEN , ec->ec_dev->max_request - extra_bytes); -if (count > max_size) { -dev_err (dev,"Program is %u bytes, too long to send (max: %u)" , - (unsigned int )count, max_size); - -return -EINVAL ; +if (lb_version <3 ) { + extra_bytes =sizeof (*param) -sizeof (param->set_program.data); + max_size =min (EC_LB_PROG_LEN , ec->ec_dev->max_request - extra_bytes); +if (count > max_size) { +dev_err (dev,"Program is %zu bytes, too long to send (max: %zu)" , + count, max_size); + +return -EINVAL ; + } + }else { + extra_bytes =sizeof (*param) -sizeof (param->set_program) + +sizeof (param->set_program_ex); + max_size = ec->ec_dev->max_request - extra_bytes; } msg =alloc_lightbar_cmd_msg (ec);
@@ -492,26 +506,44 @@ static ssize_t program_store(struct device *dev, struct device_attribute *attr, ret =lb_throttle ();if (ret) goto exit; + param = (struct ec_params_lightbar *)msg->data; -dev_info (dev,"Copying %zu byte program to EC" , count); +if (lb_version <3 ) { +dev_info (dev,"Copying %zu byte program to EC" , count); - param = (struct ec_params_lightbar *)msg->data; - param->cmd =LIGHTBAR_CMD_SET_PROGRAM ; + param->cmd =LIGHTBAR_CMD_SET_PROGRAM ; - param->set_program.size = count; -memcpy (param->set_program.data, buf, count); + param->set_program.size = count; +memcpy (param->set_program.data, buf, count); -/* -* We need to set the message size manually or else it will use -* EC_LB_PROG_LEN. This might be too long, and the program -* is unlikely to use all of the space. -*/ - msg->outsize = count + extra_bytes; +/* +* We need to set the message size manually or else it will use +* EC_LB_PROG_LEN. This might be too long, and the program +* is unlikely to use all of the space. +*/ + msg->outsize = count + extra_bytes; - ret =cros_ec_cmd_xfer_status (ec->ec_dev, msg); -if (ret <0 ) - goto exit; + ret =cros_ec_cmd_xfer_status (ec->ec_dev, msg); +if (ret <0 ) + goto exit; + }else { +size_t offset =0 ; +size_t payload =0 ; + + param->cmd =LIGHTBAR_CMD_SET_PROGRAM_EX ; +while (offset < count) { + payload =min (max_size, count - offset); + param->set_program_ex.offset = offset; + param->set_program_ex.size = payload; +memcpy (param->set_program_ex.data, &buf[offset], payload); + msg->outsize = payload + extra_bytes; + ret =cros_ec_cmd_xfer_status (ec->ec_dev, msg); +if (ret <0 ) + goto exit; + offset += payload; + } + } ret = count; exit:kfree (msg);
@@ -589,7 +621,7 @@ static int cros_ec_lightbar_probe(struct platform_device *pd)* Ask then for the lightbar version, if it's 0 then the 'cros_ec' * doesn't have a lightbar. */ -if (!get_lightbar_version (ec_dev,NULL ,NULL )) +if (!get_lightbar_version (ec_dev, &lb_version,NULL ))return -ENODEV ;/* Take control of the lightbar from the EC. */
include/linux/platform_data/cros_ec_commands.h +13 -0
@@ -2020,6 +2020,17 @@ struct lightbar_program {uint8_t data[EC_LB_PROG_LEN ]; } __ec_todo_unpacked; +/* +* Lightbar program for large sequences. Sequences are sent in pieces, with +* increasing offset. The sequences are still limited by the amount reserved in +* EC RAM. +*/ +struct lightbar_program_ex { +uint16_t offset; +uint8_t size; +uint8_t data[0 ]; +} __ec_todo_unpacked; +struct ec_params_lightbar {uint8_t cmd;/* Command (see enum lightbar_command) */ union {
@@ -2066,6 +2077,7 @@ struct ec_params_lightbar {struct lightbar_params_v2_colors set_v2par_colors;struct lightbar_program set_program; +struct lightbar_program_ex set_program_ex; }; } __ec_todo_packed;
@@ -2154,6 +2166,7 @@ enum lightbar_command {LIGHTBAR_CMD_GET_PARAMS_V2_COLORS =32 ,LIGHTBAR_CMD_SET_PARAMS_V2_COLORS =33 ,LIGHTBAR_CMD_GET_PARAMS_V3 =34 , +LIGHTBAR_CMD_SET_PROGRAM_EX =35 , LIGHTBAR_NUM_CMDS };
Signature · 1 line
2.52.0.457.g6b5491de43-goog
Tzung-Bi Shih
maintainerQuoted 6 lines · Gwendal Grignou
Add attribue
num_segmentsto return the number of exposed LED segments in the lightbar. It can be smaller than the number of physical leds in the lightbar.Test: Check the attribute is present and returns a value when read.
Quoted 8 lines
Signed-off-by Gwendal Grignou <[email protected]>---
Changes in v2:
- Put local variables at beginning ofnum_segments_show()
- Cleanup return logic.drivers/platform/chrome/cros_ec_lightbar.c | 43 +++++++++++++++++++ .../linux/platform_data/cros_ec_commands.h | 11 +++++
The patch generally looks good to me. We have to wait the header change landed in firmware.
Quoted 12 lines
drivers/platform/chrome/cros_ec_lightbar.c +12
-0+static ssize_t num_segments_show (struct device *dev, +struct device_attribute *attr,char *buf) +{ +struct ec_params_lightbar *param; +struct ec_response_lightbar *resp; +struct cros_ec_command *msg; +struct cros_ec_dev *ec =to_cros_ec_dev (dev); +uint32_t num =0 ; +int ret; + + ret =lb_throttle (); +if (ret)
drivers/platform/chrome/cros_ec_lightbar.c static ssize_t version_show(struct device *dev, · line 197
Quoted 10 lines
drivers/platform/chrome/cros_ec_lightbar.c +10
-0+return ret; + + msg =alloc_lightbar_cmd_msg (ec); +if (!msg) +return -ENOMEM ; + + param = (struct ec_params_lightbar *)msg->data; + param->cmd =LIGHTBAR_CMD_GET_PARAMS_V3 ; + msg->outsize =sizeof (param->cmd); + msg->result =sizeof (resp->get_params_v3);
^^^^^^ insize
It seems you missed the comment from v1.
Quoted 43 lines · Tzung-Bi Shih
Quoted 6 lines · Gwendal Grignou
Add attribue
num_segmentsto return the number of exposed LED segments in the lightbar. It can be smaller than the number of physical leds in the lightbar.Test: Check the attribute is present and returns a value when read.
Quoted 8 lines
Signed-off-by Gwendal Grignou <[email protected]>---
Changes in v2:
- Put local variables at beginning ofnum_segments_show()
- Cleanup return logic.drivers/platform/chrome/cros_ec_lightbar.c | 43 +++++++++++++++++++ .../linux/platform_data/cros_ec_commands.h | 11 +++++The patch generally looks good to me. We have to wait the header change landed in firmware.
Quoted 12 lines
drivers/platform/chrome/cros_ec_lightbar.c +12
-0+static ssize_t num_segments_show (struct device *dev, +struct device_attribute *attr,char *buf) +{ +struct ec_params_lightbar *param; +struct ec_response_lightbar *resp; +struct cros_ec_command *msg; +struct cros_ec_dev *ec =to_cros_ec_dev (dev); +uint32_t num =0 ; +int ret; + + ret =lb_throttle (); +if (ret)drivers/platform/chrome/cros_ec_lightbar.c static ssize_t version_show(struct device *dev, · line 197
Quoted 10 lines
drivers/platform/chrome/cros_ec_lightbar.c +10
-0+return ret; + + msg =alloc_lightbar_cmd_msg (ec); +if (!msg) +return -ENOMEM ; + + param = (struct ec_params_lightbar *)msg->data; + param->cmd =LIGHTBAR_CMD_GET_PARAMS_V3 ; + msg->outsize =sizeof (param->cmd); + msg->result =sizeof (resp->get_params_v3);^^^^^^ insize
Quoted 1 line
It seems you missed the comment from v1.
You're right, let me sent v3 patches: msg->result has been used incorrectly in this driver. Also, I found a invalid memory access during testing.
Gwendal