media-devnode.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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 <asm/system.h>
  43. #include <media/media-devnode.h>
  44. #define MEDIA_NUM_DEVICES 256
  45. #define MEDIA_NAME "media"
  46. static dev_t media_dev_t;
  47. /*
  48. * Active devices
  49. */
  50. static DEFINE_MUTEX(media_devnode_lock);
  51. static DECLARE_BITMAP(media_devnode_nums, MEDIA_NUM_DEVICES);
  52. /* Called when the last user of the media device exits. */
  53. static void media_devnode_release(struct device *cd)
  54. {
  55. struct media_devnode *mdev = to_media_devnode(cd);
  56. mutex_lock(&media_devnode_lock);
  57. /* Delete the cdev on this minor as well */
  58. cdev_del(&mdev->cdev);
  59. /* Mark device node number as free */
  60. clear_bit(mdev->minor, media_devnode_nums);
  61. mutex_unlock(&media_devnode_lock);
  62. /* Release media_devnode and perform other cleanups as needed. */
  63. if (mdev->release)
  64. mdev->release(mdev);
  65. }
  66. static struct bus_type media_bus_type = {
  67. .name = MEDIA_NAME,
  68. };
  69. static ssize_t media_read(struct file *filp, char __user *buf,
  70. size_t sz, loff_t *off)
  71. {
  72. struct media_devnode *mdev = media_devnode_data(filp);
  73. if (!mdev->fops->read)
  74. return -EINVAL;
  75. if (!media_devnode_is_registered(mdev))
  76. return -EIO;
  77. return mdev->fops->read(filp, buf, sz, off);
  78. }
  79. static ssize_t media_write(struct file *filp, const char __user *buf,
  80. size_t sz, loff_t *off)
  81. {
  82. struct media_devnode *mdev = media_devnode_data(filp);
  83. if (!mdev->fops->write)
  84. return -EINVAL;
  85. if (!media_devnode_is_registered(mdev))
  86. return -EIO;
  87. return mdev->fops->write(filp, buf, sz, off);
  88. }
  89. static unsigned int media_poll(struct file *filp,
  90. struct poll_table_struct *poll)
  91. {
  92. struct media_devnode *mdev = media_devnode_data(filp);
  93. if (!media_devnode_is_registered(mdev))
  94. return POLLERR | POLLHUP;
  95. if (!mdev->fops->poll)
  96. return DEFAULT_POLLMASK;
  97. return mdev->fops->poll(filp, poll);
  98. }
  99. static long media_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  100. {
  101. struct media_devnode *mdev = media_devnode_data(filp);
  102. if (!mdev->fops->ioctl)
  103. return -ENOTTY;
  104. if (!media_devnode_is_registered(mdev))
  105. return -EIO;
  106. return mdev->fops->ioctl(filp, cmd, arg);
  107. }
  108. /* Override for the open function */
  109. static int media_open(struct inode *inode, struct file *filp)
  110. {
  111. struct media_devnode *mdev;
  112. int ret;
  113. /* Check if the media device is available. This needs to be done with
  114. * the media_devnode_lock held to prevent an open/unregister race:
  115. * without the lock, the device could be unregistered and freed between
  116. * the media_devnode_is_registered() and get_device() calls, leading to
  117. * a crash.
  118. */
  119. mutex_lock(&media_devnode_lock);
  120. mdev = container_of(inode->i_cdev, struct media_devnode, cdev);
  121. /* return ENXIO if the media device has been removed
  122. already or if it is not registered anymore. */
  123. if (!media_devnode_is_registered(mdev)) {
  124. mutex_unlock(&media_devnode_lock);
  125. return -ENXIO;
  126. }
  127. /* and increase the device refcount */
  128. get_device(&mdev->dev);
  129. mutex_unlock(&media_devnode_lock);
  130. filp->private_data = mdev;
  131. if (mdev->fops->open) {
  132. ret = mdev->fops->open(filp);
  133. if (ret) {
  134. put_device(&mdev->dev);
  135. return ret;
  136. }
  137. }
  138. return 0;
  139. }
  140. /* Override for the release function */
  141. static int media_release(struct inode *inode, struct file *filp)
  142. {
  143. struct media_devnode *mdev = media_devnode_data(filp);
  144. int ret = 0;
  145. if (mdev->fops->release)
  146. mdev->fops->release(filp);
  147. /* decrease the refcount unconditionally since the release()
  148. return value is ignored. */
  149. put_device(&mdev->dev);
  150. filp->private_data = NULL;
  151. return ret;
  152. }
  153. static const struct file_operations media_devnode_fops = {
  154. .owner = THIS_MODULE,
  155. .read = media_read,
  156. .write = media_write,
  157. .open = media_open,
  158. .unlocked_ioctl = media_ioctl,
  159. .release = media_release,
  160. .poll = media_poll,
  161. .llseek = no_llseek,
  162. };
  163. /**
  164. * media_devnode_register - register a media device node
  165. * @mdev: media device node structure we want to register
  166. *
  167. * The registration code assigns minor numbers and registers the new device node
  168. * with the kernel. An error is returned if no free minor number can be found,
  169. * or if the registration of the device node fails.
  170. *
  171. * Zero is returned on success.
  172. *
  173. * Note that if the media_devnode_register call fails, the release() callback of
  174. * the media_devnode structure is *not* called, so the caller is responsible for
  175. * freeing any data.
  176. */
  177. int __must_check media_devnode_register(struct media_devnode *mdev)
  178. {
  179. int minor;
  180. int ret;
  181. /* Part 1: Find a free minor number */
  182. mutex_lock(&media_devnode_lock);
  183. minor = find_next_zero_bit(media_devnode_nums, MEDIA_NUM_DEVICES, 0);
  184. if (minor == MEDIA_NUM_DEVICES) {
  185. mutex_unlock(&media_devnode_lock);
  186. printk(KERN_ERR "could not get a free minor\n");
  187. return -ENFILE;
  188. }
  189. set_bit(minor, media_devnode_nums);
  190. mutex_unlock(&media_devnode_lock);
  191. mdev->minor = minor;
  192. /* Part 2: Initialize and register the character device */
  193. cdev_init(&mdev->cdev, &media_devnode_fops);
  194. mdev->cdev.owner = mdev->fops->owner;
  195. ret = cdev_add(&mdev->cdev, MKDEV(MAJOR(media_dev_t), mdev->minor), 1);
  196. if (ret < 0) {
  197. printk(KERN_ERR "%s: cdev_add failed\n", __func__);
  198. goto error;
  199. }
  200. /* Part 3: Register the media device */
  201. mdev->dev.bus = &media_bus_type;
  202. mdev->dev.devt = MKDEV(MAJOR(media_dev_t), mdev->minor);
  203. mdev->dev.release = media_devnode_release;
  204. if (mdev->parent)
  205. mdev->dev.parent = mdev->parent;
  206. dev_set_name(&mdev->dev, "media%d", mdev->minor);
  207. ret = device_register(&mdev->dev);
  208. if (ret < 0) {
  209. printk(KERN_ERR "%s: device_register failed\n", __func__);
  210. goto error;
  211. }
  212. /* Part 4: Activate this minor. The char device can now be used. */
  213. set_bit(MEDIA_FLAG_REGISTERED, &mdev->flags);
  214. return 0;
  215. error:
  216. cdev_del(&mdev->cdev);
  217. clear_bit(mdev->minor, media_devnode_nums);
  218. return ret;
  219. }
  220. /**
  221. * media_devnode_unregister - unregister a media device node
  222. * @mdev: the device node to unregister
  223. *
  224. * This unregisters the passed device. Future open calls will be met with
  225. * errors.
  226. *
  227. * This function can safely be called if the device node has never been
  228. * registered or has already been unregistered.
  229. */
  230. void media_devnode_unregister(struct media_devnode *mdev)
  231. {
  232. /* Check if mdev was ever registered at all */
  233. if (!media_devnode_is_registered(mdev))
  234. return;
  235. mutex_lock(&media_devnode_lock);
  236. clear_bit(MEDIA_FLAG_REGISTERED, &mdev->flags);
  237. mutex_unlock(&media_devnode_lock);
  238. device_unregister(&mdev->dev);
  239. }
  240. /*
  241. * Initialise media for linux
  242. */
  243. static int __init media_devnode_init(void)
  244. {
  245. int ret;
  246. printk(KERN_INFO "Linux media interface: v0.10\n");
  247. ret = alloc_chrdev_region(&media_dev_t, 0, MEDIA_NUM_DEVICES,
  248. MEDIA_NAME);
  249. if (ret < 0) {
  250. printk(KERN_WARNING "media: unable to allocate major\n");
  251. return ret;
  252. }
  253. ret = bus_register(&media_bus_type);
  254. if (ret < 0) {
  255. unregister_chrdev_region(media_dev_t, MEDIA_NUM_DEVICES);
  256. printk(KERN_WARNING "media: bus_register failed\n");
  257. return -EIO;
  258. }
  259. return 0;
  260. }
  261. static void __exit media_devnode_exit(void)
  262. {
  263. bus_unregister(&media_bus_type);
  264. unregister_chrdev_region(media_dev_t, MEDIA_NUM_DEVICES);
  265. }
  266. module_init(media_devnode_init)
  267. module_exit(media_devnode_exit)
  268. MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
  269. MODULE_DESCRIPTION("Device node registration for media drivers");
  270. MODULE_LICENSE("GPL");