video.c 46 KB

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