All of lore.kernel.org
 help / color / mirror / Atom feed
* RE: [PATCH 8/9] HID-Sensors: Added Compass/Magnetometer 3D
       [not found]     ` <501F973E.4030008@kernel.org>
@ 2012-08-06 15:40       ` Pandruvada, Srinivas
  2012-08-06 16:02         ` Jonathan Cameron
  0 siblings, 1 reply; 7+ messages in thread
From: Pandruvada, Srinivas @ 2012-08-06 15:40 UTC (permalink / raw)
  To: Jonathan Cameron; +Cc: linux-iio, Lars-Peter Clausen, Hennerich, Michael

Hi Jonathan,

Thanks for the review.
These drivers implements the main elements which I can test in current hubs=
. There are some more elements, which can be added in future.
For example Orientation sensor has many fields like Quattrocchi rotation ma=
trix, which is defined in specification, but I didn't implement as I can't =
test.
So keeping in separate folder, has advantage for extending capability of dr=
iver cleanly without much patch work to prevent regression on other drivers=
.

Our internal review, also preferred separate driver as you suggested before=
.
What do you think of letting this structure in with this structure (of cour=
se after your other suggested changes), and later improve based on feedback=
.

Thanks,
Srinivas








-----Original Message-----
From: Jonathan Cameron [mailto:jic23@kernel.org]=20
Sent: Monday, August 06, 2012 3:07 AM
To: Pandruvada, Srinivas
Cc: linux-iio; Lars-Peter Clausen; Hennerich, Michael
Subject: Re: [PATCH 8/9] HID-Sensors: Added Compass/Magnetometer 3D

On 06/08/2012 11:00, Jonathan Cameron wrote:
> On 02/08/2012 05:28, srinivas pandruvada wrote:
>> Added usage id processing for Compass 3D. This uses IIO interfaces=20
>> for triggerred buffer to present data to user mode.This uses HID=20
>> sensor framework for registering callback events from the sensor hub.
>
> Based on a quick look this can also be trivially handled by a combined=20
> accel/gyro/magnetometer driver.

Thinking a bit more about this, I'm guessing the reason you have not done t=
his so far is that I already told you to put the components into subdirecto=
ries. Looking at this I would say we need to perhaps be a little more flexi=
ble about that rule - either that or move the majority of this into your co=
mmon/hid-sensors directory and have trivial drivers for each of the compone=
nts so that we can have them in the accel/gyro/magnetometer directories. Or=
 we make an arbitary call and as with device naming (where the first part s=
upported gives the driver it's name) and arbitarily put the combined driver=
 into accel.

What do people think? (cc'd analog devices guys as they are probably most l=
ikely of current contributors to have similar situations in the future).

Sorry for sending you down the route of doing the separate drivers in the f=
irst place. I clearly wasn't thinking this through....

>
>
>>
>> Signed-off-by: srinivas pandruvada <srinivas.pandruvada@intel.com>
>> ---
>>   drivers/iio/Kconfig                           |    1 +
>>   drivers/iio/Makefile                          |    1 +
>>   drivers/iio/magnetometer/Kconfig              |   16 +
>>   drivers/iio/magnetometer/Makefile             |    6 +
>>   drivers/iio/magnetometer/hid-sensor-magn-3d.c |  419
>> +++++++++++++++++++++++++
>>   5 files changed, 443 insertions(+), 0 deletions(-)
>>   create mode 100644 drivers/iio/magnetometer/Kconfig
>>   create mode 100644 drivers/iio/magnetometer/Makefile
>>   create mode 100644 drivers/iio/magnetometer/hid-sensor-magn-3d.c
>>
>> diff --git a/drivers/iio/Kconfig b/drivers/iio/Kconfig index=20
>> 20212af..cdb3cfb 100644
>> --- a/drivers/iio/Kconfig
>> +++ b/drivers/iio/Kconfig
>> @@ -59,5 +59,6 @@ source "drivers/iio/adc/Kconfig"
>>   source "drivers/iio/amplifiers/Kconfig"
>>   source "drivers/iio/common/Kconfig"
>>   source "drivers/iio/gyro/Kconfig"
>> +source "drivers/iio/magnetometer/Kconfig"
>>
>>   endif # IIO
>> diff --git a/drivers/iio/Makefile b/drivers/iio/Makefile index=20
>> 7660bc4..71b643a 100644
>> --- a/drivers/iio/Makefile
>> +++ b/drivers/iio/Makefile
>> @@ -15,3 +15,4 @@ obj-y +=3D adc/
>>   obj-y +=3D amplifiers/
>>   obj-y +=3D common/
>>   obj-y +=3D gyro/
>> +obj-y +=3D magnetometer/
>> diff --git a/drivers/iio/magnetometer/Kconfig
>> b/drivers/iio/magnetometer/Kconfig
>> new file mode 100644
>> index 0000000..c1f0cdd
>> --- /dev/null
>> +++ b/drivers/iio/magnetometer/Kconfig
>> @@ -0,0 +1,16 @@
>> +#
>> +# Magnetometer sensors
>> +#
>> +menu "Magnetometer sensors"
>> +
>> +config HID_SENSOR_MAGNETOMETER_3D
>> +    depends on HID_SENSOR_HUB
>> +    select IIO_BUFFER
>> +    select IIO_TRIGGERED_BUFFER
>> +    select HID_SENSOR_IIO_COMMON
>> +    tristate "HID Magenetometer 3D"
>> +    help
>> +      Say yes here to build support for the HID SENSOR
>> +      Magnetometer 3D.
>> +
>> +endmenu
>> diff --git a/drivers/iio/magnetometer/Makefile
>> b/drivers/iio/magnetometer/Makefile
>> new file mode 100644
>> index 0000000..cb26b44
>> --- /dev/null
>> +++ b/drivers/iio/magnetometer/Makefile
>> @@ -0,0 +1,6 @@
>> +#
>> +# Makefile for industrial I/O Magnetometer sensor drivers #
>> +
>> +hid-sensor-magn-3d-drv-y :=3D hid-sensor-magn-3d.o
>> +obj-$(CONFIG_HID_SENSOR_MAGNETOMETER_3D) +=3D hid-sensor-magn-3d-drv.o
>> diff --git a/drivers/iio/magnetometer/hid-sensor-magn-3d.c
>> b/drivers/iio/magnetometer/hid-sensor-magn-3d.c
>> new file mode 100644
>> index 0000000..b34682d
>> --- /dev/null
>> +++ b/drivers/iio/magnetometer/hid-sensor-magn-3d.c
>> @@ -0,0 +1,419 @@
>> +/*
>> + * HID Sensors Driver
>> + * Copyright (c) 2012, Intel Corporation.
>> + *
>> + * This program is free software; you can redistribute it and/or
>> modify it
>> + * under the terms and conditions of the GNU General Public License,
>> + * version 2, as published by the Free Software Foundation.
>> + *
>> + * This program is distributed in the hope it will be useful, but
>> WITHOUT
>> + * ANY WARRANTY; without even the implied warranty of=20
>> + 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,=20
>> +Inc.,
>> + * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
>> + *
>> + */
>> +#include <linux/device.h>
>> +#include <linux/platform_device.h>
>> +#include <linux/module.h>
>> +#include <linux/interrupt.h>
>> +#include <linux/irq.h>
>> +#include <linux/slab.h>
>> +#include <linux/hid-sensor-hub.h>
>> +#include <linux/iio/iio.h>
>> +#include <linux/iio/sysfs.h>
>> +#include <linux/iio/buffer.h>
>> +#include <linux/iio/trigger_consumer.h> #include=20
>> +<linux/iio/triggered_buffer.h> #include=20
>> +"../common/hid-sensors/hid-sensor-attributes.h"
>> +#include "../common/hid-sensors/hid-sensor-trigger.h"
>> +
>> +/*Format: HID-SENSOR-usage_id_in_hex*/ #define DRIVER_NAME=20
>> +"HID-SENSOR-200083"
>> +
>> +enum magn_3d_channel {
>> +    CHANNEL_SCAN_INDEX_X,
>> +    CHANNEL_SCAN_INDEX_Y,
>> +    CHANNEL_SCAN_INDEX_Z,
>> +    MAGN_3D_CHANNEL_MAX,
>> +};
>> +
>> +struct magn_3d_state {
>> +    struct hid_sensor_iio_common common_attributes;
>> +    struct hid_sensor_hub_attribute_info magn[MAGN_3D_CHANNEL_MAX];
>> +    u32 magn_val[MAGN_3D_CHANNEL_MAX]; };
>> +
>> +const u32 magn_3d_addresses[MAGN_3D_CHANNEL_MAX] =3D {
>> +    HID_USAGE_SENSOR_ORIENT_MAGN_FLUX_X_AXIS,
>> +    HID_USAGE_SENSOR_ORIENT_MAGN_FLUX_Y_AXIS,
>> +    HID_USAGE_SENSOR_ORIENT_MAGN_FLUX_Z_AXIS
>> +};
>> +
>> +/* Channel definitions */
>> +static struct iio_chan_spec magn_3d_channels[] =3D {
>> +    {
>> +        .type =3D IIO_MAGN,
>> +        .modified =3D 1,
>> +        .channel2 =3D IIO_MOD_X,
>> +        .info_mask =3D IIO_CHAN_INFO_OFFSET_SHARED_BIT |
>> +        IIO_CHAN_INFO_SCALE_SHARED_BIT |
>> +        IIO_CHAN_INFO_SAMP_FREQ_SHARED_BIT |
>> +        IIO_CHAN_INFO_HYSTERESIS_SHARED_BIT,
>> +        .scan_index =3D CHANNEL_SCAN_INDEX_X,
>> +    }, {
>> +        .type =3D IIO_MAGN,
>> +        .modified =3D 1,
>> +        .channel2 =3D IIO_MOD_Y,
>> +        .info_mask =3D IIO_CHAN_INFO_OFFSET_SHARED_BIT |
>> +        IIO_CHAN_INFO_SCALE_SHARED_BIT |
>> +        IIO_CHAN_INFO_SAMP_FREQ_SHARED_BIT |
>> +        IIO_CHAN_INFO_HYSTERESIS_SHARED_BIT,
>> +        .scan_index =3D CHANNEL_SCAN_INDEX_Y,
>> +    }, {
>> +        .type =3D IIO_MAGN,
>> +        .modified =3D 1,
>> +        .channel2 =3D IIO_MOD_Z,
>> +        .info_mask =3D IIO_CHAN_INFO_OFFSET_SHARED_BIT |
>> +        IIO_CHAN_INFO_SCALE_SHARED_BIT |
>> +        IIO_CHAN_INFO_SAMP_FREQ_SHARED_BIT |
>> +        IIO_CHAN_INFO_HYSTERESIS_SHARED_BIT,
>> +        .scan_index =3D CHANNEL_SCAN_INDEX_Z,
>> +    }
>> +};
>> +
>> +/* Adjust channel real bits based on report descriptor */ static=20
>> +void magn_3d_adjust_channel_bit_mask(int channel, int size) {
>> +    magn_3d_channels[channel].scan_type.sign =3D 's';
>> +    /* Real storage bits will change based on the report desc. */
>> +    magn_3d_channels[channel].scan_type.realbits =3D size * 8;
>> +    /* Maximum size of a sample to capture is u32 */
>> +    magn_3d_channels[channel].scan_type.storagebits =3D sizeof(u32) *=20
>> +8; }
>> +
>> +/* Channel read_raw handler */
>> +static int magn_3d_read_raw(struct iio_dev *indio_dev,
>> +                  struct iio_chan_spec const *chan,
>> +                  int *val, int *val2,
>> +                  long mask)
>> +{
>> +    struct magn_3d_state *magn_state =3D iio_priv(indio_dev);
>> +    int report_id =3D -1;
>> +    u32 address;
>> +    int ret;
>> +
>> +    *val =3D 0;
>> +    *val2 =3D 0;
>> +    switch (mask) {
>> +    case 0:
>> +        report_id =3D
>> +            magn_state->magn[chan->scan_index].report_id;
>> +        address =3D magn_3d_addresses[chan->scan_index];
>> +        if (report_id >=3D 0)
>> +            *val =3D sensor_hub_input_attr_get_raw_value(
>> +                magn_state->common_attributes.hsdev,
>> +                HID_USAGE_SENSOR_COMPASS_3D, address,
>> +                report_id);
>> +        else
>> +            *val =3D 0;
>> +        break;
>> +    case IIO_CHAN_INFO_SCALE:
>> +        *val =3D magn_state->magn[CHANNEL_SCAN_INDEX_X].units;
>> +        break;
>> +    case IIO_CHAN_INFO_OFFSET:
>> +        *val =3D hid_sensor_convert_exponent(
>> +            magn_state->magn[CHANNEL_SCAN_INDEX_X].unit_expo);
>> +        break;
>> +    case IIO_CHAN_INFO_SAMP_FREQ:
>> +        ret =3D hid_sensor_read_samp_freq_value(
>> +            &magn_state->common_attributes, val, val2);
>> +        break;
>> +    case IIO_CHAN_INFO_HYSTERESIS:
>> +        ret =3D hid_sensor_read_raw_hyst_value(
>> +            &magn_state->common_attributes, val, val2);
>> +        break;
>> +    default:
>> +        break;
>> +    }
>> +
>> +    if (*val2)
>> +        return IIO_VAL_INT_PLUS_MICRO;
>> +    else
>> +        return IIO_VAL_INT;
>> +}
>> +
>> +/* Channel write_raw handler */
>> +static int magn_3d_write_raw(struct iio_dev *indio_dev,
>> +                   struct iio_chan_spec const *chan,
>> +                   int val,
>> +                   int val2,
>> +                   long mask)
>> +{
>> +    struct magn_3d_state *magn_state =3D iio_priv(indio_dev);
>> +    int ret =3D 0;
>> +
>> +    switch (mask) {
>> +    case IIO_CHAN_INFO_SAMP_FREQ:
>> +        ret =3D hid_sensor_write_samp_freq_value(
>> +                &magn_state->common_attributes, val, val2);
>> +        break;
>> +    case IIO_CHAN_INFO_HYSTERESIS:
>> +        ret =3D hid_sensor_write_raw_hyst_value(
>> +                &magn_state->common_attributes, val, val2);
>> +        break;
>> +    default:
>> +        ret =3D -EINVAL;
>> +    }
>> +    return ret;
>> +}
>> +
>> +static int magn_3d_write_raw_get_fmt(struct iio_dev *indio_dev,
>> +                   struct iio_chan_spec const *chan,
>> +                   long mask)
>> +{
>> +    return IIO_VAL_INT_PLUS_MICRO;
>> +}
>> +
>> +static const struct iio_info magn_3d_info =3D {
>> +    .driver_module =3D THIS_MODULE,
>> +    .read_raw =3D &magn_3d_read_raw,
>> +    .write_raw =3D &magn_3d_write_raw,
>> +    .write_raw_get_fmt =3D &magn_3d_write_raw_get_fmt, };
>> +
>> +/* Function to push data to buffer */ void=20
>> +hid_sensor_push_data(struct iio_dev *indio_dev, u8 *data, int len) {
>> +    struct iio_buffer *buffer =3D indio_dev->buffer;
>> +    s64 timestamp =3D iio_get_time_ns();
>> +    int datum_sz;
>> +
>> +    dev_dbg(&indio_dev->dev, "hid_sensor_push_data\n");
>> +    if (!buffer) {
>> +        dev_err(&indio_dev->dev, "Buffer =3D=3D NULL\n");
>> +        return;
>> +    }
>> +    datum_sz =3D buffer->access->get_bytes_per_datum(buffer);
>> +    if (len > datum_sz) {
>> +        dev_err(&indio_dev->dev, "Datum size mismatch %d:%d\n", len,
>> +                datum_sz);
>> +        return;
>> +    }
>> +    buffer->access->store_to(buffer, (u8 *)data, timestamp); }
>> +
>> +/* Callback handler to send event after all samples are received and
>> captured */
>> +int magn_3d_proc_event(struct hid_sensor_hub_device *hsdev, unsigned
>> usage_id,
>> +            void *priv)
>> +{
>> +    struct iio_dev *indio_dev =3D platform_get_drvdata(priv);
>> +    struct magn_3d_state *magn_state =3D iio_priv(indio_dev);
>> +
>> +    dev_dbg(&indio_dev->dev, "magn_3d_proc_event [%d]\n",
>> +                magn_state->common_attributes.data_ready);
>> +    if (magn_state->common_attributes.data_ready)
>> +        hid_sensor_push_data(indio_dev,
>> +                (u8 *)magn_state->magn_val,
>> +                sizeof(magn_state->magn_val));
>> +
>> +    return 0;
>> +}
>> +
>> +/* Capture samples in local storage */ int=20
>> +magn_3d_capture_sample(struct hid_sensor_hub_device *hsdev,
>> +                unsigned usage_id,
>> +                size_t raw_len, char *raw_data,
>> +                void *priv)
>> +{
>> +    struct iio_dev *indio_dev =3D platform_get_drvdata(priv);
>> +    struct magn_3d_state *magn_state =3D iio_priv(indio_dev);
>> +    int offset;
>> +    int ret =3D -EINVAL;
>> +
>> +    switch (usage_id) {
>> +    case HID_USAGE_SENSOR_ORIENT_MAGN_FLUX_X_AXIS:
>> +    case HID_USAGE_SENSOR_ORIENT_MAGN_FLUX_Y_AXIS:
>> +    case HID_USAGE_SENSOR_ORIENT_MAGN_FLUX_Z_AXIS:
>> +        offset =3D usage_id - HID_USAGE_SENSOR_ORIENT_MAGN_FLUX_X_AXIS;
>> +        magn_state->magn_val[CHANNEL_SCAN_INDEX_X + offset] =3D
>> +                        *(u32 *)raw_data;
>> +        ret =3D 0;
>> +    break;
>> +    default:
>> +        break;
>> +    }
>> +
>> +    return ret;
>> +}
>> +
>> +/* Parse report which is specific to an usage id*/ static int=20
>> +magn_3d_parse_report(struct platform_device *pdev,
>> +                struct hid_sensor_hub_device *hsdev,
>> +                unsigned usage_id,
>> +                struct magn_3d_state *st) {
>> +    int ret;
>> +    int i;
>> +
>> +    for (i =3D 0; i <=3D CHANNEL_SCAN_INDEX_Z; ++i) {
>> +        ret =3D sensor_hub_input_get_attribute_info(hsdev,
>> HID_INPUT_REPORT,
>> +                usage_id,
>> +                HID_USAGE_SENSOR_ORIENT_MAGN_FLUX_X_AXIS + i,
>> +                &st->magn[CHANNEL_SCAN_INDEX_X + i]);
>> +        magn_3d_adjust_channel_bit_mask(CHANNEL_SCAN_INDEX_X + i,
>> +                st->magn[CHANNEL_SCAN_INDEX_X + i].size);
>> +        if (ret < 0)
>> +            break;
>> +    }
>> +    dev_dbg(&pdev->dev, "magn_3d %x:%x, %x:%x, %x:%x\n",
>> +            st->magn[0].index,
>> +            st->magn[0].report_id,
>> +            st->magn[1].index, st->magn[1].report_id,
>> +            st->magn[2].index, st->magn[2].report_id);
>> +
>> +    return ret;
>> +}
>> +
>> +/* Function to initialize the processing for usage id */ static=20
>> +inline int magn_3d_init(struct platform_device *pdev,
>> +            struct hid_sensor_hub_device *hsdev,
>> +            unsigned usage_id)
>> +{
>> +    int ret =3D 0;
>> +    static char *name =3D "magn_3d";
>> +    struct iio_dev *indio_dev;
>> +    struct magn_3d_state *magn_state;
>> +
>> +    indio_dev =3D iio_device_alloc(sizeof(struct magn_3d_state));
>> +    if (indio_dev =3D=3D NULL) {
>> +        ret =3D -ENOMEM;
>> +        goto error_ret;
>> +    }
>> +    platform_set_drvdata(pdev, indio_dev);
>> +
>> +    magn_state =3D iio_priv(indio_dev);
>> +    magn_state->common_attributes.hsdev =3D hsdev;
>> +    magn_state->common_attributes.pdev =3D pdev;
>> +
>> +    ret =3D hid_sensor_parse_common_attributes(hsdev,
>> +                usage_id, &magn_state->common_attributes);
>> +    if (ret) {
>> +        dev_err(&pdev->dev, "failed to setup common attributes\n");
>> +        goto error_free_dev;
>> +    }
>> +
>> +    ret =3D magn_3d_parse_report(pdev, hsdev, usage_id, magn_state);
>> +    if (ret) {
>> +        dev_err(&pdev->dev, "failed to setup attributes\n");
>> +        goto error_free_dev;
>> +    }
>> +
>> +    indio_dev->channels =3D kmemdup(magn_3d_channels,
>> +                    sizeof(magn_3d_channels),
>> +                    GFP_KERNEL);
>> +    if (!indio_dev->channels) {
>> +        dev_err(&pdev->dev, "failed to duplicate channels\n");
>> +        goto error_free_dev;
>> +    }
>> +
>> +    indio_dev->num_channels =3D
>> +                ARRAY_SIZE(magn_3d_channels);
>> +    indio_dev->dev.parent =3D &pdev->dev;
>> +    indio_dev->info =3D &magn_3d_info;
>> +    indio_dev->name =3D name;
>> +    indio_dev->modes =3D INDIO_DIRECT_MODE;
>> +
>> +    ret =3D iio_triggered_buffer_setup(indio_dev,
>> &iio_pollfunc_store_time,
>> +        NULL, NULL);
>> +    if (ret) {
>> +        dev_err(&pdev->dev, "failed to initialize trigger buffer\n");
>> +        goto error_free_dev_mem;
>> +    }
>> +    magn_state->common_attributes.data_ready =3D false;
>> +    ret =3D hid_sensor_setup_trigger(indio_dev, name,
>> +                    &magn_state->common_attributes);
>> +    if (ret < 0) {
>> +        dev_err(&pdev->dev, "trigger setup failed\n");
>> +        goto error_unreg_buffer_funcs;
>> +    }
>> +
>> +    ret =3D iio_device_register(indio_dev);
>> +    if (ret) {
>> +        dev_err(&pdev->dev, "device register failed\n");
>> +        goto error_remove_trigger;
>> +    }
>> +
>> +    return ret;
>> +
>> +error_remove_trigger:
>> +    hid_sensor_remove_trigger(indio_dev);
>> +error_unreg_buffer_funcs:
>> +    iio_triggered_buffer_cleanup(indio_dev);
>> +error_free_dev_mem:
>> +    kfree(indio_dev->channels);
>> +error_free_dev:
>> +    iio_device_free(indio_dev);
>> +error_ret:
>> +    return ret;
>> +}
>> +
>> +/* Function to deinitialize the processing for usage id */ static=20
>> +inline void magn_3d_exit(struct platform_device *pdev) {
>> +    struct iio_dev *indio_dev =3D platform_get_drvdata(pdev);
>> +
>> +    iio_device_unregister(indio_dev);
>> +    hid_sensor_remove_trigger(indio_dev);
>> +    iio_triggered_buffer_cleanup(indio_dev);
>> +    kfree(indio_dev->channels);
>> +    iio_device_free(indio_dev);
>> +}
>> +
>> +static struct hid_sensor_hub_callbacks magn_3d_callbacks =3D {
>> +    .send_event =3D magn_3d_proc_event,
>> +    .capture_sample =3D magn_3d_capture_sample, };
>> +
>> +static int __devinit hid_magn_3d_probe(struct platform_device *pdev)=20
>> +{
>> +    int ret;
>> +    struct hid_sensor_hub_device *hsdev =3D pdev->dev.platform_data;
>> +
>> +    ret =3D magn_3d_init(pdev, hsdev, HID_USAGE_SENSOR_COMPASS_3D);
>> +    if (ret < 0) {
>> +        dev_err(&pdev->dev, "magn_3d_init failed\n");
>> +        return ret;
>> +    }
>> +    magn_3d_callbacks.pdev =3D pdev;
>> +    ret =3D sensor_hub_register_callback(hsdev,
>> HID_USAGE_SENSOR_COMPASS_3D,
>> +                    &magn_3d_callbacks);
>> +
>> +    return ret;
>> +}
>> +
>> +static int __devinit hid_magn_3d_remove(struct platform_device=20
>> +*pdev) {
>> +    struct hid_sensor_hub_device *hsdev =3D pdev->dev.platform_data;
>> +
>> +    magn_3d_exit(pdev);
>> +
>> +    return sensor_hub_remove_callback(hsdev,
>> HID_USAGE_SENSOR_COMPASS_3D);
>> +}
>> +
>> +static struct platform_driver hid_magn_3d_platform_driver =3D {
>> +    .driver =3D {
>> +        .name    =3D DRIVER_NAME,
>> +        .owner    =3D THIS_MODULE,
>> +    },
>> +    .probe        =3D hid_magn_3d_probe,
>> +    .remove        =3D hid_magn_3d_remove,
>> +};
>> +module_platform_driver(hid_magn_3d_platform_driver);
>> +
>> +MODULE_DESCRIPTION("HID Sensor Magnetometer 3D");=20
>> +MODULE_AUTHOR("Srinivas Pandruvada=20
>> +<srinivas.pandruvada@intel.com>");
>> +MODULE_LICENSE("GPL");
>>
>

^ permalink raw reply	[flat|nested] 7+ messages in thread

* RE: [PATCH 4/9] HID-Sensors: Sensor framework
       [not found]   ` <501F9DD1.1000606@kernel.org>
