test_visys.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. * Copyright (C) 2021 Alibaba Group Holding Limited
  3. * Author: LuChongzhi <chongzhi.lcz@alibaba-inc.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. */
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <unistd.h>
  12. #include <string.h>
  13. #include <errno.h>
  14. #include <sys/ioctl.h>
  15. #include <fcntl.h>
  16. #include <poll.h>
  17. #include "bm_visys_ioctl.h"
  18. #define DEV_PATHNAME "/dev/bm_visys0"
  19. typedef struct _visys_ctx
  20. {
  21. int dev_fd;
  22. } visys_ctx_t;
  23. static visys_ctx_t visys_ctx;
  24. static int visys_init(visys_ctx_t *ctx)
  25. {
  26. int fd;
  27. memset(&visys_ctx, 0, sizeof(visys_ctx));
  28. fd = open(DEV_PATHNAME, O_RDWR | O_NONBLOCK);
  29. if (fd < 0) {
  30. printf("%s:Can't open %s: %d(%s)\n", __func__,
  31. DEV_PATHNAME, errno, strerror(errno));
  32. return -1;
  33. }
  34. ctx->dev_fd = fd;
  35. return 0;
  36. }
  37. static int visys_deinit(visys_ctx_t *ctx)
  38. {
  39. if (ctx->dev_fd >= 0) {
  40. close(ctx->dev_fd);
  41. ctx->dev_fd = -1;
  42. }
  43. return 0;
  44. }
  45. static int visys_ioctl(visys_ctx_t *ctx, unsigned int cmd, void *args)
  46. {
  47. int ret;
  48. if (ctx == NULL || ctx->dev_fd < 0) {
  49. printf("%s:ctx is invalid\n", __func__);
  50. return -1;
  51. }
  52. ret = ioctl(ctx->dev_fd, cmd, args);
  53. if (ret != 0) {
  54. printf("%s:ioctl failed, ret=%d(%s)\n", __func__,
  55. ret, strerror(ret * -1));
  56. return ret;
  57. }
  58. return 0;
  59. }
  60. int main(void)
  61. {
  62. int ret;
  63. struct bm_visys_reg_t reg;
  64. ret = visys_init(&visys_ctx);
  65. if (ret != 0)
  66. exit(ret);
  67. reg.offset = 0x4; // should be 0x5000008b in reset
  68. ret = visys_ioctl(&visys_ctx, BMVISYSIOC_READ_REG, &reg);
  69. if (ret == 0) {
  70. printf("%s:Read reg OK, offset=0x%08x, value=0x%08x\n", __func__,
  71. reg.offset, reg.value);
  72. }
  73. ret = visys_deinit(&visys_ctx);
  74. if (ret != 0)
  75. exit(ret);
  76. exit(0);
  77. }