acpi_video.c 51 KB

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