acpi_video.c 59 KB

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