@ 2012-08-06 15:43     ` Pandruvada, Srinivas
  0 siblings, 0 replies; 7+ messages in thread
From: Pandruvada, Srinivas @ 2012-08-06 15:43 UTC (permalink / raw)
  To: Jonathan Cameron; +Cc: linux-iio, Jiri Kosina

It is Jiri Kosina's review list. But he wanted first look from IIO folks.

Thanks,
Srinivas

-----Original Message-----
From: Jonathan Cameron [mailto:jic23@kernel.org]=20
Sent: Monday, August 06, 2012 3:35 AM
To: Pandruvada, Srinivas
Cc: linux-iio
Subject: Re: [PATCH 4/9] HID-Sensors: Sensor framework

On 02/08/2012 05:28, srinivas pandruvada wrote:
> Adding processing for HID Sensor usage table as defined by HID 1.12,=20
> Request #: HUTRR39, dated 05 May, 2011.
> This driver uses HID driver framework to register, send and receive=20
> events.
> This uses MFD framework, so that actual processing for a specific=20
> usage id can be done in a different driver. For example an=20
> accelerometer driver can be a separate driver and use the interface=20
> provided by this driver to register for events.

This needs positive responses form the hid (and possibly mfd) side of thing=
s.

As ever my work email client has messed with the white space. grr.

Anyhow, based on a fairly superficial look this is fine as far as I can see=
.

Time to actually get back to the day job ;)


>
> Signed-off-by: srinivas pandruvada <srinivas.pandruvada@intel.com>
Acked-by: Jonathan Cameron <jic23@kernel.org>
> ---
>   drivers/hid/Kconfig            |   14 +
>   drivers/hid/Makefile           |    1 +
>   drivers/hid/hid-sensor-hub.c   |  681 +++++++++++++++++++++++++++++++++=
+++++++
>   include/linux/hid-sensor-hub.h |  160 ++++++++++
>   include/linux/hid-sensor-ids.h |  111 +++++++
>   5 files changed, 967 insertions(+), 0 deletions(-)
>   create mode 100644 drivers/hid/hid-sensor-hub.c
>   create mode 100644 include/linux/hid-sensor-hub.h
>   create mode 100644 include/linux/hid-sensor-ids.h
>
> diff --git a/drivers/hid/Kconfig b/drivers/hid/Kconfig index=20
> bef04c1..a9fd774 100644
> --- a/drivers/hid/Kconfig
> +++ b/drivers/hid/Kconfig
> @@ -653,6 +653,20 @@ config HID_ZYDACRON
>   	---help---
>   	Support for Zydacron remote control.
>
> +config HID_SENSOR_HUB
> +	tristate "HID Sensors framework support"
> +	depends on USB_HID
> +	select MFD_CORE
> +	default n
> +	-- help---
> +	  Support for HID Sensor framework. This creates a MFD instance
> +	  for a sensor hub and identifies all the sensors connected to it.
> +	  Each sensor is registered as a MFD cell, so that sensor specific
> +	  processing can be done in a separate driver. Each sensor
> +	  drivers can use the service provided by this driver to register
> +	  for events and handle data streams. Each sensor driver can format
> +	  data and present to user mode using input or IIO interface.
> +
>   endmenu
>
>   endif # HID
> diff --git a/drivers/hid/Makefile b/drivers/hid/Makefile index=20
> ca6cc9f..08f9d8f 100644
> --- a/drivers/hid/Makefile
> +++ b/drivers/hid/Makefile
> @@ -87,6 +87,7 @@ obj-$(CONFIG_HID_ZYDACRON)	+=3D hid-zydacron.o
>   obj-$(CONFIG_HID_WACOM)		+=3D hid-wacom.o
>   obj-$(CONFIG_HID_WALTOP)	+=3D hid-waltop.o
>   obj-$(CONFIG_HID_WIIMOTE)	+=3D hid-wiimote.o
> +obj-$(CONFIG_HID_SENSOR_HUB)	+=3D hid-sensor-hub.o
>
>   obj-$(CONFIG_USB_HID)		+=3D usbhid/
>   obj-$(CONFIG_USB_MOUSE)		+=3D usbhid/
> diff --git a/drivers/hid/hid-sensor-hub.c=20
> b/drivers/hid/hid-sensor-hub.c new file mode 100644 index=20
> 0000000..6c9f0f8
> --- /dev/null
> +++ b/drivers/hid/hid-sensor-hub.c
> @@ -0,0 +1,681 @@
> +/*
> + * HID Sensors Driver
> + * Copyright (c) 2012, Intel Corporation.
> + *
> + * This program is free software; you can redistribute it and/or=20
> +modify it
> + * under the terms and conditions of the GNU General Public License,
> + * version 2, as published by the Free Software Foundation.
> + *
> + * This program is distributed in the hope it will be useful, but=20
> +WITHOUT
> + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY=20
> +or
> + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public=20
> +License for
> + * more details.
> + *
> + * You should have received a copy of the GNU General Public License=20
> +along with
> + * this program; if not, write to the Free Software Foundation, Inc.,
> + * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
> + *
> + */
> +#include <linux/device.h>
> +#include <linux/hid.h>
> +#include <linux/usb.h>
> +#include "usbhid/usbhid.h"
> +#include <linux/module.h>
> +#include <linux/slab.h>
> +#include <linux/mfd/core.h>
> +#include <linux/list.h>
> +#include <linux/hid-sensor-ids.h>
> +#include <linux/hid-sensor-hub.h>
> +#include "hid-ids.h"
> +
> +/**
> + * struct sensor_hub_pending - Synchronous read pending information
> + * @status:		Pending status true/false.
> + * @ready:		Completion synchronization data.
> + * @usage_id:		Usage id for physical device, E.g. Gyro usage id.
> + * @attr_usage_id:	Usage Id of a field, E.g. X-AXIS for a gyro.
> + * @raw_size:		Response size for a read request.
> + * @raw_data:		Place holder for received response.
> + */
> +struct sensor_hub_pending {
> +	bool status;
> +	struct completion ready;
> +	u32 usage_id;
> +	u32 attr_usage_id;
> +	int raw_size;
> +	u8  *raw_data;
> +};
> +
> +/**
> + * struct sensor_hub_data - Hold a instance data for a HID hub device
> + * @hs_dev:		Stored hid instance for current hub device.
> + * @mutex:		Mutex to serialize synchronous request.
> + * @lock:		Spin lock to protect pending request structure.
> + * @pending:		Holds information of pending sync read request.
> + * @hid_sensor_hub_client_devs:	Stores all MFD cells for a hub instance.
> + * @hid_sensor_client_cnt: Number of MFD cells, (no of sensors attached)=
.
> + */
> +struct sensor_hub_data {
> +	struct hid_sensor_hub_device *hsdev;
> +	struct mutex mutex;
> +	spinlock_t lock;
> +	struct sensor_hub_pending pending;
> +	struct list_head dyn_callback_list;
> +	spinlock_t dyn_lock;
> +	struct mfd_cell *hid_sensor_hub_client_devs;
> +	int hid_sensor_client_cnt;
> +};
> +
> +/**
> + * hid_sensor_hub_callbacks_list - Stores callback list
> + * @list:		list head.
> + * @usage_id:		usage id for a physical device.
> + * @usage_callbacks:	Stores registered callback functions.
> + * @priv:		Private data for a physical device.
> + */
> +struct hid_sensor_hub_callbacks_list {
> +	struct list_head list;
> +	u32 usage_id;
> +	struct hid_sensor_hub_callbacks *usage_callback;
> +	void *priv;
> +};
> +
> +static int sensor_hub_check_for_sensor_page(struct hid_device *hdev)=20
> +{
> +	int i;
> +	int ret =3D -EINVAL;
> +
> +	for (i =3D 0; i < hdev->maxcollection; i++) {
> +		struct hid_collection *col =3D &hdev->collection[i];
> +		if (col->type =3D=3D HID_COLLECTION_PHYSICAL &&
> +		   (col->usage & HID_USAGE_PAGE) =3D=3D HID_UP_SENSOR) {
> +			ret =3D 0;
> +			break;
> +		}
> +	}
> +
> +	return ret;
> +}
> +
> +static struct hid_report *sensor_hub_report(int id, struct hid_device *h=
dev,
> +						int dir)
> +{
> +	struct hid_report *report;
> +
> +	list_for_each_entry(report, &hdev->report_enum[dir].report_list, list) =
{
> +		if (report->id =3D=3D id)
> +			return report;
> +	}
> +	hid_warn(hdev, "No report with id 0x%x found\n", id);
> +
> +	return NULL;
> +}
> +
> +static int sensor_hub_get_physical_device_count(
> +				struct hid_report_enum *report_enum) {
> +	struct hid_report *report;
> +	struct hid_field *field;
> +	int cnt =3D 0;
> +
> +	list_for_each_entry(report, &report_enum->report_list, list) {
> +		field =3D report->field[0];
> +		if (report->maxfield && field &&
> +					field->physical)
> +			cnt++;
> +	}
> +
> +	return cnt;
> +}
> +
> +static struct hid_sensor_hub_callbacks *sensor_hub_get_callback(
> +					struct hid_device *hdev,
> +					u32 usage_id, void **priv)
> +{
> +	struct hid_sensor_hub_callbacks_list *callback;
> +	struct sensor_hub_data *pdata =3D hid_get_drvdata(hdev);
> +
> +	spin_lock(&pdata->dyn_lock);
> +	list_for_each_entry(callback, &pdata->dyn_callback_list, list)
> +		if (callback->usage_id =3D=3D usage_id) {
> +			*priv =3D callback->priv;
> +			spin_unlock(&pdata->dyn_lock);
> +			return callback->usage_callback;
> +		}
> +	spin_unlock(&pdata->dyn_lock);
> +
> +	return NULL;
> +}
> +
> +int sensor_hub_register_callback(struct hid_sensor_hub_device *hsdev,
> +			u32 usage_id,
> +			struct hid_sensor_hub_callbacks *usage_callback) {
> +	struct hid_sensor_hub_callbacks_list *callback;
> +	struct sensor_hub_data *pdata =3D hid_get_drvdata(hsdev->hdev);
> +
> +	spin_lock(&pdata->dyn_lock);
> +	list_for_each_entry(callback, &pdata->dyn_callback_list, list)
> +		if (callback->usage_id =3D=3D usage_id) {
> +			spin_unlock(&pdata->dyn_lock);
> +			return -EINVAL;
> +		}
> +	callback =3D kzalloc(sizeof(*callback), GFP_KERNEL);
> +	if (!callback) {
> +		spin_unlock(&pdata->dyn_lock);
> +		return -ENOMEM;
> +	}
> +	callback->usage_callback =3D usage_callback;
> +	callback->usage_id =3D usage_id;
> +	callback->priv =3D NULL;
> +	list_add_tail(&callback->list, &pdata->dyn_callback_list);
> +	spin_unlock(&pdata->dyn_lock);
> +
> +	return 0;
> +}
> +EXPORT_SYMBOL_GPL(sensor_hub_register_callback);
> +
> +int sensor_hub_remove_callback(struct hid_sensor_hub_device *hsdev,
> +				u32 usage_id)
> +{
> +	struct hid_sensor_hub_callbacks_list *callback;
> +	struct sensor_hub_data *pdata =3D hid_get_drvdata(hsdev->hdev);
> +
> +	spin_lock(&pdata->dyn_lock);
> +	list_for_each_entry(callback, &pdata->dyn_callback_list, list)
> +		if (callback->usage_id =3D=3D usage_id) {
> +			list_del(&callback->list);
> +			kfree(callback);
> +			break;
> +		}
> +	spin_unlock(&pdata->dyn_lock);
> +
> +	return 0;
> +}
> +EXPORT_SYMBOL_GPL(sensor_hub_remove_callback);
> +
> +int sensor_hub_set_feature(struct hid_sensor_hub_device *hsdev, u32 repo=
rt_id,
> +				u32 field_index, s32 value)
> +{
> +	struct hid_report *report;
> +	struct sensor_hub_data *data =3D  hid_get_drvdata(hsdev->hdev);
> +	int ret =3D 0;
> +
> +	if (report_id < 0)
> +		return -EINVAL;
> +
> +	mutex_lock(&data->mutex);
> +	report =3D sensor_hub_report(report_id, hsdev->hdev, HID_FEATURE_REPORT=
);
> +	if (!report || (field_index >=3D  report->maxfield)) {
> +		ret =3D -EINVAL;
> +		goto done_proc;
> +	}
> +	hid_set_field(report->field[field_index], 0, value);
> +	usbhid_submit_report(hsdev->hdev, report, USB_DIR_OUT);
> +	usbhid_wait_io(hsdev->hdev);
> +
> +done_proc:
> +	mutex_unlock(&data->mutex);
> +
> +	return ret;
> +}
> +EXPORT_SYMBOL_GPL(sensor_hub_set_feature);
> +
> +int sensor_hub_get_feature(struct hid_sensor_hub_device *hsdev, u32 repo=
rt_id,
> +				u32 field_index, s32 *value)
> +{
> +	struct hid_report *report;
> +	struct sensor_hub_data *data =3D  hid_get_drvdata(hsdev->hdev);
> +	int ret =3D 0;
> +
> +	if (report_id < 0)
> +		return -EINVAL;
> +
> +	mutex_lock(&data->mutex);
> +	report =3D sensor_hub_report(report_id, hsdev->hdev, HID_FEATURE_REPORT=
);
> +	if (!report || (field_index >=3D  report->maxfield)) {
> +		ret =3D -EINVAL;
> +		goto done_proc;
> +	}
> +	usbhid_submit_report(hsdev->hdev, report, USB_DIR_IN);
> +	usbhid_wait_io(hsdev->hdev);
> +	*value =3D report->field[field_index]->value[0];
> +
> +done_proc:
> +	mutex_unlock(&data->mutex);
> +
> +	return ret;
> +}
> +EXPORT_SYMBOL_GPL(sensor_hub_get_feature);
> +
> +
> +int sensor_hub_input_attr_get_raw_value(struct hid_sensor_hub_device *hs=
dev,
> +					u32 usage_id,
> +					u32 attr_usage_id, u32 report_id) {
> +	struct sensor_hub_data *data =3D  hid_get_drvdata(hsdev->hdev);
> +	unsigned long flags;
> +	struct hid_report *report;
> +	int ret_val =3D 0;
> +
> +	if (report_id < 0)
> +		return -EINVAL;
> +
> +	mutex_lock(&data->mutex);
> +	memset(&data->pending, 0, sizeof(data->pending));
> +	init_completion(&data->pending.ready);
> +	data->pending.usage_id =3D usage_id;
> +	data->pending.attr_usage_id =3D attr_usage_id;
> +	data->pending.raw_size =3D 0;
> +
> +	spin_lock_irqsave(&data->lock, flags);
> +	data->pending.status =3D true;
> +	report =3D sensor_hub_report(report_id, hsdev->hdev, HID_INPUT_REPORT);
> +	if (!report) {
> +		spin_unlock_irqrestore(&data->lock, flags);
> +		goto err_free;
> +	}
> +	usbhid_submit_report(hsdev->hdev, report, USB_DIR_IN);
> +	spin_unlock_irqrestore(&data->lock, flags);
> +	wait_for_completion_interruptible_timeout(&data->pending.ready, HZ*5);
> +	switch (data->pending.raw_size) {
> +	case 1:
> +		ret_val =3D *(u8 *)data->pending.raw_data;
> +		break;
> +	case 2:
> +		ret_val =3D *(u16 *)data->pending.raw_data;
> +		break;
> +	case 4:
> +		ret_val =3D *(u32 *)data->pending.raw_data;
> +		break;
> +	default:
> +		ret_val =3D 0;
> +	}
> +	kfree(data->pending.raw_data);
> +
> +err_free:
> +	data->pending.status =3D false;
> +	mutex_unlock(&data->mutex);
> +
> +	return ret_val;
> +}
> +EXPORT_SYMBOL_GPL(sensor_hub_input_attr_get_raw_value);
> +
> +int sensor_hub_input_get_attribute_info(struct hid_sensor_hub_device *hs=
dev,
> +				u8 type,
> +				u32 usage_id,
> +				u32 attr_usage_id,
> +				struct hid_sensor_hub_attribute_info *info) {
> +	int ret =3D -1;
> +	int i, j;
> +	int collection_index =3D -1;
> +	struct hid_report *report;
> +	struct hid_field *field;
> +	struct hid_report_enum *report_enum;
> +	struct hid_device *hdev =3D hsdev->hdev;
> +
> +	/* Initialize with defaults */
> +	info->usage_id =3D usage_id;
> +	info->attrib_id =3D  attr_usage_id;
> +	info->report_id =3D -1;
> +	info->index =3D -1;
> +	info->units =3D -1;
> +	info->unit_expo =3D -1;
> +
> +	for (i =3D 0; i < hdev->maxcollection; ++i) {
> +		struct hid_collection *collection =3D &hdev->collection[i];
> +		if (usage_id =3D=3D collection->usage) {
> +			collection_index =3D i;
> +			break;
> +		}
> +	}
> +	if (collection_index =3D=3D -1)
> +		goto err_ret;
> +
> +	report_enum =3D &hdev->report_enum[type];
> +	list_for_each_entry(report, &report_enum->report_list, list) {
> +		for (i =3D 0; i < report->maxfield; ++i) {
> +			field =3D report->field[i];
> +			if (field->physical =3D=3D usage_id &&
> +				field->logical =3D=3D attr_usage_id) {
> +				info->index =3D i;
> +				info->report_id =3D report->id;
> +				info->units =3D field->unit;
> +				info->unit_expo =3D field->unit_exponent;
> +				info->size =3D field->report_size/8;
> +				ret =3D 0;
> +			} else {
> +				for (j =3D 0; j < field->maxusage; ++j) {
> +					if (field->usage[j].hid =3D=3D
> +					attr_usage_id &&
> +					field->usage[j].collection_index =3D=3D
> +					collection_index)  {
> +						info->index =3D i;
> +						info->report_id =3D report->id;
> +						info->units =3D field->unit;
> +						info->unit_expo =3D
> +							field->unit_exponent;
> +						info->size =3D
> +							field->report_size/8;
> +						ret =3D 0;
> +						break;
> +					}
> +				}
> +			}
> +			if (ret =3D=3D 0)
> +				break;
> +		}
> +	}
> +
> +err_ret:
> +	return ret;
> +}
> +EXPORT_SYMBOL_GPL(sensor_hub_input_get_attribute_info);
> +
> +#ifdef CONFIG_PM
> +static int sensor_hub_suspend(struct hid_device *hdev, pm_message_t=20
> +message) {
> +	struct sensor_hub_data *pdata =3D  hid_get_drvdata(hdev);
> +	struct hid_sensor_hub_callbacks_list *callback;
> +
> +	hid_dbg(hdev, " sensor_hub_suspend\n");
> +	spin_lock(&pdata->dyn_lock);
> +	list_for_each_entry(callback, &pdata->dyn_callback_list, list) {
> +		if (callback->usage_callback->suspend)
> +			callback->usage_callback->suspend(
> +					pdata->hsdev, callback->priv);
> +	}
> +	spin_unlock(&pdata->dyn_lock);
> +
> +	return 0;
> +}
> +
> +static int sensor_hub_resume(struct hid_device *hdev) {
> +	struct sensor_hub_data *pdata =3D  hid_get_drvdata(hdev);
> +	struct hid_sensor_hub_callbacks_list *callback;
> +
> +	hid_dbg(hdev, " sensor_hub_resume\n");
> +	spin_lock(&pdata->dyn_lock);
> +	list_for_each_entry(callback, &pdata->dyn_callback_list, list) {
> +		if (callback->usage_callback->resume)
> +			callback->usage_callback->resume(
> +					pdata->hsdev, callback->priv);
> +	}
> +	spin_unlock(&pdata->dyn_lock);
> +
> +	return 0;
> +}
> +
> +static int sensor_hub_reset_resume(struct hid_device *hdev) {
> +	return 0;
> +}
> +#endif
> +/*
> + * Handle raw report as sent by device  */ static int=20
> +sensor_hub_raw_event(struct hid_device *hdev,
> +		struct hid_report *report, u8 *raw_data, int size) {
> +	int i;
> +	u8 *ptr;
> +	int sz;
> +	struct sensor_hub_data *pdata =3D hid_get_drvdata(hdev);
> +	unsigned long flags;
> +	struct hid_sensor_hub_callbacks *callback =3D NULL;
> +	struct hid_collection *collection =3D NULL;
> +	void *priv =3D NULL;
> +
> +	hid_dbg(hdev, "sensor_hub_raw_event report id:0x%x size:%d type:%d\n",
> +			 report->id, size, report->type);
> +	hid_dbg(hdev, "maxfield:%d\n", report->maxfield);
> +	if (report->type !=3D HID_INPUT_REPORT)
> +		return 1;
> +
> +	ptr =3D raw_data;
> +	ptr++; /*Skip report id*/
> +
> +	if (!report)
> +		goto err_report;
> +
> +	spin_lock_irqsave(&pdata->lock, flags);
> +
> +	for (i =3D 0; i < report->maxfield; ++i) {
> +
> +		hid_dbg(hdev, "%d collection_index:%x hid:%x sz:%x\n",
> +				i, report->field[i]->usage->collection_index,
> +				report->field[i]->usage->hid,
> +				report->field[i]->report_size/8);
> +
> +		sz =3D report->field[i]->report_size/8;
> +		if (pdata->pending.status && pdata->pending.attr_usage_id =3D=3D
> +				report->field[i]->usage->hid) {
> +			hid_dbg(hdev, "data was pending ...\n");
> +			pdata->pending.raw_data =3D kmalloc(sz, GFP_KERNEL);
> +			if (pdata->pending.raw_data) {
> +				memcpy(pdata->pending.raw_data, ptr, sz);
> +				pdata->pending.raw_size  =3D sz;
> +			} else
> +				pdata->pending.raw_size =3D 0;
> +			complete(&pdata->pending.ready);
> +		}
> +		collection =3D &hdev->collection[
> +				report->field[i]->usage->collection_index];
> +		hid_dbg(hdev, "collection->usage %x\n",
> +					collection->usage);
> +		callback =3D sensor_hub_get_callback(pdata->hsdev->hdev,
> +						report->field[i]->physical,
> +							&priv);
> +		if (callback && callback->capture_sample) {
> +			if (report->field[i]->logical)
> +				callback->capture_sample(pdata->hsdev,
> +					report->field[i]->logical, sz, ptr,
> +					callback->pdev);
> +			else
> +				callback->capture_sample(pdata->hsdev,
> +					report->field[i]->usage->hid, sz, ptr,
> +					callback->pdev);
> +		}
> +		ptr +=3D sz;
> +	}
> +	if (callback && collection && callback->send_event)
> +		callback->send_event(pdata->hsdev, collection->usage,
> +				callback->pdev);
> +	spin_unlock_irqrestore(&pdata->lock, flags);
> +
> +err_report:
> +	return 1;
> +}
> +
> +static int sensor_hub_probe(struct hid_device *hdev,
> +				const struct hid_device_id *id)
> +{
> +	int ret;
> +	struct sensor_hub_data *sd;
> +	int i;
> +	char *name;
> +	struct hid_report *report;
> +	struct hid_report_enum *report_enum;
> +	struct hid_field *field;
> +	int dev_cnt;
> +
> +	sd =3D kzalloc(sizeof(struct sensor_hub_data), GFP_KERNEL);
> +	if (!sd) {
> +		hid_err(hdev, "cannot allocate Sensor data\n");
> +		return -ENOMEM;
> +	}
> +	sd->hsdev =3D kzalloc(sizeof(struct hid_sensor_hub_device), GFP_KERNEL)=
;
> +	if (!sd->hsdev) {
> +		hid_err(hdev, "cannot allocate hid_sensor_hub_device\n");
> +		ret =3D -ENOMEM;
> +		goto err_free_hub;
> +	}
> +	hid_set_drvdata(hdev, sd);
> +	sd->hsdev->hdev =3D hdev;
> +	sd->hsdev->vendor_id =3D hdev->vendor;
> +	sd->hsdev->product_id =3D hdev->product;
> +	spin_lock_init(&sd->lock);
> +	spin_lock_init(&sd->dyn_lock);
> +	mutex_init(&sd->mutex);
> +	ret =3D hid_parse(hdev);
> +	if (ret) {
> +		hid_err(hdev, "parse failed\n");
> +		goto err_free;
> +	}
> +	if (sensor_hub_check_for_sensor_page(hdev) < 0) {
> +		hid_err(hdev, "sensor page not found\n");
> +		goto err_free;
> +	}
> +	INIT_LIST_HEAD(&hdev->inputs);
> +
> +	hdev->claimed =3D HID_CLAIMED_INPUT;
> +	ret =3D hid_hw_start(hdev, 0);
> +	if (ret) {
> +		hid_err(hdev, "hw start failed\n");
> +		goto err_free;
> +	}
> +	ret =3D hid_hw_open(hdev);
> +	if (ret) {
> +		hid_err(hdev, "failed to open input interrupt pipe\n");
> +		goto err_stop_hw;
> +	}
> +
> +	INIT_LIST_HEAD(&sd->dyn_callback_list);
> +	sd->hid_sensor_client_cnt =3D 0;
> +	report_enum =3D &hdev->report_enum[HID_INPUT_REPORT];
> +
> +	dev_cnt =3D sensor_hub_get_physical_device_count(report_enum);
> +	sd->hid_sensor_hub_client_devs =3D kzalloc(dev_cnt *
> +						sizeof(struct mfd_cell),
> +						GFP_KERNEL);
> +	if (sd->hid_sensor_hub_client_devs =3D=3D NULL) {
> +		hid_err(hdev,
> +			"Failed to allocate memory for mfd cells\n");
> +			ret =3D -ENOMEM;
> +			goto err_close;
> +	}
> +	list_for_each_entry(report, &report_enum->report_list, list) {
> +		hid_dbg(hdev, "Report id:%x\n", report->id);
> +		field =3D report->field[0];
> +		if (report->maxfield && field &&
> +					field->physical) {
> +			name =3D kasprintf(GFP_KERNEL, "HID-SENSOR-%x",
> +						field->physical);
> +			if (name  =3D=3D NULL) {
> +				hid_err(hdev,
> +					"Failed MFD device name\n");
> +					ret =3D -ENOMEM;
> +					goto err_free_cells;
> +			}
> +			sd->hid_sensor_hub_client_devs[
> +				sd->hid_sensor_client_cnt].name =3D name;
> +			sd->hid_sensor_hub_client_devs[
> +				sd->hid_sensor_client_cnt].platform_data =3D
> +						sd->hsdev;
> +			sd->hid_sensor_hub_client_devs[
> +				sd->hid_sensor_client_cnt].pdata_size =3D
> +						sizeof(*sd->hsdev);
> +			hid_dbg(hdev, "Adding %s:%x\n", name,
> +					(unsigned int)sd);
> +			sd->hid_sensor_client_cnt++;
> +		}
> +	}
> +	ret =3D mfd_add_devices(&hdev->dev, 0, sd->hid_sensor_hub_client_devs,
> +		sd->hid_sensor_client_cnt, NULL, 0);
> +	if (ret < 0)
> +		goto err_free_names;
> +
> +	return ret;
> +
> +err_free_names:
> +	for (i =3D 0; i < sd->hid_sensor_client_cnt ; ++i)
> +		kfree(sd->hid_sensor_hub_client_devs[i].name);
> +err_free_cells:
> +	kfree(sd->hid_sensor_hub_client_devs);
> +err_close:
> +	hid_hw_stop(hdev);
> +	hid_hw_close(hdev);
> +err_stop_hw:
> +	hid_hw_stop(hdev);
> +err_free:
> +	kfree(sd->hsdev);
> +err_free_hub:
> +	kfree(sd);
> +
> +	return ret;
> +}
> +
> +static void sensor_hub_remove(struct hid_device *hdev) {
> +	struct sensor_hub_data *data =3D hid_get_drvdata(hdev);
> +	unsigned long flags;
> +	int i;
> +
> +	hid_dbg(hdev, " hardware removed\n");
> +	hdev->claimed &=3D ~HID_CLAIMED_INPUT;
> +	hid_hw_stop(hdev);
> +	hid_hw_close(hdev);
> +	spin_lock_irqsave(&data->lock, flags);
> +	if (data->pending.status)
> +		complete(&data->pending.ready);
> +	spin_unlock_irqrestore(&data->lock, flags);
> +	mfd_remove_devices(&hdev->dev);
> +	for (i =3D 0; i < data->hid_sensor_client_cnt ; ++i)
> +		kfree(data->hid_sensor_hub_client_devs[i].name);
> +	kfree(data->hid_sensor_hub_client_devs);
> +	hid_set_drvdata(hdev, NULL);
> +	mutex_destroy(&data->mutex);
> +	kfree(data->hsdev);
> +	kfree(data);
> +}
> +
> +static const struct hid_device_id sensor_hub_devices[] =3D {
> +	{ HID_USB_DEVICE(USB_VENDOR_ID_INTEL_8086,
> +			USB_DEVICE_ID_SENSOR_HUB_1020) },
> +	{ HID_USB_DEVICE(USB_VENDOR_ID_INTEL_8087,
> +			USB_DEVICE_ID_SENSOR_HUB_1020) },
> +	{ HID_USB_DEVICE(USB_VENDOR_ID_INTEL_8086,
> +			USB_DEVICE_ID_SENSOR_HUB_09FA) },
> +	{ HID_USB_DEVICE(USB_VENDOR_ID_INTEL_8087,
> +			USB_DEVICE_ID_SENSOR_HUB_09FA) },
> +	{ HID_USB_DEVICE(USB_VENDOR_ID_STANTUM_STM,
> +			USB_DEVICE_ID_SENSOR_HUB_7014) },
> +	{ }
> +};
> +
> +static const struct hid_usage_id sensor_hub_grabbed_usages[] =3D {
> +	{ HID_ANY_ID, HID_ANY_ID, HID_ANY_ID },
> +	{ HID_ANY_ID - 1, HID_ANY_ID - 1, HID_ANY_ID - 1 } };
> +
> +static struct hid_driver sensor_hub_driver =3D {
> +	.name =3D "hid-sensor-hub",
> +	.id_table =3D sensor_hub_devices,
> +	.probe =3D sensor_hub_probe,
> +	.remove =3D sensor_hub_remove,
> +	.raw_event =3D sensor_hub_raw_event,
> +#ifdef CONFIG_PM
> +	.suspend =3D sensor_hub_suspend,
> +	.resume =3D  sensor_hub_resume,
> +	.reset_resume =3D  sensor_hub_reset_resume, #endif };
> +
> +static int __init sensor_hub_init(void) {
> +	return hid_register_driver(&sensor_hub_driver);
> +}
> +
> +static void __exit sensor_hub_exit(void) {
> +	hid_unregister_driver(&sensor_hub_driver);
> +}
> +
> +module_init(sensor_hub_init);
> +module_exit(sensor_hub_exit);
> +
> +MODULE_DESCRIPTION("HID Sensor Hub driver"); MODULE_AUTHOR("Srinivas=20
> +Pandruvada <srinivas.pandruvada@intel.com>");
> +MODULE_LICENSE("GPL");
> diff --git a/include/linux/hid-sensor-hub.h=20
> b/include/linux/hid-sensor-hub.h new file mode 100644 index=20
> 0000000..0aa5f4c
> --- /dev/null
> +++ b/include/linux/hid-sensor-hub.h
> @@ -0,0 +1,160 @@
> +/*
> + * HID Sensors Driver
> + * Copyright (c) 2012, Intel Corporation.
> + *
> + * This program is free software; you can redistribute it and/or=20
> +modify it
> + * under the terms and conditions of the GNU General Public License,
> + * version 2, as published by the Free Software Foundation.
> + *
> + * This program is distributed in the hope it will be useful, but=20
> +WITHOUT
> + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY=20
> +or
> + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public=20
> +License for
> + * more details.
> + *
> + * You should have received a copy of the GNU General Public License=20
> +along with
> + * this program; if not, write to the Free Software Foundation, Inc.,
> + * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
> + *
> + */
> +#ifndef _HID_SENSORS_HUB_H
> +#define _HID_SENSORS_HUB_H
> +
> +#include <linux/hid.h>
> +#include <linux/hid-sensor-ids.h>
> +
> +/**
> + * struct hid_sensor_hub_attribute_info - Attribute info
> + * @usage_id:		Parent usage id of a physical device.
> + * @attrib_id:		Attribute id for this attribute.
> + * @report_id:		Report id in which this information resides.
> + * @index:		Field index in the report.
> + * @units:		Measurment unit for this attribute.
> + * @unit_expo:		Exponent used in the data.
> + * @size:		Size in bytes for data size.
> + */
> +struct hid_sensor_hub_attribute_info {
> +	u32 usage_id;
> +	u32 attrib_id;
> +	s32 report_id;
> +	s32 index;
> +	s32 units;
> +	s32 unit_expo;
> +	s32 size;
> +};
> +
> +/**
> + * struct hid_sensor_hub_device - Stores the hub instance data
> + * @hdev:		Stores the hid instance.
> + * @vendor_id:		Vendor id of hub device.
> + * @product_id:		Product id of hub device.
> + */
> +struct hid_sensor_hub_device {
> +	struct hid_device *hdev;
> +	u32 vendor_id;
> +	u32 product_id;
> +};
> +
> +/**
> + * struct hid_sensor_hub_callbacks - Client callback functions
> + * @pdev:		Platform device instance of the client driver.
> + * @suspend:		Suspend callback.
> + * @resume:		Resume callback.
> + * @capture_sample:	Callback to get a sample.
> + * @send_event:		Send notification to indicate all samples are
> + *			captured, process and send event
> + */
> +struct hid_sensor_hub_callbacks {
> +	struct platform_device *pdev;
> +	int (*suspend)(struct hid_sensor_hub_device *hsdev, void *priv);
> +	int (*resume)(struct hid_sensor_hub_device *hsdev, void *priv);
> +	int (*capture_sample)(struct hid_sensor_hub_device *hsdev,
> +			u32 usage_id, size_t raw_len, char *raw_data,
> +			void *priv);
> +	int (*send_event)(struct hid_sensor_hub_device *hsdev, u32 usage_id,
> +			 void *priv);
> +};
> +
> +/* Registration functions */
> +
> +/**
> +* sensor_hub_register_callback() - Register client callbacks
> +* @hsdev:	Hub device instance.
> +* @usage_id:	Usage id of the client (E.g. 0x200076 for Gyro).
> +* @usage_callback: Callback function storage
> +*
> +* Used to register callbacks by client processing drivers. Sensor
> +* hub core driver will call these callbacks to offload processing
> +* of data streams and notifications.
> +*/
> +int sensor_hub_register_callback(struct hid_sensor_hub_device *hsdev,
> +			u32 usage_id,
> +			struct hid_sensor_hub_callbacks *usage_callback);
> +
> +/**
> +* sensor_hub_remove_callback() - Remove client callbacks
> +* @hsdev:	Hub device instance.
> +* @usage_id:	Usage id of the client (E.g. 0x200076 for Gyro).
> +*
> +* If there is a callback registred, this call will remove that
> +* callbacks, so that it will stop data and event notifications.
> +*/
> +int sensor_hub_remove_callback(struct hid_sensor_hub_device *hsdev,
> +			u32 usage_id);
> +
> +
> +/* Hid sensor hub core interfaces */
> +
> +/**
> +* sensor_hub_input_get_attribute_info() - Get an attribute information
> +* @hsdev:	Hub device instance.
> +* @type:	Type of this attribute, input/output/feature
> +* @usage_id:	Attribute usage id of parent physical device as per spec
> +* @attr_usage_id:	Attribute usage id as per spec
> +* @info:	return information about attribute after parsing report
> +*
> +* Parses report and returns the attribute information such as report=20
> +id,
> +* field index, units and exponet etc.
> +*/
> +int sensor_hub_input_get_attribute_info(struct hid_sensor_hub_device *hs=
dev,
> +			u8 type,
> +			u32 usage_id, u32 attr_usage_id,
> +			struct hid_sensor_hub_attribute_info *info);
> +
> +/**
> +* sensor_hub_input_attr_get_raw_value() - Synchronous read request
> +* @usage_id:	Attribute usage id of parent physical device as per spec
> +* @attr_usage_id:	Attribute usage id as per spec
> +* @report_id:	Report id to look for
> +*
> +* Issues a synchronous read request for an input attribute. Returns
> +* data upto 32 bits. Since client can get events, so this call should
> +* not be used for data paths, this will impact performance.
> +*/
> +
> +int sensor_hub_input_attr_get_raw_value(struct hid_sensor_hub_device *hs=
dev,
> +			u32 usage_id,
> +			u32 attr_usage_id, u32 report_id);
> +/**
> +* sensor_hub_set_feature() - Feature set request
> +* @report_id:	Report id to look for
> +* @field_index:	Field index inside a report
> +* @value:	Value to set
> +*
> +* Used to set a field in feature report. For example this can set=20
> +polling
> +* interval, sensitivity, activate/deactivate state.
> +*/
> +int sensor_hub_set_feature(struct hid_sensor_hub_device *hsdev, u32 repo=
rt_id,
> +			u32 field_index, s32 value);
> +
> +/**
> +* sensor_hub_get_feature() - Feature get request
> +* @report_id:	Report id to look for
> +* @field_index:	Field index inside a report
> +* @value:	Place holder for return value
> +*
> +* Used to get a field in feature report. For example this can get=20
> +polling
> +* interval, sensitivity, activate/deactivate state.
> +*/
> +int sensor_hub_get_feature(struct hid_sensor_hub_device *hsdev, u32 repo=
rt_id,
> +			u32 field_index, s32 *value);
> +#endif
> diff --git a/include/linux/hid-sensor-ids.h=20
> b/include/linux/hid-sensor-ids.h new file mode 100644 index=20
> 0000000..292e80f
> --- /dev/null
> +++ b/include/linux/hid-sensor-ids.h
> @@ -0,0 +1,111 @@
> +/*
> + * HID Sensors Driver
> + * Copyright (c) 2012, Intel Corporation.
> + *
> + * This program is free software; you can redistribute it and/or=20
> +modify it
> + * under the terms and conditions of the GNU General Public License,
> + * version 2, as published by the Free Software Foundation.
> + *
> + * This program is distributed in the hope it will be useful, but=20
> +WITHOUT
> + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY=20
> +or
> + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public=20
> +License for
> + * more details.
> + *
> + * You should have received a copy of the GNU General Public License=20
> +along with
> + * this program; if not, write to the Free Software Foundation, Inc.,
> + * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
> + *
> + */
> +#ifndef _HID_SENSORS_IDS_H
> +#define _HID_SENSORS_IDS_H
> +
> +#define HID_UP_SENSOR						0x00200000
> +
> +/* Accel 3D (200073) */
> +#define HID_USAGE_SENSOR_ACCEL_3D				0x200073
> +#define HID_USAGE_SENSOR_ACCEL_X_AXIS				0x200453
> +#define HID_USAGE_SENSOR_ACCEL_Y_AXIS				0x200454
> +#define HID_USAGE_SENSOR_ACCEL_Z_AXIS				0x200455
> +
> +/* ALS (200041) */
> +#define HID_USAGE_SENSOR_ALS					0x200041
> +#define HID_USAGE_SENSOR_LIGHT_ILLUM				0x2004d1
> +
> +/* Gyro 3D: (200076) */
> +#define HID_USAGE_SENSOR_GYRO_3D				0x200076
> +#define HID_USAGE_SENSOR_ANGL_VELOCITY_X_AXIS			0x200457
> +#define HID_USAGE_SENSOR_ANGL_VELOCITY_Y_AXIS			0x200458
> +#define HID_USAGE_SENSOR_ANGL_VELOCITY_Z_AXIS			0x200459
> +
> +/*ORIENTATION: Compass 3D: (200083) */
> +#define HID_USAGE_SENSOR_COMPASS_3D				0x200083
> +#define HID_USAGE_SENSOR_ORIENT_MAGN_HEADING			0x200471
> +#define HID_USAGE_SENSOR_ORIENT_MAGN_HEADING_X			0x200472
> +#define HID_USAGE_SENSOR_ORIENT_MAGN_HEADING_Y			0x200473
> +#define HID_USAGE_SENSOR_ORIENT_MAGN_HEADING_Z			0x200474
> +
> +#define HID_USAGE_SENSOR_ORIENT_COMP_MAGN_NORTH			0x200475
> +#define HID_USAGE_SENSOR_ORIENT_COMP_TRUE_NORTH			0x200476
> +#define HID_USAGE_SENSOR_ORIENT_MAGN_NORTH			0x200477
> +#define HID_USAGE_SENSOR_ORIENT_TRUE_NORTH			0x200478
> +
> +#define HID_USAGE_SENSOR_ORIENT_DISTANCE			0x200479
> +#define HID_USAGE_SENSOR_ORIENT_DISTANCE_X			0x20047A
> +#define HID_USAGE_SENSOR_ORIENT_DISTANCE_Y			0x20047B
> +#define HID_USAGE_SENSOR_ORIENT_DISTANCE_Z			0x20047C
> +#define HID_USAGE_SENSOR_ORIENT_DISTANCE_OUT_OF_RANGE		0x20047D
> +#define HID_USAGE_SENSOR_ORIENT_TILT				0x20047E
> +#define HID_USAGE_SENSOR_ORIENT_TILT_X				0x20047F
> +#define HID_USAGE_SENSOR_ORIENT_TILT_Y				0x200480
> +#define HID_USAGE_SENSOR_ORIENT_TILT_Z				0x200481
> +#define HID_USAGE_SENSOR_ORIENT_ROTATION_MATRIX			0x200482
> +#define HID_USAGE_SENSOR_ORIENT_QUATERNION			0x200483
> +#define HID_USAGE_SENSOR_ORIENT_MAGN_FLUX			0x200484
> +
> +#define HID_USAGE_SENSOR_ORIENT_MAGN_FLUX_X_AXIS		0x200485
> +#define HID_USAGE_SENSOR_ORIENT_MAGN_FLUX_Y_AXIS		0x200486
> +#define HID_USAGE_SENSOR_ORIENT_MAGN_FLUX_Z_AXIS		0x200487
> +
> +/* Units */
> +#define HID_USAGE_SENSOR_UNITS_NOT_SPECIFIED			0x00
> +#define HID_USAGE_SENSOR_UNITS_LUX				0x01
> +#define HID_USAGE_SENSOR_UNITS_KELVIN				0x01000100
> +#define HID_USAGE_SENSOR_UNITS_FAHRENHEIT			0x03000100
> +#define HID_USAGE_SENSOR_UNITS_PASCAL				0xF1E1
> +#define HID_USAGE_SENSOR_UNITS_NEWTON				0x11E1
> +#define HID_USAGE_SENSOR_UNITS_METERS_PER_SECOND		0x11F0
> +#define HID_USAGE_SENSOR_UNITS_METERS_PER_SEC_SQRD		0x11E0
> +#define HID_USAGE_SENSOR_UNITS_FARAD				0xE14F2000
> +#define HID_USAGE_SENSOR_UNITS_AMPERE				0x01001000
> +#define HID_USAGE_SENSOR_UNITS_WATT				0x21d1
> +#define HID_USAGE_SENSOR_UNITS_HENRY				0x21E1E000
> +#define HID_USAGE_SENSOR_UNITS_OHM				0x21D1E000
> +#define HID_USAGE_SENSOR_UNITS_VOLT				0x21D1F000
> +#define HID_USAGE_SENSOR_UNITS_HERTZ				0x01F0
> +#define HID_USAGE_SENSOR_UNITS_DEGREES_PER_SEC_SQRD		0x14E0
> +#define HID_USAGE_SENSOR_UNITS_RADIANS				0x12
> +#define HID_USAGE_SENSOR_UNITS_RADIANS_PER_SECOND		0x12F0
> +#define HID_USAGE_SENSOR_UNITS_RADIANS_PER_SEC_SQRD		0x12E0
> +#define HID_USAGE_SENSOR_UNITS_SECOND				0x0110
> +#define HID_USAGE_SENSOR_UNITS_GAUSS				0x01E1F000
> +#define HID_USAGE_SENSOR_UNITS_GRAM				0x0101
> +#define HID_USAGE_SENSOR_UNITS_CENTIMETER			0x11
> +#define HID_USAGE_SENSOR_UNITS_G				0x1A
> +#define HID_USAGE_SENSOR_UNITS_MILLISECOND			0x19
> +#define HID_USAGE_SENSOR_UNITS_PERCENT				0x17
> +#define HID_USAGE_SENSOR_UNITS_DEGREES				0x14
> +#define HID_USAGE_SENSOR_UNITS_DEGREES_PER_SECOND		0x15
> +
> +/* Common selectors */
> +#define HID_USAGE_SENSOR_PROP_REPORT_INTERVAL			0x20030E
> +#define HID_USAGE_SENSOR_PROP_SENSITIVITY_ABS			0x20030F
> +#define HID_USAGE_SENSOR_PROP_SENSITIVITY_RANGE_PCT		0x200310
> +#define HID_USAGE_SENSOR_PROP_SENSITIVITY_REL_PCT		0x200311
> +#define HID_USAGE_SENSOR_PROP_ACCURACY				0x200312
> +#define HID_USAGE_SENSOR_PROP_RESOLUTION			0x200313
> +#define HID_USAGE_SENSOR_PROP_RANGE_MAXIMUM			0x200314
> +#define HID_USAGE_SENSOR_PROP_RANGE_MINIMUM			0x200315
> +#define HID_USAGE_SENSOR_PROP_REPORT_STATE			0x200316
> +#define HID_USAGE_SENSOR_PROY_POWER_STATE			0x200319
> +
> +#endif
>

^ permalink raw reply	[flat|nested] 7+ messages in thread

* RE: [PATCH 8/9] HID-Sensors: Added Compass/Magnetometer 3D
  2012-08-06 15:40       ` [PATCH 8/9] HID-Sensors: Added Compass/Magnetometer 3D Pandruvada, Srinivas
