mei: make mei_write_message more readable

1. reduce hairy casting
2. replace open code with mei_count_empty_write_slots
4. include header size in check for overflow
3. use concise for loop instead of a while loop

Signed-off-by: Tomas Winkler <tomas.winkler@intel.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
This commit is contained in:
Tomas Winkler 2012-06-19 09:13:35 +03:00 committed by Greg Kroah-Hartman
parent d20411588d
commit 169d1338ed

View File

@ -127,52 +127,39 @@ int mei_count_empty_write_slots(struct mei_device *dev)
* *
* This function returns -EIO if write has failed * This function returns -EIO if write has failed
*/ */
int mei_write_message(struct mei_device *dev, int mei_write_message(struct mei_device *dev, struct mei_msg_hdr *header,
struct mei_msg_hdr *header, unsigned char *buf, unsigned long length)
unsigned char *write_buffer,
unsigned long write_length)
{ {
u32 temp_msg = 0; unsigned long rem, dw_cnt;
unsigned long bytes_written = 0; u32 *reg_buf = (u32 *)buf;
unsigned char buffer_depth, filled_slots, empty_slots; int i;
unsigned long dw_to_write; int empty_slots;
dev->host_hw_state = mei_hcsr_read(dev);
dev_dbg(&dev->pdev->dev,
"host_hw_state = 0x%08x.\n",
dev->host_hw_state);
dev_dbg(&dev->pdev->dev, dev_dbg(&dev->pdev->dev,
"mei_write_message header=%08x.\n", "mei_write_message header=%08x.\n",
*((u32 *) header)); *((u32 *) header));
buffer_depth = (unsigned char) ((dev->host_hw_state & H_CBD) >> 24); empty_slots = mei_count_empty_write_slots(dev);
filled_slots = _host_get_filled_slots(dev); dev_dbg(&dev->pdev->dev, "empty slots = %hu.\n", empty_slots);
empty_slots = buffer_depth - filled_slots;
dev_dbg(&dev->pdev->dev,
"filled = %hu, empty = %hu.\n",
filled_slots, empty_slots);
dw_to_write = ((write_length + 3) / 4); dw_cnt = (length + sizeof(*header) + 3) / 4;
if (empty_slots < 0 || dw_cnt > empty_slots)
if (dw_to_write > empty_slots)
return -EIO; return -EIO;
mei_reg_write(dev, H_CB_WW, *((u32 *) header)); mei_reg_write(dev, H_CB_WW, *((u32 *) header));
while (write_length >= 4) { for (i = 0; i < length / 4; i++)
mei_reg_write(dev, H_CB_WW, mei_reg_write(dev, H_CB_WW, reg_buf[i]);
*(u32 *) (write_buffer + bytes_written));
bytes_written += 4; rem = length & 0x3;
write_length -= 4; if (rem > 0) {
} u32 reg = 0;
memcpy(&reg, &buf[length - rem], rem);
if (write_length > 0) { mei_reg_write(dev, H_CB_WW, reg);
memcpy(&temp_msg, &write_buffer[bytes_written], write_length);
mei_reg_write(dev, H_CB_WW, temp_msg);
} }
dev->host_hw_state = mei_hcsr_read(dev);
dev->host_hw_state |= H_IG; dev->host_hw_state |= H_IG;
mei_hcsr_set(dev); mei_hcsr_set(dev);
dev->me_hw_state = mei_mecsr_read(dev); dev->me_hw_state = mei_mecsr_read(dev);