video_detect.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. /*
  2. * Copyright (C) 2015 Red Hat Inc.
  3. * Hans de Goede <hdegoede@redhat.com>
  4. * Copyright (C) 2008 SuSE Linux Products GmbH
  5. * Thomas Renninger <trenn@suse.de>
  6. *
  7. * May be copied or modified under the terms of the GNU General Public License
  8. *
  9. * video_detect.c:
  10. * After PCI devices are glued with ACPI devices
  11. * acpi_get_pci_dev() can be called to identify ACPI graphics
  12. * devices for which a real graphics card is plugged in
  13. *
  14. * Depending on whether ACPI graphics extensions (cmp. ACPI spec Appendix B)
  15. * are available, video.ko should be used to handle the device.
  16. *
  17. * Otherwise vendor specific drivers like thinkpad_acpi, asus-laptop,
  18. * sony_acpi,... can take care about backlight brightness.
  19. *
  20. * Backlight drivers can use acpi_video_get_backlight_type() to determine
  21. * which driver should handle the backlight.
  22. *
  23. * If CONFIG_ACPI_VIDEO is neither set as "compiled in" (y) nor as a module (m)
  24. * this file will not be compiled and acpi_video_get_backlight_type() will
  25. * always return acpi_backlight_vendor.
  26. */
  27. #include <linux/export.h>
  28. #include <linux/acpi.h>
  29. #include <linux/backlight.h>
  30. #include <linux/dmi.h>
  31. #include <linux/module.h>
  32. #include <linux/pci.h>
  33. #include <linux/types.h>
  34. #include <acpi/video.h>
  35. ACPI_MODULE_NAME("video");
  36. #define _COMPONENT ACPI_VIDEO_COMPONENT
  37. void acpi_video_unregister_backlight(void);
  38. static bool backlight_notifier_registered;
  39. static struct notifier_block backlight_nb;
  40. static enum acpi_backlight_type acpi_backlight_cmdline = acpi_backlight_undef;
  41. static enum acpi_backlight_type acpi_backlight_dmi = acpi_backlight_undef;
  42. static void acpi_video_parse_cmdline(void)
  43. {
  44. if (!strcmp("vendor", acpi_video_backlight_string))
  45. acpi_backlight_cmdline = acpi_backlight_vendor;
  46. if (!strcmp("video", acpi_video_backlight_string))
  47. acpi_backlight_cmdline = acpi_backlight_video;
  48. if (!strcmp("native", acpi_video_backlight_string))
  49. acpi_backlight_cmdline = acpi_backlight_native;
  50. if (!strcmp("none", acpi_video_backlight_string))
  51. acpi_backlight_cmdline = acpi_backlight_none;
  52. }
  53. static acpi_status
  54. find_video(acpi_handle handle, u32 lvl, void *context, void **rv)
  55. {
  56. long *cap = context;
  57. struct pci_dev *dev;
  58. struct acpi_device *acpi_dev;
  59. static const struct acpi_device_id video_ids[] = {
  60. {ACPI_VIDEO_HID, 0},
  61. {"", 0},
  62. };
  63. if (acpi_bus_get_device(handle, &acpi_dev))
  64. return AE_OK;
  65. if (!acpi_match_device_ids(acpi_dev, video_ids)) {
  66. dev = acpi_get_pci_dev(handle);
  67. if (!dev)
  68. return AE_OK;
  69. pci_dev_put(dev);
  70. *cap |= acpi_is_video_device(handle);
  71. }
  72. return AE_OK;
  73. }
  74. /* Force to use vendor driver when the ACPI device is known to be
  75. * buggy */
  76. static int video_detect_force_vendor(const struct dmi_system_id *d)
  77. {
  78. acpi_backlight_dmi = acpi_backlight_vendor;
  79. return 0;
  80. }
  81. static int video_detect_force_video(const struct dmi_system_id *d)
  82. {
  83. acpi_backlight_dmi = acpi_backlight_video;
  84. return 0;
  85. }
  86. static int video_detect_force_native(const struct dmi_system_id *d)
  87. {
  88. acpi_backlight_dmi = acpi_backlight_native;
  89. return 0;
  90. }
  91. static const struct dmi_system_id video_detect_dmi_table[] = {
  92. /* On Samsung X360, the BIOS will set a flag (VDRV) if generic
  93. * ACPI backlight device is used. This flag will definitively break
  94. * the backlight interface (even the vendor interface) untill next
  95. * reboot. It's why we should prevent video.ko from being used here
  96. * and we can't rely on a later call to acpi_video_unregister().
  97. */
  98. {
  99. .callback = video_detect_force_vendor,
  100. .ident = "X360",
  101. .matches = {
  102. DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG ELECTRONICS CO., LTD."),
  103. DMI_MATCH(DMI_PRODUCT_NAME, "X360"),
  104. DMI_MATCH(DMI_BOARD_NAME, "X360"),
  105. },
  106. },
  107. {
  108. .callback = video_detect_force_vendor,
  109. .ident = "Asus UL30VT",
  110. .matches = {
  111. DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK Computer Inc."),
  112. DMI_MATCH(DMI_PRODUCT_NAME, "UL30VT"),
  113. },
  114. },
  115. {
  116. .callback = video_detect_force_vendor,
  117. .ident = "Asus UL30A",
  118. .matches = {
  119. DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK Computer Inc."),
  120. DMI_MATCH(DMI_PRODUCT_NAME, "UL30A"),
  121. },
  122. },
  123. {
  124. .callback = video_detect_force_vendor,
  125. .ident = "Dell Inspiron 5737",
  126. .matches = {
  127. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  128. DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 5737"),
  129. },
  130. },
  131. /*
  132. * These models have a working acpi_video backlight control, and using
  133. * native backlight causes a regression where backlight does not work
  134. * when userspace is not handling brightness key events. Disable
  135. * native_backlight on these to fix this:
  136. * https://bugzilla.kernel.org/show_bug.cgi?id=81691
  137. */
  138. {
  139. .callback = video_detect_force_video,
  140. .ident = "ThinkPad T420",
  141. .matches = {
  142. DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
  143. DMI_MATCH(DMI_PRODUCT_VERSION, "ThinkPad T420"),
  144. },
  145. },
  146. {
  147. .callback = video_detect_force_video,
  148. .ident = "ThinkPad T520",
  149. .matches = {
  150. DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
  151. DMI_MATCH(DMI_PRODUCT_VERSION, "ThinkPad T520"),
  152. },
  153. },
  154. {
  155. .callback = video_detect_force_video,
  156. .ident = "ThinkPad X201s",
  157. .matches = {
  158. DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
  159. DMI_MATCH(DMI_PRODUCT_VERSION, "ThinkPad X201s"),
  160. },
  161. },
  162. /* The native backlight controls do not work on some older machines */
  163. {
  164. /* https://bugs.freedesktop.org/show_bug.cgi?id=81515 */
  165. .callback = video_detect_force_video,
  166. .ident = "HP ENVY 15 Notebook",
  167. .matches = {
  168. DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
  169. DMI_MATCH(DMI_PRODUCT_NAME, "HP ENVY 15 Notebook PC"),
  170. },
  171. },
  172. {
  173. .callback = video_detect_force_video,
  174. .ident = "SAMSUNG 870Z5E/880Z5E/680Z5E",
  175. .matches = {
  176. DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG ELECTRONICS CO., LTD."),
  177. DMI_MATCH(DMI_PRODUCT_NAME, "870Z5E/880Z5E/680Z5E"),
  178. },
  179. },
  180. {
  181. .callback = video_detect_force_video,
  182. .ident = "SAMSUNG 370R4E/370R4V/370R5E/3570RE/370R5V",
  183. .matches = {
  184. DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG ELECTRONICS CO., LTD."),
  185. DMI_MATCH(DMI_PRODUCT_NAME,
  186. "370R4E/370R4V/370R5E/3570RE/370R5V"),
  187. },
  188. },
  189. {
  190. /* https://bugzilla.redhat.com/show_bug.cgi?id=1186097 */
  191. .callback = video_detect_force_video,
  192. .ident = "SAMSUNG 3570R/370R/470R/450R/510R/4450RV",
  193. .matches = {
  194. DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG ELECTRONICS CO., LTD."),
  195. DMI_MATCH(DMI_PRODUCT_NAME,
  196. "3570R/370R/470R/450R/510R/4450RV"),
  197. },
  198. },
  199. {
  200. /* https://bugzilla.redhat.com/show_bug.cgi?id=1094948 */
  201. .callback = video_detect_force_video,
  202. .ident = "SAMSUNG 730U3E/740U3E",
  203. .matches = {
  204. DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG ELECTRONICS CO., LTD."),
  205. DMI_MATCH(DMI_PRODUCT_NAME, "730U3E/740U3E"),
  206. },
  207. },
  208. {
  209. /* https://bugs.freedesktop.org/show_bug.cgi?id=87286 */
  210. .callback = video_detect_force_video,
  211. .ident = "SAMSUNG 900X3C/900X3D/900X3E/900X4C/900X4D",
  212. .matches = {
  213. DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG ELECTRONICS CO., LTD."),
  214. DMI_MATCH(DMI_PRODUCT_NAME,
  215. "900X3C/900X3D/900X3E/900X4C/900X4D"),
  216. },
  217. },
  218. {
  219. /* https://bugzilla.redhat.com/show_bug.cgi?id=1163574 */
  220. .callback = video_detect_force_video,
  221. .ident = "Dell XPS15 L521X",
  222. .matches = {
  223. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  224. DMI_MATCH(DMI_PRODUCT_NAME, "XPS L521X"),
  225. },
  226. },
  227. /* Non win8 machines which need native backlight nevertheless */
  228. {
  229. /* https://bugzilla.redhat.com/show_bug.cgi?id=1187004 */
  230. .callback = video_detect_force_native,
  231. .ident = "Lenovo Ideapad Z570",
  232. .matches = {
  233. DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
  234. DMI_MATCH(DMI_PRODUCT_NAME, "102434U"),
  235. },
  236. },
  237. {
  238. /* https://bugzilla.redhat.com/show_bug.cgi?id=1217249 */
  239. .callback = video_detect_force_native,
  240. .ident = "Apple MacBook Pro 12,1",
  241. .matches = {
  242. DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."),
  243. DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro12,1"),
  244. },
  245. },
  246. { },
  247. };
  248. static int acpi_video_backlight_notify(struct notifier_block *nb,
  249. unsigned long val, void *bd)
  250. {
  251. struct backlight_device *backlight = bd;
  252. /* A raw bl registering may change video -> native */
  253. if (backlight->props.type == BACKLIGHT_RAW &&
  254. val == BACKLIGHT_REGISTERED &&
  255. acpi_video_get_backlight_type() != acpi_backlight_video)
  256. acpi_video_unregister_backlight();
  257. return NOTIFY_OK;
  258. }
  259. /*
  260. * Determine which type of backlight interface to use on this system,
  261. * First check cmdline, then dmi quirks, then do autodetect.
  262. *
  263. * The autodetect order is:
  264. * 1) Is the acpi-video backlight interface supported ->
  265. * no, use a vendor interface
  266. * 2) Is this a win8 "ready" BIOS and do we have a native interface ->
  267. * yes, use a native interface
  268. * 3) Else use the acpi-video interface
  269. *
  270. * Arguably the native on win8 check should be done first, but that would
  271. * be a behavior change, which may causes issues.
  272. */
  273. enum acpi_backlight_type acpi_video_get_backlight_type(void)
  274. {
  275. static DEFINE_MUTEX(init_mutex);
  276. static bool init_done;
  277. static long video_caps;
  278. /* Parse cmdline, dmi and acpi only once */
  279. mutex_lock(&init_mutex);
  280. if (!init_done) {
  281. acpi_video_parse_cmdline();
  282. dmi_check_system(video_detect_dmi_table);
  283. acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
  284. ACPI_UINT32_MAX, find_video, NULL,
  285. &video_caps, NULL);
  286. backlight_nb.notifier_call = acpi_video_backlight_notify;
  287. backlight_nb.priority = 0;
  288. if (backlight_register_notifier(&backlight_nb) == 0)
  289. backlight_notifier_registered = true;
  290. init_done = true;
  291. }
  292. mutex_unlock(&init_mutex);
  293. if (acpi_backlight_cmdline != acpi_backlight_undef)
  294. return acpi_backlight_cmdline;
  295. if (acpi_backlight_dmi != acpi_backlight_undef)
  296. return acpi_backlight_dmi;
  297. if (!(video_caps & ACPI_VIDEO_BACKLIGHT))
  298. return acpi_backlight_vendor;
  299. if (acpi_osi_is_win8() && backlight_device_registered(BACKLIGHT_RAW))
  300. return acpi_backlight_native;
  301. return acpi_backlight_video;
  302. }
  303. EXPORT_SYMBOL(acpi_video_get_backlight_type);
  304. /*
  305. * Set the preferred backlight interface type based on DMI info.
  306. * This function allows DMI blacklists to be implemented by external
  307. * platform drivers instead of putting a big blacklist in video_detect.c
  308. */
  309. void acpi_video_set_dmi_backlight_type(enum acpi_backlight_type type)
  310. {
  311. acpi_backlight_dmi = type;
  312. /* Remove acpi-video backlight interface if it is no longer desired */
  313. if (acpi_video_get_backlight_type() != acpi_backlight_video)
  314. acpi_video_unregister_backlight();
  315. }
  316. EXPORT_SYMBOL(acpi_video_set_dmi_backlight_type);
  317. void __exit acpi_video_detect_exit(void)
  318. {
  319. if (backlight_notifier_registered)
  320. backlight_unregister_notifier(&backlight_nb);
  321. }