요약·해설과 원문, 전문 번역을 서로 분리했습니다. API 이름, symbol, source path는 원문 표기를 사용합니다.
1. 요약·해설
원문의 핵심 논리와 kernel programming 관점의 보충 설명입니다. 아래의 전문 번역과는 별도로 작성했습니다.
2. 영어 원문 전체
번역 기준이 된 Linux v6.18.37 원문입니다. 줄 번호는 이 버전의 파일 좌표입니다.
원문 전체 펼치기
==============
Driver Binding
==============
Driver binding is the process of associating a device with a device
driver that can control it. Bus drivers have typically handled this
because there have been bus-specific structures to represent the
devices and the drivers. With generic device and device driver
structures, most of the binding can take place using common code.
Bus
~~~
The bus type structure contains a list of all devices that are on that bus
type in the system. When device_register is called for a device, it is
inserted into the end of this list. The bus object also contains a
list of all drivers of that bus type. When driver_register is called
for a driver, it is inserted at the end of this list. These are the
two events which trigger driver binding.
device_register
~~~~~~~~~~~~~~~
When a new device is added, the bus's list of drivers is iterated over
to find one that supports it. In order to determine that, the device
ID of the device must match one of the device IDs that the driver
supports. The format and semantics for comparing IDs is bus-specific.
Instead of trying to derive a complex state machine and matching
algorithm, it is up to the bus driver to provide a callback to compare
a device against the IDs of a driver. The bus returns 1 if a match was
found; 0 otherwise.
int match(struct device * dev, struct device_driver * drv);
If a match is found, the device's driver field is set to the driver
and the driver's probe callback is called. This gives the driver a
chance to verify that it really does support the hardware, and that
it's in a working state.
Device Class
~~~~~~~~~~~~
Upon the successful completion of probe, the device is registered with
the class to which it belongs. Device drivers belong to one and only one
class, and that is set in the driver's devclass field.
devclass_add_device is called to enumerate the device within the class
and actually register it with the class, which happens with the
class's register_dev callback.
Driver
~~~~~~
When a driver is attached to a device, the device is inserted into the
driver's list of devices.
sysfs
~~~~~
A symlink is created in the bus's 'devices' directory that points to
the device's directory in the physical hierarchy.
A symlink is created in the driver's 'devices' directory that points
to the device's directory in the physical hierarchy.
A directory for the device is created in the class's directory. A
symlink is created in that directory that points to the device's
physical location in the sysfs tree.
A symlink can be created (though this isn't done yet) in the device's
physical directory to either its class directory, or the class's
top-level directory. One can also be created to point to its driver's
directory also.
driver_register
~~~~~~~~~~~~~~~
The process is almost identical for when a new driver is added.
The bus's list of devices is iterated over to find a match. Devices
that already have a driver are skipped. All the devices are iterated
over, to bind as many devices as possible to the driver.
Removal
~~~~~~~
When a device is removed, the reference count for it will eventually
go to 0. When it does, the remove callback of the driver is called. It
is removed from the driver's list of devices and the reference count
of the driver is decremented. All symlinks between the two are removed.
When a driver is removed, the list of devices that it supports is
iterated over, and the driver's remove callback is called for each
one. The device is removed from that list and the symlinks removed.
3. 한국어 전문 번역
영어 원문의 문단 순서와 의미를 유지한 전체 번역입니다. 코드, 함수명, symbol과 URL은 원문 표기를 유지합니다.
Driver binding 개요
1-11Driver binding은 device를 그 device를 제어할 수 있는 device driver와 연결하는 과정입니다. 전통적으로 bus별 device·driver 구조체가 있었기 때문에 bus driver가 이 작업을 처리했습니다. generic device와 generic device-driver 구조체를 사용하면 binding의 대부분을 공통 코드에서 수행할 수 있습니다.
device와 driver 등록에서 match와 probe로 이어지는 핵심 흐름입니다.
Bus 목록과 binding trigger
12-22bus type 구조체는 system에서 그 bus type에 속한 모든 device 목록을 가집니다. device에 `device_register`를 호출하면 목록 끝에 삽입됩니다. bus object는 같은 bus type의 모든 driver 목록도 가지며, `driver_register`를 호출한 driver는 그 목록 끝에 삽입됩니다. 이 두 사건이 driver binding을 시작하는 trigger입니다.
device_register와 match·probe
23-41새 device가 추가되면 이를 지원하는 driver를 찾기 위해 bus의 driver 목록을 순회합니다. device ID는 driver가 지원하는 device ID 가운데 하나와 일치해야 하지만 ID 형식과 비교 의미는 bus마다 다릅니다.
복잡한 공통 state machine이나 matching algorithm을 추론하는 대신 bus driver가 device와 driver ID를 비교하는 callback을 제공합니다. match를 찾으면 1, 아니면 0을 반환합니다.
int match(struct device * dev, struct device_driver * drv);
match가 성립하면 device의 `driver` field를 해당 driver로 설정하고 driver의 `probe` callback을 호출합니다. driver는 여기서 실제로 hardware를 지원하는지, hardware가 동작 가능한 상태인지 최종 확인할 수 있습니다.
Device class 등록
42-52probe가 성공하면 device를 자신이 속한 class에 등록합니다. device driver는 정확히 하나의 class에만 속하며 driver의 `devclass` field가 이를 지정합니다. `devclass_add_device`는 class 안에서 device를 열거하고 class의 `register_dev` callback을 통해 실제 등록을 수행합니다.
Driver의 device 목록
53-59driver가 device에 attach되면 그 device를 driver가 관리하는 device 목록에 삽입합니다.
Binding이 만드는 sysfs link
60-78bus의 `devices` directory에는 physical hierarchy에 있는 device directory를 가리키는 symlink를 만듭니다. driver의 `devices` directory에도 같은 physical device directory를 가리키는 symlink를 만듭니다.
class directory에는 device용 directory를 만들고 그 안에 sysfs tree의 physical location을 가리키는 symlink를 둡니다. 아직 구현되지는 않았지만 device의 physical directory에서 class device directory 또는 class top-level directory를 가리키는 symlink, 그리고 driver directory를 가리키는 symlink도 만들 수 있습니다.
bus, driver, class view가 physical device를 어떻게 참조하는지 정리했습니다.
driver_register의 역방향 탐색
79-87새 driver를 추가할 때도 과정은 거의 같습니다. bus의 device 목록을 순회해 match를 찾되 이미 driver가 연결된 device는 건너뜁니다. 가능한 한 많은 device를 새 driver에 bind하기 위해 모든 device를 순회합니다.
Device와 driver 제거
88-98device를 제거하면 그 reference count가 결국 0이 됩니다. 이때 driver의 `remove` callback을 호출하고 device를 driver의 device 목록에서 제거하며 driver reference count를 줄입니다. 두 object 사이의 모든 symlink도 제거합니다.
driver를 제거할 때는 그 driver가 지원하는 device 목록을 순회하면서 각 device에 driver의 `remove` callback을 호출합니다. 각 device를 목록에서 제거하고 관련 symlink도 삭제합니다.
device 제거와 driver 제거가 공유하는 정리 단계를 나타냅니다.
요약과 해설
binding.rst:1-98Driver core는 bus의 device·driver 목록과 bus-specific match callback을 결합해 공통 binding을 수행합니다. match 뒤 probe가 성공하면 class와 sysfs view를 연결하고, unbind 때 remove callback·목록·symlink·reference를 정리합니다.