media-device.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  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. memset(&u_ent, 0, sizeof(u_ent));
  79. if (copy_from_user(&u_ent.id, &uent->id, sizeof(u_ent.id)))
  80. return -EFAULT;
  81. ent = find_entity(mdev, u_ent.id);
  82. if (ent == NULL)
  83. return -EINVAL;
  84. u_ent.id = ent->id;
  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. memset(&pad, 0, sizeof(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. memset(&link, 0, sizeof(link));
  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. return 0;
  142. }
  143. static long media_device_enum_links(struct media_device *mdev,
  144. struct media_links_enum __user *ulinks)
  145. {
  146. struct media_links_enum links;
  147. int rval;
  148. if (copy_from_user(&links, ulinks, sizeof(links)))
  149. return -EFAULT;
  150. rval = __media_device_enum_links(mdev, &links);
  151. if (rval < 0)
  152. return rval;
  153. if (copy_to_user(ulinks, &links, sizeof(*ulinks)))
  154. return -EFAULT;
  155. return 0;
  156. }
  157. static long media_device_setup_link(struct media_device *mdev,
  158. struct media_link_desc __user *_ulink)
  159. {
  160. struct media_link *link = NULL;
  161. struct media_link_desc ulink;
  162. struct media_entity *source;
  163. struct media_entity *sink;
  164. int ret;
  165. if (copy_from_user(&ulink, _ulink, sizeof(ulink)))
  166. return -EFAULT;
  167. /* Find the source and sink entities and link.
  168. */
  169. source = find_entity(mdev, ulink.source.entity);
  170. sink = find_entity(mdev, ulink.sink.entity);
  171. if (source == NULL || sink == NULL)
  172. return -EINVAL;
  173. if (ulink.source.index >= source->num_pads ||
  174. ulink.sink.index >= sink->num_pads)
  175. return -EINVAL;
  176. link = media_entity_find_link(&source->pads[ulink.source.index],
  177. &sink->pads[ulink.sink.index]);
  178. if (link == NULL)
  179. return -EINVAL;
  180. /* Setup the link on both entities. */
  181. ret = __media_entity_setup_link(link, ulink.flags);
  182. if (copy_to_user(_ulink, &ulink, sizeof(ulink)))
  183. return -EFAULT;
  184. return ret;
  185. }
  186. static long media_device_ioctl(struct file *filp, unsigned int cmd,
  187. unsigned long arg)
  188. {
  189. struct media_devnode *devnode = media_devnode_data(filp);
  190. struct media_device *dev = to_media_device(devnode);
  191. long ret;
  192. switch (cmd) {
  193. case MEDIA_IOC_DEVICE_INFO:
  194. ret = media_device_get_info(dev,
  195. (struct media_device_info __user *)arg);
  196. break;
  197. case MEDIA_IOC_ENUM_ENTITIES:
  198. ret = media_device_enum_entities(dev,
  199. (struct media_entity_desc __user *)arg);
  200. break;
  201. case MEDIA_IOC_ENUM_LINKS:
  202. mutex_lock(&dev->graph_mutex);
  203. ret = media_device_enum_links(dev,
  204. (struct media_links_enum __user *)arg);
  205. mutex_unlock(&dev->graph_mutex);
  206. break;
  207. case MEDIA_IOC_SETUP_LINK:
  208. mutex_lock(&dev->graph_mutex);
  209. ret = media_device_setup_link(dev,
  210. (struct media_link_desc __user *)arg);
  211. mutex_unlock(&dev->graph_mutex);
  212. break;
  213. default:
  214. ret = -ENOIOCTLCMD;
  215. }
  216. return ret;
  217. }
  218. #ifdef CONFIG_COMPAT
  219. struct media_links_enum32 {
  220. __u32 entity;
  221. compat_uptr_t pads; /* struct media_pad_desc * */
  222. compat_uptr_t links; /* struct media_link_desc * */
  223. __u32 reserved[4];
  224. };
  225. static long media_device_enum_links32(struct media_device *mdev,
  226. struct media_links_enum32 __user *ulinks)
  227. {
  228. struct media_links_enum links;
  229. compat_uptr_t pads_ptr, links_ptr;
  230. memset(&links, 0, sizeof(links));
  231. if (get_user(links.entity, &ulinks->entity)
  232. || get_user(pads_ptr, &ulinks->pads)
  233. || get_user(links_ptr, &ulinks->links))
  234. return -EFAULT;
  235. links.pads = compat_ptr(pads_ptr);
  236. links.links = compat_ptr(links_ptr);
  237. return __media_device_enum_links(mdev, &links);
  238. }
  239. #define MEDIA_IOC_ENUM_LINKS32 _IOWR('|', 0x02, struct media_links_enum32)
  240. static long media_device_compat_ioctl(struct file *filp, unsigned int cmd,
  241. unsigned long arg)
  242. {
  243. struct media_devnode *devnode = media_devnode_data(filp);
  244. struct media_device *dev = to_media_device(devnode);
  245. long ret;
  246. switch (cmd) {
  247. case MEDIA_IOC_DEVICE_INFO:
  248. case MEDIA_IOC_ENUM_ENTITIES:
  249. case MEDIA_IOC_SETUP_LINK:
  250. return media_device_ioctl(filp, cmd, arg);
  251. case MEDIA_IOC_ENUM_LINKS32:
  252. mutex_lock(&dev->graph_mutex);
  253. ret = media_device_enum_links32(dev,
  254. (struct media_links_enum32 __user *)arg);
  255. mutex_unlock(&dev->graph_mutex);
  256. break;
  257. default:
  258. ret = -ENOIOCTLCMD;
  259. }
  260. return ret;
  261. }
  262. #endif /* CONFIG_COMPAT */
  263. static const struct media_file_operations media_device_fops = {
  264. .owner = THIS_MODULE,
  265. .open = media_device_open,
  266. .ioctl = media_device_ioctl,
  267. #ifdef CONFIG_COMPAT
  268. .compat_ioctl = media_device_compat_ioctl,
  269. #endif /* CONFIG_COMPAT */
  270. .release = media_device_close,
  271. };
  272. /* -----------------------------------------------------------------------------
  273. * sysfs
  274. */
  275. static ssize_t show_model(struct device *cd,
  276. struct device_attribute *attr, char *buf)
  277. {
  278. struct media_device *mdev = to_media_device(to_media_devnode(cd));
  279. return sprintf(buf, "%.*s\n", (int)sizeof(mdev->model), mdev->model);
  280. }
  281. static DEVICE_ATTR(model, S_IRUGO, show_model, NULL);
  282. /* -----------------------------------------------------------------------------
  283. * Registration/unregistration
  284. */
  285. static void media_device_release(struct media_devnode *mdev)
  286. {
  287. }
  288. /**
  289. * media_device_register - register a media device
  290. * @mdev: The media device
  291. *
  292. * The caller is responsible for initializing the media device before
  293. * registration. The following fields must be set:
  294. *
  295. * - dev must point to the parent device
  296. * - model must be filled with the device model name
  297. */
  298. int __must_check __media_device_register(struct media_device *mdev,
  299. struct module *owner)
  300. {
  301. int ret;
  302. if (WARN_ON(mdev->dev == NULL || mdev->model[0] == 0))
  303. return -EINVAL;
  304. mdev->entity_id = 1;
  305. INIT_LIST_HEAD(&mdev->entities);
  306. spin_lock_init(&mdev->lock);
  307. mutex_init(&mdev->graph_mutex);
  308. /* Register the device node. */
  309. mdev->devnode.fops = &media_device_fops;
  310. mdev->devnode.parent = mdev->dev;
  311. mdev->devnode.release = media_device_release;
  312. ret = media_devnode_register(&mdev->devnode, owner);
  313. if (ret < 0)
  314. return ret;
  315. ret = device_create_file(&mdev->devnode.dev, &dev_attr_model);
  316. if (ret < 0) {
  317. media_devnode_unregister(&mdev->devnode);
  318. return ret;
  319. }
  320. return 0;
  321. }
  322. EXPORT_SYMBOL_GPL(__media_device_register);
  323. /**
  324. * media_device_unregister - unregister a media device
  325. * @mdev: The media device
  326. *
  327. */
  328. void media_device_unregister(struct media_device *mdev)
  329. {
  330. struct media_entity *entity;
  331. struct media_entity *next;
  332. list_for_each_entry_safe(entity, next, &mdev->entities, list)
  333. media_device_unregister_entity(entity);
  334. device_remove_file(&mdev->devnode.dev, &dev_attr_model);
  335. media_devnode_unregister(&mdev->devnode);
  336. }
  337. EXPORT_SYMBOL_GPL(media_device_unregister);
  338. /**
  339. * media_device_register_entity - Register an entity with a media device
  340. * @mdev: The media device
  341. * @entity: The entity
  342. */
  343. int __must_check media_device_register_entity(struct media_device *mdev,
  344. struct media_entity *entity)
  345. {
  346. /* Warn if we apparently re-register an entity */
  347. WARN_ON(entity->parent != NULL);
  348. entity->parent = mdev;
  349. spin_lock(&mdev->lock);
  350. if (entity->id == 0)
  351. entity->id = mdev->entity_id++;
  352. else
  353. mdev->entity_id = max(entity->id + 1, mdev->entity_id);
  354. list_add_tail(&entity->list, &mdev->entities);
  355. spin_unlock(&mdev->lock);
  356. return 0;
  357. }
  358. EXPORT_SYMBOL_GPL(media_device_register_entity);
  359. /**
  360. * media_device_unregister_entity - Unregister an entity
  361. * @entity: The entity
  362. *
  363. * If the entity has never been registered this function will return
  364. * immediately.
  365. */
  366. void media_device_unregister_entity(struct media_entity *entity)
  367. {
  368. struct media_device *mdev = entity->parent;
  369. if (mdev == NULL)
  370. return;
  371. spin_lock(&mdev->lock);
  372. list_del(&entity->list);
  373. spin_unlock(&mdev->lock);
  374. entity->parent = NULL;
  375. }
  376. EXPORT_SYMBOL_GPL(media_device_unregister_entity);