video.c 52 KB

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