Style police on libztex.c.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
diff --git a/libztex.c b/libztex.c
index 28716d6..c6bd6aa 100644
--- a/libztex.c
+++ b/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;
- struct libusb_device_descriptor desc;
err = libusb_get_device_descriptor(dev, &desc);
if (unlikely(err != 0)) {
applog(LOG_ERR, "Ztex check device: Failed to open read descriptor with error %d", err);
@@ -60,30 +61,34 @@ static bool libztex_checkDevice (struct libusb_device *dev) {
return true;
}
-static bool libztex_checkCapability (struct libztex_device *ztex, int i, int j) {
- if (!((i>=0) && (i<=5) && (j>=0) && (j<8) &&
+static bool libztex_checkCapability(struct libztex_device *ztex, int i, int j)
+{
+ if (!((i >= 0) && (i <= 5) && (j >= 0) && (j < 8) &&
(((ztex->interfaceCapabilities[i] & 255) & (1 << j)) != 0)))
applog(LOG_ERR, "%s: capability missing: %d %d", ztex->repr, i, i);
return true;
}
-static int libztex_detectBitstreamBitOrder (const unsigned char *buf, int size) {
+static int libztex_detectBitstreamBitOrder(const unsigned char *buf, int size)
+{
int i;
- size -= 4;
- for (i=0; i<size; i++) {
- if ( ((buf[i] & 255)==0xaa) && ((buf[i+1] & 255)==0x99) && ((buf[i+2] & 255)==0x55) && ((buf[i+3] & 255)==0x66) )
+
+ 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))
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))
return 0;
}
applog(LOG_WARNING, "Unable to determine bitstream bit order: no signature found");
return 0;
}
-static void libztex_swapBits (unsigned char *buf, int size) {
- int i;
+static void libztex_swapBits(unsigned char *buf, int size)
+{
unsigned char c;
- for (i=0; i<size; i++) {
+ int i;
+
+ for (i = 0; i < size; i++) {
c = buf[i];
buf[i] = ((c & 128) >> 7) |
((c & 64) >> 5) |
@@ -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) {
- int cnt;
+static int libztex_getFpgaState(struct libztex_device *ztex, struct libztex_fpgastate *state)
+{
unsigned char buf[9];
+ int cnt;
+
if (!libztex_checkCapability(ztex, CAPABILITY_FPGA))
return -1;
cnt = libusb_control_transfer(ztex->hndl, 0xc0, 0x30, 0, 0, buf, 9, 1000);
@@ -108,19 +115,20 @@ static int libztex_getFpgaState (struct libztex_device *ztex, struct libztex_fpg
}
state->fpgaConfigured = (buf[0] == 0);
state->fpgaChecksum = buf[1] & 0xff;
- state->fpgaBytes = ((buf[5] & 0xff)<<24) | ((buf[4] & 0xff)<<16) | ((buf[3] & 0xff)<<8) | (buf[2] & 0xff);
+ state->fpgaBytes = ((buf[5] & 0xff) << 24) | ((buf[4] & 0xff) << 16) | ((buf[3] & 0xff) << 8) | (buf[2] & 0xff);
state->fpgaInitB = buf[6] & 0xff;
state->fpgaFlashResult = buf[7];
state->fpgaFlashBitSwap = (buf[8] != 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;
- ssize_t pos=0;
- int transactionBytes = 2048;
+ const int transactionBytes = 2048;
unsigned char buf[transactionBytes], cs;
int tries, cnt, buf_p, i;
+ ssize_t pos = 0;
FILE *fp;
if (!libztex_checkCapability(ztex, CAPABILITY_FPGA))
@@ -132,8 +140,7 @@ static int libztex_configureFpgaLS (struct libztex_device *ztex, const char* fir
return 1;
}
- for (tries=10; tries>0; tries--) {
-
+ for (tries = 10; tries > 0; tries--) {
fp = fopen(firmware, "rb");
if (!fp) {
applog(LOG_ERR, "%s: failed to read firmware '%s'", ztex->repr, firmware);
@@ -144,12 +151,13 @@ static int libztex_configureFpgaLS (struct libztex_device *ztex, const char* fir
while (pos < transactionBytes && !feof(fp)) {
buf[pos] = getc(fp);
cs += buf[pos++];
- };
+ }
+
if (feof(fp))
pos--;
- if ( bs != 0 && bs != 1 )
- bs = libztex_detectBitstreamBitOrder(buf, transactionBytes<pos ? transactionBytes : pos);
+ if (bs != 0 && bs != 1)
+ bs = libztex_detectBitstreamBitOrder(buf, transactionBytes < pos? transactionBytes: pos);
//* Reset fpga
cnt = libztex_resetFpga(ztex);
@@ -158,14 +166,14 @@ static int libztex_configureFpgaLS (struct libztex_device *ztex, const char* fir
continue;
}
- if ( bs == 1 )
+ if (bs == 1)
libztex_swapBits(buf, pos);
buf_p = pos;
while (1) {
i = 0;
while (i < buf_p) {
- cnt = libusb_control_transfer(ztex->hndl, 0x40, 0x32, 0, 0, &buf[i], buf_p-i, 5000);
+ cnt = libusb_control_transfer(ztex->hndl, 0x40, 0x32, 0, 0, &buf[i], buf_p - i, 5000);
if (unlikely(cnt < 0)) {
applog(LOG_ERR, "%s: Failed send fpga data with err %d", ztex->repr, cnt);
break;
@@ -178,13 +186,13 @@ static int libztex_configureFpgaLS (struct libztex_device *ztex, const char* fir
while (buf_p < transactionBytes && !feof(fp)) {
buf[buf_p] = getc(fp);
cs += buf[buf_p++];
- };
+ }
if (feof(fp))
buf_p--;
pos += buf_p;
if (buf_p == 0)
break;
- if ( bs == 1 )
+ if (bs == 1)
libztex_swapBits(buf, buf_p);
}
if (cnt >= 0)
@@ -202,19 +210,21 @@ static int libztex_configureFpgaLS (struct libztex_device *ztex, const char* fir
return 0;
}
-int libztex_configureFpga (struct libztex_device *ztex) {
- int rv;
+int libztex_configureFpga(struct libztex_device *ztex)
+{
char buf[256] = "bitstreams/";
+
memset(&buf[11], 0, 245);
strcpy(&buf[11], ztex->bitFileName);
strcpy(&buf[strlen(buf)], ".bit");
- rv = libztex_configureFpgaLS(ztex, buf, true, 2);
- return rv;
+ return libztex_configureFpgaLS(ztex, buf, true, 2);
}
-int libztex_setFreq (struct libztex_device *ztex, uint16_t freq) {
+int libztex_setFreq(struct libztex_device *ztex, uint16_t freq)
+{
int cnt;
+
if (freq > ztex->freqMaxM)
freq = ztex->freqMaxM;
@@ -229,19 +239,21 @@ int libztex_setFreq (struct libztex_device *ztex, uint16_t freq) {
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);
}
-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;
- int cnt, err;
unsigned char buf[64];
+ int cnt, err;
newdev = malloc(sizeof(struct libztex_device));
+ newdev->bitFileName = NULL;
newdev->valid = false;
newdev->hndl = NULL;
- newdev->bitFileName = NULL;
*ztex = newdev;
err = libusb_get_device_descriptor(dev, &newdev->descriptor);
@@ -264,7 +276,7 @@ int libztex_prepare_device (struct libusb_device *dev, struct libztex_device** z
}
cnt = libusb_get_string_descriptor_ascii (newdev->hndl, newdev->descriptor.iSerialNumber, newdev->snString,
- LIBZTEX_SNSTRING_LEN+1);
+ LIBZTEX_SNSTRING_LEN + 1);
if (unlikely(cnt < 0)) {
applog(LOG_ERR, "Ztex check device: Failed to read device snString with err %d", cnt);
return cnt;
@@ -276,7 +288,7 @@ int libztex_prepare_device (struct libusb_device *dev, struct libztex_device** z
return cnt;
}
- if ( buf[0]!=40 || buf[1]!=1 || buf[2]!='Z' || buf[3]!='T' || buf[4]!='E' || buf[5]!='X' ) {
+ if ( buf[0] != 40 || buf[1] != 1 || buf[2] != 'Z' || buf[3] != 'T' || buf[4] != 'E' || buf[5] != 'X' ) {
applog(LOG_ERR, "Ztex check device: Error reading ztex descriptor");
return 2;
}
@@ -323,21 +335,21 @@ int libztex_prepare_device (struct libusb_device *dev, struct libztex_device** z
newdev->numNonces = buf[1] + 1;
newdev->offsNonces = ((buf[2] & 255) | ((buf[3] & 255) << 8)) - 10000;
- newdev->freqM1 = ( (buf[4] & 255) | ((buf[5] & 255) << 8) ) * 0.01;
+ newdev->freqM1 = ((buf[4] & 255) | ((buf[5] & 255) << 8) ) * 0.01;
newdev->freqMaxM = (buf[7] & 255);
newdev->freqM = (buf[6] & 255);
newdev->freqMDefault = newdev->freqM;
- for (cnt=0; cnt<255; cnt++) {
+ for (cnt=0; cnt < 255; cnt++) {
newdev->errorCount[cnt] = 0;
newdev->errorWeight[cnt] = 0;
newdev->errorRate[cnt] = 0;
newdev->maxErrorRate[cnt] = 0;
}
- cnt = strlen((char *)&buf[buf[0]==4?10:8]);
- newdev->bitFileName = malloc(sizeof(char)*(cnt+1));
- memcpy(newdev->bitFileName, &buf[buf[0]==4?10:8], cnt+1);
+ cnt = strlen((char *)&buf[buf[0] == 4? 10: 8]);
+ newdev->bitFileName = malloc(sizeof(char) * (cnt + 1));
+ memcpy(newdev->bitFileName, &buf[buf[0] == 4? 10: 8], cnt + 1);
newdev->usbbus = libusb_get_bus_number(dev);
newdev->usbaddress = libusb_get_device_address(dev);
@@ -346,7 +358,8 @@ int libztex_prepare_device (struct libusb_device *dev, struct libztex_device** z
return 0;
}
-void libztex_destroy_device (struct libztex_device* ztex) {
+void libztex_destroy_device(struct libztex_device* ztex)
+{
if (ztex->hndl != NULL) {
libusb_close(ztex->hndl);
ztex->hndl = NULL;
@@ -358,20 +371,21 @@ void libztex_destroy_device (struct libztex_device* ztex) {
free(ztex);
}
-int libztex_scanDevices (struct libztex_dev_list*** devs_p) {
- libusb_device **list;
+int libztex_scanDevices(struct libztex_dev_list*** devs_p)
+{
+ int usbdevices[LIBZTEX_MAX_DESCRIPTORS];
+ struct libztex_dev_list **devs;
struct libztex_device *ztex;
- ssize_t cnt = libusb_get_device_list(NULL, &list);
- ssize_t i = 0;
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)) {
applog(LOG_ERR, "Ztex scan devices: Failed to list usb devices with err %d", cnt);
return 0;
}
- int usbdevices[LIBZTEX_MAX_DESCRIPTORS];
-
for (i = 0; i < cnt; i++) {
if (libztex_checkDevice(list[i])) {
// 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);
if (devs == NULL) {
applog(LOG_ERR, "Ztex scan devices: Failed to allocate memory");
@@ -400,7 +413,7 @@ int libztex_scanDevices (struct libztex_dev_list*** devs_p) {
devs[pos]->dev = ztex;
devs[pos]->next = NULL;
if (pos > 0)
- devs[pos-1]->next = devs[pos];
+ devs[pos - 1]->next = devs[pos];
pos++;
}
@@ -409,8 +422,10 @@ int libztex_scanDevices (struct libztex_dev_list*** devs_p) {
return pos;
}
-int libztex_sendHashData (struct libztex_device *ztex, unsigned char *sendbuf) {
+int libztex_sendHashData(struct libztex_device *ztex, unsigned char *sendbuf)
+{
int cnt;
+
if (ztex == NULL || ztex->hndl == NULL)
return 0;
cnt = libusb_control_transfer(ztex->hndl, 0x40, 0x80, 0, 0, sendbuf, 44, 1000);
@@ -420,34 +435,37 @@ int libztex_sendHashData (struct libztex_device *ztex, unsigned char *sendbuf) {
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)
- unsigned char rbuf[12*8];
+ unsigned char rbuf[12 * 8];
int cnt, i;
if (ztex->hndl == NULL)
return 0;
- cnt = libusb_control_transfer(ztex->hndl, 0xc0, 0x81, 0, 0, rbuf, 12*ztex->numNonces, 1000);
+ cnt = libusb_control_transfer(ztex->hndl, 0xc0, 0x81, 0, 0, rbuf, 12 * ztex->numNonces, 1000);
if (unlikely(cnt < 0)) {
applog(LOG_ERR, "%s: Failed readHashData with err %d", ztex->repr, cnt);
return cnt;
}
- for (i=0; i<ztex->numNonces; i++) {
- memcpy((char*)&nonces[i].goldenNonce, &rbuf[i*12], 4);
+ for (i = 0; i < ztex->numNonces; i++) {
+ memcpy((char*)&nonces[i].goldenNonce, &rbuf[i * 12], 4);
nonces[i].goldenNonce -= ztex->offsNonces;
- memcpy((char*)&nonces[i].nonce, &rbuf[(i*12)+4], 4);
+ memcpy((char*)&nonces[i].nonce, &rbuf[(i * 12) + 4], 4);
nonces[i].nonce -= ztex->offsNonces;
- memcpy((char*)&nonces[i].hash7, &rbuf[(i*12)+8], 4);
+ memcpy((char*)&nonces[i].hash7, &rbuf[(i * 12) + 8], 4);
}
return cnt;
}
-void libztex_freeDevList (struct libztex_dev_list **devs) {
- ssize_t cnt = 0;
+void libztex_freeDevList(struct libztex_dev_list **devs)
+{
bool done = false;
+ ssize_t cnt = 0;
+
while (!done) {
if (devs[cnt]->next == NULL)
done = true;
@@ -455,5 +473,3 @@ void libztex_freeDevList (struct libztex_dev_list **devs) {
}
free(devs);
}
-
-