video.c 58 KB

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