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