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