toshiba_acpi.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863
  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/backlight.h>
  44. #include <linux/platform_device.h>
  45. #include <linux/rfkill.h>
  46. #include <linux/input-polldev.h>
  47. #include <asm/uaccess.h>
  48. #include <acpi/acpi_drivers.h>
  49. MODULE_AUTHOR("John Belmonte");
  50. MODULE_DESCRIPTION("Toshiba Laptop ACPI Extras Driver");
  51. MODULE_LICENSE("GPL");
  52. #define MY_LOGPREFIX "toshiba_acpi: "
  53. #define MY_ERR KERN_ERR MY_LOGPREFIX
  54. #define MY_NOTICE KERN_NOTICE MY_LOGPREFIX
  55. #define MY_INFO KERN_INFO MY_LOGPREFIX
  56. /* Toshiba ACPI method paths */
  57. #define METHOD_LCD_BRIGHTNESS "\\_SB_.PCI0.VGA_.LCD_._BCM"
  58. #define METHOD_HCI_1 "\\_SB_.VALD.GHCI"
  59. #define METHOD_HCI_2 "\\_SB_.VALZ.GHCI"
  60. #define METHOD_VIDEO_OUT "\\_SB_.VALX.DSSX"
  61. /* Toshiba HCI interface definitions
  62. *
  63. * HCI is Toshiba's "Hardware Control Interface" which is supposed to
  64. * be uniform across all their models. Ideally we would just call
  65. * dedicated ACPI methods instead of using this primitive interface.
  66. * However the ACPI methods seem to be incomplete in some areas (for
  67. * example they allow setting, but not reading, the LCD brightness value),
  68. * so this is still useful.
  69. */
  70. #define HCI_WORDS 6
  71. /* operations */
  72. #define HCI_SET 0xff00
  73. #define HCI_GET 0xfe00
  74. /* return codes */
  75. #define HCI_SUCCESS 0x0000
  76. #define HCI_FAILURE 0x1000
  77. #define HCI_NOT_SUPPORTED 0x8000
  78. #define HCI_EMPTY 0x8c00
  79. /* registers */
  80. #define HCI_FAN 0x0004
  81. #define HCI_SYSTEM_EVENT 0x0016
  82. #define HCI_VIDEO_OUT 0x001c
  83. #define HCI_HOTKEY_EVENT 0x001e
  84. #define HCI_LCD_BRIGHTNESS 0x002a
  85. #define HCI_WIRELESS 0x0056
  86. /* field definitions */
  87. #define HCI_LCD_BRIGHTNESS_BITS 3
  88. #define HCI_LCD_BRIGHTNESS_SHIFT (16-HCI_LCD_BRIGHTNESS_BITS)
  89. #define HCI_LCD_BRIGHTNESS_LEVELS (1 << HCI_LCD_BRIGHTNESS_BITS)
  90. #define HCI_VIDEO_OUT_LCD 0x1
  91. #define HCI_VIDEO_OUT_CRT 0x2
  92. #define HCI_VIDEO_OUT_TV 0x4
  93. #define HCI_WIRELESS_KILL_SWITCH 0x01
  94. #define HCI_WIRELESS_BT_PRESENT 0x0f
  95. #define HCI_WIRELESS_BT_ATTACH 0x40
  96. #define HCI_WIRELESS_BT_POWER 0x80
  97. static const struct acpi_device_id toshiba_device_ids[] = {
  98. {"TOS6200", 0},
  99. {"TOS6208", 0},
  100. {"TOS1900", 0},
  101. {"", 0},
  102. };
  103. MODULE_DEVICE_TABLE(acpi, toshiba_device_ids);
  104. /* utility
  105. */
  106. static __inline__ void _set_bit(u32 * word, u32 mask, int value)
  107. {
  108. *word = (*word & ~mask) | (mask * value);
  109. }
  110. /* acpi interface wrappers
  111. */
  112. static int is_valid_acpi_path(const char *methodName)
  113. {
  114. acpi_handle handle;
  115. acpi_status status;
  116. status = acpi_get_handle(NULL, (char *)methodName, &handle);
  117. return !ACPI_FAILURE(status);
  118. }
  119. static int write_acpi_int(const char *methodName, int val)
  120. {
  121. struct acpi_object_list params;
  122. union acpi_object in_objs[1];
  123. acpi_status status;
  124. params.count = ARRAY_SIZE(in_objs);
  125. params.pointer = in_objs;
  126. in_objs[0].type = ACPI_TYPE_INTEGER;
  127. in_objs[0].integer.value = val;
  128. status = acpi_evaluate_object(NULL, (char *)methodName, &params, NULL);
  129. return (status == AE_OK);
  130. }
  131. #if 0
  132. static int read_acpi_int(const char *methodName, int *pVal)
  133. {
  134. struct acpi_buffer results;
  135. union acpi_object out_objs[1];
  136. acpi_status status;
  137. results.length = sizeof(out_objs);
  138. results.pointer = out_objs;
  139. status = acpi_evaluate_object(0, (char *)methodName, 0, &results);
  140. *pVal = out_objs[0].integer.value;
  141. return (status == AE_OK) && (out_objs[0].type == ACPI_TYPE_INTEGER);
  142. }
  143. #endif
  144. static const char *method_hci /*= 0*/ ;
  145. /* Perform a raw HCI call. Here we don't care about input or output buffer
  146. * format.
  147. */
  148. static acpi_status hci_raw(const u32 in[HCI_WORDS], u32 out[HCI_WORDS])
  149. {
  150. struct acpi_object_list params;
  151. union acpi_object in_objs[HCI_WORDS];
  152. struct acpi_buffer results;
  153. union acpi_object out_objs[HCI_WORDS + 1];
  154. acpi_status status;
  155. int i;
  156. params.count = HCI_WORDS;
  157. params.pointer = in_objs;
  158. for (i = 0; i < HCI_WORDS; ++i) {
  159. in_objs[i].type = ACPI_TYPE_INTEGER;
  160. in_objs[i].integer.value = in[i];
  161. }
  162. results.length = sizeof(out_objs);
  163. results.pointer = out_objs;
  164. status = acpi_evaluate_object(NULL, (char *)method_hci, &params,
  165. &results);
  166. if ((status == AE_OK) && (out_objs->package.count <= HCI_WORDS)) {
  167. for (i = 0; i < out_objs->package.count; ++i) {
  168. out[i] = out_objs->package.elements[i].integer.value;
  169. }
  170. }
  171. return status;
  172. }
  173. /* common hci tasks (get or set one or two value)
  174. *
  175. * In addition to the ACPI status, the HCI system returns a result which
  176. * may be useful (such as "not supported").
  177. */
  178. static acpi_status hci_write1(u32 reg, u32 in1, u32 * result)
  179. {
  180. u32 in[HCI_WORDS] = { HCI_SET, reg, in1, 0, 0, 0 };
  181. u32 out[HCI_WORDS];
  182. acpi_status status = hci_raw(in, out);
  183. *result = (status == AE_OK) ? out[0] : HCI_FAILURE;
  184. return status;
  185. }
  186. static acpi_status hci_read1(u32 reg, u32 * out1, u32 * result)
  187. {
  188. u32 in[HCI_WORDS] = { HCI_GET, reg, 0, 0, 0, 0 };
  189. u32 out[HCI_WORDS];
  190. acpi_status status = hci_raw(in, out);
  191. *out1 = out[2];
  192. *result = (status == AE_OK) ? out[0] : HCI_FAILURE;
  193. return status;
  194. }
  195. static acpi_status hci_write2(u32 reg, u32 in1, u32 in2, u32 *result)
  196. {
  197. u32 in[HCI_WORDS] = { HCI_SET, reg, in1, in2, 0, 0 };
  198. u32 out[HCI_WORDS];
  199. acpi_status status = hci_raw(in, out);
  200. *result = (status == AE_OK) ? out[0] : HCI_FAILURE;
  201. return status;
  202. }
  203. static acpi_status hci_read2(u32 reg, u32 *out1, u32 *out2, u32 *result)
  204. {
  205. u32 in[HCI_WORDS] = { HCI_GET, reg, *out1, *out2, 0, 0 };
  206. u32 out[HCI_WORDS];
  207. acpi_status status = hci_raw(in, out);
  208. *out1 = out[2];
  209. *out2 = out[3];
  210. *result = (status == AE_OK) ? out[0] : HCI_FAILURE;
  211. return status;
  212. }
  213. struct toshiba_acpi_dev {
  214. struct platform_device *p_dev;
  215. struct rfkill *rfk_dev;
  216. struct input_polled_dev *poll_dev;
  217. const char *bt_name;
  218. const char *rfk_name;
  219. bool last_rfk_state;
  220. struct mutex mutex;
  221. };
  222. static struct toshiba_acpi_dev toshiba_acpi = {
  223. .bt_name = "Toshiba Bluetooth",
  224. .rfk_name = "Toshiba RFKill Switch",
  225. .last_rfk_state = false,
  226. };
  227. /* Bluetooth rfkill handlers */
  228. static u32 hci_get_bt_present(bool *present)
  229. {
  230. u32 hci_result;
  231. u32 value, value2;
  232. value = 0;
  233. value2 = 0;
  234. hci_read2(HCI_WIRELESS, &value, &value2, &hci_result);
  235. if (hci_result == HCI_SUCCESS)
  236. *present = (value & HCI_WIRELESS_BT_PRESENT) ? true : false;
  237. return hci_result;
  238. }
  239. static u32 hci_get_bt_on(bool *on)
  240. {
  241. u32 hci_result;
  242. u32 value, value2;
  243. value = 0;
  244. value2 = 0x0001;
  245. hci_read2(HCI_WIRELESS, &value, &value2, &hci_result);
  246. if (hci_result == HCI_SUCCESS)
  247. *on = (value & HCI_WIRELESS_BT_POWER) &&
  248. (value & HCI_WIRELESS_BT_ATTACH);
  249. return hci_result;
  250. }
  251. static u32 hci_get_radio_state(bool *radio_state)
  252. {
  253. u32 hci_result;
  254. u32 value, value2;
  255. value = 0;
  256. value2 = 0x0001;
  257. hci_read2(HCI_WIRELESS, &value, &value2, &hci_result);
  258. *radio_state = value & HCI_WIRELESS_KILL_SWITCH;
  259. return hci_result;
  260. }
  261. static int bt_rfkill_toggle_radio(void *data, enum rfkill_state state)
  262. {
  263. u32 result1, result2;
  264. u32 value;
  265. bool radio_state;
  266. struct toshiba_acpi_dev *dev = data;
  267. value = (state == RFKILL_STATE_UNBLOCKED);
  268. if (hci_get_radio_state(&radio_state) != HCI_SUCCESS)
  269. return -EFAULT;
  270. switch (state) {
  271. case RFKILL_STATE_UNBLOCKED:
  272. if (!radio_state)
  273. return -EPERM;
  274. break;
  275. case RFKILL_STATE_SOFT_BLOCKED:
  276. break;
  277. default:
  278. return -EINVAL;
  279. }
  280. mutex_lock(&dev->mutex);
  281. hci_write2(HCI_WIRELESS, value, HCI_WIRELESS_BT_POWER, &result1);
  282. hci_write2(HCI_WIRELESS, value, HCI_WIRELESS_BT_ATTACH, &result2);
  283. mutex_unlock(&dev->mutex);
  284. if (result1 != HCI_SUCCESS || result2 != HCI_SUCCESS)
  285. return -EFAULT;
  286. return 0;
  287. }
  288. static void bt_poll_rfkill(struct input_polled_dev *poll_dev)
  289. {
  290. bool state_changed;
  291. bool new_rfk_state;
  292. bool value;
  293. u32 hci_result;
  294. struct toshiba_acpi_dev *dev = poll_dev->private;
  295. hci_result = hci_get_radio_state(&value);
  296. if (hci_result != HCI_SUCCESS)
  297. return; /* Can't do anything useful */
  298. new_rfk_state = value;
  299. mutex_lock(&dev->mutex);
  300. state_changed = new_rfk_state != dev->last_rfk_state;
  301. dev->last_rfk_state = new_rfk_state;
  302. mutex_unlock(&dev->mutex);
  303. if (unlikely(state_changed)) {
  304. rfkill_force_state(dev->rfk_dev,
  305. new_rfk_state ?
  306. RFKILL_STATE_SOFT_BLOCKED :
  307. RFKILL_STATE_HARD_BLOCKED);
  308. input_report_switch(poll_dev->input, SW_RFKILL_ALL,
  309. new_rfk_state);
  310. input_sync(poll_dev->input);
  311. }
  312. }
  313. static struct proc_dir_entry *toshiba_proc_dir /*= 0*/ ;
  314. static struct backlight_device *toshiba_backlight_device;
  315. static int force_fan;
  316. static int last_key_event;
  317. static int key_event_valid;
  318. typedef struct _ProcItem {
  319. const char *name;
  320. char *(*read_func) (char *);
  321. unsigned long (*write_func) (const char *, unsigned long);
  322. } ProcItem;
  323. /* proc file handlers
  324. */
  325. static int
  326. dispatch_read(char *page, char **start, off_t off, int count, int *eof,
  327. ProcItem * item)
  328. {
  329. char *p = page;
  330. int len;
  331. if (off == 0)
  332. p = item->read_func(p);
  333. /* ISSUE: I don't understand this code */
  334. len = (p - page);
  335. if (len <= off + count)
  336. *eof = 1;
  337. *start = page + off;
  338. len -= off;
  339. if (len > count)
  340. len = count;
  341. if (len < 0)
  342. len = 0;
  343. return len;
  344. }
  345. static int
  346. dispatch_write(struct file *file, const char __user * buffer,
  347. unsigned long count, ProcItem * item)
  348. {
  349. int result;
  350. char *tmp_buffer;
  351. /* Arg buffer points to userspace memory, which can't be accessed
  352. * directly. Since we're making a copy, zero-terminate the
  353. * destination so that sscanf can be used on it safely.
  354. */
  355. tmp_buffer = kmalloc(count + 1, GFP_KERNEL);
  356. if (!tmp_buffer)
  357. return -ENOMEM;
  358. if (copy_from_user(tmp_buffer, buffer, count)) {
  359. result = -EFAULT;
  360. } else {
  361. tmp_buffer[count] = 0;
  362. result = item->write_func(tmp_buffer, count);
  363. }
  364. kfree(tmp_buffer);
  365. return result;
  366. }
  367. static int get_lcd(struct backlight_device *bd)
  368. {
  369. u32 hci_result;
  370. u32 value;
  371. hci_read1(HCI_LCD_BRIGHTNESS, &value, &hci_result);
  372. if (hci_result == HCI_SUCCESS) {
  373. return (value >> HCI_LCD_BRIGHTNESS_SHIFT);
  374. } else
  375. return -EFAULT;
  376. }
  377. static char *read_lcd(char *p)
  378. {
  379. int value = get_lcd(NULL);
  380. if (value >= 0) {
  381. p += sprintf(p, "brightness: %d\n", value);
  382. p += sprintf(p, "brightness_levels: %d\n",
  383. HCI_LCD_BRIGHTNESS_LEVELS);
  384. } else {
  385. printk(MY_ERR "Error reading LCD brightness\n");
  386. }
  387. return p;
  388. }
  389. static int set_lcd(int value)
  390. {
  391. u32 hci_result;
  392. value = value << HCI_LCD_BRIGHTNESS_SHIFT;
  393. hci_write1(HCI_LCD_BRIGHTNESS, value, &hci_result);
  394. if (hci_result != HCI_SUCCESS)
  395. return -EFAULT;
  396. return 0;
  397. }
  398. static int set_lcd_status(struct backlight_device *bd)
  399. {
  400. return set_lcd(bd->props.brightness);
  401. }
  402. static unsigned long write_lcd(const char *buffer, unsigned long count)
  403. {
  404. int value;
  405. int ret;
  406. if (sscanf(buffer, " brightness : %i", &value) == 1 &&
  407. value >= 0 && value < HCI_LCD_BRIGHTNESS_LEVELS) {
  408. ret = set_lcd(value);
  409. if (ret == 0)
  410. ret = count;
  411. } else {
  412. ret = -EINVAL;
  413. }
  414. return ret;
  415. }
  416. static char *read_video(char *p)
  417. {
  418. u32 hci_result;
  419. u32 value;
  420. hci_read1(HCI_VIDEO_OUT, &value, &hci_result);
  421. if (hci_result == HCI_SUCCESS) {
  422. int is_lcd = (value & HCI_VIDEO_OUT_LCD) ? 1 : 0;
  423. int is_crt = (value & HCI_VIDEO_OUT_CRT) ? 1 : 0;
  424. int is_tv = (value & HCI_VIDEO_OUT_TV) ? 1 : 0;
  425. p += sprintf(p, "lcd_out: %d\n", is_lcd);
  426. p += sprintf(p, "crt_out: %d\n", is_crt);
  427. p += sprintf(p, "tv_out: %d\n", is_tv);
  428. } else {
  429. printk(MY_ERR "Error reading video out status\n");
  430. }
  431. return p;
  432. }
  433. static unsigned long write_video(const char *buffer, unsigned long count)
  434. {
  435. int value;
  436. int remain = count;
  437. int lcd_out = -1;
  438. int crt_out = -1;
  439. int tv_out = -1;
  440. u32 hci_result;
  441. u32 video_out;
  442. /* scan expression. Multiple expressions may be delimited with ;
  443. *
  444. * NOTE: to keep scanning simple, invalid fields are ignored
  445. */
  446. while (remain) {
  447. if (sscanf(buffer, " lcd_out : %i", &value) == 1)
  448. lcd_out = value & 1;
  449. else if (sscanf(buffer, " crt_out : %i", &value) == 1)
  450. crt_out = value & 1;
  451. else if (sscanf(buffer, " tv_out : %i", &value) == 1)
  452. tv_out = value & 1;
  453. /* advance to one character past the next ; */
  454. do {
  455. ++buffer;
  456. --remain;
  457. }
  458. while (remain && *(buffer - 1) != ';');
  459. }
  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 char *read_fan(char *p)
  479. {
  480. u32 hci_result;
  481. u32 value;
  482. hci_read1(HCI_FAN, &value, &hci_result);
  483. if (hci_result == HCI_SUCCESS) {
  484. p += sprintf(p, "running: %d\n", (value > 0));
  485. p += sprintf(p, "force_on: %d\n", force_fan);
  486. } else {
  487. printk(MY_ERR "Error reading fan status\n");
  488. }
  489. return p;
  490. }
  491. static unsigned long write_fan(const char *buffer, unsigned long count)
  492. {
  493. int value;
  494. u32 hci_result;
  495. if (sscanf(buffer, " force_on : %i", &value) == 1 &&
  496. value >= 0 && value <= 1) {
  497. hci_write1(HCI_FAN, value, &hci_result);
  498. if (hci_result != HCI_SUCCESS)
  499. return -EFAULT;
  500. else
  501. force_fan = value;
  502. } else {
  503. return -EINVAL;
  504. }
  505. return count;
  506. }
  507. static char *read_keys(char *p)
  508. {
  509. u32 hci_result;
  510. u32 value;
  511. if (!key_event_valid) {
  512. hci_read1(HCI_SYSTEM_EVENT, &value, &hci_result);
  513. if (hci_result == HCI_SUCCESS) {
  514. key_event_valid = 1;
  515. last_key_event = value;
  516. } else if (hci_result == HCI_EMPTY) {
  517. /* better luck next time */
  518. } else if (hci_result == HCI_NOT_SUPPORTED) {
  519. /* This is a workaround for an unresolved issue on
  520. * some machines where system events sporadically
  521. * become disabled. */
  522. hci_write1(HCI_SYSTEM_EVENT, 1, &hci_result);
  523. printk(MY_NOTICE "Re-enabled hotkeys\n");
  524. } else {
  525. printk(MY_ERR "Error reading hotkey status\n");
  526. goto end;
  527. }
  528. }
  529. p += sprintf(p, "hotkey_ready: %d\n", key_event_valid);
  530. p += sprintf(p, "hotkey: 0x%04x\n", last_key_event);
  531. end:
  532. return p;
  533. }
  534. static unsigned long write_keys(const char *buffer, unsigned long count)
  535. {
  536. int value;
  537. if (sscanf(buffer, " hotkey_ready : %i", &value) == 1 && value == 0) {
  538. key_event_valid = 0;
  539. } else {
  540. return -EINVAL;
  541. }
  542. return count;
  543. }
  544. static char *read_version(char *p)
  545. {
  546. p += sprintf(p, "driver: %s\n", TOSHIBA_ACPI_VERSION);
  547. p += sprintf(p, "proc_interface: %d\n",
  548. PROC_INTERFACE_VERSION);
  549. return p;
  550. }
  551. /* proc and module init
  552. */
  553. #define PROC_TOSHIBA "toshiba"
  554. static ProcItem proc_items[] = {
  555. {"lcd", read_lcd, write_lcd},
  556. {"video", read_video, write_video},
  557. {"fan", read_fan, write_fan},
  558. {"keys", read_keys, write_keys},
  559. {"version", read_version, NULL},
  560. {NULL}
  561. };
  562. static acpi_status __init add_device(void)
  563. {
  564. struct proc_dir_entry *proc;
  565. ProcItem *item;
  566. for (item = proc_items; item->name; ++item) {
  567. proc = create_proc_read_entry(item->name,
  568. S_IFREG | S_IRUGO | S_IWUSR,
  569. toshiba_proc_dir,
  570. (read_proc_t *) dispatch_read,
  571. item);
  572. if (proc)
  573. proc->owner = THIS_MODULE;
  574. if (proc && item->write_func)
  575. proc->write_proc = (write_proc_t *) dispatch_write;
  576. }
  577. return AE_OK;
  578. }
  579. static acpi_status remove_device(void)
  580. {
  581. ProcItem *item;
  582. for (item = proc_items; item->name; ++item)
  583. remove_proc_entry(item->name, toshiba_proc_dir);
  584. return AE_OK;
  585. }
  586. static struct backlight_ops toshiba_backlight_data = {
  587. .get_brightness = get_lcd,
  588. .update_status = set_lcd_status,
  589. };
  590. static void toshiba_acpi_exit(void)
  591. {
  592. if (toshiba_acpi.poll_dev) {
  593. input_unregister_polled_device(toshiba_acpi.poll_dev);
  594. input_free_polled_device(toshiba_acpi.poll_dev);
  595. }
  596. if (toshiba_acpi.rfk_dev)
  597. rfkill_unregister(toshiba_acpi.rfk_dev);
  598. if (toshiba_backlight_device)
  599. backlight_device_unregister(toshiba_backlight_device);
  600. remove_device();
  601. if (toshiba_proc_dir)
  602. remove_proc_entry(PROC_TOSHIBA, acpi_root_dir);
  603. platform_device_unregister(toshiba_acpi.p_dev);
  604. return;
  605. }
  606. static int __init toshiba_acpi_init(void)
  607. {
  608. acpi_status status = AE_OK;
  609. u32 hci_result;
  610. bool bt_present;
  611. bool bt_on;
  612. bool radio_on;
  613. int ret = 0;
  614. if (acpi_disabled)
  615. return -ENODEV;
  616. /* simple device detection: look for HCI method */
  617. if (is_valid_acpi_path(METHOD_HCI_1))
  618. method_hci = METHOD_HCI_1;
  619. else if (is_valid_acpi_path(METHOD_HCI_2))
  620. method_hci = METHOD_HCI_2;
  621. else
  622. return -ENODEV;
  623. printk(MY_INFO "Toshiba Laptop ACPI Extras version %s\n",
  624. TOSHIBA_ACPI_VERSION);
  625. printk(MY_INFO " HCI method: %s\n", method_hci);
  626. mutex_init(&toshiba_acpi.mutex);
  627. toshiba_acpi.p_dev = platform_device_register_simple("toshiba_acpi",
  628. -1, NULL, 0);
  629. if (IS_ERR(toshiba_acpi.p_dev)) {
  630. ret = PTR_ERR(toshiba_acpi.p_dev);
  631. printk(MY_ERR "unable to register platform device\n");
  632. toshiba_acpi.p_dev = NULL;
  633. toshiba_acpi_exit();
  634. return ret;
  635. }
  636. force_fan = 0;
  637. key_event_valid = 0;
  638. /* enable event fifo */
  639. hci_write1(HCI_SYSTEM_EVENT, 1, &hci_result);
  640. toshiba_proc_dir = proc_mkdir(PROC_TOSHIBA, acpi_root_dir);
  641. if (!toshiba_proc_dir) {
  642. toshiba_acpi_exit();
  643. return -ENODEV;
  644. } else {
  645. toshiba_proc_dir->owner = THIS_MODULE;
  646. status = add_device();
  647. if (ACPI_FAILURE(status)) {
  648. toshiba_acpi_exit();
  649. return -ENODEV;
  650. }
  651. }
  652. toshiba_backlight_device = backlight_device_register("toshiba",
  653. &toshiba_acpi.p_dev->dev,
  654. NULL,
  655. &toshiba_backlight_data);
  656. if (IS_ERR(toshiba_backlight_device)) {
  657. ret = PTR_ERR(toshiba_backlight_device);
  658. printk(KERN_ERR "Could not register toshiba backlight device\n");
  659. toshiba_backlight_device = NULL;
  660. toshiba_acpi_exit();
  661. return ret;
  662. }
  663. toshiba_backlight_device->props.max_brightness = HCI_LCD_BRIGHTNESS_LEVELS - 1;
  664. /* Register rfkill switch for Bluetooth */
  665. if (hci_get_bt_present(&bt_present) == HCI_SUCCESS && bt_present) {
  666. toshiba_acpi.rfk_dev = rfkill_allocate(&toshiba_acpi.p_dev->dev,
  667. RFKILL_TYPE_BLUETOOTH);
  668. if (!toshiba_acpi.rfk_dev) {
  669. printk(MY_ERR "unable to allocate rfkill device\n");
  670. toshiba_acpi_exit();
  671. return -ENOMEM;
  672. }
  673. toshiba_acpi.rfk_dev->name = toshiba_acpi.bt_name;
  674. toshiba_acpi.rfk_dev->toggle_radio = bt_rfkill_toggle_radio;
  675. toshiba_acpi.rfk_dev->user_claim_unsupported = 1;
  676. toshiba_acpi.rfk_dev->data = &toshiba_acpi;
  677. if (hci_get_bt_on(&bt_on) == HCI_SUCCESS && bt_on) {
  678. toshiba_acpi.rfk_dev->state = RFKILL_STATE_UNBLOCKED;
  679. } else if (hci_get_radio_state(&radio_on) == HCI_SUCCESS &&
  680. radio_on) {
  681. toshiba_acpi.rfk_dev->state = RFKILL_STATE_SOFT_BLOCKED;
  682. } else {
  683. toshiba_acpi.rfk_dev->state = RFKILL_STATE_HARD_BLOCKED;
  684. }
  685. ret = rfkill_register(toshiba_acpi.rfk_dev);
  686. if (ret) {
  687. printk(MY_ERR "unable to register rfkill device\n");
  688. toshiba_acpi_exit();
  689. return -ENOMEM;
  690. }
  691. /* Register input device for kill switch */
  692. toshiba_acpi.poll_dev = input_allocate_polled_device();
  693. if (!toshiba_acpi.poll_dev) {
  694. printk(MY_ERR
  695. "unable to allocate kill-switch input device\n");
  696. toshiba_acpi_exit();
  697. return -ENOMEM;
  698. }
  699. toshiba_acpi.poll_dev->private = &toshiba_acpi;
  700. toshiba_acpi.poll_dev->poll = bt_poll_rfkill;
  701. toshiba_acpi.poll_dev->poll_interval = 1000; /* msecs */
  702. toshiba_acpi.poll_dev->input->name = toshiba_acpi.rfk_name;
  703. toshiba_acpi.poll_dev->input->id.bustype = BUS_HOST;
  704. /* Toshiba USB ID */
  705. toshiba_acpi.poll_dev->input->id.vendor = 0x0930;
  706. set_bit(EV_SW, toshiba_acpi.poll_dev->input->evbit);
  707. set_bit(SW_RFKILL_ALL, toshiba_acpi.poll_dev->input->swbit);
  708. input_report_switch(toshiba_acpi.poll_dev->input,
  709. SW_RFKILL_ALL, TRUE);
  710. input_sync(toshiba_acpi.poll_dev->input);
  711. ret = input_register_polled_device(toshiba_acpi.poll_dev);
  712. if (ret) {
  713. printk(MY_ERR
  714. "unable to register kill-switch input device\n");
  715. toshiba_acpi_exit();
  716. return ret;
  717. }
  718. }
  719. return 0;
  720. }
  721. module_init(toshiba_acpi_init);
  722. module_exit(toshiba_acpi_exit);