video.c 48 KB

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