toshiba_acpi.c 19 KB

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