cfutill.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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/kernel.h>
  8. #include <linux/types.h>
  9. #include <linux/slab.h>
  10. #include <linux/errno.h>
  11. #include <net/caif/caif_layer.h>
  12. #include <net/caif/cfsrvl.h>
  13. #include <net/caif/cfpkt.h>
  14. #define container_obj(layr) ((struct cfsrvl *) layr)
  15. #define UTIL_PAYLOAD 0x00
  16. #define UTIL_CMD_BIT 0x80
  17. #define UTIL_REMOTE_SHUTDOWN 0x82
  18. #define UTIL_FLOW_OFF 0x81
  19. #define UTIL_FLOW_ON 0x80
  20. static int cfutill_receive(struct cflayer *layr, struct cfpkt *pkt);
  21. static int cfutill_transmit(struct cflayer *layr, struct cfpkt *pkt);
  22. struct cflayer *cfutill_create(u8 channel_id, struct dev_info *dev_info)
  23. {
  24. struct cfsrvl *util = kzalloc(sizeof(struct cfsrvl), GFP_ATOMIC);
  25. if (!util)
  26. return NULL;
  27. caif_assert(offsetof(struct cfsrvl, layer) == 0);
  28. cfsrvl_init(util, channel_id, dev_info, true);
  29. util->layer.receive = cfutill_receive;
  30. util->layer.transmit = cfutill_transmit;
  31. snprintf(util->layer.name, CAIF_LAYER_NAME_SZ - 1, "util1");
  32. return &util->layer;
  33. }
  34. static int cfutill_receive(struct cflayer *layr, struct cfpkt *pkt)
  35. {
  36. u8 cmd = -1;
  37. struct cfsrvl *service = container_obj(layr);
  38. caif_assert(layr != NULL);
  39. caif_assert(layr->up != NULL);
  40. caif_assert(layr->up->receive != NULL);
  41. caif_assert(layr->up->ctrlcmd != NULL);
  42. if (cfpkt_extr_head(pkt, &cmd, 1) < 0) {
  43. pr_err("Packet is erroneous!\n");
  44. cfpkt_destroy(pkt);
  45. return -EPROTO;
  46. }
  47. switch (cmd) {
  48. case UTIL_PAYLOAD:
  49. return layr->up->receive(layr->up, pkt);
  50. case UTIL_FLOW_OFF:
  51. layr->ctrlcmd(layr, CAIF_CTRLCMD_FLOW_OFF_IND, 0);
  52. cfpkt_destroy(pkt);
  53. return 0;
  54. case UTIL_FLOW_ON:
  55. layr->ctrlcmd(layr, CAIF_CTRLCMD_FLOW_ON_IND, 0);
  56. cfpkt_destroy(pkt);
  57. return 0;
  58. case UTIL_REMOTE_SHUTDOWN: /* Remote Shutdown Request */
  59. pr_err("REMOTE SHUTDOWN REQUEST RECEIVED\n");
  60. layr->ctrlcmd(layr, CAIF_CTRLCMD_REMOTE_SHUTDOWN_IND, 0);
  61. service->open = false;
  62. cfpkt_destroy(pkt);
  63. return 0;
  64. default:
  65. cfpkt_destroy(pkt);
  66. pr_warn("Unknown service control %d (0x%x)\n", cmd, cmd);
  67. return -EPROTO;
  68. }
  69. }
  70. static int cfutill_transmit(struct cflayer *layr, struct cfpkt *pkt)
  71. {
  72. u8 zero = 0;
  73. struct caif_payload_info *info;
  74. int ret;
  75. struct cfsrvl *service = container_obj(layr);
  76. caif_assert(layr != NULL);
  77. caif_assert(layr->dn != NULL);
  78. caif_assert(layr->dn->transmit != NULL);
  79. if (!cfsrvl_ready(service, &ret)) {
  80. cfpkt_destroy(pkt);
  81. return ret;
  82. }
  83. cfpkt_add_head(pkt, &zero, 1);
  84. /* Add info for MUX-layer to route the packet out. */
  85. info = cfpkt_info(pkt);
  86. info->channel_id = service->layer.id;
  87. /*
  88. * To optimize alignment, we add up the size of CAIF header before
  89. * payload.
  90. */
  91. info->hdr_len = 1;
  92. info->dev_info = &service->dev_info;
  93. return layr->dn->transmit(layr->dn, pkt);
  94. }