acpi_video.c 55 KB

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