acpi_video.c 60 KB

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