aboutsummaryrefslogtreecommitdiffstats
path: root/librazer/hw_taipan.c
blob: 3b8e3e8484458de8763ad0d17c9aa5a75b17db31 (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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
/*
 *   Lowlevel hardware access for the
 *   Razer Taipan mouse
 *
 *   Important notice:
 *   This hardware driver is based on reverse engineering, only.
 *
 *   Copyright (C) 2007-2010 Michael Buesch <m@bues.ch>
 *   Driver modified for Taipan by Tibor Peluch <messani@gmail.com>
 *
 *   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_taipan.h"
#include "razer_private.h"

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


enum {
	TAIPAN_LED_SCROLL = 0,
	TAIPAN_LED_LOGO,
	TAIPAN_NR_LEDS,
};

enum { /* Misc constants */
	TAIPAN_NR_DPIMAPPINGS	= 82,
	TAIPAN_NR_AXES		= 3,
};

struct taipan_command {
	uint8_t status;
	uint8_t padding0[4];
	be16_t command;
	be16_t request;
	be16_t value0;
	be16_t value1;
	uint8_t padding1[75];
	uint8_t checksum;
	uint8_t padding2;
} _packed;

struct taipan_private {
	struct razer_mouse *m;

	/* Firmware version number. */
	uint16_t fw_version;
	/* The currently set LED states. */
	bool led_states[TAIPAN_NR_LEDS];
	/* The currently set frequency. */
	enum razer_mouse_freq frequency;
	/* The currently set resolution. */
	struct razer_mouse_dpimapping *cur_dpimapping_X;
	struct razer_mouse_dpimapping *cur_dpimapping_Y;

	struct razer_mouse_profile profile;
	struct razer_mouse_dpimapping dpimapping[TAIPAN_NR_DPIMAPPINGS];
	struct razer_axis axes[TAIPAN_NR_AXES];

	bool commit_pending;
};


static void taipan_command_init(struct taipan_command *cmd)
{
	memset(cmd, 0, sizeof(*cmd));
}

static int taipan_usb_write(struct taipan_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,
		(unsigned char *)buf, size,
		RAZER_USB_TIMEOUT);
	if (err < 0 || (size_t)err != size) {
		razer_error("razer-taipan: "
			"USB write 0x%02X 0x%02X failed: %d\n",
			request, command, err);
		return err;
	}
	return 0;
}

static int taipan_usb_read(struct taipan_private *priv,
			   int request, int command,
			   void *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,
		buf, size,
		RAZER_USB_TIMEOUT);
	if (err < 0 || (size_t)err != size) {
		razer_error("razer-taipan: "
			"USB read 0x%02X 0x%02X failed: %d\n",
			request, command, err);
		return err;
	}
	return 0;
}

static int taipan_send_command(struct taipan_private *priv,
			       struct taipan_command *cmd)
{
	int err;

	cmd->checksum = razer_xor8_checksum((uint8_t *)cmd + 2, sizeof(*cmd) - 4);
	err = taipan_usb_write(priv, LIBUSB_REQUEST_SET_CONFIGURATION, 0x300,
			       cmd, sizeof(*cmd));
	if (err)
		return err;
	err = taipan_usb_read(priv, LIBUSB_REQUEST_CLEAR_FEATURE, 0x300,
			      cmd, sizeof(*cmd));
	if (err)
		return err;
	if (cmd->status != 2 &&
	    cmd->status != 1 &&
	    cmd->status != 0) {
		razer_error("razer-taipan: Command %04X/%04X failed with %02X\n",
			    be16_to_cpu(cmd->command),
			    be16_to_cpu(cmd->request),
			    cmd->status);
	}

	return 0;
}

static int taipan_read_fw_ver(struct taipan_private *priv)
{
	struct taipan_command cmd;
	uint16_t ver;
	int err;
	unsigned int i;

	/* Poke the device several times until it responds with a
	 * valid version number */
	for (i = 0; i < 5; i++) {
		taipan_command_init(&cmd);
		cmd.command = cpu_to_be16(0x0200);
		cmd.request = cpu_to_be16(0x8100);
		err = taipan_send_command(priv, &cmd);
		ver = be16_to_cpu(cmd.value0);
		if (!err && (ver & 0xFF00) != 0)
			return ver;
		razer_msleep(100);
	}
	razer_error("razer-taipan: Failed to read firmware version\n");

	/* FIXME: Ignore the error and return 0 until we find out
	 *        why some mice fail to return a valid version number.
	 */
	return 0;
}