@ 2012-08-06 16:02         ` Jonathan Cameron
  0 siblings, 0 replies; 7+ messages in thread
From: Jonathan Cameron @ 2012-08-06 16:02 UTC (permalink / raw)
  To: Pandruvada, Srinivas, Jonathan Cameron
  Cc: linux-iio, Lars-Peter Clausen, Hennerich, Michael



"Pandruvada, Srinivas" <srinivas.pandruvada@intel.com> wrote:

>Hi Jonathan,
>
>Thanks for the review.
>These drivers implements the main elements which I can test in current
>hubs. There are some more elements, which can be added in future.
>For example Orientation sensor has many fields like Quattrocchi
>rotation matrix, which is defined in specification, but I didn't
>implement as I can't test.
>So keeping in separate folder, has advantage for extending capability
>of driver cleanly without much patch work to prevent regression on
>other drivers.
>
>Our internal review, also preferred separate driver as you suggested
>before.
>What do you think of letting this structure in with this structure (of
>course after your other suggested changes), and later improve based on
>feedback.

That approach is fine.  If long term these remain similar we can look at merging them then. Looking forward to the fun bits as they become available!
>
>Thanks,
>Srinivas
>
>
>
>
>
>
>
>
>-----Original Message-----
>From: Jonathan Cameron [mailto:jic23@kernel.org] 
>Sent: Monday, August 06, 2012 3:07 AM
>To: Pandruvada, Srinivas
>Cc: linux-iio; Lars-Peter Clausen; Hennerich, Michael
>Subject: Re: [PATCH 8/9] HID-Sensors: Added Compass/Magnetometer 3D
>
>On 06/08/2012 11:00, Jonathan Cameron wrote:
>> On 02/08/2012 05:28, srinivas pandruvada wrote:
>>> Added usage id processing for Compass 3D. This uses IIO interfaces 
>>> for triggerred buffer to present data to user mode.This uses HID 
>>> sensor framework for registering callback events from the sensor
>hub.
>>
>> Based on a quick look this can also be trivially handled by a
>combined 
>> accel/gyro/magnetometer driver.
>
>Thinking a bit more about this, I'm guessing the reason you have not
>done this so far is that I already told you to put the components into
>subdirectories. Looking at this I would say we need to perhaps be a
>little more flexible about that rule - either that or move the majority
>of this into your common/hid-sensors directory and have trivial drivers
>for each of the components so that we can have them in the
>accel/gyro/magnetometer directories. Or we make an arbitary call and as
>with device naming (where the first part supported gives the driver
>it's name) and arbitarily put the combined driver into accel.
>
>What do people think? (cc'd analog devices guys as they are probably
>most likely of current contributors to have similar situations in the
>future).
>
>Sorry for sending you down the route of doing the separate drivers in
>the first place. I clearly wasn't thinking this through....
>
>>
>>
>>>
>>> Signed-off-by: srinivas pandruvada <srinivas.pandruvada@intel.com>
>>> ---
>>>   drivers/iio/Kconfig                           |    1 +
>>>   drivers/iio/Makefile                          |    1 +
>>>   drivers/iio/magnetometer/Kconfig              |   16 +
>>>   drivers/iio/magnetometer/Makefile             |    6 +
>>>   drivers/iio/magnetometer/hid-sensor-magn-3d.c |  419
>>> +++++++++++++++++++++++++
>>>   5 files changed, 443 insertions(+), 0 deletions(-)
>>>   create mode 100644 drivers/iio/magnetometer/Kconfig
>>>   create mode 100644 drivers/iio/magnetometer/Makefile
>>>   create mode 100644 drivers/iio/magnetometer/hid-sensor-magn-3d.c
>>>
>>> diff --git a/drivers/iio/Kconfig b/drivers/iio/Kconfig index 
>>> 20212af..cdb3cfb 100644
>>> --- a/drivers/iio/Kconfig
>>> +++ b/drivers/iio/Kconfig
>>> @@ -59,5 +59,6 @@ source "drivers/iio/adc/Kconfig"
>>>   source "drivers/iio/amplifiers/Kconfig"
>>>   source "drivers/iio/common/Kconfig"
>>>   source "drivers/iio/gyro/Kconfig"
>>> +source "drivers/iio/magnetometer/Kconfig"
>>>
>>>   endif # IIO
>>> diff --git a/drivers/iio/Makefile b/drivers/iio/Makefile index 
>>> 7660bc4..71b643a 100644
>>> --- a/drivers/iio/Makefile
>>> +++ b/drivers/iio/Makefile
>>> @@ -15,3 +15,4 @@ obj-y += adc/
>>>   obj-y += amplifiers/
>>>   obj-y += common/
>>>   obj-y += gyro/
>>> +obj-y += magnetometer/
>>> diff --git a/drivers/iio/magnetometer/Kconfig
>>> b/drivers/iio/magnetometer/Kconfig
>>> new file mode 100644
>>> index 0000000..c1f0cdd
>>> --- /dev/null
>>> +++ b/drivers/iio/magnetometer/Kconfig
>>> @@ -0,0 +1,16 @@
>>> +#
>>> +# Magnetometer sensors
>>> +#
>>> +menu "Magnetometer sensors"
>>> +
>>> +config HID_SENSOR_MAGNETOMETER_3D
>>> +    depends on HID_SENSOR_HUB
>>> +    select IIO_BUFFER
>>> +    select IIO_TRIGGERED_BUFFER
>>> +    select HID_SENSOR_IIO_COMMON
>>> +    tristate "HID Magenetometer 3D"
>>> +    help
>>> +      Say yes here to build support for the HID SENSOR
>>> +      Magnetometer 3D.
>>> +
>>> +endmenu
>>> diff --git a/drivers/iio/magnetometer/Makefile
>>> b/drivers/iio/magnetometer/Makefile
>>> new file mode 100644
>>> index 0000000..cb26b44
>>> --- /dev/null
>>> +++ b/drivers/iio/magnetometer/Makefile
>>> @@ -0,0 +1,6 @@
>>> +#
>>> +# Makefile for industrial I/O Magnetometer sensor drivers #
>>> +
>>> +hid-sensor-magn-3d-drv-y := hid-sensor-magn-3d.o
>>> +obj-$(CONFIG_HID_SENSOR_MAGNETOMETER_3D) +=
>hid-sensor-magn-3d-drv.o
>>> diff --git a/drivers/iio/magnetometer/hid-sensor-magn-3d.c
>>> b/drivers/iio/magnetometer/hid-sensor-magn-3d.c
>>> new file mode 100644
>>> index 0000000..b34682d
>>> --- /dev/null
>>> +++ b/drivers/iio/magnetometer/hid-sensor-magn-3d.c
>>> @@ -0,0 +1,419 @@
>>> +/*
>>> + * HID Sensors Driver
>>> + * Copyright (c) 2012, Intel Corporation.
>>> + *
>>> + * This program is free software; you can redistribute it and/or
>>> modify it
>>> + * under the terms and conditions of the GNU General Public
>License,
>>> + * version 2, as published by the Free Software Foundation.
>>> + *
>>> + * This program is distributed in the hope 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 St - Fifth Floor, Boston, MA 02110-1301 USA.
>>> + *
>>> + */
>>> +#include <linux/device.h>
>>> +#include <linux/platform_device.h>
>>> +#include <linux/module.h>
>>> +#include <linux/interrupt.h>
>>> +#include <linux/irq.h>
>>> +#include <linux/slab.h>
>>> +#include <linux/hid-sensor-hub.h>
>>> +#include <linux/iio/iio.h>
>>> +#include <linux/iio/sysfs.h>
>>> +#include <linux/iio/buffer.h>
>>> +#include <linux/iio/trigger_consumer.h> #include 
>>> +<linux/iio/triggered_buffer.h> #include 
>>> +"../common/hid-sensors/hid-sensor-attributes.h"
>>> +#include "../common/hid-sensors/hid-sensor-trigger.h"
>>> +
>>> +/*Format: HID-SENSOR-usage_id_in_hex*/ #define DRIVER_NAME 
>>> +"HID-SENSOR-200083"
>>> +
>>> +enum magn_3d_channel {
>>> +    CHANNEL_SCAN_INDEX_X,
>>> +    CHANNEL_SCAN_INDEX_Y,
>>> +    CHANNEL_SCAN_INDEX_Z,
>>> +    MAGN_3D_CHANNEL_MAX,
>>> +};
>>> +
>>> +struct magn_3d_state {
>>> +    struct hid_sensor_iio_common common_attributes;
>>> +    struct hid_sensor_hub_attribute_info magn[MAGN_3D_CHANNEL_MAX];
>>> +    u32 magn_val[MAGN_3D_CHANNEL_MAX]; };
>>> +
>>> +const u32 magn_3d_addresses[MAGN_3D_CHANNEL_MAX] = {
>>> +    HID_USAGE_SENSOR_ORIENT_MAGN_FLUX_X_AXIS,
>>> +    HID_USAGE_SENSOR_ORIENT_MAGN_FLUX_Y_AXIS,
>>> +    HID_USAGE_SENSOR_ORIENT_MAGN_FLUX_Z_AXIS
>>> +};
>>> +
>>> +/* Channel definitions */
>>> +static struct iio_chan_spec magn_3d_channels[] = {
>>> +    {
>>> +        .type = IIO_MAGN,
>>> +        .modified = 1,
>>> +        .channel2 = IIO_MOD_X,
>>> +        .info_mask = IIO_CHAN_INFO_OFFSET_SHARED_BIT |
>>> +        IIO_CHAN_INFO_SCALE_SHARED_BIT |
>>> +        IIO_CHAN_INFO_SAMP_FREQ_SHARED_BIT |
>>> +        IIO_CHAN_INFO_HYSTERESIS_SHARED_BIT,
>>> +        .scan_index = CHANNEL_SCAN_INDEX_X,
>>> +    }, {
>>> +        .type = IIO_MAGN,
>>> +        .modified = 1,
>>> +        .channel2 = IIO_MOD_Y,
>>> +        .info_mask = IIO_CHAN_INFO_OFFSET_SHARED_BIT |
>>> +        IIO_CHAN_INFO_SCALE_SHARED_BIT |
>>> +        IIO_CHAN_INFO_SAMP_FREQ_SHARED_BIT |
>>> +        IIO_CHAN_INFO_HYSTERESIS_SHARED_BIT,
>>> +        .scan_index = CHANNEL_SCAN_INDEX_Y,
>>> +    }, {
>>> +        .type = IIO_MAGN,
>>> +        .modified = 1,
>>> +        .channel2 = IIO_MOD_Z,
>>> +        .info_mask = IIO_CHAN_INFO_OFFSET_SHARED_BIT |
>>> +        IIO_CHAN_INFO_SCALE_SHARED_BIT |
>>> +        IIO_CHAN_INFO_SAMP_FREQ_SHARED_BIT |
>>> +        IIO_CHAN_INFO_HYSTERESIS_SHARED_BIT,
>>> +        .scan_index = CHANNEL_SCAN_INDEX_Z,
>>> +    }
>>> +};
>>> +
>>> +/* Adjust channel real bits based on report descriptor */ static 
>>> +void magn_3d_adjust_channel_bit_mask(int channel, int size) {
>>> +    magn_3d_channels[channel].scan_type.sign = 's';
>>> +    /* Real storage bits will change based on the report desc. */
>>> +    magn_3d_channels[channel].scan_type.realbits = size * 8;
>>> +    /* Maximum size of a sample to capture is u32 */
>>> +    magn_3d_channels[channel].scan_type.storagebits = sizeof(u32) *
>
>>> +8; }
>>> +
>>> +/* Channel read_raw handler */
>>> +static int magn_3d_read_raw(struct iio_dev *indio_dev,
>>> +                  struct iio_chan_spec const *chan,
>>> +                  int *val, int *val2,
>>> +                  long mask)
>>> +{
>>> +    struct magn_3d_state *magn_state = iio_priv(indio_dev);
>>> +    int report_id = -1;
>>> +    u32 address;
>>> +    int ret;
>>> +
>>> +    *val = 0;
>>> +    *val2 = 0;
>>> +    switch (mask) {
>>> +    case 0:
>>> +        report_id =
>>> +            magn_state->magn[chan->scan_index].report_id;
>>> +        address = magn_3d_addresses[chan->scan_index];
>>> +        if (report_id >= 0)
>>> +            *val = sensor_hub_input_attr_get_raw_value(
>>> +                magn_state->common_attributes.hsdev,
>>> +                HID_USAGE_SENSOR_COMPASS_3D, address,
>>> +                report_id);
>>> +        else
>>> +            *val = 0;
>>> +        break;
>>> +    case IIO_CHAN_INFO_SCALE:
>>> +        *val = magn_state->magn[CHANNEL_SCAN_INDEX_X].units;
>>> +        break;
>>> +    case IIO_CHAN_INFO_OFFSET:
>>> +        *val = hid_sensor_convert_exponent(
>>> +            magn_state->magn[CHANNEL_SCAN_INDEX_X].unit_expo);
>>> +        break;
>>> +    case IIO_CHAN_INFO_SAMP_FREQ:
>>> +        ret = hid_sensor_read_samp_freq_value(
>>> +            &magn_state->common_attributes, val, val2);
>>> +        break;
>>> +    case IIO_CHAN_INFO_HYSTERESIS:
>>> +        ret = hid_sensor_read_raw_hyst_value(
>>> +            &magn_state->common_attributes, val, val2);
>>> +        break;
>>> +    default:
>>> +        break;
>>> +    }
>>> +
>>> +    if (*val2)
>>> +        return IIO_VAL_INT_PLUS_MICRO;
>>> +    else
>>> +        return IIO_VAL_INT;
>>> +}
>>> +
>>> +/* Channel write_raw handler */
>>> +static int magn_3d_write_raw(struct iio_dev *indio_dev,
>>> +                   struct iio_chan_spec const *chan,
>>> +                   int val,
>>> +                   int val2,
>>> +                   long mask)
>>> +{
>>> +    struct magn_3d_state *magn_state = iio_priv(indio_dev);
>>> +    int ret = 0;
>>> +
>>> +    switch (mask) {
>>> +    case IIO_CHAN_INFO_SAMP_FREQ:
>>> +        ret = hid_sensor_write_samp_freq_value(
>>> +                &magn_state->common_attributes, val, val2);
>>> +        break;
>>> +    case IIO_CHAN_INFO_HYSTERESIS:
>>> +        ret = hid_sensor_write_raw_hyst_value(
>>> +                &magn_state->common_attributes, val, val2);
>>> +        break;
>>> +    default:
>>> +        ret = -EINVAL;
>>> +    }
>>> +    return ret;
>>> +}
>>> +
>>> +static int magn_3d_write_raw_get_fmt(struct iio_dev *indio_dev,
>>> +                   struct iio_chan_spec const *chan,
>>> +                   long mask)
>>> +{
>>> +    return IIO_VAL_INT_PLUS_MICRO;
>>> +}
>>> +
>>> +static const struct iio_info magn_3d_info = {
>>> +    .driver_module = THIS_MODULE,
>>> +    .read_raw = &magn_3d_read_raw,
>>> +    .write_raw = &magn_3d_write_raw,
>>> +    .write_raw_get_fmt = &magn_3d_write_raw_get_fmt, };
>>> +
>>> +/* Function to push data to buffer */ void 
>>> +hid_sensor_push_data(struct iio_dev *indio_dev, u8 *data, int len)
>{
>>> +    struct iio_buffer *buffer = indio_dev->buffer;
>>> +    s64 timestamp = iio_get_time_ns();
>>> +    int datum_sz;
>>> +
>>> +    dev_dbg(&indio_dev->dev, "hid_sensor_push_data\n");
>>> +    if (!buffer) {
>>> +        dev_err(&indio_dev->dev, "Buffer == NULL\n");
>>> +        return;
>>> +    }
>>> +    datum_sz = buffer->access->get_bytes_per_datum(buffer);
>>> +    if (len > datum_sz) {
>>> +        dev_err(&indio_dev->dev, "Datum size mismatch %d:%d\n",
>len,
>>> +                datum_sz);
>>> +        return;
>>> +    }
>>> +    buffer->access->store_to(buffer, (u8 *)data, timestamp); }
>>> +
>>> +/* Callback handler to send event after all samples are received
>and
>>> captured */
>>> +int magn_3d_proc_event(struct hid_sensor_hub_device *hsdev,
>unsigned
>>> usage_id,
>>> +            void *priv)
>>> +{
>>> +    struct iio_dev *indio_dev = platform_get_drvdata(priv);
>>> +    struct magn_3d_state *magn_state = iio_priv(indio_dev);
>>> +
>>> +    dev_dbg(&indio_dev->dev, "magn_3d_proc_event [%d]\n",
>>> +                magn_state->common_attributes.data_ready);
>>> +    if (magn_state->common_attributes.data_ready)
>>> +        hid_sensor_push_data(indio_dev,
>>> +                (u8 *)magn_state->magn_val,
>>> +                sizeof(magn_state->magn_val));
>>> +
>>> +    return 0;
>>> +}
>>> +
>>> +/* Capture samples in local storage */ int 
>>> +magn_3d_capture_sample(struct hid_sensor_hub_device *hsdev,
>>> +                unsigned usage_id,
>>> +                size_t raw_len, char *raw_data,
>>> +                void *priv)
>>> +{
>>> +    struct iio_dev *indio_dev = platform_get_drvdata(priv);
>>> +    struct magn_3d_state *magn_state = iio_priv(indio_dev);
>>> +    int offset;
>>> +    int ret = -EINVAL;
>>> +
>>> +    switch (usage_id) {
>>> +    case HID_USAGE_SENSOR_ORIENT_MAGN_FLUX_X_AXIS:
>>> +    case HID_USAGE_SENSOR_ORIENT_MAGN_FLUX_Y_AXIS:
>>> +    case HID_USAGE_SENSOR_ORIENT_MAGN_FLUX_Z_AXIS:
>>> +        offset = usage_id -
>HID_USAGE_SENSOR_ORIENT_MAGN_FLUX_X_AXIS;
>>> +        magn_state->magn_val[CHANNEL_SCAN_INDEX_X + offset] =
>>> +                        *(u32 *)raw_data;
>>> +        ret = 0;
>>> +    break;
>>> +    default:
>>> +        break;
>>> +    }
>>> +
>>> +    return ret;
>>> +}
>>> +
>>> +/* Parse report which is specific to an usage id*/ static int 
>>> +magn_3d_parse_report(struct platform_device *pdev,
>>> +                struct hid_sensor_hub_device *hsdev,
>>> +                unsigned usage_id,
>>> +                struct magn_3d_state *st) {
>>> +    int ret;
>>> +    int i;
>>> +
>>> +    for (i = 0; i <= CHANNEL_SCAN_INDEX_Z; ++i) {
>>> +        ret = sensor_hub_input_get_attribute_info(hsdev,
>>> HID_INPUT_REPORT,
>>> +                usage_id,
>>> +                HID_USAGE_SENSOR_ORIENT_MAGN_FLUX_X_AXIS + i,
>>> +                &st->magn[CHANNEL_SCAN_INDEX_X + i]);
>>> +        magn_3d_adjust_channel_bit_mask(CHANNEL_SCAN_INDEX_X + i,
>>> +                st->magn[CHANNEL_SCAN_INDEX_X + i].size);
>>> +        if (ret < 0)
>>> +            break;
>>> +    }
>>> +    dev_dbg(&pdev->dev, "magn_3d %x:%x, %x:%x, %x:%x\n",
>>> +            st->magn[0].index,
>>> +            st->magn[0].report_id,
>>> +            st->magn[1].index, st->magn[1].report_id,
>>> +            st->magn[2].index, st->magn[2].report_id);
>>> +
>>> +    return ret;
>>> +}
>>> +
>>> +/* Function to initialize the processing for usage id */ static 
>>> +inline int magn_3d_init(struct platform_device *pdev,
>>> +            struct hid_sensor_hub_device *hsdev,
>>> +            unsigned usage_id)
>>> +{
>>> +    int ret = 0;
>>> +    static char *name = "magn_3d";
>>> +    struct iio_dev *indio_dev;
>>> +    struct magn_3d_state *magn_state;
>>> +
>>> +    indio_dev = iio_device_alloc(sizeof(struct magn_3d_state));
>>> +    if (indio_dev == NULL) {
>>> +        ret = -ENOMEM;
>>> +        goto error_ret;
>>> +    }
>>> +    platform_set_drvdata(pdev, indio_dev);
>>> +
>>> +    magn_state = iio_priv(indio_dev);
>>> +    magn_state->common_attributes.hsdev = hsdev;
>>> +    magn_state->common_attributes.pdev = pdev;
>>> +
>>> +    ret = hid_sensor_parse_common_attributes(hsdev,
>>> +                usage_id, &magn_state->common_attributes);
>>> +    if (ret) {
>>> +        dev_err(&pdev->dev, "failed to setup common attributes\n");
>>> +        goto error_free_dev;
>>> +    }
>>> +
>>> +    ret = magn_3d_parse_report(pdev, hsdev, usage_id, magn_state);
>>> +    if (ret) {
>>> +        dev_err(&pdev->dev, "failed to setup attributes\n");
>>> +        goto error_free_dev;
>>> +    }
>>> +
>>> +    indio_dev->channels = kmemdup(magn_3d_channels,
>>> +                    sizeof(magn_3d_channels),
>>> +                    GFP_KERNEL);
>>> +    if (!indio_dev->channels) {
>>> +        dev_err(&pdev->dev, "failed to duplicate channels\n");
>>> +        goto error_free_dev;
>>> +    }
>>> +
>>> +    indio_dev->num_channels =
>>> +                ARRAY_SIZE(magn_3d_channels);
>>> +    indio_dev->dev.parent = &pdev->dev;
>>> +    indio_dev->info = &magn_3d_info;
>>> +    indio_dev->name = name;
>>> +    indio_dev->modes = INDIO_DIRECT_MODE;
>>> +
>>> +    ret = iio_triggered_buffer_setup(indio_dev,
>>> &iio_pollfunc_store_time,
>>> +        NULL, NULL);
>>> +    if (ret) {
>>> +        dev_err(&pdev->dev, "failed to initialize trigger
>buffer\n");
>>> +        goto error_free_dev_mem;
>>> +    }
>>> +    magn_state->common_attributes.data_ready = false;
>>> +    ret = hid_sensor_setup_trigger(indio_dev, name,
>>> +                    &magn_state->common_attributes);
>>> +    if (ret < 0) {
>>> +        dev_err(&pdev->dev, "trigger setup failed\n");
>>> +        goto error_unreg_buffer_funcs;
>>> +    }
>>> +
>>> +    ret = iio_device_register(indio_dev);
>>> +    if (ret) {
>>> +        dev_err(&pdev->dev, "device register failed\n");
>>> +        goto error_remove_trigger;
>>> +    }
>>> +
>>> +    return ret;
>>> +
>>> +error_remove_trigger:
>>> +    hid_sensor_remove_trigger(indio_dev);
>>> +error_unreg_buffer_funcs:
>>> +    iio_triggered_buffer_cleanup(indio_dev);
>>> +error_free_dev_mem:
>>> +    kfree(indio_dev->channels);
>>> +error_free_dev:
>>> +    iio_device_free(indio_dev);
>>> +error_ret:
>>> +    return ret;
>>> +}
>>> +
>>> +/* Function to deinitialize the processing for usage id */ static 
>>> +inline void magn_3d_exit(struct platform_device *pdev) {
>>> +    struct iio_dev *indio_dev = platform_get_drvdata(pdev);
>>> +
>>> +    iio_device_unregister(indio_dev);
>>> +    hid_sensor_remove_trigger(indio_dev);
>>> +    iio_triggered_buffer_cleanup(indio_dev);
>>> +    kfree(indio_dev->channels);
>>> +    iio_device_free(indio_dev);
>>> +}
>>> +
>>> +static struct hid_sensor_hub_callbacks magn_3d_callbacks = {
>>> +    .send_event = magn_3d_proc_event,
>>> +    .capture_sample = magn_3d_capture_sample, };
>>> +
>>> +static int __devinit hid_magn_3d_probe(struct platform_device
>*pdev) 
>>> +{
>>> +    int ret;
>>> +    struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
>>> +
>>> +    ret = magn_3d_init(pdev, hsdev, HID_USAGE_SENSOR_COMPASS_3D);
>>> +    if (ret < 0) {
>>> +        dev_err(&pdev->dev, "magn_3d_init failed\n");
>>> +        return ret;
>>> +    }
>>> +    magn_3d_callbacks.pdev = pdev;
>>> +    ret = sensor_hub_register_callback(hsdev,
>>> HID_USAGE_SENSOR_COMPASS_3D,
>>> +                    &magn_3d_callbacks);
>>> +
>>> +    return ret;
>>> +}
>>> +
>>> +static int __devinit hid_magn_3d_remove(struct platform_device 
>>> +*pdev) {
>>> +    struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
>>> +
>>> +    magn_3d_exit(pdev);
>>> +
>>> +    return sensor_hub_remove_callback(hsdev,
>>> HID_USAGE_SENSOR_COMPASS_3D);
>>> +}
>>> +
>>> +static struct platform_driver hid_magn_3d_platform_driver = {
>>> +    .driver = {
>>> +        .name    = DRIVER_NAME,
>>> +        .owner    = THIS_MODULE,
>>> +    },
>>> +    .probe        = hid_magn_3d_probe,
>>> +    .remove        = hid_magn_3d_remove,
>>> +};
>>> +module_platform_driver(hid_magn_3d_platform_driver);
>>> +
>>> +MODULE_DESCRIPTION("HID Sensor Magnetometer 3D"); 
>>> +MODULE_AUTHOR("Srinivas Pandruvada 
>>> +<srinivas.pandruvada@intel.com>");
>>> +MODULE_LICENSE("GPL");
>>>
>>

-- 
Sent from my Android phone with K-9 Mail. Please excuse my brevity.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH 4/9] HID-Sensors: Sensor framework
  2012-08-27 15:49 ` [PATCH 4/9] HID-Sensors: Sensor framework srinivas pandruvada
