media-device.c 11 KB

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