media-devnode.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  33. #include <linux/errno.h>
  34. #include <linux/init.h>
  35. #include <linux/module.h>
  36. #include <linux/kernel.h>
  37. #include <linux/kmod.h>
  38. #include <linux/slab.h>
  39. #include <linux/mm.h>
  40. #include <linux/string.h>
  41. #include <linux/types.h>
  42. #include <linux/uaccess.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
  100. __media_ioctl(struct file *filp, unsigned int cmd, unsigned long arg,
  101. long (*ioctl_func)(struct file *filp, unsigned int cmd,
  102. unsigned long arg))
  103. {
  104. struct media_devnode *mdev = media_devnode_data(filp);
  105. if (!ioctl_func)
  106. return -ENOTTY;
  107. if (!media_devnode_is_registered(mdev))
  108. return -EIO;
  109. return ioctl_func(filp, cmd, arg);
  110. }
  111. static long media_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  112. {
  113. struct media_devnode *mdev = media_devnode_data(filp);
  114. return __media_ioctl(filp, cmd, arg, mdev->fops->ioctl);
  115. }
  116. #ifdef CONFIG_COMPAT
  117. static long media_compat_ioctl(struct file *filp, unsigned int cmd,
  118. unsigned long arg)
  119. {
  120. struct media_devnode *mdev = media_devnode_data(filp);
  121. return __media_ioctl(filp, cmd, arg, mdev->fops->compat_ioctl);
  122. }
  123. #endif /* CONFIG_COMPAT */
  124. /* Override for the open function */
  125. static int media_open(struct inode *inode, struct file *filp)
  126. {
  127. struct media_devnode *mdev;
  128. int ret;
  129. /* Check if the media device is available. This needs to be done with
  130. * the media_devnode_lock held to prevent an open/unregister race:
  131. * without the lock, the device could be unregistered and freed between
  132. * the media_devnode_is_registered() and get_device() calls, leading to
  133. * a crash.
  134. */
  135. mutex_lock(&media_devnode_lock);
  136. mdev = container_of(inode->i_cdev, struct media_devnode, cdev);
  137. /* return ENXIO if the media device has been removed
  138. already or if it is not registered anymore. */
  139. if (!media_devnode_is_registered(mdev)) {
  140. mutex_unlock(&media_devnode_lock);
  141. return -ENXIO;
  142. }
  143. /* and increase the device refcount */
  144. get_device(&mdev->dev);
  145. mutex_unlock(&media_devnode_lock);
  146. filp->private_data = mdev;
  147. if (mdev->fops->open) {
  148. ret = mdev->fops->open(filp);
  149. if (ret) {
  150. put_device(&mdev->dev);
  151. return ret;
  152. }
  153. }
  154. return 0;
  155. }
  156. /* Override for the release function */
  157. static int media_release(struct inode *inode, struct file *filp)
  158. {
  159. struct media_devnode *mdev = media_devnode_data(filp);
  160. if (mdev->fops->release)
  161. mdev->fops->release(filp);
  162. /* decrease the refcount unconditionally since the release()
  163. return value is ignored. */
  164. put_device(&mdev->dev);
  165. filp->private_data = NULL;
  166. return 0;
  167. }
  168. static const struct file_operations media_devnode_fops = {
  169. .owner = THIS_MODULE,
  170. .read = media_read,
  171. .write = media_write,
  172. .open = media_open,
  173. .unlocked_ioctl = media_ioctl,
  174. #ifdef CONFIG_COMPAT
  175. .compat_ioctl = media_compat_ioctl,
  176. #endif /* CONFIG_COMPAT */
  177. .release = media_release,
  178. .poll = media_poll,
  179. .llseek = no_llseek,
  180. };
  181. /**
  182. * media_devnode_register - register a media device node
  183. * @mdev: media device node structure we want to register
  184. *
  185. * The registration code assigns minor numbers and registers the new device node
  186. * with the kernel. An error is returned if no free minor number can be found,
  187. * or if the registration of the device node fails.
  188. *
  189. * Zero is returned on success.
  190. *
  191. * Note that if the media_devnode_register call fails, the release() callback of
  192. * the media_devnode structure is *not* called, so the caller is responsible for
  193. * freeing any data.
  194. */
  195. int __must_check media_devnode_register(struct media_devnode *mdev,
  196. struct module *owner)
  197. {
  198. int minor;
  199. int ret;
  200. /* Part 1: Find a free minor number */
  201. mutex_lock(&media_devnode_lock);
  202. minor = find_next_zero_bit(media_devnode_nums, MEDIA_NUM_DEVICES, 0);
  203. if (minor == MEDIA_NUM_DEVICES) {
  204. mutex_unlock(&media_devnode_lock);
  205. pr_err("could not get a free minor\n");
  206. return -ENFILE;
  207. }
  208. set_bit(minor, media_devnode_nums);
  209. mutex_unlock(&media_devnode_lock);
  210. mdev->minor = minor;
  211. /* Part 2: Initialize and register the character device */
  212. cdev_init(&mdev->cdev, &media_devnode_fops);
  213. mdev->cdev.owner = owner;
  214. ret = cdev_add(&mdev->cdev, MKDEV(MAJOR(media_dev_t), mdev->minor), 1);
  215. if (ret < 0) {
  216. pr_err("%s: cdev_add failed\n", __func__);
  217. goto error;
  218. }
  219. /* Part 3: Register the media device */
  220. mdev->dev.bus = &media_bus_type;
  221. mdev->dev.devt = MKDEV(MAJOR(media_dev_t), mdev->minor);
  222. mdev->dev.release = media_devnode_release;
  223. if (mdev->parent)
  224. mdev->dev.parent = mdev->parent;
  225. dev_set_name(&mdev->dev, "media%d", mdev->minor);
  226. ret = device_register(&mdev->dev);
  227. if (ret < 0) {
  228. pr_err("%s: device_register failed\n", __func__);
  229. goto error;
  230. }
  231. /* Part 4: Activate this minor. The char device can now be used. */
  232. set_bit(MEDIA_FLAG_REGISTERED, &mdev->flags);
  233. return 0;
  234. error:
  235. cdev_del(&mdev->cdev);
  236. clear_bit(mdev->minor, media_devnode_nums);
  237. return ret;
  238. }
  239. /**
  240. * media_devnode_unregister - unregister a media device node
  241. * @mdev: the device node to unregister
  242. *
  243. * This unregisters the passed device. Future open calls will be met with
  244. * errors.
  245. *
  246. * This function can safely be called if the device node has never been
  247. * registered or has already been unregistered.
  248. */
  249. void media_devnode_unregister(struct media_devnode *mdev)
  250. {
  251. /* Check if mdev was ever registered at all */
  252. if (!media_devnode_is_registered(mdev))
  253. return;
  254. mutex_lock(&media_devnode_lock);
  255. clear_bit(MEDIA_FLAG_REGISTERED, &mdev->flags);
  256. mutex_unlock(&media_devnode_lock);
  257. device_unregister(&mdev->dev);
  258. }
  259. /*
  260. * Initialise media for linux
  261. */
  262. static int __init media_devnode_init(void)
  263. {
  264. int ret;
  265. pr_info("Linux media interface: v0.10\n");
  266. ret = alloc_chrdev_region(&media_dev_t, 0, MEDIA_NUM_DEVICES,
  267. MEDIA_NAME);
  268. if (ret < 0) {
  269. pr_warn("unable to allocate major\n");
  270. return ret;
  271. }
  272. ret = bus_register(&media_bus_type);
  273. if (ret < 0) {
  274. unregister_chrdev_region(media_dev_t, MEDIA_NUM_DEVICES);
  275. pr_warn("bus_register failed\n");
  276. return -EIO;
  277. }
  278. return 0;
  279. }
  280. static void __exit media_devnode_exit(void)
  281. {
  282. bus_unregister(&media_bus_type);
  283. unregister_chrdev_region(media_dev_t, MEDIA_NUM_DEVICES);
  284. }
  285. subsys_initcall(media_devnode_init);
  286. module_exit(media_devnode_exit)
  287. MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
  288. MODULE_DESCRIPTION("Device node registration for media drivers");
  289. MODULE_LICENSE("GPL");