video.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956
  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/proc_fs.h>
  32. #include <linux/seq_file.h>
  33. #include <linux/backlight.h>
  34. #include <asm/uaccess.h>
  35. #include <acpi/acpi_bus.h>
  36. #include <acpi/acpi_drivers.h>
  37. #define ACPI_VIDEO_COMPONENT 0x08000000
  38. #define ACPI_VIDEO_CLASS "video"
  39. #define ACPI_VIDEO_DRIVER_NAME "ACPI Video Driver"
  40. #define ACPI_VIDEO_BUS_NAME "Video Bus"
  41. #define ACPI_VIDEO_DEVICE_NAME "Video Device"
  42. #define ACPI_VIDEO_NOTIFY_SWITCH 0x80
  43. #define ACPI_VIDEO_NOTIFY_PROBE 0x81
  44. #define ACPI_VIDEO_NOTIFY_CYCLE 0x82
  45. #define ACPI_VIDEO_NOTIFY_NEXT_OUTPUT 0x83
  46. #define ACPI_VIDEO_NOTIFY_PREV_OUTPUT 0x84
  47. #define ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS 0x85
  48. #define ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS 0x86
  49. #define ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS 0x87
  50. #define ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS 0x88
  51. #define ACPI_VIDEO_NOTIFY_DISPLAY_OFF 0x89
  52. #define ACPI_VIDEO_HEAD_INVALID (~0u - 1)
  53. #define ACPI_VIDEO_HEAD_END (~0u)
  54. #define MAX_NAME_LEN 20
  55. #define ACPI_VIDEO_DISPLAY_CRT 1
  56. #define ACPI_VIDEO_DISPLAY_TV 2
  57. #define ACPI_VIDEO_DISPLAY_DVI 3
  58. #define ACPI_VIDEO_DISPLAY_LCD 4
  59. #define _COMPONENT ACPI_VIDEO_COMPONENT
  60. ACPI_MODULE_NAME("acpi_video")
  61. MODULE_AUTHOR("Bruno Ducrot");
  62. MODULE_DESCRIPTION(ACPI_VIDEO_DRIVER_NAME);
  63. MODULE_LICENSE("GPL");
  64. static int acpi_video_bus_add(struct acpi_device *device);
  65. static int acpi_video_bus_remove(struct acpi_device *device, int type);
  66. static int acpi_video_bus_match(struct acpi_device *device,
  67. struct acpi_driver *driver);
  68. static struct acpi_driver acpi_video_bus = {
  69. .name = ACPI_VIDEO_DRIVER_NAME,
  70. .class = ACPI_VIDEO_CLASS,
  71. .ops = {
  72. .add = acpi_video_bus_add,
  73. .remove = acpi_video_bus_remove,
  74. .match = acpi_video_bus_match,
  75. },
  76. };
  77. struct acpi_video_bus_flags {
  78. u8 multihead:1; /* can switch video heads */
  79. u8 rom:1; /* can retrieve a video rom */
  80. u8 post:1; /* can configure the head to */
  81. u8 reserved:5;
  82. };
  83. struct acpi_video_bus_cap {
  84. u8 _DOS:1; /*Enable/Disable output switching */
  85. u8 _DOD:1; /*Enumerate all devices attached to display adapter */
  86. u8 _ROM:1; /*Get ROM Data */
  87. u8 _GPD:1; /*Get POST Device */
  88. u8 _SPD:1; /*Set POST Device */
  89. u8 _VPO:1; /*Video POST Options */
  90. u8 reserved:2;
  91. };
  92. struct acpi_video_device_attrib {
  93. u32 display_index:4; /* A zero-based instance of the Display */
  94. u32 display_port_attachment:4; /*This field differenates displays type */
  95. u32 display_type:4; /*Describe the specific type in use */
  96. u32 vendor_specific:4; /*Chipset Vendor Specifi */
  97. u32 bios_can_detect:1; /*BIOS can detect the device */
  98. u32 depend_on_vga:1; /*Non-VGA output device whose power is related to
  99. the VGA device. */
  100. u32 pipe_id:3; /*For VGA multiple-head devices. */
  101. u32 reserved:10; /*Must be 0 */
  102. u32 device_id_scheme:1; /*Device ID Scheme */
  103. };
  104. struct acpi_video_enumerated_device {
  105. union {
  106. u32 int_val;
  107. struct acpi_video_device_attrib attrib;
  108. } value;
  109. struct acpi_video_device *bind_info;
  110. };
  111. struct acpi_video_bus {
  112. struct acpi_device *device;
  113. u8 dos_setting;
  114. struct acpi_video_enumerated_device *attached_array;
  115. u8 attached_count;
  116. struct acpi_video_bus_cap cap;
  117. struct acpi_video_bus_flags flags;
  118. struct semaphore sem;
  119. struct list_head video_device_list;
  120. struct proc_dir_entry *dir;
  121. };
  122. struct acpi_video_device_flags {
  123. u8 crt:1;
  124. u8 lcd:1;
  125. u8 tvout:1;
  126. u8 dvi:1;
  127. u8 bios:1;
  128. u8 unknown:1;
  129. u8 reserved:2;
  130. };
  131. struct acpi_video_device_cap {
  132. u8 _ADR:1; /*Return the unique ID */
  133. u8 _BCL:1; /*Query list of brightness control levels supported */
  134. u8 _BCM:1; /*Set the brightness level */
  135. u8 _BQC:1; /* Get current brightness level */
  136. u8 _DDC:1; /*Return the EDID for this device */
  137. u8 _DCS:1; /*Return status of output device */
  138. u8 _DGS:1; /*Query graphics state */
  139. u8 _DSS:1; /*Device state set */
  140. };
  141. struct acpi_video_device_brightness {
  142. int curr;
  143. int count;
  144. int *levels;
  145. };
  146. struct acpi_video_device {
  147. unsigned long device_id;
  148. struct acpi_video_device_flags flags;
  149. struct acpi_video_device_cap cap;
  150. struct list_head entry;
  151. struct acpi_video_bus *video;
  152. struct acpi_device *dev;
  153. struct acpi_video_device_brightness *brightness;
  154. struct backlight_device *backlight;
  155. struct backlight_properties *data;
  156. };
  157. /* bus */
  158. static int acpi_video_bus_info_open_fs(struct inode *inode, struct file *file);
  159. static struct file_operations acpi_video_bus_info_fops = {
  160. .open = acpi_video_bus_info_open_fs,
  161. .read = seq_read,
  162. .llseek = seq_lseek,
  163. .release = single_release,
  164. };
  165. static int acpi_video_bus_ROM_open_fs(struct inode *inode, struct file *file);
  166. static struct file_operations acpi_video_bus_ROM_fops = {
  167. .open = acpi_video_bus_ROM_open_fs,
  168. .read = seq_read,
  169. .llseek = seq_lseek,
  170. .release = single_release,
  171. };
  172. static int acpi_video_bus_POST_info_open_fs(struct inode *inode,
  173. struct file *file);
  174. static struct file_operations acpi_video_bus_POST_info_fops = {
  175. .open = acpi_video_bus_POST_info_open_fs,
  176. .read = seq_read,
  177. .llseek = seq_lseek,
  178. .release = single_release,
  179. };
  180. static int acpi_video_bus_POST_open_fs(struct inode *inode, struct file *file);
  181. static struct file_operations acpi_video_bus_POST_fops = {
  182. .open = acpi_video_bus_POST_open_fs,
  183. .read = seq_read,
  184. .llseek = seq_lseek,
  185. .release = single_release,
  186. };
  187. static int acpi_video_bus_DOS_open_fs(struct inode *inode, struct file *file);
  188. static struct file_operations acpi_video_bus_DOS_fops = {
  189. .open = acpi_video_bus_DOS_open_fs,
  190. .read = seq_read,
  191. .llseek = seq_lseek,
  192. .release = single_release,
  193. };
  194. /* device */
  195. static int acpi_video_device_info_open_fs(struct inode *inode,
  196. struct file *file);
  197. static struct file_operations acpi_video_device_info_fops = {
  198. .open = acpi_video_device_info_open_fs,
  199. .read = seq_read,
  200. .llseek = seq_lseek,
  201. .release = single_release,
  202. };
  203. static int acpi_video_device_state_open_fs(struct inode *inode,
  204. struct file *file);
  205. static struct file_operations acpi_video_device_state_fops = {
  206. .open = acpi_video_device_state_open_fs,
  207. .read = seq_read,
  208. .llseek = seq_lseek,
  209. .release = single_release,
  210. };
  211. static int acpi_video_device_brightness_open_fs(struct inode *inode,
  212. struct file *file);
  213. static struct file_operations acpi_video_device_brightness_fops = {
  214. .open = acpi_video_device_brightness_open_fs,
  215. .read = seq_read,
  216. .llseek = seq_lseek,
  217. .release = single_release,
  218. };
  219. static int acpi_video_device_EDID_open_fs(struct inode *inode,
  220. struct file *file);
  221. static struct file_operations acpi_video_device_EDID_fops = {
  222. .open = acpi_video_device_EDID_open_fs,
  223. .read = seq_read,
  224. .llseek = seq_lseek,
  225. .release = single_release,
  226. };
  227. static char device_decode[][30] = {
  228. "motherboard VGA device",
  229. "PCI VGA device",
  230. "AGP VGA device",
  231. "UNKNOWN",
  232. };
  233. static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data);
  234. static void acpi_video_device_rebind(struct acpi_video_bus *video);
  235. static void acpi_video_device_bind(struct acpi_video_bus *video,
  236. struct acpi_video_device *device);
  237. static int acpi_video_device_enumerate(struct acpi_video_bus *video);
  238. static int acpi_video_switch_output(struct acpi_video_bus *video, int event);
  239. static int acpi_video_device_lcd_set_level(struct acpi_video_device *device,
  240. int level);
  241. static int acpi_video_device_lcd_get_level_current(
  242. struct acpi_video_device *device,
  243. unsigned long *level);
  244. static int acpi_video_get_next_level(struct acpi_video_device *device,
  245. u32 level_current, u32 event);
  246. static void acpi_video_switch_brightness(struct acpi_video_device *device,
  247. int event);
  248. /*backlight device sysfs support*/
  249. static int acpi_video_get_brightness(struct backlight_device *bd)
  250. {
  251. unsigned long cur_level;
  252. struct acpi_video_device *vd =
  253. (struct acpi_video_device *)class_get_devdata(&bd->class_dev);
  254. acpi_video_device_lcd_get_level_current(vd, &cur_level);
  255. return (int) cur_level;
  256. }
  257. static int acpi_video_set_brightness(struct backlight_device *bd)
  258. {
  259. int request_level = bd->props->brightness;
  260. struct acpi_video_device *vd =
  261. (struct acpi_video_device *)class_get_devdata(&bd->class_dev);
  262. acpi_video_device_lcd_set_level(vd, request_level);
  263. return 0;
  264. }
  265. /* --------------------------------------------------------------------------
  266. Video Management
  267. -------------------------------------------------------------------------- */
  268. /* device */
  269. static int
  270. acpi_video_device_query(struct acpi_video_device *device, unsigned long *state)
  271. {
  272. int status;
  273. status = acpi_evaluate_integer(device->dev->handle, "_DGS", NULL, state);
  274. return status;
  275. }
  276. static int
  277. acpi_video_device_get_state(struct acpi_video_device *device,
  278. unsigned long *state)
  279. {
  280. int status;
  281. status = acpi_evaluate_integer(device->dev->handle, "_DCS", NULL, state);
  282. return status;
  283. }
  284. static int
  285. acpi_video_device_set_state(struct acpi_video_device *device, int state)
  286. {
  287. int status;
  288. union acpi_object arg0 = { ACPI_TYPE_INTEGER };
  289. struct acpi_object_list args = { 1, &arg0 };
  290. unsigned long ret;
  291. arg0.integer.value = state;
  292. status = acpi_evaluate_integer(device->dev->handle, "_DSS", &args, &ret);
  293. return status;
  294. }
  295. static int
  296. acpi_video_device_lcd_query_levels(struct acpi_video_device *device,
  297. union acpi_object **levels)
  298. {
  299. int status;
  300. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  301. union acpi_object *obj;
  302. *levels = NULL;
  303. status = acpi_evaluate_object(device->dev->handle, "_BCL", NULL, &buffer);
  304. if (!ACPI_SUCCESS(status))
  305. return status;
  306. obj = (union acpi_object *)buffer.pointer;
  307. if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
  308. printk(KERN_ERR PREFIX "Invalid _BCL data\n");
  309. status = -EFAULT;
  310. goto err;
  311. }
  312. *levels = obj;
  313. return 0;
  314. err:
  315. kfree(buffer.pointer);
  316. return status;
  317. }
  318. static int
  319. acpi_video_device_lcd_set_level(struct acpi_video_device *device, int level)
  320. {
  321. int status;
  322. union acpi_object arg0 = { ACPI_TYPE_INTEGER };
  323. struct acpi_object_list args = { 1, &arg0 };
  324. arg0.integer.value = level;
  325. status = acpi_evaluate_object(device->dev->handle, "_BCM", &args, NULL);
  326. printk(KERN_DEBUG "set_level status: %x\n", status);
  327. return status;
  328. }
  329. static int
  330. acpi_video_device_lcd_get_level_current(struct acpi_video_device *device,
  331. unsigned long *level)
  332. {
  333. int status;
  334. status = acpi_evaluate_integer(device->dev->handle, "_BQC", NULL, level);
  335. return status;
  336. }
  337. static int
  338. acpi_video_device_EDID(struct acpi_video_device *device,
  339. union acpi_object **edid, ssize_t length)
  340. {
  341. int status;
  342. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  343. union acpi_object *obj;
  344. union acpi_object arg0 = { ACPI_TYPE_INTEGER };
  345. struct acpi_object_list args = { 1, &arg0 };
  346. *edid = NULL;
  347. if (!device)
  348. return -ENODEV;
  349. if (length == 128)
  350. arg0.integer.value = 1;
  351. else if (length == 256)
  352. arg0.integer.value = 2;
  353. else
  354. return -EINVAL;
  355. status = acpi_evaluate_object(device->dev->handle, "_DDC", &args, &buffer);
  356. if (ACPI_FAILURE(status))
  357. return -ENODEV;
  358. obj = buffer.pointer;
  359. if (obj && obj->type == ACPI_TYPE_BUFFER)
  360. *edid = obj;
  361. else {
  362. printk(KERN_ERR PREFIX "Invalid _DDC data\n");
  363. status = -EFAULT;
  364. kfree(obj);
  365. }
  366. return status;
  367. }
  368. /* bus */
  369. static int
  370. acpi_video_bus_set_POST(struct acpi_video_bus *video, unsigned long option)
  371. {
  372. int status;
  373. unsigned long tmp;
  374. union acpi_object arg0 = { ACPI_TYPE_INTEGER };
  375. struct acpi_object_list args = { 1, &arg0 };
  376. arg0.integer.value = option;
  377. status = acpi_evaluate_integer(video->device->handle, "_SPD", &args, &tmp);
  378. if (ACPI_SUCCESS(status))
  379. status = tmp ? (-EINVAL) : (AE_OK);
  380. return status;
  381. }
  382. static int
  383. acpi_video_bus_get_POST(struct acpi_video_bus *video, unsigned long *id)
  384. {
  385. int status;
  386. status = acpi_evaluate_integer(video->device->handle, "_GPD", NULL, id);
  387. return status;
  388. }
  389. static int
  390. acpi_video_bus_POST_options(struct acpi_video_bus *video,
  391. unsigned long *options)
  392. {
  393. int status;
  394. status = acpi_evaluate_integer(video->device->handle, "_VPO", NULL, options);
  395. *options &= 3;
  396. return status;
  397. }
  398. /*
  399. * Arg:
  400. * video : video bus device pointer
  401. * bios_flag :
  402. * 0. The system BIOS should NOT automatically switch(toggle)
  403. * the active display output.
  404. * 1. The system BIOS should automatically switch (toggle) the
  405. * active display output. No swich event.
  406. * 2. The _DGS value should be locked.
  407. * 3. The system BIOS should not automatically switch (toggle) the
  408. * active display output, but instead generate the display switch
  409. * event notify code.
  410. * lcd_flag :
  411. * 0. The system BIOS should automatically control the brightness level
  412. * of the LCD, when the power changes from AC to DC
  413. * 1. The system BIOS should NOT automatically control the brightness
  414. * level of the LCD, when the power changes from AC to DC.
  415. * Return Value:
  416. * -1 wrong arg.
  417. */
  418. static int
  419. acpi_video_bus_DOS(struct acpi_video_bus *video, int bios_flag, int lcd_flag)
  420. {
  421. acpi_integer status = 0;
  422. union acpi_object arg0 = { ACPI_TYPE_INTEGER };
  423. struct acpi_object_list args = { 1, &arg0 };
  424. if (bios_flag < 0 || bios_flag > 3 || lcd_flag < 0 || lcd_flag > 1) {
  425. status = -1;
  426. goto Failed;
  427. }
  428. arg0.integer.value = (lcd_flag << 2) | bios_flag;
  429. video->dos_setting = arg0.integer.value;
  430. acpi_evaluate_object(video->device->handle, "_DOS", &args, NULL);
  431. Failed:
  432. return status;
  433. }
  434. /*
  435. * Arg:
  436. * device : video output device (LCD, CRT, ..)
  437. *
  438. * Return Value:
  439. * None
  440. *
  441. * Find out all required AML method defined under the output
  442. * device.
  443. */
  444. static void acpi_video_device_find_cap(struct acpi_video_device *device)
  445. {
  446. acpi_integer status;
  447. acpi_handle h_dummy1;
  448. int i;
  449. u32 max_level = 0;
  450. union acpi_object *obj = NULL;
  451. struct acpi_video_device_brightness *br = NULL;
  452. memset(&device->cap, 0, 4);
  453. if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_ADR", &h_dummy1))) {
  454. device->cap._ADR = 1;
  455. }
  456. if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_BCL", &h_dummy1))) {
  457. device->cap._BCL = 1;
  458. }
  459. if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_BCM", &h_dummy1))) {
  460. device->cap._BCM = 1;
  461. }
  462. if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle,"_BQC",&h_dummy1)))
  463. device->cap._BQC = 1;
  464. if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DDC", &h_dummy1))) {
  465. device->cap._DDC = 1;
  466. }
  467. if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DCS", &h_dummy1))) {
  468. device->cap._DCS = 1;
  469. }
  470. if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DGS", &h_dummy1))) {
  471. device->cap._DGS = 1;
  472. }
  473. if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DSS", &h_dummy1))) {
  474. device->cap._DSS = 1;
  475. }
  476. status = acpi_video_device_lcd_query_levels(device, &obj);
  477. if (obj && obj->type == ACPI_TYPE_PACKAGE && obj->package.count >= 2) {
  478. int count = 0;
  479. union acpi_object *o;
  480. br = kzalloc(sizeof(*br), GFP_KERNEL);
  481. if (!br) {
  482. printk(KERN_ERR "can't allocate memory\n");
  483. } else {
  484. br->levels = kmalloc(obj->package.count *
  485. sizeof *(br->levels), GFP_KERNEL);
  486. if (!br->levels)
  487. goto out;
  488. for (i = 0; i < obj->package.count; i++) {
  489. o = (union acpi_object *)&obj->package.
  490. elements[i];
  491. if (o->type != ACPI_TYPE_INTEGER) {
  492. printk(KERN_ERR PREFIX "Invalid data\n");
  493. continue;
  494. }
  495. br->levels[count] = (u32) o->integer.value;
  496. if (br->levels[count] > max_level)
  497. max_level = br->levels[count];
  498. count++;
  499. }
  500. out:
  501. if (count < 2) {
  502. kfree(br->levels);
  503. kfree(br);
  504. } else {
  505. br->count = count;
  506. device->brightness = br;
  507. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  508. "found %d brightness levels\n",
  509. count));
  510. }
  511. }
  512. }
  513. kfree(obj);
  514. if (device->cap._BCL && device->cap._BCM && device->cap._BQC){
  515. unsigned long tmp;
  516. static int count = 0;
  517. char *name;
  518. struct backlight_properties *acpi_video_data;
  519. name = kzalloc(MAX_NAME_LEN, GFP_KERNEL);
  520. if (!name)
  521. return;
  522. acpi_video_data = kzalloc(
  523. sizeof(struct backlight_properties),
  524. GFP_KERNEL);
  525. if (!acpi_video_data){
  526. kfree(name);
  527. return;
  528. }
  529. acpi_video_data->owner = THIS_MODULE;
  530. acpi_video_data->get_brightness =
  531. acpi_video_get_brightness;
  532. acpi_video_data->update_status =
  533. acpi_video_set_brightness;
  534. sprintf(name, "acpi_video%d", count++);
  535. device->data = acpi_video_data;
  536. acpi_video_data->max_brightness = max_level;
  537. acpi_video_device_lcd_get_level_current(device, &tmp);
  538. acpi_video_data->brightness = (int)tmp;
  539. device->backlight = backlight_device_register(name,
  540. NULL, device, acpi_video_data);
  541. kfree(name);
  542. }
  543. return;
  544. }
  545. /*
  546. * Arg:
  547. * device : video output device (VGA)
  548. *
  549. * Return Value:
  550. * None
  551. *
  552. * Find out all required AML method defined under the video bus device.
  553. */
  554. static void acpi_video_bus_find_cap(struct acpi_video_bus *video)
  555. {
  556. acpi_handle h_dummy1;
  557. memset(&video->cap, 0, 4);
  558. if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_DOS", &h_dummy1))) {
  559. video->cap._DOS = 1;
  560. }
  561. if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_DOD", &h_dummy1))) {
  562. video->cap._DOD = 1;
  563. }
  564. if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_ROM", &h_dummy1))) {
  565. video->cap._ROM = 1;
  566. }
  567. if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_GPD", &h_dummy1))) {
  568. video->cap._GPD = 1;
  569. }
  570. if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_SPD", &h_dummy1))) {
  571. video->cap._SPD = 1;
  572. }
  573. if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_VPO", &h_dummy1))) {
  574. video->cap._VPO = 1;
  575. }
  576. }
  577. /*
  578. * Check whether the video bus device has required AML method to
  579. * support the desired features
  580. */
  581. static int acpi_video_bus_check(struct acpi_video_bus *video)
  582. {
  583. acpi_status status = -ENOENT;
  584. if (!video)
  585. return -EINVAL;
  586. /* Since there is no HID, CID and so on for VGA driver, we have
  587. * to check well known required nodes.
  588. */
  589. /* Does this device able to support video switching ? */
  590. if (video->cap._DOS) {
  591. video->flags.multihead = 1;
  592. status = 0;
  593. }
  594. /* Does this device able to retrieve a retrieve a video ROM ? */
  595. if (video->cap._ROM) {
  596. video->flags.rom = 1;
  597. status = 0;
  598. }
  599. /* Does this device able to configure which video device to POST ? */
  600. if (video->cap._GPD && video->cap._SPD && video->cap._VPO) {
  601. video->flags.post = 1;
  602. status = 0;
  603. }
  604. return status;
  605. }
  606. /* --------------------------------------------------------------------------
  607. FS Interface (/proc)
  608. -------------------------------------------------------------------------- */
  609. static struct proc_dir_entry *acpi_video_dir;
  610. /* video devices */
  611. static int acpi_video_device_info_seq_show(struct seq_file *seq, void *offset)
  612. {
  613. struct acpi_video_device *dev = seq->private;
  614. if (!dev)
  615. goto end;
  616. seq_printf(seq, "device_id: 0x%04x\n", (u32) dev->device_id);
  617. seq_printf(seq, "type: ");
  618. if (dev->flags.crt)
  619. seq_printf(seq, "CRT\n");
  620. else if (dev->flags.lcd)
  621. seq_printf(seq, "LCD\n");
  622. else if (dev->flags.tvout)
  623. seq_printf(seq, "TVOUT\n");
  624. else if (dev->flags.dvi)
  625. seq_printf(seq, "DVI\n");
  626. else
  627. seq_printf(seq, "UNKNOWN\n");
  628. seq_printf(seq, "known by bios: %s\n", dev->flags.bios ? "yes" : "no");
  629. end:
  630. return 0;
  631. }
  632. static int
  633. acpi_video_device_info_open_fs(struct inode *inode, struct file *file)
  634. {
  635. return single_open(file, acpi_video_device_info_seq_show,
  636. PDE(inode)->data);
  637. }
  638. static int acpi_video_device_state_seq_show(struct seq_file *seq, void *offset)
  639. {
  640. int status;
  641. struct acpi_video_device *dev = seq->private;
  642. unsigned long state;
  643. if (!dev)
  644. goto end;
  645. status = acpi_video_device_get_state(dev, &state);
  646. seq_printf(seq, "state: ");
  647. if (ACPI_SUCCESS(status))
  648. seq_printf(seq, "0x%02lx\n", state);
  649. else
  650. seq_printf(seq, "<not supported>\n");
  651. status = acpi_video_device_query(dev, &state);
  652. seq_printf(seq, "query: ");
  653. if (ACPI_SUCCESS(status))
  654. seq_printf(seq, "0x%02lx\n", state);
  655. else
  656. seq_printf(seq, "<not supported>\n");
  657. end:
  658. return 0;
  659. }
  660. static int
  661. acpi_video_device_state_open_fs(struct inode *inode, struct file *file)
  662. {
  663. return single_open(file, acpi_video_device_state_seq_show,
  664. PDE(inode)->data);
  665. }
  666. static ssize_t
  667. acpi_video_device_write_state(struct file *file,
  668. const char __user * buffer,
  669. size_t count, loff_t * data)
  670. {
  671. int status;
  672. struct seq_file *m = file->private_data;
  673. struct acpi_video_device *dev = m->private;
  674. char str[12] = { 0 };
  675. u32 state = 0;
  676. if (!dev || count + 1 > sizeof str)
  677. return -EINVAL;
  678. if (copy_from_user(str, buffer, count))
  679. return -EFAULT;
  680. str[count] = 0;
  681. state = simple_strtoul(str, NULL, 0);
  682. state &= ((1ul << 31) | (1ul << 30) | (1ul << 0));
  683. status = acpi_video_device_set_state(dev, state);
  684. if (status)
  685. return -EFAULT;
  686. return count;
  687. }
  688. static int
  689. acpi_video_device_brightness_seq_show(struct seq_file *seq, void *offset)
  690. {
  691. struct acpi_video_device *dev = seq->private;
  692. int i;
  693. if (!dev || !dev->brightness) {
  694. seq_printf(seq, "<not supported>\n");
  695. return 0;
  696. }
  697. seq_printf(seq, "levels: ");
  698. for (i = 0; i < dev->brightness->count; i++)
  699. seq_printf(seq, " %d", dev->brightness->levels[i]);
  700. seq_printf(seq, "\ncurrent: %d\n", dev->brightness->curr);
  701. return 0;
  702. }
  703. static int
  704. acpi_video_device_brightness_open_fs(struct inode *inode, struct file *file)
  705. {
  706. return single_open(file, acpi_video_device_brightness_seq_show,
  707. PDE(inode)->data);
  708. }
  709. static ssize_t
  710. acpi_video_device_write_brightness(struct file *file,
  711. const char __user * buffer,
  712. size_t count, loff_t * data)
  713. {
  714. struct seq_file *m = file->private_data;
  715. struct acpi_video_device *dev = m->private;
  716. char str[4] = { 0 };
  717. unsigned int level = 0;
  718. int i;
  719. if (!dev || !dev->brightness || count + 1 > sizeof str)
  720. return -EINVAL;
  721. if (copy_from_user(str, buffer, count))
  722. return -EFAULT;
  723. str[count] = 0;
  724. level = simple_strtoul(str, NULL, 0);
  725. if (level > 100)
  726. return -EFAULT;
  727. /* validate though the list of available levels */
  728. for (i = 0; i < dev->brightness->count; i++)
  729. if (level == dev->brightness->levels[i]) {
  730. if (ACPI_SUCCESS
  731. (acpi_video_device_lcd_set_level(dev, level)))
  732. dev->brightness->curr = level;
  733. break;
  734. }
  735. return count;
  736. }
  737. static int acpi_video_device_EDID_seq_show(struct seq_file *seq, void *offset)
  738. {
  739. struct acpi_video_device *dev = seq->private;
  740. int status;
  741. int i;
  742. union acpi_object *edid = NULL;
  743. if (!dev)
  744. goto out;
  745. status = acpi_video_device_EDID(dev, &edid, 128);
  746. if (ACPI_FAILURE(status)) {
  747. status = acpi_video_device_EDID(dev, &edid, 256);
  748. }
  749. if (ACPI_FAILURE(status)) {
  750. goto out;
  751. }
  752. if (edid && edid->type == ACPI_TYPE_BUFFER) {
  753. for (i = 0; i < edid->buffer.length; i++)
  754. seq_putc(seq, edid->buffer.pointer[i]);
  755. }
  756. out:
  757. if (!edid)
  758. seq_printf(seq, "<not supported>\n");
  759. else
  760. kfree(edid);
  761. return 0;
  762. }
  763. static int
  764. acpi_video_device_EDID_open_fs(struct inode *inode, struct file *file)
  765. {
  766. return single_open(file, acpi_video_device_EDID_seq_show,
  767. PDE(inode)->data);
  768. }
  769. static int acpi_video_device_add_fs(struct acpi_device *device)
  770. {
  771. struct proc_dir_entry *entry = NULL;
  772. struct acpi_video_device *vid_dev;
  773. if (!device)
  774. return -ENODEV;
  775. vid_dev = acpi_driver_data(device);
  776. if (!vid_dev)
  777. return -ENODEV;
  778. if (!acpi_device_dir(device)) {
  779. acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
  780. vid_dev->video->dir);
  781. if (!acpi_device_dir(device))
  782. return -ENODEV;
  783. acpi_device_dir(device)->owner = THIS_MODULE;
  784. }
  785. /* 'info' [R] */
  786. entry = create_proc_entry("info", S_IRUGO, acpi_device_dir(device));
  787. if (!entry)
  788. return -ENODEV;
  789. else {
  790. entry->proc_fops = &acpi_video_device_info_fops;
  791. entry->data = acpi_driver_data(device);
  792. entry->owner = THIS_MODULE;
  793. }
  794. /* 'state' [R/W] */
  795. entry =
  796. create_proc_entry("state", S_IFREG | S_IRUGO | S_IWUSR,
  797. acpi_device_dir(device));
  798. if (!entry)
  799. return -ENODEV;
  800. else {
  801. acpi_video_device_state_fops.write = acpi_video_device_write_state;
  802. entry->proc_fops = &acpi_video_device_state_fops;
  803. entry->data = acpi_driver_data(device);
  804. entry->owner = THIS_MODULE;
  805. }
  806. /* 'brightness' [R/W] */
  807. entry =
  808. create_proc_entry("brightness", S_IFREG | S_IRUGO | S_IWUSR,
  809. acpi_device_dir(device));
  810. if (!entry)
  811. return -ENODEV;
  812. else {
  813. acpi_video_device_brightness_fops.write = acpi_video_device_write_brightness;
  814. entry->proc_fops = &acpi_video_device_brightness_fops;
  815. entry->data = acpi_driver_data(device);
  816. entry->owner = THIS_MODULE;
  817. }
  818. /* 'EDID' [R] */
  819. entry = create_proc_entry("EDID", S_IRUGO, acpi_device_dir(device));
  820. if (!entry)
  821. return -ENODEV;
  822. else {
  823. entry->proc_fops = &acpi_video_device_EDID_fops;
  824. entry->data = acpi_driver_data(device);
  825. entry->owner = THIS_MODULE;
  826. }
  827. return 0;
  828. }
  829. static int acpi_video_device_remove_fs(struct acpi_device *device)
  830. {
  831. struct acpi_video_device *vid_dev;
  832. vid_dev = acpi_driver_data(device);
  833. if (!vid_dev || !vid_dev->video || !vid_dev->video->dir)
  834. return -ENODEV;
  835. if (acpi_device_dir(device)) {
  836. remove_proc_entry("info", acpi_device_dir(device));
  837. remove_proc_entry("state", acpi_device_dir(device));
  838. remove_proc_entry("brightness", acpi_device_dir(device));
  839. remove_proc_entry("EDID", acpi_device_dir(device));
  840. remove_proc_entry(acpi_device_bid(device), vid_dev->video->dir);
  841. acpi_device_dir(device) = NULL;
  842. }
  843. return 0;
  844. }
  845. /* video bus */
  846. static int acpi_video_bus_info_seq_show(struct seq_file *seq, void *offset)
  847. {
  848. struct acpi_video_bus *video = seq->private;
  849. if (!video)
  850. goto end;
  851. seq_printf(seq, "Switching heads: %s\n",
  852. video->flags.multihead ? "yes" : "no");
  853. seq_printf(seq, "Video ROM: %s\n",
  854. video->flags.rom ? "yes" : "no");
  855. seq_printf(seq, "Device to be POSTed on boot: %s\n",
  856. video->flags.post ? "yes" : "no");
  857. end:
  858. return 0;
  859. }
  860. static int acpi_video_bus_info_open_fs(struct inode *inode, struct file *file)
  861. {
  862. return single_open(file, acpi_video_bus_info_seq_show,
  863. PDE(inode)->data);
  864. }
  865. static int acpi_video_bus_ROM_seq_show(struct seq_file *seq, void *offset)
  866. {
  867. struct acpi_video_bus *video = seq->private;
  868. if (!video)
  869. goto end;
  870. printk(KERN_INFO PREFIX "Please implement %s\n", __FUNCTION__);
  871. seq_printf(seq, "<TODO>\n");
  872. end:
  873. return 0;
  874. }
  875. static int acpi_video_bus_ROM_open_fs(struct inode *inode, struct file *file)
  876. {
  877. return single_open(file, acpi_video_bus_ROM_seq_show, PDE(inode)->data);
  878. }
  879. static int acpi_video_bus_POST_info_seq_show(struct seq_file *seq, void *offset)
  880. {
  881. struct acpi_video_bus *video = seq->private;
  882. unsigned long options;
  883. int status;
  884. if (!video)
  885. goto end;
  886. status = acpi_video_bus_POST_options(video, &options);
  887. if (ACPI_SUCCESS(status)) {
  888. if (!(options & 1)) {
  889. printk(KERN_WARNING PREFIX
  890. "The motherboard VGA device is not listed as a possible POST device.\n");
  891. printk(KERN_WARNING PREFIX
  892. "This indicate a BIOS bug. Please contact the manufacturer.\n");
  893. }
  894. printk("%lx\n", options);
  895. seq_printf(seq, "can POST: <intgrated video>");
  896. if (options & 2)
  897. seq_printf(seq, " <PCI video>");
  898. if (options & 4)
  899. seq_printf(seq, " <AGP video>");
  900. seq_putc(seq, '\n');
  901. } else
  902. seq_printf(seq, "<not supported>\n");
  903. end:
  904. return 0;
  905. }
  906. static int
  907. acpi_video_bus_POST_info_open_fs(struct inode *inode, struct file *file)
  908. {
  909. return single_open(file, acpi_video_bus_POST_info_seq_show,
  910. PDE(inode)->data);
  911. }
  912. static int acpi_video_bus_POST_seq_show(struct seq_file *seq, void *offset)
  913. {
  914. struct acpi_video_bus *video = seq->private;
  915. int status;
  916. unsigned long id;
  917. if (!video)
  918. goto end;
  919. status = acpi_video_bus_get_POST(video, &id);
  920. if (!ACPI_SUCCESS(status)) {
  921. seq_printf(seq, "<not supported>\n");
  922. goto end;
  923. }
  924. seq_printf(seq, "device posted is <%s>\n", device_decode[id & 3]);
  925. end:
  926. return 0;
  927. }
  928. static int acpi_video_bus_DOS_seq_show(struct seq_file *seq, void *offset)
  929. {
  930. struct acpi_video_bus *video = seq->private;
  931. seq_printf(seq, "DOS setting: <%d>\n", video->dos_setting);
  932. return 0;
  933. }
  934. static int acpi_video_bus_POST_open_fs(struct inode *inode, struct file *file)
  935. {
  936. return single_open(file, acpi_video_bus_POST_seq_show,
  937. PDE(inode)->data);
  938. }
  939. static int acpi_video_bus_DOS_open_fs(struct inode *inode, struct file *file)
  940. {
  941. return single_open(file, acpi_video_bus_DOS_seq_show, PDE(inode)->data);
  942. }
  943. static ssize_t
  944. acpi_video_bus_write_POST(struct file *file,
  945. const char __user * buffer,
  946. size_t count, loff_t * data)
  947. {
  948. int status;
  949. struct seq_file *m = file->private_data;
  950. struct acpi_video_bus *video = m->private;
  951. char str[12] = { 0 };
  952. unsigned long opt, options;
  953. if (!video || count + 1 > sizeof str)
  954. return -EINVAL;
  955. status = acpi_video_bus_POST_options(video, &options);
  956. if (!ACPI_SUCCESS(status))
  957. return -EINVAL;
  958. if (copy_from_user(str, buffer, count))
  959. return -EFAULT;
  960. str[count] = 0;
  961. opt = strtoul(str, NULL, 0);
  962. if (opt > 3)
  963. return -EFAULT;
  964. /* just in case an OEM 'forget' the motherboard... */
  965. options |= 1;
  966. if (options & (1ul << opt)) {
  967. status = acpi_video_bus_set_POST(video, opt);
  968. if (!ACPI_SUCCESS(status))
  969. return -EFAULT;
  970. }
  971. return count;
  972. }
  973. static ssize_t
  974. acpi_video_bus_write_DOS(struct file *file,
  975. const char __user * buffer,
  976. size_t count, loff_t * data)
  977. {
  978. int status;
  979. struct seq_file *m = file->private_data;
  980. struct acpi_video_bus *video = m->private;
  981. char str[12] = { 0 };
  982. unsigned long opt;
  983. if (!video || count + 1 > sizeof str)
  984. return -EINVAL;
  985. if (copy_from_user(str, buffer, count))
  986. return -EFAULT;
  987. str[count] = 0;
  988. opt = strtoul(str, NULL, 0);
  989. if (opt > 7)
  990. return -EFAULT;
  991. status = acpi_video_bus_DOS(video, opt & 0x3, (opt & 0x4) >> 2);
  992. if (!ACPI_SUCCESS(status))
  993. return -EFAULT;
  994. return count;
  995. }
  996. static int acpi_video_bus_add_fs(struct acpi_device *device)
  997. {
  998. struct proc_dir_entry *entry = NULL;
  999. struct acpi_video_bus *video;
  1000. video = acpi_driver_data(device);
  1001. if (!acpi_device_dir(device)) {
  1002. acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
  1003. acpi_video_dir);
  1004. if (!acpi_device_dir(device))
  1005. return -ENODEV;
  1006. video->dir = acpi_device_dir(device);
  1007. acpi_device_dir(device)->owner = THIS_MODULE;
  1008. }
  1009. /* 'info' [R] */
  1010. entry = create_proc_entry("info", S_IRUGO, acpi_device_dir(device));
  1011. if (!entry)
  1012. return -ENODEV;
  1013. else {
  1014. entry->proc_fops = &acpi_video_bus_info_fops;
  1015. entry->data = acpi_driver_data(device);
  1016. entry->owner = THIS_MODULE;
  1017. }
  1018. /* 'ROM' [R] */
  1019. entry = create_proc_entry("ROM", S_IRUGO, acpi_device_dir(device));
  1020. if (!entry)
  1021. return -ENODEV;
  1022. else {
  1023. entry->proc_fops = &acpi_video_bus_ROM_fops;
  1024. entry->data = acpi_driver_data(device);
  1025. entry->owner = THIS_MODULE;
  1026. }
  1027. /* 'POST_info' [R] */
  1028. entry =
  1029. create_proc_entry("POST_info", S_IRUGO, acpi_device_dir(device));
  1030. if (!entry)
  1031. return -ENODEV;
  1032. else {
  1033. entry->proc_fops = &acpi_video_bus_POST_info_fops;
  1034. entry->data = acpi_driver_data(device);
  1035. entry->owner = THIS_MODULE;
  1036. }
  1037. /* 'POST' [R/W] */
  1038. entry =
  1039. create_proc_entry("POST", S_IFREG | S_IRUGO | S_IRUSR,
  1040. acpi_device_dir(device));
  1041. if (!entry)
  1042. return -ENODEV;
  1043. else {
  1044. acpi_video_bus_POST_fops.write = acpi_video_bus_write_POST;
  1045. entry->proc_fops = &acpi_video_bus_POST_fops;
  1046. entry->data = acpi_driver_data(device);
  1047. entry->owner = THIS_MODULE;
  1048. }
  1049. /* 'DOS' [R/W] */
  1050. entry =
  1051. create_proc_entry("DOS", S_IFREG | S_IRUGO | S_IRUSR,
  1052. acpi_device_dir(device));
  1053. if (!entry)
  1054. return -ENODEV;
  1055. else {
  1056. acpi_video_bus_DOS_fops.write = acpi_video_bus_write_DOS;
  1057. entry->proc_fops = &acpi_video_bus_DOS_fops;
  1058. entry->data = acpi_driver_data(device);
  1059. entry->owner = THIS_MODULE;
  1060. }
  1061. return 0;
  1062. }
  1063. static int acpi_video_bus_remove_fs(struct acpi_device *device)
  1064. {
  1065. struct acpi_video_bus *video;
  1066. video = acpi_driver_data(device);
  1067. if (acpi_device_dir(device)) {
  1068. remove_proc_entry("info", acpi_device_dir(device));
  1069. remove_proc_entry("ROM", acpi_device_dir(device));
  1070. remove_proc_entry("POST_info", acpi_device_dir(device));
  1071. remove_proc_entry("POST", acpi_device_dir(device));
  1072. remove_proc_entry("DOS", acpi_device_dir(device));
  1073. remove_proc_entry(acpi_device_bid(device), acpi_video_dir);
  1074. acpi_device_dir(device) = NULL;
  1075. }
  1076. return 0;
  1077. }
  1078. /* --------------------------------------------------------------------------
  1079. Driver Interface
  1080. -------------------------------------------------------------------------- */
  1081. /* device interface */
  1082. static struct acpi_video_device_attrib*
  1083. acpi_video_get_device_attr(struct acpi_video_bus *video, unsigned long device_id)
  1084. {
  1085. int count;
  1086. for(count = 0; count < video->attached_count; count++)
  1087. if((video->attached_array[count].value.int_val & 0xffff) == device_id)
  1088. return &(video->attached_array[count].value.attrib);
  1089. return NULL;
  1090. }
  1091. static int
  1092. acpi_video_bus_get_one_device(struct acpi_device *device,
  1093. struct acpi_video_bus *video)
  1094. {
  1095. unsigned long device_id;
  1096. int status;
  1097. struct acpi_video_device *data;
  1098. struct acpi_video_device_attrib* attribute;
  1099. if (!device || !video)
  1100. return -EINVAL;
  1101. status =
  1102. acpi_evaluate_integer(device->handle, "_ADR", NULL, &device_id);
  1103. if (ACPI_SUCCESS(status)) {
  1104. data = kzalloc(sizeof(struct acpi_video_device), GFP_KERNEL);
  1105. if (!data)
  1106. return -ENOMEM;
  1107. strcpy(acpi_device_name(device), ACPI_VIDEO_DEVICE_NAME);
  1108. strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
  1109. acpi_driver_data(device) = data;
  1110. data->device_id = device_id;
  1111. data->video = video;
  1112. data->dev = device;
  1113. attribute = acpi_video_get_device_attr(video, device_id);
  1114. if((attribute != NULL) && attribute->device_id_scheme) {
  1115. switch (attribute->display_type) {
  1116. case ACPI_VIDEO_DISPLAY_CRT:
  1117. data->flags.crt = 1;
  1118. break;
  1119. case ACPI_VIDEO_DISPLAY_TV:
  1120. data->flags.tvout = 1;
  1121. break;
  1122. case ACPI_VIDEO_DISPLAY_DVI:
  1123. data->flags.dvi = 1;
  1124. break;
  1125. case ACPI_VIDEO_DISPLAY_LCD:
  1126. data->flags.lcd = 1;
  1127. break;
  1128. default:
  1129. data->flags.unknown = 1;
  1130. break;
  1131. }
  1132. if(attribute->bios_can_detect)
  1133. data->flags.bios = 1;
  1134. } else
  1135. data->flags.unknown = 1;
  1136. acpi_video_device_bind(video, data);
  1137. acpi_video_device_find_cap(data);
  1138. status = acpi_install_notify_handler(device->handle,
  1139. ACPI_DEVICE_NOTIFY,
  1140. acpi_video_device_notify,
  1141. data);
  1142. if (ACPI_FAILURE(status)) {
  1143. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  1144. "Error installing notify handler\n"));
  1145. if(data->brightness)
  1146. kfree(data->brightness->levels);
  1147. kfree(data->brightness);
  1148. kfree(data);
  1149. return -ENODEV;
  1150. }
  1151. down(&video->sem);
  1152. list_add_tail(&data->entry, &video->video_device_list);
  1153. up(&video->sem);
  1154. acpi_video_device_add_fs(device);
  1155. return 0;
  1156. }
  1157. return -ENOENT;
  1158. }
  1159. /*
  1160. * Arg:
  1161. * video : video bus device
  1162. *
  1163. * Return:
  1164. * none
  1165. *
  1166. * Enumerate the video device list of the video bus,
  1167. * bind the ids with the corresponding video devices
  1168. * under the video bus.
  1169. */
  1170. static void acpi_video_device_rebind(struct acpi_video_bus *video)
  1171. {
  1172. struct list_head *node, *next;
  1173. list_for_each_safe(node, next, &video->video_device_list) {
  1174. struct acpi_video_device *dev =
  1175. container_of(node, struct acpi_video_device, entry);
  1176. acpi_video_device_bind(video, dev);
  1177. }
  1178. }
  1179. /*
  1180. * Arg:
  1181. * video : video bus device
  1182. * device : video output device under the video
  1183. * bus
  1184. *
  1185. * Return:
  1186. * none
  1187. *
  1188. * Bind the ids with the corresponding video devices
  1189. * under the video bus.
  1190. */
  1191. static void
  1192. acpi_video_device_bind(struct acpi_video_bus *video,
  1193. struct acpi_video_device *device)
  1194. {
  1195. int i;
  1196. #define IDS_VAL(i) video->attached_array[i].value.int_val
  1197. #define IDS_BIND(i) video->attached_array[i].bind_info
  1198. for (i = 0; IDS_VAL(i) != ACPI_VIDEO_HEAD_INVALID &&
  1199. i < video->attached_count; i++) {
  1200. if (device->device_id == (IDS_VAL(i) & 0xffff)) {
  1201. IDS_BIND(i) = device;
  1202. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "device_bind %d\n", i));
  1203. }
  1204. }
  1205. #undef IDS_VAL
  1206. #undef IDS_BIND
  1207. }
  1208. /*
  1209. * Arg:
  1210. * video : video bus device
  1211. *
  1212. * Return:
  1213. * < 0 : error
  1214. *
  1215. * Call _DOD to enumerate all devices attached to display adapter
  1216. *
  1217. */
  1218. static int acpi_video_device_enumerate(struct acpi_video_bus *video)
  1219. {
  1220. int status;
  1221. int count;
  1222. int i;
  1223. struct acpi_video_enumerated_device *active_device_list;
  1224. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  1225. union acpi_object *dod = NULL;
  1226. union acpi_object *obj;
  1227. status = acpi_evaluate_object(video->device->handle, "_DOD", NULL, &buffer);
  1228. if (!ACPI_SUCCESS(status)) {
  1229. ACPI_EXCEPTION((AE_INFO, status, "Evaluating _DOD"));
  1230. return status;
  1231. }
  1232. dod = buffer.pointer;
  1233. if (!dod || (dod->type != ACPI_TYPE_PACKAGE)) {
  1234. ACPI_EXCEPTION((AE_INFO, status, "Invalid _DOD data"));
  1235. status = -EFAULT;
  1236. goto out;
  1237. }
  1238. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d video heads in _DOD\n",
  1239. dod->package.count));
  1240. active_device_list = kmalloc((1 +
  1241. dod->package.count) *
  1242. sizeof(struct
  1243. acpi_video_enumerated_device),
  1244. GFP_KERNEL);
  1245. if (!active_device_list) {
  1246. status = -ENOMEM;
  1247. goto out;
  1248. }
  1249. count = 0;
  1250. for (i = 0; i < dod->package.count; i++) {
  1251. obj = &dod->package.elements[i];
  1252. if (obj->type != ACPI_TYPE_INTEGER) {
  1253. printk(KERN_ERR PREFIX "Invalid _DOD data\n");
  1254. active_device_list[i].value.int_val =
  1255. ACPI_VIDEO_HEAD_INVALID;
  1256. }
  1257. active_device_list[i].value.int_val = obj->integer.value;
  1258. active_device_list[i].bind_info = NULL;
  1259. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "dod element[%d] = %d\n", i,
  1260. (int)obj->integer.value));
  1261. count++;
  1262. }
  1263. active_device_list[count].value.int_val = ACPI_VIDEO_HEAD_END;
  1264. kfree(video->attached_array);
  1265. video->attached_array = active_device_list;
  1266. video->attached_count = count;
  1267. out:
  1268. kfree(buffer.pointer);
  1269. return status;
  1270. }
  1271. /*
  1272. * Arg:
  1273. * video : video bus device
  1274. * event : Nontify Event
  1275. *
  1276. * Return:
  1277. * < 0 : error
  1278. *
  1279. * 1. Find out the current active output device.
  1280. * 2. Identify the next output device to switch
  1281. * 3. call _DSS to do actual switch.
  1282. */
  1283. static int acpi_video_switch_output(struct acpi_video_bus *video, int event)
  1284. {
  1285. struct list_head *node, *next;
  1286. struct acpi_video_device *dev = NULL;
  1287. struct acpi_video_device *dev_next = NULL;
  1288. struct acpi_video_device *dev_prev = NULL;
  1289. unsigned long state;
  1290. int status = 0;
  1291. list_for_each_safe(node, next, &video->video_device_list) {
  1292. dev = container_of(node, struct acpi_video_device, entry);
  1293. status = acpi_video_device_get_state(dev, &state);
  1294. if (state & 0x2) {
  1295. dev_next =
  1296. container_of(node->next, struct acpi_video_device,
  1297. entry);
  1298. dev_prev =
  1299. container_of(node->prev, struct acpi_video_device,
  1300. entry);
  1301. goto out;
  1302. }
  1303. }
  1304. dev_next = container_of(node->next, struct acpi_video_device, entry);
  1305. dev_prev = container_of(node->prev, struct acpi_video_device, entry);
  1306. out:
  1307. switch (event) {
  1308. case ACPI_VIDEO_NOTIFY_CYCLE:
  1309. case ACPI_VIDEO_NOTIFY_NEXT_OUTPUT:
  1310. acpi_video_device_set_state(dev, 0);
  1311. acpi_video_device_set_state(dev_next, 0x80000001);
  1312. break;
  1313. case ACPI_VIDEO_NOTIFY_PREV_OUTPUT:
  1314. acpi_video_device_set_state(dev, 0);
  1315. acpi_video_device_set_state(dev_prev, 0x80000001);
  1316. default:
  1317. break;
  1318. }
  1319. return status;
  1320. }
  1321. static int
  1322. acpi_video_get_next_level(struct acpi_video_device *device,
  1323. u32 level_current, u32 event)
  1324. {
  1325. int min, max, min_above, max_below, i, l;
  1326. max = max_below = 0;
  1327. min = min_above = 255;
  1328. for (i = 0; i < device->brightness->count; i++) {
  1329. l = device->brightness->levels[i];
  1330. if (l < min)
  1331. min = l;
  1332. if (l > max)
  1333. max = l;
  1334. if (l < min_above && l > level_current)
  1335. min_above = l;
  1336. if (l > max_below && l < level_current)
  1337. max_below = l;
  1338. }
  1339. switch (event) {
  1340. case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS:
  1341. return (level_current < max) ? min_above : min;
  1342. case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS:
  1343. return (level_current < max) ? min_above : max;
  1344. case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS:
  1345. return (level_current > min) ? max_below : min;
  1346. case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS:
  1347. case ACPI_VIDEO_NOTIFY_DISPLAY_OFF:
  1348. return 0;
  1349. default:
  1350. return level_current;
  1351. }
  1352. }
  1353. static void
  1354. acpi_video_switch_brightness(struct acpi_video_device *device, int event)
  1355. {
  1356. unsigned long level_current, level_next;
  1357. acpi_video_device_lcd_get_level_current(device, &level_current);
  1358. level_next = acpi_video_get_next_level(device, level_current, event);
  1359. acpi_video_device_lcd_set_level(device, level_next);
  1360. }
  1361. static int
  1362. acpi_video_bus_get_devices(struct acpi_video_bus *video,
  1363. struct acpi_device *device)
  1364. {
  1365. int status = 0;
  1366. struct list_head *node, *next;
  1367. acpi_video_device_enumerate(video);
  1368. list_for_each_safe(node, next, &device->children) {
  1369. struct acpi_device *dev =
  1370. list_entry(node, struct acpi_device, node);
  1371. if (!dev)
  1372. continue;
  1373. status = acpi_video_bus_get_one_device(dev, video);
  1374. if (ACPI_FAILURE(status)) {
  1375. ACPI_EXCEPTION((AE_INFO, status, "Cant attach device"));
  1376. continue;
  1377. }
  1378. }
  1379. return status;
  1380. }
  1381. static int acpi_video_bus_put_one_device(struct acpi_video_device *device)
  1382. {
  1383. acpi_status status;
  1384. struct acpi_video_bus *video;
  1385. if (!device || !device->video)
  1386. return -ENOENT;
  1387. video = device->video;
  1388. down(&video->sem);
  1389. list_del(&device->entry);
  1390. up(&video->sem);
  1391. acpi_video_device_remove_fs(device->dev);
  1392. status = acpi_remove_notify_handler(device->dev->handle,
  1393. ACPI_DEVICE_NOTIFY,
  1394. acpi_video_device_notify);
  1395. if (device->backlight){
  1396. backlight_device_unregister(device->backlight);
  1397. kfree(device->data);
  1398. }
  1399. return 0;
  1400. }
  1401. static int acpi_video_bus_put_devices(struct acpi_video_bus *video)
  1402. {
  1403. int status;
  1404. struct list_head *node, *next;
  1405. list_for_each_safe(node, next, &video->video_device_list) {
  1406. struct acpi_video_device *data =
  1407. list_entry(node, struct acpi_video_device, entry);
  1408. if (!data)
  1409. continue;
  1410. status = acpi_video_bus_put_one_device(data);
  1411. if (ACPI_FAILURE(status))
  1412. printk(KERN_WARNING PREFIX
  1413. "hhuuhhuu bug in acpi video driver.\n");
  1414. if (data->brightness)
  1415. kfree(data->brightness->levels);
  1416. kfree(data->brightness);
  1417. kfree(data);
  1418. }
  1419. return 0;
  1420. }
  1421. /* acpi_video interface */
  1422. static int acpi_video_bus_start_devices(struct acpi_video_bus *video)
  1423. {
  1424. return acpi_video_bus_DOS(video, 1, 0);
  1425. }
  1426. static int acpi_video_bus_stop_devices(struct acpi_video_bus *video)
  1427. {
  1428. return acpi_video_bus_DOS(video, 0, 1);
  1429. }
  1430. static void acpi_video_bus_notify(acpi_handle handle, u32 event, void *data)
  1431. {
  1432. struct acpi_video_bus *video = data;
  1433. struct acpi_device *device = NULL;
  1434. printk("video bus notify\n");
  1435. if (!video)
  1436. return;
  1437. device = video->device;
  1438. switch (event) {
  1439. case ACPI_VIDEO_NOTIFY_SWITCH: /* User request that a switch occur,
  1440. * most likely via hotkey. */
  1441. acpi_bus_generate_event(device, event, 0);
  1442. break;
  1443. case ACPI_VIDEO_NOTIFY_PROBE: /* User plug or remove a video
  1444. * connector. */
  1445. acpi_video_device_enumerate(video);
  1446. acpi_video_device_rebind(video);
  1447. acpi_video_switch_output(video, event);
  1448. acpi_bus_generate_event(device, event, 0);
  1449. break;
  1450. case ACPI_VIDEO_NOTIFY_CYCLE: /* Cycle Display output hotkey pressed. */
  1451. case ACPI_VIDEO_NOTIFY_NEXT_OUTPUT: /* Next Display output hotkey pressed. */
  1452. case ACPI_VIDEO_NOTIFY_PREV_OUTPUT: /* previous Display output hotkey pressed. */
  1453. acpi_video_switch_output(video, event);
  1454. acpi_bus_generate_event(device, event, 0);
  1455. break;
  1456. default:
  1457. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  1458. "Unsupported event [0x%x]\n", event));
  1459. break;
  1460. }
  1461. return;
  1462. }
  1463. static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data)
  1464. {
  1465. struct acpi_video_device *video_device = data;
  1466. struct acpi_device *device = NULL;
  1467. if (!video_device)
  1468. return;
  1469. device = video_device->dev;
  1470. switch (event) {
  1471. case ACPI_VIDEO_NOTIFY_SWITCH: /* change in status (cycle output device) */
  1472. case ACPI_VIDEO_NOTIFY_PROBE: /* change in status (output device status) */
  1473. acpi_bus_generate_event(device, event, 0);
  1474. break;
  1475. case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS: /* Cycle brightness */
  1476. case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS: /* Increase brightness */
  1477. case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS: /* Decrease brightness */
  1478. case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS: /* zero brightnesss */
  1479. case ACPI_VIDEO_NOTIFY_DISPLAY_OFF: /* display device off */
  1480. acpi_video_switch_brightness(video_device, event);
  1481. acpi_bus_generate_event(device, event, 0);
  1482. break;
  1483. default:
  1484. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  1485. "Unsupported event [0x%x]\n", event));
  1486. break;
  1487. }
  1488. return;
  1489. }
  1490. static int acpi_video_bus_add(struct acpi_device *device)
  1491. {
  1492. int result = 0;
  1493. acpi_status status = 0;
  1494. struct acpi_video_bus *video = NULL;
  1495. if (!device)
  1496. return -EINVAL;
  1497. video = kzalloc(sizeof(struct acpi_video_bus), GFP_KERNEL);
  1498. if (!video)
  1499. return -ENOMEM;
  1500. video->device = device;
  1501. strcpy(acpi_device_name(device), ACPI_VIDEO_BUS_NAME);
  1502. strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
  1503. acpi_driver_data(device) = video;
  1504. acpi_video_bus_find_cap(video);
  1505. result = acpi_video_bus_check(video);
  1506. if (result)
  1507. goto end;
  1508. result = acpi_video_bus_add_fs(device);
  1509. if (result)
  1510. goto end;
  1511. init_MUTEX(&video->sem);
  1512. INIT_LIST_HEAD(&video->video_device_list);
  1513. acpi_video_bus_get_devices(video, device);
  1514. acpi_video_bus_start_devices(video);
  1515. status = acpi_install_notify_handler(device->handle,
  1516. ACPI_DEVICE_NOTIFY,
  1517. acpi_video_bus_notify, video);
  1518. if (ACPI_FAILURE(status)) {
  1519. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  1520. "Error installing notify handler\n"));
  1521. acpi_video_bus_stop_devices(video);
  1522. acpi_video_bus_put_devices(video);
  1523. kfree(video->attached_array);
  1524. acpi_video_bus_remove_fs(device);
  1525. result = -ENODEV;
  1526. goto end;
  1527. }
  1528. printk(KERN_INFO PREFIX "%s [%s] (multi-head: %s rom: %s post: %s)\n",
  1529. ACPI_VIDEO_DEVICE_NAME, acpi_device_bid(device),
  1530. video->flags.multihead ? "yes" : "no",
  1531. video->flags.rom ? "yes" : "no",
  1532. video->flags.post ? "yes" : "no");
  1533. end:
  1534. if (result)
  1535. kfree(video);
  1536. return result;
  1537. }
  1538. static int acpi_video_bus_remove(struct acpi_device *device, int type)
  1539. {
  1540. acpi_status status = 0;
  1541. struct acpi_video_bus *video = NULL;
  1542. if (!device || !acpi_driver_data(device))
  1543. return -EINVAL;
  1544. video = acpi_driver_data(device);
  1545. acpi_video_bus_stop_devices(video);
  1546. status = acpi_remove_notify_handler(video->device->handle,
  1547. ACPI_DEVICE_NOTIFY,
  1548. acpi_video_bus_notify);
  1549. acpi_video_bus_put_devices(video);
  1550. acpi_video_bus_remove_fs(device);
  1551. kfree(video->attached_array);
  1552. kfree(video);
  1553. return 0;
  1554. }
  1555. static int
  1556. acpi_video_bus_match(struct acpi_device *device, struct acpi_driver *driver)
  1557. {
  1558. acpi_handle h_dummy1;
  1559. acpi_handle h_dummy2;
  1560. acpi_handle h_dummy3;
  1561. if (!device || !driver)
  1562. return -EINVAL;
  1563. /* Since there is no HID, CID for ACPI Video drivers, we have
  1564. * to check well known required nodes for each feature we support.
  1565. */
  1566. /* Does this device able to support video switching ? */
  1567. if (ACPI_SUCCESS(acpi_get_handle(device->handle, "_DOD", &h_dummy1)) &&
  1568. ACPI_SUCCESS(acpi_get_handle(device->handle, "_DOS", &h_dummy2)))
  1569. return 0;
  1570. /* Does this device able to retrieve a video ROM ? */
  1571. if (ACPI_SUCCESS(acpi_get_handle(device->handle, "_ROM", &h_dummy1)))
  1572. return 0;
  1573. /* Does this device able to configure which video head to be POSTed ? */
  1574. if (ACPI_SUCCESS(acpi_get_handle(device->handle, "_VPO", &h_dummy1)) &&
  1575. ACPI_SUCCESS(acpi_get_handle(device->handle, "_GPD", &h_dummy2)) &&
  1576. ACPI_SUCCESS(acpi_get_handle(device->handle, "_SPD", &h_dummy3)))
  1577. return 0;
  1578. return -ENODEV;
  1579. }
  1580. static int __init acpi_video_init(void)
  1581. {
  1582. int result = 0;
  1583. /*
  1584. acpi_dbg_level = 0xFFFFFFFF;
  1585. acpi_dbg_layer = 0x08000000;
  1586. */
  1587. acpi_video_dir = proc_mkdir(ACPI_VIDEO_CLASS, acpi_root_dir);
  1588. if (!acpi_video_dir)
  1589. return -ENODEV;
  1590. acpi_video_dir->owner = THIS_MODULE;
  1591. result = acpi_bus_register_driver(&acpi_video_bus);
  1592. if (result < 0) {
  1593. remove_proc_entry(ACPI_VIDEO_CLASS, acpi_root_dir);
  1594. return -ENODEV;
  1595. }
  1596. return 0;
  1597. }
  1598. static void __exit acpi_video_exit(void)
  1599. {
  1600. acpi_bus_unregister_driver(&acpi_video_bus);
  1601. remove_proc_entry(ACPI_VIDEO_CLASS, acpi_root_dir);
  1602. return;
  1603. }
  1604. module_init(acpi_video_init);
  1605. module_exit(acpi_video_exit);