video.c 63 KB

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