acpi_video.c 58 KB

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