media-device.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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. memset(&u_ent, 0, sizeof(u_ent));
  76. if (copy_from_user(&u_ent.id, &uent->id, sizeof(u_ent.id)))
  77. return -EFAULT;
  78. ent = find_entity(mdev, u_ent.id);
  79. if (ent == NULL)
  80. return -EINVAL;
  81. u_ent.id = ent->id;
  82. u_ent.name[0] = '\0';
  83. if (ent->name)
  84. strlcpy(u_ent.name, ent->name, sizeof(u_ent.name));
  85. u_ent.type = ent->type;
  86. u_ent.revision = ent->revision;
  87. u_ent.flags = ent->flags;
  88. u_ent.group_id = ent->group_id;
  89. u_ent.pads = ent->num_pads;
  90. u_ent.links = ent->num_links - ent->num_backlinks;
  91. memcpy(&u_ent.raw, &ent->info, sizeof(ent->info));
  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. memset(&pad, 0, sizeof(pad));
  118. media_device_kpad_to_upad(&entity->pads[p], &pad);
  119. if (copy_to_user(&links.pads[p], &pad, sizeof(pad)))
  120. return -EFAULT;
  121. }
  122. }
  123. if (links.links) {
  124. struct media_link_desc __user *ulink;
  125. unsigned int l;
  126. for (l = 0, ulink = links.links; l < entity->num_links; l++) {
  127. struct media_link_desc link;
  128. /* Ignore backlinks. */
  129. if (entity->links[l].source->entity != entity)
  130. continue;
  131. memset(&link, 0, sizeof(link));
  132. media_device_kpad_to_upad(entity->links[l].source,
  133. &link.source);
  134. media_device_kpad_to_upad(entity->links[l].sink,
  135. &link.sink);
  136. link.flags = entity->links[l].flags;
  137. if (copy_to_user(ulink, &link, sizeof(*ulink)))
  138. return -EFAULT;
  139. ulink++;
  140. }
  141. }
  142. if (copy_to_user(ulinks, &links, sizeof(*ulinks)))
  143. return -EFAULT;
  144. return 0;
  145. }
  146. static long media_device_setup_link(struct media_device *mdev,
  147. struct media_link_desc __user *_ulink)
  148. {
  149. struct media_link *link = NULL;
  150. struct media_link_desc ulink;
  151. struct media_entity *source;
  152. struct media_entity *sink;
  153. int ret;
  154. if (copy_from_user(&ulink, _ulink, sizeof(ulink)))
  155. return -EFAULT;
  156. /* Find the source and sink entities and link.
  157. */
  158. source = find_entity(mdev, ulink.source.entity);
  159. sink = find_entity(mdev, ulink.sink.entity);
  160. if (source == NULL || sink == NULL)
  161. return -EINVAL;
  162. if (ulink.source.index >= source->num_pads ||
  163. ulink.sink.index >= sink->num_pads)
  164. return -EINVAL;
  165. link = media_entity_find_link(&source->pads[ulink.source.index],
  166. &sink->pads[ulink.sink.index]);
  167. if (link == NULL)
  168. return -EINVAL;
  169. /* Setup the link on both entities. */
  170. ret = __media_entity_setup_link(link, ulink.flags);
  171. if (copy_to_user(_ulink, &ulink, sizeof(ulink)))
  172. return -EFAULT;
  173. return ret;
  174. }
  175. static long media_device_ioctl(struct file *filp, unsigned int cmd,
  176. unsigned long arg)
  177. {
  178. struct media_devnode *devnode = media_devnode_data(filp);
  179. struct media_device *dev = to_media_device(devnode);
  180. long ret;
  181. switch (cmd) {
  182. case MEDIA_IOC_DEVICE_INFO:
  183. ret = media_device_get_info(dev,
  184. (struct media_device_info __user *)arg);
  185. break;
  186. case MEDIA_IOC_ENUM_ENTITIES:
  187. ret = media_device_enum_entities(dev,
  188. (struct media_entity_desc __user *)arg);
  189. break;
  190. case MEDIA_IOC_ENUM_LINKS:
  191. mutex_lock(&dev->graph_mutex);
  192. ret = media_device_enum_links(dev,
  193. (struct media_links_enum __user *)arg);
  194. mutex_unlock(&dev->graph_mutex);
  195. break;
  196. case MEDIA_IOC_SETUP_LINK:
  197. mutex_lock(&dev->graph_mutex);
  198. ret = media_device_setup_link(dev,
  199. (struct media_link_desc __user *)arg);
  200. mutex_unlock(&dev->graph_mutex);
  201. break;
  202. default:
  203. ret = -ENOIOCTLCMD;
  204. }
  205. return ret;
  206. }
  207. static const struct media_file_operations media_device_fops = {
  208. .owner = THIS_MODULE,
  209. .open = media_device_open,
  210. .ioctl = media_device_ioctl,
  211. .release = media_device_close,
  212. };
  213. /* -----------------------------------------------------------------------------
  214. * sysfs
  215. */
  216. static ssize_t show_model(struct device *cd,
  217. struct device_attribute *attr, char *buf)
  218. {
  219. struct media_device *mdev = to_media_device(to_media_devnode(cd));
  220. return sprintf(buf, "%.*s\n", (int)sizeof(mdev->model), mdev->model);
  221. }
  222. static DEVICE_ATTR(model, S_IRUGO, show_model, NULL);
  223. /* -----------------------------------------------------------------------------
  224. * Registration/unregistration
  225. */
  226. static void media_device_release(struct media_devnode *mdev)
  227. {
  228. }
  229. /**
  230. * media_device_register - register a media device
  231. * @mdev: The media device
  232. *
  233. * The caller is responsible for initializing the media device before
  234. * registration. The following fields must be set:
  235. *
  236. * - dev must point to the parent device
  237. * - model must be filled with the device model name
  238. */
  239. int __must_check media_device_register(struct media_device *mdev)
  240. {
  241. int ret;
  242. if (WARN_ON(mdev->dev == NULL || mdev->model[0] == 0))
  243. return -EINVAL;
  244. mdev->entity_id = 1;
  245. INIT_LIST_HEAD(&mdev->entities);
  246. spin_lock_init(&mdev->lock);
  247. mutex_init(&mdev->graph_mutex);
  248. /* Register the device node. */
  249. mdev->devnode.fops = &media_device_fops;
  250. mdev->devnode.parent = mdev->dev;
  251. mdev->devnode.release = media_device_release;
  252. ret = media_devnode_register(&mdev->devnode);
  253. if (ret < 0)
  254. return ret;
  255. ret = device_create_file(&mdev->devnode.dev, &dev_attr_model);
  256. if (ret < 0) {
  257. media_devnode_unregister(&mdev->devnode);
  258. return ret;
  259. }
  260. return 0;
  261. }
  262. EXPORT_SYMBOL_GPL(media_device_register);
  263. /**
  264. * media_device_unregister - unregister a media device
  265. * @mdev: The media device
  266. *
  267. */
  268. void media_device_unregister(struct media_device *mdev)
  269. {
  270. struct media_entity *entity;
  271. struct media_entity *next;
  272. list_for_each_entry_safe(entity, next, &mdev->entities, list)
  273. media_device_unregister_entity(entity);
  274. device_remove_file(&mdev->devnode.dev, &dev_attr_model);
  275. media_devnode_unregister(&mdev->devnode);
  276. }
  277. EXPORT_SYMBOL_GPL(media_device_unregister);
  278. /**
  279. * media_device_register_entity - Register an entity with a media device
  280. * @mdev: The media device
  281. * @entity: The entity
  282. */
  283. int __must_check media_device_register_entity(struct media_device *mdev,
  284. struct media_entity *entity)
  285. {
  286. /* Warn if we apparently re-register an entity */
  287. WARN_ON(entity->parent != NULL);
  288. entity->parent = mdev;
  289. spin_lock(&mdev->lock);
  290. if (entity->id == 0)
  291. entity->id = mdev->entity_id++;
  292. else
  293. mdev->entity_id = max(entity->id + 1, mdev->entity_id);
  294. list_add_tail(&entity->list, &mdev->entities);
  295. spin_unlock(&mdev->lock);
  296. return 0;
  297. }
  298. EXPORT_SYMBOL_GPL(media_device_register_entity);
  299. /**
  300. * media_device_unregister_entity - Unregister an entity
  301. * @entity: The entity
  302. *
  303. * If the entity has never been registered this function will return
  304. * immediately.
  305. */
  306. void media_device_unregister_entity(struct media_entity *entity)
  307. {
  308. struct media_device *mdev = entity->parent;
  309. if (mdev == NULL)
  310. return;
  311. spin_lock(&mdev->lock);
  312. list_del(&entity->list);
  313. spin_unlock(&mdev->lock);
  314. entity->parent = NULL;
  315. }
  316. EXPORT_SYMBOL_GPL(media_device_unregister_entity);