video.c 59 KB

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