summaryrefslogtreecommitdiff
path: root/proof-of-concept/lomoco_reconnect.c
blob: f8479f74dc0e50be3e519f29316a820ae6f19ada (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/*
 * gcc -o lomoco_reconnect lomoco_reconnect.c
 *
 * Reconnect proof of concept code
 *
 * Copyright 2008 (c) Andreas Schneider <mail@cynapses.org>
 *
 * License: GPLv2 or later
 */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <asm/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <linux/hiddev.h>

#define VERSION "0.0.1"

#define VENDOR 0x046d
#define MOUSE_VX 0xc518
#define MOUSE_VX_NANO 0xc521
#define MOUSE_MX 0xc51a

static int send_report(int fd, int id, const unsigned int *buf, size_t size) {
  struct hiddev_report_info rinfo;
  struct hiddev_usage_ref uref;
  int i, err;

  for (i = 0; i < size; i++) {
    memset(&uref, 0, sizeof(uref));
    uref.report_type = HID_REPORT_TYPE_OUTPUT;
    uref.report_id   = id;
    uref.field_index = 0;
    uref.usage_index = i;
    uref.usage_code  = 0xff000001;
    uref.value       = buf[i];

    err = ioctl(fd, HIDIOCSUSAGE, &uref);
    if (err < 0)
      return err;
  }

  memset(&rinfo, 0, sizeof(rinfo));
  rinfo.report_type = HID_REPORT_TYPE_OUTPUT;
  rinfo.report_id   = id;
  rinfo.num_fields  = 1;
  err = ioctl(fd, HIDIOCSREPORT, &rinfo);

  return err;
}

static int query_report(int fd, int id, unsigned int *buf, size_t size) {
  struct hiddev_usage_ref_multi uref;
  struct hiddev_report_info rinfo;
  int i = 0, rc = -1;

  rinfo.report_type = HID_REPORT_TYPE_INPUT;
  rinfo.report_id = id;
  rinfo.num_fields = 1;
  rc = ioctl(fd, HIDIOCGREPORT, &rinfo);
  if (rc < 0) {
    perror("HIDIOCGREPORT");
    return rc;
  }

  uref.uref.report_type = HID_REPORT_TYPE_INPUT;
  uref.uref.report_id = id;
  uref.uref.field_index = 0;
  uref.uref.usage_index = 0;
  uref.num_values = size;
  rc = ioctl(fd, HIDIOCGUSAGES, &uref);
  if (rc < 0) {
    perror("HIDIOCGUSAGES");
    return rc;
  }

  for (i = 0; i < size; i++) {
    buf[i] = uref.values[i];
  }

  return rc;
}

void send_msg(int fd, int id, int c0, int c1, int c2, int c3, int c4, int c5)
{
  int rc = -1;
  unsigned int b[6];

  b[0] = c0;
  b[1] = c1;
  b[2] = c2;
  b[3] = c3;
  b[4] = c4;
  b[5] = c5;

  rc = send_report(fd, id, b, 6);

  if (rc < 0) {
    perror("error sending to device");
    close(fd);
    exit(1);
  }
}

int main (int argc, char **argv) {

  int fd = -1;
  int i = 0;
  int version = 0;
  struct hiddev_devinfo device_info;
  unsigned int buf[6] = {0};

  printf("lomoco reconnect version: %s\n\n", VERSION);

  /* ioctl() requires a file descriptor, so we check we got one, and
     then open it */
  if (argc != 2) {
    fprintf(stderr, "Usage: %s hiddevice\n - hiddevice probably /dev/usb/hiddev0\n", argv[0]);
    exit(1);
  }
  if ((fd = open(argv[1], O_RDONLY)) < 0) {
    perror("hiddev open");
    exit(1);
  }

  /* ioctl() accesses the underlying driver */
  ioctl(fd, HIDIOCGVERSION, &version);

  /* the HIDIOCGVERSION ioctl() returns an int
   * so we unpack it and display it
   * we create a patch
   */
  printf("hiddev driver version is %d.%d.%d\n", version >> 16, (version >> 8) & 0xff, version & 0xff);

  /* suck out some device information */
  ioctl(fd, HIDIOCGDEVINFO, &device_info);

  /* the HIDIOCGDEVINFO ioctl() returns hiddev_devinfo
   * structure - see <linux/hiddev.h> 
   * So we work through the various elements, displaying 
   * each of them 
   */
  printf("vendor 0x%04hx product 0x%04hx version 0x%04hx ",
      device_info.vendor, device_info.product,
      device_info.version);
  printf("has %i application%s ", device_info.num_applications,
      (device_info.num_applications==1?"":"s"));
  printf("and is on bus: %d   devnum: %d   ifnum: %d\n",
      device_info.busnum, device_info.devnum,
      device_info.ifnum);

  /* We have a G5? */
  if((device_info.vendor == (short)VENDOR) &&
      ((device_info.product == (short)MOUSE_VX) ||
      (device_info.product == (short)MOUSE_VX_NANO) ||
      (device_info.product == (short)MOUSE_MX))) {
    char dev[256] = {0};

    ioctl(fd, HIDIOCGNAME(255), &dev);

    if(device_info.product == (short)MOUSE_VX)
      printf(">>  VX Revolution (%s) detected!\n", dev);

    if(device_info.product == (short)MOUSE_VX_NANO)
      printf(">>  VX Nano (%s) detected!\n", dev);

    if(device_info.product == (short)MOUSE_MX)
      printf(">>  MX Revolution (%s) detected!\n", dev);

    /*
     * Initialise the internal report structures
     */
    if (ioctl(fd, HIDIOCINITREPORT, 0) < 0) {
      perror("hid report init failed");
      exit(1);
    }

    printf(">> Reconnect the mouse to receiver ...\n");
    printf("1. Turn on the mouse. If the mouse doesn't turn on, check the battery level\n\n");
    printf("2. Press and hold the left mouse button.\n\n");
    printf("3. While still holding the left mouse button.\n");
    printf("   Use the power button to switch of the mouse and back on while holding the left mouse button\n\n");
    printf("4. While still holding the left button, click the right mouse button 5 times,\n");
    printf("   then release the left mouse button.\n\n");
    printf("5. The mouse should be reconnected to the receiver in a few seconds.\n");

    send_msg(fd, 0x10, 0xff, 0x80, 0xb2, 0x01, 0x00, 0x1d);
  }

  close(fd);

  exit(0);
}