video.c 61 KB

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