video.c 57 KB

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