video.c 59 KB

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