video.c 57 KB

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