video.c 54 KB

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