Andrew Jeffery | c7d1947 | 2018-08-08 11:43:08 +0930 | [diff] [blame] | 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // Copyright (C) 2018 IBM Corp. |
| 3 | #include "config.h" |
| 4 | |
| 5 | #include <errno.h> |
| 6 | |
Andrew Jeffery | 26558db | 2018-08-10 00:22:38 +0930 | [diff] [blame] | 7 | #include "mboxd.h" |
Andrew Jeffery | c7d1947 | 2018-08-08 11:43:08 +0930 | [diff] [blame] | 8 | #include "protocol.h" |
| 9 | |
| 10 | static const struct protocol_ops protocol_ops_v1 = { |
| 11 | .reset = protocol_v1_reset, |
| 12 | .get_info = protocol_v1_get_info, |
| 13 | .get_flash_info = protocol_v1_get_flash_info, |
| 14 | .create_window = protocol_v1_create_window, |
| 15 | .mark_dirty = protocol_v1_mark_dirty, |
| 16 | .erase = NULL, |
| 17 | .flush = protocol_v1_flush, |
| 18 | .close = protocol_v1_close, |
| 19 | .ack = protocol_v1_ack, |
| 20 | }; |
| 21 | |
| 22 | static const struct protocol_ops protocol_ops_v2 = { |
| 23 | .reset = protocol_v1_reset, |
| 24 | .get_info = protocol_v2_get_info, |
| 25 | .get_flash_info = protocol_v2_get_flash_info, |
| 26 | .create_window = protocol_v2_create_window, |
| 27 | .mark_dirty = protocol_v2_mark_dirty, |
| 28 | .erase = protocol_v2_erase, |
| 29 | .flush = protocol_v2_flush, |
| 30 | .close = protocol_v2_close, |
| 31 | .ack = protocol_v1_ack, |
| 32 | }; |
| 33 | |
| 34 | static const struct protocol_ops *protocol_ops_map[] = { |
| 35 | [0] = NULL, |
| 36 | [1] = &protocol_ops_v1, |
| 37 | [2] = &protocol_ops_v2, |
| 38 | }; |
| 39 | |
| 40 | int protocol_negotiate_version(struct mbox_context *context, |
| 41 | uint8_t requested) |
| 42 | { |
| 43 | /* Check we support the version requested */ |
| 44 | if (requested < API_MIN_VERSION) |
| 45 | return -EINVAL; |
| 46 | |
| 47 | context->version = (requested > API_MAX_VERSION) ? |
| 48 | API_MAX_VERSION : requested; |
| 49 | |
| 50 | context->protocol = protocol_ops_map[context->version]; |
| 51 | |
| 52 | return context->version; |
| 53 | } |