cfserl.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. #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. #define STXLEN(layr) (layr->usestx ? 1 : 0)
  25. static int cfserl_receive(struct cflayer *layr, struct cfpkt *pkt);
  26. static int cfserl_transmit(struct cflayer *layr, struct cfpkt *pkt);
  27. static void cfserl_ctrlcmd(struct cflayer *layr, enum caif_ctrlcmd ctrl,
  28. int phyid);
  29. struct cflayer *cfserl_create(int type, int instance, bool use_stx)
  30. {
  31. struct cfserl *this = kmalloc(sizeof(struct cfserl), GFP_ATOMIC);
  32. if (!this) {
  33. pr_warn("Out of memory\n");
  34. return NULL;
  35. }
  36. caif_assert(offsetof(struct cfserl, layer) == 0);
  37. memset(this, 0, sizeof(struct cfserl));
  38. this->layer.receive = cfserl_receive;
  39. this->layer.transmit = cfserl_transmit;
  40. this->layer.ctrlcmd = cfserl_ctrlcmd;
  41. this->layer.type = type;
  42. this->usestx = use_stx;
  43. spin_lock_init(&this->sync);
  44. snprintf(this->layer.name, CAIF_LAYER_NAME_SZ, "ser1");
  45. return &this->layer;
  46. }
  47. static int cfserl_receive(struct cflayer *l, struct cfpkt *newpkt)
  48. {
  49. struct cfserl *layr = container_obj(l);
  50. u16 pkt_len;
  51. struct cfpkt *pkt = NULL;
  52. struct cfpkt *tail_pkt = NULL;
  53. u8 tmp8;
  54. u16 tmp;
  55. u8 stx = CFSERL_STX;
  56. int ret;
  57. u16 expectlen = 0;
  58. caif_assert(newpkt != NULL);
  59. spin_lock(&layr->sync);
  60. if (layr->incomplete_frm != NULL) {
  61. layr->incomplete_frm =
  62. cfpkt_append(layr->incomplete_frm, newpkt, expectlen);
  63. pkt = layr->incomplete_frm;
  64. if (pkt == NULL) {
  65. spin_unlock(&layr->sync);
  66. return -ENOMEM;
  67. }
  68. } else {
  69. pkt = newpkt;
  70. }
  71. layr->incomplete_frm = NULL;
  72. do {
  73. /* Search for STX at start of pkt if STX is used */
  74. if (layr->usestx) {
  75. cfpkt_extr_head(pkt, &tmp8, 1);
  76. if (tmp8 != CFSERL_STX) {
  77. while (cfpkt_more(pkt)
  78. && tmp8 != CFSERL_STX) {
  79. cfpkt_extr_head(pkt, &tmp8, 1);
  80. }
  81. if (!cfpkt_more(pkt)) {
  82. cfpkt_destroy(pkt);
  83. layr->incomplete_frm = NULL;
  84. spin_unlock(&layr->sync);
  85. return -EPROTO;
  86. }
  87. }
  88. }
  89. pkt_len = cfpkt_getlen(pkt);
  90. /*
  91. * pkt_len is the accumulated length of the packet data
  92. * we have received so far.
  93. * Exit if frame doesn't hold length.
  94. */
  95. if (pkt_len < 2) {
  96. if (layr->usestx)
  97. cfpkt_add_head(pkt, &stx, 1);
  98. layr->incomplete_frm = pkt;
  99. spin_unlock(&layr->sync);
  100. return 0;
  101. }
  102. /*
  103. * Find length of frame.
  104. * expectlen is the length we need for a full frame.
  105. */
  106. cfpkt_peek_head(pkt, &tmp, 2);
  107. expectlen = le16_to_cpu(tmp) + 2;
  108. /*
  109. * Frame error handling
  110. */
  111. if (expectlen < SERIAL_MINIUM_PACKET_SIZE
  112. || expectlen > SERIAL_MAX_FRAMESIZE) {
  113. if (!layr->usestx) {
  114. if (pkt != NULL)
  115. cfpkt_destroy(pkt);
  116. layr->incomplete_frm = NULL;
  117. expectlen = 0;
  118. spin_unlock(&layr->sync);
  119. return -EPROTO;
  120. }
  121. continue;
  122. }
  123. if (pkt_len < expectlen) {
  124. /* Too little received data */
  125. if (layr->usestx)
  126. cfpkt_add_head(pkt, &stx, 1);
  127. layr->incomplete_frm = pkt;
  128. spin_unlock(&layr->sync);
  129. return 0;
  130. }
  131. /*
  132. * Enough data for at least one frame.
  133. * Split the frame, if too long
  134. */
  135. if (pkt_len > expectlen)
  136. tail_pkt = cfpkt_split(pkt, expectlen);
  137. else
  138. tail_pkt = NULL;
  139. /* Send the first part of packet upwards.*/
  140. spin_unlock(&layr->sync);
  141. ret = layr->layer.up->receive(layr->layer.up, pkt);
  142. spin_lock(&layr->sync);
  143. if (ret == -EILSEQ) {
  144. if (layr->usestx) {
  145. if (tail_pkt != NULL)
  146. pkt = cfpkt_append(pkt, tail_pkt, 0);
  147. /* Start search for next STX if frame failed */
  148. continue;
  149. } else {
  150. cfpkt_destroy(pkt);
  151. pkt = NULL;
  152. }
  153. }
  154. pkt = tail_pkt;
  155. } while (pkt != NULL);
  156. spin_unlock(&layr->sync);
  157. return 0;
  158. }
  159. static int cfserl_transmit(struct cflayer *layer, struct cfpkt *newpkt)
  160. {
  161. struct cfserl *layr = container_obj(layer);
  162. int ret;
  163. u8 tmp8 = CFSERL_STX;
  164. if (layr->usestx)
  165. cfpkt_add_head(newpkt, &tmp8, 1);
  166. ret = layer->dn->transmit(layer->dn, newpkt);
  167. if (ret < 0)
  168. cfpkt_extr_head(newpkt, &tmp8, 1);
  169. return ret;
  170. }
  171. static void cfserl_ctrlcmd(struct cflayer *layr, enum caif_ctrlcmd ctrl,
  172. int phyid)
  173. {
  174. layr->up->ctrlcmd(layr->up, ctrl, phyid);
  175. }