toshiba_acpi.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859
  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. }
  311. }
  312. static struct proc_dir_entry *toshiba_proc_dir /*= 0*/ ;
  313. static struct backlight_device *toshiba_backlight_device;
  314. static int force_fan;
  315. static int last_key_event;
  316. static int key_event_valid;
  317. typedef struct _ProcItem {
  318. const char *name;
  319. char *(*read_func) (char *);
  320. unsigned long (*write_func) (const char *, unsigned long);
  321. } ProcItem;
  322. /* proc file handlers
  323. */
  324. static int
  325. dispatch_read(char *page, char **start, off_t off, int count, int *eof,
  326. ProcItem * item)
  327. {
  328. char *p = page;
  329. int len;
  330. if (off == 0)
  331. p = item->read_func(p);
  332. /* ISSUE: I don't understand this code */
  333. len = (p - page);
  334. if (len <= off + count)
  335. *eof = 1;
  336. *start = page + off;
  337. len -= off;
  338. if (len > count)
  339. len = count;
  340. if (len < 0)
  341. len = 0;
  342. return len;
  343. }
  344. static int
  345. dispatch_write(struct file *file, const char __user * buffer,
  346. unsigned long count, ProcItem * item)
  347. {
  348. int result;
  349. char *tmp_buffer;
  350. /* Arg buffer points to userspace memory, which can't be accessed
  351. * directly. Since we're making a copy, zero-terminate the
  352. * destination so that sscanf can be used on it safely.
  353. */
  354. tmp_buffer = kmalloc(count + 1, GFP_KERNEL);
  355. if (!tmp_buffer)
  356. return -ENOMEM;
  357. if (copy_from_user(tmp_buffer, buffer, count)) {
  358. result = -EFAULT;
  359. } else {
  360. tmp_buffer[count] = 0;
  361. result = item->write_func(tmp_buffer, count);
  362. }
  363. kfree(tmp_buffer);
  364. return result;
  365. }
  366. static int get_lcd(struct backlight_device *bd)
  367. {
  368. u32 hci_result;
  369. u32 value;
  370. hci_read1(HCI_LCD_BRIGHTNESS, &value, &hci_result);
  371. if (hci_result == HCI_SUCCESS) {
  372. return (value >> HCI_LCD_BRIGHTNESS_SHIFT);
  373. } else
  374. return -EFAULT;
  375. }
  376. static char *read_lcd(char *p)
  377. {
  378. int value = get_lcd(NULL);
  379. if (value >= 0) {
  380. p += sprintf(p, "brightness: %d\n", value);
  381. p += sprintf(p, "brightness_levels: %d\n",
  382. HCI_LCD_BRIGHTNESS_LEVELS);
  383. } else {
  384. printk(MY_ERR "Error reading LCD brightness\n");
  385. }
  386. return p;
  387. }
  388. static int set_lcd(int value)
  389. {
  390. u32 hci_result;
  391. value = value << HCI_LCD_BRIGHTNESS_SHIFT;
  392. hci_write1(HCI_LCD_BRIGHTNESS, value, &hci_result);
  393. if (hci_result != HCI_SUCCESS)
  394. return -EFAULT;
  395. return 0;
  396. }
  397. static int set_lcd_status(struct backlight_device *bd)
  398. {
  399. return set_lcd(bd->props.brightness);
  400. }
  401. static unsigned long write_lcd(const char *buffer, unsigned long count)
  402. {
  403. int value;
  404. int ret;
  405. if (sscanf(buffer, " brightness : %i", &value) == 1 &&
  406. value >= 0 && value < HCI_LCD_BRIGHTNESS_LEVELS) {
  407. ret = set_lcd(value);
  408. if (ret == 0)
  409. ret = count;
  410. } else {
  411. ret = -EINVAL;
  412. }
  413. return ret;
  414. }
  415. static char *read_video(char *p)
  416. {
  417. u32 hci_result;
  418. u32 value;
  419. hci_read1(HCI_VIDEO_OUT, &value, &hci_result);
  420. if (hci_result == HCI_SUCCESS) {
  421. int is_lcd = (value & HCI_VIDEO_OUT_LCD) ? 1 : 0;
  422. int is_crt = (value & HCI_VIDEO_OUT_CRT) ? 1 : 0;
  423. int is_tv = (value & HCI_VIDEO_OUT_TV) ? 1 : 0;
  424. p += sprintf(p, "lcd_out: %d\n", is_lcd);
  425. p += sprintf(p, "crt_out: %d\n", is_crt);
  426. p += sprintf(p, "tv_out: %d\n", is_tv);
  427. } else {
  428. printk(MY_ERR "Error reading video out status\n");
  429. }
  430. return p;
  431. }
  432. static unsigned long write_video(const char *buffer, unsigned long count)
  433. {
  434. int value;
  435. int remain = count;
  436. int lcd_out = -1;
  437. int crt_out = -1;
  438. int tv_out = -1;
  439. u32 hci_result;
  440. u32 video_out;
  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. hci_read1(HCI_VIDEO_OUT, &video_out, &hci_result);
  460. if (hci_result == HCI_SUCCESS) {
  461. int new_video_out = video_out;
  462. if (lcd_out != -1)
  463. _set_bit(&new_video_out, HCI_VIDEO_OUT_LCD, lcd_out);
  464. if (crt_out != -1)
  465. _set_bit(&new_video_out, HCI_VIDEO_OUT_CRT, crt_out);
  466. if (tv_out != -1)
  467. _set_bit(&new_video_out, HCI_VIDEO_OUT_TV, tv_out);
  468. /* To avoid unnecessary video disruption, only write the new
  469. * video setting if something changed. */
  470. if (new_video_out != video_out)
  471. write_acpi_int(METHOD_VIDEO_OUT, new_video_out);
  472. } else {
  473. return -EFAULT;
  474. }
  475. return count;
  476. }
  477. static char *read_fan(char *p)
  478. {
  479. u32 hci_result;
  480. u32 value;
  481. hci_read1(HCI_FAN, &value, &hci_result);
  482. if (hci_result == HCI_SUCCESS) {
  483. p += sprintf(p, "running: %d\n", (value > 0));
  484. p += sprintf(p, "force_on: %d\n", force_fan);
  485. } else {
  486. printk(MY_ERR "Error reading fan status\n");
  487. }
  488. return p;
  489. }
  490. static unsigned long write_fan(const char *buffer, unsigned long count)
  491. {
  492. int value;
  493. u32 hci_result;
  494. if (sscanf(buffer, " force_on : %i", &value) == 1 &&
  495. value >= 0 && value <= 1) {
  496. hci_write1(HCI_FAN, value, &hci_result);
  497. if (hci_result != HCI_SUCCESS)
  498. return -EFAULT;
  499. else
  500. force_fan = value;
  501. } else {
  502. return -EINVAL;
  503. }
  504. return count;
  505. }
  506. static char *read_keys(char *p)
  507. {
  508. u32 hci_result;
  509. u32 value;
  510. if (!key_event_valid) {
  511. hci_read1(HCI_SYSTEM_EVENT, &value, &hci_result);
  512. if (hci_result == HCI_SUCCESS) {
  513. key_event_valid = 1;
  514. last_key_event = value;
  515. } else if (hci_result == HCI_EMPTY) {
  516. /* better luck next time */
  517. } else if (hci_result == HCI_NOT_SUPPORTED) {
  518. /* This is a workaround for an unresolved issue on
  519. * some machines where system events sporadically
  520. * become disabled. */
  521. hci_write1(HCI_SYSTEM_EVENT, 1, &hci_result);
  522. printk(MY_NOTICE "Re-enabled hotkeys\n");
  523. } else {
  524. printk(MY_ERR "Error reading hotkey status\n");
  525. goto end;
  526. }
  527. }
  528. p += sprintf(p, "hotkey_ready: %d\n", key_event_valid);
  529. p += sprintf(p, "hotkey: 0x%04x\n", last_key_event);
  530. end:
  531. return p;
  532. }
  533. static unsigned long write_keys(const char *buffer, unsigned long count)
  534. {
  535. int value;
  536. if (sscanf(buffer, " hotkey_ready : %i", &value) == 1 && value == 0) {
  537. key_event_valid = 0;
  538. } else {
  539. return -EINVAL;
  540. }
  541. return count;
  542. }
  543. static char *read_version(char *p)
  544. {
  545. p += sprintf(p, "driver: %s\n", TOSHIBA_ACPI_VERSION);
  546. p += sprintf(p, "proc_interface: %d\n",
  547. PROC_INTERFACE_VERSION);
  548. return p;
  549. }
  550. /* proc and module init
  551. */
  552. #define PROC_TOSHIBA "toshiba"
  553. static ProcItem proc_items[] = {
  554. {"lcd", read_lcd, write_lcd},
  555. {"video", read_video, write_video},
  556. {"fan", read_fan, write_fan},
  557. {"keys", read_keys, write_keys},
  558. {"version", read_version, NULL},
  559. {NULL}
  560. };
  561. static acpi_status __init add_device(void)
  562. {
  563. struct proc_dir_entry *proc;
  564. ProcItem *item;
  565. for (item = proc_items; item->name; ++item) {
  566. proc = create_proc_read_entry(item->name,
  567. S_IFREG | S_IRUGO | S_IWUSR,
  568. toshiba_proc_dir,
  569. (read_proc_t *) dispatch_read,
  570. item);
  571. if (proc)
  572. proc->owner = THIS_MODULE;
  573. if (proc && item->write_func)
  574. proc->write_proc = (write_proc_t *) dispatch_write;
  575. }
  576. return AE_OK;
  577. }
  578. static acpi_status remove_device(void)
  579. {
  580. ProcItem *item;
  581. for (item = proc_items; item->name; ++item)
  582. remove_proc_entry(item->name, toshiba_proc_dir);
  583. return AE_OK;
  584. }
  585. static struct backlight_ops toshiba_backlight_data = {
  586. .get_brightness = get_lcd,
  587. .update_status = set_lcd_status,
  588. };
  589. static void toshiba_acpi_exit(void)
  590. {
  591. if (toshiba_acpi.poll_dev) {
  592. input_unregister_polled_device(toshiba_acpi.poll_dev);
  593. input_free_polled_device(toshiba_acpi.poll_dev);
  594. }
  595. if (toshiba_acpi.rfk_dev)
  596. rfkill_unregister(toshiba_acpi.rfk_dev);
  597. if (toshiba_backlight_device)
  598. backlight_device_unregister(toshiba_backlight_device);
  599. remove_device();
  600. if (toshiba_proc_dir)
  601. remove_proc_entry(PROC_TOSHIBA, acpi_root_dir);
  602. platform_device_unregister(toshiba_acpi.p_dev);
  603. return;
  604. }
  605. static int __init toshiba_acpi_init(void)
  606. {
  607. acpi_status status = AE_OK;
  608. u32 hci_result;
  609. bool bt_present;
  610. bool bt_on;
  611. bool radio_on;
  612. int ret = 0;
  613. if (acpi_disabled)
  614. return -ENODEV;
  615. /* simple device detection: look for HCI method */
  616. if (is_valid_acpi_path(METHOD_HCI_1))
  617. method_hci = METHOD_HCI_1;
  618. else if (is_valid_acpi_path(METHOD_HCI_2))
  619. method_hci = METHOD_HCI_2;
  620. else
  621. return -ENODEV;
  622. printk(MY_INFO "Toshiba Laptop ACPI Extras version %s\n",
  623. TOSHIBA_ACPI_VERSION);
  624. printk(MY_INFO " HCI method: %s\n", method_hci);
  625. mutex_init(&toshiba_acpi.mutex);
  626. toshiba_acpi.p_dev = platform_device_register_simple("toshiba_acpi",
  627. -1, NULL, 0);
  628. if (IS_ERR(toshiba_acpi.p_dev)) {
  629. ret = PTR_ERR(toshiba_acpi.p_dev);
  630. printk(MY_ERR "unable to register platform device\n");
  631. toshiba_acpi.p_dev = NULL;
  632. toshiba_acpi_exit();
  633. return ret;
  634. }
  635. force_fan = 0;
  636. key_event_valid = 0;
  637. /* enable event fifo */
  638. hci_write1(HCI_SYSTEM_EVENT, 1, &hci_result);
  639. toshiba_proc_dir = proc_mkdir(PROC_TOSHIBA, acpi_root_dir);
  640. if (!toshiba_proc_dir) {
  641. toshiba_acpi_exit();
  642. return -ENODEV;
  643. } else {
  644. toshiba_proc_dir->owner = THIS_MODULE;
  645. status = add_device();
  646. if (ACPI_FAILURE(status)) {
  647. toshiba_acpi_exit();
  648. return -ENODEV;
  649. }
  650. }
  651. toshiba_backlight_device = backlight_device_register("toshiba",
  652. &toshiba_acpi.p_dev->dev,
  653. NULL,
  654. &toshiba_backlight_data);
  655. if (IS_ERR(toshiba_backlight_device)) {
  656. ret = PTR_ERR(toshiba_backlight_device);
  657. printk(KERN_ERR "Could not register toshiba backlight device\n");
  658. toshiba_backlight_device = NULL;
  659. toshiba_acpi_exit();
  660. return ret;
  661. }
  662. toshiba_backlight_device->props.max_brightness = HCI_LCD_BRIGHTNESS_LEVELS - 1;
  663. /* Register rfkill switch for Bluetooth */
  664. if (hci_get_bt_present(&bt_present) == HCI_SUCCESS && bt_present) {
  665. toshiba_acpi.rfk_dev = rfkill_allocate(&toshiba_acpi.p_dev->dev,
  666. RFKILL_TYPE_BLUETOOTH);
  667. if (!toshiba_acpi.rfk_dev) {
  668. printk(MY_ERR "unable to allocate rfkill device\n");
  669. toshiba_acpi_exit();
  670. return -ENOMEM;
  671. }
  672. toshiba_acpi.rfk_dev->name = toshiba_acpi.bt_name;
  673. toshiba_acpi.rfk_dev->toggle_radio = bt_rfkill_toggle_radio;
  674. toshiba_acpi.rfk_dev->user_claim_unsupported = 1;
  675. toshiba_acpi.rfk_dev->data = &toshiba_acpi;
  676. if (hci_get_bt_on(&bt_on) == HCI_SUCCESS && bt_on) {
  677. toshiba_acpi.rfk_dev->state = RFKILL_STATE_UNBLOCKED;
  678. } else if (hci_get_radio_state(&radio_on) == HCI_SUCCESS &&
  679. radio_on) {
  680. toshiba_acpi.rfk_dev->state = RFKILL_STATE_SOFT_BLOCKED;
  681. } else {
  682. toshiba_acpi.rfk_dev->state = RFKILL_STATE_HARD_BLOCKED;
  683. }
  684. ret = rfkill_register(toshiba_acpi.rfk_dev);
  685. if (ret) {
  686. printk(MY_ERR "unable to register rfkill device\n");
  687. toshiba_acpi_exit();
  688. return -ENOMEM;
  689. }
  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 "unable to allocate kill-switch input device\n");
  695. toshiba_acpi_exit();
  696. return -ENOMEM;
  697. }
  698. toshiba_acpi.poll_dev->private = &toshiba_acpi;
  699. toshiba_acpi.poll_dev->poll = bt_poll_rfkill;
  700. toshiba_acpi.poll_dev->poll_interval = 1000; /* msecs */
  701. toshiba_acpi.poll_dev->input->name = toshiba_acpi.rfk_name;
  702. toshiba_acpi.poll_dev->input->id.bustype = BUS_HOST;
  703. toshiba_acpi.poll_dev->input->id.vendor = 0x0930; /* Toshiba USB ID */
  704. set_bit(EV_SW, toshiba_acpi.poll_dev->input->evbit);
  705. set_bit(SW_RFKILL_ALL, toshiba_acpi.poll_dev->input->swbit);
  706. input_report_switch(toshiba_acpi.poll_dev->input, SW_RFKILL_ALL, TRUE);
  707. ret = input_register_polled_device(toshiba_acpi.poll_dev);
  708. if (ret) {
  709. printk(MY_ERR "unable to register kill-switch input device\n");
  710. rfkill_free(toshiba_acpi.rfk_dev);
  711. toshiba_acpi.rfk_dev = NULL;
  712. toshiba_acpi_exit();
  713. return ret;
  714. }
  715. return 0;
  716. }
  717. module_init(toshiba_acpi_init);
  718. module_exit(toshiba_acpi_exit);