video.c 52 KB

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