2 * Copyright (c) 2011, 2012, Atheros Communications Inc.
3 * Copyright (c) 2014, I2SE GmbH
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
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.
20 /* Atheros ethernet framing. Every Ethernet frame is surrounded
21 * by an atheros frame while transmitted over a serial channel;
24 #include <linux/init.h>
25 #include <linux/kernel.h>
26 #include <linux/module.h>
28 #include "qca_7k_common.h"
31 qcafrm_create_header(u8 *buf, u16 length)
38 len = cpu_to_le16(length);
45 buf[5] = (len >> 8) & 0xff;
49 return QCAFRM_HEADER_LEN;
51 EXPORT_SYMBOL_GPL(qcafrm_create_header);
54 qcafrm_create_footer(u8 *buf)
61 return QCAFRM_FOOTER_LEN;
63 EXPORT_SYMBOL_GPL(qcafrm_create_footer);
65 /* Gather received bytes and try to extract a full ethernet frame by
66 * following a simple state machine.
68 * Return: QCAFRM_GATHER No ethernet frame fully received yet.
69 * QCAFRM_NOHEAD Header expected but not found.
70 * QCAFRM_INVLEN Atheros frame length is invalid
71 * QCAFRM_NOTAIL Footer expected but not found.
72 * > 0 Number of byte in the fully received
77 qcafrm_fsm_decode(struct qcafrm_handle *handle, u8 *buf, u16 buf_len, u8 recv_byte)
79 s32 ret = QCAFRM_GATHER;
82 switch (handle->state) {
85 /* by default, just go to next state */
88 if (recv_byte != 0x00) {
89 /* first two bytes of length must be 0 */
90 handle->state = handle->init;
97 /* 4 bytes header pattern */
100 case QCAFRM_WAIT_AA3:
101 case QCAFRM_WAIT_AA4:
102 if (recv_byte != 0xAA) {
104 handle->state = handle->init;
109 /* 2 bytes length. */
110 /* Borrow offset field to hold length for now. */
111 case QCAFRM_WAIT_LEN_BYTE0:
112 handle->offset = recv_byte;
113 handle->state = QCAFRM_WAIT_LEN_BYTE1;
115 case QCAFRM_WAIT_LEN_BYTE1:
116 handle->offset = handle->offset | (recv_byte << 8);
117 handle->state = QCAFRM_WAIT_RSVD_BYTE1;
119 case QCAFRM_WAIT_RSVD_BYTE1:
120 handle->state = QCAFRM_WAIT_RSVD_BYTE2;
122 case QCAFRM_WAIT_RSVD_BYTE2:
123 len = handle->offset;
124 if (len > buf_len || len < QCAFRM_MIN_LEN) {
126 handle->state = handle->init;
128 handle->state = (enum qcafrm_state)(len + 1);
129 /* Remaining number of bytes. */
134 /* Receiving Ethernet frame itself. */
135 buf[handle->offset] = recv_byte;
139 case QCAFRM_WAIT_551:
140 if (recv_byte != 0x55) {
142 handle->state = handle->init;
144 handle->state = QCAFRM_WAIT_552;
147 case QCAFRM_WAIT_552:
148 if (recv_byte != 0x55) {
150 handle->state = handle->init;
152 ret = handle->offset;
153 /* Frame is fully received. */
154 handle->state = handle->init;
161 EXPORT_SYMBOL_GPL(qcafrm_fsm_decode);
163 MODULE_DESCRIPTION("Qualcomm Atheros QCA7000 common");
164 MODULE_AUTHOR("Qualcomm Atheros Communications");
166 MODULE_LICENSE("Dual BSD/GPL");