[COMMON] thermal: of_thermal: Fix SVACE(WGID:82320) bug
authorSoomin Kim <sm8326.kim@samsung.com>
Mon, 2 Jan 2017 04:10:38 +0000 (13:10 +0900)
committerChungwoo Park <cww.park@samsung.com>
Mon, 21 May 2018 08:18:08 +0000 (17:18 +0900)
Uninitialized variable,'level', is used. Also, if there
is parsing error, the value of level can be strange.
So, fix both bug.

Change-Id: I21a4eaf9cfed87f6ed14c4b2c35548a0dea4cdd7
Signed-off-by: Soomin Kim <sm8326.kim@samsung.com>
drivers/thermal/of-thermal.c

index 0a87435d7d5c25561145146524931a3cb05d6111..b17dadba97e54c3a2f8f9b6427df9cc3ed033029 100644 (file)
@@ -510,10 +510,12 @@ thermal_zone_of_sensor_register(struct device *dev, int sensor_id, void *data,
        struct __thermal_zone *__tz;
        void *thermal_block;
        struct ect_ap_thermal_function *function;
-       int i = 0, j = 0;
+       int i = 0, zone = 0;
        struct exynos_tmu_data *tmu_data;
        int hotplug_threshold_temp = 0, hotplug_flag = 0;
-       unsigned int level, freq;
+       unsigned int freq;
+       unsigned long level = THERMAL_CSTATE_INVALID;
+       unsigned long max_level = THERMAL_CSTATE_INVALID;
        int temperature;
 #endif
 
@@ -571,35 +573,47 @@ thermal_zone_of_sensor_register(struct device *dev, int sensor_id, void *data,
                        __tz->ntrips = __tz->num_tbps = function->num_of_range;
                        dev_info(dev, "Trip count parsed from ECT : %d, zone : %s", function->num_of_range, tzd->type);
 
+                       /* Find thermal zone number with thermal zone name defined in DT */
+                       for (zone = 0; zone < ARRAY_SIZE(tz_zone_names); zone++)
+                               if (!strcasecmp(tzd->type, tz_zone_names[zone]))
+                                       break;
+
+                       if (zone == ARRAY_SIZE(tz_zone_names))
+                               dev_err(dev, "Error!!! Thermal zone name isn't matched!!\n");
+
                        instance = list_first_entry(&tzd->thermal_instances, typeof(*instance), tz_node);
 
-                       for (i = 0; i < function->num_of_range; ++i) {
+                       for (i = 0; i < function->num_of_range && instance != NULL; ++i) {
                                temperature = function->range_list[i].lower_bound_temperature;
                                freq = function->range_list[i].max_frequency;
 
-                               for (j = 0; j < ARRAY_SIZE(tz_zone_names); j++)
-                                       if (!strcasecmp(tzd->type, tz_zone_names[j])) {
-                                               switch (j) {
-                                                       case MNGS_QUAD :
-                                                               level = cpufreq_cooling_get_level(4, freq);
-                                                               break;
-                                                       case APOLLO :
-                                                               level = cpufreq_cooling_get_level(0, freq);
-                                                               break;
-                                                       case GPU :
-                                                               level = gpufreq_cooling_get_level(0, freq);
-                                                               break;
-                                                       case ISP :
-                                                               level = isp_cooling_get_level(0, freq);
-                                                               break;
-                                                       case MNGS_DUAL :
-                                                               level = cpufreq_cooling_get_level(4, freq);
-                                                               break;
-                                               }
-                                       }
+                               switch (zone) {
+                                       case MNGS_QUAD :
+                                       case MNGS_DUAL :
+                                               level = cpufreq_cooling_get_level(4, freq);
+                                               break;
+                                       case APOLLO :
+                                               level = cpufreq_cooling_get_level(0, freq);
+                                               break;
+                                       case GPU :
+                                               level = gpufreq_cooling_get_level(0, freq);
+                                               break;
+                                       case ISP :
+                                               level = isp_cooling_get_level(0, freq);
+                                               break;
+                                       default :
+                                               level = 0;
+                                               break;
+                               }
+
+                               instance->cdev->ops->get_max_state(instance->cdev, &max_level);
+
+                               if ((zone == ARRAY_SIZE(tz_zone_names) && level == 0) || level == THERMAL_CSTATE_INVALID)
+                                       level = max_level;
 
                                if (level > 100) {
-                                       dev_err(dev, "Level is strange!!! freq = %u, level = %u\n", freq, level);
+                                       dev_err(dev, "Level is invalid!!! freq = %u, level = %lu, max_level=%lu\n",
+                                                       freq, level, max_level);
                                        level = 0;
                                }
 
@@ -610,7 +624,8 @@ thermal_zone_of_sensor_register(struct device *dev, int sensor_id, void *data,
                                /* Change thermal instance information with tbps data */
                                instance->upper = __tz->tbps[i].max;
                                instance = list_next_entry(instance, tz_node);
-                               pr_info("Parsed From ECT : [%d] Temperature : %d, frequency : %u, level = %lu\n", i, temperature, freq, __tz->tbps[i].max);
+                               pr_info("Parsed From ECT : [%d] Temperature : %d, frequency : %u, level = %lu\n",
+                                               i, temperature, freq, __tz->tbps[i].max);
 
                                if (function->range_list[i].flag != hotplug_flag) {
                                        hotplug_threshold_temp = temperature;