video.c 54 KB

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