diff options
author | Hans Verkuil <hverkuil@xs4all.nl> | 2008-12-19 08:20:22 -0500 |
---|---|---|
committer | Mauro Carvalho Chehab <mchehab@redhat.com> | 2008-12-30 06:39:26 -0500 |
commit | a47ddf1425554ca0b1e9b16b20a9d631e5daaaa8 (patch) | |
tree | a1573c9fce0ec06dfa04ca148b875a8ce2e7fed3 | |
parent | 806e5b7cfa96195baadc4cc4663266aaac92e22b (diff) |
V4L/DVB (9943): v4l2: document video_device.
Add the missing video_device documentation to v4l2-framework.txt.
Signed-off-by: Hans Verkuil <hverkuil@xs4all.nl>
Signed-off-by: Mauro Carvalho Chehab <mchehab@redhat.com>
-rw-r--r-- | Documentation/video4linux/v4l2-framework.txt | 160 |
1 files changed, 159 insertions, 1 deletions
diff --git a/Documentation/video4linux/v4l2-framework.txt b/Documentation/video4linux/v4l2-framework.txt index 60eaf54e7eff..eeae76c22a93 100644 --- a/Documentation/video4linux/v4l2-framework.txt +++ b/Documentation/video4linux/v4l2-framework.txt | |||
@@ -86,6 +86,9 @@ to v4l2_dev. Registration will also set v4l2_dev->name to a value derived from | |||
86 | dev (driver name followed by the bus_id, to be precise). You may change the | 86 | dev (driver name followed by the bus_id, to be precise). You may change the |
87 | name after registration if you want. | 87 | name after registration if you want. |
88 | 88 | ||
89 | The first 'dev' argument is normally the struct device pointer of a pci_dev, | ||
90 | usb_device or platform_device. | ||
91 | |||
89 | You unregister with: | 92 | You unregister with: |
90 | 93 | ||
91 | v4l2_device_unregister(struct v4l2_device *v4l2_dev); | 94 | v4l2_device_unregister(struct v4l2_device *v4l2_dev); |
@@ -359,4 +362,159 @@ Both functions return NULL if something went wrong. | |||
359 | struct video_device | 362 | struct video_device |
360 | ------------------- | 363 | ------------------- |
361 | 364 | ||
362 | Not yet documented. | 365 | The actual device nodes in the /dev directory are created using the |
366 | video_device struct (v4l2-dev.h). This struct can either be allocated | ||
367 | dynamically or embedded in a larger struct. | ||
368 | |||
369 | To allocate it dynamically use: | ||
370 | |||
371 | struct video_device *vdev = video_device_alloc(); | ||
372 | |||
373 | if (vdev == NULL) | ||
374 | return -ENOMEM; | ||
375 | |||
376 | vdev->release = video_device_release; | ||
377 | |||
378 | If you embed it in a larger struct, then you must set the release() | ||
379 | callback to your own function: | ||
380 | |||
381 | struct video_device *vdev = &my_vdev->vdev; | ||
382 | |||
383 | vdev->release = my_vdev_release; | ||
384 | |||
385 | The release callback must be set and it is called when the last user | ||
386 | of the video device exits. | ||
387 | |||
388 | The default video_device_release() callback just calls kfree to free the | ||
389 | allocated memory. | ||
390 | |||
391 | You should also set these fields: | ||
392 | |||
393 | - parent: set to the parent device (same device as was used to register | ||
394 | v4l2_device). | ||
395 | - name: set to something descriptive and unique. | ||
396 | - fops: set to the file_operations struct. | ||
397 | - ioctl_ops: if you use the v4l2_ioctl_ops to simplify ioctl maintenance | ||
398 | (highly recommended to use this and it might become compulsory in the | ||
399 | future!), then set this to your v4l2_ioctl_ops struct. | ||
400 | |||
401 | If you use v4l2_ioctl_ops, then you should set .unlocked_ioctl to | ||
402 | __video_ioctl2 or .ioctl to video_ioctl2 in your file_operations struct. | ||
403 | |||
404 | |||
405 | video_device registration | ||
406 | ------------------------- | ||
407 | |||
408 | Next you register the video device: this will create the character device | ||
409 | for you. | ||
410 | |||
411 | err = video_register_device(vdev, VFL_TYPE_GRABBER, -1); | ||
412 | if (err) { | ||
413 | video_device_release(vdev); // or kfree(my_vdev); | ||
414 | return err; | ||
415 | } | ||
416 | |||
417 | Which device is registered depends on the type argument. The following | ||
418 | types exist: | ||
419 | |||
420 | VFL_TYPE_GRABBER: videoX for video input/output devices | ||
421 | VFL_TYPE_VBI: vbiX for vertical blank data (i.e. closed captions, teletext) | ||
422 | VFL_TYPE_RADIO: radioX for radio tuners | ||
423 | VFL_TYPE_VTX: vtxX for teletext devices (deprecated, don't use) | ||
424 | |||
425 | The last argument gives you a certain amount of control over the device | ||
426 | kernel number used (i.e. the X in videoX). Normally you will pass -1 to | ||
427 | let the v4l2 framework pick the first free number. But if a driver creates | ||
428 | many devices, then it can be useful to have different video devices in | ||
429 | separate ranges. For example, video capture devices start at 0, video | ||
430 | output devices start at 16. | ||
431 | |||
432 | So you can use the last argument to specify a minimum kernel number and | ||
433 | the v4l2 framework will try to pick the first free number that is equal | ||
434 | or higher to what you passed. If that fails, then it will just pick the | ||
435 | first free number. | ||
436 | |||
437 | Whenever a device node is created some attributes are also created for you. | ||
438 | If you look in /sys/class/video4linux you see the devices. Go into e.g. | ||
439 | video0 and you will see 'name' and 'index' attributes. The 'name' attribute | ||
440 | is the 'name' field of the video_device struct. The 'index' attribute is | ||
441 | a device node index that can be assigned by the driver, or that is calculated | ||
442 | for you. | ||
443 | |||
444 | If you call video_register_device(), then the index is just increased by | ||
445 | 1 for each device node you register. The first video device node you register | ||
446 | always starts off with 0. | ||
447 | |||
448 | Alternatively you can call video_register_device_index() which is identical | ||
449 | to video_register_device(), but with an extra index argument. Here you can | ||
450 | pass a specific index value (between 0 and 31) that should be used. | ||
451 | |||
452 | Users can setup udev rules that utilize the index attribute to make fancy | ||
453 | device names (e.g. 'mpegX' for MPEG video capture device nodes). | ||
454 | |||
455 | After the device was successfully registered, then you can use these fields: | ||
456 | |||
457 | - vfl_type: the device type passed to video_register_device. | ||
458 | - minor: the assigned device minor number. | ||
459 | - num: the device kernel number (i.e. the X in videoX). | ||
460 | - index: the device index number (calculated or set explicitly using | ||
461 | video_register_device_index). | ||
462 | |||
463 | If the registration failed, then you need to call video_device_release() | ||
464 | to free the allocated video_device struct, or free your own struct if the | ||
465 | video_device was embedded in it. The vdev->release() callback will never | ||
466 | be called if the registration failed, nor should you ever attempt to | ||
467 | unregister the device if the registration failed. | ||
468 | |||
469 | |||
470 | video_device cleanup | ||
471 | -------------------- | ||
472 | |||
473 | When the video device nodes have to be removed, either during the unload | ||
474 | of the driver or because the USB device was disconnected, then you should | ||
475 | unregister them: | ||
476 | |||
477 | video_unregister_device(vdev); | ||
478 | |||
479 | This will remove the device nodes from sysfs (causing udev to remove them | ||
480 | from /dev). | ||
481 | |||
482 | After video_unregister_device() returns no new opens can be done. | ||
483 | |||
484 | However, in the case of USB devices some application might still have one | ||
485 | of these device nodes open. You should block all new accesses to read, | ||
486 | write, poll, etc. except possibly for certain ioctl operations like | ||
487 | queueing buffers. | ||
488 | |||
489 | When the last user of the video device node exits, then the vdev->release() | ||
490 | callback is called and you can do the final cleanup there. | ||
491 | |||
492 | |||
493 | video_device helper functions | ||
494 | ----------------------------- | ||
495 | |||
496 | There are a few useful helper functions: | ||
497 | |||
498 | You can set/get driver private data in the video_device struct using: | ||
499 | |||
500 | void *video_get_drvdata(struct video_device *dev); | ||
501 | void video_set_drvdata(struct video_device *dev, void *data); | ||
502 | |||
503 | Note that you can safely call video_set_drvdata() before calling | ||
504 | video_register_device(). | ||
505 | |||
506 | And this function: | ||
507 | |||
508 | struct video_device *video_devdata(struct file *file); | ||
509 | |||
510 | returns the video_device belonging to the file struct. | ||
511 | |||
512 | The final helper function combines video_get_drvdata with | ||
513 | video_devdata: | ||
514 | |||
515 | void *video_drvdata(struct file *file); | ||
516 | |||
517 | You can go from a video_device struct to the v4l2_device struct using: | ||
518 | |||
519 | struct v4l2_device *v4l2_dev = dev_get_drvdata(vdev->parent); | ||
520 | |||