Subject: another endianness issue...

From: Guillaume Laurès <[email-masked]>
Date: Sat, 23 Jun 2001 11:29:19 +0200

Hello all,

I've tracked down the problem with the DAC960 driver (drivers/block), a
module that handles the mylex range of raid cards.

All the structures dealing with the card sound like this one:

typedef union DAC960_LA_InboundDoorBellRegister
{
   unsigned char All;
   struct {
     boolean HardwareMailboxNewCommand:1;                /* Bit 0 */
     boolean AcknowledgeHardwareMailboxStatus:1;         /* Bit 1 */
     boolean GenerateInterrupt:1;                        /* Bit 2 */
     boolean ControllerReset:1;                          /* Bit 3 */
     boolean MemoryMailboxNewCommand:1;                  /* Bit 4 */
     unsigned char :3;                                   /* Bits 5-7 */
   } Write;
   struct {
     boolean HardwareMailboxEmpty:1;                     /* Bit 0 */
     boolean InitializationNotInProgress:1;              /* Bit 1 */
     unsigned char :6;                                   /* Bits 2-7 */
   } Read;
}
DAC960_LA_InboundDoorBellRegister_T

And I guess on ppc we would need all the bits line reversed.

So, my question is how to handle this in the best approach, the goal
being a unified driver nt too difficult to maintain

I've attached a little sample written by hollis that reproduces the
problem. I've managed to get gcc choose the right structure according to
the endianness of the host with endian.h and a couple of #ifdef, but
this is still rather bad since we have to maintain two versions of the
structs.

Does antbody has a good idea on this or an example of driver we could
follow ?

Thanks,

GoM
/**********************************************
 *                                            *
 * gcc -Wall struct_endian.c -o struct_endian *
 *                                            *
 **********************************************/

#include <stdio.h>
#include <endian.h>

#if __BYTE_ORDER == __LITTLE_ENDIAN
union byte {
  unsigned char all;
  struct {
    unsigned char zero:1;
    unsigned char one:1;
    unsigned char two:1;
    unsigned char three:1;
    unsigned char four:1;
    unsigned char five:1;
    unsigned char size:1;
    unsigned char seven:1;
  } split;
};
#elif __BYTE_ORDER == __BIG_ENDIAN
union byte {
  unsigned char all;
  struct {
    unsigned char seven:1;
    unsigned char size:1;
    unsigned char five:1;
    unsigned char four:1;
    unsigned char three:1;
    unsigned char two:1;
    unsigned char one:1;
    unsigned char zero:1;
  } split;
};
#endif

int main(void)
{
  union byte a;
  unsigned char c;

  a.all = 0x1;
  c = 0x1;

  printf("struct:\n");
  printf("  bit 0 = %u\n", a.split.zero);
  printf("  bit 7 = %u\n", a.split.seven);

  printf("char:\n");
  printf("  bit 0 = %u\n", c & 0x1);
  printf("  bit 7 = %u\n", c >> 7);

  return 0;
}