media-device.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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 <media/media-device.h>
  26. #include <media/media-devnode.h>
  27. #include <media/media-entity.h>
  28. /* -----------------------------------------------------------------------------
  29. * Userspace API
  30. */
  31. static int media_device_open(struct file *filp)
  32. {
  33. return 0;
  34. }
  35. static int media_device_close(struct file *filp)
  36. {
  37. return 0;
  38. }
  39. static int media_device_get_info(struct media_device *dev,
  40. struct media_device_info __user *__info)
  41. {
  42. struct media_device_info info;
  43. memset(&info, 0, sizeof(info));
  44. strlcpy(info.driver, dev->dev->driver->name, sizeof(info.driver));
  45. strlcpy(info.model, dev->model, sizeof(info.model));
  46. strlcpy(info.serial, dev->serial, sizeof(info.serial));
  47. strlcpy(info.bus_info, dev->bus_info, sizeof(info.bus_info));
  48. info.media_version = MEDIA_API_VERSION;
  49. info.hw_revision = dev->hw_revision;
  50. info.driver_version = dev->driver_version;
  51. return copy_to_user(__info, &info, sizeof(*__info));
  52. }
  53. static struct media_entity *find_entity(struct media_device *mdev, u32 id)
  54. {
  55. struct media_entity *entity;
  56. int next = id & MEDIA_ENT_ID_FLAG_NEXT;
  57. id &= ~MEDIA_ENT_ID_FLAG_NEXT;
  58. spin_lock(&mdev->lock);
  59. media_device_for_each_entity(entity, mdev) {
  60. if ((entity->id == id && !next) ||
  61. (entity->id > id && next)) {
  62. spin_unlock(&mdev->lock);
  63. return entity;
  64. }
  65. }
  66. spin_unlock(&mdev->lock);
  67. return NULL;
  68. }
  69. static long media_device_enum_entities(struct media_device *mdev,
  70. struct media_entity_desc __user *uent)
  71. {
  72. struct media_entity *ent;
  73. struct media_entity_desc u_ent;
  74. if (copy_from_user(&u_ent.id, &uent->id, sizeof(u_ent.id)))
  75. return -EFAULT;
  76. ent = find_entity(mdev, u_ent.id);
  77. if (ent == NULL)
  78. return -EINVAL;
  79. u_ent.id = ent->id;
  80. u_ent.name[0] = '\0';
  81. if (ent->name)
  82. strlcpy(u_ent.name, ent->name, sizeof(u_ent.name));
  83. u_ent.type = ent->type;
  84. u_ent.revision = ent->revision;
  85. u_ent.flags = ent->flags;
  86. u_ent.group_id = ent->group_id;
  87. u_ent.pads = ent->num_pads;
  88. u_ent.links = ent->num_links - ent->num_backlinks;
  89. u_ent.v4l.major = ent->v4l.major;
  90. u_ent.v4l.minor = ent->v4l.minor;
  91. if (copy_to_user(uent, &u_ent, sizeof(u_ent)))
  92. return -EFAULT;
  93. return 0;
  94. }
  95. static void media_device_kpad_to_upad(const struct media_pad *kpad,
  96. struct media_pad_desc *upad)
  97. {
  98. upad->entity = kpad->entity->id;
  99. upad->index = kpad->index;
  100. upad->flags = kpad->flags;
  101. }
  102. static long media_device_enum_links(struct media_device *mdev,
  103. struct media_links_enum __user *ulinks)
  104. {
  105. struct media_entity *entity;
  106. struct media_links_enum links;
  107. if (copy_from_user(&links, ulinks, sizeof(links)))
  108. return -EFAULT;
  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. if (copy_to_user(ulinks, &links, sizeof(*ulinks)))
  140. return -EFAULT;
  141. return 0;
  142. }
  143. static long media_device_setup_link(struct media_device *mdev,
  144. struct media_link_desc __user *_ulink)
  145. {
  146. struct media_link *link = NULL;
  147. struct media_link_desc ulink;
  148. struct media_entity *source;
  149. struct media_entity *sink;
  150. int ret;
  151. if (copy_from_user(&ulink, _ulink, sizeof(ulink)))
  152. return -EFAULT;
  153. /* Find the source and sink entities and link.
  154. */
  155. source = find_entity(mdev, ulink.source.entity);
  156. sink = find_entity(mdev, ulink.sink.entity);
  157. if (source == NULL || sink == NULL)
  158. return -EINVAL;
  159. if (ulink.source.index >= source->num_pads ||
  160. ulink.sink.index >= sink->num_pads)
  161. return -EINVAL;
  162. link = media_entity_find_link(&source->pads[ulink.source.index],
  163. &sink->pads[ulink.sink.index]);
  164. if (link == NULL)
  165. return -EINVAL;
  166. /* Setup the link on both entities. */
  167. ret = __media_entity_setup_link(link, ulink.flags);
  168. if (copy_to_user(_ulink, &ulink, sizeof(ulink)))
  169. return -EFAULT;
  170. return ret;
  171. }
  172. static long media_device_ioctl(struct file *filp, unsigned int cmd,
  173. unsigned long arg)
  174. {
  175. struct media_devnode *devnode = media_devnode_data(filp);
  176. struct media_device *dev = to_media_device(devnode);
  177. long ret;
  178. switch (cmd) {
  179. case MEDIA_IOC_DEVICE_INFO:
  180. ret = media_device_get_info(dev,
  181. (struct media_device_info __user *)arg);
  182. break;
  183. case MEDIA_IOC_ENUM_ENTITIES:
  184. ret = media_device_enum_entities(dev,
  185. (struct media_entity_desc __user *)arg);
  186. break;
  187. case MEDIA_IOC_ENUM_LINKS:
  188. mutex_lock(&dev->graph_mutex);
  189. ret = media_device_enum_links(dev,
  190. (struct media_links_enum __user *)arg);
  191. mutex_unlock(&dev->graph_mutex);
  192. break;
  193. case MEDIA_IOC_SETUP_LINK:
  194. mutex_lock(&dev->graph_mutex);
  195. ret = media_device_setup_link(dev,
  196. (struct media_link_desc __user *)arg);
  197. mutex_unlock(&dev->graph_mutex);
  198. break;
  199. default:
  200. ret = -ENOIOCTLCMD;
  201. }
  202. return ret;
  203. }
  204. static const struct media_file_operations media_device_fops = {
  205. .owner = THIS_MODULE,
  206. .open = media_device_open,
  207. .ioctl = media_device_ioctl,
  208. .release = media_device_close,
  209. };
  210. /* -----------------------------------------------------------------------------
  211. * sysfs
  212. */
  213. static ssize_t show_model(struct device *cd,
  214. struct device_attribute *attr, char *buf)
  215. {
  216. struct media_device *mdev = to_media_device(to_media_devnode(cd));
  217. return sprintf(buf, "%.*s\n", (int)sizeof(mdev->model), mdev->model);
  218. }
  219. static DEVICE_ATTR(model, S_IRUGO, show_model, NULL);
  220. /* -----------------------------------------------------------------------------
  221. * Registration/unregistration
  222. */
  223. static void media_device_release(struct media_devnode *mdev)
  224. {
  225. }
  226. /**
  227. * media_device_register - register a media device
  228. * @mdev: The media device
  229. *
  230. * The caller is responsible for initializing the media device before
  231. * registration. The following fields must be set:
  232. *
  233. * - dev must point to the parent device
  234. * - model must be filled with the device model name
  235. */
  236. int __must_check media_device_register(struct media_device *mdev)
  237. {
  238. int ret;
  239. if (WARN_ON(mdev->dev == NULL || mdev->model[0] == 0))
  240. return -EINVAL;
  241. mdev->entity_id = 1;
  242. INIT_LIST_HEAD(&mdev->entities);
  243. spin_lock_init(&mdev->lock);
  244. mutex_init(&mdev->graph_mutex);
  245. /* Register the device node. */
  246. mdev->devnode.fops = &media_device_fops;
  247. mdev->devnode.parent = mdev->dev;
  248. mdev->devnode.release = media_device_release;
  249. ret = media_devnode_register(&mdev->devnode);
  250. if (ret < 0)
  251. return ret;
  252. ret = device_create_file(&mdev->devnode.dev, &dev_attr_model);
  253. if (ret < 0) {
  254. media_devnode_unregister(&mdev->devnode);
  255. return ret;
  256. }
  257. return 0;
  258. }
  259. EXPORT_SYMBOL_GPL(media_device_register);
  260. /**
  261. * media_device_unregister - unregister a media device
  262. * @mdev: The media device
  263. *
  264. */
  265. void media_device_unregister(struct media_device *mdev)
  266. {
  267. struct media_entity *entity;
  268. struct media_entity *next;
  269. list_for_each_entry_safe(entity, next, &mdev->entities, list)
  270. media_device_unregister_entity(entity);
  271. device_remove_file(&mdev->devnode.dev, &dev_attr_model);
  272. media_devnode_unregister(&mdev->devnode);
  273. }
  274. EXPORT_SYMBOL_GPL(media_device_unregister);
  275. /**
  276. * media_device_register_entity - Register an entity with a media device
  277. * @mdev: The media device
  278. * @entity: The entity
  279. */
  280. int __must_check media_device_register_entity(struct media_device *mdev,
  281. struct media_entity *entity)
  282. {
  283. /* Warn if we apparently re-register an entity */
  284. WARN_ON(entity->parent != NULL);
  285. entity->parent = mdev;
  286. spin_lock(&mdev->lock);
  287. if (entity->id == 0)
  288. entity->id = mdev->entity_id++;
  289. else
  290. mdev->entity_id = max(entity->id + 1, mdev->entity_id);
  291. list_add_tail(&entity->list, &mdev->entities);
  292. spin_unlock(&mdev->lock);
  293. return 0;
  294. }
  295. EXPORT_SYMBOL_GPL(media_device_register_entity);
  296. /**
  297. * media_device_unregister_entity - Unregister an entity
  298. * @entity: The entity
  299. *
  300. * If the entity has never been registered this function will return
  301. * immediately.
  302. */
  303. void media_device_unregister_entity(struct media_entity *entity)
  304. {
  305. struct media_device *mdev = entity->parent;
  306. if (mdev == NULL)
  307. return;
  308. spin_lock(&mdev->lock);
  309. list_del(&entity->list);
  310. spin_unlock(&mdev->lock);
  311. entity->parent = NULL;
  312. }
  313. EXPORT_SYMBOL_GPL(media_device_unregister_entity);