HID: hidraw, fix a NULL pointer dereference in hidraw_ioctl
[linux-2.6.git] / drivers / hid / hid-zpff.c
1 /*
2  *  Force feedback support for Zeroplus based devices
3  *
4  *  Copyright (c) 2005, 2006 Anssi Hannula <anssi.hannula@gmail.com>
5  */
6
7 /*
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21  */
22
23
24 #include <linux/hid.h>
25 #include <linux/input.h>
26 #include <linux/slab.h>
27 #include <linux/usb.h>
28
29 #include "hid-ids.h"
30
31 #ifdef CONFIG_ZEROPLUS_FF
32 #include "usbhid/usbhid.h"
33
34 struct zpff_device {
35         struct hid_report *report;
36 };
37
38 static int zpff_play(struct input_dev *dev, void *data,
39                          struct ff_effect *effect)
40 {
41         struct hid_device *hid = input_get_drvdata(dev);
42         struct zpff_device *zpff = data;
43         int left, right;
44
45         /*
46          * The following is specified the other way around in the Zeroplus
47          * datasheet but the order below is correct for the XFX Executioner;
48          * however it is possible that the XFX Executioner is an exception
49          */
50
51         left = effect->u.rumble.strong_magnitude;
52         right = effect->u.rumble.weak_magnitude;
53         dbg_hid("called with 0x%04x 0x%04x\n", left, right);
54
55         left = left * 0x7f / 0xffff;
56         right = right * 0x7f / 0xffff;
57
58         zpff->report->field[2]->value[0] = left;
59         zpff->report->field[3]->value[0] = right;
60         dbg_hid("running with 0x%02x 0x%02x\n", left, right);
61         usbhid_submit_report(hid, zpff->report, USB_DIR_OUT);
62
63         return 0;
64 }
65
66 static int zpff_init(struct hid_device *hid)
67 {
68         struct zpff_device *zpff;
69         struct hid_report *report;
70         struct hid_input *hidinput = list_entry(hid->inputs.next,
71                                                 struct hid_input, list);
72         struct list_head *report_list =
73                         &hid->report_enum[HID_OUTPUT_REPORT].report_list;
74         struct input_dev *dev = hidinput->input;
75         int error;
76
77         if (list_empty(report_list)) {
78                 dev_err(&hid->dev, "no output report found\n");
79                 return -ENODEV;
80         }
81
82         report = list_entry(report_list->next, struct hid_report, list);
83
84         if (report->maxfield < 4) {
85                 dev_err(&hid->dev, "not enough fields in report\n");
86                 return -ENODEV;
87         }
88
89         zpff = kzalloc(sizeof(struct zpff_device), GFP_KERNEL);
90         if (!zpff)
91                 return -ENOMEM;
92
93         set_bit(FF_RUMBLE, dev->ffbit);
94
95         error = input_ff_create_memless(dev, zpff, zpff_play);
96         if (error) {
97                 kfree(zpff);
98                 return error;
99         }
100
101         zpff->report = report;
102         zpff->report->field[0]->value[0] = 0x00;
103         zpff->report->field[1]->value[0] = 0x02;
104         zpff->report->field[2]->value[0] = 0x00;
105         zpff->report->field[3]->value[0] = 0x00;
106         usbhid_submit_report(hid, zpff->report, USB_DIR_OUT);
107
108         dev_info(&hid->dev, "force feedback for Zeroplus based devices by "
109                "Anssi Hannula <anssi.hannula@gmail.com>\n");
110
111         return 0;
112 }
113 #else
114 static inline int zpff_init(struct hid_device *hid)
115 {
116         return 0;
117 }
118 #endif
119
120 static int zp_probe(struct hid_device *hdev, const struct hid_device_id *id)
121 {
122         int ret;
123
124         ret = hid_parse(hdev);
125         if (ret) {
126                 dev_err(&hdev->dev, "parse failed\n");
127                 goto err;
128         }
129
130         ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT & ~HID_CONNECT_FF);
131         if (ret) {
132                 dev_err(&hdev->dev, "hw start failed\n");
133                 goto err;
134         }
135
136         zpff_init(hdev);
137
138         return 0;
139 err:
140         return ret;
141 }
142
143 static const struct hid_device_id zp_devices[] = {
144         { HID_USB_DEVICE(USB_VENDOR_ID_ZEROPLUS, 0x0005) },
145         { HID_USB_DEVICE(USB_VENDOR_ID_ZEROPLUS, 0x0030) },
146         { }
147 };
148 MODULE_DEVICE_TABLE(hid, zp_devices);
149
150 static struct hid_driver zp_driver = {
151         .name = "zeroplus",
152         .id_table = zp_devices,
153         .probe = zp_probe,
154 };
155
156 static int __init zp_init(void)
157 {
158         return hid_register_driver(&zp_driver);
159 }
160
161 static void __exit zp_exit(void)
162 {
163         hid_unregister_driver(&zp_driver);
164 }
165
166 module_init(zp_init);
167 module_exit(zp_exit);
168 MODULE_LICENSE("GPL");