Skip to content

Commit

Permalink
staging:iio:generic_buffer example - handle endian differences
Browse files Browse the repository at this point in the history
Signed-off-by: Jonathan Cameron <jic23@cam.ac.uk>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
  • Loading branch information
Jonathan Cameron authored and Greg Kroah-Hartman committed Dec 8, 2011
1 parent a7f7c36 commit 117cf8b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
8 changes: 8 additions & 0 deletions drivers/staging/iio/Documentation/generic_buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include <linux/types.h>
#include <string.h>
#include <poll.h>
#include <endian.h>
#include "iio_utils.h"

/**
Expand Down Expand Up @@ -56,6 +57,13 @@ int size_from_channelarray(struct iio_channel_info *channels, int num_channels)

void print2byte(int input, struct iio_channel_info *info)
{
/* First swap if incorrect endian */

if (info->be)
input = be16toh((uint_16t)input);
else
input = le16toh((uint_16t)input);

/* shift before conversion to avoid sign extension
of left aligned data */
input = input >> info->shift;
Expand Down
7 changes: 6 additions & 1 deletion drivers/staging/iio/Documentation/iio_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ struct iio_channel_info {
unsigned bits_used;
unsigned shift;
uint64_t mask;
unsigned be;
unsigned is_signed;
unsigned enabled;
unsigned location;
Expand All @@ -84,6 +85,7 @@ struct iio_channel_info {
* @is_signed: output whether channel is signed
* @bytes: output how many bytes the channel storage occupies
* @mask: output a bit mask for the raw data
* @be: big endian
* @device_dir: the iio device directory
* @name: the channel name
* @generic_name: the channel type name
Expand All @@ -93,6 +95,7 @@ inline int iioutils_get_type(unsigned *is_signed,
unsigned *bits_used,
unsigned *shift,
uint64_t *mask,
unsigned *be,
const char *device_dir,
const char *name,
const char *generic_name)
Expand All @@ -101,7 +104,7 @@ inline int iioutils_get_type(unsigned *is_signed,
int ret;
DIR *dp;
char *scan_el_dir, *builtname, *builtname_generic, *filename = 0;
char signchar;
char signchar, endianchar;
unsigned padint;
const struct dirent *ent;

Expand Down Expand Up @@ -156,6 +159,7 @@ inline int iioutils_get_type(unsigned *is_signed,
printf("failed to pass scan type description\n");
return ret;
}
*be = (endianchar == 'b');
*bytes = padint / 8;
if (*bits_used == 64)
*mask = ~0;
Expand Down Expand Up @@ -399,6 +403,7 @@ inline int build_channel_array(const char *device_dir,
&current->bits_used,
&current->shift,
&current->mask,
&current->be,
device_dir,
current->name,
current->generic_name);
Expand Down

0 comments on commit 117cf8b

Please sign in to comment.