test_csi.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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_csi_ioctl.h"
  18. #define DEV_PATHNAME "/dev/bm_csi0"
  19. typedef struct _csi_ctx
  20. {
  21. int dev_fd;
  22. } csi_ctx_t;
  23. static csi_ctx_t csi_ctx;
  24. static int csi_init(csi_ctx_t *ctx)
  25. {
  26. int fd;
  27. memset(&csi_ctx, 0, sizeof(csi_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 csi_deinit(csi_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 csi_ioctl(csi_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_csi_reg_t reg;
  64. reg.offset = 0;
  65. ret = csi_init(&csi_ctx);
  66. if (ret != 0)
  67. exit(ret);
  68. int cmd = BMCSI_IOC_S_RESET;
  69. while(cmd < BMCSI_IOC_MAX) {
  70. ret = csi_ioctl(&csi_ctx, cmd, &reg);
  71. if (ret != 0) {
  72. printf("!!!!!!!!!!!!!!!!!!!error\n");
  73. }
  74. cmd++;
  75. }
  76. ret = csi_deinit(&csi_ctx);
  77. if (ret != 0)
  78. exit(ret);
  79. exit(0);
  80. }