Rework the code of ehci_td_buffer() with following trivial changes:

    * Switch to using dma_addr_t for 'delta' and 'next'
    * Convert while to for loop
    * Replace explicit magic number with dedicated contants derived
      via ARRAY_SIZE
    * Use ALIGN_DOWN to calculate 'next'
    * Return -ENOMEM instead of -1 when we ran out of buffers

Signed-off-by: Andrey Smirnov <[email protected]>
---
 drivers/usb/host/ehci-hcd.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)

diff --git a/drivers/usb/host/ehci-hcd.c b/drivers/usb/host/ehci-hcd.c
index 9758e1ed7..6742a67de 100644
--- a/drivers/usb/host/ehci-hcd.c
+++ b/drivers/usb/host/ehci-hcd.c
@@ -31,6 +31,7 @@
 #include <of.h>
 #include <usb/ehci.h>
 #include <linux/err.h>
+#include <linux/sizes.h>
 
 #include "ehci.h"
 
@@ -186,24 +187,23 @@ out:
 
 static int ehci_td_buffer(struct qTD *td, dma_addr_t addr, size_t sz)
 {
-       uint32_t delta, next;
+       const size_t buffer_count = ARRAY_SIZE(td->qt_buffer);
+       dma_addr_t delta, next;
        int idx;
 
-       idx = 0;
-       while (idx < 5) {
+       for (idx = 0; idx < buffer_count; idx++) {
                td->qt_buffer[idx] = cpu_to_hc32(addr);
-               next = (addr + 4096) & ~4095;
+               next = ALIGN_DOWN(addr + SZ_4K, SZ_4K);
                delta = next - addr;
                if (delta >= sz)
                        break;
                sz -= delta;
                addr = next;
-               idx++;
        }
 
-       if (idx == 5) {
+       if (idx == buffer_count) {
                pr_debug("out of buffer pointers (%u bytes left)\n", sz);
-               return -1;
+               return -ENOMEM;
        }
 
        return 0;
-- 
2.21.0


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox

Reply via email to