media-device.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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 <linux/export.h>
  26. #include <media/media-device.h>
  27. #include <media/media-devnode.h>
  28. #include <media/media-entity.h>
  29. /* -----------------------------------------------------------------------------
  30. * Userspace API
  31. */
  32. static int media_device_open(struct file *filp)
  33. {
  34. return 0;
  35. }
  36. static int media_device_close(struct file *filp)
  37. {
  38. return 0;
  39. }
  40. static int media_device_get_info(struct media_device *dev,
  41. struct media_device_info __user *__info)
  42. {
  43. struct media_device_info info;
  44. memset(&info, 0, sizeof(info));
  45. strlcpy(info.driver, dev->dev->driver->name, sizeof(info.driver));
  46. strlcpy(info.model, dev->model, sizeof(info.model));
  47. strlcpy(info.serial, dev->serial, sizeof(info.serial));
  48. strlcpy(info.bus_info, dev->bus_info, sizeof(info.bus_info));
  49. info.media_version = MEDIA_API_VERSION;
  50. info.hw_revision = dev->hw_revision;
  51. info.driver_version = dev->driver_version;
  52. return copy_to_user(__info, &info, sizeof(*__info));
  53. }
  54. static struct media_entity *find_entity(struct media_device *mdev, u32 id)
  55. {
  56. struct media_entity *entity;
  57. int next = id & MEDIA_ENT_ID_FLAG_NEXT;
  58. id &= ~MEDIA_ENT_ID_FLAG_NEXT;
  59. spin_lock(&mdev->lock);
  60. media_device_for_each_entity(entity, mdev) {
  61. if ((entity->id == id && !next) ||
  62. (entity->id > id && next)) {
  63. spin_unlock(&mdev->lock);
  64. return entity;
  65. }
  66. }
  67. spin_unlock(&mdev->lock);
  68. return NULL;
  69. }
  70. static long media_device_enum_entities(struct media_device *mdev,
  71. struct media_entity_desc __user *uent)
  72. {
  73. struct media_entity *ent;
  74. struct media_entity_desc u_ent;
  75. if (copy_from_user(&u_ent.id, &uent->id, sizeof(u_ent.id)))
  76. return -EFAULT;
  77. ent = find_entity(mdev, u_ent.id);
  78. if (ent == NULL)
  79. return -EINVAL;
  80. u_ent.id = ent->id;
  81. u_ent.name[0] = '\0';
  82. if (ent->name)
  83. strlcpy(u_ent.name, ent->name, sizeof(u_ent.name));
  84. u_ent.type = ent->type;
  85. u_ent.revision = ent->revision;
  86. u_ent.flags = ent->flags;
  87. u_ent.group_id = ent->group_id;
  88. u_ent.pads = ent->num_pads;
  89. u_ent.links = ent->num_links - ent->num_backlinks;
  90. u_ent.v4l.major = ent->v4l.major;
  91. u_ent.v4l.minor = ent->v4l.minor;
  92. if (copy_to_user(uent, &u_ent, sizeof(u_ent)))
  93. return -EFAULT;
  94. return 0;
  95. }
  96. static void media_device_kpad_to_upad(const struct media_pad *kpad,
  97. struct media_pad_desc *upad)
  98. {
  99. upad->entity = kpad->entity->id;
  100. upad->index = kpad->index;
  101. upad->flags = kpad->flags;
  102. }
  103. static long media_device_enum_links(struct media_device *mdev,
  104. struct media_links_enum __user *ulinks)
  105. {
  106. struct media_entity *entity;
  107. struct media_links_enum links;
  108. if (copy_from_user(&links, ulinks, sizeof(links)))
  109. return -EFAULT;
  110. entity = find_entity(mdev, links.entity);
  111. if (entity == NULL)
  112. return -EINVAL;
  113. if (links.pads) {
  114. unsigned int p;
  115. for (p = 0; p < entity->num_pads; p++) {
  116. struct media_pad_desc pad;
  117. media_device_kpad_to_upad(&entity->pads[p], &pad);
  118. if (copy_to_user(&links.pads[p], &pad, sizeof(pad)))
  119. return -EFAULT;
  120. }
  121. }
  122. if (links.links) {
  123. struct media_link_desc __user *ulink;
  124. unsigned int l;
  125. for (l = 0, ulink = links.links; l < entity->num_links; l++) {
  126. struct media_link_desc link;
  127. /* Ignore backlinks. */
  128. if (entity->links[l].source->entity != entity)
  129. continue;
  130. media_device_kpad_to_upad(entity->links[l].source,
  131. &link.source);
  132. media_device_kpad_to_upad(entity->links[l].sink,
  133. &link.sink);
  134. link.flags = entity->links[l].flags;
  135. if (copy_to_user(ulink, &link, sizeof(*ulink)))
  136. return -EFAULT;
  137. ulink++;
  138. }
  139. }
  140. if (copy_to_user(ulinks, &links, sizeof(*ulinks)))
  141. return -EFAULT;
  142. return 0;
  143. }
  144. static long media_device_setup_link(struct media_device *mdev,
  145. struct media_link_desc __user *_ulink)
  146. {
  147. struct media_link *link = NULL;
  148. struct media_link_desc ulink;
  149. struct media_entity *source;
  150. struct media_entity *sink;
  151. int ret;
  152. if (copy_from_user(&ulink, _ulink, sizeof(ulink)))
  153. return -EFAULT;
  154. /* Find the source and sink entities and link.
  155. */
  156. source = find_entity(mdev, ulink.source.entity);
  157. sink = find_entity(mdev, ulink.sink.entity);
  158. if (source == NULL || sink == NULL)
  159. return -EINVAL;
  160. if (ulink.source.index >= source->num_pads ||
  161. ulink.sink.index >= sink->num_pads)
  162. return -EINVAL;
  163. link = media_entity_find_link(&source->pads[ulink.source.index],
  164. &sink->pads[ulink.sink.index]);
  165. if (link == NULL)
  166. return -EINVAL;
  167. /* Setup the link on both entities. */
  168. ret = __media_entity_setup_link(link, ulink.flags);
  169. if (copy_to_user(_ulink, &ulink, sizeof(ulink)))
  170. return -EFAULT;
  171. return ret;
  172. }
  173. static long media_device_ioctl(struct file *filp, unsigned int cmd,
  174. unsigned long arg)
  175. {
  176. struct media_devnode *devnode = media_devnode_data(filp);
  177. struct media_device *dev = to_media_device(devnode);
  178. long ret;
  179. switch (cmd) {
  180. case MEDIA_IOC_DEVICE_INFO:
  181. ret = media_device_get_info(dev,
  182. (struct media_device_info __user *)arg);
  183. break;
  184. case MEDIA_IOC_ENUM_ENTITIES:
  185. ret = media_device_enum_entities(dev,
  186. (struct media_entity_desc __user *)arg);
  187. break;
  188. case MEDIA_IOC_ENUM_LINKS:
  189. mutex_lock(&dev->graph_mutex);
  190. ret = media_device_enum_links(dev,
  191. (struct media_links_enum __user *)arg);
  192. mutex_unlock(&dev->graph_mutex);
  193. break;
  194. case MEDIA_IOC_SETUP_LINK:
  195. mutex_lock(&dev->graph_mutex);
  196. ret = media_device_setup_link(dev,
  197. (struct media_link_desc __user *)arg);
  198. mutex_unlock(&dev->graph_mutex);
  199. break;
  200. default:
  201. ret = -ENOIOCTLCMD;
  202. }
  203. return ret;
  204. }
  205. static const struct media_file_operations media_device_fops = {
  206. .owner = THIS_MODULE,
  207. .open = media_device_open,
  208. .ioctl = media_device_ioctl,
  209. .release = media_device_close,
  210. };
  211. /* -----------------------------------------------------------------------------
  212. * sysfs
  213. */
  214. static ssize_t show_model(struct device *cd,
  215. struct device_attribute *attr, char *buf)
  216. {
  217. struct media_device *mdev = to_media_device(to_media_devnode(cd));
  218. return sprintf(buf, "%.*s\n", (int)sizeof(mdev->model), mdev->model);
  219. }
  220. static DEVICE_ATTR(model, S_IRUGO, show_model, NULL);
  221. /* -----------------------------------------------------------------------------
  222. * Registration/unregistration
  223. */
  224. static void media_device_release(struct media_devnode *mdev)
  225. {
  226. }
  227. /**
  228. * media_device_register - register a media device
  229. * @mdev: The media device
  230. *
  231. * The caller is responsible for initializing the media device before
  232. * registration. The following fields must be set:
  233. *
  234. * - dev must point to the parent device
  235. * - model must be filled with the device model name
  236. */
  237. int __must_check media_device_register(struct media_device *mdev)
  238. {
  239. int ret;
  240. if (WARN_ON(mdev->dev == NULL || mdev->model[0] == 0))
  241. return -EINVAL;
  242. mdev->entity_id = 1;
  243. INIT_LIST_HEAD(&mdev->entities);
  244. spin_lock_init(&mdev->lock);
  245. mutex_init(&mdev->graph_mutex);
  246. /* Register the device node. */
  247. mdev->devnode.fops = &media_device_fops;
  248. mdev->devnode.parent = mdev->dev;
  249. mdev->devnode.release = media_device_release;
  250. ret = media_devnode_register(&mdev->devnode);
  251. if (ret < 0)
  252. return ret;
  253. ret = device_create_file(&mdev->devnode.dev, &dev_attr_model);
  254. if (ret < 0) {
  255. media_devnode_unregister(&mdev->devnode);
  256. return ret;
  257. }
  258. return 0;
  259. }
  260. EXPORT_SYMBOL_GPL(media_device_register);
  261. /**
  262. * media_device_unregister - unregister a media device
  263. * @mdev: The media device
  264. *
  265. */
  266. void media_device_unregister(struct media_device *mdev)
  267. {
  268. struct media_entity *entity;
  269. struct media_entity *next;
  270. list_for_each_entry_safe(entity, next, &mdev->entities, list)
  271. media_device_unregister_entity(entity);
  272. device_remove_file(&mdev->devnode.dev, &dev_attr_model);
  273. media_devnode_unregister(&mdev->devnode);
  274. }
  275. EXPORT_SYMBOL_GPL(media_device_unregister);
  276. /**
  277. * media_device_register_entity - Register an entity with a media device
  278. * @mdev: The media device
  279. * @entity: The entity
  280. */
  281. int __must_check media_device_register_entity(struct media_device *mdev,
  282. struct media_entity *entity)
  283. {
  284. /* Warn if we apparently re-register an entity */
  285. WARN_ON(entity->parent != NULL);
  286. entity->parent = mdev;
  287. spin_lock(&mdev->lock);
  288. if (entity->id == 0)
  289. entity->id = mdev->entity_id++;
  290. else
  291. mdev->entity_id = max(entity->id + 1, mdev->entity_id);
  292. list_add_tail(&entity->list, &mdev->entities);
  293. spin_unlock(&mdev->lock);
  294. return 0;
  295. }
  296. EXPORT_SYMBOL_GPL(media_device_register_entity);
  297. /**
  298. * media_device_unregister_entity - Unregister an entity
  299. * @entity: The entity
  300. *
  301. * If the entity has never been registered this function will return
  302. * immediately.
  303. */
  304. void media_device_unregister_entity(struct media_entity *entity)
  305. {
  306. struct media_device *mdev = entity->parent;
  307. if (mdev == NULL)
  308. return;
  309. spin_lock(&mdev->lock);
  310. list_del(&entity->list);
  311. spin_unlock(&mdev->lock);
  312. entity->parent = NULL;
  313. }
  314. EXPORT_SYMBOL_GPL(media_device_unregister_entity);