static int taipan_do_commit(struct taipan_private *priv)
{
	struct taipan_command cmd;
	unsigned int xres, yres;
	uint16_t freq;
	int err;

	/* Set the resolution. */
	taipan_command_init(&cmd);
	cmd.command = cpu_to_be16(0x0704);
	cmd.request = cpu_to_be16(0x0500);
	xres = (unsigned int)priv->cur_dpimapping_X->res[RAZER_DIM_0];
	yres = (unsigned int)priv->cur_dpimapping_Y->res[RAZER_DIM_0];
	cmd.value0 = cpu_to_be16(xres);
	cmd.value1 = cpu_to_be16(yres);
	err = taipan_send_command(priv, &cmd);
	if (err)
		return err;

	/* Set the scroll wheel and buttons LEDs. */
	taipan_command_init(&cmd);
	cmd.command = cpu_to_be16(0x0303);
	cmd.request = cpu_to_be16(0x0001);
	cmd.value0 = cpu_to_be16(0x0100);
	if (priv->led_states[TAIPAN_LED_SCROLL])
		cmd.value0 |= cpu_to_be16(0x0001);
	err = taipan_send_command(priv, &cmd);
	if (err)
		return err;

	/* Set the logo LED. */
	taipan_command_init(&cmd);
	cmd.command = cpu_to_be16(0x0303);
	cmd.request = cpu_to_be16(0x0001);
	cmd.value0 = cpu_to_be16(0x0400);
	if (priv->led_states[TAIPAN_LED_LOGO])
		cmd.value0 |= cpu_to_be16(0x0001);
	err = taipan_send_command(priv, &cmd);
	if (err)
		return err;

	/* Set scan frequency. */
	switch (priv->frequency) {
	case RAZER_MOUSE_FREQ_125HZ:
		freq = 0x0008;
		break;
	case RAZER_MOUSE_FREQ_500HZ:
		freq = 0x0002;
		break;
	case RAZER_MOUSE_FREQ_1000HZ:
	case RAZER_MOUSE_FREQ_UNKNOWN:
		freq = 0x0001;
		break;
	default:
		return -EINVAL;
	}
	taipan_command_init(&cmd);
	cmd.command = cpu_to_be16(0x0100);
	cmd.request = cpu_to_be16(0x0500);
	cmd.request |= cpu_to_be16(freq);
	err = taipan_send_command(priv, &cmd);
	if (err)
		return err;

	return 0;
}

static int taipan_get_fw_version(struct razer_mouse *m)
{
	struct taipan_private *priv = m->drv_data;

	return priv->fw_version;
}

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

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

	return err;
}

static int taipan_led_toggle(struct razer_led *led,
			     enum razer_led_state new_state)
{
	struct razer_mouse *m = led->u.mouse;
	struct taipan_private *priv = m->drv_data;

	if (led->id >= TAIPAN_NR_LEDS)
		return -EINVAL;
	if ((new_state != RAZER_LED_OFF) &&
	    (new_state != RAZER_LED_ON))
		return -EINVAL;

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

	priv->led_states[led->id] = new_state;
	priv->commit_pending = 1;

	return 0;
}

static int taipan_get_leds(struct razer_mouse *m,
			   struct razer_led **leds_list)
{
	struct taipan_private *priv = m->drv_data;
	struct razer_led *scroll, *logo;

	scroll = zalloc(sizeof(struct razer_led));
	if (!scroll)
		return -ENOMEM;
	logo = zalloc(sizeof(struct razer_led));
	if (!logo) {
		free(scroll);
		return -ENOMEM;
	}

	scroll->name = "Scrollwheel";
	scroll->id = TAIPAN_LED_SCROLL;
	scroll->state = priv->led_states[TAIPAN_LED_SCROLL];
	scroll->toggle_state = taipan_led_toggle;
	scroll->u.mouse = m;

	logo->name = "GlowingLogo";
	logo->id = TAIPAN_LED_LOGO;
	logo->state = priv->led_states[TAIPAN_LED_LOGO];
	logo->toggle_state = taipan_led_toggle;
	logo->u.mouse = m;

	/* Link the list */
	*leds_list = scroll;
	scroll->next = logo;
	logo->next = NULL;

	return TAIPAN_NR_LEDS;
}

static int taipan_supported_freqs(struct razer_mouse *m,
				  enum razer_mouse_freq **freq_list)
{
	enum razer_mouse_freq *list;
	const int count = 3;

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

	list[0] = RAZER_MOUSE_FREQ_125HZ;
	list[1] = RAZER_MOUSE_FREQ_500HZ;
	list[2] = RAZER_MOUSE_FREQ_1000HZ;

	*freq_list = list;

	return count;
}

static enum razer_mouse_freq taipan_get_freq(struct razer_mouse_profile *p)
{
	struct taipan_private *priv = p->mouse->drv_data;

	return priv->frequency;
}

static int taipan_set_freq(struct razer_mouse_profile *p,
			   enum razer_mouse_freq freq)
{
	struct taipan_private *priv = p->mouse->drv_data;

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

	priv->frequency = freq;
	priv->commit_pending = 1;

	return 0;
}

