diagchar_hdlc.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /* Copyright (c) 2008-2009, 2012-2013, The Linux Foundation.
  2. * All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 and
  6. * only version 2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. */
  14. #include <linux/init.h>
  15. #include <linux/module.h>
  16. #include <linux/cdev.h>
  17. #include <linux/fs.h>
  18. #include <linux/device.h>
  19. #include <linux/uaccess.h>
  20. #include <linux/ratelimit.h>
  21. #include <linux/crc-ccitt.h>
  22. #include "diagchar_hdlc.h"
  23. #include "diagchar.h"
  24. MODULE_LICENSE("GPL v2");
  25. #define CRC_16_L_SEED 0xFFFF
  26. #define CRC_16_L_STEP(xx_crc, xx_c) \
  27. crc_ccitt_byte(xx_crc, xx_c)
  28. void diag_hdlc_encode(struct diag_send_desc_type *src_desc,
  29. struct diag_hdlc_dest_type *enc)
  30. {
  31. uint8_t *dest;
  32. uint8_t *dest_last;
  33. const uint8_t *src;
  34. const uint8_t *src_last;
  35. uint16_t crc;
  36. unsigned char src_byte = 0;
  37. enum diag_send_state_enum_type state;
  38. unsigned int used = 0;
  39. if (src_desc && enc) {
  40. /* Copy parts to local variables. */
  41. src = src_desc->pkt;
  42. src_last = src_desc->last;
  43. state = src_desc->state;
  44. dest = enc->dest;
  45. dest_last = enc->dest_last;
  46. if (state == DIAG_STATE_START) {
  47. crc = CRC_16_L_SEED;
  48. state++;
  49. } else {
  50. /* Get a local copy of the CRC */
  51. crc = enc->crc;
  52. }
  53. /* dest or dest_last may be NULL to trigger a
  54. state transition only */
  55. if (dest && dest_last) {
  56. /* This condition needs to include the possibility
  57. of 2 dest bytes for an escaped byte */
  58. while (src <= src_last && dest <= dest_last) {
  59. src_byte = *src++;
  60. if ((src_byte == CONTROL_CHAR) ||
  61. (src_byte == ESC_CHAR)) {
  62. /* If the escape character is not the
  63. last byte */
  64. if (dest != dest_last) {
  65. crc = CRC_16_L_STEP(crc,
  66. src_byte);
  67. *dest++ = ESC_CHAR;
  68. used++;
  69. *dest++ = src_byte
  70. ^ ESC_MASK;
  71. used++;
  72. } else {
  73. src--;
  74. break;
  75. }
  76. } else {
  77. crc = CRC_16_L_STEP(crc, src_byte);
  78. *dest++ = src_byte;
  79. used++;
  80. }
  81. }
  82. if (src > src_last) {
  83. if (state == DIAG_STATE_BUSY) {
  84. if (src_desc->terminate) {
  85. crc = ~crc;
  86. state++;
  87. } else {
  88. /* Done with fragment */
  89. state = DIAG_STATE_COMPLETE;
  90. }
  91. }
  92. while (dest <= dest_last &&
  93. state >= DIAG_STATE_CRC1 &&
  94. state < DIAG_STATE_TERM) {
  95. /* Encode a byte of the CRC next */
  96. src_byte = crc & 0xFF;
  97. if ((src_byte == CONTROL_CHAR)
  98. || (src_byte == ESC_CHAR)) {
  99. if (dest != dest_last) {
  100. *dest++ = ESC_CHAR;
  101. used++;
  102. *dest++ = src_byte ^
  103. ESC_MASK;
  104. used++;
  105. crc >>= 8;
  106. } else {
  107. break;
  108. }
  109. } else {
  110. crc >>= 8;
  111. *dest++ = src_byte;
  112. used++;
  113. }
  114. state++;
  115. }
  116. if (state == DIAG_STATE_TERM) {
  117. if (dest_last >= dest) {
  118. *dest++ = CONTROL_CHAR;
  119. used++;
  120. state++; /* Complete */
  121. }
  122. }
  123. }
  124. }
  125. /* Copy local variables back into the encode structure. */
  126. enc->dest = dest;
  127. enc->dest_last = dest_last;
  128. enc->crc = crc;
  129. src_desc->pkt = src;
  130. src_desc->last = src_last;
  131. src_desc->state = state;
  132. }
  133. return;
  134. }
  135. int diag_hdlc_decode(struct diag_hdlc_decode_type *hdlc)
  136. {
  137. uint8_t *src_ptr = NULL, *dest_ptr = NULL;
  138. unsigned int src_length = 0, dest_length = 0;
  139. unsigned int len = 0;
  140. unsigned int i;
  141. uint8_t src_byte;
  142. int pkt_bnd = 0;
  143. int msg_start;
  144. if (hdlc && hdlc->src_ptr && hdlc->dest_ptr &&
  145. (hdlc->src_size > hdlc->src_idx) &&
  146. (hdlc->dest_size > hdlc->dest_idx)) {
  147. msg_start = (hdlc->src_idx == 0) ? 1 : 0;
  148. src_ptr = hdlc->src_ptr;
  149. src_ptr = &src_ptr[hdlc->src_idx];
  150. src_length = hdlc->src_size - hdlc->src_idx;
  151. dest_ptr = hdlc->dest_ptr;
  152. dest_ptr = &dest_ptr[hdlc->dest_idx];
  153. dest_length = hdlc->dest_size - hdlc->dest_idx;
  154. for (i = 0; i < src_length; i++) {
  155. src_byte = src_ptr[i];
  156. if (hdlc->escaping) {
  157. dest_ptr[len++] = src_byte ^ ESC_MASK;
  158. hdlc->escaping = 0;
  159. } else if (src_byte == ESC_CHAR) {
  160. if (i == (src_length - 1)) {
  161. hdlc->escaping = 1;
  162. i++;
  163. break;
  164. } else {
  165. dest_ptr[len++] = src_ptr[++i]
  166. ^ ESC_MASK;
  167. }
  168. } else if (src_byte == CONTROL_CHAR) {
  169. if (msg_start && i == 0 && src_length > 1)
  170. continue;
  171. /* Byte 0x7E will be considered
  172. as end of packet */
  173. dest_ptr[len++] = src_byte;
  174. i++;
  175. pkt_bnd = 1;
  176. break;
  177. } else {
  178. dest_ptr[len++] = src_byte;
  179. }
  180. if (len >= dest_length) {
  181. i++;
  182. break;
  183. }
  184. }
  185. hdlc->src_idx += i;
  186. hdlc->dest_idx += len;
  187. }
  188. return pkt_bnd;
  189. }
  190. int crc_check(uint8_t *buf, uint16_t len)
  191. {
  192. uint16_t crc = CRC_16_L_SEED;
  193. uint8_t sent_crc[2] = {0, 0};
  194. /*
  195. * The minimum length of a valid incoming packet is 4. 1 byte
  196. * of data and 3 bytes for CRC
  197. */
  198. if (!buf || len < 4) {
  199. pr_err_ratelimited("diag: In %s, invalid packet or length, buf: 0x%x, len: %d",
  200. __func__, (int)buf, len);
  201. return -EIO;
  202. }
  203. /*
  204. * Run CRC check for the original input. Skip the last 3 CRC
  205. * bytes
  206. */
  207. crc = crc_ccitt(crc, buf, len-3);
  208. crc ^= CRC_16_L_SEED;
  209. /* Check the computed CRC against the original CRC bytes. */
  210. sent_crc[0] = buf[len-3];
  211. sent_crc[1] = buf[len-2];
  212. if (crc != *((uint16_t *)sent_crc)) {
  213. pr_debug("diag: In %s, crc mismatch. expected: %x, sent %x.\n",
  214. __func__, crc, *((uint16_t *)sent_crc));
  215. return -EIO;
  216. }
  217. return 0;
  218. }