acpi_video.c 58 KB

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