video.c 59 KB

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