2015-07-16 23:32:54

by Azael Avalos

[permalink] [raw]
Subject: [PATCH] toshiba_acpi: Add /dev/toshiba_acpi device

There were previous attempts to "merge" the toshiba SMM module to the
toshiba_acpi one, they were trying to imitate what the old toshiba
module does, however, some models (TOS1900 devices) come with a
"crippled" implementation and do not provide all the "features" a
"genuine" Toshiba BIOS does.

This patch adds a new device called toshiba_acpi, which aim is to
enable userspace to access the SMM on Toshiba laptops via ACPI calls.

Creating a new convenience _IOWR command to access the SCI functions
by opening/closing the SCI internally to avoid buggy BIOS, while at
the same time providing backwards compatibility.

Older programs who wish to access the SMM on newer models can do it
without much code change, as the toshiba.h header was modified to
reflect these changes as well as adds all the toshiba_acpi paths
and commands.

Signed-off-by: Azael Avalos <[email protected]>
---
Documentation/ioctl/ioctl-number.txt | 2 +-
drivers/platform/x86/toshiba_acpi.c | 91 ++++++++++++++++++++++++++++++++++++
include/uapi/linux/toshiba.h | 32 +++++++++++--
3 files changed, 121 insertions(+), 4 deletions(-)

diff --git a/Documentation/ioctl/ioctl-number.txt b/Documentation/ioctl/ioctl-number.txt
index 51f4221..18babaf 100644
--- a/Documentation/ioctl/ioctl-number.txt
+++ b/Documentation/ioctl/ioctl-number.txt
@@ -263,7 +263,7 @@ Code Seq#(hex) Include File Comments
's' all linux/cdk.h
't' 00-7F linux/ppp-ioctl.h
't' 80-8F linux/isdn_ppp.h
-'t' 90 linux/toshiba.h
+'t' 90-91 linux/toshiba.h toshiba and toshiba_acpi SMM
'u' 00-1F linux/smb_fs.h gone
'u' 20-3F linux/uvcvideo.h USB video class host driver
'v' 00-1F linux/ext2_fs.h conflict!
diff --git a/drivers/platform/x86/toshiba_acpi.c b/drivers/platform/x86/toshiba_acpi.c
index 58d6119..dc4255b 100644
--- a/drivers/platform/x86/toshiba_acpi.c
+++ b/drivers/platform/x86/toshiba_acpi.c
@@ -50,6 +50,8 @@
#include <linux/acpi.h>
#include <linux/dmi.h>
#include <linux/uaccess.h>
+#include <linux/miscdevice.h>
+#include <linux/toshiba.h>
#include <acpi/video.h>

