toshiba_acpi.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069
  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. * Copyright (C) 2010 Pierre Ducroquet
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  22. *
  23. *
  24. * The devolpment page for this driver is located at
  25. * http://memebeam.org/toys/ToshibaAcpiDriver.
  26. *
  27. * Credits:
  28. * Jonathan A. Buzzard - Toshiba HCI info, and critical tips on reverse
  29. * engineering the Windows drivers
  30. * Yasushi Nagato - changes for linux kernel 2.4 -> 2.5
  31. * Rob Miller - TV out and hotkeys help
  32. *
  33. *
  34. * TODO
  35. *
  36. */
  37. #define TOSHIBA_ACPI_VERSION "0.19"
  38. #define PROC_INTERFACE_VERSION 1
  39. #include <linux/kernel.h>
  40. #include <linux/module.h>
  41. #include <linux/init.h>
  42. #include <linux/types.h>
  43. #include <linux/proc_fs.h>
  44. #include <linux/seq_file.h>
  45. #include <linux/backlight.h>
  46. #include <linux/platform_device.h>
  47. #include <linux/rfkill.h>
  48. #include <linux/input.h>
  49. #include <linux/input/sparse-keymap.h>
  50. #include <linux/leds.h>
  51. #include <linux/slab.h>
  52. #include <asm/uaccess.h>
  53. #include <acpi/acpi_drivers.h>
  54. MODULE_AUTHOR("John Belmonte");
  55. MODULE_DESCRIPTION("Toshiba Laptop ACPI Extras Driver");
  56. MODULE_LICENSE("GPL");
  57. #define MY_LOGPREFIX "toshiba_acpi: "
  58. #define MY_ERR KERN_ERR MY_LOGPREFIX
  59. #define MY_NOTICE KERN_NOTICE MY_LOGPREFIX
  60. #define MY_INFO KERN_INFO MY_LOGPREFIX
  61. /* Toshiba ACPI method paths */
  62. #define METHOD_LCD_BRIGHTNESS "\\_SB_.PCI0.VGA_.LCD_._BCM"
  63. #define TOSH_INTERFACE_1 "\\_SB_.VALD"
  64. #define TOSH_INTERFACE_2 "\\_SB_.VALZ"
  65. #define METHOD_VIDEO_OUT "\\_SB_.VALX.DSSX"
  66. #define GHCI_METHOD ".GHCI"
  67. /* Toshiba HCI interface definitions
  68. *
  69. * HCI is Toshiba's "Hardware Control Interface" which is supposed to
  70. * be uniform across all their models. Ideally we would just call
  71. * dedicated ACPI methods instead of using this primitive interface.
  72. * However the ACPI methods seem to be incomplete in some areas (for
  73. * example they allow setting, but not reading, the LCD brightness value),
  74. * so this is still useful.
  75. */
  76. #define HCI_WORDS 6
  77. /* operations */
  78. #define HCI_SET 0xff00
  79. #define HCI_GET 0xfe00
  80. /* return codes */
  81. #define HCI_SUCCESS 0x0000
  82. #define HCI_FAILURE 0x1000
  83. #define HCI_NOT_SUPPORTED 0x8000
  84. #define HCI_EMPTY 0x8c00
  85. /* registers */
  86. #define HCI_FAN 0x0004
  87. #define HCI_SYSTEM_EVENT 0x0016
  88. #define HCI_VIDEO_OUT 0x001c
  89. #define HCI_HOTKEY_EVENT 0x001e
  90. #define HCI_LCD_BRIGHTNESS 0x002a
  91. #define HCI_WIRELESS 0x0056
  92. /* field definitions */
  93. #define HCI_LCD_BRIGHTNESS_BITS 3
  94. #define HCI_LCD_BRIGHTNESS_SHIFT (16-HCI_LCD_BRIGHTNESS_BITS)
  95. #define HCI_LCD_BRIGHTNESS_LEVELS (1 << HCI_LCD_BRIGHTNESS_BITS)
  96. #define HCI_VIDEO_OUT_LCD 0x1
  97. #define HCI_VIDEO_OUT_CRT 0x2
  98. #define HCI_VIDEO_OUT_TV 0x4
  99. #define HCI_WIRELESS_KILL_SWITCH 0x01
  100. #define HCI_WIRELESS_BT_PRESENT 0x0f
  101. #define HCI_WIRELESS_BT_ATTACH 0x40
  102. #define HCI_WIRELESS_BT_POWER 0x80
  103. static const struct acpi_device_id toshiba_device_ids[] = {
  104. {"TOS6200", 0},
  105. {"TOS6208", 0},
  106. {"TOS1900", 0},
  107. {"", 0},
  108. };
  109. MODULE_DEVICE_TABLE(acpi, toshiba_device_ids);
  110. static const struct key_entry toshiba_acpi_keymap[] __initconst = {
  111. { KE_KEY, 0x101, { KEY_MUTE } },
  112. { KE_KEY, 0x102, { KEY_ZOOMOUT } },
  113. { KE_KEY, 0x103, { KEY_ZOOMIN } },
  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, 0x17f, { KEY_FN } },
  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 },
  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. int illumination_installed;
  248. acpi_handle handle;
  249. const char *bt_name;
  250. struct mutex mutex;
  251. };
  252. /* Illumination support */
  253. static int toshiba_illumination_available(void)
  254. {
  255. u32 in[HCI_WORDS] = { 0, 0, 0, 0, 0, 0 };
  256. u32 out[HCI_WORDS];
  257. acpi_status status;
  258. in[0] = 0xf100;
  259. status = hci_raw(in, out);
  260. if (ACPI_FAILURE(status)) {
  261. printk(MY_INFO "Illumination device not available\n");
  262. return 0;
  263. }
  264. in[0] = 0xf400;
  265. status = hci_raw(in, out);
  266. return 1;
  267. }
  268. static void toshiba_illumination_set(struct led_classdev *cdev,
  269. enum led_brightness brightness)
  270. {
  271. u32 in[HCI_WORDS] = { 0, 0, 0, 0, 0, 0 };
  272. u32 out[HCI_WORDS];
  273. acpi_status status;
  274. /* First request : initialize communication. */
  275. in[0] = 0xf100;
  276. status = hci_raw(in, out);
  277. if (ACPI_FAILURE(status)) {
  278. printk(MY_INFO "Illumination device not available\n");
  279. return;
  280. }
  281. if (brightness) {
  282. /* Switch the illumination on */
  283. in[0] = 0xf400;
  284. in[1] = 0x14e;
  285. in[2] = 1;
  286. status = hci_raw(in, out);
  287. if (ACPI_FAILURE(status)) {
  288. printk(MY_INFO "ACPI call for illumination failed.\n");
  289. return;
  290. }
  291. } else {
  292. /* Switch the illumination off */
  293. in[0] = 0xf400;
  294. in[1] = 0x14e;
  295. in[2] = 0;
  296. status = hci_raw(in, out);
  297. if (ACPI_FAILURE(status)) {
  298. printk(MY_INFO "ACPI call for illumination failed.\n");
  299. return;
  300. }
  301. }
  302. /* Last request : close communication. */
  303. in[0] = 0xf200;
  304. in[1] = 0;
  305. in[2] = 0;
  306. hci_raw(in, out);
  307. }
  308. static enum led_brightness toshiba_illumination_get(struct led_classdev *cdev)
  309. {
  310. u32 in[HCI_WORDS] = { 0, 0, 0, 0, 0, 0 };
  311. u32 out[HCI_WORDS];
  312. acpi_status status;
  313. enum led_brightness result;
  314. /* First request : initialize communication. */
  315. in[0] = 0xf100;
  316. status = hci_raw(in, out);
  317. if (ACPI_FAILURE(status)) {
  318. printk(MY_INFO "Illumination device not available\n");
  319. return LED_OFF;
  320. }
  321. /* Check the illumination */
  322. in[0] = 0xf300;
  323. in[1] = 0x14e;
  324. status = hci_raw(in, out);
  325. if (ACPI_FAILURE(status)) {
  326. printk(MY_INFO "ACPI call for illumination failed.\n");
  327. return LED_OFF;
  328. }
  329. result = out[2] ? LED_FULL : LED_OFF;
  330. /* Last request : close communication. */
  331. in[0] = 0xf200;
  332. in[1] = 0;
  333. in[2] = 0;
  334. hci_raw(in, out);
  335. return result;
  336. }
  337. static struct led_classdev toshiba_led = {
  338. .name = "toshiba::illumination",
  339. .max_brightness = 1,
  340. .brightness_set = toshiba_illumination_set,
  341. .brightness_get = toshiba_illumination_get,
  342. };
  343. static struct toshiba_acpi_dev toshiba_acpi = {
  344. .bt_name = "Toshiba Bluetooth",
  345. };
  346. /* Bluetooth rfkill handlers */
  347. static u32 hci_get_bt_present(bool *present)
  348. {
  349. u32 hci_result;
  350. u32 value, value2;
  351. value = 0;
  352. value2 = 0;
  353. hci_read2(HCI_WIRELESS, &value, &value2, &hci_result);
  354. if (hci_result == HCI_SUCCESS)
  355. *present = (value & HCI_WIRELESS_BT_PRESENT) ? true : false;
  356. return hci_result;
  357. }
  358. static u32 hci_get_radio_state(bool *radio_state)
  359. {
  360. u32 hci_result;
  361. u32 value, value2;
  362. value = 0;
  363. value2 = 0x0001;
  364. hci_read2(HCI_WIRELESS, &value, &value2, &hci_result);
  365. *radio_state = value & HCI_WIRELESS_KILL_SWITCH;
  366. return hci_result;
  367. }
  368. static int bt_rfkill_set_block(void *data, bool blocked)
  369. {
  370. struct toshiba_acpi_dev *dev = data;
  371. u32 result1, result2;
  372. u32 value;
  373. int err;
  374. bool radio_state;
  375. value = (blocked == false);
  376. mutex_lock(&dev->mutex);
  377. if (hci_get_radio_state(&radio_state) != HCI_SUCCESS) {
  378. err = -EBUSY;
  379. goto out;
  380. }
  381. if (!radio_state) {
  382. err = 0;
  383. goto out;
  384. }
  385. hci_write2(HCI_WIRELESS, value, HCI_WIRELESS_BT_POWER, &result1);
  386. hci_write2(HCI_WIRELESS, value, HCI_WIRELESS_BT_ATTACH, &result2);
  387. if (result1 != HCI_SUCCESS || result2 != HCI_SUCCESS)
  388. err = -EBUSY;
  389. else
  390. err = 0;
  391. out:
  392. mutex_unlock(&dev->mutex);
  393. return err;
  394. }
  395. static void bt_rfkill_poll(struct rfkill *rfkill, void *data)
  396. {
  397. bool new_rfk_state;
  398. bool value;
  399. u32 hci_result;
  400. struct toshiba_acpi_dev *dev = data;
  401. mutex_lock(&dev->mutex);
  402. hci_result = hci_get_radio_state(&value);
  403. if (hci_result != HCI_SUCCESS) {
  404. /* Can't do anything useful */
  405. mutex_unlock(&dev->mutex);
  406. return;
  407. }
  408. new_rfk_state = value;
  409. mutex_unlock(&dev->mutex);
  410. if (rfkill_set_hw_state(rfkill, !new_rfk_state))
  411. bt_rfkill_set_block(data, true);
  412. }
  413. static const struct rfkill_ops toshiba_rfk_ops = {
  414. .set_block = bt_rfkill_set_block,
  415. .poll = bt_rfkill_poll,
  416. };
  417. static struct proc_dir_entry *toshiba_proc_dir /*= 0*/ ;
  418. static struct backlight_device *toshiba_backlight_device;
  419. static int force_fan;
  420. static int last_key_event;
  421. static int key_event_valid;
  422. static int get_lcd(struct backlight_device *bd)
  423. {
  424. u32 hci_result;
  425. u32 value;
  426. hci_read1(HCI_LCD_BRIGHTNESS, &value, &hci_result);
  427. if (hci_result == HCI_SUCCESS) {
  428. return (value >> HCI_LCD_BRIGHTNESS_SHIFT);
  429. } else
  430. return -EFAULT;
  431. }
  432. static int lcd_proc_show(struct seq_file *m, void *v)
  433. {
  434. int value = get_lcd(NULL);
  435. if (value >= 0) {
  436. seq_printf(m, "brightness: %d\n", value);
  437. seq_printf(m, "brightness_levels: %d\n",
  438. HCI_LCD_BRIGHTNESS_LEVELS);
  439. } else {
  440. printk(MY_ERR "Error reading LCD brightness\n");
  441. }
  442. return 0;
  443. }
  444. static int lcd_proc_open(struct inode *inode, struct file *file)
  445. {
  446. return single_open(file, lcd_proc_show, NULL);
  447. }
  448. static int set_lcd(int value)
  449. {
  450. u32 hci_result;
  451. value = value << HCI_LCD_BRIGHTNESS_SHIFT;
  452. hci_write1(HCI_LCD_BRIGHTNESS, value, &hci_result);
  453. if (hci_result != HCI_SUCCESS)
  454. return -EFAULT;
  455. return 0;
  456. }
  457. static int set_lcd_status(struct backlight_device *bd)
  458. {
  459. return set_lcd(bd->props.brightness);
  460. }
  461. static ssize_t lcd_proc_write(struct file *file, const char __user *buf,
  462. size_t count, loff_t *pos)
  463. {
  464. char cmd[42];
  465. size_t len;
  466. int value;
  467. int ret;
  468. len = min(count, sizeof(cmd) - 1);
  469. if (copy_from_user(cmd, buf, len))
  470. return -EFAULT;
  471. cmd[len] = '\0';
  472. if (sscanf(cmd, " brightness : %i", &value) == 1 &&
  473. value >= 0 && value < HCI_LCD_BRIGHTNESS_LEVELS) {
  474. ret = set_lcd(value);
  475. if (ret == 0)
  476. ret = count;
  477. } else {
  478. ret = -EINVAL;
  479. }
  480. return ret;
  481. }
  482. static const struct file_operations lcd_proc_fops = {
  483. .owner = THIS_MODULE,
  484. .open = lcd_proc_open,
  485. .read = seq_read,
  486. .llseek = seq_lseek,
  487. .release = single_release,
  488. .write = lcd_proc_write,
  489. };
  490. static int video_proc_show(struct seq_file *m, void *v)
  491. {
  492. u32 hci_result;
  493. u32 value;
  494. hci_read1(HCI_VIDEO_OUT, &value, &hci_result);
  495. if (hci_result == HCI_SUCCESS) {
  496. int is_lcd = (value & HCI_VIDEO_OUT_LCD) ? 1 : 0;
  497. int is_crt = (value & HCI_VIDEO_OUT_CRT) ? 1 : 0;
  498. int is_tv = (value & HCI_VIDEO_OUT_TV) ? 1 : 0;
  499. seq_printf(m, "lcd_out: %d\n", is_lcd);
  500. seq_printf(m, "crt_out: %d\n", is_crt);
  501. seq_printf(m, "tv_out: %d\n", is_tv);
  502. } else {
  503. printk(MY_ERR "Error reading video out status\n");
  504. }
  505. return 0;
  506. }
  507. static int video_proc_open(struct inode *inode, struct file *file)
  508. {
  509. return single_open(file, video_proc_show, NULL);
  510. }
  511. static ssize_t video_proc_write(struct file *file, const char __user *buf,
  512. size_t count, loff_t *pos)
  513. {
  514. char *cmd, *buffer;
  515. int value;
  516. int remain = count;
  517. int lcd_out = -1;
  518. int crt_out = -1;
  519. int tv_out = -1;
  520. u32 hci_result;
  521. u32 video_out;
  522. cmd = kmalloc(count + 1, GFP_KERNEL);
  523. if (!cmd)
  524. return -ENOMEM;
  525. if (copy_from_user(cmd, buf, count)) {
  526. kfree(cmd);
  527. return -EFAULT;
  528. }
  529. cmd[count] = '\0';
  530. buffer = cmd;
  531. /* scan expression. Multiple expressions may be delimited with ;
  532. *
  533. * NOTE: to keep scanning simple, invalid fields are ignored
  534. */
  535. while (remain) {
  536. if (sscanf(buffer, " lcd_out : %i", &value) == 1)
  537. lcd_out = value & 1;
  538. else if (sscanf(buffer, " crt_out : %i", &value) == 1)
  539. crt_out = value & 1;
  540. else if (sscanf(buffer, " tv_out : %i", &value) == 1)
  541. tv_out = value & 1;
  542. /* advance to one character past the next ; */
  543. do {
  544. ++buffer;
  545. --remain;
  546. }
  547. while (remain && *(buffer - 1) != ';');
  548. }
  549. kfree(cmd);
  550. hci_read1(HCI_VIDEO_OUT, &video_out, &hci_result);
  551. if (hci_result == HCI_SUCCESS) {
  552. unsigned int new_video_out = video_out;
  553. if (lcd_out != -1)
  554. _set_bit(&new_video_out, HCI_VIDEO_OUT_LCD, lcd_out);
  555. if (crt_out != -1)
  556. _set_bit(&new_video_out, HCI_VIDEO_OUT_CRT, crt_out);
  557. if (tv_out != -1)
  558. _set_bit(&new_video_out, HCI_VIDEO_OUT_TV, tv_out);
  559. /* To avoid unnecessary video disruption, only write the new
  560. * video setting if something changed. */
  561. if (new_video_out != video_out)
  562. write_acpi_int(METHOD_VIDEO_OUT, new_video_out);
  563. } else {
  564. return -EFAULT;
  565. }
  566. return count;
  567. }
  568. static const struct file_operations video_proc_fops = {
  569. .owner = THIS_MODULE,
  570. .open = video_proc_open,
  571. .read = seq_read,
  572. .llseek = seq_lseek,
  573. .release = single_release,
  574. .write = video_proc_write,
  575. };
  576. static int fan_proc_show(struct seq_file *m, void *v)
  577. {
  578. u32 hci_result;
  579. u32 value;
  580. hci_read1(HCI_FAN, &value, &hci_result);
  581. if (hci_result == HCI_SUCCESS) {
  582. seq_printf(m, "running: %d\n", (value > 0));
  583. seq_printf(m, "force_on: %d\n", force_fan);
  584. } else {
  585. printk(MY_ERR "Error reading fan status\n");
  586. }
  587. return 0;
  588. }
  589. static int fan_proc_open(struct inode *inode, struct file *file)
  590. {
  591. return single_open(file, fan_proc_show, NULL);
  592. }
  593. static ssize_t fan_proc_write(struct file *file, const char __user *buf,
  594. size_t count, loff_t *pos)
  595. {
  596. char cmd[42];
  597. size_t len;
  598. int value;
  599. u32 hci_result;
  600. len = min(count, sizeof(cmd) - 1);
  601. if (copy_from_user(cmd, buf, len))
  602. return -EFAULT;
  603. cmd[len] = '\0';
  604. if (sscanf(cmd, " force_on : %i", &value) == 1 &&
  605. value >= 0 && value <= 1) {
  606. hci_write1(HCI_FAN, value, &hci_result);
  607. if (hci_result != HCI_SUCCESS)
  608. return -EFAULT;
  609. else
  610. force_fan = value;
  611. } else {
  612. return -EINVAL;
  613. }
  614. return count;
  615. }
  616. static const struct file_operations fan_proc_fops = {
  617. .owner = THIS_MODULE,
  618. .open = fan_proc_open,
  619. .read = seq_read,
  620. .llseek = seq_lseek,
  621. .release = single_release,
  622. .write = fan_proc_write,
  623. };
  624. static int keys_proc_show(struct seq_file *m, void *v)
  625. {
  626. u32 hci_result;
  627. u32 value;
  628. if (!key_event_valid) {
  629. hci_read1(HCI_SYSTEM_EVENT, &value, &hci_result);
  630. if (hci_result == HCI_SUCCESS) {
  631. key_event_valid = 1;
  632. last_key_event = value;
  633. } else if (hci_result == HCI_EMPTY) {
  634. /* better luck next time */
  635. } else if (hci_result == HCI_NOT_SUPPORTED) {
  636. /* This is a workaround for an unresolved issue on
  637. * some machines where system events sporadically
  638. * become disabled. */
  639. hci_write1(HCI_SYSTEM_EVENT, 1, &hci_result);
  640. printk(MY_NOTICE "Re-enabled hotkeys\n");
  641. } else {
  642. printk(MY_ERR "Error reading hotkey status\n");
  643. goto end;
  644. }
  645. }
  646. seq_printf(m, "hotkey_ready: %d\n", key_event_valid);
  647. seq_printf(m, "hotkey: 0x%04x\n", last_key_event);
  648. end:
  649. return 0;
  650. }
  651. static int keys_proc_open(struct inode *inode, struct file *file)
  652. {
  653. return single_open(file, keys_proc_show, NULL);
  654. }
  655. static ssize_t keys_proc_write(struct file *file, const char __user *buf,
  656. size_t count, loff_t *pos)
  657. {
  658. char cmd[42];
  659. size_t len;
  660. int value;
  661. len = min(count, sizeof(cmd) - 1);
  662. if (copy_from_user(cmd, buf, len))
  663. return -EFAULT;
  664. cmd[len] = '\0';
  665. if (sscanf(cmd, " hotkey_ready : %i", &value) == 1 && value == 0) {
  666. key_event_valid = 0;
  667. } else {
  668. return -EINVAL;
  669. }
  670. return count;
  671. }
  672. static const struct file_operations keys_proc_fops = {
  673. .owner = THIS_MODULE,
  674. .open = keys_proc_open,
  675. .read = seq_read,
  676. .llseek = seq_lseek,
  677. .release = single_release,
  678. .write = keys_proc_write,
  679. };
  680. static int version_proc_show(struct seq_file *m, void *v)
  681. {
  682. seq_printf(m, "driver: %s\n", TOSHIBA_ACPI_VERSION);
  683. seq_printf(m, "proc_interface: %d\n", PROC_INTERFACE_VERSION);
  684. return 0;
  685. }
  686. static int version_proc_open(struct inode *inode, struct file *file)
  687. {
  688. return single_open(file, version_proc_show, PDE(inode)->data);
  689. }
  690. static const struct file_operations version_proc_fops = {
  691. .owner = THIS_MODULE,
  692. .open = version_proc_open,
  693. .read = seq_read,
  694. .llseek = seq_lseek,
  695. .release = single_release,
  696. };
  697. /* proc and module init
  698. */
  699. #define PROC_TOSHIBA "toshiba"
  700. static void __init create_toshiba_proc_entries(void)
  701. {
  702. proc_create("lcd", S_IRUGO | S_IWUSR, toshiba_proc_dir, &lcd_proc_fops);
  703. proc_create("video", S_IRUGO | S_IWUSR, toshiba_proc_dir, &video_proc_fops);
  704. proc_create("fan", S_IRUGO | S_IWUSR, toshiba_proc_dir, &fan_proc_fops);
  705. proc_create("keys", S_IRUGO | S_IWUSR, toshiba_proc_dir, &keys_proc_fops);
  706. proc_create("version", S_IRUGO, toshiba_proc_dir, &version_proc_fops);
  707. }
  708. static void remove_toshiba_proc_entries(void)
  709. {
  710. remove_proc_entry("lcd", toshiba_proc_dir);
  711. remove_proc_entry("video", toshiba_proc_dir);
  712. remove_proc_entry("fan", toshiba_proc_dir);
  713. remove_proc_entry("keys", toshiba_proc_dir);
  714. remove_proc_entry("version", toshiba_proc_dir);
  715. }
  716. static struct backlight_ops toshiba_backlight_data = {
  717. .get_brightness = get_lcd,
  718. .update_status = set_lcd_status,
  719. };
  720. static void toshiba_acpi_notify(acpi_handle handle, u32 event, void *context)
  721. {
  722. u32 hci_result, value;
  723. if (event != 0x80)
  724. return;
  725. do {
  726. hci_read1(HCI_SYSTEM_EVENT, &value, &hci_result);
  727. if (hci_result == HCI_SUCCESS) {
  728. if (value == 0x100)
  729. continue;
  730. /* act on key press; ignore key release */
  731. if (value & 0x80)
  732. continue;
  733. if (!sparse_keymap_report_event(toshiba_acpi.hotkey_dev,
  734. value, 1, true)) {
  735. printk(MY_INFO "Unknown key %x\n",
  736. value);
  737. }
  738. } else if (hci_result == HCI_NOT_SUPPORTED) {
  739. /* This is a workaround for an unresolved issue on
  740. * some machines where system events sporadically
  741. * become disabled. */
  742. hci_write1(HCI_SYSTEM_EVENT, 1, &hci_result);
  743. printk(MY_NOTICE "Re-enabled hotkeys\n");
  744. }
  745. } while (hci_result != HCI_EMPTY);
  746. }
  747. static int __init toshiba_acpi_setup_keyboard(char *device)
  748. {
  749. acpi_status status;
  750. int error;
  751. status = acpi_get_handle(NULL, device, &toshiba_acpi.handle);
  752. if (ACPI_FAILURE(status)) {
  753. printk(MY_INFO "Unable to get notification device\n");
  754. return -ENODEV;
  755. }
  756. toshiba_acpi.hotkey_dev = input_allocate_device();
  757. if (!toshiba_acpi.hotkey_dev) {
  758. printk(MY_INFO "Unable to register input device\n");
  759. return -ENOMEM;
  760. }
  761. toshiba_acpi.hotkey_dev->name = "Toshiba input device";
  762. toshiba_acpi.hotkey_dev->phys = device;
  763. toshiba_acpi.hotkey_dev->id.bustype = BUS_HOST;
  764. error = sparse_keymap_setup(toshiba_acpi.hotkey_dev,
  765. toshiba_acpi_keymap, NULL);
  766. if (error)
  767. goto err_free_dev;
  768. status = acpi_install_notify_handler(toshiba_acpi.handle,
  769. ACPI_DEVICE_NOTIFY, toshiba_acpi_notify, NULL);
  770. if (ACPI_FAILURE(status)) {
  771. printk(MY_INFO "Unable to install hotkey notification\n");
  772. error = -ENODEV;
  773. goto err_free_keymap;
  774. }
  775. status = acpi_evaluate_object(toshiba_acpi.handle, "ENAB", NULL, NULL);
  776. if (ACPI_FAILURE(status)) {
  777. printk(MY_INFO "Unable to enable hotkeys\n");
  778. error = -ENODEV;
  779. goto err_remove_notify;
  780. }
  781. error = input_register_device(toshiba_acpi.hotkey_dev);
  782. if (error) {
  783. printk(MY_INFO "Unable to register input device\n");
  784. goto err_remove_notify;
  785. }
  786. return 0;
  787. err_remove_notify:
  788. acpi_remove_notify_handler(toshiba_acpi.handle,
  789. ACPI_DEVICE_NOTIFY, toshiba_acpi_notify);
  790. err_free_keymap:
  791. sparse_keymap_free(toshiba_acpi.hotkey_dev);
  792. err_free_dev:
  793. input_free_device(toshiba_acpi.hotkey_dev);
  794. toshiba_acpi.hotkey_dev = NULL;
  795. return error;
  796. }
  797. static void toshiba_acpi_exit(void)
  798. {
  799. if (toshiba_acpi.hotkey_dev) {
  800. acpi_remove_notify_handler(toshiba_acpi.handle,
  801. ACPI_DEVICE_NOTIFY, toshiba_acpi_notify);
  802. sparse_keymap_free(toshiba_acpi.hotkey_dev);
  803. input_unregister_device(toshiba_acpi.hotkey_dev);
  804. }
  805. if (toshiba_acpi.bt_rfk) {
  806. rfkill_unregister(toshiba_acpi.bt_rfk);
  807. rfkill_destroy(toshiba_acpi.bt_rfk);
  808. }
  809. if (toshiba_backlight_device)
  810. backlight_device_unregister(toshiba_backlight_device);
  811. remove_toshiba_proc_entries();
  812. if (toshiba_proc_dir)
  813. remove_proc_entry(PROC_TOSHIBA, acpi_root_dir);
  814. if (toshiba_acpi.illumination_installed)
  815. led_classdev_unregister(&toshiba_led);
  816. platform_device_unregister(toshiba_acpi.p_dev);
  817. return;
  818. }
  819. static int __init toshiba_acpi_init(void)
  820. {
  821. u32 hci_result;
  822. bool bt_present;
  823. int ret = 0;
  824. struct backlight_properties props;
  825. if (acpi_disabled)
  826. return -ENODEV;
  827. /* simple device detection: look for HCI method */
  828. if (is_valid_acpi_path(TOSH_INTERFACE_1 GHCI_METHOD)) {
  829. method_hci = TOSH_INTERFACE_1 GHCI_METHOD;
  830. if (toshiba_acpi_setup_keyboard(TOSH_INTERFACE_1))
  831. printk(MY_INFO "Unable to activate hotkeys\n");
  832. } else if (is_valid_acpi_path(TOSH_INTERFACE_2 GHCI_METHOD)) {
  833. method_hci = TOSH_INTERFACE_2 GHCI_METHOD;
  834. if (toshiba_acpi_setup_keyboard(TOSH_INTERFACE_2))
  835. printk(MY_INFO "Unable to activate hotkeys\n");
  836. } else
  837. return -ENODEV;
  838. printk(MY_INFO "Toshiba Laptop ACPI Extras version %s\n",
  839. TOSHIBA_ACPI_VERSION);
  840. printk(MY_INFO " HCI method: %s\n", method_hci);
  841. mutex_init(&toshiba_acpi.mutex);
  842. toshiba_acpi.p_dev = platform_device_register_simple("toshiba_acpi",
  843. -1, NULL, 0);
  844. if (IS_ERR(toshiba_acpi.p_dev)) {
  845. ret = PTR_ERR(toshiba_acpi.p_dev);
  846. printk(MY_ERR "unable to register platform device\n");
  847. toshiba_acpi.p_dev = NULL;
  848. toshiba_acpi_exit();
  849. return ret;
  850. }
  851. force_fan = 0;
  852. key_event_valid = 0;
  853. /* enable event fifo */
  854. hci_write1(HCI_SYSTEM_EVENT, 1, &hci_result);
  855. toshiba_proc_dir = proc_mkdir(PROC_TOSHIBA, acpi_root_dir);
  856. if (!toshiba_proc_dir) {
  857. toshiba_acpi_exit();
  858. return -ENODEV;
  859. } else {
  860. create_toshiba_proc_entries();
  861. }
  862. props.max_brightness = HCI_LCD_BRIGHTNESS_LEVELS - 1;
  863. toshiba_backlight_device = backlight_device_register("toshiba",
  864. &toshiba_acpi.p_dev->dev,
  865. NULL,
  866. &toshiba_backlight_data,
  867. &props);
  868. if (IS_ERR(toshiba_backlight_device)) {
  869. ret = PTR_ERR(toshiba_backlight_device);
  870. printk(KERN_ERR "Could not register toshiba backlight device\n");
  871. toshiba_backlight_device = NULL;
  872. toshiba_acpi_exit();
  873. return ret;
  874. }
  875. /* Register rfkill switch for Bluetooth */
  876. if (hci_get_bt_present(&bt_present) == HCI_SUCCESS && bt_present) {
  877. toshiba_acpi.bt_rfk = rfkill_alloc(toshiba_acpi.bt_name,
  878. &toshiba_acpi.p_dev->dev,
  879. RFKILL_TYPE_BLUETOOTH,
  880. &toshiba_rfk_ops,
  881. &toshiba_acpi);
  882. if (!toshiba_acpi.bt_rfk) {
  883. printk(MY_ERR "unable to allocate rfkill device\n");
  884. toshiba_acpi_exit();
  885. return -ENOMEM;
  886. }
  887. ret = rfkill_register(toshiba_acpi.bt_rfk);
  888. if (ret) {
  889. printk(MY_ERR "unable to register rfkill device\n");
  890. rfkill_destroy(toshiba_acpi.bt_rfk);
  891. toshiba_acpi_exit();
  892. return ret;
  893. }
  894. }
  895. toshiba_acpi.illumination_installed = 0;
  896. if (toshiba_illumination_available()) {
  897. if (!led_classdev_register(&(toshiba_acpi.p_dev->dev),
  898. &toshiba_led))
  899. toshiba_acpi.illumination_installed = 1;
  900. }
  901. return 0;
  902. }
  903. module_init(toshiba_acpi_init);
  904. module_exit(toshiba_acpi_exit);