media-device.c 11 KB

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