pmsg: core: Add support for name extension
Mathieu Poirier
maintainerv4 cover pmsg: core: Add support for name extension
This patchset adds the capability to supplement the base definition published by an rpmsg_driver with a postfix description so that it is easy to differentiate entities that use the same name service.
Offers the same functionality as v3[1] but was tweaked to keep the robot happy[2].
Applies cleanly on rpmsg-next (4a3c6e2327e6).
Thanks,
Mathieu
[1]. https://patchwork.kernel.org/project/linux-remoteproc/list/?series=281005
[2]. https://patchwork.kernel.org/patch/11523237/
Mathieu Poirier (2):
rpmsg: core: Add wildcard match for name service
rpmsg: core: Add support to retrieve name extension
drivers/rpmsg/rpmsg_core.c | 112 ++++++++++++++++++++++++++++++++++++- include/linux/rpmsg.h | 13 +++++ 2 files changed, 124 insertions(+), 1 deletion(-)
Signature · 1 line
2.20.1
Mathieu Poirier
maintainerPATCH v4 1/2 rpmsg: core: Add wildcard match for name service
Adding the capability to supplement the base definition published by an rpmsg_driver with a postfix description so that it is possible for several entity to use the same service.
--- drivers/rpmsg/rpmsg_core.c | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-)
drivers/rpmsg/rpmsg_core.c +19 -1
@@ -399,7 +399,25 @@ ATTRIBUTE_GROUPS(rpmsg_dev);static inline int rpmsg_id_match (const struct rpmsg_device *rpdev,const struct rpmsg_device_id *id) { -return strncmp (id->name, rpdev->id.name,RPMSG_NAME_SIZE ) ==0 ; +size_t len =min_t (size_t,strlen (id->name),RPMSG_NAME_SIZE ); + +/* +* Allow for wildcard matches. For example if rpmsg_driver::id_table +* is: +* +* static struct rpmsg_device_id rpmsg_driver_sample_id_table[] = { +* { .name = "rpmsg-client-sample" }, +* { }, +* } +* +* Then it is possible to support "rpmsg-client-sample*", i.e: +* rpmsg-client-sample +* rpmsg-client-sample_instance0 +* rpmsg-client-sample_instance1 +* ... +* rpmsg-client-sample_instanceX +*/ +return strncmp (id->name, rpdev->id.name, len) ==0 ; }/* match rpmsg channel and rpmsg driver */
Signature · 1 line
2.20.1
Mathieu Poirier
maintainerPATCH v4 2/2 rpmsg: core: Add support to retrieve name extension
In reply to Mathieu PoirierAfter adding support for rpmsg device name extension, this patch provides a function that returns the extension portion of an rpmsg device name. That way users of the name extension functionality don't have to write the same boiler plate code to extract the information.
--- drivers/rpmsg/rpmsg_core.c | 92 ++++++++++++++++++++++++++++++++++++++ include/linux/rpmsg.h | 13 ++++++ 2 files changed, 105 insertions(+)
drivers/rpmsg/rpmsg_core.c +92 -0
@@ -439,6 +439,98 @@ static int rpmsg_dev_match(struct device *dev, struct device_driver *drv)return of_driver_match_device (dev, drv); } +/** +* rpmsg_device_get_name_extension() - get the name extension of a rpmsg device +* @rpdev: the rpmsg device to work with +* @skip: how many characters in the extension should be skipped over +* +* With function rpmsg_id_match() allowing for extension of the base driver name +* in order to differentiate services, this function returns the extension part +* of an rpmsg device name. As such and with the following rpmsg driver device +* id table and rpmsg device names: +* +* static struct rpmsg_device_id rpmsg_driver_sample_id_table[] = { +* { .name = "rpmsg-client-sample" }, +* { }, +* } +* +* rpdev1->id.name == "rpmsg-client-sample"; +* rpdev2->id.name == "rpmsg-client-sample_instance0"; +* +* Calling rpmsg_device_get_name_extension() will yields the following: +* +* rpmsg_device_get_name_extension(rpdev1, 0) == NULL; +* rpmsg_device_get_name_extension(rpdev2, 0) == "_instance0"; +* rpmsg_device_get_name_extension(rpdev2, 1) == "instance0"; +* +* +* Note: The name extension should be free'd using kfree_const(). +* +* Return: the name extension if found, NULL if not found and an error +* code otherwise. +*/ +const char *rpmsg_device_get_name_extension (struct rpmsg_device *rpdev, +unsigned int skip) +{ +const char *drv_name, *dev_name, *extension; +const struct rpmsg_device_id *ids; +struct device *dev = &rpdev->dev; +struct rpmsg_driver *rpdrv; +bool match = false; +unsigned int i; + +if (!dev->driver) +return ERR_PTR (-EINVAL ); + + rpdrv =to_rpmsg_driver (dev->driver); + +/* +* No point in going further if the device and the driver don't +* have a name or a table to work with. +*/ +if (!rpdev->id.name[0 ] || !rpdrv->id_table) +return ERR_PTR (-EINVAL ); + + ids = rpdrv->id_table; + dev_name = rpdev->id.name; + +/* +* See if any name in the driver's table match the beginning +* of the rpmsg device's name. +*/ +for (i =0 ; ids[i].name[0 ]; i++) { + drv_name = ids[i].name; +if (strncmp (drv_name, + dev_name,strlen (drv_name)) ==0 ) { + match = true; +break ; + } + } + +if (!match) +return NULL ; + +/* No name extension to return if device and driver are the same */ +if (strlen (dev_name) ==strlen (drv_name)) +return NULL ; + +/* +* Make sure we were not requested to skip past the end +* of the device name. +*/ +if (strlen (drv_name) + skip >=strlen (dev_name)) +return ERR_PTR (-EINVAL ); + +/* +* Move past the base name published by the driver and +* skip any extra characters if needed. +*/ + extension = dev_name +strlen (drv_name) + skip; + +return kstrdup_const (extension,GFP_KERNEL ); +} +EXPORT_SYMBOL (rpmsg_device_get_name_extension); +static int rpmsg_uevent (struct device *dev,struct kobj_uevent_env *env) {struct rpmsg_device *rpdev =to_rpmsg_device (dev);
include/linux/rpmsg.h +13 -0
@@ -135,6 +135,9 @@ int rpmsg_trysend_offchannel(struct rpmsg_endpoint *ept, u32 src, u32 dst,__poll_t rpmsg_poll (struct rpmsg_endpoint *ept,struct file *filp,poll_table *wait); +const char *rpmsg_device_get_name_extension (struct rpmsg_device *dev, +unsigned int skip); +#else static inline int register_rpmsg_device (struct rpmsg_device *dev)
@@ -242,6 +245,16 @@ static inline __poll_t rpmsg_poll(struct rpmsg_endpoint *ept, return 0; } +static inline +const char *rpmsg_device_get_name_extension (struct rpmsg_device *dev, +unsigned int skip) +{ +/* This shouldn't be possible */ +WARN_ON (1 ); + +return NULL ; +} +#endif /* IS_ENABLED(CONFIG_RPMSG) */ /* use a macro to avoid include chaining to get THIS_MODULE */
Signature · 1 line
2.20.1
Re: [PATCH v4 2/2] rpmsg: core: Add support to retrieve name extension
Hi Mathieu,
Quoted 4 lines · Mathieu Poirier
After adding support for rpmsg device name extension, this patch provides a function that returns the extension portion of an rpmsg device name. That way users of the name extension functionality don't have to write the same boiler plate code to extract the information.
I do not test it yet,but LGTM.
I plan to use these patches for the rpmsg_tty. Please find few remarks below.
Quoted 76 lines
Suggested-by Suman Anna <[email protected]>;Signed-off-by Mathieu Poirier <[email protected]>--- drivers/rpmsg/rpmsg_core.c | 92 ++++++++++++++++++++++++++++++++++++++ include/linux/rpmsg.h | 13 ++++++ 2 files changed, 105 insertions(+)drivers/rpmsg/rpmsg_core.c +60
-0@@ -439,6 +439,98 @@ static int rpmsg_dev_match(struct device *dev, struct device_driver *drv)return of_driver_match_device (dev, drv); } +/** +* rpmsg_device_get_name_extension() - get the name extension of a rpmsg device +* @rpdev: the rpmsg device to work with +* @skip: how many characters in the extension should be skipped over +* +* With function rpmsg_id_match() allowing for extension of the base driver name +* in order to differentiate services, this function returns the extension part +* of an rpmsg device name. As such and with the following rpmsg driver device +* id table and rpmsg device names: +* +* static struct rpmsg_device_id rpmsg_driver_sample_id_table[] = { +* { .name = "rpmsg-client-sample" }, +* { }, +* } +* +* rpdev1->id.name == "rpmsg-client-sample"; +* rpdev2->id.name == "rpmsg-client-sample_instance0"; +* +* Calling rpmsg_device_get_name_extension() will yields the following: +* +* rpmsg_device_get_name_extension(rpdev1, 0) == NULL; +* rpmsg_device_get_name_extension(rpdev2, 0) == "_instance0"; +* rpmsg_device_get_name_extension(rpdev2, 1) == "instance0"; +* +* +* Note: The name extension should be free'd using kfree_const(). +* +* Return: the name extension if found, NULL if not found and an error +* code otherwise. +*/ +const char *rpmsg_device_get_name_extension (struct rpmsg_device *rpdev, +unsigned int skip) +{ +const char *drv_name, *dev_name, *extension; +const struct rpmsg_device_id *ids; +struct device *dev = &rpdev->dev; +struct rpmsg_driver *rpdrv; +bool match = false; +unsigned int i; + +if (!dev->driver) +return ERR_PTR (-EINVAL ); + + rpdrv =to_rpmsg_driver (dev->driver); + +/* +* No point in going further if the device and the driver don't +* have a name or a table to work with. +*/ +if (!rpdev->id.name[0 ] || !rpdrv->id_table) +return ERR_PTR (-EINVAL ); + + ids = rpdrv->id_table; + dev_name = rpdev->id.name; + +/* +* See if any name in the driver's table match the beginning +* of the rpmsg device's name. +*/ +for (i =0 ; ids[i].name[0 ]; i++) {
drivers/rpmsg/rpmsg_core.c static int rpmsg_dev_match(struct device *dev, struct device_driver *drv) · line 511
Quoted 10 lines
drivers/rpmsg/rpmsg_core.c +10
-0+ drv_name = ids[i].name; +if (strncmp (drv_name, + dev_name,strlen (drv_name)) ==0 ) { + match = true; +break ; + } + } + +if (!match) +return NULL ;
here i would return an error to differentiate unmatch and name without extension.
Quoted 9 lines
drivers/rpmsg/rpmsg_core.c +9
-0+ +/* No name extension to return if device and driver are the same */ +if (strlen (dev_name) ==strlen (drv_name)) +return NULL ; + +/* +* Make sure we were not requested to skip past the end +* of the device name. +*/
drivers/rpmsg/rpmsg_core.c static int rpmsg_dev_match(struct device *dev, struct device_driver *drv) · line 530
Quoted 10 lines
drivers/rpmsg/rpmsg_core.c +10
-0+if (strlen (drv_name) + skip >=strlen (dev_name)) +return ERR_PTR (-EINVAL ); + +/* +* Move past the base name published by the driver and +* skip any extra characters if needed. +*/ + extension = dev_name +strlen (drv_name) + skip; + +return kstrdup_const (extension,GFP_KERNEL );
what about just returning the extension pointer? rpdev->id.name should be valid until device is unregistered.
Regards
Arnaud
Quoted 38 lines
drivers/rpmsg/rpmsg_core.c +3
-0+} +EXPORT_SYMBOL (rpmsg_device_get_name_extension); +static int rpmsg_uevent (struct device *dev,struct kobj_uevent_env *env) {struct rpmsg_device *rpdev =to_rpmsg_device (dev);include/linux/rpmsg.h +13
-0@@ -135,6 +135,9 @@ int rpmsg_trysend_offchannel(struct rpmsg_endpoint *ept, u32 src, u32 dst,__poll_t rpmsg_poll (struct rpmsg_endpoint *ept,struct file *filp,poll_table *wait); +const char *rpmsg_device_get_name_extension (struct rpmsg_device *dev, +unsigned int skip); +#else static inline int register_rpmsg_device (struct rpmsg_device *dev)@@ -242,6 +245,16 @@ static inline __poll_t rpmsg_poll(struct rpmsg_endpoint *ept, return 0; } +static inline +const char *rpmsg_device_get_name_extension (struct rpmsg_device *dev, +unsigned int skip) +{ +/* This shouldn't be possible */ +WARN_ON (1 ); + +return NULL ; +} +#endif /* IS_ENABLED(CONFIG_RPMSG) */ /* use a macro to avoid include chaining to get THIS_MODULE */
Mathieu Poirier
maintainerRe: [PATCH v4 2/2] rpmsg: core: Add support to retrieve name extension
Quoted 93 lines · Arnaud POULIQUEN
Hi Mathieu,
Quoted 4 lines · Mathieu Poirier
After adding support for rpmsg device name extension, this patch provides a function that returns the extension portion of an rpmsg device name. That way users of the name extension functionality don't have to write the same boiler plate code to extract the information.
I do not test it yet,but LGTM.
I plan to use these patches for therpmsg_tty. Please find few remarks below.Quoted 79 lines
Suggested-by Suman Anna <[email protected]>;Signed-off-by Mathieu Poirier <[email protected]>--- drivers/rpmsg/rpmsg_core.c | 92 ++++++++++++++++++++++++++++++++++++++ include/linux/rpmsg.h | 13 ++++++ 2 files changed, 105 insertions(+)drivers/rpmsg/rpmsg_core.c +63
-0@@ -439,6 +439,98 @@ static int rpmsg_dev_match(struct device *dev, struct device_driver *drv)return of_driver_match_device (dev, drv); } +/** +* rpmsg_device_get_name_extension() - get the name extension of a rpmsg device +* @rpdev: the rpmsg device to work with +* @skip: how many characters in the extension should be skipped over +* +* With function rpmsg_id_match() allowing for extension of the base driver name +* in order to differentiate services, this function returns the extension part +* of an rpmsg device name. As such and with the following rpmsg driver device +* id table and rpmsg device names: +* +* static struct rpmsg_device_id rpmsg_driver_sample_id_table[] = { +* { .name = "rpmsg-client-sample" }, +* { }, +* } +* +* rpdev1->id.name == "rpmsg-client-sample"; +* rpdev2->id.name == "rpmsg-client-sample_instance0"; +* +* Calling rpmsg_device_get_name_extension() will yields the following: +* +* rpmsg_device_get_name_extension(rpdev1, 0) == NULL; +* rpmsg_device_get_name_extension(rpdev2, 0) == "_instance0"; +* rpmsg_device_get_name_extension(rpdev2, 1) == "instance0"; +* +* +* Note: The name extension should be free'd using kfree_const(). +* +* Return: the name extension if found, NULL if not found and an error +* code otherwise. +*/ +const char *rpmsg_device_get_name_extension (struct rpmsg_device *rpdev, +unsigned int skip) +{ +const char *drv_name, *dev_name, *extension; +const struct rpmsg_device_id *ids; +struct device *dev = &rpdev->dev; +struct rpmsg_driver *rpdrv; +bool match = false; +unsigned int i; + +if (!dev->driver) +return ERR_PTR (-EINVAL ); + + rpdrv =to_rpmsg_driver (dev->driver); + +/* +* No point in going further if the device and the driver don't +* have a name or a table to work with. +*/ +if (!rpdev->id.name[0 ] || !rpdrv->id_table) +return ERR_PTR (-EINVAL ); + + ids = rpdrv->id_table; + dev_name = rpdev->id.name; + +/* +* See if any name in the driver's table match the beginning +* of the rpmsg device's name. +*/ +for (i =0 ; ids[i].name[0 ]; i++) { + drv_name = ids[i].name; +if (strncmp (drv_name, + dev_name,strlen (drv_name)) ==0 ) {
Quoted 8 lines
drivers/rpmsg/rpmsg_core.c static int rpmsg_dev_match(struct device *dev, struct device_driver *drv) · line 511
Quoted 7 lines
drivers/rpmsg/rpmsg_core.c +7
-0+ match = true; +break ; + } + } + +if (!match) +return NULL ;here i would return an error to differentiate unmatch and name without extension.
That's a fair point.
Quoted 13 lines
Quoted 13 lines
drivers/rpmsg/rpmsg_core.c +13
-0+ +/* No name extension to return if device and driver are the same */ +if (strlen (dev_name) ==strlen (drv_name)) +return NULL ; + +/* +* Make sure we were not requested to skip past the end +* of the device name. +*/ +if (strlen (drv_name) + skip >=strlen (dev_name)) +return ERR_PTR (-EINVAL ); + +/*
Quoted 8 lines
drivers/rpmsg/rpmsg_core.c static int rpmsg_dev_match(struct device *dev, struct device_driver *drv) · line 530
Quoted 6 lines
drivers/rpmsg/rpmsg_core.c +6
-0+* Move past the base name published by the driver and +* skip any extra characters if needed. +*/ + extension = dev_name +strlen (drv_name) + skip; + +return kstrdup_const (extension,GFP_KERNEL );what about just returning the extension pointer?
rpdev->id.nameshould be valid until device is unregistered.
I grappled with that... I didn't know all the scenarios people would use this for and where the returned string would end up so I decided to play it safe. Does anyone else have an opinion on this? Am I too cautious?
Quoted 42 lines
Regards
ArnaudQuoted 38 lines
drivers/rpmsg/rpmsg_core.c +3
-0+} +EXPORT_SYMBOL (rpmsg_device_get_name_extension); +static int rpmsg_uevent (struct device *dev,struct kobj_uevent_env *env) {struct rpmsg_device *rpdev =to_rpmsg_device (dev);include/linux/rpmsg.h +13
-0@@ -135,6 +135,9 @@ int rpmsg_trysend_offchannel(struct rpmsg_endpoint *ept, u32 src, u32 dst,__poll_t rpmsg_poll (struct rpmsg_endpoint *ept,struct file *filp,poll_table *wait); +const char *rpmsg_device_get_name_extension (struct rpmsg_device *dev, +unsigned int skip); +#else static inline int register_rpmsg_device (struct rpmsg_device *dev)@@ -242,6 +245,16 @@ static inline __poll_t rpmsg_poll(struct rpmsg_endpoint *ept, return 0; } +static inline +const char *rpmsg_device_get_name_extension (struct rpmsg_device *dev, +unsigned int skip) +{ +/* This shouldn't be possible */ +WARN_ON (1 ); + +return NULL ; +} +#endif /* IS_ENABLED(CONFIG_RPMSG) */ /* use a macro to avoid include chaining to get THIS_MODULE */