qca_7k_common.c 4.2 KB

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