1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2006-2012 Robert Gerlach <khnz@gmx.de>
4 * Copyright (C) 2005-2006 Jan Rychter <jan@rychter.com>
7 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9 #include <linux/kernel.h>
10 #include <linux/module.h>
11 #include <linux/init.h>
12 #include <linux/bitops.h>
14 #include <linux/ioport.h>
15 #include <linux/acpi.h>
16 #include <linux/device.h>
17 #include <linux/interrupt.h>
18 #include <linux/input.h>
19 #include <linux/delay.h>
20 #include <linux/dmi.h>
22 #define MODULENAME "fujitsu-tablet"
24 #define ACPI_FUJITSU_CLASS "fujitsu"
26 #define INVERT_TABLET_MODE_BIT 0x01
27 #define INVERT_DOCK_STATE_BIT 0x02
28 #define FORCE_TABLET_MODE_IF_UNDOCK 0x04
32 static const struct acpi_device_id fujitsu_ids
[] = {
38 struct fujitsu_config
{
39 unsigned short keymap
[KEYMAP_LEN
];
43 static unsigned short keymap_Lifebook_Tseries
[KEYMAP_LEN
] __initdata
= {
62 static unsigned short keymap_Lifebook_T901
[KEYMAP_LEN
] __initdata
= {
81 static unsigned short keymap_Lifebook_T902
[KEYMAP_LEN
] __initdata
= {
100 static unsigned short keymap_Lifebook_U810
[KEYMAP_LEN
] __initdata
= {
119 static unsigned short keymap_Stylistic_Tseries
[KEYMAP_LEN
] __initdata
= {
138 static unsigned short keymap_Stylistic_ST5xxx
[KEYMAP_LEN
] __initdata
= {
158 struct input_dev
*idev
;
159 struct fujitsu_config config
;
160 unsigned long prev_keymask
;
169 static u8
fujitsu_ack(void)
171 return inb(fujitsu
.io_base
+ 2);
174 static u8
fujitsu_status(void)
176 return inb(fujitsu
.io_base
+ 6);
179 static u8
fujitsu_read_register(const u8 addr
)
181 outb(addr
, fujitsu
.io_base
);
182 return inb(fujitsu
.io_base
+ 4);
185 static void fujitsu_send_state(void)
188 int dock
, tablet_mode
;
190 state
= fujitsu_read_register(0xdd);
193 if (fujitsu
.config
.quirks
& INVERT_DOCK_STATE_BIT
)
196 if ((fujitsu
.config
.quirks
& FORCE_TABLET_MODE_IF_UNDOCK
) && (!dock
)) {
199 tablet_mode
= state
& 0x01;
200 if (fujitsu
.config
.quirks
& INVERT_TABLET_MODE_BIT
)
201 tablet_mode
= !tablet_mode
;
204 input_report_switch(fujitsu
.idev
, SW_DOCK
, dock
);
205 input_report_switch(fujitsu
.idev
, SW_TABLET_MODE
, tablet_mode
);
206 input_sync(fujitsu
.idev
);
209 static void fujitsu_reset(void)
215 while ((fujitsu_status() & 0x02) && (--timeout
))
218 fujitsu_send_state();
221 static int input_fujitsu_setup(struct device
*parent
, const char *name
,
224 struct input_dev
*idev
;
228 idev
= input_allocate_device();
232 idev
->dev
.parent
= parent
;
235 idev
->id
.bustype
= BUS_HOST
;
236 idev
->id
.vendor
= 0x1734; /* Fujitsu Siemens Computer GmbH */
237 idev
->id
.product
= 0x0001;
238 idev
->id
.version
= 0x0101;
240 idev
->keycode
= fujitsu
.config
.keymap
;
241 idev
->keycodesize
= sizeof(fujitsu
.config
.keymap
[0]);
242 idev
->keycodemax
= ARRAY_SIZE(fujitsu
.config
.keymap
);
244 __set_bit(EV_REP
, idev
->evbit
);
246 for (i
= 0; i
< ARRAY_SIZE(fujitsu
.config
.keymap
); i
++)
247 if (fujitsu
.config
.keymap
[i
])
248 input_set_capability(idev
, EV_KEY
, fujitsu
.config
.keymap
[i
]);
250 input_set_capability(idev
, EV_MSC
, MSC_SCAN
);
252 input_set_capability(idev
, EV_SW
, SW_DOCK
);
253 input_set_capability(idev
, EV_SW
, SW_TABLET_MODE
);
255 error
= input_register_device(idev
);
257 input_free_device(idev
);
265 static void input_fujitsu_remove(void)
267 input_unregister_device(fujitsu
.idev
);
270 static irqreturn_t
fujitsu_interrupt(int irq
, void *dev_id
)
272 unsigned long keymask
, changed
;
273 unsigned int keycode
;
277 if (unlikely(!(fujitsu_status() & 0x01)))
280 fujitsu_send_state();
282 keymask
= fujitsu_read_register(0xde);
283 keymask
|= fujitsu_read_register(0xdf) << 8;
286 changed
= keymask
^ fujitsu
.prev_keymask
;
288 fujitsu
.prev_keymask
= keymask
;
290 for_each_set_bit(i
, &changed
, KEYMAP_LEN
) {
291 keycode
= fujitsu
.config
.keymap
[i
];
292 pressed
= keymask
& changed
& BIT(i
);
295 input_event(fujitsu
.idev
, EV_MSC
, MSC_SCAN
, i
);
297 input_report_key(fujitsu
.idev
, keycode
, pressed
);
298 input_sync(fujitsu
.idev
);
306 static void __init
fujitsu_dmi_common(const struct dmi_system_id
*dmi
)
308 pr_info("%s\n", dmi
->ident
);
309 memcpy(fujitsu
.config
.keymap
, dmi
->driver_data
,
310 sizeof(fujitsu
.config
.keymap
));
313 static int __init
fujitsu_dmi_lifebook(const struct dmi_system_id
*dmi
)
315 fujitsu_dmi_common(dmi
);
316 fujitsu
.config
.quirks
|= INVERT_TABLET_MODE_BIT
;
320 static int __init
fujitsu_dmi_stylistic(const struct dmi_system_id
*dmi
)
322 fujitsu_dmi_common(dmi
);
323 fujitsu
.config
.quirks
|= FORCE_TABLET_MODE_IF_UNDOCK
;
324 fujitsu
.config
.quirks
|= INVERT_DOCK_STATE_BIT
;
328 static const struct dmi_system_id dmi_ids
[] __initconst
= {
330 .callback
= fujitsu_dmi_lifebook
,
331 .ident
= "Fujitsu Lifebook T901",
333 DMI_MATCH(DMI_SYS_VENDOR
, "FUJITSU"),
334 DMI_MATCH(DMI_PRODUCT_NAME
, "LifeBook T901")
336 .driver_data
= keymap_Lifebook_T901
339 .callback
= fujitsu_dmi_lifebook
,
340 .ident
= "Fujitsu Lifebook T901",
342 DMI_MATCH(DMI_SYS_VENDOR
, "FUJITSU"),
343 DMI_MATCH(DMI_PRODUCT_NAME
, "LIFEBOOK T901")
345 .driver_data
= keymap_Lifebook_T901
348 .callback
= fujitsu_dmi_lifebook
,
349 .ident
= "Fujitsu Lifebook T902",
351 DMI_MATCH(DMI_SYS_VENDOR
, "FUJITSU"),
352 DMI_MATCH(DMI_PRODUCT_NAME
, "LIFEBOOK T902")
354 .driver_data
= keymap_Lifebook_T902
357 .callback
= fujitsu_dmi_lifebook
,
358 .ident
= "Fujitsu Siemens P/T Series",
360 DMI_MATCH(DMI_SYS_VENDOR
, "FUJITSU"),
361 DMI_MATCH(DMI_PRODUCT_NAME
, "LIFEBOOK")
363 .driver_data
= keymap_Lifebook_Tseries
366 .callback
= fujitsu_dmi_lifebook
,
367 .ident
= "Fujitsu Lifebook T Series",
369 DMI_MATCH(DMI_SYS_VENDOR
, "FUJITSU"),
370 DMI_MATCH(DMI_PRODUCT_NAME
, "LifeBook T")
372 .driver_data
= keymap_Lifebook_Tseries
375 .callback
= fujitsu_dmi_stylistic
,
376 .ident
= "Fujitsu Siemens Stylistic T Series",
378 DMI_MATCH(DMI_SYS_VENDOR
, "FUJITSU"),
379 DMI_MATCH(DMI_PRODUCT_NAME
, "Stylistic T")
381 .driver_data
= keymap_Stylistic_Tseries
384 .callback
= fujitsu_dmi_lifebook
,
385 .ident
= "Fujitsu LifeBook U810",
387 DMI_MATCH(DMI_SYS_VENDOR
, "FUJITSU"),
388 DMI_MATCH(DMI_PRODUCT_NAME
, "LifeBook U810")
390 .driver_data
= keymap_Lifebook_U810
393 .callback
= fujitsu_dmi_stylistic
,
394 .ident
= "Fujitsu Siemens Stylistic ST5xxx Series",
396 DMI_MATCH(DMI_SYS_VENDOR
, "FUJITSU"),
397 DMI_MATCH(DMI_PRODUCT_NAME
, "STYLISTIC ST5")
399 .driver_data
= keymap_Stylistic_ST5xxx
402 .callback
= fujitsu_dmi_stylistic
,
403 .ident
= "Fujitsu Siemens Stylistic ST5xxx Series",
405 DMI_MATCH(DMI_SYS_VENDOR
, "FUJITSU"),
406 DMI_MATCH(DMI_PRODUCT_NAME
, "Stylistic ST5")
408 .driver_data
= keymap_Stylistic_ST5xxx
411 .callback
= fujitsu_dmi_lifebook
,
412 .ident
= "Unknown (using defaults)",
414 DMI_MATCH(DMI_SYS_VENDOR
, ""),
415 DMI_MATCH(DMI_PRODUCT_NAME
, "")
417 .driver_data
= keymap_Lifebook_Tseries
422 static acpi_status
fujitsu_walk_resources(struct acpi_resource
*res
, void *data
)
425 case ACPI_RESOURCE_TYPE_IRQ
:
426 fujitsu
.irq
= res
->data
.irq
.interrupts
[0];
429 case ACPI_RESOURCE_TYPE_IO
:
430 fujitsu
.io_base
= res
->data
.io
.minimum
;
431 fujitsu
.io_length
= res
->data
.io
.address_length
;
434 case ACPI_RESOURCE_TYPE_END_TAG
:
435 if (fujitsu
.irq
&& fujitsu
.io_base
)
445 static int acpi_fujitsu_add(struct acpi_device
*adev
)
453 status
= acpi_walk_resources(adev
->handle
, METHOD_NAME__CRS
,
454 fujitsu_walk_resources
, NULL
);
455 if (ACPI_FAILURE(status
) || !fujitsu
.irq
|| !fujitsu
.io_base
)
458 sprintf(acpi_device_name(adev
), "Fujitsu %s", acpi_device_hid(adev
));
459 sprintf(acpi_device_class(adev
), "%s", ACPI_FUJITSU_CLASS
);
461 snprintf(fujitsu
.phys
, sizeof(fujitsu
.phys
),
462 "%s/input0", acpi_device_hid(adev
));
464 error
= input_fujitsu_setup(&adev
->dev
,
465 acpi_device_name(adev
), fujitsu
.phys
);
469 if (!request_region(fujitsu
.io_base
, fujitsu
.io_length
, MODULENAME
)) {
470 input_fujitsu_remove();
476 error
= request_irq(fujitsu
.irq
, fujitsu_interrupt
,
477 IRQF_SHARED
, MODULENAME
, fujitsu_interrupt
);
479 release_region(fujitsu
.io_base
, fujitsu
.io_length
);
480 input_fujitsu_remove();
487 static int acpi_fujitsu_remove(struct acpi_device
*adev
)
489 free_irq(fujitsu
.irq
, fujitsu_interrupt
);
490 release_region(fujitsu
.io_base
, fujitsu
.io_length
);
491 input_fujitsu_remove();
495 #ifdef CONFIG_PM_SLEEP
496 static int acpi_fujitsu_resume(struct device
*dev
)
503 static SIMPLE_DEV_PM_OPS(acpi_fujitsu_pm
, NULL
, acpi_fujitsu_resume
);
505 static struct acpi_driver acpi_fujitsu_driver
= {
510 .add
= acpi_fujitsu_add
,
511 .remove
= acpi_fujitsu_remove
,
513 .drv
.pm
= &acpi_fujitsu_pm
,
516 static int __init
fujitsu_module_init(void)
520 dmi_check_system(dmi_ids
);
522 error
= acpi_bus_register_driver(&acpi_fujitsu_driver
);
529 static void __exit
fujitsu_module_exit(void)
531 acpi_bus_unregister_driver(&acpi_fujitsu_driver
);
534 module_init(fujitsu_module_init
);
535 module_exit(fujitsu_module_exit
);
537 MODULE_AUTHOR("Robert Gerlach <khnz@gmx.de>");
538 MODULE_DESCRIPTION("Fujitsu tablet pc extras driver");
539 MODULE_LICENSE("GPL");
540 MODULE_VERSION("2.5");
542 MODULE_DEVICE_TABLE(acpi
, fujitsu_ids
);