video.c 48 KB

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