video.c 61 KB

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