video.c 60 KB

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