altera-comp.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * altera-comp.c
  3. *
  4. * altera FPGA driver
  5. *
  6. * Copyright (C) Altera Corporation 1998-2001
  7. * Copyright (C) 2010 NetUP Inc.
  8. * Copyright (C) 2010 Igor M. Liplianin <liplianin@netup.ru>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. *
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  24. */
  25. #include <linux/kernel.h>
  26. #include "altera-exprt.h"
  27. #define SHORT_BITS 16
  28. #define CHAR_BITS 8
  29. #define DATA_BLOB_LENGTH 3
  30. #define MATCH_DATA_LENGTH 8192
  31. #define ALTERA_REQUEST_SIZE 1024
  32. #define ALTERA_BUFFER_SIZE (MATCH_DATA_LENGTH + ALTERA_REQUEST_SIZE)
  33. static u32 altera_bits_req(u32 n)
  34. {
  35. u32 result = SHORT_BITS;
  36. if (n == 0)
  37. result = 1;
  38. else {
  39. /* Look for the highest non-zero bit position */
  40. while ((n & (1 << (SHORT_BITS - 1))) == 0) {
  41. n <<= 1;
  42. --result;
  43. }
  44. }
  45. return result;
  46. }
  47. static u32 altera_read_packed(u8 *buffer, u32 bits, u32 *bits_avail,
  48. u32 *in_index)
  49. {
  50. u32 result = 0;
  51. u32 shift = 0;
  52. u32 databyte = 0;
  53. while (bits > 0) {
  54. databyte = buffer[*in_index];
  55. result |= (((databyte >> (CHAR_BITS - *bits_avail))
  56. & (0xff >> (CHAR_BITS - *bits_avail))) << shift);
  57. if (bits <= *bits_avail) {
  58. result &= (0xffff >> (SHORT_BITS - (bits + shift)));
  59. *bits_avail -= bits;
  60. bits = 0;
  61. } else {
  62. ++(*in_index);
  63. shift += *bits_avail;
  64. bits -= *bits_avail;
  65. *bits_avail = CHAR_BITS;
  66. }
  67. }
  68. return result;
  69. }
  70. u32 altera_shrink(u8 *in, u32 in_length, u8 *out, u32 out_length, s32 version)
  71. {
  72. u32 i, j, data_length = 0L;
  73. u32 offset, length;
  74. u32 match_data_length = MATCH_DATA_LENGTH;
  75. u32 bits_avail = CHAR_BITS;
  76. u32 in_index = 0L;
  77. if (version > 0)
  78. --match_data_length;
  79. for (i = 0; i < out_length; ++i)
  80. out[i] = 0;
  81. /* Read number of bytes in data. */
  82. for (i = 0; i < sizeof(in_length); ++i) {
  83. data_length = data_length | (
  84. altera_read_packed(in,
  85. CHAR_BITS,
  86. &bits_avail,
  87. &in_index) << (i * CHAR_BITS));
  88. }
  89. if (data_length > out_length) {
  90. data_length = 0L;
  91. return data_length;
  92. }
  93. i = 0;
  94. while (i < data_length) {
  95. /* A 0 bit indicates literal data. */
  96. if (altera_read_packed(in, 1, &bits_avail,
  97. &in_index) == 0) {
  98. for (j = 0; j < DATA_BLOB_LENGTH; ++j) {
  99. if (i < data_length) {
  100. out[i] = (u8)altera_read_packed(in,
  101. CHAR_BITS,
  102. &bits_avail,
  103. &in_index);
  104. i++;
  105. }
  106. }
  107. } else {
  108. /* A 1 bit indicates offset/length to follow. */
  109. offset = altera_read_packed(in, altera_bits_req((s16)
  110. (i > match_data_length ?
  111. match_data_length : i)),
  112. &bits_avail,
  113. &in_index);
  114. length = altera_read_packed(in, CHAR_BITS,
  115. &bits_avail,
  116. &in_index);
  117. for (j = 0; j < length; ++j) {
  118. if (i < data_length) {
  119. out[i] = out[i - offset];
  120. i++;
  121. }
  122. }
  123. }
  124. }
  125. return data_length;
  126. }