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 24, 2024
1 parent aa4163b commit 610c3ea
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
32 changes: 30 additions & 2 deletions examples/linux/common/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,20 +66,48 @@ char *get_rpmsg_ept_dev_name(const char *rpmsg_char_name,
int bind_rpmsg_chrdev(const char *rpmsg_dev_name)
{
char fpath[256];
char *rpmsg_chdrv = "rpmsg_chrdev";
const char *rpmsg_chdrv = "rpmsg_chrdev";
char drv_override[64] = {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);
fd = open(fpath, O_RDWR);
if (fd < 0) {
fprintf(stderr, "Failed to open %s, %s\n",
fpath, strerror(errno));
return -EINVAL;
}

ret = read(fd, drv_override, sizeof(drv_override));
if (ret < 0) {
fprintf(stderr, "Failed to read %s (%s)\n",
fpath, strerror(errno));
close(fd);
return ret;
}

printf("current drv override = %s\n", drv_override);

/*
* Check driver override. If "rpmsg_chrdev" string is
* found, then don't attempt to bind. If null string is found,
* then no driver is bound, and attempt to bind rpmsg char driver.
* Any other case, fail binding driver, as device is busy.
*/
if (strncmp(drv_override, rpmsg_chdrv, strlen(rpmsg_chdrv)) == 0) {
close(fd);
return 0;
} else if (strncmp(drv_override, "(null)", strlen("(null)")) != 0) {
printf("error: device %s is busy, drv bind=%s\n",
rpmsg_dev_name, drv_override);
close(fd);
return -EBUSY;
}

ret = write(fd, rpmsg_chdrv, strlen(rpmsg_chdrv) + 1);
if (ret < 0) {
fprintf(stderr, "Failed to write %s to %s, %s\n",
Expand Down
1 change: 1 addition & 0 deletions examples/linux/rpmsg-echo-test/echo_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ 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;
Expand Down
1 change: 1 addition & 0 deletions examples/linux/rpmsg-mat-mul/mat_mul_demo.c
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ 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;
Expand Down

0 comments on commit 610c3ea

Please sign in to comment.