video.c 53 KB

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