SHARE
TWEET

Corsair K70 RGB LED test program for Linux

CalcProgrammer1 Oct 7th, 2014 1,002 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <usb.h>
  3.  
  4. static struct usb_device *find_device(uint16_t vendor, uint16_t product);
  5.  
  6. static char data_pkt_0[] =
  7.   { 0x07,   0x27,   0x00,   0x00,   0xD8,   0x00,   0x00,   0x00,
  8.     0x00,   0x00,   0x00,   0x00,   0x00,   0x00,   0x00,   0x00,
  9.     0x00,   0x00,   0x00,   0x00,   0x00,   0x00,   0x00,   0x00,
  10.     0x00,   0x00,   0x00,   0x00,   0x00,   0x00,   0x00,   0x00,
  11.     0x00,   0x00,   0x00,   0x00,   0x00,   0x00,   0x00,   0x00,
  12.     0x00,   0x00,   0x00,   0x00,   0x00,   0x00,   0x00,   0x00,
  13.     0x00,   0x00,   0x00,   0x00,   0x00,   0x00,   0x00,   0x00,
  14.     0x00,   0x00,   0x00,   0x00,   0x00,   0x00,   0x00,   0x00 };
  15.  
  16. static char wht_pkt_1[] =
  17.   { 0x7F,   0x01,   0x3C,   0x00,   0x00,   0x00,   0x00,   0x00,
  18.     0x07,   0x77,   0x00,   0x00,   0x07,   0x70,   0x00,   0x77,
  19.     0x00,   0x00,   0x00,   0x00,   0x00,   0x77,   0x00,   0x00,
  20.     0x70,   0x00,   0x70,   0x77,   0x00,   0x00,   0x00,   0x00,
  21.     0x00,   0x77,   0x00,   0x00,   0x70,   0x00,   0x00,   0x77,
  22.     0x00,   0x00,   0x70,   0x00,   0x00,   0x77,   0x00,   0x00,
  23.     0x00,   0x00,   0x00,   0x77,   0x00,   0x00,   0x00,   0x00,
  24.     0x00,   0x77,   0x00,   0x00,   0x00,   0x07,   0x00,   0x77 };
  25.  
  26. static char wht_pkt_2[] =
  27.   { 0x7F,   0x02,   0x3C,   0x00,   0x00,   0x00,   0x70,   0x00,
  28.     0x00,   0x77,   0x00,   0x00,   0x00,   0x07,   0x00,   0x77,
  29.     0x00,   0x00,   0x00,   0x00,   0x07,   0x77,   0x00,   0x00,
  30.     0x07,   0x70,   0x00,   0x77,   0x00,   0x00,   0x00,   0x00,
  31.     0x00,   0x77,   0x00,   0x00,   0x70,   0x00,   0x70,   0x77,
  32.     0x00,   0x00,   0x00,   0x00,   0x00,   0x77,   0x00,   0x00,
  33.     0x70,   0x00,   0x00,   0x77,   0x00,   0x00,   0x70,   0x00,
  34.     0x00,   0x77,   0x00,   0x00,   0x00,   0x00,   0x00,   0x77 };
  35.  
  36. static char wht_pkt_3[] =
  37.   { 0x7F,   0x03,   0x3C,   0x00,   0x00,   0x00,   0x00,   0x00,
  38.     0x00,   0x77,   0x00,   0x00,   0x00,   0x07,   0x00,   0x77,
  39.     0x00,   0x00,   0x70,   0x00,   0x00,   0x77,   0x00,   0x00,
  40.     0x00,   0x07,   0x00,   0x77,   0x00,   0x00,   0x00,   0x00,
  41.     0x07,   0x77,   0x00,   0x00,   0x07,   0x70,   0x00,   0x77,
  42.     0x00,   0x00,   0x00,   0x00,   0x00,   0x77,   0x00,   0x00,
  43.     0x70,   0x00,   0x70,   0x77,   0x00,   0x00,   0x00,   0x00,
  44.     0x00,   0x77,   0x00,   0x00,   0x70,   0x00,   0x00,   0x77 };
  45.  
  46. static char wht_pkt_4[] =
  47.   { 0x7F,   0x04,   0x24,   0x00,   0x00,   0x00,   0x70,   0x00,
  48.     0x00,   0x77,   0x00,   0x00,   0x00,   0x00,   0x00,   0x77,
  49.     0x00,   0x00,   0x00,   0x00,   0x00,   0x77,   0x00,   0x00,
  50.     0x00,   0x07,   0x00,   0x77,   0x00,   0x00,   0x70,   0x00,
  51.     0x00,   0x77,   0x00,   0x00,   0x00,   0x07,   0x00,   0x77,
  52.     0x00,   0x00,   0x00,   0x00,   0x00,   0x00,   0x00,   0x00,
  53.     0x00,   0x00,   0x00,   0x00,   0x00,   0x00,   0x00,   0x00,
  54.     0x00,   0x00,   0x00,   0x00,   0x00,   0x00,   0x00,   0x00 };
  55.  
  56. static char blk_pkt_1[] =
  57.   { 0x7F,   0x01,   0x3C,   0x00,   0x77,   0x77,   0x77,   0x77,
  58.     0x77,   0x77,   0x77,   0x77,   0x77,   0x77,   0x77,   0x77,
  59.     0x77,   0x77,   0x77,   0x77,   0x77,   0x77,   0x77,   0x77,
  60.     0x77,   0x77,   0x77,   0x77,   0x77,   0x77,   0x77,   0x77,
  61.     0x77,   0x77,   0x77,   0x77,   0x77,   0x77,   0x77,   0x77,
  62.     0x77,   0x77,   0x77,   0x77,   0x77,   0x77,   0x77,   0x77,
  63.     0x77,   0x77,   0x77,   0x77,   0x77,   0x77,   0x77,   0x77,
  64.     0x77,   0x77,   0x77,   0x77,   0x77,   0x77,   0x77,   0x77 };
  65.  
  66. static char blk_pkt_2[] =
  67.   { 0x7F,   0x02,   0x3C,   0x00,   0x77,   0x77,   0x77,   0x77,
  68.     0x77,   0x77,   0x77,   0x77,   0x07,   0x77,   0x77,   0x77,
  69.     0x77,   0x77,   0x77,   0x77,   0x77,   0x77,   0x77,   0x77,
  70.     0x77,   0x77,   0x77,   0x77,   0x77,   0x77,   0x77,   0x77,
  71.     0x77,   0x77,   0x77,   0x77,   0x77,   0x77,   0x77,   0x77,
  72.     0x77,   0x77,   0x77,   0x77,   0x77,   0x77,   0x77,   0x77,
  73.     0x77,   0x77,   0x77,   0x77,   0x77,   0x77,   0x77,   0x77,
  74.     0x77,   0x77,   0x77,   0x77,   0x77,   0x77,   0x77,   0x77 };
  75.  
  76. static char blk_pkt_3[] =
  77.   { 0x7F,   0x03,   0x3C,   0x00,   0x77,   0x77,   0x77,   0x77,
  78.     0x77,   0x77,   0x77,   0x77,   0x77,   0x77,   0x77,   0x77,
  79.     0x77,   0x77,   0x77,   0x77,   0x77,   0x77,   0x77,   0x77,
  80.     0x07,   0x77,   0x77,   0x77,   0x77,   0x77,   0x77,   0x77,
  81.     0x77,   0x77,   0x77,   0x77,   0x77,   0x77,   0x77,   0x77,
  82.     0x77,   0x77,   0x77,   0x77,   0x77,   0x77,   0x77,   0x77,
  83.     0x77,   0x77,   0x77,   0x77,   0x77,   0x77,   0x77,   0x77,
  84.     0x77,   0x77,   0x77,   0x77,   0x77,   0x77,   0x77,   0x77 };
  85.  
  86. static char blk_pkt_4[] =
  87.   { 0x7F,   0x04,   0x24,   0x00,   0x77,   0x77,   0x77,   0x77,
  88.     0x77,   0x77,   0x77,   0x77,   0x77,   0x77,   0x77,   0x77,
  89.     0x77,   0x77,   0x77,   0x77,   0x77,   0x77,   0x77,   0x77,
  90.     0x77,   0x77,   0x77,   0x77,   0x77,   0x77,   0x77,   0x77,
  91.     0x77,   0x77,   0x77,   0x77,   0x07,   0x77,   0x77,   0x77,
  92.     0x00,   0x00,   0x00,   0x00,   0x00,   0x00,   0x00,   0x00,
  93.     0x00,   0x00,   0x00,   0x00,   0x00,   0x00,   0x00,   0x00,
  94.     0x00,   0x00,   0x00,   0x00,   0x00,   0x00,   0x00,   0x00 };
  95.  
  96. int main(int argc, char **argv)
  97. {
  98.     struct usb_device *dev;
  99.     struct usb_dev_handle *handle;
  100.     int bytes_written = 0;
  101.     int status = 0;
  102.  
  103.     printf("Searching for Corsair K70 RGB keyboard...\n");
  104.    
  105.     dev = find_device(0x1B1C, 0x1B13);
  106.  
  107.     if(dev == NULL)
  108.     {
  109.         printf("Corsair K70 RGB keyboard not detected :(\n");
  110.         return 1;
  111.     }
  112.     else
  113.     {
  114.         printf("Corsair K70 RGB keyboard detected successfully :)\n");
  115.     }    
  116.  
  117.     handle = usb_open(dev);
  118.  
  119.     if(handle == NULL)
  120.     {
  121.         printf("USB device handle did not open :(\n");
  122.         return 1;
  123.     }
  124.     else
  125.     {
  126.         printf("USB device handle opened successfully :)\n");
  127.     }
  128.  
  129.     status = usb_claim_interface(handle, 3);
  130.    
  131.     usb_detach_kernel_driver_np(handle, 3);
  132.     //if(status == 0)
  133.     //{
  134.     //    printf("USB interface claimed successfully :)\n");
  135.     //}
  136.     //else
  137.     //{
  138.     //    printf("USB interface claim failed with error %d :(\n", status);
  139.     //    return 1;
  140.     //}
  141.  
  142.     // Perform USB control message to keyboard
  143.     //
  144.     // Request Type:  0x21
  145.     // Request:       0x09
  146.     // Value          0x0300
  147.     // Index:         0x03
  148.     // Size:          64
  149.  
  150.  
  151.     bytes_written = usb_control_msg(handle, 0x21, 0x09, 0x0300, 0x03, wht_pkt_1, 64, 1000);
  152.     bytes_written = usb_control_msg(handle, 0x21, 0x09, 0x0300, 0x03, wht_pkt_2, 64, 1000);
  153.     bytes_written = usb_control_msg(handle, 0x21, 0x09, 0x0300, 0x03, wht_pkt_3, 64, 1000);
  154.     bytes_written = usb_control_msg(handle, 0x21, 0x09, 0x0300, 0x03, wht_pkt_4, 64, 1000);
  155.     bytes_written = usb_control_msg(handle, 0x21, 0x09, 0x0300, 0x03, data_pkt_0, 64, 1000);
  156.  
  157.     sleep(1);
  158.  
  159.     bytes_written = usb_control_msg(handle, 0x21, 0x09, 0x0300, 0x03, blk_pkt_1, 64, 1000);
  160.     bytes_written = usb_control_msg(handle, 0x21, 0x09, 0x0300, 0x03, blk_pkt_2, 64, 1000);
  161.     bytes_written = usb_control_msg(handle, 0x21, 0x09, 0x0300, 0x03, blk_pkt_3, 64, 1000);
  162.     bytes_written = usb_control_msg(handle, 0x21, 0x09, 0x0300, 0x03, blk_pkt_4, 64, 1000);
  163.     bytes_written = usb_control_msg(handle, 0x21, 0x09, 0x0300, 0x03, data_pkt_0, 64, 1000);
  164.  
  165.     return 0;
  166. }
  167.  
  168. static struct usb_device *find_device(uint16_t vendor, uint16_t product)
  169. {
  170.     struct usb_bus *bus;
  171.     struct usb_device *dev;
  172.     struct usb_bus *busses;
  173.  
  174.     usb_init();
  175.     usb_find_busses();
  176.     usb_find_devices();
  177.  
  178.     busses = usb_get_busses();
  179.  
  180.     for(bus = busses; bus; bus = bus->next)
  181.     {
  182.         for(dev = bus->devices; dev; dev = dev->next)
  183.         {
  184.             if((dev->descriptor.idVendor == vendor) && (dev->descriptor.idProduct == product))
  185.             {
  186.                 return dev;
  187.             }
  188.         }
  189.     }
  190.  
  191.     return NULL;
  192. }
RAW Paste Data
Top