video.c 53 KB

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