video.c 47 KB

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