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_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. struct pci_dev *pdev;
  679. acpi_handle acpi_parent;
  680. struct device *parent = NULL;
  681. int result;
  682. static int count = 0;
  683. char *name;
  684. result = acpi_video_init_brightness(device);
  685. if (result)
  686. return;
  687. name = kasprintf(GFP_KERNEL, "acpi_video%d", count);
  688. if (!name)
  689. return;
  690. count++;
  691. acpi_get_parent(device->dev->handle, &acpi_parent);
  692. pdev = acpi_get_pci_dev(acpi_parent);
  693. if (pdev) {
  694. parent = &pdev->dev;
  695. pci_dev_put(pdev);
  696. }
  697. memset(&props, 0, sizeof(struct backlight_properties));
  698. props.type = BACKLIGHT_FIRMWARE;
  699. props.max_brightness = device->brightness->count - 3;
  700. device->backlight = backlight_device_register(name,
  701. parent,
  702. device,
  703. &acpi_backlight_ops,
  704. &props);
  705. kfree(name);
  706. if (IS_ERR(device->backlight))
  707. return;
  708. /*
  709. * Save current brightness level in case we have to restore it
  710. * before acpi_video_device_lcd_set_level() is called next time.
  711. */
  712. device->backlight->props.brightness =
  713. acpi_video_get_brightness(device->backlight);
  714. device->cooling_dev = thermal_cooling_device_register("LCD",
  715. device->dev, &video_cooling_ops);
  716. if (IS_ERR(device->cooling_dev)) {
  717. /*
  718. * Set cooling_dev to NULL so we don't crash trying to
  719. * free it.
  720. * Also, why the hell we are returning early and
  721. * not attempt to register video output if cooling
  722. * device registration failed?
  723. * -- dtor
  724. */
  725. device->cooling_dev = NULL;
  726. return;
  727. }
  728. dev_info(&device->dev->dev, "registered as cooling_device%d\n",
  729. device->cooling_dev->id);
  730. result = sysfs_create_link(&device->dev->dev.kobj,
  731. &device->cooling_dev->device.kobj,
  732. "thermal_cooling");
  733. if (result)
  734. printk(KERN_ERR PREFIX "Create sysfs link\n");
  735. result = sysfs_create_link(&device->cooling_dev->device.kobj,
  736. &device->dev->dev.kobj, "device");
  737. if (result)
  738. printk(KERN_ERR PREFIX "Create sysfs link\n");
  739. }
  740. }
  741. /*
  742. * Arg:
  743. * device : video output device (VGA)
  744. *
  745. * Return Value:
  746. * None
  747. *
  748. * Find out all required AML methods defined under the video bus device.
  749. */
  750. static void acpi_video_bus_find_cap(struct acpi_video_bus *video)
  751. {
  752. acpi_handle h_dummy1;
  753. if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_DOS", &h_dummy1))) {
  754. video->cap._DOS = 1;
  755. }
  756. if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_DOD", &h_dummy1))) {
  757. video->cap._DOD = 1;
  758. }
  759. if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_ROM", &h_dummy1))) {
  760. video->cap._ROM = 1;
  761. }
  762. if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_GPD", &h_dummy1))) {
  763. video->cap._GPD = 1;
  764. }
  765. if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_SPD", &h_dummy1))) {
  766. video->cap._SPD = 1;
  767. }
  768. if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_VPO", &h_dummy1))) {
  769. video->cap._VPO = 1;
  770. }
  771. }
  772. /*
  773. * Check whether the video bus device has required AML method to
  774. * support the desired features
  775. */
  776. static int acpi_video_bus_check(struct acpi_video_bus *video)
  777. {
  778. acpi_status status = -ENOENT;
  779. struct pci_dev *dev;
  780. if (!video)
  781. return -EINVAL;
  782. dev = acpi_get_pci_dev(video->device->handle);
  783. if (!dev)
  784. return -ENODEV;
  785. pci_dev_put(dev);
  786. /* Since there is no HID, CID and so on for VGA driver, we have
  787. * to check well known required nodes.
  788. */
  789. /* Does this device support video switching? */
  790. if (video->cap._DOS || video->cap._DOD) {
  791. if (!video->cap._DOS) {
  792. printk(KERN_WARNING FW_BUG
  793. "ACPI(%s) defines _DOD but not _DOS\n",
  794. acpi_device_bid(video->device));
  795. }
  796. video->flags.multihead = 1;
  797. status = 0;
  798. }
  799. /* Does this device support retrieving a video ROM? */
  800. if (video->cap._ROM) {
  801. video->flags.rom = 1;
  802. status = 0;
  803. }
  804. /* Does this device support configuring which video device to POST? */
  805. if (video->cap._GPD && video->cap._SPD && video->cap._VPO) {
  806. video->flags.post = 1;
  807. status = 0;
  808. }
  809. return status;
  810. }
  811. /* --------------------------------------------------------------------------
  812. Driver Interface
  813. -------------------------------------------------------------------------- */
  814. /* device interface */
  815. static struct acpi_video_device_attrib*
  816. acpi_video_get_device_attr(struct acpi_video_bus *video, unsigned long device_id)
  817. {
  818. struct acpi_video_enumerated_device *ids;
  819. int i;
  820. for (i = 0; i < video->attached_count; i++) {
  821. ids = &video->attached_array[i];
  822. if ((ids->value.int_val & 0xffff) == device_id)
  823. return &ids->value.attrib;
  824. }
  825. return NULL;
  826. }
  827. static int
  828. acpi_video_get_device_type(struct acpi_video_bus *video,
  829. unsigned long device_id)
  830. {
  831. struct acpi_video_enumerated_device *ids;
  832. int i;
  833. for (i = 0; i < video->attached_count; i++) {
  834. ids = &video->attached_array[i];
  835. if ((ids->value.int_val & 0xffff) == device_id)
  836. return ids->value.int_val;
  837. }
  838. return 0;
  839. }
  840. static int
  841. acpi_video_bus_get_one_device(struct acpi_device *device,
  842. struct acpi_video_bus *video)
  843. {
  844. unsigned long long device_id;
  845. int status, device_type;
  846. struct acpi_video_device *data;
  847. struct acpi_video_device_attrib* attribute;
  848. if (!device || !video)
  849. return -EINVAL;
  850. status =
  851. acpi_evaluate_integer(device->handle, "_ADR", NULL, &device_id);
  852. if (ACPI_SUCCESS(status)) {
  853. data = kzalloc(sizeof(struct acpi_video_device), GFP_KERNEL);
  854. if (!data)
  855. return -ENOMEM;
  856. strcpy(acpi_device_name(device), ACPI_VIDEO_DEVICE_NAME);
  857. strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
  858. device->driver_data = data;
  859. data->device_id = device_id;
  860. data->video = video;
  861. data->dev = device;
  862. attribute = acpi_video_get_device_attr(video, device_id);
  863. if((attribute != NULL) && attribute->device_id_scheme) {
  864. switch (attribute->display_type) {
  865. case ACPI_VIDEO_DISPLAY_CRT:
  866. data->flags.crt = 1;
  867. break;
  868. case ACPI_VIDEO_DISPLAY_TV:
  869. data->flags.tvout = 1;
  870. break;
  871. case ACPI_VIDEO_DISPLAY_DVI:
  872. data->flags.dvi = 1;
  873. break;
  874. case ACPI_VIDEO_DISPLAY_LCD:
  875. data->flags.lcd = 1;
  876. break;
  877. default:
  878. data->flags.unknown = 1;
  879. break;
  880. }
  881. if(attribute->bios_can_detect)
  882. data->flags.bios = 1;
  883. } else {
  884. /* Check for legacy IDs */
  885. device_type = acpi_video_get_device_type(video,
  886. device_id);
  887. /* Ignore bits 16 and 18-20 */
  888. switch (device_type & 0xffe2ffff) {
  889. case ACPI_VIDEO_DISPLAY_LEGACY_MONITOR:
  890. data->flags.crt = 1;
  891. break;
  892. case ACPI_VIDEO_DISPLAY_LEGACY_PANEL:
  893. data->flags.lcd = 1;
  894. break;
  895. case ACPI_VIDEO_DISPLAY_LEGACY_TV:
  896. data->flags.tvout = 1;
  897. break;
  898. default:
  899. data->flags.unknown = 1;
  900. }
  901. }
  902. acpi_video_device_bind(video, data);
  903. acpi_video_device_find_cap(data);
  904. status = acpi_install_notify_handler(device->handle,
  905. ACPI_DEVICE_NOTIFY,
  906. acpi_video_device_notify,
  907. data);
  908. if (ACPI_FAILURE(status)) {
  909. printk(KERN_ERR PREFIX
  910. "Error installing notify handler\n");
  911. if(data->brightness)
  912. kfree(data->brightness->levels);
  913. kfree(data->brightness);
  914. kfree(data);
  915. return -ENODEV;
  916. }
  917. mutex_lock(&video->device_list_lock);
  918. list_add_tail(&data->entry, &video->video_device_list);
  919. mutex_unlock(&video->device_list_lock);
  920. return 0;
  921. }
  922. return -ENOENT;
  923. }
  924. /*
  925. * Arg:
  926. * video : video bus device
  927. *
  928. * Return:
  929. * none
  930. *
  931. * Enumerate the video device list of the video bus,
  932. * bind the ids with the corresponding video devices
  933. * under the video bus.
  934. */
  935. static void acpi_video_device_rebind(struct acpi_video_bus *video)
  936. {
  937. struct acpi_video_device *dev;
  938. mutex_lock(&video->device_list_lock);
  939. list_for_each_entry(dev, &video->video_device_list, entry)
  940. acpi_video_device_bind(video, dev);
  941. mutex_unlock(&video->device_list_lock);
  942. }
  943. /*
  944. * Arg:
  945. * video : video bus device
  946. * device : video output device under the video
  947. * bus
  948. *
  949. * Return:
  950. * none
  951. *
  952. * Bind the ids with the corresponding video devices
  953. * under the video bus.
  954. */
  955. static void
  956. acpi_video_device_bind(struct acpi_video_bus *video,
  957. struct acpi_video_device *device)
  958. {
  959. struct acpi_video_enumerated_device *ids;
  960. int i;
  961. for (i = 0; i < video->attached_count; i++) {
  962. ids = &video->attached_array[i];
  963. if (device->device_id == (ids->value.int_val & 0xffff)) {
  964. ids->bind_info = device;
  965. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "device_bind %d\n", i));
  966. }
  967. }
  968. }
  969. /*
  970. * Arg:
  971. * video : video bus device
  972. *
  973. * Return:
  974. * < 0 : error
  975. *
  976. * Call _DOD to enumerate all devices attached to display adapter
  977. *
  978. */
  979. static int acpi_video_device_enumerate(struct acpi_video_bus *video)
  980. {
  981. int status;
  982. int count;
  983. int i;
  984. struct acpi_video_enumerated_device *active_list;
  985. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  986. union acpi_object *dod = NULL;
  987. union acpi_object *obj;
  988. status = acpi_evaluate_object(video->device->handle, "_DOD", NULL, &buffer);
  989. if (!ACPI_SUCCESS(status)) {
  990. ACPI_EXCEPTION((AE_INFO, status, "Evaluating _DOD"));
  991. return status;
  992. }
  993. dod = buffer.pointer;
  994. if (!dod || (dod->type != ACPI_TYPE_PACKAGE)) {
  995. ACPI_EXCEPTION((AE_INFO, status, "Invalid _DOD data"));
  996. status = -EFAULT;
  997. goto out;
  998. }
  999. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d video heads in _DOD\n",
  1000. dod->package.count));
  1001. active_list = kcalloc(1 + dod->package.count,
  1002. sizeof(struct acpi_video_enumerated_device),
  1003. GFP_KERNEL);
  1004. if (!active_list) {
  1005. status = -ENOMEM;
  1006. goto out;
  1007. }
  1008. count = 0;
  1009. for (i = 0; i < dod->package.count; i++) {
  1010. obj = &dod->package.elements[i];
  1011. if (obj->type != ACPI_TYPE_INTEGER) {
  1012. printk(KERN_ERR PREFIX
  1013. "Invalid _DOD data in element %d\n", i);
  1014. continue;
  1015. }
  1016. active_list[count].value.int_val = obj->integer.value;
  1017. active_list[count].bind_info = NULL;
  1018. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "dod element[%d] = %d\n", i,
  1019. (int)obj->integer.value));
  1020. count++;
  1021. }
  1022. kfree(video->attached_array);
  1023. video->attached_array = active_list;
  1024. video->attached_count = count;
  1025. out:
  1026. kfree(buffer.pointer);
  1027. return status;
  1028. }
  1029. static int
  1030. acpi_video_get_next_level(struct acpi_video_device *device,
  1031. u32 level_current, u32 event)
  1032. {
  1033. int min, max, min_above, max_below, i, l, delta = 255;
  1034. max = max_below = 0;
  1035. min = min_above = 255;
  1036. /* Find closest level to level_current */
  1037. for (i = 2; i < device->brightness->count; i++) {
  1038. l = device->brightness->levels[i];
  1039. if (abs(l - level_current) < abs(delta)) {
  1040. delta = l - level_current;
  1041. if (!delta)
  1042. break;
  1043. }
  1044. }
  1045. /* Ajust level_current to closest available level */
  1046. level_current += delta;
  1047. for (i = 2; i < device->brightness->count; i++) {
  1048. l = device->brightness->levels[i];
  1049. if (l < min)
  1050. min = l;
  1051. if (l > max)
  1052. max = l;
  1053. if (l < min_above && l > level_current)
  1054. min_above = l;
  1055. if (l > max_below && l < level_current)
  1056. max_below = l;
  1057. }
  1058. switch (event) {
  1059. case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS:
  1060. return (level_current < max) ? min_above : min;
  1061. case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS:
  1062. return (level_current < max) ? min_above : max;
  1063. case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS:
  1064. return (level_current > min) ? max_below : min;
  1065. case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS:
  1066. case ACPI_VIDEO_NOTIFY_DISPLAY_OFF:
  1067. return 0;
  1068. default:
  1069. return level_current;
  1070. }
  1071. }
  1072. static int
  1073. acpi_video_switch_brightness(struct acpi_video_device *device, int event)
  1074. {
  1075. unsigned long long level_current, level_next;
  1076. int result = -EINVAL;
  1077. /* no warning message if acpi_backlight=vendor is used */
  1078. if (!acpi_video_backlight_support())
  1079. return 0;
  1080. if (!device->brightness)
  1081. goto out;
  1082. result = acpi_video_device_lcd_get_level_current(device,
  1083. &level_current, 0);
  1084. if (result)
  1085. goto out;
  1086. level_next = acpi_video_get_next_level(device, level_current, event);
  1087. result = acpi_video_device_lcd_set_level(device, level_next);
  1088. if (!result)
  1089. backlight_force_update(device->backlight,
  1090. BACKLIGHT_UPDATE_HOTKEY);
  1091. out:
  1092. if (result)
  1093. printk(KERN_ERR PREFIX "Failed to switch the brightness\n");
  1094. return result;
  1095. }
  1096. int acpi_video_get_edid(struct acpi_device *device, int type, int device_id,
  1097. void **edid)
  1098. {
  1099. struct acpi_video_bus *video;
  1100. struct acpi_video_device *video_device;
  1101. union acpi_object *buffer = NULL;
  1102. acpi_status status;
  1103. int i, length;
  1104. if (!device || !acpi_driver_data(device))
  1105. return -EINVAL;
  1106. video = acpi_driver_data(device);
  1107. for (i = 0; i < video->attached_count; i++) {
  1108. video_device = video->attached_array[i].bind_info;
  1109. length = 256;
  1110. if (!video_device)
  1111. continue;
  1112. if (!video_device->cap._DDC)
  1113. continue;
  1114. if (type) {
  1115. switch (type) {
  1116. case ACPI_VIDEO_DISPLAY_CRT:
  1117. if (!video_device->flags.crt)
  1118. continue;
  1119. break;
  1120. case ACPI_VIDEO_DISPLAY_TV:
  1121. if (!video_device->flags.tvout)
  1122. continue;
  1123. break;
  1124. case ACPI_VIDEO_DISPLAY_DVI:
  1125. if (!video_device->flags.dvi)
  1126. continue;
  1127. break;
  1128. case ACPI_VIDEO_DISPLAY_LCD:
  1129. if (!video_device->flags.lcd)
  1130. continue;
  1131. break;
  1132. }
  1133. } else if (video_device->device_id != device_id) {
  1134. continue;
  1135. }
  1136. status = acpi_video_device_EDID(video_device, &buffer, length);
  1137. if (ACPI_FAILURE(status) || !buffer ||
  1138. buffer->type != ACPI_TYPE_BUFFER) {
  1139. length = 128;
  1140. status = acpi_video_device_EDID(video_device, &buffer,
  1141. length);
  1142. if (ACPI_FAILURE(status) || !buffer ||
  1143. buffer->type != ACPI_TYPE_BUFFER) {
  1144. continue;
  1145. }
  1146. }
  1147. *edid = buffer->buffer.pointer;
  1148. return length;
  1149. }
  1150. return -ENODEV;
  1151. }
  1152. EXPORT_SYMBOL(acpi_video_get_edid);
  1153. static int
  1154. acpi_video_bus_get_devices(struct acpi_video_bus *video,
  1155. struct acpi_device *device)
  1156. {
  1157. int status = 0;
  1158. struct acpi_device *dev;
  1159. acpi_video_device_enumerate(video);
  1160. list_for_each_entry(dev, &device->children, node) {
  1161. status = acpi_video_bus_get_one_device(dev, video);
  1162. if (ACPI_FAILURE(status)) {
  1163. printk(KERN_WARNING PREFIX
  1164. "Can't attach device\n");
  1165. continue;
  1166. }
  1167. }
  1168. return status;
  1169. }
  1170. static int acpi_video_bus_put_one_device(struct acpi_video_device *device)
  1171. {
  1172. acpi_status status;
  1173. if (!device || !device->video)
  1174. return -ENOENT;
  1175. status = acpi_remove_notify_handler(device->dev->handle,
  1176. ACPI_DEVICE_NOTIFY,
  1177. acpi_video_device_notify);
  1178. if (ACPI_FAILURE(status)) {
  1179. printk(KERN_WARNING PREFIX
  1180. "Can't remove video notify handler\n");
  1181. }
  1182. if (device->backlight) {
  1183. backlight_device_unregister(device->backlight);
  1184. device->backlight = NULL;
  1185. }
  1186. if (device->cooling_dev) {
  1187. sysfs_remove_link(&device->dev->dev.kobj,
  1188. "thermal_cooling");
  1189. sysfs_remove_link(&device->cooling_dev->device.kobj,
  1190. "device");
  1191. thermal_cooling_device_unregister(device->cooling_dev);
  1192. device->cooling_dev = NULL;
  1193. }
  1194. return 0;
  1195. }
  1196. static int acpi_video_bus_put_devices(struct acpi_video_bus *video)
  1197. {
  1198. int status;
  1199. struct acpi_video_device *dev, *next;
  1200. mutex_lock(&video->device_list_lock);
  1201. list_for_each_entry_safe(dev, next, &video->video_device_list, entry) {
  1202. status = acpi_video_bus_put_one_device(dev);
  1203. if (ACPI_FAILURE(status))
  1204. printk(KERN_WARNING PREFIX
  1205. "hhuuhhuu bug in acpi video driver.\n");
  1206. if (dev->brightness) {
  1207. kfree(dev->brightness->levels);
  1208. kfree(dev->brightness);
  1209. }
  1210. list_del(&dev->entry);
  1211. kfree(dev);
  1212. }
  1213. mutex_unlock(&video->device_list_lock);
  1214. return 0;
  1215. }
  1216. /* acpi_video interface */
  1217. static int acpi_video_bus_start_devices(struct acpi_video_bus *video)
  1218. {
  1219. return acpi_video_bus_DOS(video, 0, 0);
  1220. }
  1221. static int acpi_video_bus_stop_devices(struct acpi_video_bus *video)
  1222. {
  1223. return acpi_video_bus_DOS(video, 0, 1);
  1224. }
  1225. static void acpi_video_bus_notify(struct acpi_device *device, u32 event)
  1226. {
  1227. struct acpi_video_bus *video = acpi_driver_data(device);
  1228. struct input_dev *input;
  1229. int keycode = 0;
  1230. if (!video)
  1231. return;
  1232. input = video->input;
  1233. switch (event) {
  1234. case ACPI_VIDEO_NOTIFY_SWITCH: /* User requested a switch,
  1235. * most likely via hotkey. */
  1236. acpi_bus_generate_proc_event(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. acpi_notifier_call_chain(device, event, 0);
  1264. if (keycode) {
  1265. input_report_key(input, keycode, 1);
  1266. input_sync(input);
  1267. input_report_key(input, keycode, 0);
  1268. input_sync(input);
  1269. }
  1270. return;
  1271. }
  1272. static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data)
  1273. {
  1274. struct acpi_video_device *video_device = data;
  1275. struct acpi_device *device = NULL;
  1276. struct acpi_video_bus *bus;
  1277. struct input_dev *input;
  1278. int keycode = 0;
  1279. if (!video_device)
  1280. return;
  1281. device = video_device->dev;
  1282. bus = video_device->video;
  1283. input = bus->input;
  1284. switch (event) {
  1285. case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS: /* Cycle brightness */
  1286. if (brightness_switch_enabled)
  1287. acpi_video_switch_brightness(video_device, event);
  1288. acpi_bus_generate_proc_event(device, event, 0);
  1289. keycode = KEY_BRIGHTNESS_CYCLE;
  1290. break;
  1291. case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS: /* Increase brightness */
  1292. if (brightness_switch_enabled)
  1293. acpi_video_switch_brightness(video_device, event);
  1294. acpi_bus_generate_proc_event(device, event, 0);
  1295. keycode = KEY_BRIGHTNESSUP;
  1296. break;
  1297. case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS: /* Decrease brightness */
  1298. if (brightness_switch_enabled)
  1299. acpi_video_switch_brightness(video_device, event);
  1300. acpi_bus_generate_proc_event(device, event, 0);
  1301. keycode = KEY_BRIGHTNESSDOWN;
  1302. break;
  1303. case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS: /* zero brightness */
  1304. if (brightness_switch_enabled)
  1305. acpi_video_switch_brightness(video_device, event);
  1306. acpi_bus_generate_proc_event(device, event, 0);
  1307. keycode = KEY_BRIGHTNESS_ZERO;
  1308. break;
  1309. case ACPI_VIDEO_NOTIFY_DISPLAY_OFF: /* display device off */
  1310. if (brightness_switch_enabled)
  1311. acpi_video_switch_brightness(video_device, event);
  1312. acpi_bus_generate_proc_event(device, event, 0);
  1313. keycode = KEY_DISPLAY_OFF;
  1314. break;
  1315. default:
  1316. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  1317. "Unsupported event [0x%x]\n", event));
  1318. break;
  1319. }
  1320. acpi_notifier_call_chain(device, event, 0);
  1321. if (keycode) {
  1322. input_report_key(input, keycode, 1);
  1323. input_sync(input);
  1324. input_report_key(input, keycode, 0);
  1325. input_sync(input);
  1326. }
  1327. return;
  1328. }
  1329. static int acpi_video_resume(struct notifier_block *nb,
  1330. unsigned long val, void *ign)
  1331. {
  1332. struct acpi_video_bus *video;
  1333. struct acpi_video_device *video_device;
  1334. int i;
  1335. switch (val) {
  1336. case PM_HIBERNATION_PREPARE:
  1337. case PM_SUSPEND_PREPARE:
  1338. case PM_RESTORE_PREPARE:
  1339. return NOTIFY_DONE;
  1340. }
  1341. video = container_of(nb, struct acpi_video_bus, pm_nb);
  1342. dev_info(&video->device->dev, "Restoring backlight state\n");
  1343. for (i = 0; i < video->attached_count; i++) {
  1344. video_device = video->attached_array[i].bind_info;
  1345. if (video_device && video_device->backlight)
  1346. acpi_video_set_brightness(video_device->backlight);
  1347. }
  1348. return NOTIFY_OK;
  1349. }
  1350. static acpi_status
  1351. acpi_video_bus_match(acpi_handle handle, u32 level, void *context,
  1352. void **return_value)
  1353. {
  1354. struct acpi_device *device = context;
  1355. struct acpi_device *sibling;
  1356. int result;
  1357. if (handle == device->handle)
  1358. return AE_CTRL_TERMINATE;
  1359. result = acpi_bus_get_device(handle, &sibling);
  1360. if (result)
  1361. return AE_OK;
  1362. if (!strcmp(acpi_device_name(sibling), ACPI_VIDEO_BUS_NAME))
  1363. return AE_ALREADY_EXISTS;
  1364. return AE_OK;
  1365. }
  1366. static int instance;
  1367. static int acpi_video_bus_add(struct acpi_device *device)
  1368. {
  1369. struct acpi_video_bus *video;
  1370. struct input_dev *input;
  1371. int error;
  1372. acpi_status status;
  1373. status = acpi_walk_namespace(ACPI_TYPE_DEVICE,
  1374. device->parent->handle, 1,
  1375. acpi_video_bus_match, NULL,
  1376. device, NULL);
  1377. if (status == AE_ALREADY_EXISTS) {
  1378. printk(KERN_WARNING FW_BUG
  1379. "Duplicate ACPI video bus devices for the"
  1380. " same VGA controller, please try module "
  1381. "parameter \"video.allow_duplicates=1\""
  1382. "if the current driver doesn't work.\n");
  1383. if (!allow_duplicates)
  1384. return -ENODEV;
  1385. }
  1386. video = kzalloc(sizeof(struct acpi_video_bus), GFP_KERNEL);
  1387. if (!video)
  1388. return -ENOMEM;
  1389. /* a hack to fix the duplicate name "VID" problem on T61 */
  1390. if (!strcmp(device->pnp.bus_id, "VID")) {
  1391. if (instance)
  1392. device->pnp.bus_id[3] = '0' + instance;
  1393. instance ++;
  1394. }
  1395. /* a hack to fix the duplicate name "VGA" problem on Pa 3553 */
  1396. if (!strcmp(device->pnp.bus_id, "VGA")) {
  1397. if (instance)
  1398. device->pnp.bus_id[3] = '0' + instance;
  1399. instance++;
  1400. }
  1401. video->device = device;
  1402. strcpy(acpi_device_name(device), ACPI_VIDEO_BUS_NAME);
  1403. strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
  1404. device->driver_data = video;
  1405. acpi_video_bus_find_cap(video);
  1406. error = acpi_video_bus_check(video);
  1407. if (error)
  1408. goto err_free_video;
  1409. mutex_init(&video->device_list_lock);
  1410. INIT_LIST_HEAD(&video->video_device_list);
  1411. acpi_video_bus_get_devices(video, device);
  1412. acpi_video_bus_start_devices(video);
  1413. video->input = input = input_allocate_device();
  1414. if (!input) {
  1415. error = -ENOMEM;
  1416. goto err_stop_video;
  1417. }
  1418. snprintf(video->phys, sizeof(video->phys),
  1419. "%s/video/input0", acpi_device_hid(video->device));
  1420. input->name = acpi_device_name(video->device);
  1421. input->phys = video->phys;
  1422. input->id.bustype = BUS_HOST;
  1423. input->id.product = 0x06;
  1424. input->dev.parent = &device->dev;
  1425. input->evbit[0] = BIT(EV_KEY);
  1426. set_bit(KEY_SWITCHVIDEOMODE, input->keybit);
  1427. set_bit(KEY_VIDEO_NEXT, input->keybit);
  1428. set_bit(KEY_VIDEO_PREV, input->keybit);
  1429. set_bit(KEY_BRIGHTNESS_CYCLE, input->keybit);
  1430. set_bit(KEY_BRIGHTNESSUP, input->keybit);
  1431. set_bit(KEY_BRIGHTNESSDOWN, input->keybit);
  1432. set_bit(KEY_BRIGHTNESS_ZERO, input->keybit);
  1433. set_bit(KEY_DISPLAY_OFF, input->keybit);
  1434. error = input_register_device(input);
  1435. if (error)
  1436. goto err_free_input_dev;
  1437. printk(KERN_INFO PREFIX "%s [%s] (multi-head: %s rom: %s post: %s)\n",
  1438. ACPI_VIDEO_DEVICE_NAME, acpi_device_bid(device),
  1439. video->flags.multihead ? "yes" : "no",
  1440. video->flags.rom ? "yes" : "no",
  1441. video->flags.post ? "yes" : "no");
  1442. video->pm_nb.notifier_call = acpi_video_resume;
  1443. video->pm_nb.priority = 0;
  1444. register_pm_notifier(&video->pm_nb);
  1445. return 0;
  1446. err_free_input_dev:
  1447. input_free_device(input);
  1448. err_stop_video:
  1449. acpi_video_bus_stop_devices(video);
  1450. acpi_video_bus_put_devices(video);
  1451. kfree(video->attached_array);
  1452. err_free_video:
  1453. kfree(video);
  1454. device->driver_data = NULL;
  1455. return error;
  1456. }
  1457. static int acpi_video_bus_remove(struct acpi_device *device, int type)
  1458. {
  1459. struct acpi_video_bus *video = NULL;
  1460. if (!device || !acpi_driver_data(device))
  1461. return -EINVAL;
  1462. video = acpi_driver_data(device);
  1463. unregister_pm_notifier(&video->pm_nb);
  1464. acpi_video_bus_stop_devices(video);
  1465. acpi_video_bus_put_devices(video);
  1466. input_unregister_device(video->input);
  1467. kfree(video->attached_array);
  1468. kfree(video);
  1469. return 0;
  1470. }
  1471. static int __init intel_opregion_present(void)
  1472. {
  1473. #if defined(CONFIG_DRM_I915) || defined(CONFIG_DRM_I915_MODULE)
  1474. struct pci_dev *dev = NULL;
  1475. u32 address;
  1476. for_each_pci_dev(dev) {
  1477. if ((dev->class >> 8) != PCI_CLASS_DISPLAY_VGA)
  1478. continue;
  1479. if (dev->vendor != PCI_VENDOR_ID_INTEL)
  1480. continue;
  1481. pci_read_config_dword(dev, 0xfc, &address);
  1482. if (!address)
  1483. continue;
  1484. return 1;
  1485. }
  1486. #endif
  1487. return 0;
  1488. }
  1489. int acpi_video_register(void)
  1490. {
  1491. int result = 0;
  1492. if (register_count) {
  1493. /*
  1494. * if the function of acpi_video_register is already called,
  1495. * don't register the acpi_vide_bus again and return no error.
  1496. */
  1497. return 0;
  1498. }
  1499. result = acpi_bus_register_driver(&acpi_video_bus);
  1500. if (result < 0)
  1501. return -ENODEV;
  1502. /*
  1503. * When the acpi_video_bus is loaded successfully, increase
  1504. * the counter reference.
  1505. */
  1506. register_count = 1;
  1507. return 0;
  1508. }
  1509. EXPORT_SYMBOL(acpi_video_register);
  1510. void acpi_video_unregister(void)
  1511. {
  1512. if (!register_count) {
  1513. /*
  1514. * If the acpi video bus is already unloaded, don't
  1515. * unload it again and return directly.
  1516. */
  1517. return;
  1518. }
  1519. acpi_bus_unregister_driver(&acpi_video_bus);
  1520. register_count = 0;
  1521. return;
  1522. }
  1523. EXPORT_SYMBOL(acpi_video_unregister);
  1524. /*
  1525. * This is kind of nasty. Hardware using Intel chipsets may require
  1526. * the video opregion code to be run first in order to initialise
  1527. * state before any ACPI video calls are made. To handle this we defer
  1528. * registration of the video class until the opregion code has run.
  1529. */
  1530. static int __init acpi_video_init(void)
  1531. {
  1532. dmi_check_system(video_dmi_table);
  1533. if (intel_opregion_present())
  1534. return 0;
  1535. return acpi_video_register();
  1536. }
  1537. static void __exit acpi_video_exit(void)
  1538. {
  1539. acpi_video_unregister();
  1540. return;
  1541. }
  1542. module_init(acpi_video_init);
  1543. module_exit(acpi_video_exit);