toshiba_acpi.c 14 KB

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