video.c 58 KB

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