media-devnode.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /*
  2. * Media device node
  3. *
  4. * Copyright (C) 2010 Nokia Corporation
  5. *
  6. * Based on drivers/media/video/v4l2_dev.c code authored by
  7. * Mauro Carvalho Chehab <mchehab@infradead.org> (version 2)
  8. * Alan Cox, <alan@lxorguk.ukuu.org.uk> (version 1)
  9. *
  10. * Contacts: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
  11. * Sakari Ailus <sakari.ailus@iki.fi>
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License version 2 as
  15. * published by the Free Software Foundation.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  25. *
  26. * --
  27. *
  28. * Generic media device node infrastructure to register and unregister
  29. * character devices using a dynamic major number and proper reference
  30. * counting.
  31. */
  32. #include <linux/errno.h>
  33. #include <linux/init.h>
  34. #include <linux/module.h>
  35. #include <linux/kernel.h>
  36. #include <linux/kmod.h>
  37. #include <linux/slab.h>
  38. #include <linux/mm.h>
  39. #include <linux/string.h>
  40. #include <linux/types.h>
  41. #include <linux/uaccess.h>
  42. #include <media/media-devnode.h>
  43. #define MEDIA_NUM_DEVICES 256
  44. #define MEDIA_NAME "media"
  45. static dev_t media_dev_t;
  46. /*
  47. * Active devices
  48. */
  49. static DEFINE_MUTEX(media_devnode_lock);
  50. static DECLARE_BITMAP(media_devnode_nums, MEDIA_NUM_DEVICES);
  51. /* Called when the last user of the media device exits. */
  52. static void media_devnode_release(struct device *cd)
  53. {
  54. struct media_devnode *mdev = to_media_devnode(cd);
  55. mutex_lock(&media_devnode_lock);
  56. /* Delete the cdev on this minor as well */
  57. cdev_del(&mdev->cdev);
  58. /* Mark device node number as free */
  59. clear_bit(mdev->minor, media_devnode_nums);
  60. mutex_unlock(&media_devnode_lock);
  61. /* Release media_devnode and perform other cleanups as needed. */
  62. if (mdev->release)
  63. mdev->release(mdev);
  64. }
  65. static struct bus_type media_bus_type = {
  66. .name = MEDIA_NAME,
  67. };
  68. static ssize_t media_read(struct file *filp, char __user *buf,
  69. size_t sz, loff_t *off)
  70. {
  71. struct media_devnode *mdev = media_devnode_data(filp);
  72. if (!mdev->fops->read)
  73. return -EINVAL;
  74. if (!media_devnode_is_registered(mdev))
  75. return -EIO;
  76. return mdev->fops->read(filp, buf, sz, off);
  77. }
  78. static ssize_t media_write(struct file *filp, const char __user *buf,
  79. size_t sz, loff_t *off)
  80. {
  81. struct media_devnode *mdev = media_devnode_data(filp);
  82. if (!mdev->fops->write)
  83. return -EINVAL;
  84. if (!media_devnode_is_registered(mdev))
  85. return -EIO;
  86. return mdev->fops->write(filp, buf, sz, off);
  87. }
  88. static unsigned int media_poll(struct file *filp,
  89. struct poll_table_struct *poll)
  90. {
  91. struct media_devnode *mdev = media_devnode_data(filp);
  92. if (!media_devnode_is_registered(mdev))
  93. return POLLERR | POLLHUP;
  94. if (!mdev->fops->poll)
  95. return DEFAULT_POLLMASK;
  96. return mdev->fops->poll(filp, poll);
  97. }
  98. static long media_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  99. {
  100. struct media_devnode *mdev = media_devnode_data(filp);
  101. if (!mdev->fops->ioctl)
  102. return -ENOTTY;
  103. if (!media_devnode_is_registered(mdev))
  104. return -EIO;
  105. return mdev->fops->ioctl(filp, cmd, arg);
  106. }
  107. /* Override for the open function */
  108. static int media_open(struct inode *inode, struct file *filp)
  109. {
  110. struct media_devnode *mdev;
  111. int ret;
  112. /* Check if the media device is available. This needs to be done with
  113. * the media_devnode_lock held to prevent an open/unregister race:
  114. * without the lock, the device could be unregistered and freed between
  115. * the media_devnode_is_registered() and get_device() calls, leading to
  116. * a crash.
  117. */
  118. mutex_lock(&media_devnode_lock);
  119. mdev = container_of(inode->i_cdev, struct media_devnode, cdev);
  120. /* return ENXIO if the media device has been removed
  121. already or if it is not registered anymore. */
  122. if (!media_devnode_is_registered(mdev)) {
  123. mutex_unlock(&media_devnode_lock);
  124. return -ENXIO;
  125. }
  126. /* and increase the device refcount */
  127. get_device(&mdev->dev);
  128. mutex_unlock(&media_devnode_lock);
  129. filp->private_data = mdev;
  130. if (mdev->fops->open) {
  131. ret = mdev->fops->open(filp);
  132. if (ret) {
  133. put_device(&mdev->dev);
  134. return ret;
  135. }
  136. }
  137. return 0;
  138. }
  139. /* Override for the release function */
  140. static int media_release(struct inode *inode, struct file *filp)
  141. {
  142. struct media_devnode *mdev = media_devnode_data(filp);
  143. int ret = 0;
  144. if (mdev->fops->release)
  145. mdev->fops->release(filp);
  146. /* decrease the refcount unconditionally since the release()
  147. return value is ignored. */
  148. put_device(&mdev->dev);
  149. filp->private_data = NULL;
  150. return ret;
  151. }
  152. static const struct file_operations media_devnode_fops = {
  153. .owner = THIS_MODULE,
  154. .read = media_read,
  155. .write = media_write,
  156. .open = media_open,
  157. .unlocked_ioctl = media_ioctl,
  158. .release = media_release,
  159. .poll = media_poll,
  160. .llseek = no_llseek,
  161. };
  162. /**
  163. * media_devnode_register - register a media device node
  164. * @mdev: media device node structure we want to register
  165. *
  166. * The registration code assigns minor numbers and registers the new device node
  167. * with the kernel. An error is returned if no free minor number can be found,
  168. * or if the registration of the device node fails.
  169. *
  170. * Zero is returned on success.
  171. *
  172. * Note that if the media_devnode_register call fails, the release() callback of
  173. * the media_devnode structure is *not* called, so the caller is responsible for
  174. * freeing any data.
  175. */
  176. int __must_check media_devnode_register(struct media_devnode *mdev)
  177. {
  178. int minor;
  179. int ret;
  180. /* Part 1: Find a free minor number */
  181. mutex_lock(&media_devnode_lock);
  182. minor = find_next_zero_bit(media_devnode_nums, MEDIA_NUM_DEVICES, 0);
  183. if (minor == MEDIA_NUM_DEVICES) {
  184. mutex_unlock(&media_devnode_lock);
  185. printk(KERN_ERR "could not get a free minor\n");
  186. return -ENFILE;
  187. }
  188. set_bit(minor, media_devnode_nums);
  189. mutex_unlock(&media_devnode_lock);
  190. mdev->minor = minor;
  191. /* Part 2: Initialize and register the character device */
  192. cdev_init(&mdev->cdev, &media_devnode_fops);
  193. mdev->cdev.owner = mdev->fops->owner;
  194. ret = cdev_add(&mdev->cdev, MKDEV(MAJOR(media_dev_t), mdev->minor), 1);
  195. if (ret < 0) {
  196. printk(KERN_ERR "%s: cdev_add failed\n", __func__);
  197. goto error;
  198. }
  199. /* Part 3: Register the media device */
  200. mdev->dev.bus = &media_bus_type;
  201. mdev->dev.devt = MKDEV(MAJOR(media_dev_t), mdev->minor);
  202. mdev->dev.release = media_devnode_release;
  203. if (mdev->parent)
  204. mdev->dev.parent = mdev->parent;
  205. dev_set_name(&mdev->dev, "media%d", mdev->minor);
  206. ret = device_register(&mdev->dev);
  207. if (ret < 0) {
  208. printk(KERN_ERR "%s: device_register failed\n", __func__);
  209. goto error;
  210. }
  211. /* Part 4: Activate this minor. The char device can now be used. */
  212. set_bit(MEDIA_FLAG_REGISTERED, &mdev->flags);
  213. return 0;
  214. error:
  215. cdev_del(&mdev->cdev);
  216. clear_bit(mdev->minor, media_devnode_nums);
  217. return ret;
  218. }
  219. /**
  220. * media_devnode_unregister - unregister a media device node
  221. * @mdev: the device node to unregister
  222. *
  223. * This unregisters the passed device. Future open calls will be met with
  224. * errors.
  225. *
  226. * This function can safely be called if the device node has never been
  227. * registered or has already been unregistered.
  228. */
  229. void media_devnode_unregister(struct media_devnode *mdev)
  230. {
  231. /* Check if mdev was ever registered at all */
  232. if (!media_devnode_is_registered(mdev))
  233. return;
  234. mutex_lock(&media_devnode_lock);
  235. clear_bit(MEDIA_FLAG_REGISTERED, &mdev->flags);
  236. mutex_unlock(&media_devnode_lock);
  237. device_unregister(&mdev->dev);
  238. }
  239. /*
  240. * Initialise media for linux
  241. */
  242. static int __init media_devnode_init(void)
  243. {
  244. int ret;
  245. printk(KERN_INFO "Linux media interface: v0.10\n");
  246. ret = alloc_chrdev_region(&media_dev_t, 0, MEDIA_NUM_DEVICES,
  247. MEDIA_NAME);
  248. if (ret < 0) {
  249. printk(KERN_WARNING "media: unable to allocate major\n");
  250. return ret;
  251. }
  252. ret = bus_register(&media_bus_type);
  253. if (ret < 0) {
  254. unregister_chrdev_region(media_dev_t, MEDIA_NUM_DEVICES);
  255. printk(KERN_WARNING "media: bus_register failed\n");
  256. return -EIO;
  257. }
  258. return 0;
  259. }
  260. static void __exit media_devnode_exit(void)
  261. {
  262. bus_unregister(&media_bus_type);
  263. unregister_chrdev_region(media_dev_t, MEDIA_NUM_DEVICES);
  264. }
  265. subsys_initcall(media_devnode_init);
  266. module_exit(media_devnode_exit)
  267. MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
  268. MODULE_DESCRIPTION("Device node registration for media drivers");
  269. MODULE_LICENSE("GPL");