diff options
Diffstat (limited to 'Documentation/video4linux/v4l2-framework.txt')
-rw-r--r-- | Documentation/video4linux/v4l2-framework.txt | 202 |
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: | |||
47 | 3) Creating V4L2 device nodes (/dev/videoX, /dev/vbiX, /dev/radioX and | 47 | 3) 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 | ||
50 | 4) Filehandle-specific structs containing per-filehandle data. | 50 | 4) Filehandle-specific structs containing per-filehandle data; |
51 | |||
52 | 5) video buffer handling. | ||
51 | 53 | ||
52 | This is a rough schematic of how it all relates: | 54 | This 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 | ||
84 | Registration will initialize the v4l2_device struct and link dev->driver_data | 86 | Registration will initialize the v4l2_device struct and link dev->driver_data |
85 | to v4l2_dev. Registration will also set v4l2_dev->name to a value derived from | 87 | to v4l2_dev. If v4l2_dev->name is empty then it will be set to a value derived |
86 | dev (driver name followed by the bus_id, to be precise). You may change the | 88 | from dev (driver name followed by the bus_id, to be precise). If you set it |
87 | name after registration if you want. | 89 | up before calling v4l2_device_register then it will be untouched. If dev is |
90 | NULL, then you *must* setup v4l2_dev->name before calling v4l2_device_register. | ||
88 | 91 | ||
89 | The first 'dev' argument is normally the struct device pointer of a pci_dev, | 92 | The first 'dev' argument is normally the struct device pointer of a pci_dev, |
90 | usb_device or platform_device. | 93 | usb_interface or platform_device. It is rare for dev to be NULL, but it happens |
94 | with ISA devices or when one device creates multiple PCI devices, thus making | ||
95 | it impossible to associate v4l2_dev with a particular parent. | ||
96 | |||
97 | You can also supply a notify() callback that can be called by sub-devices to | ||
98 | notify you of events. Whether you need to set this depends on the sub-device. | ||
99 | Any notifications a sub-device supports must be defined in a header in | ||
100 | include/media/<subdevice>.h. | ||
91 | 101 | ||
92 | You unregister with: | 102 | You unregister with: |
93 | 103 | ||
@@ -95,6 +105,17 @@ You unregister with: | |||
95 | 105 | ||
96 | Unregistering will also automatically unregister all subdevs from the device. | 106 | Unregistering will also automatically unregister all subdevs from the device. |
97 | 107 | ||
108 | If you have a hotpluggable device (e.g. a USB device), then when a disconnect | ||
109 | happens the parent device becomes invalid. Since v4l2_device has a pointer to | ||
110 | that parent device it has to be cleared as well to mark that the parent is | ||
111 | gone. To do this call: | ||
112 | |||
113 | v4l2_device_disconnect(struct v4l2_device *v4l2_dev); | ||
114 | |||
115 | This does *not* unregister the subdevs, so you still need to call the | ||
116 | v4l2_device_unregister() function for that. If your driver is not hotpluggable, | ||
117 | then there is no need to call v4l2_device_disconnect(). | ||
118 | |||
98 | Sometimes you need to iterate over all devices registered by a specific | 119 | Sometimes you need to iterate over all devices registered by a specific |
99 | driver. This is usually the case if multiple device drivers use the same | 120 | driver. This is usually the case if multiple device drivers use the same |
100 | hardware. E.g. the ivtvfb driver is a framebuffer driver that uses the ivtv | 121 | hardware. 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 | ||
135 | static atomic_t drv_instance = ATOMIC_INIT(0); | 156 | static atomic_t drv_instance = ATOMIC_INIT(0); |
136 | 157 | ||
137 | static int __devinit drv_probe(struct pci_dev *dev, | 158 | static 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 | ||
219 | A sub-device driver initializes the v4l2_subdev struct using: | 240 | A 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 | ||
223 | Afterwards you need to initialize subdev->name with a unique name and set the | 244 | Afterwards you need to initialize subdev->name with a unique name and set the |
224 | module owner. This is done for you if you use the i2c helper functions. | 245 | module 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. | |||
226 | A device (bridge) driver needs to register the v4l2_subdev with the | 247 | A device (bridge) driver needs to register the v4l2_subdev with the |
227 | v4l2_device: | 248 | v4l2_device: |
228 | 249 | ||
229 | int err = v4l2_device_register_subdev(device, subdev); | 250 | int err = v4l2_device_register_subdev(v4l2_dev, sd); |
230 | 251 | ||
231 | This can fail if the subdev module disappeared before it could be registered. | 252 | This can fail if the subdev module disappeared before it could be registered. |
232 | After this function was called successfully the subdev->dev field points to | 253 | After this function was called successfully the subdev->dev field points to |
@@ -234,17 +255,17 @@ the v4l2_device. | |||
234 | 255 | ||
235 | You can unregister a sub-device using: | 256 | You can unregister a sub-device using: |
236 | 257 | ||
237 | v4l2_device_unregister_subdev(subdev); | 258 | v4l2_device_unregister_subdev(sd); |
238 | 259 | ||
239 | Afterwards the subdev module can be unloaded and subdev->dev == NULL. | 260 | Afterwards the subdev module can be unloaded and sd->dev == NULL. |
240 | 261 | ||
241 | You can call an ops function either directly: | 262 | You 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 | ||
245 | but it is better and easier to use this macro: | 266 | but 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 | ||
249 | The macro will to the right NULL pointer checks and returns -ENODEV if subdev | 270 | The macro will to the right NULL pointer checks and returns -ENODEV if subdev |
250 | is NULL, -ENOIOCTLCMD if either subdev->core or subdev->core->g_chip_ident is | 271 | is 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 | ||
253 | It is also possible to call all or a subset of the sub-devices: | 274 | It 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 | ||
257 | Any subdev that does not support this ops is skipped and error results are | 278 | Any subdev that does not support this ops is skipped and error results are |
258 | ignored. If you want to check for errors use this: | 279 | ignored. 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 | ||
262 | Any error except -ENOIOCTLCMD will exit the loop with that error. If no | 283 | Any error except -ENOIOCTLCMD will exit the loop with that error. If no |
263 | errors (except -ENOIOCTLCMD) occured, then 0 is returned. | 284 | errors (except -ENOIOCTLCMD) occured, then 0 is returned. |
264 | 285 | ||
265 | The second argument to both calls is a group ID. If 0, then all subdevs are | 286 | The second argument to both calls is a group ID. If 0, then all subdevs are |
266 | called. If non-zero, then only those whose group ID match that value will | 287 | called. If non-zero, then only those whose group ID match that value will |
267 | be called. Before a bridge driver registers a subdev it can set subdev->grp_id | 288 | be called. Before a bridge driver registers a subdev it can set sd->grp_id |
268 | to whatever value it wants (it's 0 by default). This value is owned by the | 289 | to whatever value it wants (it's 0 by default). This value is owned by the |
269 | bridge driver and the sub-device driver will never modify or use it. | 290 | bridge 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 | |||
276 | v4l2_device_call_all(). That ensures that it will only go to the subdev | 297 | v4l2_device_call_all(). That ensures that it will only go to the subdev |
277 | that needs it. | 298 | that needs it. |
278 | 299 | ||
300 | If the sub-device needs to notify its v4l2_device parent of an event, then | ||
301 | it can call v4l2_subdev_notify(sd, notification, arg). This macro checks | ||
302 | whether there is a notify() callback defined and returns -ENODEV if not. | ||
303 | Otherwise the result of the notify() call is returned. | ||
304 | |||
279 | The advantage of using v4l2_subdev is that it is a generic struct and does | 305 | The advantage of using v4l2_subdev is that it is a generic struct and does |
280 | not contain any knowledge about the underlying hardware. So a driver might | 306 | not contain any knowledge about the underlying hardware. So a driver might |
281 | contain several subdevs that use an I2C bus, but also a subdev that is | 307 | contain 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 | ||
328 | Finally you need to make a command function to make driver->command() | ||
329 | call the right subdev_ops functions: | ||
330 | |||
331 | static 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 | |||
336 | If driver->command is never used then you can leave this out. Eventually the | ||
337 | driver->command usage should be removed from v4l. | ||
338 | |||
339 | Make sure to call v4l2_device_unregister_subdev(sd) when the remove() callback | 354 | Make sure to call v4l2_device_unregister_subdev(sd) when the remove() callback |
340 | is called. This will unregister the sub-device from the bridge driver. It is | 355 | is called. This will unregister the sub-device from the bridge driver. It is |
341 | safe to call this even if the sub-device was never registered. | 356 | safe to call this even if the sub-device was never registered. |
342 | 357 | ||
358 | You need to do this because when the bridge driver destroys the i2c adapter | ||
359 | the remove() callbacks are called of the i2c devices on that adapter. | ||
360 | After that the corresponding v4l2_subdev structures are invalid, so they | ||
361 | have to be unregistered first. Calling v4l2_device_unregister_subdev(sd) | ||
362 | from the remove() callback ensures that this is always done correctly. | ||
363 | |||
343 | 364 | ||
344 | The bridge driver also has some helper functions it can use: | 365 | The bridge driver also has some helper functions it can use: |
345 | 366 | ||
346 | struct v4l2_subdev *sd = v4l2_i2c_new_subdev(adapter, "module_foo", "chipid", 0x36); | 367 | struct v4l2_subdev *sd = v4l2_i2c_new_subdev(v4l2_dev, adapter, |
368 | "module_foo", "chipid", 0x36); | ||
347 | 369 | ||
348 | This loads the given module (can be NULL if no module needs to be loaded) and | 370 | This loads the given module (can be NULL if no module needs to be loaded) and |
349 | calls i2c_new_device() with the given i2c_adapter and chip/address arguments. | 371 | calls i2c_new_device() with the given i2c_adapter and chip/address arguments. |
350 | If all goes well, then it registers the subdev with the v4l2_device. It gets | 372 | If all goes well, then it registers the subdev with the v4l2_device. |
351 | the v4l2_device by calling i2c_get_adapdata(adapter), so you should make sure | ||
352 | that adapdata is set to v4l2_device when you setup the i2c_adapter in your | ||
353 | driver. | ||
354 | 373 | ||
355 | You can also use v4l2_i2c_new_probed_subdev() which is very similar to | 374 | You can also use v4l2_i2c_new_probed_subdev() which is very similar to |
356 | v4l2_i2c_new_subdev(), except that it has an array of possible I2C addresses | 375 | v4l2_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 | ||
359 | Both functions return NULL if something went wrong. | 378 | Both functions return NULL if something went wrong. |
360 | 379 | ||
380 | Note that the chipid you pass to v4l2_i2c_new_(probed_)subdev() is usually | ||
381 | the 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. | ||
383 | The use of chipid is something that needs to be looked at more closely at a | ||
384 | later date. It differs between i2c drivers and as such can be confusing. | ||
385 | To see which chip variants are supported you can look in the i2c driver code | ||
386 | for the i2c_device_id table. This lists all the possibilities. | ||
387 | |||
361 | 388 | ||
362 | struct video_device | 389 | struct 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 | ||
400 | If you use v4l2_ioctl_ops, then you should set either .unlocked_ioctl or | 436 | If 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 | ||
500 | You can set/get driver private data in the video_device struct using: | 536 | You can set/get driver private data in the video_device struct using: |
501 | 537 | ||
502 | void *video_get_drvdata(struct video_device *dev); | 538 | void *video_get_drvdata(struct video_device *vdev); |
503 | void video_set_drvdata(struct video_device *dev, void *data); | 539 | void video_set_drvdata(struct video_device *vdev, void *data); |
504 | 540 | ||
505 | Note that you can safely call video_set_drvdata() before calling | 541 | Note that you can safely call video_set_drvdata() before calling |
506 | video_register_device(). | 542 | video_register_device(). |
@@ -519,3 +555,103 @@ void *video_drvdata(struct file *file); | |||
519 | You can go from a video_device struct to the v4l2_device struct using: | 555 | You can go from a video_device struct to the v4l2_device struct using: |
520 | 556 | ||
521 | struct v4l2_device *v4l2_dev = vdev->v4l2_dev; | 557 | struct v4l2_device *v4l2_dev = vdev->v4l2_dev; |
558 | |||
559 | video buffer helper functions | ||
560 | ----------------------------- | ||
561 | |||
562 | The v4l2 core API provides a standard method for dealing with video | ||
563 | buffers. Those methods allow a driver to implement read(), mmap() and | ||
564 | overlay() on a consistent way. | ||
565 | |||
566 | There are currently methods for using video buffers on devices that | ||
567 | supports DMA with scatter/gather method (videobuf-dma-sg), DMA with | ||
568 | linear access (videobuf-dma-contig), and vmalloced buffers, mostly | ||
569 | used on USB drivers (videobuf-vmalloc). | ||
570 | |||
571 | Any driver using videobuf should provide operations (callbacks) for | ||
572 | four handlers: | ||
573 | |||
574 | ops->buf_setup - calculates the size of the video buffers and avoid they | ||
575 | to waste more than some maximum limit of RAM; | ||
576 | ops->buf_prepare - fills the video buffer structs and calls | ||
577 | videobuf_iolock() to alloc and prepare mmaped memory; | ||
578 | ops->buf_queue - advices the driver that another buffer were | ||
579 | requested (by read() or by QBUF); | ||
580 | ops->buf_release - frees any buffer that were allocated. | ||
581 | |||
582 | In order to use it, the driver need to have a code (generally called at | ||
583 | interrupt context) that will properly handle the buffer request lists, | ||
584 | announcing that a new buffer were filled. | ||
585 | |||
586 | The irq handling code should handle the videobuf task lists, in order | ||
587 | to advice videobuf that a new frame were filled, in order to honor to a | ||
588 | request. 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 | |||
603 | Those are the videobuffer functions used on drivers, implemented on | ||
604 | videobuf-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 | |||
654 | The better way to understand it is to take a look at vivi driver. One | ||
655 | of the main reasons for vivi is to be a videobuf usage example. the | ||
656 | vivi_thread_tick() does the task that the IRQ callback would do on PCI | ||
657 | drivers (or the irq callback on USB). | ||