video.c 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530
  1. /*
  2. * video.c - ACPI Video Driver ($Revision:$)
  3. *
  4. * Copyright (C) 2004 Luming Yu <luming.yu@intel.com>
  5. * Copyright (C) 2004 Bruno Ducrot <ducrot@poupinou.org>
  6. * Copyright (C) 2006 Thomas Tuttle <linux-kernel@ttuttle.net>
  7. *
  8. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or (at
  13. * your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful, but
  16. * WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License along
  21. * with this program; if not, write to the Free Software Foundation, Inc.,
  22. * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
  23. *
  24. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  25. */
  26. #include <linux/kernel.h>
  27. #include <linux/module.h>
  28. #include <linux/init.h>
  29. #include <linux/types.h>
  30. #include <linux/list.h>
  31. #include <linux/mutex.h>
  32. #include <linux/proc_fs.h>
  33. #include <linux/seq_file.h>
  34. #include <linux/input.h>
  35. #include <linux/backlight.h>
  36. #include <linux/thermal.h>
  37. #include <linux/video_output.h>
  38. #include <linux/sort.h>
  39. #include <linux/pci.h>
  40. #include <linux/pci_ids.h>
  41. #include <linux/slab.h>
  42. #include <asm/uaccess.h>
  43. #include <linux/dmi.h>
  44. #include <acpi/acpi_bus.h>
  45. #include <acpi/acpi_drivers.h>
  46. #include <linux/suspend.h>
  47. #define PREFIX "ACPI: "
  48. #define ACPI_VIDEO_CLASS "video"
  49. #define ACPI_VIDEO_BUS_NAME "Video Bus"
  50. #define ACPI_VIDEO_DEVICE_NAME "Video Device"
  51. #define ACPI_VIDEO_NOTIFY_SWITCH 0x80
  52. #define ACPI_VIDEO_NOTIFY_PROBE 0x81
  53. #define ACPI_VIDEO_NOTIFY_CYCLE 0x82
  54. #define ACPI_VIDEO_NOTIFY_NEXT_OUTPUT 0x83
  55. #define ACPI_VIDEO_NOTIFY_PREV_OUTPUT 0x84
  56. #define ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS 0x85
  57. #define ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS 0x86
  58. #define ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS 0x87
  59. #define ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS 0x88
  60. #define ACPI_VIDEO_NOTIFY_DISPLAY_OFF 0x89
  61. #define MAX_NAME_LEN 20
  62. #define ACPI_VIDEO_DISPLAY_CRT 1
  63. #define ACPI_VIDEO_DISPLAY_TV 2
  64. #define ACPI_VIDEO_DISPLAY_DVI 3
  65. #define ACPI_VIDEO_DISPLAY_LCD 4
  66. #define _COMPONENT ACPI_VIDEO_COMPONENT
  67. ACPI_MODULE_NAME("video");
  68. MODULE_AUTHOR("Bruno Ducrot");
  69. MODULE_DESCRIPTION("ACPI Video Driver");
  70. MODULE_LICENSE("GPL");
  71. static int brightness_switch_enabled = 1;
  72. module_param(brightness_switch_enabled, bool, 0644);
  73. /*
  74. * By default, we don't allow duplicate ACPI video bus devices
  75. * under the same VGA controller
  76. */
  77. static int allow_duplicates;
  78. module_param(allow_duplicates, bool, 0644);
  79. static int register_count = 0;
  80. static int acpi_video_bus_add(struct acpi_device *device);
  81. static int acpi_video_bus_remove(struct acpi_device *device, int type);
  82. static void acpi_video_bus_notify(struct acpi_device *device, u32 event);
  83. static const struct acpi_device_id video_device_ids[] = {
  84. {ACPI_VIDEO_HID, 0},
  85. {"", 0},
  86. };
  87. MODULE_DEVICE_TABLE(acpi, video_device_ids);
  88. static struct acpi_driver acpi_video_bus = {
  89. .name = "video",
  90. .class = ACPI_VIDEO_CLASS,
  91. .ids = video_device_ids,
  92. .ops = {
  93. .add = acpi_video_bus_add,
  94. .remove = acpi_video_bus_remove,
  95. .notify = acpi_video_bus_notify,
  96. },
  97. };
  98. struct acpi_video_bus_flags {
  99. u8 multihead:1; /* can switch video heads */
  100. u8 rom:1; /* can retrieve a video rom */
  101. u8 post:1; /* can configure the head to */
  102. u8 reserved:5;
  103. };
  104. struct acpi_video_bus_cap {
  105. u8 _DOS:1; /*Enable/Disable output switching */
  106. u8 _DOD:1; /*Enumerate all devices attached to display adapter */
  107. u8 _ROM:1; /*Get ROM Data */
  108. u8 _GPD:1; /*Get POST Device */
  109. u8 _SPD:1; /*Set POST Device */
  110. u8 _VPO:1; /*Video POST Options */
  111. u8 reserved:2;
  112. };
  113. struct acpi_video_device_attrib {
  114. u32 display_index:4; /* A zero-based instance of the Display */
  115. u32 display_port_attachment:4; /*This field differentiates the display type */
  116. u32 display_type:4; /*Describe the specific type in use */
  117. u32 vendor_specific:4; /*Chipset Vendor Specific */
  118. u32 bios_can_detect:1; /*BIOS can detect the device */
  119. u32 depend_on_vga:1; /*Non-VGA output device whose power is related to
  120. the VGA device. */
  121. u32 pipe_id:3; /*For VGA multiple-head devices. */
  122. u32 reserved:10; /*Must be 0 */
  123. u32 device_id_scheme:1; /*Device ID Scheme */
  124. };
  125. struct acpi_video_enumerated_device {
  126. union {
  127. u32 int_val;
  128. struct acpi_video_device_attrib attrib;
  129. } value;
  130. struct acpi_video_device *bind_info;
  131. };
  132. struct acpi_video_bus {
  133. struct acpi_device *device;
  134. u8 dos_setting;
  135. struct acpi_video_enumerated_device *attached_array;
  136. u8 attached_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 proc_dir_entry *dir;
  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 reserved:2;
  154. };
  155. struct acpi_video_device_cap {
  156. u8 _ADR:1; /*Return the unique ID */
  157. u8 _BCL:1; /*Query list of brightness control levels supported */
  158. u8 _BCM:1; /*Set the brightness level */
  159. u8 _BQC:1; /* Get current brightness level */
  160. u8 _BCQ:1; /* Some buggy BIOS uses _BCQ instead of _BQC */
  161. u8 _DDC:1; /*Return the EDID for this device */
  162. u8 _DCS:1; /*Return status of output device */
  163. u8 _DGS:1; /*Query graphics state */
  164. u8 _DSS:1; /*Device state set */
  165. };
  166. struct acpi_video_brightness_flags {
  167. u8 _BCL_no_ac_battery_levels:1; /* no AC/Battery levels in _BCL */
  168. u8 _BCL_reversed:1; /* _BCL package is in a reversed order*/
  169. u8 _BCL_use_index:1; /* levels in _BCL are index values */
  170. u8 _BCM_use_index:1; /* input of _BCM is an index value */
  171. u8 _BQC_use_index:1; /* _BQC returns an index value */
  172. };
  173. struct acpi_video_device_brightness {
  174. int curr;
  175. int count;
  176. int *levels;
  177. struct acpi_video_brightness_flags flags;
  178. };
  179. struct acpi_video_device {
  180. unsigned long device_id;
  181. struct acpi_video_device_flags flags;
  182. struct acpi_video_device_cap cap;
  183. struct list_head entry;
  184. struct acpi_video_bus *video;
  185. struct acpi_device *dev;
  186. struct acpi_video_device_brightness *brightness;
  187. struct backlight_device *backlight;
  188. struct thermal_cooling_device *cooling_dev;
  189. struct output_device *output_dev;
  190. };
  191. /* bus */
  192. static int acpi_video_bus_info_open_fs(struct inode *inode, struct file *file);
  193. static const struct file_operations acpi_video_bus_info_fops = {
  194. .owner = THIS_MODULE,
  195. .open = acpi_video_bus_info_open_fs,
  196. .read = seq_read,
  197. .llseek = seq_lseek,
  198. .release = single_release,
  199. };
  200. static int acpi_video_bus_ROM_open_fs(struct inode *inode, struct file *file);
  201. static const struct file_operations acpi_video_bus_ROM_fops = {
  202. .owner = THIS_MODULE,
  203. .open = acpi_video_bus_ROM_open_fs,
  204. .read = seq_read,
  205. .llseek = seq_lseek,
  206. .release = single_release,
  207. };
  208. static int acpi_video_bus_POST_info_open_fs(struct inode *inode,
  209. struct file *file);
  210. static const struct file_operations acpi_video_bus_POST_info_fops = {
  211. .owner = THIS_MODULE,
  212. .open = acpi_video_bus_POST_info_open_fs,
  213. .read = seq_read,
  214. .llseek = seq_lseek,
  215. .release = single_release,
  216. };
  217. static int acpi_video_bus_POST_open_fs(struct inode *inode, struct file *file);
  218. static ssize_t acpi_video_bus_write_POST(struct file *file,
  219. const char __user *buffer, size_t count, loff_t *data);
  220. static const struct file_operations acpi_video_bus_POST_fops = {
  221. .owner = THIS_MODULE,
  222. .open = acpi_video_bus_POST_open_fs,
  223. .read = seq_read,
  224. .write = acpi_video_bus_write_POST,
  225. .llseek = seq_lseek,
  226. .release = single_release,
  227. };
  228. static int acpi_video_bus_DOS_open_fs(struct inode *inode, struct file *file);
  229. static ssize_t acpi_video_bus_write_DOS(struct file *file,
  230. const char __user *buffer, size_t count, loff_t *data);
  231. static const struct file_operations acpi_video_bus_DOS_fops = {
  232. .owner = THIS_MODULE,
  233. .open = acpi_video_bus_DOS_open_fs,
  234. .read = seq_read,
  235. .write = acpi_video_bus_write_DOS,
  236. .llseek = seq_lseek,
  237. .release = single_release,
  238. };
  239. /* device */
  240. static int acpi_video_device_info_open_fs(struct inode *inode,
  241. struct file *file);
  242. static const struct file_operations acpi_video_device_info_fops = {
  243. .owner = THIS_MODULE,
  244. .open = acpi_video_device_info_open_fs,
  245. .read = seq_read,
  246. .llseek = seq_lseek,
  247. .release = single_release,
  248. };
  249. static int acpi_video_device_state_open_fs(struct inode *inode,
  250. struct file *file);
  251. static ssize_t acpi_video_device_write_state(struct file *file,
  252. const char __user *buffer, size_t count, loff_t *data);
  253. static const struct file_operations acpi_video_device_state_fops = {
  254. .owner = THIS_MODULE,
  255. .open = acpi_video_device_state_open_fs,
  256. .read = seq_read,
  257. .write = acpi_video_device_write_state,
  258. .llseek = seq_lseek,
  259. .release = single_release,
  260. };
  261. static int acpi_video_device_brightness_open_fs(struct inode *inode,
  262. struct file *file);
  263. static ssize_t acpi_video_device_write_brightness(struct file *file,
  264. const char __user *buffer, size_t count, loff_t *data);
  265. static const struct file_operations acpi_video_device_brightness_fops = {
  266. .owner = THIS_MODULE,
  267. .open = acpi_video_device_brightness_open_fs,
  268. .read = seq_read,
  269. .write = acpi_video_device_write_brightness,
  270. .llseek = seq_lseek,
  271. .release = single_release,
  272. };
  273. static int acpi_video_device_EDID_open_fs(struct inode *inode,
  274. struct file *file);
  275. static const struct file_operations acpi_video_device_EDID_fops = {
  276. .owner = THIS_MODULE,
  277. .open = acpi_video_device_EDID_open_fs,
  278. .read = seq_read,
  279. .llseek = seq_lseek,
  280. .release = single_release,
  281. };
  282. static const char device_decode[][30] = {
  283. "motherboard VGA device",
  284. "PCI VGA device",
  285. "AGP VGA device",
  286. "UNKNOWN",
  287. };
  288. static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data);
  289. static void acpi_video_device_rebind(struct acpi_video_bus *video);
  290. static void acpi_video_device_bind(struct acpi_video_bus *video,
  291. struct acpi_video_device *device);
  292. static int acpi_video_device_enumerate(struct acpi_video_bus *video);
  293. static int acpi_video_device_lcd_set_level(struct acpi_video_device *device,
  294. int level);
  295. static int acpi_video_device_lcd_get_level_current(
  296. struct acpi_video_device *device,
  297. unsigned long long *level, int init);
  298. static int acpi_video_get_next_level(struct acpi_video_device *device,
  299. u32 level_current, u32 event);
  300. static int acpi_video_switch_brightness(struct acpi_video_device *device,
  301. int event);
  302. static int acpi_video_device_get_state(struct acpi_video_device *device,
  303. unsigned long long *state);
  304. static int acpi_video_output_get(struct output_device *od);
  305. static int acpi_video_device_set_state(struct acpi_video_device *device, int state);
  306. /*backlight device sysfs support*/
  307. static int acpi_video_get_brightness(struct backlight_device *bd)
  308. {
  309. unsigned long long cur_level;
  310. int i;
  311. struct acpi_video_device *vd =
  312. (struct acpi_video_device *)bl_get_data(bd);
  313. if (acpi_video_device_lcd_get_level_current(vd, &cur_level, 0))
  314. return -EINVAL;
  315. for (i = 2; i < vd->brightness->count; i++) {
  316. if (vd->brightness->levels[i] == cur_level)
  317. /* The first two entries are special - see page 575
  318. of the ACPI spec 3.0 */
  319. return i-2;
  320. }
  321. return 0;
  322. }
  323. static int acpi_video_set_brightness(struct backlight_device *bd)
  324. {
  325. int request_level = bd->props.brightness + 2;
  326. struct acpi_video_device *vd =
  327. (struct acpi_video_device *)bl_get_data(bd);
  328. return acpi_video_device_lcd_set_level(vd,
  329. vd->brightness->levels[request_level]);
  330. }
  331. static struct backlight_ops acpi_backlight_ops = {
  332. .get_brightness = acpi_video_get_brightness,
  333. .update_status = acpi_video_set_brightness,
  334. };
  335. /*video output device sysfs support*/
  336. static int acpi_video_output_get(struct output_device *od)
  337. {
  338. unsigned long long state;
  339. struct acpi_video_device *vd =
  340. (struct acpi_video_device *)dev_get_drvdata(&od->dev);
  341. acpi_video_device_get_state(vd, &state);
  342. return (int)state;
  343. }
  344. static int acpi_video_output_set(struct output_device *od)
  345. {
  346. unsigned long state = od->request_state;
  347. struct acpi_video_device *vd=
  348. (struct acpi_video_device *)dev_get_drvdata(&od->dev);
  349. return acpi_video_device_set_state(vd, state);
  350. }
  351. static struct output_properties acpi_output_properties = {
  352. .set_state = acpi_video_output_set,
  353. .get_status = acpi_video_output_get,
  354. };
  355. /* thermal cooling device callbacks */
  356. static int video_get_max_state(struct thermal_cooling_device *cooling_dev, unsigned
  357. long *state)
  358. {
  359. struct acpi_device *device = cooling_dev->devdata;
  360. struct acpi_video_device *video = acpi_driver_data(device);
  361. *state = video->brightness->count - 3;
  362. return 0;
  363. }
  364. static int video_get_cur_state(struct thermal_cooling_device *cooling_dev, unsigned
  365. long *state)
  366. {
  367. struct acpi_device *device = cooling_dev->devdata;
  368. struct acpi_video_device *video = acpi_driver_data(device);
  369. unsigned long long level;
  370. int offset;
  371. if (acpi_video_device_lcd_get_level_current(video, &level, 0))
  372. return -EINVAL;
  373. for (offset = 2; offset < video->brightness->count; offset++)
  374. if (level == video->brightness->levels[offset]) {
  375. *state = video->brightness->count - offset - 1;
  376. return 0;
  377. }
  378. return -EINVAL;
  379. }
  380. static int
  381. video_set_cur_state(struct thermal_cooling_device *cooling_dev, unsigned long state)
  382. {
  383. struct acpi_device *device = cooling_dev->devdata;
  384. struct acpi_video_device *video = acpi_driver_data(device);
  385. int level;
  386. if ( state >= video->brightness->count - 2)
  387. return -EINVAL;
  388. state = video->brightness->count - state;
  389. level = video->brightness->levels[state -1];
  390. return acpi_video_device_lcd_set_level(video, level);
  391. }
  392. static struct thermal_cooling_device_ops video_cooling_ops = {
  393. .get_max_state = video_get_max_state,
  394. .get_cur_state = video_get_cur_state,
  395. .set_cur_state = video_set_cur_state,
  396. };
  397. /* --------------------------------------------------------------------------
  398. Video Management
  399. -------------------------------------------------------------------------- */
  400. /* device */
  401. static int
  402. acpi_video_device_query(struct acpi_video_device *device, unsigned long long *state)
  403. {
  404. int status;
  405. status = acpi_evaluate_integer(device->dev->handle, "_DGS", NULL, state);
  406. return status;
  407. }
  408. static int
  409. acpi_video_device_get_state(struct acpi_video_device *device,
  410. unsigned long long *state)
  411. {
  412. int status;
  413. status = acpi_evaluate_integer(device->dev->handle, "_DCS", NULL, state);
  414. return status;
  415. }
  416. static int
  417. acpi_video_device_set_state(struct acpi_video_device *device, int state)
  418. {
  419. int status;
  420. union acpi_object arg0 = { ACPI_TYPE_INTEGER };
  421. struct acpi_object_list args = { 1, &arg0 };
  422. unsigned long long ret;
  423. arg0.integer.value = state;
  424. status = acpi_evaluate_integer(device->dev->handle, "_DSS", &args, &ret);
  425. return status;
  426. }
  427. static int
  428. acpi_video_device_lcd_query_levels(struct acpi_video_device *device,
  429. union acpi_object **levels)
  430. {
  431. int status;
  432. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  433. union acpi_object *obj;
  434. *levels = NULL;
  435. status = acpi_evaluate_object(device->dev->handle, "_BCL", NULL, &buffer);
  436. if (!ACPI_SUCCESS(status))
  437. return status;
  438. obj = (union acpi_object *)buffer.pointer;
  439. if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
  440. printk(KERN_ERR PREFIX "Invalid _BCL data\n");
  441. status = -EFAULT;
  442. goto err;
  443. }
  444. *levels = obj;
  445. return 0;
  446. err:
  447. kfree(buffer.pointer);
  448. return status;
  449. }
  450. static int
  451. acpi_video_device_lcd_set_level(struct acpi_video_device *device, int level)
  452. {
  453. int status;
  454. union acpi_object arg0 = { ACPI_TYPE_INTEGER };
  455. struct acpi_object_list args = { 1, &arg0 };
  456. int state;
  457. arg0.integer.value = level;
  458. status = acpi_evaluate_object(device->dev->handle, "_BCM",
  459. &args, NULL);
  460. if (ACPI_FAILURE(status)) {
  461. ACPI_ERROR((AE_INFO, "Evaluating _BCM failed"));
  462. return -EIO;
  463. }
  464. device->brightness->curr = level;
  465. for (state = 2; state < device->brightness->count; state++)
  466. if (level == device->brightness->levels[state]) {
  467. if (device->backlight)
  468. device->backlight->props.brightness = state - 2;
  469. return 0;
  470. }
  471. ACPI_ERROR((AE_INFO, "Current brightness invalid"));
  472. return -EINVAL;
  473. }
  474. /*
  475. * For some buggy _BQC methods, we need to add a constant value to
  476. * the _BQC return value to get the actual current brightness level
  477. */
  478. static int bqc_offset_aml_bug_workaround;
  479. static int __init video_set_bqc_offset(const struct dmi_system_id *d)
  480. {
  481. bqc_offset_aml_bug_workaround = 9;
  482. return 0;
  483. }
  484. static struct dmi_system_id video_dmi_table[] __initdata = {
  485. /*
  486. * Broken _BQC workaround http://bugzilla.kernel.org/show_bug.cgi?id=13121
  487. */
  488. {
  489. .callback = video_set_bqc_offset,
  490. .ident = "Acer Aspire 5720",
  491. .matches = {
  492. DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
  493. DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5720"),
  494. },
  495. },
  496. {
  497. .callback = video_set_bqc_offset,
  498. .ident = "Acer Aspire 5710Z",
  499. .matches = {
  500. DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
  501. DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5710Z"),
  502. },
  503. },
  504. {
  505. .callback = video_set_bqc_offset,
  506. .ident = "eMachines E510",
  507. .matches = {
  508. DMI_MATCH(DMI_BOARD_VENDOR, "EMACHINES"),
  509. DMI_MATCH(DMI_PRODUCT_NAME, "eMachines E510"),
  510. },
  511. },
  512. {
  513. .callback = video_set_bqc_offset,
  514. .ident = "Acer Aspire 5315",
  515. .matches = {
  516. DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
  517. DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5315"),
  518. },
  519. },
  520. {
  521. .callback = video_set_bqc_offset,
  522. .ident = "Acer Aspire 7720",
  523. .matches = {
  524. DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
  525. DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 7720"),
  526. },
  527. },
  528. {}
  529. };
  530. static int
  531. acpi_video_device_lcd_get_level_current(struct acpi_video_device *device,
  532. unsigned long long *level, int init)
  533. {
  534. acpi_status status = AE_OK;
  535. int i;
  536. if (device->cap._BQC || device->cap._BCQ) {
  537. char *buf = device->cap._BQC ? "_BQC" : "_BCQ";
  538. status = acpi_evaluate_integer(device->dev->handle, buf,
  539. NULL, level);
  540. if (ACPI_SUCCESS(status)) {
  541. if (device->brightness->flags._BQC_use_index) {
  542. if (device->brightness->flags._BCL_reversed)
  543. *level = device->brightness->count
  544. - 3 - (*level);
  545. *level = device->brightness->levels[*level + 2];
  546. }
  547. *level += bqc_offset_aml_bug_workaround;
  548. for (i = 2; i < device->brightness->count; i++)
  549. if (device->brightness->levels[i] == *level) {
  550. device->brightness->curr = *level;
  551. return 0;
  552. }
  553. if (!init) {
  554. /*
  555. * BQC returned an invalid level.
  556. * Stop using it.
  557. */
  558. ACPI_WARNING((AE_INFO,
  559. "%s returned an invalid level",
  560. buf));
  561. device->cap._BQC = device->cap._BCQ = 0;
  562. }
  563. } else {
  564. /* Fixme:
  565. * should we return an error or ignore this failure?
  566. * dev->brightness->curr is a cached value which stores
  567. * the correct current backlight level in most cases.
  568. * ACPI video backlight still works w/ buggy _BQC.
  569. * http://bugzilla.kernel.org/show_bug.cgi?id=12233
  570. */
  571. ACPI_WARNING((AE_INFO, "Evaluating %s failed", buf));
  572. device->cap._BQC = device->cap._BCQ = 0;
  573. }
  574. }
  575. *level = device->brightness->curr;
  576. return 0;
  577. }
  578. static int
  579. acpi_video_device_EDID(struct acpi_video_device *device,
  580. union acpi_object **edid, ssize_t length)
  581. {
  582. int status;
  583. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  584. union acpi_object *obj;
  585. union acpi_object arg0 = { ACPI_TYPE_INTEGER };
  586. struct acpi_object_list args = { 1, &arg0 };
  587. *edid = NULL;
  588. if (!device)
  589. return -ENODEV;
  590. if (length == 128)
  591. arg0.integer.value = 1;
  592. else if (length == 256)
  593. arg0.integer.value = 2;
  594. else
  595. return -EINVAL;
  596. status = acpi_evaluate_object(device->dev->handle, "_DDC", &args, &buffer);
  597. if (ACPI_FAILURE(status))
  598. return -ENODEV;
  599. obj = buffer.pointer;
  600. if (obj && obj->type == ACPI_TYPE_BUFFER)
  601. *edid = obj;
  602. else {
  603. printk(KERN_ERR PREFIX "Invalid _DDC data\n");
  604. status = -EFAULT;
  605. kfree(obj);
  606. }
  607. return status;
  608. }
  609. /* bus */
  610. static int
  611. acpi_video_bus_set_POST(struct acpi_video_bus *video, unsigned long option)
  612. {
  613. int status;
  614. unsigned long long tmp;
  615. union acpi_object arg0 = { ACPI_TYPE_INTEGER };
  616. struct acpi_object_list args = { 1, &arg0 };
  617. arg0.integer.value = option;
  618. status = acpi_evaluate_integer(video->device->handle, "_SPD", &args, &tmp);
  619. if (ACPI_SUCCESS(status))
  620. status = tmp ? (-EINVAL) : (AE_OK);
  621. return status;
  622. }
  623. static int
  624. acpi_video_bus_get_POST(struct acpi_video_bus *video, unsigned long long *id)
  625. {
  626. int status;
  627. status = acpi_evaluate_integer(video->device->handle, "_GPD", NULL, id);
  628. return status;
  629. }
  630. static int
  631. acpi_video_bus_POST_options(struct acpi_video_bus *video,
  632. unsigned long long *options)
  633. {
  634. int status;
  635. status = acpi_evaluate_integer(video->device->handle, "_VPO", NULL, options);
  636. *options &= 3;
  637. return status;
  638. }
  639. /*
  640. * Arg:
  641. * video : video bus device pointer
  642. * bios_flag :
  643. * 0. The system BIOS should NOT automatically switch(toggle)
  644. * the active display output.
  645. * 1. The system BIOS should automatically switch (toggle) the
  646. * active display output. No switch event.
  647. * 2. The _DGS value should be locked.
  648. * 3. The system BIOS should not automatically switch (toggle) the
  649. * active display output, but instead generate the display switch
  650. * event notify code.
  651. * lcd_flag :
  652. * 0. The system BIOS should automatically control the brightness level
  653. * of the LCD when the power changes from AC to DC
  654. * 1. The system BIOS should NOT automatically control the brightness
  655. * level of the LCD when the power changes from AC to DC.
  656. * Return Value:
  657. * -1 wrong arg.
  658. */
  659. static int
  660. acpi_video_bus_DOS(struct acpi_video_bus *video, int bios_flag, int lcd_flag)
  661. {
  662. u64 status = 0;
  663. union acpi_object arg0 = { ACPI_TYPE_INTEGER };
  664. struct acpi_object_list args = { 1, &arg0 };
  665. if (bios_flag < 0 || bios_flag > 3 || lcd_flag < 0 || lcd_flag > 1) {
  666. status = -1;
  667. goto Failed;
  668. }
  669. arg0.integer.value = (lcd_flag << 2) | bios_flag;
  670. video->dos_setting = arg0.integer.value;
  671. acpi_evaluate_object(video->device->handle, "_DOS", &args, NULL);
  672. Failed:
  673. return status;
  674. }
  675. /*
  676. * Simple comparison function used to sort backlight levels.
  677. */
  678. static int
  679. acpi_video_cmp_level(const void *a, const void *b)
  680. {
  681. return *(int *)a - *(int *)b;
  682. }
  683. /*
  684. * Arg:
  685. * device : video output device (LCD, CRT, ..)
  686. *
  687. * Return Value:
  688. * Maximum brightness level
  689. *
  690. * Allocate and initialize device->brightness.
  691. */
  692. static int
  693. acpi_video_init_brightness(struct acpi_video_device *device)
  694. {
  695. union acpi_object *obj = NULL;
  696. int i, max_level = 0, count = 0, level_ac_battery = 0;
  697. unsigned long long level, level_old;
  698. union acpi_object *o;
  699. struct acpi_video_device_brightness *br = NULL;
  700. int result = -EINVAL;
  701. if (!ACPI_SUCCESS(acpi_video_device_lcd_query_levels(device, &obj))) {
  702. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Could not query available "
  703. "LCD brightness level\n"));
  704. goto out;
  705. }
  706. if (obj->package.count < 2)
  707. goto out;
  708. br = kzalloc(sizeof(*br), GFP_KERNEL);
  709. if (!br) {
  710. printk(KERN_ERR "can't allocate memory\n");
  711. result = -ENOMEM;
  712. goto out;
  713. }
  714. br->levels = kmalloc((obj->package.count + 2) * sizeof *(br->levels),
  715. GFP_KERNEL);
  716. if (!br->levels) {
  717. result = -ENOMEM;
  718. goto out_free;
  719. }
  720. for (i = 0; i < obj->package.count; i++) {
  721. o = (union acpi_object *)&obj->package.elements[i];
  722. if (o->type != ACPI_TYPE_INTEGER) {
  723. printk(KERN_ERR PREFIX "Invalid data\n");
  724. continue;
  725. }
  726. br->levels[count] = (u32) o->integer.value;
  727. if (br->levels[count] > max_level)
  728. max_level = br->levels[count];
  729. count++;
  730. }
  731. /*
  732. * some buggy BIOS don't export the levels
  733. * when machine is on AC/Battery in _BCL package.
  734. * In this case, the first two elements in _BCL packages
  735. * are also supported brightness levels that OS should take care of.
  736. */
  737. for (i = 2; i < count; i++) {
  738. if (br->levels[i] == br->levels[0])
  739. level_ac_battery++;
  740. if (br->levels[i] == br->levels[1])
  741. level_ac_battery++;
  742. }
  743. if (level_ac_battery < 2) {
  744. level_ac_battery = 2 - level_ac_battery;
  745. br->flags._BCL_no_ac_battery_levels = 1;
  746. for (i = (count - 1 + level_ac_battery); i >= 2; i--)
  747. br->levels[i] = br->levels[i - level_ac_battery];
  748. count += level_ac_battery;
  749. } else if (level_ac_battery > 2)
  750. ACPI_ERROR((AE_INFO, "Too many duplicates in _BCL package\n"));
  751. /* Check if the _BCL package is in a reversed order */
  752. if (max_level == br->levels[2]) {
  753. br->flags._BCL_reversed = 1;
  754. sort(&br->levels[2], count - 2, sizeof(br->levels[2]),
  755. acpi_video_cmp_level, NULL);
  756. } else if (max_level != br->levels[count - 1])
  757. ACPI_ERROR((AE_INFO,
  758. "Found unordered _BCL package\n"));
  759. br->count = count;
  760. device->brightness = br;
  761. /* Check the input/output of _BQC/_BCL/_BCM */
  762. if ((max_level < 100) && (max_level <= (count - 2)))
  763. br->flags._BCL_use_index = 1;
  764. /*
  765. * _BCM is always consistent with _BCL,
  766. * at least for all the laptops we have ever seen.
  767. */
  768. br->flags._BCM_use_index = br->flags._BCL_use_index;
  769. /* _BQC uses INDEX while _BCL uses VALUE in some laptops */
  770. br->curr = level = max_level;
  771. if (!device->cap._BQC)
  772. goto set_level;
  773. result = acpi_video_device_lcd_get_level_current(device, &level_old, 1);
  774. if (result)
  775. goto out_free_levels;
  776. /*
  777. * Set the level to maximum and check if _BQC uses indexed value
  778. */
  779. result = acpi_video_device_lcd_set_level(device, max_level);
  780. if (result)
  781. goto out_free_levels;
  782. result = acpi_video_device_lcd_get_level_current(device, &level, 0);
  783. if (result)
  784. goto out_free_levels;
  785. br->flags._BQC_use_index = (level == max_level ? 0 : 1);
  786. if (!br->flags._BQC_use_index) {
  787. /*
  788. * Set the backlight to the initial state.
  789. * On some buggy laptops, _BQC returns an uninitialized value
  790. * when invoked for the first time, i.e. level_old is invalid.
  791. * set the backlight to max_level in this case
  792. */
  793. for (i = 2; i < br->count; i++)
  794. if (level_old == br->levels[i])
  795. level = level_old;
  796. goto set_level;
  797. }
  798. if (br->flags._BCL_reversed)
  799. level_old = (br->count - 1) - level_old;
  800. level = br->levels[level_old];
  801. set_level:
  802. result = acpi_video_device_lcd_set_level(device, level);
  803. if (result)
  804. goto out_free_levels;
  805. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  806. "found %d brightness levels\n", count - 2));
  807. kfree(obj);
  808. return result;
  809. out_free_levels:
  810. kfree(br->levels);
  811. out_free:
  812. kfree(br);
  813. out:
  814. device->brightness = NULL;
  815. kfree(obj);
  816. return result;
  817. }
  818. /*
  819. * Arg:
  820. * device : video output device (LCD, CRT, ..)
  821. *
  822. * Return Value:
  823. * None
  824. *
  825. * Find out all required AML methods defined under the output
  826. * device.
  827. */
  828. static void acpi_video_device_find_cap(struct acpi_video_device *device)
  829. {
  830. acpi_handle h_dummy1;
  831. if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_ADR", &h_dummy1))) {
  832. device->cap._ADR = 1;
  833. }
  834. if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_BCL", &h_dummy1))) {
  835. device->cap._BCL = 1;
  836. }
  837. if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_BCM", &h_dummy1))) {
  838. device->cap._BCM = 1;
  839. }
  840. if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle,"_BQC",&h_dummy1)))
  841. device->cap._BQC = 1;
  842. else if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_BCQ",
  843. &h_dummy1))) {
  844. printk(KERN_WARNING FW_BUG "_BCQ is used instead of _BQC\n");
  845. device->cap._BCQ = 1;
  846. }
  847. if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DDC", &h_dummy1))) {
  848. device->cap._DDC = 1;
  849. }
  850. if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DCS", &h_dummy1))) {
  851. device->cap._DCS = 1;
  852. }
  853. if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DGS", &h_dummy1))) {
  854. device->cap._DGS = 1;
  855. }
  856. if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DSS", &h_dummy1))) {
  857. device->cap._DSS = 1;
  858. }
  859. if (acpi_video_backlight_support()) {
  860. struct backlight_properties props;
  861. int result;
  862. static int count = 0;
  863. char *name;
  864. result = acpi_video_init_brightness(device);
  865. if (result)
  866. return;
  867. name = kzalloc(MAX_NAME_LEN, GFP_KERNEL);
  868. if (!name)
  869. return;
  870. sprintf(name, "acpi_video%d", count++);
  871. memset(&props, 0, sizeof(struct backlight_properties));
  872. props.max_brightness = device->brightness->count - 3;
  873. device->backlight = backlight_device_register(name, NULL, device,
  874. &acpi_backlight_ops,
  875. &props);
  876. kfree(name);
  877. if (IS_ERR(device->backlight))
  878. return;
  879. /*
  880. * Save current brightness level in case we have to restore it
  881. * before acpi_video_device_lcd_set_level() is called next time.
  882. */
  883. device->backlight->props.brightness =
  884. acpi_video_get_brightness(device->backlight);
  885. result = sysfs_create_link(&device->backlight->dev.kobj,
  886. &device->dev->dev.kobj, "device");
  887. if (result)
  888. printk(KERN_ERR PREFIX "Create sysfs link\n");
  889. device->cooling_dev = thermal_cooling_device_register("LCD",
  890. device->dev, &video_cooling_ops);
  891. if (IS_ERR(device->cooling_dev)) {
  892. /*
  893. * Set cooling_dev to NULL so we don't crash trying to
  894. * free it.
  895. * Also, why the hell we are returning early and
  896. * not attempt to register video output if cooling
  897. * device registration failed?
  898. * -- dtor
  899. */
  900. device->cooling_dev = NULL;
  901. return;
  902. }
  903. dev_info(&device->dev->dev, "registered as cooling_device%d\n",
  904. device->cooling_dev->id);
  905. result = sysfs_create_link(&device->dev->dev.kobj,
  906. &device->cooling_dev->device.kobj,
  907. "thermal_cooling");
  908. if (result)
  909. printk(KERN_ERR PREFIX "Create sysfs link\n");
  910. result = sysfs_create_link(&device->cooling_dev->device.kobj,
  911. &device->dev->dev.kobj, "device");
  912. if (result)
  913. printk(KERN_ERR PREFIX "Create sysfs link\n");
  914. }
  915. if (acpi_video_display_switch_support()) {
  916. if (device->cap._DCS && device->cap._DSS) {
  917. static int count;
  918. char *name;
  919. name = kzalloc(MAX_NAME_LEN, GFP_KERNEL);
  920. if (!name)
  921. return;
  922. sprintf(name, "acpi_video%d", count++);
  923. device->output_dev = video_output_register(name,
  924. NULL, device, &acpi_output_properties);
  925. kfree(name);
  926. }
  927. }
  928. }
  929. /*
  930. * Arg:
  931. * device : video output device (VGA)
  932. *
  933. * Return Value:
  934. * None
  935. *
  936. * Find out all required AML methods defined under the video bus device.
  937. */
  938. static void acpi_video_bus_find_cap(struct acpi_video_bus *video)
  939. {
  940. acpi_handle h_dummy1;
  941. if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_DOS", &h_dummy1))) {
  942. video->cap._DOS = 1;
  943. }
  944. if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_DOD", &h_dummy1))) {
  945. video->cap._DOD = 1;
  946. }
  947. if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_ROM", &h_dummy1))) {
  948. video->cap._ROM = 1;
  949. }
  950. if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_GPD", &h_dummy1))) {
  951. video->cap._GPD = 1;
  952. }
  953. if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_SPD", &h_dummy1))) {
  954. video->cap._SPD = 1;
  955. }
  956. if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_VPO", &h_dummy1))) {
  957. video->cap._VPO = 1;
  958. }
  959. }
  960. /*
  961. * Check whether the video bus device has required AML method to
  962. * support the desired features
  963. */
  964. static int acpi_video_bus_check(struct acpi_video_bus *video)
  965. {
  966. acpi_status status = -ENOENT;
  967. struct pci_dev *dev;
  968. if (!video)
  969. return -EINVAL;
  970. dev = acpi_get_pci_dev(video->device->handle);
  971. if (!dev)
  972. return -ENODEV;
  973. pci_dev_put(dev);
  974. /* Since there is no HID, CID and so on for VGA driver, we have
  975. * to check well known required nodes.
  976. */
  977. /* Does this device support video switching? */
  978. if (video->cap._DOS || video->cap._DOD) {
  979. if (!video->cap._DOS) {
  980. printk(KERN_WARNING FW_BUG
  981. "ACPI(%s) defines _DOD but not _DOS\n",
  982. acpi_device_bid(video->device));
  983. }
  984. video->flags.multihead = 1;
  985. status = 0;
  986. }
  987. /* Does this device support retrieving a video ROM? */
  988. if (video->cap._ROM) {
  989. video->flags.rom = 1;
  990. status = 0;
  991. }
  992. /* Does this device support configuring which video device to POST? */
  993. if (video->cap._GPD && video->cap._SPD && video->cap._VPO) {
  994. video->flags.post = 1;
  995. status = 0;
  996. }
  997. return status;
  998. }
  999. /* --------------------------------------------------------------------------
  1000. FS Interface (/proc)
  1001. -------------------------------------------------------------------------- */
  1002. static struct proc_dir_entry *acpi_video_dir;
  1003. /* video devices */
  1004. static int acpi_video_device_info_seq_show(struct seq_file *seq, void *offset)
  1005. {
  1006. struct acpi_video_device *dev = seq->private;
  1007. if (!dev)
  1008. goto end;
  1009. seq_printf(seq, "device_id: 0x%04x\n", (u32) dev->device_id);
  1010. seq_printf(seq, "type: ");
  1011. if (dev->flags.crt)
  1012. seq_printf(seq, "CRT\n");
  1013. else if (dev->flags.lcd)
  1014. seq_printf(seq, "LCD\n");
  1015. else if (dev->flags.tvout)
  1016. seq_printf(seq, "TVOUT\n");
  1017. else if (dev->flags.dvi)
  1018. seq_printf(seq, "DVI\n");
  1019. else
  1020. seq_printf(seq, "UNKNOWN\n");
  1021. seq_printf(seq, "known by bios: %s\n", dev->flags.bios ? "yes" : "no");
  1022. end:
  1023. return 0;
  1024. }
  1025. static int
  1026. acpi_video_device_info_open_fs(struct inode *inode, struct file *file)
  1027. {
  1028. return single_open(file, acpi_video_device_info_seq_show,
  1029. PDE(inode)->data);
  1030. }
  1031. static int acpi_video_device_state_seq_show(struct seq_file *seq, void *offset)
  1032. {
  1033. int status;
  1034. struct acpi_video_device *dev = seq->private;
  1035. unsigned long long state;
  1036. if (!dev)
  1037. goto end;
  1038. status = acpi_video_device_get_state(dev, &state);
  1039. seq_printf(seq, "state: ");
  1040. if (ACPI_SUCCESS(status))
  1041. seq_printf(seq, "0x%02llx\n", state);
  1042. else
  1043. seq_printf(seq, "<not supported>\n");
  1044. status = acpi_video_device_query(dev, &state);
  1045. seq_printf(seq, "query: ");
  1046. if (ACPI_SUCCESS(status))
  1047. seq_printf(seq, "0x%02llx\n", state);
  1048. else
  1049. seq_printf(seq, "<not supported>\n");
  1050. end:
  1051. return 0;
  1052. }
  1053. static int
  1054. acpi_video_device_state_open_fs(struct inode *inode, struct file *file)
  1055. {
  1056. return single_open(file, acpi_video_device_state_seq_show,
  1057. PDE(inode)->data);
  1058. }
  1059. static ssize_t
  1060. acpi_video_device_write_state(struct file *file,
  1061. const char __user * buffer,
  1062. size_t count, loff_t * data)
  1063. {
  1064. int status;
  1065. struct seq_file *m = file->private_data;
  1066. struct acpi_video_device *dev = m->private;
  1067. char str[12] = { 0 };
  1068. u32 state = 0;
  1069. if (!dev || count >= sizeof(str))
  1070. return -EINVAL;
  1071. if (copy_from_user(str, buffer, count))
  1072. return -EFAULT;
  1073. str[count] = 0;
  1074. state = simple_strtoul(str, NULL, 0);
  1075. state &= ((1ul << 31) | (1ul << 30) | (1ul << 0));
  1076. status = acpi_video_device_set_state(dev, state);
  1077. if (status)
  1078. return -EFAULT;
  1079. return count;
  1080. }
  1081. static int
  1082. acpi_video_device_brightness_seq_show(struct seq_file *seq, void *offset)
  1083. {
  1084. struct acpi_video_device *dev = seq->private;
  1085. int i;
  1086. if (!dev || !dev->brightness) {
  1087. seq_printf(seq, "<not supported>\n");
  1088. return 0;
  1089. }
  1090. seq_printf(seq, "levels: ");
  1091. for (i = 2; i < dev->brightness->count; i++)
  1092. seq_printf(seq, " %d", dev->brightness->levels[i]);
  1093. seq_printf(seq, "\ncurrent: %d\n", dev->brightness->curr);
  1094. return 0;
  1095. }
  1096. static int
  1097. acpi_video_device_brightness_open_fs(struct inode *inode, struct file *file)
  1098. {
  1099. return single_open(file, acpi_video_device_brightness_seq_show,
  1100. PDE(inode)->data);
  1101. }
  1102. static ssize_t
  1103. acpi_video_device_write_brightness(struct file *file,
  1104. const char __user * buffer,
  1105. size_t count, loff_t * data)
  1106. {
  1107. struct seq_file *m = file->private_data;
  1108. struct acpi_video_device *dev = m->private;
  1109. char str[5] = { 0 };
  1110. unsigned int level = 0;
  1111. int i;
  1112. if (!dev || !dev->brightness || count >= sizeof(str))
  1113. return -EINVAL;
  1114. if (copy_from_user(str, buffer, count))
  1115. return -EFAULT;
  1116. str[count] = 0;
  1117. level = simple_strtoul(str, NULL, 0);
  1118. if (level > 100)
  1119. return -EFAULT;
  1120. /* validate through the list of available levels */
  1121. for (i = 2; i < dev->brightness->count; i++)
  1122. if (level == dev->brightness->levels[i]) {
  1123. if (!acpi_video_device_lcd_set_level(dev, level))
  1124. return count;
  1125. break;
  1126. }
  1127. return -EINVAL;
  1128. }
  1129. static int acpi_video_device_EDID_seq_show(struct seq_file *seq, void *offset)
  1130. {
  1131. struct acpi_video_device *dev = seq->private;
  1132. int status;
  1133. int i;
  1134. union acpi_object *edid = NULL;
  1135. if (!dev)
  1136. goto out;
  1137. status = acpi_video_device_EDID(dev, &edid, 128);
  1138. if (ACPI_FAILURE(status)) {
  1139. status = acpi_video_device_EDID(dev, &edid, 256);
  1140. }
  1141. if (ACPI_FAILURE(status)) {
  1142. goto out;
  1143. }
  1144. if (edid && edid->type == ACPI_TYPE_BUFFER) {
  1145. for (i = 0; i < edid->buffer.length; i++)
  1146. seq_putc(seq, edid->buffer.pointer[i]);
  1147. }
  1148. out:
  1149. if (!edid)
  1150. seq_printf(seq, "<not supported>\n");
  1151. else
  1152. kfree(edid);
  1153. return 0;
  1154. }
  1155. static int
  1156. acpi_video_device_EDID_open_fs(struct inode *inode, struct file *file)
  1157. {
  1158. return single_open(file, acpi_video_device_EDID_seq_show,
  1159. PDE(inode)->data);
  1160. }
  1161. static int acpi_video_device_add_fs(struct acpi_device *device)
  1162. {
  1163. struct proc_dir_entry *entry, *device_dir;
  1164. struct acpi_video_device *vid_dev;
  1165. vid_dev = acpi_driver_data(device);
  1166. if (!vid_dev)
  1167. return -ENODEV;
  1168. device_dir = proc_mkdir(acpi_device_bid(device),
  1169. vid_dev->video->dir);
  1170. if (!device_dir)
  1171. return -ENOMEM;
  1172. /* 'info' [R] */
  1173. entry = proc_create_data("info", S_IRUGO, device_dir,
  1174. &acpi_video_device_info_fops, acpi_driver_data(device));
  1175. if (!entry)
  1176. goto err_remove_dir;
  1177. /* 'state' [R/W] */
  1178. entry = proc_create_data("state", S_IFREG | S_IRUGO | S_IWUSR,
  1179. device_dir,
  1180. &acpi_video_device_state_fops,
  1181. acpi_driver_data(device));
  1182. if (!entry)
  1183. goto err_remove_info;
  1184. /* 'brightness' [R/W] */
  1185. entry = proc_create_data("brightness", S_IFREG | S_IRUGO | S_IWUSR,
  1186. device_dir,
  1187. &acpi_video_device_brightness_fops,
  1188. acpi_driver_data(device));
  1189. if (!entry)
  1190. goto err_remove_state;
  1191. /* 'EDID' [R] */
  1192. entry = proc_create_data("EDID", S_IRUGO, device_dir,
  1193. &acpi_video_device_EDID_fops,
  1194. acpi_driver_data(device));
  1195. if (!entry)
  1196. goto err_remove_brightness;
  1197. acpi_device_dir(device) = device_dir;
  1198. return 0;
  1199. err_remove_brightness:
  1200. remove_proc_entry("brightness", device_dir);
  1201. err_remove_state:
  1202. remove_proc_entry("state", device_dir);
  1203. err_remove_info:
  1204. remove_proc_entry("info", device_dir);
  1205. err_remove_dir:
  1206. remove_proc_entry(acpi_device_bid(device), vid_dev->video->dir);
  1207. return -ENOMEM;
  1208. }
  1209. static int acpi_video_device_remove_fs(struct acpi_device *device)
  1210. {
  1211. struct acpi_video_device *vid_dev;
  1212. struct proc_dir_entry *device_dir;
  1213. vid_dev = acpi_driver_data(device);
  1214. if (!vid_dev || !vid_dev->video || !vid_dev->video->dir)
  1215. return -ENODEV;
  1216. device_dir = acpi_device_dir(device);
  1217. if (device_dir) {
  1218. remove_proc_entry("info", device_dir);
  1219. remove_proc_entry("state", device_dir);
  1220. remove_proc_entry("brightness", device_dir);
  1221. remove_proc_entry("EDID", device_dir);
  1222. remove_proc_entry(acpi_device_bid(device), vid_dev->video->dir);
  1223. acpi_device_dir(device) = NULL;
  1224. }
  1225. return 0;
  1226. }
  1227. /* video bus */
  1228. static int acpi_video_bus_info_seq_show(struct seq_file *seq, void *offset)
  1229. {
  1230. struct acpi_video_bus *video = seq->private;
  1231. if (!video)
  1232. goto end;
  1233. seq_printf(seq, "Switching heads: %s\n",
  1234. video->flags.multihead ? "yes" : "no");
  1235. seq_printf(seq, "Video ROM: %s\n",
  1236. video->flags.rom ? "yes" : "no");
  1237. seq_printf(seq, "Device to be POSTed on boot: %s\n",
  1238. video->flags.post ? "yes" : "no");
  1239. end:
  1240. return 0;
  1241. }
  1242. static int acpi_video_bus_info_open_fs(struct inode *inode, struct file *file)
  1243. {
  1244. return single_open(file, acpi_video_bus_info_seq_show,
  1245. PDE(inode)->data);
  1246. }
  1247. static int acpi_video_bus_ROM_seq_show(struct seq_file *seq, void *offset)
  1248. {
  1249. struct acpi_video_bus *video = seq->private;
  1250. if (!video)
  1251. goto end;
  1252. printk(KERN_INFO PREFIX "Please implement %s\n", __func__);
  1253. seq_printf(seq, "<TODO>\n");
  1254. end:
  1255. return 0;
  1256. }
  1257. static int acpi_video_bus_ROM_open_fs(struct inode *inode, struct file *file)
  1258. {
  1259. return single_open(file, acpi_video_bus_ROM_seq_show, PDE(inode)->data);
  1260. }
  1261. static int acpi_video_bus_POST_info_seq_show(struct seq_file *seq, void *offset)
  1262. {
  1263. struct acpi_video_bus *video = seq->private;
  1264. unsigned long long options;
  1265. int status;
  1266. if (!video)
  1267. goto end;
  1268. status = acpi_video_bus_POST_options(video, &options);
  1269. if (ACPI_SUCCESS(status)) {
  1270. if (!(options & 1)) {
  1271. printk(KERN_WARNING PREFIX
  1272. "The motherboard VGA device is not listed as a possible POST device.\n");
  1273. printk(KERN_WARNING PREFIX
  1274. "This indicates a BIOS bug. Please contact the manufacturer.\n");
  1275. }
  1276. printk(KERN_WARNING "%llx\n", options);
  1277. seq_printf(seq, "can POST: <integrated video>");
  1278. if (options & 2)
  1279. seq_printf(seq, " <PCI video>");
  1280. if (options & 4)
  1281. seq_printf(seq, " <AGP video>");
  1282. seq_putc(seq, '\n');
  1283. } else
  1284. seq_printf(seq, "<not supported>\n");
  1285. end:
  1286. return 0;
  1287. }
  1288. static int
  1289. acpi_video_bus_POST_info_open_fs(struct inode *inode, struct file *file)
  1290. {
  1291. return single_open(file, acpi_video_bus_POST_info_seq_show,
  1292. PDE(inode)->data);
  1293. }
  1294. static int acpi_video_bus_POST_seq_show(struct seq_file *seq, void *offset)
  1295. {
  1296. struct acpi_video_bus *video = seq->private;
  1297. int status;
  1298. unsigned long long id;
  1299. if (!video)
  1300. goto end;
  1301. status = acpi_video_bus_get_POST(video, &id);
  1302. if (!ACPI_SUCCESS(status)) {
  1303. seq_printf(seq, "<not supported>\n");
  1304. goto end;
  1305. }
  1306. seq_printf(seq, "device POSTed is <%s>\n", device_decode[id & 3]);
  1307. end:
  1308. return 0;
  1309. }
  1310. static int acpi_video_bus_DOS_seq_show(struct seq_file *seq, void *offset)
  1311. {
  1312. struct acpi_video_bus *video = seq->private;
  1313. seq_printf(seq, "DOS setting: <%d>\n", video->dos_setting);
  1314. return 0;
  1315. }
  1316. static int acpi_video_bus_POST_open_fs(struct inode *inode, struct file *file)
  1317. {
  1318. return single_open(file, acpi_video_bus_POST_seq_show,
  1319. PDE(inode)->data);
  1320. }
  1321. static int acpi_video_bus_DOS_open_fs(struct inode *inode, struct file *file)
  1322. {
  1323. return single_open(file, acpi_video_bus_DOS_seq_show, PDE(inode)->data);
  1324. }
  1325. static ssize_t
  1326. acpi_video_bus_write_POST(struct file *file,
  1327. const char __user * buffer,
  1328. size_t count, loff_t * data)
  1329. {
  1330. int status;
  1331. struct seq_file *m = file->private_data;
  1332. struct acpi_video_bus *video = m->private;
  1333. char str[12] = { 0 };
  1334. unsigned long long opt, options;
  1335. if (!video || count >= sizeof(str))
  1336. return -EINVAL;
  1337. status = acpi_video_bus_POST_options(video, &options);
  1338. if (!ACPI_SUCCESS(status))
  1339. return -EINVAL;
  1340. if (copy_from_user(str, buffer, count))
  1341. return -EFAULT;
  1342. str[count] = 0;
  1343. opt = strtoul(str, NULL, 0);
  1344. if (opt > 3)
  1345. return -EFAULT;
  1346. /* just in case an OEM 'forgot' the motherboard... */
  1347. options |= 1;
  1348. if (options & (1ul << opt)) {
  1349. status = acpi_video_bus_set_POST(video, opt);
  1350. if (!ACPI_SUCCESS(status))
  1351. return -EFAULT;
  1352. }
  1353. return count;
  1354. }
  1355. static ssize_t
  1356. acpi_video_bus_write_DOS(struct file *file,
  1357. const char __user * buffer,
  1358. size_t count, loff_t * data)
  1359. {
  1360. int status;
  1361. struct seq_file *m = file->private_data;
  1362. struct acpi_video_bus *video = m->private;
  1363. char str[12] = { 0 };
  1364. unsigned long opt;
  1365. if (!video || count >= sizeof(str))
  1366. return -EINVAL;
  1367. if (copy_from_user(str, buffer, count))
  1368. return -EFAULT;
  1369. str[count] = 0;
  1370. opt = strtoul(str, NULL, 0);
  1371. if (opt > 7)
  1372. return -EFAULT;
  1373. status = acpi_video_bus_DOS(video, opt & 0x3, (opt & 0x4) >> 2);
  1374. if (!ACPI_SUCCESS(status))
  1375. return -EFAULT;
  1376. return count;
  1377. }
  1378. static int acpi_video_bus_add_fs(struct acpi_device *device)
  1379. {
  1380. struct acpi_video_bus *video = acpi_driver_data(device);
  1381. struct proc_dir_entry *device_dir;
  1382. struct proc_dir_entry *entry;
  1383. device_dir = proc_mkdir(acpi_device_bid(device), acpi_video_dir);
  1384. if (!device_dir)
  1385. return -ENOMEM;
  1386. /* 'info' [R] */
  1387. entry = proc_create_data("info", S_IRUGO, device_dir,
  1388. &acpi_video_bus_info_fops,
  1389. acpi_driver_data(device));
  1390. if (!entry)
  1391. goto err_remove_dir;
  1392. /* 'ROM' [R] */
  1393. entry = proc_create_data("ROM", S_IRUGO, device_dir,
  1394. &acpi_video_bus_ROM_fops,
  1395. acpi_driver_data(device));
  1396. if (!entry)
  1397. goto err_remove_info;
  1398. /* 'POST_info' [R] */
  1399. entry = proc_create_data("POST_info", S_IRUGO, device_dir,
  1400. &acpi_video_bus_POST_info_fops,
  1401. acpi_driver_data(device));
  1402. if (!entry)
  1403. goto err_remove_rom;
  1404. /* 'POST' [R/W] */
  1405. entry = proc_create_data("POST", S_IFREG | S_IRUGO | S_IWUSR,
  1406. device_dir,
  1407. &acpi_video_bus_POST_fops,
  1408. acpi_driver_data(device));
  1409. if (!entry)
  1410. goto err_remove_post_info;
  1411. /* 'DOS' [R/W] */
  1412. entry = proc_create_data("DOS", S_IFREG | S_IRUGO | S_IWUSR,
  1413. device_dir,
  1414. &acpi_video_bus_DOS_fops,
  1415. acpi_driver_data(device));
  1416. if (!entry)
  1417. goto err_remove_post;
  1418. video->dir = acpi_device_dir(device) = device_dir;
  1419. return 0;
  1420. err_remove_post:
  1421. remove_proc_entry("POST", device_dir);
  1422. err_remove_post_info:
  1423. remove_proc_entry("POST_info", device_dir);
  1424. err_remove_rom:
  1425. remove_proc_entry("ROM", device_dir);
  1426. err_remove_info:
  1427. remove_proc_entry("info", device_dir);
  1428. err_remove_dir:
  1429. remove_proc_entry(acpi_device_bid(device), acpi_video_dir);
  1430. return -ENOMEM;
  1431. }
  1432. static int acpi_video_bus_remove_fs(struct acpi_device *device)
  1433. {
  1434. struct proc_dir_entry *device_dir = acpi_device_dir(device);
  1435. if (device_dir) {
  1436. remove_proc_entry("info", device_dir);
  1437. remove_proc_entry("ROM", device_dir);
  1438. remove_proc_entry("POST_info", device_dir);
  1439. remove_proc_entry("POST", device_dir);
  1440. remove_proc_entry("DOS", device_dir);
  1441. remove_proc_entry(acpi_device_bid(device), acpi_video_dir);
  1442. acpi_device_dir(device) = NULL;
  1443. }
  1444. return 0;
  1445. }
  1446. /* --------------------------------------------------------------------------
  1447. Driver Interface
  1448. -------------------------------------------------------------------------- */
  1449. /* device interface */
  1450. static struct acpi_video_device_attrib*
  1451. acpi_video_get_device_attr(struct acpi_video_bus *video, unsigned long device_id)
  1452. {
  1453. struct acpi_video_enumerated_device *ids;
  1454. int i;
  1455. for (i = 0; i < video->attached_count; i++) {
  1456. ids = &video->attached_array[i];
  1457. if ((ids->value.int_val & 0xffff) == device_id)
  1458. return &ids->value.attrib;
  1459. }
  1460. return NULL;
  1461. }
  1462. static int
  1463. acpi_video_bus_get_one_device(struct acpi_device *device,
  1464. struct acpi_video_bus *video)
  1465. {
  1466. unsigned long long device_id;
  1467. int status;
  1468. struct acpi_video_device *data;
  1469. struct acpi_video_device_attrib* attribute;
  1470. if (!device || !video)
  1471. return -EINVAL;
  1472. status =
  1473. acpi_evaluate_integer(device->handle, "_ADR", NULL, &device_id);
  1474. if (ACPI_SUCCESS(status)) {
  1475. data = kzalloc(sizeof(struct acpi_video_device), GFP_KERNEL);
  1476. if (!data)
  1477. return -ENOMEM;
  1478. strcpy(acpi_device_name(device), ACPI_VIDEO_DEVICE_NAME);
  1479. strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
  1480. device->driver_data = data;
  1481. data->device_id = device_id;
  1482. data->video = video;
  1483. data->dev = device;
  1484. attribute = acpi_video_get_device_attr(video, device_id);
  1485. if((attribute != NULL) && attribute->device_id_scheme) {
  1486. switch (attribute->display_type) {
  1487. case ACPI_VIDEO_DISPLAY_CRT:
  1488. data->flags.crt = 1;
  1489. break;
  1490. case ACPI_VIDEO_DISPLAY_TV:
  1491. data->flags.tvout = 1;
  1492. break;
  1493. case ACPI_VIDEO_DISPLAY_DVI:
  1494. data->flags.dvi = 1;
  1495. break;
  1496. case ACPI_VIDEO_DISPLAY_LCD:
  1497. data->flags.lcd = 1;
  1498. break;
  1499. default:
  1500. data->flags.unknown = 1;
  1501. break;
  1502. }
  1503. if(attribute->bios_can_detect)
  1504. data->flags.bios = 1;
  1505. } else
  1506. data->flags.unknown = 1;
  1507. acpi_video_device_bind(video, data);
  1508. acpi_video_device_find_cap(data);
  1509. status = acpi_install_notify_handler(device->handle,
  1510. ACPI_DEVICE_NOTIFY,
  1511. acpi_video_device_notify,
  1512. data);
  1513. if (ACPI_FAILURE(status)) {
  1514. printk(KERN_ERR PREFIX
  1515. "Error installing notify handler\n");
  1516. if(data->brightness)
  1517. kfree(data->brightness->levels);
  1518. kfree(data->brightness);
  1519. kfree(data);
  1520. return -ENODEV;
  1521. }
  1522. mutex_lock(&video->device_list_lock);
  1523. list_add_tail(&data->entry, &video->video_device_list);
  1524. mutex_unlock(&video->device_list_lock);
  1525. acpi_video_device_add_fs(device);
  1526. return 0;
  1527. }
  1528. return -ENOENT;
  1529. }
  1530. /*
  1531. * Arg:
  1532. * video : video bus device
  1533. *
  1534. * Return:
  1535. * none
  1536. *
  1537. * Enumerate the video device list of the video bus,
  1538. * bind the ids with the corresponding video devices
  1539. * under the video bus.
  1540. */
  1541. static void acpi_video_device_rebind(struct acpi_video_bus *video)
  1542. {
  1543. struct acpi_video_device *dev;
  1544. mutex_lock(&video->device_list_lock);
  1545. list_for_each_entry(dev, &video->video_device_list, entry)
  1546. acpi_video_device_bind(video, dev);
  1547. mutex_unlock(&video->device_list_lock);
  1548. }
  1549. /*
  1550. * Arg:
  1551. * video : video bus device
  1552. * device : video output device under the video
  1553. * bus
  1554. *
  1555. * Return:
  1556. * none
  1557. *
  1558. * Bind the ids with the corresponding video devices
  1559. * under the video bus.
  1560. */
  1561. static void
  1562. acpi_video_device_bind(struct acpi_video_bus *video,
  1563. struct acpi_video_device *device)
  1564. {
  1565. struct acpi_video_enumerated_device *ids;
  1566. int i;
  1567. for (i = 0; i < video->attached_count; i++) {
  1568. ids = &video->attached_array[i];
  1569. if (device->device_id == (ids->value.int_val & 0xffff)) {
  1570. ids->bind_info = device;
  1571. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "device_bind %d\n", i));
  1572. }
  1573. }
  1574. }
  1575. /*
  1576. * Arg:
  1577. * video : video bus device
  1578. *
  1579. * Return:
  1580. * < 0 : error
  1581. *
  1582. * Call _DOD to enumerate all devices attached to display adapter
  1583. *
  1584. */
  1585. static int acpi_video_device_enumerate(struct acpi_video_bus *video)
  1586. {
  1587. int status;
  1588. int count;
  1589. int i;
  1590. struct acpi_video_enumerated_device *active_list;
  1591. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  1592. union acpi_object *dod = NULL;
  1593. union acpi_object *obj;
  1594. status = acpi_evaluate_object(video->device->handle, "_DOD", NULL, &buffer);
  1595. if (!ACPI_SUCCESS(status)) {
  1596. ACPI_EXCEPTION((AE_INFO, status, "Evaluating _DOD"));
  1597. return status;
  1598. }
  1599. dod = buffer.pointer;
  1600. if (!dod || (dod->type != ACPI_TYPE_PACKAGE)) {
  1601. ACPI_EXCEPTION((AE_INFO, status, "Invalid _DOD data"));
  1602. status = -EFAULT;
  1603. goto out;
  1604. }
  1605. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d video heads in _DOD\n",
  1606. dod->package.count));
  1607. active_list = kcalloc(1 + dod->package.count,
  1608. sizeof(struct acpi_video_enumerated_device),
  1609. GFP_KERNEL);
  1610. if (!active_list) {
  1611. status = -ENOMEM;
  1612. goto out;
  1613. }
  1614. count = 0;
  1615. for (i = 0; i < dod->package.count; i++) {
  1616. obj = &dod->package.elements[i];
  1617. if (obj->type != ACPI_TYPE_INTEGER) {
  1618. printk(KERN_ERR PREFIX
  1619. "Invalid _DOD data in element %d\n", i);
  1620. continue;
  1621. }
  1622. active_list[count].value.int_val = obj->integer.value;
  1623. active_list[count].bind_info = NULL;
  1624. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "dod element[%d] = %d\n", i,
  1625. (int)obj->integer.value));
  1626. count++;
  1627. }
  1628. kfree(video->attached_array);
  1629. video->attached_array = active_list;
  1630. video->attached_count = count;
  1631. out:
  1632. kfree(buffer.pointer);
  1633. return status;
  1634. }
  1635. static int
  1636. acpi_video_get_next_level(struct acpi_video_device *device,
  1637. u32 level_current, u32 event)
  1638. {
  1639. int min, max, min_above, max_below, i, l, delta = 255;
  1640. max = max_below = 0;
  1641. min = min_above = 255;
  1642. /* Find closest level to level_current */
  1643. for (i = 2; i < device->brightness->count; i++) {
  1644. l = device->brightness->levels[i];
  1645. if (abs(l - level_current) < abs(delta)) {
  1646. delta = l - level_current;
  1647. if (!delta)
  1648. break;
  1649. }
  1650. }
  1651. /* Ajust level_current to closest available level */
  1652. level_current += delta;
  1653. for (i = 2; i < device->brightness->count; i++) {
  1654. l = device->brightness->levels[i];
  1655. if (l < min)
  1656. min = l;
  1657. if (l > max)
  1658. max = l;
  1659. if (l < min_above && l > level_current)
  1660. min_above = l;
  1661. if (l > max_below && l < level_current)
  1662. max_below = l;
  1663. }
  1664. switch (event) {
  1665. case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS:
  1666. return (level_current < max) ? min_above : min;
  1667. case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS:
  1668. return (level_current < max) ? min_above : max;
  1669. case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS:
  1670. return (level_current > min) ? max_below : min;
  1671. case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS:
  1672. case ACPI_VIDEO_NOTIFY_DISPLAY_OFF:
  1673. return 0;
  1674. default:
  1675. return level_current;
  1676. }
  1677. }
  1678. static int
  1679. acpi_video_switch_brightness(struct acpi_video_device *device, int event)
  1680. {
  1681. unsigned long long level_current, level_next;
  1682. int result = -EINVAL;
  1683. /* no warning message if acpi_backlight=vendor is used */
  1684. if (!acpi_video_backlight_support())
  1685. return 0;
  1686. if (!device->brightness)
  1687. goto out;
  1688. result = acpi_video_device_lcd_get_level_current(device,
  1689. &level_current, 0);
  1690. if (result)
  1691. goto out;
  1692. level_next = acpi_video_get_next_level(device, level_current, event);
  1693. result = acpi_video_device_lcd_set_level(device, level_next);
  1694. if (!result)
  1695. backlight_force_update(device->backlight,
  1696. BACKLIGHT_UPDATE_HOTKEY);
  1697. out:
  1698. if (result)
  1699. printk(KERN_ERR PREFIX "Failed to switch the brightness\n");
  1700. return result;
  1701. }
  1702. static int
  1703. acpi_video_bus_get_devices(struct acpi_video_bus *video,
  1704. struct acpi_device *device)
  1705. {
  1706. int status = 0;
  1707. struct acpi_device *dev;
  1708. acpi_video_device_enumerate(video);
  1709. list_for_each_entry(dev, &device->children, node) {
  1710. status = acpi_video_bus_get_one_device(dev, video);
  1711. if (ACPI_FAILURE(status)) {
  1712. printk(KERN_WARNING PREFIX
  1713. "Cant attach device");
  1714. continue;
  1715. }
  1716. }
  1717. return status;
  1718. }
  1719. static int acpi_video_bus_put_one_device(struct acpi_video_device *device)
  1720. {
  1721. acpi_status status;
  1722. struct acpi_video_bus *video;
  1723. if (!device || !device->video)
  1724. return -ENOENT;
  1725. video = device->video;
  1726. acpi_video_device_remove_fs(device->dev);
  1727. status = acpi_remove_notify_handler(device->dev->handle,
  1728. ACPI_DEVICE_NOTIFY,
  1729. acpi_video_device_notify);
  1730. if (device->backlight) {
  1731. sysfs_remove_link(&device->backlight->dev.kobj, "device");
  1732. backlight_device_unregister(device->backlight);
  1733. device->backlight = NULL;
  1734. }
  1735. if (device->cooling_dev) {
  1736. sysfs_remove_link(&device->dev->dev.kobj,
  1737. "thermal_cooling");
  1738. sysfs_remove_link(&device->cooling_dev->device.kobj,
  1739. "device");
  1740. thermal_cooling_device_unregister(device->cooling_dev);
  1741. device->cooling_dev = NULL;
  1742. }
  1743. video_output_unregister(device->output_dev);
  1744. return 0;
  1745. }
  1746. static int acpi_video_bus_put_devices(struct acpi_video_bus *video)
  1747. {
  1748. int status;
  1749. struct acpi_video_device *dev, *next;
  1750. mutex_lock(&video->device_list_lock);
  1751. list_for_each_entry_safe(dev, next, &video->video_device_list, entry) {
  1752. status = acpi_video_bus_put_one_device(dev);
  1753. if (ACPI_FAILURE(status))
  1754. printk(KERN_WARNING PREFIX
  1755. "hhuuhhuu bug in acpi video driver.\n");
  1756. if (dev->brightness) {
  1757. kfree(dev->brightness->levels);
  1758. kfree(dev->brightness);
  1759. }
  1760. list_del(&dev->entry);
  1761. kfree(dev);
  1762. }
  1763. mutex_unlock(&video->device_list_lock);
  1764. return 0;
  1765. }
  1766. /* acpi_video interface */
  1767. static int acpi_video_bus_start_devices(struct acpi_video_bus *video)
  1768. {
  1769. return acpi_video_bus_DOS(video, 0, 0);
  1770. }
  1771. static int acpi_video_bus_stop_devices(struct acpi_video_bus *video)
  1772. {
  1773. return acpi_video_bus_DOS(video, 0, 1);
  1774. }
  1775. static void acpi_video_bus_notify(struct acpi_device *device, u32 event)
  1776. {
  1777. struct acpi_video_bus *video = acpi_driver_data(device);
  1778. struct input_dev *input;
  1779. int keycode = 0;
  1780. if (!video)
  1781. return;
  1782. input = video->input;
  1783. switch (event) {
  1784. case ACPI_VIDEO_NOTIFY_SWITCH: /* User requested a switch,
  1785. * most likely via hotkey. */
  1786. acpi_bus_generate_proc_event(device, event, 0);
  1787. keycode = KEY_SWITCHVIDEOMODE;
  1788. break;
  1789. case ACPI_VIDEO_NOTIFY_PROBE: /* User plugged in or removed a video
  1790. * connector. */
  1791. acpi_video_device_enumerate(video);
  1792. acpi_video_device_rebind(video);
  1793. acpi_bus_generate_proc_event(device, event, 0);
  1794. keycode = KEY_SWITCHVIDEOMODE;
  1795. break;
  1796. case ACPI_VIDEO_NOTIFY_CYCLE: /* Cycle Display output hotkey pressed. */
  1797. acpi_bus_generate_proc_event(device, event, 0);
  1798. keycode = KEY_SWITCHVIDEOMODE;
  1799. break;
  1800. case ACPI_VIDEO_NOTIFY_NEXT_OUTPUT: /* Next Display output hotkey pressed. */
  1801. acpi_bus_generate_proc_event(device, event, 0);
  1802. keycode = KEY_VIDEO_NEXT;
  1803. break;
  1804. case ACPI_VIDEO_NOTIFY_PREV_OUTPUT: /* previous Display output hotkey pressed. */
  1805. acpi_bus_generate_proc_event(device, event, 0);
  1806. keycode = KEY_VIDEO_PREV;
  1807. break;
  1808. default:
  1809. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  1810. "Unsupported event [0x%x]\n", event));
  1811. break;
  1812. }
  1813. acpi_notifier_call_chain(device, event, 0);
  1814. if (keycode) {
  1815. input_report_key(input, keycode, 1);
  1816. input_sync(input);
  1817. input_report_key(input, keycode, 0);
  1818. input_sync(input);
  1819. }
  1820. return;
  1821. }
  1822. static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data)
  1823. {
  1824. struct acpi_video_device *video_device = data;
  1825. struct acpi_device *device = NULL;
  1826. struct acpi_video_bus *bus;
  1827. struct input_dev *input;
  1828. int keycode = 0;
  1829. if (!video_device)
  1830. return;
  1831. device = video_device->dev;
  1832. bus = video_device->video;
  1833. input = bus->input;
  1834. switch (event) {
  1835. case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS: /* Cycle brightness */
  1836. if (brightness_switch_enabled)
  1837. acpi_video_switch_brightness(video_device, event);
  1838. acpi_bus_generate_proc_event(device, event, 0);
  1839. keycode = KEY_BRIGHTNESS_CYCLE;
  1840. break;
  1841. case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS: /* Increase brightness */
  1842. if (brightness_switch_enabled)
  1843. acpi_video_switch_brightness(video_device, event);
  1844. acpi_bus_generate_proc_event(device, event, 0);
  1845. keycode = KEY_BRIGHTNESSUP;
  1846. break;
  1847. case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS: /* Decrease brightness */
  1848. if (brightness_switch_enabled)
  1849. acpi_video_switch_brightness(video_device, event);
  1850. acpi_bus_generate_proc_event(device, event, 0);
  1851. keycode = KEY_BRIGHTNESSDOWN;
  1852. break;
  1853. case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS: /* zero brightnesss */
  1854. if (brightness_switch_enabled)
  1855. acpi_video_switch_brightness(video_device, event);
  1856. acpi_bus_generate_proc_event(device, event, 0);
  1857. keycode = KEY_BRIGHTNESS_ZERO;
  1858. break;
  1859. case ACPI_VIDEO_NOTIFY_DISPLAY_OFF: /* display device off */
  1860. if (brightness_switch_enabled)
  1861. acpi_video_switch_brightness(video_device, event);
  1862. acpi_bus_generate_proc_event(device, event, 0);
  1863. keycode = KEY_DISPLAY_OFF;
  1864. break;
  1865. default:
  1866. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  1867. "Unsupported event [0x%x]\n", event));
  1868. break;
  1869. }
  1870. acpi_notifier_call_chain(device, event, 0);
  1871. if (keycode) {
  1872. input_report_key(input, keycode, 1);
  1873. input_sync(input);
  1874. input_report_key(input, keycode, 0);
  1875. input_sync(input);
  1876. }
  1877. return;
  1878. }
  1879. static int acpi_video_resume(struct notifier_block *nb,
  1880. unsigned long val, void *ign)
  1881. {
  1882. struct acpi_video_bus *video;
  1883. struct acpi_video_device *video_device;
  1884. int i;
  1885. switch (val) {
  1886. case PM_HIBERNATION_PREPARE:
  1887. case PM_SUSPEND_PREPARE:
  1888. case PM_RESTORE_PREPARE:
  1889. return NOTIFY_DONE;
  1890. }
  1891. video = container_of(nb, struct acpi_video_bus, pm_nb);
  1892. dev_info(&video->device->dev, "Restoring backlight state\n");
  1893. for (i = 0; i < video->attached_count; i++) {
  1894. video_device = video->attached_array[i].bind_info;
  1895. if (video_device && video_device->backlight)
  1896. acpi_video_set_brightness(video_device->backlight);
  1897. }
  1898. return NOTIFY_OK;
  1899. }
  1900. static acpi_status
  1901. acpi_video_bus_match(acpi_handle handle, u32 level, void *context,
  1902. void **return_value)
  1903. {
  1904. struct acpi_device *device = context;
  1905. struct acpi_device *sibling;
  1906. int result;
  1907. if (handle == device->handle)
  1908. return AE_CTRL_TERMINATE;
  1909. result = acpi_bus_get_device(handle, &sibling);
  1910. if (result)
  1911. return AE_OK;
  1912. if (!strcmp(acpi_device_name(sibling), ACPI_VIDEO_BUS_NAME))
  1913. return AE_ALREADY_EXISTS;
  1914. return AE_OK;
  1915. }
  1916. static int instance;
  1917. static int acpi_video_bus_add(struct acpi_device *device)
  1918. {
  1919. struct acpi_video_bus *video;
  1920. struct input_dev *input;
  1921. int error;
  1922. acpi_status status;
  1923. status = acpi_walk_namespace(ACPI_TYPE_DEVICE,
  1924. device->parent->handle, 1,
  1925. acpi_video_bus_match, NULL,
  1926. device, NULL);
  1927. if (status == AE_ALREADY_EXISTS) {
  1928. printk(KERN_WARNING FW_BUG
  1929. "Duplicate ACPI video bus devices for the"
  1930. " same VGA controller, please try module "
  1931. "parameter \"video.allow_duplicates=1\""
  1932. "if the current driver doesn't work.\n");
  1933. if (!allow_duplicates)
  1934. return -ENODEV;
  1935. }
  1936. video = kzalloc(sizeof(struct acpi_video_bus), GFP_KERNEL);
  1937. if (!video)
  1938. return -ENOMEM;
  1939. /* a hack to fix the duplicate name "VID" problem on T61 */
  1940. if (!strcmp(device->pnp.bus_id, "VID")) {
  1941. if (instance)
  1942. device->pnp.bus_id[3] = '0' + instance;
  1943. instance ++;
  1944. }
  1945. /* a hack to fix the duplicate name "VGA" problem on Pa 3553 */
  1946. if (!strcmp(device->pnp.bus_id, "VGA")) {
  1947. if (instance)
  1948. device->pnp.bus_id[3] = '0' + instance;
  1949. instance++;
  1950. }
  1951. video->device = device;
  1952. strcpy(acpi_device_name(device), ACPI_VIDEO_BUS_NAME);
  1953. strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
  1954. device->driver_data = video;
  1955. acpi_video_bus_find_cap(video);
  1956. error = acpi_video_bus_check(video);
  1957. if (error)
  1958. goto err_free_video;
  1959. error = acpi_video_bus_add_fs(device);
  1960. if (error)
  1961. goto err_free_video;
  1962. mutex_init(&video->device_list_lock);
  1963. INIT_LIST_HEAD(&video->video_device_list);
  1964. acpi_video_bus_get_devices(video, device);
  1965. acpi_video_bus_start_devices(video);
  1966. video->input = input = input_allocate_device();
  1967. if (!input) {
  1968. error = -ENOMEM;
  1969. goto err_stop_video;
  1970. }
  1971. snprintf(video->phys, sizeof(video->phys),
  1972. "%s/video/input0", acpi_device_hid(video->device));
  1973. input->name = acpi_device_name(video->device);
  1974. input->phys = video->phys;
  1975. input->id.bustype = BUS_HOST;
  1976. input->id.product = 0x06;
  1977. input->dev.parent = &device->dev;
  1978. input->evbit[0] = BIT(EV_KEY);
  1979. set_bit(KEY_SWITCHVIDEOMODE, input->keybit);
  1980. set_bit(KEY_VIDEO_NEXT, input->keybit);
  1981. set_bit(KEY_VIDEO_PREV, input->keybit);
  1982. set_bit(KEY_BRIGHTNESS_CYCLE, input->keybit);
  1983. set_bit(KEY_BRIGHTNESSUP, input->keybit);
  1984. set_bit(KEY_BRIGHTNESSDOWN, input->keybit);
  1985. set_bit(KEY_BRIGHTNESS_ZERO, input->keybit);
  1986. set_bit(KEY_DISPLAY_OFF, input->keybit);
  1987. error = input_register_device(input);
  1988. if (error)
  1989. goto err_free_input_dev;
  1990. printk(KERN_INFO PREFIX "%s [%s] (multi-head: %s rom: %s post: %s)\n",
  1991. ACPI_VIDEO_DEVICE_NAME, acpi_device_bid(device),
  1992. video->flags.multihead ? "yes" : "no",
  1993. video->flags.rom ? "yes" : "no",
  1994. video->flags.post ? "yes" : "no");
  1995. video->pm_nb.notifier_call = acpi_video_resume;
  1996. video->pm_nb.priority = 0;
  1997. register_pm_notifier(&video->pm_nb);
  1998. return 0;
  1999. err_free_input_dev:
  2000. input_free_device(input);
  2001. err_stop_video:
  2002. acpi_video_bus_stop_devices(video);
  2003. acpi_video_bus_put_devices(video);
  2004. kfree(video->attached_array);
  2005. acpi_video_bus_remove_fs(device);
  2006. err_free_video:
  2007. kfree(video);
  2008. device->driver_data = NULL;
  2009. return error;
  2010. }
  2011. static int acpi_video_bus_remove(struct acpi_device *device, int type)
  2012. {
  2013. struct acpi_video_bus *video = NULL;
  2014. if (!device || !acpi_driver_data(device))
  2015. return -EINVAL;
  2016. video = acpi_driver_data(device);
  2017. unregister_pm_notifier(&video->pm_nb);
  2018. acpi_video_bus_stop_devices(video);
  2019. acpi_video_bus_put_devices(video);
  2020. acpi_video_bus_remove_fs(device);
  2021. input_unregister_device(video->input);
  2022. kfree(video->attached_array);
  2023. kfree(video);
  2024. return 0;
  2025. }
  2026. static int __init intel_opregion_present(void)
  2027. {
  2028. #if defined(CONFIG_DRM_I915) || defined(CONFIG_DRM_I915_MODULE)
  2029. struct pci_dev *dev = NULL;
  2030. u32 address;
  2031. for_each_pci_dev(dev) {
  2032. if ((dev->class >> 8) != PCI_CLASS_DISPLAY_VGA)
  2033. continue;
  2034. if (dev->vendor != PCI_VENDOR_ID_INTEL)
  2035. continue;
  2036. pci_read_config_dword(dev, 0xfc, &address);
  2037. if (!address)
  2038. continue;
  2039. return 1;
  2040. }
  2041. #endif
  2042. return 0;
  2043. }
  2044. int acpi_video_register(void)
  2045. {
  2046. int result = 0;
  2047. if (register_count) {
  2048. /*
  2049. * if the function of acpi_video_register is already called,
  2050. * don't register the acpi_vide_bus again and return no error.
  2051. */
  2052. return 0;
  2053. }
  2054. acpi_video_dir = proc_mkdir(ACPI_VIDEO_CLASS, acpi_root_dir);
  2055. if (!acpi_video_dir)
  2056. return -ENODEV;
  2057. result = acpi_bus_register_driver(&acpi_video_bus);
  2058. if (result < 0) {
  2059. remove_proc_entry(ACPI_VIDEO_CLASS, acpi_root_dir);
  2060. return -ENODEV;
  2061. }
  2062. /*
  2063. * When the acpi_video_bus is loaded successfully, increase
  2064. * the counter reference.
  2065. */
  2066. register_count = 1;
  2067. return 0;
  2068. }
  2069. EXPORT_SYMBOL(acpi_video_register);
  2070. void acpi_video_unregister(void)
  2071. {
  2072. if (!register_count) {
  2073. /*
  2074. * If the acpi video bus is already unloaded, don't
  2075. * unload it again and return directly.
  2076. */
  2077. return;
  2078. }
  2079. acpi_bus_unregister_driver(&acpi_video_bus);
  2080. remove_proc_entry(ACPI_VIDEO_CLASS, acpi_root_dir);
  2081. register_count = 0;
  2082. return;
  2083. }
  2084. EXPORT_SYMBOL(acpi_video_unregister);
  2085. /*
  2086. * This is kind of nasty. Hardware using Intel chipsets may require
  2087. * the video opregion code to be run first in order to initialise
  2088. * state before any ACPI video calls are made. To handle this we defer
  2089. * registration of the video class until the opregion code has run.
  2090. */
  2091. static int __init acpi_video_init(void)
  2092. {
  2093. dmi_check_system(video_dmi_table);
  2094. if (intel_opregion_present())
  2095. return 0;
  2096. return acpi_video_register();
  2097. }
  2098. static void __exit acpi_video_exit(void)
  2099. {
  2100. acpi_video_unregister();
  2101. return;
  2102. }
  2103. module_init(acpi_video_init);
  2104. module_exit(acpi_video_exit);