video.c 66 KB

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