The name field says what the bus should be called, and the
match field points to our match function. Right now, the match
function is left alone, always returning 1 whenever the driver core
wants to try to match a driver with a device. This logic will be
modified at a later time.

Then, in the i2c core startup code, the i2c_bus_type is
registered with a call to:

bus_register(&i2c_bus_type);

When the i2c core is shut down, a call is added to unregister
this bus:

bus_unregister(&i2c_bus_type);

When the above code runs, the following tree is created in sysfs:

$ tree /sys/bus/i2c/
/sys/bus/i2c/
|-- devices
'-- drivers

When the i2c core is removed from the system, the above directories
are removed. This is all that is needed to create the i2c bus.

i2c Adapters

An i2c bus by itself is pretty boring. Now, the i2c bus
adapter drivers need to be modified to register themselves with
this bus. To do this, a struct device variable is added to the
struct i2c_adapter structure:

struct i2c_adapter {
.....
struct device dev;
};

A to_i2c_adapter() macro is defined as:

#define to_i2c_adapter(d) container_of(d,
struct i2c_adapter, dev)

This macro is used by the i2c core to get a pointer to a real
i2c_adapter structure whenever the driver core passes it a pointer
to a struct device.

The struct device in the i2c_adapter is a whole variable
declared within the structure, not merely a pointer. This is done
so when the driver core passes a pointer to a struct device, the
i2c code can use the to_i2c_adapter() macro to get a pointer to the
real i2c_adapter structure.

The individual struct i2c_driver variables are declared in
the different i2c bus drivers. For example, in the i2c-piix4.c
driver, there is a variable called piix4_adapter of type struct
i2c_driver. This variable is passed to the i2c core in the
i2c_add_adapter() function, when a PIIX4 adapter is seen by the
i2c-piix4 driver.

In the i2c-piix4.c driver, before i2c_add_adapter() is
called, a pointer to the parent device of the PIIX4 adapter needs
to be saved within the i2c_driver structure. This is done with a
single line of code:

piix4_adapter.dev.parent = &dev->dev;

dev is a pointer to the struct pci_dev that is passed to the
i2c-piix4 driver's PCI probe function; the PIIX4 is a PCI-based
device.

To link the i2c_driver variable to the sysfs tree, the
following lines of code are added to the i2c_add_adapter()
function:

The i2c bus has a number of different drivers that control
access to a wide range of i2c devices that live on the i2c bus.
These drivers are declared with a struct i2c_driver structure.
Within this structure, a struct device_driver variable is added to
allow these drivers to be registered with the driver core:

struct i2c_driver {
.....
struct device_driver driver;
};

And, a to_i2c_driver() macro is defined as:

#define to_i2c_driver(d) container_of(d, struct
i2c_driver, driver)

An i2c driver registers itself with the i2c core in a call to
i2c_add_driver(). To add driver core support for i2c drivers, the
following lines of code are added to this function:

This sets up the driver core structure to have the same name as the
driver and a bus type of i2c_bus_type; the probe and remove
functions are set to local i2c functions. For now, these functions
are declared as:

To remove an i2c driver from the system, the i2c_del_driver()
function is called. In order to remove the i2c driver from the
driver core that was registered with the call to driver_register,
the following line of code is added to this function: