librazer.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594
  1. /*
  2. * Razer device access library
  3. *
  4. * Copyright (C) 2007-2018 Michael Buesch <m@bues.ch>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. */
  16. #include "librazer.h"
  17. #include "razer_private.h"
  18. #include "config.h"
  19. #include "profile_emulation.h"
  20. #include "hw_deathadder.h"
  21. #include "hw_deathadder2013.h"
  22. #include "hw_deathadder_chroma.h"
  23. #include "hw_naga.h"
  24. #include "hw_krait.h"
  25. #include "hw_lachesis.h"
  26. #include "hw_lachesis5k6.h"
  27. #include "hw_copperhead.h"
  28. #include "hw_boomslangce.h"
  29. #include "hw_imperator.h"
  30. #include "hw_taipan.h"
  31. #include "hw_mamba_tournament_edition.h"
  32. #include "hw_diamondback_chroma.h"
  33. #include <stdint.h>
  34. #include <stdlib.h>
  35. #include <string.h>
  36. #include <errno.h>
  37. #include <stdio.h>
  38. #include <unistd.h>
  39. #include <sys/ioctl.h>
  40. enum razer_devtype {
  41. RAZER_DEVTYPE_MOUSE,
  42. };
  43. /** struct razer_mouse_base_ops - Basic device-init operations
  44. *
  45. * @type: The type ID.
  46. *
  47. * @init: Initialize the device and its private data structures.
  48. *
  49. * @release: Release device and data structures.
  50. */
  51. struct razer_mouse_base_ops {
  52. enum razer_mouse_type type;
  53. int (*init)(struct razer_mouse *m, struct libusb_device *udev);
  54. void (*release)(struct razer_mouse *m);
  55. };
  56. struct razer_usb_device {
  57. uint16_t vendor; /* Vendor ID */
  58. uint16_t product; /* Product ID */
  59. enum razer_devtype type;
  60. union {
  61. const struct razer_mouse_base_ops *mouse_ops;
  62. } u;
  63. };
  64. static const struct razer_mouse_base_ops razer_deathadder_base_ops = {
  65. .type = RAZER_MOUSETYPE_DEATHADDER,
  66. .init = razer_deathadder_init,
  67. .release = razer_deathadder_release,
  68. };
  69. static const struct razer_mouse_base_ops razer_deathadder2013_base_ops = {
  70. .type = RAZER_MOUSETYPE_DEATHADDER,
  71. .init = razer_deathadder2013_init,
  72. .release = razer_deathadder2013_release,
  73. };
  74. static const struct razer_mouse_base_ops razer_deathadder_chroma_base_ops = {
  75. .type = RAZER_MOUSETYPE_DEATHADDER,
  76. .init = razer_deathadder_chroma_init,
  77. .release = razer_deathadder_chroma_release,
  78. };
  79. static const struct razer_mouse_base_ops razer_naga_base_ops = {
  80. .type = RAZER_MOUSETYPE_NAGA,
  81. .init = razer_naga_init,
  82. .release = razer_naga_release,
  83. };
  84. static const struct razer_mouse_base_ops razer_krait_base_ops = {
  85. .type = RAZER_MOUSETYPE_KRAIT,
  86. .init = razer_krait_init,
  87. .release = razer_krait_release,
  88. };
  89. static const struct razer_mouse_base_ops razer_lachesis_base_ops = {
  90. .type = RAZER_MOUSETYPE_LACHESIS,
  91. .init = razer_lachesis_init,
  92. .release = razer_lachesis_release,
  93. };
  94. #if 0
  95. static const struct razer_mouse_base_ops razer_lachesis5k6_base_ops = {
  96. .type = RAZER_MOUSETYPE_LACHESIS,
  97. .init = razer_lachesis5k6_init,
  98. .release = razer_lachesis5k6_release,
  99. };
  100. #endif
  101. static const struct razer_mouse_base_ops razer_copperhead_base_ops = {
  102. .type = RAZER_MOUSETYPE_COPPERHEAD,
  103. .init = razer_copperhead_init,
  104. .release = razer_copperhead_release,
  105. };
  106. static const struct razer_mouse_base_ops razer_boomslangce_base_ops = {
  107. .type = RAZER_MOUSETYPE_BOOMSLANGCE,
  108. .init = razer_boomslangce_init,
  109. .release = razer_boomslangce_release,
  110. };
  111. static const struct razer_mouse_base_ops razer_imperator_base_ops = {
  112. .type = RAZER_MOUSETYPE_IMPERATOR,
  113. .init = razer_imperator_init,
  114. .release = razer_imperator_release,
  115. };
  116. static const struct razer_mouse_base_ops razer_taipan_base_ops = {
  117. .type = RAZER_MOUSETYPE_TAIPAN,
  118. .init = razer_taipan_init,
  119. .release = razer_taipan_release,
  120. };
  121. static const struct razer_mouse_base_ops razer_mamba_te_base_ops = {
  122. .type = RAZER_MOUSETYPE_MAMBA_TE,
  123. .init = razer_mamba_te_init,
  124. .release = razer_mamba_te_release,
  125. };
  126. static const struct razer_mouse_base_ops razer_diamondback_chroma_base_ops = {
  127. .type = RAZER_MOUSETYPE_DIAMONDBACK_CHROMA,
  128. .init = razer_diamondback_chroma_init,
  129. .release = razer_diamondback_chroma_release,
  130. };
  131. #define USBVENDOR_ANY 0xFFFF
  132. #define USBPRODUCT_ANY 0xFFFF
  133. #define USB_MOUSE(_vendor, _product, _mouse_ops) \
  134. { .vendor = _vendor, .product = _product, \
  135. .type = RAZER_DEVTYPE_MOUSE, \
  136. .u = { .mouse_ops = _mouse_ops, }, }
  137. /* Table of supported USB devices. */
  138. static const struct razer_usb_device razer_usbdev_table[] = {
  139. USB_MOUSE(0x1532, 0x0007, &razer_deathadder_base_ops), /* classic */
  140. USB_MOUSE(0x1532, 0x0016, &razer_deathadder_base_ops), /* 3500 DPI */
  141. USB_MOUSE(0x1532, 0x0029, &razer_deathadder_base_ops), /* black edition */
  142. USB_MOUSE(0x1532, 0x0037, &razer_deathadder2013_base_ops), /* 2013 edition */
  143. USB_MOUSE(0x1532, 0x0038, &razer_deathadder2013_base_ops), /* 1800 edition/Korea PCBang edition */
  144. USB_MOUSE(0x1532, 0x0043, &razer_deathadder_chroma_base_ops), /* Chroma edition */
  145. // USB_MOUSE(0x04B4, 0xE006, &razer_deathadder_base_ops), /* cypress bootloader */
  146. USB_MOUSE(0x1532, 0x0003, &razer_krait_base_ops),
  147. USB_MOUSE(0x1532, 0x000C, &razer_lachesis_base_ops), /* classic */
  148. // USB_MOUSE(0x1532, 0x001E, &razer_lachesis5k6_base_ops), /* 5600 DPI */
  149. USB_MOUSE(0x1532, RAZER_NAGA_PID_CLASSIC, &razer_naga_base_ops),
  150. USB_MOUSE(0x1532, RAZER_NAGA_PID_EPIC, &razer_naga_base_ops),
  151. USB_MOUSE(0x1532, RAZER_NAGA_PID_2012, &razer_naga_base_ops),
  152. USB_MOUSE(0x1532, RAZER_NAGA_PID_HEX, &razer_naga_base_ops),
  153. USB_MOUSE(0x1532, RAZER_NAGA_PID_2014, &razer_naga_base_ops),
  154. USB_MOUSE(0x1532, RAZER_NAGA_PID_HEX_2014, &razer_naga_base_ops),
  155. USB_MOUSE(0x1532, 0x0101, &razer_copperhead_base_ops),
  156. USB_MOUSE(0x1532, 0x0005, &razer_boomslangce_base_ops),
  157. USB_MOUSE(0x1532, 0x0017, &razer_imperator_base_ops),
  158. USB_MOUSE(0x1532, 0x0034, &razer_taipan_base_ops),
  159. USB_MOUSE(0x1532, 0x0046, &razer_mamba_te_base_ops), /*mamba tournament edition*/
  160. USB_MOUSE(0x1532, 0x004C, &razer_diamondback_chroma_base_ops),
  161. { 0, }, /* List end */
  162. };
  163. #undef USB_MOUSE
  164. static struct libusb_context *libusb_ctx;
  165. static struct razer_mouse *mice_list = NULL;
  166. /* We currently only have one handler. */
  167. static razer_event_handler_t event_handler;
  168. static struct config_file *razer_config_file = NULL;
  169. static bool profile_emu_enabled;
  170. razer_logfunc_t razer_logfunc_info;
  171. razer_logfunc_t razer_logfunc_error;
  172. razer_logfunc_t razer_logfunc_debug;
  173. static inline bool razer_initialized(void)
  174. {
  175. return !!libusb_ctx;
  176. }
  177. int razer_register_event_handler(razer_event_handler_t handler)
  178. {
  179. if (event_handler)
  180. return -EEXIST;
  181. event_handler = handler;
  182. return 0;
  183. }
  184. void razer_unregister_event_handler(razer_event_handler_t handler)
  185. {
  186. event_handler = NULL;
  187. }
  188. static void razer_notify_event(enum razer_event type,
  189. const struct razer_event_data *data)
  190. {
  191. if (event_handler)
  192. event_handler(type, data);
  193. }
  194. static int match_usbdev(const struct libusb_device_descriptor *desc,
  195. const struct razer_usb_device *id)
  196. {
  197. if ((desc->idVendor != id->vendor) &&
  198. (id->vendor != USBVENDOR_ANY))
  199. return 0;
  200. if ((desc->idProduct != id->product) &&
  201. (id->product != USBPRODUCT_ANY))
  202. return 0;
  203. return 1;
  204. }
  205. static const struct razer_usb_device * usbdev_lookup(const struct libusb_device_descriptor *desc)
  206. {
  207. const struct razer_usb_device *id = &(razer_usbdev_table[0]);
  208. while (id->vendor || id->product) {
  209. if (match_usbdev(desc, id))
  210. return id;
  211. id++;
  212. }
  213. return NULL;
  214. }
  215. static void mouse_list_add(struct razer_mouse **base, struct razer_mouse *new_entry)
  216. {
  217. struct razer_mouse *i;
  218. new_entry->next = NULL;
  219. if (!(*base)) {
  220. *base = new_entry;
  221. return;
  222. }
  223. for (i = *base; i->next; i = i->next)
  224. ;
  225. i->next = new_entry;
  226. }
  227. static void mouse_list_del(struct razer_mouse **base, struct razer_mouse *del_entry)
  228. {
  229. struct razer_mouse *i;
  230. if (del_entry == *base) {
  231. *base = (*base)->next;
  232. return;
  233. }
  234. for (i = *base; i && (i->next != del_entry); i = i->next)
  235. ;
  236. if (i)
  237. i->next = del_entry->next;
  238. }
  239. static struct razer_mouse * mouse_list_find(struct razer_mouse *base,
  240. struct libusb_device *udev)
  241. {
  242. struct razer_mouse *m, *next;
  243. uint8_t busnr = libusb_get_bus_number(udev);
  244. uint8_t devaddr = libusb_get_device_address(udev);
  245. razer_for_each_mouse(m, next, base) {
  246. if (m->usb_ctx) {
  247. if (libusb_get_bus_number(m->usb_ctx->dev) == busnr &&
  248. libusb_get_device_address(m->usb_ctx->dev) == devaddr)
  249. return m;
  250. }
  251. }
  252. return NULL;
  253. }
  254. static int parse_idstr(char *idstr, char **devtype, char **devname,
  255. char **buspos, char **devid)
  256. {
  257. *devtype = idstr;
  258. *devname = razer_strsplit(*devtype, ':');
  259. *buspos = razer_strsplit(*devname, ':');
  260. *devid = razer_strsplit(*buspos, ':');
  261. if (!*devtype || !*devname || !*buspos || !*devid)
  262. return -EINVAL;
  263. return 0;
  264. }
  265. static bool simple_globcmp(const char *string,
  266. const char *template)
  267. {
  268. char s, t, tnext;
  269. while (1) {
  270. s = string[0];
  271. t = template[0];
  272. if (s == '\0' && t == '\0')
  273. break;
  274. if (t == '*') {
  275. tnext = template[1];
  276. if (s == '\0') {
  277. if (tnext == '\0')
  278. break;
  279. return 0;
  280. }
  281. if (s == tnext) {
  282. template++;
  283. continue;
  284. }
  285. } else {
  286. if (s == '\0' || t == '\0')
  287. return 0;
  288. if (s != t)
  289. return 0;
  290. template++;
  291. }
  292. string++;
  293. }
  294. return 1; /* Match */
  295. }
  296. static bool mouse_idstr_glob_match(struct config_file *f,
  297. void *context, void *data,
  298. const char *section)
  299. {
  300. struct razer_mouse *m = context;
  301. const char **matched_section = data;
  302. char idstr[RAZER_IDSTR_MAX_SIZE + 1] = { 0, };
  303. char *idstr_devtype, *idstr_devname, *idstr_buspos, *idstr_devid;
  304. char globstr[RAZER_IDSTR_MAX_SIZE + 1] = { 0, };
  305. char *globstr_devtype, *globstr_devname, *globstr_buspos, *globstr_devid;
  306. if (strlen(section) > RAZER_IDSTR_MAX_SIZE) {
  307. razer_error("globbed idstr \"%s\" in config too long\n",
  308. section);
  309. return 1;
  310. }
  311. strcpy(globstr, section);
  312. strcpy(idstr, m->idstr);
  313. if (parse_idstr(globstr, &globstr_devtype, &globstr_devname,
  314. &globstr_buspos, &globstr_devid))
  315. return 1;
  316. if (parse_idstr(idstr, &idstr_devtype, &idstr_devname,
  317. &idstr_buspos, &idstr_devid)) {
  318. razer_error("INTERNAL-ERROR: Failed to parse idstr \"%s\"\n",
  319. idstr);
  320. return 1;
  321. }
  322. if (!simple_globcmp(idstr_devtype, globstr_devtype))
  323. return 1;
  324. if (!simple_globcmp(idstr_devname, globstr_devname))
  325. return 1;
  326. if (!simple_globcmp(idstr_buspos, globstr_buspos))
  327. return 1;
  328. if (!simple_globcmp(idstr_devid, globstr_devid))
  329. return 1;
  330. *matched_section = section;
  331. return 0; /* Match */
  332. }
  333. static struct razer_mouse_profile * find_prof(struct razer_mouse *m, unsigned int nr)
  334. {
  335. struct razer_mouse_profile *list;
  336. unsigned int i;
  337. if (!m->get_profiles)
  338. return NULL;
  339. list = m->get_profiles(m);
  340. if (!list)
  341. return NULL;
  342. for (i = 0; i < m->nr_profiles; i++) {
  343. if (list[i].nr == nr)
  344. return &list[i];
  345. }
  346. return NULL;
  347. }
  348. static int parse_int_int_pair(const char *str, int *val0, int *val1)
  349. {
  350. char a[64] = { 0, }, b[64] = { 0, };
  351. int err;
  352. *val0 = *val1 = -1;
  353. err = razer_split_tuple(str, ':', min(sizeof(a), sizeof(b)),
  354. a, b, NULL);
  355. if (err) {
  356. /* It's not a pair. Interpret it as one value. */
  357. razer_strlcpy(a, str, sizeof(a));
  358. err = razer_string_to_int(razer_string_strip(a), val1);
  359. if (err)
  360. return -EINVAL;
  361. return 1;
  362. }
  363. err = razer_string_to_int(razer_string_strip(a), val0);
  364. err |= razer_string_to_int(razer_string_strip(b), val1);
  365. if (err)
  366. return -EINVAL;
  367. return 0;
  368. }
  369. static bool mouse_apply_one_config(struct config_file *f,
  370. void *context, void *data,
  371. const char *section,
  372. const char *item,
  373. const char *value)
  374. {
  375. struct razer_mouse *m = context;
  376. struct razer_mouse_profile *prof;
  377. bool *error_status = data;
  378. int err, nr;
  379. static const size_t tmplen = 128;
  380. char a[tmplen], b[tmplen], c[tmplen];
  381. //FIXME fixes for glob/prof configs
  382. if (strcasecmp(item, "profile") == 0) {
  383. int profile;
  384. err = razer_string_to_int(value, &profile);
  385. if (err || profile < 1 || (unsigned int)profile > m->nr_profiles)
  386. goto error;
  387. if (m->set_active_profile) {
  388. prof = find_prof(m, profile - 1);
  389. if (!prof)
  390. goto error;
  391. err = m->set_active_profile(m, prof);
  392. if (err)
  393. goto error;
  394. }
  395. } else if (strcasecmp(item, "res") == 0) {
  396. int profile, resolution, i;
  397. struct razer_mouse_dpimapping *mappings;
  398. err = parse_int_int_pair(value, &profile, &resolution);
  399. if (err == 1) {
  400. prof = m->get_active_profile(m);
  401. profile = prof->nr + 1;
  402. } else if (err)
  403. goto error;
  404. if (profile < 1 || resolution < 1)
  405. goto error;
  406. prof = find_prof(m, profile - 1);
  407. if (!prof)
  408. goto error;
  409. nr = m->supported_dpimappings(m, &mappings);
  410. if (nr <= 0)
  411. goto error;
  412. //FIXME dims
  413. for (i = 0; i < nr; i++) {
  414. if (resolution >= 100) {
  415. if ((int)(mappings[i].res[RAZER_DIM_0]) != resolution)
  416. continue;
  417. } else {
  418. if (mappings[i].nr != (unsigned int)resolution)
  419. continue;
  420. }
  421. err = prof->set_dpimapping(prof, NULL, &mappings[i]);
  422. if (err)
  423. goto error;
  424. goto ok;
  425. }
  426. goto invalid; /* res is invalid. Ignore it. */
  427. } else if (strcasecmp(item, "freq") == 0) {
  428. int profile, freq, i;
  429. enum razer_mouse_freq *freqs;
  430. err = parse_int_int_pair(value, &profile, &freq);
  431. if (err == 1) {
  432. prof = m->get_active_profile(m);
  433. profile = prof->nr + 1;
  434. } else if (err)
  435. goto error;
  436. if (profile < 1 || freq < 1)
  437. goto error;
  438. prof = find_prof(m, profile - 1);
  439. if (!prof)
  440. goto error;
  441. nr = m->supported_freqs(m, &freqs);
  442. if (nr <= 0)
  443. goto error;
  444. for (i = 0; i < nr; i++) {
  445. if (freqs[i] != (enum razer_mouse_freq)freq)
  446. continue;
  447. if (!prof->set_freq)
  448. goto invalid;
  449. err = prof->set_freq(prof, freqs[i]);
  450. razer_free_freq_list(freqs, nr);
  451. if (err)
  452. goto error;
  453. goto ok;
  454. }
  455. razer_free_freq_list(freqs, nr);
  456. goto error;
  457. } else if (strcasecmp(item, "led") == 0) {
  458. bool on;
  459. struct razer_led *leds, *led;
  460. const char *ledname, *ledstate;
  461. int profile;
  462. err = razer_split_tuple(value, ':', tmplen, a, b, c, NULL);
  463. if (err && err != -ENODATA)
  464. goto error;
  465. if (!strlen(a) || !strlen(b))
  466. goto error;
  467. if (strlen(c)) {
  468. /* A profile was specified */
  469. err = razer_string_to_int(razer_string_strip(a), &profile);
  470. if (err || profile < 1)
  471. goto error;
  472. prof = find_prof(m, profile - 1);
  473. if (!prof)
  474. goto error;
  475. ledname = razer_string_strip(b);
  476. ledstate = razer_string_strip(c);
  477. } else {
  478. /* Modify global LEDs */
  479. prof = NULL;
  480. ledname = razer_string_strip(a);
  481. ledstate = razer_string_strip(b);
  482. }
  483. err = razer_string_to_bool(ledstate, &on);
  484. if (err)
  485. goto error;
  486. if (prof) {
  487. if (prof->get_leds) {
  488. err = prof->get_leds(prof, &leds);
  489. } else {
  490. /* Try to fall back to global */
  491. if (!m->global_get_leds)
  492. goto ok; /* No LEDs. Ignore config. */
  493. err = m->global_get_leds(m, &leds);
  494. }
  495. } else {
  496. if (!m->global_get_leds)
  497. goto ok; /* No LEDs. Ignore config. */
  498. err = m->global_get_leds(m, &leds);
  499. }
  500. if (err < 0)
  501. goto error;
  502. if (err == 0)
  503. goto ok; /* No LEDs. Ignore config. */
  504. for (led = leds; led; led = led->next) {
  505. if (strcasecmp(led->name, ledname) != 0)
  506. continue;
  507. if (!led->toggle_state) {
  508. razer_free_leds(leds);
  509. goto invalid;
  510. }
  511. err = led->toggle_state(led,
  512. on ? RAZER_LED_ON : RAZER_LED_OFF);
  513. razer_free_leds(leds);
  514. if (err)
  515. goto error;
  516. goto ok;
  517. }
  518. razer_free_leds(leds);
  519. goto error;
  520. } else if (strcasecmp(item, "mode") == 0) {
  521. enum razer_led_mode mode;
  522. struct razer_led *leds, *led;
  523. const char *ledname, *ledmode;
  524. int profile;
  525. err = razer_split_tuple(value, ':', tmplen, a, b, c, NULL);
  526. if (err && err != -ENODATA)
  527. goto error;
  528. if (!strlen(a) || !strlen(b))
  529. goto error;
  530. if (strlen(c)) {
  531. /* A profile was specified */
  532. err = razer_string_to_int(razer_string_strip(a), &profile);
  533. if (err || profile < 1)
  534. goto error;
  535. prof = find_prof(m, profile - 1);
  536. if (!prof)
  537. goto error;
  538. ledname = razer_string_strip(b);
  539. ledmode = razer_string_strip(c);
  540. } else {
  541. /* Modify global LEDs */
  542. prof = NULL;
  543. ledname = razer_string_strip(a);
  544. ledmode = razer_string_strip(b);
  545. }
  546. err = razer_string_to_mode(ledmode, &mode);
  547. if (err)
  548. goto error;
  549. if (prof) {
  550. if (prof->get_leds) {
  551. err = prof->get_leds(prof, &leds);
  552. } else {
  553. /* Try to fall back to global */
  554. if (!m->global_get_leds)
  555. goto ok; /* No LEDs. Ignore config. */
  556. err = m->global_get_leds(m, &leds);
  557. }
  558. } else {
  559. if (!m->global_get_leds)
  560. goto ok; /* No LEDs. Ignore config. */
  561. err = m->global_get_leds(m, &leds);
  562. }
  563. if (err < 0)
  564. goto error;
  565. if (err == 0)
  566. goto ok; /* No LEDs. Ignore config. */
  567. for (led = leds; led; led = led->next) {
  568. if (strcasecmp(led->name, ledname) != 0)
  569. continue;
  570. if (!led->set_mode) {
  571. razer_free_leds(leds);
  572. goto invalid;
  573. }
  574. err = led->set_mode(led, mode);
  575. razer_free_leds(leds);
  576. if (err)
  577. goto error;
  578. goto ok;
  579. }
  580. razer_free_leds(leds);
  581. goto error;
  582. } else if (strcasecmp(item, "color") == 0) {
  583. struct razer_rgb_color color;
  584. struct razer_led *leds, *led;
  585. const char *ledname, *ledcolor;
  586. int profile;
  587. err = razer_split_tuple(value, ':', tmplen, a, b, c, NULL);
  588. if (err && err != -ENODATA)
  589. goto error;
  590. if (!strlen(a) || !strlen(b))
  591. goto error;
  592. if (strlen(c)) {
  593. /* A profile was specified */
  594. err = razer_string_to_int(razer_string_strip(a), &profile);
  595. if (err || profile < 1)
  596. goto error;
  597. prof = find_prof(m, profile - 1);
  598. if (!prof)
  599. goto error;
  600. ledname = razer_string_strip(b);
  601. ledcolor = razer_string_strip(c);
  602. } else {
  603. /* Modify global LEDs */
  604. prof = NULL;
  605. ledname = razer_string_strip(a);
  606. ledcolor = razer_string_strip(b);
  607. }
  608. err = razer_string_to_color(ledcolor, &color);
  609. if (err)
  610. goto error;
  611. if (prof) {
  612. if (prof->get_leds) {
  613. err = prof->get_leds(prof, &leds);
  614. } else {
  615. /* Try to fall back to global */
  616. if (!m->global_get_leds)
  617. goto ok; /* No LEDs. Ignore config. */
  618. err = m->global_get_leds(m, &leds);
  619. }
  620. } else {
  621. if (!m->global_get_leds)
  622. goto ok; /* No LEDs. Ignore config. */
  623. err = m->global_get_leds(m, &leds);
  624. }
  625. if (err < 0)
  626. goto error;
  627. if (err == 0)
  628. goto ok; /* No LEDs. Ignore config. */
  629. for (led = leds; led; led = led->next) {
  630. if (strcasecmp(led->name, ledname) != 0)
  631. continue;
  632. if (!led->change_color) {
  633. razer_free_leds(leds);
  634. goto invalid;
  635. }
  636. err = led->change_color(led, &color);
  637. razer_free_leds(leds);
  638. if (err)
  639. goto error;
  640. goto ok;
  641. }
  642. razer_free_leds(leds);
  643. goto error;
  644. } else if (strcasecmp(item, "disabled") == 0) {
  645. goto ok;
  646. } else
  647. goto invalid;
  648. ok:
  649. return 1;
  650. error:
  651. *error_status = 1;
  652. invalid:
  653. razer_error("Config section \"%s\" item \"%s\" "
  654. "invalid.\n", section, item);
  655. return *error_status ? 0 : 1;
  656. }
  657. static void mouse_apply_initial_config(struct razer_mouse *m)
  658. {
  659. const char *section = NULL;
  660. int err;
  661. bool error_status = 0;
  662. config_for_each_section(razer_config_file,
  663. m, &section,
  664. mouse_idstr_glob_match);
  665. if (!section)
  666. return;
  667. if (config_get_bool(razer_config_file, section,
  668. "disabled", 0, CONF_NOCASE)) {
  669. razer_debug("Initial config for \"%s\" is disabled. Not applying.\n",
  670. m->idstr);
  671. return;
  672. }
  673. razer_debug("Applying config section \"%s\" to \"%s\"\n",
  674. section, m->idstr);
  675. err = m->claim(m);
  676. if (err) {
  677. razer_error("Failed to claim \"%s\"\n", m->idstr);
  678. return;
  679. }
  680. config_for_each_item(razer_config_file,
  681. m, &error_status,
  682. section,
  683. mouse_apply_one_config);
  684. m->release(m);
  685. if (error_status) {
  686. razer_error("Failed to apply initial config "
  687. "to \"%s\"\n", m->idstr);
  688. }
  689. }
  690. static struct razer_usb_context * razer_create_usb_ctx(struct libusb_device *dev)
  691. {
  692. struct razer_usb_context *ctx;
  693. ctx = zalloc(sizeof(*ctx));
  694. if (!ctx)
  695. return NULL;
  696. ctx->dev = dev;
  697. ctx->bConfigurationValue = 1;
  698. return ctx;
  699. }
  700. static int mouse_default_claim(struct razer_mouse *m)
  701. {
  702. return razer_generic_usb_claim_refcount(m->usb_ctx, &m->claim_count);
  703. }
  704. static int mouse_default_release(struct razer_mouse *m)
  705. {
  706. int err = 0;
  707. if (m->claim_count == 1) {
  708. if (m->commit)
  709. err = m->commit(m, 0);
  710. }
  711. razer_generic_usb_release_refcount(m->usb_ctx, &m->claim_count);
  712. return err;
  713. }
  714. static struct razer_mouse * mouse_new(const struct razer_usb_device *id,
  715. struct libusb_device *udev)
  716. {
  717. struct razer_event_data ev;
  718. struct razer_mouse *m;
  719. int err;
  720. libusb_ref_device(udev);
  721. m = zalloc(sizeof(*m));
  722. if (!m)
  723. return NULL;
  724. m->usb_ctx = razer_create_usb_ctx(udev);
  725. if (!m->usb_ctx)
  726. goto err_free_mouse;
  727. /* Set default values and callbacks */
  728. m->nr_profiles = 1;
  729. m->claim = mouse_default_claim;
  730. m->release = mouse_default_release;
  731. /* Call the driver init */
  732. m->base_ops = id->u.mouse_ops;
  733. err = m->base_ops->init(m, udev);
  734. if (err)
  735. goto err_free_ctx;
  736. udev = m->usb_ctx->dev;
  737. if (WARN_ON(m->nr_profiles <= 0))
  738. goto err_release;
  739. if (m->nr_profiles == 1 && !m->get_active_profile)
  740. m->get_active_profile = m->get_profiles;
  741. if (profile_emu_enabled && m->nr_profiles == 1) {
  742. err = razer_mouse_init_profile_emulation(m);
  743. if (err)
  744. goto err_release;
  745. }
  746. mouse_apply_initial_config(m);
  747. razer_debug("Allocated and initialized new mouse \"%s\"\n",
  748. m->idstr);
  749. ev.u.mouse = m;
  750. razer_notify_event(RAZER_EV_MOUSE_ADD, &ev);
  751. return m;
  752. err_release:
  753. m->base_ops->release(m);
  754. err_free_ctx:
  755. razer_free(m->usb_ctx, sizeof(*(m->usb_ctx)));
  756. err_free_mouse:
  757. razer_free(m, sizeof(*m));
  758. libusb_unref_device(udev);
  759. return NULL;
  760. }
  761. static void razer_free_mouse(struct razer_mouse *m)
  762. {
  763. struct razer_event_data ev;
  764. razer_debug("Freeing mouse (type=%d)\n",
  765. m->base_ops->type);
  766. ev.u.mouse = m;
  767. razer_notify_event(RAZER_EV_MOUSE_REMOVE, &ev);
  768. if (m->release == mouse_default_release) {
  769. while (m->claim_count)
  770. m->release(m);
  771. }
  772. razer_mouse_exit_profile_emulation(m);
  773. m->base_ops->release(m);
  774. libusb_unref_device(m->usb_ctx->dev);
  775. razer_free(m->usb_ctx, sizeof(*(m->usb_ctx)));
  776. razer_free(m, sizeof(*m));
  777. }
  778. static void razer_free_mice(struct razer_mouse *mouse_list)
  779. {
  780. struct razer_mouse *mouse, *next;
  781. for (mouse = mouse_list; mouse; ) {
  782. next = mouse->next;
  783. razer_free_mouse(mouse);
  784. mouse = next;
  785. }
  786. }
  787. struct new_razer_usb_device {
  788. const struct razer_usb_device *id;
  789. struct usb_device *udev;
  790. };
  791. struct razer_mouse * razer_rescan_mice(void)
  792. {
  793. struct libusb_device **devlist, *dev;
  794. ssize_t nr_devices;
  795. unsigned int i;
  796. int err;
  797. struct libusb_device_descriptor desc;
  798. const struct razer_usb_device *id;
  799. struct razer_mouse *m, *next;
  800. nr_devices = libusb_get_device_list(libusb_ctx, &devlist);
  801. if (nr_devices < 0) {
  802. razer_error("razer_rescan_mice: Failed to get USB device list\n");
  803. return NULL;
  804. }
  805. for (i = 0; i < nr_devices; i++) {
  806. dev = devlist[i];
  807. err = libusb_get_device_descriptor(dev, &desc);
  808. if (err) {
  809. razer_error("razer_rescan_mice: Failed to get descriptor\n");
  810. continue;
  811. }
  812. id = usbdev_lookup(&desc);
  813. if (!id || id->type != RAZER_DEVTYPE_MOUSE)
  814. continue;
  815. m = mouse_list_find(mice_list, dev);
  816. if (m) {
  817. /* We already had this mouse */
  818. m->flags |= RAZER_MOUSEFLG_PRESENT;
  819. } else {
  820. /* We don't have this mouse, yet. Create a new one */
  821. m = mouse_new(id, dev);
  822. if (m) {
  823. m->flags |= RAZER_MOUSEFLG_PRESENT;
  824. mouse_list_add(&mice_list, m);
  825. }
  826. }
  827. }
  828. /* Remove mice that are not connected anymore. */
  829. razer_for_each_mouse(m, next, mice_list) {
  830. if (m->flags & RAZER_MOUSEFLG_PRESENT) {
  831. m->flags &= ~RAZER_MOUSEFLG_PRESENT;
  832. continue;
  833. }
  834. mouse_list_del(&mice_list, m);
  835. razer_free_mouse(m);
  836. }
  837. libusb_free_device_list(devlist, 1);
  838. return mice_list;
  839. }
  840. int razer_reconfig_mice(void)
  841. {
  842. struct razer_mouse *m, *next;
  843. int err;
  844. razer_for_each_mouse(m, next, mice_list) {
  845. err = m->claim(m);
  846. if (err)
  847. return err;
  848. if (m->commit)
  849. err = m->commit(m, 1);
  850. m->release(m);
  851. if (err)
  852. return err;
  853. }
  854. return 0;
  855. }
  856. void razer_free_freq_list(enum razer_mouse_freq *freq_list, int count)
  857. {
  858. if (freq_list)
  859. free(freq_list);
  860. }
  861. void razer_free_resolution_list(enum razer_mouse_res *res_list, int count)
  862. {
  863. if (res_list)
  864. free(res_list);
  865. }
  866. void razer_free_leds(struct razer_led *led_list)
  867. {
  868. struct razer_led *led, *next;
  869. for (led = led_list; led; ) {
  870. next = led->next;
  871. free(led);
  872. led = next;
  873. }
  874. }
  875. int razer_init(int enable_profile_emu)
  876. {
  877. int err = 0;
  878. if (!razer_initialized())
  879. err = libusb_init(&libusb_ctx);
  880. if (!err)
  881. profile_emu_enabled = enable_profile_emu;
  882. return err ? -EINVAL : 0;
  883. }
  884. void razer_exit(void)
  885. {
  886. if (!razer_initialized())
  887. return;
  888. razer_free_mice(mice_list);
  889. mice_list = NULL;
  890. config_file_free(razer_config_file);
  891. razer_config_file = NULL;
  892. libusb_exit(libusb_ctx);
  893. libusb_ctx = NULL;
  894. }
  895. int razer_usb_add_used_interface(struct razer_usb_context *ctx,
  896. int bInterfaceNumber,
  897. int bAlternateSetting)
  898. {
  899. struct razer_usb_interface *interf;
  900. if (ctx->nr_interfaces >= ARRAY_SIZE(ctx->interfaces)) {
  901. razer_error("USB context interface array overflow\n");
  902. return -ENOSPC;
  903. }
  904. interf = &ctx->interfaces[ctx->nr_interfaces];
  905. interf->bInterfaceNumber = bInterfaceNumber;
  906. interf->bAlternateSetting = bAlternateSetting;
  907. ctx->nr_interfaces++;
  908. return 0;
  909. }
  910. static void razer_reattach_usb_kdrv(struct razer_usb_context *ctx,
  911. int bInterfaceNumber)
  912. {
  913. int res;
  914. res = libusb_kernel_driver_active(ctx->h, bInterfaceNumber);
  915. if (res == 1)
  916. return;
  917. if (res) {
  918. razer_error("Failed to get kernel driver state\n");
  919. return;
  920. }
  921. res = libusb_attach_kernel_driver(ctx->h, bInterfaceNumber);
  922. if (res) {
  923. razer_error("Failed to reconnect the kernel driver (%d).\n"
  924. "The device most likely won't work now. Try to replug it.\n", res);
  925. return;
  926. }
  927. }
  928. static void razer_usb_release(struct razer_usb_context *ctx,
  929. int bInterfaceNumber)
  930. {
  931. libusb_release_interface(ctx->h, bInterfaceNumber);
  932. razer_reattach_usb_kdrv(ctx, bInterfaceNumber);
  933. }
  934. int razer_generic_usb_claim(struct razer_usb_context *ctx)
  935. {
  936. unsigned int tries, i;
  937. int err, config;
  938. struct razer_usb_interface *interf;
  939. err = libusb_open(ctx->dev, &ctx->h);
  940. if (err) {
  941. razer_error("razer_generic_usb_claim: Failed to open USB device\n");
  942. return -ENODEV;
  943. }
  944. /* Detach kernel drivers for all interfaces. */
  945. for (i = 0; i < ctx->nr_interfaces; i++) {
  946. interf = &ctx->interfaces[i];
  947. err = libusb_kernel_driver_active(ctx->h, interf->bInterfaceNumber);
  948. if (err == 1) {
  949. err = libusb_detach_kernel_driver(ctx->h, interf->bInterfaceNumber);
  950. if (err) {
  951. err = -EBUSY;
  952. razer_error("Failed to detach kernel driver\n");
  953. goto err_close;
  954. }
  955. } else if (err) {
  956. err = -ENODEV;
  957. razer_error("Failed to get kernel driver state\n");
  958. goto err_close;
  959. }
  960. }
  961. tries = 0;
  962. while (1) {
  963. if (tries >= 10) {
  964. razer_error("razer_generic_usb_claim: Failed to claim config\n");
  965. goto err_close;
  966. }
  967. /* Select the correct configuration */
  968. err = libusb_get_configuration(ctx->h, &config);
  969. if (err) {
  970. err = -EBUSY;
  971. razer_error("razer_generic_usb_claim: Failed to get configuration\n");
  972. goto err_close;
  973. }
  974. if (config != ctx->bConfigurationValue) {
  975. err = libusb_set_configuration(ctx->h, ctx->bConfigurationValue);
  976. if (err) {
  977. err = -EBUSY;
  978. razer_error("razer_generic_usb_claim: Failed to set configuration\n");
  979. goto err_close;
  980. }
  981. }
  982. /* And finally claim all interfaces. */
  983. for (i = 0; i < ctx->nr_interfaces; i++) {
  984. interf = &ctx->interfaces[i];
  985. err = libusb_claim_interface(ctx->h, interf->bInterfaceNumber);
  986. if (err) {
  987. err = -EIO;
  988. razer_error("Failed to claim USB interface\n");
  989. goto err_close;
  990. }
  991. err = libusb_set_interface_alt_setting(ctx->h, interf->bInterfaceNumber,
  992. interf->bAlternateSetting);
  993. if (err) {
  994. /* This error triggers on some devices.
  995. * Just print a warning and ignore. */
  996. razer_info("razer_generic_usb_claim: "
  997. "Failed to set alt setting %d on interface %d. "
  998. "Ignoring...\n",
  999. (int)interf->bAlternateSetting,
  1000. (int)interf->bInterfaceNumber);
  1001. }
  1002. }
  1003. /* To make sure there was no race, check config value again */
  1004. err = libusb_get_configuration(ctx->h, &config);
  1005. if (err) {
  1006. err = -EBUSY;
  1007. razer_error("razer_generic_usb_claim: Failed to get configuration\n");
  1008. goto err_close;
  1009. }
  1010. if (config == ctx->bConfigurationValue)
  1011. break;
  1012. razer_msleep(100);
  1013. tries++;
  1014. }
  1015. return 0;
  1016. err_close:
  1017. libusb_close(ctx->h);
  1018. return err;
  1019. }
  1020. int razer_generic_usb_claim_refcount(struct razer_usb_context *ctx,
  1021. unsigned int *refcount)
  1022. {
  1023. int err;
  1024. if (!(*refcount)) {
  1025. err = razer_generic_usb_claim(ctx);
  1026. if (err)
  1027. return err;
  1028. }
  1029. (*refcount)++;
  1030. return 0;
  1031. }
  1032. void razer_generic_usb_release(struct razer_usb_context *ctx)
  1033. {
  1034. int i;
  1035. for (i = ctx->nr_interfaces - 1; i >= 0; i--)
  1036. razer_usb_release(ctx, ctx->interfaces[i].bInterfaceNumber);
  1037. libusb_close(ctx->h);
  1038. }
  1039. void razer_generic_usb_release_refcount(struct razer_usb_context *ctx,
  1040. unsigned int *refcount)
  1041. {
  1042. if (*refcount) {
  1043. (*refcount)--;
  1044. if (!(*refcount))
  1045. razer_generic_usb_release(ctx);
  1046. }
  1047. }
  1048. void razer_generic_usb_gen_idstr(struct libusb_device *udev,
  1049. struct libusb_device_handle *h,
  1050. const char *devname,
  1051. bool include_devicenr,
  1052. const char *serial,
  1053. char *idstr_buf)
  1054. {
  1055. char devid[96];
  1056. char serial_buf[64];
  1057. char buspos[16];
  1058. char c;
  1059. size_t i;
  1060. unsigned int serial_index = 0;
  1061. int err;
  1062. struct libusb_device_descriptor devdesc;
  1063. struct razer_usb_context usbctx = {
  1064. .dev = udev,
  1065. .h = h,
  1066. };
  1067. err = libusb_get_device_descriptor(udev, &devdesc);
  1068. if (err) {
  1069. razer_error("razer_generic_usb_gen_idstr: Failed to get "
  1070. "device descriptor (%d)\n", err);
  1071. return;
  1072. }
  1073. if (serial && strlen(serial)) {
  1074. /* Enforce ASCII characters. */
  1075. for (i = 0; i < ARRAY_SIZE(serial_buf) - 1; i++) {
  1076. c = serial[i];
  1077. if (c == '\0')
  1078. break;
  1079. if ((unsigned char)c <= 0x7Fu)
  1080. serial_buf[i] = c;
  1081. else
  1082. serial_buf[i] = '?'; /* Non-ASCII char. */
  1083. }
  1084. serial_buf[i] = '\0';
  1085. serial = serial_buf;
  1086. } else {
  1087. serial_buf[0] = '\0';
  1088. serial_index = devdesc.iSerialNumber;
  1089. err = -EINVAL;
  1090. if (serial_index) {
  1091. err = 0;
  1092. if (!h)
  1093. err = razer_generic_usb_claim(&usbctx);
  1094. if (err) {
  1095. razer_error("Failed to claim device for serial fetching.\n");
  1096. } else {
  1097. err = libusb_get_string_descriptor_ascii(
  1098. usbctx.h, serial_index,
  1099. (unsigned char *)serial_buf, sizeof(serial_buf));
  1100. if (!h)
  1101. razer_generic_usb_release(&usbctx);
  1102. /* Enforce ASCII characters. */
  1103. for (i = 0; i < ARRAY_SIZE(serial_buf); i++) {
  1104. if (serial_buf[i] == '\0')
  1105. break;
  1106. if ((unsigned char)serial_buf[i] > 0x7Fu)
  1107. serial_buf[i] = '?'; /* Non-ASCII char. */
  1108. }
  1109. }
  1110. }
  1111. if (err <= 0)
  1112. strcpy(serial_buf, "0");
  1113. serial = serial_buf;
  1114. }
  1115. snprintf(devid, sizeof(devid), "%04X-%04X-%s",
  1116. devdesc.idVendor,
  1117. devdesc.idProduct, serial);
  1118. if (include_devicenr) {
  1119. snprintf(buspos, sizeof(buspos), "%03d-%03d",
  1120. libusb_get_bus_number(udev),
  1121. libusb_get_device_address(udev));
  1122. } else {
  1123. snprintf(buspos, sizeof(buspos), "%03d",
  1124. libusb_get_bus_number(udev));
  1125. }
  1126. razer_create_idstr(idstr_buf, BUSTYPESTR_USB, buspos,
  1127. DEVTYPESTR_MOUSE, devname, devid);
  1128. }
  1129. /** razer_usb_force_hub_reset
  1130. * Force reset of the hub the specified device is on
  1131. */
  1132. int razer_usb_force_hub_reset(struct razer_usb_context *device_ctx)
  1133. {
  1134. struct libusb_device_handle *h;
  1135. struct libusb_device *hub = NULL, *dev;
  1136. uint8_t hub_bus_number, hub_device_address;
  1137. struct razer_usb_reconnect_guard rg;
  1138. int err;
  1139. struct libusb_device **devlist;
  1140. ssize_t devlist_size, i;
  1141. razer_debug("Forcing hub reset for device %03u:%03u\n",
  1142. libusb_get_bus_number(device_ctx->dev),
  1143. libusb_get_device_address(device_ctx->dev));
  1144. razer_usb_reconnect_guard_init(&rg, device_ctx);
  1145. hub_bus_number = libusb_get_bus_number(device_ctx->dev);
  1146. hub_device_address = 1; /* Constant */
  1147. devlist_size = libusb_get_device_list(libusb_ctx, &devlist);
  1148. for (i = 0; i < devlist_size; i++) {
  1149. dev = devlist[i];
  1150. if (libusb_get_bus_number(dev) == hub_bus_number &&
  1151. libusb_get_device_address(dev) == hub_device_address) {
  1152. hub = dev;
  1153. break;
  1154. }
  1155. }
  1156. if (!hub) {
  1157. razer_error("razer_usb_force_reinit: Failed to find hub\n");
  1158. err = -ENODEV;
  1159. goto error;
  1160. }
  1161. razer_debug("Resetting root hub %03u:%03u\n",
  1162. hub_bus_number, hub_device_address);
  1163. err = libusb_open(hub, &h);
  1164. if (err) {
  1165. razer_error("razer_usb_force_reinit: Failed to open hub device\n");
  1166. err = -ENODEV;
  1167. goto error;
  1168. }
  1169. libusb_reset_device(h);
  1170. libusb_close(h);
  1171. err = razer_usb_reconnect_guard_wait(&rg, 1);
  1172. if (err) {
  1173. razer_error("razer_usb_force_reinit: "
  1174. "Failed to discover the reconnected device\n");
  1175. goto error;
  1176. }
  1177. razer_debug("Hub reset completed. Device rediscovered as %03u:%03u\n",
  1178. libusb_get_bus_number(device_ctx->dev),
  1179. libusb_get_device_address(device_ctx->dev));
  1180. err = 0;
  1181. error:
  1182. libusb_free_device_list(devlist, 1);
  1183. return err;
  1184. }
  1185. /** razer_usb_reconnect_guard_init - Init the reconnect-guard context
  1186. *
  1187. * Call this _before_ triggering any device operations that might
  1188. * reset the device.
  1189. */
  1190. int razer_usb_reconnect_guard_init(struct razer_usb_reconnect_guard *guard,
  1191. struct razer_usb_context *ctx)
  1192. {
  1193. int err;
  1194. guard->ctx = ctx;
  1195. err = libusb_get_device_descriptor(ctx->dev, &guard->old_desc);
  1196. if (err) {
  1197. razer_error("razer_usb_reconnect_guard_init: Failed to "
  1198. "get device descriptor\n");
  1199. return err;
  1200. }
  1201. guard->old_busnr = libusb_get_bus_number(ctx->dev);
  1202. guard->old_devaddr = libusb_get_device_address(ctx->dev);
  1203. return 0;
  1204. }
  1205. static struct libusb_device * guard_find_usb_dev(const struct libusb_device_descriptor *expected_desc,
  1206. uint8_t expected_bus_number,
  1207. uint8_t expected_dev_addr,
  1208. bool exact_match)
  1209. {
  1210. struct libusb_device **devlist, *dev;
  1211. struct libusb_device_descriptor desc;
  1212. uint8_t dev_addr;
  1213. ssize_t nr_devices, i, j;
  1214. int err;
  1215. nr_devices = libusb_get_device_list(libusb_ctx, &devlist);
  1216. if (nr_devices < 0) {
  1217. razer_error("guard_find_usb_dev: Failed to get device list\n");
  1218. return NULL;
  1219. }
  1220. for (i = 0; i < nr_devices; i++) {
  1221. dev = devlist[i];
  1222. if (libusb_get_bus_number(dev) != expected_bus_number)
  1223. continue;
  1224. err = libusb_get_device_descriptor(dev, &desc);
  1225. if (err)
  1226. continue;
  1227. if (memcmp(&desc, expected_desc, sizeof(desc)) != 0)
  1228. continue;
  1229. dev_addr = libusb_get_device_address(dev);
  1230. if (exact_match) {
  1231. if (dev_addr == expected_dev_addr)
  1232. goto found_dev;
  1233. } else {
  1234. for (j = 0; j < 64; j++) {
  1235. if (dev_addr == ((expected_dev_addr + j) & 0x7F))
  1236. goto found_dev;
  1237. }
  1238. }
  1239. }
  1240. libusb_free_device_list(devlist, 1);
  1241. return NULL;
  1242. found_dev:
  1243. libusb_ref_device(dev);
  1244. libusb_free_device_list(devlist, 1);
  1245. return dev;
  1246. }
  1247. /** razer_usb_reconnect_guard_wait - Protect against a firmware reconnect.
  1248. *
  1249. * If the firmware does a reconnect of the device on the USB bus, this
  1250. * function tries to keep track of the device and it will update the
  1251. * usb context information.
  1252. * Of course, this is not completely race-free, but we try to do our best.
  1253. *
  1254. * hub_reset is true, if the device reconnects due to a HUB reset event.
  1255. * Otherwise it's assumed that the device reconnects on behalf of itself.
  1256. * If hub_reset is false, the device is expected to be claimed.
  1257. */
  1258. int razer_usb_reconnect_guard_wait(struct razer_usb_reconnect_guard *guard, bool hub_reset)
  1259. {
  1260. uint8_t reconn_dev_addr;
  1261. uint8_t old_dev_addr = guard->old_devaddr;
  1262. uint8_t old_bus_number = guard->old_busnr;
  1263. int res, errorcode = 0;
  1264. struct libusb_device *dev;
  1265. struct timeval now, timeout;
  1266. if (!hub_reset) {
  1267. /* Release the device, so the kernel can detect the bus reconnect. */
  1268. razer_generic_usb_release(guard->ctx);
  1269. }
  1270. /* Wait for the device to disconnect. */
  1271. gettimeofday(&timeout, NULL);
  1272. razer_timeval_add_msec(&timeout, 3000);
  1273. while (1) {
  1274. dev = guard_find_usb_dev(&guard->old_desc,
  1275. old_bus_number, old_dev_addr, 1);
  1276. if (!dev)
  1277. break;
  1278. libusb_unref_device(dev);
  1279. gettimeofday(&now, NULL);
  1280. if (razer_timeval_after(&now, &timeout)) {
  1281. /* Timeout. Hm. It seems the device won't reconnect.
  1282. * That's probably OK. Reclaim it. */
  1283. razer_error("razer_usb_reconnect_guard: "
  1284. "The device did not disconnect! If it "
  1285. "does not work anymore, try to replug it.\n");
  1286. goto reclaim;
  1287. }
  1288. razer_msleep(50);
  1289. }
  1290. /* Construct the device address it will reconnect on.
  1291. * On a device reset the new dev addr will be >= reconn_dev_addr.
  1292. */
  1293. reconn_dev_addr = (old_dev_addr + 1) & 0x7F;
  1294. /* Wait for the device to reconnect. */
  1295. gettimeofday(&timeout, NULL);
  1296. razer_timeval_add_msec(&timeout, 3000);
  1297. while (1) {
  1298. dev = guard_find_usb_dev(&guard->old_desc,
  1299. old_bus_number, reconn_dev_addr, 0);
  1300. if (dev)
  1301. break;
  1302. gettimeofday(&now, NULL);
  1303. if (razer_timeval_after(&now, &timeout)) {
  1304. razer_error("razer_usb_reconnect_guard: The device did not "
  1305. "reconnect! It might not work anymore. Try to replug it.\n");
  1306. razer_debug("Expected reconnect busid was: %02u:>=%03u\n",
  1307. old_dev_addr, reconn_dev_addr);
  1308. errorcode = -EBUSY;
  1309. goto out;
  1310. }
  1311. razer_msleep(50);
  1312. }
  1313. /* Update the USB context. */
  1314. libusb_unref_device(guard->ctx->dev);
  1315. guard->ctx->dev = dev;
  1316. reclaim:
  1317. if (!hub_reset) {
  1318. /* Reclaim the new device. */
  1319. res = razer_generic_usb_claim(guard->ctx);
  1320. if (res) {
  1321. razer_error("razer_usb_reconnect_guard: Reclaim failed.\n");
  1322. return res;
  1323. }
  1324. }
  1325. out:
  1326. return errorcode;
  1327. }
  1328. int razer_load_config(const char *path)
  1329. {
  1330. struct config_file *conf = NULL;
  1331. if (!razer_initialized())
  1332. return -EINVAL;
  1333. if (!path)
  1334. path = RAZER_DEFAULT_CONFIG;
  1335. if (strlen(path)) {
  1336. conf = config_file_parse(path, 1);
  1337. if (!conf)
  1338. return -ENOENT;
  1339. }
  1340. config_file_free(razer_config_file);
  1341. razer_config_file = conf;
  1342. return 0;
  1343. }
  1344. void razer_set_logging(razer_logfunc_t info_callback,
  1345. razer_logfunc_t error_callback,
  1346. razer_logfunc_t debug_callback)
  1347. {
  1348. razer_logfunc_info = info_callback;
  1349. razer_logfunc_error = error_callback;
  1350. razer_logfunc_debug = debug_callback;
  1351. }
  1352. static void do_init_axis(struct razer_axis *axis,
  1353. unsigned int id, const char *name, unsigned int flags)
  1354. {
  1355. if (name) {
  1356. axis->id = id;
  1357. axis->name = name;
  1358. axis->flags = flags;
  1359. }
  1360. }
  1361. void razer_init_axes(struct razer_axis *axes,
  1362. const char *name0, unsigned int flags0,
  1363. const char *name1, unsigned int flags1,
  1364. const char *name2, unsigned int flags2)
  1365. {
  1366. do_init_axis(&axes[0], 0, name0, flags0);
  1367. do_init_axis(&axes[1], 1, name1, flags1);
  1368. do_init_axis(&axes[2], 2, name2, flags2);
  1369. }
  1370. struct razer_mouse_dpimapping * razer_mouse_get_dpimapping_by_res(
  1371. struct razer_mouse_dpimapping *mappings, size_t nr_mappings,
  1372. enum razer_dimension dim, enum razer_mouse_res res)
  1373. {
  1374. struct razer_mouse_dpimapping *mapping = NULL;
  1375. size_t i;
  1376. for (i = 0; i < nr_mappings; i++) {
  1377. if (mappings[i].res[dim] == res) {
  1378. mapping = &mappings[i];
  1379. break;
  1380. }
  1381. }
  1382. return mapping;
  1383. }
  1384. void razer_event_spacing_init(struct razer_event_spacing *es,
  1385. unsigned int msec)
  1386. {
  1387. memset(es, 0, sizeof(*es));
  1388. es->spacing_msec = msec;
  1389. }
  1390. void razer_event_spacing_enter(struct razer_event_spacing *es)
  1391. {
  1392. struct timeval now, deadline;
  1393. int wait_msec;
  1394. gettimeofday(&now, NULL);
  1395. deadline = es->last_event;
  1396. razer_timeval_add_msec(&deadline, es->spacing_msec);
  1397. if (razer_timeval_after(&deadline, &now)) {
  1398. /* We have to sleep long enough to ensure we're
  1399. * after the deadline. */
  1400. wait_msec = razer_timeval_msec_diff(&deadline, &now);
  1401. WARN_ON(wait_msec < 0);
  1402. razer_msleep(wait_msec + 1);
  1403. gettimeofday(&now, NULL);
  1404. razer_error_on(razer_timeval_after(&deadline, &now),
  1405. "Failed to maintain event spacing\n");
  1406. }
  1407. }
  1408. void razer_event_spacing_leave(struct razer_event_spacing *es)
  1409. {
  1410. gettimeofday(&es->last_event, NULL);
  1411. }