Merge branches 'for-4.2/i2c-hid', 'for-4.2/lenovo', 'for-4.2/plantronics', 'for-4...
[cascardo/linux.git] / drivers / hid / hid-lg4ff.c
index f7f4eb6..02cec83 100644 (file)
@@ -72,24 +72,25 @@ static void lg4ff_set_range_dfp(struct hid_device *hid, u16 range);
 static void lg4ff_set_range_g25(struct hid_device *hid, u16 range);
 
 struct lg4ff_wheel_data {
-       u32 product_id;
+       const u32 product_id;
        u16 range;
-       u16 min_range;
-       u16 max_range;
+       const u16 min_range;
+       const u16 max_range;
 #ifdef CONFIG_LEDS_CLASS
        u8  led_state;
        struct led_classdev *led[5];
 #endif
-       u32 alternate_modes;
-       const char *real_tag;
-       const char *real_name;
-       u16 real_product_id;
+       const u32 alternate_modes;
+       const char * const real_tag;
+       const char * const real_name;
+       const u16 real_product_id;
 
        void (*set_range)(struct hid_device *hid, u16 range);
 };
 
 struct lg4ff_device_entry {
        spinlock_t report_lock; /* Protect output HID report */
+       struct hid_report *report;
        struct lg4ff_wheel_data wdata;
 };
 
@@ -299,15 +300,41 @@ int lg4ff_adjust_input_event(struct hid_device *hid, struct hid_field *field,
        }
 }
 
+static void lg4ff_init_wheel_data(struct lg4ff_wheel_data * const wdata, const struct lg4ff_wheel *wheel,
+                                 const struct lg4ff_multimode_wheel *mmode_wheel,
+                                 const u16 real_product_id)
+{
+       u32 alternate_modes = 0;
+       const char *real_tag = NULL;
+       const char *real_name = NULL;
+
+       if (mmode_wheel) {
+               alternate_modes = mmode_wheel->alternate_modes;
+               real_tag = mmode_wheel->real_tag;
+               real_name = mmode_wheel->real_name;
+       }
+
+       {
+               struct lg4ff_wheel_data t_wdata =  { .product_id = wheel->product_id,
+                                                    .real_product_id = real_product_id,
+                                                    .min_range = wheel->min_range,
+                                                    .max_range = wheel->max_range,
+                                                    .set_range = wheel->set_range,
+                                                    .alternate_modes = alternate_modes,
+                                                    .real_tag = real_tag,
+                                                    .real_name = real_name };
+
+               memcpy(wdata, &t_wdata, sizeof(t_wdata));
+       }
+}
+
 static int lg4ff_play(struct input_dev *dev, void *data, struct ff_effect *effect)
 {
        struct hid_device *hid = input_get_drvdata(dev);
-       struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
-       struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
        struct lg4ff_device_entry *entry;
        struct lg_drv_data *drv_data;
        unsigned long flags;
-       s32 *value = report->field[0]->value;
+       s32 *value;
        int x;
 
        drv_data = hid_get_drvdata(hid);
@@ -321,6 +348,7 @@ static int lg4ff_play(struct input_dev *dev, void *data, struct ff_effect *effec
                hid_err(hid, "Device properties not found!\n");
                return -EINVAL;
        }
+       value = entry->report->field[0]->value;
 
 #define CLAMP(x) do { if (x < 0) x = 0; else if (x > 0xff) x = 0xff; } while (0)
 
@@ -340,7 +368,7 @@ static int lg4ff_play(struct input_dev *dev, void *data, struct ff_effect *effec
                        value[5] = 0x00;
                        value[6] = 0x00;
 
-                       hid_hw_request(hid, report, HID_REQ_SET_REPORT);
+                       hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT);
                        spin_unlock_irqrestore(&entry->report_lock, flags);
                        return 0;
                }
@@ -353,7 +381,7 @@ static int lg4ff_play(struct input_dev *dev, void *data, struct ff_effect *effec
                value[5] = 0x00;
                value[6] = 0x00;
 
-               hid_hw_request(hid, report, HID_REQ_SET_REPORT);
+               hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT);
                spin_unlock_irqrestore(&entry->report_lock, flags);
                break;
        }
