v4l2-int-device.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. * drivers/media/video/v4l2-int-device.c
  3. *
  4. * V4L2 internal ioctl interface.
  5. *
  6. * Copyright (C) 2007 Nokia Corporation.
  7. *
  8. * Contact: Sakari Ailus <sakari.ailus@nokia.com>
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License
  12. * version 2 as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  22. * 02110-1301 USA
  23. */
  24. #include <linux/kernel.h>
  25. #include <linux/list.h>
  26. #include <linux/sort.h>
  27. #include <linux/string.h>
  28. #include <media/v4l2-int-device.h>
  29. static DEFINE_MUTEX(mutex);
  30. static LIST_HEAD(int_list);
  31. void v4l2_int_device_try_attach_all(void)
  32. {
  33. struct v4l2_int_device *m, *s;
  34. list_for_each_entry(m, &int_list, head) {
  35. if (m->type != v4l2_int_type_master)
  36. continue;
  37. list_for_each_entry(s, &int_list, head) {
  38. if (s->type != v4l2_int_type_slave)
  39. continue;
  40. /* Slave is connected? */
  41. if (s->u.slave->master)
  42. continue;
  43. /* Slave wants to attach to master? */
  44. if (s->u.slave->attach_to[0] != 0
  45. && strncmp(m->name, s->u.slave->attach_to,
  46. V4L2NAMESIZE))
  47. continue;
  48. if (!try_module_get(m->module))
  49. continue;
  50. s->u.slave->master = m;
  51. if (m->u.master->attach(s)) {
  52. s->u.slave->master = NULL;
  53. module_put(m->module);
  54. continue;
  55. }
  56. }
  57. }
  58. }
  59. EXPORT_SYMBOL_GPL(v4l2_int_device_try_attach_all);
  60. static int ioctl_sort_cmp(const void *a, const void *b)
  61. {
  62. const struct v4l2_int_ioctl_desc *d1 = a, *d2 = b;
  63. if (d1->num > d2->num)
  64. return 1;
  65. if (d1->num < d2->num)
  66. return -1;
  67. return 0;
  68. }
  69. int v4l2_int_device_register(struct v4l2_int_device *d)
  70. {
  71. if (d->type == v4l2_int_type_slave)
  72. sort(d->u.slave->ioctls, d->u.slave->num_ioctls,
  73. sizeof(struct v4l2_int_ioctl_desc),
  74. &ioctl_sort_cmp, NULL);
  75. mutex_lock(&mutex);
  76. list_add(&d->head, &int_list);
  77. v4l2_int_device_try_attach_all();
  78. mutex_unlock(&mutex);
  79. return 0;
  80. }
  81. EXPORT_SYMBOL_GPL(v4l2_int_device_register);
  82. void v4l2_int_device_unregister(struct v4l2_int_device *d)
  83. {
  84. mutex_lock(&mutex);
  85. list_del(&d->head);
  86. if (d->type == v4l2_int_type_slave
  87. && d->u.slave->master != NULL) {
  88. d->u.slave->master->u.master->detach(d);
  89. module_put(d->u.slave->master->module);
  90. d->u.slave->master = NULL;
  91. }
  92. mutex_unlock(&mutex);
  93. }
  94. EXPORT_SYMBOL_GPL(v4l2_int_device_unregister);
  95. /* Adapted from search_extable in extable.c. */
  96. static v4l2_int_ioctl_func *find_ioctl(struct v4l2_int_slave *slave, int cmd,
  97. v4l2_int_ioctl_func *no_such_ioctl)
  98. {
  99. const struct v4l2_int_ioctl_desc *first = slave->ioctls;
  100. const struct v4l2_int_ioctl_desc *last =
  101. first + slave->num_ioctls - 1;
  102. while (first <= last) {
  103. const struct v4l2_int_ioctl_desc *mid;
  104. mid = (last - first) / 2 + first;
  105. if (mid->num < cmd)
  106. first = mid + 1;
  107. else if (mid->num > cmd)
  108. last = mid - 1;
  109. else
  110. return mid->func;
  111. }
  112. return no_such_ioctl;
  113. }
  114. static int no_such_ioctl_0(struct v4l2_int_device *d)
  115. {
  116. return -ENOIOCTLCMD;
  117. }
  118. int v4l2_int_ioctl_0(struct v4l2_int_device *d, int cmd)
  119. {
  120. return ((v4l2_int_ioctl_func_0 *)
  121. find_ioctl(d->u.slave, cmd,
  122. (v4l2_int_ioctl_func *)no_such_ioctl_0))(d);
  123. }
  124. EXPORT_SYMBOL_GPL(v4l2_int_ioctl_0);
  125. static int no_such_ioctl_1(struct v4l2_int_device *d, void *arg)
  126. {
  127. return -ENOIOCTLCMD;
  128. }
  129. int v4l2_int_ioctl_1(struct v4l2_int_device *d, int cmd, void *arg)
  130. {
  131. return ((v4l2_int_ioctl_func_1 *)
  132. find_ioctl(d->u.slave, cmd,
  133. (v4l2_int_ioctl_func *)no_such_ioctl_1))(d, arg);
  134. }
  135. EXPORT_SYMBOL_GPL(v4l2_int_ioctl_1);
  136. MODULE_LICENSE("GPL");