video.c 53 KB

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