toshiba_acpi.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  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 _set_bit(u32 * word, u32 mask, int value)
  90. {
  91. *word = (*word & ~mask) | (mask * value);
  92. }
  93. /* acpi interface wrappers
  94. */
  95. static int is_valid_acpi_path(const char *methodName)
  96. {
  97. acpi_handle handle;
  98. acpi_status status;
  99. status = acpi_get_handle(NULL, (char *)methodName, &handle);
  100. return !ACPI_FAILURE(status);
  101. }
  102. static int write_acpi_int(const char *methodName, int val)
  103. {
  104. struct acpi_object_list params;
  105. union acpi_object in_objs[1];
  106. acpi_status status;
  107. params.count = sizeof(in_objs) / sizeof(in_objs[0]);
  108. params.pointer = in_objs;
  109. in_objs[0].type = ACPI_TYPE_INTEGER;
  110. in_objs[0].integer.value = val;
  111. status = acpi_evaluate_object(NULL, (char *)methodName, &params, NULL);
  112. return (status == AE_OK);
  113. }
  114. #if 0
  115. static int read_acpi_int(const char *methodName, int *pVal)
  116. {
  117. struct acpi_buffer results;
  118. union acpi_object out_objs[1];
  119. acpi_status status;
  120. results.length = sizeof(out_objs);
  121. results.pointer = out_objs;
  122. status = acpi_evaluate_object(0, (char *)methodName, 0, &results);
  123. *pVal = out_objs[0].integer.value;
  124. return (status == AE_OK) && (out_objs[0].type == ACPI_TYPE_INTEGER);
  125. }
  126. #endif
  127. static const char *method_hci /*= 0*/ ;
  128. /* Perform a raw HCI call. Here we don't care about input or output buffer
  129. * format.
  130. */
  131. static acpi_status hci_raw(const u32 in[HCI_WORDS], u32 out[HCI_WORDS])
  132. {
  133. struct acpi_object_list params;
  134. union acpi_object in_objs[HCI_WORDS];
  135. struct acpi_buffer results;
  136. union acpi_object out_objs[HCI_WORDS + 1];
  137. acpi_status status;
  138. int i;
  139. params.count = HCI_WORDS;
  140. params.pointer = in_objs;
  141. for (i = 0; i < HCI_WORDS; ++i) {
  142. in_objs[i].type = ACPI_TYPE_INTEGER;
  143. in_objs[i].integer.value = in[i];
  144. }
  145. results.length = sizeof(out_objs);
  146. results.pointer = out_objs;
  147. status = acpi_evaluate_object(NULL, (char *)method_hci, &params,
  148. &results);
  149. if ((status == AE_OK) && (out_objs->package.count <= HCI_WORDS)) {
  150. for (i = 0; i < out_objs->package.count; ++i) {
  151. out[i] = out_objs->package.elements[i].integer.value;
  152. }
  153. }
  154. return status;
  155. }
  156. /* common hci tasks (get or set one value)
  157. *
  158. * In addition to the ACPI status, the HCI system returns a result which
  159. * may be useful (such as "not supported").
  160. */
  161. static acpi_status hci_write1(u32 reg, u32 in1, u32 * result)
  162. {
  163. u32 in[HCI_WORDS] = { HCI_SET, reg, in1, 0, 0, 0 };
  164. u32 out[HCI_WORDS];
  165. acpi_status status = hci_raw(in, out);
  166. *result = (status == AE_OK) ? out[0] : HCI_FAILURE;
  167. return status;
  168. }
  169. static acpi_status hci_read1(u32 reg, u32 * out1, u32 * result)
  170. {
  171. u32 in[HCI_WORDS] = { HCI_GET, reg, 0, 0, 0, 0 };
  172. u32 out[HCI_WORDS];
  173. acpi_status status = hci_raw(in, out);
  174. *out1 = out[2];
  175. *result = (status == AE_OK) ? out[0] : HCI_FAILURE;
  176. return status;
  177. }
  178. static struct proc_dir_entry *toshiba_proc_dir /*= 0*/ ;
  179. static int force_fan;
  180. static int last_key_event;
  181. static int key_event_valid;
  182. typedef struct _ProcItem {
  183. const char *name;
  184. char *(*read_func) (char *);
  185. unsigned long (*write_func) (const char *, unsigned long);
  186. } ProcItem;
  187. /* proc file handlers
  188. */
  189. static int
  190. dispatch_read(char *page, char **start, off_t off, int count, int *eof,
  191. ProcItem * item)
  192. {
  193. char *p = page;
  194. int len;
  195. if (off == 0)
  196. p = item->read_func(p);
  197. /* ISSUE: I don't understand this code */
  198. len = (p - page);
  199. if (len <= off + count)
  200. *eof = 1;
  201. *start = page + off;
  202. len -= off;
  203. if (len > count)
  204. len = count;
  205. if (len < 0)
  206. len = 0;
  207. return len;
  208. }
  209. static int
  210. dispatch_write(struct file *file, const char __user * buffer,
  211. unsigned long count, ProcItem * item)
  212. {
  213. int result;
  214. char *tmp_buffer;
  215. /* Arg buffer points to userspace memory, which can't be accessed
  216. * directly. Since we're making a copy, zero-terminate the
  217. * destination so that sscanf can be used on it safely.
  218. */
  219. tmp_buffer = kmalloc(count + 1, GFP_KERNEL);
  220. if (!tmp_buffer)
  221. return -ENOMEM;
  222. if (copy_from_user(tmp_buffer, buffer, count)) {
  223. result = -EFAULT;
  224. } else {
  225. tmp_buffer[count] = 0;
  226. result = item->write_func(tmp_buffer, count);
  227. }
  228. kfree(tmp_buffer);
  229. return result;
  230. }
  231. static char *read_lcd(char *p)
  232. {
  233. u32 hci_result;
  234. u32 value;
  235. hci_read1(HCI_LCD_BRIGHTNESS, &value, &hci_result);
  236. if (hci_result == HCI_SUCCESS) {
  237. value = value >> HCI_LCD_BRIGHTNESS_SHIFT;
  238. p += sprintf(p, "brightness: %d\n", value);
  239. p += sprintf(p, "brightness_levels: %d\n",
  240. HCI_LCD_BRIGHTNESS_LEVELS);
  241. } else {
  242. printk(MY_ERR "Error reading LCD brightness\n");
  243. }
  244. return p;
  245. }
  246. static unsigned long write_lcd(const char *buffer, unsigned long count)
  247. {
  248. int value;
  249. u32 hci_result;
  250. if (sscanf(buffer, " brightness : %i", &value) == 1 &&
  251. value >= 0 && value < HCI_LCD_BRIGHTNESS_LEVELS) {
  252. value = value << HCI_LCD_BRIGHTNESS_SHIFT;
  253. hci_write1(HCI_LCD_BRIGHTNESS, value, &hci_result);
  254. if (hci_result != HCI_SUCCESS)
  255. return -EFAULT;
  256. } else {
  257. return -EINVAL;
  258. }
  259. return count;
  260. }
  261. static char *read_video(char *p)
  262. {
  263. u32 hci_result;
  264. u32 value;
  265. hci_read1(HCI_VIDEO_OUT, &value, &hci_result);
  266. if (hci_result == HCI_SUCCESS) {
  267. int is_lcd = (value & HCI_VIDEO_OUT_LCD) ? 1 : 0;
  268. int is_crt = (value & HCI_VIDEO_OUT_CRT) ? 1 : 0;
  269. int is_tv = (value & HCI_VIDEO_OUT_TV) ? 1 : 0;
  270. p += sprintf(p, "lcd_out: %d\n", is_lcd);
  271. p += sprintf(p, "crt_out: %d\n", is_crt);
  272. p += sprintf(p, "tv_out: %d\n", is_tv);
  273. } else {
  274. printk(MY_ERR "Error reading video out status\n");
  275. }
  276. return p;
  277. }
  278. static unsigned long write_video(const char *buffer, unsigned long count)
  279. {
  280. int value;
  281. int remain = count;
  282. int lcd_out = -1;
  283. int crt_out = -1;
  284. int tv_out = -1;
  285. u32 hci_result;
  286. int video_out;
  287. /* scan expression. Multiple expressions may be delimited with ;
  288. *
  289. * NOTE: to keep scanning simple, invalid fields are ignored
  290. */
  291. while (remain) {
  292. if (sscanf(buffer, " lcd_out : %i", &value) == 1)
  293. lcd_out = value & 1;
  294. else if (sscanf(buffer, " crt_out : %i", &value) == 1)
  295. crt_out = value & 1;
  296. else if (sscanf(buffer, " tv_out : %i", &value) == 1)
  297. tv_out = value & 1;
  298. /* advance to one character past the next ; */
  299. do {
  300. ++buffer;
  301. --remain;
  302. }
  303. while (remain && *(buffer - 1) != ';');
  304. }
  305. hci_read1(HCI_VIDEO_OUT, &video_out, &hci_result);
  306. if (hci_result == HCI_SUCCESS) {
  307. int new_video_out = video_out;
  308. if (lcd_out != -1)
  309. _set_bit(&new_video_out, HCI_VIDEO_OUT_LCD, lcd_out);
  310. if (crt_out != -1)
  311. _set_bit(&new_video_out, HCI_VIDEO_OUT_CRT, crt_out);
  312. if (tv_out != -1)
  313. _set_bit(&new_video_out, HCI_VIDEO_OUT_TV, tv_out);
  314. /* To avoid unnecessary video disruption, only write the new
  315. * video setting if something changed. */
  316. if (new_video_out != video_out)
  317. write_acpi_int(METHOD_VIDEO_OUT, new_video_out);
  318. } else {
  319. return -EFAULT;
  320. }
  321. return count;
  322. }
  323. static char *read_fan(char *p)
  324. {
  325. u32 hci_result;
  326. u32 value;
  327. hci_read1(HCI_FAN, &value, &hci_result);
  328. if (hci_result == HCI_SUCCESS) {
  329. p += sprintf(p, "running: %d\n", (value > 0));
  330. p += sprintf(p, "force_on: %d\n", force_fan);
  331. } else {
  332. printk(MY_ERR "Error reading fan status\n");
  333. }
  334. return p;
  335. }
  336. static unsigned long write_fan(const char *buffer, unsigned long count)
  337. {
  338. int value;
  339. u32 hci_result;
  340. if (sscanf(buffer, " force_on : %i", &value) == 1 &&
  341. value >= 0 && value <= 1) {
  342. hci_write1(HCI_FAN, value, &hci_result);
  343. if (hci_result != HCI_SUCCESS)
  344. return -EFAULT;
  345. else
  346. force_fan = value;
  347. } else {
  348. return -EINVAL;
  349. }
  350. return count;
  351. }
  352. static char *read_keys(char *p)
  353. {
  354. u32 hci_result;
  355. u32 value;
  356. if (!key_event_valid) {
  357. hci_read1(HCI_SYSTEM_EVENT, &value, &hci_result);
  358. if (hci_result == HCI_SUCCESS) {
  359. key_event_valid = 1;
  360. last_key_event = value;
  361. } else if (hci_result == HCI_EMPTY) {
  362. /* better luck next time */
  363. } else if (hci_result == HCI_NOT_SUPPORTED) {
  364. /* This is a workaround for an unresolved issue on
  365. * some machines where system events sporadically
  366. * become disabled. */
  367. hci_write1(HCI_SYSTEM_EVENT, 1, &hci_result);
  368. printk(MY_NOTICE "Re-enabled hotkeys\n");
  369. } else {
  370. printk(MY_ERR "Error reading hotkey status\n");
  371. goto end;
  372. }
  373. }
  374. p += sprintf(p, "hotkey_ready: %d\n", key_event_valid);
  375. p += sprintf(p, "hotkey: 0x%04x\n", last_key_event);
  376. end:
  377. return p;
  378. }
  379. static unsigned long write_keys(const char *buffer, unsigned long count)
  380. {
  381. int value;
  382. if (sscanf(buffer, " hotkey_ready : %i", &value) == 1 && value == 0) {
  383. key_event_valid = 0;
  384. } else {
  385. return -EINVAL;
  386. }
  387. return count;
  388. }
  389. static char *read_version(char *p)
  390. {
  391. p += sprintf(p, "driver: %s\n", TOSHIBA_ACPI_VERSION);
  392. p += sprintf(p, "proc_interface: %d\n",
  393. PROC_INTERFACE_VERSION);
  394. return p;
  395. }
  396. /* proc and module init
  397. */
  398. #define PROC_TOSHIBA "toshiba"
  399. static ProcItem proc_items[] = {
  400. {"lcd", read_lcd, write_lcd},
  401. {"video", read_video, write_video},
  402. {"fan", read_fan, write_fan},
  403. {"keys", read_keys, write_keys},
  404. {"version", read_version, NULL},
  405. {NULL}
  406. };
  407. static acpi_status __init add_device(void)
  408. {
  409. struct proc_dir_entry *proc;
  410. ProcItem *item;
  411. for (item = proc_items; item->name; ++item) {
  412. proc = create_proc_read_entry(item->name,
  413. S_IFREG | S_IRUGO | S_IWUSR,
  414. toshiba_proc_dir,
  415. (read_proc_t *) dispatch_read,
  416. item);
  417. if (proc)
  418. proc->owner = THIS_MODULE;
  419. if (proc && item->write_func)
  420. proc->write_proc = (write_proc_t *) dispatch_write;
  421. }
  422. return AE_OK;
  423. }
  424. static acpi_status __exit remove_device(void)
  425. {
  426. ProcItem *item;
  427. for (item = proc_items; item->name; ++item)
  428. remove_proc_entry(item->name, toshiba_proc_dir);
  429. return AE_OK;
  430. }
  431. static int __init toshiba_acpi_init(void)
  432. {
  433. acpi_status status = AE_OK;
  434. u32 hci_result;
  435. if (acpi_disabled)
  436. return -ENODEV;
  437. if (!acpi_specific_hotkey_enabled) {
  438. printk(MY_INFO "Using generic hotkey driver\n");
  439. return -ENODEV;
  440. }
  441. /* simple device detection: look for HCI method */
  442. if (is_valid_acpi_path(METHOD_HCI_1))
  443. method_hci = METHOD_HCI_1;
  444. else if (is_valid_acpi_path(METHOD_HCI_2))
  445. method_hci = METHOD_HCI_2;
  446. else
  447. return -ENODEV;
  448. printk(MY_INFO "Toshiba Laptop ACPI Extras version %s\n",
  449. TOSHIBA_ACPI_VERSION);
  450. printk(MY_INFO " HCI method: %s\n", method_hci);
  451. force_fan = 0;
  452. key_event_valid = 0;
  453. /* enable event fifo */
  454. hci_write1(HCI_SYSTEM_EVENT, 1, &hci_result);
  455. toshiba_proc_dir = proc_mkdir(PROC_TOSHIBA, acpi_root_dir);
  456. if (!toshiba_proc_dir) {
  457. status = AE_ERROR;
  458. } else {
  459. toshiba_proc_dir->owner = THIS_MODULE;
  460. status = add_device();
  461. if (ACPI_FAILURE(status))
  462. remove_proc_entry(PROC_TOSHIBA, acpi_root_dir);
  463. }
  464. return (ACPI_SUCCESS(status)) ? 0 : -ENODEV;
  465. }
  466. static void __exit toshiba_acpi_exit(void)
  467. {
  468. remove_device();
  469. if (toshiba_proc_dir)
  470. remove_proc_entry(PROC_TOSHIBA, acpi_root_dir);
  471. return;
  472. }
  473. module_init(toshiba_acpi_init);
  474. module_exit(toshiba_acpi_exit);