media-device.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*
  2. * Media device
  3. *
  4. * Copyright (C) 2010 Nokia Corporation
  5. *
  6. * Contacts: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
  7. * Sakari Ailus <sakari.ailus@iki.fi>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. */
  22. #include <linux/types.h>
  23. #include <linux/ioctl.h>
  24. #include <linux/media.h>
  25. #include <media/media-device.h>
  26. #include <media/media-devnode.h>
  27. #include <media/media-entity.h>
  28. /* -----------------------------------------------------------------------------
  29. * Userspace API
  30. */
  31. static int media_device_open(struct file *filp)
  32. {
  33. return 0;
  34. }
  35. static int media_device_close(struct file *filp)
  36. {
  37. return 0;
  38. }
  39. static int media_device_get_info(struct media_device *dev,
  40. struct media_device_info __user *__info)
  41. {
  42. struct media_device_info info;
  43. memset(&info, 0, sizeof(info));
  44. strlcpy(info.driver, dev->dev->driver->name, sizeof(info.driver));
  45. strlcpy(info.model, dev->model, sizeof(info.model));
  46. strlcpy(info.serial, dev->serial, sizeof(info.serial));
  47. strlcpy(info.bus_info, dev->bus_info, sizeof(info.bus_info));
  48. info.media_version = MEDIA_API_VERSION;
  49. info.hw_revision = dev->hw_revision;
  50. info.driver_version = dev->driver_version;
  51. return copy_to_user(__info, &info, sizeof(*__info));
  52. }
  53. static long media_device_ioctl(struct file *filp, unsigned int cmd,
  54. unsigned long arg)
  55. {
  56. struct media_devnode *devnode = media_devnode_data(filp);
  57. struct media_device *dev = to_media_device(devnode);
  58. long ret;
  59. switch (cmd) {
  60. case MEDIA_IOC_DEVICE_INFO:
  61. ret = media_device_get_info(dev,
  62. (struct media_device_info __user *)arg);
  63. break;
  64. default:
  65. ret = -ENOIOCTLCMD;
  66. }
  67. return ret;
  68. }
  69. static const struct media_file_operations media_device_fops = {
  70. .owner = THIS_MODULE,
  71. .open = media_device_open,
  72. .ioctl = media_device_ioctl,
  73. .release = media_device_close,
  74. };
  75. /* -----------------------------------------------------------------------------
  76. * sysfs
  77. */
  78. static ssize_t show_model(struct device *cd,
  79. struct device_attribute *attr, char *buf)
  80. {
  81. struct media_device *mdev = to_media_device(to_media_devnode(cd));
  82. return sprintf(buf, "%.*s\n", (int)sizeof(mdev->model), mdev->model);
  83. }
  84. static DEVICE_ATTR(model, S_IRUGO, show_model, NULL);
  85. /* -----------------------------------------------------------------------------
  86. * Registration/unregistration
  87. */
  88. static void media_device_release(struct media_devnode *mdev)
  89. {
  90. }
  91. /**
  92. * media_device_register - register a media device
  93. * @mdev: The media device
  94. *
  95. * The caller is responsible for initializing the media device before
  96. * registration. The following fields must be set:
  97. *
  98. * - dev must point to the parent device
  99. * - model must be filled with the device model name
  100. */
  101. int __must_check media_device_register(struct media_device *mdev)
  102. {
  103. int ret;
  104. if (WARN_ON(mdev->dev == NULL || mdev->model[0] == 0))
  105. return -EINVAL;
  106. mdev->entity_id = 1;
  107. INIT_LIST_HEAD(&mdev->entities);
  108. spin_lock_init(&mdev->lock);
  109. mutex_init(&mdev->graph_mutex);
  110. /* Register the device node. */
  111. mdev->devnode.fops = &media_device_fops;
  112. mdev->devnode.parent = mdev->dev;
  113. mdev->devnode.release = media_device_release;
  114. ret = media_devnode_register(&mdev->devnode);
  115. if (ret < 0)
  116. return ret;
  117. ret = device_create_file(&mdev->devnode.dev, &dev_attr_model);
  118. if (ret < 0) {
  119. media_devnode_unregister(&mdev->devnode);
  120. return ret;
  121. }
  122. return 0;
  123. }
  124. EXPORT_SYMBOL_GPL(media_device_register);
  125. /**
  126. * media_device_unregister - unregister a media device
  127. * @mdev: The media device
  128. *
  129. */
  130. void media_device_unregister(struct media_device *mdev)
  131. {
  132. struct media_entity *entity;
  133. struct media_entity *next;
  134. list_for_each_entry_safe(entity, next, &mdev->entities, list)
  135. media_device_unregister_entity(entity);
  136. device_remove_file(&mdev->devnode.dev, &dev_attr_model);
  137. media_devnode_unregister(&mdev->devnode);
  138. }
  139. EXPORT_SYMBOL_GPL(media_device_unregister);
  140. /**
  141. * media_device_register_entity - Register an entity with a media device
  142. * @mdev: The media device
  143. * @entity: The entity
  144. */
  145. int __must_check media_device_register_entity(struct media_device *mdev,
  146. struct media_entity *entity)
  147. {
  148. /* Warn if we apparently re-register an entity */
  149. WARN_ON(entity->parent != NULL);
  150. entity->parent = mdev;
  151. spin_lock(&mdev->lock);
  152. if (entity->id == 0)
  153. entity->id = mdev->entity_id++;
  154. else
  155. mdev->entity_id = max(entity->id + 1, mdev->entity_id);
  156. list_add_tail(&entity->list, &mdev->entities);
  157. spin_unlock(&mdev->lock);
  158. return 0;
  159. }
  160. EXPORT_SYMBOL_GPL(media_device_register_entity);
  161. /**
  162. * media_device_unregister_entity - Unregister an entity
  163. * @entity: The entity
  164. *
  165. * If the entity has never been registered this function will return
  166. * immediately.
  167. */
  168. void media_device_unregister_entity(struct media_entity *entity)
  169. {
  170. struct media_device *mdev = entity->parent;
  171. if (mdev == NULL)
  172. return;
  173. spin_lock(&mdev->lock);
  174. list_del(&entity->list);
  175. spin_unlock(&mdev->lock);
  176. entity->parent = NULL;
  177. }
  178. EXPORT_SYMBOL_GPL(media_device_unregister_entity);