* [PATCH v2 0/7] RATP i2c and GPIO support
@ 2018-09-12 13:45 Aleksander Morgado
2018-09-12 13:45 ` [PATCH v2 1/7] ratp: implement i2c read/write support Aleksander Morgado
` (7 more replies)
0 siblings, 8 replies; 9+ messages in thread
From: Aleksander Morgado @ 2018-09-12 13:45 UTC (permalink / raw)
To: barebox; +Cc: andrew.smirnov, Aleksander Morgado
This series of patches implements support for i2c and GPIO operations via RATP.
It addresses most of the comments from the first series review, except for Sascha's suggestion to implement name resolving for the i2c buses and Andrey's suggestion to prepare a new helper method to initialize the i2c_client structure. I already have a patch for the latter, but will send it for review once these have been accepted and merged, as it's really a bit orthogonal to the whole RATP logic.
Another different w.r.t. v1 apart from addressing the comments is the review is that I made the RATP i2c support depend on CONFIG_I2C and the RATP GPIO support on CONFIG_GENERIC_GPIO. That was missing, and was breaking build if RATP was selected but no i2c or GPIO support was included in the build.
Comments welcome!
Aleksander Morgado (7):
ratp: implement i2c read/write support
bbremote: implement i2c read/write support
ratp: implement support for GPIO commands
bbremote: implement support for GPIO operations
ratp: use __packed instead of the full form
ratp: use pr_ macros to print messages
ratp: fix incorrect whitespaces in method calls
common/ratp/Kconfig | 15 ++
common/ratp/Makefile | 2 +
common/ratp/getenv.c | 12 +-
common/ratp/gpio.c | 144 ++++++++++++++++++
common/ratp/i2c.c | 284 +++++++++++++++++++++++++++++++++++
common/ratp/md.c | 24 +--
common/ratp/mw.c | 24 +--
common/ratp/reset.c | 10 +-
include/ratp_bb.h | 10 ++
scripts/remote/controller.py | 60 ++++++++
scripts/remote/main.py | 74 +++++++++
scripts/remote/messages.py | 179 ++++++++++++++++++++++
12 files changed, 806 insertions(+), 32 deletions(-)
create mode 100644 common/ratp/gpio.c
create mode 100644 common/ratp/i2c.c
--
2.19.0
_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox
^ permalink raw reply [flat|nested] 9+ messages in thread
* [PATCH v2 1/7] ratp: implement i2c read/write support
2018-09-12 13:45 [PATCH v2 0/7] RATP i2c and GPIO support Aleksander Morgado
@ 2018-09-12 13:45 ` Aleksander Morgado
2018-09-12 13:45 ` [PATCH v2 2/7] bbremote: " Aleksander Morgado
` (6 subsequent siblings)
7 siblings, 0 replies; 9+ messages in thread
From: Aleksander Morgado @ 2018-09-12 13:45 UTC (permalink / raw)
To: barebox; +Cc: andrew.smirnov, Aleksander Morgado
Introduce two new RATP commands that allow running i2c read/write
operations, very similar in format to the already existing md/mw
RATP commands.
The messages are defined with a fixed 16-bit long register field, but
it will only be treated as a 16-bit address if I2C_FLAG_WIDE_ADDRESS
is set in the message flags field. If this flag is unset, the start
register address is assumed 8-bit long.
If the message includes the I2C_FLAG_MASTER_MODE flag, the start
register field is ignored and a i2c master send/receive operation is
performed.
Signed-off-by: Aleksander Morgado <aleksander@aleksander.es>
---
common/ratp/Kconfig | 7 ++
common/ratp/Makefile | 1 +
common/ratp/i2c.c | 284 +++++++++++++++++++++++++++++++++++++++++++
include/ratp_bb.h | 4 +
4 files changed, 296 insertions(+)
create mode 100644 common/ratp/i2c.c
diff --git a/common/ratp/Kconfig b/common/ratp/Kconfig
index 93ff75d64..4b28d5f9f 100644
--- a/common/ratp/Kconfig
+++ b/common/ratp/Kconfig
@@ -12,3 +12,10 @@ config CONSOLE_RATP
this option adds a machine readable interface for controlling barebox.
Say yes here if you want to control barebox from a remote host.
+config RATP_CMD_I2C
+ bool
+ depends on RATP
+ depends on I2C
+ prompt "RATP i2c support"
+ help
+ This option adds support for i2c read/write commands via RATP.
\ No newline at end of file
diff --git a/common/ratp/Makefile b/common/ratp/Makefile
index 2c6d674f6..b83c48327 100644
--- a/common/ratp/Makefile
+++ b/common/ratp/Makefile
@@ -4,3 +4,4 @@ obj-y += getenv.o
obj-y += md.o
obj-y += mw.o
obj-y += reset.o
+obj-$(CONFIG_RATP_CMD_I2C) += i2c.o
diff --git a/common/ratp/i2c.c b/common/ratp/i2c.c
new file mode 100644
index 000000000..07097c7f4
--- /dev/null
+++ b/common/ratp/i2c.c
@@ -0,0 +1,284 @@
+/*
+ * Copyright (c) 2018 Sascha Hauer <s.hauer@pengutronix.de>, Pengutronix
+ * Copyright (c) 2018 Zodiac Inflight Innovations
+ *
+ * See file CREDITS for list of people who contributed to this
+ * project.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ */
+
+#define pr_fmt(fmt) "barebox-ratp: i2c: " fmt
+
+#include <common.h>
+#include <ratp_bb.h>
+#include <init.h>
+#include <driver.h>
+#include <malloc.h>
+#include <errno.h>
+#include <i2c/i2c.h>
+
+/* NOTE:
+ * - Fixed-size fields (e.g. integers) are given just after the header.
+ * - Variable-length fields are stored inside the buffer[] and their position
+ * within the buffer[] and their size are given as fixed-sized fields after
+ * the header.
+ * The message may be extended at any time keeping backwards compatibility,
+ * as the position of the buffer[] is given by the buffer_offset field. i.e.
+ * increasing the buffer_offset field we can extend the fixed-sized section
+ * to add more fields.
+ */
+
+#define I2C_FLAG_WIDE_ADDRESS BIT(0)
+#define I2C_FLAG_MASTER_MODE BIT(1)
+
+struct ratp_bb_i2c_read_request {
+ struct ratp_bb header;
+ uint16_t buffer_offset;
+ uint8_t bus;
+ uint8_t addr;
+ uint16_t reg;
+ uint8_t flags;
+ uint16_t size;
+ uint8_t buffer[];
+} __packed;
+
+struct ratp_bb_i2c_read_response {
+ struct ratp_bb header;
+ uint16_t buffer_offset;
+ uint32_t errno;
+ uint16_t data_size;
+ uint16_t data_offset;
+ uint8_t buffer[];
+} __packed;
+
+struct ratp_bb_i2c_write_request {
+ struct ratp_bb header;
+ uint16_t buffer_offset;
+ uint8_t bus;
+ uint8_t addr;
+ uint16_t reg;
+ uint8_t flags;
+ uint16_t data_size;
+ uint16_t data_offset;
+ uint8_t buffer[];
+} __packed;
+
+struct ratp_bb_i2c_write_response {
+ struct ratp_bb header;
+ uint16_t buffer_offset;
+ uint32_t errno;
+ uint16_t written;
+ uint8_t buffer[];
+} __packed;
+
+static int ratp_cmd_i2c_read(const struct ratp_bb *req, int req_len,
+ struct ratp_bb **rsp, int *rsp_len)
+{
+ struct ratp_bb_i2c_read_request *i2c_read_req = (struct ratp_bb_i2c_read_request *)req;
+ struct ratp_bb_i2c_read_response *i2c_read_rsp;
+ struct i2c_adapter *adapter;
+ struct i2c_client client;
+ uint16_t buffer_offset;
+ int i2c_read_rsp_len;
+ uint16_t reg;
+ uint16_t size;
+ uint32_t wide = 0;
+ int ret = 0;
+
+ /* At least message header should be valid */
+ if (req_len < sizeof(*i2c_read_req)) {
+ pr_err("read ignored: size mismatch (%d < %zu)\n",
+ req_len, sizeof(*i2c_read_req));
+ ret = -EINVAL;
+ goto out_rsp;
+ }
+
+ /* We don't require any buffer here, but just validate buffer position and size */
+ buffer_offset = be16_to_cpu(i2c_read_req->buffer_offset);
+ if (buffer_offset != req_len) {
+ pr_err("read ignored: invalid buffer offset (%hu != %d)\n",
+ buffer_offset, req_len);
+ ret = -EINVAL;
+ goto out_rsp;
+ }
+
+ reg = be16_to_cpu(i2c_read_req->reg);
+ size = be16_to_cpu(i2c_read_req->size);
+ if (i2c_read_req->flags & I2C_FLAG_WIDE_ADDRESS)
+ wide = I2C_ADDR_16_BIT;
+
+out_rsp:
+ /* Avoid reading anything on error */
+ if (ret != 0)
+ size = 0;
+
+ i2c_read_rsp_len = sizeof(*i2c_read_rsp) + size;
+ i2c_read_rsp = xzalloc(i2c_read_rsp_len);
+ i2c_read_rsp->header.type = cpu_to_be16(BB_RATP_TYPE_I2C_READ_RETURN);
+ i2c_read_rsp->buffer_offset = cpu_to_be16(sizeof(*i2c_read_rsp));
+ i2c_read_rsp->data_offset = 0;
+
+ /* Don't read anything on error or if 0 bytes were requested */
+ if (size > 0) {
+ adapter = i2c_get_adapter(i2c_read_req->bus);
+ if (!adapter) {
+ pr_err("read ignored: i2c bus %u not found\n", i2c_read_req->bus);
+ ret = -ENODEV;
+ goto out;
+ }
+
+ client.adapter = adapter;
+ client.addr = i2c_read_req->addr;
+
+ if (i2c_read_req->flags & I2C_FLAG_MASTER_MODE) {
+ ret = i2c_master_recv(&client, i2c_read_rsp->buffer, size);
+ } else {
+ ret = i2c_read_reg(&client, reg | wide, i2c_read_rsp->buffer, size);
+ }
+ if (ret != size) {
+ pr_err("read ignored: not all bytes read (%u < %u)\n", ret, size);
+ ret = -EIO;
+ goto out;
+ }
+ ret = 0;
+ }
+
+out:
+ if (ret != 0) {
+ i2c_read_rsp->data_size = 0;
+ i2c_read_rsp->errno = cpu_to_be32(ret);
+ i2c_read_rsp_len = sizeof(*i2c_read_rsp);
+ } else {
+ i2c_read_rsp->data_size = cpu_to_be16(size);
+ i2c_read_rsp->errno = 0;
+ }
+
+ *rsp = (struct ratp_bb *)i2c_read_rsp;
+ *rsp_len = i2c_read_rsp_len;
+
+ return ret;
+}
+
+BAREBOX_RATP_CMD_START(I2C_READ)
+ .request_id = BB_RATP_TYPE_I2C_READ,
+ .response_id = BB_RATP_TYPE_I2C_READ_RETURN,
+ .cmd = ratp_cmd_i2c_read
+BAREBOX_RATP_CMD_END
+
+
+static int ratp_cmd_i2c_write(const struct ratp_bb *req, int req_len,
+ struct ratp_bb **rsp, int *rsp_len)
+{
+ struct ratp_bb_i2c_write_request *i2c_write_req = (struct ratp_bb_i2c_write_request *)req;
+ struct ratp_bb_i2c_write_response *i2c_write_rsp;
+ struct i2c_adapter *adapter;
+ struct i2c_client client;
+ uint8_t *buffer;
+ uint16_t buffer_offset;
+ uint16_t buffer_size;
+ uint16_t reg;
+ uint16_t data_offset;
+ uint16_t data_size;
+ uint32_t wide = 0;
+ int ret = 0;
+ int written = 0;
+
+ /* At least message header should be valid */
+ if (req_len < sizeof(*i2c_write_req)) {
+ pr_err("write ignored: size mismatch (%d < %zu)\n",
+ req_len, sizeof(*i2c_write_req));
+ ret = -EINVAL;
+ goto out;
+ }
+
+ /* Validate buffer position and size */
+ buffer_offset = be16_to_cpu(i2c_write_req->buffer_offset);
+ if (req_len < buffer_offset) {
+ pr_err("write ignored: invalid buffer offset (%d < %hu)\n",
+ req_len, buffer_offset);
+ ret = -EINVAL;
+ goto out;
+ }
+ buffer_size = req_len - buffer_offset;
+ buffer = ((uint8_t *)i2c_write_req) + buffer_offset;
+
+ /* Validate data position and size */
+ data_offset = be16_to_cpu(i2c_write_req->data_offset);
+ if (data_offset != 0) {
+ pr_err("write ignored: invalid data offset\n");
+ ret = -EINVAL;
+ goto out;
+ }
+ data_size = be16_to_cpu(i2c_write_req->data_size);
+ if (!data_size) {
+ /* Success */
+ goto out;
+ }
+
+ /* Validate buffer size */
+ if (buffer_size < data_size) {
+ pr_err("write ignored: size mismatch (%d < %hu): data not fully given\n",
+ buffer_size, data_size);
+ ret = -EINVAL;
+ goto out;
+ }
+
+ reg = be16_to_cpu(i2c_write_req->reg);
+ if (i2c_write_req->flags & I2C_FLAG_WIDE_ADDRESS)
+ wide = I2C_ADDR_16_BIT;
+
+ adapter = i2c_get_adapter(i2c_write_req->bus);
+ if (!adapter) {
+ pr_err("write ignored: i2c bus %u not found\n", i2c_write_req->bus);
+ ret = -ENODEV;
+ goto out;
+ }
+
+ client.adapter = adapter;
+ client.addr = i2c_write_req->addr;
+
+ if (i2c_write_req->flags & I2C_FLAG_MASTER_MODE) {
+ written = i2c_master_send(&client, &buffer[data_offset], data_size);
+ } else {
+ written = i2c_write_reg(&client, reg | wide, &buffer[data_offset], data_size);
+ }
+
+ if (written != data_size) {
+ pr_err("write ignored: not all bytes written (%u < %u)\n", ret, data_size);
+ ret = -EIO;
+ goto out;
+ }
+
+out:
+ i2c_write_rsp = xzalloc(sizeof(*i2c_write_rsp));
+ i2c_write_rsp->header.type = cpu_to_be16(BB_RATP_TYPE_I2C_WRITE_RETURN);
+ i2c_write_rsp->buffer_offset = cpu_to_be16(sizeof(*i2c_write_rsp)); /* n/a */
+
+ if (ret != 0) {
+ i2c_write_rsp->written = 0;
+ i2c_write_rsp->errno = cpu_to_be32(ret);
+ } else {
+ i2c_write_rsp->written = cpu_to_be16((uint16_t)written);
+ i2c_write_rsp->errno = 0;
+ }
+
+ *rsp = (struct ratp_bb *)i2c_write_rsp;
+ *rsp_len = sizeof(*i2c_write_rsp);
+
+ return ret;
+}
+
+BAREBOX_RATP_CMD_START(I2C_WRITE)
+ .request_id = BB_RATP_TYPE_I2C_WRITE,
+ .response_id = BB_RATP_TYPE_I2C_WRITE_RETURN,
+ .cmd = ratp_cmd_i2c_write
+BAREBOX_RATP_CMD_END
diff --git a/include/ratp_bb.h b/include/ratp_bb.h
index 3a80cf6ae..32b8040b6 100644
--- a/include/ratp_bb.h
+++ b/include/ratp_bb.h
@@ -17,6 +17,10 @@
#define BB_RATP_TYPE_MW 12
#define BB_RATP_TYPE_MW_RETURN 13
#define BB_RATP_TYPE_RESET 14
+#define BB_RATP_TYPE_I2C_READ 15
+#define BB_RATP_TYPE_I2C_READ_RETURN 16
+#define BB_RATP_TYPE_I2C_WRITE 17
+#define BB_RATP_TYPE_I2C_WRITE_RETURN 18
struct ratp_bb {
uint16_t type;
--
2.19.0
_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox
^ permalink raw reply [flat|nested] 9+ messages in thread
* [PATCH v2 2/7] bbremote: implement i2c read/write support
2018-09-12 13:45 [PATCH v2 0/7] RATP i2c and GPIO support Aleksander Morgado
2018-09-12 13:45 ` [PATCH v2 1/7] ratp: implement i2c read/write support Aleksander Morgado
@ 2018-09-12 13:45 ` Aleksander Morgado
2018-09-12 13:45 ` [PATCH v2 3/7] ratp: implement support for GPIO commands Aleksander Morgado
` (5 subsequent siblings)
7 siblings, 0 replies; 9+ messages in thread
From: Aleksander Morgado @ 2018-09-12 13:45 UTC (permalink / raw)
To: barebox; +Cc: andrew.smirnov, Aleksander Morgado
Extend the bbremote script with operations to perform binary i2c
read/write operations.
E.g.:
barebox:/ i2c_read -b 0 -a 0x68 -r 0x06A0 -c 4 -w
0x8f 0x30 0x00 0x00
barebox:/ i2c_write -b 0 -a 0x68 -r 0x06A0 -w 0x87 0x30 0x00 0x00
barebox:/ i2c_read -b 0 -a 0x68 -r 0x06A0 -c 4 -w
0x87 0x30 0x00 0x00
barebox:/ i2c_write -b 0 -a 0x68 -r 0x06A0 -w 0x8f 0x30 0x00 0x00
barebox:/ i2c_read -b 0 -a 0x68 -r 0x06A0 -c 4 -w
0x8f 0x30 0x00 0x00
===================================================================
$ ./scripts/bbremote --port /dev/ttyUSB2 i2c-read 0x00 0x68 0x06A0 0x01 4
8f300000
$ ./scripts/bbremote --port /dev/ttyUSB2 i2c-write 0x00 0x68 0x06A0 0x01 "87300000"
4 bytes written
$ ./scripts/bbremote --port /dev/ttyUSB2 i2c-read 0x00 0x68 0x06A0 0x01 4
87300000
$ ./scripts/bbremote --port /dev/ttyUSB2 i2c-write 0x00 0x68 0x06A0 0x01 "8f300000"
4 bytes written
$ ./scripts/bbremote --port /dev/ttyUSB2 i2c-read 0x00 0x68 0x06A0 0x01 4
8f300000
Signed-off-by: Aleksander Morgado <aleksander@aleksander.es>
---
scripts/remote/controller.py | 24 ++++++++++
scripts/remote/main.py | 37 ++++++++++++++++
scripts/remote/messages.py | 86 ++++++++++++++++++++++++++++++++++++
3 files changed, 147 insertions(+)
diff --git a/scripts/remote/controller.py b/scripts/remote/controller.py
index 1a8390904..10037b890 100644
--- a/scripts/remote/controller.py
+++ b/scripts/remote/controller.py
@@ -61,6 +61,18 @@ def unpack(data):
elif p_type == BBType.reset:
logging.debug("received: reset")
return BBPacketReset(raw=data)
+ elif p_type == BBType.i2c_read:
+ logging.debug("received: i2c_read")
+ return BBPacketI2cRead(raw=data)
+ elif p_type == BBType.i2c_read_return:
+ logging.debug("received: i2c_read_return")
+ return BBPacketI2cReadReturn(raw=data)
+ elif p_type == BBType.i2c_write:
+ logging.debug("received: i2c_write")
+ return BBPacketI2cWrite(raw=data)
+ elif p_type == BBType.i2c_write_return:
+ logging.debug("received: i2c_write_return")
+ return BBPacketI2cWriteReturn(raw=data)
else:
logging.debug("received: UNKNOWN")
return BBPacket(raw=data)
@@ -139,6 +151,18 @@ class Controller(Thread):
logging.info("Mw return: %r", r)
return (r.exit_code,r.written)
+ def i2c_read(self, bus, addr, reg, flags, size):
+ self._send(BBPacketI2cRead(bus=bus, addr=addr, reg=reg, flags=flags, size=size))
+ r = self._expect(BBPacketI2cReadReturn)
+ logging.info("i2c read return: %r", r)
+ return (r.exit_code,r.data)
+
+ def i2c_write(self, bus, addr, reg, flags, data):
+ self._send(BBPacketI2cWrite(bus=bus, addr=addr, reg=reg, flags=flags, data=data))
+ r = self._expect(BBPacketI2cWriteReturn)
+ logging.info("i2c write return: %r", r)
+ return (r.exit_code,r.written)
+
def reset(self, force):
self._send(BBPacketReset(force=force))
diff --git a/scripts/remote/main.py b/scripts/remote/main.py
index 38d280bfe..0f7783927 100644
--- a/scripts/remote/main.py
+++ b/scripts/remote/main.py
@@ -98,6 +98,27 @@ def handle_mw(args):
return res
+def handle_i2c_read(args):
+ ctrl = get_controller(args)
+ (res,data) = ctrl.i2c_read(args.bus, args.address, args.reg, args.flags, args.size)
+ if res == 0:
+ print(binascii.hexlify(data))
+ ctrl.close()
+ return res
+
+
+def handle_i2c_write(args):
+ ctrl = get_controller(args)
+ data=args.data
+ if ((len(data) % 2) != 0):
+ data="0"+data
+ (res,written) = ctrl.i2c_write(args.bus, args.address, args.reg, args.flags, binascii.unhexlify(data))
+ if res == 0:
+ print("%i bytes written" % written)
+ ctrl.close()
+ return res
+
+
def handle_reset(args):
ctrl = get_controller(args)
ctrl.reset(args.force)
@@ -188,6 +209,22 @@ parser_mw.add_argument('address', type=auto_int, help="address")
parser_mw.add_argument('data', help="data")
parser_mw.set_defaults(func=handle_mw)
+parser_i2c_read = subparsers.add_parser('i2c-read', help="run i2c read command")
+parser_i2c_read.add_argument('bus', type=auto_int, help="bus")
+parser_i2c_read.add_argument('address', type=auto_int, help="address")
+parser_i2c_read.add_argument('reg', type=auto_int, help="reg")
+parser_i2c_read.add_argument('flags', type=auto_int, help="flags")
+parser_i2c_read.add_argument('size', type=auto_int, help="size")
+parser_i2c_read.set_defaults(func=handle_i2c_read)
+
+parser_i2c_write = subparsers.add_parser('i2c-write', help="run i2c write command")
+parser_i2c_write.add_argument('bus', type=auto_int, help="bus")
+parser_i2c_write.add_argument('address', type=auto_int, help="address")
+parser_i2c_write.add_argument('reg', type=auto_int, help="reg")
+parser_i2c_write.add_argument('flags', type=auto_int, help="flags")
+parser_i2c_write.add_argument('data', help="data")
+parser_i2c_write.set_defaults(func=handle_i2c_write)
+
parser_reset = subparsers.add_parser('reset', help="run reset command")
parser_reset_force = parser_reset.add_mutually_exclusive_group(required=False)
parser_reset_force.add_argument('--force', dest='force', action='store_true')
diff --git a/scripts/remote/messages.py b/scripts/remote/messages.py
index 729f2e617..bc40cbcc3 100644
--- a/scripts/remote/messages.py
+++ b/scripts/remote/messages.py
@@ -22,6 +22,10 @@ class BBType(object):
mw = 12
mw_return = 13
reset = 14
+ i2c_read = 15
+ i2c_read_return = 16
+ i2c_write = 17
+ i2c_write_return = 18
class BBPacket(object):
@@ -257,3 +261,85 @@ class BBPacketReset(BBPacket):
def _pack_payload(self):
return struct.pack("?", self.force)
+
+
+class BBPacketI2cRead(BBPacket):
+ def __init__(self, raw=None, bus=None, addr=None, reg=None, flags=None, size=None):
+ self.bus = bus
+ self.addr = addr
+ self.reg = reg
+ self.flags = flags
+ self.size = size
+ super(BBPacketI2cRead, self).__init__(BBType.i2c_read, raw=raw)
+
+ def __repr__(self):
+ return "BBPacketI2cRead(bus=0x%x,addr=0x%x,reg=0x%x,flags=0x%x,size=%u)" % (self.bus, self.addr, self.reg, self.flags, self.size)
+
+ def _unpack_payload(self, payload):
+ buffer_offset, self.bus, self.addr, self.reg, self.flags, self.size = struct.unpack("!HBBHBH", payload[:9])
+
+ def _pack_payload(self):
+ # header size is always 4 bytes (HH) and we have 9 bytes of fixed data (HBBHBH), so buffer offset is 13
+ return struct.pack("!HBBHBH", 13, self.bus, self.addr, self.reg, self.flags, self.size)
+
+
+class BBPacketI2cReadReturn(BBPacket):
+ def __init__(self, raw=None, exit_code=None, data=None):
+ self.exit_code = exit_code
+ self.data = data
+ super(BBPacketI2cReadReturn, self).__init__(BBType.i2c_read_return, raw=raw)
+
+ def __repr__(self):
+ return "BBPacketI2cReadReturn(exit_code=%i, data=%s)" % (self.exit_code, binascii.hexlify(self.data))
+
+ def _unpack_payload(self, payload):
+ buffer_offset, self.exit_code, data_size, data_offset = struct.unpack("!HLHH", payload[:10])
+ # header size is always 4 bytes (HH), so adjust the absolute data offset without the header size
+ absolute_data_offset = buffer_offset + data_offset - 4
+ self.data = payload[absolute_data_offset:absolute_data_offset + data_size]
+
+ def _pack_payload(self):
+ # header size is always 4 bytes (HH) and we have 10 bytes of fixed data (HLHH), so buffer offset is 14
+ return struct.pack("!HLHH%ds" % len(self.data), 14, self.exit_code, len(self.data), 0, self.data)
+ return self.text
+
+
+class BBPacketI2cWrite(BBPacket):
+ def __init__(self, raw=None, bus=None, addr=None, reg=None, flags=None, data=None):
+ self.bus = bus
+ self.addr = addr
+ self.reg = reg
+ self.flags = flags
+ self.data = data
+ super(BBPacketI2cWrite, self).__init__(BBType.i2c_write, raw=raw)
+
+ def __repr__(self):
+ return "BBPacketI2cWrite(bus=0x%x,addr=0x%x,reg=0x%x,flags=0x%x,data=%r)" % (self.bus, self.addr, self.reg, self.flags, self.data)
+
+ def _unpack_payload(self, payload):
+ buffer_offset, self.bus, self.addr, self.reg, self.flags, data_size, data_offset = struct.unpack("!HBBHBHH", payload[:11])
+ # header size is always 4 bytes (HH), so adjust the absolute data offset without the header size
+ absolute_data_offset = buffer_offset + data_offset - 4
+ self.data = payload[absolute_data_offset:absolute_data_offset+data_size]
+
+ def _pack_payload(self):
+ # header size is always 4 bytes (HH) and we have 11 bytes of fixed data (HBBHBHH), so buffer offset is 15
+ data_size = len(self.data)
+ return struct.pack("!HBBHBHH%ds" % data_size, 15, self.bus, self.addr, self.reg, self.flags, data_size, 0, self.data)
+
+
+class BBPacketI2cWriteReturn(BBPacket):
+ def __init__(self, raw=None, exit_code=None, written=None):
+ self.exit_code = exit_code
+ self.written = written
+ super(BBPacketI2cWriteReturn, self).__init__(BBType.i2c_write_return, raw=raw)
+
+ def __repr__(self):
+ return "BBPacketI2cWriteReturn(exit_code=%i, written=%i)" % (self.exit_code, self.written)
+
+ def _unpack_payload(self, payload):
+ buffer_offset, self.exit_code, self.written = struct.unpack("!HLH", payload[:8])
+
+ def _pack_payload(self):
+ # header size is always 4 bytes (HH) and we have 8 bytes of fixed data (HLH), so buffer offset is 14
+ return struct.pack("!HLH", 12, self.exit_code, self.written)
--
2.19.0
_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox
^ permalink raw reply [flat|nested] 9+ messages in thread
* [PATCH v2 3/7] ratp: implement support for GPIO commands
2018-09-12 13:45 [PATCH v2 0/7] RATP i2c and GPIO support Aleksander Morgado
2018-09-12 13:45 ` [PATCH v2 1/7] ratp: implement i2c read/write support Aleksander Morgado
2018-09-12 13:45 ` [PATCH v2 2/7] bbremote: " Aleksander Morgado
@ 2018-09-12 13:45 ` Aleksander Morgado
2018-09-12 13:45 ` [PATCH v2 4/7] bbremote: implement support for GPIO operations Aleksander Morgado
` (4 subsequent siblings)
7 siblings, 0 replies; 9+ messages in thread
From: Aleksander Morgado @ 2018-09-12 13:45 UTC (permalink / raw)
To: barebox; +Cc: andrew.smirnov, Aleksander Morgado
Introduce three new RATP commands that allow getting and setting GPIO
values as well as configuring the direction of the GPIO pins.
Signed-off-by: Aleksander Morgado <aleksander@aleksander.es>
---
common/ratp/Kconfig | 10 ++-
common/ratp/Makefile | 1 +
common/ratp/gpio.c | 144 +++++++++++++++++++++++++++++++++++++++++++
include/ratp_bb.h | 6 ++
4 files changed, 160 insertions(+), 1 deletion(-)
create mode 100644 common/ratp/gpio.c
diff --git a/common/ratp/Kconfig b/common/ratp/Kconfig
index 4b28d5f9f..ddbbd955c 100644
--- a/common/ratp/Kconfig
+++ b/common/ratp/Kconfig
@@ -18,4 +18,12 @@ config RATP_CMD_I2C
depends on I2C
prompt "RATP i2c support"
help
- This option adds support for i2c read/write commands via RATP.
\ No newline at end of file
+ This option adds support for i2c read/write commands via RATP.
+
+config RATP_CMD_GPIO
+ bool
+ depends on RATP
+ depends on GENERIC_GPIO
+ prompt "RATP GPIO support"
+ help
+ This option adds support for GPIO get/set/direction commands via RATP.
\ No newline at end of file
diff --git a/common/ratp/Makefile b/common/ratp/Makefile
index b83c48327..71288bcb8 100644
--- a/common/ratp/Makefile
+++ b/common/ratp/Makefile
@@ -5,3 +5,4 @@ obj-y += md.o
obj-y += mw.o
obj-y += reset.o
obj-$(CONFIG_RATP_CMD_I2C) += i2c.o
+obj-$(CONFIG_RATP_CMD_GPIO) += gpio.o
diff --git a/common/ratp/gpio.c b/common/ratp/gpio.c
new file mode 100644
index 000000000..d2c527a40
--- /dev/null
+++ b/common/ratp/gpio.c
@@ -0,0 +1,144 @@
+/*
+ * Copyright (c) 2018 Sascha Hauer <s.hauer@pengutronix.de>, Pengutronix
+ * Copyright (c) 2018 Zodiac Inflight Innovations
+ *
+ * See file CREDITS for list of people who contributed to this
+ * project.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ */
+
+#define pr_fmt(fmt) "barebox-ratp: gpio: " fmt
+
+#include <common.h>
+#include <ratp_bb.h>
+#include <malloc.h>
+#include <environment.h>
+#include <gpio.h>
+#include <errno.h>
+
+struct ratp_bb_gpio_get_value_request {
+ struct ratp_bb header;
+ uint32_t gpio;
+} __packed;
+
+struct ratp_bb_gpio_get_value_response {
+ struct ratp_bb header;
+ uint8_t value;
+} __packed;
+
+static int ratp_cmd_gpio_get_value(const struct ratp_bb *req, int req_len,
+ struct ratp_bb **rsp, int *rsp_len)
+{
+ struct ratp_bb_gpio_get_value_request *gpio_req = (struct ratp_bb_gpio_get_value_request *)req;
+ struct ratp_bb_gpio_get_value_response *gpio_rsp;
+ int gpio_rsp_len;
+
+ if (req_len < sizeof(*gpio_req)) {
+ pr_err("get value request ignored: size mismatch (%d < %zu)\n", req_len, sizeof(*gpio_req));
+ return 2;
+ }
+
+ gpio_rsp_len = sizeof(struct ratp_bb_gpio_get_value_response);
+ gpio_rsp = xzalloc(gpio_rsp_len);
+ gpio_rsp->header.type = cpu_to_be16(BB_RATP_TYPE_GPIO_GET_VALUE_RETURN);
+ gpio_rsp->value = !!gpio_get_value(be32_to_cpu(gpio_req->gpio));
+
+ *rsp_len = gpio_rsp_len;
+ *rsp = (struct ratp_bb *)gpio_rsp;
+ return 0;
+}
+
+BAREBOX_RATP_CMD_START(GPIO_GET_VALUE)
+ .request_id = BB_RATP_TYPE_GPIO_GET_VALUE,
+ .response_id = BB_RATP_TYPE_GPIO_GET_VALUE_RETURN,
+ .cmd = ratp_cmd_gpio_get_value
+BAREBOX_RATP_CMD_END
+
+
+struct ratp_bb_gpio_set_value_request {
+ struct ratp_bb header;
+ uint32_t gpio;
+ uint8_t value;
+} __packed;
+
+static int ratp_cmd_gpio_set_value(const struct ratp_bb *req, int req_len,
+ struct ratp_bb **rsp, int *rsp_len)
+{
+ struct ratp_bb_gpio_set_value_request *gpio_req = (struct ratp_bb_gpio_set_value_request *)req;
+
+ if (req_len < sizeof(*gpio_req)) {
+ pr_err("set value request ignored: size mismatch (%d < %zu)\n", req_len, sizeof(*gpio_req));
+ return 2;
+ }
+
+ gpio_set_value(be32_to_cpu(gpio_req->gpio), gpio_req->value);
+
+ *rsp_len = sizeof(struct ratp_bb);
+ *rsp = xzalloc(*rsp_len);
+ (*rsp)->type = cpu_to_be16(BB_RATP_TYPE_GPIO_SET_VALUE_RETURN);
+ return 0;
+}
+
+BAREBOX_RATP_CMD_START(GPIO_SET_VALUE)
+ .request_id = BB_RATP_TYPE_GPIO_SET_VALUE,
+ .response_id = BB_RATP_TYPE_GPIO_SET_VALUE_RETURN,
+ .cmd = ratp_cmd_gpio_set_value
+BAREBOX_RATP_CMD_END
+
+
+struct ratp_bb_gpio_set_direction_request {
+ struct ratp_bb header;
+ uint32_t gpio;
+ uint8_t direction; /* 0: input, 1: output */
+ uint8_t value; /* applicable only if direction output */
+} __packed;
+
+struct ratp_bb_gpio_set_direction_response {
+ struct ratp_bb header;
+ uint32_t errno;
+} __packed;
+
+static int ratp_cmd_gpio_set_direction(const struct ratp_bb *req, int req_len,
+ struct ratp_bb **rsp, int *rsp_len)
+{
+ struct ratp_bb_gpio_set_direction_request *gpio_req = (struct ratp_bb_gpio_set_direction_request *)req;
+ struct ratp_bb_gpio_set_direction_response *gpio_rsp;
+ int gpio_rsp_len;
+ uint32_t gpio;
+ int ret;
+
+ if (req_len < sizeof(*gpio_req)) {
+ pr_err("set direction request ignored: size mismatch (%d < %zu)\n", req_len, sizeof(*gpio_req));
+ return 2;
+ }
+
+ gpio = be32_to_cpu(gpio_req->gpio);
+ if (gpio_req->direction)
+ ret = gpio_direction_output(gpio, gpio_req->value);
+ else
+ ret = gpio_direction_input(gpio);
+
+ gpio_rsp_len = sizeof(struct ratp_bb_gpio_set_direction_response);
+ gpio_rsp = xzalloc(gpio_rsp_len);
+ gpio_rsp->header.type = cpu_to_be16(BB_RATP_TYPE_GPIO_SET_DIRECTION_RETURN);
+ gpio_rsp->errno = (ret == 0 ? 0 : EIO);
+
+ *rsp_len = gpio_rsp_len;
+ *rsp = (struct ratp_bb *)gpio_rsp;
+ return 0;
+}
+
+BAREBOX_RATP_CMD_START(GPIO_SET_DIRECTION)
+ .request_id = BB_RATP_TYPE_GPIO_SET_DIRECTION,
+ .response_id = BB_RATP_TYPE_GPIO_SET_DIRECTION_RETURN,
+ .cmd = ratp_cmd_gpio_set_direction
+BAREBOX_RATP_CMD_END
diff --git a/include/ratp_bb.h b/include/ratp_bb.h
index 32b8040b6..b6699979b 100644
--- a/include/ratp_bb.h
+++ b/include/ratp_bb.h
@@ -21,6 +21,12 @@
#define BB_RATP_TYPE_I2C_READ_RETURN 16
#define BB_RATP_TYPE_I2C_WRITE 17
#define BB_RATP_TYPE_I2C_WRITE_RETURN 18
+#define BB_RATP_TYPE_GPIO_GET_VALUE 19
+#define BB_RATP_TYPE_GPIO_GET_VALUE_RETURN 20
+#define BB_RATP_TYPE_GPIO_SET_VALUE 21
+#define BB_RATP_TYPE_GPIO_SET_VALUE_RETURN 22
+#define BB_RATP_TYPE_GPIO_SET_DIRECTION 23
+#define BB_RATP_TYPE_GPIO_SET_DIRECTION_RETURN 24
struct ratp_bb {
uint16_t type;
--
2.19.0
_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox
^ permalink raw reply [flat|nested] 9+ messages in thread
* [PATCH v2 4/7] bbremote: implement support for GPIO operations
2018-09-12 13:45 [PATCH v2 0/7] RATP i2c and GPIO support Aleksander Morgado
` (2 preceding siblings ...)
2018-09-12 13:45 ` [PATCH v2 3/7] ratp: implement support for GPIO commands Aleksander Morgado
@ 2018-09-12 13:45 ` Aleksander Morgado
2018-09-12 13:45 ` [PATCH v2 5/7] ratp: use __packed instead of the full form Aleksander Morgado
` (3 subsequent siblings)
7 siblings, 0 replies; 9+ messages in thread
From: Aleksander Morgado @ 2018-09-12 13:45 UTC (permalink / raw)
To: barebox; +Cc: andrew.smirnov, Aleksander Morgado
Signed-off-by: Aleksander Morgado <aleksander@aleksander.es>
---
scripts/remote/controller.py | 36 ++++++++++++++
scripts/remote/main.py | 37 ++++++++++++++
scripts/remote/messages.py | 93 ++++++++++++++++++++++++++++++++++++
3 files changed, 166 insertions(+)
diff --git a/scripts/remote/controller.py b/scripts/remote/controller.py
index 10037b890..b4493591d 100644
--- a/scripts/remote/controller.py
+++ b/scripts/remote/controller.py
@@ -73,6 +73,24 @@ def unpack(data):
elif p_type == BBType.i2c_write_return:
logging.debug("received: i2c_write_return")
return BBPacketI2cWriteReturn(raw=data)
+ elif p_type == BBType.gpio_get_value:
+ logging.debug("received: gpio_get_value")
+ return BBPacketGpioGetValue(raw=data)
+ elif p_type == BBType.gpio_get_value_return:
+ logging.debug("received: gpio_get_value_return")
+ return BBPacketGpioGetValueReturn(raw=data)
+ elif p_type == BBType.gpio_set_value:
+ logging.debug("received: gpio_set_value")
+ return BBPacketGpioSetValue(raw=data)
+ elif p_type == BBType.gpio_set_value_return:
+ logging.debug("received: gpio_set_value_return")
+ return BBPacketGpioSetValueReturn(raw=data)
+ elif p_type == BBType.gpio_set_direction:
+ logging.debug("received: gpio_set_direction")
+ return BBPacketGpioSetDirection(raw=data)
+ elif p_type == BBType.gpio_set_direction_return:
+ logging.debug("received: gpio_set_direction_return")
+ return BBPacketGpioSetDirectionReturn(raw=data)
else:
logging.debug("received: UNKNOWN")
return BBPacket(raw=data)
@@ -163,6 +181,24 @@ class Controller(Thread):
logging.info("i2c write return: %r", r)
return (r.exit_code,r.written)
+ def gpio_get_value(self, gpio):
+ self._send(BBPacketGpioGetValue(gpio=gpio))
+ r = self._expect(BBPacketGpioGetValueReturn)
+ logging.info("gpio get value return: %r", r)
+ return r.value
+
+ def gpio_set_value(self, gpio, value):
+ self._send(BBPacketGpioSetValue(gpio=gpio, value=value))
+ r = self._expect(BBPacketGpioSetValueReturn)
+ logging.info("gpio set value return: %r", r)
+ return 0
+
+ def gpio_set_direction(self, gpio, direction, value):
+ self._send(BBPacketGpioSetDirection(gpio=gpio, direction=direction, value=value))
+ r = self._expect(BBPacketGpioSetDirectionReturn)
+ logging.info("gpio set direction return: %r", r)
+ return r.exit_code
+
def reset(self, force):
self._send(BBPacketReset(force=force))
diff --git a/scripts/remote/main.py b/scripts/remote/main.py
index 0f7783927..cef5d92ee 100644
--- a/scripts/remote/main.py
+++ b/scripts/remote/main.py
@@ -119,6 +119,28 @@ def handle_i2c_write(args):
return res
+def handle_gpio_get_value(args):
+ ctrl = get_controller(args)
+ value = ctrl.gpio_get_value(args.gpio)
+ print ("%u" % value);
+ ctrl.close()
+ return 0
+
+
+def handle_gpio_set_value(args):
+ ctrl = get_controller(args)
+ ctrl.gpio_set_value(args.gpio, args.value)
+ ctrl.close()
+ return 0
+
+
+def handle_gpio_set_direction(args):
+ ctrl = get_controller(args)
+ res = ctrl.gpio_set_direction(args.gpio, args.direction, args.value)
+ ctrl.close()
+ return res
+
+
def handle_reset(args):
ctrl = get_controller(args)
ctrl.reset(args.force)
@@ -225,6 +247,21 @@ parser_i2c_write.add_argument('flags', type=auto_int, help="flags")
parser_i2c_write.add_argument('data', help="data")
parser_i2c_write.set_defaults(func=handle_i2c_write)
+parser_gpio_get_value = subparsers.add_parser('gpio-get-value', help="run gpio get value command")
+parser_gpio_get_value.add_argument('gpio', type=auto_int, help="gpio")
+parser_gpio_get_value.set_defaults(func=handle_gpio_get_value)
+
+parser_gpio_set_value = subparsers.add_parser('gpio-set-value', help="run gpio set value command")
+parser_gpio_set_value.add_argument('gpio', type=auto_int, help="gpio")
+parser_gpio_set_value.add_argument('value', type=auto_int, help="value")
+parser_gpio_set_value.set_defaults(func=handle_gpio_set_value)
+
+parser_gpio_set_direction = subparsers.add_parser('gpio-set-direction', help="run gpio set direction command")
+parser_gpio_set_direction.add_argument('gpio', type=auto_int, help="gpio")
+parser_gpio_set_direction.add_argument('direction', type=auto_int, help="direction (0: input, 1: output)")
+parser_gpio_set_direction.add_argument('value', type=auto_int, help="value (if output)")
+parser_gpio_set_direction.set_defaults(func=handle_gpio_set_direction)
+
parser_reset = subparsers.add_parser('reset', help="run reset command")
parser_reset_force = parser_reset.add_mutually_exclusive_group(required=False)
parser_reset_force.add_argument('--force', dest='force', action='store_true')
diff --git a/scripts/remote/messages.py b/scripts/remote/messages.py
index bc40cbcc3..abd331c8b 100644
--- a/scripts/remote/messages.py
+++ b/scripts/remote/messages.py
@@ -26,6 +26,12 @@ class BBType(object):
i2c_read_return = 16
i2c_write = 17
i2c_write_return = 18
+ gpio_get_value = 19
+ gpio_get_value_return = 20
+ gpio_set_value = 21
+ gpio_set_value_return = 22
+ gpio_set_direction = 23
+ gpio_set_direction_return = 24
class BBPacket(object):
@@ -343,3 +349,90 @@ class BBPacketI2cWriteReturn(BBPacket):
def _pack_payload(self):
# header size is always 4 bytes (HH) and we have 8 bytes of fixed data (HLH), so buffer offset is 14
return struct.pack("!HLH", 12, self.exit_code, self.written)
+
+
+class BBPacketGpioGetValue(BBPacket):
+ def __init__(self, raw=None, gpio=None):
+ self.gpio = gpio
+ super(BBPacketGpioGetValue, self).__init__(BBType.gpio_get_value, raw=raw)
+
+ def __repr__(self):
+ return "BBPacketGpioGetValue(gpio=%u)" % (self.gpio)
+
+ def _unpack_payload(self, payload):
+ self.gpio = struct.unpack("!L", payload[:4])
+
+ def _pack_payload(self):
+ return struct.pack("!L", self.gpio)
+
+
+class BBPacketGpioGetValueReturn(BBPacket):
+ def __init__(self, raw=None, value=None):
+ self.value = value
+ super(BBPacketGpioGetValueReturn, self).__init__(BBType.gpio_get_value_return, raw=raw)
+
+ def __repr__(self):
+ return "BBPacketGpioGetValueReturn(value=%u)" % (self.value)
+
+ def _unpack_payload(self, payload):
+ self.value = struct.unpack("!B", payload[:1])
+
+ def _pack_payload(self):
+ return struct.pack("!B", self.value)
+
+
+class BBPacketGpioSetValue(BBPacket):
+ def __init__(self, raw=None, gpio=None, value=None):
+ self.gpio = gpio
+ self.value = value
+ super(BBPacketGpioSetValue, self).__init__(BBType.gpio_set_value, raw=raw)
+
+ def __repr__(self):
+ return "BBPacketGpioSetValue(gpio=%u,value=%u)" % (self.gpio, self.value)
+
+ def _unpack_payload(self, payload):
+ self.gpio = struct.unpack("!LB", payload[:5])
+
+ def _pack_payload(self):
+ return struct.pack("!LB", self.gpio, self.value)
+
+
+class BBPacketGpioSetValueReturn(BBPacket):
+ def __init__(self, raw=None, value=None):
+ self.value = value
+ super(BBPacketGpioSetValueReturn, self).__init__(BBType.gpio_set_value_return, raw=raw)
+
+ def __repr__(self):
+ return "BBPacketGpioSetValueReturn()"
+
+
+class BBPacketGpioSetDirection(BBPacket):
+ def __init__(self, raw=None, gpio=None, direction=None, value=None):
+ self.gpio = gpio
+ self.direction = direction
+ self.value = value
+ super(BBPacketGpioSetDirection, self).__init__(BBType.gpio_set_direction, raw=raw)
+
+ def __repr__(self):
+ return "BBPacketGpioSetDirection(gpio=%u,direction=%u,value=%u)" % (self.gpio, self.direction, self.value)
+
+ def _unpack_payload(self, payload):
+ self.gpio = struct.unpack("!LBB", payload[:6])
+
+ def _pack_payload(self):
+ return struct.pack("!LBB", self.gpio, self.direction, self.value)
+
+
+class BBPacketGpioSetDirectionReturn(BBPacket):
+ def __init__(self, raw=None, exit_code=None):
+ self.exit_code = exit_code
+ super(BBPacketGpioSetDirectionReturn, self).__init__(BBType.gpio_set_direction_return, raw=raw)
+
+ def __repr__(self):
+ return "BBPacketGpioSetDirectionReturn(exit_code=%u)" % (self.exit_code)
+
+ def _unpack_payload(self, payload):
+ self.exit_code = struct.unpack("!L", payload[:4])
+
+ def _pack_payload(self):
+ return struct.pack("!L", self.exit_code)
--
2.19.0
_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox
^ permalink raw reply [flat|nested] 9+ messages in thread
* [PATCH v2 5/7] ratp: use __packed instead of the full form
2018-09-12 13:45 [PATCH v2 0/7] RATP i2c and GPIO support Aleksander Morgado
` (3 preceding siblings ...)
2018-09-12 13:45 ` [PATCH v2 4/7] bbremote: implement support for GPIO operations Aleksander Morgado
@ 2018-09-12 13:45 ` Aleksander Morgado
2018-09-12 13:45 ` [PATCH v2 6/7] ratp: use pr_ macros to print messages Aleksander Morgado
` (2 subsequent siblings)
7 siblings, 0 replies; 9+ messages in thread
From: Aleksander Morgado @ 2018-09-12 13:45 UTC (permalink / raw)
To: barebox; +Cc: andrew.smirnov, Aleksander Morgado
Just a minor coding style change to follow the suggestions given in
patch reviews for other RATP commands.
Signed-off-by: Aleksander Morgado <aleksander@aleksander.es>
---
common/ratp/md.c | 4 ++--
common/ratp/mw.c | 4 ++--
common/ratp/reset.c | 2 +-
3 files changed, 5 insertions(+), 5 deletions(-)
diff --git a/common/ratp/md.c b/common/ratp/md.c
index 9b8fc2bc5..5b877947c 100644
--- a/common/ratp/md.c
+++ b/common/ratp/md.c
@@ -46,7 +46,7 @@ struct ratp_bb_md_request {
uint16_t path_size;
uint16_t path_offset;
uint8_t buffer[];
-} __attribute__((packed));
+} __packed;
struct ratp_bb_md_response {
struct ratp_bb header;
@@ -55,7 +55,7 @@ struct ratp_bb_md_response {
uint16_t data_size;
uint16_t data_offset;
uint8_t buffer[];
-} __attribute__((packed));
+} __packed;
extern char *mem_rw_buf;
diff --git a/common/ratp/mw.c b/common/ratp/mw.c
index 7d6df3d0a..3234d7dac 100644
--- a/common/ratp/mw.c
+++ b/common/ratp/mw.c
@@ -47,7 +47,7 @@ struct ratp_bb_mw_request {
uint16_t data_size;
uint16_t data_offset;
uint8_t buffer[];
-} __attribute__((packed));
+} __packed;
struct ratp_bb_mw_response {
struct ratp_bb header;
@@ -55,7 +55,7 @@ struct ratp_bb_mw_response {
uint32_t errno;
uint16_t written;
uint8_t buffer[];
-} __attribute__((packed));
+} __packed;
static int ratp_cmd_mw(const struct ratp_bb *req, int req_len,
struct ratp_bb **rsp, int *rsp_len)
diff --git a/common/ratp/reset.c b/common/ratp/reset.c
index ca8be4e62..60b6ff536 100644
--- a/common/ratp/reset.c
+++ b/common/ratp/reset.c
@@ -27,7 +27,7 @@
struct ratp_bb_reset {
struct ratp_bb header;
uint8_t force;
-} __attribute__((packed));
+} __packed;
static int ratp_cmd_reset(const struct ratp_bb *req, int req_len,
struct ratp_bb **rsp, int *rsp_len)
--
2.19.0
_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox
^ permalink raw reply [flat|nested] 9+ messages in thread
* [PATCH v2 6/7] ratp: use pr_ macros to print messages
2018-09-12 13:45 [PATCH v2 0/7] RATP i2c and GPIO support Aleksander Morgado
` (4 preceding siblings ...)
2018-09-12 13:45 ` [PATCH v2 5/7] ratp: use __packed instead of the full form Aleksander Morgado
@ 2018-09-12 13:45 ` Aleksander Morgado
2018-09-12 13:45 ` [PATCH v2 7/7] ratp: fix incorrect whitespaces in method calls Aleksander Morgado
2018-09-17 7:50 ` [PATCH v2 0/7] RATP i2c and GPIO support Sascha Hauer
7 siblings, 0 replies; 9+ messages in thread
From: Aleksander Morgado @ 2018-09-12 13:45 UTC (permalink / raw)
To: barebox; +Cc: andrew.smirnov, Aleksander Morgado
Following suggestions in other patch reviews, the RAPT commands are
updated to use pr_err() instead of plain printf() to report errors
to the user.
Signed-off-by: Aleksander Morgado <aleksander@aleksander.es>
---
common/ratp/md.c | 12 +++++++-----
common/ratp/mw.c | 14 ++++++++------
common/ratp/reset.c | 6 ++++--
3 files changed, 19 insertions(+), 13 deletions(-)
diff --git a/common/ratp/md.c b/common/ratp/md.c
index 5b877947c..2e5a956cb 100644
--- a/common/ratp/md.c
+++ b/common/ratp/md.c
@@ -15,6 +15,8 @@
*
*/
+#define pr_fmt(fmt) "barebox-ratp: md: " fmt
+
#include <common.h>
#include <ratp_bb.h>
#include <init.h>
@@ -121,7 +123,7 @@ static int ratp_cmd_md(const struct ratp_bb *req, int req_len,
/* At least message header should be valid */
if (req_len < sizeof(*md_req)) {
- printf("ratp md ignored: size mismatch (%d < %zu)\n",
+ pr_err("ignored: size mismatch (%d < %zu)\n",
req_len, sizeof (*md_req));
ret = -EINVAL;
goto out;
@@ -130,7 +132,7 @@ static int ratp_cmd_md(const struct ratp_bb *req, int req_len,
/* Validate buffer position and size */
buffer_offset = be16_to_cpu(md_req->buffer_offset);
if (req_len < buffer_offset) {
- printf("ratp md ignored: invalid buffer offset (%d < %hu)\n",
+ pr_err("ignored: invalid buffer offset (%d < %hu)\n",
req_len, buffer_offset);
ret = -EINVAL;
goto out;
@@ -141,20 +143,20 @@ static int ratp_cmd_md(const struct ratp_bb *req, int req_len,
/* Validate path position and size */
path_offset = be16_to_cpu(md_req->path_offset);
if (path_offset != 0) {
- printf("ratp md ignored: invalid path offset\n");
+ pr_err("ignored: invalid path offset\n");
ret = -EINVAL;
goto out;
}
path_size = be16_to_cpu(md_req->path_size);
if (!path_size) {
- printf("ratp md ignored: no filepath given\n");
+ pr_err("ignored: no filepath given\n");
ret = -EINVAL;
goto out;
}
/* Validate buffer size */
if (buffer_size < path_size) {
- printf("ratp mw ignored: size mismatch (%d < %hu): path may not be fully given\n",
+ pr_err("ignored: size mismatch (%d < %hu): path may not be fully given\n",
req_len, path_size);
ret = -EINVAL;
goto out;
diff --git a/common/ratp/mw.c b/common/ratp/mw.c
index 3234d7dac..0579da3c1 100644
--- a/common/ratp/mw.c
+++ b/common/ratp/mw.c
@@ -16,6 +16,8 @@
*
*/
+#define pr_fmt(fmt) "barebox-ratp: mw: " fmt
+
#include <common.h>
#include <ratp_bb.h>
#include <init.h>
@@ -77,7 +79,7 @@ static int ratp_cmd_mw(const struct ratp_bb *req, int req_len,
/* At least message header should be valid */
if (req_len < sizeof(*mw_req)) {
- printf("ratp mw ignored: size mismatch (%d < %zu)\n",
+ pr_err("ignored: size mismatch (%d < %zu)\n",
req_len, sizeof (*mw_req));
ret = -EINVAL;
goto out;
@@ -86,7 +88,7 @@ static int ratp_cmd_mw(const struct ratp_bb *req, int req_len,
/* Validate buffer position and size */
buffer_offset = be16_to_cpu(mw_req->buffer_offset);
if (req_len < buffer_offset) {
- printf("ratp mw ignored: invalid buffer offset (%d < %hu)\n",
+ pr_err("ignored: invalid buffer offset (%d < %hu)\n",
req_len, buffer_offset);
ret = -EINVAL;
goto out;
@@ -97,13 +99,13 @@ static int ratp_cmd_mw(const struct ratp_bb *req, int req_len,
/* Validate path position and size */
path_offset = be16_to_cpu(mw_req->path_offset);
if (path_offset != 0) {
- printf("ratp mw ignored: invalid path offset\n");
+ pr_err("ignored: invalid path offset\n");
ret = -EINVAL;
goto out;
}
path_size = be16_to_cpu(mw_req->path_size);
if (!path_size) {
- printf("ratp mw ignored: no filepath given\n");
+ pr_err("ignored: no filepath given\n");
ret = -EINVAL;
goto out;
}
@@ -111,7 +113,7 @@ static int ratp_cmd_mw(const struct ratp_bb *req, int req_len,
/* Validate data position and size */
data_offset = be16_to_cpu(mw_req->data_offset);
if (data_offset != (path_offset + path_size)) {
- printf("ratp mw ignored: invalid path offset\n");
+ pr_err("ignored: invalid path offset\n");
ret = -EINVAL;
goto out;
}
@@ -123,7 +125,7 @@ static int ratp_cmd_mw(const struct ratp_bb *req, int req_len,
/* Validate buffer size */
if (buffer_size < (path_size + data_size)) {
- printf("ratp mw ignored: size mismatch (%d < %hu): path or data not be fully given\n",
+ pr_err("ignored: size mismatch (%d < %hu): path or data not be fully given\n",
req_len, path_size + data_size);
ret = -EINVAL;
goto out;
diff --git a/common/ratp/reset.c b/common/ratp/reset.c
index 60b6ff536..5439f344f 100644
--- a/common/ratp/reset.c
+++ b/common/ratp/reset.c
@@ -17,6 +17,8 @@
*
*/
+#define pr_fmt(fmt) "barebox-ratp: reset: " fmt
+
#include <common.h>
#include <command.h>
#include <ratp_bb.h>
@@ -35,11 +37,11 @@ static int ratp_cmd_reset(const struct ratp_bb *req, int req_len,
struct ratp_bb_reset *reset_req = (struct ratp_bb_reset *)req;
if (req_len < sizeof (*reset_req)) {
- printf ("ratp reset ignored: size mismatch (%d < %zu)\n", req_len, sizeof (*reset_req));
+ pr_err("ignored: size mismatch (%d < %zu)\n", req_len, sizeof (*reset_req));
return 2;
}
- debug("running reset %s\n", reset_req->force ? "(forced)" : "");
+ pr_debug("running %s\n", reset_req->force ? "(forced)" : "");
if (!reset_req->force)
shutdown_barebox();
--
2.19.0
_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox
^ permalink raw reply [flat|nested] 9+ messages in thread
* [PATCH v2 7/7] ratp: fix incorrect whitespaces in method calls
2018-09-12 13:45 [PATCH v2 0/7] RATP i2c and GPIO support Aleksander Morgado
` (5 preceding siblings ...)
2018-09-12 13:45 ` [PATCH v2 6/7] ratp: use pr_ macros to print messages Aleksander Morgado
@ 2018-09-12 13:45 ` Aleksander Morgado
2018-09-17 7:50 ` [PATCH v2 0/7] RATP i2c and GPIO support Sascha Hauer
7 siblings, 0 replies; 9+ messages in thread
From: Aleksander Morgado @ 2018-09-12 13:45 UTC (permalink / raw)
To: barebox; +Cc: andrew.smirnov, Aleksander Morgado
This is a simple coding style fix to avoid the whitespace before the
open-parenthesis in method calls.
Signed-off-by: Aleksander Morgado <aleksander@aleksander.es>
---
common/ratp/getenv.c | 12 ++++++------
common/ratp/md.c | 8 ++++----
common/ratp/mw.c | 6 +++---
common/ratp/reset.c | 4 ++--
4 files changed, 15 insertions(+), 15 deletions(-)
diff --git a/common/ratp/getenv.c b/common/ratp/getenv.c
index b40963488..fdb4b0b28 100644
--- a/common/ratp/getenv.c
+++ b/common/ratp/getenv.c
@@ -27,20 +27,20 @@
static int ratp_cmd_getenv(const struct ratp_bb *req, int req_len,
struct ratp_bb **rsp, int *rsp_len)
{
- int dlen = req_len - sizeof (struct ratp_bb);
+ int dlen = req_len - sizeof(struct ratp_bb);
char *varname;
const char *value;
- varname = xstrndup ((const char *)req->data, dlen);
- value = getenv (varname);
- free (varname);
+ varname = xstrndup((const char *)req->data, dlen);
+ value = getenv(varname);
+ free(varname);
- dlen = strlen (value);
+ dlen = strlen(value);
*rsp_len = sizeof(struct ratp_bb) + dlen;
*rsp = xzalloc(*rsp_len);
(*rsp)->type = cpu_to_be16(BB_RATP_TYPE_GETENV_RETURN);
- memcpy ((*rsp)->data, value, dlen);
+ memcpy((*rsp)->data, value, dlen);
return 0;
}
diff --git a/common/ratp/md.c b/common/ratp/md.c
index 2e5a956cb..9ce7e99df 100644
--- a/common/ratp/md.c
+++ b/common/ratp/md.c
@@ -124,7 +124,7 @@ static int ratp_cmd_md(const struct ratp_bb *req, int req_len,
/* At least message header should be valid */
if (req_len < sizeof(*md_req)) {
pr_err("ignored: size mismatch (%d < %zu)\n",
- req_len, sizeof (*md_req));
+ req_len, sizeof(*md_req));
ret = -EINVAL;
goto out;
}
@@ -162,8 +162,8 @@ static int ratp_cmd_md(const struct ratp_bb *req, int req_len,
goto out;
}
- addr = be16_to_cpu (md_req->addr);
- size = be16_to_cpu (md_req->size);
+ addr = be16_to_cpu(md_req->addr);
+ size = be16_to_cpu(md_req->size);
path = xstrndup((const char *)&buffer[path_offset], path_size);
out:
@@ -193,7 +193,7 @@ out:
*rsp = (struct ratp_bb *)md_rsp;
*rsp_len = md_rsp_len;
- free (path);
+ free(path);
return ret;
}
diff --git a/common/ratp/mw.c b/common/ratp/mw.c
index 0579da3c1..55e79bbaf 100644
--- a/common/ratp/mw.c
+++ b/common/ratp/mw.c
@@ -80,7 +80,7 @@ static int ratp_cmd_mw(const struct ratp_bb *req, int req_len,
/* At least message header should be valid */
if (req_len < sizeof(*mw_req)) {
pr_err("ignored: size mismatch (%d < %zu)\n",
- req_len, sizeof (*mw_req));
+ req_len, sizeof(*mw_req));
ret = -EINVAL;
goto out;
}
@@ -131,7 +131,7 @@ static int ratp_cmd_mw(const struct ratp_bb *req, int req_len,
goto out;
}
- addr = be16_to_cpu (mw_req->addr);
+ addr = be16_to_cpu(mw_req->addr);
path = xstrndup((const char *)&buffer[path_offset], path_size);
fd = open_and_lseek(path, O_RWSIZE_1 | O_WRONLY, addr);
@@ -164,7 +164,7 @@ out:
*rsp = (struct ratp_bb *)mw_rsp;
*rsp_len = sizeof(*mw_rsp);
- free (path);
+ free(path);
return ret;
}
diff --git a/common/ratp/reset.c b/common/ratp/reset.c
index 5439f344f..d0229d5d6 100644
--- a/common/ratp/reset.c
+++ b/common/ratp/reset.c
@@ -36,8 +36,8 @@ static int ratp_cmd_reset(const struct ratp_bb *req, int req_len,
{
struct ratp_bb_reset *reset_req = (struct ratp_bb_reset *)req;
- if (req_len < sizeof (*reset_req)) {
- pr_err("ignored: size mismatch (%d < %zu)\n", req_len, sizeof (*reset_req));
+ if (req_len < sizeof(*reset_req)) {
+ pr_err("ignored: size mismatch (%d < %zu)\n", req_len, sizeof(*reset_req));
return 2;
}
--
2.19.0
_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH v2 0/7] RATP i2c and GPIO support
2018-09-12 13:45 [PATCH v2 0/7] RATP i2c and GPIO support Aleksander Morgado
` (6 preceding siblings ...)
2018-09-12 13:45 ` [PATCH v2 7/7] ratp: fix incorrect whitespaces in method calls Aleksander Morgado
@ 2018-09-17 7:50 ` Sascha Hauer
7 siblings, 0 replies; 9+ messages in thread
From: Sascha Hauer @ 2018-09-17 7:50 UTC (permalink / raw)
To: Aleksander Morgado; +Cc: andrew.smirnov, barebox
On Wed, Sep 12, 2018 at 03:45:43PM +0200, Aleksander Morgado wrote:
> This series of patches implements support for i2c and GPIO operations via RATP.
>
> It addresses most of the comments from the first series review, except for Sascha's suggestion to implement name resolving for the i2c buses and Andrey's suggestion to prepare a new helper method to initialize the i2c_client structure. I already have a patch for the latter, but will send it for review once these have been accepted and merged, as it's really a bit orthogonal to the whole RATP logic.
>
> Another different w.r.t. v1 apart from addressing the comments is the review is that I made the RATP i2c support depend on CONFIG_I2C and the RATP GPIO support on CONFIG_GENERIC_GPIO. That was missing, and was breaking build if RATP was selected but no i2c or GPIO support was included in the build.
>
> Comments welcome!
>
> Aleksander Morgado (7):
> ratp: implement i2c read/write support
> bbremote: implement i2c read/write support
> ratp: implement support for GPIO commands
> bbremote: implement support for GPIO operations
> ratp: use __packed instead of the full form
> ratp: use pr_ macros to print messages
> ratp: fix incorrect whitespaces in method calls
Applied, thanks
Sascha
--
Pengutronix e.K. | |
Industrial Linux Solutions | http://www.pengutronix.de/ |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0 |
Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 |
_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox
^ permalink raw reply [flat|nested] 9+ messages in thread
end of thread, other threads:[~2018-09-17 7:51 UTC | newest]
Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-09-12 13:45 [PATCH v2 0/7] RATP i2c and GPIO support Aleksander Morgado
2018-09-12 13:45 ` [PATCH v2 1/7] ratp: implement i2c read/write support Aleksander Morgado
2018-09-12 13:45 ` [PATCH v2 2/7] bbremote: " Aleksander Morgado
2018-09-12 13:45 ` [PATCH v2 3/7] ratp: implement support for GPIO commands Aleksander Morgado
2018-09-12 13:45 ` [PATCH v2 4/7] bbremote: implement support for GPIO operations Aleksander Morgado
2018-09-12 13:45 ` [PATCH v2 5/7] ratp: use __packed instead of the full form Aleksander Morgado
2018-09-12 13:45 ` [PATCH v2 6/7] ratp: use pr_ macros to print messages Aleksander Morgado
2018-09-12 13:45 ` [PATCH v2 7/7] ratp: fix incorrect whitespaces in method calls Aleksander Morgado
2018-09-17 7:50 ` [PATCH v2 0/7] RATP i2c and GPIO support Sascha Hauer
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox