video.c 45 KB

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