@@ -384,6 +412,7 @@ static void lg4ff_set_autocenter_default(struct input_dev *dev, u16 magnitude)
                hid_err(hid, "Device properties not found!\n");
                return;
        }
+       value = entry->report->field[0]->value;
 
        /* De-activate Auto-Center */
        spin_lock_irqsave(&entry->report_lock, flags);
@@ -396,7 +425,7 @@ static void lg4ff_set_autocenter_default(struct input_dev *dev, u16 magnitude)
                value[5] = 0x00;
                value[6] = 0x00;
 
-               hid_hw_request(hid, report, HID_REQ_SET_REPORT);
+               hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT);
                spin_unlock_irqrestore(&entry->report_lock, flags);
                return;
        }
@@ -427,7 +456,7 @@ static void lg4ff_set_autocenter_default(struct input_dev *dev, u16 magnitude)
        value[5] = 0x00;
        value[6] = 0x00;
 
-       hid_hw_request(hid, report, HID_REQ_SET_REPORT);
+       hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT);
 
        /* Activate Auto-Center */
        value[0] = 0x14;
@@ -438,7 +467,7 @@ static void lg4ff_set_autocenter_default(struct input_dev *dev, u16 magnitude)
        value[5] = 0x00;
        value[6] = 0x00;
 
-       hid_hw_request(hid, report, HID_REQ_SET_REPORT);
+       hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT);
        spin_unlock_irqrestore(&entry->report_lock, flags);
 }
 
@@ -446,12 +475,10 @@ static void lg4ff_set_autocenter_default(struct input_dev *dev, u16 magnitude)
 static void lg4ff_set_autocenter_ffex(struct input_dev *dev, u16 magnitude)
 {
        struct hid_device *hid = input_get_drvdata(dev);
-       struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
-       struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
        struct lg4ff_device_entry *entry;
        struct lg_drv_data *drv_data;
        unsigned long flags;
-       s32 *value = report->field[0]->value;
+       s32 *value;
        magnitude = magnitude * 90 / 65535;
 
        drv_data = hid_get_drvdata(hid);
@@ -465,6 +492,7 @@ static void lg4ff_set_autocenter_ffex(struct input_dev *dev, u16 magnitude)
                hid_err(hid, "Device properties not found!\n");
                return;
        }
+       value = entry->report->field[0]->value;
 
        spin_lock_irqsave(&entry->report_lock, flags);
        value[0] = 0xfe;
@@ -475,19 +503,17 @@ static void lg4ff_set_autocenter_ffex(struct input_dev *dev, u16 magnitude)
        value[5] = 0x00;
        value[6] = 0x00;
 
