media-device.c 9.5 KB

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