@ 2012-09-05  9:40   ` Jiri Kosina
  0 siblings, 0 replies; 7+ messages in thread
From: Jiri Kosina @ 2012-09-05  9:40 UTC (permalink / raw)
  To: srinivas pandruvada; +Cc: linux-iio, jic23

On Mon, 27 Aug 2012, srinivas pandruvada wrote:

> Adding processing for HID Sensor usage table as defined by HID 1.12, 
> Request #: HUTRR39, dated 05 May, 2011. This driver uses HID driver 
> framework to register, send and receive events. This uses MFD framework, 
> so that actual processing for a specific usage id can be done in a 
> different driver. For example an accelerometer driver can be a separate 
> driver and use the interface provided by this driver to register for 
> events.

Also, please change the summary/subject line of this commit to

	HID: sensors: introduce sensor framework

> Signed-off-by: srinivas pandruvada <srinivas.pandruvada@intel.com>
> ---
>  drivers/hid/Kconfig            |   14 +
>  drivers/hid/Makefile           |    1 +
>  drivers/hid/hid-sensor-hub.c   |  682 ++++++++++++++++++++++++++++++++++++++++
>  include/linux/hid-sensor-hub.h |  160 ++++++++++
>  include/linux/hid-sensor-ids.h |  111 +++++++
>  5 files changed, 968 insertions(+), 0 deletions(-)
>  create mode 100644 drivers/hid/hid-sensor-hub.c
>  create mode 100644 include/linux/hid-sensor-hub.h
>  create mode 100644 include/linux/hid-sensor-ids.h
> 
> diff --git a/drivers/hid/Kconfig b/drivers/hid/Kconfig
> index 034c80a..3138c26 100644
> --- a/drivers/hid/Kconfig
> +++ b/drivers/hid/Kconfig
> @@ -660,6 +660,20 @@ config HID_ZYDACRON
>  	---help---
>  	Support for Zydacron remote control.
>  
> +config HID_SENSOR_HUB
> +	tristate "HID Sensors framework support"
> +	depends on USB_HID
> +	select MFD_CORE
> +	default n
> +	-- help---
> +	  Support for HID Sensor framework. This creates a MFD instance
> +	  for a sensor hub and identifies all the sensors connected to it.
> +	  Each sensor is registered as a MFD cell, so that sensor specific
> +	  processing can be done in a separate driver. Each sensor
> +	  drivers can use the service provided by this driver to register
> +	  for events and handle data streams. Each sensor driver can format
> +	  data and present to user mode using input or IIO interface.
> +
>  endmenu
>  
>  endif # HID_SUPPORT
> diff --git a/drivers/hid/Makefile b/drivers/hid/Makefile
> index ca6cc9f..08f9d8f 100644
> --- a/drivers/hid/Makefile
> +++ b/drivers/hid/Makefile
> @@ -87,6 +87,7 @@ obj-$(CONFIG_HID_ZYDACRON)	+= hid-zydacron.o
>  obj-$(CONFIG_HID_WACOM)		+= hid-wacom.o
>  obj-$(CONFIG_HID_WALTOP)	+= hid-waltop.o
>  obj-$(CONFIG_HID_WIIMOTE)	+= hid-wiimote.o
> +obj-$(CONFIG_HID_SENSOR_HUB)	+= hid-sensor-hub.o
>  
>  obj-$(CONFIG_USB_HID)		+= usbhid/
>  obj-$(CONFIG_USB_MOUSE)		+= usbhid/
> diff --git a/drivers/hid/hid-sensor-hub.c b/drivers/hid/hid-sensor-hub.c
> new file mode 100644
> index 0000000..60e78fb
> --- /dev/null
> +++ b/drivers/hid/hid-sensor-hub.c
> @@ -0,0 +1,682 @@
> +/*
> + * HID Sensors Driver
> + * Copyright (c) 2012, Intel Corporation.
> + *
> + * This program is free software; you can redistribute it and/or modify it
> + * under the terms and conditions of the GNU General Public License,
> + * version 2, as published by the Free Software Foundation.
> + *
> + * This program is distributed in the hope 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 St - Fifth Floor, Boston, MA 02110-1301 USA.
> + *
> + */
> +#include <linux/device.h>
> +#include <linux/hid.h>
> +#include <linux/usb.h>
> +#include "usbhid/usbhid.h"
> +#include <linux/module.h>
> +#include <linux/slab.h>
> +#include <linux/mfd/core.h>
> +#include <linux/list.h>
> +#include <linux/hid-sensor-ids.h>
> +#include <linux/hid-sensor-hub.h>
> +#include "hid-ids.h"
> +
> +/**
> + * struct sensor_hub_pending - Synchronous read pending information
> + * @status:		Pending status true/false.
> + * @ready:		Completion synchronization data.
> + * @usage_id:		Usage id for physical device, E.g. Gyro usage id.
> + * @attr_usage_id:	Usage Id of a field, E.g. X-AXIS for a gyro.
> + * @raw_size:		Response size for a read request.
> + * @raw_data:		Place holder for received response.
> + */
> +struct sensor_hub_pending {
> +	bool status;
> +	struct completion ready;
> +	u32 usage_id;
> +	u32 attr_usage_id;
> +	int raw_size;
> +	u8  *raw_data;
> +};
> +
> +/**
> + * struct sensor_hub_data - Hold a instance data for a HID hub device
> + * @hsdev:		Stored hid instance for current hub device.
> + * @mutex:		Mutex to serialize synchronous request.
> + * @lock:		Spin lock to protect pending request structure.
> + * @pending:		Holds information of pending sync read request.
> + * @dyn_callback_list:	Holds callback function
> + * @dyn_lock:		spin lock to prevent callback list

I guess 'prevent' should be 'protect' here, right?

Also, I'd prefer some more descriptive name ... it took me a while to 
figure out what this lock is actually protecting. Perhaps 'callback_lock' 
would work better?

> + * @hid_sensor_hub_client_devs:	Stores all MFD cells for a hub instance.
> + * @hid_sensor_client_cnt: Number of MFD cells, (no of sensors attached).
> + */
> +struct sensor_hub_data {
> +	struct hid_sensor_hub_device *hsdev;
> +	struct mutex mutex;
> +	spinlock_t lock;
> +	struct sensor_hub_pending pending;
> +	struct list_head dyn_callback_list;
> +	spinlock_t dyn_lock;
> +	struct mfd_cell *hid_sensor_hub_client_devs;
> +	int hid_sensor_client_cnt;
> +};
> +
> +/**
> + * struct hid_sensor_hub_callbacks_list - Stores callback list
> + * @list:		list head.
> + * @usage_id:		usage id for a physical device.
> + * @usage_callback:	Stores registered callback functions.
> + * @priv:		Private data for a physical device.
> + */
> +struct hid_sensor_hub_callbacks_list {
> +	struct list_head list;
> +	u32 usage_id;
> +	struct hid_sensor_hub_callbacks *usage_callback;
> +	void *priv;
> +};
> +
> +static int sensor_hub_check_for_sensor_page(struct hid_device *hdev)
> +{
> +	int i;
> +	int ret = -EINVAL;
> +
> +	for (i = 0; i < hdev->maxcollection; i++) {
> +		struct hid_collection *col = &hdev->collection[i];
> +		if (col->type == HID_COLLECTION_PHYSICAL &&
> +		   (col->usage & HID_USAGE_PAGE) == HID_UP_SENSOR) {
> +			ret = 0;
> +			break;
> +		}
> +	}
> +
> +	return ret;
> +}
> +
> +static struct hid_report *sensor_hub_report(int id, struct hid_device *hdev,
> +						int dir)
> +{
> +	struct hid_report *report;
> +
> +	list_for_each_entry(report, &hdev->report_enum[dir].report_list, list) {
> +		if (report->id == id)
> +			return report;
> +	}
> +	hid_warn(hdev, "No report with id 0x%x found\n", id);
> +
> +	return NULL;
> +}
> +
> +static int sensor_hub_get_physical_device_count(
> +				struct hid_report_enum *report_enum)
> +{
> +	struct hid_report *report;
> +	struct hid_field *field;
> +	int cnt = 0;
> +
> +	list_for_each_entry(report, &report_enum->report_list, list) {
> +		field = report->field[0];
> +		if (report->maxfield && field &&
> +					field->physical)
> +			cnt++;
> +	}
> +
> +	return cnt;
> +}
> +
> +static struct hid_sensor_hub_callbacks *sensor_hub_get_callback(
> +					struct hid_device *hdev,
> +					u32 usage_id, void **priv)
> +{
> +	struct hid_sensor_hub_callbacks_list *callback;
> +	struct sensor_hub_data *pdata = hid_get_drvdata(hdev);
> +
> +	spin_lock(&pdata->dyn_lock);
> +	list_for_each_entry(callback, &pdata->dyn_callback_list, list)
> +		if (callback->usage_id == usage_id) {
> +			*priv = callback->priv;
> +			spin_unlock(&pdata->dyn_lock);
> +			return callback->usage_callback;
> +		}
> +	spin_unlock(&pdata->dyn_lock);
> +
> +	return NULL;
> +}
> +
> +int sensor_hub_register_callback(struct hid_sensor_hub_device *hsdev,
> +			u32 usage_id,
> +			struct hid_sensor_hub_callbacks *usage_callback)
> +{
> +	struct hid_sensor_hub_callbacks_list *callback;
> +	struct sensor_hub_data *pdata = hid_get_drvdata(hsdev->hdev);
> +
> +	spin_lock(&pdata->dyn_lock);
> +	list_for_each_entry(callback, &pdata->dyn_callback_list, list)
> +		if (callback->usage_id == usage_id) {
> +			spin_unlock(&pdata->dyn_lock);
> +			return -EINVAL;
> +		}
> +	callback = kzalloc(sizeof(*callback), GFP_KERNEL);
> +	if (!callback) {
> +		spin_unlock(&pdata->dyn_lock);
> +		return -ENOMEM;
> +	}
> +	callback->usage_callback = usage_callback;
> +	callback->usage_id = usage_id;
> +	callback->priv = NULL;
> +	list_add_tail(&callback->list, &pdata->dyn_callback_list);
> +	spin_unlock(&pdata->dyn_lock);
> +
> +	return 0;
> +}
> +EXPORT_SYMBOL_GPL(sensor_hub_register_callback);
> +
> +int sensor_hub_remove_callback(struct hid_sensor_hub_device *hsdev,
> +				u32 usage_id)
> +{
> +	struct hid_sensor_hub_callbacks_list *callback;
> +	struct sensor_hub_data *pdata = hid_get_drvdata(hsdev->hdev);
> +
> +	spin_lock(&pdata->dyn_lock);
> +	list_for_each_entry(callback, &pdata->dyn_callback_list, list)
> +		if (callback->usage_id == usage_id) {
> +			list_del(&callback->list);
> +			kfree(callback);
> +			break;
> +		}
> +	spin_unlock(&pdata->dyn_lock);
> +
> +	return 0;
> +}
> +EXPORT_SYMBOL_GPL(sensor_hub_remove_callback);
> +
> +int sensor_hub_set_feature(struct hid_sensor_hub_device *hsdev, u32 report_id,
> +				u32 field_index, s32 value)
> +{
> +	struct hid_report *report;
> +	struct sensor_hub_data *data =  hid_get_drvdata(hsdev->hdev);
> +	int ret = 0;
> +
> +	if (report_id < 0)
> +		return -EINVAL;
> +
> +	mutex_lock(&data->mutex);
> +	report = sensor_hub_report(report_id, hsdev->hdev, HID_FEATURE_REPORT);
> +	if (!report || (field_index >=  report->maxfield)) {
> +		ret = -EINVAL;
> +		goto done_proc;
> +	}
> +	hid_set_field(report->field[field_index], 0, value);
> +	usbhid_submit_report(hsdev->hdev, report, USB_DIR_OUT);
> +	usbhid_wait_io(hsdev->hdev);
> +
> +done_proc:
> +	mutex_unlock(&data->mutex);
> +
> +	return ret;
> +}
> +EXPORT_SYMBOL_GPL(sensor_hub_set_feature);
> +
> +int sensor_hub_get_feature(struct hid_sensor_hub_device *hsdev, u32 report_id,
> +				u32 field_index, s32 *value)
> +{
> +	struct hid_report *report;
> +	struct sensor_hub_data *data =  hid_get_drvdata(hsdev->hdev);
> +	int ret = 0;
> +
> +	if (report_id < 0)
> +		return -EINVAL;
> +
> +	mutex_lock(&data->mutex);
> +	report = sensor_hub_report(report_id, hsdev->hdev, HID_FEATURE_REPORT);
> +	if (!report || (field_index >=  report->maxfield)) {
> +		ret = -EINVAL;
> +		goto done_proc;
> +	}
> +	usbhid_submit_report(hsdev->hdev, report, USB_DIR_IN);
> +	usbhid_wait_io(hsdev->hdev);
> +	*value = report->field[field_index]->value[0];
> +
> +done_proc:
> +	mutex_unlock(&data->mutex);
> +
> +	return ret;
> +}
> +EXPORT_SYMBOL_GPL(sensor_hub_get_feature);
> +
> +
> +int sensor_hub_input_attr_get_raw_value(struct hid_sensor_hub_device *hsdev,
> +					u32 usage_id,
> +					u32 attr_usage_id, u32 report_id)
> +{
> +	struct sensor_hub_data *data =  hid_get_drvdata(hsdev->hdev);
> +	unsigned long flags;
> +	struct hid_report *report;
> +	int ret_val = 0;
> +
> +	if (report_id < 0)
> +		return -EINVAL;
> +
> +	mutex_lock(&data->mutex);
> +	memset(&data->pending, 0, sizeof(data->pending));
> +	init_completion(&data->pending.ready);
> +	data->pending.usage_id = usage_id;
> +	data->pending.attr_usage_id = attr_usage_id;
> +	data->pending.raw_size = 0;
> +
> +	spin_lock_irqsave(&data->lock, flags);
> +	data->pending.status = true;
> +	report = sensor_hub_report(report_id, hsdev->hdev, HID_INPUT_REPORT);
> +	if (!report) {
> +		spin_unlock_irqrestore(&data->lock, flags);
> +		goto err_free;
> +	}
> +	usbhid_submit_report(hsdev->hdev, report, USB_DIR_IN);
> +	spin_unlock_irqrestore(&data->lock, flags);
> +	wait_for_completion_interruptible_timeout(&data->pending.ready, HZ*5);
> +	switch (data->pending.raw_size) {
> +	case 1:
> +		ret_val = *(u8 *)data->pending.raw_data;
> +		break;
> +	case 2:
> +		ret_val = *(u16 *)data->pending.raw_data;
> +		break;
> +	case 4:
> +		ret_val = *(u32 *)data->pending.raw_data;
> +		break;
> +	default:
> +		ret_val = 0;
> +	}
> +	kfree(data->pending.raw_data);
> +
> +err_free:
> +	data->pending.status = false;
> +	mutex_unlock(&data->mutex);
> +
> +	return ret_val;
> +}
> +EXPORT_SYMBOL_GPL(sensor_hub_input_attr_get_raw_value);
> +
> +int sensor_hub_input_get_attribute_info(struct hid_sensor_hub_device *hsdev,
> +				u8 type,
> +				u32 usage_id,
> +				u32 attr_usage_id,
> +				struct hid_sensor_hub_attribute_info *info)
> +{
> +	int ret = -1;
> +	int i, j;
> +	int collection_index = -1;
> +	struct hid_report *report;
> +	struct hid_field *field;
> +	struct hid_report_enum *report_enum;
> +	struct hid_device *hdev = hsdev->hdev;
> +
> +	/* Initialize with defaults */
> +	info->usage_id = usage_id;
> +	info->attrib_id =  attr_usage_id;
> +	info->report_id = -1;
> +	info->index = -1;
> +	info->units = -1;
> +	info->unit_expo = -1;
> +
> +	for (i = 0; i < hdev->maxcollection; ++i) {
> +		struct hid_collection *collection = &hdev->collection[i];
> +		if (usage_id == collection->usage) {
> +			collection_index = i;
> +			break;
> +		}
> +	}
> +	if (collection_index == -1)
> +		goto err_ret;
> +
> +	report_enum = &hdev->report_enum[type];
> +	list_for_each_entry(report, &report_enum->report_list, list) {
> +		for (i = 0; i < report->maxfield; ++i) {
> +			field = report->field[i];
> +			if (field->physical == usage_id &&
> +				field->logical == attr_usage_id) {
> +				info->index = i;
> +				info->report_id = report->id;
> +				info->units = field->unit;
> +				info->unit_expo = field->unit_exponent;
> +				info->size = field->report_size/8;
> +				ret = 0;
> +			} else {
> +				for (j = 0; j < field->maxusage; ++j) {
> +					if (field->usage[j].hid ==
> +					attr_usage_id &&
> +					field->usage[j].collection_index ==
> +					collection_index)  {
> +						info->index = i;
> +						info->report_id = report->id;
> +						info->units = field->unit;
> +						info->unit_expo =
> +							field->unit_exponent;
> +						info->size =
> +							field->report_size/8;
> +						ret = 0;

This looks ugly. Filling of the info structure is the same in both 
branches, so how about just factoring it out to something like 
fill_info_struct() and use it in both this block and the block above?

> +						break;
> +					}
> +				}
> +			}
> +			if (ret == 0)
> +				break;
> +		}
> +	}
> +
> +err_ret:
> +	return ret;
> +}
> +EXPORT_SYMBOL_GPL(sensor_hub_input_get_attribute_info);
> +
> +#ifdef CONFIG_PM
> +static int sensor_hub_suspend(struct hid_device *hdev, pm_message_t message)
> +{
> +	struct sensor_hub_data *pdata =  hid_get_drvdata(hdev);
> +	struct hid_sensor_hub_callbacks_list *callback;
> +
> +	hid_dbg(hdev, " sensor_hub_suspend\n");
> +	spin_lock(&pdata->dyn_lock);
> +	list_for_each_entry(callback, &pdata->dyn_callback_list, list) {
> +		if (callback->usage_callback->suspend)
> +			callback->usage_callback->suspend(
> +					pdata->hsdev, callback->priv);
> +	}
> +	spin_unlock(&pdata->dyn_lock);
> +
> +	return 0;
> +}
> +
> +static int sensor_hub_resume(struct hid_device *hdev)
> +{
> +	struct sensor_hub_data *pdata =  hid_get_drvdata(hdev);
> +	struct hid_sensor_hub_callbacks_list *callback;
> +
> +	hid_dbg(hdev, " sensor_hub_resume\n");
> +	spin_lock(&pdata->dyn_lock);
> +	list_for_each_entry(callback, &pdata->dyn_callback_list, list) {
> +		if (callback->usage_callback->resume)
> +			callback->usage_callback->resume(
> +					pdata->hsdev, callback->priv);
> +	}
> +	spin_unlock(&pdata->dyn_lock);
> +
> +	return 0;
> +}
> +
> +static int sensor_hub_reset_resume(struct hid_device *hdev)
> +{
> +	return 0;
> +}
> +#endif
> +/*
> + * Handle raw report as sent by device
> + */
> +static int sensor_hub_raw_event(struct hid_device *hdev,
> +		struct hid_report *report, u8 *raw_data, int size)
> +{
> +	int i;
> +	u8 *ptr;
> +	int sz;
> +	struct sensor_hub_data *pdata = hid_get_drvdata(hdev);
> +	unsigned long flags;
> +	struct hid_sensor_hub_callbacks *callback = NULL;
> +	struct hid_collection *collection = NULL;
> +	void *priv = NULL;
> +
> +	hid_dbg(hdev, "sensor_hub_raw_event report id:0x%x size:%d type:%d\n",
> +			 report->id, size, report->type);
> +	hid_dbg(hdev, "maxfield:%d\n", report->maxfield);
> +	if (report->type != HID_INPUT_REPORT)
> +		return 1;
> +
> +	ptr = raw_data;
> +	ptr++; /*Skip report id*/
> +
> +	if (!report)
> +		goto err_report;
> +
> +	spin_lock_irqsave(&pdata->lock, flags);
> +
> +	for (i = 0; i < report->maxfield; ++i) {
> +
> +		hid_dbg(hdev, "%d collection_index:%x hid:%x sz:%x\n",
> +				i, report->field[i]->usage->collection_index,
> +				report->field[i]->usage->hid,
> +				report->field[i]->report_size/8);
> +
> +		sz = report->field[i]->report_size/8;
> +		if (pdata->pending.status && pdata->pending.attr_usage_id ==
> +				report->field[i]->usage->hid) {
> +			hid_dbg(hdev, "data was pending ...\n");
> +			pdata->pending.raw_data = kmalloc(sz, GFP_KERNEL);
> +			if (pdata->pending.raw_data) {
> +				memcpy(pdata->pending.raw_data, ptr, sz);
> +				pdata->pending.raw_size  = sz;
> +			} else
> +				pdata->pending.raw_size = 0;
> +			complete(&pdata->pending.ready);
> +		}
> +		collection = &hdev->collection[
> +				report->field[i]->usage->collection_index];
> +		hid_dbg(hdev, "collection->usage %x\n",
> +					collection->usage);
> +		callback = sensor_hub_get_callback(pdata->hsdev->hdev,
> +						report->field[i]->physical,
> +							&priv);
> +		if (callback && callback->capture_sample) {
> +			if (report->field[i]->logical)
> +				callback->capture_sample(pdata->hsdev,
> +					report->field[i]->logical, sz, ptr,
> +					callback->pdev);
> +			else
> +				callback->capture_sample(pdata->hsdev,
> +					report->field[i]->usage->hid, sz, ptr,
> +					callback->pdev);
> +		}
> +		ptr += sz;
> +	}
> +	if (callback && collection && callback->send_event)
> +		callback->send_event(pdata->hsdev, collection->usage,
> +				callback->pdev);
> +	spin_unlock_irqrestore(&pdata->lock, flags);
> +
> +err_report:
> +	return 1;
> +}
> +
> +static int sensor_hub_probe(struct hid_device *hdev,
> +				const struct hid_device_id *id)
> +{
> +	int ret;
> +	struct sensor_hub_data *sd;
> +	int i;
> +	char *name;
> +	struct hid_report *report;
> +	struct hid_report_enum *report_enum;
> +	struct hid_field *field;
> +	int dev_cnt;
> +
> +	sd = kzalloc(sizeof(struct sensor_hub_data), GFP_KERNEL);
> +	if (!sd) {
> +		hid_err(hdev, "cannot allocate Sensor data\n");
> +		return -ENOMEM;
> +	}
> +	sd->hsdev = kzalloc(sizeof(struct hid_sensor_hub_device), GFP_KERNEL);
> +	if (!sd->hsdev) {
> +		hid_err(hdev, "cannot allocate hid_sensor_hub_device\n");
> +		ret = -ENOMEM;
> +		goto err_free_hub;
> +	}
> +	hid_set_drvdata(hdev, sd);
> +	sd->hsdev->hdev = hdev;
> +	sd->hsdev->vendor_id = hdev->vendor;
> +	sd->hsdev->product_id = hdev->product;
> +	spin_lock_init(&sd->lock);
> +	spin_lock_init(&sd->dyn_lock);
> +	mutex_init(&sd->mutex);
> +	ret = hid_parse(hdev);
> +	if (ret) {
> +		hid_err(hdev, "parse failed\n");
> +		goto err_free;
> +	}
> +	if (sensor_hub_check_for_sensor_page(hdev) < 0) {
> +		hid_err(hdev, "sensor page not found\n");
> +		goto err_free;
> +	}
> +	INIT_LIST_HEAD(&hdev->inputs);
> +
> +	hdev->claimed = HID_CLAIMED_INPUT;
> +	ret = hid_hw_start(hdev, 0);
> +	if (ret) {
> +		hid_err(hdev, "hw start failed\n");
> +		goto err_free;
> +	}
> +	ret = hid_hw_open(hdev);
> +	if (ret) {
> +		hid_err(hdev, "failed to open input interrupt pipe\n");
> +		goto err_stop_hw;
> +	}
> +
> +	INIT_LIST_HEAD(&sd->dyn_callback_list);
> +	sd->hid_sensor_client_cnt = 0;
> +	report_enum = &hdev->report_enum[HID_INPUT_REPORT];
> +
> +	dev_cnt = sensor_hub_get_physical_device_count(report_enum);
> +	sd->hid_sensor_hub_client_devs = kzalloc(dev_cnt *
> +						sizeof(struct mfd_cell),
> +						GFP_KERNEL);

In theory there is nothing preventing buggy device, which will cause very 
large dev_cnt, to cause overflow here and cause kernel memory corruption, 
right?

So I'd like to see some boundary check on dev_cnt.

These all are rather minor comments, overall it looks fine to me. Once the 
feedback above is handled/discussed, I will add my Signed-off-by and you 
could push it through Greg then.

Thanks for all the work!

-- 
Jiri Kosina
SUSE Labs

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH 4/9] HID-Sensors: Sensor framework
  2012-08-27 15:49 [PATCH 0/9] HID-Sensor v8 srinivas pandruvada
@ 2012-08-27 15:49 ` srinivas pandruvada
  2012-09-05  9:40   ` Jiri Kosina
  0 siblings, 1 reply; 7+ messages in thread
From: srinivas pandruvada @ 2012-08-27 15:49 UTC (permalink / raw)
  To: linux-iio; +Cc: jic23, jkosina, srinivas pandruvada

Adding processing for HID Sensor usage table as defined by
HID 1.12, Request #: HUTRR39, dated 05 May, 2011.
This driver uses HID driver framework to register, send and
receive events.
This uses MFD framework, so that actual processing for a
specific usage id can be done in a different driver. For
example an accelerometer driver can be a separate driver and
use the interface provided by this driver to register for
events.

Signed-off-by: srinivas pandruvada <srinivas.pandruvada@intel.com>
---
 drivers/hid/Kconfig            |   14 +
 drivers/hid/Makefile           |    1 +
 drivers/hid/hid-sensor-hub.c   |  682 ++++++++++++++++++++++++++++++++++++++++
 include/linux/hid-sensor-hub.h |  160 ++++++++++
 include/linux/hid-sensor-ids.h |  111 +++++++
 5 files changed, 968 insertions(+), 0 deletions(-)
 create mode 100644 drivers/hid/hid-sensor-hub.c
 create mode 100644 include/linux/hid-sensor-hub.h
 create mode 100644 include/linux/hid-sensor-ids.h

diff --git a/drivers/hid/Kconfig b/drivers/hid/Kconfig
index 034c80a..3138c26 100644
--- a/drivers/hid/Kconfig
+++ b/drivers/hid/Kconfig
@@ -660,6 +660,20 @@ config HID_ZYDACRON
 	---help---
 	Support for Zydacron remote control.
 
+config HID_SENSOR_HUB
+	tristate "HID Sensors framework support"
+	depends on USB_HID
+	select MFD_CORE
+	default n
+	-- help---
+	  Support for HID Sensor framework. This creates a MFD instance
+	  for a sensor hub and identifies all the sensors connected to it.
+	  Each sensor is registered as a MFD cell, so that sensor specific
+	  processing can be done in a separate driver. Each sensor
+	  drivers can use the service provided by this driver to register
+	  for events and handle data streams. Each sensor driver can format
+	  data and present to user mode using input or IIO interface.
+
 endmenu
 
 endif # HID_SUPPORT
diff --git a/drivers/hid/Makefile b/drivers/hid/Makefile
index ca6cc9f..08f9d8f 100644
--- a/drivers/hid/Makefile
+++ b/drivers/hid/Makefile
@@ -87,6 +87,7 @@ obj-$(CONFIG_HID_ZYDACRON)	+= hid-zydacron.o
 obj-$(CONFIG_HID_WACOM)		+= hid-wacom.o
 obj-$(CONFIG_HID_WALTOP)	+= hid-waltop.o
 obj-$(CONFIG_HID_WIIMOTE)	+= hid-wiimote.o
+obj-$(CONFIG_HID_SENSOR_HUB)	+= hid-sensor-hub.o
 
 obj-$(CONFIG_USB_HID)		+= usbhid/
 obj-$(CONFIG_USB_MOUSE)		+= usbhid/
diff --git a/drivers/hid/hid-sensor-hub.c b/drivers/hid/hid-sensor-hub.c
new file mode 100644
index 0000000..60e78fb
--- /dev/null
+++ b/drivers/hid/hid-sensor-hub.c
@@ -0,0 +1,682 @@
+/*
+ * HID Sensors Driver
+ * Copyright (c) 2012, Intel Corporation.
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms and conditions of the GNU General Public License,
+ * version 2, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope 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 St - Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ */
+#include <linux/device.h>
+#include <linux/hid.h>
+#include <linux/usb.h>
+#include "usbhid/usbhid.h"
+#include <linux/module.h>
+#include <linux/slab.h>
+#include <linux/mfd/core.h>
+#include <linux/list.h>
+#include <linux/hid-sensor-ids.h>
+#include <linux/hid-sensor-hub.h>
+#include "hid-ids.h"
+
+/**
+ * struct sensor_hub_pending - Synchronous read pending information
+ * @status:		Pending status true/false.
+ * @ready:		Completion synchronization data.
+ * @usage_id:		Usage id for physical device, E.g. Gyro usage id.
+ * @attr_usage_id:	Usage Id of a field, E.g. X-AXIS for a gyro.
+ * @raw_size:		Response size for a read request.
+ * @raw_data:		Place holder for received response.
+ */
+struct sensor_hub_pending {
+	bool status;
+	struct completion ready;
+	u32 usage_id;
+	u32 attr_usage_id;
+	int raw_size;
+	u8  *raw_data;
+};
+
+/**
+ * struct sensor_hub_data - Hold a instance data for a HID hub device
+ * @hsdev:		Stored hid instance for current hub device.
+ * @mutex:		Mutex to serialize synchronous request.
+ * @lock:		Spin lock to protect pending request structure.
+ * @pending:		Holds information of pending sync read request.
+ * @dyn_callback_list:	Holds callback function
+ * @dyn_lock:		spin lock to prevent callback list
+ * @hid_sensor_hub_client_devs:	Stores all MFD cells for a hub instance.
+ * @hid_sensor_client_cnt: Number of MFD cells, (no of sensors attached).
+ */
+struct sensor_hub_data {
+	struct hid_sensor_hub_device *hsdev;
+	struct mutex mutex;
+	spinlock_t lock;
+	struct sensor_hub_pending pending;
+	struct list_head dyn_callback_list;
+	spinlock_t dyn_lock;
+	struct mfd_cell *hid_sensor_hub_client_devs;
+	int hid_sensor_client_cnt;
+};
+
+/**
+ * struct hid_sensor_hub_callbacks_list - Stores callback list
+ * @list:		list head.
+ * @usage_id:		usage id for a physical device.
+ * @usage_callback:	Stores registered callback functions.
+ * @priv:		Private data for a physical device.
+ */
+struct hid_sensor_hub_callbacks_list {
+	struct list_head list;
+	u32 usage_id;
+	struct hid_sensor_hub_callbacks *usage_callback;
+	void *priv;
+};
+
+static int sensor_hub_check_for_sensor_page(struct hid_device *hdev)
+{
+	int i;
+	int ret = -EINVAL;
+
+	for (i = 0; i < hdev->maxcollection; i++) {
+		struct hid_collection *col = &hdev->collection[i];
+		if (col->type == HID_COLLECTION_PHYSICAL &&
+		   (col->usage & HID_USAGE_PAGE) == HID_UP_SENSOR) {
+			ret = 0;
+			break;
+		}
+	}
+
+	return ret;
+}
+
+static struct hid_report *sensor_hub_report(int id, struct hid_device *hdev,
+						int dir)
+{
+	struct hid_report *report;
+
+	list_for_each_entry(report, &hdev->report_enum[dir].report_list, list) {
+		if (report->id == id)
+			return report;
+	}
+	hid_warn(hdev, "No report with id 0x%x found\n", id);
+
+	return NULL;
+}
+
+static int sensor_hub_get_physical_device_count(
+				struct hid_report_enum *report_enum)
+{
+	struct hid_report *report;
+	struct hid_field *field;
+	int cnt = 0;
+
+	list_for_each_entry(report, &report_enum->report_list, list) {
+		field = report->field[0];
+		if (report->maxfield && field &&
+					field->physical)
+			cnt++;
+	}
+
+	return cnt;
+}
+
+static struct hid_sensor_hub_callbacks *sensor_hub_get_callback(
+					struct hid_device *hdev,
+					u32 usage_id, void **priv)
+{
+	struct hid_sensor_hub_callbacks_list *callback;
+	struct sensor_hub_data *pdata = hid_get_drvdata(hdev);
+
+	spin_lock(&pdata->dyn_lock);
+	list_for_each_entry(callback, &pdata->dyn_callback_list, list)
+		if (callback->usage_id == usage_id) {
+			*priv = callback->priv;
+			spin_unlock(&pdata->dyn_lock);
+			return callback->usage_callback;
+		}
+	spin_unlock(&pdata->dyn_lock);
+
+	return NULL;
+}
+
+int sensor_hub_register_callback(struct hid_sensor_hub_device *hsdev,
+			u32 usage_id,
+			struct hid_sensor_hub_callbacks *usage_callback)
+{
+	struct hid_sensor_hub_callbacks_list *callback;
+	struct sensor_hub_data *pdata = hid_get_drvdata(hsdev->hdev);
+
+	spin_lock(&pdata->dyn_lock);
+	list_for_each_entry(callback, &pdata->dyn_callback_list, list)
+		if (callback->usage_id == usage_id) {
+			spin_unlock(&pdata->dyn_lock);
+			return -EINVAL;
+		}
+	callback = kzalloc(sizeof(*callback), GFP_KERNEL);
+	if (!callback) {
+		spin_unlock(&pdata->dyn_lock);
+		return -ENOMEM;
+	}
+	callback->usage_callback = usage_callback;
+	callback->usage_id = usage_id;
+	callback->priv = NULL;
+	list_add_tail(&callback->list, &pdata->dyn_callback_list);
+	spin_unlock(&pdata->dyn_lock);
+
+	return 0;
+}
+EXPORT_SYMBOL_GPL(sensor_hub_register_callback);
+
+int sensor_hub_remove_callback(struct hid_sensor_hub_device *hsdev,
+				u32 usage_id)
+{
+	struct hid_sensor_hub_callbacks_list *callback;
+	struct sensor_hub_data *pdata = hid_get_drvdata(hsdev->hdev);
+
+	spin_lock(&pdata->dyn_lock);
+	list_for_each_entry(callback, &pdata->dyn_callback_list, list)
+		if (callback->usage_id == usage_id) {
+			list_del(&callback->list);
+			kfree(callback);
+			break;
+		}
+	spin_unlock(&pdata->dyn_lock);
+
+	return 0;
+}
+EXPORT_SYMBOL_GPL(sensor_hub_remove_callback);
+
+int sensor_hub_set_feature(struct hid_sensor_hub_device *hsdev, u32 report_id,
+				u32 field_index, s32 value)
+{
+	struct hid_report *report;
+	struct sensor_hub_data *data =  hid_get_drvdata(hsdev->hdev);
+	int ret = 0;
+
+	if (report_id < 0)
+		return -EINVAL;
+
+	mutex_lock(&data->mutex);
+	report = sensor_hub_report(report_id, hsdev->hdev, HID_FEATURE_REPORT);
+	if (!report || (field_index >=  report->maxfield)) {
+		ret = -EINVAL;
+		goto done_proc;
+	}
+	hid_set_field(report->field[field_index], 0, value);
+	usbhid_submit_report(hsdev->hdev, report, USB_DIR_OUT);
+	usbhid_wait_io(hsdev->hdev);
+
+done_proc:
+	mutex_unlock(&data->mutex);
+
+	return ret;
+}
+EXPORT_SYMBOL_GPL(sensor_hub_set_feature);
+
+int sensor_hub_get_feature(struct hid_sensor_hub_device *hsdev, u32 report_id,
+				u32 field_index, s32 *value)
+{
+	struct hid_report *report;
+	struct sensor_hub_data *data =  hid_get_drvdata(hsdev->hdev);
+	int ret = 0;
+
+	if (report_id < 0)
+		return -EINVAL;
+
+	mutex_lock(&data->mutex);
+	report = sensor_hub_report(report_id, hsdev->hdev, HID_FEATURE_REPORT);
+	if (!report || (field_index >=  report->maxfield)) {
+		ret = -EINVAL;
+		goto done_proc;
+	}
+	usbhid_submit_report(hsdev->hdev, report, USB_DIR_IN);
+	usbhid_wait_io(hsdev->hdev);
+	*value = report->field[field_index]->value[0];
+
+done_proc:
+	mutex_unlock(&data->mutex);
+
+	return ret;
+}
+EXPORT_SYMBOL_GPL(sensor_hub_get_feature);
+
+
+int sensor_hub_input_attr_get_raw_value(struct hid_sensor_hub_device *hsdev,
+					u32 usage_id,
+					u32 attr_usage_id, u32 report_id)
+{
+	struct sensor_hub_data *data =  hid_get_drvdata(hsdev->hdev);
+	unsigned long flags;
+	struct hid_report *report;
+	int ret_val = 0;
+
+	if (report_id < 0)
+		return -EINVAL;
+
+	mutex_lock(&data->mutex);
+	memset(&data->pending, 0, sizeof(data->pending));
+	init_completion(&data->pending.ready);
+	data->pending.usage_id = usage_id;
+	data->pending.attr_usage_id = attr_usage_id;
+	data->pending.raw_size = 0;
+
+	spin_lock_irqsave(&data->lock, flags);
+	data->pending.status = true;
+	report = sensor_hub_report(report_id, hsdev->hdev, HID_INPUT_REPORT);
+	if (!report) {
+		spin_unlock_irqrestore(&data->lock, flags);
+		goto err_free;
+	}
+	usbhid_submit_report(hsdev->hdev, report, USB_DIR_IN);
+	spin_unlock_irqrestore(&data->lock, flags);
+	wait_for_completion_interruptible_timeout(&data->pending.ready, HZ*5);
+	switch (data->pending.raw_size) {
+	case 1:
+		ret_val = *(u8 *)data->pending.raw_data;
+		break;
+	case 2:
+		ret_val = *(u16 *)data->pending.raw_data;
+		break;
+	case 4:
+		ret_val = *(u32 *)data->pending.raw_data;
+		break;
+	default:
+		ret_val = 0;
+	}
+	kfree(data->pending.raw_data);
+
+err_free:
+	data->pending.status = false;
+	mutex_unlock(&data->mutex);
+
+	return ret_val;
+}
+EXPORT_SYMBOL_GPL(sensor_hub_input_attr_get_raw_value);
+
+int sensor_hub_input_get_attribute_info(struct hid_sensor_hub_device *hsdev,
+				u8 type,
+				u32 usage_id,
+				u32 attr_usage_id,
+				struct hid_sensor_hub_attribute_info *info)
+{
+	int ret = -1;
+	int i, j;
+	int collection_index = -1;
+	struct hid_report *report;
+	struct hid_field *field;
+	struct hid_report_enum *report_enum;
+	struct hid_device *hdev = hsdev->hdev;
+
+	/* Initialize with defaults */
+	info->usage_id = usage_id;
+	info->attrib_id =  attr_usage_id;
+	info->report_id = -1;
+	info->index = -1;
+	info->units = -1;
+	info->unit_expo = -1;
+
+	for (i = 0; i < hdev->maxcollection; ++i) {
+		struct hid_collection *collection = &hdev->collection[i];
+		if (usage_id == collection->usage) {
+			collection_index = i;
+			break;
+		}
+	}
+	if (collection_index == -1)
+		goto err_ret;
+
+	report_enum = &hdev->report_enum[type];
+	list_for_each_entry(report, &report_enum->report_list, list) {
+		for (i = 0; i < report->maxfield; ++i) {
+			field = report->field[i];
+			if (field->physical == usage_id &&
+				field->logical == attr_usage_id) {
+				info->index = i;
+				info->report_id = report->id;
+				info->units = field->unit;
+				info->unit_expo = field->unit_exponent;
+				info->size = field->report_size/8;
+				ret = 0;
+			} else {
+				for (j = 0; j < field->maxusage; ++j) {
+					if (field->usage[j].hid ==
+					attr_usage_id &&
+					field->usage[j].collection_index ==
+					collection_index)  {
+						info->index = i;
+						info->report_id = report->id;
+						info->units = field->unit;
+						info->unit_expo =
+							field->unit_exponent;
+						info->size =
+							field->report_size/8;
+						ret = 0;
+						break;
+					}
+				}
+			}
+			if (ret == 0)
+				break;
+		}
+	}
+
+err_ret:
+	return ret;
+}
+EXPORT_SYMBOL_GPL(sensor_hub_input_get_attribute_info);
+
+#ifdef CONFIG_PM
+static int sensor_hub_suspend(struct hid_device *hdev, pm_message_t message)
+{
+	struct sensor_hub_data *pdata =  hid_get_drvdata(hdev);
+	struct hid_sensor_hub_callbacks_list *callback;
+
+	hid_dbg(hdev, " sensor_hub_suspend\n");
+	spin_lock(&pdata->dyn_lock);
+	list_for_each_entry(callback, &pdata->dyn_callback_list, list) {
+		if (callback->usage_callback->suspend)
+			callback->usage_callback->suspend(
+					pdata->hsdev, callback->priv);
+	}
+	spin_unlock(&pdata->dyn_lock);
+
+	return 0;
+}
+
+static int sensor_hub_resume(struct hid_device *hdev)
+{
+	struct sensor_hub_data *pdata =  hid_get_drvdata(hdev);
+	struct hid_sensor_hub_callbacks_list *callback;
+
+	hid_dbg(hdev, " sensor_hub_resume\n");
+	spin_lock(&pdata->dyn_lock);
+	list_for_each_entry(callback, &pdata->dyn_callback_list, list) {
+		if (callback->usage_callback->resume)
+			callback->usage_callback->resume(
+					pdata->hsdev, callback->priv);
+	}
+	spin_unlock(&pdata->dyn_lock);
+
+	return 0;
+}
+
+static int sensor_hub_reset_resume(struct hid_device *hdev)
+{
+	return 0;
+}
+#endif
+/*
+ * Handle raw report as sent by device
+ */
+static int sensor_hub_raw_event(struct hid_device *hdev,
+		struct hid_report *report, u8 *raw_data, int size)
+{
+	int i;
+	u8 *ptr;
+	int sz;
+	struct sensor_hub_data *pdata = hid_get_drvdata(hdev);
+	unsigned long flags;
+	struct hid_sensor_hub_callbacks *callback = NULL;
+	struct hid_collection *collection = NULL;
+	void *priv = NULL;
+
+	hid_dbg(hdev, "sensor_hub_raw_event report id:0x%x size:%d type:%d\n",
+			 report->id, size, report->type);
+	hid_dbg(hdev, "maxfield:%d\n", report->maxfield);
+	if (report->type != HID_INPUT_REPORT)
+		return 1;
+
+	ptr = raw_data;
+	ptr++; /*Skip report id*/
+
+	if (!report)
+		goto err_report;
+
+	spin_lock_irqsave(&pdata->lock, flags);
+
+	for (i = 0; i < report->maxfield; ++i) {
+
+		hid_dbg(hdev, "%d collection_index:%x hid:%x sz:%x\n",
+				i, report->field[i]->usage->collection_index,
+				report->field[i]->usage->hid,
+				report->field[i]->report_size/8);
+
+		sz = report->field[i]->report_size/8;
+		if (pdata->pending.status && pdata->pending.attr_usage_id ==
+				report->field[i]->usage->hid) {
+			hid_dbg(hdev, "data was pending ...\n");
+			pdata->pending.raw_data = kmalloc(sz, GFP_KERNEL);
+			if (pdata->pending.raw_data) {
+				memcpy(pdata->pending.raw_data, ptr, sz);
+				pdata->pending.raw_size  = sz;
+			} else
+				pdata->pending.raw_size = 0;
+			complete(&pdata->pending.ready);
+		}
+		collection = &hdev->collection[
+				report->field[i]->usage->collection_index];
+		hid_dbg(hdev, "collection->usage %x\n",
+					collection->usage);
+		callback = sensor_hub_get_callback(pdata->hsdev->hdev,
+						report->field[i]->physical,
+							&priv);
+		if (callback && callback->capture_sample) {
+			if (report->field[i]->logical)
+				callback->capture_sample(pdata->hsdev,
+					report->field[i]->logical, sz, ptr,
+					callback->pdev);
+			else
+				callback->capture_sample(pdata->hsdev,
+					report->field[i]->usage->hid, sz, ptr,
+					callback->pdev);
+		}
+		ptr += sz;
+	}
+	if (callback && collection && callback->send_event)
+		callback->send_event(pdata->hsdev, collection->usage,
+				callback->pdev);
+	spin_unlock_irqrestore(&pdata->lock, flags);
+
+err_report:
+	return 1;
+}
+
+static int sensor_hub_probe(struct hid_device *hdev,
+				const struct hid_device_id *id)
+{
+	int ret;
+	struct sensor_hub_data *sd;
+	int i;
+	char *name;
+	struct hid_report *report;
+	struct hid_report_enum *report_enum;
+	struct hid_field *field;
+	int dev_cnt;
+
+	sd = kzalloc(sizeof(struct sensor_hub_data), GFP_KERNEL);
+	if (!sd) {
+		hid_err(hdev, "cannot allocate Sensor data\n");
+		return -ENOMEM;
+	}
+	sd->hsdev = kzalloc(sizeof(struct hid_sensor_hub_device), GFP_KERNEL);
+	if (!sd->hsdev) {
+		hid_err(hdev, "cannot allocate hid_sensor_hub_device\n");
+		ret = -ENOMEM;
+		goto err_free_hub;
+	}
+	hid_set_drvdata(hdev, sd);
+	sd->hsdev->hdev = hdev;
+	sd->hsdev->vendor_id = hdev->vendor;
+	sd->hsdev->product_id = hdev->product;
+	spin_lock_init(&sd->lock);
+	spin_lock_init(&sd->dyn_lock);
+	mutex_init(&sd->mutex);
+	ret = hid_parse(hdev);
+	if (ret) {
+		hid_err(hdev, "parse failed\n");
+		goto err_free;
+	}
+	if (sensor_hub_check_for_sensor_page(hdev) < 0) {
+		hid_err(hdev, "sensor page not found\n");
+		goto err_free;
+	}
+	INIT_LIST_HEAD(&hdev->inputs);
+
+	hdev->claimed = HID_CLAIMED_INPUT;
+	ret = hid_hw_start(hdev, 0);
+	if (ret) {
+		hid_err(hdev, "hw start failed\n");
+		goto err_free;
+	}
+	ret = hid_hw_open(hdev);
+	if (ret) {
+		hid_err(hdev, "failed to open input interrupt pipe\n");
+		goto err_stop_hw;
+	}
+
+	INIT_LIST_HEAD(&sd->dyn_callback_list);
+	sd->hid_sensor_client_cnt = 0;
+	report_enum = &hdev->report_enum[HID_INPUT_REPORT];
+
+	dev_cnt = sensor_hub_get_physical_device_count(report_enum);
+	sd->hid_sensor_hub_client_devs = kzalloc(dev_cnt *
+						sizeof(struct mfd_cell),
+						GFP_KERNEL);
+	if (sd->hid_sensor_hub_client_devs == NULL) {
+		hid_err(hdev,
+			"Failed to allocate memory for mfd cells\n");
+			ret = -ENOMEM;
+			goto err_close;
+	}
+	list_for_each_entry(report, &report_enum->report_list, list) {
+		hid_dbg(hdev, "Report id:%x\n", report->id);
+		field = report->field[0];
+		if (report->maxfield && field &&
+					field->physical) {
+			name = kasprintf(GFP_KERNEL, "HID-SENSOR-%x",
+						field->physical);
+			if (name  == NULL) {
+				hid_err(hdev,
+					"Failed MFD device name\n");
+					ret = -ENOMEM;
+					goto err_free_cells;
+			}
+			sd->hid_sensor_hub_client_devs[
+				sd->hid_sensor_client_cnt].name = name;
+			sd->hid_sensor_hub_client_devs[
+				sd->hid_sensor_client_cnt].platform_data =
+						sd->hsdev;
+			sd->hid_sensor_hub_client_devs[
+				sd->hid_sensor_client_cnt].pdata_size =
+						sizeof(*sd->hsdev);
+			hid_dbg(hdev, "Adding %s:%p\n", name, sd);
+			sd->hid_sensor_client_cnt++;
+		}
+	}
+	ret = mfd_add_devices(&hdev->dev, 0, sd->hid_sensor_hub_client_devs,
+		sd->hid_sensor_client_cnt, NULL, 0);
+	if (ret < 0)
+		goto err_free_names;
+
+	return ret;
+
+err_free_names:
+	for (i = 0; i < sd->hid_sensor_client_cnt ; ++i)
+		kfree(sd->hid_sensor_hub_client_devs[i].name);
+err_free_cells:
+	kfree(sd->hid_sensor_hub_client_devs);
+err_close:
+	hid_hw_stop(hdev);
+	hid_hw_close(hdev);
+err_stop_hw:
+	hid_hw_stop(hdev);
+err_free:
+	kfree(sd->hsdev);
+err_free_hub:
+	kfree(sd);
+
+	return ret;
+}
+
+static void sensor_hub_remove(struct hid_device *hdev)
+{
+	struct sensor_hub_data *data = hid_get_drvdata(hdev);
+	unsigned long flags;
+	int i;
+
+	hid_dbg(hdev, " hardware removed\n");
+	hdev->claimed &= ~HID_CLAIMED_INPUT;
+	hid_hw_stop(hdev);
+	hid_hw_close(hdev);
+	spin_lock_irqsave(&data->lock, flags);
+	if (data->pending.status)
+		complete(&data->pending.ready);
+	spin_unlock_irqrestore(&data->lock, flags);
+	mfd_remove_devices(&hdev->dev);
+	for (i = 0; i < data->hid_sensor_client_cnt ; ++i)
+		kfree(data->hid_sensor_hub_client_devs[i].name);
+	kfree(data->hid_sensor_hub_client_devs);
+	hid_set_drvdata(hdev, NULL);
+	mutex_destroy(&data->mutex);
+	kfree(data->hsdev);
+	kfree(data);
+}
+
+static const struct hid_device_id sensor_hub_devices[] = {
+	{ HID_USB_DEVICE(USB_VENDOR_ID_INTEL_8086,
+			USB_DEVICE_ID_SENSOR_HUB_1020) },
+	{ HID_USB_DEVICE(USB_VENDOR_ID_INTEL_8087,
+			USB_DEVICE_ID_SENSOR_HUB_1020) },
+	{ HID_USB_DEVICE(USB_VENDOR_ID_INTEL_8086,
+			USB_DEVICE_ID_SENSOR_HUB_09FA) },
+	{ HID_USB_DEVICE(USB_VENDOR_ID_INTEL_8087,
+			USB_DEVICE_ID_SENSOR_HUB_09FA) },
+	{ HID_USB_DEVICE(USB_VENDOR_ID_STANTUM_STM,
+			USB_DEVICE_ID_SENSOR_HUB_7014) },
+	{ }
+};
+
+static const struct hid_usage_id sensor_hub_grabbed_usages[] = {
+	{ HID_ANY_ID, HID_ANY_ID, HID_ANY_ID },
+	{ HID_ANY_ID - 1, HID_ANY_ID - 1, HID_ANY_ID - 1 }
+};
+
+static struct hid_driver sensor_hub_driver = {
+	.name = "hid-sensor-hub",
+	.id_table = sensor_hub_devices,
+	.probe = sensor_hub_probe,
+	.remove = sensor_hub_remove,
+	.raw_event = sensor_hub_raw_event,
+#ifdef CONFIG_PM
+	.suspend = sensor_hub_suspend,
+	.resume =  sensor_hub_resume,
+	.reset_resume =  sensor_hub_reset_resume,
+#endif
+};
+
+static int __init sensor_hub_init(void)
+{
+	return hid_register_driver(&sensor_hub_driver);
+}
+
+static void __exit sensor_hub_exit(void)
+{
+	hid_unregister_driver(&sensor_hub_driver);
+}
+
+module_init(sensor_hub_init);
+module_exit(sensor_hub_exit);
+
+MODULE_DESCRIPTION("HID Sensor Hub driver");
+MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@intel.com>");
+MODULE_LICENSE("GPL");
diff --git a/include/linux/hid-sensor-hub.h b/include/linux/hid-sensor-hub.h
new file mode 100644
index 0000000..0aa5f4c
--- /dev/null
+++ b/include/linux/hid-sensor-hub.h
@@ -0,0 +1,160 @@
+/*
+ * HID Sensors Driver
+ * Copyright (c) 2012, Intel Corporation.
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms and conditions of the GNU General Public License,
+ * version 2, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope 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 St - Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ */
+#ifndef _HID_SENSORS_HUB_H
+#define _HID_SENSORS_HUB_H
+
+#include <linux/hid.h>
+#include <linux/hid-sensor-ids.h>
+
+/**
+ * struct hid_sensor_hub_attribute_info - Attribute info
+ * @usage_id:		Parent usage id of a physical device.
+ * @attrib_id:		Attribute id for this attribute.
+ * @report_id:		Report id in which this information resides.
+ * @index:		Field index in the report.
+ * @units:		Measurment unit for this attribute.
+ * @unit_expo:		Exponent used in the data.
+ * @size:		Size in bytes for data size.
+ */
+struct hid_sensor_hub_attribute_info {
+	u32 usage_id;
+	u32 attrib_id;
+	s32 report_id;
+	s32 index;
+	s32 units;
+	s32 unit_expo;
+	s32 size;
+};
+
+/**
+ * struct hid_sensor_hub_device - Stores the hub instance data
+ * @hdev:		Stores the hid instance.
+ * @vendor_id:		Vendor id of hub device.
+ * @product_id:		Product id of hub device.
+ */
+struct hid_sensor_hub_device {
+	struct hid_device *hdev;
+	u32 vendor_id;
+	u32 product_id;
+};
+
+/**
+ * struct hid_sensor_hub_callbacks - Client callback functions
+ * @pdev:		Platform device instance of the client driver.
+ * @suspend:		Suspend callback.
+ * @resume:		Resume callback.
+ * @capture_sample:	Callback to get a sample.
+ * @send_event:		Send notification to indicate all samples are
+ *			captured, process and send event
+ */
+struct hid_sensor_hub_callbacks {
+	struct platform_device *pdev;
+	int (*suspend)(struct hid_sensor_hub_device *hsdev, void *priv);
+	int (*resume)(struct hid_sensor_hub_device *hsdev, void *priv);
+	int (*capture_sample)(struct hid_sensor_hub_device *hsdev,
+			u32 usage_id, size_t raw_len, char *raw_data,
+			void *priv);
+	int (*send_event)(struct hid_sensor_hub_device *hsdev, u32 usage_id,
+			 void *priv);
+};
+
+/* Registration functions */
+
+/**
+* sensor_hub_register_callback() - Register client callbacks
+* @hsdev:	Hub device instance.
+* @usage_id:	Usage id of the client (E.g. 0x200076 for Gyro).
+* @usage_callback: Callback function storage
+*
+* Used to register callbacks by client processing drivers. Sensor
+* hub core driver will call these callbacks to offload processing
+* of data streams and notifications.
+*/
+int sensor_hub_register_callback(struct hid_sensor_hub_device *hsdev,
+			u32 usage_id,
+			struct hid_sensor_hub_callbacks *usage_callback);
+
+/**
+* sensor_hub_remove_callback() - Remove client callbacks
+* @hsdev:	Hub device instance.
+* @usage_id:	Usage id of the client (E.g. 0x200076 for Gyro).
+*
+* If there is a callback registred, this call will remove that
+* callbacks, so that it will stop data and event notifications.
+*/
+int sensor_hub_remove_callback(struct hid_sensor_hub_device *hsdev,
+			u32 usage_id);
+
+
+/* Hid sensor hub core interfaces */
+
+/**
+* sensor_hub_input_get_attribute_info() - Get an attribute information
+* @hsdev:	Hub device instance.
+* @type:	Type of this attribute, input/output/feature
+* @usage_id:	Attribute usage id of parent physical device as per spec
+* @attr_usage_id:	Attribute usage id as per spec
+* @info:	return information about attribute after parsing report
+*
+* Parses report and returns the attribute information such as report id,
+* field index, units and exponet etc.
+*/
+int sensor_hub_input_get_attribute_info(struct hid_sensor_hub_device *hsdev,
+			u8 type,
+			u32 usage_id, u32 attr_usage_id,
+			struct hid_sensor_hub_attribute_info *info);
+
+/**
+* sensor_hub_input_attr_get_raw_value() - Synchronous read request
+* @usage_id:	Attribute usage id of parent physical device as per spec
+* @attr_usage_id:	Attribute usage id as per spec
+* @report_id:	Report id to look for
+*
+* Issues a synchronous read request for an input attribute. Returns
+* data upto 32 bits. Since client can get events, so this call should
+* not be used for data paths, this will impact performance.
+*/
+
+int sensor_hub_input_attr_get_raw_value(struct hid_sensor_hub_device *hsdev,
+			u32 usage_id,
+			u32 attr_usage_id, u32 report_id);
+/**
+* sensor_hub_set_feature() - Feature set request
+* @report_id:	Report id to look for
+* @field_index:	Field index inside a report
+* @value:	Value to set
+*
+* Used to set a field in feature report. For example this can set polling
+* interval, sensitivity, activate/deactivate state.
+*/
+int sensor_hub_set_feature(struct hid_sensor_hub_device *hsdev, u32 report_id,
+			u32 field_index, s32 value);
+
+/**
+* sensor_hub_get_feature() - Feature get request
+* @report_id:	Report id to look for
+* @field_index:	Field index inside a report
+* @value:	Place holder for return value
+*
+* Used to get a field in feature report. For example this can get polling
+* interval, sensitivity, activate/deactivate state.
+*/
+int sensor_hub_get_feature(struct hid_sensor_hub_device *hsdev, u32 report_id,
+			u32 field_index, s32 *value);
+#endif
diff --git a/include/linux/hid-sensor-ids.h b/include/linux/hid-sensor-ids.h
new file mode 100644
index 0000000..292e80f
--- /dev/null
+++ b/include/linux/hid-sensor-ids.h
@@ -0,0 +1,111 @@
+/*
+ * HID Sensors Driver
+ * Copyright (c) 2012, Intel Corporation.
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms and conditions of the GNU General Public License,
+ * version 2, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope 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 St - Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ */
+#ifndef _HID_SENSORS_IDS_H
+#define _HID_SENSORS_IDS_H
+
+#define HID_UP_SENSOR						0x00200000
+
+/* Accel 3D (200073) */
+#define HID_USAGE_SENSOR_ACCEL_3D				0x200073
+#define HID_USAGE_SENSOR_ACCEL_X_AXIS				0x200453
+#define HID_USAGE_SENSOR_ACCEL_Y_AXIS				0x200454
+#define HID_USAGE_SENSOR_ACCEL_Z_AXIS				0x200455
+
+/* ALS (200041) */
+#define HID_USAGE_SENSOR_ALS					0x200041
+#define HID_USAGE_SENSOR_LIGHT_ILLUM				0x2004d1
+
+/* Gyro 3D: (200076) */
+#define HID_USAGE_SENSOR_GYRO_3D				0x200076
+#define HID_USAGE_SENSOR_ANGL_VELOCITY_X_AXIS			0x200457
+#define HID_USAGE_SENSOR_ANGL_VELOCITY_Y_AXIS			0x200458
+#define HID_USAGE_SENSOR_ANGL_VELOCITY_Z_AXIS			0x200459
+
+/*ORIENTATION: Compass 3D: (200083) */
+#define HID_USAGE_SENSOR_COMPASS_3D				0x200083
+#define HID_USAGE_SENSOR_ORIENT_MAGN_HEADING			0x200471
+#define HID_USAGE_SENSOR_ORIENT_MAGN_HEADING_X			0x200472
+#define HID_USAGE_SENSOR_ORIENT_MAGN_HEADING_Y			0x200473
+#define HID_USAGE_SENSOR_ORIENT_MAGN_HEADING_Z			0x200474
+
+#define HID_USAGE_SENSOR_ORIENT_COMP_MAGN_NORTH			0x200475
+#define HID_USAGE_SENSOR_ORIENT_COMP_TRUE_NORTH			0x200476
+#define HID_USAGE_SENSOR_ORIENT_MAGN_NORTH			0x200477
+#define HID_USAGE_SENSOR_ORIENT_TRUE_NORTH			0x200478
+
+#define HID_USAGE_SENSOR_ORIENT_DISTANCE			0x200479
+#define HID_USAGE_SENSOR_ORIENT_DISTANCE_X			0x20047A
+#define HID_USAGE_SENSOR_ORIENT_DISTANCE_Y			0x20047B
+#define HID_USAGE_SENSOR_ORIENT_DISTANCE_Z			0x20047C
+#define HID_USAGE_SENSOR_ORIENT_DISTANCE_OUT_OF_RANGE		0x20047D
+#define HID_USAGE_SENSOR_ORIENT_TILT				0x20047E
+#define HID_USAGE_SENSOR_ORIENT_TILT_X				0x20047F
+#define HID_USAGE_SENSOR_ORIENT_TILT_Y				0x200480
+#define HID_USAGE_SENSOR_ORIENT_TILT_Z				0x200481
+#define HID_USAGE_SENSOR_ORIENT_ROTATION_MATRIX			0x200482
+#define HID_USAGE_SENSOR_ORIENT_QUATERNION			0x200483
+#define HID_USAGE_SENSOR_ORIENT_MAGN_FLUX			0x200484
+
+#define HID_USAGE_SENSOR_ORIENT_MAGN_FLUX_X_AXIS		0x200485
+#define HID_USAGE_SENSOR_ORIENT_MAGN_FLUX_Y_AXIS		0x200486
+#define HID_USAGE_SENSOR_ORIENT_MAGN_FLUX_Z_AXIS		0x200487
+
+/* Units */
+#define HID_USAGE_SENSOR_UNITS_NOT_SPECIFIED			0x00
+#define HID_USAGE_SENSOR_UNITS_LUX				0x01
+#define HID_USAGE_SENSOR_UNITS_KELVIN				0x01000100
+#define HID_USAGE_SENSOR_UNITS_FAHRENHEIT			0x03000100
+#define HID_USAGE_SENSOR_UNITS_PASCAL				0xF1E1
+#define HID_USAGE_SENSOR_UNITS_NEWTON				0x11E1
+#define HID_USAGE_SENSOR_UNITS_METERS_PER_SECOND		0x11F0
+#define HID_USAGE_SENSOR_UNITS_METERS_PER_SEC_SQRD		0x11E0
+#define HID_USAGE_SENSOR_UNITS_FARAD				0xE14F2000
+#define HID_USAGE_SENSOR_UNITS_AMPERE				0x01001000
+#define HID_USAGE_SENSOR_UNITS_WATT				0x21d1
+#define HID_USAGE_SENSOR_UNITS_HENRY				0x21E1E000
+#define HID_USAGE_SENSOR_UNITS_OHM				0x21D1E000
+#define HID_USAGE_SENSOR_UNITS_VOLT				0x21D1F000
+#define HID_USAGE_SENSOR_UNITS_HERTZ				0x01F0
+#define HID_USAGE_SENSOR_UNITS_DEGREES_PER_SEC_SQRD		0x14E0
+#define HID_USAGE_SENSOR_UNITS_RADIANS				0x12
+#define HID_USAGE_SENSOR_UNITS_RADIANS_PER_SECOND		0x12F0
+#define HID_USAGE_SENSOR_UNITS_RADIANS_PER_SEC_SQRD		0x12E0
+#define HID_USAGE_SENSOR_UNITS_SECOND				0x0110
+#define HID_USAGE_SENSOR_UNITS_GAUSS				0x01E1F000
+#define HID_USAGE_SENSOR_UNITS_GRAM				0x0101
+#define HID_USAGE_SENSOR_UNITS_CENTIMETER			0x11
+#define HID_USAGE_SENSOR_UNITS_G				0x1A
+#define HID_USAGE_SENSOR_UNITS_MILLISECOND			0x19
+#define HID_USAGE_SENSOR_UNITS_PERCENT				0x17
+#define HID_USAGE_SENSOR_UNITS_DEGREES				0x14
+#define HID_USAGE_SENSOR_UNITS_DEGREES_PER_SECOND		0x15
+
+/* Common selectors */
+#define HID_USAGE_SENSOR_PROP_REPORT_INTERVAL			0x20030E
+#define HID_USAGE_SENSOR_PROP_SENSITIVITY_ABS			0x20030F
+#define HID_USAGE_SENSOR_PROP_SENSITIVITY_RANGE_PCT		0x200310
+#define HID_USAGE_SENSOR_PROP_SENSITIVITY_REL_PCT		0x200311
+#define HID_USAGE_SENSOR_PROP_ACCURACY				0x200312
+#define HID_USAGE_SENSOR_PROP_RESOLUTION			0x200313
+#define HID_USAGE_SENSOR_PROP_RANGE_MAXIMUM			0x200314
+#define HID_USAGE_SENSOR_PROP_RANGE_MINIMUM			0x200315
+#define HID_USAGE_SENSOR_PROP_REPORT_STATE			0x200316
+#define HID_USAGE_SENSOR_PROY_POWER_STATE			0x200319
+
+#endif
-- 
1.7.7.6


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH 4/9] HID-Sensors: Sensor framework
  2012-08-07 16:28 [PATCH 0/9] HID-Sensor v7 srinivas pandruvada
@ 2012-08-07 16:28 ` srinivas pandruvada
  0 siblings, 0 replies; 7+ messages in thread
