Skip to content

Commit

Permalink
[media] hdpvr: reduce latency of i2c read/write w/recycled buffer
Browse files Browse the repository at this point in the history
The current hdpvr code kmalloc's a new buffer for every i2c read and
write. Rather than do that, lets allocate a buffer in the driver's
device struct and just use that every time.

The size I've chosen for the buffer is the maximum size I could
ascertain might be used by either ir-kbd-i2c or lirc_zilog, plus a bit
of padding (lirc_zilog may use up to 100 bytes on tx, rounded that up
to 128).

Note that this might also remedy user reports of very sluggish behavior
of IR receive with hdpvr hardware.

v2: make sure (len <= (dev->i2c_buf)) [Jean Delvare]

Reported-by: Jean Delvare <khali@linux-fr.org>
Acked-by: Jean Delvare <khali@linux-fr.org>
Signed-off-by: Jarod Wilson <jarod@redhat.com>
Signed-off-by: Mauro Carvalho Chehab <mchehab@redhat.com>
  • Loading branch information
Jarod Wilson authored and Mauro Carvalho Chehab committed Jan 19, 2011
1 parent 324b04b commit 559d162
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 17 deletions.
30 changes: 13 additions & 17 deletions drivers/media/video/hdpvr/hdpvr-i2c.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,55 +57,51 @@ static int hdpvr_i2c_read(struct hdpvr_device *dev, int bus,
unsigned char addr, char *data, int len)
{
int ret;
char *buf = kmalloc(len, GFP_KERNEL);
if (!buf)
return -ENOMEM;

if (len > sizeof(dev->i2c_buf))
return -EINVAL;

ret = usb_control_msg(dev->udev,
usb_rcvctrlpipe(dev->udev, 0),
REQTYPE_I2C_READ, CTRL_READ_REQUEST,
(bus << 8) | addr, 0, buf, len, 1000);
(bus << 8) | addr, 0, &dev->i2c_buf, len, 1000);

if (ret == len) {
memcpy(data, buf, len);
memcpy(data, &dev->i2c_buf, len);
ret = 0;
} else if (ret >= 0)
ret = -EIO;

kfree(buf);

return ret;
}

static int hdpvr_i2c_write(struct hdpvr_device *dev, int bus,
unsigned char addr, char *data, int len)
{
int ret;
char *buf = kmalloc(len, GFP_KERNEL);
if (!buf)
return -ENOMEM;

memcpy(buf, data, len);
if (len > sizeof(dev->i2c_buf))
return -EINVAL;

memcpy(&dev->i2c_buf, data, len);
ret = usb_control_msg(dev->udev,
usb_sndctrlpipe(dev->udev, 0),
REQTYPE_I2C_WRITE, CTRL_WRITE_REQUEST,
(bus << 8) | addr, 0, buf, len, 1000);
(bus << 8) | addr, 0, &dev->i2c_buf, len, 1000);

if (ret < 0)
goto error;
return ret;

ret = usb_control_msg(dev->udev,
usb_rcvctrlpipe(dev->udev, 0),
REQTYPE_I2C_WRITE_STATT, CTRL_READ_REQUEST,
0, 0, buf, 2, 1000);
0, 0, &dev->i2c_buf, 2, 1000);

if ((ret == 2) && (buf[1] == (len - 1)))
if ((ret == 2) && (dev->i2c_buf[1] == (len - 1)))
ret = 0;
else if (ret >= 0)
ret = -EIO;

error:
kfree(buf);
return ret;
}

Expand Down
3 changes: 3 additions & 0 deletions drivers/media/video/hdpvr/hdpvr.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
KERNEL_VERSION(HDPVR_MAJOR_VERSION, HDPVR_MINOR_VERSION, HDPVR_RELEASE)

#define HDPVR_MAX 8
#define HDPVR_I2C_MAX_SIZE 128

/* Define these values to match your devices */
#define HD_PVR_VENDOR_ID 0x2040
Expand Down Expand Up @@ -109,6 +110,8 @@ struct hdpvr_device {
struct i2c_adapter i2c_adapter;
/* I2C lock */
struct mutex i2c_mutex;
/* I2C message buffer space */
char i2c_buf[HDPVR_I2C_MAX_SIZE];

/* For passing data to ir-kbd-i2c */
struct IR_i2c_init_data ir_i2c_init_data;
Expand Down

0 comments on commit 559d162

Please sign in to comment.