HID: hid-cypress: validate length of report
[pandora-kernel.git] / drivers / hid / hid-debug.c
1 /*
2  *  (c) 1999 Andreas Gal                <gal@cs.uni-magdeburg.de>
3  *  (c) 2000-2001 Vojtech Pavlik        <vojtech@ucw.cz>
4  *  (c) 2007-2009 Jiri Kosina
5  *
6  *  HID debugging support
7  */
8
9 /*
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23  *
24  * Should you need to contact me, the author, you can do so either by
25  * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail:
26  * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic
27  */
28
29 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
30
31 #include <linux/debugfs.h>
32 #include <linux/seq_file.h>
33 #include <linux/sched.h>
34 #include <linux/export.h>
35 #include <linux/slab.h>
36 #include <linux/uaccess.h>
37 #include <linux/poll.h>
38
39 #include <linux/hid.h>
40 #include <linux/hid-debug.h>
41
42 static struct dentry *hid_debug_root;
43
44 struct hid_usage_entry {
45         unsigned  page;
46         unsigned  usage;
47         const char     *description;
48 };
49
50 static const struct hid_usage_entry hid_usage_table[] = {
51   {  0,      0, "Undefined" },
52   {  1,      0, "GenericDesktop" },
53     {0, 0x01, "Pointer"},
54     {0, 0x02, "Mouse"},
55     {0, 0x04, "Joystick"},
56     {0, 0x05, "GamePad"},
57     {0, 0x06, "Keyboard"},
58     {0, 0x07, "Keypad"},
59     {0, 0x08, "MultiAxis"},
60       {0, 0x30, "X"},
61       {0, 0x31, "Y"},
62       {0, 0x32, "Z"},
63       {0, 0x33, "Rx"},
64       {0, 0x34, "Ry"},
65       {0, 0x35, "Rz"},
66       {0, 0x36, "Slider"},
67       {0, 0x37, "Dial"},
68       {0, 0x38, "Wheel"},
69       {0, 0x39, "HatSwitch"},
70     {0, 0x3a, "CountedBuffer"},
71       {0, 0x3b, "ByteCount"},
72       {0, 0x3c, "MotionWakeup"},
73       {0, 0x3d, "Start"},
74       {0, 0x3e, "Select"},
75       {0, 0x40, "Vx"},
76       {0, 0x41, "Vy"},
77       {0, 0x42, "Vz"},
78       {0, 0x43, "Vbrx"},
79       {0, 0x44, "Vbry"},
80       {0, 0x45, "Vbrz"},
81       {0, 0x46, "Vno"},
82     {0, 0x80, "SystemControl"},
83       {0, 0x81, "SystemPowerDown"},
84       {0, 0x82, "SystemSleep"},
85       {0, 0x83, "SystemWakeUp"},
86       {0, 0x84, "SystemContextMenu"},
87       {0, 0x85, "SystemMainMenu"},
88       {0, 0x86, "SystemAppMenu"},
89       {0, 0x87, "SystemMenuHelp"},
90       {0, 0x88, "SystemMenuExit"},
91       {0, 0x89, "SystemMenuSelect"},
92       {0, 0x8a, "SystemMenuRight"},
93       {0, 0x8b, "SystemMenuLeft"},
94       {0, 0x8c, "SystemMenuUp"},
95       {0, 0x8d, "SystemMenuDown"},
96       {0, 0x90, "D-PadUp"},
97       {0, 0x91, "D-PadDown"},
98       {0, 0x92, "D-PadRight"},
99       {0, 0x93, "D-PadLeft"},
100   {  2, 0, "Simulation" },
101       {0, 0xb0, "Aileron"},
102       {0, 0xb1, "AileronTrim"},
103       {0, 0xb2, "Anti-Torque"},
104       {0, 0xb3, "Autopilot"},
105       {0, 0xb4, "Chaff"},
106       {0, 0xb5, "Collective"},
107       {0, 0xb6, "DiveBrake"},
108       {0, 0xb7, "ElectronicCountermeasures"},
109       {0, 0xb8, "Elevator"},
110       {0, 0xb9, "ElevatorTrim"},
111       {0, 0xba, "Rudder"},
112       {0, 0xbb, "Throttle"},
113       {0, 0xbc, "FlightCommunications"},
114       {0, 0xbd, "FlareRelease"},
115       {0, 0xbe, "LandingGear"},
116       {0, 0xbf, "ToeBrake"},
117   {  7, 0, "Keyboard" },
118   {  8, 0, "LED" },
119       {0, 0x01, "NumLock"},
120       {0, 0x02, "CapsLock"},
121       {0, 0x03, "ScrollLock"},
122       {0, 0x04, "Compose"},
123       {0, 0x05, "Kana"},
124       {0, 0x4b, "GenericIndicator"},
125   {  9, 0, "Button" },
126   { 10, 0, "Ordinal" },
127   { 12, 0, "Consumer" },
128       {0, 0x238, "HorizontalWheel"},
129   { 13, 0, "Digitizers" },
130     {0, 0x01, "Digitizer"},
131     {0, 0x02, "Pen"},
132     {0, 0x03, "LightPen"},
133     {0, 0x04, "TouchScreen"},
134     {0, 0x05, "TouchPad"},
135     {0, 0x20, "Stylus"},
136     {0, 0x21, "Puck"},
137     {0, 0x22, "Finger"},
138     {0, 0x30, "TipPressure"},
139     {0, 0x31, "BarrelPressure"},
140     {0, 0x32, "InRange"},
141     {0, 0x33, "Touch"},
142     {0, 0x34, "UnTouch"},
143     {0, 0x35, "Tap"},
144     {0, 0x39, "TabletFunctionKey"},
145     {0, 0x3a, "ProgramChangeKey"},
146     {0, 0x3c, "Invert"},
147     {0, 0x42, "TipSwitch"},
148     {0, 0x43, "SecondaryTipSwitch"},
149     {0, 0x44, "BarrelSwitch"},
150     {0, 0x45, "Eraser"},
151     {0, 0x46, "TabletPick"},
152     {0, 0x47, "Confidence"},
153     {0, 0x48, "Width"},
154     {0, 0x49, "Height"},
155     {0, 0x51, "ContactID"},
156     {0, 0x52, "InputMode"},
157     {0, 0x53, "DeviceIndex"},
158     {0, 0x54, "ContactCount"},
159     {0, 0x55, "ContactMaximumNumber"},
160   { 15, 0, "PhysicalInterfaceDevice" },
161     {0, 0x00, "Undefined"},
162     {0, 0x01, "Physical_Interface_Device"},
163       {0, 0x20, "Normal"},
164     {0, 0x21, "Set_Effect_Report"},
165       {0, 0x22, "Effect_Block_Index"},
166       {0, 0x23, "Parameter_Block_Offset"},
167       {0, 0x24, "ROM_Flag"},
168       {0, 0x25, "Effect_Type"},
169         {0, 0x26, "ET_Constant_Force"},
170         {0, 0x27, "ET_Ramp"},
171         {0, 0x28, "ET_Custom_Force_Data"},
172         {0, 0x30, "ET_Square"},
173         {0, 0x31, "ET_Sine"},
174         {0, 0x32, "ET_Triangle"},
175         {0, 0x33, "ET_Sawtooth_Up"},
176         {0, 0x34, "ET_Sawtooth_Down"},
177         {0, 0x40, "ET_Spring"},
178         {0, 0x41, "ET_Damper"},
179         {0, 0x42, "ET_Inertia"},
180         {0, 0x43, "ET_Friction"},
181       {0, 0x50, "Duration"},
182       {0, 0x51, "Sample_Period"},
183       {0, 0x52, "Gain"},
184       {0, 0x53, "Trigger_Button"},
185       {0, 0x54, "Trigger_Repeat_Interval"},
186       {0, 0x55, "Axes_Enable"},
187         {0, 0x56, "Direction_Enable"},
188       {0, 0x57, "Direction"},
189       {0, 0x58, "Type_Specific_Block_Offset"},
190         {0, 0x59, "Block_Type"},
191         {0, 0x5A, "Set_Envelope_Report"},
192           {0, 0x5B, "Attack_Level"},
193           {0, 0x5C, "Attack_Time"},
194           {0, 0x5D, "Fade_Level"},
195           {0, 0x5E, "Fade_Time"},
196         {0, 0x5F, "Set_Condition_Report"},
197         {0, 0x60, "CP_Offset"},
198         {0, 0x61, "Positive_Coefficient"},
199         {0, 0x62, "Negative_Coefficient"},
200         {0, 0x63, "Positive_Saturation"},
201         {0, 0x64, "Negative_Saturation"},
202         {0, 0x65, "Dead_Band"},
203       {0, 0x66, "Download_Force_Sample"},
204       {0, 0x67, "Isoch_Custom_Force_Enable"},
205       {0, 0x68, "Custom_Force_Data_Report"},
206         {0, 0x69, "Custom_Force_Data"},
207         {0, 0x6A, "Custom_Force_Vendor_Defined_Data"},
208       {0, 0x6B, "Set_Custom_Force_Report"},
209         {0, 0x6C, "Custom_Force_Data_Offset"},
210         {0, 0x6D, "Sample_Count"},
211       {0, 0x6E, "Set_Periodic_Report"},
212         {0, 0x6F, "Offset"},
213         {0, 0x70, "Magnitude"},
214         {0, 0x71, "Phase"},
215         {0, 0x72, "Period"},
216       {0, 0x73, "Set_Constant_Force_Report"},
217         {0, 0x74, "Set_Ramp_Force_Report"},
218         {0, 0x75, "Ramp_Start"},
219         {0, 0x76, "Ramp_End"},
220       {0, 0x77, "Effect_Operation_Report"},
221         {0, 0x78, "Effect_Operation"},
222           {0, 0x79, "Op_Effect_Start"},
223           {0, 0x7A, "Op_Effect_Start_Solo"},
224           {0, 0x7B, "Op_Effect_Stop"},
225           {0, 0x7C, "Loop_Count"},
226       {0, 0x7D, "Device_Gain_Report"},
227         {0, 0x7E, "Device_Gain"},
228     {0, 0x7F, "PID_Pool_Report"},
229       {0, 0x80, "RAM_Pool_Size"},
230       {0, 0x81, "ROM_Pool_Size"},
231       {0, 0x82, "ROM_Effect_Block_Count"},
232       {0, 0x83, "Simultaneous_Effects_Max"},
233       {0, 0x84, "Pool_Alignment"},
234     {0, 0x85, "PID_Pool_Move_Report"},
235       {0, 0x86, "Move_Source"},
236       {0, 0x87, "Move_Destination"},
237       {0, 0x88, "Move_Length"},
238     {0, 0x89, "PID_Block_Load_Report"},
239       {0, 0x8B, "Block_Load_Status"},
240       {0, 0x8C, "Block_Load_Success"},
241       {0, 0x8D, "Block_Load_Full"},
242       {0, 0x8E, "Block_Load_Error"},
243       {0, 0x8F, "Block_Handle"},
244       {0, 0x90, "PID_Block_Free_Report"},
245       {0, 0x91, "Type_Specific_Block_Handle"},
246     {0, 0x92, "PID_State_Report"},
247       {0, 0x94, "Effect_Playing"},
248       {0, 0x95, "PID_Device_Control_Report"},
249         {0, 0x96, "PID_Device_Control"},
250         {0, 0x97, "DC_Enable_Actuators"},
251         {0, 0x98, "DC_Disable_Actuators"},
252         {0, 0x99, "DC_Stop_All_Effects"},
253         {0, 0x9A, "DC_Device_Reset"},
254         {0, 0x9B, "DC_Device_Pause"},
255         {0, 0x9C, "DC_Device_Continue"},
256       {0, 0x9F, "Device_Paused"},
257       {0, 0xA0, "Actuators_Enabled"},
258       {0, 0xA4, "Safety_Switch"},
259       {0, 0xA5, "Actuator_Override_Switch"},
260       {0, 0xA6, "Actuator_Power"},
261     {0, 0xA7, "Start_Delay"},
262     {0, 0xA8, "Parameter_Block_Size"},
263     {0, 0xA9, "Device_Managed_Pool"},
264     {0, 0xAA, "Shared_Parameter_Blocks"},
265     {0, 0xAB, "Create_New_Effect_Report"},
266     {0, 0xAC, "RAM_Pool_Available"},
267   { 0x84, 0, "Power Device" },
268     { 0x84, 0x02, "PresentStatus" },
269     { 0x84, 0x03, "ChangeStatus" },
270     { 0x84, 0x04, "UPS" },
271     { 0x84, 0x05, "PowerSupply" },
272     { 0x84, 0x10, "BatterySystem" },
273     { 0x84, 0x11, "BatterySystemID" },
274     { 0x84, 0x12, "Battery" },
275     { 0x84, 0x13, "BatteryID" },
276     { 0x84, 0x14, "Charger" },
277     { 0x84, 0x15, "ChargerID" },
278     { 0x84, 0x16, "PowerConverter" },
279     { 0x84, 0x17, "PowerConverterID" },
280     { 0x84, 0x18, "OutletSystem" },
281     { 0x84, 0x19, "OutletSystemID" },
282     { 0x84, 0x1a, "Input" },
283     { 0x84, 0x1b, "InputID" },
284     { 0x84, 0x1c, "Output" },
285     { 0x84, 0x1d, "OutputID" },
286     { 0x84, 0x1e, "Flow" },
287     { 0x84, 0x1f, "FlowID" },
288     { 0x84, 0x20, "Outlet" },
289     { 0x84, 0x21, "OutletID" },
290     { 0x84, 0x22, "Gang" },
291     { 0x84, 0x24, "PowerSummary" },
292     { 0x84, 0x25, "PowerSummaryID" },
293     { 0x84, 0x30, "Voltage" },
294     { 0x84, 0x31, "Current" },
295     { 0x84, 0x32, "Frequency" },
296     { 0x84, 0x33, "ApparentPower" },
297     { 0x84, 0x35, "PercentLoad" },
298     { 0x84, 0x40, "ConfigVoltage" },
299     { 0x84, 0x41, "ConfigCurrent" },
300     { 0x84, 0x43, "ConfigApparentPower" },
301     { 0x84, 0x53, "LowVoltageTransfer" },
302     { 0x84, 0x54, "HighVoltageTransfer" },
303     { 0x84, 0x56, "DelayBeforeStartup" },
304     { 0x84, 0x57, "DelayBeforeShutdown" },
305     { 0x84, 0x58, "Test" },
306     { 0x84, 0x5a, "AudibleAlarmControl" },
307     { 0x84, 0x60, "Present" },
308     { 0x84, 0x61, "Good" },
309     { 0x84, 0x62, "InternalFailure" },
310     { 0x84, 0x65, "Overload" },
311     { 0x84, 0x66, "OverCharged" },
312     { 0x84, 0x67, "OverTemperature" },
313     { 0x84, 0x68, "ShutdownRequested" },
314     { 0x84, 0x69, "ShutdownImminent" },
315     { 0x84, 0x6b, "SwitchOn/Off" },
316     { 0x84, 0x6c, "Switchable" },
317     { 0x84, 0x6d, "Used" },
318     { 0x84, 0x6e, "Boost" },
319     { 0x84, 0x73, "CommunicationLost" },
320     { 0x84, 0xfd, "iManufacturer" },
321     { 0x84, 0xfe, "iProduct" },
322     { 0x84, 0xff, "iSerialNumber" },
323   { 0x85, 0, "Battery System" },
324     { 0x85, 0x01, "SMBBatteryMode" },
325     { 0x85, 0x02, "SMBBatteryStatus" },
326     { 0x85, 0x03, "SMBAlarmWarning" },
327     { 0x85, 0x04, "SMBChargerMode" },
328     { 0x85, 0x05, "SMBChargerStatus" },
329     { 0x85, 0x06, "SMBChargerSpecInfo" },
330     { 0x85, 0x07, "SMBSelectorState" },
331     { 0x85, 0x08, "SMBSelectorPresets" },
332     { 0x85, 0x09, "SMBSelectorInfo" },
333     { 0x85, 0x29, "RemainingCapacityLimit" },
334     { 0x85, 0x2c, "CapacityMode" },
335     { 0x85, 0x42, "BelowRemainingCapacityLimit" },
336     { 0x85, 0x44, "Charging" },
337     { 0x85, 0x45, "Discharging" },
338     { 0x85, 0x4b, "NeedReplacement" },
339     { 0x85, 0x66, "RemainingCapacity" },
340     { 0x85, 0x68, "RunTimeToEmpty" },
341     { 0x85, 0x6a, "AverageTimeToFull" },
342     { 0x85, 0x83, "DesignCapacity" },
343     { 0x85, 0x85, "ManufacturerDate" },
344     { 0x85, 0x89, "iDeviceChemistry" },
345     { 0x85, 0x8b, "Rechargeable" },
346     { 0x85, 0x8f, "iOEMInformation" },
347     { 0x85, 0x8d, "CapacityGranularity1" },
348     { 0x85, 0xd0, "ACPresent" },
349   /* pages 0xff00 to 0xffff are vendor-specific */
350   { 0xffff, 0, "Vendor-specific-FF" },
351   { 0, 0, NULL }
352 };
353
354 /* Either output directly into simple seq_file, or (if f == NULL)
355  * allocate a separate buffer that will then be passed to the 'events'
356  * ringbuffer.
357  *
358  * This is because these functions can be called both for "one-shot"
359  * "rdesc" while resolving, or for blocking "events".
360  *
361  * This holds both for resolv_usage_page() and hid_resolv_usage().
362  */
363 static char *resolv_usage_page(unsigned page, struct seq_file *f) {
364         const struct hid_usage_entry *p;
365         char *buf = NULL;
366
367         if (!f) {
368                 buf = kzalloc(sizeof(char) * HID_DEBUG_BUFSIZE, GFP_ATOMIC);
369                 if (!buf)
370                         return ERR_PTR(-ENOMEM);
371         }
372
373         for (p = hid_usage_table; p->description; p++)
374                 if (p->page == page) {
375                         if (!f) {
376                                 snprintf(buf, HID_DEBUG_BUFSIZE, "%s",
377                                                 p->description);
378                                 return buf;
379                         }
380                         else {
381                                 seq_printf(f, "%s", p->description);
382                                 return NULL;
383                         }
384                 }
385         if (!f)
386                 snprintf(buf, HID_DEBUG_BUFSIZE, "%04x", page);
387         else
388                 seq_printf(f, "%04x", page);
389         return buf;
390 }
391
392 char *hid_resolv_usage(unsigned usage, struct seq_file *f) {
393         const struct hid_usage_entry *p;
394         char *buf = NULL;
395         int len = 0;
396
397         buf = resolv_usage_page(usage >> 16, f);
398         if (IS_ERR(buf)) {
399                 pr_err("error allocating HID debug buffer\n");
400                 return NULL;
401         }
402
403
404         if (!f) {
405                 len = strlen(buf);
406                 snprintf(buf+len, max(0, HID_DEBUG_BUFSIZE - len), ".");
407                 len++;
408         }
409         else {
410                 seq_printf(f, ".");
411         }
412         for (p = hid_usage_table; p->description; p++)
413                 if (p->page == (usage >> 16)) {
414                         for(++p; p->description && p->usage != 0; p++)
415                                 if (p->usage == (usage & 0xffff)) {
416                                         if (!f)
417                                                 snprintf(buf + len,
418                                                         max(0,HID_DEBUG_BUFSIZE - len - 1),
419                                                         "%s", p->description);
420                                         else
421                                                 seq_printf(f,
422                                                         "%s",
423                                                         p->description);
424                                         return buf;
425                                 }
426                         break;
427                 }
428         if (!f)
429                 snprintf(buf + len, max(0, HID_DEBUG_BUFSIZE - len - 1),
430                                 "%04x", usage & 0xffff);
431         else
432                 seq_printf(f, "%04x", usage & 0xffff);
433         return buf;
434 }
435 EXPORT_SYMBOL_GPL(hid_resolv_usage);
436
437 static void tab(int n, struct seq_file *f) {
438         seq_printf(f, "%*s", n, "");
439 }
440
441 void hid_dump_field(struct hid_field *field, int n, struct seq_file *f) {
442         int j;
443
444         if (field->physical) {
445                 tab(n, f);
446                 seq_printf(f, "Physical(");
447                 hid_resolv_usage(field->physical, f); seq_printf(f, ")\n");
448         }
449         if (field->logical) {
450                 tab(n, f);
451                 seq_printf(f, "Logical(");
452                 hid_resolv_usage(field->logical, f); seq_printf(f, ")\n");
453         }
454         if (field->application) {
455                 tab(n, f);
456                 seq_printf(f, "Application(");
457                 hid_resolv_usage(field->application, f); seq_printf(f, ")\n");
458         }
459         tab(n, f); seq_printf(f, "Usage(%d)\n", field->maxusage);
460         for (j = 0; j < field->maxusage; j++) {
461                 tab(n+2, f); hid_resolv_usage(field->usage[j].hid, f); seq_printf(f, "\n");
462         }
463         if (field->logical_minimum != field->logical_maximum) {
464                 tab(n, f); seq_printf(f, "Logical Minimum(%d)\n", field->logical_minimum);
465                 tab(n, f); seq_printf(f, "Logical Maximum(%d)\n", field->logical_maximum);
466         }
467         if (field->physical_minimum != field->physical_maximum) {
468                 tab(n, f); seq_printf(f, "Physical Minimum(%d)\n", field->physical_minimum);
469                 tab(n, f); seq_printf(f, "Physical Maximum(%d)\n", field->physical_maximum);
470         }
471         if (field->unit_exponent) {
472                 tab(n, f); seq_printf(f, "Unit Exponent(%d)\n", field->unit_exponent);
473         }
474         if (field->unit) {
475                 static const char *systems[5] = { "None", "SI Linear", "SI Rotation", "English Linear", "English Rotation" };
476                 static const char *units[5][8] = {
477                         { "None", "None", "None", "None", "None", "None", "None", "None" },
478                         { "None", "Centimeter", "Gram", "Seconds", "Kelvin",     "Ampere", "Candela", "None" },
479                         { "None", "Radians",    "Gram", "Seconds", "Kelvin",     "Ampere", "Candela", "None" },
480                         { "None", "Inch",       "Slug", "Seconds", "Fahrenheit", "Ampere", "Candela", "None" },
481                         { "None", "Degrees",    "Slug", "Seconds", "Fahrenheit", "Ampere", "Candela", "None" }
482                 };
483
484                 int i;
485                 int sys;
486                 __u32 data = field->unit;
487
488                 /* First nibble tells us which system we're in. */
489                 sys = data & 0xf;
490                 data >>= 4;
491
492                 if(sys > 4) {
493                         tab(n, f); seq_printf(f, "Unit(Invalid)\n");
494                 }
495                 else {
496                         int earlier_unit = 0;
497
498                         tab(n, f); seq_printf(f, "Unit(%s : ", systems[sys]);
499
500                         for (i=1 ; i<sizeof(__u32)*2 ; i++) {
501                                 char nibble = data & 0xf;
502                                 data >>= 4;
503                                 if (nibble != 0) {
504                                         if(earlier_unit++ > 0)
505                                                 seq_printf(f, "*");
506                                         seq_printf(f, "%s", units[sys][i]);
507                                         if(nibble != 1) {
508                                                 /* This is a _signed_ nibble(!) */
509
510                                                 int val = nibble & 0x7;
511                                                 if(nibble & 0x08)
512                                                         val = -((0x7 & ~val) +1);
513                                                 seq_printf(f, "^%d", val);
514                                         }
515                                 }
516                         }
517                         seq_printf(f, ")\n");
518                 }
519         }
520         tab(n, f); seq_printf(f, "Report Size(%u)\n", field->report_size);
521         tab(n, f); seq_printf(f, "Report Count(%u)\n", field->report_count);
522         tab(n, f); seq_printf(f, "Report Offset(%u)\n", field->report_offset);
523
524         tab(n, f); seq_printf(f, "Flags( ");
525         j = field->flags;
526         seq_printf(f, "%s", HID_MAIN_ITEM_CONSTANT & j ? "Constant " : "");
527         seq_printf(f, "%s", HID_MAIN_ITEM_VARIABLE & j ? "Variable " : "Array ");
528         seq_printf(f, "%s", HID_MAIN_ITEM_RELATIVE & j ? "Relative " : "Absolute ");
529         seq_printf(f, "%s", HID_MAIN_ITEM_WRAP & j ? "Wrap " : "");
530         seq_printf(f, "%s", HID_MAIN_ITEM_NONLINEAR & j ? "NonLinear " : "");
531         seq_printf(f, "%s", HID_MAIN_ITEM_NO_PREFERRED & j ? "NoPreferredState " : "");
532         seq_printf(f, "%s", HID_MAIN_ITEM_NULL_STATE & j ? "NullState " : "");
533         seq_printf(f, "%s", HID_MAIN_ITEM_VOLATILE & j ? "Volatile " : "");
534         seq_printf(f, "%s", HID_MAIN_ITEM_BUFFERED_BYTE & j ? "BufferedByte " : "");
535         seq_printf(f, ")\n");
536 }
537 EXPORT_SYMBOL_GPL(hid_dump_field);
538
539 void hid_dump_device(struct hid_device *device, struct seq_file *f)
540 {
541         struct hid_report_enum *report_enum;
542         struct hid_report *report;
543         struct list_head *list;
544         unsigned i,k;
545         static const char *table[] = {"INPUT", "OUTPUT", "FEATURE"};
546
547         for (i = 0; i < HID_REPORT_TYPES; i++) {
548                 report_enum = device->report_enum + i;
549                 list = report_enum->report_list.next;
550                 while (list != &report_enum->report_list) {
551                         report = (struct hid_report *) list;
552                         tab(2, f);
553                         seq_printf(f, "%s", table[i]);
554                         if (report->id)
555                                 seq_printf(f, "(%d)", report->id);
556                         seq_printf(f, "[%s]", table[report->type]);
557                         seq_printf(f, "\n");
558                         for (k = 0; k < report->maxfield; k++) {
559                                 tab(4, f);
560                                 seq_printf(f, "Field(%d)\n", k);
561                                 hid_dump_field(report->field[k], 6, f);
562                         }
563                         list = list->next;
564                 }
565         }
566 }
567 EXPORT_SYMBOL_GPL(hid_dump_device);
568
569 /* enqueue string to 'events' ring buffer */
570 void hid_debug_event(struct hid_device *hdev, char *buf)
571 {
572         int i;
573         struct hid_debug_list *list;
574
575         list_for_each_entry(list, &hdev->debug_list, node) {
576                 for (i = 0; i < strlen(buf); i++)
577                         list->hid_debug_buf[(list->tail + i) % HID_DEBUG_BUFSIZE] =
578                                 buf[i];
579                 list->tail = (list->tail + i) % HID_DEBUG_BUFSIZE;
580         }
581
582         wake_up_interruptible(&hdev->debug_wait);
583 }
584 EXPORT_SYMBOL_GPL(hid_debug_event);
585
586 void hid_dump_input(struct hid_device *hdev, struct hid_usage *usage, __s32 value)
587 {
588         char *buf;
589         int len;
590
591         buf = hid_resolv_usage(usage->hid, NULL);
592         if (!buf)
593                 return;
594         len = strlen(buf);
595         snprintf(buf + len, HID_DEBUG_BUFSIZE - len - 1, " = %d\n", value);
596
597         hid_debug_event(hdev, buf);
598
599         kfree(buf);
600         wake_up_interruptible(&hdev->debug_wait);
601
602 }
603 EXPORT_SYMBOL_GPL(hid_dump_input);
604
605 static const char *events[EV_MAX + 1] = {
606         [EV_SYN] = "Sync",                      [EV_KEY] = "Key",
607         [EV_REL] = "Relative",                  [EV_ABS] = "Absolute",
608         [EV_MSC] = "Misc",                      [EV_LED] = "LED",
609         [EV_SND] = "Sound",                     [EV_REP] = "Repeat",
610         [EV_FF] = "ForceFeedback",              [EV_PWR] = "Power",
611         [EV_FF_STATUS] = "ForceFeedbackStatus",
612 };
613
614 static const char *syncs[3] = {
615         [SYN_REPORT] = "Report",                [SYN_CONFIG] = "Config",
616         [SYN_MT_REPORT] = "MT Report",
617 };
618
619 static const char *keys[KEY_MAX + 1] = {
620         [KEY_RESERVED] = "Reserved",            [KEY_ESC] = "Esc",
621         [KEY_1] = "1",                          [KEY_2] = "2",
622         [KEY_3] = "3",                          [KEY_4] = "4",
623         [KEY_5] = "5",                          [KEY_6] = "6",
624         [KEY_7] = "7",                          [KEY_8] = "8",
625         [KEY_9] = "9",                          [KEY_0] = "0",
626         [KEY_MINUS] = "Minus",                  [KEY_EQUAL] = "Equal",
627         [KEY_BACKSPACE] = "Backspace",          [KEY_TAB] = "Tab",
628         [KEY_Q] = "Q",                          [KEY_W] = "W",
629         [KEY_E] = "E",                          [KEY_R] = "R",
630         [KEY_T] = "T",                          [KEY_Y] = "Y",
631         [KEY_U] = "U",                          [KEY_I] = "I",
632         [KEY_O] = "O",                          [KEY_P] = "P",
633         [KEY_LEFTBRACE] = "LeftBrace",          [KEY_RIGHTBRACE] = "RightBrace",
634         [KEY_ENTER] = "Enter",                  [KEY_LEFTCTRL] = "LeftControl",
635         [KEY_A] = "A",                          [KEY_S] = "S",
636         [KEY_D] = "D",                          [KEY_F] = "F",
637         [KEY_G] = "G",                          [KEY_H] = "H",
638         [KEY_J] = "J",                          [KEY_K] = "K",
639         [KEY_L] = "L",                          [KEY_SEMICOLON] = "Semicolon",
640         [KEY_APOSTROPHE] = "Apostrophe",        [KEY_GRAVE] = "Grave",
641         [KEY_LEFTSHIFT] = "LeftShift",          [KEY_BACKSLASH] = "BackSlash",
642         [KEY_Z] = "Z",                          [KEY_X] = "X",
643         [KEY_C] = "C",                          [KEY_V] = "V",
644         [KEY_B] = "B",                          [KEY_N] = "N",
645         [KEY_M] = "M",                          [KEY_COMMA] = "Comma",
646         [KEY_DOT] = "Dot",                      [KEY_SLASH] = "Slash",
647         [KEY_RIGHTSHIFT] = "RightShift",        [KEY_KPASTERISK] = "KPAsterisk",
648         [KEY_LEFTALT] = "LeftAlt",              [KEY_SPACE] = "Space",
649         [KEY_CAPSLOCK] = "CapsLock",            [KEY_F1] = "F1",
650         [KEY_F2] = "F2",                        [KEY_F3] = "F3",
651         [KEY_F4] = "F4",                        [KEY_F5] = "F5",
652         [KEY_F6] = "F6",                        [KEY_F7] = "F7",
653         [KEY_F8] = "F8",                        [KEY_F9] = "F9",
654         [KEY_F10] = "F10",                      [KEY_NUMLOCK] = "NumLock",
655         [KEY_SCROLLLOCK] = "ScrollLock",        [KEY_KP7] = "KP7",
656         [KEY_KP8] = "KP8",                      [KEY_KP9] = "KP9",
657         [KEY_KPMINUS] = "KPMinus",              [KEY_KP4] = "KP4",
658         [KEY_KP5] = "KP5",                      [KEY_KP6] = "KP6",
659         [KEY_KPPLUS] = "KPPlus",                [KEY_KP1] = "KP1",
660         [KEY_KP2] = "KP2",                      [KEY_KP3] = "KP3",
661         [KEY_KP0] = "KP0",                      [KEY_KPDOT] = "KPDot",
662         [KEY_ZENKAKUHANKAKU] = "Zenkaku/Hankaku", [KEY_102ND] = "102nd",
663         [KEY_F11] = "F11",                      [KEY_F12] = "F12",
664         [KEY_RO] = "RO",                        [KEY_KATAKANA] = "Katakana",
665         [KEY_HIRAGANA] = "HIRAGANA",            [KEY_HENKAN] = "Henkan",
666         [KEY_KATAKANAHIRAGANA] = "Katakana/Hiragana", [KEY_MUHENKAN] = "Muhenkan",
667         [KEY_KPJPCOMMA] = "KPJpComma",          [KEY_KPENTER] = "KPEnter",
668         [KEY_RIGHTCTRL] = "RightCtrl",          [KEY_KPSLASH] = "KPSlash",
669         [KEY_SYSRQ] = "SysRq",                  [KEY_RIGHTALT] = "RightAlt",
670         [KEY_LINEFEED] = "LineFeed",            [KEY_HOME] = "Home",
671         [KEY_UP] = "Up",                        [KEY_PAGEUP] = "PageUp",
672         [KEY_LEFT] = "Left",                    [KEY_RIGHT] = "Right",
673         [KEY_END] = "End",                      [KEY_DOWN] = "Down",
674         [KEY_PAGEDOWN] = "PageDown",            [KEY_INSERT] = "Insert",
675         [KEY_DELETE] = "Delete",                [KEY_MACRO] = "Macro",
676         [KEY_MUTE] = "Mute",                    [KEY_VOLUMEDOWN] = "VolumeDown",
677         [KEY_VOLUMEUP] = "VolumeUp",            [KEY_POWER] = "Power",
678         [KEY_KPEQUAL] = "KPEqual",              [KEY_KPPLUSMINUS] = "KPPlusMinus",
679         [KEY_PAUSE] = "Pause",                  [KEY_KPCOMMA] = "KPComma",
680         [KEY_HANGUEL] = "Hangeul",              [KEY_HANJA] = "Hanja",
681         [KEY_YEN] = "Yen",                      [KEY_LEFTMETA] = "LeftMeta",
682         [KEY_RIGHTMETA] = "RightMeta",          [KEY_COMPOSE] = "Compose",
683         [KEY_STOP] = "Stop",                    [KEY_AGAIN] = "Again",
684         [KEY_PROPS] = "Props",                  [KEY_UNDO] = "Undo",
685         [KEY_FRONT] = "Front",                  [KEY_COPY] = "Copy",
686         [KEY_OPEN] = "Open",                    [KEY_PASTE] = "Paste",
687         [KEY_FIND] = "Find",                    [KEY_CUT] = "Cut",
688         [KEY_HELP] = "Help",                    [KEY_MENU] = "Menu",
689         [KEY_CALC] = "Calc",                    [KEY_SETUP] = "Setup",
690         [KEY_SLEEP] = "Sleep",                  [KEY_WAKEUP] = "WakeUp",
691         [KEY_FILE] = "File",                    [KEY_SENDFILE] = "SendFile",
692         [KEY_DELETEFILE] = "DeleteFile",        [KEY_XFER] = "X-fer",
693         [KEY_PROG1] = "Prog1",                  [KEY_PROG2] = "Prog2",
694         [KEY_WWW] = "WWW",                      [KEY_MSDOS] = "MSDOS",
695         [KEY_COFFEE] = "Coffee",                [KEY_DIRECTION] = "Direction",
696         [KEY_CYCLEWINDOWS] = "CycleWindows",    [KEY_MAIL] = "Mail",
697         [KEY_BOOKMARKS] = "Bookmarks",          [KEY_COMPUTER] = "Computer",
698         [KEY_BACK] = "Back",                    [KEY_FORWARD] = "Forward",
699         [KEY_CLOSECD] = "CloseCD",              [KEY_EJECTCD] = "EjectCD",
700         [KEY_EJECTCLOSECD] = "EjectCloseCD",    [KEY_NEXTSONG] = "NextSong",
701         [KEY_PLAYPAUSE] = "PlayPause",          [KEY_PREVIOUSSONG] = "PreviousSong",
702         [KEY_STOPCD] = "StopCD",                [KEY_RECORD] = "Record",
703         [KEY_REWIND] = "Rewind",                [KEY_PHONE] = "Phone",
704         [KEY_ISO] = "ISOKey",                   [KEY_CONFIG] = "Config",
705         [KEY_HOMEPAGE] = "HomePage",            [KEY_REFRESH] = "Refresh",
706         [KEY_EXIT] = "Exit",                    [KEY_MOVE] = "Move",
707         [KEY_EDIT] = "Edit",                    [KEY_SCROLLUP] = "ScrollUp",
708         [KEY_SCROLLDOWN] = "ScrollDown",        [KEY_KPLEFTPAREN] = "KPLeftParenthesis",
709         [KEY_KPRIGHTPAREN] = "KPRightParenthesis", [KEY_NEW] = "New",
710         [KEY_REDO] = "Redo",                    [KEY_F13] = "F13",
711         [KEY_F14] = "F14",                      [KEY_F15] = "F15",
712         [KEY_F16] = "F16",                      [KEY_F17] = "F17",
713         [KEY_F18] = "F18",                      [KEY_F19] = "F19",
714         [KEY_F20] = "F20",                      [KEY_F21] = "F21",
715         [KEY_F22] = "F22",                      [KEY_F23] = "F23",
716         [KEY_F24] = "F24",                      [KEY_PLAYCD] = "PlayCD",
717         [KEY_PAUSECD] = "PauseCD",              [KEY_PROG3] = "Prog3",
718         [KEY_PROG4] = "Prog4",                  [KEY_SUSPEND] = "Suspend",
719         [KEY_CLOSE] = "Close",                  [KEY_PLAY] = "Play",
720         [KEY_FASTFORWARD] = "FastForward",      [KEY_BASSBOOST] = "BassBoost",
721         [KEY_PRINT] = "Print",                  [KEY_HP] = "HP",
722         [KEY_CAMERA] = "Camera",                [KEY_SOUND] = "Sound",
723         [KEY_QUESTION] = "Question",            [KEY_EMAIL] = "Email",
724         [KEY_CHAT] = "Chat",                    [KEY_SEARCH] = "Search",
725         [KEY_CONNECT] = "Connect",              [KEY_FINANCE] = "Finance",
726         [KEY_SPORT] = "Sport",                  [KEY_SHOP] = "Shop",
727         [KEY_ALTERASE] = "AlternateErase",      [KEY_CANCEL] = "Cancel",
728         [KEY_BRIGHTNESSDOWN] = "BrightnessDown", [KEY_BRIGHTNESSUP] = "BrightnessUp",
729         [KEY_MEDIA] = "Media",                  [KEY_UNKNOWN] = "Unknown",
730         [BTN_0] = "Btn0",                       [BTN_1] = "Btn1",
731         [BTN_2] = "Btn2",                       [BTN_3] = "Btn3",
732         [BTN_4] = "Btn4",                       [BTN_5] = "Btn5",
733         [BTN_6] = "Btn6",                       [BTN_7] = "Btn7",
734         [BTN_8] = "Btn8",                       [BTN_9] = "Btn9",
735         [BTN_LEFT] = "LeftBtn",                 [BTN_RIGHT] = "RightBtn",
736         [BTN_MIDDLE] = "MiddleBtn",             [BTN_SIDE] = "SideBtn",
737         [BTN_EXTRA] = "ExtraBtn",               [BTN_FORWARD] = "ForwardBtn",
738         [BTN_BACK] = "BackBtn",                 [BTN_TASK] = "TaskBtn",
739         [BTN_TRIGGER] = "Trigger",              [BTN_THUMB] = "ThumbBtn",
740         [BTN_THUMB2] = "ThumbBtn2",             [BTN_TOP] = "TopBtn",
741         [BTN_TOP2] = "TopBtn2",                 [BTN_PINKIE] = "PinkieBtn",
742         [BTN_BASE] = "BaseBtn",                 [BTN_BASE2] = "BaseBtn2",
743         [BTN_BASE3] = "BaseBtn3",               [BTN_BASE4] = "BaseBtn4",
744         [BTN_BASE5] = "BaseBtn5",               [BTN_BASE6] = "BaseBtn6",
745         [BTN_DEAD] = "BtnDead",                 [BTN_A] = "BtnA",
746         [BTN_B] = "BtnB",                       [BTN_C] = "BtnC",
747         [BTN_X] = "BtnX",                       [BTN_Y] = "BtnY",
748         [BTN_Z] = "BtnZ",                       [BTN_TL] = "BtnTL",
749         [BTN_TR] = "BtnTR",                     [BTN_TL2] = "BtnTL2",
750         [BTN_TR2] = "BtnTR2",                   [BTN_SELECT] = "BtnSelect",
751         [BTN_START] = "BtnStart",               [BTN_MODE] = "BtnMode",
752         [BTN_THUMBL] = "BtnThumbL",             [BTN_THUMBR] = "BtnThumbR",
753         [BTN_TOOL_PEN] = "ToolPen",             [BTN_TOOL_RUBBER] = "ToolRubber",
754         [BTN_TOOL_BRUSH] = "ToolBrush",         [BTN_TOOL_PENCIL] = "ToolPencil",
755         [BTN_TOOL_AIRBRUSH] = "ToolAirbrush",   [BTN_TOOL_FINGER] = "ToolFinger",
756         [BTN_TOOL_MOUSE] = "ToolMouse",         [BTN_TOOL_LENS] = "ToolLens",
757         [BTN_TOUCH] = "Touch",                  [BTN_STYLUS] = "Stylus",
758         [BTN_STYLUS2] = "Stylus2",              [BTN_TOOL_DOUBLETAP] = "ToolDoubleTap",
759         [BTN_TOOL_TRIPLETAP] = "ToolTripleTap", [BTN_GEAR_DOWN] = "WheelBtn",
760         [BTN_GEAR_UP] = "Gear up",              [KEY_OK] = "Ok",
761         [KEY_SELECT] = "Select",                [KEY_GOTO] = "Goto",
762         [KEY_CLEAR] = "Clear",                  [KEY_POWER2] = "Power2",
763         [KEY_OPTION] = "Option",                [KEY_INFO] = "Info",
764         [KEY_TIME] = "Time",                    [KEY_VENDOR] = "Vendor",
765         [KEY_ARCHIVE] = "Archive",              [KEY_PROGRAM] = "Program",
766         [KEY_CHANNEL] = "Channel",              [KEY_FAVORITES] = "Favorites",
767         [KEY_EPG] = "EPG",                      [KEY_PVR] = "PVR",
768         [KEY_MHP] = "MHP",                      [KEY_LANGUAGE] = "Language",
769         [KEY_TITLE] = "Title",                  [KEY_SUBTITLE] = "Subtitle",
770         [KEY_ANGLE] = "Angle",                  [KEY_ZOOM] = "Zoom",
771         [KEY_MODE] = "Mode",                    [KEY_KEYBOARD] = "Keyboard",
772         [KEY_SCREEN] = "Screen",                [KEY_PC] = "PC",
773         [KEY_TV] = "TV",                        [KEY_TV2] = "TV2",
774         [KEY_VCR] = "VCR",                      [KEY_VCR2] = "VCR2",
775         [KEY_SAT] = "Sat",                      [KEY_SAT2] = "Sat2",
776         [KEY_CD] = "CD",                        [KEY_TAPE] = "Tape",
777         [KEY_RADIO] = "Radio",                  [KEY_TUNER] = "Tuner",
778         [KEY_PLAYER] = "Player",                [KEY_TEXT] = "Text",
779         [KEY_DVD] = "DVD",                      [KEY_AUX] = "Aux",
780         [KEY_MP3] = "MP3",                      [KEY_AUDIO] = "Audio",
781         [KEY_VIDEO] = "Video",                  [KEY_DIRECTORY] = "Directory",
782         [KEY_LIST] = "List",                    [KEY_MEMO] = "Memo",
783         [KEY_CALENDAR] = "Calendar",            [KEY_RED] = "Red",
784         [KEY_GREEN] = "Green",                  [KEY_YELLOW] = "Yellow",
785         [KEY_BLUE] = "Blue",                    [KEY_CHANNELUP] = "ChannelUp",
786         [KEY_CHANNELDOWN] = "ChannelDown",      [KEY_FIRST] = "First",
787         [KEY_LAST] = "Last",                    [KEY_AB] = "AB",
788         [KEY_NEXT] = "Next",                    [KEY_RESTART] = "Restart",
789         [KEY_SLOW] = "Slow",                    [KEY_SHUFFLE] = "Shuffle",
790         [KEY_BREAK] = "Break",                  [KEY_PREVIOUS] = "Previous",
791         [KEY_DIGITS] = "Digits",                [KEY_TEEN] = "TEEN",
792         [KEY_TWEN] = "TWEN",                    [KEY_DEL_EOL] = "DeleteEOL",
793         [KEY_DEL_EOS] = "DeleteEOS",            [KEY_INS_LINE] = "InsertLine",
794         [KEY_DEL_LINE] = "DeleteLine",
795         [KEY_SEND] = "Send",                    [KEY_REPLY] = "Reply",
796         [KEY_FORWARDMAIL] = "ForwardMail",      [KEY_SAVE] = "Save",
797         [KEY_DOCUMENTS] = "Documents",          [KEY_SPELLCHECK] = "SpellCheck",
798         [KEY_LOGOFF] = "Logoff",
799         [KEY_FN] = "Fn",                        [KEY_FN_ESC] = "Fn+ESC",
800         [KEY_FN_1] = "Fn+1",                    [KEY_FN_2] = "Fn+2",
801         [KEY_FN_B] = "Fn+B",                    [KEY_FN_D] = "Fn+D",
802         [KEY_FN_E] = "Fn+E",                    [KEY_FN_F] = "Fn+F",
803         [KEY_FN_S] = "Fn+S",
804         [KEY_FN_F1] = "Fn+F1",                  [KEY_FN_F2] = "Fn+F2",
805         [KEY_FN_F3] = "Fn+F3",                  [KEY_FN_F4] = "Fn+F4",
806         [KEY_FN_F5] = "Fn+F5",                  [KEY_FN_F6] = "Fn+F6",
807         [KEY_FN_F7] = "Fn+F7",                  [KEY_FN_F8] = "Fn+F8",
808         [KEY_FN_F9] = "Fn+F9",                  [KEY_FN_F10] = "Fn+F10",
809         [KEY_FN_F11] = "Fn+F11",                [KEY_FN_F12] = "Fn+F12",
810         [KEY_KBDILLUMTOGGLE] = "KbdIlluminationToggle",
811         [KEY_KBDILLUMDOWN] = "KbdIlluminationDown",
812         [KEY_KBDILLUMUP] = "KbdIlluminationUp",
813         [KEY_SWITCHVIDEOMODE] = "SwitchVideoMode",
814 };
815
816 static const char *relatives[REL_MAX + 1] = {
817         [REL_X] = "X",                  [REL_Y] = "Y",
818         [REL_Z] = "Z",                  [REL_RX] = "Rx",
819         [REL_RY] = "Ry",                [REL_RZ] = "Rz",
820         [REL_HWHEEL] = "HWheel",        [REL_DIAL] = "Dial",
821         [REL_WHEEL] = "Wheel",          [REL_MISC] = "Misc",
822 };
823
824 static const char *absolutes[ABS_CNT] = {
825         [ABS_X] = "X",                  [ABS_Y] = "Y",
826         [ABS_Z] = "Z",                  [ABS_RX] = "Rx",
827         [ABS_RY] = "Ry",                [ABS_RZ] = "Rz",
828         [ABS_THROTTLE] = "Throttle",    [ABS_RUDDER] = "Rudder",
829         [ABS_WHEEL] = "Wheel",          [ABS_GAS] = "Gas",
830         [ABS_BRAKE] = "Brake",          [ABS_HAT0X] = "Hat0X",
831         [ABS_HAT0Y] = "Hat0Y",          [ABS_HAT1X] = "Hat1X",
832         [ABS_HAT1Y] = "Hat1Y",          [ABS_HAT2X] = "Hat2X",
833         [ABS_HAT2Y] = "Hat2Y",          [ABS_HAT3X] = "Hat3X",
834         [ABS_HAT3Y] = "Hat 3Y",         [ABS_PRESSURE] = "Pressure",
835         [ABS_DISTANCE] = "Distance",    [ABS_TILT_X] = "XTilt",
836         [ABS_TILT_Y] = "YTilt",         [ABS_TOOL_WIDTH] = "ToolWidth",
837         [ABS_VOLUME] = "Volume",        [ABS_MISC] = "Misc",
838         [ABS_MT_TOUCH_MAJOR] = "MTMajor",
839         [ABS_MT_TOUCH_MINOR] = "MTMinor",
840         [ABS_MT_WIDTH_MAJOR] = "MTMajorW",
841         [ABS_MT_WIDTH_MINOR] = "MTMinorW",
842         [ABS_MT_ORIENTATION] = "MTOrientation",
843         [ABS_MT_POSITION_X] = "MTPositionX",
844         [ABS_MT_POSITION_Y] = "MTPositionY",
845         [ABS_MT_TOOL_TYPE] = "MTToolType",
846         [ABS_MT_BLOB_ID] = "MTBlobID",
847 };
848
849 static const char *misc[MSC_MAX + 1] = {
850         [MSC_SERIAL] = "Serial",        [MSC_PULSELED] = "Pulseled",
851         [MSC_GESTURE] = "Gesture",      [MSC_RAW] = "RawData"
852 };
853
854 static const char *leds[LED_MAX + 1] = {
855         [LED_NUML] = "NumLock",         [LED_CAPSL] = "CapsLock",
856         [LED_SCROLLL] = "ScrollLock",   [LED_COMPOSE] = "Compose",
857         [LED_KANA] = "Kana",            [LED_SLEEP] = "Sleep",
858         [LED_SUSPEND] = "Suspend",      [LED_MUTE] = "Mute",
859         [LED_MISC] = "Misc",
860 };
861
862 static const char *repeats[REP_MAX + 1] = {
863         [REP_DELAY] = "Delay",          [REP_PERIOD] = "Period"
864 };
865
866 static const char *sounds[SND_MAX + 1] = {
867         [SND_CLICK] = "Click",          [SND_BELL] = "Bell",
868         [SND_TONE] = "Tone"
869 };
870
871 static const char **names[EV_MAX + 1] = {
872         [EV_SYN] = syncs,                       [EV_KEY] = keys,
873         [EV_REL] = relatives,                   [EV_ABS] = absolutes,
874         [EV_MSC] = misc,                        [EV_LED] = leds,
875         [EV_SND] = sounds,                      [EV_REP] = repeats,
876 };
877
878 static void hid_resolv_event(__u8 type, __u16 code, struct seq_file *f)
879 {
880         seq_printf(f, "%s.%s", events[type] ? events[type] : "?",
881                 names[type] ? (names[type][code] ? names[type][code] : "?") : "?");
882 }
883
884 static void hid_dump_input_mapping(struct hid_device *hid, struct seq_file *f)
885 {
886         int i, j, k;
887         struct hid_report *report;
888         struct hid_usage *usage;
889
890         for (k = HID_INPUT_REPORT; k <= HID_OUTPUT_REPORT; k++) {
891                 list_for_each_entry(report, &hid->report_enum[k].report_list, list) {
892                         for (i = 0; i < report->maxfield; i++) {
893                                 for ( j = 0; j < report->field[i]->maxusage; j++) {
894                                         usage = report->field[i]->usage + j;
895                                         hid_resolv_usage(usage->hid, f);
896                                         seq_printf(f, " ---> ");
897                                         hid_resolv_event(usage->type, usage->code, f);
898                                         seq_printf(f, "\n");
899                                 }
900                         }
901                 }
902         }
903
904 }
905
906
907 static int hid_debug_rdesc_show(struct seq_file *f, void *p)
908 {
909         struct hid_device *hdev = f->private;
910         int i;
911
912         /* dump HID report descriptor */
913         for (i = 0; i < hdev->rsize; i++)
914                 seq_printf(f, "%02x ", hdev->rdesc[i]);
915         seq_printf(f, "\n\n");
916
917         /* dump parsed data and input mappings */
918         hid_dump_device(hdev, f);
919         seq_printf(f, "\n");
920         hid_dump_input_mapping(hdev, f);
921
922         return 0;
923 }
924
925 static int hid_debug_rdesc_open(struct inode *inode, struct file *file)
926 {
927         return single_open(file, hid_debug_rdesc_show, inode->i_private);
928 }
929
930 static int hid_debug_events_open(struct inode *inode, struct file *file)
931 {
932         int err = 0;
933         struct hid_debug_list *list;
934
935         if (!(list = kzalloc(sizeof(struct hid_debug_list), GFP_KERNEL))) {
936                 err = -ENOMEM;
937                 goto out;
938         }
939
940         if (!(list->hid_debug_buf = kzalloc(sizeof(char) * HID_DEBUG_BUFSIZE, GFP_KERNEL))) {
941                 err = -ENOMEM;
942                 kfree(list);
943                 goto out;
944         }
945         list->hdev = (struct hid_device *) inode->i_private;
946         file->private_data = list;
947         mutex_init(&list->read_mutex);
948
949         list_add_tail(&list->node, &list->hdev->debug_list);
950
951 out:
952         return err;
953 }
954
955 static ssize_t hid_debug_events_read(struct file *file, char __user *buffer,
956                 size_t count, loff_t *ppos)
957 {
958         struct hid_debug_list *list = file->private_data;
959         int ret = 0, len;
960         DECLARE_WAITQUEUE(wait, current);
961
962         mutex_lock(&list->read_mutex);
963         while (ret == 0) {
964                 if (list->head == list->tail) {
965                         add_wait_queue(&list->hdev->debug_wait, &wait);
966                         set_current_state(TASK_INTERRUPTIBLE);
967
968                         while (list->head == list->tail) {
969                                 if (file->f_flags & O_NONBLOCK) {
970                                         ret = -EAGAIN;
971                                         break;
972                                 }
973                                 if (signal_pending(current)) {
974                                         ret = -ERESTARTSYS;
975                                         break;
976                                 }
977
978                                 if (!list->hdev || !list->hdev->debug) {
979                                         ret = -EIO;
980                                         break;
981                                 }
982
983                                 /* allow O_NONBLOCK from other threads */
984                                 mutex_unlock(&list->read_mutex);
985                                 schedule();
986                                 mutex_lock(&list->read_mutex);
987                                 set_current_state(TASK_INTERRUPTIBLE);
988                         }
989
990                         set_current_state(TASK_RUNNING);
991                         remove_wait_queue(&list->hdev->debug_wait, &wait);
992                 }
993
994                 if (ret)
995                         goto out;
996
997                 /* pass the ringbuffer contents to userspace */
998 copy_rest:
999                 if (list->tail == list->head)
1000                         goto out;
1001                 if (list->tail > list->head) {
1002                         len = list->tail - list->head;
1003
1004                         if (copy_to_user(buffer + ret, &list->hid_debug_buf[list->head], len)) {
1005                                 ret = -EFAULT;
1006                                 goto out;
1007                         }
1008                         ret += len;
1009                         list->head += len;
1010                 } else {
1011                         len = HID_DEBUG_BUFSIZE - list->head;
1012
1013                         if (copy_to_user(buffer, &list->hid_debug_buf[list->head], len)) {
1014                                 ret = -EFAULT;
1015                                 goto out;
1016                         }
1017                         list->head = 0;
1018                         ret += len;
1019                         goto copy_rest;
1020                 }
1021
1022         }
1023 out:
1024         mutex_unlock(&list->read_mutex);
1025         return ret;
1026 }
1027
1028 static unsigned int hid_debug_events_poll(struct file *file, poll_table *wait)
1029 {
1030         struct hid_debug_list *list = file->private_data;
1031
1032         poll_wait(file, &list->hdev->debug_wait, wait);
1033         if (list->head != list->tail)
1034                 return POLLIN | POLLRDNORM;
1035         if (!list->hdev->debug)
1036                 return POLLERR | POLLHUP;
1037         return 0;
1038 }
1039
1040 static int hid_debug_events_release(struct inode *inode, struct file *file)
1041 {
1042         struct hid_debug_list *list = file->private_data;
1043
1044         list_del(&list->node);
1045         kfree(list->hid_debug_buf);
1046         kfree(list);
1047
1048         return 0;
1049 }
1050
1051 static const struct file_operations hid_debug_rdesc_fops = {
1052         .open           = hid_debug_rdesc_open,
1053         .read           = seq_read,
1054         .llseek         = seq_lseek,
1055         .release        = single_release,
1056 };
1057
1058 static const struct file_operations hid_debug_events_fops = {
1059         .owner =        THIS_MODULE,
1060         .open           = hid_debug_events_open,
1061         .read           = hid_debug_events_read,
1062         .poll           = hid_debug_events_poll,
1063         .release        = hid_debug_events_release,
1064         .llseek         = noop_llseek,
1065 };
1066
1067
1068 void hid_debug_register(struct hid_device *hdev, const char *name)
1069 {
1070         hdev->debug_dir = debugfs_create_dir(name, hid_debug_root);
1071         hdev->debug_rdesc = debugfs_create_file("rdesc", 0400,
1072                         hdev->debug_dir, hdev, &hid_debug_rdesc_fops);
1073         hdev->debug_events = debugfs_create_file("events", 0400,
1074                         hdev->debug_dir, hdev, &hid_debug_events_fops);
1075         hdev->debug = 1;
1076 }
1077
1078 void hid_debug_unregister(struct hid_device *hdev)
1079 {
1080         hdev->debug = 0;
1081         wake_up_interruptible(&hdev->debug_wait);
1082         debugfs_remove(hdev->debug_rdesc);
1083         debugfs_remove(hdev->debug_events);
1084         debugfs_remove(hdev->debug_dir);
1085 }
1086
1087 void hid_debug_init(void)
1088 {
1089         hid_debug_root = debugfs_create_dir("hid", NULL);
1090 }
1091
1092 void hid_debug_exit(void)
1093 {
1094         debugfs_remove_recursive(hid_debug_root);
1095 }
1096