video.c 55 KB

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