media-devnode.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. * --
  19. *
  20. * Common functions for media-related drivers to register and unregister media
  21. * device nodes.
  22. */
  23. #ifndef _MEDIA_DEVNODE_H
  24. #define _MEDIA_DEVNODE_H
  25. #include <linux/poll.h>
  26. #include <linux/fs.h>
  27. #include <linux/device.h>
  28. #include <linux/cdev.h>
  29. struct media_device;
  30. /*
  31. * Flag to mark the media_devnode struct as registered. Drivers must not touch
  32. * this flag directly, it will be set and cleared by media_devnode_register and
  33. * media_devnode_unregister.
  34. */
  35. #define MEDIA_FLAG_REGISTERED 0
  36. /**
  37. * struct media_file_operations - Media device file operations
  38. *
  39. * @owner: should be filled with %THIS_MODULE
  40. * @read: pointer to the function that implements read() syscall
  41. * @write: pointer to the function that implements write() syscall
  42. * @poll: pointer to the function that implements poll() syscall
  43. * @ioctl: pointer to the function that implements ioctl() syscall
  44. * @compat_ioctl: pointer to the function that will handle 32 bits userspace
  45. * calls to the the ioctl() syscall on a Kernel compiled with 64 bits.
  46. * @open: pointer to the function that implements open() syscall
  47. * @release: pointer to the function that will release the resources allocated
  48. * by the @open function.
  49. */
  50. struct media_file_operations {
  51. struct module *owner;
  52. ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);
  53. ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);
  54. __poll_t (*poll) (struct file *, struct poll_table_struct *);
  55. long (*ioctl) (struct file *, unsigned int, unsigned long);
  56. long (*compat_ioctl) (struct file *, unsigned int, unsigned long);
  57. int (*open) (struct file *);
  58. int (*release) (struct file *);
  59. };
  60. /**
  61. * struct media_devnode - Media device node
  62. * @media_dev: pointer to struct &media_device
  63. * @fops: pointer to struct &media_file_operations with media device ops
  64. * @dev: pointer to struct &device containing the media controller device
  65. * @cdev: struct cdev pointer character device
  66. * @parent: parent device
  67. * @minor: device node minor number
  68. * @flags: flags, combination of the ``MEDIA_FLAG_*`` constants
  69. * @release: release callback called at the end of ``media_devnode_release()``
  70. * routine at media-device.c.
  71. *
  72. * This structure represents a media-related device node.
  73. *
  74. * The @parent is a physical device. It must be set by core or device drivers
  75. * before registering the node.
  76. */
  77. struct media_devnode {
  78. struct media_device *media_dev;
  79. /* device ops */
  80. const struct media_file_operations *fops;
  81. /* sysfs */
  82. struct device dev; /* media device */
  83. struct cdev cdev; /* character device */
  84. struct device *parent; /* device parent */
  85. /* device info */
  86. int minor;
  87. unsigned long flags; /* Use bitops to access flags */
  88. /* callbacks */
  89. void (*release)(struct media_devnode *devnode);
  90. };
  91. /* dev to media_devnode */
  92. #define to_media_devnode(cd) container_of(cd, struct media_devnode, dev)
  93. /**
  94. * media_devnode_register - register a media device node
  95. *
  96. * @mdev: struct media_device we want to register a device node
  97. * @devnode: media device node structure we want to register
  98. * @owner: should be filled with %THIS_MODULE
  99. *
  100. * The registration code assigns minor numbers and registers the new device node
  101. * with the kernel. An error is returned if no free minor number can be found,
  102. * or if the registration of the device node fails.
  103. *
  104. * Zero is returned on success.
  105. *
  106. * Note that if the media_devnode_register call fails, the release() callback of
  107. * the media_devnode structure is *not* called, so the caller is responsible for
  108. * freeing any data.
  109. */
  110. int __must_check media_devnode_register(struct media_device *mdev,
  111. struct media_devnode *devnode,
  112. struct module *owner);
  113. /**
  114. * media_devnode_unregister_prepare - clear the media device node register bit
  115. * @devnode: the device node to prepare for unregister
  116. *
  117. * This clears the passed device register bit. Future open calls will be met
  118. * with errors. Should be called before media_devnode_unregister() to avoid
  119. * races with unregister and device file open calls.
  120. *
  121. * This function can safely be called if the device node has never been
  122. * registered or has already been unregistered.
  123. */
  124. void media_devnode_unregister_prepare(struct media_devnode *devnode);
  125. /**
  126. * media_devnode_unregister - unregister a media device node
  127. * @devnode: the device node to unregister
  128. *
  129. * This unregisters the passed device. Future open calls will be met with
  130. * errors.
  131. *
  132. * Should be called after media_devnode_unregister_prepare()
  133. */
  134. void media_devnode_unregister(struct media_devnode *devnode);
  135. /**
  136. * media_devnode_data - returns a pointer to the &media_devnode
  137. *
  138. * @filp: pointer to struct &file
  139. */
  140. static inline struct media_devnode *media_devnode_data(struct file *filp)
  141. {
  142. return filp->private_data;
  143. }
  144. /**
  145. * media_devnode_is_registered - returns true if &media_devnode is registered;
  146. * false otherwise.
  147. *
  148. * @devnode: pointer to struct &media_devnode.
  149. *
  150. * Note: If mdev is NULL, it also returns false.
  151. */
  152. static inline int media_devnode_is_registered(struct media_devnode *devnode)
  153. {
  154. if (!devnode)
  155. return false;
  156. return test_bit(MEDIA_FLAG_REGISTERED, &devnode->flags);
  157. }
  158. #endif /* _MEDIA_DEVNODE_H */