video.c 60 KB

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