toshiba_acpi.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  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 (copy_from_user(tmp_buffer, buffer, count)) {
  226. result = -EFAULT;
  227. }
  228. else {
  229. tmp_buffer[count] = 0;
  230. result = item->write_func(tmp_buffer, count);
  231. }
  232. kfree(tmp_buffer);
  233. return result;
  234. }
  235. static char*
  236. read_lcd(char* p)
  237. {
  238. u32 hci_result;
  239. u32 value;
  240. hci_read1(HCI_LCD_BRIGHTNESS, &value, &hci_result);
  241. if (hci_result == HCI_SUCCESS) {
  242. value = value >> HCI_LCD_BRIGHTNESS_SHIFT;
  243. p += sprintf(p, "brightness: %d\n", value);
  244. p += sprintf(p, "brightness_levels: %d\n",
  245. HCI_LCD_BRIGHTNESS_LEVELS);
  246. } else {
  247. printk(MY_ERR "Error reading LCD brightness\n");
  248. }
  249. return p;
  250. }
  251. static unsigned long
  252. write_lcd(const char* buffer, unsigned long count)
  253. {
  254. int value;
  255. u32 hci_result;
  256. if (sscanf(buffer, " brightness : %i", &value) == 1 &&
  257. value >= 0 && value < HCI_LCD_BRIGHTNESS_LEVELS) {
  258. value = value << HCI_LCD_BRIGHTNESS_SHIFT;
  259. hci_write1(HCI_LCD_BRIGHTNESS, value, &hci_result);
  260. if (hci_result != HCI_SUCCESS)
  261. return -EFAULT;
  262. } else {
  263. return -EINVAL;
  264. }
  265. return count;
  266. }
  267. static char*
  268. read_video(char* p)
  269. {
  270. u32 hci_result;
  271. u32 value;
  272. hci_read1(HCI_VIDEO_OUT, &value, &hci_result);
  273. if (hci_result == HCI_SUCCESS) {
  274. int is_lcd = (value & HCI_VIDEO_OUT_LCD) ? 1 : 0;
  275. int is_crt = (value & HCI_VIDEO_OUT_CRT) ? 1 : 0;
  276. int is_tv = (value & HCI_VIDEO_OUT_TV ) ? 1 : 0;
  277. p += sprintf(p, "lcd_out: %d\n", is_lcd);
  278. p += sprintf(p, "crt_out: %d\n", is_crt);
  279. p += sprintf(p, "tv_out: %d\n", is_tv);
  280. } else {
  281. printk(MY_ERR "Error reading video out status\n");
  282. }
  283. return p;
  284. }
  285. static unsigned long
  286. write_video(const char* buffer, unsigned long count)
  287. {
  288. int value;
  289. int remain = count;
  290. int lcd_out = -1;
  291. int crt_out = -1;
  292. int tv_out = -1;
  293. u32 hci_result;
  294. int video_out;
  295. /* scan expression. Multiple expressions may be delimited with ;
  296. *
  297. * NOTE: to keep scanning simple, invalid fields are ignored
  298. */
  299. while (remain) {
  300. if (sscanf(buffer, " lcd_out : %i", &value) == 1)
  301. lcd_out = value & 1;
  302. else if (sscanf(buffer, " crt_out : %i", &value) == 1)
  303. crt_out = value & 1;
  304. else if (sscanf(buffer, " tv_out : %i", &value) == 1)
  305. tv_out = value & 1;
  306. /* advance to one character past the next ; */
  307. do {
  308. ++buffer;
  309. --remain;
  310. }
  311. while (remain && *(buffer-1) != ';');
  312. }
  313. hci_read1(HCI_VIDEO_OUT, &video_out, &hci_result);
  314. if (hci_result == HCI_SUCCESS) {
  315. int new_video_out = video_out;
  316. if (lcd_out != -1)
  317. _set_bit(&new_video_out, HCI_VIDEO_OUT_LCD, lcd_out);
  318. if (crt_out != -1)
  319. _set_bit(&new_video_out, HCI_VIDEO_OUT_CRT, crt_out);
  320. if (tv_out != -1)
  321. _set_bit(&new_video_out, HCI_VIDEO_OUT_TV, tv_out);
  322. /* To avoid unnecessary video disruption, only write the new
  323. * video setting if something changed. */
  324. if (new_video_out != video_out)
  325. write_acpi_int(METHOD_VIDEO_OUT, new_video_out);
  326. } else {
  327. return -EFAULT;
  328. }
  329. return count;
  330. }
  331. static char*
  332. read_fan(char* p)
  333. {
  334. u32 hci_result;
  335. u32 value;
  336. hci_read1(HCI_FAN, &value, &hci_result);
  337. if (hci_result == HCI_SUCCESS) {
  338. p += sprintf(p, "running: %d\n", (value > 0));
  339. p += sprintf(p, "force_on: %d\n", force_fan);
  340. } else {
  341. printk(MY_ERR "Error reading fan status\n");
  342. }
  343. return p;
  344. }
  345. static unsigned long
  346. write_fan(const char* buffer, unsigned long count)
  347. {
  348. int value;
  349. u32 hci_result;
  350. if (sscanf(buffer, " force_on : %i", &value) == 1 &&
  351. value >= 0 && value <= 1) {
  352. hci_write1(HCI_FAN, value, &hci_result);
  353. if (hci_result != HCI_SUCCESS)
  354. return -EFAULT;
  355. else
  356. force_fan = value;
  357. } else {
  358. return -EINVAL;
  359. }
  360. return count;
  361. }
  362. static char*
  363. read_keys(char* p)
  364. {
  365. u32 hci_result;
  366. u32 value;
  367. if (!key_event_valid) {
  368. hci_read1(HCI_SYSTEM_EVENT, &value, &hci_result);
  369. if (hci_result == HCI_SUCCESS) {
  370. key_event_valid = 1;
  371. last_key_event = value;
  372. } else if (hci_result == HCI_EMPTY) {
  373. /* better luck next time */
  374. } else if (hci_result == HCI_NOT_SUPPORTED) {
  375. /* This is a workaround for an unresolved issue on
  376. * some machines where system events sporadically
  377. * become disabled. */
  378. hci_write1(HCI_SYSTEM_EVENT, 1, &hci_result);
  379. printk(MY_NOTICE "Re-enabled hotkeys\n");
  380. } else {
  381. printk(MY_ERR "Error reading hotkey status\n");
  382. goto end;
  383. }
  384. }
  385. p += sprintf(p, "hotkey_ready: %d\n", key_event_valid);
  386. p += sprintf(p, "hotkey: 0x%04x\n", last_key_event);
  387. end:
  388. return p;
  389. }
  390. static unsigned long
  391. write_keys(const char* buffer, unsigned long count)
  392. {
  393. int value;
  394. if (sscanf(buffer, " hotkey_ready : %i", &value) == 1 &&
  395. value == 0) {
  396. key_event_valid = 0;
  397. } else {
  398. return -EINVAL;
  399. }
  400. return count;
  401. }
  402. static char*
  403. read_version(char* p)
  404. {
  405. p += sprintf(p, "driver: %s\n", TOSHIBA_ACPI_VERSION);
  406. p += sprintf(p, "proc_interface: %d\n",
  407. PROC_INTERFACE_VERSION);
  408. return p;
  409. }
  410. /* proc and module init
  411. */
  412. #define PROC_TOSHIBA "toshiba"
  413. static ProcItem proc_items[] =
  414. {
  415. { "lcd" , read_lcd , write_lcd },
  416. { "video" , read_video , write_video },
  417. { "fan" , read_fan , write_fan },
  418. { "keys" , read_keys , write_keys },
  419. { "version" , read_version , NULL },
  420. { NULL }
  421. };
  422. static acpi_status __init
  423. add_device(void)
  424. {
  425. struct proc_dir_entry* proc;
  426. ProcItem* item;
  427. for (item = proc_items; item->name; ++item)
  428. {
  429. proc = create_proc_read_entry(item->name,
  430. S_IFREG | S_IRUGO | S_IWUSR,
  431. toshiba_proc_dir, (read_proc_t*)dispatch_read, item);
  432. if (proc)
  433. proc->owner = THIS_MODULE;
  434. if (proc && item->write_func)
  435. proc->write_proc = (write_proc_t*)dispatch_write;
  436. }
  437. return AE_OK;
  438. }
  439. static acpi_status __exit
  440. remove_device(void)
  441. {
  442. ProcItem* item;
  443. for (item = proc_items; item->name; ++item)
  444. remove_proc_entry(item->name, toshiba_proc_dir);
  445. return AE_OK;
  446. }
  447. static int __init
  448. toshiba_acpi_init(void)
  449. {
  450. acpi_status status = AE_OK;
  451. u32 hci_result;
  452. if (acpi_disabled)
  453. return -ENODEV;
  454. /* simple device detection: look for HCI method */
  455. if (is_valid_acpi_path(METHOD_HCI_1))
  456. method_hci = METHOD_HCI_1;
  457. else if (is_valid_acpi_path(METHOD_HCI_2))
  458. method_hci = METHOD_HCI_2;
  459. else
  460. return -ENODEV;
  461. printk(MY_INFO "Toshiba Laptop ACPI Extras version %s\n",
  462. TOSHIBA_ACPI_VERSION);
  463. printk(MY_INFO " HCI method: %s\n", method_hci);
  464. force_fan = 0;
  465. key_event_valid = 0;
  466. /* enable event fifo */
  467. hci_write1(HCI_SYSTEM_EVENT, 1, &hci_result);
  468. toshiba_proc_dir = proc_mkdir(PROC_TOSHIBA, acpi_root_dir);
  469. if (!toshiba_proc_dir) {
  470. status = AE_ERROR;
  471. } else {
  472. toshiba_proc_dir->owner = THIS_MODULE;
  473. status = add_device();
  474. if (ACPI_FAILURE(status))
  475. remove_proc_entry(PROC_TOSHIBA, acpi_root_dir);
  476. }
  477. return (ACPI_SUCCESS(status)) ? 0 : -ENODEV;
  478. }
  479. static void __exit
  480. toshiba_acpi_exit(void)
  481. {
  482. remove_device();
  483. if (toshiba_proc_dir)
  484. remove_proc_entry(PROC_TOSHIBA, acpi_root_dir);
  485. return;
  486. }
  487. module_init(toshiba_acpi_init);
  488. module_exit(toshiba_acpi_exit);