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