librazer.c 39 KB

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