video.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851
  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. *
  7. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or (at
  12. * your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License along
  20. * with this program; if not, write to the Free Software Foundation, Inc.,
  21. * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
  22. *
  23. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  24. */
  25. #include <linux/kernel.h>
  26. #include <linux/module.h>
  27. #include <linux/init.h>
  28. #include <linux/types.h>
  29. #include <linux/list.h>
  30. #include <linux/proc_fs.h>
  31. #include <linux/seq_file.h>
  32. #include <asm/uaccess.h>
  33. #include <acpi/acpi_bus.h>
  34. #include <acpi/acpi_drivers.h>
  35. #define ACPI_VIDEO_COMPONENT 0x08000000
  36. #define ACPI_VIDEO_CLASS "video"
  37. #define ACPI_VIDEO_DRIVER_NAME "ACPI Video Driver"
  38. #define ACPI_VIDEO_BUS_NAME "Video Bus"
  39. #define ACPI_VIDEO_DEVICE_NAME "Video Device"
  40. #define ACPI_VIDEO_NOTIFY_SWITCH 0x80
  41. #define ACPI_VIDEO_NOTIFY_PROBE 0x81
  42. #define ACPI_VIDEO_NOTIFY_CYCLE 0x82
  43. #define ACPI_VIDEO_NOTIFY_NEXT_OUTPUT 0x83
  44. #define ACPI_VIDEO_NOTIFY_PREV_OUTPUT 0x84
  45. #define ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS 0x82
  46. #define ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS 0x83
  47. #define ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS 0x84
  48. #define ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS 0x85
  49. #define ACPI_VIDEO_NOTIFY_DISPLAY_OFF 0x86
  50. #define ACPI_VIDEO_HEAD_INVALID (~0u - 1)
  51. #define ACPI_VIDEO_HEAD_END (~0u)
  52. #define _COMPONENT ACPI_VIDEO_COMPONENT
  53. ACPI_MODULE_NAME("acpi_video")
  54. MODULE_AUTHOR("Bruno Ducrot");
  55. MODULE_DESCRIPTION(ACPI_VIDEO_DRIVER_NAME);
  56. MODULE_LICENSE("GPL");
  57. static int acpi_video_bus_add(struct acpi_device *device);
  58. static int acpi_video_bus_remove(struct acpi_device *device, int type);
  59. static int acpi_video_bus_match(struct acpi_device *device,
  60. struct acpi_driver *driver);
  61. static struct acpi_driver acpi_video_bus = {
  62. .name = ACPI_VIDEO_DRIVER_NAME,
  63. .class = ACPI_VIDEO_CLASS,
  64. .ops = {
  65. .add = acpi_video_bus_add,
  66. .remove = acpi_video_bus_remove,
  67. .match = acpi_video_bus_match,
  68. },
  69. };
  70. struct acpi_video_bus_flags {
  71. u8 multihead:1; /* can switch video heads */
  72. u8 rom:1; /* can retrieve a video rom */
  73. u8 post:1; /* can configure the head to */
  74. u8 reserved:5;
  75. };
  76. struct acpi_video_bus_cap {
  77. u8 _DOS:1; /*Enable/Disable output switching */
  78. u8 _DOD:1; /*Enumerate all devices attached to display adapter */
  79. u8 _ROM:1; /*Get ROM Data */
  80. u8 _GPD:1; /*Get POST Device */
  81. u8 _SPD:1; /*Set POST Device */
  82. u8 _VPO:1; /*Video POST Options */
  83. u8 reserved:2;
  84. };
  85. struct acpi_video_device_attrib {
  86. u32 display_index:4; /* A zero-based instance of the Display */
  87. u32 display_port_attachment:4; /*This field differenates displays type */
  88. u32 display_type:4; /*Describe the specific type in use */
  89. u32 vendor_specific:4; /*Chipset Vendor Specifi */
  90. u32 bios_can_detect:1; /*BIOS can detect the device */
  91. u32 depend_on_vga:1; /*Non-VGA output device whose power is related to
  92. the VGA device. */
  93. u32 pipe_id:3; /*For VGA multiple-head devices. */
  94. u32 reserved:10; /*Must be 0 */
  95. u32 device_id_scheme:1; /*Device ID Scheme */
  96. };
  97. struct acpi_video_enumerated_device {
  98. union {
  99. u32 int_val;
  100. struct acpi_video_device_attrib attrib;
  101. } value;
  102. struct acpi_video_device *bind_info;
  103. };
  104. struct acpi_video_bus {
  105. acpi_handle handle;
  106. u8 dos_setting;
  107. struct acpi_video_enumerated_device *attached_array;
  108. u8 attached_count;
  109. struct acpi_video_bus_cap cap;
  110. struct acpi_video_bus_flags flags;
  111. struct semaphore sem;
  112. struct list_head video_device_list;
  113. struct proc_dir_entry *dir;
  114. };
  115. struct acpi_video_device_flags {
  116. u8 crt:1;
  117. u8 lcd:1;
  118. u8 tvout:1;
  119. u8 bios:1;
  120. u8 unknown:1;
  121. u8 reserved:3;
  122. };
  123. struct acpi_video_device_cap {
  124. u8 _ADR:1; /*Return the unique ID */
  125. u8 _BCL:1; /*Query list of brightness control levels supported */
  126. u8 _BCM:1; /*Set the brightness level */
  127. u8 _DDC:1; /*Return the EDID for this device */
  128. u8 _DCS:1; /*Return status of output device */
  129. u8 _DGS:1; /*Query graphics state */
  130. u8 _DSS:1; /*Device state set */
  131. u8 _reserved:1;
  132. };
  133. struct acpi_video_device_brightness {
  134. int curr;
  135. int count;
  136. int *levels;
  137. };
  138. struct acpi_video_device {
  139. acpi_handle handle;
  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->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->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->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->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->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->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->handle, "_DDC", &args, &buffer);
  325. if (ACPI_FAILURE(status))
  326. return -ENODEV;
  327. obj = (union acpi_object *)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->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->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->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->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->handle, "_ADR", &h_dummy1))) {
  422. device->cap._ADR = 1;
  423. }
  424. if (ACPI_SUCCESS(acpi_get_handle(device->handle, "_BCL", &h_dummy1))) {
  425. device->cap._BCL = 1;
  426. }
  427. if (ACPI_SUCCESS(acpi_get_handle(device->handle, "_BCM", &h_dummy1))) {
  428. device->cap._BCM = 1;
  429. }
  430. if (ACPI_SUCCESS(acpi_get_handle(device->handle, "_DDC", &h_dummy1))) {
  431. device->cap._DDC = 1;
  432. }
  433. if (ACPI_SUCCESS(acpi_get_handle(device->handle, "_DCS", &h_dummy1))) {
  434. device->cap._DCS = 1;
  435. }
  436. if (ACPI_SUCCESS(acpi_get_handle(device->handle, "_DGS", &h_dummy1))) {
  437. device->cap._DGS = 1;
  438. }
  439. if (ACPI_SUCCESS(acpi_get_handle(device->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 = kmalloc(sizeof(*br), GFP_KERNEL);
  447. if (!br) {
  448. printk(KERN_ERR "can't allocate memory\n");
  449. } else {
  450. memset(br, 0, sizeof(*br));
  451. br->levels = kmalloc(obj->package.count *
  452. sizeof *(br->levels), GFP_KERNEL);
  453. if (!br->levels)
  454. goto out;
  455. for (i = 0; i < obj->package.count; i++) {
  456. o = (union acpi_object *)&obj->package.
  457. elements[i];
  458. if (o->type != ACPI_TYPE_INTEGER) {
  459. printk(KERN_ERR PREFIX "Invalid data\n");
  460. continue;
  461. }
  462. br->levels[count] = (u32) o->integer.value;
  463. count++;
  464. }
  465. out:
  466. if (count < 2) {
  467. kfree(br->levels);
  468. kfree(br);
  469. } else {
  470. br->count = count;
  471. device->brightness = br;
  472. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  473. "found %d brightness levels\n",
  474. count));
  475. }
  476. }
  477. }
  478. kfree(obj);
  479. return;
  480. }
  481. /*
  482. * Arg:
  483. * device : video output device (VGA)
  484. *
  485. * Return Value:
  486. * None
  487. *
  488. * Find out all required AML method defined under the video bus device.
  489. */
  490. static void acpi_video_bus_find_cap(struct acpi_video_bus *video)
  491. {
  492. acpi_handle h_dummy1;
  493. memset(&video->cap, 0, 4);
  494. if (ACPI_SUCCESS(acpi_get_handle(video->handle, "_DOS", &h_dummy1))) {
  495. video->cap._DOS = 1;
  496. }
  497. if (ACPI_SUCCESS(acpi_get_handle(video->handle, "_DOD", &h_dummy1))) {
  498. video->cap._DOD = 1;
  499. }
  500. if (ACPI_SUCCESS(acpi_get_handle(video->handle, "_ROM", &h_dummy1))) {
  501. video->cap._ROM = 1;
  502. }
  503. if (ACPI_SUCCESS(acpi_get_handle(video->handle, "_GPD", &h_dummy1))) {
  504. video->cap._GPD = 1;
  505. }
  506. if (ACPI_SUCCESS(acpi_get_handle(video->handle, "_SPD", &h_dummy1))) {
  507. video->cap._SPD = 1;
  508. }
  509. if (ACPI_SUCCESS(acpi_get_handle(video->handle, "_VPO", &h_dummy1))) {
  510. video->cap._VPO = 1;
  511. }
  512. }
  513. /*
  514. * Check whether the video bus device has required AML method to
  515. * support the desired features
  516. */
  517. static int acpi_video_bus_check(struct acpi_video_bus *video)
  518. {
  519. acpi_status status = -ENOENT;
  520. if (!video)
  521. return -EINVAL;
  522. /* Since there is no HID, CID and so on for VGA driver, we have
  523. * to check well known required nodes.
  524. */
  525. /* Does this device able to support video switching ? */
  526. if (video->cap._DOS) {
  527. video->flags.multihead = 1;
  528. status = 0;
  529. }
  530. /* Does this device able to retrieve a retrieve a video ROM ? */
  531. if (video->cap._ROM) {
  532. video->flags.rom = 1;
  533. status = 0;
  534. }
  535. /* Does this device able to configure which video device to POST ? */
  536. if (video->cap._GPD && video->cap._SPD && video->cap._VPO) {
  537. video->flags.post = 1;
  538. status = 0;
  539. }
  540. return status;
  541. }
  542. /* --------------------------------------------------------------------------
  543. FS Interface (/proc)
  544. -------------------------------------------------------------------------- */
  545. static struct proc_dir_entry *acpi_video_dir;
  546. /* video devices */
  547. static int acpi_video_device_info_seq_show(struct seq_file *seq, void *offset)
  548. {
  549. struct acpi_video_device *dev =
  550. (struct acpi_video_device *)seq->private;
  551. if (!dev)
  552. goto end;
  553. seq_printf(seq, "device_id: 0x%04x\n", (u32) dev->device_id);
  554. seq_printf(seq, "type: ");
  555. if (dev->flags.crt)
  556. seq_printf(seq, "CRT\n");
  557. else if (dev->flags.lcd)
  558. seq_printf(seq, "LCD\n");
  559. else if (dev->flags.tvout)
  560. seq_printf(seq, "TVOUT\n");
  561. else
  562. seq_printf(seq, "UNKNOWN\n");
  563. seq_printf(seq, "known by bios: %s\n", dev->flags.bios ? "yes" : "no");
  564. end:
  565. return 0;
  566. }
  567. static int
  568. acpi_video_device_info_open_fs(struct inode *inode, struct file *file)
  569. {
  570. return single_open(file, acpi_video_device_info_seq_show,
  571. PDE(inode)->data);
  572. }
  573. static int acpi_video_device_state_seq_show(struct seq_file *seq, void *offset)
  574. {
  575. int status;
  576. struct acpi_video_device *dev =
  577. (struct acpi_video_device *)seq->private;
  578. unsigned long state;
  579. if (!dev)
  580. goto end;
  581. status = acpi_video_device_get_state(dev, &state);
  582. seq_printf(seq, "state: ");
  583. if (ACPI_SUCCESS(status))
  584. seq_printf(seq, "0x%02lx\n", state);
  585. else
  586. seq_printf(seq, "<not supported>\n");
  587. status = acpi_video_device_query(dev, &state);
  588. seq_printf(seq, "query: ");
  589. if (ACPI_SUCCESS(status))
  590. seq_printf(seq, "0x%02lx\n", state);
  591. else
  592. seq_printf(seq, "<not supported>\n");
  593. end:
  594. return 0;
  595. }
  596. static int
  597. acpi_video_device_state_open_fs(struct inode *inode, struct file *file)
  598. {
  599. return single_open(file, acpi_video_device_state_seq_show,
  600. PDE(inode)->data);
  601. }
  602. static ssize_t
  603. acpi_video_device_write_state(struct file *file,
  604. const char __user * buffer,
  605. size_t count, loff_t * data)
  606. {
  607. int status;
  608. struct seq_file *m = (struct seq_file *)file->private_data;
  609. struct acpi_video_device *dev = (struct acpi_video_device *)m->private;
  610. char str[12] = { 0 };
  611. u32 state = 0;
  612. if (!dev || count + 1 > sizeof str)
  613. return -EINVAL;
  614. if (copy_from_user(str, buffer, count))
  615. return -EFAULT;
  616. str[count] = 0;
  617. state = simple_strtoul(str, NULL, 0);
  618. state &= ((1ul << 31) | (1ul << 30) | (1ul << 0));
  619. status = acpi_video_device_set_state(dev, state);
  620. if (status)
  621. return -EFAULT;
  622. return count;
  623. }
  624. static int
  625. acpi_video_device_brightness_seq_show(struct seq_file *seq, void *offset)
  626. {
  627. struct acpi_video_device *dev =
  628. (struct acpi_video_device *)seq->private;
  629. int i;
  630. if (!dev || !dev->brightness) {
  631. seq_printf(seq, "<not supported>\n");
  632. return 0;
  633. }
  634. seq_printf(seq, "levels: ");
  635. for (i = 0; i < dev->brightness->count; i++)
  636. seq_printf(seq, " %d", dev->brightness->levels[i]);
  637. seq_printf(seq, "\ncurrent: %d\n", dev->brightness->curr);
  638. return 0;
  639. }
  640. static int
  641. acpi_video_device_brightness_open_fs(struct inode *inode, struct file *file)
  642. {
  643. return single_open(file, acpi_video_device_brightness_seq_show,
  644. PDE(inode)->data);
  645. }
  646. static ssize_t
  647. acpi_video_device_write_brightness(struct file *file,
  648. const char __user * buffer,
  649. size_t count, loff_t * data)
  650. {
  651. struct seq_file *m = (struct seq_file *)file->private_data;
  652. struct acpi_video_device *dev = (struct acpi_video_device *)m->private;
  653. char str[4] = { 0 };
  654. unsigned int level = 0;
  655. int i;
  656. if (!dev || !dev->brightness || count + 1 > sizeof str)
  657. return -EINVAL;
  658. if (copy_from_user(str, buffer, count))
  659. return -EFAULT;
  660. str[count] = 0;
  661. level = simple_strtoul(str, NULL, 0);
  662. if (level > 100)
  663. return -EFAULT;
  664. /* validate though the list of available levels */
  665. for (i = 0; i < dev->brightness->count; i++)
  666. if (level == dev->brightness->levels[i]) {
  667. if (ACPI_SUCCESS
  668. (acpi_video_device_lcd_set_level(dev, level)))
  669. dev->brightness->curr = level;
  670. break;
  671. }
  672. return count;
  673. }
  674. static int acpi_video_device_EDID_seq_show(struct seq_file *seq, void *offset)
  675. {
  676. struct acpi_video_device *dev =
  677. (struct acpi_video_device *)seq->private;
  678. int status;
  679. int i;
  680. union acpi_object *edid = NULL;
  681. if (!dev)
  682. goto out;
  683. status = acpi_video_device_EDID(dev, &edid, 128);
  684. if (ACPI_FAILURE(status)) {
  685. status = acpi_video_device_EDID(dev, &edid, 256);
  686. }
  687. if (ACPI_FAILURE(status)) {
  688. goto out;
  689. }
  690. if (edid && edid->type == ACPI_TYPE_BUFFER) {
  691. for (i = 0; i < edid->buffer.length; i++)
  692. seq_putc(seq, edid->buffer.pointer[i]);
  693. }
  694. out:
  695. if (!edid)
  696. seq_printf(seq, "<not supported>\n");
  697. else
  698. kfree(edid);
  699. return 0;
  700. }
  701. static int
  702. acpi_video_device_EDID_open_fs(struct inode *inode, struct file *file)
  703. {
  704. return single_open(file, acpi_video_device_EDID_seq_show,
  705. PDE(inode)->data);
  706. }
  707. static int acpi_video_device_add_fs(struct acpi_device *device)
  708. {
  709. struct proc_dir_entry *entry = NULL;
  710. struct acpi_video_device *vid_dev;
  711. if (!device)
  712. return -ENODEV;
  713. vid_dev = (struct acpi_video_device *)acpi_driver_data(device);
  714. if (!vid_dev)
  715. return -ENODEV;
  716. if (!acpi_device_dir(device)) {
  717. acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
  718. vid_dev->video->dir);
  719. if (!acpi_device_dir(device))
  720. return -ENODEV;
  721. acpi_device_dir(device)->owner = THIS_MODULE;
  722. }
  723. /* 'info' [R] */
  724. entry = create_proc_entry("info", S_IRUGO, acpi_device_dir(device));
  725. if (!entry)
  726. return -ENODEV;
  727. else {
  728. entry->proc_fops = &acpi_video_device_info_fops;
  729. entry->data = acpi_driver_data(device);
  730. entry->owner = THIS_MODULE;
  731. }
  732. /* 'state' [R/W] */
  733. entry =
  734. create_proc_entry("state", S_IFREG | S_IRUGO | S_IWUSR,
  735. acpi_device_dir(device));
  736. if (!entry)
  737. return -ENODEV;
  738. else {
  739. acpi_video_device_state_fops.write = acpi_video_device_write_state;
  740. entry->proc_fops = &acpi_video_device_state_fops;
  741. entry->data = acpi_driver_data(device);
  742. entry->owner = THIS_MODULE;
  743. }
  744. /* 'brightness' [R/W] */
  745. entry =
  746. create_proc_entry("brightness", S_IFREG | S_IRUGO | S_IWUSR,
  747. acpi_device_dir(device));
  748. if (!entry)
  749. return -ENODEV;
  750. else {
  751. acpi_video_device_brightness_fops.write = acpi_video_device_write_brightness;
  752. entry->proc_fops = &acpi_video_device_brightness_fops;
  753. entry->data = acpi_driver_data(device);
  754. entry->owner = THIS_MODULE;
  755. }
  756. /* 'EDID' [R] */
  757. entry = create_proc_entry("EDID", S_IRUGO, acpi_device_dir(device));
  758. if (!entry)
  759. return -ENODEV;
  760. else {
  761. entry->proc_fops = &acpi_video_device_EDID_fops;
  762. entry->data = acpi_driver_data(device);
  763. entry->owner = THIS_MODULE;
  764. }
  765. return 0;
  766. }
  767. static int acpi_video_device_remove_fs(struct acpi_device *device)
  768. {
  769. struct acpi_video_device *vid_dev;
  770. vid_dev = (struct acpi_video_device *)acpi_driver_data(device);
  771. if (!vid_dev || !vid_dev->video || !vid_dev->video->dir)
  772. return -ENODEV;
  773. if (acpi_device_dir(device)) {
  774. remove_proc_entry("info", acpi_device_dir(device));
  775. remove_proc_entry("state", acpi_device_dir(device));
  776. remove_proc_entry("brightness", acpi_device_dir(device));
  777. remove_proc_entry("EDID", acpi_device_dir(device));
  778. remove_proc_entry(acpi_device_bid(device), vid_dev->video->dir);
  779. acpi_device_dir(device) = NULL;
  780. }
  781. return 0;
  782. }
  783. /* video bus */
  784. static int acpi_video_bus_info_seq_show(struct seq_file *seq, void *offset)
  785. {
  786. struct acpi_video_bus *video = (struct acpi_video_bus *)seq->private;
  787. if (!video)
  788. goto end;
  789. seq_printf(seq, "Switching heads: %s\n",
  790. video->flags.multihead ? "yes" : "no");
  791. seq_printf(seq, "Video ROM: %s\n",
  792. video->flags.rom ? "yes" : "no");
  793. seq_printf(seq, "Device to be POSTed on boot: %s\n",
  794. video->flags.post ? "yes" : "no");
  795. end:
  796. return 0;
  797. }
  798. static int acpi_video_bus_info_open_fs(struct inode *inode, struct file *file)
  799. {
  800. return single_open(file, acpi_video_bus_info_seq_show,
  801. PDE(inode)->data);
  802. }
  803. static int acpi_video_bus_ROM_seq_show(struct seq_file *seq, void *offset)
  804. {
  805. struct acpi_video_bus *video = (struct acpi_video_bus *)seq->private;
  806. if (!video)
  807. goto end;
  808. printk(KERN_INFO PREFIX "Please implement %s\n", __FUNCTION__);
  809. seq_printf(seq, "<TODO>\n");
  810. end:
  811. return 0;
  812. }
  813. static int acpi_video_bus_ROM_open_fs(struct inode *inode, struct file *file)
  814. {
  815. return single_open(file, acpi_video_bus_ROM_seq_show, PDE(inode)->data);
  816. }
  817. static int acpi_video_bus_POST_info_seq_show(struct seq_file *seq, void *offset)
  818. {
  819. struct acpi_video_bus *video = (struct acpi_video_bus *)seq->private;
  820. unsigned long options;
  821. int status;
  822. if (!video)
  823. goto end;
  824. status = acpi_video_bus_POST_options(video, &options);
  825. if (ACPI_SUCCESS(status)) {
  826. if (!(options & 1)) {
  827. printk(KERN_WARNING PREFIX
  828. "The motherboard VGA device is not listed as a possible POST device.\n");
  829. printk(KERN_WARNING PREFIX
  830. "This indicate a BIOS bug. Please contact the manufacturer.\n");
  831. }
  832. printk("%lx\n", options);
  833. seq_printf(seq, "can POST: <intgrated video>");
  834. if (options & 2)
  835. seq_printf(seq, " <PCI video>");
  836. if (options & 4)
  837. seq_printf(seq, " <AGP video>");
  838. seq_putc(seq, '\n');
  839. } else
  840. seq_printf(seq, "<not supported>\n");
  841. end:
  842. return 0;
  843. }
  844. static int
  845. acpi_video_bus_POST_info_open_fs(struct inode *inode, struct file *file)
  846. {
  847. return single_open(file, acpi_video_bus_POST_info_seq_show,
  848. PDE(inode)->data);
  849. }
  850. static int acpi_video_bus_POST_seq_show(struct seq_file *seq, void *offset)
  851. {
  852. struct acpi_video_bus *video = (struct acpi_video_bus *)seq->private;
  853. int status;
  854. unsigned long id;
  855. if (!video)
  856. goto end;
  857. status = acpi_video_bus_get_POST(video, &id);
  858. if (!ACPI_SUCCESS(status)) {
  859. seq_printf(seq, "<not supported>\n");
  860. goto end;
  861. }
  862. seq_printf(seq, "device posted is <%s>\n", device_decode[id & 3]);
  863. end:
  864. return 0;
  865. }
  866. static int acpi_video_bus_DOS_seq_show(struct seq_file *seq, void *offset)
  867. {
  868. struct acpi_video_bus *video = (struct acpi_video_bus *)seq->private;
  869. seq_printf(seq, "DOS setting: <%d>\n", video->dos_setting);
  870. return 0;
  871. }
  872. static int acpi_video_bus_POST_open_fs(struct inode *inode, struct file *file)
  873. {
  874. return single_open(file, acpi_video_bus_POST_seq_show,
  875. PDE(inode)->data);
  876. }
  877. static int acpi_video_bus_DOS_open_fs(struct inode *inode, struct file *file)
  878. {
  879. return single_open(file, acpi_video_bus_DOS_seq_show, PDE(inode)->data);
  880. }
  881. static ssize_t
  882. acpi_video_bus_write_POST(struct file *file,
  883. const char __user * buffer,
  884. size_t count, loff_t * data)
  885. {
  886. int status;
  887. struct seq_file *m = (struct seq_file *)file->private_data;
  888. struct acpi_video_bus *video = (struct acpi_video_bus *)m->private;
  889. char str[12] = { 0 };
  890. unsigned long opt, options;
  891. if (!video || count + 1 > sizeof str)
  892. return -EINVAL;
  893. status = acpi_video_bus_POST_options(video, &options);
  894. if (!ACPI_SUCCESS(status))
  895. return -EINVAL;
  896. if (copy_from_user(str, buffer, count))
  897. return -EFAULT;
  898. str[count] = 0;
  899. opt = strtoul(str, NULL, 0);
  900. if (opt > 3)
  901. return -EFAULT;
  902. /* just in case an OEM 'forget' the motherboard... */
  903. options |= 1;
  904. if (options & (1ul << opt)) {
  905. status = acpi_video_bus_set_POST(video, opt);
  906. if (!ACPI_SUCCESS(status))
  907. return -EFAULT;
  908. }
  909. return count;
  910. }
  911. static ssize_t
  912. acpi_video_bus_write_DOS(struct file *file,
  913. const char __user * buffer,
  914. size_t count, loff_t * data)
  915. {
  916. int status;
  917. struct seq_file *m = (struct seq_file *)file->private_data;
  918. struct acpi_video_bus *video = (struct acpi_video_bus *)m->private;
  919. char str[12] = { 0 };
  920. unsigned long opt;
  921. if (!video || count + 1 > sizeof str)
  922. return -EINVAL;
  923. if (copy_from_user(str, buffer, count))
  924. return -EFAULT;
  925. str[count] = 0;
  926. opt = strtoul(str, NULL, 0);
  927. if (opt > 7)
  928. return -EFAULT;
  929. status = acpi_video_bus_DOS(video, opt & 0x3, (opt & 0x4) >> 2);
  930. if (!ACPI_SUCCESS(status))
  931. return -EFAULT;
  932. return count;
  933. }
  934. static int acpi_video_bus_add_fs(struct acpi_device *device)
  935. {
  936. struct proc_dir_entry *entry = NULL;
  937. struct acpi_video_bus *video;
  938. video = (struct acpi_video_bus *)acpi_driver_data(device);
  939. if (!acpi_device_dir(device)) {
  940. acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
  941. acpi_video_dir);
  942. if (!acpi_device_dir(device))
  943. return -ENODEV;
  944. video->dir = acpi_device_dir(device);
  945. acpi_device_dir(device)->owner = THIS_MODULE;
  946. }
  947. /* 'info' [R] */
  948. entry = create_proc_entry("info", S_IRUGO, acpi_device_dir(device));
  949. if (!entry)
  950. return -ENODEV;
  951. else {
  952. entry->proc_fops = &acpi_video_bus_info_fops;
  953. entry->data = acpi_driver_data(device);
  954. entry->owner = THIS_MODULE;
  955. }
  956. /* 'ROM' [R] */
  957. entry = create_proc_entry("ROM", S_IRUGO, acpi_device_dir(device));
  958. if (!entry)
  959. return -ENODEV;
  960. else {
  961. entry->proc_fops = &acpi_video_bus_ROM_fops;
  962. entry->data = acpi_driver_data(device);
  963. entry->owner = THIS_MODULE;
  964. }
  965. /* 'POST_info' [R] */
  966. entry =
  967. create_proc_entry("POST_info", S_IRUGO, acpi_device_dir(device));
  968. if (!entry)
  969. return -ENODEV;
  970. else {
  971. entry->proc_fops = &acpi_video_bus_POST_info_fops;
  972. entry->data = acpi_driver_data(device);
  973. entry->owner = THIS_MODULE;
  974. }
  975. /* 'POST' [R/W] */
  976. entry =
  977. create_proc_entry("POST", S_IFREG | S_IRUGO | S_IRUSR,
  978. acpi_device_dir(device));
  979. if (!entry)
  980. return -ENODEV;
  981. else {
  982. acpi_video_bus_POST_fops.write = acpi_video_bus_write_POST;
  983. entry->proc_fops = &acpi_video_bus_POST_fops;
  984. entry->data = acpi_driver_data(device);
  985. entry->owner = THIS_MODULE;
  986. }
  987. /* 'DOS' [R/W] */
  988. entry =
  989. create_proc_entry("DOS", S_IFREG | S_IRUGO | S_IRUSR,
  990. acpi_device_dir(device));
  991. if (!entry)
  992. return -ENODEV;
  993. else {
  994. acpi_video_bus_DOS_fops.write = acpi_video_bus_write_DOS;
  995. entry->proc_fops = &acpi_video_bus_DOS_fops;
  996. entry->data = acpi_driver_data(device);
  997. entry->owner = THIS_MODULE;
  998. }
  999. return 0;
  1000. }
  1001. static int acpi_video_bus_remove_fs(struct acpi_device *device)
  1002. {
  1003. struct acpi_video_bus *video;
  1004. video = (struct acpi_video_bus *)acpi_driver_data(device);
  1005. if (acpi_device_dir(device)) {
  1006. remove_proc_entry("info", acpi_device_dir(device));
  1007. remove_proc_entry("ROM", acpi_device_dir(device));
  1008. remove_proc_entry("POST_info", acpi_device_dir(device));
  1009. remove_proc_entry("POST", acpi_device_dir(device));
  1010. remove_proc_entry("DOS", acpi_device_dir(device));
  1011. remove_proc_entry(acpi_device_bid(device), acpi_video_dir);
  1012. acpi_device_dir(device) = NULL;
  1013. }
  1014. return 0;
  1015. }
  1016. /* --------------------------------------------------------------------------
  1017. Driver Interface
  1018. -------------------------------------------------------------------------- */
  1019. /* device interface */
  1020. static int
  1021. acpi_video_bus_get_one_device(struct acpi_device *device,
  1022. struct acpi_video_bus *video)
  1023. {
  1024. unsigned long device_id;
  1025. int status;
  1026. struct acpi_video_device *data;
  1027. if (!device || !video)
  1028. return -EINVAL;
  1029. status =
  1030. acpi_evaluate_integer(device->handle, "_ADR", NULL, &device_id);
  1031. if (ACPI_SUCCESS(status)) {
  1032. data = kmalloc(sizeof(struct acpi_video_device), GFP_KERNEL);
  1033. if (!data)
  1034. return -ENOMEM;
  1035. memset(data, 0, sizeof(struct acpi_video_device));
  1036. data->handle = device->handle;
  1037. strcpy(acpi_device_name(device), ACPI_VIDEO_DEVICE_NAME);
  1038. strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
  1039. acpi_driver_data(device) = data;
  1040. data->device_id = device_id;
  1041. data->video = video;
  1042. data->dev = device;
  1043. switch (device_id & 0xffff) {
  1044. case 0x0100:
  1045. data->flags.crt = 1;
  1046. break;
  1047. case 0x0400:
  1048. data->flags.lcd = 1;
  1049. break;
  1050. case 0x0200:
  1051. data->flags.tvout = 1;
  1052. break;
  1053. default:
  1054. data->flags.unknown = 1;
  1055. break;
  1056. }
  1057. acpi_video_device_bind(video, data);
  1058. acpi_video_device_find_cap(data);
  1059. status = acpi_install_notify_handler(data->handle,
  1060. ACPI_DEVICE_NOTIFY,
  1061. acpi_video_device_notify,
  1062. data);
  1063. if (ACPI_FAILURE(status)) {
  1064. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  1065. "Error installing notify handler\n"));
  1066. if(data->brightness)
  1067. kfree(data->brightness->levels);
  1068. kfree(data->brightness);
  1069. kfree(data);
  1070. return -ENODEV;
  1071. }
  1072. down(&video->sem);
  1073. list_add_tail(&data->entry, &video->video_device_list);
  1074. up(&video->sem);
  1075. acpi_video_device_add_fs(device);
  1076. return 0;
  1077. }
  1078. return -ENOENT;
  1079. }
  1080. /*
  1081. * Arg:
  1082. * video : video bus device
  1083. *
  1084. * Return:
  1085. * none
  1086. *
  1087. * Enumerate the video device list of the video bus,
  1088. * bind the ids with the corresponding video devices
  1089. * under the video bus.
  1090. */
  1091. static void acpi_video_device_rebind(struct acpi_video_bus *video)
  1092. {
  1093. struct list_head *node, *next;
  1094. list_for_each_safe(node, next, &video->video_device_list) {
  1095. struct acpi_video_device *dev =
  1096. container_of(node, struct acpi_video_device, entry);
  1097. acpi_video_device_bind(video, dev);
  1098. }
  1099. }
  1100. /*
  1101. * Arg:
  1102. * video : video bus device
  1103. * device : video output device under the video
  1104. * bus
  1105. *
  1106. * Return:
  1107. * none
  1108. *
  1109. * Bind the ids with the corresponding video devices
  1110. * under the video bus.
  1111. */
  1112. static void
  1113. acpi_video_device_bind(struct acpi_video_bus *video,
  1114. struct acpi_video_device *device)
  1115. {
  1116. int i;
  1117. #define IDS_VAL(i) video->attached_array[i].value.int_val
  1118. #define IDS_BIND(i) video->attached_array[i].bind_info
  1119. for (i = 0; IDS_VAL(i) != ACPI_VIDEO_HEAD_INVALID &&
  1120. i < video->attached_count; i++) {
  1121. if (device->device_id == (IDS_VAL(i) & 0xffff)) {
  1122. IDS_BIND(i) = device;
  1123. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "device_bind %d\n", i));
  1124. }
  1125. }
  1126. #undef IDS_VAL
  1127. #undef IDS_BIND
  1128. }
  1129. /*
  1130. * Arg:
  1131. * video : video bus device
  1132. *
  1133. * Return:
  1134. * < 0 : error
  1135. *
  1136. * Call _DOD to enumerate all devices attached to display adapter
  1137. *
  1138. */
  1139. static int acpi_video_device_enumerate(struct acpi_video_bus *video)
  1140. {
  1141. int status;
  1142. int count;
  1143. int i;
  1144. struct acpi_video_enumerated_device *active_device_list;
  1145. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  1146. union acpi_object *dod = NULL;
  1147. union acpi_object *obj;
  1148. status = acpi_evaluate_object(video->handle, "_DOD", NULL, &buffer);
  1149. if (!ACPI_SUCCESS(status)) {
  1150. ACPI_EXCEPTION((AE_INFO, status, "Evaluating _DOD"));
  1151. return status;
  1152. }
  1153. dod = (union acpi_object *)buffer.pointer;
  1154. if (!dod || (dod->type != ACPI_TYPE_PACKAGE)) {
  1155. ACPI_EXCEPTION((AE_INFO, status, "Invalid _DOD data"));
  1156. status = -EFAULT;
  1157. goto out;
  1158. }
  1159. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d video heads in _DOD\n",
  1160. dod->package.count));
  1161. active_device_list = kmalloc((1 +
  1162. dod->package.count) *
  1163. sizeof(struct
  1164. acpi_video_enumerated_device),
  1165. GFP_KERNEL);
  1166. if (!active_device_list) {
  1167. status = -ENOMEM;
  1168. goto out;
  1169. }
  1170. count = 0;
  1171. for (i = 0; i < dod->package.count; i++) {
  1172. obj = (union acpi_object *)&dod->package.elements[i];
  1173. if (obj->type != ACPI_TYPE_INTEGER) {
  1174. printk(KERN_ERR PREFIX "Invalid _DOD data\n");
  1175. active_device_list[i].value.int_val =
  1176. ACPI_VIDEO_HEAD_INVALID;
  1177. }
  1178. active_device_list[i].value.int_val = obj->integer.value;
  1179. active_device_list[i].bind_info = NULL;
  1180. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "dod element[%d] = %d\n", i,
  1181. (int)obj->integer.value));
  1182. count++;
  1183. }
  1184. active_device_list[count].value.int_val = ACPI_VIDEO_HEAD_END;
  1185. kfree(video->attached_array);
  1186. video->attached_array = active_device_list;
  1187. video->attached_count = count;
  1188. out:
  1189. kfree(buffer.pointer);
  1190. return status;
  1191. }
  1192. /*
  1193. * Arg:
  1194. * video : video bus device
  1195. * event : Nontify Event
  1196. *
  1197. * Return:
  1198. * < 0 : error
  1199. *
  1200. * 1. Find out the current active output device.
  1201. * 2. Identify the next output device to switch
  1202. * 3. call _DSS to do actual switch.
  1203. */
  1204. static int acpi_video_switch_output(struct acpi_video_bus *video, int event)
  1205. {
  1206. struct list_head *node, *next;
  1207. struct acpi_video_device *dev = NULL;
  1208. struct acpi_video_device *dev_next = NULL;
  1209. struct acpi_video_device *dev_prev = NULL;
  1210. unsigned long state;
  1211. int status = 0;
  1212. list_for_each_safe(node, next, &video->video_device_list) {
  1213. dev = container_of(node, struct acpi_video_device, entry);
  1214. status = acpi_video_device_get_state(dev, &state);
  1215. if (state & 0x2) {
  1216. dev_next =
  1217. container_of(node->next, struct acpi_video_device,
  1218. entry);
  1219. dev_prev =
  1220. container_of(node->prev, struct acpi_video_device,
  1221. entry);
  1222. goto out;
  1223. }
  1224. }
  1225. dev_next = container_of(node->next, struct acpi_video_device, entry);
  1226. dev_prev = container_of(node->prev, struct acpi_video_device, entry);
  1227. out:
  1228. switch (event) {
  1229. case ACPI_VIDEO_NOTIFY_CYCLE:
  1230. case ACPI_VIDEO_NOTIFY_NEXT_OUTPUT:
  1231. acpi_video_device_set_state(dev, 0);
  1232. acpi_video_device_set_state(dev_next, 0x80000001);
  1233. break;
  1234. case ACPI_VIDEO_NOTIFY_PREV_OUTPUT:
  1235. acpi_video_device_set_state(dev, 0);
  1236. acpi_video_device_set_state(dev_prev, 0x80000001);
  1237. default:
  1238. break;
  1239. }
  1240. return status;
  1241. }
  1242. static int
  1243. acpi_video_get_next_level(struct acpi_video_device *device,
  1244. u32 level_current, u32 event)
  1245. {
  1246. /*Fix me */
  1247. return level_current;
  1248. }
  1249. static void
  1250. acpi_video_switch_brightness(struct acpi_video_device *device, int event)
  1251. {
  1252. unsigned long level_current, level_next;
  1253. acpi_video_device_lcd_get_level_current(device, &level_current);
  1254. level_next = acpi_video_get_next_level(device, level_current, event);
  1255. acpi_video_device_lcd_set_level(device, level_next);
  1256. }
  1257. static int
  1258. acpi_video_bus_get_devices(struct acpi_video_bus *video,
  1259. struct acpi_device *device)
  1260. {
  1261. int status = 0;
  1262. struct list_head *node, *next;
  1263. acpi_video_device_enumerate(video);
  1264. list_for_each_safe(node, next, &device->children) {
  1265. struct acpi_device *dev =
  1266. list_entry(node, struct acpi_device, node);
  1267. if (!dev)
  1268. continue;
  1269. status = acpi_video_bus_get_one_device(dev, video);
  1270. if (ACPI_FAILURE(status)) {
  1271. ACPI_EXCEPTION((AE_INFO, status, "Cant attach device"));
  1272. continue;
  1273. }
  1274. }
  1275. return status;
  1276. }
  1277. static int acpi_video_bus_put_one_device(struct acpi_video_device *device)
  1278. {
  1279. acpi_status status;
  1280. struct acpi_video_bus *video;
  1281. if (!device || !device->video)
  1282. return -ENOENT;
  1283. video = device->video;
  1284. down(&video->sem);
  1285. list_del(&device->entry);
  1286. up(&video->sem);
  1287. acpi_video_device_remove_fs(device->dev);
  1288. status = acpi_remove_notify_handler(device->handle,
  1289. ACPI_DEVICE_NOTIFY,
  1290. acpi_video_device_notify);
  1291. return 0;
  1292. }
  1293. static int acpi_video_bus_put_devices(struct acpi_video_bus *video)
  1294. {
  1295. int status;
  1296. struct list_head *node, *next;
  1297. list_for_each_safe(node, next, &video->video_device_list) {
  1298. struct acpi_video_device *data =
  1299. list_entry(node, struct acpi_video_device, entry);
  1300. if (!data)
  1301. continue;
  1302. status = acpi_video_bus_put_one_device(data);
  1303. if (ACPI_FAILURE(status))
  1304. printk(KERN_WARNING PREFIX
  1305. "hhuuhhuu bug in acpi video driver.\n");
  1306. if (data->brightness)
  1307. kfree(data->brightness->levels);
  1308. kfree(data->brightness);
  1309. kfree(data);
  1310. }
  1311. return 0;
  1312. }
  1313. /* acpi_video interface */
  1314. static int acpi_video_bus_start_devices(struct acpi_video_bus *video)
  1315. {
  1316. return acpi_video_bus_DOS(video, 1, 0);
  1317. }
  1318. static int acpi_video_bus_stop_devices(struct acpi_video_bus *video)
  1319. {
  1320. return acpi_video_bus_DOS(video, 0, 1);
  1321. }
  1322. static void acpi_video_bus_notify(acpi_handle handle, u32 event, void *data)
  1323. {
  1324. struct acpi_video_bus *video = (struct acpi_video_bus *)data;
  1325. struct acpi_device *device = NULL;
  1326. printk("video bus notify\n");
  1327. if (!video)
  1328. return;
  1329. if (acpi_bus_get_device(handle, &device))
  1330. return;
  1331. switch (event) {
  1332. case ACPI_VIDEO_NOTIFY_SWITCH: /* User request that a switch occur,
  1333. * most likely via hotkey. */
  1334. acpi_bus_generate_event(device, event, 0);
  1335. break;
  1336. case ACPI_VIDEO_NOTIFY_PROBE: /* User plug or remove a video
  1337. * connector. */
  1338. acpi_video_device_enumerate(video);
  1339. acpi_video_device_rebind(video);
  1340. acpi_video_switch_output(video, event);
  1341. acpi_bus_generate_event(device, event, 0);
  1342. break;
  1343. case ACPI_VIDEO_NOTIFY_CYCLE: /* Cycle Display output hotkey pressed. */
  1344. case ACPI_VIDEO_NOTIFY_NEXT_OUTPUT: /* Next Display output hotkey pressed. */
  1345. case ACPI_VIDEO_NOTIFY_PREV_OUTPUT: /* previous Display output hotkey pressed. */
  1346. acpi_video_switch_output(video, event);
  1347. acpi_bus_generate_event(device, event, 0);
  1348. break;
  1349. default:
  1350. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  1351. "Unsupported event [0x%x]\n", event));
  1352. break;
  1353. }
  1354. return;
  1355. }
  1356. static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data)
  1357. {
  1358. struct acpi_video_device *video_device =
  1359. (struct acpi_video_device *)data;
  1360. struct acpi_device *device = NULL;
  1361. printk("video device notify\n");
  1362. if (!video_device)
  1363. return;
  1364. if (acpi_bus_get_device(handle, &device))
  1365. return;
  1366. switch (event) {
  1367. case ACPI_VIDEO_NOTIFY_SWITCH: /* change in status (cycle output device) */
  1368. case ACPI_VIDEO_NOTIFY_PROBE: /* change in status (output device status) */
  1369. acpi_bus_generate_event(device, event, 0);
  1370. break;
  1371. case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS: /* Cycle brightness */
  1372. case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS: /* Increase brightness */
  1373. case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS: /* Decrease brightness */
  1374. case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS: /* zero brightnesss */
  1375. case ACPI_VIDEO_NOTIFY_DISPLAY_OFF: /* display device off */
  1376. acpi_video_switch_brightness(video_device, event);
  1377. acpi_bus_generate_event(device, event, 0);
  1378. break;
  1379. default:
  1380. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  1381. "Unsupported event [0x%x]\n", event));
  1382. break;
  1383. }
  1384. return;
  1385. }
  1386. static int acpi_video_bus_add(struct acpi_device *device)
  1387. {
  1388. int result = 0;
  1389. acpi_status status = 0;
  1390. struct acpi_video_bus *video = NULL;
  1391. if (!device)
  1392. return -EINVAL;
  1393. video = kmalloc(sizeof(struct acpi_video_bus), GFP_KERNEL);
  1394. if (!video)
  1395. return -ENOMEM;
  1396. memset(video, 0, sizeof(struct acpi_video_bus));
  1397. video->handle = device->handle;
  1398. strcpy(acpi_device_name(device), ACPI_VIDEO_BUS_NAME);
  1399. strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
  1400. acpi_driver_data(device) = video;
  1401. acpi_video_bus_find_cap(video);
  1402. result = acpi_video_bus_check(video);
  1403. if (result)
  1404. goto end;
  1405. result = acpi_video_bus_add_fs(device);
  1406. if (result)
  1407. goto end;
  1408. init_MUTEX(&video->sem);
  1409. INIT_LIST_HEAD(&video->video_device_list);
  1410. acpi_video_bus_get_devices(video, device);
  1411. acpi_video_bus_start_devices(video);
  1412. status = acpi_install_notify_handler(video->handle,
  1413. ACPI_DEVICE_NOTIFY,
  1414. acpi_video_bus_notify, video);
  1415. if (ACPI_FAILURE(status)) {
  1416. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  1417. "Error installing notify handler\n"));
  1418. acpi_video_bus_stop_devices(video);
  1419. acpi_video_bus_put_devices(video);
  1420. kfree(video->attached_array);
  1421. acpi_video_bus_remove_fs(device);
  1422. result = -ENODEV;
  1423. goto end;
  1424. }
  1425. printk(KERN_INFO PREFIX "%s [%s] (multi-head: %s rom: %s post: %s)\n",
  1426. ACPI_VIDEO_DEVICE_NAME, acpi_device_bid(device),
  1427. video->flags.multihead ? "yes" : "no",
  1428. video->flags.rom ? "yes" : "no",
  1429. video->flags.post ? "yes" : "no");
  1430. end:
  1431. if (result)
  1432. kfree(video);
  1433. return result;
  1434. }
  1435. static int acpi_video_bus_remove(struct acpi_device *device, int type)
  1436. {
  1437. acpi_status status = 0;
  1438. struct acpi_video_bus *video = NULL;
  1439. if (!device || !acpi_driver_data(device))
  1440. return -EINVAL;
  1441. video = (struct acpi_video_bus *)acpi_driver_data(device);
  1442. acpi_video_bus_stop_devices(video);
  1443. status = acpi_remove_notify_handler(video->handle,
  1444. ACPI_DEVICE_NOTIFY,
  1445. acpi_video_bus_notify);
  1446. acpi_video_bus_put_devices(video);
  1447. acpi_video_bus_remove_fs(device);
  1448. kfree(video->attached_array);
  1449. kfree(video);
  1450. return 0;
  1451. }
  1452. static int
  1453. acpi_video_bus_match(struct acpi_device *device, struct acpi_driver *driver)
  1454. {
  1455. acpi_handle h_dummy1;
  1456. acpi_handle h_dummy2;
  1457. acpi_handle h_dummy3;
  1458. if (!device || !driver)
  1459. return -EINVAL;
  1460. /* Since there is no HID, CID for ACPI Video drivers, we have
  1461. * to check well known required nodes for each feature we support.
  1462. */
  1463. /* Does this device able to support video switching ? */
  1464. if (ACPI_SUCCESS(acpi_get_handle(device->handle, "_DOD", &h_dummy1)) &&
  1465. ACPI_SUCCESS(acpi_get_handle(device->handle, "_DOS", &h_dummy2)))
  1466. return 0;
  1467. /* Does this device able to retrieve a video ROM ? */
  1468. if (ACPI_SUCCESS(acpi_get_handle(device->handle, "_ROM", &h_dummy1)))
  1469. return 0;
  1470. /* Does this device able to configure which video head to be POSTed ? */
  1471. if (ACPI_SUCCESS(acpi_get_handle(device->handle, "_VPO", &h_dummy1)) &&
  1472. ACPI_SUCCESS(acpi_get_handle(device->handle, "_GPD", &h_dummy2)) &&
  1473. ACPI_SUCCESS(acpi_get_handle(device->handle, "_SPD", &h_dummy3)))
  1474. return 0;
  1475. return -ENODEV;
  1476. }
  1477. static int __init acpi_video_init(void)
  1478. {
  1479. int result = 0;
  1480. /*
  1481. acpi_dbg_level = 0xFFFFFFFF;
  1482. acpi_dbg_layer = 0x08000000;
  1483. */
  1484. acpi_video_dir = proc_mkdir(ACPI_VIDEO_CLASS, acpi_root_dir);
  1485. if (!acpi_video_dir)
  1486. return -ENODEV;
  1487. acpi_video_dir->owner = THIS_MODULE;
  1488. result = acpi_bus_register_driver(&acpi_video_bus);
  1489. if (result < 0) {
  1490. remove_proc_entry(ACPI_VIDEO_CLASS, acpi_root_dir);
  1491. return -ENODEV;
  1492. }
  1493. return 0;
  1494. }
  1495. static void __exit acpi_video_exit(void)
  1496. {
  1497. acpi_bus_unregister_driver(&acpi_video_bus);
  1498. remove_proc_entry(ACPI_VIDEO_CLASS, acpi_root_dir);
  1499. return;
  1500. }
  1501. module_init(acpi_video_init);
  1502. module_exit(acpi_video_exit);