views:

145

answers:

1

As I understood after reading the chapter related to The Linux Device Model in the Linux Device Drivers 3rd Edition, when a new device is configured, the kernel (2.6) follows more or less this sequence:

  1. The Device is registered in the driver core (device_register(), what includes device initialization)
  2. A kobject is registered in the device model
  3. It creates an entry in sysfs and provokes a hotplug event
  4. Bus and drivers are checked to see which one matches with the device
  5. Probe
  6. Device is binded to the driver

My main doubt is, in step 1, when is device_register() called and what fields should already be set in the device struct?

Is it called by the bus to which the device is connected? Any example in the code?

Have I misunderstood anything? :)

+1  A: 

PCI hotplug code is going to call pci_do_scan_bus() to go through all slots, see if we find a device/bridge and add them to our device tree :

unsigned int __devinit pci_do_scan_bus(struct pci_bus *bus)   { 
    max = pci_scan_child_bus(bus) //scan bus for all slots and devices in them
    pci_bus_add_devices(bus);  //add what we find
...
}

The fields in struct device are actually filled up as part of call to pci_scan_child_bus(). Here's the call graph (sort of :)):

pci_scan_child_bus > pci_scan_slot (scan for slots on the bus) > pci_scan_single_device > pci_device_add > device_initialize.

Note that device_initialize() is the first part of device_register(). You will see that the fields of struct device are filled up in pci_device_add after the call to device_initialize(). You can find it under drivers/pci/probe.c in the kernel sources. The struct pci_dev will also be filled up which will later be used by the device specific driver.

The actual addition of the kobject to the device hierarchy happens in pci_bus_add_devices. Here's the call graph :

pci_bus_add_devices > pci_bus_add_device > device_add.

As you can see, this call flow completes the second part of the device_register() function.

So, in short, device_register() consists of : 1. Initialize device and 2. Add device. pci_device_add does step 1 and pci_bus_add_device does step 2.

Files of interest are : drivers/pci/{pci.c,bus.c,probe.c}

Bandan