video.c 56 KB

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