video.c 51 KB

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