acpi_video.c 51 KB

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