]> asedeno.scripts.mit.edu Git - linux.git/blob - include/media/v4l2-async.h
3489e4ccb29bfbf0457dc8a2954eb3bac569348f
[linux.git] / include / media / v4l2-async.h
1 /*
2  * V4L2 asynchronous subdevice registration API
3  *
4  * Copyright (C) 2012-2013, Guennadi Liakhovetski <g.liakhovetski@gmx.de>
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 version 2 as
8  * published by the Free Software Foundation.
9  */
10
11 #ifndef V4L2_ASYNC_H
12 #define V4L2_ASYNC_H
13
14 #include <linux/list.h>
15 #include <linux/mutex.h>
16
17 struct device;
18 struct device_node;
19 struct v4l2_device;
20 struct v4l2_subdev;
21 struct v4l2_async_notifier;
22
23 /* A random max subdevice number, used to allocate an array on stack */
24 #define V4L2_MAX_SUBDEVS 128U
25
26 /**
27  * enum v4l2_async_match_type - type of asynchronous subdevice logic to be used
28  *      in order to identify a match
29  *
30  * @V4L2_ASYNC_MATCH_CUSTOM: Match will use the logic provided by &struct
31  *      v4l2_async_subdev.match ops
32  * @V4L2_ASYNC_MATCH_DEVNAME: Match will use the device name
33  * @V4L2_ASYNC_MATCH_I2C: Match will check for I2C adapter ID and address
34  * @V4L2_ASYNC_MATCH_FWNODE: Match will use firmware node
35  *
36  * This enum is used by the asyncrhronous sub-device logic to define the
37  * algorithm that will be used to match an asynchronous device.
38  */
39 enum v4l2_async_match_type {
40         V4L2_ASYNC_MATCH_CUSTOM,
41         V4L2_ASYNC_MATCH_DEVNAME,
42         V4L2_ASYNC_MATCH_I2C,
43         V4L2_ASYNC_MATCH_FWNODE,
44 };
45
46 /**
47  * struct v4l2_async_subdev - sub-device descriptor, as known to a bridge
48  *
49  * @match_type: type of match that will be used
50  * @match:      union of per-bus type matching data sets
51  * @match.fwnode:
52  *              pointer to &struct fwnode_handle to be matched.
53  *              Used if @match_type is %V4L2_ASYNC_MATCH_FWNODE.
54  * @match.device_name:
55  *              string containing the device name to be matched.
56  *              Used if @match_type is %V4L2_ASYNC_MATCH_DEVNAME.
57  * @match.i2c:  embedded struct with I2C parameters to be matched.
58  *              Both @match.i2c.adapter_id and @match.i2c.address
59  *              should be matched.
60  *              Used if @match_type is %V4L2_ASYNC_MATCH_I2C.
61  * @match.i2c.adapter_id:
62  *              I2C adapter ID to be matched.
63  *              Used if @match_type is %V4L2_ASYNC_MATCH_I2C.
64  * @match.i2c.address:
65  *              I2C address to be matched.
66  *              Used if @match_type is %V4L2_ASYNC_MATCH_I2C.
67  * @match.custom:
68  *              Driver-specific match criteria.
69  *              Used if @match_type is %V4L2_ASYNC_MATCH_CUSTOM.
70  * @match.custom.match:
71  *              Driver-specific match function to be used if
72  *              %V4L2_ASYNC_MATCH_CUSTOM.
73  * @match.custom.priv:
74  *              Driver-specific private struct with match parameters
75  *              to be used if %V4L2_ASYNC_MATCH_CUSTOM.
76  * @asd_list:   used to add struct v4l2_async_subdev objects to the
77  *              master notifier @asd_list
78  * @list:       used to link struct v4l2_async_subdev objects, waiting to be
79  *              probed, to a notifier->waiting list
80  *
81  * When this struct is used as a member in a driver specific struct,
82  * the driver specific struct shall contain the &struct
83  * v4l2_async_subdev as its first member.
84  */
85 struct v4l2_async_subdev {
86         enum v4l2_async_match_type match_type;
87         union {
88                 struct fwnode_handle *fwnode;
89                 const char *device_name;
90                 struct {
91                         int adapter_id;
92                         unsigned short address;
93                 } i2c;
94                 struct {
95                         bool (*match)(struct device *,
96                                       struct v4l2_async_subdev *);
97                         void *priv;
98                 } custom;
99         } match;
100
101         /* v4l2-async core private: not to be used by drivers */
102         struct list_head list;
103         struct list_head asd_list;
104 };
105
106 /**
107  * struct v4l2_async_notifier_operations - Asynchronous V4L2 notifier operations
108  * @bound:      a subdevice driver has successfully probed one of the subdevices
109  * @complete:   All subdevices have been probed successfully. The complete
110  *              callback is only executed for the root notifier.
111  * @unbind:     a subdevice is leaving
112  */
113 struct v4l2_async_notifier_operations {
114         int (*bound)(struct v4l2_async_notifier *notifier,
115                      struct v4l2_subdev *subdev,
116                      struct v4l2_async_subdev *asd);
117         int (*complete)(struct v4l2_async_notifier *notifier);
118         void (*unbind)(struct v4l2_async_notifier *notifier,
119                        struct v4l2_subdev *subdev,
120                        struct v4l2_async_subdev *asd);
121 };
122
123 /**
124  * struct v4l2_async_notifier - v4l2_device notifier data
125  *
126  * @ops:        notifier operations
127  * @num_subdevs: number of subdevices used in the subdevs array
128  * @max_subdevs: number of subdevices allocated in the subdevs array
129  * @subdevs:    array of pointers to subdevice descriptors
130  * @v4l2_dev:   v4l2_device of the root notifier, NULL otherwise
131  * @sd:         sub-device that registered the notifier, NULL otherwise
132  * @parent:     parent notifier
133  * @asd_list:   master list of struct v4l2_async_subdev, replaces @subdevs
134  * @waiting:    list of struct v4l2_async_subdev, waiting for their drivers
135  * @done:       list of struct v4l2_subdev, already probed
136  * @list:       member in a global list of notifiers
137  */
138 struct v4l2_async_notifier {
139         const struct v4l2_async_notifier_operations *ops;
140         unsigned int num_subdevs;
141         unsigned int max_subdevs;
142         struct v4l2_async_subdev **subdevs;
143         struct v4l2_device *v4l2_dev;
144         struct v4l2_subdev *sd;
145         struct v4l2_async_notifier *parent;
146         struct list_head asd_list;
147         struct list_head waiting;
148         struct list_head done;
149         struct list_head list;
150 };
151
152 /**
153  * v4l2_async_notifier_init - Initialize a notifier.
154  *
155  * @notifier: pointer to &struct v4l2_async_notifier
156  *
157  * This function initializes the notifier @asd_list. It must be called
158  * before the first call to @v4l2_async_notifier_add_subdev.
159  */
160 void v4l2_async_notifier_init(struct v4l2_async_notifier *notifier);
161
162 /**
163  * v4l2_async_notifier_add_subdev - Add an async subdev to the
164  *                              notifier's master asd list.
165  *
166  * @notifier: pointer to &struct v4l2_async_notifier
167  * @asd: pointer to &struct v4l2_async_subdev
168  *
169  * This can be used before registering a notifier to add an
170  * asd to the notifiers @asd_list. If the caller uses this
171  * method to compose an asd list, it must never allocate
172  * or place asd's in the @subdevs array.
173  */
174 int v4l2_async_notifier_add_subdev(struct v4l2_async_notifier *notifier,
175                                    struct v4l2_async_subdev *asd);
176
177 /**
178  * v4l2_async_notifier_add_fwnode_subdev - Allocate and add a fwnode async
179  *                              subdev to the notifier's master asd_list.
180  *
181  * @notifier: pointer to &struct v4l2_async_notifier
182  * @fwnode: fwnode handle of the sub-device to be matched
183  * @asd_struct_size: size of the driver's async sub-device struct, including
184  *                   sizeof(struct v4l2_async_subdev). The &struct
185  *                   v4l2_async_subdev shall be the first member of
186  *                   the driver's async sub-device struct, i.e. both
187  *                   begin at the same memory address.
188  *
189  * This can be used before registering a notifier to add a
190  * fwnode-matched asd to the notifiers master asd_list. If the caller
191  * uses this method to compose an asd list, it must never allocate
192  * or place asd's in the @subdevs array.
193  */
194 struct v4l2_async_subdev *
195 v4l2_async_notifier_add_fwnode_subdev(struct v4l2_async_notifier *notifier,
196                                       struct fwnode_handle *fwnode,
197                                       unsigned int asd_struct_size);
198
199 /**
200  * v4l2_async_notifier_add_i2c_subdev - Allocate and add an i2c async
201  *                              subdev to the notifier's master asd_list.
202  *
203  * @notifier: pointer to &struct v4l2_async_notifier
204  * @adapter_id: I2C adapter ID to be matched
205  * @address: I2C address of sub-device to be matched
206  * @asd_struct_size: size of the driver's async sub-device struct, including
207  *                   sizeof(struct v4l2_async_subdev). The &struct
208  *                   v4l2_async_subdev shall be the first member of
209  *                   the driver's async sub-device struct, i.e. both
210  *                   begin at the same memory address.
211  *
212  * Same as above but for I2C matched sub-devices.
213  */
214 struct v4l2_async_subdev *
215 v4l2_async_notifier_add_i2c_subdev(struct v4l2_async_notifier *notifier,
216                                    int adapter_id, unsigned short address,
217                                    unsigned int asd_struct_size);
218
219 /**
220  * v4l2_async_notifier_add_devname_subdev - Allocate and add a device-name
221  *                              async subdev to the notifier's master asd_list.
222  *
223  * @notifier: pointer to &struct v4l2_async_notifier
224  * @device_name: device name string to be matched
225  * @asd_struct_size: size of the driver's async sub-device struct, including
226  *                   sizeof(struct v4l2_async_subdev). The &struct
227  *                   v4l2_async_subdev shall be the first member of
228  *                   the driver's async sub-device struct, i.e. both
229  *                   begin at the same memory address.
230  *
231  * Same as above but for device-name matched sub-devices.
232  */
233 struct v4l2_async_subdev *
234 v4l2_async_notifier_add_devname_subdev(struct v4l2_async_notifier *notifier,
235                                        const char *device_name,
236                                        unsigned int asd_struct_size);
237
238
239 /**
240  * v4l2_async_notifier_register - registers a subdevice asynchronous notifier
241  *
242  * @v4l2_dev: pointer to &struct v4l2_device
243  * @notifier: pointer to &struct v4l2_async_notifier
244  */
245 int v4l2_async_notifier_register(struct v4l2_device *v4l2_dev,
246                                  struct v4l2_async_notifier *notifier);
247
248 /**
249  * v4l2_async_subdev_notifier_register - registers a subdevice asynchronous
250  *                                       notifier for a sub-device
251  *
252  * @sd: pointer to &struct v4l2_subdev
253  * @notifier: pointer to &struct v4l2_async_notifier
254  */
255 int v4l2_async_subdev_notifier_register(struct v4l2_subdev *sd,
256                                         struct v4l2_async_notifier *notifier);
257
258 /**
259  * v4l2_async_notifier_unregister - unregisters a subdevice asynchronous notifier
260  *
261  * @notifier: pointer to &struct v4l2_async_notifier
262  */
263 void v4l2_async_notifier_unregister(struct v4l2_async_notifier *notifier);
264
265 /**
266  * v4l2_async_notifier_cleanup - clean up notifier resources
267  * @notifier: the notifier the resources of which are to be cleaned up
268  *
269  * Release memory resources related to a notifier, including the async
270  * sub-devices allocated for the purposes of the notifier but not the notifier
271  * itself. The user is responsible for calling this function to clean up the
272  * notifier after calling
273  * @v4l2_async_notifier_add_subdev,
274  * @v4l2_async_notifier_parse_fwnode_endpoints or
275  * @v4l2_fwnode_reference_parse_sensor_common.
276  *
277  * There is no harm from calling v4l2_async_notifier_cleanup in other
278  * cases as long as its memory has been zeroed after it has been
279  * allocated.
280  */
281 void v4l2_async_notifier_cleanup(struct v4l2_async_notifier *notifier);
282
283 /**
284  * v4l2_async_register_subdev - registers a sub-device to the asynchronous
285  *      subdevice framework
286  *
287  * @sd: pointer to &struct v4l2_subdev
288  */
289 int v4l2_async_register_subdev(struct v4l2_subdev *sd);
290
291 /**
292  * v4l2_async_register_subdev_sensor_common - registers a sensor sub-device to
293  *                                            the asynchronous sub-device
294  *                                            framework and parse set up common
295  *                                            sensor related devices
296  *
297  * @sd: pointer to struct &v4l2_subdev
298  *
299  * This function is just like v4l2_async_register_subdev() with the exception
300  * that calling it will also parse firmware interfaces for remote references
301  * using v4l2_async_notifier_parse_fwnode_sensor_common() and registers the
302  * async sub-devices. The sub-device is similarly unregistered by calling
303  * v4l2_async_unregister_subdev().
304  *
305  * While registered, the subdev module is marked as in-use.
306  *
307  * An error is returned if the module is no longer loaded on any attempts
308  * to register it.
309  */
310 int __must_check v4l2_async_register_subdev_sensor_common(
311         struct v4l2_subdev *sd);
312
313 /**
314  * v4l2_async_unregister_subdev - unregisters a sub-device to the asynchronous
315  *      subdevice framework
316  *
317  * @sd: pointer to &struct v4l2_subdev
318  */
319 void v4l2_async_unregister_subdev(struct v4l2_subdev *sd);
320 #endif