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