media-devnode.c 9.1 KB

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