video.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921
  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/proc_fs.h>
  32. #include <linux/seq_file.h>
  33. #include <linux/backlight.h>
  34. #include <asm/uaccess.h>
  35. #include <acpi/acpi_bus.h>
  36. #include <acpi/acpi_drivers.h>
  37. #define ACPI_VIDEO_COMPONENT 0x08000000
  38. #define ACPI_VIDEO_CLASS "video"
  39. #define ACPI_VIDEO_DRIVER_NAME "ACPI Video Driver"
  40. #define ACPI_VIDEO_BUS_NAME "Video Bus"
  41. #define ACPI_VIDEO_DEVICE_NAME "Video Device"
  42. #define ACPI_VIDEO_NOTIFY_SWITCH 0x80
  43. #define ACPI_VIDEO_NOTIFY_PROBE 0x81
  44. #define ACPI_VIDEO_NOTIFY_CYCLE 0x82
  45. #define ACPI_VIDEO_NOTIFY_NEXT_OUTPUT 0x83
  46. #define ACPI_VIDEO_NOTIFY_PREV_OUTPUT 0x84
  47. #define ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS 0x85
  48. #define ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS 0x86
  49. #define ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS 0x87
  50. #define ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS 0x88
  51. #define ACPI_VIDEO_NOTIFY_DISPLAY_OFF 0x89
  52. #define ACPI_VIDEO_HEAD_INVALID (~0u - 1)
  53. #define ACPI_VIDEO_HEAD_END (~0u)
  54. #define MAX_NAME_LEN 20
  55. #define ACPI_VIDEO_DISPLAY_CRT 1
  56. #define ACPI_VIDEO_DISPLAY_TV 2
  57. #define ACPI_VIDEO_DISPLAY_DVI 3
  58. #define ACPI_VIDEO_DISPLAY_LCD 4
  59. #define _COMPONENT ACPI_VIDEO_COMPONENT
  60. ACPI_MODULE_NAME("acpi_video")
  61. MODULE_AUTHOR("Bruno Ducrot");
  62. MODULE_DESCRIPTION(ACPI_VIDEO_DRIVER_NAME);
  63. MODULE_LICENSE("GPL");
  64. static int acpi_video_bus_add(struct acpi_device *device);
  65. static int acpi_video_bus_remove(struct acpi_device *device, int type);
  66. static struct acpi_driver acpi_video_bus = {
  67. .name = ACPI_VIDEO_DRIVER_NAME,
  68. .class = ACPI_VIDEO_CLASS,
  69. .ids = ACPI_VIDEO_HID,
  70. .ops = {
  71. .add = acpi_video_bus_add,
  72. .remove = acpi_video_bus_remove,
  73. },
  74. };
  75. struct acpi_video_bus_flags {
  76. u8 multihead:1; /* can switch video heads */
  77. u8 rom:1; /* can retrieve a video rom */
  78. u8 post:1; /* can configure the head to */
  79. u8 reserved:5;
  80. };
  81. struct acpi_video_bus_cap {
  82. u8 _DOS:1; /*Enable/Disable output switching */
  83. u8 _DOD:1; /*Enumerate all devices attached to display adapter */
  84. u8 _ROM:1; /*Get ROM Data */
  85. u8 _GPD:1; /*Get POST Device */
  86. u8 _SPD:1; /*Set POST Device */
  87. u8 _VPO:1; /*Video POST Options */
  88. u8 reserved:2;
  89. };
  90. struct acpi_video_device_attrib {
  91. u32 display_index:4; /* A zero-based instance of the Display */
  92. u32 display_port_attachment:4; /*This field differenates displays type */
  93. u32 display_type:4; /*Describe the specific type in use */
  94. u32 vendor_specific:4; /*Chipset Vendor Specifi */
  95. u32 bios_can_detect:1; /*BIOS can detect the device */
  96. u32 depend_on_vga:1; /*Non-VGA output device whose power is related to
  97. the VGA device. */
  98. u32 pipe_id:3; /*For VGA multiple-head devices. */
  99. u32 reserved:10; /*Must be 0 */
  100. u32 device_id_scheme:1; /*Device ID Scheme */
  101. };
  102. struct acpi_video_enumerated_device {
  103. union {
  104. u32 int_val;
  105. struct acpi_video_device_attrib attrib;
  106. } value;
  107. struct acpi_video_device *bind_info;
  108. };
  109. struct acpi_video_bus {
  110. struct acpi_device *device;
  111. u8 dos_setting;
  112. struct acpi_video_enumerated_device *attached_array;
  113. u8 attached_count;
  114. struct acpi_video_bus_cap cap;
  115. struct acpi_video_bus_flags flags;
  116. struct semaphore sem;
  117. struct list_head video_device_list;
  118. struct proc_dir_entry *dir;
  119. };
  120. struct acpi_video_device_flags {
  121. u8 crt:1;
  122. u8 lcd:1;
  123. u8 tvout:1;
  124. u8 dvi:1;
  125. u8 bios:1;
  126. u8 unknown:1;
  127. u8 reserved:2;
  128. };
  129. struct acpi_video_device_cap {
  130. u8 _ADR:1; /*Return the unique ID */
  131. u8 _BCL:1; /*Query list of brightness control levels supported */
  132. u8 _BCM:1; /*Set the brightness level */
  133. u8 _BQC:1; /* Get current brightness level */
  134. u8 _DDC:1; /*Return the EDID for this device */
  135. u8 _DCS:1; /*Return status of output device */
  136. u8 _DGS:1; /*Query graphics state */
  137. u8 _DSS:1; /*Device state set */
  138. };
  139. struct acpi_video_device_brightness {
  140. int curr;
  141. int count;
  142. int *levels;
  143. };
  144. struct acpi_video_device {
  145. unsigned long device_id;
  146. struct acpi_video_device_flags flags;
  147. struct acpi_video_device_cap cap;
  148. struct list_head entry;
  149. struct acpi_video_bus *video;
  150. struct acpi_device *dev;
  151. struct acpi_video_device_brightness *brightness;
  152. struct backlight_device *backlight;
  153. struct backlight_properties *data;
  154. };
  155. /* bus */
  156. static int acpi_video_bus_info_open_fs(struct inode *inode, struct file *file);
  157. static struct file_operations acpi_video_bus_info_fops = {
  158. .open = acpi_video_bus_info_open_fs,
  159. .read = seq_read,
  160. .llseek = seq_lseek,
  161. .release = single_release,
  162. };
  163. static int acpi_video_bus_ROM_open_fs(struct inode *inode, struct file *file);
  164. static struct file_operations acpi_video_bus_ROM_fops = {
  165. .open = acpi_video_bus_ROM_open_fs,
  166. .read = seq_read,
  167. .llseek = seq_lseek,
  168. .release = single_release,
  169. };
  170. static int acpi_video_bus_POST_info_open_fs(struct inode *inode,
  171. struct file *file);
  172. static struct file_operations acpi_video_bus_POST_info_fops = {
  173. .open = acpi_video_bus_POST_info_open_fs,
  174. .read = seq_read,
  175. .llseek = seq_lseek,
  176. .release = single_release,
  177. };
  178. static int acpi_video_bus_POST_open_fs(struct inode *inode, struct file *file);
  179. static struct file_operations acpi_video_bus_POST_fops = {
  180. .open = acpi_video_bus_POST_open_fs,
  181. .read = seq_read,
  182. .llseek = seq_lseek,
  183. .release = single_release,
  184. };
  185. static int acpi_video_bus_DOS_open_fs(struct inode *inode, struct file *file);
  186. static struct file_operations acpi_video_bus_DOS_fops = {
  187. .open = acpi_video_bus_DOS_open_fs,
  188. .read = seq_read,
  189. .llseek = seq_lseek,
  190. .release = single_release,
  191. };
  192. /* device */
  193. static int acpi_video_device_info_open_fs(struct inode *inode,
  194. struct file *file);
  195. static struct file_operations acpi_video_device_info_fops = {
  196. .open = acpi_video_device_info_open_fs,
  197. .read = seq_read,
  198. .llseek = seq_lseek,
  199. .release = single_release,
  200. };
  201. static int acpi_video_device_state_open_fs(struct inode *inode,
  202. struct file *file);
  203. static struct file_operations acpi_video_device_state_fops = {
  204. .open = acpi_video_device_state_open_fs,
  205. .read = seq_read,
  206. .llseek = seq_lseek,
  207. .release = single_release,
  208. };
  209. static int acpi_video_device_brightness_open_fs(struct inode *inode,
  210. struct file *file);
  211. static struct file_operations acpi_video_device_brightness_fops = {
  212. .open = acpi_video_device_brightness_open_fs,
  213. .read = seq_read,
  214. .llseek = seq_lseek,
  215. .release = single_release,
  216. };
  217. static int acpi_video_device_EDID_open_fs(struct inode *inode,
  218. struct file *file);
  219. static struct file_operations acpi_video_device_EDID_fops = {
  220. .open = acpi_video_device_EDID_open_fs,
  221. .read = seq_read,
  222. .llseek = seq_lseek,
  223. .release = single_release,
  224. };
  225. static char device_decode[][30] = {
  226. "motherboard VGA device",
  227. "PCI VGA device",
  228. "AGP VGA device",
  229. "UNKNOWN",
  230. };
  231. static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data);
  232. static void acpi_video_device_rebind(struct acpi_video_bus *video);
  233. static void acpi_video_device_bind(struct acpi_video_bus *video,
  234. struct acpi_video_device *device);
  235. static int acpi_video_device_enumerate(struct acpi_video_bus *video);
  236. static int acpi_video_switch_output(struct acpi_video_bus *video, int event);
  237. static int acpi_video_device_lcd_set_level(struct acpi_video_device *device,
  238. int level);
  239. static int acpi_video_device_lcd_get_level_current(
  240. struct acpi_video_device *device,
  241. unsigned long *level);
  242. static int acpi_video_get_next_level(struct acpi_video_device *device,
  243. u32 level_current, u32 event);
  244. static void acpi_video_switch_brightness(struct acpi_video_device *device,
  245. int event);
  246. /*backlight device sysfs support*/
  247. static int acpi_video_get_brightness(struct backlight_device *bd)
  248. {
  249. unsigned long cur_level;
  250. struct acpi_video_device *vd =
  251. (struct acpi_video_device *)class_get_devdata(&bd->class_dev);
  252. acpi_video_device_lcd_get_level_current(vd, &cur_level);
  253. return (int) cur_level;
  254. }
  255. static int acpi_video_set_brightness(struct backlight_device *bd)
  256. {
  257. int request_level = bd->props->brightness;
  258. struct acpi_video_device *vd =
  259. (struct acpi_video_device *)class_get_devdata(&bd->class_dev);
  260. acpi_video_device_lcd_set_level(vd, request_level);
  261. return 0;
  262. }
  263. /* --------------------------------------------------------------------------
  264. Video Management
  265. -------------------------------------------------------------------------- */
  266. /* device */
  267. static int
  268. acpi_video_device_query(struct acpi_video_device *device, unsigned long *state)
  269. {
  270. int status;
  271. status = acpi_evaluate_integer(device->dev->handle, "_DGS", NULL, state);
  272. return status;
  273. }
  274. static int
  275. acpi_video_device_get_state(struct acpi_video_device *device,
  276. unsigned long *state)
  277. {
  278. int status;
  279. status = acpi_evaluate_integer(device->dev->handle, "_DCS", NULL, state);
  280. return status;
  281. }
  282. static int
  283. acpi_video_device_set_state(struct acpi_video_device *device, int state)
  284. {
  285. int status;
  286. union acpi_object arg0 = { ACPI_TYPE_INTEGER };
  287. struct acpi_object_list args = { 1, &arg0 };
  288. unsigned long ret;
  289. arg0.integer.value = state;
  290. status = acpi_evaluate_integer(device->dev->handle, "_DSS", &args, &ret);
  291. return status;
  292. }
  293. static int
  294. acpi_video_device_lcd_query_levels(struct acpi_video_device *device,
  295. union acpi_object **levels)
  296. {
  297. int status;
  298. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  299. union acpi_object *obj;
  300. *levels = NULL;
  301. status = acpi_evaluate_object(device->dev->handle, "_BCL", NULL, &buffer);
  302. if (!ACPI_SUCCESS(status))
  303. return status;
  304. obj = (union acpi_object *)buffer.pointer;
  305. if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
  306. printk(KERN_ERR PREFIX "Invalid _BCL data\n");
  307. status = -EFAULT;
  308. goto err;
  309. }
  310. *levels = obj;
  311. return 0;
  312. err:
  313. kfree(buffer.pointer);
  314. return status;
  315. }
  316. static int
  317. acpi_video_device_lcd_set_level(struct acpi_video_device *device, int level)
  318. {
  319. int status;
  320. union acpi_object arg0 = { ACPI_TYPE_INTEGER };
  321. struct acpi_object_list args = { 1, &arg0 };
  322. arg0.integer.value = level;
  323. status = acpi_evaluate_object(device->dev->handle, "_BCM", &args, NULL);
  324. printk(KERN_DEBUG "set_level status: %x\n", status);
  325. return status;
  326. }
  327. static int
  328. acpi_video_device_lcd_get_level_current(struct acpi_video_device *device,
  329. unsigned long *level)
  330. {
  331. int status;
  332. status = acpi_evaluate_integer(device->dev->handle, "_BQC", NULL, level);
  333. return status;
  334. }
  335. static int
  336. acpi_video_device_EDID(struct acpi_video_device *device,
  337. union acpi_object **edid, ssize_t length)
  338. {
  339. int status;
  340. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  341. union acpi_object *obj;
  342. union acpi_object arg0 = { ACPI_TYPE_INTEGER };
  343. struct acpi_object_list args = { 1, &arg0 };
  344. *edid = NULL;
  345. if (!device)
  346. return -ENODEV;
  347. if (length == 128)
  348. arg0.integer.value = 1;
  349. else if (length == 256)
  350. arg0.integer.value = 2;
  351. else
  352. return -EINVAL;
  353. status = acpi_evaluate_object(device->dev->handle, "_DDC", &args, &buffer);
  354. if (ACPI_FAILURE(status))
  355. return -ENODEV;
  356. obj = buffer.pointer;
  357. if (obj && obj->type == ACPI_TYPE_BUFFER)
  358. *edid = obj;
  359. else {
  360. printk(KERN_ERR PREFIX "Invalid _DDC data\n");
  361. status = -EFAULT;
  362. kfree(obj);
  363. }
  364. return status;
  365. }
  366. /* bus */
  367. static int
  368. acpi_video_bus_set_POST(struct acpi_video_bus *video, unsigned long option)
  369. {
  370. int status;
  371. unsigned long tmp;
  372. union acpi_object arg0 = { ACPI_TYPE_INTEGER };
  373. struct acpi_object_list args = { 1, &arg0 };
  374. arg0.integer.value = option;
  375. status = acpi_evaluate_integer(video->device->handle, "_SPD", &args, &tmp);
  376. if (ACPI_SUCCESS(status))
  377. status = tmp ? (-EINVAL) : (AE_OK);
  378. return status;
  379. }
  380. static int
  381. acpi_video_bus_get_POST(struct acpi_video_bus *video, unsigned long *id)
  382. {
  383. int status;
  384. status = acpi_evaluate_integer(video->device->handle, "_GPD", NULL, id);
  385. return status;
  386. }
  387. static int
  388. acpi_video_bus_POST_options(struct acpi_video_bus *video,
  389. unsigned long *options)
  390. {
  391. int status;
  392. status = acpi_evaluate_integer(video->device->handle, "_VPO", NULL, options);
  393. *options &= 3;
  394. return status;
  395. }
  396. /*
  397. * Arg:
  398. * video : video bus device pointer
  399. * bios_flag :
  400. * 0. The system BIOS should NOT automatically switch(toggle)
  401. * the active display output.
  402. * 1. The system BIOS should automatically switch (toggle) the
  403. * active display output. No swich event.
  404. * 2. The _DGS value should be locked.
  405. * 3. The system BIOS should not automatically switch (toggle) the
  406. * active display output, but instead generate the display switch
  407. * event notify code.
  408. * lcd_flag :
  409. * 0. The system BIOS should automatically control the brightness level
  410. * of the LCD, when the power changes from AC to DC
  411. * 1. The system BIOS should NOT automatically control the brightness
  412. * level of the LCD, when the power changes from AC to DC.
  413. * Return Value:
  414. * -1 wrong arg.
  415. */
  416. static int
  417. acpi_video_bus_DOS(struct acpi_video_bus *video, int bios_flag, int lcd_flag)
  418. {
  419. acpi_integer status = 0;
  420. union acpi_object arg0 = { ACPI_TYPE_INTEGER };
  421. struct acpi_object_list args = { 1, &arg0 };
  422. if (bios_flag < 0 || bios_flag > 3 || lcd_flag < 0 || lcd_flag > 1) {
  423. status = -1;
  424. goto Failed;
  425. }
  426. arg0.integer.value = (lcd_flag << 2) | bios_flag;
  427. video->dos_setting = arg0.integer.value;
  428. acpi_evaluate_object(video->device->handle, "_DOS", &args, NULL);
  429. Failed:
  430. return status;
  431. }
  432. /*
  433. * Arg:
  434. * device : video output device (LCD, CRT, ..)
  435. *
  436. * Return Value:
  437. * None
  438. *
  439. * Find out all required AML method defined under the output
  440. * device.
  441. */
  442. static void acpi_video_device_find_cap(struct acpi_video_device *device)
  443. {
  444. acpi_integer status;
  445. acpi_handle h_dummy1;
  446. int i;
  447. u32 max_level = 0;
  448. union acpi_object *obj = NULL;
  449. struct acpi_video_device_brightness *br = NULL;
  450. memset(&device->cap, 0, 4);
  451. if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_ADR", &h_dummy1))) {
  452. device->cap._ADR = 1;
  453. }
  454. if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_BCL", &h_dummy1))) {
  455. device->cap._BCL = 1;
  456. }
  457. if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_BCM", &h_dummy1))) {
  458. device->cap._BCM = 1;
  459. }
  460. if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle,"_BQC",&h_dummy1)))
  461. device->cap._BQC = 1;
  462. if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DDC", &h_dummy1))) {
  463. device->cap._DDC = 1;
  464. }
  465. if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DCS", &h_dummy1))) {
  466. device->cap._DCS = 1;
  467. }
  468. if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DGS", &h_dummy1))) {
  469. device->cap._DGS = 1;
  470. }
  471. if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DSS", &h_dummy1))) {
  472. device->cap._DSS = 1;
  473. }
  474. status = acpi_video_device_lcd_query_levels(device, &obj);
  475. if (obj && obj->type == ACPI_TYPE_PACKAGE && obj->package.count >= 2) {
  476. int count = 0;
  477. union acpi_object *o;
  478. br = kzalloc(sizeof(*br), GFP_KERNEL);
  479. if (!br) {
  480. printk(KERN_ERR "can't allocate memory\n");
  481. } else {
  482. br->levels = kmalloc(obj->package.count *
  483. sizeof *(br->levels), GFP_KERNEL);
  484. if (!br->levels)
  485. goto out;
  486. for (i = 0; i < obj->package.count; i++) {
  487. o = (union acpi_object *)&obj->package.
  488. elements[i];
  489. if (o->type != ACPI_TYPE_INTEGER) {
  490. printk(KERN_ERR PREFIX "Invalid data\n");
  491. continue;
  492. }
  493. br->levels[count] = (u32) o->integer.value;
  494. if (br->levels[count] > max_level)
  495. max_level = br->levels[count];
  496. count++;
  497. }
  498. out:
  499. if (count < 2) {
  500. kfree(br->levels);
  501. kfree(br);
  502. } else {
  503. br->count = count;
  504. device->brightness = br;
  505. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  506. "found %d brightness levels\n",
  507. count));
  508. }
  509. }
  510. }
  511. kfree(obj);
  512. if (device->cap._BCL && device->cap._BCM && device->cap._BQC){
  513. unsigned long tmp;
  514. static int count = 0;
  515. char *name;
  516. struct backlight_properties *acpi_video_data;
  517. name = kzalloc(MAX_NAME_LEN, GFP_KERNEL);
  518. if (!name)
  519. return;
  520. acpi_video_data = kzalloc(
  521. sizeof(struct backlight_properties),
  522. GFP_KERNEL);
  523. if (!acpi_video_data){
  524. kfree(name);
  525. return;
  526. }
  527. acpi_video_data->owner = THIS_MODULE;
  528. acpi_video_data->get_brightness =
  529. acpi_video_get_brightness;
  530. acpi_video_data->update_status =
  531. acpi_video_set_brightness;
  532. sprintf(name, "acpi_video%d", count++);
  533. device->data = acpi_video_data;
  534. acpi_video_data->max_brightness = max_level;
  535. acpi_video_device_lcd_get_level_current(device, &tmp);
  536. acpi_video_data->brightness = (int)tmp;
  537. device->backlight = backlight_device_register(name,
  538. NULL, device, acpi_video_data);
  539. kfree(name);
  540. }
  541. return;
  542. }
  543. /*
  544. * Arg:
  545. * device : video output device (VGA)
  546. *
  547. * Return Value:
  548. * None
  549. *
  550. * Find out all required AML method defined under the video bus device.
  551. */
  552. static void acpi_video_bus_find_cap(struct acpi_video_bus *video)
  553. {
  554. acpi_handle h_dummy1;
  555. memset(&video->cap, 0, 4);
  556. if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_DOS", &h_dummy1))) {
  557. video->cap._DOS = 1;
  558. }
  559. if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_DOD", &h_dummy1))) {
  560. video->cap._DOD = 1;
  561. }
  562. if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_ROM", &h_dummy1))) {
  563. video->cap._ROM = 1;
  564. }
  565. if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_GPD", &h_dummy1))) {
  566. video->cap._GPD = 1;
  567. }
  568. if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_SPD", &h_dummy1))) {
  569. video->cap._SPD = 1;
  570. }
  571. if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_VPO", &h_dummy1))) {
  572. video->cap._VPO = 1;
  573. }
  574. }
  575. /*
  576. * Check whether the video bus device has required AML method to
  577. * support the desired features
  578. */
  579. static int acpi_video_bus_check(struct acpi_video_bus *video)
  580. {
  581. acpi_status status = -ENOENT;
  582. if (!video)
  583. return -EINVAL;
  584. /* Since there is no HID, CID and so on for VGA driver, we have
  585. * to check well known required nodes.
  586. */
  587. /* Does this device able to support video switching ? */
  588. if (video->cap._DOS) {
  589. video->flags.multihead = 1;
  590. status = 0;
  591. }
  592. /* Does this device able to retrieve a retrieve a video ROM ? */
  593. if (video->cap._ROM) {
  594. video->flags.rom = 1;
  595. status = 0;
  596. }
  597. /* Does this device able to configure which video device to POST ? */
  598. if (video->cap._GPD && video->cap._SPD && video->cap._VPO) {
  599. video->flags.post = 1;
  600. status = 0;
  601. }
  602. return status;
  603. }
  604. /* --------------------------------------------------------------------------
  605. FS Interface (/proc)
  606. -------------------------------------------------------------------------- */
  607. static struct proc_dir_entry *acpi_video_dir;
  608. /* video devices */
  609. static int acpi_video_device_info_seq_show(struct seq_file *seq, void *offset)
  610. {
  611. struct acpi_video_device *dev = seq->private;
  612. if (!dev)
  613. goto end;
  614. seq_printf(seq, "device_id: 0x%04x\n", (u32) dev->device_id);
  615. seq_printf(seq, "type: ");
  616. if (dev->flags.crt)
  617. seq_printf(seq, "CRT\n");
  618. else if (dev->flags.lcd)
  619. seq_printf(seq, "LCD\n");
  620. else if (dev->flags.tvout)
  621. seq_printf(seq, "TVOUT\n");
  622. else if (dev->flags.dvi)
  623. seq_printf(seq, "DVI\n");
  624. else
  625. seq_printf(seq, "UNKNOWN\n");
  626. seq_printf(seq, "known by bios: %s\n", dev->flags.bios ? "yes" : "no");
  627. end:
  628. return 0;
  629. }
  630. static int
  631. acpi_video_device_info_open_fs(struct inode *inode, struct file *file)
  632. {
  633. return single_open(file, acpi_video_device_info_seq_show,
  634. PDE(inode)->data);
  635. }
  636. static int acpi_video_device_state_seq_show(struct seq_file *seq, void *offset)
  637. {
  638. int status;
  639. struct acpi_video_device *dev = seq->private;
  640. unsigned long state;
  641. if (!dev)
  642. goto end;
  643. status = acpi_video_device_get_state(dev, &state);
  644. seq_printf(seq, "state: ");
  645. if (ACPI_SUCCESS(status))
  646. seq_printf(seq, "0x%02lx\n", state);
  647. else
  648. seq_printf(seq, "<not supported>\n");
  649. status = acpi_video_device_query(dev, &state);
  650. seq_printf(seq, "query: ");
  651. if (ACPI_SUCCESS(status))
  652. seq_printf(seq, "0x%02lx\n", state);
  653. else
  654. seq_printf(seq, "<not supported>\n");
  655. end:
  656. return 0;
  657. }
  658. static int
  659. acpi_video_device_state_open_fs(struct inode *inode, struct file *file)
  660. {
  661. return single_open(file, acpi_video_device_state_seq_show,
  662. PDE(inode)->data);
  663. }
  664. static ssize_t
  665. acpi_video_device_write_state(struct file *file,
  666. const char __user * buffer,
  667. size_t count, loff_t * data)
  668. {
  669. int status;
  670. struct seq_file *m = file->private_data;
  671. struct acpi_video_device *dev = m->private;
  672. char str[12] = { 0 };
  673. u32 state = 0;
  674. if (!dev || count + 1 > sizeof str)
  675. return -EINVAL;
  676. if (copy_from_user(str, buffer, count))
  677. return -EFAULT;
  678. str[count] = 0;
  679. state = simple_strtoul(str, NULL, 0);
  680. state &= ((1ul << 31) | (1ul << 30) | (1ul << 0));
  681. status = acpi_video_device_set_state(dev, state);
  682. if (status)
  683. return -EFAULT;
  684. return count;
  685. }
  686. static int
  687. acpi_video_device_brightness_seq_show(struct seq_file *seq, void *offset)
  688. {
  689. struct acpi_video_device *dev = seq->private;
  690. int i;
  691. if (!dev || !dev->brightness) {
  692. seq_printf(seq, "<not supported>\n");
  693. return 0;
  694. }
  695. seq_printf(seq, "levels: ");
  696. for (i = 0; i < dev->brightness->count; i++)
  697. seq_printf(seq, " %d", dev->brightness->levels[i]);
  698. seq_printf(seq, "\ncurrent: %d\n", dev->brightness->curr);
  699. return 0;
  700. }
  701. static int
  702. acpi_video_device_brightness_open_fs(struct inode *inode, struct file *file)
  703. {
  704. return single_open(file, acpi_video_device_brightness_seq_show,
  705. PDE(inode)->data);
  706. }
  707. static ssize_t
  708. acpi_video_device_write_brightness(struct file *file,
  709. const char __user * buffer,
  710. size_t count, loff_t * data)
  711. {
  712. struct seq_file *m = file->private_data;
  713. struct acpi_video_device *dev = m->private;
  714. char str[4] = { 0 };
  715. unsigned int level = 0;
  716. int i;
  717. if (!dev || !dev->brightness || count + 1 > sizeof str)
  718. return -EINVAL;
  719. if (copy_from_user(str, buffer, count))
  720. return -EFAULT;
  721. str[count] = 0;
  722. level = simple_strtoul(str, NULL, 0);
  723. if (level > 100)
  724. return -EFAULT;
  725. /* validate though the list of available levels */
  726. for (i = 0; i < dev->brightness->count; i++)
  727. if (level == dev->brightness->levels[i]) {
  728. if (ACPI_SUCCESS
  729. (acpi_video_device_lcd_set_level(dev, level)))
  730. dev->brightness->curr = level;
  731. break;
  732. }
  733. return count;
  734. }
  735. static int acpi_video_device_EDID_seq_show(struct seq_file *seq, void *offset)
  736. {
  737. struct acpi_video_device *dev = seq->private;
  738. int status;
  739. int i;
  740. union acpi_object *edid = NULL;
  741. if (!dev)
  742. goto out;
  743. status = acpi_video_device_EDID(dev, &edid, 128);
  744. if (ACPI_FAILURE(status)) {
  745. status = acpi_video_device_EDID(dev, &edid, 256);
  746. }
  747. if (ACPI_FAILURE(status)) {
  748. goto out;
  749. }
  750. if (edid && edid->type == ACPI_TYPE_BUFFER) {
  751. for (i = 0; i < edid->buffer.length; i++)
  752. seq_putc(seq, edid->buffer.pointer[i]);
  753. }
  754. out:
  755. if (!edid)
  756. seq_printf(seq, "<not supported>\n");
  757. else
  758. kfree(edid);
  759. return 0;
  760. }
  761. static int
  762. acpi_video_device_EDID_open_fs(struct inode *inode, struct file *file)
  763. {
  764. return single_open(file, acpi_video_device_EDID_seq_show,
  765. PDE(inode)->data);
  766. }
  767. static int acpi_video_device_add_fs(struct acpi_device *device)
  768. {
  769. struct proc_dir_entry *entry = NULL;
  770. struct acpi_video_device *vid_dev;
  771. if (!device)
  772. return -ENODEV;
  773. vid_dev = acpi_driver_data(device);
  774. if (!vid_dev)
  775. return -ENODEV;
  776. if (!acpi_device_dir(device)) {
  777. acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
  778. vid_dev->video->dir);
  779. if (!acpi_device_dir(device))
  780. return -ENODEV;
  781. acpi_device_dir(device)->owner = THIS_MODULE;
  782. }
  783. /* 'info' [R] */
  784. entry = create_proc_entry("info", S_IRUGO, acpi_device_dir(device));
  785. if (!entry)
  786. return -ENODEV;
  787. else {
  788. entry->proc_fops = &acpi_video_device_info_fops;
  789. entry->data = acpi_driver_data(device);
  790. entry->owner = THIS_MODULE;
  791. }
  792. /* 'state' [R/W] */
  793. entry =
  794. create_proc_entry("state", S_IFREG | S_IRUGO | S_IWUSR,
  795. acpi_device_dir(device));
  796. if (!entry)
  797. return -ENODEV;
  798. else {
  799. acpi_video_device_state_fops.write = acpi_video_device_write_state;
  800. entry->proc_fops = &acpi_video_device_state_fops;
  801. entry->data = acpi_driver_data(device);
  802. entry->owner = THIS_MODULE;
  803. }
  804. /* 'brightness' [R/W] */
  805. entry =
  806. create_proc_entry("brightness", S_IFREG | S_IRUGO | S_IWUSR,
  807. acpi_device_dir(device));
  808. if (!entry)
  809. return -ENODEV;
  810. else {
  811. acpi_video_device_brightness_fops.write = acpi_video_device_write_brightness;
  812. entry->proc_fops = &acpi_video_device_brightness_fops;
  813. entry->data = acpi_driver_data(device);
  814. entry->owner = THIS_MODULE;
  815. }
  816. /* 'EDID' [R] */
  817. entry = create_proc_entry("EDID", S_IRUGO, acpi_device_dir(device));
  818. if (!entry)
  819. return -ENODEV;
  820. else {
  821. entry->proc_fops = &acpi_video_device_EDID_fops;
  822. entry->data = acpi_driver_data(device);
  823. entry->owner = THIS_MODULE;
  824. }
  825. return 0;
  826. }
  827. static int acpi_video_device_remove_fs(struct acpi_device *device)
  828. {
  829. struct acpi_video_device *vid_dev;
  830. vid_dev = acpi_driver_data(device);
  831. if (!vid_dev || !vid_dev->video || !vid_dev->video->dir)
  832. return -ENODEV;
  833. if (acpi_device_dir(device)) {
  834. remove_proc_entry("info", acpi_device_dir(device));
  835. remove_proc_entry("state", acpi_device_dir(device));
  836. remove_proc_entry("brightness", acpi_device_dir(device));
  837. remove_proc_entry("EDID", acpi_device_dir(device));
  838. remove_proc_entry(acpi_device_bid(device), vid_dev->video->dir);
  839. acpi_device_dir(device) = NULL;
  840. }
  841. return 0;
  842. }
  843. /* video bus */
  844. static int acpi_video_bus_info_seq_show(struct seq_file *seq, void *offset)
  845. {
  846. struct acpi_video_bus *video = seq->private;
  847. if (!video)
  848. goto end;
  849. seq_printf(seq, "Switching heads: %s\n",
  850. video->flags.multihead ? "yes" : "no");
  851. seq_printf(seq, "Video ROM: %s\n",
  852. video->flags.rom ? "yes" : "no");
  853. seq_printf(seq, "Device to be POSTed on boot: %s\n",
  854. video->flags.post ? "yes" : "no");
  855. end:
  856. return 0;
  857. }
  858. static int acpi_video_bus_info_open_fs(struct inode *inode, struct file *file)
  859. {
  860. return single_open(file, acpi_video_bus_info_seq_show,
  861. PDE(inode)->data);
  862. }
  863. static int acpi_video_bus_ROM_seq_show(struct seq_file *seq, void *offset)
  864. {
  865. struct acpi_video_bus *video = seq->private;
  866. if (!video)
  867. goto end;
  868. printk(KERN_INFO PREFIX "Please implement %s\n", __FUNCTION__);
  869. seq_printf(seq, "<TODO>\n");
  870. end:
  871. return 0;
  872. }
  873. static int acpi_video_bus_ROM_open_fs(struct inode *inode, struct file *file)
  874. {
  875. return single_open(file, acpi_video_bus_ROM_seq_show, PDE(inode)->data);
  876. }
  877. static int acpi_video_bus_POST_info_seq_show(struct seq_file *seq, void *offset)
  878. {
  879. struct acpi_video_bus *video = seq->private;
  880. unsigned long options;
  881. int status;
  882. if (!video)
  883. goto end;
  884. status = acpi_video_bus_POST_options(video, &options);
  885. if (ACPI_SUCCESS(status)) {
  886. if (!(options & 1)) {
  887. printk(KERN_WARNING PREFIX
  888. "The motherboard VGA device is not listed as a possible POST device.\n");
  889. printk(KERN_WARNING PREFIX
  890. "This indicate a BIOS bug. Please contact the manufacturer.\n");
  891. }
  892. printk("%lx\n", options);
  893. seq_printf(seq, "can POST: <intgrated video>");
  894. if (options & 2)
  895. seq_printf(seq, " <PCI video>");
  896. if (options & 4)
  897. seq_printf(seq, " <AGP video>");
  898. seq_putc(seq, '\n');
  899. } else
  900. seq_printf(seq, "<not supported>\n");
  901. end:
  902. return 0;
  903. }
  904. static int
  905. acpi_video_bus_POST_info_open_fs(struct inode *inode, struct file *file)
  906. {
  907. return single_open(file, acpi_video_bus_POST_info_seq_show,
  908. PDE(inode)->data);
  909. }
  910. static int acpi_video_bus_POST_seq_show(struct seq_file *seq, void *offset)
  911. {
  912. struct acpi_video_bus *video = seq->private;
  913. int status;
  914. unsigned long id;
  915. if (!video)
  916. goto end;
  917. status = acpi_video_bus_get_POST(video, &id);
  918. if (!ACPI_SUCCESS(status)) {
  919. seq_printf(seq, "<not supported>\n");
  920. goto end;
  921. }
  922. seq_printf(seq, "device posted is <%s>\n", device_decode[id & 3]);
  923. end:
  924. return 0;
  925. }
  926. static int acpi_video_bus_DOS_seq_show(struct seq_file *seq, void *offset)
  927. {
  928. struct acpi_video_bus *video = seq->private;
  929. seq_printf(seq, "DOS setting: <%d>\n", video->dos_setting);
  930. return 0;
  931. }
  932. static int acpi_video_bus_POST_open_fs(struct inode *inode, struct file *file)
  933. {
  934. return single_open(file, acpi_video_bus_POST_seq_show,
  935. PDE(inode)->data);
  936. }
  937. static int acpi_video_bus_DOS_open_fs(struct inode *inode, struct file *file)
  938. {
  939. return single_open(file, acpi_video_bus_DOS_seq_show, PDE(inode)->data);
  940. }
  941. static ssize_t
  942. acpi_video_bus_write_POST(struct file *file,
  943. const char __user * buffer,
  944. size_t count, loff_t * data)
  945. {
  946. int status;
  947. struct seq_file *m = file->private_data;
  948. struct acpi_video_bus *video = m->private;
  949. char str[12] = { 0 };
  950. unsigned long opt, options;
  951. if (!video || count + 1 > sizeof str)
  952. return -EINVAL;
  953. status = acpi_video_bus_POST_options(video, &options);
  954. if (!ACPI_SUCCESS(status))
  955. return -EINVAL;
  956. if (copy_from_user(str, buffer, count))
  957. return -EFAULT;
  958. str[count] = 0;
  959. opt = strtoul(str, NULL, 0);
  960. if (opt > 3)
  961. return -EFAULT;
  962. /* just in case an OEM 'forget' the motherboard... */
  963. options |= 1;
  964. if (options & (1ul << opt)) {
  965. status = acpi_video_bus_set_POST(video, opt);
  966. if (!ACPI_SUCCESS(status))
  967. return -EFAULT;
  968. }
  969. return count;
  970. }
  971. static ssize_t
  972. acpi_video_bus_write_DOS(struct file *file,
  973. const char __user * buffer,
  974. size_t count, loff_t * data)
  975. {
  976. int status;
  977. struct seq_file *m = file->private_data;
  978. struct acpi_video_bus *video = m->private;
  979. char str[12] = { 0 };
  980. unsigned long opt;
  981. if (!video || count + 1 > sizeof str)
  982. return -EINVAL;
  983. if (copy_from_user(str, buffer, count))
  984. return -EFAULT;
  985. str[count] = 0;
  986. opt = strtoul(str, NULL, 0);
  987. if (opt > 7)
  988. return -EFAULT;
  989. status = acpi_video_bus_DOS(video, opt & 0x3, (opt & 0x4) >> 2);
  990. if (!ACPI_SUCCESS(status))
  991. return -EFAULT;
  992. return count;
  993. }
  994. static int acpi_video_bus_add_fs(struct acpi_device *device)
  995. {
  996. struct proc_dir_entry *entry = NULL;
  997. struct acpi_video_bus *video;
  998. video = acpi_driver_data(device);
  999. if (!acpi_device_dir(device)) {
  1000. acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
  1001. acpi_video_dir);
  1002. if (!acpi_device_dir(device))
  1003. return -ENODEV;
  1004. video->dir = acpi_device_dir(device);
  1005. acpi_device_dir(device)->owner = THIS_MODULE;
  1006. }
  1007. /* 'info' [R] */
  1008. entry = create_proc_entry("info", S_IRUGO, acpi_device_dir(device));
  1009. if (!entry)
  1010. return -ENODEV;
  1011. else {
  1012. entry->proc_fops = &acpi_video_bus_info_fops;
  1013. entry->data = acpi_driver_data(device);
  1014. entry->owner = THIS_MODULE;
  1015. }
  1016. /* 'ROM' [R] */
  1017. entry = create_proc_entry("ROM", S_IRUGO, acpi_device_dir(device));
  1018. if (!entry)
  1019. return -ENODEV;
  1020. else {
  1021. entry->proc_fops = &acpi_video_bus_ROM_fops;
  1022. entry->data = acpi_driver_data(device);
  1023. entry->owner = THIS_MODULE;
  1024. }
  1025. /* 'POST_info' [R] */
  1026. entry =
  1027. create_proc_entry("POST_info", S_IRUGO, acpi_device_dir(device));
  1028. if (!entry)
  1029. return -ENODEV;
  1030. else {
  1031. entry->proc_fops = &acpi_video_bus_POST_info_fops;
  1032. entry->data = acpi_driver_data(device);
  1033. entry->owner = THIS_MODULE;
  1034. }
  1035. /* 'POST' [R/W] */
  1036. entry =
  1037. create_proc_entry("POST", S_IFREG | S_IRUGO | S_IRUSR,
  1038. acpi_device_dir(device));
  1039. if (!entry)
  1040. return -ENODEV;
  1041. else {
  1042. acpi_video_bus_POST_fops.write = acpi_video_bus_write_POST;
  1043. entry->proc_fops = &acpi_video_bus_POST_fops;
  1044. entry->data = acpi_driver_data(device);
  1045. entry->owner = THIS_MODULE;
  1046. }
  1047. /* 'DOS' [R/W] */
  1048. entry =
  1049. create_proc_entry("DOS", S_IFREG | S_IRUGO | S_IRUSR,
  1050. acpi_device_dir(device));
  1051. if (!entry)
  1052. return -ENODEV;
  1053. else {
  1054. acpi_video_bus_DOS_fops.write = acpi_video_bus_write_DOS;
  1055. entry->proc_fops = &acpi_video_bus_DOS_fops;
  1056. entry->data = acpi_driver_data(device);
  1057. entry->owner = THIS_MODULE;
  1058. }
  1059. return 0;
  1060. }
  1061. static int acpi_video_bus_remove_fs(struct acpi_device *device)
  1062. {
  1063. struct acpi_video_bus *video;
  1064. video = acpi_driver_data(device);
  1065. if (acpi_device_dir(device)) {
  1066. remove_proc_entry("info", acpi_device_dir(device));
  1067. remove_proc_entry("ROM", acpi_device_dir(device));
  1068. remove_proc_entry("POST_info", acpi_device_dir(device));
  1069. remove_proc_entry("POST", acpi_device_dir(device));
  1070. remove_proc_entry("DOS", acpi_device_dir(device));
  1071. remove_proc_entry(acpi_device_bid(device), acpi_video_dir);
  1072. acpi_device_dir(device) = NULL;
  1073. }
  1074. return 0;
  1075. }
  1076. /* --------------------------------------------------------------------------
  1077. Driver Interface
  1078. -------------------------------------------------------------------------- */
  1079. /* device interface */
  1080. static struct acpi_video_device_attrib*
  1081. acpi_video_get_device_attr(struct acpi_video_bus *video, unsigned long device_id)
  1082. {
  1083. int count;
  1084. for(count = 0; count < video->attached_count; count++)
  1085. if((video->attached_array[count].value.int_val & 0xffff) == device_id)
  1086. return &(video->attached_array[count].value.attrib);
  1087. return NULL;
  1088. }
  1089. static int
  1090. acpi_video_bus_get_one_device(struct acpi_device *device,
  1091. struct acpi_video_bus *video)
  1092. {
  1093. unsigned long device_id;
  1094. int status;
  1095. struct acpi_video_device *data;
  1096. struct acpi_video_device_attrib* attribute;
  1097. if (!device || !video)
  1098. return -EINVAL;
  1099. status =
  1100. acpi_evaluate_integer(device->handle, "_ADR", NULL, &device_id);
  1101. if (ACPI_SUCCESS(status)) {
  1102. data = kzalloc(sizeof(struct acpi_video_device), GFP_KERNEL);
  1103. if (!data)
  1104. return -ENOMEM;
  1105. strcpy(acpi_device_name(device), ACPI_VIDEO_DEVICE_NAME);
  1106. strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
  1107. acpi_driver_data(device) = data;
  1108. data->device_id = device_id;
  1109. data->video = video;
  1110. data->dev = device;
  1111. attribute = acpi_video_get_device_attr(video, device_id);
  1112. if((attribute != NULL) && attribute->device_id_scheme) {
  1113. switch (attribute->display_type) {
  1114. case ACPI_VIDEO_DISPLAY_CRT:
  1115. data->flags.crt = 1;
  1116. break;
  1117. case ACPI_VIDEO_DISPLAY_TV:
  1118. data->flags.tvout = 1;
  1119. break;
  1120. case ACPI_VIDEO_DISPLAY_DVI:
  1121. data->flags.dvi = 1;
  1122. break;
  1123. case ACPI_VIDEO_DISPLAY_LCD:
  1124. data->flags.lcd = 1;
  1125. break;
  1126. default:
  1127. data->flags.unknown = 1;
  1128. break;
  1129. }
  1130. if(attribute->bios_can_detect)
  1131. data->flags.bios = 1;
  1132. } else
  1133. data->flags.unknown = 1;
  1134. acpi_video_device_bind(video, data);
  1135. acpi_video_device_find_cap(data);
  1136. status = acpi_install_notify_handler(device->handle,
  1137. ACPI_DEVICE_NOTIFY,
  1138. acpi_video_device_notify,
  1139. data);
  1140. if (ACPI_FAILURE(status)) {
  1141. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  1142. "Error installing notify handler\n"));
  1143. if(data->brightness)
  1144. kfree(data->brightness->levels);
  1145. kfree(data->brightness);
  1146. kfree(data);
  1147. return -ENODEV;
  1148. }
  1149. down(&video->sem);
  1150. list_add_tail(&data->entry, &video->video_device_list);
  1151. up(&video->sem);
  1152. acpi_video_device_add_fs(device);
  1153. return 0;
  1154. }
  1155. return -ENOENT;
  1156. }
  1157. /*
  1158. * Arg:
  1159. * video : video bus device
  1160. *
  1161. * Return:
  1162. * none
  1163. *
  1164. * Enumerate the video device list of the video bus,
  1165. * bind the ids with the corresponding video devices
  1166. * under the video bus.
  1167. */
  1168. static void acpi_video_device_rebind(struct acpi_video_bus *video)
  1169. {
  1170. struct list_head *node, *next;
  1171. list_for_each_safe(node, next, &video->video_device_list) {
  1172. struct acpi_video_device *dev =
  1173. container_of(node, struct acpi_video_device, entry);
  1174. acpi_video_device_bind(video, dev);
  1175. }
  1176. }
  1177. /*
  1178. * Arg:
  1179. * video : video bus device
  1180. * device : video output device under the video
  1181. * bus
  1182. *
  1183. * Return:
  1184. * none
  1185. *
  1186. * Bind the ids with the corresponding video devices
  1187. * under the video bus.
  1188. */
  1189. static void
  1190. acpi_video_device_bind(struct acpi_video_bus *video,
  1191. struct acpi_video_device *device)
  1192. {
  1193. int i;
  1194. #define IDS_VAL(i) video->attached_array[i].value.int_val
  1195. #define IDS_BIND(i) video->attached_array[i].bind_info
  1196. for (i = 0; IDS_VAL(i) != ACPI_VIDEO_HEAD_INVALID &&
  1197. i < video->attached_count; i++) {
  1198. if (device->device_id == (IDS_VAL(i) & 0xffff)) {
  1199. IDS_BIND(i) = device;
  1200. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "device_bind %d\n", i));
  1201. }
  1202. }
  1203. #undef IDS_VAL
  1204. #undef IDS_BIND
  1205. }
  1206. /*
  1207. * Arg:
  1208. * video : video bus device
  1209. *
  1210. * Return:
  1211. * < 0 : error
  1212. *
  1213. * Call _DOD to enumerate all devices attached to display adapter
  1214. *
  1215. */
  1216. static int acpi_video_device_enumerate(struct acpi_video_bus *video)
  1217. {
  1218. int status;
  1219. int count;
  1220. int i;
  1221. struct acpi_video_enumerated_device *active_device_list;
  1222. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  1223. union acpi_object *dod = NULL;
  1224. union acpi_object *obj;
  1225. status = acpi_evaluate_object(video->device->handle, "_DOD", NULL, &buffer);
  1226. if (!ACPI_SUCCESS(status)) {
  1227. ACPI_EXCEPTION((AE_INFO, status, "Evaluating _DOD"));
  1228. return status;
  1229. }
  1230. dod = buffer.pointer;
  1231. if (!dod || (dod->type != ACPI_TYPE_PACKAGE)) {
  1232. ACPI_EXCEPTION((AE_INFO, status, "Invalid _DOD data"));
  1233. status = -EFAULT;
  1234. goto out;
  1235. }
  1236. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d video heads in _DOD\n",
  1237. dod->package.count));
  1238. active_device_list = kmalloc((1 +
  1239. dod->package.count) *
  1240. sizeof(struct
  1241. acpi_video_enumerated_device),
  1242. GFP_KERNEL);
  1243. if (!active_device_list) {
  1244. status = -ENOMEM;
  1245. goto out;
  1246. }
  1247. count = 0;
  1248. for (i = 0; i < dod->package.count; i++) {
  1249. obj = &dod->package.elements[i];
  1250. if (obj->type != ACPI_TYPE_INTEGER) {
  1251. printk(KERN_ERR PREFIX "Invalid _DOD data\n");
  1252. active_device_list[i].value.int_val =
  1253. ACPI_VIDEO_HEAD_INVALID;
  1254. }
  1255. active_device_list[i].value.int_val = obj->integer.value;
  1256. active_device_list[i].bind_info = NULL;
  1257. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "dod element[%d] = %d\n", i,
  1258. (int)obj->integer.value));
  1259. count++;
  1260. }
  1261. active_device_list[count].value.int_val = ACPI_VIDEO_HEAD_END;
  1262. kfree(video->attached_array);
  1263. video->attached_array = active_device_list;
  1264. video->attached_count = count;
  1265. out:
  1266. kfree(buffer.pointer);
  1267. return status;
  1268. }
  1269. /*
  1270. * Arg:
  1271. * video : video bus device
  1272. * event : Nontify Event
  1273. *
  1274. * Return:
  1275. * < 0 : error
  1276. *
  1277. * 1. Find out the current active output device.
  1278. * 2. Identify the next output device to switch
  1279. * 3. call _DSS to do actual switch.
  1280. */
  1281. static int acpi_video_switch_output(struct acpi_video_bus *video, int event)
  1282. {
  1283. struct list_head *node, *next;
  1284. struct acpi_video_device *dev = NULL;
  1285. struct acpi_video_device *dev_next = NULL;
  1286. struct acpi_video_device *dev_prev = NULL;
  1287. unsigned long state;
  1288. int status = 0;
  1289. list_for_each_safe(node, next, &video->video_device_list) {
  1290. dev = container_of(node, struct acpi_video_device, entry);
  1291. status = acpi_video_device_get_state(dev, &state);
  1292. if (state & 0x2) {
  1293. dev_next =
  1294. container_of(node->next, struct acpi_video_device,
  1295. entry);
  1296. dev_prev =
  1297. container_of(node->prev, struct acpi_video_device,
  1298. entry);
  1299. goto out;
  1300. }
  1301. }
  1302. dev_next = container_of(node->next, struct acpi_video_device, entry);
  1303. dev_prev = container_of(node->prev, struct acpi_video_device, entry);
  1304. out:
  1305. switch (event) {
  1306. case ACPI_VIDEO_NOTIFY_CYCLE:
  1307. case ACPI_VIDEO_NOTIFY_NEXT_OUTPUT:
  1308. acpi_video_device_set_state(dev, 0);
  1309. acpi_video_device_set_state(dev_next, 0x80000001);
  1310. break;
  1311. case ACPI_VIDEO_NOTIFY_PREV_OUTPUT:
  1312. acpi_video_device_set_state(dev, 0);
  1313. acpi_video_device_set_state(dev_prev, 0x80000001);
  1314. default:
  1315. break;
  1316. }
  1317. return status;
  1318. }
  1319. static int
  1320. acpi_video_get_next_level(struct acpi_video_device *device,
  1321. u32 level_current, u32 event)
  1322. {
  1323. int min, max, min_above, max_below, i, l;
  1324. max = max_below = 0;
  1325. min = min_above = 255;
  1326. for (i = 0; i < device->brightness->count; i++) {
  1327. l = device->brightness->levels[i];
  1328. if (l < min)
  1329. min = l;
  1330. if (l > max)
  1331. max = l;
  1332. if (l < min_above && l > level_current)
  1333. min_above = l;
  1334. if (l > max_below && l < level_current)
  1335. max_below = l;
  1336. }
  1337. switch (event) {
  1338. case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS:
  1339. return (level_current < max) ? min_above : min;
  1340. case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS:
  1341. return (level_current < max) ? min_above : max;
  1342. case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS:
  1343. return (level_current > min) ? max_below : min;
  1344. case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS:
  1345. case ACPI_VIDEO_NOTIFY_DISPLAY_OFF:
  1346. return 0;
  1347. default:
  1348. return level_current;
  1349. }
  1350. }
  1351. static void
  1352. acpi_video_switch_brightness(struct acpi_video_device *device, int event)
  1353. {
  1354. unsigned long level_current, level_next;
  1355. acpi_video_device_lcd_get_level_current(device, &level_current);
  1356. level_next = acpi_video_get_next_level(device, level_current, event);
  1357. acpi_video_device_lcd_set_level(device, level_next);
  1358. }
  1359. static int
  1360. acpi_video_bus_get_devices(struct acpi_video_bus *video,
  1361. struct acpi_device *device)
  1362. {
  1363. int status = 0;
  1364. struct list_head *node, *next;
  1365. acpi_video_device_enumerate(video);
  1366. list_for_each_safe(node, next, &device->children) {
  1367. struct acpi_device *dev =
  1368. list_entry(node, struct acpi_device, node);
  1369. if (!dev)
  1370. continue;
  1371. status = acpi_video_bus_get_one_device(dev, video);
  1372. if (ACPI_FAILURE(status)) {
  1373. ACPI_EXCEPTION((AE_INFO, status, "Cant attach device"));
  1374. continue;
  1375. }
  1376. }
  1377. return status;
  1378. }
  1379. static int acpi_video_bus_put_one_device(struct acpi_video_device *device)
  1380. {
  1381. acpi_status status;
  1382. struct acpi_video_bus *video;
  1383. if (!device || !device->video)
  1384. return -ENOENT;
  1385. video = device->video;
  1386. down(&video->sem);
  1387. list_del(&device->entry);
  1388. up(&video->sem);
  1389. acpi_video_device_remove_fs(device->dev);
  1390. status = acpi_remove_notify_handler(device->dev->handle,
  1391. ACPI_DEVICE_NOTIFY,
  1392. acpi_video_device_notify);
  1393. if (device->backlight){
  1394. backlight_device_unregister(device->backlight);
  1395. kfree(device->data);
  1396. }
  1397. return 0;
  1398. }
  1399. static int acpi_video_bus_put_devices(struct acpi_video_bus *video)
  1400. {
  1401. int status;
  1402. struct list_head *node, *next;
  1403. list_for_each_safe(node, next, &video->video_device_list) {
  1404. struct acpi_video_device *data =
  1405. list_entry(node, struct acpi_video_device, entry);
  1406. if (!data)
  1407. continue;
  1408. status = acpi_video_bus_put_one_device(data);
  1409. if (ACPI_FAILURE(status))
  1410. printk(KERN_WARNING PREFIX
  1411. "hhuuhhuu bug in acpi video driver.\n");
  1412. if (data->brightness)
  1413. kfree(data->brightness->levels);
  1414. kfree(data->brightness);
  1415. kfree(data);
  1416. }
  1417. return 0;
  1418. }
  1419. /* acpi_video interface */
  1420. static int acpi_video_bus_start_devices(struct acpi_video_bus *video)
  1421. {
  1422. return acpi_video_bus_DOS(video, 1, 0);
  1423. }
  1424. static int acpi_video_bus_stop_devices(struct acpi_video_bus *video)
  1425. {
  1426. return acpi_video_bus_DOS(video, 0, 1);
  1427. }
  1428. static void acpi_video_bus_notify(acpi_handle handle, u32 event, void *data)
  1429. {
  1430. struct acpi_video_bus *video = data;
  1431. struct acpi_device *device = NULL;
  1432. printk("video bus notify\n");
  1433. if (!video)
  1434. return;
  1435. device = video->device;
  1436. switch (event) {
  1437. case ACPI_VIDEO_NOTIFY_SWITCH: /* User request that a switch occur,
  1438. * most likely via hotkey. */
  1439. acpi_bus_generate_event(device, event, 0);
  1440. break;
  1441. case ACPI_VIDEO_NOTIFY_PROBE: /* User plug or remove a video
  1442. * connector. */
  1443. acpi_video_device_enumerate(video);
  1444. acpi_video_device_rebind(video);
  1445. acpi_video_switch_output(video, event);
  1446. acpi_bus_generate_event(device, event, 0);
  1447. break;
  1448. case ACPI_VIDEO_NOTIFY_CYCLE: /* Cycle Display output hotkey pressed. */
  1449. case ACPI_VIDEO_NOTIFY_NEXT_OUTPUT: /* Next Display output hotkey pressed. */
  1450. case ACPI_VIDEO_NOTIFY_PREV_OUTPUT: /* previous Display output hotkey pressed. */
  1451. acpi_video_switch_output(video, event);
  1452. acpi_bus_generate_event(device, event, 0);
  1453. break;
  1454. default:
  1455. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  1456. "Unsupported event [0x%x]\n", event));
  1457. break;
  1458. }
  1459. return;
  1460. }
  1461. static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data)
  1462. {
  1463. struct acpi_video_device *video_device = data;
  1464. struct acpi_device *device = NULL;
  1465. if (!video_device)
  1466. return;
  1467. device = video_device->dev;
  1468. switch (event) {
  1469. case ACPI_VIDEO_NOTIFY_SWITCH: /* change in status (cycle output device) */
  1470. case ACPI_VIDEO_NOTIFY_PROBE: /* change in status (output device status) */
  1471. acpi_bus_generate_event(device, event, 0);
  1472. break;
  1473. case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS: /* Cycle brightness */
  1474. case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS: /* Increase brightness */
  1475. case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS: /* Decrease brightness */
  1476. case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS: /* zero brightnesss */
  1477. case ACPI_VIDEO_NOTIFY_DISPLAY_OFF: /* display device off */
  1478. acpi_video_switch_brightness(video_device, event);
  1479. acpi_bus_generate_event(device, event, 0);
  1480. break;
  1481. default:
  1482. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  1483. "Unsupported event [0x%x]\n", event));
  1484. break;
  1485. }
  1486. return;
  1487. }
  1488. static int acpi_video_bus_add(struct acpi_device *device)
  1489. {
  1490. int result = 0;
  1491. acpi_status status = 0;
  1492. struct acpi_video_bus *video = NULL;
  1493. if (!device)
  1494. return -EINVAL;
  1495. video = kzalloc(sizeof(struct acpi_video_bus), GFP_KERNEL);
  1496. if (!video)
  1497. return -ENOMEM;
  1498. video->device = device;
  1499. strcpy(acpi_device_name(device), ACPI_VIDEO_BUS_NAME);
  1500. strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
  1501. acpi_driver_data(device) = video;
  1502. acpi_video_bus_find_cap(video);
  1503. result = acpi_video_bus_check(video);
  1504. if (result)
  1505. goto end;
  1506. result = acpi_video_bus_add_fs(device);
  1507. if (result)
  1508. goto end;
  1509. init_MUTEX(&video->sem);
  1510. INIT_LIST_HEAD(&video->video_device_list);
  1511. acpi_video_bus_get_devices(video, device);
  1512. acpi_video_bus_start_devices(video);
  1513. status = acpi_install_notify_handler(device->handle,
  1514. ACPI_DEVICE_NOTIFY,
  1515. acpi_video_bus_notify, video);
  1516. if (ACPI_FAILURE(status)) {
  1517. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  1518. "Error installing notify handler\n"));
  1519. acpi_video_bus_stop_devices(video);
  1520. acpi_video_bus_put_devices(video);
  1521. kfree(video->attached_array);
  1522. acpi_video_bus_remove_fs(device);
  1523. result = -ENODEV;
  1524. goto end;
  1525. }
  1526. printk(KERN_INFO PREFIX "%s [%s] (multi-head: %s rom: %s post: %s)\n",
  1527. ACPI_VIDEO_DEVICE_NAME, acpi_device_bid(device),
  1528. video->flags.multihead ? "yes" : "no",
  1529. video->flags.rom ? "yes" : "no",
  1530. video->flags.post ? "yes" : "no");
  1531. end:
  1532. if (result)
  1533. kfree(video);
  1534. return result;
  1535. }
  1536. static int acpi_video_bus_remove(struct acpi_device *device, int type)
  1537. {
  1538. acpi_status status = 0;
  1539. struct acpi_video_bus *video = NULL;
  1540. if (!device || !acpi_driver_data(device))
  1541. return -EINVAL;
  1542. video = acpi_driver_data(device);
  1543. acpi_video_bus_stop_devices(video);
  1544. status = acpi_remove_notify_handler(video->device->handle,
  1545. ACPI_DEVICE_NOTIFY,
  1546. acpi_video_bus_notify);
  1547. acpi_video_bus_put_devices(video);
  1548. acpi_video_bus_remove_fs(device);
  1549. kfree(video->attached_array);
  1550. kfree(video);
  1551. return 0;
  1552. }
  1553. static int __init acpi_video_init(void)
  1554. {
  1555. int result = 0;
  1556. /*
  1557. acpi_dbg_level = 0xFFFFFFFF;
  1558. acpi_dbg_layer = 0x08000000;
  1559. */
  1560. acpi_video_dir = proc_mkdir(ACPI_VIDEO_CLASS, acpi_root_dir);
  1561. if (!acpi_video_dir)
  1562. return -ENODEV;
  1563. acpi_video_dir->owner = THIS_MODULE;
  1564. result = acpi_bus_register_driver(&acpi_video_bus);
  1565. if (result < 0) {
  1566. remove_proc_entry(ACPI_VIDEO_CLASS, acpi_root_dir);
  1567. return -ENODEV;
  1568. }
  1569. return 0;
  1570. }
  1571. static void __exit acpi_video_exit(void)
  1572. {
  1573. acpi_bus_unregister_driver(&acpi_video_bus);
  1574. remove_proc_entry(ACPI_VIDEO_CLASS, acpi_root_dir);
  1575. return;
  1576. }
  1577. module_init(acpi_video_init);
  1578. module_exit(acpi_video_exit);