Jiri Slaby | 0f22132 | 2008-06-23 22:54:08 +0200 | [diff] [blame] | 1 | /* |
| 2 | * HID driver for some cypress "special" devices |
| 3 | * |
| 4 | * Copyright (c) 1999 Andreas Gal |
| 5 | * Copyright (c) 2000-2005 Vojtech Pavlik <vojtech@suse.cz> |
| 6 | * Copyright (c) 2005 Michael Haboustak <mike-@cinci.rr.com> for Concept2, Inc |
| 7 | * Copyright (c) 2006-2007 Jiri Kosina |
| 8 | * Copyright (c) 2007 Paul Walmsley |
| 9 | * Copyright (c) 2008 Jiri Slaby |
| 10 | */ |
| 11 | |
| 12 | /* |
| 13 | * This program is free software; you can redistribute it and/or modify it |
| 14 | * under the terms of the GNU General Public License as published by the Free |
| 15 | * Software Foundation; either version 2 of the License, or (at your option) |
| 16 | * any later version. |
| 17 | */ |
| 18 | |
| 19 | #include <linux/device.h> |
| 20 | #include <linux/hid.h> |
| 21 | #include <linux/input.h> |
| 22 | #include <linux/module.h> |
| 23 | |
| 24 | #include "hid-ids.h" |
| 25 | |
| 26 | #define CP_RDESC_SWAPPED_MIN_MAX 0x01 |
| 27 | #define CP_2WHEEL_MOUSE_HACK 0x02 |
| 28 | #define CP_2WHEEL_MOUSE_HACK_ON 0x04 |
| 29 | |
| 30 | /* |
| 31 | * Some USB barcode readers from cypress have usage min and usage max in |
| 32 | * the wrong order |
| 33 | */ |
Nikolai Kondrashov | 73e4008 | 2010-08-06 23:03:06 +0400 | [diff] [blame] | 34 | static __u8 *cp_report_fixup(struct hid_device *hdev, __u8 *rdesc, |
| 35 | unsigned int *rsize) |
Jiri Slaby | 0f22132 | 2008-06-23 22:54:08 +0200 | [diff] [blame] | 36 | { |
| 37 | unsigned long quirks = (unsigned long)hid_get_drvdata(hdev); |
| 38 | unsigned int i; |
| 39 | |
| 40 | if (!(quirks & CP_RDESC_SWAPPED_MIN_MAX)) |
Nikolai Kondrashov | 73e4008 | 2010-08-06 23:03:06 +0400 | [diff] [blame] | 41 | return rdesc; |
Jiri Slaby | 0f22132 | 2008-06-23 22:54:08 +0200 | [diff] [blame] | 42 | |
Nikolai Kondrashov | 73e4008 | 2010-08-06 23:03:06 +0400 | [diff] [blame] | 43 | for (i = 0; i < *rsize - 4; i++) |
Jiri Slaby | 0f22132 | 2008-06-23 22:54:08 +0200 | [diff] [blame] | 44 | if (rdesc[i] == 0x29 && rdesc[i + 2] == 0x19) { |
| 45 | __u8 tmp; |
| 46 | |
| 47 | rdesc[i] = 0x19; |
| 48 | rdesc[i + 2] = 0x29; |
| 49 | tmp = rdesc[i + 3]; |
| 50 | rdesc[i + 3] = rdesc[i + 1]; |
| 51 | rdesc[i + 1] = tmp; |
| 52 | } |
Nikolai Kondrashov | 73e4008 | 2010-08-06 23:03:06 +0400 | [diff] [blame] | 53 | return rdesc; |
Jiri Slaby | 0f22132 | 2008-06-23 22:54:08 +0200 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | static int cp_input_mapped(struct hid_device *hdev, struct hid_input *hi, |
| 57 | struct hid_field *field, struct hid_usage *usage, |
| 58 | unsigned long **bit, int *max) |
| 59 | { |
| 60 | unsigned long quirks = (unsigned long)hid_get_drvdata(hdev); |
| 61 | |
| 62 | if (!(quirks & CP_2WHEEL_MOUSE_HACK)) |
| 63 | return 0; |
| 64 | |
| 65 | if (usage->type == EV_REL && usage->code == REL_WHEEL) |
| 66 | set_bit(REL_HWHEEL, *bit); |
| 67 | if (usage->hid == 0x00090005) |
| 68 | return -1; |
| 69 | |
| 70 | return 0; |
| 71 | } |
| 72 | |
| 73 | static int cp_event(struct hid_device *hdev, struct hid_field *field, |
| 74 | struct hid_usage *usage, __s32 value) |
| 75 | { |
| 76 | unsigned long quirks = (unsigned long)hid_get_drvdata(hdev); |
| 77 | |
| 78 | if (!(hdev->claimed & HID_CLAIMED_INPUT) || !field->hidinput || |
| 79 | !usage->type || !(quirks & CP_2WHEEL_MOUSE_HACK)) |
| 80 | return 0; |
| 81 | |
| 82 | if (usage->hid == 0x00090005) { |
| 83 | if (value) |
| 84 | quirks |= CP_2WHEEL_MOUSE_HACK_ON; |
| 85 | else |
| 86 | quirks &= ~CP_2WHEEL_MOUSE_HACK_ON; |
| 87 | hid_set_drvdata(hdev, (void *)quirks); |
| 88 | return 1; |
| 89 | } |
| 90 | |
| 91 | if (usage->code == REL_WHEEL && (quirks & CP_2WHEEL_MOUSE_HACK_ON)) { |
| 92 | struct input_dev *input = field->hidinput->input; |
| 93 | |
| 94 | input_event(input, usage->type, REL_HWHEEL, value); |
| 95 | return 1; |
| 96 | } |
| 97 | |
| 98 | return 0; |
| 99 | } |
| 100 | |
| 101 | static int cp_probe(struct hid_device *hdev, const struct hid_device_id *id) |
| 102 | { |
| 103 | unsigned long quirks = id->driver_data; |
| 104 | int ret; |
| 105 | |
| 106 | hid_set_drvdata(hdev, (void *)quirks); |
| 107 | |
| 108 | ret = hid_parse(hdev); |
| 109 | if (ret) { |
Joe Perches | 4291ee3 | 2010-12-09 19:29:03 -0800 | [diff] [blame] | 110 | hid_err(hdev, "parse failed\n"); |
Jiri Slaby | 0f22132 | 2008-06-23 22:54:08 +0200 | [diff] [blame] | 111 | goto err_free; |
| 112 | } |
| 113 | |
Jiri Slaby | 93c1013 | 2008-06-27 00:04:24 +0200 | [diff] [blame] | 114 | ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT); |
Jiri Slaby | 0f22132 | 2008-06-23 22:54:08 +0200 | [diff] [blame] | 115 | if (ret) { |
Joe Perches | 4291ee3 | 2010-12-09 19:29:03 -0800 | [diff] [blame] | 116 | hid_err(hdev, "hw start failed\n"); |
Jiri Slaby | 0f22132 | 2008-06-23 22:54:08 +0200 | [diff] [blame] | 117 | goto err_free; |
| 118 | } |
| 119 | |
| 120 | return 0; |
| 121 | err_free: |
| 122 | return ret; |
| 123 | } |
| 124 | |
| 125 | static const struct hid_device_id cp_devices[] = { |
| 126 | { HID_USB_DEVICE(USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_BARCODE_1), |
| 127 | .driver_data = CP_RDESC_SWAPPED_MIN_MAX }, |
| 128 | { HID_USB_DEVICE(USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_BARCODE_2), |
| 129 | .driver_data = CP_RDESC_SWAPPED_MIN_MAX }, |
Jiri Kosina | e8d0eab | 2009-12-02 22:54:11 +0100 | [diff] [blame] | 130 | { HID_USB_DEVICE(USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_BARCODE_3), |
| 131 | .driver_data = CP_RDESC_SWAPPED_MIN_MAX }, |
Jiri Slaby | 0f22132 | 2008-06-23 22:54:08 +0200 | [diff] [blame] | 132 | { HID_USB_DEVICE(USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_MOUSE), |
| 133 | .driver_data = CP_2WHEEL_MOUSE_HACK }, |
| 134 | { } |
| 135 | }; |
| 136 | MODULE_DEVICE_TABLE(hid, cp_devices); |
| 137 | |
| 138 | static struct hid_driver cp_driver = { |
| 139 | .name = "cypress", |
| 140 | .id_table = cp_devices, |
| 141 | .report_fixup = cp_report_fixup, |
| 142 | .input_mapped = cp_input_mapped, |
| 143 | .event = cp_event, |
| 144 | .probe = cp_probe, |
| 145 | }; |
| 146 | |
Peter Huewe | a24f423b | 2009-07-02 19:08:38 +0200 | [diff] [blame] | 147 | static int __init cp_init(void) |
Jiri Slaby | 0f22132 | 2008-06-23 22:54:08 +0200 | [diff] [blame] | 148 | { |
| 149 | return hid_register_driver(&cp_driver); |
| 150 | } |
| 151 | |
Peter Huewe | a24f423b | 2009-07-02 19:08:38 +0200 | [diff] [blame] | 152 | static void __exit cp_exit(void) |
Jiri Slaby | 0f22132 | 2008-06-23 22:54:08 +0200 | [diff] [blame] | 153 | { |
| 154 | hid_unregister_driver(&cp_driver); |
| 155 | } |
| 156 | |
| 157 | module_init(cp_init); |
| 158 | module_exit(cp_exit); |
| 159 | MODULE_LICENSE("GPL"); |