video_detect.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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 <linux/workqueue.h>
  35. #include <acpi/video.h>
  36. ACPI_MODULE_NAME("video");
  37. #define _COMPONENT ACPI_VIDEO_COMPONENT
  38. void acpi_video_unregister_backlight(void);
  39. static bool backlight_notifier_registered;
  40. static struct notifier_block backlight_nb;
  41. static struct work_struct backlight_notify_work;
  42. static enum acpi_backlight_type acpi_backlight_cmdline = acpi_backlight_undef;
  43. static enum acpi_backlight_type acpi_backlight_dmi = acpi_backlight_undef;
  44. static void acpi_video_parse_cmdline(void)
  45. {
  46. if (!strcmp("vendor", acpi_video_backlight_string))
  47. acpi_backlight_cmdline = acpi_backlight_vendor;
  48. if (!strcmp("video", acpi_video_backlight_string))
  49. acpi_backlight_cmdline = acpi_backlight_video;
  50. if (!strcmp("native", acpi_video_backlight_string))
  51. acpi_backlight_cmdline = acpi_backlight_native;
  52. if (!strcmp("none", acpi_video_backlight_string))
  53. acpi_backlight_cmdline = acpi_backlight_none;
  54. }
  55. static acpi_status
  56. find_video(acpi_handle handle, u32 lvl, void *context, void **rv)
  57. {
  58. long *cap = context;
  59. struct pci_dev *dev;
  60. struct acpi_device *acpi_dev;
  61. static const struct acpi_device_id video_ids[] = {
  62. {ACPI_VIDEO_HID, 0},
  63. {"", 0},
  64. };
  65. if (acpi_bus_get_device(handle, &acpi_dev))
  66. return AE_OK;
  67. if (!acpi_match_device_ids(acpi_dev, video_ids)) {
  68. dev = acpi_get_pci_dev(handle);
  69. if (!dev)
  70. return AE_OK;
  71. pci_dev_put(dev);
  72. *cap |= acpi_is_video_device(handle);
  73. }
  74. return AE_OK;
  75. }
  76. /* Force to use vendor driver when the ACPI device is known to be
  77. * buggy */
  78. static int video_detect_force_vendor(const struct dmi_system_id *d)
  79. {
  80. acpi_backlight_dmi = acpi_backlight_vendor;
  81. return 0;
  82. }
  83. static int video_detect_force_video(const struct dmi_system_id *d)
  84. {
  85. acpi_backlight_dmi = acpi_backlight_video;
  86. return 0;
  87. }
  88. static int video_detect_force_native(const struct dmi_system_id *d)
  89. {
  90. acpi_backlight_dmi = acpi_backlight_native;
  91. return 0;
  92. }
  93. static const struct dmi_system_id video_detect_dmi_table[] = {
  94. /* On Samsung X360, the BIOS will set a flag (VDRV) if generic
  95. * ACPI backlight device is used. This flag will definitively break
  96. * the backlight interface (even the vendor interface) untill next
  97. * reboot. It's why we should prevent video.ko from being used here
  98. * and we can't rely on a later call to acpi_video_unregister().
  99. */
  100. {
  101. .callback = video_detect_force_vendor,
  102. .ident = "X360",
  103. .matches = {
  104. DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG ELECTRONICS CO., LTD."),
  105. DMI_MATCH(DMI_PRODUCT_NAME, "X360"),
  106. DMI_MATCH(DMI_BOARD_NAME, "X360"),
  107. },
  108. },
  109. {
  110. .callback = video_detect_force_vendor,
  111. .ident = "Asus UL30VT",
  112. .matches = {
  113. DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK Computer Inc."),
  114. DMI_MATCH(DMI_PRODUCT_NAME, "UL30VT"),
  115. },
  116. },
  117. {
  118. .callback = video_detect_force_vendor,
  119. .ident = "Asus UL30A",
  120. .matches = {
  121. DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK Computer Inc."),
  122. DMI_MATCH(DMI_PRODUCT_NAME, "UL30A"),
  123. },
  124. },
  125. /*
  126. * These models have a working acpi_video backlight control, and using
  127. * native backlight causes a regression where backlight does not work
  128. * when userspace is not handling brightness key events. Disable
  129. * native_backlight on these to fix this:
  130. * https://bugzilla.kernel.org/show_bug.cgi?id=81691
  131. */
  132. {
  133. .callback = video_detect_force_video,
  134. .ident = "ThinkPad T420",
  135. .matches = {
  136. DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
  137. DMI_MATCH(DMI_PRODUCT_VERSION, "ThinkPad T420"),
  138. },
  139. },
  140. {
  141. .callback = video_detect_force_video,
  142. .ident = "ThinkPad T520",
  143. .matches = {
  144. DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
  145. DMI_MATCH(DMI_PRODUCT_VERSION, "ThinkPad T520"),
  146. },
  147. },
  148. {
  149. .callback = video_detect_force_video,
  150. .ident = "ThinkPad X201s",
  151. .matches = {
  152. DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
  153. DMI_MATCH(DMI_PRODUCT_VERSION, "ThinkPad X201s"),
  154. },
  155. },
  156. {
  157. .callback = video_detect_force_video,
  158. .ident = "ThinkPad X201T",
  159. .matches = {
  160. DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
  161. DMI_MATCH(DMI_PRODUCT_VERSION, "ThinkPad X201T"),
  162. },
  163. },
  164. /* The native backlight controls do not work on some older machines */
  165. {
  166. /* https://bugs.freedesktop.org/show_bug.cgi?id=81515 */
  167. .callback = video_detect_force_video,
  168. .ident = "HP ENVY 15 Notebook",
  169. .matches = {
  170. DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
  171. DMI_MATCH(DMI_PRODUCT_NAME, "HP ENVY 15 Notebook PC"),
  172. },
  173. },
  174. {
  175. .callback = video_detect_force_video,
  176. .ident = "SAMSUNG 870Z5E/880Z5E/680Z5E",
  177. .matches = {
  178. DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG ELECTRONICS CO., LTD."),
  179. DMI_MATCH(DMI_PRODUCT_NAME, "870Z5E/880Z5E/680Z5E"),
  180. },
  181. },
  182. {
  183. .callback = video_detect_force_video,
  184. .ident = "SAMSUNG 370R4E/370R4V/370R5E/3570RE/370R5V",
  185. .matches = {
  186. DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG ELECTRONICS CO., LTD."),
  187. DMI_MATCH(DMI_PRODUCT_NAME,
  188. "370R4E/370R4V/370R5E/3570RE/370R5V"),
  189. },
  190. },
  191. {
  192. /* https://bugzilla.redhat.com/show_bug.cgi?id=1186097 */
  193. .callback = video_detect_force_video,
  194. .ident = "SAMSUNG 3570R/370R/470R/450R/510R/4450RV",
  195. .matches = {
  196. DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG ELECTRONICS CO., LTD."),
  197. DMI_MATCH(DMI_PRODUCT_NAME,
  198. "3570R/370R/470R/450R/510R/4450RV"),
  199. },
  200. },
  201. {
  202. /* https://bugzilla.redhat.com/show_bug.cgi?id=1557060 */
  203. .callback = video_detect_force_video,
  204. .ident = "SAMSUNG 670Z5E",
  205. .matches = {
  206. DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG ELECTRONICS CO., LTD."),
  207. DMI_MATCH(DMI_PRODUCT_NAME, "670Z5E"),
  208. },
  209. },
  210. {
  211. /* https://bugzilla.redhat.com/show_bug.cgi?id=1094948 */
  212. .callback = video_detect_force_video,
  213. .ident = "SAMSUNG 730U3E/740U3E",
  214. .matches = {
  215. DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG ELECTRONICS CO., LTD."),
  216. DMI_MATCH(DMI_PRODUCT_NAME, "730U3E/740U3E"),
  217. },
  218. },
  219. {
  220. /* https://bugs.freedesktop.org/show_bug.cgi?id=87286 */
  221. .callback = video_detect_force_video,
  222. .ident = "SAMSUNG 900X3C/900X3D/900X3E/900X4C/900X4D",
  223. .matches = {
  224. DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG ELECTRONICS CO., LTD."),
  225. DMI_MATCH(DMI_PRODUCT_NAME,
  226. "900X3C/900X3D/900X3E/900X4C/900X4D"),
  227. },
  228. },
  229. {
  230. /* https://bugzilla.redhat.com/show_bug.cgi?id=1272633 */
  231. .callback = video_detect_force_video,
  232. .ident = "Dell XPS14 L421X",
  233. .matches = {
  234. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  235. DMI_MATCH(DMI_PRODUCT_NAME, "XPS L421X"),
  236. },
  237. },
  238. {
  239. /* https://bugzilla.redhat.com/show_bug.cgi?id=1163574 */
  240. .callback = video_detect_force_video,
  241. .ident = "Dell XPS15 L521X",
  242. .matches = {
  243. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  244. DMI_MATCH(DMI_PRODUCT_NAME, "XPS L521X"),
  245. },
  246. },
  247. {
  248. /* https://bugzilla.kernel.org/show_bug.cgi?id=108971 */
  249. .callback = video_detect_force_video,
  250. .ident = "SAMSUNG 530U4E/540U4E",
  251. .matches = {
  252. DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG ELECTRONICS CO., LTD."),
  253. DMI_MATCH(DMI_PRODUCT_NAME, "530U4E/540U4E"),
  254. },
  255. },
  256. /* Non win8 machines which need native backlight nevertheless */
  257. {
  258. /* https://bugzilla.redhat.com/show_bug.cgi?id=1201530 */
  259. .callback = video_detect_force_native,
  260. .ident = "Lenovo Ideapad S405",
  261. .matches = {
  262. DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
  263. DMI_MATCH(DMI_BOARD_NAME, "Lenovo IdeaPad S405"),
  264. },
  265. },
  266. {
  267. /* https://bugzilla.redhat.com/show_bug.cgi?id=1187004 */
  268. .callback = video_detect_force_native,
  269. .ident = "Lenovo Ideapad Z570",
  270. .matches = {
  271. DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
  272. DMI_MATCH(DMI_PRODUCT_NAME, "102434U"),
  273. },
  274. },
  275. {
  276. /* https://bugzilla.redhat.com/show_bug.cgi?id=1217249 */
  277. .callback = video_detect_force_native,
  278. .ident = "Apple MacBook Pro 12,1",
  279. .matches = {
  280. DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."),
  281. DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro12,1"),
  282. },
  283. },
  284. {
  285. .callback = video_detect_force_native,
  286. .ident = "Dell Vostro V131",
  287. .matches = {
  288. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  289. DMI_MATCH(DMI_PRODUCT_NAME, "Vostro V131"),
  290. },
  291. },
  292. {
  293. /* https://bugzilla.redhat.com/show_bug.cgi?id=1123661 */
  294. .callback = video_detect_force_native,
  295. .ident = "Dell XPS 17 L702X",
  296. .matches = {
  297. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  298. DMI_MATCH(DMI_PRODUCT_NAME, "Dell System XPS L702X"),
  299. },
  300. },
  301. {
  302. /* https://bugzilla.redhat.com/show_bug.cgi?id=1204476 */
  303. /* https://bugs.launchpad.net/ubuntu/+source/linux-lts-trusty/+bug/1416940 */
  304. .callback = video_detect_force_native,
  305. .ident = "HP Pavilion dv6",
  306. .matches = {
  307. DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
  308. DMI_MATCH(DMI_PRODUCT_NAME, "HP Pavilion dv6 Notebook PC"),
  309. },
  310. },
  311. { },
  312. };
  313. /* This uses a workqueue to avoid various locking ordering issues */
  314. static void acpi_video_backlight_notify_work(struct work_struct *work)
  315. {
  316. if (acpi_video_get_backlight_type() != acpi_backlight_video)
  317. acpi_video_unregister_backlight();
  318. }
  319. static int acpi_video_backlight_notify(struct notifier_block *nb,
  320. unsigned long val, void *bd)
  321. {
  322. struct backlight_device *backlight = bd;
  323. /* A raw bl registering may change video -> native */
  324. if (backlight->props.type == BACKLIGHT_RAW &&
  325. val == BACKLIGHT_REGISTERED)
  326. schedule_work(&backlight_notify_work);
  327. return NOTIFY_OK;
  328. }
  329. /*
  330. * Determine which type of backlight interface to use on this system,
  331. * First check cmdline, then dmi quirks, then do autodetect.
  332. *
  333. * The autodetect order is:
  334. * 1) Is the acpi-video backlight interface supported ->
  335. * no, use a vendor interface
  336. * 2) Is this a win8 "ready" BIOS and do we have a native interface ->
  337. * yes, use a native interface
  338. * 3) Else use the acpi-video interface
  339. *
  340. * Arguably the native on win8 check should be done first, but that would
  341. * be a behavior change, which may causes issues.
  342. */
  343. enum acpi_backlight_type acpi_video_get_backlight_type(void)
  344. {
  345. static DEFINE_MUTEX(init_mutex);
  346. static bool init_done;
  347. static long video_caps;
  348. /* Parse cmdline, dmi and acpi only once */
  349. mutex_lock(&init_mutex);
  350. if (!init_done) {
  351. acpi_video_parse_cmdline();
  352. dmi_check_system(video_detect_dmi_table);
  353. acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
  354. ACPI_UINT32_MAX, find_video, NULL,
  355. &video_caps, NULL);
  356. INIT_WORK(&backlight_notify_work,
  357. acpi_video_backlight_notify_work);
  358. backlight_nb.notifier_call = acpi_video_backlight_notify;
  359. backlight_nb.priority = 0;
  360. if (backlight_register_notifier(&backlight_nb) == 0)
  361. backlight_notifier_registered = true;
  362. init_done = true;
  363. }
  364. mutex_unlock(&init_mutex);
  365. if (acpi_backlight_cmdline != acpi_backlight_undef)
  366. return acpi_backlight_cmdline;
  367. if (acpi_backlight_dmi != acpi_backlight_undef)
  368. return acpi_backlight_dmi;
  369. if (!(video_caps & ACPI_VIDEO_BACKLIGHT))
  370. return acpi_backlight_vendor;
  371. if (acpi_osi_is_win8() && backlight_device_get_by_type(BACKLIGHT_RAW))
  372. return acpi_backlight_native;
  373. return acpi_backlight_video;
  374. }
  375. EXPORT_SYMBOL(acpi_video_get_backlight_type);
  376. /*
  377. * Set the preferred backlight interface type based on DMI info.
  378. * This function allows DMI blacklists to be implemented by external
  379. * platform drivers instead of putting a big blacklist in video_detect.c
  380. */
  381. void acpi_video_set_dmi_backlight_type(enum acpi_backlight_type type)
  382. {
  383. acpi_backlight_dmi = type;
  384. /* Remove acpi-video backlight interface if it is no longer desired */
  385. if (acpi_video_get_backlight_type() != acpi_backlight_video)
  386. acpi_video_unregister_backlight();
  387. }
  388. EXPORT_SYMBOL(acpi_video_set_dmi_backlight_type);
  389. void __exit acpi_video_detect_exit(void)
  390. {
  391. if (backlight_notifier_registered)
  392. backlight_unregister_notifier(&backlight_nb);
  393. }