video.c 47 KB

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