video.c 48 KB

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