Skip to content

Commit

Permalink
examples: linux: check rpmsg dev bind status
Browse files Browse the repository at this point in the history
Check rpmsg device driver override and if correct driver
is already bound, then execute rest of application. If driver
is not bound, then bind rpmsg char driver.

Signed-off-by: Tanmay Shah <[email protected]>
  • Loading branch information
tnmysh committed Jul 17, 2024
1 parent aa4163b commit 690b6ab
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 9 deletions.
29 changes: 29 additions & 0 deletions examples/linux/common/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,35 @@ char *get_rpmsg_ept_dev_name(const char *rpmsg_char_name,
return NULL;
}

int is_rpmsg_chrdev_bind(const char *rpmsg_dev_name)
{
char fpath[256];
char rpmsg_chdrv[32] = {0};
int fd;
int ret;

/* rpmsg dev overrides path */
sprintf(fpath, "%s/devices/%s/driver_override",
RPMSG_BUS_SYS, rpmsg_dev_name);
printf("open %s\n", fpath);
fd = open(fpath, O_WRONLY);
if (fd < 0) {
fprintf(stderr, "Failed to open %s, %s\n",
fpath, strerror(errno));
return -EINVAL;
}
ret = read(fd, rpmsg_chdrv, sizeof(rpmsg_chdrv));
if (ret < 0) {
fprintf(stderr, "Failed to write %s to %s, %s\n",
rpmsg_chdrv, fpath, strerror(errno));
close(fd);
return -EINVAL;
}
close (fd);

return (strcmp(rpmsg_chdrv, "rpmsg_chrdev") == 0);
}

int bind_rpmsg_chrdev(const char *rpmsg_dev_name)
{
char fpath[256];
Expand Down
1 change: 1 addition & 0 deletions examples/linux/common/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ char *get_rpmsg_ept_dev_name(const char *rpmsg_char_name,
int bind_rpmsg_chrdev(const char *rpmsg_dev_name);
int get_rpmsg_chrdev_fd(const char *rpmsg_dev_name, char *rpmsg_ctrl_name);
int lookup_channel(char *out, struct rpmsg_endpoint_info *pep);
int is_rpmsg_chrdev_bind(const char *rpmsg_dev_name);

#endif /* __COMMON__H__ */
9 changes: 6 additions & 3 deletions examples/linux/rpmsg-echo-test/echo_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,12 @@ int main(int argc, char *argv[])
fprintf(stderr, "access(%s): %s\n", fpath, strerror(errno));
return -EINVAL;
}
ret = bind_rpmsg_chrdev(rpmsg_dev);
if (ret < 0)
return ret;

if (!is_rpmsg_chrdev_bind(rpmsg_dev)) {
ret = bind_rpmsg_chrdev(rpmsg_dev);
if (ret < 0)
return ret;
}

/* kernel >= 6.0 has new path for rpmsg_ctrl device */
charfd = get_rpmsg_chrdev_fd(rpmsg_ctrl_dev_name, rpmsg_char_name);
Expand Down
9 changes: 6 additions & 3 deletions examples/linux/rpmsg-mat-mul/mat_mul_demo.c
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,12 @@ int main(int argc, char *argv[])
fprintf(stderr, "access(%s): %s\n", fpath, strerror(errno));
return -EINVAL;
}
ret = bind_rpmsg_chrdev(rpmsg_dev);
if (ret < 0)
return ret;

if (!is_rpmsg_chrdev_bind(rpmsg_dev)) {
ret = bind_rpmsg_chrdev(rpmsg_dev);
if (ret < 0)
return ret;
}

/* The Linux kernel >= 6.0 expects rpmsg_ctrl interface under virtio*.rpmsg_ctrl*.* dir */
charfd = get_rpmsg_chrdev_fd(rpmsg_ctrl_dev_name, rpmsg_char_name);
Expand Down
8 changes: 5 additions & 3 deletions examples/linux/rpmsg-proxy-app/proxy_app.c
Original file line number Diff line number Diff line change
Expand Up @@ -253,9 +253,11 @@ int main(int argc, char *argv[])
if (ret < 0)
goto error0;

ret = bind_rpmsg_chrdev(rpmsg_dev_name);
if (ret < 0)
goto error0;
if (!is_rpmsg_chrdev_bind(rpmsg_dev_name)) {
ret = bind_rpmsg_chrdev(rpmsg_dev_name);
if (ret < 0)
goto error0;
}

/* The Linux kernel version >= 6.0 uses rpmsg_ctrl from virtio*.rpmsg_ctrl* dir */
rpmsg_char_fd = get_rpmsg_chrdev_fd(rpmsg_ctrl_dev_name, rpmsg_char_name);
Expand Down

0 comments on commit 690b6ab

Please sign in to comment.