video.c 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307
  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. static int
  465. acpi_video_device_lcd_get_level_current(struct acpi_video_device *device,
  466. unsigned long long *level)
  467. {
  468. acpi_status status = AE_OK;
  469. if (device->cap._BQC || device->cap._BCQ) {
  470. char *buf = device->cap._BQC ? "_BQC" : "_BCQ";
  471. status = acpi_evaluate_integer(device->dev->handle, buf,
  472. NULL, level);
  473. if (ACPI_SUCCESS(status)) {
  474. if (device->brightness->flags._BQC_use_index) {
  475. if (device->brightness->flags._BCL_reversed)
  476. *level = device->brightness->count
  477. - 3 - (*level);
  478. *level = device->brightness->levels[*level + 2];
  479. }
  480. device->brightness->curr = *level;
  481. return 0;
  482. } else {
  483. /* Fixme:
  484. * should we return an error or ignore this failure?
  485. * dev->brightness->curr is a cached value which stores
  486. * the correct current backlight level in most cases.
  487. * ACPI video backlight still works w/ buggy _BQC.
  488. * http://bugzilla.kernel.org/show_bug.cgi?id=12233
  489. */
  490. ACPI_WARNING((AE_INFO, "Evaluating %s failed", buf));
  491. device->cap._BQC = device->cap._BCQ = 0;
  492. }
  493. }
  494. *level = device->brightness->curr;
  495. return 0;
  496. }
  497. static int
  498. acpi_video_device_EDID(struct acpi_video_device *device,
  499. union acpi_object **edid, ssize_t length)
  500. {
  501. int status;
  502. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  503. union acpi_object *obj;
  504. union acpi_object arg0 = { ACPI_TYPE_INTEGER };
  505. struct acpi_object_list args = { 1, &arg0 };
  506. *edid = NULL;
  507. if (!device)
  508. return -ENODEV;
  509. if (length == 128)
  510. arg0.integer.value = 1;
  511. else if (length == 256)
  512. arg0.integer.value = 2;
  513. else
  514. return -EINVAL;
  515. status = acpi_evaluate_object(device->dev->handle, "_DDC", &args, &buffer);
  516. if (ACPI_FAILURE(status))
  517. return -ENODEV;
  518. obj = buffer.pointer;
  519. if (obj && obj->type == ACPI_TYPE_BUFFER)
  520. *edid = obj;
  521. else {
  522. printk(KERN_ERR PREFIX "Invalid _DDC data\n");
  523. status = -EFAULT;
  524. kfree(obj);
  525. }
  526. return status;
  527. }
  528. /* bus */
  529. static int
  530. acpi_video_bus_set_POST(struct acpi_video_bus *video, unsigned long option)
  531. {
  532. int status;
  533. unsigned long long tmp;
  534. union acpi_object arg0 = { ACPI_TYPE_INTEGER };
  535. struct acpi_object_list args = { 1, &arg0 };
  536. arg0.integer.value = option;
  537. status = acpi_evaluate_integer(video->device->handle, "_SPD", &args, &tmp);
  538. if (ACPI_SUCCESS(status))
  539. status = tmp ? (-EINVAL) : (AE_OK);
  540. return status;
  541. }
  542. static int
  543. acpi_video_bus_get_POST(struct acpi_video_bus *video, unsigned long long *id)
  544. {
  545. int status;
  546. status = acpi_evaluate_integer(video->device->handle, "_GPD", NULL, id);
  547. return status;
  548. }
  549. static int
  550. acpi_video_bus_POST_options(struct acpi_video_bus *video,
  551. unsigned long long *options)
  552. {
  553. int status;
  554. status = acpi_evaluate_integer(video->device->handle, "_VPO", NULL, options);
  555. *options &= 3;
  556. return status;
  557. }
  558. /*
  559. * Arg:
  560. * video : video bus device pointer
  561. * bios_flag :
  562. * 0. The system BIOS should NOT automatically switch(toggle)
  563. * the active display output.
  564. * 1. The system BIOS should automatically switch (toggle) the
  565. * active display output. No switch event.
  566. * 2. The _DGS value should be locked.
  567. * 3. The system BIOS should not automatically switch (toggle) the
  568. * active display output, but instead generate the display switch
  569. * event notify code.
  570. * lcd_flag :
  571. * 0. The system BIOS should automatically control the brightness level
  572. * of the LCD when the power changes from AC to DC
  573. * 1. The system BIOS should NOT automatically control the brightness
  574. * level of the LCD when the power changes from AC to DC.
  575. * Return Value:
  576. * -1 wrong arg.
  577. */
  578. static int
  579. acpi_video_bus_DOS(struct acpi_video_bus *video, int bios_flag, int lcd_flag)
  580. {
  581. acpi_integer status = 0;
  582. union acpi_object arg0 = { ACPI_TYPE_INTEGER };
  583. struct acpi_object_list args = { 1, &arg0 };
  584. if (bios_flag < 0 || bios_flag > 3 || lcd_flag < 0 || lcd_flag > 1) {
  585. status = -1;
  586. goto Failed;
  587. }
  588. arg0.integer.value = (lcd_flag << 2) | bios_flag;
  589. video->dos_setting = arg0.integer.value;
  590. acpi_evaluate_object(video->device->handle, "_DOS", &args, NULL);
  591. Failed:
  592. return status;
  593. }
  594. /*
  595. * Simple comparison function used to sort backlight levels.
  596. */
  597. static int
  598. acpi_video_cmp_level(const void *a, const void *b)
  599. {
  600. return *(int *)a - *(int *)b;
  601. }
  602. /*
  603. * Arg:
  604. * device : video output device (LCD, CRT, ..)
  605. *
  606. * Return Value:
  607. * Maximum brightness level
  608. *
  609. * Allocate and initialize device->brightness.
  610. */
  611. static int
  612. acpi_video_init_brightness(struct acpi_video_device *device)
  613. {
  614. union acpi_object *obj = NULL;
  615. int i, max_level = 0, count = 0, level_ac_battery = 0;
  616. unsigned long long level, level_old;
  617. union acpi_object *o;
  618. struct acpi_video_device_brightness *br = NULL;
  619. int result = -EINVAL;
  620. if (!ACPI_SUCCESS(acpi_video_device_lcd_query_levels(device, &obj))) {
  621. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Could not query available "
  622. "LCD brightness level\n"));
  623. goto out;
  624. }
  625. if (obj->package.count < 2)
  626. goto out;
  627. br = kzalloc(sizeof(*br), GFP_KERNEL);
  628. if (!br) {
  629. printk(KERN_ERR "can't allocate memory\n");
  630. result = -ENOMEM;
  631. goto out;
  632. }
  633. br->levels = kmalloc((obj->package.count + 2) * sizeof *(br->levels),
  634. GFP_KERNEL);
  635. if (!br->levels) {
  636. result = -ENOMEM;
  637. goto out_free;
  638. }
  639. for (i = 0; i < obj->package.count; i++) {
  640. o = (union acpi_object *)&obj->package.elements[i];
  641. if (o->type != ACPI_TYPE_INTEGER) {
  642. printk(KERN_ERR PREFIX "Invalid data\n");
  643. continue;
  644. }
  645. br->levels[count] = (u32) o->integer.value;
  646. if (br->levels[count] > max_level)
  647. max_level = br->levels[count];
  648. count++;
  649. }
  650. /*
  651. * some buggy BIOS don't export the levels
  652. * when machine is on AC/Battery in _BCL package.
  653. * In this case, the first two elements in _BCL packages
  654. * are also supported brightness levels that OS should take care of.
  655. */
  656. for (i = 2; i < count; i++)
  657. if (br->levels[i] == br->levels[0] ||
  658. br->levels[i] == br->levels[1])
  659. level_ac_battery++;
  660. if (level_ac_battery < 2) {
  661. level_ac_battery = 2 - level_ac_battery;
  662. br->flags._BCL_no_ac_battery_levels = 1;
  663. for (i = (count - 1 + level_ac_battery); i >= 2; i--)
  664. br->levels[i] = br->levels[i - level_ac_battery];
  665. count += level_ac_battery;
  666. } else if (level_ac_battery > 2)
  667. ACPI_ERROR((AE_INFO, "Too many duplicates in _BCL package\n"));
  668. /* Check if the _BCL package is in a reversed order */
  669. if (max_level == br->levels[2]) {
  670. br->flags._BCL_reversed = 1;
  671. sort(&br->levels[2], count - 2, sizeof(br->levels[2]),
  672. acpi_video_cmp_level, NULL);
  673. } else if (max_level != br->levels[count - 1])
  674. ACPI_ERROR((AE_INFO,
  675. "Found unordered _BCL package\n"));
  676. br->count = count;
  677. device->brightness = br;
  678. /* Check the input/output of _BQC/_BCL/_BCM */
  679. if ((max_level < 100) && (max_level <= (count - 2)))
  680. br->flags._BCL_use_index = 1;
  681. /*
  682. * _BCM is always consistent with _BCL,
  683. * at least for all the laptops we have ever seen.
  684. */
  685. br->flags._BCM_use_index = br->flags._BCL_use_index;
  686. /* _BQC uses INDEX while _BCL uses VALUE in some laptops */
  687. br->curr = max_level;
  688. result = acpi_video_device_lcd_get_level_current(device, &level_old);
  689. if (result)
  690. goto out_free_levels;
  691. result = acpi_video_device_lcd_set_level(device, br->curr);
  692. if (result)
  693. goto out_free_levels;
  694. result = acpi_video_device_lcd_get_level_current(device, &level);
  695. if (result)
  696. goto out_free_levels;
  697. if ((level != level_old) && !br->flags._BCM_use_index) {
  698. /* Note:
  699. * This piece of code does not work correctly if the current
  700. * brightness levels is 0.
  701. * But I guess boxes that boot with such a dark screen are rare
  702. * and no more code is needed to cover this specifial case.
  703. */
  704. if (level_ac_battery != 2) {
  705. /*
  706. * For now, we don't support the _BCL like this:
  707. * 16, 15, 0, 1, 2, 3, ..., 14, 15, 16
  708. * because we may mess up the index returned by _BQC.
  709. * Plus: we have not got a box like this.
  710. */
  711. ACPI_ERROR((AE_INFO, "_BCL not supported\n"));
  712. }
  713. br->flags._BQC_use_index = 1;
  714. }
  715. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  716. "found %d brightness levels\n", count - 2));
  717. kfree(obj);
  718. return result;
  719. out_free_levels:
  720. kfree(br->levels);
  721. out_free:
  722. kfree(br);
  723. out:
  724. device->brightness = NULL;
  725. kfree(obj);
  726. return result;
  727. }
  728. /*
  729. * Arg:
  730. * device : video output device (LCD, CRT, ..)
  731. *
  732. * Return Value:
  733. * None
  734. *
  735. * Find out all required AML methods defined under the output
  736. * device.
  737. */
  738. static void acpi_video_device_find_cap(struct acpi_video_device *device)
  739. {
  740. acpi_handle h_dummy1;
  741. memset(&device->cap, 0, sizeof(device->cap));
  742. if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_ADR", &h_dummy1))) {
  743. device->cap._ADR = 1;
  744. }
  745. if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_BCL", &h_dummy1))) {
  746. device->cap._BCL = 1;
  747. }
  748. if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_BCM", &h_dummy1))) {
  749. device->cap._BCM = 1;
  750. }
  751. if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle,"_BQC",&h_dummy1)))
  752. device->cap._BQC = 1;
  753. else if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_BCQ",
  754. &h_dummy1))) {
  755. printk(KERN_WARNING FW_BUG "_BCQ is used instead of _BQC\n");
  756. device->cap._BCQ = 1;
  757. }
  758. if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DDC", &h_dummy1))) {
  759. device->cap._DDC = 1;
  760. }
  761. if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DCS", &h_dummy1))) {
  762. device->cap._DCS = 1;
  763. }
  764. if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DGS", &h_dummy1))) {
  765. device->cap._DGS = 1;
  766. }
  767. if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DSS", &h_dummy1))) {
  768. device->cap._DSS = 1;
  769. }
  770. if (acpi_video_backlight_support()) {
  771. int result;
  772. static int count = 0;
  773. char *name;
  774. result = acpi_video_init_brightness(device);
  775. if (result)
  776. return;
  777. name = kzalloc(MAX_NAME_LEN, GFP_KERNEL);
  778. if (!name)
  779. return;
  780. sprintf(name, "acpi_video%d", count++);
  781. device->backlight = backlight_device_register(name,
  782. NULL, device, &acpi_backlight_ops);
  783. device->backlight->props.max_brightness = device->brightness->count-3;
  784. kfree(name);
  785. device->cdev = thermal_cooling_device_register("LCD",
  786. device->dev, &video_cooling_ops);
  787. if (IS_ERR(device->cdev))
  788. return;
  789. dev_info(&device->dev->dev, "registered as cooling_device%d\n",
  790. device->cdev->id);
  791. result = sysfs_create_link(&device->dev->dev.kobj,
  792. &device->cdev->device.kobj,
  793. "thermal_cooling");
  794. if (result)
  795. printk(KERN_ERR PREFIX "Create sysfs link\n");
  796. result = sysfs_create_link(&device->cdev->device.kobj,
  797. &device->dev->dev.kobj, "device");
  798. if (result)
  799. printk(KERN_ERR PREFIX "Create sysfs link\n");
  800. }
  801. if (acpi_video_display_switch_support()) {
  802. if (device->cap._DCS && device->cap._DSS) {
  803. static int count;
  804. char *name;
  805. name = kzalloc(MAX_NAME_LEN, GFP_KERNEL);
  806. if (!name)
  807. return;
  808. sprintf(name, "acpi_video%d", count++);
  809. device->output_dev = video_output_register(name,
  810. NULL, device, &acpi_output_properties);
  811. kfree(name);
  812. }
  813. }
  814. }
  815. /*
  816. * Arg:
  817. * device : video output device (VGA)
  818. *
  819. * Return Value:
  820. * None
  821. *
  822. * Find out all required AML methods defined under the video bus device.
  823. */
  824. static void acpi_video_bus_find_cap(struct acpi_video_bus *video)
  825. {
  826. acpi_handle h_dummy1;
  827. memset(&video->cap, 0, sizeof(video->cap));
  828. if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_DOS", &h_dummy1))) {
  829. video->cap._DOS = 1;
  830. }
  831. if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_DOD", &h_dummy1))) {
  832. video->cap._DOD = 1;
  833. }
  834. if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_ROM", &h_dummy1))) {
  835. video->cap._ROM = 1;
  836. }
  837. if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_GPD", &h_dummy1))) {
  838. video->cap._GPD = 1;
  839. }
  840. if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_SPD", &h_dummy1))) {
  841. video->cap._SPD = 1;
  842. }
  843. if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_VPO", &h_dummy1))) {
  844. video->cap._VPO = 1;
  845. }
  846. }
  847. /*
  848. * Check whether the video bus device has required AML method to
  849. * support the desired features
  850. */
  851. static int acpi_video_bus_check(struct acpi_video_bus *video)
  852. {
  853. acpi_status status = -ENOENT;
  854. struct device *dev;
  855. if (!video)
  856. return -EINVAL;
  857. dev = acpi_get_physical_pci_device(video->device->handle);
  858. if (!dev)
  859. return -ENODEV;
  860. put_device(dev);
  861. /* Since there is no HID, CID and so on for VGA driver, we have
  862. * to check well known required nodes.
  863. */
  864. /* Does this device support video switching? */
  865. if (video->cap._DOS) {
  866. video->flags.multihead = 1;
  867. status = 0;
  868. }
  869. /* Does this device support retrieving a video ROM? */
  870. if (video->cap._ROM) {
  871. video->flags.rom = 1;
  872. status = 0;
  873. }
  874. /* Does this device support configuring which video device to POST? */
  875. if (video->cap._GPD && video->cap._SPD && video->cap._VPO) {
  876. video->flags.post = 1;
  877. status = 0;
  878. }
  879. return status;
  880. }
  881. /* --------------------------------------------------------------------------
  882. FS Interface (/proc)
  883. -------------------------------------------------------------------------- */
  884. static struct proc_dir_entry *acpi_video_dir;
  885. /* video devices */
  886. static int acpi_video_device_info_seq_show(struct seq_file *seq, void *offset)
  887. {
  888. struct acpi_video_device *dev = seq->private;
  889. if (!dev)
  890. goto end;
  891. seq_printf(seq, "device_id: 0x%04x\n", (u32) dev->device_id);
  892. seq_printf(seq, "type: ");
  893. if (dev->flags.crt)
  894. seq_printf(seq, "CRT\n");
  895. else if (dev->flags.lcd)
  896. seq_printf(seq, "LCD\n");
  897. else if (dev->flags.tvout)
  898. seq_printf(seq, "TVOUT\n");
  899. else if (dev->flags.dvi)
  900. seq_printf(seq, "DVI\n");
  901. else
  902. seq_printf(seq, "UNKNOWN\n");
  903. seq_printf(seq, "known by bios: %s\n", dev->flags.bios ? "yes" : "no");
  904. end:
  905. return 0;
  906. }
  907. static int
  908. acpi_video_device_info_open_fs(struct inode *inode, struct file *file)
  909. {
  910. return single_open(file, acpi_video_device_info_seq_show,
  911. PDE(inode)->data);
  912. }
  913. static int acpi_video_device_state_seq_show(struct seq_file *seq, void *offset)
  914. {
  915. int status;
  916. struct acpi_video_device *dev = seq->private;
  917. unsigned long long state;
  918. if (!dev)
  919. goto end;
  920. status = acpi_video_device_get_state(dev, &state);
  921. seq_printf(seq, "state: ");
  922. if (ACPI_SUCCESS(status))
  923. seq_printf(seq, "0x%02llx\n", state);
  924. else
  925. seq_printf(seq, "<not supported>\n");
  926. status = acpi_video_device_query(dev, &state);
  927. seq_printf(seq, "query: ");
  928. if (ACPI_SUCCESS(status))
  929. seq_printf(seq, "0x%02llx\n", state);
  930. else
  931. seq_printf(seq, "<not supported>\n");
  932. end:
  933. return 0;
  934. }
  935. static int
  936. acpi_video_device_state_open_fs(struct inode *inode, struct file *file)
  937. {
  938. return single_open(file, acpi_video_device_state_seq_show,
  939. PDE(inode)->data);
  940. }
  941. static ssize_t
  942. acpi_video_device_write_state(struct file *file,
  943. const char __user * buffer,
  944. size_t count, loff_t * data)
  945. {
  946. int status;
  947. struct seq_file *m = file->private_data;
  948. struct acpi_video_device *dev = m->private;
  949. char str[12] = { 0 };
  950. u32 state = 0;
  951. if (!dev || count + 1 > sizeof str)
  952. return -EINVAL;
  953. if (copy_from_user(str, buffer, count))
  954. return -EFAULT;
  955. str[count] = 0;
  956. state = simple_strtoul(str, NULL, 0);
  957. state &= ((1ul << 31) | (1ul << 30) | (1ul << 0));
  958. status = acpi_video_device_set_state(dev, state);
  959. if (status)
  960. return -EFAULT;
  961. return count;
  962. }
  963. static int
  964. acpi_video_device_brightness_seq_show(struct seq_file *seq, void *offset)
  965. {
  966. struct acpi_video_device *dev = seq->private;
  967. int i;
  968. if (!dev || !dev->brightness) {
  969. seq_printf(seq, "<not supported>\n");
  970. return 0;
  971. }
  972. seq_printf(seq, "levels: ");
  973. for (i = 2; i < dev->brightness->count; i++)
  974. seq_printf(seq, " %d", dev->brightness->levels[i]);
  975. seq_printf(seq, "\ncurrent: %d\n", dev->brightness->curr);
  976. return 0;
  977. }
  978. static int
  979. acpi_video_device_brightness_open_fs(struct inode *inode, struct file *file)
  980. {
  981. return single_open(file, acpi_video_device_brightness_seq_show,
  982. PDE(inode)->data);
  983. }
  984. static ssize_t
  985. acpi_video_device_write_brightness(struct file *file,
  986. const char __user * buffer,
  987. size_t count, loff_t * data)
  988. {
  989. struct seq_file *m = file->private_data;
  990. struct acpi_video_device *dev = m->private;
  991. char str[5] = { 0 };
  992. unsigned int level = 0;
  993. int i;
  994. if (!dev || !dev->brightness || count + 1 > sizeof str)
  995. return -EINVAL;
  996. if (copy_from_user(str, buffer, count))
  997. return -EFAULT;
  998. str[count] = 0;
  999. level = simple_strtoul(str, NULL, 0);
  1000. if (level > 100)
  1001. return -EFAULT;
  1002. /* validate through the list of available levels */
  1003. for (i = 2; i < dev->brightness->count; i++)
  1004. if (level == dev->brightness->levels[i]) {
  1005. if (!acpi_video_device_lcd_set_level(dev, level))
  1006. return count;
  1007. break;
  1008. }
  1009. return -EINVAL;
  1010. }
  1011. static int acpi_video_device_EDID_seq_show(struct seq_file *seq, void *offset)
  1012. {
  1013. struct acpi_video_device *dev = seq->private;
  1014. int status;
  1015. int i;
  1016. union acpi_object *edid = NULL;
  1017. if (!dev)
  1018. goto out;
  1019. status = acpi_video_device_EDID(dev, &edid, 128);
  1020. if (ACPI_FAILURE(status)) {
  1021. status = acpi_video_device_EDID(dev, &edid, 256);
  1022. }
  1023. if (ACPI_FAILURE(status)) {
  1024. goto out;
  1025. }
  1026. if (edid && edid->type == ACPI_TYPE_BUFFER) {
  1027. for (i = 0; i < edid->buffer.length; i++)
  1028. seq_putc(seq, edid->buffer.pointer[i]);
  1029. }
  1030. out:
  1031. if (!edid)
  1032. seq_printf(seq, "<not supported>\n");
  1033. else
  1034. kfree(edid);
  1035. return 0;
  1036. }
  1037. static int
  1038. acpi_video_device_EDID_open_fs(struct inode *inode, struct file *file)
  1039. {
  1040. return single_open(file, acpi_video_device_EDID_seq_show,
  1041. PDE(inode)->data);
  1042. }
  1043. static int acpi_video_device_add_fs(struct acpi_device *device)
  1044. {
  1045. struct proc_dir_entry *entry, *device_dir;
  1046. struct acpi_video_device *vid_dev;
  1047. vid_dev = acpi_driver_data(device);
  1048. if (!vid_dev)
  1049. return -ENODEV;
  1050. device_dir = proc_mkdir(acpi_device_bid(device),
  1051. vid_dev->video->dir);
  1052. if (!device_dir)
  1053. return -ENOMEM;
  1054. /* 'info' [R] */
  1055. entry = proc_create_data("info", S_IRUGO, device_dir,
  1056. &acpi_video_device_info_fops, acpi_driver_data(device));
  1057. if (!entry)
  1058. goto err_remove_dir;
  1059. /* 'state' [R/W] */
  1060. entry = proc_create_data("state", S_IFREG | S_IRUGO | S_IWUSR,
  1061. device_dir,
  1062. &acpi_video_device_state_fops,
  1063. acpi_driver_data(device));
  1064. if (!entry)
  1065. goto err_remove_info;
  1066. /* 'brightness' [R/W] */
  1067. entry = proc_create_data("brightness", S_IFREG | S_IRUGO | S_IWUSR,
  1068. device_dir,
  1069. &acpi_video_device_brightness_fops,
  1070. acpi_driver_data(device));
  1071. if (!entry)
  1072. goto err_remove_state;
  1073. /* 'EDID' [R] */
  1074. entry = proc_create_data("EDID", S_IRUGO, device_dir,
  1075. &acpi_video_device_EDID_fops,
  1076. acpi_driver_data(device));
  1077. if (!entry)
  1078. goto err_remove_brightness;
  1079. acpi_device_dir(device) = device_dir;
  1080. return 0;
  1081. err_remove_brightness:
  1082. remove_proc_entry("brightness", device_dir);
  1083. err_remove_state:
  1084. remove_proc_entry("state", device_dir);
  1085. err_remove_info:
  1086. remove_proc_entry("info", device_dir);
  1087. err_remove_dir:
  1088. remove_proc_entry(acpi_device_bid(device), vid_dev->video->dir);
  1089. return -ENOMEM;
  1090. }
  1091. static int acpi_video_device_remove_fs(struct acpi_device *device)
  1092. {
  1093. struct acpi_video_device *vid_dev;
  1094. struct proc_dir_entry *device_dir;
  1095. vid_dev = acpi_driver_data(device);
  1096. if (!vid_dev || !vid_dev->video || !vid_dev->video->dir)
  1097. return -ENODEV;
  1098. device_dir = acpi_device_dir(device);
  1099. if (device_dir) {
  1100. remove_proc_entry("info", device_dir);
  1101. remove_proc_entry("state", device_dir);
  1102. remove_proc_entry("brightness", device_dir);
  1103. remove_proc_entry("EDID", device_dir);
  1104. remove_proc_entry(acpi_device_bid(device), vid_dev->video->dir);
  1105. acpi_device_dir(device) = NULL;
  1106. }
  1107. return 0;
  1108. }
  1109. /* video bus */
  1110. static int acpi_video_bus_info_seq_show(struct seq_file *seq, void *offset)
  1111. {
  1112. struct acpi_video_bus *video = seq->private;
  1113. if (!video)
  1114. goto end;
  1115. seq_printf(seq, "Switching heads: %s\n",
  1116. video->flags.multihead ? "yes" : "no");
  1117. seq_printf(seq, "Video ROM: %s\n",
  1118. video->flags.rom ? "yes" : "no");
  1119. seq_printf(seq, "Device to be POSTed on boot: %s\n",
  1120. video->flags.post ? "yes" : "no");
  1121. end:
  1122. return 0;
  1123. }
  1124. static int acpi_video_bus_info_open_fs(struct inode *inode, struct file *file)
  1125. {
  1126. return single_open(file, acpi_video_bus_info_seq_show,
  1127. PDE(inode)->data);
  1128. }
  1129. static int acpi_video_bus_ROM_seq_show(struct seq_file *seq, void *offset)
  1130. {
  1131. struct acpi_video_bus *video = seq->private;
  1132. if (!video)
  1133. goto end;
  1134. printk(KERN_INFO PREFIX "Please implement %s\n", __func__);
  1135. seq_printf(seq, "<TODO>\n");
  1136. end:
  1137. return 0;
  1138. }
  1139. static int acpi_video_bus_ROM_open_fs(struct inode *inode, struct file *file)
  1140. {
  1141. return single_open(file, acpi_video_bus_ROM_seq_show, PDE(inode)->data);
  1142. }
  1143. static int acpi_video_bus_POST_info_seq_show(struct seq_file *seq, void *offset)
  1144. {
  1145. struct acpi_video_bus *video = seq->private;
  1146. unsigned long long options;
  1147. int status;
  1148. if (!video)
  1149. goto end;
  1150. status = acpi_video_bus_POST_options(video, &options);
  1151. if (ACPI_SUCCESS(status)) {
  1152. if (!(options & 1)) {
  1153. printk(KERN_WARNING PREFIX
  1154. "The motherboard VGA device is not listed as a possible POST device.\n");
  1155. printk(KERN_WARNING PREFIX
  1156. "This indicates a BIOS bug. Please contact the manufacturer.\n");
  1157. }
  1158. printk(KERN_WARNING "%llx\n", options);
  1159. seq_printf(seq, "can POST: <integrated video>");
  1160. if (options & 2)
  1161. seq_printf(seq, " <PCI video>");
  1162. if (options & 4)
  1163. seq_printf(seq, " <AGP video>");
  1164. seq_putc(seq, '\n');
  1165. } else
  1166. seq_printf(seq, "<not supported>\n");
  1167. end:
  1168. return 0;
  1169. }
  1170. static int
  1171. acpi_video_bus_POST_info_open_fs(struct inode *inode, struct file *file)
  1172. {
  1173. return single_open(file, acpi_video_bus_POST_info_seq_show,
  1174. PDE(inode)->data);
  1175. }
  1176. static int acpi_video_bus_POST_seq_show(struct seq_file *seq, void *offset)
  1177. {
  1178. struct acpi_video_bus *video = seq->private;
  1179. int status;
  1180. unsigned long long id;
  1181. if (!video)
  1182. goto end;
  1183. status = acpi_video_bus_get_POST(video, &id);
  1184. if (!ACPI_SUCCESS(status)) {
  1185. seq_printf(seq, "<not supported>\n");
  1186. goto end;
  1187. }
  1188. seq_printf(seq, "device POSTed is <%s>\n", device_decode[id & 3]);
  1189. end:
  1190. return 0;
  1191. }
  1192. static int acpi_video_bus_DOS_seq_show(struct seq_file *seq, void *offset)
  1193. {
  1194. struct acpi_video_bus *video = seq->private;
  1195. seq_printf(seq, "DOS setting: <%d>\n", video->dos_setting);
  1196. return 0;
  1197. }
  1198. static int acpi_video_bus_POST_open_fs(struct inode *inode, struct file *file)
  1199. {
  1200. return single_open(file, acpi_video_bus_POST_seq_show,
  1201. PDE(inode)->data);
  1202. }
  1203. static int acpi_video_bus_DOS_open_fs(struct inode *inode, struct file *file)
  1204. {
  1205. return single_open(file, acpi_video_bus_DOS_seq_show, PDE(inode)->data);
  1206. }
  1207. static ssize_t
  1208. acpi_video_bus_write_POST(struct file *file,
  1209. const char __user * buffer,
  1210. size_t count, loff_t * data)
  1211. {
  1212. int status;
  1213. struct seq_file *m = file->private_data;
  1214. struct acpi_video_bus *video = m->private;
  1215. char str[12] = { 0 };
  1216. unsigned long long opt, options;
  1217. if (!video || count + 1 > sizeof str)
  1218. return -EINVAL;
  1219. status = acpi_video_bus_POST_options(video, &options);
  1220. if (!ACPI_SUCCESS(status))
  1221. return -EINVAL;
  1222. if (copy_from_user(str, buffer, count))
  1223. return -EFAULT;
  1224. str[count] = 0;
  1225. opt = strtoul(str, NULL, 0);
  1226. if (opt > 3)
  1227. return -EFAULT;
  1228. /* just in case an OEM 'forgot' the motherboard... */
  1229. options |= 1;
  1230. if (options & (1ul << opt)) {
  1231. status = acpi_video_bus_set_POST(video, opt);
  1232. if (!ACPI_SUCCESS(status))
  1233. return -EFAULT;
  1234. }
  1235. return count;
  1236. }
  1237. static ssize_t
  1238. acpi_video_bus_write_DOS(struct file *file,
  1239. const char __user * buffer,
  1240. size_t count, loff_t * data)
  1241. {
  1242. int status;
  1243. struct seq_file *m = file->private_data;
  1244. struct acpi_video_bus *video = m->private;
  1245. char str[12] = { 0 };
  1246. unsigned long opt;
  1247. if (!video || count + 1 > sizeof str)
  1248. return -EINVAL;
  1249. if (copy_from_user(str, buffer, count))
  1250. return -EFAULT;
  1251. str[count] = 0;
  1252. opt = strtoul(str, NULL, 0);
  1253. if (opt > 7)
  1254. return -EFAULT;
  1255. status = acpi_video_bus_DOS(video, opt & 0x3, (opt & 0x4) >> 2);
  1256. if (!ACPI_SUCCESS(status))
  1257. return -EFAULT;
  1258. return count;
  1259. }
  1260. static int acpi_video_bus_add_fs(struct acpi_device *device)
  1261. {
  1262. struct acpi_video_bus *video = acpi_driver_data(device);
  1263. struct proc_dir_entry *device_dir;
  1264. struct proc_dir_entry *entry;
  1265. device_dir = proc_mkdir(acpi_device_bid(device), acpi_video_dir);
  1266. if (!device_dir)
  1267. return -ENOMEM;
  1268. /* 'info' [R] */
  1269. entry = proc_create_data("info", S_IRUGO, device_dir,
  1270. &acpi_video_bus_info_fops,
  1271. acpi_driver_data(device));
  1272. if (!entry)
  1273. goto err_remove_dir;
  1274. /* 'ROM' [R] */
  1275. entry = proc_create_data("ROM", S_IRUGO, device_dir,
  1276. &acpi_video_bus_ROM_fops,
  1277. acpi_driver_data(device));
  1278. if (!entry)
  1279. goto err_remove_info;
  1280. /* 'POST_info' [R] */
  1281. entry = proc_create_data("POST_info", S_IRUGO, device_dir,
  1282. &acpi_video_bus_POST_info_fops,
  1283. acpi_driver_data(device));
  1284. if (!entry)
  1285. goto err_remove_rom;
  1286. /* 'POST' [R/W] */
  1287. entry = proc_create_data("POST", S_IFREG | S_IRUGO | S_IWUSR,
  1288. device_dir,
  1289. &acpi_video_bus_POST_fops,
  1290. acpi_driver_data(device));
  1291. if (!entry)
  1292. goto err_remove_post_info;
  1293. /* 'DOS' [R/W] */
  1294. entry = proc_create_data("DOS", S_IFREG | S_IRUGO | S_IWUSR,
  1295. device_dir,
  1296. &acpi_video_bus_DOS_fops,
  1297. acpi_driver_data(device));
  1298. if (!entry)
  1299. goto err_remove_post;
  1300. video->dir = acpi_device_dir(device) = device_dir;
  1301. return 0;
  1302. err_remove_post:
  1303. remove_proc_entry("POST", device_dir);
  1304. err_remove_post_info:
  1305. remove_proc_entry("POST_info", device_dir);
  1306. err_remove_rom:
  1307. remove_proc_entry("ROM", device_dir);
  1308. err_remove_info:
  1309. remove_proc_entry("info", device_dir);
  1310. err_remove_dir:
  1311. remove_proc_entry(acpi_device_bid(device), acpi_video_dir);
  1312. return -ENOMEM;
  1313. }
  1314. static int acpi_video_bus_remove_fs(struct acpi_device *device)
  1315. {
  1316. struct proc_dir_entry *device_dir = acpi_device_dir(device);
  1317. if (device_dir) {
  1318. remove_proc_entry("info", device_dir);
  1319. remove_proc_entry("ROM", device_dir);
  1320. remove_proc_entry("POST_info", device_dir);
  1321. remove_proc_entry("POST", device_dir);
  1322. remove_proc_entry("DOS", device_dir);
  1323. remove_proc_entry(acpi_device_bid(device), acpi_video_dir);
  1324. acpi_device_dir(device) = NULL;
  1325. }
  1326. return 0;
  1327. }
  1328. /* --------------------------------------------------------------------------
  1329. Driver Interface
  1330. -------------------------------------------------------------------------- */
  1331. /* device interface */
  1332. static struct acpi_video_device_attrib*
  1333. acpi_video_get_device_attr(struct acpi_video_bus *video, unsigned long device_id)
  1334. {
  1335. struct acpi_video_enumerated_device *ids;
  1336. int i;
  1337. for (i = 0; i < video->attached_count; i++) {
  1338. ids = &video->attached_array[i];
  1339. if ((ids->value.int_val & 0xffff) == device_id)
  1340. return &ids->value.attrib;
  1341. }
  1342. return NULL;
  1343. }
  1344. static int
  1345. acpi_video_bus_get_one_device(struct acpi_device *device,
  1346. struct acpi_video_bus *video)
  1347. {
  1348. unsigned long long device_id;
  1349. int status;
  1350. struct acpi_video_device *data;
  1351. struct acpi_video_device_attrib* attribute;
  1352. if (!device || !video)
  1353. return -EINVAL;
  1354. status =
  1355. acpi_evaluate_integer(device->handle, "_ADR", NULL, &device_id);
  1356. if (ACPI_SUCCESS(status)) {
  1357. data = kzalloc(sizeof(struct acpi_video_device), GFP_KERNEL);
  1358. if (!data)
  1359. return -ENOMEM;
  1360. strcpy(acpi_device_name(device), ACPI_VIDEO_DEVICE_NAME);
  1361. strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
  1362. device->driver_data = data;
  1363. data->device_id = device_id;
  1364. data->video = video;
  1365. data->dev = device;
  1366. attribute = acpi_video_get_device_attr(video, device_id);
  1367. if((attribute != NULL) && attribute->device_id_scheme) {
  1368. switch (attribute->display_type) {
  1369. case ACPI_VIDEO_DISPLAY_CRT:
  1370. data->flags.crt = 1;
  1371. break;
  1372. case ACPI_VIDEO_DISPLAY_TV:
  1373. data->flags.tvout = 1;
  1374. break;
  1375. case ACPI_VIDEO_DISPLAY_DVI:
  1376. data->flags.dvi = 1;
  1377. break;
  1378. case ACPI_VIDEO_DISPLAY_LCD:
  1379. data->flags.lcd = 1;
  1380. break;
  1381. default:
  1382. data->flags.unknown = 1;
  1383. break;
  1384. }
  1385. if(attribute->bios_can_detect)
  1386. data->flags.bios = 1;
  1387. } else
  1388. data->flags.unknown = 1;
  1389. acpi_video_device_bind(video, data);
  1390. acpi_video_device_find_cap(data);
  1391. status = acpi_install_notify_handler(device->handle,
  1392. ACPI_DEVICE_NOTIFY,
  1393. acpi_video_device_notify,
  1394. data);
  1395. if (ACPI_FAILURE(status)) {
  1396. printk(KERN_ERR PREFIX
  1397. "Error installing notify handler\n");
  1398. if(data->brightness)
  1399. kfree(data->brightness->levels);
  1400. kfree(data->brightness);
  1401. kfree(data);
  1402. return -ENODEV;
  1403. }
  1404. mutex_lock(&video->device_list_lock);
  1405. list_add_tail(&data->entry, &video->video_device_list);
  1406. mutex_unlock(&video->device_list_lock);
  1407. acpi_video_device_add_fs(device);
  1408. return 0;
  1409. }
  1410. return -ENOENT;
  1411. }
  1412. /*
  1413. * Arg:
  1414. * video : video bus device
  1415. *
  1416. * Return:
  1417. * none
  1418. *
  1419. * Enumerate the video device list of the video bus,
  1420. * bind the ids with the corresponding video devices
  1421. * under the video bus.
  1422. */
  1423. static void acpi_video_device_rebind(struct acpi_video_bus *video)
  1424. {
  1425. struct acpi_video_device *dev;
  1426. mutex_lock(&video->device_list_lock);
  1427. list_for_each_entry(dev, &video->video_device_list, entry)
  1428. acpi_video_device_bind(video, dev);
  1429. mutex_unlock(&video->device_list_lock);
  1430. }
  1431. /*
  1432. * Arg:
  1433. * video : video bus device
  1434. * device : video output device under the video
  1435. * bus
  1436. *
  1437. * Return:
  1438. * none
  1439. *
  1440. * Bind the ids with the corresponding video devices
  1441. * under the video bus.
  1442. */
  1443. static void
  1444. acpi_video_device_bind(struct acpi_video_bus *video,
  1445. struct acpi_video_device *device)
  1446. {
  1447. struct acpi_video_enumerated_device *ids;
  1448. int i;
  1449. for (i = 0; i < video->attached_count; i++) {
  1450. ids = &video->attached_array[i];
  1451. if (device->device_id == (ids->value.int_val & 0xffff)) {
  1452. ids->bind_info = device;
  1453. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "device_bind %d\n", i));
  1454. }
  1455. }
  1456. }
  1457. /*
  1458. * Arg:
  1459. * video : video bus device
  1460. *
  1461. * Return:
  1462. * < 0 : error
  1463. *
  1464. * Call _DOD to enumerate all devices attached to display adapter
  1465. *
  1466. */
  1467. static int acpi_video_device_enumerate(struct acpi_video_bus *video)
  1468. {
  1469. int status;
  1470. int count;
  1471. int i;
  1472. struct acpi_video_enumerated_device *active_list;
  1473. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  1474. union acpi_object *dod = NULL;
  1475. union acpi_object *obj;
  1476. status = acpi_evaluate_object(video->device->handle, "_DOD", NULL, &buffer);
  1477. if (!ACPI_SUCCESS(status)) {
  1478. ACPI_EXCEPTION((AE_INFO, status, "Evaluating _DOD"));
  1479. return status;
  1480. }
  1481. dod = buffer.pointer;
  1482. if (!dod || (dod->type != ACPI_TYPE_PACKAGE)) {
  1483. ACPI_EXCEPTION((AE_INFO, status, "Invalid _DOD data"));
  1484. status = -EFAULT;
  1485. goto out;
  1486. }
  1487. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d video heads in _DOD\n",
  1488. dod->package.count));
  1489. active_list = kcalloc(1 + dod->package.count,
  1490. sizeof(struct acpi_video_enumerated_device),
  1491. GFP_KERNEL);
  1492. if (!active_list) {
  1493. status = -ENOMEM;
  1494. goto out;
  1495. }
  1496. count = 0;
  1497. for (i = 0; i < dod->package.count; i++) {
  1498. obj = &dod->package.elements[i];
  1499. if (obj->type != ACPI_TYPE_INTEGER) {
  1500. printk(KERN_ERR PREFIX
  1501. "Invalid _DOD data in element %d\n", i);
  1502. continue;
  1503. }
  1504. active_list[count].value.int_val = obj->integer.value;
  1505. active_list[count].bind_info = NULL;
  1506. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "dod element[%d] = %d\n", i,
  1507. (int)obj->integer.value));
  1508. count++;
  1509. }
  1510. kfree(video->attached_array);
  1511. video->attached_array = active_list;
  1512. video->attached_count = count;
  1513. out:
  1514. kfree(buffer.pointer);
  1515. return status;
  1516. }
  1517. static int
  1518. acpi_video_get_next_level(struct acpi_video_device *device,
  1519. u32 level_current, u32 event)
  1520. {
  1521. int min, max, min_above, max_below, i, l, delta = 255;
  1522. max = max_below = 0;
  1523. min = min_above = 255;
  1524. /* Find closest level to level_current */
  1525. for (i = 2; i < device->brightness->count; i++) {
  1526. l = device->brightness->levels[i];
  1527. if (abs(l - level_current) < abs(delta)) {
  1528. delta = l - level_current;
  1529. if (!delta)
  1530. break;
  1531. }
  1532. }
  1533. /* Ajust level_current to closest available level */
  1534. level_current += delta;
  1535. for (i = 2; i < device->brightness->count; i++) {
  1536. l = device->brightness->levels[i];
  1537. if (l < min)
  1538. min = l;
  1539. if (l > max)
  1540. max = l;
  1541. if (l < min_above && l > level_current)
  1542. min_above = l;
  1543. if (l > max_below && l < level_current)
  1544. max_below = l;
  1545. }
  1546. switch (event) {
  1547. case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS:
  1548. return (level_current < max) ? min_above : min;
  1549. case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS:
  1550. return (level_current < max) ? min_above : max;
  1551. case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS:
  1552. return (level_current > min) ? max_below : min;
  1553. case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS:
  1554. case ACPI_VIDEO_NOTIFY_DISPLAY_OFF:
  1555. return 0;
  1556. default:
  1557. return level_current;
  1558. }
  1559. }
  1560. static int
  1561. acpi_video_switch_brightness(struct acpi_video_device *device, int event)
  1562. {
  1563. unsigned long long level_current, level_next;
  1564. int result = -EINVAL;
  1565. if (!device->brightness)
  1566. goto out;
  1567. result = acpi_video_device_lcd_get_level_current(device,
  1568. &level_current);
  1569. if (result)
  1570. goto out;
  1571. level_next = acpi_video_get_next_level(device, level_current, event);
  1572. result = acpi_video_device_lcd_set_level(device, level_next);
  1573. out:
  1574. if (result)
  1575. printk(KERN_ERR PREFIX "Failed to switch the brightness\n");
  1576. return result;
  1577. }
  1578. static int
  1579. acpi_video_bus_get_devices(struct acpi_video_bus *video,
  1580. struct acpi_device *device)
  1581. {
  1582. int status = 0;
  1583. struct acpi_device *dev;
  1584. acpi_video_device_enumerate(video);
  1585. list_for_each_entry(dev, &device->children, node) {
  1586. status = acpi_video_bus_get_one_device(dev, video);
  1587. if (ACPI_FAILURE(status)) {
  1588. printk(KERN_WARNING PREFIX
  1589. "Cant attach device");
  1590. continue;
  1591. }
  1592. }
  1593. return status;
  1594. }
  1595. static int acpi_video_bus_put_one_device(struct acpi_video_device *device)
  1596. {
  1597. acpi_status status;
  1598. struct acpi_video_bus *video;
  1599. if (!device || !device->video)
  1600. return -ENOENT;
  1601. video = device->video;
  1602. acpi_video_device_remove_fs(device->dev);
  1603. status = acpi_remove_notify_handler(device->dev->handle,
  1604. ACPI_DEVICE_NOTIFY,
  1605. acpi_video_device_notify);
  1606. backlight_device_unregister(device->backlight);
  1607. if (device->cdev) {
  1608. sysfs_remove_link(&device->dev->dev.kobj,
  1609. "thermal_cooling");
  1610. sysfs_remove_link(&device->cdev->device.kobj,
  1611. "device");
  1612. thermal_cooling_device_unregister(device->cdev);
  1613. device->cdev = NULL;
  1614. }
  1615. video_output_unregister(device->output_dev);
  1616. return 0;
  1617. }
  1618. static int acpi_video_bus_put_devices(struct acpi_video_bus *video)
  1619. {
  1620. int status;
  1621. struct acpi_video_device *dev, *next;
  1622. mutex_lock(&video->device_list_lock);
  1623. list_for_each_entry_safe(dev, next, &video->video_device_list, entry) {
  1624. status = acpi_video_bus_put_one_device(dev);
  1625. if (ACPI_FAILURE(status))
  1626. printk(KERN_WARNING PREFIX
  1627. "hhuuhhuu bug in acpi video driver.\n");
  1628. if (dev->brightness) {
  1629. kfree(dev->brightness->levels);
  1630. kfree(dev->brightness);
  1631. }
  1632. list_del(&dev->entry);
  1633. kfree(dev);
  1634. }
  1635. mutex_unlock(&video->device_list_lock);
  1636. return 0;
  1637. }
  1638. /* acpi_video interface */
  1639. static int acpi_video_bus_start_devices(struct acpi_video_bus *video)
  1640. {
  1641. return acpi_video_bus_DOS(video, 0, 0);
  1642. }
  1643. static int acpi_video_bus_stop_devices(struct acpi_video_bus *video)
  1644. {
  1645. return acpi_video_bus_DOS(video, 0, 1);
  1646. }
  1647. static void acpi_video_bus_notify(struct acpi_device *device, u32 event)
  1648. {
  1649. struct acpi_video_bus *video = acpi_driver_data(device);
  1650. struct input_dev *input;
  1651. int keycode;
  1652. if (!video)
  1653. return;
  1654. input = video->input;
  1655. switch (event) {
  1656. case ACPI_VIDEO_NOTIFY_SWITCH: /* User requested a switch,
  1657. * most likely via hotkey. */
  1658. acpi_bus_generate_proc_event(device, event, 0);
  1659. keycode = KEY_SWITCHVIDEOMODE;
  1660. break;
  1661. case ACPI_VIDEO_NOTIFY_PROBE: /* User plugged in or removed a video
  1662. * connector. */
  1663. acpi_video_device_enumerate(video);
  1664. acpi_video_device_rebind(video);
  1665. acpi_bus_generate_proc_event(device, event, 0);
  1666. keycode = KEY_SWITCHVIDEOMODE;
  1667. break;
  1668. case ACPI_VIDEO_NOTIFY_CYCLE: /* Cycle Display output hotkey pressed. */
  1669. acpi_bus_generate_proc_event(device, event, 0);
  1670. keycode = KEY_SWITCHVIDEOMODE;
  1671. break;
  1672. case ACPI_VIDEO_NOTIFY_NEXT_OUTPUT: /* Next Display output hotkey pressed. */
  1673. acpi_bus_generate_proc_event(device, event, 0);
  1674. keycode = KEY_VIDEO_NEXT;
  1675. break;
  1676. case ACPI_VIDEO_NOTIFY_PREV_OUTPUT: /* previous Display output hotkey pressed. */
  1677. acpi_bus_generate_proc_event(device, event, 0);
  1678. keycode = KEY_VIDEO_PREV;
  1679. break;
  1680. default:
  1681. keycode = KEY_UNKNOWN;
  1682. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  1683. "Unsupported event [0x%x]\n", event));
  1684. break;
  1685. }
  1686. acpi_notifier_call_chain(device, event, 0);
  1687. input_report_key(input, keycode, 1);
  1688. input_sync(input);
  1689. input_report_key(input, keycode, 0);
  1690. input_sync(input);
  1691. return;
  1692. }
  1693. static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data)
  1694. {
  1695. struct acpi_video_device *video_device = data;
  1696. struct acpi_device *device = NULL;
  1697. struct acpi_video_bus *bus;
  1698. struct input_dev *input;
  1699. int keycode;
  1700. if (!video_device)
  1701. return;
  1702. device = video_device->dev;
  1703. bus = video_device->video;
  1704. input = bus->input;
  1705. switch (event) {
  1706. case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS: /* Cycle brightness */
  1707. if (brightness_switch_enabled)
  1708. acpi_video_switch_brightness(video_device, event);
  1709. acpi_bus_generate_proc_event(device, event, 0);
  1710. keycode = KEY_BRIGHTNESS_CYCLE;
  1711. break;
  1712. case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS: /* Increase brightness */
  1713. if (brightness_switch_enabled)
  1714. acpi_video_switch_brightness(video_device, event);
  1715. acpi_bus_generate_proc_event(device, event, 0);
  1716. keycode = KEY_BRIGHTNESSUP;
  1717. break;
  1718. case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS: /* Decrease brightness */
  1719. if (brightness_switch_enabled)
  1720. acpi_video_switch_brightness(video_device, event);
  1721. acpi_bus_generate_proc_event(device, event, 0);
  1722. keycode = KEY_BRIGHTNESSDOWN;
  1723. break;
  1724. case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS: /* zero brightnesss */
  1725. if (brightness_switch_enabled)
  1726. acpi_video_switch_brightness(video_device, event);
  1727. acpi_bus_generate_proc_event(device, event, 0);
  1728. keycode = KEY_BRIGHTNESS_ZERO;
  1729. break;
  1730. case ACPI_VIDEO_NOTIFY_DISPLAY_OFF: /* display device off */
  1731. if (brightness_switch_enabled)
  1732. acpi_video_switch_brightness(video_device, event);
  1733. acpi_bus_generate_proc_event(device, event, 0);
  1734. keycode = KEY_DISPLAY_OFF;
  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 int instance;
  1750. static int acpi_video_resume(struct acpi_device *device)
  1751. {
  1752. struct acpi_video_bus *video;
  1753. struct acpi_video_device *video_device;
  1754. int i;
  1755. if (!device || !acpi_driver_data(device))
  1756. return -EINVAL;
  1757. video = acpi_driver_data(device);
  1758. for (i = 0; i < video->attached_count; i++) {
  1759. video_device = video->attached_array[i].bind_info;
  1760. if (video_device && video_device->backlight)
  1761. acpi_video_set_brightness(video_device->backlight);
  1762. }
  1763. return AE_OK;
  1764. }
  1765. static int acpi_video_bus_add(struct acpi_device *device)
  1766. {
  1767. struct acpi_video_bus *video;
  1768. struct input_dev *input;
  1769. int error;
  1770. video = kzalloc(sizeof(struct acpi_video_bus), GFP_KERNEL);
  1771. if (!video)
  1772. return -ENOMEM;
  1773. /* a hack to fix the duplicate name "VID" problem on T61 */
  1774. if (!strcmp(device->pnp.bus_id, "VID")) {
  1775. if (instance)
  1776. device->pnp.bus_id[3] = '0' + instance;
  1777. instance ++;
  1778. }
  1779. /* a hack to fix the duplicate name "VGA" problem on Pa 3553 */
  1780. if (!strcmp(device->pnp.bus_id, "VGA")) {
  1781. if (instance)
  1782. device->pnp.bus_id[3] = '0' + instance;
  1783. instance++;
  1784. }
  1785. video->device = device;
  1786. strcpy(acpi_device_name(device), ACPI_VIDEO_BUS_NAME);
  1787. strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
  1788. device->driver_data = video;
  1789. acpi_video_bus_find_cap(video);
  1790. error = acpi_video_bus_check(video);
  1791. if (error)
  1792. goto err_free_video;
  1793. error = acpi_video_bus_add_fs(device);
  1794. if (error)
  1795. goto err_free_video;
  1796. mutex_init(&video->device_list_lock);
  1797. INIT_LIST_HEAD(&video->video_device_list);
  1798. acpi_video_bus_get_devices(video, device);
  1799. acpi_video_bus_start_devices(video);
  1800. video->input = input = input_allocate_device();
  1801. if (!input) {
  1802. error = -ENOMEM;
  1803. goto err_stop_video;
  1804. }
  1805. snprintf(video->phys, sizeof(video->phys),
  1806. "%s/video/input0", acpi_device_hid(video->device));
  1807. input->name = acpi_device_name(video->device);
  1808. input->phys = video->phys;
  1809. input->id.bustype = BUS_HOST;
  1810. input->id.product = 0x06;
  1811. input->dev.parent = &device->dev;
  1812. input->evbit[0] = BIT(EV_KEY);
  1813. set_bit(KEY_SWITCHVIDEOMODE, input->keybit);
  1814. set_bit(KEY_VIDEO_NEXT, input->keybit);
  1815. set_bit(KEY_VIDEO_PREV, input->keybit);
  1816. set_bit(KEY_BRIGHTNESS_CYCLE, input->keybit);
  1817. set_bit(KEY_BRIGHTNESSUP, input->keybit);
  1818. set_bit(KEY_BRIGHTNESSDOWN, input->keybit);
  1819. set_bit(KEY_BRIGHTNESS_ZERO, input->keybit);
  1820. set_bit(KEY_DISPLAY_OFF, input->keybit);
  1821. set_bit(KEY_UNKNOWN, input->keybit);
  1822. error = input_register_device(input);
  1823. if (error)
  1824. goto err_free_input_dev;
  1825. printk(KERN_INFO PREFIX "%s [%s] (multi-head: %s rom: %s post: %s)\n",
  1826. ACPI_VIDEO_DEVICE_NAME, acpi_device_bid(device),
  1827. video->flags.multihead ? "yes" : "no",
  1828. video->flags.rom ? "yes" : "no",
  1829. video->flags.post ? "yes" : "no");
  1830. return 0;
  1831. err_free_input_dev:
  1832. input_free_device(input);
  1833. err_stop_video:
  1834. acpi_video_bus_stop_devices(video);
  1835. acpi_video_bus_put_devices(video);
  1836. kfree(video->attached_array);
  1837. acpi_video_bus_remove_fs(device);
  1838. err_free_video:
  1839. kfree(video);
  1840. device->driver_data = NULL;
  1841. return error;
  1842. }
  1843. static int acpi_video_bus_remove(struct acpi_device *device, int type)
  1844. {
  1845. struct acpi_video_bus *video = NULL;
  1846. if (!device || !acpi_driver_data(device))
  1847. return -EINVAL;
  1848. video = acpi_driver_data(device);
  1849. acpi_video_bus_stop_devices(video);
  1850. acpi_video_bus_put_devices(video);
  1851. acpi_video_bus_remove_fs(device);
  1852. input_unregister_device(video->input);
  1853. kfree(video->attached_array);
  1854. kfree(video);
  1855. return 0;
  1856. }
  1857. static int __init intel_opregion_present(void)
  1858. {
  1859. #if defined(CONFIG_DRM_I915) || defined(CONFIG_DRM_I915_MODULE)
  1860. struct pci_dev *dev = NULL;
  1861. u32 address;
  1862. for_each_pci_dev(dev) {
  1863. if ((dev->class >> 8) != PCI_CLASS_DISPLAY_VGA)
  1864. continue;
  1865. if (dev->vendor != PCI_VENDOR_ID_INTEL)
  1866. continue;
  1867. pci_read_config_dword(dev, 0xfc, &address);
  1868. if (!address)
  1869. continue;
  1870. return 1;
  1871. }
  1872. #endif
  1873. return 0;
  1874. }
  1875. int acpi_video_register(void)
  1876. {
  1877. int result = 0;
  1878. acpi_video_dir = proc_mkdir(ACPI_VIDEO_CLASS, acpi_root_dir);
  1879. if (!acpi_video_dir)
  1880. return -ENODEV;
  1881. result = acpi_bus_register_driver(&acpi_video_bus);
  1882. if (result < 0) {
  1883. remove_proc_entry(ACPI_VIDEO_CLASS, acpi_root_dir);
  1884. return -ENODEV;
  1885. }
  1886. return 0;
  1887. }
  1888. EXPORT_SYMBOL(acpi_video_register);
  1889. /*
  1890. * This is kind of nasty. Hardware using Intel chipsets may require
  1891. * the video opregion code to be run first in order to initialise
  1892. * state before any ACPI video calls are made. To handle this we defer
  1893. * registration of the video class until the opregion code has run.
  1894. */
  1895. static int __init acpi_video_init(void)
  1896. {
  1897. if (intel_opregion_present())
  1898. return 0;
  1899. return acpi_video_register();
  1900. }
  1901. static void __exit acpi_video_exit(void)
  1902. {
  1903. acpi_bus_unregister_driver(&acpi_video_bus);
  1904. remove_proc_entry(ACPI_VIDEO_CLASS, acpi_root_dir);
  1905. return;
  1906. }
  1907. module_init(acpi_video_init);
  1908. module_exit(acpi_video_exit);