video.c 55 KB

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