video.c 59 KB

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