- rename serprog_delay parameter to usecs
- fix code style, (output) formatting issues and comments
- sp_docommand: remove unnecessary malloc+memcpy and fix formatting

Signed-off-by: Stefan Tauner <[email protected]>
---
 programmer.h         |    5 ++-
 serprog-protocol.txt |    2 +-
 serprog.c            |   53 +++++++++++++++++++++++--------------------------
 3 files changed, 29 insertions(+), 31 deletions(-)

diff --git a/programmer.h b/programmer.h
index 7df2120..b6700c0 100644
--- a/programmer.h
+++ b/programmer.h
@@ -601,9 +601,10 @@ int serprog_init(void);
 void serprog_chip_writeb(uint8_t val, chipaddr addr);
 uint8_t serprog_chip_readb(const chipaddr addr);
 void serprog_chip_readn(uint8_t *buf, const chipaddr addr, size_t len);
-void serprog_delay(int delay);
+void serprog_delay(int usecs);
 int serprog_spi_send_command(unsigned int writecnt, unsigned int readcnt,
-                       const unsigned char *writearr, unsigned char *readarr);
+                            const unsigned char *writearr,
+                            unsigned char *readarr);
 int serprog_spi_read(struct flashchip *flash, uint8_t *buf, int start, int 
len);
 #endif
 
diff --git a/serprog-protocol.txt b/serprog-protocol.txt
index 2391aef..9a09287 100644
--- a/serprog-protocol.txt
+++ b/serprog-protocol.txt
@@ -19,7 +19,7 @@ COMMAND       Description                     Parameters      
                Return Value
 0x05   Query supported bustypes        none                            ACK + 
8-bit flags (as per flashrom) / NAK
 0x06   Query connected address lines   none                            ACK + 
8bit line count / NAK
 0x07   Query operation buffer size     none                            ACK + 
16bit size / NAK
-0x08   Query write-n maximum data len  none                            ACK + 
24bit maximum length / NAK
+0x08   Query maximum write-n length    none                            ACK + 
24bit length (0==2^24) / NAK
 0x09   Read byte                       24-bit addr                     ACK + 
BYTE / NAK
 0x0A   Read n bytes                    24-bit addr + 24-bit length     ACK + 
length bytes / NAK
 0x0B   Initialize operation buffer     none                            ACK / 
NAK
diff --git a/serprog.c b/serprog.c
index 5f2c014..7585baf 100644
--- a/serprog.c
+++ b/serprog.c
@@ -204,7 +204,7 @@ static void sp_synchronize(void)
                        return;
                }
        }
-       msg_perr("Error: cannot synchronize protocol\n"
+       msg_perr("Error: cannot synchronize protocol "
                "- check communications and reset device?\n");
        exit(1);
 }
@@ -220,32 +220,27 @@ static int sp_check_commandavail(uint8_t command)
 static int sp_automatic_cmdcheck(uint8_t cmd)
 {
        if ((sp_check_avail_automatic) && (sp_check_commandavail(cmd) == 0)) {
-               msg_pdbg("Warning: Automatic command availability check"
-                               " failed for cmd %d - wont execute cmd\n",cmd);
+               msg_pdbg("Warning: Automatic command availability check failed "
+                        "for cmd 0x%x - wont execute cmd\n", cmd);
                return 1;
                }
        return 0;
 }
 
 static int sp_docommand(uint8_t command, uint32_t parmlen,
-                            uint8_t * params, uint32_t retlen, void *retparms)
+                       uint8_t *params, uint32_t retlen, void *retparms)
 {
-       unsigned char *sendpacket;
        unsigned char c;
        if (sp_automatic_cmdcheck(command))
                return 1;
-       sendpacket = malloc(1 + parmlen);
-       if (!sendpacket)
-               sp_die("Error: cannot malloc command buffer");
-       sendpacket[0] = command;
-       memcpy(&(sendpacket[1]), params, parmlen);
-       if (write(sp_fd, sendpacket, 1 + parmlen) != (1 + parmlen)) {
-               sp_die("Error: cannot write command");
-       }
-       free(sendpacket);
+       if (write(sp_fd, &command, 1) != 1)
+               sp_die("Error: cannot write op code");
+       if (write(sp_fd, params, parmlen) != (parmlen))
+               sp_die("Error: cannot write parameters");
        if (read(sp_fd, &c, 1) != 1)
                sp_die("Error: cannot read from device");
-       if (c == S_NAK) return 1;
+       if (c == S_NAK)
+               return 1;
        if (c != S_ACK) {
                msg_perr("Error: invalid response 0x%02X from device\n",c);
                exit(1);
@@ -256,8 +251,8 @@ static int sp_docommand(uint8_t command, uint32_t parmlen,
                        int r;
                        r = read(sp_fd, retparms + rd_bytes,
                                 retlen - rd_bytes);
-                       if (r <= 0) sp_die
-                                   ("Error: cannot read return parameters");
+                       if (r <= 0)
+                               sp_die("Error: cannot read return parameters");
                        rd_bytes += r;
                } while (rd_bytes != retlen);
        }
@@ -735,30 +730,32 @@ void serprog_chip_readn(uint8_t * buf, const chipaddr 
addr, size_t len)
 {
        size_t lenm = len;
        chipaddr addrm = addr;
-       while ((sp_max_read_n)&&(lenm > sp_max_read_n)) {
-               sp_do_read_n(&(buf[addrm-addr]),addrm,sp_max_read_n);
+       while ((sp_max_read_n != 0) && (lenm > sp_max_read_n)) {
+               sp_do_read_n(&(buf[addrm-addr]), addrm, sp_max_read_n);
                addrm += sp_max_read_n;
                lenm -= sp_max_read_n;
        }
-       if (lenm) sp_do_read_n(&(buf[addrm-addr]),addrm,lenm);
+       if (lenm)
+               sp_do_read_n(&(buf[addrm-addr]), addrm, lenm);
 }
 
-void serprog_delay(int delay)
+void serprog_delay(int usecs)
 {
        unsigned char buf[4];
-       msg_pspew("%s\n", __func__);
+       msg_pspew("%s usecs=%d\n", __func__, usecs);
        if (!sp_check_commandavail(S_CMD_O_DELAY)) {
-               internal_delay(delay);
-               msg_pdbg("Note: serprog_delay used, but the programmer doesnt 
support delay\n");
+               msg_pdbg("Note: using internal_delay, because the attached "
+                        "programmer does not support the delay opcode.\n");
+               internal_delay(usecs);
                return;
        }
        if ((sp_max_write_n) && (sp_write_n_bytes))
                sp_pass_writen();
        sp_check_opbuf_usage(5);
-       buf[0] = ((delay >> 0) & 0xFF);
-       buf[1] = ((delay >> 8) & 0xFF);
-       buf[2] = ((delay >> 16) & 0xFF);
-       buf[3] = ((delay >> 24) & 0xFF);
+       buf[0] = ((usecs >> 0) & 0xFF);
+       buf[1] = ((usecs >> 8) & 0xFF);
+       buf[2] = ((usecs >> 16) & 0xFF);
+       buf[3] = ((usecs >> 24) & 0xFF);
        sp_stream_buffer_op(S_CMD_O_DELAY, 4, buf);
        sp_opbuf_usage += 5;
        sp_prev_was_write = 0;
-- 
1.7.1


_______________________________________________
flashrom mailing list
[email protected]
http://www.flashrom.org/mailman/listinfo/flashrom

Reply via email to