driver-hashfast.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. * Copyright 2013 Con Kolivas <kernel@kolivas.org>
  3. * Copyright 2013 Hashfast Inc.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the Free
  7. * Software Foundation; either version 3 of the License, or (at your option)
  8. * any later version. See COPYING for more details.
  9. */
  10. #include "config.h"
  11. #include <stdbool.h>
  12. #include "usbutils.h"
  13. #include "driver-hashfast.h"
  14. ////////////////////////////////////////////////////////////////////////////////
  15. // Support for the CRC's used in header (CRC-8) and packet body (CRC-32)
  16. ////////////////////////////////////////////////////////////////////////////////
  17. #define GP8 0x107 /* x^8 + x^2 + x + 1 */
  18. #define DI8 0x07
  19. static unsigned char crc8_table[256]; /* CRC-8 table */
  20. static void hf_init_crc8(void)
  21. {
  22. int i,j;
  23. unsigned char crc;
  24. for (i = 0; i < 256; i++) {
  25. crc = i;
  26. for (j = 0; j < 8; j++)
  27. crc = (crc << 1) ^ ((crc & 0x80) ? DI8 : 0);
  28. crc8_table[i] = crc & 0xFF;
  29. }
  30. }
  31. static unsigned char __maybe_unused hf_crc8(unsigned char *h)
  32. {
  33. int i;
  34. unsigned char crc;
  35. h++; // Preamble not included
  36. for (i = 1, crc = 0xff; i < 7; i++)
  37. crc = crc8_table[crc ^ *h++];
  38. return crc;
  39. }
  40. struct hf_cmd {
  41. int cmd;
  42. char *cmd_name;
  43. enum usb_cmds usb_cmd;
  44. };
  45. #define C_NULL C_MAX
  46. static const struct hf_cmd hf_cmds[] = {
  47. {OP_NULL, "OP_NULL", C_NULL},
  48. {OP_ROOT, "OP_ROOT", C_NULL},
  49. {OP_RESET, "OP_RESET", C_HF_RESET},
  50. {OP_PLL_CONFIG, "OP_PLL_CONFIG", C_HF_PLL_CONFIG},
  51. {OP_ADDRESS, "OP_ADDRESS", C_HF_ADDRESS},
  52. {OP_READDRESS, "OP_READDRESS", C_NULL},
  53. {OP_HIGHEST, "OP_HIGHEST", C_NULL},
  54. {OP_BAUD, "OP_BAUD", C_HF_BAUD},
  55. {OP_UNROOT, "OP_UNROOT", C_NULL},
  56. {OP_HASH, "OP_HASH", C_HF_HASH},
  57. {OP_NONCE, "OP_NONCE", C_HF_NONCE},
  58. {OP_ABORT, "OP_ABORT", C_HF_ABORT},
  59. {OP_STATUS, "OP_STATUS", C_HF_STATUS},
  60. {OP_GPIO, "OP_GPIO", C_NULL},
  61. {OP_CONFIG, "OP_CONFIG", C_HF_CONFIG},
  62. {OP_STATISTICS, "OP_STATISTICS", C_HF_STATISTICS},
  63. {OP_GROUP, "OP_GROUP", C_NULL},
  64. {OP_CLOCKGATE, "OP_CLOCKGATE", C_HF_CLOCKGATE}
  65. };
  66. /* Send an arbitrary frame, consisting of an 8 byte header and an optional
  67. * packet body. */
  68. static int __maybe_unused hashfast_send_frame(struct cgpu_info *hashfast, uint8_t opcode,
  69. uint8_t chip, uint8_t core, uint16_t hdata,
  70. uint8_t *data, int len)
  71. {
  72. int tx_length, ret, amount, id = hashfast->device_id;
  73. uint8_t packet[256];
  74. struct hf_header *p = (struct hf_header *)packet;
  75. p->preamble = HF_PREAMBLE;
  76. p->operation_code = opcode;
  77. p->chip_address = chip;
  78. p->core_address = core;
  79. p->hdata = htole16(hdata);
  80. p->data_length = len / 4;
  81. p->crc8 = hf_crc8(packet);
  82. if (len)
  83. memcpy(&packet[sizeof(struct hf_header)], data, len);
  84. tx_length = sizeof(struct hf_header) + len;
  85. tx_length = sizeof(struct hf_header);
  86. ret = usb_write(hashfast, (char *)packet, tx_length, &amount,
  87. hf_cmds[opcode].usb_cmd);
  88. if (ret < 0 || amount != tx_length) {
  89. applog(LOG_ERR, "HF%d: hashfast_send_frame: USB Send error, ret %d amount %d vs. tx_length %d",
  90. id, ret, amount, tx_length);
  91. return 1;
  92. }
  93. return 0;
  94. }
  95. static int __maybe_unused hashfast_reset(struct cgpu_info __maybe_unused *hashfast)
  96. {
  97. return 0;
  98. }
  99. static bool hashfast_detect_common(struct cgpu_info __maybe_unused *hashfast)
  100. {
  101. return true;
  102. }
  103. static void hashfast_usb_initialise(struct cgpu_info *hashfast)
  104. {
  105. if (hashfast->usbinfo.nodev)
  106. return;
  107. // FIXME Do necessary initialising here
  108. }
  109. static bool hashfast_detect_one_usb(libusb_device *dev, struct usb_find_devices *found)
  110. {
  111. struct cgpu_info *hashfast;
  112. hashfast = usb_alloc_cgpu(&hashfast_drv, HASHFAST_MINER_THREADS);
  113. if (!hashfast)
  114. return false;
  115. if (!usb_init(hashfast, dev, found)) {
  116. free(hashfast->device_data);
  117. hashfast->device_data = NULL;
  118. hashfast = usb_free_cgpu(hashfast);
  119. return false;
  120. }
  121. hashfast->usbdev->usb_type = USB_TYPE_STD;
  122. hashfast_usb_initialise(hashfast);
  123. add_cgpu(hashfast);
  124. return hashfast_detect_common(hashfast);
  125. }
  126. static void hashfast_detect(bool hotplug)
  127. {
  128. /* Set up the CRC tables only once. */
  129. if (!hotplug)
  130. hf_init_crc8();
  131. usb_detect(&hashfast_drv, hashfast_detect_one_usb);
  132. }
  133. static bool hashfast_prepare(struct thr_info __maybe_unused *thr)
  134. {
  135. return true;
  136. }
  137. static int64_t hashfast_scanwork(struct thr_info __maybe_unused *thr)
  138. {
  139. return 0;
  140. }
  141. static struct api_data *hashfast_api_stats(struct cgpu_info __maybe_unused *cgpu)
  142. {
  143. return NULL;
  144. }
  145. static void hashfast_init(struct cgpu_info *hashfast)
  146. {
  147. usb_buffer_enable(hashfast);
  148. }
  149. static void hashfast_shutdown(struct thr_info __maybe_unused *thr)
  150. {
  151. }
  152. struct device_drv hashfast_drv = {
  153. .drv_id = DRIVER_hashfast,
  154. .dname = "Hashfast",
  155. .name = "HFA",
  156. .drv_detect = hashfast_detect,
  157. .thread_prepare = hashfast_prepare,
  158. .hash_work = &hash_driver_work,
  159. .scanwork = hashfast_scanwork,
  160. .get_api_stats = hashfast_api_stats,
  161. .reinit_device = hashfast_init,
  162. .thread_shutdown = hashfast_shutdown,
  163. };