video.c 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495
  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);
  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))
  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))
  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)
  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. /* BQC returned an invalid level. Stop using it. */
  553. ACPI_WARNING((AE_INFO, "%s returned an invalid level",
  554. buf));
  555. device->cap._BQC = device->cap._BCQ = 0;
  556. } else {
  557. /* Fixme:
  558. * should we return an error or ignore this failure?
  559. * dev->brightness->curr is a cached value which stores
  560. * the correct current backlight level in most cases.
  561. * ACPI video backlight still works w/ buggy _BQC.
  562. * http://bugzilla.kernel.org/show_bug.cgi?id=12233
  563. */
  564. ACPI_WARNING((AE_INFO, "Evaluating %s failed", buf));
  565. device->cap._BQC = device->cap._BCQ = 0;
  566. }
  567. }
  568. *level = device->brightness->curr;
  569. return 0;
  570. }
  571. static int
  572. acpi_video_device_EDID(struct acpi_video_device *device,
  573. union acpi_object **edid, ssize_t length)
  574. {
  575. int status;
  576. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  577. union acpi_object *obj;
  578. union acpi_object arg0 = { ACPI_TYPE_INTEGER };
  579. struct acpi_object_list args = { 1, &arg0 };
  580. *edid = NULL;
  581. if (!device)
  582. return -ENODEV;
  583. if (length == 128)
  584. arg0.integer.value = 1;
  585. else if (length == 256)
  586. arg0.integer.value = 2;
  587. else
  588. return -EINVAL;
  589. status = acpi_evaluate_object(device->dev->handle, "_DDC", &args, &buffer);
  590. if (ACPI_FAILURE(status))
  591. return -ENODEV;
  592. obj = buffer.pointer;
  593. if (obj && obj->type == ACPI_TYPE_BUFFER)
  594. *edid = obj;
  595. else {
  596. printk(KERN_ERR PREFIX "Invalid _DDC data\n");
  597. status = -EFAULT;
  598. kfree(obj);
  599. }
  600. return status;
  601. }
  602. /* bus */
  603. static int
  604. acpi_video_bus_set_POST(struct acpi_video_bus *video, unsigned long option)
  605. {
  606. int status;
  607. unsigned long long tmp;
  608. union acpi_object arg0 = { ACPI_TYPE_INTEGER };
  609. struct acpi_object_list args = { 1, &arg0 };
  610. arg0.integer.value = option;
  611. status = acpi_evaluate_integer(video->device->handle, "_SPD", &args, &tmp);
  612. if (ACPI_SUCCESS(status))
  613. status = tmp ? (-EINVAL) : (AE_OK);
  614. return status;
  615. }
  616. static int
  617. acpi_video_bus_get_POST(struct acpi_video_bus *video, unsigned long long *id)
  618. {
  619. int status;
  620. status = acpi_evaluate_integer(video->device->handle, "_GPD", NULL, id);
  621. return status;
  622. }
  623. static int
  624. acpi_video_bus_POST_options(struct acpi_video_bus *video,
  625. unsigned long long *options)
  626. {
  627. int status;
  628. status = acpi_evaluate_integer(video->device->handle, "_VPO", NULL, options);
  629. *options &= 3;
  630. return status;
  631. }
  632. /*
  633. * Arg:
  634. * video : video bus device pointer
  635. * bios_flag :
  636. * 0. The system BIOS should NOT automatically switch(toggle)
  637. * the active display output.
  638. * 1. The system BIOS should automatically switch (toggle) the
  639. * active display output. No switch event.
  640. * 2. The _DGS value should be locked.
  641. * 3. The system BIOS should not automatically switch (toggle) the
  642. * active display output, but instead generate the display switch
  643. * event notify code.
  644. * lcd_flag :
  645. * 0. The system BIOS should automatically control the brightness level
  646. * of the LCD when the power changes from AC to DC
  647. * 1. The system BIOS should NOT automatically control the brightness
  648. * level of the LCD when the power changes from AC to DC.
  649. * Return Value:
  650. * -1 wrong arg.
  651. */
  652. static int
  653. acpi_video_bus_DOS(struct acpi_video_bus *video, int bios_flag, int lcd_flag)
  654. {
  655. u64 status = 0;
  656. union acpi_object arg0 = { ACPI_TYPE_INTEGER };
  657. struct acpi_object_list args = { 1, &arg0 };
  658. if (bios_flag < 0 || bios_flag > 3 || lcd_flag < 0 || lcd_flag > 1) {
  659. status = -1;
  660. goto Failed;
  661. }
  662. arg0.integer.value = (lcd_flag << 2) | bios_flag;
  663. video->dos_setting = arg0.integer.value;
  664. acpi_evaluate_object(video->device->handle, "_DOS", &args, NULL);
  665. Failed:
  666. return status;
  667. }
  668. /*
  669. * Simple comparison function used to sort backlight levels.
  670. */
  671. static int
  672. acpi_video_cmp_level(const void *a, const void *b)
  673. {
  674. return *(int *)a - *(int *)b;
  675. }
  676. /*
  677. * Arg:
  678. * device : video output device (LCD, CRT, ..)
  679. *
  680. * Return Value:
  681. * Maximum brightness level
  682. *
  683. * Allocate and initialize device->brightness.
  684. */
  685. static int
  686. acpi_video_init_brightness(struct acpi_video_device *device)
  687. {
  688. union acpi_object *obj = NULL;
  689. int i, max_level = 0, count = 0, level_ac_battery = 0;
  690. unsigned long long level, level_old;
  691. union acpi_object *o;
  692. struct acpi_video_device_brightness *br = NULL;
  693. int result = -EINVAL;
  694. if (!ACPI_SUCCESS(acpi_video_device_lcd_query_levels(device, &obj))) {
  695. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Could not query available "
  696. "LCD brightness level\n"));
  697. goto out;
  698. }
  699. if (obj->package.count < 2)
  700. goto out;
  701. br = kzalloc(sizeof(*br), GFP_KERNEL);
  702. if (!br) {
  703. printk(KERN_ERR "can't allocate memory\n");
  704. result = -ENOMEM;
  705. goto out;
  706. }
  707. br->levels = kmalloc((obj->package.count + 2) * sizeof *(br->levels),
  708. GFP_KERNEL);
  709. if (!br->levels) {
  710. result = -ENOMEM;
  711. goto out_free;
  712. }
  713. for (i = 0; i < obj->package.count; i++) {
  714. o = (union acpi_object *)&obj->package.elements[i];
  715. if (o->type != ACPI_TYPE_INTEGER) {
  716. printk(KERN_ERR PREFIX "Invalid data\n");
  717. continue;
  718. }
  719. br->levels[count] = (u32) o->integer.value;
  720. if (br->levels[count] > max_level)
  721. max_level = br->levels[count];
  722. count++;
  723. }
  724. /*
  725. * some buggy BIOS don't export the levels
  726. * when machine is on AC/Battery in _BCL package.
  727. * In this case, the first two elements in _BCL packages
  728. * are also supported brightness levels that OS should take care of.
  729. */
  730. for (i = 2; i < count; i++) {
  731. if (br->levels[i] == br->levels[0])
  732. level_ac_battery++;
  733. if (br->levels[i] == br->levels[1])
  734. level_ac_battery++;
  735. }
  736. if (level_ac_battery < 2) {
  737. level_ac_battery = 2 - level_ac_battery;
  738. br->flags._BCL_no_ac_battery_levels = 1;
  739. for (i = (count - 1 + level_ac_battery); i >= 2; i--)
  740. br->levels[i] = br->levels[i - level_ac_battery];
  741. count += level_ac_battery;
  742. } else if (level_ac_battery > 2)
  743. ACPI_ERROR((AE_INFO, "Too many duplicates in _BCL package\n"));
  744. /* Check if the _BCL package is in a reversed order */
  745. if (max_level == br->levels[2]) {
  746. br->flags._BCL_reversed = 1;
  747. sort(&br->levels[2], count - 2, sizeof(br->levels[2]),
  748. acpi_video_cmp_level, NULL);
  749. } else if (max_level != br->levels[count - 1])
  750. ACPI_ERROR((AE_INFO,
  751. "Found unordered _BCL package\n"));
  752. br->count = count;
  753. device->brightness = br;
  754. /* Check the input/output of _BQC/_BCL/_BCM */
  755. if ((max_level < 100) && (max_level <= (count - 2)))
  756. br->flags._BCL_use_index = 1;
  757. /*
  758. * _BCM is always consistent with _BCL,
  759. * at least for all the laptops we have ever seen.
  760. */
  761. br->flags._BCM_use_index = br->flags._BCL_use_index;
  762. /* _BQC uses INDEX while _BCL uses VALUE in some laptops */
  763. br->curr = level = max_level;
  764. if (!device->cap._BQC)
  765. goto set_level;
  766. result = acpi_video_device_lcd_get_level_current(device, &level_old);
  767. if (result)
  768. goto out_free_levels;
  769. /*
  770. * Set the level to maximum and check if _BQC uses indexed value
  771. */
  772. result = acpi_video_device_lcd_set_level(device, max_level);
  773. if (result)
  774. goto out_free_levels;
  775. result = acpi_video_device_lcd_get_level_current(device, &level);
  776. if (result)
  777. goto out_free_levels;
  778. br->flags._BQC_use_index = (level == max_level ? 0 : 1);
  779. if (!br->flags._BQC_use_index) {
  780. /*
  781. * Set the backlight to the initial state.
  782. * On some buggy laptops, _BQC returns an uninitialized value
  783. * when invoked for the first time, i.e. level_old is invalid.
  784. * set the backlight to max_level in this case
  785. */
  786. for (i = 2; i < br->count; i++)
  787. if (level_old == br->levels[i])
  788. level = level_old;
  789. goto set_level;
  790. }
  791. if (br->flags._BCL_reversed)
  792. level_old = (br->count - 1) - level_old;
  793. level = br->levels[level_old];
  794. set_level:
  795. result = acpi_video_device_lcd_set_level(device, level);
  796. if (result)
  797. goto out_free_levels;
  798. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  799. "found %d brightness levels\n", count - 2));
  800. kfree(obj);
  801. return result;
  802. out_free_levels:
  803. kfree(br->levels);
  804. out_free:
  805. kfree(br);
  806. out:
  807. device->brightness = NULL;
  808. kfree(obj);
  809. return result;
  810. }
  811. /*
  812. * Arg:
  813. * device : video output device (LCD, CRT, ..)
  814. *
  815. * Return Value:
  816. * None
  817. *
  818. * Find out all required AML methods defined under the output
  819. * device.
  820. */
  821. static void acpi_video_device_find_cap(struct acpi_video_device *device)
  822. {
  823. acpi_handle h_dummy1;
  824. if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_ADR", &h_dummy1))) {
  825. device->cap._ADR = 1;
  826. }
  827. if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_BCL", &h_dummy1))) {
  828. device->cap._BCL = 1;
  829. }
  830. if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_BCM", &h_dummy1))) {
  831. device->cap._BCM = 1;
  832. }
  833. if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle,"_BQC",&h_dummy1)))
  834. device->cap._BQC = 1;
  835. else if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_BCQ",
  836. &h_dummy1))) {
  837. printk(KERN_WARNING FW_BUG "_BCQ is used instead of _BQC\n");
  838. device->cap._BCQ = 1;
  839. }
  840. if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DDC", &h_dummy1))) {
  841. device->cap._DDC = 1;
  842. }
  843. if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DCS", &h_dummy1))) {
  844. device->cap._DCS = 1;
  845. }
  846. if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DGS", &h_dummy1))) {
  847. device->cap._DGS = 1;
  848. }
  849. if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DSS", &h_dummy1))) {
  850. device->cap._DSS = 1;
  851. }
  852. if (acpi_video_backlight_support()) {
  853. int result;
  854. static int count = 0;
  855. char *name;
  856. result = acpi_video_init_brightness(device);
  857. if (result)
  858. return;
  859. name = kzalloc(MAX_NAME_LEN, GFP_KERNEL);
  860. if (!name)
  861. return;
  862. sprintf(name, "acpi_video%d", count++);
  863. device->backlight = backlight_device_register(name,
  864. NULL, device, &acpi_backlight_ops);
  865. kfree(name);
  866. if (IS_ERR(device->backlight))
  867. return;
  868. device->backlight->props.max_brightness = device->brightness->count-3;
  869. result = sysfs_create_link(&device->backlight->dev.kobj,
  870. &device->dev->dev.kobj, "device");
  871. if (result)
  872. printk(KERN_ERR PREFIX "Create sysfs link\n");
  873. device->cooling_dev = thermal_cooling_device_register("LCD",
  874. device->dev, &video_cooling_ops);
  875. if (IS_ERR(device->cooling_dev)) {
  876. /*
  877. * Set cooling_dev to NULL so we don't crash trying to
  878. * free it.
  879. * Also, why the hell we are returning early and
  880. * not attempt to register video output if cooling
  881. * device registration failed?
  882. * -- dtor
  883. */
  884. device->cooling_dev = NULL;
  885. return;
  886. }
  887. dev_info(&device->dev->dev, "registered as cooling_device%d\n",
  888. device->cooling_dev->id);
  889. result = sysfs_create_link(&device->dev->dev.kobj,
  890. &device->cooling_dev->device.kobj,
  891. "thermal_cooling");
  892. if (result)
  893. printk(KERN_ERR PREFIX "Create sysfs link\n");
  894. result = sysfs_create_link(&device->cooling_dev->device.kobj,
  895. &device->dev->dev.kobj, "device");
  896. if (result)
  897. printk(KERN_ERR PREFIX "Create sysfs link\n");
  898. }
  899. if (acpi_video_display_switch_support()) {
  900. if (device->cap._DCS && device->cap._DSS) {
  901. static int count;
  902. char *name;
  903. name = kzalloc(MAX_NAME_LEN, GFP_KERNEL);
  904. if (!name)
  905. return;
  906. sprintf(name, "acpi_video%d", count++);
  907. device->output_dev = video_output_register(name,
  908. NULL, device, &acpi_output_properties);
  909. kfree(name);
  910. }
  911. }
  912. }
  913. /*
  914. * Arg:
  915. * device : video output device (VGA)
  916. *
  917. * Return Value:
  918. * None
  919. *
  920. * Find out all required AML methods defined under the video bus device.
  921. */
  922. static void acpi_video_bus_find_cap(struct acpi_video_bus *video)
  923. {
  924. acpi_handle h_dummy1;
  925. if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_DOS", &h_dummy1))) {
  926. video->cap._DOS = 1;
  927. }
  928. if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_DOD", &h_dummy1))) {
  929. video->cap._DOD = 1;
  930. }
  931. if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_ROM", &h_dummy1))) {
  932. video->cap._ROM = 1;
  933. }
  934. if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_GPD", &h_dummy1))) {
  935. video->cap._GPD = 1;
  936. }
  937. if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_SPD", &h_dummy1))) {
  938. video->cap._SPD = 1;
  939. }
  940. if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_VPO", &h_dummy1))) {
  941. video->cap._VPO = 1;
  942. }
  943. }
  944. /*
  945. * Check whether the video bus device has required AML method to
  946. * support the desired features
  947. */
  948. static int acpi_video_bus_check(struct acpi_video_bus *video)
  949. {
  950. acpi_status status = -ENOENT;
  951. struct pci_dev *dev;
  952. if (!video)
  953. return -EINVAL;
  954. dev = acpi_get_pci_dev(video->device->handle);
  955. if (!dev)
  956. return -ENODEV;
  957. pci_dev_put(dev);
  958. /* Since there is no HID, CID and so on for VGA driver, we have
  959. * to check well known required nodes.
  960. */
  961. /* Does this device support video switching? */
  962. if (video->cap._DOS || video->cap._DOD) {
  963. if (!video->cap._DOS) {
  964. printk(KERN_WARNING FW_BUG
  965. "ACPI(%s) defines _DOD but not _DOS\n",
  966. acpi_device_bid(video->device));
  967. }
  968. video->flags.multihead = 1;
  969. status = 0;
  970. }
  971. /* Does this device support retrieving a video ROM? */
  972. if (video->cap._ROM) {
  973. video->flags.rom = 1;
  974. status = 0;
  975. }
  976. /* Does this device support configuring which video device to POST? */
  977. if (video->cap._GPD && video->cap._SPD && video->cap._VPO) {
  978. video->flags.post = 1;
  979. status = 0;
  980. }
  981. return status;
  982. }
  983. /* --------------------------------------------------------------------------
  984. FS Interface (/proc)
  985. -------------------------------------------------------------------------- */
  986. static struct proc_dir_entry *acpi_video_dir;
  987. /* video devices */
  988. static int acpi_video_device_info_seq_show(struct seq_file *seq, void *offset)
  989. {
  990. struct acpi_video_device *dev = seq->private;
  991. if (!dev)
  992. goto end;
  993. seq_printf(seq, "device_id: 0x%04x\n", (u32) dev->device_id);
  994. seq_printf(seq, "type: ");
  995. if (dev->flags.crt)
  996. seq_printf(seq, "CRT\n");
  997. else if (dev->flags.lcd)
  998. seq_printf(seq, "LCD\n");
  999. else if (dev->flags.tvout)
  1000. seq_printf(seq, "TVOUT\n");
  1001. else if (dev->flags.dvi)
  1002. seq_printf(seq, "DVI\n");
  1003. else
  1004. seq_printf(seq, "UNKNOWN\n");
  1005. seq_printf(seq, "known by bios: %s\n", dev->flags.bios ? "yes" : "no");
  1006. end:
  1007. return 0;
  1008. }
  1009. static int
  1010. acpi_video_device_info_open_fs(struct inode *inode, struct file *file)
  1011. {
  1012. return single_open(file, acpi_video_device_info_seq_show,
  1013. PDE(inode)->data);
  1014. }
  1015. static int acpi_video_device_state_seq_show(struct seq_file *seq, void *offset)
  1016. {
  1017. int status;
  1018. struct acpi_video_device *dev = seq->private;
  1019. unsigned long long state;
  1020. if (!dev)
  1021. goto end;
  1022. status = acpi_video_device_get_state(dev, &state);
  1023. seq_printf(seq, "state: ");
  1024. if (ACPI_SUCCESS(status))
  1025. seq_printf(seq, "0x%02llx\n", state);
  1026. else
  1027. seq_printf(seq, "<not supported>\n");
  1028. status = acpi_video_device_query(dev, &state);
  1029. seq_printf(seq, "query: ");
  1030. if (ACPI_SUCCESS(status))
  1031. seq_printf(seq, "0x%02llx\n", state);
  1032. else
  1033. seq_printf(seq, "<not supported>\n");
  1034. end:
  1035. return 0;
  1036. }
  1037. static int
  1038. acpi_video_device_state_open_fs(struct inode *inode, struct file *file)
  1039. {
  1040. return single_open(file, acpi_video_device_state_seq_show,
  1041. PDE(inode)->data);
  1042. }
  1043. static ssize_t
  1044. acpi_video_device_write_state(struct file *file,
  1045. const char __user * buffer,
  1046. size_t count, loff_t * data)
  1047. {
  1048. int status;
  1049. struct seq_file *m = file->private_data;
  1050. struct acpi_video_device *dev = m->private;
  1051. char str[12] = { 0 };
  1052. u32 state = 0;
  1053. if (!dev || count >= sizeof(str))
  1054. return -EINVAL;
  1055. if (copy_from_user(str, buffer, count))
  1056. return -EFAULT;
  1057. str[count] = 0;
  1058. state = simple_strtoul(str, NULL, 0);
  1059. state &= ((1ul << 31) | (1ul << 30) | (1ul << 0));
  1060. status = acpi_video_device_set_state(dev, state);
  1061. if (status)
  1062. return -EFAULT;
  1063. return count;
  1064. }
  1065. static int
  1066. acpi_video_device_brightness_seq_show(struct seq_file *seq, void *offset)
  1067. {
  1068. struct acpi_video_device *dev = seq->private;
  1069. int i;
  1070. if (!dev || !dev->brightness) {
  1071. seq_printf(seq, "<not supported>\n");
  1072. return 0;
  1073. }
  1074. seq_printf(seq, "levels: ");
  1075. for (i = 2; i < dev->brightness->count; i++)
  1076. seq_printf(seq, " %d", dev->brightness->levels[i]);
  1077. seq_printf(seq, "\ncurrent: %d\n", dev->brightness->curr);
  1078. return 0;
  1079. }
  1080. static int
  1081. acpi_video_device_brightness_open_fs(struct inode *inode, struct file *file)
  1082. {
  1083. return single_open(file, acpi_video_device_brightness_seq_show,
  1084. PDE(inode)->data);
  1085. }
  1086. static ssize_t
  1087. acpi_video_device_write_brightness(struct file *file,
  1088. const char __user * buffer,
  1089. size_t count, loff_t * data)
  1090. {
  1091. struct seq_file *m = file->private_data;
  1092. struct acpi_video_device *dev = m->private;
  1093. char str[5] = { 0 };
  1094. unsigned int level = 0;
  1095. int i;
  1096. if (!dev || !dev->brightness || count >= sizeof(str))
  1097. return -EINVAL;
  1098. if (copy_from_user(str, buffer, count))
  1099. return -EFAULT;
  1100. str[count] = 0;
  1101. level = simple_strtoul(str, NULL, 0);
  1102. if (level > 100)
  1103. return -EFAULT;
  1104. /* validate through the list of available levels */
  1105. for (i = 2; i < dev->brightness->count; i++)
  1106. if (level == dev->brightness->levels[i]) {
  1107. if (!acpi_video_device_lcd_set_level(dev, level))
  1108. return count;
  1109. break;
  1110. }
  1111. return -EINVAL;
  1112. }
  1113. static int acpi_video_device_EDID_seq_show(struct seq_file *seq, void *offset)
  1114. {
  1115. struct acpi_video_device *dev = seq->private;
  1116. int status;
  1117. int i;
  1118. union acpi_object *edid = NULL;
  1119. if (!dev)
  1120. goto out;
  1121. status = acpi_video_device_EDID(dev, &edid, 128);
  1122. if (ACPI_FAILURE(status)) {
  1123. status = acpi_video_device_EDID(dev, &edid, 256);
  1124. }
  1125. if (ACPI_FAILURE(status)) {
  1126. goto out;
  1127. }
  1128. if (edid && edid->type == ACPI_TYPE_BUFFER) {
  1129. for (i = 0; i < edid->buffer.length; i++)
  1130. seq_putc(seq, edid->buffer.pointer[i]);
  1131. }
  1132. out:
  1133. if (!edid)
  1134. seq_printf(seq, "<not supported>\n");
  1135. else
  1136. kfree(edid);
  1137. return 0;
  1138. }
  1139. static int
  1140. acpi_video_device_EDID_open_fs(struct inode *inode, struct file *file)
  1141. {
  1142. return single_open(file, acpi_video_device_EDID_seq_show,
  1143. PDE(inode)->data);
  1144. }
  1145. static int acpi_video_device_add_fs(struct acpi_device *device)
  1146. {
  1147. struct proc_dir_entry *entry, *device_dir;
  1148. struct acpi_video_device *vid_dev;
  1149. vid_dev = acpi_driver_data(device);
  1150. if (!vid_dev)
  1151. return -ENODEV;
  1152. device_dir = proc_mkdir(acpi_device_bid(device),
  1153. vid_dev->video->dir);
  1154. if (!device_dir)
  1155. return -ENOMEM;
  1156. /* 'info' [R] */
  1157. entry = proc_create_data("info", S_IRUGO, device_dir,
  1158. &acpi_video_device_info_fops, acpi_driver_data(device));
  1159. if (!entry)
  1160. goto err_remove_dir;
  1161. /* 'state' [R/W] */
  1162. entry = proc_create_data("state", S_IFREG | S_IRUGO | S_IWUSR,
  1163. device_dir,
  1164. &acpi_video_device_state_fops,
  1165. acpi_driver_data(device));
  1166. if (!entry)
  1167. goto err_remove_info;
  1168. /* 'brightness' [R/W] */
  1169. entry = proc_create_data("brightness", S_IFREG | S_IRUGO | S_IWUSR,
  1170. device_dir,
  1171. &acpi_video_device_brightness_fops,
  1172. acpi_driver_data(device));
  1173. if (!entry)
  1174. goto err_remove_state;
  1175. /* 'EDID' [R] */
  1176. entry = proc_create_data("EDID", S_IRUGO, device_dir,
  1177. &acpi_video_device_EDID_fops,
  1178. acpi_driver_data(device));
  1179. if (!entry)
  1180. goto err_remove_brightness;
  1181. acpi_device_dir(device) = device_dir;
  1182. return 0;
  1183. err_remove_brightness:
  1184. remove_proc_entry("brightness", device_dir);
  1185. err_remove_state:
  1186. remove_proc_entry("state", device_dir);
  1187. err_remove_info:
  1188. remove_proc_entry("info", device_dir);
  1189. err_remove_dir:
  1190. remove_proc_entry(acpi_device_bid(device), vid_dev->video->dir);
  1191. return -ENOMEM;
  1192. }
  1193. static int acpi_video_device_remove_fs(struct acpi_device *device)
  1194. {
  1195. struct acpi_video_device *vid_dev;
  1196. struct proc_dir_entry *device_dir;
  1197. vid_dev = acpi_driver_data(device);
  1198. if (!vid_dev || !vid_dev->video || !vid_dev->video->dir)
  1199. return -ENODEV;
  1200. device_dir = acpi_device_dir(device);
  1201. if (device_dir) {
  1202. remove_proc_entry("info", device_dir);
  1203. remove_proc_entry("state", device_dir);
  1204. remove_proc_entry("brightness", device_dir);
  1205. remove_proc_entry("EDID", device_dir);
  1206. remove_proc_entry(acpi_device_bid(device), vid_dev->video->dir);
  1207. acpi_device_dir(device) = NULL;
  1208. }
  1209. return 0;
  1210. }
  1211. /* video bus */
  1212. static int acpi_video_bus_info_seq_show(struct seq_file *seq, void *offset)
  1213. {
  1214. struct acpi_video_bus *video = seq->private;
  1215. if (!video)
  1216. goto end;
  1217. seq_printf(seq, "Switching heads: %s\n",
  1218. video->flags.multihead ? "yes" : "no");
  1219. seq_printf(seq, "Video ROM: %s\n",
  1220. video->flags.rom ? "yes" : "no");
  1221. seq_printf(seq, "Device to be POSTed on boot: %s\n",
  1222. video->flags.post ? "yes" : "no");
  1223. end:
  1224. return 0;
  1225. }
  1226. static int acpi_video_bus_info_open_fs(struct inode *inode, struct file *file)
  1227. {
  1228. return single_open(file, acpi_video_bus_info_seq_show,
  1229. PDE(inode)->data);
  1230. }
  1231. static int acpi_video_bus_ROM_seq_show(struct seq_file *seq, void *offset)
  1232. {
  1233. struct acpi_video_bus *video = seq->private;
  1234. if (!video)
  1235. goto end;
  1236. printk(KERN_INFO PREFIX "Please implement %s\n", __func__);
  1237. seq_printf(seq, "<TODO>\n");
  1238. end:
  1239. return 0;
  1240. }
  1241. static int acpi_video_bus_ROM_open_fs(struct inode *inode, struct file *file)
  1242. {
  1243. return single_open(file, acpi_video_bus_ROM_seq_show, PDE(inode)->data);
  1244. }
  1245. static int acpi_video_bus_POST_info_seq_show(struct seq_file *seq, void *offset)
  1246. {
  1247. struct acpi_video_bus *video = seq->private;
  1248. unsigned long long options;
  1249. int status;
  1250. if (!video)
  1251. goto end;
  1252. status = acpi_video_bus_POST_options(video, &options);
  1253. if (ACPI_SUCCESS(status)) {
  1254. if (!(options & 1)) {
  1255. printk(KERN_WARNING PREFIX
  1256. "The motherboard VGA device is not listed as a possible POST device.\n");
  1257. printk(KERN_WARNING PREFIX
  1258. "This indicates a BIOS bug. Please contact the manufacturer.\n");
  1259. }
  1260. printk(KERN_WARNING "%llx\n", options);
  1261. seq_printf(seq, "can POST: <integrated video>");
  1262. if (options & 2)
  1263. seq_printf(seq, " <PCI video>");
  1264. if (options & 4)
  1265. seq_printf(seq, " <AGP video>");
  1266. seq_putc(seq, '\n');
  1267. } else
  1268. seq_printf(seq, "<not supported>\n");
  1269. end:
  1270. return 0;
  1271. }
  1272. static int
  1273. acpi_video_bus_POST_info_open_fs(struct inode *inode, struct file *file)
  1274. {
  1275. return single_open(file, acpi_video_bus_POST_info_seq_show,
  1276. PDE(inode)->data);
  1277. }
  1278. static int acpi_video_bus_POST_seq_show(struct seq_file *seq, void *offset)
  1279. {
  1280. struct acpi_video_bus *video = seq->private;
  1281. int status;
  1282. unsigned long long id;
  1283. if (!video)
  1284. goto end;
  1285. status = acpi_video_bus_get_POST(video, &id);
  1286. if (!ACPI_SUCCESS(status)) {
  1287. seq_printf(seq, "<not supported>\n");
  1288. goto end;
  1289. }
  1290. seq_printf(seq, "device POSTed is <%s>\n", device_decode[id & 3]);
  1291. end:
  1292. return 0;
  1293. }
  1294. static int acpi_video_bus_DOS_seq_show(struct seq_file *seq, void *offset)
  1295. {
  1296. struct acpi_video_bus *video = seq->private;
  1297. seq_printf(seq, "DOS setting: <%d>\n", video->dos_setting);
  1298. return 0;
  1299. }
  1300. static int acpi_video_bus_POST_open_fs(struct inode *inode, struct file *file)
  1301. {
  1302. return single_open(file, acpi_video_bus_POST_seq_show,
  1303. PDE(inode)->data);
  1304. }
  1305. static int acpi_video_bus_DOS_open_fs(struct inode *inode, struct file *file)
  1306. {
  1307. return single_open(file, acpi_video_bus_DOS_seq_show, PDE(inode)->data);
  1308. }
  1309. static ssize_t
  1310. acpi_video_bus_write_POST(struct file *file,
  1311. const char __user * buffer,
  1312. size_t count, loff_t * data)
  1313. {
  1314. int status;
  1315. struct seq_file *m = file->private_data;
  1316. struct acpi_video_bus *video = m->private;
  1317. char str[12] = { 0 };
  1318. unsigned long long opt, options;
  1319. if (!video || count >= sizeof(str))
  1320. return -EINVAL;
  1321. status = acpi_video_bus_POST_options(video, &options);
  1322. if (!ACPI_SUCCESS(status))
  1323. return -EINVAL;
  1324. if (copy_from_user(str, buffer, count))
  1325. return -EFAULT;
  1326. str[count] = 0;
  1327. opt = strtoul(str, NULL, 0);
  1328. if (opt > 3)
  1329. return -EFAULT;
  1330. /* just in case an OEM 'forgot' the motherboard... */
  1331. options |= 1;
  1332. if (options & (1ul << opt)) {
  1333. status = acpi_video_bus_set_POST(video, opt);
  1334. if (!ACPI_SUCCESS(status))
  1335. return -EFAULT;
  1336. }
  1337. return count;
  1338. }
  1339. static ssize_t
  1340. acpi_video_bus_write_DOS(struct file *file,
  1341. const char __user * buffer,
  1342. size_t count, loff_t * data)
  1343. {
  1344. int status;
  1345. struct seq_file *m = file->private_data;
  1346. struct acpi_video_bus *video = m->private;
  1347. char str[12] = { 0 };
  1348. unsigned long opt;
  1349. if (!video || count >= sizeof(str))
  1350. return -EINVAL;
  1351. if (copy_from_user(str, buffer, count))
  1352. return -EFAULT;
  1353. str[count] = 0;
  1354. opt = strtoul(str, NULL, 0);
  1355. if (opt > 7)
  1356. return -EFAULT;
  1357. status = acpi_video_bus_DOS(video, opt & 0x3, (opt & 0x4) >> 2);
  1358. if (!ACPI_SUCCESS(status))
  1359. return -EFAULT;
  1360. return count;
  1361. }
  1362. static int acpi_video_bus_add_fs(struct acpi_device *device)
  1363. {
  1364. struct acpi_video_bus *video = acpi_driver_data(device);
  1365. struct proc_dir_entry *device_dir;
  1366. struct proc_dir_entry *entry;
  1367. device_dir = proc_mkdir(acpi_device_bid(device), acpi_video_dir);
  1368. if (!device_dir)
  1369. return -ENOMEM;
  1370. /* 'info' [R] */
  1371. entry = proc_create_data("info", S_IRUGO, device_dir,
  1372. &acpi_video_bus_info_fops,
  1373. acpi_driver_data(device));
  1374. if (!entry)
  1375. goto err_remove_dir;
  1376. /* 'ROM' [R] */
  1377. entry = proc_create_data("ROM", S_IRUGO, device_dir,
  1378. &acpi_video_bus_ROM_fops,
  1379. acpi_driver_data(device));
  1380. if (!entry)
  1381. goto err_remove_info;
  1382. /* 'POST_info' [R] */
  1383. entry = proc_create_data("POST_info", S_IRUGO, device_dir,
  1384. &acpi_video_bus_POST_info_fops,
  1385. acpi_driver_data(device));
  1386. if (!entry)
  1387. goto err_remove_rom;
  1388. /* 'POST' [R/W] */
  1389. entry = proc_create_data("POST", S_IFREG | S_IRUGO | S_IWUSR,
  1390. device_dir,
  1391. &acpi_video_bus_POST_fops,
  1392. acpi_driver_data(device));
  1393. if (!entry)
  1394. goto err_remove_post_info;
  1395. /* 'DOS' [R/W] */
  1396. entry = proc_create_data("DOS", S_IFREG | S_IRUGO | S_IWUSR,
  1397. device_dir,
  1398. &acpi_video_bus_DOS_fops,
  1399. acpi_driver_data(device));
  1400. if (!entry)
  1401. goto err_remove_post;
  1402. video->dir = acpi_device_dir(device) = device_dir;
  1403. return 0;
  1404. err_remove_post:
  1405. remove_proc_entry("POST", device_dir);
  1406. err_remove_post_info:
  1407. remove_proc_entry("POST_info", device_dir);
  1408. err_remove_rom:
  1409. remove_proc_entry("ROM", device_dir);
  1410. err_remove_info:
  1411. remove_proc_entry("info", device_dir);
  1412. err_remove_dir:
  1413. remove_proc_entry(acpi_device_bid(device), acpi_video_dir);
  1414. return -ENOMEM;
  1415. }
  1416. static int acpi_video_bus_remove_fs(struct acpi_device *device)
  1417. {
  1418. struct proc_dir_entry *device_dir = acpi_device_dir(device);
  1419. if (device_dir) {
  1420. remove_proc_entry("info", device_dir);
  1421. remove_proc_entry("ROM", device_dir);
  1422. remove_proc_entry("POST_info", device_dir);
  1423. remove_proc_entry("POST", device_dir);
  1424. remove_proc_entry("DOS", device_dir);
  1425. remove_proc_entry(acpi_device_bid(device), acpi_video_dir);
  1426. acpi_device_dir(device) = NULL;
  1427. }
  1428. return 0;
  1429. }
  1430. /* --------------------------------------------------------------------------
  1431. Driver Interface
  1432. -------------------------------------------------------------------------- */
  1433. /* device interface */
  1434. static struct acpi_video_device_attrib*
  1435. acpi_video_get_device_attr(struct acpi_video_bus *video, unsigned long device_id)
  1436. {
  1437. struct acpi_video_enumerated_device *ids;
  1438. int i;
  1439. for (i = 0; i < video->attached_count; i++) {
  1440. ids = &video->attached_array[i];
  1441. if ((ids->value.int_val & 0xffff) == device_id)
  1442. return &ids->value.attrib;
  1443. }
  1444. return NULL;
  1445. }
  1446. static int
  1447. acpi_video_bus_get_one_device(struct acpi_device *device,
  1448. struct acpi_video_bus *video)
  1449. {
  1450. unsigned long long device_id;
  1451. int status;
  1452. struct acpi_video_device *data;
  1453. struct acpi_video_device_attrib* attribute;
  1454. if (!device || !video)
  1455. return -EINVAL;
  1456. status =
  1457. acpi_evaluate_integer(device->handle, "_ADR", NULL, &device_id);
  1458. if (ACPI_SUCCESS(status)) {
  1459. data = kzalloc(sizeof(struct acpi_video_device), GFP_KERNEL);
  1460. if (!data)
  1461. return -ENOMEM;
  1462. strcpy(acpi_device_name(device), ACPI_VIDEO_DEVICE_NAME);
  1463. strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
  1464. device->driver_data = data;
  1465. data->device_id = device_id;
  1466. data->video = video;
  1467. data->dev = device;
  1468. attribute = acpi_video_get_device_attr(video, device_id);
  1469. if((attribute != NULL) && attribute->device_id_scheme) {
  1470. switch (attribute->display_type) {
  1471. case ACPI_VIDEO_DISPLAY_CRT:
  1472. data->flags.crt = 1;
  1473. break;
  1474. case ACPI_VIDEO_DISPLAY_TV:
  1475. data->flags.tvout = 1;
  1476. break;
  1477. case ACPI_VIDEO_DISPLAY_DVI:
  1478. data->flags.dvi = 1;
  1479. break;
  1480. case ACPI_VIDEO_DISPLAY_LCD:
  1481. data->flags.lcd = 1;
  1482. break;
  1483. default:
  1484. data->flags.unknown = 1;
  1485. break;
  1486. }
  1487. if(attribute->bios_can_detect)
  1488. data->flags.bios = 1;
  1489. } else
  1490. data->flags.unknown = 1;
  1491. acpi_video_device_bind(video, data);
  1492. acpi_video_device_find_cap(data);
  1493. status = acpi_install_notify_handler(device->handle,
  1494. ACPI_DEVICE_NOTIFY,
  1495. acpi_video_device_notify,
  1496. data);
  1497. if (ACPI_FAILURE(status)) {
  1498. printk(KERN_ERR PREFIX
  1499. "Error installing notify handler\n");
  1500. if(data->brightness)
  1501. kfree(data->brightness->levels);
  1502. kfree(data->brightness);
  1503. kfree(data);
  1504. return -ENODEV;
  1505. }
  1506. mutex_lock(&video->device_list_lock);
  1507. list_add_tail(&data->entry, &video->video_device_list);
  1508. mutex_unlock(&video->device_list_lock);
  1509. acpi_video_device_add_fs(device);
  1510. return 0;
  1511. }
  1512. return -ENOENT;
  1513. }
  1514. /*
  1515. * Arg:
  1516. * video : video bus device
  1517. *
  1518. * Return:
  1519. * none
  1520. *
  1521. * Enumerate the video device list of the video bus,
  1522. * bind the ids with the corresponding video devices
  1523. * under the video bus.
  1524. */
  1525. static void acpi_video_device_rebind(struct acpi_video_bus *video)
  1526. {
  1527. struct acpi_video_device *dev;
  1528. mutex_lock(&video->device_list_lock);
  1529. list_for_each_entry(dev, &video->video_device_list, entry)
  1530. acpi_video_device_bind(video, dev);
  1531. mutex_unlock(&video->device_list_lock);
  1532. }
  1533. /*
  1534. * Arg:
  1535. * video : video bus device
  1536. * device : video output device under the video
  1537. * bus
  1538. *
  1539. * Return:
  1540. * none
  1541. *
  1542. * Bind the ids with the corresponding video devices
  1543. * under the video bus.
  1544. */
  1545. static void
  1546. acpi_video_device_bind(struct acpi_video_bus *video,
  1547. struct acpi_video_device *device)
  1548. {
  1549. struct acpi_video_enumerated_device *ids;
  1550. int i;
  1551. for (i = 0; i < video->attached_count; i++) {
  1552. ids = &video->attached_array[i];
  1553. if (device->device_id == (ids->value.int_val & 0xffff)) {
  1554. ids->bind_info = device;
  1555. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "device_bind %d\n", i));
  1556. }
  1557. }
  1558. }
  1559. /*
  1560. * Arg:
  1561. * video : video bus device
  1562. *
  1563. * Return:
  1564. * < 0 : error
  1565. *
  1566. * Call _DOD to enumerate all devices attached to display adapter
  1567. *
  1568. */
  1569. static int acpi_video_device_enumerate(struct acpi_video_bus *video)
  1570. {
  1571. int status;
  1572. int count;
  1573. int i;
  1574. struct acpi_video_enumerated_device *active_list;
  1575. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  1576. union acpi_object *dod = NULL;
  1577. union acpi_object *obj;
  1578. status = acpi_evaluate_object(video->device->handle, "_DOD", NULL, &buffer);
  1579. if (!ACPI_SUCCESS(status)) {
  1580. ACPI_EXCEPTION((AE_INFO, status, "Evaluating _DOD"));
  1581. return status;
  1582. }
  1583. dod = buffer.pointer;
  1584. if (!dod || (dod->type != ACPI_TYPE_PACKAGE)) {
  1585. ACPI_EXCEPTION((AE_INFO, status, "Invalid _DOD data"));
  1586. status = -EFAULT;
  1587. goto out;
  1588. }
  1589. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d video heads in _DOD\n",
  1590. dod->package.count));
  1591. active_list = kcalloc(1 + dod->package.count,
  1592. sizeof(struct acpi_video_enumerated_device),
  1593. GFP_KERNEL);
  1594. if (!active_list) {
  1595. status = -ENOMEM;
  1596. goto out;
  1597. }
  1598. count = 0;
  1599. for (i = 0; i < dod->package.count; i++) {
  1600. obj = &dod->package.elements[i];
  1601. if (obj->type != ACPI_TYPE_INTEGER) {
  1602. printk(KERN_ERR PREFIX
  1603. "Invalid _DOD data in element %d\n", i);
  1604. continue;
  1605. }
  1606. active_list[count].value.int_val = obj->integer.value;
  1607. active_list[count].bind_info = NULL;
  1608. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "dod element[%d] = %d\n", i,
  1609. (int)obj->integer.value));
  1610. count++;
  1611. }
  1612. kfree(video->attached_array);
  1613. video->attached_array = active_list;
  1614. video->attached_count = count;
  1615. out:
  1616. kfree(buffer.pointer);
  1617. return status;
  1618. }
  1619. static int
  1620. acpi_video_get_next_level(struct acpi_video_device *device,
  1621. u32 level_current, u32 event)
  1622. {
  1623. int min, max, min_above, max_below, i, l, delta = 255;
  1624. max = max_below = 0;
  1625. min = min_above = 255;
  1626. /* Find closest level to level_current */
  1627. for (i = 2; i < device->brightness->count; i++) {
  1628. l = device->brightness->levels[i];
  1629. if (abs(l - level_current) < abs(delta)) {
  1630. delta = l - level_current;
  1631. if (!delta)
  1632. break;
  1633. }
  1634. }
  1635. /* Ajust level_current to closest available level */
  1636. level_current += delta;
  1637. for (i = 2; i < device->brightness->count; i++) {
  1638. l = device->brightness->levels[i];
  1639. if (l < min)
  1640. min = l;
  1641. if (l > max)
  1642. max = l;
  1643. if (l < min_above && l > level_current)
  1644. min_above = l;
  1645. if (l > max_below && l < level_current)
  1646. max_below = l;
  1647. }
  1648. switch (event) {
  1649. case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS:
  1650. return (level_current < max) ? min_above : min;
  1651. case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS:
  1652. return (level_current < max) ? min_above : max;
  1653. case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS:
  1654. return (level_current > min) ? max_below : min;
  1655. case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS:
  1656. case ACPI_VIDEO_NOTIFY_DISPLAY_OFF:
  1657. return 0;
  1658. default:
  1659. return level_current;
  1660. }
  1661. }
  1662. static int
  1663. acpi_video_switch_brightness(struct acpi_video_device *device, int event)
  1664. {
  1665. unsigned long long level_current, level_next;
  1666. int result = -EINVAL;
  1667. /* no warning message if acpi_backlight=vendor is used */
  1668. if (!acpi_video_backlight_support())
  1669. return 0;
  1670. if (!device->brightness)
  1671. goto out;
  1672. result = acpi_video_device_lcd_get_level_current(device,
  1673. &level_current);
  1674. if (result)
  1675. goto out;
  1676. level_next = acpi_video_get_next_level(device, level_current, event);
  1677. result = acpi_video_device_lcd_set_level(device, level_next);
  1678. if (!result)
  1679. backlight_force_update(device->backlight,
  1680. BACKLIGHT_UPDATE_HOTKEY);
  1681. out:
  1682. if (result)
  1683. printk(KERN_ERR PREFIX "Failed to switch the brightness\n");
  1684. return result;
  1685. }
  1686. static int
  1687. acpi_video_bus_get_devices(struct acpi_video_bus *video,
  1688. struct acpi_device *device)
  1689. {
  1690. int status = 0;
  1691. struct acpi_device *dev;
  1692. acpi_video_device_enumerate(video);
  1693. list_for_each_entry(dev, &device->children, node) {
  1694. status = acpi_video_bus_get_one_device(dev, video);
  1695. if (ACPI_FAILURE(status)) {
  1696. printk(KERN_WARNING PREFIX
  1697. "Cant attach device");
  1698. continue;
  1699. }
  1700. }
  1701. return status;
  1702. }
  1703. static int acpi_video_bus_put_one_device(struct acpi_video_device *device)
  1704. {
  1705. acpi_status status;
  1706. struct acpi_video_bus *video;
  1707. if (!device || !device->video)
  1708. return -ENOENT;
  1709. video = device->video;
  1710. acpi_video_device_remove_fs(device->dev);
  1711. status = acpi_remove_notify_handler(device->dev->handle,
  1712. ACPI_DEVICE_NOTIFY,
  1713. acpi_video_device_notify);
  1714. if (device->backlight) {
  1715. sysfs_remove_link(&device->backlight->dev.kobj, "device");
  1716. backlight_device_unregister(device->backlight);
  1717. device->backlight = NULL;
  1718. }
  1719. if (device->cooling_dev) {
  1720. sysfs_remove_link(&device->dev->dev.kobj,
  1721. "thermal_cooling");
  1722. sysfs_remove_link(&device->cooling_dev->device.kobj,
  1723. "device");
  1724. thermal_cooling_device_unregister(device->cooling_dev);
  1725. device->cooling_dev = NULL;
  1726. }
  1727. video_output_unregister(device->output_dev);
  1728. return 0;
  1729. }
  1730. static int acpi_video_bus_put_devices(struct acpi_video_bus *video)
  1731. {
  1732. int status;
  1733. struct acpi_video_device *dev, *next;
  1734. mutex_lock(&video->device_list_lock);
  1735. list_for_each_entry_safe(dev, next, &video->video_device_list, entry) {
  1736. status = acpi_video_bus_put_one_device(dev);
  1737. if (ACPI_FAILURE(status))
  1738. printk(KERN_WARNING PREFIX
  1739. "hhuuhhuu bug in acpi video driver.\n");
  1740. if (dev->brightness) {
  1741. kfree(dev->brightness->levels);
  1742. kfree(dev->brightness);
  1743. }
  1744. list_del(&dev->entry);
  1745. kfree(dev);
  1746. }
  1747. mutex_unlock(&video->device_list_lock);
  1748. return 0;
  1749. }
  1750. /* acpi_video interface */
  1751. static int acpi_video_bus_start_devices(struct acpi_video_bus *video)
  1752. {
  1753. return acpi_video_bus_DOS(video, 0, 0);
  1754. }
  1755. static int acpi_video_bus_stop_devices(struct acpi_video_bus *video)
  1756. {
  1757. return acpi_video_bus_DOS(video, 0, 1);
  1758. }
  1759. static void acpi_video_bus_notify(struct acpi_device *device, u32 event)
  1760. {
  1761. struct acpi_video_bus *video = acpi_driver_data(device);
  1762. struct input_dev *input;
  1763. int keycode;
  1764. if (!video)
  1765. return;
  1766. input = video->input;
  1767. switch (event) {
  1768. case ACPI_VIDEO_NOTIFY_SWITCH: /* User requested a switch,
  1769. * most likely via hotkey. */
  1770. acpi_bus_generate_proc_event(device, event, 0);
  1771. keycode = KEY_SWITCHVIDEOMODE;
  1772. break;
  1773. case ACPI_VIDEO_NOTIFY_PROBE: /* User plugged in or removed a video
  1774. * connector. */
  1775. acpi_video_device_enumerate(video);
  1776. acpi_video_device_rebind(video);
  1777. acpi_bus_generate_proc_event(device, event, 0);
  1778. keycode = KEY_SWITCHVIDEOMODE;
  1779. break;
  1780. case ACPI_VIDEO_NOTIFY_CYCLE: /* Cycle Display output hotkey pressed. */
  1781. acpi_bus_generate_proc_event(device, event, 0);
  1782. keycode = KEY_SWITCHVIDEOMODE;
  1783. break;
  1784. case ACPI_VIDEO_NOTIFY_NEXT_OUTPUT: /* Next Display output hotkey pressed. */
  1785. acpi_bus_generate_proc_event(device, event, 0);
  1786. keycode = KEY_VIDEO_NEXT;
  1787. break;
  1788. case ACPI_VIDEO_NOTIFY_PREV_OUTPUT: /* previous Display output hotkey pressed. */
  1789. acpi_bus_generate_proc_event(device, event, 0);
  1790. keycode = KEY_VIDEO_PREV;
  1791. break;
  1792. default:
  1793. keycode = KEY_UNKNOWN;
  1794. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  1795. "Unsupported event [0x%x]\n", event));
  1796. break;
  1797. }
  1798. acpi_notifier_call_chain(device, event, 0);
  1799. input_report_key(input, keycode, 1);
  1800. input_sync(input);
  1801. input_report_key(input, keycode, 0);
  1802. input_sync(input);
  1803. return;
  1804. }
  1805. static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data)
  1806. {
  1807. struct acpi_video_device *video_device = data;
  1808. struct acpi_device *device = NULL;
  1809. struct acpi_video_bus *bus;
  1810. struct input_dev *input;
  1811. int keycode;
  1812. if (!video_device)
  1813. return;
  1814. device = video_device->dev;
  1815. bus = video_device->video;
  1816. input = bus->input;
  1817. switch (event) {
  1818. case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS: /* Cycle brightness */
  1819. if (brightness_switch_enabled)
  1820. acpi_video_switch_brightness(video_device, event);
  1821. acpi_bus_generate_proc_event(device, event, 0);
  1822. keycode = KEY_BRIGHTNESS_CYCLE;
  1823. break;
  1824. case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS: /* Increase brightness */
  1825. if (brightness_switch_enabled)
  1826. acpi_video_switch_brightness(video_device, event);
  1827. acpi_bus_generate_proc_event(device, event, 0);
  1828. keycode = KEY_BRIGHTNESSUP;
  1829. break;
  1830. case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS: /* Decrease brightness */
  1831. if (brightness_switch_enabled)
  1832. acpi_video_switch_brightness(video_device, event);
  1833. acpi_bus_generate_proc_event(device, event, 0);
  1834. keycode = KEY_BRIGHTNESSDOWN;
  1835. break;
  1836. case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS: /* zero brightnesss */
  1837. if (brightness_switch_enabled)
  1838. acpi_video_switch_brightness(video_device, event);
  1839. acpi_bus_generate_proc_event(device, event, 0);
  1840. keycode = KEY_BRIGHTNESS_ZERO;
  1841. break;
  1842. case ACPI_VIDEO_NOTIFY_DISPLAY_OFF: /* display device off */
  1843. if (brightness_switch_enabled)
  1844. acpi_video_switch_brightness(video_device, event);
  1845. acpi_bus_generate_proc_event(device, event, 0);
  1846. keycode = KEY_DISPLAY_OFF;
  1847. break;
  1848. default:
  1849. keycode = KEY_UNKNOWN;
  1850. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  1851. "Unsupported event [0x%x]\n", event));
  1852. break;
  1853. }
  1854. acpi_notifier_call_chain(device, event, 0);
  1855. input_report_key(input, keycode, 1);
  1856. input_sync(input);
  1857. input_report_key(input, keycode, 0);
  1858. input_sync(input);
  1859. return;
  1860. }
  1861. static int instance;
  1862. static int acpi_video_resume(struct acpi_device *device)
  1863. {
  1864. struct acpi_video_bus *video;
  1865. struct acpi_video_device *video_device;
  1866. int i;
  1867. if (!device || !acpi_driver_data(device))
  1868. return -EINVAL;
  1869. video = acpi_driver_data(device);
  1870. for (i = 0; i < video->attached_count; i++) {
  1871. video_device = video->attached_array[i].bind_info;
  1872. if (video_device && video_device->backlight)
  1873. acpi_video_set_brightness(video_device->backlight);
  1874. }
  1875. return AE_OK;
  1876. }
  1877. static acpi_status
  1878. acpi_video_bus_match(acpi_handle handle, u32 level, void *context,
  1879. void **return_value)
  1880. {
  1881. struct acpi_device *device = context;
  1882. struct acpi_device *sibling;
  1883. int result;
  1884. if (handle == device->handle)
  1885. return AE_CTRL_TERMINATE;
  1886. result = acpi_bus_get_device(handle, &sibling);
  1887. if (result)
  1888. return AE_OK;
  1889. if (!strcmp(acpi_device_name(sibling), ACPI_VIDEO_BUS_NAME))
  1890. return AE_ALREADY_EXISTS;
  1891. return AE_OK;
  1892. }
  1893. static int acpi_video_bus_add(struct acpi_device *device)
  1894. {
  1895. struct acpi_video_bus *video;
  1896. struct input_dev *input;
  1897. int error;
  1898. acpi_status status;
  1899. status = acpi_walk_namespace(ACPI_TYPE_DEVICE,
  1900. device->parent->handle, 1,
  1901. acpi_video_bus_match, NULL,
  1902. device, NULL);
  1903. if (status == AE_ALREADY_EXISTS) {
  1904. printk(KERN_WARNING FW_BUG
  1905. "Duplicate ACPI video bus devices for the"
  1906. " same VGA controller, please try module "
  1907. "parameter \"video.allow_duplicates=1\""
  1908. "if the current driver doesn't work.\n");
  1909. if (!allow_duplicates)
  1910. return -ENODEV;
  1911. }
  1912. video = kzalloc(sizeof(struct acpi_video_bus), GFP_KERNEL);
  1913. if (!video)
  1914. return -ENOMEM;
  1915. /* a hack to fix the duplicate name "VID" problem on T61 */
  1916. if (!strcmp(device->pnp.bus_id, "VID")) {
  1917. if (instance)
  1918. device->pnp.bus_id[3] = '0' + instance;
  1919. instance ++;
  1920. }
  1921. /* a hack to fix the duplicate name "VGA" problem on Pa 3553 */
  1922. if (!strcmp(device->pnp.bus_id, "VGA")) {
  1923. if (instance)
  1924. device->pnp.bus_id[3] = '0' + instance;
  1925. instance++;
  1926. }
  1927. video->device = device;
  1928. strcpy(acpi_device_name(device), ACPI_VIDEO_BUS_NAME);
  1929. strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
  1930. device->driver_data = video;
  1931. acpi_video_bus_find_cap(video);
  1932. error = acpi_video_bus_check(video);
  1933. if (error)
  1934. goto err_free_video;
  1935. error = acpi_video_bus_add_fs(device);
  1936. if (error)
  1937. goto err_free_video;
  1938. mutex_init(&video->device_list_lock);
  1939. INIT_LIST_HEAD(&video->video_device_list);
  1940. acpi_video_bus_get_devices(video, device);
  1941. acpi_video_bus_start_devices(video);
  1942. video->input = input = input_allocate_device();
  1943. if (!input) {
  1944. error = -ENOMEM;
  1945. goto err_stop_video;
  1946. }
  1947. snprintf(video->phys, sizeof(video->phys),
  1948. "%s/video/input0", acpi_device_hid(video->device));
  1949. input->name = acpi_device_name(video->device);
  1950. input->phys = video->phys;
  1951. input->id.bustype = BUS_HOST;
  1952. input->id.product = 0x06;
  1953. input->dev.parent = &device->dev;
  1954. input->evbit[0] = BIT(EV_KEY);
  1955. set_bit(KEY_SWITCHVIDEOMODE, input->keybit);
  1956. set_bit(KEY_VIDEO_NEXT, input->keybit);
  1957. set_bit(KEY_VIDEO_PREV, input->keybit);
  1958. set_bit(KEY_BRIGHTNESS_CYCLE, input->keybit);
  1959. set_bit(KEY_BRIGHTNESSUP, input->keybit);
  1960. set_bit(KEY_BRIGHTNESSDOWN, input->keybit);
  1961. set_bit(KEY_BRIGHTNESS_ZERO, input->keybit);
  1962. set_bit(KEY_DISPLAY_OFF, input->keybit);
  1963. set_bit(KEY_UNKNOWN, input->keybit);
  1964. error = input_register_device(input);
  1965. if (error)
  1966. goto err_free_input_dev;
  1967. printk(KERN_INFO PREFIX "%s [%s] (multi-head: %s rom: %s post: %s)\n",
  1968. ACPI_VIDEO_DEVICE_NAME, acpi_device_bid(device),
  1969. video->flags.multihead ? "yes" : "no",
  1970. video->flags.rom ? "yes" : "no",
  1971. video->flags.post ? "yes" : "no");
  1972. return 0;
  1973. err_free_input_dev:
  1974. input_free_device(input);
  1975. err_stop_video:
  1976. acpi_video_bus_stop_devices(video);
  1977. acpi_video_bus_put_devices(video);
  1978. kfree(video->attached_array);
  1979. acpi_video_bus_remove_fs(device);
  1980. err_free_video:
  1981. kfree(video);
  1982. device->driver_data = NULL;
  1983. return error;
  1984. }
  1985. static int acpi_video_bus_remove(struct acpi_device *device, int type)
  1986. {
  1987. struct acpi_video_bus *video = NULL;
  1988. if (!device || !acpi_driver_data(device))
  1989. return -EINVAL;
  1990. video = acpi_driver_data(device);
  1991. acpi_video_bus_stop_devices(video);
  1992. acpi_video_bus_put_devices(video);
  1993. acpi_video_bus_remove_fs(device);
  1994. input_unregister_device(video->input);
  1995. kfree(video->attached_array);
  1996. kfree(video);
  1997. return 0;
  1998. }
  1999. static int __init intel_opregion_present(void)
  2000. {
  2001. #if defined(CONFIG_DRM_I915) || defined(CONFIG_DRM_I915_MODULE)
  2002. struct pci_dev *dev = NULL;
  2003. u32 address;
  2004. for_each_pci_dev(dev) {
  2005. if ((dev->class >> 8) != PCI_CLASS_DISPLAY_VGA)
  2006. continue;
  2007. if (dev->vendor != PCI_VENDOR_ID_INTEL)
  2008. continue;
  2009. pci_read_config_dword(dev, 0xfc, &address);
  2010. if (!address)
  2011. continue;
  2012. return 1;
  2013. }
  2014. #endif
  2015. return 0;
  2016. }
  2017. int acpi_video_register(void)
  2018. {
  2019. int result = 0;
  2020. if (register_count) {
  2021. /*
  2022. * if the function of acpi_video_register is already called,
  2023. * don't register the acpi_vide_bus again and return no error.
  2024. */
  2025. return 0;
  2026. }
  2027. acpi_video_dir = proc_mkdir(ACPI_VIDEO_CLASS, acpi_root_dir);
  2028. if (!acpi_video_dir)
  2029. return -ENODEV;
  2030. result = acpi_bus_register_driver(&acpi_video_bus);
  2031. if (result < 0) {
  2032. remove_proc_entry(ACPI_VIDEO_CLASS, acpi_root_dir);
  2033. return -ENODEV;
  2034. }
  2035. /*
  2036. * When the acpi_video_bus is loaded successfully, increase
  2037. * the counter reference.
  2038. */
  2039. register_count = 1;
  2040. return 0;
  2041. }
  2042. EXPORT_SYMBOL(acpi_video_register);
  2043. void acpi_video_unregister(void)
  2044. {
  2045. if (!register_count) {
  2046. /*
  2047. * If the acpi video bus is already unloaded, don't
  2048. * unload it again and return directly.
  2049. */
  2050. return;
  2051. }
  2052. acpi_bus_unregister_driver(&acpi_video_bus);
  2053. remove_proc_entry(ACPI_VIDEO_CLASS, acpi_root_dir);
  2054. register_count = 0;
  2055. return;
  2056. }
  2057. EXPORT_SYMBOL(acpi_video_unregister);
  2058. /*
  2059. * This is kind of nasty. Hardware using Intel chipsets may require
  2060. * the video opregion code to be run first in order to initialise
  2061. * state before any ACPI video calls are made. To handle this we defer
  2062. * registration of the video class until the opregion code has run.
  2063. */
  2064. static int __init acpi_video_init(void)
  2065. {
  2066. dmi_check_system(video_dmi_table);
  2067. if (intel_opregion_present())
  2068. return 0;
  2069. return acpi_video_register();
  2070. }
  2071. static void __exit acpi_video_exit(void)
  2072. {
  2073. acpi_video_unregister();
  2074. return;
  2075. }
  2076. module_init(acpi_video_init);
  2077. module_exit(acpi_video_exit);