cfdgml.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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/cfsrvl.h>
  12. #include <net/caif/cfpkt.h>
  13. #define container_obj(layr) ((struct cfsrvl *) layr)
  14. #define DGM_CMD_BIT 0x80
  15. #define DGM_FLOW_OFF 0x81
  16. #define DGM_FLOW_ON 0x80
  17. #define DGM_MTU 1500
  18. static int cfdgml_receive(struct cflayer *layr, struct cfpkt *pkt);
  19. static int cfdgml_transmit(struct cflayer *layr, struct cfpkt *pkt);
  20. struct cflayer *cfdgml_create(u8 channel_id, struct dev_info *dev_info)
  21. {
  22. struct cfsrvl *dgm = kzalloc(sizeof(struct cfsrvl), GFP_ATOMIC);
  23. if (!dgm)
  24. return NULL;
  25. caif_assert(offsetof(struct cfsrvl, layer) == 0);
  26. cfsrvl_init(dgm, channel_id, dev_info, true);
  27. dgm->layer.receive = cfdgml_receive;
  28. dgm->layer.transmit = cfdgml_transmit;
  29. snprintf(dgm->layer.name, CAIF_LAYER_NAME_SZ - 1, "dgm%d", channel_id);
  30. dgm->layer.name[CAIF_LAYER_NAME_SZ - 1] = '\0';
  31. return &dgm->layer;
  32. }
  33. static int cfdgml_receive(struct cflayer *layr, struct cfpkt *pkt)
  34. {
  35. u8 cmd = -1;
  36. u8 dgmhdr[3];
  37. int ret;
  38. caif_assert(layr->up != NULL);
  39. caif_assert(layr->receive != NULL);
  40. caif_assert(layr->ctrlcmd != NULL);
  41. if (cfpkt_extr_head(pkt, &cmd, 1) < 0) {
  42. pr_err("Packet is erroneous!\n");
  43. cfpkt_destroy(pkt);
  44. return -EPROTO;
  45. }
  46. if ((cmd & DGM_CMD_BIT) == 0) {
  47. if (cfpkt_extr_head(pkt, &dgmhdr, 3) < 0) {
  48. pr_err("Packet is erroneous!\n");
  49. cfpkt_destroy(pkt);
  50. return -EPROTO;
  51. }
  52. ret = layr->up->receive(layr->up, pkt);
  53. return ret;
  54. }
  55. switch (cmd) {
  56. case DGM_FLOW_OFF: /* FLOW OFF */
  57. layr->ctrlcmd(layr, CAIF_CTRLCMD_FLOW_OFF_IND, 0);
  58. cfpkt_destroy(pkt);
  59. return 0;
  60. case DGM_FLOW_ON: /* FLOW ON */
  61. layr->ctrlcmd(layr, CAIF_CTRLCMD_FLOW_ON_IND, 0);
  62. cfpkt_destroy(pkt);
  63. return 0;
  64. default:
  65. cfpkt_destroy(pkt);
  66. pr_info("Unknown datagram control %d (0x%x)\n", cmd, cmd);
  67. return -EPROTO;
  68. }
  69. }
  70. static int cfdgml_transmit(struct cflayer *layr, struct cfpkt *pkt)
  71. {
  72. u8 packet_type;
  73. u32 zero = 0;
  74. struct caif_payload_info *info;
  75. struct cfsrvl *service = container_obj(layr);
  76. int ret;
  77. if (!cfsrvl_ready(service, &ret)) {
  78. cfpkt_destroy(pkt);
  79. return ret;
  80. }
  81. /* STE Modem cannot handle more than 1500 bytes datagrams */
  82. if (cfpkt_getlen(pkt) > DGM_MTU) {
  83. cfpkt_destroy(pkt);
  84. return -EMSGSIZE;
  85. }
  86. cfpkt_add_head(pkt, &zero, 3);
  87. packet_type = 0x08; /* B9 set - UNCLASSIFIED */
  88. cfpkt_add_head(pkt, &packet_type, 1);
  89. /* Add info for MUX-layer to route the packet out. */
  90. info = cfpkt_info(pkt);
  91. info->channel_id = service->layer.id;
  92. /* To optimize alignment, we add up the size of CAIF header
  93. * before payload.
  94. */
  95. info->hdr_len = 4;
  96. info->dev_info = &service->dev_info;
  97. return layr->dn->transmit(layr->dn, pkt);
  98. }