video.c 45 KB

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