v4l2-device.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. get_device(dev);
  35. v4l2_dev->dev = dev;
  36. if (dev == NULL) {
  37. /* If dev == NULL, then name must be filled in by the caller */
  38. WARN_ON(!v4l2_dev->name[0]);
  39. return 0;
  40. }
  41. /* Set name to driver name + device name if it is empty. */
  42. if (!v4l2_dev->name[0])
  43. snprintf(v4l2_dev->name, sizeof(v4l2_dev->name), "%s %s",
  44. dev->driver->name, dev_name(dev));
  45. if (!dev_get_drvdata(dev))
  46. dev_set_drvdata(dev, v4l2_dev);
  47. return 0;
  48. }
  49. EXPORT_SYMBOL_GPL(v4l2_device_register);
  50. static void v4l2_device_release(struct kref *ref)
  51. {
  52. struct v4l2_device *v4l2_dev =
  53. container_of(ref, struct v4l2_device, ref);
  54. if (v4l2_dev->release)
  55. v4l2_dev->release(v4l2_dev);
  56. }
  57. int v4l2_device_put(struct v4l2_device *v4l2_dev)
  58. {
  59. return kref_put(&v4l2_dev->ref, v4l2_device_release);
  60. }
  61. EXPORT_SYMBOL_GPL(v4l2_device_put);
  62. int v4l2_device_set_name(struct v4l2_device *v4l2_dev, const char *basename,
  63. atomic_t *instance)
  64. {
  65. int num = atomic_inc_return(instance) - 1;
  66. int len = strlen(basename);
  67. if (basename[len - 1] >= '0' && basename[len - 1] <= '9')
  68. snprintf(v4l2_dev->name, sizeof(v4l2_dev->name),
  69. "%s-%d", basename, num);
  70. else
  71. snprintf(v4l2_dev->name, sizeof(v4l2_dev->name),
  72. "%s%d", basename, num);
  73. return num;
  74. }
  75. EXPORT_SYMBOL_GPL(v4l2_device_set_name);
  76. void v4l2_device_disconnect(struct v4l2_device *v4l2_dev)
  77. {
  78. if (v4l2_dev->dev == NULL)
  79. return;
  80. if (dev_get_drvdata(v4l2_dev->dev) == v4l2_dev)
  81. dev_set_drvdata(v4l2_dev->dev, NULL);
  82. put_device(v4l2_dev->dev);
  83. v4l2_dev->dev = NULL;
  84. }
  85. EXPORT_SYMBOL_GPL(v4l2_device_disconnect);
  86. void v4l2_device_unregister(struct v4l2_device *v4l2_dev)
  87. {
  88. struct v4l2_subdev *sd, *next;
  89. if (v4l2_dev == NULL)
  90. return;
  91. v4l2_device_disconnect(v4l2_dev);
  92. /* Unregister subdevs */
  93. list_for_each_entry_safe(sd, next, &v4l2_dev->subdevs, list) {
  94. v4l2_device_unregister_subdev(sd);
  95. #if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
  96. if (sd->flags & V4L2_SUBDEV_FL_IS_I2C) {
  97. struct i2c_client *client = v4l2_get_subdevdata(sd);
  98. /* We need to unregister the i2c client explicitly.
  99. We cannot rely on i2c_del_adapter to always
  100. unregister clients for us, since if the i2c bus
  101. is a platform bus, then it is never deleted. */
  102. if (client)
  103. i2c_unregister_device(client);
  104. continue;
  105. }
  106. #endif
  107. #if defined(CONFIG_SPI)
  108. if (sd->flags & V4L2_SUBDEV_FL_IS_SPI) {
  109. struct spi_device *spi = v4l2_get_subdevdata(sd);
  110. if (spi)
  111. spi_unregister_device(spi);
  112. continue;
  113. }
  114. #endif
  115. }
  116. }
  117. EXPORT_SYMBOL_GPL(v4l2_device_unregister);
  118. int v4l2_device_register_subdev(struct v4l2_device *v4l2_dev,
  119. struct v4l2_subdev *sd)
  120. {
  121. #if defined(CONFIG_MEDIA_CONTROLLER)
  122. struct media_entity *entity = &sd->entity;
  123. #endif
  124. int err;
  125. /* Check for valid input */
  126. if (v4l2_dev == NULL || sd == NULL || !sd->name[0])
  127. return -EINVAL;
  128. /* Warn if we apparently re-register a subdev */
  129. WARN_ON(sd->v4l2_dev != NULL);
  130. if (!try_module_get(sd->owner))
  131. return -ENODEV;
  132. sd->v4l2_dev = v4l2_dev;
  133. if (sd->internal_ops && sd->internal_ops->registered) {
  134. err = sd->internal_ops->registered(sd);
  135. if (err) {
  136. module_put(sd->owner);
  137. return err;
  138. }
  139. }
  140. /* This just returns 0 if either of the two args is NULL */
  141. err = v4l2_ctrl_add_handler(v4l2_dev->ctrl_handler, sd->ctrl_handler);
  142. if (err) {
  143. if (sd->internal_ops && sd->internal_ops->unregistered)
  144. sd->internal_ops->unregistered(sd);
  145. module_put(sd->owner);
  146. return err;
  147. }
  148. #if defined(CONFIG_MEDIA_CONTROLLER)
  149. /* Register the entity. */
  150. if (v4l2_dev->mdev) {
  151. err = media_device_register_entity(v4l2_dev->mdev, entity);
  152. if (err < 0) {
  153. if (sd->internal_ops && sd->internal_ops->unregistered)
  154. sd->internal_ops->unregistered(sd);
  155. module_put(sd->owner);
  156. return err;
  157. }
  158. }
  159. #endif
  160. spin_lock(&v4l2_dev->lock);
  161. list_add_tail(&sd->list, &v4l2_dev->subdevs);
  162. spin_unlock(&v4l2_dev->lock);
  163. return 0;
  164. }
  165. EXPORT_SYMBOL_GPL(v4l2_device_register_subdev);
  166. int v4l2_device_register_subdev_nodes(struct v4l2_device *v4l2_dev)
  167. {
  168. struct video_device *vdev;
  169. struct v4l2_subdev *sd;
  170. int err;
  171. /* Register a device node for every subdev marked with the
  172. * V4L2_SUBDEV_FL_HAS_DEVNODE flag.
  173. */
  174. list_for_each_entry(sd, &v4l2_dev->subdevs, list) {
  175. if (!(sd->flags & V4L2_SUBDEV_FL_HAS_DEVNODE))
  176. continue;
  177. vdev = &sd->devnode;
  178. strlcpy(vdev->name, sd->name, sizeof(vdev->name));
  179. vdev->v4l2_dev = v4l2_dev;
  180. vdev->fops = &v4l2_subdev_fops;
  181. vdev->release = video_device_release_empty;
  182. vdev->ctrl_handler = sd->ctrl_handler;
  183. err = __video_register_device(vdev, VFL_TYPE_SUBDEV, -1, 1,
  184. sd->owner);
  185. if (err < 0)
  186. return err;
  187. #if defined(CONFIG_MEDIA_CONTROLLER)
  188. sd->entity.v4l.major = VIDEO_MAJOR;
  189. sd->entity.v4l.minor = vdev->minor;
  190. #endif
  191. }
  192. return 0;
  193. }
  194. EXPORT_SYMBOL_GPL(v4l2_device_register_subdev_nodes);
  195. void v4l2_device_unregister_subdev(struct v4l2_subdev *sd)
  196. {
  197. struct v4l2_device *v4l2_dev;
  198. /* return if it isn't registered */
  199. if (sd == NULL || sd->v4l2_dev == NULL)
  200. return;
  201. v4l2_dev = sd->v4l2_dev;
  202. spin_lock(&v4l2_dev->lock);
  203. list_del(&sd->list);
  204. spin_unlock(&v4l2_dev->lock);
  205. if (sd->internal_ops && sd->internal_ops->unregistered)
  206. sd->internal_ops->unregistered(sd);
  207. sd->v4l2_dev = NULL;
  208. #if defined(CONFIG_MEDIA_CONTROLLER)
  209. if (v4l2_dev->mdev)
  210. media_device_unregister_entity(&sd->entity);
  211. #endif
  212. video_unregister_device(&sd->devnode);
  213. module_put(sd->owner);
  214. }
  215. EXPORT_SYMBOL_GPL(v4l2_device_unregister_subdev);