video.c 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168
  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=1163574 */
  466. .callback = video_disable_native_backlight,
  467. .ident = "Dell XPS15 L521X",
  468. .matches = {
  469. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  470. DMI_MATCH(DMI_PRODUCT_NAME, "XPS L521X"),
  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. /*
  661. * Arg:
  662. * device : video output device (LCD, CRT, ..)
  663. *
  664. * Return Value:
  665. * Maximum brightness level
  666. *
  667. * Allocate and initialize device->brightness.
  668. */
  669. static int
  670. acpi_video_init_brightness(struct acpi_video_device *device)
  671. {
  672. union acpi_object *obj = NULL;
  673. int i, max_level = 0, count = 0, level_ac_battery = 0;
  674. unsigned long long level, level_old;
  675. union acpi_object *o;
  676. struct acpi_video_device_brightness *br = NULL;
  677. int result = -EINVAL;
  678. u32 value;
  679. if (!ACPI_SUCCESS(acpi_video_device_lcd_query_levels(device, &obj))) {
  680. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Could not query available "
  681. "LCD brightness level\n"));
  682. goto out;
  683. }
  684. if (obj->package.count < 2)
  685. goto out;
  686. br = kzalloc(sizeof(*br), GFP_KERNEL);
  687. if (!br) {
  688. printk(KERN_ERR "can't allocate memory\n");
  689. result = -ENOMEM;
  690. goto out;
  691. }
  692. br->levels = kmalloc((obj->package.count + 2) * sizeof *(br->levels),
  693. GFP_KERNEL);
  694. if (!br->levels) {
  695. result = -ENOMEM;
  696. goto out_free;
  697. }
  698. for (i = 0; i < obj->package.count; i++) {
  699. o = (union acpi_object *)&obj->package.elements[i];
  700. if (o->type != ACPI_TYPE_INTEGER) {
  701. printk(KERN_ERR PREFIX "Invalid data\n");
  702. continue;
  703. }
  704. value = (u32) o->integer.value;
  705. /* Skip duplicate entries */
  706. if (count > 2 && br->levels[count - 1] == value)
  707. continue;
  708. br->levels[count] = value;
  709. if (br->levels[count] > max_level)
  710. max_level = br->levels[count];
  711. count++;
  712. }
  713. /*
  714. * some buggy BIOS don't export the levels
  715. * when machine is on AC/Battery in _BCL package.
  716. * In this case, the first two elements in _BCL packages
  717. * are also supported brightness levels that OS should take care of.
  718. */
  719. for (i = 2; i < count; i++) {
  720. if (br->levels[i] == br->levels[0])
  721. level_ac_battery++;
  722. if (br->levels[i] == br->levels[1])
  723. level_ac_battery++;
  724. }
  725. if (level_ac_battery < 2) {
  726. level_ac_battery = 2 - level_ac_battery;
  727. br->flags._BCL_no_ac_battery_levels = 1;
  728. for (i = (count - 1 + level_ac_battery); i >= 2; i--)
  729. br->levels[i] = br->levels[i - level_ac_battery];
  730. count += level_ac_battery;
  731. } else if (level_ac_battery > 2)
  732. ACPI_ERROR((AE_INFO, "Too many duplicates in _BCL package"));
  733. /* Check if the _BCL package is in a reversed order */
  734. if (max_level == br->levels[2]) {
  735. br->flags._BCL_reversed = 1;
  736. sort(&br->levels[2], count - 2, sizeof(br->levels[2]),
  737. acpi_video_cmp_level, NULL);
  738. } else if (max_level != br->levels[count - 1])
  739. ACPI_ERROR((AE_INFO,
  740. "Found unordered _BCL package"));
  741. br->count = count;
  742. device->brightness = br;
  743. /* _BQC uses INDEX while _BCL uses VALUE in some laptops */
  744. br->curr = level = max_level;
  745. if (!device->cap._BQC)
  746. goto set_level;
  747. result = acpi_video_device_lcd_get_level_current(device,
  748. &level_old, true);
  749. if (result)
  750. goto out_free_levels;
  751. result = acpi_video_bqc_quirk(device, max_level, level_old);
  752. if (result)
  753. goto out_free_levels;
  754. /*
  755. * cap._BQC may get cleared due to _BQC is found to be broken
  756. * in acpi_video_bqc_quirk, so check again here.
  757. */
  758. if (!device->cap._BQC)
  759. goto set_level;
  760. level = acpi_video_bqc_value_to_level(device, level_old);
  761. /*
  762. * On some buggy laptops, _BQC returns an uninitialized
  763. * value when invoked for the first time, i.e.
  764. * level_old is invalid (no matter whether it's a level
  765. * or an index). Set the backlight to max_level in this case.
  766. */
  767. for (i = 2; i < br->count; i++)
  768. if (level == br->levels[i])
  769. break;
  770. if (i == br->count || !level)
  771. level = max_level;
  772. set_level:
  773. result = acpi_video_device_lcd_set_level(device, level);
  774. if (result)
  775. goto out_free_levels;
  776. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  777. "found %d brightness levels\n", count - 2));
  778. kfree(obj);
  779. return result;
  780. out_free_levels:
  781. kfree(br->levels);
  782. out_free:
  783. kfree(br);
  784. out:
  785. device->brightness = NULL;
  786. kfree(obj);
  787. return result;
  788. }
  789. /*
  790. * Arg:
  791. * device : video output device (LCD, CRT, ..)
  792. *
  793. * Return Value:
  794. * None
  795. *
  796. * Find out all required AML methods defined under the output
  797. * device.
  798. */
  799. static void acpi_video_device_find_cap(struct acpi_video_device *device)
  800. {
  801. if (acpi_has_method(device->dev->handle, "_ADR"))
  802. device->cap._ADR = 1;
  803. if (acpi_has_method(device->dev->handle, "_BCL"))
  804. device->cap._BCL = 1;
  805. if (acpi_has_method(device->dev->handle, "_BCM"))
  806. device->cap._BCM = 1;
  807. if (acpi_has_method(device->dev->handle, "_BQC")) {
  808. device->cap._BQC = 1;
  809. } else if (acpi_has_method(device->dev->handle, "_BCQ")) {
  810. printk(KERN_WARNING FW_BUG "_BCQ is used instead of _BQC\n");
  811. device->cap._BCQ = 1;
  812. }
  813. if (acpi_has_method(device->dev->handle, "_DDC"))
  814. device->cap._DDC = 1;
  815. }
  816. /*
  817. * Arg:
  818. * device : video output device (VGA)
  819. *
  820. * Return Value:
  821. * None
  822. *
  823. * Find out all required AML methods defined under the video bus device.
  824. */
  825. static void acpi_video_bus_find_cap(struct acpi_video_bus *video)
  826. {
  827. if (acpi_has_method(video->device->handle, "_DOS"))
  828. video->cap._DOS = 1;
  829. if (acpi_has_method(video->device->handle, "_DOD"))
  830. video->cap._DOD = 1;
  831. if (acpi_has_method(video->device->handle, "_ROM"))
  832. video->cap._ROM = 1;
  833. if (acpi_has_method(video->device->handle, "_GPD"))
  834. video->cap._GPD = 1;
  835. if (acpi_has_method(video->device->handle, "_SPD"))
  836. video->cap._SPD = 1;
  837. if (acpi_has_method(video->device->handle, "_VPO"))
  838. video->cap._VPO = 1;
  839. }
  840. /*
  841. * Check whether the video bus device has required AML method to
  842. * support the desired features
  843. */
  844. static int acpi_video_bus_check(struct acpi_video_bus *video)
  845. {
  846. acpi_status status = -ENOENT;
  847. struct pci_dev *dev;
  848. if (!video)
  849. return -EINVAL;
  850. dev = acpi_get_pci_dev(video->device->handle);
  851. if (!dev)
  852. return -ENODEV;
  853. pci_dev_put(dev);
  854. /*
  855. * Since there is no HID, CID and so on for VGA driver, we have
  856. * to check well known required nodes.
  857. */
  858. /* Does this device support video switching? */
  859. if (video->cap._DOS || video->cap._DOD) {
  860. if (!video->cap._DOS) {
  861. printk(KERN_WARNING FW_BUG
  862. "ACPI(%s) defines _DOD but not _DOS\n",
  863. acpi_device_bid(video->device));
  864. }
  865. video->flags.multihead = 1;
  866. status = 0;
  867. }
  868. /* Does this device support retrieving a video ROM? */
  869. if (video->cap._ROM) {
  870. video->flags.rom = 1;
  871. status = 0;
  872. }
  873. /* Does this device support configuring which video device to POST? */
  874. if (video->cap._GPD && video->cap._SPD && video->cap._VPO) {
  875. video->flags.post = 1;
  876. status = 0;
  877. }
  878. return status;
  879. }
  880. /*
  881. * --------------------------------------------------------------------------
  882. * Driver Interface
  883. * --------------------------------------------------------------------------
  884. */
  885. /* device interface */
  886. static struct acpi_video_device_attrib *
  887. acpi_video_get_device_attr(struct acpi_video_bus *video, unsigned long device_id)
  888. {
  889. struct acpi_video_enumerated_device *ids;
  890. int i;
  891. for (i = 0; i < video->attached_count; i++) {
  892. ids = &video->attached_array[i];
  893. if ((ids->value.int_val & 0xffff) == device_id)
  894. return &ids->value.attrib;
  895. }
  896. return NULL;
  897. }
  898. static int
  899. acpi_video_get_device_type(struct acpi_video_bus *video,
  900. unsigned long device_id)
  901. {
  902. struct acpi_video_enumerated_device *ids;
  903. int i;
  904. for (i = 0; i < video->attached_count; i++) {
  905. ids = &video->attached_array[i];
  906. if ((ids->value.int_val & 0xffff) == device_id)
  907. return ids->value.int_val;
  908. }
  909. return 0;
  910. }
  911. static int
  912. acpi_video_bus_get_one_device(struct acpi_device *device,
  913. struct acpi_video_bus *video)
  914. {
  915. unsigned long long device_id;
  916. int status, device_type;
  917. struct acpi_video_device *data;
  918. struct acpi_video_device_attrib *attribute;
  919. status =
  920. acpi_evaluate_integer(device->handle, "_ADR", NULL, &device_id);
  921. /* Some device omits _ADR, we skip them instead of fail */
  922. if (ACPI_FAILURE(status))
  923. return 0;
  924. data = kzalloc(sizeof(struct acpi_video_device), GFP_KERNEL);
  925. if (!data)
  926. return -ENOMEM;
  927. strcpy(acpi_device_name(device), ACPI_VIDEO_DEVICE_NAME);
  928. strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
  929. device->driver_data = data;
  930. data->device_id = device_id;
  931. data->video = video;
  932. data->dev = device;
  933. INIT_DELAYED_WORK(&data->switch_brightness_work,
  934. acpi_video_switch_brightness);
  935. attribute = acpi_video_get_device_attr(video, device_id);
  936. if (attribute && attribute->device_id_scheme) {
  937. switch (attribute->display_type) {
  938. case ACPI_VIDEO_DISPLAY_CRT:
  939. data->flags.crt = 1;
  940. break;
  941. case ACPI_VIDEO_DISPLAY_TV:
  942. data->flags.tvout = 1;
  943. break;
  944. case ACPI_VIDEO_DISPLAY_DVI:
  945. data->flags.dvi = 1;
  946. break;
  947. case ACPI_VIDEO_DISPLAY_LCD:
  948. data->flags.lcd = 1;
  949. break;
  950. default:
  951. data->flags.unknown = 1;
  952. break;
  953. }
  954. if (attribute->bios_can_detect)
  955. data->flags.bios = 1;
  956. } else {
  957. /* Check for legacy IDs */
  958. device_type = acpi_video_get_device_type(video, device_id);
  959. /* Ignore bits 16 and 18-20 */
  960. switch (device_type & 0xffe2ffff) {
  961. case ACPI_VIDEO_DISPLAY_LEGACY_MONITOR:
  962. data->flags.crt = 1;
  963. break;
  964. case ACPI_VIDEO_DISPLAY_LEGACY_PANEL:
  965. data->flags.lcd = 1;
  966. break;
  967. case ACPI_VIDEO_DISPLAY_LEGACY_TV:
  968. data->flags.tvout = 1;
  969. break;
  970. default:
  971. data->flags.unknown = 1;
  972. }
  973. }
  974. acpi_video_device_bind(video, data);
  975. acpi_video_device_find_cap(data);
  976. mutex_lock(&video->device_list_lock);
  977. list_add_tail(&data->entry, &video->video_device_list);
  978. mutex_unlock(&video->device_list_lock);
  979. return status;
  980. }
  981. /*
  982. * Arg:
  983. * video : video bus device
  984. *
  985. * Return:
  986. * none
  987. *
  988. * Enumerate the video device list of the video bus,
  989. * bind the ids with the corresponding video devices
  990. * under the video bus.
  991. */
  992. static void acpi_video_device_rebind(struct acpi_video_bus *video)
  993. {
  994. struct acpi_video_device *dev;
  995. mutex_lock(&video->device_list_lock);
  996. list_for_each_entry(dev, &video->video_device_list, entry)
  997. acpi_video_device_bind(video, dev);
  998. mutex_unlock(&video->device_list_lock);
  999. }
  1000. /*
  1001. * Arg:
  1002. * video : video bus device
  1003. * device : video output device under the video
  1004. * bus
  1005. *
  1006. * Return:
  1007. * none
  1008. *
  1009. * Bind the ids with the corresponding video devices
  1010. * under the video bus.
  1011. */
  1012. static void
  1013. acpi_video_device_bind(struct acpi_video_bus *video,
  1014. struct acpi_video_device *device)
  1015. {
  1016. struct acpi_video_enumerated_device *ids;
  1017. int i;
  1018. for (i = 0; i < video->attached_count; i++) {
  1019. ids = &video->attached_array[i];
  1020. if (device->device_id == (ids->value.int_val & 0xffff)) {
  1021. ids->bind_info = device;
  1022. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "device_bind %d\n", i));
  1023. }
  1024. }
  1025. }
  1026. static bool acpi_video_device_in_dod(struct acpi_video_device *device)
  1027. {
  1028. struct acpi_video_bus *video = device->video;
  1029. int i;
  1030. /*
  1031. * If we have a broken _DOD or we have more than 8 output devices
  1032. * under the graphics controller node that we can't proper deal with
  1033. * in the operation region code currently, no need to test.
  1034. */
  1035. if (!video->attached_count || video->child_count > 8)
  1036. return true;
  1037. for (i = 0; i < video->attached_count; i++) {
  1038. if ((video->attached_array[i].value.int_val & 0xfff) ==
  1039. (device->device_id & 0xfff))
  1040. return true;
  1041. }
  1042. return false;
  1043. }
  1044. /*
  1045. * Arg:
  1046. * video : video bus device
  1047. *
  1048. * Return:
  1049. * < 0 : error
  1050. *
  1051. * Call _DOD to enumerate all devices attached to display adapter
  1052. *
  1053. */
  1054. static int acpi_video_device_enumerate(struct acpi_video_bus *video)
  1055. {
  1056. int status;
  1057. int count;
  1058. int i;
  1059. struct acpi_video_enumerated_device *active_list;
  1060. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  1061. union acpi_object *dod = NULL;
  1062. union acpi_object *obj;
  1063. status = acpi_evaluate_object(video->device->handle, "_DOD", NULL, &buffer);
  1064. if (!ACPI_SUCCESS(status)) {
  1065. ACPI_EXCEPTION((AE_INFO, status, "Evaluating _DOD"));
  1066. return status;
  1067. }
  1068. dod = buffer.pointer;
  1069. if (!dod || (dod->type != ACPI_TYPE_PACKAGE)) {
  1070. ACPI_EXCEPTION((AE_INFO, status, "Invalid _DOD data"));
  1071. status = -EFAULT;
  1072. goto out;
  1073. }
  1074. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d video heads in _DOD\n",
  1075. dod->package.count));
  1076. active_list = kcalloc(1 + dod->package.count,
  1077. sizeof(struct acpi_video_enumerated_device),
  1078. GFP_KERNEL);
  1079. if (!active_list) {
  1080. status = -ENOMEM;
  1081. goto out;
  1082. }
  1083. count = 0;
  1084. for (i = 0; i < dod->package.count; i++) {
  1085. obj = &dod->package.elements[i];
  1086. if (obj->type != ACPI_TYPE_INTEGER) {
  1087. printk(KERN_ERR PREFIX
  1088. "Invalid _DOD data in element %d\n", i);
  1089. continue;
  1090. }
  1091. active_list[count].value.int_val = obj->integer.value;
  1092. active_list[count].bind_info = NULL;
  1093. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "dod element[%d] = %d\n", i,
  1094. (int)obj->integer.value));
  1095. count++;
  1096. }
  1097. kfree(video->attached_array);
  1098. video->attached_array = active_list;
  1099. video->attached_count = count;
  1100. out:
  1101. kfree(buffer.pointer);
  1102. return status;
  1103. }
  1104. static int
  1105. acpi_video_get_next_level(struct acpi_video_device *device,
  1106. u32 level_current, u32 event)
  1107. {
  1108. int min, max, min_above, max_below, i, l, delta = 255;
  1109. max = max_below = 0;
  1110. min = min_above = 255;
  1111. /* Find closest level to level_current */
  1112. for (i = 2; i < device->brightness->count; i++) {
  1113. l = device->brightness->levels[i];
  1114. if (abs(l - level_current) < abs(delta)) {
  1115. delta = l - level_current;
  1116. if (!delta)
  1117. break;
  1118. }
  1119. }
  1120. /* Ajust level_current to closest available level */
  1121. level_current += delta;
  1122. for (i = 2; i < device->brightness->count; i++) {
  1123. l = device->brightness->levels[i];
  1124. if (l < min)
  1125. min = l;
  1126. if (l > max)
  1127. max = l;
  1128. if (l < min_above && l > level_current)
  1129. min_above = l;
  1130. if (l > max_below && l < level_current)
  1131. max_below = l;
  1132. }
  1133. switch (event) {
  1134. case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS:
  1135. return (level_current < max) ? min_above : min;
  1136. case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS:
  1137. return (level_current < max) ? min_above : max;
  1138. case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS:
  1139. return (level_current > min) ? max_below : min;
  1140. case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS:
  1141. case ACPI_VIDEO_NOTIFY_DISPLAY_OFF:
  1142. return 0;
  1143. default:
  1144. return level_current;
  1145. }
  1146. }
  1147. static void
  1148. acpi_video_switch_brightness(struct work_struct *work)
  1149. {
  1150. struct acpi_video_device *device = container_of(to_delayed_work(work),
  1151. struct acpi_video_device, switch_brightness_work);
  1152. unsigned long long level_current, level_next;
  1153. int event = device->switch_brightness_event;
  1154. int result = -EINVAL;
  1155. /* no warning message if acpi_backlight=vendor or a quirk is used */
  1156. if (!acpi_video_verify_backlight_support())
  1157. return;
  1158. if (!device->brightness)
  1159. goto out;
  1160. result = acpi_video_device_lcd_get_level_current(device,
  1161. &level_current,
  1162. false);
  1163. if (result)
  1164. goto out;
  1165. level_next = acpi_video_get_next_level(device, level_current, event);
  1166. result = acpi_video_device_lcd_set_level(device, level_next);
  1167. if (!result)
  1168. backlight_force_update(device->backlight,
  1169. BACKLIGHT_UPDATE_HOTKEY);
  1170. out:
  1171. if (result)
  1172. printk(KERN_ERR PREFIX "Failed to switch the brightness\n");
  1173. }
  1174. int acpi_video_get_edid(struct acpi_device *device, int type, int device_id,
  1175. void **edid)
  1176. {
  1177. struct acpi_video_bus *video;
  1178. struct acpi_video_device *video_device;
  1179. union acpi_object *buffer = NULL;
  1180. acpi_status status;
  1181. int i, length;
  1182. if (!device || !acpi_driver_data(device))
  1183. return -EINVAL;
  1184. video = acpi_driver_data(device);
  1185. for (i = 0; i < video->attached_count; i++) {
  1186. video_device = video->attached_array[i].bind_info;
  1187. length = 256;
  1188. if (!video_device)
  1189. continue;
  1190. if (!video_device->cap._DDC)
  1191. continue;
  1192. if (type) {
  1193. switch (type) {
  1194. case ACPI_VIDEO_DISPLAY_CRT:
  1195. if (!video_device->flags.crt)
  1196. continue;
  1197. break;
  1198. case ACPI_VIDEO_DISPLAY_TV:
  1199. if (!video_device->flags.tvout)
  1200. continue;
  1201. break;
  1202. case ACPI_VIDEO_DISPLAY_DVI:
  1203. if (!video_device->flags.dvi)
  1204. continue;
  1205. break;
  1206. case ACPI_VIDEO_DISPLAY_LCD:
  1207. if (!video_device->flags.lcd)
  1208. continue;
  1209. break;
  1210. }
  1211. } else if (video_device->device_id != device_id) {
  1212. continue;
  1213. }
  1214. status = acpi_video_device_EDID(video_device, &buffer, length);
  1215. if (ACPI_FAILURE(status) || !buffer ||
  1216. buffer->type != ACPI_TYPE_BUFFER) {
  1217. length = 128;
  1218. status = acpi_video_device_EDID(video_device, &buffer,
  1219. length);
  1220. if (ACPI_FAILURE(status) || !buffer ||
  1221. buffer->type != ACPI_TYPE_BUFFER) {
  1222. continue;
  1223. }
  1224. }
  1225. *edid = buffer->buffer.pointer;
  1226. return length;
  1227. }
  1228. return -ENODEV;
  1229. }
  1230. EXPORT_SYMBOL(acpi_video_get_edid);
  1231. static int
  1232. acpi_video_bus_get_devices(struct acpi_video_bus *video,
  1233. struct acpi_device *device)
  1234. {
  1235. int status = 0;
  1236. struct acpi_device *dev;
  1237. /*
  1238. * There are systems where video module known to work fine regardless
  1239. * of broken _DOD and ignoring returned value here doesn't cause
  1240. * any issues later.
  1241. */
  1242. acpi_video_device_enumerate(video);
  1243. list_for_each_entry(dev, &device->children, node) {
  1244. status = acpi_video_bus_get_one_device(dev, video);
  1245. if (status) {
  1246. dev_err(&dev->dev, "Can't attach device\n");
  1247. break;
  1248. }
  1249. video->child_count++;
  1250. }
  1251. return status;
  1252. }
  1253. /* acpi_video interface */
  1254. /*
  1255. * Win8 requires setting bit2 of _DOS to let firmware know it shouldn't
  1256. * preform any automatic brightness change on receiving a notification.
  1257. */
  1258. static int acpi_video_bus_start_devices(struct acpi_video_bus *video)
  1259. {
  1260. return acpi_video_bus_DOS(video, 0,
  1261. acpi_osi_is_win8() ? 1 : 0);
  1262. }
  1263. static int acpi_video_bus_stop_devices(struct acpi_video_bus *video)
  1264. {
  1265. return acpi_video_bus_DOS(video, 0,
  1266. acpi_osi_is_win8() ? 0 : 1);
  1267. }
  1268. static void acpi_video_bus_notify(struct acpi_device *device, u32 event)
  1269. {
  1270. struct acpi_video_bus *video = acpi_driver_data(device);
  1271. struct input_dev *input;
  1272. int keycode = 0;
  1273. if (!video || !video->input)
  1274. return;
  1275. input = video->input;
  1276. switch (event) {
  1277. case ACPI_VIDEO_NOTIFY_SWITCH: /* User requested a switch,
  1278. * most likely via hotkey. */
  1279. keycode = KEY_SWITCHVIDEOMODE;
  1280. break;
  1281. case ACPI_VIDEO_NOTIFY_PROBE: /* User plugged in or removed a video
  1282. * connector. */
  1283. acpi_video_device_enumerate(video);
  1284. acpi_video_device_rebind(video);
  1285. keycode = KEY_SWITCHVIDEOMODE;
  1286. break;
  1287. case ACPI_VIDEO_NOTIFY_CYCLE: /* Cycle Display output hotkey pressed. */
  1288. keycode = KEY_SWITCHVIDEOMODE;
  1289. break;
  1290. case ACPI_VIDEO_NOTIFY_NEXT_OUTPUT: /* Next Display output hotkey pressed. */
  1291. keycode = KEY_VIDEO_NEXT;
  1292. break;
  1293. case ACPI_VIDEO_NOTIFY_PREV_OUTPUT: /* previous Display output hotkey pressed. */
  1294. keycode = KEY_VIDEO_PREV;
  1295. break;
  1296. default:
  1297. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  1298. "Unsupported event [0x%x]\n", event));
  1299. break;
  1300. }
  1301. if (acpi_notifier_call_chain(device, event, 0))
  1302. /* Something vetoed the keypress. */
  1303. keycode = 0;
  1304. if (keycode) {
  1305. input_report_key(input, keycode, 1);
  1306. input_sync(input);
  1307. input_report_key(input, keycode, 0);
  1308. input_sync(input);
  1309. }
  1310. return;
  1311. }
  1312. static void brightness_switch_event(struct acpi_video_device *video_device,
  1313. u32 event)
  1314. {
  1315. if (!brightness_switch_enabled)
  1316. return;
  1317. video_device->switch_brightness_event = event;
  1318. schedule_delayed_work(&video_device->switch_brightness_work, HZ / 10);
  1319. }
  1320. static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data)
  1321. {
  1322. struct acpi_video_device *video_device = data;
  1323. struct acpi_device *device = NULL;
  1324. struct acpi_video_bus *bus;
  1325. struct input_dev *input;
  1326. int keycode = 0;
  1327. if (!video_device)
  1328. return;
  1329. device = video_device->dev;
  1330. bus = video_device->video;
  1331. input = bus->input;
  1332. switch (event) {
  1333. case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS: /* Cycle brightness */
  1334. brightness_switch_event(video_device, event);
  1335. keycode = KEY_BRIGHTNESS_CYCLE;
  1336. break;
  1337. case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS: /* Increase brightness */
  1338. brightness_switch_event(video_device, event);
  1339. keycode = KEY_BRIGHTNESSUP;
  1340. break;
  1341. case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS: /* Decrease brightness */
  1342. brightness_switch_event(video_device, event);
  1343. keycode = KEY_BRIGHTNESSDOWN;
  1344. break;
  1345. case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS: /* zero brightness */
  1346. brightness_switch_event(video_device, event);
  1347. keycode = KEY_BRIGHTNESS_ZERO;
  1348. break;
  1349. case ACPI_VIDEO_NOTIFY_DISPLAY_OFF: /* display device off */
  1350. brightness_switch_event(video_device, event);
  1351. keycode = KEY_DISPLAY_OFF;
  1352. break;
  1353. default:
  1354. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  1355. "Unsupported event [0x%x]\n", event));
  1356. break;
  1357. }
  1358. acpi_notifier_call_chain(device, event, 0);
  1359. if (keycode) {
  1360. input_report_key(input, keycode, 1);
  1361. input_sync(input);
  1362. input_report_key(input, keycode, 0);
  1363. input_sync(input);
  1364. }
  1365. return;
  1366. }
  1367. static int acpi_video_resume(struct notifier_block *nb,
  1368. unsigned long val, void *ign)
  1369. {
  1370. struct acpi_video_bus *video;
  1371. struct acpi_video_device *video_device;
  1372. int i;
  1373. switch (val) {
  1374. case PM_HIBERNATION_PREPARE:
  1375. case PM_SUSPEND_PREPARE:
  1376. case PM_RESTORE_PREPARE:
  1377. return NOTIFY_DONE;
  1378. }
  1379. video = container_of(nb, struct acpi_video_bus, pm_nb);
  1380. dev_info(&video->device->dev, "Restoring backlight state\n");
  1381. for (i = 0; i < video->attached_count; i++) {
  1382. video_device = video->attached_array[i].bind_info;
  1383. if (video_device && video_device->backlight)
  1384. acpi_video_set_brightness(video_device->backlight);
  1385. }
  1386. return NOTIFY_OK;
  1387. }
  1388. static acpi_status
  1389. acpi_video_bus_match(acpi_handle handle, u32 level, void *context,
  1390. void **return_value)
  1391. {
  1392. struct acpi_device *device = context;
  1393. struct acpi_device *sibling;
  1394. int result;
  1395. if (handle == device->handle)
  1396. return AE_CTRL_TERMINATE;
  1397. result = acpi_bus_get_device(handle, &sibling);
  1398. if (result)
  1399. return AE_OK;
  1400. if (!strcmp(acpi_device_name(sibling), ACPI_VIDEO_BUS_NAME))
  1401. return AE_ALREADY_EXISTS;
  1402. return AE_OK;
  1403. }
  1404. static void acpi_video_dev_register_backlight(struct acpi_video_device *device)
  1405. {
  1406. struct backlight_properties props;
  1407. struct pci_dev *pdev;
  1408. acpi_handle acpi_parent;
  1409. struct device *parent = NULL;
  1410. int result;
  1411. static int count;
  1412. char *name;
  1413. /*
  1414. * Do not create backlight device for video output
  1415. * device that is not in the enumerated list.
  1416. */
  1417. if (!acpi_video_device_in_dod(device)) {
  1418. dev_dbg(&device->dev->dev, "not in _DOD list, ignore\n");
  1419. return;
  1420. }
  1421. result = acpi_video_init_brightness(device);
  1422. if (result)
  1423. return;
  1424. name = kasprintf(GFP_KERNEL, "acpi_video%d", count);
  1425. if (!name)
  1426. return;
  1427. count++;
  1428. acpi_get_parent(device->dev->handle, &acpi_parent);
  1429. pdev = acpi_get_pci_dev(acpi_parent);
  1430. if (pdev) {
  1431. parent = &pdev->dev;
  1432. pci_dev_put(pdev);
  1433. }
  1434. memset(&props, 0, sizeof(struct backlight_properties));
  1435. props.type = BACKLIGHT_FIRMWARE;
  1436. props.max_brightness = device->brightness->count - 3;
  1437. device->backlight = backlight_device_register(name,
  1438. parent,
  1439. device,
  1440. &acpi_backlight_ops,
  1441. &props);
  1442. kfree(name);
  1443. if (IS_ERR(device->backlight))
  1444. return;
  1445. /*
  1446. * Save current brightness level in case we have to restore it
  1447. * before acpi_video_device_lcd_set_level() is called next time.
  1448. */
  1449. device->backlight->props.brightness =
  1450. acpi_video_get_brightness(device->backlight);
  1451. device->cooling_dev = thermal_cooling_device_register("LCD",
  1452. device->dev, &video_cooling_ops);
  1453. if (IS_ERR(device->cooling_dev)) {
  1454. /*
  1455. * Set cooling_dev to NULL so we don't crash trying to free it.
  1456. * Also, why the hell we are returning early and not attempt to
  1457. * register video output if cooling device registration failed?
  1458. * -- dtor
  1459. */
  1460. device->cooling_dev = NULL;
  1461. return;
  1462. }
  1463. dev_info(&device->dev->dev, "registered as cooling_device%d\n",
  1464. device->cooling_dev->id);
  1465. result = sysfs_create_link(&device->dev->dev.kobj,
  1466. &device->cooling_dev->device.kobj,
  1467. "thermal_cooling");
  1468. if (result)
  1469. printk(KERN_ERR PREFIX "Create sysfs link\n");
  1470. result = sysfs_create_link(&device->cooling_dev->device.kobj,
  1471. &device->dev->dev.kobj, "device");
  1472. if (result)
  1473. printk(KERN_ERR PREFIX "Create sysfs link\n");
  1474. }
  1475. static void acpi_video_run_bcl_for_osi(struct acpi_video_bus *video)
  1476. {
  1477. struct acpi_video_device *dev;
  1478. union acpi_object *levels;
  1479. mutex_lock(&video->device_list_lock);
  1480. list_for_each_entry(dev, &video->video_device_list, entry) {
  1481. if (!acpi_video_device_lcd_query_levels(dev, &levels))
  1482. kfree(levels);
  1483. }
  1484. mutex_unlock(&video->device_list_lock);
  1485. }
  1486. static int acpi_video_bus_register_backlight(struct acpi_video_bus *video)
  1487. {
  1488. struct acpi_video_device *dev;
  1489. if (video->backlight_registered)
  1490. return 0;
  1491. acpi_video_run_bcl_for_osi(video);
  1492. if (!acpi_video_verify_backlight_support())
  1493. return 0;
  1494. mutex_lock(&video->device_list_lock);
  1495. list_for_each_entry(dev, &video->video_device_list, entry)
  1496. acpi_video_dev_register_backlight(dev);
  1497. mutex_unlock(&video->device_list_lock);
  1498. video->backlight_registered = true;
  1499. video->pm_nb.notifier_call = acpi_video_resume;
  1500. video->pm_nb.priority = 0;
  1501. return register_pm_notifier(&video->pm_nb);
  1502. }
  1503. static void acpi_video_dev_unregister_backlight(struct acpi_video_device *device)
  1504. {
  1505. if (device->backlight) {
  1506. backlight_device_unregister(device->backlight);
  1507. device->backlight = NULL;
  1508. }
  1509. if (device->brightness) {
  1510. kfree(device->brightness->levels);
  1511. kfree(device->brightness);
  1512. device->brightness = NULL;
  1513. }
  1514. if (device->cooling_dev) {
  1515. sysfs_remove_link(&device->dev->dev.kobj, "thermal_cooling");
  1516. sysfs_remove_link(&device->cooling_dev->device.kobj, "device");
  1517. thermal_cooling_device_unregister(device->cooling_dev);
  1518. device->cooling_dev = NULL;
  1519. }
  1520. }
  1521. static int acpi_video_bus_unregister_backlight(struct acpi_video_bus *video)
  1522. {
  1523. struct acpi_video_device *dev;
  1524. int error;
  1525. if (!video->backlight_registered)
  1526. return 0;
  1527. error = unregister_pm_notifier(&video->pm_nb);
  1528. mutex_lock(&video->device_list_lock);
  1529. list_for_each_entry(dev, &video->video_device_list, entry)
  1530. acpi_video_dev_unregister_backlight(dev);
  1531. mutex_unlock(&video->device_list_lock);
  1532. video->backlight_registered = false;
  1533. return error;
  1534. }
  1535. static void acpi_video_dev_add_notify_handler(struct acpi_video_device *device)
  1536. {
  1537. acpi_status status;
  1538. struct acpi_device *adev = device->dev;
  1539. status = acpi_install_notify_handler(adev->handle, ACPI_DEVICE_NOTIFY,
  1540. acpi_video_device_notify, device);
  1541. if (ACPI_FAILURE(status))
  1542. dev_err(&adev->dev, "Error installing notify handler\n");
  1543. else
  1544. device->flags.notify = 1;
  1545. }
  1546. static int acpi_video_bus_add_notify_handler(struct acpi_video_bus *video)
  1547. {
  1548. struct input_dev *input;
  1549. struct acpi_video_device *dev;
  1550. int error;
  1551. video->input = input = input_allocate_device();
  1552. if (!input) {
  1553. error = -ENOMEM;
  1554. goto out;
  1555. }
  1556. error = acpi_video_bus_start_devices(video);
  1557. if (error)
  1558. goto err_free_input;
  1559. snprintf(video->phys, sizeof(video->phys),
  1560. "%s/video/input0", acpi_device_hid(video->device));
  1561. input->name = acpi_device_name(video->device);
  1562. input->phys = video->phys;
  1563. input->id.bustype = BUS_HOST;
  1564. input->id.product = 0x06;
  1565. input->dev.parent = &video->device->dev;
  1566. input->evbit[0] = BIT(EV_KEY);
  1567. set_bit(KEY_SWITCHVIDEOMODE, input->keybit);
  1568. set_bit(KEY_VIDEO_NEXT, input->keybit);
  1569. set_bit(KEY_VIDEO_PREV, input->keybit);
  1570. set_bit(KEY_BRIGHTNESS_CYCLE, input->keybit);
  1571. set_bit(KEY_BRIGHTNESSUP, input->keybit);
  1572. set_bit(KEY_BRIGHTNESSDOWN, input->keybit);
  1573. set_bit(KEY_BRIGHTNESS_ZERO, input->keybit);
  1574. set_bit(KEY_DISPLAY_OFF, input->keybit);
  1575. error = input_register_device(input);
  1576. if (error)
  1577. goto err_stop_dev;
  1578. mutex_lock(&video->device_list_lock);
  1579. list_for_each_entry(dev, &video->video_device_list, entry)
  1580. acpi_video_dev_add_notify_handler(dev);
  1581. mutex_unlock(&video->device_list_lock);
  1582. return 0;
  1583. err_stop_dev:
  1584. acpi_video_bus_stop_devices(video);
  1585. err_free_input:
  1586. input_free_device(input);
  1587. video->input = NULL;
  1588. out:
  1589. return error;
  1590. }
  1591. static void acpi_video_dev_remove_notify_handler(struct acpi_video_device *dev)
  1592. {
  1593. if (dev->flags.notify) {
  1594. acpi_remove_notify_handler(dev->dev->handle, ACPI_DEVICE_NOTIFY,
  1595. acpi_video_device_notify);
  1596. dev->flags.notify = 0;
  1597. }
  1598. }
  1599. static void acpi_video_bus_remove_notify_handler(struct acpi_video_bus *video)
  1600. {
  1601. struct acpi_video_device *dev;
  1602. mutex_lock(&video->device_list_lock);
  1603. list_for_each_entry(dev, &video->video_device_list, entry)
  1604. acpi_video_dev_remove_notify_handler(dev);
  1605. mutex_unlock(&video->device_list_lock);
  1606. acpi_video_bus_stop_devices(video);
  1607. input_unregister_device(video->input);
  1608. video->input = NULL;
  1609. }
  1610. static int acpi_video_backlight_notify(struct notifier_block *nb,
  1611. unsigned long val, void *bd)
  1612. {
  1613. struct backlight_device *backlight = bd;
  1614. struct acpi_video_bus *video;
  1615. /* acpi_video_verify_backlight_support only cares about raw devices */
  1616. if (backlight->props.type != BACKLIGHT_RAW)
  1617. return NOTIFY_DONE;
  1618. video = container_of(nb, struct acpi_video_bus, backlight_nb);
  1619. switch (val) {
  1620. case BACKLIGHT_REGISTERED:
  1621. if (!acpi_video_verify_backlight_support())
  1622. acpi_video_bus_unregister_backlight(video);
  1623. break;
  1624. case BACKLIGHT_UNREGISTERED:
  1625. acpi_video_bus_register_backlight(video);
  1626. break;
  1627. }
  1628. return NOTIFY_OK;
  1629. }
  1630. static int acpi_video_bus_add_backlight_notify_handler(
  1631. struct acpi_video_bus *video)
  1632. {
  1633. int error;
  1634. video->backlight_nb.notifier_call = acpi_video_backlight_notify;
  1635. video->backlight_nb.priority = 0;
  1636. error = backlight_register_notifier(&video->backlight_nb);
  1637. if (error == 0)
  1638. video->backlight_notifier_registered = true;
  1639. return error;
  1640. }
  1641. static int acpi_video_bus_remove_backlight_notify_handler(
  1642. struct acpi_video_bus *video)
  1643. {
  1644. if (!video->backlight_notifier_registered)
  1645. return 0;
  1646. video->backlight_notifier_registered = false;
  1647. return backlight_unregister_notifier(&video->backlight_nb);
  1648. }
  1649. static int acpi_video_bus_put_devices(struct acpi_video_bus *video)
  1650. {
  1651. struct acpi_video_device *dev, *next;
  1652. mutex_lock(&video->device_list_lock);
  1653. list_for_each_entry_safe(dev, next, &video->video_device_list, entry) {
  1654. list_del(&dev->entry);
  1655. kfree(dev);
  1656. }
  1657. mutex_unlock(&video->device_list_lock);
  1658. return 0;
  1659. }
  1660. static int instance;
  1661. static int acpi_video_bus_add(struct acpi_device *device)
  1662. {
  1663. struct acpi_video_bus *video;
  1664. int error;
  1665. acpi_status status;
  1666. status = acpi_walk_namespace(ACPI_TYPE_DEVICE,
  1667. device->parent->handle, 1,
  1668. acpi_video_bus_match, NULL,
  1669. device, NULL);
  1670. if (status == AE_ALREADY_EXISTS) {
  1671. printk(KERN_WARNING FW_BUG
  1672. "Duplicate ACPI video bus devices for the"
  1673. " same VGA controller, please try module "
  1674. "parameter \"video.allow_duplicates=1\""
  1675. "if the current driver doesn't work.\n");
  1676. if (!allow_duplicates)
  1677. return -ENODEV;
  1678. }
  1679. video = kzalloc(sizeof(struct acpi_video_bus), GFP_KERNEL);
  1680. if (!video)
  1681. return -ENOMEM;
  1682. /* a hack to fix the duplicate name "VID" problem on T61 */
  1683. if (!strcmp(device->pnp.bus_id, "VID")) {
  1684. if (instance)
  1685. device->pnp.bus_id[3] = '0' + instance;
  1686. instance++;
  1687. }
  1688. /* a hack to fix the duplicate name "VGA" problem on Pa 3553 */
  1689. if (!strcmp(device->pnp.bus_id, "VGA")) {
  1690. if (instance)
  1691. device->pnp.bus_id[3] = '0' + instance;
  1692. instance++;
  1693. }
  1694. video->device = device;
  1695. strcpy(acpi_device_name(device), ACPI_VIDEO_BUS_NAME);
  1696. strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
  1697. device->driver_data = video;
  1698. acpi_video_bus_find_cap(video);
  1699. error = acpi_video_bus_check(video);
  1700. if (error)
  1701. goto err_free_video;
  1702. mutex_init(&video->device_list_lock);
  1703. INIT_LIST_HEAD(&video->video_device_list);
  1704. error = acpi_video_bus_get_devices(video, device);
  1705. if (error)
  1706. goto err_put_video;
  1707. printk(KERN_INFO PREFIX "%s [%s] (multi-head: %s rom: %s post: %s)\n",
  1708. ACPI_VIDEO_DEVICE_NAME, acpi_device_bid(device),
  1709. video->flags.multihead ? "yes" : "no",
  1710. video->flags.rom ? "yes" : "no",
  1711. video->flags.post ? "yes" : "no");
  1712. mutex_lock(&video_list_lock);
  1713. list_add_tail(&video->entry, &video_bus_head);
  1714. mutex_unlock(&video_list_lock);
  1715. acpi_video_bus_register_backlight(video);
  1716. acpi_video_bus_add_notify_handler(video);
  1717. acpi_video_bus_add_backlight_notify_handler(video);
  1718. return 0;
  1719. err_put_video:
  1720. acpi_video_bus_put_devices(video);
  1721. kfree(video->attached_array);
  1722. err_free_video:
  1723. kfree(video);
  1724. device->driver_data = NULL;
  1725. return error;
  1726. }
  1727. static int acpi_video_bus_remove(struct acpi_device *device)
  1728. {
  1729. struct acpi_video_bus *video = NULL;
  1730. if (!device || !acpi_driver_data(device))
  1731. return -EINVAL;
  1732. video = acpi_driver_data(device);
  1733. acpi_video_bus_remove_backlight_notify_handler(video);
  1734. acpi_video_bus_remove_notify_handler(video);
  1735. acpi_video_bus_unregister_backlight(video);
  1736. acpi_video_bus_put_devices(video);
  1737. mutex_lock(&video_list_lock);
  1738. list_del(&video->entry);
  1739. mutex_unlock(&video_list_lock);
  1740. kfree(video->attached_array);
  1741. kfree(video);
  1742. return 0;
  1743. }
  1744. static int __init is_i740(struct pci_dev *dev)
  1745. {
  1746. if (dev->device == 0x00D1)
  1747. return 1;
  1748. if (dev->device == 0x7000)
  1749. return 1;
  1750. return 0;
  1751. }
  1752. static int __init intel_opregion_present(void)
  1753. {
  1754. int opregion = 0;
  1755. struct pci_dev *dev = NULL;
  1756. u32 address;
  1757. for_each_pci_dev(dev) {
  1758. if ((dev->class >> 8) != PCI_CLASS_DISPLAY_VGA)
  1759. continue;
  1760. if (dev->vendor != PCI_VENDOR_ID_INTEL)
  1761. continue;
  1762. /* We don't want to poke around undefined i740 registers */
  1763. if (is_i740(dev))
  1764. continue;
  1765. pci_read_config_dword(dev, 0xfc, &address);
  1766. if (!address)
  1767. continue;
  1768. opregion = 1;
  1769. }
  1770. return opregion;
  1771. }
  1772. int acpi_video_register(void)
  1773. {
  1774. int result = 0;
  1775. if (register_count) {
  1776. /*
  1777. * if the function of acpi_video_register is already called,
  1778. * don't register the acpi_vide_bus again and return no error.
  1779. */
  1780. return 0;
  1781. }
  1782. mutex_init(&video_list_lock);
  1783. INIT_LIST_HEAD(&video_bus_head);
  1784. result = acpi_bus_register_driver(&acpi_video_bus);
  1785. if (result < 0)
  1786. return -ENODEV;
  1787. /*
  1788. * When the acpi_video_bus is loaded successfully, increase
  1789. * the counter reference.
  1790. */
  1791. register_count = 1;
  1792. return 0;
  1793. }
  1794. EXPORT_SYMBOL(acpi_video_register);
  1795. void acpi_video_unregister(void)
  1796. {
  1797. if (!register_count) {
  1798. /*
  1799. * If the acpi video bus is already unloaded, don't
  1800. * unload it again and return directly.
  1801. */
  1802. return;
  1803. }
  1804. acpi_bus_unregister_driver(&acpi_video_bus);
  1805. register_count = 0;
  1806. return;
  1807. }
  1808. EXPORT_SYMBOL(acpi_video_unregister);
  1809. void acpi_video_unregister_backlight(void)
  1810. {
  1811. struct acpi_video_bus *video;
  1812. if (!register_count)
  1813. return;
  1814. mutex_lock(&video_list_lock);
  1815. list_for_each_entry(video, &video_bus_head, entry)
  1816. acpi_video_bus_unregister_backlight(video);
  1817. mutex_unlock(&video_list_lock);
  1818. }
  1819. EXPORT_SYMBOL(acpi_video_unregister_backlight);
  1820. /*
  1821. * This is kind of nasty. Hardware using Intel chipsets may require
  1822. * the video opregion code to be run first in order to initialise
  1823. * state before any ACPI video calls are made. To handle this we defer
  1824. * registration of the video class until the opregion code has run.
  1825. */
  1826. static int __init acpi_video_init(void)
  1827. {
  1828. dmi_check_system(video_dmi_table);
  1829. if (intel_opregion_present())
  1830. return 0;
  1831. return acpi_video_register();
  1832. }
  1833. static void __exit acpi_video_exit(void)
  1834. {
  1835. acpi_video_unregister();
  1836. return;
  1837. }
  1838. module_init(acpi_video_init);
  1839. module_exit(acpi_video_exit);