v4l2-device.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /*
  2. V4L2 device support.
  3. Copyright (C) 2008 Hans Verkuil <hverkuil@xs4all.nl>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  15. */
  16. #include <linux/types.h>
  17. #include <linux/ioctl.h>
  18. #include <linux/i2c.h>
  19. #if defined(CONFIG_SPI)
  20. #include <linux/spi/spi.h>
  21. #endif
  22. #include <linux/videodev2.h>
  23. #include <media/v4l2-device.h>
  24. #include <media/v4l2-ctrls.h>
  25. int v4l2_device_register(struct device *dev, struct v4l2_device *v4l2_dev)
  26. {
  27. if (v4l2_dev == NULL)
  28. return -EINVAL;
  29. INIT_LIST_HEAD(&v4l2_dev->subdevs);
  30. spin_lock_init(&v4l2_dev->lock);
  31. mutex_init(&v4l2_dev->ioctl_lock);
  32. v4l2_prio_init(&v4l2_dev->prio);
  33. kref_init(&v4l2_dev->ref);
  34. v4l2_dev->dev = dev;
  35. if (dev == NULL) {
  36. /* If dev == NULL, then name must be filled in by the caller */
  37. WARN_ON(!v4l2_dev->name[0]);
  38. return 0;
  39. }
  40. /* Set name to driver name + device name if it is empty. */
  41. if (!v4l2_dev->name[0])
  42. snprintf(v4l2_dev->name, sizeof(v4l2_dev->name), "%s %s",
  43. dev->driver->name, dev_name(dev));
  44. if (!dev_get_drvdata(dev))
  45. dev_set_drvdata(dev, v4l2_dev);
  46. return 0;
  47. }
  48. EXPORT_SYMBOL_GPL(v4l2_device_register);
  49. static void v4l2_device_release(struct kref *ref)
  50. {
  51. struct v4l2_device *v4l2_dev =
  52. container_of(ref, struct v4l2_device, ref);
  53. if (v4l2_dev->release)
  54. v4l2_dev->release(v4l2_dev);
  55. }
  56. int v4l2_device_put(struct v4l2_device *v4l2_dev)
  57. {
  58. return kref_put(&v4l2_dev->ref, v4l2_device_release);
  59. }
  60. EXPORT_SYMBOL_GPL(v4l2_device_put);
  61. int v4l2_device_set_name(struct v4l2_device *v4l2_dev, const char *basename,
  62. atomic_t *instance)
  63. {
  64. int num = atomic_inc_return(instance) - 1;
  65. int len = strlen(basename);
  66. if (basename[len - 1] >= '0' && basename[len - 1] <= '9')
  67. snprintf(v4l2_dev->name, sizeof(v4l2_dev->name),
  68. "%s-%d", basename, num);
  69. else
  70. snprintf(v4l2_dev->name, sizeof(v4l2_dev->name),
  71. "%s%d", basename, num);
  72. return num;
  73. }
  74. EXPORT_SYMBOL_GPL(v4l2_device_set_name);
  75. void v4l2_device_disconnect(struct v4l2_device *v4l2_dev)
  76. {
  77. if (v4l2_dev->dev == NULL)
  78. return;
  79. if (dev_get_drvdata(v4l2_dev->dev) == v4l2_dev)
  80. dev_set_drvdata(v4l2_dev->dev, NULL);
  81. v4l2_dev->dev = NULL;
  82. }
  83. EXPORT_SYMBOL_GPL(v4l2_device_disconnect);
  84. void v4l2_device_unregister(struct v4l2_device *v4l2_dev)
  85. {
  86. struct v4l2_subdev *sd, *next;
  87. if (v4l2_dev == NULL)
  88. return;
  89. v4l2_device_disconnect(v4l2_dev);
  90. /* Unregister subdevs */
  91. list_for_each_entry_safe(sd, next, &v4l2_dev->subdevs, list) {
  92. v4l2_device_unregister_subdev(sd);
  93. #if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
  94. if (sd->flags & V4L2_SUBDEV_FL_IS_I2C) {
  95. struct i2c_client *client = v4l2_get_subdevdata(sd);
  96. /* We need to unregister the i2c client explicitly.
  97. We cannot rely on i2c_del_adapter to always
  98. unregister clients for us, since if the i2c bus
  99. is a platform bus, then it is never deleted. */
  100. if (client)
  101. i2c_unregister_device(client);
  102. continue;
  103. }
  104. #endif
  105. #if defined(CONFIG_SPI)
  106. if (sd->flags & V4L2_SUBDEV_FL_IS_SPI) {
  107. struct spi_device *spi = v4l2_get_subdevdata(sd);
  108. if (spi)
  109. spi_unregister_device(spi);
  110. continue;
  111. }
  112. #endif
  113. }
  114. }
  115. EXPORT_SYMBOL_GPL(v4l2_device_unregister);
  116. int v4l2_device_register_subdev(struct v4l2_device *v4l2_dev,
  117. struct v4l2_subdev *sd)
  118. {
  119. #if defined(CONFIG_MEDIA_CONTROLLER)
  120. struct media_entity *entity = &sd->entity;
  121. #endif
  122. int err;
  123. /* Check for valid input */
  124. if (v4l2_dev == NULL || sd == NULL || !sd->name[0])
  125. return -EINVAL;
  126. /* Warn if we apparently re-register a subdev */
  127. WARN_ON(sd->v4l2_dev != NULL);
  128. if (!try_module_get(sd->owner))
  129. return -ENODEV;
  130. sd->v4l2_dev = v4l2_dev;
  131. if (sd->internal_ops && sd->internal_ops->registered) {
  132. err = sd->internal_ops->registered(sd);
  133. if (err)
  134. return err;
  135. }
  136. /* This just returns 0 if either of the two args is NULL */
  137. err = v4l2_ctrl_add_handler(v4l2_dev->ctrl_handler, sd->ctrl_handler);
  138. if (err) {
  139. if (sd->internal_ops && sd->internal_ops->unregistered)
  140. sd->internal_ops->unregistered(sd);
  141. return err;
  142. }
  143. #if defined(CONFIG_MEDIA_CONTROLLER)
  144. /* Register the entity. */
  145. if (v4l2_dev->mdev) {
  146. err = media_device_register_entity(v4l2_dev->mdev, entity);
  147. if (err < 0) {
  148. if (sd->internal_ops && sd->internal_ops->unregistered)
  149. sd->internal_ops->unregistered(sd);
  150. module_put(sd->owner);
  151. return err;
  152. }
  153. }
  154. #endif
  155. spin_lock(&v4l2_dev->lock);
  156. list_add_tail(&sd->list, &v4l2_dev->subdevs);
  157. spin_unlock(&v4l2_dev->lock);
  158. return 0;
  159. }
  160. EXPORT_SYMBOL_GPL(v4l2_device_register_subdev);
  161. int v4l2_device_register_subdev_nodes(struct v4l2_device *v4l2_dev)
  162. {
  163. struct video_device *vdev;
  164. struct v4l2_subdev *sd;
  165. int err;
  166. /* Register a device node for every subdev marked with the
  167. * V4L2_SUBDEV_FL_HAS_DEVNODE flag.
  168. */
  169. list_for_each_entry(sd, &v4l2_dev->subdevs, list) {
  170. if (!(sd->flags & V4L2_SUBDEV_FL_HAS_DEVNODE))
  171. continue;
  172. vdev = &sd->devnode;
  173. strlcpy(vdev->name, sd->name, sizeof(vdev->name));
  174. vdev->v4l2_dev = v4l2_dev;
  175. vdev->fops = &v4l2_subdev_fops;
  176. vdev->release = video_device_release_empty;
  177. err = __video_register_device(vdev, VFL_TYPE_SUBDEV, -1, 1,
  178. sd->owner);
  179. if (err < 0)
  180. return err;
  181. #if defined(CONFIG_MEDIA_CONTROLLER)
  182. sd->entity.v4l.major = VIDEO_MAJOR;
  183. sd->entity.v4l.minor = vdev->minor;
  184. #endif
  185. }
  186. return 0;
  187. }
  188. EXPORT_SYMBOL_GPL(v4l2_device_register_subdev_nodes);
  189. void v4l2_device_unregister_subdev(struct v4l2_subdev *sd)
  190. {
  191. struct v4l2_device *v4l2_dev;
  192. /* return if it isn't registered */
  193. if (sd == NULL || sd->v4l2_dev == NULL)
  194. return;
  195. v4l2_dev = sd->v4l2_dev;
  196. spin_lock(&v4l2_dev->lock);
  197. list_del(&sd->list);
  198. spin_unlock(&v4l2_dev->lock);
  199. if (sd->internal_ops && sd->internal_ops->unregistered)
  200. sd->internal_ops->unregistered(sd);
  201. sd->v4l2_dev = NULL;
  202. #if defined(CONFIG_MEDIA_CONTROLLER)
  203. if (v4l2_dev->mdev)
  204. media_device_unregister_entity(&sd->entity);
  205. #endif
  206. video_unregister_device(&sd->devnode);
  207. module_put(sd->owner);
  208. }
  209. EXPORT_SYMBOL_GPL(v4l2_device_unregister_subdev);