Browse Source

Style police on libztex.c.

nfactor-troky
Con Kolivas 13 years ago
parent
commit
bd46119c11
  1. 94
      libztex.c

94
libztex.c

@ -44,10 +44,11 @@
static bool libztex_checkDevice (struct libusb_device *dev) { static bool libztex_checkDevice(struct libusb_device *dev)
{
struct libusb_device_descriptor desc;
int err; int err;
struct libusb_device_descriptor desc;
err = libusb_get_device_descriptor(dev, &desc); err = libusb_get_device_descriptor(dev, &desc);
if (unlikely(err != 0)) { if (unlikely(err != 0)) {
applog(LOG_ERR, "Ztex check device: Failed to open read descriptor with error %d", err); applog(LOG_ERR, "Ztex check device: Failed to open read descriptor with error %d", err);
@ -60,17 +61,19 @@ static bool libztex_checkDevice (struct libusb_device *dev) {
return true; return true;
} }
static bool libztex_checkCapability (struct libztex_device *ztex, int i, int j) { static bool libztex_checkCapability(struct libztex_device *ztex, int i, int j)
{
if (!((i >= 0) && (i <= 5) && (j >= 0) && (j < 8) && if (!((i >= 0) && (i <= 5) && (j >= 0) && (j < 8) &&
(((ztex->interfaceCapabilities[i] & 255) & (1 << j)) != 0))) (((ztex->interfaceCapabilities[i] & 255) & (1 << j)) != 0)))
applog(LOG_ERR, "%s: capability missing: %d %d", ztex->repr, i, i); applog(LOG_ERR, "%s: capability missing: %d %d", ztex->repr, i, i);
return true; return true;
} }
static int libztex_detectBitstreamBitOrder (const unsigned char *buf, int size) { static int libztex_detectBitstreamBitOrder(const unsigned char *buf, int size)
{
int i; int i;
size -= 4;
for (i=0; i<size; i++) { for (i = 0; i < size - 4; i++) {
if (((buf[i] & 255) == 0xaa) && ((buf[i + 1] & 255) == 0x99) && ((buf[i + 2] & 255) == 0x55) && ((buf[i + 3] & 255) == 0x66)) if (((buf[i] & 255) == 0xaa) && ((buf[i + 1] & 255) == 0x99) && ((buf[i + 2] & 255) == 0x55) && ((buf[i + 3] & 255) == 0x66))
return 1; return 1;
if (((buf[i] & 255) == 0x55) && ((buf[i + 1] & 255) == 0x99) && ((buf[i + 2] & 255) == 0xaa) && ((buf[i + 3] & 255) == 0x66)) if (((buf[i] & 255) == 0x55) && ((buf[i + 1] & 255) == 0x99) && ((buf[i + 2] & 255) == 0xaa) && ((buf[i + 3] & 255) == 0x66))
@ -80,9 +83,11 @@ static int libztex_detectBitstreamBitOrder (const unsigned char *buf, int size)
return 0; return 0;
} }
static void libztex_swapBits (unsigned char *buf, int size) { static void libztex_swapBits(unsigned char *buf, int size)
int i; {
unsigned char c; unsigned char c;
int i;
for (i = 0; i < size; i++) { for (i = 0; i < size; i++) {
c = buf[i]; c = buf[i];
buf[i] = ((c & 128) >> 7) | buf[i] = ((c & 128) >> 7) |
@ -96,9 +101,11 @@ static void libztex_swapBits (unsigned char *buf, int size) {
} }
} }
static int libztex_getFpgaState (struct libztex_device *ztex, struct libztex_fpgastate *state) { static int libztex_getFpgaState(struct libztex_device *ztex, struct libztex_fpgastate *state)
int cnt; {
unsigned char buf[9]; unsigned char buf[9];
int cnt;
if (!libztex_checkCapability(ztex, CAPABILITY_FPGA)) if (!libztex_checkCapability(ztex, CAPABILITY_FPGA))
return -1; return -1;
cnt = libusb_control_transfer(ztex->hndl, 0xc0, 0x30, 0, 0, buf, 9, 1000); cnt = libusb_control_transfer(ztex->hndl, 0xc0, 0x30, 0, 0, buf, 9, 1000);
@ -115,12 +122,13 @@ static int libztex_getFpgaState (struct libztex_device *ztex, struct libztex_fpg
return 0; return 0;
} }
static int libztex_configureFpgaLS (struct libztex_device *ztex, const char* firmware, bool force, char bs) { static int libztex_configureFpgaLS(struct libztex_device *ztex, const char* firmware, bool force, char bs)
{
struct libztex_fpgastate state; struct libztex_fpgastate state;
ssize_t pos=0; const int transactionBytes = 2048;
int transactionBytes = 2048;
unsigned char buf[transactionBytes], cs; unsigned char buf[transactionBytes], cs;
int tries, cnt, buf_p, i; int tries, cnt, buf_p, i;
ssize_t pos = 0;
FILE *fp; FILE *fp;
if (!libztex_checkCapability(ztex, CAPABILITY_FPGA)) if (!libztex_checkCapability(ztex, CAPABILITY_FPGA))
@ -133,7 +141,6 @@ static int libztex_configureFpgaLS (struct libztex_device *ztex, const char* fir
} }
for (tries = 10; tries > 0; tries--) { for (tries = 10; tries > 0; tries--) {
fp = fopen(firmware, "rb"); fp = fopen(firmware, "rb");
if (!fp) { if (!fp) {
applog(LOG_ERR, "%s: failed to read firmware '%s'", ztex->repr, firmware); applog(LOG_ERR, "%s: failed to read firmware '%s'", ztex->repr, firmware);
@ -144,7 +151,8 @@ static int libztex_configureFpgaLS (struct libztex_device *ztex, const char* fir
while (pos < transactionBytes && !feof(fp)) { while (pos < transactionBytes && !feof(fp)) {
buf[pos] = getc(fp); buf[pos] = getc(fp);
cs += buf[pos++]; cs += buf[pos++];
}; }
if (feof(fp)) if (feof(fp))
pos--; pos--;
@ -178,7 +186,7 @@ static int libztex_configureFpgaLS (struct libztex_device *ztex, const char* fir
while (buf_p < transactionBytes && !feof(fp)) { while (buf_p < transactionBytes && !feof(fp)) {
buf[buf_p] = getc(fp); buf[buf_p] = getc(fp);
cs += buf[buf_p++]; cs += buf[buf_p++];
}; }
if (feof(fp)) if (feof(fp))
buf_p--; buf_p--;
pos += buf_p; pos += buf_p;
@ -202,19 +210,21 @@ static int libztex_configureFpgaLS (struct libztex_device *ztex, const char* fir
return 0; return 0;
} }
int libztex_configureFpga (struct libztex_device *ztex) { int libztex_configureFpga(struct libztex_device *ztex)
int rv; {
char buf[256] = "bitstreams/"; char buf[256] = "bitstreams/";
memset(&buf[11], 0, 245); memset(&buf[11], 0, 245);
strcpy(&buf[11], ztex->bitFileName); strcpy(&buf[11], ztex->bitFileName);
strcpy(&buf[strlen(buf)], ".bit"); strcpy(&buf[strlen(buf)], ".bit");
rv = libztex_configureFpgaLS(ztex, buf, true, 2); return libztex_configureFpgaLS(ztex, buf, true, 2);
return rv;
} }
int libztex_setFreq (struct libztex_device *ztex, uint16_t freq) { int libztex_setFreq(struct libztex_device *ztex, uint16_t freq)
{
int cnt; int cnt;
if (freq > ztex->freqMaxM) if (freq > ztex->freqMaxM)
freq = ztex->freqMaxM; freq = ztex->freqMaxM;
@ -229,19 +239,21 @@ int libztex_setFreq (struct libztex_device *ztex, uint16_t freq) {
return 0; return 0;
} }
int libztex_resetFpga (struct libztex_device *ztex) { int libztex_resetFpga(struct libztex_device *ztex)
{
return libusb_control_transfer(ztex->hndl, 0x40, 0x31, 0, 0, NULL, 0, 1000); return libusb_control_transfer(ztex->hndl, 0x40, 0x31, 0, 0, NULL, 0, 1000);
} }
int libztex_prepare_device (struct libusb_device *dev, struct libztex_device** ztex) { int libztex_prepare_device(struct libusb_device *dev, struct libztex_device** ztex)
{
struct libztex_device *newdev; struct libztex_device *newdev;
int cnt, err;
unsigned char buf[64]; unsigned char buf[64];
int cnt, err;
newdev = malloc(sizeof(struct libztex_device)); newdev = malloc(sizeof(struct libztex_device));
newdev->bitFileName = NULL;
newdev->valid = false; newdev->valid = false;
newdev->hndl = NULL; newdev->hndl = NULL;
newdev->bitFileName = NULL;
*ztex = newdev; *ztex = newdev;
err = libusb_get_device_descriptor(dev, &newdev->descriptor); err = libusb_get_device_descriptor(dev, &newdev->descriptor);
@ -346,7 +358,8 @@ int libztex_prepare_device (struct libusb_device *dev, struct libztex_device** z
return 0; return 0;
} }
void libztex_destroy_device (struct libztex_device* ztex) { void libztex_destroy_device(struct libztex_device* ztex)
{
if (ztex->hndl != NULL) { if (ztex->hndl != NULL) {
libusb_close(ztex->hndl); libusb_close(ztex->hndl);
ztex->hndl = NULL; ztex->hndl = NULL;
@ -358,20 +371,21 @@ void libztex_destroy_device (struct libztex_device* ztex) {
free(ztex); free(ztex);
} }
int libztex_scanDevices (struct libztex_dev_list*** devs_p) { int libztex_scanDevices(struct libztex_dev_list*** devs_p)
libusb_device **list; {
int usbdevices[LIBZTEX_MAX_DESCRIPTORS];
struct libztex_dev_list **devs;
struct libztex_device *ztex; struct libztex_device *ztex;
ssize_t cnt = libusb_get_device_list(NULL, &list);
ssize_t i = 0;
int found = 0, pos = 0, err; int found = 0, pos = 0, err;
libusb_device **list;
ssize_t cnt, i = 0;
cnt = libusb_get_device_list(NULL, &list);
if (unlikely(cnt < 0)) { if (unlikely(cnt < 0)) {
applog(LOG_ERR, "Ztex scan devices: Failed to list usb devices with err %d", cnt); applog(LOG_ERR, "Ztex scan devices: Failed to list usb devices with err %d", cnt);
return 0; return 0;
} }
int usbdevices[LIBZTEX_MAX_DESCRIPTORS];
for (i = 0; i < cnt; i++) { for (i = 0; i < cnt; i++) {
if (libztex_checkDevice(list[i])) { if (libztex_checkDevice(list[i])) {
// Got one! // Got one!
@ -380,7 +394,6 @@ int libztex_scanDevices (struct libztex_dev_list*** devs_p) {
} }
} }
struct libztex_dev_list **devs;
devs = malloc(sizeof(struct libztex_dev_list *) * found); devs = malloc(sizeof(struct libztex_dev_list *) * found);
if (devs == NULL) { if (devs == NULL) {
applog(LOG_ERR, "Ztex scan devices: Failed to allocate memory"); applog(LOG_ERR, "Ztex scan devices: Failed to allocate memory");
@ -409,8 +422,10 @@ int libztex_scanDevices (struct libztex_dev_list*** devs_p) {
return pos; return pos;
} }
int libztex_sendHashData (struct libztex_device *ztex, unsigned char *sendbuf) { int libztex_sendHashData(struct libztex_device *ztex, unsigned char *sendbuf)
{
int cnt; int cnt;
if (ztex == NULL || ztex->hndl == NULL) if (ztex == NULL || ztex->hndl == NULL)
return 0; return 0;
cnt = libusb_control_transfer(ztex->hndl, 0x40, 0x80, 0, 0, sendbuf, 44, 1000); cnt = libusb_control_transfer(ztex->hndl, 0x40, 0x80, 0, 0, sendbuf, 44, 1000);
@ -420,7 +435,8 @@ int libztex_sendHashData (struct libztex_device *ztex, unsigned char *sendbuf) {
return cnt; return cnt;
} }
int libztex_readHashData (struct libztex_device *ztex, struct libztex_hash_data nonces[]) { int libztex_readHashData(struct libztex_device *ztex, struct libztex_hash_data nonces[])
{
// length of buf must be 8 * (numNonces + 1) // length of buf must be 8 * (numNonces + 1)
unsigned char rbuf[12 * 8]; unsigned char rbuf[12 * 8];
int cnt, i; int cnt, i;
@ -445,9 +461,11 @@ int libztex_readHashData (struct libztex_device *ztex, struct libztex_hash_data
return cnt; return cnt;
} }
void libztex_freeDevList (struct libztex_dev_list **devs) { void libztex_freeDevList(struct libztex_dev_list **devs)
ssize_t cnt = 0; {
bool done = false; bool done = false;
ssize_t cnt = 0;
while (!done) { while (!done) {
if (devs[cnt]->next == NULL) if (devs[cnt]->next == NULL)
done = true; done = true;
@ -455,5 +473,3 @@ void libztex_freeDevList (struct libztex_dev_list **devs) {
} }
free(devs); free(devs);
} }

Loading…
Cancel
Save