video.c 45 KB

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