v4l2-int-device.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 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. static int ioctl_sort_cmp(const void *a, const void *b)
  60. {
  61. const struct v4l2_int_ioctl_desc *d1 = a, *d2 = b;
  62. if (d1->num > d2->num)
  63. return 1;
  64. if (d1->num < d2->num)
  65. return -1;
  66. return 0;
  67. }
  68. int v4l2_int_device_register(struct v4l2_int_device *d)
  69. {
  70. if (d->type == v4l2_int_type_slave)
  71. sort(d->u.slave->ioctls, d->u.slave->num_ioctls,
  72. sizeof(struct v4l2_int_ioctl_desc),
  73. &ioctl_sort_cmp, NULL);
  74. mutex_lock(&mutex);
  75. list_add(&d->head, &int_list);
  76. v4l2_int_device_try_attach_all();
  77. mutex_unlock(&mutex);
  78. return 0;
  79. }
  80. EXPORT_SYMBOL_GPL(v4l2_int_device_register);
  81. void v4l2_int_device_unregister(struct v4l2_int_device *d)
  82. {
  83. mutex_lock(&mutex);
  84. list_del(&d->head);
  85. if (d->type == v4l2_int_type_slave
  86. && d->u.slave->master != NULL) {
  87. d->u.slave->master->u.master->detach(d);
  88. module_put(d->u.slave->master->module);
  89. d->u.slave->master = NULL;
  90. }
  91. mutex_unlock(&mutex);
  92. }
  93. EXPORT_SYMBOL_GPL(v4l2_int_device_unregister);
  94. /* Adapted from search_extable in extable.c. */
  95. static v4l2_int_ioctl_func *find_ioctl(struct v4l2_int_slave *slave, int cmd,
  96. v4l2_int_ioctl_func *no_such_ioctl)
  97. {
  98. const struct v4l2_int_ioctl_desc *first = slave->ioctls;
  99. const struct v4l2_int_ioctl_desc *last =
  100. first + slave->num_ioctls - 1;
  101. while (first <= last) {
  102. const struct v4l2_int_ioctl_desc *mid;
  103. mid = (last - first) / 2 + first;
  104. if (mid->num < cmd)
  105. first = mid + 1;
  106. else if (mid->num > cmd)
  107. last = mid - 1;
  108. else
  109. return mid->func;
  110. }
  111. return no_such_ioctl;
  112. }
  113. static int no_such_ioctl_0(struct v4l2_int_device *d)
  114. {
  115. return -ENOIOCTLCMD;
  116. }
  117. int v4l2_int_ioctl_0(struct v4l2_int_device *d, int cmd)
  118. {
  119. return ((v4l2_int_ioctl_func_0 *)
  120. find_ioctl(d->u.slave, cmd,
  121. (v4l2_int_ioctl_func *)no_such_ioctl_0))(d);
  122. }
  123. static int no_such_ioctl_1(struct v4l2_int_device *d, void *arg)
  124. {
  125. return -ENOIOCTLCMD;
  126. }
  127. int v4l2_int_ioctl_1(struct v4l2_int_device *d, int cmd, void *arg)
  128. {
  129. return ((v4l2_int_ioctl_func_1 *)
  130. find_ioctl(d->u.slave, cmd,
  131. (v4l2_int_ioctl_func *)no_such_ioctl_1))(d, arg);
  132. }
  133. MODULE_LICENSE("GPL");