NEXT UP previous
Next: read Function

write Function

The write function, tdd_write(),is called every time a process uses the write() system call on an open file descriptor associated with one of the device special files belonging to this device driver:

	static int tdd_write(struct mode *inode, struct file *file,
			char *buffer, int count)
	{
		int i, len; 
		struct tdd_buf *ptr;
		TRACE_TXT( "tdd~write")

		/* 1 */
		if (MINOR(inode->i_rdev)!=TDD_WRITE) 
			return -EINVAL;

		/* 2 */
		if ((ptr = kmalloc(sizeof(struct tdd_buf), GFP_KERNEL))==0) 
			return -ENOMEM;

		/* 3 */
		len = count<MAX_BUF?count:MAX_BUF;

		if (verify_area(VERIFY_READ, buffer, len)) 
			return -EFAULT;

		for (i = 0; i<count && i<MAX_BUF; ++i)
		{
			ptr->buffer[i] = get_user_byte(buffer+i); 
			TRACE_CHR("w")
		}
		
		/* 4 */ 
		ptr->
		link = 0;

		if (qhead==0)
			qhead = ptr; 
		else
			qtail->link = ptr;

		qtail = ptr; 
		TRACE_CHR( "\n")

		/* 5 */
		ptr->buf_size = i; 
		return i;
	}

The third and fourth parameters to tdd_write() are the buffer and character count passed by the user process into the write() system call. The contents of this buffer need to be copied into the device driver's internal linked list of messages. Remember, though, that you cannot access the user space memory directly via the buffer parameter to tdd_write(); you have to use one of the special data transfer functions for that job. The numbered comments in the listing are as follows:

  1. Check and ensure that only the process with /dev/tddw open writes messages.

  2. Allocate a block of kernel memory large enough for a single message. Check to make sure there are no problems and return an error if necessary.

  3. Verify that the kernel can read from the specified memory block, then copy count or BUF_MAX characters, whichever is the smaller, from user space into the kernel allocated message space.

  4. Link the new message structure on to the end of the linked list in the device driver.

  5. Set up the actual message length in the message structure and also return this value from tdd_write(). This value will also become the return value to the calling process from the write() system call.

NEXT UP previous
Next: read Function