cfserl.c 4.4 KB

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