media_device_open.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. * media_device_open.c - Media Controller Device Open Test
  3. *
  4. * Copyright (c) 2016 Shuah Khan <shuahkh@osg.samsung.com>
  5. * Copyright (c) 2016 Samsung Electronics Co., Ltd.
  6. *
  7. * This file is released under the GPLv2.
  8. */
  9. /*
  10. * This file adds a test for Media Controller API.
  11. * This test should be run as root and should not be
  12. * included in the Kselftest run. This test should be
  13. * run when hardware and driver that makes use Media
  14. * Controller API are present in the system.
  15. *
  16. * This test opens user specified Media Device and calls
  17. * MEDIA_IOC_DEVICE_INFO ioctl, closes the file, and exits.
  18. *
  19. * Usage:
  20. * sudo ./media_device_open -d /dev/mediaX
  21. *
  22. * Run this test is a loop and run bind/unbind on the driver.
  23. */
  24. #include <stdio.h>
  25. #include <unistd.h>
  26. #include <stdlib.h>
  27. #include <errno.h>
  28. #include <string.h>
  29. #include <fcntl.h>
  30. #include <sys/ioctl.h>
  31. #include <sys/stat.h>
  32. #include <linux/media.h>
  33. int main(int argc, char **argv)
  34. {
  35. int opt;
  36. char media_device[256];
  37. int count = 0;
  38. struct media_device_info mdi;
  39. int ret;
  40. int fd;
  41. if (argc < 2) {
  42. printf("Usage: %s [-d </dev/mediaX>]\n", argv[0]);
  43. exit(-1);
  44. }
  45. /* Process arguments */
  46. while ((opt = getopt(argc, argv, "d:")) != -1) {
  47. switch (opt) {
  48. case 'd':
  49. strncpy(media_device, optarg, sizeof(media_device) - 1);
  50. media_device[sizeof(media_device)-1] = '\0';
  51. break;
  52. default:
  53. printf("Usage: %s [-d </dev/mediaX>]\n", argv[0]);
  54. exit(-1);
  55. }
  56. }
  57. if (getuid() != 0) {
  58. printf("Please run the test as root - Exiting.\n");
  59. exit(-1);
  60. }
  61. /* Open Media device and keep it open */
  62. fd = open(media_device, O_RDWR);
  63. if (fd == -1) {
  64. printf("Media Device open errno %s\n", strerror(errno));
  65. exit(-1);
  66. }
  67. ret = ioctl(fd, MEDIA_IOC_DEVICE_INFO, &mdi);
  68. if (ret < 0)
  69. printf("Media Device Info errno %s\n", strerror(errno));
  70. else
  71. printf("Media device model %s driver %s\n",
  72. mdi.model, mdi.driver);
  73. }