v4l2-subdev.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. V4L2 sub-device support header.
  3. Copyright (C) 2008 Hans Verkuil <hverkuil@xs4all.nl>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  15. */
  16. #ifndef _V4L2_SUBDEV_H
  17. #define _V4L2_SUBDEV_H
  18. #include <media/v4l2-common.h>
  19. struct v4l2_device;
  20. struct v4l2_subdev;
  21. struct tuner_setup;
  22. /* Sub-devices are devices that are connected somehow to the main bridge
  23. device. These devices are usually audio/video muxers/encoders/decoders or
  24. sensors and webcam controllers.
  25. Usually these devices are controlled through an i2c bus, but other busses
  26. may also be used.
  27. The v4l2_subdev struct provides a way of accessing these devices in a
  28. generic manner. Most operations that these sub-devices support fall in
  29. a few categories: core ops, audio ops, video ops and tuner ops.
  30. More categories can be added if needed, although this should remain a
  31. limited set (no more than approx. 8 categories).
  32. Each category has its own set of ops that subdev drivers can implement.
  33. A subdev driver can leave the pointer to the category ops NULL if
  34. it does not implement them (e.g. an audio subdev will generally not
  35. implement the video category ops). The exception is the core category:
  36. this must always be present.
  37. These ops are all used internally so it is no problem to change, remove
  38. or add ops or move ops from one to another category. Currently these
  39. ops are based on the original ioctls, but since ops are not limited to
  40. one argument there is room for improvement here once all i2c subdev
  41. drivers are converted to use these ops.
  42. */
  43. /* Core ops: it is highly recommended to implement at least these ops:
  44. g_chip_ident
  45. log_status
  46. g_register
  47. s_register
  48. This provides basic debugging support.
  49. The ioctl ops is meant for generic ioctl-like commands. Depending on
  50. the use-case it might be better to use subdev-specific ops (currently
  51. not yet implemented) since ops provide proper type-checking.
  52. */
  53. struct v4l2_subdev_core_ops {
  54. int (*g_chip_ident)(struct v4l2_subdev *sd, struct v4l2_dbg_chip_ident *chip);
  55. int (*log_status)(struct v4l2_subdev *sd);
  56. int (*init)(struct v4l2_subdev *sd, u32 val);
  57. int (*s_standby)(struct v4l2_subdev *sd, u32 standby);
  58. int (*reset)(struct v4l2_subdev *sd, u32 val);
  59. int (*s_gpio)(struct v4l2_subdev *sd, u32 val);
  60. int (*queryctrl)(struct v4l2_subdev *sd, struct v4l2_queryctrl *qc);
  61. int (*g_ctrl)(struct v4l2_subdev *sd, struct v4l2_control *ctrl);
  62. int (*s_ctrl)(struct v4l2_subdev *sd, struct v4l2_control *ctrl);
  63. int (*querymenu)(struct v4l2_subdev *sd, struct v4l2_querymenu *qm);
  64. long (*ioctl)(struct v4l2_subdev *sd, unsigned int cmd, void *arg);
  65. #ifdef CONFIG_VIDEO_ADV_DEBUG
  66. int (*g_register)(struct v4l2_subdev *sd, struct v4l2_dbg_register *reg);
  67. int (*s_register)(struct v4l2_subdev *sd, struct v4l2_dbg_register *reg);
  68. #endif
  69. };
  70. struct v4l2_subdev_tuner_ops {
  71. int (*s_mode)(struct v4l2_subdev *sd, enum v4l2_tuner_type);
  72. int (*s_radio)(struct v4l2_subdev *sd);
  73. int (*s_frequency)(struct v4l2_subdev *sd, struct v4l2_frequency *freq);
  74. int (*g_frequency)(struct v4l2_subdev *sd, struct v4l2_frequency *freq);
  75. int (*g_tuner)(struct v4l2_subdev *sd, struct v4l2_tuner *vt);
  76. int (*s_tuner)(struct v4l2_subdev *sd, struct v4l2_tuner *vt);
  77. int (*s_std)(struct v4l2_subdev *sd, v4l2_std_id norm);
  78. int (*s_type_addr)(struct v4l2_subdev *sd, struct tuner_setup *type);
  79. int (*s_config)(struct v4l2_subdev *sd, const struct v4l2_priv_tun_config *config);
  80. };
  81. struct v4l2_subdev_audio_ops {
  82. int (*s_clock_freq)(struct v4l2_subdev *sd, u32 freq);
  83. int (*s_i2s_clock_freq)(struct v4l2_subdev *sd, u32 freq);
  84. int (*s_routing)(struct v4l2_subdev *sd, const struct v4l2_routing *route);
  85. };
  86. struct v4l2_subdev_video_ops {
  87. int (*s_routing)(struct v4l2_subdev *sd, const struct v4l2_routing *route);
  88. int (*s_crystal_freq)(struct v4l2_subdev *sd, struct v4l2_crystal_freq *freq);
  89. int (*decode_vbi_line)(struct v4l2_subdev *sd, struct v4l2_decode_vbi_line *vbi_line);
  90. int (*s_vbi_data)(struct v4l2_subdev *sd, const struct v4l2_sliced_vbi_data *vbi_data);
  91. int (*g_vbi_data)(struct v4l2_subdev *sd, struct v4l2_sliced_vbi_data *vbi_data);
  92. int (*g_sliced_vbi_cap)(struct v4l2_subdev *sd, struct v4l2_sliced_vbi_cap *cap);
  93. int (*s_std_output)(struct v4l2_subdev *sd, v4l2_std_id std);
  94. int (*s_stream)(struct v4l2_subdev *sd, int enable);
  95. int (*s_fmt)(struct v4l2_subdev *sd, struct v4l2_format *fmt);
  96. int (*g_fmt)(struct v4l2_subdev *sd, struct v4l2_format *fmt);
  97. };
  98. struct v4l2_subdev_ops {
  99. const struct v4l2_subdev_core_ops *core;
  100. const struct v4l2_subdev_tuner_ops *tuner;
  101. const struct v4l2_subdev_audio_ops *audio;
  102. const struct v4l2_subdev_video_ops *video;
  103. };
  104. #define V4L2_SUBDEV_NAME_SIZE 32
  105. /* Each instance of a subdev driver should create this struct, either
  106. stand-alone or embedded in a larger struct.
  107. */
  108. struct v4l2_subdev {
  109. struct list_head list;
  110. struct module *owner;
  111. struct v4l2_device *dev;
  112. const struct v4l2_subdev_ops *ops;
  113. /* name must be unique */
  114. char name[V4L2_SUBDEV_NAME_SIZE];
  115. /* can be used to group similar subdevs, value is driver-specific */
  116. u32 grp_id;
  117. /* pointer to private data */
  118. void *priv;
  119. };
  120. static inline void v4l2_set_subdevdata(struct v4l2_subdev *sd, void *p)
  121. {
  122. sd->priv = p;
  123. }
  124. static inline void *v4l2_get_subdevdata(const struct v4l2_subdev *sd)
  125. {
  126. return sd->priv;
  127. }
  128. /* Convert an ioctl-type command to the proper v4l2_subdev_ops function call.
  129. This is used by subdev modules that can be called by both old-style ioctl
  130. commands and through the v4l2_subdev_ops.
  131. The ioctl API of the subdev driver can call this function to call the
  132. right ops based on the ioctl cmd and arg.
  133. Once all subdev drivers have been converted and all drivers no longer
  134. use the ioctl interface, then this function can be removed.
  135. */
  136. int v4l2_subdev_command(struct v4l2_subdev *sd, unsigned cmd, void *arg);
  137. static inline void v4l2_subdev_init(struct v4l2_subdev *sd,
  138. const struct v4l2_subdev_ops *ops)
  139. {
  140. INIT_LIST_HEAD(&sd->list);
  141. /* ops->core MUST be set */
  142. BUG_ON(!ops || !ops->core);
  143. sd->ops = ops;
  144. sd->dev = NULL;
  145. sd->name[0] = '\0';
  146. sd->grp_id = 0;
  147. sd->priv = NULL;
  148. }
  149. /* Call an ops of a v4l2_subdev, doing the right checks against
  150. NULL pointers.
  151. Example: err = v4l2_subdev_call(sd, core, g_chip_ident, &chip);
  152. */
  153. #define v4l2_subdev_call(sd, o, f, args...) \
  154. (!(sd) ? -ENODEV : (((sd) && (sd)->ops->o && (sd)->ops->o->f) ? \
  155. (sd)->ops->o->f((sd) , ##args) : -ENOIOCTLCMD))
  156. #endif