toshiba_acpi.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078
  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 pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  38. #define TOSHIBA_ACPI_VERSION "0.19"
  39. #define PROC_INTERFACE_VERSION 1
  40. #include <linux/kernel.h>
  41. #include <linux/module.h>
  42. #include <linux/init.h>
  43. #include <linux/types.h>
  44. #include <linux/proc_fs.h>
  45. #include <linux/seq_file.h>
  46. #include <linux/backlight.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. /* 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. struct toshiba_acpi_dev {
  100. struct acpi_device *acpi_dev;
  101. const char *method_hci;
  102. struct rfkill *bt_rfk;
  103. struct input_dev *hotkey_dev;
  104. struct backlight_device *backlight_dev;
  105. struct led_classdev led_dev;
  106. int illumination_installed;
  107. int force_fan;
  108. int last_key_event;
  109. int key_event_valid;
  110. acpi_handle handle;
  111. struct mutex mutex;
  112. };
  113. static const struct acpi_device_id toshiba_device_ids[] = {
  114. {"TOS6200", 0},
  115. {"TOS6208", 0},
  116. {"TOS1900", 0},
  117. {"", 0},
  118. };
  119. MODULE_DEVICE_TABLE(acpi, toshiba_device_ids);
  120. static const struct key_entry toshiba_acpi_keymap[] __devinitconst = {
  121. { KE_KEY, 0x101, { KEY_MUTE } },
  122. { KE_KEY, 0x102, { KEY_ZOOMOUT } },
  123. { KE_KEY, 0x103, { KEY_ZOOMIN } },
  124. { KE_KEY, 0x13b, { KEY_COFFEE } },
  125. { KE_KEY, 0x13c, { KEY_BATTERY } },
  126. { KE_KEY, 0x13d, { KEY_SLEEP } },
  127. { KE_KEY, 0x13e, { KEY_SUSPEND } },
  128. { KE_KEY, 0x13f, { KEY_SWITCHVIDEOMODE } },
  129. { KE_KEY, 0x140, { KEY_BRIGHTNESSDOWN } },
  130. { KE_KEY, 0x141, { KEY_BRIGHTNESSUP } },
  131. { KE_KEY, 0x142, { KEY_WLAN } },
  132. { KE_KEY, 0x143, { KEY_PROG1 } },
  133. { KE_KEY, 0x17f, { KEY_FN } },
  134. { KE_KEY, 0xb05, { KEY_PROG2 } },
  135. { KE_KEY, 0xb06, { KEY_WWW } },
  136. { KE_KEY, 0xb07, { KEY_MAIL } },
  137. { KE_KEY, 0xb30, { KEY_STOP } },
  138. { KE_KEY, 0xb31, { KEY_PREVIOUSSONG } },
  139. { KE_KEY, 0xb32, { KEY_NEXTSONG } },
  140. { KE_KEY, 0xb33, { KEY_PLAYPAUSE } },
  141. { KE_KEY, 0xb5a, { KEY_MEDIA } },
  142. { KE_END, 0 },
  143. };
  144. /* utility
  145. */
  146. static __inline__ void _set_bit(u32 * word, u32 mask, int value)
  147. {
  148. *word = (*word & ~mask) | (mask * value);
  149. }
  150. /* acpi interface wrappers
  151. */
  152. static int is_valid_acpi_path(const char *methodName)
  153. {
  154. acpi_handle handle;
  155. acpi_status status;
  156. status = acpi_get_handle(NULL, (char *)methodName, &handle);
  157. return !ACPI_FAILURE(status);
  158. }
  159. static int write_acpi_int(const char *methodName, int val)
  160. {
  161. struct acpi_object_list params;
  162. union acpi_object in_objs[1];
  163. acpi_status status;
  164. params.count = ARRAY_SIZE(in_objs);
  165. params.pointer = in_objs;
  166. in_objs[0].type = ACPI_TYPE_INTEGER;
  167. in_objs[0].integer.value = val;
  168. status = acpi_evaluate_object(NULL, (char *)methodName, &params, NULL);
  169. return (status == AE_OK) ? 0 : -EIO;
  170. }
  171. /* Perform a raw HCI call. Here we don't care about input or output buffer
  172. * format.
  173. */
  174. static acpi_status hci_raw(struct toshiba_acpi_dev *dev,
  175. const u32 in[HCI_WORDS], u32 out[HCI_WORDS])
  176. {
  177. struct acpi_object_list params;
  178. union acpi_object in_objs[HCI_WORDS];
  179. struct acpi_buffer results;
  180. union acpi_object out_objs[HCI_WORDS + 1];
  181. acpi_status status;
  182. int i;
  183. params.count = HCI_WORDS;
  184. params.pointer = in_objs;
  185. for (i = 0; i < HCI_WORDS; ++i) {
  186. in_objs[i].type = ACPI_TYPE_INTEGER;
  187. in_objs[i].integer.value = in[i];
  188. }
  189. results.length = sizeof(out_objs);
  190. results.pointer = out_objs;
  191. status = acpi_evaluate_object(NULL, (char *)dev->method_hci, &params,
  192. &results);
  193. if ((status == AE_OK) && (out_objs->package.count <= HCI_WORDS)) {
  194. for (i = 0; i < out_objs->package.count; ++i) {
  195. out[i] = out_objs->package.elements[i].integer.value;
  196. }
  197. }
  198. return status;
  199. }
  200. /* common hci tasks (get or set one or two value)
  201. *
  202. * In addition to the ACPI status, the HCI system returns a result which
  203. * may be useful (such as "not supported").
  204. */
  205. static acpi_status hci_write1(struct toshiba_acpi_dev *dev, u32 reg,
  206. u32 in1, u32 *result)
  207. {
  208. u32 in[HCI_WORDS] = { HCI_SET, reg, in1, 0, 0, 0 };
  209. u32 out[HCI_WORDS];
  210. acpi_status status = hci_raw(dev, in, out);
  211. *result = (status == AE_OK) ? out[0] : HCI_FAILURE;
  212. return status;
  213. }
  214. static acpi_status hci_read1(struct toshiba_acpi_dev *dev, u32 reg,
  215. 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(dev, 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(struct toshiba_acpi_dev *dev, u32 reg,
  225. 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(dev, in, out);
  230. *result = (status == AE_OK) ? out[0] : HCI_FAILURE;
  231. return status;
  232. }
  233. static acpi_status hci_read2(struct toshiba_acpi_dev *dev, u32 reg,
  234. u32 *out1, u32 *out2, u32 *result)
  235. {
  236. u32 in[HCI_WORDS] = { HCI_GET, reg, *out1, *out2, 0, 0 };
  237. u32 out[HCI_WORDS];
  238. acpi_status status = hci_raw(dev, in, out);
  239. *out1 = out[2];
  240. *out2 = out[3];
  241. *result = (status == AE_OK) ? out[0] : HCI_FAILURE;
  242. return status;
  243. }
  244. /* Illumination support */
  245. static int toshiba_illumination_available(struct toshiba_acpi_dev *dev)
  246. {
  247. u32 in[HCI_WORDS] = { 0, 0, 0, 0, 0, 0 };
  248. u32 out[HCI_WORDS];
  249. acpi_status status;
  250. in[0] = 0xf100;
  251. status = hci_raw(dev, in, out);
  252. if (ACPI_FAILURE(status)) {
  253. pr_info("Illumination device not available\n");
  254. return 0;
  255. }
  256. in[0] = 0xf400;
  257. status = hci_raw(dev, in, out);
  258. return 1;
  259. }
  260. static void toshiba_illumination_set(struct led_classdev *cdev,
  261. enum led_brightness brightness)
  262. {
  263. struct toshiba_acpi_dev *dev = container_of(cdev,
  264. struct toshiba_acpi_dev, led_dev);
  265. u32 in[HCI_WORDS] = { 0, 0, 0, 0, 0, 0 };
  266. u32 out[HCI_WORDS];
  267. acpi_status status;
  268. /* First request : initialize communication. */
  269. in[0] = 0xf100;
  270. status = hci_raw(dev, in, out);
  271. if (ACPI_FAILURE(status)) {
  272. pr_info("Illumination device not available\n");
  273. return;
  274. }
  275. if (brightness) {
  276. /* Switch the illumination on */
  277. in[0] = 0xf400;
  278. in[1] = 0x14e;
  279. in[2] = 1;
  280. status = hci_raw(dev, in, out);
  281. if (ACPI_FAILURE(status)) {
  282. pr_info("ACPI call for illumination failed\n");
  283. return;
  284. }
  285. } else {
  286. /* Switch the illumination off */
  287. in[0] = 0xf400;
  288. in[1] = 0x14e;
  289. in[2] = 0;
  290. status = hci_raw(dev, in, out);
  291. if (ACPI_FAILURE(status)) {
  292. pr_info("ACPI call for illumination failed.\n");
  293. return;
  294. }
  295. }
  296. /* Last request : close communication. */
  297. in[0] = 0xf200;
  298. in[1] = 0;
  299. in[2] = 0;
  300. hci_raw(dev, in, out);
  301. }
  302. static enum led_brightness toshiba_illumination_get(struct led_classdev *cdev)
  303. {
  304. struct toshiba_acpi_dev *dev = container_of(cdev,
  305. struct toshiba_acpi_dev, led_dev);
  306. u32 in[HCI_WORDS] = { 0, 0, 0, 0, 0, 0 };
  307. u32 out[HCI_WORDS];
  308. acpi_status status;
  309. enum led_brightness result;
  310. /* First request : initialize communication. */
  311. in[0] = 0xf100;
  312. status = hci_raw(dev, in, out);
  313. if (ACPI_FAILURE(status)) {
  314. pr_info("Illumination device not available\n");
  315. return LED_OFF;
  316. }
  317. /* Check the illumination */
  318. in[0] = 0xf300;
  319. in[1] = 0x14e;
  320. status = hci_raw(dev, in, out);
  321. if (ACPI_FAILURE(status)) {
  322. pr_info("ACPI call for illumination failed.\n");
  323. return LED_OFF;
  324. }
  325. result = out[2] ? LED_FULL : LED_OFF;
  326. /* Last request : close communication. */
  327. in[0] = 0xf200;
  328. in[1] = 0;
  329. in[2] = 0;
  330. hci_raw(dev, in, out);
  331. return result;
  332. }
  333. /* Bluetooth rfkill handlers */
  334. static u32 hci_get_bt_present(struct toshiba_acpi_dev *dev, bool *present)
  335. {
  336. u32 hci_result;
  337. u32 value, value2;
  338. value = 0;
  339. value2 = 0;
  340. hci_read2(dev, HCI_WIRELESS, &value, &value2, &hci_result);
  341. if (hci_result == HCI_SUCCESS)
  342. *present = (value & HCI_WIRELESS_BT_PRESENT) ? true : false;
  343. return hci_result;
  344. }
  345. static u32 hci_get_radio_state(struct toshiba_acpi_dev *dev, bool *radio_state)
  346. {
  347. u32 hci_result;
  348. u32 value, value2;
  349. value = 0;
  350. value2 = 0x0001;
  351. hci_read2(dev, HCI_WIRELESS, &value, &value2, &hci_result);
  352. *radio_state = value & HCI_WIRELESS_KILL_SWITCH;
  353. return hci_result;
  354. }
  355. static int bt_rfkill_set_block(void *data, bool blocked)
  356. {
  357. struct toshiba_acpi_dev *dev = data;
  358. u32 result1, result2;
  359. u32 value;
  360. int err;
  361. bool radio_state;
  362. value = (blocked == false);
  363. mutex_lock(&dev->mutex);
  364. if (hci_get_radio_state(dev, &radio_state) != HCI_SUCCESS) {
  365. err = -EIO;
  366. goto out;
  367. }
  368. if (!radio_state) {
  369. err = 0;
  370. goto out;
  371. }
  372. hci_write2(dev, HCI_WIRELESS, value, HCI_WIRELESS_BT_POWER, &result1);
  373. hci_write2(dev, HCI_WIRELESS, value, HCI_WIRELESS_BT_ATTACH, &result2);
  374. if (result1 != HCI_SUCCESS || result2 != HCI_SUCCESS)
  375. err = -EIO;
  376. else
  377. err = 0;
  378. out:
  379. mutex_unlock(&dev->mutex);
  380. return err;
  381. }
  382. static void bt_rfkill_poll(struct rfkill *rfkill, void *data)
  383. {
  384. bool new_rfk_state;
  385. bool value;
  386. u32 hci_result;
  387. struct toshiba_acpi_dev *dev = data;
  388. mutex_lock(&dev->mutex);
  389. hci_result = hci_get_radio_state(dev, &value);
  390. if (hci_result != HCI_SUCCESS) {
  391. /* Can't do anything useful */
  392. mutex_unlock(&dev->mutex);
  393. return;
  394. }
  395. new_rfk_state = value;
  396. mutex_unlock(&dev->mutex);
  397. if (rfkill_set_hw_state(rfkill, !new_rfk_state))
  398. bt_rfkill_set_block(data, true);
  399. }
  400. static const struct rfkill_ops toshiba_rfk_ops = {
  401. .set_block = bt_rfkill_set_block,
  402. .poll = bt_rfkill_poll,
  403. };
  404. static struct proc_dir_entry *toshiba_proc_dir /*= 0*/ ;
  405. static int get_lcd(struct backlight_device *bd)
  406. {
  407. struct toshiba_acpi_dev *dev = bl_get_data(bd);
  408. u32 hci_result;
  409. u32 value;
  410. hci_read1(dev, HCI_LCD_BRIGHTNESS, &value, &hci_result);
  411. if (hci_result == HCI_SUCCESS)
  412. return (value >> HCI_LCD_BRIGHTNESS_SHIFT);
  413. return -EIO;
  414. }
  415. static int lcd_proc_show(struct seq_file *m, void *v)
  416. {
  417. struct toshiba_acpi_dev *dev = m->private;
  418. int value;
  419. if (!dev->backlight_dev)
  420. return -ENODEV;
  421. value = get_lcd(dev->backlight_dev);
  422. if (value >= 0) {
  423. seq_printf(m, "brightness: %d\n", value);
  424. seq_printf(m, "brightness_levels: %d\n",
  425. HCI_LCD_BRIGHTNESS_LEVELS);
  426. return 0;
  427. }
  428. pr_err("Error reading LCD brightness\n");
  429. return -EIO;
  430. }
  431. static int lcd_proc_open(struct inode *inode, struct file *file)
  432. {
  433. return single_open(file, lcd_proc_show, PDE(inode)->data);
  434. }
  435. static int set_lcd(struct toshiba_acpi_dev *dev, int value)
  436. {
  437. u32 hci_result;
  438. value = value << HCI_LCD_BRIGHTNESS_SHIFT;
  439. hci_write1(dev, HCI_LCD_BRIGHTNESS, value, &hci_result);
  440. return hci_result == HCI_SUCCESS ? 0 : -EIO;
  441. }
  442. static int set_lcd_status(struct backlight_device *bd)
  443. {
  444. struct toshiba_acpi_dev *dev = bl_get_data(bd);
  445. return set_lcd(dev, bd->props.brightness);
  446. }
  447. static ssize_t lcd_proc_write(struct file *file, const char __user *buf,
  448. size_t count, loff_t *pos)
  449. {
  450. struct toshiba_acpi_dev *dev = PDE(file->f_path.dentry->d_inode)->data;
  451. char cmd[42];
  452. size_t len;
  453. int value;
  454. int ret;
  455. len = min(count, sizeof(cmd) - 1);
  456. if (copy_from_user(cmd, buf, len))
  457. return -EFAULT;
  458. cmd[len] = '\0';
  459. if (sscanf(cmd, " brightness : %i", &value) == 1 &&
  460. value >= 0 && value < HCI_LCD_BRIGHTNESS_LEVELS) {
  461. ret = set_lcd(dev, value);
  462. if (ret == 0)
  463. ret = count;
  464. } else {
  465. ret = -EINVAL;
  466. }
  467. return ret;
  468. }
  469. static const struct file_operations lcd_proc_fops = {
  470. .owner = THIS_MODULE,
  471. .open = lcd_proc_open,
  472. .read = seq_read,
  473. .llseek = seq_lseek,
  474. .release = single_release,
  475. .write = lcd_proc_write,
  476. };
  477. static int video_proc_show(struct seq_file *m, void *v)
  478. {
  479. struct toshiba_acpi_dev *dev = m->private;
  480. u32 hci_result;
  481. u32 value;
  482. hci_read1(dev, HCI_VIDEO_OUT, &value, &hci_result);
  483. if (hci_result == HCI_SUCCESS) {
  484. int is_lcd = (value & HCI_VIDEO_OUT_LCD) ? 1 : 0;
  485. int is_crt = (value & HCI_VIDEO_OUT_CRT) ? 1 : 0;
  486. int is_tv = (value & HCI_VIDEO_OUT_TV) ? 1 : 0;
  487. seq_printf(m, "lcd_out: %d\n", is_lcd);
  488. seq_printf(m, "crt_out: %d\n", is_crt);
  489. seq_printf(m, "tv_out: %d\n", is_tv);
  490. return 0;
  491. }
  492. return -EIO;
  493. }
  494. static int video_proc_open(struct inode *inode, struct file *file)
  495. {
  496. return single_open(file, video_proc_show, PDE(inode)->data);
  497. }
  498. static ssize_t video_proc_write(struct file *file, const char __user *buf,
  499. size_t count, loff_t *pos)
  500. {
  501. struct toshiba_acpi_dev *dev = PDE(file->f_path.dentry->d_inode)->data;
  502. char *cmd, *buffer;
  503. int ret = 0;
  504. int value;
  505. int remain = count;
  506. int lcd_out = -1;
  507. int crt_out = -1;
  508. int tv_out = -1;
  509. u32 hci_result;
  510. u32 video_out;
  511. cmd = kmalloc(count + 1, GFP_KERNEL);
  512. if (!cmd)
  513. return -ENOMEM;
  514. if (copy_from_user(cmd, buf, count)) {
  515. kfree(cmd);
  516. return -EFAULT;
  517. }
  518. cmd[count] = '\0';
  519. buffer = cmd;
  520. /* scan expression. Multiple expressions may be delimited with ;
  521. *
  522. * NOTE: to keep scanning simple, invalid fields are ignored
  523. */
  524. while (remain) {
  525. if (sscanf(buffer, " lcd_out : %i", &value) == 1)
  526. lcd_out = value & 1;
  527. else if (sscanf(buffer, " crt_out : %i", &value) == 1)
  528. crt_out = value & 1;
  529. else if (sscanf(buffer, " tv_out : %i", &value) == 1)
  530. tv_out = value & 1;
  531. /* advance to one character past the next ; */
  532. do {
  533. ++buffer;
  534. --remain;
  535. }
  536. while (remain && *(buffer - 1) != ';');
  537. }
  538. kfree(cmd);
  539. hci_read1(dev, HCI_VIDEO_OUT, &video_out, &hci_result);
  540. if (hci_result == HCI_SUCCESS) {
  541. unsigned int new_video_out = video_out;
  542. if (lcd_out != -1)
  543. _set_bit(&new_video_out, HCI_VIDEO_OUT_LCD, lcd_out);
  544. if (crt_out != -1)
  545. _set_bit(&new_video_out, HCI_VIDEO_OUT_CRT, crt_out);
  546. if (tv_out != -1)
  547. _set_bit(&new_video_out, HCI_VIDEO_OUT_TV, tv_out);
  548. /* To avoid unnecessary video disruption, only write the new
  549. * video setting if something changed. */
  550. if (new_video_out != video_out)
  551. ret = write_acpi_int(METHOD_VIDEO_OUT, new_video_out);
  552. } else {
  553. ret = -EIO;
  554. }
  555. return ret ? ret : count;
  556. }
  557. static const struct file_operations video_proc_fops = {
  558. .owner = THIS_MODULE,
  559. .open = video_proc_open,
  560. .read = seq_read,
  561. .llseek = seq_lseek,
  562. .release = single_release,
  563. .write = video_proc_write,
  564. };
  565. static int fan_proc_show(struct seq_file *m, void *v)
  566. {
  567. struct toshiba_acpi_dev *dev = m->private;
  568. u32 hci_result;
  569. u32 value;
  570. hci_read1(dev, HCI_FAN, &value, &hci_result);
  571. if (hci_result == HCI_SUCCESS) {
  572. seq_printf(m, "running: %d\n", (value > 0));
  573. seq_printf(m, "force_on: %d\n", dev->force_fan);
  574. return 0;
  575. }
  576. return -EIO;
  577. }
  578. static int fan_proc_open(struct inode *inode, struct file *file)
  579. {
  580. return single_open(file, fan_proc_show, PDE(inode)->data);
  581. }
  582. static ssize_t fan_proc_write(struct file *file, const char __user *buf,
  583. size_t count, loff_t *pos)
  584. {
  585. struct toshiba_acpi_dev *dev = PDE(file->f_path.dentry->d_inode)->data;
  586. char cmd[42];
  587. size_t len;
  588. int value;
  589. u32 hci_result;
  590. len = min(count, sizeof(cmd) - 1);
  591. if (copy_from_user(cmd, buf, len))
  592. return -EFAULT;
  593. cmd[len] = '\0';
  594. if (sscanf(cmd, " force_on : %i", &value) == 1 &&
  595. value >= 0 && value <= 1) {
  596. hci_write1(dev, HCI_FAN, value, &hci_result);
  597. if (hci_result != HCI_SUCCESS)
  598. return -EIO;
  599. else
  600. dev->force_fan = value;
  601. } else {
  602. return -EINVAL;
  603. }
  604. return count;
  605. }
  606. static const struct file_operations fan_proc_fops = {
  607. .owner = THIS_MODULE,
  608. .open = fan_proc_open,
  609. .read = seq_read,
  610. .llseek = seq_lseek,
  611. .release = single_release,
  612. .write = fan_proc_write,
  613. };
  614. static int keys_proc_show(struct seq_file *m, void *v)
  615. {
  616. struct toshiba_acpi_dev *dev = m->private;
  617. u32 hci_result;
  618. u32 value;
  619. if (!dev->key_event_valid) {
  620. hci_read1(dev, HCI_SYSTEM_EVENT, &value, &hci_result);
  621. if (hci_result == HCI_SUCCESS) {
  622. dev->key_event_valid = 1;
  623. dev->last_key_event = value;
  624. } else if (hci_result == HCI_EMPTY) {
  625. /* better luck next time */
  626. } else if (hci_result == HCI_NOT_SUPPORTED) {
  627. /* This is a workaround for an unresolved issue on
  628. * some machines where system events sporadically
  629. * become disabled. */
  630. hci_write1(dev, HCI_SYSTEM_EVENT, 1, &hci_result);
  631. pr_notice("Re-enabled hotkeys\n");
  632. } else {
  633. pr_err("Error reading hotkey status\n");
  634. return -EIO;
  635. }
  636. }
  637. seq_printf(m, "hotkey_ready: %d\n", dev->key_event_valid);
  638. seq_printf(m, "hotkey: 0x%04x\n", dev->last_key_event);
  639. return 0;
  640. }
  641. static int keys_proc_open(struct inode *inode, struct file *file)
  642. {
  643. return single_open(file, keys_proc_show, PDE(inode)->data);
  644. }
  645. static ssize_t keys_proc_write(struct file *file, const char __user *buf,
  646. size_t count, loff_t *pos)
  647. {
  648. struct toshiba_acpi_dev *dev = PDE(file->f_path.dentry->d_inode)->data;
  649. char cmd[42];
  650. size_t len;
  651. int value;
  652. len = min(count, sizeof(cmd) - 1);
  653. if (copy_from_user(cmd, buf, len))
  654. return -EFAULT;
  655. cmd[len] = '\0';
  656. if (sscanf(cmd, " hotkey_ready : %i", &value) == 1 && value == 0) {
  657. dev->key_event_valid = 0;
  658. } else {
  659. return -EINVAL;
  660. }
  661. return count;
  662. }
  663. static const struct file_operations keys_proc_fops = {
  664. .owner = THIS_MODULE,
  665. .open = keys_proc_open,
  666. .read = seq_read,
  667. .llseek = seq_lseek,
  668. .release = single_release,
  669. .write = keys_proc_write,
  670. };
  671. static int version_proc_show(struct seq_file *m, void *v)
  672. {
  673. seq_printf(m, "driver: %s\n", TOSHIBA_ACPI_VERSION);
  674. seq_printf(m, "proc_interface: %d\n", PROC_INTERFACE_VERSION);
  675. return 0;
  676. }
  677. static int version_proc_open(struct inode *inode, struct file *file)
  678. {
  679. return single_open(file, version_proc_show, PDE(inode)->data);
  680. }
  681. static const struct file_operations version_proc_fops = {
  682. .owner = THIS_MODULE,
  683. .open = version_proc_open,
  684. .read = seq_read,
  685. .llseek = seq_lseek,
  686. .release = single_release,
  687. };
  688. /* proc and module init
  689. */
  690. #define PROC_TOSHIBA "toshiba"
  691. static void __devinit
  692. create_toshiba_proc_entries(struct toshiba_acpi_dev *dev)
  693. {
  694. proc_create_data("lcd", S_IRUGO | S_IWUSR, toshiba_proc_dir,
  695. &lcd_proc_fops, dev);
  696. proc_create_data("video", S_IRUGO | S_IWUSR, toshiba_proc_dir,
  697. &video_proc_fops, dev);
  698. proc_create_data("fan", S_IRUGO | S_IWUSR, toshiba_proc_dir,
  699. &fan_proc_fops, dev);
  700. proc_create_data("keys", S_IRUGO | S_IWUSR, toshiba_proc_dir,
  701. &keys_proc_fops, dev);
  702. proc_create_data("version", S_IRUGO, toshiba_proc_dir,
  703. &version_proc_fops, dev);
  704. }
  705. static void remove_toshiba_proc_entries(void)
  706. {
  707. remove_proc_entry("lcd", toshiba_proc_dir);
  708. remove_proc_entry("video", toshiba_proc_dir);
  709. remove_proc_entry("fan", toshiba_proc_dir);
  710. remove_proc_entry("keys", toshiba_proc_dir);
  711. remove_proc_entry("version", toshiba_proc_dir);
  712. }
  713. static const struct backlight_ops toshiba_backlight_data = {
  714. .get_brightness = get_lcd,
  715. .update_status = set_lcd_status,
  716. };
  717. static int __devinit toshiba_acpi_setup_keyboard(struct toshiba_acpi_dev *dev,
  718. char *device_path)
  719. {
  720. acpi_status status;
  721. int error;
  722. status = acpi_get_handle(NULL, device_path, &dev->handle);
  723. if (ACPI_FAILURE(status)) {
  724. pr_info("Unable to get notification device\n");
  725. return -ENODEV;
  726. }
  727. dev->hotkey_dev = input_allocate_device();
  728. if (!dev->hotkey_dev) {
  729. pr_info("Unable to register input device\n");
  730. return -ENOMEM;
  731. }
  732. dev->hotkey_dev->name = "Toshiba input device";
  733. dev->hotkey_dev->phys = device_path;
  734. dev->hotkey_dev->id.bustype = BUS_HOST;
  735. error = sparse_keymap_setup(dev->hotkey_dev, toshiba_acpi_keymap, NULL);
  736. if (error)
  737. goto err_free_dev;
  738. status = acpi_evaluate_object(dev->handle, "ENAB", NULL, NULL);
  739. if (ACPI_FAILURE(status)) {
  740. pr_info("Unable to enable hotkeys\n");
  741. error = -ENODEV;
  742. goto err_free_keymap;
  743. }
  744. error = input_register_device(dev->hotkey_dev);
  745. if (error) {
  746. pr_info("Unable to register input device\n");
  747. goto err_free_keymap;
  748. }
  749. return 0;
  750. err_free_keymap:
  751. sparse_keymap_free(dev->hotkey_dev);
  752. err_free_dev:
  753. input_free_device(dev->hotkey_dev);
  754. dev->hotkey_dev = NULL;
  755. return error;
  756. }
  757. static int toshiba_acpi_remove(struct acpi_device *acpi_dev, int type)
  758. {
  759. struct toshiba_acpi_dev *dev = acpi_driver_data(acpi_dev);
  760. remove_toshiba_proc_entries();
  761. if (dev->hotkey_dev) {
  762. input_unregister_device(dev->hotkey_dev);
  763. sparse_keymap_free(dev->hotkey_dev);
  764. }
  765. if (dev->bt_rfk) {
  766. rfkill_unregister(dev->bt_rfk);
  767. rfkill_destroy(dev->bt_rfk);
  768. }
  769. if (dev->backlight_dev)
  770. backlight_device_unregister(dev->backlight_dev);
  771. if (dev->illumination_installed)
  772. led_classdev_unregister(&dev->led_dev);
  773. kfree(dev);
  774. return 0;
  775. }
  776. static int __devinit toshiba_acpi_add(struct acpi_device *acpi_dev)
  777. {
  778. struct toshiba_acpi_dev *dev;
  779. u32 hci_result;
  780. bool bt_present;
  781. int ret = 0;
  782. struct backlight_properties props;
  783. pr_info("Toshiba Laptop ACPI Extras version %s\n",
  784. TOSHIBA_ACPI_VERSION);
  785. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  786. if (!dev)
  787. return -ENOMEM;
  788. dev->acpi_dev = acpi_dev;
  789. acpi_dev->driver_data = dev;
  790. /* simple device detection: look for HCI method */
  791. if (is_valid_acpi_path(TOSH_INTERFACE_1 GHCI_METHOD)) {
  792. dev->method_hci = TOSH_INTERFACE_1 GHCI_METHOD;
  793. if (toshiba_acpi_setup_keyboard(dev, TOSH_INTERFACE_1))
  794. pr_info("Unable to activate hotkeys\n");
  795. } else if (is_valid_acpi_path(TOSH_INTERFACE_2 GHCI_METHOD)) {
  796. dev->method_hci = TOSH_INTERFACE_2 GHCI_METHOD;
  797. if (toshiba_acpi_setup_keyboard(dev, TOSH_INTERFACE_2))
  798. pr_info("Unable to activate hotkeys\n");
  799. } else {
  800. ret = -ENODEV;
  801. goto error;
  802. }
  803. pr_info("HCI method: %s\n", dev->method_hci);
  804. mutex_init(&dev->mutex);
  805. /* enable event fifo */
  806. hci_write1(dev, HCI_SYSTEM_EVENT, 1, &hci_result);
  807. create_toshiba_proc_entries(dev);
  808. props.type = BACKLIGHT_PLATFORM;
  809. props.max_brightness = HCI_LCD_BRIGHTNESS_LEVELS - 1;
  810. dev->backlight_dev = backlight_device_register("toshiba",
  811. &acpi_dev->dev,
  812. dev,
  813. &toshiba_backlight_data,
  814. &props);
  815. if (IS_ERR(dev->backlight_dev)) {
  816. ret = PTR_ERR(dev->backlight_dev);
  817. pr_err("Could not register toshiba backlight device\n");
  818. dev->backlight_dev = NULL;
  819. goto error;
  820. }
  821. /* Register rfkill switch for Bluetooth */
  822. if (hci_get_bt_present(dev, &bt_present) == HCI_SUCCESS && bt_present) {
  823. dev->bt_rfk = rfkill_alloc("Toshiba Bluetooth",
  824. &acpi_dev->dev,
  825. RFKILL_TYPE_BLUETOOTH,
  826. &toshiba_rfk_ops,
  827. dev);
  828. if (!dev->bt_rfk) {
  829. pr_err("unable to allocate rfkill device\n");
  830. ret = -ENOMEM;
  831. goto error;
  832. }
  833. ret = rfkill_register(dev->bt_rfk);
  834. if (ret) {
  835. pr_err("unable to register rfkill device\n");
  836. rfkill_destroy(dev->bt_rfk);
  837. goto error;
  838. }
  839. }
  840. if (toshiba_illumination_available(dev)) {
  841. dev->led_dev.name = "toshiba::illumination";
  842. dev->led_dev.max_brightness = 1;
  843. dev->led_dev.brightness_set = toshiba_illumination_set;
  844. dev->led_dev.brightness_get = toshiba_illumination_get;
  845. if (!led_classdev_register(&acpi_dev->dev, &dev->led_dev))
  846. dev->illumination_installed = 1;
  847. }
  848. return 0;
  849. error:
  850. toshiba_acpi_remove(acpi_dev, 0);
  851. return ret;
  852. }
  853. static void toshiba_acpi_notify(struct acpi_device *acpi_dev, u32 event)
  854. {
  855. struct toshiba_acpi_dev *dev = acpi_driver_data(acpi_dev);
  856. u32 hci_result, value;
  857. if (event != 0x80)
  858. return;
  859. do {
  860. hci_read1(dev, HCI_SYSTEM_EVENT, &value, &hci_result);
  861. if (hci_result == HCI_SUCCESS) {
  862. if (value == 0x100)
  863. continue;
  864. /* act on key press; ignore key release */
  865. if (value & 0x80)
  866. continue;
  867. if (!sparse_keymap_report_event(dev->hotkey_dev,
  868. value, 1, true)) {
  869. pr_info("Unknown key %x\n",
  870. value);
  871. }
  872. } else if (hci_result == HCI_NOT_SUPPORTED) {
  873. /* This is a workaround for an unresolved issue on
  874. * some machines where system events sporadically
  875. * become disabled. */
  876. hci_write1(dev, HCI_SYSTEM_EVENT, 1, &hci_result);
  877. pr_notice("Re-enabled hotkeys\n");
  878. }
  879. } while (hci_result != HCI_EMPTY);
  880. }
  881. static struct acpi_driver toshiba_acpi_driver = {
  882. .name = "Toshiba ACPI driver",
  883. .owner = THIS_MODULE,
  884. .ids = toshiba_device_ids,
  885. .flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS,
  886. .ops = {
  887. .add = toshiba_acpi_add,
  888. .remove = toshiba_acpi_remove,
  889. .notify = toshiba_acpi_notify,
  890. },
  891. };
  892. static int __init toshiba_acpi_init(void)
  893. {
  894. int ret;
  895. toshiba_proc_dir = proc_mkdir(PROC_TOSHIBA, acpi_root_dir);
  896. if (!toshiba_proc_dir) {
  897. pr_err("Unable to create proc dir " PROC_TOSHIBA "\n");
  898. return -ENODEV;
  899. }
  900. ret = acpi_bus_register_driver(&toshiba_acpi_driver);
  901. if (ret) {
  902. pr_err("Failed to register ACPI driver: %d\n", ret);
  903. remove_proc_entry(PROC_TOSHIBA, acpi_root_dir);
  904. }
  905. return ret;
  906. }
  907. static void __exit toshiba_acpi_exit(void)
  908. {
  909. acpi_bus_unregister_driver(&toshiba_acpi_driver);
  910. if (toshiba_proc_dir)
  911. remove_proc_entry(PROC_TOSHIBA, acpi_root_dir);
  912. }
  913. module_init(toshiba_acpi_init);
  914. module_exit(toshiba_acpi_exit);