video.c 63 KB

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