acpi_video.c 58 KB

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