cfserl.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. * Copyright (C) ST-Ericsson AB 2010
  3. * Author: Sjur Brendeland
  4. * License terms: GNU General Public License (GPL) version 2
  5. */
  6. #define pr_fmt(fmt) KBUILD_MODNAME ":%s(): " fmt, __func__
  7. #include <linux/stddef.h>
  8. #include <linux/spinlock.h>
  9. #include <linux/slab.h>
  10. #include <net/caif/caif_layer.h>
  11. #include <net/caif/cfpkt.h>
  12. #include <net/caif/cfserl.h>
  13. #define container_obj(layr) ((struct cfserl *) layr)
  14. #define CFSERL_STX 0x02
  15. #define SERIAL_MINIUM_PACKET_SIZE 4
  16. #define SERIAL_MAX_FRAMESIZE 4096
  17. struct cfserl {
  18. struct cflayer layer;
  19. struct cfpkt *incomplete_frm;
  20. /* Protects parallel processing of incoming packets */
  21. spinlock_t sync;
  22. bool usestx;
  23. };
  24. static int cfserl_receive(struct cflayer *layr, struct cfpkt *pkt);
  25. static int cfserl_transmit(struct cflayer *layr, struct cfpkt *pkt);
  26. static void cfserl_ctrlcmd(struct cflayer *layr, enum caif_ctrlcmd ctrl,
  27. int phyid);
  28. struct cflayer *cfserl_create(int instance, bool use_stx)
  29. {
  30. struct cfserl *this = kzalloc(sizeof(struct cfserl), GFP_ATOMIC);
  31. if (!this)
  32. return NULL;
  33. caif_assert(offsetof(struct cfserl, layer) == 0);
  34. this->layer.receive = cfserl_receive;
  35. this->layer.transmit = cfserl_transmit;
  36. this->layer.ctrlcmd = cfserl_ctrlcmd;
  37. this->usestx = use_stx;
  38. spin_lock_init(&this->sync);
  39. snprintf(this->layer.name, CAIF_LAYER_NAME_SZ, "ser1");
  40. return &this->layer;
  41. }
  42. static int cfserl_receive(struct cflayer *l, struct cfpkt *newpkt)
  43. {
  44. struct cfserl *layr = container_obj(l);
  45. u16 pkt_len;
  46. struct cfpkt *pkt = NULL;
  47. struct cfpkt *tail_pkt = NULL;
  48. u8 tmp8;
  49. u16 tmp;
  50. u8 stx = CFSERL_STX;
  51. int ret;
  52. u16 expectlen = 0;
  53. caif_assert(newpkt != NULL);
  54. spin_lock(&layr->sync);
  55. if (layr->incomplete_frm != NULL) {
  56. layr->incomplete_frm =
  57. cfpkt_append(layr->incomplete_frm, newpkt, expectlen);
  58. pkt = layr->incomplete_frm;
  59. if (pkt == NULL) {
  60. spin_unlock(&layr->sync);
  61. return -ENOMEM;
  62. }
  63. } else {
  64. pkt = newpkt;
  65. }
  66. layr->incomplete_frm = NULL;
  67. do {
  68. /* Search for STX at start of pkt if STX is used */
  69. if (layr->usestx) {
  70. cfpkt_extr_head(pkt, &tmp8, 1);
  71. if (tmp8 != CFSERL_STX) {
  72. while (cfpkt_more(pkt)
  73. && tmp8 != CFSERL_STX) {
  74. cfpkt_extr_head(pkt, &tmp8, 1);
  75. }
  76. if (!cfpkt_more(pkt)) {
  77. cfpkt_destroy(pkt);
  78. layr->incomplete_frm = NULL;
  79. spin_unlock(&layr->sync);
  80. return -EPROTO;
  81. }
  82. }
  83. }
  84. pkt_len = cfpkt_getlen(pkt);
  85. /*
  86. * pkt_len is the accumulated length of the packet data
  87. * we have received so far.
  88. * Exit if frame doesn't hold length.
  89. */
  90. if (pkt_len < 2) {
  91. if (layr->usestx)
  92. cfpkt_add_head(pkt, &stx, 1);
  93. layr->incomplete_frm = pkt;
  94. spin_unlock(&layr->sync);
  95. return 0;
  96. }
  97. /*
  98. * Find length of frame.
  99. * expectlen is the length we need for a full frame.
  100. */
  101. cfpkt_peek_head(pkt, &tmp, 2);
  102. expectlen = le16_to_cpu(tmp) + 2;
  103. /*
  104. * Frame error handling
  105. */
  106. if (expectlen < SERIAL_MINIUM_PACKET_SIZE
  107. || expectlen > SERIAL_MAX_FRAMESIZE) {
  108. if (!layr->usestx) {
  109. if (pkt != NULL)
  110. cfpkt_destroy(pkt);
  111. layr->incomplete_frm = NULL;
  112. expectlen = 0;
  113. spin_unlock(&layr->sync);
  114. return -EPROTO;
  115. }
  116. continue;
  117. }
  118. if (pkt_len < expectlen) {
  119. /* Too little received data */
  120. if (layr->usestx)
  121. cfpkt_add_head(pkt, &stx, 1);
  122. layr->incomplete_frm = pkt;
  123. spin_unlock(&layr->sync);
  124. return 0;
  125. }
  126. /*
  127. * Enough data for at least one frame.
  128. * Split the frame, if too long
  129. */
  130. if (pkt_len > expectlen)
  131. tail_pkt = cfpkt_split(pkt, expectlen);
  132. else
  133. tail_pkt = NULL;
  134. /* Send the first part of packet upwards.*/
  135. spin_unlock(&layr->sync);
  136. ret = layr->layer.up->receive(layr->layer.up, pkt);
  137. spin_lock(&layr->sync);
  138. if (ret == -EILSEQ) {
  139. if (layr->usestx) {
  140. if (tail_pkt != NULL)
  141. pkt = cfpkt_append(pkt, tail_pkt, 0);
  142. /* Start search for next STX if frame failed */
  143. continue;
  144. } else {
  145. cfpkt_destroy(pkt);
  146. pkt = NULL;
  147. }
  148. }
  149. pkt = tail_pkt;
  150. } while (pkt != NULL);
  151. spin_unlock(&layr->sync);
  152. return 0;
  153. }
  154. static int cfserl_transmit(struct cflayer *layer, struct cfpkt *newpkt)
  155. {
  156. struct cfserl *layr = container_obj(layer);
  157. u8 tmp8 = CFSERL_STX;
  158. if (layr->usestx)
  159. cfpkt_add_head(newpkt, &tmp8, 1);
  160. return layer->dn->transmit(layer->dn, newpkt);
  161. }
  162. static void cfserl_ctrlcmd(struct cflayer *layr, enum caif_ctrlcmd ctrl,
  163. int phyid)
  164. {
  165. layr->up->ctrlcmd(layr->up, ctrl, phyid);
  166. }