usb: typec: ucsi: sysfs mailbox for commands
Subsystems (2)
Heikki Krogerus
maintainercover usb: typec: ucsi: sysfs mailbox for commands
Hi,
UCSI has commands that can be used to configure the platform policy manager (PPM, which is the EC in most cases) on top of individual connectors. That kind of commands are very UCSI specific, and because of that, don't fit very well into any of our existing device classes that are all designed to represent the connectors in generic fashion.
Nevertheless, the user space needs some way to configure also the entire PPM with these commands. Exposing the UCSI data structure as a mailbox file to the user space felt to me as the simplest way to do that, so that's why this proposal.
This mailbox is of course not limited to those commands only - any UCSI command can be send to the PPM with it. Łukasz, would this cover also your debugging and testing needs that you were planning the netlink for (although, for the ChromeOS UCSI driver only)?
Br,
Heikki Krogerus (2):
usb: typec: ucsi: Command mailbox interface for the userspace
tools: usb: UCSI command testing tool
Documentation/ABI/testing/sysfs-driver-ucsi | 20 ++ drivers/usb/typec/ucsi/Makefile | 2 +- drivers/usb/typec/ucsi/sysfs.c | 127 ++++++++++ drivers/usb/typec/ucsi/ucsi.c | 31 ++- drivers/usb/typec/ucsi/ucsi.h | 7 + tools/usb/.gitignore | 1 + tools/usb/Build | 1 + tools/usb/Makefile | 8 +- tools/usb/ucsi.c | 250 ++++++++++++++++++++ 9 files changed, 432 insertions(+), 15 deletions(-) create mode 100644 Documentation/ABI/testing/sysfs-driver-ucsi create mode 100644 drivers/usb/typec/ucsi/sysfs.c create mode 100644 tools/usb/ucsi.c
Signature · 1 line
2.47.2
Heikki Krogerus
maintainerPATCH 1/2 usb: typec: ucsi: Command mailbox interface for the userspace
Some of the UCSI commands can be used to configure the entire Platform Policy Manager (PPM) instead of just individual connectors. To allow the user space communicate those commands with the PPM, adding a mailbox interface. The interface is a single attribute file that represents the main "OPM to PPM" UCSI data structure.
The mailbox allows any UCSI command to be sent to the PPM so it should be also useful for validation, testing and debugging purposes.
--- Documentation/ABI/testing/sysfs-driver-ucsi | 20 +++ drivers/usb/typec/ucsi/Makefile | 2 +- drivers/usb/typec/ucsi/sysfs.c | 127 ++++++++++++++++++++ drivers/usb/typec/ucsi/ucsi.c | 31 +++-- drivers/usb/typec/ucsi/ucsi.h | 7 ++ 5 files changed, 173 insertions(+), 14 deletions(-) create mode 100644 Documentation/ABI/testing/sysfs-driver-ucsi create mode 100644 drivers/usb/typec/ucsi/sysfs.c
Documentation/ABI/testing/sysfs-driver-ucsi new file +20 -0
@@ -0,0 +1,20 @@ +What: /sys/class/typec/<port>/device/ucsi +Date: February 2025 +Contact: Heikki Krogerus <[email protected]> +Description: + Command mailbox for UCSI (USB Type-C System Software Interface). + + The mailbox contains a copy of the main UCSI data structure. + Sending a command happens by writing the command specific data + structure to the CONTROL offset just like defined in the UCSI + specification. When a command is written to the mailbox, it is + automatically forwarded to the Platform Policy Manager (PPM) of + the UCSI instance. + + After writing the command to the mailbox, the result can be read + directly from the CCI and MESSAGE_IN offsets. The mailbox takes + care of command completion acknowledges automatically. + + Note. The mailbox is meant, and can only be used for, sending + commands. I.e. the mailbox is not updated when the UCSI receives + asynchronous events.
drivers/usb/typec/ucsi/Makefile +1 -1
@@ -3,7 +3,7 @@ CFLAGS_trace.o := -I$(src) obj-$(CONFIG_TYPEC_UCSI) += typec_ucsi.o -typec_ucsi-y := ucsi.o +typec_ucsi-y := ucsi.o sysfs.o typec_ucsi-$(CONFIG_DEBUG_FS) += debugfs.o
drivers/usb/typec/ucsi/sysfs.c new file +127 -0
127 lines not on this page · raw message
drivers/usb/typec/ucsi/ucsi.c +18 -13
@@ -1340,16 +1340,18 @@ static int ucsi_reset_connector(struct ucsi_connector *con, bool hard)return ucsi_send_command (con->ucsi, command,NULL ,0 ); } -static int ucsi_reset_ppm (struct ucsi *ucsi) +int ucsi_reset_ppm (struct ucsi *ucsi,u32 *cci) {u64 command;unsigned long tmo; -u32 cci; +u32 _cci;int ret; + cci = cci ?: &_cci; +mutex_lock (&ucsi->ppm_lock); - ret = ucsi->ops->read_cci (ucsi, &cci); + ret = ucsi->ops->read_cci (ucsi, cci);if (ret <0 ) goto out;
@@ -1359,7 +1361,7 @@ static int ucsi_reset_ppm(struct ucsi *ucsi)* UCSI_SET_NOTIFICATION_ENABLE command to achieve this. * Ignore a timeout and try the reset anyway if this fails. */ -if (cci &UCSI_CCI_RESET_COMPLETE ) { +if (*cci &UCSI_CCI_RESET_COMPLETE ) { command =UCSI_SET_NOTIFICATION_ENABLE ; ret = ucsi->ops->async_control (ucsi, command);if (ret <0 )
@@ -1367,17 +1369,17 @@ static int ucsi_reset_ppm(struct ucsi *ucsi) tmo = jiffies +msecs_to_jiffies (UCSI_TIMEOUT_MS );do { - ret = ucsi->ops->read_cci (ucsi, &cci); + ret = ucsi->ops->read_cci (ucsi, cci);if (ret <0 ) goto out; -if (cci &UCSI_CCI_COMMAND_COMPLETE ) +if (*cci &UCSI_CCI_COMMAND_COMPLETE )break ;if (time_is_before_jiffies (tmo))break ;msleep (20 ); }while (1 ); -WARN_ON (cci &UCSI_CCI_RESET_COMPLETE ); +WARN_ON (*cci &UCSI_CCI_RESET_COMPLETE ); } command =UCSI_PPM_RESET ;
@@ -1396,18 +1398,18 @@ static int ucsi_reset_ppm(struct ucsi *ucsi)/* Give the PPM time to process a reset before reading CCI */ msleep (20 ); - ret = ucsi->ops->read_cci (ucsi, &cci); + ret = ucsi->ops->read_cci (ucsi, cci);if (ret) goto out;/* If the PPM is still doing something else, reset it again. */ -if (cci & ~UCSI_CCI_RESET_COMPLETE ) { +if (*cci & ~UCSI_CCI_RESET_COMPLETE ) { ret = ucsi->ops->async_control (ucsi, command);if (ret <0 ) goto out; } - }while (!(cci &UCSI_CCI_RESET_COMPLETE )); + }while (!(*cci &UCSI_CCI_RESET_COMPLETE )); out:mutex_unlock (&ucsi->ppm_lock);
@@ -1423,7 +1425,7 @@ static int ucsi_role_cmd(struct ucsi_connector *con, u64 command)u64 c;/* PPM most likely stopped responding. Resetting everything. */ -ucsi_reset_ppm (con->ucsi); +ucsi_reset_ppm (con->ucsi,NULL ); c =UCSI_SET_NOTIFICATION_ENABLE | con->ucsi->ntfy;ucsi_send_command (con->ucsi, c,NULL ,0 );
@@ -1766,7 +1768,7 @@ static int ucsi_init(struct ucsi *ucsi)int i;/* Reset the PPM */ - ret =ucsi_reset_ppm (ucsi); + ret =ucsi_reset_ppm (ucsi,NULL );if (ret) {dev_err (ucsi->dev,"failed to reset PPM!\n" ); goto err;
@@ -1846,7 +1848,7 @@ static int ucsi_init(struct ucsi *ucsi)kfree (connector); err_reset:memset (&ucsi->cap,0 ,sizeof (ucsi->cap)); -ucsi_reset_ppm (ucsi); +ucsi_reset_ppm (ucsi,NULL ); err:return ret; }
@@ -1958,6 +1960,7 @@ EXPORT_SYMBOL_GPL(ucsi_create);void ucsi_destroy (struct ucsi *ucsi) {ucsi_debugfs_unregister (ucsi); +ucsi_sysfs_unregister (ucsi);kfree (ucsi); }EXPORT_SYMBOL_GPL (ucsi_destroy);
@@ -1989,6 +1992,8 @@ int ucsi_register(struct ucsi *ucsi)queue_delayed_work (system_long_wq, &ucsi->work,0 );ucsi_debugfs_register (ucsi); +ucsi_sysfs_register (ucsi); +return 0 ; }EXPORT_SYMBOL_GPL (ucsi_register);
drivers/usb/typec/ucsi/ucsi.h +7 -0
@@ -468,6 +468,8 @@ struct ucsi {unsigned long quirks;#define UCSI_NO_PARTNER_PDOS BIT(0)/* Don't read partner's PDOs */ #define UCSI_DELAY_DEVICE_PDOS BIT(1)/* Reading PDOs fails until the parter is in PD mode */ + +void *sysfs; };#define UCSI_MAX_DATA_LENGTH (u) (((u)->version < UCSI_VERSION_2_0) ? 0x10 : 0xff)
@@ -535,6 +537,8 @@ void ucsi_notify_common(struct ucsi *ucsi, u32 cci);int ucsi_sync_control_common (struct ucsi *ucsi,u64 command,u32 *cci,void *data,size_t size); +int ucsi_reset_ppm (struct ucsi *ucsi,u32 *cci); +#if IS_ENABLED (CONFIG_POWER_SUPPLY )int ucsi_register_port_psy (struct ucsi_connector *con);void ucsi_unregister_port_psy (struct ucsi_connector *con);
@@ -578,6 +582,9 @@ static inline void ucsi_debugfs_register(struct ucsi *ucsi) { }static inline void ucsi_debugfs_unregister (struct ucsi *ucsi) { }#endif /* CONFIG_DEBUG_FS */ +int ucsi_sysfs_register (struct ucsi *ucsi); +void ucsi_sysfs_unregister (struct ucsi *ucsi); +/* * NVIDIA VirtualLink (svid 0x955) has two altmode. VirtualLink * DP mode with vdo=0x1 and NVIDIA test mode with vdo=0x3
Signature · 1 line
2.47.2
Greg Kroah-Hartman
maintainerRe: [PATCH v1 1/2] usb: typec: ucsi: Command mailbox interface for the userspace
Quoted 10 lines · Heikki Krogerus
Some of the UCSI commands can be used to configure the entire Platform Policy Manager (PPM) instead of just individual connectors. To allow the user space communicate those commands with the PPM, adding a mailbox interface. The interface is a single attribute file that represents the main "OPM to PPM" UCSI data structure.
The mailbox allows any UCSI command to be sent to the PPM so it should be also useful for validation, testing and debugging purposes.
As it's for this type of thing, why not put it in debugfs instead?
Quoted 35 lines
drivers/usb/typec/ucsi/sysfs.c new file +35
-0+static ssize_t ucsi_write (struct file *filp,struct kobject *kobj, +const struct bin_attribute *attr, +char *buf,loff_t off,size_t count) +{ +struct ucsi_sysfs *sysfs = attr->private; +struct ucsi *ucsi = sysfs->ucsi; +int ret; + +u64 *control = (u64 *)&sysfs->mailbox[UCSI_CONTROL ]; +u32 *cci = (u32 *)&sysfs->mailbox[UCSI_CCI ]; +void *data = &sysfs->mailbox[UCSI_MESSAGE_IN ]; + +/* TODO: MESSAGE_OUT. */ +if (off !=UCSI_CONTROL || count !=sizeof (*control)) +return -EFAULT ; + +mutex_lock (&sysfs->lock); + +memset (data,0 ,UCSI_MAX_DATA_LENGTH (ucsi)); + +/* PPM_RESET has to be handled separately. */ + *control =get_unaligned_le64 (buf); +if (UCSI_COMMAND (*control) ==UCSI_PPM_RESET ) { + ret =ucsi_reset_ppm (ucsi, cci); + goto out_unlock_sysfs; + } + +mutex_lock (&ucsi->ppm_lock); + + ret = ucsi->ops->sync_control (ucsi, *control, cci,NULL ,0 ); +if (ret) + goto out_unlock_ppm; + +if (UCSI_CCI_LENGTH (*cci) && ucsi->ops->read_message_in (ucsi, data,UCSI_CCI_LENGTH (*cci))) +dev_err (ucsi->dev,"failed to read MESSAGE_IN\n" );
drivers/usb/typec/ucsi/sysfs.c line 83
Quoted 10 lines
drivers/usb/typec/ucsi/sysfs.c new file +10
-0+ + ret = ucsi->ops->sync_control (ucsi,UCSI_ACK_CC_CI |UCSI_ACK_COMMAND_COMPLETE , +NULL ,NULL ,0 ); +out_unlock_ppm: +mutex_unlock (&ucsi->ppm_lock); +out_unlock_sysfs: +mutex_unlock (&sysfs->lock); + +return ret ?: count; +}
This worries me, any userspace tool can now do this? What other "bad" things can it to the connection?
Quoted 15 lines
drivers/usb/typec/ucsi/sysfs.c new file +15
-0+ +int ucsi_sysfs_register (struct ucsi *ucsi) +{ +struct ucsi_sysfs *sysfs; +int ret; + + sysfs =kzalloc (struct_size (sysfs, mailbox,UCSI_MAILBOX_SIZE (ucsi)),GFP_KERNEL ); +if (!sysfs) +return -ENOMEM ; + + sysfs->ucsi = ucsi; +mutex_init (&sysfs->lock); +memcpy (sysfs->mailbox, &ucsi->version,sizeof (ucsi->version)); + +sysfs_bin_attr_init (&sysfs->bin_attr);
drivers/usb/typec/ucsi/sysfs.c line 108
Quoted 10 lines
drivers/usb/typec/ucsi/sysfs.c new file +10
-0+ + sysfs->bin_attr.attr.name ="ucsi" ; + sysfs->bin_attr.attr.mode =0644 ; + + sysfs->bin_attr.size =UCSI_MAILBOX_SIZE (ucsi); + sysfs->bin_attr.private = sysfs; + sysfs->bin_attr.read_new = ucsi_read; + sysfs->bin_attr.write_new = ucsi_write; + + ret =sysfs_create_bin_file (&ucsi->dev->kobj, &sysfs->bin_attr);
You raced with userspace and lost, right? Why are you dynamically creating this attribute, can't you just use a static one?
But again, why not debugfs? I'd feel a lot more comfortable with that instead of sysfs.
thanks,
greg k-h
Heikki Krogerus
maintainerRe: [PATCH v1 1/2] usb: typec: ucsi: Command mailbox interface for the userspace
Quoted 54 lines · Greg Kroah-Hartman
Quoted 10 lines · Heikki Krogerus
Some of the UCSI commands can be used to configure the entire Platform Policy Manager (PPM) instead of just individual connectors. To allow the user space communicate those commands with the PPM, adding a mailbox interface. The interface is a single attribute file that represents the main "OPM to PPM" UCSI data structure.
The mailbox allows any UCSI command to be sent to the PPM so it should be also useful for validation, testing and debugging purposes.
As it's for this type of thing, why not put it in debugfs instead?
Quoted 40 lines
drivers/usb/typec/ucsi/sysfs.c new file +40
-0+static ssize_t ucsi_write (struct file *filp,struct kobject *kobj, +const struct bin_attribute *attr, +char *buf,loff_t off,size_t count) +{ +struct ucsi_sysfs *sysfs = attr->private; +struct ucsi *ucsi = sysfs->ucsi; +int ret; + +u64 *control = (u64 *)&sysfs->mailbox[UCSI_CONTROL ]; +u32 *cci = (u32 *)&sysfs->mailbox[UCSI_CCI ]; +void *data = &sysfs->mailbox[UCSI_MESSAGE_IN ]; + +/* TODO: MESSAGE_OUT. */ +if (off !=UCSI_CONTROL || count !=sizeof (*control)) +return -EFAULT ; + +mutex_lock (&sysfs->lock); + +memset (data,0 ,UCSI_MAX_DATA_LENGTH (ucsi)); + +/* PPM_RESET has to be handled separately. */ + *control =get_unaligned_le64 (buf); +if (UCSI_COMMAND (*control) ==UCSI_PPM_RESET ) { + ret =ucsi_reset_ppm (ucsi, cci); + goto out_unlock_sysfs; + } + +mutex_lock (&ucsi->ppm_lock); + + ret = ucsi->ops->sync_control (ucsi, *control, cci,NULL ,0 ); +if (ret) + goto out_unlock_ppm; + +if (UCSI_CCI_LENGTH (*cci) && ucsi->ops->read_message_in (ucsi, data,UCSI_CCI_LENGTH (*cci))) +dev_err (ucsi->dev,"failed to read MESSAGE_IN\n" ); + + ret = ucsi->ops->sync_control (ucsi,UCSI_ACK_CC_CI |UCSI_ACK_COMMAND_COMPLETE , +NULL ,NULL ,0 ); +out_unlock_ppm: +mutex_unlock (&ucsi->ppm_lock);
Quoted 8 lines
drivers/usb/typec/ucsi/sysfs.c line 83
Quoted 5 lines
drivers/usb/typec/ucsi/sysfs.c new file +5
-0+out_unlock_sysfs: +mutex_unlock (&sysfs->lock); + +return ret ?: count; +}This worries me, any userspace tool can now do this? What other "bad" things can it to the connection?
Although, there is actually only a limited number of things that you can do to the connection using UCSI, that is definitely a concern.
The PPM (which is the EC firmware in most cases) is expected to prevent any harmful or "unauthorized" UCSI commands from being executed, but I'm not sure there is any guarantees for that at the moment.
Quoted 19 lines
Quoted 19 lines
drivers/usb/typec/ucsi/sysfs.c new file +19
-0+int ucsi_sysfs_register (struct ucsi *ucsi) +{ +struct ucsi_sysfs *sysfs; +int ret; + + sysfs =kzalloc (struct_size (sysfs, mailbox,UCSI_MAILBOX_SIZE (ucsi)),GFP_KERNEL ); +if (!sysfs) +return -ENOMEM ; + + sysfs->ucsi = ucsi; +mutex_init (&sysfs->lock); +memcpy (sysfs->mailbox, &ucsi->version,sizeof (ucsi->version)); + +sysfs_bin_attr_init (&sysfs->bin_attr); + + sysfs->bin_attr.attr.name ="ucsi" ; + sysfs->bin_attr.attr.mode =0644 ; + + sysfs->bin_attr.size =UCSI_MAILBOX_SIZE (ucsi);
Quoted 8 lines
drivers/usb/typec/ucsi/sysfs.c line 108
Quoted 5 lines
drivers/usb/typec/ucsi/sysfs.c new file +5
-0+ sysfs->bin_attr.private = sysfs; + sysfs->bin_attr.read_new = ucsi_read; + sysfs->bin_attr.write_new = ucsi_write; + + ret =sysfs_create_bin_file (&ucsi->dev->kobj, &sysfs->bin_attr);You raced with userspace and lost, right? Why are you dynamically creating this attribute, can't you just use a static one?
The size of the attribute depends on the UCSI version.
Quoted 2 lines
But again, why not debugfs? I'd feel a lot more comfortable with that instead of sysfs.
I would actually prefer debugfs for this, but this is in any case not primarily for debugging and validation.
The initial goal was to supply the user space some way to control the EC's power related policies using UCSI commands such as SET_POWER_LEVEL and GET_POWER_LEVEL (guys, please correct me if I got that wrong).
But I'm now again wondering could those power policy tasks be handled using the UCSI power supplies after all? Venkat, did you look into that?
thanks,
Signature · 1 line
heikki
Re: [PATCH v1 1/2] usb: typec: ucsi: Command mailbox interface for the userspace
Quoted 104 lines · Heikki Krogerus
Quoted 54 lines · Greg Kroah-Hartman
Quoted 10 lines · Heikki Krogerus
Some of the UCSI commands can be used to configure the entire Platform Policy Manager (PPM) instead of just individual connectors. To allow the user space communicate those commands with the PPM, adding a mailbox interface. The interface is a single attribute file that represents the main "OPM to PPM" UCSI data structure.
The mailbox allows any UCSI command to be sent to the PPM so it should be also useful for validation, testing and debugging purposes.
As it's for this type of thing, why not put it in debugfs instead?
Quoted 40 lines
drivers/usb/typec/ucsi/sysfs.c new file +40
-0+static ssize_t ucsi_write (struct file *filp,struct kobject *kobj, +const struct bin_attribute *attr, +char *buf,loff_t off,size_t count) +{ +struct ucsi_sysfs *sysfs = attr->private; +struct ucsi *ucsi = sysfs->ucsi; +int ret; + +u64 *control = (u64 *)&sysfs->mailbox[UCSI_CONTROL ]; +u32 *cci = (u32 *)&sysfs->mailbox[UCSI_CCI ]; +void *data = &sysfs->mailbox[UCSI_MESSAGE_IN ]; + +/* TODO: MESSAGE_OUT. */ +if (off !=UCSI_CONTROL || count !=sizeof (*control)) +return -EFAULT ; + +mutex_lock (&sysfs->lock); + +memset (data,0 ,UCSI_MAX_DATA_LENGTH (ucsi)); + +/* PPM_RESET has to be handled separately. */ + *control =get_unaligned_le64 (buf); +if (UCSI_COMMAND (*control) ==UCSI_PPM_RESET ) { + ret =ucsi_reset_ppm (ucsi, cci); + goto out_unlock_sysfs; + } + +mutex_lock (&ucsi->ppm_lock); + + ret = ucsi->ops->sync_control (ucsi, *control, cci,NULL ,0 ); +if (ret) + goto out_unlock_ppm; + +if (UCSI_CCI_LENGTH (*cci) && ucsi->ops->read_message_in (ucsi, data,UCSI_CCI_LENGTH (*cci))) +dev_err (ucsi->dev,"failed to read MESSAGE_IN\n" ); + + ret = ucsi->ops->sync_control (ucsi,UCSI_ACK_CC_CI |UCSI_ACK_COMMAND_COMPLETE , +NULL ,NULL ,0 ); +out_unlock_ppm: +mutex_unlock (&ucsi->ppm_lock);Quoted 8 lines
drivers/usb/typec/ucsi/sysfs.c line 83
Quoted 5 lines
drivers/usb/typec/ucsi/sysfs.c new file +5
-0+out_unlock_sysfs: +mutex_unlock (&sysfs->lock); + +return ret ?: count; +}This worries me, any userspace tool can now do this? What other "bad" things can it to the connection?
Although, there is actually only a limited number of things that you can do to the connection using UCSI, that is definitely a concern.
The PPM (which is the EC firmware in most cases) is expected to prevent any harmful or "unauthorized" UCSI commands from being executed, but I'm not sure there is any guarantees for that at the moment.
Quoted 19 lines
Quoted 19 lines
drivers/usb/typec/ucsi/sysfs.c new file +19
-0+int ucsi_sysfs_register (struct ucsi *ucsi) +{ +struct ucsi_sysfs *sysfs; +int ret; + + sysfs =kzalloc (struct_size (sysfs, mailbox,UCSI_MAILBOX_SIZE (ucsi)),GFP_KERNEL ); +if (!sysfs) +return -ENOMEM ; + + sysfs->ucsi = ucsi; +mutex_init (&sysfs->lock); +memcpy (sysfs->mailbox, &ucsi->version,sizeof (ucsi->version)); + +sysfs_bin_attr_init (&sysfs->bin_attr); + + sysfs->bin_attr.attr.name ="ucsi" ; + sysfs->bin_attr.attr.mode =0644 ; + + sysfs->bin_attr.size =UCSI_MAILBOX_SIZE (ucsi);Quoted 8 lines
drivers/usb/typec/ucsi/sysfs.c line 108
Quoted 5 lines
drivers/usb/typec/ucsi/sysfs.c new file +5
-0+ sysfs->bin_attr.private = sysfs; + sysfs->bin_attr.read_new = ucsi_read; + sysfs->bin_attr.write_new = ucsi_write; + + ret =sysfs_create_bin_file (&ucsi->dev->kobj, &sysfs->bin_attr);You raced with userspace and lost, right? Why are you dynamically creating this attribute, can't you just use a static one?
The size of the attribute depends on the UCSI version.
Quoted 2 lines
But again, why not debugfs? I'd feel a lot more comfortable with that instead of sysfs.
Quoted 7 lines
I would actually prefer debugfs for this, but this is in any case not primarily for debugging and validation.
The initial goal was to supply the user space some way to control the EC's power related policies using UCSI commands such as
SET_POWER_LEVELandGET_POWER_LEVEL(guys, please correct me if I got that wrong).
It generally feels that exporting the whole unmoderated channel to the firmware just to set power level is wrong. It should be interfaced through the PSY driver.
Quoted 9 lines
But I'm now again wondering could those power policy tasks be handled using the UCSI power supplies after all? Venkat, did you look into that?
thanks,
Signature · 1 line
heikki
Signature · 2 lines
With best wishes Dmitry
RE: [PATCH v1 1/2] usb: typec: ucsi: Command mailbox interface for the userspace
Quoted 10 lines · Pathak Asutosh
Quoted 9 lines · Heikki Krogerus
Quoted 8 lines · Greg Kroah-Hartman
Quoted 7 lines · Heikki Krogerus
Some of the UCSI commands can be used to configure the entire Platform Policy Manager (PPM) instead of just individual connectors. To allow the user space communicate those commands with the PPM, adding a mailbox interface. The interface is a single attribute file that represents the main "OPM to PPM" UCSI data structure.
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 3 lines
The mailbox allows any UCSI command to be sent to the PPM so it should be also useful for validation, testing and debugging purposes.
As it's for this type of thing, why not put it in debugfs instead?
The intend of this sysfs is not limited to validation, testing and debugging purposes but rather providing interface for major user space application developments.
At present we are working on an application/ user space service which will be calling UCSI read/write power level commands. But in future there would be more such applications which may require additional UCSI commands to use. We wanted to have a common and generic solution - and hence thought of going with sysfs interface.
Issue with debugfs is, it is default disabled in release kernels. User has to rebuild the kernel if the application is based on the debugfs interface. This will become a bottleneck for wider use of such appliances.
Quoted 36 lines
Quoted 27 lines
Quoted 27 lines
Quoted 27 lines
drivers/usb/typec/ucsi/sysfs.c new file +26
-0+static ssize_t ucsi_write (struct file *filp,struct kobject *kobj, +const struct bin_attribute *attr, +char *buf,loff_t off,size_t count) +{ +struct ucsi_sysfs *sysfs = attr->private; +struct ucsi *ucsi = sysfs->ucsi; +int ret; + +u64 *control = (u64 *)&sysfs->mailbox[UCSI_CONTROL ]; +u32 *cci = (u32 *)&sysfs->mailbox[UCSI_CCI ]; +void *data = &sysfs->mailbox[UCSI_MESSAGE_IN ]; + +/* TODO: MESSAGE_OUT. */ +if (off !=UCSI_CONTROL || count !=sizeof (*control)) +return -EFAULT ; + +mutex_lock (&sysfs->lock); + +memset (data,0 ,UCSI_MAX_DATA_LENGTH (ucsi)); + +/* PPM_RESET has to be handled separately. */ + *control =get_unaligned_le64 (buf); +if (UCSI_COMMAND (*control) ==UCSI_PPM_RESET ) { + ret =ucsi_reset_ppm (ucsi, cci); + goto out_unlock_sysfs; + }Quoted 8 lines
Quoted 8 lines
drivers/usb/typec/ucsi/sysfs.c
Quoted 8 lines
drivers/usb/typec/ucsi/sysfs.c new file +8
-0+ +mutex_lock (&ucsi->ppm_lock); + + ret = ucsi->ops->sync_control (ucsi, *control, cci,NULL ,0 ); +if (ret) + goto out_unlock_ppm; + + if (UCSI_CCI_LENGTH(*cci) && ucsi->ops->read_message_in(ucsi, data,
UCSI_CCI_LENGTH(*cci)))
Quoted 23 lines
Quoted 3 lines
Quoted 3 lines
drivers/usb/typec/ucsi/sysfs.c
Quoted 3 lines
drivers/usb/typec/ucsi/sysfs.c new file +3
-0+dev_err (ucsi->dev,"failed to read MESSAGE_IN\n" ); + + ret = ucsi->ops->sync_control(ucsi, UCSI_ACK_CC_CI |
UCSI_ACK_COMMAND_COMPLETE,Quoted 19 lines
Quoted 11 lines
drivers/usb/typec/ucsi/sysfs.c line 83
Quoted 8 lines
drivers/usb/typec/ucsi/sysfs.c new file +8
-0+ NULL, NULL, 0); +out_unlock_ppm: +mutex_unlock (&ucsi->ppm_lock); +out_unlock_sysfs: +mutex_unlock (&sysfs->lock); + +return ret ?: count; +}This worries me, any userspace tool can now do this? What other "bad" things can it to the connection?
Although, there is actually only a limited number of things that you can do to the connection using UCSI, that is definitely a concern.
The PPM (which is the EC firmware in most cases) is expected to prevent any harmful or "unauthorized" UCSI commands from being executed, but I'm not sure there is any guarantees for that at the moment.
Critical power setting related features and options are tightly controlled by PPM/LPM. In such cases, those UCSI command request by user space will be blocked by PPM/LPM and will eventually end of into DoS.
Moreover, to further mitigate the risk of any malicious attack our understanding is this sysfs interface will be accessible only with root or super user privilege.
Quoted 37 lines
Quoted 6 lines
Quoted 6 lines
drivers/usb/typec/ucsi/sysfs.c
Quoted 6 lines
drivers/usb/typec/ucsi/sysfs.c new file +6
-0+int ucsi_sysfs_register (struct ucsi *ucsi) +{ +struct ucsi_sysfs *sysfs; +int ret; + + sysfs = kzalloc(struct_size(sysfs, mailbox, UCSI_MAILBOX_SIZE(ucsi)),
GFP_KERNEL);Quoted 30 lines
Quoted 13 lines
Quoted 13 lines
drivers/usb/typec/ucsi/sysfs.c new file +13
-0+if (!sysfs) +return -ENOMEM ; + + sysfs->ucsi = ucsi; +mutex_init (&sysfs->lock); +memcpy (sysfs->mailbox, &ucsi->version,sizeof (ucsi->version)); + +sysfs_bin_attr_init (&sysfs->bin_attr); + + sysfs->bin_attr.attr.name ="ucsi" ; + sysfs->bin_attr.attr.mode =0644 ; + + sysfs->bin_attr.size =UCSI_MAILBOX_SIZE (ucsi);Quoted 8 lines
drivers/usb/typec/ucsi/sysfs.c line 108
Quoted 5 lines
drivers/usb/typec/ucsi/sysfs.c new file +5
-0+ sysfs->bin_attr.private = sysfs; + sysfs->bin_attr.read_new = ucsi_read; + sysfs->bin_attr.write_new = ucsi_write; + + ret =sysfs_create_bin_file (&ucsi->dev->kobj, &sysfs->bin_attr);You raced with userspace and lost, right? Why are you dynamically creating this attribute, can't you just use a static one?
The size of the attribute depends on the UCSI version.
Quoted 2 lines
But again, why not debugfs? I'd feel a lot more comfortable with that instead of sysfs.
I would actually prefer debugfs for this, but this is in any case not primarily for debugging and validation.
Quoted 14 lines
Quoted 4 lines
The initial goal was to supply the user space some way to control the EC's power related policies using UCSI commands such as
SET_POWER_LEVELandGET_POWER_LEVEL(guys, please correct me if I got that wrong).It generally feels that exporting the whole unmoderated channel to the firmware just to set power level is wrong. It should be interfaced through the PSY driver.
Quoted 5 lines
But I'm now again wondering could those power policy tasks be handled using the UCSI power supplies after all? Venkat, did you look into that?
We are looking into this to figure out if there is any best way to expose power level settings options using UCSI power supply class interface. But even so, I believe that also does not completely eliminate risk of any malicious use.
At present we are working on an application/ user space service which will be calling UCSI read/write power level commands. But in future there would be more such applications which may require additional UCSI commands to use. We wanted to have a common and generic solution - and hence thought of going with sysfs interface.
Issue with debugfs is, it is default disabled in release kernels. User has to rebuild the kernel if the application is based on the debugfs interface. This will become a bottleneck for wider use of such appliances.
Can we still think of going ahead with sysfs interface and double make sure to make this accessible only with root/su privilege to minimize any potential risk of bad uses?
Quoted 8 lines
Quoted 4 lines
thanks,
--
heikki--
With best wishes
Dmitry
Greg Kroah-Hartman
maintainerRe: [PATCH v1 1/2] usb: typec: ucsi: Command mailbox interface for the userspace
Quoted 15 lines · Asutosh
Quoted 14 lines · Pathak Asutosh
Quoted 13 lines · Heikki Krogerus
Quoted 12 lines · Greg Kroah-Hartman
Quoted 10 lines · Heikki Krogerus
Some of the UCSI commands can be used to configure the entire Platform Policy Manager (PPM) instead of just individual connectors. To allow the user space communicate those commands with the PPM, adding a mailbox interface. The interface is a single attribute file that represents the main "OPM to PPM" UCSI data structure.
The mailbox allows any UCSI command to be sent to the PPM so it should be also useful for validation, testing and debugging purposes.
Quoted 5 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line
As it's for this type of thing, why not put it in debugfs instead?
The intend of this sysfs is not limited to validation, testing and debugging purposes but rather providing interface for major user space application developments.
But that's not what you are saying above. sysfs is for attributes of a device, NOT for full device control. Use a proper api for that that can be correctly mediated if needed.
Quoted 5 lines
At present we are working on an application/ user space service which will be calling UCSI read/write power level commands. But in future there would be more such applications which may require additional UCSI commands to use. We wanted to have a common and generic solution - and hence thought of going with sysfs interface.
We can't take new user/kernel apis without a real user, so please hold off on this series until you have a real user. Otherwise it is guaranteed that you will have to change that api based on actually using it.
Quoted 3 lines
Issue with debugfs is, it is default disabled in release kernels. User has to rebuild the kernel if the application is based on the debugfs interface. This will become a bottleneck for wider use of such appliances.
It is up to the distro to enable/disable debugfs, that's not our issue. debugfs is NOT for normal system operation, so if you want to make this a proper api for normal users, than no, don't use debugfs, make it a real api. Which is probably NOT going to be sysfs.
Quoted 54 lines
Quoted 54 lines
Quoted 26 lines
Quoted 26 lines
Quoted 26 lines
drivers/usb/typec/ucsi/sysfs.c new file +26
-0+static ssize_t ucsi_write (struct file *filp,struct kobject *kobj, +const struct bin_attribute *attr, +char *buf,loff_t off,size_t count) +{ +struct ucsi_sysfs *sysfs = attr->private; +struct ucsi *ucsi = sysfs->ucsi; +int ret; + +u64 *control = (u64 *)&sysfs->mailbox[UCSI_CONTROL ]; +u32 *cci = (u32 *)&sysfs->mailbox[UCSI_CCI ]; +void *data = &sysfs->mailbox[UCSI_MESSAGE_IN ]; + +/* TODO: MESSAGE_OUT. */ +if (off !=UCSI_CONTROL || count !=sizeof (*control)) +return -EFAULT ; + +mutex_lock (&sysfs->lock); + +memset (data,0 ,UCSI_MAX_DATA_LENGTH (ucsi)); + +/* PPM_RESET has to be handled separately. */ + *control =get_unaligned_le64 (buf); +if (UCSI_COMMAND (*control) ==UCSI_PPM_RESET ) { + ret =ucsi_reset_ppm (ucsi, cci); + goto out_unlock_sysfs; + }Quoted 8 lines
Quoted 8 lines
drivers/usb/typec/ucsi/sysfs.c
Quoted 8 lines
drivers/usb/typec/ucsi/sysfs.c new file +8
-0+ +mutex_lock (&ucsi->ppm_lock); + + ret = ucsi->ops->sync_control (ucsi, *control, cci,NULL ,0 ); +if (ret) + goto out_unlock_ppm; + + if (UCSI_CCI_LENGTH(*cci) && ucsi->ops->read_message_in(ucsi, data,
UCSI_CCI_LENGTH(*cci)))Quoted 3 lines
Quoted 3 lines
drivers/usb/typec/ucsi/sysfs.c
Quoted 3 lines
drivers/usb/typec/ucsi/sysfs.c new file +3
-0+dev_err (ucsi->dev,"failed to read MESSAGE_IN\n" ); + + ret = ucsi->ops->sync_control(ucsi, UCSI_ACK_CC_CI |
UCSI_ACK_COMMAND_COMPLETE,Quoted 15 lines
Quoted 11 lines
drivers/usb/typec/ucsi/sysfs.c line 83
Quoted 8 lines
drivers/usb/typec/ucsi/sysfs.c new file +8
-0+ NULL, NULL, 0); +out_unlock_ppm: +mutex_unlock (&ucsi->ppm_lock); +out_unlock_sysfs: +mutex_unlock (&sysfs->lock); + +return ret ?: count; +}This worries me, any userspace tool can now do this? What other "bad" things can it to the connection?
Although, there is actually only a limited number of things that you can do to the connection using UCSI, that is definitely a concern.
Quoted 7 lines
Quoted 4 lines
Quoted 4 lines
The PPM (which is the EC firmware in most cases) is expected to prevent any harmful or "unauthorized" UCSI commands from being executed, but I'm not sure there is any guarantees for that at the moment.
Critical power setting related features and options are tightly controlled by PPM/LPM. In such cases, those UCSI command request by user space will be blocked by PPM/LPM and will eventually end of into DoS.
What is "PPM/LPM"? I don't see that here.
Quoted 3 lines
Moreover, to further mitigate the risk of any malicious attack our understanding is this sysfs interface will be accessible only with root or super user privilege.
Is it? You really want normal users being forced to be root in order to talk to this device?
Make this a real api please, don't try to just do "provide raw access to the hardware and we will hope any userspace program can get it right", that way lies madness :)
Quoted 3 lines
Can we still think of going ahead with sysfs interface and double make sure to make this accessible only with root/su privilege to minimize any potential risk of bad uses?
Nope! Get it right please, once you add it, you can't remove it.
thanks,
greg k-h
Heikki Krogerus
maintainerPATCH 2/2 tools: usb: UCSI command testing tool
In reply to Heikki KrogerusA tool that can be used to send UCSI commands to UCSI mailboxes. The tool outputs the contents of the mailbox that result from the command execution.
On most systems there will be only one UCSI interface, but if the system has for example discrete GPUs with USB Type-C connectors, each GPU card may expose its own UCSI. All detected UCSI interfaces can be listed with -l option.
The UCSI interface that the command is meant for can be optionally specified with -d option. By default, when the interface is not specified, the first found will used.
--- tools/usb/.gitignore | 1 + tools/usb/Build | 1 + tools/usb/Makefile | 8 +- tools/usb/ucsi.c | 250 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 259 insertions(+), 1 deletion(-) create mode 100644 tools/usb/ucsi.c
tools/usb/.gitignore +1 -0
@@ -1,3 +1,4 @@ # SPDX-License-Identifier: GPL-2.0-only ffs-test testusb +ucsi
tools/usb/Build +1 -0
@@ -1,2 +1,3 @@ testusb-y += testusb.o ffs-test-y += ffs-test.o +ucsi-y += ucsi.o
tools/usb/Makefile +7 -1
@@ -16,7 +16,7 @@ MAKEFLAGS += -r override CFLAGS += -O2 -Wall -Wextra -g -D_GNU_SOURCE -I$(OUTPUT)include -I$(srctree)/tools/include override LDFLAGS += -lpthread -ALL_TARGETS := testusb ffs-test +ALL_TARGETS := testusb ffs-test ucsi ALL_PROGRAMS := $(patsubst %,$(OUTPUT)%,$(ALL_TARGETS)) all: $(ALL_PROGRAMS)
@@ -36,6 +36,12 @@ $(FFS_TEST_IN): FORCE $(OUTPUT)ffs-test: $(FFS_TEST_IN) $(QUIET_LINK)$(CC) $(CFLAGS) $< -o $@ $(LDFLAGS) +UCSI_IN := $(OUTPUT)ucsi-in.o +$(UCSI_IN): FORCE + $(Q)$(MAKE) $(build)=ucsi +$(OUTPUT)ucsi: $(UCSI_IN) + $(QUIET_LINK)$(CC) $(CFLAGS) $< -o $@ $(LDFLAGS) + clean: rm -f $(ALL_PROGRAMS) find $(or $(OUTPUT),.) -name '*.o' -delete -o -name '\.*.d' -delete -o -name '\.*.o.cmd' -delete
tools/usb/ucsi.c new file +250 -0
250 lines not on this page · raw message
Signature · 1 line
2.47.2