video.c 50 KB

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