toshiba_acpi.c 26 KB

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