media-device.c 10 KB

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