Linux Audio

Check our new training course

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