hw_deathadder.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802
  1. /*
  2. * Lowlevel hardware access for the
  3. * Razer Deathadder mouse
  4. *
  5. * Important notice:
  6. * This hardware driver is based on reverse engineering and
  7. * hardware documentation provided under NDA.
  8. *
  9. * Copyright (C) 2007-2011 Michael Buesch <m@bues.ch>
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License
  13. * as published by the Free Software Foundation; either version 2
  14. * of the License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. */
  21. #include "hw_deathadder.h"
  22. #include "razer_private.h"
  23. #include "cypress_bootloader.h"
  24. #include <errno.h>
  25. #include <stdlib.h>
  26. #include <stdio.h>
  27. #include <stdint.h>
  28. #include <string.h>
  29. enum deathadder_type {
  30. DEATHADDER_CLASSIC, /* DeathAdder Classic */
  31. DEATHADDER_3500, /* DeathAdder 3500DPI */
  32. DEATHADDER_BLACK, /* DeathAdder Black Edition */
  33. };
  34. enum {
  35. DEATHADDER_LED_SCROLL = 0,
  36. DEATHADDER_LED_LOGO,
  37. DEATHADDER_NR_LEDS,
  38. };
  39. /* This is the device configuration structure sent to the device
  40. * for firmware version >= 1.25 */
  41. struct deathadder_125_cfg {
  42. uint8_t freq;
  43. uint8_t res;
  44. uint8_t profile;
  45. uint8_t leds;
  46. } _packed;
  47. struct deathadder_private {
  48. struct razer_mouse *m;
  49. /* The deathadder hardware revision type */
  50. enum deathadder_type type;
  51. bool in_bootloader;
  52. /* Firmware version number. */
  53. uint16_t fw_version;
  54. /* The currently set LED states. */
  55. bool led_states[DEATHADDER_NR_LEDS];
  56. /* The currently set frequency. */
  57. enum razer_mouse_freq frequency;
  58. /* Previous freq. For predicting reconnect events only. */
  59. enum razer_mouse_freq old_frequency;
  60. /* The currently set resolution. */
  61. struct razer_mouse_dpimapping *cur_dpimapping;
  62. struct razer_mouse_profile profile;
  63. struct razer_mouse_dpimapping dpimapping[4];
  64. bool commit_pending;
  65. struct razer_event_spacing commit_spacing;
  66. };
  67. #define DADD_FW(major, minor) (((major) << 8) | (minor))
  68. #define DEATHADDER_FW_IMAGE_SIZE 0x4000
  69. static int deathadder_usb_write(struct deathadder_private *priv,
  70. int request, int command,
  71. void *buf, size_t size)
  72. {
  73. int err;
  74. if (priv->in_bootloader) {
  75. /* Deathadder firmware is down and we're in the bootloader. */
  76. return 0;
  77. }
  78. err = libusb_control_transfer(
  79. priv->m->usb_ctx->h,
  80. LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_CLASS |
  81. LIBUSB_RECIPIENT_INTERFACE,
  82. request, command, 0,
  83. (unsigned char *)buf, size,
  84. RAZER_USB_TIMEOUT);
  85. if (err < 0 || (size_t)err != size) {
  86. razer_error("razer-deathadder: "
  87. "USB write 0x%02X 0x%02X failed: %d\n",
  88. request, command, err);
  89. return err;
  90. }
  91. return 0;
  92. }
  93. static int deathadder_usb_read(struct deathadder_private *priv,
  94. int request, int command,
  95. void *buf, size_t size)
  96. {
  97. int err;
  98. if (priv->in_bootloader) {
  99. /* Deathadder firmware is down and we're in the bootloader. */
  100. memset(buf, 0, size);
  101. return 0;
  102. }
  103. err = libusb_control_transfer(
  104. priv->m->usb_ctx->h,
  105. LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_CLASS |
  106. LIBUSB_RECIPIENT_INTERFACE,
  107. request, command, 0,
  108. buf, size,
  109. RAZER_USB_TIMEOUT);
  110. if (err < 0 || (size_t)err != size) {
  111. razer_error("razer-deathadder: "
  112. "USB read 0x%02X 0x%02X failed: %d\n",
  113. request, command, err);
  114. return err;
  115. }
  116. return 0;
  117. }
  118. static int deathadder_read_fw_ver(struct deathadder_private *priv)
  119. {
  120. char buf[2];
  121. uint16_t ver;
  122. int err;
  123. err = deathadder_usb_read(priv, LIBUSB_REQUEST_CLEAR_FEATURE,
  124. 0x05, buf, sizeof(buf));
  125. if (err)
  126. return err;
  127. ver = buf[0];
  128. ver <<= 8;
  129. ver |= buf[1];
  130. return ver;
  131. }
  132. static int deathadder_do_commit(struct deathadder_private *priv)
  133. {
  134. struct razer_usb_reconnect_guard guard;
  135. int i, err;
  136. if (priv->in_bootloader)
  137. return 0;
  138. razer_event_spacing_enter(&priv->commit_spacing);
  139. err = razer_usb_reconnect_guard_init(&guard, priv->m->usb_ctx);
  140. if (err)
  141. goto out;
  142. if (priv->type == DEATHADDER_CLASSIC &&
  143. priv->fw_version < DADD_FW(1,25)) {
  144. char value, freq_value, res_value;
  145. /* Translate frequency setting. */
  146. switch (priv->frequency) {
  147. case RAZER_MOUSE_FREQ_125HZ:
  148. freq_value = 3;
  149. break;
  150. case RAZER_MOUSE_FREQ_500HZ:
  151. freq_value = 2;
  152. break;
  153. case RAZER_MOUSE_FREQ_1000HZ:
  154. case RAZER_MOUSE_FREQ_UNKNOWN:
  155. freq_value = 1;
  156. break;
  157. default:
  158. err = -EINVAL;
  159. goto out;
  160. }
  161. /* Translate resolution setting. */
  162. switch (priv->cur_dpimapping->res[RAZER_DIM_0]) {
  163. case RAZER_MOUSE_RES_450DPI:
  164. res_value = 3;
  165. break;
  166. case RAZER_MOUSE_RES_900DPI:
  167. res_value = 2;
  168. break;
  169. case RAZER_MOUSE_RES_1800DPI:
  170. case RAZER_MOUSE_RES_UNKNOWN:
  171. res_value = 1;
  172. break;
  173. default:
  174. err = -EINVAL;
  175. goto out;
  176. }
  177. if (priv->old_frequency != priv->frequency) {
  178. /* Commit frequency setting. */
  179. err = deathadder_usb_write(priv, LIBUSB_REQUEST_SET_CONFIGURATION,
  180. 0x07, &freq_value, sizeof(freq_value));
  181. if (err)
  182. goto out;
  183. /* The frequency setting changed. The device firmware
  184. * will reboot the mouse now. This will cause a reconnect
  185. * on the USB bus. Call the guard... */
  186. err = razer_usb_reconnect_guard_wait(&guard, 0);
  187. if (err)
  188. goto out;
  189. /* The device needs a bit of punching in the face after reconnect. */
  190. for (i = 0; i < 5; i++) {
  191. int ver = deathadder_read_fw_ver(priv);
  192. if ((ver > 0) && ((ver & 0xFFFF) == priv->fw_version))
  193. break;
  194. razer_msleep(100);
  195. }
  196. if (i >= 5) {
  197. razer_error("razer-deathadder: The device didn't wake up "
  198. "after a frequency change. Try to replug it.\n");
  199. }
  200. }
  201. /* Commit LED states. */
  202. value = 0;
  203. if (priv->led_states[DEATHADDER_LED_LOGO])
  204. value |= 0x01;
  205. if (priv->led_states[DEATHADDER_LED_SCROLL])
  206. value |= 0x02;
  207. err = deathadder_usb_write(priv, LIBUSB_REQUEST_SET_CONFIGURATION,
  208. 0x06, &value, sizeof(value));
  209. if (err)
  210. goto out;
  211. /* Commit resolution setting. */
  212. err = deathadder_usb_write(priv, LIBUSB_REQUEST_SET_CONFIGURATION,
  213. 0x09, &res_value, sizeof(res_value));
  214. if (err)
  215. goto out;
  216. } else {
  217. struct deathadder_125_cfg config = { 0, };
  218. /* Translate frequency setting. */
  219. switch (priv->frequency) {
  220. case RAZER_MOUSE_FREQ_125HZ:
  221. config.freq = 3;
  222. break;
  223. case RAZER_MOUSE_FREQ_500HZ:
  224. config.freq = 2;
  225. break;
  226. case RAZER_MOUSE_FREQ_1000HZ:
  227. case RAZER_MOUSE_FREQ_UNKNOWN:
  228. config.freq = 1;
  229. break;
  230. default:
  231. err = -EINVAL;
  232. goto out;
  233. }
  234. /* Translate resolution setting. */
  235. if (priv->type == DEATHADDER_CLASSIC) {
  236. switch (priv->cur_dpimapping->res[RAZER_DIM_0]) {
  237. case RAZER_MOUSE_RES_450DPI:
  238. config.res = 3;
  239. break;
  240. case RAZER_MOUSE_RES_900DPI:
  241. config.res = 2;
  242. break;
  243. case RAZER_MOUSE_RES_1800DPI:
  244. case RAZER_MOUSE_RES_UNKNOWN:
  245. config.res = 1;
  246. break;
  247. default:
  248. err = -EINVAL;
  249. goto out;
  250. }
  251. } else {
  252. switch (priv->cur_dpimapping->res[RAZER_DIM_0]) {
  253. case RAZER_MOUSE_RES_450DPI:
  254. config.res = 4;
  255. break;
  256. case RAZER_MOUSE_RES_900DPI:
  257. config.res = 3;
  258. break;
  259. case RAZER_MOUSE_RES_1800DPI:
  260. config.res = 2;
  261. break;
  262. case RAZER_MOUSE_RES_3500DPI:
  263. case RAZER_MOUSE_RES_UNKNOWN:
  264. config.res = 1;
  265. break;
  266. default:
  267. err = -EINVAL;
  268. goto out;
  269. }
  270. }
  271. /* The profile ID. */
  272. config.profile = 1;
  273. /* Translate the LED states. */
  274. if (priv->type == DEATHADDER_BLACK) {
  275. /* There are no LEDs.
  276. * Bit 0 and 1 are always set, though. */
  277. config.leds = 0x03;
  278. } else {
  279. if (priv->led_states[DEATHADDER_LED_LOGO])
  280. config.leds |= 0x01;
  281. if (priv->led_states[DEATHADDER_LED_SCROLL])
  282. config.leds |= 0x02;
  283. }
  284. /* Commit the settings. */
  285. err = deathadder_usb_write(priv, LIBUSB_REQUEST_SET_CONFIGURATION,
  286. 0x10, &config, sizeof(config));
  287. if (err)
  288. goto out;
  289. if ((priv->type == DEATHADDER_CLASSIC ||
  290. priv->type == DEATHADDER_3500) &&
  291. priv->frequency != priv->old_frequency) {
  292. /* The frequency setting changed. The device firmware
  293. * will reboot the mouse now. This will cause a reconnect
  294. * on the USB bus. Call the guard... */
  295. err = razer_usb_reconnect_guard_wait(&guard, 0);
  296. if (err)
  297. goto out;
  298. /* The device has reconnected, so write the config
  299. * another time to ensure all settings are active.
  300. */
  301. err = deathadder_usb_write(priv, LIBUSB_REQUEST_SET_CONFIGURATION,
  302. 0x10, &config, sizeof(config));
  303. if (err)
  304. goto out;
  305. }
  306. if (priv->type == DEATHADDER_CLASSIC) {
  307. /* The device needs a bit of punching in the face.
  308. * Ensure it properly responds to read accesses. */
  309. for (i = 0; i < 5; i++) {
  310. int ver = deathadder_read_fw_ver(priv);
  311. if ((ver > 0) && ((ver & 0xFFFF) == priv->fw_version))
  312. break;
  313. razer_msleep(100);
  314. }
  315. if (i >= 5) {
  316. razer_error("razer-deathadder: The device didn't wake up "
  317. "after a config change. Try to replug it.\n");
  318. }
  319. }
  320. }
  321. priv->old_frequency = priv->frequency;
  322. err = 0;
  323. out:
  324. razer_event_spacing_leave(&priv->commit_spacing);
  325. return err;
  326. }
  327. static int deathadder_get_fw_version(struct razer_mouse *m)
  328. {
  329. struct deathadder_private *priv = m->drv_data;
  330. return priv->fw_version;
  331. }
  332. static int deathadder_commit(struct razer_mouse *m, int force)
  333. {
  334. struct deathadder_private *priv = m->drv_data;
  335. int err = 0;
  336. if (!m->claim_count)
  337. return -EBUSY;
  338. if (priv->commit_pending || force) {
  339. err = deathadder_do_commit(priv);
  340. if (!err)
  341. priv->commit_pending = 0;
  342. }
  343. return err;
  344. }
  345. static int deathadder_led_toggle(struct razer_led *led,
  346. enum razer_led_state new_state)
  347. {
  348. struct razer_mouse *m = led->u.mouse;
  349. struct deathadder_private *priv = m->drv_data;
  350. if (led->id >= DEATHADDER_NR_LEDS)
  351. return -EINVAL;
  352. if ((new_state != RAZER_LED_OFF) &&
  353. (new_state != RAZER_LED_ON))
  354. return -EINVAL;
  355. if (priv->type == DEATHADDER_BLACK)
  356. return -ENODEV;
  357. if (!m->claim_count)
  358. return -EBUSY;
  359. priv->led_states[led->id] = new_state;
  360. priv->commit_pending = 1;
  361. return 0;
  362. }
  363. static int deathadder_get_leds(struct razer_mouse *m,
  364. struct razer_led **leds_list)
  365. {
  366. struct deathadder_private *priv = m->drv_data;
  367. struct razer_led *scroll, *logo;
  368. if (priv->type == DEATHADDER_BLACK)
  369. return 0; /* No LEDs */
  370. scroll = zalloc(sizeof(struct razer_led));
  371. if (!scroll)
  372. return -ENOMEM;
  373. logo = zalloc(sizeof(struct razer_led));
  374. if (!logo) {
  375. free(scroll);
  376. return -ENOMEM;
  377. }
  378. scroll->name = "Scrollwheel";
  379. scroll->id = DEATHADDER_LED_SCROLL;
  380. scroll->state = priv->led_states[DEATHADDER_LED_SCROLL];
  381. scroll->toggle_state = deathadder_led_toggle;
  382. scroll->u.mouse = m;
  383. logo->name = "GlowingLogo";
  384. logo->id = DEATHADDER_LED_LOGO;
  385. logo->state = priv->led_states[DEATHADDER_LED_LOGO];
  386. logo->toggle_state = deathadder_led_toggle;
  387. logo->u.mouse = m;
  388. /* Link the list */
  389. *leds_list = scroll;
  390. scroll->next = logo;
  391. logo->next = NULL;
  392. return DEATHADDER_NR_LEDS;
  393. }
  394. static int deathadder_supported_freqs(struct razer_mouse *m,
  395. enum razer_mouse_freq **freq_list)
  396. {
  397. enum razer_mouse_freq *list;
  398. const int count = 3;
  399. list = zalloc(sizeof(*list) * count);
  400. if (!list)
  401. return -ENOMEM;
  402. list[0] = RAZER_MOUSE_FREQ_125HZ;
  403. list[1] = RAZER_MOUSE_FREQ_500HZ;
  404. list[2] = RAZER_MOUSE_FREQ_1000HZ;
  405. *freq_list = list;
  406. return count;
  407. }
  408. static enum razer_mouse_freq deathadder_get_freq(struct razer_mouse_profile *p)
  409. {
  410. struct deathadder_private *priv = p->mouse->drv_data;
  411. return priv->frequency;
  412. }
  413. static int deathadder_set_freq(struct razer_mouse_profile *p,
  414. enum razer_mouse_freq freq)
  415. {
  416. struct deathadder_private *priv = p->mouse->drv_data;
  417. if (!priv->m->claim_count)
  418. return -EBUSY;
  419. priv->frequency = freq;
  420. priv->commit_pending = 1;
  421. return 0;
  422. }
  423. static int deathadder_supported_resolutions(struct razer_mouse *m,
  424. enum razer_mouse_res **res_list)
  425. {
  426. struct deathadder_private *priv = m->drv_data;
  427. enum razer_mouse_res *list;
  428. const int count = (priv->type == DEATHADDER_CLASSIC) ? 3 : 4;
  429. list = zalloc(sizeof(*list) * count);
  430. if (!list)
  431. return -ENOMEM;
  432. list[0] = RAZER_MOUSE_RES_450DPI;
  433. list[1] = RAZER_MOUSE_RES_900DPI;
  434. list[2] = RAZER_MOUSE_RES_1800DPI;
  435. if (priv->type != DEATHADDER_CLASSIC)
  436. list[3] = RAZER_MOUSE_RES_3500DPI;
  437. *res_list = list;
  438. return count;
  439. }
  440. //TODO
  441. #if 0
  442. static struct usb_device * wait_for_usbdev(struct usb_device *dev,
  443. uint16_t vendor_id,
  444. uint16_t product_id)
  445. {
  446. struct usb_bus *bus, *buslist;
  447. unsigned int i;
  448. if (dev->descriptor.idVendor == vendor_id &&
  449. dev->descriptor.idProduct == product_id)
  450. return dev;
  451. for (i = 0; i < 100; i++) {
  452. usb_find_busses();
  453. usb_find_devices();
  454. buslist = usb_get_busses();
  455. for_each_usbbus(bus, buslist) {
  456. for_each_usbdev(dev, bus->devices) {
  457. if (dev->descriptor.idVendor == vendor_id &&
  458. dev->descriptor.idProduct == product_id)
  459. return dev;
  460. }
  461. }
  462. razer_msleep(100);
  463. }
  464. return NULL;
  465. }
  466. #endif
  467. static int deathadder_flash_firmware(struct razer_mouse *m,
  468. const char *data, size_t len,
  469. unsigned int magic_number)
  470. {
  471. struct deathadder_private *priv = m->drv_data;
  472. int err;
  473. char value;
  474. struct libusb_device *cydev;
  475. struct cypress cy;
  476. if (magic_number != RAZER_FW_FLASH_MAGIC)
  477. return -EINVAL;
  478. if (!m->claim_count)
  479. return -EBUSY;
  480. if (len != DEATHADDER_FW_IMAGE_SIZE) {
  481. razer_error("razer-deathadder: "
  482. "Firmware image has wrong size %u (expected %u).\n",
  483. (unsigned int)len,
  484. (unsigned int)DEATHADDER_FW_IMAGE_SIZE);
  485. return -EINVAL;
  486. }
  487. razer_msleep(50);
  488. if (priv->in_bootloader) {
  489. /* We're already inside of the bootloader */
  490. cydev = m->usb_ctx->dev;
  491. } else {
  492. /* Enter bootloader mode */
  493. value = 0;
  494. err = deathadder_usb_write(priv, LIBUSB_REQUEST_SET_CONFIGURATION,
  495. 0x08, &value, sizeof(value));
  496. if (err) {
  497. razer_error("razer-deathadder: Failed to enter the bootloader.\n");
  498. return err;
  499. }
  500. /* Wait for the cypress device to appear. */
  501. //TODO cydev = wait_for_usbdev(priv->usb.dev, CYPRESS_BOOT_VENDORID, CYPRESS_BOOT_PRODUCTID);
  502. cydev=NULL;
  503. if (!cydev) {
  504. razer_error("razer-deathadder: Cypress device didn't appear.\n");
  505. return -1;
  506. }
  507. }
  508. razer_msleep(100);
  509. err = cypress_open(&cy, cydev, NULL);
  510. if (err)
  511. return err;
  512. err = cypress_upload_image(&cy, data, len);
  513. cypress_close(&cy);
  514. if (err)
  515. return err;
  516. //FIXME need to reconnect the device?
  517. return 0;
  518. }
  519. static struct razer_mouse_profile * deathadder_get_profiles(struct razer_mouse *m)
  520. {
  521. struct deathadder_private *priv = m->drv_data;
  522. return &priv->profile;
  523. }
  524. static int deathadder_supported_dpimappings(struct razer_mouse *m,
  525. struct razer_mouse_dpimapping **res_ptr)
  526. {
  527. struct deathadder_private *priv = m->drv_data;
  528. *res_ptr = &priv->dpimapping[0];
  529. if (priv->type == DEATHADDER_CLASSIC)
  530. return ARRAY_SIZE(priv->dpimapping) - 1;
  531. return ARRAY_SIZE(priv->dpimapping);
  532. }
  533. static struct razer_mouse_dpimapping * deathadder_get_dpimapping(struct razer_mouse_profile *p,
  534. struct razer_axis *axis)
  535. {
  536. struct deathadder_private *priv = p->mouse->drv_data;
  537. return priv->cur_dpimapping;
  538. }
  539. static int deathadder_set_dpimapping(struct razer_mouse_profile *p,
  540. struct razer_axis *axis,
  541. struct razer_mouse_dpimapping *d)
  542. {
  543. struct deathadder_private *priv = p->mouse->drv_data;
  544. if (!priv->m->claim_count)
  545. return -EBUSY;
  546. priv->cur_dpimapping = d;
  547. priv->commit_pending = 1;
  548. return 0;
  549. }
  550. int razer_deathadder_init(struct razer_mouse *m,
  551. struct libusb_device *usbdev)
  552. {
  553. struct deathadder_private *priv;
  554. struct libusb_device_descriptor desc;
  555. unsigned int i;
  556. int err, fwver;
  557. const char *devname = "";
  558. err = libusb_get_device_descriptor(usbdev, &desc);
  559. if (err) {
  560. razer_error("hw_deathadder: Failed to get device descriptor\n");
  561. return -EIO;
  562. }
  563. priv = zalloc(sizeof(struct deathadder_private));
  564. if (!priv)
  565. return -ENOMEM;
  566. priv->m = m;
  567. m->drv_data = priv;
  568. priv->in_bootloader = is_cypress_bootloader(&desc);
  569. /* We need to wait some time between commits */
  570. razer_event_spacing_init(&priv->commit_spacing, 1000);
  571. err = razer_usb_add_used_interface(m->usb_ctx, 0, 0);
  572. if (err)
  573. goto err_free;
  574. if (!priv->in_bootloader && desc.idProduct == 0x0007) {
  575. err = razer_usb_force_hub_reset(m->usb_ctx);
  576. if (err) {
  577. razer_error("hw_deathadder: Failed to reinit USB device\n");
  578. goto err_free;
  579. }
  580. usbdev = m->usb_ctx->dev;
  581. }
  582. err = m->claim(m);
  583. if (err) {
  584. razer_error("hw_deathadder: Failed to claim device\n");
  585. goto err_free;
  586. }
  587. /* Fetch firmware version */
  588. fwver = deathadder_read_fw_ver(priv);
  589. if (fwver < 0) {
  590. razer_error("hw_deathadder: Failed to get firmware version\n");
  591. err = fwver;
  592. goto err_release;
  593. }
  594. priv->fw_version = fwver;
  595. /* Determine the hardware revision */
  596. priv->type = DEATHADDER_CLASSIC;
  597. if (desc.idVendor == 0x1532 && desc.idProduct == 0x0029) {
  598. priv->type = DEATHADDER_BLACK;
  599. } else {
  600. if (fwver >= DADD_FW(2,0))
  601. priv->type = DEATHADDER_3500;
  602. }
  603. priv->frequency = RAZER_MOUSE_FREQ_1000HZ;
  604. priv->old_frequency = priv->frequency;
  605. for (i = 0; i < DEATHADDER_NR_LEDS; i++)
  606. priv->led_states[i] = RAZER_LED_ON;
  607. priv->profile.nr = 0;
  608. priv->profile.get_freq = deathadder_get_freq;
  609. priv->profile.set_freq = deathadder_set_freq;
  610. priv->profile.get_dpimapping = deathadder_get_dpimapping;
  611. priv->profile.set_dpimapping = deathadder_set_dpimapping;
  612. priv->profile.mouse = m;
  613. priv->dpimapping[0].nr = 0;
  614. priv->dpimapping[0].res[RAZER_DIM_0] = RAZER_MOUSE_RES_450DPI;
  615. priv->dpimapping[0].dimension_mask = (1 << RAZER_DIM_0);
  616. priv->dpimapping[0].change = NULL;
  617. priv->dpimapping[0].mouse = m;
  618. priv->dpimapping[1].nr = 1;
  619. priv->dpimapping[1].res[RAZER_DIM_0] = RAZER_MOUSE_RES_900DPI;
  620. priv->dpimapping[1].dimension_mask = (1 << RAZER_DIM_0);
  621. priv->dpimapping[1].change = NULL;
  622. priv->dpimapping[1].mouse = m;
  623. priv->dpimapping[2].nr = 2;
  624. priv->dpimapping[2].res[RAZER_DIM_0] = RAZER_MOUSE_RES_1800DPI;
  625. priv->dpimapping[2].dimension_mask = (1 << RAZER_DIM_0);
  626. priv->dpimapping[2].change = NULL;
  627. priv->dpimapping[2].mouse = m;
  628. if (priv->type == DEATHADDER_CLASSIC) {
  629. priv->cur_dpimapping = &priv->dpimapping[2];
  630. } else {
  631. priv->dpimapping[3].nr = 3;
  632. priv->dpimapping[3].res[RAZER_DIM_0] = RAZER_MOUSE_RES_3500DPI;
  633. priv->dpimapping[3].dimension_mask = (1 << RAZER_DIM_0);
  634. priv->dpimapping[3].change = NULL;
  635. priv->dpimapping[3].mouse = m;
  636. priv->cur_dpimapping = &priv->dpimapping[3];
  637. }
  638. m->type = RAZER_MOUSETYPE_DEATHADDER;
  639. switch (priv->type) {
  640. case DEATHADDER_CLASSIC:
  641. devname = "DeathAdder Classic";
  642. break;
  643. case DEATHADDER_3500:
  644. devname = "DeathAdder 3500DPI";
  645. break;
  646. case DEATHADDER_BLACK:
  647. devname = "DeathAdder Black Edition";
  648. break;
  649. }
  650. razer_generic_usb_gen_idstr(usbdev, m->usb_ctx->h, devname, 0,
  651. NULL, m->idstr);
  652. m->get_fw_version = deathadder_get_fw_version;
  653. m->commit = deathadder_commit;
  654. m->flash_firmware = deathadder_flash_firmware;
  655. m->global_get_leds = deathadder_get_leds;
  656. m->get_profiles = deathadder_get_profiles;
  657. m->supported_resolutions = deathadder_supported_resolutions;
  658. m->supported_freqs = deathadder_supported_freqs;
  659. m->supported_dpimappings = deathadder_supported_dpimappings;
  660. err = deathadder_do_commit(priv);
  661. if (err) {
  662. razer_error("hw_deathadder: Failed to commit initial settings\n");
  663. goto err_release;
  664. }
  665. m->release(m);
  666. return 0;
  667. err_release:
  668. m->release(m);
  669. err_free:
  670. free(priv);
  671. return err;
  672. }
  673. void razer_deathadder_release(struct razer_mouse *m)
  674. {
  675. struct deathadder_private *priv = m->drv_data;
  676. free(priv);
  677. }