From: srinivas pandruvada @ 2012-08-07 16:28 UTC (permalink / raw)
  To: linux-iio; +Cc: jic23, jkosina, srinivas pandruvada

Adding processing for HID Sensor usage table as defined by
HID 1.12, Request #: HUTRR39, dated 05 May, 2011.
This driver uses HID driver framework to register, send and
receive events.
This uses MFD framework, so that actual processing for a
specific usage id can be done in a different driver. For
example an accelerometer driver can be a separate driver and
use the interface provided by this driver to register for
events.

Signed-off-by: srinivas pandruvada <srinivas.pandruvada@intel.com>
---
 drivers/hid/Kconfig            |   14 +
 drivers/hid/Makefile           |    1 +
 drivers/hid/hid-sensor-hub.c   |  681 ++++++++++++++++++++++++++++++++++++++++
 include/linux/hid-sensor-hub.h |  160 ++++++++++
 include/linux/hid-sensor-ids.h |  111 +++++++
 5 files changed, 967 insertions(+), 0 deletions(-)
 create mode 100644 drivers/hid/hid-sensor-hub.c
 create mode 100644 include/linux/hid-sensor-hub.h
 create mode 100644 include/linux/hid-sensor-ids.h

diff --git a/drivers/hid/Kconfig b/drivers/hid/Kconfig
index bef04c1..a9fd774 100644
--- a/drivers/hid/Kconfig
+++ b/drivers/hid/Kconfig
@@ -653,6 +653,20 @@ config HID_ZYDACRON
 	---help---
 	Support for Zydacron remote control.
 
