video.c 48 KB

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