v4l2-device.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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/module.h>
  19. #include <linux/i2c.h>
  20. #include <linux/slab.h>
  21. #if defined(CONFIG_SPI)
  22. #include <linux/spi/spi.h>
  23. #endif
  24. #include <linux/videodev2.h>
  25. #include <media/v4l2-device.h>
  26. #include <media/v4l2-ctrls.h>
  27. int v4l2_device_register(struct device *dev, struct v4l2_device *v4l2_dev)
  28. {
  29. if (v4l2_dev == NULL)
  30. return -EINVAL;
  31. INIT_LIST_HEAD(&v4l2_dev->subdevs);
  32. spin_lock_init(&v4l2_dev->lock);
  33. v4l2_prio_init(&v4l2_dev->prio);
  34. kref_init(&v4l2_dev->ref);
  35. get_device(dev);
  36. v4l2_dev->dev = dev;
  37. if (dev == NULL) {
  38. /* If dev == NULL, then name must be filled in by the caller */
  39. if (WARN_ON(!v4l2_dev->name[0]))
  40. return -EINVAL;
  41. return 0;
  42. }
  43. /* Set name to driver name + device name if it is empty. */
  44. if (!v4l2_dev->name[0])
  45. snprintf(v4l2_dev->name, sizeof(v4l2_dev->name), "%s %s",
  46. dev->driver->name, dev_name(dev));
  47. if (!dev_get_drvdata(dev))
  48. dev_set_drvdata(dev, v4l2_dev);
  49. return 0;
  50. }
  51. EXPORT_SYMBOL_GPL(v4l2_device_register);
  52. static void v4l2_device_release(struct kref *ref)
  53. {
  54. struct v4l2_device *v4l2_dev =
  55. container_of(ref, struct v4l2_device, ref);
  56. if (v4l2_dev->release)
  57. v4l2_dev->release(v4l2_dev);
  58. }
  59. int v4l2_device_put(struct v4l2_device *v4l2_dev)
  60. {
  61. return kref_put(&v4l2_dev->ref, v4l2_device_release);
  62. }
  63. EXPORT_SYMBOL_GPL(v4l2_device_put);
  64. int v4l2_device_set_name(struct v4l2_device *v4l2_dev, const char *basename,
  65. atomic_t *instance)
  66. {
  67. int num = atomic_inc_return(instance) - 1;
  68. int len = strlen(basename);
  69. if (basename[len - 1] >= '0' && basename[len - 1] <= '9')
  70. snprintf(v4l2_dev->name, sizeof(v4l2_dev->name),
  71. "%s-%d", basename, num);
  72. else
  73. snprintf(v4l2_dev->name, sizeof(v4l2_dev->name),
  74. "%s%d", basename, num);
  75. return num;
  76. }
  77. EXPORT_SYMBOL_GPL(v4l2_device_set_name);
  78. void v4l2_device_disconnect(struct v4l2_device *v4l2_dev)
  79. {
  80. if (v4l2_dev->dev == NULL)
  81. return;
  82. if (dev_get_drvdata(v4l2_dev->dev) == v4l2_dev)
  83. dev_set_drvdata(v4l2_dev->dev, NULL);
  84. put_device(v4l2_dev->dev);
  85. v4l2_dev->dev = NULL;
  86. }
  87. EXPORT_SYMBOL_GPL(v4l2_device_disconnect);
  88. void v4l2_device_unregister(struct v4l2_device *v4l2_dev)
  89. {
  90. struct v4l2_subdev *sd, *next;
  91. /* Just return if v4l2_dev is NULL or if it was already
  92. * unregistered before. */
  93. if (v4l2_dev == NULL || !v4l2_dev->name[0])
  94. return;
  95. v4l2_device_disconnect(v4l2_dev);
  96. /* Unregister subdevs */
  97. list_for_each_entry_safe(sd, next, &v4l2_dev->subdevs, list) {
  98. v4l2_device_unregister_subdev(sd);
  99. #if IS_ENABLED(CONFIG_I2C)
  100. if (sd->flags & V4L2_SUBDEV_FL_IS_I2C) {
  101. struct i2c_client *client = v4l2_get_subdevdata(sd);
  102. /*
  103. * We need to unregister the i2c client
  104. * explicitly. We cannot rely on
  105. * i2c_del_adapter to always unregister
  106. * clients for us, since if the i2c bus is a
  107. * platform bus, then it is never deleted.
  108. *
  109. * Device tree or ACPI based devices must not
  110. * be unregistered as they have not been
  111. * registered by us, and would not be
  112. * re-created by just probing the V4L2 driver.
  113. */
  114. if (client &&
  115. !client->dev.of_node && !client->dev.fwnode)
  116. i2c_unregister_device(client);
  117. continue;
  118. }
  119. #endif
  120. #if defined(CONFIG_SPI)
  121. if (sd->flags & V4L2_SUBDEV_FL_IS_SPI) {
  122. struct spi_device *spi = v4l2_get_subdevdata(sd);
  123. if (spi && !spi->dev.of_node && !spi->dev.fwnode)
  124. spi_unregister_device(spi);
  125. continue;
  126. }
  127. #endif
  128. }
  129. /* Mark as unregistered, thus preventing duplicate unregistrations */
  130. v4l2_dev->name[0] = '\0';
  131. }
  132. EXPORT_SYMBOL_GPL(v4l2_device_unregister);
  133. int v4l2_device_register_subdev(struct v4l2_device *v4l2_dev,
  134. struct v4l2_subdev *sd)
  135. {
  136. #if defined(CONFIG_MEDIA_CONTROLLER)
  137. struct media_entity *entity = &sd->entity;
  138. #endif
  139. int err;
  140. /* Check for valid input */
  141. if (!v4l2_dev || !sd || sd->v4l2_dev || !sd->name[0])
  142. return -EINVAL;
  143. /*
  144. * The reason to acquire the module here is to avoid unloading
  145. * a module of sub-device which is registered to a media
  146. * device. To make it possible to unload modules for media
  147. * devices that also register sub-devices, do not
  148. * try_module_get() such sub-device owners.
  149. */
  150. sd->owner_v4l2_dev = v4l2_dev->dev && v4l2_dev->dev->driver &&
  151. sd->owner == v4l2_dev->dev->driver->owner;
  152. if (!sd->owner_v4l2_dev && !try_module_get(sd->owner))
  153. return -ENODEV;
  154. sd->v4l2_dev = v4l2_dev;
  155. /* This just returns 0 if either of the two args is NULL */
  156. err = v4l2_ctrl_add_handler(v4l2_dev->ctrl_handler, sd->ctrl_handler, NULL);
  157. if (err)
  158. goto error_module;
  159. #if defined(CONFIG_MEDIA_CONTROLLER)
  160. /* Register the entity. */
  161. if (v4l2_dev->mdev) {
  162. err = media_device_register_entity(v4l2_dev->mdev, entity);
  163. if (err < 0)
  164. goto error_module;
  165. }
  166. #endif
  167. if (sd->internal_ops && sd->internal_ops->registered) {
  168. err = sd->internal_ops->registered(sd);
  169. if (err)
  170. goto error_unregister;
  171. }
  172. spin_lock(&v4l2_dev->lock);
  173. list_add_tail(&sd->list, &v4l2_dev->subdevs);
  174. spin_unlock(&v4l2_dev->lock);
  175. return 0;
  176. error_unregister:
  177. #if defined(CONFIG_MEDIA_CONTROLLER)
  178. media_device_unregister_entity(entity);
  179. #endif
  180. error_module:
  181. if (!sd->owner_v4l2_dev)
  182. module_put(sd->owner);
  183. sd->v4l2_dev = NULL;
  184. return err;
  185. }
  186. EXPORT_SYMBOL_GPL(v4l2_device_register_subdev);
  187. static void v4l2_device_release_subdev_node(struct video_device *vdev)
  188. {
  189. struct v4l2_subdev *sd = video_get_drvdata(vdev);
  190. sd->devnode = NULL;
  191. kfree(vdev);
  192. }
  193. int v4l2_device_register_subdev_nodes(struct v4l2_device *v4l2_dev)
  194. {
  195. struct video_device *vdev;
  196. struct v4l2_subdev *sd;
  197. int err;
  198. /* Register a device node for every subdev marked with the
  199. * V4L2_SUBDEV_FL_HAS_DEVNODE flag.
  200. */
  201. list_for_each_entry(sd, &v4l2_dev->subdevs, list) {
  202. if (!(sd->flags & V4L2_SUBDEV_FL_HAS_DEVNODE))
  203. continue;
  204. vdev = kzalloc(sizeof(*vdev), GFP_KERNEL);
  205. if (!vdev) {
  206. err = -ENOMEM;
  207. goto clean_up;
  208. }
  209. video_set_drvdata(vdev, sd);
  210. strlcpy(vdev->name, sd->name, sizeof(vdev->name));
  211. vdev->v4l2_dev = v4l2_dev;
  212. vdev->fops = &v4l2_subdev_fops;
  213. vdev->release = v4l2_device_release_subdev_node;
  214. vdev->ctrl_handler = sd->ctrl_handler;
  215. err = __video_register_device(vdev, VFL_TYPE_SUBDEV, -1, 1,
  216. sd->owner);
  217. if (err < 0) {
  218. kfree(vdev);
  219. goto clean_up;
  220. }
  221. #if defined(CONFIG_MEDIA_CONTROLLER)
  222. sd->entity.info.dev.major = VIDEO_MAJOR;
  223. sd->entity.info.dev.minor = vdev->minor;
  224. /* Interface is created by __video_register_device() */
  225. if (vdev->v4l2_dev->mdev) {
  226. struct media_link *link;
  227. link = media_create_intf_link(&sd->entity,
  228. &vdev->intf_devnode->intf,
  229. MEDIA_LNK_FL_ENABLED);
  230. if (!link) {
  231. err = -ENOMEM;
  232. goto clean_up;
  233. }
  234. }
  235. #endif
  236. sd->devnode = vdev;
  237. }
  238. return 0;
  239. clean_up:
  240. list_for_each_entry(sd, &v4l2_dev->subdevs, list) {
  241. if (!sd->devnode)
  242. break;
  243. video_unregister_device(sd->devnode);
  244. }
  245. return err;
  246. }
  247. EXPORT_SYMBOL_GPL(v4l2_device_register_subdev_nodes);
  248. void v4l2_device_unregister_subdev(struct v4l2_subdev *sd)
  249. {
  250. struct v4l2_device *v4l2_dev;
  251. /* return if it isn't registered */
  252. if (sd == NULL || sd->v4l2_dev == NULL)
  253. return;
  254. v4l2_dev = sd->v4l2_dev;
  255. spin_lock(&v4l2_dev->lock);
  256. list_del(&sd->list);
  257. spin_unlock(&v4l2_dev->lock);
  258. if (sd->internal_ops && sd->internal_ops->unregistered)
  259. sd->internal_ops->unregistered(sd);
  260. sd->v4l2_dev = NULL;
  261. #if defined(CONFIG_MEDIA_CONTROLLER)
  262. if (v4l2_dev->mdev) {
  263. /*
  264. * No need to explicitly remove links, as both pads and
  265. * links are removed by the function below, in the right order
  266. */
  267. media_device_unregister_entity(&sd->entity);
  268. }
  269. #endif
  270. video_unregister_device(sd->devnode);
  271. if (!sd->owner_v4l2_dev)
  272. module_put(sd->owner);
  273. }
  274. EXPORT_SYMBOL_GPL(v4l2_device_unregister_subdev);