diff options
author | Hans Verkuil <hverkuil@xs4all.nl> | 2008-11-29 19:36:58 -0500 |
---|---|---|
committer | Mauro Carvalho Chehab <mchehab@redhat.com> | 2008-12-30 06:38:37 -0500 |
commit | 2a1fcdf08230522bd5024f91da24aaa6e8d81f59 (patch) | |
tree | 03781d767920d0569e6441ff9c74186d50f70a23 /include | |
parent | 07b1747c8d0bb463311f9dd05d4c013765abe2eb (diff) |
V4L/DVB (9820): v4l2: add v4l2_device and v4l2_subdev structs to the v4l2 framework.
Start implementing a proper v4l2 framework as discussed during the
Linux Plumbers Conference 2008.
Introduces v4l2_device (for device instances) and v4l2_subdev (representing
sub-device instances).
Signed-off-by: Hans Verkuil <hverkuil@xs4all.nl>
Reviewed-by: Laurent Pinchart <laurent.pinchart@skynet.be>
Reviewed-by: Guennadi Liakhovetski <g.liakhovetski@gmx.de>
Reviewed-by: Andy Walls <awalls@radix.net>
Reviewed-by: David Brownell <david-b@pacbell.net>
Signed-off-by: Mauro Carvalho Chehab <mchehab@redhat.com>
Diffstat (limited to 'include')
-rw-r--r-- | include/media/v4l2-device.h | 109 | ||||
-rw-r--r-- | include/media/v4l2-subdev.h | 188 |
2 files changed, 297 insertions, 0 deletions
diff --git a/include/media/v4l2-device.h b/include/media/v4l2-device.h new file mode 100644 index 000000000000..97b283a04289 --- /dev/null +++ b/include/media/v4l2-device.h | |||
@@ -0,0 +1,109 @@ | |||
1 | /* | ||
2 | V4L2 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_DEVICE_H | ||
22 | #define _V4L2_DEVICE_H | ||
23 | |||
24 | #include <media/v4l2-subdev.h> | ||
25 | |||
26 | /* Each instance of a V4L2 device should create the v4l2_device struct, | ||
27 | either stand-alone or embedded in a larger struct. | ||
28 | |||
29 | It allows easy access to sub-devices (see v4l2-subdev.h) and provides | ||
30 | basic V4L2 device-level support. | ||
31 | */ | ||
32 | |||
33 | #define V4L2_DEVICE_NAME_SIZE (BUS_ID_SIZE + 16) | ||
34 | |||
35 | struct v4l2_device { | ||
36 | /* dev->driver_data points to this struct */ | ||
37 | struct device *dev; | ||
38 | /* used to keep track of the registered subdevs */ | ||
39 | struct list_head subdevs; | ||
40 | /* lock this struct; can be used by the driver as well if this | ||
41 | struct is embedded into a larger struct. */ | ||
42 | spinlock_t lock; | ||
43 | /* unique device name, by default the driver name + bus ID */ | ||
44 | char name[V4L2_DEVICE_NAME_SIZE]; | ||
45 | }; | ||
46 | |||
47 | /* Initialize v4l2_dev and make dev->driver_data point to v4l2_dev */ | ||
48 | int __must_check v4l2_device_register(struct device *dev, struct v4l2_device *v4l2_dev); | ||
49 | /* Set v4l2_dev->dev->driver_data to NULL and unregister all sub-devices */ | ||
50 | void v4l2_device_unregister(struct v4l2_device *v4l2_dev); | ||
51 | |||
52 | /* Register a subdev with a v4l2 device. While registered the subdev module | ||
53 | is marked as in-use. An error is returned if the module is no longer | ||
54 | loaded when you attempt to register it. */ | ||
55 | int __must_check v4l2_device_register_subdev(struct v4l2_device *dev, struct v4l2_subdev *sd); | ||
56 | /* Unregister a subdev with a v4l2 device. Can also be called if the subdev | ||
57 | wasn't registered. In that case it will do nothing. */ | ||
58 | void v4l2_device_unregister_subdev(struct v4l2_subdev *sd); | ||
59 | |||
60 | /* Iterate over all subdevs. */ | ||
61 | #define v4l2_device_for_each_subdev(sd, dev) \ | ||
62 | list_for_each_entry(sd, &(dev)->subdevs, list) | ||
63 | |||
64 | /* Call the specified callback for all subdevs matching the condition. | ||
65 | Ignore any errors. Note that you cannot add or delete a subdev | ||
66 | while walking the subdevs list. */ | ||
67 | #define __v4l2_device_call_subdevs(dev, cond, o, f, args...) \ | ||
68 | do { \ | ||
69 | struct v4l2_subdev *sd; \ | ||
70 | \ | ||
71 | list_for_each_entry(sd, &(dev)->subdevs, list) \ | ||
72 | if ((cond) && sd->ops->o && sd->ops->o->f) \ | ||
73 | sd->ops->o->f(sd , ##args); \ | ||
74 | } while (0) | ||
75 | |||
76 | /* Call the specified callback for all subdevs matching the condition. | ||
77 | If the callback returns an error other than 0 or -ENOIOCTLCMD, then | ||
78 | return with that error code. Note that you cannot add or delete a | ||
79 | subdev while walking the subdevs list. */ | ||
80 | #define __v4l2_device_call_subdevs_until_err(dev, cond, o, f, args...) \ | ||
81 | ({ \ | ||
82 | struct v4l2_subdev *sd; \ | ||
83 | int err = 0; \ | ||
84 | \ | ||
85 | list_for_each_entry(sd, &(dev)->subdevs, list) { \ | ||
86 | if ((cond) && sd->ops->o && sd->ops->o->f) \ | ||
87 | err = sd->ops->o->f(sd , ##args); \ | ||
88 | if (err && err != -ENOIOCTLCMD) \ | ||
89 | break; \ | ||
90 | } \ | ||
91 | (err == -ENOIOCTLCMD) ? 0 : err; \ | ||
92 | }) | ||
93 | |||
94 | /* Call the specified callback for all subdevs matching grp_id (if 0, then | ||
95 | match them all). Ignore any errors. Note that you cannot add or delete | ||
96 | a subdev while walking the subdevs list. */ | ||
97 | #define v4l2_device_call_all(dev, grp_id, o, f, args...) \ | ||
98 | __v4l2_device_call_subdevs(dev, \ | ||
99 | !(grp_id) || sd->grp_id == (grp_id), o, f , ##args) | ||
100 | |||
101 | /* Call the specified callback for all subdevs matching grp_id (if 0, then | ||
102 | match them all). If the callback returns an error other than 0 or | ||
103 | -ENOIOCTLCMD, then return with that error code. Note that you cannot | ||
104 | add or delete a subdev while walking the subdevs list. */ | ||
105 | #define v4l2_device_call_until_err(dev, grp_id, o, f, args...) \ | ||
106 | __v4l2_device_call_subdevs_until_err(dev, \ | ||
107 | !(grp_id) || sd->grp_id == (grp_id), o, f , ##args) | ||
108 | |||
109 | #endif | ||
diff --git a/include/media/v4l2-subdev.h b/include/media/v4l2-subdev.h new file mode 100644 index 000000000000..bc9e0fbf2822 --- /dev/null +++ b/include/media/v4l2-subdev.h | |||
@@ -0,0 +1,188 @@ | |||
1 | /* | ||
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 | |||
26 | struct v4l2_device; | ||
27 | struct v4l2_subdev; | ||
28 | struct 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 | */ | ||
71 | struct v4l2_subdev_core_ops { | ||
72 | int (*g_chip_ident)(struct v4l2_subdev *sd, struct v4l2_chip_ident *chip); | ||
73 | 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); | ||
81 | int (*querymenu)(struct v4l2_subdev *sd, struct v4l2_querymenu *qm); | ||
82 | int (*ioctl)(struct v4l2_subdev *sd, int cmd, void *arg); | ||
83 | #ifdef CONFIG_VIDEO_ADV_DEBUG | ||
84 | int (*g_register)(struct v4l2_subdev *sd, struct v4l2_register *reg); | ||
85 | int (*s_register)(struct v4l2_subdev *sd, struct v4l2_register *reg); | ||
86 | #endif | ||
87 | }; | ||
88 | |||
89 | struct v4l2_subdev_tuner_ops { | ||
90 | int (*s_mode)(struct v4l2_subdev *sd, enum v4l2_tuner_type); | ||
91 | int (*s_radio)(struct v4l2_subdev *sd); | ||
92 | int (*s_frequency)(struct v4l2_subdev *sd, struct v4l2_frequency *freq); | ||
93 | int (*g_frequency)(struct v4l2_subdev *sd, struct v4l2_frequency *freq); | ||
94 | int (*g_tuner)(struct v4l2_subdev *sd, struct v4l2_tuner *vt); | ||
95 | int (*s_tuner)(struct v4l2_subdev *sd, struct v4l2_tuner *vt); | ||
96 | int (*s_std)(struct v4l2_subdev *sd, v4l2_std_id norm); | ||
97 | int (*s_type_addr)(struct v4l2_subdev *sd, struct tuner_setup *type); | ||
98 | int (*s_config)(struct v4l2_subdev *sd, const struct v4l2_priv_tun_config *config); | ||
99 | }; | ||
100 | |||
101 | struct v4l2_subdev_audio_ops { | ||
102 | int (*s_clock_freq)(struct v4l2_subdev *sd, u32 freq); | ||
103 | int (*s_i2s_clock_freq)(struct v4l2_subdev *sd, u32 freq); | ||
104 | int (*s_routing)(struct v4l2_subdev *sd, const struct v4l2_routing *route); | ||
105 | }; | ||
106 | |||
107 | struct v4l2_subdev_video_ops { | ||
108 | int (*s_routing)(struct v4l2_subdev *sd, const struct v4l2_routing *route); | ||
109 | int (*s_crystal_freq)(struct v4l2_subdev *sd, struct v4l2_crystal_freq *freq); | ||
110 | int (*decode_vbi_line)(struct v4l2_subdev *sd, struct v4l2_decode_vbi_line *vbi_line); | ||
111 | int (*s_vbi_data)(struct v4l2_subdev *sd, const struct v4l2_sliced_vbi_data *vbi_data); | ||
112 | int (*g_vbi_data)(struct v4l2_subdev *sd, struct v4l2_sliced_vbi_data *vbi_data); | ||
113 | int (*s_std_output)(struct v4l2_subdev *sd, v4l2_std_id std); | ||
114 | int (*s_stream)(struct v4l2_subdev *sd, int enable); | ||
115 | int (*s_fmt)(struct v4l2_subdev *sd, struct v4l2_format *fmt); | ||
116 | int (*g_fmt)(struct v4l2_subdev *sd, struct v4l2_format *fmt); | ||
117 | }; | ||
118 | |||
119 | struct v4l2_subdev_ops { | ||
120 | const struct v4l2_subdev_core_ops *core; | ||
121 | const struct v4l2_subdev_tuner_ops *tuner; | ||
122 | const struct v4l2_subdev_audio_ops *audio; | ||
123 | const struct v4l2_subdev_video_ops *video; | ||
124 | }; | ||
125 | |||
126 | #define V4L2_SUBDEV_NAME_SIZE 32 | ||
127 | |||
128 | /* Each instance of a subdev driver should create this struct, either | ||
129 | stand-alone or embedded in a larger struct. | ||
130 | */ | ||
131 | struct v4l2_subdev { | ||
132 | struct list_head list; | ||
133 | struct module *owner; | ||
134 | struct v4l2_device *dev; | ||
135 | const struct v4l2_subdev_ops *ops; | ||
136 | /* name must be unique */ | ||
137 | char name[V4L2_SUBDEV_NAME_SIZE]; | ||
138 | /* can be used to group similar subdevs, value is driver-specific */ | ||
139 | u32 grp_id; | ||
140 | /* pointer to private data */ | ||
141 | void *priv; | ||
142 | }; | ||
143 | |||
144 | static inline void v4l2_set_subdevdata(struct v4l2_subdev *sd, void *p) | ||
145 | { | ||
146 | sd->priv = p; | ||
147 | } | ||
148 | |||
149 | static inline void *v4l2_get_subdevdata(const struct v4l2_subdev *sd) | ||
150 | { | ||
151 | return sd->priv; | ||
152 | } | ||
153 | |||
154 | /* Convert an ioctl-type command to the proper v4l2_subdev_ops function call. | ||
155 | This is used by subdev modules that can be called by both old-style ioctl | ||
156 | commands and through the v4l2_subdev_ops. | ||
157 | |||
158 | The ioctl API of the subdev driver can call this function to call the | ||
159 | right ops based on the ioctl cmd and arg. | ||
160 | |||
161 | Once all subdev drivers have been converted and all drivers no longer | ||
162 | use the ioctl interface, then this function can be removed. | ||
163 | */ | ||
164 | int v4l2_subdev_command(struct v4l2_subdev *sd, unsigned cmd, void *arg); | ||
165 | |||
166 | static inline void v4l2_subdev_init(struct v4l2_subdev *sd, | ||
167 | const struct v4l2_subdev_ops *ops) | ||
168 | { | ||
169 | INIT_LIST_HEAD(&sd->list); | ||
170 | /* ops->core MUST be set */ | ||
171 | BUG_ON(!ops || !ops->core); | ||
172 | sd->ops = ops; | ||
173 | sd->dev = NULL; | ||
174 | sd->name[0] = '\0'; | ||
175 | sd->grp_id = 0; | ||
176 | sd->priv = NULL; | ||
177 | } | ||
178 | |||
179 | /* Call an ops of a v4l2_subdev, doing the right checks against | ||
180 | NULL pointers. | ||
181 | |||
182 | Example: err = v4l2_subdev_call(sd, core, g_chip_ident, &chip); | ||
183 | */ | ||
184 | #define v4l2_subdev_call(sd, o, f, args...) \ | ||
185 | (!(sd) ? -ENODEV : (((sd) && (sd)->ops->o && (sd)->ops->o->f) ? \ | ||
186 | (sd)->ops->o->f((sd) , ##args) : -ENOIOCTLCMD)) | ||
187 | |||
188 | #endif | ||