qca_framing.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. /* Atheros ethernet framing. Every Ethernet frame is surrounded
  20. * by an atheros frame while transmitted over a serial channel;
  21. */
  22. #include <linux/kernel.h>
  23. #include "qca_framing.h"
  24. u16
  25. qcafrm_create_header(u8 *buf, u16 length)
  26. {
  27. __le16 len;
  28. if (!buf)
  29. return 0;
  30. len = cpu_to_le16(length);
  31. buf[0] = 0xAA;
  32. buf[1] = 0xAA;
  33. buf[2] = 0xAA;
  34. buf[3] = 0xAA;
  35. buf[4] = len & 0xff;
  36. buf[5] = (len >> 8) & 0xff;
  37. buf[6] = 0;
  38. buf[7] = 0;
  39. return QCAFRM_HEADER_LEN;
  40. }
  41. u16
  42. qcafrm_create_footer(u8 *buf)
  43. {
  44. if (!buf)
  45. return 0;
  46. buf[0] = 0x55;
  47. buf[1] = 0x55;
  48. return QCAFRM_FOOTER_LEN;
  49. }
  50. /* Gather received bytes and try to extract a full ethernet frame by
  51. * following a simple state machine.
  52. *
  53. * Return: QCAFRM_GATHER No ethernet frame fully received yet.
  54. * QCAFRM_NOHEAD Header expected but not found.
  55. * QCAFRM_INVLEN Atheros frame length is invalid
  56. * QCAFRM_NOTAIL Footer expected but not found.
  57. * > 0 Number of byte in the fully received
  58. * Ethernet frame
  59. */
  60. s32
  61. qcafrm_fsm_decode(struct qcafrm_handle *handle, u8 *buf, u16 buf_len, u8 recv_byte)
  62. {
  63. s32 ret = QCAFRM_GATHER;
  64. u16 len;
  65. switch (handle->state) {
  66. case QCAFRM_HW_LEN0:
  67. case QCAFRM_HW_LEN1:
  68. /* by default, just go to next state */
  69. handle->state--;
  70. if (recv_byte != 0x00) {
  71. /* first two bytes of length must be 0 */
  72. handle->state = QCAFRM_HW_LEN0;
  73. }
  74. break;
  75. case QCAFRM_HW_LEN2:
  76. case QCAFRM_HW_LEN3:
  77. handle->state--;
  78. break;
  79. /* 4 bytes header pattern */
  80. case QCAFRM_WAIT_AA1:
  81. case QCAFRM_WAIT_AA2:
  82. case QCAFRM_WAIT_AA3:
  83. case QCAFRM_WAIT_AA4:
  84. if (recv_byte != 0xAA) {
  85. ret = QCAFRM_NOHEAD;
  86. handle->state = QCAFRM_HW_LEN0;
  87. } else {
  88. handle->state--;
  89. }
  90. break;
  91. /* 2 bytes length. */
  92. /* Borrow offset field to hold length for now. */
  93. case QCAFRM_WAIT_LEN_BYTE0:
  94. handle->offset = recv_byte;
  95. handle->state = QCAFRM_WAIT_LEN_BYTE1;
  96. break;
  97. case QCAFRM_WAIT_LEN_BYTE1:
  98. handle->offset = handle->offset | (recv_byte << 8);
  99. handle->state = QCAFRM_WAIT_RSVD_BYTE1;
  100. break;
  101. case QCAFRM_WAIT_RSVD_BYTE1:
  102. handle->state = QCAFRM_WAIT_RSVD_BYTE2;
  103. break;
  104. case QCAFRM_WAIT_RSVD_BYTE2:
  105. len = handle->offset;
  106. if (len > buf_len || len < QCAFRM_ETHMINLEN) {
  107. ret = QCAFRM_INVLEN;
  108. handle->state = QCAFRM_HW_LEN0;
  109. } else {
  110. handle->state = (enum qcafrm_state)(len + 1);
  111. /* Remaining number of bytes. */
  112. handle->offset = 0;
  113. }
  114. break;
  115. default:
  116. /* Receiving Ethernet frame itself. */
  117. buf[handle->offset] = recv_byte;
  118. handle->offset++;
  119. handle->state--;
  120. break;
  121. case QCAFRM_WAIT_551:
  122. if (recv_byte != 0x55) {
  123. ret = QCAFRM_NOTAIL;
  124. handle->state = QCAFRM_HW_LEN0;
  125. } else {
  126. handle->state = QCAFRM_WAIT_552;
  127. }
  128. break;
  129. case QCAFRM_WAIT_552:
  130. if (recv_byte != 0x55) {
  131. ret = QCAFRM_NOTAIL;
  132. handle->state = QCAFRM_HW_LEN0;
  133. } else {
  134. ret = handle->offset;
  135. /* Frame is fully received. */
  136. handle->state = QCAFRM_HW_LEN0;
  137. }
  138. break;
  139. }
  140. return ret;
  141. }