toshiba_acpi.c 27 KB

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