toshiba_acpi.c 27 KB

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