hw_lachesis.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846
  1. /*
  2. * Lowlevel hardware access for the
  3. * Razer Lachesis mouse
  4. *
  5. * Important notice:
  6. * This hardware driver is based on reverse engineering, only.
  7. *
  8. * Copyright (C) 2008-2011 Michael Buesch <m@bues.ch>
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License
  12. * as published by the Free Software Foundation; either version 2
  13. * of the License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. */
  20. #include "hw_lachesis.h"
  21. #include "razer_private.h"
  22. #include "buttonmapping.h"
  23. #include <errno.h>
  24. #include <stdlib.h>
  25. #include <stdio.h>
  26. #include <stdint.h>
  27. #include <string.h>
  28. enum { /* LED IDs */
  29. LACHESIS_LED_SCROLL = 0,
  30. LACHESIS_LED_LOGO,
  31. LACHESIS_NR_LEDS,
  32. };
  33. enum { /* Misc constants */
  34. LACHESIS_NR_PROFILES = 5,
  35. LACHESIS_NR_DPIMAPPINGS = 5,
  36. LACHESIS_NR_AXES = 3,
  37. LACHESIS_SERIAL_MAX_LEN = 32,
  38. LACHESIS_PROFNAME_MAX_LEN = 20,
  39. };
  40. /* The wire protocol data structures... */
  41. enum lachesis_phys_button {
  42. /* Physical button IDs */
  43. LACHESIS_PHYSBUT_LEFT = 0x01, /* Left button */
  44. LACHESIS_PHYSBUT_RIGHT, /* Right button */
  45. LACHESIS_PHYSBUT_MIDDLE, /* Middle button */
  46. LACHESIS_PHYSBUT_LFRONT, /* Left side, front button */
  47. LACHESIS_PHYSBUT_LREAR, /* Left side, rear button */
  48. LACHESIS_PHYSBUT_RFRONT, /* Right side, front button */
  49. LACHESIS_PHYSBUT_RREAR, /* Right side, rear button */
  50. LACHESIS_PHYSBUT_TFRONT, /* Top side, front button */
  51. LACHESIS_PHYSBUT_TREAR, /* Top side, rear button */
  52. LACHESIS_PHYSBUT_SCROLLUP, /* Scroll wheel up */
  53. LACHESIS_PHYSBUT_SCROLLDWN, /* Scroll wheel down */
  54. NR_LACHESIS_PHYSBUT = 11, /* Number of physical buttons */
  55. };
  56. struct lachesis_profcfg_cmd {
  57. le16_t packetlength;
  58. le16_t magic;
  59. uint8_t profile;
  60. uint8_t _padding0;
  61. uint8_t dpisel;
  62. uint8_t freq;
  63. uint8_t _padding1;
  64. uint8_t buttonmap[35 * NR_LACHESIS_PHYSBUT];
  65. le16_t checksum;
  66. } _packed;
  67. #define LACHESIS_PROFCFG_MAGIC cpu_to_le16(0x0002)
  68. struct lachesis_one_dpimapping {
  69. uint8_t magic;
  70. uint8_t dpival0;
  71. uint8_t dpival1;
  72. } _packed;
  73. #define LACHESIS_DPIMAPPING_MAGIC 0x01
  74. struct lachesis_dpimap_cmd {
  75. struct lachesis_one_dpimapping mappings[5];
  76. uint8_t _padding[81];
  77. } _packed;
  78. struct lachesis_buttons {
  79. struct razer_buttonmapping mapping[NR_LACHESIS_PHYSBUT];
  80. };
  81. /* Context data structure */
  82. struct lachesis_private {
  83. struct razer_mouse *m;
  84. uint16_t fw_version;
  85. /* The currently set LED states. */
  86. enum razer_led_state led_states[LACHESIS_NR_LEDS];
  87. /* The active profile. */
  88. struct razer_mouse_profile *cur_profile;
  89. /* Profile configuration (one per profile). */
  90. struct razer_mouse_profile profiles[LACHESIS_NR_PROFILES];
  91. /* Supported mouse axes */
  92. struct razer_axis axes[LACHESIS_NR_AXES];
  93. /* The active DPI mapping; per profile. */
  94. struct razer_mouse_dpimapping *cur_dpimapping[LACHESIS_NR_PROFILES];
  95. /* The possible DPI mappings. */
  96. struct razer_mouse_dpimapping dpimappings[LACHESIS_NR_DPIMAPPINGS];
  97. /* The active scan frequency. */
  98. enum razer_mouse_freq cur_freq[LACHESIS_NR_PROFILES];
  99. /* The active button mapping; per profile. */
  100. struct lachesis_buttons buttons[LACHESIS_NR_PROFILES];
  101. bool commit_pending;
  102. };
  103. /* A list of physical buttons on the device. */
  104. static struct razer_button lachesis_physical_buttons[] = {
  105. { .id = LACHESIS_PHYSBUT_LEFT, .name = "Leftclick", },
  106. { .id = LACHESIS_PHYSBUT_RIGHT, .name = "Rightclick", },
  107. { .id = LACHESIS_PHYSBUT_MIDDLE, .name = "Middleclick", },
  108. { .id = LACHESIS_PHYSBUT_LFRONT, .name = "Leftside front", },
  109. { .id = LACHESIS_PHYSBUT_LREAR, .name = "Leftside rear", },
  110. { .id = LACHESIS_PHYSBUT_RFRONT, .name = "Rightside front", },
  111. { .id = LACHESIS_PHYSBUT_RREAR, .name = "Rightside rear", },
  112. { .id = LACHESIS_PHYSBUT_TFRONT, .name = "Top front", },
  113. { .id = LACHESIS_PHYSBUT_TREAR, .name = "Top rear", },
  114. { .id = LACHESIS_PHYSBUT_SCROLLUP, .name = "Scroll up", },
  115. { .id = LACHESIS_PHYSBUT_SCROLLDWN, .name = "Scroll down", },
  116. };
  117. /* A list of possible button functions. */
  118. static struct razer_button_function lachesis_button_functions[] = {
  119. BUTTONFUNC_LEFT,
  120. BUTTONFUNC_RIGHT,
  121. BUTTONFUNC_MIDDLE,
  122. BUTTONFUNC_PROFDOWN,
  123. BUTTONFUNC_PROFUP,
  124. BUTTONFUNC_DPIUP,
  125. BUTTONFUNC_DPIDOWN,
  126. BUTTONFUNC_DPI1,
  127. BUTTONFUNC_DPI2,
  128. BUTTONFUNC_DPI3,
  129. BUTTONFUNC_DPI4,
  130. BUTTONFUNC_DPI5,
  131. BUTTONFUNC_WIN5,
  132. BUTTONFUNC_WIN4,
  133. BUTTONFUNC_SCROLLUP,
  134. BUTTONFUNC_SCROLLDWN,
  135. };
  136. /*XXX: lachesis-CLASSIC notes
  137. *
  138. * read commands:
  139. *
  140. * CLEAR_FEATURE
  141. * 0x10: Read DPI map
  142. * 0x06: read fw version
  143. * 0x05: LEDs
  144. * 0x09: cur profile
  145. * 0x03: profile config
  146. * 0x02: ?busy flag?
  147. *
  148. * write commands:
  149. * SET_CONFIG
  150. * 0x0F: ? Data=0x01. Executed on startup
  151. *
  152. * TODO: react to profile/dpi/whatever changes via hw buttons. need to poll?
  153. */
  154. static int lachesis_usb_write(struct lachesis_private *priv,
  155. int request, int command, int index,
  156. void *buf, size_t size)
  157. {
  158. int err;
  159. err = libusb_control_transfer(
  160. priv->m->usb_ctx->h,
  161. LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_CLASS |
  162. LIBUSB_RECIPIENT_INTERFACE,
  163. request, command, index,
  164. buf, size,
  165. RAZER_USB_TIMEOUT);
  166. if (err < 0 || (size_t)err != size) {
  167. razer_error("hw_lachesis: usb_write failed\n");
  168. return -EIO;
  169. }
  170. razer_msleep(5);
  171. return 0;
  172. }
  173. static int lachesis_usb_read(struct lachesis_private *priv,
  174. int request, int command, int index,
  175. void *buf, size_t size)
  176. {
  177. int err;
  178. err = libusb_control_transfer(
  179. priv->m->usb_ctx->h,
  180. LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_CLASS |
  181. LIBUSB_RECIPIENT_INTERFACE,
  182. request, command, index,
  183. buf, size,
  184. RAZER_USB_TIMEOUT);
  185. if (err < 0 || (size_t)err != size) {
  186. razer_error("hw_lachesis: usb_read failed\n");
  187. return -EIO;
  188. }
  189. razer_msleep(5);
  190. return 0;
  191. }
  192. static int lachesis_read_devinfo(struct lachesis_private *priv)
  193. {
  194. uint8_t buf[2];
  195. int err;
  196. err = lachesis_usb_read(priv, LIBUSB_REQUEST_CLEAR_FEATURE,
  197. 0x06, 0, buf, sizeof(buf));
  198. if (err)
  199. return -EIO;
  200. priv->fw_version = ((uint16_t)(buf[0]) << 8) | buf[1];
  201. return 0;
  202. }
  203. static int lachesis_do_commit(struct lachesis_private *priv)
  204. {
  205. unsigned int i;
  206. int err;
  207. char value, status;
  208. struct lachesis_profcfg_cmd profcfg;
  209. struct lachesis_dpimap_cmd dpimap;
  210. /* Commit the profile configuration. */
  211. for (i = 0; i < LACHESIS_NR_PROFILES; i++) {
  212. memset(&profcfg, 0, sizeof(profcfg));
  213. profcfg.packetlength = cpu_to_le16(sizeof(profcfg));
  214. profcfg.magic = LACHESIS_PROFCFG_MAGIC;
  215. profcfg.profile = i + 1;
  216. profcfg.dpisel = (priv->cur_dpimapping[i]->nr % 10) + 1;
  217. switch (priv->cur_freq[i]) {
  218. default:
  219. case RAZER_MOUSE_FREQ_1000HZ:
  220. profcfg.freq = 1;
  221. break;
  222. case RAZER_MOUSE_FREQ_500HZ:
  223. profcfg.freq = 2;
  224. break;
  225. case RAZER_MOUSE_FREQ_125HZ:
  226. profcfg.freq = 3;
  227. break;
  228. }
  229. err = razer_create_buttonmap(profcfg.buttonmap, sizeof(profcfg.buttonmap),
  230. priv->buttons[i].mapping,
  231. ARRAY_SIZE(priv->buttons[i].mapping), 33);
  232. if (err)
  233. return err;
  234. profcfg.checksum = razer_xor16_checksum(&profcfg,
  235. sizeof(profcfg) - sizeof(profcfg.checksum));
  236. err = lachesis_usb_write(priv, LIBUSB_REQUEST_SET_CONFIGURATION,
  237. 0x01, 0, &profcfg, sizeof(profcfg));
  238. if (err)
  239. return err;
  240. err = lachesis_usb_read(priv, LIBUSB_REQUEST_CLEAR_FEATURE,
  241. 0x02, 0, &status, sizeof(status));
  242. if (err || status != 1) {
  243. razer_error("hw_lachesis: Failed to commit profile\n");
  244. return err;
  245. }
  246. }
  247. /* Commit LED states. */
  248. value = 0;
  249. if (priv->led_states[LACHESIS_LED_LOGO])
  250. value |= 0x01;
  251. if (priv->led_states[LACHESIS_LED_SCROLL])
  252. value |= 0x02;
  253. err = lachesis_usb_write(priv, LIBUSB_REQUEST_SET_CONFIGURATION,
  254. 0x04, 0, &value, sizeof(value));
  255. if (err)
  256. return err;
  257. /* Commit the active profile selection. */
  258. value = priv->cur_profile->nr + 1;
  259. err = lachesis_usb_write(priv, LIBUSB_REQUEST_SET_CONFIGURATION,
  260. 0x08, 0, &value, sizeof(value));
  261. if (err)
  262. return err;
  263. /* Commit the DPI map. */
  264. memset(&dpimap, 0, sizeof(dpimap));
  265. for (i = 0; i < LACHESIS_NR_DPIMAPPINGS; i++) {
  266. dpimap.mappings[i].magic = LACHESIS_DPIMAPPING_MAGIC;
  267. dpimap.mappings[i].dpival0 = (priv->dpimappings[i].res[RAZER_DIM_0] / 125) - 1;
  268. dpimap.mappings[i].dpival1 = dpimap.mappings[i].dpival0;
  269. }
  270. err = lachesis_usb_write(priv, LIBUSB_REQUEST_SET_CONFIGURATION,
  271. 0x12, 0, &dpimap, sizeof(dpimap));
  272. if (err)
  273. return err;
  274. return 0;
  275. }
  276. static int lachesis_read_config_from_hw(struct lachesis_private *priv)
  277. {
  278. int err;
  279. unsigned char value;
  280. unsigned int i;
  281. struct lachesis_dpimap_cmd dpimap;
  282. struct lachesis_profcfg_cmd profcfg;
  283. value = 0x01;
  284. err = lachesis_usb_write(priv, LIBUSB_REQUEST_SET_CONFIGURATION,
  285. 0x0F, 0, &value, sizeof(value));
  286. if (err)
  287. return err;
  288. /* Get the current profile number */
  289. err = lachesis_usb_read(priv, LIBUSB_REQUEST_CLEAR_FEATURE,
  290. 0x09, 0, &value, sizeof(value));
  291. if (err)
  292. return err;
  293. if (value < 1 || value > LACHESIS_NR_PROFILES) {
  294. razer_error("hw_lachesis: Got invalid profile number\n");
  295. return -EIO;
  296. }
  297. priv->cur_profile = &priv->profiles[value - 1];
  298. /* Get the profile configuration */
  299. for (i = 0; i < LACHESIS_NR_PROFILES; i++) {
  300. /* Change to the profile */
  301. value = i + 1;
  302. err = lachesis_usb_write(priv, LIBUSB_REQUEST_SET_CONFIGURATION,
  303. 0x08, 0, &value, sizeof(value));
  304. if (err)
  305. return err;
  306. /* And read the profile config */
  307. err = lachesis_usb_read(priv, LIBUSB_REQUEST_CLEAR_FEATURE,
  308. 0x03, 1, &profcfg, sizeof(profcfg));
  309. if (err)
  310. return err;
  311. if (profcfg.dpisel < 1 || profcfg.dpisel > LACHESIS_NR_DPIMAPPINGS) {
  312. razer_error("hw_lachesis: Got invalid DPI selection\n");
  313. return -EIO;
  314. }
  315. razer_debug("hw_lachesis: Got profile config %d "
  316. "(magic 0x%04X, prof %u, freq %u, dpisel %u)\n",
  317. i + 1, profcfg.magic, profcfg.profile, profcfg.freq, profcfg.dpisel);
  318. priv->cur_dpimapping[i] = &priv->dpimappings[profcfg.dpisel - 1];
  319. switch (profcfg.freq) {
  320. case 1:
  321. priv->cur_freq[i] = RAZER_MOUSE_FREQ_1000HZ;
  322. break;
  323. case 2:
  324. priv->cur_freq[i] = RAZER_MOUSE_FREQ_500HZ;
  325. break;
  326. case 3:
  327. priv->cur_freq[i] = RAZER_MOUSE_FREQ_125HZ;
  328. break;
  329. default:
  330. razer_error("hw_lachesis: "
  331. "Read invalid frequency value from device (%u)\n",
  332. profcfg.freq);
  333. return -EINVAL;
  334. }
  335. err = razer_parse_buttonmap(profcfg.buttonmap, sizeof(profcfg.buttonmap),
  336. priv->buttons[i].mapping,
  337. ARRAY_SIZE(priv->buttons[i].mapping), 33);
  338. if (err)
  339. return err;
  340. }
  341. /* Select original profile */
  342. value = priv->cur_profile->nr + 1;
  343. err = lachesis_usb_write(priv, LIBUSB_REQUEST_SET_CONFIGURATION,
  344. 0x08, 0, &value, sizeof(value));
  345. if (err)
  346. return err;
  347. /* Get the LED states */
  348. err = lachesis_usb_read(priv, LIBUSB_REQUEST_CLEAR_FEATURE,
  349. 0x05, 0, &value, sizeof(value));
  350. if (err)
  351. return err;
  352. priv->led_states[LACHESIS_LED_LOGO] = !!(value & 0x01);
  353. priv->led_states[LACHESIS_LED_SCROLL] = !!(value & 0x02);
  354. /* Get the DPI map */
  355. err = lachesis_usb_read(priv, LIBUSB_REQUEST_CLEAR_FEATURE,
  356. 0x10, 0, &dpimap, sizeof(dpimap));
  357. if (err)
  358. return err;
  359. for (i = 0; i < LACHESIS_NR_DPIMAPPINGS; i++)
  360. priv->dpimappings[i].res[RAZER_DIM_0] = (dpimap.mappings[i].dpival0 + 1) * 125;
  361. return 0;
  362. }
  363. static int lachesis_get_fw_version(struct razer_mouse *m)
  364. {
  365. struct lachesis_private *priv = m->drv_data;
  366. return priv->fw_version;
  367. }
  368. static int lachesis_commit(struct razer_mouse *m, int force)
  369. {
  370. struct lachesis_private *priv = m->drv_data;
  371. int err = 0;
  372. if (!m->claim_count)
  373. return -EBUSY;
  374. if (priv->commit_pending || force) {
  375. err = lachesis_do_commit(priv);
  376. if (!err)
  377. priv->commit_pending = 0;
  378. }
  379. return err;
  380. }
  381. static int lachesis_led_toggle(struct razer_led *led,
  382. enum razer_led_state new_state)
  383. {
  384. struct razer_mouse_profile *p = led->u.mouse_prof;
  385. struct razer_mouse *m = p->mouse;
  386. struct lachesis_private *priv = m->drv_data;
  387. if (led->id >= LACHESIS_NR_LEDS)
  388. return -EINVAL;
  389. if ((new_state != RAZER_LED_OFF) &&
  390. (new_state != RAZER_LED_ON))
  391. return -EINVAL;
  392. if (p->nr != 0)
  393. return -EINVAL;
  394. if (!priv->m->claim_count)
  395. return -EBUSY;
  396. priv->led_states[led->id] = new_state;
  397. priv->commit_pending = 1;
  398. return 0;
  399. }
  400. static int lachesis_global_get_leds(struct razer_mouse *m,
  401. struct razer_led **leds_list)
  402. {
  403. struct lachesis_private *priv = m->drv_data;
  404. struct razer_led *scroll, *logo;
  405. scroll = zalloc(sizeof(struct razer_led));
  406. if (!scroll)
  407. return -ENOMEM;
  408. logo = zalloc(sizeof(struct razer_led));
  409. if (!logo) {
  410. free(scroll);
  411. return -ENOMEM;
  412. }
  413. scroll->name = "Scrollwheel";
  414. scroll->id = LACHESIS_LED_SCROLL;
  415. scroll->state = priv->led_states[LACHESIS_LED_SCROLL];
  416. scroll->toggle_state = lachesis_led_toggle;
  417. scroll->u.mouse_prof = &priv->profiles[0];
  418. logo->name = "GlowingLogo";
  419. logo->id = LACHESIS_LED_LOGO;
  420. logo->state = priv->led_states[LACHESIS_LED_LOGO];
  421. logo->toggle_state = lachesis_led_toggle;
  422. logo->u.mouse_prof = &priv->profiles[0];
  423. /* Link the list */
  424. *leds_list = scroll;
  425. scroll->next = logo;
  426. logo->next = NULL;
  427. return LACHESIS_NR_LEDS;
  428. }
  429. static int lachesis_supported_axes(struct razer_mouse *m,
  430. struct razer_axis **axes_list)
  431. {
  432. struct lachesis_private *priv = m->drv_data;
  433. *axes_list = priv->axes;
  434. return ARRAY_SIZE(priv->axes);
  435. }
  436. static int lachesis_supported_freqs(struct razer_mouse *m,
  437. enum razer_mouse_freq **freq_list)
  438. {
  439. enum razer_mouse_freq *list;
  440. const int count = 3;
  441. list = malloc(sizeof(*list) * count);
  442. if (!list)
  443. return -ENOMEM;
  444. list[0] = RAZER_MOUSE_FREQ_1000HZ;
  445. list[1] = RAZER_MOUSE_FREQ_500HZ;
  446. list[2] = RAZER_MOUSE_FREQ_125HZ;
  447. *freq_list = list;
  448. return count;
  449. }
  450. static enum razer_mouse_freq lachesis_get_freq(struct razer_mouse *m,
  451. unsigned int profile_nr)
  452. {
  453. struct lachesis_private *priv = m->drv_data;
  454. if (profile_nr >= ARRAY_SIZE(priv->cur_freq))
  455. return -EINVAL;
  456. return priv->cur_freq[profile_nr];
  457. }
  458. static enum razer_mouse_freq lachesis_profile_get_freq(struct razer_mouse_profile *p)
  459. {
  460. return lachesis_get_freq(p->mouse, p->nr);
  461. }
  462. static int lachesis_set_freq(struct razer_mouse *m,
  463. unsigned int profile_nr,
  464. enum razer_mouse_freq freq)
  465. {
  466. struct lachesis_private *priv = m->drv_data;
  467. if (!priv->m->claim_count)
  468. return -EBUSY;
  469. if (profile_nr >= ARRAY_SIZE(priv->cur_freq))
  470. return -EINVAL;
  471. priv->cur_freq[profile_nr] = freq;
  472. priv->commit_pending = 1;
  473. return 0;
  474. }
  475. static int lachesis_profile_set_freq(struct razer_mouse_profile *p,
  476. enum razer_mouse_freq freq)
  477. {
  478. return lachesis_set_freq(p->mouse, p->nr, freq);
  479. }
  480. static int lachesis_supported_resolutions(struct razer_mouse *m,
  481. enum razer_mouse_res **res_list)
  482. {
  483. enum razer_mouse_res *list;
  484. unsigned int i, count = 0, res, step = 0;
  485. count = 32;
  486. step = RAZER_MOUSE_RES_125DPI;
  487. list = malloc(sizeof(*list) * count);
  488. if (!list)
  489. return -ENOMEM;
  490. res = step;
  491. for (i = 0; i < count; i++) {
  492. list[i] = res;
  493. res += step;
  494. }
  495. *res_list = list;
  496. return count;
  497. }
  498. static struct razer_mouse_profile * lachesis_get_profiles(struct razer_mouse *m)
  499. {
  500. struct lachesis_private *priv = m->drv_data;
  501. return &priv->profiles[0];
  502. }
  503. static struct razer_mouse_profile * lachesis_get_active_profile(struct razer_mouse *m)
  504. {
  505. struct lachesis_private *priv = m->drv_data;
  506. return priv->cur_profile;
  507. }
  508. static int lachesis_set_active_profile(struct razer_mouse *m,
  509. struct razer_mouse_profile *p)
  510. {
  511. struct lachesis_private *priv = m->drv_data;
  512. if (!priv->m->claim_count)
  513. return -EBUSY;
  514. priv->cur_profile = p;
  515. priv->commit_pending = 1;
  516. return 0;
  517. }
  518. static int lachesis_supported_dpimappings(struct razer_mouse *m,
  519. struct razer_mouse_dpimapping **res_ptr)
  520. {
  521. struct lachesis_private *priv = m->drv_data;
  522. *res_ptr = &priv->dpimappings[0];
  523. return LACHESIS_NR_DPIMAPPINGS;
  524. }
  525. static struct razer_mouse_dpimapping * lachesis_get_dpimapping(struct razer_mouse_profile *p,
  526. struct razer_axis *axis)
  527. {
  528. struct lachesis_private *priv = p->mouse->drv_data;
  529. if (p->nr >= ARRAY_SIZE(priv->cur_dpimapping))
  530. return NULL;
  531. return priv->cur_dpimapping[p->nr];
  532. }
  533. static int lachesis_set_dpimapping(struct razer_mouse_profile *p,
  534. struct razer_axis *axis,
  535. struct razer_mouse_dpimapping *d)
  536. {
  537. struct lachesis_private *priv = p->mouse->drv_data;
  538. razer_id_mask_t idmask;
  539. if (!priv->m->claim_count)
  540. return -EBUSY;
  541. if (p->nr >= ARRAY_SIZE(priv->cur_dpimapping))
  542. return -EINVAL;
  543. razer_id_mask_zero(&idmask);
  544. if (d->profile_mask != idmask)
  545. return -EINVAL;
  546. priv->cur_dpimapping[p->nr] = d;
  547. priv->commit_pending = 1;
  548. return 0;
  549. }
  550. static int lachesis_dpimapping_modify(struct razer_mouse_dpimapping *d,
  551. enum razer_dimension dim,
  552. enum razer_mouse_res res)
  553. {
  554. struct lachesis_private *priv = d->mouse->drv_data;
  555. if ((int)dim < 0 || (unsigned int)dim >= ARRAY_SIZE(d->res))
  556. return -EINVAL;
  557. if (!priv->m->claim_count)
  558. return -EBUSY;
  559. d->res[dim] = res;
  560. priv->commit_pending = 1;
  561. return 0;
  562. }
  563. static int lachesis_supported_buttons(struct razer_mouse *m,
  564. struct razer_button **res_ptr)
  565. {
  566. *res_ptr = lachesis_physical_buttons;
  567. return ARRAY_SIZE(lachesis_physical_buttons);
  568. }
  569. static int lachesis_supported_button_functions(struct razer_mouse *m,
  570. struct razer_button_function **res_ptr)
  571. {
  572. *res_ptr = lachesis_button_functions;
  573. return ARRAY_SIZE(lachesis_button_functions);
  574. }
  575. static struct razer_button_function * lachesis_get_button_function(struct razer_mouse_profile *p,
  576. struct razer_button *b)
  577. {
  578. struct lachesis_private *priv = p->mouse->drv_data;
  579. struct lachesis_buttons *buttons;
  580. if (p->nr > ARRAY_SIZE(priv->buttons))
  581. return NULL;
  582. buttons = &priv->buttons[p->nr];
  583. return razer_get_buttonfunction_by_button(
  584. buttons->mapping, ARRAY_SIZE(buttons->mapping),
  585. lachesis_button_functions, ARRAY_SIZE(lachesis_button_functions),
  586. b);
  587. }
  588. static int lachesis_set_button_function(struct razer_mouse_profile *p,
  589. struct razer_button *b,
  590. struct razer_button_function *f)
  591. {
  592. struct lachesis_private *priv = p->mouse->drv_data;
  593. struct lachesis_buttons *buttons;
  594. struct razer_buttonmapping *mapping;
  595. if (!priv->m->claim_count)
  596. return -EBUSY;
  597. if (p->nr > ARRAY_SIZE(priv->buttons))
  598. return -EINVAL;
  599. buttons = &priv->buttons[p->nr];
  600. mapping = razer_get_buttonmapping_by_physid(
  601. buttons->mapping, ARRAY_SIZE(buttons->mapping),
  602. b->id);
  603. if (!mapping)
  604. return -ENODEV;
  605. mapping->logical = f->id;
  606. priv->commit_pending = 1;
  607. return 0;
  608. }
  609. int razer_lachesis_init(struct razer_mouse *m,
  610. struct libusb_device *usbdev)
  611. {
  612. struct lachesis_private *priv;
  613. struct libusb_device_descriptor desc;
  614. unsigned int i, j;
  615. int err;
  616. BUILD_BUG_ON(sizeof(struct lachesis_profcfg_cmd) != 0x18C);
  617. BUILD_BUG_ON(sizeof(struct lachesis_dpimap_cmd) != 0x60);
  618. err = libusb_get_device_descriptor(usbdev, &desc);
  619. if (err) {
  620. razer_error("hw_lachesis: Failed to get device descriptor\n");
  621. return -EIO;
  622. }
  623. priv = zalloc(sizeof(struct lachesis_private));
  624. if (!priv)
  625. return -ENOMEM;
  626. priv->m = m;
  627. m->drv_data = priv;
  628. err = razer_usb_add_used_interface(m->usb_ctx, 0, 0);
  629. err |= razer_usb_add_used_interface(m->usb_ctx, 1, 0);
  630. if (err) {
  631. err = -ENODEV;
  632. goto err_free;
  633. }
  634. for (i = 0; i < LACHESIS_NR_PROFILES; i++) {
  635. priv->profiles[i].nr = i;
  636. priv->profiles[i].get_freq = lachesis_profile_get_freq;
  637. priv->profiles[i].set_freq = lachesis_profile_set_freq;
  638. priv->profiles[i].get_dpimapping = lachesis_get_dpimapping;
  639. priv->profiles[i].set_dpimapping = lachesis_set_dpimapping;
  640. priv->profiles[i].get_button_function = lachesis_get_button_function;
  641. priv->profiles[i].set_button_function = lachesis_set_button_function;
  642. priv->profiles[i].mouse = m;
  643. }
  644. razer_init_axes(&priv->axes[0],
  645. "X", 0,
  646. "Y", 0,
  647. "Scroll", 0);
  648. for (i = 0; i < LACHESIS_NR_DPIMAPPINGS; i++) {
  649. priv->dpimappings[i].nr = i;
  650. for (j = 0; j < RAZER_NR_DIMS; j++)
  651. priv->dpimappings[i].res[j] = RAZER_MOUSE_RES_UNKNOWN;
  652. priv->dpimappings[i].dimension_mask = (1 << RAZER_DIM_0);
  653. razer_id_mask_zero(&priv->dpimappings[i].profile_mask);
  654. priv->dpimappings[i].change = lachesis_dpimapping_modify;
  655. priv->dpimappings[i].mouse = m;
  656. }
  657. err = m->claim(m);
  658. if (err) {
  659. razer_error("hw_lachesis: "
  660. "Failed to initially claim the device\n");
  661. goto err_free;
  662. }
  663. err = lachesis_read_devinfo(priv);
  664. if (err) {
  665. razer_error("hw_lachesis: Failed to get firmware version\n");
  666. goto err_release;
  667. }
  668. err = lachesis_read_config_from_hw(priv);
  669. if (err) {
  670. razer_error("hw_lachesis: "
  671. "Failed to read the configuration from hardware\n");
  672. goto err_release;
  673. }
  674. razer_generic_usb_gen_idstr(usbdev, m->usb_ctx->h, "Lachesis Classic", 1,
  675. NULL, m->idstr);
  676. m->type = RAZER_MOUSETYPE_LACHESIS;
  677. m->get_fw_version = lachesis_get_fw_version;
  678. m->commit = lachesis_commit;
  679. m->global_get_leds = lachesis_global_get_leds;
  680. m->nr_profiles = ARRAY_SIZE(priv->profiles);
  681. m->get_profiles = lachesis_get_profiles;
  682. m->get_active_profile = lachesis_get_active_profile;
  683. m->set_active_profile = lachesis_set_active_profile;
  684. m->supported_axes = lachesis_supported_axes;
  685. m->supported_resolutions = lachesis_supported_resolutions;
  686. m->supported_freqs = lachesis_supported_freqs;
  687. m->supported_dpimappings = lachesis_supported_dpimappings;
  688. m->supported_buttons = lachesis_supported_buttons;
  689. m->supported_button_functions = lachesis_supported_button_functions;
  690. err = lachesis_do_commit(priv);
  691. if (err) {
  692. razer_error("hw_lachesis: Failed to commit initial settings\n");
  693. goto err_release;
  694. }
  695. m->release(m);
  696. return 0;
  697. err_release:
  698. m->release(m);
  699. err_free:
  700. free(priv);
  701. return err;
  702. }
  703. void razer_lachesis_release(struct razer_mouse *m)
  704. {
  705. struct lachesis_private *priv = m->drv_data;
  706. free(priv);
  707. }