Advertisement
Guest User

Untitled

a guest
Jul 5th, 2014
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.16 KB | None | 0 0
  1. Bus 004 Device 003: ID 1532:0102 Razer USA, Ltd Tarantula Keyboard
  2. ^^^^^^^^^^^^^^^ my lsusb output ^^^^^^^^^^^^^^^^^^^^^^^
  3.  
  4. #python driver below...
  5. #!/usr/bin/python
  6.  
  7. # blackwidow_enable.py
  8. #
  9. # Enables the M1-5 and FN keys to send scancodes on the Razer BlackWidow
  10. # and BlackWidow Ultimate keyboards.
  11. #
  12. # Requires the PyUSB library.
  13. #
  14. # By Michael Fincham <michael@finch.am> 2012-03-05
  15. # This code is released in to the public domain.
  16.  
  17. import sys
  18. import usb
  19.  
  20. USB_VENDOR = 0x1532 # Razer
  21. USB_PRODUCT = 0x010d # BlackWidow / BlackWidow Ultimate
  22.  
  23. # These values are from the USB HID 1.11 spec section 7.2.
  24. # <http://www.usb.org/developers/devclass_docs/HID1_11.pdf>
  25. USB_REQUEST_TYPE = 0x21 # Host To Device | Class | Interface
  26. USB_REQUEST = 0x09 # SET_REPORT
  27.  
  28. # These values are from the manufacturer's driver.
  29. USB_VALUE = 0x0300
  30. USB_INDEX = 0x2
  31. USB_INTERFACE = 2
  32.  
  33. # These magic bytes are also from the manufacturer's driver.
  34. USB_BUFFER = b"\x00\x00\x00\x00\x00\x01\x00\x04\x03\x00\x00\x00\x00\x00\
  35. \x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\
  36. \x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\
  37. \x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\
  38. \x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x06\x00"
  39.  
  40.  
  41. def find_keyboard_device():
  42. for bus in usb.busses():
  43. for device in bus.devices:
  44. if device.idVendor == USB_VENDOR and \
  45. device.idProduct == USB_PRODUCT:
  46. return device
  47.  
  48.  
  49. def main():
  50.  
  51. device = find_keyboard_device()
  52. if device == None:
  53. sys.stderr.write("BlackWidow not found.\n")
  54. sys.exit(1)
  55.  
  56. try:
  57. handle = device.open()
  58. interface = device.configurations[0].interfaces[USB_INTERFACE][0]
  59. endpoint = interface.endpoints[0]
  60. except:
  61. sys.stderr.write("Could not select configuration endpoint.\n")
  62. sys.exit(1)
  63.  
  64. try:
  65. handle.detachKernelDriver(interface)
  66. except usb.USBError:
  67. pass # This is usually because the enabler was run before and the kernel is still detached
  68. except Exception:
  69. sys.stderr.write("A very unusual error happened trying to detch the kernel driver.\n")
  70. sys.exit(1)
  71.  
  72. try:
  73. handle.claimInterface(interface)
  74. except:
  75. sys.stderr.write("Unable to claim the configuration interface. Do you have the appropriate privileges?\n")
  76. sys.exit(1)
  77.  
  78. result = 0
  79.  
  80. try:
  81. result = handle.controlMsg(requestType=USB_REQUEST_TYPE,
  82. request=USB_REQUEST,
  83. value=USB_VALUE,
  84. index=USB_INDEX,
  85. buffer=USB_BUFFER)
  86. except:
  87. sys.stderr.write("Could not write the magic bytes to the BlackWidow.\n")
  88.  
  89. if result == len(USB_BUFFER):
  90. sys.stderr.write("Configured BlackWidow.\n")
  91.  
  92. try:
  93. handle.releaseInterface()
  94. except:
  95. sys.stderr.write("Unable to release interface.\n")
  96. sys.exit(1)
  97.  
  98. sys.exit(0)
  99.  
  100. if __name__ == "__main__":
  101. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement