video.c 54 KB

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