video.c 48 KB

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