MODULE_AUTHOR("John Belmonte");
@@ -170,6 +172,7 @@ struct toshiba_acpi_dev {
struct led_classdev led_dev;
struct led_classdev kbd_led;
struct led_classdev eco_led;
+ struct miscdevice miscdev;

int force_fan;
int last_key_event;
@@ -2262,6 +2265,81 @@ static struct attribute_group toshiba_attr_group = {
};

/*
+ * Misc device
+ */
+static int toshiba_acpi_smm_bridge(SMMRegisters *regs)
+{
+ u32 in[TCI_WORDS] = { regs->eax, regs->ebx, regs->ecx,
+ regs->edx, regs->esi, regs->edi };
+ u32 out[TCI_WORDS];
+ acpi_status status;
+
+ status = tci_raw(toshiba_acpi, in, out);
+ if (ACPI_FAILURE(status)) {
+ pr_err("ACPI call to query TCI registers failed\n");
+ return -EIO;
+ }
+
+ /* Fillout the SMM struct with the TCI call results */
+ regs->eax = out[0];
+ regs->ebx = out[1];
+ regs->ecx = out[2];
+ regs->edx = out[3];
+ regs->esi = out[4];
+ regs->edi = out[5];
+
+ return 0;
+}
+
+static long toshiba_acpi_ioctl(struct file *fp, unsigned int cmd,
+ unsigned long arg)
+{
+ SMMRegisters __user *argp = (SMMRegisters __user *)arg;
+ SMMRegisters regs;
+ int ret;
+
+ if (!argp)
+ return -EINVAL;
+
+ switch (cmd) {
+ case TOSH_SMM:
+ if (copy_from_user(&regs, argp, sizeof(SMMRegisters)))
+ return -EFAULT;
+ ret = toshiba_acpi_smm_bridge(&regs);
+ if (ret)
+ return ret;
+ if (copy_to_user(argp, &regs, sizeof(SMMRegisters)))
+ return -EFAULT;
+ break;
+ case TOSHIBA_ACPI_SCI:
+ if (copy_from_user(&regs, argp, sizeof(SMMRegisters)))
+ return -EFAULT;
+ /* Ensure we are being called with a SCI_{GET, SET} register */
+ if (regs.eax != SCI_GET && regs.eax != SCI_SET)
+ return -EINVAL;
+ if (!sci_open(toshiba_acpi))
+ return -EIO;
+ ret = toshiba_acpi_smm_bridge(&regs);
+ sci_close(toshiba_acpi);
+ if (ret)
+ return ret;
+ if (copy_to_user(argp, &regs, sizeof(SMMRegisters)))
+ return -EFAULT;
+ break;
+ default:
+ return -EINVAL;
+ }
+
+ return 0;
+}
+
+static const struct file_operations toshiba_acpi_fops = {
+ .owner = THIS_MODULE,
+ .unlocked_ioctl = toshiba_acpi_ioctl,
+ .llseek = noop_llseek,
+};
+
+/*
* Hotkeys
*/
static int toshiba_acpi_enable_hotkeys(struct toshiba_acpi_dev *dev)
@@ -2559,6 +2637,8 @@ static int toshiba_acpi_remove(struct acpi_device *acpi_dev)
{
struct toshiba_acpi_dev *dev = acpi_driver_data(acpi_dev);

+ misc_deregister(&dev->miscdev);
+
remove_toshiba_proc_entries(dev);

if (dev->sysfs_created)
@@ -2630,6 +2710,17 @@ static int toshiba_acpi_add(struct acpi_device *acpi_dev)
return -ENOMEM;
dev->acpi_dev = acpi_dev;
dev->method_hci = hci_method;
+ dev->miscdev.minor = MISC_DYNAMIC_MINOR;
+ dev->miscdev.name = "toshiba_acpi";
+ dev->miscdev.fops = &toshiba_acpi_fops;
+
+ ret = misc_register(&dev->miscdev);
+ if (ret) {
+ pr_err("Failed to register miscdevice\n");
+ kfree(dev);
+ return ret;
+ }
+
acpi_dev->driver_data = dev;
dev_set_drvdata(&acpi_dev->dev, dev);

diff --git a/include/uapi/linux/toshiba.h b/include/uapi/linux/toshiba.h
index e9bef5b..c58bf4b 100644
--- a/include/uapi/linux/toshiba.h
+++ b/include/uapi/linux/toshiba.h
@@ -1,6 +1,7 @@
/* toshiba.h -- Linux driver for accessing the SMM on Toshiba laptops
*
* Copyright (c) 1996-2000 Jonathan A. Buzzard ([email protected])
+ * Copyright (c) 2015 Azael Avalos <[email protected]>
*
* Thanks to Juergen Heinzl <[email protected]> for the pointers
* on making sure the structure is aligned and packed.
@@ -20,9 +21,18 @@
#ifndef _UAPI_LINUX_TOSHIBA_H
#define _UAPI_LINUX_TOSHIBA_H

-#define TOSH_PROC "/proc/toshiba"
-#define TOSH_DEVICE "/dev/toshiba"
-#define TOSH_SMM _IOWR('t', 0x90, int) /* broken: meant 24 bytes */
+/*
+ * Toshiba modules paths
+ */
+
+#define TOSH_PROC "/proc/toshiba"
+#define TOSH_DEVICE "/dev/toshiba"
+#define TOSHIBA_ACPI_PROC "/proc/acpi/toshiba"
+#define TOSHIBA_ACPI_DEVICE "/dev/toshiba_acpi"
+
+/*
+ * Toshiba SMM structure
+ */

typedef struct {
unsigned int eax;
@@ -33,5 +43,21 @@ typedef struct {
unsigned int edi __attribute__ ((packed));
} SMMRegisters;

+/*
+ * IOCTLs (0x90 - 0x91)
+ */
+
+#define TOSH_SMM _IOWR('t', 0x90, SMMRegisters)
+/*
+ * Convenience toshiba_acpi command.
+ *
+ * The System Configuration Interface (SCI) is opened/closed internally
+ * to avoid userspace of buggy BIOSes.
+ *
+ * The toshiba_acpi module checks whether the eax register is set with
+ * SCI_GET (0xf300) or SCI_SET (0xf400), returning -EINVAL if not.
+ */
+#define TOSHIBA_ACPI_SCI _IOWR('t', 0x91, SMMRegisters)
+

#endif /* _UAPI_LINUX_TOSHIBA_H */
--
2.4.2


2015-07-17 06:42:30

by Darren Hart

[permalink] [raw]
Subject: Re: [PATCH] toshiba_acpi: Add /dev/toshiba_acpi device

On Thu, Jul 16, 2015 at 05:32:44PM -0600, Azael Avalos wrote:
> There were previous attempts to "merge" the toshiba SMM module to the
> toshiba_acpi one, they were trying to imitate what the old toshiba
> module does, however, some models (TOS1900 devices) come with a
> "crippled" implementation and do not provide all the "features" a
> "genuine" Toshiba BIOS does.
>
> This patch adds a new device called toshiba_acpi, which aim is to
> enable userspace to access the SMM on Toshiba laptops via ACPI calls.
>
> Creating a new convenience _IOWR command to access the SCI functions
> by opening/closing the SCI internally to avoid buggy BIOS, while at
> the same time providing backwards compatibility.
>
> Older programs who wish to access the SMM on newer models can do it
> without much code change, as the toshiba.h header was modified to
> reflect these changes as well as adds all the toshiba_acpi paths
> and commands.
>
> Signed-off-by: Azael Avalos <[email protected]>

This and the other 2 are queued to for-review. They will remain there for a few
days to give others a chance to review. After that, I will provide a thorough
review and move them into for-next for 4.3

They do pass all my preliminary tests, checkpatch, build, and ninja-check.

As this is the first series that I've received since enabling a lot of new
automation, I wanted to say thanks for clean series that is ready review. Nice.

--
Darren Hart
Intel Open Source Technology Center

2015-07-20 21:36:42

by Darren Hart

[permalink] [raw]
Subject: Re: [PATCH] toshiba_acpi: Add /dev/toshiba_acpi device

On Thu, Jul 16, 2015 at 05:32:44PM -0600, Azael Avalos wrote:
> There were previous attempts to "merge" the toshiba SMM module to the
> toshiba_acpi one, they were trying to imitate what the old toshiba
> module does, however, some models (TOS1900 devices) come with a
> "crippled" implementation and do not provide all the "features" a
> "genuine" Toshiba BIOS does.
>
> This patch adds a new device called toshiba_acpi, which aim is to
> enable userspace to access the SMM on Toshiba laptops via ACPI calls.
>
> Creating a new convenience _IOWR command to access the SCI functions
> by opening/closing the SCI internally to avoid buggy BIOS, while at
> the same time providing backwards compatibility.
>
> Older programs who wish to access the SMM on newer models can do it
> without much code change, as the toshiba.h header was modified to
> reflect these changes as well as adds all the toshiba_acpi paths
> and commands.

To clarify, the newer models didn't work at all previously, so this is not
changing the user:kernel interface, but rather enabling it on newer machines,
and programs wishing to take advantage of that can do so with minimal change.

Correct?

--
Darren Hart
Intel Open Source Technology Center

2015-07-20 21:55:23

by Azael Avalos

[permalink] [raw]
Subject: Re: [PATCH] toshiba_acpi: Add /dev/toshiba_acpi device

Hi Darren,

2015-07-20 15:36 GMT-06:00 Darren Hart <[email protected]>:
> On Thu, Jul 16, 2015 at 05:32:44PM -0600, Azael Avalos wrote:
>> There were previous attempts to "merge" the toshiba SMM module to the
>> toshiba_acpi one, they were trying to imitate what the old toshiba
>> module does, however, some models (TOS1900 devices) come with a
>> "crippled" implementation and do not provide all the "features" a
>> "genuine" Toshiba BIOS does.
>>
>> This patch adds a new device called toshiba_acpi, which aim is to
>> enable userspace to access the SMM on Toshiba laptops via ACPI calls.
>>
>> Creating a new convenience _IOWR command to access the SCI functions
>> by opening/closing the SCI internally to avoid buggy BIOS, while at
>> the same time providing backwards compatibility.
>>
>> Older programs who wish to access the SMM on newer models can do it
>> without much code change, as the toshiba.h header was modified to
>> reflect these changes as well as adds all the toshiba_acpi paths
>> and commands.
>
> To clarify, the newer models didn't work at all previously, so this is not
> changing the user:kernel interface, but rather enabling it on newer machines,
> and programs wishing to take advantage of that can do so with minimal change.
>
> Correct?

Yes, the only real (or new) change here is the introduction of the IOCTL
"TOSHIBA_ACPI_SCI", which was added to circumvent buggy or "incomplete"
BIOSes found (so far) on TOS1900 devices.

Any new (or old) program that want to access the SMM can do so by pointing
their path to /dev/toshiba_acpi instead of /dev/toshiba, however, it is strongly
recommended to use the new IOCTL for any SCI command to avoid any buggy
BIOS.

>
> --
> Darren Hart
> Intel Open Source Technology Center


Cheers
Azael


--
-- El mundo apesta y vosotros apestais tambien --