media-devnode.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. * Media device node
  3. *
  4. * Copyright (C) 2010 Nokia Corporation
  5. *
  6. * Contacts: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
  7. * Sakari Ailus <sakari.ailus@iki.fi>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. *
  22. * --
  23. *
  24. * Common functions for media-related drivers to register and unregister media
  25. * device nodes.
  26. */
  27. #ifndef _MEDIA_DEVNODE_H
  28. #define _MEDIA_DEVNODE_H
  29. #include <linux/poll.h>
  30. #include <linux/fs.h>
  31. #include <linux/device.h>
  32. #include <linux/cdev.h>
  33. struct media_device;
  34. /*
  35. * Flag to mark the media_devnode struct as registered. Drivers must not touch
  36. * this flag directly, it will be set and cleared by media_devnode_register and
  37. * media_devnode_unregister.
  38. */
  39. #define MEDIA_FLAG_REGISTERED 0
  40. /**
  41. * struct media_file_operations - Media device file operations
  42. *
  43. * @owner: should be filled with %THIS_MODULE
  44. * @read: pointer to the function that implements read() syscall
  45. * @write: pointer to the function that implements write() syscall
  46. * @poll: pointer to the function that implements poll() syscall
  47. * @ioctl: pointer to the function that implements ioctl() syscall
  48. * @compat_ioctl: pointer to the function that will handle 32 bits userspace
  49. * calls to the the ioctl() syscall on a Kernel compiled with 64 bits.
  50. * @open: pointer to the function that implements open() syscall
  51. * @release: pointer to the function that will release the resources allocated
  52. * by the @open function.
  53. */
  54. struct media_file_operations {
  55. struct module *owner;
  56. ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);
  57. ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);
  58. unsigned int (*poll) (struct file *, struct poll_table_struct *);
  59. long (*ioctl) (struct file *, unsigned int, unsigned long);
  60. long (*compat_ioctl) (struct file *, unsigned int, unsigned long);
  61. int (*open) (struct file *);
  62. int (*release) (struct file *);
  63. };
  64. /**
  65. * struct media_devnode - Media device node
  66. * @media_dev: pointer to struct &media_device
  67. * @fops: pointer to struct &media_file_operations with media device ops
  68. * @dev: pointer to struct &device containing the media controller device
  69. * @cdev: struct cdev pointer character device
  70. * @parent: parent device
  71. * @minor: device node minor number
  72. * @flags: flags, combination of the ``MEDIA_FLAG_*`` constants
  73. * @release: release callback called at the end of ``media_devnode_release()``
  74. * routine at media-device.c.
  75. *
  76. * This structure represents a media-related device node.
  77. *
  78. * The @parent is a physical device. It must be set by core or device drivers
  79. * before registering the node.
  80. */
  81. struct media_devnode {
  82. struct media_device *media_dev;
  83. /* device ops */
  84. const struct media_file_operations *fops;
  85. /* sysfs */
  86. struct device dev; /* media device */
  87. struct cdev cdev; /* character device */
  88. struct device *parent; /* device parent */
  89. /* device info */
  90. int minor;
  91. unsigned long flags; /* Use bitops to access flags */
  92. /* callbacks */
  93. void (*release)(struct media_devnode *devnode);
  94. };
  95. /* dev to media_devnode */
  96. #define to_media_devnode(cd) container_of(cd, struct media_devnode, dev)
  97. /**
  98. * media_devnode_register - register a media device node
  99. *
  100. * @mdev: struct media_device we want to register a device node
  101. * @devnode: media device node structure we want to register
  102. * @owner: should be filled with %THIS_MODULE
  103. *
  104. * The registration code assigns minor numbers and registers the new device node
  105. * with the kernel. An error is returned if no free minor number can be found,
  106. * or if the registration of the device node fails.
  107. *
  108. * Zero is returned on success.
  109. *
  110. * Note that if the media_devnode_register call fails, the release() callback of
  111. * the media_devnode structure is *not* called, so the caller is responsible for
  112. * freeing any data.
  113. */
  114. int __must_check media_devnode_register(struct media_device *mdev,
  115. struct media_devnode *devnode,
  116. struct module *owner);
  117. /**
  118. * media_devnode_unregister_prepare - clear the media device node register bit
  119. * @devnode: the device node to prepare for unregister
  120. *
  121. * This clears the passed device register bit. Future open calls will be met
  122. * with errors. Should be called before media_devnode_unregister() to avoid
  123. * races with unregister and device file open calls.
  124. *
  125. * This function can safely be called if the device node has never been
  126. * registered or has already been unregistered.
  127. */
  128. void media_devnode_unregister_prepare(struct media_devnode *devnode);
  129. /**
  130. * media_devnode_unregister - unregister a media device node
  131. * @devnode: the device node to unregister
  132. *
  133. * This unregisters the passed device. Future open calls will be met with
  134. * errors.
  135. *
  136. * Should be called after media_devnode_unregister_prepare()
  137. */
  138. void media_devnode_unregister(struct media_devnode *devnode);
  139. /**
  140. * media_devnode_data - returns a pointer to the &media_devnode
  141. *
  142. * @filp: pointer to struct &file
  143. */
  144. static inline struct media_devnode *media_devnode_data(struct file *filp)
  145. {
  146. return filp->private_data;
  147. }
  148. /**
  149. * media_devnode_is_registered - returns true if &media_devnode is registered;
  150. * false otherwise.
  151. *
  152. * @devnode: pointer to struct &media_devnode.
  153. *
  154. * Note: If mdev is NULL, it also returns false.
  155. */
  156. static inline int media_devnode_is_registered(struct media_devnode *devnode)
  157. {
  158. if (!devnode)
  159. return false;
  160. return test_bit(MEDIA_FLAG_REGISTERED, &devnode->flags);
  161. }
  162. #endif /* _MEDIA_DEVNODE_H */