acpi_video.c 54 KB

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