video.c 47 KB

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