daily pastebin goal
10%
SHARE
TWEET

Untitled

a guest Aug 13th, 2012 339 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * gcc -o g_hack g_hack.c
  3.  * ./g_hack /dev/hidraw0
  4.  *
  5.  * g_hack - proof of conecpt code
  6.  *
  7.  *
  8.  * Copyright (c) 2006-2009 Andreas Schneider <mail@cynapses.org>
  9.  * Copyright (c) 2006-2007 Peter Feuerer <piie@piie.net>
  10.  *
  11.  * License: GPLv2 or later
  12.  *
  13.  * Additional Info:
  14.  *
  15.  * the G-series has two modes:
  16.  *
  17.  * 1. mode: no driver
  18.  * in this mode there is a list of resolutions implemented in the mouse
  19.  * and you can switch between them using the buttons
  20.  *
  21.  * 2. mode is: windows driver
  22.  * in this mode the buttons talk to the driver
  23.  
  24.  * they have absolute no effect on the hardware in first place
  25.  * the driver gets the button event looks in its table, which resolution to use
  26.  * sends it to the mouse sets the leds and the resolution
  27.  */
  28. #include <stdlib.h>
  29. #include <stdio.h>
  30. #include <string.h>
  31. #include <sys/ioctl.h>
  32. #include <sys/types.h>
  33. #include <sys/stat.h>
  34. #include <asm/types.h>
  35. #include <fcntl.h>
  36. #include <unistd.h>
  37. #include <linux/hiddev.h>
  38.  
  39. #define VERSION "0.0.7"
  40.  
  41. #define VENDOR 0x046d
  42. #define MOUSE_G5 0xc041
  43. #define MOUSE_G7 0xc51a
  44.  
  45. // Icehawk78 - first insertion for new mouse product code
  46. #define MOUSE_MX1100 0xc526
  47.  
  48. /* thanks to Michael "sanni" Dewein */
  49. #define MOUSE_G3 0xc042
  50.  
  51. /* thanks to Niels "aboe" Abspoel */
  52. #define MOUSE_G9 0xc048
  53.  
  54.  
  55. static int send_report(int fd, const unsigned char *buf, size_t size) {
  56.   struct hiddev_report_info rinfo;
  57.   struct hiddev_usage_ref uref;
  58.   int i, err;
  59.  
  60.   for (i = 0; i < size; i++) {
  61.     memset(&uref, 0, sizeof(uref));
  62.     uref.report_type = HID_REPORT_TYPE_OUTPUT;
  63.     uref.report_id   = 0x10;
  64.     uref.field_index = 0;
  65.     uref.usage_index = i;
  66.     uref.usage_code  = 0xff000001;
  67.     uref.value       = buf[i];
  68.  
  69.     err = ioctl(fd, HIDIOCSUSAGE, &uref);
  70.  
  71.     if (err < 0)
  72.       return err;
  73.   }
  74.  
  75.   memset(&rinfo, 0, sizeof(rinfo));
  76.   rinfo.report_type = HID_REPORT_TYPE_OUTPUT;
  77.   rinfo.report_id   = 0x10;
  78.   rinfo.num_fields  = 1;
  79.   err = ioctl(fd, HIDIOCSREPORT, &rinfo);
  80.  
  81.   return err;
  82. }
  83.  
  84. void send_msg(int fd,char c0,char c1,char c2, char c3, char c4, char c5)
  85. {
  86.   unsigned char b[6];
  87.   b[0]=c0;
  88.   b[1]=c1;
  89.   b[2]=c2;
  90.   b[3]=c3;
  91.   b[4]=c4;
  92.   b[5]=c5;
  93.  
  94.   if(send_report(fd,b,6)<0)
  95.   {
  96.     perror("error sending to device");
  97.     close(fd);
  98.     exit(1);
  99.   }
  100. }
  101. int main (int argc, char **argv) {
  102.  
  103.   int fd = -1;
  104.   int version;
  105.   int resolution=0;
  106.   struct hiddev_devinfo device_info;
  107.  
  108.   printf("g5hack version: %s\n\n", VERSION);
  109.  
  110.   /* ioctl() requires a file descriptor, so we check we got one, and
  111.      then open it */
  112.   if (argc != 3) {
  113.     fprintf(stderr, "Usage: %s hiddevice resolution\n - hiddevice probably /dev/usb/hiddev0 or /dev/hidraw0\n - resolution: 0=400 1=800 2=1600 3=2000\n"
  114.         "only g9: 4-7 (7=3200)\n\n", argv[0]);
  115.  
  116.     exit(1);
  117.   }
  118.   if ((fd = open(argv[1], O_RDONLY)) < 0) {
  119.     perror("hiddev open");
  120.     exit(1);
  121.   }
  122.   resolution=atoi(argv[2]);
  123.  
  124.   /* ioctl() accesses the underlying driver */
  125.   ioctl(fd, HIDIOCGVERSION, &version);
  126.  
  127.   /* the HIDIOCGVERSION ioctl() returns an int
  128.    * so we unpack it and display it
  129.    * we create a patch
  130.    */
  131.   printf("hiddev driver version is %d.%d.%d\n", version >> 16, (version >> 8) & 0xff, version & 0xff);
  132.  
  133.   /* suck out some device information */
  134.   ioctl(fd, HIDIOCGDEVINFO, &device_info);
  135.  
  136.   /* the HIDIOCGDEVINFO ioctl() returns hiddev_devinfo
  137.  
  138.    * structure - see <linux/hiddev.h>
  139.    * So we work through the various elements, displaying
  140.    * each of them
  141.    */
  142.   printf("vendor 0x%04hx product 0x%04hx version 0x%04hx ",
  143.       device_info.vendor, device_info.product,
  144.       device_info.version);
  145.   printf("has %i application%s ", device_info.num_applications,
  146.       (device_info.num_applications==1?"":"s"));
  147.   printf("and is on bus: %d   devnum: %d   ifnum: %d\n",
  148.       device_info.busnum, device_info.devnum,
  149.       device_info.ifnum);
  150.  
  151. // Icehawk78 - Second section - need to add in the check so that the product continues with your mouse code. The printf isn't necessary, and was mostly added for consistency sake.
  152.   /* We have a G5? */
  153.   if((device_info.vendor == (short)VENDOR) &&
  154.       ((device_info.product == (short)MOUSE_MX1100) ||
  155.        (device_info.product == (short)MOUSE_G3) ||
  156.        (device_info.product == (short)MOUSE_G9) ||
  157.        (device_info.product == (short)MOUSE_G5) ||
  158.        (device_info.product == (short)MOUSE_G7))) {
  159.  
  160.     if(device_info.product == (short)MOUSE_MX1100)
  161.       printf(">> MX1100 Mouse detected!\n");
  162.  
  163.     if(device_info.product == (short)MOUSE_G3)
  164.       printf(">>  G3 Gaming Mouse detected!\n");
  165.  
  166.     if(device_info.product == (short)MOUSE_G9)
  167.       printf(">>  G9 Gaming Mouse detected!\n");
  168.  
  169.     if(device_info.product == (short)MOUSE_G5)
  170.       printf(">>  G5 Gaming Mouse detected!\n");
  171.  
  172.     if(device_info.product == (short)MOUSE_G7)
  173.       printf(">>  G7 cordless Gaming Mouse detected!\n");
  174.     /*
  175.      * Initialise the internal report structures
  176.      *
  177.      * The G5 has an additional HID Interface
  178.      * with a report descriptor.
  179.      */
  180.     if (ioctl(fd, HIDIOCINITREPORT, 0) < 0) {
  181.       perror("hid report init failed");
  182.       exit(1);
  183.  
  184.     }
  185.  
  186. /* Icehawk78 - Now we're getting into the meat of it.
  187.    This is the first place that you'll have to change.
  188.    On my mouse, when changing between any two non-DPI-related modes,
  189.    I captured a total of three URB_CONTROL out signals. The first and the third matched
  190.    the first four digits [10 01 80 65] while the second did not [10 01 80 63].
  191.    The second signal appears to be the first required signal, which I added here as
  192.    a super-hacky conditional. Note that you drop the first 0x10 because send_msg()
  193.    is already including that, for some reason. */
  194.  
  195.     /* disable speed buttons */
  196.     if (device_info.product != (short)MOUSE_MX1100) {
  197.       send_msg(fd,0x00, 0x80, 0x01, 0x00, 0x00, 0x00);
  198.     } else {
  199.       send_msg(fd,0x01,0x80,0x63,0x82,0x00,0x00);
  200.     }
  201.  
  202. /* Icehawk78 - This is the second location to change.
  203.    The first/third capture codes I put into the hacky if block inside
  204.    case 0 and case 1; case 0 enables the buttons, case 1 sets it back to
  205.    DPI mode. */
  206.  
  207.     switch(resolution){
  208.       case 0:
  209.         if (device_info.product == (short)MOUSE_MX1100) {
  210.           send_msg(fd,0x01,0x80,0x65,0x82,0x85,0xFF);
  211.           break;
  212.         } else {
  213.           /* set current speed to 400dpi */
  214.           send_msg(fd,0x00,0x80,0x63,0x80,0x00,0x00);
  215.  
  216.           /* turn off all speed leds */
  217.           send_msg(fd,0x00,0x80,0x51,0x11,0x01,0x00);
  218.           break;
  219.         }
  220.  
  221.       case 1:
  222.         if (device_info.product != (short)MOUSE_MX1100) {
  223.           /* set current speed to 800dpi */
  224.           send_msg(fd,0x00,0x80,0x63,0x81,0x00,0x00);
  225.           /* turn on first led */
  226.           send_msg(fd,0x00,0x80,0x51,0x12,0x01,0x00);
  227.           break;
  228.         } else {
  229.           send_msg(fd,0x00,0x80,0x65,0x82,0x85,0x9A);
  230.           break;
  231.         }
  232.  
  233.       case 2:
  234.         /* set current speed to 1600dpi */
  235.         send_msg(fd,0x00, 0x80, 0x63, 0x82, 0x00, 0x00);
  236.         /* turn on second led */
  237.         send_msg(fd,0x00, 0x80, 0x51, 0x21, 0x01, 0x00);
  238.  
  239.         break;
  240.  
  241.       case 3:
  242.         /* set current speed to 2000dpi */
  243.         send_msg(fd,0x00, 0x80, 0x63, 0x83, 0x00, 0x00);
  244.         /* turn on third led */
  245.         send_msg(fd,0x00, 0x80, 0x51, 0x11, 0x02, 0x00);
  246.         break;
  247.  
  248.       case 4:
  249.         /* set current speed to whatever dpi
  250.          * - only available for g9 */
  251.         if(device_info.product == (short)MOUSE_G9){
  252.           send_msg(fd,0x00, 0x80, 0x63, 0x84, 0x00, 0x00);
  253.           /* turn on third led */
  254.           send_msg(fd,0x00, 0x80, 0x51, 0x11, 0x02, 0x00);
  255.         }
  256.         else{
  257.           printf("speed not supported for your device\n");
  258.         }
  259.         break;
  260.  
  261.       case 5:
  262.         /* set current speed to whatever dpi
  263.          * - only available for g9 */
  264.         if(device_info.product == (short)MOUSE_G9){
  265.           send_msg(fd,0x00, 0x80, 0x63, 0x85, 0x00, 0x00);
  266.           /* turn on third led */
  267.           send_msg(fd,0x00, 0x80, 0x51, 0x11, 0x02, 0x00);
  268.         }
  269.         else{
  270.           printf("speed not supported for your device\n");
  271.         }
  272.         break;
  273.  
  274.       case 6:
  275.         /* set current speed to whatever dpi (most probably 3200)
  276.          * - only available for g9 */
  277.         if(device_info.product == (short)MOUSE_G9){
  278.           send_msg(fd,0x00, 0x80, 0x63, 0x86, 0x00, 0x00);
  279.           /* turn on third led */
  280.           send_msg(fd,0x00, 0x80, 0x51, 0x11, 0x02, 0x00);
  281.         }
  282.         else{
  283.  
  284.           printf("speed not supported for your device\n");
  285.         }
  286.         break;
  287.  
  288.       case 7:
  289.         /* set current speed to whatever dpi (most probably 3200)
  290.          * - only available for g9 */
  291.         if(device_info.product == (short)MOUSE_G9){
  292.           send_msg(fd,0x00, 0x80, 0x63, 0x87, 0x00, 0x00);
  293.           /* turn on third led */
  294.           send_msg(fd,0x00, 0x80, 0x51, 0x11, 0x02, 0x00);
  295.         }
  296.         else{
  297.           printf("speed not supported for your device\n");
  298.         }
  299.         break;
  300.  
  301.       default:
  302.         printf("speed not supported for your device\n");
  303.         break;
  304.     }
  305.  
  306.     /* foobar: */
  307.     /* at lowest speed ("-" button does not function anymore) */
  308.     /* send_msg(fd,0x00, 0x81, 0x51, 0x00, 0x00, 0x00); */
  309.  
  310.     /* turn off all speed leds */
  311.     /* send_msg(fd,0x00, 0x80, 0x51, 0x11, 0x01, 0x00); */
  312.  
  313.     /* turn on all speed leds */
  314.     /* send_msg(fd,0x00, 0x80, 0x51, 0x22, 0x02, 0x00); */
  315.  
  316.   } /* G5 */
  317.  
  318.   close(fd);
  319.  
  320.   exit(0);
  321. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top