From patchwork Thu Aug 4 14:20:40 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Michal_Mal=C3=BD?= X-Patchwork-Id: 1035842 X-Patchwork-Delegate: jikos@jikos.cz Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by demeter2.kernel.org (8.14.4/8.14.4) with ESMTP id p74EGEV3028006 for ; Thu, 4 Aug 2011 14:20:45 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751510Ab1HDOUp (ORCPT ); Thu, 4 Aug 2011 10:20:45 -0400 Received: from mail-fx0-f46.google.com ([209.85.161.46]:44791 "EHLO mail-fx0-f46.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751312Ab1HDOUo convert rfc822-to-8bit (ORCPT ); Thu, 4 Aug 2011 10:20:44 -0400 Received: by fxh19 with SMTP id 19so1796435fxh.19 for ; Thu, 04 Aug 2011 07:20:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=from:to:cc:subject:date:message-id:user-agent:mime-version :content-transfer-encoding:content-type; bh=gH5fPIzGJHO6L+gMZsCrZwHx103CQbi+zOxLommFVEw=; b=uQ2Hbcv9fNKr7ftWCM/N5tGpSyk9Ds1ipUDUgsf8D3MYwogYiFR61EbksAFd8pj2XV zAjplskDbsbFQGKDrAuGxcb+tTY874BGX1OUM7d7EggZjL+XlahnFyXPThvlQydjqfhT Xa8GylaLTxrL4uZO7sgdpQG+zSdQFnqzkhx7I= Received: by 10.223.145.16 with SMTP id b16mr1240805fav.128.1312467642782; Thu, 04 Aug 2011 07:20:42 -0700 (PDT) Received: from qosmio-x300.localnet (18.53.broadband14.iol.cz [90.181.53.18]) by mx.google.com with ESMTPS id f27sm1263137fak.31.2011.08.04.07.20.41 (version=SSLv3 cipher=OTHER); Thu, 04 Aug 2011 07:20:42 -0700 (PDT) From: Michal =?ISO-8859-1?Q?Mal=FD?= To: jkosina@suse.cz Cc: linux-input@vger.kernel.org, simon@mungewell.org Subject: [v3 3/6] HID: lg4ff - Add range setting support and sysfs interface Date: Thu, 04 Aug 2011 16:20:40 +0200 Message-ID: <5359413.nh1PXkDRGN@qosmio-x300> User-Agent: KMail/4.7.0 (Linux/3.0.0-ARCHMOD; KDE/4.7.0; x86_64; ; ) MIME-Version: 1.0 Sender: linux-input-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-input@vger.kernel.org X-Greylist: IP, sender and recipient auto-whitelisted, not delayed by milter-greylist-4.2.6 (demeter2.kernel.org [140.211.167.43]); Thu, 04 Aug 2011 14:20:45 +0000 (UTC) (Hopefully) fixed wordwrapping Signed-off-by: Michal MalĂ˝ --- -- To unsubscribe from this list: send the line "unsubscribe linux-input" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html diff --git a/drivers/hid/hid-lg.c b/drivers/hid/hid-lg.c index e0ae4a0..e7a7bd1 100644 --- a/drivers/hid/hid-lg.c +++ b/drivers/hid/hid-lg.c @@ -406,6 +406,15 @@ err_free: return ret; } +static void lg_remove(struct hid_device *hdev) +{ + unsigned long quirks = (unsigned long)hid_get_drvdata(hdev); + if(quirks & LG_FF4) + lg4ff_deinit(hdev); + + hid_hw_stop(hdev); +} + static const struct hid_device_id lg_devices[] = { { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_MX3000_RECEIVER), .driver_data = LG_RDESC | LG_WIRELESS }, @@ -481,6 +490,7 @@ static struct hid_driver lg_driver = { .input_mapped = lg_input_mapped, .event = lg_event, .probe = lg_probe, + .remove = lg_remove, }; static int __init lg_init(void) diff --git a/drivers/hid/hid-lg.h b/drivers/hid/hid-lg.h index b0100ba..3a32959 100644 --- a/drivers/hid/hid-lg.h +++ b/drivers/hid/hid-lg.h @@ -21,8 +21,10 @@ static inline int lg3ff_init(struct hid_device *hdev) { return -1; } #ifdef CONFIG_LOGIWII_FF int lg4ff_init(struct hid_device *hdev); +int lg4ff_deinit(struct hid_device *hdev); #else static inline int lg4ff_init(struct hid_device *hdev) { return -1; } +static inline int lg4ff_deinit(struct hid_device *hdev) { return -1; } #endif #endif diff --git a/drivers/hid/hid-lg4ff.c b/drivers/hid/hid-lg4ff.c index acef0cf..1ee78d3 100644 --- a/drivers/hid/hid-lg4ff.c +++ b/drivers/hid/hid-lg4ff.c @@ -42,6 +42,29 @@ #define G27_REV_MAJ 0x12 #define G27_REV_MIN 0x38 +#define to_hid_device(pdev) container_of(pdev, struct hid_device, dev) + +static void hid_lg4ff_set_range_dfp(struct hid_device *hid, u16 range); +static void hid_lg4ff_set_range_g25(struct hid_device *hid, u16 range); +static ssize_t lg4ff_range_show(struct device *dev, struct device_attribute *attr, char *buf); +static ssize_t lg4ff_range_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count); + +static DEVICE_ATTR(range, S_IRWXU | S_IRWXG | S_IRWXO, lg4ff_range_show, lg4ff_range_store); + +static bool list_inited; + +struct lg4ff_device_entry { + char *device_id; /* Use name in respective kobject structure's address as the ID */ + __u16 range; + __u16 min_range; + __u16 max_range; + __u8 leds; + struct list_head list; + void (*set_range)(struct hid_device *hid, u16 range); +}; + +static struct lg4ff_device_entry device_list; + static const signed short lg4ff_wheel_effects[] = { FF_CONSTANT, FF_AUTOCENTER, @@ -53,17 +76,18 @@ struct lg4ff_wheel { const signed short *ff_effects; const __u16 min_range; const __u16 max_range; + void (*set_range)(struct hid_device *hid, u16 range); }; static const struct lg4ff_wheel lg4ff_devices[] = { - {USB_DEVICE_ID_LOGITECH_WHEEL, lg4ff_wheel_effects, 40, 270}, - {USB_DEVICE_ID_LOGITECH_MOMO_WHEEL, lg4ff_wheel_effects, 40, 270}, - {USB_DEVICE_ID_LOGITECH_DFP_WHEEL, lg4ff_wheel_effects, 40, 900}, - {USB_DEVICE_ID_LOGITECH_G25_WHEEL, lg4ff_wheel_effects, 40, 900}, - {USB_DEVICE_ID_LOGITECH_DFGT_WHEEL, lg4ff_wheel_effects, 40, 900}, - {USB_DEVICE_ID_LOGITECH_G27_WHEEL, lg4ff_wheel_effects, 40, 900}, - {USB_DEVICE_ID_LOGITECH_MOMO_WHEEL2, lg4ff_wheel_effects, 40, 270}, - {USB_DEVICE_ID_LOGITECH_WII_WHEEL, lg4ff_wheel_effects, 40, 270} + {USB_DEVICE_ID_LOGITECH_WHEEL, lg4ff_wheel_effects, 40, 270, NULL}, + {USB_DEVICE_ID_LOGITECH_MOMO_WHEEL, lg4ff_wheel_effects, 40, 270, NULL}, + {USB_DEVICE_ID_LOGITECH_DFP_WHEEL, lg4ff_wheel_effects, 40, 900, hid_lg4ff_set_range_dfp}, + {USB_DEVICE_ID_LOGITECH_G25_WHEEL, lg4ff_wheel_effects, 40, 900, hid_lg4ff_set_range_g25}, + {USB_DEVICE_ID_LOGITECH_DFGT_WHEEL, lg4ff_wheel_effects, 40, 900, hid_lg4ff_set_range_g25}, + {USB_DEVICE_ID_LOGITECH_G27_WHEEL, lg4ff_wheel_effects, 40, 900, hid_lg4ff_set_range_g25}, + {USB_DEVICE_ID_LOGITECH_MOMO_WHEEL2, lg4ff_wheel_effects, 40, 270, NULL}, + {USB_DEVICE_ID_LOGITECH_WII_WHEEL, lg4ff_wheel_effects, 40, 270, NULL} }; struct lg4ff_native_cmd { @@ -106,8 +130,7 @@ static const struct lg4ff_usb_revision lg4ff_revs[] = { {G27_REV_MAJ, G27_REV_MIN, &native_g27}, /* G27 */ }; -static int hid_lg4ff_play(struct input_dev *dev, void *data, - struct ff_effect *effect) +static int hid_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; @@ -151,6 +174,77 @@ static void hid_lg4ff_set_autocenter(struct input_dev *dev, u16 magnitude) usbhid_submit_report(hid, report, USB_DIR_OUT); } +/* Sends command to set range compatible with G25/G27/Driving Force GT */ +static void hid_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); + dbg_hid("G25/G27/DFGT: setting range to %u\n", range); + + report->field[0]->value[0] = 0xf8; + report->field[0]->value[1] = 0x81; + report->field[0]->value[2] = range & 0x00ff; + report->field[0]->value[3] = (range & 0xff00) >> 8; + report->field[0]->value[4] = 0x00; + report->field[0]->value[5] = 0x00; + report->field[0]->value[6] = 0x00; + + usbhid_submit_report(hid, report, USB_DIR_OUT); +} + +/* Sends commands to set range compatible with Driving Force Pro wheel */ +static void hid_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; + dbg_hid("Driving Force Pro: setting range to %u\n", range); + + /* Prepare "coarse" limit command */ + report->field[0]->value[0] = 0xf8; + report->field[0]->value[1] = 0x00; /* Set later */ + report->field[0]->value[2] = 0x00; + report->field[0]->value[3] = 0x00; + report->field[0]->value[4] = 0x00; + report->field[0]->value[5] = 0x00; + report->field[0]->value[6] = 0x00; + + if (range > 200) { + report->field[0]->value[1] = 0x03; + full_range = 900; + } else { + report->field[0]->value[1] = 0x02; + full_range = 200; + } + usbhid_submit_report(hid, report, USB_DIR_OUT); + + /* Prepare "fine" limit command */ + report->field[0]->value[0] = 0x81; + report->field[0]->value[1] = 0x0b; + report->field[0]->value[2] = 0x00; + report->field[0]->value[3] = 0x00; + report->field[0]->value[4] = 0x00; + report->field[0]->value[5] = 0x00; + report->field[0]->value[6] = 0x00; + + if (range == 200 || range == 900) { /* Do not apply any fine limit */ + usbhid_submit_report(hid, report, USB_DIR_OUT); + return; + } + + /* Construct fine limit command */ + start_left = (((full_range - range + 1) * 2047) / full_range); + start_right = 0xfff - start_left; + + report->field[0]->value[2] = start_left >> 4; + report->field[0]->value[3] = start_right >> 4; + report->field[0]->value[4] = 0xff; + report->field[0]->value[5] = (start_right & 0xe) << 4 | (start_left & 0xe); + report->field[0]->value[6] = 0xff; + + usbhid_submit_report(hid, report, USB_DIR_OUT); +} + static void hid_lg4ff_switch_native(struct hid_device *hid, const struct lg4ff_native_cmd *cmd) { struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list; @@ -166,6 +260,60 @@ static void hid_lg4ff_switch_native(struct hid_device *hid, const struct lg4ff_n } } +/* Read current range and display it in terminal */ +static ssize_t lg4ff_range_show(struct device *dev, struct device_attribute *attr, char *buf) +{ + struct lg4ff_device_entry *entry = 0; + struct list_head *h; + struct hid_device *hid = to_hid_device(dev); + size_t count; + + list_for_each(h, &device_list.list) { + entry = list_entry(h, struct lg4ff_device_entry, list); + if (strcmp(entry->device_id, (&hid->dev)->kobj.name) == 0) + break; + } + if (h == &device_list.list) { + dbg_hid("Device not found!"); + return 0; + } + + count = scnprintf(buf, PAGE_SIZE, "%u\n", entry->range); + return count; +} + +/* Set range to user specified value, call appropriate function + * according to the type of the wheel */ +static ssize_t lg4ff_range_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) +{ + struct lg4ff_device_entry *entry = 0; + struct list_head *h; + struct hid_device *hid = to_hid_device(dev); + __u16 range = simple_strtoul(buf, NULL, 10); + + list_for_each(h, &device_list.list) { + entry = list_entry(h, struct lg4ff_device_entry, list); + if (strcmp(entry->device_id, (&hid->dev)->kobj.name) == 0) + break; + } + if (h == &device_list.list) { + dbg_hid("Device not found!"); + return count; + } + + if (range == 0) + range = entry->max_range; + + /* Check if the wheel supports range setting + * and that the range is within limits for the wheel */ + if (entry->set_range != NULL && range >= entry->min_range && range <= entry->max_range) { + entry->set_range(hid, range); + entry->range = range; + } + + return count; +} + int lg4ff_init(struct hid_device *hid) { struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list); @@ -173,7 +321,8 @@ int lg4ff_init(struct hid_device *hid) struct input_dev *dev = hidinput->input; struct hid_report *report; struct hid_field *field; - struct usb_device_descriptor *udesc = 0; + struct lg4ff_device_entry *entry; + struct usb_device_descriptor *udesc; int error, i, j; __u16 bcdDevice, rev_maj, rev_min; @@ -195,7 +344,7 @@ int lg4ff_init(struct hid_device *hid) hid_err(hid, "NULL field\n"); return -1; } - + /* Check what wheel has been connected */ for (i = 0; i < ARRAY_SIZE(lg4ff_devices); i++) { if (hid->product == lg4ff_devices[i].product_id) { @@ -244,7 +393,66 @@ int lg4ff_init(struct hid_device *hid) if (test_bit(FF_AUTOCENTER, dev->ffbit)) dev->ff->set_autocenter = hid_lg4ff_set_autocenter; + /* Initialize device_list if this is the first device to handle by lg4ff */ + if (!list_inited) { + INIT_LIST_HEAD(&device_list.list); + list_inited = 1; + } + + /* Add the device to device_list */ + entry = (struct lg4ff_device_entry *)kzalloc(sizeof(struct lg4ff_device_entry), GFP_KERNEL); + if (!entry) { + hid_err(hid, "Cannot add device, insufficient memory.\n"); + return -ENOMEM; + } + entry->device_id = (char *)kzalloc(strlen((&hid->dev)->kobj.name) + 1, GFP_KERNEL); + if (!entry->device_id) { + hid_err(hid, "Cannot set device_id, insufficient memory.\n"); + return -ENOMEM; + } + strcpy(entry->device_id, (&hid->dev)->kobj.name); + entry->min_range = lg4ff_devices[i].min_range; + entry->max_range = lg4ff_devices[i].max_range; + entry->set_range = lg4ff_devices[i].set_range; + list_add(&entry->list, &device_list.list); + + /* Create sysfs interface */ + error = device_create_file(&hid->dev, &dev_attr_range); + if (error) + return error; + dbg_hid("sysfs interface created\n"); + + /* Set the maximum range to start with */ + entry->range = entry->max_range; + if (entry->set_range != NULL) + entry->set_range(hid, entry->range); + hid_info(hid, "Force feedback for Logitech Speed Force Wireless by Simon Wood \n"); return 0; } +int lg4ff_deinit(struct hid_device *hid) +{ + bool found = 0; + struct lg4ff_device_entry *entry; + struct list_head *h, *g; + list_for_each_safe(h, g, &device_list.list) { + entry = list_entry(h, struct lg4ff_device_entry, list); + if (strcmp(entry->device_id, (&hid->dev)->kobj.name) == 0) { + list_del(h); + kfree(entry->device_id); + kfree(entry); + found = 1; + break; + } + } + + if (!found) { + dbg_hid("Device entry not found!\n"); + return -1; + } + + device_remove_file(&hid->dev, &dev_attr_range); + dbg_hid("Device successfully unregistered\n"); + return 0; +}