video.c 47 KB

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