aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDmitry Frank <mail@dmitryfrank.com>2017-04-19 05:48:07 -0400
committerRafael J. Wysocki <rafael.j.wysocki@intel.com>2017-04-19 06:32:26 -0400
commitd485ef43072e543e81c08276999987039dfa2755 (patch)
tree26f2696e68e658dc9a6039dc0c56ee36c9a47fb8
parent4f7d029b9bf009fbee76bb10c0c4351a1870d2f3 (diff)
ACPI / video: get rid of magic numbers and use enum instead
The first two items in the _BCL method response are special: - Level when machine has full power - Level when machine is on batteries - .... actual supported levels go there .... So this commits adds an enum and uses its descriptive elements throughout the code, instead of magic numbers. Signed-off-by: Dmitry Frank <mail@dmitryfrank.com> Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
-rw-r--r--drivers/acpi/acpi_video.c110
1 files changed, 66 insertions, 44 deletions
diff --git a/drivers/acpi/acpi_video.c b/drivers/acpi/acpi_video.c
index d00bc0ef87a6..9a607af971e7 100644
--- a/drivers/acpi/acpi_video.c
+++ b/drivers/acpi/acpi_video.c
@@ -88,6 +88,18 @@ static int acpi_video_bus_remove(struct acpi_device *device);
88static void acpi_video_bus_notify(struct acpi_device *device, u32 event); 88static void acpi_video_bus_notify(struct acpi_device *device, u32 event);
89void acpi_video_detect_exit(void); 89void acpi_video_detect_exit(void);
90 90
91/*
92 * Indices in the _BCL method response: the first two items are special,
93 * the rest are all supported levels.
94 *
95 * See page 575 of the ACPI spec 3.0
96 */
97enum acpi_video_level_idx {
98 ACPI_VIDEO_AC_LEVEL, /* level when machine has full power */
99 ACPI_VIDEO_BATTERY_LEVEL, /* level when machine is on batteries */
100 ACPI_VIDEO_FIRST_LEVEL, /* actual supported levels begin here */
101};
102
91static const struct acpi_device_id video_device_ids[] = { 103static const struct acpi_device_id video_device_ids[] = {
92 {ACPI_VIDEO_HID, 0}, 104 {ACPI_VIDEO_HID, 0},
93 {"", 0}, 105 {"", 0},
@@ -217,20 +229,16 @@ static int acpi_video_get_brightness(struct backlight_device *bd)
217 229
218 if (acpi_video_device_lcd_get_level_current(vd, &cur_level, false)) 230 if (acpi_video_device_lcd_get_level_current(vd, &cur_level, false))
219 return -EINVAL; 231 return -EINVAL;
220 for (i = 2; i < vd->brightness->count; i++) { 232 for (i = ACPI_VIDEO_FIRST_LEVEL; i < vd->brightness->count; i++) {
221 if (vd->brightness->levels[i] == cur_level) 233 if (vd->brightness->levels[i] == cur_level)
222 /* 234 return i - ACPI_VIDEO_FIRST_LEVEL;
223 * The first two entries are special - see page 575
224 * of the ACPI spec 3.0
225 */
226 return i - 2;
227 } 235 }
228 return 0; 236 return 0;
229} 237}
230 238
231static int acpi_video_set_brightness(struct backlight_device *bd) 239static int acpi_video_set_brightness(struct backlight_device *bd)
232{ 240{
233 int request_level = bd->props.brightness + 2; 241 int request_level = bd->props.brightness + ACPI_VIDEO_FIRST_LEVEL;
234 struct acpi_video_device *vd = bl_get_data(bd); 242 struct acpi_video_device *vd = bl_get_data(bd);
235 243
236 cancel_delayed_work(&vd->switch_brightness_work); 244 cancel_delayed_work(&vd->switch_brightness_work);
@@ -244,18 +252,18 @@ static const struct backlight_ops acpi_backlight_ops = {
244}; 252};
245 253
246/* thermal cooling device callbacks */ 254/* thermal cooling device callbacks */
247static int video_get_max_state(struct thermal_cooling_device *cooling_dev, unsigned 255static int video_get_max_state(struct thermal_cooling_device *cooling_dev,
248 long *state) 256 unsigned long *state)
249{ 257{
250 struct acpi_device *device = cooling_dev->devdata; 258 struct acpi_device *device = cooling_dev->devdata;
251 struct acpi_video_device *video = acpi_driver_data(device); 259 struct acpi_video_device *video = acpi_driver_data(device);
252 260
253 *state = video->brightness->count - 3; 261 *state = video->brightness->count - ACPI_VIDEO_FIRST_LEVEL - 1;
254 return 0; 262 return 0;
255} 263}
256 264
257static int video_get_cur_state(struct thermal_cooling_device *cooling_dev, unsigned 265static int video_get_cur_state(struct thermal_cooling_device *cooling_dev,
258 long *state) 266 unsigned long *state)
259{ 267{
260 struct acpi_device *device = cooling_dev->devdata; 268 struct acpi_device *device = cooling_dev->devdata;
261 struct acpi_video_device *video = acpi_driver_data(device); 269 struct acpi_video_device *video = acpi_driver_data(device);
@@ -264,7 +272,8 @@ static int video_get_cur_state(struct thermal_cooling_device *cooling_dev, unsig
264 272
265 if (acpi_video_device_lcd_get_level_current(video, &level, false)) 273 if (acpi_video_device_lcd_get_level_current(video, &level, false))
266 return -EINVAL; 274 return -EINVAL;
267 for (offset = 2; offset < video->brightness->count; offset++) 275 for (offset = ACPI_VIDEO_FIRST_LEVEL; offset < video->brightness->count;
276 offset++)
268 if (level == video->brightness->levels[offset]) { 277 if (level == video->brightness->levels[offset]) {
269 *state = video->brightness->count - offset - 1; 278 *state = video->brightness->count - offset - 1;
270 return 0; 279 return 0;
@@ -280,7 +289,7 @@ video_set_cur_state(struct thermal_cooling_device *cooling_dev, unsigned long st
280 struct acpi_video_device *video = acpi_driver_data(device); 289 struct acpi_video_device *video = acpi_driver_data(device);
281 int level; 290 int level;
282 291
283 if (state >= video->brightness->count - 2) 292 if (state >= video->brightness->count - ACPI_VIDEO_FIRST_LEVEL)
284 return -EINVAL; 293 return -EINVAL;
285 294
286 state = video->brightness->count - state; 295 state = video->brightness->count - state;
@@ -345,10 +354,12 @@ acpi_video_device_lcd_set_level(struct acpi_video_device *device, int level)
345 } 354 }
346 355
347 device->brightness->curr = level; 356 device->brightness->curr = level;
348 for (state = 2; state < device->brightness->count; state++) 357 for (state = ACPI_VIDEO_FIRST_LEVEL; state < device->brightness->count;
358 state++)
349 if (level == device->brightness->levels[state]) { 359 if (level == device->brightness->levels[state]) {
350 if (device->backlight) 360 if (device->backlight)
351 device->backlight->props.brightness = state - 2; 361 device->backlight->props.brightness =
362 state - ACPI_VIDEO_FIRST_LEVEL;
352 return 0; 363 return 0;
353 } 364 }
354 365
@@ -530,14 +541,16 @@ acpi_video_bqc_value_to_level(struct acpi_video_device *device,
530 541
531 if (device->brightness->flags._BQC_use_index) { 542 if (device->brightness->flags._BQC_use_index) {
532 /* 543 /*
533 * _BQC returns an index that doesn't account for 544 * _BQC returns an index that doesn't account for the first 2
534 * the first 2 items with special meaning, so we need 545 * items with special meaning (see enum acpi_video_level_idx),
535 * to compensate for that by offsetting ourselves 546 * so we need to compensate for that by offsetting ourselves
536 */ 547 */
537 if (device->brightness->flags._BCL_reversed) 548 if (device->brightness->flags._BCL_reversed)
538 bqc_value = device->brightness->count - 3 - bqc_value; 549 bqc_value = device->brightness->count -
550 ACPI_VIDEO_FIRST_LEVEL - 1 - bqc_value;
539 551
540 level = device->brightness->levels[bqc_value + 2]; 552 level = device->brightness->levels[bqc_value +
553 ACPI_VIDEO_FIRST_LEVEL];
541 } else { 554 } else {
542 level = bqc_value; 555 level = bqc_value;
543 } 556 }
@@ -571,7 +584,8 @@ acpi_video_device_lcd_get_level_current(struct acpi_video_device *device,
571 584
572 *level = acpi_video_bqc_value_to_level(device, *level); 585 *level = acpi_video_bqc_value_to_level(device, *level);
573 586
574 for (i = 2; i < device->brightness->count; i++) 587 for (i = ACPI_VIDEO_FIRST_LEVEL;
588 i < device->brightness->count; i++)
575 if (device->brightness->levels[i] == *level) { 589 if (device->brightness->levels[i] == *level) {
576 device->brightness->curr = *level; 590 device->brightness->curr = *level;
577 return 0; 591 return 0;
@@ -716,7 +730,9 @@ static int acpi_video_bqc_quirk(struct acpi_video_device *device,
716 * Some systems always report current brightness level as maximum 730 * Some systems always report current brightness level as maximum
717 * through _BQC, we need to test another value for them. 731 * through _BQC, we need to test another value for them.
718 */ 732 */
719 test_level = current_level == max_level ? br->levels[3] : max_level; 733 test_level = current_level == max_level
734 ? br->levels[ACPI_VIDEO_FIRST_LEVEL + 1]
735 : max_level;
720 736
721 result = acpi_video_device_lcd_set_level(device, test_level); 737 result = acpi_video_device_lcd_set_level(device, test_level);
722 if (result) 738 if (result)
@@ -730,8 +746,8 @@ static int acpi_video_bqc_quirk(struct acpi_video_device *device,
730 /* buggy _BQC found, need to find out if it uses index */ 746 /* buggy _BQC found, need to find out if it uses index */
731 if (level < br->count) { 747 if (level < br->count) {
732 if (br->flags._BCL_reversed) 748 if (br->flags._BCL_reversed)
733 level = br->count - 3 - level; 749 level = br->count - ACPI_VIDEO_FIRST_LEVEL - 1 - level;
734 if (br->levels[level + 2] == test_level) 750 if (br->levels[level + ACPI_VIDEO_FIRST_LEVEL] == test_level)
735 br->flags._BQC_use_index = 1; 751 br->flags._BQC_use_index = 1;
736 } 752 }
737 753
@@ -761,7 +777,7 @@ int acpi_video_get_levels(struct acpi_device *device,
761 goto out; 777 goto out;
762 } 778 }
763 779
764 if (obj->package.count < 2) { 780 if (obj->package.count < ACPI_VIDEO_FIRST_LEVEL) {
765 result = -EINVAL; 781 result = -EINVAL;
766 goto out; 782 goto out;
767 } 783 }
@@ -773,8 +789,8 @@ int acpi_video_get_levels(struct acpi_device *device,
773 goto out; 789 goto out;
774 } 790 }
775 791
776 br->levels = kmalloc((obj->package.count + 2) * sizeof *(br->levels), 792 br->levels = kmalloc((obj->package.count + ACPI_VIDEO_FIRST_LEVEL) *
777 GFP_KERNEL); 793 sizeof(*br->levels), GFP_KERNEL);
778 if (!br->levels) { 794 if (!br->levels) {
779 result = -ENOMEM; 795 result = -ENOMEM;
780 goto out_free; 796 goto out_free;
@@ -788,7 +804,8 @@ int acpi_video_get_levels(struct acpi_device *device,
788 } 804 }
789 value = (u32) o->integer.value; 805 value = (u32) o->integer.value;
790 /* Skip duplicate entries */ 806 /* Skip duplicate entries */
791 if (count > 2 && br->levels[count - 1] == value) 807 if (count > ACPI_VIDEO_FIRST_LEVEL
808 && br->levels[count - 1] == value)
792 continue; 809 continue;
793 810
794 br->levels[count] = value; 811 br->levels[count] = value;
@@ -804,27 +821,30 @@ int acpi_video_get_levels(struct acpi_device *device,
804 * In this case, the first two elements in _BCL packages 821 * In this case, the first two elements in _BCL packages
805 * are also supported brightness levels that OS should take care of. 822 * are also supported brightness levels that OS should take care of.
806 */ 823 */
807 for (i = 2; i < count; i++) { 824 for (i = ACPI_VIDEO_FIRST_LEVEL; i < count; i++) {
808 if (br->levels[i] == br->levels[0]) 825 if (br->levels[i] == br->levels[ACPI_VIDEO_AC_LEVEL])
809 level_ac_battery++; 826 level_ac_battery++;
810 if (br->levels[i] == br->levels[1]) 827 if (br->levels[i] == br->levels[ACPI_VIDEO_BATTERY_LEVEL])
811 level_ac_battery++; 828 level_ac_battery++;
812 } 829 }
813 830
814 if (level_ac_battery < 2) { 831 if (level_ac_battery < ACPI_VIDEO_FIRST_LEVEL) {
815 level_ac_battery = 2 - level_ac_battery; 832 level_ac_battery = ACPI_VIDEO_FIRST_LEVEL - level_ac_battery;
816 br->flags._BCL_no_ac_battery_levels = 1; 833 br->flags._BCL_no_ac_battery_levels = 1;
817 for (i = (count - 1 + level_ac_battery); i >= 2; i--) 834 for (i = (count - 1 + level_ac_battery);
835 i >= ACPI_VIDEO_FIRST_LEVEL; i--)
818 br->levels[i] = br->levels[i - level_ac_battery]; 836 br->levels[i] = br->levels[i - level_ac_battery];
819 count += level_ac_battery; 837 count += level_ac_battery;
820 } else if (level_ac_battery > 2) 838 } else if (level_ac_battery > ACPI_VIDEO_FIRST_LEVEL)
821 ACPI_ERROR((AE_INFO, "Too many duplicates in _BCL package")); 839 ACPI_ERROR((AE_INFO, "Too many duplicates in _BCL package"));
822 840
823 /* Check if the _BCL package is in a reversed order */ 841 /* Check if the _BCL package is in a reversed order */
824 if (max_level == br->levels[2]) { 842 if (max_level == br->levels[ACPI_VIDEO_FIRST_LEVEL]) {
825 br->flags._BCL_reversed = 1; 843 br->flags._BCL_reversed = 1;
826 sort(&br->levels[2], count - 2, sizeof(br->levels[2]), 844 sort(&br->levels[ACPI_VIDEO_FIRST_LEVEL],
827 acpi_video_cmp_level, NULL); 845 count - ACPI_VIDEO_FIRST_LEVEL,
846 sizeof(br->levels[ACPI_VIDEO_FIRST_LEVEL]),
847 acpi_video_cmp_level, NULL);
828 } else if (max_level != br->levels[count - 1]) 848 } else if (max_level != br->levels[count - 1])
829 ACPI_ERROR((AE_INFO, 849 ACPI_ERROR((AE_INFO,
830 "Found unordered _BCL package")); 850 "Found unordered _BCL package"));
@@ -894,7 +914,7 @@ acpi_video_init_brightness(struct acpi_video_device *device)
894 * level_old is invalid (no matter whether it's a level 914 * level_old is invalid (no matter whether it's a level
895 * or an index). Set the backlight to max_level in this case. 915 * or an index). Set the backlight to max_level in this case.
896 */ 916 */
897 for (i = 2; i < br->count; i++) 917 for (i = ACPI_VIDEO_FIRST_LEVEL; i < br->count; i++)
898 if (level == br->levels[i]) 918 if (level == br->levels[i])
899 break; 919 break;
900 if (i == br->count || !level) 920 if (i == br->count || !level)
@@ -906,7 +926,8 @@ set_level:
906 goto out_free_levels; 926 goto out_free_levels;
907 927
908 ACPI_DEBUG_PRINT((ACPI_DB_INFO, 928 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
909 "found %d brightness levels\n", br->count - 2)); 929 "found %d brightness levels\n",
930 br->count - ACPI_VIDEO_FIRST_LEVEL));
910 return 0; 931 return 0;
911 932
912out_free_levels: 933out_free_levels:
@@ -1297,7 +1318,7 @@ acpi_video_get_next_level(struct acpi_video_device *device,
1297 max = max_below = 0; 1318 max = max_below = 0;
1298 min = min_above = 255; 1319 min = min_above = 255;
1299 /* Find closest level to level_current */ 1320 /* Find closest level to level_current */
1300 for (i = 2; i < device->brightness->count; i++) { 1321 for (i = ACPI_VIDEO_FIRST_LEVEL; i < device->brightness->count; i++) {
1301 l = device->brightness->levels[i]; 1322 l = device->brightness->levels[i];
1302 if (abs(l - level_current) < abs(delta)) { 1323 if (abs(l - level_current) < abs(delta)) {
1303 delta = l - level_current; 1324 delta = l - level_current;
@@ -1307,7 +1328,7 @@ acpi_video_get_next_level(struct acpi_video_device *device,
1307 } 1328 }
1308 /* Ajust level_current to closest available level */ 1329 /* Ajust level_current to closest available level */
1309 level_current += delta; 1330 level_current += delta;
1310 for (i = 2; i < device->brightness->count; i++) { 1331 for (i = ACPI_VIDEO_FIRST_LEVEL; i < device->brightness->count; i++) {
1311 l = device->brightness->levels[i]; 1332 l = device->brightness->levels[i];
1312 if (l < min) 1333 if (l < min)
1313 min = l; 1334 min = l;
@@ -1680,7 +1701,8 @@ static void acpi_video_dev_register_backlight(struct acpi_video_device *device)
1680 1701
1681 memset(&props, 0, sizeof(struct backlight_properties)); 1702 memset(&props, 0, sizeof(struct backlight_properties));
1682 props.type = BACKLIGHT_FIRMWARE; 1703 props.type = BACKLIGHT_FIRMWARE;
1683 props.max_brightness = device->brightness->count - 3; 1704 props.max_brightness =
1705 device->brightness->count - ACPI_VIDEO_FIRST_LEVEL - 1;
1684 device->backlight = backlight_device_register(name, 1706 device->backlight = backlight_device_register(name,
1685 parent, 1707 parent,
1686 device, 1708 device,