hw_deathadder_chroma.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855
  1. /*
  2. * Lowlevel hardware access for the Razer DeathAdder Chroma mouse.
  3. *
  4. * Important notice:
  5. * This hardware driver is based on reverse engineering, only.
  6. *
  7. * Copyright (C) 2015 Konrad Zemek <konrad.zemek@gmail.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify it under
  10. * the terms of the GNU General Public License as published by the Free Software
  11. * Foundation; either version 2 of the License, or (at your option) any later
  12. * version.
  13. *
  14. * This program is distributed in the hope that it will be useful, but WITHOUT
  15. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  16. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  17. * details.
  18. */
  19. #include "hw_deathadder_chroma.h"
  20. #include "razer_private.h"
  21. #include <errno.h>
  22. #include <stdlib.h>
  23. #include <stdio.h>
  24. #include <stdint.h>
  25. #include <string.h>
  26. static enum razer_mouse_freq deathadder_chroma_freqs_list[] = {
  27. RAZER_MOUSE_FREQ_125HZ, RAZER_MOUSE_FREQ_500HZ, RAZER_MOUSE_FREQ_1000HZ};
  28. static enum razer_mouse_res deathadder_chroma_resolution_stages_list[] = {
  29. RAZER_MOUSE_RES_800DPI, RAZER_MOUSE_RES_1800DPI, RAZER_MOUSE_RES_3500DPI,
  30. RAZER_MOUSE_RES_5600DPI, RAZER_MOUSE_RES_10000DPI};
  31. #define DEATHADDER_CHROMA_DEVICE_NAME "DeathAdder Chroma"
  32. #define DEATHADDER_CHROMA_SCROLL_NAME "Scrollwheel"
  33. #define DEATHADDER_CHROMA_LOGO_NAME "GlowingLogo"
  34. enum deathadder_chroma_led_id {
  35. DEATHADDER_CHROMA_LED_ID_SCROLL = 0x01,
  36. DEATHADDER_CHROMA_LED_ID_LOGO = 0x04
  37. };
  38. enum deathadder_chroma_led_mode {
  39. DEATHADDER_CHROMA_LED_MODE_STATIC = 0x00,
  40. DEATHADDER_CHROMA_LED_MODE_BREATHING = 0x02,
  41. DEATHADDER_CHROMA_LED_MODE_SPECTRUM = 0X04
  42. };
  43. enum deathadder_chroma_led_state {
  44. DEATHADDER_CHROMA_LED_STATE_OFF = 0x00,
  45. DEATHADDER_CHROMA_LED_STATE_ON = 0x01
  46. };
  47. /*
  48. * The 6th byte of DeathAdder Chroma's command seems to be the size of arguments
  49. * (size of arguments to read in case of read operations). It's not necessarily
  50. * so, since some values are slightly off (i.e. bigger than the apparent size
  51. * of the arguments).
  52. * Experiments suggest that the value given in the 'size' byte does not matter.
  53. * I chose to go with the values used by the Synapse driver.
  54. */
  55. enum deathadder_chroma_request_size {
  56. DEATHADDER_CHROMA_REQUEST_SIZE_INIT = 0x02,
  57. DEATHADDER_CHROMA_REQUEST_SIZE_SET_RESOLUTION = 0x07,
  58. DEATHADDER_CHROMA_REQUEST_SIZE_GET_FIRMWARE = 0x04,
  59. DEATHADDER_CHROMA_REQUEST_SIZE_GET_SERIAL_NO = 0x16,
  60. DEATHADDER_CHROMA_REQUEST_SIZE_SET_FREQUENCY = 0x01,
  61. DEATHADDER_CHROMA_REQUEST_SIZE_SET_LED_STATE = 0x03,
  62. DEATHADDER_CHROMA_REQUEST_SIZE_SET_LED_MODE = 0x03,
  63. DEATHADDER_CHROMA_REQUEST_SIZE_SET_LED_COLOR = 0x05
  64. };
  65. enum deathadder_chroma_request {
  66. DEATHADDER_CHROMA_REQUEST_INIT = 0x0004,
  67. DEATHADDER_CHROMA_REQUEST_SET_RESOLUTION = 0x0405,
  68. DEATHADDER_CHROMA_REQUEST_GET_FIRMWARE = 0x0087,
  69. DEATHADDER_CHROMA_REQUEST_GET_SERIAL_NO = 0x0082,
  70. DEATHADDER_CHROMA_REQUEST_SET_FREQUENCY = 0x0005,
  71. DEATHADDER_CHROMA_REQUEST_SET_LED_STATE = 0x0300,
  72. DEATHADDER_CHROMA_REQUEST_SET_LED_MODE = 0x0302,
  73. DEATHADDER_CHROMA_REQUEST_SET_LED_COLOR = 0x0301
  74. };
  75. enum deathadder_chroma_constants {
  76. DEATHADDER_CHROMA_MAX_FREQUENCY = RAZER_MOUSE_FREQ_1000HZ,
  77. DEATHADDER_CHROMA_MAX_RESOLUTION = RAZER_MOUSE_RES_10000DPI,
  78. DEATHADDER_CHROMA_RESOLUTION_STEP = RAZER_MOUSE_RES_100DPI,
  79. DEATHADDER_CHROMA_LED_NUM = 2,
  80. DEATHADDER_CHROMA_AXES_NUM = 2,
  81. DEATHADDER_CHROMA_SUPPORTED_FREQ_NUM =
  82. ARRAY_SIZE(deathadder_chroma_freqs_list),
  83. DEATHADDER_CHROMA_DPIMAPPINGS_NUM =
  84. ARRAY_SIZE(deathadder_chroma_resolution_stages_list),
  85. DEATHADDER_CHROMA_USB_SETUP_PACKET_VALUE = 0x300,
  86. DEATHADDER_CHROMA_SUCCESS_STATUS = 0x02,
  87. DEATHADDER_CHROMA_PACKET_SPACING_MS = 35,
  88. /*
  89. * Experiments suggest that the value in the 'magic' byte of the command
  90. * does not necessarily matter (e.g. the commands work when the 'magic'
  91. * byte equals 0x77). I chose to go with the value used by the Synapse
  92. * driver.
  93. */
  94. DEATHADDER_CHROMA_MAGIC_BYTE = 0xFF,
  95. /*
  96. * These specific arg0 bytes are used by the Synapse driver for their
  97. * respective commands. Their value may or may not matter (e.g. matters
  98. * in the case of LED commands, doesn't seem to matter for the
  99. * resolution command).
  100. */
  101. DEATHADDER_CHROMA_LED_ARG0 = 0x01,
  102. DEATHADDER_CHROMA_INIT_ARG0 = 0x03,
  103. DEATHADDER_CHROMA_RESOLUTION_ARG0 = 0x00
  104. };
  105. struct deathadder_chroma_command
  106. {
  107. uint8_t status;
  108. uint8_t magic;
  109. uint8_t padding0[3];
  110. uint8_t size;
  111. be16_t request;
  112. union
  113. {
  114. uint8_t bvalue[80];
  115. struct
  116. {
  117. uint8_t padding1;
  118. be16_t value[38];
  119. uint8_t padding2;
  120. } _packed;
  121. } _packed;
  122. uint8_t checksum;
  123. uint8_t padding3;
  124. } _packed;
  125. #define DEATHADDER_CHROMA_COMMAND_INIT \
  126. (struct deathadder_chroma_command) \
  127. { \
  128. .magic = DEATHADDER_CHROMA_MAGIC_BYTE \
  129. }
  130. struct deathadder_chroma_rgb_color
  131. {
  132. uint8_t r;
  133. uint8_t g;
  134. uint8_t b;
  135. };
  136. struct deathadder_chroma_led
  137. {
  138. enum deathadder_chroma_led_id id;
  139. enum deathadder_chroma_led_mode mode;
  140. enum deathadder_chroma_led_state state;
  141. struct deathadder_chroma_rgb_color color;
  142. };
  143. struct deathadder_chroma_driver_data
  144. {
  145. struct razer_event_spacing packet_spacing;
  146. struct razer_mouse_profile profile;
  147. struct razer_mouse_dpimapping *current_dpimapping;
  148. enum razer_mouse_freq current_freq;
  149. struct deathadder_chroma_led scroll_led;
  150. struct deathadder_chroma_led logo_led;
  151. struct razer_mouse_dpimapping
  152. dpimappings[DEATHADDER_CHROMA_DPIMAPPINGS_NUM];
  153. struct razer_axis axes[DEATHADDER_CHROMA_AXES_NUM];
  154. uint16_t fw_version;
  155. char serial[DEATHADDER_CHROMA_REQUEST_SIZE_GET_SERIAL_NO];
  156. };
  157. static uint8_t deathadder_chroma_checksum(struct deathadder_chroma_command *cmd)
  158. {
  159. size_t control_size;
  160. control_size = sizeof(cmd->size) + sizeof(cmd->request);
  161. return razer_xor8_checksum((uint8_t *)&cmd->size,
  162. control_size + cmd->size);
  163. }
  164. static int deathadder_chroma_translate_frequency(enum razer_mouse_freq freq)
  165. {
  166. switch (freq) {
  167. case RAZER_MOUSE_FREQ_UNKNOWN:
  168. freq = RAZER_MOUSE_FREQ_500HZ;
  169. case RAZER_MOUSE_FREQ_125HZ:
  170. case RAZER_MOUSE_FREQ_500HZ:
  171. case RAZER_MOUSE_FREQ_1000HZ:
  172. return DEATHADDER_CHROMA_MAX_FREQUENCY / freq;
  173. default:
  174. return -EINVAL;
  175. }
  176. }
  177. static int deathadder_chroma_usb_action(
  178. struct razer_mouse *m, enum libusb_endpoint_direction direction,
  179. enum libusb_standard_request request, uint16_t command,
  180. struct deathadder_chroma_command *cmd)
  181. {
  182. int err;
  183. struct deathadder_chroma_driver_data *drv_data;
  184. drv_data = m->drv_data;
  185. razer_event_spacing_enter(&drv_data->packet_spacing);
  186. err = libusb_control_transfer(m->usb_ctx->h,
  187. direction | LIBUSB_REQUEST_TYPE_CLASS |
  188. LIBUSB_RECIPIENT_INTERFACE,
  189. request, command, 0, (unsigned char *)cmd,
  190. sizeof(*cmd), RAZER_USB_TIMEOUT);
  191. razer_event_spacing_leave(&drv_data->packet_spacing);
  192. if (err != sizeof(*cmd)) {
  193. razer_error("razer-deathadder-chroma: "
  194. "USB %s 0x%01X 0x%02X failed with %d\n",
  195. direction == LIBUSB_ENDPOINT_IN ? "read" : "write",
  196. request, command, err);
  197. return err;
  198. }
  199. return 0;
  200. }
  201. static int deathadder_chroma_send_command(struct razer_mouse *m,
  202. struct deathadder_chroma_command *cmd)
  203. {
  204. int err;
  205. uint8_t checksum;
  206. cmd->checksum = deathadder_chroma_checksum(cmd);
  207. err = deathadder_chroma_usb_action(
  208. m, LIBUSB_ENDPOINT_OUT, LIBUSB_REQUEST_SET_CONFIGURATION,
  209. DEATHADDER_CHROMA_USB_SETUP_PACKET_VALUE, cmd);
  210. if (err)
  211. return err;
  212. err = deathadder_chroma_usb_action(
  213. m, LIBUSB_ENDPOINT_IN, LIBUSB_REQUEST_CLEAR_FEATURE,
  214. DEATHADDER_CHROMA_USB_SETUP_PACKET_VALUE, cmd);
  215. if (err)
  216. return err;
  217. checksum = deathadder_chroma_checksum(cmd);
  218. if (checksum != cmd->checksum) {
  219. razer_error("razer-deathadder-chroma: "
  220. "Command %02X %04X bad response checksum %02X "
  221. "(expected %02X)\n",
  222. cmd->size, be16_to_cpu(cmd->request), checksum,
  223. cmd->checksum);
  224. return -EBADMSG;
  225. }
  226. if (cmd->status != DEATHADDER_CHROMA_SUCCESS_STATUS)
  227. razer_error("razer-deathadder-chroma: "
  228. "Command %02X %04X failed with %02X\n",
  229. cmd->size, be16_to_cpu(cmd->request), cmd->status);
  230. return 0;
  231. }
  232. static int deathadder_chroma_send_init_command(struct razer_mouse *m)
  233. {
  234. struct deathadder_chroma_command cmd;
  235. cmd = DEATHADDER_CHROMA_COMMAND_INIT;
  236. cmd.size = DEATHADDER_CHROMA_REQUEST_SIZE_INIT;
  237. cmd.request = cpu_to_be16(DEATHADDER_CHROMA_REQUEST_INIT);
  238. cmd.bvalue[0] = DEATHADDER_CHROMA_INIT_ARG0;
  239. return deathadder_chroma_send_command(m, &cmd);
  240. }
  241. static int deathadder_chroma_send_set_resolution_command(struct razer_mouse *m)
  242. {
  243. enum razer_mouse_res res_x, res_y;
  244. struct deathadder_chroma_command cmd;
  245. struct deathadder_chroma_driver_data *drv_data;
  246. drv_data = m->drv_data;
  247. res_x = drv_data->current_dpimapping->res[RAZER_DIM_X];
  248. res_y = drv_data->current_dpimapping->res[RAZER_DIM_Y];
  249. cmd = DEATHADDER_CHROMA_COMMAND_INIT;
  250. cmd.size = DEATHADDER_CHROMA_REQUEST_SIZE_SET_RESOLUTION;
  251. cmd.request = cpu_to_be16(DEATHADDER_CHROMA_REQUEST_SET_RESOLUTION);
  252. cmd.bvalue[0] = DEATHADDER_CHROMA_RESOLUTION_ARG0;
  253. cmd.value[0] = cpu_to_be16(res_x);
  254. cmd.value[1] = cpu_to_be16(res_y);
  255. return deathadder_chroma_send_command(m, &cmd);
  256. }
  257. static int deathadder_chroma_send_get_firmware_command(struct razer_mouse *m)
  258. {
  259. int err;
  260. uint8_t fw_major;
  261. uint16_t fw_minor;
  262. struct deathadder_chroma_command cmd;
  263. struct deathadder_chroma_driver_data *drv_data;
  264. drv_data = m->drv_data;
  265. cmd = DEATHADDER_CHROMA_COMMAND_INIT;
  266. cmd.size = DEATHADDER_CHROMA_REQUEST_SIZE_GET_FIRMWARE;
  267. cmd.request = cpu_to_be16(DEATHADDER_CHROMA_REQUEST_GET_FIRMWARE);
  268. err = deathadder_chroma_send_command(m, &cmd);
  269. if (err)
  270. return err;
  271. fw_major = cmd.bvalue[0];
  272. fw_minor = be16_to_cpu(cmd.value[0]);
  273. drv_data->fw_version = (fw_major << 8) | fw_minor;
  274. return 0;
  275. }
  276. static int deathadder_chroma_send_get_serial_no_command(struct razer_mouse *m)
  277. {
  278. int err;
  279. struct deathadder_chroma_command cmd;
  280. struct deathadder_chroma_driver_data *drv_data;
  281. drv_data = m->drv_data;
  282. cmd = DEATHADDER_CHROMA_COMMAND_INIT;
  283. cmd.size = DEATHADDER_CHROMA_REQUEST_SIZE_GET_SERIAL_NO;
  284. cmd.request = cpu_to_be16(DEATHADDER_CHROMA_REQUEST_GET_SERIAL_NO);
  285. err = deathadder_chroma_send_command(m, &cmd);
  286. if (err)
  287. return err;
  288. strncpy(drv_data->serial, (const char *)cmd.bvalue,
  289. DEATHADDER_CHROMA_REQUEST_SIZE_GET_SERIAL_NO);
  290. return 0;
  291. }
  292. static int deathadder_chroma_send_set_frequency_command(struct razer_mouse *m)
  293. {
  294. int tfreq;
  295. struct deathadder_chroma_command cmd;
  296. struct deathadder_chroma_driver_data *drv_data;
  297. drv_data = m->drv_data;
  298. cmd = DEATHADDER_CHROMA_COMMAND_INIT;
  299. tfreq = deathadder_chroma_translate_frequency(drv_data->current_freq);
  300. if (tfreq < 0)
  301. return tfreq;
  302. cmd.size = DEATHADDER_CHROMA_REQUEST_SIZE_SET_FREQUENCY;
  303. cmd.request = cpu_to_be16(DEATHADDER_CHROMA_REQUEST_SET_FREQUENCY);
  304. cmd.bvalue[0] = tfreq;
  305. return deathadder_chroma_send_command(m, &cmd);
  306. }
  307. static struct deathadder_chroma_led *
  308. deathadder_chroma_get_led(struct deathadder_chroma_driver_data *d,
  309. enum deathadder_chroma_led_id led_id)
  310. {
  311. switch (led_id) {
  312. case DEATHADDER_CHROMA_LED_ID_LOGO:
  313. return &d->logo_led;
  314. case DEATHADDER_CHROMA_LED_ID_SCROLL:
  315. return &d->scroll_led;
  316. default:
  317. return NULL;
  318. }
  319. }
  320. static int
  321. deathadder_chroma_send_set_led_state_command(struct razer_mouse *m,
  322. struct deathadder_chroma_led *led)
  323. {
  324. struct deathadder_chroma_command cmd;
  325. cmd = DEATHADDER_CHROMA_COMMAND_INIT;
  326. cmd.size = DEATHADDER_CHROMA_REQUEST_SIZE_SET_LED_STATE;
  327. cmd.request = cpu_to_be16(DEATHADDER_CHROMA_REQUEST_SET_LED_STATE);
  328. cmd.bvalue[0] = DEATHADDER_CHROMA_LED_ARG0;
  329. cmd.bvalue[1] = led->id;
  330. cmd.bvalue[2] = led->state;
  331. return deathadder_chroma_send_command(m, &cmd);
  332. }
  333. static int
  334. deathadder_chroma_send_set_led_mode_command(struct razer_mouse *m,
  335. struct deathadder_chroma_led *led)
  336. {
  337. struct deathadder_chroma_command cmd;
  338. cmd = DEATHADDER_CHROMA_COMMAND_INIT;
  339. cmd.size = DEATHADDER_CHROMA_REQUEST_SIZE_SET_LED_MODE;
  340. cmd.request = cpu_to_be16(DEATHADDER_CHROMA_REQUEST_SET_LED_MODE);
  341. cmd.bvalue[0] = DEATHADDER_CHROMA_LED_ARG0;
  342. cmd.bvalue[1] = led->id;
  343. cmd.bvalue[2] = led->mode;
  344. return deathadder_chroma_send_command(m, &cmd);
  345. }
  346. static int
  347. deathadder_chroma_send_set_led_color_command(struct razer_mouse *m,
  348. struct deathadder_chroma_led *led)
  349. {
  350. struct deathadder_chroma_command cmd;
  351. cmd = DEATHADDER_CHROMA_COMMAND_INIT;
  352. cmd.size = DEATHADDER_CHROMA_REQUEST_SIZE_SET_LED_COLOR;
  353. cmd.request = cpu_to_be16(DEATHADDER_CHROMA_REQUEST_SET_LED_COLOR);
  354. cmd.bvalue[0] = DEATHADDER_CHROMA_LED_ARG0;
  355. cmd.bvalue[1] = led->id;
  356. cmd.bvalue[2] = led->color.r;
  357. cmd.bvalue[3] = led->color.g;
  358. cmd.bvalue[4] = led->color.b;
  359. return deathadder_chroma_send_command(m, &cmd);
  360. }
  361. static int deathadder_chroma_get_fw_version(struct razer_mouse *m)
  362. {
  363. struct deathadder_chroma_driver_data *drv_data;
  364. drv_data = m->drv_data;
  365. return drv_data->fw_version;
  366. }
  367. static struct razer_mouse_profile *
  368. deathadder_chroma_get_profiles(struct razer_mouse *m)
  369. {
  370. struct deathadder_chroma_driver_data *drv_data;
  371. drv_data = m->drv_data;
  372. return &drv_data->profile;
  373. }
  374. static int deathadder_chroma_supported_axes(struct razer_mouse *m,
  375. struct razer_axis **res_ptr)
  376. {
  377. struct deathadder_chroma_driver_data *drv_data;
  378. drv_data = m->drv_data;
  379. *res_ptr = drv_data->axes;
  380. return ARRAY_SIZE(drv_data->axes);
  381. }
  382. static int
  383. deathadder_chroma_supported_dpimappings(struct razer_mouse *m,
  384. struct razer_mouse_dpimapping **res_ptr)
  385. {
  386. struct deathadder_chroma_driver_data *drv_data;
  387. drv_data = m->drv_data;
  388. *res_ptr = drv_data->dpimappings;
  389. return ARRAY_SIZE(drv_data->dpimappings);
  390. }
  391. static int
  392. deathadder_chroma_supported_resolutions(struct razer_mouse *m,
  393. enum razer_mouse_res **res_ptr)
  394. {
  395. size_t i;
  396. size_t step_number;
  397. step_number = DEATHADDER_CHROMA_MAX_RESOLUTION /
  398. DEATHADDER_CHROMA_RESOLUTION_STEP;
  399. *res_ptr = calloc(step_number, sizeof(enum razer_mouse_res));
  400. if (!*res_ptr)
  401. return -ENOMEM;
  402. for (i = 0; i < step_number; ++i)
  403. (*res_ptr)[i] = (i + 1) * DEATHADDER_CHROMA_RESOLUTION_STEP;
  404. return step_number;
  405. }
  406. static int deathadder_chroma_supported_freqs(struct razer_mouse *m,
  407. enum razer_mouse_freq **res_ptr)
  408. {
  409. *res_ptr = malloc(sizeof(deathadder_chroma_freqs_list));
  410. if (!*res_ptr)
  411. return -ENOMEM;
  412. memcpy(*res_ptr, deathadder_chroma_freqs_list,
  413. sizeof(deathadder_chroma_freqs_list));
  414. return DEATHADDER_CHROMA_SUPPORTED_FREQ_NUM;
  415. }
  416. static enum razer_mouse_freq
  417. deathadder_chroma_get_freq(struct razer_mouse_profile *p)
  418. {
  419. struct deathadder_chroma_driver_data *drv_data;
  420. drv_data = p->mouse->drv_data;
  421. return drv_data->current_freq;
  422. }
  423. static struct razer_mouse_dpimapping *
  424. deathadder_chroma_get_dpimapping(struct razer_mouse_profile *p,
  425. struct razer_axis *axis)
  426. {
  427. struct deathadder_chroma_driver_data *drv_data;
  428. drv_data = p->mouse->drv_data;
  429. return drv_data->current_dpimapping;
  430. }
  431. static int deathadder_chroma_change_dpimapping(struct razer_mouse_dpimapping *d,
  432. enum razer_dimension dim,
  433. enum razer_mouse_res res)
  434. {
  435. struct deathadder_chroma_driver_data *drv_data;
  436. if (!(d->dimension_mask & (1 << dim)))
  437. return -EINVAL;
  438. if (res == RAZER_MOUSE_RES_UNKNOWN)
  439. res = RAZER_MOUSE_RES_1800DPI;
  440. if (res < RAZER_MOUSE_RES_100DPI || res > RAZER_MOUSE_RES_10000DPI)
  441. return -EINVAL;
  442. d->res[dim] = res;
  443. drv_data = d->mouse->drv_data;
  444. if (d == drv_data->current_dpimapping)
  445. return deathadder_chroma_send_set_resolution_command(d->mouse);
  446. return 0;
  447. }
  448. static int deathadder_chroma_led_toggle_state(struct razer_led *led,
  449. enum razer_led_state new_state)
  450. {
  451. struct deathadder_chroma_driver_data *drv_data;
  452. struct deathadder_chroma_led *priv_led;
  453. drv_data = led->u.mouse->drv_data;
  454. priv_led = deathadder_chroma_get_led(drv_data, led->id);
  455. if (!priv_led)
  456. return -EINVAL;
  457. switch (new_state) {
  458. case RAZER_LED_UNKNOWN:
  459. case RAZER_LED_ON:
  460. priv_led->state = DEATHADDER_CHROMA_LED_STATE_ON;
  461. break;
  462. case RAZER_LED_OFF:
  463. priv_led->state = DEATHADDER_CHROMA_LED_STATE_OFF;
  464. break;
  465. }
  466. return deathadder_chroma_send_set_led_state_command(led->u.mouse,
  467. priv_led);
  468. }
  469. static int
  470. deathadder_chroma_led_change_color(struct razer_led *led,
  471. const struct razer_rgb_color *new_color)
  472. {
  473. struct deathadder_chroma_driver_data *drv_data;
  474. struct deathadder_chroma_led *priv_led;
  475. drv_data = led->u.mouse->drv_data;
  476. priv_led = deathadder_chroma_get_led(drv_data, led->id);
  477. if (!priv_led)
  478. return -EINVAL;
  479. if (priv_led->mode == DEATHADDER_CHROMA_LED_MODE_SPECTRUM)
  480. return -EINVAL;
  481. priv_led->color = (struct deathadder_chroma_rgb_color){
  482. .r = new_color->r, .g = new_color->g, .b = new_color->b};
  483. return deathadder_chroma_send_set_led_color_command(led->u.mouse,
  484. priv_led);
  485. }
  486. static int deathadder_chroma_set_freq(struct razer_mouse_profile *p,
  487. enum razer_mouse_freq freq)
  488. {
  489. struct deathadder_chroma_driver_data *drv_data;
  490. if (freq == RAZER_MOUSE_FREQ_UNKNOWN)
  491. freq = RAZER_MOUSE_FREQ_500HZ;
  492. if (freq != RAZER_MOUSE_FREQ_125HZ && freq != RAZER_MOUSE_FREQ_500HZ &&
  493. freq != RAZER_MOUSE_FREQ_1000HZ)
  494. return -EINVAL;
  495. drv_data = p->mouse->drv_data;
  496. drv_data->current_freq = freq;
  497. return deathadder_chroma_send_set_frequency_command(p->mouse);
  498. }
  499. static int deathadder_chroma_set_dpimapping(struct razer_mouse_profile *p,
  500. struct razer_axis *axis,
  501. struct razer_mouse_dpimapping *d)
  502. {
  503. struct deathadder_chroma_driver_data *drv_data;
  504. if (axis && axis->id > 0)
  505. return -EINVAL;
  506. drv_data = p->mouse->drv_data;
  507. drv_data->current_dpimapping = &drv_data->dpimappings[d->nr];
  508. return deathadder_chroma_send_set_resolution_command(p->mouse);
  509. }
  510. static int
  511. deathadder_chroma_translate_led_mode(enum deathadder_chroma_led_mode mode)
  512. {
  513. switch (mode) {
  514. case DEATHADDER_CHROMA_LED_MODE_STATIC:
  515. return RAZER_LED_MODE_STATIC;
  516. case DEATHADDER_CHROMA_LED_MODE_BREATHING:
  517. return RAZER_LED_MODE_BREATHING;
  518. case DEATHADDER_CHROMA_LED_MODE_SPECTRUM:
  519. return RAZER_LED_MODE_SPECTRUM;
  520. default:
  521. return -EINVAL;
  522. }
  523. }
  524. static int deathadder_chroma_translate_razer_led_mode(enum razer_led_mode mode)
  525. {
  526. switch (mode) {
  527. case RAZER_LED_MODE_STATIC:
  528. return DEATHADDER_CHROMA_LED_MODE_STATIC;
  529. case RAZER_LED_MODE_BREATHING:
  530. return DEATHADDER_CHROMA_LED_MODE_BREATHING;
  531. case RAZER_LED_MODE_SPECTRUM:
  532. return DEATHADDER_CHROMA_LED_MODE_SPECTRUM;
  533. default:
  534. return -EINVAL;
  535. }
  536. }
  537. static int deathadder_chroma_led_set_mode(struct razer_led *led,
  538. enum razer_led_mode new_mode)
  539. {
  540. int err;
  541. struct deathadder_chroma_driver_data *drv_data;
  542. struct deathadder_chroma_led *priv_led;
  543. drv_data = led->u.mouse->drv_data;
  544. priv_led = deathadder_chroma_get_led(drv_data, led->id);
  545. if (!priv_led)
  546. return -EINVAL;
  547. err = deathadder_chroma_translate_razer_led_mode(new_mode);
  548. if (err < 0)
  549. return err;
  550. priv_led->mode = err;
  551. return deathadder_chroma_send_set_led_mode_command(led->u.mouse,
  552. priv_led);
  553. }
  554. static int deathadder_chroma_get_leds(struct razer_mouse *m,
  555. struct razer_led **leds_list)
  556. {
  557. unsigned int supported_modes;
  558. enum razer_led_state scroll_state, logo_state;
  559. struct deathadder_chroma_driver_data *drv_data;
  560. struct razer_led *scroll, *logo;
  561. drv_data = m->drv_data;
  562. scroll = zalloc(sizeof(struct razer_led));
  563. if (!scroll)
  564. return -ENOMEM;
  565. logo = zalloc(sizeof(struct razer_led));
  566. if (!logo) {
  567. free(scroll);
  568. return -ENOMEM;
  569. }
  570. supported_modes = (1 << RAZER_LED_MODE_BREATHING) |
  571. (1 << RAZER_LED_MODE_SPECTRUM) |
  572. (1 << RAZER_LED_MODE_STATIC);
  573. scroll_state =
  574. drv_data->scroll_led.state == DEATHADDER_CHROMA_LED_STATE_OFF
  575. ? RAZER_LED_OFF
  576. : RAZER_LED_ON;
  577. *scroll = (struct razer_led){
  578. .id = drv_data->scroll_led.id,
  579. .name = DEATHADDER_CHROMA_SCROLL_NAME,
  580. .state = scroll_state,
  581. .u.mouse = m,
  582. .toggle_state = deathadder_chroma_led_toggle_state,
  583. .change_color = deathadder_chroma_led_change_color,
  584. .set_mode = deathadder_chroma_led_set_mode,
  585. .next = logo,
  586. .color = {.r = drv_data->scroll_led.color.r,
  587. .g = drv_data->scroll_led.color.g,
  588. .b = drv_data->scroll_led.color.b,
  589. .valid = 1},
  590. .supported_modes_mask = supported_modes,
  591. .mode = deathadder_chroma_translate_led_mode(
  592. drv_data->scroll_led.mode)};
  593. logo_state = drv_data->logo_led.state == DEATHADDER_CHROMA_LED_STATE_OFF
  594. ? RAZER_LED_OFF
  595. : RAZER_LED_ON;
  596. *logo = (struct razer_led){
  597. .id = drv_data->logo_led.id,
  598. .name = DEATHADDER_CHROMA_LOGO_NAME,
  599. .state = logo_state,
  600. .u.mouse = m,
  601. .toggle_state = deathadder_chroma_led_toggle_state,
  602. .change_color = deathadder_chroma_led_change_color,
  603. .set_mode = deathadder_chroma_led_set_mode,
  604. .color = {.r = drv_data->logo_led.color.r,
  605. .g = drv_data->logo_led.color.g,
  606. .b = drv_data->logo_led.color.b,
  607. .valid = 1},
  608. .supported_modes_mask = supported_modes,
  609. .mode =
  610. deathadder_chroma_translate_led_mode(drv_data->logo_led.mode)};
  611. *leds_list = scroll;
  612. return DEATHADDER_CHROMA_LED_NUM;
  613. }
  614. int razer_deathadder_chroma_init(struct razer_mouse *m,
  615. struct libusb_device *usbdev)
  616. {
  617. int err;
  618. size_t i;
  619. struct deathadder_chroma_driver_data *drv_data;
  620. struct deathadder_chroma_led *scroll_led, *logo_led;
  621. BUILD_BUG_ON(sizeof(struct deathadder_chroma_command) != 90);
  622. drv_data = zalloc(sizeof(*drv_data));
  623. if (!drv_data)
  624. return -ENOMEM;
  625. razer_event_spacing_init(&drv_data->packet_spacing,
  626. DEATHADDER_CHROMA_PACKET_SPACING_MS);
  627. for (i = 0; i < DEATHADDER_CHROMA_DPIMAPPINGS_NUM; ++i) {
  628. drv_data->dpimappings[i] = (struct razer_mouse_dpimapping){
  629. .nr = i,
  630. .change = deathadder_chroma_change_dpimapping,
  631. .dimension_mask = (1 << RAZER_DIM_X) | (1 << RAZER_DIM_Y),
  632. .mouse = m};
  633. drv_data->dpimappings[i].res[RAZER_DIM_X] =
  634. drv_data->dpimappings[i].res[RAZER_DIM_Y] =
  635. deathadder_chroma_resolution_stages_list[i];
  636. }
  637. drv_data->current_dpimapping = &drv_data->dpimappings[1];
  638. drv_data->current_freq = RAZER_MOUSE_FREQ_500HZ;
  639. drv_data->scroll_led = (struct deathadder_chroma_led){
  640. .id = DEATHADDER_CHROMA_LED_ID_SCROLL,
  641. .mode = DEATHADDER_CHROMA_LED_MODE_SPECTRUM,
  642. .state = DEATHADDER_CHROMA_LED_STATE_ON,
  643. .color = {0x00, 0xFF, 0x00}};
  644. drv_data->logo_led = (struct deathadder_chroma_led){
  645. .id = DEATHADDER_CHROMA_LED_ID_LOGO,
  646. .mode = DEATHADDER_CHROMA_LED_MODE_SPECTRUM,
  647. .state = DEATHADDER_CHROMA_LED_STATE_ON,
  648. .color = {0x00, 0xFF, 0x00}};
  649. razer_init_axes(drv_data->axes, "X/Y",
  650. RAZER_AXIS_INDEPENDENT_DPIMAPPING, "Scroll", 0, NULL,
  651. 0);
  652. m->drv_data = drv_data;
  653. if ((err = razer_usb_add_used_interface(m->usb_ctx, 0, 0)) ||
  654. (err = m->claim(m))) {
  655. free(drv_data);
  656. return err;
  657. }
  658. scroll_led = &drv_data->scroll_led;
  659. logo_led = &drv_data->logo_led;
  660. if ((err = deathadder_chroma_send_init_command(m)) ||
  661. (err = deathadder_chroma_send_set_resolution_command(m)) ||
  662. (err = deathadder_chroma_send_get_firmware_command(m)) ||
  663. (err = deathadder_chroma_send_get_serial_no_command(m)) ||
  664. (err = deathadder_chroma_send_set_frequency_command(m)) ||
  665. (err =
  666. deathadder_chroma_send_set_led_state_command(m, scroll_led)) ||
  667. (err =
  668. deathadder_chroma_send_set_led_mode_command(m, scroll_led)) ||
  669. (err =
  670. deathadder_chroma_send_set_led_color_command(m, scroll_led)) ||
  671. (err = deathadder_chroma_send_set_led_state_command(m, logo_led)) ||
  672. (err = deathadder_chroma_send_set_led_mode_command(m, logo_led)) ||
  673. (err = deathadder_chroma_send_set_led_color_command(m, logo_led))) {
  674. m->release(m);
  675. free(drv_data);
  676. return err;
  677. }
  678. m->release(m);
  679. drv_data->profile = (struct razer_mouse_profile){
  680. .mouse = m,
  681. .get_freq = deathadder_chroma_get_freq,
  682. .set_freq = deathadder_chroma_set_freq,
  683. .get_dpimapping = deathadder_chroma_get_dpimapping,
  684. .set_dpimapping = deathadder_chroma_set_dpimapping};
  685. razer_generic_usb_gen_idstr(usbdev, m->usb_ctx->h,
  686. DEATHADDER_CHROMA_DEVICE_NAME, false,
  687. drv_data->serial, m->idstr);
  688. m->type = RAZER_MOUSETYPE_DEATHADDER;
  689. m->get_fw_version = deathadder_chroma_get_fw_version;
  690. m->global_get_leds = deathadder_chroma_get_leds;
  691. m->get_profiles = deathadder_chroma_get_profiles;
  692. m->supported_axes = deathadder_chroma_supported_axes;
  693. m->supported_resolutions = deathadder_chroma_supported_resolutions;
  694. m->supported_freqs = deathadder_chroma_supported_freqs;
  695. m->supported_dpimappings = deathadder_chroma_supported_dpimappings;
  696. return 0;
  697. }
  698. void razer_deathadder_chroma_release(struct razer_mouse *m)
  699. {
  700. struct deathadder_chroma_driver_data *drv_data;
  701. drv_data = m->drv_data;
  702. free(drv_data);
  703. m->drv_data = NULL;
  704. }