toshiba_acpi.c 15 KB

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