video.c 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441
  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/mutex.h>
  32. #include <linux/proc_fs.h>
  33. #include <linux/seq_file.h>
  34. #include <linux/input.h>
  35. #include <linux/backlight.h>
  36. #include <linux/thermal.h>
  37. #include <linux/video_output.h>
  38. #include <linux/sort.h>
  39. #include <linux/pci.h>
  40. #include <linux/pci_ids.h>
  41. #include <asm/uaccess.h>
  42. #include <linux/dmi.h>
  43. #include <acpi/acpi_bus.h>
  44. #include <acpi/acpi_drivers.h>
  45. #define PREFIX "ACPI: "
  46. #define ACPI_VIDEO_CLASS "video"
  47. #define ACPI_VIDEO_BUS_NAME "Video Bus"
  48. #define ACPI_VIDEO_DEVICE_NAME "Video Device"
  49. #define ACPI_VIDEO_NOTIFY_SWITCH 0x80
  50. #define ACPI_VIDEO_NOTIFY_PROBE 0x81
  51. #define ACPI_VIDEO_NOTIFY_CYCLE 0x82
  52. #define ACPI_VIDEO_NOTIFY_NEXT_OUTPUT 0x83
  53. #define ACPI_VIDEO_NOTIFY_PREV_OUTPUT 0x84
  54. #define ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS 0x85
  55. #define ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS 0x86
  56. #define ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS 0x87
  57. #define ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS 0x88
  58. #define ACPI_VIDEO_NOTIFY_DISPLAY_OFF 0x89
  59. #define MAX_NAME_LEN 20
  60. #define ACPI_VIDEO_DISPLAY_CRT 1
  61. #define ACPI_VIDEO_DISPLAY_TV 2
  62. #define ACPI_VIDEO_DISPLAY_DVI 3
  63. #define ACPI_VIDEO_DISPLAY_LCD 4
  64. #define _COMPONENT ACPI_VIDEO_COMPONENT
  65. ACPI_MODULE_NAME("video");
  66. MODULE_AUTHOR("Bruno Ducrot");
  67. MODULE_DESCRIPTION("ACPI Video Driver");
  68. MODULE_LICENSE("GPL");
  69. static int brightness_switch_enabled = 1;
  70. module_param(brightness_switch_enabled, bool, 0644);
  71. static int register_count = 0;
  72. static int acpi_video_bus_add(struct acpi_device *device);
  73. static int acpi_video_bus_remove(struct acpi_device *device, int type);
  74. static int acpi_video_resume(struct acpi_device *device);
  75. static void acpi_video_bus_notify(struct acpi_device *device, u32 event);
  76. static const struct acpi_device_id video_device_ids[] = {
  77. {ACPI_VIDEO_HID, 0},
  78. {"", 0},
  79. };
  80. MODULE_DEVICE_TABLE(acpi, video_device_ids);
  81. static struct acpi_driver acpi_video_bus = {
  82. .name = "video",
  83. .class = ACPI_VIDEO_CLASS,
  84. .ids = video_device_ids,
  85. .ops = {
  86. .add = acpi_video_bus_add,
  87. .remove = acpi_video_bus_remove,
  88. .resume = acpi_video_resume,
  89. .notify = acpi_video_bus_notify,
  90. },
  91. };
  92. struct acpi_video_bus_flags {
  93. u8 multihead:1; /* can switch video heads */
  94. u8 rom:1; /* can retrieve a video rom */
  95. u8 post:1; /* can configure the head to */
  96. u8 reserved:5;
  97. };
  98. struct acpi_video_bus_cap {
  99. u8 _DOS:1; /*Enable/Disable output switching */
  100. u8 _DOD:1; /*Enumerate all devices attached to display adapter */
  101. u8 _ROM:1; /*Get ROM Data */
  102. u8 _GPD:1; /*Get POST Device */
  103. u8 _SPD:1; /*Set POST Device */
  104. u8 _VPO:1; /*Video POST Options */
  105. u8 reserved:2;
  106. };
  107. struct acpi_video_device_attrib {
  108. u32 display_index:4; /* A zero-based instance of the Display */
  109. u32 display_port_attachment:4; /*This field differentiates the display type */
  110. u32 display_type:4; /*Describe the specific type in use */
  111. u32 vendor_specific:4; /*Chipset Vendor Specific */
  112. u32 bios_can_detect:1; /*BIOS can detect the device */
  113. u32 depend_on_vga:1; /*Non-VGA output device whose power is related to
  114. the VGA device. */
  115. u32 pipe_id:3; /*For VGA multiple-head devices. */
  116. u32 reserved:10; /*Must be 0 */
  117. u32 device_id_scheme:1; /*Device ID Scheme */
  118. };
  119. struct acpi_video_enumerated_device {
  120. union {
  121. u32 int_val;
  122. struct acpi_video_device_attrib attrib;
  123. } value;
  124. struct acpi_video_device *bind_info;
  125. };
  126. struct acpi_video_bus {
  127. struct acpi_device *device;
  128. u8 dos_setting;
  129. struct acpi_video_enumerated_device *attached_array;
  130. u8 attached_count;
  131. struct acpi_video_bus_cap cap;
  132. struct acpi_video_bus_flags flags;
  133. struct list_head video_device_list;
  134. struct mutex device_list_lock; /* protects video_device_list */
  135. struct proc_dir_entry *dir;
  136. struct input_dev *input;
  137. char phys[32]; /* for input device */
  138. };
  139. struct acpi_video_device_flags {
  140. u8 crt:1;
  141. u8 lcd:1;
  142. u8 tvout:1;
  143. u8 dvi:1;
  144. u8 bios:1;
  145. u8 unknown:1;
  146. u8 reserved:2;
  147. };
  148. struct acpi_video_device_cap {
  149. u8 _ADR:1; /*Return the unique ID */
  150. u8 _BCL:1; /*Query list of brightness control levels supported */
  151. u8 _BCM:1; /*Set the brightness level */
  152. u8 _BQC:1; /* Get current brightness level */
  153. u8 _BCQ:1; /* Some buggy BIOS uses _BCQ instead of _BQC */
  154. u8 _DDC:1; /*Return the EDID for this device */
  155. u8 _DCS:1; /*Return status of output device */
  156. u8 _DGS:1; /*Query graphics state */
  157. u8 _DSS:1; /*Device state set */
  158. };
  159. struct acpi_video_brightness_flags {
  160. u8 _BCL_no_ac_battery_levels:1; /* no AC/Battery levels in _BCL */
  161. u8 _BCL_reversed:1; /* _BCL package is in a reversed order*/
  162. u8 _BCL_use_index:1; /* levels in _BCL are index values */
  163. u8 _BCM_use_index:1; /* input of _BCM is an index value */
  164. u8 _BQC_use_index:1; /* _BQC returns an index value */
  165. };
  166. struct acpi_video_device_brightness {
  167. int curr;
  168. int count;
  169. int *levels;
  170. struct acpi_video_brightness_flags flags;
  171. };
  172. struct acpi_video_device {
  173. unsigned long device_id;
  174. struct acpi_video_device_flags flags;
  175. struct acpi_video_device_cap cap;
  176. struct list_head entry;
  177. struct acpi_video_bus *video;
  178. struct acpi_device *dev;
  179. struct acpi_video_device_brightness *brightness;
  180. struct backlight_device *backlight;
  181. struct thermal_cooling_device *cooling_dev;
  182. struct output_device *output_dev;
  183. };
  184. /* bus */
  185. static int acpi_video_bus_info_open_fs(struct inode *inode, struct file *file);
  186. static const struct file_operations acpi_video_bus_info_fops = {
  187. .owner = THIS_MODULE,
  188. .open = acpi_video_bus_info_open_fs,
  189. .read = seq_read,
  190. .llseek = seq_lseek,
  191. .release = single_release,
  192. };
  193. static int acpi_video_bus_ROM_open_fs(struct inode *inode, struct file *file);
  194. static const struct file_operations acpi_video_bus_ROM_fops = {
  195. .owner = THIS_MODULE,
  196. .open = acpi_video_bus_ROM_open_fs,
  197. .read = seq_read,
  198. .llseek = seq_lseek,
  199. .release = single_release,
  200. };
  201. static int acpi_video_bus_POST_info_open_fs(struct inode *inode,
  202. struct file *file);
  203. static const struct file_operations acpi_video_bus_POST_info_fops = {
  204. .owner = THIS_MODULE,
  205. .open = acpi_video_bus_POST_info_open_fs,
  206. .read = seq_read,
  207. .llseek = seq_lseek,
  208. .release = single_release,
  209. };
  210. static int acpi_video_bus_POST_open_fs(struct inode *inode, struct file *file);
  211. static ssize_t acpi_video_bus_write_POST(struct file *file,
  212. const char __user *buffer, size_t count, loff_t *data);
  213. static const struct file_operations acpi_video_bus_POST_fops = {
  214. .owner = THIS_MODULE,
  215. .open = acpi_video_bus_POST_open_fs,
  216. .read = seq_read,
  217. .write = acpi_video_bus_write_POST,
  218. .llseek = seq_lseek,
  219. .release = single_release,
  220. };
  221. static int acpi_video_bus_DOS_open_fs(struct inode *inode, struct file *file);
  222. static ssize_t acpi_video_bus_write_DOS(struct file *file,
  223. const char __user *buffer, size_t count, loff_t *data);
  224. static const struct file_operations acpi_video_bus_DOS_fops = {
  225. .owner = THIS_MODULE,
  226. .open = acpi_video_bus_DOS_open_fs,
  227. .read = seq_read,
  228. .write = acpi_video_bus_write_DOS,
  229. .llseek = seq_lseek,
  230. .release = single_release,
  231. };
  232. /* device */
  233. static int acpi_video_device_info_open_fs(struct inode *inode,
  234. struct file *file);
  235. static const struct file_operations acpi_video_device_info_fops = {
  236. .owner = THIS_MODULE,
  237. .open = acpi_video_device_info_open_fs,
  238. .read = seq_read,
  239. .llseek = seq_lseek,
  240. .release = single_release,
  241. };
  242. static int acpi_video_device_state_open_fs(struct inode *inode,
  243. struct file *file);
  244. static ssize_t acpi_video_device_write_state(struct file *file,
  245. const char __user *buffer, size_t count, loff_t *data);
  246. static const struct file_operations acpi_video_device_state_fops = {
  247. .owner = THIS_MODULE,
  248. .open = acpi_video_device_state_open_fs,
  249. .read = seq_read,
  250. .write = acpi_video_device_write_state,
  251. .llseek = seq_lseek,
  252. .release = single_release,
  253. };
  254. static int acpi_video_device_brightness_open_fs(struct inode *inode,
  255. struct file *file);
  256. static ssize_t acpi_video_device_write_brightness(struct file *file,
  257. const char __user *buffer, size_t count, loff_t *data);
  258. static const struct file_operations acpi_video_device_brightness_fops = {
  259. .owner = THIS_MODULE,
  260. .open = acpi_video_device_brightness_open_fs,
  261. .read = seq_read,
  262. .write = acpi_video_device_write_brightness,
  263. .llseek = seq_lseek,
  264. .release = single_release,
  265. };
  266. static int acpi_video_device_EDID_open_fs(struct inode *inode,
  267. struct file *file);
  268. static const struct file_operations acpi_video_device_EDID_fops = {
  269. .owner = THIS_MODULE,
  270. .open = acpi_video_device_EDID_open_fs,
  271. .read = seq_read,
  272. .llseek = seq_lseek,
  273. .release = single_release,
  274. };
  275. static const char device_decode[][30] = {
  276. "motherboard VGA device",
  277. "PCI VGA device",
  278. "AGP VGA device",
  279. "UNKNOWN",
  280. };
  281. static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data);
  282. static void acpi_video_device_rebind(struct acpi_video_bus *video);
  283. static void acpi_video_device_bind(struct acpi_video_bus *video,
  284. struct acpi_video_device *device);
  285. static int acpi_video_device_enumerate(struct acpi_video_bus *video);
  286. static int acpi_video_device_lcd_set_level(struct acpi_video_device *device,
  287. int level);
  288. static int acpi_video_device_lcd_get_level_current(
  289. struct acpi_video_device *device,
  290. unsigned long long *level);
  291. static int acpi_video_get_next_level(struct acpi_video_device *device,
  292. u32 level_current, u32 event);
  293. static int acpi_video_switch_brightness(struct acpi_video_device *device,
  294. int event);
  295. static int acpi_video_device_get_state(struct acpi_video_device *device,
  296. unsigned long long *state);
  297. static int acpi_video_output_get(struct output_device *od);
  298. static int acpi_video_device_set_state(struct acpi_video_device *device, int state);
  299. /*backlight device sysfs support*/
  300. static int acpi_video_get_brightness(struct backlight_device *bd)
  301. {
  302. unsigned long long cur_level;
  303. int i;
  304. struct acpi_video_device *vd =
  305. (struct acpi_video_device *)bl_get_data(bd);
  306. if (acpi_video_device_lcd_get_level_current(vd, &cur_level))
  307. return -EINVAL;
  308. for (i = 2; i < vd->brightness->count; i++) {
  309. if (vd->brightness->levels[i] == cur_level)
  310. /* The first two entries are special - see page 575
  311. of the ACPI spec 3.0 */
  312. return i-2;
  313. }
  314. return 0;
  315. }
  316. static int acpi_video_set_brightness(struct backlight_device *bd)
  317. {
  318. int request_level = bd->props.brightness + 2;
  319. struct acpi_video_device *vd =
  320. (struct acpi_video_device *)bl_get_data(bd);
  321. return acpi_video_device_lcd_set_level(vd,
  322. vd->brightness->levels[request_level]);
  323. }
  324. static struct backlight_ops acpi_backlight_ops = {
  325. .get_brightness = acpi_video_get_brightness,
  326. .update_status = acpi_video_set_brightness,
  327. };
  328. /*video output device sysfs support*/
  329. static int acpi_video_output_get(struct output_device *od)
  330. {
  331. unsigned long long state;
  332. struct acpi_video_device *vd =
  333. (struct acpi_video_device *)dev_get_drvdata(&od->dev);
  334. acpi_video_device_get_state(vd, &state);
  335. return (int)state;
  336. }
  337. static int acpi_video_output_set(struct output_device *od)
  338. {
  339. unsigned long state = od->request_state;
  340. struct acpi_video_device *vd=
  341. (struct acpi_video_device *)dev_get_drvdata(&od->dev);
  342. return acpi_video_device_set_state(vd, state);
  343. }
  344. static struct output_properties acpi_output_properties = {
  345. .set_state = acpi_video_output_set,
  346. .get_status = acpi_video_output_get,
  347. };
  348. /* thermal cooling device callbacks */
  349. static int video_get_max_state(struct thermal_cooling_device *cooling_dev, unsigned
  350. long *state)
  351. {
  352. struct acpi_device *device = cooling_dev->devdata;
  353. struct acpi_video_device *video = acpi_driver_data(device);
  354. *state = video->brightness->count - 3;
  355. return 0;
  356. }
  357. static int video_get_cur_state(struct thermal_cooling_device *cooling_dev, unsigned
  358. long *state)
  359. {
  360. struct acpi_device *device = cooling_dev->devdata;
  361. struct acpi_video_device *video = acpi_driver_data(device);
  362. unsigned long long level;
  363. int offset;
  364. if (acpi_video_device_lcd_get_level_current(video, &level))
  365. return -EINVAL;
  366. for (offset = 2; offset < video->brightness->count; offset++)
  367. if (level == video->brightness->levels[offset]) {
  368. *state = video->brightness->count - offset - 1;
  369. return 0;
  370. }
  371. return -EINVAL;
  372. }
  373. static int
  374. video_set_cur_state(struct thermal_cooling_device *cooling_dev, unsigned long state)
  375. {
  376. struct acpi_device *device = cooling_dev->devdata;
  377. struct acpi_video_device *video = acpi_driver_data(device);
  378. int level;
  379. if ( state >= video->brightness->count - 2)
  380. return -EINVAL;
  381. state = video->brightness->count - state;
  382. level = video->brightness->levels[state -1];
  383. return acpi_video_device_lcd_set_level(video, level);
  384. }
  385. static struct thermal_cooling_device_ops video_cooling_ops = {
  386. .get_max_state = video_get_max_state,
  387. .get_cur_state = video_get_cur_state,
  388. .set_cur_state = video_set_cur_state,
  389. };
  390. /* --------------------------------------------------------------------------
  391. Video Management
  392. -------------------------------------------------------------------------- */
  393. /* device */
  394. static int
  395. acpi_video_device_query(struct acpi_video_device *device, unsigned long long *state)
  396. {
  397. int status;
  398. status = acpi_evaluate_integer(device->dev->handle, "_DGS", NULL, state);
  399. return status;
  400. }
  401. static int
  402. acpi_video_device_get_state(struct acpi_video_device *device,
  403. unsigned long long *state)
  404. {
  405. int status;
  406. status = acpi_evaluate_integer(device->dev->handle, "_DCS", NULL, state);
  407. return status;
  408. }
  409. static int
  410. acpi_video_device_set_state(struct acpi_video_device *device, int state)
  411. {
  412. int status;
  413. union acpi_object arg0 = { ACPI_TYPE_INTEGER };
  414. struct acpi_object_list args = { 1, &arg0 };
  415. unsigned long long ret;
  416. arg0.integer.value = state;
  417. status = acpi_evaluate_integer(device->dev->handle, "_DSS", &args, &ret);
  418. return status;
  419. }
  420. static int
  421. acpi_video_device_lcd_query_levels(struct acpi_video_device *device,
  422. union acpi_object **levels)
  423. {
  424. int status;
  425. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  426. union acpi_object *obj;
  427. *levels = NULL;
  428. status = acpi_evaluate_object(device->dev->handle, "_BCL", NULL, &buffer);
  429. if (!ACPI_SUCCESS(status))
  430. return status;
  431. obj = (union acpi_object *)buffer.pointer;
  432. if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
  433. printk(KERN_ERR PREFIX "Invalid _BCL data\n");
  434. status = -EFAULT;
  435. goto err;
  436. }
  437. *levels = obj;
  438. return 0;
  439. err:
  440. kfree(buffer.pointer);
  441. return status;
  442. }
  443. static int
  444. acpi_video_device_lcd_set_level(struct acpi_video_device *device, int level)
  445. {
  446. int status;
  447. union acpi_object arg0 = { ACPI_TYPE_INTEGER };
  448. struct acpi_object_list args = { 1, &arg0 };
  449. int state;
  450. arg0.integer.value = level;
  451. status = acpi_evaluate_object(device->dev->handle, "_BCM",
  452. &args, NULL);
  453. if (ACPI_FAILURE(status)) {
  454. ACPI_ERROR((AE_INFO, "Evaluating _BCM failed"));
  455. return -EIO;
  456. }
  457. device->brightness->curr = level;
  458. for (state = 2; state < device->brightness->count; state++)
  459. if (level == device->brightness->levels[state]) {
  460. if (device->backlight)
  461. device->backlight->props.brightness = state - 2;
  462. return 0;
  463. }
  464. ACPI_ERROR((AE_INFO, "Current brightness invalid"));
  465. return -EINVAL;
  466. }
  467. /*
  468. * For some buggy _BQC methods, we need to add a constant value to
  469. * the _BQC return value to get the actual current brightness level
  470. */
  471. static int bqc_offset_aml_bug_workaround;
  472. static int __init video_set_bqc_offset(const struct dmi_system_id *d)
  473. {
  474. bqc_offset_aml_bug_workaround = 9;
  475. return 0;
  476. }
  477. static struct dmi_system_id video_dmi_table[] __initdata = {
  478. /*
  479. * Broken _BQC workaround http://bugzilla.kernel.org/show_bug.cgi?id=13121
  480. */
  481. {
  482. .callback = video_set_bqc_offset,
  483. .ident = "Acer Aspire 5720",
  484. .matches = {
  485. DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
  486. DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5720"),
  487. },
  488. },
  489. {
  490. .callback = video_set_bqc_offset,
  491. .ident = "Acer Aspire 5710Z",
  492. .matches = {
  493. DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
  494. DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5710Z"),
  495. },
  496. },
  497. {
  498. .callback = video_set_bqc_offset,
  499. .ident = "eMachines E510",
  500. .matches = {
  501. DMI_MATCH(DMI_BOARD_VENDOR, "EMACHINES"),
  502. DMI_MATCH(DMI_PRODUCT_NAME, "eMachines E510"),
  503. },
  504. },
  505. {
  506. .callback = video_set_bqc_offset,
  507. .ident = "Acer Aspire 5315",
  508. .matches = {
  509. DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
  510. DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5315"),
  511. },
  512. },
  513. {
  514. .callback = video_set_bqc_offset,
  515. .ident = "Acer Aspire 7720",
  516. .matches = {
  517. DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
  518. DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 7720"),
  519. },
  520. },
  521. {}
  522. };
  523. static int
  524. acpi_video_device_lcd_get_level_current(struct acpi_video_device *device,
  525. unsigned long long *level)
  526. {
  527. acpi_status status = AE_OK;
  528. int i;
  529. if (device->cap._BQC || device->cap._BCQ) {
  530. char *buf = device->cap._BQC ? "_BQC" : "_BCQ";
  531. status = acpi_evaluate_integer(device->dev->handle, buf,
  532. NULL, level);
  533. if (ACPI_SUCCESS(status)) {
  534. if (device->brightness->flags._BQC_use_index) {
  535. if (device->brightness->flags._BCL_reversed)
  536. *level = device->brightness->count
  537. - 3 - (*level);
  538. *level = device->brightness->levels[*level + 2];
  539. }
  540. *level += bqc_offset_aml_bug_workaround;
  541. for (i = 2; i < device->brightness->count; i++)
  542. if (device->brightness->levels[i] == *level) {
  543. device->brightness->curr = *level;
  544. return 0;
  545. }
  546. /* BQC returned an invalid level. Stop using it. */
  547. ACPI_WARNING((AE_INFO, "%s returned an invalid level",
  548. buf));
  549. device->cap._BQC = device->cap._BCQ = 0;
  550. } else {
  551. /* Fixme:
  552. * should we return an error or ignore this failure?
  553. * dev->brightness->curr is a cached value which stores
  554. * the correct current backlight level in most cases.
  555. * ACPI video backlight still works w/ buggy _BQC.
  556. * http://bugzilla.kernel.org/show_bug.cgi?id=12233
  557. */
  558. ACPI_WARNING((AE_INFO, "Evaluating %s failed", buf));
  559. device->cap._BQC = device->cap._BCQ = 0;
  560. }
  561. }
  562. *level = device->brightness->curr;
  563. return 0;
  564. }
  565. static int
  566. acpi_video_device_EDID(struct acpi_video_device *device,
  567. union acpi_object **edid, ssize_t length)
  568. {
  569. int status;
  570. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  571. union acpi_object *obj;
  572. union acpi_object arg0 = { ACPI_TYPE_INTEGER };
  573. struct acpi_object_list args = { 1, &arg0 };
  574. *edid = NULL;
  575. if (!device)
  576. return -ENODEV;
  577. if (length == 128)
  578. arg0.integer.value = 1;
  579. else if (length == 256)
  580. arg0.integer.value = 2;
  581. else
  582. return -EINVAL;
  583. status = acpi_evaluate_object(device->dev->handle, "_DDC", &args, &buffer);
  584. if (ACPI_FAILURE(status))
  585. return -ENODEV;
  586. obj = buffer.pointer;
  587. if (obj && obj->type == ACPI_TYPE_BUFFER)
  588. *edid = obj;
  589. else {
  590. printk(KERN_ERR PREFIX "Invalid _DDC data\n");
  591. status = -EFAULT;
  592. kfree(obj);
  593. }
  594. return status;
  595. }
  596. /* bus */
  597. static int
  598. acpi_video_bus_set_POST(struct acpi_video_bus *video, unsigned long option)
  599. {
  600. int status;
  601. unsigned long long tmp;
  602. union acpi_object arg0 = { ACPI_TYPE_INTEGER };
  603. struct acpi_object_list args = { 1, &arg0 };
  604. arg0.integer.value = option;
  605. status = acpi_evaluate_integer(video->device->handle, "_SPD", &args, &tmp);
  606. if (ACPI_SUCCESS(status))
  607. status = tmp ? (-EINVAL) : (AE_OK);
  608. return status;
  609. }
  610. static int
  611. acpi_video_bus_get_POST(struct acpi_video_bus *video, unsigned long long *id)
  612. {
  613. int status;
  614. status = acpi_evaluate_integer(video->device->handle, "_GPD", NULL, id);
  615. return status;
  616. }
  617. static int
  618. acpi_video_bus_POST_options(struct acpi_video_bus *video,
  619. unsigned long long *options)
  620. {
  621. int status;
  622. status = acpi_evaluate_integer(video->device->handle, "_VPO", NULL, options);
  623. *options &= 3;
  624. return status;
  625. }
  626. /*
  627. * Arg:
  628. * video : video bus device pointer
  629. * bios_flag :
  630. * 0. The system BIOS should NOT automatically switch(toggle)
  631. * the active display output.
  632. * 1. The system BIOS should automatically switch (toggle) the
  633. * active display output. No switch event.
  634. * 2. The _DGS value should be locked.
  635. * 3. The system BIOS should not automatically switch (toggle) the
  636. * active display output, but instead generate the display switch
  637. * event notify code.
  638. * lcd_flag :
  639. * 0. The system BIOS should automatically control the brightness level
  640. * of the LCD when the power changes from AC to DC
  641. * 1. The system BIOS should NOT automatically control the brightness
  642. * level of the LCD when the power changes from AC to DC.
  643. * Return Value:
  644. * -1 wrong arg.
  645. */
  646. static int
  647. acpi_video_bus_DOS(struct acpi_video_bus *video, int bios_flag, int lcd_flag)
  648. {
  649. acpi_integer status = 0;
  650. union acpi_object arg0 = { ACPI_TYPE_INTEGER };
  651. struct acpi_object_list args = { 1, &arg0 };
  652. if (bios_flag < 0 || bios_flag > 3 || lcd_flag < 0 || lcd_flag > 1) {
  653. status = -1;
  654. goto Failed;
  655. }
  656. arg0.integer.value = (lcd_flag << 2) | bios_flag;
  657. video->dos_setting = arg0.integer.value;
  658. acpi_evaluate_object(video->device->handle, "_DOS", &args, NULL);
  659. Failed:
  660. return status;
  661. }
  662. /*
  663. * Simple comparison function used to sort backlight levels.
  664. */
  665. static int
  666. acpi_video_cmp_level(const void *a, const void *b)
  667. {
  668. return *(int *)a - *(int *)b;
  669. }
  670. /*
  671. * Arg:
  672. * device : video output device (LCD, CRT, ..)
  673. *
  674. * Return Value:
  675. * Maximum brightness level
  676. *
  677. * Allocate and initialize device->brightness.
  678. */
  679. static int
  680. acpi_video_init_brightness(struct acpi_video_device *device)
  681. {
  682. union acpi_object *obj = NULL;
  683. int i, max_level = 0, count = 0, level_ac_battery = 0;
  684. unsigned long long level, level_old;
  685. union acpi_object *o;
  686. struct acpi_video_device_brightness *br = NULL;
  687. int result = -EINVAL;
  688. if (!ACPI_SUCCESS(acpi_video_device_lcd_query_levels(device, &obj))) {
  689. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Could not query available "
  690. "LCD brightness level\n"));
  691. goto out;
  692. }
  693. if (obj->package.count < 2)
  694. goto out;
  695. br = kzalloc(sizeof(*br), GFP_KERNEL);
  696. if (!br) {
  697. printk(KERN_ERR "can't allocate memory\n");
  698. result = -ENOMEM;
  699. goto out;
  700. }
  701. br->levels = kmalloc((obj->package.count + 2) * sizeof *(br->levels),
  702. GFP_KERNEL);
  703. if (!br->levels) {
  704. result = -ENOMEM;
  705. goto out_free;
  706. }
  707. for (i = 0; i < obj->package.count; i++) {
  708. o = (union acpi_object *)&obj->package.elements[i];
  709. if (o->type != ACPI_TYPE_INTEGER) {
  710. printk(KERN_ERR PREFIX "Invalid data\n");
  711. continue;
  712. }
  713. br->levels[count] = (u32) o->integer.value;
  714. if (br->levels[count] > max_level)
  715. max_level = br->levels[count];
  716. count++;
  717. }
  718. /*
  719. * some buggy BIOS don't export the levels
  720. * when machine is on AC/Battery in _BCL package.
  721. * In this case, the first two elements in _BCL packages
  722. * are also supported brightness levels that OS should take care of.
  723. */
  724. for (i = 2; i < count; i++) {
  725. if (br->levels[i] == br->levels[0])
  726. level_ac_battery++;
  727. if (br->levels[i] == br->levels[1])
  728. level_ac_battery++;
  729. }
  730. if (level_ac_battery < 2) {
  731. level_ac_battery = 2 - level_ac_battery;
  732. br->flags._BCL_no_ac_battery_levels = 1;
  733. for (i = (count - 1 + level_ac_battery); i >= 2; i--)
  734. br->levels[i] = br->levels[i - level_ac_battery];
  735. count += level_ac_battery;
  736. } else if (level_ac_battery > 2)
  737. ACPI_ERROR((AE_INFO, "Too many duplicates in _BCL package\n"));
  738. /* Check if the _BCL package is in a reversed order */
  739. if (max_level == br->levels[2]) {
  740. br->flags._BCL_reversed = 1;
  741. sort(&br->levels[2], count - 2, sizeof(br->levels[2]),
  742. acpi_video_cmp_level, NULL);
  743. } else if (max_level != br->levels[count - 1])
  744. ACPI_ERROR((AE_INFO,
  745. "Found unordered _BCL package\n"));
  746. br->count = count;
  747. device->brightness = br;
  748. /* Check the input/output of _BQC/_BCL/_BCM */
  749. if ((max_level < 100) && (max_level <= (count - 2)))
  750. br->flags._BCL_use_index = 1;
  751. /*
  752. * _BCM is always consistent with _BCL,
  753. * at least for all the laptops we have ever seen.
  754. */
  755. br->flags._BCM_use_index = br->flags._BCL_use_index;
  756. /* _BQC uses INDEX while _BCL uses VALUE in some laptops */
  757. br->curr = level = max_level;
  758. if (!device->cap._BQC)
  759. goto set_level;
  760. result = acpi_video_device_lcd_get_level_current(device, &level_old);
  761. if (result)
  762. goto out_free_levels;
  763. /*
  764. * Set the level to maximum and check if _BQC uses indexed value
  765. */
  766. result = acpi_video_device_lcd_set_level(device, max_level);
  767. if (result)
  768. goto out_free_levels;
  769. result = acpi_video_device_lcd_get_level_current(device, &level);
  770. if (result)
  771. goto out_free_levels;
  772. br->flags._BQC_use_index = (level == max_level ? 0 : 1);
  773. if (!br->flags._BQC_use_index) {
  774. /*
  775. * Set the backlight to the initial state.
  776. * On some buggy laptops, _BQC returns an uninitialized value
  777. * when invoked for the first time, i.e. level_old is invalid.
  778. * set the backlight to max_level in this case
  779. */
  780. for (i = 2; i < br->count; i++)
  781. if (level_old == br->levels[i])
  782. level = level_old;
  783. goto set_level;
  784. }
  785. if (br->flags._BCL_reversed)
  786. level_old = (br->count - 1) - level_old;
  787. level = br->levels[level_old];
  788. set_level:
  789. result = acpi_video_device_lcd_set_level(device, level);
  790. if (result)
  791. goto out_free_levels;
  792. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  793. "found %d brightness levels\n", count - 2));
  794. kfree(obj);
  795. return result;
  796. out_free_levels:
  797. kfree(br->levels);
  798. out_free:
  799. kfree(br);
  800. out:
  801. device->brightness = NULL;
  802. kfree(obj);
  803. return result;
  804. }
  805. /*
  806. * Arg:
  807. * device : video output device (LCD, CRT, ..)
  808. *
  809. * Return Value:
  810. * None
  811. *
  812. * Find out all required AML methods defined under the output
  813. * device.
  814. */
  815. static void acpi_video_device_find_cap(struct acpi_video_device *device)
  816. {
  817. acpi_handle h_dummy1;
  818. if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_ADR", &h_dummy1))) {
  819. device->cap._ADR = 1;
  820. }
  821. if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_BCL", &h_dummy1))) {
  822. device->cap._BCL = 1;
  823. }
  824. if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_BCM", &h_dummy1))) {
  825. device->cap._BCM = 1;
  826. }
  827. if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle,"_BQC",&h_dummy1)))
  828. device->cap._BQC = 1;
  829. else if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_BCQ",
  830. &h_dummy1))) {
  831. printk(KERN_WARNING FW_BUG "_BCQ is used instead of _BQC\n");
  832. device->cap._BCQ = 1;
  833. }
  834. if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DDC", &h_dummy1))) {
  835. device->cap._DDC = 1;
  836. }
  837. if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DCS", &h_dummy1))) {
  838. device->cap._DCS = 1;
  839. }
  840. if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DGS", &h_dummy1))) {
  841. device->cap._DGS = 1;
  842. }
  843. if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DSS", &h_dummy1))) {
  844. device->cap._DSS = 1;
  845. }
  846. if (acpi_video_backlight_support()) {
  847. int result;
  848. static int count = 0;
  849. char *name;
  850. result = acpi_video_init_brightness(device);
  851. if (result)
  852. return;
  853. name = kzalloc(MAX_NAME_LEN, GFP_KERNEL);
  854. if (!name)
  855. return;
  856. sprintf(name, "acpi_video%d", count++);
  857. device->backlight = backlight_device_register(name,
  858. NULL, device, &acpi_backlight_ops);
  859. device->backlight->props.max_brightness = device->brightness->count-3;
  860. kfree(name);
  861. result = sysfs_create_link(&device->backlight->dev.kobj,
  862. &device->dev->dev.kobj, "device");
  863. if (result)
  864. printk(KERN_ERR PREFIX "Create sysfs link\n");
  865. device->cooling_dev = thermal_cooling_device_register("LCD",
  866. device->dev, &video_cooling_ops);
  867. if (IS_ERR(device->cooling_dev)) {
  868. /*
  869. * Set cooling_dev to NULL so we don't crash trying to
  870. * free it.
  871. * Also, why the hell we are returning early and
  872. * not attempt to register video output if cooling
  873. * device registration failed?
  874. * -- dtor
  875. */
  876. device->cooling_dev = NULL;
  877. return;
  878. }
  879. dev_info(&device->dev->dev, "registered as cooling_device%d\n",
  880. device->cooling_dev->id);
  881. result = sysfs_create_link(&device->dev->dev.kobj,
  882. &device->cooling_dev->device.kobj,
  883. "thermal_cooling");
  884. if (result)
  885. printk(KERN_ERR PREFIX "Create sysfs link\n");
  886. result = sysfs_create_link(&device->cooling_dev->device.kobj,
  887. &device->dev->dev.kobj, "device");
  888. if (result)
  889. printk(KERN_ERR PREFIX "Create sysfs link\n");
  890. }
  891. if (acpi_video_display_switch_support()) {
  892. if (device->cap._DCS && device->cap._DSS) {
  893. static int count;
  894. char *name;
  895. name = kzalloc(MAX_NAME_LEN, GFP_KERNEL);
  896. if (!name)
  897. return;
  898. sprintf(name, "acpi_video%d", count++);
  899. device->output_dev = video_output_register(name,
  900. NULL, device, &acpi_output_properties);
  901. kfree(name);
  902. }
  903. }
  904. }
  905. /*
  906. * Arg:
  907. * device : video output device (VGA)
  908. *
  909. * Return Value:
  910. * None
  911. *
  912. * Find out all required AML methods defined under the video bus device.
  913. */
  914. static void acpi_video_bus_find_cap(struct acpi_video_bus *video)
  915. {
  916. acpi_handle h_dummy1;
  917. if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_DOS", &h_dummy1))) {
  918. video->cap._DOS = 1;
  919. }
  920. if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_DOD", &h_dummy1))) {
  921. video->cap._DOD = 1;
  922. }
  923. if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_ROM", &h_dummy1))) {
  924. video->cap._ROM = 1;
  925. }
  926. if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_GPD", &h_dummy1))) {
  927. video->cap._GPD = 1;
  928. }
  929. if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_SPD", &h_dummy1))) {
  930. video->cap._SPD = 1;
  931. }
  932. if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_VPO", &h_dummy1))) {
  933. video->cap._VPO = 1;
  934. }
  935. }
  936. /*
  937. * Check whether the video bus device has required AML method to
  938. * support the desired features
  939. */
  940. static int acpi_video_bus_check(struct acpi_video_bus *video)
  941. {
  942. acpi_status status = -ENOENT;
  943. struct pci_dev *dev;
  944. if (!video)
  945. return -EINVAL;
  946. dev = acpi_get_pci_dev(video->device->handle);
  947. if (!dev)
  948. return -ENODEV;
  949. pci_dev_put(dev);
  950. /* Since there is no HID, CID and so on for VGA driver, we have
  951. * to check well known required nodes.
  952. */
  953. /* Does this device support video switching? */
  954. if (video->cap._DOS) {
  955. video->flags.multihead = 1;
  956. status = 0;
  957. }
  958. /* Does this device support retrieving a video ROM? */
  959. if (video->cap._ROM) {
  960. video->flags.rom = 1;
  961. status = 0;
  962. }
  963. /* Does this device support configuring which video device to POST? */
  964. if (video->cap._GPD && video->cap._SPD && video->cap._VPO) {
  965. video->flags.post = 1;
  966. status = 0;
  967. }
  968. return status;
  969. }
  970. /* --------------------------------------------------------------------------
  971. FS Interface (/proc)
  972. -------------------------------------------------------------------------- */
  973. static struct proc_dir_entry *acpi_video_dir;
  974. /* video devices */
  975. static int acpi_video_device_info_seq_show(struct seq_file *seq, void *offset)
  976. {
  977. struct acpi_video_device *dev = seq->private;
  978. if (!dev)
  979. goto end;
  980. seq_printf(seq, "device_id: 0x%04x\n", (u32) dev->device_id);
  981. seq_printf(seq, "type: ");
  982. if (dev->flags.crt)
  983. seq_printf(seq, "CRT\n");
  984. else if (dev->flags.lcd)
  985. seq_printf(seq, "LCD\n");
  986. else if (dev->flags.tvout)
  987. seq_printf(seq, "TVOUT\n");
  988. else if (dev->flags.dvi)
  989. seq_printf(seq, "DVI\n");
  990. else
  991. seq_printf(seq, "UNKNOWN\n");
  992. seq_printf(seq, "known by bios: %s\n", dev->flags.bios ? "yes" : "no");
  993. end:
  994. return 0;
  995. }
  996. static int
  997. acpi_video_device_info_open_fs(struct inode *inode, struct file *file)
  998. {
  999. return single_open(file, acpi_video_device_info_seq_show,
  1000. PDE(inode)->data);
  1001. }
  1002. static int acpi_video_device_state_seq_show(struct seq_file *seq, void *offset)
  1003. {
  1004. int status;
  1005. struct acpi_video_device *dev = seq->private;
  1006. unsigned long long state;
  1007. if (!dev)
  1008. goto end;
  1009. status = acpi_video_device_get_state(dev, &state);
  1010. seq_printf(seq, "state: ");
  1011. if (ACPI_SUCCESS(status))
  1012. seq_printf(seq, "0x%02llx\n", state);
  1013. else
  1014. seq_printf(seq, "<not supported>\n");
  1015. status = acpi_video_device_query(dev, &state);
  1016. seq_printf(seq, "query: ");
  1017. if (ACPI_SUCCESS(status))
  1018. seq_printf(seq, "0x%02llx\n", state);
  1019. else
  1020. seq_printf(seq, "<not supported>\n");
  1021. end:
  1022. return 0;
  1023. }
  1024. static int
  1025. acpi_video_device_state_open_fs(struct inode *inode, struct file *file)
  1026. {
  1027. return single_open(file, acpi_video_device_state_seq_show,
  1028. PDE(inode)->data);
  1029. }
  1030. static ssize_t
  1031. acpi_video_device_write_state(struct file *file,
  1032. const char __user * buffer,
  1033. size_t count, loff_t * data)
  1034. {
  1035. int status;
  1036. struct seq_file *m = file->private_data;
  1037. struct acpi_video_device *dev = m->private;
  1038. char str[12] = { 0 };
  1039. u32 state = 0;
  1040. if (!dev || count + 1 > sizeof str)
  1041. return -EINVAL;
  1042. if (copy_from_user(str, buffer, count))
  1043. return -EFAULT;
  1044. str[count] = 0;
  1045. state = simple_strtoul(str, NULL, 0);
  1046. state &= ((1ul << 31) | (1ul << 30) | (1ul << 0));
  1047. status = acpi_video_device_set_state(dev, state);
  1048. if (status)
  1049. return -EFAULT;
  1050. return count;
  1051. }
  1052. static int
  1053. acpi_video_device_brightness_seq_show(struct seq_file *seq, void *offset)
  1054. {
  1055. struct acpi_video_device *dev = seq->private;
  1056. int i;
  1057. if (!dev || !dev->brightness) {
  1058. seq_printf(seq, "<not supported>\n");
  1059. return 0;
  1060. }
  1061. seq_printf(seq, "levels: ");
  1062. for (i = 2; i < dev->brightness->count; i++)
  1063. seq_printf(seq, " %d", dev->brightness->levels[i]);
  1064. seq_printf(seq, "\ncurrent: %d\n", dev->brightness->curr);
  1065. return 0;
  1066. }
  1067. static int
  1068. acpi_video_device_brightness_open_fs(struct inode *inode, struct file *file)
  1069. {
  1070. return single_open(file, acpi_video_device_brightness_seq_show,
  1071. PDE(inode)->data);
  1072. }
  1073. static ssize_t
  1074. acpi_video_device_write_brightness(struct file *file,
  1075. const char __user * buffer,
  1076. size_t count, loff_t * data)
  1077. {
  1078. struct seq_file *m = file->private_data;
  1079. struct acpi_video_device *dev = m->private;
  1080. char str[5] = { 0 };
  1081. unsigned int level = 0;
  1082. int i;
  1083. if (!dev || !dev->brightness || count + 1 > sizeof str)
  1084. return -EINVAL;
  1085. if (copy_from_user(str, buffer, count))
  1086. return -EFAULT;
  1087. str[count] = 0;
  1088. level = simple_strtoul(str, NULL, 0);
  1089. if (level > 100)
  1090. return -EFAULT;
  1091. /* validate through the list of available levels */
  1092. for (i = 2; i < dev->brightness->count; i++)
  1093. if (level == dev->brightness->levels[i]) {
  1094. if (!acpi_video_device_lcd_set_level(dev, level))
  1095. return count;
  1096. break;
  1097. }
  1098. return -EINVAL;
  1099. }
  1100. static int acpi_video_device_EDID_seq_show(struct seq_file *seq, void *offset)
  1101. {
  1102. struct acpi_video_device *dev = seq->private;
  1103. int status;
  1104. int i;
  1105. union acpi_object *edid = NULL;
  1106. if (!dev)
  1107. goto out;
  1108. status = acpi_video_device_EDID(dev, &edid, 128);
  1109. if (ACPI_FAILURE(status)) {
  1110. status = acpi_video_device_EDID(dev, &edid, 256);
  1111. }
  1112. if (ACPI_FAILURE(status)) {
  1113. goto out;
  1114. }
  1115. if (edid && edid->type == ACPI_TYPE_BUFFER) {
  1116. for (i = 0; i < edid->buffer.length; i++)
  1117. seq_putc(seq, edid->buffer.pointer[i]);
  1118. }
  1119. out:
  1120. if (!edid)
  1121. seq_printf(seq, "<not supported>\n");
  1122. else
  1123. kfree(edid);
  1124. return 0;
  1125. }
  1126. static int
  1127. acpi_video_device_EDID_open_fs(struct inode *inode, struct file *file)
  1128. {
  1129. return single_open(file, acpi_video_device_EDID_seq_show,
  1130. PDE(inode)->data);
  1131. }
  1132. static int acpi_video_device_add_fs(struct acpi_device *device)
  1133. {
  1134. struct proc_dir_entry *entry, *device_dir;
  1135. struct acpi_video_device *vid_dev;
  1136. vid_dev = acpi_driver_data(device);
  1137. if (!vid_dev)
  1138. return -ENODEV;
  1139. device_dir = proc_mkdir(acpi_device_bid(device),
  1140. vid_dev->video->dir);
  1141. if (!device_dir)
  1142. return -ENOMEM;
  1143. /* 'info' [R] */
  1144. entry = proc_create_data("info", S_IRUGO, device_dir,
  1145. &acpi_video_device_info_fops, acpi_driver_data(device));
  1146. if (!entry)
  1147. goto err_remove_dir;
  1148. /* 'state' [R/W] */
  1149. entry = proc_create_data("state", S_IFREG | S_IRUGO | S_IWUSR,
  1150. device_dir,
  1151. &acpi_video_device_state_fops,
  1152. acpi_driver_data(device));
  1153. if (!entry)
  1154. goto err_remove_info;
  1155. /* 'brightness' [R/W] */
  1156. entry = proc_create_data("brightness", S_IFREG | S_IRUGO | S_IWUSR,
  1157. device_dir,
  1158. &acpi_video_device_brightness_fops,
  1159. acpi_driver_data(device));
  1160. if (!entry)
  1161. goto err_remove_state;
  1162. /* 'EDID' [R] */
  1163. entry = proc_create_data("EDID", S_IRUGO, device_dir,
  1164. &acpi_video_device_EDID_fops,
  1165. acpi_driver_data(device));
  1166. if (!entry)
  1167. goto err_remove_brightness;
  1168. acpi_device_dir(device) = device_dir;
  1169. return 0;
  1170. err_remove_brightness:
  1171. remove_proc_entry("brightness", device_dir);
  1172. err_remove_state:
  1173. remove_proc_entry("state", device_dir);
  1174. err_remove_info:
  1175. remove_proc_entry("info", device_dir);
  1176. err_remove_dir:
  1177. remove_proc_entry(acpi_device_bid(device), vid_dev->video->dir);
  1178. return -ENOMEM;
  1179. }
  1180. static int acpi_video_device_remove_fs(struct acpi_device *device)
  1181. {
  1182. struct acpi_video_device *vid_dev;
  1183. struct proc_dir_entry *device_dir;
  1184. vid_dev = acpi_driver_data(device);
  1185. if (!vid_dev || !vid_dev->video || !vid_dev->video->dir)
  1186. return -ENODEV;
  1187. device_dir = acpi_device_dir(device);
  1188. if (device_dir) {
  1189. remove_proc_entry("info", device_dir);
  1190. remove_proc_entry("state", device_dir);
  1191. remove_proc_entry("brightness", device_dir);
  1192. remove_proc_entry("EDID", device_dir);
  1193. remove_proc_entry(acpi_device_bid(device), vid_dev->video->dir);
  1194. acpi_device_dir(device) = NULL;
  1195. }
  1196. return 0;
  1197. }
  1198. /* video bus */
  1199. static int acpi_video_bus_info_seq_show(struct seq_file *seq, void *offset)
  1200. {
  1201. struct acpi_video_bus *video = seq->private;
  1202. if (!video)
  1203. goto end;
  1204. seq_printf(seq, "Switching heads: %s\n",
  1205. video->flags.multihead ? "yes" : "no");
  1206. seq_printf(seq, "Video ROM: %s\n",
  1207. video->flags.rom ? "yes" : "no");
  1208. seq_printf(seq, "Device to be POSTed on boot: %s\n",
  1209. video->flags.post ? "yes" : "no");
  1210. end:
  1211. return 0;
  1212. }
  1213. static int acpi_video_bus_info_open_fs(struct inode *inode, struct file *file)
  1214. {
  1215. return single_open(file, acpi_video_bus_info_seq_show,
  1216. PDE(inode)->data);
  1217. }
  1218. static int acpi_video_bus_ROM_seq_show(struct seq_file *seq, void *offset)
  1219. {
  1220. struct acpi_video_bus *video = seq->private;
  1221. if (!video)
  1222. goto end;
  1223. printk(KERN_INFO PREFIX "Please implement %s\n", __func__);
  1224. seq_printf(seq, "<TODO>\n");
  1225. end:
  1226. return 0;
  1227. }
  1228. static int acpi_video_bus_ROM_open_fs(struct inode *inode, struct file *file)
  1229. {
  1230. return single_open(file, acpi_video_bus_ROM_seq_show, PDE(inode)->data);
  1231. }
  1232. static int acpi_video_bus_POST_info_seq_show(struct seq_file *seq, void *offset)
  1233. {
  1234. struct acpi_video_bus *video = seq->private;
  1235. unsigned long long options;
  1236. int status;
  1237. if (!video)
  1238. goto end;
  1239. status = acpi_video_bus_POST_options(video, &options);
  1240. if (ACPI_SUCCESS(status)) {
  1241. if (!(options & 1)) {
  1242. printk(KERN_WARNING PREFIX
  1243. "The motherboard VGA device is not listed as a possible POST device.\n");
  1244. printk(KERN_WARNING PREFIX
  1245. "This indicates a BIOS bug. Please contact the manufacturer.\n");
  1246. }
  1247. printk(KERN_WARNING "%llx\n", options);
  1248. seq_printf(seq, "can POST: <integrated video>");
  1249. if (options & 2)
  1250. seq_printf(seq, " <PCI video>");
  1251. if (options & 4)
  1252. seq_printf(seq, " <AGP video>");
  1253. seq_putc(seq, '\n');
  1254. } else
  1255. seq_printf(seq, "<not supported>\n");
  1256. end:
  1257. return 0;
  1258. }
  1259. static int
  1260. acpi_video_bus_POST_info_open_fs(struct inode *inode, struct file *file)
  1261. {
  1262. return single_open(file, acpi_video_bus_POST_info_seq_show,
  1263. PDE(inode)->data);
  1264. }
  1265. static int acpi_video_bus_POST_seq_show(struct seq_file *seq, void *offset)
  1266. {
  1267. struct acpi_video_bus *video = seq->private;
  1268. int status;
  1269. unsigned long long id;
  1270. if (!video)
  1271. goto end;
  1272. status = acpi_video_bus_get_POST(video, &id);
  1273. if (!ACPI_SUCCESS(status)) {
  1274. seq_printf(seq, "<not supported>\n");
  1275. goto end;
  1276. }
  1277. seq_printf(seq, "device POSTed is <%s>\n", device_decode[id & 3]);
  1278. end:
  1279. return 0;
  1280. }
  1281. static int acpi_video_bus_DOS_seq_show(struct seq_file *seq, void *offset)
  1282. {
  1283. struct acpi_video_bus *video = seq->private;
  1284. seq_printf(seq, "DOS setting: <%d>\n", video->dos_setting);
  1285. return 0;
  1286. }
  1287. static int acpi_video_bus_POST_open_fs(struct inode *inode, struct file *file)
  1288. {
  1289. return single_open(file, acpi_video_bus_POST_seq_show,
  1290. PDE(inode)->data);
  1291. }
  1292. static int acpi_video_bus_DOS_open_fs(struct inode *inode, struct file *file)
  1293. {
  1294. return single_open(file, acpi_video_bus_DOS_seq_show, PDE(inode)->data);
  1295. }
  1296. static ssize_t
  1297. acpi_video_bus_write_POST(struct file *file,
  1298. const char __user * buffer,
  1299. size_t count, loff_t * data)
  1300. {
  1301. int status;
  1302. struct seq_file *m = file->private_data;
  1303. struct acpi_video_bus *video = m->private;
  1304. char str[12] = { 0 };
  1305. unsigned long long opt, options;
  1306. if (!video || count + 1 > sizeof str)
  1307. return -EINVAL;
  1308. status = acpi_video_bus_POST_options(video, &options);
  1309. if (!ACPI_SUCCESS(status))
  1310. return -EINVAL;
  1311. if (copy_from_user(str, buffer, count))
  1312. return -EFAULT;
  1313. str[count] = 0;
  1314. opt = strtoul(str, NULL, 0);
  1315. if (opt > 3)
  1316. return -EFAULT;
  1317. /* just in case an OEM 'forgot' the motherboard... */
  1318. options |= 1;
  1319. if (options & (1ul << opt)) {
  1320. status = acpi_video_bus_set_POST(video, opt);
  1321. if (!ACPI_SUCCESS(status))
  1322. return -EFAULT;
  1323. }
  1324. return count;
  1325. }
  1326. static ssize_t
  1327. acpi_video_bus_write_DOS(struct file *file,
  1328. const char __user * buffer,
  1329. size_t count, loff_t * data)
  1330. {
  1331. int status;
  1332. struct seq_file *m = file->private_data;
  1333. struct acpi_video_bus *video = m->private;
  1334. char str[12] = { 0 };
  1335. unsigned long opt;
  1336. if (!video || count + 1 > sizeof str)
  1337. return -EINVAL;
  1338. if (copy_from_user(str, buffer, count))
  1339. return -EFAULT;
  1340. str[count] = 0;
  1341. opt = strtoul(str, NULL, 0);
  1342. if (opt > 7)
  1343. return -EFAULT;
  1344. status = acpi_video_bus_DOS(video, opt & 0x3, (opt & 0x4) >> 2);
  1345. if (!ACPI_SUCCESS(status))
  1346. return -EFAULT;
  1347. return count;
  1348. }
  1349. static int acpi_video_bus_add_fs(struct acpi_device *device)
  1350. {
  1351. struct acpi_video_bus *video = acpi_driver_data(device);
  1352. struct proc_dir_entry *device_dir;
  1353. struct proc_dir_entry *entry;
  1354. device_dir = proc_mkdir(acpi_device_bid(device), acpi_video_dir);
  1355. if (!device_dir)
  1356. return -ENOMEM;
  1357. /* 'info' [R] */
  1358. entry = proc_create_data("info", S_IRUGO, device_dir,
  1359. &acpi_video_bus_info_fops,
  1360. acpi_driver_data(device));
  1361. if (!entry)
  1362. goto err_remove_dir;
  1363. /* 'ROM' [R] */
  1364. entry = proc_create_data("ROM", S_IRUGO, device_dir,
  1365. &acpi_video_bus_ROM_fops,
  1366. acpi_driver_data(device));
  1367. if (!entry)
  1368. goto err_remove_info;
  1369. /* 'POST_info' [R] */
  1370. entry = proc_create_data("POST_info", S_IRUGO, device_dir,
  1371. &acpi_video_bus_POST_info_fops,
  1372. acpi_driver_data(device));
  1373. if (!entry)
  1374. goto err_remove_rom;
  1375. /* 'POST' [R/W] */
  1376. entry = proc_create_data("POST", S_IFREG | S_IRUGO | S_IWUSR,
  1377. device_dir,
  1378. &acpi_video_bus_POST_fops,
  1379. acpi_driver_data(device));
  1380. if (!entry)
  1381. goto err_remove_post_info;
  1382. /* 'DOS' [R/W] */
  1383. entry = proc_create_data("DOS", S_IFREG | S_IRUGO | S_IWUSR,
  1384. device_dir,
  1385. &acpi_video_bus_DOS_fops,
  1386. acpi_driver_data(device));
  1387. if (!entry)
  1388. goto err_remove_post;
  1389. video->dir = acpi_device_dir(device) = device_dir;
  1390. return 0;
  1391. err_remove_post:
  1392. remove_proc_entry("POST", device_dir);
  1393. err_remove_post_info:
  1394. remove_proc_entry("POST_info", device_dir);
  1395. err_remove_rom:
  1396. remove_proc_entry("ROM", device_dir);
  1397. err_remove_info:
  1398. remove_proc_entry("info", device_dir);
  1399. err_remove_dir:
  1400. remove_proc_entry(acpi_device_bid(device), acpi_video_dir);
  1401. return -ENOMEM;
  1402. }
  1403. static int acpi_video_bus_remove_fs(struct acpi_device *device)
  1404. {
  1405. struct proc_dir_entry *device_dir = acpi_device_dir(device);
  1406. if (device_dir) {
  1407. remove_proc_entry("info", device_dir);
  1408. remove_proc_entry("ROM", device_dir);
  1409. remove_proc_entry("POST_info", device_dir);
  1410. remove_proc_entry("POST", device_dir);
  1411. remove_proc_entry("DOS", device_dir);
  1412. remove_proc_entry(acpi_device_bid(device), acpi_video_dir);
  1413. acpi_device_dir(device) = NULL;
  1414. }
  1415. return 0;
  1416. }
  1417. /* --------------------------------------------------------------------------
  1418. Driver Interface
  1419. -------------------------------------------------------------------------- */
  1420. /* device interface */
  1421. static struct acpi_video_device_attrib*
  1422. acpi_video_get_device_attr(struct acpi_video_bus *video, unsigned long device_id)
  1423. {
  1424. struct acpi_video_enumerated_device *ids;
  1425. int i;
  1426. for (i = 0; i < video->attached_count; i++) {
  1427. ids = &video->attached_array[i];
  1428. if ((ids->value.int_val & 0xffff) == device_id)
  1429. return &ids->value.attrib;
  1430. }
  1431. return NULL;
  1432. }
  1433. static int
  1434. acpi_video_bus_get_one_device(struct acpi_device *device,
  1435. struct acpi_video_bus *video)
  1436. {
  1437. unsigned long long device_id;
  1438. int status;
  1439. struct acpi_video_device *data;
  1440. struct acpi_video_device_attrib* attribute;
  1441. if (!device || !video)
  1442. return -EINVAL;
  1443. status =
  1444. acpi_evaluate_integer(device->handle, "_ADR", NULL, &device_id);
  1445. if (ACPI_SUCCESS(status)) {
  1446. data = kzalloc(sizeof(struct acpi_video_device), GFP_KERNEL);
  1447. if (!data)
  1448. return -ENOMEM;
  1449. strcpy(acpi_device_name(device), ACPI_VIDEO_DEVICE_NAME);
  1450. strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
  1451. device->driver_data = data;
  1452. data->device_id = device_id;
  1453. data->video = video;
  1454. data->dev = device;
  1455. attribute = acpi_video_get_device_attr(video, device_id);
  1456. if((attribute != NULL) && attribute->device_id_scheme) {
  1457. switch (attribute->display_type) {
  1458. case ACPI_VIDEO_DISPLAY_CRT:
  1459. data->flags.crt = 1;
  1460. break;
  1461. case ACPI_VIDEO_DISPLAY_TV:
  1462. data->flags.tvout = 1;
  1463. break;
  1464. case ACPI_VIDEO_DISPLAY_DVI:
  1465. data->flags.dvi = 1;
  1466. break;
  1467. case ACPI_VIDEO_DISPLAY_LCD:
  1468. data->flags.lcd = 1;
  1469. break;
  1470. default:
  1471. data->flags.unknown = 1;
  1472. break;
  1473. }
  1474. if(attribute->bios_can_detect)
  1475. data->flags.bios = 1;
  1476. } else
  1477. data->flags.unknown = 1;
  1478. acpi_video_device_bind(video, data);
  1479. acpi_video_device_find_cap(data);
  1480. status = acpi_install_notify_handler(device->handle,
  1481. ACPI_DEVICE_NOTIFY,
  1482. acpi_video_device_notify,
  1483. data);
  1484. if (ACPI_FAILURE(status)) {
  1485. printk(KERN_ERR PREFIX
  1486. "Error installing notify handler\n");
  1487. if(data->brightness)
  1488. kfree(data->brightness->levels);
  1489. kfree(data->brightness);
  1490. kfree(data);
  1491. return -ENODEV;
  1492. }
  1493. mutex_lock(&video->device_list_lock);
  1494. list_add_tail(&data->entry, &video->video_device_list);
  1495. mutex_unlock(&video->device_list_lock);
  1496. acpi_video_device_add_fs(device);
  1497. return 0;
  1498. }
  1499. return -ENOENT;
  1500. }
  1501. /*
  1502. * Arg:
  1503. * video : video bus device
  1504. *
  1505. * Return:
  1506. * none
  1507. *
  1508. * Enumerate the video device list of the video bus,
  1509. * bind the ids with the corresponding video devices
  1510. * under the video bus.
  1511. */
  1512. static void acpi_video_device_rebind(struct acpi_video_bus *video)
  1513. {
  1514. struct acpi_video_device *dev;
  1515. mutex_lock(&video->device_list_lock);
  1516. list_for_each_entry(dev, &video->video_device_list, entry)
  1517. acpi_video_device_bind(video, dev);
  1518. mutex_unlock(&video->device_list_lock);
  1519. }
  1520. /*
  1521. * Arg:
  1522. * video : video bus device
  1523. * device : video output device under the video
  1524. * bus
  1525. *
  1526. * Return:
  1527. * none
  1528. *
  1529. * Bind the ids with the corresponding video devices
  1530. * under the video bus.
  1531. */
  1532. static void
  1533. acpi_video_device_bind(struct acpi_video_bus *video,
  1534. struct acpi_video_device *device)
  1535. {
  1536. struct acpi_video_enumerated_device *ids;
  1537. int i;
  1538. for (i = 0; i < video->attached_count; i++) {
  1539. ids = &video->attached_array[i];
  1540. if (device->device_id == (ids->value.int_val & 0xffff)) {
  1541. ids->bind_info = device;
  1542. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "device_bind %d\n", i));
  1543. }
  1544. }
  1545. }
  1546. /*
  1547. * Arg:
  1548. * video : video bus device
  1549. *
  1550. * Return:
  1551. * < 0 : error
  1552. *
  1553. * Call _DOD to enumerate all devices attached to display adapter
  1554. *
  1555. */
  1556. static int acpi_video_device_enumerate(struct acpi_video_bus *video)
  1557. {
  1558. int status;
  1559. int count;
  1560. int i;
  1561. struct acpi_video_enumerated_device *active_list;
  1562. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  1563. union acpi_object *dod = NULL;
  1564. union acpi_object *obj;
  1565. status = acpi_evaluate_object(video->device->handle, "_DOD", NULL, &buffer);
  1566. if (!ACPI_SUCCESS(status)) {
  1567. ACPI_EXCEPTION((AE_INFO, status, "Evaluating _DOD"));
  1568. return status;
  1569. }
  1570. dod = buffer.pointer;
  1571. if (!dod || (dod->type != ACPI_TYPE_PACKAGE)) {
  1572. ACPI_EXCEPTION((AE_INFO, status, "Invalid _DOD data"));
  1573. status = -EFAULT;
  1574. goto out;
  1575. }
  1576. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d video heads in _DOD\n",
  1577. dod->package.count));
  1578. active_list = kcalloc(1 + dod->package.count,
  1579. sizeof(struct acpi_video_enumerated_device),
  1580. GFP_KERNEL);
  1581. if (!active_list) {
  1582. status = -ENOMEM;
  1583. goto out;
  1584. }
  1585. count = 0;
  1586. for (i = 0; i < dod->package.count; i++) {
  1587. obj = &dod->package.elements[i];
  1588. if (obj->type != ACPI_TYPE_INTEGER) {
  1589. printk(KERN_ERR PREFIX
  1590. "Invalid _DOD data in element %d\n", i);
  1591. continue;
  1592. }
  1593. active_list[count].value.int_val = obj->integer.value;
  1594. active_list[count].bind_info = NULL;
  1595. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "dod element[%d] = %d\n", i,
  1596. (int)obj->integer.value));
  1597. count++;
  1598. }
  1599. kfree(video->attached_array);
  1600. video->attached_array = active_list;
  1601. video->attached_count = count;
  1602. out:
  1603. kfree(buffer.pointer);
  1604. return status;
  1605. }
  1606. static int
  1607. acpi_video_get_next_level(struct acpi_video_device *device,
  1608. u32 level_current, u32 event)
  1609. {
  1610. int min, max, min_above, max_below, i, l, delta = 255;
  1611. max = max_below = 0;
  1612. min = min_above = 255;
  1613. /* Find closest level to level_current */
  1614. for (i = 2; i < device->brightness->count; i++) {
  1615. l = device->brightness->levels[i];
  1616. if (abs(l - level_current) < abs(delta)) {
  1617. delta = l - level_current;
  1618. if (!delta)
  1619. break;
  1620. }
  1621. }
  1622. /* Ajust level_current to closest available level */
  1623. level_current += delta;
  1624. for (i = 2; i < device->brightness->count; i++) {
  1625. l = device->brightness->levels[i];
  1626. if (l < min)
  1627. min = l;
  1628. if (l > max)
  1629. max = l;
  1630. if (l < min_above && l > level_current)
  1631. min_above = l;
  1632. if (l > max_below && l < level_current)
  1633. max_below = l;
  1634. }
  1635. switch (event) {
  1636. case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS:
  1637. return (level_current < max) ? min_above : min;
  1638. case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS:
  1639. return (level_current < max) ? min_above : max;
  1640. case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS:
  1641. return (level_current > min) ? max_below : min;
  1642. case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS:
  1643. case ACPI_VIDEO_NOTIFY_DISPLAY_OFF:
  1644. return 0;
  1645. default:
  1646. return level_current;
  1647. }
  1648. }
  1649. static int
  1650. acpi_video_switch_brightness(struct acpi_video_device *device, int event)
  1651. {
  1652. unsigned long long level_current, level_next;
  1653. int result = -EINVAL;
  1654. if (!device->brightness)
  1655. goto out;
  1656. result = acpi_video_device_lcd_get_level_current(device,
  1657. &level_current);
  1658. if (result)
  1659. goto out;
  1660. level_next = acpi_video_get_next_level(device, level_current, event);
  1661. result = acpi_video_device_lcd_set_level(device, level_next);
  1662. if (!result)
  1663. backlight_force_update(device->backlight,
  1664. BACKLIGHT_UPDATE_HOTKEY);
  1665. out:
  1666. if (result)
  1667. printk(KERN_ERR PREFIX "Failed to switch the brightness\n");
  1668. return result;
  1669. }
  1670. static int
  1671. acpi_video_bus_get_devices(struct acpi_video_bus *video,
  1672. struct acpi_device *device)
  1673. {
  1674. int status = 0;
  1675. struct acpi_device *dev;
  1676. acpi_video_device_enumerate(video);
  1677. list_for_each_entry(dev, &device->children, node) {
  1678. status = acpi_video_bus_get_one_device(dev, video);
  1679. if (ACPI_FAILURE(status)) {
  1680. printk(KERN_WARNING PREFIX
  1681. "Cant attach device");
  1682. continue;
  1683. }
  1684. }
  1685. return status;
  1686. }
  1687. static int acpi_video_bus_put_one_device(struct acpi_video_device *device)
  1688. {
  1689. acpi_status status;
  1690. struct acpi_video_bus *video;
  1691. if (!device || !device->video)
  1692. return -ENOENT;
  1693. video = device->video;
  1694. acpi_video_device_remove_fs(device->dev);
  1695. status = acpi_remove_notify_handler(device->dev->handle,
  1696. ACPI_DEVICE_NOTIFY,
  1697. acpi_video_device_notify);
  1698. if (device->backlight) {
  1699. sysfs_remove_link(&device->backlight->dev.kobj, "device");
  1700. backlight_device_unregister(device->backlight);
  1701. device->backlight = NULL;
  1702. }
  1703. if (device->cooling_dev) {
  1704. sysfs_remove_link(&device->dev->dev.kobj,
  1705. "thermal_cooling");
  1706. sysfs_remove_link(&device->cooling_dev->device.kobj,
  1707. "device");
  1708. thermal_cooling_device_unregister(device->cooling_dev);
  1709. device->cooling_dev = NULL;
  1710. }
  1711. video_output_unregister(device->output_dev);
  1712. return 0;
  1713. }
  1714. static int acpi_video_bus_put_devices(struct acpi_video_bus *video)
  1715. {
  1716. int status;
  1717. struct acpi_video_device *dev, *next;
  1718. mutex_lock(&video->device_list_lock);
  1719. list_for_each_entry_safe(dev, next, &video->video_device_list, entry) {
  1720. status = acpi_video_bus_put_one_device(dev);
  1721. if (ACPI_FAILURE(status))
  1722. printk(KERN_WARNING PREFIX
  1723. "hhuuhhuu bug in acpi video driver.\n");
  1724. if (dev->brightness) {
  1725. kfree(dev->brightness->levels);
  1726. kfree(dev->brightness);
  1727. }
  1728. list_del(&dev->entry);
  1729. kfree(dev);
  1730. }
  1731. mutex_unlock(&video->device_list_lock);
  1732. return 0;
  1733. }
  1734. /* acpi_video interface */
  1735. static int acpi_video_bus_start_devices(struct acpi_video_bus *video)
  1736. {
  1737. return acpi_video_bus_DOS(video, 0, 0);
  1738. }
  1739. static int acpi_video_bus_stop_devices(struct acpi_video_bus *video)
  1740. {
  1741. return acpi_video_bus_DOS(video, 0, 1);
  1742. }
  1743. static void acpi_video_bus_notify(struct acpi_device *device, u32 event)
  1744. {
  1745. struct acpi_video_bus *video = acpi_driver_data(device);
  1746. struct input_dev *input;
  1747. int keycode;
  1748. if (!video)
  1749. return;
  1750. input = video->input;
  1751. switch (event) {
  1752. case ACPI_VIDEO_NOTIFY_SWITCH: /* User requested a switch,
  1753. * most likely via hotkey. */
  1754. acpi_bus_generate_proc_event(device, event, 0);
  1755. keycode = KEY_SWITCHVIDEOMODE;
  1756. break;
  1757. case ACPI_VIDEO_NOTIFY_PROBE: /* User plugged in or removed a video
  1758. * connector. */
  1759. acpi_video_device_enumerate(video);
  1760. acpi_video_device_rebind(video);
  1761. acpi_bus_generate_proc_event(device, event, 0);
  1762. keycode = KEY_SWITCHVIDEOMODE;
  1763. break;
  1764. case ACPI_VIDEO_NOTIFY_CYCLE: /* Cycle Display output hotkey pressed. */
  1765. acpi_bus_generate_proc_event(device, event, 0);
  1766. keycode = KEY_SWITCHVIDEOMODE;
  1767. break;
  1768. case ACPI_VIDEO_NOTIFY_NEXT_OUTPUT: /* Next Display output hotkey pressed. */
  1769. acpi_bus_generate_proc_event(device, event, 0);
  1770. keycode = KEY_VIDEO_NEXT;
  1771. break;
  1772. case ACPI_VIDEO_NOTIFY_PREV_OUTPUT: /* previous Display output hotkey pressed. */
  1773. acpi_bus_generate_proc_event(device, event, 0);
  1774. keycode = KEY_VIDEO_PREV;
  1775. break;
  1776. default:
  1777. keycode = KEY_UNKNOWN;
  1778. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  1779. "Unsupported event [0x%x]\n", event));
  1780. break;
  1781. }
  1782. acpi_notifier_call_chain(device, event, 0);
  1783. input_report_key(input, keycode, 1);
  1784. input_sync(input);
  1785. input_report_key(input, keycode, 0);
  1786. input_sync(input);
  1787. return;
  1788. }
  1789. static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data)
  1790. {
  1791. struct acpi_video_device *video_device = data;
  1792. struct acpi_device *device = NULL;
  1793. struct acpi_video_bus *bus;
  1794. struct input_dev *input;
  1795. int keycode;
  1796. if (!video_device)
  1797. return;
  1798. device = video_device->dev;
  1799. bus = video_device->video;
  1800. input = bus->input;
  1801. switch (event) {
  1802. case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS: /* Cycle brightness */
  1803. if (brightness_switch_enabled)
  1804. acpi_video_switch_brightness(video_device, event);
  1805. acpi_bus_generate_proc_event(device, event, 0);
  1806. keycode = KEY_BRIGHTNESS_CYCLE;
  1807. break;
  1808. case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS: /* Increase brightness */
  1809. if (brightness_switch_enabled)
  1810. acpi_video_switch_brightness(video_device, event);
  1811. acpi_bus_generate_proc_event(device, event, 0);
  1812. keycode = KEY_BRIGHTNESSUP;
  1813. break;
  1814. case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS: /* Decrease brightness */
  1815. if (brightness_switch_enabled)
  1816. acpi_video_switch_brightness(video_device, event);
  1817. acpi_bus_generate_proc_event(device, event, 0);
  1818. keycode = KEY_BRIGHTNESSDOWN;
  1819. break;
  1820. case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS: /* zero brightnesss */
  1821. if (brightness_switch_enabled)
  1822. acpi_video_switch_brightness(video_device, event);
  1823. acpi_bus_generate_proc_event(device, event, 0);
  1824. keycode = KEY_BRIGHTNESS_ZERO;
  1825. break;
  1826. case ACPI_VIDEO_NOTIFY_DISPLAY_OFF: /* display device off */
  1827. if (brightness_switch_enabled)
  1828. acpi_video_switch_brightness(video_device, event);
  1829. acpi_bus_generate_proc_event(device, event, 0);
  1830. keycode = KEY_DISPLAY_OFF;
  1831. break;
  1832. default:
  1833. keycode = KEY_UNKNOWN;
  1834. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  1835. "Unsupported event [0x%x]\n", event));
  1836. break;
  1837. }
  1838. acpi_notifier_call_chain(device, event, 0);
  1839. input_report_key(input, keycode, 1);
  1840. input_sync(input);
  1841. input_report_key(input, keycode, 0);
  1842. input_sync(input);
  1843. return;
  1844. }
  1845. static int instance;
  1846. static int acpi_video_resume(struct acpi_device *device)
  1847. {
  1848. struct acpi_video_bus *video;
  1849. struct acpi_video_device *video_device;
  1850. int i;
  1851. if (!device || !acpi_driver_data(device))
  1852. return -EINVAL;
  1853. video = acpi_driver_data(device);
  1854. for (i = 0; i < video->attached_count; i++) {
  1855. video_device = video->attached_array[i].bind_info;
  1856. if (video_device && video_device->backlight)
  1857. acpi_video_set_brightness(video_device->backlight);
  1858. }
  1859. return AE_OK;
  1860. }
  1861. static int acpi_video_bus_add(struct acpi_device *device)
  1862. {
  1863. struct acpi_video_bus *video;
  1864. struct input_dev *input;
  1865. int error;
  1866. video = kzalloc(sizeof(struct acpi_video_bus), GFP_KERNEL);
  1867. if (!video)
  1868. return -ENOMEM;
  1869. /* a hack to fix the duplicate name "VID" problem on T61 */
  1870. if (!strcmp(device->pnp.bus_id, "VID")) {
  1871. if (instance)
  1872. device->pnp.bus_id[3] = '0' + instance;
  1873. instance ++;
  1874. }
  1875. /* a hack to fix the duplicate name "VGA" problem on Pa 3553 */
  1876. if (!strcmp(device->pnp.bus_id, "VGA")) {
  1877. if (instance)
  1878. device->pnp.bus_id[3] = '0' + instance;
  1879. instance++;
  1880. }
  1881. video->device = device;
  1882. strcpy(acpi_device_name(device), ACPI_VIDEO_BUS_NAME);
  1883. strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
  1884. device->driver_data = video;
  1885. acpi_video_bus_find_cap(video);
  1886. error = acpi_video_bus_check(video);
  1887. if (error)
  1888. goto err_free_video;
  1889. error = acpi_video_bus_add_fs(device);
  1890. if (error)
  1891. goto err_free_video;
  1892. mutex_init(&video->device_list_lock);
  1893. INIT_LIST_HEAD(&video->video_device_list);
  1894. acpi_video_bus_get_devices(video, device);
  1895. acpi_video_bus_start_devices(video);
  1896. video->input = input = input_allocate_device();
  1897. if (!input) {
  1898. error = -ENOMEM;
  1899. goto err_stop_video;
  1900. }
  1901. snprintf(video->phys, sizeof(video->phys),
  1902. "%s/video/input0", acpi_device_hid(video->device));
  1903. input->name = acpi_device_name(video->device);
  1904. input->phys = video->phys;
  1905. input->id.bustype = BUS_HOST;
  1906. input->id.product = 0x06;
  1907. input->dev.parent = &device->dev;
  1908. input->evbit[0] = BIT(EV_KEY);
  1909. set_bit(KEY_SWITCHVIDEOMODE, input->keybit);
  1910. set_bit(KEY_VIDEO_NEXT, input->keybit);
  1911. set_bit(KEY_VIDEO_PREV, input->keybit);
  1912. set_bit(KEY_BRIGHTNESS_CYCLE, input->keybit);
  1913. set_bit(KEY_BRIGHTNESSUP, input->keybit);
  1914. set_bit(KEY_BRIGHTNESSDOWN, input->keybit);
  1915. set_bit(KEY_BRIGHTNESS_ZERO, input->keybit);
  1916. set_bit(KEY_DISPLAY_OFF, input->keybit);
  1917. set_bit(KEY_UNKNOWN, input->keybit);
  1918. error = input_register_device(input);
  1919. if (error)
  1920. goto err_free_input_dev;
  1921. printk(KERN_INFO PREFIX "%s [%s] (multi-head: %s rom: %s post: %s)\n",
  1922. ACPI_VIDEO_DEVICE_NAME, acpi_device_bid(device),
  1923. video->flags.multihead ? "yes" : "no",
  1924. video->flags.rom ? "yes" : "no",
  1925. video->flags.post ? "yes" : "no");
  1926. return 0;
  1927. err_free_input_dev:
  1928. input_free_device(input);
  1929. err_stop_video:
  1930. acpi_video_bus_stop_devices(video);
  1931. acpi_video_bus_put_devices(video);
  1932. kfree(video->attached_array);
  1933. acpi_video_bus_remove_fs(device);
  1934. err_free_video:
  1935. kfree(video);
  1936. device->driver_data = NULL;
  1937. return error;
  1938. }
  1939. static int acpi_video_bus_remove(struct acpi_device *device, int type)
  1940. {
  1941. struct acpi_video_bus *video = NULL;
  1942. if (!device || !acpi_driver_data(device))
  1943. return -EINVAL;
  1944. video = acpi_driver_data(device);
  1945. acpi_video_bus_stop_devices(video);
  1946. acpi_video_bus_put_devices(video);
  1947. acpi_video_bus_remove_fs(device);
  1948. input_unregister_device(video->input);
  1949. kfree(video->attached_array);
  1950. kfree(video);
  1951. return 0;
  1952. }
  1953. static int __init intel_opregion_present(void)
  1954. {
  1955. #if defined(CONFIG_DRM_I915) || defined(CONFIG_DRM_I915_MODULE)
  1956. struct pci_dev *dev = NULL;
  1957. u32 address;
  1958. for_each_pci_dev(dev) {
  1959. if ((dev->class >> 8) != PCI_CLASS_DISPLAY_VGA)
  1960. continue;
  1961. if (dev->vendor != PCI_VENDOR_ID_INTEL)
  1962. continue;
  1963. pci_read_config_dword(dev, 0xfc, &address);
  1964. if (!address)
  1965. continue;
  1966. return 1;
  1967. }
  1968. #endif
  1969. return 0;
  1970. }
  1971. int acpi_video_register(void)
  1972. {
  1973. int result = 0;
  1974. if (register_count) {
  1975. /*
  1976. * if the function of acpi_video_register is already called,
  1977. * don't register the acpi_vide_bus again and return no error.
  1978. */
  1979. return 0;
  1980. }
  1981. acpi_video_dir = proc_mkdir(ACPI_VIDEO_CLASS, acpi_root_dir);
  1982. if (!acpi_video_dir)
  1983. return -ENODEV;
  1984. result = acpi_bus_register_driver(&acpi_video_bus);
  1985. if (result < 0) {
  1986. remove_proc_entry(ACPI_VIDEO_CLASS, acpi_root_dir);
  1987. return -ENODEV;
  1988. }
  1989. /*
  1990. * When the acpi_video_bus is loaded successfully, increase
  1991. * the counter reference.
  1992. */
  1993. register_count = 1;
  1994. return 0;
  1995. }
  1996. EXPORT_SYMBOL(acpi_video_register);
  1997. void acpi_video_unregister(void)
  1998. {
  1999. if (!register_count) {
  2000. /*
  2001. * If the acpi video bus is already unloaded, don't
  2002. * unload it again and return directly.
  2003. */
  2004. return;
  2005. }
  2006. acpi_bus_unregister_driver(&acpi_video_bus);
  2007. remove_proc_entry(ACPI_VIDEO_CLASS, acpi_root_dir);
  2008. register_count = 0;
  2009. return;
  2010. }
  2011. EXPORT_SYMBOL(acpi_video_unregister);
  2012. /*
  2013. * This is kind of nasty. Hardware using Intel chipsets may require
  2014. * the video opregion code to be run first in order to initialise
  2015. * state before any ACPI video calls are made. To handle this we defer
  2016. * registration of the video class until the opregion code has run.
  2017. */
  2018. static int __init acpi_video_init(void)
  2019. {
  2020. dmi_check_system(video_dmi_table);
  2021. if (intel_opregion_present())
  2022. return 0;
  2023. return acpi_video_register();
  2024. }
  2025. static void __exit acpi_video_exit(void)
  2026. {
  2027. acpi_video_unregister();
  2028. return;
  2029. }
  2030. module_init(acpi_video_init);
  2031. module_exit(acpi_video_exit);