Stefan Wahren | 291ab06 | 2014-09-26 22:21:21 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2011, 2012, Atheros Communications Inc. |
| 3 | * Copyright (c) 2014, I2SE GmbH |
| 4 | * |
| 5 | * Permission to use, copy, modify, and/or distribute this software |
| 6 | * for any purpose with or without fee is hereby granted, provided |
| 7 | * that the above copyright notice and this permission notice appear |
| 8 | * in all copies. |
| 9 | * |
| 10 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL |
| 11 | * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED |
| 12 | * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL |
| 13 | * THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR |
| 14 | * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM |
| 15 | * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, |
| 16 | * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN |
| 17 | * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 18 | */ |
| 19 | |
| 20 | /* Atheros Ethernet framing. Every Ethernet frame is surrounded by an atheros |
| 21 | * frame while transmitted over a serial channel. |
| 22 | */ |
| 23 | |
| 24 | #ifndef _QCA_FRAMING_H |
| 25 | #define _QCA_FRAMING_H |
| 26 | |
| 27 | #include <linux/if_ether.h> |
| 28 | #include <linux/if_vlan.h> |
| 29 | #include <linux/types.h> |
| 30 | |
| 31 | /* Frame is currently being received */ |
| 32 | #define QCAFRM_GATHER 0 |
| 33 | |
| 34 | /* No header byte while expecting it */ |
| 35 | #define QCAFRM_NOHEAD (QCAFRM_ERR_BASE - 1) |
| 36 | |
| 37 | /* No tailer byte while expecting it */ |
| 38 | #define QCAFRM_NOTAIL (QCAFRM_ERR_BASE - 2) |
| 39 | |
| 40 | /* Frame length is invalid */ |
| 41 | #define QCAFRM_INVLEN (QCAFRM_ERR_BASE - 3) |
| 42 | |
| 43 | /* Frame length is invalid */ |
| 44 | #define QCAFRM_INVFRAME (QCAFRM_ERR_BASE - 4) |
| 45 | |
| 46 | /* Min/Max Ethernet MTU */ |
| 47 | #define QCAFRM_ETHMINMTU 46 |
| 48 | #define QCAFRM_ETHMAXMTU 1500 |
| 49 | |
| 50 | /* Min/Max frame lengths */ |
| 51 | #define QCAFRM_ETHMINLEN (QCAFRM_ETHMINMTU + ETH_HLEN) |
| 52 | #define QCAFRM_ETHMAXLEN (QCAFRM_ETHMAXMTU + VLAN_ETH_HLEN) |
| 53 | |
| 54 | /* QCA7K header len */ |
| 55 | #define QCAFRM_HEADER_LEN 8 |
| 56 | |
| 57 | /* QCA7K footer len */ |
| 58 | #define QCAFRM_FOOTER_LEN 2 |
| 59 | |
| 60 | /* QCA7K Framing. */ |
| 61 | #define QCAFRM_ERR_BASE -1000 |
| 62 | |
| 63 | enum qcafrm_state { |
| 64 | QCAFRM_HW_LEN0 = 0x8000, |
| 65 | QCAFRM_HW_LEN1 = QCAFRM_HW_LEN0 - 1, |
| 66 | QCAFRM_HW_LEN2 = QCAFRM_HW_LEN1 - 1, |
| 67 | QCAFRM_HW_LEN3 = QCAFRM_HW_LEN2 - 1, |
| 68 | |
| 69 | /* Waiting first 0xAA of header */ |
| 70 | QCAFRM_WAIT_AA1 = QCAFRM_HW_LEN3 - 1, |
| 71 | |
| 72 | /* Waiting second 0xAA of header */ |
| 73 | QCAFRM_WAIT_AA2 = QCAFRM_WAIT_AA1 - 1, |
| 74 | |
| 75 | /* Waiting third 0xAA of header */ |
| 76 | QCAFRM_WAIT_AA3 = QCAFRM_WAIT_AA2 - 1, |
| 77 | |
| 78 | /* Waiting fourth 0xAA of header */ |
| 79 | QCAFRM_WAIT_AA4 = QCAFRM_WAIT_AA3 - 1, |
| 80 | |
| 81 | /* Waiting Byte 0-1 of length (litte endian) */ |
| 82 | QCAFRM_WAIT_LEN_BYTE0 = QCAFRM_WAIT_AA4 - 1, |
| 83 | QCAFRM_WAIT_LEN_BYTE1 = QCAFRM_WAIT_AA4 - 2, |
| 84 | |
| 85 | /* Reserved bytes */ |
| 86 | QCAFRM_WAIT_RSVD_BYTE1 = QCAFRM_WAIT_AA4 - 3, |
| 87 | QCAFRM_WAIT_RSVD_BYTE2 = QCAFRM_WAIT_AA4 - 4, |
| 88 | |
| 89 | /* The frame length is used as the state until |
| 90 | * the end of the Ethernet frame |
| 91 | * Waiting for first 0x55 of footer |
| 92 | */ |
| 93 | QCAFRM_WAIT_551 = 1, |
| 94 | |
| 95 | /* Waiting for second 0x55 of footer */ |
| 96 | QCAFRM_WAIT_552 = QCAFRM_WAIT_551 - 1 |
| 97 | }; |
| 98 | |
| 99 | /* Structure to maintain the frame decoding during reception. */ |
| 100 | |
| 101 | struct qcafrm_handle { |
| 102 | /* Current decoding state */ |
| 103 | enum qcafrm_state state; |
| 104 | |
| 105 | /* Offset in buffer (borrowed for length too) */ |
| 106 | s16 offset; |
| 107 | |
| 108 | /* Frame length as kept by this module */ |
| 109 | u16 len; |
| 110 | }; |
| 111 | |
| 112 | u16 qcafrm_create_header(u8 *buf, u16 len); |
| 113 | |
| 114 | u16 qcafrm_create_footer(u8 *buf); |
| 115 | |
| 116 | static inline void qcafrm_fsm_init(struct qcafrm_handle *handle) |
| 117 | { |
| 118 | handle->state = QCAFRM_HW_LEN0; |
| 119 | } |
| 120 | |
| 121 | /* Gather received bytes and try to extract a full Ethernet frame |
| 122 | * by following a simple state machine. |
| 123 | * |
| 124 | * Return: QCAFRM_GATHER No Ethernet frame fully received yet. |
| 125 | * QCAFRM_NOHEAD Header expected but not found. |
| 126 | * QCAFRM_INVLEN QCA7K frame length is invalid |
| 127 | * QCAFRM_NOTAIL Footer expected but not found. |
| 128 | * > 0 Number of byte in the fully received |
| 129 | * Ethernet frame |
| 130 | */ |
| 131 | |
| 132 | s32 qcafrm_fsm_decode(struct qcafrm_handle *handle, u8 *buf, u16 buf_len, u8 recv_byte); |
| 133 | |
| 134 | #endif /* _QCA_FRAMING_H */ |