static int taipan_supported_axes(struct razer_mouse *m,
				 struct razer_axis **axes_list)
{
	struct taipan_private *priv = m->drv_data;

	*axes_list = priv->axes;

	return ARRAY_SIZE(priv->axes);
}

static int taipan_supported_resolutions(struct razer_mouse *m,
					enum razer_mouse_res **res_list)
{
	enum razer_mouse_res *list;
	unsigned int i;
	const unsigned int count = TAIPAN_NR_DPIMAPPINGS;

	list = zalloc(sizeof(*list) * count);
	if (!list)
		return -ENOMEM;
	for (i = 0; i < count; i++)
		list[i] = (i + 1) * 100;
	*res_list = list;

	return count;
}

static struct razer_mouse_profile * taipan_get_profiles(struct razer_mouse *m)
{
	struct taipan_private *priv = m->drv_data;

	return &priv->profile;
}

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

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

	return ARRAY_SIZE(priv->dpimapping);
}

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

	if (!axis)
		axis = &priv->axes[0];
	if (axis->id == 0)
		return priv->cur_dpimapping_X;
	if (axis->id == 1)
		return priv->cur_dpimapping_Y;

	return NULL;
}

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

	if (!priv->m->claim_count)
		return -EBUSY;
	if (axis && axis->id >= ARRAY_SIZE(priv->axes))
		return -EINVAL;

	if (axis) {
		if (axis->id == 0)
			priv->cur_dpimapping_X = d;
		else if (axis->id == 1)
			priv->cur_dpimapping_Y = d;
		else
			return -EINVAL;
	} else {
		priv->cur_dpimapping_X = d;
		priv->cur_dpimapping_Y = d;
	}
	priv->commit_pending = 1;

	return 0;
}

int razer_taipan_init(struct razer_mouse *m,
		      struct libusb_device *usbdev)
{
	struct taipan_private *priv;
	unsigned int i;
	int fwver, err;

	BUILD_BUG_ON(sizeof(struct taipan_command) != 90);

	priv = zalloc(sizeof(struct taipan_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)
		goto err_free;

	err = m->claim(m);
	if (err) {
		razer_error("hw_taipan: Failed to claim device\n");
		goto err_free;
	}

	/* Fetch firmware version */
	fwver = taipan_read_fw_ver(priv);
	if (fwver < 0) {
		err = fwver;
		goto err_release;
	}
	priv->fw_version = fwver;

	priv->frequency = RAZER_MOUSE_FREQ_1000HZ;
	for (i = 0; i < TAIPAN_NR_LEDS; i++)
		priv->led_states[i] = RAZER_LED_ON;

	priv->profile.nr = 0;
	priv->profile.get_freq = taipan_get_freq;
	priv->profile.set_freq = taipan_set_freq;
	priv->profile.get_dpimapping = taipan_get_dpimapping;
	priv->profile.set_dpimapping = taipan_set_dpimapping;
	priv->profile.mouse = m;

	for (i = 0; i < TAIPAN_NR_DPIMAPPINGS; i++) {
		priv->dpimapping[i].nr = i;
		priv->dpimapping[i].res[RAZER_DIM_0] = (i + 1) * 100;
		if (priv->dpimapping[i].res[RAZER_DIM_0] == 1000) {
			priv->cur_dpimapping_X = &priv->dpimapping[i];
			priv->cur_dpimapping_Y = &priv->dpimapping[i];
		}
		priv->dpimapping[i].dimension_mask = (1 << RAZER_DIM_0);
		priv->dpimapping[i].change = NULL;
		priv->dpimapping[i].mouse = m;
	}
	razer_init_axes(&priv->axes[0],
			"X", RAZER_AXIS_INDEPENDENT_DPIMAPPING,
			"Y", RAZER_AXIS_INDEPENDENT_DPIMAPPING,
			"Scroll", 0);

	m->type = RAZER_MOUSETYPE_TAIPAN;
	razer_generic_usb_gen_idstr(usbdev, m->usb_ctx->h, "Taipan", 1,
				    NULL, m->idstr);

	m->get_fw_version = taipan_get_fw_version;
	m->commit = taipan_commit;
	m->global_get_leds = taipan_get_leds;
	m->get_profiles = taipan_get_profiles;
	m->supported_axes = taipan_supported_axes;
	m->supported_resolutions = taipan_supported_resolutions;
	m->supported_freqs = taipan_supported_freqs;
	m->supported_dpimappings = taipan_supported_dpimappings;

	err = taipan_do_commit(priv);
	if (err) {
		razer_error("hw_taipan: Failed to commit initial settings\n");
		goto err_release;
	}

	m->release(m);

	return 0;

err_release:
	m->release(m);
err_free:
	free(priv);
	return err;
}

void razer_taipan_release(struct razer_mouse *m)
{
	struct taipan_private *priv = m->drv_data;

	free(priv);
}
bues.ch cgit interface