Skip to content

Commit

Permalink
examples: linux: unbind rpmsg device
Browse files Browse the repository at this point in the history
RPMsg linux app should unbind rpmsg device from char driver once
communication is done. Without unbind, rpmsg app can't bind same
device with rpmsg char driver again.

Signed-off-by: Tanmay Shah <[email protected]>
  • Loading branch information
tnmysh committed Jun 28, 2024
1 parent 132e8a4 commit a364e30
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 0 deletions.
27 changes: 27 additions & 0 deletions examples/linux/common/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,33 @@ int bind_rpmsg_chrdev(const char *rpmsg_dev_name)
return 0;
}

int unbind_rpmsg_chrdev(const char *rpmsg_dev_name)
{
char fpath[256];
char *rpmsg_chdrv = "rpmsg_chrdev";
int fd;
int ret;

/* unbind the rpmsg device to rpmsg char driver */
sprintf(fpath, "%s/drivers/%s/unbind", RPMSG_BUS_SYS, rpmsg_chdrv);
fd = open(fpath, O_WRONLY);
if (fd < 0) {
fprintf(stderr, "Failed to open %s, %s\n",
fpath, strerror(errno));
return -EINVAL;
}
printf("write %s to %s\n", rpmsg_dev_name, fpath);
ret = write(fd, rpmsg_dev_name, strlen(rpmsg_dev_name) + 1);
if (ret < 0) {
fprintf(stderr, "Failed to write %s to %s, %s\n",
rpmsg_dev_name, fpath, strerror(errno));
close(fd);
return -EINVAL;
}
close(fd);
return 0;
}

int get_rpmsg_chrdev_fd(const char *rpmsg_dev_name, char *rpmsg_ctrl_name)
{
char dpath[2*NAME_MAX];
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 @@ -12,6 +12,7 @@ char *get_rpmsg_ept_dev_name(const char *rpmsg_char_name,
const char *ept_name,
char *ept_dev_name);
int bind_rpmsg_chrdev(const char *rpmsg_dev_name);
int unbind_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);

Expand Down
4 changes: 4 additions & 0 deletions examples/linux/rpmsg-echo-test/echo_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,10 @@ int main(int argc, char *argv[])
printf("****\r\n");
}

ret = unbind_rpmsg_chrdev(rpmsg_dev);
if (ret < 0)
return ret;

send_shutdown(fd);
free(i_payload);
free(r_payload);
Expand Down
4 changes: 4 additions & 0 deletions examples/linux/rpmsg-mat-mul/mat_mul_demo.c
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,10 @@ int main(int argc, char *argv[])
printf("End of Matrix multiplication demo round %d\n", i);
}

ret = unbind_rpmsg_chrdev(rpmsg_dev);
if (ret < 0)
return ret;

send_shutdown(fd);
close(fd);
if (charfd >= 0)
Expand Down
4 changes: 4 additions & 0 deletions examples/linux/rpmsg-proxy-app/proxy_app.c
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,10 @@ int main(int argc, char *argv[])
/* Close proxy rpmsg device */
close(proxy->rpmsg_proxy_fd);

ret = unbind_rpmsg_chrdev(rpmsg_dev_name);
if (ret < 0)
return ret;

/* Wait for other end to cleanup
* Otherwise, virtio_rpmsg_bus can post msg with no recipient
* warning as it can receive NS destroy after the rpmsg char
Expand Down

0 comments on commit a364e30

Please sign in to comment.