+config HID_SENSOR_HUB
+	tristate "HID Sensors framework support"
+	depends on USB_HID
+	select MFD_CORE
+	default n
+	-- help---
+	  Support for HID Sensor framework. This creates a MFD instance
+	  for a sensor hub and identifies all the sensors connected to it.
+	  Each sensor is registered as a MFD cell, so that sensor specific
+	  processing can be done in a separate driver. Each sensor
+	  drivers can use the service provided by this driver to register
+	  for events and handle data streams. Each sensor driver can format
+	  data and present to user mode using input or IIO interface.
+
 endmenu
 
 endif # HID
diff --git a/drivers/hid/Makefile b/drivers/hid/Makefile
index ca6cc9f..08f9d8f 100644
--- a/drivers/hid/Makefile
+++ b/drivers/hid/Makefile
@@ -87,6 +87,7 @@ obj-$(CONFIG_HID_ZYDACRON)	+= hid-zydacron.o
 obj-$(CONFIG_HID_WACOM)		+= hid-wacom.o
 obj-$(CONFIG_HID_WALTOP)	+= hid-waltop.o
 obj-$(CONFIG_HID_WIIMOTE)	+= hid-wiimote.o
+obj-$(CONFIG_HID_SENSOR_HUB)	+= hid-sensor-hub.o
 
 obj-$(CONFIG_USB_HID)		+= usbhid/
 obj-$(CONFIG_USB_MOUSE)		+= usbhid/
