Opening and Closing

Our driver can probe for the interface at module load time or at kernel boot. The next step is to assign an address to the interface so the driver can exchange data through it. Opening and closing an interface is performed by 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) sets dev->pa_addr, dev->family, dev->pa_mask, and dev->pa_brdaddr, but 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 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.

One last step is needed if the driver isn’t going to use shared interrupts (for example, if it is to be compatible with older ...

Get Linux Device Drivers now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.