-       hid_hw_request(hid, report, HID_REQ_SET_REPORT);
+       hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT);
        spin_unlock_irqrestore(&entry->report_lock, flags);
 }
 
 /* Sends command to set range compatible with G25/G27/Driving Force GT */
 static void lg4ff_set_range_g25(struct hid_device *hid, u16 range)
 {
-       struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
-       struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
        struct lg4ff_device_entry *entry;
        struct lg_drv_data *drv_data;
        unsigned long flags;
-       s32 *value = report->field[0]->value;
+       s32 *value;
 
        drv_data = hid_get_drvdata(hid);
        if (!drv_data) {
@@ -500,7 +526,7 @@ static void lg4ff_set_range_g25(struct hid_device *hid, u16 range)
                hid_err(hid, "Device properties not found!\n");
                return;
        }
-
+       value = entry->report->field[0]->value;
        dbg_hid("G25/G27/DFGT: setting range to %u\n", range);
 
        spin_lock_irqsave(&entry->report_lock, flags);
@@ -512,20 +538,18 @@ static void lg4ff_set_range_g25(struct hid_device *hid, u16 range)
        value[5] = 0x00;
        value[6] = 0x00;
 
-       hid_hw_request(hid, report, HID_REQ_SET_REPORT);
+       hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT);
        spin_unlock_irqrestore(&entry->report_lock, flags);
 }
 
 /* Sends commands to set range compatible with Driving Force Pro wheel */
 static void lg4ff_set_range_dfp(struct hid_device *hid, u16 range)
 {
-       struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
-       struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
-       int start_left, start_right, full_range;
        struct lg4ff_device_entry *entry;
        struct lg_drv_data *drv_data;
        unsigned long flags;
-       s32 *value = report->field[0]->value;
+       int start_left, start_right, full_range;
+       s32 *value;
 
        drv_data = hid_get_drvdata(hid);
        if (!drv_data) {
@@ -538,7 +562,7 @@ static void lg4ff_set_range_dfp(struct hid_device *hid, u16 range)
                hid_err(hid, "Device properties not found!\n");
                return;
        }
-
+       value = entry->report->field[0]->value;
        dbg_hid("Driving Force Pro: setting range to %u\n", range);
 
        /* Prepare "coarse" limit command */
@@ -552,13 +576,13 @@ static void lg4ff_set_range_dfp(struct hid_device *hid, u16 range)
        value[6] = 0x00;
 
        if (range > 200) {
-               report->field[0]->value[1] = 0x03;
+               value[1] = 0x03;
                full_range = 900;
        } else {
-               report->field[0]->value[1] = 0x02;
+               value[1] = 0x02;
                full_range = 200;
        }
-       hid_hw_request(hid, report, HID_REQ_SET_REPORT);
+       hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT);
 
        /* Prepare "fine" limit command */
        value[0] = 0x81;
@@ -570,7 +594,7 @@ static void lg4ff_set_range_dfp(struct hid_device *hid, u16 range)
        value[6] = 0x00;
 
        if (range == 200 || range == 900) {     /* Do not apply any fine limit */
-               hid_hw_request(hid, report, HID_REQ_SET_REPORT);
+               hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT);
                spin_unlock_irqrestore(&entry->report_lock, flags);
                return;
        }
@@ -585,7 +609,7 @@ static void lg4ff_set_range_dfp(struct hid_device *hid, u16 range)
        value[5] = (start_right & 0xe) << 4 | (start_left & 0xe);
        value[6] = 0xff;
 
-       hid_hw_request(hid, report, HID_REQ_SET_REPORT);
+       hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT);
        spin_unlock_irqrestore(&entry->report_lock, flags);
 }
 