diff --git a/drivers/hid/hid-sensor-hub.c b/drivers/hid/hid-sensor-hub.c
new file mode 100644
index 0000000..6c9f0f8
--- /dev/null
+++ b/drivers/hid/hid-sensor-hub.c
@@ -0,0 +1,681 @@
+/*
+ * HID Sensors Driver
+ * Copyright (c) 2012, Intel Corporation.
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms and conditions of the GNU General Public License,
+ * version 2, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope 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 St - Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ */
+#include <linux/device.h>
+#include <linux/hid.h>
+#include <linux/usb.h>
+#include "usbhid/usbhid.h"
+#include <linux/module.h>
+#include <linux/slab.h>
+#include <linux/mfd/core.h>
+#include <linux/list.h>
+#include <linux/hid-sensor-ids.h>
+#include <linux/hid-sensor-hub.h>
+#include "hid-ids.h"
+
+/**
+ * struct sensor_hub_pending - Synchronous read pending information
+ * @status:		Pending status true/false.
+ * @ready:		Completion synchronization data.
+ * @usage_id:		Usage id for physical device, E.g. Gyro usage id.
+ * @attr_usage_id:	Usage Id of a field, E.g. X-AXIS for a gyro.
+ * @raw_size:		Response size for a read request.
+ * @raw_data:		Place holder for received response.
+ */
+struct sensor_hub_pending {
+	bool status;
+	struct completion ready;
+	u32 usage_id;
+	u32 attr_usage_id;
+	int raw_size;
+	u8  *raw_data;
+};
+
+/**
+ * struct sensor_hub_data - Hold a instance data for a HID hub device
+ * @hs_dev:		Stored hid instance for current hub device.
+ * @mutex:		Mutex to serialize synchronous request.
+ * @lock:		Spin lock to protect pending request structure.
+ * @pending:		Holds information of pending sync read request.
+ * @hid_sensor_hub_client_devs:	Stores all MFD cells for a hub instance.
+ * @hid_sensor_client_cnt: Number of MFD cells, (no of sensors attached).
+ */
+struct sensor_hub_data {
+	struct hid_sensor_hub_device *hsdev;
+	struct mutex mutex;
+	spinlock_t lock;
+	struct sensor_hub_pending pending;
+	struct list_head dyn_callback_list;
+	spinlock_t dyn_lock;
+	struct mfd_cell *hid_sensor_hub_client_devs;
+	int hid_sensor_client_cnt;
+};
+
+/**
+ * hid_sensor_hub_callbacks_list - Stores callback list
+ * @list:		list head.
+ * @usage_id:		usage id for a physical device.
+ * @usage_callbacks:	Stores registered callback functions.
+ * @priv:		Private data for a physical device.
+ */
+struct hid_sensor_hub_callbacks_list {
+	struct list_head list;
+	u32 usage_id;
+	struct hid_sensor_hub_callbacks *usage_callback;
+	void *priv;
+};
+
+static int sensor_hub_check_for_sensor_page(struct hid_device *hdev)
+{
+	int i;
+	int ret = -EINVAL;
+
+	for (i = 0; i < hdev->maxcollection; i++) {
+		struct hid_collection *col = &hdev->collection[i];
+		if (col->type == HID_COLLECTION_PHYSICAL &&
+		   (col->usage & HID_USAGE_PAGE) == HID_UP_SENSOR) {
+			ret = 0;
+			break;
+		}
+	}
+
+	return ret;
+}
+
+static struct hid_report *sensor_hub_report(int id, struct hid_device *hdev,
+						int dir)
+{
+	struct hid_report *report;
+
+	list_for_each_entry(report, &hdev->report_enum[dir].report_list, list) {
+		if (report->id == id)
+			return report;
+	}
+	hid_warn(hdev, "No report with id 0x%x found\n", id);
+
+	return NULL;
+}
+
+static int sensor_hub_get_physical_device_count(
+				struct hid_report_enum *report_enum)
+{
+	struct hid_report *report;
+	struct hid_field *field;
+	int cnt = 0;
+
+	list_for_each_entry(report, &report_enum->report_list, list) {
+		field = report->field[0];
+		if (report->maxfield && field &&
+					field->physical)
+			cnt++;
+	}
+
+	return cnt;
+}
+
+static struct hid_sensor_hub_callbacks *sensor_hub_get_callback(
+					struct hid_device *hdev,
+					u32 usage_id, void **priv)
+{
+	struct hid_sensor_hub_callbacks_list *callback;
+	struct sensor_hub_data *pdata = hid_get_drvdata(hdev);
+
+	spin_lock(&pdata->dyn_lock);
+	list_for_each_entry(callback, &pdata->dyn_callback_list, list)
+		if (callback->usage_id == usage_id) {
+			*priv = callback->priv;
+			spin_unlock(&pdata->dyn_lock);
+			return callback->usage_callback;
+		}
+	spin_unlock(&pdata->dyn_lock);
+
+	return NULL;
+}
+
+int sensor_hub_register_callback(struct hid_sensor_hub_device *hsdev,
+			u32 usage_id,
+			struct hid_sensor_hub_callbacks *usage_callback)
+{
+	struct hid_sensor_hub_callbacks_list *callback;
+	struct sensor_hub_data *pdata = hid_get_drvdata(hsdev->hdev);
+
+	spin_lock(&pdata->dyn_lock);
+	list_for_each_entry(callback, &pdata->dyn_callback_list, list)
+		if (callback->usage_id == usage_id) {
+			spin_unlock(&pdata->dyn_lock);
+			return -EINVAL;
+		}
+	callback = kzalloc(sizeof(*callback), GFP_KERNEL);
+	if (!callback) {
+		spin_unlock(&pdata->dyn_lock);
+		return -ENOMEM;
+	}
+	callback->usage_callback = usage_callback;
+	callback->usage_id = usage_id;
+	callback->priv = NULL;
+	list_add_tail(&callback->list, &pdata->dyn_callback_list);
+	spin_unlock(&pdata->dyn_lock);
+
+	return 0;
+}
+EXPORT_SYMBOL_GPL(sensor_hub_register_callback);
+
+int sensor_hub_remove_callback(struct hid_sensor_hub_device *hsdev,
+				u32 usage_id)
+{
+	struct hid_sensor_hub_callbacks_list *callback;
+	struct sensor_hub_data *pdata = hid_get_drvdata(hsdev->hdev);
+
+	spin_lock(&pdata->dyn_lock);
+	list_for_each_entry(callback, &pdata->dyn_callback_list, list)
+		if (callback->usage_id == usage_id) {
+			list_del(&callback->list);
+			kfree(callback);
+			break;
+		}
+	spin_unlock(&pdata->dyn_lock);
+
+	return 0;
+}
+EXPORT_SYMBOL_GPL(sensor_hub_remove_callback);
+
+int sensor_hub_set_feature(struct hid_sensor_hub_device *hsdev, u32 report_id,
+				u32 field_index, s32 value)
+{
+	struct hid_report *report;
+	struct sensor_hub_data *data =  hid_get_drvdata(hsdev->hdev);
+	int ret = 0;
+
+	if (report_id < 0)
+		return -EINVAL;
+
+	mutex_lock(&data->mutex);
+	report = sensor_hub_report(report_id, hsdev->hdev, HID_FEATURE_REPORT);
+	if (!report || (field_index >=  report->maxfield)) {
+		ret = -EINVAL;
+		goto done_proc;
+	}
+	hid_set_field(report->field[field_index], 0, value);
+	usbhid_submit_report(hsdev->hdev, report, USB_DIR_OUT);
+	usbhid_wait_io(hsdev->hdev);
+
+done_proc:
+	mutex_unlock(&data->mutex);
+
+	return ret;
+}
+EXPORT_SYMBOL_GPL(sensor_hub_set_feature);
+
+int sensor_hub_get_feature(struct hid_sensor_hub_device *hsdev, u32 report_id,
+				u32 field_index, s32 *value)
+{
+	struct hid_report *report;
+	struct sensor_hub_data *data =  hid_get_drvdata(hsdev->hdev);
+	int ret = 0;
+
+	if (report_id < 0)
+		return -EINVAL;
+
+	mutex_lock(&data->mutex);
+	report = sensor_hub_report(report_id, hsdev->hdev, HID_FEATURE_REPORT);
+	if (!report || (field_index >=  report->maxfield)) {
+		ret = -EINVAL;
+		goto done_proc;
+	}
+	usbhid_submit_report(hsdev->hdev, report, USB_DIR_IN);
+	usbhid_wait_io(hsdev->hdev);
+	*value = report->field[field_index]->value[0];
+
+done_proc:
+	mutex_unlock(&data->mutex);
+
+	return ret;
+}
+EXPORT_SYMBOL_GPL(sensor_hub_get_feature);
+
+
+int sensor_hub_input_attr_get_raw_value(struct hid_sensor_hub_device *hsdev,
+					u32 usage_id,
+					u32 attr_usage_id, u32 report_id)
+{
+	struct sensor_hub_data *data =  hid_get_drvdata(hsdev->hdev);
+	unsigned long flags;
+	struct hid_report *report;
+	int ret_val = 0;
+
+	if (report_id < 0)
+		return -EINVAL;
+
+	mutex_lock(&data->mutex);
+	memset(&data->pending, 0, sizeof(data->pending));
+	init_completion(&data->pending.ready);
+	data->pending.usage_id = usage_id;
+	data->pending.attr_usage_id = attr_usage_id;
+	data->pending.raw_size = 0;
+
+	spin_lock_irqsave(&data->lock, flags);
+	data->pending.status = true;
+	report = sensor_hub_report(report_id, hsdev->hdev, HID_INPUT_REPORT);
+	if (!report) {
+		spin_unlock_irqrestore(&data->lock, flags);
+		goto err_free;
+	}
+	usbhid_submit_report(hsdev->hdev, report, USB_DIR_IN);
+	spin_unlock_irqrestore(&data->lock, flags);
+	wait_for_completion_interruptible_timeout(&data->pending.ready, HZ*5);
+	switch (data->pending.raw_size) {
+	case 1:
+		ret_val = *(u8 *)data->pending.raw_data;
+		break;
+	case 2:
+		ret_val = *(u16 *)data->pending.raw_data;
+		break;
+	case 4:
+		ret_val = *(u32 *)data->pending.raw_data;
+		break;
+	default:
+		ret_val = 0;
+	}
+	kfree(data->pending.raw_data);
+
+err_free:
+	data->pending.status = false;
+	mutex_unlock(&data->mutex);
+
+	return ret_val;
+}
+EXPORT_SYMBOL_GPL(sensor_hub_input_attr_get_raw_value);
+
+int sensor_hub_input_get_attribute_info(struct hid_sensor_hub_device *hsdev,
+				u8 type,
+				u32 usage_id,
+				u32 attr_usage_id,
+				struct hid_sensor_hub_attribute_info *info)
+{
+	int ret = -1;
+	int i, j;
+	int collection_index = -1;
+	struct hid_report *report;
+	struct hid_field *field;
+	struct hid_report_enum *report_enum;
+	struct hid_device *hdev = hsdev->hdev;
+
+	/* Initialize with defaults */
+	info->usage_id = usage_id;
+	info->attrib_id =  attr_usage_id;
+	info->report_id = -1;
+	info->index = -1;
+	info->units = -1;
+	info->unit_expo = -1;
+
+	for (i = 0; i < hdev->maxcollection; ++i) {
+		struct hid_collection *collection = &hdev->collection[i];
+		if (usage_id == collection->usage) {
+			collection_index = i;
+			break;
+		}
+	}
+	if (collection_index == -1)
+		goto err_ret;
+
+	report_enum = &hdev->report_enum[type];
+	list_for_each_entry(report, &report_enum->report_list, list) {
+		for (i = 0; i < report->maxfield; ++i) {
+			field = report->field[i];
+			if (field->physical == usage_id &&
+				field->logical == attr_usage_id) {
+				info->index = i;
+				info->report_id = report->id;
+				info->units = field->unit;
+				info->unit_expo = field->unit_exponent;
+				info->size = field->report_size/8;
+				ret = 0;
+			} else {
+				for (j = 0; j < field->maxusage; ++j) {
+					if (field->usage[j].hid ==
+					attr_usage_id &&
+					field->usage[j].collection_index ==
+					collection_index)  {
+						info->index = i;
+						info->report_id = report->id;
+						info->units = field->unit;
+						info->unit_expo =
+							field->unit_exponent;
+						info->size =
+							field->report_size/8;
+						ret = 0;
+						break;
+					}
+				}
+			}
+			if (ret == 0)
+				break;
+		}
+	}
+
+err_ret:
+	return ret;
+}
+EXPORT_SYMBOL_GPL(sensor_hub_input_get_attribute_info);
+
+#ifdef CONFIG_PM
+static int sensor_hub_suspend(struct hid_device *hdev, pm_message_t message)
+{
+	struct sensor_hub_data *pdata =  hid_get_drvdata(hdev);
+	struct hid_sensor_hub_callbacks_list *callback;
+
+	hid_dbg(hdev, " sensor_hub_suspend\n");
+	spin_lock(&pdata->dyn_lock);
+	list_for_each_entry(callback, &pdata->dyn_callback_list, list) {
+		if (callback->usage_callback->suspend)
+			callback->usage_callback->suspend(
+					pdata->hsdev, callback->priv);
+	}
+	spin_unlock(&pdata->dyn_lock);
+
+	return 0;
+}
+
+static int sensor_hub_resume(struct hid_device *hdev)
+{
+	struct sensor_hub_data *pdata =  hid_get_drvdata(hdev);
+	struct hid_sensor_hub_callbacks_list *callback;
+
+	hid_dbg(hdev, " sensor_hub_resume\n");
+	spin_lock(&pdata->dyn_lock);
+	list_for_each_entry(callback, &pdata->dyn_callback_list, list) {
+		if (callback->usage_callback->resume)
+			callback->usage_callback->resume(
+					pdata->hsdev, callback->priv);
+	}
+	spin_unlock(&pdata->dyn_lock);
+
+	return 0;
+}
+
+static int sensor_hub_reset_resume(struct hid_device *hdev)
+{
+	return 0;
+}
+#endif
+/*
+ * Handle raw report as sent by device
+ */
+static int sensor_hub_raw_event(struct hid_device *hdev,
+		struct hid_report *report, u8 *raw_data, int size)
+{
+	int i;
+	u8 *ptr;
+	int sz;
+	struct sensor_hub_data *pdata = hid_get_drvdata(hdev);
+	unsigned long flags;
+	struct hid_sensor_hub_callbacks *callback = NULL;
+	struct hid_collection *collection = NULL;
+	void *priv = NULL;
+
+	hid_dbg(hdev, "sensor_hub_raw_event report id:0x%x size:%d type:%d\n",
+			 report->id, size, report->type);
+	hid_dbg(hdev, "maxfield:%d\n", report->maxfield);
+	if (report->type != HID_INPUT_REPORT)
+		return 1;
+
+	ptr = raw_data;
+	ptr++; /*Skip report id*/
+
+	if (!report)
+		goto err_report;
+
+	spin_lock_irqsave(&pdata->lock, flags);
+
+	for (i = 0; i < report->maxfield; ++i) {
+
+		hid_dbg(hdev, "%d collection_index:%x hid:%x sz:%x\n",
+				i, report->field[i]->usage->collection_index,
+				report->field[i]->usage->hid,
+				report->field[i]->report_size/8);
+
+		sz = report->field[i]->report_size/8;
+		if (pdata->pending.status && pdata->pending.attr_usage_id ==
+				report->field[i]->usage->hid) {
+			hid_dbg(hdev, "data was pending ...\n");
+			pdata->pending.raw_data = kmalloc(sz, GFP_KERNEL);
+			if (pdata->pending.raw_data) {
+				memcpy(pdata->pending.raw_data, ptr, sz);
+				pdata->pending.raw_size  = sz;
+			} else
+				pdata->pending.raw_size = 0;
+			complete(&pdata->pending.ready);
+		}
+		collection = &hdev->collection[
+				report->field[i]->usage->collection_index];
+		hid_dbg(hdev, "collection->usage %x\n",
+					collection->usage);
+		callback = sensor_hub_get_callback(pdata->hsdev->hdev,
+						report->field[i]->physical,
+							&priv);
+		if (callback && callback->capture_sample) {
+			if (report->field[i]->logical)
+				callback->capture_sample(pdata->hsdev,
+					report->field[i]->logical, sz, ptr,
+					callback->pdev);
+			else
+				callback->capture_sample(pdata->hsdev,
+					report->field[i]->usage->hid, sz, ptr,
+					callback->pdev);
+		}
+		ptr += sz;
+	}
+	if (callback && collection && callback->send_event)
+		callback->send_event(pdata->hsdev, collection->usage,
+				callback->pdev);
+	spin_unlock_irqrestore(&pdata->lock, flags);
+
+err_report:
+	return 1;
+}
+
+static int sensor_hub_probe(struct hid_device *hdev,
+				const struct hid_device_id *id)
+{
+	int ret;
+	struct sensor_hub_data *sd;
+	int i;
+	char *name;
+	struct hid_report *report;
+	struct hid_report_enum *report_enum;
+	struct hid_field *field;
+	int dev_cnt;
+
+	sd = kzalloc(sizeof(struct sensor_hub_data), GFP_KERNEL);
+	if (!sd) {
+		hid_err(hdev, "cannot allocate Sensor data\n");
+		return -ENOMEM;
+	}
+	sd->hsdev = kzalloc(sizeof(struct hid_sensor_hub_device), GFP_KERNEL);
+	if (!sd->hsdev) {
+		hid_err(hdev, "cannot allocate hid_sensor_hub_device\n");
+		ret = -ENOMEM;
+		goto err_free_hub;
+	}
+	hid_set_drvdata(hdev, sd);
+	sd->hsdev->hdev = hdev;
+	sd->hsdev->vendor_id = hdev->vendor;
+	sd->hsdev->product_id = hdev->product;
+	spin_lock_init(&sd->lock);
+	spin_lock_init(&sd->dyn_lock);
+	mutex_init(&sd->mutex);
+	ret = hid_parse(hdev);
+	if (ret) {
+		hid_err(hdev, "parse failed\n");
+		goto err_free;
+	}
+	if (sensor_hub_check_for_sensor_page(hdev) < 0) {
+		hid_err(hdev, "sensor page not found\n");
+		goto err_free;
+	}
+	INIT_LIST_HEAD(&hdev->inputs);
+
+	hdev->claimed = HID_CLAIMED_INPUT;
+	ret = hid_hw_start(hdev, 0);
+	if (ret) {
+		hid_err(hdev, "hw start failed\n");
+		goto err_free;
+	}
+	ret = hid_hw_open(hdev);
+	if (ret) {
+		hid_err(hdev, "failed to open input interrupt pipe\n");
+		goto err_stop_hw;
+	}
+
+	INIT_LIST_HEAD(&sd->dyn_callback_list);
+	sd->hid_sensor_client_cnt = 0;
+	report_enum = &hdev->report_enum[HID_INPUT_REPORT];
+
+	dev_cnt = sensor_hub_get_physical_device_count(report_enum);
+	sd->hid_sensor_hub_client_devs = kzalloc(dev_cnt *
+						sizeof(struct mfd_cell),
+						GFP_KERNEL);
+	if (sd->hid_sensor_hub_client_devs == NULL) {
+		hid_err(hdev,
+			"Failed to allocate memory for mfd cells\n");
+			ret = -ENOMEM;
+			goto err_close;
+	}
+	list_for_each_entry(report, &report_enum->report_list, list) {
+		hid_dbg(hdev, "Report id:%x\n", report->id);
+		field = report->field[0];
+		if (report->maxfield && field &&
+					field->physical) {
+			name = kasprintf(GFP_KERNEL, "HID-SENSOR-%x",
+						field->physical);
+			if (name  == NULL) {
+				hid_err(hdev,
+					"Failed MFD device name\n");
+					ret = -ENOMEM;
+					goto err_free_cells;
+			}
+			sd->hid_sensor_hub_client_devs[
+				sd->hid_sensor_client_cnt].name = name;
+			sd->hid_sensor_hub_client_devs[
+				sd->hid_sensor_client_cnt].platform_data =
+						sd->hsdev;
+			sd->hid_sensor_hub_client_devs[
+				sd->hid_sensor_client_cnt].pdata_size =
+						sizeof(*sd->hsdev);
+			hid_dbg(hdev, "Adding %s:%x\n", name,
+					(unsigned int)sd);
+			sd->hid_sensor_client_cnt++;
+		}
+	}
+	ret = mfd_add_devices(&hdev->dev, 0, sd->hid_sensor_hub_client_devs,
+		sd->hid_sensor_client_cnt, NULL, 0);
+	if (ret < 0)
+		goto err_free_names;
+
+	return ret;
+
+err_free_names:
+	for (i = 0; i < sd->hid_sensor_client_cnt ; ++i)
+		kfree(sd->hid_sensor_hub_client_devs[i].name);
+err_free_cells:
+	kfree(sd->hid_sensor_hub_client_devs);
+err_close:
+	hid_hw_stop(hdev);
+	hid_hw_close(hdev);
+err_stop_hw:
+	hid_hw_stop(hdev);
+err_free:
+	kfree(sd->hsdev);
+err_free_hub:
+	kfree(sd);
+
+	return ret;
+}
+
+static void sensor_hub_remove(struct hid_device *hdev)
+{
+	struct sensor_hub_data *data = hid_get_drvdata(hdev);
+	unsigned long flags;
+	int i;
+
+	hid_dbg(hdev, " hardware removed\n");
+	hdev->claimed &= ~HID_CLAIMED_INPUT;
+	hid_hw_stop(hdev);
+	hid_hw_close(hdev);
+	spin_lock_irqsave(&data->lock, flags);
+	if (data->pending.status)
+		complete(&data->pending.ready);
+	spin_unlock_irqrestore(&data->lock, flags);
+	mfd_remove_devices(&hdev->dev);
+	for (i = 0; i < data->hid_sensor_client_cnt ; ++i)
+		kfree(data->hid_sensor_hub_client_devs[i].name);
+	kfree(data->hid_sensor_hub_client_devs);
+	hid_set_drvdata(hdev, NULL);
+	mutex_destroy(&data->mutex);
+	kfree(data->hsdev);
+	kfree(data);
+}
+
+static const struct hid_device_id sensor_hub_devices[] = {
+	{ HID_USB_DEVICE(USB_VENDOR_ID_INTEL_8086,
+			USB_DEVICE_ID_SENSOR_HUB_1020) },
+	{ HID_USB_DEVICE(USB_VENDOR_ID_INTEL_8087,
+			USB_DEVICE_ID_SENSOR_HUB_1020) },
+	{ HID_USB_DEVICE(USB_VENDOR_ID_INTEL_8086,
+			USB_DEVICE_ID_SENSOR_HUB_09FA) },
+	{ HID_USB_DEVICE(USB_VENDOR_ID_INTEL_8087,
+			USB_DEVICE_ID_SENSOR_HUB_09FA) },
+	{ HID_USB_DEVICE(USB_VENDOR_ID_STANTUM_STM,
+			USB_DEVICE_ID_SENSOR_HUB_7014) },
+	{ }
+};
+
+static const struct hid_usage_id sensor_hub_grabbed_usages[] = {
+	{ HID_ANY_ID, HID_ANY_ID, HID_ANY_ID },
+	{ HID_ANY_ID - 1, HID_ANY_ID - 1, HID_ANY_ID - 1 }
+};
+
+static struct hid_driver sensor_hub_driver = {
+	.name = "hid-sensor-hub",
+	.id_table = sensor_hub_devices,
+	.probe = sensor_hub_probe,
+	.remove = sensor_hub_remove,
+	.raw_event = sensor_hub_raw_event,
+#ifdef CONFIG_PM
+	.suspend = sensor_hub_suspend,
+	.resume =  sensor_hub_resume,
+	.reset_resume =  sensor_hub_reset_resume,
+#endif
+};
+
+static int __init sensor_hub_init(void)
+{
+	return hid_register_driver(&sensor_hub_driver);
+}
+
+static void __exit sensor_hub_exit(void)
+{
+	hid_unregister_driver(&sensor_hub_driver);
+}
+
+module_init(sensor_hub_init);
+module_exit(sensor_hub_exit);
+
+MODULE_DESCRIPTION("HID Sensor Hub driver");
+MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@intel.com>");
+MODULE_LICENSE("GPL");
diff --git a/include/linux/hid-sensor-hub.h b/include/linux/hid-sensor-hub.h
new file mode 100644
index 0000000..0aa5f4c
--- /dev/null
+++ b/include/linux/hid-sensor-hub.h
@@ -0,0 +1,160 @@
+/*
+ * HID Sensors Driver
+ * Copyright (c) 2012, Intel Corporation.
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms and conditions of the GNU General Public License,
+ * version 2, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope 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 St - Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ */
+#ifndef _HID_SENSORS_HUB_H
+#define _HID_SENSORS_HUB_H
+
+#include <linux/hid.h>
+#include <linux/hid-sensor-ids.h>
+
+/**
+ * struct hid_sensor_hub_attribute_info - Attribute info
+ * @usage_id:		Parent usage id of a physical device.
+ * @attrib_id:		Attribute id for this attribute.
+ * @report_id:		Report id in which this information resides.
+ * @index:		Field index in the report.
+ * @units:		Measurment unit for this attribute.
+ * @unit_expo:		Exponent used in the data.
+ * @size:		Size in bytes for data size.
+ */
+struct hid_sensor_hub_attribute_info {
+	u32 usage_id;
+	u32 attrib_id;
+	s32 report_id;
+	s32 index;
+	s32 units;
+	s32 unit_expo;
+	s32 size;
+};
+
+/**
+ * struct hid_sensor_hub_device - Stores the hub instance data
+ * @hdev:		Stores the hid instance.
+ * @vendor_id:		Vendor id of hub device.
+ * @product_id:		Product id of hub device.
+ */
+struct hid_sensor_hub_device {
+	struct hid_device *hdev;
+	u32 vendor_id;
+	u32 product_id;
+};
+
+/**
+ * struct hid_sensor_hub_callbacks - Client callback functions
+ * @pdev:		Platform device instance of the client driver.
+ * @suspend:		Suspend callback.
+ * @resume:		Resume callback.
+ * @capture_sample:	Callback to get a sample.
+ * @send_event:		Send notification to indicate all samples are
+ *			captured, process and send event
+ */
+struct hid_sensor_hub_callbacks {
+	struct platform_device *pdev;
+	int (*suspend)(struct hid_sensor_hub_device *hsdev, void *priv);
+	int (*resume)(struct hid_sensor_hub_device *hsdev, void *priv);
+	int (*capture_sample)(struct hid_sensor_hub_device *hsdev,
+			u32 usage_id, size_t raw_len, char *raw_data,
+			void *priv);
+	int (*send_event)(struct hid_sensor_hub_device *hsdev, u32 usage_id,
+			 void *priv);
+};
+
+/* Registration functions */
+
+/**
+* sensor_hub_register_callback() - Register client callbacks
+* @hsdev:	Hub device instance.
+* @usage_id:	Usage id of the client (E.g. 0x200076 for Gyro).
+* @usage_callback: Callback function storage
+*
+* Used to register callbacks by client processing drivers. Sensor
+* hub core driver will call these callbacks to offload processing
+* of data streams and notifications.
+*/
+int sensor_hub_register_callback(struct hid_sensor_hub_device *hsdev,
+			u32 usage_id,
+			struct hid_sensor_hub_callbacks *usage_callback);
+
+/**
+* sensor_hub_remove_callback() - Remove client callbacks
+* @hsdev:	Hub device instance.
+* @usage_id:	Usage id of the client (E.g. 0x200076 for Gyro).
+*
+* If there is a callback registred, this call will remove that
+* callbacks, so that it will stop data and event notifications.
+*/
+int sensor_hub_remove_callback(struct hid_sensor_hub_device *hsdev,
+			u32 usage_id);
+
+
+/* Hid sensor hub core interfaces */
+
+/**
+* sensor_hub_input_get_attribute_info() - Get an attribute information
+* @hsdev:	Hub device instance.
+* @type:	Type of this attribute, input/output/feature
+* @usage_id:	Attribute usage id of parent physical device as per spec
+* @attr_usage_id:	Attribute usage id as per spec
+* @info:	return information about attribute after parsing report
+*
+* Parses report and returns the attribute information such as report id,
+* field index, units and exponet etc.
+*/
+int sensor_hub_input_get_attribute_info(struct hid_sensor_hub_device *hsdev,
+			u8 type,
+			u32 usage_id, u32 attr_usage_id,
+			struct hid_sensor_hub_attribute_info *info);
+
+/**
+* sensor_hub_input_attr_get_raw_value() - Synchronous read request
+* @usage_id:	Attribute usage id of parent physical device as per spec
+* @attr_usage_id:	Attribute usage id as per spec
+* @report_id:	Report id to look for
+*
+* Issues a synchronous read request for an input attribute. Returns
+* data upto 32 bits. Since client can get events, so this call should
+* not be used for data paths, this will impact performance.
+*/
+
+int sensor_hub_input_attr_get_raw_value(struct hid_sensor_hub_device *hsdev,
+			u32 usage_id,
+			u32 attr_usage_id, u32 report_id);
+/**
+* sensor_hub_set_feature() - Feature set request
+* @report_id:	Report id to look for
+* @field_index:	Field index inside a report
+* @value:	Value to set
+*
+* Used to set a field in feature report. For example this can set polling
+* interval, sensitivity, activate/deactivate state.
+*/
+int sensor_hub_set_feature(struct hid_sensor_hub_device *hsdev, u32 report_id,
+			u32 field_index, s32 value);
+
+/**
+* sensor_hub_get_feature() - Feature get request
+* @report_id:	Report id to look for
+* @field_index:	Field index inside a report
+* @value:	Place holder for return value
+*
+* Used to get a field in feature report. For example this can get polling
+* interval, sensitivity, activate/deactivate state.
+*/
+int sensor_hub_get_feature(struct hid_sensor_hub_device *hsdev, u32 report_id,
+			u32 field_index, s32 *value);
+#endif
diff --git a/include/linux/hid-sensor-ids.h b/include/linux/hid-sensor-ids.h
new file mode 100644
index 0000000..292e80f
--- /dev/null
+++ b/include/linux/hid-sensor-ids.h
@@ -0,0 +1,111 @@
+/*
+ * HID Sensors Driver
+ * Copyright (c) 2012, Intel Corporation.
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms and conditions of the GNU General Public License,
+ * version 2, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope 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 St - Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ */
+#ifndef _HID_SENSORS_IDS_H
+#define _HID_SENSORS_IDS_H
+
+#define HID_UP_SENSOR						0x00200000
+
+/* Accel 3D (200073) */
+#define HID_USAGE_SENSOR_ACCEL_3D				0x200073
+#define HID_USAGE_SENSOR_ACCEL_X_AXIS				0x200453
+#define HID_USAGE_SENSOR_ACCEL_Y_AXIS				0x200454
+#define HID_USAGE_SENSOR_ACCEL_Z_AXIS				0x200455
+
+/* ALS (200041) */
+#define HID_USAGE_SENSOR_ALS					0x200041
+#define HID_USAGE_SENSOR_LIGHT_ILLUM				0x2004d1
+
+/* Gyro 3D: (200076) */
+#define HID_USAGE_SENSOR_GYRO_3D				0x200076
+#define HID_USAGE_SENSOR_ANGL_VELOCITY_X_AXIS			0x200457
+#define HID_USAGE_SENSOR_ANGL_VELOCITY_Y_AXIS			0x200458
+#define HID_USAGE_SENSOR_ANGL_VELOCITY_Z_AXIS			0x200459
+
+/*ORIENTATION: Compass 3D: (200083) */
+#define HID_USAGE_SENSOR_COMPASS_3D				0x200083
+#define HID_USAGE_SENSOR_ORIENT_MAGN_HEADING			0x200471
+#define HID_USAGE_SENSOR_ORIENT_MAGN_HEADING_X			0x200472
+#define HID_USAGE_SENSOR_ORIENT_MAGN_HEADING_Y			0x200473
+#define HID_USAGE_SENSOR_ORIENT_MAGN_HEADING_Z			0x200474
+
+#define HID_USAGE_SENSOR_ORIENT_COMP_MAGN_NORTH			0x200475
+#define HID_USAGE_SENSOR_ORIENT_COMP_TRUE_NORTH			0x200476
+#define HID_USAGE_SENSOR_ORIENT_MAGN_NORTH			0x200477
+#define HID_USAGE_SENSOR_ORIENT_TRUE_NORTH			0x200478
+
+#define HID_USAGE_SENSOR_ORIENT_DISTANCE			0x200479
+#define HID_USAGE_SENSOR_ORIENT_DISTANCE_X			0x20047A
+#define HID_USAGE_SENSOR_ORIENT_DISTANCE_Y			0x20047B
+#define HID_USAGE_SENSOR_ORIENT_DISTANCE_Z			0x20047C
+#define HID_USAGE_SENSOR_ORIENT_DISTANCE_OUT_OF_RANGE		0x20047D
+#define HID_USAGE_SENSOR_ORIENT_TILT				0x20047E
+#define HID_USAGE_SENSOR_ORIENT_TILT_X				0x20047F
+#define HID_USAGE_SENSOR_ORIENT_TILT_Y				0x200480
+#define HID_USAGE_SENSOR_ORIENT_TILT_Z				0x200481
+#define HID_USAGE_SENSOR_ORIENT_ROTATION_MATRIX			0x200482
+#define HID_USAGE_SENSOR_ORIENT_QUATERNION			0x200483
+#define HID_USAGE_SENSOR_ORIENT_MAGN_FLUX			0x200484
+
+#define HID_USAGE_SENSOR_ORIENT_MAGN_FLUX_X_AXIS		0x200485
+#define HID_USAGE_SENSOR_ORIENT_MAGN_FLUX_Y_AXIS		0x200486
+#define HID_USAGE_SENSOR_ORIENT_MAGN_FLUX_Z_AXIS		0x200487
+
+/* Units */
+#define HID_USAGE_SENSOR_UNITS_NOT_SPECIFIED			0x00
+#define HID_USAGE_SENSOR_UNITS_LUX				0x01
+#define HID_USAGE_SENSOR_UNITS_KELVIN				0x01000100
+#define HID_USAGE_SENSOR_UNITS_FAHRENHEIT			0x03000100
+#define HID_USAGE_SENSOR_UNITS_PASCAL				0xF1E1
+#define HID_USAGE_SENSOR_UNITS_NEWTON				0x11E1
+#define HID_USAGE_SENSOR_UNITS_METERS_PER_SECOND		0x11F0
+#define HID_USAGE_SENSOR_UNITS_METERS_PER_SEC_SQRD		0x11E0
+#define HID_USAGE_SENSOR_UNITS_FARAD				0xE14F2000
+#define HID_USAGE_SENSOR_UNITS_AMPERE				0x01001000
+#define HID_USAGE_SENSOR_UNITS_WATT				0x21d1
+#define HID_USAGE_SENSOR_UNITS_HENRY				0x21E1E000
+#define HID_USAGE_SENSOR_UNITS_OHM				0x21D1E000
+#define HID_USAGE_SENSOR_UNITS_VOLT				0x21D1F000
+#define HID_USAGE_SENSOR_UNITS_HERTZ				0x01F0
+#define HID_USAGE_SENSOR_UNITS_DEGREES_PER_SEC_SQRD		0x14E0
+#define HID_USAGE_SENSOR_UNITS_RADIANS				0x12
+#define HID_USAGE_SENSOR_UNITS_RADIANS_PER_SECOND		0x12F0
+#define HID_USAGE_SENSOR_UNITS_RADIANS_PER_SEC_SQRD		0x12E0
+#define HID_USAGE_SENSOR_UNITS_SECOND				0x0110
+#define HID_USAGE_SENSOR_UNITS_GAUSS				0x01E1F000
+#define HID_USAGE_SENSOR_UNITS_GRAM				0x0101
+#define HID_USAGE_SENSOR_UNITS_CENTIMETER			0x11
+#define HID_USAGE_SENSOR_UNITS_G				0x1A
+#define HID_USAGE_SENSOR_UNITS_MILLISECOND			0x19
+#define HID_USAGE_SENSOR_UNITS_PERCENT				0x17
+#define HID_USAGE_SENSOR_UNITS_DEGREES				0x14
+#define HID_USAGE_SENSOR_UNITS_DEGREES_PER_SECOND		0x15
+
+/* Common selectors */
+#define HID_USAGE_SENSOR_PROP_REPORT_INTERVAL			0x20030E
+#define HID_USAGE_SENSOR_PROP_SENSITIVITY_ABS			0x20030F
+#define HID_USAGE_SENSOR_PROP_SENSITIVITY_RANGE_PCT		0x200310
+#define HID_USAGE_SENSOR_PROP_SENSITIVITY_REL_PCT		0x200311
+#define HID_USAGE_SENSOR_PROP_ACCURACY				0x200312
+#define HID_USAGE_SENSOR_PROP_RESOLUTION			0x200313
+#define HID_USAGE_SENSOR_PROP_RANGE_MAXIMUM			0x200314
+#define HID_USAGE_SENSOR_PROP_RANGE_MINIMUM			0x200315
+#define HID_USAGE_SENSOR_PROP_REPORT_STATE			0x200316
+#define HID_USAGE_SENSOR_PROY_POWER_STATE			0x200319
+
+#endif
-- 
1.7.7.6

^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH 4/9] HID-Sensors: Sensor framework
  2012-07-19 18:17 [PATCH 0/9] HID-Sensor v5 srinivas pandruvada
