toshiba_acpi.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020
  1. /*
  2. * toshiba_acpi.c - Toshiba Laptop ACPI Extras
  3. *
  4. *
  5. * Copyright (C) 2002-2004 John Belmonte
  6. * Copyright (C) 2008 Philip Langdale
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. *
  22. *
  23. * The devolpment page for this driver is located at
  24. * http://memebeam.org/toys/ToshibaAcpiDriver.
  25. *
  26. * Credits:
  27. * Jonathan A. Buzzard - Toshiba HCI info, and critical tips on reverse
  28. * engineering the Windows drivers
  29. * Yasushi Nagato - changes for linux kernel 2.4 -> 2.5
  30. * Rob Miller - TV out and hotkeys help
  31. *
  32. *
  33. * TODO
  34. *
  35. */
  36. #define TOSHIBA_ACPI_VERSION "0.19"
  37. #define PROC_INTERFACE_VERSION 1
  38. #include <linux/kernel.h>
  39. #include <linux/module.h>
  40. #include <linux/init.h>
  41. #include <linux/types.h>
  42. #include <linux/proc_fs.h>
  43. #include <linux/seq_file.h>
  44. #include <linux/backlight.h>
  45. #include <linux/platform_device.h>
  46. #include <linux/rfkill.h>
  47. #include <linux/input.h>
  48. #include <linux/slab.h>
  49. #include <asm/uaccess.h>
  50. #include <acpi/acpi_drivers.h>
  51. MODULE_AUTHOR("John Belmonte");
  52. MODULE_DESCRIPTION("Toshiba Laptop ACPI Extras Driver");
  53. MODULE_LICENSE("GPL");
  54. #define MY_LOGPREFIX "toshiba_acpi: "
  55. #define MY_ERR KERN_ERR MY_LOGPREFIX
  56. #define MY_NOTICE KERN_NOTICE MY_LOGPREFIX
  57. #define MY_INFO KERN_INFO MY_LOGPREFIX
  58. /* Toshiba ACPI method paths */
  59. #define METHOD_LCD_BRIGHTNESS "\\_SB_.PCI0.VGA_.LCD_._BCM"
  60. #define TOSH_INTERFACE_1 "\\_SB_.VALD"
  61. #define TOSH_INTERFACE_2 "\\_SB_.VALZ"
  62. #define METHOD_VIDEO_OUT "\\_SB_.VALX.DSSX"
  63. #define GHCI_METHOD ".GHCI"
  64. /* Toshiba HCI interface definitions
  65. *
  66. * HCI is Toshiba's "Hardware Control Interface" which is supposed to
  67. * be uniform across all their models. Ideally we would just call
  68. * dedicated ACPI methods instead of using this primitive interface.
  69. * However the ACPI methods seem to be incomplete in some areas (for
  70. * example they allow setting, but not reading, the LCD brightness value),
  71. * so this is still useful.
  72. */
  73. #define HCI_WORDS 6
  74. /* operations */
  75. #define HCI_SET 0xff00
  76. #define HCI_GET 0xfe00
  77. /* return codes */
  78. #define HCI_SUCCESS 0x0000
  79. #define HCI_FAILURE 0x1000
  80. #define HCI_NOT_SUPPORTED 0x8000
  81. #define HCI_EMPTY 0x8c00
  82. /* registers */
  83. #define HCI_FAN 0x0004
  84. #define HCI_SYSTEM_EVENT 0x0016
  85. #define HCI_VIDEO_OUT 0x001c
  86. #define HCI_HOTKEY_EVENT 0x001e
  87. #define HCI_LCD_BRIGHTNESS 0x002a
  88. #define HCI_WIRELESS 0x0056
  89. /* field definitions */
  90. #define HCI_LCD_BRIGHTNESS_BITS 3
  91. #define HCI_LCD_BRIGHTNESS_SHIFT (16-HCI_LCD_BRIGHTNESS_BITS)
  92. #define HCI_LCD_BRIGHTNESS_LEVELS (1 << HCI_LCD_BRIGHTNESS_BITS)
  93. #define HCI_VIDEO_OUT_LCD 0x1
  94. #define HCI_VIDEO_OUT_CRT 0x2
  95. #define HCI_VIDEO_OUT_TV 0x4
  96. #define HCI_WIRELESS_KILL_SWITCH 0x01
  97. #define HCI_WIRELESS_BT_PRESENT 0x0f
  98. #define HCI_WIRELESS_BT_ATTACH 0x40
  99. #define HCI_WIRELESS_BT_POWER 0x80
  100. static const struct acpi_device_id toshiba_device_ids[] = {
  101. {"TOS6200", 0},
  102. {"TOS6208", 0},
  103. {"TOS1900", 0},
  104. {"", 0},
  105. };
  106. MODULE_DEVICE_TABLE(acpi, toshiba_device_ids);
  107. struct key_entry {
  108. char type;
  109. u16 code;
  110. u16 keycode;
  111. };
  112. enum {KE_KEY, KE_END};
  113. static struct key_entry toshiba_acpi_keymap[] = {
  114. {KE_KEY, 0x101, KEY_MUTE},
  115. {KE_KEY, 0x13b, KEY_COFFEE},
  116. {KE_KEY, 0x13c, KEY_BATTERY},
  117. {KE_KEY, 0x13d, KEY_SLEEP},
  118. {KE_KEY, 0x13e, KEY_SUSPEND},
  119. {KE_KEY, 0x13f, KEY_SWITCHVIDEOMODE},
  120. {KE_KEY, 0x140, KEY_BRIGHTNESSDOWN},
  121. {KE_KEY, 0x141, KEY_BRIGHTNESSUP},
  122. {KE_KEY, 0x142, KEY_WLAN},
  123. {KE_KEY, 0x143, KEY_PROG1},
  124. {KE_KEY, 0xb05, KEY_PROG2},
  125. {KE_KEY, 0xb06, KEY_WWW},
  126. {KE_KEY, 0xb07, KEY_MAIL},
  127. {KE_KEY, 0xb30, KEY_STOP},
  128. {KE_KEY, 0xb31, KEY_PREVIOUSSONG},
  129. {KE_KEY, 0xb32, KEY_NEXTSONG},
  130. {KE_KEY, 0xb33, KEY_PLAYPAUSE},
  131. {KE_KEY, 0xb5a, KEY_MEDIA},
  132. {KE_END, 0, 0},
  133. };
  134. /* utility
  135. */
  136. static __inline__ void _set_bit(u32 * word, u32 mask, int value)
  137. {
  138. *word = (*word & ~mask) | (mask * value);
  139. }
  140. /* acpi interface wrappers
  141. */
  142. static int is_valid_acpi_path(const char *methodName)
  143. {
  144. acpi_handle handle;
  145. acpi_status status;
  146. status = acpi_get_handle(NULL, (char *)methodName, &handle);
  147. return !ACPI_FAILURE(status);
  148. }
  149. static int write_acpi_int(const char *methodName, int val)
  150. {
  151. struct acpi_object_list params;
  152. union acpi_object in_objs[1];
  153. acpi_status status;
  154. params.count = ARRAY_SIZE(in_objs);
  155. params.pointer = in_objs;
  156. in_objs[0].type = ACPI_TYPE_INTEGER;
  157. in_objs[0].integer.value = val;
  158. status = acpi_evaluate_object(NULL, (char *)methodName, &params, NULL);
  159. return (status == AE_OK);
  160. }
  161. #if 0
  162. static int read_acpi_int(const char *methodName, int *pVal)
  163. {
  164. struct acpi_buffer results;
  165. union acpi_object out_objs[1];
  166. acpi_status status;
  167. results.length = sizeof(out_objs);
  168. results.pointer = out_objs;
  169. status = acpi_evaluate_object(0, (char *)methodName, 0, &results);
  170. *pVal = out_objs[0].integer.value;
  171. return (status == AE_OK) && (out_objs[0].type == ACPI_TYPE_INTEGER);
  172. }
  173. #endif
  174. static const char *method_hci /*= 0*/ ;
  175. /* Perform a raw HCI call. Here we don't care about input or output buffer
  176. * format.
  177. */
  178. static acpi_status hci_raw(const u32 in[HCI_WORDS], u32 out[HCI_WORDS])
  179. {
  180. struct acpi_object_list params;
  181. union acpi_object in_objs[HCI_WORDS];
  182. struct acpi_buffer results;
  183. union acpi_object out_objs[HCI_WORDS + 1];
  184. acpi_status status;
  185. int i;
  186. params.count = HCI_WORDS;
  187. params.pointer = in_objs;
  188. for (i = 0; i < HCI_WORDS; ++i) {
  189. in_objs[i].type = ACPI_TYPE_INTEGER;
  190. in_objs[i].integer.value = in[i];
  191. }
  192. results.length = sizeof(out_objs);
  193. results.pointer = out_objs;
  194. status = acpi_evaluate_object(NULL, (char *)method_hci, &params,
  195. &results);
  196. if ((status == AE_OK) && (out_objs->package.count <= HCI_WORDS)) {
  197. for (i = 0; i < out_objs->package.count; ++i) {
  198. out[i] = out_objs->package.elements[i].integer.value;
  199. }
  200. }
  201. return status;
  202. }
  203. /* common hci tasks (get or set one or two value)
  204. *
  205. * In addition to the ACPI status, the HCI system returns a result which
  206. * may be useful (such as "not supported").
  207. */
  208. static acpi_status hci_write1(u32 reg, u32 in1, u32 * result)
  209. {
  210. u32 in[HCI_WORDS] = { HCI_SET, reg, in1, 0, 0, 0 };
  211. u32 out[HCI_WORDS];
  212. acpi_status status = hci_raw(in, out);
  213. *result = (status == AE_OK) ? out[0] : HCI_FAILURE;
  214. return status;
  215. }
  216. static acpi_status hci_read1(u32 reg, u32 * out1, u32 * result)
  217. {
  218. u32 in[HCI_WORDS] = { HCI_GET, reg, 0, 0, 0, 0 };
  219. u32 out[HCI_WORDS];
  220. acpi_status status = hci_raw(in, out);
  221. *out1 = out[2];
  222. *result = (status == AE_OK) ? out[0] : HCI_FAILURE;
  223. return status;
  224. }
  225. static acpi_status hci_write2(u32 reg, u32 in1, u32 in2, u32 *result)
  226. {
  227. u32 in[HCI_WORDS] = { HCI_SET, reg, in1, in2, 0, 0 };
  228. u32 out[HCI_WORDS];
  229. acpi_status status = hci_raw(in, out);
  230. *result = (status == AE_OK) ? out[0] : HCI_FAILURE;
  231. return status;
  232. }
  233. static acpi_status hci_read2(u32 reg, u32 *out1, u32 *out2, u32 *result)
  234. {
  235. u32 in[HCI_WORDS] = { HCI_GET, reg, *out1, *out2, 0, 0 };
  236. u32 out[HCI_WORDS];
  237. acpi_status status = hci_raw(in, out);
  238. *out1 = out[2];
  239. *out2 = out[3];
  240. *result = (status == AE_OK) ? out[0] : HCI_FAILURE;
  241. return status;
  242. }
  243. struct toshiba_acpi_dev {
  244. struct platform_device *p_dev;
  245. struct rfkill *bt_rfk;
  246. struct input_dev *hotkey_dev;
  247. acpi_handle handle;
  248. const char *bt_name;
  249. struct mutex mutex;
  250. };
  251. static struct toshiba_acpi_dev toshiba_acpi = {
  252. .bt_name = "Toshiba Bluetooth",
  253. };
  254. /* Bluetooth rfkill handlers */
  255. static u32 hci_get_bt_present(bool *present)
  256. {
  257. u32 hci_result;
  258. u32 value, value2;
  259. value = 0;
  260. value2 = 0;
  261. hci_read2(HCI_WIRELESS, &value, &value2, &hci_result);
  262. if (hci_result == HCI_SUCCESS)
  263. *present = (value & HCI_WIRELESS_BT_PRESENT) ? true : false;
  264. return hci_result;
  265. }
  266. static u32 hci_get_radio_state(bool *radio_state)
  267. {
  268. u32 hci_result;
  269. u32 value, value2;
  270. value = 0;
  271. value2 = 0x0001;
  272. hci_read2(HCI_WIRELESS, &value, &value2, &hci_result);
  273. *radio_state = value & HCI_WIRELESS_KILL_SWITCH;
  274. return hci_result;
  275. }
  276. static int bt_rfkill_set_block(void *data, bool blocked)
  277. {
  278. struct toshiba_acpi_dev *dev = data;
  279. u32 result1, result2;
  280. u32 value;
  281. int err;
  282. bool radio_state;
  283. value = (blocked == false);
  284. mutex_lock(&dev->mutex);
  285. if (hci_get_radio_state(&radio_state) != HCI_SUCCESS) {
  286. err = -EBUSY;
  287. goto out;
  288. }
  289. if (!radio_state) {
  290. err = 0;
  291. goto out;
  292. }
  293. hci_write2(HCI_WIRELESS, value, HCI_WIRELESS_BT_POWER, &result1);
  294. hci_write2(HCI_WIRELESS, value, HCI_WIRELESS_BT_ATTACH, &result2);
  295. if (result1 != HCI_SUCCESS || result2 != HCI_SUCCESS)
  296. err = -EBUSY;
  297. else
  298. err = 0;
  299. out:
  300. mutex_unlock(&dev->mutex);
  301. return err;
  302. }
  303. static void bt_rfkill_poll(struct rfkill *rfkill, void *data)
  304. {
  305. bool new_rfk_state;
  306. bool value;
  307. u32 hci_result;
  308. struct toshiba_acpi_dev *dev = data;
  309. mutex_lock(&dev->mutex);
  310. hci_result = hci_get_radio_state(&value);
  311. if (hci_result != HCI_SUCCESS) {
  312. /* Can't do anything useful */
  313. mutex_unlock(&dev->mutex);
  314. return;
  315. }
  316. new_rfk_state = value;
  317. mutex_unlock(&dev->mutex);
  318. if (rfkill_set_hw_state(rfkill, !new_rfk_state))
  319. bt_rfkill_set_block(data, true);
  320. }
  321. static const struct rfkill_ops toshiba_rfk_ops = {
  322. .set_block = bt_rfkill_set_block,
  323. .poll = bt_rfkill_poll,
  324. };
  325. static struct proc_dir_entry *toshiba_proc_dir /*= 0*/ ;
  326. static struct backlight_device *toshiba_backlight_device;
  327. static int force_fan;
  328. static int last_key_event;
  329. static int key_event_valid;
  330. static int get_lcd(struct backlight_device *bd)
  331. {
  332. u32 hci_result;
  333. u32 value;
  334. hci_read1(HCI_LCD_BRIGHTNESS, &value, &hci_result);
  335. if (hci_result == HCI_SUCCESS) {
  336. return (value >> HCI_LCD_BRIGHTNESS_SHIFT);
  337. } else
  338. return -EFAULT;
  339. }
  340. static int lcd_proc_show(struct seq_file *m, void *v)
  341. {
  342. int value = get_lcd(NULL);
  343. if (value >= 0) {
  344. seq_printf(m, "brightness: %d\n", value);
  345. seq_printf(m, "brightness_levels: %d\n",
  346. HCI_LCD_BRIGHTNESS_LEVELS);
  347. } else {
  348. printk(MY_ERR "Error reading LCD brightness\n");
  349. }
  350. return 0;
  351. }
  352. static int lcd_proc_open(struct inode *inode, struct file *file)
  353. {
  354. return single_open(file, lcd_proc_show, NULL);
  355. }
  356. static int set_lcd(int value)
  357. {
  358. u32 hci_result;
  359. value = value << HCI_LCD_BRIGHTNESS_SHIFT;
  360. hci_write1(HCI_LCD_BRIGHTNESS, value, &hci_result);
  361. if (hci_result != HCI_SUCCESS)
  362. return -EFAULT;
  363. return 0;
  364. }
  365. static int set_lcd_status(struct backlight_device *bd)
  366. {
  367. return set_lcd(bd->props.brightness);
  368. }
  369. static ssize_t lcd_proc_write(struct file *file, const char __user *buf,
  370. size_t count, loff_t *pos)
  371. {
  372. char cmd[42];
  373. size_t len;
  374. int value;
  375. int ret;
  376. len = min(count, sizeof(cmd) - 1);
  377. if (copy_from_user(cmd, buf, len))
  378. return -EFAULT;
  379. cmd[len] = '\0';
  380. if (sscanf(cmd, " brightness : %i", &value) == 1 &&
  381. value >= 0 && value < HCI_LCD_BRIGHTNESS_LEVELS) {
  382. ret = set_lcd(value);
  383. if (ret == 0)
  384. ret = count;
  385. } else {
  386. ret = -EINVAL;
  387. }
  388. return ret;
  389. }
  390. static const struct file_operations lcd_proc_fops = {
  391. .owner = THIS_MODULE,
  392. .open = lcd_proc_open,
  393. .read = seq_read,
  394. .llseek = seq_lseek,
  395. .release = single_release,
  396. .write = lcd_proc_write,
  397. };
  398. static int video_proc_show(struct seq_file *m, void *v)
  399. {
  400. u32 hci_result;
  401. u32 value;
  402. hci_read1(HCI_VIDEO_OUT, &value, &hci_result);
  403. if (hci_result == HCI_SUCCESS) {
  404. int is_lcd = (value & HCI_VIDEO_OUT_LCD) ? 1 : 0;
  405. int is_crt = (value & HCI_VIDEO_OUT_CRT) ? 1 : 0;
  406. int is_tv = (value & HCI_VIDEO_OUT_TV) ? 1 : 0;
  407. seq_printf(m, "lcd_out: %d\n", is_lcd);
  408. seq_printf(m, "crt_out: %d\n", is_crt);
  409. seq_printf(m, "tv_out: %d\n", is_tv);
  410. } else {
  411. printk(MY_ERR "Error reading video out status\n");
  412. }
  413. return 0;
  414. }
  415. static int video_proc_open(struct inode *inode, struct file *file)
  416. {
  417. return single_open(file, video_proc_show, NULL);
  418. }
  419. static ssize_t video_proc_write(struct file *file, const char __user *buf,
  420. size_t count, loff_t *pos)
  421. {
  422. char *cmd, *buffer;
  423. int value;
  424. int remain = count;
  425. int lcd_out = -1;
  426. int crt_out = -1;
  427. int tv_out = -1;
  428. u32 hci_result;
  429. u32 video_out;
  430. cmd = kmalloc(count + 1, GFP_KERNEL);
  431. if (!cmd)
  432. return -ENOMEM;
  433. if (copy_from_user(cmd, buf, count)) {
  434. kfree(cmd);
  435. return -EFAULT;
  436. }
  437. cmd[count] = '\0';
  438. buffer = cmd;
  439. /* scan expression. Multiple expressions may be delimited with ;
  440. *
  441. * NOTE: to keep scanning simple, invalid fields are ignored
  442. */
  443. while (remain) {
  444. if (sscanf(buffer, " lcd_out : %i", &value) == 1)
  445. lcd_out = value & 1;
  446. else if (sscanf(buffer, " crt_out : %i", &value) == 1)
  447. crt_out = value & 1;
  448. else if (sscanf(buffer, " tv_out : %i", &value) == 1)
  449. tv_out = value & 1;
  450. /* advance to one character past the next ; */
  451. do {
  452. ++buffer;
  453. --remain;
  454. }
  455. while (remain && *(buffer - 1) != ';');
  456. }
  457. kfree(cmd);
  458. hci_read1(HCI_VIDEO_OUT, &video_out, &hci_result);
  459. if (hci_result == HCI_SUCCESS) {
  460. unsigned int new_video_out = video_out;
  461. if (lcd_out != -1)
  462. _set_bit(&new_video_out, HCI_VIDEO_OUT_LCD, lcd_out);
  463. if (crt_out != -1)
  464. _set_bit(&new_video_out, HCI_VIDEO_OUT_CRT, crt_out);
  465. if (tv_out != -1)
  466. _set_bit(&new_video_out, HCI_VIDEO_OUT_TV, tv_out);
  467. /* To avoid unnecessary video disruption, only write the new
  468. * video setting if something changed. */
  469. if (new_video_out != video_out)
  470. write_acpi_int(METHOD_VIDEO_OUT, new_video_out);
  471. } else {
  472. return -EFAULT;
  473. }
  474. return count;
  475. }
  476. static const struct file_operations video_proc_fops = {
  477. .owner = THIS_MODULE,
  478. .open = video_proc_open,
  479. .read = seq_read,
  480. .llseek = seq_lseek,
  481. .release = single_release,
  482. .write = video_proc_write,
  483. };
  484. static int fan_proc_show(struct seq_file *m, void *v)
  485. {
  486. u32 hci_result;
  487. u32 value;
  488. hci_read1(HCI_FAN, &value, &hci_result);
  489. if (hci_result == HCI_SUCCESS) {
  490. seq_printf(m, "running: %d\n", (value > 0));
  491. seq_printf(m, "force_on: %d\n", force_fan);
  492. } else {
  493. printk(MY_ERR "Error reading fan status\n");
  494. }
  495. return 0;
  496. }
  497. static int fan_proc_open(struct inode *inode, struct file *file)
  498. {
  499. return single_open(file, fan_proc_show, NULL);
  500. }
  501. static ssize_t fan_proc_write(struct file *file, const char __user *buf,
  502. size_t count, loff_t *pos)
  503. {
  504. char cmd[42];
  505. size_t len;
  506. int value;
  507. u32 hci_result;
  508. len = min(count, sizeof(cmd) - 1);
  509. if (copy_from_user(cmd, buf, len))
  510. return -EFAULT;
  511. cmd[len] = '\0';
  512. if (sscanf(cmd, " force_on : %i", &value) == 1 &&
  513. value >= 0 && value <= 1) {
  514. hci_write1(HCI_FAN, value, &hci_result);
  515. if (hci_result != HCI_SUCCESS)
  516. return -EFAULT;
  517. else
  518. force_fan = value;
  519. } else {
  520. return -EINVAL;
  521. }
  522. return count;
  523. }
  524. static const struct file_operations fan_proc_fops = {
  525. .owner = THIS_MODULE,
  526. .open = fan_proc_open,
  527. .read = seq_read,
  528. .llseek = seq_lseek,
  529. .release = single_release,
  530. .write = fan_proc_write,
  531. };
  532. static int keys_proc_show(struct seq_file *m, void *v)
  533. {
  534. u32 hci_result;
  535. u32 value;
  536. if (!key_event_valid) {
  537. hci_read1(HCI_SYSTEM_EVENT, &value, &hci_result);
  538. if (hci_result == HCI_SUCCESS) {
  539. key_event_valid = 1;
  540. last_key_event = value;
  541. } else if (hci_result == HCI_EMPTY) {
  542. /* better luck next time */
  543. } else if (hci_result == HCI_NOT_SUPPORTED) {
  544. /* This is a workaround for an unresolved issue on
  545. * some machines where system events sporadically
  546. * become disabled. */
  547. hci_write1(HCI_SYSTEM_EVENT, 1, &hci_result);
  548. printk(MY_NOTICE "Re-enabled hotkeys\n");
  549. } else {
  550. printk(MY_ERR "Error reading hotkey status\n");
  551. goto end;
  552. }
  553. }
  554. seq_printf(m, "hotkey_ready: %d\n", key_event_valid);
  555. seq_printf(m, "hotkey: 0x%04x\n", last_key_event);
  556. end:
  557. return 0;
  558. }
  559. static int keys_proc_open(struct inode *inode, struct file *file)
  560. {
  561. return single_open(file, keys_proc_show, NULL);
  562. }
  563. static ssize_t keys_proc_write(struct file *file, const char __user *buf,
  564. size_t count, loff_t *pos)
  565. {
  566. char cmd[42];
  567. size_t len;
  568. int value;
  569. len = min(count, sizeof(cmd) - 1);
  570. if (copy_from_user(cmd, buf, len))
  571. return -EFAULT;
  572. cmd[len] = '\0';
  573. if (sscanf(cmd, " hotkey_ready : %i", &value) == 1 && value == 0) {
  574. key_event_valid = 0;
  575. } else {
  576. return -EINVAL;
  577. }
  578. return count;
  579. }
  580. static const struct file_operations keys_proc_fops = {
  581. .owner = THIS_MODULE,
  582. .open = keys_proc_open,
  583. .read = seq_read,
  584. .llseek = seq_lseek,
  585. .release = single_release,
  586. .write = keys_proc_write,
  587. };
  588. static int version_proc_show(struct seq_file *m, void *v)
  589. {
  590. seq_printf(m, "driver: %s\n", TOSHIBA_ACPI_VERSION);
  591. seq_printf(m, "proc_interface: %d\n", PROC_INTERFACE_VERSION);
  592. return 0;
  593. }
  594. static int version_proc_open(struct inode *inode, struct file *file)
  595. {
  596. return single_open(file, version_proc_show, PDE(inode)->data);
  597. }
  598. static const struct file_operations version_proc_fops = {
  599. .owner = THIS_MODULE,
  600. .open = version_proc_open,
  601. .read = seq_read,
  602. .llseek = seq_lseek,
  603. .release = single_release,
  604. };
  605. /* proc and module init
  606. */
  607. #define PROC_TOSHIBA "toshiba"
  608. static acpi_status __init add_device(void)
  609. {
  610. proc_create("lcd", S_IRUGO | S_IWUSR, toshiba_proc_dir, &lcd_proc_fops);
  611. proc_create("video", S_IRUGO | S_IWUSR, toshiba_proc_dir, &video_proc_fops);
  612. proc_create("fan", S_IRUGO | S_IWUSR, toshiba_proc_dir, &fan_proc_fops);
  613. proc_create("keys", S_IRUGO | S_IWUSR, toshiba_proc_dir, &keys_proc_fops);
  614. proc_create("version", S_IRUGO, toshiba_proc_dir, &version_proc_fops);
  615. return AE_OK;
  616. }
  617. static acpi_status remove_device(void)
  618. {
  619. remove_proc_entry("lcd", toshiba_proc_dir);
  620. remove_proc_entry("video", toshiba_proc_dir);
  621. remove_proc_entry("fan", toshiba_proc_dir);
  622. remove_proc_entry("keys", toshiba_proc_dir);
  623. remove_proc_entry("version", toshiba_proc_dir);
  624. return AE_OK;
  625. }
  626. static struct backlight_ops toshiba_backlight_data = {
  627. .get_brightness = get_lcd,
  628. .update_status = set_lcd_status,
  629. };
  630. static struct key_entry *toshiba_acpi_get_entry_by_scancode(unsigned int code)
  631. {
  632. struct key_entry *key;
  633. for (key = toshiba_acpi_keymap; key->type != KE_END; key++)
  634. if (code == key->code)
  635. return key;
  636. return NULL;
  637. }
  638. static struct key_entry *toshiba_acpi_get_entry_by_keycode(unsigned int code)
  639. {
  640. struct key_entry *key;
  641. for (key = toshiba_acpi_keymap; key->type != KE_END; key++)
  642. if (code == key->keycode && key->type == KE_KEY)
  643. return key;
  644. return NULL;
  645. }
  646. static int toshiba_acpi_getkeycode(struct input_dev *dev,
  647. unsigned int scancode, unsigned int *keycode)
  648. {
  649. struct key_entry *key = toshiba_acpi_get_entry_by_scancode(scancode);
  650. if (key && key->type == KE_KEY) {
  651. *keycode = key->keycode;
  652. return 0;
  653. }
  654. return -EINVAL;
  655. }
  656. static int toshiba_acpi_setkeycode(struct input_dev *dev,
  657. unsigned int scancode, unsigned int keycode)
  658. {
  659. struct key_entry *key;
  660. unsigned int old_keycode;
  661. key = toshiba_acpi_get_entry_by_scancode(scancode);
  662. if (key && key->type == KE_KEY) {
  663. old_keycode = key->keycode;
  664. key->keycode = keycode;
  665. set_bit(keycode, dev->keybit);
  666. if (!toshiba_acpi_get_entry_by_keycode(old_keycode))
  667. clear_bit(old_keycode, dev->keybit);
  668. return 0;
  669. }
  670. return -EINVAL;
  671. }
  672. static void toshiba_acpi_notify(acpi_handle handle, u32 event, void *context)
  673. {
  674. u32 hci_result, value;
  675. struct key_entry *key;
  676. if (event != 0x80)
  677. return;
  678. do {
  679. hci_read1(HCI_SYSTEM_EVENT, &value, &hci_result);
  680. if (hci_result == HCI_SUCCESS) {
  681. if (value == 0x100)
  682. continue;
  683. /* act on key press; ignore key release */
  684. if (value & 0x80)
  685. continue;
  686. key = toshiba_acpi_get_entry_by_scancode
  687. (value);
  688. if (!key) {
  689. printk(MY_INFO "Unknown key %x\n",
  690. value);
  691. continue;
  692. }
  693. input_report_key(toshiba_acpi.hotkey_dev,
  694. key->keycode, 1);
  695. input_sync(toshiba_acpi.hotkey_dev);
  696. input_report_key(toshiba_acpi.hotkey_dev,
  697. key->keycode, 0);
  698. input_sync(toshiba_acpi.hotkey_dev);
  699. } else if (hci_result == HCI_NOT_SUPPORTED) {
  700. /* This is a workaround for an unresolved issue on
  701. * some machines where system events sporadically
  702. * become disabled. */
  703. hci_write1(HCI_SYSTEM_EVENT, 1, &hci_result);
  704. printk(MY_NOTICE "Re-enabled hotkeys\n");
  705. }
  706. } while (hci_result != HCI_EMPTY);
  707. }
  708. static int toshiba_acpi_setup_keyboard(char *device)
  709. {
  710. acpi_status status;
  711. acpi_handle handle;
  712. int result;
  713. const struct key_entry *key;
  714. status = acpi_get_handle(NULL, device, &handle);
  715. if (ACPI_FAILURE(status)) {
  716. printk(MY_INFO "Unable to get notification device\n");
  717. return -ENODEV;
  718. }
  719. toshiba_acpi.handle = handle;
  720. status = acpi_evaluate_object(handle, "ENAB", NULL, NULL);
  721. if (ACPI_FAILURE(status)) {
  722. printk(MY_INFO "Unable to enable hotkeys\n");
  723. return -ENODEV;
  724. }
  725. status = acpi_install_notify_handler(handle, ACPI_DEVICE_NOTIFY,
  726. toshiba_acpi_notify, NULL);
  727. if (ACPI_FAILURE(status)) {
  728. printk(MY_INFO "Unable to install hotkey notification\n");
  729. return -ENODEV;
  730. }
  731. toshiba_acpi.hotkey_dev = input_allocate_device();
  732. if (!toshiba_acpi.hotkey_dev) {
  733. printk(MY_INFO "Unable to register input device\n");
  734. return -ENOMEM;
  735. }
  736. toshiba_acpi.hotkey_dev->name = "Toshiba input device";
  737. toshiba_acpi.hotkey_dev->phys = device;
  738. toshiba_acpi.hotkey_dev->id.bustype = BUS_HOST;
  739. toshiba_acpi.hotkey_dev->getkeycode = toshiba_acpi_getkeycode;
  740. toshiba_acpi.hotkey_dev->setkeycode = toshiba_acpi_setkeycode;
  741. for (key = toshiba_acpi_keymap; key->type != KE_END; key++) {
  742. set_bit(EV_KEY, toshiba_acpi.hotkey_dev->evbit);
  743. set_bit(key->keycode, toshiba_acpi.hotkey_dev->keybit);
  744. }
  745. result = input_register_device(toshiba_acpi.hotkey_dev);
  746. if (result) {
  747. printk(MY_INFO "Unable to register input device\n");
  748. return result;
  749. }
  750. return 0;
  751. }
  752. static void toshiba_acpi_exit(void)
  753. {
  754. if (toshiba_acpi.hotkey_dev)
  755. input_unregister_device(toshiba_acpi.hotkey_dev);
  756. if (toshiba_acpi.bt_rfk) {
  757. rfkill_unregister(toshiba_acpi.bt_rfk);
  758. rfkill_destroy(toshiba_acpi.bt_rfk);
  759. }
  760. if (toshiba_backlight_device)
  761. backlight_device_unregister(toshiba_backlight_device);
  762. remove_device();
  763. if (toshiba_proc_dir)
  764. remove_proc_entry(PROC_TOSHIBA, acpi_root_dir);
  765. acpi_remove_notify_handler(toshiba_acpi.handle, ACPI_DEVICE_NOTIFY,
  766. toshiba_acpi_notify);
  767. platform_device_unregister(toshiba_acpi.p_dev);
  768. return;
  769. }
  770. static int __init toshiba_acpi_init(void)
  771. {
  772. acpi_status status = AE_OK;
  773. u32 hci_result;
  774. bool bt_present;
  775. int ret = 0;
  776. struct backlight_properties props;
  777. if (acpi_disabled)
  778. return -ENODEV;
  779. /* simple device detection: look for HCI method */
  780. if (is_valid_acpi_path(TOSH_INTERFACE_1 GHCI_METHOD)) {
  781. method_hci = TOSH_INTERFACE_1 GHCI_METHOD;
  782. if (toshiba_acpi_setup_keyboard(TOSH_INTERFACE_1))
  783. printk(MY_INFO "Unable to activate hotkeys\n");
  784. } else if (is_valid_acpi_path(TOSH_INTERFACE_2 GHCI_METHOD)) {
  785. method_hci = TOSH_INTERFACE_2 GHCI_METHOD;
  786. if (toshiba_acpi_setup_keyboard(TOSH_INTERFACE_2))
  787. printk(MY_INFO "Unable to activate hotkeys\n");
  788. } else
  789. return -ENODEV;
  790. printk(MY_INFO "Toshiba Laptop ACPI Extras version %s\n",
  791. TOSHIBA_ACPI_VERSION);
  792. printk(MY_INFO " HCI method: %s\n", method_hci);
  793. mutex_init(&toshiba_acpi.mutex);
  794. toshiba_acpi.p_dev = platform_device_register_simple("toshiba_acpi",
  795. -1, NULL, 0);
  796. if (IS_ERR(toshiba_acpi.p_dev)) {
  797. ret = PTR_ERR(toshiba_acpi.p_dev);
  798. printk(MY_ERR "unable to register platform device\n");
  799. toshiba_acpi.p_dev = NULL;
  800. toshiba_acpi_exit();
  801. return ret;
  802. }
  803. force_fan = 0;
  804. key_event_valid = 0;
  805. /* enable event fifo */
  806. hci_write1(HCI_SYSTEM_EVENT, 1, &hci_result);
  807. toshiba_proc_dir = proc_mkdir(PROC_TOSHIBA, acpi_root_dir);
  808. if (!toshiba_proc_dir) {
  809. toshiba_acpi_exit();
  810. return -ENODEV;
  811. } else {
  812. status = add_device();
  813. if (ACPI_FAILURE(status)) {
  814. toshiba_acpi_exit();
  815. return -ENODEV;
  816. }
  817. }
  818. props.max_brightness = HCI_LCD_BRIGHTNESS_LEVELS - 1;
  819. toshiba_backlight_device = backlight_device_register("toshiba",
  820. &toshiba_acpi.p_dev->dev,
  821. NULL,
  822. &toshiba_backlight_data,
  823. &props);
  824. if (IS_ERR(toshiba_backlight_device)) {
  825. ret = PTR_ERR(toshiba_backlight_device);
  826. printk(KERN_ERR "Could not register toshiba backlight device\n");
  827. toshiba_backlight_device = NULL;
  828. toshiba_acpi_exit();
  829. return ret;
  830. }
  831. /* Register rfkill switch for Bluetooth */
  832. if (hci_get_bt_present(&bt_present) == HCI_SUCCESS && bt_present) {
  833. toshiba_acpi.bt_rfk = rfkill_alloc(toshiba_acpi.bt_name,
  834. &toshiba_acpi.p_dev->dev,
  835. RFKILL_TYPE_BLUETOOTH,
  836. &toshiba_rfk_ops,
  837. &toshiba_acpi);
  838. if (!toshiba_acpi.bt_rfk) {
  839. printk(MY_ERR "unable to allocate rfkill device\n");
  840. toshiba_acpi_exit();
  841. return -ENOMEM;
  842. }
  843. ret = rfkill_register(toshiba_acpi.bt_rfk);
  844. if (ret) {
  845. printk(MY_ERR "unable to register rfkill device\n");
  846. rfkill_destroy(toshiba_acpi.bt_rfk);
  847. toshiba_acpi_exit();
  848. return ret;
  849. }
  850. }
  851. return 0;
  852. }
  853. module_init(toshiba_acpi_init);
  854. module_exit(toshiba_acpi_exit);