video.c 47 KB

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