@ 2012-07-19 18:17 ` srinivas pandruvada
  0 siblings, 0 replies; 7+ messages in thread
From: srinivas pandruvada @ 2012-07-19 18:17 UTC (permalink / raw)
  To: linux-iio; +Cc: jic23, jkosina, srinivas pandruvada

Adding processing for HID Sensor usage table as defined by
HID 1.12, Request #: HUTRR39, dated 05 May, 2011.
This driver uses HID driver framework to register, send and
receive events.
This uses MFD framework, so that actual processing for a
specific usage id can be done in a different driver. For
example an accelerometer driver can be a separate driver and
use the interface provided by this driver to register for
events.

Signed-off-by: srinivas pandruvada <srinivas.pandruvada@intel.com>
---
 drivers/hid/Kconfig            |   14 +
 drivers/hid/Makefile           |    1 +
 drivers/hid/hid-sensor-hub.c   |  681 ++++++++++++++++++++++++++++++++++++++++
 include/linux/hid-sensor-hub.h |  160 ++++++++++
 include/linux/hid-sensor-ids.h |  111 +++++++
 5 files changed, 967 insertions(+), 0 deletions(-)
 create mode 100644 drivers/hid/hid-sensor-hub.c
 create mode 100644 include/linux/hid-sensor-hub.h
 create mode 100644 include/linux/hid-sensor-ids.h

diff --git a/drivers/hid/Kconfig b/drivers/hid/Kconfig
index bef04c1..a9fd774 100644
--- a/drivers/hid/Kconfig
+++ b/drivers/hid/Kconfig
@@ -653,6 +653,20 @@ config HID_ZYDACRON
 	---help---
 	Support for Zydacron remote control.
 
+config HID_SENSOR_HUB
+	tristate "HID Sensors framework support"
+	depends on USB_HID
+	select MFD_CORE
+	default n
+	-- help---
+	  Support for HID Sensor framework. This creates a MFD instance
+	  for a sensor hub and identifies all the sensors connected to it.
+	  Each sensor is registered as a MFD cell, so that sensor specific
+	  processing can be done in a separate driver. Each sensor
+	  drivers can use the service provided by this driver to register
+	  for events and handle data streams. Each sensor driver can format
+	  data and present to user mode using input or IIO interface.
+
 endmenu
 
 endif # HID
diff --git a/drivers/hid/Makefile b/drivers/hid/Makefile
index ca6cc9f..08f9d8f 100644
--- a/drivers/hid/Makefile
+++ b/drivers/hid/Makefile
@@ -87,6 +87,7 @@ obj-$(CONFIG_HID_ZYDACRON)	+= hid-zydacron.o
 obj-$(CONFIG_HID_WACOM)		+= hid-wacom.o
 obj-$(CONFIG_HID_WALTOP)	+= hid-waltop.o
 obj-$(CONFIG_HID_WIIMOTE)	+= hid-wiimote.o
+obj-$(CONFIG_HID_SENSOR_HUB)	+= hid-sensor-hub.o
 
 obj-$(CONFIG_USB_HID)		+= usbhid/
 obj-$(CONFIG_USB_MOUSE)		+= usbhid/
diff --git a/drivers/hid/hid-sensor-hub.c b/drivers/hid/hid-sensor-hub.c
new file mode 100644
index 0000000..98c1a77
--- /dev/null
+++ b/drivers/hid/hid-sensor-hub.c
@@ -0,0 +1,681 @@
+/*
+ * HID Sensors Driver
+ * Copyright (c) 2012, Intel Corporation.
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms and conditions of the GNU General Public License,
+ * version 2, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope 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 St - Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ */
+#include <linux/device.h>
+#include <linux/hid.h>
+#include <linux/usb.h>
+#include "usbhid/usbhid.h"
+#include <linux/module.h>
+#include <linux/slab.h>
+#include <linux/mfd/core.h>
+#include <linux/list.h>
+#include <linux/hid-sensor-ids.h>
+#include <linux/hid-sensor-hub.h>
+#include "hid-ids.h"
+
+/**
+ * struct sensor_hub_pending - Synchronous read pending information
+ * @status:		Pending status true/false.
+ * @ready:		Completion synchronization data.
+ * @usage_id:		Usage id for physical device, E.g. Gyro usage id.
+ * @attr_usage_id:	Usage Id of a field, E.g. X-AXIS for a gyro.
+ * @raw_size:		Response size for a read request.
+ * @raw_data:		Place holder for received response.
+ */
+struct sensor_hub_pending {
+	bool status;
+	struct completion ready;
+	u32 usage_id;
+	u32 attr_usage_id;
+	int raw_size;
+	u8  *raw_data;
+};
+
+/**
+ * struct sensor_hub_data - Hold a instance data for a HID hub device
+ * @hs_dev:		Stored hid instance for current hub device.
+ * @mutex:		Mutex to serialize synchronous request.
+ * @lock:		Spin lock to protect pending request structure.
+ * @pending:		Holds information of pending sync read request.
+ * @hid_sensor_hub_client_devs:	Stores all MFD cells for a hub instance.
+ * @hid_sensor_client_cnt: Number of MFD cells, (no of sensors attached).
+ */
+struct sensor_hub_data {
+	struct hid_sensor_hub_device *hsdev;
+	struct mutex mutex;
+	spinlock_t lock;
+	struct sensor_hub_pending pending;
+	struct list_head dyn_callback_list;
+	spinlock_t dyn_lock;
+	struct mfd_cell *hid_sensor_hub_client_devs;
+	int hid_sensor_client_cnt;
+};
+
+/**
+ * hid_sensor_hub_callbacks_list - Stores callback list
+ * @list:		list head.
+ * @usage_id:		usage id for a physical device.
+ * @usage_callbacks:	Stores registered callback functions.
+ * @priv:		Private data for a physical device.
+ */
+struct hid_sensor_hub_callbacks_list {
+	struct list_head list;
+	u32 usage_id;
+	struct hid_sensor_hub_callbacks *usage_callback;
+	void *priv;
+};
+
+static int sensor_hub_check_for_sensor_page(struct hid_device *hdev)
+{
+	int i;
+	int ret = -EINVAL;
+
+	for (i = 0; i < hdev->maxcollection; i++) {
+		struct hid_collection *col = &hdev->collection[i];
+		if (col->type == HID_COLLECTION_PHYSICAL &&
+		   (col->usage & HID_USAGE_PAGE) == HID_UP_SENSOR) {
+			ret = 0;
+			break;
+		}
+	}
+
+	return ret;
+}
+
+static struct hid_report *sensor_hub_report(int id, struct hid_device *hdev,
+						int dir)
+{
+	struct hid_report *report;
+
+	list_for_each_entry(report, &hdev->report_enum[dir].report_list, list) {
+		if (report->id == id)
+			return report;
+	}
+	hid_warn(hdev, "No report with id 0x%x found\n", id);
+
+	return NULL;
+}
+
+static int sensor_hub_get_physical_device_count(
+				struct hid_report_enum *report_enum)
+{
+	struct hid_report *report;
+	struct hid_field *field;
+	int cnt = 0;
+
+	list_for_each_entry(report, &report_enum->report_list, list) {
+		field = report->field[0];
+		if (report->maxfield && field &&
+					field->physical)
+			cnt++;
+	}
+
+	return cnt;
+}
+
+static struct hid_sensor_hub_callbacks *sensor_hub_get_callback(
+					struct hid_device *hdev,
+					u32 usage_id, void **priv)
+{
+	struct hid_sensor_hub_callbacks_list *callback;
+	struct sensor_hub_data *pdata = hid_get_drvdata(hdev);
+
+	spin_lock(&pdata->dyn_lock);
+	list_for_each_entry(callback, &pdata->dyn_callback_list, list)
+		if (callback->usage_id == usage_id) {
+			*priv = callback->priv;
+			spin_unlock(&pdata->dyn_lock);
+			return callback->usage_callback;
+		}
+	spin_unlock(&pdata->dyn_lock);
+
+	return NULL;
+}
+
+int sensor_hub_register_callback(struct hid_sensor_hub_device *hsdev,
+			u32 usage_id,
+			struct hid_sensor_hub_callbacks *usage_callback)
+{
+	struct hid_sensor_hub_callbacks_list *callback;
+	struct sensor_hub_data *pdata = hid_get_drvdata(hsdev->hdev);
+
+	spin_lock(&pdata->dyn_lock);
+	list_for_each_entry(callback, &pdata->dyn_callback_list, list)
+		if (callback->usage_id == usage_id) {
+			spin_unlock(&pdata->dyn_lock);
+			return -EINVAL;
+		}
+	callback = kzalloc(sizeof(*callback), GFP_KERNEL);
+	if (!callback) {
+		spin_unlock(&pdata->dyn_lock);
+		return -ENOMEM;
+	}
+	callback->usage_callback = usage_callback;
+	callback->usage_id = usage_id;
+	callback->priv = NULL;
+	list_add_tail(&callback->list, &pdata->dyn_callback_list);
+	spin_unlock(&pdata->dyn_lock);
+
+	return 0;
+}
+EXPORT_SYMBOL_GPL(sensor_hub_register_callback);
+
+int sensor_hub_remove_callback(struct hid_sensor_hub_device *hsdev,
+				u32 usage_id)
+{
+	struct hid_sensor_hub_callbacks_list *callback;
+	struct sensor_hub_data *pdata = hid_get_drvdata(hsdev->hdev);
+
+	spin_lock(&pdata->dyn_lock);
+	list_for_each_entry(callback, &pdata->dyn_callback_list, list)
+		if (callback->usage_id == usage_id) {
+			list_del(&callback->list);
+			kfree(callback);
+			break;
+		}
+	spin_unlock(&pdata->dyn_lock);
+
+	return 0;
+}
+EXPORT_SYMBOL_GPL(sensor_hub_remove_callback);
+
+int sensor_hub_set_feature(struct hid_sensor_hub_device *hsdev, u32 report_id,
+				u32 field_index, s32 value)
+{
+	struct hid_report *report;
+	struct sensor_hub_data *data =  hid_get_drvdata(hsdev->hdev);
+	int ret = 0;
+
+	if (report_id < 0)
+		return -EINVAL;
+
+	mutex_lock(&data->mutex);
+	report = sensor_hub_report(report_id, hsdev->hdev, HID_FEATURE_REPORT);
+	if (!report || (field_index >=  report->maxfield)) {
+		ret = -EINVAL;
+		goto done_proc;
+	}
+	hid_set_field(report->field[field_index], 0, value);
+	usbhid_submit_report(hsdev->hdev, report, USB_DIR_OUT);
+	usbhid_wait_io(hsdev->hdev);
+
+done_proc:
+	mutex_unlock(&data->mutex);
+
+	return ret;
+}
+EXPORT_SYMBOL_GPL(sensor_hub_set_feature);
+
+int sensor_hub_get_feature(struct hid_sensor_hub_device *hsdev, u32 report_id,
+				u32 field_index, s32 *value)
+{
+	struct hid_report *report;
+	struct sensor_hub_data *data =  hid_get_drvdata(hsdev->hdev);
+	int ret = 0;
+
+	if (report_id < 0)
+		return -EINVAL;
+
+	mutex_lock(&data->mutex);
+	report = sensor_hub_report(report_id, hsdev->hdev, HID_FEATURE_REPORT);
+	if (!report || (field_index >=  report->maxfield)) {
+		ret = -EINVAL;
+		goto done_proc;
+	}
+	usbhid_submit_report(hsdev->hdev, report, USB_DIR_IN);
+	usbhid_wait_io(hsdev->hdev);
+	*value = report->field[field_index]->value[0];
+
+done_proc:
+	mutex_unlock(&data->mutex);
+
+	return ret;
+}
+EXPORT_SYMBOL_GPL(sensor_hub_get_feature);
+
+
+int sensor_hub_input_attr_get_raw_value(struct hid_sensor_hub_device *hsdev,
+					u32 usage_id,
+					u32 attr_usage_id, u32 report_id)
+{
+	struct sensor_hub_data *data =  hid_get_drvdata(hsdev->hdev);
+	unsigned long flags;
+	struct hid_report *report;
+	int ret_val = 0;
+
+	if (report_id < 0)
+		return -EINVAL;
+
+	mutex_lock(&data->mutex);
+	memset(&data->pending, 0, sizeof(data->pending));
+	init_completion(&data->pending.ready);
+	data->pending.usage_id = usage_id;
+	data->pending.attr_usage_id = attr_usage_id;
+	data->pending.raw_size = 0;
+
+	spin_lock_irqsave(&data->lock, flags);
+	data->pending.status = true;
+	report = sensor_hub_report(report_id, hsdev->hdev, HID_INPUT_REPORT);
+	if (!report) {
+		spin_unlock_irqrestore(&data->lock, flags);
+		goto err_free;
+	}
+	usbhid_submit_report(hsdev->hdev, report, USB_DIR_IN);
+	spin_unlock_irqrestore(&data->lock, flags);
+	wait_for_completion_interruptible_timeout(&data->pending.ready, HZ*5);
+	switch (data->pending.raw_size) {
+	case 1:
+		ret_val = *(u8 *)data->pending.raw_data;
+		break;
+	case 2:
+		ret_val = *(u16 *)data->pending.raw_data;
+		break;
+	case 4:
+		ret_val = *(u32 *)data->pending.raw_data;
+		break;
+	default:
+		ret_val = 0;
+	}
+	kfree(data->pending.raw_data);
+
+err_free:
+	data->pending.status = false;
+	mutex_unlock(&data->mutex);
+
+	return ret_val;
+}
+EXPORT_SYMBOL_GPL(sensor_hub_input_attr_get_raw_value);
+
+int sensor_hub_input_get_attribute_info(struct hid_sensor_hub_device *hsdev,
+				u8 type,
+				u32 usage_id,
+				u32 attr_usage_id,
+				struct hid_sensor_hub_attribute_info *info)
+{
+	int ret = -1;
+	int i, j;
+	int collection_index = -1;
+	struct hid_report *report;
+	struct hid_field *field;
+	struct hid_report_enum *report_enum;
+	struct hid_device *hdev = hsdev->hdev;
+
+	/* Initialize with defaults */
+	info->usage_id = usage_id;
+	info->attrib_id =  attr_usage_id;
+	info->report_id = -1;
+	info->index = -1;
+	info->units = -1;
+	info->unit_expo = -1;
+
+	for (i = 0; i < hdev->maxcollection; ++i) {
+		struct hid_collection *collection = &hdev->collection[i];
+		if (usage_id == collection->usage) {
+			collection_index = i;
+			break;
+		}
+	}
+	if (collection_index == -1)
+		goto err_ret;
+
+	report_enum = &hdev->report_enum[type];
+	list_for_each_entry(report, &report_enum->report_list, list) {
+		for (i = 0; i < report->maxfield; ++i) {
+			field = report->field[i];
+			if (field->physical == usage_id &&
+				field->logical == attr_usage_id) {
+				info->index = i;
+				info->report_id = report->id;
+				info->units = field->unit;
+				info->unit_expo = field->unit_exponent;
+				info->size = field->report_size/8;
+				ret = 0;
+			} else {
+				for (j = 0; j < field->maxusage; ++j) {
+					if (field->usage[j].hid ==
+					attr_usage_id &&
+					field->usage[j].collection_index ==
+					collection_index)  {
+						info->index = i;
+						info->report_id = report->id;
+						info->units = field->unit;
+						info->unit_expo =
+							field->unit_exponent;
+						info->size =
+							field->report_size/8;
+						ret = 0;
+						break;
+					}
+				}
+			}
+			if (ret == 0)
+				break;
+		}
+	}
+
+err_ret:
+	return ret;
+}
+EXPORT_SYMBOL_GPL(sensor_hub_input_get_attribute_info);
+
+#ifdef CONFIG_PM
+static int sensor_hub_suspend(struct hid_device *hdev, pm_message_t message)
+{
+	struct sensor_hub_data *pdata =  hid_get_drvdata(hdev);
+	struct hid_sensor_hub_callbacks_list *callback;
+
+	hid_dbg(hdev, " sensor_hub_suspend\n");
+	spin_lock(&pdata->dyn_lock);
+	list_for_each_entry(callback, &pdata->dyn_callback_list, list) {
+		if (callback->usage_callback->suspend)
+			callback->usage_callback->suspend(
+					pdata->hsdev, callback->priv);
+	}
+	spin_unlock(&pdata->dyn_lock);
+
+	return 0;
+}
+
+static int sensor_hub_resume(struct hid_device *hdev)
+{
+	struct sensor_hub_data *pdata =  hid_get_drvdata(hdev);
+	struct hid_sensor_hub_callbacks_list *callback;
+
+	hid_dbg(hdev, " sensor_hub_resume\n");
+	spin_lock(&pdata->dyn_lock);
+	list_for_each_entry(callback, &pdata->dyn_callback_list, list) {
+		if (callback->usage_callback->resume)
+			callback->usage_callback->resume(
+					pdata->hsdev, callback->priv);
+	}
+	spin_unlock(&pdata->dyn_lock);
+
+	return 0;
+}
+
+static int sensor_hub_reset_resume(struct hid_device *hdev)
+{
+	return 0;
+}
+#endif
+/*
+ * Handle raw report as sent by device
+ */
+static int sensor_hub_raw_event(struct hid_device *hdev,
+		struct hid_report *report, u8 *raw_data, int size)
+{
+	int i;
+	u8 *ptr;
+	int sz;
+	struct sensor_hub_data *pdata = hid_get_drvdata(hdev);
+	unsigned long flags;
+	struct hid_sensor_hub_callbacks *callback = NULL;
+	struct hid_collection *collection = NULL;
+	void *priv = NULL;
+
+	hid_dbg(hdev, "sensor_hub_raw_event report id:0x%x size:%d type:%d\n",
+			 report->id, size, report->type);
+	hid_dbg(hdev, "maxfield:%d\n", report->maxfield);
+	if (report->type != HID_INPUT_REPORT)
+		return 1;
+
+	ptr = raw_data;
+	ptr++; /*Skip report id*/
+
+	if (!report)
+		goto err_report;
+
+	spin_lock_irqsave(&pdata->lock, flags);
+
+	for (i = 0; i < report->maxfield; ++i) {
+
+		hid_dbg(hdev, "%d collection_index:%x hid:%x sz:%x\n",
+				i, report->field[i]->usage->collection_index,
+				report->field[i]->usage->hid,
+				report->field[i]->report_size/8);
+
+		sz = report->field[i]->report_size/8;
+		if (pdata->pending.status && pdata->pending.attr_usage_id ==
+				report->field[i]->usage->hid) {
+			hid_dbg(hdev, "data was pending ...\n");
+			pdata->pending.raw_data = kmalloc(sz, GFP_KERNEL);
+			if (pdata->pending.raw_data) {
+				memcpy(pdata->pending.raw_data, ptr, sz);
+				pdata->pending.raw_size  = sz;
+			} else
+				pdata->pending.raw_size = 0;
+			complete(&pdata->pending.ready);
+		}
+		collection = &hdev->collection[
+				report->field[i]->usage->collection_index];
+		hid_dbg(hdev, "collection->usage %x\n",
+					collection->usage);
+		callback = sensor_hub_get_callback(pdata->hsdev->hdev,
+						report->field[i]->physical,
+							&priv);
+		if (callback && callback->capture_sample) {
+			if (report->field[i]->logical)
+				callback->capture_sample(pdata->hsdev,
+					report->field[i]->logical, sz, ptr,
+					callback->pdev);
+			else
+				callback->capture_sample(pdata->hsdev,
+					report->field[i]->usage->hid, sz, ptr,
+					callback->pdev);
+		}
+		ptr += sz;
+	}
+	if (callback && collection && callback->send_event)
+		callback->send_event(pdata->hsdev, collection->usage,
+				callback->pdev);
+	spin_unlock_irqrestore(&pdata->lock, flags);
+
+err_report:
+	return 1;
+}
+
+static int sensor_hub_probe(struct hid_device *hdev,
+				const struct hid_device_id *id)
+{
+	int ret;
+	struct sensor_hub_data *sd;
+	int i;
+	char *name;
+	struct hid_report *report;
+	struct hid_report_enum *report_enum;
+	struct hid_field *field;
+	int dev_cnt;
+
+	sd = kzalloc(sizeof(struct sensor_hub_data), GFP_KERNEL);
+	if (!sd) {
+		hid_err(hdev, "cannot allocate Sensor data\n");
+		return -ENOMEM;
+	}
+	sd->hsdev = kzalloc(sizeof(struct hid_sensor_hub_device), GFP_KERNEL);
+	if (!sd->hsdev) {
+		hid_err(hdev, "cannot allocate hid_sensor_hub_device\n");
+		ret = -ENOMEM;
+		goto err_free_hub;
+	}
+	hid_set_drvdata(hdev, sd);
+	sd->hsdev->hdev = hdev;
+	sd->hsdev->vendor_id = hdev->vendor;
+	sd->hsdev->product_id = hdev->product;
+	spin_lock_init(&sd->lock);
+	spin_lock_init(&sd->dyn_lock);
+	mutex_init(&sd->mutex);
+	ret = hid_parse(hdev);
+	if (ret) {
+		hid_err(hdev, "parse failed\n");
+		goto err_free;
+	}
+	if (sensor_hub_check_for_sensor_page(hdev) < 0) {
+		hid_err(hdev, "sensor page not found\n");
+		goto err_free;
+	}
+	INIT_LIST_HEAD(&hdev->inputs);
+
+	hdev->claimed = HID_CLAIMED_INPUT;
+	ret = hid_hw_start(hdev, 0);
+	if (ret) {
+		hid_err(hdev, "hw start failed\n");
+		goto err_free;
+	}
+	ret = hid_hw_open(hdev);
+	if (ret) {
+		hid_err(hdev, "failed to open input interrupt pipe\n");
+		goto err_stop_hw;
+	}
+
+	INIT_LIST_HEAD(&sd->dyn_callback_list);
+	sd->hid_sensor_client_cnt = 0;
+	report_enum = &hdev->report_enum[HID_INPUT_REPORT];
+
+	dev_cnt = sensor_hub_get_physical_device_count(report_enum);
+	sd->hid_sensor_hub_client_devs = kmalloc(dev_cnt *
+						sizeof(struct mfd_cell),
+						GFP_KERNEL);
+	if (sd->hid_sensor_hub_client_devs == NULL) {
+		hid_err(hdev,
+			"Failed to allocate memory for mfd cells\n");
+			ret = -ENOMEM;
+			goto err_close;
+	}
+	list_for_each_entry(report, &report_enum->report_list, list) {
+		hid_dbg(hdev, "Report id:%x\n", report->id);
+		field = report->field[0];
+		if (report->maxfield && field &&
+					field->physical) {
+			name = kasprintf(GFP_KERNEL, "HID-SENSOR-%x",
+						field->physical);
+			if (name  == NULL) {
+				hid_err(hdev,
+					"Failed MFD device name\n");
+					ret = -ENOMEM;
+					goto err_free_cells;
+			}
+			sd->hid_sensor_hub_client_devs[
+				sd->hid_sensor_client_cnt].name = name;
+			sd->hid_sensor_hub_client_devs[
+				sd->hid_sensor_client_cnt].platform_data =
+						sd->hsdev;
+			sd->hid_sensor_hub_client_devs[
+				sd->hid_sensor_client_cnt].pdata_size =
+						sizeof(*sd->hsdev);
+			hid_dbg(hdev, "Adding %s:%x\n", name,
+					(unsigned int)sd);
+			sd->hid_sensor_client_cnt++;
+		}
+	}
+	ret = mfd_add_devices(&hdev->dev, 0, sd->hid_sensor_hub_client_devs,
+		sd->hid_sensor_client_cnt, NULL, 0);
+	if (ret < 0)
+		goto err_free_names;
+
+	return ret;
+
+err_free_names:
+	for (i = 0; i < sd->hid_sensor_client_cnt ; ++i)
+		kfree(sd->hid_sensor_hub_client_devs[i].name);
+err_free_cells:
+	kfree(sd->hid_sensor_hub_client_devs);
+err_close:
+	hid_hw_stop(hdev);
+	hid_hw_close(hdev);
+err_stop_hw:
+	hid_hw_stop(hdev);
+err_free:
+	kfree(sd->hsdev);
+err_free_hub:
+	kfree(sd);
+
+	return ret;
+}
+
+static void sensor_hub_remove(struct hid_device *hdev)
+{
+	struct sensor_hub_data *data = hid_get_drvdata(hdev);
+	unsigned long flags;
+	int i;
+
+	hid_dbg(hdev, " hardware removed\n");
+	hdev->claimed &= ~HID_CLAIMED_INPUT;
+	hid_hw_stop(hdev);
+	hid_hw_close(hdev);
+	spin_lock_irqsave(&data->lock, flags);
+	if (data->pending.status)
+		complete(&data->pending.ready);
+	spin_unlock_irqrestore(&data->lock, flags);
+	mfd_remove_devices(&hdev->dev);
+	for (i = 0; i < data->hid_sensor_client_cnt ; ++i)
+		kfree(data->hid_sensor_hub_client_devs[i].name);
+	kfree(data->hid_sensor_hub_client_devs);
+	hid_set_drvdata(hdev, NULL);
+	mutex_destroy(&data->mutex);
+	kfree(data->hsdev);
+	kfree(data);
+}
+
+static const struct hid_device_id sensor_hub_devices[] = {
+	{ HID_USB_DEVICE(USB_VENDOR_ID_INTEL_8086,
+			USB_DEVICE_ID_SENSOR_HUB_1020) },
+	{ HID_USB_DEVICE(USB_VENDOR_ID_INTEL_8087,
+			USB_DEVICE_ID_SENSOR_HUB_1020) },
+	{ HID_USB_DEVICE(USB_VENDOR_ID_INTEL_8086,
+			USB_DEVICE_ID_SENSOR_HUB_09FA) },
+	{ HID_USB_DEVICE(USB_VENDOR_ID_INTEL_8087,
+			USB_DEVICE_ID_SENSOR_HUB_09FA) },
+	{ HID_USB_DEVICE(USB_VENDOR_ID_STANTUM_STM,
+			USB_DEVICE_ID_SENSOR_HUB_7014) },
+	{ }
+};
+
+static const struct hid_usage_id sensor_hub_grabbed_usages[] = {
+	{ HID_ANY_ID, HID_ANY_ID, HID_ANY_ID },
+	{ HID_ANY_ID - 1, HID_ANY_ID - 1, HID_ANY_ID - 1 }
+};
+
+static struct hid_driver sensor_hub_driver = {
+	.name = "hid-sensor-hub",
+	.id_table = sensor_hub_devices,
+	.probe = sensor_hub_probe,
+	.remove = sensor_hub_remove,
+	.raw_event = sensor_hub_raw_event,
+#ifdef CONFIG_PM
+	.suspend = sensor_hub_suspend,
+	.resume =  sensor_hub_resume,
+	.reset_resume =  sensor_hub_reset_resume,
+#endif
+};
+
+static int __init sensor_hub_init(void)
+{
+	return hid_register_driver(&sensor_hub_driver);
+}
+
+static void __exit sensor_hub_exit(void)
+{
+	hid_unregister_driver(&sensor_hub_driver);
+}
+
+module_init(sensor_hub_init);
+module_exit(sensor_hub_exit);
+
+MODULE_DESCRIPTION("HID Sensor Hub driver");
+MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@intel.com>");
+MODULE_LICENSE("GPL");
diff --git a/include/linux/hid-sensor-hub.h b/include/linux/hid-sensor-hub.h
new file mode 100644
index 0000000..a0283c0
--- /dev/null
+++ b/include/linux/hid-sensor-hub.h
@@ -0,0 +1,160 @@
+/*
+ * HID Sensors Driver
+ * Copyright (c) 2012, Intel Corporation.
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms and conditions of the GNU General Public License,
+ * version 2, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope 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 St - Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ */
+#ifndef _HID_SENSORS_HUB_H
+#define _HID_SENSORS_HUB_H
+
+#include <linux/hid.h>
+#include <linux/hid-sensor-ids.h>
+
+/**
+ * struct hid_sensor_hub_attribute_info - Attribute info
+ * @usage_id:		Parent usage id of a physical device.
+ * @attrib_id:		Attribute id for this attribute.
+ * @report_id:		Report id in which this information resides.
+ * @index:		Field index in the report.
+ * @units:		Measurment unit for this attribute.
+ * @unit_expo:		Exponent used in the data.
+ * @size:		Size in bytes for data size.
+ */
+struct hid_sensor_hub_attribute_info {
+	u32 usage_id;
+	u32 attrib_id;
+	s32 report_id;
+	s32 index;
+	s32 units;
+	s32 unit_expo;
+	s32 size;
+};
+
+/**
+ * struct hid_sensor_hub_device - Stores the hub instance data
+ * @hdev:		Stores the hid instance.
+ * @vendor_id:		Vendor id of hub device.
+ * @product_id:		Product id of hub device.
+ */
+struct hid_sensor_hub_device {
+	struct hid_device *hdev;
+	u32 vendor_id;
+	u32 product_id;
+};
+
+/**
+ * struct hid_sensor_hub_callbacks - Client callback functions
+ * @pdev:		Platform device instance of the client driver.
+ * @suspend:		Suspend callback.
+ * @resume:		Resume callback.
+ * @capture_sample:	Callback to get a sample.
+ * @send_event:		Send notification to indicate all samples are
+ *			captured, process and send event
+ */
+struct hid_sensor_hub_callbacks {
+	struct platform_device *pdev;
+	int (*suspend)(struct hid_sensor_hub_device *hsdev, void *priv);
+	int (*resume)(struct hid_sensor_hub_device *hsdev, void *priv);
+	int (*capture_sample)(struct hid_sensor_hub_device *hsdev,
+			u32 usage_id, size_t raw_len, char *raw_data,
+			void *priv);
+	int (*send_event)(struct hid_sensor_hub_device *hsdev, u32 usage_id,
+			 void *priv);
+};
+
+/* Registeration functions */
+
+/**
+* sensor_hub_register_callback() - Register client callbacks
+* @hsdev:	Hub device instance.
+* @usage_id:	Usage id of the client (E.g. 0x200076 for Gyro).
+* @usage_callback: Callback function storage
+*
+* Used to register callbacks by client processing drivers. Sensor
+* hub core driver will call these callbacks to offload processing
+* of data streams and notifications.
+*/
+int sensor_hub_register_callback(struct hid_sensor_hub_device *hsdev,
+			u32 usage_id,
+			struct hid_sensor_hub_callbacks *usage_callback);
+
+/**
+* sensor_hub_remove_callback() - Remove client callbacks
+* @hsdev:	Hub device instance.
+* @usage_id:	Usage id of the client (E.g. 0x200076 for Gyro).
+*
+* If there is a callback registred, this call will remove that
+* callbacks, so that it will stop data and event notifications.
+*/
+int sensor_hub_remove_callback(struct hid_sensor_hub_device *hsdev,
+			u32 usage_id);
+
+
+/* Hid sensor hub core interfaces */
+
+/**
+* sensor_hub_input_get_attribute_info() - Get an attribute information
+* @hsdev:	Hub device instance.
+* @type:	Type of this attribute, input/output/feature
+* @usage_id:	Attribute usage id of parent physical device as per spec
+* @attr_usage_id:	Attribute usage id as per spec
+* @info:	return information about attribute after parsing report
+*
+* Parses report and returns the attribute information such as report id,
+* field index, units and exponet etc.
+*/
+int sensor_hub_input_get_attribute_info(struct hid_sensor_hub_device *hsdev,
+			u8 type,
+			u32 usage_id, u32 attr_usage_id,
+			struct hid_sensor_hub_attribute_info *info);
+
+/**
+* sensor_hub_input_attr_get_raw_value() - Synchronous read request
+* @usage_id:	Attribute usage id of parent physical device as per spec
+* @attr_usage_id:	Attribute usage id as per spec
+* @report_id:	Report id to look for
+*
+* Issues a synchronous read request for an input attribute. Returns
+* data upto 32 bits. Since client can get events, so this call should
+* not be used for data paths, this will impact performance.
+*/
+
+int sensor_hub_input_attr_get_raw_value(struct hid_sensor_hub_device *hsdev,
+			u32 usage_id,
+			u32 attr_usage_id, u32 report_id);
+/**
+* sensor_hub_set_feature() - Feature set request
+* @report_id:	Report id to look for
+* @field_index:	Field index inside a report
+* @value:	Value to set
+*
+* Used to set a field in feature report. For example this can set polling
+* interval, sensitivity, activate/deactivate state.
+*/
+int sensor_hub_set_feature(struct hid_sensor_hub_device *hsdev, u32 report_id,
+			u32 field_index, s32 value);
+
+/**
+* sensor_hub_get_feature() - Feature get request
+* @report_id:	Report id to look for
+* @field_index:	Field index inside a report
+* @value:	Place holder for return value
+*
+* Used to get a field in feature report. For example this can get polling
+* interval, sensitivity, activate/deactivate state.
+*/
+int sensor_hub_get_feature(struct hid_sensor_hub_device *hsdev, u32 report_id,
+			u32 field_index, s32 *value);
+#endif
diff --git a/include/linux/hid-sensor-ids.h b/include/linux/hid-sensor-ids.h
new file mode 100644
index 0000000..292e80f
--- /dev/null
+++ b/include/linux/hid-sensor-ids.h
@@ -0,0 +1,111 @@
+/*
+ * HID Sensors Driver
+ * Copyright (c) 2012, Intel Corporation.
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms and conditions of the GNU General Public License,
+ * version 2, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope 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 St - Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ */
+#ifndef _HID_SENSORS_IDS_H
+#define _HID_SENSORS_IDS_H
+
+#define HID_UP_SENSOR						0x00200000
+
+/* Accel 3D (200073) */
+#define HID_USAGE_SENSOR_ACCEL_3D				0x200073
+#define HID_USAGE_SENSOR_ACCEL_X_AXIS				0x200453
+#define HID_USAGE_SENSOR_ACCEL_Y_AXIS				0x200454
+#define HID_USAGE_SENSOR_ACCEL_Z_AXIS				0x200455
+
+/* ALS (200041) */
+#define HID_USAGE_SENSOR_ALS					0x200041
+#define HID_USAGE_SENSOR_LIGHT_ILLUM				0x2004d1
+
+/* Gyro 3D: (200076) */
+#define HID_USAGE_SENSOR_GYRO_3D				0x200076
+#define HID_USAGE_SENSOR_ANGL_VELOCITY_X_AXIS			0x200457
+#define HID_USAGE_SENSOR_ANGL_VELOCITY_Y_AXIS			0x200458
+#define HID_USAGE_SENSOR_ANGL_VELOCITY_Z_AXIS			0x200459
+
+/*ORIENTATION: Compass 3D: (200083) */
+#define HID_USAGE_SENSOR_COMPASS_3D				0x200083
+#define HID_USAGE_SENSOR_ORIENT_MAGN_HEADING			0x200471
+#define HID_USAGE_SENSOR_ORIENT_MAGN_HEADING_X			0x200472
+#define HID_USAGE_SENSOR_ORIENT_MAGN_HEADING_Y			0x200473
+#define HID_USAGE_SENSOR_ORIENT_MAGN_HEADING_Z			0x200474
+
+#define HID_USAGE_SENSOR_ORIENT_COMP_MAGN_NORTH			0x200475
+#define HID_USAGE_SENSOR_ORIENT_COMP_TRUE_NORTH			0x200476
+#define HID_USAGE_SENSOR_ORIENT_MAGN_NORTH			0x200477
+#define HID_USAGE_SENSOR_ORIENT_TRUE_NORTH			0x200478
+
+#define HID_USAGE_SENSOR_ORIENT_DISTANCE			0x200479
+#define HID_USAGE_SENSOR_ORIENT_DISTANCE_X			0x20047A
+#define HID_USAGE_SENSOR_ORIENT_DISTANCE_Y			0x20047B
+#define HID_USAGE_SENSOR_ORIENT_DISTANCE_Z			0x20047C
+#define HID_USAGE_SENSOR_ORIENT_DISTANCE_OUT_OF_RANGE		0x20047D
+#define HID_USAGE_SENSOR_ORIENT_TILT				0x20047E
+#define HID_USAGE_SENSOR_ORIENT_TILT_X				0x20047F
+#define HID_USAGE_SENSOR_ORIENT_TILT_Y				0x200480
+#define HID_USAGE_SENSOR_ORIENT_TILT_Z				0x200481
+#define HID_USAGE_SENSOR_ORIENT_ROTATION_MATRIX			0x200482
+#define HID_USAGE_SENSOR_ORIENT_QUATERNION			0x200483
+#define HID_USAGE_SENSOR_ORIENT_MAGN_FLUX			0x200484
+
+#define HID_USAGE_SENSOR_ORIENT_MAGN_FLUX_X_AXIS		0x200485
+#define HID_USAGE_SENSOR_ORIENT_MAGN_FLUX_Y_AXIS		0x200486
+#define HID_USAGE_SENSOR_ORIENT_MAGN_FLUX_Z_AXIS		0x200487
+
+/* Units */
+#define HID_USAGE_SENSOR_UNITS_NOT_SPECIFIED			0x00
+#define HID_USAGE_SENSOR_UNITS_LUX				0x01
+#define HID_USAGE_SENSOR_UNITS_KELVIN				0x01000100
+#define HID_USAGE_SENSOR_UNITS_FAHRENHEIT			0x03000100
+#define HID_USAGE_SENSOR_UNITS_PASCAL				0xF1E1
+#define HID_USAGE_SENSOR_UNITS_NEWTON				0x11E1
+#define HID_USAGE_SENSOR_UNITS_METERS_PER_SECOND		0x11F0
+#define HID_USAGE_SENSOR_UNITS_METERS_PER_SEC_SQRD		0x11E0
+#define HID_USAGE_SENSOR_UNITS_FARAD				0xE14F2000
+#define HID_USAGE_SENSOR_UNITS_AMPERE				0x01001000
+#define HID_USAGE_SENSOR_UNITS_WATT				0x21d1
+#define HID_USAGE_SENSOR_UNITS_HENRY				0x21E1E000
+#define HID_USAGE_SENSOR_UNITS_OHM				0x21D1E000
+#define HID_USAGE_SENSOR_UNITS_VOLT				0x21D1F000
+#define HID_USAGE_SENSOR_UNITS_HERTZ				0x01F0
+#define HID_USAGE_SENSOR_UNITS_DEGREES_PER_SEC_SQRD		0x14E0
+#define HID_USAGE_SENSOR_UNITS_RADIANS				0x12
+#define HID_USAGE_SENSOR_UNITS_RADIANS_PER_SECOND		0x12F0
+#define HID_USAGE_SENSOR_UNITS_RADIANS_PER_SEC_SQRD		0x12E0
+#define HID_USAGE_SENSOR_UNITS_SECOND				0x0110
+#define HID_USAGE_SENSOR_UNITS_GAUSS				0x01E1F000
+#define HID_USAGE_SENSOR_UNITS_GRAM				0x0101
+#define HID_USAGE_SENSOR_UNITS_CENTIMETER			0x11
+#define HID_USAGE_SENSOR_UNITS_G				0x1A
+#define HID_USAGE_SENSOR_UNITS_MILLISECOND			0x19
+#define HID_USAGE_SENSOR_UNITS_PERCENT				0x17
+#define HID_USAGE_SENSOR_UNITS_DEGREES				0x14
+#define HID_USAGE_SENSOR_UNITS_DEGREES_PER_SECOND		0x15
+
+/* Common selectors */
+#define HID_USAGE_SENSOR_PROP_REPORT_INTERVAL			0x20030E
+#define HID_USAGE_SENSOR_PROP_SENSITIVITY_ABS			0x20030F
+#define HID_USAGE_SENSOR_PROP_SENSITIVITY_RANGE_PCT		0x200310
+#define HID_USAGE_SENSOR_PROP_SENSITIVITY_REL_PCT		0x200311
+#define HID_USAGE_SENSOR_PROP_ACCURACY				0x200312
+#define HID_USAGE_SENSOR_PROP_RESOLUTION			0x200313
+#define HID_USAGE_SENSOR_PROP_RANGE_MAXIMUM			0x200314
+#define HID_USAGE_SENSOR_PROP_RANGE_MINIMUM			0x200315
+#define HID_USAGE_SENSOR_PROP_REPORT_STATE			0x200316
+#define HID_USAGE_SENSOR_PROY_POWER_STATE			0x200319
+
+#endif
-- 
1.7.7.6

^ permalink raw reply related	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2012-09-05  9:40 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <1343881711-31209-1-git-send-email-srinivas.pandruvada@intel.com>
     [not found] ` <1343881711-31209-9-git-send-email-srinivas.pandruvada@intel.com>
     [not found]   ` <501F95C2.6020803@kernel.org>
     [not found]     ` <501F973E.4030008@kernel.org>
2012-08-06 15:40       ` [PATCH 8/9] HID-Sensors: Added Compass/Magnetometer 3D Pandruvada, Srinivas
2012-08-06 16:02         ` Jonathan Cameron
     [not found] ` <1343881711-31209-5-git-send-email-srinivas.pandruvada@intel.com>
     [not found]   ` <501F9DD1.1000606@kernel.org>
2012-08-06 15:43     ` [PATCH 4/9] HID-Sensors: Sensor framework Pandruvada, Srinivas
2012-08-27 15:49 [PATCH 0/9] HID-Sensor v8 srinivas pandruvada
2012-08-27 15:49 ` [PATCH 4/9] HID-Sensors: Sensor framework srinivas pandruvada
2012-09-05  9:40   ` Jiri Kosina
  -- strict thread matches above, loose matches on Subject: below --
2012-08-07 16:28 [PATCH 0/9] HID-Sensor v7 srinivas pandruvada
2012-08-07 16:28 ` [PATCH 4/9] HID-Sensors: Sensor framework srinivas pandruvada
2012-07-19 18:17 [PATCH 0/9] HID-Sensor v5 srinivas pandruvada
2012-07-19 18:17 ` [PATCH 4/9] HID-Sensors: Sensor framework srinivas pandruvada

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.