Skip to content

Commit

Permalink
Input: atmel_mxt_ts - refactor firmware flash to extract context into…
Browse files Browse the repository at this point in the history
… struct

Signed-off-by: Nick Dyer <[email protected]>
  • Loading branch information
ndyer committed Apr 26, 2016
1 parent c909ada commit 1bbe20f
Showing 1 changed file with 36 additions and 23 deletions.
59 changes: 36 additions & 23 deletions drivers/input/touchscreen/atmel_mxt_ts.c
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,22 @@ struct mxt_cfg {
struct mxt_info info;
};

/* Firmware frame structure */
struct mxt_fw_frame {
__be16 size;
u8 data[];
};

/* Firmware update context */
struct mxt_flash {
const struct firmware *fw;
struct mxt_fw_frame *frame;
loff_t pos;
size_t frame_size;
unsigned int count;
unsigned int retry;
};

/* Each client has this additional data */
struct mxt_data {
struct i2c_client *client;
Expand Down Expand Up @@ -2710,21 +2726,17 @@ static int mxt_check_firmware_format(struct device *dev,
static int mxt_load_fw(struct device *dev)
{
struct mxt_data *data = dev_get_drvdata(dev);
const struct firmware *fw = NULL;
unsigned int frame_size;
unsigned int pos = 0;
unsigned int retry = 0;
unsigned int frame = 0;
struct mxt_flash f = { 0, };
int ret;

ret = request_firmware(&fw, data->fw_name, dev);
ret = request_firmware(&f.fw, data->fw_name, dev);
if (ret) {
dev_err(dev, "Unable to open firmware %s\n", data->fw_name);
return ret;
}

/* Check for incorrect enc file */
ret = mxt_check_firmware_format(dev, fw);
ret = mxt_check_firmware_format(dev, f.fw);
if (ret)
goto release_firmware;

Expand Down Expand Up @@ -2778,41 +2790,42 @@ static int mxt_load_fw(struct device *dev)
goto disable_irq;
}

while (pos < fw->size) {
while (f.pos < f.fw->size) {
f.frame = (struct mxt_fw_frame *)(f.fw->data + f.pos);

ret = mxt_check_bootloader(data, MXT_WAITING_FRAME_DATA, true);
if (ret)
goto disable_irq;

frame_size = ((*(fw->data + pos) << 8) | *(fw->data + pos + 1));

/* Take account of CRC bytes */
frame_size += 2;
f.frame_size = __be16_to_cpu(f.frame->size) + 2U;

/* Write one frame to device */
ret = mxt_bootloader_write(data, fw->data + pos, frame_size);
ret = mxt_bootloader_write(data, f.fw->data + f.pos,
f.frame_size);
if (ret)
goto disable_irq;

ret = mxt_check_bootloader(data, MXT_FRAME_CRC_PASS, true);
if (ret) {
retry++;
f.retry++;

/* Back off by 20ms per retry */
msleep(retry * 20);
msleep(f.retry * 20);

if (retry > 20) {
if (f.retry > 20) {
dev_err(dev, "Retry count exceeded\n");
goto disable_irq;
}
} else {
retry = 0;
pos += frame_size;
frame++;
f.retry = 0;
f.pos += f.frame_size;
f.count++;
}

if (frame % 50 == 0)
dev_dbg(dev, "Sent %d frames, %d/%zd bytes\n",
frame, pos, fw->size);
if (f.count % 50 == 0)
dev_dbg(dev, "Sent %u frames, %lld/%zu bytes\n",
f.count, f.pos, f.fw->size);
}

/* Wait for flash. */
Expand All @@ -2821,7 +2834,7 @@ static int mxt_load_fw(struct device *dev)
if (ret)
goto disable_irq;

dev_dbg(dev, "Sent %d frames, %d bytes\n", frame, pos);
dev_dbg(dev, "Sent %u frames, %lld bytes\n", f.count, f.pos);

/*
* Wait for device to reset. Some bootloader versions do not assert
Expand All @@ -2835,7 +2848,7 @@ static int mxt_load_fw(struct device *dev)
disable_irq:
disable_irq(data->irq);
release_firmware:
release_firmware(fw);
release_firmware(f.fw);
return ret;
}

Expand Down

0 comments on commit 1bbe20f

Please sign in to comment.