video.c 60 KB

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