video.c 55 KB

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