toshiba_acpi.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018
  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. else if (value & 0x80) {
  685. key = toshiba_acpi_get_entry_by_scancode
  686. (value & ~0x80);
  687. if (!key) {
  688. printk(MY_INFO "Unknown key %x\n",
  689. value & ~0x80);
  690. continue;
  691. }
  692. input_report_key(toshiba_acpi.hotkey_dev,
  693. key->keycode, 1);
  694. input_sync(toshiba_acpi.hotkey_dev);
  695. input_report_key(toshiba_acpi.hotkey_dev,
  696. key->keycode, 0);
  697. input_sync(toshiba_acpi.hotkey_dev);
  698. }
  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. if (acpi_disabled)
  777. return -ENODEV;
  778. /* simple device detection: look for HCI method */
  779. if (is_valid_acpi_path(TOSH_INTERFACE_1 GHCI_METHOD)) {
  780. method_hci = TOSH_INTERFACE_1 GHCI_METHOD;
  781. if (toshiba_acpi_setup_keyboard(TOSH_INTERFACE_1))
  782. printk(MY_INFO "Unable to activate hotkeys\n");
  783. } else if (is_valid_acpi_path(TOSH_INTERFACE_2 GHCI_METHOD)) {
  784. method_hci = TOSH_INTERFACE_2 GHCI_METHOD;
  785. if (toshiba_acpi_setup_keyboard(TOSH_INTERFACE_2))
  786. printk(MY_INFO "Unable to activate hotkeys\n");
  787. } else
  788. return -ENODEV;
  789. printk(MY_INFO "Toshiba Laptop ACPI Extras version %s\n",
  790. TOSHIBA_ACPI_VERSION);
  791. printk(MY_INFO " HCI method: %s\n", method_hci);
  792. mutex_init(&toshiba_acpi.mutex);
  793. toshiba_acpi.p_dev = platform_device_register_simple("toshiba_acpi",
  794. -1, NULL, 0);
  795. if (IS_ERR(toshiba_acpi.p_dev)) {
  796. ret = PTR_ERR(toshiba_acpi.p_dev);
  797. printk(MY_ERR "unable to register platform device\n");
  798. toshiba_acpi.p_dev = NULL;
  799. toshiba_acpi_exit();
  800. return ret;
  801. }
  802. force_fan = 0;
  803. key_event_valid = 0;
  804. /* enable event fifo */
  805. hci_write1(HCI_SYSTEM_EVENT, 1, &hci_result);
  806. toshiba_proc_dir = proc_mkdir(PROC_TOSHIBA, acpi_root_dir);
  807. if (!toshiba_proc_dir) {
  808. toshiba_acpi_exit();
  809. return -ENODEV;
  810. } else {
  811. status = add_device();
  812. if (ACPI_FAILURE(status)) {
  813. toshiba_acpi_exit();
  814. return -ENODEV;
  815. }
  816. }
  817. toshiba_backlight_device = backlight_device_register("toshiba",
  818. &toshiba_acpi.p_dev->dev,
  819. NULL,
  820. &toshiba_backlight_data);
  821. if (IS_ERR(toshiba_backlight_device)) {
  822. ret = PTR_ERR(toshiba_backlight_device);
  823. printk(KERN_ERR "Could not register toshiba backlight device\n");
  824. toshiba_backlight_device = NULL;
  825. toshiba_acpi_exit();
  826. return ret;
  827. }
  828. toshiba_backlight_device->props.max_brightness = HCI_LCD_BRIGHTNESS_LEVELS - 1;
  829. /* Register rfkill switch for Bluetooth */
  830. if (hci_get_bt_present(&bt_present) == HCI_SUCCESS && bt_present) {
  831. toshiba_acpi.bt_rfk = rfkill_alloc(toshiba_acpi.bt_name,
  832. &toshiba_acpi.p_dev->dev,
  833. RFKILL_TYPE_BLUETOOTH,
  834. &toshiba_rfk_ops,
  835. &toshiba_acpi);
  836. if (!toshiba_acpi.bt_rfk) {
  837. printk(MY_ERR "unable to allocate rfkill device\n");
  838. toshiba_acpi_exit();
  839. return -ENOMEM;
  840. }
  841. ret = rfkill_register(toshiba_acpi.bt_rfk);
  842. if (ret) {
  843. printk(MY_ERR "unable to register rfkill device\n");
  844. rfkill_destroy(toshiba_acpi.bt_rfk);
  845. toshiba_acpi_exit();
  846. return ret;
  847. }
  848. }
  849. return 0;
  850. }
  851. module_init(toshiba_acpi_init);
  852. module_exit(toshiba_acpi_exit);