3. Making and Accessing the Video Device

3.1. Device Filesystem

Devfs, or 'device filesystem', has been an option in the Linux kernel since the late 2.2 series. If you haven't used it up to this point, then I would suggest you at least consider it. It can immensely simplify device management. Devfsd, the device filesystem daemon, creates and removes devices on your system dynamically without the need for user input. You can tell if your system is running devfsd if you run ls -f /dev/ at the command line or in an xterm, and see mostly symlinks pointing to a device file, or "node" within a logically ordered hierarchy of subdirectories within /dev. You can also see devfsd running when you check running programs with ps -A at the command line. The daemon consumes very little memory. If you are running devfsd/devfs, you can probably skip the following sections as the archaic process of creating device nodes will be done for you, and it's simply a matter of finding the device by selecting the appropriate symlink in /dev.

If you want to give devfs a try, you need to enable '/dev file system' and 'Automatically mount at boot' in the 'File Systems' section of kernel configuration. Yes, it absolutely requires a recompile of your kernel if you don't have it in there already. WARNING: The only other supporting package required is 'devfsd', which you can obtain from your distribution vendor. WARNING: If you enable devicefs to automatically mount at boot-time without installing the devfs daemon, you will be left with an unbootable system!

Devfs does not obviate the need to change permissions of devices for access by users.

Naturally, the above comments about devfs are the opinion of your humble author (among others) and should be treated as just that, especially if you are a relative newbie or are not quite ready to compile your own kernel.

Beginning in the 2.6 series kernel devfs has been deprecated in favor of a userspace (i.e., non-kernel) daemon known as udev, though devfs remains as an option. You can find information on udev here if you are inclined to live on the bleeding edge.

3.2. Creating Video Devices Manually

If you aren't running devfs and devfsd, this is how it will need to be done, unless you are using libusb or udev in which case you can skip this step. A device can be created as a block (such as a drive), a fifo (file-in-file-out or pipe, as in xconsole) or a character device, which represents other hardware. Each device has a major and a minor number "coordinate" to tell the kernel what it is and where to access it. These numbers are not arbitrary.

Video4linux device nodes are used to access video devices (including webcams) and have the major number 81 and minor number 0, 1, 2, etc... First, check /dev to see what directory your distribution lays out its video devices in. Some distributions might have the video device(s) in the root /dev directory, such as /dev/video0, /dev/video1...and so on. Others might place them within /dev/v4l or in /dev/video. If you find that the video devices are already present (made by your distributor or devfsd) then your work is done except for possibly permissions. If not, you will need to create the device nodes yourself. You can use the following script, which I have borrowed from the kernel source (located in linux/Documentation/video4linux/bttv/MAKEDEV of the source tree):

		#!/bin/bash

		function makedev () {

		        for dev in 0 1 2 3; do
	        	echo "/dev/$1$dev: char 81 $[ $2 + $dev ]"
		        rm -f /dev/$1$dev
	        	mknod /dev/$1$dev c 81 $[ $2 + $dev ]
		        chmod 666 /dev/$1$dev
	        done
 
	 	# symlink for default device
			rm -f /dev/$1
			ln -s /dev/${1}0 /dev/$1
		}

		# see http://roadrunner.swansea.uk.linux.org/v4lapi.shtml
			echo "*** new device names ***"
			makedev video 0
			makedev radio 64
			makedev vtx 192
			makedev vbi 224
		# "*** old device names (for compatibility only) ***"
		#makedev bttv 0
		#makedev bttv-fm 64
		#makedev bttv-vbi 224
	

Simply copy and paste the above into your favorite editing program, save it as MAKEDEV or whatever name you like and then make it executable (i.e., chmod u+x MAKEDEV). Next, execute it as root:

		# ./MAKEDEV
	

3.3. Groups and Permissions

It is a good idea to be sure that your user account can access the device once all modules are loaded and device nodes created. The most security-conscious way to do that is to add access for a particular group. On my system, the members of the group 'video' are allowed to use the webcam, scanner and other photographic devices. The way to accomplish this is to first change the ownership of the devices in /dev like so (as root):


		# chown root.video /dev/usb/video1*

	

...where root.video are the owner and group the device will now belong to. Obviously, the specific command will vary by your system and the type of device. It is important that you change the ownership of the device node itself and not the symlink; symlinks' ownerships are affected only by changing the parent devices or files they point to.

To see if your user account is a member of the group in question, as root issue the following command: grep -e video /etc/group. You should see something like the following:

		video:x:44:
	

...where '44' is the group number. Since no members follow the last colon in the 'video' group, we can add them, let's say user 'jhs' with the command

		# adduser jhs  video
	

After this, it's simply a matter of allowing read and write access for the user in question of the device like so:

		# chmod g+rw /dev/v4l/video0
	

...where g+rw means add read and write access for group. See the documentation for chmod (man chmod or info chmod) for further info.