1*2da572c9SDan Streetman 2*2da572c9SDan Streetman #ifndef __842_H__ 3*2da572c9SDan Streetman #define __842_H__ 4*2da572c9SDan Streetman 5*2da572c9SDan Streetman /* The 842 compressed format is made up of multiple blocks, each of 6*2da572c9SDan Streetman * which have the format: 7*2da572c9SDan Streetman * 8*2da572c9SDan Streetman * <template>[arg1][arg2][arg3][arg4] 9*2da572c9SDan Streetman * 10*2da572c9SDan Streetman * where there are between 0 and 4 template args, depending on the specific 11*2da572c9SDan Streetman * template operation. For normal operations, each arg is either a specific 12*2da572c9SDan Streetman * number of data bytes to add to the output buffer, or an index pointing 13*2da572c9SDan Streetman * to a previously-written number of data bytes to copy to the output buffer. 14*2da572c9SDan Streetman * 15*2da572c9SDan Streetman * The template code is a 5-bit value. This code indicates what to do with 16*2da572c9SDan Streetman * the following data. Template codes from 0 to 0x19 should use the template 17*2da572c9SDan Streetman * table, the static "decomp_ops" table used in decompress. For each template 18*2da572c9SDan Streetman * (table row), there are between 1 and 4 actions; each action corresponds to 19*2da572c9SDan Streetman * an arg following the template code bits. Each action is either a "data" 20*2da572c9SDan Streetman * type action, or a "index" type action, and each action results in 2, 4, or 8 21*2da572c9SDan Streetman * bytes being written to the output buffer. Each template (i.e. all actions 22*2da572c9SDan Streetman * in the table row) will add up to 8 bytes being written to the output buffer. 23*2da572c9SDan Streetman * Any row with less than 4 actions is padded with noop actions, indicated by 24*2da572c9SDan Streetman * N0 (for which there is no corresponding arg in the compressed data buffer). 25*2da572c9SDan Streetman * 26*2da572c9SDan Streetman * "Data" actions, indicated in the table by D2, D4, and D8, mean that the 27*2da572c9SDan Streetman * corresponding arg is 2, 4, or 8 bytes, respectively, in the compressed data 28*2da572c9SDan Streetman * buffer should be copied directly to the output buffer. 29*2da572c9SDan Streetman * 30*2da572c9SDan Streetman * "Index" actions, indicated in the table by I2, I4, and I8, mean the 31*2da572c9SDan Streetman * corresponding arg is an index parameter that points to, respectively, a 2, 32*2da572c9SDan Streetman * 4, or 8 byte value already in the output buffer, that should be copied to 33*2da572c9SDan Streetman * the end of the output buffer. Essentially, the index points to a position 34*2da572c9SDan Streetman * in a ring buffer that contains the last N bytes of output buffer data. 35*2da572c9SDan Streetman * The number of bits for each index's arg are: 8 bits for I2, 9 bits for I4, 36*2da572c9SDan Streetman * and 8 bits for I8. Since each index points to a 2, 4, or 8 byte section, 37*2da572c9SDan Streetman * this means that I2 can reference 512 bytes ((2^8 bits = 256) * 2 bytes), I4 38*2da572c9SDan Streetman * can reference 2048 bytes ((2^9 = 512) * 4 bytes), and I8 can reference 2048 39*2da572c9SDan Streetman * bytes ((2^8 = 256) * 8 bytes). Think of it as a kind-of ring buffer for 40*2da572c9SDan Streetman * each of I2, I4, and I8 that are updated for each byte written to the output 41*2da572c9SDan Streetman * buffer. In this implementation, the output buffer is directly used for each 42*2da572c9SDan Streetman * index; there is no additional memory required. Note that the index is into 43*2da572c9SDan Streetman * a ring buffer, not a sliding window; for example, if there have been 260 44*2da572c9SDan Streetman * bytes written to the output buffer, an I2 index of 0 would index to byte 256 45*2da572c9SDan Streetman * in the output buffer, while an I2 index of 16 would index to byte 16 in the 46*2da572c9SDan Streetman * output buffer. 47*2da572c9SDan Streetman * 48*2da572c9SDan Streetman * There are also 3 special template codes; 0x1b for "repeat", 0x1c for 49*2da572c9SDan Streetman * "zeros", and 0x1e for "end". The "repeat" operation is followed by a 6 bit 50*2da572c9SDan Streetman * arg N indicating how many times to repeat. The last 8 bytes written to the 51*2da572c9SDan Streetman * output buffer are written again to the output buffer, N + 1 times. The 52*2da572c9SDan Streetman * "zeros" operation, which has no arg bits, writes 8 zeros to the output 53*2da572c9SDan Streetman * buffer. The "end" operation, which also has no arg bits, signals the end 54*2da572c9SDan Streetman * of the compressed data. There may be some number of padding (don't care, 55*2da572c9SDan Streetman * but usually 0) bits after the "end" operation bits, to fill the buffer 56*2da572c9SDan Streetman * length to a specific byte multiple (usually a multiple of 8, 16, or 32 57*2da572c9SDan Streetman * bytes). 58*2da572c9SDan Streetman * 59*2da572c9SDan Streetman * This software implementation also uses one of the undefined template values, 60*2da572c9SDan Streetman * 0x1d as a special "short data" template code, to represent less than 8 bytes 61*2da572c9SDan Streetman * of uncompressed data. It is followed by a 3 bit arg N indicating how many 62*2da572c9SDan Streetman * data bytes will follow, and then N bytes of data, which should be copied to 63*2da572c9SDan Streetman * the output buffer. This allows the software 842 compressor to accept input 64*2da572c9SDan Streetman * buffers that are not an exact multiple of 8 bytes long. However, those 65*2da572c9SDan Streetman * compressed buffers containing this sw-only template will be rejected by 66*2da572c9SDan Streetman * the 842 hardware decompressor, and must be decompressed with this software 67*2da572c9SDan Streetman * library. The 842 software compression module includes a parameter to 68*2da572c9SDan Streetman * disable using this sw-only "short data" template, and instead simply 69*2da572c9SDan Streetman * reject any input buffer that is not a multiple of 8 bytes long. 70*2da572c9SDan Streetman * 71*2da572c9SDan Streetman * After all actions for each operation code are processed, another template 72*2da572c9SDan Streetman * code is in the next 5 bits. The decompression ends once the "end" template 73*2da572c9SDan Streetman * code is detected. 74*2da572c9SDan Streetman */ 75*2da572c9SDan Streetman 76*2da572c9SDan Streetman #include <linux/module.h> 77*2da572c9SDan Streetman #include <linux/kernel.h> 78*2da572c9SDan Streetman #include <linux/bitops.h> 79*2da572c9SDan Streetman #include <asm/unaligned.h> 80*2da572c9SDan Streetman 81*2da572c9SDan Streetman #include <linux/sw842.h> 82*2da572c9SDan Streetman 83*2da572c9SDan Streetman /* special templates */ 84*2da572c9SDan Streetman #define OP_REPEAT (0x1B) 85*2da572c9SDan Streetman #define OP_ZEROS (0x1C) 86*2da572c9SDan Streetman #define OP_END (0x1E) 87*2da572c9SDan Streetman 88*2da572c9SDan Streetman /* sw only template - this is not in the hw design; it's used only by this 89*2da572c9SDan Streetman * software compressor and decompressor, to allow input buffers that aren't 90*2da572c9SDan Streetman * a multiple of 8. 91*2da572c9SDan Streetman */ 92*2da572c9SDan Streetman #define OP_SHORT_DATA (0x1D) 93*2da572c9SDan Streetman 94*2da572c9SDan Streetman /* additional bits of each op param */ 95*2da572c9SDan Streetman #define OP_BITS (5) 96*2da572c9SDan Streetman #define REPEAT_BITS (6) 97*2da572c9SDan Streetman #define SHORT_DATA_BITS (3) 98*2da572c9SDan Streetman #define I2_BITS (8) 99*2da572c9SDan Streetman #define I4_BITS (9) 100*2da572c9SDan Streetman #define I8_BITS (8) 101*2da572c9SDan Streetman 102*2da572c9SDan Streetman #define REPEAT_BITS_MAX (0x3f) 103*2da572c9SDan Streetman #define SHORT_DATA_BITS_MAX (0x7) 104*2da572c9SDan Streetman 105*2da572c9SDan Streetman /* Arbitrary values used to indicate action */ 106*2da572c9SDan Streetman #define OP_ACTION (0x70) 107*2da572c9SDan Streetman #define OP_ACTION_INDEX (0x10) 108*2da572c9SDan Streetman #define OP_ACTION_DATA (0x20) 109*2da572c9SDan Streetman #define OP_ACTION_NOOP (0x40) 110*2da572c9SDan Streetman #define OP_AMOUNT (0x0f) 111*2da572c9SDan Streetman #define OP_AMOUNT_0 (0x00) 112*2da572c9SDan Streetman #define OP_AMOUNT_2 (0x02) 113*2da572c9SDan Streetman #define OP_AMOUNT_4 (0x04) 114*2da572c9SDan Streetman #define OP_AMOUNT_8 (0x08) 115*2da572c9SDan Streetman 116*2da572c9SDan Streetman #define D2 (OP_ACTION_DATA | OP_AMOUNT_2) 117*2da572c9SDan Streetman #define D4 (OP_ACTION_DATA | OP_AMOUNT_4) 118*2da572c9SDan Streetman #define D8 (OP_ACTION_DATA | OP_AMOUNT_8) 119*2da572c9SDan Streetman #define I2 (OP_ACTION_INDEX | OP_AMOUNT_2) 120*2da572c9SDan Streetman #define I4 (OP_ACTION_INDEX | OP_AMOUNT_4) 121*2da572c9SDan Streetman #define I8 (OP_ACTION_INDEX | OP_AMOUNT_8) 122*2da572c9SDan Streetman #define N0 (OP_ACTION_NOOP | OP_AMOUNT_0) 123*2da572c9SDan Streetman 124*2da572c9SDan Streetman /* the max of the regular templates - not including the special templates */ 125*2da572c9SDan Streetman #define OPS_MAX (0x1a) 126*2da572c9SDan Streetman 127*2da572c9SDan Streetman #endif 128