Opening and Closing
Our driver can probe for the interface at module load time or at kernel boot. Before the interface can carry packets, however, the kernel must open it and assign an address to it. The kernel will open or close an interface in response to the ifconfig command.
When ifconfig is used to assign an address
to the interface, it performs two tasks. First, it assigns the
address by means of ioctl(SIOCSIFADDR) (Socket I/O
Control Set Interface Address). Then it sets the
IFF_UP bit in dev->flag by
means of ioctl(SIOCSIFFLAGS) (Socket I/O Control
Set Interface Flags) to turn the interface on.
As far as the device is concerned,
ioctl(SIOCSIFADDR) does nothing. No driver function
is invoked—the task is device independent, and the kernel
performs it. The latter command
(ioctl(SIOCSIFFLAGS)), though, calls the
open method for the device.
Similarly, when the interface is shut down,
ifconfig uses
ioctl(SIOCSIFFLAGS) to clear
IFF_UP, and the stop method is
called.
Both device methods return 0 in case of success and the usual negative value in case of error.
As far as the actual code is concerned, the driver has to perform many of the same tasks as the char and block drivers do. open requests any system resources it needs and tells the interface to come up; stop shuts down the interface and releases system resources. There are a couple of additional steps to be performed, however.
First, the hardware address needs to be copied from the hardware
device to dev->dev_addr ...