video.c 52 KB

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