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 25, 2024
1 parent aa4163b commit 8e831df
Showing 1 changed file with 30 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

0 comments on commit 8e831df

Please sign in to comment.