cfserl.c 4.4 KB

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