aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/hid/hid-lg4ff.c
diff options
context:
space:
mode:
authorMichal Malý <madcatxster@gmail.com>2011-08-04 10:20:40 -0400
committerJiri Kosina <jkosina@suse.cz>2011-08-04 10:45:55 -0400
commit30bb75d71b3732c0adb6297815288ce0fb9cc04c (patch)
tree7666cc033dbf3a4239803b43702f2d4a57447a8e /drivers/hid/hid-lg4ff.c
parent96440c8a00e22e541135dee2eba9f3e7d8195f65 (diff)
HID: lg4ff - Add range setting support and sysfs interface
Wheel range of certain Logitech wheels - namely Driving Force GT, Driving Force Pro, G25 and G27 can be adjusted. Minimu is 40 degrees, maximum 900. DFGT, G25 and G27 all use a common command, DFP uses another one. Range can be set from userspace by writing to "/sys/module/hid_logitech/drivers/hid:logitech/<dev>range". The driver use list to store range of each connected wheel; it's not possible to use driver_data in hid_device struct as it's already b hig-lg driver. Signed-off-by: Michal Malý <madcatxster@gmail.com> Signed-off-by: Simon Wood <simon@mungewell.org> Signed-off-by: Jiri Kosina <jkosina@suse.cz>
Diffstat (limited to 'drivers/hid/hid-lg4ff.c')
-rw-r--r--drivers/hid/hid-lg4ff.c232
1 files changed, 220 insertions, 12 deletions
diff --git a/drivers/hid/hid-lg4ff.c b/drivers/hid/hid-lg4ff.c
index 8eb938d7aa02..5c9eef2267ed 100644
--- a/drivers/hid/hid-lg4ff.c
+++ b/drivers/hid/hid-lg4ff.c
@@ -42,6 +42,29 @@
42#define G27_REV_MAJ 0x12 42#define G27_REV_MAJ 0x12
43#define G27_REV_MIN 0x38 43#define G27_REV_MIN 0x38
44 44
45#define to_hid_device(pdev) container_of(pdev, struct hid_device, dev)
46
47static void hid_lg4ff_set_range_dfp(struct hid_device *hid, u16 range);
48static void hid_lg4ff_set_range_g25(struct hid_device *hid, u16 range);
49static ssize_t lg4ff_range_show(struct device *dev, struct device_attribute *attr, char *buf);
50static ssize_t lg4ff_range_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count);
51
52static DEVICE_ATTR(range, S_IRWXU | S_IRWXG | S_IRWXO, lg4ff_range_show, lg4ff_range_store);
53
54static bool list_inited;
55
56struct lg4ff_device_entry {
57 char *device_id; /* Use name in respective kobject structure's address as the ID */
58 __u16 range;
59 __u16 min_range;
60 __u16 max_range;
61 __u8 leds;
62 struct list_head list;
63 void (*set_range)(struct hid_device *hid, u16 range);
64};
65
66static struct lg4ff_device_entry device_list;
67
45static const signed short lg4ff_wheel_effects[] = { 68static const signed short lg4ff_wheel_effects[] = {
46 FF_CONSTANT, 69 FF_CONSTANT,
47 FF_AUTOCENTER, 70 FF_AUTOCENTER,
@@ -53,17 +76,18 @@ struct lg4ff_wheel {
53 const signed short *ff_effects; 76 const signed short *ff_effects;
54 const __u16 min_range; 77 const __u16 min_range;
55 const __u16 max_range; 78 const __u16 max_range;
79 void (*set_range)(struct hid_device *hid, u16 range);
56}; 80};
57 81
58static const struct lg4ff_wheel lg4ff_devices[] = { 82static const struct lg4ff_wheel lg4ff_devices[] = {
59 {USB_DEVICE_ID_LOGITECH_WHEEL, lg4ff_wheel_effects, 40, 270}, 83 {USB_DEVICE_ID_LOGITECH_WHEEL, lg4ff_wheel_effects, 40, 270, NULL},
60 {USB_DEVICE_ID_LOGITECH_MOMO_WHEEL, lg4ff_wheel_effects, 40, 270}, 84 {USB_DEVICE_ID_LOGITECH_MOMO_WHEEL, lg4ff_wheel_effects, 40, 270, NULL},
61 {USB_DEVICE_ID_LOGITECH_DFP_WHEEL, lg4ff_wheel_effects, 40, 900}, 85 {USB_DEVICE_ID_LOGITECH_DFP_WHEEL, lg4ff_wheel_effects, 40, 900, hid_lg4ff_set_range_dfp},
62 {USB_DEVICE_ID_LOGITECH_G25_WHEEL, lg4ff_wheel_effects, 40, 900}, 86 {USB_DEVICE_ID_LOGITECH_G25_WHEEL, lg4ff_wheel_effects, 40, 900, hid_lg4ff_set_range_g25},
63 {USB_DEVICE_ID_LOGITECH_DFGT_WHEEL, lg4ff_wheel_effects, 40, 900}, 87 {USB_DEVICE_ID_LOGITECH_DFGT_WHEEL, lg4ff_wheel_effects, 40, 900, hid_lg4ff_set_range_g25},
64 {USB_DEVICE_ID_LOGITECH_G27_WHEEL, lg4ff_wheel_effects, 40, 900}, 88 {USB_DEVICE_ID_LOGITECH_G27_WHEEL, lg4ff_wheel_effects, 40, 900, hid_lg4ff_set_range_g25},
65 {USB_DEVICE_ID_LOGITECH_MOMO_WHEEL2, lg4ff_wheel_effects, 40, 270}, 89 {USB_DEVICE_ID_LOGITECH_MOMO_WHEEL2, lg4ff_wheel_effects, 40, 270, NULL},
66 {USB_DEVICE_ID_LOGITECH_WII_WHEEL, lg4ff_wheel_effects, 40, 270} 90 {USB_DEVICE_ID_LOGITECH_WII_WHEEL, lg4ff_wheel_effects, 40, 270, NULL}
67}; 91};
68 92
69struct lg4ff_native_cmd { 93struct lg4ff_native_cmd {
@@ -106,8 +130,7 @@ static const struct lg4ff_usb_revision lg4ff_revs[] = {
106 {G27_REV_MAJ, G27_REV_MIN, &native_g27}, /* G27 */ 130 {G27_REV_MAJ, G27_REV_MIN, &native_g27}, /* G27 */
107}; 131};
108 132
109static int hid_lg4ff_play(struct input_dev *dev, void *data, 133static int hid_lg4ff_play(struct input_dev *dev, void *data, struct ff_effect *effect)
110 struct ff_effect *effect)
111{ 134{
112 struct hid_device *hid = input_get_drvdata(dev); 135 struct hid_device *hid = input_get_drvdata(dev);
113 struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list; 136 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)
151 usbhid_submit_report(hid, report, USB_DIR_OUT); 174 usbhid_submit_report(hid, report, USB_DIR_OUT);
152} 175}
153 176
177/* Sends command to set range compatible with G25/G27/Driving Force GT */
178static void hid_lg4ff_set_range_g25(struct hid_device *hid, u16 range)
179{
180 struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
181 struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
182 dbg_hid("G25/G27/DFGT: setting range to %u\n", range);
183
184 report->field[0]->value[0] = 0xf8;
185 report->field[0]->value[1] = 0x81;
186 report->field[0]->value[2] = range & 0x00ff;
187 report->field[0]->value[3] = (range & 0xff00) >> 8;
188 report->field[0]->value[4] = 0x00;
189 report->field[0]->value[5] = 0x00;
190 report->field[0]->value[6] = 0x00;
191
192 usbhid_submit_report(hid, report, USB_DIR_OUT);
193}
194
195/* Sends commands to set range compatible with Driving Force Pro wheel */
196static void hid_lg4ff_set_range_dfp(struct hid_device *hid, __u16 range)
197{
198 struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
199 struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
200 int start_left, start_right, full_range;
201 dbg_hid("Driving Force Pro: setting range to %u\n", range);
202
203 /* Prepare "coarse" limit command */
204 report->field[0]->value[0] = 0xf8;
205 report->field[0]->value[1] = 0x00; /* Set later */
206 report->field[0]->value[2] = 0x00;
207 report->field[0]->value[3] = 0x00;
208 report->field[0]->value[4] = 0x00;
209 report->field[0]->value[5] = 0x00;
210 report->field[0]->value[6] = 0x00;
211
212 if (range > 200) {
213 report->field[0]->value[1] = 0x03;
214 full_range = 900;
215 } else {
216 report->field[0]->value[1] = 0x02;
217 full_range = 200;
218 }
219 usbhid_submit_report(hid, report, USB_DIR_OUT);
220
221 /* Prepare "fine" limit command */
222 report->field[0]->value[0] = 0x81;
223 report->field[0]->value[1] = 0x0b;
224 report->field[0]->value[2] = 0x00;
225 report->field[0]->value[3] = 0x00;
226 report->field[0]->value[4] = 0x00;
227 report->field[0]->value[5] = 0x00;
228 report->field[0]->value[6] = 0x00;
229
230 if (range == 200 || range == 900) { /* Do not apply any fine limit */
231 usbhid_submit_report(hid, report, USB_DIR_OUT);
232 return;
233 }
234
235 /* Construct fine limit command */
236 start_left = (((full_range - range + 1) * 2047) / full_range);
237 start_right = 0xfff - start_left;
238
239 report->field[0]->value[2] = start_left >> 4;
240 report->field[0]->value[3] = start_right >> 4;
241 report->field[0]->value[4] = 0xff;
242 report->field[0]->value[5] = (start_right & 0xe) << 4 | (start_left & 0xe);
243 report->field[0]->value[6] = 0xff;
244
245 usbhid_submit_report(hid, report, USB_DIR_OUT);
246}
247
154static void hid_lg4ff_switch_native(struct hid_device *hid, const struct lg4ff_native_cmd *cmd) 248static void hid_lg4ff_switch_native(struct hid_device *hid, const struct lg4ff_native_cmd *cmd)
155{ 249{
156 struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list; 250 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
166 } 260 }
167} 261}
168 262
263/* Read current range and display it in terminal */
264static ssize_t lg4ff_range_show(struct device *dev, struct device_attribute *attr, char *buf)
265{
266 struct lg4ff_device_entry *entry = 0;
267 struct list_head *h;
268 struct hid_device *hid = to_hid_device(dev);
269 size_t count;
270
271 list_for_each(h, &device_list.list) {
272 entry = list_entry(h, struct lg4ff_device_entry, list);
273 if (strcmp(entry->device_id, (&hid->dev)->kobj.name) == 0)
274 break;
275 }
276 if (h == &device_list.list) {
277 dbg_hid("Device not found!");
278 return 0;
279 }
280
281 count = scnprintf(buf, PAGE_SIZE, "%u\n", entry->range);
282 return count;
283}
284
285/* Set range to user specified value, call appropriate function
286 * according to the type of the wheel */
287static ssize_t lg4ff_range_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
288{
289 struct lg4ff_device_entry *entry = 0;
290 struct list_head *h;
291 struct hid_device *hid = to_hid_device(dev);
292 __u16 range = simple_strtoul(buf, NULL, 10);
293
294 list_for_each(h, &device_list.list) {
295 entry = list_entry(h, struct lg4ff_device_entry, list);
296 if (strcmp(entry->device_id, (&hid->dev)->kobj.name) == 0)
297 break;
298 }
299 if (h == &device_list.list) {
300 dbg_hid("Device not found!");
301 return count;
302 }
303
304 if (range == 0)
305 range = entry->max_range;
306
307 /* Check if the wheel supports range setting
308 * and that the range is within limits for the wheel */
309 if (entry->set_range != NULL && range >= entry->min_range && range <= entry->max_range) {
310 entry->set_range(hid, range);
311 entry->range = range;
312 }
313
314 return count;
315}
316
169int lg4ff_init(struct hid_device *hid) 317int lg4ff_init(struct hid_device *hid)
170{ 318{
171 struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list); 319 struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
@@ -173,7 +321,8 @@ int lg4ff_init(struct hid_device *hid)
173 struct input_dev *dev = hidinput->input; 321 struct input_dev *dev = hidinput->input;
174 struct hid_report *report; 322 struct hid_report *report;
175 struct hid_field *field; 323 struct hid_field *field;
176 struct usb_device_descriptor *udesc = 0; 324 struct lg4ff_device_entry *entry;
325 struct usb_device_descriptor *udesc;
177 int error, i, j; 326 int error, i, j;
178 __u16 bcdDevice, rev_maj, rev_min; 327 __u16 bcdDevice, rev_maj, rev_min;
179 328
@@ -195,7 +344,7 @@ int lg4ff_init(struct hid_device *hid)
195 hid_err(hid, "NULL field\n"); 344 hid_err(hid, "NULL field\n");
196 return -1; 345 return -1;
197 } 346 }
198 347
199 /* Check what wheel has been connected */ 348 /* Check what wheel has been connected */
200 for (i = 0; i < ARRAY_SIZE(lg4ff_devices); i++) { 349 for (i = 0; i < ARRAY_SIZE(lg4ff_devices); i++) {
201 if (hid->product == lg4ff_devices[i].product_id) { 350 if (hid->product == lg4ff_devices[i].product_id) {
@@ -244,7 +393,66 @@ int lg4ff_init(struct hid_device *hid)
244 if (test_bit(FF_AUTOCENTER, dev->ffbit)) 393 if (test_bit(FF_AUTOCENTER, dev->ffbit))
245 dev->ff->set_autocenter = hid_lg4ff_set_autocenter; 394 dev->ff->set_autocenter = hid_lg4ff_set_autocenter;
246 395
396 /* Initialize device_list if this is the first device to handle by lg4ff */
397 if (!list_inited) {
398 INIT_LIST_HEAD(&device_list.list);
399 list_inited = 1;
400 }
401
402 /* Add the device to device_list */
403 entry = (struct lg4ff_device_entry *)kzalloc(sizeof(struct lg4ff_device_entry), GFP_KERNEL);
404 if (!entry) {
405 hid_err(hid, "Cannot add device, insufficient memory.\n");
406 return -ENOMEM;
407 }
408 entry->device_id = (char *)kzalloc(strlen((&hid->dev)->kobj.name) + 1, GFP_KERNEL);
409 if (!entry->device_id) {
410 hid_err(hid, "Cannot set device_id, insufficient memory.\n");
411 return -ENOMEM;
412 }
413 strcpy(entry->device_id, (&hid->dev)->kobj.name);
414 entry->min_range = lg4ff_devices[i].min_range;
415 entry->max_range = lg4ff_devices[i].max_range;
416 entry->set_range = lg4ff_devices[i].set_range;
417 list_add(&entry->list, &device_list.list);
418
419 /* Create sysfs interface */
420 error = device_create_file(&hid->dev, &dev_attr_range);
421 if (error)
422 return error;
423 dbg_hid("sysfs interface created\n");
424
425 /* Set the maximum range to start with */
426 entry->range = entry->max_range;
427 if (entry->set_range != NULL)
428 entry->set_range(hid, entry->range);
429
247 hid_info(hid, "Force feedback for Logitech Speed Force Wireless by Simon Wood <simon@mungewell.org>\n"); 430 hid_info(hid, "Force feedback for Logitech Speed Force Wireless by Simon Wood <simon@mungewell.org>\n");
248 return 0; 431 return 0;
249} 432}
250 433
434int lg4ff_deinit(struct hid_device *hid)
435{
436 bool found = 0;
437 struct lg4ff_device_entry *entry;
438 struct list_head *h, *g;
439 list_for_each_safe(h, g, &device_list.list) {
440 entry = list_entry(h, struct lg4ff_device_entry, list);
441 if (strcmp(entry->device_id, (&hid->dev)->kobj.name) == 0) {
442 list_del(h);
443 kfree(entry->device_id);
444 kfree(entry);
445 found = 1;
446 break;
447 }
448 }
449
450 if (!found) {
451 dbg_hid("Device entry not found!\n");
452 return -1;
453 }
454
455 device_remove_file(&hid->dev, &dev_attr_range);
456 dbg_hid("Device successfully unregistered\n");
457 return 0;
458}