acpi_video.c 55 KB

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