toshiba_acpi.c 27 KB

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