aboutsummaryrefslogtreecommitdiffstats
path: root/librazer/hw_krait.c
blob: 8ab40bf49c4d2d46927af72bafbd1b55a2b9e509 (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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/*
 *   Lowlevel hardware access for the
 *   Razer Krait mouse
 *
 *   Important notice:
 *   This hardware driver is based on reverse engineering, only.
 *
 *   Copyright (C) 2007-2011 Michael Buesch <m@bues.ch>
 *
 *   This program is free software; you can redistribute it and/or
 *   modify it under the terms of the GNU General Public License
 *   as published by the Free Software Foundation; either version 2
 *   of the License, or (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 */

#include "hw_krait.h"
#include "razer_private.h"

#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>


struct krait_private {
	struct razer_mouse *m;

	struct razer_mouse_dpimapping *cur_dpimapping;
	struct razer_mouse_profile profile;
	struct razer_mouse_dpimapping dpimapping[2];

	bool commit_pending;
};


static int krait_usb_write(struct krait_private *priv,
			   int request, int command,
			   void *buf, size_t size)
{
	int err;

	err = libusb_control_transfer(
		priv->m->usb_ctx->h,
		LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_CLASS |
		LIBUSB_RECIPIENT_INTERFACE,
		request, command, 0,
		buf, size,
		RAZER_USB_TIMEOUT);
	if (err < 0 || (size_t)err != size)
		return err;
	return 0;
}

#if 0
static int krait_usb_read(struct krait_private *priv,
			  int request, int command,
			  char *buf, size_t size)
{
	int err;

	err = libusb_control_transfer(
		priv->m->usb_ctx->h,
		LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_CLASS |
		LIBUSB_RECIPIENT_INTERFACE,
		request, command, 0,
		(unsigned char *)buf, size,
		RAZER_USB_TIMEOUT);
	if (err < 0 || (size_t)err != size)
		return err;
	return 0;
}
#endif

static int krait_do_commit(struct krait_private *priv)
{
	uint8_t value;
	int err;

	switch (priv->cur_dpimapping->res[RAZER_DIM_0]) {
	case RAZER_MOUSE_RES_400DPI:
		value = 6;
		break;
	case RAZER_MOUSE_RES_1600DPI:
		value = 4;
		break;
	default:
		return -EINVAL;
	}
	err = krait_usb_write(priv, LIBUSB_REQUEST_SET_CONFIGURATION,
			      0x02, &value, sizeof(value));
	if (err)
		return err;

	return 0;
}

static int krait_commit(struct razer_mouse *m, int force)
{
	struct krait_private *priv = m->drv_data;
	int err = 0;

	if (!m->claim_count)
		return -EBUSY;
	if (priv->commit_pending || force) {
		err = krait_do_commit(priv);
		if (!err)
			priv->commit_pending = 0;
	}

	return err;
}

static int krait_supported_resolutions(struct razer_mouse *m,
				       enum razer_mouse_res **res_list)
{
	enum razer_mouse_res *list;
	const int count = 2;

	list = zalloc(sizeof(*list) * count);
	if (!list)
		return -ENOMEM;

	list[0] = RAZER_MOUSE_RES_400DPI;
	list[1] = RAZER_MOUSE_RES_1600DPI;

	*res_list = list;

	return count;
}

static struct razer_mouse_profile * krait_get_profiles(struct razer_mouse *m)
{
	struct krait_private *priv = m->drv_data;

	return &priv->profile;
}

static int krait_supported_dpimappings(struct razer_mouse *m,
				       struct razer_mouse_dpimapping **res_ptr)
{
	struct krait_private *priv = m->drv_data;

	*res_ptr = &priv->dpimapping[0];

	return ARRAY_SIZE(priv->dpimapping);
}

static struct razer_mouse_dpimapping * krait_get_dpimapping(struct razer_mouse_profile *p,
							    struct razer_axis *axis)
{
	struct krait_private *priv = p->mouse->drv_data;

	return priv->cur_dpimapping;
}

static int krait_set_dpimapping(struct razer_mouse_profile *p,
				struct razer_axis *axis,
				struct razer_mouse_dpimapping *d)
{
	struct krait_private *priv = p->mouse->drv_data;

	if (!priv->m->claim_count)
		return -EBUSY;

	priv->cur_dpimapping = d;
	priv->commit_pending = 1;

	return 0;
}

int razer_krait_init(struct razer_mouse *m,
		     struct libusb_device *usbdev)
{
	struct krait_private *priv;
	int err;

	priv = zalloc(sizeof(struct krait_private));
	if (!priv)
		return -ENOMEM;
	priv->m = m;
	m->drv_data = priv;

	err = razer_usb_add_used_interface(m->usb_ctx, 0, 0);
	if (err) {
		free(priv);
		return err;
	}

	priv->profile.nr = 0;
	priv->profile.get_dpimapping = krait_get_dpimapping;
	priv->profile.set_dpimapping = krait_set_dpimapping;
	priv->profile.mouse = m;

	priv->dpimapping[0].nr = 0;
	priv->dpimapping[0].res[RAZER_DIM_0] = RAZER_MOUSE_RES_400DPI;
	priv->dpimapping[0].dimension_mask = (1 << RAZER_DIM_0);
	priv->dpimapping[0].change = NULL;
	priv->dpimapping[0].mouse = m;

	priv->dpimapping[1].nr = 1;
	priv->dpimapping[1].res[RAZER_DIM_0] = RAZER_MOUSE_RES_1600DPI;
	priv->dpimapping[1].dimension_mask = (1 << RAZER_DIM_0);
	priv->dpimapping[1].change = NULL;
	priv->dpimapping[1].mouse = m;

	priv->cur_dpimapping = &priv->dpimapping[1];

	m->type = RAZER_MOUSETYPE_KRAIT;
	razer_generic_usb_gen_idstr(usbdev, NULL, "Krait", 1,
				    NULL, m->idstr);

	m->commit = krait_commit;
	m->get_profiles = krait_get_profiles;
	m->supported_resolutions = krait_supported_resolutions;
	m->supported_dpimappings = krait_supported_dpimappings;

	return 0;
}

void razer_krait_release(struct razer_mouse *m)
{
	struct krait_private *priv = m->drv_data;

	free(priv);
}
bues.ch cgit interface