video.c 45 KB

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