video.c 60 KB

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