video.c 49 KB

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