pandev.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*
  2. * © Copyright 2017 The BiOpenly Community
  3. *
  4. * This program is free software and is provided to you under the terms of the
  5. * GNU General Public License version 2 as published by the Free Software
  6. * Foundation, and any use by you of this program is subject to the terms
  7. * of such GNU licence.
  8. *
  9. * A copy of the licence is included with the program, and can also be obtained
  10. * from Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  11. * Boston, MA 02110-1301, USA.
  12. *
  13. */
  14. #include <stdio.h>
  15. #include <fcntl.h>
  16. #include <sys/stat.h>
  17. #include <sys/ioctl.h>
  18. #include <linux/ioctl.h>
  19. #include <mali-ioctl.h>
  20. static int
  21. pandev_get_driver_version(int fd, unsigned *major, unsigned *minor)
  22. {
  23. struct mali_ioctl_get_version args = {0};
  24. int rc;
  25. /* So far this seems to be the only ioctl that uses 0x80 for dir */
  26. rc = ioctl(fd, MALI_IOCTL_GET_VERSION, &args);
  27. if (rc)
  28. return rc;
  29. *major = args.major;
  30. *minor = args.minor;
  31. return 0;
  32. }
  33. /**
  34. * Open the device file for communicating with the mali kernelspace driver,
  35. * and make sure it's a version of the kernel driver we're familiar with.
  36. *
  37. * Returns: fd on success, -1 on failure
  38. */
  39. int
  40. pandev_open()
  41. {
  42. int fd = open("/dev/mali0", O_RDWR | O_NONBLOCK | O_CLOEXEC),
  43. rc;
  44. unsigned major, minor;
  45. if (fd < 0)
  46. return fd;
  47. rc = pandev_get_driver_version(fd, &major, &minor);
  48. if (rc)
  49. return rc;
  50. printf("Found kernel driver version v%d.%d at /dev/mali0\n",
  51. major, minor);
  52. /* We only support using v10 since this is the kernel driver version
  53. * HiKey 960's come with pre-built on Android. Mali changes things a
  54. * lot, so it's not worth the effort to support anything else
  55. */
  56. if (major != 10) {
  57. fprintf(stderr,
  58. "Warning! This has only been tested with v10 of the "
  59. "Bifrost kernel driver. There is no guarantee anything "
  60. "will work with this version.\n");
  61. }
  62. return fd;
  63. }