NEXT UP previous
Next: release Function

open Function

The device driver's open function (tdd_open()) is called whenever a open() system call is performed on one of the two device special files associated with this driver:

	static int tdd_open(struct mode *inode, struct file *file)
	{
		TRACE_TXT ( "tdd_open")
		/* 1 */
		switch (MIN0R(inode->i_rdev))
		{
			/* 2 */
			case TDD_WRITE:
				if (write_busy) 
					return -EBUSY;
				else
					write_busy = TRUE; 
					return 0;

			/* 3 */
			case TDD_READ:
				if (read_busy) 
					return -EBUSY;

				else
					read_busy = TRUE;
					return 0;
			default:
				return -ENXIO;
		}
	}

If there were any hardware involved with the device driver then this routine would arrange to bring it into service. As it is, our routine just manages the semantic requirement that the associated device special files may only be opened once each, as follows:

  1. The first task is to pick out the minor device number for the device special file associated with this open() call. This can be extracted by the MINOR() macro from the i_rdev field in the structure pointed to by the mode parameter.

  2. If the device special file is TDD_WRITE (i.e. /dev/tddr) then the write_busy flag is checked to see if this device is already open. If it is then an EBUSY error is returned; otherwise the flag is set to TRUE to busy the device against further open() calls and a zero value is returned, indicating no errors.

  3. Similarly, if this is an open() request for /dev/tddr (using TDD_RFAD) then the read_busy flag is checked and, if necessary, set to ensure exclusive access to the device special file.

NEXT UP previous
Next: release Function