v4l2-int-device.c 3.9 KB

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