Re: [PATCH] platform-driver-x86: ACPI EC Extra driver for Oaktrail
From: Corentin Chary
Date: Thu Jan 06 2011 - 02:29:29 EST
Hi,
On Thu, Jan 6, 2011 at 3:59 AM, Yin Kangkai <kangkai.yin@xxxxxxxxxxxxxxx> wrote:
> Hi,
>
> This driver implements an Extra ACPI EC driver for products based on
> Intel Oaktrail platform. ÂIt is programming the EC space, through
> existing ACPI EC driver, to provide user space layer the sysfs and
> rfkill interfaces to enable/disable the Camera, Bluetooth, GPS, WiFi,
> 3G, and to show the status of Touchscreen.
>
> $ ./scripts/checkpatch.pl
> 0001-platform-driver-x86-ACPI-EC-Extra-driver-for-Oaktrai.patch
> total: 0 errors, 0 warnings, 366 lines checked
>
> 0001-platform-driver-x86-ACPI-EC-Extra-driver-for-Oaktrai.patch has no
> obvious style problems and is ready for submission.
>
>
> From 5d817d0b120b52ae3685edf1d67a552ab7b6b12a Mon Sep 17 00:00:00 2001
> From: Yin Kangkai <kangkai.yin@xxxxxxxxx>
> Date: Wed, 22 Dec 2010 10:53:36 +0800
> Subject: [PATCH] platform-driver-x86: ACPI EC Extra driver for Oaktrail
>
> This driver implements an Extra ACPI EC driver for products based on Intel
> Oaktrail platform. ÂIt is programming the EC space, through existing ACPI EC
> driver, to provide user space layer the sysfs and rfkill interfaces to
> enable/disable the Camera, Bluetooth, GPS, WiFi, 3G, and to show the status of
> Touchscreen.
>
> Signed-off-by: Yin Kangkai <kangkai.yin@xxxxxxxxx>
> ---
> Âdrivers/platform/x86/Kconfig     Â|  Â9 +
> Âdrivers/platform/x86/Makefile     |  Â1 +
> Âdrivers/platform/x86/intel_oaktrail.c | Â349 +++++++++++++++++++++++++++++++++
> Â3 files changed, 359 insertions(+), 0 deletions(-)
> Âcreate mode 100644 drivers/platform/x86/intel_oaktrail.c
>
> diff --git a/drivers/platform/x86/Kconfig b/drivers/platform/x86/Kconfig
> index 2b4038a..d1f6981 100644
> --- a/drivers/platform/x86/Kconfig
> +++ b/drivers/platform/x86/Kconfig
> @@ -655,4 +655,13 @@ config XO1_RFKILL
> Â Â Â Â ÂSupport for enabling/disabling the WLAN interface on the OLPC XO-1
> Â Â Â Â Âlaptop.
>
> +config INTEL_OAKTRAIL
> + Â Â Â tristate "Intel Oaktrail Platform Extras"
> + Â Â Â depends on ACPI
> + Â Â Â depends on RFKILL
> + Â Â Â ---help---
> + Â Â Â Â Intel Oaktrail platform need this driver to provide interfaces to
> + Â Â Â Â enable/disable the Camera, WiFi, BT etc. devices. If in doubt, say Y
> + Â Â Â Â here; it will only load on supported platforms.
> +
> Âendif # X86_PLATFORM_DEVICES
> diff --git a/drivers/platform/x86/Makefile b/drivers/platform/x86/Makefile
> index 7ff60e6..add8ab7 100644
> --- a/drivers/platform/x86/Makefile
> +++ b/drivers/platform/x86/Makefile
> @@ -35,3 +35,4 @@ obj-$(CONFIG_INTEL_IPS) Â Â Â Â Â Â Â += intel_ips.o
> Âobj-$(CONFIG_GPIO_INTEL_PMIC) Â+= intel_pmic_gpio.o
> Âobj-$(CONFIG_XO1_RFKILL) Â Â Â += xo1-rfkill.o
> Âobj-$(CONFIG_IBM_RTL) Â Â Â Â Â+= ibm_rtl.o
> +obj-$(CONFIG_INTEL_OAKTRAIL) Â += intel_oaktrail.o
> diff --git a/drivers/platform/x86/intel_oaktrail.c b/drivers/platform/x86/intel_oaktrail.c
> new file mode 100644
> index 0000000..d28d14c
> --- /dev/null
> +++ b/drivers/platform/x86/intel_oaktrail.c
> @@ -0,0 +1,349 @@
> +/*-*-linux-c-*-*/
I don't know what's our general policy about that, but I don't think
each text editor should be allowed to add its own header on each
files. Most of the time you can configure your editor to set the
right indent style based on the path of the file or something like that.
> +/*
> + ÂCopyright (C) 2010 Intel Corporation
> + ÂAuthor: Yin Kangkai (kangkai.yin@xxxxxxxxx)
> +
> + Âbased on Compal driver
> +
> + ÂCopyright (C) 2008 Cezary Jackiewicz <cezary.jackiewicz (at) gmail.com>
> +
> + Âbased on MSI driver
> +
> + ÂCopyright (C) 2006 Lennart Poettering <mzxreary (at) 0pointer (dot) de>
> +
> + ÂThis program is free software; you can redistribute it and/or modify
> + Âit under the terms of the GNU General Public License as published by
> + Âthe Free Software Foundation; either version 2 of the License, or
> + Â(at your option) any later version.
> +
> + ÂThis program is distributed in the hope that it will be useful, but
> + ÂWITHOUT ANY WARRANTY; without even the implied warranty of
> + ÂMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
> + ÂGeneral Public License for more details.
> +
> + ÂYou should have received a copy of the GNU General Public License
> + Âalong with this program; if not, write to the Free Software
> + ÂFoundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
> + Â02110-1301, USA.
> + */
> +
> +/*
> + * intel_oaktrail.c - Intel OakTrail Platform support.
> + *
> + * This driver exports a few files in /sys/devices/platform/intel_oaktrail/:
> + *
> + * gps - GPS subsystem enabled: contains either 0 or 1. (rw)
> + * wifi - WiFi subsystem enabled: contains either 0 or 1. (rw)
> + * wwan - WWAN (3G) subsystem enabled: contains either 0 or 1. (rw)
Is there a reason do add these files in /sys/devices/platform while the
functionality is already provided by rfkill ?
> + * camera - Camera subsystem enabled: contains either 0 or 1. (rw)
> + * bluetooth - Bluetooth subsystem enabled: contains either 0 or 1. (rw)
> + * touchscreen - Touchscreen subsystem enabled: contains either 0 or 1. (ro)
This should be in Documentation/ABI/testing/
> + * In addition to these platform device attributes the driver registers itself
> + * in the Linux rfkill subsystem and is available to userspace under
> + * /sys/class/rfkill/rfkillX/
> + *
> + * This driver might work on other products based on Oaktrail. If you
> + * want to try it you can pass force=1 as argument to the module which
> + * will force it to load even when the DMI data doesn't identify the
> + * product as compatible.
> + */
> +
> +#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
> +
> +#include <linux/module.h>
> +#include <linux/kernel.h>
> +#include <linux/init.h>
> +#include <linux/acpi.h>
> +#include <linux/platform_device.h>
> +#include <linux/dmi.h>
> +#include <linux/rfkill.h>
> +
> +#define DRIVER_NAME Â Â"intel_oaktrail"
> +#define DRIVER_VERSION "0.1"
> +
> +/*
> + * This is the devices status address in EC space, and the control bits
> + * definition:
> + *
> + * (1 << 0): Â Camera enable/disable, RW.
> + * (1 << 1): Â Bluetooth enable/disable, RW.
> + * (1 << 2): Â GPS enable/disable, RW.
> + * (1 << 3): Â WiFi enable/disable, RW.
> + * (1 << 4): Â WWAN (3G) enable/disalbe, RW.
> + * (1 << 5): Â Touchscreen enable/disable, Read Only.
> + */
> +#define OT_EC_DEVICE_STATE_ADDRESS Â Â 0xD6
> +
> +#define OT_EC_CAMERA_MASK Â Â Â(1 << 0)
> +#define OT_EC_BT_MASK Â Â Â Â Â(1 << 1)
> +#define OT_EC_GPS_MASK Â Â Â Â (1 << 2)
> +#define OT_EC_WIFI_MASK Â Â Â Â Â Â Â Â(1 << 3)
> +#define OT_EC_WWAN_MASK Â Â Â Â Â Â Â Â(1 << 4)
> +#define OT_EC_TS_MASK Â Â Â Â Â(1 << 5)
> +
> +static int force;
> +module_param(force, bool, 0);
> +MODULE_PARM_DESC(force, "Force driver load, ignore DMI data");
> +
> +static struct platform_device *oaktrail_device;
> +static struct rfkill *bt_rfkill;
> +static struct rfkill *gps_rfkill;
> +static struct rfkill *wifi_rfkill;
> +static struct rfkill *wwan_rfkill;
Here you could create two (four ?) helpers that contains the logic,
and craft dummy functions which only call the helpers with the right
parameters in your macros.
These helpers could also be used by later functions.
> +#define SIMPLE_MASKED_STORE_SHOW(NAME, MASK) Â Â Â Â Â Â Â Â Â Â Â Â Â \
> +static ssize_t NAME##_show(struct device *dev, Â Â Â Â Â Â Â Â Â Â Â Â \
> + Â Â Â struct device_attribute *attr, char *buf) Â Â Â Â Â Â Â Â Â Â Â \
> +{ Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â\
> + Â Â Â u8 value; Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â \
> + Â Â Â ec_read(OT_EC_DEVICE_STATE_ADDRESS, &value); Â Â Â Â Â Â Â Â Â Â\
> + Â Â Â return sprintf(buf, "%d\n", ((value & MASK) != 0)); Â Â Â Â Â Â \
> +} Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â\
> +static ssize_t NAME##_store(struct device *dev, Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â\
> + Â Â Â struct device_attribute *attr, const char *buf, size_t count) Â \
> +{ Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â\
> + Â Â Â int state; Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â\
> + Â Â Â u8 old_val; Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â \
> + Â Â Â ec_read(OT_EC_DEVICE_STATE_ADDRESS, &old_val); Â Â Â Â Â Â Â Â Â\
> + Â Â Â if (sscanf(buf, "%d", &state) != 1 || (state < 0 || state > 1)) \
> + Â Â Â Â Â Â Â return -EINVAL; Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â \
> + Â Â Â ec_write(OT_EC_DEVICE_STATE_ADDRESS, state ? Â Â Â Â Â Â Â Â Â Â\
> + Â Â Â Â Â Â Â Â(old_val | MASK) : (old_val & ~MASK)); Â Â Â Â Â Â Â Â \
> + Â Â Â return count; Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â \
> +}
> +
> +SIMPLE_MASKED_STORE_SHOW(camera, OT_EC_CAMERA_MASK)
> +SIMPLE_MASKED_STORE_SHOW(bluetooth, OT_EC_BT_MASK)
> +SIMPLE_MASKED_STORE_SHOW(gps, OT_EC_GPS_MASK)
> +SIMPLE_MASKED_STORE_SHOW(wifi, OT_EC_WIFI_MASK)
> +SIMPLE_MASKED_STORE_SHOW(wwan, OT_EC_WWAN_MASK)
> +SIMPLE_MASKED_STORE_SHOW(touchscreen, OT_EC_TS_MASK)
> +
> +static DEVICE_ATTR(camera, 0644, camera_show, camera_store);
> +static DEVICE_ATTR(bluetooth, 0644, bluetooth_show, bluetooth_store);
> +static DEVICE_ATTR(gps, 0644, gps_show, gps_store);
> +static DEVICE_ATTR(wifi, 0644, wifi_show, wifi_store);
> +static DEVICE_ATTR(wwan, 0644, wwan_show, wwan_store);
> +static DEVICE_ATTR(touchscreen, 0444, touchscreen_show, NULL);
> +
> +static struct attribute *oaktrail_attributes[] = {
> + Â Â Â &dev_attr_camera.attr,
> + Â Â Â &dev_attr_bluetooth.attr,
> + Â Â Â &dev_attr_gps.attr,
> + Â Â Â &dev_attr_wifi.attr,
> + Â Â Â &dev_attr_wwan.attr,
> + Â Â Â &dev_attr_touchscreen.attr,
> + Â Â Â NULL
> +};
> +
> +static struct attribute_group oaktrail_attribute_group = {
> + Â Â Â .attrs = oaktrail_attributes
> +};
> +
> +static int oaktrail_rfkill_set(void *data, bool blocked)
> +{
> + Â Â Â u8 value;
> + Â Â Â u8 result;
> + Â Â Â unsigned long radio = (unsigned long) data;
> +
> + Â Â Â ec_read(OT_EC_DEVICE_STATE_ADDRESS, &result);
> +
> + Â Â Â if (!blocked)
> + Â Â Â Â Â Â Â value = (u8) (result | radio);
> + Â Â Â else
> + Â Â Â Â Â Â Â value = (u8) (result & ~radio);
> +
> + Â Â Â ec_write(OT_EC_DEVICE_STATE_ADDRESS, value);
> +
> + Â Â Â return 0;
> +}
> +
> +static const struct rfkill_ops oaktrail_rfkill_ops = {
> + Â Â Â .set_block = oaktrail_rfkill_set,
> +};
> +
> +static int setup_rfkill(void)
oaktrail_rfkill_init() ?
> +{
> + Â Â Â int ret;
> +
> + Â Â Â wifi_rfkill = rfkill_alloc("oaktrail-wifi", &oaktrail_device->dev,
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â RFKILL_TYPE_WLAN, &oaktrail_rfkill_ops,
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â (void *) OT_EC_WIFI_MASK);
> + Â Â Â if (!wifi_rfkill)
> + Â Â Â Â Â Â Â return -ENOMEM;
> +
> + Â Â Â ret = rfkill_register(wifi_rfkill);
> + Â Â Â if (ret)
> + Â Â Â Â Â Â Â goto err_register_wifi;
> +
> + Â Â Â bt_rfkill = rfkill_alloc("oaktrail-bluetooth", &oaktrail_device->dev,
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â RFKILL_TYPE_BLUETOOTH, &oaktrail_rfkill_ops,
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â (void *) OT_EC_BT_MASK);
> + Â Â Â if (!bt_rfkill) {
> + Â Â Â Â Â Â Â ret = -ENOMEM;
> + Â Â Â Â Â Â Â goto err_allocate_bt;
> + Â Â Â }
> + Â Â Â ret = rfkill_register(bt_rfkill);
> + Â Â Â if (ret)
> + Â Â Â Â Â Â Â goto err_register_bt;
> +
> + Â Â Â gps_rfkill = rfkill_alloc("oaktrail-gps", &oaktrail_device->dev,
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â RFKILL_TYPE_GPS, &oaktrail_rfkill_ops,
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â (void *) OT_EC_GPS_MASK);
> + Â Â Â if (!gps_rfkill) {
> + Â Â Â Â Â Â Â ret = -ENOMEM;
> + Â Â Â Â Â Â Â goto err_allocate_gps;
> + Â Â Â }
> + Â Â Â ret = rfkill_register(gps_rfkill);
> + Â Â Â if (ret)
> + Â Â Â Â Â Â Â goto err_register_gps;
> +
> + Â Â Â wwan_rfkill = rfkill_alloc("oaktrail-wwan", &oaktrail_device->dev,
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â RFKILL_TYPE_WWAN, &oaktrail_rfkill_ops,
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â (void *) OT_EC_WWAN_MASK);
> + Â Â Â if (!wwan_rfkill) {
> + Â Â Â Â Â Â Â ret = -ENOMEM;
> + Â Â Â Â Â Â Â goto err_allocate_wwan;
> + Â Â Â }
> + Â Â Â ret = rfkill_register(wwan_rfkill);
> + Â Â Â if (ret)
> + Â Â Â Â Â Â Â goto err_register_wwan;
> +
> + Â Â Â return 0;
> +
> +err_register_wwan:
> + Â Â Â rfkill_destroy(wwan_rfkill);
> +err_allocate_wwan:
> + Â Â Â rfkill_unregister(gps_rfkill);
> +err_register_gps:
> + Â Â Â rfkill_destroy(gps_rfkill);
> +err_allocate_gps:
> + Â Â Â rfkill_unregister(bt_rfkill);
> +err_register_bt:
> + Â Â Â rfkill_destroy(bt_rfkill);
> +err_allocate_bt:
> + Â Â Â rfkill_unregister(wifi_rfkill);
> +err_register_wifi:
> + Â Â Â rfkill_destroy(wifi_rfkill);
> +
> + Â Â Â return ret;
> +}
Here I'd write an helper function to call rfkill_alloc,
rfkill_register and handle
rfkill_register failure. And then, if any of the helper calls fail, just call
oaktrail_rfkill_exit (which which rfkill if the rfkill pointer is NULL or not).
oaktrail_rfkill_exit could also be used in the module exit function.
> +static int __devinit oaktrail_probe(struct platform_device *pdev)
> +{
> + Â Â Â int err;
> +
> + Â Â Â err = sysfs_create_group(&pdev->dev.kobj, &oaktrail_attribute_group);
> + Â Â Â return err;
> +}
return sysfs_create_group(&pdev->dev.kobj, &oaktrail_attribute_group);
we don't really need err right now, do we ?
> +static int __devexit oaktrail_remove(struct platform_device *pdev)
> +{
> + Â Â Â sysfs_remove_group(&pdev->dev.kobj, &oaktrail_attribute_group);
> +
> + Â Â Â return 0;
> +}
> +
> +static struct platform_driver oaktrail_driver = {
> + Â Â Â .driver = {
> + Â Â Â Â Â Â Â .name = DRIVER_NAME,
> + Â Â Â Â Â Â Â .owner = THIS_MODULE,
> + Â Â Â },
> + Â Â Â .probe Â= oaktrail_probe,
> + Â Â Â .remove = __devexit_p(oaktrail_remove)
> +};
> +
> +static int dmi_check_cb(const struct dmi_system_id *id)
> +{
> + Â Â Â pr_info("Identified model '%s'\n", id->ident);
> + Â Â Â return 0;
> +}
> +
> +static struct dmi_system_id __initdata oaktrail_dmi_table[] = {
> + Â Â Â {
> + Â Â Â Â Â Â Â .ident = "OakTrail platform",
> + Â Â Â Â Â Â Â .matches = {
> + Â Â Â Â Â Â Â Â Â Â Â DMI_MATCH(DMI_PRODUCT_NAME, "OakTrail platform"),
> + Â Â Â Â Â Â Â },
> + Â Â Â Â Â Â Â .callback = dmi_check_cb
> + Â Â Â },
> + Â Â Â { }
> +};
> +
> +static int __init oaktrail_init(void)
> +{
> + Â Â Â int ret;
> +
> + Â Â Â if (acpi_disabled) {
> + Â Â Â Â Â Â Â pr_err("ACPI needs to be enabled for this driver to work!\n");
> + Â Â Â Â Â Â Â return -ENODEV;
> + Â Â Â }
> +
> + Â Â Â if (!force && !dmi_check_system(oaktrail_dmi_table)) {
> + Â Â Â Â Â Â Â pr_err("Platform not recognized (You could try the module's force-parameter)");
> + Â Â Â Â Â Â Â return -ENODEV;
> + Â Â Â }
> +
> + Â Â Â ret = platform_driver_register(&oaktrail_driver);
> + Â Â Â if (ret) {
> + Â Â Â Â Â Â Â pr_warning("Unable to register platform driver\n");
> + Â Â Â Â Â Â Â goto err_driver_reg;
> + Â Â Â }
> +
> + Â Â Â oaktrail_device = platform_device_alloc(DRIVER_NAME, -1);
> + Â Â Â if (!oaktrail_device) {
> + Â Â Â Â Â Â Â pr_warning("Unable to allocate platform device\n");
> + Â Â Â Â Â Â Â ret = -ENOMEM;
> + Â Â Â Â Â Â Â goto err_device_alloc;
> + Â Â Â }
> +
> + Â Â Â ret = platform_device_add(oaktrail_device);
> + Â Â Â if (ret) {
> + Â Â Â Â Â Â Â pr_warning("Unable to add platform device\n");
> + Â Â Â Â Â Â Â goto err_device_add;
> + Â Â Â }
> +
> + Â Â Â ret = setup_rfkill();
> + Â Â Â if (ret) {
> + Â Â Â Â Â Â Â pr_warning("Setup rfkill failed\n");
> + Â Â Â Â Â Â Â goto err_rfkill;
> + Â Â Â }
> +
> + Â Â Â pr_info("Driver "DRIVER_VERSION" successfully loaded\n");
> + Â Â Â return 0;
> +
> +err_rfkill:
> + Â Â Â platform_device_del(oaktrail_device);
> +err_device_add:
> + Â Â Â platform_device_put(oaktrail_device);
> +err_device_alloc:
> + Â Â Â platform_driver_unregister(&oaktrail_driver);
> +err_driver_reg:
> + Â Â Â return ret;
> +}
> +
> +static void __exit oaktrail_cleanup(void)
> +{
> + Â Â Â platform_device_unregister(oaktrail_device);
> + Â Â Â platform_driver_unregister(&oaktrail_driver);
> + Â Â Â rfkill_unregister(wifi_rfkill);
> + Â Â Â rfkill_unregister(bt_rfkill);
> + Â Â Â rfkill_unregister(gps_rfkill);
> + Â Â Â rfkill_unregister(wwan_rfkill);
> + Â Â Â rfkill_destroy(wifi_rfkill);
> + Â Â Â rfkill_destroy(bt_rfkill);
> + Â Â Â rfkill_destroy(gps_rfkill);
> + Â Â Â rfkill_destroy(wwan_rfkill);
> +
> + Â Â Â pr_info("Driver unloaded\n");
> +}
> +
> +module_init(oaktrail_init);
> +module_exit(oaktrail_cleanup);
> +
> +MODULE_AUTHOR("Yin Kangkai (kangkai.yin@xxxxxxxxx)");
> +MODULE_DESCRIPTION("Intel Oaktrail Platform ACPI Extras");
> +MODULE_VERSION(DRIVER_VERSION);
> +MODULE_LICENSE("GPL");
Maybe you could add some MODULE_ALIAS("dmi:xxxxx") lines
to enable module autoloading ?
Thanks,
--
Corentin Chary
http://xf.iksaif.net
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/