video.c 47 KB

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