video.c 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949
  1. /*
  2. * video.c - ACPI Video Driver ($Revision:$)
  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 = 0;
  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 _BCL_use_index:1; /* levels in _BCL are index values */
  167. u8 _BCM_use_index:1; /* input of _BCM is an index value */
  168. u8 _BQC_use_index:1; /* _BQC returns an index value */
  169. };
  170. struct acpi_video_device_brightness {
  171. int curr;
  172. int count;
  173. int *levels;
  174. struct acpi_video_brightness_flags flags;
  175. };
  176. struct acpi_video_device {
  177. unsigned long device_id;
  178. struct acpi_video_device_flags flags;
  179. struct acpi_video_device_cap cap;
  180. struct list_head entry;
  181. struct acpi_video_bus *video;
  182. struct acpi_device *dev;
  183. struct acpi_video_device_brightness *brightness;
  184. struct backlight_device *backlight;
  185. struct thermal_cooling_device *cooling_dev;
  186. };
  187. static const char device_decode[][30] = {
  188. "motherboard VGA device",
  189. "PCI VGA device",
  190. "AGP VGA device",
  191. "UNKNOWN",
  192. };
  193. static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data);
  194. static void acpi_video_device_rebind(struct acpi_video_bus *video);
  195. static void acpi_video_device_bind(struct acpi_video_bus *video,
  196. struct acpi_video_device *device);
  197. static int acpi_video_device_enumerate(struct acpi_video_bus *video);
  198. static int acpi_video_device_lcd_set_level(struct acpi_video_device *device,
  199. int level);
  200. static int acpi_video_device_lcd_get_level_current(
  201. struct acpi_video_device *device,
  202. unsigned long long *level, bool raw);
  203. static int acpi_video_get_next_level(struct acpi_video_device *device,
  204. u32 level_current, u32 event);
  205. static int acpi_video_switch_brightness(struct acpi_video_device *device,
  206. int event);
  207. /*backlight device sysfs support*/
  208. static int acpi_video_get_brightness(struct backlight_device *bd)
  209. {
  210. unsigned long long cur_level;
  211. int i;
  212. struct acpi_video_device *vd =
  213. (struct acpi_video_device *)bl_get_data(bd);
  214. if (acpi_video_device_lcd_get_level_current(vd, &cur_level, false))
  215. return -EINVAL;
  216. for (i = 2; i < vd->brightness->count; i++) {
  217. if (vd->brightness->levels[i] == cur_level)
  218. /* The first two entries are special - see page 575
  219. of the ACPI spec 3.0 */
  220. return i-2;
  221. }
  222. return 0;
  223. }
  224. static int acpi_video_set_brightness(struct backlight_device *bd)
  225. {
  226. int request_level = bd->props.brightness + 2;
  227. struct acpi_video_device *vd =
  228. (struct acpi_video_device *)bl_get_data(bd);
  229. return acpi_video_device_lcd_set_level(vd,
  230. vd->brightness->levels[request_level]);
  231. }
  232. static const struct backlight_ops acpi_backlight_ops = {
  233. .get_brightness = acpi_video_get_brightness,
  234. .update_status = acpi_video_set_brightness,
  235. };
  236. /* thermal cooling device callbacks */
  237. static int video_get_max_state(struct thermal_cooling_device *cooling_dev, unsigned
  238. long *state)
  239. {
  240. struct acpi_device *device = cooling_dev->devdata;
  241. struct acpi_video_device *video = acpi_driver_data(device);
  242. *state = video->brightness->count - 3;
  243. return 0;
  244. }
  245. static int video_get_cur_state(struct thermal_cooling_device *cooling_dev, unsigned
  246. long *state)
  247. {
  248. struct acpi_device *device = cooling_dev->devdata;
  249. struct acpi_video_device *video = acpi_driver_data(device);
  250. unsigned long long level;
  251. int offset;
  252. if (acpi_video_device_lcd_get_level_current(video, &level, false))
  253. return -EINVAL;
  254. for (offset = 2; offset < video->brightness->count; offset++)
  255. if (level == video->brightness->levels[offset]) {
  256. *state = video->brightness->count - offset - 1;
  257. return 0;
  258. }
  259. return -EINVAL;
  260. }
  261. static int
  262. video_set_cur_state(struct thermal_cooling_device *cooling_dev, unsigned long state)
  263. {
  264. struct acpi_device *device = cooling_dev->devdata;
  265. struct acpi_video_device *video = acpi_driver_data(device);
  266. int level;
  267. if ( state >= video->brightness->count - 2)
  268. return -EINVAL;
  269. state = video->brightness->count - state;
  270. level = video->brightness->levels[state -1];
  271. return acpi_video_device_lcd_set_level(video, level);
  272. }
  273. static const struct thermal_cooling_device_ops video_cooling_ops = {
  274. .get_max_state = video_get_max_state,
  275. .get_cur_state = video_get_cur_state,
  276. .set_cur_state = video_set_cur_state,
  277. };
  278. /* --------------------------------------------------------------------------
  279. Video Management
  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. /* Fixme:
  489. * should we return an error or ignore this failure?
  490. * dev->brightness->curr is a cached value which stores
  491. * the correct current backlight level in most cases.
  492. * ACPI video backlight still works w/ buggy _BQC.
  493. * http://bugzilla.kernel.org/show_bug.cgi?id=12233
  494. */
  495. ACPI_WARNING((AE_INFO, "Evaluating %s failed", buf));
  496. device->cap._BQC = device->cap._BCQ = 0;
  497. }
  498. }
  499. *level = device->brightness->curr;
  500. return 0;
  501. }
  502. static int
  503. acpi_video_device_EDID(struct acpi_video_device *device,
  504. union acpi_object **edid, ssize_t length)
  505. {
  506. int status;
  507. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  508. union acpi_object *obj;
  509. union acpi_object arg0 = { ACPI_TYPE_INTEGER };
  510. struct acpi_object_list args = { 1, &arg0 };
  511. *edid = NULL;
  512. if (!device)
  513. return -ENODEV;
  514. if (length == 128)
  515. arg0.integer.value = 1;
  516. else if (length == 256)
  517. arg0.integer.value = 2;
  518. else
  519. return -EINVAL;
  520. status = acpi_evaluate_object(device->dev->handle, "_DDC", &args, &buffer);
  521. if (ACPI_FAILURE(status))
  522. return -ENODEV;
  523. obj = buffer.pointer;
  524. if (obj && obj->type == ACPI_TYPE_BUFFER)
  525. *edid = obj;
  526. else {
  527. printk(KERN_ERR PREFIX "Invalid _DDC data\n");
  528. status = -EFAULT;
  529. kfree(obj);
  530. }
  531. return status;
  532. }
  533. /* bus */
  534. /*
  535. * Arg:
  536. * video : video bus device pointer
  537. * bios_flag :
  538. * 0. The system BIOS should NOT automatically switch(toggle)
  539. * the active display output.
  540. * 1. The system BIOS should automatically switch (toggle) the
  541. * active display output. No switch event.
  542. * 2. The _DGS value should be locked.
  543. * 3. The system BIOS should not automatically switch (toggle) the
  544. * active display output, but instead generate the display switch
  545. * event notify code.
  546. * lcd_flag :
  547. * 0. The system BIOS should automatically control the brightness level
  548. * of the LCD when the power changes from AC to DC
  549. * 1. The system BIOS should NOT automatically control the brightness
  550. * level of the LCD when the power changes from AC to DC.
  551. * Return Value:
  552. * -EINVAL wrong arg.
  553. */
  554. static int
  555. acpi_video_bus_DOS(struct acpi_video_bus *video, int bios_flag, int lcd_flag)
  556. {
  557. acpi_status status;
  558. if (!video->cap._DOS)
  559. return 0;
  560. if (bios_flag < 0 || bios_flag > 3 || lcd_flag < 0 || lcd_flag > 1)
  561. return -EINVAL;
  562. video->dos_setting = (lcd_flag << 2) | bios_flag;
  563. status = acpi_execute_simple_method(video->device->handle, "_DOS",
  564. (lcd_flag << 2) | bios_flag);
  565. if (ACPI_FAILURE(status))
  566. return -EIO;
  567. return 0;
  568. }
  569. /*
  570. * Simple comparison function used to sort backlight levels.
  571. */
  572. static int
  573. acpi_video_cmp_level(const void *a, const void *b)
  574. {
  575. return *(int *)a - *(int *)b;
  576. }
  577. /*
  578. * Decides if _BQC/_BCQ for this system is usable
  579. *
  580. * We do this by changing the level first and then read out the current
  581. * brightness level, if the value does not match, find out if it is using
  582. * index. If not, clear the _BQC/_BCQ capability.
  583. */
  584. static int acpi_video_bqc_quirk(struct acpi_video_device *device,
  585. int max_level, int current_level)
  586. {
  587. struct acpi_video_device_brightness *br = device->brightness;
  588. int result;
  589. unsigned long long level;
  590. int test_level;
  591. /* don't mess with existing known broken systems */
  592. if (bqc_offset_aml_bug_workaround)
  593. return 0;
  594. /*
  595. * Some systems always report current brightness level as maximum
  596. * through _BQC, we need to test another value for them.
  597. */
  598. test_level = current_level == max_level ? br->levels[3] : max_level;
  599. result = acpi_video_device_lcd_set_level(device, test_level);
  600. if (result)
  601. return result;
  602. result = acpi_video_device_lcd_get_level_current(device, &level, true);
  603. if (result)
  604. return result;
  605. if (level != test_level) {
  606. /* buggy _BQC found, need to find out if it uses index */
  607. if (level < br->count) {
  608. if (br->flags._BCL_reversed)
  609. level = br->count - 3 - level;
  610. if (br->levels[level + 2] == test_level)
  611. br->flags._BQC_use_index = 1;
  612. }
  613. if (!br->flags._BQC_use_index)
  614. device->cap._BQC = device->cap._BCQ = 0;
  615. }
  616. return 0;
  617. }
  618. /*
  619. * Arg:
  620. * device : video output device (LCD, CRT, ..)
  621. *
  622. * Return Value:
  623. * Maximum brightness level
  624. *
  625. * Allocate and initialize device->brightness.
  626. */
  627. static int
  628. acpi_video_init_brightness(struct acpi_video_device *device)
  629. {
  630. union acpi_object *obj = NULL;
  631. int i, max_level = 0, count = 0, level_ac_battery = 0;
  632. unsigned long long level, level_old;
  633. union acpi_object *o;
  634. struct acpi_video_device_brightness *br = NULL;
  635. int result = -EINVAL;
  636. if (!ACPI_SUCCESS(acpi_video_device_lcd_query_levels(device, &obj))) {
  637. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Could not query available "
  638. "LCD brightness level\n"));
  639. goto out;
  640. }
  641. if (obj->package.count < 2)
  642. goto out;
  643. br = kzalloc(sizeof(*br), GFP_KERNEL);
  644. if (!br) {
  645. printk(KERN_ERR "can't allocate memory\n");
  646. result = -ENOMEM;
  647. goto out;
  648. }
  649. br->levels = kmalloc((obj->package.count + 2) * sizeof *(br->levels),
  650. GFP_KERNEL);
  651. if (!br->levels) {
  652. result = -ENOMEM;
  653. goto out_free;
  654. }
  655. for (i = 0; i < obj->package.count; i++) {
  656. o = (union acpi_object *)&obj->package.elements[i];
  657. if (o->type != ACPI_TYPE_INTEGER) {
  658. printk(KERN_ERR PREFIX "Invalid data\n");
  659. continue;
  660. }
  661. br->levels[count] = (u32) o->integer.value;
  662. if (br->levels[count] > max_level)
  663. max_level = br->levels[count];
  664. count++;
  665. }
  666. /*
  667. * some buggy BIOS don't export the levels
  668. * when machine is on AC/Battery in _BCL package.
  669. * In this case, the first two elements in _BCL packages
  670. * are also supported brightness levels that OS should take care of.
  671. */
  672. for (i = 2; i < count; i++) {
  673. if (br->levels[i] == br->levels[0])
  674. level_ac_battery++;
  675. if (br->levels[i] == br->levels[1])
  676. level_ac_battery++;
  677. }
  678. if (level_ac_battery < 2) {
  679. level_ac_battery = 2 - level_ac_battery;
  680. br->flags._BCL_no_ac_battery_levels = 1;
  681. for (i = (count - 1 + level_ac_battery); i >= 2; i--)
  682. br->levels[i] = br->levels[i - level_ac_battery];
  683. count += level_ac_battery;
  684. } else if (level_ac_battery > 2)
  685. ACPI_ERROR((AE_INFO, "Too many duplicates in _BCL package"));
  686. /* Check if the _BCL package is in a reversed order */
  687. if (max_level == br->levels[2]) {
  688. br->flags._BCL_reversed = 1;
  689. sort(&br->levels[2], count - 2, sizeof(br->levels[2]),
  690. acpi_video_cmp_level, NULL);
  691. } else if (max_level != br->levels[count - 1])
  692. ACPI_ERROR((AE_INFO,
  693. "Found unordered _BCL package"));
  694. br->count = count;
  695. device->brightness = br;
  696. /* Check the input/output of _BQC/_BCL/_BCM */
  697. if ((max_level < 100) && (max_level <= (count - 2)))
  698. br->flags._BCL_use_index = 1;
  699. /*
  700. * _BCM is always consistent with _BCL,
  701. * at least for all the laptops we have ever seen.
  702. */
  703. br->flags._BCM_use_index = br->flags._BCL_use_index;
  704. /* _BQC uses INDEX while _BCL uses VALUE in some laptops */
  705. br->curr = level = max_level;
  706. if (!device->cap._BQC)
  707. goto set_level;
  708. result = acpi_video_device_lcd_get_level_current(device,
  709. &level_old, true);
  710. if (result)
  711. goto out_free_levels;
  712. result = acpi_video_bqc_quirk(device, max_level, level_old);
  713. if (result)
  714. goto out_free_levels;
  715. /*
  716. * cap._BQC may get cleared due to _BQC is found to be broken
  717. * in acpi_video_bqc_quirk, so check again here.
  718. */
  719. if (!device->cap._BQC)
  720. goto set_level;
  721. if (use_bios_initial_backlight) {
  722. level = acpi_video_bqc_value_to_level(device, level_old);
  723. /*
  724. * On some buggy laptops, _BQC returns an uninitialized
  725. * value when invoked for the first time, i.e.
  726. * level_old is invalid (no matter whether it's a level
  727. * or an index). Set the backlight to max_level in this case.
  728. */
  729. for (i = 2; i < br->count; i++)
  730. if (level_old == br->levels[i])
  731. break;
  732. if (i == br->count)
  733. level = max_level;
  734. }
  735. set_level:
  736. result = acpi_video_device_lcd_set_level(device, level);
  737. if (result)
  738. goto out_free_levels;
  739. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  740. "found %d brightness levels\n", count - 2));
  741. kfree(obj);
  742. return result;
  743. out_free_levels:
  744. kfree(br->levels);
  745. out_free:
  746. kfree(br);
  747. out:
  748. device->brightness = NULL;
  749. kfree(obj);
  750. return result;
  751. }
  752. /*
  753. * Arg:
  754. * device : video output device (LCD, CRT, ..)
  755. *
  756. * Return Value:
  757. * None
  758. *
  759. * Find out all required AML methods defined under the output
  760. * device.
  761. */
  762. static void acpi_video_device_find_cap(struct acpi_video_device *device)
  763. {
  764. if (acpi_has_method(device->dev->handle, "_ADR"))
  765. device->cap._ADR = 1;
  766. if (acpi_has_method(device->dev->handle, "_BCL"))
  767. device->cap._BCL = 1;
  768. if (acpi_has_method(device->dev->handle, "_BCM"))
  769. device->cap._BCM = 1;
  770. if (acpi_has_method(device->dev->handle, "_BQC")) {
  771. device->cap._BQC = 1;
  772. } else if (acpi_has_method(device->dev->handle, "_BCQ")) {
  773. printk(KERN_WARNING FW_BUG "_BCQ is used instead of _BQC\n");
  774. device->cap._BCQ = 1;
  775. }
  776. if (acpi_has_method(device->dev->handle, "_DDC"))
  777. device->cap._DDC = 1;
  778. if (acpi_video_backlight_support()) {
  779. struct backlight_properties props;
  780. struct pci_dev *pdev;
  781. acpi_handle acpi_parent;
  782. struct device *parent = NULL;
  783. int result;
  784. static int count = 0;
  785. char *name;
  786. result = acpi_video_init_brightness(device);
  787. if (result)
  788. return;
  789. name = kasprintf(GFP_KERNEL, "acpi_video%d", count);
  790. if (!name)
  791. return;
  792. count++;
  793. acpi_get_parent(device->dev->handle, &acpi_parent);
  794. pdev = acpi_get_pci_dev(acpi_parent);
  795. if (pdev) {
  796. parent = &pdev->dev;
  797. pci_dev_put(pdev);
  798. }
  799. memset(&props, 0, sizeof(struct backlight_properties));
  800. props.type = BACKLIGHT_FIRMWARE;
  801. props.max_brightness = device->brightness->count - 3;
  802. device->backlight = backlight_device_register(name,
  803. parent,
  804. device,
  805. &acpi_backlight_ops,
  806. &props);
  807. kfree(name);
  808. if (IS_ERR(device->backlight))
  809. return;
  810. /*
  811. * Save current brightness level in case we have to restore it
  812. * before acpi_video_device_lcd_set_level() is called next time.
  813. */
  814. device->backlight->props.brightness =
  815. acpi_video_get_brightness(device->backlight);
  816. device->cooling_dev = thermal_cooling_device_register("LCD",
  817. device->dev, &video_cooling_ops);
  818. if (IS_ERR(device->cooling_dev)) {
  819. /*
  820. * Set cooling_dev to NULL so we don't crash trying to
  821. * free it.
  822. * Also, why the hell we are returning early and
  823. * not attempt to register video output if cooling
  824. * device registration failed?
  825. * -- dtor
  826. */
  827. device->cooling_dev = NULL;
  828. return;
  829. }
  830. dev_info(&device->dev->dev, "registered as cooling_device%d\n",
  831. device->cooling_dev->id);
  832. result = sysfs_create_link(&device->dev->dev.kobj,
  833. &device->cooling_dev->device.kobj,
  834. "thermal_cooling");
  835. if (result)
  836. printk(KERN_ERR PREFIX "Create sysfs link\n");
  837. result = sysfs_create_link(&device->cooling_dev->device.kobj,
  838. &device->dev->dev.kobj, "device");
  839. if (result)
  840. printk(KERN_ERR PREFIX "Create sysfs link\n");
  841. }
  842. }
  843. /*
  844. * Arg:
  845. * device : video output device (VGA)
  846. *
  847. * Return Value:
  848. * None
  849. *
  850. * Find out all required AML methods defined under the video bus device.
  851. */
  852. static void acpi_video_bus_find_cap(struct acpi_video_bus *video)
  853. {
  854. if (acpi_has_method(video->device->handle, "_DOS"))
  855. video->cap._DOS = 1;
  856. if (acpi_has_method(video->device->handle, "_DOD"))
  857. video->cap._DOD = 1;
  858. if (acpi_has_method(video->device->handle, "_ROM"))
  859. video->cap._ROM = 1;
  860. if (acpi_has_method(video->device->handle, "_GPD"))
  861. video->cap._GPD = 1;
  862. if (acpi_has_method(video->device->handle, "_SPD"))
  863. video->cap._SPD = 1;
  864. if (acpi_has_method(video->device->handle, "_VPO"))
  865. video->cap._VPO = 1;
  866. }
  867. /*
  868. * Check whether the video bus device has required AML method to
  869. * support the desired features
  870. */
  871. static int acpi_video_bus_check(struct acpi_video_bus *video)
  872. {
  873. acpi_status status = -ENOENT;
  874. struct pci_dev *dev;
  875. if (!video)
  876. return -EINVAL;
  877. dev = acpi_get_pci_dev(video->device->handle);
  878. if (!dev)
  879. return -ENODEV;
  880. pci_dev_put(dev);
  881. /* Since there is no HID, CID and so on for VGA driver, we have
  882. * to check well known required nodes.
  883. */
  884. /* Does this device support video switching? */
  885. if (video->cap._DOS || video->cap._DOD) {
  886. if (!video->cap._DOS) {
  887. printk(KERN_WARNING FW_BUG
  888. "ACPI(%s) defines _DOD but not _DOS\n",
  889. acpi_device_bid(video->device));
  890. }
  891. video->flags.multihead = 1;
  892. status = 0;
  893. }
  894. /* Does this device support retrieving a video ROM? */
  895. if (video->cap._ROM) {
  896. video->flags.rom = 1;
  897. status = 0;
  898. }
  899. /* Does this device support configuring which video device to POST? */
  900. if (video->cap._GPD && video->cap._SPD && video->cap._VPO) {
  901. video->flags.post = 1;
  902. status = 0;
  903. }
  904. return status;
  905. }
  906. /* --------------------------------------------------------------------------
  907. Driver Interface
  908. -------------------------------------------------------------------------- */
  909. /* device interface */
  910. static struct acpi_video_device_attrib*
  911. acpi_video_get_device_attr(struct acpi_video_bus *video, unsigned long device_id)
  912. {
  913. struct acpi_video_enumerated_device *ids;
  914. int i;
  915. for (i = 0; i < video->attached_count; i++) {
  916. ids = &video->attached_array[i];
  917. if ((ids->value.int_val & 0xffff) == device_id)
  918. return &ids->value.attrib;
  919. }
  920. return NULL;
  921. }
  922. static int
  923. acpi_video_get_device_type(struct acpi_video_bus *video,
  924. unsigned long device_id)
  925. {
  926. struct acpi_video_enumerated_device *ids;
  927. int i;
  928. for (i = 0; i < video->attached_count; i++) {
  929. ids = &video->attached_array[i];
  930. if ((ids->value.int_val & 0xffff) == device_id)
  931. return ids->value.int_val;
  932. }
  933. return 0;
  934. }
  935. static int
  936. acpi_video_bus_get_one_device(struct acpi_device *device,
  937. struct acpi_video_bus *video)
  938. {
  939. unsigned long long device_id;
  940. int status, device_type;
  941. struct acpi_video_device *data;
  942. struct acpi_video_device_attrib* attribute;
  943. status =
  944. acpi_evaluate_integer(device->handle, "_ADR", NULL, &device_id);
  945. /* Some device omits _ADR, we skip them instead of fail */
  946. if (ACPI_FAILURE(status))
  947. return 0;
  948. data = kzalloc(sizeof(struct acpi_video_device), GFP_KERNEL);
  949. if (!data)
  950. return -ENOMEM;
  951. strcpy(acpi_device_name(device), ACPI_VIDEO_DEVICE_NAME);
  952. strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
  953. device->driver_data = data;
  954. data->device_id = device_id;
  955. data->video = video;
  956. data->dev = device;
  957. attribute = acpi_video_get_device_attr(video, device_id);
  958. if((attribute != NULL) && attribute->device_id_scheme) {
  959. switch (attribute->display_type) {
  960. case ACPI_VIDEO_DISPLAY_CRT:
  961. data->flags.crt = 1;
  962. break;
  963. case ACPI_VIDEO_DISPLAY_TV:
  964. data->flags.tvout = 1;
  965. break;
  966. case ACPI_VIDEO_DISPLAY_DVI:
  967. data->flags.dvi = 1;
  968. break;
  969. case ACPI_VIDEO_DISPLAY_LCD:
  970. data->flags.lcd = 1;
  971. break;
  972. default:
  973. data->flags.unknown = 1;
  974. break;
  975. }
  976. if(attribute->bios_can_detect)
  977. data->flags.bios = 1;
  978. } else {
  979. /* Check for legacy IDs */
  980. device_type = acpi_video_get_device_type(video, device_id);
  981. /* Ignore bits 16 and 18-20 */
  982. switch (device_type & 0xffe2ffff) {
  983. case ACPI_VIDEO_DISPLAY_LEGACY_MONITOR:
  984. data->flags.crt = 1;
  985. break;
  986. case ACPI_VIDEO_DISPLAY_LEGACY_PANEL:
  987. data->flags.lcd = 1;
  988. break;
  989. case ACPI_VIDEO_DISPLAY_LEGACY_TV:
  990. data->flags.tvout = 1;
  991. break;
  992. default:
  993. data->flags.unknown = 1;
  994. }
  995. }
  996. acpi_video_device_bind(video, data);
  997. acpi_video_device_find_cap(data);
  998. status = acpi_install_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
  999. acpi_video_device_notify, data);
  1000. if (ACPI_FAILURE(status))
  1001. dev_err(&device->dev, "Error installing notify handler\n");
  1002. else
  1003. data->flags.notify = 1;
  1004. mutex_lock(&video->device_list_lock);
  1005. list_add_tail(&data->entry, &video->video_device_list);
  1006. mutex_unlock(&video->device_list_lock);
  1007. return status;
  1008. }
  1009. /*
  1010. * Arg:
  1011. * video : video bus device
  1012. *
  1013. * Return:
  1014. * none
  1015. *
  1016. * Enumerate the video device list of the video bus,
  1017. * bind the ids with the corresponding video devices
  1018. * under the video bus.
  1019. */
  1020. static void acpi_video_device_rebind(struct acpi_video_bus *video)
  1021. {
  1022. struct acpi_video_device *dev;
  1023. mutex_lock(&video->device_list_lock);
  1024. list_for_each_entry(dev, &video->video_device_list, entry)
  1025. acpi_video_device_bind(video, dev);
  1026. mutex_unlock(&video->device_list_lock);
  1027. }
  1028. /*
  1029. * Arg:
  1030. * video : video bus device
  1031. * device : video output device under the video
  1032. * bus
  1033. *
  1034. * Return:
  1035. * none
  1036. *
  1037. * Bind the ids with the corresponding video devices
  1038. * under the video bus.
  1039. */
  1040. static void
  1041. acpi_video_device_bind(struct acpi_video_bus *video,
  1042. struct acpi_video_device *device)
  1043. {
  1044. struct acpi_video_enumerated_device *ids;
  1045. int i;
  1046. for (i = 0; i < video->attached_count; i++) {
  1047. ids = &video->attached_array[i];
  1048. if (device->device_id == (ids->value.int_val & 0xffff)) {
  1049. ids->bind_info = device;
  1050. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "device_bind %d\n", i));
  1051. }
  1052. }
  1053. }
  1054. /*
  1055. * Arg:
  1056. * video : video bus device
  1057. *
  1058. * Return:
  1059. * < 0 : error
  1060. *
  1061. * Call _DOD to enumerate all devices attached to display adapter
  1062. *
  1063. */
  1064. static int acpi_video_device_enumerate(struct acpi_video_bus *video)
  1065. {
  1066. int status;
  1067. int count;
  1068. int i;
  1069. struct acpi_video_enumerated_device *active_list;
  1070. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  1071. union acpi_object *dod = NULL;
  1072. union acpi_object *obj;
  1073. status = acpi_evaluate_object(video->device->handle, "_DOD", NULL, &buffer);
  1074. if (!ACPI_SUCCESS(status)) {
  1075. ACPI_EXCEPTION((AE_INFO, status, "Evaluating _DOD"));
  1076. return status;
  1077. }
  1078. dod = buffer.pointer;
  1079. if (!dod || (dod->type != ACPI_TYPE_PACKAGE)) {
  1080. ACPI_EXCEPTION((AE_INFO, status, "Invalid _DOD data"));
  1081. status = -EFAULT;
  1082. goto out;
  1083. }
  1084. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d video heads in _DOD\n",
  1085. dod->package.count));
  1086. active_list = kcalloc(1 + dod->package.count,
  1087. sizeof(struct acpi_video_enumerated_device),
  1088. GFP_KERNEL);
  1089. if (!active_list) {
  1090. status = -ENOMEM;
  1091. goto out;
  1092. }
  1093. count = 0;
  1094. for (i = 0; i < dod->package.count; i++) {
  1095. obj = &dod->package.elements[i];
  1096. if (obj->type != ACPI_TYPE_INTEGER) {
  1097. printk(KERN_ERR PREFIX
  1098. "Invalid _DOD data in element %d\n", i);
  1099. continue;
  1100. }
  1101. active_list[count].value.int_val = obj->integer.value;
  1102. active_list[count].bind_info = NULL;
  1103. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "dod element[%d] = %d\n", i,
  1104. (int)obj->integer.value));
  1105. count++;
  1106. }
  1107. kfree(video->attached_array);
  1108. video->attached_array = active_list;
  1109. video->attached_count = count;
  1110. out:
  1111. kfree(buffer.pointer);
  1112. return status;
  1113. }
  1114. static int
  1115. acpi_video_get_next_level(struct acpi_video_device *device,
  1116. u32 level_current, u32 event)
  1117. {
  1118. int min, max, min_above, max_below, i, l, delta = 255;
  1119. max = max_below = 0;
  1120. min = min_above = 255;
  1121. /* Find closest level to level_current */
  1122. for (i = 2; i < device->brightness->count; i++) {
  1123. l = device->brightness->levels[i];
  1124. if (abs(l - level_current) < abs(delta)) {
  1125. delta = l - level_current;
  1126. if (!delta)
  1127. break;
  1128. }
  1129. }
  1130. /* Ajust level_current to closest available level */
  1131. level_current += delta;
  1132. for (i = 2; i < device->brightness->count; i++) {
  1133. l = device->brightness->levels[i];
  1134. if (l < min)
  1135. min = l;
  1136. if (l > max)
  1137. max = l;
  1138. if (l < min_above && l > level_current)
  1139. min_above = l;
  1140. if (l > max_below && l < level_current)
  1141. max_below = l;
  1142. }
  1143. switch (event) {
  1144. case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS:
  1145. return (level_current < max) ? min_above : min;
  1146. case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS:
  1147. return (level_current < max) ? min_above : max;
  1148. case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS:
  1149. return (level_current > min) ? max_below : min;
  1150. case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS:
  1151. case ACPI_VIDEO_NOTIFY_DISPLAY_OFF:
  1152. return 0;
  1153. default:
  1154. return level_current;
  1155. }
  1156. }
  1157. static int
  1158. acpi_video_switch_brightness(struct acpi_video_device *device, int event)
  1159. {
  1160. unsigned long long level_current, level_next;
  1161. int result = -EINVAL;
  1162. /* no warning message if acpi_backlight=vendor is used */
  1163. if (!acpi_video_backlight_support())
  1164. return 0;
  1165. if (!device->brightness)
  1166. goto out;
  1167. result = acpi_video_device_lcd_get_level_current(device,
  1168. &level_current,
  1169. false);
  1170. if (result)
  1171. goto out;
  1172. level_next = acpi_video_get_next_level(device, level_current, event);
  1173. result = acpi_video_device_lcd_set_level(device, level_next);
  1174. if (!result)
  1175. backlight_force_update(device->backlight,
  1176. BACKLIGHT_UPDATE_HOTKEY);
  1177. out:
  1178. if (result)
  1179. printk(KERN_ERR PREFIX "Failed to switch the brightness\n");
  1180. return result;
  1181. }
  1182. int acpi_video_get_edid(struct acpi_device *device, int type, int device_id,
  1183. void **edid)
  1184. {
  1185. struct acpi_video_bus *video;
  1186. struct acpi_video_device *video_device;
  1187. union acpi_object *buffer = NULL;
  1188. acpi_status status;
  1189. int i, length;
  1190. if (!device || !acpi_driver_data(device))
  1191. return -EINVAL;
  1192. video = acpi_driver_data(device);
  1193. for (i = 0; i < video->attached_count; i++) {
  1194. video_device = video->attached_array[i].bind_info;
  1195. length = 256;
  1196. if (!video_device)
  1197. continue;
  1198. if (!video_device->cap._DDC)
  1199. continue;
  1200. if (type) {
  1201. switch (type) {
  1202. case ACPI_VIDEO_DISPLAY_CRT:
  1203. if (!video_device->flags.crt)
  1204. continue;
  1205. break;
  1206. case ACPI_VIDEO_DISPLAY_TV:
  1207. if (!video_device->flags.tvout)
  1208. continue;
  1209. break;
  1210. case ACPI_VIDEO_DISPLAY_DVI:
  1211. if (!video_device->flags.dvi)
  1212. continue;
  1213. break;
  1214. case ACPI_VIDEO_DISPLAY_LCD:
  1215. if (!video_device->flags.lcd)
  1216. continue;
  1217. break;
  1218. }
  1219. } else if (video_device->device_id != device_id) {
  1220. continue;
  1221. }
  1222. status = acpi_video_device_EDID(video_device, &buffer, length);
  1223. if (ACPI_FAILURE(status) || !buffer ||
  1224. buffer->type != ACPI_TYPE_BUFFER) {
  1225. length = 128;
  1226. status = acpi_video_device_EDID(video_device, &buffer,
  1227. length);
  1228. if (ACPI_FAILURE(status) || !buffer ||
  1229. buffer->type != ACPI_TYPE_BUFFER) {
  1230. continue;
  1231. }
  1232. }
  1233. *edid = buffer->buffer.pointer;
  1234. return length;
  1235. }
  1236. return -ENODEV;
  1237. }
  1238. EXPORT_SYMBOL(acpi_video_get_edid);
  1239. static int
  1240. acpi_video_bus_get_devices(struct acpi_video_bus *video,
  1241. struct acpi_device *device)
  1242. {
  1243. int status = 0;
  1244. struct acpi_device *dev;
  1245. /*
  1246. * There are systems where video module known to work fine regardless
  1247. * of broken _DOD and ignoring returned value here doesn't cause
  1248. * any issues later.
  1249. */
  1250. acpi_video_device_enumerate(video);
  1251. list_for_each_entry(dev, &device->children, node) {
  1252. status = acpi_video_bus_get_one_device(dev, video);
  1253. if (status) {
  1254. dev_err(&dev->dev, "Can't attach device\n");
  1255. break;
  1256. }
  1257. }
  1258. return status;
  1259. }
  1260. static int acpi_video_bus_put_one_device(struct acpi_video_device *device)
  1261. {
  1262. acpi_status status;
  1263. if (!device || !device->video)
  1264. return -ENOENT;
  1265. if (device->flags.notify) {
  1266. status = acpi_remove_notify_handler(device->dev->handle,
  1267. ACPI_DEVICE_NOTIFY, acpi_video_device_notify);
  1268. if (ACPI_FAILURE(status))
  1269. dev_err(&device->dev->dev,
  1270. "Can't remove video notify handler\n");
  1271. }
  1272. if (device->backlight) {
  1273. backlight_device_unregister(device->backlight);
  1274. device->backlight = NULL;
  1275. }
  1276. if (device->cooling_dev) {
  1277. sysfs_remove_link(&device->dev->dev.kobj,
  1278. "thermal_cooling");
  1279. sysfs_remove_link(&device->cooling_dev->device.kobj,
  1280. "device");
  1281. thermal_cooling_device_unregister(device->cooling_dev);
  1282. device->cooling_dev = NULL;
  1283. }
  1284. return 0;
  1285. }
  1286. static int acpi_video_bus_put_devices(struct acpi_video_bus *video)
  1287. {
  1288. int status;
  1289. struct acpi_video_device *dev, *next;
  1290. mutex_lock(&video->device_list_lock);
  1291. list_for_each_entry_safe(dev, next, &video->video_device_list, entry) {
  1292. status = acpi_video_bus_put_one_device(dev);
  1293. if (ACPI_FAILURE(status))
  1294. printk(KERN_WARNING PREFIX
  1295. "hhuuhhuu bug in acpi video driver.\n");
  1296. if (dev->brightness) {
  1297. kfree(dev->brightness->levels);
  1298. kfree(dev->brightness);
  1299. }
  1300. list_del(&dev->entry);
  1301. kfree(dev);
  1302. }
  1303. mutex_unlock(&video->device_list_lock);
  1304. return 0;
  1305. }
  1306. /* acpi_video interface */
  1307. /*
  1308. * Win8 requires setting bit2 of _DOS to let firmware know it shouldn't
  1309. * preform any automatic brightness change on receiving a notification.
  1310. */
  1311. static int acpi_video_bus_start_devices(struct acpi_video_bus *video)
  1312. {
  1313. return acpi_video_bus_DOS(video, 0,
  1314. acpi_video_backlight_quirks() ? 1 : 0);
  1315. }
  1316. static int acpi_video_bus_stop_devices(struct acpi_video_bus *video)
  1317. {
  1318. return acpi_video_bus_DOS(video, 0,
  1319. acpi_video_backlight_quirks() ? 0 : 1);
  1320. }
  1321. static void acpi_video_bus_notify(struct acpi_device *device, u32 event)
  1322. {
  1323. struct acpi_video_bus *video = acpi_driver_data(device);
  1324. struct input_dev *input;
  1325. int keycode = 0;
  1326. if (!video)
  1327. return;
  1328. input = video->input;
  1329. switch (event) {
  1330. case ACPI_VIDEO_NOTIFY_SWITCH: /* User requested a switch,
  1331. * most likely via hotkey. */
  1332. acpi_bus_generate_proc_event(device, event, 0);
  1333. keycode = KEY_SWITCHVIDEOMODE;
  1334. break;
  1335. case ACPI_VIDEO_NOTIFY_PROBE: /* User plugged in or removed a video
  1336. * connector. */
  1337. acpi_video_device_enumerate(video);
  1338. acpi_video_device_rebind(video);
  1339. acpi_bus_generate_proc_event(device, event, 0);
  1340. keycode = KEY_SWITCHVIDEOMODE;
  1341. break;
  1342. case ACPI_VIDEO_NOTIFY_CYCLE: /* Cycle Display output hotkey pressed. */
  1343. acpi_bus_generate_proc_event(device, event, 0);
  1344. keycode = KEY_SWITCHVIDEOMODE;
  1345. break;
  1346. case ACPI_VIDEO_NOTIFY_NEXT_OUTPUT: /* Next Display output hotkey pressed. */
  1347. acpi_bus_generate_proc_event(device, event, 0);
  1348. keycode = KEY_VIDEO_NEXT;
  1349. break;
  1350. case ACPI_VIDEO_NOTIFY_PREV_OUTPUT: /* previous Display output hotkey pressed. */
  1351. acpi_bus_generate_proc_event(device, event, 0);
  1352. keycode = KEY_VIDEO_PREV;
  1353. break;
  1354. default:
  1355. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  1356. "Unsupported event [0x%x]\n", event));
  1357. break;
  1358. }
  1359. if (acpi_notifier_call_chain(device, event, 0))
  1360. /* Something vetoed the keypress. */
  1361. keycode = 0;
  1362. if (keycode) {
  1363. input_report_key(input, keycode, 1);
  1364. input_sync(input);
  1365. input_report_key(input, keycode, 0);
  1366. input_sync(input);
  1367. }
  1368. return;
  1369. }
  1370. static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data)
  1371. {
  1372. struct acpi_video_device *video_device = data;
  1373. struct acpi_device *device = NULL;
  1374. struct acpi_video_bus *bus;
  1375. struct input_dev *input;
  1376. int keycode = 0;
  1377. if (!video_device)
  1378. return;
  1379. device = video_device->dev;
  1380. bus = video_device->video;
  1381. input = bus->input;
  1382. switch (event) {
  1383. case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS: /* Cycle brightness */
  1384. if (brightness_switch_enabled)
  1385. acpi_video_switch_brightness(video_device, event);
  1386. acpi_bus_generate_proc_event(device, event, 0);
  1387. keycode = KEY_BRIGHTNESS_CYCLE;
  1388. break;
  1389. case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS: /* Increase brightness */
  1390. if (brightness_switch_enabled)
  1391. acpi_video_switch_brightness(video_device, event);
  1392. acpi_bus_generate_proc_event(device, event, 0);
  1393. keycode = KEY_BRIGHTNESSUP;
  1394. break;
  1395. case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS: /* Decrease brightness */
  1396. if (brightness_switch_enabled)
  1397. acpi_video_switch_brightness(video_device, event);
  1398. acpi_bus_generate_proc_event(device, event, 0);
  1399. keycode = KEY_BRIGHTNESSDOWN;
  1400. break;
  1401. case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS: /* zero brightness */
  1402. if (brightness_switch_enabled)
  1403. acpi_video_switch_brightness(video_device, event);
  1404. acpi_bus_generate_proc_event(device, event, 0);
  1405. keycode = KEY_BRIGHTNESS_ZERO;
  1406. break;
  1407. case ACPI_VIDEO_NOTIFY_DISPLAY_OFF: /* display device off */
  1408. if (brightness_switch_enabled)
  1409. acpi_video_switch_brightness(video_device, event);
  1410. acpi_bus_generate_proc_event(device, event, 0);
  1411. keycode = KEY_DISPLAY_OFF;
  1412. break;
  1413. default:
  1414. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  1415. "Unsupported event [0x%x]\n", event));
  1416. break;
  1417. }
  1418. acpi_notifier_call_chain(device, event, 0);
  1419. if (keycode) {
  1420. input_report_key(input, keycode, 1);
  1421. input_sync(input);
  1422. input_report_key(input, keycode, 0);
  1423. input_sync(input);
  1424. }
  1425. return;
  1426. }
  1427. static int acpi_video_resume(struct notifier_block *nb,
  1428. unsigned long val, void *ign)
  1429. {
  1430. struct acpi_video_bus *video;
  1431. struct acpi_video_device *video_device;
  1432. int i;
  1433. switch (val) {
  1434. case PM_HIBERNATION_PREPARE:
  1435. case PM_SUSPEND_PREPARE:
  1436. case PM_RESTORE_PREPARE:
  1437. return NOTIFY_DONE;
  1438. }
  1439. video = container_of(nb, struct acpi_video_bus, pm_nb);
  1440. dev_info(&video->device->dev, "Restoring backlight state\n");
  1441. for (i = 0; i < video->attached_count; i++) {
  1442. video_device = video->attached_array[i].bind_info;
  1443. if (video_device && video_device->backlight)
  1444. acpi_video_set_brightness(video_device->backlight);
  1445. }
  1446. return NOTIFY_OK;
  1447. }
  1448. static acpi_status
  1449. acpi_video_bus_match(acpi_handle handle, u32 level, void *context,
  1450. void **return_value)
  1451. {
  1452. struct acpi_device *device = context;
  1453. struct acpi_device *sibling;
  1454. int result;
  1455. if (handle == device->handle)
  1456. return AE_CTRL_TERMINATE;
  1457. result = acpi_bus_get_device(handle, &sibling);
  1458. if (result)
  1459. return AE_OK;
  1460. if (!strcmp(acpi_device_name(sibling), ACPI_VIDEO_BUS_NAME))
  1461. return AE_ALREADY_EXISTS;
  1462. return AE_OK;
  1463. }
  1464. static int instance;
  1465. static int acpi_video_bus_add(struct acpi_device *device)
  1466. {
  1467. struct acpi_video_bus *video;
  1468. struct input_dev *input;
  1469. int error;
  1470. acpi_status status;
  1471. status = acpi_walk_namespace(ACPI_TYPE_DEVICE,
  1472. device->parent->handle, 1,
  1473. acpi_video_bus_match, NULL,
  1474. device, NULL);
  1475. if (status == AE_ALREADY_EXISTS) {
  1476. printk(KERN_WARNING FW_BUG
  1477. "Duplicate ACPI video bus devices for the"
  1478. " same VGA controller, please try module "
  1479. "parameter \"video.allow_duplicates=1\""
  1480. "if the current driver doesn't work.\n");
  1481. if (!allow_duplicates)
  1482. return -ENODEV;
  1483. }
  1484. video = kzalloc(sizeof(struct acpi_video_bus), GFP_KERNEL);
  1485. if (!video)
  1486. return -ENOMEM;
  1487. /* a hack to fix the duplicate name "VID" problem on T61 */
  1488. if (!strcmp(device->pnp.bus_id, "VID")) {
  1489. if (instance)
  1490. device->pnp.bus_id[3] = '0' + instance;
  1491. instance ++;
  1492. }
  1493. /* a hack to fix the duplicate name "VGA" problem on Pa 3553 */
  1494. if (!strcmp(device->pnp.bus_id, "VGA")) {
  1495. if (instance)
  1496. device->pnp.bus_id[3] = '0' + instance;
  1497. instance++;
  1498. }
  1499. video->device = device;
  1500. strcpy(acpi_device_name(device), ACPI_VIDEO_BUS_NAME);
  1501. strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
  1502. device->driver_data = video;
  1503. acpi_video_bus_find_cap(video);
  1504. error = acpi_video_bus_check(video);
  1505. if (error)
  1506. goto err_free_video;
  1507. mutex_init(&video->device_list_lock);
  1508. INIT_LIST_HEAD(&video->video_device_list);
  1509. error = acpi_video_bus_get_devices(video, device);
  1510. if (error)
  1511. goto err_put_video;
  1512. video->input = input = input_allocate_device();
  1513. if (!input) {
  1514. error = -ENOMEM;
  1515. goto err_put_video;
  1516. }
  1517. error = acpi_video_bus_start_devices(video);
  1518. if (error)
  1519. goto err_free_input_dev;
  1520. snprintf(video->phys, sizeof(video->phys),
  1521. "%s/video/input0", acpi_device_hid(video->device));
  1522. input->name = acpi_device_name(video->device);
  1523. input->phys = video->phys;
  1524. input->id.bustype = BUS_HOST;
  1525. input->id.product = 0x06;
  1526. input->dev.parent = &device->dev;
  1527. input->evbit[0] = BIT(EV_KEY);
  1528. set_bit(KEY_SWITCHVIDEOMODE, input->keybit);
  1529. set_bit(KEY_VIDEO_NEXT, input->keybit);
  1530. set_bit(KEY_VIDEO_PREV, input->keybit);
  1531. set_bit(KEY_BRIGHTNESS_CYCLE, input->keybit);
  1532. set_bit(KEY_BRIGHTNESSUP, input->keybit);
  1533. set_bit(KEY_BRIGHTNESSDOWN, input->keybit);
  1534. set_bit(KEY_BRIGHTNESS_ZERO, input->keybit);
  1535. set_bit(KEY_DISPLAY_OFF, input->keybit);
  1536. printk(KERN_INFO PREFIX "%s [%s] (multi-head: %s rom: %s post: %s)\n",
  1537. ACPI_VIDEO_DEVICE_NAME, acpi_device_bid(device),
  1538. video->flags.multihead ? "yes" : "no",
  1539. video->flags.rom ? "yes" : "no",
  1540. video->flags.post ? "yes" : "no");
  1541. video->pm_nb.notifier_call = acpi_video_resume;
  1542. video->pm_nb.priority = 0;
  1543. error = register_pm_notifier(&video->pm_nb);
  1544. if (error)
  1545. goto err_stop_video;
  1546. error = input_register_device(input);
  1547. if (error)
  1548. goto err_unregister_pm_notifier;
  1549. return 0;
  1550. err_unregister_pm_notifier:
  1551. unregister_pm_notifier(&video->pm_nb);
  1552. err_stop_video:
  1553. acpi_video_bus_stop_devices(video);
  1554. err_free_input_dev:
  1555. input_free_device(input);
  1556. err_put_video:
  1557. acpi_video_bus_put_devices(video);
  1558. kfree(video->attached_array);
  1559. err_free_video:
  1560. kfree(video);
  1561. device->driver_data = NULL;
  1562. return error;
  1563. }
  1564. static int acpi_video_bus_remove(struct acpi_device *device)
  1565. {
  1566. struct acpi_video_bus *video = NULL;
  1567. if (!device || !acpi_driver_data(device))
  1568. return -EINVAL;
  1569. video = acpi_driver_data(device);
  1570. unregister_pm_notifier(&video->pm_nb);
  1571. acpi_video_bus_stop_devices(video);
  1572. acpi_video_bus_put_devices(video);
  1573. input_unregister_device(video->input);
  1574. kfree(video->attached_array);
  1575. kfree(video);
  1576. return 0;
  1577. }
  1578. static int __init is_i740(struct pci_dev *dev)
  1579. {
  1580. if (dev->device == 0x00D1)
  1581. return 1;
  1582. if (dev->device == 0x7000)
  1583. return 1;
  1584. return 0;
  1585. }
  1586. static int __init intel_opregion_present(void)
  1587. {
  1588. int opregion = 0;
  1589. struct pci_dev *dev = NULL;
  1590. u32 address;
  1591. for_each_pci_dev(dev) {
  1592. if ((dev->class >> 8) != PCI_CLASS_DISPLAY_VGA)
  1593. continue;
  1594. if (dev->vendor != PCI_VENDOR_ID_INTEL)
  1595. continue;
  1596. /* We don't want to poke around undefined i740 registers */
  1597. if (is_i740(dev))
  1598. continue;
  1599. pci_read_config_dword(dev, 0xfc, &address);
  1600. if (!address)
  1601. continue;
  1602. opregion = 1;
  1603. }
  1604. return opregion;
  1605. }
  1606. int acpi_video_register(void)
  1607. {
  1608. int result = 0;
  1609. if (register_count) {
  1610. /*
  1611. * if the function of acpi_video_register is already called,
  1612. * don't register the acpi_vide_bus again and return no error.
  1613. */
  1614. return 0;
  1615. }
  1616. result = acpi_bus_register_driver(&acpi_video_bus);
  1617. if (result < 0)
  1618. return -ENODEV;
  1619. /*
  1620. * When the acpi_video_bus is loaded successfully, increase
  1621. * the counter reference.
  1622. */
  1623. register_count = 1;
  1624. return 0;
  1625. }
  1626. EXPORT_SYMBOL(acpi_video_register);
  1627. void acpi_video_unregister(void)
  1628. {
  1629. if (!register_count) {
  1630. /*
  1631. * If the acpi video bus is already unloaded, don't
  1632. * unload it again and return directly.
  1633. */
  1634. return;
  1635. }
  1636. acpi_bus_unregister_driver(&acpi_video_bus);
  1637. register_count = 0;
  1638. return;
  1639. }
  1640. EXPORT_SYMBOL(acpi_video_unregister);
  1641. /*
  1642. * This is kind of nasty. Hardware using Intel chipsets may require
  1643. * the video opregion code to be run first in order to initialise
  1644. * state before any ACPI video calls are made. To handle this we defer
  1645. * registration of the video class until the opregion code has run.
  1646. */
  1647. static int __init acpi_video_init(void)
  1648. {
  1649. dmi_check_system(video_dmi_table);
  1650. if (intel_opregion_present())
  1651. return 0;
  1652. return acpi_video_register();
  1653. }
  1654. static void __exit acpi_video_exit(void)
  1655. {
  1656. acpi_video_unregister();
  1657. return;
  1658. }
  1659. module_init(acpi_video_init);
  1660. module_exit(acpi_video_exit);