driver-gridseed.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /*
  2. * Copyright 2014 Luke Dashjr
  3. * Copyright 2014 Nate Woolls
  4. * Copyright 2014 GridSeed Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the Free
  8. * Software Foundation; either version 3 of the License, or (at your option)
  9. * any later version. See COPYING for more details.
  10. */
  11. #include "config.h"
  12. #include <stdbool.h>
  13. #include "deviceapi.h"
  14. #include "lowlevel.h"
  15. #include "lowl-vcom.h"
  16. #include "gc3355.h"
  17. #define GRIDSEED_DEFAULT_FREQUENCY 600
  18. #define GRIDSEED_HASH_SPEED 0.0851128926 // in ms
  19. BFG_REGISTER_DRIVER(gridseed_drv)
  20. /*
  21. * helper functions
  22. */
  23. static
  24. struct cgpu_info *gridseed_alloc_device(const char *path, struct device_drv *driver, struct gc3355_orb_info *info)
  25. {
  26. struct cgpu_info *device = calloc(1, sizeof(struct cgpu_info));
  27. if (unlikely(!device))
  28. quit(1, "Failed to malloc cgpu_info");
  29. device->drv = driver;
  30. device->device_path = strdup(path);
  31. device->device_fd = -1;
  32. device->threads = 1;
  33. device->device_data = info;
  34. return device;
  35. }
  36. static
  37. struct gc3355_orb_info *gridseed_alloc_info()
  38. {
  39. struct gc3355_orb_info *info = calloc(1, sizeof(struct gc3355_orb_info));
  40. if (unlikely(!info))
  41. quit(1, "Failed to malloc gc3355_orb_info");
  42. info->freq = GRIDSEED_DEFAULT_FREQUENCY;
  43. return info;
  44. }
  45. static
  46. void gridseed_empty_work(int fd)
  47. {
  48. unsigned char buf[GC3355_READ_SIZE];
  49. gc3355_read(fd, (char *)buf, GC3355_READ_SIZE);
  50. }
  51. /*
  52. * device detection
  53. */
  54. static
  55. bool gridseed_detect_custom(const char *path, struct device_drv *driver, struct gc3355_orb_info *info)
  56. {
  57. int fd = gc3355_open(path);
  58. if(fd < 0)
  59. return false;
  60. gridseed_empty_work(fd);
  61. int64_t fw_version = gc3355_get_firmware_version(fd);
  62. if (fw_version == -1)
  63. {
  64. applog(LOG_ERR, "%s: Invalid detect response from %s", gridseed_drv.dname, path);
  65. gc3355_close(fd);
  66. return false;
  67. }
  68. struct cgpu_info *device = gridseed_alloc_device(path, driver, info);
  69. if (serial_claim_v(path, driver))
  70. return false;
  71. if (!add_cgpu(device))
  72. return false;
  73. device->device_fd = fd;
  74. info->chips = GC3355_ORB_DEFAULT_CHIPS;
  75. if((fw_version & 0xffff) == 0x1402)
  76. info->chips = GC3355_BLADE_DEFAULT_CHIPS;
  77. gc3355_init_usborb(device->device_fd, info->freq, false, false);
  78. applog(LOG_INFO, "Found %"PRIpreprv" at %s", device->proc_repr, path);
  79. applog(LOG_DEBUG, "%"PRIpreprv": Init: firmware=%"PRId64", chips=%d", device->proc_repr, fw_version, info->chips);
  80. return true;
  81. }
  82. static
  83. bool gridseed_detect_one(const char *path)
  84. {
  85. struct gc3355_orb_info *info = gridseed_alloc_info();
  86. if (!gridseed_detect_custom(path, &gridseed_drv, info))
  87. {
  88. free(info);
  89. return false;
  90. }
  91. return true;
  92. }
  93. static
  94. bool gridseed_lowl_probe(const struct lowlevel_device_info * const info)
  95. {
  96. return vcom_lowl_probe_wrapper(info, gridseed_detect_one);
  97. }
  98. /*
  99. * setup & shutdown
  100. */
  101. static
  102. bool gridseed_thread_prepare(struct thr_info *thr)
  103. {
  104. thr->cgpu_data = calloc(1, sizeof(*thr->cgpu_data));
  105. struct cgpu_info *device = thr->cgpu;
  106. device->min_nonce_diff = 1./0x10000;
  107. return true;
  108. }
  109. static
  110. void gridseed_thread_shutdown(struct thr_info *thr)
  111. {
  112. struct cgpu_info *device = thr->cgpu;
  113. gc3355_close(device->device_fd);
  114. free(thr->cgpu_data);
  115. }
  116. /*
  117. * scanhash mining loop
  118. */
  119. // send work to the device
  120. static
  121. bool gridseed_prepare_work(struct thr_info __maybe_unused *thr, struct work *work)
  122. {
  123. struct cgpu_info *device = thr->cgpu;
  124. struct gc3355_orb_info *info = device->device_data;
  125. unsigned char cmd[156];
  126. timer_set_now(&info->scanhash_time);
  127. gc3355_scrypt_reset(device->device_fd);
  128. gc3355_scrypt_prepare_work(cmd, work);
  129. // send work
  130. if (sizeof(cmd) != gc3355_write(device->device_fd, cmd, sizeof(cmd)))
  131. {
  132. applog(LOG_ERR, "%s: Failed to send work", device->dev_repr);
  133. return false;
  134. }
  135. return true;
  136. }
  137. static
  138. void gridseed_submit_nonce(struct thr_info * const thr, const unsigned char buf[GC3355_READ_SIZE], struct work * const work)
  139. {
  140. uint32_t nonce = *(uint32_t *)(buf + 4);
  141. nonce = le32toh(nonce);
  142. submit_nonce(thr, work, nonce);
  143. }
  144. static
  145. int64_t gridseed_estimate_hashes(struct thr_info *thr)
  146. {
  147. struct cgpu_info *device = thr->cgpu;
  148. struct gc3355_orb_info *info = device->device_data;
  149. struct timeval old_scanhash_time = info->scanhash_time;
  150. timer_set_now(&info->scanhash_time);
  151. int elapsed_ms = ms_tdiff(&info->scanhash_time, &old_scanhash_time);
  152. return GRIDSEED_HASH_SPEED * (double)elapsed_ms * (double)(info->freq * info->chips);
  153. }
  154. // read from device for nonce or command
  155. static
  156. int64_t gridseed_scanhash(struct thr_info *thr, struct work *work, int64_t __maybe_unused max_nonce)
  157. {
  158. struct cgpu_info *device = thr->cgpu;
  159. unsigned char buf[GC3355_READ_SIZE];
  160. int read = 0;
  161. int fd = device->device_fd;
  162. while (!thr->work_restart && (read = gc3355_read(fd, (char *)buf, GC3355_READ_SIZE)) > 0)
  163. {
  164. if ((buf[0] == 0x55) && (buf[1] == 0x20))
  165. {
  166. // LTC result
  167. gridseed_submit_nonce(thr, buf, work);
  168. break;
  169. }
  170. else
  171. {
  172. applog(LOG_ERR, "%"PRIpreprv": Unrecognized response", device->proc_repr);
  173. break;
  174. }
  175. }
  176. return gridseed_estimate_hashes(thr);
  177. }
  178. /*
  179. * specify settings / options
  180. */
  181. // support for --set-device dualminer:clock=freq
  182. static
  183. char *gridseed_set_device(struct cgpu_info *device, char *option, char *setting, char *replybuf)
  184. {
  185. int val = atoi(setting);
  186. struct gc3355_orb_info *info = device->device_data;
  187. if (strcasecmp(option, "clock") == 0)
  188. {
  189. info->freq = val;
  190. gc3355_set_pll_freq(device->device_fd, val);
  191. return NULL;
  192. }
  193. if (strcasecmp(option, "chips") == 0)
  194. {
  195. info->chips = val;
  196. return NULL;
  197. }
  198. sprintf(replybuf, "Unknown option: %s", option);
  199. return replybuf;
  200. }
  201. struct device_drv gridseed_drv =
  202. {
  203. // metadata
  204. .dname = "gridseed",
  205. .name = "GSD",
  206. .supported_algos = POW_SCRYPT,
  207. // detect device
  208. .lowl_probe = gridseed_lowl_probe,
  209. // initialize device
  210. .thread_prepare = gridseed_thread_prepare,
  211. // specify mining type - scanhash
  212. .minerloop = minerloop_scanhash,
  213. // scanhash mining hooks
  214. .prepare_work = gridseed_prepare_work,
  215. .scanhash = gridseed_scanhash,
  216. // teardown device
  217. .thread_shutdown = gridseed_thread_shutdown,
  218. // specify settings / options
  219. .set_device = gridseed_set_device,
  220. };