video.c 54 KB

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