misc: Update read() and write() methods to return bytes xfered

At present these functions return 0 on success. For some devices we want
to know how many bytes were transferred. It seems useful to adjust the API
to be more like the POSIX read() and write() functions.

Update these two methods, a test and all users.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Patrick Delaunay <patrick.delaunay@st.com>
This commit is contained in:
Simon Glass 2018-11-06 15:21:39 -07:00
parent 96794a3eae
commit 8729b1ae2c
8 changed files with 27 additions and 15 deletions

View file

@ -306,7 +306,7 @@ static int setup_mac_address(void)
ret = misc_read(dev, BSEC_OTP_MAC * 4 + STM32_BSEC_OTP_OFFSET,
otp, sizeof(otp));
if (ret)
if (ret < 0)
return ret;
for (i = 0; i < 6; i++)
@ -344,7 +344,7 @@ static int setup_serial_number(void)
ret = misc_read(dev, BSEC_OTP_SERIAL * 4 + STM32_BSEC_OTP_OFFSET,
otp, sizeof(otp));
if (ret)
if (ret < 0)
return ret;
sprintf(serial_string, "%08x%08x%08x", otp[0], otp[1], otp[2]);

View file

@ -29,7 +29,7 @@ static ulong vexpress_osc_clk_get_rate(struct clk *clk)
data = CLK_FUNCTION | priv->osc;
err = misc_read(vexpress_cfg, 0, &data, sizeof(data));
if (err)
if (err < 0)
return err;
return data;
@ -53,7 +53,7 @@ static ulong vexpress_osc_clk_set_rate(struct clk *clk, ulong rate)
buffer[0] = CLK_FUNCTION | priv->osc;
buffer[1] = rate;
err = misc_write(vexpress_cfg, 0, buffer, 2 * sizeof(u32));
if (err)
if (err < 0)
return err;
return rate;

View file

@ -35,7 +35,7 @@ void display_sysid(void)
if (ret)
return;
ret = misc_read(dev, 0, &sysid, sizeof(sysid));
if (ret)
if (ret < 0)
return;
stamp = sysid[1];

View file

@ -20,7 +20,7 @@ int misc_sandbox_read(struct udevice *dev, int offset, void *buf, int size)
memcpy(buf, priv->mem + offset, size);
return 0;
return size;
}
int misc_sandbox_write(struct udevice *dev, int offset, const void *buf,
@ -30,7 +30,7 @@ int misc_sandbox_write(struct udevice *dev, int offset, const void *buf,
memcpy(priv->mem + offset, buf, size);
return 0;
return size;
}
int misc_sandbox_ioctl(struct udevice *dev, unsigned long request, void *buf)

View file

@ -65,7 +65,7 @@ static int dump_efuses(cmd_tbl_t *cmdtp, int flag,
}
ret = misc_read(dev, 0, &fuses, sizeof(fuses));
if (ret) {
if (ret < 0) {
printf("%s: misc_read failed\n", __func__);
return 0;
}

View file

@ -29,6 +29,9 @@ int fuse_read(u32 bank, u32 word, u32 *val)
return ret;
ret = misc_read(dev, word * 4 + STM32_BSEC_SHADOW_OFFSET,
val, 4);
if (ret < 0)
return ret;
ret = 0;
break;
default:
@ -54,6 +57,9 @@ int fuse_prog(u32 bank, u32 word, u32 val)
return ret;
ret = misc_write(dev, word * 4 + STM32_BSEC_OTP_OFFSET,
&val, 4);
if (ret < 0)
return ret;
ret = 0;
break;
default:
@ -78,6 +84,9 @@ int fuse_sense(u32 bank, u32 word, u32 *val)
if (ret)
return ret;
ret = misc_read(dev, word * 4 + STM32_BSEC_OTP_OFFSET, val, 4);
if (ret < 0)
return ret;
ret = 0;
break;
default:
@ -103,6 +112,9 @@ int fuse_override(u32 bank, u32 word, u32 val)
return ret;
ret = misc_write(dev, word * 4 + STM32_BSEC_SHADOW_OFFSET,
&val, 4);
if (ret < 0)
return ret;
ret = 0;
break;
default:

View file

@ -13,7 +13,7 @@
* @buf: pointer to data buffer
* @size: data size in bytes to read the device
*
* Return: 0 if OK, -ve on error
* Return: number of bytes read if OK (may be 0 if EOF), -ve on error
*/
int misc_read(struct udevice *dev, int offset, void *buf, int size);
@ -24,7 +24,7 @@ int misc_read(struct udevice *dev, int offset, void *buf, int size);
* @buf: pointer to data buffer
* @size: data size in bytes to write the device
*
* Return: 0 if OK, -ve on error
* Return: number of bytes written if OK (may be < @size), -ve on error
*/
int misc_write(struct udevice *dev, int offset, void *buf, int size);
@ -90,7 +90,7 @@ struct misc_ops {
* @buf: pointer to data buffer
* @size: data size in bytes to read the device
*
* Return: 0 if OK, -ve on error
* Return: number of bytes read if OK (may be 0 if EOF), -ve on error
*/
int (*read)(struct udevice *dev, int offset, void *buf, int size);
@ -101,7 +101,7 @@ struct misc_ops {
* @buf: pointer to data buffer
* @size: data size in bytes to write the device
*
* Return: 0 if OK, -ve on error
* Return: number of bytes written if OK (may be < @size), -ve on error
*/
int (*write)(struct udevice *dev, int offset, const void *buf,
int size);

View file

@ -21,9 +21,9 @@ static int dm_test_misc(struct unit_test_state *uts)
ut_assertok(uclass_get_device_by_name(UCLASS_MISC, "misc-test", &dev));
/* Read / write tests */
ut_assertok(misc_write(dev, 0, "TEST", 4));
ut_assertok(misc_write(dev, 4, "WRITE", 5));
ut_assertok(misc_read(dev, 0, buf, 9));
ut_asserteq(4, misc_write(dev, 0, "TEST", 4));
ut_asserteq(5, misc_write(dev, 4, "WRITE", 5));
ut_asserteq(9, misc_read(dev, 0, buf, 9));
ut_assertok(memcmp(buf, "TESTWRITE", 9));