video.c 44 KB

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