fujitsu-tablet.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  1. /*
  2. * Copyright (C) 2006-2012 Robert Gerlach <khnz@gmx.de>
  3. * Copyright (C) 2005-2006 Jan Rychter <jan@rychter.com>
  4. *
  5. * You can redistribute and/or modify this program under the terms of the
  6. * GNU General Public License version 2 as published by the Free Software
  7. * Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
  12. * Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along
  15. * with this program; if not, write to the Free Software Foundation, Inc.,
  16. * 59 Temple Place Suite 330, Boston, MA 02111-1307, USA.
  17. */
  18. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  19. #include <linux/kernel.h>
  20. #include <linux/module.h>
  21. #include <linux/init.h>
  22. #include <linux/bitops.h>
  23. #include <linux/io.h>
  24. #include <linux/ioport.h>
  25. #include <linux/acpi.h>
  26. #include <linux/device.h>
  27. #include <linux/interrupt.h>
  28. #include <linux/input.h>
  29. #include <linux/delay.h>
  30. #include <linux/dmi.h>
  31. #define MODULENAME "fujitsu-tablet"
  32. #define ACPI_FUJITSU_CLASS "fujitsu"
  33. #define INVERT_TABLET_MODE_BIT 0x01
  34. #define INVERT_DOCK_STATE_BIT 0x02
  35. #define FORCE_TABLET_MODE_IF_UNDOCK 0x04
  36. #define KEYMAP_LEN 16
  37. static const struct acpi_device_id fujitsu_ids[] = {
  38. { .id = "FUJ02BD" },
  39. { .id = "FUJ02BF" },
  40. { .id = "" }
  41. };
  42. struct fujitsu_config {
  43. unsigned short keymap[KEYMAP_LEN];
  44. unsigned int quirks;
  45. };
  46. static unsigned short keymap_Lifebook_Tseries[KEYMAP_LEN] __initdata = {
  47. KEY_RESERVED,
  48. KEY_RESERVED,
  49. KEY_RESERVED,
  50. KEY_RESERVED,
  51. KEY_SCROLLDOWN,
  52. KEY_SCROLLUP,
  53. KEY_ROTATE_DISPLAY,
  54. KEY_LEFTCTRL,
  55. KEY_BRIGHTNESSUP,
  56. KEY_BRIGHTNESSDOWN,
  57. KEY_BRIGHTNESS_ZERO,
  58. KEY_RESERVED,
  59. KEY_RESERVED,
  60. KEY_RESERVED,
  61. KEY_RESERVED,
  62. KEY_LEFTALT
  63. };
  64. static unsigned short keymap_Lifebook_T901[KEYMAP_LEN] __initdata = {
  65. KEY_RESERVED,
  66. KEY_RESERVED,
  67. KEY_RESERVED,
  68. KEY_RESERVED,
  69. KEY_SCROLLDOWN,
  70. KEY_SCROLLUP,
  71. KEY_CYCLEWINDOWS,
  72. KEY_LEFTCTRL,
  73. KEY_RESERVED,
  74. KEY_RESERVED,
  75. KEY_RESERVED,
  76. KEY_RESERVED,
  77. KEY_RESERVED,
  78. KEY_RESERVED,
  79. KEY_RESERVED,
  80. KEY_LEFTMETA
  81. };
  82. static unsigned short keymap_Lifebook_T902[KEYMAP_LEN] __initdata = {
  83. KEY_RESERVED,
  84. KEY_VOLUMEDOWN,
  85. KEY_VOLUMEUP,
  86. KEY_CYCLEWINDOWS,
  87. KEY_PROG1,
  88. KEY_PROG2,
  89. KEY_LEFTMETA,
  90. KEY_RESERVED,
  91. KEY_RESERVED,
  92. KEY_RESERVED,
  93. KEY_RESERVED,
  94. KEY_RESERVED,
  95. KEY_RESERVED,
  96. KEY_RESERVED,
  97. KEY_RESERVED,
  98. KEY_RESERVED,
  99. };
  100. static unsigned short keymap_Lifebook_U810[KEYMAP_LEN] __initdata = {
  101. KEY_RESERVED,
  102. KEY_RESERVED,
  103. KEY_RESERVED,
  104. KEY_RESERVED,
  105. KEY_PROG1,
  106. KEY_PROG2,
  107. KEY_ROTATE_DISPLAY,
  108. KEY_RESERVED,
  109. KEY_RESERVED,
  110. KEY_RESERVED,
  111. KEY_UP,
  112. KEY_DOWN,
  113. KEY_RESERVED,
  114. KEY_RESERVED,
  115. KEY_LEFTCTRL,
  116. KEY_LEFTALT
  117. };
  118. static unsigned short keymap_Stylistic_Tseries[KEYMAP_LEN] __initdata = {
  119. KEY_RESERVED,
  120. KEY_RESERVED,
  121. KEY_RESERVED,
  122. KEY_RESERVED,
  123. KEY_PRINT,
  124. KEY_BACKSPACE,
  125. KEY_SPACE,
  126. KEY_ENTER,
  127. KEY_BRIGHTNESSUP,
  128. KEY_BRIGHTNESSDOWN,
  129. KEY_DOWN,
  130. KEY_UP,
  131. KEY_SCROLLUP,
  132. KEY_SCROLLDOWN,
  133. KEY_LEFTCTRL,
  134. KEY_LEFTALT
  135. };
  136. static unsigned short keymap_Stylistic_ST5xxx[KEYMAP_LEN] __initdata = {
  137. KEY_RESERVED,
  138. KEY_RESERVED,
  139. KEY_RESERVED,
  140. KEY_RESERVED,
  141. KEY_MAIL,
  142. KEY_ROTATE_DISPLAY,
  143. KEY_ESC,
  144. KEY_ENTER,
  145. KEY_BRIGHTNESSUP,
  146. KEY_BRIGHTNESSDOWN,
  147. KEY_DOWN,
  148. KEY_UP,
  149. KEY_SCROLLUP,
  150. KEY_SCROLLDOWN,
  151. KEY_LEFTCTRL,
  152. KEY_LEFTALT
  153. };
  154. static struct {
  155. struct input_dev *idev;
  156. struct fujitsu_config config;
  157. unsigned long prev_keymask;
  158. char phys[21];
  159. int irq;
  160. int io_base;
  161. int io_length;
  162. } fujitsu;
  163. static u8 fujitsu_ack(void)
  164. {
  165. return inb(fujitsu.io_base + 2);
  166. }
  167. static u8 fujitsu_status(void)
  168. {
  169. return inb(fujitsu.io_base + 6);
  170. }
  171. static u8 fujitsu_read_register(const u8 addr)
  172. {
  173. outb(addr, fujitsu.io_base);
  174. return inb(fujitsu.io_base + 4);
  175. }
  176. static void fujitsu_send_state(void)
  177. {
  178. int state;
  179. int dock, tablet_mode;
  180. state = fujitsu_read_register(0xdd);
  181. dock = state & 0x02;
  182. if (fujitsu.config.quirks & INVERT_DOCK_STATE_BIT)
  183. dock = !dock;
  184. if ((fujitsu.config.quirks & FORCE_TABLET_MODE_IF_UNDOCK) && (!dock)) {
  185. tablet_mode = 1;
  186. } else{
  187. tablet_mode = state & 0x01;
  188. if (fujitsu.config.quirks & INVERT_TABLET_MODE_BIT)
  189. tablet_mode = !tablet_mode;
  190. }
  191. input_report_switch(fujitsu.idev, SW_DOCK, dock);
  192. input_report_switch(fujitsu.idev, SW_TABLET_MODE, tablet_mode);
  193. input_sync(fujitsu.idev);
  194. }
  195. static void fujitsu_reset(void)
  196. {
  197. int timeout = 50;
  198. fujitsu_ack();
  199. while ((fujitsu_status() & 0x02) && (--timeout))
  200. msleep(20);
  201. fujitsu_send_state();
  202. }
  203. static int input_fujitsu_setup(struct device *parent, const char *name,
  204. const char *phys)
  205. {
  206. struct input_dev *idev;
  207. int error;
  208. int i;
  209. idev = input_allocate_device();
  210. if (!idev)
  211. return -ENOMEM;
  212. idev->dev.parent = parent;
  213. idev->phys = phys;
  214. idev->name = name;
  215. idev->id.bustype = BUS_HOST;
  216. idev->id.vendor = 0x1734; /* Fujitsu Siemens Computer GmbH */
  217. idev->id.product = 0x0001;
  218. idev->id.version = 0x0101;
  219. idev->keycode = fujitsu.config.keymap;
  220. idev->keycodesize = sizeof(fujitsu.config.keymap[0]);
  221. idev->keycodemax = ARRAY_SIZE(fujitsu.config.keymap);
  222. __set_bit(EV_REP, idev->evbit);
  223. for (i = 0; i < ARRAY_SIZE(fujitsu.config.keymap); i++)
  224. if (fujitsu.config.keymap[i])
  225. input_set_capability(idev, EV_KEY, fujitsu.config.keymap[i]);
  226. input_set_capability(idev, EV_MSC, MSC_SCAN);
  227. input_set_capability(idev, EV_SW, SW_DOCK);
  228. input_set_capability(idev, EV_SW, SW_TABLET_MODE);
  229. error = input_register_device(idev);
  230. if (error) {
  231. input_free_device(idev);
  232. return error;
  233. }
  234. fujitsu.idev = idev;
  235. return 0;
  236. }
  237. static void input_fujitsu_remove(void)
  238. {
  239. input_unregister_device(fujitsu.idev);
  240. }
  241. static irqreturn_t fujitsu_interrupt(int irq, void *dev_id)
  242. {
  243. unsigned long keymask, changed;
  244. unsigned int keycode;
  245. int pressed;
  246. int i;
  247. if (unlikely(!(fujitsu_status() & 0x01)))
  248. return IRQ_NONE;
  249. fujitsu_send_state();
  250. keymask = fujitsu_read_register(0xde);
  251. keymask |= fujitsu_read_register(0xdf) << 8;
  252. keymask ^= 0xffff;
  253. changed = keymask ^ fujitsu.prev_keymask;
  254. if (changed) {
  255. fujitsu.prev_keymask = keymask;
  256. for_each_set_bit(i, &changed, KEYMAP_LEN) {
  257. keycode = fujitsu.config.keymap[i];
  258. pressed = keymask & changed & BIT(i);
  259. if (pressed)
  260. input_event(fujitsu.idev, EV_MSC, MSC_SCAN, i);
  261. input_report_key(fujitsu.idev, keycode, pressed);
  262. input_sync(fujitsu.idev);
  263. }
  264. }
  265. fujitsu_ack();
  266. return IRQ_HANDLED;
  267. }
  268. static void __init fujitsu_dmi_common(const struct dmi_system_id *dmi)
  269. {
  270. pr_info("%s\n", dmi->ident);
  271. memcpy(fujitsu.config.keymap, dmi->driver_data,
  272. sizeof(fujitsu.config.keymap));
  273. }
  274. static int __init fujitsu_dmi_lifebook(const struct dmi_system_id *dmi)
  275. {
  276. fujitsu_dmi_common(dmi);
  277. fujitsu.config.quirks |= INVERT_TABLET_MODE_BIT;
  278. return 1;
  279. }
  280. static int __init fujitsu_dmi_stylistic(const struct dmi_system_id *dmi)
  281. {
  282. fujitsu_dmi_common(dmi);
  283. fujitsu.config.quirks |= FORCE_TABLET_MODE_IF_UNDOCK;
  284. fujitsu.config.quirks |= INVERT_DOCK_STATE_BIT;
  285. return 1;
  286. }
  287. static const struct dmi_system_id dmi_ids[] __initconst = {
  288. {
  289. .callback = fujitsu_dmi_lifebook,
  290. .ident = "Fujitsu Lifebook T901",
  291. .matches = {
  292. DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU"),
  293. DMI_MATCH(DMI_PRODUCT_NAME, "LifeBook T901")
  294. },
  295. .driver_data = keymap_Lifebook_T901
  296. },
  297. {
  298. .callback = fujitsu_dmi_lifebook,
  299. .ident = "Fujitsu Lifebook T901",
  300. .matches = {
  301. DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU"),
  302. DMI_MATCH(DMI_PRODUCT_NAME, "LIFEBOOK T901")
  303. },
  304. .driver_data = keymap_Lifebook_T901
  305. },
  306. {
  307. .callback = fujitsu_dmi_lifebook,
  308. .ident = "Fujitsu Lifebook T902",
  309. .matches = {
  310. DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU"),
  311. DMI_MATCH(DMI_PRODUCT_NAME, "LIFEBOOK T902")
  312. },
  313. .driver_data = keymap_Lifebook_T902
  314. },
  315. {
  316. .callback = fujitsu_dmi_lifebook,
  317. .ident = "Fujitsu Siemens P/T Series",
  318. .matches = {
  319. DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU"),
  320. DMI_MATCH(DMI_PRODUCT_NAME, "LIFEBOOK")
  321. },
  322. .driver_data = keymap_Lifebook_Tseries
  323. },
  324. {
  325. .callback = fujitsu_dmi_lifebook,
  326. .ident = "Fujitsu Lifebook T Series",
  327. .matches = {
  328. DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU"),
  329. DMI_MATCH(DMI_PRODUCT_NAME, "LifeBook T")
  330. },
  331. .driver_data = keymap_Lifebook_Tseries
  332. },
  333. {
  334. .callback = fujitsu_dmi_stylistic,
  335. .ident = "Fujitsu Siemens Stylistic T Series",
  336. .matches = {
  337. DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU"),
  338. DMI_MATCH(DMI_PRODUCT_NAME, "Stylistic T")
  339. },
  340. .driver_data = keymap_Stylistic_Tseries
  341. },
  342. {
  343. .callback = fujitsu_dmi_lifebook,
  344. .ident = "Fujitsu LifeBook U810",
  345. .matches = {
  346. DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU"),
  347. DMI_MATCH(DMI_PRODUCT_NAME, "LifeBook U810")
  348. },
  349. .driver_data = keymap_Lifebook_U810
  350. },
  351. {
  352. .callback = fujitsu_dmi_stylistic,
  353. .ident = "Fujitsu Siemens Stylistic ST5xxx Series",
  354. .matches = {
  355. DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU"),
  356. DMI_MATCH(DMI_PRODUCT_NAME, "STYLISTIC ST5")
  357. },
  358. .driver_data = keymap_Stylistic_ST5xxx
  359. },
  360. {
  361. .callback = fujitsu_dmi_stylistic,
  362. .ident = "Fujitsu Siemens Stylistic ST5xxx Series",
  363. .matches = {
  364. DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU"),
  365. DMI_MATCH(DMI_PRODUCT_NAME, "Stylistic ST5")
  366. },
  367. .driver_data = keymap_Stylistic_ST5xxx
  368. },
  369. {
  370. .callback = fujitsu_dmi_lifebook,
  371. .ident = "Unknown (using defaults)",
  372. .matches = {
  373. DMI_MATCH(DMI_SYS_VENDOR, ""),
  374. DMI_MATCH(DMI_PRODUCT_NAME, "")
  375. },
  376. .driver_data = keymap_Lifebook_Tseries
  377. },
  378. { NULL }
  379. };
  380. static acpi_status fujitsu_walk_resources(struct acpi_resource *res, void *data)
  381. {
  382. switch (res->type) {
  383. case ACPI_RESOURCE_TYPE_IRQ:
  384. fujitsu.irq = res->data.irq.interrupts[0];
  385. return AE_OK;
  386. case ACPI_RESOURCE_TYPE_IO:
  387. fujitsu.io_base = res->data.io.minimum;
  388. fujitsu.io_length = res->data.io.address_length;
  389. return AE_OK;
  390. case ACPI_RESOURCE_TYPE_END_TAG:
  391. if (fujitsu.irq && fujitsu.io_base)
  392. return AE_OK;
  393. else
  394. return AE_NOT_FOUND;
  395. default:
  396. return AE_ERROR;
  397. }
  398. }
  399. static int acpi_fujitsu_add(struct acpi_device *adev)
  400. {
  401. acpi_status status;
  402. int error;
  403. if (!adev)
  404. return -EINVAL;
  405. status = acpi_walk_resources(adev->handle, METHOD_NAME__CRS,
  406. fujitsu_walk_resources, NULL);
  407. if (ACPI_FAILURE(status) || !fujitsu.irq || !fujitsu.io_base)
  408. return -ENODEV;
  409. sprintf(acpi_device_name(adev), "Fujitsu %s", acpi_device_hid(adev));
  410. sprintf(acpi_device_class(adev), "%s", ACPI_FUJITSU_CLASS);
  411. snprintf(fujitsu.phys, sizeof(fujitsu.phys),
  412. "%s/input0", acpi_device_hid(adev));
  413. error = input_fujitsu_setup(&adev->dev,
  414. acpi_device_name(adev), fujitsu.phys);
  415. if (error)
  416. return error;
  417. if (!request_region(fujitsu.io_base, fujitsu.io_length, MODULENAME)) {
  418. input_fujitsu_remove();
  419. return -EBUSY;
  420. }
  421. fujitsu_reset();
  422. error = request_irq(fujitsu.irq, fujitsu_interrupt,
  423. IRQF_SHARED, MODULENAME, fujitsu_interrupt);
  424. if (error) {
  425. release_region(fujitsu.io_base, fujitsu.io_length);
  426. input_fujitsu_remove();
  427. return error;
  428. }
  429. return 0;
  430. }
  431. static int acpi_fujitsu_remove(struct acpi_device *adev)
  432. {
  433. free_irq(fujitsu.irq, fujitsu_interrupt);
  434. release_region(fujitsu.io_base, fujitsu.io_length);
  435. input_fujitsu_remove();
  436. return 0;
  437. }
  438. #ifdef CONFIG_PM_SLEEP
  439. static int acpi_fujitsu_resume(struct device *dev)
  440. {
  441. fujitsu_reset();
  442. return 0;
  443. }
  444. #endif
  445. static SIMPLE_DEV_PM_OPS(acpi_fujitsu_pm, NULL, acpi_fujitsu_resume);
  446. static struct acpi_driver acpi_fujitsu_driver = {
  447. .name = MODULENAME,
  448. .class = "hotkey",
  449. .ids = fujitsu_ids,
  450. .ops = {
  451. .add = acpi_fujitsu_add,
  452. .remove = acpi_fujitsu_remove,
  453. },
  454. .drv.pm = &acpi_fujitsu_pm,
  455. };
  456. static int __init fujitsu_module_init(void)
  457. {
  458. int error;
  459. dmi_check_system(dmi_ids);
  460. error = acpi_bus_register_driver(&acpi_fujitsu_driver);
  461. if (error)
  462. return error;
  463. return 0;
  464. }
  465. static void __exit fujitsu_module_exit(void)
  466. {
  467. acpi_bus_unregister_driver(&acpi_fujitsu_driver);
  468. }
  469. module_init(fujitsu_module_init);
  470. module_exit(fujitsu_module_exit);
  471. MODULE_AUTHOR("Robert Gerlach <khnz@gmx.de>");
  472. MODULE_DESCRIPTION("Fujitsu tablet pc extras driver");
  473. MODULE_LICENSE("GPL");
  474. MODULE_VERSION("2.5");
  475. MODULE_DEVICE_TABLE(acpi, fujitsu_ids);