This chapter introduced the following symbols and header files. The
list of the fields in struct file_operations
and
struct file
is not repeated here.
#include <linux/fs.h>
The “file system” header is the header required for writing device drivers. All the important functions are declared in here.
int register_chrdev(unsigned int major, const char *name, struct file_operations *fops);
Registers a character device driver. If the major number is not 0, it is used unchanged; if the number is 0, then a dynamic number is assigned for this device.
int unregister_chrdev(unsigned int major, const char *name);
Unregisters the driver at unload time. Both major
and the name
string must contain the same values
that were used to register the driver.
kdev_t inode->i_rdev;
The device “number” for the current device is accessible from the
inode
structure.
int MAJOR(kdev_t dev);
,
int MINOR(kdev_t dev);
These macros extract the major and minor numbers from a device item.
kdev_t MKDEV(int major, int minor);
This macro builds a kdev_t
data item from the major
and minor numbers.
SET_MODULE_OWNER(struct file_operations *fops)
This macro sets the owner
field in the given
file_operations
structure.
#include <asm/semaphore.h>
void sema_init (struct semaphore *sem, int val);
Initializes a semaphore to a known value. Mutual exclusion semaphores are usually initialized to a value of 1.
int down_interruptible (struct semaphore *sem);
,
void up (struct semaphore *sem);
Obtains a semaphore (sleeping, if necessary) and releases it, respectively.
#include <asm/segment.h>
,
#include <asm/uaccess.h>
segment.h defines functions related to cross-space copying in all kernels up to and including 2.0. The name was changed to uaccess.h in the 2.1 development series.
unsigned long __copy_from_user (void *to, const void *from, unsigned long count);
,
unsigned long __copy_to_user (void *to, const void *from, unsigned long count);
void memcpy_fromfs(void *to, const void *from, unsigned long count);
,
void memcpy_tofs(void *to, const void *from, unsigned long count);
These functions were used to copy an array of bytes from user space to kernel space and vice versa in version 2.0 of the kernel.
#include <linux/devfs_fs_kernel.h>
,
devfs_handle_t devfs_mk_dir (devfs_handle_t dir, const char *name, void *info);
,
devfs_handle_t devfs_register (devfs_handle_t dir, const char *name, unsigned int flags,
,
unsigned int major, unsigned int minor, umode_t mode, void *ops, void *info);
,
void devfs_unregister (devfs_handle_t de);
These are the basic functions for registering devices with the device filesystem (devfs).