video.c 58 KB

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