toshiba_acpi.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  1. /*
  2. * toshiba_acpi.c - Toshiba Laptop ACPI Extras
  3. *
  4. *
  5. * Copyright (C) 2002-2004 John Belmonte
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. *
  21. *
  22. * The devolpment page for this driver is located at
  23. * http://memebeam.org/toys/ToshibaAcpiDriver.
  24. *
  25. * Credits:
  26. * Jonathan A. Buzzard - Toshiba HCI info, and critical tips on reverse
  27. * engineering the Windows drivers
  28. * Yasushi Nagato - changes for linux kernel 2.4 -> 2.5
  29. * Rob Miller - TV out and hotkeys help
  30. *
  31. *
  32. * TODO
  33. *
  34. */
  35. #define TOSHIBA_ACPI_VERSION "0.18"
  36. #define PROC_INTERFACE_VERSION 1
  37. #include <linux/kernel.h>
  38. #include <linux/module.h>
  39. #include <linux/init.h>
  40. #include <linux/types.h>
  41. #include <linux/proc_fs.h>
  42. #include <asm/uaccess.h>
  43. #include <acpi/acpi_drivers.h>
  44. MODULE_AUTHOR("John Belmonte");
  45. MODULE_DESCRIPTION("Toshiba Laptop ACPI Extras Driver");
  46. MODULE_LICENSE("GPL");
  47. #define MY_LOGPREFIX "toshiba_acpi: "
  48. #define MY_ERR KERN_ERR MY_LOGPREFIX
  49. #define MY_NOTICE KERN_NOTICE MY_LOGPREFIX
  50. #define MY_INFO KERN_INFO MY_LOGPREFIX
  51. /* Toshiba ACPI method paths */
  52. #define METHOD_LCD_BRIGHTNESS "\\_SB_.PCI0.VGA_.LCD_._BCM"
  53. #define METHOD_HCI_1 "\\_SB_.VALD.GHCI"
  54. #define METHOD_HCI_2 "\\_SB_.VALZ.GHCI"
  55. #define METHOD_VIDEO_OUT "\\_SB_.VALX.DSSX"
  56. /* Toshiba HCI interface definitions
  57. *
  58. * HCI is Toshiba's "Hardware Control Interface" which is supposed to
  59. * be uniform across all their models. Ideally we would just call
  60. * dedicated ACPI methods instead of using this primitive interface.
  61. * However the ACPI methods seem to be incomplete in some areas (for
  62. * example they allow setting, but not reading, the LCD brightness value),
  63. * so this is still useful.
  64. */
  65. #define HCI_WORDS 6
  66. /* operations */
  67. #define HCI_SET 0xff00
  68. #define HCI_GET 0xfe00
  69. /* return codes */
  70. #define HCI_SUCCESS 0x0000
  71. #define HCI_FAILURE 0x1000
  72. #define HCI_NOT_SUPPORTED 0x8000
  73. #define HCI_EMPTY 0x8c00
  74. /* registers */
  75. #define HCI_FAN 0x0004
  76. #define HCI_SYSTEM_EVENT 0x0016
  77. #define HCI_VIDEO_OUT 0x001c
  78. #define HCI_HOTKEY_EVENT 0x001e
  79. #define HCI_LCD_BRIGHTNESS 0x002a
  80. /* field definitions */
  81. #define HCI_LCD_BRIGHTNESS_BITS 3
  82. #define HCI_LCD_BRIGHTNESS_SHIFT (16-HCI_LCD_BRIGHTNESS_BITS)
  83. #define HCI_LCD_BRIGHTNESS_LEVELS (1 << HCI_LCD_BRIGHTNESS_BITS)
  84. #define HCI_VIDEO_OUT_LCD 0x1
  85. #define HCI_VIDEO_OUT_CRT 0x2
  86. #define HCI_VIDEO_OUT_TV 0x4
  87. /* utility
  88. */
  89. static __inline__ void
  90. _set_bit(u32* word, u32 mask, int value)
  91. {
  92. *word = (*word & ~mask) | (mask * value);
  93. }
  94. /* acpi interface wrappers
  95. */
  96. static int
  97. is_valid_acpi_path(const char* methodName)
  98. {
  99. acpi_handle handle;
  100. acpi_status status;
  101. status = acpi_get_handle(NULL, (char*)methodName, &handle);
  102. return !ACPI_FAILURE(status);
  103. }
  104. static int
  105. write_acpi_int(const char* methodName, int val)
  106. {
  107. struct acpi_object_list params;
  108. union acpi_object in_objs[1];
  109. acpi_status status;
  110. params.count = sizeof(in_objs)/sizeof(in_objs[0]);
  111. params.pointer = in_objs;
  112. in_objs[0].type = ACPI_TYPE_INTEGER;
  113. in_objs[0].integer.value = val;
  114. status = acpi_evaluate_object(NULL, (char*)methodName, &params, NULL);
  115. return (status == AE_OK);
  116. }
  117. #if 0
  118. static int
  119. read_acpi_int(const char* methodName, int* pVal)
  120. {
  121. struct acpi_buffer results;
  122. union acpi_object out_objs[1];
  123. acpi_status status;
  124. results.length = sizeof(out_objs);
  125. results.pointer = out_objs;
  126. status = acpi_evaluate_object(0, (char*)methodName, 0, &results);
  127. *pVal = out_objs[0].integer.value;
  128. return (status == AE_OK) && (out_objs[0].type == ACPI_TYPE_INTEGER);
  129. }
  130. #endif
  131. static const char* method_hci /*= 0*/;
  132. /* Perform a raw HCI call. Here we don't care about input or output buffer
  133. * format.
  134. */
  135. static acpi_status
  136. hci_raw(const u32 in[HCI_WORDS], u32 out[HCI_WORDS])
  137. {
  138. struct acpi_object_list params;
  139. union acpi_object in_objs[HCI_WORDS];
  140. struct acpi_buffer results;
  141. union acpi_object out_objs[HCI_WORDS+1];
  142. acpi_status status;
  143. int i;
  144. params.count = HCI_WORDS;
  145. params.pointer = in_objs;
  146. for (i = 0; i < HCI_WORDS; ++i) {
  147. in_objs[i].type = ACPI_TYPE_INTEGER;
  148. in_objs[i].integer.value = in[i];
  149. }
  150. results.length = sizeof(out_objs);
  151. results.pointer = out_objs;
  152. status = acpi_evaluate_object(NULL, (char*)method_hci, &params,
  153. &results);
  154. if ((status == AE_OK) && (out_objs->package.count <= HCI_WORDS)) {
  155. for (i = 0; i < out_objs->package.count; ++i) {
  156. out[i] = out_objs->package.elements[i].integer.value;
  157. }
  158. }
  159. return status;
  160. }
  161. /* common hci tasks (get or set one value)
  162. *
  163. * In addition to the ACPI status, the HCI system returns a result which
  164. * may be useful (such as "not supported").
  165. */
  166. static acpi_status
  167. hci_write1(u32 reg, u32 in1, u32* result)
  168. {
  169. u32 in[HCI_WORDS] = { HCI_SET, reg, in1, 0, 0, 0 };
  170. u32 out[HCI_WORDS];
  171. acpi_status status = hci_raw(in, out);
  172. *result = (status == AE_OK) ? out[0] : HCI_FAILURE;
  173. return status;
  174. }
  175. static acpi_status
  176. hci_read1(u32 reg, u32* out1, u32* result)
  177. {
  178. u32 in[HCI_WORDS] = { HCI_GET, reg, 0, 0, 0, 0 };
  179. u32 out[HCI_WORDS];
  180. acpi_status status = hci_raw(in, out);
  181. *out1 = out[2];
  182. *result = (status == AE_OK) ? out[0] : HCI_FAILURE;
  183. return status;
  184. }
  185. static struct proc_dir_entry* toshiba_proc_dir /*= 0*/;
  186. static int force_fan;
  187. static int last_key_event;
  188. static int key_event_valid;
  189. typedef struct _ProcItem
  190. {
  191. const char* name;
  192. char* (*read_func)(char*);
  193. unsigned long (*write_func)(const char*, unsigned long);
  194. } ProcItem;
  195. /* proc file handlers
  196. */
  197. static int
  198. dispatch_read(char* page, char** start, off_t off, int count, int* eof,
  199. ProcItem* item)
  200. {
  201. char* p = page;
  202. int len;
  203. if (off == 0)
  204. p = item->read_func(p);
  205. /* ISSUE: I don't understand this code */
  206. len = (p - page);
  207. if (len <= off+count) *eof = 1;
  208. *start = page + off;
  209. len -= off;
  210. if (len>count) len = count;
  211. if (len<0) len = 0;
  212. return len;
  213. }
  214. static int
  215. dispatch_write(struct file* file, const char __user * buffer,
  216. unsigned long count, ProcItem* item)
  217. {
  218. int result;
  219. char* tmp_buffer;
  220. /* Arg buffer points to userspace memory, which can't be accessed
  221. * directly. Since we're making a copy, zero-terminate the
  222. * destination so that sscanf can be used on it safely.
  223. */
  224. tmp_buffer = kmalloc(count + 1, GFP_KERNEL);
  225. if(!tmp_buffer)
  226. return -ENOMEM;
  227. if (copy_from_user(tmp_buffer, buffer, count)) {
  228. result = -EFAULT;
  229. }
  230. else {
  231. tmp_buffer[count] = 0;
  232. result = item->write_func(tmp_buffer, count);
  233. }
  234. kfree(tmp_buffer);
  235. return result;
  236. }
  237. static char*
  238. read_lcd(char* p)
  239. {
  240. u32 hci_result;
  241. u32 value;
  242. hci_read1(HCI_LCD_BRIGHTNESS, &value, &hci_result);
  243. if (hci_result == HCI_SUCCESS) {
  244. value = value >> HCI_LCD_BRIGHTNESS_SHIFT;
  245. p += sprintf(p, "brightness: %d\n", value);
  246. p += sprintf(p, "brightness_levels: %d\n",
  247. HCI_LCD_BRIGHTNESS_LEVELS);
  248. } else {
  249. printk(MY_ERR "Error reading LCD brightness\n");
  250. }
  251. return p;
  252. }
  253. static unsigned long
  254. write_lcd(const char* buffer, unsigned long count)
  255. {
  256. int value;
  257. u32 hci_result;
  258. if (sscanf(buffer, " brightness : %i", &value) == 1 &&
  259. value >= 0 && value < HCI_LCD_BRIGHTNESS_LEVELS) {
  260. value = value << HCI_LCD_BRIGHTNESS_SHIFT;
  261. hci_write1(HCI_LCD_BRIGHTNESS, value, &hci_result);
  262. if (hci_result != HCI_SUCCESS)
  263. return -EFAULT;
  264. } else {
  265. return -EINVAL;
  266. }
  267. return count;
  268. }
  269. static char*
  270. read_video(char* p)
  271. {
  272. u32 hci_result;
  273. u32 value;
  274. hci_read1(HCI_VIDEO_OUT, &value, &hci_result);
  275. if (hci_result == HCI_SUCCESS) {
  276. int is_lcd = (value & HCI_VIDEO_OUT_LCD) ? 1 : 0;
  277. int is_crt = (value & HCI_VIDEO_OUT_CRT) ? 1 : 0;
  278. int is_tv = (value & HCI_VIDEO_OUT_TV ) ? 1 : 0;
  279. p += sprintf(p, "lcd_out: %d\n", is_lcd);
  280. p += sprintf(p, "crt_out: %d\n", is_crt);
  281. p += sprintf(p, "tv_out: %d\n", is_tv);
  282. } else {
  283. printk(MY_ERR "Error reading video out status\n");
  284. }
  285. return p;
  286. }
  287. static unsigned long
  288. write_video(const char* buffer, unsigned long count)
  289. {
  290. int value;
  291. int remain = count;
  292. int lcd_out = -1;
  293. int crt_out = -1;
  294. int tv_out = -1;
  295. u32 hci_result;
  296. int video_out;
  297. /* scan expression. Multiple expressions may be delimited with ;
  298. *
  299. * NOTE: to keep scanning simple, invalid fields are ignored
  300. */
  301. while (remain) {
  302. if (sscanf(buffer, " lcd_out : %i", &value) == 1)
  303. lcd_out = value & 1;
  304. else if (sscanf(buffer, " crt_out : %i", &value) == 1)
  305. crt_out = value & 1;
  306. else if (sscanf(buffer, " tv_out : %i", &value) == 1)
  307. tv_out = value & 1;
  308. /* advance to one character past the next ; */
  309. do {
  310. ++buffer;
  311. --remain;
  312. }
  313. while (remain && *(buffer-1) != ';');
  314. }
  315. hci_read1(HCI_VIDEO_OUT, &video_out, &hci_result);
  316. if (hci_result == HCI_SUCCESS) {
  317. int new_video_out = video_out;
  318. if (lcd_out != -1)
  319. _set_bit(&new_video_out, HCI_VIDEO_OUT_LCD, lcd_out);
  320. if (crt_out != -1)
  321. _set_bit(&new_video_out, HCI_VIDEO_OUT_CRT, crt_out);
  322. if (tv_out != -1)
  323. _set_bit(&new_video_out, HCI_VIDEO_OUT_TV, tv_out);
  324. /* To avoid unnecessary video disruption, only write the new
  325. * video setting if something changed. */
  326. if (new_video_out != video_out)
  327. write_acpi_int(METHOD_VIDEO_OUT, new_video_out);
  328. } else {
  329. return -EFAULT;
  330. }
  331. return count;
  332. }
  333. static char*
  334. read_fan(char* p)
  335. {
  336. u32 hci_result;
  337. u32 value;
  338. hci_read1(HCI_FAN, &value, &hci_result);
  339. if (hci_result == HCI_SUCCESS) {
  340. p += sprintf(p, "running: %d\n", (value > 0));
  341. p += sprintf(p, "force_on: %d\n", force_fan);
  342. } else {
  343. printk(MY_ERR "Error reading fan status\n");
  344. }
  345. return p;
  346. }
  347. static unsigned long
  348. write_fan(const char* buffer, unsigned long count)
  349. {
  350. int value;
  351. u32 hci_result;
  352. if (sscanf(buffer, " force_on : %i", &value) == 1 &&
  353. value >= 0 && value <= 1) {
  354. hci_write1(HCI_FAN, value, &hci_result);
  355. if (hci_result != HCI_SUCCESS)
  356. return -EFAULT;
  357. else
  358. force_fan = value;
  359. } else {
  360. return -EINVAL;
  361. }
  362. return count;
  363. }
  364. static char*
  365. read_keys(char* p)
  366. {
  367. u32 hci_result;
  368. u32 value;
  369. if (!key_event_valid) {
  370. hci_read1(HCI_SYSTEM_EVENT, &value, &hci_result);
  371. if (hci_result == HCI_SUCCESS) {
  372. key_event_valid = 1;
  373. last_key_event = value;
  374. } else if (hci_result == HCI_EMPTY) {
  375. /* better luck next time */
  376. } else if (hci_result == HCI_NOT_SUPPORTED) {
  377. /* This is a workaround for an unresolved issue on
  378. * some machines where system events sporadically
  379. * become disabled. */
  380. hci_write1(HCI_SYSTEM_EVENT, 1, &hci_result);
  381. printk(MY_NOTICE "Re-enabled hotkeys\n");
  382. } else {
  383. printk(MY_ERR "Error reading hotkey status\n");
  384. goto end;
  385. }
  386. }
  387. p += sprintf(p, "hotkey_ready: %d\n", key_event_valid);
  388. p += sprintf(p, "hotkey: 0x%04x\n", last_key_event);
  389. end:
  390. return p;
  391. }
  392. static unsigned long
  393. write_keys(const char* buffer, unsigned long count)
  394. {
  395. int value;
  396. if (sscanf(buffer, " hotkey_ready : %i", &value) == 1 &&
  397. value == 0) {
  398. key_event_valid = 0;
  399. } else {
  400. return -EINVAL;
  401. }
  402. return count;
  403. }
  404. static char*
  405. read_version(char* p)
  406. {
  407. p += sprintf(p, "driver: %s\n", TOSHIBA_ACPI_VERSION);
  408. p += sprintf(p, "proc_interface: %d\n",
  409. PROC_INTERFACE_VERSION);
  410. return p;
  411. }
  412. /* proc and module init
  413. */
  414. #define PROC_TOSHIBA "toshiba"
  415. static ProcItem proc_items[] =
  416. {
  417. { "lcd" , read_lcd , write_lcd },
  418. { "video" , read_video , write_video },
  419. { "fan" , read_fan , write_fan },
  420. { "keys" , read_keys , write_keys },
  421. { "version" , read_version , NULL },
  422. { NULL }
  423. };
  424. static acpi_status __init
  425. add_device(void)
  426. {
  427. struct proc_dir_entry* proc;
  428. ProcItem* item;
  429. for (item = proc_items; item->name; ++item)
  430. {
  431. proc = create_proc_read_entry(item->name,
  432. S_IFREG | S_IRUGO | S_IWUSR,
  433. toshiba_proc_dir, (read_proc_t*)dispatch_read, item);
  434. if (proc)
  435. proc->owner = THIS_MODULE;
  436. if (proc && item->write_func)
  437. proc->write_proc = (write_proc_t*)dispatch_write;
  438. }
  439. return AE_OK;
  440. }
  441. static acpi_status __exit
  442. remove_device(void)
  443. {
  444. ProcItem* item;
  445. for (item = proc_items; item->name; ++item)
  446. remove_proc_entry(item->name, toshiba_proc_dir);
  447. return AE_OK;
  448. }
  449. static int __init
  450. toshiba_acpi_init(void)
  451. {
  452. acpi_status status = AE_OK;
  453. u32 hci_result;
  454. if (acpi_disabled)
  455. return -ENODEV;
  456. if (!acpi_specific_hotkey_enabled){
  457. printk(MY_INFO "Using generic hotkey driver\n");
  458. return -ENODEV;
  459. }
  460. /* simple device detection: look for HCI method */
  461. if (is_valid_acpi_path(METHOD_HCI_1))
  462. method_hci = METHOD_HCI_1;
  463. else if (is_valid_acpi_path(METHOD_HCI_2))
  464. method_hci = METHOD_HCI_2;
  465. else
  466. return -ENODEV;
  467. printk(MY_INFO "Toshiba Laptop ACPI Extras version %s\n",
  468. TOSHIBA_ACPI_VERSION);
  469. printk(MY_INFO " HCI method: %s\n", method_hci);
  470. force_fan = 0;
  471. key_event_valid = 0;
  472. /* enable event fifo */
  473. hci_write1(HCI_SYSTEM_EVENT, 1, &hci_result);
  474. toshiba_proc_dir = proc_mkdir(PROC_TOSHIBA, acpi_root_dir);
  475. if (!toshiba_proc_dir) {
  476. status = AE_ERROR;
  477. } else {
  478. toshiba_proc_dir->owner = THIS_MODULE;
  479. status = add_device();
  480. if (ACPI_FAILURE(status))
  481. remove_proc_entry(PROC_TOSHIBA, acpi_root_dir);
  482. }
  483. return (ACPI_SUCCESS(status)) ? 0 : -ENODEV;
  484. }
  485. static void __exit
  486. toshiba_acpi_exit(void)
  487. {
  488. remove_device();
  489. if (toshiba_proc_dir)
  490. remove_proc_entry(PROC_TOSHIBA, acpi_root_dir);
  491. return;
  492. }
  493. module_init(toshiba_acpi_init);
  494. module_exit(toshiba_acpi_exit);