acpi_video.c 56 KB

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