media-device.c 11 KB

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