acpi_video.c 57 KB

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