video.c 46 KB

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