video.c 46 KB

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