media-devnode.c 9.4 KB

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