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 | |
| 7 | extern "C" { |
Andrew Jeffery | 26558db | 2018-08-10 00:22:38 +0930 | [diff] [blame] | 8 | #include "mboxd.h" |
Andrew Jeffery | c7d1947 | 2018-08-08 11:43:08 +0930 | [diff] [blame] | 9 | #include "protocol.h" |
| 10 | #include "vpnor/protocol.h" |
| 11 | } |
| 12 | |
| 13 | /* We need to hijack create_window for vpnor */ |
| 14 | |
| 15 | static const struct protocol_ops protocol_ops_v1 = { |
| 16 | .reset = protocol_v1_reset, |
| 17 | .get_info = protocol_v1_get_info, |
| 18 | .get_flash_info = protocol_v1_get_flash_info, |
| 19 | .create_window = protocol_v1_vpnor_create_window, |
| 20 | .mark_dirty = protocol_v1_mark_dirty, |
| 21 | .erase = NULL, |
| 22 | .flush = protocol_v1_flush, |
| 23 | .close = protocol_v1_close, |
| 24 | .ack = protocol_v1_ack, |
| 25 | }; |
| 26 | |
| 27 | static const struct protocol_ops protocol_ops_v2 = { |
| 28 | .reset = protocol_v1_reset, |
| 29 | .get_info = protocol_v2_get_info, |
| 30 | .get_flash_info = protocol_v2_get_flash_info, |
| 31 | .create_window = protocol_v2_vpnor_create_window, |
| 32 | .mark_dirty = protocol_v2_mark_dirty, |
| 33 | .erase = protocol_v2_erase, |
| 34 | .flush = protocol_v2_flush, |
| 35 | .close = protocol_v2_close, |
| 36 | .ack = protocol_v1_ack, |
| 37 | }; |
| 38 | |
William A. Kennington III | d5f1d40 | 2018-10-11 13:55:04 -0700 | [diff] [blame] | 39 | static const struct protocol_ops* protocol_ops_map[] = { |
Andrew Jeffery | c7d1947 | 2018-08-08 11:43:08 +0930 | [diff] [blame] | 40 | [0] = NULL, |
| 41 | [1] = &protocol_ops_v1, |
| 42 | [2] = &protocol_ops_v2, |
| 43 | }; |
| 44 | |
William A. Kennington III | d5f1d40 | 2018-10-11 13:55:04 -0700 | [diff] [blame] | 45 | int protocol_negotiate_version(struct mbox_context* context, uint8_t requested) |
Andrew Jeffery | c7d1947 | 2018-08-08 11:43:08 +0930 | [diff] [blame] | 46 | { |
| 47 | /* Check we support the version requested */ |
| 48 | if (requested < API_MIN_VERSION) |
| 49 | return -EINVAL; |
| 50 | |
| 51 | uint8_t version = |
| 52 | (requested > API_MAX_VERSION) ? API_MAX_VERSION : requested; |
| 53 | context->version = static_cast<enum api_version>(version); |
| 54 | |
| 55 | context->protocol = protocol_ops_map[context->version]; |
| 56 | |
| 57 | return context->version; |
| 58 | } |