video.c 46 KB

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