aboutsummaryrefslogtreecommitdiffstats
path: root/Documentation/video4linux/v4l2-framework.txt
diff options
context:
space:
mode:
Diffstat (limited to 'Documentation/video4linux/v4l2-framework.txt')
-rw-r--r--Documentation/video4linux/v4l2-framework.txt202
1 files changed, 169 insertions, 33 deletions
diff --git a/Documentation/video4linux/v4l2-framework.txt b/Documentation/video4linux/v4l2-framework.txt
index ff124374e9ba..854808b67fae 100644
--- a/Documentation/video4linux/v4l2-framework.txt
+++ b/Documentation/video4linux/v4l2-framework.txt
@@ -47,7 +47,9 @@ All drivers have the following structure:
473) Creating V4L2 device nodes (/dev/videoX, /dev/vbiX, /dev/radioX and 473) Creating V4L2 device nodes (/dev/videoX, /dev/vbiX, /dev/radioX and
48 /dev/vtxX) and keeping track of device-node specific data. 48 /dev/vtxX) and keeping track of device-node specific data.
49 49
504) Filehandle-specific structs containing per-filehandle data. 504) Filehandle-specific structs containing per-filehandle data;
51
525) video buffer handling.
51 53
52This is a rough schematic of how it all relates: 54This is a rough schematic of how it all relates:
53 55
@@ -82,12 +84,20 @@ You must register the device instance:
82 v4l2_device_register(struct device *dev, struct v4l2_device *v4l2_dev); 84 v4l2_device_register(struct device *dev, struct v4l2_device *v4l2_dev);
83 85
84Registration will initialize the v4l2_device struct and link dev->driver_data 86Registration will initialize the v4l2_device struct and link dev->driver_data
85to v4l2_dev. Registration will also set v4l2_dev->name to a value derived from 87to v4l2_dev. If v4l2_dev->name is empty then it will be set to a value derived
86dev (driver name followed by the bus_id, to be precise). You may change the 88from dev (driver name followed by the bus_id, to be precise). If you set it
87name after registration if you want. 89up before calling v4l2_device_register then it will be untouched. If dev is
90NULL, then you *must* setup v4l2_dev->name before calling v4l2_device_register.
88 91
89The first 'dev' argument is normally the struct device pointer of a pci_dev, 92The first 'dev' argument is normally the struct device pointer of a pci_dev,
90usb_device or platform_device. 93usb_interface or platform_device. It is rare for dev to be NULL, but it happens
94with ISA devices or when one device creates multiple PCI devices, thus making
95it impossible to associate v4l2_dev with a particular parent.
96
97You can also supply a notify() callback that can be called by sub-devices to
98notify you of events. Whether you need to set this depends on the sub-device.
99Any notifications a sub-device supports must be defined in a header in
100include/media/<subdevice>.h.
91 101
92You unregister with: 102You unregister with:
93 103
@@ -95,6 +105,17 @@ You unregister with:
95 105
96Unregistering will also automatically unregister all subdevs from the device. 106Unregistering will also automatically unregister all subdevs from the device.
97 107
108If you have a hotpluggable device (e.g. a USB device), then when a disconnect
109happens the parent device becomes invalid. Since v4l2_device has a pointer to
110that parent device it has to be cleared as well to mark that the parent is
111gone. To do this call:
112
113 v4l2_device_disconnect(struct v4l2_device *v4l2_dev);
114
115This does *not* unregister the subdevs, so you still need to call the
116v4l2_device_unregister() function for that. If your driver is not hotpluggable,
117then there is no need to call v4l2_device_disconnect().
118
98Sometimes you need to iterate over all devices registered by a specific 119Sometimes you need to iterate over all devices registered by a specific
99driver. This is usually the case if multiple device drivers use the same 120driver. This is usually the case if multiple device drivers use the same
100hardware. E.g. the ivtvfb driver is a framebuffer driver that uses the ivtv 121hardware. E.g. the ivtvfb driver is a framebuffer driver that uses the ivtv
@@ -134,7 +155,7 @@ The recommended approach is as follows:
134 155
135static atomic_t drv_instance = ATOMIC_INIT(0); 156static atomic_t drv_instance = ATOMIC_INIT(0);
136 157
137static int __devinit drv_probe(struct pci_dev *dev, 158static int __devinit drv_probe(struct pci_dev *pdev,
138 const struct pci_device_id *pci_id) 159 const struct pci_device_id *pci_id)
139{ 160{
140 ... 161 ...
@@ -218,7 +239,7 @@ to add new ops and categories.
218 239
219A sub-device driver initializes the v4l2_subdev struct using: 240A sub-device driver initializes the v4l2_subdev struct using:
220 241
221 v4l2_subdev_init(subdev, &ops); 242 v4l2_subdev_init(sd, &ops);
222 243
223Afterwards you need to initialize subdev->name with a unique name and set the 244Afterwards you need to initialize subdev->name with a unique name and set the
224module owner. This is done for you if you use the i2c helper functions. 245module owner. This is done for you if you use the i2c helper functions.
@@ -226,7 +247,7 @@ module owner. This is done for you if you use the i2c helper functions.
226A device (bridge) driver needs to register the v4l2_subdev with the 247A device (bridge) driver needs to register the v4l2_subdev with the
227v4l2_device: 248v4l2_device:
228 249
229 int err = v4l2_device_register_subdev(device, subdev); 250 int err = v4l2_device_register_subdev(v4l2_dev, sd);
230 251
231This can fail if the subdev module disappeared before it could be registered. 252This can fail if the subdev module disappeared before it could be registered.
232After this function was called successfully the subdev->dev field points to 253After this function was called successfully the subdev->dev field points to
@@ -234,17 +255,17 @@ the v4l2_device.
234 255
235You can unregister a sub-device using: 256You can unregister a sub-device using:
236 257
237 v4l2_device_unregister_subdev(subdev); 258 v4l2_device_unregister_subdev(sd);
238 259
239Afterwards the subdev module can be unloaded and subdev->dev == NULL. 260Afterwards the subdev module can be unloaded and sd->dev == NULL.
240 261
241You can call an ops function either directly: 262You can call an ops function either directly:
242 263
243 err = subdev->ops->core->g_chip_ident(subdev, &chip); 264 err = sd->ops->core->g_chip_ident(sd, &chip);
244 265
245but it is better and easier to use this macro: 266but it is better and easier to use this macro:
246 267
247 err = v4l2_subdev_call(subdev, core, g_chip_ident, &chip); 268 err = v4l2_subdev_call(sd, core, g_chip_ident, &chip);
248 269
249The macro will to the right NULL pointer checks and returns -ENODEV if subdev 270The macro will to the right NULL pointer checks and returns -ENODEV if subdev
250is NULL, -ENOIOCTLCMD if either subdev->core or subdev->core->g_chip_ident is 271is NULL, -ENOIOCTLCMD if either subdev->core or subdev->core->g_chip_ident is
@@ -252,19 +273,19 @@ NULL, or the actual result of the subdev->ops->core->g_chip_ident ops.
252 273
253It is also possible to call all or a subset of the sub-devices: 274It is also possible to call all or a subset of the sub-devices:
254 275
255 v4l2_device_call_all(dev, 0, core, g_chip_ident, &chip); 276 v4l2_device_call_all(v4l2_dev, 0, core, g_chip_ident, &chip);
256 277
257Any subdev that does not support this ops is skipped and error results are 278Any subdev that does not support this ops is skipped and error results are
258ignored. If you want to check for errors use this: 279ignored. If you want to check for errors use this:
259 280
260 err = v4l2_device_call_until_err(dev, 0, core, g_chip_ident, &chip); 281 err = v4l2_device_call_until_err(v4l2_dev, 0, core, g_chip_ident, &chip);
261 282
262Any error except -ENOIOCTLCMD will exit the loop with that error. If no 283Any error except -ENOIOCTLCMD will exit the loop with that error. If no
263errors (except -ENOIOCTLCMD) occured, then 0 is returned. 284errors (except -ENOIOCTLCMD) occured, then 0 is returned.
264 285
265The second argument to both calls is a group ID. If 0, then all subdevs are 286The second argument to both calls is a group ID. If 0, then all subdevs are
266called. If non-zero, then only those whose group ID match that value will 287called. If non-zero, then only those whose group ID match that value will
267be called. Before a bridge driver registers a subdev it can set subdev->grp_id 288be called. Before a bridge driver registers a subdev it can set sd->grp_id
268to whatever value it wants (it's 0 by default). This value is owned by the 289to whatever value it wants (it's 0 by default). This value is owned by the
269bridge driver and the sub-device driver will never modify or use it. 290bridge driver and the sub-device driver will never modify or use it.
270 291
@@ -276,6 +297,11 @@ e.g. AUDIO_CONTROLLER and specify that as the group ID value when calling
276v4l2_device_call_all(). That ensures that it will only go to the subdev 297v4l2_device_call_all(). That ensures that it will only go to the subdev
277that needs it. 298that needs it.
278 299
300If the sub-device needs to notify its v4l2_device parent of an event, then
301it can call v4l2_subdev_notify(sd, notification, arg). This macro checks
302whether there is a notify() callback defined and returns -ENODEV if not.
303Otherwise the result of the notify() call is returned.
304
279The advantage of using v4l2_subdev is that it is a generic struct and does 305The advantage of using v4l2_subdev is that it is a generic struct and does
280not contain any knowledge about the underlying hardware. So a driver might 306not contain any knowledge about the underlying hardware. So a driver might
281contain several subdevs that use an I2C bus, but also a subdev that is 307contain several subdevs that use an I2C bus, but also a subdev that is
@@ -325,32 +351,25 @@ And this to go from an i2c_client to a v4l2_subdev struct:
325 351
326 struct v4l2_subdev *sd = i2c_get_clientdata(client); 352 struct v4l2_subdev *sd = i2c_get_clientdata(client);
327 353
328Finally you need to make a command function to make driver->command()
329call the right subdev_ops functions:
330
331static int subdev_command(struct i2c_client *client, unsigned cmd, void *arg)
332{
333 return v4l2_subdev_command(i2c_get_clientdata(client), cmd, arg);
334}
335
336If driver->command is never used then you can leave this out. Eventually the
337driver->command usage should be removed from v4l.
338
339Make sure to call v4l2_device_unregister_subdev(sd) when the remove() callback 354Make sure to call v4l2_device_unregister_subdev(sd) when the remove() callback
340is called. This will unregister the sub-device from the bridge driver. It is 355is called. This will unregister the sub-device from the bridge driver. It is
341safe to call this even if the sub-device was never registered. 356safe to call this even if the sub-device was never registered.
342 357
358You need to do this because when the bridge driver destroys the i2c adapter
359the remove() callbacks are called of the i2c devices on that adapter.
360After that the corresponding v4l2_subdev structures are invalid, so they
361have to be unregistered first. Calling v4l2_device_unregister_subdev(sd)
362from the remove() callback ensures that this is always done correctly.
363
343 364
344The bridge driver also has some helper functions it can use: 365The bridge driver also has some helper functions it can use:
345 366
346struct v4l2_subdev *sd = v4l2_i2c_new_subdev(adapter, "module_foo", "chipid", 0x36); 367struct v4l2_subdev *sd = v4l2_i2c_new_subdev(v4l2_dev, adapter,
368 "module_foo", "chipid", 0x36);
347 369
348This loads the given module (can be NULL if no module needs to be loaded) and 370This loads the given module (can be NULL if no module needs to be loaded) and
349calls i2c_new_device() with the given i2c_adapter and chip/address arguments. 371calls i2c_new_device() with the given i2c_adapter and chip/address arguments.
350If all goes well, then it registers the subdev with the v4l2_device. It gets 372If all goes well, then it registers the subdev with the v4l2_device.
351the v4l2_device by calling i2c_get_adapdata(adapter), so you should make sure
352that adapdata is set to v4l2_device when you setup the i2c_adapter in your
353driver.
354 373
355You can also use v4l2_i2c_new_probed_subdev() which is very similar to 374You can also use v4l2_i2c_new_probed_subdev() which is very similar to
356v4l2_i2c_new_subdev(), except that it has an array of possible I2C addresses 375v4l2_i2c_new_subdev(), except that it has an array of possible I2C addresses
@@ -358,6 +377,14 @@ that it should probe. Internally it calls i2c_new_probed_device().
358 377
359Both functions return NULL if something went wrong. 378Both functions return NULL if something went wrong.
360 379
380Note that the chipid you pass to v4l2_i2c_new_(probed_)subdev() is usually
381the same as the module name. It allows you to specify a chip variant, e.g.
382"saa7114" or "saa7115". In general though the i2c driver autodetects this.
383The use of chipid is something that needs to be looked at more closely at a
384later date. It differs between i2c drivers and as such can be confusing.
385To see which chip variants are supported you can look in the i2c driver code
386for the i2c_device_id table. This lists all the possibilities.
387
361 388
362struct video_device 389struct video_device
363------------------- 390-------------------
@@ -396,6 +423,15 @@ You should also set these fields:
396- ioctl_ops: if you use the v4l2_ioctl_ops to simplify ioctl maintenance 423- ioctl_ops: if you use the v4l2_ioctl_ops to simplify ioctl maintenance
397 (highly recommended to use this and it might become compulsory in the 424 (highly recommended to use this and it might become compulsory in the
398 future!), then set this to your v4l2_ioctl_ops struct. 425 future!), then set this to your v4l2_ioctl_ops struct.
426- parent: you only set this if v4l2_device was registered with NULL as
427 the parent device struct. This only happens in cases where one hardware
428 device has multiple PCI devices that all share the same v4l2_device core.
429
430 The cx88 driver is an example of this: one core v4l2_device struct, but
431 it is used by both an raw video PCI device (cx8800) and a MPEG PCI device
432 (cx8802). Since the v4l2_device cannot be associated with a particular
433 PCI device it is setup without a parent device. But when the struct
434 video_device is setup you do know which parent PCI device to use.
399 435
400If you use v4l2_ioctl_ops, then you should set either .unlocked_ioctl or 436If you use v4l2_ioctl_ops, then you should set either .unlocked_ioctl or
401.ioctl to video_ioctl2 in your v4l2_file_operations struct. 437.ioctl to video_ioctl2 in your v4l2_file_operations struct.
@@ -499,8 +535,8 @@ There are a few useful helper functions:
499 535
500You can set/get driver private data in the video_device struct using: 536You can set/get driver private data in the video_device struct using:
501 537
502void *video_get_drvdata(struct video_device *dev); 538void *video_get_drvdata(struct video_device *vdev);
503void video_set_drvdata(struct video_device *dev, void *data); 539void video_set_drvdata(struct video_device *vdev, void *data);
504 540
505Note that you can safely call video_set_drvdata() before calling 541Note that you can safely call video_set_drvdata() before calling
506video_register_device(). 542video_register_device().
@@ -519,3 +555,103 @@ void *video_drvdata(struct file *file);
519You can go from a video_device struct to the v4l2_device struct using: 555You can go from a video_device struct to the v4l2_device struct using:
520 556
521struct v4l2_device *v4l2_dev = vdev->v4l2_dev; 557struct v4l2_device *v4l2_dev = vdev->v4l2_dev;
558
559video buffer helper functions
560-----------------------------
561
562The v4l2 core API provides a standard method for dealing with video
563buffers. Those methods allow a driver to implement read(), mmap() and
564overlay() on a consistent way.
565
566There are currently methods for using video buffers on devices that
567supports DMA with scatter/gather method (videobuf-dma-sg), DMA with
568linear access (videobuf-dma-contig), and vmalloced buffers, mostly
569used on USB drivers (videobuf-vmalloc).
570
571Any driver using videobuf should provide operations (callbacks) for
572four handlers:
573
574ops->buf_setup - calculates the size of the video buffers and avoid they
575 to waste more than some maximum limit of RAM;
576ops->buf_prepare - fills the video buffer structs and calls
577 videobuf_iolock() to alloc and prepare mmaped memory;
578ops->buf_queue - advices the driver that another buffer were
579 requested (by read() or by QBUF);
580ops->buf_release - frees any buffer that were allocated.
581
582In order to use it, the driver need to have a code (generally called at
583interrupt context) that will properly handle the buffer request lists,
584announcing that a new buffer were filled.
585
586The irq handling code should handle the videobuf task lists, in order
587to advice videobuf that a new frame were filled, in order to honor to a
588request. The code is generally like this one:
589 if (list_empty(&dma_q->active))
590 return;
591
592 buf = list_entry(dma_q->active.next, struct vbuffer, vb.queue);
593
594 if (!waitqueue_active(&buf->vb.done))
595 return;
596
597 /* Some logic to handle the buf may be needed here */
598
599 list_del(&buf->vb.queue);
600 do_gettimeofday(&buf->vb.ts);
601 wake_up(&buf->vb.done);
602
603Those are the videobuffer functions used on drivers, implemented on
604videobuf-core:
605
606- Videobuf init functions
607 videobuf_queue_sg_init()
608 Initializes the videobuf infrastructure. This function should be
609 called before any other videobuf function on drivers that uses DMA
610 Scatter/Gather buffers.
611
612 videobuf_queue_dma_contig_init
613 Initializes the videobuf infrastructure. This function should be
614 called before any other videobuf function on drivers that need DMA
615 contiguous buffers.
616
617 videobuf_queue_vmalloc_init()
618 Initializes the videobuf infrastructure. This function should be
619 called before any other videobuf function on USB (and other drivers)
620 that need a vmalloced type of videobuf.
621
622- videobuf_iolock()
623 Prepares the videobuf memory for the proper method (read, mmap, overlay).
624
625- videobuf_queue_is_busy()
626 Checks if a videobuf is streaming.
627
628- videobuf_queue_cancel()
629 Stops video handling.
630
631- videobuf_mmap_free()
632 frees mmap buffers.
633
634- videobuf_stop()
635 Stops video handling, ends mmap and frees mmap and other buffers.
636
637- V4L2 api functions. Those functions correspond to VIDIOC_foo ioctls:
638 videobuf_reqbufs(), videobuf_querybuf(), videobuf_qbuf(),
639 videobuf_dqbuf(), videobuf_streamon(), videobuf_streamoff().
640
641- V4L1 api function (corresponds to VIDIOCMBUF ioctl):
642 videobuf_cgmbuf()
643 This function is used to provide backward compatibility with V4L1
644 API.
645
646- Some help functions for read()/poll() operations:
647 videobuf_read_stream()
648 For continuous stream read()
649 videobuf_read_one()
650 For snapshot read()
651 videobuf_poll_stream()
652 polling help function
653
654The better way to understand it is to take a look at vivi driver. One
655of the main reasons for vivi is to be a videobuf usage example. the
656vivi_thread_tick() does the task that the IRQ callback would do on PCI
657drivers (or the irq callback on USB).