video.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939
  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 _COMPONENT ACPI_VIDEO_COMPONENT
  56. ACPI_MODULE_NAME("acpi_video")
  57. MODULE_AUTHOR("Bruno Ducrot");
  58. MODULE_DESCRIPTION(ACPI_VIDEO_DRIVER_NAME);
  59. MODULE_LICENSE("GPL");
  60. static int acpi_video_bus_add(struct acpi_device *device);
  61. static int acpi_video_bus_remove(struct acpi_device *device, int type);
  62. static int acpi_video_bus_match(struct acpi_device *device,
  63. struct acpi_driver *driver);
  64. static struct acpi_driver acpi_video_bus = {
  65. .name = ACPI_VIDEO_DRIVER_NAME,
  66. .class = ACPI_VIDEO_CLASS,
  67. .ops = {
  68. .add = acpi_video_bus_add,
  69. .remove = acpi_video_bus_remove,
  70. .match = acpi_video_bus_match,
  71. },
  72. };
  73. struct acpi_video_bus_flags {
  74. u8 multihead:1; /* can switch video heads */
  75. u8 rom:1; /* can retrieve a video rom */
  76. u8 post:1; /* can configure the head to */
  77. u8 reserved:5;
  78. };
  79. struct acpi_video_bus_cap {
  80. u8 _DOS:1; /*Enable/Disable output switching */
  81. u8 _DOD:1; /*Enumerate all devices attached to display adapter */
  82. u8 _ROM:1; /*Get ROM Data */
  83. u8 _GPD:1; /*Get POST Device */
  84. u8 _SPD:1; /*Set POST Device */
  85. u8 _VPO:1; /*Video POST Options */
  86. u8 reserved:2;
  87. };
  88. struct acpi_video_device_attrib {
  89. u32 display_index:4; /* A zero-based instance of the Display */
  90. u32 display_port_attachment:4; /*This field differenates displays type */
  91. u32 display_type:4; /*Describe the specific type in use */
  92. u32 vendor_specific:4; /*Chipset Vendor Specifi */
  93. u32 bios_can_detect:1; /*BIOS can detect the device */
  94. u32 depend_on_vga:1; /*Non-VGA output device whose power is related to
  95. the VGA device. */
  96. u32 pipe_id:3; /*For VGA multiple-head devices. */
  97. u32 reserved:10; /*Must be 0 */
  98. u32 device_id_scheme:1; /*Device ID Scheme */
  99. };
  100. struct acpi_video_enumerated_device {
  101. union {
  102. u32 int_val;
  103. struct acpi_video_device_attrib attrib;
  104. } value;
  105. struct acpi_video_device *bind_info;
  106. };
  107. struct acpi_video_bus {
  108. struct acpi_device *device;
  109. u8 dos_setting;
  110. struct acpi_video_enumerated_device *attached_array;
  111. u8 attached_count;
  112. struct acpi_video_bus_cap cap;
  113. struct acpi_video_bus_flags flags;
  114. struct semaphore sem;
  115. struct list_head video_device_list;
  116. struct proc_dir_entry *dir;
  117. };
  118. struct acpi_video_device_flags {
  119. u8 crt:1;
  120. u8 lcd:1;
  121. u8 tvout:1;
  122. u8 bios:1;
  123. u8 unknown:1;
  124. u8 reserved:3;
  125. };
  126. struct acpi_video_device_cap {
  127. u8 _ADR:1; /*Return the unique ID */
  128. u8 _BCL:1; /*Query list of brightness control levels supported */
  129. u8 _BCM:1; /*Set the brightness level */
  130. u8 _BQC:1; /* Get current brightness level */
  131. u8 _DDC:1; /*Return the EDID for this device */
  132. u8 _DCS:1; /*Return status of output device */
  133. u8 _DGS:1; /*Query graphics state */
  134. u8 _DSS:1; /*Device state set */
  135. };
  136. struct acpi_video_device_brightness {
  137. int curr;
  138. int count;
  139. int *levels;
  140. };
  141. struct acpi_video_device {
  142. unsigned long device_id;
  143. struct acpi_video_device_flags flags;
  144. struct acpi_video_device_cap cap;
  145. struct list_head entry;
  146. struct acpi_video_bus *video;
  147. struct acpi_device *dev;
  148. struct acpi_video_device_brightness *brightness;
  149. struct backlight_device *backlight;
  150. struct backlight_properties *data;
  151. };
  152. /* bus */
  153. static int acpi_video_bus_info_open_fs(struct inode *inode, struct file *file);
  154. static struct file_operations acpi_video_bus_info_fops = {
  155. .open = acpi_video_bus_info_open_fs,
  156. .read = seq_read,
  157. .llseek = seq_lseek,
  158. .release = single_release,
  159. };
  160. static int acpi_video_bus_ROM_open_fs(struct inode *inode, struct file *file);
  161. static struct file_operations acpi_video_bus_ROM_fops = {
  162. .open = acpi_video_bus_ROM_open_fs,
  163. .read = seq_read,
  164. .llseek = seq_lseek,
  165. .release = single_release,
  166. };
  167. static int acpi_video_bus_POST_info_open_fs(struct inode *inode,
  168. struct file *file);
  169. static struct file_operations acpi_video_bus_POST_info_fops = {
  170. .open = acpi_video_bus_POST_info_open_fs,
  171. .read = seq_read,
  172. .llseek = seq_lseek,
  173. .release = single_release,
  174. };
  175. static int acpi_video_bus_POST_open_fs(struct inode *inode, struct file *file);
  176. static struct file_operations acpi_video_bus_POST_fops = {
  177. .open = acpi_video_bus_POST_open_fs,
  178. .read = seq_read,
  179. .llseek = seq_lseek,
  180. .release = single_release,
  181. };
  182. static int acpi_video_bus_DOS_open_fs(struct inode *inode, struct file *file);
  183. static struct file_operations acpi_video_bus_DOS_fops = {
  184. .open = acpi_video_bus_DOS_open_fs,
  185. .read = seq_read,
  186. .llseek = seq_lseek,
  187. .release = single_release,
  188. };
  189. /* device */
  190. static int acpi_video_device_info_open_fs(struct inode *inode,
  191. struct file *file);
  192. static struct file_operations acpi_video_device_info_fops = {
  193. .open = acpi_video_device_info_open_fs,
  194. .read = seq_read,
  195. .llseek = seq_lseek,
  196. .release = single_release,
  197. };
  198. static int acpi_video_device_state_open_fs(struct inode *inode,
  199. struct file *file);
  200. static struct file_operations acpi_video_device_state_fops = {
  201. .open = acpi_video_device_state_open_fs,
  202. .read = seq_read,
  203. .llseek = seq_lseek,
  204. .release = single_release,
  205. };
  206. static int acpi_video_device_brightness_open_fs(struct inode *inode,
  207. struct file *file);
  208. static struct file_operations acpi_video_device_brightness_fops = {
  209. .open = acpi_video_device_brightness_open_fs,
  210. .read = seq_read,
  211. .llseek = seq_lseek,
  212. .release = single_release,
  213. };
  214. static int acpi_video_device_EDID_open_fs(struct inode *inode,
  215. struct file *file);
  216. static struct file_operations acpi_video_device_EDID_fops = {
  217. .open = acpi_video_device_EDID_open_fs,
  218. .read = seq_read,
  219. .llseek = seq_lseek,
  220. .release = single_release,
  221. };
  222. static char device_decode[][30] = {
  223. "motherboard VGA device",
  224. "PCI VGA device",
  225. "AGP VGA device",
  226. "UNKNOWN",
  227. };
  228. static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data);
  229. static void acpi_video_device_rebind(struct acpi_video_bus *video);
  230. static void acpi_video_device_bind(struct acpi_video_bus *video,
  231. struct acpi_video_device *device);
  232. static int acpi_video_device_enumerate(struct acpi_video_bus *video);
  233. static int acpi_video_switch_output(struct acpi_video_bus *video, int event);
  234. static int acpi_video_device_lcd_set_level(struct acpi_video_device *device,
  235. int level);
  236. static int acpi_video_device_lcd_get_level_current(
  237. struct acpi_video_device *device,
  238. unsigned long *level);
  239. static int acpi_video_get_next_level(struct acpi_video_device *device,
  240. u32 level_current, u32 event);
  241. static void acpi_video_switch_brightness(struct acpi_video_device *device,
  242. int event);
  243. /*backlight device sysfs support*/
  244. static int acpi_video_get_brightness(struct backlight_device *bd)
  245. {
  246. unsigned long cur_level;
  247. struct acpi_video_device *vd =
  248. (struct acpi_video_device *)class_get_devdata(&bd->class_dev);
  249. acpi_video_device_lcd_get_level_current(vd, &cur_level);
  250. return (int) cur_level;
  251. }
  252. static int acpi_video_set_brightness(struct backlight_device *bd)
  253. {
  254. int request_level = bd->props->brightness;
  255. struct acpi_video_device *vd =
  256. (struct acpi_video_device *)class_get_devdata(&bd->class_dev);
  257. acpi_video_device_lcd_set_level(vd, request_level);
  258. return 0;
  259. }
  260. /* --------------------------------------------------------------------------
  261. Video Management
  262. -------------------------------------------------------------------------- */
  263. /* device */
  264. static int
  265. acpi_video_device_query(struct acpi_video_device *device, unsigned long *state)
  266. {
  267. int status;
  268. status = acpi_evaluate_integer(device->dev->handle, "_DGS", NULL, state);
  269. return status;
  270. }
  271. static int
  272. acpi_video_device_get_state(struct acpi_video_device *device,
  273. unsigned long *state)
  274. {
  275. int status;
  276. status = acpi_evaluate_integer(device->dev->handle, "_DCS", NULL, state);
  277. return status;
  278. }
  279. static int
  280. acpi_video_device_set_state(struct acpi_video_device *device, int state)
  281. {
  282. int status;
  283. union acpi_object arg0 = { ACPI_TYPE_INTEGER };
  284. struct acpi_object_list args = { 1, &arg0 };
  285. unsigned long ret;
  286. arg0.integer.value = state;
  287. status = acpi_evaluate_integer(device->dev->handle, "_DSS", &args, &ret);
  288. return status;
  289. }
  290. static int
  291. acpi_video_device_lcd_query_levels(struct acpi_video_device *device,
  292. union acpi_object **levels)
  293. {
  294. int status;
  295. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  296. union acpi_object *obj;
  297. *levels = NULL;
  298. status = acpi_evaluate_object(device->dev->handle, "_BCL", NULL, &buffer);
  299. if (!ACPI_SUCCESS(status))
  300. return status;
  301. obj = (union acpi_object *)buffer.pointer;
  302. if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
  303. printk(KERN_ERR PREFIX "Invalid _BCL data\n");
  304. status = -EFAULT;
  305. goto err;
  306. }
  307. *levels = obj;
  308. return 0;
  309. err:
  310. kfree(buffer.pointer);
  311. return status;
  312. }
  313. static int
  314. acpi_video_device_lcd_set_level(struct acpi_video_device *device, int level)
  315. {
  316. int status;
  317. union acpi_object arg0 = { ACPI_TYPE_INTEGER };
  318. struct acpi_object_list args = { 1, &arg0 };
  319. arg0.integer.value = level;
  320. status = acpi_evaluate_object(device->dev->handle, "_BCM", &args, NULL);
  321. printk(KERN_DEBUG "set_level status: %x\n", status);
  322. return status;
  323. }
  324. static int
  325. acpi_video_device_lcd_get_level_current(struct acpi_video_device *device,
  326. unsigned long *level)
  327. {
  328. int status;
  329. status = acpi_evaluate_integer(device->dev->handle, "_BQC", NULL, level);
  330. return status;
  331. }
  332. static int
  333. acpi_video_device_EDID(struct acpi_video_device *device,
  334. union acpi_object **edid, ssize_t length)
  335. {
  336. int status;
  337. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  338. union acpi_object *obj;
  339. union acpi_object arg0 = { ACPI_TYPE_INTEGER };
  340. struct acpi_object_list args = { 1, &arg0 };
  341. *edid = NULL;
  342. if (!device)
  343. return -ENODEV;
  344. if (length == 128)
  345. arg0.integer.value = 1;
  346. else if (length == 256)
  347. arg0.integer.value = 2;
  348. else
  349. return -EINVAL;
  350. status = acpi_evaluate_object(device->dev->handle, "_DDC", &args, &buffer);
  351. if (ACPI_FAILURE(status))
  352. return -ENODEV;
  353. obj = (union acpi_object *)buffer.pointer;
  354. if (obj && obj->type == ACPI_TYPE_BUFFER)
  355. *edid = obj;
  356. else {
  357. printk(KERN_ERR PREFIX "Invalid _DDC data\n");
  358. status = -EFAULT;
  359. kfree(obj);
  360. }
  361. return status;
  362. }
  363. /* bus */
  364. static int
  365. acpi_video_bus_set_POST(struct acpi_video_bus *video, unsigned long option)
  366. {
  367. int status;
  368. unsigned long tmp;
  369. union acpi_object arg0 = { ACPI_TYPE_INTEGER };
  370. struct acpi_object_list args = { 1, &arg0 };
  371. arg0.integer.value = option;
  372. status = acpi_evaluate_integer(video->device->handle, "_SPD", &args, &tmp);
  373. if (ACPI_SUCCESS(status))
  374. status = tmp ? (-EINVAL) : (AE_OK);
  375. return status;
  376. }
  377. static int
  378. acpi_video_bus_get_POST(struct acpi_video_bus *video, unsigned long *id)
  379. {
  380. int status;
  381. status = acpi_evaluate_integer(video->device->handle, "_GPD", NULL, id);
  382. return status;
  383. }
  384. static int
  385. acpi_video_bus_POST_options(struct acpi_video_bus *video,
  386. unsigned long *options)
  387. {
  388. int status;
  389. status = acpi_evaluate_integer(video->device->handle, "_VPO", NULL, options);
  390. *options &= 3;
  391. return status;
  392. }
  393. /*
  394. * Arg:
  395. * video : video bus device pointer
  396. * bios_flag :
  397. * 0. The system BIOS should NOT automatically switch(toggle)
  398. * the active display output.
  399. * 1. The system BIOS should automatically switch (toggle) the
  400. * active display output. No swich event.
  401. * 2. The _DGS value should be locked.
  402. * 3. The system BIOS should not automatically switch (toggle) the
  403. * active display output, but instead generate the display switch
  404. * event notify code.
  405. * lcd_flag :
  406. * 0. The system BIOS should automatically control the brightness level
  407. * of the LCD, when the power changes from AC to DC
  408. * 1. The system BIOS should NOT automatically control the brightness
  409. * level of the LCD, when the power changes from AC to DC.
  410. * Return Value:
  411. * -1 wrong arg.
  412. */
  413. static int
  414. acpi_video_bus_DOS(struct acpi_video_bus *video, int bios_flag, int lcd_flag)
  415. {
  416. acpi_integer status = 0;
  417. union acpi_object arg0 = { ACPI_TYPE_INTEGER };
  418. struct acpi_object_list args = { 1, &arg0 };
  419. if (bios_flag < 0 || bios_flag > 3 || lcd_flag < 0 || lcd_flag > 1) {
  420. status = -1;
  421. goto Failed;
  422. }
  423. arg0.integer.value = (lcd_flag << 2) | bios_flag;
  424. video->dos_setting = arg0.integer.value;
  425. acpi_evaluate_object(video->device->handle, "_DOS", &args, NULL);
  426. Failed:
  427. return status;
  428. }
  429. /*
  430. * Arg:
  431. * device : video output device (LCD, CRT, ..)
  432. *
  433. * Return Value:
  434. * None
  435. *
  436. * Find out all required AML method defined under the output
  437. * device.
  438. */
  439. static void acpi_video_device_find_cap(struct acpi_video_device *device)
  440. {
  441. acpi_integer status;
  442. acpi_handle h_dummy1;
  443. int i;
  444. u32 max_level = 0;
  445. union acpi_object *obj = NULL;
  446. struct acpi_video_device_brightness *br = NULL;
  447. memset(&device->cap, 0, 4);
  448. if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_ADR", &h_dummy1))) {
  449. device->cap._ADR = 1;
  450. }
  451. if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_BCL", &h_dummy1))) {
  452. device->cap._BCL = 1;
  453. }
  454. if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_BCM", &h_dummy1))) {
  455. device->cap._BCM = 1;
  456. }
  457. if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle,"_BQC",&h_dummy1)))
  458. device->cap._BQC = 1;
  459. if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DDC", &h_dummy1))) {
  460. device->cap._DDC = 1;
  461. }
  462. if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DCS", &h_dummy1))) {
  463. device->cap._DCS = 1;
  464. }
  465. if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DGS", &h_dummy1))) {
  466. device->cap._DGS = 1;
  467. }
  468. if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DSS", &h_dummy1))) {
  469. device->cap._DSS = 1;
  470. }
  471. status = acpi_video_device_lcd_query_levels(device, &obj);
  472. if (obj && obj->type == ACPI_TYPE_PACKAGE && obj->package.count >= 2) {
  473. int count = 0;
  474. union acpi_object *o;
  475. br = kmalloc(sizeof(*br), GFP_KERNEL);
  476. if (!br) {
  477. printk(KERN_ERR "can't allocate memory\n");
  478. } else {
  479. memset(br, 0, sizeof(*br));
  480. br->levels = kmalloc(obj->package.count *
  481. sizeof *(br->levels), GFP_KERNEL);
  482. if (!br->levels)
  483. goto out;
  484. for (i = 0; i < obj->package.count; i++) {
  485. o = (union acpi_object *)&obj->package.
  486. elements[i];
  487. if (o->type != ACPI_TYPE_INTEGER) {
  488. printk(KERN_ERR PREFIX "Invalid data\n");
  489. continue;
  490. }
  491. br->levels[count] = (u32) o->integer.value;
  492. if (br->levels[count] > max_level)
  493. max_level = br->levels[count];
  494. count++;
  495. }
  496. out:
  497. if (count < 2) {
  498. kfree(br->levels);
  499. kfree(br);
  500. } else {
  501. br->count = count;
  502. device->brightness = br;
  503. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  504. "found %d brightness levels\n",
  505. count));
  506. }
  507. }
  508. }
  509. kfree(obj);
  510. if (device->cap._BCL && device->cap._BCM && device->cap._BQC){
  511. unsigned long tmp;
  512. static int count = 0;
  513. char *name;
  514. struct backlight_properties *acpi_video_data;
  515. name = kzalloc(MAX_NAME_LEN, GFP_KERNEL);
  516. if (!name)
  517. return;
  518. acpi_video_data = kzalloc(
  519. sizeof(struct backlight_properties),
  520. GFP_KERNEL);
  521. if (!acpi_video_data){
  522. kfree(name);
  523. return;
  524. }
  525. acpi_video_data->owner = THIS_MODULE;
  526. acpi_video_data->get_brightness =
  527. acpi_video_get_brightness;
  528. acpi_video_data->update_status =
  529. acpi_video_set_brightness;
  530. sprintf(name, "acpi_video%d", count++);
  531. device->data = acpi_video_data;
  532. acpi_video_data->max_brightness = max_level;
  533. acpi_video_device_lcd_get_level_current(device, &tmp);
  534. acpi_video_data->brightness = (int)tmp;
  535. device->backlight = backlight_device_register(name,
  536. NULL, device, acpi_video_data);
  537. kfree(name);
  538. }
  539. return;
  540. }
  541. /*
  542. * Arg:
  543. * device : video output device (VGA)
  544. *
  545. * Return Value:
  546. * None
  547. *
  548. * Find out all required AML method defined under the video bus device.
  549. */
  550. static void acpi_video_bus_find_cap(struct acpi_video_bus *video)
  551. {
  552. acpi_handle h_dummy1;
  553. memset(&video->cap, 0, 4);
  554. if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_DOS", &h_dummy1))) {
  555. video->cap._DOS = 1;
  556. }
  557. if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_DOD", &h_dummy1))) {
  558. video->cap._DOD = 1;
  559. }
  560. if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_ROM", &h_dummy1))) {
  561. video->cap._ROM = 1;
  562. }
  563. if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_GPD", &h_dummy1))) {
  564. video->cap._GPD = 1;
  565. }
  566. if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_SPD", &h_dummy1))) {
  567. video->cap._SPD = 1;
  568. }
  569. if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_VPO", &h_dummy1))) {
  570. video->cap._VPO = 1;
  571. }
  572. }
  573. /*
  574. * Check whether the video bus device has required AML method to
  575. * support the desired features
  576. */
  577. static int acpi_video_bus_check(struct acpi_video_bus *video)
  578. {
  579. acpi_status status = -ENOENT;
  580. if (!video)
  581. return -EINVAL;
  582. /* Since there is no HID, CID and so on for VGA driver, we have
  583. * to check well known required nodes.
  584. */
  585. /* Does this device able to support video switching ? */
  586. if (video->cap._DOS) {
  587. video->flags.multihead = 1;
  588. status = 0;
  589. }
  590. /* Does this device able to retrieve a retrieve a video ROM ? */
  591. if (video->cap._ROM) {
  592. video->flags.rom = 1;
  593. status = 0;
  594. }
  595. /* Does this device able to configure which video device to POST ? */
  596. if (video->cap._GPD && video->cap._SPD && video->cap._VPO) {
  597. video->flags.post = 1;
  598. status = 0;
  599. }
  600. return status;
  601. }
  602. /* --------------------------------------------------------------------------
  603. FS Interface (/proc)
  604. -------------------------------------------------------------------------- */
  605. static struct proc_dir_entry *acpi_video_dir;
  606. /* video devices */
  607. static int acpi_video_device_info_seq_show(struct seq_file *seq, void *offset)
  608. {
  609. struct acpi_video_device *dev =
  610. (struct acpi_video_device *)seq->private;
  611. if (!dev)
  612. goto end;
  613. seq_printf(seq, "device_id: 0x%04x\n", (u32) dev->device_id);
  614. seq_printf(seq, "type: ");
  615. if (dev->flags.crt)
  616. seq_printf(seq, "CRT\n");
  617. else if (dev->flags.lcd)
  618. seq_printf(seq, "LCD\n");
  619. else if (dev->flags.tvout)
  620. seq_printf(seq, "TVOUT\n");
  621. else
  622. seq_printf(seq, "UNKNOWN\n");
  623. seq_printf(seq, "known by bios: %s\n", dev->flags.bios ? "yes" : "no");
  624. end:
  625. return 0;
  626. }
  627. static int
  628. acpi_video_device_info_open_fs(struct inode *inode, struct file *file)
  629. {
  630. return single_open(file, acpi_video_device_info_seq_show,
  631. PDE(inode)->data);
  632. }
  633. static int acpi_video_device_state_seq_show(struct seq_file *seq, void *offset)
  634. {
  635. int status;
  636. struct acpi_video_device *dev =
  637. (struct acpi_video_device *)seq->private;
  638. unsigned long state;
  639. if (!dev)
  640. goto end;
  641. status = acpi_video_device_get_state(dev, &state);
  642. seq_printf(seq, "state: ");
  643. if (ACPI_SUCCESS(status))
  644. seq_printf(seq, "0x%02lx\n", state);
  645. else
  646. seq_printf(seq, "<not supported>\n");
  647. status = acpi_video_device_query(dev, &state);
  648. seq_printf(seq, "query: ");
  649. if (ACPI_SUCCESS(status))
  650. seq_printf(seq, "0x%02lx\n", state);
  651. else
  652. seq_printf(seq, "<not supported>\n");
  653. end:
  654. return 0;
  655. }
  656. static int
  657. acpi_video_device_state_open_fs(struct inode *inode, struct file *file)
  658. {
  659. return single_open(file, acpi_video_device_state_seq_show,
  660. PDE(inode)->data);
  661. }
  662. static ssize_t
  663. acpi_video_device_write_state(struct file *file,
  664. const char __user * buffer,
  665. size_t count, loff_t * data)
  666. {
  667. int status;
  668. struct seq_file *m = (struct seq_file *)file->private_data;
  669. struct acpi_video_device *dev = (struct acpi_video_device *)m->private;
  670. char str[12] = { 0 };
  671. u32 state = 0;
  672. if (!dev || count + 1 > sizeof str)
  673. return -EINVAL;
  674. if (copy_from_user(str, buffer, count))
  675. return -EFAULT;
  676. str[count] = 0;
  677. state = simple_strtoul(str, NULL, 0);
  678. state &= ((1ul << 31) | (1ul << 30) | (1ul << 0));
  679. status = acpi_video_device_set_state(dev, state);
  680. if (status)
  681. return -EFAULT;
  682. return count;
  683. }
  684. static int
  685. acpi_video_device_brightness_seq_show(struct seq_file *seq, void *offset)
  686. {
  687. struct acpi_video_device *dev =
  688. (struct acpi_video_device *)seq->private;
  689. int i;
  690. if (!dev || !dev->brightness) {
  691. seq_printf(seq, "<not supported>\n");
  692. return 0;
  693. }
  694. seq_printf(seq, "levels: ");
  695. for (i = 0; i < dev->brightness->count; i++)
  696. seq_printf(seq, " %d", dev->brightness->levels[i]);
  697. seq_printf(seq, "\ncurrent: %d\n", dev->brightness->curr);
  698. return 0;
  699. }
  700. static int
  701. acpi_video_device_brightness_open_fs(struct inode *inode, struct file *file)
  702. {
  703. return single_open(file, acpi_video_device_brightness_seq_show,
  704. PDE(inode)->data);
  705. }
  706. static ssize_t
  707. acpi_video_device_write_brightness(struct file *file,
  708. const char __user * buffer,
  709. size_t count, loff_t * data)
  710. {
  711. struct seq_file *m = (struct seq_file *)file->private_data;
  712. struct acpi_video_device *dev = (struct acpi_video_device *)m->private;
  713. char str[4] = { 0 };
  714. unsigned int level = 0;
  715. int i;
  716. if (!dev || !dev->brightness || count + 1 > sizeof str)
  717. return -EINVAL;
  718. if (copy_from_user(str, buffer, count))
  719. return -EFAULT;
  720. str[count] = 0;
  721. level = simple_strtoul(str, NULL, 0);
  722. if (level > 100)
  723. return -EFAULT;
  724. /* validate though the list of available levels */
  725. for (i = 0; i < dev->brightness->count; i++)
  726. if (level == dev->brightness->levels[i]) {
  727. if (ACPI_SUCCESS
  728. (acpi_video_device_lcd_set_level(dev, level)))
  729. dev->brightness->curr = level;
  730. break;
  731. }
  732. return count;
  733. }
  734. static int acpi_video_device_EDID_seq_show(struct seq_file *seq, void *offset)
  735. {
  736. struct acpi_video_device *dev =
  737. (struct acpi_video_device *)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 = (struct acpi_video_device *)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 = (struct acpi_video_device *)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 = (struct acpi_video_bus *)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 = (struct acpi_video_bus *)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 = (struct acpi_video_bus *)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 = (struct acpi_video_bus *)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 = (struct acpi_video_bus *)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 = (struct seq_file *)file->private_data;
  948. struct acpi_video_bus *video = (struct acpi_video_bus *)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 = (struct seq_file *)file->private_data;
  978. struct acpi_video_bus *video = (struct acpi_video_bus *)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 = (struct acpi_video_bus *)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 = (struct acpi_video_bus *)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 int
  1081. acpi_video_bus_get_one_device(struct acpi_device *device,
  1082. struct acpi_video_bus *video)
  1083. {
  1084. unsigned long device_id;
  1085. int status;
  1086. struct acpi_video_device *data;
  1087. if (!device || !video)
  1088. return -EINVAL;
  1089. status =
  1090. acpi_evaluate_integer(device->handle, "_ADR", NULL, &device_id);
  1091. if (ACPI_SUCCESS(status)) {
  1092. data = kmalloc(sizeof(struct acpi_video_device), GFP_KERNEL);
  1093. if (!data)
  1094. return -ENOMEM;
  1095. memset(data, 0, sizeof(struct acpi_video_device));
  1096. strcpy(acpi_device_name(device), ACPI_VIDEO_DEVICE_NAME);
  1097. strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
  1098. acpi_driver_data(device) = data;
  1099. data->device_id = device_id;
  1100. data->video = video;
  1101. data->dev = device;
  1102. switch (device_id & 0xffff) {
  1103. case 0x0100:
  1104. data->flags.crt = 1;
  1105. break;
  1106. case 0x0400:
  1107. data->flags.lcd = 1;
  1108. break;
  1109. case 0x0200:
  1110. data->flags.tvout = 1;
  1111. break;
  1112. default:
  1113. data->flags.unknown = 1;
  1114. break;
  1115. }
  1116. acpi_video_device_bind(video, data);
  1117. acpi_video_device_find_cap(data);
  1118. status = acpi_install_notify_handler(device->handle,
  1119. ACPI_DEVICE_NOTIFY,
  1120. acpi_video_device_notify,
  1121. data);
  1122. if (ACPI_FAILURE(status)) {
  1123. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  1124. "Error installing notify handler\n"));
  1125. if(data->brightness)
  1126. kfree(data->brightness->levels);
  1127. kfree(data->brightness);
  1128. kfree(data);
  1129. return -ENODEV;
  1130. }
  1131. down(&video->sem);
  1132. list_add_tail(&data->entry, &video->video_device_list);
  1133. up(&video->sem);
  1134. acpi_video_device_add_fs(device);
  1135. return 0;
  1136. }
  1137. return -ENOENT;
  1138. }
  1139. /*
  1140. * Arg:
  1141. * video : video bus device
  1142. *
  1143. * Return:
  1144. * none
  1145. *
  1146. * Enumerate the video device list of the video bus,
  1147. * bind the ids with the corresponding video devices
  1148. * under the video bus.
  1149. */
  1150. static void acpi_video_device_rebind(struct acpi_video_bus *video)
  1151. {
  1152. struct list_head *node, *next;
  1153. list_for_each_safe(node, next, &video->video_device_list) {
  1154. struct acpi_video_device *dev =
  1155. container_of(node, struct acpi_video_device, entry);
  1156. acpi_video_device_bind(video, dev);
  1157. }
  1158. }
  1159. /*
  1160. * Arg:
  1161. * video : video bus device
  1162. * device : video output device under the video
  1163. * bus
  1164. *
  1165. * Return:
  1166. * none
  1167. *
  1168. * Bind the ids with the corresponding video devices
  1169. * under the video bus.
  1170. */
  1171. static void
  1172. acpi_video_device_bind(struct acpi_video_bus *video,
  1173. struct acpi_video_device *device)
  1174. {
  1175. int i;
  1176. #define IDS_VAL(i) video->attached_array[i].value.int_val
  1177. #define IDS_BIND(i) video->attached_array[i].bind_info
  1178. for (i = 0; IDS_VAL(i) != ACPI_VIDEO_HEAD_INVALID &&
  1179. i < video->attached_count; i++) {
  1180. if (device->device_id == (IDS_VAL(i) & 0xffff)) {
  1181. IDS_BIND(i) = device;
  1182. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "device_bind %d\n", i));
  1183. }
  1184. }
  1185. #undef IDS_VAL
  1186. #undef IDS_BIND
  1187. }
  1188. /*
  1189. * Arg:
  1190. * video : video bus device
  1191. *
  1192. * Return:
  1193. * < 0 : error
  1194. *
  1195. * Call _DOD to enumerate all devices attached to display adapter
  1196. *
  1197. */
  1198. static int acpi_video_device_enumerate(struct acpi_video_bus *video)
  1199. {
  1200. int status;
  1201. int count;
  1202. int i;
  1203. struct acpi_video_enumerated_device *active_device_list;
  1204. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  1205. union acpi_object *dod = NULL;
  1206. union acpi_object *obj;
  1207. status = acpi_evaluate_object(video->device->handle, "_DOD", NULL, &buffer);
  1208. if (!ACPI_SUCCESS(status)) {
  1209. ACPI_EXCEPTION((AE_INFO, status, "Evaluating _DOD"));
  1210. return status;
  1211. }
  1212. dod = (union acpi_object *)buffer.pointer;
  1213. if (!dod || (dod->type != ACPI_TYPE_PACKAGE)) {
  1214. ACPI_EXCEPTION((AE_INFO, status, "Invalid _DOD data"));
  1215. status = -EFAULT;
  1216. goto out;
  1217. }
  1218. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d video heads in _DOD\n",
  1219. dod->package.count));
  1220. active_device_list = kmalloc((1 +
  1221. dod->package.count) *
  1222. sizeof(struct
  1223. acpi_video_enumerated_device),
  1224. GFP_KERNEL);
  1225. if (!active_device_list) {
  1226. status = -ENOMEM;
  1227. goto out;
  1228. }
  1229. count = 0;
  1230. for (i = 0; i < dod->package.count; i++) {
  1231. obj = (union acpi_object *)&dod->package.elements[i];
  1232. if (obj->type != ACPI_TYPE_INTEGER) {
  1233. printk(KERN_ERR PREFIX "Invalid _DOD data\n");
  1234. active_device_list[i].value.int_val =
  1235. ACPI_VIDEO_HEAD_INVALID;
  1236. }
  1237. active_device_list[i].value.int_val = obj->integer.value;
  1238. active_device_list[i].bind_info = NULL;
  1239. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "dod element[%d] = %d\n", i,
  1240. (int)obj->integer.value));
  1241. count++;
  1242. }
  1243. active_device_list[count].value.int_val = ACPI_VIDEO_HEAD_END;
  1244. kfree(video->attached_array);
  1245. video->attached_array = active_device_list;
  1246. video->attached_count = count;
  1247. out:
  1248. kfree(buffer.pointer);
  1249. return status;
  1250. }
  1251. /*
  1252. * Arg:
  1253. * video : video bus device
  1254. * event : Nontify Event
  1255. *
  1256. * Return:
  1257. * < 0 : error
  1258. *
  1259. * 1. Find out the current active output device.
  1260. * 2. Identify the next output device to switch
  1261. * 3. call _DSS to do actual switch.
  1262. */
  1263. static int acpi_video_switch_output(struct acpi_video_bus *video, int event)
  1264. {
  1265. struct list_head *node, *next;
  1266. struct acpi_video_device *dev = NULL;
  1267. struct acpi_video_device *dev_next = NULL;
  1268. struct acpi_video_device *dev_prev = NULL;
  1269. unsigned long state;
  1270. int status = 0;
  1271. list_for_each_safe(node, next, &video->video_device_list) {
  1272. dev = container_of(node, struct acpi_video_device, entry);
  1273. status = acpi_video_device_get_state(dev, &state);
  1274. if (state & 0x2) {
  1275. dev_next =
  1276. container_of(node->next, struct acpi_video_device,
  1277. entry);
  1278. dev_prev =
  1279. container_of(node->prev, struct acpi_video_device,
  1280. entry);
  1281. goto out;
  1282. }
  1283. }
  1284. dev_next = container_of(node->next, struct acpi_video_device, entry);
  1285. dev_prev = container_of(node->prev, struct acpi_video_device, entry);
  1286. out:
  1287. switch (event) {
  1288. case ACPI_VIDEO_NOTIFY_CYCLE:
  1289. case ACPI_VIDEO_NOTIFY_NEXT_OUTPUT:
  1290. acpi_video_device_set_state(dev, 0);
  1291. acpi_video_device_set_state(dev_next, 0x80000001);
  1292. break;
  1293. case ACPI_VIDEO_NOTIFY_PREV_OUTPUT:
  1294. acpi_video_device_set_state(dev, 0);
  1295. acpi_video_device_set_state(dev_prev, 0x80000001);
  1296. default:
  1297. break;
  1298. }
  1299. return status;
  1300. }
  1301. static int
  1302. acpi_video_get_next_level(struct acpi_video_device *device,
  1303. u32 level_current, u32 event)
  1304. {
  1305. int min, max, min_above, max_below, i, l;
  1306. max = max_below = 0;
  1307. min = min_above = 255;
  1308. for (i = 0; i < device->brightness->count; i++) {
  1309. l = device->brightness->levels[i];
  1310. if (l < min)
  1311. min = l;
  1312. if (l > max)
  1313. max = l;
  1314. if (l < min_above && l > level_current)
  1315. min_above = l;
  1316. if (l > max_below && l < level_current)
  1317. max_below = l;
  1318. }
  1319. switch (event) {
  1320. case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS:
  1321. return (level_current < max) ? min_above : min;
  1322. case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS:
  1323. return (level_current < max) ? min_above : max;
  1324. case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS:
  1325. return (level_current > min) ? max_below : min;
  1326. case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS:
  1327. case ACPI_VIDEO_NOTIFY_DISPLAY_OFF:
  1328. return 0;
  1329. default:
  1330. return level_current;
  1331. }
  1332. }
  1333. static void
  1334. acpi_video_switch_brightness(struct acpi_video_device *device, int event)
  1335. {
  1336. unsigned long level_current, level_next;
  1337. acpi_video_device_lcd_get_level_current(device, &level_current);
  1338. level_next = acpi_video_get_next_level(device, level_current, event);
  1339. acpi_video_device_lcd_set_level(device, level_next);
  1340. }
  1341. static int
  1342. acpi_video_bus_get_devices(struct acpi_video_bus *video,
  1343. struct acpi_device *device)
  1344. {
  1345. int status = 0;
  1346. struct list_head *node, *next;
  1347. acpi_video_device_enumerate(video);
  1348. list_for_each_safe(node, next, &device->children) {
  1349. struct acpi_device *dev =
  1350. list_entry(node, struct acpi_device, node);
  1351. if (!dev)
  1352. continue;
  1353. status = acpi_video_bus_get_one_device(dev, video);
  1354. if (ACPI_FAILURE(status)) {
  1355. ACPI_EXCEPTION((AE_INFO, status, "Cant attach device"));
  1356. continue;
  1357. }
  1358. }
  1359. return status;
  1360. }
  1361. static int acpi_video_bus_put_one_device(struct acpi_video_device *device)
  1362. {
  1363. acpi_status status;
  1364. struct acpi_video_bus *video;
  1365. if (!device || !device->video)
  1366. return -ENOENT;
  1367. video = device->video;
  1368. down(&video->sem);
  1369. list_del(&device->entry);
  1370. up(&video->sem);
  1371. acpi_video_device_remove_fs(device->dev);
  1372. status = acpi_remove_notify_handler(device->dev->handle,
  1373. ACPI_DEVICE_NOTIFY,
  1374. acpi_video_device_notify);
  1375. if (device->backlight){
  1376. backlight_device_unregister(device->backlight);
  1377. kfree(device->data);
  1378. }
  1379. return 0;
  1380. }
  1381. static int acpi_video_bus_put_devices(struct acpi_video_bus *video)
  1382. {
  1383. int status;
  1384. struct list_head *node, *next;
  1385. list_for_each_safe(node, next, &video->video_device_list) {
  1386. struct acpi_video_device *data =
  1387. list_entry(node, struct acpi_video_device, entry);
  1388. if (!data)
  1389. continue;
  1390. status = acpi_video_bus_put_one_device(data);
  1391. if (ACPI_FAILURE(status))
  1392. printk(KERN_WARNING PREFIX
  1393. "hhuuhhuu bug in acpi video driver.\n");
  1394. if (data->brightness)
  1395. kfree(data->brightness->levels);
  1396. kfree(data->brightness);
  1397. kfree(data);
  1398. }
  1399. return 0;
  1400. }
  1401. /* acpi_video interface */
  1402. static int acpi_video_bus_start_devices(struct acpi_video_bus *video)
  1403. {
  1404. return acpi_video_bus_DOS(video, 1, 0);
  1405. }
  1406. static int acpi_video_bus_stop_devices(struct acpi_video_bus *video)
  1407. {
  1408. return acpi_video_bus_DOS(video, 0, 1);
  1409. }
  1410. static void acpi_video_bus_notify(acpi_handle handle, u32 event, void *data)
  1411. {
  1412. struct acpi_video_bus *video = (struct acpi_video_bus *)data;
  1413. struct acpi_device *device = NULL;
  1414. printk("video bus notify\n");
  1415. if (!video)
  1416. return;
  1417. device = video->device;
  1418. switch (event) {
  1419. case ACPI_VIDEO_NOTIFY_SWITCH: /* User request that a switch occur,
  1420. * most likely via hotkey. */
  1421. acpi_bus_generate_event(device, event, 0);
  1422. break;
  1423. case ACPI_VIDEO_NOTIFY_PROBE: /* User plug or remove a video
  1424. * connector. */
  1425. acpi_video_device_enumerate(video);
  1426. acpi_video_device_rebind(video);
  1427. acpi_video_switch_output(video, event);
  1428. acpi_bus_generate_event(device, event, 0);
  1429. break;
  1430. case ACPI_VIDEO_NOTIFY_CYCLE: /* Cycle Display output hotkey pressed. */
  1431. case ACPI_VIDEO_NOTIFY_NEXT_OUTPUT: /* Next Display output hotkey pressed. */
  1432. case ACPI_VIDEO_NOTIFY_PREV_OUTPUT: /* previous Display output hotkey pressed. */
  1433. acpi_video_switch_output(video, event);
  1434. acpi_bus_generate_event(device, event, 0);
  1435. break;
  1436. default:
  1437. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  1438. "Unsupported event [0x%x]\n", event));
  1439. break;
  1440. }
  1441. return;
  1442. }
  1443. static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data)
  1444. {
  1445. struct acpi_video_device *video_device =
  1446. (struct acpi_video_device *)data;
  1447. struct acpi_device *device = NULL;
  1448. printk("video device notify\n");
  1449. if (!video_device)
  1450. return;
  1451. device = video_device->dev;
  1452. switch (event) {
  1453. case ACPI_VIDEO_NOTIFY_SWITCH: /* change in status (cycle output device) */
  1454. case ACPI_VIDEO_NOTIFY_PROBE: /* change in status (output device status) */
  1455. acpi_bus_generate_event(device, event, 0);
  1456. break;
  1457. case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS: /* Cycle brightness */
  1458. case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS: /* Increase brightness */
  1459. case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS: /* Decrease brightness */
  1460. case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS: /* zero brightnesss */
  1461. case ACPI_VIDEO_NOTIFY_DISPLAY_OFF: /* display device off */
  1462. acpi_video_switch_brightness(video_device, event);
  1463. acpi_bus_generate_event(device, event, 0);
  1464. break;
  1465. default:
  1466. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  1467. "Unsupported event [0x%x]\n", event));
  1468. break;
  1469. }
  1470. return;
  1471. }
  1472. static int acpi_video_bus_add(struct acpi_device *device)
  1473. {
  1474. int result = 0;
  1475. acpi_status status = 0;
  1476. struct acpi_video_bus *video = NULL;
  1477. if (!device)
  1478. return -EINVAL;
  1479. video = kmalloc(sizeof(struct acpi_video_bus), GFP_KERNEL);
  1480. if (!video)
  1481. return -ENOMEM;
  1482. memset(video, 0, sizeof(struct acpi_video_bus));
  1483. video->device = device;
  1484. strcpy(acpi_device_name(device), ACPI_VIDEO_BUS_NAME);
  1485. strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
  1486. acpi_driver_data(device) = video;
  1487. acpi_video_bus_find_cap(video);
  1488. result = acpi_video_bus_check(video);
  1489. if (result)
  1490. goto end;
  1491. result = acpi_video_bus_add_fs(device);
  1492. if (result)
  1493. goto end;
  1494. init_MUTEX(&video->sem);
  1495. INIT_LIST_HEAD(&video->video_device_list);
  1496. acpi_video_bus_get_devices(video, device);
  1497. acpi_video_bus_start_devices(video);
  1498. status = acpi_install_notify_handler(device->handle,
  1499. ACPI_DEVICE_NOTIFY,
  1500. acpi_video_bus_notify, video);
  1501. if (ACPI_FAILURE(status)) {
  1502. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  1503. "Error installing notify handler\n"));
  1504. acpi_video_bus_stop_devices(video);
  1505. acpi_video_bus_put_devices(video);
  1506. kfree(video->attached_array);
  1507. acpi_video_bus_remove_fs(device);
  1508. result = -ENODEV;
  1509. goto end;
  1510. }
  1511. printk(KERN_INFO PREFIX "%s [%s] (multi-head: %s rom: %s post: %s)\n",
  1512. ACPI_VIDEO_DEVICE_NAME, acpi_device_bid(device),
  1513. video->flags.multihead ? "yes" : "no",
  1514. video->flags.rom ? "yes" : "no",
  1515. video->flags.post ? "yes" : "no");
  1516. end:
  1517. if (result)
  1518. kfree(video);
  1519. return result;
  1520. }
  1521. static int acpi_video_bus_remove(struct acpi_device *device, int type)
  1522. {
  1523. acpi_status status = 0;
  1524. struct acpi_video_bus *video = NULL;
  1525. if (!device || !acpi_driver_data(device))
  1526. return -EINVAL;
  1527. video = (struct acpi_video_bus *)acpi_driver_data(device);
  1528. acpi_video_bus_stop_devices(video);
  1529. status = acpi_remove_notify_handler(video->device->handle,
  1530. ACPI_DEVICE_NOTIFY,
  1531. acpi_video_bus_notify);
  1532. acpi_video_bus_put_devices(video);
  1533. acpi_video_bus_remove_fs(device);
  1534. kfree(video->attached_array);
  1535. kfree(video);
  1536. return 0;
  1537. }
  1538. static int
  1539. acpi_video_bus_match(struct acpi_device *device, struct acpi_driver *driver)
  1540. {
  1541. acpi_handle h_dummy1;
  1542. acpi_handle h_dummy2;
  1543. acpi_handle h_dummy3;
  1544. if (!device || !driver)
  1545. return -EINVAL;
  1546. /* Since there is no HID, CID for ACPI Video drivers, we have
  1547. * to check well known required nodes for each feature we support.
  1548. */
  1549. /* Does this device able to support video switching ? */
  1550. if (ACPI_SUCCESS(acpi_get_handle(device->handle, "_DOD", &h_dummy1)) &&
  1551. ACPI_SUCCESS(acpi_get_handle(device->handle, "_DOS", &h_dummy2)))
  1552. return 0;
  1553. /* Does this device able to retrieve a video ROM ? */
  1554. if (ACPI_SUCCESS(acpi_get_handle(device->handle, "_ROM", &h_dummy1)))
  1555. return 0;
  1556. /* Does this device able to configure which video head to be POSTed ? */
  1557. if (ACPI_SUCCESS(acpi_get_handle(device->handle, "_VPO", &h_dummy1)) &&
  1558. ACPI_SUCCESS(acpi_get_handle(device->handle, "_GPD", &h_dummy2)) &&
  1559. ACPI_SUCCESS(acpi_get_handle(device->handle, "_SPD", &h_dummy3)))
  1560. return 0;
  1561. return -ENODEV;
  1562. }
  1563. static int __init acpi_video_init(void)
  1564. {
  1565. int result = 0;
  1566. /*
  1567. acpi_dbg_level = 0xFFFFFFFF;
  1568. acpi_dbg_layer = 0x08000000;
  1569. */
  1570. acpi_video_dir = proc_mkdir(ACPI_VIDEO_CLASS, acpi_root_dir);
  1571. if (!acpi_video_dir)
  1572. return -ENODEV;
  1573. acpi_video_dir->owner = THIS_MODULE;
  1574. result = acpi_bus_register_driver(&acpi_video_bus);
  1575. if (result < 0) {
  1576. remove_proc_entry(ACPI_VIDEO_CLASS, acpi_root_dir);
  1577. return -ENODEV;
  1578. }
  1579. return 0;
  1580. }
  1581. static void __exit acpi_video_exit(void)
  1582. {
  1583. acpi_bus_unregister_driver(&acpi_video_bus);
  1584. remove_proc_entry(ACPI_VIDEO_CLASS, acpi_root_dir);
  1585. return;
  1586. }
  1587. module_init(acpi_video_init);
  1588. module_exit(acpi_video_exit);