# Device Tree: compatible 목록에서 드라이버가 고르는 정보

v6.18.37 / drivers/of/base.c

Device Tree는 보드에 어떤 장치가 있고 어떤 주소·인터럽트·전원 자원을 사용하는지 설명합니다. 드라이버는 compatible 등으로 자신이 다룰 수 있는 장치를 찾습니다. __of_match_node 전체를 보면 단순히 이름이 같은 첫 항목을 반환하는 것이 아니라 호환성 점수를 비교해 가장 적합한 항목을 고릅니다.

## __of_match_node

```c

static
const struct of_device_id *__of_match_node(const struct of_device_id *matches,
					   const struct device_node *node)
{
	const struct of_device_id *best_match = NULL;
	int score, best_score = 0;

	if (!matches)
		return NULL;

	for (; matches->name[0] || matches->type[0] || matches->compatible[0]; matches++) {
		score = __of_device_is_compatible(node, matches->compatible,
						  matches->type, matches->name);
		if (score > best_score) {
			best_match = matches;
			best_score = score;
		}
	}

	return best_match;
}

```

### 1072행

```c

static

```

static은 이 내부 함수의 C 연결 범위를 현재 소스 파일로 제한합니다. 장치의 정적 주소나 메모리 고정을 뜻하지 않습니다.

### 1073행

```c

const struct of_device_id *__of_match_node(const struct of_device_id *matches,

```

드라이버의 매칭 표를 받아 가장 적합한 항목의 포인터를 반환합니다.

### 1074행

```c

					   const struct device_node *node)

```

비교할 Device Tree 노드를 함께 받습니다.

### 1076행

```c

	const struct of_device_id *best_match = NULL;

```

아직 일치하는 항목을 찾지 못했으므로 최선 후보는 NULL입니다.

### 1077행

```c

	int score, best_score = 0;

```

현재 항목의 점수와 지금까지 얻은 최고 점수를 준비합니다. 0은 아직 유효한 일치가 없다는 기준입니다.

### 1079행

```c

	if (!matches)

```

매칭 표 자체를 제공하지 않았는지 확인합니다.

### 1080행

```c

		return NULL;

```

비교 대상 표가 없으면 일치 결과도 없습니다.

### 1082행

```c

	for (; matches->name[0] || matches->type[0] || matches->compatible[0]; matches++) {

```

name·type·compatible이 모두 빈 종료 항목 전까지 배열을 순회합니다.

### 1083행

```c

		score = __of_device_is_compatible(node, matches->compatible,

```

노드와 현재 항목의 compatible을 기준으로 호환성 점수 계산을 시작합니다.

### 1084행

```c

						  matches->type, matches->name);

```

항목의 type과 name 조건도 점수 계산 함수에 전달합니다.

### 1085행

```c

		if (score > best_score) {

```

지금 후보가 이전 최고 점수보다 더 적합한지 비교합니다. 동점은 교체하지 않습니다.

### 1086행

```c

			best_match = matches;

```

더 적합한 표 항목을 최선 후보로 기억합니다. 구조체 전체를 복사하는 것이 아닙니다.

### 1087행

```c

			best_score = score;

```

새 최고 점수도 저장해 다음 항목과 비교합니다.

### 1091행

```c

	return best_match;

```

선택한 항목을 반환합니다. 끝까지 유효한 일치가 없으면 NULL입니다.

