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