video.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840
  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. *
  7. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or (at
  12. * your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License along
  20. * with this program; if not, write to the Free Software Foundation, Inc.,
  21. * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
  22. *
  23. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  24. */
  25. #include <linux/kernel.h>
  26. #include <linux/module.h>
  27. #include <linux/init.h>
  28. #include <linux/types.h>
  29. #include <linux/list.h>
  30. #include <linux/proc_fs.h>
  31. #include <linux/seq_file.h>
  32. #include <asm/uaccess.h>
  33. #include <acpi/acpi_bus.h>
  34. #include <acpi/acpi_drivers.h>
  35. #define ACPI_VIDEO_COMPONENT 0x08000000
  36. #define ACPI_VIDEO_CLASS "video"
  37. #define ACPI_VIDEO_DRIVER_NAME "ACPI Video Driver"
  38. #define ACPI_VIDEO_BUS_NAME "Video Bus"
  39. #define ACPI_VIDEO_DEVICE_NAME "Video Device"
  40. #define ACPI_VIDEO_NOTIFY_SWITCH 0x80
  41. #define ACPI_VIDEO_NOTIFY_PROBE 0x81
  42. #define ACPI_VIDEO_NOTIFY_CYCLE 0x82
  43. #define ACPI_VIDEO_NOTIFY_NEXT_OUTPUT 0x83
  44. #define ACPI_VIDEO_NOTIFY_PREV_OUTPUT 0x84
  45. #define ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS 0x82
  46. #define ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS 0x83
  47. #define ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS 0x84
  48. #define ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS 0x85
  49. #define ACPI_VIDEO_NOTIFY_DISPLAY_OFF 0x86
  50. #define ACPI_VIDEO_HEAD_INVALID (~0u - 1)
  51. #define ACPI_VIDEO_HEAD_END (~0u)
  52. #define _COMPONENT ACPI_VIDEO_COMPONENT
  53. ACPI_MODULE_NAME("acpi_video")
  54. MODULE_AUTHOR("Bruno Ducrot");
  55. MODULE_DESCRIPTION(ACPI_VIDEO_DRIVER_NAME);
  56. MODULE_LICENSE("GPL");
  57. static int acpi_video_bus_add(struct acpi_device *device);
  58. static int acpi_video_bus_remove(struct acpi_device *device, int type);
  59. static int acpi_video_bus_match(struct acpi_device *device,
  60. struct acpi_driver *driver);
  61. static struct acpi_driver acpi_video_bus = {
  62. .name = ACPI_VIDEO_DRIVER_NAME,
  63. .class = ACPI_VIDEO_CLASS,
  64. .ops = {
  65. .add = acpi_video_bus_add,
  66. .remove = acpi_video_bus_remove,
  67. .match = acpi_video_bus_match,
  68. },
  69. };
  70. struct acpi_video_bus_flags {
  71. u8 multihead:1; /* can switch video heads */
  72. u8 rom:1; /* can retrieve a video rom */
  73. u8 post:1; /* can configure the head to */
  74. u8 reserved:5;
  75. };
  76. struct acpi_video_bus_cap {
  77. u8 _DOS:1; /*Enable/Disable output switching */
  78. u8 _DOD:1; /*Enumerate all devices attached to display adapter */
  79. u8 _ROM:1; /*Get ROM Data */
  80. u8 _GPD:1; /*Get POST Device */
  81. u8 _SPD:1; /*Set POST Device */
  82. u8 _VPO:1; /*Video POST Options */
  83. u8 reserved:2;
  84. };
  85. struct acpi_video_device_attrib {
  86. u32 display_index:4; /* A zero-based instance of the Display */
  87. u32 display_port_attachment:4; /*This field differenates displays type */
  88. u32 display_type:4; /*Describe the specific type in use */
  89. u32 vendor_specific:4; /*Chipset Vendor Specifi */
  90. u32 bios_can_detect:1; /*BIOS can detect the device */
  91. u32 depend_on_vga:1; /*Non-VGA output device whose power is related to
  92. the VGA device. */
  93. u32 pipe_id:3; /*For VGA multiple-head devices. */
  94. u32 reserved:10; /*Must be 0 */
  95. u32 device_id_scheme:1; /*Device ID Scheme */
  96. };
  97. struct acpi_video_enumerated_device {
  98. union {
  99. u32 int_val;
  100. struct acpi_video_device_attrib attrib;
  101. } value;
  102. struct acpi_video_device *bind_info;
  103. };
  104. struct acpi_video_bus {
  105. struct acpi_device *device;
  106. u8 dos_setting;
  107. struct acpi_video_enumerated_device *attached_array;
  108. u8 attached_count;
  109. struct acpi_video_bus_cap cap;
  110. struct acpi_video_bus_flags flags;
  111. struct semaphore sem;
  112. struct list_head video_device_list;
  113. struct proc_dir_entry *dir;
  114. };
  115. struct acpi_video_device_flags {
  116. u8 crt:1;
  117. u8 lcd:1;
  118. u8 tvout:1;
  119. u8 bios:1;
  120. u8 unknown:1;
  121. u8 reserved:3;
  122. };
  123. struct acpi_video_device_cap {
  124. u8 _ADR:1; /*Return the unique ID */
  125. u8 _BCL:1; /*Query list of brightness control levels supported */
  126. u8 _BCM:1; /*Set the brightness level */
  127. u8 _DDC:1; /*Return the EDID for this device */
  128. u8 _DCS:1; /*Return status of output device */
  129. u8 _DGS:1; /*Query graphics state */
  130. u8 _DSS:1; /*Device state set */
  131. u8 _reserved:1;
  132. };
  133. struct acpi_video_device_brightness {
  134. int curr;
  135. int count;
  136. int *levels;
  137. };
  138. struct acpi_video_device {
  139. unsigned long device_id;
  140. struct acpi_video_device_flags flags;
  141. struct acpi_video_device_cap cap;
  142. struct list_head entry;
  143. struct acpi_video_bus *video;
  144. struct acpi_device *dev;
  145. struct acpi_video_device_brightness *brightness;
  146. };
  147. /* bus */
  148. static int acpi_video_bus_info_open_fs(struct inode *inode, struct file *file);
  149. static struct file_operations acpi_video_bus_info_fops = {
  150. .open = acpi_video_bus_info_open_fs,
  151. .read = seq_read,
  152. .llseek = seq_lseek,
  153. .release = single_release,
  154. };
  155. static int acpi_video_bus_ROM_open_fs(struct inode *inode, struct file *file);
  156. static struct file_operations acpi_video_bus_ROM_fops = {
  157. .open = acpi_video_bus_ROM_open_fs,
  158. .read = seq_read,
  159. .llseek = seq_lseek,
  160. .release = single_release,
  161. };
  162. static int acpi_video_bus_POST_info_open_fs(struct inode *inode,
  163. struct file *file);
  164. static struct file_operations acpi_video_bus_POST_info_fops = {
  165. .open = acpi_video_bus_POST_info_open_fs,
  166. .read = seq_read,
  167. .llseek = seq_lseek,
  168. .release = single_release,
  169. };
  170. static int acpi_video_bus_POST_open_fs(struct inode *inode, struct file *file);
  171. static struct file_operations acpi_video_bus_POST_fops = {
  172. .open = acpi_video_bus_POST_open_fs,
  173. .read = seq_read,
  174. .llseek = seq_lseek,
  175. .release = single_release,
  176. };
  177. static int acpi_video_bus_DOS_open_fs(struct inode *inode, struct file *file);
  178. static struct file_operations acpi_video_bus_DOS_fops = {
  179. .open = acpi_video_bus_DOS_open_fs,
  180. .read = seq_read,
  181. .llseek = seq_lseek,
  182. .release = single_release,
  183. };
  184. /* device */
  185. static int acpi_video_device_info_open_fs(struct inode *inode,
  186. struct file *file);
  187. static struct file_operations acpi_video_device_info_fops = {
  188. .open = acpi_video_device_info_open_fs,
  189. .read = seq_read,
  190. .llseek = seq_lseek,
  191. .release = single_release,
  192. };
  193. static int acpi_video_device_state_open_fs(struct inode *inode,
  194. struct file *file);
  195. static struct file_operations acpi_video_device_state_fops = {
  196. .open = acpi_video_device_state_open_fs,
  197. .read = seq_read,
  198. .llseek = seq_lseek,
  199. .release = single_release,
  200. };
  201. static int acpi_video_device_brightness_open_fs(struct inode *inode,
  202. struct file *file);
  203. static struct file_operations acpi_video_device_brightness_fops = {
  204. .open = acpi_video_device_brightness_open_fs,
  205. .read = seq_read,
  206. .llseek = seq_lseek,
  207. .release = single_release,
  208. };
  209. static int acpi_video_device_EDID_open_fs(struct inode *inode,
  210. struct file *file);
  211. static struct file_operations acpi_video_device_EDID_fops = {
  212. .open = acpi_video_device_EDID_open_fs,
  213. .read = seq_read,
  214. .llseek = seq_lseek,
  215. .release = single_release,
  216. };
  217. static char device_decode[][30] = {
  218. "motherboard VGA device",
  219. "PCI VGA device",
  220. "AGP VGA device",
  221. "UNKNOWN",
  222. };
  223. static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data);
  224. static void acpi_video_device_rebind(struct acpi_video_bus *video);
  225. static void acpi_video_device_bind(struct acpi_video_bus *video,
  226. struct acpi_video_device *device);
  227. static int acpi_video_device_enumerate(struct acpi_video_bus *video);
  228. static int acpi_video_switch_output(struct acpi_video_bus *video, int event);
  229. static int acpi_video_get_next_level(struct acpi_video_device *device,
  230. u32 level_current, u32 event);
  231. static void acpi_video_switch_brightness(struct acpi_video_device *device,
  232. int event);
  233. /* --------------------------------------------------------------------------
  234. Video Management
  235. -------------------------------------------------------------------------- */
  236. /* device */
  237. static int
  238. acpi_video_device_query(struct acpi_video_device *device, unsigned long *state)
  239. {
  240. int status;
  241. status = acpi_evaluate_integer(device->dev->handle, "_DGS", NULL, state);
  242. return status;
  243. }
  244. static int
  245. acpi_video_device_get_state(struct acpi_video_device *device,
  246. unsigned long *state)
  247. {
  248. int status;
  249. status = acpi_evaluate_integer(device->dev->handle, "_DCS", NULL, state);
  250. return status;
  251. }
  252. static int
  253. acpi_video_device_set_state(struct acpi_video_device *device, int state)
  254. {
  255. int status;
  256. union acpi_object arg0 = { ACPI_TYPE_INTEGER };
  257. struct acpi_object_list args = { 1, &arg0 };
  258. unsigned long ret;
  259. arg0.integer.value = state;
  260. status = acpi_evaluate_integer(device->dev->handle, "_DSS", &args, &ret);
  261. return status;
  262. }
  263. static int
  264. acpi_video_device_lcd_query_levels(struct acpi_video_device *device,
  265. union acpi_object **levels)
  266. {
  267. int status;
  268. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  269. union acpi_object *obj;
  270. *levels = NULL;
  271. status = acpi_evaluate_object(device->dev->handle, "_BCL", NULL, &buffer);
  272. if (!ACPI_SUCCESS(status))
  273. return status;
  274. obj = (union acpi_object *)buffer.pointer;
  275. if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
  276. printk(KERN_ERR PREFIX "Invalid _BCL data\n");
  277. status = -EFAULT;
  278. goto err;
  279. }
  280. *levels = obj;
  281. return 0;
  282. err:
  283. kfree(buffer.pointer);
  284. return status;
  285. }
  286. static int
  287. acpi_video_device_lcd_set_level(struct acpi_video_device *device, int level)
  288. {
  289. int status;
  290. union acpi_object arg0 = { ACPI_TYPE_INTEGER };
  291. struct acpi_object_list args = { 1, &arg0 };
  292. arg0.integer.value = level;
  293. status = acpi_evaluate_object(device->dev->handle, "_BCM", &args, NULL);
  294. printk(KERN_DEBUG "set_level status: %x\n", status);
  295. return status;
  296. }
  297. static int
  298. acpi_video_device_lcd_get_level_current(struct acpi_video_device *device,
  299. unsigned long *level)
  300. {
  301. int status;
  302. status = acpi_evaluate_integer(device->dev->handle, "_BQC", NULL, level);
  303. return status;
  304. }
  305. static int
  306. acpi_video_device_EDID(struct acpi_video_device *device,
  307. union acpi_object **edid, ssize_t length)
  308. {
  309. int status;
  310. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  311. union acpi_object *obj;
  312. union acpi_object arg0 = { ACPI_TYPE_INTEGER };
  313. struct acpi_object_list args = { 1, &arg0 };
  314. *edid = NULL;
  315. if (!device)
  316. return -ENODEV;
  317. if (length == 128)
  318. arg0.integer.value = 1;
  319. else if (length == 256)
  320. arg0.integer.value = 2;
  321. else
  322. return -EINVAL;
  323. status = acpi_evaluate_object(device->dev->handle, "_DDC", &args, &buffer);
  324. if (ACPI_FAILURE(status))
  325. return -ENODEV;
  326. obj = buffer.pointer;
  327. if (obj && obj->type == ACPI_TYPE_BUFFER)
  328. *edid = obj;
  329. else {
  330. printk(KERN_ERR PREFIX "Invalid _DDC data\n");
  331. status = -EFAULT;
  332. kfree(obj);
  333. }
  334. return status;
  335. }
  336. /* bus */
  337. static int
  338. acpi_video_bus_set_POST(struct acpi_video_bus *video, unsigned long option)
  339. {
  340. int status;
  341. unsigned long tmp;
  342. union acpi_object arg0 = { ACPI_TYPE_INTEGER };
  343. struct acpi_object_list args = { 1, &arg0 };
  344. arg0.integer.value = option;
  345. status = acpi_evaluate_integer(video->device->handle, "_SPD", &args, &tmp);
  346. if (ACPI_SUCCESS(status))
  347. status = tmp ? (-EINVAL) : (AE_OK);
  348. return status;
  349. }
  350. static int
  351. acpi_video_bus_get_POST(struct acpi_video_bus *video, unsigned long *id)
  352. {
  353. int status;
  354. status = acpi_evaluate_integer(video->device->handle, "_GPD", NULL, id);
  355. return status;
  356. }
  357. static int
  358. acpi_video_bus_POST_options(struct acpi_video_bus *video,
  359. unsigned long *options)
  360. {
  361. int status;
  362. status = acpi_evaluate_integer(video->device->handle, "_VPO", NULL, options);
  363. *options &= 3;
  364. return status;
  365. }
  366. /*
  367. * Arg:
  368. * video : video bus device pointer
  369. * bios_flag :
  370. * 0. The system BIOS should NOT automatically switch(toggle)
  371. * the active display output.
  372. * 1. The system BIOS should automatically switch (toggle) the
  373. * active display output. No swich event.
  374. * 2. The _DGS value should be locked.
  375. * 3. The system BIOS should not automatically switch (toggle) the
  376. * active display output, but instead generate the display switch
  377. * event notify code.
  378. * lcd_flag :
  379. * 0. The system BIOS should automatically control the brightness level
  380. * of the LCD, when the power changes from AC to DC
  381. * 1. The system BIOS should NOT automatically control the brightness
  382. * level of the LCD, when the power changes from AC to DC.
  383. * Return Value:
  384. * -1 wrong arg.
  385. */
  386. static int
  387. acpi_video_bus_DOS(struct acpi_video_bus *video, int bios_flag, int lcd_flag)
  388. {
  389. acpi_integer status = 0;
  390. union acpi_object arg0 = { ACPI_TYPE_INTEGER };
  391. struct acpi_object_list args = { 1, &arg0 };
  392. if (bios_flag < 0 || bios_flag > 3 || lcd_flag < 0 || lcd_flag > 1) {
  393. status = -1;
  394. goto Failed;
  395. }
  396. arg0.integer.value = (lcd_flag << 2) | bios_flag;
  397. video->dos_setting = arg0.integer.value;
  398. acpi_evaluate_object(video->device->handle, "_DOS", &args, NULL);
  399. Failed:
  400. return status;
  401. }
  402. /*
  403. * Arg:
  404. * device : video output device (LCD, CRT, ..)
  405. *
  406. * Return Value:
  407. * None
  408. *
  409. * Find out all required AML method defined under the output
  410. * device.
  411. */
  412. static void acpi_video_device_find_cap(struct acpi_video_device *device)
  413. {
  414. acpi_integer status;
  415. acpi_handle h_dummy1;
  416. int i;
  417. union acpi_object *obj = NULL;
  418. struct acpi_video_device_brightness *br = NULL;
  419. memset(&device->cap, 0, 4);
  420. if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_ADR", &h_dummy1))) {
  421. device->cap._ADR = 1;
  422. }
  423. if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_BCL", &h_dummy1))) {
  424. device->cap._BCL = 1;
  425. }
  426. if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_BCM", &h_dummy1))) {
  427. device->cap._BCM = 1;
  428. }
  429. if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DDC", &h_dummy1))) {
  430. device->cap._DDC = 1;
  431. }
  432. if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DCS", &h_dummy1))) {
  433. device->cap._DCS = 1;
  434. }
  435. if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DGS", &h_dummy1))) {
  436. device->cap._DGS = 1;
  437. }
  438. if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DSS", &h_dummy1))) {
  439. device->cap._DSS = 1;
  440. }
  441. status = acpi_video_device_lcd_query_levels(device, &obj);
  442. if (obj && obj->type == ACPI_TYPE_PACKAGE && obj->package.count >= 2) {
  443. int count = 0;
  444. union acpi_object *o;
  445. br = kmalloc(sizeof(*br), GFP_KERNEL);
  446. if (!br) {
  447. printk(KERN_ERR "can't allocate memory\n");
  448. } else {
  449. memset(br, 0, sizeof(*br));
  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 = kmalloc(sizeof(struct acpi_video_device), GFP_KERNEL);
  1028. if (!data)
  1029. return -ENOMEM;
  1030. memset(data, 0, sizeof(struct acpi_video_device));
  1031. strcpy(acpi_device_name(device), ACPI_VIDEO_DEVICE_NAME);
  1032. strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
  1033. acpi_driver_data(device) = data;
  1034. data->device_id = device_id;
  1035. data->video = video;
  1036. data->dev = device;
  1037. switch (device_id & 0xffff) {
  1038. case 0x0100:
  1039. data->flags.crt = 1;
  1040. break;
  1041. case 0x0400:
  1042. data->flags.lcd = 1;
  1043. break;
  1044. case 0x0200:
  1045. data->flags.tvout = 1;
  1046. break;
  1047. default:
  1048. data->flags.unknown = 1;
  1049. break;
  1050. }
  1051. acpi_video_device_bind(video, data);
  1052. acpi_video_device_find_cap(data);
  1053. status = acpi_install_notify_handler(device->handle,
  1054. ACPI_DEVICE_NOTIFY,
  1055. acpi_video_device_notify,
  1056. data);
  1057. if (ACPI_FAILURE(status)) {
  1058. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  1059. "Error installing notify handler\n"));
  1060. if(data->brightness)
  1061. kfree(data->brightness->levels);
  1062. kfree(data->brightness);
  1063. kfree(data);
  1064. return -ENODEV;
  1065. }
  1066. down(&video->sem);
  1067. list_add_tail(&data->entry, &video->video_device_list);
  1068. up(&video->sem);
  1069. acpi_video_device_add_fs(device);
  1070. return 0;
  1071. }
  1072. return -ENOENT;
  1073. }
  1074. /*
  1075. * Arg:
  1076. * video : video bus device
  1077. *
  1078. * Return:
  1079. * none
  1080. *
  1081. * Enumerate the video device list of the video bus,
  1082. * bind the ids with the corresponding video devices
  1083. * under the video bus.
  1084. */
  1085. static void acpi_video_device_rebind(struct acpi_video_bus *video)
  1086. {
  1087. struct list_head *node, *next;
  1088. list_for_each_safe(node, next, &video->video_device_list) {
  1089. struct acpi_video_device *dev =
  1090. container_of(node, struct acpi_video_device, entry);
  1091. acpi_video_device_bind(video, dev);
  1092. }
  1093. }
  1094. /*
  1095. * Arg:
  1096. * video : video bus device
  1097. * device : video output device under the video
  1098. * bus
  1099. *
  1100. * Return:
  1101. * none
  1102. *
  1103. * Bind the ids with the corresponding video devices
  1104. * under the video bus.
  1105. */
  1106. static void
  1107. acpi_video_device_bind(struct acpi_video_bus *video,
  1108. struct acpi_video_device *device)
  1109. {
  1110. int i;
  1111. #define IDS_VAL(i) video->attached_array[i].value.int_val
  1112. #define IDS_BIND(i) video->attached_array[i].bind_info
  1113. for (i = 0; IDS_VAL(i) != ACPI_VIDEO_HEAD_INVALID &&
  1114. i < video->attached_count; i++) {
  1115. if (device->device_id == (IDS_VAL(i) & 0xffff)) {
  1116. IDS_BIND(i) = device;
  1117. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "device_bind %d\n", i));
  1118. }
  1119. }
  1120. #undef IDS_VAL
  1121. #undef IDS_BIND
  1122. }
  1123. /*
  1124. * Arg:
  1125. * video : video bus device
  1126. *
  1127. * Return:
  1128. * < 0 : error
  1129. *
  1130. * Call _DOD to enumerate all devices attached to display adapter
  1131. *
  1132. */
  1133. static int acpi_video_device_enumerate(struct acpi_video_bus *video)
  1134. {
  1135. int status;
  1136. int count;
  1137. int i;
  1138. struct acpi_video_enumerated_device *active_device_list;
  1139. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  1140. union acpi_object *dod = NULL;
  1141. union acpi_object *obj;
  1142. status = acpi_evaluate_object(video->device->handle, "_DOD", NULL, &buffer);
  1143. if (!ACPI_SUCCESS(status)) {
  1144. ACPI_EXCEPTION((AE_INFO, status, "Evaluating _DOD"));
  1145. return status;
  1146. }
  1147. dod = buffer.pointer;
  1148. if (!dod || (dod->type != ACPI_TYPE_PACKAGE)) {
  1149. ACPI_EXCEPTION((AE_INFO, status, "Invalid _DOD data"));
  1150. status = -EFAULT;
  1151. goto out;
  1152. }
  1153. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d video heads in _DOD\n",
  1154. dod->package.count));
  1155. active_device_list = kmalloc((1 +
  1156. dod->package.count) *
  1157. sizeof(struct
  1158. acpi_video_enumerated_device),
  1159. GFP_KERNEL);
  1160. if (!active_device_list) {
  1161. status = -ENOMEM;
  1162. goto out;
  1163. }
  1164. count = 0;
  1165. for (i = 0; i < dod->package.count; i++) {
  1166. obj = &dod->package.elements[i];
  1167. if (obj->type != ACPI_TYPE_INTEGER) {
  1168. printk(KERN_ERR PREFIX "Invalid _DOD data\n");
  1169. active_device_list[i].value.int_val =
  1170. ACPI_VIDEO_HEAD_INVALID;
  1171. }
  1172. active_device_list[i].value.int_val = obj->integer.value;
  1173. active_device_list[i].bind_info = NULL;
  1174. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "dod element[%d] = %d\n", i,
  1175. (int)obj->integer.value));
  1176. count++;
  1177. }
  1178. active_device_list[count].value.int_val = ACPI_VIDEO_HEAD_END;
  1179. kfree(video->attached_array);
  1180. video->attached_array = active_device_list;
  1181. video->attached_count = count;
  1182. out:
  1183. kfree(buffer.pointer);
  1184. return status;
  1185. }
  1186. /*
  1187. * Arg:
  1188. * video : video bus device
  1189. * event : Nontify Event
  1190. *
  1191. * Return:
  1192. * < 0 : error
  1193. *
  1194. * 1. Find out the current active output device.
  1195. * 2. Identify the next output device to switch
  1196. * 3. call _DSS to do actual switch.
  1197. */
  1198. static int acpi_video_switch_output(struct acpi_video_bus *video, int event)
  1199. {
  1200. struct list_head *node, *next;
  1201. struct acpi_video_device *dev = NULL;
  1202. struct acpi_video_device *dev_next = NULL;
  1203. struct acpi_video_device *dev_prev = NULL;
  1204. unsigned long state;
  1205. int status = 0;
  1206. list_for_each_safe(node, next, &video->video_device_list) {
  1207. dev = container_of(node, struct acpi_video_device, entry);
  1208. status = acpi_video_device_get_state(dev, &state);
  1209. if (state & 0x2) {
  1210. dev_next =
  1211. container_of(node->next, struct acpi_video_device,
  1212. entry);
  1213. dev_prev =
  1214. container_of(node->prev, struct acpi_video_device,
  1215. entry);
  1216. goto out;
  1217. }
  1218. }
  1219. dev_next = container_of(node->next, struct acpi_video_device, entry);
  1220. dev_prev = container_of(node->prev, struct acpi_video_device, entry);
  1221. out:
  1222. switch (event) {
  1223. case ACPI_VIDEO_NOTIFY_CYCLE:
  1224. case ACPI_VIDEO_NOTIFY_NEXT_OUTPUT:
  1225. acpi_video_device_set_state(dev, 0);
  1226. acpi_video_device_set_state(dev_next, 0x80000001);
  1227. break;
  1228. case ACPI_VIDEO_NOTIFY_PREV_OUTPUT:
  1229. acpi_video_device_set_state(dev, 0);
  1230. acpi_video_device_set_state(dev_prev, 0x80000001);
  1231. default:
  1232. break;
  1233. }
  1234. return status;
  1235. }
  1236. static int
  1237. acpi_video_get_next_level(struct acpi_video_device *device,
  1238. u32 level_current, u32 event)
  1239. {
  1240. /*Fix me */
  1241. return level_current;
  1242. }
  1243. static void
  1244. acpi_video_switch_brightness(struct acpi_video_device *device, int event)
  1245. {
  1246. unsigned long level_current, level_next;
  1247. acpi_video_device_lcd_get_level_current(device, &level_current);
  1248. level_next = acpi_video_get_next_level(device, level_current, event);
  1249. acpi_video_device_lcd_set_level(device, level_next);
  1250. }
  1251. static int
  1252. acpi_video_bus_get_devices(struct acpi_video_bus *video,
  1253. struct acpi_device *device)
  1254. {
  1255. int status = 0;
  1256. struct list_head *node, *next;
  1257. acpi_video_device_enumerate(video);
  1258. list_for_each_safe(node, next, &device->children) {
  1259. struct acpi_device *dev =
  1260. list_entry(node, struct acpi_device, node);
  1261. if (!dev)
  1262. continue;
  1263. status = acpi_video_bus_get_one_device(dev, video);
  1264. if (ACPI_FAILURE(status)) {
  1265. ACPI_EXCEPTION((AE_INFO, status, "Cant attach device"));
  1266. continue;
  1267. }
  1268. }
  1269. return status;
  1270. }
  1271. static int acpi_video_bus_put_one_device(struct acpi_video_device *device)
  1272. {
  1273. acpi_status status;
  1274. struct acpi_video_bus *video;
  1275. if (!device || !device->video)
  1276. return -ENOENT;
  1277. video = device->video;
  1278. down(&video->sem);
  1279. list_del(&device->entry);
  1280. up(&video->sem);
  1281. acpi_video_device_remove_fs(device->dev);
  1282. status = acpi_remove_notify_handler(device->dev->handle,
  1283. ACPI_DEVICE_NOTIFY,
  1284. acpi_video_device_notify);
  1285. return 0;
  1286. }
  1287. static int acpi_video_bus_put_devices(struct acpi_video_bus *video)
  1288. {
  1289. int status;
  1290. struct list_head *node, *next;
  1291. list_for_each_safe(node, next, &video->video_device_list) {
  1292. struct acpi_video_device *data =
  1293. list_entry(node, struct acpi_video_device, entry);
  1294. if (!data)
  1295. continue;
  1296. status = acpi_video_bus_put_one_device(data);
  1297. if (ACPI_FAILURE(status))
  1298. printk(KERN_WARNING PREFIX
  1299. "hhuuhhuu bug in acpi video driver.\n");
  1300. if (data->brightness)
  1301. kfree(data->brightness->levels);
  1302. kfree(data->brightness);
  1303. kfree(data);
  1304. }
  1305. return 0;
  1306. }
  1307. /* acpi_video interface */
  1308. static int acpi_video_bus_start_devices(struct acpi_video_bus *video)
  1309. {
  1310. return acpi_video_bus_DOS(video, 1, 0);
  1311. }
  1312. static int acpi_video_bus_stop_devices(struct acpi_video_bus *video)
  1313. {
  1314. return acpi_video_bus_DOS(video, 0, 1);
  1315. }
  1316. static void acpi_video_bus_notify(acpi_handle handle, u32 event, void *data)
  1317. {
  1318. struct acpi_video_bus *video = data;
  1319. struct acpi_device *device = NULL;
  1320. printk("video bus notify\n");
  1321. if (!video)
  1322. return;
  1323. device = video->device;
  1324. switch (event) {
  1325. case ACPI_VIDEO_NOTIFY_SWITCH: /* User request that a switch occur,
  1326. * most likely via hotkey. */
  1327. acpi_bus_generate_event(device, event, 0);
  1328. break;
  1329. case ACPI_VIDEO_NOTIFY_PROBE: /* User plug or remove a video
  1330. * connector. */
  1331. acpi_video_device_enumerate(video);
  1332. acpi_video_device_rebind(video);
  1333. acpi_video_switch_output(video, event);
  1334. acpi_bus_generate_event(device, event, 0);
  1335. break;
  1336. case ACPI_VIDEO_NOTIFY_CYCLE: /* Cycle Display output hotkey pressed. */
  1337. case ACPI_VIDEO_NOTIFY_NEXT_OUTPUT: /* Next Display output hotkey pressed. */
  1338. case ACPI_VIDEO_NOTIFY_PREV_OUTPUT: /* previous Display output hotkey pressed. */
  1339. acpi_video_switch_output(video, event);
  1340. acpi_bus_generate_event(device, event, 0);
  1341. break;
  1342. default:
  1343. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  1344. "Unsupported event [0x%x]\n", event));
  1345. break;
  1346. }
  1347. return;
  1348. }
  1349. static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data)
  1350. {
  1351. struct acpi_video_device *video_device = data;
  1352. struct acpi_device *device = NULL;
  1353. printk("video device notify\n");
  1354. if (!video_device)
  1355. return;
  1356. device = video_device->dev;
  1357. switch (event) {
  1358. case ACPI_VIDEO_NOTIFY_SWITCH: /* change in status (cycle output device) */
  1359. case ACPI_VIDEO_NOTIFY_PROBE: /* change in status (output device status) */
  1360. acpi_bus_generate_event(device, event, 0);
  1361. break;
  1362. case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS: /* Cycle brightness */
  1363. case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS: /* Increase brightness */
  1364. case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS: /* Decrease brightness */
  1365. case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS: /* zero brightnesss */
  1366. case ACPI_VIDEO_NOTIFY_DISPLAY_OFF: /* display device off */
  1367. acpi_video_switch_brightness(video_device, event);
  1368. acpi_bus_generate_event(device, event, 0);
  1369. break;
  1370. default:
  1371. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  1372. "Unsupported event [0x%x]\n", event));
  1373. break;
  1374. }
  1375. return;
  1376. }
  1377. static int acpi_video_bus_add(struct acpi_device *device)
  1378. {
  1379. int result = 0;
  1380. acpi_status status = 0;
  1381. struct acpi_video_bus *video = NULL;
  1382. if (!device)
  1383. return -EINVAL;
  1384. video = kmalloc(sizeof(struct acpi_video_bus), GFP_KERNEL);
  1385. if (!video)
  1386. return -ENOMEM;
  1387. memset(video, 0, sizeof(struct acpi_video_bus));
  1388. video->device = device;
  1389. strcpy(acpi_device_name(device), ACPI_VIDEO_BUS_NAME);
  1390. strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
  1391. acpi_driver_data(device) = video;
  1392. acpi_video_bus_find_cap(video);
  1393. result = acpi_video_bus_check(video);
  1394. if (result)
  1395. goto end;
  1396. result = acpi_video_bus_add_fs(device);
  1397. if (result)
  1398. goto end;
  1399. init_MUTEX(&video->sem);
  1400. INIT_LIST_HEAD(&video->video_device_list);
  1401. acpi_video_bus_get_devices(video, device);
  1402. acpi_video_bus_start_devices(video);
  1403. status = acpi_install_notify_handler(device->handle,
  1404. ACPI_DEVICE_NOTIFY,
  1405. acpi_video_bus_notify, video);
  1406. if (ACPI_FAILURE(status)) {
  1407. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  1408. "Error installing notify handler\n"));
  1409. acpi_video_bus_stop_devices(video);
  1410. acpi_video_bus_put_devices(video);
  1411. kfree(video->attached_array);
  1412. acpi_video_bus_remove_fs(device);
  1413. result = -ENODEV;
  1414. goto end;
  1415. }
  1416. printk(KERN_INFO PREFIX "%s [%s] (multi-head: %s rom: %s post: %s)\n",
  1417. ACPI_VIDEO_DEVICE_NAME, acpi_device_bid(device),
  1418. video->flags.multihead ? "yes" : "no",
  1419. video->flags.rom ? "yes" : "no",
  1420. video->flags.post ? "yes" : "no");
  1421. end:
  1422. if (result)
  1423. kfree(video);
  1424. return result;
  1425. }
  1426. static int acpi_video_bus_remove(struct acpi_device *device, int type)
  1427. {
  1428. acpi_status status = 0;
  1429. struct acpi_video_bus *video = NULL;
  1430. if (!device || !acpi_driver_data(device))
  1431. return -EINVAL;
  1432. video = acpi_driver_data(device);
  1433. acpi_video_bus_stop_devices(video);
  1434. status = acpi_remove_notify_handler(video->device->handle,
  1435. ACPI_DEVICE_NOTIFY,
  1436. acpi_video_bus_notify);
  1437. acpi_video_bus_put_devices(video);
  1438. acpi_video_bus_remove_fs(device);
  1439. kfree(video->attached_array);
  1440. kfree(video);
  1441. return 0;
  1442. }
  1443. static int
  1444. acpi_video_bus_match(struct acpi_device *device, struct acpi_driver *driver)
  1445. {
  1446. acpi_handle h_dummy1;
  1447. acpi_handle h_dummy2;
  1448. acpi_handle h_dummy3;
  1449. if (!device || !driver)
  1450. return -EINVAL;
  1451. /* Since there is no HID, CID for ACPI Video drivers, we have
  1452. * to check well known required nodes for each feature we support.
  1453. */
  1454. /* Does this device able to support video switching ? */
  1455. if (ACPI_SUCCESS(acpi_get_handle(device->handle, "_DOD", &h_dummy1)) &&
  1456. ACPI_SUCCESS(acpi_get_handle(device->handle, "_DOS", &h_dummy2)))
  1457. return 0;
  1458. /* Does this device able to retrieve a video ROM ? */
  1459. if (ACPI_SUCCESS(acpi_get_handle(device->handle, "_ROM", &h_dummy1)))
  1460. return 0;
  1461. /* Does this device able to configure which video head to be POSTed ? */
  1462. if (ACPI_SUCCESS(acpi_get_handle(device->handle, "_VPO", &h_dummy1)) &&
  1463. ACPI_SUCCESS(acpi_get_handle(device->handle, "_GPD", &h_dummy2)) &&
  1464. ACPI_SUCCESS(acpi_get_handle(device->handle, "_SPD", &h_dummy3)))
  1465. return 0;
  1466. return -ENODEV;
  1467. }
  1468. static int __init acpi_video_init(void)
  1469. {
  1470. int result = 0;
  1471. /*
  1472. acpi_dbg_level = 0xFFFFFFFF;
  1473. acpi_dbg_layer = 0x08000000;
  1474. */
  1475. acpi_video_dir = proc_mkdir(ACPI_VIDEO_CLASS, acpi_root_dir);
  1476. if (!acpi_video_dir)
  1477. return -ENODEV;
  1478. acpi_video_dir->owner = THIS_MODULE;
  1479. result = acpi_bus_register_driver(&acpi_video_bus);
  1480. if (result < 0) {
  1481. remove_proc_entry(ACPI_VIDEO_CLASS, acpi_root_dir);
  1482. return -ENODEV;
  1483. }
  1484. return 0;
  1485. }
  1486. static void __exit acpi_video_exit(void)
  1487. {
  1488. acpi_bus_unregister_driver(&acpi_video_bus);
  1489. remove_proc_entry(ACPI_VIDEO_CLASS, acpi_root_dir);
  1490. return;
  1491. }
  1492. module_init(acpi_video_init);
  1493. module_exit(acpi_video_exit);