toshiba_acpi.c 25 KB

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