acpi_video.c 53 KB

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