video.c 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821
  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 int 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 int 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 int 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, int type);
  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, int init);
  201. static int acpi_video_get_next_level(struct acpi_video_device *device,
  202. u32 level_current, u32 event);
  203. static int acpi_video_switch_brightness(struct acpi_video_device *device,
  204. int event);
  205. /*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, 0))
  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, 0))
  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 struct dmi_system_id video_dmi_table[] __initdata = {
  337. /*
  338. * Broken _BQC workaround http://bugzilla.kernel.org/show_bug.cgi?id=13121
  339. */
  340. {
  341. .callback = video_set_bqc_offset,
  342. .ident = "Acer Aspire 5720",
  343. .matches = {
  344. DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
  345. DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5720"),
  346. },
  347. },
  348. {
  349. .callback = video_set_bqc_offset,
  350. .ident = "Acer Aspire 5710Z",
  351. .matches = {
  352. DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
  353. DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5710Z"),
  354. },
  355. },
  356. {
  357. .callback = video_set_bqc_offset,
  358. .ident = "eMachines E510",
  359. .matches = {
  360. DMI_MATCH(DMI_BOARD_VENDOR, "EMACHINES"),
  361. DMI_MATCH(DMI_PRODUCT_NAME, "eMachines E510"),
  362. },
  363. },
  364. {
  365. .callback = video_set_bqc_offset,
  366. .ident = "Acer Aspire 5315",
  367. .matches = {
  368. DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
  369. DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5315"),
  370. },
  371. },
  372. {
  373. .callback = video_set_bqc_offset,
  374. .ident = "Acer Aspire 7720",
  375. .matches = {
  376. DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
  377. DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 7720"),
  378. },
  379. },
  380. {}
  381. };
  382. static int
  383. acpi_video_device_lcd_get_level_current(struct acpi_video_device *device,
  384. unsigned long long *level, int init)
  385. {
  386. acpi_status status = AE_OK;
  387. int i;
  388. if (device->cap._BQC || device->cap._BCQ) {
  389. char *buf = device->cap._BQC ? "_BQC" : "_BCQ";
  390. status = acpi_evaluate_integer(device->dev->handle, buf,
  391. NULL, level);
  392. if (ACPI_SUCCESS(status)) {
  393. if (device->brightness->flags._BQC_use_index) {
  394. if (device->brightness->flags._BCL_reversed)
  395. *level = device->brightness->count
  396. - 3 - (*level);
  397. *level = device->brightness->levels[*level + 2];
  398. }
  399. *level += bqc_offset_aml_bug_workaround;
  400. for (i = 2; i < device->brightness->count; i++)
  401. if (device->brightness->levels[i] == *level) {
  402. device->brightness->curr = *level;
  403. return 0;
  404. }
  405. if (!init) {
  406. /*
  407. * BQC returned an invalid level.
  408. * Stop using it.
  409. */
  410. ACPI_WARNING((AE_INFO,
  411. "%s returned an invalid level",
  412. buf));
  413. device->cap._BQC = device->cap._BCQ = 0;
  414. }
  415. } else {
  416. /* Fixme:
  417. * should we return an error or ignore this failure?
  418. * dev->brightness->curr is a cached value which stores
  419. * the correct current backlight level in most cases.
  420. * ACPI video backlight still works w/ buggy _BQC.
  421. * http://bugzilla.kernel.org/show_bug.cgi?id=12233
  422. */
  423. ACPI_WARNING((AE_INFO, "Evaluating %s failed", buf));
  424. device->cap._BQC = device->cap._BCQ = 0;
  425. }
  426. }
  427. *level = device->brightness->curr;
  428. return 0;
  429. }
  430. static int
  431. acpi_video_device_EDID(struct acpi_video_device *device,
  432. union acpi_object **edid, ssize_t length)
  433. {
  434. int status;
  435. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  436. union acpi_object *obj;
  437. union acpi_object arg0 = { ACPI_TYPE_INTEGER };
  438. struct acpi_object_list args = { 1, &arg0 };
  439. *edid = NULL;
  440. if (!device)
  441. return -ENODEV;
  442. if (length == 128)
  443. arg0.integer.value = 1;
  444. else if (length == 256)
  445. arg0.integer.value = 2;
  446. else
  447. return -EINVAL;
  448. status = acpi_evaluate_object(device->dev->handle, "_DDC", &args, &buffer);
  449. if (ACPI_FAILURE(status))
  450. return -ENODEV;
  451. obj = buffer.pointer;
  452. if (obj && obj->type == ACPI_TYPE_BUFFER)
  453. *edid = obj;
  454. else {
  455. printk(KERN_ERR PREFIX "Invalid _DDC data\n");
  456. status = -EFAULT;
  457. kfree(obj);
  458. }
  459. return status;
  460. }
  461. /* bus */
  462. /*
  463. * Arg:
  464. * video : video bus device pointer
  465. * bios_flag :
  466. * 0. The system BIOS should NOT automatically switch(toggle)
  467. * the active display output.
  468. * 1. The system BIOS should automatically switch (toggle) the
  469. * active display output. No switch event.
  470. * 2. The _DGS value should be locked.
  471. * 3. The system BIOS should not automatically switch (toggle) the
  472. * active display output, but instead generate the display switch
  473. * event notify code.
  474. * lcd_flag :
  475. * 0. The system BIOS should automatically control the brightness level
  476. * of the LCD when the power changes from AC to DC
  477. * 1. The system BIOS should NOT automatically control the brightness
  478. * level of the LCD when the power changes from AC to DC.
  479. * Return Value:
  480. * -1 wrong arg.
  481. */
  482. static int
  483. acpi_video_bus_DOS(struct acpi_video_bus *video, int bios_flag, int lcd_flag)
  484. {
  485. u64 status = 0;
  486. union acpi_object arg0 = { ACPI_TYPE_INTEGER };
  487. struct acpi_object_list args = { 1, &arg0 };
  488. if (bios_flag < 0 || bios_flag > 3 || lcd_flag < 0 || lcd_flag > 1) {
  489. status = -1;
  490. goto Failed;
  491. }
  492. arg0.integer.value = (lcd_flag << 2) | bios_flag;
  493. video->dos_setting = arg0.integer.value;
  494. acpi_evaluate_object(video->device->handle, "_DOS", &args, NULL);
  495. Failed:
  496. return status;
  497. }
  498. /*
  499. * Simple comparison function used to sort backlight levels.
  500. */
  501. static int
  502. acpi_video_cmp_level(const void *a, const void *b)
  503. {
  504. return *(int *)a - *(int *)b;
  505. }
  506. /*
  507. * Arg:
  508. * device : video output device (LCD, CRT, ..)
  509. *
  510. * Return Value:
  511. * Maximum brightness level
  512. *
  513. * Allocate and initialize device->brightness.
  514. */
  515. static int
  516. acpi_video_init_brightness(struct acpi_video_device *device)
  517. {
  518. union acpi_object *obj = NULL;
  519. int i, max_level = 0, count = 0, level_ac_battery = 0;
  520. unsigned long long level, level_old;
  521. union acpi_object *o;
  522. struct acpi_video_device_brightness *br = NULL;
  523. int result = -EINVAL;
  524. if (!ACPI_SUCCESS(acpi_video_device_lcd_query_levels(device, &obj))) {
  525. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Could not query available "
  526. "LCD brightness level\n"));
  527. goto out;
  528. }
  529. if (obj->package.count < 2)
  530. goto out;
  531. br = kzalloc(sizeof(*br), GFP_KERNEL);
  532. if (!br) {
  533. printk(KERN_ERR "can't allocate memory\n");
  534. result = -ENOMEM;
  535. goto out;
  536. }
  537. br->levels = kmalloc((obj->package.count + 2) * sizeof *(br->levels),
  538. GFP_KERNEL);
  539. if (!br->levels) {
  540. result = -ENOMEM;
  541. goto out_free;
  542. }
  543. for (i = 0; i < obj->package.count; i++) {
  544. o = (union acpi_object *)&obj->package.elements[i];
  545. if (o->type != ACPI_TYPE_INTEGER) {
  546. printk(KERN_ERR PREFIX "Invalid data\n");
  547. continue;
  548. }
  549. br->levels[count] = (u32) o->integer.value;
  550. if (br->levels[count] > max_level)
  551. max_level = br->levels[count];
  552. count++;
  553. }
  554. /*
  555. * some buggy BIOS don't export the levels
  556. * when machine is on AC/Battery in _BCL package.
  557. * In this case, the first two elements in _BCL packages
  558. * are also supported brightness levels that OS should take care of.
  559. */
  560. for (i = 2; i < count; i++) {
  561. if (br->levels[i] == br->levels[0])
  562. level_ac_battery++;
  563. if (br->levels[i] == br->levels[1])
  564. level_ac_battery++;
  565. }
  566. if (level_ac_battery < 2) {
  567. level_ac_battery = 2 - level_ac_battery;
  568. br->flags._BCL_no_ac_battery_levels = 1;
  569. for (i = (count - 1 + level_ac_battery); i >= 2; i--)
  570. br->levels[i] = br->levels[i - level_ac_battery];
  571. count += level_ac_battery;
  572. } else if (level_ac_battery > 2)
  573. ACPI_ERROR((AE_INFO, "Too many duplicates in _BCL package\n"));
  574. /* Check if the _BCL package is in a reversed order */
  575. if (max_level == br->levels[2]) {
  576. br->flags._BCL_reversed = 1;
  577. sort(&br->levels[2], count - 2, sizeof(br->levels[2]),
  578. acpi_video_cmp_level, NULL);
  579. } else if (max_level != br->levels[count - 1])
  580. ACPI_ERROR((AE_INFO,
  581. "Found unordered _BCL package\n"));
  582. br->count = count;
  583. device->brightness = br;
  584. /* Check the input/output of _BQC/_BCL/_BCM */
  585. if ((max_level < 100) && (max_level <= (count - 2)))
  586. br->flags._BCL_use_index = 1;
  587. /*
  588. * _BCM is always consistent with _BCL,
  589. * at least for all the laptops we have ever seen.
  590. */
  591. br->flags._BCM_use_index = br->flags._BCL_use_index;
  592. /* _BQC uses INDEX while _BCL uses VALUE in some laptops */
  593. br->curr = level = max_level;
  594. if (!device->cap._BQC)
  595. goto set_level;
  596. result = acpi_video_device_lcd_get_level_current(device, &level_old, 1);
  597. if (result)
  598. goto out_free_levels;
  599. /*
  600. * Set the level to maximum and check if _BQC uses indexed value
  601. */
  602. result = acpi_video_device_lcd_set_level(device, max_level);
  603. if (result)
  604. goto out_free_levels;
  605. result = acpi_video_device_lcd_get_level_current(device, &level, 0);
  606. if (result)
  607. goto out_free_levels;
  608. br->flags._BQC_use_index = (level == max_level ? 0 : 1);
  609. if (!br->flags._BQC_use_index) {
  610. /*
  611. * Set the backlight to the initial state.
  612. * On some buggy laptops, _BQC returns an uninitialized value
  613. * when invoked for the first time, i.e. level_old is invalid.
  614. * set the backlight to max_level in this case
  615. */
  616. if (use_bios_initial_backlight) {
  617. for (i = 2; i < br->count; i++)
  618. if (level_old == br->levels[i])
  619. level = level_old;
  620. }
  621. goto set_level;
  622. }
  623. if (br->flags._BCL_reversed)
  624. level_old = (br->count - 1) - level_old;
  625. level = br->levels[level_old];
  626. set_level:
  627. result = acpi_video_device_lcd_set_level(device, level);
  628. if (result)
  629. goto out_free_levels;
  630. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  631. "found %d brightness levels\n", count - 2));
  632. kfree(obj);
  633. return result;
  634. out_free_levels:
  635. kfree(br->levels);
  636. out_free:
  637. kfree(br);
  638. out:
  639. device->brightness = NULL;
  640. kfree(obj);
  641. return result;
  642. }
  643. /*
  644. * Arg:
  645. * device : video output device (LCD, CRT, ..)
  646. *
  647. * Return Value:
  648. * None
  649. *
  650. * Find out all required AML methods defined under the output
  651. * device.
  652. */
  653. static void acpi_video_device_find_cap(struct acpi_video_device *device)
  654. {
  655. acpi_handle h_dummy1;
  656. if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_ADR", &h_dummy1))) {
  657. device->cap._ADR = 1;
  658. }
  659. if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_BCL", &h_dummy1))) {
  660. device->cap._BCL = 1;
  661. }
  662. if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_BCM", &h_dummy1))) {
  663. device->cap._BCM = 1;
  664. }
  665. if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle,"_BQC",&h_dummy1)))
  666. device->cap._BQC = 1;
  667. else if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_BCQ",
  668. &h_dummy1))) {
  669. printk(KERN_WARNING FW_BUG "_BCQ is used instead of _BQC\n");
  670. device->cap._BCQ = 1;
  671. }
  672. if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DDC", &h_dummy1))) {
  673. device->cap._DDC = 1;
  674. }
  675. if (acpi_video_backlight_support()) {
  676. struct backlight_properties props;
  677. struct pci_dev *pdev;
  678. acpi_handle acpi_parent;
  679. struct device *parent = NULL;
  680. int result;
  681. static int count = 0;
  682. char *name;
  683. result = acpi_video_init_brightness(device);
  684. if (result)
  685. return;
  686. name = kasprintf(GFP_KERNEL, "acpi_video%d", count);
  687. if (!name)
  688. return;
  689. count++;
  690. acpi_get_parent(device->dev->handle, &acpi_parent);
  691. pdev = acpi_get_pci_dev(acpi_parent);
  692. if (pdev) {
  693. parent = &pdev->dev;
  694. pci_dev_put(pdev);
  695. }
  696. memset(&props, 0, sizeof(struct backlight_properties));
  697. props.type = BACKLIGHT_FIRMWARE;
  698. props.max_brightness = device->brightness->count - 3;
  699. device->backlight = backlight_device_register(name,
  700. parent,
  701. device,
  702. &acpi_backlight_ops,
  703. &props);
  704. kfree(name);
  705. if (IS_ERR(device->backlight))
  706. return;
  707. /*
  708. * Save current brightness level in case we have to restore it
  709. * before acpi_video_device_lcd_set_level() is called next time.
  710. */
  711. device->backlight->props.brightness =
  712. acpi_video_get_brightness(device->backlight);
  713. device->cooling_dev = thermal_cooling_device_register("LCD",
  714. device->dev, &video_cooling_ops);
  715. if (IS_ERR(device->cooling_dev)) {
  716. /*
  717. * Set cooling_dev to NULL so we don't crash trying to
  718. * free it.
  719. * Also, why the hell we are returning early and
  720. * not attempt to register video output if cooling
  721. * device registration failed?
  722. * -- dtor
  723. */
  724. device->cooling_dev = NULL;
  725. return;
  726. }
  727. dev_info(&device->dev->dev, "registered as cooling_device%d\n",
  728. device->cooling_dev->id);
  729. result = sysfs_create_link(&device->dev->dev.kobj,
  730. &device->cooling_dev->device.kobj,
  731. "thermal_cooling");
  732. if (result)
  733. printk(KERN_ERR PREFIX "Create sysfs link\n");
  734. result = sysfs_create_link(&device->cooling_dev->device.kobj,
  735. &device->dev->dev.kobj, "device");
  736. if (result)
  737. printk(KERN_ERR PREFIX "Create sysfs link\n");
  738. }
  739. }
  740. /*
  741. * Arg:
  742. * device : video output device (VGA)
  743. *
  744. * Return Value:
  745. * None
  746. *
  747. * Find out all required AML methods defined under the video bus device.
  748. */
  749. static void acpi_video_bus_find_cap(struct acpi_video_bus *video)
  750. {
  751. acpi_handle h_dummy1;
  752. if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_DOS", &h_dummy1))) {
  753. video->cap._DOS = 1;
  754. }
  755. if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_DOD", &h_dummy1))) {
  756. video->cap._DOD = 1;
  757. }
  758. if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_ROM", &h_dummy1))) {
  759. video->cap._ROM = 1;
  760. }
  761. if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_GPD", &h_dummy1))) {
  762. video->cap._GPD = 1;
  763. }
  764. if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_SPD", &h_dummy1))) {
  765. video->cap._SPD = 1;
  766. }
  767. if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_VPO", &h_dummy1))) {
  768. video->cap._VPO = 1;
  769. }
  770. }
  771. /*
  772. * Check whether the video bus device has required AML method to
  773. * support the desired features
  774. */
  775. static int acpi_video_bus_check(struct acpi_video_bus *video)
  776. {
  777. acpi_status status = -ENOENT;
  778. struct pci_dev *dev;
  779. if (!video)
  780. return -EINVAL;
  781. dev = acpi_get_pci_dev(video->device->handle);
  782. if (!dev)
  783. return -ENODEV;
  784. pci_dev_put(dev);
  785. /* Since there is no HID, CID and so on for VGA driver, we have
  786. * to check well known required nodes.
  787. */
  788. /* Does this device support video switching? */
  789. if (video->cap._DOS || video->cap._DOD) {
  790. if (!video->cap._DOS) {
  791. printk(KERN_WARNING FW_BUG
  792. "ACPI(%s) defines _DOD but not _DOS\n",
  793. acpi_device_bid(video->device));
  794. }
  795. video->flags.multihead = 1;
  796. status = 0;
  797. }
  798. /* Does this device support retrieving a video ROM? */
  799. if (video->cap._ROM) {
  800. video->flags.rom = 1;
  801. status = 0;
  802. }
  803. /* Does this device support configuring which video device to POST? */
  804. if (video->cap._GPD && video->cap._SPD && video->cap._VPO) {
  805. video->flags.post = 1;
  806. status = 0;
  807. }
  808. return status;
  809. }
  810. /* --------------------------------------------------------------------------
  811. Driver Interface
  812. -------------------------------------------------------------------------- */
  813. /* device interface */
  814. static struct acpi_video_device_attrib*
  815. acpi_video_get_device_attr(struct acpi_video_bus *video, unsigned long device_id)
  816. {
  817. struct acpi_video_enumerated_device *ids;
  818. int i;
  819. for (i = 0; i < video->attached_count; i++) {
  820. ids = &video->attached_array[i];
  821. if ((ids->value.int_val & 0xffff) == device_id)
  822. return &ids->value.attrib;
  823. }
  824. return NULL;
  825. }
  826. static int
  827. acpi_video_get_device_type(struct acpi_video_bus *video,
  828. unsigned long device_id)
  829. {
  830. struct acpi_video_enumerated_device *ids;
  831. int i;
  832. for (i = 0; i < video->attached_count; i++) {
  833. ids = &video->attached_array[i];
  834. if ((ids->value.int_val & 0xffff) == device_id)
  835. return ids->value.int_val;
  836. }
  837. return 0;
  838. }
  839. static int
  840. acpi_video_bus_get_one_device(struct acpi_device *device,
  841. struct acpi_video_bus *video)
  842. {
  843. unsigned long long device_id;
  844. int status, device_type;
  845. struct acpi_video_device *data;
  846. struct acpi_video_device_attrib* attribute;
  847. if (!device || !video)
  848. return -EINVAL;
  849. status =
  850. acpi_evaluate_integer(device->handle, "_ADR", NULL, &device_id);
  851. if (ACPI_SUCCESS(status)) {
  852. data = kzalloc(sizeof(struct acpi_video_device), GFP_KERNEL);
  853. if (!data)
  854. return -ENOMEM;
  855. strcpy(acpi_device_name(device), ACPI_VIDEO_DEVICE_NAME);
  856. strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
  857. device->driver_data = data;
  858. data->device_id = device_id;
  859. data->video = video;
  860. data->dev = device;
  861. attribute = acpi_video_get_device_attr(video, device_id);
  862. if((attribute != NULL) && attribute->device_id_scheme) {
  863. switch (attribute->display_type) {
  864. case ACPI_VIDEO_DISPLAY_CRT:
  865. data->flags.crt = 1;
  866. break;
  867. case ACPI_VIDEO_DISPLAY_TV:
  868. data->flags.tvout = 1;
  869. break;
  870. case ACPI_VIDEO_DISPLAY_DVI:
  871. data->flags.dvi = 1;
  872. break;
  873. case ACPI_VIDEO_DISPLAY_LCD:
  874. data->flags.lcd = 1;
  875. break;
  876. default:
  877. data->flags.unknown = 1;
  878. break;
  879. }
  880. if(attribute->bios_can_detect)
  881. data->flags.bios = 1;
  882. } else {
  883. /* Check for legacy IDs */
  884. device_type = acpi_video_get_device_type(video,
  885. device_id);
  886. /* Ignore bits 16 and 18-20 */
  887. switch (device_type & 0xffe2ffff) {
  888. case ACPI_VIDEO_DISPLAY_LEGACY_MONITOR:
  889. data->flags.crt = 1;
  890. break;
  891. case ACPI_VIDEO_DISPLAY_LEGACY_PANEL:
  892. data->flags.lcd = 1;
  893. break;
  894. case ACPI_VIDEO_DISPLAY_LEGACY_TV:
  895. data->flags.tvout = 1;
  896. break;
  897. default:
  898. data->flags.unknown = 1;
  899. }
  900. }
  901. acpi_video_device_bind(video, data);
  902. acpi_video_device_find_cap(data);
  903. status = acpi_install_notify_handler(device->handle,
  904. ACPI_DEVICE_NOTIFY,
  905. acpi_video_device_notify,
  906. data);
  907. if (ACPI_FAILURE(status)) {
  908. printk(KERN_ERR PREFIX
  909. "Error installing notify handler\n");
  910. if(data->brightness)
  911. kfree(data->brightness->levels);
  912. kfree(data->brightness);
  913. kfree(data);
  914. return -ENODEV;
  915. }
  916. mutex_lock(&video->device_list_lock);
  917. list_add_tail(&data->entry, &video->video_device_list);
  918. mutex_unlock(&video->device_list_lock);
  919. return 0;
  920. }
  921. return -ENOENT;
  922. }
  923. /*
  924. * Arg:
  925. * video : video bus device
  926. *
  927. * Return:
  928. * none
  929. *
  930. * Enumerate the video device list of the video bus,
  931. * bind the ids with the corresponding video devices
  932. * under the video bus.
  933. */
  934. static void acpi_video_device_rebind(struct acpi_video_bus *video)
  935. {
  936. struct acpi_video_device *dev;
  937. mutex_lock(&video->device_list_lock);
  938. list_for_each_entry(dev, &video->video_device_list, entry)
  939. acpi_video_device_bind(video, dev);
  940. mutex_unlock(&video->device_list_lock);
  941. }
  942. /*
  943. * Arg:
  944. * video : video bus device
  945. * device : video output device under the video
  946. * bus
  947. *
  948. * Return:
  949. * none
  950. *
  951. * Bind the ids with the corresponding video devices
  952. * under the video bus.
  953. */
  954. static void
  955. acpi_video_device_bind(struct acpi_video_bus *video,
  956. struct acpi_video_device *device)
  957. {
  958. struct acpi_video_enumerated_device *ids;
  959. int i;
  960. for (i = 0; i < video->attached_count; i++) {
  961. ids = &video->attached_array[i];
  962. if (device->device_id == (ids->value.int_val & 0xffff)) {
  963. ids->bind_info = device;
  964. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "device_bind %d\n", i));
  965. }
  966. }
  967. }
  968. /*
  969. * Arg:
  970. * video : video bus device
  971. *
  972. * Return:
  973. * < 0 : error
  974. *
  975. * Call _DOD to enumerate all devices attached to display adapter
  976. *
  977. */
  978. static int acpi_video_device_enumerate(struct acpi_video_bus *video)
  979. {
  980. int status;
  981. int count;
  982. int i;
  983. struct acpi_video_enumerated_device *active_list;
  984. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  985. union acpi_object *dod = NULL;
  986. union acpi_object *obj;
  987. status = acpi_evaluate_object(video->device->handle, "_DOD", NULL, &buffer);
  988. if (!ACPI_SUCCESS(status)) {
  989. ACPI_EXCEPTION((AE_INFO, status, "Evaluating _DOD"));
  990. return status;
  991. }
  992. dod = buffer.pointer;
  993. if (!dod || (dod->type != ACPI_TYPE_PACKAGE)) {
  994. ACPI_EXCEPTION((AE_INFO, status, "Invalid _DOD data"));
  995. status = -EFAULT;
  996. goto out;
  997. }
  998. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d video heads in _DOD\n",
  999. dod->package.count));
  1000. active_list = kcalloc(1 + dod->package.count,
  1001. sizeof(struct acpi_video_enumerated_device),
  1002. GFP_KERNEL);
  1003. if (!active_list) {
  1004. status = -ENOMEM;
  1005. goto out;
  1006. }
  1007. count = 0;
  1008. for (i = 0; i < dod->package.count; i++) {
  1009. obj = &dod->package.elements[i];
  1010. if (obj->type != ACPI_TYPE_INTEGER) {
  1011. printk(KERN_ERR PREFIX
  1012. "Invalid _DOD data in element %d\n", i);
  1013. continue;
  1014. }
  1015. active_list[count].value.int_val = obj->integer.value;
  1016. active_list[count].bind_info = NULL;
  1017. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "dod element[%d] = %d\n", i,
  1018. (int)obj->integer.value));
  1019. count++;
  1020. }
  1021. kfree(video->attached_array);
  1022. video->attached_array = active_list;
  1023. video->attached_count = count;
  1024. out:
  1025. kfree(buffer.pointer);
  1026. return status;
  1027. }
  1028. static int
  1029. acpi_video_get_next_level(struct acpi_video_device *device,
  1030. u32 level_current, u32 event)
  1031. {
  1032. int min, max, min_above, max_below, i, l, delta = 255;
  1033. max = max_below = 0;
  1034. min = min_above = 255;
  1035. /* Find closest level to level_current */
  1036. for (i = 2; i < device->brightness->count; i++) {
  1037. l = device->brightness->levels[i];
  1038. if (abs(l - level_current) < abs(delta)) {
  1039. delta = l - level_current;
  1040. if (!delta)
  1041. break;
  1042. }
  1043. }
  1044. /* Ajust level_current to closest available level */
  1045. level_current += delta;
  1046. for (i = 2; i < device->brightness->count; i++) {
  1047. l = device->brightness->levels[i];
  1048. if (l < min)
  1049. min = l;
  1050. if (l > max)
  1051. max = l;
  1052. if (l < min_above && l > level_current)
  1053. min_above = l;
  1054. if (l > max_below && l < level_current)
  1055. max_below = l;
  1056. }
  1057. switch (event) {
  1058. case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS:
  1059. return (level_current < max) ? min_above : min;
  1060. case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS:
  1061. return (level_current < max) ? min_above : max;
  1062. case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS:
  1063. return (level_current > min) ? max_below : min;
  1064. case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS:
  1065. case ACPI_VIDEO_NOTIFY_DISPLAY_OFF:
  1066. return 0;
  1067. default:
  1068. return level_current;
  1069. }
  1070. }
  1071. static int
  1072. acpi_video_switch_brightness(struct acpi_video_device *device, int event)
  1073. {
  1074. unsigned long long level_current, level_next;
  1075. int result = -EINVAL;
  1076. /* no warning message if acpi_backlight=vendor is used */
  1077. if (!acpi_video_backlight_support())
  1078. return 0;
  1079. if (!device->brightness)
  1080. goto out;
  1081. result = acpi_video_device_lcd_get_level_current(device,
  1082. &level_current, 0);
  1083. if (result)
  1084. goto out;
  1085. level_next = acpi_video_get_next_level(device, level_current, event);
  1086. result = acpi_video_device_lcd_set_level(device, level_next);
  1087. if (!result)
  1088. backlight_force_update(device->backlight,
  1089. BACKLIGHT_UPDATE_HOTKEY);
  1090. out:
  1091. if (result)
  1092. printk(KERN_ERR PREFIX "Failed to switch the brightness\n");
  1093. return result;
  1094. }
  1095. int acpi_video_get_edid(struct acpi_device *device, int type, int device_id,
  1096. void **edid)
  1097. {
  1098. struct acpi_video_bus *video;
  1099. struct acpi_video_device *video_device;
  1100. union acpi_object *buffer = NULL;
  1101. acpi_status status;
  1102. int i, length;
  1103. if (!device || !acpi_driver_data(device))
  1104. return -EINVAL;
  1105. video = acpi_driver_data(device);
  1106. for (i = 0; i < video->attached_count; i++) {
  1107. video_device = video->attached_array[i].bind_info;
  1108. length = 256;
  1109. if (!video_device)
  1110. continue;
  1111. if (!video_device->cap._DDC)
  1112. continue;
  1113. if (type) {
  1114. switch (type) {
  1115. case ACPI_VIDEO_DISPLAY_CRT:
  1116. if (!video_device->flags.crt)
  1117. continue;
  1118. break;
  1119. case ACPI_VIDEO_DISPLAY_TV:
  1120. if (!video_device->flags.tvout)
  1121. continue;
  1122. break;
  1123. case ACPI_VIDEO_DISPLAY_DVI:
  1124. if (!video_device->flags.dvi)
  1125. continue;
  1126. break;
  1127. case ACPI_VIDEO_DISPLAY_LCD:
  1128. if (!video_device->flags.lcd)
  1129. continue;
  1130. break;
  1131. }
  1132. } else if (video_device->device_id != device_id) {
  1133. continue;
  1134. }
  1135. status = acpi_video_device_EDID(video_device, &buffer, length);
  1136. if (ACPI_FAILURE(status) || !buffer ||
  1137. buffer->type != ACPI_TYPE_BUFFER) {
  1138. length = 128;
  1139. status = acpi_video_device_EDID(video_device, &buffer,
  1140. length);
  1141. if (ACPI_FAILURE(status) || !buffer ||
  1142. buffer->type != ACPI_TYPE_BUFFER) {
  1143. continue;
  1144. }
  1145. }
  1146. *edid = buffer->buffer.pointer;
  1147. return length;
  1148. }
  1149. return -ENODEV;
  1150. }
  1151. EXPORT_SYMBOL(acpi_video_get_edid);
  1152. static int
  1153. acpi_video_bus_get_devices(struct acpi_video_bus *video,
  1154. struct acpi_device *device)
  1155. {
  1156. int status = 0;
  1157. struct acpi_device *dev;
  1158. acpi_video_device_enumerate(video);
  1159. list_for_each_entry(dev, &device->children, node) {
  1160. status = acpi_video_bus_get_one_device(dev, video);
  1161. if (ACPI_FAILURE(status)) {
  1162. printk(KERN_WARNING PREFIX
  1163. "Can't attach device\n");
  1164. continue;
  1165. }
  1166. }
  1167. return status;
  1168. }
  1169. static int acpi_video_bus_put_one_device(struct acpi_video_device *device)
  1170. {
  1171. acpi_status status;
  1172. if (!device || !device->video)
  1173. return -ENOENT;
  1174. status = acpi_remove_notify_handler(device->dev->handle,
  1175. ACPI_DEVICE_NOTIFY,
  1176. acpi_video_device_notify);
  1177. if (ACPI_FAILURE(status)) {
  1178. printk(KERN_WARNING PREFIX
  1179. "Can't remove video notify handler\n");
  1180. }
  1181. if (device->backlight) {
  1182. backlight_device_unregister(device->backlight);
  1183. device->backlight = NULL;
  1184. }
  1185. if (device->cooling_dev) {
  1186. sysfs_remove_link(&device->dev->dev.kobj,
  1187. "thermal_cooling");
  1188. sysfs_remove_link(&device->cooling_dev->device.kobj,
  1189. "device");
  1190. thermal_cooling_device_unregister(device->cooling_dev);
  1191. device->cooling_dev = NULL;
  1192. }
  1193. return 0;
  1194. }
  1195. static int acpi_video_bus_put_devices(struct acpi_video_bus *video)
  1196. {
  1197. int status;
  1198. struct acpi_video_device *dev, *next;
  1199. mutex_lock(&video->device_list_lock);
  1200. list_for_each_entry_safe(dev, next, &video->video_device_list, entry) {
  1201. status = acpi_video_bus_put_one_device(dev);
  1202. if (ACPI_FAILURE(status))
  1203. printk(KERN_WARNING PREFIX
  1204. "hhuuhhuu bug in acpi video driver.\n");
  1205. if (dev->brightness) {
  1206. kfree(dev->brightness->levels);
  1207. kfree(dev->brightness);
  1208. }
  1209. list_del(&dev->entry);
  1210. kfree(dev);
  1211. }
  1212. mutex_unlock(&video->device_list_lock);
  1213. return 0;
  1214. }
  1215. /* acpi_video interface */
  1216. static int acpi_video_bus_start_devices(struct acpi_video_bus *video)
  1217. {
  1218. return acpi_video_bus_DOS(video, 0, 0);
  1219. }
  1220. static int acpi_video_bus_stop_devices(struct acpi_video_bus *video)
  1221. {
  1222. return acpi_video_bus_DOS(video, 0, 1);
  1223. }
  1224. static void acpi_video_bus_notify(struct acpi_device *device, u32 event)
  1225. {
  1226. struct acpi_video_bus *video = acpi_driver_data(device);
  1227. struct input_dev *input;
  1228. int keycode = 0;
  1229. if (!video)
  1230. return;
  1231. input = video->input;
  1232. switch (event) {
  1233. case ACPI_VIDEO_NOTIFY_SWITCH: /* User requested a switch,
  1234. * most likely via hotkey. */
  1235. acpi_bus_generate_proc_event(device, event, 0);
  1236. if (!acpi_notifier_call_chain(device, event, 0))
  1237. keycode = KEY_SWITCHVIDEOMODE;
  1238. break;
  1239. case ACPI_VIDEO_NOTIFY_PROBE: /* User plugged in or removed a video
  1240. * connector. */
  1241. acpi_video_device_enumerate(video);
  1242. acpi_video_device_rebind(video);
  1243. acpi_bus_generate_proc_event(device, event, 0);
  1244. keycode = KEY_SWITCHVIDEOMODE;
  1245. break;
  1246. case ACPI_VIDEO_NOTIFY_CYCLE: /* Cycle Display output hotkey pressed. */
  1247. acpi_bus_generate_proc_event(device, event, 0);
  1248. keycode = KEY_SWITCHVIDEOMODE;
  1249. break;
  1250. case ACPI_VIDEO_NOTIFY_NEXT_OUTPUT: /* Next Display output hotkey pressed. */
  1251. acpi_bus_generate_proc_event(device, event, 0);
  1252. keycode = KEY_VIDEO_NEXT;
  1253. break;
  1254. case ACPI_VIDEO_NOTIFY_PREV_OUTPUT: /* previous Display output hotkey pressed. */
  1255. acpi_bus_generate_proc_event(device, event, 0);
  1256. keycode = KEY_VIDEO_PREV;
  1257. break;
  1258. default:
  1259. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  1260. "Unsupported event [0x%x]\n", event));
  1261. break;
  1262. }
  1263. if (event != ACPI_VIDEO_NOTIFY_SWITCH)
  1264. acpi_notifier_call_chain(device, event, 0);
  1265. if (keycode) {
  1266. input_report_key(input, keycode, 1);
  1267. input_sync(input);
  1268. input_report_key(input, keycode, 0);
  1269. input_sync(input);
  1270. }
  1271. return;
  1272. }
  1273. static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data)
  1274. {
  1275. struct acpi_video_device *video_device = data;
  1276. struct acpi_device *device = NULL;
  1277. struct acpi_video_bus *bus;
  1278. struct input_dev *input;
  1279. int keycode = 0;
  1280. if (!video_device)
  1281. return;
  1282. device = video_device->dev;
  1283. bus = video_device->video;
  1284. input = bus->input;
  1285. switch (event) {
  1286. case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS: /* Cycle brightness */
  1287. if (brightness_switch_enabled)
  1288. acpi_video_switch_brightness(video_device, event);
  1289. acpi_bus_generate_proc_event(device, event, 0);
  1290. keycode = KEY_BRIGHTNESS_CYCLE;
  1291. break;
  1292. case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS: /* Increase brightness */
  1293. if (brightness_switch_enabled)
  1294. acpi_video_switch_brightness(video_device, event);
  1295. acpi_bus_generate_proc_event(device, event, 0);
  1296. keycode = KEY_BRIGHTNESSUP;
  1297. break;
  1298. case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS: /* Decrease brightness */
  1299. if (brightness_switch_enabled)
  1300. acpi_video_switch_brightness(video_device, event);
  1301. acpi_bus_generate_proc_event(device, event, 0);
  1302. keycode = KEY_BRIGHTNESSDOWN;
  1303. break;
  1304. case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS: /* zero brightness */
  1305. if (brightness_switch_enabled)
  1306. acpi_video_switch_brightness(video_device, event);
  1307. acpi_bus_generate_proc_event(device, event, 0);
  1308. keycode = KEY_BRIGHTNESS_ZERO;
  1309. break;
  1310. case ACPI_VIDEO_NOTIFY_DISPLAY_OFF: /* display device off */
  1311. if (brightness_switch_enabled)
  1312. acpi_video_switch_brightness(video_device, event);
  1313. acpi_bus_generate_proc_event(device, event, 0);
  1314. keycode = KEY_DISPLAY_OFF;
  1315. break;
  1316. default:
  1317. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  1318. "Unsupported event [0x%x]\n", event));
  1319. break;
  1320. }
  1321. acpi_notifier_call_chain(device, event, 0);
  1322. if (keycode) {
  1323. input_report_key(input, keycode, 1);
  1324. input_sync(input);
  1325. input_report_key(input, keycode, 0);
  1326. input_sync(input);
  1327. }
  1328. return;
  1329. }
  1330. static int acpi_video_resume(struct notifier_block *nb,
  1331. unsigned long val, void *ign)
  1332. {
  1333. struct acpi_video_bus *video;
  1334. struct acpi_video_device *video_device;
  1335. int i;
  1336. switch (val) {
  1337. case PM_HIBERNATION_PREPARE:
  1338. case PM_SUSPEND_PREPARE:
  1339. case PM_RESTORE_PREPARE:
  1340. return NOTIFY_DONE;
  1341. }
  1342. video = container_of(nb, struct acpi_video_bus, pm_nb);
  1343. dev_info(&video->device->dev, "Restoring backlight state\n");
  1344. for (i = 0; i < video->attached_count; i++) {
  1345. video_device = video->attached_array[i].bind_info;
  1346. if (video_device && video_device->backlight)
  1347. acpi_video_set_brightness(video_device->backlight);
  1348. }
  1349. return NOTIFY_OK;
  1350. }
  1351. static acpi_status
  1352. acpi_video_bus_match(acpi_handle handle, u32 level, void *context,
  1353. void **return_value)
  1354. {
  1355. struct acpi_device *device = context;
  1356. struct acpi_device *sibling;
  1357. int result;
  1358. if (handle == device->handle)
  1359. return AE_CTRL_TERMINATE;
  1360. result = acpi_bus_get_device(handle, &sibling);
  1361. if (result)
  1362. return AE_OK;
  1363. if (!strcmp(acpi_device_name(sibling), ACPI_VIDEO_BUS_NAME))
  1364. return AE_ALREADY_EXISTS;
  1365. return AE_OK;
  1366. }
  1367. static int instance;
  1368. static int acpi_video_bus_add(struct acpi_device *device)
  1369. {
  1370. struct acpi_video_bus *video;
  1371. struct input_dev *input;
  1372. int error;
  1373. acpi_status status;
  1374. status = acpi_walk_namespace(ACPI_TYPE_DEVICE,
  1375. device->parent->handle, 1,
  1376. acpi_video_bus_match, NULL,
  1377. device, NULL);
  1378. if (status == AE_ALREADY_EXISTS) {
  1379. printk(KERN_WARNING FW_BUG
  1380. "Duplicate ACPI video bus devices for the"
  1381. " same VGA controller, please try module "
  1382. "parameter \"video.allow_duplicates=1\""
  1383. "if the current driver doesn't work.\n");
  1384. if (!allow_duplicates)
  1385. return -ENODEV;
  1386. }
  1387. video = kzalloc(sizeof(struct acpi_video_bus), GFP_KERNEL);
  1388. if (!video)
  1389. return -ENOMEM;
  1390. /* a hack to fix the duplicate name "VID" problem on T61 */
  1391. if (!strcmp(device->pnp.bus_id, "VID")) {
  1392. if (instance)
  1393. device->pnp.bus_id[3] = '0' + instance;
  1394. instance ++;
  1395. }
  1396. /* a hack to fix the duplicate name "VGA" problem on Pa 3553 */
  1397. if (!strcmp(device->pnp.bus_id, "VGA")) {
  1398. if (instance)
  1399. device->pnp.bus_id[3] = '0' + instance;
  1400. instance++;
  1401. }
  1402. video->device = device;
  1403. strcpy(acpi_device_name(device), ACPI_VIDEO_BUS_NAME);
  1404. strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
  1405. device->driver_data = video;
  1406. acpi_video_bus_find_cap(video);
  1407. error = acpi_video_bus_check(video);
  1408. if (error)
  1409. goto err_free_video;
  1410. mutex_init(&video->device_list_lock);
  1411. INIT_LIST_HEAD(&video->video_device_list);
  1412. acpi_video_bus_get_devices(video, device);
  1413. acpi_video_bus_start_devices(video);
  1414. video->input = input = input_allocate_device();
  1415. if (!input) {
  1416. error = -ENOMEM;
  1417. goto err_stop_video;
  1418. }
  1419. snprintf(video->phys, sizeof(video->phys),
  1420. "%s/video/input0", acpi_device_hid(video->device));
  1421. input->name = acpi_device_name(video->device);
  1422. input->phys = video->phys;
  1423. input->id.bustype = BUS_HOST;
  1424. input->id.product = 0x06;
  1425. input->dev.parent = &device->dev;
  1426. input->evbit[0] = BIT(EV_KEY);
  1427. set_bit(KEY_SWITCHVIDEOMODE, input->keybit);
  1428. set_bit(KEY_VIDEO_NEXT, input->keybit);
  1429. set_bit(KEY_VIDEO_PREV, input->keybit);
  1430. set_bit(KEY_BRIGHTNESS_CYCLE, input->keybit);
  1431. set_bit(KEY_BRIGHTNESSUP, input->keybit);
  1432. set_bit(KEY_BRIGHTNESSDOWN, input->keybit);
  1433. set_bit(KEY_BRIGHTNESS_ZERO, input->keybit);
  1434. set_bit(KEY_DISPLAY_OFF, input->keybit);
  1435. error = input_register_device(input);
  1436. if (error)
  1437. goto err_free_input_dev;
  1438. printk(KERN_INFO PREFIX "%s [%s] (multi-head: %s rom: %s post: %s)\n",
  1439. ACPI_VIDEO_DEVICE_NAME, acpi_device_bid(device),
  1440. video->flags.multihead ? "yes" : "no",
  1441. video->flags.rom ? "yes" : "no",
  1442. video->flags.post ? "yes" : "no");
  1443. video->pm_nb.notifier_call = acpi_video_resume;
  1444. video->pm_nb.priority = 0;
  1445. register_pm_notifier(&video->pm_nb);
  1446. return 0;
  1447. err_free_input_dev:
  1448. input_free_device(input);
  1449. err_stop_video:
  1450. acpi_video_bus_stop_devices(video);
  1451. acpi_video_bus_put_devices(video);
  1452. kfree(video->attached_array);
  1453. err_free_video:
  1454. kfree(video);
  1455. device->driver_data = NULL;
  1456. return error;
  1457. }
  1458. static int acpi_video_bus_remove(struct acpi_device *device, int type)
  1459. {
  1460. struct acpi_video_bus *video = NULL;
  1461. if (!device || !acpi_driver_data(device))
  1462. return -EINVAL;
  1463. video = acpi_driver_data(device);
  1464. unregister_pm_notifier(&video->pm_nb);
  1465. acpi_video_bus_stop_devices(video);
  1466. acpi_video_bus_put_devices(video);
  1467. input_unregister_device(video->input);
  1468. kfree(video->attached_array);
  1469. kfree(video);
  1470. return 0;
  1471. }
  1472. static int __init intel_opregion_present(void)
  1473. {
  1474. #if defined(CONFIG_DRM_I915) || defined(CONFIG_DRM_I915_MODULE)
  1475. struct pci_dev *dev = NULL;
  1476. u32 address;
  1477. for_each_pci_dev(dev) {
  1478. if ((dev->class >> 8) != PCI_CLASS_DISPLAY_VGA)
  1479. continue;
  1480. if (dev->vendor != PCI_VENDOR_ID_INTEL)
  1481. continue;
  1482. pci_read_config_dword(dev, 0xfc, &address);
  1483. if (!address)
  1484. continue;
  1485. return 1;
  1486. }
  1487. #endif
  1488. return 0;
  1489. }
  1490. int acpi_video_register(void)
  1491. {
  1492. int result = 0;
  1493. if (register_count) {
  1494. /*
  1495. * if the function of acpi_video_register is already called,
  1496. * don't register the acpi_vide_bus again and return no error.
  1497. */
  1498. return 0;
  1499. }
  1500. result = acpi_bus_register_driver(&acpi_video_bus);
  1501. if (result < 0)
  1502. return -ENODEV;
  1503. /*
  1504. * When the acpi_video_bus is loaded successfully, increase
  1505. * the counter reference.
  1506. */
  1507. register_count = 1;
  1508. return 0;
  1509. }
  1510. EXPORT_SYMBOL(acpi_video_register);
  1511. void acpi_video_unregister(void)
  1512. {
  1513. if (!register_count) {
  1514. /*
  1515. * If the acpi video bus is already unloaded, don't
  1516. * unload it again and return directly.
  1517. */
  1518. return;
  1519. }
  1520. acpi_bus_unregister_driver(&acpi_video_bus);
  1521. register_count = 0;
  1522. return;
  1523. }
  1524. EXPORT_SYMBOL(acpi_video_unregister);
  1525. /*
  1526. * This is kind of nasty. Hardware using Intel chipsets may require
  1527. * the video opregion code to be run first in order to initialise
  1528. * state before any ACPI video calls are made. To handle this we defer
  1529. * registration of the video class until the opregion code has run.
  1530. */
  1531. static int __init acpi_video_init(void)
  1532. {
  1533. dmi_check_system(video_dmi_table);
  1534. if (intel_opregion_present())
  1535. return 0;
  1536. return acpi_video_register();
  1537. }
  1538. static void __exit acpi_video_exit(void)
  1539. {
  1540. acpi_video_unregister();
  1541. return;
  1542. }
  1543. module_init(acpi_video_init);
  1544. module_exit(acpi_video_exit);