toshiba_acpi.c 26 KB

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