Linux Audio

Check our new training course

Loading...
v6.13.7
  1// SPDX-License-Identifier: GPL-2.0 OR BSD-2-Clause
  2/*
  3 *   Copyright (c) 2011, 2012, Atheros Communications Inc.
  4 *   Copyright (c) 2014, I2SE GmbH
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  5 */
  6
  7/*   Atheros ethernet framing. Every Ethernet frame is surrounded
  8 *   by an atheros frame while transmitted over a serial channel;
  9 */
 10
 11#include <linux/init.h>
 12#include <linux/kernel.h>
 13#include <linux/module.h>
 14
 15#include "qca_7k_common.h"
 16
 17u16
 18qcafrm_create_header(u8 *buf, u16 length)
 19{
 20	__le16 len;
 21
 22	if (!buf)
 23		return 0;
 24
 25	len = cpu_to_le16(length);
 26
 27	buf[0] = 0xAA;
 28	buf[1] = 0xAA;
 29	buf[2] = 0xAA;
 30	buf[3] = 0xAA;
 31	buf[4] = len & 0xff;
 32	buf[5] = (len >> 8) & 0xff;
 33	buf[6] = 0;
 34	buf[7] = 0;
 35
 36	return QCAFRM_HEADER_LEN;
 37}
 38EXPORT_SYMBOL_GPL(qcafrm_create_header);
 39
 40u16
 41qcafrm_create_footer(u8 *buf)
 42{
 43	if (!buf)
 44		return 0;
 45
 46	buf[0] = 0x55;
 47	buf[1] = 0x55;
 48	return QCAFRM_FOOTER_LEN;
 49}
 50EXPORT_SYMBOL_GPL(qcafrm_create_footer);
 51
 52/*   Gather received bytes and try to extract a full ethernet frame by
 53 *   following a simple state machine.
 54 *
 55 * Return:   QCAFRM_GATHER       No ethernet frame fully received yet.
 56 *           QCAFRM_NOHEAD       Header expected but not found.
 57 *           QCAFRM_INVLEN       Atheros frame length is invalid
 58 *           QCAFRM_NOTAIL       Footer expected but not found.
 59 *           > 0                 Number of byte in the fully received
 60 *                               Ethernet frame
 61 */
 62
 63s32
 64qcafrm_fsm_decode(struct qcafrm_handle *handle, u8 *buf, u16 buf_len, u8 recv_byte)
 65{
 66	s32 ret = QCAFRM_GATHER;
 67	u16 len;
 68
 69	switch (handle->state) {
 70	case QCAFRM_HW_LEN0:
 71	case QCAFRM_HW_LEN1:
 72		/* by default, just go to next state */
 73		handle->state--;
 74
 75		if (recv_byte != 0x00) {
 76			/* first two bytes of length must be 0 */
 77			handle->state = handle->init;
 78		}
 79		break;
 80	case QCAFRM_HW_LEN2:
 81	case QCAFRM_HW_LEN3:
 82		handle->state--;
 83		break;
 84	/* 4 bytes header pattern */
 85	case QCAFRM_WAIT_AA1:
 86	case QCAFRM_WAIT_AA2:
 87	case QCAFRM_WAIT_AA3:
 88	case QCAFRM_WAIT_AA4:
 89		if (recv_byte != 0xAA) {
 90			ret = QCAFRM_NOHEAD;
 91			handle->state = handle->init;
 92		} else {
 93			handle->state--;
 94		}
 95		break;
 96		/* 2 bytes length. */
 97		/* Borrow offset field to hold length for now. */
 98	case QCAFRM_WAIT_LEN_BYTE0:
 99		handle->offset = recv_byte;
100		handle->state = QCAFRM_WAIT_LEN_BYTE1;
101		break;
102	case QCAFRM_WAIT_LEN_BYTE1:
103		handle->offset = handle->offset | (recv_byte << 8);
104		handle->state = QCAFRM_WAIT_RSVD_BYTE1;
105		break;
106	case QCAFRM_WAIT_RSVD_BYTE1:
107		handle->state = QCAFRM_WAIT_RSVD_BYTE2;
108		break;
109	case QCAFRM_WAIT_RSVD_BYTE2:
110		len = handle->offset;
111		if (len > buf_len || len < QCAFRM_MIN_LEN) {
112			ret = QCAFRM_INVLEN;
113			handle->state = handle->init;
114		} else {
115			handle->state = (enum qcafrm_state)(len + 1);
116			/* Remaining number of bytes. */
117			handle->offset = 0;
118		}
119		break;
120	default:
121		/* Receiving Ethernet frame itself. */
122		buf[handle->offset] = recv_byte;
123		handle->offset++;
124		handle->state--;
125		break;
126	case QCAFRM_WAIT_551:
127		if (recv_byte != 0x55) {
128			ret = QCAFRM_NOTAIL;
129			handle->state = handle->init;
130		} else {
131			handle->state = QCAFRM_WAIT_552;
132		}
133		break;
134	case QCAFRM_WAIT_552:
135		if (recv_byte != 0x55) {
136			ret = QCAFRM_NOTAIL;
137			handle->state = handle->init;
138		} else {
139			ret = handle->offset;
140			/* Frame is fully received. */
141			handle->state = handle->init;
142		}
143		break;
144	}
145
146	return ret;
147}
148EXPORT_SYMBOL_GPL(qcafrm_fsm_decode);
149
150MODULE_DESCRIPTION("Qualcomm Atheros QCA7000 common");
151MODULE_AUTHOR("Qualcomm Atheros Communications");
152MODULE_AUTHOR("Stefan Wahren <wahrenst@gmx.net>");
153MODULE_LICENSE("Dual BSD/GPL");
v6.2
 
  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
 21 *   by an atheros frame while transmitted over a serial channel;
 22 */
 23
 24#include <linux/init.h>
 25#include <linux/kernel.h>
 26#include <linux/module.h>
 27
 28#include "qca_7k_common.h"
 29
 30u16
 31qcafrm_create_header(u8 *buf, u16 length)
 32{
 33	__le16 len;
 34
 35	if (!buf)
 36		return 0;
 37
 38	len = cpu_to_le16(length);
 39
 40	buf[0] = 0xAA;
 41	buf[1] = 0xAA;
 42	buf[2] = 0xAA;
 43	buf[3] = 0xAA;
 44	buf[4] = len & 0xff;
 45	buf[5] = (len >> 8) & 0xff;
 46	buf[6] = 0;
 47	buf[7] = 0;
 48
 49	return QCAFRM_HEADER_LEN;
 50}
 51EXPORT_SYMBOL_GPL(qcafrm_create_header);
 52
 53u16
 54qcafrm_create_footer(u8 *buf)
 55{
 56	if (!buf)
 57		return 0;
 58
 59	buf[0] = 0x55;
 60	buf[1] = 0x55;
 61	return QCAFRM_FOOTER_LEN;
 62}
 63EXPORT_SYMBOL_GPL(qcafrm_create_footer);
 64
 65/*   Gather received bytes and try to extract a full ethernet frame by
 66 *   following a simple state machine.
 67 *
 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
 73 *                               Ethernet frame
 74 */
 75
 76s32
 77qcafrm_fsm_decode(struct qcafrm_handle *handle, u8 *buf, u16 buf_len, u8 recv_byte)
 78{
 79	s32 ret = QCAFRM_GATHER;
 80	u16 len;
 81
 82	switch (handle->state) {
 83	case QCAFRM_HW_LEN0:
 84	case QCAFRM_HW_LEN1:
 85		/* by default, just go to next state */
 86		handle->state--;
 87
 88		if (recv_byte != 0x00) {
 89			/* first two bytes of length must be 0 */
 90			handle->state = handle->init;
 91		}
 92		break;
 93	case QCAFRM_HW_LEN2:
 94	case QCAFRM_HW_LEN3:
 95		handle->state--;
 96		break;
 97	/* 4 bytes header pattern */
 98	case QCAFRM_WAIT_AA1:
 99	case QCAFRM_WAIT_AA2:
100	case QCAFRM_WAIT_AA3:
101	case QCAFRM_WAIT_AA4:
102		if (recv_byte != 0xAA) {
103			ret = QCAFRM_NOHEAD;
104			handle->state = handle->init;
105		} else {
106			handle->state--;
107		}
108		break;
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;
114		break;
115	case QCAFRM_WAIT_LEN_BYTE1:
116		handle->offset = handle->offset | (recv_byte << 8);
117		handle->state = QCAFRM_WAIT_RSVD_BYTE1;
118		break;
119	case QCAFRM_WAIT_RSVD_BYTE1:
120		handle->state = QCAFRM_WAIT_RSVD_BYTE2;
121		break;
122	case QCAFRM_WAIT_RSVD_BYTE2:
123		len = handle->offset;
124		if (len > buf_len || len < QCAFRM_MIN_LEN) {
125			ret = QCAFRM_INVLEN;
126			handle->state = handle->init;
127		} else {
128			handle->state = (enum qcafrm_state)(len + 1);
129			/* Remaining number of bytes. */
130			handle->offset = 0;
131		}
132		break;
133	default:
134		/* Receiving Ethernet frame itself. */
135		buf[handle->offset] = recv_byte;
136		handle->offset++;
137		handle->state--;
138		break;
139	case QCAFRM_WAIT_551:
140		if (recv_byte != 0x55) {
141			ret = QCAFRM_NOTAIL;
142			handle->state = handle->init;
143		} else {
144			handle->state = QCAFRM_WAIT_552;
145		}
146		break;
147	case QCAFRM_WAIT_552:
148		if (recv_byte != 0x55) {
149			ret = QCAFRM_NOTAIL;
150			handle->state = handle->init;
151		} else {
152			ret = handle->offset;
153			/* Frame is fully received. */
154			handle->state = handle->init;
155		}
156		break;
157	}
158
159	return ret;
160}
161EXPORT_SYMBOL_GPL(qcafrm_fsm_decode);
162
163MODULE_DESCRIPTION("Qualcomm Atheros QCA7000 common");
164MODULE_AUTHOR("Qualcomm Atheros Communications");
165MODULE_AUTHOR("Stefan Wahren <stefan.wahren@i2se.com>");
166MODULE_LICENSE("Dual BSD/GPL");