@@ -648,12 +672,10 @@ static const struct lg4ff_compat_mode_switch *lg4ff_get_mode_switch_command(cons
 
 static int lg4ff_switch_compatibility_mode(struct hid_device *hid, const struct lg4ff_compat_mode_switch *s)
 {
-       struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
-       struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
        struct lg4ff_device_entry *entry;
        struct lg_drv_data *drv_data;
        unsigned long flags;
-       s32 *value = report->field[0]->value;
+       s32 *value;
        u8 i;
 
        drv_data = hid_get_drvdata(hid);
@@ -667,6 +689,7 @@ static int lg4ff_switch_compatibility_mode(struct hid_device *hid, const struct
                hid_err(hid, "Device properties not found!\n");
                return -EINVAL;
        }
+       value = entry->report->field[0]->value;
 
        spin_lock_irqsave(&entry->report_lock, flags);
        for (i = 0; i < s->cmd_count; i++) {
@@ -675,7 +698,7 @@ static int lg4ff_switch_compatibility_mode(struct hid_device *hid, const struct
                for (j = 0; j < 7; j++)
                        value[j] = s->cmd[j + (7*i)];
 
-               hid_hw_request(hid, report, HID_REQ_SET_REPORT);
+               hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT);
        }
        spin_unlock_irqrestore(&entry->report_lock, flags);
        hid_hw_wait(hid);
@@ -916,12 +939,10 @@ static DEVICE_ATTR(real_id, S_IRUGO, lg4ff_real_id_show, lg4ff_real_id_store);
 #ifdef CONFIG_LEDS_CLASS
 static void lg4ff_set_leds(struct hid_device *hid, u8 leds)
 {
-       struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
-       struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
        struct lg_drv_data *drv_data;
        struct lg4ff_device_entry *entry;
        unsigned long flags;
-       s32 *value = report->field[0]->value;
+       s32 *value;
 
        drv_data = hid_get_drvdata(hid);
        if (!drv_data) {
@@ -934,6 +955,7 @@ static void lg4ff_set_leds(struct hid_device *hid, u8 leds)
                hid_err(hid, "Device properties not found!\n");
                return;
        }
+       value = entry->report->field[0]->value;
 
        spin_lock_irqsave(&entry->report_lock, flags);
        value[0] = 0xf8;
@@ -943,7 +965,7 @@ static void lg4ff_set_leds(struct hid_device *hid, u8 leds)
        value[4] = 0x00;
        value[5] = 0x00;
        value[6] = 0x00;
-       hid_hw_request(hid, report, HID_REQ_SET_REPORT);
+       hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT);
        spin_unlock_irqrestore(&entry->report_lock, flags);
 }
 
@@ -1104,8 +1126,11 @@ int lg4ff_init(struct hid_device *hid)
 {
        struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
        struct input_dev *dev = hidinput->input;
+       struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
+       struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
        const struct usb_device_descriptor *udesc = &(hid_to_usb_dev(hid)->descriptor);
        const u16 bcdDevice = le16_to_cpu(udesc->bcdDevice);
+       const struct lg4ff_multimode_wheel *mmode_wheel = NULL;
        struct lg4ff_device_entry *entry;
        struct lg_drv_data *drv_data;
        int error, i, j;
@@ -1125,6 +1150,7 @@ int lg4ff_init(struct hid_device *hid)
        if (!entry)
                return -ENOMEM;
        spin_lock_init(&entry->report_lock);
+       entry->report = report;
        drv_data->device_props = entry;
 
        /* Check if a multimode wheel has been connected and
@@ -1180,17 +1206,12 @@ int lg4ff_init(struct hid_device *hid)
        if (error)
                goto err_init;
 
-       entry->wdata.product_id = lg4ff_devices[i].product_id;
-       entry->wdata.real_product_id = real_product_id;
-       entry->wdata.min_range = lg4ff_devices[i].min_range;
-       entry->wdata.max_range = lg4ff_devices[i].max_range;
-       entry->wdata.set_range = lg4ff_devices[i].set_range;
+       /* Initialize device properties */
        if (mmode_ret == LG4FF_MMODE_IS_MULTIMODE) {
                BUG_ON(mmode_idx == -1);
-               entry->wdata.alternate_modes = lg4ff_multimode_wheels[mmode_idx].alternate_modes;
-               entry->wdata.real_tag = lg4ff_multimode_wheels[mmode_idx].real_tag;
-               entry->wdata.real_name = lg4ff_multimode_wheels[mmode_idx].real_name;
+               mmode_wheel = &lg4ff_multimode_wheels[mmode_idx];
        }
+       lg4ff_init_wheel_data(&entry->wdata, &lg4ff_devices[i], mmode_wheel, real_product_id);
 
        /* Check if autocentering is available and
         * set the centering force to zero by default */
@@ -1208,15 +1229,14 @@ int lg4ff_init(struct hid_device *hid)
        /* Create sysfs interface */
        error = device_create_file(&hid->dev, &dev_attr_range);
        if (error)
-               goto err_init;
+               hid_warn(hid, "Unable to create sysfs interface for \"range\", errno %d\n", error);
        if (mmode_ret == LG4FF_MMODE_IS_MULTIMODE) {
                error = device_create_file(&hid->dev, &dev_attr_real_id);
                if (error)
-                       goto err_init;
-
+                       hid_warn(hid, "Unable to create sysfs interface for \"real_id\", errno %d\n", error);
                error = device_create_file(&hid->dev, &dev_attr_alternate_modes);
                if (error)
-                       goto err_init;
+                       hid_warn(hid, "Unable to create sysfs interface for \"alternate_modes\", errno %d\n", error);
        }
        dbg_hid("sysfs interface created\n");