video.c 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308
  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. static int
  465. acpi_video_device_lcd_get_level_current(struct acpi_video_device *device,
  466. unsigned long long *level)
  467. {
  468. acpi_status status = AE_OK;
  469. if (device->cap._BQC || device->cap._BCQ) {
  470. char *buf = device->cap._BQC ? "_BQC" : "_BCQ";
  471. status = acpi_evaluate_integer(device->dev->handle, buf,
  472. NULL, level);
  473. if (ACPI_SUCCESS(status)) {
  474. if (device->brightness->flags._BQC_use_index) {
  475. if (device->brightness->flags._BCL_reversed)
  476. *level = device->brightness->count
  477. - 3 - (*level);
  478. *level = device->brightness->levels[*level + 2];
  479. }
  480. device->brightness->curr = *level;
  481. return 0;
  482. } else {
  483. /* Fixme:
  484. * should we return an error or ignore this failure?
  485. * dev->brightness->curr is a cached value which stores
  486. * the correct current backlight level in most cases.
  487. * ACPI video backlight still works w/ buggy _BQC.
  488. * http://bugzilla.kernel.org/show_bug.cgi?id=12233
  489. */
  490. ACPI_WARNING((AE_INFO, "Evaluating %s failed", buf));
  491. device->cap._BQC = device->cap._BCQ = 0;
  492. }
  493. }
  494. *level = device->brightness->curr;
  495. return 0;
  496. }
  497. static int
  498. acpi_video_device_EDID(struct acpi_video_device *device,
  499. union acpi_object **edid, ssize_t length)
  500. {
  501. int status;
  502. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  503. union acpi_object *obj;
  504. union acpi_object arg0 = { ACPI_TYPE_INTEGER };
  505. struct acpi_object_list args = { 1, &arg0 };
  506. *edid = NULL;
  507. if (!device)
  508. return -ENODEV;
  509. if (length == 128)
  510. arg0.integer.value = 1;
  511. else if (length == 256)
  512. arg0.integer.value = 2;
  513. else
  514. return -EINVAL;
  515. status = acpi_evaluate_object(device->dev->handle, "_DDC", &args, &buffer);
  516. if (ACPI_FAILURE(status))
  517. return -ENODEV;
  518. obj = buffer.pointer;
  519. if (obj && obj->type == ACPI_TYPE_BUFFER)
  520. *edid = obj;
  521. else {
  522. printk(KERN_ERR PREFIX "Invalid _DDC data\n");
  523. status = -EFAULT;
  524. kfree(obj);
  525. }
  526. return status;
  527. }
  528. /* bus */
  529. static int
  530. acpi_video_bus_set_POST(struct acpi_video_bus *video, unsigned long option)
  531. {
  532. int status;
  533. unsigned long long tmp;
  534. union acpi_object arg0 = { ACPI_TYPE_INTEGER };
  535. struct acpi_object_list args = { 1, &arg0 };
  536. arg0.integer.value = option;
  537. status = acpi_evaluate_integer(video->device->handle, "_SPD", &args, &tmp);
  538. if (ACPI_SUCCESS(status))
  539. status = tmp ? (-EINVAL) : (AE_OK);
  540. return status;
  541. }
  542. static int
  543. acpi_video_bus_get_POST(struct acpi_video_bus *video, unsigned long long *id)
  544. {
  545. int status;
  546. status = acpi_evaluate_integer(video->device->handle, "_GPD", NULL, id);
  547. return status;
  548. }
  549. static int
  550. acpi_video_bus_POST_options(struct acpi_video_bus *video,
  551. unsigned long long *options)
  552. {
  553. int status;
  554. status = acpi_evaluate_integer(video->device->handle, "_VPO", NULL, options);
  555. *options &= 3;
  556. return status;
  557. }
  558. /*
  559. * Arg:
  560. * video : video bus device pointer
  561. * bios_flag :
  562. * 0. The system BIOS should NOT automatically switch(toggle)
  563. * the active display output.
  564. * 1. The system BIOS should automatically switch (toggle) the
  565. * active display output. No switch event.
  566. * 2. The _DGS value should be locked.
  567. * 3. The system BIOS should not automatically switch (toggle) the
  568. * active display output, but instead generate the display switch
  569. * event notify code.
  570. * lcd_flag :
  571. * 0. The system BIOS should automatically control the brightness level
  572. * of the LCD when the power changes from AC to DC
  573. * 1. The system BIOS should NOT automatically control the brightness
  574. * level of the LCD when the power changes from AC to DC.
  575. * Return Value:
  576. * -1 wrong arg.
  577. */
  578. static int
  579. acpi_video_bus_DOS(struct acpi_video_bus *video, int bios_flag, int lcd_flag)
  580. {
  581. acpi_integer status = 0;
  582. union acpi_object arg0 = { ACPI_TYPE_INTEGER };
  583. struct acpi_object_list args = { 1, &arg0 };
  584. if (bios_flag < 0 || bios_flag > 3 || lcd_flag < 0 || lcd_flag > 1) {
  585. status = -1;
  586. goto Failed;
  587. }
  588. arg0.integer.value = (lcd_flag << 2) | bios_flag;
  589. video->dos_setting = arg0.integer.value;
  590. acpi_evaluate_object(video->device->handle, "_DOS", &args, NULL);
  591. Failed:
  592. return status;
  593. }
  594. /*
  595. * Simple comparison function used to sort backlight levels.
  596. */
  597. static int
  598. acpi_video_cmp_level(const void *a, const void *b)
  599. {
  600. return *(int *)a - *(int *)b;
  601. }
  602. /*
  603. * Arg:
  604. * device : video output device (LCD, CRT, ..)
  605. *
  606. * Return Value:
  607. * Maximum brightness level
  608. *
  609. * Allocate and initialize device->brightness.
  610. */
  611. static int
  612. acpi_video_init_brightness(struct acpi_video_device *device)
  613. {
  614. union acpi_object *obj = NULL;
  615. int i, max_level = 0, count = 0, level_ac_battery = 0;
  616. unsigned long long level, level_old;
  617. union acpi_object *o;
  618. struct acpi_video_device_brightness *br = NULL;
  619. int result = -EINVAL;
  620. if (!ACPI_SUCCESS(acpi_video_device_lcd_query_levels(device, &obj))) {
  621. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Could not query available "
  622. "LCD brightness level\n"));
  623. goto out;
  624. }
  625. if (obj->package.count < 2)
  626. goto out;
  627. br = kzalloc(sizeof(*br), GFP_KERNEL);
  628. if (!br) {
  629. printk(KERN_ERR "can't allocate memory\n");
  630. result = -ENOMEM;
  631. goto out;
  632. }
  633. br->levels = kmalloc((obj->package.count + 2) * sizeof *(br->levels),
  634. GFP_KERNEL);
  635. if (!br->levels) {
  636. result = -ENOMEM;
  637. goto out_free;
  638. }
  639. for (i = 0; i < obj->package.count; i++) {
  640. o = (union acpi_object *)&obj->package.elements[i];
  641. if (o->type != ACPI_TYPE_INTEGER) {
  642. printk(KERN_ERR PREFIX "Invalid data\n");
  643. continue;
  644. }
  645. br->levels[count] = (u32) o->integer.value;
  646. if (br->levels[count] > max_level)
  647. max_level = br->levels[count];
  648. count++;
  649. }
  650. /*
  651. * some buggy BIOS don't export the levels
  652. * when machine is on AC/Battery in _BCL package.
  653. * In this case, the first two elements in _BCL packages
  654. * are also supported brightness levels that OS should take care of.
  655. */
  656. for (i = 2; i < count; i++)
  657. if (br->levels[i] == br->levels[0] ||
  658. br->levels[i] == br->levels[1])
  659. level_ac_battery++;
  660. if (level_ac_battery < 2) {
  661. level_ac_battery = 2 - level_ac_battery;
  662. br->flags._BCL_no_ac_battery_levels = 1;
  663. for (i = (count - 1 + level_ac_battery); i >= 2; i--)
  664. br->levels[i] = br->levels[i - level_ac_battery];
  665. count += level_ac_battery;
  666. } else if (level_ac_battery > 2)
  667. ACPI_ERROR((AE_INFO, "Too many duplicates in _BCL package\n"));
  668. /* Check if the _BCL package is in a reversed order */
  669. if (max_level == br->levels[2]) {
  670. br->flags._BCL_reversed = 1;
  671. sort(&br->levels[2], count - 2, sizeof(br->levels[2]),
  672. acpi_video_cmp_level, NULL);
  673. } else if (max_level != br->levels[count - 1])
  674. ACPI_ERROR((AE_INFO,
  675. "Found unordered _BCL package\n"));
  676. br->count = count;
  677. device->brightness = br;
  678. /* Check the input/output of _BQC/_BCL/_BCM */
  679. if ((max_level < 100) && (max_level <= (count - 2)))
  680. br->flags._BCL_use_index = 1;
  681. /*
  682. * _BCM is always consistent with _BCL,
  683. * at least for all the laptops we have ever seen.
  684. */
  685. br->flags._BCM_use_index = br->flags._BCL_use_index;
  686. /* _BQC uses INDEX while _BCL uses VALUE in some laptops */
  687. br->curr = level_old = max_level;
  688. if (!device->cap._BQC)
  689. goto set_level;
  690. result = acpi_video_device_lcd_get_level_current(device, &level_old);
  691. if (result)
  692. goto out_free_levels;
  693. /*
  694. * Set the level to maximum and check if _BQC uses indexed value
  695. */
  696. result = acpi_video_device_lcd_set_level(device, max_level);
  697. if (result)
  698. goto out_free_levels;
  699. result = acpi_video_device_lcd_get_level_current(device, &level);
  700. if (result)
  701. goto out_free_levels;
  702. br->flags._BQC_use_index = (level == max_level ? 0 : 1);
  703. if (!br->flags._BQC_use_index)
  704. goto set_level;
  705. if (br->flags._BCL_reversed)
  706. level_old = (br->count - 1) - level_old;
  707. level_old = br->levels[level_old];
  708. set_level:
  709. result = acpi_video_device_lcd_set_level(device, level_old);
  710. if (result)
  711. goto out_free_levels;
  712. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  713. "found %d brightness levels\n", count - 2));
  714. kfree(obj);
  715. return result;
  716. out_free_levels:
  717. kfree(br->levels);
  718. out_free:
  719. kfree(br);
  720. out:
  721. device->brightness = NULL;
  722. kfree(obj);
  723. return result;
  724. }
  725. /*
  726. * Arg:
  727. * device : video output device (LCD, CRT, ..)
  728. *
  729. * Return Value:
  730. * None
  731. *
  732. * Find out all required AML methods defined under the output
  733. * device.
  734. */
  735. static void acpi_video_device_find_cap(struct acpi_video_device *device)
  736. {
  737. acpi_handle h_dummy1;
  738. memset(&device->cap, 0, sizeof(device->cap));
  739. if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_ADR", &h_dummy1))) {
  740. device->cap._ADR = 1;
  741. }
  742. if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_BCL", &h_dummy1))) {
  743. device->cap._BCL = 1;
  744. }
  745. if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_BCM", &h_dummy1))) {
  746. device->cap._BCM = 1;
  747. }
  748. if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle,"_BQC",&h_dummy1)))
  749. device->cap._BQC = 1;
  750. else if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_BCQ",
  751. &h_dummy1))) {
  752. printk(KERN_WARNING FW_BUG "_BCQ is used instead of _BQC\n");
  753. device->cap._BCQ = 1;
  754. }
  755. if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DDC", &h_dummy1))) {
  756. device->cap._DDC = 1;
  757. }
  758. if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DCS", &h_dummy1))) {
  759. device->cap._DCS = 1;
  760. }
  761. if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DGS", &h_dummy1))) {
  762. device->cap._DGS = 1;
  763. }
  764. if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DSS", &h_dummy1))) {
  765. device->cap._DSS = 1;
  766. }
  767. if (acpi_video_backlight_support()) {
  768. int result;
  769. static int count = 0;
  770. char *name;
  771. result = acpi_video_init_brightness(device);
  772. if (result)
  773. return;
  774. name = kzalloc(MAX_NAME_LEN, GFP_KERNEL);
  775. if (!name)
  776. return;
  777. sprintf(name, "acpi_video%d", count++);
  778. device->backlight = backlight_device_register(name,
  779. NULL, device, &acpi_backlight_ops);
  780. device->backlight->props.max_brightness = device->brightness->count-3;
  781. kfree(name);
  782. device->cdev = thermal_cooling_device_register("LCD",
  783. device->dev, &video_cooling_ops);
  784. if (IS_ERR(device->cdev))
  785. return;
  786. dev_info(&device->dev->dev, "registered as cooling_device%d\n",
  787. device->cdev->id);
  788. result = sysfs_create_link(&device->dev->dev.kobj,
  789. &device->cdev->device.kobj,
  790. "thermal_cooling");
  791. if (result)
  792. printk(KERN_ERR PREFIX "Create sysfs link\n");
  793. result = sysfs_create_link(&device->cdev->device.kobj,
  794. &device->dev->dev.kobj, "device");
  795. if (result)
  796. printk(KERN_ERR PREFIX "Create sysfs link\n");
  797. }
  798. if (acpi_video_display_switch_support()) {
  799. if (device->cap._DCS && device->cap._DSS) {
  800. static int count;
  801. char *name;
  802. name = kzalloc(MAX_NAME_LEN, GFP_KERNEL);
  803. if (!name)
  804. return;
  805. sprintf(name, "acpi_video%d", count++);
  806. device->output_dev = video_output_register(name,
  807. NULL, device, &acpi_output_properties);
  808. kfree(name);
  809. }
  810. }
  811. }
  812. /*
  813. * Arg:
  814. * device : video output device (VGA)
  815. *
  816. * Return Value:
  817. * None
  818. *
  819. * Find out all required AML methods defined under the video bus device.
  820. */
  821. static void acpi_video_bus_find_cap(struct acpi_video_bus *video)
  822. {
  823. acpi_handle h_dummy1;
  824. memset(&video->cap, 0, sizeof(video->cap));
  825. if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_DOS", &h_dummy1))) {
  826. video->cap._DOS = 1;
  827. }
  828. if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_DOD", &h_dummy1))) {
  829. video->cap._DOD = 1;
  830. }
  831. if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_ROM", &h_dummy1))) {
  832. video->cap._ROM = 1;
  833. }
  834. if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_GPD", &h_dummy1))) {
  835. video->cap._GPD = 1;
  836. }
  837. if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_SPD", &h_dummy1))) {
  838. video->cap._SPD = 1;
  839. }
  840. if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_VPO", &h_dummy1))) {
  841. video->cap._VPO = 1;
  842. }
  843. }
  844. /*
  845. * Check whether the video bus device has required AML method to
  846. * support the desired features
  847. */
  848. static int acpi_video_bus_check(struct acpi_video_bus *video)
  849. {
  850. acpi_status status = -ENOENT;
  851. struct device *dev;
  852. if (!video)
  853. return -EINVAL;
  854. dev = acpi_get_physical_pci_device(video->device->handle);
  855. if (!dev)
  856. return -ENODEV;
  857. put_device(dev);
  858. /* Since there is no HID, CID and so on for VGA driver, we have
  859. * to check well known required nodes.
  860. */
  861. /* Does this device support video switching? */
  862. if (video->cap._DOS) {
  863. video->flags.multihead = 1;
  864. status = 0;
  865. }
  866. /* Does this device support retrieving a video ROM? */
  867. if (video->cap._ROM) {
  868. video->flags.rom = 1;
  869. status = 0;
  870. }
  871. /* Does this device support configuring which video device to POST? */
  872. if (video->cap._GPD && video->cap._SPD && video->cap._VPO) {
  873. video->flags.post = 1;
  874. status = 0;
  875. }
  876. return status;
  877. }
  878. /* --------------------------------------------------------------------------
  879. FS Interface (/proc)
  880. -------------------------------------------------------------------------- */
  881. static struct proc_dir_entry *acpi_video_dir;
  882. /* video devices */
  883. static int acpi_video_device_info_seq_show(struct seq_file *seq, void *offset)
  884. {
  885. struct acpi_video_device *dev = seq->private;
  886. if (!dev)
  887. goto end;
  888. seq_printf(seq, "device_id: 0x%04x\n", (u32) dev->device_id);
  889. seq_printf(seq, "type: ");
  890. if (dev->flags.crt)
  891. seq_printf(seq, "CRT\n");
  892. else if (dev->flags.lcd)
  893. seq_printf(seq, "LCD\n");
  894. else if (dev->flags.tvout)
  895. seq_printf(seq, "TVOUT\n");
  896. else if (dev->flags.dvi)
  897. seq_printf(seq, "DVI\n");
  898. else
  899. seq_printf(seq, "UNKNOWN\n");
  900. seq_printf(seq, "known by bios: %s\n", dev->flags.bios ? "yes" : "no");
  901. end:
  902. return 0;
  903. }
  904. static int
  905. acpi_video_device_info_open_fs(struct inode *inode, struct file *file)
  906. {
  907. return single_open(file, acpi_video_device_info_seq_show,
  908. PDE(inode)->data);
  909. }
  910. static int acpi_video_device_state_seq_show(struct seq_file *seq, void *offset)
  911. {
  912. int status;
  913. struct acpi_video_device *dev = seq->private;
  914. unsigned long long state;
  915. if (!dev)
  916. goto end;
  917. status = acpi_video_device_get_state(dev, &state);
  918. seq_printf(seq, "state: ");
  919. if (ACPI_SUCCESS(status))
  920. seq_printf(seq, "0x%02llx\n", state);
  921. else
  922. seq_printf(seq, "<not supported>\n");
  923. status = acpi_video_device_query(dev, &state);
  924. seq_printf(seq, "query: ");
  925. if (ACPI_SUCCESS(status))
  926. seq_printf(seq, "0x%02llx\n", state);
  927. else
  928. seq_printf(seq, "<not supported>\n");
  929. end:
  930. return 0;
  931. }
  932. static int
  933. acpi_video_device_state_open_fs(struct inode *inode, struct file *file)
  934. {
  935. return single_open(file, acpi_video_device_state_seq_show,
  936. PDE(inode)->data);
  937. }
  938. static ssize_t
  939. acpi_video_device_write_state(struct file *file,
  940. const char __user * buffer,
  941. size_t count, loff_t * data)
  942. {
  943. int status;
  944. struct seq_file *m = file->private_data;
  945. struct acpi_video_device *dev = m->private;
  946. char str[12] = { 0 };
  947. u32 state = 0;
  948. if (!dev || count + 1 > sizeof str)
  949. return -EINVAL;
  950. if (copy_from_user(str, buffer, count))
  951. return -EFAULT;
  952. str[count] = 0;
  953. state = simple_strtoul(str, NULL, 0);
  954. state &= ((1ul << 31) | (1ul << 30) | (1ul << 0));
  955. status = acpi_video_device_set_state(dev, state);
  956. if (status)
  957. return -EFAULT;
  958. return count;
  959. }
  960. static int
  961. acpi_video_device_brightness_seq_show(struct seq_file *seq, void *offset)
  962. {
  963. struct acpi_video_device *dev = seq->private;
  964. int i;
  965. if (!dev || !dev->brightness) {
  966. seq_printf(seq, "<not supported>\n");
  967. return 0;
  968. }
  969. seq_printf(seq, "levels: ");
  970. for (i = 2; i < dev->brightness->count; i++)
  971. seq_printf(seq, " %d", dev->brightness->levels[i]);
  972. seq_printf(seq, "\ncurrent: %d\n", dev->brightness->curr);
  973. return 0;
  974. }
  975. static int
  976. acpi_video_device_brightness_open_fs(struct inode *inode, struct file *file)
  977. {
  978. return single_open(file, acpi_video_device_brightness_seq_show,
  979. PDE(inode)->data);
  980. }
  981. static ssize_t
  982. acpi_video_device_write_brightness(struct file *file,
  983. const char __user * buffer,
  984. size_t count, loff_t * data)
  985. {
  986. struct seq_file *m = file->private_data;
  987. struct acpi_video_device *dev = m->private;
  988. char str[5] = { 0 };
  989. unsigned int level = 0;
  990. int i;
  991. if (!dev || !dev->brightness || count + 1 > sizeof str)
  992. return -EINVAL;
  993. if (copy_from_user(str, buffer, count))
  994. return -EFAULT;
  995. str[count] = 0;
  996. level = simple_strtoul(str, NULL, 0);
  997. if (level > 100)
  998. return -EFAULT;
  999. /* validate through the list of available levels */
  1000. for (i = 2; i < dev->brightness->count; i++)
  1001. if (level == dev->brightness->levels[i]) {
  1002. if (!acpi_video_device_lcd_set_level(dev, level))
  1003. return count;
  1004. break;
  1005. }
  1006. return -EINVAL;
  1007. }
  1008. static int acpi_video_device_EDID_seq_show(struct seq_file *seq, void *offset)
  1009. {
  1010. struct acpi_video_device *dev = seq->private;
  1011. int status;
  1012. int i;
  1013. union acpi_object *edid = NULL;
  1014. if (!dev)
  1015. goto out;
  1016. status = acpi_video_device_EDID(dev, &edid, 128);
  1017. if (ACPI_FAILURE(status)) {
  1018. status = acpi_video_device_EDID(dev, &edid, 256);
  1019. }
  1020. if (ACPI_FAILURE(status)) {
  1021. goto out;
  1022. }
  1023. if (edid && edid->type == ACPI_TYPE_BUFFER) {
  1024. for (i = 0; i < edid->buffer.length; i++)
  1025. seq_putc(seq, edid->buffer.pointer[i]);
  1026. }
  1027. out:
  1028. if (!edid)
  1029. seq_printf(seq, "<not supported>\n");
  1030. else
  1031. kfree(edid);
  1032. return 0;
  1033. }
  1034. static int
  1035. acpi_video_device_EDID_open_fs(struct inode *inode, struct file *file)
  1036. {
  1037. return single_open(file, acpi_video_device_EDID_seq_show,
  1038. PDE(inode)->data);
  1039. }
  1040. static int acpi_video_device_add_fs(struct acpi_device *device)
  1041. {
  1042. struct proc_dir_entry *entry, *device_dir;
  1043. struct acpi_video_device *vid_dev;
  1044. vid_dev = acpi_driver_data(device);
  1045. if (!vid_dev)
  1046. return -ENODEV;
  1047. device_dir = proc_mkdir(acpi_device_bid(device),
  1048. vid_dev->video->dir);
  1049. if (!device_dir)
  1050. return -ENOMEM;
  1051. /* 'info' [R] */
  1052. entry = proc_create_data("info", S_IRUGO, device_dir,
  1053. &acpi_video_device_info_fops, acpi_driver_data(device));
  1054. if (!entry)
  1055. goto err_remove_dir;
  1056. /* 'state' [R/W] */
  1057. entry = proc_create_data("state", S_IFREG | S_IRUGO | S_IWUSR,
  1058. device_dir,
  1059. &acpi_video_device_state_fops,
  1060. acpi_driver_data(device));
  1061. if (!entry)
  1062. goto err_remove_info;
  1063. /* 'brightness' [R/W] */
  1064. entry = proc_create_data("brightness", S_IFREG | S_IRUGO | S_IWUSR,
  1065. device_dir,
  1066. &acpi_video_device_brightness_fops,
  1067. acpi_driver_data(device));
  1068. if (!entry)
  1069. goto err_remove_state;
  1070. /* 'EDID' [R] */
  1071. entry = proc_create_data("EDID", S_IRUGO, device_dir,
  1072. &acpi_video_device_EDID_fops,
  1073. acpi_driver_data(device));
  1074. if (!entry)
  1075. goto err_remove_brightness;
  1076. acpi_device_dir(device) = device_dir;
  1077. return 0;
  1078. err_remove_brightness:
  1079. remove_proc_entry("brightness", device_dir);
  1080. err_remove_state:
  1081. remove_proc_entry("state", device_dir);
  1082. err_remove_info:
  1083. remove_proc_entry("info", device_dir);
  1084. err_remove_dir:
  1085. remove_proc_entry(acpi_device_bid(device), vid_dev->video->dir);
  1086. return -ENOMEM;
  1087. }
  1088. static int acpi_video_device_remove_fs(struct acpi_device *device)
  1089. {
  1090. struct acpi_video_device *vid_dev;
  1091. struct proc_dir_entry *device_dir;
  1092. vid_dev = acpi_driver_data(device);
  1093. if (!vid_dev || !vid_dev->video || !vid_dev->video->dir)
  1094. return -ENODEV;
  1095. device_dir = acpi_device_dir(device);
  1096. if (device_dir) {
  1097. remove_proc_entry("info", device_dir);
  1098. remove_proc_entry("state", device_dir);
  1099. remove_proc_entry("brightness", device_dir);
  1100. remove_proc_entry("EDID", device_dir);
  1101. remove_proc_entry(acpi_device_bid(device), vid_dev->video->dir);
  1102. acpi_device_dir(device) = NULL;
  1103. }
  1104. return 0;
  1105. }
  1106. /* video bus */
  1107. static int acpi_video_bus_info_seq_show(struct seq_file *seq, void *offset)
  1108. {
  1109. struct acpi_video_bus *video = seq->private;
  1110. if (!video)
  1111. goto end;
  1112. seq_printf(seq, "Switching heads: %s\n",
  1113. video->flags.multihead ? "yes" : "no");
  1114. seq_printf(seq, "Video ROM: %s\n",
  1115. video->flags.rom ? "yes" : "no");
  1116. seq_printf(seq, "Device to be POSTed on boot: %s\n",
  1117. video->flags.post ? "yes" : "no");
  1118. end:
  1119. return 0;
  1120. }
  1121. static int acpi_video_bus_info_open_fs(struct inode *inode, struct file *file)
  1122. {
  1123. return single_open(file, acpi_video_bus_info_seq_show,
  1124. PDE(inode)->data);
  1125. }
  1126. static int acpi_video_bus_ROM_seq_show(struct seq_file *seq, void *offset)
  1127. {
  1128. struct acpi_video_bus *video = seq->private;
  1129. if (!video)
  1130. goto end;
  1131. printk(KERN_INFO PREFIX "Please implement %s\n", __func__);
  1132. seq_printf(seq, "<TODO>\n");
  1133. end:
  1134. return 0;
  1135. }
  1136. static int acpi_video_bus_ROM_open_fs(struct inode *inode, struct file *file)
  1137. {
  1138. return single_open(file, acpi_video_bus_ROM_seq_show, PDE(inode)->data);
  1139. }
  1140. static int acpi_video_bus_POST_info_seq_show(struct seq_file *seq, void *offset)
  1141. {
  1142. struct acpi_video_bus *video = seq->private;
  1143. unsigned long long options;
  1144. int status;
  1145. if (!video)
  1146. goto end;
  1147. status = acpi_video_bus_POST_options(video, &options);
  1148. if (ACPI_SUCCESS(status)) {
  1149. if (!(options & 1)) {
  1150. printk(KERN_WARNING PREFIX
  1151. "The motherboard VGA device is not listed as a possible POST device.\n");
  1152. printk(KERN_WARNING PREFIX
  1153. "This indicates a BIOS bug. Please contact the manufacturer.\n");
  1154. }
  1155. printk(KERN_WARNING "%llx\n", options);
  1156. seq_printf(seq, "can POST: <integrated video>");
  1157. if (options & 2)
  1158. seq_printf(seq, " <PCI video>");
  1159. if (options & 4)
  1160. seq_printf(seq, " <AGP video>");
  1161. seq_putc(seq, '\n');
  1162. } else
  1163. seq_printf(seq, "<not supported>\n");
  1164. end:
  1165. return 0;
  1166. }
  1167. static int
  1168. acpi_video_bus_POST_info_open_fs(struct inode *inode, struct file *file)
  1169. {
  1170. return single_open(file, acpi_video_bus_POST_info_seq_show,
  1171. PDE(inode)->data);
  1172. }
  1173. static int acpi_video_bus_POST_seq_show(struct seq_file *seq, void *offset)
  1174. {
  1175. struct acpi_video_bus *video = seq->private;
  1176. int status;
  1177. unsigned long long id;
  1178. if (!video)
  1179. goto end;
  1180. status = acpi_video_bus_get_POST(video, &id);
  1181. if (!ACPI_SUCCESS(status)) {
  1182. seq_printf(seq, "<not supported>\n");
  1183. goto end;
  1184. }
  1185. seq_printf(seq, "device POSTed is <%s>\n", device_decode[id & 3]);
  1186. end:
  1187. return 0;
  1188. }
  1189. static int acpi_video_bus_DOS_seq_show(struct seq_file *seq, void *offset)
  1190. {
  1191. struct acpi_video_bus *video = seq->private;
  1192. seq_printf(seq, "DOS setting: <%d>\n", video->dos_setting);
  1193. return 0;
  1194. }
  1195. static int acpi_video_bus_POST_open_fs(struct inode *inode, struct file *file)
  1196. {
  1197. return single_open(file, acpi_video_bus_POST_seq_show,
  1198. PDE(inode)->data);
  1199. }
  1200. static int acpi_video_bus_DOS_open_fs(struct inode *inode, struct file *file)
  1201. {
  1202. return single_open(file, acpi_video_bus_DOS_seq_show, PDE(inode)->data);
  1203. }
  1204. static ssize_t
  1205. acpi_video_bus_write_POST(struct file *file,
  1206. const char __user * buffer,
  1207. size_t count, loff_t * data)
  1208. {
  1209. int status;
  1210. struct seq_file *m = file->private_data;
  1211. struct acpi_video_bus *video = m->private;
  1212. char str[12] = { 0 };
  1213. unsigned long long opt, options;
  1214. if (!video || count + 1 > sizeof str)
  1215. return -EINVAL;
  1216. status = acpi_video_bus_POST_options(video, &options);
  1217. if (!ACPI_SUCCESS(status))
  1218. return -EINVAL;
  1219. if (copy_from_user(str, buffer, count))
  1220. return -EFAULT;
  1221. str[count] = 0;
  1222. opt = strtoul(str, NULL, 0);
  1223. if (opt > 3)
  1224. return -EFAULT;
  1225. /* just in case an OEM 'forgot' the motherboard... */
  1226. options |= 1;
  1227. if (options & (1ul << opt)) {
  1228. status = acpi_video_bus_set_POST(video, opt);
  1229. if (!ACPI_SUCCESS(status))
  1230. return -EFAULT;
  1231. }
  1232. return count;
  1233. }
  1234. static ssize_t
  1235. acpi_video_bus_write_DOS(struct file *file,
  1236. const char __user * buffer,
  1237. size_t count, loff_t * data)
  1238. {
  1239. int status;
  1240. struct seq_file *m = file->private_data;
  1241. struct acpi_video_bus *video = m->private;
  1242. char str[12] = { 0 };
  1243. unsigned long opt;
  1244. if (!video || count + 1 > sizeof str)
  1245. return -EINVAL;
  1246. if (copy_from_user(str, buffer, count))
  1247. return -EFAULT;
  1248. str[count] = 0;
  1249. opt = strtoul(str, NULL, 0);
  1250. if (opt > 7)
  1251. return -EFAULT;
  1252. status = acpi_video_bus_DOS(video, opt & 0x3, (opt & 0x4) >> 2);
  1253. if (!ACPI_SUCCESS(status))
  1254. return -EFAULT;
  1255. return count;
  1256. }
  1257. static int acpi_video_bus_add_fs(struct acpi_device *device)
  1258. {
  1259. struct acpi_video_bus *video = acpi_driver_data(device);
  1260. struct proc_dir_entry *device_dir;
  1261. struct proc_dir_entry *entry;
  1262. device_dir = proc_mkdir(acpi_device_bid(device), acpi_video_dir);
  1263. if (!device_dir)
  1264. return -ENOMEM;
  1265. /* 'info' [R] */
  1266. entry = proc_create_data("info", S_IRUGO, device_dir,
  1267. &acpi_video_bus_info_fops,
  1268. acpi_driver_data(device));
  1269. if (!entry)
  1270. goto err_remove_dir;
  1271. /* 'ROM' [R] */
  1272. entry = proc_create_data("ROM", S_IRUGO, device_dir,
  1273. &acpi_video_bus_ROM_fops,
  1274. acpi_driver_data(device));
  1275. if (!entry)
  1276. goto err_remove_info;
  1277. /* 'POST_info' [R] */
  1278. entry = proc_create_data("POST_info", S_IRUGO, device_dir,
  1279. &acpi_video_bus_POST_info_fops,
  1280. acpi_driver_data(device));
  1281. if (!entry)
  1282. goto err_remove_rom;
  1283. /* 'POST' [R/W] */
  1284. entry = proc_create_data("POST", S_IFREG | S_IRUGO | S_IWUSR,
  1285. device_dir,
  1286. &acpi_video_bus_POST_fops,
  1287. acpi_driver_data(device));
  1288. if (!entry)
  1289. goto err_remove_post_info;
  1290. /* 'DOS' [R/W] */
  1291. entry = proc_create_data("DOS", S_IFREG | S_IRUGO | S_IWUSR,
  1292. device_dir,
  1293. &acpi_video_bus_DOS_fops,
  1294. acpi_driver_data(device));
  1295. if (!entry)
  1296. goto err_remove_post;
  1297. video->dir = acpi_device_dir(device) = device_dir;
  1298. return 0;
  1299. err_remove_post:
  1300. remove_proc_entry("POST", device_dir);
  1301. err_remove_post_info:
  1302. remove_proc_entry("POST_info", device_dir);
  1303. err_remove_rom:
  1304. remove_proc_entry("ROM", device_dir);
  1305. err_remove_info:
  1306. remove_proc_entry("info", device_dir);
  1307. err_remove_dir:
  1308. remove_proc_entry(acpi_device_bid(device), acpi_video_dir);
  1309. return -ENOMEM;
  1310. }
  1311. static int acpi_video_bus_remove_fs(struct acpi_device *device)
  1312. {
  1313. struct proc_dir_entry *device_dir = acpi_device_dir(device);
  1314. if (device_dir) {
  1315. remove_proc_entry("info", device_dir);
  1316. remove_proc_entry("ROM", device_dir);
  1317. remove_proc_entry("POST_info", device_dir);
  1318. remove_proc_entry("POST", device_dir);
  1319. remove_proc_entry("DOS", device_dir);
  1320. remove_proc_entry(acpi_device_bid(device), acpi_video_dir);
  1321. acpi_device_dir(device) = NULL;
  1322. }
  1323. return 0;
  1324. }
  1325. /* --------------------------------------------------------------------------
  1326. Driver Interface
  1327. -------------------------------------------------------------------------- */
  1328. /* device interface */
  1329. static struct acpi_video_device_attrib*
  1330. acpi_video_get_device_attr(struct acpi_video_bus *video, unsigned long device_id)
  1331. {
  1332. struct acpi_video_enumerated_device *ids;
  1333. int i;
  1334. for (i = 0; i < video->attached_count; i++) {
  1335. ids = &video->attached_array[i];
  1336. if ((ids->value.int_val & 0xffff) == device_id)
  1337. return &ids->value.attrib;
  1338. }
  1339. return NULL;
  1340. }
  1341. static int
  1342. acpi_video_bus_get_one_device(struct acpi_device *device,
  1343. struct acpi_video_bus *video)
  1344. {
  1345. unsigned long long device_id;
  1346. int status;
  1347. struct acpi_video_device *data;
  1348. struct acpi_video_device_attrib* attribute;
  1349. if (!device || !video)
  1350. return -EINVAL;
  1351. status =
  1352. acpi_evaluate_integer(device->handle, "_ADR", NULL, &device_id);
  1353. if (ACPI_SUCCESS(status)) {
  1354. data = kzalloc(sizeof(struct acpi_video_device), GFP_KERNEL);
  1355. if (!data)
  1356. return -ENOMEM;
  1357. strcpy(acpi_device_name(device), ACPI_VIDEO_DEVICE_NAME);
  1358. strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
  1359. device->driver_data = data;
  1360. data->device_id = device_id;
  1361. data->video = video;
  1362. data->dev = device;
  1363. attribute = acpi_video_get_device_attr(video, device_id);
  1364. if((attribute != NULL) && attribute->device_id_scheme) {
  1365. switch (attribute->display_type) {
  1366. case ACPI_VIDEO_DISPLAY_CRT:
  1367. data->flags.crt = 1;
  1368. break;
  1369. case ACPI_VIDEO_DISPLAY_TV:
  1370. data->flags.tvout = 1;
  1371. break;
  1372. case ACPI_VIDEO_DISPLAY_DVI:
  1373. data->flags.dvi = 1;
  1374. break;
  1375. case ACPI_VIDEO_DISPLAY_LCD:
  1376. data->flags.lcd = 1;
  1377. break;
  1378. default:
  1379. data->flags.unknown = 1;
  1380. break;
  1381. }
  1382. if(attribute->bios_can_detect)
  1383. data->flags.bios = 1;
  1384. } else
  1385. data->flags.unknown = 1;
  1386. acpi_video_device_bind(video, data);
  1387. acpi_video_device_find_cap(data);
  1388. status = acpi_install_notify_handler(device->handle,
  1389. ACPI_DEVICE_NOTIFY,
  1390. acpi_video_device_notify,
  1391. data);
  1392. if (ACPI_FAILURE(status)) {
  1393. printk(KERN_ERR PREFIX
  1394. "Error installing notify handler\n");
  1395. if(data->brightness)
  1396. kfree(data->brightness->levels);
  1397. kfree(data->brightness);
  1398. kfree(data);
  1399. return -ENODEV;
  1400. }
  1401. mutex_lock(&video->device_list_lock);
  1402. list_add_tail(&data->entry, &video->video_device_list);
  1403. mutex_unlock(&video->device_list_lock);
  1404. acpi_video_device_add_fs(device);
  1405. return 0;
  1406. }
  1407. return -ENOENT;
  1408. }
  1409. /*
  1410. * Arg:
  1411. * video : video bus device
  1412. *
  1413. * Return:
  1414. * none
  1415. *
  1416. * Enumerate the video device list of the video bus,
  1417. * bind the ids with the corresponding video devices
  1418. * under the video bus.
  1419. */
  1420. static void acpi_video_device_rebind(struct acpi_video_bus *video)
  1421. {
  1422. struct acpi_video_device *dev;
  1423. mutex_lock(&video->device_list_lock);
  1424. list_for_each_entry(dev, &video->video_device_list, entry)
  1425. acpi_video_device_bind(video, dev);
  1426. mutex_unlock(&video->device_list_lock);
  1427. }
  1428. /*
  1429. * Arg:
  1430. * video : video bus device
  1431. * device : video output device under the video
  1432. * bus
  1433. *
  1434. * Return:
  1435. * none
  1436. *
  1437. * Bind the ids with the corresponding video devices
  1438. * under the video bus.
  1439. */
  1440. static void
  1441. acpi_video_device_bind(struct acpi_video_bus *video,
  1442. struct acpi_video_device *device)
  1443. {
  1444. struct acpi_video_enumerated_device *ids;
  1445. int i;
  1446. for (i = 0; i < video->attached_count; i++) {
  1447. ids = &video->attached_array[i];
  1448. if (device->device_id == (ids->value.int_val & 0xffff)) {
  1449. ids->bind_info = device;
  1450. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "device_bind %d\n", i));
  1451. }
  1452. }
  1453. }
  1454. /*
  1455. * Arg:
  1456. * video : video bus device
  1457. *
  1458. * Return:
  1459. * < 0 : error
  1460. *
  1461. * Call _DOD to enumerate all devices attached to display adapter
  1462. *
  1463. */
  1464. static int acpi_video_device_enumerate(struct acpi_video_bus *video)
  1465. {
  1466. int status;
  1467. int count;
  1468. int i;
  1469. struct acpi_video_enumerated_device *active_list;
  1470. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  1471. union acpi_object *dod = NULL;
  1472. union acpi_object *obj;
  1473. status = acpi_evaluate_object(video->device->handle, "_DOD", NULL, &buffer);
  1474. if (!ACPI_SUCCESS(status)) {
  1475. ACPI_EXCEPTION((AE_INFO, status, "Evaluating _DOD"));
  1476. return status;
  1477. }
  1478. dod = buffer.pointer;
  1479. if (!dod || (dod->type != ACPI_TYPE_PACKAGE)) {
  1480. ACPI_EXCEPTION((AE_INFO, status, "Invalid _DOD data"));
  1481. status = -EFAULT;
  1482. goto out;
  1483. }
  1484. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d video heads in _DOD\n",
  1485. dod->package.count));
  1486. active_list = kcalloc(1 + dod->package.count,
  1487. sizeof(struct acpi_video_enumerated_device),
  1488. GFP_KERNEL);
  1489. if (!active_list) {
  1490. status = -ENOMEM;
  1491. goto out;
  1492. }
  1493. count = 0;
  1494. for (i = 0; i < dod->package.count; i++) {
  1495. obj = &dod->package.elements[i];
  1496. if (obj->type != ACPI_TYPE_INTEGER) {
  1497. printk(KERN_ERR PREFIX
  1498. "Invalid _DOD data in element %d\n", i);
  1499. continue;
  1500. }
  1501. active_list[count].value.int_val = obj->integer.value;
  1502. active_list[count].bind_info = NULL;
  1503. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "dod element[%d] = %d\n", i,
  1504. (int)obj->integer.value));
  1505. count++;
  1506. }
  1507. kfree(video->attached_array);
  1508. video->attached_array = active_list;
  1509. video->attached_count = count;
  1510. out:
  1511. kfree(buffer.pointer);
  1512. return status;
  1513. }
  1514. static int
  1515. acpi_video_get_next_level(struct acpi_video_device *device,
  1516. u32 level_current, u32 event)
  1517. {
  1518. int min, max, min_above, max_below, i, l, delta = 255;
  1519. max = max_below = 0;
  1520. min = min_above = 255;
  1521. /* Find closest level to level_current */
  1522. for (i = 2; i < device->brightness->count; i++) {
  1523. l = device->brightness->levels[i];
  1524. if (abs(l - level_current) < abs(delta)) {
  1525. delta = l - level_current;
  1526. if (!delta)
  1527. break;
  1528. }
  1529. }
  1530. /* Ajust level_current to closest available level */
  1531. level_current += delta;
  1532. for (i = 2; i < device->brightness->count; i++) {
  1533. l = device->brightness->levels[i];
  1534. if (l < min)
  1535. min = l;
  1536. if (l > max)
  1537. max = l;
  1538. if (l < min_above && l > level_current)
  1539. min_above = l;
  1540. if (l > max_below && l < level_current)
  1541. max_below = l;
  1542. }
  1543. switch (event) {
  1544. case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS:
  1545. return (level_current < max) ? min_above : min;
  1546. case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS:
  1547. return (level_current < max) ? min_above : max;
  1548. case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS:
  1549. return (level_current > min) ? max_below : min;
  1550. case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS:
  1551. case ACPI_VIDEO_NOTIFY_DISPLAY_OFF:
  1552. return 0;
  1553. default:
  1554. return level_current;
  1555. }
  1556. }
  1557. static int
  1558. acpi_video_switch_brightness(struct acpi_video_device *device, int event)
  1559. {
  1560. unsigned long long level_current, level_next;
  1561. int result = -EINVAL;
  1562. if (!device->brightness)
  1563. goto out;
  1564. result = acpi_video_device_lcd_get_level_current(device,
  1565. &level_current);
  1566. if (result)
  1567. goto out;
  1568. level_next = acpi_video_get_next_level(device, level_current, event);
  1569. result = acpi_video_device_lcd_set_level(device, level_next);
  1570. out:
  1571. if (result)
  1572. printk(KERN_ERR PREFIX "Failed to switch the brightness\n");
  1573. return result;
  1574. }
  1575. static int
  1576. acpi_video_bus_get_devices(struct acpi_video_bus *video,
  1577. struct acpi_device *device)
  1578. {
  1579. int status = 0;
  1580. struct acpi_device *dev;
  1581. acpi_video_device_enumerate(video);
  1582. list_for_each_entry(dev, &device->children, node) {
  1583. status = acpi_video_bus_get_one_device(dev, video);
  1584. if (ACPI_FAILURE(status)) {
  1585. printk(KERN_WARNING PREFIX
  1586. "Cant attach device");
  1587. continue;
  1588. }
  1589. }
  1590. return status;
  1591. }
  1592. static int acpi_video_bus_put_one_device(struct acpi_video_device *device)
  1593. {
  1594. acpi_status status;
  1595. struct acpi_video_bus *video;
  1596. if (!device || !device->video)
  1597. return -ENOENT;
  1598. video = device->video;
  1599. acpi_video_device_remove_fs(device->dev);
  1600. status = acpi_remove_notify_handler(device->dev->handle,
  1601. ACPI_DEVICE_NOTIFY,
  1602. acpi_video_device_notify);
  1603. backlight_device_unregister(device->backlight);
  1604. if (device->cdev) {
  1605. sysfs_remove_link(&device->dev->dev.kobj,
  1606. "thermal_cooling");
  1607. sysfs_remove_link(&device->cdev->device.kobj,
  1608. "device");
  1609. thermal_cooling_device_unregister(device->cdev);
  1610. device->cdev = NULL;
  1611. }
  1612. video_output_unregister(device->output_dev);
  1613. return 0;
  1614. }
  1615. static int acpi_video_bus_put_devices(struct acpi_video_bus *video)
  1616. {
  1617. int status;
  1618. struct acpi_video_device *dev, *next;
  1619. mutex_lock(&video->device_list_lock);
  1620. list_for_each_entry_safe(dev, next, &video->video_device_list, entry) {
  1621. status = acpi_video_bus_put_one_device(dev);
  1622. if (ACPI_FAILURE(status))
  1623. printk(KERN_WARNING PREFIX
  1624. "hhuuhhuu bug in acpi video driver.\n");
  1625. if (dev->brightness) {
  1626. kfree(dev->brightness->levels);
  1627. kfree(dev->brightness);
  1628. }
  1629. list_del(&dev->entry);
  1630. kfree(dev);
  1631. }
  1632. mutex_unlock(&video->device_list_lock);
  1633. return 0;
  1634. }
  1635. /* acpi_video interface */
  1636. static int acpi_video_bus_start_devices(struct acpi_video_bus *video)
  1637. {
  1638. return acpi_video_bus_DOS(video, 0, 0);
  1639. }
  1640. static int acpi_video_bus_stop_devices(struct acpi_video_bus *video)
  1641. {
  1642. return acpi_video_bus_DOS(video, 0, 1);
  1643. }
  1644. static void acpi_video_bus_notify(struct acpi_device *device, u32 event)
  1645. {
  1646. struct acpi_video_bus *video = acpi_driver_data(device);
  1647. struct input_dev *input;
  1648. int keycode;
  1649. if (!video)
  1650. return;
  1651. input = video->input;
  1652. switch (event) {
  1653. case ACPI_VIDEO_NOTIFY_SWITCH: /* User requested a switch,
  1654. * most likely via hotkey. */
  1655. acpi_bus_generate_proc_event(device, event, 0);
  1656. keycode = KEY_SWITCHVIDEOMODE;
  1657. break;
  1658. case ACPI_VIDEO_NOTIFY_PROBE: /* User plugged in or removed a video
  1659. * connector. */
  1660. acpi_video_device_enumerate(video);
  1661. acpi_video_device_rebind(video);
  1662. acpi_bus_generate_proc_event(device, event, 0);
  1663. keycode = KEY_SWITCHVIDEOMODE;
  1664. break;
  1665. case ACPI_VIDEO_NOTIFY_CYCLE: /* Cycle Display output hotkey pressed. */
  1666. acpi_bus_generate_proc_event(device, event, 0);
  1667. keycode = KEY_SWITCHVIDEOMODE;
  1668. break;
  1669. case ACPI_VIDEO_NOTIFY_NEXT_OUTPUT: /* Next Display output hotkey pressed. */
  1670. acpi_bus_generate_proc_event(device, event, 0);
  1671. keycode = KEY_VIDEO_NEXT;
  1672. break;
  1673. case ACPI_VIDEO_NOTIFY_PREV_OUTPUT: /* previous Display output hotkey pressed. */
  1674. acpi_bus_generate_proc_event(device, event, 0);
  1675. keycode = KEY_VIDEO_PREV;
  1676. break;
  1677. default:
  1678. keycode = KEY_UNKNOWN;
  1679. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  1680. "Unsupported event [0x%x]\n", event));
  1681. break;
  1682. }
  1683. acpi_notifier_call_chain(device, event, 0);
  1684. input_report_key(input, keycode, 1);
  1685. input_sync(input);
  1686. input_report_key(input, keycode, 0);
  1687. input_sync(input);
  1688. return;
  1689. }
  1690. static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data)
  1691. {
  1692. struct acpi_video_device *video_device = data;
  1693. struct acpi_device *device = NULL;
  1694. struct acpi_video_bus *bus;
  1695. struct input_dev *input;
  1696. int keycode;
  1697. if (!video_device)
  1698. return;
  1699. device = video_device->dev;
  1700. bus = video_device->video;
  1701. input = bus->input;
  1702. switch (event) {
  1703. case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS: /* Cycle brightness */
  1704. if (brightness_switch_enabled)
  1705. acpi_video_switch_brightness(video_device, event);
  1706. acpi_bus_generate_proc_event(device, event, 0);
  1707. keycode = KEY_BRIGHTNESS_CYCLE;
  1708. break;
  1709. case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS: /* Increase brightness */
  1710. if (brightness_switch_enabled)
  1711. acpi_video_switch_brightness(video_device, event);
  1712. acpi_bus_generate_proc_event(device, event, 0);
  1713. keycode = KEY_BRIGHTNESSUP;
  1714. break;
  1715. case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS: /* Decrease brightness */
  1716. if (brightness_switch_enabled)
  1717. acpi_video_switch_brightness(video_device, event);
  1718. acpi_bus_generate_proc_event(device, event, 0);
  1719. keycode = KEY_BRIGHTNESSDOWN;
  1720. break;
  1721. case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS: /* zero brightnesss */
  1722. if (brightness_switch_enabled)
  1723. acpi_video_switch_brightness(video_device, event);
  1724. acpi_bus_generate_proc_event(device, event, 0);
  1725. keycode = KEY_BRIGHTNESS_ZERO;
  1726. break;
  1727. case ACPI_VIDEO_NOTIFY_DISPLAY_OFF: /* display device off */
  1728. if (brightness_switch_enabled)
  1729. acpi_video_switch_brightness(video_device, event);
  1730. acpi_bus_generate_proc_event(device, event, 0);
  1731. keycode = KEY_DISPLAY_OFF;
  1732. break;
  1733. default:
  1734. keycode = KEY_UNKNOWN;
  1735. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  1736. "Unsupported event [0x%x]\n", event));
  1737. break;
  1738. }
  1739. acpi_notifier_call_chain(device, event, 0);
  1740. input_report_key(input, keycode, 1);
  1741. input_sync(input);
  1742. input_report_key(input, keycode, 0);
  1743. input_sync(input);
  1744. return;
  1745. }
  1746. static int instance;
  1747. static int acpi_video_resume(struct acpi_device *device)
  1748. {
  1749. struct acpi_video_bus *video;
  1750. struct acpi_video_device *video_device;
  1751. int i;
  1752. if (!device || !acpi_driver_data(device))
  1753. return -EINVAL;
  1754. video = acpi_driver_data(device);
  1755. for (i = 0; i < video->attached_count; i++) {
  1756. video_device = video->attached_array[i].bind_info;
  1757. if (video_device && video_device->backlight)
  1758. acpi_video_set_brightness(video_device->backlight);
  1759. }
  1760. return AE_OK;
  1761. }
  1762. static int acpi_video_bus_add(struct acpi_device *device)
  1763. {
  1764. struct acpi_video_bus *video;
  1765. struct input_dev *input;
  1766. int error;
  1767. video = kzalloc(sizeof(struct acpi_video_bus), GFP_KERNEL);
  1768. if (!video)
  1769. return -ENOMEM;
  1770. /* a hack to fix the duplicate name "VID" problem on T61 */
  1771. if (!strcmp(device->pnp.bus_id, "VID")) {
  1772. if (instance)
  1773. device->pnp.bus_id[3] = '0' + instance;
  1774. instance ++;
  1775. }
  1776. /* a hack to fix the duplicate name "VGA" problem on Pa 3553 */
  1777. if (!strcmp(device->pnp.bus_id, "VGA")) {
  1778. if (instance)
  1779. device->pnp.bus_id[3] = '0' + instance;
  1780. instance++;
  1781. }
  1782. video->device = device;
  1783. strcpy(acpi_device_name(device), ACPI_VIDEO_BUS_NAME);
  1784. strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
  1785. device->driver_data = video;
  1786. acpi_video_bus_find_cap(video);
  1787. error = acpi_video_bus_check(video);
  1788. if (error)
  1789. goto err_free_video;
  1790. error = acpi_video_bus_add_fs(device);
  1791. if (error)
  1792. goto err_free_video;
  1793. mutex_init(&video->device_list_lock);
  1794. INIT_LIST_HEAD(&video->video_device_list);
  1795. acpi_video_bus_get_devices(video, device);
  1796. acpi_video_bus_start_devices(video);
  1797. video->input = input = input_allocate_device();
  1798. if (!input) {
  1799. error = -ENOMEM;
  1800. goto err_stop_video;
  1801. }
  1802. snprintf(video->phys, sizeof(video->phys),
  1803. "%s/video/input0", acpi_device_hid(video->device));
  1804. input->name = acpi_device_name(video->device);
  1805. input->phys = video->phys;
  1806. input->id.bustype = BUS_HOST;
  1807. input->id.product = 0x06;
  1808. input->dev.parent = &device->dev;
  1809. input->evbit[0] = BIT(EV_KEY);
  1810. set_bit(KEY_SWITCHVIDEOMODE, input->keybit);
  1811. set_bit(KEY_VIDEO_NEXT, input->keybit);
  1812. set_bit(KEY_VIDEO_PREV, input->keybit);
  1813. set_bit(KEY_BRIGHTNESS_CYCLE, input->keybit);
  1814. set_bit(KEY_BRIGHTNESSUP, input->keybit);
  1815. set_bit(KEY_BRIGHTNESSDOWN, input->keybit);
  1816. set_bit(KEY_BRIGHTNESS_ZERO, input->keybit);
  1817. set_bit(KEY_DISPLAY_OFF, input->keybit);
  1818. set_bit(KEY_UNKNOWN, input->keybit);
  1819. error = input_register_device(input);
  1820. if (error)
  1821. goto err_free_input_dev;
  1822. printk(KERN_INFO PREFIX "%s [%s] (multi-head: %s rom: %s post: %s)\n",
  1823. ACPI_VIDEO_DEVICE_NAME, acpi_device_bid(device),
  1824. video->flags.multihead ? "yes" : "no",
  1825. video->flags.rom ? "yes" : "no",
  1826. video->flags.post ? "yes" : "no");
  1827. return 0;
  1828. err_free_input_dev:
  1829. input_free_device(input);
  1830. err_stop_video:
  1831. acpi_video_bus_stop_devices(video);
  1832. acpi_video_bus_put_devices(video);
  1833. kfree(video->attached_array);
  1834. acpi_video_bus_remove_fs(device);
  1835. err_free_video:
  1836. kfree(video);
  1837. device->driver_data = NULL;
  1838. return error;
  1839. }
  1840. static int acpi_video_bus_remove(struct acpi_device *device, int type)
  1841. {
  1842. struct acpi_video_bus *video = NULL;
  1843. if (!device || !acpi_driver_data(device))
  1844. return -EINVAL;
  1845. video = acpi_driver_data(device);
  1846. acpi_video_bus_stop_devices(video);
  1847. acpi_video_bus_put_devices(video);
  1848. acpi_video_bus_remove_fs(device);
  1849. input_unregister_device(video->input);
  1850. kfree(video->attached_array);
  1851. kfree(video);
  1852. return 0;
  1853. }
  1854. static int __init intel_opregion_present(void)
  1855. {
  1856. #if defined(CONFIG_DRM_I915) || defined(CONFIG_DRM_I915_MODULE)
  1857. struct pci_dev *dev = NULL;
  1858. u32 address;
  1859. for_each_pci_dev(dev) {
  1860. if ((dev->class >> 8) != PCI_CLASS_DISPLAY_VGA)
  1861. continue;
  1862. if (dev->vendor != PCI_VENDOR_ID_INTEL)
  1863. continue;
  1864. pci_read_config_dword(dev, 0xfc, &address);
  1865. if (!address)
  1866. continue;
  1867. return 1;
  1868. }
  1869. #endif
  1870. return 0;
  1871. }
  1872. int acpi_video_register(void)
  1873. {
  1874. int result = 0;
  1875. acpi_video_dir = proc_mkdir(ACPI_VIDEO_CLASS, acpi_root_dir);
  1876. if (!acpi_video_dir)
  1877. return -ENODEV;
  1878. result = acpi_bus_register_driver(&acpi_video_bus);
  1879. if (result < 0) {
  1880. remove_proc_entry(ACPI_VIDEO_CLASS, acpi_root_dir);
  1881. return -ENODEV;
  1882. }
  1883. return 0;
  1884. }
  1885. EXPORT_SYMBOL(acpi_video_register);
  1886. /*
  1887. * This is kind of nasty. Hardware using Intel chipsets may require
  1888. * the video opregion code to be run first in order to initialise
  1889. * state before any ACPI video calls are made. To handle this we defer
  1890. * registration of the video class until the opregion code has run.
  1891. */
  1892. static int __init acpi_video_init(void)
  1893. {
  1894. if (intel_opregion_present())
  1895. return 0;
  1896. return acpi_video_register();
  1897. }
  1898. static void __exit acpi_video_exit(void)
  1899. {
  1900. acpi_bus_unregister_driver(&acpi_video_bus);
  1901. remove_proc_entry(ACPI_VIDEO_CLASS, acpi_root_dir);
  1902. return;
  1903. }
  1904. module_init(acpi_video_init);
  1905. module_exit(acpi_video_exit);