video.c 60 KB

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