···11Video device creation22=====================3344-The actual device nodes in the /dev directory are created using the55-video_device struct (v4l2-dev.h). This struct can either be allocated66-dynamically or embedded in a larger struct.44+The actual device nodes in the ``/dev`` directory are created using the55+:c:type:`video_device` struct (``v4l2-dev.h``). This struct can either be66+allocated dynamically or embedded in a larger struct.7788-To allocate it dynamically use:88+To allocate it dynamically use :cpp:func:`video_device_alloc`:991010-.. code-block:: none1010+.. code-block:: c11111212 struct video_device *vdev = video_device_alloc();1313···16161717 vdev->release = video_device_release;18181919-If you embed it in a larger struct, then you must set the release()1919+If you embed it in a larger struct, then you must set the ``release()``2020callback to your own function:21212222-.. code-block:: none2222+.. code-block:: c23232424 struct video_device *vdev = &my_vdev->vdev;25252626 vdev->release = my_vdev_release;27272828-The release callback must be set and it is called when the last user2828+The ``release()`` callback must be set and it is called when the last user2929of the video device exits.30303131-The default video_device_release() callback just calls kfree to free the3232-allocated memory.3131+The default :cpp:func:`video_device_release` callback currently3232+just calls ``kfree`` to free the allocated memory.33333434-There is also a video_device_release_empty() function that does nothing3535-(is empty) and can be used if the struct is embedded and there is nothing3636-to do when it is released.3434+There is also a ::cpp:func:`video_device_release_empty` function that does3535+nothing (is empty) and should be used if the struct is embedded and there3636+is nothing to do when it is released.37373838-You should also set these fields:3838+You should also set these fields of :c:type:`video_device`:39394040-- v4l2_dev: must be set to the v4l2_device parent device.4040+- :c:type:`video_device`->v4l2_dev: must be set to the :c:type:`v4l2_device`4141+ parent device.41424242-- name: set to something descriptive and unique.4343+- :c:type:`video_device`->name: set to something descriptive and unique.43444444-- vfl_dir: set this to VFL_DIR_RX for capture devices (VFL_DIR_RX has value 0,4545- so this is normally already the default), set to VFL_DIR_TX for output4646- devices and VFL_DIR_M2M for mem2mem (codec) devices.4545+- :c:type:`video_device`->vfl_dir: set this to ``VFL_DIR_RX`` for capture4646+ devices (``VFL_DIR_RX`` has value 0, so this is normally already the4747+ default), set to ``VFL_DIR_TX`` for output devices and ``VFL_DIR_M2M`` for mem2mem (codec) devices.47484848-- fops: set to the v4l2_file_operations struct.4949+- :c:type:`video_device`->fops: set to the :c:type:`v4l2_file_operations`5050+ struct.49515050-- ioctl_ops: if you use the v4l2_ioctl_ops to simplify ioctl maintenance5151- (highly recommended to use this and it might become compulsory in the5252- future!), then set this to your v4l2_ioctl_ops struct. The vfl_type and5353- vfl_dir fields are used to disable ops that do not match the type/dir5454- combination. E.g. VBI ops are disabled for non-VBI nodes, and output ops5555- are disabled for a capture device. This makes it possible to provide5656- just one v4l2_ioctl_ops struct for both vbi and video nodes.5252+- :c:type:`video_device`->ioctl_ops: if you use the :c:type:`v4l2_ioctl_ops`5353+ to simplify ioctl maintenance (highly recommended to use this and it might5454+ become compulsory in the future!), then set this to your5555+ :c:type:`v4l2_ioctl_ops` struct. The :c:type:`video_device`->vfl_type and5656+ :c:type:`video_device`->vfl_dir fields are used to disable ops that do not5757+ match the type/dir combination. E.g. VBI ops are disabled for non-VBI nodes,5858+ and output ops are disabled for a capture device. This makes it possible to5959+ provide just one :c:type:`v4l2_ioctl_ops struct` for both vbi and6060+ video nodes.57615858-- lock: leave to NULL if you want to do all the locking in the driver.5959- Otherwise you give it a pointer to a struct mutex_lock and before the6060- unlocked_ioctl file operation is called this lock will be taken by the6161- core and released afterwards. See the next section for more details.6262+- :c:type:`video_device`->lock: leave to ``NULL`` if you want to do all the6363+ locking in the driver. Otherwise you give it a pointer to a struct6464+ ``mutex_lock`` and before the :c:type:`video_device`->unlocked_ioctl6565+ file operation is called this lock will be taken by the core and released6666+ afterwards. See the next section for more details.62676363-- queue: a pointer to the struct vb2_queue associated with this device node.6464- If queue is non-NULL, and queue->lock is non-NULL, then queue->lock is6565- used for the queuing ioctls (VIDIOC_REQBUFS, CREATE_BUFS, QBUF, DQBUF,6666- QUERYBUF, PREPARE_BUF, STREAMON and STREAMOFF) instead of the lock above.6767- That way the vb2 queuing framework does not have to wait for other ioctls.6868- This queue pointer is also used by the vb2 helper functions to check for6868+- :c:type:`video_device`->queue: a pointer to the struct :c:type:`vb2_queue`6969+ associated with this device node.7070+ If queue is not ``NULL``, and queue->lock is not ``NULL``, then queue->lock7171+ is used for the queuing ioctls (``VIDIOC_REQBUFS``, ``CREATE_BUFS``,7272+ ``QBUF``, ``DQBUF``, ``QUERYBUF``, ``PREPARE_BUF``, ``STREAMON`` and7373+ ``STREAMOFF``) instead of the lock above.7474+ That way the :ref:`vb2 <vb2_framework>` queuing framework does not have7575+ to wait for other ioctls. This queue pointer is also used by the7676+ :ref:`vb2 <vb2_framework>` helper functions to check for6977 queuing ownership (i.e. is the filehandle calling it allowed to do the7078 operation).71797272-- prio: keeps track of the priorities. Used to implement VIDIOC_G/S_PRIORITY.7373- If left to NULL, then it will use the struct v4l2_prio_state in v4l2_device.7474- If you want to have a separate priority state per (group of) device node(s),7575- then you can point it to your own struct v4l2_prio_state.8080+- :c:type:`video_device`->prio: keeps track of the priorities. Used to8181+ implement ``VIDIOC_G_PRIORITY`` and ``VIDIOC_S_PRIORITY``.8282+ If left to ``NULL``, then it will use the struct :c:type:`v4l2_prio_state`8383+ in :c:type:`v4l2_device`. If you want to have a separate priority state per8484+ (group of) device node(s), then you can point it to your own struct8585+ :c:type:`v4l2_prio_state`.76867777-- dev_parent: you only set this if v4l2_device was registered with NULL as7878- the parent device struct. This only happens in cases where one hardware7979- device has multiple PCI devices that all share the same v4l2_device core.8787+- :c:type:`video_device`->dev_parent: you only set this if v4l2_device was8888+ registered with ``NULL`` as the parent ``device`` struct. This only happens8989+ in cases where one hardware device has multiple PCI devices that all share9090+ the same :c:type:`v4l2_device` core.80918181- The cx88 driver is an example of this: one core v4l2_device struct, but8282- it is used by both a raw video PCI device (cx8800) and a MPEG PCI device8383- (cx8802). Since the v4l2_device cannot be associated with two PCI devices8484- at the same time it is setup without a parent device. But when the struct8585- video_device is initialized you *do* know which parent PCI device to use and8686- so you set dev_device to the correct PCI device.9292+ The cx88 driver is an example of this: one core :c:type:`v4l2_device` struct,9393+ but it is used by both a raw video PCI device (cx8800) and a MPEG PCI device9494+ (cx8802). Since the :c:type:`v4l2_device` cannot be associated with two PCI9595+ devices at the same time it is setup without a parent device. But when the9696+ struct :c:type:`video_device` is initialized you **do** know which parent9797+ PCI device to use and so you set ``dev_device`` to the correct PCI device.87988888-If you use v4l2_ioctl_ops, then you should set .unlocked_ioctl to video_ioctl28989-in your v4l2_file_operations struct.9090-9191-Do not use .ioctl! This is deprecated and will go away in the future.9999+If you use :c:type:`v4l2_ioctl_ops`, then you should set100100+:c:type:`video_device`->unlocked_ioctl to :cpp:func:`video_ioctl2` in your101101+:c:type:`v4l2_file_operations` struct.9210293103In some cases you want to tell the core that a function you had specified in9494-your v4l2_ioctl_ops should be ignored. You can mark such ioctls by calling this9595-function before video_device_register is called:104104+your :c:type:`v4l2_ioctl_ops` should be ignored. You can mark such ioctls by105105+calling this function before :cpp:func:`video_register_device` is called:961069797-.. code-block:: none9898-9999- void v4l2_disable_ioctl(struct video_device *vdev, unsigned int cmd);107107+ :cpp:func:`v4l2_disable_ioctl <v4l2_disable_ioctl>`108108+ (:c:type:`vdev <video_device>`, cmd).100109101110This tends to be needed if based on external factors (e.g. which card is102102-being used) you want to turns off certain features in v4l2_ioctl_ops without103103-having to make a new struct.111111+being used) you want to turns off certain features in :c:type:`v4l2_ioctl_ops`112112+without having to make a new struct.104113105105-The v4l2_file_operations struct is a subset of file_operations. The main106106-difference is that the inode argument is omitted since it is never used.114114+The :c:type:`v4l2_file_operations` struct is a subset of file_operations.115115+The main difference is that the inode argument is omitted since it is never116116+used.107117108118If integration with the media framework is needed, you must initialize the109109-media_entity struct embedded in the video_device struct (entity field) by110110-calling media_entity_pads_init():119119+:c:type:`media_entity` struct embedded in the :c:type:`video_device` struct120120+(entity field) by calling :cpp:func:`media_entity_pads_init`:111121112112-.. code-block:: none122122+.. code-block:: c113123114124 struct media_pad *pad = &my_vdev->pad;115125 int err;···136126------------------137127138128The V4L core provides optional locking services. The main service is the139139-lock field in struct video_device, which is a pointer to a mutex. If you set140140-this pointer, then that will be used by unlocked_ioctl to serialize all ioctls.129129+lock field in struct :c:type:`video_device`, which is a pointer to a mutex.130130+If you set this pointer, then that will be used by unlocked_ioctl to131131+serialize all ioctls.141132142142-If you are using the videobuf2 framework, then there is a second lock that you143143-can set: video_device->queue->lock. If set, then this lock will be used instead144144-of video_device->lock to serialize all queuing ioctls (see the previous section133133+If you are using the :ref:`videobuf2 framework <vb2_framework>`, then there134134+is a second lock that you can set: :c:type:`video_device`->queue->lock. If135135+set, then this lock will be used instead of :c:type:`video_device`->lock136136+to serialize all queuing ioctls (see the previous section145137for the full list of those ioctls).146138147139The advantage of using a different lock for the queuing ioctls is that for some148140drivers (particularly USB drivers) certain commands such as setting controls149141can take a long time, so you want to use a separate lock for the buffer queuing150150-ioctls. That way your VIDIOC_DQBUF doesn't stall because the driver is busy142142+ioctls. That way your ``VIDIOC_DQBUF`` doesn't stall because the driver is busy151143changing the e.g. exposure of the webcam.152144153145Of course, you can always do all the locking yourself by leaving both lock154154-pointers at NULL.146146+pointers at ``NULL``.155147156156-If you use the old videobuf then you must pass the video_device lock to the157157-videobuf queue initialize function: if videobuf has to wait for a frame to158158-arrive, then it will temporarily unlock the lock and relock it afterwards. If159159-your driver also waits in the code, then you should do the same to allow other148148+If you use the old :ref:`videobuf framework <vb_framework>` then you must149149+pass the :c:type:`video_device`->lock to the videobuf queue initialize150150+function: if videobuf has to wait for a frame to arrive, then it will151151+temporarily unlock the lock and relock it afterwards. If your driver also152152+waits in the code, then you should do the same to allow other160153processes to access the device node while the first process is waiting for161154something.162155163163-In the case of videobuf2 you will need to implement the wait_prepare and164164-wait_finish callbacks to unlock/lock if applicable. If you use the queue->lock165165-pointer, then you can use the helper functions vb2_ops_wait_prepare/finish.156156+In the case of :ref:`videobuf2 <vb2_framework>` you will need to implement the157157+``wait_prepare()`` and ``wait_finish()`` callbacks to unlock/lock if applicable.158158+If you use the ``queue->lock`` pointer, then you can use the helper functions159159+:cpp:func:`vb2_ops_wait_prepare` and :cpp:func:`vb2_ops_wait_finish`.166160167161The implementation of a hotplug disconnect should also take the lock from168168-video_device before calling v4l2_device_disconnect. If you are also using169169-video_device->queue->lock, then you have to first lock video_device->queue->lock170170-followed by video_device->lock. That way you can be sure no ioctl is running171171-when you call v4l2_device_disconnect.162162+:c:type:`video_device` before calling v4l2_device_disconnect. If you are also163163+using :c:type:`video_device`->queue->lock, then you have to first lock164164+:c:type:`video_device`->queue->lock followed by :c:type:`video_device`->lock.165165+That way you can be sure no ioctl is running when you call166166+:c:type:`v4l2_device_disconnect`.172167173173-video_device registration168168+Video device registration174169-------------------------175170176176-Next you register the video device: this will create the character device177177-for you.171171+Next you register the video device with :cpp:func:`video_register_device`.172172+This will create the character device for you.178173179179-.. code-block:: none174174+.. code-block:: c180175181176 err = video_register_device(vdev, VFL_TYPE_GRABBER, -1);182177 if (err) {···189174 return err;190175 }191176192192-If the v4l2_device parent device has a non-NULL mdev field, the video device193193-entity will be automatically registered with the media device.177177+If the :c:type:`v4l2_device` parent device has a not ``NULL`` mdev field,178178+the video device entity will be automatically registered with the media179179+device.194180195181Which device is registered depends on the type argument. The following196182types exist:197183198198-VFL_TYPE_GRABBER: videoX for video input/output devices199199-VFL_TYPE_VBI: vbiX for vertical blank data (i.e. closed captions, teletext)200200-VFL_TYPE_RADIO: radioX for radio tuners201201-VFL_TYPE_SDR: swradioX for Software Defined Radio tuners184184+- ``VFL_TYPE_GRABBER``: ``/dev/videoX`` for video input/output devices185185+- ``VFL_TYPE_VBI``: ``/dev/vbiX`` for vertical blank data (i.e. closed captions, teletext)186186+- ``VFL_TYPE_RADIO``: ``/dev/radioX`` for radio tuners187187+- ``VFL_TYPE_SDR``: ``/dev/swradioX`` for Software Defined Radio tuners202188203189The last argument gives you a certain amount of control over the device204204-device node number used (i.e. the X in videoX). Normally you will pass -1190190+device node number used (i.e. the X in ``videoX``). Normally you will pass -1205191to let the v4l2 framework pick the first free number. But sometimes users206192want to select a specific node number. It is common that drivers allow207193the user to select a specific device node number through a driver module···221205222206Since in this case you do not care about a warning about not being able223207to select the specified device node number, you can call the function224224-video_register_device_no_warn() instead.208208+:cpp:func:`video_register_device_no_warn` instead.225209226210Whenever a device node is created some attributes are also created for you.227227-If you look in /sys/class/video4linux you see the devices. Go into e.g.228228-video0 and you will see 'name', 'dev_debug' and 'index' attributes. The 'name'229229-attribute is the 'name' field of the video_device struct. The 'dev_debug' attribute230230-can be used to enable core debugging. See the next section for more detailed231231-information on this.211211+If you look in ``/sys/class/video4linux`` you see the devices. Go into e.g.212212+``video0`` and you will see 'name', 'dev_debug' and 'index' attributes. The213213+'name' attribute is the 'name' field of the video_device struct. The214214+'dev_debug' attribute can be used to enable core debugging. See the next215215+section for more detailed information on this.232216233217The 'index' attribute is the index of the device node: for each call to234234-video_register_device() the index is just increased by 1. The first video235235-device node you register always starts with index 0.218218+:cpp:func:`video_register_device()` the index is just increased by 1. The219219+first video device node you register always starts with index 0.236220237221Users can setup udev rules that utilize the index attribute to make fancy238238-device names (e.g. 'mpegX' for MPEG video capture device nodes).222222+device names (e.g. '``mpegX``' for MPEG video capture device nodes).239223240224After the device was successfully registered, then you can use these fields:241225242242-- vfl_type: the device type passed to video_register_device.243243-- minor: the assigned device minor number.244244-- num: the device node number (i.e. the X in videoX).245245-- index: the device index number.226226+- :c:type:`video_device`->vfl_type: the device type passed to227227+ :cpp:func:`video_register_device`.228228+- :c:type:`video_device`->minor: the assigned device minor number.229229+- :c:type:`video_device`->num: the device node number (i.e. the X in230230+ ``videoX``).231231+- :c:type:`video_device`->index: the device index number.246232247247-If the registration failed, then you need to call video_device_release()248248-to free the allocated video_device struct, or free your own struct if the249249-video_device was embedded in it. The vdev->release() callback will never250250-be called if the registration failed, nor should you ever attempt to251251-unregister the device if the registration failed.233233+If the registration failed, then you need to call234234+:cpp:func:`video_device_release` to free the allocated :c:type:`video_device`235235+struct, or free your own struct if the :c:type:`video_device` was embedded in236236+it. The ``vdev->release()`` callback will never be called if the registration237237+failed, nor should you ever attempt to unregister the device if the238238+registration failed.252239253240video device debugging254241----------------------255242256243The 'dev_debug' attribute that is created for each video, vbi, radio or swradio257257-device in /sys/class/video4linux/<devX>/ allows you to enable logging of244244+device in ``/sys/class/video4linux/<devX>/`` allows you to enable logging of258245file operations.259246260247It is a bitmask and the following bits can be set:261248262262-.. code-block:: none263249264264- 0x01: Log the ioctl name and error code. VIDIOC_(D)QBUF ioctls are only logged265265- if bit 0x08 is also set.266266- 0x02: Log the ioctl name arguments and error code. VIDIOC_(D)QBUF ioctls are267267- only logged if bit 0x08 is also set.268268- 0x04: Log the file operations open, release, read, write, mmap and269269- get_unmapped_area. The read and write operations are only logged if270270- bit 0x08 is also set.271271- 0x08: Log the read and write file operations and the VIDIOC_QBUF and272272- VIDIOC_DQBUF ioctls.273273- 0x10: Log the poll file operation.250250+===== ================================================================251251+Mask Description252252+===== ================================================================253253+0x01 Log the ioctl name and error code. VIDIOC_(D)QBUF ioctls are254254+ only logged if bit 0x08 is also set.255255+0x02 Log the ioctl name arguments and error code. VIDIOC_(D)QBUF256256+ ioctls are257257+ only logged if bit 0x08 is also set.258258+0x04 Log the file operations open, release, read, write, mmap and259259+ get_unmapped_area. The read and write operations are only260260+ logged if bit 0x08 is also set.261261+0x08 Log the read and write file operations and the VIDIOC_QBUF and262262+ VIDIOC_DQBUF ioctls.263263+0x10 Log the poll file operation.264264+===== ================================================================274265275275-video_device cleanup266266+Video device cleanup276267--------------------277268278269When the video device nodes have to be removed, either during the unload279270of the driver or because the USB device was disconnected, then you should280280-unregister them:271271+unregister them with:281272282282-.. code-block:: none283283-284284- video_unregister_device(vdev);273273+ :cpp:func:`video_unregister_device`274274+ (:c:type:`vdev <video_device>`);285275286276This will remove the device nodes from sysfs (causing udev to remove them287287-from /dev).277277+from ``/dev``).288278289289-After video_unregister_device() returns no new opens can be done. However,290290-in the case of USB devices some application might still have one of these291291-device nodes open. So after the unregister all file operations (except279279+After :cpp:func:`video_unregister_device` returns no new opens can be done.280280+However, in the case of USB devices some application might still have one of281281+these device nodes open. So after the unregister all file operations (except292282release, of course) will return an error as well.293283294294-When the last user of the video device node exits, then the vdev->release()284284+When the last user of the video device node exits, then the ``vdev->release()``295285callback is called and you can do the final cleanup there.296286297287Don't forget to cleanup the media entity associated with the video device if298288it has been initialized:299289300300-.. code-block:: none301301-302302- media_entity_cleanup(&vdev->entity);290290+ :cpp:func:`media_entity_cleanup <media_entity_cleanup>`291291+ (&vdev->entity);303292304293This can be done from the release callback.305294···314293315294There are a few useful helper functions:316295317317-- file/video_device private data296296+- file and :c:type:`video_device` private data318297319298You can set/get driver private data in the video_device struct using:320299321321-.. code-block:: none300300+ :cpp:func:`video_get_drvdata <video_get_drvdata>`301301+ (:c:type:`vdev <video_device>`);322302323323- void *video_get_drvdata(struct video_device *vdev);324324- void video_set_drvdata(struct video_device *vdev, void *data);303303+ :cpp:func:`video_set_drvdata <video_set_drvdata>`304304+ (:c:type:`vdev <video_device>`);325305326326-Note that you can safely call video_set_drvdata() before calling327327-video_register_device().306306+Note that you can safely call :cpp:func:`video_set_drvdata` before calling307307+:cpp:func:`video_register_device`.328308329309And this function:330310331331-.. code-block:: none332332-333333- struct video_device *video_devdata(struct file *file);311311+ :cpp:func:`video_devdata <video_devdata>`312312+ (struct file \*file);334313335314returns the video_device belonging to the file struct.336315337337-The video_drvdata function combines video_get_drvdata with video_devdata:316316+The :cpp:func:`video_devdata` function combines :cpp:func:`video_get_drvdata`317317+with :cpp:func:`video_devdata`:338318339339-.. code-block:: none319319+ :cpp:func:`video_drvdata <video_drvdata>`320320+ (struct file \*file);340321341341- void *video_drvdata(struct file *file);322322+You can go from a :c:type:`video_device` struct to the v4l2_device struct using:342323343343-You can go from a video_device struct to the v4l2_device struct using:344344-345345-.. code-block:: none324324+.. code-block:: c346325347326 struct v4l2_device *v4l2_dev = vdev->v4l2_dev;348327349328- Device node name350329351351-The video_device node kernel name can be retrieved using330330+The :c:type:`video_device` node kernel name can be retrieved using:352331353353-.. code-block:: none354354-355355- const char *video_device_node_name(struct video_device *vdev);332332+ :cpp:func:`video_device_node_name <video_device_node_name>`333333+ (:c:type:`vdev <video_device>`);356334357335The name is used as a hint by userspace tools such as udev. The function358336should be used where possible instead of accessing the video_device::num and