v4l2-int-device.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 <linux/module.h>
  29. #include <media/v4l2-int-device.h>
  30. static DEFINE_MUTEX(mutex);
  31. static LIST_HEAD(int_list);
  32. void v4l2_int_device_try_attach_all(void)
  33. {
  34. struct v4l2_int_device *m, *s;
  35. list_for_each_entry(m, &int_list, head) {
  36. if (m->type != v4l2_int_type_master)
  37. continue;
  38. list_for_each_entry(s, &int_list, head) {
  39. if (s->type != v4l2_int_type_slave)
  40. continue;
  41. /* Slave is connected? */
  42. if (s->u.slave->master)
  43. continue;
  44. /* Slave wants to attach to master? */
  45. if (s->u.slave->attach_to[0] != 0
  46. && strncmp(m->name, s->u.slave->attach_to,
  47. V4L2NAMESIZE))
  48. continue;
  49. if (!try_module_get(m->module))
  50. continue;
  51. s->u.slave->master = m;
  52. if (m->u.master->attach(s)) {
  53. s->u.slave->master = NULL;
  54. module_put(m->module);
  55. continue;
  56. }
  57. }
  58. }
  59. }
  60. EXPORT_SYMBOL_GPL(v4l2_int_device_try_attach_all);
  61. static int ioctl_sort_cmp(const void *a, const void *b)
  62. {
  63. const struct v4l2_int_ioctl_desc *d1 = a, *d2 = b;
  64. if (d1->num > d2->num)
  65. return 1;
  66. if (d1->num < d2->num)
  67. return -1;
  68. return 0;
  69. }
  70. int v4l2_int_device_register(struct v4l2_int_device *d)
  71. {
  72. if (d->type == v4l2_int_type_slave)
  73. sort(d->u.slave->ioctls, d->u.slave->num_ioctls,
  74. sizeof(struct v4l2_int_ioctl_desc),
  75. &ioctl_sort_cmp, NULL);
  76. mutex_lock(&mutex);
  77. list_add(&d->head, &int_list);
  78. v4l2_int_device_try_attach_all();
  79. mutex_unlock(&mutex);
  80. return 0;
  81. }
  82. EXPORT_SYMBOL_GPL(v4l2_int_device_register);
  83. void v4l2_int_device_unregister(struct v4l2_int_device *d)
  84. {
  85. mutex_lock(&mutex);
  86. list_del(&d->head);
  87. if (d->type == v4l2_int_type_slave
  88. && d->u.slave->master != NULL) {
  89. d->u.slave->master->u.master->detach(d);
  90. module_put(d->u.slave->master->module);
  91. d->u.slave->master = NULL;
  92. }
  93. mutex_unlock(&mutex);
  94. }
  95. EXPORT_SYMBOL_GPL(v4l2_int_device_unregister);
  96. /* Adapted from search_extable in extable.c. */
  97. static v4l2_int_ioctl_func *find_ioctl(struct v4l2_int_slave *slave, int cmd,
  98. v4l2_int_ioctl_func *no_such_ioctl)
  99. {
  100. const struct v4l2_int_ioctl_desc *first = slave->ioctls;
  101. const struct v4l2_int_ioctl_desc *last =
  102. first + slave->num_ioctls - 1;
  103. while (first <= last) {
  104. const struct v4l2_int_ioctl_desc *mid;
  105. mid = (last - first) / 2 + first;
  106. if (mid->num < cmd)
  107. first = mid + 1;
  108. else if (mid->num > cmd)
  109. last = mid - 1;
  110. else
  111. return mid->func;
  112. }
  113. return no_such_ioctl;
  114. }
  115. static int no_such_ioctl_0(struct v4l2_int_device *d)
  116. {
  117. return -ENOIOCTLCMD;
  118. }
  119. int v4l2_int_ioctl_0(struct v4l2_int_device *d, int cmd)
  120. {
  121. return ((v4l2_int_ioctl_func_0 *)
  122. find_ioctl(d->u.slave, cmd,
  123. (v4l2_int_ioctl_func *)no_such_ioctl_0))(d);
  124. }
  125. EXPORT_SYMBOL_GPL(v4l2_int_ioctl_0);
  126. static int no_such_ioctl_1(struct v4l2_int_device *d, void *arg)
  127. {
  128. return -ENOIOCTLCMD;
  129. }
  130. int v4l2_int_ioctl_1(struct v4l2_int_device *d, int cmd, void *arg)
  131. {
  132. return ((v4l2_int_ioctl_func_1 *)
  133. find_ioctl(d->u.slave, cmd,
  134. (v4l2_int_ioctl_func *)no_such_ioctl_1))(d, arg);
  135. }
  136. EXPORT_SYMBOL_GPL(v4l2_int_ioctl_1);
  137. MODULE_LICENSE("GPL");