/drivers/acpi/video.c

http://github.com/mirrors/linux · C · 1933 lines · 1387 code · 287 blank · 259 comment · 241 complexity · 05232212a4f6a0f630ab0d619e69f543 MD5 · raw file

  1. /*
  2. * video.c - ACPI Video Driver
  3. *
  4. * Copyright (C) 2004 Luming Yu <luming.yu@intel.com>
  5. * Copyright (C) 2004 Bruno Ducrot <ducrot@poupinou.org>
  6. * Copyright (C) 2006 Thomas Tuttle <linux-kernel@ttuttle.net>
  7. *
  8. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or (at
  13. * your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful, but
  16. * WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License along
  21. * with this program; if not, write to the Free Software Foundation, Inc.,
  22. * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
  23. *
  24. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  25. */
  26. #include <linux/kernel.h>
  27. #include <linux/module.h>
  28. #include <linux/init.h>
  29. #include <linux/types.h>
  30. #include <linux/list.h>
  31. #include <linux/mutex.h>
  32. #include <linux/input.h>
  33. #include <linux/backlight.h>
  34. #include <linux/thermal.h>
  35. #include <linux/sort.h>
  36. #include <linux/pci.h>
  37. #include <linux/pci_ids.h>
  38. #include <linux/slab.h>
  39. #include <asm/uaccess.h>
  40. #include <linux/dmi.h>
  41. #include <acpi/acpi_bus.h>
  42. #include <acpi/acpi_drivers.h>
  43. #include <linux/suspend.h>
  44. #include <acpi/video.h>
  45. #include "internal.h"
  46. #define PREFIX "ACPI: "
  47. #define ACPI_VIDEO_BUS_NAME "Video Bus"
  48. #define ACPI_VIDEO_DEVICE_NAME "Video Device"
  49. #define ACPI_VIDEO_NOTIFY_SWITCH 0x80
  50. #define ACPI_VIDEO_NOTIFY_PROBE 0x81
  51. #define ACPI_VIDEO_NOTIFY_CYCLE 0x82
  52. #define ACPI_VIDEO_NOTIFY_NEXT_OUTPUT 0x83
  53. #define ACPI_VIDEO_NOTIFY_PREV_OUTPUT 0x84
  54. #define ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS 0x85
  55. #define ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS 0x86
  56. #define ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS 0x87
  57. #define ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS 0x88
  58. #define ACPI_VIDEO_NOTIFY_DISPLAY_OFF 0x89
  59. #define MAX_NAME_LEN 20
  60. #define _COMPONENT ACPI_VIDEO_COMPONENT
  61. ACPI_MODULE_NAME("video");
  62. MODULE_AUTHOR("Bruno Ducrot");
  63. MODULE_DESCRIPTION("ACPI Video Driver");
  64. MODULE_LICENSE("GPL");
  65. static bool brightness_switch_enabled = 1;
  66. module_param(brightness_switch_enabled, bool, 0644);
  67. /*
  68. * By default, we don't allow duplicate ACPI video bus devices
  69. * under the same VGA controller
  70. */
  71. static bool allow_duplicates;
  72. module_param(allow_duplicates, bool, 0644);
  73. /*
  74. * Some BIOSes claim they use minimum backlight at boot,
  75. * and this may bring dimming screen after boot
  76. */
  77. static bool use_bios_initial_backlight = 1;
  78. module_param(use_bios_initial_backlight, bool, 0644);
  79. static int register_count;
  80. static int acpi_video_bus_add(struct acpi_device *device);
  81. static int acpi_video_bus_remove(struct acpi_device *device);
  82. static void acpi_video_bus_notify(struct acpi_device *device, u32 event);
  83. static const struct acpi_device_id video_device_ids[] = {
  84. {ACPI_VIDEO_HID, 0},
  85. {"", 0},
  86. };
  87. MODULE_DEVICE_TABLE(acpi, video_device_ids);
  88. static struct acpi_driver acpi_video_bus = {
  89. .name = "video",
  90. .class = ACPI_VIDEO_CLASS,
  91. .ids = video_device_ids,
  92. .ops = {
  93. .add = acpi_video_bus_add,
  94. .remove = acpi_video_bus_remove,
  95. .notify = acpi_video_bus_notify,
  96. },
  97. };
  98. struct acpi_video_bus_flags {
  99. u8 multihead:1; /* can switch video heads */
  100. u8 rom:1; /* can retrieve a video rom */
  101. u8 post:1; /* can configure the head to */
  102. u8 reserved:5;
  103. };
  104. struct acpi_video_bus_cap {
  105. u8 _DOS:1; /* Enable/Disable output switching */
  106. u8 _DOD:1; /* Enumerate all devices attached to display adapter */
  107. u8 _ROM:1; /* Get ROM Data */
  108. u8 _GPD:1; /* Get POST Device */
  109. u8 _SPD:1; /* Set POST Device */
  110. u8 _VPO:1; /* Video POST Options */
  111. u8 reserved:2;
  112. };
  113. struct acpi_video_device_attrib {
  114. u32 display_index:4; /* A zero-based instance of the Display */
  115. u32 display_port_attachment:4; /* This field differentiates the display type */
  116. u32 display_type:4; /* Describe the specific type in use */
  117. u32 vendor_specific:4; /* Chipset Vendor Specific */
  118. u32 bios_can_detect:1; /* BIOS can detect the device */
  119. u32 depend_on_vga:1; /* Non-VGA output device whose power is related to
  120. the VGA device. */
  121. u32 pipe_id:3; /* For VGA multiple-head devices. */
  122. u32 reserved:10; /* Must be 0 */
  123. u32 device_id_scheme:1; /* Device ID Scheme */
  124. };
  125. struct acpi_video_enumerated_device {
  126. union {
  127. u32 int_val;
  128. struct acpi_video_device_attrib attrib;
  129. } value;
  130. struct acpi_video_device *bind_info;
  131. };
  132. struct acpi_video_bus {
  133. struct acpi_device *device;
  134. u8 dos_setting;
  135. struct acpi_video_enumerated_device *attached_array;
  136. u8 attached_count;
  137. struct acpi_video_bus_cap cap;
  138. struct acpi_video_bus_flags flags;
  139. struct list_head video_device_list;
  140. struct mutex device_list_lock; /* protects video_device_list */
  141. struct input_dev *input;
  142. char phys[32]; /* for input device */
  143. struct notifier_block pm_nb;
  144. };
  145. struct acpi_video_device_flags {
  146. u8 crt:1;
  147. u8 lcd:1;
  148. u8 tvout:1;
  149. u8 dvi:1;
  150. u8 bios:1;
  151. u8 unknown:1;
  152. u8 notify:1;
  153. u8 reserved:1;
  154. };
  155. struct acpi_video_device_cap {
  156. u8 _ADR:1; /* Return the unique ID */
  157. u8 _BCL:1; /* Query list of brightness control levels supported */
  158. u8 _BCM:1; /* Set the brightness level */
  159. u8 _BQC:1; /* Get current brightness level */
  160. u8 _BCQ:1; /* Some buggy BIOS uses _BCQ instead of _BQC */
  161. u8 _DDC:1; /* Return the EDID for this device */
  162. };
  163. struct acpi_video_brightness_flags {
  164. u8 _BCL_no_ac_battery_levels:1; /* no AC/Battery levels in _BCL */
  165. u8 _BCL_reversed:1; /* _BCL package is in a reversed order */
  166. u8 _BQC_use_index:1; /* _BQC returns an index value */
  167. };
  168. struct acpi_video_device_brightness {
  169. int curr;
  170. int count;
  171. int *levels;
  172. struct acpi_video_brightness_flags flags;
  173. };
  174. struct acpi_video_device {
  175. unsigned long device_id;
  176. struct acpi_video_device_flags flags;
  177. struct acpi_video_device_cap cap;
  178. struct list_head entry;
  179. struct acpi_video_bus *video;
  180. struct acpi_device *dev;
  181. struct acpi_video_device_brightness *brightness;
  182. struct backlight_device *backlight;
  183. struct thermal_cooling_device *cooling_dev;
  184. };
  185. static const char device_decode[][30] = {
  186. "motherboard VGA device",
  187. "PCI VGA device",
  188. "AGP VGA device",
  189. "UNKNOWN",
  190. };
  191. static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data);
  192. static void acpi_video_device_rebind(struct acpi_video_bus *video);
  193. static void acpi_video_device_bind(struct acpi_video_bus *video,
  194. struct acpi_video_device *device);
  195. static int acpi_video_device_enumerate(struct acpi_video_bus *video);
  196. static int acpi_video_device_lcd_set_level(struct acpi_video_device *device,
  197. int level);
  198. static int acpi_video_device_lcd_get_level_current(
  199. struct acpi_video_device *device,
  200. unsigned long long *level, bool raw);
  201. static int acpi_video_get_next_level(struct acpi_video_device *device,
  202. u32 level_current, u32 event);
  203. static int acpi_video_switch_brightness(struct acpi_video_device *device,
  204. int event);
  205. /* backlight device sysfs support */
  206. static int acpi_video_get_brightness(struct backlight_device *bd)
  207. {
  208. unsigned long long cur_level;
  209. int i;
  210. struct acpi_video_device *vd = bl_get_data(bd);
  211. if (acpi_video_device_lcd_get_level_current(vd, &cur_level, false))
  212. return -EINVAL;
  213. for (i = 2; i < vd->brightness->count; i++) {
  214. if (vd->brightness->levels[i] == cur_level)
  215. /*
  216. * The first two entries are special - see page 575
  217. * of the ACPI spec 3.0
  218. */
  219. return i - 2;
  220. }
  221. return 0;
  222. }
  223. static int acpi_video_set_brightness(struct backlight_device *bd)
  224. {
  225. int request_level = bd->props.brightness + 2;
  226. struct acpi_video_device *vd = bl_get_data(bd);
  227. return acpi_video_device_lcd_set_level(vd,
  228. vd->brightness->levels[request_level]);
  229. }
  230. static const struct backlight_ops acpi_backlight_ops = {
  231. .get_brightness = acpi_video_get_brightness,
  232. .update_status = acpi_video_set_brightness,
  233. };
  234. /* thermal cooling device callbacks */
  235. static int video_get_max_state(struct thermal_cooling_device *cooling_dev, unsigned
  236. long *state)
  237. {
  238. struct acpi_device *device = cooling_dev->devdata;
  239. struct acpi_video_device *video = acpi_driver_data(device);
  240. *state = video->brightness->count - 3;
  241. return 0;
  242. }
  243. static int video_get_cur_state(struct thermal_cooling_device *cooling_dev, unsigned
  244. long *state)
  245. {
  246. struct acpi_device *device = cooling_dev->devdata;
  247. struct acpi_video_device *video = acpi_driver_data(device);
  248. unsigned long long level;
  249. int offset;
  250. if (acpi_video_device_lcd_get_level_current(video, &level, false))
  251. return -EINVAL;
  252. for (offset = 2; offset < video->brightness->count; offset++)
  253. if (level == video->brightness->levels[offset]) {
  254. *state = video->brightness->count - offset - 1;
  255. return 0;
  256. }
  257. return -EINVAL;
  258. }
  259. static int
  260. video_set_cur_state(struct thermal_cooling_device *cooling_dev, unsigned long state)
  261. {
  262. struct acpi_device *device = cooling_dev->devdata;
  263. struct acpi_video_device *video = acpi_driver_data(device);
  264. int level;
  265. if (state >= video->brightness->count - 2)
  266. return -EINVAL;
  267. state = video->brightness->count - state;
  268. level = video->brightness->levels[state - 1];
  269. return acpi_video_device_lcd_set_level(video, level);
  270. }
  271. static const struct thermal_cooling_device_ops video_cooling_ops = {
  272. .get_max_state = video_get_max_state,
  273. .get_cur_state = video_get_cur_state,
  274. .set_cur_state = video_set_cur_state,
  275. };
  276. /*
  277. * --------------------------------------------------------------------------
  278. * Video Management
  279. * --------------------------------------------------------------------------
  280. */
  281. static int
  282. acpi_video_device_lcd_query_levels(struct acpi_video_device *device,
  283. union acpi_object **levels)
  284. {
  285. int status;
  286. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  287. union acpi_object *obj;
  288. *levels = NULL;
  289. status = acpi_evaluate_object(device->dev->handle, "_BCL", NULL, &buffer);
  290. if (!ACPI_SUCCESS(status))
  291. return status;
  292. obj = (union acpi_object *)buffer.pointer;
  293. if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
  294. printk(KERN_ERR PREFIX "Invalid _BCL data\n");
  295. status = -EFAULT;
  296. goto err;
  297. }
  298. *levels = obj;
  299. return 0;
  300. err:
  301. kfree(buffer.pointer);
  302. return status;
  303. }
  304. static int
  305. acpi_video_device_lcd_set_level(struct acpi_video_device *device, int level)
  306. {
  307. int status;
  308. int state;
  309. status = acpi_execute_simple_method(device->dev->handle,
  310. "_BCM", level);
  311. if (ACPI_FAILURE(status)) {
  312. ACPI_ERROR((AE_INFO, "Evaluating _BCM failed"));
  313. return -EIO;
  314. }
  315. device->brightness->curr = level;
  316. for (state = 2; state < device->brightness->count; state++)
  317. if (level == device->brightness->levels[state]) {
  318. if (device->backlight)
  319. device->backlight->props.brightness = state - 2;
  320. return 0;
  321. }
  322. ACPI_ERROR((AE_INFO, "Current brightness invalid"));
  323. return -EINVAL;
  324. }
  325. /*
  326. * For some buggy _BQC methods, we need to add a constant value to
  327. * the _BQC return value to get the actual current brightness level
  328. */
  329. static int bqc_offset_aml_bug_workaround;
  330. static int __init video_set_bqc_offset(const struct dmi_system_id *d)
  331. {
  332. bqc_offset_aml_bug_workaround = 9;
  333. return 0;
  334. }
  335. static int video_ignore_initial_backlight(const struct dmi_system_id *d)
  336. {
  337. use_bios_initial_backlight = 0;
  338. return 0;
  339. }
  340. static struct dmi_system_id video_dmi_table[] __initdata = {
  341. /*
  342. * Broken _BQC workaround http://bugzilla.kernel.org/show_bug.cgi?id=13121
  343. */
  344. {
  345. .callback = video_set_bqc_offset,
  346. .ident = "Acer Aspire 5720",
  347. .matches = {
  348. DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
  349. DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5720"),
  350. },
  351. },
  352. {
  353. .callback = video_set_bqc_offset,
  354. .ident = "Acer Aspire 5710Z",
  355. .matches = {
  356. DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
  357. DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5710Z"),
  358. },
  359. },
  360. {
  361. .callback = video_set_bqc_offset,
  362. .ident = "eMachines E510",
  363. .matches = {
  364. DMI_MATCH(DMI_BOARD_VENDOR, "EMACHINES"),
  365. DMI_MATCH(DMI_PRODUCT_NAME, "eMachines E510"),
  366. },
  367. },
  368. {
  369. .callback = video_set_bqc_offset,
  370. .ident = "Acer Aspire 5315",
  371. .matches = {
  372. DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
  373. DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5315"),
  374. },
  375. },
  376. {
  377. .callback = video_set_bqc_offset,
  378. .ident = "Acer Aspire 7720",
  379. .matches = {
  380. DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
  381. DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 7720"),
  382. },
  383. },
  384. {
  385. .callback = video_ignore_initial_backlight,
  386. .ident = "HP Folio 13-2000",
  387. .matches = {
  388. DMI_MATCH(DMI_BOARD_VENDOR, "Hewlett-Packard"),
  389. DMI_MATCH(DMI_PRODUCT_NAME, "HP Folio 13 - 2000 Notebook PC"),
  390. },
  391. },
  392. {
  393. .callback = video_ignore_initial_backlight,
  394. .ident = "Fujitsu E753",
  395. .matches = {
  396. DMI_MATCH(DMI_BOARD_VENDOR, "FUJITSU"),
  397. DMI_MATCH(DMI_PRODUCT_NAME, "LIFEBOOK E753"),
  398. },
  399. },
  400. {
  401. .callback = video_ignore_initial_backlight,
  402. .ident = "HP Pavilion dm4",
  403. .matches = {
  404. DMI_MATCH(DMI_BOARD_VENDOR, "Hewlett-Packard"),
  405. DMI_MATCH(DMI_PRODUCT_NAME, "HP Pavilion dm4 Notebook PC"),
  406. },
  407. },
  408. {
  409. .callback = video_ignore_initial_backlight,
  410. .ident = "HP Pavilion g6 Notebook PC",
  411. .matches = {
  412. DMI_MATCH(DMI_BOARD_VENDOR, "Hewlett-Packard"),
  413. DMI_MATCH(DMI_PRODUCT_NAME, "HP Pavilion g6 Notebook PC"),
  414. },
  415. },
  416. {
  417. .callback = video_ignore_initial_backlight,
  418. .ident = "HP 1000 Notebook PC",
  419. .matches = {
  420. DMI_MATCH(DMI_BOARD_VENDOR, "Hewlett-Packard"),
  421. DMI_MATCH(DMI_PRODUCT_NAME, "HP 1000 Notebook PC"),
  422. },
  423. },
  424. {
  425. .callback = video_ignore_initial_backlight,
  426. .ident = "HP Pavilion m4",
  427. .matches = {
  428. DMI_MATCH(DMI_BOARD_VENDOR, "Hewlett-Packard"),
  429. DMI_MATCH(DMI_PRODUCT_NAME, "HP Pavilion m4 Notebook PC"),
  430. },
  431. },
  432. {}
  433. };
  434. static unsigned long long
  435. acpi_video_bqc_value_to_level(struct acpi_video_device *device,
  436. unsigned long long bqc_value)
  437. {
  438. unsigned long long level;
  439. if (device->brightness->flags._BQC_use_index) {
  440. /*
  441. * _BQC returns an index that doesn't account for
  442. * the first 2 items with special meaning, so we need
  443. * to compensate for that by offsetting ourselves
  444. */
  445. if (device->brightness->flags._BCL_reversed)
  446. bqc_value = device->brightness->count - 3 - bqc_value;
  447. level = device->brightness->levels[bqc_value + 2];
  448. } else {
  449. level = bqc_value;
  450. }
  451. level += bqc_offset_aml_bug_workaround;
  452. return level;
  453. }
  454. static int
  455. acpi_video_device_lcd_get_level_current(struct acpi_video_device *device,
  456. unsigned long long *level, bool raw)
  457. {
  458. acpi_status status = AE_OK;
  459. int i;
  460. if (device->cap._BQC || device->cap._BCQ) {
  461. char *buf = device->cap._BQC ? "_BQC" : "_BCQ";
  462. status = acpi_evaluate_integer(device->dev->handle, buf,
  463. NULL, level);
  464. if (ACPI_SUCCESS(status)) {
  465. if (raw) {
  466. /*
  467. * Caller has indicated he wants the raw
  468. * value returned by _BQC, so don't furtherly
  469. * mess with the value.
  470. */
  471. return 0;
  472. }
  473. *level = acpi_video_bqc_value_to_level(device, *level);
  474. for (i = 2; i < device->brightness->count; i++)
  475. if (device->brightness->levels[i] == *level) {
  476. device->brightness->curr = *level;
  477. return 0;
  478. }
  479. /*
  480. * BQC returned an invalid level.
  481. * Stop using it.
  482. */
  483. ACPI_WARNING((AE_INFO,
  484. "%s returned an invalid level",
  485. buf));
  486. device->cap._BQC = device->cap._BCQ = 0;
  487. } else {
  488. /*
  489. * Fixme:
  490. * should we return an error or ignore this failure?
  491. * dev->brightness->curr is a cached value which stores
  492. * the correct current backlight level in most cases.
  493. * ACPI video backlight still works w/ buggy _BQC.
  494. * http://bugzilla.kernel.org/show_bug.cgi?id=12233
  495. */
  496. ACPI_WARNING((AE_INFO, "Evaluating %s failed", buf));
  497. device->cap._BQC = device->cap._BCQ = 0;
  498. }
  499. }
  500. *level = device->brightness->curr;
  501. return 0;
  502. }
  503. static int
  504. acpi_video_device_EDID(struct acpi_video_device *device,
  505. union acpi_object **edid, ssize_t length)
  506. {
  507. int status;
  508. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  509. union acpi_object *obj;
  510. union acpi_object arg0 = { ACPI_TYPE_INTEGER };
  511. struct acpi_object_list args = { 1, &arg0 };
  512. *edid = NULL;
  513. if (!device)
  514. return -ENODEV;
  515. if (length == 128)
  516. arg0.integer.value = 1;
  517. else if (length == 256)
  518. arg0.integer.value = 2;
  519. else
  520. return -EINVAL;
  521. status = acpi_evaluate_object(device->dev->handle, "_DDC", &args, &buffer);
  522. if (ACPI_FAILURE(status))
  523. return -ENODEV;
  524. obj = buffer.pointer;
  525. if (obj && obj->type == ACPI_TYPE_BUFFER)
  526. *edid = obj;
  527. else {
  528. printk(KERN_ERR PREFIX "Invalid _DDC data\n");
  529. status = -EFAULT;
  530. kfree(obj);
  531. }
  532. return status;
  533. }
  534. /* bus */
  535. /*
  536. * Arg:
  537. * video : video bus device pointer
  538. * bios_flag :
  539. * 0. The system BIOS should NOT automatically switch(toggle)
  540. * the active display output.
  541. * 1. The system BIOS should automatically switch (toggle) the
  542. * active display output. No switch event.
  543. * 2. The _DGS value should be locked.
  544. * 3. The system BIOS should not automatically switch (toggle) the
  545. * active display output, but instead generate the display switch
  546. * event notify code.
  547. * lcd_flag :
  548. * 0. The system BIOS should automatically control the brightness level
  549. * of the LCD when the power changes from AC to DC
  550. * 1. The system BIOS should NOT automatically control the brightness
  551. * level of the LCD when the power changes from AC to DC.
  552. * Return Value:
  553. * -EINVAL wrong arg.
  554. */
  555. static int
  556. acpi_video_bus_DOS(struct acpi_video_bus *video, int bios_flag, int lcd_flag)
  557. {
  558. acpi_status status;
  559. if (!video->cap._DOS)
  560. return 0;
  561. if (bios_flag < 0 || bios_flag > 3 || lcd_flag < 0 || lcd_flag > 1)
  562. return -EINVAL;
  563. video->dos_setting = (lcd_flag << 2) | bios_flag;
  564. status = acpi_execute_simple_method(video->device->handle, "_DOS",
  565. (lcd_flag << 2) | bios_flag);
  566. if (ACPI_FAILURE(status))
  567. return -EIO;
  568. return 0;
  569. }
  570. /*
  571. * Simple comparison function used to sort backlight levels.
  572. */
  573. static int
  574. acpi_video_cmp_level(const void *a, const void *b)
  575. {
  576. return *(int *)a - *(int *)b;
  577. }
  578. /*
  579. * Decides if _BQC/_BCQ for this system is usable
  580. *
  581. * We do this by changing the level first and then read out the current
  582. * brightness level, if the value does not match, find out if it is using
  583. * index. If not, clear the _BQC/_BCQ capability.
  584. */
  585. static int acpi_video_bqc_quirk(struct acpi_video_device *device,
  586. int max_level, int current_level)
  587. {
  588. struct acpi_video_device_brightness *br = device->brightness;
  589. int result;
  590. unsigned long long level;
  591. int test_level;
  592. /* don't mess with existing known broken systems */
  593. if (bqc_offset_aml_bug_workaround)
  594. return 0;
  595. /*
  596. * Some systems always report current brightness level as maximum
  597. * through _BQC, we need to test another value for them.
  598. */
  599. test_level = current_level == max_level ? br->levels[3] : max_level;
  600. result = acpi_video_device_lcd_set_level(device, test_level);
  601. if (result)
  602. return result;
  603. result = acpi_video_device_lcd_get_level_current(device, &level, true);
  604. if (result)
  605. return result;
  606. if (level != test_level) {
  607. /* buggy _BQC found, need to find out if it uses index */
  608. if (level < br->count) {
  609. if (br->flags._BCL_reversed)
  610. level = br->count - 3 - level;
  611. if (br->levels[level + 2] == test_level)
  612. br->flags._BQC_use_index = 1;
  613. }
  614. if (!br->flags._BQC_use_index)
  615. device->cap._BQC = device->cap._BCQ = 0;
  616. }
  617. return 0;
  618. }
  619. /*
  620. * Arg:
  621. * device : video output device (LCD, CRT, ..)
  622. *
  623. * Return Value:
  624. * Maximum brightness level
  625. *
  626. * Allocate and initialize device->brightness.
  627. */
  628. static int
  629. acpi_video_init_brightness(struct acpi_video_device *device)
  630. {
  631. union acpi_object *obj = NULL;
  632. int i, max_level = 0, count = 0, level_ac_battery = 0;
  633. unsigned long long level, level_old;
  634. union acpi_object *o;
  635. struct acpi_video_device_brightness *br = NULL;
  636. int result = -EINVAL;
  637. if (!ACPI_SUCCESS(acpi_video_device_lcd_query_levels(device, &obj))) {
  638. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Could not query available "
  639. "LCD brightness level\n"));
  640. goto out;
  641. }
  642. if (obj->package.count < 2)
  643. goto out;
  644. br = kzalloc(sizeof(*br), GFP_KERNEL);
  645. if (!br) {
  646. printk(KERN_ERR "can't allocate memory\n");
  647. result = -ENOMEM;
  648. goto out;
  649. }
  650. br->levels = kmalloc((obj->package.count + 2) * sizeof *(br->levels),
  651. GFP_KERNEL);
  652. if (!br->levels) {
  653. result = -ENOMEM;
  654. goto out_free;
  655. }
  656. for (i = 0; i < obj->package.count; i++) {
  657. o = (union acpi_object *)&obj->package.elements[i];
  658. if (o->type != ACPI_TYPE_INTEGER) {
  659. printk(KERN_ERR PREFIX "Invalid data\n");
  660. continue;
  661. }
  662. br->levels[count] = (u32) o->integer.value;
  663. if (br->levels[count] > max_level)
  664. max_level = br->levels[count];
  665. count++;
  666. }
  667. /*
  668. * some buggy BIOS don't export the levels
  669. * when machine is on AC/Battery in _BCL package.
  670. * In this case, the first two elements in _BCL packages
  671. * are also supported brightness levels that OS should take care of.
  672. */
  673. for (i = 2; i < count; i++) {
  674. if (br->levels[i] == br->levels[0])
  675. level_ac_battery++;
  676. if (br->levels[i] == br->levels[1])
  677. level_ac_battery++;
  678. }
  679. if (level_ac_battery < 2) {
  680. level_ac_battery = 2 - level_ac_battery;
  681. br->flags._BCL_no_ac_battery_levels = 1;
  682. for (i = (count - 1 + level_ac_battery); i >= 2; i--)
  683. br->levels[i] = br->levels[i - level_ac_battery];
  684. count += level_ac_battery;
  685. } else if (level_ac_battery > 2)
  686. ACPI_ERROR((AE_INFO, "Too many duplicates in _BCL package"));
  687. /* Check if the _BCL package is in a reversed order */
  688. if (max_level == br->levels[2]) {
  689. br->flags._BCL_reversed = 1;
  690. sort(&br->levels[2], count - 2, sizeof(br->levels[2]),
  691. acpi_video_cmp_level, NULL);
  692. } else if (max_level != br->levels[count - 1])
  693. ACPI_ERROR((AE_INFO,
  694. "Found unordered _BCL package"));
  695. br->count = count;
  696. device->brightness = br;
  697. /* _BQC uses INDEX while _BCL uses VALUE in some laptops */
  698. br->curr = level = max_level;
  699. if (!device->cap._BQC)
  700. goto set_level;
  701. result = acpi_video_device_lcd_get_level_current(device,
  702. &level_old, true);
  703. if (result)
  704. goto out_free_levels;
  705. result = acpi_video_bqc_quirk(device, max_level, level_old);
  706. if (result)
  707. goto out_free_levels;
  708. /*
  709. * cap._BQC may get cleared due to _BQC is found to be broken
  710. * in acpi_video_bqc_quirk, so check again here.
  711. */
  712. if (!device->cap._BQC)
  713. goto set_level;
  714. if (use_bios_initial_backlight) {
  715. level = acpi_video_bqc_value_to_level(device, level_old);
  716. /*
  717. * On some buggy laptops, _BQC returns an uninitialized
  718. * value when invoked for the first time, i.e.
  719. * level_old is invalid (no matter whether it's a level
  720. * or an index). Set the backlight to max_level in this case.
  721. */
  722. for (i = 2; i < br->count; i++)
  723. if (level_old == br->levels[i])
  724. break;
  725. if (i == br->count)
  726. level = max_level;
  727. }
  728. set_level:
  729. result = acpi_video_device_lcd_set_level(device, level);
  730. if (result)
  731. goto out_free_levels;
  732. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  733. "found %d brightness levels\n", count - 2));
  734. kfree(obj);
  735. return result;
  736. out_free_levels:
  737. kfree(br->levels);
  738. out_free:
  739. kfree(br);
  740. out:
  741. device->brightness = NULL;
  742. kfree(obj);
  743. return result;
  744. }
  745. /*
  746. * Arg:
  747. * device : video output device (LCD, CRT, ..)
  748. *
  749. * Return Value:
  750. * None
  751. *
  752. * Find out all required AML methods defined under the output
  753. * device.
  754. */
  755. static void acpi_video_device_find_cap(struct acpi_video_device *device)
  756. {
  757. if (acpi_has_method(device->dev->handle, "_ADR"))
  758. device->cap._ADR = 1;
  759. if (acpi_has_method(device->dev->handle, "_BCL"))
  760. device->cap._BCL = 1;
  761. if (acpi_has_method(device->dev->handle, "_BCM"))
  762. device->cap._BCM = 1;
  763. if (acpi_has_method(device->dev->handle, "_BQC")) {
  764. device->cap._BQC = 1;
  765. } else if (acpi_has_method(device->dev->handle, "_BCQ")) {
  766. printk(KERN_WARNING FW_BUG "_BCQ is used instead of _BQC\n");
  767. device->cap._BCQ = 1;
  768. }
  769. if (acpi_has_method(device->dev->handle, "_DDC"))
  770. device->cap._DDC = 1;
  771. if (acpi_video_backlight_support()) {
  772. struct backlight_properties props;
  773. struct pci_dev *pdev;
  774. acpi_handle acpi_parent;
  775. struct device *parent = NULL;
  776. int result;
  777. static int count;
  778. char *name;
  779. result = acpi_video_init_brightness(device);
  780. if (result)
  781. return;
  782. name = kasprintf(GFP_KERNEL, "acpi_video%d", count);
  783. if (!name)
  784. return;
  785. count++;
  786. acpi_get_parent(device->dev->handle, &acpi_parent);
  787. pdev = acpi_get_pci_dev(acpi_parent);
  788. if (pdev) {
  789. parent = &pdev->dev;
  790. pci_dev_put(pdev);
  791. }
  792. memset(&props, 0, sizeof(struct backlight_properties));
  793. props.type = BACKLIGHT_FIRMWARE;
  794. props.max_brightness = device->brightness->count - 3;
  795. device->backlight = backlight_device_register(name,
  796. parent,
  797. device,
  798. &acpi_backlight_ops,
  799. &props);
  800. kfree(name);
  801. if (IS_ERR(device->backlight))
  802. return;
  803. /*
  804. * Save current brightness level in case we have to restore it
  805. * before acpi_video_device_lcd_set_level() is called next time.
  806. */
  807. device->backlight->props.brightness =
  808. acpi_video_get_brightness(device->backlight);
  809. device->cooling_dev = thermal_cooling_device_register("LCD",
  810. device->dev, &video_cooling_ops);
  811. if (IS_ERR(device->cooling_dev)) {
  812. /*
  813. * Set cooling_dev to NULL so we don't crash trying to
  814. * free it.
  815. * Also, why the hell we are returning early and
  816. * not attempt to register video output if cooling
  817. * device registration failed?
  818. * -- dtor
  819. */
  820. device->cooling_dev = NULL;
  821. return;
  822. }
  823. dev_info(&device->dev->dev, "registered as cooling_device%d\n",
  824. device->cooling_dev->id);
  825. result = sysfs_create_link(&device->dev->dev.kobj,
  826. &device->cooling_dev->device.kobj,
  827. "thermal_cooling");
  828. if (result)
  829. printk(KERN_ERR PREFIX "Create sysfs link\n");
  830. result = sysfs_create_link(&device->cooling_dev->device.kobj,
  831. &device->dev->dev.kobj, "device");
  832. if (result)
  833. printk(KERN_ERR PREFIX "Create sysfs link\n");
  834. }
  835. }
  836. /*
  837. * Arg:
  838. * device : video output device (VGA)
  839. *
  840. * Return Value:
  841. * None
  842. *
  843. * Find out all required AML methods defined under the video bus device.
  844. */
  845. static void acpi_video_bus_find_cap(struct acpi_video_bus *video)
  846. {
  847. if (acpi_has_method(video->device->handle, "_DOS"))
  848. video->cap._DOS = 1;
  849. if (acpi_has_method(video->device->handle, "_DOD"))
  850. video->cap._DOD = 1;
  851. if (acpi_has_method(video->device->handle, "_ROM"))
  852. video->cap._ROM = 1;
  853. if (acpi_has_method(video->device->handle, "_GPD"))
  854. video->cap._GPD = 1;
  855. if (acpi_has_method(video->device->handle, "_SPD"))
  856. video->cap._SPD = 1;
  857. if (acpi_has_method(video->device->handle, "_VPO"))
  858. video->cap._VPO = 1;
  859. }
  860. /*
  861. * Check whether the video bus device has required AML method to
  862. * support the desired features
  863. */
  864. static int acpi_video_bus_check(struct acpi_video_bus *video)
  865. {
  866. acpi_status status = -ENOENT;
  867. struct pci_dev *dev;
  868. if (!video)
  869. return -EINVAL;
  870. dev = acpi_get_pci_dev(video->device->handle);
  871. if (!dev)
  872. return -ENODEV;
  873. pci_dev_put(dev);
  874. /*
  875. * Since there is no HID, CID and so on for VGA driver, we have
  876. * to check well known required nodes.
  877. */
  878. /* Does this device support video switching? */
  879. if (video->cap._DOS || video->cap._DOD) {
  880. if (!video->cap._DOS) {
  881. printk(KERN_WARNING FW_BUG
  882. "ACPI(%s) defines _DOD but not _DOS\n",
  883. acpi_device_bid(video->device));
  884. }
  885. video->flags.multihead = 1;
  886. status = 0;
  887. }
  888. /* Does this device support retrieving a video ROM? */
  889. if (video->cap._ROM) {
  890. video->flags.rom = 1;
  891. status = 0;
  892. }
  893. /* Does this device support configuring which video device to POST? */
  894. if (video->cap._GPD && video->cap._SPD && video->cap._VPO) {
  895. video->flags.post = 1;
  896. status = 0;
  897. }
  898. return status;
  899. }
  900. /*
  901. * --------------------------------------------------------------------------
  902. * Driver Interface
  903. * --------------------------------------------------------------------------
  904. */
  905. /* device interface */
  906. static struct acpi_video_device_attrib *
  907. acpi_video_get_device_attr(struct acpi_video_bus *video, unsigned long device_id)
  908. {
  909. struct acpi_video_enumerated_device *ids;
  910. int i;
  911. for (i = 0; i < video->attached_count; i++) {
  912. ids = &video->attached_array[i];
  913. if ((ids->value.int_val & 0xffff) == device_id)
  914. return &ids->value.attrib;
  915. }
  916. return NULL;
  917. }
  918. static int
  919. acpi_video_get_device_type(struct acpi_video_bus *video,
  920. unsigned long device_id)
  921. {
  922. struct acpi_video_enumerated_device *ids;
  923. int i;
  924. for (i = 0; i < video->attached_count; i++) {
  925. ids = &video->attached_array[i];
  926. if ((ids->value.int_val & 0xffff) == device_id)
  927. return ids->value.int_val;
  928. }
  929. return 0;
  930. }
  931. static int
  932. acpi_video_bus_get_one_device(struct acpi_device *device,
  933. struct acpi_video_bus *video)
  934. {
  935. unsigned long long device_id;
  936. int status, device_type;
  937. struct acpi_video_device *data;
  938. struct acpi_video_device_attrib *attribute;
  939. status =
  940. acpi_evaluate_integer(device->handle, "_ADR", NULL, &device_id);
  941. /* Some device omits _ADR, we skip them instead of fail */
  942. if (ACPI_FAILURE(status))
  943. return 0;
  944. data = kzalloc(sizeof(struct acpi_video_device), GFP_KERNEL);
  945. if (!data)
  946. return -ENOMEM;
  947. strcpy(acpi_device_name(device), ACPI_VIDEO_DEVICE_NAME);
  948. strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
  949. device->driver_data = data;
  950. data->device_id = device_id;
  951. data->video = video;
  952. data->dev = device;
  953. attribute = acpi_video_get_device_attr(video, device_id);
  954. if (attribute && attribute->device_id_scheme) {
  955. switch (attribute->display_type) {
  956. case ACPI_VIDEO_DISPLAY_CRT:
  957. data->flags.crt = 1;
  958. break;
  959. case ACPI_VIDEO_DISPLAY_TV:
  960. data->flags.tvout = 1;
  961. break;
  962. case ACPI_VIDEO_DISPLAY_DVI:
  963. data->flags.dvi = 1;
  964. break;
  965. case ACPI_VIDEO_DISPLAY_LCD:
  966. data->flags.lcd = 1;
  967. break;
  968. default:
  969. data->flags.unknown = 1;
  970. break;
  971. }
  972. if (attribute->bios_can_detect)
  973. data->flags.bios = 1;
  974. } else {
  975. /* Check for legacy IDs */
  976. device_type = acpi_video_get_device_type(video, device_id);
  977. /* Ignore bits 16 and 18-20 */
  978. switch (device_type & 0xffe2ffff) {
  979. case ACPI_VIDEO_DISPLAY_LEGACY_MONITOR:
  980. data->flags.crt = 1;
  981. break;
  982. case ACPI_VIDEO_DISPLAY_LEGACY_PANEL:
  983. data->flags.lcd = 1;
  984. break;
  985. case ACPI_VIDEO_DISPLAY_LEGACY_TV:
  986. data->flags.tvout = 1;
  987. break;
  988. default:
  989. data->flags.unknown = 1;
  990. }
  991. }
  992. acpi_video_device_bind(video, data);
  993. acpi_video_device_find_cap(data);
  994. status = acpi_install_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
  995. acpi_video_device_notify, data);
  996. if (ACPI_FAILURE(status))
  997. dev_err(&device->dev, "Error installing notify handler\n");
  998. else
  999. data->flags.notify = 1;
  1000. mutex_lock(&video->device_list_lock);
  1001. list_add_tail(&data->entry, &video->video_device_list);
  1002. mutex_unlock(&video->device_list_lock);
  1003. return status;
  1004. }
  1005. /*
  1006. * Arg:
  1007. * video : video bus device
  1008. *
  1009. * Return:
  1010. * none
  1011. *
  1012. * Enumerate the video device list of the video bus,
  1013. * bind the ids with the corresponding video devices
  1014. * under the video bus.
  1015. */
  1016. static void acpi_video_device_rebind(struct acpi_video_bus *video)
  1017. {
  1018. struct acpi_video_device *dev;
  1019. mutex_lock(&video->device_list_lock);
  1020. list_for_each_entry(dev, &video->video_device_list, entry)
  1021. acpi_video_device_bind(video, dev);
  1022. mutex_unlock(&video->device_list_lock);
  1023. }
  1024. /*
  1025. * Arg:
  1026. * video : video bus device
  1027. * device : video output device under the video
  1028. * bus
  1029. *
  1030. * Return:
  1031. * none
  1032. *
  1033. * Bind the ids with the corresponding video devices
  1034. * under the video bus.
  1035. */
  1036. static void
  1037. acpi_video_device_bind(struct acpi_video_bus *video,
  1038. struct acpi_video_device *device)
  1039. {
  1040. struct acpi_video_enumerated_device *ids;
  1041. int i;
  1042. for (i = 0; i < video->attached_count; i++) {
  1043. ids = &video->attached_array[i];
  1044. if (device->device_id == (ids->value.int_val & 0xffff)) {
  1045. ids->bind_info = device;
  1046. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "device_bind %d\n", i));
  1047. }
  1048. }
  1049. }
  1050. /*
  1051. * Arg:
  1052. * video : video bus device
  1053. *
  1054. * Return:
  1055. * < 0 : error
  1056. *
  1057. * Call _DOD to enumerate all devices attached to display adapter
  1058. *
  1059. */
  1060. static int acpi_video_device_enumerate(struct acpi_video_bus *video)
  1061. {
  1062. int status;
  1063. int count;
  1064. int i;
  1065. struct acpi_video_enumerated_device *active_list;
  1066. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  1067. union acpi_object *dod = NULL;
  1068. union acpi_object *obj;
  1069. status = acpi_evaluate_object(video->device->handle, "_DOD", NULL, &buffer);
  1070. if (!ACPI_SUCCESS(status)) {
  1071. ACPI_EXCEPTION((AE_INFO, status, "Evaluating _DOD"));
  1072. return status;
  1073. }
  1074. dod = buffer.pointer;
  1075. if (!dod || (dod->type != ACPI_TYPE_PACKAGE)) {
  1076. ACPI_EXCEPTION((AE_INFO, status, "Invalid _DOD data"));
  1077. status = -EFAULT;
  1078. goto out;
  1079. }
  1080. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d video heads in _DOD\n",
  1081. dod->package.count));
  1082. active_list = kcalloc(1 + dod->package.count,
  1083. sizeof(struct acpi_video_enumerated_device),
  1084. GFP_KERNEL);
  1085. if (!active_list) {
  1086. status = -ENOMEM;
  1087. goto out;
  1088. }
  1089. count = 0;
  1090. for (i = 0; i < dod->package.count; i++) {
  1091. obj = &dod->package.elements[i];
  1092. if (obj->type != ACPI_TYPE_INTEGER) {
  1093. printk(KERN_ERR PREFIX
  1094. "Invalid _DOD data in element %d\n", i);
  1095. continue;
  1096. }
  1097. active_list[count].value.int_val = obj->integer.value;
  1098. active_list[count].bind_info = NULL;
  1099. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "dod element[%d] = %d\n", i,
  1100. (int)obj->integer.value));
  1101. count++;
  1102. }
  1103. kfree(video->attached_array);
  1104. video->attached_array = active_list;
  1105. video->attached_count = count;
  1106. out:
  1107. kfree(buffer.pointer);
  1108. return status;
  1109. }
  1110. static int
  1111. acpi_video_get_next_level(struct acpi_video_device *device,
  1112. u32 level_current, u32 event)
  1113. {
  1114. int min, max, min_above, max_below, i, l, delta = 255;
  1115. max = max_below = 0;
  1116. min = min_above = 255;
  1117. /* Find closest level to level_current */
  1118. for (i = 2; i < device->brightness->count; i++) {
  1119. l = device->brightness->levels[i];
  1120. if (abs(l - level_current) < abs(delta)) {
  1121. delta = l - level_current;
  1122. if (!delta)
  1123. break;
  1124. }
  1125. }
  1126. /* Ajust level_current to closest available level */
  1127. level_current += delta;
  1128. for (i = 2; i < device->brightness->count; i++) {
  1129. l = device->brightness->levels[i];
  1130. if (l < min)
  1131. min = l;
  1132. if (l > max)
  1133. max = l;
  1134. if (l < min_above && l > level_current)
  1135. min_above = l;
  1136. if (l > max_below && l < level_current)
  1137. max_below = l;
  1138. }
  1139. switch (event) {
  1140. case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS:
  1141. return (level_current < max) ? min_above : min;
  1142. case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS:
  1143. return (level_current < max) ? min_above : max;
  1144. case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS:
  1145. return (level_current > min) ? max_below : min;
  1146. case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS:
  1147. case ACPI_VIDEO_NOTIFY_DISPLAY_OFF:
  1148. return 0;
  1149. default:
  1150. return level_current;
  1151. }
  1152. }
  1153. static int
  1154. acpi_video_switch_brightness(struct acpi_video_device *device, int event)
  1155. {
  1156. unsigned long long level_current, level_next;
  1157. int result = -EINVAL;
  1158. /* no warning message if acpi_backlight=vendor is used */
  1159. if (!acpi_video_backlight_support())
  1160. return 0;
  1161. if (!device->brightness)
  1162. goto out;
  1163. result = acpi_video_device_lcd_get_level_current(device,
  1164. &level_current,
  1165. false);
  1166. if (result)
  1167. goto out;
  1168. level_next = acpi_video_get_next_level(device, level_current, event);
  1169. result = acpi_video_device_lcd_set_level(device, level_next);
  1170. if (!result)
  1171. backlight_force_update(device->backlight,
  1172. BACKLIGHT_UPDATE_HOTKEY);
  1173. out:
  1174. if (result)
  1175. printk(KERN_ERR PREFIX "Failed to switch the brightness\n");
  1176. return result;
  1177. }
  1178. int acpi_video_get_edid(struct acpi_device *device, int type, int device_id,
  1179. void **edid)
  1180. {
  1181. struct acpi_video_bus *video;
  1182. struct acpi_video_device *video_device;
  1183. union acpi_object *buffer = NULL;
  1184. acpi_status status;
  1185. int i, length;
  1186. if (!device || !acpi_driver_data(device))
  1187. return -EINVAL;
  1188. video = acpi_driver_data(device);
  1189. for (i = 0; i < video->attached_count; i++) {
  1190. video_device = video->attached_array[i].bind_info;
  1191. length = 256;
  1192. if (!video_device)
  1193. continue;
  1194. if (!video_device->cap._DDC)
  1195. continue;
  1196. if (type) {
  1197. switch (type) {
  1198. case ACPI_VIDEO_DISPLAY_CRT:
  1199. if (!video_device->flags.crt)
  1200. continue;
  1201. break;
  1202. case ACPI_VIDEO_DISPLAY_TV:
  1203. if (!video_device->flags.tvout)
  1204. continue;
  1205. break;
  1206. case ACPI_VIDEO_DISPLAY_DVI:
  1207. if (!video_device->flags.dvi)
  1208. continue;
  1209. break;
  1210. case ACPI_VIDEO_DISPLAY_LCD:
  1211. if (!video_device->flags.lcd)
  1212. continue;
  1213. break;
  1214. }
  1215. } else if (video_device->device_id != device_id) {
  1216. continue;
  1217. }
  1218. status = acpi_video_device_EDID(video_device, &buffer, length);
  1219. if (ACPI_FAILURE(status) || !buffer ||
  1220. buffer->type != ACPI_TYPE_BUFFER) {
  1221. length = 128;
  1222. status = acpi_video_device_EDID(video_device, &buffer,
  1223. length);
  1224. if (ACPI_FAILURE(status) || !buffer ||
  1225. buffer->type != ACPI_TYPE_BUFFER) {
  1226. continue;
  1227. }
  1228. }
  1229. *edid = buffer->buffer.pointer;
  1230. return length;
  1231. }
  1232. return -ENODEV;
  1233. }
  1234. EXPORT_SYMBOL(acpi_video_get_edid);
  1235. static int
  1236. acpi_video_bus_get_devices(struct acpi_video_bus *video,
  1237. struct acpi_device *device)
  1238. {
  1239. int status = 0;
  1240. struct acpi_device *dev;
  1241. /*
  1242. * There are systems where video module known to work fine regardless
  1243. * of broken _DOD and ignoring returned value here doesn't cause
  1244. * any issues later.
  1245. */
  1246. acpi_video_device_enumerate(video);
  1247. list_for_each_entry(dev, &device->children, node) {
  1248. status = acpi_video_bus_get_one_device(dev, video);
  1249. if (status) {
  1250. dev_err(&dev->dev, "Can't attach device\n");
  1251. break;
  1252. }
  1253. }
  1254. return status;
  1255. }
  1256. static int acpi_video_bus_put_one_device(struct acpi_video_device *device)
  1257. {
  1258. acpi_status status;
  1259. if (!device || !device->video)
  1260. return -ENOENT;
  1261. if (device->flags.notify) {
  1262. status = acpi_remove_notify_handler(device->dev->handle,
  1263. ACPI_DEVICE_NOTIFY, acpi_video_device_notify);
  1264. if (ACPI_FAILURE(status))
  1265. dev_err(&device->dev->dev,
  1266. "Can't remove video notify handler\n");
  1267. }
  1268. if (device->backlight) {
  1269. backlight_device_unregister(device->backlight);
  1270. device->backlight = NULL;
  1271. }
  1272. if (device->cooling_dev) {
  1273. sysfs_remove_link(&device->dev->dev.kobj,
  1274. "thermal_cooling");
  1275. sysfs_remove_link(&device->cooling_dev->device.kobj,
  1276. "device");
  1277. thermal_cooling_device_unregister(device->cooling_dev);
  1278. device->cooling_dev = NULL;
  1279. }
  1280. return 0;
  1281. }
  1282. static int acpi_video_bus_put_devices(struct acpi_video_bus *video)
  1283. {
  1284. int status;
  1285. struct acpi_video_device *dev, *next;
  1286. mutex_lock(&video->device_list_lock);
  1287. list_for_each_entry_safe(dev, next, &video->video_device_list, entry) {
  1288. status = acpi_video_bus_put_one_device(dev);
  1289. if (ACPI_FAILURE(status))
  1290. printk(KERN_WARNING PREFIX
  1291. "hhuuhhuu bug in acpi video driver.\n");
  1292. if (dev->brightness) {
  1293. kfree(dev->brightness->levels);
  1294. kfree(dev->brightness);
  1295. }
  1296. list_del(&dev->entry);
  1297. kfree(dev);
  1298. }
  1299. mutex_unlock(&video->device_list_lock);
  1300. return 0;
  1301. }
  1302. /* acpi_video interface */
  1303. /*
  1304. * Win8 requires setting bit2 of _DOS to let firmware know it shouldn't
  1305. * preform any automatic brightness change on receiving a notification.
  1306. */
  1307. static int acpi_video_bus_start_devices(struct acpi_video_bus *video)
  1308. {
  1309. return acpi_video_bus_DOS(video, 0,
  1310. acpi_video_backlight_quirks() ? 1 : 0);
  1311. }
  1312. static int acpi_video_bus_stop_devices(struct acpi_video_bus *video)
  1313. {
  1314. return acpi_video_bus_DOS(video, 0,
  1315. acpi_video_backlight_quirks() ? 0 : 1);
  1316. }
  1317. static void acpi_video_bus_notify(struct acpi_device *device, u32 event)
  1318. {
  1319. struct acpi_video_bus *video = acpi_driver_data(device);
  1320. struct input_dev *input;
  1321. int keycode = 0;
  1322. if (!video)
  1323. return;
  1324. input = video->input;
  1325. switch (event) {
  1326. case ACPI_VIDEO_NOTIFY_SWITCH: /* User requested a switch,
  1327. * most likely via hotkey. */
  1328. keycode = KEY_SWITCHVIDEOMODE;
  1329. break;
  1330. case ACPI_VIDEO_NOTIFY_PROBE: /* User plugged in or removed a video
  1331. * connector. */
  1332. acpi_video_device_enumerate(video);
  1333. acpi_video_device_rebind(video);
  1334. keycode = KEY_SWITCHVIDEOMODE;
  1335. break;
  1336. case ACPI_VIDEO_NOTIFY_CYCLE: /* Cycle Display output hotkey pressed. */
  1337. keycode = KEY_SWITCHVIDEOMODE;
  1338. break;
  1339. case ACPI_VIDEO_NOTIFY_NEXT_OUTPUT: /* Next Display output hotkey pressed. */
  1340. keycode = KEY_VIDEO_NEXT;
  1341. break;
  1342. case ACPI_VIDEO_NOTIFY_PREV_OUTPUT: /* previous Display output hotkey pressed. */
  1343. keycode = KEY_VIDEO_PREV;
  1344. break;
  1345. default:
  1346. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  1347. "Unsupported event [0x%x]\n", event));
  1348. break;
  1349. }
  1350. if (acpi_notifier_call_chain(device, event, 0))
  1351. /* Something vetoed the keypress. */
  1352. keycode = 0;
  1353. if (keycode) {
  1354. input_report_key(input, keycode, 1);
  1355. input_sync(input);
  1356. input_report_key(input, keycode, 0);
  1357. input_sync(input);
  1358. }
  1359. return;
  1360. }
  1361. static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data)
  1362. {
  1363. struct acpi_video_device *video_device = data;
  1364. struct acpi_device *device = NULL;
  1365. struct acpi_video_bus *bus;
  1366. struct input_dev *input;
  1367. int keycode = 0;
  1368. if (!video_device)
  1369. return;
  1370. device = video_device->dev;
  1371. bus = video_device->video;
  1372. input = bus->input;
  1373. switch (event) {
  1374. case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS: /* Cycle brightness */
  1375. if (brightness_switch_enabled)
  1376. acpi_video_switch_brightness(video_device, event);
  1377. keycode = KEY_BRIGHTNESS_CYCLE;
  1378. break;
  1379. case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS: /* Increase brightness */
  1380. if (brightness_switch_enabled)
  1381. acpi_video_switch_brightness(video_device, event);
  1382. keycode = KEY_BRIGHTNESSUP;
  1383. break;
  1384. case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS: /* Decrease brightness */
  1385. if (brightness_switch_enabled)
  1386. acpi_video_switch_brightness(video_device, event);
  1387. keycode = KEY_BRIGHTNESSDOWN;
  1388. break;
  1389. case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS: /* zero brightness */
  1390. if (brightness_switch_enabled)
  1391. acpi_video_switch_brightness(video_device, event);
  1392. keycode = KEY_BRIGHTNESS_ZERO;
  1393. break;
  1394. case ACPI_VIDEO_NOTIFY_DISPLAY_OFF: /* display device off */
  1395. if (brightness_switch_enabled)
  1396. acpi_video_switch_brightness(video_device, event);
  1397. keycode = KEY_DISPLAY_OFF;
  1398. break;
  1399. default:
  1400. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  1401. "Unsupported event [0x%x]\n", event));
  1402. break;
  1403. }
  1404. acpi_notifier_call_chain(device, event, 0);
  1405. if (keycode) {
  1406. input_report_key(input, keycode, 1);
  1407. input_sync(input);
  1408. input_report_key(input, keycode, 0);
  1409. input_sync(input);
  1410. }
  1411. return;
  1412. }
  1413. static int acpi_video_resume(struct notifier_block *nb,
  1414. unsigned long val, void *ign)
  1415. {
  1416. struct acpi_video_bus *video;
  1417. struct acpi_video_device *video_device;
  1418. int i;
  1419. switch (val) {
  1420. case PM_HIBERNATION_PREPARE:
  1421. case PM_SUSPEND_PREPARE:
  1422. case PM_RESTORE_PREPARE:
  1423. return NOTIFY_DONE;
  1424. }
  1425. video = container_of(nb, struct acpi_video_bus, pm_nb);
  1426. dev_info(&video->device->dev, "Restoring backlight state\n");
  1427. for (i = 0; i < video->attached_count; i++) {
  1428. video_device = video->attached_array[i].bind_info;
  1429. if (video_device && video_device->backlight)
  1430. acpi_video_set_brightness(video_device->backlight);
  1431. }
  1432. return NOTIFY_OK;
  1433. }
  1434. static acpi_status
  1435. acpi_video_bus_match(acpi_handle handle, u32 level, void *context,
  1436. void **return_value)
  1437. {
  1438. struct acpi_device *device = context;
  1439. struct acpi_device *sibling;
  1440. int result;
  1441. if (handle == device->handle)
  1442. return AE_CTRL_TERMINATE;
  1443. result = acpi_bus_get_device(handle, &sibling);
  1444. if (result)
  1445. return AE_OK;
  1446. if (!strcmp(acpi_device_name(sibling), ACPI_VIDEO_BUS_NAME))
  1447. return AE_ALREADY_EXISTS;
  1448. return AE_OK;
  1449. }
  1450. static int instance;
  1451. static int acpi_video_bus_add(struct acpi_device *device)
  1452. {
  1453. struct acpi_video_bus *video;
  1454. struct input_dev *input;
  1455. int error;
  1456. acpi_status status;
  1457. status = acpi_walk_namespace(ACPI_TYPE_DEVICE,
  1458. device->parent->handle, 1,
  1459. acpi_video_bus_match, NULL,
  1460. device, NULL);
  1461. if (status == AE_ALREADY_EXISTS) {
  1462. printk(KERN_WARNING FW_BUG
  1463. "Duplicate ACPI video bus devices for the"
  1464. " same VGA controller, please try module "
  1465. "parameter \"video.allow_duplicates=1\""
  1466. "if the current driver doesn't work.\n");
  1467. if (!allow_duplicates)
  1468. return -ENODEV;
  1469. }
  1470. video = kzalloc(sizeof(struct acpi_video_bus), GFP_KERNEL);
  1471. if (!video)
  1472. return -ENOMEM;
  1473. /* a hack to fix the duplicate name "VID" problem on T61 */
  1474. if (!strcmp(device->pnp.bus_id, "VID")) {
  1475. if (instance)
  1476. device->pnp.bus_id[3] = '0' + instance;
  1477. instance++;
  1478. }
  1479. /* a hack to fix the duplicate name "VGA" problem on Pa 3553 */
  1480. if (!strcmp(device->pnp.bus_id, "VGA")) {
  1481. if (instance)
  1482. device->pnp.bus_id[3] = '0' + instance;
  1483. instance++;
  1484. }
  1485. video->device = device;
  1486. strcpy(acpi_device_name(device), ACPI_VIDEO_BUS_NAME);
  1487. strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
  1488. device->driver_data = video;
  1489. acpi_video_bus_find_cap(video);
  1490. error = acpi_video_bus_check(video);
  1491. if (error)
  1492. goto err_free_video;
  1493. mutex_init(&video->device_list_lock);
  1494. INIT_LIST_HEAD(&video->video_device_list);
  1495. error = acpi_video_bus_get_devices(video, device);
  1496. if (error)
  1497. goto err_put_video;
  1498. video->input = input = input_allocate_device();
  1499. if (!input) {
  1500. error = -ENOMEM;
  1501. goto err_put_video;
  1502. }
  1503. error = acpi_video_bus_start_devices(video);
  1504. if (error)
  1505. goto err_free_input_dev;
  1506. snprintf(video->phys, sizeof(video->phys),
  1507. "%s/video/input0", acpi_device_hid(video->device));
  1508. input->name = acpi_device_name(video->device);
  1509. input->phys = video->phys;
  1510. input->id.bustype = BUS_HOST;
  1511. input->id.product = 0x06;
  1512. input->dev.parent = &device->dev;
  1513. input->evbit[0] = BIT(EV_KEY);
  1514. set_bit(KEY_SWITCHVIDEOMODE, input->keybit);
  1515. set_bit(KEY_VIDEO_NEXT, input->keybit);
  1516. set_bit(KEY_VIDEO_PREV, input->keybit);
  1517. set_bit(KEY_BRIGHTNESS_CYCLE, input->keybit);
  1518. set_bit(KEY_BRIGHTNESSUP, input->keybit);
  1519. set_bit(KEY_BRIGHTNESSDOWN, input->keybit);
  1520. set_bit(KEY_BRIGHTNESS_ZERO, input->keybit);
  1521. set_bit(KEY_DISPLAY_OFF, input->keybit);
  1522. printk(KERN_INFO PREFIX "%s [%s] (multi-head: %s rom: %s post: %s)\n",
  1523. ACPI_VIDEO_DEVICE_NAME, acpi_device_bid(device),
  1524. video->flags.multihead ? "yes" : "no",
  1525. video->flags.rom ? "yes" : "no",
  1526. video->flags.post ? "yes" : "no");
  1527. video->pm_nb.notifier_call = acpi_video_resume;
  1528. video->pm_nb.priority = 0;
  1529. error = register_pm_notifier(&video->pm_nb);
  1530. if (error)
  1531. goto err_stop_video;
  1532. error = input_register_device(input);
  1533. if (error)
  1534. goto err_unregister_pm_notifier;
  1535. return 0;
  1536. err_unregister_pm_notifier:
  1537. unregister_pm_notifier(&video->pm_nb);
  1538. err_stop_video:
  1539. acpi_video_bus_stop_devices(video);
  1540. err_free_input_dev:
  1541. input_free_device(input);
  1542. err_put_video:
  1543. acpi_video_bus_put_devices(video);
  1544. kfree(video->attached_array);
  1545. err_free_video:
  1546. kfree(video);
  1547. device->driver_data = NULL;
  1548. return error;
  1549. }
  1550. static int acpi_video_bus_remove(struct acpi_device *device)
  1551. {
  1552. struct acpi_video_bus *video = NULL;
  1553. if (!device || !acpi_driver_data(device))
  1554. return -EINVAL;
  1555. video = acpi_driver_data(device);
  1556. unregister_pm_notifier(&video->pm_nb);
  1557. acpi_video_bus_stop_devices(video);
  1558. acpi_video_bus_put_devices(video);
  1559. input_unregister_device(video->input);
  1560. kfree(video->attached_array);
  1561. kfree(video);
  1562. return 0;
  1563. }
  1564. static int __init is_i740(struct pci_dev *dev)
  1565. {
  1566. if (dev->device == 0x00D1)
  1567. return 1;
  1568. if (dev->device == 0x7000)
  1569. return 1;
  1570. return 0;
  1571. }
  1572. static int __init intel_opregion_present(void)
  1573. {
  1574. int opregion = 0;
  1575. struct pci_dev *dev = NULL;
  1576. u32 address;
  1577. for_each_pci_dev(dev) {
  1578. if ((dev->class >> 8) != PCI_CLASS_DISPLAY_VGA)
  1579. continue;
  1580. if (dev->vendor != PCI_VENDOR_ID_INTEL)
  1581. continue;
  1582. /* We don't want to poke around undefined i740 registers */
  1583. if (is_i740(dev))
  1584. continue;
  1585. pci_read_config_dword(dev, 0xfc, &address);
  1586. if (!address)
  1587. continue;
  1588. opregion = 1;
  1589. }
  1590. return opregion;
  1591. }
  1592. int acpi_video_register(void)
  1593. {
  1594. int result = 0;
  1595. if (register_count) {
  1596. /*
  1597. * if the function of acpi_video_register is already called,
  1598. * don't register the acpi_vide_bus again and return no error.
  1599. */
  1600. return 0;
  1601. }
  1602. result = acpi_bus_register_driver(&acpi_video_bus);
  1603. if (result < 0)
  1604. return -ENODEV;
  1605. /*
  1606. * When the acpi_video_bus is loaded successfully, increase
  1607. * the counter reference.
  1608. */
  1609. register_count = 1;
  1610. return 0;
  1611. }
  1612. EXPORT_SYMBOL(acpi_video_register);
  1613. void acpi_video_unregister(void)
  1614. {
  1615. if (!register_count) {
  1616. /*
  1617. * If the acpi video bus is already unloaded, don't
  1618. * unload it again and return directly.
  1619. */
  1620. return;
  1621. }
  1622. acpi_bus_unregister_driver(&acpi_video_bus);
  1623. register_count = 0;
  1624. return;
  1625. }
  1626. EXPORT_SYMBOL(acpi_video_unregister);
  1627. /*
  1628. * This is kind of nasty. Hardware using Intel chipsets may require
  1629. * the video opregion code to be run first in order to initialise
  1630. * state before any ACPI video calls are made. To handle this we defer
  1631. * registration of the video class until the opregion code has run.
  1632. */
  1633. static int __init acpi_video_init(void)
  1634. {
  1635. dmi_check_system(video_dmi_table);
  1636. if (intel_opregion_present())
  1637. return 0;
  1638. return acpi_video_register();
  1639. }
  1640. static void __exit acpi_video_exit(void)
  1641. {
  1642. acpi_video_unregister();
  1643. return;
  1644. }
  1645. module_init(acpi_video_init);
  1646. module_exit(acpi_video_exit);